[英]Caching AVFrames using av_read_frame in a loop only get last couple of frames
I'm trying to process a video frame by frame using opengl. 我正在尝试使用opengl逐帧处理视频。 I use ffmpeg to read frames from the video file.
我使用ffmpeg从视频文件中读取帧。
Before I start processing the frames, I want read some frames and store them in the memory first. 在开始处理帧之前,我想读取一些帧并将其首先存储在内存中。 So I try to using av_read_frame in a while loop, and copy frame data into a array,
因此,我尝试在while循环中使用av_read_frame,并将帧数据复制到数组中,
But, when I try to display those frames, I find that I only get the last a couple of frames . 但是,当我尝试显示那些帧时,我发现我只得到了最后两帧。 For example, If I want to cache 50 frames, but I can only get the last couple frames (frame 45 to frame 50).
例如,如果我想缓存50帧,但是我只能获取最后几帧(帧45至帧50)。
Here's the code I'm using to caching the frames: 这是我用来缓存帧的代码:
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);
}
So, what I'm doing wrong? 所以,我做错了什么?
data
is defined as data
定义为
uint8_t* AVFrame::data[AV_NUM_DATA_POINTERS]
The operation 手术
memcpy(datas[i].datas, frame->data, sizeof(frame->data)); // copy the frame data into an array
will copy AV_NUM_DATA_POINTERS
pointers into datas[i].datas. 将
AV_NUM_DATA_POINTERS
指针复制到AV_NUM_DATA_POINTERS
[i] .datas。 This is incorrect because you only copy references to frame buffers you havent allocated yourself. 这是不正确的,因为您仅将引用复制到尚未分配给自己的帧缓冲区。 Plus only the last frame(s) buffers are guaranteed to be available after
avcodec_decode_video2
. 加上在
avcodec_decode_video2
之后, avcodec_decode_video2
保证最后一个帧的缓冲区可用。
To keep the data as long as you want to need to clone the frame. 要保留数据,只要您想克隆框架即可。
AVFrame* framearray[NUM_FRAMES];
...
if (finished)
{
framearray[i] = av_frame_clone(frame);
i++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.