OSDN Git Service

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