OSDN Git Service

fix subtitle indepth scan abort & hang.
[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     int64_t last;   // last timestamp seen on this stream
12     double average; // average time between packets
13     int id;         // stream id
14 } stream_timing_t;
15
16 typedef struct
17 {
18     hb_job_t     * job;
19     hb_title_t   * title;
20     volatile int * die;
21
22     hb_dvd_t     * dvd;
23     hb_stream_t  * stream;
24
25     stream_timing_t *stream_timing;
26     int64_t        scr_offset;
27     hb_psdemux_t   demux;
28     int            scr_changes;
29     uint           sequence;
30     int            saw_video;
31     int            st_slots;            // size (in slots) of stream_timing array
32 } hb_reader_t;
33
34 /***********************************************************************
35  * Local prototypes
36  **********************************************************************/
37 static void        ReaderFunc( void * );
38 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id );
39
40 /***********************************************************************
41  * hb_reader_init
42  ***********************************************************************
43  *
44  **********************************************************************/
45 hb_thread_t * hb_reader_init( hb_job_t * job )
46 {
47     hb_reader_t * r;
48
49     r = calloc( sizeof( hb_reader_t ), 1 );
50
51     r->job   = job;
52     r->title = job->title;
53     r->die   = job->die;
54     r->sequence = 0;
55
56     r->st_slots = 4;
57     r->stream_timing = calloc( sizeof(stream_timing_t), r->st_slots );
58     r->stream_timing[0].id = r->title->video_id;
59     r->stream_timing[0].average = 90000. * (double)job->vrate_base /
60                                            (double)job->vrate;
61     r->stream_timing[1].id = -1;
62
63     return hb_thread_init( "reader", ReaderFunc, r,
64                            HB_NORMAL_PRIORITY );
65 }
66
67 static void push_buf( const hb_reader_t *r, hb_fifo_t *fifo, hb_buffer_t *buf )
68 {
69     while( !*r->die && !r->job->done && hb_fifo_is_full( fifo ) )
70     {
71         /*
72          * Loop until the incoming fifo is ready to receive
73          * this buffer.
74          */
75         hb_snooze( 50 );
76     }
77     hb_fifo_push( fifo, buf );
78 }
79
80 // The MPEG STD (Standard Timing Decoder) essentially requires that we keep
81 // per-stream timing so that when there's a timing discontinuity we can
82 // seemlessly join packets on either side of the discontinuity. This join
83 // requires that we know the timestamp of the previous packet and the
84 // average inter-packet time (since we position the new packet at the end
85 // of the previous packet). The next three routines keep track of this
86 // per-stream timing.
87
88 // find the per-stream timing state associated with 'buf'
89
90 static stream_timing_t *id_to_st( hb_reader_t *r, const hb_buffer_t *buf )
91 {
92     stream_timing_t *st = r->stream_timing;
93     while ( st->id != buf->id && st->id != -1)
94     {
95         ++st;
96     }
97     // if we haven't seen this stream add it.
98     if ( st->id == -1 )
99     {
100         // we keep the steam timing info in an array with some power-of-two
101         // number of slots. If we don't have two slots left (one for our new
102         // entry plus one for the "-1" eol) we need to expand the array.
103         int slot = st - r->stream_timing;
104         if ( slot + 1 >= r->st_slots )
105         {
106             r->st_slots *= 2;
107             r->stream_timing = realloc( r->stream_timing, r->st_slots *
108                                         sizeof(*r->stream_timing) );
109             st = r->stream_timing + slot;
110         }
111         st->id = buf->id;
112         st->last = buf->renderOffset;
113         st->average = 30.*90.;
114
115         st[1].id = -1;
116     }
117     return st;
118 }
119
120 // update the average inter-packet time of the stream associated with 'buf'
121 // using a recursive low-pass filter with a 16 packet time constant.
122
123 static void update_ipt( hb_reader_t *r, const hb_buffer_t *buf )
124 {
125     stream_timing_t *st = id_to_st( r, buf );
126     double dt = buf->renderOffset - st->last;
127     st->average += ( dt - st->average ) * (1./16.);
128     st->last = buf->renderOffset;
129 }
130
131 // use the per-stream state associated with 'buf' to compute a new scr_offset
132 // such that 'buf' will follow the previous packet of this stream separated
133 // by the average packet time of the stream.
134
135 static void new_scr_offset( hb_reader_t *r, const hb_buffer_t *buf )
136 {
137     stream_timing_t *st = id_to_st( r, buf );
138     int64_t nxt = st->last + st->average - r->scr_offset;
139     r->scr_offset = buf->renderOffset - nxt;
140     r->scr_changes = r->demux.scr_changes;
141     st->last = buf->renderOffset;
142 }
143
144 /***********************************************************************
145  * ReaderFunc
146  ***********************************************************************
147  *
148  **********************************************************************/
149 static void ReaderFunc( void * _r )
150 {
151     hb_reader_t  * r = _r;
152     hb_fifo_t   ** fifos;
153     hb_buffer_t  * buf;
154     hb_list_t    * list;
155     int            n;
156     int            chapter = -1;
157     int            chapter_end = r->job->chapter_end;
158
159     if( !( r->dvd = hb_dvd_init( r->title->dvd ) ) )
160     {
161         if ( !( r->stream = hb_stream_open( r->title->dvd, r->title ) ) )
162         {
163           return;
164         }
165     }
166
167     if (r->dvd)
168     {
169       /*
170        * XXX this code is a temporary hack that should go away if/when
171        *     chapter merging goes away in libhb/dvd.c
172        * map the start and end chapter numbers to on-media chapter
173        * numbers since chapter merging could cause the handbrake numbers
174        * to diverge from the media numbers and, if our chapter_end is after
175        * a media chapter that got merged, we'll stop ripping too early.
176        */
177       int start = r->job->chapter_start;
178       hb_chapter_t * chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
179
180       chapter_end = chap->index;
181       if (start > 1)
182       {
183          chap = hb_list_item( r->title->list_chapter, start - 1 );
184          start = chap->index;
185       }
186       /* end chapter mapping XXX */
187
188       if( !hb_dvd_start( r->dvd, r->title->index, start ) )
189       {
190           hb_dvd_close( &r->dvd );
191           return;
192       }
193     }
194
195     list  = hb_list_init();
196     hb_buffer_t *ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
197
198     while( !*r->die && !r->job->done )
199     {
200         if (r->dvd)
201           chapter = hb_dvd_chapter( r->dvd );
202         else if (r->stream)
203           chapter = 1;
204
205         if( chapter < 0 )
206         {
207             hb_log( "reader: end of the title reached" );
208             break;
209         }
210         if( chapter > chapter_end )
211         {
212             hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
213                     r->job->chapter_end, chapter_end, chapter );
214             break;
215         }
216
217         if (r->dvd)
218         {
219           if( !hb_dvd_read( r->dvd, ps ) )
220           {
221               break;
222           }
223         }
224         else if (r->stream)
225         {
226           if ( !hb_stream_read( r->stream, ps ) )
227           {
228             break;
229           }
230         }
231
232         if( r->job->indepth_scan )
233         {
234             /*
235              * Need to update the progress during a subtitle scan
236              */
237             hb_state_t state;
238
239 #define p state.param.working
240
241             state.state = HB_STATE_WORKING;
242             p.progress = (double)chapter / (double)r->job->chapter_end;
243             if( p.progress > 1.0 )
244             {
245                 p.progress = 1.0;
246             }
247             p.rate_avg = 0.0;
248             p.hours    = -1;
249             p.minutes  = -1;
250             p.seconds  = -1;
251             hb_set_state( r->job->h, &state );
252         }
253
254         if ( r->title->demuxer == HB_NULL_DEMUXER )
255         {
256             hb_demux_null( ps, list, &r->demux );
257         }
258         else
259         {
260             hb_demux_ps( ps, list, &r->demux );
261         }
262
263         while( ( buf = hb_list_item( list, 0 ) ) )
264         {
265             hb_list_rem( list, buf );
266             fifos = GetFifoForId( r->job, buf->id );
267
268             if ( ! r->saw_video )
269             {
270                 /* The first video packet defines 'time zero' so discard
271                    data until we get a video packet with a PTS */
272                 if ( buf->id == r->title->video_id && buf->start != -1 )
273                 {
274                     r->saw_video = 1;
275                     r->scr_changes = r->demux.scr_changes;
276                     new_scr_offset( r, buf );
277                     hb_log( "reader: first SCR %llu scr_offset %llu",
278                             r->demux.last_scr, r->scr_offset );
279                 }
280                 else
281                 {
282                     fifos = NULL;
283                 }
284             }
285             if( fifos )
286             {
287                 if ( buf->start != -1 )
288                 {
289                     if ( r->scr_changes == r->demux.scr_changes )
290                     {
291                         // This packet is referenced to the same SCR as the last.
292                         // Update the average inter-packet time for this stream.
293                         update_ipt( r, buf );
294                     }
295                     else
296                     {
297                         // This is the first audio or video packet after an SCR
298                         // change. Compute a new scr offset that would make this
299                         // packet follow the last of this stream with the correct
300                         // average spacing.
301                         new_scr_offset( r, buf );
302                     }
303                     // adjust timestamps to remove System Clock Reference offsets.
304                     buf->start -= r->scr_offset;
305                     buf->renderOffset -= r->scr_offset;
306                 }
307
308                 buf->sequence = r->sequence++;
309                 /* if there are mutiple output fifos, send a copy of the
310                  * buffer down all but the first (we have to not ship the
311                  * original buffer or we'll race with the thread that's
312                  * consuming the buffer & inject garbage into the data stream). */
313                 for( n = 1; fifos[n] != NULL; n++)
314                 {
315                     hb_buffer_t *buf_copy = hb_buffer_init( buf->size );
316                     hb_buffer_copy_settings( buf_copy, buf );
317                     memcpy( buf_copy->data, buf->data, buf->size );
318                     push_buf( r, fifos[n], buf_copy );
319                 }
320                 push_buf( r, fifos[0], buf );
321             }
322             else
323             {
324                 hb_buffer_close( &buf );
325             }
326         }
327     }
328
329     /* send empty buffers upstream to video & audio decoders to signal we're done */
330     push_buf( r, r->job->fifo_mpeg2, hb_buffer_init(0) );
331
332     hb_audio_t *audio;
333     for( n = 0;
334          ( audio = hb_list_item( r->job->title->list_audio, n ) ) != NULL;
335          ++n )
336     {
337         if ( audio->priv.fifo_in )
338             push_buf( r, audio->priv.fifo_in, hb_buffer_init(0) );
339     }
340
341     hb_list_empty( &list );
342     hb_buffer_close( &ps );
343     if (r->dvd)
344     {
345       hb_dvd_stop( r->dvd );
346       hb_dvd_close( &r->dvd );
347     }
348     else if (r->stream)
349     {
350       hb_stream_close(&r->stream);
351     }
352
353     if ( r->stream_timing )
354     {
355         free( r->stream_timing );
356     }
357
358     hb_log( "reader: done. %d scr changes", r->demux.scr_changes );
359
360     free( r );
361     _r = NULL;
362 }
363
364 /***********************************************************************
365  * GetFifoForId
366  ***********************************************************************
367  *
368  **********************************************************************/
369 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
370 {
371     hb_title_t    * title = job->title;
372     hb_audio_t    * audio;
373     hb_subtitle_t * subtitle;
374     int             i, n;
375     static hb_fifo_t * fifos[8];
376
377     memset(fifos, 0, sizeof(fifos));
378
379     if( id == title->video_id )
380     {
381         if( job->indepth_scan )
382         {
383             /*
384              * Ditch the video here during the indepth scan until
385              * we can improve the MPEG2 decode performance.
386              */
387             return NULL;
388         }
389         else
390         {
391             fifos[0] = job->fifo_mpeg2;
392             return fifos;
393         }
394     }
395
396     if( job->indepth_scan ) {
397         /*
398          * Count the occurances of the subtitles, don't actually
399          * return any to encode unless we are looking fro forced
400          * subtitles in which case we need to look in the sub picture
401          * to see if it has the forced flag enabled.
402          */
403         for (i=0; i < hb_list_count(title->list_subtitle); i++) {
404             subtitle =  hb_list_item( title->list_subtitle, i);
405             if (id == subtitle->id) {
406                 /*
407                  * A hit, count it.
408                  */
409                 subtitle->hits++;
410                 if( job->subtitle_force )
411                 {
412
413                     fifos[0] = subtitle->fifo_in;
414                     return fifos;
415                 }
416                 break;
417             }
418         }
419     } else {
420         if( ( subtitle = hb_list_item( title->list_subtitle, 0 ) ) &&
421             id == subtitle->id )
422         {
423             fifos[0] = subtitle->fifo_in;
424             return fifos;
425         }
426     }
427     if( !job->indepth_scan )
428     {
429         n = 0;
430         for( i = 0; i < hb_list_count( title->list_audio ); i++ )
431         {
432             audio = hb_list_item( title->list_audio, i );
433             if( id == audio->id )
434             {
435                 fifos[n++] = audio->priv.fifo_in;
436             }
437         }
438
439         if( n != 0 )
440         {
441             return fifos;
442         }
443     }
444
445     return NULL;
446 }
447