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"
31 /* GetFourCC helper */
32 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
33 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
35 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
39 matroska_segment_c::~matroska_segment_c()
41 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
43 delete tracks[i_track]->p_compression_data;
44 es_format_Clean( &tracks[i_track]->fmt );
45 free( tracks[i_track]->p_extra_data );
46 free( tracks[i_track]->psz_codec );
47 delete tracks[i_track];
50 free( psz_writing_application );
51 free( psz_muxing_application );
52 free( psz_segment_filename );
60 delete p_prev_segment_uid;
61 delete p_next_segment_uid;
63 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
64 while ( index != stored_editions.end() )
69 std::vector<chapter_translation_c*>::iterator indext = translations.begin();
70 while ( indext != translations.end() )
75 std::vector<KaxSegmentFamily*>::iterator indexf = families.begin();
76 while ( indexf != families.end() )
84 /*****************************************************************************
86 * * LoadCues : load the cues element and update index
88 * * LoadTags : load ... the tags element
90 * * InformationCreate : create all information, load tags if present
92 *****************************************************************************/
93 void matroska_segment_c::LoadCues( KaxCues *cues )
100 msg_Err( &sys.demuxer, "There can be only 1 Cues per section." );
104 ep = new EbmlParser( &es, cues, &sys.demuxer );
105 while( ( el = ep->Get() ) != NULL )
107 if( MKV_IS_ID( el, KaxCuePoint ) )
109 #define idx p_indexes[i_index]
112 idx.i_block_number= -1;
118 while( ( el = ep->Get() ) != NULL )
120 if( MKV_IS_ID( el, KaxCueTime ) )
122 KaxCueTime &ctime = *(KaxCueTime*)el;
124 ctime.ReadData( es.I_O() );
126 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
128 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
131 while( ( el = ep->Get() ) != NULL )
133 if( MKV_IS_ID( el, KaxCueTrack ) )
135 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
137 ctrack.ReadData( es.I_O() );
138 idx.i_track = uint16( ctrack );
140 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
142 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
144 ccpos.ReadData( es.I_O() );
145 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
147 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
149 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
151 cbnum.ReadData( es.I_O() );
152 idx.i_block_number = uint32( cbnum );
156 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
163 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
169 msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
170 " track=%d bnum=%d", idx.i_time, idx.i_position,
171 idx.i_track, idx.i_block_number );
175 if( i_index >= i_index_max )
178 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
184 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
189 msg_Dbg( &sys.demuxer, "| - loading cues done." );
192 void matroska_segment_c::LoadTags( KaxTags *tags )
197 /* Master elements */
198 ep = new EbmlParser( &es, tags, &sys.demuxer );
200 while( ( el = ep->Get() ) != NULL )
202 if( MKV_IS_ID( el, KaxTag ) )
204 msg_Dbg( &sys.demuxer, "+ Tag" );
206 while( ( el = ep->Get() ) != NULL )
208 if( MKV_IS_ID( el, KaxTagTargets ) )
210 msg_Dbg( &sys.demuxer, "| + Targets" );
212 while( ( el = ep->Get() ) != NULL )
214 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
218 else if( MKV_IS_ID( el, KaxTagGeneral ) )
220 msg_Dbg( &sys.demuxer, "| + General" );
222 while( ( el = ep->Get() ) != NULL )
224 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
228 else if( MKV_IS_ID( el, KaxTagGenres ) )
230 msg_Dbg( &sys.demuxer, "| + Genres" );
232 while( ( el = ep->Get() ) != NULL )
234 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
238 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
240 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
242 while( ( el = ep->Get() ) != NULL )
244 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
248 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
250 msg_Dbg( &sys.demuxer, "| + Images Specific" );
252 while( ( el = ep->Get() ) != NULL )
254 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
258 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
260 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
262 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
264 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
266 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
268 msg_Dbg( &sys.demuxer, "| + Multi Date" );
270 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
272 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
274 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
276 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
278 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
280 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
282 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
284 msg_Dbg( &sys.demuxer, "| + Multi Title" );
288 msg_Dbg( &sys.demuxer, "| + LoadTag Unknown (%s)", typeid( *el ).name() );
295 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
300 msg_Dbg( &sys.demuxer, "loading tags done." );
303 /*****************************************************************************
305 *****************************************************************************/
306 void matroska_segment_c::InformationCreate( )
308 sys.meta = vlc_meta_New();
312 vlc_meta_SetTitle( sys.meta, psz_title );
316 vlc_meta_SetDate( sys.meta, psz_date_utc );
319 if( psz_segment_filename )
321 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
323 if( psz_muxing_application )
325 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
327 if( psz_writing_application )
329 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
332 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
334 // mkv_track_t *tk = tracks[i_track];
335 // vlc_meta_t *mtk = vlc_meta_New();
336 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
340 if( i_tags_position >= 0 )
344 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
354 /*****************************************************************************
356 *****************************************************************************/
358 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
360 #define idx p_indexes[i_index]
362 idx.i_block_number= -1;
363 idx.i_position = cluster->GetElementPosition();
368 if( i_index >= i_index_max )
371 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
377 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
382 for (size_t i=0; i<families.size(); i++)
384 for (size_t j=0; j<of_segment.families.size(); j++)
386 if ( *(families[i]) == *(of_segment.families[j]) )
394 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
398 if ( p_item_a == NULL || p_item_b == NULL )
401 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
402 if ( p_item_b->p_prev_segment_uid != NULL
403 && *p_tmp == *p_item_b->p_prev_segment_uid )
406 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
410 if ( p_item_b->p_segment_uid != NULL
411 && *p_tmp == *p_item_b->p_segment_uid )
414 if ( p_item_b->p_prev_segment_uid != NULL
415 && *p_tmp == *p_item_b->p_prev_segment_uid )
421 bool matroska_segment_c::Preload( )
426 EbmlElement *el = NULL;
428 ep->Reset( &sys.demuxer );
430 while( ( el = ep->Get() ) != NULL )
432 if( MKV_IS_ID( el, KaxSeekHead ) )
434 /* Multiple allowed */
435 /* We bail at 10, to prevent possible recursion */
436 msg_Dbg( &sys.demuxer, "| + Seek head" );
437 if( i_seekhead_count < 10 )
439 i_seekhead_position = (int64_t) es.I_O().getFilePointer();
440 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
443 else if( MKV_IS_ID( el, KaxInfo ) )
445 /* Multiple allowed, mandatory */
446 msg_Dbg( &sys.demuxer, "| + Information" );
447 if( i_info_position < 0 ) // FIXME
448 ParseInfo( static_cast<KaxInfo*>( el ) );
449 i_info_position = (int64_t) es.I_O().getFilePointer();
451 else if( MKV_IS_ID( el, KaxTracks ) )
453 /* Multiple allowed */
454 msg_Dbg( &sys.demuxer, "| + Tracks" );
455 if( i_tracks_position < 0 ) // FIXME
456 ParseTracks( static_cast<KaxTracks*>( el ) );
457 if ( tracks.size() == 0 )
459 msg_Err( &sys.demuxer, "No tracks supported" );
462 i_tracks_position = (int64_t) es.I_O().getFilePointer();
464 else if( MKV_IS_ID( el, KaxCues ) )
466 msg_Dbg( &sys.demuxer, "| + Cues" );
467 if( i_cues_position < 0 )
468 LoadCues( static_cast<KaxCues*>( el ) );
469 i_cues_position = (int64_t) es.I_O().getFilePointer();
471 else if( MKV_IS_ID( el, KaxCluster ) )
473 msg_Dbg( &sys.demuxer, "| + Cluster" );
475 cluster = (KaxCluster*)el;
477 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
481 /* stop pre-parsing the stream */
484 else if( MKV_IS_ID( el, KaxAttachments ) )
486 msg_Dbg( &sys.demuxer, "| + Attachments" );
487 if( i_attachments_position < 0 )
488 ParseAttachments( static_cast<KaxAttachments*>( el ) );
489 i_attachments_position = (int64_t) es.I_O().getFilePointer();
491 else if( MKV_IS_ID( el, KaxChapters ) )
493 msg_Dbg( &sys.demuxer, "| + Chapters" );
494 if( i_chapters_position < 0 )
495 ParseChapters( static_cast<KaxChapters*>( el ) );
496 i_chapters_position = (int64_t) es.I_O().getFilePointer();
498 else if( MKV_IS_ID( el, KaxTag ) )
500 msg_Dbg( &sys.demuxer, "| + Tags" );
501 if( i_tags_position < 0) // FIXME
502 ;//LoadTags( static_cast<KaxTags*>( el ) );
503 i_tags_position = (int64_t) es.I_O().getFilePointer();
506 msg_Dbg( &sys.demuxer, "| + Preload Unknown (%s)", typeid(*el).name() );
514 /* Here we try to load elements that were found in Seek Heads, but not yet parsed */
515 bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position )
517 int64_t i_sav_position = (int64_t)es.I_O().getFilePointer();
520 es.I_O().setFilePointer( i_element_position, seek_beginning );
521 el = es.FindNextID( ClassInfos, 0xFFFFFFFFL);
525 msg_Err( &sys.demuxer, "cannot load some cues/chapters/tags etc. (broken seekhead or file)" );
526 es.I_O().setFilePointer( i_sav_position, seek_beginning );
530 if( MKV_IS_ID( el, KaxSeekHead ) )
532 /* Multiple allowed */
533 msg_Dbg( &sys.demuxer, "| + Seek head" );
534 if( i_seekhead_count < 10 )
536 i_seekhead_position = i_element_position;
537 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
540 else if( MKV_IS_ID( el, KaxInfo ) ) // FIXME
542 /* Multiple allowed, mandatory */
543 msg_Dbg( &sys.demuxer, "| + Information" );
544 if( i_info_position < 0 )
545 ParseInfo( static_cast<KaxInfo*>( el ) );
546 i_info_position = i_element_position;
548 else if( MKV_IS_ID( el, KaxTracks ) ) // FIXME
550 /* Multiple allowed */
551 msg_Dbg( &sys.demuxer, "| + Tracks" );
552 if( i_tracks_position < 0 )
553 ParseTracks( static_cast<KaxTracks*>( el ) );
554 if ( tracks.size() == 0 )
556 msg_Err( &sys.demuxer, "No tracks supported" );
558 es.I_O().setFilePointer( i_sav_position, seek_beginning );
561 i_tracks_position = i_element_position;
563 else if( MKV_IS_ID( el, KaxCues ) )
565 msg_Dbg( &sys.demuxer, "| + Cues" );
566 if( i_cues_position < 0 )
567 LoadCues( static_cast<KaxCues*>( el ) );
568 i_cues_position = i_element_position;
570 else if( MKV_IS_ID( el, KaxAttachments ) )
572 msg_Dbg( &sys.demuxer, "| + Attachments" );
573 if( i_attachments_position < 0 )
574 ParseAttachments( static_cast<KaxAttachments*>( el ) );
575 i_attachments_position = i_element_position;
577 else if( MKV_IS_ID( el, KaxChapters ) )
579 msg_Dbg( &sys.demuxer, "| + Chapters" );
580 if( i_chapters_position < 0 )
581 ParseChapters( static_cast<KaxChapters*>( el ) );
582 i_chapters_position = i_element_position;
584 else if( MKV_IS_ID( el, KaxTag ) ) // FIXME
586 msg_Dbg( &sys.demuxer, "| + Tags" );
587 if( i_tags_position < 0 )
588 ;//LoadTags( static_cast<KaxTags*>( el ) );
589 i_tags_position = i_element_position;
593 msg_Dbg( &sys.demuxer, "| + LoadSeekHeadItem Unknown (%s)", typeid(*el).name() );
597 es.I_O().setFilePointer( i_sav_position, seek_beginning );
601 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
602 const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
606 for( i_track = 0; i_track < tracks.size(); i_track++ )
608 const mkv_track_t *tk = tracks[i_track];
610 if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
611 ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
617 if( i_track >= tracks.size() )
625 bool matroska_segment_c::Select( mtime_t i_start_time )
630 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
631 sys.b_pci_packet_set = false;
633 for( i_track = 0; i_track < tracks.size(); i_track++ )
635 mkv_track_t *p_tk = tracks[i_track];
636 es_format_t *p_fmt = &p_tk->fmt;
638 if( tracks[i_track]->fmt.i_cat == UNKNOWN_ES )
640 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, tracks[i_track]->i_number );
641 tracks[i_track]->p_es = NULL;
645 if( !strcmp( tracks[i_track]->psz_codec, "V_MS/VFW/FOURCC" ) )
647 if( tracks[i_track]->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
649 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
650 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
654 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tracks[i_track]->p_extra_data;
656 tracks[i_track]->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
657 tracks[i_track]->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
658 tracks[i_track]->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
660 tracks[i_track]->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
661 if( tracks[i_track]->fmt.i_extra > 0 )
663 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
664 memcpy( tracks[i_track]->fmt.p_extra, &p_bih[1], tracks[i_track]->fmt.i_extra );
668 else if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG1" ) ||
669 !strcmp( tracks[i_track]->psz_codec, "V_MPEG2" ) )
671 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
673 else if( !strncmp( tracks[i_track]->psz_codec, "V_THEORA", 8 ) )
675 uint8_t *p_data = tracks[i_track]->p_extra_data;
676 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
677 if( tracks[i_track]->i_extra_data >= 4 ) {
678 if( p_data[0] == 2 ) {
680 int i_size1 = 0, i_size2 = 0;
682 /* read size of first header packet */
683 while( *p_data == 0xFF &&
684 i < tracks[i_track]->i_extra_data )
693 msg_Dbg( &sys.demuxer, "first theora header size %d", i_size1 );
694 /* read size of second header packet */
695 while( *p_data == 0xFF &&
696 i < tracks[i_track]->i_extra_data )
705 int i_size3 = tracks[i_track]->i_extra_data - i - i_size1
707 msg_Dbg( &sys.demuxer, "second theora header size %d", i_size2 );
708 msg_Dbg( &sys.demuxer, "third theora header size %d", i_size3 );
709 tracks[i_track]->fmt.i_extra = i_size1 + i_size2 + i_size3
711 if( i_size1 > 0 && i_size2 > 0 && i_size3 > 0 ) {
712 tracks[i_track]->fmt.p_extra =
713 malloc( tracks[i_track]->fmt.i_extra );
714 uint8_t *p_out = (uint8_t*)tracks[i_track]->fmt.p_extra;
715 *p_out++ = (i_size1>>8) & 0xFF;
716 *p_out++ = i_size1 & 0xFF;
717 memcpy( p_out, p_data, i_size1 );
721 *p_out++ = (i_size2>>8) & 0xFF;
722 *p_out++ = i_size2 & 0xFF;
723 memcpy( p_out, p_data, i_size2 );
727 *p_out++ = (i_size3>>8) & 0xFF;
728 *p_out++ = i_size3 & 0xFF;
729 memcpy( p_out, p_data, i_size3 );
735 msg_Err( &sys.demuxer, "inconsistant theora extradata" );
739 msg_Err( &sys.demuxer, "Wrong number of ogg packets with theora headers (%d)", p_data[0] + 1 );
743 else if( !strncmp( tracks[i_track]->psz_codec, "V_REAL/RV", 9 ) )
745 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
746 p_fmt->i_codec = VLC_FOURCC( 'R', 'V', '1', '0' );
747 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
748 p_fmt->i_codec = VLC_FOURCC( 'R', 'V', '2', '0' );
749 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
750 p_fmt->i_codec = VLC_FOURCC( 'R', 'V', '3', '0' );
751 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
752 p_fmt->i_codec = VLC_FOURCC( 'R', 'V', '4', '0' );
754 if( p_tk->i_extra_data > 26 )
756 p_fmt->p_extra = malloc( p_tk->i_extra_data - 26 );
759 p_fmt->i_extra = p_tk->i_extra_data - 26;
760 memcpy( p_fmt->p_extra, &p_tk->p_extra_data[26], p_fmt->i_extra );
764 else if( !strncmp( tracks[i_track]->psz_codec, "V_DIRAC", 7 ) )
766 tracks[i_track]->fmt.i_codec = VLC_FOURCC('d', 'r', 'a', 'c' );
768 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4", 7 ) )
770 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/MS/V3" ) )
772 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
774 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO", 11 ) )
776 /* A MPEG 4 codec, SP, ASP, AP or AVC */
777 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO/AVC" ) )
778 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
780 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
781 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
782 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
783 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
786 else if( !strcmp( tracks[i_track]->psz_codec, "V_QUICKTIME" ) )
788 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
789 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
790 tracks[i_track]->p_extra_data,
791 tracks[i_track]->i_extra_data,
793 if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
794 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
796 tracks[i_track]->fmt.i_codec = p_box->i_type;
797 tracks[i_track]->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
798 tracks[i_track]->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
799 tracks[i_track]->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
800 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
801 memcpy( tracks[i_track]->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tracks[i_track]->fmt.i_extra );
802 MP4_FreeBox_sample_vide( p_box );
808 stream_Delete( p_mp4_stream );
810 else if( !strcmp( tracks[i_track]->psz_codec, "A_MS/ACM" ) )
812 if( tracks[i_track]->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
814 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
815 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
819 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tracks[i_track]->p_extra_data;
821 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tracks[i_track]->fmt.i_codec, NULL );
823 tracks[i_track]->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
824 tracks[i_track]->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
825 tracks[i_track]->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
826 tracks[i_track]->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
827 tracks[i_track]->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
829 tracks[i_track]->fmt.i_extra = GetWLE( &p_wf->cbSize );
830 if( tracks[i_track]->fmt.i_extra > 0 )
832 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
833 memcpy( tracks[i_track]->fmt.p_extra, &p_wf[1], tracks[i_track]->fmt.i_extra );
837 else if( !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L3" ) ||
838 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L2" ) ||
839 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L1" ) )
841 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
843 else if( !strcmp( tracks[i_track]->psz_codec, "A_AC3" ) )
845 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
847 else if( !strcmp( tracks[i_track]->psz_codec, "A_EAC3" ) )
849 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'e', 'a', 'c', '3' );
851 else if( !strcmp( tracks[i_track]->psz_codec, "A_DTS" ) )
853 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
855 else if( !strcmp( tracks[i_track]->psz_codec, "A_FLAC" ) )
857 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
858 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
859 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
860 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
862 else if( !strcmp( tracks[i_track]->psz_codec, "A_VORBIS" ) )
864 int i, i_offset = 1, i_size[3], i_extra;
867 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
869 /* Split the 3 headers */
870 if( tracks[i_track]->p_extra_data[0] != 0x02 )
871 msg_Err( &sys.demuxer, "invalid vorbis header" );
873 for( i = 0; i < 2; i++ )
876 while( i_offset < tracks[i_track]->i_extra_data )
878 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
879 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
883 i_size[0] = __MIN(i_size[0], tracks[i_track]->i_extra_data - i_offset);
884 i_size[1] = __MIN(i_size[1], tracks[i_track]->i_extra_data -i_offset -i_size[0]);
885 i_size[2] = tracks[i_track]->i_extra_data - i_offset - i_size[0] - i_size[1];
887 tracks[i_track]->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
888 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
889 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra; i_extra = 0;
890 for( i = 0; i < 3; i++ )
892 *(p_extra++) = i_size[i] >> 8;
893 *(p_extra++) = i_size[i] & 0xFF;
894 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra,
896 p_extra += i_size[i];
897 i_extra += i_size[i];
900 else if( !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
901 !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
903 int i_profile, i_srate, sbr = 0;
904 static const unsigned int i_sample_rates[] =
906 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
907 16000, 12000, 11025, 8000, 7350, 0, 0, 0
910 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
911 /* create data for faad (MP4DecSpecificDescrTag)*/
913 if( !strcmp( &tracks[i_track]->psz_codec[12], "MAIN" ) )
917 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC" ) )
921 else if( !strcmp( &tracks[i_track]->psz_codec[12], "SSR" ) )
925 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC/SBR" ) )
935 for( i_srate = 0; i_srate < 13; i_srate++ )
937 if( i_sample_rates[i_srate] == tracks[i_track]->i_original_rate )
942 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
944 tracks[i_track]->fmt.i_extra = sbr ? 5 : 2;
945 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
946 ((uint8_t*)tracks[i_track]->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
947 ((uint8_t*)tracks[i_track]->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tracks[i_track]->fmt.audio.i_channels << 3);
950 int syncExtensionType = 0x2B7;
952 for (iDSRI=0; iDSRI<13; iDSRI++)
953 if( i_sample_rates[iDSRI] == tracks[i_track]->fmt.audio.i_rate )
955 ((uint8_t*)tracks[i_track]->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
956 ((uint8_t*)tracks[i_track]->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
957 ((uint8_t*)tracks[i_track]->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
960 else if( !strcmp( tracks[i_track]->psz_codec, "A_AAC" ) )
962 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
963 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
964 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
965 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
967 else if( !strcmp( tracks[i_track]->psz_codec, "A_WAVPACK4" ) )
969 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'W', 'V', 'P', 'K' );
970 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
971 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
972 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
974 else if( !strcmp( tracks[i_track]->psz_codec, "A_TTA1" ) )
976 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'T', 'T', 'A', '1' );
977 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
978 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
979 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
981 else if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) ||
982 !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/LIT" ) ||
983 !strcmp( tracks[i_track]->psz_codec, "A_PCM/FLOAT/IEEE" ) )
985 if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) )
987 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
991 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
993 tracks[i_track]->fmt.audio.i_blockalign = ( tracks[i_track]->fmt.audio.i_bitspersample + 7 ) / 8 * tracks[i_track]->fmt.audio.i_channels;
995 /* disabled due to the potential "S_KATE" namespace issue */
996 else if( !strcmp( tracks[i_track]->psz_codec, "S_KATE" ) )
998 int i, i_offset = 1, *i_size, i_extra, num_headers, size_so_far;
1001 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'k', 'a', 't', 'e' );
1002 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
1004 /* Recover the number of headers to expect */
1005 num_headers = tracks[i_track]->p_extra_data[0]+1;
1006 msg_Dbg( &sys.demuxer, "kate in mkv detected: %d headers in %u bytes",
1007 num_headers, tracks[i_track]->i_extra_data);
1009 /* this won't overflow the stack as is can allocate only 1020 bytes max */
1010 i_size = (int*)alloca(num_headers*sizeof(int));
1012 /* Split the headers */
1014 for( i = 0; i < num_headers-1; i++ )
1017 while( i_offset < tracks[i_track]->i_extra_data )
1019 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
1020 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
1022 msg_Dbg( &sys.demuxer, "kate header %d is %d bytes", i, i_size[i]);
1023 size_so_far += i_size[i];
1025 i_size[num_headers-1] = tracks[i_track]->i_extra_data - (size_so_far+i_offset);
1026 msg_Dbg( &sys.demuxer, "kate last header (%d) is %d bytes", num_headers-1, i_size[num_headers-1]);
1028 tracks[i_track]->fmt.i_extra = 1 + num_headers * 2 + size_so_far + i_size[num_headers-1];
1029 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
1031 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra;
1033 *(p_extra++) = num_headers;
1035 for( i = 0; i < num_headers; i++ )
1037 *(p_extra++) = i_size[i] >> 8;
1038 *(p_extra++) = i_size[i] & 0xFF;
1039 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra-1,
1041 p_extra += i_size[i];
1042 i_extra += i_size[i];
1045 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASCII" ) )
1047 p_fmt->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1048 p_fmt->subs.psz_encoding = NULL; /* Is there a place where it is stored ? */
1050 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/UTF8" ) )
1052 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1053 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
1055 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/USF" ) )
1057 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1058 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
1059 if( tracks[i_track]->i_extra_data )
1061 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
1062 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
1063 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
1066 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/SSA" ) ||
1067 !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASS" ) ||
1068 !strcmp( tracks[i_track]->psz_codec, "S_SSA" ) ||
1069 !strcmp( tracks[i_track]->psz_codec, "S_ASS" ))
1071 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1072 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
1073 if( tracks[i_track]->i_extra_data )
1075 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
1076 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
1077 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
1080 else if( !strcmp( tracks[i_track]->psz_codec, "S_VOBSUB" ) )
1082 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1083 if( tracks[i_track]->i_extra_data )
1086 char *p_buf = (char *)malloc( tracks[i_track]->i_extra_data + 1);
1087 memcpy( p_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
1088 p_buf[tracks[i_track]->i_extra_data] = '\0';
1090 p_start = strstr( p_buf, "size:" );
1091 if( sscanf( p_start, "size: %dx%d",
1092 &tracks[i_track]->fmt.subs.spu.i_original_frame_width, &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) == 2 )
1094 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 );
1098 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1103 else if( !strcmp( tracks[i_track]->psz_codec, "B_VOBBTN" ) )
1105 tracks[i_track]->fmt.i_cat = NAV_ES;
1108 else if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1110 p_fmt->i_codec = VLC_FOURCC( '1', '4', '_', '4');
1111 p_fmt->audio.i_channels = 1;
1112 p_fmt->audio.i_rate = 8000;
1113 p_fmt->audio.i_blockalign = 0x14;
1117 msg_Err( &sys.demuxer, "unknown codec id=`%s'", tracks[i_track]->psz_codec );
1118 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1120 if( tracks[i_track]->b_default )
1122 tracks[i_track]->fmt.i_priority = 1000;
1125 tracks[i_track]->p_es = es_out_Add( sys.demuxer.out, &tracks[i_track]->fmt );
1127 /* Turn on a subtitles track if it has been flagged as default -
1128 * but only do this if no subtitles track has already been engaged,
1129 * either by an earlier 'default track' (??) or by default
1130 * language choice behaviour.
1132 if( tracks[i_track]->b_default )
1134 es_out_Control( sys.demuxer.out,
1135 ES_OUT_SET_ES_DEFAULT,
1136 tracks[i_track]->p_es );
1139 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
1141 sys.i_start_pts = i_start_time;
1142 // reset the stream reading to the first cluster of the segment used
1143 es.I_O().setFilePointer( i_start_pos );
1146 ep = new EbmlParser( &es, segment, &sys.demuxer );
1151 void matroska_segment_c::UnSelect( )
1155 for( i_track = 0; i_track < tracks.size(); i_track++ )
1157 if ( tracks[i_track]->p_es != NULL )
1159 // es_format_Clean( &tracks[i_track]->fmt );
1160 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
1161 tracks[i_track]->p_es = NULL;
1168 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1170 pp_simpleblock = NULL;
1177 EbmlElement *el = NULL;
1181 return VLC_EGENERIC;
1183 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1185 /* Check blocks validity to protect againts broken files */
1186 if( BlockFindTrackIndex( NULL, pp_block , pp_simpleblock ) )
1189 pp_simpleblock = NULL;
1194 /* update the index */
1195 #define idx p_indexes[i_index - 1]
1196 if( i_index > 0 && idx.i_time == -1 )
1198 if ( pp_simpleblock != NULL )
1199 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1201 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1202 idx.b_key = *pi_ref1 == 0 ? true : false;
1208 i_level = ep->GetLevel();
1217 msg_Warn( &sys.demuxer, "EOF" );
1218 return VLC_EGENERIC;
1221 /* Verify that we are still inside our cluster
1222 * It can happens whith broken files and when seeking
1226 if( cluster && !ep->IsTopPresent( cluster ) )
1228 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1239 if( MKV_IS_ID( el, KaxCluster ) )
1241 cluster = (KaxCluster*)el;
1242 i_cluster_pos = cluster->GetElementPosition();
1244 /* add it to the index */
1246 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1248 IndexAppendCluster( cluster );
1251 // reset silent tracks
1252 for (size_t i=0; i<tracks.size(); i++)
1254 tracks[i]->b_silent = false;
1259 else if( MKV_IS_ID( el, KaxCues ) )
1261 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1262 return VLC_EGENERIC;
1266 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1270 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1272 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1274 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1275 cluster->InitTimecode( uint64( ctc ), i_timescale );
1277 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1281 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1283 i_block_pos = el->GetElementPosition();
1286 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1288 pp_simpleblock = (KaxSimpleBlock*)el;
1290 pp_simpleblock->ReadData( es.I_O() );
1291 pp_simpleblock->SetParent( *cluster );
1295 if( MKV_IS_ID( el, KaxBlock ) )
1297 pp_block = (KaxBlock*)el;
1299 pp_block->ReadData( es.I_O() );
1300 pp_block->SetParent( *cluster );
1304 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1306 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1308 dur.ReadData( es.I_O() );
1309 *pi_duration = uint64( dur );
1311 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1313 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1315 ref.ReadData( es.I_O() );
1318 *pi_ref1 = int64( ref ) * cluster->GlobalTimecodeScale();
1320 else if( *pi_ref2 == 0 )
1322 *pi_ref2 = int64( ref ) * cluster->GlobalTimecodeScale();
1325 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1327 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1328 track_num.ReadData( es.I_O() );
1330 for (size_t i=0; i<tracks.size(); i++)
1332 if ( tracks[i]->i_number == uint32(track_num))
1334 tracks[i]->b_silent = true;
1341 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1342 return VLC_EGENERIC;