【Android FFMPEG 开发】FFMPEG 解码 AVPacket 数据到 AVFrame ( AVPacket->解码器 | 初始化 AVFrame | 解码为 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 失败处理
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值