c++ffmpegh.264libavlive555

PTS not set after decoding H264/RTSP stream


Question: What does the Libav/FFmpeg decoding pipeline need in order to produce valid presentation timestamps (PTS) in the decoded AVFrames?

I'm decoding an H264 stream received via RTSP. I use Live555 to parse H264 and feed the stream to my LibAV decoder. Decoding and displaying is working fine, except I'm not using timestamp info and get some stuttering.

After getting a frame with avcodec_decode_video2, the presentation timestamp (PTS) is not set.

I need the PTS in order to find out for how long each frame needs to be displayed, and avoid any stuttering.

Notes on my pipeline

Unclear:

The RTP timestamp is set to the sampling timestamp of the content. A 90 kHz clock rate MUST be used.


Solution

  • Copy the live555 pts into the avpacket pts. Process the packet with avcodec_decode_video2, and then retrieve the pts from avframe->pkt_pts, these will be monotonically increasing.

    There is no need to set anything in the codec context, apart from setting the SPS and PPS in the AVCodecContex extradata

    You can find a good example in VLC's github: Setting AVPacket pts: https://github.com/videolan/vlc/blob/master/modules/codec/avcodec/video.c#L983

    Decoding AVPacket into AVFrame: https://github.com/videolan/vlc/blob/master/modules/codec/avcodec/video.c#L1014

    Retrieving from AVFrame pts: https://github.com/videolan/vlc/blob/master/modules/codec/avcodec/video.c#L1078