[Ffmpeg-devel] av_seek_frame() units

Andy Parkins andyparkins
Wed Jul 6 15:54:45 CEST 2005


On Wednesday 2005 July 06 14:27, Steve Willis wrote:

> Let's say I want to seek to a frame that is 14.25 seconds into a stream.
> Would someone please post actual code showing (1) how to calculate the
> value for 14.25 seconds to pass to av_seek_frame to seek to the nearest
> key frame (Ian's question), and (2) a recommended way to actually seek
> to the precise frame for 14.25 seconds, regardless of whether it is a
> keyframe (my question).

My example code from earlier in this thread, with the correction suggested by 
Michael...

// normal frame loop av_read_frame() done above here
if( seekRequested ) {
    TARGET_PTS = 14.25 * AV_TIME_BASE;
    av_seek_frame( fmtCtx, -1, TARGET_PTS, AVSEEK_FLAG_BACKWARD );
    CodecCtx->hurry_up = 1;
    do {
        av_read_frame( fmtCtx, &Packet );
        // should really be checking that this is a video packet
        MyPts = av_rescale( Packet.pts,
            AV_TIME_BASE * (int64_t) Stream->time_base.num,
            Stream->time_base.den );
        // Once we pass the target point, break from the loop
        if( MyPts >= TARGET_PTS )
            break;
        avcodec_decode_video( CodecCtx, pFrame, &gotFrame, Packet.data,
            Packet.size );
        av_free_packet( &Packet );
    } while(1);
    CodecCtx->hurry_up = 0;
}
// Continue with normal frame loop (i.e. decode, show and free)

The documentation for av_seek_frame() (and the code) says that if the stream 
is not given (i.e. -1) then AV_TIME_BASE units are used, otherwise the time 
base for the particular stream is used - in which case you would need to 
rescale TARGET_PTS to stream units before calling it.  This would be done as 
the inverse of the MyPts calculation above.  Looking at the code again, it 
would also be better to use the stream time base throughout, which would then 
save the repeated call to av_rescale() every loop.

I've only recently started working with the libav* libraries myself, but have 
found that everything I want is there; it's finding the thing that's the 
trick :-)


Andy

-- 
Dr Andrew Parkins, M Eng (hons), AMIEE
andyparkins at gmail.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20050706/9eb715a3/attachment.pgp>



More information about the ffmpeg-devel mailing list