1 /*****************************************************************************
2 * matroska_segment.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2010 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Steve Lhomme <steve.lhomme@free.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "matroska_segment.hpp"
26 #include "chapters.hpp"
29 #include "Ebml_parser.hpp"
32 #include "../vobsub.h"
35 #include <vlc_codecs.h>
37 /* GetFourCC helper */
38 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
39 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
41 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
44 matroska_segment_c::matroska_segment_c( demux_sys_t & demuxer, EbmlStream & estream )
47 ,i_timescale(MKVD_TIMECODESCALE)
51 ,i_seekhead_position(-1)
53 ,i_tracks_position(-1)
55 ,i_chapters_position(-1)
57 ,i_attachments_position(-1)
63 ,p_prev_segment_uid(NULL)
64 ,p_next_segment_uid(NULL)
68 ,psz_muxing_application(NULL)
69 ,psz_writing_application(NULL)
70 ,psz_segment_filename(NULL)
77 ,b_ref_external_segments(false)
79 p_indexes = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
82 matroska_segment_c::~matroska_segment_c()
84 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
86 delete tracks[i_track]->p_compression_data;
87 es_format_Clean( &tracks[i_track]->fmt );
88 delete tracks[i_track]->p_sys;
89 free( tracks[i_track]->p_extra_data );
90 free( tracks[i_track]->psz_codec );
91 delete tracks[i_track];
94 free( psz_writing_application );
95 free( psz_muxing_application );
96 free( psz_segment_filename );
103 delete p_segment_uid;
104 delete p_prev_segment_uid;
105 delete p_next_segment_uid;
107 vlc_delete_all( stored_editions );
108 vlc_delete_all( translations );
109 vlc_delete_all( families );
113 /*****************************************************************************
115 *****************************************************************************
116 * * LoadCues : load the cues element and update index
117 * * LoadTags : load ... the tags element
118 * * InformationCreate : create all information, load tags if present
119 *****************************************************************************/
120 void matroska_segment_c::LoadCues( KaxCues *cues )
127 msg_Err( &sys.demuxer, "There can be only 1 Cues per section." );
131 ep = new EbmlParser( &es, cues, &sys.demuxer );
132 while( ( el = ep->Get() ) != NULL )
134 if( MKV_IS_ID( el, KaxCuePoint ) )
136 #define idx p_indexes[i_index]
139 idx.i_block_number= -1;
145 while( ( el = ep->Get() ) != NULL )
147 if( MKV_IS_ID( el, KaxCueTime ) )
149 KaxCueTime &ctime = *(KaxCueTime*)el;
151 ctime.ReadData( es.I_O() );
153 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
155 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
158 while( ( el = ep->Get() ) != NULL )
160 if( MKV_IS_ID( el, KaxCueTrack ) )
162 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
164 ctrack.ReadData( es.I_O() );
165 idx.i_track = uint16( ctrack );
167 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
169 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
171 ccpos.ReadData( es.I_O() );
172 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
174 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
176 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
178 cbnum.ReadData( es.I_O() );
179 idx.i_block_number = uint32( cbnum );
183 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
190 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
196 msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
197 " track=%d bnum=%d", idx.i_time, idx.i_position,
198 idx.i_track, idx.i_block_number );
202 if( i_index >= i_index_max )
205 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
206 sizeof( mkv_index_t ) * i_index_max );
212 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
217 msg_Dbg( &sys.demuxer, "| - loading cues done." );
221 #define PARSE_TAG( type ) \
223 msg_Dbg( &sys.demuxer, "| + " type ); \
225 while( ( el = ep->Get() ) != NULL ) \
227 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() ); \
229 ep->Up(); } while( 0 )
231 static const struct {
232 vlc_meta_type_t type;
234 } metadata_map[] = { {vlc_meta_Title, "TITLE"},
235 {vlc_meta_Artist, "ARTIST"},
236 {vlc_meta_Genre, "GENRE"},
237 {vlc_meta_Copyright, "COPYRIGHT"},
238 {vlc_meta_Description, "DESCRIPTION"},
239 {vlc_meta_Publisher, "PUBLISHER"},
240 {vlc_meta_URL, "URL"},
241 {vlc_meta_TrackNumber, "PART_NUMBER"},
242 {vlc_meta_TrackTotal, "TOTAL_PARTS"},
243 {vlc_meta_Date, "DATE_RELEASE"},
244 {vlc_meta_EncodedBy, "ENCODED_BY"},
245 {vlc_meta_Title, NULL},
248 void matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag )
251 EbmlParser *ep = new EbmlParser( &es, tag, &sys.demuxer );
252 char *k = NULL, *v = NULL;
255 sys.meta = vlc_meta_New();
257 msg_Dbg( &sys.demuxer, "| + Simple Tag ");
258 while( ( el = ep->Get() ) != NULL )
260 if( MKV_IS_ID( el, KaxTagName ) )
262 KaxTagName &key = *(KaxTagName*)el;
263 key.ReadData( es.I_O(), SCOPE_ALL_DATA );
264 k = strdup( UTFstring( key ).GetUTF8().c_str() );
266 if( MKV_IS_ID( el, KaxTagString ) )
268 KaxTagString &value = *(KaxTagString*)el;
269 value.ReadData( es.I_O(), SCOPE_ALL_DATA );
270 v = strdup( UTFstring( value ).GetUTF8().c_str() );
277 msg_Warn( &sys.demuxer, "Invalid MKV SimpleTag found.");
281 for( int i = 0; metadata_map[i].key; i++ )
283 if( !strcmp( k, metadata_map[i].key ) )
285 vlc_meta_Set( sys.meta, metadata_map[i].type, v );
289 msg_Dbg( &sys.demuxer, "| | + %s: %s", k, v);
290 vlc_meta_AddExtra( sys.meta, k, v );
297 void matroska_segment_c::LoadTags( KaxTags *tags )
299 /* Master elements */
300 EbmlParser *ep = new EbmlParser( &es, tags, &sys.demuxer );
303 while( ( el = ep->Get() ) != NULL )
305 if( MKV_IS_ID( el, KaxTag ) )
307 msg_Dbg( &sys.demuxer, "+ Tag" );
309 while( ( el = ep->Get() ) != NULL )
311 if( MKV_IS_ID( el, KaxTagTargets ) )
312 PARSE_TAG( "Targets" );
313 #if 0 // not valid anymore
314 else if( MKV_IS_ID( el, KaxTagGeneral ) )
315 PARSE_TAG( "General" );
316 else if( MKV_IS_ID( el, KaxTagGenres ) )
317 PARSE_TAG( "Genres" );
318 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
319 PARSE_TAG( "Audio Specific" );
320 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
321 PARSE_TAG( "Images Specific" );
322 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
324 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
326 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
328 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
330 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
332 msg_Dbg( &sys.demuxer, "| + Multi Date" );
334 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
336 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
338 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
340 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
342 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
344 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
346 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
348 msg_Dbg( &sys.demuxer, "| + Multi Title" );
351 else if( MKV_IS_ID( el, KaxTagSimple ) )
352 ParseSimpleTags( static_cast<KaxTagSimple*>( el ) );
355 msg_Dbg( &sys.demuxer, "| + LoadTag Unknown (%s)", typeid( *el ).name() );
362 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
367 msg_Dbg( &sys.demuxer, "loading tags done." );
371 /*****************************************************************************
373 *****************************************************************************/
374 void matroska_segment_c::InformationCreate( )
377 sys.meta = vlc_meta_New();
381 vlc_meta_SetTitle( sys.meta, psz_title );
385 vlc_meta_SetDate( sys.meta, psz_date_utc );
389 if( psz_segment_filename )
391 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
393 if( psz_muxing_application )
395 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
397 if( psz_writing_application )
399 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
402 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
404 // mkv_track_t *tk = tracks[i_track];
405 // vlc_meta_t *mtk = vlc_meta_New();
406 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
410 if( i_tags_position >= 0 )
414 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
424 /*****************************************************************************
426 *****************************************************************************/
428 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
430 #define idx p_indexes[i_index]
432 idx.i_block_number= -1;
433 idx.i_position = cluster->GetElementPosition();
434 idx.i_time = cluster->GlobalTimecode()/ (mtime_t) 1000;
438 if( i_index >= i_index_max )
441 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
442 sizeof( mkv_index_t ) * i_index_max );
447 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
452 for (size_t i=0; i<families.size(); i++)
454 for (size_t j=0; j<of_segment.families.size(); j++)
456 if ( *(families[i]) == *(of_segment.families[j]) )
464 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
468 if ( p_item_a == NULL || p_item_b == NULL )
471 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
472 if ( p_item_b->p_prev_segment_uid != NULL
473 && *p_tmp == *p_item_b->p_prev_segment_uid )
476 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
480 if ( p_item_b->p_segment_uid != NULL
481 && *p_tmp == *p_item_b->p_segment_uid )
484 if ( p_item_b->p_prev_segment_uid != NULL
485 && *p_tmp == *p_item_b->p_prev_segment_uid )
491 bool matroska_segment_c::Preload( )
496 EbmlElement *el = NULL;
498 ep->Reset( &sys.demuxer );
500 while( ( el = ep->Get() ) != NULL )
502 if( MKV_IS_ID( el, KaxSeekHead ) )
504 /* Multiple allowed */
505 /* We bail at 10, to prevent possible recursion */
506 msg_Dbg( &sys.demuxer, "| + Seek head" );
507 if( i_seekhead_count < 10 )
509 i_seekhead_position = (int64_t) es.I_O().getFilePointer();
510 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
513 else if( MKV_IS_ID( el, KaxInfo ) )
515 /* Multiple allowed, mandatory */
516 msg_Dbg( &sys.demuxer, "| + Information" );
517 if( i_info_position < 0 ) // FIXME
518 ParseInfo( static_cast<KaxInfo*>( el ) );
519 i_info_position = (int64_t) es.I_O().getFilePointer();
521 else if( MKV_IS_ID( el, KaxTracks ) )
523 /* Multiple allowed */
524 msg_Dbg( &sys.demuxer, "| + Tracks" );
525 if( i_tracks_position < 0 ) // FIXME
526 ParseTracks( static_cast<KaxTracks*>( el ) );
527 if ( tracks.size() == 0 )
529 msg_Err( &sys.demuxer, "No tracks supported" );
532 i_tracks_position = (int64_t) es.I_O().getFilePointer();
534 else if( MKV_IS_ID( el, KaxCues ) )
536 msg_Dbg( &sys.demuxer, "| + Cues" );
537 if( i_cues_position < 0 )
538 LoadCues( static_cast<KaxCues*>( el ) );
539 i_cues_position = (int64_t) es.I_O().getFilePointer();
541 else if( MKV_IS_ID( el, KaxCluster ) )
543 msg_Dbg( &sys.demuxer, "| + Cluster" );
545 cluster = (KaxCluster*)el;
547 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
551 /* stop pre-parsing the stream */
554 else if( MKV_IS_ID( el, KaxAttachments ) )
556 msg_Dbg( &sys.demuxer, "| + Attachments" );
557 if( i_attachments_position < 0 )
558 ParseAttachments( static_cast<KaxAttachments*>( el ) );
559 i_attachments_position = (int64_t) es.I_O().getFilePointer();
561 else if( MKV_IS_ID( el, KaxChapters ) )
563 msg_Dbg( &sys.demuxer, "| + Chapters" );
564 if( i_chapters_position < 0 )
565 ParseChapters( static_cast<KaxChapters*>( el ) );
566 i_chapters_position = (int64_t) es.I_O().getFilePointer();
568 else if( MKV_IS_ID( el, KaxTag ) )
570 msg_Dbg( &sys.demuxer, "| + Tags" );
571 /*FIXME if( i_tags_position < 0)
572 LoadTags( static_cast<KaxTags*>( el ) );*/
573 i_tags_position = (int64_t) es.I_O().getFilePointer();
575 else if( MKV_IS_ID( el, EbmlVoid ) )
576 msg_Dbg( &sys.demuxer, "| + Void" );
578 msg_Dbg( &sys.demuxer, "| + Preload Unknown (%s)", typeid(*el).name() );
586 /* Here we try to load elements that were found in Seek Heads, but not yet parsed */
587 bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position )
589 int64_t i_sav_position = (int64_t)es.I_O().getFilePointer();
592 es.I_O().setFilePointer( i_element_position, seek_beginning );
593 el = es.FindNextID( ClassInfos, 0xFFFFFFFFL);
597 msg_Err( &sys.demuxer, "cannot load some cues/chapters/tags etc. (broken seekhead or file)" );
598 es.I_O().setFilePointer( i_sav_position, seek_beginning );
602 if( MKV_IS_ID( el, KaxSeekHead ) )
604 /* Multiple allowed */
605 msg_Dbg( &sys.demuxer, "| + Seek head" );
606 if( i_seekhead_count < 10 )
608 i_seekhead_position = i_element_position;
609 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
612 else if( MKV_IS_ID( el, KaxInfo ) ) // FIXME
614 /* Multiple allowed, mandatory */
615 msg_Dbg( &sys.demuxer, "| + Information" );
616 if( i_info_position < 0 )
617 ParseInfo( static_cast<KaxInfo*>( el ) );
618 i_info_position = i_element_position;
620 else if( MKV_IS_ID( el, KaxTracks ) ) // FIXME
622 /* Multiple allowed */
623 msg_Dbg( &sys.demuxer, "| + Tracks" );
624 if( i_tracks_position < 0 )
625 ParseTracks( static_cast<KaxTracks*>( el ) );
626 if ( tracks.size() == 0 )
628 msg_Err( &sys.demuxer, "No tracks supported" );
630 es.I_O().setFilePointer( i_sav_position, seek_beginning );
633 i_tracks_position = i_element_position;
635 else if( MKV_IS_ID( el, KaxCues ) )
637 msg_Dbg( &sys.demuxer, "| + Cues" );
638 if( i_cues_position < 0 )
639 LoadCues( static_cast<KaxCues*>( el ) );
640 i_cues_position = i_element_position;
642 else if( MKV_IS_ID( el, KaxAttachments ) )
644 msg_Dbg( &sys.demuxer, "| + Attachments" );
645 if( i_attachments_position < 0 )
646 ParseAttachments( static_cast<KaxAttachments*>( el ) );
647 i_attachments_position = i_element_position;
649 else if( MKV_IS_ID( el, KaxChapters ) )
651 msg_Dbg( &sys.demuxer, "| + Chapters" );
652 if( i_chapters_position < 0 )
653 ParseChapters( static_cast<KaxChapters*>( el ) );
654 i_chapters_position = i_element_position;
656 else if( MKV_IS_ID( el, KaxTags ) )
658 msg_Dbg( &sys.demuxer, "| + Tags" );
659 if( i_tags_position < 0 )
660 LoadTags( static_cast<KaxTags*>( el ) );
661 i_tags_position = i_element_position;
665 msg_Dbg( &sys.demuxer, "| + LoadSeekHeadItem Unknown (%s)", typeid(*el).name() );
669 es.I_O().setFilePointer( i_sav_position, seek_beginning );
675 spoint(unsigned int tk, mtime_t date, int64_t pos, int64_t cpos):
676 i_track(tk),i_date(date), i_seek_pos(pos),
677 i_cluster_pos(cpos), p_next(NULL){}
678 unsigned int i_track;
681 int64_t i_cluster_pos;
685 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_global_position )
688 KaxSimpleBlock *simpleblock;
689 int64_t i_block_duration;
691 int64_t i_seek_position = i_start_pos;
692 int64_t i_seek_time = i_start_time;
694 spoint *p_first = NULL;
695 spoint *p_last = NULL;
697 bool b_has_key = false;
699 for( size_t i = 0; i < tracks.size(); i++)
700 tracks[i]->i_last_dts = VLC_TS_INVALID;
702 if( i_global_position >= 0 )
704 /* Special case for seeking in files with no cues */
705 EbmlElement *el = NULL;
707 /* Start from the last known index instead of the beginning eachtime */
709 es.I_O().setFilePointer( i_start_pos, seek_beginning );
711 es.I_O().setFilePointer( p_indexes[ i_index - 1 ].i_position,
714 ep = new EbmlParser( &es, segment, &sys.demuxer );
717 while( ( el = ep->Get() ) != NULL )
719 if( MKV_IS_ID( el, KaxCluster ) )
721 cluster = (KaxCluster *)el;
722 i_cluster_pos = cluster->GetElementPosition();
725 p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
728 IndexAppendCluster( cluster );
730 if( es.I_O().getFilePointer() >= (unsigned) i_global_position )
736 #if !defined(WIN32) && !defined(__ANDROID__)
737 /* Don't try complex seek if we seek to 0 */
740 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, 0 );
741 es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 );
742 es.I_O().setFilePointer( i_start_pos );
745 ep = new EbmlParser( &es, segment, &sys.demuxer );
758 for( ; i_idx < i_index; i_idx++ )
759 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
765 i_seek_position = p_indexes[i_idx].i_position;
766 i_seek_time = p_indexes[i_idx].i_time;
769 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
770 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
772 es.I_O().setFilePointer( i_seek_position, seek_beginning );
775 ep = new EbmlParser( &es, segment, &sys.demuxer );
778 sys.i_start_pts = i_date;
780 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
782 /* now parse until key frame */
783 const int es_types[3] = { VIDEO_ES, AUDIO_ES, SPU_ES };
785 for( int i = 0; i < 2; i_cat = es_types[++i] )
787 for( i_track = 0; i_track < tracks.size(); i_track++ )
789 if( tracks[i_track]->fmt.i_cat == i_cat )
791 spoint * seekpoint = new spoint(i_track, i_seek_time, i_seek_position, i_seek_position);
792 if( unlikely( !seekpoint ) )
794 for( spoint * sp = p_first; sp; )
802 if( unlikely( !p_first ) )
809 p_last->p_next = seekpoint;
814 if( likely( p_first ) )
817 /*Neither video nor audio track... no seek further*/
818 if( unlikely( !p_first ) )
823 while( i_pts < i_date )
826 bool b_discardable_picture;
827 if( BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
829 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
833 /* check if block's track is in our list */
834 for( i_track = 0; i_track < tracks.size(); i_track++ )
836 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
837 (block && tracks[i_track]->i_number == block->TrackNum()) )
842 i_pts = sys.i_chapter_time + simpleblock->GlobalTimecode() / (mtime_t) 1000;
844 i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
845 if( i_track < tracks.size() )
847 if( tracks[i_track]->fmt.i_cat == i_cat && b_key_picture )
849 /* get the seekpoint */
851 for( sp = p_first; sp; sp = sp->p_next )
852 if( sp->i_track == i_track )
857 sp->i_seek_pos = simpleblock->GetElementPosition();
859 sp->i_seek_pos = i_block_pos;
860 sp->i_cluster_pos = i_cluster_pos;
867 if( b_has_key || !i_idx )
870 /* No key picture was found in the cluster seek to previous seekpoint */
871 i_date = i_time_offset + p_indexes[i_idx].i_time;
874 es.I_O().setFilePointer( p_indexes[i_idx].i_position );
876 ep = new EbmlParser( &es, segment, &sys.demuxer );
880 /* rewind to the last I img */
882 for( p_min = p_first, p_last = p_first; p_last; p_last = p_last->p_next )
883 if( p_last->i_date < p_min->i_date )
886 sys.i_pcr = sys.i_pts = p_min->i_date;
887 es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 + sys.i_pcr );
888 cluster = (KaxCluster *) ep->UnGet( p_min->i_seek_pos, p_min->i_cluster_pos );
890 /* hack use BlockGet to get the cluster then goto the wanted block */
894 bool b_discardable_picture;
895 BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration );
897 cluster = (KaxCluster *) ep->UnGet( p_min->i_seek_pos, p_min->i_cluster_pos );
903 p_first = p_first->p_next;
908 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
909 const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
912 for( i_track = 0; i_track < tracks.size(); i_track++ )
914 const mkv_track_t *tk = tracks[i_track];
916 if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
917 ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
923 if( i_track >= tracks.size() )
931 static inline void fill_extra_data( mkv_track_t *p_tk, unsigned int offset )
933 if(p_tk->i_extra_data <= offset) return;
934 p_tk->fmt.i_extra = p_tk->i_extra_data - offset;
935 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
936 if(!p_tk->fmt.p_extra) { p_tk->fmt.i_extra = 0; return; };
937 memcpy( p_tk->fmt.p_extra, p_tk->p_extra_data + offset, p_tk->fmt.i_extra );
940 bool matroska_segment_c::Select( mtime_t i_start_time )
943 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
945 bool b_has_default_video = false;
946 bool b_has_default_audio = false;
947 /* check for default */
948 for(size_t i_track = 0; i_track < tracks.size(); i_track++)
950 mkv_track_t *p_tk = tracks[i_track];
951 es_format_t *p_fmt = &p_tk->fmt;
952 if( p_fmt->i_cat == VIDEO_ES )
953 b_has_default_video |=
954 p_tk->b_enabled && ( p_tk->b_default || p_tk->b_forced );
955 else if( p_fmt->i_cat == AUDIO_ES )
956 b_has_default_audio |=
957 p_tk->b_enabled && ( p_tk->b_default || p_tk->b_forced );
960 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
962 mkv_track_t *p_tk = tracks[i_track];
963 es_format_t *p_fmt = &p_tk->fmt;
965 if( unlikely( p_fmt->i_cat == UNKNOWN_ES || !p_tk->psz_codec ) )
967 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, p_tk->i_number );
971 else if( unlikely( !b_has_default_video && p_fmt->i_cat == VIDEO_ES ) )
973 p_tk->b_default = true;
974 b_has_default_video = true;
976 else if( unlikely( !b_has_default_audio && p_fmt->i_cat == AUDIO_ES ) )
978 p_tk->b_default = true;
979 b_has_default_audio = true;
982 if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
984 if( p_tk->i_extra_data < (int)sizeof( VLC_BITMAPINFOHEADER ) )
986 msg_Err( &sys.demuxer, "missing/invalid VLC_BITMAPINFOHEADER" );
987 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
991 VLC_BITMAPINFOHEADER *p_bih = (VLC_BITMAPINFOHEADER*)p_tk->p_extra_data;
993 p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
994 p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
995 p_tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
997 p_tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( VLC_BITMAPINFOHEADER );
998 if( p_tk->fmt.i_extra > 0 )
1000 /* Very unlikely yet possible: bug #5659*/
1001 size_t maxlen = p_tk->i_extra_data - sizeof( VLC_BITMAPINFOHEADER );
1002 p_tk->fmt.i_extra = ( (unsigned)p_tk->fmt.i_extra < maxlen )?
1003 p_tk->fmt.i_extra : maxlen;
1005 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1006 memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
1009 p_tk->b_dts_only = true;
1011 else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
1012 !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
1014 p_tk->fmt.i_codec = VLC_CODEC_MPGV;
1015 if( p_tk->i_extra_data )
1016 fill_extra_data( p_tk, 0 );
1018 else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
1020 p_tk->fmt.i_codec = VLC_CODEC_THEORA;
1021 fill_extra_data( p_tk, 0 );
1022 p_tk->b_pts_only = true;
1024 else if( !strncmp( p_tk->psz_codec, "V_REAL/RV", 9 ) )
1026 uint8_t *p = p_tk->p_extra_data;
1028 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
1029 p_fmt->i_codec = VLC_CODEC_RV10;
1030 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
1031 p_fmt->i_codec = VLC_CODEC_RV20;
1032 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
1033 p_fmt->i_codec = VLC_CODEC_RV30;
1034 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
1035 p_fmt->i_codec = VLC_CODEC_RV40;
1037 /* Extract the framerate from the header */
1038 if( p_tk->i_extra_data >= 26 &&
1039 p[4] == 'V' && p[5] == 'I' && p[6] == 'D' && p[7] == 'O' &&
1040 p[8] == 'R' && p[9] == 'V' &&
1041 (p[10] == '3' || p[10] == '4') && p[11] == '0' )
1043 p_tk->fmt.video.i_frame_rate =
1044 p[22] << 24 | p[23] << 16 | p[24] << 8 | p[25] << 0;
1045 p_tk->fmt.video.i_frame_rate_base = 65536;
1048 fill_extra_data( p_tk, 26 );
1049 p_tk->b_dts_only = true;
1051 else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
1053 p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
1055 else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
1057 p_tk->fmt.i_codec = VLC_CODEC_VP8;
1058 p_tk->b_pts_only = true;
1060 else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
1062 if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
1064 p_tk->fmt.i_codec = VLC_CODEC_DIV3;
1066 else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
1068 /* A MPEG 4 codec, SP, ASP, AP or AVC */
1069 if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1070 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1072 p_tk->fmt.i_codec = VLC_CODEC_MP4V;
1073 fill_extra_data( p_tk, 0 );
1076 else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
1078 MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
1079 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1083 if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
1084 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
1086 p_tk->fmt.i_codec = p_box->i_type;
1087 p_tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1088 p_tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1089 p_tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1090 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1091 memcpy( p_tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, p_tk->fmt.i_extra );
1092 MP4_FreeBox_sample_vide( p_box );
1098 stream_Delete( p_mp4_stream );
1100 else if( !strcmp( p_tk->psz_codec, "V_MJPEG" ) )
1102 p_tk->fmt.i_codec = VLC_CODEC_MJPG;
1104 else if( !strcmp( p_tk->psz_codec, "A_MS/ACM" ) )
1106 if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1108 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1109 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1113 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
1115 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
1117 if( p_tk->fmt.i_codec == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
1118 msg_Err( &sys.demuxer, "Unrecognized wf tag: 0x%x", GetWLE( &p_wf->wFormatTag ) );
1119 p_tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1120 p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1121 p_tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1122 p_tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1123 p_tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1125 p_tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1126 if( p_tk->fmt.i_extra > 0 )
1128 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1129 memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
1133 else if( !strcmp( p_tk->psz_codec, "A_MPEG/L3" ) ||
1134 !strcmp( p_tk->psz_codec, "A_MPEG/L2" ) ||
1135 !strcmp( p_tk->psz_codec, "A_MPEG/L1" ) )
1137 p_tk->fmt.i_codec = VLC_CODEC_MPGA;
1139 else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
1141 p_tk->fmt.i_codec = VLC_CODEC_A52;
1143 else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
1145 p_tk->fmt.i_codec = VLC_CODEC_EAC3;
1147 else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
1149 p_tk->fmt.i_codec = VLC_CODEC_DTS;
1151 else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
1153 p_tk->fmt.i_codec = VLC_CODEC_MLP;
1155 else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
1157 /* FIXME when more samples arrive */
1158 p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
1159 p_fmt->b_packetized = false;
1161 else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
1163 p_tk->fmt.i_codec = VLC_CODEC_FLAC;
1164 fill_extra_data( p_tk, 0 );
1166 else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
1168 p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
1169 fill_extra_data( p_tk, 0 );
1171 else if( !strncmp( p_tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1172 !strncmp( p_tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1174 int i_profile, i_srate, sbr = 0;
1175 static const unsigned int i_sample_rates[] =
1177 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1178 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1181 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1182 /* create data for faad (MP4DecSpecificDescrTag)*/
1184 if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
1188 else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
1192 else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
1196 else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
1206 for( i_srate = 0; i_srate < 13; i_srate++ )
1208 if( i_sample_rates[i_srate] == p_tk->i_original_rate )
1213 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1215 p_tk->fmt.i_extra = sbr ? 5 : 2;
1216 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1217 ((uint8_t*)p_tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1218 ((uint8_t*)p_tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (p_tk->fmt.audio.i_channels << 3);
1221 int syncExtensionType = 0x2B7;
1223 for (iDSRI=0; iDSRI<13; iDSRI++)
1224 if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
1226 ((uint8_t*)p_tk->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
1227 ((uint8_t*)p_tk->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
1228 ((uint8_t*)p_tk->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
1231 else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
1233 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1234 fill_extra_data( p_tk, 0 );
1236 else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
1238 p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
1239 fill_extra_data( p_tk, 0 );
1241 else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
1243 p_fmt->i_codec = VLC_CODEC_TTA;
1244 if( p_tk->i_extra_data > 0 )
1246 fill_extra_data( p_tk, 0 );
1250 p_fmt->i_extra = 30;
1251 p_fmt->p_extra = xmalloc( p_fmt->i_extra );
1252 uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
1253 memcpy( &p_extra[ 0], "TTA1", 4 );
1254 SetWLE( &p_extra[ 4], 1 );
1255 SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
1256 SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
1257 SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
1258 SetDWLE( &p_extra[14], 0xffffffff );
1259 memset( &p_extra[18], 0, 30 - 18 );
1262 else if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) ||
1263 !strcmp( p_tk->psz_codec, "A_PCM/INT/LIT" ) ||
1264 !strcmp( p_tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1266 if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
1268 p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1272 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1274 p_tk->fmt.audio.i_blockalign = ( p_tk->fmt.audio.i_bitspersample + 7 ) / 8 * p_tk->fmt.audio.i_channels;
1276 else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
1278 if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1280 p_fmt->i_codec = VLC_CODEC_RA_144;
1281 p_fmt->audio.i_channels = 1;
1282 p_fmt->audio.i_rate = 8000;
1283 p_fmt->audio.i_blockalign = 0x14;
1285 else if( p_tk->i_extra_data > 28 )
1287 uint8_t *p = p_tk->p_extra_data;
1288 if( memcmp( p, ".ra", 3 ) ) {
1289 msg_Err( &sys.demuxer, "Invalid Real ExtraData 0x%4.4s", (char *)p );
1290 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1294 real_audio_private * priv = (real_audio_private*) p_tk->p_extra_data;
1295 if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
1297 p_tk->fmt.i_codec = VLC_CODEC_COOK;
1298 p_tk->fmt.audio.i_blockalign = hton16(priv->sub_packet_size);
1300 else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
1301 p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
1302 else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
1303 p_tk->fmt.i_codec = VLC_CODEC_RA_288;
1304 /* FIXME RALF and SIPR */
1305 uint16_t version = (uint16_t) hton16(priv->version);
1307 new Cook_PrivateTrackData( hton16(priv->sub_packet_h),
1308 hton16(priv->frame_size),
1309 hton16(priv->sub_packet_size));
1310 if( unlikely( !p_tk->p_sys ) )
1313 if( unlikely( p_tk->p_sys->Init() ) )
1318 real_audio_private_v4 * v4 = (real_audio_private_v4*) priv;
1319 p_tk->fmt.audio.i_channels = hton16(v4->channels);
1320 p_tk->fmt.audio.i_bitspersample = hton16(v4->sample_size);
1321 p_tk->fmt.audio.i_rate = hton16(v4->sample_rate);
1323 else if( version == 5 )
1325 real_audio_private_v5 * v5 = (real_audio_private_v5*) priv;
1326 p_tk->fmt.audio.i_channels = hton16(v5->channels);
1327 p_tk->fmt.audio.i_bitspersample = hton16(v5->sample_size);
1328 p_tk->fmt.audio.i_rate = hton16(v5->sample_rate);
1330 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);
1332 fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 78);
1336 else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
1338 p_tk->fmt.i_codec = VLC_CODEC_KATE;
1339 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1341 fill_extra_data( p_tk, 0 );
1343 else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
1345 p_fmt->i_codec = VLC_CODEC_SUBT;
1346 p_fmt->subs.psz_encoding = strdup( "ASCII" );
1348 else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
1350 p_tk->fmt.i_codec = VLC_CODEC_SUBT;
1351 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1353 else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
1355 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1356 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1357 if( p_tk->i_extra_data )
1358 fill_extra_data( p_tk, 0 );
1360 else if( !strcmp( p_tk->psz_codec, "S_TEXT/SSA" ) ||
1361 !strcmp( p_tk->psz_codec, "S_TEXT/ASS" ) ||
1362 !strcmp( p_tk->psz_codec, "S_SSA" ) ||
1363 !strcmp( p_tk->psz_codec, "S_ASS" ))
1365 p_tk->fmt.i_codec = VLC_CODEC_SSA;
1366 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1367 if( p_tk->i_extra_data )
1368 fill_extra_data( p_tk, 0 );
1370 else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
1372 p_tk->fmt.i_codec = VLC_CODEC_SPU;
1373 if( p_tk->i_extra_data )
1376 char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
1377 if( psz_buf != NULL )
1379 memcpy( psz_buf, p_tk->p_extra_data , p_tk->i_extra_data );
1380 psz_buf[p_tk->i_extra_data] = '\0';
1382 psz_start = strstr( psz_buf, "size:" );
1384 vobsub_size_parse( psz_start,
1385 &p_tk->fmt.subs.spu.i_original_frame_width,
1386 &p_tk->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1388 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1389 p_tk->fmt.subs.spu.i_original_frame_width,
1390 p_tk->fmt.subs.spu.i_original_frame_height );
1394 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1397 psz_start = strstr( psz_buf, "palette:" );
1399 vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1401 p_tk->fmt.subs.spu.palette[0] = 0xBeef;
1402 msg_Dbg( &sys.demuxer, "vobsub palette read" );
1406 msg_Warn( &sys.demuxer, "reading original palette failed" );
1412 else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
1414 p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
1416 else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
1418 p_tk->fmt.i_cat = NAV_ES;
1423 msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
1424 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1426 if( unlikely( !p_tk->b_enabled ) )
1427 p_tk->fmt.i_priority = -2;
1428 else if( p_tk->b_forced )
1429 p_tk->fmt.i_priority = 2;
1430 else if( p_tk->b_default )
1431 p_tk->fmt.i_priority = 1;
1433 p_tk->fmt.i_priority = 0;
1435 /* Avoid multivideo tracks when unnecessary */
1436 if( p_tk->fmt.i_cat == VIDEO_ES )
1437 p_tk->fmt.i_priority--;
1439 p_tk->p_es = es_out_Add( sys.demuxer.out, &p_tk->fmt );
1441 /* Turn on a subtitles track if it has been flagged as default -
1442 * but only do this if no subtitles track has already been engaged,
1443 * either by an earlier 'default track' (??) or by default
1444 * language choice behaviour.
1446 if( p_tk->b_default )
1448 es_out_Control( sys.demuxer.out,
1449 ES_OUT_SET_ES_DEFAULT,
1453 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
1455 sys.i_start_pts = i_start_time;
1456 // reset the stream reading to the first cluster of the segment used
1457 es.I_O().setFilePointer( i_start_pos );
1460 ep = new EbmlParser( &es, segment, &sys.demuxer );
1465 void matroska_segment_c::UnSelect( )
1467 sys.p_ev->ResetPci();
1468 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1470 if ( tracks[i_track]->p_es != NULL )
1472 // es_format_Clean( &tracks[i_track]->fmt );
1473 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
1474 tracks[i_track]->p_es = NULL;
1481 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, bool *pb_key_picture, bool *pb_discardable_picture, int64_t *pi_duration )
1483 pp_simpleblock = NULL;
1486 *pb_key_picture = true;
1487 *pb_discardable_picture = false;
1491 EbmlElement *el = NULL;
1495 return VLC_EGENERIC;
1497 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1499 /* Check blocks validity to protect againts broken files */
1500 if( BlockFindTrackIndex( NULL, pp_block , pp_simpleblock ) )
1503 pp_simpleblock = NULL;
1507 if( pp_simpleblock != NULL )
1509 *pb_key_picture = pp_simpleblock->IsKeyframe();
1510 *pb_discardable_picture = pp_simpleblock->IsDiscardable();
1513 /* update the index */
1514 #define idx p_indexes[i_index - 1]
1515 if( i_index > 0 && idx.i_time == -1 )
1517 if ( pp_simpleblock != NULL )
1518 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1520 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1521 idx.b_key = *pb_key_picture;
1527 i_level = ep->GetLevel();
1536 msg_Warn( &sys.demuxer, "EOF" );
1537 return VLC_EGENERIC;
1540 /* Verify that we are still inside our cluster
1541 * It can happens whith broken files and when seeking
1545 if( cluster && !ep->IsTopPresent( cluster ) )
1547 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1558 if( MKV_IS_ID( el, KaxCluster ) )
1560 cluster = (KaxCluster*)el;
1561 i_cluster_pos = cluster->GetElementPosition();
1563 // reset silent tracks
1564 for (size_t i=0; i<tracks.size(); i++)
1566 tracks[i]->b_silent = false;
1571 else if( MKV_IS_ID( el, KaxCues ) )
1573 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1574 return VLC_EGENERIC;
1578 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1582 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1584 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1586 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1587 cluster->InitTimecode( uint64( ctc ), i_timescale );
1589 /* add it to the index */
1592 p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1593 IndexAppendCluster( cluster );
1595 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1599 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1601 i_block_pos = el->GetElementPosition();
1604 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1606 pp_simpleblock = (KaxSimpleBlock*)el;
1608 pp_simpleblock->ReadData( es.I_O() );
1609 pp_simpleblock->SetParent( *cluster );
1613 if( MKV_IS_ID( el, KaxBlock ) )
1615 pp_block = (KaxBlock*)el;
1617 pp_block->ReadData( es.I_O() );
1618 pp_block->SetParent( *cluster );
1622 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1624 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1626 dur.ReadData( es.I_O() );
1627 *pi_duration = uint64( dur );
1629 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1631 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1633 ref.ReadData( es.I_O() );
1635 if( *pb_key_picture )
1636 *pb_key_picture = false;
1637 else if( int64( ref ) > 0 )
1638 *pb_discardable_picture = true;
1640 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1642 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1643 track_num.ReadData( es.I_O() );
1645 for (size_t i=0; i<tracks.size(); i++)
1647 if ( tracks[i]->i_number == uint32(track_num))
1649 tracks[i]->b_silent = true;
1656 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1657 return VLC_EGENERIC;