OSDN Git Service

- support blu-ray, avchd & dvb x264
[handbrake-jp/handbrake-jp-git.git] / libhb / reader.c
1 /* $Id: reader.c,v 1.21 2005/11/25 15:05:25 titer Exp $
2
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. */
6
7 #include "hb.h"
8
9 typedef struct
10 {
11     hb_job_t     * job;
12     hb_title_t   * title;
13     volatile int * die;
14
15     hb_dvd_t     * dvd;
16     hb_buffer_t  * ps;
17     hb_stream_t  * stream;
18
19     hb_psdemux_t   demux;
20     uint           sequence;
21     int            saw_video;
22 } hb_reader_t;
23
24 /***********************************************************************
25  * Local prototypes
26  **********************************************************************/
27 static void        ReaderFunc( void * );
28 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id );
29
30 /***********************************************************************
31  * hb_reader_init
32  ***********************************************************************
33  *
34  **********************************************************************/
35 hb_thread_t * hb_reader_init( hb_job_t * job )
36 {
37     hb_reader_t * r;
38
39     r = calloc( sizeof( hb_reader_t ), 1 );
40
41     r->job   = job;
42     r->title = job->title;
43     r->die   = job->die;
44     r->sequence = 0;
45
46     return hb_thread_init( "reader", ReaderFunc, r,
47                            HB_NORMAL_PRIORITY );
48 }
49
50 static void push_buf( hb_reader_t *r, hb_fifo_t *fifo, hb_buffer_t *buf )
51 {
52     while( !*r->die && !r->job->done && hb_fifo_is_full( fifo ) )
53     {
54         /*
55          * Loop until the incoming fifo is reaqdy to receive
56          * this buffer.
57          */
58         hb_snooze( 50 );
59     }
60     hb_fifo_push( fifo, buf );
61 }
62
63 /***********************************************************************
64  * ReaderFunc
65  ***********************************************************************
66  *
67  **********************************************************************/
68 static void ReaderFunc( void * _r )
69 {
70     hb_reader_t  * r = _r;
71     hb_fifo_t   ** fifos;
72     hb_buffer_t  * buf;
73     hb_list_t    * list;
74     int            n;
75     int            chapter = -1;
76     int            chapter_end = r->job->chapter_end;
77
78     if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
79     {
80         if ( !( r->stream = hb_stream_open( r->title->dvd, r->title ) ) )
81         {
82           return;
83         }
84     }
85
86     if (r->dvd)
87     {
88       /*
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.
95        */
96       int start = r->job->chapter_start;
97       hb_chapter_t * chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
98
99       chapter_end = chap->index;
100       if (start > 1)
101       {
102          chap = hb_list_item( r->title->list_chapter, start - 1 );
103          start = chap->index;
104       }
105       /* end chapter mapping XXX */
106
107       if( !hb_dvd_start( r->dvd, r->title->index, start ) )
108       {
109           hb_dvd_close( &r->dvd );
110           return;
111       }
112     }
113
114     list  = hb_list_init();
115     r->ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
116
117     while( !*r->die && !r->job->done )
118     {
119         if (r->dvd)
120           chapter = hb_dvd_chapter( r->dvd );
121         else if (r->stream)
122           chapter = 1;
123
124         if( chapter < 0 )
125         {
126             hb_log( "reader: end of the title reached" );
127             break;
128         }
129         if( chapter > chapter_end )
130         {
131             hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
132                     r->job->chapter_end, chapter_end, chapter );
133             break;
134         }
135
136         if (r->dvd)
137         {
138           if( !hb_dvd_read( r->dvd, r->ps ) )
139           {
140               break;
141           }
142         }
143         else if (r->stream)
144         {
145           if ( !hb_stream_read( r->stream, r->ps ) )
146           {
147             break;
148           }
149         }
150
151         if( r->job->indepth_scan )
152         {
153             /*
154              * Need to update the progress during a subtitle scan
155              */
156             hb_state_t state;
157
158 #define p state.param.working
159
160             state.state = HB_STATE_WORKING;
161             p.progress = (float)chapter / (float)r->job->chapter_end;
162             if( p.progress > 1.0 )
163             {
164                 p.progress = 1.0;
165             }
166             p.rate_avg = 0.0;
167             p.hours    = -1;
168             p.minutes  = -1;
169             p.seconds  = -1;
170             hb_set_state( r->job->h, &state );
171         }
172
173         if ( r->title->demuxer == HB_NULL_DEMUXER )
174         {
175             hb_demux_null( r->ps, list, &r->demux );
176         }
177         else
178         {
179             hb_demux_ps( r->ps, list, &r->demux );
180         }
181
182         while( ( buf = hb_list_item( list, 0 ) ) )
183         {
184             hb_list_rem( list, buf );
185             fifos = GetFifoForId( r->job, buf->id );
186
187             if ( ! r->saw_video )
188             {
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 )
192                 {
193                     r->saw_video = 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 );
197                 }
198                 else
199                 {
200                     fifos = NULL;
201                 }
202             }
203             if( fifos )
204             {
205                 if ( buf->start != -1 )
206                 {
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;
213                 }
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++)
220                 {
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 );
225                 }
226                 push_buf( r, fifos[0], buf );
227             }
228             else
229             {
230                 hb_buffer_close( &buf );
231             }
232         }
233     }
234
235     /* send an empty buffer upstream to signal we're done */
236     hb_fifo_push( r->job->fifo_mpeg2, hb_buffer_init(0) );
237
238     hb_list_empty( &list );
239     hb_buffer_close( &r->ps );
240     if (r->dvd)
241     {
242       hb_dvd_stop( r->dvd );
243       hb_dvd_close( &r->dvd );
244     }
245     else if (r->stream)
246     {
247       hb_stream_close(&r->stream);
248     }
249
250     hb_log( "reader: done. %d scr changes", r->demux.scr_changes );
251
252     free( r );
253     _r = NULL;
254 }
255
256 /***********************************************************************
257  * GetFifoForId
258  ***********************************************************************
259  *
260  **********************************************************************/
261 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
262 {
263     hb_title_t    * title = job->title;
264     hb_audio_t    * audio;
265     hb_subtitle_t * subtitle;
266     int             i, n;
267     static hb_fifo_t * fifos[8];
268
269     memset(fifos, 0, sizeof(fifos));
270
271     if( id == title->video_id )
272     {
273         if( job->indepth_scan )
274         {
275             /*
276              * Ditch the video here during the indepth scan until
277              * we can improve the MPEG2 decode performance.
278              */
279             return NULL;
280         }
281         else
282         {
283             fifos[0] = job->fifo_mpeg2;
284             return fifos;
285         }
286     }
287
288     if( job->indepth_scan ) {
289         /*
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.
294          */
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) {
298                 /*
299                  * A hit, count it.
300                  */
301                 subtitle->hits++;
302                 if( job->subtitle_force )
303                 {
304
305                     fifos[0] = subtitle->fifo_in;
306                     return fifos;
307                 }
308                 break;
309             }
310         }
311     } else {
312         if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
313             id == subtitle->id )
314         {
315             fifos[0] = subtitle->fifo_in;
316             return fifos;
317         }
318     }
319     if( !job->indepth_scan )
320     {
321         n = 0;
322         for( i = 0; i < hb_list_count( title->list_audio ); i++ )
323         {
324             audio = hb_list_item( title->list_audio, i );
325             if( id == audio->id )
326             {
327                 fifos[n++] = audio->priv.fifo_in;
328             }
329         }
330
331         if( n != 0 )
332         {
333             return fifos;
334         }
335     }
336
337     return NULL;
338 }
339