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"
27 #include "chapters.hpp"
31 #include "Ebml_parser.hpp"
34 #include "../vobsub.h"
37 #include <vlc_codecs.h>
39 /* GetFourCC helper */
40 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
41 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
43 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
46 matroska_segment_c::matroska_segment_c( demux_sys_t & demuxer, EbmlStream & estream )
49 ,i_timescale(MKVD_TIMECODESCALE)
53 ,i_seekhead_position(-1)
55 ,i_tracks_position(-1)
57 ,i_chapters_position(-1)
59 ,i_attachments_position(-1)
65 ,p_prev_segment_uid(NULL)
66 ,p_next_segment_uid(NULL)
70 ,psz_muxing_application(NULL)
71 ,psz_writing_application(NULL)
72 ,psz_segment_filename(NULL)
79 ,b_ref_external_segments(false)
81 p_indexes = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
84 matroska_segment_c::~matroska_segment_c()
86 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
88 delete tracks[i_track]->p_compression_data;
89 es_format_Clean( &tracks[i_track]->fmt );
90 free( tracks[i_track]->p_extra_data );
91 free( tracks[i_track]->psz_codec );
92 delete tracks[i_track];
95 free( psz_writing_application );
96 free( psz_muxing_application );
97 free( psz_segment_filename );
104 delete p_segment_uid;
105 delete p_prev_segment_uid;
106 delete p_next_segment_uid;
108 vlc_delete_all( stored_editions );
109 vlc_delete_all( translations );
110 vlc_delete_all( families );
114 /*****************************************************************************
116 *****************************************************************************
117 * * LoadCues : load the cues element and update index
118 * * LoadTags : load ... the tags element
119 * * InformationCreate : create all information, load tags if present
120 *****************************************************************************/
121 void matroska_segment_c::LoadCues( KaxCues *cues )
128 msg_Err( &sys.demuxer, "There can be only 1 Cues per section." );
132 ep = new EbmlParser( &es, cues, &sys.demuxer );
133 while( ( el = ep->Get() ) != NULL )
135 if( MKV_IS_ID( el, KaxCuePoint ) )
137 #define idx p_indexes[i_index]
140 idx.i_block_number= -1;
146 while( ( el = ep->Get() ) != NULL )
148 if( MKV_IS_ID( el, KaxCueTime ) )
150 KaxCueTime &ctime = *(KaxCueTime*)el;
152 ctime.ReadData( es.I_O() );
154 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
156 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
159 while( ( el = ep->Get() ) != NULL )
161 if( MKV_IS_ID( el, KaxCueTrack ) )
163 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
165 ctrack.ReadData( es.I_O() );
166 idx.i_track = uint16( ctrack );
168 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
170 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
172 ccpos.ReadData( es.I_O() );
173 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
175 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
177 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
179 cbnum.ReadData( es.I_O() );
180 idx.i_block_number = uint32( cbnum );
184 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
191 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
197 msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
198 " track=%d bnum=%d", idx.i_time, idx.i_position,
199 idx.i_track, idx.i_block_number );
203 if( i_index >= i_index_max )
206 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
207 sizeof( mkv_index_t ) * i_index_max );
213 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
218 msg_Dbg( &sys.demuxer, "| - loading cues done." );
222 #define PARSE_TAG( type ) \
224 msg_Dbg( &sys.demuxer, "| + " type ); \
226 while( ( el = ep->Get() ) != NULL ) \
228 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() ); \
230 ep->Up(); } while( 0 )
232 static const struct {
233 vlc_meta_type_t type;
235 } metadata_map[] = { {vlc_meta_Title, "TITLE"},
236 {vlc_meta_Artist, "ARTIST"},
237 {vlc_meta_Genre, "GENRE"},
238 {vlc_meta_Copyright, "COPYRIGHT"},
239 {vlc_meta_Description, "DESCRIPTION"},
240 {vlc_meta_Publisher, "PUBLISHER"},
241 {vlc_meta_URL, "URL"},
242 {vlc_meta_TrackNumber, "PART_NUMBER"},
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 if( i_global_position >= 0 )
700 /* Special case for seeking in files with no cues */
701 EbmlElement *el = NULL;
703 /* Start from the last known index instead of the beginning eachtime */
705 es.I_O().setFilePointer( i_start_pos, seek_beginning );
707 es.I_O().setFilePointer( p_indexes[ i_index - 1 ].i_position,
710 ep = new EbmlParser( &es, segment, &sys.demuxer );
713 while( ( el = ep->Get() ) != NULL )
715 if( MKV_IS_ID( el, KaxCluster ) )
717 cluster = (KaxCluster *)el;
718 i_cluster_pos = cluster->GetElementPosition();
721 p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
724 IndexAppendCluster( cluster );
726 if( es.I_O().getFilePointer() >= (unsigned) i_global_position )
733 /* Don't try complex seek if we seek to 0 */
736 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, 0 );
737 es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 );
738 es.I_O().setFilePointer( i_start_pos );
741 ep = new EbmlParser( &es, segment, &sys.demuxer );
754 for( ; i_idx < i_index; i_idx++ )
755 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
761 i_seek_position = p_indexes[i_idx].i_position;
762 i_seek_time = p_indexes[i_idx].i_time;
765 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
766 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
768 es.I_O().setFilePointer( i_seek_position, seek_beginning );
771 ep = new EbmlParser( &es, segment, &sys.demuxer );
774 sys.i_start_pts = i_date;
776 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
778 /* now parse until key frame */
779 const int es_types[3] = { VIDEO_ES, AUDIO_ES, SPU_ES };
781 for( int i = 0; i < 2; i_cat = es_types[++i] )
783 for( i_track = 0; i_track < tracks.size(); i_track++ )
785 if( tracks[i_track]->fmt.i_cat == i_cat )
787 spoint * seekpoint = new spoint(i_track, i_seek_time, i_seek_position, i_seek_position);
788 if( unlikely( !seekpoint ) )
790 for( spoint * sp = p_first; sp; )
798 if( unlikely( !p_first ) )
805 p_last->p_next = seekpoint;
810 if( likely( p_first ) )
813 /*Neither video nor audio track... no seek further*/
814 if( unlikely( !p_first ) )
819 while( i_pts < i_date )
822 bool b_discardable_picture;
823 if( BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
825 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
829 /* check if block's track is in our list */
830 for( i_track = 0; i_track < tracks.size(); i_track++ )
832 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
833 (block && tracks[i_track]->i_number == block->TrackNum()) )
838 i_pts = sys.i_chapter_time + simpleblock->GlobalTimecode() / (mtime_t) 1000;
840 i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
841 if( i_track < tracks.size() )
843 if( tracks[i_track]->fmt.i_cat == i_cat && b_key_picture )
845 /* get the seekpoint */
847 for( sp = p_first; sp; sp = sp->p_next )
848 if( sp->i_track == i_track )
853 sp->i_seek_pos = simpleblock->GetElementPosition();
855 sp->i_seek_pos = i_block_pos;
856 sp->i_cluster_pos = i_cluster_pos;
863 if( b_has_key || !i_idx )
866 /* No key picture was found in the cluster seek to previous seekpoint */
867 i_date = i_time_offset + p_indexes[i_idx].i_time;
870 es.I_O().setFilePointer( p_indexes[i_idx].i_position );
872 ep = new EbmlParser( &es, segment, &sys.demuxer );
876 /* rewind to the last I img */
878 for( p_min = p_first, p_last = p_first; p_last; p_last = p_last->p_next )
879 if( p_last->i_date < p_min->i_date )
882 sys.i_pcr = sys.i_pts = p_min->i_date;
883 es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 + sys.i_pcr );
884 cluster = (KaxCluster *) ep->UnGet( p_min->i_seek_pos, p_min->i_cluster_pos );
886 /* hack use BlockGet to get the cluster then goto the wanted block */
890 bool b_discardable_picture;
891 BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration );
893 cluster = (KaxCluster *) ep->UnGet( p_min->i_seek_pos, p_min->i_cluster_pos );
899 p_first = p_first->p_next;
904 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
905 const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
908 for( i_track = 0; i_track < tracks.size(); i_track++ )
910 const mkv_track_t *tk = tracks[i_track];
912 if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
913 ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
919 if( i_track >= tracks.size() )
927 static inline void fill_extra_data( mkv_track_t *p_tk, unsigned int offset )
929 if(p_tk->i_extra_data <= offset) return;
930 p_tk->fmt.i_extra = p_tk->i_extra_data - offset;
931 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
932 if(!p_tk->fmt.p_extra) { p_tk->fmt.i_extra = 0; return; };
933 memcpy( p_tk->fmt.p_extra, p_tk->p_extra_data + offset, p_tk->fmt.i_extra );
936 bool matroska_segment_c::Select( mtime_t i_start_time )
939 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
941 bool b_has_default_video = false;
942 bool b_has_default_audio = false;
943 /* check for default */
944 for(size_t i_track = 0; i_track < tracks.size(); i_track++)
946 mkv_track_t *p_tk = tracks[i_track];
947 es_format_t *p_fmt = &p_tk->fmt;
948 if( p_fmt->i_cat == VIDEO_ES )
949 b_has_default_video |=
950 p_tk->b_enabled && ( p_tk->b_default || p_tk->b_forced );
951 else if( p_fmt->i_cat == AUDIO_ES )
952 b_has_default_audio |=
953 p_tk->b_enabled && ( p_tk->b_default || p_tk->b_forced );
956 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
958 mkv_track_t *p_tk = tracks[i_track];
959 es_format_t *p_fmt = &p_tk->fmt;
961 if( unlikely( p_fmt->i_cat == UNKNOWN_ES || !p_tk->psz_codec ) )
963 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, p_tk->i_number );
967 else if( unlikely( !b_has_default_video && p_fmt->i_cat == VIDEO_ES ) )
969 p_tk->b_default = true;
970 b_has_default_video = true;
972 else if( unlikely( !b_has_default_audio && p_fmt->i_cat == AUDIO_ES ) )
974 p_tk->b_default = true;
975 b_has_default_audio = true;
978 if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
980 if( p_tk->i_extra_data < (int)sizeof( VLC_BITMAPINFOHEADER ) )
982 msg_Err( &sys.demuxer, "missing/invalid VLC_BITMAPINFOHEADER" );
983 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
987 VLC_BITMAPINFOHEADER *p_bih = (VLC_BITMAPINFOHEADER*)p_tk->p_extra_data;
989 p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
990 p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
991 p_tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
993 p_tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( VLC_BITMAPINFOHEADER );
994 if( p_tk->fmt.i_extra > 0 )
996 /* Very unlikely yet possible: bug #5659*/
997 size_t maxlen = p_tk->i_extra_data - sizeof( VLC_BITMAPINFOHEADER );
998 p_tk->fmt.i_extra = ( (unsigned)p_tk->fmt.i_extra < maxlen )?
999 p_tk->fmt.i_extra : maxlen;
1001 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1002 memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
1005 p_tk->b_dts_only = true;
1007 else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
1008 !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
1010 p_tk->fmt.i_codec = VLC_CODEC_MPGV;
1011 if( p_tk->i_extra_data )
1012 fill_extra_data( p_tk, 0 );
1014 else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
1016 p_tk->fmt.i_codec = VLC_CODEC_THEORA;
1017 fill_extra_data( p_tk, 0 );
1018 p_tk->b_pts_only = true;
1020 else if( !strncmp( p_tk->psz_codec, "V_REAL/RV", 9 ) )
1022 uint8_t *p = p_tk->p_extra_data;
1024 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
1025 p_fmt->i_codec = VLC_CODEC_RV10;
1026 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
1027 p_fmt->i_codec = VLC_CODEC_RV20;
1028 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
1029 p_fmt->i_codec = VLC_CODEC_RV30;
1030 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
1031 p_fmt->i_codec = VLC_CODEC_RV40;
1033 /* Extract the framerate from the header */
1034 if( p_tk->i_extra_data >= 26 &&
1035 p[4] == 'V' && p[5] == 'I' && p[6] == 'D' && p[7] == 'O' &&
1036 p[8] == 'R' && p[9] == 'V' &&
1037 (p[10] == '3' || p[10] == '4') && p[11] == '0' )
1039 p_tk->fmt.video.i_frame_rate =
1040 p[22] << 24 | p[23] << 16 | p[24] << 8 | p[25] << 0;
1041 p_tk->fmt.video.i_frame_rate_base = 65536;
1044 fill_extra_data( p_tk, 26 );
1045 p_tk->b_dts_only = true;
1047 else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
1049 p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
1051 else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
1053 p_tk->fmt.i_codec = VLC_CODEC_VP8;
1054 p_tk->b_pts_only = true;
1056 else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
1058 if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
1060 p_tk->fmt.i_codec = VLC_CODEC_DIV3;
1062 else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
1064 /* A MPEG 4 codec, SP, ASP, AP or AVC */
1065 if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1066 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1068 p_tk->fmt.i_codec = VLC_CODEC_MP4V;
1069 fill_extra_data( p_tk, 0 );
1072 else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
1074 MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
1075 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1079 if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
1080 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
1082 p_tk->fmt.i_codec = p_box->i_type;
1083 p_tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1084 p_tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1085 p_tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1086 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1087 memcpy( p_tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, p_tk->fmt.i_extra );
1088 MP4_FreeBox_sample_vide( p_box );
1094 stream_Delete( p_mp4_stream );
1096 else if( !strcmp( p_tk->psz_codec, "V_MJPEG" ) )
1098 p_tk->fmt.i_codec = VLC_CODEC_MJPG;
1100 else if( !strcmp( p_tk->psz_codec, "A_MS/ACM" ) )
1102 if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1104 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1105 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1109 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
1111 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
1113 if( p_tk->fmt.i_codec == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
1114 msg_Err( &sys.demuxer, "Unrecognized wf tag: 0x%x", GetWLE( &p_wf->wFormatTag ) );
1115 p_tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1116 p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1117 p_tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1118 p_tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1119 p_tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1121 p_tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1122 if( p_tk->fmt.i_extra > 0 )
1124 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1125 memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
1129 else if( !strcmp( p_tk->psz_codec, "A_MPEG/L3" ) ||
1130 !strcmp( p_tk->psz_codec, "A_MPEG/L2" ) ||
1131 !strcmp( p_tk->psz_codec, "A_MPEG/L1" ) )
1133 p_tk->fmt.i_codec = VLC_CODEC_MPGA;
1135 else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
1137 p_tk->fmt.i_codec = VLC_CODEC_A52;
1139 else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
1141 p_tk->fmt.i_codec = VLC_CODEC_EAC3;
1143 else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
1145 p_tk->fmt.i_codec = VLC_CODEC_DTS;
1147 else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
1149 p_tk->fmt.i_codec = VLC_CODEC_MLP;
1151 else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
1153 /* FIXME when more samples arrive */
1154 p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
1155 p_fmt->b_packetized = false;
1157 else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
1159 p_tk->fmt.i_codec = VLC_CODEC_FLAC;
1160 fill_extra_data( p_tk, 0 );
1162 else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
1164 p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
1165 fill_extra_data( p_tk, 0 );
1167 else if( !strncmp( p_tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1168 !strncmp( p_tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1170 int i_profile, i_srate, sbr = 0;
1171 static const unsigned int i_sample_rates[] =
1173 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1174 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1177 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1178 /* create data for faad (MP4DecSpecificDescrTag)*/
1180 if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
1184 else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
1188 else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
1192 else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
1202 for( i_srate = 0; i_srate < 13; i_srate++ )
1204 if( i_sample_rates[i_srate] == p_tk->i_original_rate )
1209 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1211 p_tk->fmt.i_extra = sbr ? 5 : 2;
1212 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1213 ((uint8_t*)p_tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1214 ((uint8_t*)p_tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (p_tk->fmt.audio.i_channels << 3);
1217 int syncExtensionType = 0x2B7;
1219 for (iDSRI=0; iDSRI<13; iDSRI++)
1220 if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
1222 ((uint8_t*)p_tk->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
1223 ((uint8_t*)p_tk->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
1224 ((uint8_t*)p_tk->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
1227 else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
1229 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1230 fill_extra_data( p_tk, 0 );
1232 else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
1234 p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
1235 fill_extra_data( p_tk, 0 );
1237 else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
1239 p_fmt->i_codec = VLC_CODEC_TTA;
1240 if( p_tk->i_extra_data > 0 )
1242 fill_extra_data( p_tk, 0 );
1246 p_fmt->i_extra = 30;
1247 p_fmt->p_extra = xmalloc( p_fmt->i_extra );
1248 uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
1249 memcpy( &p_extra[ 0], "TTA1", 4 );
1250 SetWLE( &p_extra[ 4], 1 );
1251 SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
1252 SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
1253 SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
1254 SetDWLE( &p_extra[14], 0xffffffff );
1255 memset( &p_extra[18], 0, 30 - 18 );
1258 else if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) ||
1259 !strcmp( p_tk->psz_codec, "A_PCM/INT/LIT" ) ||
1260 !strcmp( p_tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1262 if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
1264 p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1268 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1270 p_tk->fmt.audio.i_blockalign = ( p_tk->fmt.audio.i_bitspersample + 7 ) / 8 * p_tk->fmt.audio.i_channels;
1272 else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
1274 if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1276 p_fmt->i_codec = VLC_CODEC_RA_144;
1277 p_fmt->audio.i_channels = 1;
1278 p_fmt->audio.i_rate = 8000;
1279 p_fmt->audio.i_blockalign = 0x14;
1281 else if( p_tk->i_extra_data > 28 )
1283 uint8_t *p = p_tk->p_extra_data;
1284 if( memcmp( p, ".ra", 3 ) ) {
1285 msg_Err( &sys.demuxer, "Invalid Real ExtraData 0x%4.4s", (char *)p );
1286 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1289 if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
1290 p_tk->fmt.i_codec = VLC_CODEC_COOK;
1291 else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
1292 p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
1293 else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
1294 p_tk->fmt.i_codec = VLC_CODEC_RA_288;
1295 /* FIXME RALF and SIPR */
1297 fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 78);
1301 else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
1303 p_tk->fmt.i_codec = VLC_CODEC_KATE;
1304 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1306 fill_extra_data( p_tk, 0 );
1308 else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
1310 p_fmt->i_codec = VLC_CODEC_SUBT;
1311 p_fmt->subs.psz_encoding = strdup( "ASCII" );
1313 else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
1315 p_tk->fmt.i_codec = VLC_CODEC_SUBT;
1316 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1318 else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
1320 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1321 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1322 if( p_tk->i_extra_data )
1323 fill_extra_data( p_tk, 0 );
1325 else if( !strcmp( p_tk->psz_codec, "S_TEXT/SSA" ) ||
1326 !strcmp( p_tk->psz_codec, "S_TEXT/ASS" ) ||
1327 !strcmp( p_tk->psz_codec, "S_SSA" ) ||
1328 !strcmp( p_tk->psz_codec, "S_ASS" ))
1330 p_tk->fmt.i_codec = VLC_CODEC_SSA;
1331 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1332 if( p_tk->i_extra_data )
1333 fill_extra_data( p_tk, 0 );
1335 else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
1337 p_tk->fmt.i_codec = VLC_CODEC_SPU;
1338 if( p_tk->i_extra_data )
1341 char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
1342 if( psz_buf != NULL )
1344 memcpy( psz_buf, p_tk->p_extra_data , p_tk->i_extra_data );
1345 psz_buf[p_tk->i_extra_data] = '\0';
1347 psz_start = strstr( psz_buf, "size:" );
1349 vobsub_size_parse( psz_start,
1350 &p_tk->fmt.subs.spu.i_original_frame_width,
1351 &p_tk->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1353 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1354 p_tk->fmt.subs.spu.i_original_frame_width,
1355 p_tk->fmt.subs.spu.i_original_frame_height );
1359 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1362 psz_start = strstr( psz_buf, "palette:" );
1364 vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1366 p_tk->fmt.subs.spu.palette[0] = 0xBeef;
1367 msg_Dbg( &sys.demuxer, "vobsub palette read" );
1371 msg_Warn( &sys.demuxer, "reading original palette failed" );
1377 else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
1379 p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
1381 else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
1383 p_tk->fmt.i_cat = NAV_ES;
1388 msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
1389 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1391 if( unlikely( !p_tk->b_enabled ) )
1392 p_tk->fmt.i_priority = -2;
1393 else if( p_tk->b_forced )
1394 p_tk->fmt.i_priority = 2;
1395 else if( p_tk->b_default )
1396 p_tk->fmt.i_priority = 1;
1398 p_tk->fmt.i_priority = 0;
1400 /* Avoid multivideo tracks when unnecessary */
1401 if( p_tk->fmt.i_cat == VIDEO_ES )
1402 p_tk->fmt.i_priority--;
1404 p_tk->p_es = es_out_Add( sys.demuxer.out, &p_tk->fmt );
1406 /* Turn on a subtitles track if it has been flagged as default -
1407 * but only do this if no subtitles track has already been engaged,
1408 * either by an earlier 'default track' (??) or by default
1409 * language choice behaviour.
1411 if( p_tk->b_default )
1413 es_out_Control( sys.demuxer.out,
1414 ES_OUT_SET_ES_DEFAULT,
1418 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
1420 sys.i_start_pts = i_start_time;
1421 // reset the stream reading to the first cluster of the segment used
1422 es.I_O().setFilePointer( i_start_pos );
1425 ep = new EbmlParser( &es, segment, &sys.demuxer );
1430 void matroska_segment_c::UnSelect( )
1432 sys.p_ev->ResetPci();
1433 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1435 if ( tracks[i_track]->p_es != NULL )
1437 // es_format_Clean( &tracks[i_track]->fmt );
1438 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
1439 tracks[i_track]->p_es = NULL;
1446 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, bool *pb_key_picture, bool *pb_discardable_picture, int64_t *pi_duration )
1448 pp_simpleblock = NULL;
1451 *pb_key_picture = true;
1452 *pb_discardable_picture = false;
1456 EbmlElement *el = NULL;
1460 return VLC_EGENERIC;
1462 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1464 /* Check blocks validity to protect againts broken files */
1465 if( BlockFindTrackIndex( NULL, pp_block , pp_simpleblock ) )
1468 pp_simpleblock = NULL;
1472 if( pp_simpleblock != NULL )
1474 *pb_key_picture = pp_simpleblock->IsKeyframe();
1475 *pb_discardable_picture = pp_simpleblock->IsDiscardable();
1478 /* update the index */
1479 #define idx p_indexes[i_index - 1]
1480 if( i_index > 0 && idx.i_time == -1 )
1482 if ( pp_simpleblock != NULL )
1483 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1485 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1486 idx.b_key = *pb_key_picture;
1492 i_level = ep->GetLevel();
1501 msg_Warn( &sys.demuxer, "EOF" );
1502 return VLC_EGENERIC;
1505 /* Verify that we are still inside our cluster
1506 * It can happens whith broken files and when seeking
1510 if( cluster && !ep->IsTopPresent( cluster ) )
1512 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1523 if( MKV_IS_ID( el, KaxCluster ) )
1525 cluster = (KaxCluster*)el;
1526 i_cluster_pos = cluster->GetElementPosition();
1528 // reset silent tracks
1529 for (size_t i=0; i<tracks.size(); i++)
1531 tracks[i]->b_silent = false;
1536 else if( MKV_IS_ID( el, KaxCues ) )
1538 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1539 return VLC_EGENERIC;
1543 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1547 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1549 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1551 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1552 cluster->InitTimecode( uint64( ctc ), i_timescale );
1554 /* add it to the index */
1557 p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1558 IndexAppendCluster( cluster );
1560 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1564 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1566 i_block_pos = el->GetElementPosition();
1569 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1571 pp_simpleblock = (KaxSimpleBlock*)el;
1573 pp_simpleblock->ReadData( es.I_O() );
1574 pp_simpleblock->SetParent( *cluster );
1578 if( MKV_IS_ID( el, KaxBlock ) )
1580 pp_block = (KaxBlock*)el;
1582 pp_block->ReadData( es.I_O() );
1583 pp_block->SetParent( *cluster );
1587 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1589 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1591 dur.ReadData( es.I_O() );
1592 *pi_duration = uint64( dur );
1594 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1596 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1598 ref.ReadData( es.I_O() );
1600 if( *pb_key_picture )
1601 *pb_key_picture = false;
1602 else if( int64( ref ) > 0 )
1603 *pb_discardable_picture = true;
1605 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1607 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1608 track_num.ReadData( es.I_O() );
1610 for (size_t i=0; i<tracks.size(); i++)
1612 if ( tracks[i]->i_number == uint32(track_num))
1614 tracks[i]->b_silent = true;
1621 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1622 return VLC_EGENERIC;