+ m->current_chapter = buf->new_chap;
+ m->chapter_duration += duration;
+ }
+
+ // since we're changing the sample rate we need to keep track of
+ // the truncation bias so that the audio and video don't go out
+ // of sync. m->sum_dur_in is the sum of the input durations so far.
+ // m->sum_dur is the sum of the output durations. Their difference
+ // (in output sample rate units) is the accumulated truncation bias.
+ int64_t bias = ( m->sum_dur_in * m->samplerate / 90000 ) - m->sum_dur;
+ int64_t dur_in = buf->stop - buf->start;
+ duration = dur_in * m->samplerate / 90000 + bias;
+ if ( duration <= 0 )
+ {
+ /* We got an illegal mp4/h264 duration. This shouldn't
+ be possible and usually indicates a bug in the upstream code.
+ Complain in the hope that someone will go find the bug but
+ try to fix the error so that the file will still be playable. */
+ hb_log("MP4Mux: illegal duration %lld, bias %lld, start %lld (%lld),"
+ "stop %lld (%lld), sum_dur %lld",
+ duration, bias, buf->start * m->samplerate / 90000, buf->start,
+ buf->stop * m->samplerate / 90000, buf->stop, m->sum_dur );
+ /* we don't know when the next frame starts so we can't pick a
+ valid duration for this one so we pick something "short"
+ (roughly 1/3 of an NTSC frame time) and rely on the bias calc
+ for the next frame to correct things (a duration underestimate
+ just results in a large bias on the next frame). */
+ duration = 1000 * m->samplerate / 90000;