[Ffmpeg-devel] Troubles with av_seek_frame

tenze at libero.it tenze
Wed Oct 25 20:44:07 CEST 2006


Hi all,

I have some problems in using the av_seek_frame() function.
I followed the tutorial of Martin Boehme and I tried to modify his code by adding a call to av_seek_frame. 
No errors are reported but the video file is decoded always starting from the first frame even if I set a timestamp which differs from 0.

May someone help me? Thanks in advance!
Livius

I append a block of the code. I highlighted the interesting code with //*** symbols.

  if(avcodec_open(pCodecCtx, pCodec)<0)
        return -1; // Could not open codec

    // Hack to correct wrong frame rates that seem to be generated by some 
    // codecs
    /*if(pCodecCtx->frame_rate>1000 && pCodecCtx->frame_rate_base==1)
        pCodecCtx->frame_rate_base=1000;*/

    // Allocate video frame
    pFrame=avcodec_alloc_frame();

    // Allocate an AVFrame structure
    pFrameRGB=avcodec_alloc_frame();
    if(pFrameRGB==NULL)
        return -1;

    // Determine required buffer size and allocate buffer
    numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
        pCodecCtx->height);
    buffer=new uint8_t[numBytes];

    // Assign appropriate parts of buffer to image planes in pFrameRGB
    avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
        pCodecCtx->width, pCodecCtx->height);

//**********************************

		// Here I set the position given by the argv[2]
		if (argc==3){			
			int64_t	timestamp=atoll(argv[2]);//*AV_TIME_BASE;
			// add the stream start time 
			/*if (pFormatCtx->start_time != AV_NOPTS_VALUE)
				timestamp += pFormatCtx->start_time;*/
			ret = av_seek_frame(pFormatCtx, videoStream, timestamp, 1);
			if (ret < 0) {
				fprintf(stderr, "%s: could not seek to position %0.3f\n",
				argv[1], (double)timestamp / AV_TIME_BASE);
			}
		}
		avcodec_flush_buffers(pFormatCtx->streams[videoStream]->codec);

//**********************************

    // Read frames and save first five frames to disk
    i=0;
    while(av_read_frame(pFormatCtx, &packet)>=0)
    {
        // Is this a packet from the video stream?
        if(packet.stream_index==videoStream)
        {	
            // Decode video frame
            avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, 
                packet.data, packet.size);

            // Did we get a video frame?
            if(frameFinished)
            {
                // Convert the image from its native format to RGB
                img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24, 
                    (AVPicture*)pFrame, pCodecCtx->pix_fmt, pCodecCtx->width, 
                    pCodecCtx->height);

                // Save the frame to disk
                if(++i<=10)
                    SaveFrame(pFrameRGB, pCodecCtx->width, pCodecCtx->height, 
                        i);
            }
        }

        // Free the packet that was allocated by av_read_frame
        av_free_packet(&packet);
    }


------------------------------------------------------
Vinci un TomTom One! Fai subito un preventivo gratuito con Genialloyd e puoi vincere un fantastico TomTom One!
http://click.libero.it/genialloyd25ott






More information about the ffmpeg-devel mailing list