OSDN Git Service

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