FFmpeg3最新的解码接口avcodec_send_packet和avcodec_receive_frame

FFmpeg编解码处理2-编解码API详解 ***//*** @return*//***//***/1.2 API使用说明1.2.1 解码API使用详解关于 avcodec_send_packet() 与 avcodec_receive_frame() 的使用说明:按 dts 递增的顺序向解码器送入编码帧 packet,解码器按 pts 递增的顺序输出原始帧 frame,实际上解码器不关注输入 packe t的 dts(错值都没关系),它只管依次处理收到的 packet,按需缓冲和解码。 阅读详情

原文地址:http://www.bubuko.com/infodetail-2106896.html

ffmpeg3版本的解码接口做了不少调整,之前的视频解码接口avcodec_decode_video2和avcodec_decode_audio4音频解码被设置为deprecated,对这两个接口做了合并,使用统一的接口。并且将音视频解码步骤分为了两步,第一步avcodec_send_packet,第二步avcodec_receive_frame,通过接口名字我们就可以知道第一步是发送编码数据包,第二步是接收解码后数据。新版本是否只是做了接口的变化,还有有哪些我们需要注意的事项,我们来分析一下。

首先我们先看一下这两个接口。

avcodec_send_packet

  • 接口源码

/**
 * Supply raw packet data as input to a decoder.
 *
 * Internally, this call will copy relevant AVCodecContext fields, which can
 * influence decoding per-packet, and apply them when the packet is actually
 * decoded. (For example AVCodecContext.skip_frame, which might direct the
 * decoder to drop the frame contained by the packet sent with this function.)
 *
 * @warning The input buffer, avpkt->data must be AV_INPUT_BUFFER_PADDING_SIZE
 *          larger than the actual read bytes because some optimized bitstream
 *          readers read 32 or 64 bits at once and could read over the end.
 *
 * @warning Do not mix this API with the legacy API (like avcodec_decode_video2())
 *          on the same AVCodecContext. It will return unexpected results now
 *          or in future libavcodec versions.
 *
 * @note The AVCodecContext MUST have been opened with @ref avcodec_open2()
 *       before packets may be fed to the decoder.
 *
 * @param avctx codec context
 * @param[in] avpkt The input AVPacket. Usually, this will be a single video
 *                  frame, or several complete audio frames.
 *                  Ownership of the packet remains with the caller, and the
 *                  decoder will not write to the packet. The decoder may create
 *                  a reference to the packet data (or copy it if the packet is
 *                  not reference-counted).
 *                  Unlike with older APIs, the packet is always fully consumed,
 *                  and if it contains multiple frames (e.g. some audio codecs),
 *                  will require you to call avcodec_receive_frame() multiple
 *                  times afterwards before you can send a new packet.
 *                  It can be NULL (or an AVPacket with data set to NULL and
 *                  size set to 0); in this case, it is considered a flush
 *                  packet, which signals the end of the stream. Sending the
 *                  first flush packet will return success. Subsequent ones are
 *                  unnecessary and will return AVERROR_EOF. If the decoder
 *                  still has frames buffered, it will return them after sending
 *                  a flush packet.
 *
 * @return 0 on success, otherwise negative error code:
 *      AVERROR(EAGAIN):   input is not accepted right now - the packet must be
 *                         resent after trying to read output
 *      AVERROR_EOF:       the decoder has been flushed, and no new packets can
 *                         be sent to it (also returned if more than 1 flush
 *                         packet is sent)
 *      AVERROR(EINVAL):   codec not opened, it is an encoder, or requires flush
 *      AVERROR(ENOMEM):   failed to add packet to internal queue, or similar
 *      other errors: legitimate decoding errors

 */

int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt);
  • 参数分析

AVCodecContext *avctx:第一个参数与旧的接口一致,是视频解码的上下文,包含解码器。

const AVPacket *avpkt: 编码的音视频帧数据

  • 为什么要传递空的avpkt

这里有一个说明是可以传递NULL,什么情况下需要传递NULL,你平时看一些视频播放器,播放经常会少最后几帧,很多情况就是因为没有处理好缓冲帧的问题,ffmpeg内部会缓冲几帧,要想取出来就需要传递空的AVPacket进去。

avcodec_receive_frame

  • 接口源码

/**
 * Return decoded output data from a decoder.
 *
 * @param avctx codec context
 * @param frame This will be set to a reference-counted video or audio
 *              frame (depending on the decoder type) allocated by the
 *              decoder. Note that the function will always call
 *              av_frame_unref(frame) before doing anything else.
 *
 * @return
 *      0:                 success, a frame was returned
 *      AVERROR(EAGAIN):   output is not available right now - user must try
 *                         to send new input
 *      AVERROR_EOF:       the decoder has been fully flushed, and there will be
 *                         no more output frames
 *      AVERROR(EINVAL):   codec not opened, or it is an encoder
 *      other negative values: legitimate decoding errors

 */

int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame);


  • 参数分析

AVCodecContext *avctx:第一个参数视频解码的上下文,与上面接口一致。

AVFrame *frame:解码后的视频帧数据。

  • 空间申请和释放问题

解码后图像空间由函数内部申请,你所做的只需要分配 AVFrame 对象空间,如果你每次调用avcodec_receive_frame传递同一个对象,接口内部会判断空间是否已经分配,如果没有分配会在函数内部分配。

avcodec_send_packet和avcodec_receive_frame调用关系并不一定是一对一的,比如一些音频数据一个AVPacket中包含了1秒钟的音频,调用一次avcodec_send_packet之后,可能需要调用25次 avcodec_receive_frame才能获取全部的解码音频数据,所以要做如下处理:

int re = avcodec_send_packet(codec, pkt);

if (re != 0)

{

    return;

}

while( avcodec_receive_frame(codec, frame) >= 0)

{

    //读取到一帧音频或者视频

    //处理解码后音视频 frame

}


更多的资料也可以关注我51CTO上的视频课程

夏老师的课堂 http://edu.51cto.com/lecturer/index/user_id-12016059.html

手把手教您开发视频播放器http://edu.51cto.com/course/course_id-8059.html

转载这篇文章之前并没有使用while去接收数据,一直没成功过,终于知道原因了

FFmpeg源码分析:avcodec_send_packet()与avcodec_receive_frame()音视频解码 FFmpeg在libavcodec模块,旧版本提供avcodec_decode_video2()作为视频解码函数,avcodec_decode_audio4()作为音频解码函数。在FFmpeg 3.1版本新增avcodec_send_packet()与avcodec_receive_frame()作为音视频解码函数。后来,在3.4版本把avcodec_decode_video2()和avcodec_decode_audio4()标记为过时API。 阅读详情

相关推荐

FFmpeg 源码分析 - avcodec_send_packet 和 avcodec_receive_frame

使用 从 FFmpeg 3.x 开始,avcodec_decode_video2 就被废弃了,取而代之的是 avcodec_send_packet 和 avcodec_receive_frame。使用方法很简单,可以查看 ffmpeg 源码文件夹 ffmpeg-< version>/doc/example/decode_video.c 下的代码,这里摘抄关键部分如下: static v...

我的学习笔记 1万+

FFmepg avcodec_receive_frame函数分析

int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame);/*brief:从×××中获取解码的输出数据*/@参数 avctx 编码上下文@参数 frame 这将会指向从×××分配的一个引用计数的视频或者音频帧(取决于解码类型)@注意该函数在处理其他事情之前会调用av_frame_unref(frame)@...

weixin_34032621的博客 1293

FFmpeg源码分析:avcodec_send_frame()和avcodec_receive_packet()音视频编码

FFmpeg在libavcodec模块,旧版本提供avcodec_encode_video2()作为视频编码函数,avcodec_encode_audio2()作为音频编码函数。在FFmpeg 3.1版本新增avcodec_send_frame()与avcodec_receive_packet()作为音视频编码函数。后来,在3.4版本把avcodec_encode_video2()和avcodec_encode_audio2()标记为过时API。

徐福记456 8506

FFmpeg3最新的解码接口avcodec_send_packet和avcodec_receive_frame分析

      ffmpeg3版本的解码接口做了不少调整,之前的视频解码接口avcodec_decode_video2和avcodec_decode_audio4音频解码被设置为deprecated,对这两个接口做了合并,使用统一的接口。并且将音视频解码步骤分为了两步,第一步avcodec_send_packet,第二步avcodec_receive_frame,通过接口名字我们就可以知道第一步是...

老夏课堂-夏曹俊的技术专栏 1万+

【FFmpeg】avcodec_send_frame函数

avcodec_send_frame函数的主要功能是将外部输入的frame送入到编码器中实现编码,函数的实现位于libavcodec\encode.c中,从代码看,主要进行的流程为: (1)输入信息avctx的检查 (2)如果输入的帧frame不存在,设置draining为1,随后将输入的frame存入avctx的buffer中,存入的过程使用encode_send_frame_internal实现 (3)将frame送入编码器进行编码,使用encode_receive_packet_internal实现

weixin_42877471的博客 2925

avcodec_send_packet()/ avcodec_receive_frame()

ffmpeg中解码的API之前的是avcodec_decode_video2()和avcodec_decode_audio4(),现在使用avcodec_send_packet()/ avcodec_receive_frame()来代替原有的接口。 API与编码/解码和音频/视频非常相似,工作原理如下: 1、像往常一样设置和打开AVCodecContext。

qq_35044535的博客 5531

【FFMPEG】AVERROR(EAGAIN)含义

继续调用avcodec_send_packet发送数据,直到解码数据足够为止。而是代表解码数据不足,需要提供更多的AVPacket才能解码出数据。这是一个常见的错误码,它实际上并不是真的报错。

天天向上 Up Up Up (*^▽^*) 2226

Infineon MOSFET开关损耗计算实战指南

本文是英飞凌MOSFET开关损耗计算的实战指南。针对MOSFET发热问题,文章深入剖析了开关损耗的产生机理,并基于Infineon数据手册的关键参数,手把手推导了开通与关断损耗的工程计算公式。同时,指南强调了栅极电阻、寄生参数等实际因素对损耗的影响,并提供了从理论计算到实测优化的完整路径,帮助工程师精准评估与优化电源设计。

weixin_29212867的博客 453

FFMpeg学习笔记

ffmpeg学习笔记

asiwxy的博客 1729

【飞控】MATLAB中UAV Toolbox Support Package for PX4 Autopilots和Pixhawk Pilot Support Package (PSP) 的关系?

区分UAV Toolbox Support Package for PX4 Autopilots和Pixhawk Pilot Support Package (PSP)

m0_58373406的博客 746

损失函数大汇总(四十五)(clDice Loss附公式推导与代码)

clDice Loss是一种针对细长结构分割任务的拓扑保持损失函数,通过比较预测区域和真实区域的中心线重叠情况来评估分割质量。与传统Dice Loss关注区域重叠不同,clDice Loss重点考察连通性和骨架拓扑结构,能有效避免血管断裂、分支错误连接等问题。该损失函数包含两个核心指标:预测骨架在真实区域中的覆盖比例(Topology Precision)和真实骨架被预测区域覆盖的比例(Topology Sensitivity),通过调和平均得到最终评估值。由于传统骨架提取算法不可导,实际实现采用可微的so

悦学共鸣,温柔以待,汇聚光芒,共同成长 456

ffmpeg avcodec_send_packet avcodec_receive_frame解码接口使用讲解

解码demo 通常来说,一个packet会被解码出一个frame,不过也存在一个packet被解码出多个frame或者多个packet才能解码出一个frame的情况,甚至也有些解码器在输入以及输出端上可能会有延迟。因此原来的API在某种程度上存在对调用者误导的可能,使得调用者认为输入的一个或者多个Packet就对应着解码器所输出的一个frame,但实际上可能并非如此。 新的API完全隐藏了“解码”这一概念,只提供一个输入packet的接口以及输出frame的接口,如此一来调用者可以不必了解解码器的具体细节,

weixin_41176628的博客 7573

硅电容的工艺结构原理及选型参数总结

硅电容,也称为硅质电容或硅二极管电容,是一种特殊的电容器。它利用硅二极管的结构来实现电容的功能。本文对硅电容的工作原理,结构特点,选型参数及设计注意事项进行总结。

fydar的博客 2345

工作小记 ffmpeg avcodec_receive_frame 缓存问题

ffmpeg 解码缓存问题

饭真好吃的博客 3048

【图像超分】论文精读:Residual Dense Network for Image Super-Resolution(RDN)

一个非常深的卷积神经网络 (CNN) 最近在图像分类 (SR) 方面取得了巨大成功,并提供了分层特征。然而,大多数基于深度 CNN 的 SR 模型没有充分利用原始低分辨率 (LR) 图像中的层次特征,从而实现相对较低的性能。在本文中,我们提出了一种新的残差密集网络(RDN)来解决图像SR中的这个问题。我们充分利用所有卷积层的分层特征。具体来说,我们提出了残差密集块(RDB)通过密集连接的卷积层提取丰富的局部特征。RDB 进一步允许将前一个 RDB 的状态与当前 RDB 的所有层直接连接,从而产生连续的内存

主要更新底层视觉(去噪、超分等)相关的科研内容,形式为【论文精读】+【论文复现】 7265

Linux系列:SecurtCRT中的密码如何找回

【代码】Linux系列:SecurtCRT中的密码如何找回。

NIO4444 1094

标注 avcodec_send_packet 和 avcodec_receive_frame 函数

本文分析了FFmpeg 6.1中avcodec_send_packet和avcodec_receive_frame两个核心解码函数的实现原理。通过代码标注发现,avcodec_send_packet主要将外部AVPacket引用到内部buffer_pkt,并调用decode_receive_frame_internal接收帧数据到内部buffer_frame;avcodec_receive_frame则负责将内部buffer_frame移动到输出参数或再次调用接收函数。底层解码流程涉及内存分配、帧处理等操作

hjjdebug的专栏 1021

小波变换(深入浅出)

小波变换

weixin_46713695的博客 3万+

FFmpeg 核心 API 系列:av_read_frame / avcodec_send_packet / avcodec_receive_frame

详细介绍ffmpeg中的av_read_frame/avcodec_send_packet/avcodec_receive_frame

筏的博客 1080

用映射把 Semantic Object 说清楚:Fiori Elements 出站导航中的 Mapping Semantic Objects 全面实战

本文系统讲解了Fiori Elements中出站导航的Mapping机制,它用于解决源应用与目标应用间字段名不一致的问题。文章详细介绍了Mapping在XML、ABAP CDS和CAP CDS三种注解中的具体写法,并阐述了其运行时行为:Fiori Launchpad通过Target Mapping将参数注入目标应用。同时分析了Mapping与Filter Bar的交互规则,指出空值传递的条件,并提供了扩展方法adaptNavigationParameterExtension。最后通过从Travel到Pass

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 181

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

FFmpeg3最新的视频解码avcodec_send_packet和avcodec_receive_frame

#include <jni.h> #include <android/log.h> #include <libavcodec/avcodec.h> #include <android/native_window_jni.h> #include <libavformat/avformat.h> #include <libavutil...

Snow_Ice_Yang的博客 1995

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

上一篇: OCX客户端异常System.TypeInitializationException
下一篇: C# Log4net日志代码配置解决OCX不写日志问题
boonya
boonya 领域专家: 后端开发技术领域 领域专家: 后端开发技术领域
博客等级 码龄16年 1万+粉丝 963原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值