+ /* Not pass-thru - do sample rate conversion */
+ int count_in, count_out;
+ hb_buffer_t * buf_raw = buf;
+ int channel_count = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown) *
+ sizeof( float );
+
+ count_in = buf_raw->size / channel_count;
+ /*
+ * When using stupid rates like 44.1 there will always be some
+ * truncation error. E.g., a 1536 sample AC3 frame will turn into a
+ * 1536*44.1/48.0 = 1411.2 sample frame. If we just truncate the .2
+ * the error will build up over time and eventually the audio will
+ * substantially lag the video. libsamplerate will keep track of the
+ * fractional sample & give it to us when appropriate if we give it
+ * an extra sample of space in the output buffer.
+ */
+ count_out = ( duration * audio->config.out.samplerate ) / 90000 + 1;
+
+ sync->data.input_frames = count_in;
+ sync->data.output_frames = count_out;
+ sync->data.src_ratio = (double)audio->config.out.samplerate /
+ (double)audio->config.in.samplerate;
+
+ buf = hb_buffer_init( count_out * channel_count );
+ sync->data.data_in = (float *) buf_raw->data;
+ sync->data.data_out = (float *) buf->data;
+ if( src_process( sync->state, &sync->data ) )
+ {
+ /* XXX If this happens, we're screwed */
+ hb_log( "sync: audio %d src_process failed", audio->id );
+ }
+ hb_buffer_close( &buf_raw );
+
+ buf->size = sync->data.output_frames_gen * channel_count;
+ duration = ( sync->data.output_frames_gen * 90000 ) /
+ audio->config.out.samplerate;
+ }
+ buf->frametype = HB_FRAME_AUDIO;
+ buf->start = start;
+ buf->stop = start + duration;
+ sync->next_start = start + duration;
+ return buf;
+}
+
+static void InsertSilence( hb_work_object_t * w, int64_t duration )
+{
+ hb_work_private_t * pv = w->private_data;
+ hb_sync_audio_t *sync = &pv->type.audio;
+ hb_buffer_t *buf;
+ hb_fifo_t *fifo;
+
+ // to keep pass-thru and regular audio in sync we generate silence in
+ // AC3 frame-sized units. If the silence duration isn't an integer multiple
+ // of the AC3 frame duration we will truncate or round up depending on
+ // which minimizes the timing error.
+ const int frame_dur = ( 90000 * AC3_SAMPLES_PER_FRAME ) /
+ w->audio->config.in.samplerate;
+ int frame_count = ( duration + (frame_dur >> 1) ) / frame_dur;
+
+ while ( --frame_count >= 0 )
+ {
+ if( w->audio->config.out.codec == HB_ACODEC_AC3 )
+ {
+ buf = hb_buffer_init( sync->ac3_size );
+ buf->start = sync->next_pts;
+ buf->stop = buf->start + frame_dur;
+ memcpy( buf->data, sync->ac3_buf, buf->size );
+ fifo = w->audio->priv.fifo_out;
+ }
+ else
+ {
+ buf = hb_buffer_init( AC3_SAMPLES_PER_FRAME * sizeof( float ) *
+ HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(
+ w->audio->config.out.mixdown) );
+ buf->start = sync->next_pts;
+ buf->stop = buf->start + frame_dur;
+ memset( buf->data, 0, buf->size );
+ fifo = w->audio->priv.fifo_sync;
+ }
+ buf = OutputAudioFrame( w->audio, buf, sync );
+ hb_fifo_push( fifo, buf );