OSDN Git Service

- Add mpeg2 "Standard Target Decoder" clock recovery to the low level mpeg stream...
[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.m0k.org/>.
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     uint           sequence;
20     int            saw_video;
21     int64_t        scr_offset;
22     int64_t        last_scr;
23     int            scr_changes;
24
25 } hb_reader_t;
26
27 /***********************************************************************
28  * Local prototypes
29  **********************************************************************/
30 static void        ReaderFunc( void * );
31 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id );
32
33 /***********************************************************************
34  * hb_reader_init
35  ***********************************************************************
36  *
37  **********************************************************************/
38 hb_thread_t * hb_reader_init( hb_job_t * job )
39 {
40     hb_reader_t * r;
41
42     r = calloc( sizeof( hb_reader_t ), 1 );
43
44     r->job   = job;
45     r->title = job->title;
46     r->die   = job->die;
47     r->sequence = 0;
48
49     return hb_thread_init( "reader", ReaderFunc, r,
50                            HB_NORMAL_PRIORITY );
51 }
52
53 /***********************************************************************
54  * ReaderFunc
55  ***********************************************************************
56  *
57  **********************************************************************/
58 static void ReaderFunc( void * _r )
59 {
60     hb_reader_t  * r = _r;
61     hb_fifo_t   ** fifos;
62     hb_buffer_t  * buf;
63     hb_buffer_t  * buf_old;
64     hb_list_t    * list;
65     int            n;
66     int            chapter = -1;
67     int            chapter_end = r->job->chapter_end;
68
69     if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
70     {
71         if ( !(r->stream = hb_stream_open(r->title->dvd) ) )
72         {
73           return;
74         }
75     }
76
77     if (r->dvd)
78     {
79       /*
80        * XXX this code is a temporary hack that should go away if/when
81        *     chapter merging goes away in libhb/dvd.c
82        * map the start and end chapter numbers to on-media chapter
83        * numbers since chapter merging could cause the handbrake numbers
84        * to diverge from the media numbers and, if our chapter_end is after
85        * a media chapter that got merged, we'll stop ripping too early.
86        */
87       int start = r->job->chapter_start;
88       hb_chapter_t * chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
89
90       chapter_end = chap->index;
91       if (start > 1)
92       {
93          chap = hb_list_item( r->title->list_chapter, start - 1 );
94          start = chap->index;
95       }
96       /* end chapter mapping XXX */
97
98       if( !hb_dvd_start( r->dvd, r->title->index, start ) )
99       {
100           hb_dvd_close( &r->dvd );
101           return;
102       }
103     }
104
105     list  = hb_list_init();
106     r->ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
107
108     while( !*r->die && !r->job->done )
109     {
110         if (r->dvd)
111           chapter = hb_dvd_chapter( r->dvd );
112         else if (r->stream)
113           chapter = 1;
114
115         if( chapter < 0 )
116         {
117             hb_log( "reader: end of the title reached" );
118             break;
119         }
120         if( chapter > chapter_end )
121         {
122             hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
123                     r->job->chapter_end, chapter_end, chapter );
124             break;
125         }
126
127         if (r->dvd)
128         {
129           if( !hb_dvd_read( r->dvd, r->ps ) )
130           {
131               break;
132           }
133         }
134         else if (r->stream)
135         {
136           if ( !hb_stream_read( r->stream, r->ps ) )
137           {
138             break;
139           }
140         }
141
142         if( r->job->indepth_scan )
143         {
144             /*
145              * Need to update the progress during a subtitle scan
146              */
147             hb_state_t state;
148
149 #define p state.param.working
150
151             state.state = HB_STATE_WORKING;
152             p.progress = (float)chapter / (float)r->job->chapter_end;
153             if( p.progress > 1.0 )
154             {
155                 p.progress = 1.0;
156             }
157             p.rate_avg = 0.0;
158             p.hours    = -1;
159             p.minutes  = -1;
160             p.seconds  = -1;
161             hb_set_state( r->job->h, &state );
162         }
163
164         hb_demux_ps( r->ps, list );
165
166         while( ( buf = hb_list_item( list, 0 ) ) )
167         {
168             hb_list_rem( list, buf );
169             fifos = GetFifoForId( r->job, buf->id );
170
171             if ( ! r->saw_video )
172             {
173                 /* The first video packet defines 'time zero' so discard
174                    data until we get a video packet with a PTS */
175                 if ( buf->id == 0xE0 && buf->start != -1 )
176                 {
177                     r->saw_video = 1;
178                     r->scr_offset = buf->start;
179                     r->last_scr = buf->stop;
180                     hb_log( "reader: first SCR %llu scr_offset %llu",
181                             r->last_scr, r->scr_offset );
182                 }
183                 else
184                 {
185                     fifos = NULL;
186                 }
187             }
188             if( fifos )
189             {
190                 /*
191                  * This section of code implements the timing model of
192                  * the "Standard Target Decoder" (STD) of the MPEG2 standard
193                  * (specified in ISO 13818-1 sections 2.4.2, 2.5.2 & Annex D).
194                  * The STD removes and corrects for clock discontinuities so
195                  * that the time stamps on the video, audio & other media
196                  * streams can be used for cross-media synchronization. To do
197                  * this the STD has its own timestamp value, the System Clock
198                  * Reference or SCR, in the PACK header. Clock discontinuities
199                  * are detected using the SCR & and the adjustment needed
200                  * to correct post-discontinuity timestamps to be contiguous
201                  * with pre-discontinuity timestamps is computed from pre- and
202                  * post-discontinuity values of the SCR. Then this adjustment
203                  * is applied to every media timestamp (PTS).
204                  *
205                  * hb_demux_ps left the SCR for this pack in buf->stop.
206                  * ISO 13818-1 says there must be an SCR at least every 700ms
207                  * (100ms for Transport Streams) so if the difference between
208                  * this SCR & the previous is >700ms it's a discontinuity.
209                  * If the difference is negative it's non-physical (time doesn't
210                  * go backward) and must also be a discontinuity. When we find a
211                  * discontinuity we adjust the scr_offset so that the SCR of the
212                  * new packet lines up with that of the previous packet.
213                  */
214                 int64_t scr_delta = buf->stop - r->last_scr;
215                 if ( scr_delta > 67500 || scr_delta < -900 )
216                 {
217                     ++r->scr_changes;
218                     r->scr_offset += scr_delta - 1;
219                 }
220                 r->last_scr = buf->stop;
221                 buf->stop = -1;
222
223                 /*
224                  * The last section detected discontinuites and computed the
225                  * appropriate correction to remove them. The next couple of
226                  * lines apply the correction to the media timestamps so the
227                  * code downstream of us sees only timestamps relative to the
228                  * same, continuous clock with time zero on that clock being
229                  * the time of the first video packet.
230                  */
231                 if ( buf->start != -1 )
232                 {
233                     /* this packet has a PTS - correct it for the initial
234                        video time offset & any timing discontinuities. */
235                     buf->start -= r->scr_offset;
236                 }
237                 buf->sequence = r->sequence++;
238                 for( n = 0; fifos[n] != NULL; n++)
239                 {
240                     if( n != 0 )
241                     {
242                         /*
243                          * Replace the buffer with a new copy of itself for when
244                          * it is being sent down multiple fifos.
245                          */
246                         buf_old = buf;
247                         buf = hb_buffer_init(buf_old->size);
248                         memcpy( buf->data, buf_old->data, buf->size );
249                         hb_buffer_copy_settings( buf, buf_old );
250                     }
251
252                     while( !*r->die && !r->job->done &&
253                            hb_fifo_is_full( fifos[n] ) )
254                     {
255                         /*
256                          * Loop until the incoming fifo is reaqdy to receive
257                          * this buffer.
258                          */
259                         hb_snooze( 50 );
260                     }
261
262                     hb_fifo_push( fifos[n], buf );
263                 }
264             }
265             else
266             {
267                 hb_buffer_close( &buf );
268             }
269         }
270     }
271
272     hb_list_empty( &list );
273     hb_buffer_close( &r->ps );
274     if (r->dvd)
275     {
276       hb_dvd_stop( r->dvd );
277       hb_dvd_close( &r->dvd );
278     }
279     else if (r->stream)
280     {
281       hb_stream_close(&r->stream);
282     }
283
284     hb_log( "reader: done. %d scr changes", r->scr_changes );
285
286     free( r );
287     _r = NULL;
288 }
289
290 /***********************************************************************
291  * GetFifoForId
292  ***********************************************************************
293  *
294  **********************************************************************/
295 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
296 {
297     hb_title_t    * title = job->title;
298     hb_audio_t    * audio;
299     hb_subtitle_t * subtitle;
300     int             i, n;
301     static hb_fifo_t * fifos[8];
302
303     memset(fifos, 0, sizeof(fifos));
304
305     if( id == 0xE0 )
306     {
307         if( job->indepth_scan )
308         {
309             /*
310              * Ditch the video here during the indepth scan until
311              * we can improve the MPEG2 decode performance.
312              */
313             return NULL;
314         }
315         else
316         {
317             fifos[0] = job->fifo_mpeg2;
318             return fifos;
319         }
320     }
321
322     if( job->indepth_scan ) {
323         /*
324          * Count the occurances of the subtitles, don't actually
325          * return any to encode unless we are looking fro forced
326          * subtitles in which case we need to look in the sub picture
327          * to see if it has the forced flag enabled.
328          */
329         for (i=0; i < hb_list_count(title->list_subtitle); i++) {
330             subtitle =  hb_list_item( title->list_subtitle, i);
331             if (id == subtitle->id) {
332                 /*
333                  * A hit, count it.
334                  */
335                 subtitle->hits++;
336                 if( job->subtitle_force )
337                 {
338
339                     fifos[0] = subtitle->fifo_in;
340                     return fifos;
341                 }
342                 break;
343             }
344         }
345     } else {
346         if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
347             id == subtitle->id )
348         {
349             fifos[0] = subtitle->fifo_in;
350             return fifos;
351         }
352     }
353     if( !job->indepth_scan )
354     {
355         n = 0;
356         for( i = 0; i < hb_list_count( title->list_audio ); i++ )
357         {
358             audio = hb_list_item( title->list_audio, i );
359             if( id == audio->id )
360             {
361                 fifos[n++] = audio->fifo_in;
362             }
363         }
364
365         if( n != 0 )
366         {
367             return fifos;
368         }
369     }
370
371     return NULL;
372 }
373