文章目录

  • I . FFMPEG 解码 AVPacket 数据到 AVFrame 前置操作
  • II . FFMPEG 解码 AVPacket 数据到 AVFrame 流程
  • III . FFMPEG 发送 AVPacket 数据包给编解码器 ( AVPacket->解码器 )
  • IV . FFMPEG AVPacket 内存释放
  • V . FFMPEG 初始化 AVFrame 结构体
  • VI . FFMPEG 解码器 AVCodec 接收并解码 AVPacket 数据到 AVFrame 中
  • VII . FFMPEG 解码 AVPacket 数据到 AVFrame 部分代码示例

I . FFMPEG 解码 AVPacket 数据到 AVFrame 前置操作


FFMPEG 解码 AVPacket 数据到 AVFrame 数据前置操作 :

① FFMPEG 初始化 : 参考博客 【Android FFMPEG 开发】FFMPEG 初始化 ( 网络初始化 | 打开音视频 | 查找音视频流 )

② FFMPEG 获取 AVStream 音视频流 : 参考博客 【Android FFMPEG 开发】FFMPEG 获取 AVStream 音视频流 ( AVFormatContext 结构体 | 获取音视频流信息 | 获取音视频流个数 | 获取音视频流 )

③ FFMPEG 获取 AVCodec 编解码器 : 参考博客 【Android FFMPEG 开发】FFMPEG 获取编解码器 ( 获取编解码参数 | 查找编解码器 | 获取编解码器上下文 | 设置上下文参数 | 打开编解码器 )

④ FFMPEG 读取音视频流中的数据到 AVPacket : 参考博客 【Android FFMPEG 开发】FFMPEG 读取音视频流中的数据到 AVPacket ( 初始化 AVPacket 数据 | 读取 AVPacket )

II . FFMPEG 解码 AVPacket 数据到 AVFrame 流程


FFMPEG 解码 AVPacket 数据到 AVFrame 流程 :

〇 前置操作 : FFMPEG 环境初始化 , 获取 AVStream 音视频流 , 获取 AVCodec 编解码器 , 读取音视频流中的数据到 AVPacket , 然后才能进行下面的操作 ;

① 发送 AVPacket 数据包给编解码器 : int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)

int result_send_packet = avcodec_send_packet(avCodecContext, avPacket);

② 释放 AVPacket 内存 : void av_packet_free(AVPacket **pkt) , AVPacket 数据包解码后 , 就没用了 , 执行完该步骤以后 , 马上将 AVPacket 释放掉 , 以免占用内存 ;

av_packet_free(&avPacket);

③ 初始化 AVFrame 结构体 : AVFrame *av_frame_alloc ( void ) , 该结构体用于存储解码后的数据 , 可以直接用于音视频播放 ;

AVFrame *avFrame = av_frame_alloc();

④ 解码器接收并解码 AVPacket 数据到 AVFrame 中 : int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)

int result_receive_frame = avcodec_receive_frame(avCodecContext, avFrame);

III . FFMPEG 发送 AVPacket 数据包给编解码器 ( AVPacket->解码器 )


1 . 发送 AVPacket 数据 : 从 AVStream 音视频流中取出 AVPacket 数据包 , 这个数据是经过压缩编码后的数据 , 无法直接使用 , 还需要将其发送到解码器解码后 , 才能使用 ; 发送 AVPacket 数据到解码器的方法是 avcodec_send_packet ( ) ;

2 . avcodec_send_packet ( ) 函数原型 : 向解码器发送未解码的数据 , 这些数据需要解码 ;

① AVCodecContext *avctx 参数 : 解码器上下文 , 从音视频流中查找编解码器 , 从编解码器中获取编解码器上下文 , 该参数中存储了音视频流格式相关信息 , 该参数是在之前使用 avformat_find_stream_info ( ) 方法获取的 ;

② const AVPacket *avpkt 参数 : 需要解码的数据包 ;

③ int 返回值 : 返回 0 成功 , 其它失败 ; 只要失败 , 直接退出即可 ;

/*** 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 in the current state - user*                         must read output with avcodec_receive_frame() (once*                         all output is read, the packet should be resent, and*                         the call will not fail with EAGAIN).*      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);

3 . FFMPEG 发送 AVPacket 数据包给编解码器 代码示例 :

/**  ① 发送数据包将数据包发送给解码器 , 返回 0 成功 , 其它失败*/
int result_send_packet = avcodec_send_packet(avCodecContext, avPacket);//失败处理
if(result_send_packet != 0){//TODO 发送失败处理
}

IV . FFMPEG AVPacket 内存释放


1 . AVPacket 内存释放 : AVPacket 解码压缩后的数据 , 发送给解码器之后 , 就没有用了 , 这里要及时释放 AVPacket 结构体所占用的内存 , 以免出现内存泄漏的情况 ;

2 . AVPacket 初始化与释放 : AVPacket 结构体不管是初始化 , 还是释放 , 都必须使用 FFMPEG 提供的方法 ;

① AVPacket 初始化 : 调用 AVPacket *av_packet_alloc(void) 方法 ;

② AVPacket 释放 : 调用 void av_packet_free(AVPacket **pkt) 方法 ;

3 . av_packet_free ( ) 函数原型 : 传入 AVPacket ** 二维指针参数 , 该结构体释放后 , 其指针指向也要被修改 ;

① AVPacket **pkt 参数 : 该二维指针指向 AVPacket * 结构体指针 , 在该方法中释放其内存 , 并指向 NULL ;

/*** Free the packet, if the packet is reference counted, it will be* unreferenced first.** @param pkt packet to be freed. The pointer will be set to NULL.* @note passing NULL is a no-op.*/
void av_packet_free(AVPacket **pkt);

4 . FFMPEG AVPacket 内存释放 示例代码 :

//AVPacket* avPacket
av_packet_free(&avPacket);

V . FFMPEG 初始化 AVFrame 结构体


1 . AVFrame 结构体 : AVFrame 结构体存储解码后的数据 , 该数据可以直接用于播放音视频 ;

2 . AVFrame 结构体使用 : 首先要初始化 AVFrame 结构体 , 该结构体的初始化和释放 , 同样也要使用 FFMPEG 提供的相应的方法 ;

① AVFrame 初始化方法 : AVFrame *av_frame_alloc(void)

/*** Allocate an AVFrame and set its fields to default values.  The resulting* struct must be freed using av_frame_free().** @return An AVFrame filled with default values or NULL on failure.** @note this only allocates the AVFrame itself, not the data buffers. Those* must be allocated through other means, e.g. with av_frame_get_buffer() or* manually.*/
AVFrame *av_frame_alloc(void);

② AVFrame 释放方法 : void av_frame_free(AVFrame **frame)

/*** Free the frame and any dynamically allocated objects in it,* e.g. extended_data. If the frame is reference counted, it will be* unreferenced first.** @param frame frame to be freed. The pointer will be set to NULL.*/
void av_frame_free(AVFrame **frame);

3 . FFMPEG AVFrame 结构体初始化 代码示例 :

//用于存放解码后的数据包 , 一个 AVFrame 代表一个图像
AVFrame *avFrame = av_frame_alloc();

VI . FFMPEG 解码器 AVCodec 接收并解码 AVPacket 数据到 AVFrame 中


1 . 前置操作 : 在上面的步骤中 , 将 AVPacket 未解码的数据发送给了解码器 , 又初始化了 AVFrame 结构体 ;

2 . 解码过程 : 在本步骤中 , 将初始化好的 AVFrame 设置给解码器 , 解码器解码完成后 , 将解码后的音视频数据存放到 AVFrame 结构体中 , 之后就可以进行播放操作了 ;

3 . 解码方法 : 调用 int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame) 方法 , 即可将 AVFrame 设置给解码器 , 用于接收解码后的数据帧 ;

4 . avcodec_receive_frame ( ) 函数原型 :

① AVCodecContext *avctx 参数 : 解码器上下文 , 从音视频流中查找编解码器 , 从编解码器中获取编解码器上下文 , 该参数中存储了音视频流格式相关信息 , 该参数是在之前使用 avformat_find_stream_info ( ) 方法获取的 ;

② AVFrame *frame : 初始化好的 AVFrame 结构体指针 ;

③ int 返回值 : 返回 0 说明解码成功 , 否则失败 ; 返回 AVERROR(EAGAIN) , 当前状态没有输出 , 需要输入更多数据 ; 返回 AVERROR_EOF , 解码器中没有数据 , 已经读取到结尾 ; 返回 AVERROR(EINVAL) , 解码器没有打开 ;

/*** 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 in this state - 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);

5 . FFMPEG 解码器 AVCodec 接收并解码 AVPacket 数据到 AVFrame 代码示例 :

//解码器中将数据包解码后 , 存放到 AVFrame * 中 , 这里将其取出并解码
//  返回 AVERROR(EAGAIN) : 当前状态没有输出 , 需要输入更多数据
//  返回 AVERROR_EOF : 解码器中没有数据 , 已经读取到结尾
//  返回 AVERROR(EINVAL) : 解码器没有打开
int result_receive_frame = avcodec_receive_frame(avCodecContext, avFrame);

VII . FFMPEG 解码 AVPacket 数据到 AVFrame 部分代码示例


/**  1 . 发送数据包将数据包发送给解码器 , 返回 0 成功 , 其它失败AVERROR(EAGAIN): 说明当前解码器满了 , 不能接受新的数据包了这里先将解码器的数据都处理了, 才能接收新数据其它错误处理 : 直接退出循环*/
int result_send_packet = avcodec_send_packet(avCodecContext, avPacket);//2 . 本次循环中 , 将 AVPacket 丢到解码器中解码完毕后 , 就可以释放 AVPacket 内存了
av_packet_free(&avPacket);if(result_send_packet != 0){//TODO 失败处理
}//3 . 接收并解码数据包 , 存放在 AVFrame 中
//用于存放解码后的数据包 , 一个 AVFrame 代表一个图像
AVFrame *avFrame = av_frame_alloc();//4 . 解码器中将数据包解码后 , 存放到 AVFrame * 中 , 这里将其取出并解码
//  返回 AVERROR(EAGAIN) : 当前状态没有输出 , 需要输入更多数据
//  返回 AVERROR_EOF : 解码器中没有数据 , 已经读取到结尾
//  返回 AVERROR(EINVAL) : 解码器没有打开
int result_receive_frame = avcodec_receive_frame(avCodecContext, avFrame);
//失败处理
if(result_receive_frame != 0){//TODO 失败处理
}

【Android FFMPEG 开发】FFMPEG 解码 AVPacket 数据到 AVFrame ( AVPacket-解码器 | 初始化 AVFrame | 解码为 AVFrame 数据 )相关推荐

  1. 【Android 应用开发】 Android 相关代码规范 更新中 ...

    . 简介 : Android 常用的代码结构, 包括包的规范, 测试用例规范, 数据库模块常用编写规范; 参考 : 之前写的一篇博客  [Android 应用开发] Application 使用分析  ...

  2. 【Android FFMPEG 开发】FFMPEG 读取音视频流中的数据到 AVPacket ( 初始化 AVPacket 数据 | 读取 AVPacket )

    文章目录 I . FFMPEG 获取 AVPacket 数据前置操作 II . FFMPEG 获取 AVPacket 数据流程 III . FFMPEG AVPacket 结构体 IV . AVPac ...

  3. 【Android FFMPEG 开发】FFMPEG ANativeWindow 原生绘制 ( 设置 ANativeWindow 缓冲区属性 | 获取绘制缓冲区 | 填充数据到缓冲区 | 启动绘制 )

    文章目录 I . FFMPEG ANativeWindow 原生绘制 前置操作 II . FFMPEG 原生绘制流程 III . 设置 ANativeWindow 绘制窗口属性 ANativeWind ...

  4. 【Android FFMPEG 开发】FFMPEG AVFrame 图像格式转换 YUV - RGBA ( 获取 SwsContext | 初始化图像数据存储内存 | 图像格式转换 )

    文章目录 I . FFMPEG AVFrame 图像数据帧处理 前置操作 II . FFMPEG 解码 AVPacket 数据到 AVFrame 流程 III. FFMPEG 解码前后的图像格式 IV ...

  5. 【Android FFMPEG 开发】FFMPEG 获取编解码器 ( 获取编解码参数 | 查找编解码器 | 获取编解码器上下文 | 设置上下文参数 | 打开编解码器 )

    文章目录 博客简介 . FFMPEG 编解码器获取流程 I . FFMPEG 获取音视频流的编解码参数 AVCodecParameters *codecpar II . FFMPEG 查找解码器 av ...

  6. 【Android FFMPEG 开发】FFMPEG 交叉编译配置 ( 下载 | 配置脚本 | 输出路径 | 函数库配置 | 程序配置 | 组件配置 | 编码解码配置 | 交叉编译配置 | 最终脚本 )

    文章目录 一.FFMPEG 源码下载 解压 二.交叉编译工具 三.configure 脚本及帮助命令 四.配置 configure 脚本 五.输出目录配置 六.函数库配置 七.程序配置选项 八.组件配 ...

  7. 【Android FFMPEG 开发】OpenSLES 播放音频 ( 创建引擎 | 输出混音设置 | 配置输入输出 | 创建播放器 | 获取播放/队列接口 | 回调函数 | 开始播放 | 激活回调 )

    文章目录 I . FFMPEG 播放视频流程 II . OpenSLES 播放音频流程 III . OpenSLES 播放参考 Google 官方示例 IV . OpenSL ES 播放代码 ( 详细 ...

  8. 【Android FFMPEG 开发】FFMPEG ANativeWindow 原生绘制 ( Java 层获取 Surface | 传递画布到本地 | 创建 ANativeWindow )

    文章目录 I . FFMPEG ANativeWindow 原生绘制 II . FFMPEG 原生绘制流程 III . Java 层获取 Surface 画布 IV . 传递 Surface 画布到 ...

  9. 【Android FFMPEG 开发】FFMPEG 音频重采样 ( 初始化音频重采样上下文 SwrContext | 计算音频延迟 | 计算输出样本个数 | 音频重采样 swr_convert )

    文章目录 I . FFMPEG 播放视频流程 II . FFMPEG 音频重采样流程 III . FFMPEG 音频重采样 IV . FFMPEG 初始化音频重采样上下文 SwrContext V . ...

最新文章

  1. 京东发布“下一代智能协同开放平台”战略 助力政企数字化转型
  2. RecycleView的Item Decoration间隔样式
  3. java语言在线编译器的设计与实现,已获万赞
  4. 【构造】Gym - 101411F - Figure ans Spots
  5. php 抽象 接口类 区别,PHP 抽象類和接口區別
  6. 系统学习数字图像处理之图像复原与重建
  7. Docker安装以及一些常用命令
  8. ssh工作流程及原理
  9. 弹出框(bootStrap模态框、bootbox、dbailog)
  10. 博饼游戏c语言,2015中秋博饼游戏规则
  11. 东芝Toshiba e-STUDIO4515AC 一体机驱动
  12. 新的开始——参加培训
  13. 云存储及分布式文件系统
  14. iPhone 苹果手机尺寸大全
  15. 待定系数法求不定积分中的待定系数法的拆分总结
  16. Spark Shell 的使用
  17. BERT-BiLSTM-CRF模型代码
  18. Qt 使用 Matlab函数
  19. Ubuntu7.10安装Antivir和dazuko纪实
  20. angular 日期选择器_使用Angular,Bulma和Moment.JS构建日期时间范围选择器

热门文章

  1. 敏捷个人架构图 V1.3
  2. leetcode 43. 字符串相乘(Multiply Strings)
  3. 构建之法读书笔记03
  4. 7、redis之使用spring集成commons-pool来操作常见数据类型
  5. git的常用命令总结
  6. 企业级的开发组件02 - DevExpress DXperience Universal 2011.2.5 Final
  7. 文本信息检索基本知识【转】
  8. Linq学习笔记(三)
  9. celery的log如何传递给django,由django管理
  10. 11-jQuery的事件绑定和解绑