[Libav-user] Seeking causes the demuxer to skip some packets (after the seek)... why? Bug?

Michael Bradshaw mbradshaw at sorensonmedia.com
Thu Oct 25 22:10:19 CEST 2012


On Thu, Oct 25, 2012 at 2:02 PM, Michael Bradshaw <
mbradshaw at sorensonmedia.com> wrote:
> I've also included

Sorry, I meant to say that should you be interested, the source video file
can be found at:
https://docs.google.com/uc?export=download&id=0BxWx_dIBnyRoSnMyTFNGOHR1c2c

The source I'm using, should you also be interested, is (remove the 'extern
"C"' part to compile as C):

#include <stdio.h>

extern "C"
{
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>
}

int main(int argc, char** argv)
{
    AVFrame* frame;
    AVFormatContext* formatContext;
    AVStream* videoStream;
    AVCodecContext* codecContext;
    AVPacket packet;
    int frameFinished;
    int result;

    av_register_all();

    frame = avcodec_alloc_frame();
    if (!frame)
    {
        printf("Error allocating the frame\n");
        return 1;
    }

    formatContext = NULL;
    if (avformat_open_input(&formatContext,
"C:/Users/mbradshaw/Desktop/b/c/SIMPSONS_D2-CallOfTheSimpsons.m4v", NULL,
NULL) != 0)
    {
        av_free(frame);
        printf("Error opening the file\n");
        return 1;
    }

    if (avformat_find_stream_info(formatContext, NULL) < 0)
    {
        av_free(frame);
        avformat_close_input(&formatContext);
        printf("Error finding the stream info\n");
        return 1;
    }

    videoStream = NULL;
    for (unsigned int i = 0; i < formatContext->nb_streams; ++i)
    {
        if (formatContext->streams[i]->codec->codec_type ==
AVMEDIA_TYPE_VIDEO)
        {
            videoStream = formatContext->streams[i];
            break;
        }
    }

    if (videoStream == NULL)
    {
        av_free(frame);
        avformat_close_input(&formatContext);
        printf("Could not find any video stream in the file\n");
        return 1;
    }

    codecContext = videoStream->codec;
    codecContext->codec = avcodec_find_decoder(codecContext->codec_id);
    if (codecContext->codec == NULL)
    {
        av_free(frame);
        avformat_close_input(&formatContext);
        printf("Couldn't find a proper decoder\n");
        return 1;
    }
    else if (avcodec_open2(codecContext, codecContext->codec, NULL) != 0)
    {
        av_free(frame);
        avformat_close_input(&formatContext);
        printf("Couldn't open the context with the decoder\n");
        return 1;
    }

    av_init_packet(&packet);
    while (av_read_frame(formatContext, &packet) == 0)
    {
        if (packet.stream_index == videoStream->index)
        {
            printf("packet.dts = %d\n", packet.dts);

            frameFinished = 0;
            result = avcodec_decode_video2(codecContext, frame,
&frameFinished, &packet);

            if (result >= 0 && frameFinished)
            {
                break;
                av_free_packet(&packet);
            }
        }

        av_free_packet(&packet);
    }

    avcodec_flush_buffers(codecContext);
    printf("seek result: %d\n",
           av_seek_frame(formatContext, 1, -2000000000,
AVSEEK_FLAG_BACKWARD | AVSEEK_FLAG_ANY));

    while (av_read_frame(formatContext, &packet) == 0)
    {
        if (packet.stream_index == videoStream->index)
        {
            printf("packet.dts = %d\n", packet.dts);

            frameFinished = 0;
            result = avcodec_decode_video2(codecContext, frame,
&frameFinished, &packet);

            if (result >= 0 && frameFinished)
            {
                break;
                av_free_packet(&packet);
            }
        }

        av_free_packet(&packet);
    }

    av_free(frame);
    avcodec_close(codecContext);
    avformat_close_input(&formatContext);
    return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20121025/34f231c7/attachment.html>


More information about the Libav-user mailing list