BlenderBender
BlenderBender

Reputation: 566

Why is packet.pts != frame->pkt_pts in AVFrame?

Trying to understand some audio/video sync issues via ffmpeg, I noticed the following. Running this code

while (av_read_frame(formatCtx, &packet) >= 0)
{
   if (packet.stream_index == videoStream)
   {
      avcodec_decode_video2(videoCodecCtx, frame, &got_frame, &packet);
   }

   printf("packet.pts = %d\n", packet.pts);
   printf("frame->pkt_pts", frame->pkt_pts);
}

shows that frame->pkt_pts in general differs from packet.pts, despite the documentation claiming that frame->pkt_pts is the

PTS copied from the AVPacket that was decoded to produce this frame

Moreover I noticed that the difference between the two is big exactly at the places where audio and video are out of sync.

So, why is packet.pts != frame->pkt_pts ?

Upvotes: 6

Views: 4481

Answers (1)

Ronald S. Bultje
Ronald S. Bultje

Reputation: 11174

Video may have delayed frames, which means the input frame and output frame may refer to differently ordered units. E.g. in case of MPEG, display order of IBP is coded as IPB, and the pts of input is different from output, and the reordering introduces delay between input pts and output pts. Additionally, when using multi-threaded decoding, an additional delay of n_threads - 1 packets is added between input and output. In all these cases, pkt.pts != frame->pkt_pts. For display, you should rely on frame->pkt_pts.

Upvotes: 7

Related Questions