1 /*****************************************************************************
2 * matroska_segment.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2010 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * 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"
31 matroska_segment_c::matroska_segment_c( demux_sys_t & demuxer, EbmlStream & estream )
34 ,i_timescale(MKVD_TIMECODESCALE)
38 ,i_seekhead_position(-1)
40 ,i_tracks_position(-1)
42 ,i_chapters_position(-1)
44 ,i_attachments_position(-1)
50 ,p_prev_segment_uid(NULL)
51 ,p_next_segment_uid(NULL)
55 ,psz_muxing_application(NULL)
56 ,psz_writing_application(NULL)
57 ,psz_segment_filename(NULL)
64 ,b_ref_external_segments(false)
66 p_indexes = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
69 matroska_segment_c::~matroska_segment_c()
71 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
73 delete tracks[i_track]->p_compression_data;
74 es_format_Clean( &tracks[i_track]->fmt );
75 delete tracks[i_track]->p_sys;
76 free( tracks[i_track]->p_extra_data );
77 free( tracks[i_track]->psz_codec );
78 delete tracks[i_track];
81 free( psz_writing_application );
82 free( psz_muxing_application );
83 free( psz_segment_filename );
91 delete p_prev_segment_uid;
92 delete p_next_segment_uid;
94 vlc_delete_all( stored_editions );
95 vlc_delete_all( translations );
96 vlc_delete_all( families );
100 /*****************************************************************************
102 *****************************************************************************
103 * * LoadCues : load the cues element and update index
104 * * LoadTags : load ... the tags element
105 * * InformationCreate : create all information, load tags if present
106 *****************************************************************************/
107 void matroska_segment_c::LoadCues( KaxCues *cues )
114 msg_Err( &sys.demuxer, "There can be only 1 Cues per section." );
118 ep = new EbmlParser( &es, cues, &sys.demuxer );
119 while( ( el = ep->Get() ) != NULL )
121 if( MKV_IS_ID( el, KaxCuePoint ) )
123 #define idx p_indexes[i_index]
126 idx.i_block_number= -1;
132 while( ( el = ep->Get() ) != NULL )
134 if( MKV_IS_ID( el, KaxCueTime ) )
136 KaxCueTime &ctime = *(KaxCueTime*)el;
138 ctime.ReadData( es.I_O() );
140 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
142 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
145 while( ( el = ep->Get() ) != NULL )
147 if( MKV_IS_ID( el, KaxCueTrack ) )
149 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
151 ctrack.ReadData( es.I_O() );
152 idx.i_track = uint16( ctrack );
154 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
156 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
158 ccpos.ReadData( es.I_O() );
159 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
161 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
163 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
165 cbnum.ReadData( es.I_O() );
166 idx.i_block_number = uint32( cbnum );
170 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
177 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
183 msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
184 " track=%d bnum=%d", idx.i_time, idx.i_position,
185 idx.i_track, idx.i_block_number );
189 if( i_index >= i_index_max )
192 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
193 sizeof( mkv_index_t ) * i_index_max );
199 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
204 msg_Dbg( &sys.demuxer, "| - loading cues done." );
208 static const struct {
209 vlc_meta_type_t type;
211 int target_type; /* 0 is valid for all target_type */
213 {vlc_meta_Album, "TITLE", 50},
214 {vlc_meta_Title, "TITLE", 0},
215 {vlc_meta_Artist, "ARTIST", 0},
216 {vlc_meta_Genre, "GENRE", 0},
217 {vlc_meta_Copyright, "COPYRIGHT", 0},
218 {vlc_meta_TrackNumber, "PART_NUMBER", 0},
219 {vlc_meta_Description, "DESCRIPTION", 0},
220 {vlc_meta_Description, "COMMENT", 0},
221 {vlc_meta_Rating, "RATING", 0},
222 {vlc_meta_Date, "DATE_RELEASED", 0},
223 {vlc_meta_Date, "DATE_RELEASE", 0},
224 {vlc_meta_Date, "DATE_RECORDED", 0},
225 {vlc_meta_URL, "URL", 0},
226 {vlc_meta_Publisher, "PUBLISHER", 0},
227 {vlc_meta_EncodedBy, "ENCODED_BY", 0},
228 {vlc_meta_TrackTotal, "TOTAL_PARTS", 0},
229 {vlc_meta_Title, NULL, 0},
232 SimpleTag * matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag, int target_type )
235 EbmlParser *ep = new EbmlParser( &es, tag, &sys.demuxer );
236 SimpleTag * p_simple = new SimpleTag;
240 msg_Err( &sys.demuxer, "Couldn't allocate memory for Simple Tag... ignoring it");
245 sys.meta = vlc_meta_New();
247 msg_Dbg( &sys.demuxer, "| + Simple Tag ");
248 while( ( el = ep->Get() ) != NULL )
250 if( MKV_IS_ID( el, KaxTagName ) )
252 KaxTagName &key = *(KaxTagName*)el;
253 key.ReadData( es.I_O(), SCOPE_ALL_DATA );
254 p_simple->psz_tag_name = strdup( UTFstring( key ).GetUTF8().c_str() );
256 else if( MKV_IS_ID( el, KaxTagString ) )
258 KaxTagString &value = *(KaxTagString*)el;
259 value.ReadData( es.I_O(), SCOPE_ALL_DATA );
260 p_simple->p_value = strdup( UTFstring( value ).GetUTF8().c_str() );
262 else if( MKV_IS_ID( el, KaxTagLangue ) )
264 KaxTagLangue &language = *(KaxTagLangue*) el;
265 language.ReadData( es.I_O(), SCOPE_ALL_DATA );
266 p_simple->psz_lang = strdup( string( language ).c_str());
268 else if( MKV_IS_ID( el, KaxTagDefault ) )
270 KaxTagDefault & dft = *(KaxTagDefault*) el;
271 dft.ReadData( es.I_O(), SCOPE_ALL_DATA );
272 p_simple->b_default = (bool) uint8( dft );
274 /*Tags can be nested*/
275 else if( MKV_IS_ID( el, KaxTagSimple) )
277 SimpleTag * p_st = ParseSimpleTags( (KaxTagSimple*)el, target_type );
279 p_simple->sub_tags.push_back( p_st );
281 /*TODO Handle binary tags*/
285 if( !p_simple->psz_tag_name || !p_simple->p_value )
287 msg_Warn( &sys.demuxer, "Invalid MKV SimpleTag found.");
292 for( int i = 0; metadata_map[i].key; i++ )
294 if( !strcmp( p_simple->psz_tag_name, metadata_map[i].key ) &&
295 (metadata_map[i].target_type == 0 || target_type == metadata_map[i].target_type ) )
297 vlc_meta_Set( sys.meta, metadata_map[i].type, p_simple->p_value );
298 msg_Dbg( &sys.demuxer, "| | + Meta %s: %s", p_simple->psz_tag_name, p_simple->p_value);
302 msg_Dbg( &sys.demuxer, "| | + Meta %s: %s", p_simple->psz_tag_name, p_simple->p_value);
303 vlc_meta_AddExtra( sys.meta, p_simple->psz_tag_name, p_simple->p_value);
308 #define PARSE_TAG( type ) \
310 msg_Dbg( &sys.demuxer, "| + " type ); \
312 while( ( el = ep->Get() ) != NULL ) \
314 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() ); \
316 ep->Up(); } while( 0 )
319 void matroska_segment_c::LoadTags( KaxTags *tags )
321 /* Master elements */
322 EbmlParser *ep = new EbmlParser( &es, tags, &sys.demuxer );
325 while( ( el = ep->Get() ) != NULL )
327 if( MKV_IS_ID( el, KaxTag ) )
329 Tag * p_tag = new Tag;
332 msg_Err( &sys.demuxer,"Couldn't allocate memory for tag... ignoring it");
335 msg_Dbg( &sys.demuxer, "+ Tag" );
337 int target_type = 50;
338 while( ( el = ep->Get() ) != NULL )
340 if( MKV_IS_ID( el, KaxTagTargets ) )
342 msg_Dbg( &sys.demuxer, "| + Targets" );
344 while( ( el = ep->Get() ) != NULL )
346 if( MKV_IS_ID( el, KaxTagTargetTypeValue ) )
348 KaxTagTargetTypeValue &value = *(KaxTagTargetTypeValue*)el;
349 value.ReadData( es.I_O() );
351 msg_Dbg( &sys.demuxer, "| | + TargetTypeValue: %u", uint32(value));
352 target_type = uint32(value);
354 if( MKV_IS_ID( el, KaxTagTrackUID ) )
356 p_tag->i_tag_type = TRACK_UID;
357 KaxTagTrackUID &uid = *(KaxTagTrackUID*) el;
358 uid.ReadData( es.I_O() );
359 p_tag->i_uid = uint64( uid );
360 msg_Dbg( &sys.demuxer, "| | + TrackUID: %"PRIu64, p_tag->i_uid);
363 if( MKV_IS_ID( el, KaxTagEditionUID ) )
365 p_tag->i_tag_type = EDITION_UID;
366 KaxTagEditionUID &uid = *(KaxTagEditionUID*) el;
367 uid.ReadData( es.I_O() );
368 p_tag->i_uid = uint64( uid );
369 msg_Dbg( &sys.demuxer, "| | + EditionUID: %"PRIu64, p_tag->i_uid);
371 if( MKV_IS_ID( el, KaxTagChapterUID ) )
373 p_tag->i_tag_type = CHAPTER_UID;
374 KaxTagChapterUID &uid = *(KaxTagChapterUID*) el;
375 uid.ReadData( es.I_O() );
376 p_tag->i_uid = uint64( uid );
377 msg_Dbg( &sys.demuxer, "| | + ChapterUID: %"PRIu64, p_tag->i_uid);
379 if( MKV_IS_ID( el, KaxTagAttachmentUID ) )
381 p_tag->i_tag_type = ATTACHMENT_UID;
382 KaxTagAttachmentUID &uid = *(KaxTagAttachmentUID*) el;
383 uid.ReadData( es.I_O() );
384 p_tag->i_uid = uint64( uid );
385 msg_Dbg( &sys.demuxer, "| | + AttachmentUID: %"PRIu64, p_tag->i_uid);
390 else if( MKV_IS_ID( el, KaxTagSimple ) )
392 SimpleTag * p_simple =
393 ParseSimpleTags( static_cast<KaxTagSimple*>( el ),
396 p_tag->simple_tags.push_back( p_simple );
398 #if 0 // not valid anymore
399 else if( MKV_IS_ID( el, KaxTagGeneral ) )
400 PARSE_TAG( "General" );
401 else if( MKV_IS_ID( el, KaxTagGenres ) )
402 PARSE_TAG( "Genres" );
403 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
404 PARSE_TAG( "Audio Specific" );
405 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
406 PARSE_TAG( "Images Specific" );
407 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
409 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
411 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
413 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
415 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
417 msg_Dbg( &sys.demuxer, "| + Multi Date" );
419 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
421 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
423 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
425 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
427 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
429 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
431 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
433 msg_Dbg( &sys.demuxer, "| + Multi Title" );
438 msg_Dbg( &sys.demuxer, "| + LoadTag Unknown (%s)", typeid( *el ).name() );
442 this->tags.push_back(p_tag);
446 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
451 msg_Dbg( &sys.demuxer, "loading tags done." );
455 /*****************************************************************************
457 *****************************************************************************/
458 void matroska_segment_c::InformationCreate( )
461 sys.meta = vlc_meta_New();
465 vlc_meta_SetTitle( sys.meta, psz_title );
470 vlc_meta_SetDate( sys.meta, psz_date_utc );
473 if( psz_segment_filename )
475 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
477 if( psz_muxing_application )
479 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
481 if( psz_writing_application )
483 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
486 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
488 // mkv_track_t *tk = tracks[i_track];
489 // vlc_meta_t *mtk = vlc_meta_New();
490 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
494 if( i_tags_position >= 0 )
498 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
508 /*****************************************************************************
510 *****************************************************************************/
512 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
514 #define idx p_indexes[i_index]
516 idx.i_block_number= -1;
517 idx.i_position = cluster->GetElementPosition();
518 idx.i_time = cluster->GlobalTimecode()/ (mtime_t) 1000;
522 if( i_index >= i_index_max )
525 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
526 sizeof( mkv_index_t ) * i_index_max );
531 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
536 for (size_t i=0; i<families.size(); i++)
538 for (size_t j=0; j<of_segment.families.size(); j++)
540 if ( *(families[i]) == *(of_segment.families[j]) )
548 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
552 if ( p_item_a == NULL || p_item_b == NULL )
555 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
556 if ( p_item_b->p_prev_segment_uid != NULL
557 && *p_tmp == *p_item_b->p_prev_segment_uid )
560 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
564 if ( p_item_b->p_segment_uid != NULL
565 && *p_tmp == *p_item_b->p_segment_uid )
568 if ( p_item_b->p_prev_segment_uid != NULL
569 && *p_tmp == *p_item_b->p_prev_segment_uid )
575 bool matroska_segment_c::Preload( )
580 EbmlElement *el = NULL;
582 ep->Reset( &sys.demuxer );
584 while( ( el = ep->Get() ) != NULL )
586 if( MKV_IS_ID( el, KaxSeekHead ) )
588 /* Multiple allowed */
589 /* We bail at 10, to prevent possible recursion */
590 msg_Dbg( &sys.demuxer, "| + Seek head" );
591 if( i_seekhead_count < 10 )
593 i_seekhead_position = (int64_t) es.I_O().getFilePointer();
594 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
597 else if( MKV_IS_ID( el, KaxInfo ) )
599 /* Multiple allowed, mandatory */
600 msg_Dbg( &sys.demuxer, "| + Information" );
601 if( i_info_position < 0 ) // FIXME
602 ParseInfo( static_cast<KaxInfo*>( el ) );
603 i_info_position = (int64_t) es.I_O().getFilePointer();
605 else if( MKV_IS_ID( el, KaxTracks ) )
607 /* Multiple allowed */
608 msg_Dbg( &sys.demuxer, "| + Tracks" );
609 if( i_tracks_position < 0 ) // FIXME
610 ParseTracks( static_cast<KaxTracks*>( el ) );
611 if ( tracks.size() == 0 )
613 msg_Err( &sys.demuxer, "No tracks supported" );
616 i_tracks_position = (int64_t) es.I_O().getFilePointer();
618 else if( MKV_IS_ID( el, KaxCues ) )
620 msg_Dbg( &sys.demuxer, "| + Cues" );
621 if( i_cues_position < 0 )
622 LoadCues( static_cast<KaxCues*>( el ) );
623 i_cues_position = (int64_t) es.I_O().getFilePointer();
625 else if( MKV_IS_ID( el, KaxCluster ) )
627 msg_Dbg( &sys.demuxer, "| + Cluster" );
629 cluster = (KaxCluster*)el;
631 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
635 /* stop pre-parsing the stream */
638 else if( MKV_IS_ID( el, KaxAttachments ) )
640 msg_Dbg( &sys.demuxer, "| + Attachments" );
641 if( i_attachments_position < 0 )
642 ParseAttachments( static_cast<KaxAttachments*>( el ) );
643 i_attachments_position = (int64_t) es.I_O().getFilePointer();
645 else if( MKV_IS_ID( el, KaxChapters ) )
647 msg_Dbg( &sys.demuxer, "| + Chapters" );
648 if( i_chapters_position < 0 )
649 ParseChapters( static_cast<KaxChapters*>( el ) );
650 i_chapters_position = (int64_t) es.I_O().getFilePointer();
652 else if( MKV_IS_ID( el, KaxTag ) )
654 msg_Dbg( &sys.demuxer, "| + Tags" );
655 /*FIXME if( i_tags_position < 0)
656 LoadTags( static_cast<KaxTags*>( el ) );*/
657 i_tags_position = (int64_t) es.I_O().getFilePointer();
659 else if( MKV_IS_ID( el, EbmlVoid ) )
660 msg_Dbg( &sys.demuxer, "| + Void" );
662 msg_Dbg( &sys.demuxer, "| + Preload Unknown (%s)", typeid(*el).name() );
670 /* Here we try to load elements that were found in Seek Heads, but not yet parsed */
671 bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position )
673 int64_t i_sav_position = (int64_t)es.I_O().getFilePointer();
676 es.I_O().setFilePointer( i_element_position, seek_beginning );
677 el = es.FindNextID( ClassInfos, 0xFFFFFFFFL);
681 msg_Err( &sys.demuxer, "cannot load some cues/chapters/tags etc. (broken seekhead or file)" );
682 es.I_O().setFilePointer( i_sav_position, seek_beginning );
686 if( MKV_IS_ID( el, KaxSeekHead ) )
688 /* Multiple allowed */
689 msg_Dbg( &sys.demuxer, "| + Seek head" );
690 if( i_seekhead_count < 10 )
692 i_seekhead_position = i_element_position;
693 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
696 else if( MKV_IS_ID( el, KaxInfo ) ) // FIXME
698 /* Multiple allowed, mandatory */
699 msg_Dbg( &sys.demuxer, "| + Information" );
700 if( i_info_position < 0 )
701 ParseInfo( static_cast<KaxInfo*>( el ) );
702 i_info_position = i_element_position;
704 else if( MKV_IS_ID( el, KaxTracks ) ) // FIXME
706 /* Multiple allowed */
707 msg_Dbg( &sys.demuxer, "| + Tracks" );
708 if( i_tracks_position < 0 )
709 ParseTracks( static_cast<KaxTracks*>( el ) );
710 if ( tracks.size() == 0 )
712 msg_Err( &sys.demuxer, "No tracks supported" );
714 es.I_O().setFilePointer( i_sav_position, seek_beginning );
717 i_tracks_position = i_element_position;
719 else if( MKV_IS_ID( el, KaxCues ) )
721 msg_Dbg( &sys.demuxer, "| + Cues" );
722 if( i_cues_position < 0 )
723 LoadCues( static_cast<KaxCues*>( el ) );
724 i_cues_position = i_element_position;
726 else if( MKV_IS_ID( el, KaxAttachments ) )
728 msg_Dbg( &sys.demuxer, "| + Attachments" );
729 if( i_attachments_position < 0 )
730 ParseAttachments( static_cast<KaxAttachments*>( el ) );
731 i_attachments_position = i_element_position;
733 else if( MKV_IS_ID( el, KaxChapters ) )
735 msg_Dbg( &sys.demuxer, "| + Chapters" );
736 if( i_chapters_position < 0 )
737 ParseChapters( static_cast<KaxChapters*>( el ) );
738 i_chapters_position = i_element_position;
740 else if( MKV_IS_ID( el, KaxTags ) )
742 msg_Dbg( &sys.demuxer, "| + Tags" );
743 if( i_tags_position < 0 )
744 LoadTags( static_cast<KaxTags*>( el ) );
745 i_tags_position = i_element_position;
749 msg_Dbg( &sys.demuxer, "| + LoadSeekHeadItem Unknown (%s)", typeid(*el).name() );
753 es.I_O().setFilePointer( i_sav_position, seek_beginning );
759 spoint(unsigned int tk, mtime_t date, int64_t pos, int64_t cpos):
760 i_track(tk),i_date(date), i_seek_pos(pos),
761 i_cluster_pos(cpos), p_next(NULL){}
762 unsigned int i_track;
765 int64_t i_cluster_pos;
769 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_global_position )
772 KaxSimpleBlock *simpleblock;
773 int64_t i_block_duration;
775 int64_t i_seek_position = i_start_pos;
776 int64_t i_seek_time = i_start_time;
778 spoint *p_first = NULL;
779 spoint *p_last = NULL;
781 bool b_has_key = false;
783 for( size_t i = 0; i < tracks.size(); i++)
784 tracks[i]->i_last_dts = VLC_TS_INVALID;
786 if( i_global_position >= 0 )
788 /* Special case for seeking in files with no cues */
789 EbmlElement *el = NULL;
791 /* Start from the last known index instead of the beginning eachtime */
793 es.I_O().setFilePointer( i_start_pos, seek_beginning );
795 es.I_O().setFilePointer( p_indexes[ i_index - 1 ].i_position,
798 ep = new EbmlParser( &es, segment, &sys.demuxer );
801 while( ( el = ep->Get() ) != NULL )
803 if( MKV_IS_ID( el, KaxCluster ) )
805 cluster = (KaxCluster *)el;
806 i_cluster_pos = cluster->GetElementPosition();
809 p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
812 IndexAppendCluster( cluster );
814 if( es.I_O().getFilePointer() >= (unsigned) i_global_position )
820 /* Don't try complex seek if we seek to 0 */
821 if( i_date == 0 && i_time_offset == 0 )
823 es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 );
824 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME,
826 es.I_O().setFilePointer( i_start_pos );
829 ep = new EbmlParser( &es, segment, &sys.demuxer );
841 for( ; i_idx < i_index; i_idx++ )
842 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
848 i_seek_position = p_indexes[i_idx].i_position;
849 i_seek_time = p_indexes[i_idx].i_time;
852 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
853 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
855 es.I_O().setFilePointer( i_seek_position, seek_beginning );
858 ep = new EbmlParser( &es, segment, &sys.demuxer );
861 sys.i_start_pts = i_date;
863 /* now parse until key frame */
864 const int es_types[3] = { VIDEO_ES, AUDIO_ES, SPU_ES };
866 for( int i = 0; i < 2; i_cat = es_types[++i] )
868 for( i_track = 0; i_track < tracks.size(); i_track++ )
870 if( tracks[i_track]->fmt.i_cat == i_cat )
872 spoint * seekpoint = new spoint(i_track, i_seek_time, i_seek_position, i_seek_position);
873 if( unlikely( !seekpoint ) )
875 for( spoint * sp = p_first; sp; )
883 if( unlikely( !p_first ) )
890 p_last->p_next = seekpoint;
895 if( likely( p_first ) )
898 /*Neither video nor audio track... no seek further*/
899 if( unlikely( !p_first ) )
901 es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, i_date );
902 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
911 bool b_discardable_picture;
912 if( BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
914 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
918 /* check if block's track is in our list */
919 for( i_track = 0; i_track < tracks.size(); i_track++ )
921 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
922 (block && tracks[i_track]->i_number == block->TrackNum()) )
927 i_pts = sys.i_chapter_time + simpleblock->GlobalTimecode() / (mtime_t) 1000;
929 i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
930 if( i_track < tracks.size() )
932 if( tracks[i_track]->fmt.i_cat == i_cat && b_key_picture )
934 /* get the seekpoint */
936 for( sp = p_first; sp; sp = sp->p_next )
937 if( sp->i_track == i_track )
942 sp->i_seek_pos = simpleblock->GetElementPosition();
944 sp->i_seek_pos = i_block_pos;
945 sp->i_cluster_pos = i_cluster_pos;
951 } while( i_pts < i_date );
952 if( b_has_key || !i_idx )
955 /* No key picture was found in the cluster seek to previous seekpoint */
956 i_date = i_time_offset + p_indexes[i_idx].i_time;
959 es.I_O().setFilePointer( p_indexes[i_idx].i_position );
961 ep = new EbmlParser( &es, segment, &sys.demuxer );
965 /* rewind to the last I img */
967 for( p_min = p_first, p_last = p_first; p_last; p_last = p_last->p_next )
968 if( p_last->i_date < p_min->i_date )
971 sys.i_pcr = sys.i_pts = p_min->i_date;
972 es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 + sys.i_pcr );
973 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
974 cluster = (KaxCluster *) ep->UnGet( p_min->i_seek_pos, p_min->i_cluster_pos );
976 /* hack use BlockGet to get the cluster then goto the wanted block */
980 bool b_discardable_picture;
981 BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration );
983 cluster = (KaxCluster *) ep->UnGet( p_min->i_seek_pos, p_min->i_cluster_pos );
989 p_first = p_first->p_next;
994 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
995 const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
998 for( i_track = 0; i_track < tracks.size(); i_track++ )
1000 const mkv_track_t *tk = tracks[i_track];
1002 if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
1003 ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
1009 if( i_track >= tracks.size() )
1010 return VLC_EGENERIC;
1013 *pi_track = i_track;
1017 bool matroska_segment_c::Select( mtime_t i_start_time )
1020 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
1022 bool b_has_default_video = false;
1023 bool b_has_default_audio = false;
1024 /* check for default */
1025 for(size_t i_track = 0; i_track < tracks.size(); i_track++)
1027 mkv_track_t *p_tk = tracks[i_track];
1028 es_format_t *p_fmt = &p_tk->fmt;
1029 if( p_fmt->i_cat == VIDEO_ES )
1030 b_has_default_video |=
1031 p_tk->b_enabled && ( p_tk->b_default || p_tk->b_forced );
1032 else if( p_fmt->i_cat == AUDIO_ES )
1033 b_has_default_audio |=
1034 p_tk->b_enabled && ( p_tk->b_default || p_tk->b_forced );
1037 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1039 mkv_track_t *p_tk = tracks[i_track];
1040 es_format_t *p_fmt = &p_tk->fmt;
1042 if( unlikely( p_fmt->i_cat == UNKNOWN_ES || !p_tk->psz_codec ) )
1044 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, p_tk->i_number );
1048 else if( unlikely( !b_has_default_video && p_fmt->i_cat == VIDEO_ES ) )
1050 p_tk->b_default = true;
1051 b_has_default_video = true;
1053 else if( unlikely( !b_has_default_audio && p_fmt->i_cat == AUDIO_ES ) )
1055 p_tk->b_default = true;
1056 b_has_default_audio = true;
1058 if( unlikely( !p_tk->b_enabled ) )
1059 p_tk->fmt.i_priority = -2;
1060 else if( p_tk->b_forced )
1061 p_tk->fmt.i_priority = 2;
1062 else if( p_tk->b_default )
1063 p_tk->fmt.i_priority = 1;
1065 p_tk->fmt.i_priority = 0;
1067 /* Avoid multivideo tracks when unnecessary */
1068 if( p_tk->fmt.i_cat == VIDEO_ES )
1069 p_tk->fmt.i_priority--;
1071 p_tk->p_es = es_out_Add( sys.demuxer.out, &p_tk->fmt );
1073 /* Turn on a subtitles track if it has been flagged as default -
1074 * but only do this if no subtitles track has already been engaged,
1075 * either by an earlier 'default track' (??) or by default
1076 * language choice behaviour.
1078 if( p_tk->b_default )
1080 es_out_Control( sys.demuxer.out,
1081 ES_OUT_SET_ES_DEFAULT,
1085 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
1087 sys.i_start_pts = i_start_time;
1088 // reset the stream reading to the first cluster of the segment used
1089 es.I_O().setFilePointer( i_start_pos );
1092 ep = new EbmlParser( &es, segment, &sys.demuxer );
1097 void matroska_segment_c::UnSelect( )
1099 sys.p_ev->ResetPci();
1100 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1102 if ( tracks[i_track]->p_es != NULL )
1104 // es_format_Clean( &tracks[i_track]->fmt );
1105 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
1106 tracks[i_track]->p_es = NULL;
1113 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, bool *pb_key_picture, bool *pb_discardable_picture, int64_t *pi_duration )
1115 pp_simpleblock = NULL;
1118 *pb_key_picture = true;
1119 *pb_discardable_picture = false;
1124 EbmlElement *el = NULL;
1128 return VLC_EGENERIC;
1130 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1132 /* Check blocks validity to protect againts broken files */
1133 if( BlockFindTrackIndex( &i_tk, pp_block , pp_simpleblock ) )
1136 pp_simpleblock = NULL;
1140 if( pp_simpleblock != NULL )
1142 *pb_key_picture = pp_simpleblock->IsKeyframe();
1143 *pb_discardable_picture = pp_simpleblock->IsDiscardable();
1145 /* We have block group let's check if the picture is a keyframe */
1146 else if( *pb_key_picture )
1148 switch(tracks[i_tk]->fmt.i_codec)
1150 case VLC_CODEC_THEORA:
1152 DataBuffer *p_data = &pp_block->GetBuffer(0);
1153 size_t sz = p_data->Size();
1154 const uint8_t * p_buff = p_data->Buffer();
1155 /* if the second bit of a Theora frame is 1
1156 it's not a keyframe */
1159 if( p_buff[0] & 0x40 )
1160 *pb_key_picture = false;
1163 *pb_key_picture = false;
1169 /* update the index */
1170 #define idx p_indexes[i_index - 1]
1171 if( i_index > 0 && idx.i_time == -1 )
1173 if ( pp_simpleblock != NULL )
1174 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1176 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1177 idx.b_key = *pb_key_picture;
1183 i_level = ep->GetLevel();
1192 msg_Warn( &sys.demuxer, "EOF" );
1193 return VLC_EGENERIC;
1196 /* Verify that we are still inside our cluster
1197 * It can happens whith broken files and when seeking
1201 if( cluster && !ep->IsTopPresent( cluster ) )
1203 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1214 if( MKV_IS_ID( el, KaxCluster ) )
1216 cluster = (KaxCluster*)el;
1217 i_cluster_pos = cluster->GetElementPosition();
1219 // reset silent tracks
1220 for (size_t i=0; i<tracks.size(); i++)
1222 tracks[i]->b_silent = false;
1227 else if( MKV_IS_ID( el, KaxCues ) )
1229 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1230 return VLC_EGENERIC;
1234 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1238 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1240 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1242 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1243 cluster->InitTimecode( uint64( ctc ), i_timescale );
1245 /* add it to the index */
1248 p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1249 IndexAppendCluster( cluster );
1251 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1255 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1257 i_block_pos = el->GetElementPosition();
1260 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1262 pp_simpleblock = (KaxSimpleBlock*)el;
1264 pp_simpleblock->ReadData( es.I_O() );
1265 pp_simpleblock->SetParent( *cluster );
1269 if( MKV_IS_ID( el, KaxBlock ) )
1271 pp_block = (KaxBlock*)el;
1273 pp_block->ReadData( es.I_O() );
1274 pp_block->SetParent( *cluster );
1278 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1280 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1282 dur.ReadData( es.I_O() );
1283 *pi_duration = uint64( dur );
1285 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1287 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1289 ref.ReadData( es.I_O() );
1291 if( *pb_key_picture )
1292 *pb_key_picture = false;
1293 else if( int64( ref ) > 0 )
1294 *pb_discardable_picture = true;
1296 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1298 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1299 track_num.ReadData( es.I_O() );
1301 for (size_t i=0; i<tracks.size(); i++)
1303 if ( tracks[i]->i_number == uint32(track_num))
1305 tracks[i]->b_silent = true;
1312 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1313 return VLC_EGENERIC;
1318 SimpleTag::~SimpleTag()
1323 for(size_t i = 0; i < sub_tags.size(); i++)
1329 for(size_t i = 0; i < simple_tags.size(); i++)
1330 delete simple_tags[i];