OSDN Git Service

fix potential runaway buffer usage
[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     double average; // average time between packets
12     int64_t last;   // last timestamp seen on this stream
13     int id;         // stream id
14     int is_audio;   // != 0 if this is an audio stream
15 } stream_timing_t;
16
17 typedef struct
18 {
19     hb_job_t     * job;
20     hb_title_t   * title;
21     volatile int * die;
22
23     hb_dvd_t     * dvd;
24     hb_stream_t  * stream;
25
26     stream_timing_t *stream_timing;
27     int64_t        scr_offset;
28     hb_psdemux_t   demux;
29     int            scr_changes;
30     uint32_t       sequence;
31     uint8_t        st_slots;        // size (in slots) of stream_timing array
32     uint8_t        saw_video;       // != 0 if we've seen video
33     uint8_t        saw_audio;       // != 0 if we've seen audio
34 } hb_reader_t;
35
36 /***********************************************************************
37  * Local prototypes
38  **********************************************************************/
39 static void        ReaderFunc( void * );
40 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id );
41
42 /***********************************************************************
43  * hb_reader_init
44  ***********************************************************************
45  *
46  **********************************************************************/
47 hb_thread_t * hb_reader_init( hb_job_t * job )
48 {
49     hb_reader_t * r;
50
51     r = calloc( sizeof( hb_reader_t ), 1 );
52
53     r->job   = job;
54     r->title = job->title;
55     r->die   = job->die;
56     r->sequence = 0;
57
58     r->st_slots = 4;
59     r->stream_timing = calloc( sizeof(stream_timing_t), r->st_slots );
60     r->stream_timing[0].id = r->title->video_id;
61     r->stream_timing[0].average = 90000. * (double)job->vrate_base /
62                                            (double)job->vrate;
63     r->stream_timing[0].last = -r->stream_timing[0].average;
64     r->stream_timing[1].id = -1;
65
66     return hb_thread_init( "reader", ReaderFunc, r,
67                            HB_NORMAL_PRIORITY );
68 }
69
70 static void push_buf( const hb_reader_t *r, hb_fifo_t *fifo, hb_buffer_t *buf )
71 {
72     while ( !*r->die )
73     {
74         if ( hb_fifo_full_wait( fifo ) )
75         {
76             hb_fifo_push( fifo, buf );
77             break;
78         }
79     }
80 }
81
82 static int is_audio( hb_reader_t *r, int id )
83 {
84     int i;
85     hb_audio_t *audio;
86
87     for( i = 0; ( audio = hb_list_item( r->title->list_audio, i ) ); ++i )
88     {
89         if ( audio->id == id )
90         {
91             return 1;
92         }
93     }
94     return 0;
95 }
96
97 // The MPEG STD (Standard Target Decoder) essentially requires that we keep
98 // per-stream timing so that when there's a timing discontinuity we can
99 // seemlessly join packets on either side of the discontinuity. This join
100 // requires that we know the timestamp of the previous packet and the
101 // average inter-packet time (since we position the new packet at the end
102 // of the previous packet). The next four routines keep track of this
103 // per-stream timing.
104
105 // find the per-stream timing state for 'buf'
106
107 static stream_timing_t *find_st( hb_reader_t *r, const hb_buffer_t *buf )
108 {
109     stream_timing_t *st = r->stream_timing;
110     for ( ; st->id != -1; ++st )
111     {
112         if ( st->id == buf->id )
113             return st;
114     }
115     return NULL;
116 }
117
118 // find or create the per-stream timing state for 'buf'
119
120 static stream_timing_t *id_to_st( hb_reader_t *r, const hb_buffer_t *buf )
121 {
122     stream_timing_t *st = r->stream_timing;
123     while ( st->id != buf->id && st->id != -1)
124     {
125         ++st;
126     }
127     // if we haven't seen this stream add it.
128     if ( st->id == -1 )
129     {
130         // we keep the steam timing info in an array with some power-of-two
131         // number of slots. If we don't have two slots left (one for our new
132         // entry plus one for the "-1" eol) we need to expand the array.
133         int slot = st - r->stream_timing;
134         if ( slot + 1 >= r->st_slots )
135         {
136             r->st_slots *= 2;
137             r->stream_timing = realloc( r->stream_timing, r->st_slots *
138                                         sizeof(*r->stream_timing) );
139             st = r->stream_timing + slot;
140         }
141         st->id = buf->id;
142         st->average = 30.*90.;
143         if ( r->saw_video )
144             st->last = buf->renderOffset - st->average;
145         else
146             st->last = -st->average;
147         if ( ( st->is_audio = is_audio( r, buf->id ) ) != 0 )
148         {
149             r->saw_audio = 1;
150         }
151         st[1].id = -1;
152     }
153     return st;
154 }
155
156 // update the average inter-packet time of the stream associated with 'buf'
157 // using a recursive low-pass filter with a 16 packet time constant.
158
159 static void update_ipt( hb_reader_t *r, const hb_buffer_t *buf )
160 {
161     stream_timing_t *st = id_to_st( r, buf );
162     double dt = buf->renderOffset - st->last;
163     st->average += ( dt - st->average ) * (1./32.);
164     st->last = buf->renderOffset;
165 }
166
167 // use the per-stream state associated with 'buf' to compute a new scr_offset
168 // such that 'buf' will follow the previous packet of this stream separated
169 // by the average packet time of the stream.
170
171 static void new_scr_offset( hb_reader_t *r, hb_buffer_t *buf )
172 {
173     stream_timing_t *st = id_to_st( r, buf );
174     int64_t nxt = st->last + st->average;
175     r->scr_offset = buf->renderOffset - nxt;
176     buf->renderOffset = nxt;
177     r->scr_changes = r->demux.scr_changes;
178     st->last = buf->renderOffset;
179 }
180
181 /***********************************************************************
182  * ReaderFunc
183  ***********************************************************************
184  *
185  **********************************************************************/
186 static void ReaderFunc( void * _r )
187 {
188     hb_reader_t  * r = _r;
189     hb_fifo_t   ** fifos;
190     hb_buffer_t  * buf;
191     hb_list_t    * list;
192     int            n;
193     int            chapter = -1;
194     int            chapter_end = r->job->chapter_end;
195
196     if ( r->title->type == HB_DVD_TYPE )
197     {
198         if ( !( r->dvd = hb_dvd_init( r->title->path ) ) )
199             return;
200     }
201     else if ( r->title->type == HB_STREAM_TYPE )
202     {
203         if ( !( r->stream = hb_stream_open( r->title->path, r->title ) ) )
204             return;
205     }
206     else
207     {
208         // Unknown type, should never happen
209         return;
210     }
211
212     if (r->dvd)
213     {
214         /*
215          * XXX this code is a temporary hack that should go away if/when
216          *     chapter merging goes away in libhb/dvd.c
217          * map the start and end chapter numbers to on-media chapter
218          * numbers since chapter merging could cause the handbrake numbers
219          * to diverge from the media numbers and, if our chapter_end is after
220          * a media chapter that got merged, we'll stop ripping too early.
221          */
222         int start = r->job->chapter_start;
223         hb_chapter_t *chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
224
225         chapter_end = chap->index;
226         if (start > 1)
227         {
228            chap = hb_list_item( r->title->list_chapter, start - 1 );
229            start = chap->index;
230         }
231         /* end chapter mapping XXX */
232
233         if( !hb_dvd_start( r->dvd, r->title, start ) )
234         {
235             hb_dvd_close( &r->dvd );
236             return;
237         }
238         if (r->job->angle)
239         {
240             hb_dvd_set_angle( r->dvd, r->job->angle );
241         }
242
243         if ( r->job->start_at_preview )
244         {
245             // XXX code from DecodePreviews - should go into its own routine
246             hb_dvd_seek( r->dvd, (float)r->job->start_at_preview /
247                          ( r->job->seek_points ? ( r->job->seek_points + 1.0 ) : 11.0 ) );
248         }
249     }
250     else if ( r->stream && r->job->start_at_preview )
251     {
252         
253         // XXX code from DecodePreviews - should go into its own routine
254         hb_stream_seek( r->stream, (float)( r->job->start_at_preview - 1 ) /
255                         ( r->job->seek_points ? ( r->job->seek_points + 1.0 ) : 11.0 ) );
256
257     } 
258     else if( r->stream )
259     {
260         /*
261          * Standard stream, seek to the starting chapter, if set, and track the
262          * end chapter so that we end at the right time.
263          */
264         int start = r->job->chapter_start;
265         hb_chapter_t *chap = hb_list_item( r->title->list_chapter, chapter_end - 1 );
266         
267         chapter_end = chap->index;
268         if (start > 1)
269         {
270             chap = hb_list_item( r->title->list_chapter, start - 1 );
271             start = chap->index;
272         }
273         
274         /*
275          * Seek to the start chapter.
276          */
277         hb_stream_seek_chapter( r->stream, start );
278     }
279
280     list  = hb_list_init();
281     hb_buffer_t *ps = hb_buffer_init( HB_DVD_READ_BUFFER_SIZE );
282
283     while( !*r->die && !r->job->done )
284     {
285         if (r->dvd)
286             chapter = hb_dvd_chapter( r->dvd );
287         else if (r->stream)
288             chapter = hb_stream_chapter( r->stream );
289
290         if( chapter < 0 )
291         {
292             hb_log( "reader: end of the title reached" );
293             break;
294         }
295         if( chapter > chapter_end )
296         {
297             hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
298                     r->job->chapter_end, chapter_end, chapter );
299             break;
300         }
301
302         if (r->dvd)
303         {
304           if( !hb_dvd_read( r->dvd, ps ) )
305           {
306               break;
307           }
308         }
309         else if (r->stream)
310         {
311           if ( !hb_stream_read( r->stream, ps ) )
312           {
313             break;
314           }
315         }
316
317         if( r->job->indepth_scan )
318         {
319             /*
320              * Need to update the progress during a subtitle scan
321              */
322             hb_state_t state;
323
324 #define p state.param.working
325
326             state.state = HB_STATE_WORKING;
327             p.progress = (double)chapter / (double)r->job->chapter_end;
328             if( p.progress > 1.0 )
329             {
330                 p.progress = 1.0;
331             }
332             p.rate_avg = 0.0;
333             p.hours    = -1;
334             p.minutes  = -1;
335             p.seconds  = -1;
336             hb_set_state( r->job->h, &state );
337         }
338
339         (hb_demux[r->title->demuxer])( ps, list, &r->demux );
340
341         while( ( buf = hb_list_item( list, 0 ) ) )
342         {
343             hb_list_rem( list, buf );
344             fifos = GetFifoForId( r->job, buf->id );
345
346             if ( fifos && ! r->saw_video && !r->job->indepth_scan )
347             {
348                 // The first data packet with a PTS from an audio or video stream
349                 // that we're decoding defines 'time zero'. Discard packets until
350                 // we get one.
351                 if ( buf->start != -1 && buf->renderOffset != -1 &&
352                      ( buf->id == r->title->video_id || is_audio( r, buf->id ) ) )
353                 {
354                     // force a new scr offset computation
355                     r->scr_changes = r->demux.scr_changes - 1;
356                     // create a stream state if we don't have one so the
357                     // offset will get computed correctly.
358                     id_to_st( r, buf );
359                     r->saw_video = 1;
360                     hb_log( "reader: first SCR %"PRId64" id %d DTS %"PRId64,
361                             r->demux.last_scr, buf->id, buf->renderOffset );
362                 }
363                 else
364                 {
365                     fifos = NULL;
366                 }
367             }
368             if( fifos )
369             {
370                 if ( buf->renderOffset != -1 )
371                 {
372                     if ( r->scr_changes == r->demux.scr_changes )
373                     {
374                         // This packet is referenced to the same SCR as the last.
375                         // Adjust timestamp to remove the System Clock Reference
376                         // offset then update the average inter-packet time
377                         // for this stream.
378                         buf->renderOffset -= r->scr_offset;
379                         update_ipt( r, buf );
380                     }
381                     else
382                     {
383                         // This is the first audio or video packet after an SCR
384                         // change. Compute a new scr offset that would make this
385                         // packet follow the last of this stream with the correct
386                         // average spacing.
387                         stream_timing_t *st = find_st( r, buf );
388
389                         if ( st )
390                         {
391                             // if this is the video stream and we don't have
392                             // audio yet or this is an audio stream
393                             // generate a new scr
394                             if ( st->is_audio ||
395                                  ( st == r->stream_timing && !r->saw_audio ) )
396                             {
397                                 new_scr_offset( r, buf );
398                             }
399                             else
400                             {
401                                 // defer the scr change until we get some
402                                 // audio since audio has a timestamp per
403                                 // frame but video & subtitles don't. Clear
404                                 // the timestamps so the decoder will generate
405                                 // them from the frame durations.
406                                 if ( st != r->stream_timing )
407                                 {
408                                     // not a video stream so it's probably
409                                     // subtitles - the best we can do is to
410                                     // line it up with the last video packet.
411                                     buf->start = r->stream_timing->last;
412                                 }
413                                 else
414                                 {
415                                     buf->start = -1;
416                                     buf->renderOffset = -1;
417                                 }
418                             }
419                         }
420                         else
421                         {
422                             // we got a new scr at the same time as the first
423                             // packet of a stream we've never seen before. We
424                             // have no idea what the timing should be so toss
425                             // this buffer & wait for a stream we've already seen.
426                             // add stream to list of streams we have seen
427                             id_to_st( r, buf );
428                             hb_buffer_close( &buf );
429                             continue;
430                         }
431                     }
432                 }
433                 if ( buf->start != -1 )
434                 {
435                     buf->start -= r->scr_offset;
436                     if ( r->job->pts_to_stop && buf->start > r->job->pts_to_stop )
437                     {
438                         // we're doing a subset of the input and we've hit the
439                         // stopping point.
440                         hb_buffer_close( &buf );
441                         goto done;
442                     }
443                 }
444
445                 buf->sequence = r->sequence++;
446                 /* if there are mutiple output fifos, send a copy of the
447                  * buffer down all but the first (we have to not ship the
448                  * original buffer or we'll race with the thread that's
449                  * consuming the buffer & inject garbage into the data stream). */
450                 for( n = 1; fifos[n] != NULL; n++)
451                 {
452                     hb_buffer_t *buf_copy = hb_buffer_init( buf->size );
453                     hb_buffer_copy_settings( buf_copy, buf );
454                     memcpy( buf_copy->data, buf->data, buf->size );
455                     push_buf( r, fifos[n], buf_copy );
456                 }
457                 push_buf( r, fifos[0], buf );
458             }
459             else
460             {
461                 hb_buffer_close( &buf );
462             }
463         }
464     }
465
466   done:
467     // send empty buffers downstream to video & audio decoders to signal we're done.
468     if( !*r->die && !r->job->done )
469     {
470         push_buf( r, r->job->fifo_mpeg2, hb_buffer_init(0) );
471
472         hb_audio_t *audio;
473         for( n = 0; (audio = hb_list_item( r->job->title->list_audio, n)); ++n )
474         {
475             if ( audio->priv.fifo_in )
476                 push_buf( r, audio->priv.fifo_in, hb_buffer_init(0) );
477         }
478
479         hb_subtitle_t *subtitle;
480         for( n = 0; (subtitle = hb_list_item( r->job->title->list_subtitle, n)); ++n )
481         {
482             if ( subtitle->fifo_in && subtitle->source == VOBSUB)
483                 push_buf( r, subtitle->fifo_in, hb_buffer_init(0) );
484         }
485     }
486
487     hb_list_empty( &list );
488     hb_buffer_close( &ps );
489     if (r->dvd)
490     {
491         hb_dvd_stop( r->dvd );
492         hb_dvd_close( &r->dvd );
493     }
494     else if (r->stream)
495     {
496         hb_stream_close(&r->stream);
497     }
498
499     if ( r->stream_timing )
500     {
501         free( r->stream_timing );
502     }
503
504     hb_log( "reader: done. %d scr changes", r->demux.scr_changes );
505     if ( r->demux.dts_drops )
506     {
507         hb_log( "reader: %d drops because DTS out of range", r->demux.dts_drops );
508     }
509
510     free( r );
511     _r = NULL;
512 }
513
514 /***********************************************************************
515  * GetFifoForId
516  ***********************************************************************
517  *
518  **********************************************************************/
519 static hb_fifo_t ** GetFifoForId( hb_job_t * job, int id )
520 {
521     hb_title_t    * title = job->title;
522     hb_audio_t    * audio;
523     hb_subtitle_t * subtitle;
524     int             i, n, count;
525     static hb_fifo_t * fifos[100];
526
527     memset(fifos, 0, sizeof(fifos));
528
529     if( id == title->video_id )
530     {
531         if( job->indepth_scan )
532         {
533             /*
534              * Ditch the video here during the indepth scan until
535              * we can improve the MPEG2 decode performance.
536              */
537             return NULL;
538         }
539         else
540         {
541             fifos[0] = job->fifo_mpeg2;
542             return fifos;
543         }
544     }
545
546     n = 0;
547     count = hb_list_count( title->list_subtitle );
548     count = count > 99 ? 99 : count;
549     for( i=0; i < count; i++ ) {
550         subtitle =  hb_list_item( title->list_subtitle, i );
551         if (id == subtitle->id) {
552             subtitle->hits++;
553             if( !job->indepth_scan || job->select_subtitle_config.force )
554             {
555                 /*
556                  * Pass the subtitles to be processed if we are not scanning, or if
557                  * we are scanning and looking for forced subs, then pass them up
558                  * to decode whether the sub is a forced one.
559                  */
560                 fifos[n++] = subtitle->fifo_in;
561             }
562         }
563     }
564     if ( n != 0 )
565     {
566         return fifos;
567     }
568     
569     if( !job->indepth_scan )
570     {
571         n = 0;
572         for( i = 0; i < hb_list_count( title->list_audio ); i++ )
573         {
574             audio = hb_list_item( title->list_audio, i );
575             if( id == audio->id )
576             {
577                 fifos[n++] = audio->priv.fifo_in;
578             }
579         }
580
581         if( n != 0 )
582         {
583             return fifos;
584         }
585     }
586
587     return NULL;
588 }
589