1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2004 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 /* GetFourCC helper */
36 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
37 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
39 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
43 matroska_segment_c::~matroska_segment_c()
45 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
47 delete tracks[i_track]->p_compression_data;
48 es_format_Clean( &tracks[i_track]->fmt );
49 free( tracks[i_track]->p_extra_data );
50 free( tracks[i_track]->psz_codec );
51 delete tracks[i_track];
54 free( psz_writing_application );
55 free( psz_muxing_application );
56 free( psz_segment_filename );
64 delete p_prev_segment_uid;
65 delete p_next_segment_uid;
67 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
68 while ( index != stored_editions.end() )
73 std::vector<chapter_translation_c*>::iterator indext = translations.begin();
74 while ( indext != translations.end() )
79 std::vector<KaxSegmentFamily*>::iterator indexf = families.begin();
80 while ( indexf != families.end() )
88 /*****************************************************************************
90 * * LoadCues : load the cues element and update index
92 * * LoadTags : load ... the tags element
94 * * 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." );
197 void matroska_segment_c::LoadTags( KaxTags *tags )
202 /* Master elements */
203 ep = new EbmlParser( &es, tags, &sys.demuxer );
205 while( ( el = ep->Get() ) != NULL )
207 if( MKV_IS_ID( el, KaxTag ) )
209 msg_Dbg( &sys.demuxer, "+ Tag" );
211 while( ( el = ep->Get() ) != NULL )
213 if( MKV_IS_ID( el, KaxTagTargets ) )
215 msg_Dbg( &sys.demuxer, "| + Targets" );
217 while( ( el = ep->Get() ) != NULL )
219 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
223 else if( MKV_IS_ID( el, KaxTagGeneral ) )
225 msg_Dbg( &sys.demuxer, "| + General" );
227 while( ( el = ep->Get() ) != NULL )
229 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
233 else if( MKV_IS_ID( el, KaxTagGenres ) )
235 msg_Dbg( &sys.demuxer, "| + Genres" );
237 while( ( el = ep->Get() ) != NULL )
239 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
243 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
245 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
247 while( ( el = ep->Get() ) != NULL )
249 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
253 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
255 msg_Dbg( &sys.demuxer, "| + Images Specific" );
257 while( ( el = ep->Get() ) != NULL )
259 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
263 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
265 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
267 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
269 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
271 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
273 msg_Dbg( &sys.demuxer, "| + Multi Date" );
275 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
277 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
279 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
281 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
283 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
285 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
287 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
289 msg_Dbg( &sys.demuxer, "| + Multi Title" );
293 msg_Dbg( &sys.demuxer, "| + LoadTag Unknown (%s)", typeid( *el ).name() );
300 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
305 msg_Dbg( &sys.demuxer, "loading tags done." );
308 /*****************************************************************************
310 *****************************************************************************/
311 void matroska_segment_c::InformationCreate( )
313 sys.meta = vlc_meta_New();
317 vlc_meta_SetTitle( sys.meta, psz_title );
321 vlc_meta_SetDate( sys.meta, psz_date_utc );
324 if( psz_segment_filename )
326 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
328 if( psz_muxing_application )
330 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
332 if( psz_writing_application )
334 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
337 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
339 // mkv_track_t *tk = tracks[i_track];
340 // vlc_meta_t *mtk = vlc_meta_New();
341 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
345 if( i_tags_position >= 0 )
349 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
359 /*****************************************************************************
361 *****************************************************************************/
363 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
365 #define idx p_indexes[i_index]
367 idx.i_block_number= -1;
368 idx.i_position = cluster->GetElementPosition();
373 if( i_index >= i_index_max )
376 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
377 sizeof( mkv_index_t ) * i_index_max );
383 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
388 for (size_t i=0; i<families.size(); i++)
390 for (size_t j=0; j<of_segment.families.size(); j++)
392 if ( *(families[i]) == *(of_segment.families[j]) )
400 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
404 if ( p_item_a == NULL || p_item_b == NULL )
407 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
408 if ( p_item_b->p_prev_segment_uid != NULL
409 && *p_tmp == *p_item_b->p_prev_segment_uid )
412 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
416 if ( p_item_b->p_segment_uid != NULL
417 && *p_tmp == *p_item_b->p_segment_uid )
420 if ( p_item_b->p_prev_segment_uid != NULL
421 && *p_tmp == *p_item_b->p_prev_segment_uid )
427 bool matroska_segment_c::Preload( )
432 EbmlElement *el = NULL;
434 ep->Reset( &sys.demuxer );
436 while( ( el = ep->Get() ) != NULL )
438 if( MKV_IS_ID( el, KaxSeekHead ) )
440 /* Multiple allowed */
441 /* We bail at 10, to prevent possible recursion */
442 msg_Dbg( &sys.demuxer, "| + Seek head" );
443 if( i_seekhead_count < 10 )
445 i_seekhead_position = (int64_t) es.I_O().getFilePointer();
446 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
449 else if( MKV_IS_ID( el, KaxInfo ) )
451 /* Multiple allowed, mandatory */
452 msg_Dbg( &sys.demuxer, "| + Information" );
453 if( i_info_position < 0 ) // FIXME
454 ParseInfo( static_cast<KaxInfo*>( el ) );
455 i_info_position = (int64_t) es.I_O().getFilePointer();
457 else if( MKV_IS_ID( el, KaxTracks ) )
459 /* Multiple allowed */
460 msg_Dbg( &sys.demuxer, "| + Tracks" );
461 if( i_tracks_position < 0 ) // FIXME
462 ParseTracks( static_cast<KaxTracks*>( el ) );
463 if ( tracks.size() == 0 )
465 msg_Err( &sys.demuxer, "No tracks supported" );
468 i_tracks_position = (int64_t) es.I_O().getFilePointer();
470 else if( MKV_IS_ID( el, KaxCues ) )
472 msg_Dbg( &sys.demuxer, "| + Cues" );
473 if( i_cues_position < 0 )
474 LoadCues( static_cast<KaxCues*>( el ) );
475 i_cues_position = (int64_t) es.I_O().getFilePointer();
477 else if( MKV_IS_ID( el, KaxCluster ) )
479 msg_Dbg( &sys.demuxer, "| + Cluster" );
481 cluster = (KaxCluster*)el;
483 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
487 /* stop pre-parsing the stream */
490 else if( MKV_IS_ID( el, KaxAttachments ) )
492 msg_Dbg( &sys.demuxer, "| + Attachments" );
493 if( i_attachments_position < 0 )
494 ParseAttachments( static_cast<KaxAttachments*>( el ) );
495 i_attachments_position = (int64_t) es.I_O().getFilePointer();
497 else if( MKV_IS_ID( el, KaxChapters ) )
499 msg_Dbg( &sys.demuxer, "| + Chapters" );
500 if( i_chapters_position < 0 )
501 ParseChapters( static_cast<KaxChapters*>( el ) );
502 i_chapters_position = (int64_t) es.I_O().getFilePointer();
504 else if( MKV_IS_ID( el, KaxTag ) )
506 msg_Dbg( &sys.demuxer, "| + Tags" );
507 if( i_tags_position < 0) // FIXME
508 ;//LoadTags( static_cast<KaxTags*>( el ) );
509 i_tags_position = (int64_t) es.I_O().getFilePointer();
512 msg_Dbg( &sys.demuxer, "| + Preload Unknown (%s)", typeid(*el).name() );
520 /* Here we try to load elements that were found in Seek Heads, but not yet parsed */
521 bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position )
523 int64_t i_sav_position = (int64_t)es.I_O().getFilePointer();
526 es.I_O().setFilePointer( i_element_position, seek_beginning );
527 el = es.FindNextID( ClassInfos, 0xFFFFFFFFL);
531 msg_Err( &sys.demuxer, "cannot load some cues/chapters/tags etc. (broken seekhead or file)" );
532 es.I_O().setFilePointer( i_sav_position, seek_beginning );
536 if( MKV_IS_ID( el, KaxSeekHead ) )
538 /* Multiple allowed */
539 msg_Dbg( &sys.demuxer, "| + Seek head" );
540 if( i_seekhead_count < 10 )
542 i_seekhead_position = i_element_position;
543 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
546 else if( MKV_IS_ID( el, KaxInfo ) ) // FIXME
548 /* Multiple allowed, mandatory */
549 msg_Dbg( &sys.demuxer, "| + Information" );
550 if( i_info_position < 0 )
551 ParseInfo( static_cast<KaxInfo*>( el ) );
552 i_info_position = i_element_position;
554 else if( MKV_IS_ID( el, KaxTracks ) ) // FIXME
556 /* Multiple allowed */
557 msg_Dbg( &sys.demuxer, "| + Tracks" );
558 if( i_tracks_position < 0 )
559 ParseTracks( static_cast<KaxTracks*>( el ) );
560 if ( tracks.size() == 0 )
562 msg_Err( &sys.demuxer, "No tracks supported" );
564 es.I_O().setFilePointer( i_sav_position, seek_beginning );
567 i_tracks_position = i_element_position;
569 else if( MKV_IS_ID( el, KaxCues ) )
571 msg_Dbg( &sys.demuxer, "| + Cues" );
572 if( i_cues_position < 0 )
573 LoadCues( static_cast<KaxCues*>( el ) );
574 i_cues_position = i_element_position;
576 else if( MKV_IS_ID( el, KaxAttachments ) )
578 msg_Dbg( &sys.demuxer, "| + Attachments" );
579 if( i_attachments_position < 0 )
580 ParseAttachments( static_cast<KaxAttachments*>( el ) );
581 i_attachments_position = i_element_position;
583 else if( MKV_IS_ID( el, KaxChapters ) )
585 msg_Dbg( &sys.demuxer, "| + Chapters" );
586 if( i_chapters_position < 0 )
587 ParseChapters( static_cast<KaxChapters*>( el ) );
588 i_chapters_position = i_element_position;
590 else if( MKV_IS_ID( el, KaxTag ) ) // FIXME
592 msg_Dbg( &sys.demuxer, "| + Tags" );
593 if( i_tags_position < 0 )
594 ;//LoadTags( static_cast<KaxTags*>( el ) );
595 i_tags_position = i_element_position;
599 msg_Dbg( &sys.demuxer, "| + LoadSeekHeadItem Unknown (%s)", typeid(*el).name() );
603 es.I_O().setFilePointer( i_sav_position, seek_beginning );
607 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
608 const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
612 for( i_track = 0; i_track < tracks.size(); i_track++ )
614 const mkv_track_t *tk = tracks[i_track];
616 if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
617 ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
623 if( i_track >= tracks.size() )
631 bool matroska_segment_c::Select( mtime_t i_start_time )
636 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
637 sys.b_pci_packet_set = false;
639 for( i_track = 0; i_track < tracks.size(); i_track++ )
641 mkv_track_t *p_tk = tracks[i_track];
642 es_format_t *p_fmt = &p_tk->fmt;
644 if( p_fmt->i_cat == UNKNOWN_ES || !p_tk->psz_codec )
646 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, tracks[i_track]->i_number );
647 tracks[i_track]->p_es = NULL;
651 if( !strcmp( tracks[i_track]->psz_codec, "V_MS/VFW/FOURCC" ) )
653 if( tracks[i_track]->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
655 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
656 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
660 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tracks[i_track]->p_extra_data;
662 tracks[i_track]->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
663 tracks[i_track]->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
664 tracks[i_track]->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
666 tracks[i_track]->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
667 if( tracks[i_track]->fmt.i_extra > 0 )
669 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->fmt.i_extra );
670 memcpy( tracks[i_track]->fmt.p_extra, &p_bih[1], tracks[i_track]->fmt.i_extra );
673 p_tk->b_dts_only = true;
675 else if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG1" ) ||
676 !strcmp( tracks[i_track]->psz_codec, "V_MPEG2" ) )
678 tracks[i_track]->fmt.i_codec = VLC_CODEC_MPGV;
680 else if( !strncmp( tracks[i_track]->psz_codec, "V_THEORA", 8 ) )
682 tracks[i_track]->fmt.i_codec = VLC_CODEC_THEORA;
683 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
684 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
685 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
687 else if( !strncmp( tracks[i_track]->psz_codec, "V_REAL/RV", 9 ) )
689 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
690 p_fmt->i_codec = VLC_CODEC_RV10;
691 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
692 p_fmt->i_codec = VLC_CODEC_RV20;
693 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
694 p_fmt->i_codec = VLC_CODEC_RV30;
695 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
696 p_fmt->i_codec = VLC_CODEC_RV40;
698 if( p_tk->i_extra_data > 26 )
700 p_fmt->p_extra = malloc( p_tk->i_extra_data - 26 );
703 p_fmt->i_extra = p_tk->i_extra_data - 26;
704 memcpy( p_fmt->p_extra, &p_tk->p_extra_data[26], p_fmt->i_extra );
707 p_tk->b_dts_only = true;
709 else if( !strncmp( tracks[i_track]->psz_codec, "V_DIRAC", 7 ) )
711 tracks[i_track]->fmt.i_codec = VLC_CODEC_DIRAC;
713 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4", 7 ) )
715 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/MS/V3" ) )
717 tracks[i_track]->fmt.i_codec = VLC_CODEC_DIV3;
719 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO", 11 ) )
721 /* A MPEG 4 codec, SP, ASP, AP or AVC */
722 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO/AVC" ) )
723 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
725 tracks[i_track]->fmt.i_codec = VLC_CODEC_MP4V;
726 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
727 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
728 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
731 else if( !strcmp( tracks[i_track]->psz_codec, "V_QUICKTIME" ) )
733 MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
734 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
735 tracks[i_track]->p_extra_data,
736 tracks[i_track]->i_extra_data,
738 if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
739 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
741 tracks[i_track]->fmt.i_codec = p_box->i_type;
742 tracks[i_track]->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
743 tracks[i_track]->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
744 tracks[i_track]->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
745 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->fmt.i_extra );
746 memcpy( tracks[i_track]->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tracks[i_track]->fmt.i_extra );
747 MP4_FreeBox_sample_vide( p_box );
753 stream_Delete( p_mp4_stream );
755 else if( !strcmp( tracks[i_track]->psz_codec, "A_MS/ACM" ) )
757 if( tracks[i_track]->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
759 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
760 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
764 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tracks[i_track]->p_extra_data;
766 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tracks[i_track]->fmt.i_codec, NULL );
768 tracks[i_track]->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
769 tracks[i_track]->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
770 tracks[i_track]->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
771 tracks[i_track]->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
772 tracks[i_track]->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
774 tracks[i_track]->fmt.i_extra = GetWLE( &p_wf->cbSize );
775 if( tracks[i_track]->fmt.i_extra > 0 )
777 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->fmt.i_extra );
778 memcpy( tracks[i_track]->fmt.p_extra, &p_wf[1], tracks[i_track]->fmt.i_extra );
782 else if( !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L3" ) ||
783 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L2" ) ||
784 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L1" ) )
786 tracks[i_track]->fmt.i_codec = VLC_CODEC_MPGA;
788 else if( !strcmp( tracks[i_track]->psz_codec, "A_AC3" ) )
790 tracks[i_track]->fmt.i_codec = VLC_CODEC_A52;
792 else if( !strcmp( tracks[i_track]->psz_codec, "A_EAC3" ) )
794 tracks[i_track]->fmt.i_codec = VLC_CODEC_EAC3;
796 else if( !strcmp( tracks[i_track]->psz_codec, "A_DTS" ) )
798 tracks[i_track]->fmt.i_codec = VLC_CODEC_DTS;
800 else if( !strcmp( tracks[i_track]->psz_codec, "A_MLP" ) )
802 tracks[i_track]->fmt.i_codec = VLC_CODEC_MLP;
804 else if( !strcmp( tracks[i_track]->psz_codec, "A_TRUEHD" ) )
806 /* FIXME when more samples arrive */
807 tracks[i_track]->fmt.i_codec = VLC_CODEC_TRUEHD;
808 p_fmt->b_packetized = false;
810 else if( !strcmp( tracks[i_track]->psz_codec, "A_FLAC" ) )
812 tracks[i_track]->fmt.i_codec = VLC_CODEC_FLAC;
813 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
814 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
815 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
817 else if( !strcmp( tracks[i_track]->psz_codec, "A_VORBIS" ) )
819 tracks[i_track]->fmt.i_codec = VLC_CODEC_VORBIS;
820 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
821 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
822 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
824 else if( !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
825 !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
827 int i_profile, i_srate, sbr = 0;
828 static const unsigned int i_sample_rates[] =
830 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
831 16000, 12000, 11025, 8000, 7350, 0, 0, 0
834 tracks[i_track]->fmt.i_codec = VLC_CODEC_MP4A;
835 /* create data for faad (MP4DecSpecificDescrTag)*/
837 if( !strcmp( &tracks[i_track]->psz_codec[12], "MAIN" ) )
841 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC" ) )
845 else if( !strcmp( &tracks[i_track]->psz_codec[12], "SSR" ) )
849 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC/SBR" ) )
859 for( i_srate = 0; i_srate < 13; i_srate++ )
861 if( i_sample_rates[i_srate] == tracks[i_track]->i_original_rate )
866 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
868 tracks[i_track]->fmt.i_extra = sbr ? 5 : 2;
869 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->fmt.i_extra );
870 ((uint8_t*)tracks[i_track]->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
871 ((uint8_t*)tracks[i_track]->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tracks[i_track]->fmt.audio.i_channels << 3);
874 int syncExtensionType = 0x2B7;
876 for (iDSRI=0; iDSRI<13; iDSRI++)
877 if( i_sample_rates[iDSRI] == tracks[i_track]->fmt.audio.i_rate )
879 ((uint8_t*)tracks[i_track]->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
880 ((uint8_t*)tracks[i_track]->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
881 ((uint8_t*)tracks[i_track]->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
884 else if( !strcmp( tracks[i_track]->psz_codec, "A_AAC" ) )
886 tracks[i_track]->fmt.i_codec = VLC_CODEC_MP4A;
887 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
888 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
889 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
891 else if( !strcmp( tracks[i_track]->psz_codec, "A_WAVPACK4" ) )
893 tracks[i_track]->fmt.i_codec = VLC_CODEC_WAVPACK;
894 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
895 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
896 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
898 else if( !strcmp( tracks[i_track]->psz_codec, "A_TTA1" ) )
900 p_fmt->i_codec = VLC_CODEC_TTA;
901 p_fmt->i_extra = p_tk->i_extra_data;
902 if( p_fmt->i_extra > 0 )
904 p_fmt->p_extra = xmalloc( p_tk->i_extra_data );
905 memcpy( p_fmt->p_extra, p_tk->p_extra_data, p_tk->i_extra_data );
910 p_fmt->p_extra = xmalloc( p_fmt->i_extra );
911 uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
912 memcpy( &p_extra[ 0], "TTA1", 4 );
913 SetWLE( &p_extra[ 4], 1 );
914 SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
915 SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
916 SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
917 SetDWLE( &p_extra[14], 0xffffffff );
918 memset( &p_extra[18], 0, 30 - 18 );
921 else if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) ||
922 !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/LIT" ) ||
923 !strcmp( tracks[i_track]->psz_codec, "A_PCM/FLOAT/IEEE" ) )
925 if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) )
927 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
931 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
933 tracks[i_track]->fmt.audio.i_blockalign = ( tracks[i_track]->fmt.audio.i_bitspersample + 7 ) / 8 * tracks[i_track]->fmt.audio.i_channels;
935 /* disabled due to the potential "S_KATE" namespace issue */
936 else if( !strcmp( tracks[i_track]->psz_codec, "S_KATE" ) )
938 int i, i_offset = 1, i_extra, num_headers, size_so_far;
941 tracks[i_track]->fmt.i_codec = VLC_CODEC_KATE;
942 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
944 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
945 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
946 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
948 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASCII" ) )
950 p_fmt->i_codec = VLC_CODEC_SUBT;
951 p_fmt->subs.psz_encoding = NULL; /* Is there a place where it is stored ? */
953 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/UTF8" ) )
955 tracks[i_track]->fmt.i_codec = VLC_CODEC_SUBT;
956 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
958 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/USF" ) )
960 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
961 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
962 if( tracks[i_track]->i_extra_data )
964 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
965 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
966 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
969 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/SSA" ) ||
970 !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASS" ) ||
971 !strcmp( tracks[i_track]->psz_codec, "S_SSA" ) ||
972 !strcmp( tracks[i_track]->psz_codec, "S_ASS" ))
974 tracks[i_track]->fmt.i_codec = VLC_CODEC_SSA;
975 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
976 if( tracks[i_track]->i_extra_data )
978 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
979 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
980 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
983 else if( !strcmp( tracks[i_track]->psz_codec, "S_VOBSUB" ) )
985 tracks[i_track]->fmt.i_codec = VLC_CODEC_SPU;
986 if( tracks[i_track]->i_extra_data )
989 char *psz_buf = (char *)malloc( tracks[i_track]->i_extra_data + 1);
990 if( psz_buf != NULL )
992 memcpy( psz_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
993 psz_buf[tracks[i_track]->i_extra_data] = '\0';
995 psz_start = strstr( psz_buf, "size:" );
997 vobsub_size_parse( psz_start,
998 &tracks[i_track]->fmt.subs.spu.i_original_frame_width,
999 &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1001 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1002 tracks[i_track]->fmt.subs.spu.i_original_frame_width,
1003 tracks[i_track]->fmt.subs.spu.i_original_frame_height );
1007 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1010 psz_start = strstr( psz_buf, "palette:" );
1012 vobsub_palette_parse( psz_start, &tracks[i_track]->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1014 tracks[i_track]->fmt.subs.spu.palette[0] = 0xBeef;
1015 msg_Dbg( &sys.demuxer, "vobsub palette read" );
1019 msg_Warn( &sys.demuxer, "reading original palette failed" );
1025 else if( !strcmp( tracks[i_track]->psz_codec, "S_HDMV/PGS" ) )
1027 tracks[i_track]->fmt.i_codec = VLC_CODEC_BD_PG;
1029 else if( !strcmp( tracks[i_track]->psz_codec, "B_VOBBTN" ) )
1031 tracks[i_track]->fmt.i_cat = NAV_ES;
1034 else if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1036 p_fmt->i_codec = VLC_CODEC_RA_144;
1037 p_fmt->audio.i_channels = 1;
1038 p_fmt->audio.i_rate = 8000;
1039 p_fmt->audio.i_blockalign = 0x14;
1043 msg_Err( &sys.demuxer, "unknown codec id=`%s'", tracks[i_track]->psz_codec );
1044 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1046 if( tracks[i_track]->b_default )
1048 tracks[i_track]->fmt.i_priority = 1000;
1051 tracks[i_track]->p_es = es_out_Add( sys.demuxer.out, &tracks[i_track]->fmt );
1053 /* Turn on a subtitles track if it has been flagged as default -
1054 * but only do this if no subtitles track has already been engaged,
1055 * either by an earlier 'default track' (??) or by default
1056 * language choice behaviour.
1058 if( tracks[i_track]->b_default )
1060 es_out_Control( sys.demuxer.out,
1061 ES_OUT_SET_ES_DEFAULT,
1062 tracks[i_track]->p_es );
1065 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
1067 sys.i_start_pts = i_start_time;
1068 // reset the stream reading to the first cluster of the segment used
1069 es.I_O().setFilePointer( i_start_pos );
1072 ep = new EbmlParser( &es, segment, &sys.demuxer );
1077 void matroska_segment_c::UnSelect( )
1081 for( i_track = 0; i_track < tracks.size(); i_track++ )
1083 if ( tracks[i_track]->p_es != NULL )
1085 // es_format_Clean( &tracks[i_track]->fmt );
1086 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
1087 tracks[i_track]->p_es = NULL;
1094 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1096 pp_simpleblock = NULL;
1103 EbmlElement *el = NULL;
1107 return VLC_EGENERIC;
1109 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1111 /* Check blocks validity to protect againts broken files */
1112 if( BlockFindTrackIndex( NULL, pp_block , pp_simpleblock ) )
1115 pp_simpleblock = NULL;
1120 /* update the index */
1121 #define idx p_indexes[i_index - 1]
1122 if( i_index > 0 && idx.i_time == -1 )
1124 if ( pp_simpleblock != NULL )
1125 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1127 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1128 idx.b_key = *pi_ref1 == 0 ? true : false;
1134 i_level = ep->GetLevel();
1143 msg_Warn( &sys.demuxer, "EOF" );
1144 return VLC_EGENERIC;
1147 /* Verify that we are still inside our cluster
1148 * It can happens whith broken files and when seeking
1152 if( cluster && !ep->IsTopPresent( cluster ) )
1154 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1165 if( MKV_IS_ID( el, KaxCluster ) )
1167 cluster = (KaxCluster*)el;
1168 i_cluster_pos = cluster->GetElementPosition();
1170 /* add it to the index */
1172 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1174 IndexAppendCluster( cluster );
1177 // reset silent tracks
1178 for (size_t i=0; i<tracks.size(); i++)
1180 tracks[i]->b_silent = false;
1185 else if( MKV_IS_ID( el, KaxCues ) )
1187 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1188 return VLC_EGENERIC;
1192 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1196 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1198 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1200 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1201 cluster->InitTimecode( uint64( ctc ), i_timescale );
1203 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1207 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1209 i_block_pos = el->GetElementPosition();
1212 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1214 pp_simpleblock = (KaxSimpleBlock*)el;
1216 pp_simpleblock->ReadData( es.I_O() );
1217 pp_simpleblock->SetParent( *cluster );
1221 if( MKV_IS_ID( el, KaxBlock ) )
1223 pp_block = (KaxBlock*)el;
1225 pp_block->ReadData( es.I_O() );
1226 pp_block->SetParent( *cluster );
1230 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1232 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1234 dur.ReadData( es.I_O() );
1235 *pi_duration = uint64( dur );
1237 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1239 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1241 ref.ReadData( es.I_O() );
1244 *pi_ref1 = int64( ref ) * cluster->GlobalTimecodeScale();
1246 else if( *pi_ref2 == 0 )
1248 *pi_ref2 = int64( ref ) * cluster->GlobalTimecodeScale();
1251 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1253 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1254 track_num.ReadData( es.I_O() );
1256 for (size_t i=0; i<tracks.size(); i++)
1258 if ( tracks[i]->i_number == uint32(track_num))
1260 tracks[i]->b_silent = true;
1267 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1268 return VLC_EGENERIC;