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
25 stream_timing_t *stream_timing;
31 int st_slots; // size (in slots) of stream_timing array
34 /***********************************************************************
36 **********************************************************************/
37 static void ReaderFunc( void * );
38 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id );
40 /***********************************************************************
42 ***********************************************************************
44 **********************************************************************/
45 hb_thread_t * hb_reader_init( hb_job_t * job )
49 r = calloc( sizeof( hb_reader_t ), 1 );
52 r->title = job->title;
57 r->stream_timing = calloc( sizeof(stream_timing_t), r->st_slots );
58 r->stream_timing[0].id = r->title->video_id;
59 r->stream_timing[0].average = 90000. * (double)job->vrate_base /
61 r->stream_timing[0].last = -r->stream_timing[0].average;
62 r->stream_timing[1].id = -1;
64 return hb_thread_init( "reader", ReaderFunc, r,
68 static void push_buf( const hb_reader_t *r, hb_fifo_t *fifo, hb_buffer_t *buf )
70 while( !*r->die && !r->job->done && hb_fifo_is_full( fifo ) )
73 * Loop until the incoming fifo is ready to receive
78 hb_fifo_push( fifo, buf );
81 // The MPEG STD (Standard Target Decoder) essentially requires that we keep
82 // per-stream timing so that when there's a timing discontinuity we can
83 // seemlessly join packets on either side of the discontinuity. This join
84 // requires that we know the timestamp of the previous packet and the
85 // average inter-packet time (since we position the new packet at the end
86 // of the previous packet). The next four routines keep track of this
89 // find the per-stream timing state for 'buf'
91 static stream_timing_t *find_st( hb_reader_t *r, const hb_buffer_t *buf )
93 stream_timing_t *st = r->stream_timing;
94 for ( ; st->id != -1; ++st )
96 if ( st->id == buf->id )
102 // find or create the per-stream timing state for 'buf'
104 static stream_timing_t *id_to_st( hb_reader_t *r, const hb_buffer_t *buf )
106 stream_timing_t *st = r->stream_timing;
107 while ( st->id != buf->id && st->id != -1)
111 // if we haven't seen this stream add it.
114 // we keep the steam timing info in an array with some power-of-two
115 // number of slots. If we don't have two slots left (one for our new
116 // entry plus one for the "-1" eol) we need to expand the array.
117 int slot = st - r->stream_timing;
118 if ( slot + 1 >= r->st_slots )
121 r->stream_timing = realloc( r->stream_timing, r->st_slots *
122 sizeof(*r->stream_timing) );
123 st = r->stream_timing + slot;
126 st->average = 30.*90.;
127 st->last = buf->renderOffset - st->average;
134 // update the average inter-packet time of the stream associated with 'buf'
135 // using a recursive low-pass filter with a 16 packet time constant.
137 static void update_ipt( hb_reader_t *r, const hb_buffer_t *buf )
139 stream_timing_t *st = id_to_st( r, buf );
140 double dt = buf->renderOffset - st->last;
141 st->average += ( dt - st->average ) * (1./32.);
142 st->last = buf->renderOffset;
145 // use the per-stream state associated with 'buf' to compute a new scr_offset
146 // such that 'buf' will follow the previous packet of this stream separated
147 // by the average packet time of the stream.
149 static void new_scr_offset( hb_reader_t *r, hb_buffer_t *buf )
151 stream_timing_t *st = id_to_st( r, buf );
152 int64_t nxt = st->last + st->average;
153 r->scr_offset = buf->renderOffset - nxt;
154 buf->renderOffset = nxt;
155 r->scr_changes = r->demux.scr_changes;
156 st->last = buf->renderOffset;
159 /***********************************************************************
161 ***********************************************************************
163 **********************************************************************/
164 static void ReaderFunc( void * _r )
166 hb_reader_t * r = _r;
172 int chapter_end = r->job->chapter_end;
174 if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
176 if ( !( r->stream = hb_stream_open( r->title->dvd, r->title ) ) )
185 * XXX this code is a temporary hack that should go away if/when
186 * chapter merging goes away in libhb/dvd.c
187 * map the start and end chapter numbers to on-media chapter
188 * numbers since chapter merging could cause the handbrake numbers
189 * to diverge from the media numbers and, if our chapter_end is after
190 * a media chapter that got merged, we'll stop ripping too early.
192 int start = r->job->chapter_start;
193 hb_chapter_t *chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
195 chapter_end = chap->index;
198 chap = hb_list_item( r->title->list_chapter, start - 1 );
201 /* end chapter mapping XXX */
203 if( !hb_dvd_start( r->dvd, r->title->index, start ) )
205 hb_dvd_close( &r->dvd );
209 if ( r->job->start_at_preview )
211 // XXX code from DecodePreviews - should go into its own routine
212 hb_dvd_seek( r->dvd, (float)r->job->start_at_preview / 11. );
215 else if ( r->stream && r->job->start_at_preview )
217 // XXX code from DecodePreviews - should go into its own routine
218 hb_stream_seek( r->stream, (float)( r->job->start_at_preview - 1 ) / 11. );
221 list = hb_list_init();
222 hb_buffer_t *ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
223 r->demux.flaky_clock = r->title->flaky_clock;
225 while( !*r->die && !r->job->done )
228 chapter = hb_dvd_chapter( r->dvd );
234 hb_log( "reader: end of the title reached" );
237 if( chapter > chapter_end )
239 hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
240 r->job->chapter_end, chapter_end, chapter );
246 if( !hb_dvd_read( r->dvd, ps ) )
253 if ( !hb_stream_read( r->stream, ps ) )
259 if( r->job->indepth_scan )
262 * Need to update the progress during a subtitle scan
266 #define p state.param.working
268 state.state = HB_STATE_WORKING;
269 p.progress = (double)chapter / (double)r->job->chapter_end;
270 if( p.progress > 1.0 )
278 hb_set_state( r->job->h, &state );
281 (hb_demux[r->title->demuxer])( ps, list, &r->demux );
283 while( ( buf = hb_list_item( list, 0 ) ) )
285 hb_list_rem( list, buf );
286 fifos = GetFifoForId( r->job, buf->id );
288 if ( ! r->saw_video )
290 /* The first video packet defines 'time zero' so discard
291 data until we get a video packet with a PTS & DTS */
292 if ( buf->id == r->title->video_id && buf->start != -1 &&
293 buf->renderOffset != -1 )
295 // force a new scr offset computation
296 r->scr_changes = r->demux.scr_changes - 1;
298 hb_log( "reader: first SCR %lld", r->demux.last_scr );
307 if ( buf->renderOffset != -1 )
309 if ( r->scr_changes == r->demux.scr_changes )
311 // This packet is referenced to the same SCR as the last.
312 // Adjust timestamp to remove the System Clock Reference
313 // offset then update the average inter-packet time
315 buf->renderOffset -= r->scr_offset;
316 update_ipt( r, buf );
320 // This is the first audio or video packet after an SCR
321 // change. Compute a new scr offset that would make this
322 // packet follow the last of this stream with the correct
324 stream_timing_t *st = find_st( r, buf );
328 // if this isn't the video stream or we don't
329 // have audio yet then generate a new scr
330 if ( st != r->stream_timing ||
331 r->stream_timing[1].id == -1 )
333 new_scr_offset( r, buf );
337 // defer the scr change until we get some
338 // audio since audio has a timestamp per
339 // frame but video doesn't. Clear the timestamps
340 // so the decoder will regenerate them from
341 // the frame durations.
343 buf->renderOffset = -1;
348 // we got a new scr at the same time as the first
349 // packet of a stream we've never seen before. We
350 // have no idea what the timing should be so toss
351 // this buffer & wait for a stream we've already seen.
352 hb_buffer_close( &buf );
357 if ( buf->start != -1 )
359 buf->start -= r->scr_offset;
360 if ( r->job->pts_to_stop && buf->start > r->job->pts_to_stop )
362 // we're doing a subset of the input and we've hit the
364 hb_buffer_close( &buf );
369 buf->sequence = r->sequence++;
370 /* if there are mutiple output fifos, send a copy of the
371 * buffer down all but the first (we have to not ship the
372 * original buffer or we'll race with the thread that's
373 * consuming the buffer & inject garbage into the data stream). */
374 for( n = 1; fifos[n] != NULL; n++)
376 hb_buffer_t *buf_copy = hb_buffer_init( buf->size );
377 hb_buffer_copy_settings( buf_copy, buf );
378 memcpy( buf_copy->data, buf->data, buf->size );
379 push_buf( r, fifos[n], buf_copy );
381 push_buf( r, fifos[0], buf );
385 hb_buffer_close( &buf );
391 // send empty buffers downstream to video & audio decoders to signal we're done.
392 push_buf( r, r->job->fifo_mpeg2, hb_buffer_init(0) );
395 for( n = 0; ( audio = hb_list_item( r->job->title->list_audio, n ) ); ++n )
397 if ( audio->priv.fifo_in )
398 push_buf( r, audio->priv.fifo_in, hb_buffer_init(0) );
401 hb_list_empty( &list );
402 hb_buffer_close( &ps );
405 hb_dvd_stop( r->dvd );
406 hb_dvd_close( &r->dvd );
410 hb_stream_close(&r->stream);
413 if ( r->stream_timing )
415 free( r->stream_timing );
418 hb_log( "reader: done. %d scr changes", r->demux.scr_changes );
419 if ( r->demux.dts_drops )
421 hb_log( "reader: %d drops because DTS out of range", r->demux.dts_drops );
428 /***********************************************************************
430 ***********************************************************************
432 **********************************************************************/
433 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
435 hb_title_t * title = job->title;
437 hb_subtitle_t * subtitle;
439 static hb_fifo_t * fifos[8];
441 memset(fifos, 0, sizeof(fifos));
443 if( id == title->video_id )
445 if( job->indepth_scan )
448 * Ditch the video here during the indepth scan until
449 * we can improve the MPEG2 decode performance.
455 fifos[0] = job->fifo_mpeg2;
460 if( job->indepth_scan ) {
462 * Count the occurances of the subtitles, don't actually
463 * return any to encode unless we are looking fro forced
464 * subtitles in which case we need to look in the sub picture
465 * to see if it has the forced flag enabled.
467 for (i=0; i < hb_list_count(title->list_subtitle); i++) {
468 subtitle = hb_list_item( title->list_subtitle, i);
469 if (id == subtitle->id) {
474 if( job->subtitle_force )
477 fifos[0] = subtitle->fifo_in;
484 if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
487 fifos[0] = subtitle->fifo_in;
491 if( !job->indepth_scan )
494 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
496 audio = hb_list_item( title->list_audio, i );
497 if( id == audio->id )
499 fifos[n++] = audio->priv.fifo_in;