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"
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 static const struct {
222 vlc_meta_type_t type;
224 int target_type; /* 0 is valid for all target_type */
226 {vlc_meta_Album, "TITLE", 50},
227 {vlc_meta_Title, "TITLE", 0},
228 {vlc_meta_Artist, "ARTIST", 0},
229 {vlc_meta_Genre, "GENRE", 0},
230 {vlc_meta_Copyright, "COPYRIGHT", 0},
231 {vlc_meta_TrackNumber, "PART_NUMBER", 0},
232 {vlc_meta_Description, "DESCRIPTION", 0},
233 {vlc_meta_Description, "COMMENT", 0},
234 {vlc_meta_Rating, "RATING", 0},
235 {vlc_meta_Date, "DATE_RELEASED", 0},
236 {vlc_meta_Date, "DATE_RELEASE", 0},
237 {vlc_meta_Date, "DATE_RECORDED", 0},
238 {vlc_meta_URL, "URL", 0},
239 {vlc_meta_Publisher, "PUBLISHER", 0},
240 {vlc_meta_EncodedBy, "ENCODED_BY", 0},
241 {vlc_meta_TrackTotal, "TOTAL_PARTS", 0},
242 {vlc_meta_Title, NULL, 0},
245 SimpleTag * matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag, int target_type )
248 EbmlParser *ep = new EbmlParser( &es, tag, &sys.demuxer );
249 SimpleTag * p_simple = new SimpleTag;
253 msg_Err( &sys.demuxer, "Couldn't allocate memory for Simple Tag... ignoring it");
258 sys.meta = vlc_meta_New();
260 msg_Dbg( &sys.demuxer, "| + Simple Tag ");
261 while( ( el = ep->Get() ) != NULL )
263 if( MKV_IS_ID( el, KaxTagName ) )
265 KaxTagName &key = *(KaxTagName*)el;
266 key.ReadData( es.I_O(), SCOPE_ALL_DATA );
267 p_simple->psz_tag_name = strdup( UTFstring( key ).GetUTF8().c_str() );
269 else if( MKV_IS_ID( el, KaxTagString ) )
271 KaxTagString &value = *(KaxTagString*)el;
272 value.ReadData( es.I_O(), SCOPE_ALL_DATA );
273 p_simple->p_value = strdup( UTFstring( value ).GetUTF8().c_str() );
275 else if( MKV_IS_ID( el, KaxTagLangue ) )
277 KaxTagLangue &language = *(KaxTagLangue*) el;
278 language.ReadData( es.I_O(), SCOPE_ALL_DATA );
279 p_simple->psz_lang = strdup( string( language ).c_str());
281 else if( MKV_IS_ID( el, KaxTagDefault ) )
283 KaxTagDefault & dft = *(KaxTagDefault*) el;
284 dft.ReadData( es.I_O(), SCOPE_ALL_DATA );
285 p_simple->b_default = (bool) uint8( dft );
287 /*Tags can be nested*/
288 else if( MKV_IS_ID( el, KaxTagSimple) )
290 SimpleTag * p_st = ParseSimpleTags( (KaxTagSimple*)el, target_type );
292 p_simple->sub_tags.push_back( p_st );
294 /*TODO Handle binary tags*/
298 if( !p_simple->psz_tag_name || !p_simple->p_value )
300 msg_Warn( &sys.demuxer, "Invalid MKV SimpleTag found.");
305 for( int i = 0; metadata_map[i].key; i++ )
307 if( !strcmp( p_simple->psz_tag_name, metadata_map[i].key ) &&
308 (metadata_map[i].target_type == 0 || target_type == metadata_map[i].target_type ) )
310 vlc_meta_Set( sys.meta, metadata_map[i].type, p_simple->p_value );
311 msg_Dbg( &sys.demuxer, "| | + Meta %s: %s", p_simple->psz_tag_name, p_simple->p_value);
315 msg_Dbg( &sys.demuxer, "| | + Meta %s: %s", p_simple->psz_tag_name, p_simple->p_value);
316 vlc_meta_AddExtra( sys.meta, p_simple->psz_tag_name, p_simple->p_value);
321 #define PARSE_TAG( type ) \
323 msg_Dbg( &sys.demuxer, "| + " type ); \
325 while( ( el = ep->Get() ) != NULL ) \
327 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() ); \
329 ep->Up(); } while( 0 )
332 void matroska_segment_c::LoadTags( KaxTags *tags )
334 /* Master elements */
335 EbmlParser *ep = new EbmlParser( &es, tags, &sys.demuxer );
338 while( ( el = ep->Get() ) != NULL )
340 if( MKV_IS_ID( el, KaxTag ) )
342 Tag * p_tag = new Tag;
345 msg_Err( &sys.demuxer,"Couldn't allocate memory for tag... ignoring it");
348 msg_Dbg( &sys.demuxer, "+ Tag" );
350 int target_type = 50;
351 while( ( el = ep->Get() ) != NULL )
353 if( MKV_IS_ID( el, KaxTagTargets ) )
355 msg_Dbg( &sys.demuxer, "| + Targets" );
357 while( ( el = ep->Get() ) != NULL )
359 if( MKV_IS_ID( el, KaxTagTargetTypeValue ) )
361 KaxTagTargetTypeValue &value = *(KaxTagTargetTypeValue*)el;
362 value.ReadData( es.I_O() );
364 msg_Dbg( &sys.demuxer, "| | + TargetTypeValue: %u", uint32(value));
365 target_type = uint32(value);
367 if( MKV_IS_ID( el, KaxTagTrackUID ) )
369 p_tag->i_tag_type = TRACK_UID;
370 KaxTagTrackUID &uid = *(KaxTagTrackUID*) el;
371 uid.ReadData( es.I_O() );
372 p_tag->i_uid = uint64( uid );
373 msg_Dbg( &sys.demuxer, "| | + TrackUID: %"PRIu64, p_tag->i_uid);
376 if( MKV_IS_ID( el, KaxTagEditionUID ) )
378 p_tag->i_tag_type = EDITION_UID;
379 KaxTagEditionUID &uid = *(KaxTagEditionUID*) el;
380 uid.ReadData( es.I_O() );
381 p_tag->i_uid = uint64( uid );
382 msg_Dbg( &sys.demuxer, "| | + EditionUID: %"PRIu64, p_tag->i_uid);
384 if( MKV_IS_ID( el, KaxTagChapterUID ) )
386 p_tag->i_tag_type = CHAPTER_UID;
387 KaxTagChapterUID &uid = *(KaxTagChapterUID*) el;
388 uid.ReadData( es.I_O() );
389 p_tag->i_uid = uint64( uid );
390 msg_Dbg( &sys.demuxer, "| | + ChapterUID: %"PRIu64, p_tag->i_uid);
392 if( MKV_IS_ID( el, KaxTagAttachmentUID ) )
394 p_tag->i_tag_type = ATTACHMENT_UID;
395 KaxTagAttachmentUID &uid = *(KaxTagAttachmentUID*) el;
396 uid.ReadData( es.I_O() );
397 p_tag->i_uid = uint64( uid );
398 msg_Dbg( &sys.demuxer, "| | + AttachmentUID: %"PRIu64, p_tag->i_uid);
403 else if( MKV_IS_ID( el, KaxTagSimple ) )
405 SimpleTag * p_simple =
406 ParseSimpleTags( static_cast<KaxTagSimple*>( el ),
409 p_tag->simple_tags.push_back( p_simple );
411 #if 0 // not valid anymore
412 else if( MKV_IS_ID( el, KaxTagGeneral ) )
413 PARSE_TAG( "General" );
414 else if( MKV_IS_ID( el, KaxTagGenres ) )
415 PARSE_TAG( "Genres" );
416 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
417 PARSE_TAG( "Audio Specific" );
418 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
419 PARSE_TAG( "Images Specific" );
420 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
422 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
424 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
426 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
428 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
430 msg_Dbg( &sys.demuxer, "| + Multi Date" );
432 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
434 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
436 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
438 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
440 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
442 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
444 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
446 msg_Dbg( &sys.demuxer, "| + Multi Title" );
451 msg_Dbg( &sys.demuxer, "| + LoadTag Unknown (%s)", typeid( *el ).name() );
455 this->tags.push_back(p_tag);
459 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
464 msg_Dbg( &sys.demuxer, "loading tags done." );
468 /*****************************************************************************
470 *****************************************************************************/
471 void matroska_segment_c::InformationCreate( )
474 sys.meta = vlc_meta_New();
478 vlc_meta_SetTitle( sys.meta, psz_title );
483 vlc_meta_SetDate( sys.meta, psz_date_utc );
486 if( psz_segment_filename )
488 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
490 if( psz_muxing_application )
492 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
494 if( psz_writing_application )
496 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
499 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
501 // mkv_track_t *tk = tracks[i_track];
502 // vlc_meta_t *mtk = vlc_meta_New();
503 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
507 if( i_tags_position >= 0 )
511 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
521 /*****************************************************************************
523 *****************************************************************************/
525 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
527 #define idx p_indexes[i_index]
529 idx.i_block_number= -1;
530 idx.i_position = cluster->GetElementPosition();
531 idx.i_time = cluster->GlobalTimecode()/ (mtime_t) 1000;
535 if( i_index >= i_index_max )
538 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
539 sizeof( mkv_index_t ) * i_index_max );
544 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
549 for (size_t i=0; i<families.size(); i++)
551 for (size_t j=0; j<of_segment.families.size(); j++)
553 if ( *(families[i]) == *(of_segment.families[j]) )
561 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
565 if ( p_item_a == NULL || p_item_b == NULL )
568 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
569 if ( p_item_b->p_prev_segment_uid != NULL
570 && *p_tmp == *p_item_b->p_prev_segment_uid )
573 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
577 if ( p_item_b->p_segment_uid != NULL
578 && *p_tmp == *p_item_b->p_segment_uid )
581 if ( p_item_b->p_prev_segment_uid != NULL
582 && *p_tmp == *p_item_b->p_prev_segment_uid )
588 bool matroska_segment_c::Preload( )
593 EbmlElement *el = NULL;
595 ep->Reset( &sys.demuxer );
597 while( ( el = ep->Get() ) != NULL )
599 if( MKV_IS_ID( el, KaxSeekHead ) )
601 /* Multiple allowed */
602 /* We bail at 10, to prevent possible recursion */
603 msg_Dbg( &sys.demuxer, "| + Seek head" );
604 if( i_seekhead_count < 10 )
606 i_seekhead_position = (int64_t) es.I_O().getFilePointer();
607 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
610 else if( MKV_IS_ID( el, KaxInfo ) )
612 /* Multiple allowed, mandatory */
613 msg_Dbg( &sys.demuxer, "| + Information" );
614 if( i_info_position < 0 ) // FIXME
615 ParseInfo( static_cast<KaxInfo*>( el ) );
616 i_info_position = (int64_t) es.I_O().getFilePointer();
618 else if( MKV_IS_ID( el, KaxTracks ) )
620 /* Multiple allowed */
621 msg_Dbg( &sys.demuxer, "| + Tracks" );
622 if( i_tracks_position < 0 ) // FIXME
623 ParseTracks( static_cast<KaxTracks*>( el ) );
624 if ( tracks.size() == 0 )
626 msg_Err( &sys.demuxer, "No tracks supported" );
629 i_tracks_position = (int64_t) es.I_O().getFilePointer();
631 else if( MKV_IS_ID( el, KaxCues ) )
633 msg_Dbg( &sys.demuxer, "| + Cues" );
634 if( i_cues_position < 0 )
635 LoadCues( static_cast<KaxCues*>( el ) );
636 i_cues_position = (int64_t) es.I_O().getFilePointer();
638 else if( MKV_IS_ID( el, KaxCluster ) )
640 msg_Dbg( &sys.demuxer, "| + Cluster" );
642 cluster = (KaxCluster*)el;
644 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
648 /* stop pre-parsing the stream */
651 else if( MKV_IS_ID( el, KaxAttachments ) )
653 msg_Dbg( &sys.demuxer, "| + Attachments" );
654 if( i_attachments_position < 0 )
655 ParseAttachments( static_cast<KaxAttachments*>( el ) );
656 i_attachments_position = (int64_t) es.I_O().getFilePointer();
658 else if( MKV_IS_ID( el, KaxChapters ) )
660 msg_Dbg( &sys.demuxer, "| + Chapters" );
661 if( i_chapters_position < 0 )
662 ParseChapters( static_cast<KaxChapters*>( el ) );
663 i_chapters_position = (int64_t) es.I_O().getFilePointer();
665 else if( MKV_IS_ID( el, KaxTag ) )
667 msg_Dbg( &sys.demuxer, "| + Tags" );
668 /*FIXME if( i_tags_position < 0)
669 LoadTags( static_cast<KaxTags*>( el ) );*/
670 i_tags_position = (int64_t) es.I_O().getFilePointer();
672 else if( MKV_IS_ID( el, EbmlVoid ) )
673 msg_Dbg( &sys.demuxer, "| + Void" );
675 msg_Dbg( &sys.demuxer, "| + Preload Unknown (%s)", typeid(*el).name() );
683 /* Here we try to load elements that were found in Seek Heads, but not yet parsed */
684 bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position )
686 int64_t i_sav_position = (int64_t)es.I_O().getFilePointer();
689 es.I_O().setFilePointer( i_element_position, seek_beginning );
690 el = es.FindNextID( ClassInfos, 0xFFFFFFFFL);
694 msg_Err( &sys.demuxer, "cannot load some cues/chapters/tags etc. (broken seekhead or file)" );
695 es.I_O().setFilePointer( i_sav_position, seek_beginning );
699 if( MKV_IS_ID( el, KaxSeekHead ) )
701 /* Multiple allowed */
702 msg_Dbg( &sys.demuxer, "| + Seek head" );
703 if( i_seekhead_count < 10 )
705 i_seekhead_position = i_element_position;
706 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
709 else if( MKV_IS_ID( el, KaxInfo ) ) // FIXME
711 /* Multiple allowed, mandatory */
712 msg_Dbg( &sys.demuxer, "| + Information" );
713 if( i_info_position < 0 )
714 ParseInfo( static_cast<KaxInfo*>( el ) );
715 i_info_position = i_element_position;
717 else if( MKV_IS_ID( el, KaxTracks ) ) // FIXME
719 /* Multiple allowed */
720 msg_Dbg( &sys.demuxer, "| + Tracks" );
721 if( i_tracks_position < 0 )
722 ParseTracks( static_cast<KaxTracks*>( el ) );
723 if ( tracks.size() == 0 )
725 msg_Err( &sys.demuxer, "No tracks supported" );
727 es.I_O().setFilePointer( i_sav_position, seek_beginning );
730 i_tracks_position = i_element_position;
732 else if( MKV_IS_ID( el, KaxCues ) )
734 msg_Dbg( &sys.demuxer, "| + Cues" );
735 if( i_cues_position < 0 )
736 LoadCues( static_cast<KaxCues*>( el ) );
737 i_cues_position = i_element_position;
739 else if( MKV_IS_ID( el, KaxAttachments ) )
741 msg_Dbg( &sys.demuxer, "| + Attachments" );
742 if( i_attachments_position < 0 )
743 ParseAttachments( static_cast<KaxAttachments*>( el ) );
744 i_attachments_position = i_element_position;
746 else if( MKV_IS_ID( el, KaxChapters ) )
748 msg_Dbg( &sys.demuxer, "| + Chapters" );
749 if( i_chapters_position < 0 )
750 ParseChapters( static_cast<KaxChapters*>( el ) );
751 i_chapters_position = i_element_position;
753 else if( MKV_IS_ID( el, KaxTags ) )
755 msg_Dbg( &sys.demuxer, "| + Tags" );
756 if( i_tags_position < 0 )
757 LoadTags( static_cast<KaxTags*>( el ) );
758 i_tags_position = i_element_position;
762 msg_Dbg( &sys.demuxer, "| + LoadSeekHeadItem Unknown (%s)", typeid(*el).name() );
766 es.I_O().setFilePointer( i_sav_position, seek_beginning );
772 spoint(unsigned int tk, mtime_t date, int64_t pos, int64_t cpos):
773 i_track(tk),i_date(date), i_seek_pos(pos),
774 i_cluster_pos(cpos), p_next(NULL){}
775 unsigned int i_track;
778 int64_t i_cluster_pos;
782 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_global_position )
785 KaxSimpleBlock *simpleblock;
786 int64_t i_block_duration;
788 int64_t i_seek_position = i_start_pos;
789 int64_t i_seek_time = i_start_time;
791 spoint *p_first = NULL;
792 spoint *p_last = NULL;
794 bool b_has_key = false;
796 for( size_t i = 0; i < tracks.size(); i++)
797 tracks[i]->i_last_dts = VLC_TS_INVALID;
799 if( i_global_position >= 0 )
801 /* Special case for seeking in files with no cues */
802 EbmlElement *el = NULL;
804 /* Start from the last known index instead of the beginning eachtime */
806 es.I_O().setFilePointer( i_start_pos, seek_beginning );
808 es.I_O().setFilePointer( p_indexes[ i_index - 1 ].i_position,
811 ep = new EbmlParser( &es, segment, &sys.demuxer );
814 while( ( el = ep->Get() ) != NULL )
816 if( MKV_IS_ID( el, KaxCluster ) )
818 cluster = (KaxCluster *)el;
819 i_cluster_pos = cluster->GetElementPosition();
822 p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
825 IndexAppendCluster( cluster );
827 if( es.I_O().getFilePointer() >= (unsigned) i_global_position )
833 /* Don't try complex seek if we seek to 0 */
834 if( i_date == 0 && i_time_offset == 0 )
836 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME,
838 es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 );
839 es.I_O().setFilePointer( i_start_pos );
842 ep = new EbmlParser( &es, segment, &sys.demuxer );
854 for( ; i_idx < i_index; i_idx++ )
855 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
861 i_seek_position = p_indexes[i_idx].i_position;
862 i_seek_time = p_indexes[i_idx].i_time;
865 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
866 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
868 es.I_O().setFilePointer( i_seek_position, seek_beginning );
871 ep = new EbmlParser( &es, segment, &sys.demuxer );
874 sys.i_start_pts = i_date;
876 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
878 /* now parse until key frame */
879 const int es_types[3] = { VIDEO_ES, AUDIO_ES, SPU_ES };
881 for( int i = 0; i < 2; i_cat = es_types[++i] )
883 for( i_track = 0; i_track < tracks.size(); i_track++ )
885 if( tracks[i_track]->fmt.i_cat == i_cat )
887 spoint * seekpoint = new spoint(i_track, i_seek_time, i_seek_position, i_seek_position);
888 if( unlikely( !seekpoint ) )
890 for( spoint * sp = p_first; sp; )
898 if( unlikely( !p_first ) )
905 p_last->p_next = seekpoint;
910 if( likely( p_first ) )
913 /*Neither video nor audio track... no seek further*/
914 if( unlikely( !p_first ) )
922 bool b_discardable_picture;
923 if( BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
925 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
929 /* check if block's track is in our list */
930 for( i_track = 0; i_track < tracks.size(); i_track++ )
932 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
933 (block && tracks[i_track]->i_number == block->TrackNum()) )
938 i_pts = sys.i_chapter_time + simpleblock->GlobalTimecode() / (mtime_t) 1000;
940 i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
941 if( i_track < tracks.size() )
943 if( tracks[i_track]->fmt.i_cat == i_cat && b_key_picture )
945 /* get the seekpoint */
947 for( sp = p_first; sp; sp = sp->p_next )
948 if( sp->i_track == i_track )
953 sp->i_seek_pos = simpleblock->GetElementPosition();
955 sp->i_seek_pos = i_block_pos;
956 sp->i_cluster_pos = i_cluster_pos;
962 } while( i_pts < i_date );
963 if( b_has_key || !i_idx )
966 /* No key picture was found in the cluster seek to previous seekpoint */
967 i_date = i_time_offset + p_indexes[i_idx].i_time;
970 es.I_O().setFilePointer( p_indexes[i_idx].i_position );
972 ep = new EbmlParser( &es, segment, &sys.demuxer );
976 /* rewind to the last I img */
978 for( p_min = p_first, p_last = p_first; p_last; p_last = p_last->p_next )
979 if( p_last->i_date < p_min->i_date )
982 sys.i_pcr = sys.i_pts = p_min->i_date;
983 es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 + sys.i_pcr );
984 cluster = (KaxCluster *) ep->UnGet( p_min->i_seek_pos, p_min->i_cluster_pos );
986 /* hack use BlockGet to get the cluster then goto the wanted block */
990 bool b_discardable_picture;
991 BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration );
993 cluster = (KaxCluster *) ep->UnGet( p_min->i_seek_pos, p_min->i_cluster_pos );
999 p_first = p_first->p_next;
1004 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
1005 const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
1008 for( i_track = 0; i_track < tracks.size(); i_track++ )
1010 const mkv_track_t *tk = tracks[i_track];
1012 if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
1013 ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
1019 if( i_track >= tracks.size() )
1020 return VLC_EGENERIC;
1023 *pi_track = i_track;
1027 static inline void fill_extra_data( mkv_track_t *p_tk, unsigned int offset )
1029 if(p_tk->i_extra_data <= offset) return;
1030 p_tk->fmt.i_extra = p_tk->i_extra_data - offset;
1031 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1032 if(!p_tk->fmt.p_extra) { p_tk->fmt.i_extra = 0; return; };
1033 memcpy( p_tk->fmt.p_extra, p_tk->p_extra_data + offset, p_tk->fmt.i_extra );
1036 bool matroska_segment_c::Select( mtime_t i_start_time )
1039 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
1041 bool b_has_default_video = false;
1042 bool b_has_default_audio = false;
1043 /* check for default */
1044 for(size_t i_track = 0; i_track < tracks.size(); i_track++)
1046 mkv_track_t *p_tk = tracks[i_track];
1047 es_format_t *p_fmt = &p_tk->fmt;
1048 if( p_fmt->i_cat == VIDEO_ES )
1049 b_has_default_video |=
1050 p_tk->b_enabled && ( p_tk->b_default || p_tk->b_forced );
1051 else if( p_fmt->i_cat == AUDIO_ES )
1052 b_has_default_audio |=
1053 p_tk->b_enabled && ( p_tk->b_default || p_tk->b_forced );
1056 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1058 mkv_track_t *p_tk = tracks[i_track];
1059 es_format_t *p_fmt = &p_tk->fmt;
1061 if( unlikely( p_fmt->i_cat == UNKNOWN_ES || !p_tk->psz_codec ) )
1063 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, p_tk->i_number );
1067 else if( unlikely( !b_has_default_video && p_fmt->i_cat == VIDEO_ES ) )
1069 p_tk->b_default = true;
1070 b_has_default_video = true;
1072 else if( unlikely( !b_has_default_audio && p_fmt->i_cat == AUDIO_ES ) )
1074 p_tk->b_default = true;
1075 b_has_default_audio = true;
1078 if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
1080 if( p_tk->i_extra_data < (int)sizeof( VLC_BITMAPINFOHEADER ) )
1082 msg_Err( &sys.demuxer, "missing/invalid VLC_BITMAPINFOHEADER" );
1083 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1087 VLC_BITMAPINFOHEADER *p_bih = (VLC_BITMAPINFOHEADER*)p_tk->p_extra_data;
1089 p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1090 p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1091 p_tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1093 p_tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( VLC_BITMAPINFOHEADER );
1094 if( p_tk->fmt.i_extra > 0 )
1096 /* Very unlikely yet possible: bug #5659*/
1097 size_t maxlen = p_tk->i_extra_data - sizeof( VLC_BITMAPINFOHEADER );
1098 p_tk->fmt.i_extra = ( (unsigned)p_tk->fmt.i_extra < maxlen )?
1099 p_tk->fmt.i_extra : maxlen;
1101 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1102 memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
1105 p_tk->b_dts_only = true;
1107 else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
1108 !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
1110 p_tk->fmt.i_codec = VLC_CODEC_MPGV;
1111 if( p_tk->i_extra_data )
1112 fill_extra_data( p_tk, 0 );
1114 else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
1116 p_tk->fmt.i_codec = VLC_CODEC_THEORA;
1117 fill_extra_data( p_tk, 0 );
1118 p_tk->b_pts_only = true;
1120 else if( !strncmp( p_tk->psz_codec, "V_REAL/RV", 9 ) )
1122 uint8_t *p = p_tk->p_extra_data;
1124 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
1125 p_fmt->i_codec = VLC_CODEC_RV10;
1126 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
1127 p_fmt->i_codec = VLC_CODEC_RV20;
1128 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
1129 p_fmt->i_codec = VLC_CODEC_RV30;
1130 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
1131 p_fmt->i_codec = VLC_CODEC_RV40;
1133 /* Extract the framerate from the header */
1134 if( p_tk->i_extra_data >= 26 &&
1135 p[4] == 'V' && p[5] == 'I' && p[6] == 'D' && p[7] == 'O' &&
1136 p[8] == 'R' && p[9] == 'V' &&
1137 (p[10] == '3' || p[10] == '4') && p[11] == '0' )
1139 p_tk->fmt.video.i_frame_rate =
1140 p[22] << 24 | p[23] << 16 | p[24] << 8 | p[25] << 0;
1141 p_tk->fmt.video.i_frame_rate_base = 65536;
1144 fill_extra_data( p_tk, 26 );
1145 p_tk->b_dts_only = true;
1147 else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
1149 p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
1151 else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
1153 p_tk->fmt.i_codec = VLC_CODEC_VP8;
1154 p_tk->b_pts_only = true;
1156 else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
1158 if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
1160 p_tk->fmt.i_codec = VLC_CODEC_DIV3;
1162 else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
1164 /* A MPEG 4 codec, SP, ASP, AP or AVC */
1165 if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1166 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1168 p_tk->fmt.i_codec = VLC_CODEC_MP4V;
1169 fill_extra_data( p_tk, 0 );
1172 else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
1174 MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
1175 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1179 if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
1180 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
1182 p_tk->fmt.i_codec = p_box->i_type;
1183 p_tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1184 p_tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1185 p_tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1186 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1187 memcpy( p_tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, p_tk->fmt.i_extra );
1188 MP4_FreeBox_sample_vide( p_box );
1194 stream_Delete( p_mp4_stream );
1196 else if( !strcmp( p_tk->psz_codec, "V_MJPEG" ) )
1198 p_tk->fmt.i_codec = VLC_CODEC_MJPG;
1200 else if( !strcmp( p_tk->psz_codec, "A_MS/ACM" ) )
1202 if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1204 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1205 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1209 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
1211 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
1213 if( p_tk->fmt.i_codec == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
1214 msg_Err( &sys.demuxer, "Unrecognized wf tag: 0x%x", GetWLE( &p_wf->wFormatTag ) );
1215 p_tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1216 p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1217 p_tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1218 p_tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1219 p_tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1221 p_tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1222 if( p_tk->fmt.i_extra > 0 )
1224 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1225 memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
1229 else if( !strcmp( p_tk->psz_codec, "A_MPEG/L3" ) ||
1230 !strcmp( p_tk->psz_codec, "A_MPEG/L2" ) ||
1231 !strcmp( p_tk->psz_codec, "A_MPEG/L1" ) )
1233 p_tk->fmt.i_codec = VLC_CODEC_MPGA;
1235 else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
1237 p_tk->fmt.i_codec = VLC_CODEC_A52;
1239 else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
1241 p_tk->fmt.i_codec = VLC_CODEC_EAC3;
1243 else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
1245 p_tk->fmt.i_codec = VLC_CODEC_DTS;
1247 else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
1249 p_tk->fmt.i_codec = VLC_CODEC_MLP;
1251 else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
1253 /* FIXME when more samples arrive */
1254 p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
1255 p_fmt->b_packetized = false;
1257 else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
1259 p_tk->fmt.i_codec = VLC_CODEC_FLAC;
1260 fill_extra_data( p_tk, 0 );
1262 else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
1264 p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
1265 fill_extra_data( p_tk, 0 );
1267 else if( !strncmp( p_tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1268 !strncmp( p_tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1270 int i_profile, i_srate, sbr = 0;
1271 static const unsigned int i_sample_rates[] =
1273 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1274 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1277 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1278 /* create data for faad (MP4DecSpecificDescrTag)*/
1280 if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
1284 else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
1288 else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
1292 else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
1302 for( i_srate = 0; i_srate < 13; i_srate++ )
1304 if( i_sample_rates[i_srate] == p_tk->i_original_rate )
1309 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1311 p_tk->fmt.i_extra = sbr ? 5 : 2;
1312 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1313 ((uint8_t*)p_tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1314 ((uint8_t*)p_tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (p_tk->fmt.audio.i_channels << 3);
1317 int syncExtensionType = 0x2B7;
1319 for (iDSRI=0; iDSRI<13; iDSRI++)
1320 if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
1322 ((uint8_t*)p_tk->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
1323 ((uint8_t*)p_tk->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
1324 ((uint8_t*)p_tk->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
1327 else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
1329 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1330 fill_extra_data( p_tk, 0 );
1332 else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
1334 p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
1335 fill_extra_data( p_tk, 0 );
1337 else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
1339 p_fmt->i_codec = VLC_CODEC_TTA;
1340 if( p_tk->i_extra_data > 0 )
1342 fill_extra_data( p_tk, 0 );
1346 p_fmt->i_extra = 30;
1347 p_fmt->p_extra = xmalloc( p_fmt->i_extra );
1348 uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
1349 memcpy( &p_extra[ 0], "TTA1", 4 );
1350 SetWLE( &p_extra[ 4], 1 );
1351 SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
1352 SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
1353 SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
1354 SetDWLE( &p_extra[14], 0xffffffff );
1355 memset( &p_extra[18], 0, 30 - 18 );
1358 else if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) ||
1359 !strcmp( p_tk->psz_codec, "A_PCM/INT/LIT" ) ||
1360 !strcmp( p_tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1362 if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
1364 p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1368 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1370 p_tk->fmt.audio.i_blockalign = ( p_tk->fmt.audio.i_bitspersample + 7 ) / 8 * p_tk->fmt.audio.i_channels;
1372 else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
1374 if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1376 p_fmt->i_codec = VLC_CODEC_RA_144;
1377 p_fmt->audio.i_channels = 1;
1378 p_fmt->audio.i_rate = 8000;
1379 p_fmt->audio.i_blockalign = 0x14;
1381 else if( p_tk->i_extra_data > 28 )
1383 uint8_t *p = p_tk->p_extra_data;
1384 if( memcmp( p, ".ra", 3 ) ) {
1385 msg_Err( &sys.demuxer, "Invalid Real ExtraData 0x%4.4s", (char *)p );
1386 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1390 real_audio_private * priv = (real_audio_private*) p_tk->p_extra_data;
1391 if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
1393 p_tk->fmt.i_codec = VLC_CODEC_COOK;
1394 p_tk->fmt.audio.i_blockalign = hton16(priv->sub_packet_size);
1396 else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
1397 p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
1398 else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
1399 p_tk->fmt.i_codec = VLC_CODEC_RA_288;
1400 /* FIXME RALF and SIPR */
1401 uint16_t version = (uint16_t) hton16(priv->version);
1403 new Cook_PrivateTrackData( hton16(priv->sub_packet_h),
1404 hton16(priv->frame_size),
1405 hton16(priv->sub_packet_size));
1406 if( unlikely( !p_tk->p_sys ) )
1409 if( unlikely( p_tk->p_sys->Init() ) )
1414 real_audio_private_v4 * v4 = (real_audio_private_v4*) priv;
1415 p_tk->fmt.audio.i_channels = hton16(v4->channels);
1416 p_tk->fmt.audio.i_bitspersample = hton16(v4->sample_size);
1417 p_tk->fmt.audio.i_rate = hton16(v4->sample_rate);
1419 else if( version == 5 )
1421 real_audio_private_v5 * v5 = (real_audio_private_v5*) priv;
1422 p_tk->fmt.audio.i_channels = hton16(v5->channels);
1423 p_tk->fmt.audio.i_bitspersample = hton16(v5->sample_size);
1424 p_tk->fmt.audio.i_rate = hton16(v5->sample_rate);
1426 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);
1428 fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 78);
1432 else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
1434 p_tk->fmt.i_codec = VLC_CODEC_KATE;
1435 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1437 fill_extra_data( p_tk, 0 );
1439 else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
1441 p_fmt->i_codec = VLC_CODEC_SUBT;
1442 p_fmt->subs.psz_encoding = strdup( "ASCII" );
1444 else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
1446 p_tk->fmt.i_codec = VLC_CODEC_SUBT;
1447 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1449 else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
1451 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1452 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1453 if( p_tk->i_extra_data )
1454 fill_extra_data( p_tk, 0 );
1456 else if( !strcmp( p_tk->psz_codec, "S_TEXT/SSA" ) ||
1457 !strcmp( p_tk->psz_codec, "S_TEXT/ASS" ) ||
1458 !strcmp( p_tk->psz_codec, "S_SSA" ) ||
1459 !strcmp( p_tk->psz_codec, "S_ASS" ))
1461 p_tk->fmt.i_codec = VLC_CODEC_SSA;
1462 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1463 if( p_tk->i_extra_data )
1464 fill_extra_data( p_tk, 0 );
1466 else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
1468 p_tk->fmt.i_codec = VLC_CODEC_SPU;
1469 if( p_tk->i_extra_data )
1472 char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
1473 if( psz_buf != NULL )
1475 memcpy( psz_buf, p_tk->p_extra_data , p_tk->i_extra_data );
1476 psz_buf[p_tk->i_extra_data] = '\0';
1478 psz_start = strstr( psz_buf, "size:" );
1480 vobsub_size_parse( psz_start,
1481 &p_tk->fmt.subs.spu.i_original_frame_width,
1482 &p_tk->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1484 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1485 p_tk->fmt.subs.spu.i_original_frame_width,
1486 p_tk->fmt.subs.spu.i_original_frame_height );
1490 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1493 psz_start = strstr( psz_buf, "palette:" );
1495 vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1497 p_tk->fmt.subs.spu.palette[0] = 0xBeef;
1498 msg_Dbg( &sys.demuxer, "vobsub palette read" );
1502 msg_Warn( &sys.demuxer, "reading original palette failed" );
1508 else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
1510 p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
1512 else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
1514 p_tk->fmt.i_cat = NAV_ES;
1519 msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
1520 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1522 if( unlikely( !p_tk->b_enabled ) )
1523 p_tk->fmt.i_priority = -2;
1524 else if( p_tk->b_forced )
1525 p_tk->fmt.i_priority = 2;
1526 else if( p_tk->b_default )
1527 p_tk->fmt.i_priority = 1;
1529 p_tk->fmt.i_priority = 0;
1531 /* Avoid multivideo tracks when unnecessary */
1532 if( p_tk->fmt.i_cat == VIDEO_ES )
1533 p_tk->fmt.i_priority--;
1535 p_tk->p_es = es_out_Add( sys.demuxer.out, &p_tk->fmt );
1537 /* Turn on a subtitles track if it has been flagged as default -
1538 * but only do this if no subtitles track has already been engaged,
1539 * either by an earlier 'default track' (??) or by default
1540 * language choice behaviour.
1542 if( p_tk->b_default )
1544 es_out_Control( sys.demuxer.out,
1545 ES_OUT_SET_ES_DEFAULT,
1549 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
1551 sys.i_start_pts = i_start_time;
1552 // reset the stream reading to the first cluster of the segment used
1553 es.I_O().setFilePointer( i_start_pos );
1556 ep = new EbmlParser( &es, segment, &sys.demuxer );
1561 void matroska_segment_c::UnSelect( )
1563 sys.p_ev->ResetPci();
1564 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1566 if ( tracks[i_track]->p_es != NULL )
1568 // es_format_Clean( &tracks[i_track]->fmt );
1569 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
1570 tracks[i_track]->p_es = NULL;
1577 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, bool *pb_key_picture, bool *pb_discardable_picture, int64_t *pi_duration )
1579 pp_simpleblock = NULL;
1582 *pb_key_picture = true;
1583 *pb_discardable_picture = false;
1588 EbmlElement *el = NULL;
1592 return VLC_EGENERIC;
1594 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1596 /* Check blocks validity to protect againts broken files */
1597 if( BlockFindTrackIndex( &i_tk, pp_block , pp_simpleblock ) )
1600 pp_simpleblock = NULL;
1604 if( pp_simpleblock != NULL )
1606 *pb_key_picture = pp_simpleblock->IsKeyframe();
1607 *pb_discardable_picture = pp_simpleblock->IsDiscardable();
1609 /* We have block group let's check if the picture is a keyframe */
1610 else if( *pb_key_picture )
1612 switch(tracks[i_tk]->fmt.i_codec)
1614 case VLC_CODEC_THEORA:
1616 DataBuffer *p_data = &pp_block->GetBuffer(0);
1617 size_t sz = p_data->Size();
1618 const uint8_t * p_buff = p_data->Buffer();
1619 /* if the second bit of a Theora frame is 1
1620 it's not a keyframe */
1623 if( p_buff[0] & 0x40 )
1624 *pb_key_picture = false;
1627 *pb_key_picture = false;
1633 /* update the index */
1634 #define idx p_indexes[i_index - 1]
1635 if( i_index > 0 && idx.i_time == -1 )
1637 if ( pp_simpleblock != NULL )
1638 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1640 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1641 idx.b_key = *pb_key_picture;
1647 i_level = ep->GetLevel();
1656 msg_Warn( &sys.demuxer, "EOF" );
1657 return VLC_EGENERIC;
1660 /* Verify that we are still inside our cluster
1661 * It can happens whith broken files and when seeking
1665 if( cluster && !ep->IsTopPresent( cluster ) )
1667 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1678 if( MKV_IS_ID( el, KaxCluster ) )
1680 cluster = (KaxCluster*)el;
1681 i_cluster_pos = cluster->GetElementPosition();
1683 // reset silent tracks
1684 for (size_t i=0; i<tracks.size(); i++)
1686 tracks[i]->b_silent = false;
1691 else if( MKV_IS_ID( el, KaxCues ) )
1693 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1694 return VLC_EGENERIC;
1698 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1702 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1704 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1706 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1707 cluster->InitTimecode( uint64( ctc ), i_timescale );
1709 /* add it to the index */
1712 p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1713 IndexAppendCluster( cluster );
1715 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1719 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1721 i_block_pos = el->GetElementPosition();
1724 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1726 pp_simpleblock = (KaxSimpleBlock*)el;
1728 pp_simpleblock->ReadData( es.I_O() );
1729 pp_simpleblock->SetParent( *cluster );
1733 if( MKV_IS_ID( el, KaxBlock ) )
1735 pp_block = (KaxBlock*)el;
1737 pp_block->ReadData( es.I_O() );
1738 pp_block->SetParent( *cluster );
1742 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1744 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1746 dur.ReadData( es.I_O() );
1747 *pi_duration = uint64( dur );
1749 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1751 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1753 ref.ReadData( es.I_O() );
1755 if( *pb_key_picture )
1756 *pb_key_picture = false;
1757 else if( int64( ref ) > 0 )
1758 *pb_discardable_picture = true;
1760 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1762 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1763 track_num.ReadData( es.I_O() );
1765 for (size_t i=0; i<tracks.size(); i++)
1767 if ( tracks[i]->i_number == uint32(track_num))
1769 tracks[i]->b_silent = true;
1776 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1777 return VLC_EGENERIC;
1782 SimpleTag::~SimpleTag()
1787 for(size_t i = 0; i < sub_tags.size(); i++)
1793 for(size_t i = 0; i < simple_tags.size(); i++)
1794 delete simple_tags[i];