OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / libhb / sync.c
index 53b3193..b44765e 100644 (file)
@@ -21,7 +21,7 @@ typedef struct
     hb_lock_t * mutex;
     int         ref;        /* Reference count to tell us when it's unused */
     int         count_frames;
-    int64_t     audio_passthru_slip;
+    int64_t     audio_pts_slip;
     int64_t     video_pts_slip;
     int64_t     pts_offset;
 
@@ -36,8 +36,7 @@ typedef struct
 typedef struct
 {
     int          index;
-    int64_t      next_start;    /* start time of next output frame */
-    int64_t      next_pts;     /* start time of next input frame */
+    double       next_start;   /* start time of next output frame */
     int64_t      first_drop;   /* PTS of first 'went backwards' frame dropped */
     int          drop_count;   /* count of 'time went backwards' drops */
 
@@ -56,7 +55,6 @@ typedef struct
     int        first_frame;
     int64_t    pts_skip;
     int64_t    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 */
     int        drops;         /* frames dropped to make a cbr video stream */
@@ -247,6 +245,8 @@ void syncVideoClose( hb_work_object_t * w )
  ***********************************************************************
  *
  **********************************************************************/
+static hb_buffer_t * copy_subtitle( hb_buffer_t * src );
+
 int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
               hb_buffer_t ** buf_out )
 {
@@ -256,11 +256,38 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
     hb_subtitle_t     * subtitle;
     hb_sync_video_t   * sync = &pv->type.video;
     int i;
+    int64_t start, next_start;
 
     *buf_out = NULL;
     next = *buf_in;
     *buf_in = NULL;
 
+    /* Wait till we can determine the initial pts of all streams */
+    if( next->size != 0 && pv->common->pts_offset == INT64_MIN )
+    {
+        pv->common->first_pts[0] = next->start;
+        hb_lock( pv->common->mutex );
+        while( pv->common->pts_offset == INT64_MIN )
+        {
+            // Full fifos will make us wait forever, so get the
+            // pts offset from the available streams if full
+            if ( hb_fifo_is_full( job->fifo_raw ) )
+            {
+                getPtsOffset( w );
+                hb_cond_broadcast( pv->common->next_frame );
+            }
+            else if ( checkPtsOffset( w ) )
+                hb_cond_broadcast( pv->common->next_frame );
+            else
+                hb_cond_timedwait( pv->common->next_frame, pv->common->mutex, 200 );
+        }
+        hb_unlock( pv->common->mutex );
+    }
+
+    hb_lock( pv->common->mutex );
+    next_start = next->start - pv->common->video_pts_slip;
+    hb_unlock( pv->common->mutex );
+
     /* Wait for start of point-to-point encoding */
     if( !pv->common->start_found )
     {
@@ -307,16 +334,20 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
             }
             hb_lock( pv->common->mutex );
             // Tell the audio threads what must be dropped
-            pv->common->audio_pts_thresh = next->start;
+            pv->common->audio_pts_thresh = next_start + pv->common->video_pts_slip;
             hb_cond_broadcast( pv->common->next_frame );
             hb_unlock( pv->common->mutex );
 
-            UpdateSearchState( w, next->start );
+            UpdateSearchState( w, next_start );
             hb_buffer_close( &next );
 
             return HB_WORK_OK;
         }
         hb_lock( pv->common->mutex );
+        pv->common->audio_pts_thresh = 0;
+        pv->common->audio_pts_slip += next_start;
+        pv->common->video_pts_slip += next_start;
+        next_start = 0;
         pv->common->start_found = 1;
         pv->common->count_frames = 0;
         hb_cond_broadcast( pv->common->next_frame );
@@ -324,28 +355,6 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         sync->st_first = 0;
     }
 
-    /* Wait till we can determine the initial pts of all streams */
-    if( pv->common->pts_offset == INT64_MIN )
-    {
-        pv->common->first_pts[0] = next->start;
-        hb_lock( pv->common->mutex );
-        while( pv->common->pts_offset == INT64_MIN )
-        {
-            // Full fifos will make us wait forever, so get the
-            // pts offset from the available streams if full
-            if ( hb_fifo_is_full( job->fifo_raw ) )
-            {
-                getPtsOffset( w );
-                hb_cond_broadcast( pv->common->next_frame );
-            }
-            else if ( checkPtsOffset( w ) )
-                hb_cond_broadcast( pv->common->next_frame );
-            else
-                hb_cond_timedwait( pv->common->next_frame, pv->common->mutex, 200 );
-        }
-        hb_unlock( pv->common->mutex );
-    }
-
     if( !sync->cur )
     {
         sync->cur = next;
@@ -450,13 +459,16 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         return HB_WORK_DONE;
     }
 
+    hb_lock( pv->common->mutex );
+    start = cur->start - pv->common->video_pts_slip;
+    hb_unlock( pv->common->mutex );
+
     /* Check for end of point-to-point pts encoding */
     if( job->pts_to_stop && sync->next_start >= job->pts_to_stop )
     {
         // Drop an empty buffer into our output to ensure that things
         // get flushed all the way out.
-        hb_log( "sync: reached pts %"PRId64", exiting early",
-                sync->cur->start );
+        hb_log( "sync: reached pts %"PRId64", exiting early", start );
         hb_buffer_close( &sync->cur );
         hb_buffer_close( &next );
         *buf_out = hb_buffer_init( 0 );
@@ -481,7 +493,7 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
     if( sync->first_frame )
     {
         /* This is our first frame */
-        if ( cur->start > pv->common->pts_offset )
+        if ( start > 0 )
         {
             /*
              * The first pts from a dvd should always be zero but
@@ -491,8 +503,8 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
              * as if it started at zero so that our audio timing will
              * be in sync.
              */
-            hb_log( "sync: first pts is %"PRId64, cur->start );
-            cur->start = pv->common->pts_offset;
+            hb_log( "sync: first pts is %"PRId64, start );
+            start = 0;
         }
         sync->first_frame = 0;
     }
@@ -508,20 +520,13 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
      * can deal with overlaps of up to a frame time but anything larger
      * we handle by dropping frames here.
      */
-    hb_lock( pv->common->mutex );
-    if ( (int64_t)( next->start - pv->common->video_pts_slip - cur->start ) <= 0 )
+    if ( next_start - start <= 0 )
     {
         if ( sync->first_drop == 0 )
         {
-            sync->first_drop = next->start;
+            sync->first_drop = next_start;
         }
         ++sync->drop_count;
-        if (next->start - cur->start > 0)
-        {
-            sync->pts_skip += next->start - cur->start;
-            pv->common->video_pts_slip -= next->start - cur->start;
-        }
-        hb_unlock( pv->common->mutex );
         if ( next->new_chap )
         {
             // don't drop a chapter mark when we drop the buffer
@@ -530,13 +535,12 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         hb_buffer_close( &next );
         return HB_WORK_OK;
     }
-    hb_unlock( pv->common->mutex );
     if ( sync->first_drop )
     {
         hb_log( "sync: video time didn't advance - dropped %d frames "
                 "(delta %d ms, current %"PRId64", next %"PRId64", dur %d)",
-                sync->drop_count, (int)( cur->start - sync->first_drop ) / 90,
-                cur->start, next->start, (int)( next->start - cur->start ) );
+                sync->drop_count, (int)( start - sync->first_drop ) / 90,
+                start, next_start, (int)( next_start - start ) );
         sync->first_drop = 0;
         sync->drop_count = 0;
     }
@@ -557,17 +561,16 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
      */
     for( i = 0; i < hb_list_count( job->list_subtitle ); i++)
     {
+        int64_t sub_start, sub_stop, duration;
         subtitle = hb_list_item( job->list_subtitle, i );
 
         /*
          * Rewrite timestamps on subtitles that need it (on raw queue).
          */
-        if( subtitle->source == CC608SUB ||
-            subtitle->source == CC708SUB ||
-            subtitle->source == SRTSUB ||
-            subtitle->source == UTF8SUB ||
-            subtitle->source == TX3GSUB ||
-            subtitle->source == SSASUB)
+        // NOTE: It's probably fine to use this logic for passthru VOBSUBs as well,
+        //       but I am currently preserving backwards compatibility with the old
+        //       VOBSUB behavior, which uses the more complex logic following this if-statement.
+        if( subtitle->config.dest == PASSTHRUSUB && subtitle->source != VOBSUB )
         {
             /*
              * Rewrite timestamps on subtitles that came from Closed Captions
@@ -575,6 +578,12 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
              */
             while( ( sub = hb_fifo_see( subtitle->fifo_raw ) ) )
             {
+                hb_lock( pv->common->mutex );
+                sub_start = sub->start - pv->common->video_pts_slip;
+                hb_unlock( pv->common->mutex );
+                duration = sub->stop - sub->start;
+                sub_stop = sub_start + duration;
+
                 /*
                  * Rewrite the timestamps as and when the video
                  * (cur->start) reaches the same timestamp as a
@@ -601,19 +610,26 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
                      * so just push them through for rendering.
                      *
                      */
-                    if( sub->start < cur->start )
+                    if( sub_start <= start )
                     {
                         sub = hb_fifo_get( subtitle->fifo_raw );
+                        sub->start = sub_start;
+                        sub->stop = sub_stop;
                         hb_fifo_push( subtitle->fifo_out, sub );
                     } else {
+                        // sub too early. Leave it in the fifo.
                         sub = NULL;
                         break;
                     }
                 }
             }
+            
+            continue;
         }
 
-        if( subtitle->source == VOBSUB ) 
+        // For rendered subtitles (and, for backward compatibility, passthru VOBSUBs),
+        // delay pushing subtitle packets through the pipeline until the video catches up
+        if( subtitle->config.dest == RENDERSUB || subtitle->source == VOBSUB ) 
         {
             hb_buffer_t * sub2;
             while( ( sub = hb_fifo_see( subtitle->fifo_raw ) ) )
@@ -626,6 +642,12 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
                     break;
                 }
 
+                hb_lock( pv->common->mutex );
+                sub_start = sub->start - pv->common->video_pts_slip;
+                hb_unlock( pv->common->mutex );
+                duration = sub->stop - sub->start;
+                sub_stop = sub_start + duration;
+
                 /* If two subtitles overlap, make the first one stop
                    when the second one starts */
                 sub2 = hb_fifo_see2( subtitle->fifo_raw );
@@ -649,7 +671,8 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
                     break;
                 }
                 
-                if( sub->stop > cur->start ) {
+                if( sub_stop > start ) 
+                {
                     /*
                      * The stop time is in the future, so fall through
                      * and we'll deal with it in the next block of
@@ -659,15 +682,15 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
                     /*
                      * There is a valid subtitle, is it time to display it?
                      */
-                    if( sub->stop > sub->start)
+                    if( sub_stop > sub_start)
                     {
                         /*
                          * Normal subtitle which ends after it starts, 
                          * check to see that the current video is between 
                          * the start and end.
                          */
-                        if( cur->start > sub->start &&
-                            cur->start < sub->stop )
+                        if( start > sub_start &&
+                            start < sub_stop )
                         {
                             /*
                             * We should be playing this, so leave the
@@ -675,28 +698,6 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
                             *
                             * fall through to display
                             */
-                            if( ( sub->stop - sub->start ) < ( 2 * 90000 ) )
-                            {
-                                /*
-                                 * Subtitle is on for less than three 
-                                 * seconds, extend the time that it is 
-                                 * displayed to make it easier to read. 
-                                 * Make it 3 seconds or until the next
-                                 * subtitle is displayed.
-                                 *
-                                 * This is in response to Indochine which 
-                                 * only displays subs for 1 second - 
-                                 * too fast to read.
-                                 */
-                                sub->stop = sub->start + ( 2 * 90000 );
-                            
-                                sub2 = hb_fifo_see2( subtitle->fifo_raw );
-                            
-                                if( sub2 && sub->stop > sub2->start )
-                                {
-                                    sub->stop = sub2->start;
-                                }
-                            }
                         }
                         else
                         {
@@ -713,14 +714,14 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
                          * The end of the subtitle is less than the start, 
                          * this is a sign of a PTS discontinuity.
                          */
-                        if( sub->start > cur->start )
+                        if( sub_start > start )
                         {
                             /*
                              * we haven't reached the start time yet, or
                              * we have jumped backwards after having
                              * already started this subtitle.
                              */
-                            if( cur->start < sub->stop )
+                            if( start < sub_stop )
                             {
                                 /*
                                  * We have jumped backwards and so should
@@ -760,30 +761,33 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
             }
             
             /* If we have a subtitle for this picture, copy it */
-            /* FIXME: we should avoid this memcpy */
             if( sub )
             {
                 if( sub->size > 0 )
                 {
                     if( subtitle->config.dest == RENDERSUB )
                     {
+                        // Only allow one subtitle to be showing at once; ignore others
                         if ( cur->sub == NULL )
                         {
                             /*
                              * Tack onto the video buffer for rendering
                              */
-                            cur->sub         = hb_buffer_init( sub->size );
-                            cur->sub->x      = sub->x;
-                            cur->sub->y      = sub->y;
-                            cur->sub->width  = sub->width;
-                            cur->sub->height = sub->height;
-                            memcpy( cur->sub->data, sub->data, sub->size ); 
+                            /* FIXME: we should avoid this memcpy */
+                            cur->sub = copy_subtitle( sub );
+                            cur->sub->start = sub_start;
+                            cur->sub->stop = sub_stop;
+                            
+                            // Leave the subtitle on the raw queue
+                            // (until it no longer needs to be displayed)
                         }
                     } else {
                         /*
                          * Pass-Through, pop it off of the raw queue, 
                          */
                         sub = hb_fifo_get( subtitle->fifo_raw );
+                        sub->start = sub_start;
+                        sub->stop = sub_stop;
                         hb_fifo_push( subtitle->fifo_sync, sub );
                     }
                 } else {
@@ -796,6 +800,8 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
                         hb_buffer_close( &sub );
                     } else {
                         sub = hb_fifo_get( subtitle->fifo_raw );
+                        sub->start = sub_start;
+                        sub->stop = sub_stop;
                         hb_fifo_push( subtitle->fifo_sync, sub );
                     }
                 }
@@ -819,13 +825,12 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
     *buf_out = cur;
     sync->cur = cur = next;
     cur->sub = NULL;
-    sync->next_pts = cur->start;
-    int64_t duration = cur->start - sync->pts_skip - (*buf_out)->start;
+    int64_t duration = next_start - start;
     sync->pts_skip = 0;
     if ( duration <= 0 )
     {
         hb_log( "sync: invalid video duration %"PRId64", start %"PRId64", next %"PRId64"",
-                duration, (*buf_out)->start, next->start );
+                duration, start, next_start );
     }
 
     (*buf_out)->start = sync->next_start;
@@ -846,6 +851,29 @@ int syncVideoWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
     return HB_WORK_OK;
 }
 
+static hb_buffer_t * copy_subtitle( hb_buffer_t * src_list )
+{
+    hb_buffer_t * dst_list = NULL;
+    
+    hb_buffer_t * src;
+    hb_buffer_t * dst;
+    hb_buffer_t ** dst_ptr = &dst_list;
+    for ( src = src_list, dst_ptr = &dst_list;
+          src;
+          src = src->next_subpicture, dst_ptr = &dst->next_subpicture )
+    {
+        (*dst_ptr)  = hb_buffer_init( src->size );
+        dst         = (*dst_ptr); 
+        dst->x      = src->x;
+        dst->y      = src->y;
+        dst->width  = src->width;
+        dst->height = src->height;
+        memcpy( dst->data, src->data, src->size );
+    }
+    
+    return dst_list;
+}
+
 // sync*Init does nothing because sync has a special initializer
 // that takes care of initializing video and all audio tracks
 int syncVideoInit( hb_work_object_t * w, hb_job_t * job)
@@ -872,7 +900,7 @@ void syncAudioClose( hb_work_object_t * w )
     hb_work_private_t * pv    = w->private_data;
     hb_sync_audio_t   * sync  = &pv->type.audio;
 
-    if( w->audio->config.out.codec == HB_ACODEC_AC3 )
+    if( w->audio->config.out.codec == HB_ACODEC_AC3_PASS )
     {
         free( sync->ac3_buf );
     }
@@ -927,8 +955,31 @@ static int syncAudioWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         return HB_WORK_DONE;
     }
 
+    /* Wait till we can determine the initial pts of all streams */
+    if( pv->common->pts_offset == INT64_MIN )
+    {
+        pv->common->first_pts[sync->index+1] = buf->start;
+        hb_lock( pv->common->mutex );
+        while( pv->common->pts_offset == INT64_MIN )
+        {
+            // Full fifos will make us wait forever, so get the
+            // pts offset from the available streams if full
+            if (hb_fifo_is_full(w->fifo_in))
+            {
+                getPtsOffset( w );
+                hb_cond_broadcast( pv->common->next_frame );
+            }
+            else if ( checkPtsOffset( w ) )
+                hb_cond_broadcast( pv->common->next_frame );
+            else
+                hb_cond_timedwait( pv->common->next_frame, pv->common->mutex, 200 );
+        }
+        hb_unlock( pv->common->mutex );
+    }
+
     /* Wait for start frame if doing point-to-point */
     hb_lock( pv->common->mutex );
+    start = buf->start - pv->common->audio_pts_slip;
     while ( !pv->common->start_found )
     {
         if ( pv->common->audio_pts_thresh < 0 )
@@ -949,8 +1000,9 @@ static int syncAudioWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         {
             hb_cond_timedwait( pv->common->next_frame, pv->common->mutex, 200 );
         }
+        start = buf->start - pv->common->audio_pts_slip;
     }
-    if ( buf->start < pv->common->audio_pts_thresh )
+    if ( start < 0 )
     {
         hb_buffer_close( &buf );
         hb_unlock( pv->common->mutex );
@@ -958,28 +1010,6 @@ static int syncAudioWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
     }
     hb_unlock( pv->common->mutex );
 
-    /* Wait till we can determine the initial pts of all streams */
-    if( pv->common->pts_offset == INT64_MIN )
-    {
-        pv->common->first_pts[sync->index+1] = buf->start;
-        hb_lock( pv->common->mutex );
-        while( pv->common->pts_offset == INT64_MIN )
-        {
-            // Full fifos will make us wait forever, so get the
-            // pts offset from the available streams if full
-            if (hb_fifo_is_full(w->fifo_in))
-            {
-                getPtsOffset( w );
-                hb_cond_broadcast( pv->common->next_frame );
-            }
-            else if ( checkPtsOffset( w ) )
-                hb_cond_broadcast( pv->common->next_frame );
-            else
-                hb_cond_timedwait( pv->common->next_frame, pv->common->mutex, 200 );
-        }
-        hb_unlock( pv->common->mutex );
-    }
-
     if( job->frame_to_stop && pv->common->count_frames >= job->frame_to_stop )
     {
         hb_buffer_close( &buf );
@@ -994,10 +1024,7 @@ static int syncAudioWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
         return HB_WORK_DONE;
     }
 
-    hb_lock( pv->common->mutex );
-    start = buf->start - pv->common->audio_passthru_slip;
-    hb_unlock( pv->common->mutex );
-    if ( (int64_t)( start - sync->next_pts ) < 0 )
+    if ( start - sync->next_start < 0 )
     {
         // audio time went backwards.
         // If our output clock is more than a half frame ahead of the
@@ -1008,36 +1035,34 @@ static int syncAudioWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
             // Discard data that's in the past.
             if ( sync->first_drop == 0 )
             {
-                sync->first_drop = sync->next_pts;
+                sync->first_drop = sync->next_start;
             }
             ++sync->drop_count;
             hb_buffer_close( &buf );
             return HB_WORK_OK;
         }
-        sync->next_pts = 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 %"PRId64", current %"PRId64")", w->audio->id,
-                (int)( sync->next_pts - sync->first_drop ) / 90,
-                sync->drop_count, sync->first_drop, sync->next_pts );
+                (int)( sync->next_start - sync->first_drop ) / 90,
+                sync->drop_count, sync->first_drop, (int64_t)sync->next_start );
         sync->first_drop = 0;
         sync->drop_count = 0;
-        sync->next_pts = start;
     }
-    if ( start - sync->next_pts >= (90 * 70) )
+    if ( start - sync->next_start >= (90 * 70) )
     {
-        if ( start - sync->next_pts > (90000LL * 60) )
+        if ( start - sync->next_start > (90000LL * 60) )
         {
             // there's a gap of more than a minute between the last
             // frame and this. assume we got a corrupted timestamp
             // and just drop the next buf.
             hb_log( "sync: %d minute time gap in audio %d - dropping buf"
                     "  start %"PRId64", next %"PRId64,
-                    (int)((start - sync->next_pts) / (90000*60)),
-                    w->audio->id, start, sync->next_pts );
+                    (int)((start - sync->next_start) / (90000*60)),
+                    w->audio->id, start, (int64_t)sync->next_start );
             hb_buffer_close( &buf );
             return HB_WORK_OK;
         }
@@ -1047,24 +1072,24 @@ static int syncAudioWork( hb_work_object_t * w, hb_buffer_t ** buf_in,
          * Or in the case of DCA, skip some frames from the
          * other streams.
          */
-        if( w->audio->config.out.codec == HB_ACODEC_DCA )
+        if( w->audio->config.out.codec == HB_ACODEC_DCA_PASS )
         {
             hb_log( "sync: audio gap %d ms. Skipping frames. Audio %d"
                     "  start %"PRId64", next %"PRId64,
-                    (int)((start - sync->next_pts) / 90),
-                    w->audio->id, start, sync->next_pts );
+                    (int)((start - sync->next_start) / 90),
+                    w->audio->id, start, (int64_t)sync->next_start );
             hb_lock( pv->common->mutex );
-            pv->common->audio_passthru_slip += (start - sync->next_pts);
-            pv->common->video_pts_slip += (start - sync->next_pts);
+            pv->common->audio_pts_slip += (start - sync->next_start);
+            pv->common->video_pts_slip += (start - sync->next_start);
             hb_unlock( pv->common->mutex );
             *buf_out = buf;
             return HB_WORK_OK;
         }
         hb_log( "sync: adding %d ms of silence to audio %d"
                 "  start %"PRId64", next %"PRId64,
-                (int)((start - sync->next_pts) / 90),
-                w->audio->id, start, sync->next_pts );
-        InsertSilence( w, start - sync->next_pts );
+                (int)((start - sync->next_start) / 90),
+                w->audio->id, start, (int64_t)sync->next_start );
+        InsertSilence( w, start - sync->next_start );
     }
 
     /*
@@ -1105,8 +1130,8 @@ static void InitAudio( hb_job_t * job, hb_sync_common_t * common, int i )
     w->audio = hb_list_item( title->list_audio, i );
     w->fifo_in = w->audio->priv.fifo_raw;
 
-    if( w->audio->config.out.codec == HB_ACODEC_AC3 ||
-        w->audio->config.out.codec == HB_ACODEC_DCA )
+    if( w->audio->config.out.codec == HB_ACODEC_AC3_PASS ||
+        w->audio->config.out.codec == HB_ACODEC_DCA_PASS )
     {
         w->fifo_out = w->audio->priv.fifo_out;
     }
@@ -1115,7 +1140,7 @@ static void InitAudio( hb_job_t * job, hb_sync_common_t * common, int i )
         w->fifo_out = w->audio->priv.fifo_sync;
     }
 
-    if( w->audio->config.out.codec == HB_ACODEC_AC3 )
+    if( w->audio->config.out.codec == HB_ACODEC_AC3_PASS )
     {
         /* Have a silent AC-3 frame ready in case we have to fill a
            gap */
@@ -1167,14 +1192,12 @@ 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;
-
-    sync->next_pts += duration;
+    int64_t start = (int64_t)sync->next_start;
+    double duration = buf->stop - buf->start;
 
     if( audio->config.in.samplerate == audio->config.out.samplerate ||
-        audio->config.out.codec == HB_ACODEC_AC3 ||
-        audio->config.out.codec == HB_ACODEC_DCA )
+        audio->config.out.codec == HB_ACODEC_AC3_PASS ||
+        audio->config.out.codec == HB_ACODEC_DCA_PASS )
     {
         /*
          * If we don't have to do sample rate conversion or this audio is 
@@ -1218,13 +1241,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;
 }
 
@@ -1245,10 +1268,10 @@ static void InsertSilence( hb_work_object_t * w, int64_t duration )
 
     while ( --frame_count >= 0 )
     {
-        if( w->audio->config.out.codec == HB_ACODEC_AC3 )
+        if( w->audio->config.out.codec == HB_ACODEC_AC3_PASS )
         {
             buf        = hb_buffer_init( sync->ac3_size );
-            buf->start = sync->next_pts;
+            buf->start = sync->next_start;
             buf->stop  = buf->start + frame_dur;
             memcpy( buf->data, sync->ac3_buf, buf->size );
             fifo = w->audio->priv.fifo_out;
@@ -1258,7 +1281,7 @@ static void InsertSilence( hb_work_object_t * w, int64_t duration )
             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->start = sync->next_start;
             buf->stop  = buf->start + frame_dur;
             memset( buf->data, 0, buf->size );
             fifo = w->audio->priv.fifo_sync;
@@ -1396,7 +1419,7 @@ static void getPtsOffset( hb_work_object_t * w )
         if ( pv->common->first_pts[i] < first_pts )
             first_pts = pv->common->first_pts[i];
     }
-    pv->common->audio_passthru_slip = pv->common->pts_offset = first_pts;
+    pv->common->video_pts_slip = pv->common->audio_pts_slip = pv->common->pts_offset = first_pts;
     return;
 }