1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2010 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Steve Lhomme <steve.lhomme@free.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "matroska_segment.hpp"
27 #include "chapters.hpp"
32 #include "../vobsub.h"
35 #include <vlc_codecs.h>
37 /* GetFourCC helper */
38 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
39 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
41 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
45 matroska_segment_c::~matroska_segment_c()
47 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
49 delete tracks[i_track]->p_compression_data;
50 es_format_Clean( &tracks[i_track]->fmt );
51 free( tracks[i_track]->p_extra_data );
52 free( tracks[i_track]->psz_codec );
53 delete tracks[i_track];
56 free( psz_writing_application );
57 free( psz_muxing_application );
58 free( psz_segment_filename );
66 delete p_prev_segment_uid;
67 delete p_next_segment_uid;
69 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
70 while ( index != stored_editions.end() )
75 std::vector<chapter_translation_c*>::iterator indext = translations.begin();
76 while ( indext != translations.end() )
81 std::vector<KaxSegmentFamily*>::iterator indexf = families.begin();
82 while ( indexf != families.end() )
90 /*****************************************************************************
92 *****************************************************************************
93 * * LoadCues : load the cues element and update index
94 * * LoadTags : load ... the tags element
95 * * InformationCreate : create all information, load tags if present
96 *****************************************************************************/
97 void matroska_segment_c::LoadCues( KaxCues *cues )
104 msg_Err( &sys.demuxer, "There can be only 1 Cues per section." );
108 ep = new EbmlParser( &es, cues, &sys.demuxer );
109 while( ( el = ep->Get() ) != NULL )
111 if( MKV_IS_ID( el, KaxCuePoint ) )
113 #define idx p_indexes[i_index]
116 idx.i_block_number= -1;
122 while( ( el = ep->Get() ) != NULL )
124 if( MKV_IS_ID( el, KaxCueTime ) )
126 KaxCueTime &ctime = *(KaxCueTime*)el;
128 ctime.ReadData( es.I_O() );
130 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
132 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
135 while( ( el = ep->Get() ) != NULL )
137 if( MKV_IS_ID( el, KaxCueTrack ) )
139 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
141 ctrack.ReadData( es.I_O() );
142 idx.i_track = uint16( ctrack );
144 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
146 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
148 ccpos.ReadData( es.I_O() );
149 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
151 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
153 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
155 cbnum.ReadData( es.I_O() );
156 idx.i_block_number = uint32( cbnum );
160 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
167 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
173 msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
174 " track=%d bnum=%d", idx.i_time, idx.i_position,
175 idx.i_track, idx.i_block_number );
179 if( i_index >= i_index_max )
182 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
183 sizeof( mkv_index_t ) * i_index_max );
189 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
194 msg_Dbg( &sys.demuxer, "| - loading cues done." );
198 #define PARSE_TAG( type ) \
200 msg_Dbg( &sys.demuxer, "| + " type ); \
202 while( ( el = ep->Get() ) != NULL ) \
204 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() ); \
206 ep->Up(); } while( 0 )
208 void matroska_segment_c::LoadTags( KaxTags *tags )
210 /* Master elements */
211 EbmlParser *ep = new EbmlParser( &es, tags, &sys.demuxer );
214 while( ( el = ep->Get() ) != NULL )
216 if( MKV_IS_ID( el, KaxTag ) )
218 msg_Dbg( &sys.demuxer, "+ Tag" );
220 while( ( el = ep->Get() ) != NULL )
222 if( MKV_IS_ID( el, KaxTagTargets ) )
223 PARSE_TAG( "Targets" );
224 else if( MKV_IS_ID( el, KaxTagGeneral ) )
225 PARSE_TAG( "General" );
226 else if( MKV_IS_ID( el, KaxTagGenres ) )
227 PARSE_TAG( "Genres" );
228 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
229 PARSE_TAG( "Audio Specific" );
230 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
231 PARSE_TAG( "Images Specific" );
232 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
234 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
236 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
238 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
240 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
242 msg_Dbg( &sys.demuxer, "| + Multi Date" );
244 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
246 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
248 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
250 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
252 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
254 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
256 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
258 msg_Dbg( &sys.demuxer, "| + Multi Title" );
262 msg_Dbg( &sys.demuxer, "| + LoadTag Unknown (%s)", typeid( *el ).name() );
269 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
274 msg_Dbg( &sys.demuxer, "loading tags done." );
278 /*****************************************************************************
280 *****************************************************************************/
281 void matroska_segment_c::InformationCreate( )
283 sys.meta = vlc_meta_New();
287 vlc_meta_SetTitle( sys.meta, psz_title );
291 vlc_meta_SetDate( sys.meta, psz_date_utc );
294 if( psz_segment_filename )
296 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
298 if( psz_muxing_application )
300 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
302 if( psz_writing_application )
304 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
307 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
309 // mkv_track_t *tk = tracks[i_track];
310 // vlc_meta_t *mtk = vlc_meta_New();
311 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
315 if( i_tags_position >= 0 )
319 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
329 /*****************************************************************************
331 *****************************************************************************/
333 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
335 #define idx p_indexes[i_index]
337 idx.i_block_number= -1;
338 idx.i_position = cluster->GetElementPosition();
343 if( i_index >= i_index_max )
346 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
347 sizeof( mkv_index_t ) * i_index_max );
352 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
357 for (size_t i=0; i<families.size(); i++)
359 for (size_t j=0; j<of_segment.families.size(); j++)
361 if ( *(families[i]) == *(of_segment.families[j]) )
369 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
373 if ( p_item_a == NULL || p_item_b == NULL )
376 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
377 if ( p_item_b->p_prev_segment_uid != NULL
378 && *p_tmp == *p_item_b->p_prev_segment_uid )
381 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
385 if ( p_item_b->p_segment_uid != NULL
386 && *p_tmp == *p_item_b->p_segment_uid )
389 if ( p_item_b->p_prev_segment_uid != NULL
390 && *p_tmp == *p_item_b->p_prev_segment_uid )
396 bool matroska_segment_c::Preload( )
401 EbmlElement *el = NULL;
403 ep->Reset( &sys.demuxer );
405 while( ( el = ep->Get() ) != NULL )
407 if( MKV_IS_ID( el, KaxSeekHead ) )
409 /* Multiple allowed */
410 /* We bail at 10, to prevent possible recursion */
411 msg_Dbg( &sys.demuxer, "| + Seek head" );
412 if( i_seekhead_count < 10 )
414 i_seekhead_position = (int64_t) es.I_O().getFilePointer();
415 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
418 else if( MKV_IS_ID( el, KaxInfo ) )
420 /* Multiple allowed, mandatory */
421 msg_Dbg( &sys.demuxer, "| + Information" );
422 if( i_info_position < 0 ) // FIXME
423 ParseInfo( static_cast<KaxInfo*>( el ) );
424 i_info_position = (int64_t) es.I_O().getFilePointer();
426 else if( MKV_IS_ID( el, KaxTracks ) )
428 /* Multiple allowed */
429 msg_Dbg( &sys.demuxer, "| + Tracks" );
430 if( i_tracks_position < 0 ) // FIXME
431 ParseTracks( static_cast<KaxTracks*>( el ) );
432 if ( tracks.size() == 0 )
434 msg_Err( &sys.demuxer, "No tracks supported" );
437 i_tracks_position = (int64_t) es.I_O().getFilePointer();
439 else if( MKV_IS_ID( el, KaxCues ) )
441 msg_Dbg( &sys.demuxer, "| + Cues" );
442 if( i_cues_position < 0 )
443 LoadCues( static_cast<KaxCues*>( el ) );
444 i_cues_position = (int64_t) es.I_O().getFilePointer();
446 else if( MKV_IS_ID( el, KaxCluster ) )
448 msg_Dbg( &sys.demuxer, "| + Cluster" );
450 cluster = (KaxCluster*)el;
452 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
456 /* stop pre-parsing the stream */
459 else if( MKV_IS_ID( el, KaxAttachments ) )
461 msg_Dbg( &sys.demuxer, "| + Attachments" );
462 if( i_attachments_position < 0 )
463 ParseAttachments( static_cast<KaxAttachments*>( el ) );
464 i_attachments_position = (int64_t) es.I_O().getFilePointer();
466 else if( MKV_IS_ID( el, KaxChapters ) )
468 msg_Dbg( &sys.demuxer, "| + Chapters" );
469 if( i_chapters_position < 0 )
470 ParseChapters( static_cast<KaxChapters*>( el ) );
471 i_chapters_position = (int64_t) es.I_O().getFilePointer();
473 else if( MKV_IS_ID( el, KaxTag ) )
475 msg_Dbg( &sys.demuxer, "| + Tags" );
476 if( i_tags_position < 0) // FIXME
477 ;//LoadTags( static_cast<KaxTags*>( el ) );
478 i_tags_position = (int64_t) es.I_O().getFilePointer();
480 else if( MKV_IS_ID( el, EbmlVoid ) )
481 msg_Dbg( &sys.demuxer, "| + Void" );
483 msg_Dbg( &sys.demuxer, "| + Preload Unknown (%s)", typeid(*el).name() );
491 /* Here we try to load elements that were found in Seek Heads, but not yet parsed */
492 bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position )
494 int64_t i_sav_position = (int64_t)es.I_O().getFilePointer();
497 es.I_O().setFilePointer( i_element_position, seek_beginning );
498 el = es.FindNextID( ClassInfos, 0xFFFFFFFFL);
502 msg_Err( &sys.demuxer, "cannot load some cues/chapters/tags etc. (broken seekhead or file)" );
503 es.I_O().setFilePointer( i_sav_position, seek_beginning );
507 if( MKV_IS_ID( el, KaxSeekHead ) )
509 /* Multiple allowed */
510 msg_Dbg( &sys.demuxer, "| + Seek head" );
511 if( i_seekhead_count < 10 )
513 i_seekhead_position = i_element_position;
514 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
517 else if( MKV_IS_ID( el, KaxInfo ) ) // FIXME
519 /* Multiple allowed, mandatory */
520 msg_Dbg( &sys.demuxer, "| + Information" );
521 if( i_info_position < 0 )
522 ParseInfo( static_cast<KaxInfo*>( el ) );
523 i_info_position = i_element_position;
525 else if( MKV_IS_ID( el, KaxTracks ) ) // FIXME
527 /* Multiple allowed */
528 msg_Dbg( &sys.demuxer, "| + Tracks" );
529 if( i_tracks_position < 0 )
530 ParseTracks( static_cast<KaxTracks*>( el ) );
531 if ( tracks.size() == 0 )
533 msg_Err( &sys.demuxer, "No tracks supported" );
535 es.I_O().setFilePointer( i_sav_position, seek_beginning );
538 i_tracks_position = i_element_position;
540 else if( MKV_IS_ID( el, KaxCues ) )
542 msg_Dbg( &sys.demuxer, "| + Cues" );
543 if( i_cues_position < 0 )
544 LoadCues( static_cast<KaxCues*>( el ) );
545 i_cues_position = i_element_position;
547 else if( MKV_IS_ID( el, KaxAttachments ) )
549 msg_Dbg( &sys.demuxer, "| + Attachments" );
550 if( i_attachments_position < 0 )
551 ParseAttachments( static_cast<KaxAttachments*>( el ) );
552 i_attachments_position = i_element_position;
554 else if( MKV_IS_ID( el, KaxChapters ) )
556 msg_Dbg( &sys.demuxer, "| + Chapters" );
557 if( i_chapters_position < 0 )
558 ParseChapters( static_cast<KaxChapters*>( el ) );
559 i_chapters_position = i_element_position;
561 else if( MKV_IS_ID( el, KaxTag ) ) // FIXME
563 msg_Dbg( &sys.demuxer, "| + Tags" );
564 if( i_tags_position < 0 )
565 ;//LoadTags( static_cast<KaxTags*>( el ) );
566 i_tags_position = i_element_position;
570 msg_Dbg( &sys.demuxer, "| + LoadSeekHeadItem Unknown (%s)", typeid(*el).name() );
574 es.I_O().setFilePointer( i_sav_position, seek_beginning );
578 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_global_position )
581 KaxSimpleBlock *simpleblock;
582 int i_track_skipping;
583 int64_t i_block_duration;
585 int64_t i_seek_position = i_start_pos;
586 int64_t i_seek_time = i_start_time;
588 if( i_global_position >= 0 )
590 /* Special case for seeking in files with no cues */
591 EbmlElement *el = NULL;
592 es.I_O().setFilePointer( i_start_pos, seek_beginning );
594 ep = new EbmlParser( &es, segment, &sys.demuxer );
597 while( ( el = ep->Get() ) != NULL )
599 if( MKV_IS_ID( el, KaxCluster ) )
601 cluster = (KaxCluster *)el;
602 i_cluster_pos = cluster->GetElementPosition();
604 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
606 IndexAppendCluster( cluster );
608 if( es.I_O().getFilePointer() >= i_global_position )
611 msg_Dbg( &sys.demuxer, "we found a cluster that is in the neighbourhood" );
616 msg_Err( &sys.demuxer, "This file has no cues, and we were unable to seek to the requested position by parsing." );
624 for( ; i_idx < i_index; i_idx++ )
626 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
637 i_seek_position = p_indexes[i_idx].i_position;
638 i_seek_time = p_indexes[i_idx].i_time;
641 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
642 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
644 es.I_O().setFilePointer( i_seek_position, seek_beginning );
647 ep = new EbmlParser( &es, segment, &sys.demuxer );
650 sys.i_start_pts = i_date;
652 /* now parse until key frame */
653 i_track_skipping = 0;
654 for( i_track = 0; i_track < tracks.size(); i_track++ )
656 if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
658 tracks[i_track]->b_search_keyframe = true;
662 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
664 while( i_track_skipping > 0 )
667 bool b_discardable_picture;
668 if( BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
670 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
675 for( i_track = 0; i_track < tracks.size(); i_track++ )
677 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
678 (block && tracks[i_track]->i_number == block->TrackNum()) )
685 sys.i_pts = (sys.i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
687 sys.i_pts = (sys.i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
689 if( i_track < tracks.size() )
691 if( sys.i_pts > sys.i_start_pts )
693 cluster = static_cast<KaxCluster*>(ep->UnGet( i_block_pos, i_cluster_pos ));
694 i_track_skipping = 0;
696 else if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
698 if( b_key_picture && tracks[i_track]->b_search_keyframe )
700 tracks[i_track]->b_search_keyframe = false;
703 if( !tracks[i_track]->b_search_keyframe )
705 BlockDecode( &sys.demuxer, block, simpleblock, sys.i_pts, 0, b_key_picture || b_discardable_picture );
713 /* FIXME current ES_OUT_SET_NEXT_DISPLAY_TIME does not work that well if
714 * the delay is too high. */
715 if( sys.i_pts + 500*1000 < sys.i_start_pts )
717 sys.i_start_pts = sys.i_pts;
719 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, sys.i_start_pts );
723 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
724 const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
727 for( i_track = 0; i_track < tracks.size(); i_track++ )
729 const mkv_track_t *tk = tracks[i_track];
731 if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
732 ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
738 if( i_track >= tracks.size() )
746 static inline void fill_extra_data( mkv_track_t *p_tk )
748 p_tk->fmt.i_extra = p_tk->i_extra_data;
749 p_tk->fmt.p_extra = xmalloc( p_tk->i_extra_data );
750 memcpy( p_tk->fmt.p_extra, p_tk->p_extra_data, p_tk->i_extra_data );
753 bool matroska_segment_c::Select( mtime_t i_start_time )
756 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
757 sys.b_pci_packet_set = false;
759 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
761 mkv_track_t *p_tk = tracks[i_track];
762 es_format_t *p_fmt = &p_tk->fmt;
764 if( p_fmt->i_cat == UNKNOWN_ES || !p_tk->psz_codec )
766 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, p_tk->i_number );
771 if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
773 if( p_tk->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
775 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
776 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
780 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)p_tk->p_extra_data;
782 p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
783 p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
784 p_tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
786 p_tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
787 if( p_tk->fmt.i_extra > 0 )
789 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
790 memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
793 p_tk->b_dts_only = true;
795 else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
796 !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
798 p_tk->fmt.i_codec = VLC_CODEC_MPGV;
799 if( p_tk->i_extra_data )
800 fill_extra_data( p_tk );
802 else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
804 p_tk->fmt.i_codec = VLC_CODEC_THEORA;
805 fill_extra_data( p_tk );
806 p_tk->b_pts_only = true;
808 else if( !strncmp( p_tk->psz_codec, "V_REAL/RV", 9 ) )
810 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
811 p_fmt->i_codec = VLC_CODEC_RV10;
812 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
813 p_fmt->i_codec = VLC_CODEC_RV20;
814 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
815 p_fmt->i_codec = VLC_CODEC_RV30;
816 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
817 p_fmt->i_codec = VLC_CODEC_RV40;
819 if( p_tk->i_extra_data > 26 )
821 p_fmt->p_extra = malloc( p_tk->i_extra_data - 26 );
824 p_fmt->i_extra = p_tk->i_extra_data - 26;
825 memcpy( p_fmt->p_extra, &p_tk->p_extra_data[26], p_fmt->i_extra );
828 p_tk->b_dts_only = true;
830 else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
832 p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
834 else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
836 p_tk->fmt.i_codec = VLC_CODEC_VP8;
837 p_tk->b_pts_only = true;
839 else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
841 if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
843 p_tk->fmt.i_codec = VLC_CODEC_DIV3;
845 else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
847 /* A MPEG 4 codec, SP, ASP, AP or AVC */
848 if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
849 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
851 p_tk->fmt.i_codec = VLC_CODEC_MP4V;
852 fill_extra_data( p_tk );
855 else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
857 MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
858 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
862 if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
863 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
865 p_tk->fmt.i_codec = p_box->i_type;
866 p_tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
867 p_tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
868 p_tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
869 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
870 memcpy( p_tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, p_tk->fmt.i_extra );
871 MP4_FreeBox_sample_vide( p_box );
877 stream_Delete( p_mp4_stream );
879 else if( !strcmp( p_tk->psz_codec, "A_MS/ACM" ) )
881 if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
883 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
884 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
888 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
890 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
892 p_tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
893 p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
894 p_tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
895 p_tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
896 p_tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
898 p_tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
899 if( p_tk->fmt.i_extra > 0 )
901 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
902 memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
906 else if( !strcmp( p_tk->psz_codec, "A_MPEG/L3" ) ||
907 !strcmp( p_tk->psz_codec, "A_MPEG/L2" ) ||
908 !strcmp( p_tk->psz_codec, "A_MPEG/L1" ) )
910 p_tk->fmt.i_codec = VLC_CODEC_MPGA;
912 else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
914 p_tk->fmt.i_codec = VLC_CODEC_A52;
916 else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
918 p_tk->fmt.i_codec = VLC_CODEC_EAC3;
920 else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
922 p_tk->fmt.i_codec = VLC_CODEC_DTS;
924 else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
926 p_tk->fmt.i_codec = VLC_CODEC_MLP;
928 else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
930 /* FIXME when more samples arrive */
931 p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
932 p_fmt->b_packetized = false;
934 else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
936 p_tk->fmt.i_codec = VLC_CODEC_FLAC;
937 fill_extra_data( p_tk );
939 else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
941 p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
942 fill_extra_data( p_tk );
944 else if( !strncmp( p_tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
945 !strncmp( p_tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
947 int i_profile, i_srate, sbr = 0;
948 static const unsigned int i_sample_rates[] =
950 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
951 16000, 12000, 11025, 8000, 7350, 0, 0, 0
954 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
955 /* create data for faad (MP4DecSpecificDescrTag)*/
957 if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
961 else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
965 else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
969 else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
979 for( i_srate = 0; i_srate < 13; i_srate++ )
981 if( i_sample_rates[i_srate] == p_tk->i_original_rate )
986 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
988 p_tk->fmt.i_extra = sbr ? 5 : 2;
989 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
990 ((uint8_t*)p_tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
991 ((uint8_t*)p_tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (p_tk->fmt.audio.i_channels << 3);
994 int syncExtensionType = 0x2B7;
996 for (iDSRI=0; iDSRI<13; iDSRI++)
997 if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
999 ((uint8_t*)p_tk->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
1000 ((uint8_t*)p_tk->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
1001 ((uint8_t*)p_tk->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
1004 else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
1006 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1007 fill_extra_data( p_tk );
1009 else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
1011 p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
1012 fill_extra_data( p_tk );
1014 else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
1016 p_fmt->i_codec = VLC_CODEC_TTA;
1017 if( p_tk->i_extra_data > 0 )
1019 fill_extra_data( p_tk );
1023 p_fmt->i_extra = 30;
1024 p_fmt->p_extra = xmalloc( p_fmt->i_extra );
1025 uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
1026 memcpy( &p_extra[ 0], "TTA1", 4 );
1027 SetWLE( &p_extra[ 4], 1 );
1028 SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
1029 SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
1030 SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
1031 SetDWLE( &p_extra[14], 0xffffffff );
1032 memset( &p_extra[18], 0, 30 - 18 );
1035 else if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) ||
1036 !strcmp( p_tk->psz_codec, "A_PCM/INT/LIT" ) ||
1037 !strcmp( p_tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1039 if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
1041 p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1045 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1047 p_tk->fmt.audio.i_blockalign = ( p_tk->fmt.audio.i_bitspersample + 7 ) / 8 * p_tk->fmt.audio.i_channels;
1049 else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
1051 if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
1052 p_tk->fmt.i_codec = VLC_CODEC_COOK;
1053 else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
1054 p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
1055 else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
1056 p_tk->fmt.i_codec = VLC_CODEC_RA_288;
1057 /* FIXME 14_4, RALF and SIPR */
1058 fill_extra_data( p_tk );
1060 else if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1062 p_fmt->i_codec = VLC_CODEC_RA_144;
1063 p_fmt->audio.i_channels = 1;
1064 p_fmt->audio.i_rate = 8000;
1065 p_fmt->audio.i_blockalign = 0x14;
1067 /* disabled due to the potential "S_KATE" namespace issue */
1068 else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
1070 int i, i_offset = 1, i_extra, num_headers, size_so_far;
1073 p_tk->fmt.i_codec = VLC_CODEC_KATE;
1074 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1076 fill_extra_data( p_tk );
1078 else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
1080 p_fmt->i_codec = VLC_CODEC_SUBT;
1081 p_fmt->subs.psz_encoding = NULL; /* Is there a place where it is stored ? */
1083 else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
1085 p_tk->fmt.i_codec = VLC_CODEC_SUBT;
1086 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1088 else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
1090 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1091 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1092 if( p_tk->i_extra_data )
1094 fill_extra_data( p_tk );
1097 else if( !strcmp( p_tk->psz_codec, "S_TEXT/SSA" ) ||
1098 !strcmp( p_tk->psz_codec, "S_TEXT/ASS" ) ||
1099 !strcmp( p_tk->psz_codec, "S_SSA" ) ||
1100 !strcmp( p_tk->psz_codec, "S_ASS" ))
1102 p_tk->fmt.i_codec = VLC_CODEC_SSA;
1103 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1104 if( p_tk->i_extra_data )
1106 fill_extra_data( p_tk );
1109 else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
1111 p_tk->fmt.i_codec = VLC_CODEC_SPU;
1112 if( p_tk->i_extra_data )
1115 char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
1116 if( psz_buf != NULL )
1118 memcpy( psz_buf, p_tk->p_extra_data , p_tk->i_extra_data );
1119 psz_buf[p_tk->i_extra_data] = '\0';
1121 psz_start = strstr( psz_buf, "size:" );
1123 vobsub_size_parse( psz_start,
1124 &p_tk->fmt.subs.spu.i_original_frame_width,
1125 &p_tk->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1127 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1128 p_tk->fmt.subs.spu.i_original_frame_width,
1129 p_tk->fmt.subs.spu.i_original_frame_height );
1133 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1136 psz_start = strstr( psz_buf, "palette:" );
1138 vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1140 p_tk->fmt.subs.spu.palette[0] = 0xBeef;
1141 msg_Dbg( &sys.demuxer, "vobsub palette read" );
1145 msg_Warn( &sys.demuxer, "reading original palette failed" );
1151 else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
1153 p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
1155 else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
1157 p_tk->fmt.i_cat = NAV_ES;
1162 msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
1163 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1165 if( p_tk->b_default )
1167 p_tk->fmt.i_priority = 1000;
1170 p_tk->p_es = es_out_Add( sys.demuxer.out, &p_tk->fmt );
1172 /* Turn on a subtitles track if it has been flagged as default -
1173 * but only do this if no subtitles track has already been engaged,
1174 * either by an earlier 'default track' (??) or by default
1175 * language choice behaviour.
1177 if( p_tk->b_default )
1179 es_out_Control( sys.demuxer.out,
1180 ES_OUT_SET_ES_DEFAULT,
1184 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
1186 sys.i_start_pts = i_start_time;
1187 // reset the stream reading to the first cluster of the segment used
1188 es.I_O().setFilePointer( i_start_pos );
1191 ep = new EbmlParser( &es, segment, &sys.demuxer );
1196 void matroska_segment_c::UnSelect( )
1198 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1200 if ( tracks[i_track]->p_es != NULL )
1202 // es_format_Clean( &tracks[i_track]->fmt );
1203 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
1204 tracks[i_track]->p_es = NULL;
1211 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, bool *pb_key_picture, bool *pb_discardable_picture, int64_t *pi_duration )
1213 pp_simpleblock = NULL;
1216 *pb_key_picture = true;
1217 *pb_discardable_picture = false;
1221 EbmlElement *el = NULL;
1225 return VLC_EGENERIC;
1227 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1229 /* Check blocks validity to protect againts broken files */
1230 if( BlockFindTrackIndex( NULL, pp_block , pp_simpleblock ) )
1233 pp_simpleblock = NULL;
1237 if( pp_simpleblock != NULL )
1239 *pb_key_picture = pp_simpleblock->IsKeyframe();
1240 *pb_discardable_picture = pp_simpleblock->IsDiscardable();
1243 /* update the index */
1244 #define idx p_indexes[i_index - 1]
1245 if( i_index > 0 && idx.i_time == -1 )
1247 if ( pp_simpleblock != NULL )
1248 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1250 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1251 idx.b_key = *pb_key_picture;
1257 i_level = ep->GetLevel();
1266 msg_Warn( &sys.demuxer, "EOF" );
1267 return VLC_EGENERIC;
1270 /* Verify that we are still inside our cluster
1271 * It can happens whith broken files and when seeking
1275 if( cluster && !ep->IsTopPresent( cluster ) )
1277 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1288 if( MKV_IS_ID( el, KaxCluster ) )
1290 cluster = (KaxCluster*)el;
1291 i_cluster_pos = cluster->GetElementPosition();
1293 /* add it to the index */
1295 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1297 IndexAppendCluster( cluster );
1300 // reset silent tracks
1301 for (size_t i=0; i<tracks.size(); i++)
1303 tracks[i]->b_silent = false;
1308 else if( MKV_IS_ID( el, KaxCues ) )
1310 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1311 return VLC_EGENERIC;
1315 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1319 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1321 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1323 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1324 cluster->InitTimecode( uint64( ctc ), i_timescale );
1326 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1330 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1332 i_block_pos = el->GetElementPosition();
1335 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1337 pp_simpleblock = (KaxSimpleBlock*)el;
1339 pp_simpleblock->ReadData( es.I_O() );
1340 pp_simpleblock->SetParent( *cluster );
1344 if( MKV_IS_ID( el, KaxBlock ) )
1346 pp_block = (KaxBlock*)el;
1348 pp_block->ReadData( es.I_O() );
1349 pp_block->SetParent( *cluster );
1353 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1355 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1357 dur.ReadData( es.I_O() );
1358 *pi_duration = uint64( dur );
1360 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1362 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1364 ref.ReadData( es.I_O() );
1366 if( *pb_key_picture )
1367 *pb_key_picture = false;
1368 else if( int64( ref ) > 0 )
1369 *pb_discardable_picture = true;
1371 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1373 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1374 track_num.ReadData( es.I_O() );
1376 for (size_t i=0; i<tracks.size(); i++)
1378 if ( tracks[i]->i_number == uint32(track_num))
1380 tracks[i]->b_silent = true;
1387 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1388 return VLC_EGENERIC;