OSDN Git Service

09ef0d9a27317cf4cca8dd2a4a210777481965ba
[handbrake-jp/handbrake-jp-git.git] / libhb / sync.c
1 /* $Id: sync.c,v 1.38 2005/04/14 21:57:58 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.fr/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "hb.h"
8 #include <stdio.h>
9
10 #include "samplerate.h"
11 #include "libavcodec/avcodec.h"
12
13 #ifdef INT64_MIN
14 #undef INT64_MIN /* Because it isn't defined correctly in Zeta */
15 #endif
16 #define INT64_MIN (-9223372036854775807LL-1)
17
18 #define AC3_SAMPLES_PER_FRAME 1536
19
20 typedef struct
21 {
22     hb_audio_t * audio;
23
24     int64_t      next_start;    /* start time of next output frame */
25     int64_t      next_pts;      /* start time of next input frame */
26     int64_t      first_drop;    /* PTS of first 'went backwards' frame dropped */
27     int          drop_count;    /* count of 'time went backwards' drops */
28
29     /* Raw */
30     SRC_STATE  * state;
31     SRC_DATA     data;
32
33     /* AC-3 */
34     int          ac3_size;
35     uint8_t    * ac3_buf;
36
37 } hb_sync_audio_t;
38
39 struct hb_work_private_s
40 {
41     hb_job_t * job;
42     int        busy;            // bitmask with one bit for each active input
43                                 // (bit 0 = video; 1 = audio 0, 2 = audio 1, ...
44                                 // appropriate bit is cleared when input gets
45                                 // an eof buf. syncWork returns done when all
46                                 // bits are clear.
47     /* Video */
48     hb_subtitle_t * subtitle;
49     int64_t pts_offset;
50     int64_t next_start;         /* start time of next output frame */
51     int64_t next_pts;           /* start time of next input frame */
52     int64_t first_drop;         /* PTS of first 'went backwards' frame dropped */
53     int drop_count;             /* count of 'time went backwards' drops */
54     int drops;                  /* frames dropped to make a cbr video stream */
55     int dups;                   /* frames duplicated to make a cbr video stream */
56     int video_sequence;
57     int count_frames;
58     int count_frames_max;
59     int chap_mark;              /* to propagate chapter mark across a drop */
60     hb_buffer_t * cur; /* The next picture to process */
61
62     /* Audio */
63     hb_sync_audio_t sync_audio[8];
64
65     /* Statistics */
66     uint64_t st_counts[4];
67     uint64_t st_dates[4];
68     uint64_t st_first;
69 };
70
71 /***********************************************************************
72  * Local prototypes
73  **********************************************************************/
74 static void InitAudio( hb_work_object_t * w, int i );
75 static void SyncVideo( hb_work_object_t * w );
76 static void SyncAudio( hb_work_object_t * w, int i );
77 static void InsertSilence( hb_work_object_t * w, int i, int64_t d );
78 static void UpdateState( hb_work_object_t * w );
79
80 /***********************************************************************
81  * hb_work_sync_init
82  ***********************************************************************
83  * Initialize the work object
84  **********************************************************************/
85 int syncInit( hb_work_object_t * w, hb_job_t * job )
86 {
87     hb_title_t       * title = job->title;
88     hb_chapter_t     * chapter;
89     int                i;
90     uint64_t           duration;
91     hb_work_private_t * pv;
92
93     pv = calloc( 1, sizeof( hb_work_private_t ) );
94     w->private_data = pv;
95
96     pv->job            = job;
97     pv->pts_offset     = INT64_MIN;
98
99     /* Calculate how many video frames we are expecting */
100     duration = 0;
101     for( i = job->chapter_start; i <= job->chapter_end; i++ )
102     {
103         chapter   = hb_list_item( title->list_chapter, i - 1 );
104         duration += chapter->duration;
105     }
106     duration += 90000;
107         /* 1 second safety so we're sure we won't miss anything */
108     pv->count_frames_max = duration * job->vrate / job->vrate_base / 90000;
109
110     hb_log( "sync: expecting %d video frames", pv->count_frames_max );
111     pv->busy |= 1;
112
113     /* Initialize libsamplerate for every audio track we have */
114     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
115     {
116         pv->busy |= ( 1 << (i + 1) );
117         InitAudio( w, i );
118     }
119
120     /* Get subtitle info, if any */
121     pv->subtitle = hb_list_item( title->list_subtitle, 0 );
122
123     return 0;
124 }
125
126 /***********************************************************************
127  * Close
128  ***********************************************************************
129  *
130  **********************************************************************/
131 void syncClose( hb_work_object_t * w )
132 {
133     hb_work_private_t * pv = w->private_data;
134     hb_job_t          * job   = pv->job;
135     hb_title_t        * title = job->title;
136     hb_audio_t        * audio = NULL;
137     int i;
138
139     if( pv->cur )
140     {
141         hb_buffer_close( &pv->cur );
142     }
143
144     hb_log( "sync: got %d frames, %d expected",
145             pv->count_frames, pv->count_frames_max );
146
147     if (pv->drops || pv->dups )
148     {
149         hb_log( "sync: %d frames dropped, %d duplicated", pv->drops, pv->dups );
150     }
151
152     for( i = 0; i < hb_list_count( title->list_audio ); i++ )
153     {
154         audio = hb_list_item( title->list_audio, i );
155         if( audio->config.out.codec == HB_ACODEC_AC3 )
156         {
157             free( pv->sync_audio[i].ac3_buf );
158         }
159         else
160         {
161             src_delete( pv->sync_audio[i].state );
162         }
163     }
164
165     free( pv );
166     w->private_data = NULL;
167 }
168
169 /***********************************************************************
170  * Work
171  ***********************************************************************
172  * The root routine of this work abject
173  *
174  * The way this works is that we are syncing the audio to the PTS of
175  * the last video that we processed. That's why we skip the audio sync
176  * if we haven't got a valid PTS from the video yet.
177  *
178  **********************************************************************/
179 int syncWork( hb_work_object_t * w, hb_buffer_t ** unused1,
180               hb_buffer_t ** unused2 )
181 {
182     hb_work_private_t * pv = w->private_data;
183     int i;
184
185     if ( pv->busy & 1 )
186         SyncVideo( w );
187
188     /* If we ever got a video frame, handle audio now */
189     if( pv->pts_offset != INT64_MIN )
190     {
191         for( i = 0; i < hb_list_count( pv->job->title->list_audio ); i++ )
192         {
193             if ( pv->busy & ( 1 << (i + 1) ) )
194                 SyncAudio( w, i );
195         }
196     }
197
198     return ( pv->busy? HB_WORK_OK : HB_WORK_DONE );
199 }
200
201 hb_work_object_t hb_sync =
202 {
203     WORK_SYNC,
204     "Synchronization",
205     syncInit,
206     syncWork,
207     syncClose
208 };
209
210 static void InitAudio( hb_work_object_t * w, int i )
211 {
212     hb_work_private_t * pv = w->private_data;
213     hb_job_t        * job   = pv->job;
214     hb_title_t      * title = job->title;
215     hb_sync_audio_t * sync;
216
217     sync        = &pv->sync_audio[i];
218     sync->audio = hb_list_item( title->list_audio, i );
219
220     if( sync->audio->config.out.codec == HB_ACODEC_AC3 )
221     {
222         /* Have a silent AC-3 frame ready in case we have to fill a
223            gap */
224         AVCodec        * codec;
225         AVCodecContext * c;
226         short          * zeros;
227
228         codec = avcodec_find_encoder( CODEC_ID_AC3 );
229         c     = avcodec_alloc_context();
230
231         c->bit_rate    = sync->audio->config.in.bitrate;
232         c->sample_rate = sync->audio->config.in.samplerate;
233         c->channels    = HB_INPUT_CH_LAYOUT_GET_DISCRETE_COUNT( sync->audio->config.in.channel_layout );
234
235         if( avcodec_open( c, codec ) < 0 )
236         {
237             hb_log( "sync: avcodec_open failed" );
238             return;
239         }
240
241         zeros          = calloc( AC3_SAMPLES_PER_FRAME *
242                                  sizeof( short ) * c->channels, 1 );
243         sync->ac3_size = sync->audio->config.in.bitrate * AC3_SAMPLES_PER_FRAME /
244                              sync->audio->config.in.samplerate / 8;
245         sync->ac3_buf  = malloc( sync->ac3_size );
246
247         if( avcodec_encode_audio( c, sync->ac3_buf, sync->ac3_size,
248                                   zeros ) != sync->ac3_size )
249         {
250             hb_log( "sync: avcodec_encode_audio failed" );
251         }
252
253         free( zeros );
254         avcodec_close( c );
255         av_free( c );
256     }
257     else
258     {
259         /* Initialize libsamplerate */
260         int error;
261         sync->state             = src_new( SRC_SINC_MEDIUM_QUALITY, HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(sync->audio->config.out.mixdown), &error );
262         sync->data.end_of_input = 0;
263     }
264 }
265
266 /***********************************************************************
267  * SyncVideo
268  ***********************************************************************
269  *
270  **********************************************************************/
271 static void SyncVideo( hb_work_object_t * w )
272 {
273     hb_work_private_t * pv = w->private_data;
274     hb_buffer_t * cur, * next, * sub = NULL;
275     hb_job_t * job = pv->job;
276
277     if( !pv->cur && !( pv->cur = hb_fifo_get( job->fifo_raw ) ) )
278     {
279         /* We haven't even got a frame yet */
280         return;
281     }
282     cur = pv->cur;
283     if( cur->size == 0 )
284     {
285         /* we got an end-of-stream. Feed it downstream & signal that we're done. */
286         hb_fifo_push( job->fifo_sync, hb_buffer_init( 0 ) );
287         pv->busy &=~ 1;
288         return;
289     }
290
291     /* At this point we have a frame to process. Let's check
292         1) if we will be able to push into the fifo ahead
293         2) if the next frame is there already, since we need it to
294            compute the duration of the current frame*/
295     while( !hb_fifo_is_full( job->fifo_sync ) &&
296            ( next = hb_fifo_see( job->fifo_raw ) ) )
297     {
298         hb_buffer_t * buf_tmp;
299
300         if( next->size == 0 )
301         {
302             /* we got an end-of-stream. Feed it downstream & signal that
303              * we're done. Note that this means we drop the final frame of
304              * video (we don't know its duration). On DVDs the final frame
305              * is often strange and dropping it seems to be a good idea. */
306             hb_fifo_push( job->fifo_sync, hb_buffer_init( 0 ) );
307             pv->busy &=~ 1;
308             return;
309         }
310         if( pv->pts_offset == INT64_MIN )
311         {
312             /* This is our first frame */
313             pv->pts_offset = 0;
314             if ( cur->start != 0 )
315             {
316                 /*
317                  * The first pts from a dvd should always be zero but
318                  * can be non-zero with a transport or program stream since
319                  * we're not guaranteed to start on an IDR frame. If we get
320                  * a non-zero initial PTS extend its duration so it behaves
321                  * as if it started at zero so that our audio timing will
322                  * be in sync.
323                  */
324                 hb_log( "sync: first pts is %lld", cur->start );
325                 cur->start = 0;
326             }
327         }
328
329         /*
330          * since the first frame is always 0 and the upstream reader code
331          * is taking care of adjusting for pts discontinuities, we just have
332          * to deal with the next frame's start being in the past. This can
333          * happen when the PTS is adjusted after data loss but video frame
334          * reordering causes some frames with the old clock to appear after
335          * the clock change. This creates frames that overlap in time which
336          * looks to us like time going backward. The downstream muxing code
337          * can deal with overlaps of up to a frame time but anything larger
338          * we handle by dropping frames here.
339          */
340         if ( (int64_t)( next->start - cur->start ) <= 0 )
341         {
342             if ( pv->first_drop == 0 )
343             {
344                 pv->first_drop = next->start;
345             }
346             ++pv->drop_count;
347             buf_tmp = hb_fifo_get( job->fifo_raw );
348             if ( buf_tmp->new_chap )
349             {
350                 // don't drop a chapter mark when we drop the buffer
351                 pv->chap_mark = buf_tmp->new_chap;
352             }
353             hb_buffer_close( &buf_tmp );
354             continue;
355         }
356         if ( pv->first_drop )
357         {
358             hb_log( "sync: video time didn't advance - dropped %d frames "
359                     "(delta %d ms, current %lld, next %lld, dur %d)",
360                     pv->drop_count, (int)( cur->start - pv->first_drop ) / 90,
361                     cur->start, next->start, (int)( next->start - cur->start ) );
362             pv->first_drop = 0;
363             pv->drop_count = 0;
364         }
365
366         /*
367          * Track the video sequence number localy so that we can sync the audio
368          * to it using the sequence number as well as the PTS.
369          */
370         pv->video_sequence = cur->sequence;
371
372         /* Look for a subtitle for this frame */
373         if( pv->subtitle )
374         {
375             hb_buffer_t * sub2;
376             while( ( sub = hb_fifo_see( pv->subtitle->fifo_raw ) ) )
377             {
378                 /* If two subtitles overlap, make the first one stop
379                    when the second one starts */
380                 sub2 = hb_fifo_see2( pv->subtitle->fifo_raw );
381                 if( sub2 && sub->stop > sub2->start )
382                     sub->stop = sub2->start;
383
384                 // hb_log("0x%x: video seq: %lld  subtitle sequence: %lld",
385                 //       sub, cur->sequence, sub->sequence);
386
387                 if( sub->sequence > cur->sequence )
388                 {
389                     /*
390                      * The video is behind where we are, so wait until
391                      * it catches up to the same reader point on the
392                      * DVD. Then our PTS should be in the same region
393                      * as the video.
394                      */
395                     sub = NULL;
396                     break;
397                 }
398
399                 if( sub->stop > cur->start ) {
400                     /*
401                      * The stop time is in the future, so fall through
402                      * and we'll deal with it in the next block of
403                      * code.
404                      */
405                     break;
406                 }
407
408                 /*
409                  * The subtitle is older than this picture, trash it
410                  */
411                 sub = hb_fifo_get( pv->subtitle->fifo_raw );
412                 hb_buffer_close( &sub );
413             }
414
415             /*
416              * There is a valid subtitle, is it time to display it?
417              */
418             if( sub )
419             {
420                 if( sub->stop > sub->start)
421                 {
422                     /*
423                      * Normal subtitle which ends after it starts, check to
424                      * see that the current video is between the start and end.
425                      */
426                     if( cur->start > sub->start &&
427                         cur->start < sub->stop )
428                     {
429                         /*
430                          * We should be playing this, so leave the
431                          * subtitle in place.
432                          *
433                          * fall through to display
434                          */
435                         if( ( sub->stop - sub->start ) < ( 3 * 90000 ) )
436                         {
437                             /*
438                              * Subtitle is on for less than three seconds, extend
439                              * the time that it is displayed to make it easier
440                              * to read. Make it 3 seconds or until the next
441                              * subtitle is displayed.
442                              *
443                              * This is in response to Indochine which only
444                              * displays subs for 1 second - too fast to read.
445                              */
446                             sub->stop = sub->start + ( 3 * 90000 );
447
448                             sub2 = hb_fifo_see2( pv->subtitle->fifo_raw );
449
450                             if( sub2 && sub->stop > sub2->start )
451                             {
452                                 sub->stop = sub2->start;
453                             }
454                         }
455                     }
456                     else
457                     {
458                         /*
459                          * Defer until the play point is within the subtitle
460                          */
461                         sub = NULL;
462                     }
463                 }
464                 else
465                 {
466                     /*
467                      * The end of the subtitle is less than the start, this is a
468                      * sign of a PTS discontinuity.
469                      */
470                     if( sub->start > cur->start )
471                     {
472                         /*
473                          * we haven't reached the start time yet, or
474                          * we have jumped backwards after having
475                          * already started this subtitle.
476                          */
477                         if( cur->start < sub->stop )
478                         {
479                             /*
480                              * We have jumped backwards and so should
481                              * continue displaying this subtitle.
482                              *
483                              * fall through to display.
484                              */
485                         }
486                         else
487                         {
488                             /*
489                              * Defer until the play point is within the subtitle
490                              */
491                             sub = NULL;
492                         }
493                     } else {
494                         /*
495                          * Play this subtitle as the start is greater than our
496                          * video point.
497                          *
498                          * fall through to display/
499                          */
500                     }
501                 }
502             }
503         }
504
505         int64_t duration;
506         if ( job->mux & HB_MUX_AVI || job->cfr )
507         {
508             /*
509              * The concept of variable frame rate video was a bit too advanced
510              * for Microsoft so AVI doesn't support it. Since almost all dvd
511              * video is VFR we have to convert it to constant frame rate to
512              * put it in an AVI container. So here we duplicate, drop and
513              * otherwise trash video frames to appease the gods of Redmond.
514              */
515
516             /* mpeg durations are exact when expressed in ticks of the
517              * 27MHz System clock but not in HB's 90KHz PTS clock. To avoid
518              * a truncation bias that will eventually cause the audio to desync
519              * we compute the duration of the next frame using 27MHz ticks
520              * then truncate it to 90KHz. */
521             duration = ( (int64_t)(pv->count_frames + 1 ) * job->vrate_base ) / 300 -
522                        pv->next_start;
523
524             /* We don't want the input & output clocks to be exactly in phase
525              * otherwise small variations in the time will cause us to think
526              * we're a full frame off & there will be lots of drops and dups.
527              * We offset the input clock by half the duration so it's maximally
528              * out of phase with the output clock. */
529             if( cur->start < pv->next_start  - ( duration >> 1 ) )
530             {
531                 /* current frame too old - drop it */
532                 if ( cur->new_chap )
533                 {
534                     pv->chap_mark = cur->new_chap;
535                 }
536                 hb_buffer_close( &cur );
537                 pv->cur = cur = hb_fifo_get( job->fifo_raw );
538                 pv->next_pts = next->start;
539                 ++pv->drops;
540                 continue;
541             }
542
543             if( next->start > pv->next_start + duration + ( duration >> 1 ) )
544             {
545                 /* next frame too far ahead - dup current frame */
546                 buf_tmp = hb_buffer_init( cur->size );
547                 hb_buffer_copy_settings( buf_tmp, cur );
548                 memcpy( buf_tmp->data, cur->data, cur->size );
549                 buf_tmp->sequence = cur->sequence;
550                 ++pv->dups;
551             }
552             else
553             {
554                 /* this frame in our time window & doesn't need to be duped */
555                 buf_tmp = cur;
556                 pv->cur = cur = hb_fifo_get( job->fifo_raw );
557                 pv->next_pts = next->start;
558             }
559         }
560         else
561         {
562             /*
563              * Adjust the pts of the current frame so that it's contiguous
564              * with the previous frame. The start time of the current frame
565              * has to be the end time of the previous frame and the stop
566              * time has to be the start of the next frame.  We don't
567              * make any adjustments to the source timestamps other than removing
568              * the clock offsets (which also removes pts discontinuities).
569              * This means we automatically encode at the source's frame rate.
570              * MP2 uses an implicit duration (frames end when the next frame
571              * starts) but more advanced containers like MP4 use an explicit
572              * duration. Since we're looking ahead one frame we set the
573              * explicit stop time from the start time of the next frame.
574              */
575             buf_tmp = cur;
576             pv->cur = cur = hb_fifo_get( job->fifo_raw );
577             pv->next_pts = cur->start;
578             duration = cur->start - buf_tmp->start;
579             if ( duration <= 0 )
580             {
581                 hb_log( "sync: invalid video duration %lld, start %lld, next %lld",
582                         duration, buf_tmp->start, next->start );
583             }
584         }
585
586         buf_tmp->start = pv->next_start;
587         pv->next_start += duration;
588         buf_tmp->stop = pv->next_start;
589
590         if ( pv->chap_mark )
591         {
592             // we have a pending chapter mark from a recent drop - put it on this
593             // buffer (this may make it one frame late but we can't do any better).
594             buf_tmp->new_chap = pv->chap_mark;
595             pv->chap_mark = 0;
596         }
597
598         /* If we have a subtitle for this picture, copy it */
599         /* FIXME: we should avoid this memcpy */
600         if( sub )
601         {
602             buf_tmp->sub         = hb_buffer_init( sub->size );
603             buf_tmp->sub->x      = sub->x;
604             buf_tmp->sub->y      = sub->y;
605             buf_tmp->sub->width  = sub->width;
606             buf_tmp->sub->height = sub->height;
607             memcpy( buf_tmp->sub->data, sub->data, sub->size );
608         }
609
610         /* Push the frame to the renderer */
611         hb_fifo_push( job->fifo_sync, buf_tmp );
612
613         /* Update UI */
614         UpdateState( w );
615
616         /* Make sure we won't get more frames then expected */
617         if( pv->count_frames >= pv->count_frames_max * 2)
618         {
619             hb_log( "sync: got too many frames (%d), exiting early",
620                     pv->count_frames );
621
622             // Drop an empty buffer into our output to ensure that things
623             // get flushed all the way out.
624             hb_fifo_push( job->fifo_sync, hb_buffer_init( 0 ) );
625             pv->busy &=~ 1;
626             return;
627         }
628     }
629 }
630
631 static void OutputAudioFrame( hb_job_t *job, hb_audio_t *audio, hb_buffer_t *buf,
632                               hb_sync_audio_t *sync, hb_fifo_t *fifo, int i )
633 {
634     int64_t start = sync->next_start;
635     int64_t duration = buf->stop - buf->start;
636
637     sync->next_pts += duration;
638
639     if( audio->config.in.samplerate == audio->config.out.samplerate ||
640         audio->config.out.codec == HB_ACODEC_AC3 ||
641         audio->config.out.codec == HB_ACODEC_DCA )
642     {
643         /*
644          * If we don't have to do sample rate conversion or this audio is 
645          * pass-thru just send the input buffer downstream after adjusting
646          * its timestamps to make the output stream continuous.
647          */
648     }
649     else
650     {
651         /* Not pass-thru - do sample rate conversion */
652         int count_in, count_out;
653         hb_buffer_t * buf_raw = buf;
654         int channel_count = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown) *
655                             sizeof( float );
656
657         count_in  = buf_raw->size / channel_count;
658         /*
659          * When using stupid rates like 44.1 there will always be some
660          * truncation error. E.g., a 1536 sample AC3 frame will turn into a
661          * 1536*44.1/48.0 = 1411.2 sample frame. If we just truncate the .2
662          * the error will build up over time and eventually the audio will
663          * substantially lag the video. libsamplerate will keep track of the
664          * fractional sample & give it to us when appropriate if we give it
665          * an extra sample of space in the output buffer.
666          */
667         count_out = ( duration * audio->config.out.samplerate ) / 90000 + 1;
668
669         sync->data.input_frames = count_in;
670         sync->data.output_frames = count_out;
671         sync->data.src_ratio = (double)audio->config.out.samplerate /
672                                (double)audio->config.in.samplerate;
673
674         buf = hb_buffer_init( count_out * channel_count );
675         sync->data.data_in  = (float *) buf_raw->data;
676         sync->data.data_out = (float *) buf->data;
677         if( src_process( sync->state, &sync->data ) )
678         {
679             /* XXX If this happens, we're screwed */
680             hb_log( "sync: audio %d src_process failed", i );
681         }
682         hb_buffer_close( &buf_raw );
683
684         buf->size = sync->data.output_frames_gen * channel_count;
685         duration = ( sync->data.output_frames_gen * 90000 ) /
686                    audio->config.out.samplerate;
687     }
688     buf->frametype = HB_FRAME_AUDIO;
689     buf->start = start;
690     buf->stop  = start + duration;
691     sync->next_start = start + duration;
692     hb_fifo_push( fifo, buf );
693 }
694
695 /***********************************************************************
696  * SyncAudio
697  ***********************************************************************
698  *
699  **********************************************************************/
700 static void SyncAudio( hb_work_object_t * w, int i )
701 {
702     hb_work_private_t * pv = w->private_data;
703     hb_job_t        * job = pv->job;
704     hb_sync_audio_t * sync = &pv->sync_audio[i];
705     hb_audio_t      * audio = sync->audio;
706     hb_buffer_t     * buf;
707     hb_fifo_t       * fifo;
708
709     if( audio->config.out.codec == HB_ACODEC_AC3 )
710     {
711         fifo = audio->priv.fifo_out;
712     }
713     else
714     {
715         fifo = audio->priv.fifo_sync;
716     }
717
718     while( !hb_fifo_is_full( fifo ) && ( buf = hb_fifo_see( audio->priv.fifo_raw ) ) )
719     {
720         /* if the next buffer is an eof send it downstream */
721         if ( buf->size <= 0 )
722         {
723             buf = hb_fifo_get( audio->priv.fifo_raw );
724             hb_fifo_push( fifo, buf );
725             pv->busy &=~ (1 << (i + 1) );
726             return;
727         }
728         if ( (int64_t)( buf->start - sync->next_pts ) < 0 )
729         {
730             // audio time went backwards.
731             // If our output clock is more than a half frame ahead of the
732             // input clock drop this frame to move closer to sync.
733             // Otherwise drop frames until the input clock matches the output clock.
734             if ( sync->first_drop || sync->next_start - buf->start > 90*15 )
735             {
736                 // Discard data that's in the past.
737                 if ( sync->first_drop == 0 )
738                 {
739                     sync->first_drop = sync->next_pts;
740                 }
741                 ++sync->drop_count;
742                 buf = hb_fifo_get( audio->priv.fifo_raw );
743                 hb_buffer_close( &buf );
744                 continue;
745             }
746             sync->next_pts = buf->start;
747         }
748         if ( sync->first_drop )
749         {
750             // we were dropping old data but input buf time is now current
751             hb_log( "sync: audio %d time went backwards %d ms, dropped %d frames "
752                     "(next %lld, current %lld)", i,
753                     (int)( sync->next_pts - sync->first_drop ) / 90,
754                     sync->drop_count, sync->first_drop, sync->next_pts );
755             sync->first_drop = 0;
756             sync->drop_count = 0;
757             sync->next_pts = buf->start;
758         }
759         if ( buf->start - sync->next_pts >= (90 * 70) )
760         {
761             /*
762              * there's a gap of at least 70ms between the last
763              * frame we processed & the next. Fill it with silence.
764              */
765             hb_log( "sync: adding %d ms of silence to audio %d"
766                     "  start %lld, next %lld",
767                     (int)((buf->start - sync->next_pts) / 90),
768                     i, buf->start, sync->next_pts );
769             InsertSilence( w, i, buf->start - sync->next_pts );
770             return;
771         }
772
773         /*
774          * When we get here we've taken care of all the dups and gaps in the
775          * audio stream and are ready to inject the next input frame into
776          * the output stream.
777          */
778         buf = hb_fifo_get( audio->priv.fifo_raw );
779         OutputAudioFrame( job, audio, buf, sync, fifo, i );
780     }
781 }
782
783 static void InsertSilence( hb_work_object_t * w, int i, int64_t duration )
784 {
785     hb_work_private_t * pv = w->private_data;
786     hb_job_t        *job = pv->job;
787     hb_sync_audio_t *sync = &pv->sync_audio[i];
788     hb_buffer_t     *buf;
789     hb_fifo_t       *fifo;
790
791     // to keep pass-thru and regular audio in sync we generate silence in
792     // AC3 frame-sized units. If the silence duration isn't an integer multiple
793     // of the AC3 frame duration we will truncate or round up depending on
794     // which minimizes the timing error.
795     const int frame_dur = ( 90000 * AC3_SAMPLES_PER_FRAME ) /
796                           sync->audio->config.in.samplerate;
797     int frame_count = ( duration + (frame_dur >> 1) ) / frame_dur;
798
799     while ( --frame_count >= 0 )
800     {
801         if( sync->audio->config.out.codec == HB_ACODEC_AC3 )
802         {
803             buf        = hb_buffer_init( sync->ac3_size );
804             buf->start = sync->next_pts;
805             buf->stop  = buf->start + frame_dur;
806             memcpy( buf->data, sync->ac3_buf, buf->size );
807             fifo = sync->audio->priv.fifo_out;
808         }
809         else
810         {
811             buf = hb_buffer_init( AC3_SAMPLES_PER_FRAME * sizeof( float ) *
812                                      HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(
813                                          sync->audio->config.out.mixdown) );
814             buf->start = sync->next_pts;
815             buf->stop  = buf->start + frame_dur;
816             memset( buf->data, 0, buf->size );
817             fifo = sync->audio->priv.fifo_sync;
818         }
819         OutputAudioFrame( job, sync->audio, buf, sync, fifo, i );
820     }
821 }
822
823 static void UpdateState( hb_work_object_t * w )
824 {
825     hb_work_private_t * pv = w->private_data;
826     hb_state_t state;
827
828     if( !pv->count_frames )
829     {
830         pv->st_first = hb_get_date();
831     }
832     pv->count_frames++;
833
834     if( hb_get_date() > pv->st_dates[3] + 1000 )
835     {
836         memmove( &pv->st_dates[0], &pv->st_dates[1],
837                  3 * sizeof( uint64_t ) );
838         memmove( &pv->st_counts[0], &pv->st_counts[1],
839                  3 * sizeof( uint64_t ) );
840         pv->st_dates[3]  = hb_get_date();
841         pv->st_counts[3] = pv->count_frames;
842     }
843
844 #define p state.param.working
845     state.state = HB_STATE_WORKING;
846     p.progress  = (float) pv->count_frames / (float) pv->count_frames_max;
847     if( p.progress > 1.0 )
848     {
849         p.progress = 1.0;
850     }
851     p.rate_cur   = 1000.0 *
852         (float) ( pv->st_counts[3] - pv->st_counts[0] ) /
853         (float) ( pv->st_dates[3] - pv->st_dates[0] );
854     if( hb_get_date() > pv->st_first + 4000 )
855     {
856         int eta;
857         p.rate_avg = 1000.0 * (float) pv->st_counts[3] /
858             (float) ( pv->st_dates[3] - pv->st_first );
859         eta = (float) ( pv->count_frames_max - pv->st_counts[3] ) /
860             p.rate_avg;
861         p.hours   = eta / 3600;
862         p.minutes = ( eta % 3600 ) / 60;
863         p.seconds = eta % 60;
864     }
865     else
866     {
867         p.rate_avg = 0.0;
868         p.hours    = -1;
869         p.minutes  = -1;
870         p.seconds  = -1;
871     }
872 #undef p
873
874     hb_set_state( pv->job->h, &state );
875 }