ffmpeg解码现在完全放在后台去做了,现在分为2个步骤
avcodec_send_packet 发送数据到ffmepg,放到解码队列中
avcodec_receive_frame 将成功的解码队列中取出1个frame
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpacket)
avpacket 解码和解封装是2个线程,如果解封装后,调用此函数后,会将avpacket的引用计数加1 或者 复制一份(没有计数引用)。因此在调用了后,释放掉 avpacket。
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
frame会每次清掉上一次frame,然后重新赋值,可以给同一个frame。
解码例子
note 例子 有点小问题是最后几帧读取不到,因为avcodec_send_packet 后就开始解码,会将解码的frame放在缓冲区,刚开始获取的时候也会失败,因为还没解码好,因此这里会有一些些小问题,这里只讲基本用法:
AVPacket *pkt = av_packet_alloc();
AVFrame *avFrame =av_frame_alloc();
while(1){
int re = av_read_frame(ic,pkt);
if(re != 0)
{
LOGW("读取完毕");
// av_seek_frame(ic, videoStream,ic->duration/2, AVSEEK_FLAG_BACKWARD|AVSEEK_FLAG_FRAME);
continue;
}
AVCodecContext* avCodecContext = videoCodecContext;
if(pkt->stream_index != videoStream){
avCodecContext = audioCodecContext;
continue;
}
re = avcodec_send_packet(avCodecContext, pkt);
av_packet_unref(pkt);
if(re!=0){
continue;
}
re = avcodec_receive_frame(avCodecContext, avFrame);
if(re!=0){
LOGE("avcodec_receive_frame failed !");
}
LOGE("avcodec_receive_frame sucess pts = %lld !",avFrame->pts );
}