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_Title, NULL},
247 void matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag )
250 EbmlParser *ep = new EbmlParser( &es, tag, &sys.demuxer );
251 char *k = NULL, *v = NULL;
254 sys.meta = vlc_meta_New();
256 msg_Dbg( &sys.demuxer, "| + Simple Tag ");
257 while( ( el = ep->Get() ) != NULL )
259 if( MKV_IS_ID( el, KaxTagName ) )
261 KaxTagName &key = *(KaxTagName*)el;
262 key.ReadData( es.I_O(), SCOPE_ALL_DATA );
263 k = strdup( UTFstring( key ).GetUTF8().c_str() );
265 if( MKV_IS_ID( el, KaxTagString ) )
267 KaxTagString &value = *(KaxTagString*)el;
268 value.ReadData( es.I_O(), SCOPE_ALL_DATA );
269 v = strdup( UTFstring( value ).GetUTF8().c_str() );
276 msg_Warn( &sys.demuxer, "Invalid MKV SimpleTag found.");
280 for( int i = 0; metadata_map[i].key; i++ )
282 if( !strcmp( k, metadata_map[i].key ) )
284 vlc_meta_Set( sys.meta, metadata_map[i].type, v );
288 msg_Dbg( &sys.demuxer, "| | + %s: %s", k, v);
289 vlc_meta_AddExtra( sys.meta, k, v );
296 void matroska_segment_c::LoadTags( KaxTags *tags )
298 /* Master elements */
299 EbmlParser *ep = new EbmlParser( &es, tags, &sys.demuxer );
302 while( ( el = ep->Get() ) != NULL )
304 if( MKV_IS_ID( el, KaxTag ) )
306 msg_Dbg( &sys.demuxer, "+ Tag" );
308 while( ( el = ep->Get() ) != NULL )
310 if( MKV_IS_ID( el, KaxTagTargets ) )
311 PARSE_TAG( "Targets" );
312 #if 0 // not valid anymore
313 else if( MKV_IS_ID( el, KaxTagGeneral ) )
314 PARSE_TAG( "General" );
315 else if( MKV_IS_ID( el, KaxTagGenres ) )
316 PARSE_TAG( "Genres" );
317 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
318 PARSE_TAG( "Audio Specific" );
319 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
320 PARSE_TAG( "Images Specific" );
321 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
323 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
325 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
327 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
329 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
331 msg_Dbg( &sys.demuxer, "| + Multi Date" );
333 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
335 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
337 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
339 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
341 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
343 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
345 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
347 msg_Dbg( &sys.demuxer, "| + Multi Title" );
350 else if( MKV_IS_ID( el, KaxTagSimple ) )
351 ParseSimpleTags( static_cast<KaxTagSimple*>( el ) );
354 msg_Dbg( &sys.demuxer, "| + LoadTag Unknown (%s)", typeid( *el ).name() );
361 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
366 msg_Dbg( &sys.demuxer, "loading tags done." );
370 /*****************************************************************************
372 *****************************************************************************/
373 void matroska_segment_c::InformationCreate( )
376 sys.meta = vlc_meta_New();
380 vlc_meta_SetTitle( sys.meta, psz_title );
384 vlc_meta_SetDate( sys.meta, psz_date_utc );
388 if( psz_segment_filename )
390 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
392 if( psz_muxing_application )
394 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
396 if( psz_writing_application )
398 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
401 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
403 // mkv_track_t *tk = tracks[i_track];
404 // vlc_meta_t *mtk = vlc_meta_New();
405 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
409 if( i_tags_position >= 0 )
413 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
423 /*****************************************************************************
425 *****************************************************************************/
427 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
429 #define idx p_indexes[i_index]
431 idx.i_block_number= -1;
432 idx.i_position = cluster->GetElementPosition();
433 idx.i_time = cluster->GlobalTimecode()/ (mtime_t) 1000;
437 if( i_index >= i_index_max )
440 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
441 sizeof( mkv_index_t ) * i_index_max );
446 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
451 for (size_t i=0; i<families.size(); i++)
453 for (size_t j=0; j<of_segment.families.size(); j++)
455 if ( *(families[i]) == *(of_segment.families[j]) )
463 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
467 if ( p_item_a == NULL || p_item_b == NULL )
470 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
471 if ( p_item_b->p_prev_segment_uid != NULL
472 && *p_tmp == *p_item_b->p_prev_segment_uid )
475 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
479 if ( p_item_b->p_segment_uid != NULL
480 && *p_tmp == *p_item_b->p_segment_uid )
483 if ( p_item_b->p_prev_segment_uid != NULL
484 && *p_tmp == *p_item_b->p_prev_segment_uid )
490 bool matroska_segment_c::Preload( )
495 EbmlElement *el = NULL;
497 ep->Reset( &sys.demuxer );
499 while( ( el = ep->Get() ) != NULL )
501 if( MKV_IS_ID( el, KaxSeekHead ) )
503 /* Multiple allowed */
504 /* We bail at 10, to prevent possible recursion */
505 msg_Dbg( &sys.demuxer, "| + Seek head" );
506 if( i_seekhead_count < 10 )
508 i_seekhead_position = (int64_t) es.I_O().getFilePointer();
509 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
512 else if( MKV_IS_ID( el, KaxInfo ) )
514 /* Multiple allowed, mandatory */
515 msg_Dbg( &sys.demuxer, "| + Information" );
516 if( i_info_position < 0 ) // FIXME
517 ParseInfo( static_cast<KaxInfo*>( el ) );
518 i_info_position = (int64_t) es.I_O().getFilePointer();
520 else if( MKV_IS_ID( el, KaxTracks ) )
522 /* Multiple allowed */
523 msg_Dbg( &sys.demuxer, "| + Tracks" );
524 if( i_tracks_position < 0 ) // FIXME
525 ParseTracks( static_cast<KaxTracks*>( el ) );
526 if ( tracks.size() == 0 )
528 msg_Err( &sys.demuxer, "No tracks supported" );
531 i_tracks_position = (int64_t) es.I_O().getFilePointer();
533 else if( MKV_IS_ID( el, KaxCues ) )
535 msg_Dbg( &sys.demuxer, "| + Cues" );
536 if( i_cues_position < 0 )
537 LoadCues( static_cast<KaxCues*>( el ) );
538 i_cues_position = (int64_t) es.I_O().getFilePointer();
540 else if( MKV_IS_ID( el, KaxCluster ) )
542 msg_Dbg( &sys.demuxer, "| + Cluster" );
544 cluster = (KaxCluster*)el;
546 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
550 /* stop pre-parsing the stream */
553 else if( MKV_IS_ID( el, KaxAttachments ) )
555 msg_Dbg( &sys.demuxer, "| + Attachments" );
556 if( i_attachments_position < 0 )
557 ParseAttachments( static_cast<KaxAttachments*>( el ) );
558 i_attachments_position = (int64_t) es.I_O().getFilePointer();
560 else if( MKV_IS_ID( el, KaxChapters ) )
562 msg_Dbg( &sys.demuxer, "| + Chapters" );
563 if( i_chapters_position < 0 )
564 ParseChapters( static_cast<KaxChapters*>( el ) );
565 i_chapters_position = (int64_t) es.I_O().getFilePointer();
567 else if( MKV_IS_ID( el, KaxTag ) )
569 msg_Dbg( &sys.demuxer, "| + Tags" );
570 /*FIXME if( i_tags_position < 0)
571 LoadTags( static_cast<KaxTags*>( el ) );*/
572 i_tags_position = (int64_t) es.I_O().getFilePointer();
574 else if( MKV_IS_ID( el, EbmlVoid ) )
575 msg_Dbg( &sys.demuxer, "| + Void" );
577 msg_Dbg( &sys.demuxer, "| + Preload Unknown (%s)", typeid(*el).name() );
585 /* Here we try to load elements that were found in Seek Heads, but not yet parsed */
586 bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position )
588 int64_t i_sav_position = (int64_t)es.I_O().getFilePointer();
591 es.I_O().setFilePointer( i_element_position, seek_beginning );
592 el = es.FindNextID( ClassInfos, 0xFFFFFFFFL);
596 msg_Err( &sys.demuxer, "cannot load some cues/chapters/tags etc. (broken seekhead or file)" );
597 es.I_O().setFilePointer( i_sav_position, seek_beginning );
601 if( MKV_IS_ID( el, KaxSeekHead ) )
603 /* Multiple allowed */
604 msg_Dbg( &sys.demuxer, "| + Seek head" );
605 if( i_seekhead_count < 10 )
607 i_seekhead_position = i_element_position;
608 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
611 else if( MKV_IS_ID( el, KaxInfo ) ) // FIXME
613 /* Multiple allowed, mandatory */
614 msg_Dbg( &sys.demuxer, "| + Information" );
615 if( i_info_position < 0 )
616 ParseInfo( static_cast<KaxInfo*>( el ) );
617 i_info_position = i_element_position;
619 else if( MKV_IS_ID( el, KaxTracks ) ) // FIXME
621 /* Multiple allowed */
622 msg_Dbg( &sys.demuxer, "| + Tracks" );
623 if( i_tracks_position < 0 )
624 ParseTracks( static_cast<KaxTracks*>( el ) );
625 if ( tracks.size() == 0 )
627 msg_Err( &sys.demuxer, "No tracks supported" );
629 es.I_O().setFilePointer( i_sav_position, seek_beginning );
632 i_tracks_position = i_element_position;
634 else if( MKV_IS_ID( el, KaxCues ) )
636 msg_Dbg( &sys.demuxer, "| + Cues" );
637 if( i_cues_position < 0 )
638 LoadCues( static_cast<KaxCues*>( el ) );
639 i_cues_position = i_element_position;
641 else if( MKV_IS_ID( el, KaxAttachments ) )
643 msg_Dbg( &sys.demuxer, "| + Attachments" );
644 if( i_attachments_position < 0 )
645 ParseAttachments( static_cast<KaxAttachments*>( el ) );
646 i_attachments_position = i_element_position;
648 else if( MKV_IS_ID( el, KaxChapters ) )
650 msg_Dbg( &sys.demuxer, "| + Chapters" );
651 if( i_chapters_position < 0 )
652 ParseChapters( static_cast<KaxChapters*>( el ) );
653 i_chapters_position = i_element_position;
655 else if( MKV_IS_ID( el, KaxTags ) )
657 msg_Dbg( &sys.demuxer, "| + Tags" );
658 if( i_tags_position < 0 )
659 LoadTags( static_cast<KaxTags*>( el ) );
660 i_tags_position = i_element_position;
664 msg_Dbg( &sys.demuxer, "| + LoadSeekHeadItem Unknown (%s)", typeid(*el).name() );
668 es.I_O().setFilePointer( i_sav_position, seek_beginning );
674 spoint(unsigned int tk, mtime_t date, int64_t pos, int64_t cpos):
675 i_track(tk),i_date(date), i_seek_pos(pos),
676 i_cluster_pos(cpos), p_next(NULL){}
677 unsigned int i_track;
680 int64_t i_cluster_pos;
684 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_global_position )
687 KaxSimpleBlock *simpleblock;
688 int64_t i_block_duration;
690 int64_t i_seek_position = i_start_pos;
691 int64_t i_seek_time = i_start_time;
693 spoint *p_first = NULL;
694 spoint *p_last = NULL;
696 bool b_has_key = false;
698 for( size_t i = 0; i < tracks.size(); i++)
699 tracks[i]->i_last_dts = VLC_TS_INVALID;
701 if( i_global_position >= 0 )
703 /* Special case for seeking in files with no cues */
704 EbmlElement *el = NULL;
706 /* Start from the last known index instead of the beginning eachtime */
708 es.I_O().setFilePointer( i_start_pos, seek_beginning );
710 es.I_O().setFilePointer( p_indexes[ i_index - 1 ].i_position,
713 ep = new EbmlParser( &es, segment, &sys.demuxer );
716 while( ( el = ep->Get() ) != NULL )
718 if( MKV_IS_ID( el, KaxCluster ) )
720 cluster = (KaxCluster *)el;
721 i_cluster_pos = cluster->GetElementPosition();
724 p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
727 IndexAppendCluster( cluster );
729 if( es.I_O().getFilePointer() >= (unsigned) i_global_position )
736 /* Don't try complex seek if we seek to 0 */
739 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, 0 );
740 es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 );
741 es.I_O().setFilePointer( i_start_pos );
744 ep = new EbmlParser( &es, segment, &sys.demuxer );
757 for( ; i_idx < i_index; i_idx++ )
758 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
764 i_seek_position = p_indexes[i_idx].i_position;
765 i_seek_time = p_indexes[i_idx].i_time;
768 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
769 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
771 es.I_O().setFilePointer( i_seek_position, seek_beginning );
774 ep = new EbmlParser( &es, segment, &sys.demuxer );
777 sys.i_start_pts = i_date;
779 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
781 /* now parse until key frame */
782 const int es_types[3] = { VIDEO_ES, AUDIO_ES, SPU_ES };
784 for( int i = 0; i < 2; i_cat = es_types[++i] )
786 for( i_track = 0; i_track < tracks.size(); i_track++ )
788 if( tracks[i_track]->fmt.i_cat == i_cat )
790 spoint * seekpoint = new spoint(i_track, i_seek_time, i_seek_position, i_seek_position);
791 if( unlikely( !seekpoint ) )
793 for( spoint * sp = p_first; sp; )
801 if( unlikely( !p_first ) )
808 p_last->p_next = seekpoint;
813 if( likely( p_first ) )
816 /*Neither video nor audio track... no seek further*/
817 if( unlikely( !p_first ) )
822 while( i_pts < i_date )
825 bool b_discardable_picture;
826 if( BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
828 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
832 /* check if block's track is in our list */
833 for( i_track = 0; i_track < tracks.size(); i_track++ )
835 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
836 (block && tracks[i_track]->i_number == block->TrackNum()) )
841 i_pts = sys.i_chapter_time + simpleblock->GlobalTimecode() / (mtime_t) 1000;
843 i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
844 if( i_track < tracks.size() )
846 if( tracks[i_track]->fmt.i_cat == i_cat && b_key_picture )
848 /* get the seekpoint */
850 for( sp = p_first; sp; sp = sp->p_next )
851 if( sp->i_track == i_track )
856 sp->i_seek_pos = simpleblock->GetElementPosition();
858 sp->i_seek_pos = i_block_pos;
859 sp->i_cluster_pos = i_cluster_pos;
866 if( b_has_key || !i_idx )
869 /* No key picture was found in the cluster seek to previous seekpoint */
870 i_date = i_time_offset + p_indexes[i_idx].i_time;
873 es.I_O().setFilePointer( p_indexes[i_idx].i_position );
875 ep = new EbmlParser( &es, segment, &sys.demuxer );
879 /* rewind to the last I img */
881 for( p_min = p_first, p_last = p_first; p_last; p_last = p_last->p_next )
882 if( p_last->i_date < p_min->i_date )
885 sys.i_pcr = sys.i_pts = p_min->i_date;
886 es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 + sys.i_pcr );
887 cluster = (KaxCluster *) ep->UnGet( p_min->i_seek_pos, p_min->i_cluster_pos );
889 /* hack use BlockGet to get the cluster then goto the wanted block */
893 bool b_discardable_picture;
894 BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration );
896 cluster = (KaxCluster *) ep->UnGet( p_min->i_seek_pos, p_min->i_cluster_pos );
902 p_first = p_first->p_next;
907 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
908 const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
911 for( i_track = 0; i_track < tracks.size(); i_track++ )
913 const mkv_track_t *tk = tracks[i_track];
915 if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
916 ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
922 if( i_track >= tracks.size() )
930 static inline void fill_extra_data( mkv_track_t *p_tk, unsigned int offset )
932 if(p_tk->i_extra_data <= offset) return;
933 p_tk->fmt.i_extra = p_tk->i_extra_data - offset;
934 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
935 if(!p_tk->fmt.p_extra) { p_tk->fmt.i_extra = 0; return; };
936 memcpy( p_tk->fmt.p_extra, p_tk->p_extra_data + offset, p_tk->fmt.i_extra );
939 bool matroska_segment_c::Select( mtime_t i_start_time )
942 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
944 bool b_has_default_video = false;
945 bool b_has_default_audio = false;
946 /* check for default */
947 for(size_t i_track = 0; i_track < tracks.size(); i_track++)
949 mkv_track_t *p_tk = tracks[i_track];
950 es_format_t *p_fmt = &p_tk->fmt;
951 if( p_fmt->i_cat == VIDEO_ES )
952 b_has_default_video |=
953 p_tk->b_enabled && ( p_tk->b_default || p_tk->b_forced );
954 else if( p_fmt->i_cat == AUDIO_ES )
955 b_has_default_audio |=
956 p_tk->b_enabled && ( p_tk->b_default || p_tk->b_forced );
959 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
961 mkv_track_t *p_tk = tracks[i_track];
962 es_format_t *p_fmt = &p_tk->fmt;
964 if( unlikely( p_fmt->i_cat == UNKNOWN_ES || !p_tk->psz_codec ) )
966 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, p_tk->i_number );
970 else if( unlikely( !b_has_default_video && p_fmt->i_cat == VIDEO_ES ) )
972 p_tk->b_default = true;
973 b_has_default_video = true;
975 else if( unlikely( !b_has_default_audio && p_fmt->i_cat == AUDIO_ES ) )
977 p_tk->b_default = true;
978 b_has_default_audio = true;
981 if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
983 if( p_tk->i_extra_data < (int)sizeof( VLC_BITMAPINFOHEADER ) )
985 msg_Err( &sys.demuxer, "missing/invalid VLC_BITMAPINFOHEADER" );
986 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
990 VLC_BITMAPINFOHEADER *p_bih = (VLC_BITMAPINFOHEADER*)p_tk->p_extra_data;
992 p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
993 p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
994 p_tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
996 p_tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( VLC_BITMAPINFOHEADER );
997 if( p_tk->fmt.i_extra > 0 )
999 /* Very unlikely yet possible: bug #5659*/
1000 size_t maxlen = p_tk->i_extra_data - sizeof( VLC_BITMAPINFOHEADER );
1001 p_tk->fmt.i_extra = ( (unsigned)p_tk->fmt.i_extra < maxlen )?
1002 p_tk->fmt.i_extra : maxlen;
1004 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1005 memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
1008 p_tk->b_dts_only = true;
1010 else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
1011 !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
1013 p_tk->fmt.i_codec = VLC_CODEC_MPGV;
1014 if( p_tk->i_extra_data )
1015 fill_extra_data( p_tk, 0 );
1017 else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
1019 p_tk->fmt.i_codec = VLC_CODEC_THEORA;
1020 fill_extra_data( p_tk, 0 );
1021 p_tk->b_pts_only = true;
1023 else if( !strncmp( p_tk->psz_codec, "V_REAL/RV", 9 ) )
1025 uint8_t *p = p_tk->p_extra_data;
1027 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
1028 p_fmt->i_codec = VLC_CODEC_RV10;
1029 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
1030 p_fmt->i_codec = VLC_CODEC_RV20;
1031 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
1032 p_fmt->i_codec = VLC_CODEC_RV30;
1033 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
1034 p_fmt->i_codec = VLC_CODEC_RV40;
1036 /* Extract the framerate from the header */
1037 if( p_tk->i_extra_data >= 26 &&
1038 p[4] == 'V' && p[5] == 'I' && p[6] == 'D' && p[7] == 'O' &&
1039 p[8] == 'R' && p[9] == 'V' &&
1040 (p[10] == '3' || p[10] == '4') && p[11] == '0' )
1042 p_tk->fmt.video.i_frame_rate =
1043 p[22] << 24 | p[23] << 16 | p[24] << 8 | p[25] << 0;
1044 p_tk->fmt.video.i_frame_rate_base = 65536;
1047 fill_extra_data( p_tk, 26 );
1048 p_tk->b_dts_only = true;
1050 else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
1052 p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
1054 else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
1056 p_tk->fmt.i_codec = VLC_CODEC_VP8;
1057 p_tk->b_pts_only = true;
1059 else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
1061 if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
1063 p_tk->fmt.i_codec = VLC_CODEC_DIV3;
1065 else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
1067 /* A MPEG 4 codec, SP, ASP, AP or AVC */
1068 if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1069 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1071 p_tk->fmt.i_codec = VLC_CODEC_MP4V;
1072 fill_extra_data( p_tk, 0 );
1075 else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
1077 MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
1078 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1082 if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
1083 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
1085 p_tk->fmt.i_codec = p_box->i_type;
1086 p_tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1087 p_tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1088 p_tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1089 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1090 memcpy( p_tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, p_tk->fmt.i_extra );
1091 MP4_FreeBox_sample_vide( p_box );
1097 stream_Delete( p_mp4_stream );
1099 else if( !strcmp( p_tk->psz_codec, "V_MJPEG" ) )
1101 p_tk->fmt.i_codec = VLC_CODEC_MJPG;
1103 else if( !strcmp( p_tk->psz_codec, "A_MS/ACM" ) )
1105 if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1107 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1108 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1112 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
1114 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
1116 if( p_tk->fmt.i_codec == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
1117 msg_Err( &sys.demuxer, "Unrecognized wf tag: 0x%x", GetWLE( &p_wf->wFormatTag ) );
1118 p_tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1119 p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1120 p_tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1121 p_tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1122 p_tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1124 p_tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1125 if( p_tk->fmt.i_extra > 0 )
1127 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1128 memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
1132 else if( !strcmp( p_tk->psz_codec, "A_MPEG/L3" ) ||
1133 !strcmp( p_tk->psz_codec, "A_MPEG/L2" ) ||
1134 !strcmp( p_tk->psz_codec, "A_MPEG/L1" ) )
1136 p_tk->fmt.i_codec = VLC_CODEC_MPGA;
1138 else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
1140 p_tk->fmt.i_codec = VLC_CODEC_A52;
1142 else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
1144 p_tk->fmt.i_codec = VLC_CODEC_EAC3;
1146 else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
1148 p_tk->fmt.i_codec = VLC_CODEC_DTS;
1150 else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
1152 p_tk->fmt.i_codec = VLC_CODEC_MLP;
1154 else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
1156 /* FIXME when more samples arrive */
1157 p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
1158 p_fmt->b_packetized = false;
1160 else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
1162 p_tk->fmt.i_codec = VLC_CODEC_FLAC;
1163 fill_extra_data( p_tk, 0 );
1165 else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
1167 p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
1168 fill_extra_data( p_tk, 0 );
1170 else if( !strncmp( p_tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1171 !strncmp( p_tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1173 int i_profile, i_srate, sbr = 0;
1174 static const unsigned int i_sample_rates[] =
1176 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1177 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1180 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1181 /* create data for faad (MP4DecSpecificDescrTag)*/
1183 if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
1187 else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
1191 else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
1195 else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
1205 for( i_srate = 0; i_srate < 13; i_srate++ )
1207 if( i_sample_rates[i_srate] == p_tk->i_original_rate )
1212 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1214 p_tk->fmt.i_extra = sbr ? 5 : 2;
1215 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1216 ((uint8_t*)p_tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1217 ((uint8_t*)p_tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (p_tk->fmt.audio.i_channels << 3);
1220 int syncExtensionType = 0x2B7;
1222 for (iDSRI=0; iDSRI<13; iDSRI++)
1223 if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
1225 ((uint8_t*)p_tk->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
1226 ((uint8_t*)p_tk->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
1227 ((uint8_t*)p_tk->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
1230 else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
1232 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1233 fill_extra_data( p_tk, 0 );
1235 else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
1237 p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
1238 fill_extra_data( p_tk, 0 );
1240 else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
1242 p_fmt->i_codec = VLC_CODEC_TTA;
1243 if( p_tk->i_extra_data > 0 )
1245 fill_extra_data( p_tk, 0 );
1249 p_fmt->i_extra = 30;
1250 p_fmt->p_extra = xmalloc( p_fmt->i_extra );
1251 uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
1252 memcpy( &p_extra[ 0], "TTA1", 4 );
1253 SetWLE( &p_extra[ 4], 1 );
1254 SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
1255 SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
1256 SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
1257 SetDWLE( &p_extra[14], 0xffffffff );
1258 memset( &p_extra[18], 0, 30 - 18 );
1261 else if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) ||
1262 !strcmp( p_tk->psz_codec, "A_PCM/INT/LIT" ) ||
1263 !strcmp( p_tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1265 if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
1267 p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1271 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1273 p_tk->fmt.audio.i_blockalign = ( p_tk->fmt.audio.i_bitspersample + 7 ) / 8 * p_tk->fmt.audio.i_channels;
1275 else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
1277 if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1279 p_fmt->i_codec = VLC_CODEC_RA_144;
1280 p_fmt->audio.i_channels = 1;
1281 p_fmt->audio.i_rate = 8000;
1282 p_fmt->audio.i_blockalign = 0x14;
1284 else if( p_tk->i_extra_data > 28 )
1286 uint8_t *p = p_tk->p_extra_data;
1287 if( memcmp( p, ".ra", 3 ) ) {
1288 msg_Err( &sys.demuxer, "Invalid Real ExtraData 0x%4.4s", (char *)p );
1289 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1293 real_audio_private * priv = (real_audio_private*) p_tk->p_extra_data;
1294 if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
1296 p_tk->fmt.i_codec = VLC_CODEC_COOK;
1297 p_tk->fmt.audio.i_blockalign = hton16(priv->sub_packet_size);
1299 else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
1300 p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
1301 else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
1302 p_tk->fmt.i_codec = VLC_CODEC_RA_288;
1303 /* FIXME RALF and SIPR */
1304 uint16_t version = (uint16_t) hton16(priv->version);
1306 new Cook_PrivateTrackData( hton16(priv->sub_packet_h),
1307 hton16(priv->frame_size),
1308 hton16(priv->sub_packet_size));
1309 if( unlikely( !p_tk->p_sys ) )
1312 if( unlikely( p_tk->p_sys->Init() ) )
1317 real_audio_private_v4 * v4 = (real_audio_private_v4*) priv;
1318 p_tk->fmt.audio.i_channels = hton16(v4->channels);
1319 p_tk->fmt.audio.i_bitspersample = hton16(v4->sample_size);
1320 p_tk->fmt.audio.i_rate = hton16(v4->sample_rate);
1322 else if( version == 5 )
1324 real_audio_private_v5 * v5 = (real_audio_private_v5*) priv;
1325 p_tk->fmt.audio.i_channels = hton16(v5->channels);
1326 p_tk->fmt.audio.i_bitspersample = hton16(v5->sample_size);
1327 p_tk->fmt.audio.i_rate = hton16(v5->sample_rate);
1329 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);
1331 fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 78);
1335 else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
1337 p_tk->fmt.i_codec = VLC_CODEC_KATE;
1338 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1340 fill_extra_data( p_tk, 0 );
1342 else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
1344 p_fmt->i_codec = VLC_CODEC_SUBT;
1345 p_fmt->subs.psz_encoding = strdup( "ASCII" );
1347 else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
1349 p_tk->fmt.i_codec = VLC_CODEC_SUBT;
1350 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1352 else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
1354 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1355 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1356 if( p_tk->i_extra_data )
1357 fill_extra_data( p_tk, 0 );
1359 else if( !strcmp( p_tk->psz_codec, "S_TEXT/SSA" ) ||
1360 !strcmp( p_tk->psz_codec, "S_TEXT/ASS" ) ||
1361 !strcmp( p_tk->psz_codec, "S_SSA" ) ||
1362 !strcmp( p_tk->psz_codec, "S_ASS" ))
1364 p_tk->fmt.i_codec = VLC_CODEC_SSA;
1365 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1366 if( p_tk->i_extra_data )
1367 fill_extra_data( p_tk, 0 );
1369 else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
1371 p_tk->fmt.i_codec = VLC_CODEC_SPU;
1372 if( p_tk->i_extra_data )
1375 char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
1376 if( psz_buf != NULL )
1378 memcpy( psz_buf, p_tk->p_extra_data , p_tk->i_extra_data );
1379 psz_buf[p_tk->i_extra_data] = '\0';
1381 psz_start = strstr( psz_buf, "size:" );
1383 vobsub_size_parse( psz_start,
1384 &p_tk->fmt.subs.spu.i_original_frame_width,
1385 &p_tk->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1387 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1388 p_tk->fmt.subs.spu.i_original_frame_width,
1389 p_tk->fmt.subs.spu.i_original_frame_height );
1393 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1396 psz_start = strstr( psz_buf, "palette:" );
1398 vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1400 p_tk->fmt.subs.spu.palette[0] = 0xBeef;
1401 msg_Dbg( &sys.demuxer, "vobsub palette read" );
1405 msg_Warn( &sys.demuxer, "reading original palette failed" );
1411 else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
1413 p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
1415 else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
1417 p_tk->fmt.i_cat = NAV_ES;
1422 msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
1423 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1425 if( unlikely( !p_tk->b_enabled ) )
1426 p_tk->fmt.i_priority = -2;
1427 else if( p_tk->b_forced )
1428 p_tk->fmt.i_priority = 2;
1429 else if( p_tk->b_default )
1430 p_tk->fmt.i_priority = 1;
1432 p_tk->fmt.i_priority = 0;
1434 /* Avoid multivideo tracks when unnecessary */
1435 if( p_tk->fmt.i_cat == VIDEO_ES )
1436 p_tk->fmt.i_priority--;
1438 p_tk->p_es = es_out_Add( sys.demuxer.out, &p_tk->fmt );
1440 /* Turn on a subtitles track if it has been flagged as default -
1441 * but only do this if no subtitles track has already been engaged,
1442 * either by an earlier 'default track' (??) or by default
1443 * language choice behaviour.
1445 if( p_tk->b_default )
1447 es_out_Control( sys.demuxer.out,
1448 ES_OUT_SET_ES_DEFAULT,
1452 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
1454 sys.i_start_pts = i_start_time;
1455 // reset the stream reading to the first cluster of the segment used
1456 es.I_O().setFilePointer( i_start_pos );
1459 ep = new EbmlParser( &es, segment, &sys.demuxer );
1464 void matroska_segment_c::UnSelect( )
1466 sys.p_ev->ResetPci();
1467 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1469 if ( tracks[i_track]->p_es != NULL )
1471 // es_format_Clean( &tracks[i_track]->fmt );
1472 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
1473 tracks[i_track]->p_es = NULL;
1480 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, bool *pb_key_picture, bool *pb_discardable_picture, int64_t *pi_duration )
1482 pp_simpleblock = NULL;
1485 *pb_key_picture = true;
1486 *pb_discardable_picture = false;
1490 EbmlElement *el = NULL;
1494 return VLC_EGENERIC;
1496 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1498 /* Check blocks validity to protect againts broken files */
1499 if( BlockFindTrackIndex( NULL, pp_block , pp_simpleblock ) )
1502 pp_simpleblock = NULL;
1506 if( pp_simpleblock != NULL )
1508 *pb_key_picture = pp_simpleblock->IsKeyframe();
1509 *pb_discardable_picture = pp_simpleblock->IsDiscardable();
1512 /* update the index */
1513 #define idx p_indexes[i_index - 1]
1514 if( i_index > 0 && idx.i_time == -1 )
1516 if ( pp_simpleblock != NULL )
1517 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1519 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1520 idx.b_key = *pb_key_picture;
1526 i_level = ep->GetLevel();
1535 msg_Warn( &sys.demuxer, "EOF" );
1536 return VLC_EGENERIC;
1539 /* Verify that we are still inside our cluster
1540 * It can happens whith broken files and when seeking
1544 if( cluster && !ep->IsTopPresent( cluster ) )
1546 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1557 if( MKV_IS_ID( el, KaxCluster ) )
1559 cluster = (KaxCluster*)el;
1560 i_cluster_pos = cluster->GetElementPosition();
1562 // reset silent tracks
1563 for (size_t i=0; i<tracks.size(); i++)
1565 tracks[i]->b_silent = false;
1570 else if( MKV_IS_ID( el, KaxCues ) )
1572 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1573 return VLC_EGENERIC;
1577 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1581 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1583 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1585 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1586 cluster->InitTimecode( uint64( ctc ), i_timescale );
1588 /* add it to the index */
1591 p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1592 IndexAppendCluster( cluster );
1594 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1598 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1600 i_block_pos = el->GetElementPosition();
1603 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1605 pp_simpleblock = (KaxSimpleBlock*)el;
1607 pp_simpleblock->ReadData( es.I_O() );
1608 pp_simpleblock->SetParent( *cluster );
1612 if( MKV_IS_ID( el, KaxBlock ) )
1614 pp_block = (KaxBlock*)el;
1616 pp_block->ReadData( es.I_O() );
1617 pp_block->SetParent( *cluster );
1621 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1623 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1625 dur.ReadData( es.I_O() );
1626 *pi_duration = uint64( dur );
1628 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1630 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1632 ref.ReadData( es.I_O() );
1634 if( *pb_key_picture )
1635 *pb_key_picture = false;
1636 else if( int64( ref ) > 0 )
1637 *pb_discardable_picture = true;
1639 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1641 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1642 track_num.ReadData( es.I_O() );
1644 for (size_t i=0; i<tracks.size(); i++)
1646 if ( tracks[i]->i_number == uint32(track_num))
1648 tracks[i]->b_silent = true;
1655 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1656 return VLC_EGENERIC;