1 /* $Id: sync.c,v 1.38 2005/04/14 21:57:58 titer Exp $
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. */
10 #include "samplerate.h"
13 #undef INT64_MIN /* Because it isn't defined correctly in Zeta */
15 #define INT64_MIN (-9223372036854775807LL-1)
17 #define AC3_SAMPLES_PER_FRAME 1536
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 */
38 struct hb_work_private_s
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
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 */
57 int chap_mark; /* to propagate chapter mark across a drop */
58 hb_buffer_t * cur; /* The next picture to process */
61 hb_sync_audio_t sync_audio[8];
62 int64_t audio_passthru_slip;
63 int64_t video_pts_slip;
66 uint64_t st_counts[4];
71 /***********************************************************************
73 **********************************************************************/
74 static void InitAudio( hb_work_object_t * w, int i );
75 static void SyncVideo( hb_work_object_t * w );
76 static void SyncAudio( hb_work_object_t * w, int i );
77 static void InsertSilence( hb_work_object_t * w, int i, int64_t d );
78 static void UpdateState( hb_work_object_t * w );
80 /***********************************************************************
82 ***********************************************************************
83 * Initialize the work object
84 **********************************************************************/
85 int syncInit( hb_work_object_t * w, hb_job_t * job )
87 hb_title_t * title = job->title;
88 hb_chapter_t * chapter;
91 hb_work_private_t * pv;
93 pv = calloc( 1, sizeof( hb_work_private_t ) );
97 pv->pts_offset = INT64_MIN;
99 /* Calculate how many video frames we are expecting */
100 if (job->pts_to_stop)
102 duration = job->pts_to_stop + 90000;
104 else if( job->frame_to_stop )
106 /* Set the duration to a rough estimate */
107 duration = ( job->frame_to_stop / ( job->vrate / job->vrate_base ) ) * 90000;
112 for( i = job->chapter_start; i <= job->chapter_end; i++ )
114 chapter = hb_list_item( title->list_chapter, i - 1 );
115 duration += chapter->duration;
118 /* 1 second safety so we're sure we won't miss anything */
120 pv->count_frames_max = duration * job->vrate / job->vrate_base / 90000;
122 hb_log( "sync: expecting %d video frames", pv->count_frames_max );
125 /* Initialize libsamplerate for every audio track we have */
126 if ( ! job->indepth_scan )
128 for( i = 0; i < hb_list_count( title->list_audio ) && i < 8; i++ )
130 pv->busy |= ( 1 << (i + 1) );
138 /***********************************************************************
140 ***********************************************************************
142 **********************************************************************/
143 void syncClose( hb_work_object_t * w )
145 hb_work_private_t * pv = w->private_data;
146 hb_job_t * job = pv->job;
147 hb_title_t * title = job->title;
148 hb_audio_t * audio = NULL;
153 hb_buffer_close( &pv->cur );
156 hb_log( "sync: got %d frames, %d expected",
157 pv->count_frames, pv->count_frames_max );
159 if (pv->drops || pv->dups )
161 hb_log( "sync: %d frames dropped, %d duplicated", pv->drops, pv->dups );
164 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
166 audio = hb_list_item( title->list_audio, i );
167 if( audio->config.out.codec == HB_ACODEC_AC3 )
169 free( pv->sync_audio[i].ac3_buf );
173 src_delete( pv->sync_audio[i].state );
178 w->private_data = NULL;
181 /***********************************************************************
183 ***********************************************************************
184 * The root routine of this work abject
186 * The way this works is that we are syncing the audio to the PTS of
187 * the last video that we processed. That's why we skip the audio sync
188 * if we haven't got a valid PTS from the video yet.
190 **********************************************************************/
191 int syncWork( hb_work_object_t * w, hb_buffer_t ** unused1,
192 hb_buffer_t ** unused2 )
194 hb_work_private_t * pv = w->private_data;
200 for( i = 0; i < hb_list_count( pv->job->title->list_audio ); i++ )
202 if ( pv->busy & ( 1 << (i + 1) ) )
206 return ( pv->busy? HB_WORK_OK : HB_WORK_DONE );
209 hb_work_object_t hb_sync =
218 static void InitAudio( hb_work_object_t * w, int i )
220 hb_work_private_t * pv = w->private_data;
221 hb_job_t * job = pv->job;
222 hb_title_t * title = job->title;
223 hb_sync_audio_t * sync;
225 sync = &pv->sync_audio[i];
226 sync->audio = hb_list_item( title->list_audio, i );
228 if( sync->audio->config.out.codec == HB_ACODEC_AC3 )
230 /* Have a silent AC-3 frame ready in case we have to fill a
236 codec = avcodec_find_encoder( CODEC_ID_AC3 );
237 c = avcodec_alloc_context();
239 c->bit_rate = sync->audio->config.in.bitrate;
240 c->sample_rate = sync->audio->config.in.samplerate;
241 c->channels = HB_INPUT_CH_LAYOUT_GET_DISCRETE_COUNT( sync->audio->config.in.channel_layout );
243 if( hb_avcodec_open( c, codec ) < 0 )
245 hb_log( "sync: avcodec_open failed" );
249 zeros = calloc( AC3_SAMPLES_PER_FRAME *
250 sizeof( short ) * c->channels, 1 );
251 sync->ac3_size = sync->audio->config.in.bitrate * AC3_SAMPLES_PER_FRAME /
252 sync->audio->config.in.samplerate / 8;
253 sync->ac3_buf = malloc( sync->ac3_size );
255 if( avcodec_encode_audio( c, sync->ac3_buf, sync->ac3_size,
256 zeros ) != sync->ac3_size )
258 hb_log( "sync: avcodec_encode_audio failed" );
262 hb_avcodec_close( c );
267 /* Initialize libsamplerate */
269 sync->state = src_new( SRC_SINC_MEDIUM_QUALITY, HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(sync->audio->config.out.mixdown), &error );
270 sync->data.end_of_input = 0;
274 /***********************************************************************
276 ***********************************************************************
278 **********************************************************************/
279 static void SyncVideo( hb_work_object_t * w )
281 hb_work_private_t * pv = w->private_data;
282 hb_buffer_t * cur, * next, * sub = NULL;
283 hb_job_t * job = pv->job;
284 hb_subtitle_t *subtitle;
288 if( !pv->cur && !( pv->cur = hb_fifo_get( job->fifo_raw ) ) )
290 /* We haven't even got a frame yet */
297 /* we got an end-of-stream. Feed it downstream & signal that we're done. */
298 hb_fifo_push( job->fifo_sync, hb_buffer_init( 0 ) );
301 * Push through any subtitle EOFs in case they were not synced through.
303 for( i = 0; i < hb_list_count( job->list_subtitle ); i++)
305 subtitle = hb_list_item( job->list_subtitle, i );
306 if( subtitle->config.dest == PASSTHRUSUB )
308 hb_fifo_push( subtitle->fifo_out, hb_buffer_init( 0 ) );
316 /* At this point we have a frame to process. Let's check
317 1) if we will be able to push into the fifo ahead
318 2) if the next frame is there already, since we need it to
319 compute the duration of the current frame*/
320 while( !hb_fifo_is_full( job->fifo_sync ) &&
321 ( next = hb_fifo_see( job->fifo_raw ) ) )
323 hb_buffer_t * buf_tmp;
325 if( next->size == 0 )
327 /* we got an end-of-stream. Feed it downstream & signal that
328 * we're done. Note that this means we drop the final frame of
329 * video (we don't know its duration). On DVDs the final frame
330 * is often strange and dropping it seems to be a good idea. */
331 hb_fifo_push( job->fifo_sync, hb_buffer_init( 0 ) );
334 * Push through any subtitle EOFs in case they were not synced through.
336 for( i = 0; i < hb_list_count( job->list_subtitle ); i++)
338 subtitle = hb_list_item( job->list_subtitle, i );
339 if( subtitle->config.dest == PASSTHRUSUB )
341 hb_fifo_push( subtitle->fifo_out, hb_buffer_init( 0 ) );
347 if( pv->pts_offset == INT64_MIN )
349 /* This is our first frame */
351 if ( cur->start != 0 )
354 * The first pts from a dvd should always be zero but
355 * can be non-zero with a transport or program stream since
356 * we're not guaranteed to start on an IDR frame. If we get
357 * a non-zero initial PTS extend its duration so it behaves
358 * as if it started at zero so that our audio timing will
361 hb_log( "sync: first pts is %lld", cur->start );
367 * since the first frame is always 0 and the upstream reader code
368 * is taking care of adjusting for pts discontinuities, we just have
369 * to deal with the next frame's start being in the past. This can
370 * happen when the PTS is adjusted after data loss but video frame
371 * reordering causes some frames with the old clock to appear after
372 * the clock change. This creates frames that overlap in time which
373 * looks to us like time going backward. The downstream muxing code
374 * can deal with overlaps of up to a frame time but anything larger
375 * we handle by dropping frames here.
377 if ( (int64_t)( next->start - pv->video_pts_slip - cur->start ) <= 0 )
379 if ( pv->first_drop == 0 )
381 pv->first_drop = next->start;
384 if (next->start - cur->start > 0)
386 pts_skip += next->start - cur->start;
387 pv->video_pts_slip -= next->start - cur->start;
389 buf_tmp = hb_fifo_get( job->fifo_raw );
390 if ( buf_tmp->new_chap )
392 // don't drop a chapter mark when we drop the buffer
393 pv->chap_mark = buf_tmp->new_chap;
395 hb_buffer_close( &buf_tmp );
398 if ( pv->first_drop )
400 hb_log( "sync: video time didn't advance - dropped %d frames "
401 "(delta %d ms, current %lld, next %lld, dur %d)",
402 pv->drop_count, (int)( cur->start - pv->first_drop ) / 90,
403 cur->start, next->start, (int)( next->start - cur->start ) );
409 * Track the video sequence number localy so that we can sync the audio
410 * to it using the sequence number as well as the PTS.
412 pv->video_sequence = cur->sequence;
415 * Look for a subtitle for this frame.
417 * If found then it will be tagged onto a video buffer of the correct time and
418 * sent in to the render pipeline. This only needs to be done for VOBSUBs which
419 * get rendered, other types of subtitles can just sit in their raw_queue until
420 * delt with at muxing.
422 for( i = 0; i < hb_list_count( job->list_subtitle ); i++)
424 subtitle = hb_list_item( job->list_subtitle, i );
427 * Rewrite timestamps on subtitles that need it (on raw queue).
429 if( subtitle->source == CC608SUB ||
430 subtitle->source == CC708SUB )
433 * Rewrite timestamps on subtitles that came from Closed Captions
434 * since they are using the MPEG2 timestamps.
436 while( ( sub = hb_fifo_see( subtitle->fifo_raw ) ) )
439 * Rewrite the timestamps as and when the video
440 * (cur->start) reaches the same timestamp as a
441 * closed caption (sub->start).
443 * What about discontinuity boundaries - not delt
446 * Bypass the sync fifo altogether.
450 sub = hb_fifo_get( subtitle->fifo_raw );
451 hb_fifo_push( subtitle->fifo_out, sub );
456 * Sync the subtitles to the incoming video, and use
457 * the matching converted video timestamp.
459 * Note that it doesn't appear that we need to convert
460 * timestamps, I guess that they were already correct,
461 * so just push them through for rendering.
464 if( sub->start < cur->start )
467 duration = sub->stop - sub->start;
468 sub = hb_fifo_get( subtitle->fifo_raw );
469 hb_fifo_push( subtitle->fifo_out, sub );
478 if( subtitle->source == VOBSUB )
481 while( ( sub = hb_fifo_see( subtitle->fifo_raw ) ) )
486 * EOF, pass it through immediately.
491 /* If two subtitles overlap, make the first one stop
492 when the second one starts */
493 sub2 = hb_fifo_see2( subtitle->fifo_raw );
494 if( sub2 && sub->stop > sub2->start )
496 sub->stop = sub2->start;
499 // hb_log("0x%x: video seq: %lld subtitle sequence: %lld",
500 // sub, cur->sequence, sub->sequence);
502 if( sub->sequence > cur->sequence )
505 * The video is behind where we are, so wait until
506 * it catches up to the same reader point on the
507 * DVD. Then our PTS should be in the same region
514 if( sub->stop > cur->start ) {
516 * The stop time is in the future, so fall through
517 * and we'll deal with it in the next block of
522 * There is a valid subtitle, is it time to display it?
524 if( sub->stop > sub->start)
527 * Normal subtitle which ends after it starts,
528 * check to see that the current video is between
531 if( cur->start > sub->start &&
532 cur->start < sub->stop )
535 * We should be playing this, so leave the
538 * fall through to display
540 if( ( sub->stop - sub->start ) < ( 2 * 90000 ) )
543 * Subtitle is on for less than three
544 * seconds, extend the time that it is
545 * displayed to make it easier to read.
546 * Make it 3 seconds or until the next
547 * subtitle is displayed.
549 * This is in response to Indochine which
550 * only displays subs for 1 second -
553 sub->stop = sub->start + ( 2 * 90000 );
555 sub2 = hb_fifo_see2( subtitle->fifo_raw );
557 if( sub2 && sub->stop > sub2->start )
559 sub->stop = sub2->start;
566 * Defer until the play point is within
575 * The end of the subtitle is less than the start,
576 * this is a sign of a PTS discontinuity.
578 if( sub->start > cur->start )
581 * we haven't reached the start time yet, or
582 * we have jumped backwards after having
583 * already started this subtitle.
585 if( cur->start < sub->stop )
588 * We have jumped backwards and so should
589 * continue displaying this subtitle.
591 * fall through to display.
597 * Defer until the play point is
598 * within the subtitle
604 * Play this subtitle as the start is
605 * greater than our video point.
607 * fall through to display/
617 * The subtitle is older than this picture, trash it
619 sub = hb_fifo_get( subtitle->fifo_raw );
620 hb_buffer_close( &sub );
624 /* If we have a subtitle for this picture, copy it */
625 /* FIXME: we should avoid this memcpy */
630 if( subtitle->config.dest == RENDERSUB )
632 if ( cur->sub == NULL )
635 * Tack onto the video buffer for rendering
637 cur->sub = hb_buffer_init( sub->size );
638 cur->sub->x = sub->x;
639 cur->sub->y = sub->y;
640 cur->sub->width = sub->width;
641 cur->sub->height = sub->height;
642 memcpy( cur->sub->data, sub->data, sub->size );
646 * Pass-Through, pop it off of the raw queue,
647 * rewrite times and make it available to be
650 uint64_t sub_duration;
651 sub = hb_fifo_get( subtitle->fifo_raw );
652 sub_duration = sub->stop - sub->start;
653 sub->start = cur->start;
654 buf_tmp = hb_fifo_see( job->fifo_raw );
655 int64_t duration = buf_tmp->start - cur->start;
656 sub->stop = sub->start + duration;
657 hb_fifo_push( subtitle->fifo_sync, sub );
661 * EOF - consume for rendered, else pass through
663 if( subtitle->config.dest == RENDERSUB )
665 sub = hb_fifo_get( subtitle->fifo_raw );
666 hb_buffer_close( &sub );
668 sub = hb_fifo_get( subtitle->fifo_raw );
669 hb_fifo_push( subtitle->fifo_out, sub );
677 * Adjust the pts of the current frame so that it's contiguous
678 * with the previous frame. The start time of the current frame
679 * has to be the end time of the previous frame and the stop
680 * time has to be the start of the next frame. We don't
681 * make any adjustments to the source timestamps other than removing
682 * the clock offsets (which also removes pts discontinuities).
683 * This means we automatically encode at the source's frame rate.
684 * MP2 uses an implicit duration (frames end when the next frame
685 * starts) but more advanced containers like MP4 use an explicit
686 * duration. Since we're looking ahead one frame we set the
687 * explicit stop time from the start time of the next frame.
690 pv->cur = cur = hb_fifo_get( job->fifo_raw );
692 pv->next_pts = cur->start;
693 int64_t duration = cur->start - pts_skip - buf_tmp->start;
697 hb_log( "sync: invalid video duration %lld, start %lld, next %lld",
698 duration, buf_tmp->start, next->start );
701 buf_tmp->start = pv->next_start;
702 pv->next_start += duration;
703 buf_tmp->stop = pv->next_start;
707 // we have a pending chapter mark from a recent drop - put it on this
708 // buffer (this may make it one frame late but we can't do any better).
709 buf_tmp->new_chap = pv->chap_mark;
713 /* Push the frame to the renderer */
714 hb_fifo_push( job->fifo_sync, buf_tmp );
719 if( job->frame_to_stop && pv->count_frames > job->frame_to_stop )
721 // Drop an empty buffer into our output to ensure that things
722 // get flushed all the way out.
723 hb_fifo_push( job->fifo_sync, hb_buffer_init( 0 ) );
725 hb_log( "sync: reached %d frames, exiting early (%i busy)",
726 pv->count_frames, pv->busy );
730 /* Make sure we won't get more frames then expected */
731 if( pv->count_frames >= pv->count_frames_max * 2)
733 hb_log( "sync: got too many frames (%d), exiting early",
736 // Drop an empty buffer into our output to ensure that things
737 // get flushed all the way out.
738 hb_fifo_push( job->fifo_sync, hb_buffer_init( 0 ) );
745 static void OutputAudioFrame( hb_job_t *job, hb_audio_t *audio, hb_buffer_t *buf,
746 hb_sync_audio_t *sync, hb_fifo_t *fifo, int i )
748 int64_t start = sync->next_start;
749 int64_t duration = buf->stop - buf->start;
751 sync->next_pts += duration;
753 if( audio->config.in.samplerate == audio->config.out.samplerate ||
754 audio->config.out.codec == HB_ACODEC_AC3 ||
755 audio->config.out.codec == HB_ACODEC_DCA )
758 * If we don't have to do sample rate conversion or this audio is
759 * pass-thru just send the input buffer downstream after adjusting
760 * its timestamps to make the output stream continuous.
765 /* Not pass-thru - do sample rate conversion */
766 int count_in, count_out;
767 hb_buffer_t * buf_raw = buf;
768 int channel_count = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown) *
771 count_in = buf_raw->size / channel_count;
773 * When using stupid rates like 44.1 there will always be some
774 * truncation error. E.g., a 1536 sample AC3 frame will turn into a
775 * 1536*44.1/48.0 = 1411.2 sample frame. If we just truncate the .2
776 * the error will build up over time and eventually the audio will
777 * substantially lag the video. libsamplerate will keep track of the
778 * fractional sample & give it to us when appropriate if we give it
779 * an extra sample of space in the output buffer.
781 count_out = ( duration * audio->config.out.samplerate ) / 90000 + 1;
783 sync->data.input_frames = count_in;
784 sync->data.output_frames = count_out;
785 sync->data.src_ratio = (double)audio->config.out.samplerate /
786 (double)audio->config.in.samplerate;
788 buf = hb_buffer_init( count_out * channel_count );
789 sync->data.data_in = (float *) buf_raw->data;
790 sync->data.data_out = (float *) buf->data;
791 if( src_process( sync->state, &sync->data ) )
793 /* XXX If this happens, we're screwed */
794 hb_log( "sync: audio %d src_process failed", i );
796 hb_buffer_close( &buf_raw );
798 buf->size = sync->data.output_frames_gen * channel_count;
799 duration = ( sync->data.output_frames_gen * 90000 ) /
800 audio->config.out.samplerate;
802 buf->frametype = HB_FRAME_AUDIO;
804 buf->stop = start + duration;
805 sync->next_start = start + duration;
806 hb_fifo_push( fifo, buf );
809 /***********************************************************************
811 ***********************************************************************
813 **********************************************************************/
814 static void SyncAudio( hb_work_object_t * w, int i )
816 hb_work_private_t * pv = w->private_data;
817 hb_job_t * job = pv->job;
818 hb_sync_audio_t * sync = &pv->sync_audio[i];
819 hb_audio_t * audio = sync->audio;
824 if( audio->config.out.codec == HB_ACODEC_AC3 ||
825 audio->config.out.codec == HB_ACODEC_DCA )
827 fifo = audio->priv.fifo_out;
831 fifo = audio->priv.fifo_sync;
834 while( !hb_fifo_is_full( fifo ) && ( buf = hb_fifo_see( audio->priv.fifo_raw ) ) )
836 start = buf->start - pv->audio_passthru_slip;
837 /* if the next buffer is an eof send it downstream */
838 if ( buf->size <= 0 )
840 buf = hb_fifo_get( audio->priv.fifo_raw );
841 hb_fifo_push( fifo, buf );
842 pv->busy &=~ (1 << (i + 1) );
845 if( job->frame_to_stop && pv->count_frames >= job->frame_to_stop )
847 hb_fifo_push( fifo, hb_buffer_init(0) );
848 pv->busy &=~ (1 << (i + 1) );
851 if ( (int64_t)( start - sync->next_pts ) < 0 )
853 // audio time went backwards.
854 // If our output clock is more than a half frame ahead of the
855 // input clock drop this frame to move closer to sync.
856 // Otherwise drop frames until the input clock matches the output clock.
857 if ( sync->first_drop || sync->next_start - start > 90*15 )
859 // Discard data that's in the past.
860 if ( sync->first_drop == 0 )
862 sync->first_drop = sync->next_pts;
865 buf = hb_fifo_get( audio->priv.fifo_raw );
866 hb_buffer_close( &buf );
869 sync->next_pts = start;
871 if ( sync->first_drop )
873 // we were dropping old data but input buf time is now current
874 hb_log( "sync: audio %d time went backwards %d ms, dropped %d frames "
875 "(next %lld, current %lld)", i,
876 (int)( sync->next_pts - sync->first_drop ) / 90,
877 sync->drop_count, sync->first_drop, sync->next_pts );
878 sync->first_drop = 0;
879 sync->drop_count = 0;
880 sync->next_pts = start;
882 if ( start - sync->next_pts >= (90 * 70) )
884 if ( start - sync->next_pts > (90000LL * 60) )
886 // there's a gap of more than a minute between the last
887 // frame and this. assume we got a corrupted timestamp
888 // and just drop the next buf.
889 hb_log( "sync: %d minute time gap in audio %d - dropping buf"
890 " start %lld, next %lld",
891 (int)((start - sync->next_pts) / (90000*60)),
892 i, start, sync->next_pts );
893 buf = hb_fifo_get( audio->priv.fifo_raw );
894 hb_buffer_close( &buf );
898 * there's a gap of at least 70ms between the last
899 * frame we processed & the next. Fill it with silence.
900 * Or in the case of DCA, skip some frames from the
903 if( sync->audio->config.out.codec == HB_ACODEC_DCA )
905 hb_log( "sync: audio gap %d ms. Skipping frames. Audio %d"
906 " start %lld, next %lld",
907 (int)((start - sync->next_pts) / 90),
908 i, start, sync->next_pts );
909 pv->audio_passthru_slip += (start - sync->next_pts);
910 pv->video_pts_slip += (start - sync->next_pts);
913 hb_log( "sync: adding %d ms of silence to audio %d"
914 " start %lld, next %lld",
915 (int)((start - sync->next_pts) / 90),
916 i, start, sync->next_pts );
917 InsertSilence( w, i, start - sync->next_pts );
922 * When we get here we've taken care of all the dups and gaps in the
923 * audio stream and are ready to inject the next input frame into
926 buf = hb_fifo_get( audio->priv.fifo_raw );
927 OutputAudioFrame( job, audio, buf, sync, fifo, i );
931 static void InsertSilence( hb_work_object_t * w, int i, int64_t duration )
933 hb_work_private_t * pv = w->private_data;
934 hb_job_t *job = pv->job;
935 hb_sync_audio_t *sync = &pv->sync_audio[i];
939 // to keep pass-thru and regular audio in sync we generate silence in
940 // AC3 frame-sized units. If the silence duration isn't an integer multiple
941 // of the AC3 frame duration we will truncate or round up depending on
942 // which minimizes the timing error.
943 const int frame_dur = ( 90000 * AC3_SAMPLES_PER_FRAME ) /
944 sync->audio->config.in.samplerate;
945 int frame_count = ( duration + (frame_dur >> 1) ) / frame_dur;
947 while ( --frame_count >= 0 )
949 if( sync->audio->config.out.codec == HB_ACODEC_AC3 )
951 buf = hb_buffer_init( sync->ac3_size );
952 buf->start = sync->next_pts;
953 buf->stop = buf->start + frame_dur;
954 memcpy( buf->data, sync->ac3_buf, buf->size );
955 fifo = sync->audio->priv.fifo_out;
959 buf = hb_buffer_init( AC3_SAMPLES_PER_FRAME * sizeof( float ) *
960 HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(
961 sync->audio->config.out.mixdown) );
962 buf->start = sync->next_pts;
963 buf->stop = buf->start + frame_dur;
964 memset( buf->data, 0, buf->size );
965 fifo = sync->audio->priv.fifo_sync;
967 OutputAudioFrame( job, sync->audio, buf, sync, fifo, i );
971 static void UpdateState( hb_work_object_t * w )
973 hb_work_private_t * pv = w->private_data;
976 if( !pv->count_frames )
978 pv->st_first = hb_get_date();
982 if( hb_get_date() > pv->st_dates[3] + 1000 )
984 memmove( &pv->st_dates[0], &pv->st_dates[1],
985 3 * sizeof( uint64_t ) );
986 memmove( &pv->st_counts[0], &pv->st_counts[1],
987 3 * sizeof( uint64_t ) );
988 pv->st_dates[3] = hb_get_date();
989 pv->st_counts[3] = pv->count_frames;
992 #define p state.param.working
993 state.state = HB_STATE_WORKING;
994 p.progress = (float) pv->count_frames / (float) pv->count_frames_max;
995 if( p.progress > 1.0 )
999 p.rate_cur = 1000.0 *
1000 (float) ( pv->st_counts[3] - pv->st_counts[0] ) /
1001 (float) ( pv->st_dates[3] - pv->st_dates[0] );
1002 if( hb_get_date() > pv->st_first + 4000 )
1005 p.rate_avg = 1000.0 * (float) pv->st_counts[3] /
1006 (float) ( pv->st_dates[3] - pv->st_first );
1007 eta = (float) ( pv->count_frames_max - pv->st_counts[3] ) /
1009 p.hours = eta / 3600;
1010 p.minutes = ( eta % 3600 ) / 60;
1011 p.seconds = eta % 60;
1022 hb_set_state( pv->job->h, &state );