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. */
24 /***********************************************************************
26 **********************************************************************/
27 static void ReaderFunc( void * );
28 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id );
30 /***********************************************************************
32 ***********************************************************************
34 **********************************************************************/
35 hb_thread_t * hb_reader_init( hb_job_t * job )
39 r = calloc( sizeof( hb_reader_t ), 1 );
42 r->title = job->title;
46 return hb_thread_init( "reader", ReaderFunc, r,
50 /***********************************************************************
52 ***********************************************************************
54 **********************************************************************/
55 static void ReaderFunc( void * _r )
60 hb_buffer_t * buf_old;
64 int chapter_end = r->job->chapter_end;
66 if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
68 if ( !( r->stream = hb_stream_open( r->title->dvd, 1 ) ) )
77 * XXX this code is a temporary hack that should go away if/when
78 * chapter merging goes away in libhb/dvd.c
79 * map the start and end chapter numbers to on-media chapter
80 * numbers since chapter merging could cause the handbrake numbers
81 * to diverge from the media numbers and, if our chapter_end is after
82 * a media chapter that got merged, we'll stop ripping too early.
84 int start = r->job->chapter_start;
85 hb_chapter_t * chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
87 chapter_end = chap->index;
90 chap = hb_list_item( r->title->list_chapter, start - 1 );
93 /* end chapter mapping XXX */
95 if( !hb_dvd_start( r->dvd, r->title->index, start ) )
97 hb_dvd_close( &r->dvd );
102 list = hb_list_init();
103 r->ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
105 while( !*r->die && !r->job->done )
108 chapter = hb_dvd_chapter( r->dvd );
114 hb_log( "reader: end of the title reached" );
117 if( chapter > chapter_end )
119 hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
120 r->job->chapter_end, chapter_end, chapter );
126 if( !hb_dvd_read( r->dvd, r->ps ) )
133 if ( !hb_stream_read( r->stream, r->ps ) )
139 if( r->job->indepth_scan )
142 * Need to update the progress during a subtitle scan
146 #define p state.param.working
148 state.state = HB_STATE_WORKING;
149 p.progress = (float)chapter / (float)r->job->chapter_end;
150 if( p.progress > 1.0 )
158 hb_set_state( r->job->h, &state );
161 hb_demux_ps( r->ps, list, &r->demux );
163 while( ( buf = hb_list_item( list, 0 ) ) )
165 hb_list_rem( list, buf );
166 fifos = GetFifoForId( r->job, buf->id );
168 if ( ! r->saw_video )
170 /* The first video packet defines 'time zero' so discard
171 data until we get a video packet with a PTS */
172 if ( buf->id == 0xE0 && buf->start != -1 )
175 r->demux.scr_offset = buf->start;
176 hb_log( "reader: first SCR %llu scr_offset %llu",
177 r->demux.last_scr, r->demux.scr_offset );
186 if ( buf->start != -1 )
188 /* this packet has a PTS - correct it for the initial
189 video time offset & any timing discontinuities so that
190 everything after this sees a continuous clock with 0
191 being the time of the first video packet. */
192 buf->start -= r->demux.scr_offset;
194 buf->sequence = r->sequence++;
195 for( n = 0; fifos[n] != NULL; n++)
200 * Replace the buffer with a new copy of itself for when
201 * it is being sent down multiple fifos.
204 buf = hb_buffer_init(buf_old->size);
205 memcpy( buf->data, buf_old->data, buf->size );
206 hb_buffer_copy_settings( buf, buf_old );
209 while( !*r->die && !r->job->done &&
210 hb_fifo_is_full( fifos[n] ) )
213 * Loop until the incoming fifo is reaqdy to receive
219 hb_fifo_push( fifos[n], buf );
224 hb_buffer_close( &buf );
229 /* send an empty buffer upstream to signal we're done */
230 hb_fifo_push( r->job->fifo_mpeg2, hb_buffer_init(0) );
232 hb_list_empty( &list );
233 hb_buffer_close( &r->ps );
236 hb_dvd_stop( r->dvd );
237 hb_dvd_close( &r->dvd );
241 hb_stream_close(&r->stream);
244 hb_log( "reader: done. %d scr changes", r->demux.scr_changes );
250 /***********************************************************************
252 ***********************************************************************
254 **********************************************************************/
255 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
257 hb_title_t * title = job->title;
259 hb_subtitle_t * subtitle;
261 static hb_fifo_t * fifos[8];
263 memset(fifos, 0, sizeof(fifos));
267 if( job->indepth_scan )
270 * Ditch the video here during the indepth scan until
271 * we can improve the MPEG2 decode performance.
277 fifos[0] = job->fifo_mpeg2;
282 if( job->indepth_scan ) {
284 * Count the occurances of the subtitles, don't actually
285 * return any to encode unless we are looking fro forced
286 * subtitles in which case we need to look in the sub picture
287 * to see if it has the forced flag enabled.
289 for (i=0; i < hb_list_count(title->list_subtitle); i++) {
290 subtitle = hb_list_item( title->list_subtitle, i);
291 if (id == subtitle->id) {
296 if( job->subtitle_force )
299 fifos[0] = subtitle->fifo_in;
306 if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
309 fifos[0] = subtitle->fifo_in;
313 if( !job->indepth_scan )
316 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
318 audio = hb_list_item( title->list_audio, i );
319 if( id == audio->id )
321 fifos[n++] = audio->priv.fifo_in;