[Ffmpeg-devel] Problem seeking frames

kant1304 at free.fr kant1304
Mon Jun 19 19:45:09 CEST 2006


Hi,

I would like to use avformat to seek frames (keyframes but also normal frames)
in various ways, forward, backward, by giving a frame number... I'm using
av_seek_frame(), and for a normal video file I have no problem.
Now I want to work on a raw video file, with all frames being keyframes. The
forward search still works flawlessly, but once I want to search backwards, say
frame 20 when I'm at frame 21, the decoder always gives me frame 21...
Here is the code I'm using:


int getFrame()
{
	int decoded, length, gotPicture;
	AVFormatContext *formatContext;
	AVCodecContext *codecContext;
	AVPacket packet;

	codecContext = videoStream->codec;

	while ( 1 )
	{
		decoded = av_read_frame(formatContext, &packet);
		if (packet.stream_index == videoStreamIndex)
		{
			length = avcodec_decode_video(codecContext, context->curFrame, &gotPicture,
packet.data, packet.size);
			if ( gotPicture )
			{
				av_free_packet(&packet);
				return 0;
			}
		}
		av_free_packet(&packet);
	}

	return -1;
}

/* If we need to perform any kind of complex search */
ret = av_seek_frame(stream, frameNumber, searchFlag);
avcodec_flush_buffers(codecContext);

/* currentFrameNumber is initialized when opening the video file and decoding
the 1st frame, then updated when needed */
while (currentFrameNumber < frameNumber)
{
	getFrame();
	currentFrameNumber++;
}


This code works for a normal video file (avi container, mpeg4 video, mp3 audio,
keyframes are irregularly placed), but for the same video file type (avi
container, mpeg4 video, mp3 audio) where every frame is a keyframe, the
backwards search always returns the current frame...

I'm quite a newbie to avcodec/avformat, so I apologize if the question seems
dumb, but if someone knows about this problem or could give me some advice on
where I went wrong, I would appreciate...

Thanks in advance

Quentin Decavel




More information about the ffmpeg-devel mailing list