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 matroska_segment_c::~matroska_segment_c()
34 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
36 delete tracks[i_track]->p_compression_data;
37 es_format_Clean( &tracks[i_track]->fmt );
38 free( tracks[i_track]->p_extra_data );
39 free( tracks[i_track]->psz_codec );
40 delete tracks[i_track];
43 free( psz_writing_application );
44 free( psz_muxing_application );
45 free( psz_segment_filename );
53 delete p_prev_segment_uid;
54 delete p_next_segment_uid;
56 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
57 while ( index != stored_editions.end() )
62 std::vector<chapter_translation_c*>::iterator indext = translations.begin();
63 while ( indext != translations.end() )
68 std::vector<KaxSegmentFamily*>::iterator indexf = families.begin();
69 while ( indexf != families.end() )
77 /*****************************************************************************
79 * * LoadCues : load the cues element and update index
81 * * LoadTags : load ... the tags element
83 * * InformationCreate : create all information, load tags if present
85 *****************************************************************************/
86 void matroska_segment_c::LoadCues( KaxCues *cues )
93 msg_Err( &sys.demuxer, "There can be only 1 Cues per section." );
97 ep = new EbmlParser( &es, cues, &sys.demuxer );
98 while( ( el = ep->Get() ) != NULL )
100 if( MKV_IS_ID( el, KaxCuePoint ) )
102 #define idx p_indexes[i_index]
105 idx.i_block_number= -1;
111 while( ( el = ep->Get() ) != NULL )
113 if( MKV_IS_ID( el, KaxCueTime ) )
115 KaxCueTime &ctime = *(KaxCueTime*)el;
117 ctime.ReadData( es.I_O() );
119 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
121 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
124 while( ( el = ep->Get() ) != NULL )
126 if( MKV_IS_ID( el, KaxCueTrack ) )
128 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
130 ctrack.ReadData( es.I_O() );
131 idx.i_track = uint16( ctrack );
133 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
135 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
137 ccpos.ReadData( es.I_O() );
138 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
140 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
142 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
144 cbnum.ReadData( es.I_O() );
145 idx.i_block_number = uint32( cbnum );
149 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
156 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
162 msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
163 " track=%d bnum=%d", idx.i_time, idx.i_position,
164 idx.i_track, idx.i_block_number );
168 if( i_index >= i_index_max )
171 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
177 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
182 msg_Dbg( &sys.demuxer, "| - loading cues done." );
185 void matroska_segment_c::LoadTags( KaxTags *tags )
190 /* Master elements */
191 ep = new EbmlParser( &es, tags, &sys.demuxer );
193 while( ( el = ep->Get() ) != NULL )
195 if( MKV_IS_ID( el, KaxTag ) )
197 msg_Dbg( &sys.demuxer, "+ Tag" );
199 while( ( el = ep->Get() ) != NULL )
201 if( MKV_IS_ID( el, KaxTagTargets ) )
203 msg_Dbg( &sys.demuxer, "| + Targets" );
205 while( ( el = ep->Get() ) != NULL )
207 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
211 else if( MKV_IS_ID( el, KaxTagGeneral ) )
213 msg_Dbg( &sys.demuxer, "| + General" );
215 while( ( el = ep->Get() ) != NULL )
217 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
221 else if( MKV_IS_ID( el, KaxTagGenres ) )
223 msg_Dbg( &sys.demuxer, "| + Genres" );
225 while( ( el = ep->Get() ) != NULL )
227 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
231 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
233 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
235 while( ( el = ep->Get() ) != NULL )
237 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
241 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
243 msg_Dbg( &sys.demuxer, "| + Images Specific" );
245 while( ( el = ep->Get() ) != NULL )
247 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
251 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
253 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
255 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
257 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
259 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
261 msg_Dbg( &sys.demuxer, "| + Multi Date" );
263 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
265 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
267 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
269 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
271 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
273 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
275 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
277 msg_Dbg( &sys.demuxer, "| + Multi Title" );
281 msg_Dbg( &sys.demuxer, "| + LoadTag Unknown (%s)", typeid( *el ).name() );
288 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
293 msg_Dbg( &sys.demuxer, "loading tags done." );
296 /*****************************************************************************
298 *****************************************************************************/
299 void matroska_segment_c::InformationCreate( )
301 sys.meta = vlc_meta_New();
305 vlc_meta_SetTitle( sys.meta, psz_title );
309 vlc_meta_SetDate( sys.meta, psz_date_utc );
312 if( psz_segment_filename )
314 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
316 if( psz_muxing_application )
318 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
320 if( psz_writing_application )
322 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
325 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
327 // mkv_track_t *tk = tracks[i_track];
328 // vlc_meta_t *mtk = vlc_meta_New();
329 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
333 if( i_tags_position >= 0 )
337 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
347 /*****************************************************************************
349 *****************************************************************************/
351 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
353 #define idx p_indexes[i_index]
355 idx.i_block_number= -1;
356 idx.i_position = cluster->GetElementPosition();
361 if( i_index >= i_index_max )
364 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
370 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
375 for (size_t i=0; i<families.size(); i++)
377 for (size_t j=0; j<of_segment.families.size(); j++)
379 if ( *(families[i]) == *(of_segment.families[j]) )
387 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
391 if ( p_item_a == NULL || p_item_b == NULL )
394 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
395 if ( p_item_b->p_prev_segment_uid != NULL
396 && *p_tmp == *p_item_b->p_prev_segment_uid )
399 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
403 if ( p_item_b->p_segment_uid != NULL
404 && *p_tmp == *p_item_b->p_segment_uid )
407 if ( p_item_b->p_prev_segment_uid != NULL
408 && *p_tmp == *p_item_b->p_prev_segment_uid )
414 bool matroska_segment_c::Preload( )
419 EbmlElement *el = NULL;
421 ep->Reset( &sys.demuxer );
423 while( ( el = ep->Get() ) != NULL )
425 if( MKV_IS_ID( el, KaxSeekHead ) )
427 /* Multiple allowed */
428 /* We bail at 10, to prevent possible recursion */
429 msg_Dbg( &sys.demuxer, "| + Seek head" );
430 if( i_seekhead_count < 10 )
432 i_seekhead_position = (int64_t) es.I_O().getFilePointer();
433 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
436 else if( MKV_IS_ID( el, KaxInfo ) )
438 /* Multiple allowed, mandatory */
439 msg_Dbg( &sys.demuxer, "| + Information" );
440 if( i_info_position < 0 ) // FIXME
441 ParseInfo( static_cast<KaxInfo*>( el ) );
442 i_info_position = (int64_t) es.I_O().getFilePointer();
444 else if( MKV_IS_ID( el, KaxTracks ) )
446 /* Multiple allowed */
447 msg_Dbg( &sys.demuxer, "| + Tracks" );
448 if( i_tracks_position < 0 ) // FIXME
449 ParseTracks( static_cast<KaxTracks*>( el ) );
450 if ( tracks.size() == 0 )
452 msg_Err( &sys.demuxer, "No tracks supported" );
455 i_tracks_position = (int64_t) es.I_O().getFilePointer();
457 else if( MKV_IS_ID( el, KaxCues ) )
459 msg_Dbg( &sys.demuxer, "| + Cues" );
460 if( i_cues_position < 0 )
461 LoadCues( static_cast<KaxCues*>( el ) );
462 i_cues_position = (int64_t) es.I_O().getFilePointer();
464 else if( MKV_IS_ID( el, KaxCluster ) )
466 msg_Dbg( &sys.demuxer, "| + Cluster" );
468 cluster = (KaxCluster*)el;
470 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
474 /* stop pre-parsing the stream */
477 else if( MKV_IS_ID( el, KaxAttachments ) )
479 msg_Dbg( &sys.demuxer, "| + Attachments" );
480 if( i_attachments_position < 0 )
481 ParseAttachments( static_cast<KaxAttachments*>( el ) );
482 i_attachments_position = (int64_t) es.I_O().getFilePointer();
484 else if( MKV_IS_ID( el, KaxChapters ) )
486 msg_Dbg( &sys.demuxer, "| + Chapters" );
487 if( i_chapters_position < 0 )
488 ParseChapters( static_cast<KaxChapters*>( el ) );
489 i_chapters_position = (int64_t) es.I_O().getFilePointer();
491 else if( MKV_IS_ID( el, KaxTag ) )
493 msg_Dbg( &sys.demuxer, "| + Tags" );
494 if( i_tags_position < 0) // FIXME
495 ;//LoadTags( static_cast<KaxTags*>( el ) );
496 i_tags_position = (int64_t) es.I_O().getFilePointer();
499 msg_Dbg( &sys.demuxer, "| + Preload Unknown (%s)", typeid(*el).name() );
507 /* Here we try to load elements that were found in Seek Heads, but not yet parsed */
508 bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position )
510 int64_t i_sav_position = (int64_t)es.I_O().getFilePointer();
513 es.I_O().setFilePointer( i_element_position, seek_beginning );
514 el = es.FindNextID( ClassInfos, 0xFFFFFFFFL);
518 msg_Err( &sys.demuxer, "cannot load some cues/chapters/tags etc. (broken seekhead or file)" );
519 es.I_O().setFilePointer( i_sav_position, seek_beginning );
523 if( MKV_IS_ID( el, KaxSeekHead ) )
525 /* Multiple allowed */
526 msg_Dbg( &sys.demuxer, "| + Seek head" );
527 if( i_seekhead_count < 10 )
529 i_seekhead_position = i_element_position;
530 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
533 else if( MKV_IS_ID( el, KaxInfo ) ) // FIXME
535 /* Multiple allowed, mandatory */
536 msg_Dbg( &sys.demuxer, "| + Information" );
537 if( i_info_position < 0 )
538 ParseInfo( static_cast<KaxInfo*>( el ) );
539 i_info_position = i_element_position;
541 else if( MKV_IS_ID( el, KaxTracks ) ) // FIXME
543 /* Multiple allowed */
544 msg_Dbg( &sys.demuxer, "| + Tracks" );
545 if( i_tracks_position < 0 )
546 ParseTracks( static_cast<KaxTracks*>( el ) );
547 if ( tracks.size() == 0 )
549 msg_Err( &sys.demuxer, "No tracks supported" );
551 es.I_O().setFilePointer( i_sav_position, seek_beginning );
554 i_tracks_position = i_element_position;
556 else if( MKV_IS_ID( el, KaxCues ) )
558 msg_Dbg( &sys.demuxer, "| + Cues" );
559 if( i_cues_position < 0 )
560 LoadCues( static_cast<KaxCues*>( el ) );
561 i_cues_position = i_element_position;
563 else if( MKV_IS_ID( el, KaxAttachments ) )
565 msg_Dbg( &sys.demuxer, "| + Attachments" );
566 if( i_attachments_position < 0 )
567 ParseAttachments( static_cast<KaxAttachments*>( el ) );
568 i_attachments_position = i_element_position;
570 else if( MKV_IS_ID( el, KaxChapters ) )
572 msg_Dbg( &sys.demuxer, "| + Chapters" );
573 if( i_chapters_position < 0 )
574 ParseChapters( static_cast<KaxChapters*>( el ) );
575 i_chapters_position = i_element_position;
577 else if( MKV_IS_ID( el, KaxTag ) ) // FIXME
579 msg_Dbg( &sys.demuxer, "| + Tags" );
580 if( i_tags_position < 0 )
581 ;//LoadTags( static_cast<KaxTags*>( el ) );
582 i_tags_position = i_element_position;
586 msg_Dbg( &sys.demuxer, "| + LoadSeekHeadItem Unknown (%s)", typeid(*el).name() );
590 es.I_O().setFilePointer( i_sav_position, seek_beginning );
594 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
595 const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
599 for( i_track = 0; i_track < tracks.size(); i_track++ )
601 const mkv_track_t *tk = tracks[i_track];
603 if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
604 ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
610 if( i_track >= tracks.size() )
618 bool matroska_segment_c::Select( mtime_t i_start_time )
623 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
624 sys.b_pci_packet_set = false;
626 for( i_track = 0; i_track < tracks.size(); i_track++ )
628 mkv_track_t *p_tk = tracks[i_track];
629 es_format_t *p_fmt = &p_tk->fmt;
631 if( tracks[i_track]->fmt.i_cat == UNKNOWN_ES )
633 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, tracks[i_track]->i_number );
634 tracks[i_track]->p_es = NULL;
638 if( !strcmp( tracks[i_track]->psz_codec, "V_MS/VFW/FOURCC" ) )
640 if( tracks[i_track]->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
642 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
643 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
647 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tracks[i_track]->p_extra_data;
649 tracks[i_track]->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
650 tracks[i_track]->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
651 tracks[i_track]->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
653 tracks[i_track]->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
654 if( tracks[i_track]->fmt.i_extra > 0 )
656 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
657 memcpy( tracks[i_track]->fmt.p_extra, &p_bih[1], tracks[i_track]->fmt.i_extra );
661 else if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG1" ) ||
662 !strcmp( tracks[i_track]->psz_codec, "V_MPEG2" ) )
664 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
666 else if( !strncmp( tracks[i_track]->psz_codec, "V_THEORA", 8 ) )
668 uint8_t *p_data = tracks[i_track]->p_extra_data;
669 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
670 if( tracks[i_track]->i_extra_data >= 4 ) {
671 if( p_data[0] == 2 ) {
673 int i_size1 = 0, i_size2 = 0;
675 /* read size of first header packet */
676 while( *p_data == 0xFF &&
677 i < tracks[i_track]->i_extra_data )
686 msg_Dbg( &sys.demuxer, "first theora header size %d", i_size1 );
687 /* read size of second header packet */
688 while( *p_data == 0xFF &&
689 i < tracks[i_track]->i_extra_data )
698 int i_size3 = tracks[i_track]->i_extra_data - i - i_size1
700 msg_Dbg( &sys.demuxer, "second theora header size %d", i_size2 );
701 msg_Dbg( &sys.demuxer, "third theora header size %d", i_size3 );
702 tracks[i_track]->fmt.i_extra = i_size1 + i_size2 + i_size3
704 if( i_size1 > 0 && i_size2 > 0 && i_size3 > 0 ) {
705 tracks[i_track]->fmt.p_extra =
706 malloc( tracks[i_track]->fmt.i_extra );
707 uint8_t *p_out = (uint8_t*)tracks[i_track]->fmt.p_extra;
708 *p_out++ = (i_size1>>8) & 0xFF;
709 *p_out++ = i_size1 & 0xFF;
710 memcpy( p_out, p_data, i_size1 );
714 *p_out++ = (i_size2>>8) & 0xFF;
715 *p_out++ = i_size2 & 0xFF;
716 memcpy( p_out, p_data, i_size2 );
720 *p_out++ = (i_size3>>8) & 0xFF;
721 *p_out++ = i_size3 & 0xFF;
722 memcpy( p_out, p_data, i_size3 );
728 msg_Err( &sys.demuxer, "inconsistant theora extradata" );
732 msg_Err( &sys.demuxer, "Wrong number of ogg packets with theora headers (%d)", p_data[0] + 1 );
736 else if( !strncmp( tracks[i_track]->psz_codec, "V_REAL/RV", 9 ) )
738 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
739 p_fmt->i_codec = VLC_FOURCC( 'R', 'V', '1', '0' );
740 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
741 p_fmt->i_codec = VLC_FOURCC( 'R', 'V', '2', '0' );
742 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
743 p_fmt->i_codec = VLC_FOURCC( 'R', 'V', '3', '0' );
744 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
745 p_fmt->i_codec = VLC_FOURCC( 'R', 'V', '4', '0' );
747 if( p_tk->i_extra_data > 26 )
749 p_fmt->p_extra = malloc( p_tk->i_extra_data - 26 );
752 p_fmt->i_extra = p_tk->i_extra_data - 26;
753 memcpy( p_fmt->p_extra, &p_tk->p_extra_data[26], p_fmt->i_extra );
757 else if( !strncmp( tracks[i_track]->psz_codec, "V_DIRAC", 7 ) )
759 tracks[i_track]->fmt.i_codec = VLC_FOURCC('d', 'r', 'a', 'c' );
761 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4", 7 ) )
763 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/MS/V3" ) )
765 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
767 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO", 11 ) )
769 /* A MPEG 4 codec, SP, ASP, AP or AVC */
770 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO/AVC" ) )
771 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
773 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
774 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
775 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
776 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
779 else if( !strcmp( tracks[i_track]->psz_codec, "V_QUICKTIME" ) )
781 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
782 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
783 tracks[i_track]->p_extra_data,
784 tracks[i_track]->i_extra_data,
786 MP4_ReadBoxCommon( p_mp4_stream, p_box );
787 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
788 tracks[i_track]->fmt.i_codec = p_box->i_type;
789 tracks[i_track]->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
790 tracks[i_track]->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
791 tracks[i_track]->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
792 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
793 memcpy( tracks[i_track]->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tracks[i_track]->fmt.i_extra );
794 MP4_FreeBox_sample_vide( p_box );
795 stream_Delete( p_mp4_stream );
797 else if( !strcmp( tracks[i_track]->psz_codec, "A_MS/ACM" ) )
799 if( tracks[i_track]->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
801 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
802 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
806 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tracks[i_track]->p_extra_data;
808 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tracks[i_track]->fmt.i_codec, NULL );
810 tracks[i_track]->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
811 tracks[i_track]->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
812 tracks[i_track]->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
813 tracks[i_track]->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
814 tracks[i_track]->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
816 tracks[i_track]->fmt.i_extra = GetWLE( &p_wf->cbSize );
817 if( tracks[i_track]->fmt.i_extra > 0 )
819 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
820 memcpy( tracks[i_track]->fmt.p_extra, &p_wf[1], tracks[i_track]->fmt.i_extra );
824 else if( !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L3" ) ||
825 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L2" ) ||
826 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L1" ) )
828 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
830 else if( !strcmp( tracks[i_track]->psz_codec, "A_AC3" ) )
832 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
834 else if( !strcmp( tracks[i_track]->psz_codec, "A_EAC3" ) )
836 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'e', 'a', 'c', '3' );
838 else if( !strcmp( tracks[i_track]->psz_codec, "A_DTS" ) )
840 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
842 else if( !strcmp( tracks[i_track]->psz_codec, "A_FLAC" ) )
844 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
845 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
846 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
847 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
849 else if( !strcmp( tracks[i_track]->psz_codec, "A_VORBIS" ) )
851 int i, i_offset = 1, i_size[3], i_extra;
854 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
856 /* Split the 3 headers */
857 if( tracks[i_track]->p_extra_data[0] != 0x02 )
858 msg_Err( &sys.demuxer, "invalid vorbis header" );
860 for( i = 0; i < 2; i++ )
863 while( i_offset < tracks[i_track]->i_extra_data )
865 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
866 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
870 i_size[0] = __MIN(i_size[0], tracks[i_track]->i_extra_data - i_offset);
871 i_size[1] = __MIN(i_size[1], tracks[i_track]->i_extra_data -i_offset -i_size[0]);
872 i_size[2] = tracks[i_track]->i_extra_data - i_offset - i_size[0] - i_size[1];
874 tracks[i_track]->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
875 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
876 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra; i_extra = 0;
877 for( i = 0; i < 3; i++ )
879 *(p_extra++) = i_size[i] >> 8;
880 *(p_extra++) = i_size[i] & 0xFF;
881 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra,
883 p_extra += i_size[i];
884 i_extra += i_size[i];
887 else if( !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
888 !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
890 int i_profile, i_srate, sbr = 0;
891 static const unsigned int i_sample_rates[] =
893 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
894 16000, 12000, 11025, 8000, 7350, 0, 0, 0
897 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
898 /* create data for faad (MP4DecSpecificDescrTag)*/
900 if( !strcmp( &tracks[i_track]->psz_codec[12], "MAIN" ) )
904 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC" ) )
908 else if( !strcmp( &tracks[i_track]->psz_codec[12], "SSR" ) )
912 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC/SBR" ) )
922 for( i_srate = 0; i_srate < 13; i_srate++ )
924 if( i_sample_rates[i_srate] == tracks[i_track]->i_original_rate )
929 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
931 tracks[i_track]->fmt.i_extra = sbr ? 5 : 2;
932 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
933 ((uint8_t*)tracks[i_track]->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
934 ((uint8_t*)tracks[i_track]->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tracks[i_track]->fmt.audio.i_channels << 3);
937 int syncExtensionType = 0x2B7;
939 for (iDSRI=0; iDSRI<13; iDSRI++)
940 if( i_sample_rates[iDSRI] == tracks[i_track]->fmt.audio.i_rate )
942 ((uint8_t*)tracks[i_track]->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
943 ((uint8_t*)tracks[i_track]->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
944 ((uint8_t*)tracks[i_track]->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
947 else if( !strcmp( tracks[i_track]->psz_codec, "A_AAC" ) )
949 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
950 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
951 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
952 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
954 else if( !strcmp( tracks[i_track]->psz_codec, "A_WAVPACK4" ) )
956 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'W', 'V', 'P', 'K' );
957 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
958 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
959 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
961 else if( !strcmp( tracks[i_track]->psz_codec, "A_TTA1" ) )
963 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'T', 'T', 'A', '1' );
964 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
965 tracks[i_track]->fmt.p_extra = malloc( 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 );
968 else if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) ||
969 !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/LIT" ) ||
970 !strcmp( tracks[i_track]->psz_codec, "A_PCM/FLOAT/IEEE" ) )
972 if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) )
974 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
978 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
980 tracks[i_track]->fmt.audio.i_blockalign = ( tracks[i_track]->fmt.audio.i_bitspersample + 7 ) / 8 * tracks[i_track]->fmt.audio.i_channels;
982 /* disabled due to the potential "S_KATE" namespace issue */
983 else if( !strcmp( tracks[i_track]->psz_codec, "S_KATE" ) )
985 int i, i_offset = 1, *i_size, i_extra, num_headers, size_so_far;
988 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'k', 'a', 't', 'e' );
989 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
991 /* Recover the number of headers to expect */
992 num_headers = tracks[i_track]->p_extra_data[0]+1;
993 msg_Dbg( &sys.demuxer, "kate in mkv detected: %d headers in %u bytes",
994 num_headers, tracks[i_track]->i_extra_data);
996 /* this won't overflow the stack as is can allocate only 1020 bytes max */
997 i_size = (int*)alloca(num_headers*sizeof(int));
999 /* Split the headers */
1001 for( i = 0; i < num_headers-1; i++ )
1004 while( i_offset < tracks[i_track]->i_extra_data )
1006 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
1007 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
1009 msg_Dbg( &sys.demuxer, "kate header %d is %d bytes", i, i_size[i]);
1010 size_so_far += i_size[i];
1012 i_size[num_headers-1] = tracks[i_track]->i_extra_data - (size_so_far+i_offset);
1013 msg_Dbg( &sys.demuxer, "kate last header (%d) is %d bytes", num_headers-1, i_size[num_headers-1]);
1015 tracks[i_track]->fmt.i_extra = 1 + num_headers * 2 + size_so_far + i_size[num_headers-1];
1016 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
1018 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra;
1020 *(p_extra++) = num_headers;
1022 for( i = 0; i < num_headers; i++ )
1024 *(p_extra++) = i_size[i] >> 8;
1025 *(p_extra++) = i_size[i] & 0xFF;
1026 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra-1,
1028 p_extra += i_size[i];
1029 i_extra += i_size[i];
1032 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASCII" ) )
1034 p_fmt->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1035 p_fmt->subs.psz_encoding = NULL; /* Is there a place where it is stored ? */
1037 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/UTF8" ) )
1039 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1040 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
1042 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/USF" ) )
1044 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1045 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
1046 if( tracks[i_track]->i_extra_data )
1048 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
1049 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
1050 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
1053 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/SSA" ) ||
1054 !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASS" ) ||
1055 !strcmp( tracks[i_track]->psz_codec, "S_SSA" ) ||
1056 !strcmp( tracks[i_track]->psz_codec, "S_ASS" ))
1058 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1059 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
1060 if( tracks[i_track]->i_extra_data )
1062 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
1063 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
1064 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
1067 else if( !strcmp( tracks[i_track]->psz_codec, "S_VOBSUB" ) )
1069 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1070 if( tracks[i_track]->i_extra_data )
1073 char *p_buf = (char *)malloc( tracks[i_track]->i_extra_data + 1);
1074 memcpy( p_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
1075 p_buf[tracks[i_track]->i_extra_data] = '\0';
1077 p_start = strstr( p_buf, "size:" );
1078 if( sscanf( p_start, "size: %dx%d",
1079 &tracks[i_track]->fmt.subs.spu.i_original_frame_width, &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) == 2 )
1081 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d", tracks[i_track]->fmt.subs.spu.i_original_frame_width, tracks[i_track]->fmt.subs.spu.i_original_frame_height );
1085 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1090 else if( !strcmp( tracks[i_track]->psz_codec, "B_VOBBTN" ) )
1092 tracks[i_track]->fmt.i_cat = NAV_ES;
1095 else if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1097 p_fmt->i_codec = VLC_FOURCC( '1', '4', '_', '4');
1098 p_fmt->audio.i_channels = 1;
1099 p_fmt->audio.i_rate = 8000;
1100 p_fmt->audio.i_blockalign = 0x14;
1104 msg_Err( &sys.demuxer, "unknown codec id=`%s'", tracks[i_track]->psz_codec );
1105 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1107 if( tracks[i_track]->b_default )
1109 tracks[i_track]->fmt.i_priority = 1000;
1112 tracks[i_track]->p_es = es_out_Add( sys.demuxer.out, &tracks[i_track]->fmt );
1114 /* Turn on a subtitles track if it has been flagged as default -
1115 * but only do this if no subtitles track has already been engaged,
1116 * either by an earlier 'default track' (??) or by default
1117 * language choice behaviour.
1119 if( tracks[i_track]->b_default )
1121 es_out_Control( sys.demuxer.out,
1122 ES_OUT_SET_ES_DEFAULT,
1123 tracks[i_track]->p_es );
1126 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
1128 sys.i_start_pts = i_start_time;
1129 // reset the stream reading to the first cluster of the segment used
1130 es.I_O().setFilePointer( i_start_pos );
1133 ep = new EbmlParser( &es, segment, &sys.demuxer );
1138 void matroska_segment_c::UnSelect( )
1142 for( i_track = 0; i_track < tracks.size(); i_track++ )
1144 if ( tracks[i_track]->p_es != NULL )
1146 // es_format_Clean( &tracks[i_track]->fmt );
1147 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
1148 tracks[i_track]->p_es = NULL;
1155 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1157 pp_simpleblock = NULL;
1164 EbmlElement *el = NULL;
1168 return VLC_EGENERIC;
1170 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1172 /* Check blocks validity to protect againts broken files */
1173 if( BlockFindTrackIndex( NULL, pp_block , pp_simpleblock ) )
1176 pp_simpleblock = NULL;
1181 /* update the index */
1182 #define idx p_indexes[i_index - 1]
1183 if( i_index > 0 && idx.i_time == -1 )
1185 if ( pp_simpleblock != NULL )
1186 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1188 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1189 idx.b_key = *pi_ref1 == 0 ? true : false;
1195 i_level = ep->GetLevel();
1204 msg_Warn( &sys.demuxer, "EOF" );
1205 return VLC_EGENERIC;
1208 /* Verify that we are still inside our cluster
1209 * It can happens whith broken files and when seeking
1213 if( cluster && !ep->IsTopPresent( cluster ) )
1215 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1226 if( MKV_IS_ID( el, KaxCluster ) )
1228 cluster = (KaxCluster*)el;
1229 i_cluster_pos = cluster->GetElementPosition();
1231 /* add it to the index */
1233 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1235 IndexAppendCluster( cluster );
1238 // reset silent tracks
1239 for (size_t i=0; i<tracks.size(); i++)
1241 tracks[i]->b_silent = false;
1246 else if( MKV_IS_ID( el, KaxCues ) )
1248 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1249 return VLC_EGENERIC;
1253 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1257 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1259 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1261 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1262 cluster->InitTimecode( uint64( ctc ), i_timescale );
1264 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1268 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1270 i_block_pos = el->GetElementPosition();
1273 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1275 pp_simpleblock = (KaxSimpleBlock*)el;
1277 pp_simpleblock->ReadData( es.I_O() );
1278 pp_simpleblock->SetParent( *cluster );
1282 if( MKV_IS_ID( el, KaxBlock ) )
1284 pp_block = (KaxBlock*)el;
1286 pp_block->ReadData( es.I_O() );
1287 pp_block->SetParent( *cluster );
1291 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1293 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1295 dur.ReadData( es.I_O() );
1296 *pi_duration = uint64( dur );
1298 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1300 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1302 ref.ReadData( es.I_O() );
1305 *pi_ref1 = int64( ref ) * cluster->GlobalTimecodeScale();
1307 else if( *pi_ref2 == 0 )
1309 *pi_ref2 = int64( ref ) * cluster->GlobalTimecodeScale();
1312 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1314 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1315 track_num.ReadData( es.I_O() );
1317 for (size_t i=0; i<tracks.size(); i++)
1319 if ( tracks[i]->i_number == uint32(track_num))
1321 tracks[i]->b_silent = true;
1328 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1329 return VLC_EGENERIC;