OSDN Git Service

Move clock recovery code from reader to demuxmpeg so it sees all frames & not just...
[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 /***********************************************************************
51  * ReaderFunc
52  ***********************************************************************
53  *
54  **********************************************************************/
55 static void ReaderFunc( void * _r )
56 {
57     hb_reader_t  * r = _r;
58     hb_fifo_t   ** fifos;
59     hb_buffer_t  * buf;
60     hb_buffer_t  * buf_old;
61     hb_list_t    * list;
62     int            n;
63     int            chapter = -1;
64     int            chapter_end = r->job->chapter_end;
65
66     if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
67     {
68         if ( !( r->stream = hb_stream_open( r->title->dvd, 1 ) ) )
69         {
70           return;
71         }
72     }
73
74     if (r->dvd)
75     {
76       /*
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.
83        */
84       int start = r->job->chapter_start;
85       hb_chapter_t * chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
86
87       chapter_end = chap->index;
88       if (start > 1)
89       {
90          chap = hb_list_item( r->title->list_chapter, start - 1 );
91          start = chap->index;
92       }
93       /* end chapter mapping XXX */
94
95       if( !hb_dvd_start( r->dvd, r->title->index, start ) )
96       {
97           hb_dvd_close( &r->dvd );
98           return;
99       }
100     }
101
102     list  = hb_list_init();
103     r->ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
104
105     while( !*r->die && !r->job->done )
106     {
107         if (r->dvd)
108           chapter = hb_dvd_chapter( r->dvd );
109         else if (r->stream)
110           chapter = 1;
111
112         if( chapter < 0 )
113         {
114             hb_log( "reader: end of the title reached" );
115             break;
116         }
117         if( chapter > chapter_end )
118         {
119             hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
120                     r->job->chapter_end, chapter_end, chapter );
121             break;
122         }
123
124         if (r->dvd)
125         {
126           if( !hb_dvd_read( r->dvd, r->ps ) )
127           {
128               break;
129           }
130         }
131         else if (r->stream)
132         {
133           if ( !hb_stream_read( r->stream, r->ps ) )
134           {
135             break;
136           }
137         }
138
139         if( r->job->indepth_scan )
140         {
141             /*
142              * Need to update the progress during a subtitle scan
143              */
144             hb_state_t state;
145
146 #define p state.param.working
147
148             state.state = HB_STATE_WORKING;
149             p.progress = (float)chapter / (float)r->job->chapter_end;
150             if( p.progress > 1.0 )
151             {
152                 p.progress = 1.0;
153             }
154             p.rate_avg = 0.0;
155             p.hours    = -1;
156             p.minutes  = -1;
157             p.seconds  = -1;
158             hb_set_state( r->job->h, &state );
159         }
160
161         hb_demux_ps( r->ps, list, &r->demux );
162
163         while( ( buf = hb_list_item( list, 0 ) ) )
164         {
165             hb_list_rem( list, buf );
166             fifos = GetFifoForId( r->job, buf->id );
167
168             if ( ! r->saw_video )
169             {
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 )
173                 {
174                     r->saw_video = 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 );
178                 }
179                 else
180                 {
181                     fifos = NULL;
182                 }
183             }
184             if( fifos )
185             {
186                 if ( buf->start != -1 )
187                 {
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;
193                 }
194                 buf->sequence = r->sequence++;
195                 for( n = 0; fifos[n] != NULL; n++)
196                 {
197                     if( n != 0 )
198                     {
199                         /*
200                          * Replace the buffer with a new copy of itself for when
201                          * it is being sent down multiple fifos.
202                          */
203                         buf_old = buf;
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 );
207                     }
208
209                     while( !*r->die && !r->job->done &&
210                            hb_fifo_is_full( fifos[n] ) )
211                     {
212                         /*
213                          * Loop until the incoming fifo is reaqdy to receive
214                          * this buffer.
215                          */
216                         hb_snooze( 50 );
217                     }
218
219                     hb_fifo_push( fifos[n], buf );
220                 }
221             }
222             else
223             {
224                 hb_buffer_close( &buf );
225             }
226         }
227     }
228
229     /* send an empty buffer upstream to signal we're done */
230     hb_fifo_push( r->job->fifo_mpeg2, hb_buffer_init(0) );
231
232     hb_list_empty( &list );
233     hb_buffer_close( &r->ps );
234     if (r->dvd)
235     {
236       hb_dvd_stop( r->dvd );
237       hb_dvd_close( &r->dvd );
238     }
239     else if (r->stream)
240     {
241       hb_stream_close(&r->stream);
242     }
243
244     hb_log( "reader: done. %d scr changes", r->demux.scr_changes );
245
246     free( r );
247     _r = NULL;
248 }
249
250 /***********************************************************************
251  * GetFifoForId
252  ***********************************************************************
253  *
254  **********************************************************************/
255 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
256 {
257     hb_title_t    * title = job->title;
258     hb_audio_t    * audio;
259     hb_subtitle_t * subtitle;
260     int             i, n;
261     static hb_fifo_t * fifos[8];
262
263     memset(fifos, 0, sizeof(fifos));
264
265     if( id == 0xE0 )
266     {
267         if( job->indepth_scan )
268         {
269             /*
270              * Ditch the video here during the indepth scan until
271              * we can improve the MPEG2 decode performance.
272              */
273             return NULL;
274         }
275         else
276         {
277             fifos[0] = job->fifo_mpeg2;
278             return fifos;
279         }
280     }
281
282     if( job->indepth_scan ) {
283         /*
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.
288          */
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) {
292                 /*
293                  * A hit, count it.
294                  */
295                 subtitle->hits++;
296                 if( job->subtitle_force )
297                 {
298
299                     fifos[0] = subtitle->fifo_in;
300                     return fifos;
301                 }
302                 break;
303             }
304         }
305     } else {
306         if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
307             id == subtitle->id )
308         {
309             fifos[0] = subtitle->fifo_in;
310             return fifos;
311         }
312     }
313     if( !job->indepth_scan )
314     {
315         n = 0;
316         for( i = 0; i < hb_list_count( title->list_audio ); i++ )
317         {
318             audio = hb_list_item( title->list_audio, i );
319             if( id == audio->id )
320             {
321                 fifos[n++] = audio->priv.fifo_in;
322             }
323         }
324
325         if( n != 0 )
326         {
327             return fifos;
328         }
329     }
330
331     return NULL;
332 }
333