89
  • In simple terms what are pts and dts values?
  • Why are they important while transcoding [decode-encode] videos ?

What does this code bit do in ffmpeg.c , what is its purpose?

01562    ist->next_pts = ist->pts = picture.best_effort_timestamp;
01563    if (ist->st->codec->time_base.num != 0) {
01564        int ticks= ist->st->parser ? ist->st->parser->repeat_pict+1 : ist->st->codec->ticks_per_frame;
01565        ist->next_pts += ((int64_t)AV_TIME_BASE *
01566                         ist->st->codec->time_base.num * ticks) /
01567                         ist->st->codec->time_base.den;
01568    }

2 Answers 2

167

Those are the decoding time stamp (DTS) and presentation time stamp (PTS). You can find an explanation here inside a tutorial.

So let's say we had a movie, and the frames were displayed like: I B B P. Now, we need to know the information in P before we can display either B frame. Because of this, the frames might be stored like this: I P B B. This is why we have a decoding timestamp and a presentation timestamp on each frame. The decoding timestamp tells us when we need to decode something, and the presentation time stamp tells us when we need to display something. So, in this case, our stream might look like this:

   PTS: 1 4 2 3
   DTS: 1 2 3 4
Stream: I P B B

Generally the PTS and DTS will only differ when the stream we are playing has B frames in it.

13
  • 16
    @nirvanaswap that is actually explained fairly well here en.wikipedia.org/wiki/Video_compression_picture_types
    – Bart
    May 20, 2016 at 7:36
  • 2
    I still don't understand. The stream has to be displayed as I B B P, But since P ought to come before B, we store it as I P B B. Hence we decode it in the order of 1 2 3 4, that makes sense. But shouldn't we present it in the order of 1 3 4 2? Why is it 1 4 2 3? When we have consecutive B frames, do these B frames take advantage of the I and P frames alone, or do they use the B frames next to each other too? May 20, 2016 at 18:31
  • 5
    No, you're displaying in IBBP order. But because the first B relies on the I before it and the B after it, and subsequently the second B frame relies on the final P frame, to be able to display the first B frame you'll already need the information of the P frame. And that's why the stream is IPBB.
    – Bart
    May 20, 2016 at 18:36
  • 4
    @neevek Should; ffmpeg complains when it doesn't, eg “Non-monotonous DTS in output stream” (in this case they used -c copy).
    – arielCo
    Jul 24, 2020 at 15:08
  • 7
    Note for confused readers like me: I, P, and B are not arbitrary frame names they stand for Intra-coded frame, Predicted frame, Bi-directional predicted frame, respectively.
    – ahmetknk
    Apr 5, 2022 at 23:37
-7

B frames are predicted from I and P frames. B frames usually have more errors compared to I and P and hence are not recommended for prediction, though they might be closer in time. There are algorithms in which B is used for prediction but it is from a past B frame and not future B frames.

So in a sequence of I P B1 B2, Decode order is I P B1 B2 and Display order is I B1 B2 P. P is predicted from I, B1 from both I and P, B2 again from I and P.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.