From 3c422e7eef44ac8508071c6dbeff3872cf0a4cbe Mon Sep 17 00:00:00 2001 From: jstebbins Date: Sat, 6 Nov 2010 21:14:14 +0000 Subject: [PATCH] fix audio sync when resampling 48khz to 44.1khz Rounding errors in timestamp calculations caused a gradual slip in both sync.c and encfaac.c. git-svn-id: svn://localhost/HandBrake/trunk@3653 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- libhb/encfaac.c | 7 ++++--- libhb/sync.c | 12 ++++++------ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/libhb/encfaac.c b/libhb/encfaac.c index c955199d..cc7e3b91 100644 --- a/libhb/encfaac.c +++ b/libhb/encfaac.c @@ -18,8 +18,8 @@ struct hb_work_private_s uint8_t * buf; uint8_t * obuf; hb_list_t * list; - int64_t pts; - int64_t framedur; + double pts; + double framedur; int out_discrete_channels; }; @@ -97,8 +97,9 @@ int encfaacInit( hb_work_object_t * w, hb_job_t * job ) &pv->input_samples, &pv->output_bytes ); pv->buf = malloc( pv->input_samples * sizeof( float ) ); pv->obuf = malloc( pv->output_bytes ); - pv->framedur = 90000LL * pv->input_samples / + pv->framedur = 90000.0 * pv->input_samples / ( audio->config.out.samplerate * pv->out_discrete_channels ); +printf("in %ld out %ld sr %d dur %g\n", pv->input_samples, pv->output_bytes, audio->config.out.samplerate, pv->framedur); cfg = faacEncGetCurrentConfiguration( pv->faac ); cfg->mpegVersion = MPEG4; diff --git a/libhb/sync.c b/libhb/sync.c index 57809d7e..fac0c343 100644 --- a/libhb/sync.c +++ b/libhb/sync.c @@ -36,7 +36,7 @@ typedef struct typedef struct { int index; - int64_t next_start; /* start time of next output frame */ + double next_start; /* start time of next output frame */ int64_t next_pts; /* start time of next input frame */ int64_t first_drop; /* PTS of first 'went backwards' frame dropped */ int drop_count; /* count of 'time went backwards' drops */ @@ -1193,8 +1193,8 @@ static void InitAudio( hb_job_t * job, hb_sync_common_t * common, int i ) static hb_buffer_t * OutputAudioFrame( hb_audio_t *audio, hb_buffer_t *buf, hb_sync_audio_t *sync ) { - int64_t start = sync->next_start; - int64_t duration = buf->stop - buf->start; + int64_t start = (int64_t)sync->next_start; + double duration = buf->stop - buf->start; sync->next_pts += duration; @@ -1244,13 +1244,13 @@ static hb_buffer_t * OutputAudioFrame( hb_audio_t *audio, hb_buffer_t *buf, hb_buffer_close( &buf_raw ); buf->size = sync->data.output_frames_gen * channel_count; - duration = ( sync->data.output_frames_gen * 90000 ) / + duration = (double)( 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; + sync->next_start += duration; + buf->stop = (int64_t)sync->next_start; return buf; } -- 2.11.0