1 /* $Id: reader.c,v 1.21 2005/11/25 15:05:25 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. */
11 double average; // average time between packets
12 int64_t last; // last timestamp seen on this stream
14 int is_audio; // != 0 if this is an audio stream
15 int valid; // Stream timing is not valid until next scr.
28 stream_timing_t *stream_timing;
33 uint8_t st_slots; // size (in slots) of stream_timing array
34 uint8_t saw_video; // != 0 if we've seen video
35 uint8_t saw_audio; // != 0 if we've seen audio
37 int start_found; // found pts_to_start point
41 /***********************************************************************
43 **********************************************************************/
44 static void ReaderFunc( void * );
45 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id );
46 static void UpdateState( hb_reader_t * r, int64_t start);
48 /***********************************************************************
50 ***********************************************************************
52 **********************************************************************/
53 hb_thread_t * hb_reader_init( hb_job_t * job )
57 r = calloc( sizeof( hb_reader_t ), 1 );
60 r->title = job->title;
65 r->stream_timing = calloc( sizeof(stream_timing_t), r->st_slots );
66 r->stream_timing[0].id = r->title->video_id;
67 r->stream_timing[0].average = 90000. * (double)job->vrate_base /
69 r->stream_timing[0].last = -r->stream_timing[0].average;
70 r->stream_timing[0].valid = 1;
71 r->stream_timing[1].id = -1;
73 if ( !job->pts_to_start )
76 return hb_thread_init( "reader", ReaderFunc, r,
80 static void push_buf( const hb_reader_t *r, hb_fifo_t *fifo, hb_buffer_t *buf )
82 while ( !*r->die && !r->job->done )
84 if ( hb_fifo_full_wait( fifo ) )
86 hb_fifo_push( fifo, buf );
92 static int is_audio( hb_reader_t *r, int id )
97 for( i = 0; ( audio = hb_list_item( r->title->list_audio, i ) ); ++i )
99 if ( audio->id == id )
107 // The MPEG STD (Standard Target Decoder) essentially requires that we keep
108 // per-stream timing so that when there's a timing discontinuity we can
109 // seemlessly join packets on either side of the discontinuity. This join
110 // requires that we know the timestamp of the previous packet and the
111 // average inter-packet time (since we position the new packet at the end
112 // of the previous packet). The next four routines keep track of this
113 // per-stream timing.
115 // find the per-stream timing state for 'buf'
117 static stream_timing_t *find_st( hb_reader_t *r, const hb_buffer_t *buf )
119 stream_timing_t *st = r->stream_timing;
120 for ( ; st->id != -1; ++st )
122 if ( st->id == buf->id )
128 // find or create the per-stream timing state for 'buf'
130 static stream_timing_t *id_to_st( hb_reader_t *r, const hb_buffer_t *buf, int valid )
132 stream_timing_t *st = r->stream_timing;
133 while ( st->id != buf->id && st->id != -1)
137 // if we haven't seen this stream add it.
140 // we keep the steam timing info in an array with some power-of-two
141 // number of slots. If we don't have two slots left (one for our new
142 // entry plus one for the "-1" eol) we need to expand the array.
143 int slot = st - r->stream_timing;
144 if ( slot + 1 >= r->st_slots )
147 r->stream_timing = realloc( r->stream_timing, r->st_slots *
148 sizeof(*r->stream_timing) );
149 st = r->stream_timing + slot;
152 st->average = 30.*90.;
153 st->last = -st->average;
154 if ( ( st->is_audio = is_audio( r, buf->id ) ) != 0 )
164 // update the average inter-packet time of the stream associated with 'buf'
165 // using a recursive low-pass filter with a 16 packet time constant.
167 static void update_ipt( hb_reader_t *r, const hb_buffer_t *buf )
169 stream_timing_t *st = id_to_st( r, buf, 1 );
170 double dt = buf->renderOffset - st->last;
171 // Protect against spurious bad timestamps
172 if ( dt > -5 * 90000LL && dt < 5 * 90000LL )
174 st->average += ( dt - st->average ) * (1./32.);
175 st->last = buf->renderOffset;
180 // use the per-stream state associated with 'buf' to compute a new scr_offset
181 // such that 'buf' will follow the previous packet of this stream separated
182 // by the average packet time of the stream.
184 static void new_scr_offset( hb_reader_t *r, hb_buffer_t *buf )
186 stream_timing_t *st = id_to_st( r, buf, 1 );
190 // !valid means we've not received any previous data
191 // for this stream. There is no 'last' packet time.
192 // So approximate it with video's last time.
193 last = r->stream_timing[0].last;
200 int64_t nxt = last + st->average;
201 r->scr_offset = buf->renderOffset - nxt;
202 // This log is handy when you need to debug timing problems...
203 //hb_log("id %x last %ld avg %g nxt %ld renderOffset %ld scr_offset %ld",
204 // buf->id, last, st->average, nxt, buf->renderOffset, r->scr_offset);
205 r->scr_changes = r->demux.scr_changes;
209 /***********************************************************************
211 ***********************************************************************
213 **********************************************************************/
214 static void ReaderFunc( void * _r )
216 hb_reader_t * r = _r;
222 int chapter_end = r->job->chapter_end;
224 if ( r->title->type == HB_BD_TYPE )
226 if ( !( r->bd = hb_bd_init( r->title->path ) ) )
229 else if ( r->title->type == HB_DVD_TYPE )
231 if ( !( r->dvd = hb_dvd_init( r->title->path ) ) )
234 else if ( r->title->type == HB_STREAM_TYPE )
236 if ( !( r->stream = hb_stream_open( r->title->path, r->title ) ) )
241 // Unknown type, should never happen
245 hb_buffer_t *ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
248 if( !hb_bd_start( r->bd, r->title ) )
250 hb_bd_close( &r->bd );
251 hb_buffer_close( &ps );
254 if ( r->job->start_at_preview )
256 // XXX code from DecodePreviews - should go into its own routine
257 hb_bd_seek( r->bd, (float)r->job->start_at_preview /
258 ( r->job->seek_points ? ( r->job->seek_points + 1.0 ) : 11.0 ) );
260 else if ( r->job->pts_to_start )
262 hb_bd_seek_pts( r->bd, r->job->pts_to_start );
263 r->job->pts_to_start = 0;
268 hb_bd_seek_chapter( r->bd, r->job->chapter_start );
270 if (r->job->angle > 1)
272 hb_bd_set_angle( r->bd, r->job->angle - 1 );
278 * XXX this code is a temporary hack that should go away if/when
279 * chapter merging goes away in libhb/dvd.c
280 * map the start and end chapter numbers to on-media chapter
281 * numbers since chapter merging could cause the handbrake numbers
282 * to diverge from the media numbers and, if our chapter_end is after
283 * a media chapter that got merged, we'll stop ripping too early.
285 int start = r->job->chapter_start;
286 hb_chapter_t *chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
288 chapter_end = chap->index;
291 chap = hb_list_item( r->title->list_chapter, start - 1 );
294 /* end chapter mapping XXX */
296 if( !hb_dvd_start( r->dvd, r->title, start ) )
298 hb_dvd_close( &r->dvd );
299 hb_buffer_close( &ps );
304 hb_dvd_set_angle( r->dvd, r->job->angle );
307 if ( r->job->start_at_preview )
309 // XXX code from DecodePreviews - should go into its own routine
310 hb_dvd_seek( r->dvd, (float)r->job->start_at_preview /
311 ( r->job->seek_points ? ( r->job->seek_points + 1.0 ) : 11.0 ) );
314 else if ( r->stream && r->job->start_at_preview )
317 // XXX code from DecodePreviews - should go into its own routine
318 hb_stream_seek( r->stream, (float)( r->job->start_at_preview - 1 ) /
319 ( r->job->seek_points ? ( r->job->seek_points + 1.0 ) : 11.0 ) );
322 else if ( r->stream && r->job->pts_to_start )
324 int64_t pts_to_start = r->job->pts_to_start;
326 // Find out what the first timestamp of the stream is
327 // and then seek to the appropriate offset from it
328 if ( hb_stream_read( r->stream, ps ) )
331 pts_to_start += ps->start;
334 if ( hb_stream_seek_ts( r->stream, pts_to_start ) >= 0 )
336 // Seek takes us to the nearest I-frame before the timestamp
337 // that we want. So we will retrieve the start time of the
338 // first packet we get, subtract that from pts_to_start, and
339 // inspect the reset of the frames in sync.
341 r->job->pts_to_start = pts_to_start;
347 * Standard stream, seek to the starting chapter, if set, and track the
348 * end chapter so that we end at the right time.
350 int start = r->job->chapter_start;
351 hb_chapter_t *chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
353 chapter_end = chap->index;
356 chap = hb_list_item( r->title->list_chapter, start - 1 );
361 * Seek to the start chapter.
363 hb_stream_seek_chapter( r->stream, start );
366 list = hb_list_init();
368 while( !*r->die && !r->job->done )
371 chapter = hb_bd_chapter( r->bd );
373 chapter = hb_dvd_chapter( r->dvd );
375 chapter = hb_stream_chapter( r->stream );
379 hb_log( "reader: end of the title reached" );
382 if( chapter > chapter_end )
384 hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
385 r->job->chapter_end, chapter_end, chapter );
391 if( !hb_bd_read( r->bd, ps ) )
398 if( !hb_dvd_read( r->dvd, ps ) )
405 if ( !hb_stream_read( r->stream, ps ) )
409 if ( r->start_found == 2 )
411 // We will inspect the timestamps of each frame in sync
412 // to skip from this seek point to the timestamp we
414 if ( ps->start > 0 && ps->start < r->job->pts_to_start )
416 r->job->pts_to_start -= ps->start;
418 else if ( ps->start >= r->job->pts_to_start )
420 r->job->pts_to_start = 0;
426 if( r->job->indepth_scan )
429 * Need to update the progress during a subtitle scan
433 #define p state.param.working
435 state.state = HB_STATE_WORKING;
436 p.progress = (double)chapter / (double)r->job->chapter_end;
437 if( p.progress > 1.0 )
445 hb_set_state( r->job->h, &state );
448 (hb_demux[r->title->demuxer])( ps, list, &r->demux );
450 while( ( buf = hb_list_item( list, 0 ) ) )
452 hb_list_rem( list, buf );
453 fifos = GetFifoForId( r->job, buf->id );
455 if ( fifos && ! r->saw_video && !r->job->indepth_scan )
457 // The first data packet with a PTS from an audio or video stream
458 // that we're decoding defines 'time zero'. Discard packets until
460 if ( buf->start != -1 && buf->renderOffset != -1 &&
461 ( buf->id == r->title->video_id || is_audio( r, buf->id ) ) )
463 // force a new scr offset computation
464 r->scr_changes = r->demux.scr_changes - 1;
465 // create a stream state if we don't have one so the
466 // offset will get computed correctly.
467 id_to_st( r, buf, 1 );
469 hb_log( "reader: first SCR %"PRId64" id %d DTS %"PRId64,
470 r->demux.last_scr, buf->id, buf->renderOffset );
479 if ( buf->renderOffset != -1 )
481 if ( r->scr_changes != r->demux.scr_changes )
483 // This is the first audio or video packet after an SCR
484 // change. Compute a new scr offset that would make this
485 // packet follow the last of this stream with the
486 // correct average spacing.
487 stream_timing_t *st = id_to_st( r, buf, 0 );
489 // if this is the video stream and we don't have
490 // audio yet or this is an audio stream
491 // generate a new scr
493 ( st == r->stream_timing && !r->saw_audio ) )
495 new_scr_offset( r, buf );
499 // defer the scr change until we get some
500 // audio since audio has a timestamp per
501 // frame but video & subtitles don't. Clear
502 // the timestamps so the decoder will generate
503 // them from the frame durations.
505 buf->renderOffset = -1;
509 if ( buf->start != -1 )
511 int64_t start = buf->start - r->scr_offset;
512 if ( !r->start_found )
513 UpdateState( r, start );
515 if ( !r->start_found &&
516 start >= r->job->pts_to_start )
518 // pts_to_start point found
521 // This log is handy when you need to debug timing problems
522 //hb_log("id %x scr_offset %ld start %ld --> %ld",
523 // buf->id, r->scr_offset, buf->start,
524 // buf->start - r->scr_offset);
525 buf->start -= r->scr_offset;
527 if ( buf->renderOffset != -1 )
529 if ( r->scr_changes == r->demux.scr_changes )
531 // This packet is referenced to the same SCR as the last.
532 // Adjust timestamp to remove the System Clock Reference
533 // offset then update the average inter-packet time
535 buf->renderOffset -= r->scr_offset;
536 update_ipt( r, buf );
539 if ( !r->start_found )
541 hb_buffer_close( &buf );
545 buf->sequence = r->sequence++;
546 /* if there are mutiple output fifos, send a copy of the
547 * buffer down all but the first (we have to not ship the
548 * original buffer or we'll race with the thread that's
549 * consuming the buffer & inject garbage into the data stream). */
550 for( n = 1; fifos[n] != NULL; n++)
552 hb_buffer_t *buf_copy = hb_buffer_init( buf->size );
553 hb_buffer_copy_settings( buf_copy, buf );
554 memcpy( buf_copy->data, buf->data, buf->size );
555 push_buf( r, fifos[n], buf_copy );
557 push_buf( r, fifos[0], buf );
561 hb_buffer_close( &buf );
566 // send empty buffers downstream to video & audio decoders to signal we're done.
567 if( !*r->die && !r->job->done )
569 push_buf( r, r->job->fifo_mpeg2, hb_buffer_init(0) );
572 for( n = 0; (audio = hb_list_item( r->job->title->list_audio, n)); ++n )
574 if ( audio->priv.fifo_in )
575 push_buf( r, audio->priv.fifo_in, hb_buffer_init(0) );
578 hb_subtitle_t *subtitle;
579 for( n = 0; (subtitle = hb_list_item( r->job->title->list_subtitle, n)); ++n )
581 if ( subtitle->fifo_in && subtitle->source == VOBSUB)
582 push_buf( r, subtitle->fifo_in, hb_buffer_init(0) );
586 hb_list_empty( &list );
587 hb_buffer_close( &ps );
591 hb_bd_close( &r->bd );
595 hb_dvd_stop( r->dvd );
596 hb_dvd_close( &r->dvd );
600 hb_stream_close(&r->stream);
603 if ( r->stream_timing )
605 free( r->stream_timing );
608 hb_log( "reader: done. %d scr changes", r->demux.scr_changes );
609 if ( r->demux.dts_drops )
611 hb_log( "reader: %d drops because DTS out of range", r->demux.dts_drops );
618 static void UpdateState( hb_reader_t * r, int64_t start)
630 #define p state.param.working
631 state.state = HB_STATE_SEARCHING;
632 p.progress = (float) start / (float) r->job->pts_to_start;
633 if( p.progress > 1.0 )
637 if (now > r->st_first)
641 avg = 1000.0 * (double)start / (now - r->st_first);
642 eta = ( r->job->pts_to_start - start ) / avg;
643 p.hours = eta / 3600;
644 p.minutes = ( eta % 3600 ) / 60;
645 p.seconds = eta % 60;
656 hb_set_state( r->job->h, &state );
658 /***********************************************************************
660 ***********************************************************************
662 **********************************************************************/
663 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
665 hb_title_t * title = job->title;
667 hb_subtitle_t * subtitle;
669 static hb_fifo_t * fifos[100];
671 memset(fifos, 0, sizeof(fifos));
673 if( id == title->video_id )
675 if( job->indepth_scan )
678 * Ditch the video here during the indepth scan until
679 * we can improve the MPEG2 decode performance.
685 fifos[0] = job->fifo_mpeg2;
691 count = hb_list_count( title->list_subtitle );
692 count = count > 99 ? 99 : count;
693 for( i=0; i < count; i++ ) {
694 subtitle = hb_list_item( title->list_subtitle, i );
695 if (id == subtitle->id) {
697 if( !job->indepth_scan || job->select_subtitle_config.force )
700 * Pass the subtitles to be processed if we are not scanning, or if
701 * we are scanning and looking for forced subs, then pass them up
702 * to decode whether the sub is a forced one.
704 fifos[n++] = subtitle->fifo_in;
713 if( !job->indepth_scan )
716 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
718 audio = hb_list_item( title->list_audio, i );
719 if( id == audio->id )
721 fifos[n++] = audio->priv.fifo_in;