[Libav-user] [SPAM] Reliably getting the current frame number

Mihai Chindea mihai.chindea at uti.eu.com
Fri Jan 23 19:27:09 CET 2015

From: libav-user-bounces at ffmpeg.org [libav-user-bounces at ffmpeg.org] on behalf of coordz at megaroms.co.uk [coordz at megaroms.co.uk]
Sent: Friday, January 23, 2015 7:10 PM
To: libav-user at ffmpeg.org
Subject: [SPAM] [Libav-user] Reliably getting the current frame number

Hi all,

I'm having some problems just doing a simple thing: getting the current
frame number from a decoded AVFrame. I tried a few things (starting from
the most obvious) but I couldn't find one that consistently works:

1) Look at AVFrame.display_picture_number. Sadly, this seems to be zero
for all the files I've decoded.

2) Look at AVFrame.display_picture_number. This seems to count
sequentially from zero for most files (e.g. MP4) but stays at zero for
others (e.g. LAGS). (Aside: it also makes me wonder if the h264 is
reordering frames for me.)

3) Use av_frame_get_best_effort_timestamp(frame) to get frame timestamp.
This field always appears to be set but to convert it to a frame number I
need to know the average display time of a frame. Sadly
AVStream.avg_frame_rate is often zero.

4) Look at AVFrame.pts. Seems to stay stuck at zero for many files and I
still can't convert it similar to 3.

So, is it possible to get the current frame number reliably?

(Background: I'm getting skipped frames when decoding a lagarith (LAGS)
video. I'm trying to detect when a frame has been skipped which
(hopefully) will give me a clue to why a frame is skipped.)


Libav-user mailing list
Libav-user at ffmpeg.org

you should rely on the AVPacket.pts, and rescale that value from the AVStream.time_base to 1/avg_framerate. 
you can compute the average framerate base on AVStream.time_base.

int64_t framerate = floor( 1.00 / ( ( double )AVStream.time_base.num / ( double )AVStream.time_base.den ) );
int64_t fnumber = av_rescale_q( AVPacket.pts, AVStream.time_base, { 1, avg_framerate } );

just watch pay attention to the time_base your are using, the stream usually has different timebase than the codec.

More information about the Libav-user mailing list