[Libav-user] Accessing coded frame pts for audio

Brad O'Hearne brado at bighillsoftware.com
Mon Apr 15 19:08:15 CEST 2013


In a processing workflow that includes first encoding a video / audio frame followed by writing that frame to a file/network stream, it is required to set the packet pts prior to encoding. The subsequent encoding operation then returns an output value (gotPacket) indicating whether a packet was returned, e.g.: 

                returnVal = avcodec_encode_audio2(codecCtx, &_avPacket, _streamAudioFrame, &gotPacket);

A packet might not have been returned, but if one has been returned, there are two notable characteristics about it: 

1. it is not necessarily the same packet that was encoded; i.e. the encoder can return a different packet (presumably due to reordering as necessary, etc.). 

2. The returned packet does not have an accurate pts set, and so this must be manually set in the code.

When dealing with a video stream, the general approach to setting packet pts is to grab the AVCodecContext's pts of its coded frame, and then rescale it from a value relative to the context's time_base (that's the one based on frame rate) to one relative to the stream's time_base (the one based on time), as such: 

_avPacket.pts = av_rescale_q(codecCtx->coded_frame->pts, codecCtx->time_base, _videoStream->time_base);

The key point there is that the pts of the frame involved with the packet returned is accessible in the context's coded_frame, i.e.: 


That's great for video. But when processing audio, coded_frame->pts is consistently junk, always valued -9223372036854775808. So my question is after encoding, if the avcodec_encode_audio2 indeed returns a packet how to access the pts for the encoded frame (or in the case of audio, it would probably be more proper to say the encoded samples)? Again, given that the packet returned by the encoder isn't necessarily the one that was encoded, we need the pts of the packet when it was encoded. 

How do I access this value? 



More information about the Libav-user mailing list