diff --git a/common/common.h b/common/common.h index 72fc1d8..9b5a490 100644 --- a/common/common.h +++ b/common/common.h @@ -495,6 +495,7 @@ struct x264_t int i_delay; /* Number of frames buffered for B reordering */ int i_bframe_delay; int64_t i_bframe_delay_time; + int64_t i_first_pts; int64_t i_init_delta; int64_t i_prev_reordered_pts[2]; int64_t i_largest_pts; diff --git a/encoder/encoder.c b/encoder/encoder.c index 6fd8838..5f0def5 100644 --- a/encoder/encoder.c +++ b/encoder/encoder.c @@ -2317,8 +2317,13 @@ int x264_encoder_encode( x264_t *h, fenc->i_frame = h->frames.i_input++; - if( h->frames.i_bframe_delay && fenc->i_frame == h->frames.i_bframe_delay ) - h->frames.i_bframe_delay_time = fenc->i_pts; + if( h->frames.i_bframe_delay ) + { + if ( fenc->i_frame == 0 ) + h->frames.i_first_pts = fenc->i_pts; + if ( fenc->i_frame == h->frames.i_bframe_delay ) + h->frames.i_bframe_delay_time = fenc->i_pts - h->frames.i_first_pts; + } if( h->param.b_vfr_input && fenc->i_pts <= h->frames.i_largest_pts ) x264_log( h, X264_LOG_WARNING, "non-strictly-monotonic PTS\n" ); @@ -2483,8 +2488,8 @@ int x264_encoder_encode( x264_t *h, { /* DTS compression */ if( h->i_frame == 1 ) - thread_current->frames.i_init_delta = h->fenc->i_reordered_pts * h->i_dts_compress_multiplier; - h->fdec->i_dts = h->i_frame * thread_current->frames.i_init_delta / h->i_dts_compress_multiplier; + thread_current->frames.i_init_delta = (h->fenc->i_reordered_pts - h->frames.i_first_pts) * h->i_dts_compress_multiplier; + h->fdec->i_dts = h->i_frame * thread_current->frames.i_init_delta / h->i_dts_compress_multiplier + h->frames.i_first_pts * h->i_dts_compress_multiplier; } } else diff --git a/x264.h b/x264.h index 683a024..42a3a6d 100644 --- a/x264.h +++ b/x264.h @@ -625,7 +625,7 @@ typedef struct int b_keyframe; /* In: user pts, Out: pts of encoded picture (user)*/ int64_t i_pts; - /* Out: frame dts. Since the pts of the first frame is always zero, + /* Out: frame dts. When the pts of the first frame is close to zero, * initial frames may have a negative dts which must be dealt with by any muxer */ int64_t i_dts; /* In: custom encoding parameters to be set from this frame forwards