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;
65 uint64_t st_counts[4];
70 /***********************************************************************
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 );
79 /***********************************************************************
81 ***********************************************************************
82 * Initialize the work object
83 **********************************************************************/
84 int syncInit( hb_work_object_t * w, hb_job_t * job )
86 hb_title_t * title = job->title;
87 hb_chapter_t * chapter;
90 hb_work_private_t * pv;
92 pv = calloc( 1, sizeof( hb_work_private_t ) );
96 pv->pts_offset = INT64_MIN;
98 /* Calculate how many video frames we are expecting */
101 duration = job->pts_to_stop + 90000;
103 else if( job->frame_to_stop )
105 /* Set the duration to a rough estimate */
106 duration = ( job->frame_to_stop / ( job->vrate / job->vrate_base ) ) * 90000;
111 for( i = job->chapter_start; i <= job->chapter_end; i++ )
113 chapter = hb_list_item( title->list_chapter, i - 1 );
114 duration += chapter->duration;
117 /* 1 second safety so we're sure we won't miss anything */
119 pv->count_frames_max = duration * job->vrate / job->vrate_base / 90000;
121 hb_log( "sync: expecting %d video frames", pv->count_frames_max );
124 /* Initialize libsamplerate for every audio track we have */
125 if ( ! job->indepth_scan )
127 for( i = 0; i < hb_list_count( title->list_audio ) && i < 8; i++ )
129 pv->busy |= ( 1 << (i + 1) );
137 /***********************************************************************
139 ***********************************************************************
141 **********************************************************************/
142 void syncClose( hb_work_object_t * w )
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;
152 hb_buffer_close( &pv->cur );
155 hb_log( "sync: got %d frames, %d expected",
156 pv->count_frames, pv->count_frames_max );
158 if (pv->drops || pv->dups )
160 hb_log( "sync: %d frames dropped, %d duplicated", pv->drops, pv->dups );
163 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
165 audio = hb_list_item( title->list_audio, i );
166 if( audio->config.out.codec == HB_ACODEC_AC3 )
168 free( pv->sync_audio[i].ac3_buf );
172 src_delete( pv->sync_audio[i].state );
177 w->private_data = NULL;
180 /***********************************************************************
182 ***********************************************************************
183 * The root routine of this work abject
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.
189 **********************************************************************/
190 int syncWork( hb_work_object_t * w, hb_buffer_t ** unused1,
191 hb_buffer_t ** unused2 )
193 hb_work_private_t * pv = w->private_data;
199 for( i = 0; i < hb_list_count( pv->job->title->list_audio ); i++ )
201 if ( pv->busy & ( 1 << (i + 1) ) )
205 return ( pv->busy? HB_WORK_OK : HB_WORK_DONE );
208 hb_work_object_t hb_sync =
217 static void InitAudio( hb_work_object_t * w, int i )
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;
224 sync = &pv->sync_audio[i];
225 sync->audio = hb_list_item( title->list_audio, i );
227 if( sync->audio->config.out.codec == HB_ACODEC_AC3 )
229 /* Have a silent AC-3 frame ready in case we have to fill a
235 codec = avcodec_find_encoder( CODEC_ID_AC3 );
236 c = avcodec_alloc_context();
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 );
242 if( hb_avcodec_open( c, codec ) < 0 )
244 hb_log( "sync: avcodec_open failed" );
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 );
254 if( avcodec_encode_audio( c, sync->ac3_buf, sync->ac3_size,
255 zeros ) != sync->ac3_size )
257 hb_log( "sync: avcodec_encode_audio failed" );
261 hb_avcodec_close( c );
266 /* Initialize libsamplerate */
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;
273 /***********************************************************************
275 ***********************************************************************
277 **********************************************************************/
278 static void SyncVideo( hb_work_object_t * w )
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;
286 if( !pv->cur && !( pv->cur = hb_fifo_get( job->fifo_raw ) ) )
288 /* We haven't even got a frame yet */
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 ) );
298 * Push through any subtitle EOFs in case they were not synced through.
300 for( i = 0; i < hb_list_count( job->list_subtitle ); i++)
302 subtitle = hb_list_item( job->list_subtitle, i );
303 if( subtitle->dest == PASSTHRUSUB )
305 hb_fifo_push( subtitle->fifo_out, hb_buffer_init( 0 ) );
313 /* At this point we have a frame to process. Let's check
314 1) if we will be able to push into the fifo ahead
315 2) if the next frame is there already, since we need it to
316 compute the duration of the current frame*/
317 while( !hb_fifo_is_full( job->fifo_sync ) &&
318 ( next = hb_fifo_see( job->fifo_raw ) ) )
320 hb_buffer_t * buf_tmp;
322 if( next->size == 0 )
324 /* we got an end-of-stream. Feed it downstream & signal that
325 * we're done. Note that this means we drop the final frame of
326 * video (we don't know its duration). On DVDs the final frame
327 * is often strange and dropping it seems to be a good idea. */
328 hb_fifo_push( job->fifo_sync, hb_buffer_init( 0 ) );
331 * Push through any subtitle EOFs in case they were not synced through.
333 for( i = 0; i < hb_list_count( job->list_subtitle ); i++)
335 subtitle = hb_list_item( job->list_subtitle, i );
336 if( subtitle->dest == PASSTHRUSUB )
338 hb_fifo_push( subtitle->fifo_out, hb_buffer_init( 0 ) );
344 if( pv->pts_offset == INT64_MIN )
346 /* This is our first frame */
348 if ( cur->start != 0 )
351 * The first pts from a dvd should always be zero but
352 * can be non-zero with a transport or program stream since
353 * we're not guaranteed to start on an IDR frame. If we get
354 * a non-zero initial PTS extend its duration so it behaves
355 * as if it started at zero so that our audio timing will
358 hb_log( "sync: first pts is %lld", cur->start );
363 if( cur->new_chap ) {
364 hb_log("sync got new chapter %d", cur->new_chap );
368 * since the first frame is always 0 and the upstream reader code
369 * is taking care of adjusting for pts discontinuities, we just have
370 * to deal with the next frame's start being in the past. This can
371 * happen when the PTS is adjusted after data loss but video frame
372 * reordering causes some frames with the old clock to appear after
373 * the clock change. This creates frames that overlap in time which
374 * looks to us like time going backward. The downstream muxing code
375 * can deal with overlaps of up to a frame time but anything larger
376 * we handle by dropping frames here.
378 if ( (int64_t)( next->start - cur->start ) <= 0 ||
379 (int64_t)( (cur->start - pv->audio_passthru_slip ) - pv->next_pts ) < 0 )
381 if ( pv->first_drop == 0 )
383 pv->first_drop = next->start;
386 buf_tmp = hb_fifo_get( job->fifo_raw );
387 if ( buf_tmp->new_chap )
389 // don't drop a chapter mark when we drop the buffer
390 pv->chap_mark = buf_tmp->new_chap;
392 hb_buffer_close( &buf_tmp );
395 if ( pv->first_drop )
397 hb_log( "sync: video time didn't advance - dropped %d frames "
398 "(delta %d ms, current %lld, next %lld, dur %d)",
399 pv->drop_count, (int)( cur->start - pv->first_drop ) / 90,
400 cur->start, next->start, (int)( next->start - cur->start ) );
406 * Track the video sequence number localy so that we can sync the audio
407 * to it using the sequence number as well as the PTS.
409 pv->video_sequence = cur->sequence;
412 * Look for a subtitle for this frame.
414 * If found then it will be tagged onto a video buffer of the correct time and
415 * sent in to the render pipeline. This only needs to be done for VOBSUBs which
416 * get rendered, other types of subtitles can just sit in their raw_queue until
417 * delt with at muxing.
419 for( i = 0; i < hb_list_count( job->list_subtitle ); i++)
421 subtitle = hb_list_item( job->list_subtitle, i );
424 * Rewrite timestamps on subtitles that need it (on raw queue).
426 if( subtitle->source == CC608SUB ||
427 subtitle->source == CC708SUB )
430 * Rewrite timestamps on subtitles that came from Closed Captions
431 * since they are using the MPEG2 timestamps.
433 while( ( sub = hb_fifo_see( subtitle->fifo_raw ) ) )
436 * Rewrite the timestamps as and when the video
437 * (cur->start) reaches the same timestamp as a
438 * closed caption (sub->start).
440 * What about discontinuity boundaries - not delt
443 * Bypass the sync fifo altogether.
447 sub = hb_fifo_get( subtitle->fifo_raw );
448 hb_fifo_push( subtitle->fifo_out, sub );
453 * Sync the subtitles to the incoming video, and use
454 * the matching converted video timestamp.
456 * Note that it doesn't appear that we need to convert
457 * timestamps, I guess that they were already correct,
458 * so just push them through for rendering.
461 if( sub->start < cur->start )
464 duration = sub->stop - sub->start;
465 sub = hb_fifo_get( subtitle->fifo_raw );
466 hb_fifo_push( subtitle->fifo_out, sub );
475 if( subtitle->source == VOBSUB )
478 while( ( sub = hb_fifo_see( subtitle->fifo_raw ) ) )
483 * EOF, pass it through immediately.
488 /* If two subtitles overlap, make the first one stop
489 when the second one starts */
490 sub2 = hb_fifo_see2( subtitle->fifo_raw );
491 if( sub2 && sub->stop > sub2->start )
492 sub->stop = sub2->start;
494 // hb_log("0x%x: video seq: %lld subtitle sequence: %lld",
495 // sub, cur->sequence, sub->sequence);
497 if( sub->sequence > cur->sequence )
500 * The video is behind where we are, so wait until
501 * it catches up to the same reader point on the
502 * DVD. Then our PTS should be in the same region
509 if( sub->stop > cur->start ) {
511 * The stop time is in the future, so fall through
512 * and we'll deal with it in the next block of
519 * The subtitle is older than this picture, trash it
521 sub = hb_fifo_get( subtitle->fifo_raw );
522 hb_buffer_close( &sub );
525 if( sub && sub->size == 0 )
528 * Continue immediately on subtitle EOF
534 * There is a valid subtitle, is it time to display it?
538 if( sub->stop > sub->start)
541 * Normal subtitle which ends after it starts, check to
542 * see that the current video is between the start and end.
544 if( cur->start > sub->start &&
545 cur->start < sub->stop )
548 * We should be playing this, so leave the
551 * fall through to display
553 if( ( sub->stop - sub->start ) < ( 3 * 90000 ) )
556 * Subtitle is on for less than three seconds, extend
557 * the time that it is displayed to make it easier
558 * to read. Make it 3 seconds or until the next
559 * subtitle is displayed.
561 * This is in response to Indochine which only
562 * displays subs for 1 second - too fast to read.
564 sub->stop = sub->start + ( 3 * 90000 );
566 sub2 = hb_fifo_see2( subtitle->fifo_raw );
568 if( sub2 && sub->stop > sub2->start )
570 sub->stop = sub2->start;
577 * Defer until the play point is within the subtitle
585 * The end of the subtitle is less than the start, this is a
586 * sign of a PTS discontinuity.
588 if( sub->start > cur->start )
591 * we haven't reached the start time yet, or
592 * we have jumped backwards after having
593 * already started this subtitle.
595 if( cur->start < sub->stop )
598 * We have jumped backwards and so should
599 * continue displaying this subtitle.
601 * fall through to display.
607 * Defer until the play point is within the subtitle
613 * Play this subtitle as the start is greater than our
616 * fall through to display/
625 * Got a sub to display...
632 * Adjust the pts of the current frame so that it's contiguous
633 * with the previous frame. The start time of the current frame
634 * has to be the end time of the previous frame and the stop
635 * time has to be the start of the next frame. We don't
636 * make any adjustments to the source timestamps other than removing
637 * the clock offsets (which also removes pts discontinuities).
638 * This means we automatically encode at the source's frame rate.
639 * MP2 uses an implicit duration (frames end when the next frame
640 * starts) but more advanced containers like MP4 use an explicit
641 * duration. Since we're looking ahead one frame we set the
642 * explicit stop time from the start time of the next frame.
645 pv->cur = cur = hb_fifo_get( job->fifo_raw );
646 pv->next_pts = cur->start;
647 int64_t duration = cur->start - buf_tmp->start;
650 hb_log( "sync: invalid video duration %lld, start %lld, next %lld",
651 duration, buf_tmp->start, next->start );
654 buf_tmp->start = pv->next_start;
655 pv->next_start += duration;
656 buf_tmp->stop = pv->next_start;
660 // we have a pending chapter mark from a recent drop - put it on this
661 // buffer (this may make it one frame late but we can't do any better).
662 buf_tmp->new_chap = pv->chap_mark;
666 /* If we have a subtitle for this picture, copy it */
667 /* FIXME: we should avoid this memcpy */
668 if( sub && subtitle &&
669 subtitle->format == PICTURESUB )
673 if( subtitle->dest == RENDERSUB )
676 * Tack onto the video buffer for rendering
678 buf_tmp->sub = hb_buffer_init( sub->size );
679 buf_tmp->sub->x = sub->x;
680 buf_tmp->sub->y = sub->y;
681 buf_tmp->sub->width = sub->width;
682 buf_tmp->sub->height = sub->height;
683 memcpy( buf_tmp->sub->data, sub->data, sub->size );
686 * Pass-Through, pop it off of the raw queue, rewrite times and
687 * make it available to be reencoded.
689 uint64_t sub_duration;
690 sub = hb_fifo_get( subtitle->fifo_raw );
691 sub_duration = sub->stop - sub->start;
692 sub->start = buf_tmp->start;
693 sub->stop = sub->start + duration;
694 hb_fifo_push( subtitle->fifo_sync, sub );
698 * EOF - consume for rendered, else pass through
700 if( subtitle->dest == RENDERSUB )
702 sub = hb_fifo_get( subtitle->fifo_raw );
703 hb_buffer_close( &sub );
705 sub = hb_fifo_get( subtitle->fifo_raw );
706 hb_fifo_push( subtitle->fifo_out, sub );
711 /* Push the frame to the renderer */
712 hb_fifo_push( job->fifo_sync, buf_tmp );
717 if( job->frame_to_stop && pv->count_frames > job->frame_to_stop )
719 // Drop an empty buffer into our output to ensure that things
720 // get flushed all the way out.
721 hb_fifo_push( job->fifo_sync, hb_buffer_init( 0 ) );
723 hb_log( "sync: reached %d frames, exiting early (%i busy)",
724 pv->count_frames, pv->busy );
728 /* Make sure we won't get more frames then expected */
729 if( pv->count_frames >= pv->count_frames_max * 2)
731 hb_log( "sync: got too many frames (%d), exiting early",
734 // Drop an empty buffer into our output to ensure that things
735 // get flushed all the way out.
736 hb_fifo_push( job->fifo_sync, hb_buffer_init( 0 ) );
743 static void OutputAudioFrame( hb_job_t *job, hb_audio_t *audio, hb_buffer_t *buf,
744 hb_sync_audio_t *sync, hb_fifo_t *fifo, int i )
746 int64_t start = sync->next_start;
747 int64_t duration = buf->stop - buf->start;
749 sync->next_pts += duration;
751 if( audio->config.in.samplerate == audio->config.out.samplerate ||
752 audio->config.out.codec == HB_ACODEC_AC3 ||
753 audio->config.out.codec == HB_ACODEC_DCA )
756 * If we don't have to do sample rate conversion or this audio is
757 * pass-thru just send the input buffer downstream after adjusting
758 * its timestamps to make the output stream continuous.
763 /* Not pass-thru - do sample rate conversion */
764 int count_in, count_out;
765 hb_buffer_t * buf_raw = buf;
766 int channel_count = HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(audio->config.out.mixdown) *
769 count_in = buf_raw->size / channel_count;
771 * When using stupid rates like 44.1 there will always be some
772 * truncation error. E.g., a 1536 sample AC3 frame will turn into a
773 * 1536*44.1/48.0 = 1411.2 sample frame. If we just truncate the .2
774 * the error will build up over time and eventually the audio will
775 * substantially lag the video. libsamplerate will keep track of the
776 * fractional sample & give it to us when appropriate if we give it
777 * an extra sample of space in the output buffer.
779 count_out = ( duration * audio->config.out.samplerate ) / 90000 + 1;
781 sync->data.input_frames = count_in;
782 sync->data.output_frames = count_out;
783 sync->data.src_ratio = (double)audio->config.out.samplerate /
784 (double)audio->config.in.samplerate;
786 buf = hb_buffer_init( count_out * channel_count );
787 sync->data.data_in = (float *) buf_raw->data;
788 sync->data.data_out = (float *) buf->data;
789 if( src_process( sync->state, &sync->data ) )
791 /* XXX If this happens, we're screwed */
792 hb_log( "sync: audio %d src_process failed", i );
794 hb_buffer_close( &buf_raw );
796 buf->size = sync->data.output_frames_gen * channel_count;
797 duration = ( sync->data.output_frames_gen * 90000 ) /
798 audio->config.out.samplerate;
800 buf->frametype = HB_FRAME_AUDIO;
802 buf->stop = start + duration;
803 sync->next_start = start + duration;
804 hb_fifo_push( fifo, buf );
807 /***********************************************************************
809 ***********************************************************************
811 **********************************************************************/
812 static void SyncAudio( hb_work_object_t * w, int i )
814 hb_work_private_t * pv = w->private_data;
815 hb_job_t * job = pv->job;
816 hb_sync_audio_t * sync = &pv->sync_audio[i];
817 hb_audio_t * audio = sync->audio;
822 if( audio->config.out.codec == HB_ACODEC_AC3 ||
823 audio->config.out.codec == HB_ACODEC_DCA )
825 fifo = audio->priv.fifo_out;
829 fifo = audio->priv.fifo_sync;
832 while( !hb_fifo_is_full( fifo ) && ( buf = hb_fifo_see( audio->priv.fifo_raw ) ) )
834 start = buf->start - pv->audio_passthru_slip;
835 /* if the next buffer is an eof send it downstream */
836 if ( buf->size <= 0 )
838 buf = hb_fifo_get( audio->priv.fifo_raw );
839 hb_fifo_push( fifo, buf );
840 pv->busy &=~ (1 << (i + 1) );
843 if( job->frame_to_stop && pv->count_frames >= job->frame_to_stop )
845 hb_fifo_push( fifo, hb_buffer_init(0) );
846 pv->busy &=~ (1 << (i + 1) );
849 if ( (int64_t)( start - sync->next_pts ) < 0 )
851 // audio time went backwards.
852 // If our output clock is more than a half frame ahead of the
853 // input clock drop this frame to move closer to sync.
854 // Otherwise drop frames until the input clock matches the output clock.
855 if ( sync->first_drop || sync->next_start - start > 90*15 )
857 // Discard data that's in the past.
858 if ( sync->first_drop == 0 )
860 sync->first_drop = sync->next_pts;
863 buf = hb_fifo_get( audio->priv.fifo_raw );
864 hb_buffer_close( &buf );
867 sync->next_pts = start;
869 if ( sync->first_drop )
871 // we were dropping old data but input buf time is now current
872 hb_log( "sync: audio %d time went backwards %d ms, dropped %d frames "
873 "(next %lld, current %lld)", i,
874 (int)( sync->next_pts - sync->first_drop ) / 90,
875 sync->drop_count, sync->first_drop, sync->next_pts );
876 sync->first_drop = 0;
877 sync->drop_count = 0;
878 sync->next_pts = start;
880 if ( start - sync->next_pts >= (90 * 70) )
882 if ( start - sync->next_pts > (90000LL * 60) )
884 // there's a gap of more than a minute between the last
885 // frame and this. assume we got a corrupted timestamp
886 // and just drop the next buf.
887 hb_log( "sync: %d minute time gap in audio %d - dropping buf"
888 " start %lld, next %lld",
889 (int)((start - sync->next_pts) / (90000*60)),
890 i, start, sync->next_pts );
891 buf = hb_fifo_get( audio->priv.fifo_raw );
892 hb_buffer_close( &buf );
896 * there's a gap of at least 70ms between the last
897 * frame we processed & the next. Fill it with silence.
898 * Or in the case of DCA, skip some frames from the
901 if( sync->audio->config.out.codec == HB_ACODEC_DCA )
903 hb_log( "sync: audio gap %d ms. Skipping frames. Audio %d"
904 " start %lld, next %lld",
905 (int)((start - sync->next_pts) / 90),
906 i, start, sync->next_pts );
907 pv->audio_passthru_slip += (start - sync->next_pts);
910 hb_log( "sync: adding %d ms of silence to audio %d"
911 " start %lld, next %lld",
912 (int)((start - sync->next_pts) / 90),
913 i, start, sync->next_pts );
914 InsertSilence( w, i, start - sync->next_pts );
919 * When we get here we've taken care of all the dups and gaps in the
920 * audio stream and are ready to inject the next input frame into
923 buf = hb_fifo_get( audio->priv.fifo_raw );
924 OutputAudioFrame( job, audio, buf, sync, fifo, i );
928 static void InsertSilence( hb_work_object_t * w, int i, int64_t duration )
930 hb_work_private_t * pv = w->private_data;
931 hb_job_t *job = pv->job;
932 hb_sync_audio_t *sync = &pv->sync_audio[i];
936 // to keep pass-thru and regular audio in sync we generate silence in
937 // AC3 frame-sized units. If the silence duration isn't an integer multiple
938 // of the AC3 frame duration we will truncate or round up depending on
939 // which minimizes the timing error.
940 const int frame_dur = ( 90000 * AC3_SAMPLES_PER_FRAME ) /
941 sync->audio->config.in.samplerate;
942 int frame_count = ( duration + (frame_dur >> 1) ) / frame_dur;
944 while ( --frame_count >= 0 )
946 if( sync->audio->config.out.codec == HB_ACODEC_AC3 )
948 buf = hb_buffer_init( sync->ac3_size );
949 buf->start = sync->next_pts;
950 buf->stop = buf->start + frame_dur;
951 memcpy( buf->data, sync->ac3_buf, buf->size );
952 fifo = sync->audio->priv.fifo_out;
956 buf = hb_buffer_init( AC3_SAMPLES_PER_FRAME * sizeof( float ) *
957 HB_AMIXDOWN_GET_DISCRETE_CHANNEL_COUNT(
958 sync->audio->config.out.mixdown) );
959 buf->start = sync->next_pts;
960 buf->stop = buf->start + frame_dur;
961 memset( buf->data, 0, buf->size );
962 fifo = sync->audio->priv.fifo_sync;
964 OutputAudioFrame( job, sync->audio, buf, sync, fifo, i );
968 static void UpdateState( hb_work_object_t * w )
970 hb_work_private_t * pv = w->private_data;
973 if( !pv->count_frames )
975 pv->st_first = hb_get_date();
979 if( hb_get_date() > pv->st_dates[3] + 1000 )
981 memmove( &pv->st_dates[0], &pv->st_dates[1],
982 3 * sizeof( uint64_t ) );
983 memmove( &pv->st_counts[0], &pv->st_counts[1],
984 3 * sizeof( uint64_t ) );
985 pv->st_dates[3] = hb_get_date();
986 pv->st_counts[3] = pv->count_frames;
989 #define p state.param.working
990 state.state = HB_STATE_WORKING;
991 p.progress = (float) pv->count_frames / (float) pv->count_frames_max;
992 if( p.progress > 1.0 )
996 p.rate_cur = 1000.0 *
997 (float) ( pv->st_counts[3] - pv->st_counts[0] ) /
998 (float) ( pv->st_dates[3] - pv->st_dates[0] );
999 if( hb_get_date() > pv->st_first + 4000 )
1002 p.rate_avg = 1000.0 * (float) pv->st_counts[3] /
1003 (float) ( pv->st_dates[3] - pv->st_first );
1004 eta = (float) ( pv->count_frames_max - pv->st_counts[3] ) /
1006 p.hours = eta / 3600;
1007 p.minutes = ( eta % 3600 ) / 60;
1008 p.seconds = eta % 60;
1019 hb_set_state( pv->job->h, &state );