- hb_fifo_push( fifo, buf );
-}
-
-/***********************************************************************
- * SyncAudio
- ***********************************************************************
- *
- **********************************************************************/
-static void SyncAudio( hb_work_object_t * w, int i )
-{
- hb_work_private_t * pv = w->private_data;
- hb_job_t * job = pv->job;
- hb_sync_audio_t * sync = &pv->sync_audio[i];
- hb_audio_t * audio = sync->audio;
- hb_buffer_t * buf;
- hb_fifo_t * fifo;
-
- if( audio->config.out.codec == HB_ACODEC_AC3 )
- {
- fifo = audio->priv.fifo_out;
- }
- else
- {
- fifo = audio->priv.fifo_sync;
- }
-
- while( !hb_fifo_is_full( fifo ) && ( buf = hb_fifo_see( audio->priv.fifo_raw ) ) )
- {
- if ( (int64_t)( buf->start - sync->next_pts ) < 0 )
- {
- // audio time went backwards.
- // If our output clock is more than a half frame ahead of the
- // input clock drop this frame to move closer to sync.
- // Otherwise drop frames until the input clock matches the output clock.
- if ( sync->first_drop || sync->next_start - buf->start > 90*15 )
- {
- // Discard data that's in the past.
- if ( sync->first_drop == 0 )
- {
- sync->first_drop = sync->next_pts;
- }
- ++sync->drop_count;
- buf = hb_fifo_get( audio->priv.fifo_raw );
- hb_buffer_close( &buf );
- continue;
- }
- sync->next_pts = buf->start;
- }
- if ( sync->first_drop )
- {
- // we were dropping old data but input buf time is now current
- hb_log( "sync: audio %d time went backwards %d ms, dropped %d frames "
- "(next %lld, current %lld)", i,
- (int)( sync->next_pts - sync->first_drop ) / 90,
- sync->drop_count, sync->first_drop, sync->next_pts );
- sync->first_drop = 0;
- sync->drop_count = 0;
- sync->next_pts = buf->start;
- }
- if ( buf->start - sync->next_pts >= (90 * 70) )
- {
- /*
- * there's a gap of at least 70ms between the last
- * frame we processed & the next. Fill it with silence.
- */
- hb_log( "sync: adding %d ms of silence to audio %d"
- " start %lld, next %lld",
- (int)((buf->start - sync->next_pts) / 90),
- i, buf->start, sync->next_pts );
- InsertSilence( w, i, buf->start - sync->next_pts );
- return;
- }
-
- /*
- * When we get here we've taken care of all the dups and gaps in the
- * audio stream and are ready to inject the next input frame into
- * the output stream.
- */
- buf = hb_fifo_get( audio->priv.fifo_raw );
- OutputAudioFrame( job, audio, buf, sync, fifo, i );
- }
-
- if( NeedSilence( w, audio, i ) )
- {
- InsertSilence( w, i, (90000 * AC3_SAMPLES_PER_FRAME) /
- sync->audio->config.in.samplerate );
- }