- 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 by more than a frame time (this can
- * happen when we reset the PTS because of lost data).
- * Discard data that's in the past.
- */
- if ( sync->first_drop == 0 )
- {
- sync->first_drop = buf->start;
- }
- ++sync->drop_count;
- buf = hb_fifo_get( audio->priv.fifo_raw );
- hb_buffer_close( &buf );
- continue;
- }
- if ( sync->first_drop )
- {
- 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;
- }
- 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 );
- }
-}
-
-static int NeedSilence( hb_work_object_t * w, hb_audio_t * audio, 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];
-
- if( hb_fifo_size( audio->priv.fifo_in ) ||
- hb_fifo_size( audio->priv.fifo_raw ) ||
- hb_fifo_size( audio->priv.fifo_sync ) ||
- hb_fifo_size( audio->priv.fifo_out ) )
- {
- /* We have some audio, we are fine */
- return 0;
- }
-
- /* No audio left in fifos */
-
- if( hb_thread_has_exited( job->reader ) )
- {
- /* We might miss some audio to complete encoding and muxing
- the video track */
- if ( sync->start_silence == 0 )
- {
- hb_log("sync: reader has exited, adding silence to audio %d", i);
- sync->start_silence = sync->next_pts;
- }
- return 1;
- }
- return 0;