OSDN Git Service

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