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


目录
相关文章
|
5天前
|
Linux 编译器 Android开发
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
在Linux环境下,本文指导如何交叉编译x265的so库以适应Android。首先,需安装cmake和下载android-ndk-r21e。接着,下载x265源码,修改crosscompile.cmake的编译器设置。配置x265源码,使用指定的NDK路径,并在配置界面修改相关选项。随后,修改编译规则,编译并安装x265,调整pc描述文件并更新PKG_CONFIG_PATH。最后,修改FFmpeg配置脚本启用x265支持,编译安装FFmpeg,将生成的so文件导入Android工程,调整gradle配置以确保顺利运行。
24 1
FFmpeg开发笔记(九)Linux交叉编译Android的x265库
|
30天前
|
算法 数据处理 开发者
FFmpeg库的使用与深度解析:解码音频流流程
FFmpeg库的使用与深度解析:解码音频流流程
36 0
|
1月前
|
存储 编解码 算法
探索FFmpeg复用:深入理解媒体数据的组织与封装(三)
探索FFmpeg复用:深入理解媒体数据的组织与封装
34 0
|
1月前
|
存储 编解码 数据处理
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码(三)
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码
35 0
|
1月前
|
存储 编解码 数据处理
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码(二)
【FFmpeg 视频基本格式】深入理解FFmpeg:从YUV到PCM,解码到编码
37 0
|
20天前
|
编解码 缓存 算法
FFmpeg开发笔记(四)FFmpeg的动态链接库介绍
FFmpeg是一个强大的多媒体处理框架,提供ffmpeg、ffplay和ffprobe工具及八个库:avcodec(编解码)、avdevice(设备输入输出)、avfilter(音视频滤镜)、avformat(格式处理)、avutil(通用工具和算法)、postproc(后期效果)、swresample(音频重采样)和swscale(视频图像转换)。这些库支持定制化开发,涵盖了从采集、编码、过滤到输出的全过程。了解详细FFmpeg开发信息,可参考《FFmpeg开发实战:从零基础到短视频上线》。
32 0
FFmpeg开发笔记(四)FFmpeg的动态链接库介绍
|
20天前
|
编解码 搜索推荐 开发者
FFmpeg开发笔记(三)FFmpeg的可执行程序介绍
FFmpeg提供ffmpeg、ffplay和ffprobe三个可执行程序。ffmpeg用于音视频转换和查询支持信息,如编解码器、文件格式和协议。ffplay是一个简单的播放器,支持播放音视频并显示相关信息。ffprobe用于分析多媒体文件参数和数据包详情。《FFmpeg开发实战:从零基础到短视频上线》一书提供更深入的开发知识。
25 4
FFmpeg开发笔记(三)FFmpeg的可执行程序介绍
|
21天前
|
Linux API C语言
FFmpeg开发笔记(一)搭建Linux系统的开发环境
本文指导初学者如何在Linux上搭建FFmpeg开发环境。首先,由于FFmpeg依赖第三方库,可以免去编译源码的复杂过程,直接安装预编译的FFmpeg动态库。推荐网站<https://github.com/BtbN/FFmpeg-Builds/releases>提供适用于不同系统的FFmpeg包。但在安装前,需确保系统有不低于2.22版本的glibc库。详细步骤包括下载glibc-2.23源码,配置、编译和安装。接着,下载Linux版FFmpeg安装包,解压至/usr/local/ffmpeg,并设置环境变量。最后编写和编译简单的C或C++测试程序验证FFmpeg环境是否正确配置。
37 8
FFmpeg开发笔记(一)搭建Linux系统的开发环境
|
1月前
|
存储 编解码 安全
探索FFmpeg复用:深入理解媒体数据的组织与封装(二)
探索FFmpeg复用:深入理解媒体数据的组织与封装
48 0
|
1月前
|
存储 编解码 算法
探索FFmpeg复用:深入理解媒体数据的组织与封装(一)
探索FFmpeg复用:深入理解媒体数据的组织与封装
55 0