繁体   English   中英

在循环中使用av_read_frame缓存AVFrame仅获得最后几帧

[英]Caching AVFrames using av_read_frame in a loop only get last couple of frames

我正在尝试使用opengl逐帧处理视频。 我使用ffmpeg从视频文件中读取帧。

在开始处理帧之前,我想读取一些帧并将其首先存储在内存中。 因此,我尝试在while循环中使用av_read_frame,并将帧数据复制到数组中,

但是,当我尝试显示那些帧时,我发现我只得到了最后两帧。 例如,如果我想缓存50帧,但是我只能获取最后几帧(帧45至帧50)。

这是我用来缓存帧的代码:

void cacheFrames()
{
    AVPacket tempPacket;
    av_init_packet(&tempPacket);

    int i = 0;
    avcodec_flush_buffers(formatContext->streams[streamIndex] ->codec);
    codecContext = formatContext->streams[streamIndex] ->codec;

    while (av_read_frame(formatContext, &tempPacket) >= 0 && i <NUM_FRAMES)
    {
        int finished = 0;
        if (tempPacket.stream_index == streamIndex)
        {
            avcodec_decode_video2(
                                  codecContext,
                                  frame,
                                  &finished,
                                  &tempPacket);
            if (finished)
            {
                memcpy(datas[i].datas, frame->data, sizeof(frame->data)); // copy the frame data into an array
                i++;
            }
        }

    }
    av_free_packet(&tempPacket);
}

所以,我做错了什么?

data定义为

 uint8_t* AVFrame::data[AV_NUM_DATA_POINTERS]

手术

memcpy(datas[i].datas, frame->data, sizeof(frame->data)); // copy the frame data into an array

AV_NUM_DATA_POINTERS指针复制到AV_NUM_DATA_POINTERS [i] .datas。 这是不正确的,因为您仅将引用复制到尚未分配给自己的帧缓冲区。 加上在avcodec_decode_video2之后, avcodec_decode_video2保证最后一个帧的缓冲区可用。

要保留数据,只要您想克隆框架即可。

AVFrame* framearray[NUM_FRAMES];
...
if (finished)
{
     framearray[i] = av_frame_clone(frame);
     i++;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM