如果我的FFmpeg使用av_read_frame多帧丢失 [英] Multiple frames lost if I use av_read_frame in FFmpeg

查看:1202
本文介绍了如果我的FFmpeg使用av_read_frame多帧丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3500帧的HEVC序列和我写的去codeR阅读(通过阅读框架和转储到YUV)。在我的main(),我有一个for循环,调用去codeR()3500次(我假设在这个阶段,在main()知道有多少帧有)。

I have an HEVC sequence with 3500 frames and I am writing a decoder for reading it (read frame by frame and dump to yuv). In my main(), I have a for loop that calls a decoder() 3500 times (I am assuming at this stage that the main() knows how many frames there are).

因此​​,每次调用去codeR(),我需要返回一个完整的框架。这就是德codeR()看起来像..

So, for every call to decoder(), I need a complete frame to be returned. This is what the decoder() looks like..

bool decode(pFormatCtx, pCodecCtx)
{
    int gotaFrame=0;

    while (gotaFrame==0) {

        printf("1\t");

        if ( !av_read_frame(pFormatCtx, &packet) ) { 
            if(packet.stream_index==videoStreamIndex) {

                // try decoding
                avcodec_decode_video2(pCodecCtx, pFrame, &gotaFrame, &packet);

                if (gotaFrame) {  // decode success.

                    printf("2\t");

                    // dump to yuv ... not shown here. 

                    // cleanup
                    av_frame_unref(pFrame);
                    av_frame_free(&pFrame);
                    av_free_packet(&packet);

                    return true;
                }
            }
        }
    }
}

该行为是这样的:1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 ......看起来它解码一个之前读取几个帧?第一帧是一个I帧,所以不应该说是德codeD上吗?

The behavior is like this: 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 2 1 2 ...... it looks like it reads several frames before decoding one? The first frame is an I-frame, so shouldn't that be decoded right away?

有了这个code,我最终失去了几帧(由一系列1秒表示)。有人可以帮助我在这里?有什么我在我的code做错了吗?

With this code, I end up losing the several frames (indicated by the series of 1s). Can someone help me out here? Is there something I am doing wrong in my code?

更新:测试剪辑视频而已。没有音讯。

Update: the test clip is video-only. No audio.

推荐答案

你们看到的是正确的行为。去codeR bufferers多线程效率的几帧。它可能需要几个帧以黄金泵,因为它是。基本上,让你的程序反应灵敏,AV codec_de code_video2排队解码框架,然后返回。这prevents阻塞很长一段时间你的程序。它也绝对需要以延迟在B帧的情况下,其中,所述去code顺序可能不是相同的显示顺序解码

What you are seeing is the correct behavior. The decoder bufferers a few frames for multithreaded efficiency. And it may take several frames to 'prime the pump' as it were. Basically, to keep your program responsive, avcodec_decode_video2 queues up the frame for decoding, then returns. This prevents your program from blocking for a long time. It is also absolutely required to delay decoding in the case of B frames where the decode order may not be the same as the display order.

那么,如何才能不失去这些框架? av_read_frame停止回新框架后,必须通过调用AV codec_de code_video2空数据包,直到没有更多的帧重新调整冲去$ C $铬。

So, how to not lose these frames? After av_read_frame stops returning new frames, you must flush the decoder by calling avcodec_decode_video2 with empty packets until no more frames are retuned.

这篇关于如果我的FFmpeg使用av_read_frame多帧丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆