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 static void push_buf( hb_reader_t *r, hb_fifo_t *fifo, hb_buffer_t *buf )
52 while( !*r->die && !r->job->done && hb_fifo_is_full( fifo ) )
55 * Loop until the incoming fifo is reaqdy to receive
60 hb_fifo_push( fifo, buf );
63 /***********************************************************************
65 ***********************************************************************
67 **********************************************************************/
68 static void ReaderFunc( void * _r )
76 int chapter_end = r->job->chapter_end;
78 if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
80 if ( !( r->stream = hb_stream_open( r->title->dvd, r->title ) ) )
89 * XXX this code is a temporary hack that should go away if/when
90 * chapter merging goes away in libhb/dvd.c
91 * map the start and end chapter numbers to on-media chapter
92 * numbers since chapter merging could cause the handbrake numbers
93 * to diverge from the media numbers and, if our chapter_end is after
94 * a media chapter that got merged, we'll stop ripping too early.
96 int start = r->job->chapter_start;
97 hb_chapter_t * chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
99 chapter_end = chap->index;
102 chap = hb_list_item( r->title->list_chapter, start - 1 );
105 /* end chapter mapping XXX */
107 if( !hb_dvd_start( r->dvd, r->title->index, start ) )
109 hb_dvd_close( &r->dvd );
114 list = hb_list_init();
115 r->ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
117 while( !*r->die && !r->job->done )
120 chapter = hb_dvd_chapter( r->dvd );
126 hb_log( "reader: end of the title reached" );
129 if( chapter > chapter_end )
131 hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
132 r->job->chapter_end, chapter_end, chapter );
138 if( !hb_dvd_read( r->dvd, r->ps ) )
145 if ( !hb_stream_read( r->stream, r->ps ) )
151 if( r->job->indepth_scan )
154 * Need to update the progress during a subtitle scan
158 #define p state.param.working
160 state.state = HB_STATE_WORKING;
161 p.progress = (float)chapter / (float)r->job->chapter_end;
162 if( p.progress > 1.0 )
170 hb_set_state( r->job->h, &state );
173 if ( r->title->demuxer == HB_NULL_DEMUXER )
175 hb_demux_null( r->ps, list, &r->demux );
179 hb_demux_ps( r->ps, list, &r->demux );
182 while( ( buf = hb_list_item( list, 0 ) ) )
184 hb_list_rem( list, buf );
185 fifos = GetFifoForId( r->job, buf->id );
187 if ( ! r->saw_video )
189 /* The first video packet defines 'time zero' so discard
190 data until we get a video packet with a PTS */
191 if ( buf->id == r->title->video_id && buf->start != -1 )
194 r->demux.scr_offset = buf->renderOffset;
195 hb_log( "reader: first SCR %llu scr_offset %llu",
196 r->demux.last_scr, r->demux.scr_offset );
205 if ( buf->start != -1 )
207 /* this packet has a PTS - correct it for the initial
208 video time offset & any timing discontinuities so that
209 everything after this sees a continuous clock with 0
210 being the time of the first video packet. */
211 buf->start -= r->demux.scr_offset;
212 buf->renderOffset -= r->demux.scr_offset;
214 buf->sequence = r->sequence++;
215 /* if there are mutiple output fifos, send a copy of the
216 * buffer down all but the first (we have to not ship the
217 * original buffer or we'll race with the thread that's
218 * consuming the buffer & inject garbage into the data stream). */
219 for( n = 1; fifos[n] != NULL; n++)
221 hb_buffer_t *buf_copy = hb_buffer_init( buf->size );
222 hb_buffer_copy_settings( buf_copy, buf );
223 memcpy( buf_copy->data, buf->data, buf->size );
224 push_buf( r, fifos[n], buf_copy );
226 push_buf( r, fifos[0], buf );
230 hb_buffer_close( &buf );
235 /* send an empty buffer upstream to signal we're done */
236 hb_fifo_push( r->job->fifo_mpeg2, hb_buffer_init(0) );
238 hb_list_empty( &list );
239 hb_buffer_close( &r->ps );
242 hb_dvd_stop( r->dvd );
243 hb_dvd_close( &r->dvd );
247 hb_stream_close(&r->stream);
250 hb_log( "reader: done. %d scr changes", r->demux.scr_changes );
256 /***********************************************************************
258 ***********************************************************************
260 **********************************************************************/
261 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
263 hb_title_t * title = job->title;
265 hb_subtitle_t * subtitle;
267 static hb_fifo_t * fifos[8];
269 memset(fifos, 0, sizeof(fifos));
271 if( id == title->video_id )
273 if( job->indepth_scan )
276 * Ditch the video here during the indepth scan until
277 * we can improve the MPEG2 decode performance.
283 fifos[0] = job->fifo_mpeg2;
288 if( job->indepth_scan ) {
290 * Count the occurances of the subtitles, don't actually
291 * return any to encode unless we are looking fro forced
292 * subtitles in which case we need to look in the sub picture
293 * to see if it has the forced flag enabled.
295 for (i=0; i < hb_list_count(title->list_subtitle); i++) {
296 subtitle = hb_list_item( title->list_subtitle, i);
297 if (id == subtitle->id) {
302 if( job->subtitle_force )
305 fifos[0] = subtitle->fifo_in;
312 if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
315 fifos[0] = subtitle->fifo_in;
319 if( !job->indepth_scan )
322 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
324 audio = hb_list_item( title->list_audio, i );
325 if( id == audio->id )
327 fifos[n++] = audio->priv.fifo_in;