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
36 /***********************************************************************
38 **********************************************************************/
39 static void ReaderFunc( void * );
40 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id );
42 /***********************************************************************
44 ***********************************************************************
46 **********************************************************************/
47 hb_thread_t * hb_reader_init( hb_job_t * job )
51 r = calloc( sizeof( hb_reader_t ), 1 );
54 r->title = job->title;
59 r->stream_timing = calloc( sizeof(stream_timing_t), r->st_slots );
60 r->stream_timing[0].id = r->title->video_id;
61 r->stream_timing[0].average = 90000. * (double)job->vrate_base /
63 r->stream_timing[0].last = -r->stream_timing[0].average;
64 r->stream_timing[1].id = -1;
66 return hb_thread_init( "reader", ReaderFunc, r,
70 static void push_buf( const hb_reader_t *r, hb_fifo_t *fifo, hb_buffer_t *buf )
72 hb_fifo_push_wait( fifo, buf );
75 static int is_audio( hb_reader_t *r, int id )
80 for( i = 0; ( audio = hb_list_item( r->title->list_audio, i ) ); ++i )
82 if ( audio->id == id )
90 // The MPEG STD (Standard Target Decoder) essentially requires that we keep
91 // per-stream timing so that when there's a timing discontinuity we can
92 // seemlessly join packets on either side of the discontinuity. This join
93 // requires that we know the timestamp of the previous packet and the
94 // average inter-packet time (since we position the new packet at the end
95 // of the previous packet). The next four routines keep track of this
98 // find the per-stream timing state for 'buf'
100 static stream_timing_t *find_st( hb_reader_t *r, const hb_buffer_t *buf )
102 stream_timing_t *st = r->stream_timing;
103 for ( ; st->id != -1; ++st )
105 if ( st->id == buf->id )
111 // find or create the per-stream timing state for 'buf'
113 static stream_timing_t *id_to_st( hb_reader_t *r, const hb_buffer_t *buf )
115 stream_timing_t *st = r->stream_timing;
116 while ( st->id != buf->id && st->id != -1)
120 // if we haven't seen this stream add it.
123 // we keep the steam timing info in an array with some power-of-two
124 // number of slots. If we don't have two slots left (one for our new
125 // entry plus one for the "-1" eol) we need to expand the array.
126 int slot = st - r->stream_timing;
127 if ( slot + 1 >= r->st_slots )
130 r->stream_timing = realloc( r->stream_timing, r->st_slots *
131 sizeof(*r->stream_timing) );
132 st = r->stream_timing + slot;
135 st->average = 30.*90.;
137 st->last = buf->renderOffset - st->average;
139 st->last = -st->average;
140 if ( ( st->is_audio = is_audio( r, buf->id ) ) != 0 )
149 // update the average inter-packet time of the stream associated with 'buf'
150 // using a recursive low-pass filter with a 16 packet time constant.
152 static void update_ipt( hb_reader_t *r, const hb_buffer_t *buf )
154 stream_timing_t *st = id_to_st( r, buf );
155 double dt = buf->renderOffset - st->last;
156 st->average += ( dt - st->average ) * (1./32.);
157 st->last = buf->renderOffset;
160 // use the per-stream state associated with 'buf' to compute a new scr_offset
161 // such that 'buf' will follow the previous packet of this stream separated
162 // by the average packet time of the stream.
164 static void new_scr_offset( hb_reader_t *r, hb_buffer_t *buf )
166 stream_timing_t *st = id_to_st( r, buf );
167 int64_t nxt = st->last + st->average;
168 r->scr_offset = buf->renderOffset - nxt;
169 buf->renderOffset = nxt;
170 r->scr_changes = r->demux.scr_changes;
171 st->last = buf->renderOffset;
174 /***********************************************************************
176 ***********************************************************************
178 **********************************************************************/
179 static void ReaderFunc( void * _r )
181 hb_reader_t * r = _r;
187 int chapter_end = r->job->chapter_end;
189 if ( r->title->type == HB_DVD_TYPE )
191 if ( !( r->dvd = hb_dvd_init( r->title->path ) ) )
194 else if ( r->title->type == HB_STREAM_TYPE )
196 if ( !( r->stream = hb_stream_open( r->title->path, r->title ) ) )
201 // Unknown type, should never happen
208 * XXX this code is a temporary hack that should go away if/when
209 * chapter merging goes away in libhb/dvd.c
210 * map the start and end chapter numbers to on-media chapter
211 * numbers since chapter merging could cause the handbrake numbers
212 * to diverge from the media numbers and, if our chapter_end is after
213 * a media chapter that got merged, we'll stop ripping too early.
215 int start = r->job->chapter_start;
216 hb_chapter_t *chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
218 chapter_end = chap->index;
221 chap = hb_list_item( r->title->list_chapter, start - 1 );
224 /* end chapter mapping XXX */
226 if( !hb_dvd_start( r->dvd, r->title, start ) )
228 hb_dvd_close( &r->dvd );
233 hb_dvd_set_angle( r->dvd, r->job->angle );
236 if ( r->job->start_at_preview )
238 // XXX code from DecodePreviews - should go into its own routine
239 hb_dvd_seek( r->dvd, (float)r->job->start_at_preview /
240 ( r->job->seek_points ? ( r->job->seek_points + 1.0 ) : 11.0 ) );
243 else if ( r->stream && r->job->start_at_preview )
246 // XXX code from DecodePreviews - should go into its own routine
247 hb_stream_seek( r->stream, (float)( r->job->start_at_preview - 1 ) /
248 ( r->job->seek_points ? ( r->job->seek_points + 1.0 ) : 11.0 ) );
254 * Standard stream, seek to the starting chapter, if set, and track the
255 * end chapter so that we end at the right time.
257 int start = r->job->chapter_start;
258 hb_chapter_t *chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
260 chapter_end = chap->index;
263 chap = hb_list_item( r->title->list_chapter, start - 1 );
268 * Seek to the start chapter.
270 hb_stream_seek_chapter( r->stream, start );
273 list = hb_list_init();
274 hb_buffer_t *ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
276 while( !*r->die && !r->job->done )
279 chapter = hb_dvd_chapter( r->dvd );
281 chapter = hb_stream_chapter( r->stream );
285 hb_log( "reader: end of the title reached" );
288 if( chapter > chapter_end )
290 hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
291 r->job->chapter_end, chapter_end, chapter );
297 if( !hb_dvd_read( r->dvd, ps ) )
304 if ( !hb_stream_read( r->stream, ps ) )
310 if( r->job->indepth_scan )
313 * Need to update the progress during a subtitle scan
317 #define p state.param.working
319 state.state = HB_STATE_WORKING;
320 p.progress = (double)chapter / (double)r->job->chapter_end;
321 if( p.progress > 1.0 )
329 hb_set_state( r->job->h, &state );
332 (hb_demux[r->title->demuxer])( ps, list, &r->demux );
334 while( ( buf = hb_list_item( list, 0 ) ) )
336 hb_list_rem( list, buf );
337 fifos = GetFifoForId( r->job, buf->id );
339 if ( fifos && ! r->saw_video )
341 // The first data packet with a PTS from an audio or video stream
342 // that we're decoding defines 'time zero'. Discard packets until
344 if ( buf->start != -1 && buf->renderOffset != -1 &&
345 ( buf->id == r->title->video_id || is_audio( r, buf->id ) ) )
347 // force a new scr offset computation
348 r->scr_changes = r->demux.scr_changes - 1;
349 // create a stream state if we don't have one so the
350 // offset will get computed correctly.
353 hb_log( "reader: first SCR %"PRId64" id %d DTS %"PRId64,
354 r->demux.last_scr, buf->id, buf->renderOffset );
363 if ( buf->renderOffset != -1 )
365 if ( r->scr_changes == r->demux.scr_changes )
367 // This packet is referenced to the same SCR as the last.
368 // Adjust timestamp to remove the System Clock Reference
369 // offset then update the average inter-packet time
371 buf->renderOffset -= r->scr_offset;
372 update_ipt( r, buf );
376 // This is the first audio or video packet after an SCR
377 // change. Compute a new scr offset that would make this
378 // packet follow the last of this stream with the correct
380 stream_timing_t *st = find_st( r, buf );
384 // if this is the video stream and we don't have
385 // audio yet or this is an audio stream
386 // generate a new scr
388 ( st == r->stream_timing && !r->saw_audio ) )
390 new_scr_offset( r, buf );
394 // defer the scr change until we get some
395 // audio since audio has a timestamp per
396 // frame but video & subtitles don't. Clear
397 // the timestamps so the decoder will generate
398 // them from the frame durations.
399 if ( st != r->stream_timing )
401 // not a video stream so it's probably
402 // subtitles - the best we can do is to
403 // line it up with the last video packet.
404 buf->start = r->stream_timing->last;
409 buf->renderOffset = -1;
415 // we got a new scr at the same time as the first
416 // packet of a stream we've never seen before. We
417 // have no idea what the timing should be so toss
418 // this buffer & wait for a stream we've already seen.
419 // add stream to list of streams we have seen
421 hb_buffer_close( &buf );
426 if ( buf->start != -1 )
428 buf->start -= r->scr_offset;
429 if ( r->job->pts_to_stop && buf->start > r->job->pts_to_stop )
431 // we're doing a subset of the input and we've hit the
433 hb_buffer_close( &buf );
438 buf->sequence = r->sequence++;
439 /* if there are mutiple output fifos, send a copy of the
440 * buffer down all but the first (we have to not ship the
441 * original buffer or we'll race with the thread that's
442 * consuming the buffer & inject garbage into the data stream). */
443 for( n = 1; fifos[n] != NULL; n++)
445 hb_buffer_t *buf_copy = hb_buffer_init( buf->size );
446 hb_buffer_copy_settings( buf_copy, buf );
447 memcpy( buf_copy->data, buf->data, buf->size );
448 push_buf( r, fifos[n], buf_copy );
450 push_buf( r, fifos[0], buf );
454 hb_buffer_close( &buf );
460 // send empty buffers downstream to video & audio decoders to signal we're done.
461 if( !*r->die && !r->job->done )
463 push_buf( r, r->job->fifo_mpeg2, hb_buffer_init(0) );
466 for( n = 0; (audio = hb_list_item( r->job->title->list_audio, n)); ++n )
468 if ( audio->priv.fifo_in )
469 push_buf( r, audio->priv.fifo_in, hb_buffer_init(0) );
472 hb_subtitle_t *subtitle;
473 for( n = 0; (subtitle = hb_list_item( r->job->title->list_subtitle, n)); ++n )
475 if ( subtitle->fifo_in && subtitle->source == VOBSUB)
476 push_buf( r, subtitle->fifo_in, hb_buffer_init(0) );
480 hb_list_empty( &list );
481 hb_buffer_close( &ps );
484 hb_dvd_stop( r->dvd );
485 hb_dvd_close( &r->dvd );
489 hb_stream_close(&r->stream);
492 if ( r->stream_timing )
494 free( r->stream_timing );
497 hb_log( "reader: done. %d scr changes", r->demux.scr_changes );
498 if ( r->demux.dts_drops )
500 hb_log( "reader: %d drops because DTS out of range", r->demux.dts_drops );
507 /***********************************************************************
509 ***********************************************************************
511 **********************************************************************/
512 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
514 hb_title_t * title = job->title;
516 hb_subtitle_t * subtitle;
518 static hb_fifo_t * fifos[100];
520 memset(fifos, 0, sizeof(fifos));
522 if( id == title->video_id )
524 if( job->indepth_scan )
527 * Ditch the video here during the indepth scan until
528 * we can improve the MPEG2 decode performance.
534 fifos[0] = job->fifo_mpeg2;
540 count = hb_list_count( title->list_subtitle );
541 count = count > 99 ? 99 : count;
542 for( i=0; i < count; i++ ) {
543 subtitle = hb_list_item( title->list_subtitle, i );
544 if (id == subtitle->id) {
546 if( !job->indepth_scan || job->select_subtitle_config.force )
549 * Pass the subtitles to be processed if we are not scanning, or if
550 * we are scanning and looking for forced subs, then pass them up
551 * to decode whether the sub is a forced one.
553 fifos[n++] = subtitle->fifo_in;
562 if( !job->indepth_scan )
565 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
567 audio = hb_list_item( title->list_audio, i );
568 if( id == audio->id )
570 fifos[n++] = audio->priv.fifo_in;