+ if( job->vcodec == HB_VCODEC_X264 )
+ {
+ // x264 supplies us with DTS
+ if ( m->delay_buf )
+ {
+ duration = m->delay_buf->renderOffset - buf->renderOffset;
+ }
+ else
+ {
+ duration = buf->stop - m->sum_dur;
+ // Due to how libx264 generates DTS, it's possible for the
+ // above calculation to be negative.
+ //
+ // x264 generates DTS by rearranging PTS in this sequence:
+ // pts0 - delay, pts1 - delay, pts2 - delay, pts1, pts2, pts3...
+ //
+ // where delay == pts2. This guarantees that DTS <= PTS for
+ // any frame, but also generates this sequence of durations:
+ // d0 + d1 + d0 + d1 + d2 + d3 ... + d(N-2)
+ //
+ // so the sum up to the last frame is:
+ // sum_dur = d0 + d1 + d0 + d1 + d2 + d3 ... + d(N-3)
+ //
+ // while the original total duration of the video was:
+ // duration = d0 + d1 + d2 + d3 ... + d(N)
+ //
+ // Note that if d0 + d1 != d(N-1) + d(N), the total
+ // length of the video changes since d(N-1) and d(N) are
+ // replaced by d0 and d1 in the final duration sum.
+ //
+ // To keep the total length of the video the same as the source
+ // we try to make
+ // d(N-2) = duration - sum_dur
+ //
+ // But if d0 + d1 >= d(N-1) + d(N), the above calculation
+ // results in a nagative value and we need to fix it.
+ if ( duration <= 0 )
+ duration = 90000. / ((double)job->vrate / (double)job->vrate_base);
+ }
+ }
+ else
+ {
+ // We're getting the frames in decode order but the timestamps are
+ // for presentation so we have to use durations and effectively
+ // compute a DTS.
+ duration = buf->stop - buf->start;
+ }
+