OSDN Git Service

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