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. */
23 /***********************************************************************
25 **********************************************************************/
26 static void ReaderFunc( void * );
27 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id );
29 /***********************************************************************
31 ***********************************************************************
33 **********************************************************************/
34 hb_thread_t * hb_reader_init( hb_job_t * job )
38 r = calloc( sizeof( hb_reader_t ), 1 );
41 r->title = job->title;
45 return hb_thread_init( "reader", ReaderFunc, r,
49 /***********************************************************************
51 ***********************************************************************
53 **********************************************************************/
54 static void ReaderFunc( void * _r )
59 hb_buffer_t * buf_old;
63 int chapter_end = r->job->chapter_end;
65 if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
67 if ( !(r->stream = hb_stream_open(r->title->dvd) ) )
76 * XXX this code is a temporary hack that should go away if/when
77 * chapter merging goes away in libhb/dvd.c
78 * map the start and end chapter numbers to on-media chapter
79 * numbers since chapter merging could cause the handbrake numbers
80 * to diverge from the media numbers and, if our chapter_end is after
81 * a media chapter that got merged, we'll stop ripping too early.
83 int start = r->job->chapter_start;
84 hb_chapter_t * chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
86 chapter_end = chap->index;
89 chap = hb_list_item( r->title->list_chapter, start - 1 );
92 /* end chapter mapping XXX */
94 if( !hb_dvd_start( r->dvd, r->title->index, start ) )
96 hb_dvd_close( &r->dvd );
101 list = hb_list_init();
102 r->ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
104 while( !*r->die && !r->job->done )
107 chapter = hb_dvd_chapter( r->dvd );
113 hb_log( "reader: end of the title reached" );
116 if( chapter > chapter_end )
118 hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
119 r->job->chapter_end, chapter_end, chapter );
125 if( !hb_dvd_read( r->dvd, r->ps ) )
132 if ( !hb_stream_read( r->stream, r->ps ) )
138 if( r->job->indepth_scan )
141 * Need to update the progress during a subtitle scan
145 #define p state.param.working
147 state.state = HB_STATE_WORKING;
148 p.progress = (float)chapter / (float)r->job->chapter_end;
149 if( p.progress > 1.0 )
157 hb_set_state( r->job->h, &state );
160 hb_demux_ps( r->ps, list );
162 while( ( buf = hb_list_item( list, 0 ) ) )
164 hb_list_rem( list, buf );
165 fifos = GetFifoForId( r->job, buf->id );
168 buf->sequence = r->sequence++;
169 for( n = 0; fifos[n] != NULL; n++)
174 * Replace the buffer with a new copy of itself for when
175 * it is being sent down multiple fifos.
178 buf = hb_buffer_init(buf_old->size);
179 memcpy( buf->data, buf_old->data, buf->size );
180 hb_buffer_copy_settings( buf, buf_old );
183 while( !*r->die && !r->job->done &&
184 hb_fifo_is_full( fifos[n] ) )
187 * Loop until the incoming fifo is reaqdy to receive
193 hb_fifo_push( fifos[n], buf );
198 hb_buffer_close( &buf );
203 hb_list_empty( &list );
204 hb_buffer_close( &r->ps );
207 hb_dvd_stop( r->dvd );
208 hb_dvd_close( &r->dvd );
212 hb_stream_close(&r->stream);
218 hb_log( "reader: done" );
221 /***********************************************************************
223 ***********************************************************************
225 **********************************************************************/
226 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
228 hb_title_t * title = job->title;
230 hb_subtitle_t * subtitle;
232 static hb_fifo_t * fifos[8];
234 memset(fifos, 0, sizeof(fifos));
238 if( job->indepth_scan )
241 * Ditch the video here during the indepth scan until
242 * we can improve the MPEG2 decode performance.
248 fifos[0] = job->fifo_mpeg2;
253 if( job->indepth_scan ) {
255 * Count the occurances of the subtitles, don't actually
256 * return any to encode unless we are looking fro forced
257 * subtitles in which case we need to look in the sub picture
258 * to see if it has the forced flag enabled.
260 for (i=0; i < hb_list_count(title->list_subtitle); i++) {
261 subtitle = hb_list_item( title->list_subtitle, i);
262 if (id == subtitle->id) {
267 if( job->subtitle_force )
270 fifos[0] = subtitle->fifo_in;
277 if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
280 fifos[0] = subtitle->fifo_in;
284 if( !job->indepth_scan )
287 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
289 audio = hb_list_item( title->list_audio, i );
290 if( id == audio->id )
292 fifos[n++] = audio->fifo_in;