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
26 stream_timing_t *stream_timing;
31 uint8_t st_slots; // size (in slots) of stream_timing array
32 uint8_t saw_video; // != 0 if we've seen video
33 uint8_t saw_audio; // != 0 if we've seen audio
35 int start_found; // found pts_to_start point
39 /***********************************************************************
41 **********************************************************************/
42 static void ReaderFunc( void * );
43 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id );
44 static void UpdateState( hb_reader_t * r, int64_t start);
46 /***********************************************************************
48 ***********************************************************************
50 **********************************************************************/
51 hb_thread_t * hb_reader_init( hb_job_t * job )
55 r = calloc( sizeof( hb_reader_t ), 1 );
58 r->title = job->title;
63 r->stream_timing = calloc( sizeof(stream_timing_t), r->st_slots );
64 r->stream_timing[0].id = r->title->video_id;
65 r->stream_timing[0].average = 90000. * (double)job->vrate_base /
67 r->stream_timing[0].last = -r->stream_timing[0].average;
68 r->stream_timing[1].id = -1;
70 if ( !job->pts_to_start )
73 return hb_thread_init( "reader", ReaderFunc, r,
77 static void push_buf( const hb_reader_t *r, hb_fifo_t *fifo, hb_buffer_t *buf )
79 while ( !*r->die && !r->job->done )
81 if ( hb_fifo_full_wait( fifo ) )
83 hb_fifo_push( fifo, buf );
89 static int is_audio( hb_reader_t *r, int id )
94 for( i = 0; ( audio = hb_list_item( r->title->list_audio, i ) ); ++i )
96 if ( audio->id == id )
104 // The MPEG STD (Standard Target Decoder) essentially requires that we keep
105 // per-stream timing so that when there's a timing discontinuity we can
106 // seemlessly join packets on either side of the discontinuity. This join
107 // requires that we know the timestamp of the previous packet and the
108 // average inter-packet time (since we position the new packet at the end
109 // of the previous packet). The next four routines keep track of this
110 // per-stream timing.
112 // find the per-stream timing state for 'buf'
114 static stream_timing_t *find_st( hb_reader_t *r, const hb_buffer_t *buf )
116 stream_timing_t *st = r->stream_timing;
117 for ( ; st->id != -1; ++st )
119 if ( st->id == buf->id )
125 // find or create the per-stream timing state for 'buf'
127 static stream_timing_t *id_to_st( hb_reader_t *r, const hb_buffer_t *buf )
129 stream_timing_t *st = r->stream_timing;
130 while ( st->id != buf->id && st->id != -1)
134 // if we haven't seen this stream add it.
137 // we keep the steam timing info in an array with some power-of-two
138 // number of slots. If we don't have two slots left (one for our new
139 // entry plus one for the "-1" eol) we need to expand the array.
140 int slot = st - r->stream_timing;
141 if ( slot + 1 >= r->st_slots )
144 r->stream_timing = realloc( r->stream_timing, r->st_slots *
145 sizeof(*r->stream_timing) );
146 st = r->stream_timing + slot;
149 st->average = 30.*90.;
151 st->last = buf->renderOffset - st->average;
153 st->last = -st->average;
154 if ( ( st->is_audio = is_audio( r, buf->id ) ) != 0 )
163 // update the average inter-packet time of the stream associated with 'buf'
164 // using a recursive low-pass filter with a 16 packet time constant.
166 static void update_ipt( hb_reader_t *r, const hb_buffer_t *buf )
168 stream_timing_t *st = id_to_st( r, buf );
169 double dt = buf->renderOffset - st->last;
170 st->average += ( dt - st->average ) * (1./32.);
171 st->last = buf->renderOffset;
174 // use the per-stream state associated with 'buf' to compute a new scr_offset
175 // such that 'buf' will follow the previous packet of this stream separated
176 // by the average packet time of the stream.
178 static void new_scr_offset( hb_reader_t *r, hb_buffer_t *buf )
180 stream_timing_t *st = id_to_st( r, buf );
181 int64_t nxt = st->last + st->average;
182 r->scr_offset = buf->renderOffset - nxt;
183 buf->renderOffset = nxt;
184 r->scr_changes = r->demux.scr_changes;
185 st->last = buf->renderOffset;
188 /***********************************************************************
190 ***********************************************************************
192 **********************************************************************/
193 static void ReaderFunc( void * _r )
195 hb_reader_t * r = _r;
201 int chapter_end = r->job->chapter_end;
203 if ( r->title->type == HB_DVD_TYPE )
205 if ( !( r->dvd = hb_dvd_init( r->title->path ) ) )
208 else if ( r->title->type == HB_STREAM_TYPE )
210 if ( !( r->stream = hb_stream_open( r->title->path, r->title ) ) )
215 // Unknown type, should never happen
219 hb_buffer_t *ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
223 * XXX this code is a temporary hack that should go away if/when
224 * chapter merging goes away in libhb/dvd.c
225 * map the start and end chapter numbers to on-media chapter
226 * numbers since chapter merging could cause the handbrake numbers
227 * to diverge from the media numbers and, if our chapter_end is after
228 * a media chapter that got merged, we'll stop ripping too early.
230 int start = r->job->chapter_start;
231 hb_chapter_t *chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
233 chapter_end = chap->index;
236 chap = hb_list_item( r->title->list_chapter, start - 1 );
239 /* end chapter mapping XXX */
241 if( !hb_dvd_start( r->dvd, r->title, start ) )
243 hb_dvd_close( &r->dvd );
244 hb_buffer_close( &ps );
249 hb_dvd_set_angle( r->dvd, r->job->angle );
252 if ( r->job->start_at_preview )
254 // XXX code from DecodePreviews - should go into its own routine
255 hb_dvd_seek( r->dvd, (float)r->job->start_at_preview /
256 ( r->job->seek_points ? ( r->job->seek_points + 1.0 ) : 11.0 ) );
259 else if ( r->stream && r->job->start_at_preview )
262 // XXX code from DecodePreviews - should go into its own routine
263 hb_stream_seek( r->stream, (float)( r->job->start_at_preview - 1 ) /
264 ( r->job->seek_points ? ( r->job->seek_points + 1.0 ) : 11.0 ) );
267 else if ( r->stream && r->job->pts_to_start )
269 int64_t pts_to_start = r->job->pts_to_start;
271 // Find out what the first timestamp of the stream is
272 // and then seek to the appropriate offset from it
273 if ( hb_stream_read( r->stream, ps ) )
276 pts_to_start += ps->start;
279 if ( hb_stream_seek_ts( r->stream, pts_to_start ) >= 0 )
281 // Seek takes us to the nearest I-frame before the timestamp
282 // that we want. So we will retrieve the start time of the
283 // first packet we get, subtract that from pts_to_start, and
284 // inspect the reset of the frames in sync.
286 r->job->pts_to_start = pts_to_start;
292 * Standard stream, seek to the starting chapter, if set, and track the
293 * end chapter so that we end at the right time.
295 int start = r->job->chapter_start;
296 hb_chapter_t *chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
298 chapter_end = chap->index;
301 chap = hb_list_item( r->title->list_chapter, start - 1 );
306 * Seek to the start chapter.
308 hb_stream_seek_chapter( r->stream, start );
311 list = hb_list_init();
313 while( !*r->die && !r->job->done )
316 chapter = hb_dvd_chapter( r->dvd );
318 chapter = hb_stream_chapter( r->stream );
322 hb_log( "reader: end of the title reached" );
325 if( chapter > chapter_end )
327 hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
328 r->job->chapter_end, chapter_end, chapter );
334 if( !hb_dvd_read( r->dvd, ps ) )
341 if ( !hb_stream_read( r->stream, ps ) )
345 if ( r->start_found == 2 )
347 // We will inspect the timestamps of each frame in sync
348 // to skip from this seek point to the timestamp we
350 if ( ps->start > 0 && ps->start < r->job->pts_to_start )
351 r->job->pts_to_start -= ps->start;
352 else if ( ps->start >= r->job->pts_to_start )
353 r->job->pts_to_start = 0;
358 if( r->job->indepth_scan )
361 * Need to update the progress during a subtitle scan
365 #define p state.param.working
367 state.state = HB_STATE_WORKING;
368 p.progress = (double)chapter / (double)r->job->chapter_end;
369 if( p.progress > 1.0 )
377 hb_set_state( r->job->h, &state );
380 (hb_demux[r->title->demuxer])( ps, list, &r->demux );
382 while( ( buf = hb_list_item( list, 0 ) ) )
384 hb_list_rem( list, buf );
385 fifos = GetFifoForId( r->job, buf->id );
387 if ( fifos && ! r->saw_video && !r->job->indepth_scan )
389 // The first data packet with a PTS from an audio or video stream
390 // that we're decoding defines 'time zero'. Discard packets until
392 if ( buf->start != -1 && buf->renderOffset != -1 &&
393 ( buf->id == r->title->video_id || is_audio( r, buf->id ) ) )
395 // force a new scr offset computation
396 r->scr_changes = r->demux.scr_changes - 1;
397 // create a stream state if we don't have one so the
398 // offset will get computed correctly.
401 hb_log( "reader: first SCR %"PRId64" id %d DTS %"PRId64,
402 r->demux.last_scr, buf->id, buf->renderOffset );
411 if ( buf->renderOffset != -1 )
413 if ( r->scr_changes != r->demux.scr_changes )
415 // This is the first audio or video packet after an SCR
416 // change. Compute a new scr offset that would make this
417 // packet follow the last of this stream with the correct
419 stream_timing_t *st = find_st( r, buf );
423 // if this is the video stream and we don't have
424 // audio yet or this is an audio stream
425 // generate a new scr
427 ( st == r->stream_timing && !r->saw_audio ) )
429 new_scr_offset( r, buf );
433 // defer the scr change until we get some
434 // audio since audio has a timestamp per
435 // frame but video & subtitles don't. Clear
436 // the timestamps so the decoder will generate
437 // them from the frame durations.
438 if ( st != r->stream_timing )
440 // not a video stream so it's probably
441 // subtitles - the best we can do is to
442 // line it up with the last video packet.
443 buf->start = r->stream_timing->last;
448 buf->renderOffset = -1;
454 // we got a new scr at the same time as the first
455 // packet of a stream we've never seen before. We
456 // have no idea what the timing should be so toss
457 // this buffer & wait for a stream we've already seen.
458 // add stream to list of streams we have seen
460 hb_buffer_close( &buf );
465 if ( buf->start != -1 )
467 int64_t start = buf->start - r->scr_offset;
468 if ( !r->start_found )
469 UpdateState( r, start );
471 if ( !r->start_found &&
472 r->job->pts_to_start &&
473 buf->renderOffset != -1 &&
474 start >= r->job->pts_to_start )
476 // pts_to_start point found
477 // force a new scr offset computation
478 stream_timing_t *st = find_st( r, buf );
481 ( st == r->stream_timing && !r->saw_audio ) ) )
483 // Re-zero our timestamps
484 st->last = -st->average;
485 new_scr_offset( r, buf );
487 r->job->pts_to_start = 0;
490 buf->start -= r->scr_offset;
492 if ( buf->renderOffset != -1 )
494 if ( r->scr_changes == r->demux.scr_changes )
496 // This packet is referenced to the same SCR as the last.
497 // Adjust timestamp to remove the System Clock Reference
498 // offset then update the average inter-packet time
500 buf->renderOffset -= r->scr_offset;
501 update_ipt( r, buf );
504 if ( !r->start_found )
506 hb_buffer_close( &buf );
510 buf->sequence = r->sequence++;
511 /* if there are mutiple output fifos, send a copy of the
512 * buffer down all but the first (we have to not ship the
513 * original buffer or we'll race with the thread that's
514 * consuming the buffer & inject garbage into the data stream). */
515 for( n = 1; fifos[n] != NULL; n++)
517 hb_buffer_t *buf_copy = hb_buffer_init( buf->size );
518 hb_buffer_copy_settings( buf_copy, buf );
519 memcpy( buf_copy->data, buf->data, buf->size );
520 push_buf( r, fifos[n], buf_copy );
522 push_buf( r, fifos[0], buf );
526 hb_buffer_close( &buf );
531 // send empty buffers downstream to video & audio decoders to signal we're done.
532 if( !*r->die && !r->job->done )
534 push_buf( r, r->job->fifo_mpeg2, hb_buffer_init(0) );
537 for( n = 0; (audio = hb_list_item( r->job->title->list_audio, n)); ++n )
539 if ( audio->priv.fifo_in )
540 push_buf( r, audio->priv.fifo_in, hb_buffer_init(0) );
543 hb_subtitle_t *subtitle;
544 for( n = 0; (subtitle = hb_list_item( r->job->title->list_subtitle, n)); ++n )
546 if ( subtitle->fifo_in && subtitle->source == VOBSUB)
547 push_buf( r, subtitle->fifo_in, hb_buffer_init(0) );
551 hb_list_empty( &list );
552 hb_buffer_close( &ps );
555 hb_dvd_stop( r->dvd );
556 hb_dvd_close( &r->dvd );
560 hb_stream_close(&r->stream);
563 if ( r->stream_timing )
565 free( r->stream_timing );
568 hb_log( "reader: done. %d scr changes", r->demux.scr_changes );
569 if ( r->demux.dts_drops )
571 hb_log( "reader: %d drops because DTS out of range", r->demux.dts_drops );
578 static void UpdateState( hb_reader_t * r, int64_t start)
590 #define p state.param.working
591 state.state = HB_STATE_SEARCHING;
592 p.progress = (float) start / (float) r->job->pts_to_start;
593 if( p.progress > 1.0 )
597 if (now > r->st_first)
601 avg = 1000.0 * (double)start / (now - r->st_first);
602 eta = ( r->job->pts_to_start - start ) / avg;
603 p.hours = eta / 3600;
604 p.minutes = ( eta % 3600 ) / 60;
605 p.seconds = eta % 60;
616 hb_set_state( r->job->h, &state );
618 /***********************************************************************
620 ***********************************************************************
622 **********************************************************************/
623 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
625 hb_title_t * title = job->title;
627 hb_subtitle_t * subtitle;
629 static hb_fifo_t * fifos[100];
631 memset(fifos, 0, sizeof(fifos));
633 if( id == title->video_id )
635 if( job->indepth_scan )
638 * Ditch the video here during the indepth scan until
639 * we can improve the MPEG2 decode performance.
645 fifos[0] = job->fifo_mpeg2;
651 count = hb_list_count( title->list_subtitle );
652 count = count > 99 ? 99 : count;
653 for( i=0; i < count; i++ ) {
654 subtitle = hb_list_item( title->list_subtitle, i );
655 if (id == subtitle->id) {
657 if( !job->indepth_scan || job->select_subtitle_config.force )
660 * Pass the subtitles to be processed if we are not scanning, or if
661 * we are scanning and looking for forced subs, then pass them up
662 * to decode whether the sub is a forced one.
664 fifos[n++] = subtitle->fifo_in;
673 if( !job->indepth_scan )
676 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
678 audio = hb_list_item( title->list_audio, i );
679 if( id == audio->id )
681 fifos[n++] = audio->priv.fifo_in;