/*****************************************************************************
- * mkv.cpp : matroska demuxer
+ * matroska_segment.cpp : matroska demuxer
*****************************************************************************
* Copyright (C) 2003-2010 the VideoLAN team
* $Id$
*****************************************************************************/
#include "matroska_segment.hpp"
-
#include "chapters.hpp"
-
#include "demux.hpp"
-
+#include "util.hpp"
#include "Ebml_parser.hpp"
extern "C" {
,sys(demuxer)
,ep(NULL)
,b_preloaded(false)
+ ,b_ref_external_segments(false)
{
p_indexes = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
}
{
delete tracks[i_track]->p_compression_data;
es_format_Clean( &tracks[i_track]->fmt );
+ delete tracks[i_track]->p_sys;
free( tracks[i_track]->p_extra_data );
free( tracks[i_track]->psz_codec );
delete tracks[i_track];
{vlc_meta_Publisher, "PUBLISHER"},
{vlc_meta_URL, "URL"},
{vlc_meta_TrackNumber, "PART_NUMBER"},
+ {vlc_meta_TrackTotal, "TOTAL_PARTS"},
{vlc_meta_Date, "DATE_RELEASE"},
+ {vlc_meta_EncodedBy, "ENCODED_BY"},
{vlc_meta_Title, NULL},
};
goto done;
}
}
+ msg_Dbg( &sys.demuxer, "| | + %s: %s", k, v);
vlc_meta_AddExtra( sys.meta, k, v );
done:
free( k );
*****************************************************************************/
void matroska_segment_c::InformationCreate( )
{
-#if 0
- sys.meta = vlc_meta_New();
+ if( !sys.meta )
+ sys.meta = vlc_meta_New();
if( psz_title )
{
{
vlc_meta_SetDate( sys.meta, psz_date_utc );
}
+#if 0
if( psz_segment_filename )
{
idx.i_track = -1;
idx.i_block_number= -1;
idx.i_position = cluster->GetElementPosition();
- idx.i_time = -1;
+ idx.i_time = cluster->GlobalTimecode()/ (mtime_t) 1000;
idx.b_key = true;
i_index++;
struct spoint
{
- spoint(unsigned int tk): i_track(tk),i_date(0), i_seek_pos(0), i_cluster_pos(0),
- p_next(NULL){}
+ spoint(unsigned int tk, mtime_t date, int64_t pos, int64_t cpos):
+ i_track(tk),i_date(date), i_seek_pos(pos),
+ i_cluster_pos(cpos), p_next(NULL){}
unsigned int i_track;
mtime_t i_date;
int64_t i_seek_pos;
{
KaxBlock *block;
KaxSimpleBlock *simpleblock;
- int i_track_skipping;
int64_t i_block_duration;
size_t i_track;
int64_t i_seek_position = i_start_pos;
mtime_t i_pts = 0;
spoint *p_first = NULL;
spoint *p_last = NULL;
+ int i_cat;
+ bool b_has_key = false;
+
+ for( size_t i = 0; i < tracks.size(); i++)
+ tracks[i]->i_last_dts = VLC_TS_INVALID;
if( i_global_position >= 0 )
{
/* Special case for seeking in files with no cues */
EbmlElement *el = NULL;
- es.I_O().setFilePointer( i_start_pos, seek_beginning );
+
+ /* Start from the last known index instead of the beginning eachtime */
+ if( i_index == 0)
+ es.I_O().setFilePointer( i_start_pos, seek_beginning );
+ else
+ es.I_O().setFilePointer( p_indexes[ i_index - 1 ].i_position,
+ seek_beginning );
delete ep;
ep = new EbmlParser( &es, segment, &sys.demuxer );
cluster = NULL;
cluster = (KaxCluster *)el;
i_cluster_pos = cluster->GetElementPosition();
if( i_index == 0 ||
- ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
+ ( i_index > 0 &&
+ p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
{
+ ParseCluster(false);
IndexAppendCluster( cluster );
}
if( es.I_O().getFilePointer() >= (unsigned) i_global_position )
- {
- ParseCluster();
- msg_Dbg( &sys.demuxer, "we found a cluster that is in the neighbourhood" );
- return;
- }
+ break;
}
}
- msg_Err( &sys.demuxer, "This file has no cues, and we were unable to seek to the requested position by parsing." );
- return;
}
+#if !defined(WIN32) && !defined(__ANDROID__)
/* Don't try complex seek if we seek to 0 */
if( i_date == 0 )
{
es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, 0 );
+ es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 );
es.I_O().setFilePointer( i_start_pos );
delete ep;
ep = new EbmlParser( &es, segment, &sys.demuxer );
cluster = NULL;
+ sys.i_start_pts = 0;
+ sys.i_pts = 0;
+ sys.i_pcr = 0;
return;
}
+#endif
+ int i_idx = 0;
if ( i_index > 0 )
{
- int i_idx = 0;
for( ; i_idx < i_index; i_idx++ )
if( p_indexes[i_idx].i_time + i_time_offset > i_date )
sys.i_start_pts = i_date;
es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
+
/* now parse until key frame */
- i_track_skipping = 0;
- for( i_track = 0; i_track < tracks.size(); i_track++ )
+ const int es_types[3] = { VIDEO_ES, AUDIO_ES, SPU_ES };
+ i_cat = es_types[0];
+ for( int i = 0; i < 2; i_cat = es_types[++i] )
{
- if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
+ for( i_track = 0; i_track < tracks.size(); i_track++ )
{
- spoint * seekpoint = new spoint(i_track);
- if( unlikely( !seekpoint ) )
+ if( tracks[i_track]->fmt.i_cat == i_cat )
{
- for( spoint * sp = p_first; sp; )
+ spoint * seekpoint = new spoint(i_track, i_seek_time, i_seek_position, i_seek_position);
+ if( unlikely( !seekpoint ) )
{
- spoint * tmp = sp;
- sp = sp->p_next;
- delete tmp;
+ for( spoint * sp = p_first; sp; )
+ {
+ spoint * tmp = sp;
+ sp = sp->p_next;
+ delete tmp;
+ }
+ return;
+ }
+ if( unlikely( !p_first ) )
+ {
+ p_first = seekpoint;
+ p_last = seekpoint;
+ }
+ else
+ {
+ p_last->p_next = seekpoint;
+ p_last = seekpoint;
}
- return;
- }
- if( unlikely( !p_first ) )
- {
- p_first = seekpoint;
- p_last = seekpoint;
- }
- else
- {
- p_last->p_next = seekpoint;
- p_last = seekpoint;
}
}
+ if( likely( p_first ) )
+ break;
}
+ /*Neither video nor audio track... no seek further*/
+ if( unlikely( !p_first ) )
+ return;
- while( i_pts < i_date )
+ for(;;)
{
- bool b_key_picture;
- bool b_discardable_picture;
- if( BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
- {
- msg_Warn( &sys.demuxer, "cannot get block EOF?" );
-
- return;
- }
-
- /* check if block's track is in our list */
- for( i_track = 0; i_track < tracks.size(); i_track++ )
+ while( i_pts < i_date )
{
- if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
- (block && tracks[i_track]->i_number == block->TrackNum()) )
- break;
- }
-
- if( simpleblock )
- i_pts = sys.i_chapter_time + simpleblock->GlobalTimecode() / (mtime_t) 1000;
- else
- i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
+ bool b_key_picture;
+ bool b_discardable_picture;
+ if( BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
+ {
+ msg_Warn( &sys.demuxer, "cannot get block EOF?" );
+ return;
+ }
- if( i_track < tracks.size() )
- {
- if( tracks[i_track]->fmt.i_cat == VIDEO_ES && b_key_picture )
+ /* check if block's track is in our list */
+ for( i_track = 0; i_track < tracks.size(); i_track++ )
{
- /* get the seekpoint */
- spoint * sp;
- for( sp = p_first; sp; sp = sp->p_next )
- if( sp->i_track == i_track )
- break;
+ if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
+ (block && tracks[i_track]->i_number == block->TrackNum()) )
+ break;
+ }
- sp->i_date = i_pts;
- if( simpleblock )
- sp->i_seek_pos = simpleblock->GetElementPosition();
- else
- sp->i_seek_pos = i_block_pos;
- sp->i_cluster_pos = i_cluster_pos;
+ if( simpleblock )
+ i_pts = sys.i_chapter_time + simpleblock->GlobalTimecode() / (mtime_t) 1000;
+ else
+ i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
+ if( i_track < tracks.size() )
+ {
+ if( tracks[i_track]->fmt.i_cat == i_cat && b_key_picture )
+ {
+ /* get the seekpoint */
+ spoint * sp;
+ for( sp = p_first; sp; sp = sp->p_next )
+ if( sp->i_track == i_track )
+ break;
+
+ sp->i_date = i_pts;
+ if( simpleblock )
+ sp->i_seek_pos = simpleblock->GetElementPosition();
+ else
+ sp->i_seek_pos = i_block_pos;
+ sp->i_cluster_pos = i_cluster_pos;
+ b_has_key = true;
+ }
}
+
+ delete block;
}
+ if( b_has_key || !i_idx )
+ break;
- delete block;
+ /* No key picture was found in the cluster seek to previous seekpoint */
+ i_date = i_time_offset + p_indexes[i_idx].i_time;
+ i_idx--;
+ i_pts = 0;
+ es.I_O().setFilePointer( p_indexes[i_idx].i_position );
+ delete ep;
+ ep = new EbmlParser( &es, segment, &sys.demuxer );
+ cluster = NULL;
}
/* rewind to the last I img */
if( p_last->i_date < p_min->i_date )
p_min = p_last;
- sys.i_pts = p_min->i_date;
+ sys.i_pcr = sys.i_pts = p_min->i_date;
+ es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 + sys.i_pcr );
cluster = (KaxCluster *) ep->UnGet( p_min->i_seek_pos, p_min->i_cluster_pos );
/* hack use BlockGet to get the cluster then goto the wanted block */
/* add all es */
msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
+ bool b_has_default_video = false;
+ bool b_has_default_audio = false;
+ /* check for default */
+ for(size_t i_track = 0; i_track < tracks.size(); i_track++)
+ {
+ mkv_track_t *p_tk = tracks[i_track];
+ es_format_t *p_fmt = &p_tk->fmt;
+ if( p_fmt->i_cat == VIDEO_ES )
+ b_has_default_video |=
+ p_tk->b_enabled && ( p_tk->b_default || p_tk->b_forced );
+ else if( p_fmt->i_cat == AUDIO_ES )
+ b_has_default_audio |=
+ p_tk->b_enabled && ( p_tk->b_default || p_tk->b_forced );
+ }
+
for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
{
mkv_track_t *p_tk = tracks[i_track];
es_format_t *p_fmt = &p_tk->fmt;
- if( p_fmt->i_cat == UNKNOWN_ES || !p_tk->psz_codec )
+ if( unlikely( p_fmt->i_cat == UNKNOWN_ES || !p_tk->psz_codec ) )
{
msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, p_tk->i_number );
p_tk->p_es = NULL;
continue;
}
+ else if( unlikely( !b_has_default_video && p_fmt->i_cat == VIDEO_ES ) )
+ {
+ p_tk->b_default = true;
+ b_has_default_video = true;
+ }
+ else if( unlikely( !b_has_default_audio && p_fmt->i_cat == AUDIO_ES ) )
+ {
+ p_tk->b_default = true;
+ b_has_default_audio = true;
+ }
if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
{
- if( p_tk->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
+ if( p_tk->i_extra_data < (int)sizeof( VLC_BITMAPINFOHEADER ) )
{
- msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
+ msg_Err( &sys.demuxer, "missing/invalid VLC_BITMAPINFOHEADER" );
p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
}
else
{
- BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)p_tk->p_extra_data;
+ VLC_BITMAPINFOHEADER *p_bih = (VLC_BITMAPINFOHEADER*)p_tk->p_extra_data;
p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
p_tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
- p_tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
+ p_tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( VLC_BITMAPINFOHEADER );
if( p_tk->fmt.i_extra > 0 )
{
+ /* Very unlikely yet possible: bug #5659*/
+ size_t maxlen = p_tk->i_extra_data - sizeof( VLC_BITMAPINFOHEADER );
+ p_tk->fmt.i_extra = ( (unsigned)p_tk->fmt.i_extra < maxlen )?
+ p_tk->fmt.i_extra : maxlen;
+
p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
}
wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
+ if( p_tk->fmt.i_codec == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
+ msg_Err( &sys.demuxer, "Unrecognized wf tag: 0x%x", GetWLE( &p_wf->wFormatTag ) );
p_tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
p_tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
}
else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
{
- if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
- p_tk->fmt.i_codec = VLC_CODEC_COOK;
- else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
- p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
- else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
- p_tk->fmt.i_codec = VLC_CODEC_RA_288;
- /* FIXME 14_4, RALF and SIPR */
- fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 0 /*78 - FIXME need to implement reading support for cook */ );
- }
- else if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
- {
- p_fmt->i_codec = VLC_CODEC_RA_144;
- p_fmt->audio.i_channels = 1;
- p_fmt->audio.i_rate = 8000;
- p_fmt->audio.i_blockalign = 0x14;
+ if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
+ {
+ p_fmt->i_codec = VLC_CODEC_RA_144;
+ p_fmt->audio.i_channels = 1;
+ p_fmt->audio.i_rate = 8000;
+ p_fmt->audio.i_blockalign = 0x14;
+ }
+ else if( p_tk->i_extra_data > 28 )
+ {
+ uint8_t *p = p_tk->p_extra_data;
+ if( memcmp( p, ".ra", 3 ) ) {
+ msg_Err( &sys.demuxer, "Invalid Real ExtraData 0x%4.4s", (char *)p );
+ p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
+ }
+ else
+ {
+ real_audio_private * priv = (real_audio_private*) p_tk->p_extra_data;
+ if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
+ {
+ p_tk->fmt.i_codec = VLC_CODEC_COOK;
+ p_tk->fmt.audio.i_blockalign = hton16(priv->sub_packet_size);
+ }
+ else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
+ p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
+ else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
+ p_tk->fmt.i_codec = VLC_CODEC_RA_288;
+ /* FIXME RALF and SIPR */
+ uint16_t version = (uint16_t) hton16(priv->version);
+ p_tk->p_sys =
+ new Cook_PrivateTrackData( hton16(priv->sub_packet_h),
+ hton16(priv->frame_size),
+ hton16(priv->sub_packet_size));
+ if( unlikely( !p_tk->p_sys ) )
+ continue;
+
+ if( unlikely( p_tk->p_sys->Init() ) )
+ continue;
+
+ if( version == 4 )
+ {
+ real_audio_private_v4 * v4 = (real_audio_private_v4*) priv;
+ p_tk->fmt.audio.i_channels = hton16(v4->channels);
+ p_tk->fmt.audio.i_bitspersample = hton16(v4->sample_size);
+ p_tk->fmt.audio.i_rate = hton16(v4->sample_rate);
+ }
+ else if( version == 5 )
+ {
+ real_audio_private_v5 * v5 = (real_audio_private_v5*) priv;
+ p_tk->fmt.audio.i_channels = hton16(v5->channels);
+ p_tk->fmt.audio.i_bitspersample = hton16(v5->sample_size);
+ p_tk->fmt.audio.i_rate = hton16(v5->sample_rate);
+ }
+ msg_Dbg(&sys.demuxer, "%d channels %d bits %d Hz",p_tk->fmt.audio.i_channels, p_tk->fmt.audio.i_bitspersample, p_tk->fmt.audio.i_rate);
+
+ fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 78);
+ }
+ }
}
else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
{
msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
}
- if( p_tk->b_default )
- {
- p_tk->fmt.i_priority = 1000;
- }
+ if( unlikely( !p_tk->b_enabled ) )
+ p_tk->fmt.i_priority = -2;
+ else if( p_tk->b_forced )
+ p_tk->fmt.i_priority = 2;
+ else if( p_tk->b_default )
+ p_tk->fmt.i_priority = 1;
+ else
+ p_tk->fmt.i_priority = 0;
+
+ /* Avoid multivideo tracks when unnecessary */
+ if( p_tk->fmt.i_cat == VIDEO_ES )
+ p_tk->fmt.i_priority--;
p_tk->p_es = es_out_Add( sys.demuxer.out, &p_tk->fmt );
cluster = (KaxCluster*)el;
i_cluster_pos = cluster->GetElementPosition();
- /* add it to the index */
- if( i_index == 0 ||
- ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
- {
- IndexAppendCluster( cluster );
- }
-
// reset silent tracks
for (size_t i=0; i<tracks.size(); i++)
{
ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
cluster->InitTimecode( uint64( ctc ), i_timescale );
+
+ /* add it to the index */
+ if( i_index == 0 ||
+ ( i_index > 0 &&
+ p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
+ IndexAppendCluster( cluster );
}
else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
{