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

简介: 【Android FFMPEG 开发】FFMPEG 解码 AVPacket 数据到 AVFrame ( AVPacket->解码器 | 初始化 AVFrame | 解码为 AVFrame 数据 )

文章目录

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 失败处理
}


目录
相关文章
|
21天前
|
移动开发 JavaScript 应用服务中间件
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
146 5
【06】优化完善落地页样式内容-精度优化-vue加vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
28天前
|
移动开发 前端开发 Android开发
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
213 12
【02】建立各项目录和页面标准化产品-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
29天前
|
移动开发 Rust JavaScript
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
451 3
【01】首页建立-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
|
2月前
|
开发工具 Android开发
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
430 11
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
|
27天前
|
移动开发 Android开发
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
【03】建立隐私关于等相关页面和内容-vue+vite开发实战-做一个非常漂亮的APP下载落地页-支持PC和H5自适应提供安卓苹果鸿蒙下载和网页端访问-优雅草卓伊凡
92 0
|
2月前
|
Java 开发工具 Maven
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
【01】完整的安卓二次商业实战-详细的初级步骤同步项目和gradle配置以及开发思路-优雅草伊凡
168 6
|
4月前
|
安全 数据库 Android开发
在Android开发中实现两个Intent跳转及数据交换的方法
总结上述内容,在Android开发中,Intent不仅是活动跳转的桥梁,也是两个活动之间进行数据交换的媒介。运用Intent传递数据时需注意数据类型、传输大小限制以及安全性问题的处理,以确保应用的健壯性和安全性。
304 11
|
4月前
|
移动开发 Java 编译器
Kotlin与Jetpack Compose:Android开发生态的演进与架构思考
本文从资深Android工程师视角深入分析Kotlin与Jetpack Compose在Android系统中的技术定位。Kotlin通过空安全、协程等特性解决了Java在移动开发中的痛点,成为Android官方首选语言。Jetpack Compose则引入声明式UI范式,通过重组机制实现高效UI更新。两者结合不仅提升开发效率,更为跨平台战略和现代架构模式提供技术基础,代表了Android开发生态的根本性演进。
153 0
|
8月前
|
JavaScript Linux 网络安全
Termux安卓终端美化与开发实战:从下载到插件优化,小白也能玩转Linux
Termux是一款安卓平台上的开源终端模拟器,支持apt包管理、SSH连接及Python/Node.js/C++开发环境搭建,被誉为“手机上的Linux系统”。其特点包括零ROOT权限、跨平台开发和强大扩展性。本文详细介绍其安装准备、基础与高级环境配置、必备插件推荐、常见问题解决方法以及延伸学习资源,帮助用户充分利用Termux进行开发与学习。适用于Android 7+设备,原创内容转载请注明来源。
1807 77
|
5月前
|
安全 Java Android开发
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡
240 0
为什么大厂要求安卓开发者掌握Kotlin和Jetpack?深度解析现代Android开发生态优雅草卓伊凡

热门文章

最新文章