Use weightb=0 for turbo 1st pass x264 options. This prevents frame count discrepancie...
[HandBrake.git] / libhb / reader.c
blob1d89c4f70fecb3a04785315ff14f77d0c01f8e7d
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. */
7 #include "hb.h"
9 typedef struct
11 hb_job_t * job;
12 hb_title_t * title;
13 volatile int * die;
15 hb_dvd_t * dvd;
16 hb_buffer_t * ps;
17 hb_stream_t * stream;
19 uint sequence;
21 } hb_reader_t;
23 /***********************************************************************
24 * Local prototypes
25 **********************************************************************/
26 static void ReaderFunc( void * );
27 static hb_fifo_t * GetFifoForId( hb_job_t * job, int id );
29 /***********************************************************************
30 * hb_reader_init
31 ***********************************************************************
33 **********************************************************************/
34 hb_thread_t * hb_reader_init( hb_job_t * job )
36 hb_reader_t * r;
38 r = calloc( sizeof( hb_reader_t ), 1 );
40 r->job = job;
41 r->title = job->title;
42 r->die = job->die;
43 r->sequence = 0;
45 return hb_thread_init( "reader", ReaderFunc, r,
46 HB_NORMAL_PRIORITY );
49 /***********************************************************************
50 * ReaderFunc
51 ***********************************************************************
53 **********************************************************************/
54 static void ReaderFunc( void * _r )
56 hb_reader_t * r = _r;
57 hb_fifo_t * fifo;
58 hb_buffer_t * buf;
59 hb_list_t * list;
60 int chapter = -1;
61 int chapter_end = r->job->chapter_end;
63 if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
65 if ( !(r->stream = hb_stream_open(r->title->dvd) ) )
67 return;
71 if (r->dvd)
74 * XXX this code is a temporary hack that should go away if/when
75 * chapter merging goes away in libhb/dvd.c
76 * map the start and end chapter numbers to on-media chapter
77 * numbers since chapter merging could cause the handbrake numbers
78 * to diverge from the media numbers and, if our chapter_end is after
79 * a media chapter that got merged, we'll stop ripping too early.
81 int start = r->job->chapter_start;
82 hb_chapter_t * chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
84 chapter_end = chap->index;
85 if (start > 1)
87 chap = hb_list_item( r->title->list_chapter, start - 1 );
88 start = chap->index;
90 /* end chapter mapping XXX */
92 if( !hb_dvd_start( r->dvd, r->title->index, start ) )
94 hb_dvd_close( &r->dvd );
95 return;
99 list = hb_list_init();
100 r->ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
102 while( !*r->die && !r->job->done )
104 if (r->dvd)
105 chapter = hb_dvd_chapter( r->dvd );
106 else if (r->stream)
107 chapter = 1;
109 if( chapter < 0 )
111 hb_log( "reader: end of the title reached" );
112 break;
114 if( chapter > chapter_end )
116 hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
117 r->job->chapter_end, chapter_end, chapter );
118 break;
121 if (r->dvd)
123 if( !hb_dvd_read( r->dvd, r->ps ) )
125 break;
128 else if (r->stream)
130 if ( !hb_stream_read( r->stream, r->ps ) )
132 break;
136 if( r->job->indepth_scan )
139 * Need to update the progress during a subtitle scan
141 hb_state_t state;
143 #define p state.param.working
145 state.state = HB_STATE_WORKING;
146 p.progress = (float)chapter / (float)r->job->chapter_end;
147 if( p.progress > 1.0 )
149 p.progress = 1.0;
151 p.rate_avg = 0.0;
152 p.hours = -1;
153 p.minutes = -1;
154 p.seconds = -1;
155 hb_set_state( r->job->h, &state );
158 hb_demux_ps( r->ps, list );
160 while( ( buf = hb_list_item( list, 0 ) ) )
162 hb_list_rem( list, buf );
163 fifo = GetFifoForId( r->job, buf->id );
164 if( fifo )
166 while( !*r->die && !r->job->done &&
167 hb_fifo_is_full( fifo ) )
169 hb_snooze( 50 );
171 buf->sequence = r->sequence++;
172 hb_fifo_push( fifo, buf );
174 else
176 hb_buffer_close( &buf );
181 hb_list_empty( &list );
182 hb_buffer_close( &r->ps );
183 if (r->dvd)
185 hb_dvd_stop( r->dvd );
186 hb_dvd_close( &r->dvd );
188 else if (r->stream)
190 hb_stream_close(&r->stream);
193 free( r );
194 _r = NULL;
196 hb_log( "reader: done" );
199 /***********************************************************************
200 * GetFifoForId
201 ***********************************************************************
203 **********************************************************************/
204 static hb_fifo_t * GetFifoForId( hb_job_t * job, int id )
206 hb_title_t * title = job->title;
207 hb_audio_t * audio;
208 hb_subtitle_t * subtitle;
209 int i;
211 if( id == 0xE0 )
213 if( job->indepth_scan )
216 * Ditch the video here during the indepth scan until
217 * we can improve the MPEG2 decode performance.
219 return NULL;
221 else
223 return job->fifo_mpeg2;
227 if( job->indepth_scan ) {
229 * Count the occurances of the subtitles, don't actually
230 * return any to encode unless we are looking fro forced
231 * subtitles in which case we need to look in the sub picture
232 * to see if it has the forced flag enabled.
234 for (i=0; i < hb_list_count(title->list_subtitle); i++) {
235 subtitle = hb_list_item( title->list_subtitle, i);
236 if (id == subtitle->id) {
238 * A hit, count it.
240 subtitle->hits++;
241 if( job->subtitle_force )
243 return subtitle->fifo_in;
245 break;
248 } else {
249 if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
250 id == subtitle->id )
252 return subtitle->fifo_in;
255 if( !job->indepth_scan )
257 for( i = 0; i < hb_list_count( title->list_audio ); i++ )
259 audio = hb_list_item( title->list_audio, i );
260 if( id == audio->id )
262 return audio->fifo_in;
267 return NULL;