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] );
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)
78 p_indexes = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
81 matroska_segment_c::~matroska_segment_c()
83 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
85 delete tracks[i_track]->p_compression_data;
86 es_format_Clean( &tracks[i_track]->fmt );
87 free( tracks[i_track]->p_extra_data );
88 free( tracks[i_track]->psz_codec );
89 delete tracks[i_track];
92 free( psz_writing_application );
93 free( psz_muxing_application );
94 free( psz_segment_filename );
101 delete p_segment_uid;
102 delete p_prev_segment_uid;
103 delete p_next_segment_uid;
105 vlc_delete_all( stored_editions );
106 vlc_delete_all( translations );
107 vlc_delete_all( families );
111 /*****************************************************************************
113 *****************************************************************************
114 * * LoadCues : load the cues element and update index
115 * * LoadTags : load ... the tags element
116 * * InformationCreate : create all information, load tags if present
117 *****************************************************************************/
118 void matroska_segment_c::LoadCues( KaxCues *cues )
125 msg_Err( &sys.demuxer, "There can be only 1 Cues per section." );
129 ep = new EbmlParser( &es, cues, &sys.demuxer );
130 while( ( el = ep->Get() ) != NULL )
132 if( MKV_IS_ID( el, KaxCuePoint ) )
134 #define idx p_indexes[i_index]
137 idx.i_block_number= -1;
143 while( ( el = ep->Get() ) != NULL )
145 if( MKV_IS_ID( el, KaxCueTime ) )
147 KaxCueTime &ctime = *(KaxCueTime*)el;
149 ctime.ReadData( es.I_O() );
151 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
153 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
156 while( ( el = ep->Get() ) != NULL )
158 if( MKV_IS_ID( el, KaxCueTrack ) )
160 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
162 ctrack.ReadData( es.I_O() );
163 idx.i_track = uint16( ctrack );
165 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
167 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
169 ccpos.ReadData( es.I_O() );
170 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
172 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
174 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
176 cbnum.ReadData( es.I_O() );
177 idx.i_block_number = uint32( cbnum );
181 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
188 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
194 msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
195 " track=%d bnum=%d", idx.i_time, idx.i_position,
196 idx.i_track, idx.i_block_number );
200 if( i_index >= i_index_max )
203 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
204 sizeof( mkv_index_t ) * i_index_max );
210 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
215 msg_Dbg( &sys.demuxer, "| - loading cues done." );
219 #define PARSE_TAG( type ) \
221 msg_Dbg( &sys.demuxer, "| + " type ); \
223 while( ( el = ep->Get() ) != NULL ) \
225 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() ); \
227 ep->Up(); } while( 0 )
229 static const struct {
230 vlc_meta_type_t type;
232 } metadata_map[] = { {vlc_meta_Title, "TITLE"},
233 {vlc_meta_Artist, "ARTIST"},
234 {vlc_meta_Genre, "GENRE"},
235 {vlc_meta_Copyright, "COPYRIGHT"},
236 {vlc_meta_Description, "DESCRIPTION"},
237 {vlc_meta_Publisher, "PUBLISHER"},
238 {vlc_meta_URL, "URL"},
239 {vlc_meta_Title, NULL},
242 void matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag )
245 EbmlParser *ep = new EbmlParser( &es, tag, &sys.demuxer );
246 char *k = NULL, *v = NULL;
249 sys.meta = vlc_meta_New();
251 msg_Dbg( &sys.demuxer, "| + Simple Tag ");
252 while( ( el = ep->Get() ) != NULL )
254 if( MKV_IS_ID( el, KaxTagName ) )
256 KaxTagName &key = *(KaxTagName*)el;
257 key.ReadData( es.I_O(), SCOPE_ALL_DATA );
258 k = strdup( UTFstring( key ).GetUTF8().c_str() );
260 if( MKV_IS_ID( el, KaxTagString ) )
262 KaxTagString &value = *(KaxTagString*)el;
263 value.ReadData( es.I_O(), SCOPE_ALL_DATA );
264 v = strdup( UTFstring( value ).GetUTF8().c_str() );
271 msg_Warn( &sys.demuxer, "Invalid MKV SimpleTag found.");
275 for( int i = 0; metadata_map[i].key; i++ )
277 if( !strcmp( k, metadata_map[i].key ) )
279 vlc_meta_Set( sys.meta, metadata_map[i].type, v );
283 vlc_meta_AddExtra( sys.meta, k, v );
290 void matroska_segment_c::LoadTags( KaxTags *tags )
292 /* Master elements */
293 EbmlParser *ep = new EbmlParser( &es, tags, &sys.demuxer );
296 while( ( el = ep->Get() ) != NULL )
298 if( MKV_IS_ID( el, KaxTag ) )
300 msg_Dbg( &sys.demuxer, "+ Tag" );
302 while( ( el = ep->Get() ) != NULL )
304 if( MKV_IS_ID( el, KaxTagTargets ) )
305 PARSE_TAG( "Targets" );
306 #if 0 // not valid anymore
307 else if( MKV_IS_ID( el, KaxTagGeneral ) )
308 PARSE_TAG( "General" );
309 else if( MKV_IS_ID( el, KaxTagGenres ) )
310 PARSE_TAG( "Genres" );
311 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
312 PARSE_TAG( "Audio Specific" );
313 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
314 PARSE_TAG( "Images Specific" );
315 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
317 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
319 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
321 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
323 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
325 msg_Dbg( &sys.demuxer, "| + Multi Date" );
327 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
329 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
331 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
333 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
335 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
337 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
339 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
341 msg_Dbg( &sys.demuxer, "| + Multi Title" );
344 else if( MKV_IS_ID( el, KaxTagSimple ) )
345 ParseSimpleTags( static_cast<KaxTagSimple*>( el ) );
348 msg_Dbg( &sys.demuxer, "| + LoadTag Unknown (%s)", typeid( *el ).name() );
355 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
360 msg_Dbg( &sys.demuxer, "loading tags done." );
364 /*****************************************************************************
366 *****************************************************************************/
367 void matroska_segment_c::InformationCreate( )
370 sys.meta = vlc_meta_New();
374 vlc_meta_SetTitle( sys.meta, psz_title );
378 vlc_meta_SetDate( sys.meta, psz_date_utc );
381 if( psz_segment_filename )
383 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
385 if( psz_muxing_application )
387 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
389 if( psz_writing_application )
391 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
394 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
396 // mkv_track_t *tk = tracks[i_track];
397 // vlc_meta_t *mtk = vlc_meta_New();
398 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
402 if( i_tags_position >= 0 )
406 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
416 /*****************************************************************************
418 *****************************************************************************/
420 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
422 #define idx p_indexes[i_index]
424 idx.i_block_number= -1;
425 idx.i_position = cluster->GetElementPosition();
430 if( i_index >= i_index_max )
433 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
434 sizeof( mkv_index_t ) * i_index_max );
439 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
444 for (size_t i=0; i<families.size(); i++)
446 for (size_t j=0; j<of_segment.families.size(); j++)
448 if ( *(families[i]) == *(of_segment.families[j]) )
456 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
460 if ( p_item_a == NULL || p_item_b == NULL )
463 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
464 if ( p_item_b->p_prev_segment_uid != NULL
465 && *p_tmp == *p_item_b->p_prev_segment_uid )
468 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
472 if ( p_item_b->p_segment_uid != NULL
473 && *p_tmp == *p_item_b->p_segment_uid )
476 if ( p_item_b->p_prev_segment_uid != NULL
477 && *p_tmp == *p_item_b->p_prev_segment_uid )
483 bool matroska_segment_c::Preload( )
488 EbmlElement *el = NULL;
490 ep->Reset( &sys.demuxer );
492 while( ( el = ep->Get() ) != NULL )
494 if( MKV_IS_ID( el, KaxSeekHead ) )
496 /* Multiple allowed */
497 /* We bail at 10, to prevent possible recursion */
498 msg_Dbg( &sys.demuxer, "| + Seek head" );
499 if( i_seekhead_count < 10 )
501 i_seekhead_position = (int64_t) es.I_O().getFilePointer();
502 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
505 else if( MKV_IS_ID( el, KaxInfo ) )
507 /* Multiple allowed, mandatory */
508 msg_Dbg( &sys.demuxer, "| + Information" );
509 if( i_info_position < 0 ) // FIXME
510 ParseInfo( static_cast<KaxInfo*>( el ) );
511 i_info_position = (int64_t) es.I_O().getFilePointer();
513 else if( MKV_IS_ID( el, KaxTracks ) )
515 /* Multiple allowed */
516 msg_Dbg( &sys.demuxer, "| + Tracks" );
517 if( i_tracks_position < 0 ) // FIXME
518 ParseTracks( static_cast<KaxTracks*>( el ) );
519 if ( tracks.size() == 0 )
521 msg_Err( &sys.demuxer, "No tracks supported" );
524 i_tracks_position = (int64_t) es.I_O().getFilePointer();
526 else if( MKV_IS_ID( el, KaxCues ) )
528 msg_Dbg( &sys.demuxer, "| + Cues" );
529 if( i_cues_position < 0 )
530 LoadCues( static_cast<KaxCues*>( el ) );
531 i_cues_position = (int64_t) es.I_O().getFilePointer();
533 else if( MKV_IS_ID( el, KaxCluster ) )
535 msg_Dbg( &sys.demuxer, "| + Cluster" );
537 cluster = (KaxCluster*)el;
539 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
543 /* stop pre-parsing the stream */
546 else if( MKV_IS_ID( el, KaxAttachments ) )
548 msg_Dbg( &sys.demuxer, "| + Attachments" );
549 if( i_attachments_position < 0 )
550 ParseAttachments( static_cast<KaxAttachments*>( el ) );
551 i_attachments_position = (int64_t) es.I_O().getFilePointer();
553 else if( MKV_IS_ID( el, KaxChapters ) )
555 msg_Dbg( &sys.demuxer, "| + Chapters" );
556 if( i_chapters_position < 0 )
557 ParseChapters( static_cast<KaxChapters*>( el ) );
558 i_chapters_position = (int64_t) es.I_O().getFilePointer();
560 else if( MKV_IS_ID( el, KaxTag ) )
562 msg_Dbg( &sys.demuxer, "| + Tags" );
563 /*FIXME if( i_tags_position < 0)
564 LoadTags( static_cast<KaxTags*>( el ) );*/
565 i_tags_position = (int64_t) es.I_O().getFilePointer();
567 else if( MKV_IS_ID( el, EbmlVoid ) )
568 msg_Dbg( &sys.demuxer, "| + Void" );
570 msg_Dbg( &sys.demuxer, "| + Preload Unknown (%s)", typeid(*el).name() );
578 /* Here we try to load elements that were found in Seek Heads, but not yet parsed */
579 bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position )
581 int64_t i_sav_position = (int64_t)es.I_O().getFilePointer();
584 es.I_O().setFilePointer( i_element_position, seek_beginning );
585 el = es.FindNextID( ClassInfos, 0xFFFFFFFFL);
589 msg_Err( &sys.demuxer, "cannot load some cues/chapters/tags etc. (broken seekhead or file)" );
590 es.I_O().setFilePointer( i_sav_position, seek_beginning );
594 if( MKV_IS_ID( el, KaxSeekHead ) )
596 /* Multiple allowed */
597 msg_Dbg( &sys.demuxer, "| + Seek head" );
598 if( i_seekhead_count < 10 )
600 i_seekhead_position = i_element_position;
601 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
604 else if( MKV_IS_ID( el, KaxInfo ) ) // FIXME
606 /* Multiple allowed, mandatory */
607 msg_Dbg( &sys.demuxer, "| + Information" );
608 if( i_info_position < 0 )
609 ParseInfo( static_cast<KaxInfo*>( el ) );
610 i_info_position = i_element_position;
612 else if( MKV_IS_ID( el, KaxTracks ) ) // FIXME
614 /* Multiple allowed */
615 msg_Dbg( &sys.demuxer, "| + Tracks" );
616 if( i_tracks_position < 0 )
617 ParseTracks( static_cast<KaxTracks*>( el ) );
618 if ( tracks.size() == 0 )
620 msg_Err( &sys.demuxer, "No tracks supported" );
622 es.I_O().setFilePointer( i_sav_position, seek_beginning );
625 i_tracks_position = i_element_position;
627 else if( MKV_IS_ID( el, KaxCues ) )
629 msg_Dbg( &sys.demuxer, "| + Cues" );
630 if( i_cues_position < 0 )
631 LoadCues( static_cast<KaxCues*>( el ) );
632 i_cues_position = i_element_position;
634 else if( MKV_IS_ID( el, KaxAttachments ) )
636 msg_Dbg( &sys.demuxer, "| + Attachments" );
637 if( i_attachments_position < 0 )
638 ParseAttachments( static_cast<KaxAttachments*>( el ) );
639 i_attachments_position = i_element_position;
641 else if( MKV_IS_ID( el, KaxChapters ) )
643 msg_Dbg( &sys.demuxer, "| + Chapters" );
644 if( i_chapters_position < 0 )
645 ParseChapters( static_cast<KaxChapters*>( el ) );
646 i_chapters_position = i_element_position;
648 else if( MKV_IS_ID( el, KaxTags ) )
650 msg_Dbg( &sys.demuxer, "| + Tags" );
651 if( i_tags_position < 0 )
652 LoadTags( static_cast<KaxTags*>( el ) );
653 i_tags_position = i_element_position;
657 msg_Dbg( &sys.demuxer, "| + LoadSeekHeadItem Unknown (%s)", typeid(*el).name() );
661 es.I_O().setFilePointer( i_sav_position, seek_beginning );
665 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_global_position )
668 KaxSimpleBlock *simpleblock;
669 int i_track_skipping;
670 int64_t i_block_duration;
672 int64_t i_seek_position = i_start_pos;
673 int64_t i_seek_time = i_start_time;
675 if( i_global_position >= 0 )
677 /* Special case for seeking in files with no cues */
678 EbmlElement *el = NULL;
679 es.I_O().setFilePointer( i_start_pos, seek_beginning );
681 ep = new EbmlParser( &es, segment, &sys.demuxer );
684 while( ( el = ep->Get() ) != NULL )
686 if( MKV_IS_ID( el, KaxCluster ) )
688 cluster = (KaxCluster *)el;
689 i_cluster_pos = cluster->GetElementPosition();
691 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
693 IndexAppendCluster( cluster );
695 if( es.I_O().getFilePointer() >= i_global_position )
698 msg_Dbg( &sys.demuxer, "we found a cluster that is in the neighbourhood" );
703 msg_Err( &sys.demuxer, "This file has no cues, and we were unable to seek to the requested position by parsing." );
711 for( ; i_idx < i_index; i_idx++ )
713 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
724 i_seek_position = p_indexes[i_idx].i_position;
725 i_seek_time = p_indexes[i_idx].i_time;
728 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
729 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
731 es.I_O().setFilePointer( i_seek_position, seek_beginning );
734 ep = new EbmlParser( &es, segment, &sys.demuxer );
737 sys.i_start_pts = i_date;
739 /* now parse until key frame */
740 i_track_skipping = 0;
741 for( i_track = 0; i_track < tracks.size(); i_track++ )
743 if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
745 tracks[i_track]->b_search_keyframe = true;
749 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
751 while( i_track_skipping > 0 )
754 bool b_discardable_picture;
755 if( BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
757 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
762 for( i_track = 0; i_track < tracks.size(); i_track++ )
764 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
765 (block && tracks[i_track]->i_number == block->TrackNum()) )
772 sys.i_pts = (sys.i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
774 sys.i_pts = (sys.i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
776 if( i_track < tracks.size() )
778 if( sys.i_pts > sys.i_start_pts )
780 cluster = static_cast<KaxCluster*>(ep->UnGet( i_block_pos, i_cluster_pos ));
781 i_track_skipping = 0;
783 else if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
785 if( b_key_picture && tracks[i_track]->b_search_keyframe )
787 tracks[i_track]->b_search_keyframe = false;
790 if( !tracks[i_track]->b_search_keyframe )
792 BlockDecode( &sys.demuxer, block, simpleblock, sys.i_pts, 0, b_key_picture || b_discardable_picture );
800 /* FIXME current ES_OUT_SET_NEXT_DISPLAY_TIME does not work that well if
801 * the delay is too high. */
802 if( sys.i_pts + 500*1000 < sys.i_start_pts )
804 sys.i_start_pts = sys.i_pts;
806 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, sys.i_start_pts );
810 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
811 const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
814 for( i_track = 0; i_track < tracks.size(); i_track++ )
816 const mkv_track_t *tk = tracks[i_track];
818 if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
819 ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
825 if( i_track >= tracks.size() )
833 static inline void fill_extra_data( mkv_track_t *p_tk, unsigned int offset )
835 if(p_tk->i_extra_data <= offset) return;
836 p_tk->fmt.i_extra = p_tk->i_extra_data - offset;
837 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
838 if(!p_tk->fmt.p_extra) { p_tk->fmt.i_extra = 0; return; };
839 memcpy( p_tk->fmt.p_extra, p_tk->p_extra_data + offset, p_tk->fmt.i_extra );
842 bool matroska_segment_c::Select( mtime_t i_start_time )
845 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
847 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
849 mkv_track_t *p_tk = tracks[i_track];
850 es_format_t *p_fmt = &p_tk->fmt;
852 if( p_fmt->i_cat == UNKNOWN_ES || !p_tk->psz_codec )
854 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, p_tk->i_number );
859 if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
861 if( p_tk->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
863 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
864 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
868 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)p_tk->p_extra_data;
870 p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
871 p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
872 p_tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
874 p_tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
875 if( p_tk->fmt.i_extra > 0 )
877 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
878 memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
881 p_tk->b_dts_only = true;
883 else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
884 !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
886 p_tk->fmt.i_codec = VLC_CODEC_MPGV;
887 if( p_tk->i_extra_data )
888 fill_extra_data( p_tk, 0 );
890 else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
892 p_tk->fmt.i_codec = VLC_CODEC_THEORA;
893 fill_extra_data( p_tk, 0 );
894 p_tk->b_pts_only = true;
896 else if( !strncmp( p_tk->psz_codec, "V_REAL/RV", 9 ) )
898 uint8_t *p = p_tk->p_extra_data;
900 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
901 p_fmt->i_codec = VLC_CODEC_RV10;
902 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
903 p_fmt->i_codec = VLC_CODEC_RV20;
904 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
905 p_fmt->i_codec = VLC_CODEC_RV30;
906 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
907 p_fmt->i_codec = VLC_CODEC_RV40;
909 /* Extract the framerate from the header */
910 if( p_tk->i_extra_data >= 26 &&
911 p[4] == 'V' && p[5] == 'I' && p[6] == 'D' && p[7] == 'O' &&
912 p[8] == 'R' && p[9] == 'V' &&
913 (p[10] == '3' || p[10] == '4') && p[11] == '0' )
915 p_tk->fmt.video.i_frame_rate =
916 p[22] << 24 | p[23] << 16 | p[24] << 8 | p[25] << 0;
917 p_tk->fmt.video.i_frame_rate_base = 65536;
920 fill_extra_data( p_tk, 26 );
921 p_tk->b_dts_only = true;
923 else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
925 p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
927 else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
929 p_tk->fmt.i_codec = VLC_CODEC_VP8;
930 p_tk->b_pts_only = true;
932 else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
934 if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
936 p_tk->fmt.i_codec = VLC_CODEC_DIV3;
938 else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
940 /* A MPEG 4 codec, SP, ASP, AP or AVC */
941 if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
942 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
944 p_tk->fmt.i_codec = VLC_CODEC_MP4V;
945 fill_extra_data( p_tk, 0 );
948 else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
950 MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
951 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
955 if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
956 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
958 p_tk->fmt.i_codec = p_box->i_type;
959 p_tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
960 p_tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
961 p_tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
962 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
963 memcpy( p_tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, p_tk->fmt.i_extra );
964 MP4_FreeBox_sample_vide( p_box );
970 stream_Delete( p_mp4_stream );
972 else if( !strcmp( p_tk->psz_codec, "V_MJPEG" ) )
974 p_tk->fmt.i_codec = VLC_CODEC_MJPG;
976 else if( !strcmp( p_tk->psz_codec, "A_MS/ACM" ) )
978 if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
980 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
981 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
985 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
987 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
989 p_tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
990 p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
991 p_tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
992 p_tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
993 p_tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
995 p_tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
996 if( p_tk->fmt.i_extra > 0 )
998 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
999 memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
1003 else if( !strcmp( p_tk->psz_codec, "A_MPEG/L3" ) ||
1004 !strcmp( p_tk->psz_codec, "A_MPEG/L2" ) ||
1005 !strcmp( p_tk->psz_codec, "A_MPEG/L1" ) )
1007 p_tk->fmt.i_codec = VLC_CODEC_MPGA;
1009 else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
1011 p_tk->fmt.i_codec = VLC_CODEC_A52;
1013 else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
1015 p_tk->fmt.i_codec = VLC_CODEC_EAC3;
1017 else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
1019 p_tk->fmt.i_codec = VLC_CODEC_DTS;
1021 else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
1023 p_tk->fmt.i_codec = VLC_CODEC_MLP;
1025 else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
1027 /* FIXME when more samples arrive */
1028 p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
1029 p_fmt->b_packetized = false;
1031 else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
1033 p_tk->fmt.i_codec = VLC_CODEC_FLAC;
1034 fill_extra_data( p_tk, 0 );
1036 else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
1038 p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
1039 fill_extra_data( p_tk, 0 );
1041 else if( !strncmp( p_tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1042 !strncmp( p_tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1044 int i_profile, i_srate, sbr = 0;
1045 static const unsigned int i_sample_rates[] =
1047 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1048 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1051 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1052 /* create data for faad (MP4DecSpecificDescrTag)*/
1054 if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
1058 else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
1062 else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
1066 else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
1076 for( i_srate = 0; i_srate < 13; i_srate++ )
1078 if( i_sample_rates[i_srate] == p_tk->i_original_rate )
1083 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1085 p_tk->fmt.i_extra = sbr ? 5 : 2;
1086 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1087 ((uint8_t*)p_tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1088 ((uint8_t*)p_tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (p_tk->fmt.audio.i_channels << 3);
1091 int syncExtensionType = 0x2B7;
1093 for (iDSRI=0; iDSRI<13; iDSRI++)
1094 if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
1096 ((uint8_t*)p_tk->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
1097 ((uint8_t*)p_tk->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
1098 ((uint8_t*)p_tk->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
1101 else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
1103 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1104 fill_extra_data( p_tk, 0 );
1106 else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
1108 p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
1109 fill_extra_data( p_tk, 0 );
1111 else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
1113 p_fmt->i_codec = VLC_CODEC_TTA;
1114 if( p_tk->i_extra_data > 0 )
1116 fill_extra_data( p_tk, 0 );
1120 p_fmt->i_extra = 30;
1121 p_fmt->p_extra = xmalloc( p_fmt->i_extra );
1122 uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
1123 memcpy( &p_extra[ 0], "TTA1", 4 );
1124 SetWLE( &p_extra[ 4], 1 );
1125 SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
1126 SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
1127 SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
1128 SetDWLE( &p_extra[14], 0xffffffff );
1129 memset( &p_extra[18], 0, 30 - 18 );
1132 else if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) ||
1133 !strcmp( p_tk->psz_codec, "A_PCM/INT/LIT" ) ||
1134 !strcmp( p_tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1136 if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
1138 p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1142 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1144 p_tk->fmt.audio.i_blockalign = ( p_tk->fmt.audio.i_bitspersample + 7 ) / 8 * p_tk->fmt.audio.i_channels;
1146 else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
1148 if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
1149 p_tk->fmt.i_codec = VLC_CODEC_COOK;
1150 else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
1151 p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
1152 else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
1153 p_tk->fmt.i_codec = VLC_CODEC_RA_288;
1154 /* FIXME 14_4, RALF and SIPR */
1155 fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 0 /*78 - FIXME need to implement reading support for cook */ );
1157 else if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1159 p_fmt->i_codec = VLC_CODEC_RA_144;
1160 p_fmt->audio.i_channels = 1;
1161 p_fmt->audio.i_rate = 8000;
1162 p_fmt->audio.i_blockalign = 0x14;
1164 else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
1166 p_tk->fmt.i_codec = VLC_CODEC_KATE;
1167 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1169 fill_extra_data( p_tk, 0 );
1171 else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
1173 p_fmt->i_codec = VLC_CODEC_SUBT;
1174 p_fmt->subs.psz_encoding = strdup( "ASCII" );
1176 else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
1178 p_tk->fmt.i_codec = VLC_CODEC_SUBT;
1179 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1181 else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
1183 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1184 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1185 if( p_tk->i_extra_data )
1186 fill_extra_data( p_tk, 0 );
1188 else if( !strcmp( p_tk->psz_codec, "S_TEXT/SSA" ) ||
1189 !strcmp( p_tk->psz_codec, "S_TEXT/ASS" ) ||
1190 !strcmp( p_tk->psz_codec, "S_SSA" ) ||
1191 !strcmp( p_tk->psz_codec, "S_ASS" ))
1193 p_tk->fmt.i_codec = VLC_CODEC_SSA;
1194 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1195 if( p_tk->i_extra_data )
1196 fill_extra_data( p_tk, 0 );
1198 else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
1200 p_tk->fmt.i_codec = VLC_CODEC_SPU;
1201 if( p_tk->i_extra_data )
1204 char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
1205 if( psz_buf != NULL )
1207 memcpy( psz_buf, p_tk->p_extra_data , p_tk->i_extra_data );
1208 psz_buf[p_tk->i_extra_data] = '\0';
1210 psz_start = strstr( psz_buf, "size:" );
1212 vobsub_size_parse( psz_start,
1213 &p_tk->fmt.subs.spu.i_original_frame_width,
1214 &p_tk->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1216 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1217 p_tk->fmt.subs.spu.i_original_frame_width,
1218 p_tk->fmt.subs.spu.i_original_frame_height );
1222 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1225 psz_start = strstr( psz_buf, "palette:" );
1227 vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1229 p_tk->fmt.subs.spu.palette[0] = 0xBeef;
1230 msg_Dbg( &sys.demuxer, "vobsub palette read" );
1234 msg_Warn( &sys.demuxer, "reading original palette failed" );
1240 else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
1242 p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
1244 else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
1246 p_tk->fmt.i_cat = NAV_ES;
1251 msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
1252 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1254 if( p_tk->b_default )
1256 p_tk->fmt.i_priority = 1000;
1259 p_tk->p_es = es_out_Add( sys.demuxer.out, &p_tk->fmt );
1261 /* Turn on a subtitles track if it has been flagged as default -
1262 * but only do this if no subtitles track has already been engaged,
1263 * either by an earlier 'default track' (??) or by default
1264 * language choice behaviour.
1266 if( p_tk->b_default )
1268 es_out_Control( sys.demuxer.out,
1269 ES_OUT_SET_ES_DEFAULT,
1273 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
1275 sys.i_start_pts = i_start_time;
1276 // reset the stream reading to the first cluster of the segment used
1277 es.I_O().setFilePointer( i_start_pos );
1280 ep = new EbmlParser( &es, segment, &sys.demuxer );
1285 void matroska_segment_c::UnSelect( )
1287 sys.p_ev->ResetPci();
1288 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1290 if ( tracks[i_track]->p_es != NULL )
1292 // es_format_Clean( &tracks[i_track]->fmt );
1293 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
1294 tracks[i_track]->p_es = NULL;
1301 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, bool *pb_key_picture, bool *pb_discardable_picture, int64_t *pi_duration )
1303 pp_simpleblock = NULL;
1306 *pb_key_picture = true;
1307 *pb_discardable_picture = false;
1311 EbmlElement *el = NULL;
1315 return VLC_EGENERIC;
1317 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1319 /* Check blocks validity to protect againts broken files */
1320 if( BlockFindTrackIndex( NULL, pp_block , pp_simpleblock ) )
1323 pp_simpleblock = NULL;
1327 if( pp_simpleblock != NULL )
1329 *pb_key_picture = pp_simpleblock->IsKeyframe();
1330 *pb_discardable_picture = pp_simpleblock->IsDiscardable();
1333 /* update the index */
1334 #define idx p_indexes[i_index - 1]
1335 if( i_index > 0 && idx.i_time == -1 )
1337 if ( pp_simpleblock != NULL )
1338 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1340 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1341 idx.b_key = *pb_key_picture;
1347 i_level = ep->GetLevel();
1356 msg_Warn( &sys.demuxer, "EOF" );
1357 return VLC_EGENERIC;
1360 /* Verify that we are still inside our cluster
1361 * It can happens whith broken files and when seeking
1365 if( cluster && !ep->IsTopPresent( cluster ) )
1367 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1378 if( MKV_IS_ID( el, KaxCluster ) )
1380 cluster = (KaxCluster*)el;
1381 i_cluster_pos = cluster->GetElementPosition();
1383 /* add it to the index */
1385 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1387 IndexAppendCluster( cluster );
1390 // reset silent tracks
1391 for (size_t i=0; i<tracks.size(); i++)
1393 tracks[i]->b_silent = false;
1398 else if( MKV_IS_ID( el, KaxCues ) )
1400 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1401 return VLC_EGENERIC;
1405 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1409 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1411 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1413 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1414 cluster->InitTimecode( uint64( ctc ), i_timescale );
1416 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1420 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1422 i_block_pos = el->GetElementPosition();
1425 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1427 pp_simpleblock = (KaxSimpleBlock*)el;
1429 pp_simpleblock->ReadData( es.I_O() );
1430 pp_simpleblock->SetParent( *cluster );
1434 if( MKV_IS_ID( el, KaxBlock ) )
1436 pp_block = (KaxBlock*)el;
1438 pp_block->ReadData( es.I_O() );
1439 pp_block->SetParent( *cluster );
1443 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1445 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1447 dur.ReadData( es.I_O() );
1448 *pi_duration = uint64( dur );
1450 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1452 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1454 ref.ReadData( es.I_O() );
1456 if( *pb_key_picture )
1457 *pb_key_picture = false;
1458 else if( int64( ref ) > 0 )
1459 *pb_discardable_picture = true;
1461 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1463 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1464 track_num.ReadData( es.I_O() );
1466 for (size_t i=0; i<tracks.size(); i++)
1468 if ( tracks[i]->i_number == uint32(track_num))
1470 tracks[i]->b_silent = true;
1477 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1478 return VLC_EGENERIC;