+ return hb_ts_stream_decode( src_stream, b );
+}
+
+int hb_stream_seek_chapter( hb_stream_t * stream, int chapter_num )
+{
+
+ if ( stream->hb_stream_type != ffmpeg )
+ {
+ // currently meaningliess for transport and program streams
+ return 1;
+ }
+ if ( !stream || !stream->title ||
+ chapter_num > hb_list_count( stream->title->list_chapter ) )
+ {
+ return 0;
+ }
+
+ int64_t sum_dur = 0;
+ hb_chapter_t *chapter = NULL;
+ int i;
+ for ( i = 0; i < chapter_num; ++i)
+ {
+ chapter = hb_list_item( stream->title->list_chapter, i );
+ sum_dur += chapter->duration;
+ }
+ stream->chapter = chapter_num - 1;
+ stream->chapter_end = sum_dur;
+
+ int64_t pos = ( ( ( sum_dur - chapter->duration ) * AV_TIME_BASE ) / 90000 );
+
+ hb_deep_log( 2, "Seeking to chapter %d: starts %"PRId64", ends %"PRId64", AV pos %"PRId64,
+ chapter_num, sum_dur - chapter->duration, sum_dur, pos);
+
+ if ( chapter_num > 1 && pos > 0 )
+ {
+ av_seek_frame( stream->ffmpeg_ic, -1, pos, 0);
+ }
+ else
+ {
+ // ffmpeg has a bug that causes the first PTS after
+ // av_find_stream_info() is called to be incorrect.
+ // av_find_stream_info is called whenever opening a file
+ // with ffmpeg. av_seek_frame clears the condition
+ // that causes the problem. since hb_stream_seek_chapter
+ // is called before we start reading, make sure
+ // we do a seek here.
+ av_seek_frame( stream->ffmpeg_ic, -1, 0LL, AVSEEK_FLAG_BACKWARD );
+ }
+ return 1;
+}
+
+/***********************************************************************
+ * hb_stream_chapter
+ ***********************************************************************
+ * Return the number of the chapter that we are currently in. We store
+ * the chapter number starting from 0, so + 1 for the real chpater num.
+ **********************************************************************/
+int hb_stream_chapter( hb_stream_t * src_stream )
+{
+ return( src_stream->chapter + 1 );