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 int64_t last; // last timestamp seen on this stream
12 double average; // average time between packets
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[1].id = -1;
63 return hb_thread_init( "reader", ReaderFunc, r,
67 static void push_buf( const hb_reader_t *r, hb_fifo_t *fifo, hb_buffer_t *buf )
69 while( !*r->die && !r->job->done && hb_fifo_is_full( fifo ) )
72 * Loop until the incoming fifo is ready to receive
77 hb_fifo_push( fifo, buf );
80 // The MPEG STD (Standard Timing Decoder) essentially requires that we keep
81 // per-stream timing so that when there's a timing discontinuity we can
82 // seemlessly join packets on either side of the discontinuity. This join
83 // requires that we know the timestamp of the previous packet and the
84 // average inter-packet time (since we position the new packet at the end
85 // of the previous packet). The next three routines keep track of this
88 // find the per-stream timing state associated with 'buf'
90 static stream_timing_t *id_to_st( hb_reader_t *r, const hb_buffer_t *buf )
92 stream_timing_t *st = r->stream_timing;
93 while ( st->id != buf->id && st->id != -1)
97 // if we haven't seen this stream add it.
100 // we keep the steam timing info in an array with some power-of-two
101 // number of slots. If we don't have two slots left (one for our new
102 // entry plus one for the "-1" eol) we need to expand the array.
103 int slot = st - r->stream_timing;
104 if ( slot + 1 >= r->st_slots )
107 r->stream_timing = realloc( r->stream_timing, r->st_slots *
108 sizeof(*r->stream_timing) );
109 st = r->stream_timing + slot;
112 st->last = buf->renderOffset;
113 st->average = 30.*90.;
120 // update the average inter-packet time of the stream associated with 'buf'
121 // using a recursive low-pass filter with a 16 packet time constant.
123 static void update_ipt( hb_reader_t *r, const hb_buffer_t *buf )
125 stream_timing_t *st = id_to_st( r, buf );
126 double dt = buf->renderOffset - st->last;
127 st->average += ( dt - st->average ) * (1./16.);
128 st->last = buf->renderOffset;
131 // use the per-stream state associated with 'buf' to compute a new scr_offset
132 // such that 'buf' will follow the previous packet of this stream separated
133 // by the average packet time of the stream.
135 static void new_scr_offset( hb_reader_t *r, const hb_buffer_t *buf )
137 stream_timing_t *st = id_to_st( r, buf );
138 int64_t nxt = st->last + st->average - r->scr_offset;
139 r->scr_offset = buf->renderOffset - nxt;
140 r->scr_changes = r->demux.scr_changes;
141 st->last = buf->renderOffset;
144 /***********************************************************************
146 ***********************************************************************
148 **********************************************************************/
149 static void ReaderFunc( void * _r )
151 hb_reader_t * r = _r;
157 int chapter_end = r->job->chapter_end;
159 if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
161 if ( !( r->stream = hb_stream_open( r->title->dvd, r->title ) ) )
170 * XXX this code is a temporary hack that should go away if/when
171 * chapter merging goes away in libhb/dvd.c
172 * map the start and end chapter numbers to on-media chapter
173 * numbers since chapter merging could cause the handbrake numbers
174 * to diverge from the media numbers and, if our chapter_end is after
175 * a media chapter that got merged, we'll stop ripping too early.
177 int start = r->job->chapter_start;
178 hb_chapter_t * chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
180 chapter_end = chap->index;
183 chap = hb_list_item( r->title->list_chapter, start - 1 );
186 /* end chapter mapping XXX */
188 if( !hb_dvd_start( r->dvd, r->title->index, start ) )
190 hb_dvd_close( &r->dvd );
195 list = hb_list_init();
196 hb_buffer_t *ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
198 while( !*r->die && !r->job->done )
201 chapter = hb_dvd_chapter( r->dvd );
207 hb_log( "reader: end of the title reached" );
210 if( chapter > chapter_end )
212 hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
213 r->job->chapter_end, chapter_end, chapter );
219 if( !hb_dvd_read( r->dvd, ps ) )
226 if ( !hb_stream_read( r->stream, ps ) )
232 if( r->job->indepth_scan )
235 * Need to update the progress during a subtitle scan
239 #define p state.param.working
241 state.state = HB_STATE_WORKING;
242 p.progress = (double)chapter / (double)r->job->chapter_end;
243 if( p.progress > 1.0 )
251 hb_set_state( r->job->h, &state );
254 if ( r->title->demuxer == HB_NULL_DEMUXER )
256 hb_demux_null( ps, list, &r->demux );
260 hb_demux_ps( ps, list, &r->demux );
263 while( ( buf = hb_list_item( list, 0 ) ) )
265 hb_list_rem( list, buf );
266 fifos = GetFifoForId( r->job, buf->id );
268 if ( ! r->saw_video )
270 /* The first video packet defines 'time zero' so discard
271 data until we get a video packet with a PTS */
272 if ( buf->id == r->title->video_id && buf->start != -1 )
275 r->scr_changes = r->demux.scr_changes;
276 new_scr_offset( r, buf );
277 hb_log( "reader: first SCR %llu scr_offset %llu",
278 r->demux.last_scr, r->scr_offset );
287 if ( buf->start != -1 )
289 if ( r->scr_changes == r->demux.scr_changes )
291 // This packet is referenced to the same SCR as the last.
292 // Update the average inter-packet time for this stream.
293 update_ipt( r, buf );
297 // This is the first audio or video packet after an SCR
298 // change. Compute a new scr offset that would make this
299 // packet follow the last of this stream with the correct
301 new_scr_offset( r, buf );
303 // adjust timestamps to remove System Clock Reference offsets.
304 buf->start -= r->scr_offset;
305 buf->renderOffset -= r->scr_offset;
308 buf->sequence = r->sequence++;
309 /* if there are mutiple output fifos, send a copy of the
310 * buffer down all but the first (we have to not ship the
311 * original buffer or we'll race with the thread that's
312 * consuming the buffer & inject garbage into the data stream). */
313 for( n = 1; fifos[n] != NULL; n++)
315 hb_buffer_t *buf_copy = hb_buffer_init( buf->size );
316 hb_buffer_copy_settings( buf_copy, buf );
317 memcpy( buf_copy->data, buf->data, buf->size );
318 push_buf( r, fifos[n], buf_copy );
320 push_buf( r, fifos[0], buf );
324 hb_buffer_close( &buf );
329 /* send empty buffers upstream to video & audio decoders to signal we're done */
330 push_buf( r, r->job->fifo_mpeg2, hb_buffer_init(0) );
334 ( audio = hb_list_item( r->job->title->list_audio, n ) ) != NULL;
337 push_buf( r, audio->priv.fifo_in, hb_buffer_init(0) );
340 hb_list_empty( &list );
341 hb_buffer_close( &ps );
344 hb_dvd_stop( r->dvd );
345 hb_dvd_close( &r->dvd );
349 hb_stream_close(&r->stream);
352 if ( r->stream_timing )
354 free( r->stream_timing );
357 hb_log( "reader: done. %d scr changes", r->demux.scr_changes );
363 /***********************************************************************
365 ***********************************************************************
367 **********************************************************************/
368 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
370 hb_title_t * title = job->title;
372 hb_subtitle_t * subtitle;
374 static hb_fifo_t * fifos[8];
376 memset(fifos, 0, sizeof(fifos));
378 if( id == title->video_id )
380 if( job->indepth_scan )
383 * Ditch the video here during the indepth scan until
384 * we can improve the MPEG2 decode performance.
390 fifos[0] = job->fifo_mpeg2;
395 if( job->indepth_scan ) {
397 * Count the occurances of the subtitles, don't actually
398 * return any to encode unless we are looking fro forced
399 * subtitles in which case we need to look in the sub picture
400 * to see if it has the forced flag enabled.
402 for (i=0; i < hb_list_count(title->list_subtitle); i++) {
403 subtitle = hb_list_item( title->list_subtitle, i);
404 if (id == subtitle->id) {
409 if( job->subtitle_force )
412 fifos[0] = subtitle->fifo_in;
419 if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
422 fifos[0] = subtitle->fifo_in;
426 if( !job->indepth_scan )
429 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
431 audio = hb_list_item( title->list_audio, i );
432 if( id == audio->id )
434 fifos[n++] = audio->priv.fifo_in;