OSDN Git Service

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