+ if ( r->scr_changes != r->demux.scr_changes )
+ {
+ // This is the first audio or video packet after an SCR
+ // change. Compute a new scr offset that would make this
+ // packet follow the last of this stream with the
+ // correct average spacing.
+ stream_timing_t *st = id_to_st( r, buf, 0 );
+
+ // if this is the video stream and we don't have
+ // audio yet or this is an audio stream
+ // generate a new scr
+ if ( st->is_audio ||
+ ( st == r->stream_timing && !r->saw_audio ) )
+ {
+ new_scr_offset( r, buf );
+ }
+ else
+ {
+ // defer the scr change until we get some
+ // audio since audio has a timestamp per
+ // frame but video & subtitles don't. Clear
+ // the timestamps so the decoder will generate
+ // them from the frame durations.
+ if ( st != r->stream_timing )
+ {
+ // not a video stream so it's probably
+ // subtitles - the best we can do is to
+ // line it up with the last video packet.
+ buf->start = r->stream_timing->last;
+ }
+ else
+ {
+ buf->start = -1;
+ buf->renderOffset = -1;
+ }
+ }
+ }
+ }
+ if ( buf->start != -1 )
+ {
+ int64_t start = buf->start - r->scr_offset;
+ if ( !r->start_found )
+ UpdateState( r, start );
+
+ if ( !r->start_found &&
+ r->job->pts_to_start &&
+ buf->renderOffset != -1 &&
+ start >= r->job->pts_to_start )
+ {
+ // pts_to_start point found
+ // force a new scr offset computation
+ stream_timing_t *st = find_st( r, buf );
+ if ( st &&
+ (st->is_audio ||
+ ( st == r->stream_timing && !r->saw_audio ) ) )
+ {
+ // Re-zero our timestamps
+ st->last = -st->average;
+ new_scr_offset( r, buf );
+ r->start_found = 1;
+ r->job->pts_to_start = 0;
+ }
+ }
+ // This log is handy when you need to debug timing problems
+ //hb_log("id %x scr_offset %ld start %ld --> %ld",
+ // buf->id, r->scr_offset, buf->start,
+ // buf->start - r->scr_offset);
+ buf->start -= r->scr_offset;
+ }
+ if ( buf->renderOffset != -1 )
+ {