1 /*****************************************************************************
2 * mkv.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"
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] );
45 matroska_segment_c::~matroska_segment_c()
47 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
49 delete tracks[i_track]->p_compression_data;
50 es_format_Clean( &tracks[i_track]->fmt );
51 free( tracks[i_track]->p_extra_data );
52 free( tracks[i_track]->psz_codec );
53 delete tracks[i_track];
56 free( psz_writing_application );
57 free( psz_muxing_application );
58 free( psz_segment_filename );
66 delete p_prev_segment_uid;
67 delete p_next_segment_uid;
69 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
70 while ( index != stored_editions.end() )
75 std::vector<chapter_translation_c*>::iterator indext = translations.begin();
76 while ( indext != translations.end() )
81 std::vector<KaxSegmentFamily*>::iterator indexf = families.begin();
82 while ( indexf != families.end() )
90 /*****************************************************************************
92 *****************************************************************************
93 * * LoadCues : load the cues element and update index
94 * * LoadTags : load ... the tags element
95 * * InformationCreate : create all information, load tags if present
96 *****************************************************************************/
97 void matroska_segment_c::LoadCues( KaxCues *cues )
104 msg_Err( &sys.demuxer, "There can be only 1 Cues per section." );
108 ep = new EbmlParser( &es, cues, &sys.demuxer );
109 while( ( el = ep->Get() ) != NULL )
111 if( MKV_IS_ID( el, KaxCuePoint ) )
113 #define idx p_indexes[i_index]
116 idx.i_block_number= -1;
122 while( ( el = ep->Get() ) != NULL )
124 if( MKV_IS_ID( el, KaxCueTime ) )
126 KaxCueTime &ctime = *(KaxCueTime*)el;
128 ctime.ReadData( es.I_O() );
130 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
132 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
135 while( ( el = ep->Get() ) != NULL )
137 if( MKV_IS_ID( el, KaxCueTrack ) )
139 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
141 ctrack.ReadData( es.I_O() );
142 idx.i_track = uint16( ctrack );
144 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
146 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
148 ccpos.ReadData( es.I_O() );
149 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
151 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
153 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
155 cbnum.ReadData( es.I_O() );
156 idx.i_block_number = uint32( cbnum );
160 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
167 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
173 msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
174 " track=%d bnum=%d", idx.i_time, idx.i_position,
175 idx.i_track, idx.i_block_number );
179 if( i_index >= i_index_max )
182 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
183 sizeof( mkv_index_t ) * i_index_max );
189 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
194 msg_Dbg( &sys.demuxer, "| - loading cues done." );
198 #define PARSE_TAG( type ) \
200 msg_Dbg( &sys.demuxer, "| + " type ); \
202 while( ( el = ep->Get() ) != NULL ) \
204 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() ); \
206 ep->Up(); } while( 0 )
208 static const struct {
209 vlc_meta_type_t type;
211 } metadata_map[] = { {vlc_meta_Title, "TITLE"},
212 {vlc_meta_Artist, "ARTIST"},
213 {vlc_meta_Genre, "GENRE"},
214 {vlc_meta_Copyright, "COPYRIGHT"},
215 {vlc_meta_Description, "DESCRIPTION"},
216 {vlc_meta_Publisher, "PUBLISHER"},
217 {vlc_meta_URL, "URL"},
218 {vlc_meta_Title, NULL},
221 void matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag )
224 EbmlParser *ep = new EbmlParser( &es, tag, &sys.demuxer );
225 char *k = NULL, *v = NULL;
228 sys.meta = vlc_meta_New();
230 msg_Dbg( &sys.demuxer, "| + Simple Tag ");
231 while( ( el = ep->Get() ) != NULL )
233 if( MKV_IS_ID( el, KaxTagName ) )
235 KaxTagName &key = *(KaxTagName*)el;
236 key.ReadData( es.I_O(), SCOPE_ALL_DATA );
237 k = strdup( UTFstring( key ).GetUTF8().c_str() );
239 if( MKV_IS_ID( el, KaxTagString ) )
241 KaxTagString &value = *(KaxTagString*)el;
242 value.ReadData( es.I_O(), SCOPE_ALL_DATA );
243 v = strdup( UTFstring( value ).GetUTF8().c_str() );
250 msg_Warn( &sys.demuxer, "Invalid MKV SimpleTag found.");
254 for( int i = 0; metadata_map[i].key; i++ )
256 if( !strcmp( k, metadata_map[i].key ) )
258 vlc_meta_Set( sys.meta, metadata_map[i].type, v );
262 vlc_meta_AddExtra( sys.meta, k, v );
269 void matroska_segment_c::LoadTags( KaxTags *tags )
271 /* Master elements */
272 EbmlParser *ep = new EbmlParser( &es, tags, &sys.demuxer );
275 while( ( el = ep->Get() ) != NULL )
277 if( MKV_IS_ID( el, KaxTag ) )
279 msg_Dbg( &sys.demuxer, "+ Tag" );
281 while( ( el = ep->Get() ) != NULL )
283 if( MKV_IS_ID( el, KaxTagTargets ) )
284 PARSE_TAG( "Targets" );
285 else if( MKV_IS_ID( el, KaxTagGeneral ) )
286 PARSE_TAG( "General" );
287 else if( MKV_IS_ID( el, KaxTagGenres ) )
288 PARSE_TAG( "Genres" );
289 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
290 PARSE_TAG( "Audio Specific" );
291 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
292 PARSE_TAG( "Images Specific" );
293 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
295 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
297 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
299 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
301 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
303 msg_Dbg( &sys.demuxer, "| + Multi Date" );
305 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
307 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
309 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
311 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
313 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
315 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
317 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
319 msg_Dbg( &sys.demuxer, "| + Multi Title" );
321 else if( MKV_IS_ID( el, KaxTagSimple ) )
322 ParseSimpleTags( static_cast<KaxTagSimple*>( el ) );
325 msg_Dbg( &sys.demuxer, "| + LoadTag Unknown (%s)", typeid( *el ).name() );
332 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
337 msg_Dbg( &sys.demuxer, "loading tags done." );
341 /*****************************************************************************
343 *****************************************************************************/
344 void matroska_segment_c::InformationCreate( )
347 sys.meta = vlc_meta_New();
351 vlc_meta_SetTitle( sys.meta, psz_title );
355 vlc_meta_SetDate( sys.meta, psz_date_utc );
358 if( psz_segment_filename )
360 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
362 if( psz_muxing_application )
364 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
366 if( psz_writing_application )
368 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
371 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
373 // mkv_track_t *tk = tracks[i_track];
374 // vlc_meta_t *mtk = vlc_meta_New();
375 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
379 if( i_tags_position >= 0 )
383 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
393 /*****************************************************************************
395 *****************************************************************************/
397 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
399 #define idx p_indexes[i_index]
401 idx.i_block_number= -1;
402 idx.i_position = cluster->GetElementPosition();
407 if( i_index >= i_index_max )
410 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
411 sizeof( mkv_index_t ) * i_index_max );
416 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
421 for (size_t i=0; i<families.size(); i++)
423 for (size_t j=0; j<of_segment.families.size(); j++)
425 if ( *(families[i]) == *(of_segment.families[j]) )
433 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
437 if ( p_item_a == NULL || p_item_b == NULL )
440 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
441 if ( p_item_b->p_prev_segment_uid != NULL
442 && *p_tmp == *p_item_b->p_prev_segment_uid )
445 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
449 if ( p_item_b->p_segment_uid != NULL
450 && *p_tmp == *p_item_b->p_segment_uid )
453 if ( p_item_b->p_prev_segment_uid != NULL
454 && *p_tmp == *p_item_b->p_prev_segment_uid )
460 bool matroska_segment_c::Preload( )
465 EbmlElement *el = NULL;
467 ep->Reset( &sys.demuxer );
469 while( ( el = ep->Get() ) != NULL )
471 if( MKV_IS_ID( el, KaxSeekHead ) )
473 /* Multiple allowed */
474 /* We bail at 10, to prevent possible recursion */
475 msg_Dbg( &sys.demuxer, "| + Seek head" );
476 if( i_seekhead_count < 10 )
478 i_seekhead_position = (int64_t) es.I_O().getFilePointer();
479 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
482 else if( MKV_IS_ID( el, KaxInfo ) )
484 /* Multiple allowed, mandatory */
485 msg_Dbg( &sys.demuxer, "| + Information" );
486 if( i_info_position < 0 ) // FIXME
487 ParseInfo( static_cast<KaxInfo*>( el ) );
488 i_info_position = (int64_t) es.I_O().getFilePointer();
490 else if( MKV_IS_ID( el, KaxTracks ) )
492 /* Multiple allowed */
493 msg_Dbg( &sys.demuxer, "| + Tracks" );
494 if( i_tracks_position < 0 ) // FIXME
495 ParseTracks( static_cast<KaxTracks*>( el ) );
496 if ( tracks.size() == 0 )
498 msg_Err( &sys.demuxer, "No tracks supported" );
501 i_tracks_position = (int64_t) es.I_O().getFilePointer();
503 else if( MKV_IS_ID( el, KaxCues ) )
505 msg_Dbg( &sys.demuxer, "| + Cues" );
506 if( i_cues_position < 0 )
507 LoadCues( static_cast<KaxCues*>( el ) );
508 i_cues_position = (int64_t) es.I_O().getFilePointer();
510 else if( MKV_IS_ID( el, KaxCluster ) )
512 msg_Dbg( &sys.demuxer, "| + Cluster" );
514 cluster = (KaxCluster*)el;
516 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
520 /* stop pre-parsing the stream */
523 else if( MKV_IS_ID( el, KaxAttachments ) )
525 msg_Dbg( &sys.demuxer, "| + Attachments" );
526 if( i_attachments_position < 0 )
527 ParseAttachments( static_cast<KaxAttachments*>( el ) );
528 i_attachments_position = (int64_t) es.I_O().getFilePointer();
530 else if( MKV_IS_ID( el, KaxChapters ) )
532 msg_Dbg( &sys.demuxer, "| + Chapters" );
533 if( i_chapters_position < 0 )
534 ParseChapters( static_cast<KaxChapters*>( el ) );
535 i_chapters_position = (int64_t) es.I_O().getFilePointer();
537 else if( MKV_IS_ID( el, KaxTag ) )
539 msg_Dbg( &sys.demuxer, "| + Tags" );
540 if( i_tags_position < 0) // FIXME
541 ;//LoadTags( static_cast<KaxTags*>( el ) );
542 i_tags_position = (int64_t) es.I_O().getFilePointer();
544 else if( MKV_IS_ID( el, EbmlVoid ) )
545 msg_Dbg( &sys.demuxer, "| + Void" );
547 msg_Dbg( &sys.demuxer, "| + Preload Unknown (%s)", typeid(*el).name() );
555 /* Here we try to load elements that were found in Seek Heads, but not yet parsed */
556 bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position )
558 int64_t i_sav_position = (int64_t)es.I_O().getFilePointer();
561 es.I_O().setFilePointer( i_element_position, seek_beginning );
562 el = es.FindNextID( ClassInfos, 0xFFFFFFFFL);
566 msg_Err( &sys.demuxer, "cannot load some cues/chapters/tags etc. (broken seekhead or file)" );
567 es.I_O().setFilePointer( i_sav_position, seek_beginning );
571 if( MKV_IS_ID( el, KaxSeekHead ) )
573 /* Multiple allowed */
574 msg_Dbg( &sys.demuxer, "| + Seek head" );
575 if( i_seekhead_count < 10 )
577 i_seekhead_position = i_element_position;
578 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
581 else if( MKV_IS_ID( el, KaxInfo ) ) // FIXME
583 /* Multiple allowed, mandatory */
584 msg_Dbg( &sys.demuxer, "| + Information" );
585 if( i_info_position < 0 )
586 ParseInfo( static_cast<KaxInfo*>( el ) );
587 i_info_position = i_element_position;
589 else if( MKV_IS_ID( el, KaxTracks ) ) // FIXME
591 /* Multiple allowed */
592 msg_Dbg( &sys.demuxer, "| + Tracks" );
593 if( i_tracks_position < 0 )
594 ParseTracks( static_cast<KaxTracks*>( el ) );
595 if ( tracks.size() == 0 )
597 msg_Err( &sys.demuxer, "No tracks supported" );
599 es.I_O().setFilePointer( i_sav_position, seek_beginning );
602 i_tracks_position = i_element_position;
604 else if( MKV_IS_ID( el, KaxCues ) )
606 msg_Dbg( &sys.demuxer, "| + Cues" );
607 if( i_cues_position < 0 )
608 LoadCues( static_cast<KaxCues*>( el ) );
609 i_cues_position = i_element_position;
611 else if( MKV_IS_ID( el, KaxAttachments ) )
613 msg_Dbg( &sys.demuxer, "| + Attachments" );
614 if( i_attachments_position < 0 )
615 ParseAttachments( static_cast<KaxAttachments*>( el ) );
616 i_attachments_position = i_element_position;
618 else if( MKV_IS_ID( el, KaxChapters ) )
620 msg_Dbg( &sys.demuxer, "| + Chapters" );
621 if( i_chapters_position < 0 )
622 ParseChapters( static_cast<KaxChapters*>( el ) );
623 i_chapters_position = i_element_position;
625 else if( MKV_IS_ID( el, KaxTags ) )
627 msg_Dbg( &sys.demuxer, "| + Tags" );
628 if( i_tags_position < 0 )
629 LoadTags( static_cast<KaxTags*>( el ) );
630 i_tags_position = i_element_position;
634 msg_Dbg( &sys.demuxer, "| + LoadSeekHeadItem Unknown (%s)", typeid(*el).name() );
638 es.I_O().setFilePointer( i_sav_position, seek_beginning );
642 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_global_position )
645 KaxSimpleBlock *simpleblock;
646 int i_track_skipping;
647 int64_t i_block_duration;
649 int64_t i_seek_position = i_start_pos;
650 int64_t i_seek_time = i_start_time;
652 if( i_global_position >= 0 )
654 /* Special case for seeking in files with no cues */
655 EbmlElement *el = NULL;
656 es.I_O().setFilePointer( i_start_pos, seek_beginning );
658 ep = new EbmlParser( &es, segment, &sys.demuxer );
661 while( ( el = ep->Get() ) != NULL )
663 if( MKV_IS_ID( el, KaxCluster ) )
665 cluster = (KaxCluster *)el;
666 i_cluster_pos = cluster->GetElementPosition();
668 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
670 IndexAppendCluster( cluster );
672 if( es.I_O().getFilePointer() >= i_global_position )
675 msg_Dbg( &sys.demuxer, "we found a cluster that is in the neighbourhood" );
680 msg_Err( &sys.demuxer, "This file has no cues, and we were unable to seek to the requested position by parsing." );
688 for( ; i_idx < i_index; i_idx++ )
690 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
701 i_seek_position = p_indexes[i_idx].i_position;
702 i_seek_time = p_indexes[i_idx].i_time;
705 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
706 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
708 es.I_O().setFilePointer( i_seek_position, seek_beginning );
711 ep = new EbmlParser( &es, segment, &sys.demuxer );
714 sys.i_start_pts = i_date;
716 /* now parse until key frame */
717 i_track_skipping = 0;
718 for( i_track = 0; i_track < tracks.size(); i_track++ )
720 if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
722 tracks[i_track]->b_search_keyframe = true;
726 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
728 while( i_track_skipping > 0 )
731 bool b_discardable_picture;
732 if( BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
734 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
739 for( i_track = 0; i_track < tracks.size(); i_track++ )
741 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
742 (block && tracks[i_track]->i_number == block->TrackNum()) )
749 sys.i_pts = (sys.i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
751 sys.i_pts = (sys.i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
753 if( i_track < tracks.size() )
755 if( sys.i_pts > sys.i_start_pts )
757 cluster = static_cast<KaxCluster*>(ep->UnGet( i_block_pos, i_cluster_pos ));
758 i_track_skipping = 0;
760 else if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
762 if( b_key_picture && tracks[i_track]->b_search_keyframe )
764 tracks[i_track]->b_search_keyframe = false;
767 if( !tracks[i_track]->b_search_keyframe )
769 BlockDecode( &sys.demuxer, block, simpleblock, sys.i_pts, 0, b_key_picture || b_discardable_picture );
777 /* FIXME current ES_OUT_SET_NEXT_DISPLAY_TIME does not work that well if
778 * the delay is too high. */
779 if( sys.i_pts + 500*1000 < sys.i_start_pts )
781 sys.i_start_pts = sys.i_pts;
783 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, sys.i_start_pts );
787 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
788 const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
791 for( i_track = 0; i_track < tracks.size(); i_track++ )
793 const mkv_track_t *tk = tracks[i_track];
795 if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
796 ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
802 if( i_track >= tracks.size() )
810 static inline void fill_extra_data( mkv_track_t *p_tk )
812 p_tk->fmt.i_extra = p_tk->i_extra_data;
813 p_tk->fmt.p_extra = xmalloc( p_tk->i_extra_data );
814 memcpy( p_tk->fmt.p_extra, p_tk->p_extra_data, p_tk->i_extra_data );
817 bool matroska_segment_c::Select( mtime_t i_start_time )
820 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
821 sys.b_pci_packet_set = false;
823 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
825 mkv_track_t *p_tk = tracks[i_track];
826 es_format_t *p_fmt = &p_tk->fmt;
828 if( p_fmt->i_cat == UNKNOWN_ES || !p_tk->psz_codec )
830 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, p_tk->i_number );
835 if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
837 if( p_tk->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
839 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
840 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
844 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)p_tk->p_extra_data;
846 p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
847 p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
848 p_tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
850 p_tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
851 if( p_tk->fmt.i_extra > 0 )
853 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
854 memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
857 p_tk->b_dts_only = true;
859 else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
860 !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
862 p_tk->fmt.i_codec = VLC_CODEC_MPGV;
863 if( p_tk->i_extra_data )
864 fill_extra_data( p_tk );
866 else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
868 p_tk->fmt.i_codec = VLC_CODEC_THEORA;
869 fill_extra_data( p_tk );
870 p_tk->b_pts_only = true;
872 else if( !strncmp( p_tk->psz_codec, "V_REAL/RV", 9 ) )
874 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
875 p_fmt->i_codec = VLC_CODEC_RV10;
876 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
877 p_fmt->i_codec = VLC_CODEC_RV20;
878 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
879 p_fmt->i_codec = VLC_CODEC_RV30;
880 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
881 p_fmt->i_codec = VLC_CODEC_RV40;
883 if( p_tk->i_extra_data > 26 )
885 p_fmt->p_extra = malloc( p_tk->i_extra_data - 26 );
888 p_fmt->i_extra = p_tk->i_extra_data - 26;
889 memcpy( p_fmt->p_extra, &p_tk->p_extra_data[26], p_fmt->i_extra );
892 p_tk->b_dts_only = true;
894 else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
896 p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
898 else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
900 p_tk->fmt.i_codec = VLC_CODEC_VP8;
901 p_tk->b_pts_only = true;
903 else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
905 if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
907 p_tk->fmt.i_codec = VLC_CODEC_DIV3;
909 else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
911 /* A MPEG 4 codec, SP, ASP, AP or AVC */
912 if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
913 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
915 p_tk->fmt.i_codec = VLC_CODEC_MP4V;
916 fill_extra_data( p_tk );
919 else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
921 MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
922 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
926 if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
927 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
929 p_tk->fmt.i_codec = p_box->i_type;
930 p_tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
931 p_tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
932 p_tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
933 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
934 memcpy( p_tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, p_tk->fmt.i_extra );
935 MP4_FreeBox_sample_vide( p_box );
941 stream_Delete( p_mp4_stream );
943 else if( !strcmp( p_tk->psz_codec, "V_MJPEG" ) )
945 p_tk->fmt.i_codec = VLC_CODEC_MJPG;
947 else if( !strcmp( p_tk->psz_codec, "A_MS/ACM" ) )
949 if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
951 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
952 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
956 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
958 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
960 p_tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
961 p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
962 p_tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
963 p_tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
964 p_tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
966 p_tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
967 if( p_tk->fmt.i_extra > 0 )
969 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
970 memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
974 else if( !strcmp( p_tk->psz_codec, "A_MPEG/L3" ) ||
975 !strcmp( p_tk->psz_codec, "A_MPEG/L2" ) ||
976 !strcmp( p_tk->psz_codec, "A_MPEG/L1" ) )
978 p_tk->fmt.i_codec = VLC_CODEC_MPGA;
980 else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
982 p_tk->fmt.i_codec = VLC_CODEC_A52;
984 else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
986 p_tk->fmt.i_codec = VLC_CODEC_EAC3;
988 else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
990 p_tk->fmt.i_codec = VLC_CODEC_DTS;
992 else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
994 p_tk->fmt.i_codec = VLC_CODEC_MLP;
996 else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
998 /* FIXME when more samples arrive */
999 p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
1000 p_fmt->b_packetized = false;
1002 else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
1004 p_tk->fmt.i_codec = VLC_CODEC_FLAC;
1005 fill_extra_data( p_tk );
1007 else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
1009 p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
1010 fill_extra_data( p_tk );
1012 else if( !strncmp( p_tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1013 !strncmp( p_tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1015 int i_profile, i_srate, sbr = 0;
1016 static const unsigned int i_sample_rates[] =
1018 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1019 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1022 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1023 /* create data for faad (MP4DecSpecificDescrTag)*/
1025 if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
1029 else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
1033 else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
1037 else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
1047 for( i_srate = 0; i_srate < 13; i_srate++ )
1049 if( i_sample_rates[i_srate] == p_tk->i_original_rate )
1054 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1056 p_tk->fmt.i_extra = sbr ? 5 : 2;
1057 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1058 ((uint8_t*)p_tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1059 ((uint8_t*)p_tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (p_tk->fmt.audio.i_channels << 3);
1062 int syncExtensionType = 0x2B7;
1064 for (iDSRI=0; iDSRI<13; iDSRI++)
1065 if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
1067 ((uint8_t*)p_tk->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
1068 ((uint8_t*)p_tk->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
1069 ((uint8_t*)p_tk->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
1072 else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
1074 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1075 fill_extra_data( p_tk );
1077 else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
1079 p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
1080 fill_extra_data( p_tk );
1082 else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
1084 p_fmt->i_codec = VLC_CODEC_TTA;
1085 if( p_tk->i_extra_data > 0 )
1087 fill_extra_data( p_tk );
1091 p_fmt->i_extra = 30;
1092 p_fmt->p_extra = xmalloc( p_fmt->i_extra );
1093 uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
1094 memcpy( &p_extra[ 0], "TTA1", 4 );
1095 SetWLE( &p_extra[ 4], 1 );
1096 SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
1097 SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
1098 SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
1099 SetDWLE( &p_extra[14], 0xffffffff );
1100 memset( &p_extra[18], 0, 30 - 18 );
1103 else if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) ||
1104 !strcmp( p_tk->psz_codec, "A_PCM/INT/LIT" ) ||
1105 !strcmp( p_tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1107 if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
1109 p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1113 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1115 p_tk->fmt.audio.i_blockalign = ( p_tk->fmt.audio.i_bitspersample + 7 ) / 8 * p_tk->fmt.audio.i_channels;
1117 else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
1119 if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
1120 p_tk->fmt.i_codec = VLC_CODEC_COOK;
1121 else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
1122 p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
1123 else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
1124 p_tk->fmt.i_codec = VLC_CODEC_RA_288;
1125 /* FIXME 14_4, RALF and SIPR */
1126 fill_extra_data( p_tk );
1128 else if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1130 p_fmt->i_codec = VLC_CODEC_RA_144;
1131 p_fmt->audio.i_channels = 1;
1132 p_fmt->audio.i_rate = 8000;
1133 p_fmt->audio.i_blockalign = 0x14;
1135 /* disabled due to the potential "S_KATE" namespace issue */
1136 else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
1138 int i, i_offset = 1, i_extra, num_headers, size_so_far;
1141 p_tk->fmt.i_codec = VLC_CODEC_KATE;
1142 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1144 fill_extra_data( p_tk );
1146 else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
1148 p_fmt->i_codec = VLC_CODEC_SUBT;
1149 p_fmt->subs.psz_encoding = NULL; /* Is there a place where it is stored ? */
1151 else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
1153 p_tk->fmt.i_codec = VLC_CODEC_SUBT;
1154 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1156 else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
1158 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1159 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1160 if( p_tk->i_extra_data )
1162 fill_extra_data( p_tk );
1165 else if( !strcmp( p_tk->psz_codec, "S_TEXT/SSA" ) ||
1166 !strcmp( p_tk->psz_codec, "S_TEXT/ASS" ) ||
1167 !strcmp( p_tk->psz_codec, "S_SSA" ) ||
1168 !strcmp( p_tk->psz_codec, "S_ASS" ))
1170 p_tk->fmt.i_codec = VLC_CODEC_SSA;
1171 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1172 if( p_tk->i_extra_data )
1174 fill_extra_data( p_tk );
1177 else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
1179 p_tk->fmt.i_codec = VLC_CODEC_SPU;
1180 if( p_tk->i_extra_data )
1183 char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
1184 if( psz_buf != NULL )
1186 memcpy( psz_buf, p_tk->p_extra_data , p_tk->i_extra_data );
1187 psz_buf[p_tk->i_extra_data] = '\0';
1189 psz_start = strstr( psz_buf, "size:" );
1191 vobsub_size_parse( psz_start,
1192 &p_tk->fmt.subs.spu.i_original_frame_width,
1193 &p_tk->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1195 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1196 p_tk->fmt.subs.spu.i_original_frame_width,
1197 p_tk->fmt.subs.spu.i_original_frame_height );
1201 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1204 psz_start = strstr( psz_buf, "palette:" );
1206 vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1208 p_tk->fmt.subs.spu.palette[0] = 0xBeef;
1209 msg_Dbg( &sys.demuxer, "vobsub palette read" );
1213 msg_Warn( &sys.demuxer, "reading original palette failed" );
1219 else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
1221 p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
1223 else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
1225 p_tk->fmt.i_cat = NAV_ES;
1230 msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
1231 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1233 if( p_tk->b_default )
1235 p_tk->fmt.i_priority = 1000;
1238 p_tk->p_es = es_out_Add( sys.demuxer.out, &p_tk->fmt );
1240 /* Turn on a subtitles track if it has been flagged as default -
1241 * but only do this if no subtitles track has already been engaged,
1242 * either by an earlier 'default track' (??) or by default
1243 * language choice behaviour.
1245 if( p_tk->b_default )
1247 es_out_Control( sys.demuxer.out,
1248 ES_OUT_SET_ES_DEFAULT,
1252 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
1254 sys.i_start_pts = i_start_time;
1255 // reset the stream reading to the first cluster of the segment used
1256 es.I_O().setFilePointer( i_start_pos );
1259 ep = new EbmlParser( &es, segment, &sys.demuxer );
1264 void matroska_segment_c::UnSelect( )
1266 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1268 if ( tracks[i_track]->p_es != NULL )
1270 // es_format_Clean( &tracks[i_track]->fmt );
1271 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
1272 tracks[i_track]->p_es = NULL;
1279 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, bool *pb_key_picture, bool *pb_discardable_picture, int64_t *pi_duration )
1281 pp_simpleblock = NULL;
1284 *pb_key_picture = true;
1285 *pb_discardable_picture = false;
1289 EbmlElement *el = NULL;
1293 return VLC_EGENERIC;
1295 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1297 /* Check blocks validity to protect againts broken files */
1298 if( BlockFindTrackIndex( NULL, pp_block , pp_simpleblock ) )
1301 pp_simpleblock = NULL;
1305 if( pp_simpleblock != NULL )
1307 *pb_key_picture = pp_simpleblock->IsKeyframe();
1308 *pb_discardable_picture = pp_simpleblock->IsDiscardable();
1311 /* update the index */
1312 #define idx p_indexes[i_index - 1]
1313 if( i_index > 0 && idx.i_time == -1 )
1315 if ( pp_simpleblock != NULL )
1316 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1318 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1319 idx.b_key = *pb_key_picture;
1325 i_level = ep->GetLevel();
1334 msg_Warn( &sys.demuxer, "EOF" );
1335 return VLC_EGENERIC;
1338 /* Verify that we are still inside our cluster
1339 * It can happens whith broken files and when seeking
1343 if( cluster && !ep->IsTopPresent( cluster ) )
1345 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1356 if( MKV_IS_ID( el, KaxCluster ) )
1358 cluster = (KaxCluster*)el;
1359 i_cluster_pos = cluster->GetElementPosition();
1361 /* add it to the index */
1363 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1365 IndexAppendCluster( cluster );
1368 // reset silent tracks
1369 for (size_t i=0; i<tracks.size(); i++)
1371 tracks[i]->b_silent = false;
1376 else if( MKV_IS_ID( el, KaxCues ) )
1378 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1379 return VLC_EGENERIC;
1383 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1387 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1389 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1391 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1392 cluster->InitTimecode( uint64( ctc ), i_timescale );
1394 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1398 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1400 i_block_pos = el->GetElementPosition();
1403 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1405 pp_simpleblock = (KaxSimpleBlock*)el;
1407 pp_simpleblock->ReadData( es.I_O() );
1408 pp_simpleblock->SetParent( *cluster );
1412 if( MKV_IS_ID( el, KaxBlock ) )
1414 pp_block = (KaxBlock*)el;
1416 pp_block->ReadData( es.I_O() );
1417 pp_block->SetParent( *cluster );
1421 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1423 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1425 dur.ReadData( es.I_O() );
1426 *pi_duration = uint64( dur );
1428 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1430 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1432 ref.ReadData( es.I_O() );
1434 if( *pb_key_picture )
1435 *pb_key_picture = false;
1436 else if( int64( ref ) > 0 )
1437 *pb_discardable_picture = true;
1439 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1441 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1442 track_num.ReadData( es.I_O() );
1444 for (size_t i=0; i<tracks.size(); i++)
1446 if ( tracks[i]->i_number == uint32(track_num))
1448 tracks[i]->b_silent = true;
1455 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1456 return VLC_EGENERIC;