OSDN Git Service

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