OSDN Git Service

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