WinGui: Fix another instance of the Caliburn vs Json.net sillyness where objects...
[HandBrake.git] / libhb / reader.c
blob9c742e6fd05f2ccf65b7c7c8ae4bee1114fbc1f5
1 /* reader.c
3 Copyright (c) 2003-2015 HandBrake Team
4 This file is part of the HandBrake source code
5 Homepage: <http://handbrake.fr/>.
6 It may be used under the terms of the GNU General Public License v2.
7 For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
8 */
9 #include "hb.h"
11 static int hb_reader_init( hb_work_object_t * w, hb_job_t * job );
12 static void hb_reader_close( hb_work_object_t * w );
14 hb_work_object_t hb_reader =
16 WORK_READER,
17 "Reader",
18 hb_reader_init,
19 NULL,
20 hb_reader_close,
21 NULL,
22 NULL
25 typedef struct
27 int startup;
28 double average; // average time between packets
29 double filtered_average; // average time between packets
30 int64_t last; // last timestamp seen on this stream
31 int id; // stream id
32 int is_audio; // != 0 if this is an audio stream
33 int valid; // Stream timing is not valid until next scr.
34 } stream_timing_t;
36 struct hb_work_private_s
38 hb_handle_t * h;
39 hb_job_t * job;
40 hb_title_t * title;
41 volatile int * die;
43 hb_bd_t * bd;
44 hb_dvd_t * dvd;
45 hb_stream_t * stream;
47 stream_timing_t *stream_timing;
48 int64_t scr_offset;
49 int sub_scr_set;
50 hb_psdemux_t demux;
51 int scr_changes;
52 uint32_t sequence;
53 uint8_t st_slots; // size (in slots) of stream_timing array
54 uint8_t saw_video; // != 0 if we've seen video
55 uint8_t saw_audio; // != 0 if we've seen audio
57 int start_found; // found pts_to_start point
58 int64_t pts_to_start;
59 uint64_t st_first;
60 uint64_t duration;
61 hb_fifo_t * fifos[100];
64 /***********************************************************************
65 * Local prototypes
66 **********************************************************************/
67 static hb_fifo_t ** GetFifoForId( hb_work_private_t * r, int id );
68 static void UpdateState( hb_work_private_t * r, int64_t start);
70 /***********************************************************************
71 * hb_reader_init
72 ***********************************************************************
74 **********************************************************************/
75 static int hb_reader_open( hb_work_private_t * r )
77 if ( r->title->type == HB_BD_TYPE )
79 if ( !( r->bd = hb_bd_init( r->h, r->title->path ) ) )
80 return 1;
82 else if ( r->title->type == HB_DVD_TYPE )
84 if ( !( r->dvd = hb_dvd_init( r->h, r->title->path ) ) )
85 return 1;
87 else if ( r->title->type == HB_STREAM_TYPE ||
88 r->title->type == HB_FF_STREAM_TYPE )
90 if (!(r->stream = hb_stream_open(r->h, r->title->path, r->title, 0)))
91 return 1;
93 else
95 // Unknown type, should never happen
96 return 1;
98 return 0;
101 static int hb_reader_init( hb_work_object_t * w, hb_job_t * job )
103 hb_work_private_t * r;
105 r = calloc( sizeof( hb_work_private_t ), 1 );
106 w->private_data = r;
108 r->h = job->h;
109 r->job = job;
110 r->title = job->title;
111 r->die = job->die;
112 r->sequence = 0;
114 r->st_slots = 4;
115 r->stream_timing = calloc( sizeof(stream_timing_t), r->st_slots );
116 r->stream_timing[0].id = r->title->video_id;
117 r->stream_timing[0].average = 90000. * (double)job->vrate.den /
118 job->vrate.num;
119 r->stream_timing[0].filtered_average = r->stream_timing[0].average;
120 r->stream_timing[0].last = -r->stream_timing[0].average;
121 r->stream_timing[0].valid = 1;
122 r->stream_timing[0].startup = 10;
123 r->stream_timing[1].id = -1;
125 r->demux.last_scr = AV_NOPTS_VALUE;
127 if ( !job->pts_to_start )
128 r->start_found = 1;
129 else
131 // The frame at the actual start time may not be an i-frame
132 // so can't be decoded without starting a little early.
133 // sync.c will drop early frames.
134 // Starting a little over 10 seconds early
135 r->pts_to_start = MAX(0, job->pts_to_start - 1000000);
138 if (job->pts_to_stop)
140 r->duration = job->pts_to_start + job->pts_to_stop;
142 else if (job->frame_to_stop)
144 int frames = job->frame_to_start + job->frame_to_stop;
145 r->duration = (int64_t)frames * job->title->vrate.den * 90000 /
146 job->title->vrate.num;
148 else
150 hb_chapter_t *chapter;
151 int ii;
153 r->duration = 0;
154 for (ii = job->chapter_start; ii < job->chapter_end; ii++)
156 chapter = hb_list_item( job->title->list_chapter, ii - 1);
157 r->duration += chapter->duration;
161 // The stream needs to be open before starting the reader thead
162 // to prevent a race with decoders that may share information
163 // with the reader. Specifically avcodec needs this.
164 if ( hb_reader_open( r ) )
166 free( r->stream_timing );
167 free( r );
168 return 1;
170 return 0;
174 static void hb_reader_close( hb_work_object_t * w )
176 hb_work_private_t * r = w->private_data;
178 if (r->bd)
180 hb_bd_stop( r->bd );
181 hb_bd_close( &r->bd );
183 else if (r->dvd)
185 hb_dvd_stop( r->dvd );
186 hb_dvd_close( &r->dvd );
188 else if (r->stream)
190 hb_stream_close(&r->stream);
193 if ( r->stream_timing )
195 free( r->stream_timing );
198 free( r );
201 static void push_buf( const hb_work_private_t *r, hb_fifo_t *fifo, hb_buffer_t *buf )
203 while ( !*r->die && !r->job->done )
205 if ( hb_fifo_full_wait( fifo ) )
207 hb_fifo_push( fifo, buf );
208 buf = NULL;
209 break;
212 if ( buf )
214 hb_buffer_close( &buf );
218 static int is_audio( hb_work_private_t *r, int id )
220 int i;
221 hb_audio_t *audio;
223 for( i = 0; ( audio = hb_list_item( r->title->list_audio, i ) ); ++i )
225 if ( audio->id == id )
227 return 1;
230 return 0;
233 static int is_subtitle( hb_work_private_t *r, int id )
235 int i;
236 hb_subtitle_t *sub;
238 for( i = 0; ( sub = hb_list_item( r->title->list_subtitle, i ) ); ++i )
240 if ( sub->id == id )
242 return 1;
245 return 0;
248 // The MPEG STD (Standard Target Decoder) essentially requires that we keep
249 // per-stream timing so that when there's a timing discontinuity we can
250 // seemlessly join packets on either side of the discontinuity. This join
251 // requires that we know the timestamp of the previous packet and the
252 // average inter-packet time (since we position the new packet at the end
253 // of the previous packet). The next four routines keep track of this
254 // per-stream timing.
256 // find or create the per-stream timing state for 'buf'
258 static stream_timing_t *id_to_st( hb_work_private_t *r, const hb_buffer_t *buf, int valid )
260 stream_timing_t *st = r->stream_timing;
261 while ( st->id != buf->s.id && st->id != -1)
263 ++st;
265 // if we haven't seen this stream add it.
266 if ( st->id == -1 )
268 // we keep the steam timing info in an array with some power-of-two
269 // number of slots. If we don't have two slots left (one for our new
270 // entry plus one for the "-1" eol) we need to expand the array.
271 int slot = st - r->stream_timing;
272 if ( slot + 1 >= r->st_slots )
274 r->st_slots *= 2;
275 r->stream_timing = realloc( r->stream_timing, r->st_slots *
276 sizeof(*r->stream_timing) );
277 st = r->stream_timing + slot;
279 st->id = buf->s.id;
280 st->average = 30.*90.;
281 st->filtered_average = st->average;
282 st->startup = 10;
283 st->last = -st->average;
284 if ( ( st->is_audio = is_audio( r, buf->s.id ) ) != 0 )
286 r->saw_audio = 1;
288 st[1].id = -1;
289 st->valid = valid;
291 return st;
294 // update the average inter-packet time of the stream associated with 'buf'
295 // using a recursive low-pass filter with a 16 packet time constant.
297 static void update_ipt( hb_work_private_t *r, const hb_buffer_t *buf )
299 stream_timing_t *st = id_to_st( r, buf, 1 );
301 if (buf->s.renderOffset == AV_NOPTS_VALUE)
303 st->last += st->filtered_average;
304 return;
307 double dt = buf->s.renderOffset - st->last;
309 // Protect against spurious bad timestamps
310 // timestamps should only move forward and by reasonable increments
311 if ( dt > 0 && dt < 5 * 90000LL )
313 if( st->startup )
315 st->average += ( dt - st->average ) * (1./4.);
316 st->startup--;
318 else
320 st->average += ( dt - st->average ) * (1./32.);
322 // Ignore outliers
323 if (dt < 1.5 * st->average)
325 st->filtered_average += ( dt - st->filtered_average ) * (1./32.);
328 st->last = buf->s.renderOffset;
329 st->valid = 1;
332 // use the per-stream state associated with 'buf' to compute a new scr_offset
333 // such that 'buf' will follow the previous packet of this stream separated
334 // by the average packet time of the stream.
336 static void new_scr_offset( hb_work_private_t *r, hb_buffer_t *buf )
338 stream_timing_t *st = id_to_st( r, buf, 1 );
339 int64_t last;
340 if ( !st->valid )
342 // !valid means we've not received any previous data
343 // for this stream. There is no 'last' packet time.
344 // So approximate it with video's last time.
345 last = r->stream_timing[0].last;
346 st->valid = 1;
348 else
350 last = st->last;
352 int64_t nxt = last + st->filtered_average;
353 r->scr_offset = buf->s.renderOffset - nxt;
354 // This log is handy when you need to debug timing problems...
355 //hb_log("id %x last %"PRId64" avg %g nxt %"PRId64" renderOffset %"PRId64
356 // " scr_offset %"PRId64"",
357 // buf->s.id, last, st->filtered_average, nxt,
358 // buf->s.renderOffset, r->scr_offset);
359 r->scr_changes = r->demux.scr_changes;
362 /***********************************************************************
363 * ReaderFunc
364 ***********************************************************************
366 **********************************************************************/
367 void ReadLoop( void * _w )
369 hb_work_object_t * w = _w;
370 hb_work_private_t * r = w->private_data;
371 hb_fifo_t ** fifos;
372 hb_buffer_t * buf = NULL;
373 hb_list_t * list;
374 int n;
375 int chapter = -1;
376 int chapter_end = r->job->chapter_end;
377 uint8_t done = 0;
379 if (r->bd)
381 if( !hb_bd_start( r->bd, r->title ) )
383 hb_bd_close( &r->bd );
384 return;
386 if ( r->job->start_at_preview )
388 // XXX code from DecodePreviews - should go into its own routine
389 hb_bd_seek( r->bd, (float)r->job->start_at_preview /
390 ( r->job->seek_points ? ( r->job->seek_points + 1.0 ) : 11.0 ) );
392 else if ( r->job->pts_to_start )
394 // Note, bd seeks always put us to an i-frame. no need
395 // to start decoding early using r->pts_to_start
396 hb_bd_seek_pts( r->bd, r->job->pts_to_start );
397 r->duration -= r->job->pts_to_start;
398 r->job->pts_to_start = 0;
399 r->start_found = 1;
401 else
403 hb_bd_seek_chapter( r->bd, r->job->chapter_start );
405 if (r->job->angle > 1)
407 hb_bd_set_angle( r->bd, r->job->angle - 1 );
410 else if (r->dvd)
413 * XXX this code is a temporary hack that should go away if/when
414 * chapter merging goes away in libhb/dvd.c
415 * map the start and end chapter numbers to on-media chapter
416 * numbers since chapter merging could cause the handbrake numbers
417 * to diverge from the media numbers and, if our chapter_end is after
418 * a media chapter that got merged, we'll stop ripping too early.
420 int start = r->job->chapter_start;
421 hb_chapter_t *chap = hb_list_item( r->job->list_chapter, chapter_end - 1 );
423 chapter_end = chap->index;
424 if (start > 1)
426 chap = hb_list_item( r->job->list_chapter, start - 1 );
427 start = chap->index;
429 /* end chapter mapping XXX */
431 if( !hb_dvd_start( r->dvd, r->title, start ) )
433 hb_dvd_close( &r->dvd );
434 return;
436 if (r->job->angle)
438 hb_dvd_set_angle( r->dvd, r->job->angle );
441 if ( r->job->start_at_preview )
443 // XXX code from DecodePreviews - should go into its own routine
444 hb_dvd_seek( r->dvd, (float)r->job->start_at_preview /
445 ( r->job->seek_points ? ( r->job->seek_points + 1.0 ) : 11.0 ) );
448 else if ( r->stream && r->job->start_at_preview )
451 // XXX code from DecodePreviews - should go into its own routine
452 hb_stream_seek( r->stream, (float)( r->job->start_at_preview - 1 ) /
453 ( r->job->seek_points ? ( r->job->seek_points + 1.0 ) : 11.0 ) );
456 else if ( r->stream && r->job->pts_to_start )
458 if ( hb_stream_seek_ts( r->stream, r->job->pts_to_start ) >= 0 )
460 // Seek takes us to the nearest I-frame before the timestamp
461 // that we want. So we will retrieve the start time of the
462 // first packet we get, subtract that from pts_to_start, and
463 // inspect the reset of the frames in sync.
464 r->start_found = 2;
465 r->duration -= r->job->pts_to_start;
467 // hb_stream_seek_ts does nothing for TS streams and will return
468 // an error.
470 else if( r->stream )
473 * Standard stream, seek to the starting chapter, if set, and track the
474 * end chapter so that we end at the right time.
476 int start = r->job->chapter_start;
477 hb_chapter_t *chap = hb_list_item( r->job->list_chapter, chapter_end - 1 );
479 chapter_end = chap->index;
480 if (start > 1)
482 chap = hb_list_item( r->job->list_chapter, start - 1 );
483 start = chap->index;
487 * Seek to the start chapter.
489 hb_stream_seek_chapter( r->stream, start );
492 list = hb_list_init();
494 while(!*r->die && !r->job->done && !done)
496 if (r->bd)
497 chapter = hb_bd_chapter( r->bd );
498 else if (r->dvd)
499 chapter = hb_dvd_chapter( r->dvd );
500 else if (r->stream)
501 chapter = hb_stream_chapter( r->stream );
503 if( chapter < 0 )
505 hb_log( "reader: end of the title reached" );
506 break;
508 if( chapter > chapter_end )
510 hb_log( "reader: end of chapter %d (media %d) reached at media chapter %d",
511 r->job->chapter_end, chapter_end, chapter );
512 break;
515 if (buf == NULL)
517 if (r->bd)
519 if( (buf = hb_bd_read( r->bd )) == NULL )
521 break;
524 else if (r->dvd)
526 if( (buf = hb_dvd_read( r->dvd )) == NULL )
528 break;
531 else if (r->stream)
533 if ( (buf = hb_stream_read( r->stream )) == NULL )
535 break;
540 (hb_demux[r->title->demuxer])( buf, list, &r->demux );
542 while( ( buf = hb_list_item( list, 0 ) ) )
544 hb_list_rem( list, buf );
545 fifos = GetFifoForId( r, buf->s.id );
547 if (fifos && r->stream && r->start_found == 2 )
549 // We will inspect the timestamps of each frame in sync
550 // to skip from this seek point to the timestamp we
551 // want to start at.
552 if (buf->s.start != AV_NOPTS_VALUE &&
553 buf->s.start < r->job->pts_to_start)
555 r->job->pts_to_start -= buf->s.start;
557 else if ( buf->s.start >= r->job->pts_to_start )
559 r->job->pts_to_start = 0;
561 r->start_found = 1;
564 if ( fifos && ! r->saw_video && !r->job->indepth_scan )
566 // The first data packet with a PTS from an audio or video stream
567 // that we're decoding defines 'time zero'. Discard packets until
568 // we get one.
569 if (buf->s.start != AV_NOPTS_VALUE &&
570 buf->s.renderOffset != AV_NOPTS_VALUE &&
571 (buf->s.id == r->title->video_id ||
572 is_audio( r, buf->s.id)))
574 // force a new scr offset computation
575 r->scr_changes = r->demux.scr_changes - 1;
576 // create a stream state if we don't have one so the
577 // offset will get computed correctly.
578 id_to_st( r, buf, 1 );
579 r->saw_video = 1;
580 hb_log( "reader: first SCR %"PRId64" id 0x%x DTS %"PRId64,
581 r->demux.last_scr, buf->s.id, buf->s.renderOffset );
583 else
585 fifos = NULL;
589 if ( r->job->indepth_scan || fifos )
591 if ( buf->s.renderOffset != AV_NOPTS_VALUE )
593 if ( r->scr_changes != r->demux.scr_changes )
595 // This is the first audio or video packet after an SCR
596 // change. Compute a new scr offset that would make this
597 // packet follow the last of this stream with the
598 // correct average spacing.
599 stream_timing_t *st = id_to_st( r, buf, 0 );
601 // if this is the video stream and we don't have
602 // audio yet or this is an audio stream
603 // generate a new scr
604 if ( st->is_audio ||
605 ( st == r->stream_timing && !r->saw_audio ) )
607 new_scr_offset( r, buf );
608 r->sub_scr_set = 0;
610 else
612 // defer the scr change until we get some
613 // audio since audio has a timestamp per
614 // frame but video & subtitles don't. Clear
615 // the timestamps so the decoder will generate
616 // them from the frame durations.
617 if (is_subtitle(r, buf->s.id) &&
618 buf->s.start != AV_NOPTS_VALUE)
620 if (!r->sub_scr_set)
622 // We can't generate timestamps in the
623 // subtitle decoder as we can for
624 // audio & video. So we need to make
625 // the closest guess that we can
626 // for the subtitles start time here.
627 int64_t last = r->stream_timing[0].last;
628 r->scr_offset = buf->s.start - last;
629 r->sub_scr_set = 1;
632 else
634 buf->s.start = AV_NOPTS_VALUE;
635 buf->s.renderOffset = AV_NOPTS_VALUE;
640 if ( buf->s.start != AV_NOPTS_VALUE )
642 int64_t start = buf->s.start - r->scr_offset;
644 if (!r->start_found || r->job->indepth_scan)
646 UpdateState( r, start );
649 if (r->job->indepth_scan && r->job->pts_to_stop &&
650 start >= r->pts_to_start + r->job->pts_to_stop)
652 // sync normally would terminate p-to-p
653 // but sync doesn't run during indepth scan
654 hb_log( "reader: reached pts %"PRId64", exiting early", start );
655 done = 1;
656 break;
659 if (!r->start_found && start >= r->pts_to_start)
661 // pts_to_start point found
662 r->start_found = 1;
663 if (r->stream)
665 // libav multi-threaded decoders can get into
666 // a bad state if the initial data is not
667 // decodable. So try to improve the chances of
668 // a good start by waiting for an initial iframe
669 hb_stream_set_need_keyframe(r->stream, 1);
670 hb_buffer_close( &buf );
671 continue;
674 // This log is handy when you need to debug timing problems
675 //hb_log("id %x scr_offset %"PRId64
676 // " start %"PRId64" --> %"PRId64"",
677 // buf->s.id, r->scr_offset, buf->s.start,
678 // buf->s.start - r->scr_offset);
679 buf->s.start -= r->scr_offset;
680 if ( buf->s.stop != AV_NOPTS_VALUE )
682 buf->s.stop -= r->scr_offset;
685 if ( buf->s.renderOffset != AV_NOPTS_VALUE )
687 // This packet is referenced to the same SCR as the last.
688 // Adjust timestamp to remove the System Clock Reference
689 // offset then update the average inter-packet time
690 // for this stream.
691 buf->s.renderOffset -= r->scr_offset;
692 update_ipt( r, buf );
694 #if 0
695 // JAS: This was added to fix a rare "audio time went backward"
696 // sync error I found in one sample. But it has a bad side
697 // effect on DVDs, causing frequent "adding silence" sync
698 // errors. So I am disabling it.
699 else
701 update_ipt( r, buf );
703 #endif
705 if( fifos )
707 if ( !r->start_found )
709 hb_buffer_close( &buf );
710 continue;
713 buf->sequence = r->sequence++;
714 /* if there are mutiple output fifos, send a copy of the
715 * buffer down all but the first (we have to not ship the
716 * original buffer or we'll race with the thread that's
717 * consuming the buffer & inject garbage into the data stream). */
718 for( n = 1; fifos[n] != NULL; n++)
720 hb_buffer_t *buf_copy = hb_buffer_init( buf->size );
721 buf_copy->s = buf->s;
722 memcpy( buf_copy->data, buf->data, buf->size );
723 push_buf( r, fifos[n], buf_copy );
725 push_buf( r, fifos[0], buf );
726 buf = NULL;
728 else
730 hb_buffer_close( &buf );
735 // send empty buffers downstream to video & audio decoders to signal we're done.
736 if( !*r->die && !r->job->done )
738 push_buf(r, r->job->fifo_mpeg2, hb_buffer_eof_init());
740 hb_audio_t *audio;
741 for( n = 0; (audio = hb_list_item( r->job->list_audio, n)); ++n )
743 if ( audio->priv.fifo_in )
744 push_buf(r, audio->priv.fifo_in, hb_buffer_eof_init());
747 hb_subtitle_t *subtitle;
748 for( n = 0; (subtitle = hb_list_item( r->job->list_subtitle, n)); ++n )
750 if ( subtitle->fifo_in && subtitle->source == VOBSUB)
751 push_buf(r, subtitle->fifo_in, hb_buffer_eof_init());
755 hb_list_empty( &list );
757 hb_log( "reader: done. %d scr changes", r->demux.scr_changes );
760 static void UpdateState( hb_work_private_t * r, int64_t start)
762 hb_state_t state;
763 uint64_t now;
764 double avg;
766 now = hb_get_date();
767 if( !r->st_first )
769 r->st_first = now;
772 hb_get_state2(r->job->h, &state);
773 #define p state.param.working
774 if ( !r->job->indepth_scan )
776 state.state = HB_STATE_SEARCHING;
777 p.progress = (float) start / (float) r->job->pts_to_start;
779 else
781 state.state = HB_STATE_WORKING;
782 p.progress = (float) start / (float) r->duration;
784 if( p.progress > 1.0 )
786 p.progress = 1.0;
788 p.rate_cur = 0.0;
789 p.rate_avg = 0.0;
790 if (now > r->st_first)
792 int eta;
794 avg = 1000.0 * (double)start / (now - r->st_first);
795 if ( !r->job->indepth_scan )
796 eta = ( r->job->pts_to_start - start ) / avg;
797 else
798 eta = ( r->duration - start ) / avg;
799 p.hours = eta / 3600;
800 p.minutes = ( eta % 3600 ) / 60;
801 p.seconds = eta % 60;
803 else
805 p.hours = -1;
806 p.minutes = -1;
807 p.seconds = -1;
809 #undef p
811 hb_set_state( r->job->h, &state );
813 /***********************************************************************
814 * GetFifoForId
815 ***********************************************************************
817 **********************************************************************/
818 static hb_fifo_t ** GetFifoForId( hb_work_private_t * r, int id )
820 hb_job_t * job = r->job;
821 hb_title_t * title = job->title;
822 hb_audio_t * audio;
823 hb_subtitle_t * subtitle;
824 int i, n, count;
826 memset(r->fifos, 0, sizeof(r->fifos));
828 if( id == title->video_id )
830 if (job->indepth_scan && !job->frame_to_stop)
833 * Ditch the video here during the indepth scan until
834 * we can improve the MPEG2 decode performance.
836 * But if we specify a stop frame, we must decode the
837 * frames in order to count them.
839 return NULL;
841 else
843 r->fifos[0] = job->fifo_mpeg2;
844 return r->fifos;
848 count = hb_list_count( job->list_subtitle );
849 count = count > 99 ? 99 : count;
850 for( i = n = 0; i < count; i++ )
852 subtitle = hb_list_item( job->list_subtitle, i );
853 if (id == subtitle->id)
855 /* pass the subtitles to be processed */
856 r->fifos[n++] = subtitle->fifo_in;
859 if ( n != 0 )
861 return r->fifos;
864 if( !job->indepth_scan )
866 for( i = n = 0; i < hb_list_count( job->list_audio ); i++ )
868 audio = hb_list_item( job->list_audio, i );
869 if( id == audio->id )
871 r->fifos[n++] = audio->priv.fifo_in;
875 if( n != 0 )
877 return r->fifos;
881 return NULL;