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
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
=
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
32 int is_audio
; // != 0 if this is an audio stream
33 int valid
; // Stream timing is not valid until next scr.
36 struct hb_work_private_s
47 stream_timing_t
*stream_timing
;
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
61 hb_fifo_t
* fifos
[100];
64 /***********************************************************************
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 /***********************************************************************
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
) ) )
82 else if ( r
->title
->type
== HB_DVD_TYPE
)
84 if ( !( r
->dvd
= hb_dvd_init( r
->h
, r
->title
->path
) ) )
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)))
95 // Unknown type, should never happen
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 );
110 r
->title
= job
->title
;
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
/
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
)
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
;
150 hb_chapter_t
*chapter
;
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
);
174 static void hb_reader_close( hb_work_object_t
* w
)
176 hb_work_private_t
* r
= w
->private_data
;
181 hb_bd_close( &r
->bd
);
185 hb_dvd_stop( r
->dvd
);
186 hb_dvd_close( &r
->dvd
);
190 hb_stream_close(&r
->stream
);
193 if ( r
->stream_timing
)
195 free( r
->stream_timing
);
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
);
214 hb_buffer_close( &buf
);
218 static int is_audio( hb_work_private_t
*r
, int id
)
223 for( i
= 0; ( audio
= hb_list_item( r
->title
->list_audio
, i
) ); ++i
)
225 if ( audio
->id
== id
)
233 static int is_subtitle( hb_work_private_t
*r
, int id
)
238 for( i
= 0; ( sub
= hb_list_item( r
->title
->list_subtitle
, i
) ); ++i
)
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)
265 // if we haven't seen this stream add it.
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
)
275 r
->stream_timing
= realloc( r
->stream_timing
, r
->st_slots
*
276 sizeof(*r
->stream_timing
) );
277 st
= r
->stream_timing
+ slot
;
280 st
->average
= 30.*90.;
281 st
->filtered_average
= st
->average
;
283 st
->last
= -st
->average
;
284 if ( ( st
->is_audio
= is_audio( r
, buf
->s
.id
) ) != 0 )
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
;
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 )
315 st
->average
+= ( dt
- st
->average
) * (1./4.);
320 st
->average
+= ( dt
- st
->average
) * (1./32.);
323 if (dt
< 1.5 * st
->average
)
325 st
->filtered_average
+= ( dt
- st
->filtered_average
) * (1./32.);
328 st
->last
= buf
->s
.renderOffset
;
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 );
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
;
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 /***********************************************************************
364 ***********************************************************************
366 **********************************************************************/
367 void ReadLoop( void * _w
)
369 hb_work_object_t
* w
= _w
;
370 hb_work_private_t
* r
= w
->private_data
;
372 hb_buffer_t
* buf
= NULL
;
376 int chapter_end
= r
->job
->chapter_end
;
381 if( !hb_bd_start( r
->bd
, r
->title
) )
383 hb_bd_close( &r
->bd
);
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;
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 );
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
;
426 chap
= hb_list_item( r
->job
->list_chapter
, start
- 1 );
429 /* end chapter mapping XXX */
431 if( !hb_dvd_start( r
->dvd
, r
->title
, start
) )
433 hb_dvd_close( &r
->dvd
);
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.
465 r
->duration
-= r
->job
->pts_to_start
;
467 // hb_stream_seek_ts does nothing for TS streams and will return
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
;
482 chap
= hb_list_item( r
->job
->list_chapter
, start
- 1 );
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
)
497 chapter
= hb_bd_chapter( r
->bd
);
499 chapter
= hb_dvd_chapter( r
->dvd
);
501 chapter
= hb_stream_chapter( r
->stream
);
505 hb_log( "reader: end of the title reached" );
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
);
519 if( (buf
= hb_bd_read( r
->bd
)) == NULL
)
526 if( (buf
= hb_dvd_read( r
->dvd
)) == NULL
)
533 if ( (buf
= hb_stream_read( r
->stream
)) == NULL
)
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
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;
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
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 );
580 hb_log( "reader: first SCR %"PRId64
" id 0x%x DTS %"PRId64
,
581 r
->demux
.last_scr
, buf
->s
.id
, buf
->s
.renderOffset
);
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
605 ( st
== r
->stream_timing
&& !r
->saw_audio
) )
607 new_scr_offset( r
, buf
);
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
)
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
;
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
);
659 if (!r
->start_found
&& start
>= r
->pts_to_start
)
661 // pts_to_start point found
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
);
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
691 buf
->s
.renderOffset
-= r
->scr_offset
;
692 update_ipt( r
, buf
);
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.
701 update_ipt( r
, buf
);
707 if ( !r
->start_found
)
709 hb_buffer_close( &buf
);
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
);
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());
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
)
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
;
781 state
.state
= HB_STATE_WORKING
;
782 p
.progress
= (float) start
/ (float) r
->duration
;
784 if( p
.progress
> 1.0 )
790 if (now
> r
->st_first
)
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
;
798 eta
= ( r
->duration
- start
) / avg
;
799 p
.hours
= eta
/ 3600;
800 p
.minutes
= ( eta
% 3600 ) / 60;
801 p
.seconds
= eta
% 60;
811 hb_set_state( r
->job
->h
, &state
);
813 /***********************************************************************
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
;
823 hb_subtitle_t
* subtitle
;
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.
843 r
->fifos
[0] = job
->fifo_mpeg2
;
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
;
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
;