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.m0k.org/>.
5 It may be used under the terms of the GNU General Public License. */
27 /***********************************************************************
29 **********************************************************************/
30 static void ReaderFunc( void * );
31 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id );
33 /***********************************************************************
35 ***********************************************************************
37 **********************************************************************/
38 hb_thread_t * hb_reader_init( hb_job_t * job )
42 r = calloc( sizeof( hb_reader_t ), 1 );
45 r->title = job->title;
49 return hb_thread_init( "reader", ReaderFunc, r,
53 /***********************************************************************
55 ***********************************************************************
57 **********************************************************************/
58 static void ReaderFunc( void * _r )
63 hb_buffer_t * buf_old;
67 int chapter_end = r->job->chapter_end;
69 if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
71 if ( !(r->stream = hb_stream_open(r->title->dvd) ) )
80 * XXX this code is a temporary hack that should go away if/when
81 * chapter merging goes away in libhb/dvd.c
82 * map the start and end chapter numbers to on-media chapter
83 * numbers since chapter merging could cause the handbrake numbers
84 * to diverge from the media numbers and, if our chapter_end is after
85 * a media chapter that got merged, we'll stop ripping too early.
87 int start = r->job->chapter_start;
88 hb_chapter_t * chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
90 chapter_end = chap->index;
93 chap = hb_list_item( r->title->list_chapter, start - 1 );
96 /* end chapter mapping XXX */
98 if( !hb_dvd_start( r->dvd, r->title->index, start ) )
100 hb_dvd_close( &r->dvd );
105 list = hb_list_init();
106 r->ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
108 while( !*r->die && !r->job->done )
111 chapter = hb_dvd_chapter( r->dvd );
117 hb_log( "reader: end of the title reached" );
120 if( chapter > chapter_end )
122 hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
123 r->job->chapter_end, chapter_end, chapter );
129 if( !hb_dvd_read( r->dvd, r->ps ) )
136 if ( !hb_stream_read( r->stream, r->ps ) )
142 if( r->job->indepth_scan )
145 * Need to update the progress during a subtitle scan
149 #define p state.param.working
151 state.state = HB_STATE_WORKING;
152 p.progress = (float)chapter / (float)r->job->chapter_end;
153 if( p.progress > 1.0 )
161 hb_set_state( r->job->h, &state );
164 hb_demux_ps( r->ps, list );
166 while( ( buf = hb_list_item( list, 0 ) ) )
168 hb_list_rem( list, buf );
169 fifos = GetFifoForId( r->job, buf->id );
171 if ( ! r->saw_video )
173 /* The first video packet defines 'time zero' so discard
174 data until we get a video packet with a PTS */
175 if ( buf->id == 0xE0 && buf->start != -1 )
178 r->scr_offset = buf->start;
179 r->last_scr = buf->stop;
180 hb_log( "reader: first SCR %llu scr_offset %llu",
181 r->last_scr, r->scr_offset );
191 * This section of code implements the timing model of
192 * the "Standard Target Decoder" (STD) of the MPEG2 standard
193 * (specified in ISO 13818-1 sections 2.4.2, 2.5.2 & Annex D).
194 * The STD removes and corrects for clock discontinuities so
195 * that the time stamps on the video, audio & other media
196 * streams can be used for cross-media synchronization. To do
197 * this the STD has its own timestamp value, the System Clock
198 * Reference or SCR, in the PACK header. Clock discontinuities
199 * are detected using the SCR & and the adjustment needed
200 * to correct post-discontinuity timestamps to be contiguous
201 * with pre-discontinuity timestamps is computed from pre- and
202 * post-discontinuity values of the SCR. Then this adjustment
203 * is applied to every media timestamp (PTS).
205 * hb_demux_ps left the SCR for this pack in buf->stop.
206 * ISO 13818-1 says there must be an SCR at least every 700ms
207 * (100ms for Transport Streams) so if the difference between
208 * this SCR & the previous is >700ms it's a discontinuity.
209 * If the difference is negative it's non-physical (time doesn't
210 * go backward) and must also be a discontinuity. When we find a
211 * discontinuity we adjust the scr_offset so that the SCR of the
212 * new packet lines up with that of the previous packet.
214 int64_t scr_delta = buf->stop - r->last_scr;
215 if ( scr_delta > 67500 || scr_delta < -900 )
218 r->scr_offset += scr_delta - 1;
220 r->last_scr = buf->stop;
224 * The last section detected discontinuites and computed the
225 * appropriate correction to remove them. The next couple of
226 * lines apply the correction to the media timestamps so the
227 * code downstream of us sees only timestamps relative to the
228 * same, continuous clock with time zero on that clock being
229 * the time of the first video packet.
231 if ( buf->start != -1 )
233 /* this packet has a PTS - correct it for the initial
234 video time offset & any timing discontinuities. */
235 buf->start -= r->scr_offset;
237 buf->sequence = r->sequence++;
238 for( n = 0; fifos[n] != NULL; n++)
243 * Replace the buffer with a new copy of itself for when
244 * it is being sent down multiple fifos.
247 buf = hb_buffer_init(buf_old->size);
248 memcpy( buf->data, buf_old->data, buf->size );
249 hb_buffer_copy_settings( buf, buf_old );
252 while( !*r->die && !r->job->done &&
253 hb_fifo_is_full( fifos[n] ) )
256 * Loop until the incoming fifo is reaqdy to receive
262 hb_fifo_push( fifos[n], buf );
267 hb_buffer_close( &buf );
272 hb_list_empty( &list );
273 hb_buffer_close( &r->ps );
276 hb_dvd_stop( r->dvd );
277 hb_dvd_close( &r->dvd );
281 hb_stream_close(&r->stream);
284 hb_log( "reader: done. %d scr changes", r->scr_changes );
290 /***********************************************************************
292 ***********************************************************************
294 **********************************************************************/
295 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
297 hb_title_t * title = job->title;
299 hb_subtitle_t * subtitle;
301 static hb_fifo_t * fifos[8];
303 memset(fifos, 0, sizeof(fifos));
307 if( job->indepth_scan )
310 * Ditch the video here during the indepth scan until
311 * we can improve the MPEG2 decode performance.
317 fifos[0] = job->fifo_mpeg2;
322 if( job->indepth_scan ) {
324 * Count the occurances of the subtitles, don't actually
325 * return any to encode unless we are looking fro forced
326 * subtitles in which case we need to look in the sub picture
327 * to see if it has the forced flag enabled.
329 for (i=0; i < hb_list_count(title->list_subtitle); i++) {
330 subtitle = hb_list_item( title->list_subtitle, i);
331 if (id == subtitle->id) {
336 if( job->subtitle_force )
339 fifos[0] = subtitle->fifo_in;
346 if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
349 fifos[0] = subtitle->fifo_in;
353 if( !job->indepth_scan )
356 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
358 audio = hb_list_item( title->list_audio, i );
359 if( id == audio->id )
361 fifos[n++] = audio->fifo_in;