OSDN Git Service

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