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


目录
相关文章
|
Linux 开发工具 Android开发
FFmpeg开发笔记(六十)使用国产的ijkplayer播放器观看网络视频
ijkplayer是由Bilibili基于FFmpeg3.4研发并开源的播放器,适用于Android和iOS,支持本地视频及网络流媒体播放。本文详细介绍如何在新版Android Studio中导入并使用ijkplayer库,包括Gradle版本及配置更新、导入编译好的so文件以及添加直播链接播放代码等步骤,帮助开发者顺利进行App调试与开发。更多FFmpeg开发知识可参考《FFmpeg开发实战:从零基础到短视频上线》。
2079 2
FFmpeg开发笔记(六十)使用国产的ijkplayer播放器观看网络视频
|
编解码 语音技术 内存技术
FFmpeg开发笔记(五十八)把32位采样的MP3转换为16位的PCM音频
《FFmpeg开发实战:从零基础到短视频上线》一书中的“5.1.2 把音频流保存为PCM文件”章节介绍了将媒体文件中的音频流转换为原始PCM音频的方法。示例代码直接保存解码后的PCM数据,保留了原始音频的采样频率、声道数量和采样位数。但在实际应用中,有时需要特定规格的PCM音频。例如,某些语音识别引擎仅接受16位PCM数据,而标准MP3音频通常采用32位采样,因此需将32位MP3音频转换为16位PCM音频。
636 0
FFmpeg开发笔记(五十八)把32位采样的MP3转换为16位的PCM音频
|
XML 开发工具 Android开发
FFmpeg开发笔记(五十六)使用Media3的Exoplayer播放网络视频
ExoPlayer最初是为了解决Android早期MediaPlayer控件对网络视频兼容性差的问题而推出的。现在,Android官方已将其升级并纳入Jetpack的Media3库,使其成为音视频操作的统一引擎。新版ExoPlayer支持多种协议,解决了设备和系统碎片化问题,可在整个Android生态中一致运行。通过修改`build.gradle`文件、布局文件及Activity代码,并添加必要的权限,即可集成并使用ExoPlayer进行网络视频播放。具体步骤包括引入依赖库、配置播放界面、编写播放逻辑以及添加互联网访问权限。
2009 1
FFmpeg开发笔记(五十六)使用Media3的Exoplayer播放网络视频
|
Web App开发 安全 程序员
FFmpeg开发笔记(五十五)寒冬里的安卓程序员可进阶修炼的几种姿势
多年的互联网寒冬在今年尤为凛冽,坚守安卓开发愈发不易。面对是否转行或学习新技术的迷茫,安卓程序员可从三个方向进阶:1)钻研谷歌新技术,如Kotlin、Flutter、Jetpack等;2)拓展新功能应用,掌握Socket、OpenGL、WebRTC等专业领域技能;3)结合其他行业,如汽车、游戏、安全等,拓宽职业道路。这三个方向各有学习难度和保饭碗指数,助你在安卓开发领域持续成长。
453 1
FFmpeg开发笔记(五十五)寒冬里的安卓程序员可进阶修炼的几种姿势
|
API
FFmpeg中AVPacket、AVFrame结构的基本使用
FFmpeg中AVPacket和AVFrame结构的内存分配、释放和引用计数处理,以及如何避免内存泄漏。
617 3
|
Linux API 开发工具
FFmpeg开发笔记(五十九)Linux编译ijkplayer的Android平台so库
ijkplayer是由B站研发的移动端播放器,基于FFmpeg 3.4,支持Android和iOS。其源码托管于GitHub,截至2024年9月15日,获得了3.24万星标和0.81万分支,尽管已停止更新6年。本文档介绍了如何在Linux环境下编译ijkplayer的so库,以便在较新的开发环境中使用。首先需安装编译工具并调整/tmp分区大小,接着下载并安装Android SDK和NDK,最后下载ijkplayer源码并编译。详细步骤包括环境准备、工具安装及库编译等。更多FFmpeg开发知识可参考相关书籍。
956 0
FFmpeg开发笔记(五十九)Linux编译ijkplayer的Android平台so库
|
Android开发 开发者
FFmpeg开发笔记(五十七)使用Media3的Transformer加工视频文件
谷歌推出的Transformer,作为Jetpack Media3架构的一部分,助力开发者实现音视频格式转换与编辑。Media3简化了媒体处理流程,提升了定制性和可靠性。Transformer可用于剪辑、添加滤镜等操作,其示例代码可在指定GitHub仓库中找到。要使用Transformer,需在`build.gradle`中添加相关依赖,并按文档编写处理逻辑,最终完成音视频转换任务。具体步骤包括配置剪辑参数、设置空间效果以及监听转换事件等。
474 0
FFmpeg开发笔记(五十七)使用Media3的Transformer加工视频文件
|
Linux 视频直播
FFmpeg开发笔记(五十四)使用EasyPusher实现移动端的RTSP直播
本文介绍了如何使用EasyPusher-Android实现RTSP直播流程。首先对比了RTSP、RTMP、SRT和RIST四种流媒体协议,并以RTSP为例,详细说明了使用EasyPusher-Android向流媒体服务器进行RTSP直播推流的方法。文中还提供了OBS Studio配置RTSP插件及ZLMediaKit云服务器部署的相关信息,通过修改EasyPusher-Android源码使其支持通用RTSP地址,最终验证了直播功能的成功实现。
968 0
FFmpeg开发笔记(五十四)使用EasyPusher实现移动端的RTSP直播
|
XML Java Android开发
FFmpeg开发笔记(五十二)移动端的国产视频播放器GSYVideoPlayer
GSYVideoPlayer是一款国产移动端视频播放器,支持弹幕、滤镜、广告等功能,采用IJKPlayer、Media3(EXOPlayer)、MediaPlayer及AliPlayer多种内核。截至2024年8月,其GitHub星标数达2万。集成时需使用新版Android Studio,并按特定步骤配置依赖与权限。提供了NormalGSYVideoPlayer、GSYADVideoPlayer及ListGSYVideoPlayer三种控件,支持HLS、RTMP等多种直播链接。
820 18
FFmpeg开发笔记(五十二)移动端的国产视频播放器GSYVideoPlayer

热门文章

最新文章