static int hb_stream_check_for_ps(const uint8_t *buf)
{
- // program streams should start with a PACK then some other mpeg start
- // code (usually a SYS but that might be missing if we only have a clip).
- return check_ps_sync(buf) && check_ps_sc(buf);
+ // program streams should start with a PACK then some other mpeg start
+ // code (usually a SYS but that might be missing if we only have a clip).
+ int offset = 0;
+
+ for ( offset = 0; offset < 8*1024-24; ++offset )
+ {
+ if ( check_ps_sync( &buf[offset] ) && check_ps_sc( &buf[offset] ) )
+ return 1;
+ }
+ return 0;
}
static int hb_stream_check_for_dvd_ps(const uint8_t *buf)
static int hb_stream_get_type(hb_stream_t *stream)
{
uint8_t buf[2048*4];
+ int i = 64;
if ( fread(buf, 1, sizeof(buf), stream->file_handle) == sizeof(buf) )
{
stream->hb_stream_type = dvd_program;
return 1;
}
- if ( hb_stream_check_for_ps(buf) != 0 )
+ do
{
- hb_log("file is MPEG Program Stream");
- stream->hb_stream_type = program;
- return 1;
- }
+ if ( hb_stream_check_for_ps(buf) != 0 )
+ {
+ hb_log("file is MPEG Program Stream");
+ stream->hb_stream_type = program;
+ return 1;
+ }
+ // Seek back to handle start codes that run over end of last buffer
+ fseek( stream->file_handle, -28, SEEK_CUR );
+ } while ( --i && fread(buf, 1, sizeof(buf), stream->file_handle) == sizeof(buf) );
}
return 0;
}
ep = b->data + b->alloc;
}
*cp++ = c;
+ // Non-video streams can emulate start codes, so we need
+ // to inspect PES packets and skip over their data
+ // sections to avoid mis-detection of the next pack header.
+ if ( ( strt_code >> 8 ) == 0x000001 &&
+ ( strt_code & 0xff ) >= 0xbb )
+ {
+ int len = 0;
+ c = getc_unlocked( src_stream->file_handle );
+ if ( c == EOF )
+ break;
+ len = c << 8;
+ c = getc_unlocked( src_stream->file_handle );
+ if ( c == EOF )
+ break;
+ len |= c;
+ if ( cp+len+2 > ep )
+ {
+ // need to expand the buffer
+ int curSize = cp - b->data;
+ if ( curSize * 2 > curSize+len+2 )
+ hb_buffer_realloc( b, curSize * 2 );
+ else
+ hb_buffer_realloc( b, curSize + len + 2 );
+ cp = b->data + curSize;
+ ep = b->data + b->alloc;
+ }
+ *cp++ = len >> 8;
+ *cp++ = len & 0xff;
+ fread( cp, 1, len, src_stream->file_handle );
+ cp += len;
+ }
}
funlockfile( src_stream->file_handle );
if ( c != EOF )
{
fseeko( src_stream->file_handle, -4, SEEK_CUR );
- b->size -= 4;
+ // Only 3 of the 4 bytes read were added to the buffer.
+ b->size -= 3;
}
return 1;
}
{
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;
}