1 /*****************************************************************************
2 * matroska_segment_parse.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2010 VLC authors and VideoLAN
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 it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "matroska_segment.hpp"
26 #include "chapters.hpp"
28 #include "Ebml_parser.hpp"
32 #include "../vobsub.h"
34 #include "../windows_audio_commons.h"
35 #include "../mp4/libmp4.h"
38 #include <vlc_codecs.h>
40 /* GetFourCC helper */
41 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
42 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
44 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
47 static inline void fill_extra_data( mkv_track_t *p_tk, unsigned int offset )
49 if(p_tk->i_extra_data <= offset) return;
50 p_tk->fmt.i_extra = p_tk->i_extra_data - offset;
51 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
52 if(!p_tk->fmt.p_extra) { p_tk->fmt.i_extra = 0; return; };
53 memcpy( p_tk->fmt.p_extra, p_tk->p_extra_data + offset, p_tk->fmt.i_extra );
56 /*****************************************************************************
57 * Some functions to manipulate memory
58 *****************************************************************************/
59 static inline char * ToUTF8( const UTFstring &u )
61 return strdup( u.GetUTF8().c_str() );
64 /*****************************************************************************
66 *****************************************************************************/
67 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
75 stream_Control( sys.demuxer.s, STREAM_CAN_SEEK, &b_seekable );
79 ep = new EbmlParser( &es, seekhead, &sys.demuxer );
81 while( ( l = ep->Get() ) != NULL )
83 if( MKV_IS_ID( l, KaxSeek ) )
85 EbmlId id = EBML_ID(EbmlVoid);
89 msg_Dbg( &sys.demuxer, "| | + Seek" );
94 while( ( l = ep->Get() ) != NULL )
96 if( unlikely( l->GetSize() >= SIZE_MAX ) )
98 msg_Err( &sys.demuxer,"%s too big... skipping it", typeid(*l).name() );
101 if( MKV_IS_ID( l, KaxSeekID ) )
103 KaxSeekID &sid = *(KaxSeekID*)l;
104 sid.ReadData( es.I_O() );
105 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
107 else if( MKV_IS_ID( l, KaxSeekPosition ) )
109 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
110 spos.ReadData( es.I_O() );
111 i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) );
115 /* Many mkvmerge files hit this case. It seems to be a broken SeekHead */
116 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
122 msg_Err( &sys.demuxer,"Error while reading %s", typeid(*l).name() );
128 if( id == EBML_ID(KaxCues) )
130 msg_Dbg( &sys.demuxer, "| - cues at %" PRId64, i_pos );
131 LoadSeekHeadItem( EBML_INFO(KaxCues), i_pos );
133 else if( id == EBML_ID(KaxInfo) )
135 msg_Dbg( &sys.demuxer, "| - info at %" PRId64, i_pos );
136 LoadSeekHeadItem( EBML_INFO(KaxInfo), i_pos );
138 else if( id == EBML_ID(KaxChapters) )
140 msg_Dbg( &sys.demuxer, "| - chapters at %" PRId64, i_pos );
141 LoadSeekHeadItem( EBML_INFO(KaxChapters), i_pos );
143 else if( id == EBML_ID(KaxTags) )
145 msg_Dbg( &sys.demuxer, "| - tags at %" PRId64, i_pos );
146 LoadSeekHeadItem( EBML_INFO(KaxTags), i_pos );
148 else if( id == EBML_ID(KaxSeekHead) )
150 msg_Dbg( &sys.demuxer, "| - chained seekhead at %" PRId64, i_pos );
151 LoadSeekHeadItem( EBML_INFO(KaxSeekHead), i_pos );
153 else if( id == EBML_ID(KaxTracks) )
155 msg_Dbg( &sys.demuxer, "| - tracks at %" PRId64, i_pos );
156 LoadSeekHeadItem( EBML_INFO(KaxTracks), i_pos );
158 else if( id == EBML_ID(KaxAttachments) )
160 msg_Dbg( &sys.demuxer, "| - attachments at %" PRId64, i_pos );
161 LoadSeekHeadItem( EBML_INFO(KaxAttachments), i_pos );
165 msg_Dbg( &sys.demuxer, "| - unknown seekhead reference at %" PRId64, i_pos );
170 msg_Dbg( &sys.demuxer, "| | + ParseSeekHead Unknown (%s)", typeid(*l).name() );
177 * Helper function to print the mkv parse tree
179 static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
184 msg_Err( &demuxer, "MKV tree is too deep" );
187 va_start( args, psz_format );
188 static const char psz_foo[] = "| | | | | | | | | |";
189 char *psz_foo2 = (char*)malloc( i_level * 4 + 3 + strlen( psz_format ) );
190 strncpy( psz_foo2, psz_foo, 4 * i_level );
191 psz_foo2[ 4 * i_level ] = '+';
192 psz_foo2[ 4 * i_level + 1 ] = ' ';
193 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
194 msg_GenericVa( &demuxer,VLC_MSG_DBG, "mkv", psz_foo2, args );
200 /*****************************************************************************
202 *****************************************************************************/
203 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
205 bool bSupported = true;
208 mkv_track_t *tk = new mkv_track_t();
209 memset( tk, 0, sizeof( mkv_track_t ) );
211 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
213 tk->fmt.psz_language = strdup("English");
214 tk->fmt.psz_description = NULL;
216 tk->b_default = true;
217 tk->b_enabled = true;
218 tk->b_forced = false;
219 tk->b_silent = false;
220 tk->i_number = tracks.size() - 1;
221 tk->i_extra_data = 0;
222 tk->p_extra_data = NULL;
223 tk->psz_codec = NULL;
224 tk->b_dts_only = false;
225 tk->i_default_duration = 0;
226 tk->b_no_duration = false;
227 tk->f_timecodescale = 1.0;
229 tk->b_inited = false;
231 tk->p_data_init = NULL;
233 tk->psz_codec_name = NULL;
234 tk->psz_codec_settings = NULL;
235 tk->psz_codec_info_url = NULL;
236 tk->psz_codec_download_url = NULL;
238 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
239 tk->i_encoding_scope = MATROSKA_ENCODING_SCOPE_ALL_FRAMES;
240 tk->p_compression_data = NULL;
242 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
244 for( size_t i = 0; i < m->ListSize(); i++ )
246 EbmlElement *l = (*m)[i];
248 if( MKV_IS_ID( l, KaxTrackNumber ) )
250 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
252 tk->i_number = uint32( tnum );
253 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
255 else if( MKV_IS_ID( l, KaxTrackUID ) )
257 KaxTrackUID &tuid = *(KaxTrackUID*)l;
259 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
261 else if( MKV_IS_ID( l, KaxTrackType ) )
263 const char *psz_type;
264 KaxTrackType &ttype = *(KaxTrackType*)l;
266 switch( uint8(ttype) )
270 tk->fmt.i_cat = AUDIO_ES;
271 tk->fmt.audio.i_channels = 1;
272 tk->fmt.audio.i_rate = 8000;
276 tk->fmt.i_cat = VIDEO_ES;
279 psz_type = "subtitle";
280 tk->fmt.i_cat = SPU_ES;
283 psz_type = "buttons";
284 tk->fmt.i_cat = SPU_ES;
287 psz_type = "unknown";
288 tk->fmt.i_cat = UNKNOWN_ES;
292 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
294 else if( MKV_IS_ID( l, KaxTrackFlagEnabled ) ) // UNUSED
296 KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
298 tk->b_enabled = uint32( fenb );
299 msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u", uint32( fenb ) );
301 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
303 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
305 tk->b_default = uint32( fdef );
306 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
308 else if( MKV_IS_ID( l, KaxTrackFlagForced ) ) // UNUSED
310 KaxTrackFlagForced &ffor = *(KaxTrackFlagForced*)l;
311 tk->b_forced = uint32( ffor );
313 msg_Dbg( &sys.demuxer, "| | | + Track Forced=%u", uint32( ffor ) );
315 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) ) // UNUSED
317 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
319 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
321 else if( MKV_IS_ID( l, KaxTrackMinCache ) ) // UNUSED
323 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
325 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
327 else if( MKV_IS_ID( l, KaxTrackMaxCache ) ) // UNUSED
329 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
331 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
333 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
335 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
337 tk->i_default_duration = uint64(defd);
338 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%" PRId64, uint64(defd) );
340 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
342 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
344 tk->f_timecodescale = float( ttcs );
345 if ( tk->f_timecodescale <= 0 ) tk->f_timecodescale = 1.0;
346 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
348 else if( MKV_IS_ID( l, KaxMaxBlockAdditionID ) ) // UNUSED
350 KaxMaxBlockAdditionID &mbl = *(KaxMaxBlockAdditionID*)l;
352 msg_Dbg( &sys.demuxer, "| | | + Track Max BlockAdditionID=%d", uint32( mbl ) );
354 else if( MKV_IS_ID( l, KaxTrackName ) )
356 KaxTrackName &tname = *(KaxTrackName*)l;
358 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
359 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
361 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
363 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
365 free( tk->fmt.psz_language );
366 tk->fmt.psz_language = strdup( string( lang ).c_str() );
367 msg_Dbg( &sys.demuxer,
368 "| | | + Track Language=`%s'", tk->fmt.psz_language );
370 else if( MKV_IS_ID( l, KaxCodecID ) )
372 KaxCodecID &codecid = *(KaxCodecID*)l;
374 tk->psz_codec = strdup( string( codecid ).c_str() );
375 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
377 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
379 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
381 tk->i_extra_data = cpriv.GetSize();
382 if( tk->i_extra_data > 0 )
384 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
385 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
387 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%" PRId64, cpriv.GetSize() );
389 else if( MKV_IS_ID( l, KaxCodecName ) )
391 KaxCodecName &cname = *(KaxCodecName*)l;
393 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
394 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
397 else if( MKV_IS_ID( l, KaxCodecDecodeAll ) ) // UNUSED
399 KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
401 msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u", uint8( cdall ) );
403 else if( MKV_IS_ID( l, KaxTrackOverlay ) ) // UNUSED
405 KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
407 msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u", uint32( tovr ) );
409 #if LIBMATROSKA_VERSION >= 0x010401
410 else if( MKV_IS_ID( l, KaxCodecDelay ) )
412 KaxCodecDelay &codecdelay = *(KaxCodecDelay*)l;
413 tk->i_codec_delay = uint64_t( codecdelay ) / 1000;
414 msg_Dbg( &sys.demuxer, "| | | + Track Codec Delay =%" PRIu64,
417 else if( MKV_IS_ID( l, KaxSeekPreRoll ) )
419 KaxSeekPreRoll &spr = *(KaxSeekPreRoll*)l;
420 tk->i_seek_preroll = uint64_t(spr) / 1000;
421 msg_Dbg( &sys.demuxer, "| | | + Track Seek Preroll =%" PRIu64, tk->i_seek_preroll );
424 else if( MKV_IS_ID( l, KaxContentEncodings ) )
426 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
427 MkvTree( sys.demuxer, 3, "Content Encodings" );
428 if ( cencs->ListSize() > 1 )
430 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
433 for( size_t j = 0; j < cencs->ListSize(); j++ )
435 EbmlElement *l2 = (*cencs)[j];
436 if( MKV_IS_ID( l2, KaxContentEncoding ) )
438 MkvTree( sys.demuxer, 4, "Content Encoding" );
439 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
440 for( size_t k = 0; k < cenc->ListSize(); k++ )
442 EbmlElement *l3 = (*cenc)[k];
443 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
445 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
446 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
448 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
450 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
451 tk->i_encoding_scope = uint32( encscope );
452 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
454 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
456 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
457 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
459 else if( MKV_IS_ID( l3, KaxContentCompression ) )
461 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
462 MkvTree( sys.demuxer, 5, "Content Compression" );
463 //Default compression type is 0 (Zlib)
464 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
465 for( size_t n = 0; n < compr->ListSize(); n++ )
467 EbmlElement *l4 = (*compr)[n];
468 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
470 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
471 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
472 tk->i_compression_type = uint32( compalg );
473 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
474 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
476 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
480 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
482 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
486 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
490 // ContentEncryption Unsupported
493 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
499 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
503 // else if( MKV_IS_ID( l, KaxCodecSettings) ) DEPRECATED by matroska
505 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
507 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
508 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
510 // else if( MKV_IS_ID( l, KaxCodecInfoURL) ) DEPRECATED by matroska
512 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
514 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
515 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
517 // else if( MKV_IS_ID( l, KaxCodecDownloadURL) ) DEPRECATED by matroska
519 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
521 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
522 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
524 else if( MKV_IS_ID( l, KaxTrackVideo ) )
526 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
527 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
528 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
530 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
533 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
534 tk->fmt.video.i_frame_rate = 1000000;
536 for( unsigned int j = 0; j < tkv->ListSize(); j++ )
538 EbmlElement *l = (*tkv)[j];
539 if( MKV_IS_ID( l, KaxVideoFlagInterlaced ) ) // UNUSED
541 KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)l;
543 msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
545 else if( MKV_IS_ID( l, KaxVideoStereoMode ) ) // UNUSED
547 KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)l;
549 msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
551 else if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
553 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
555 tk->fmt.video.i_width += uint16( vwidth );
556 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
558 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
560 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
562 tk->fmt.video.i_height += uint16( vheight );
563 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
565 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
567 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
569 i_display_width = uint16( vwidth );
570 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
572 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
574 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
576 i_display_height = uint16( vheight );
577 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
579 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
581 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
583 i_crop_bottom = uint16( cropval );
584 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
586 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
588 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
590 i_crop_top = uint16( cropval );
591 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
593 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
595 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
597 i_crop_right = uint16( cropval );
598 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
600 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
602 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
604 i_crop_left = uint16( cropval );
605 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
607 else if( MKV_IS_ID( l, KaxVideoDisplayUnit ) )
609 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
611 i_display_unit = uint8( vdmode );
612 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
613 i_display_unit == 0 ? "pixels" : ( i_display_unit == 1 ? "centimeters": "inches" ) );
615 else if( MKV_IS_ID( l, KaxVideoAspectRatio ) ) // UNUSED
617 KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
619 msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
621 // ColourSpace UNUSED
622 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
624 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
626 tk->f_fps = __MAX( float( vfps ), 1 );
627 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
629 // else if( MKV_IS_ID( l, KaxVideoGamma) ) //DEPRECATED by Matroska
631 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
633 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
637 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
640 if( i_display_height && i_display_width )
642 tk->fmt.video.i_sar_num = i_display_width * tk->fmt.video.i_height;
643 tk->fmt.video.i_sar_den = i_display_height * tk->fmt.video.i_width;
645 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
647 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
648 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
649 tk->fmt.video.i_x_offset = i_crop_left;
650 tk->fmt.video.i_y_offset = i_crop_top;
651 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
652 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
654 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
655 we do not support this atm */
657 else if( MKV_IS_ID( l, KaxTrackAudio ) )
659 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
661 /* Initialize default values */
662 tk->fmt.audio.i_channels = 1;
663 tk->fmt.audio.i_rate = 8000;
665 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
667 for( unsigned int j = 0; j < tka->ListSize(); j++ )
669 EbmlElement *l = (*tka)[j];
671 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
673 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
675 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
676 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
678 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
680 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
682 tk->fmt.audio.i_rate = (int)float( afreq );
683 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
685 else if( MKV_IS_ID( l, KaxAudioChannels ) )
687 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
689 tk->fmt.audio.i_channels = uint8( achan );
690 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
692 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
694 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
696 tk->fmt.audio.i_bitspersample = uint8( abits );
697 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
701 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
707 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
715 if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB &&
716 tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_PRIVATE &&
717 tk->i_extra_data && tk->p_extra_data &&
718 zlib_decompress_extra( &sys.demuxer, tk) )
721 if( TrackInit( tk ) )
723 msg_Err(&sys.demuxer, "Couldn't init track %d", tk->i_number );
724 free(tk->p_extra_data);
729 tracks.push_back( tk );
733 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
734 free(tk->p_extra_data);
739 /*****************************************************************************
741 *****************************************************************************/
742 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
745 int i_upper_level = 0;
747 /* Master elements */
748 if( unlikely( tracks->GetSize() >= SIZE_MAX ) )
750 msg_Err( &sys.demuxer, "Track too big, aborting" );
755 tracks->Read( es, EBML_CONTEXT(tracks), i_upper_level, el, true );
759 msg_Err( &sys.demuxer, "Couldn't read tracks" );
763 for( size_t i = 0; i < tracks->ListSize(); i++ )
765 EbmlElement *l = (*tracks)[i];
767 if( MKV_IS_ID( l, KaxTrackEntry ) )
769 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
773 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
778 /*****************************************************************************
780 *****************************************************************************/
781 void matroska_segment_c::ParseInfo( KaxInfo *info )
785 int i_upper_level = 0;
787 /* Master elements */
788 m = static_cast<EbmlMaster *>(info);
789 if( unlikely( m->GetSize() >= SIZE_MAX ) )
791 msg_Err( &sys.demuxer, "Info too big, aborting" );
796 m->Read( es, EBML_CONTEXT(info), i_upper_level, el, true );
800 msg_Err( &sys.demuxer, "Couldn't read info" );
804 for( size_t i = 0; i < m->ListSize(); i++ )
806 EbmlElement *l = (*m)[i];
808 if( MKV_IS_ID( l, KaxSegmentUID ) )
810 if ( p_segment_uid == NULL )
811 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
813 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
815 else if( MKV_IS_ID( l, KaxPrevUID ) )
817 if ( p_prev_segment_uid == NULL )
819 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
820 b_ref_external_segments = true;
823 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
825 else if( MKV_IS_ID( l, KaxNextUID ) )
827 if ( p_next_segment_uid == NULL )
829 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
830 b_ref_external_segments = true;
833 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
835 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
837 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
839 i_timescale = uint64(tcs);
841 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%" PRId64,
844 else if( MKV_IS_ID( l, KaxDuration ) )
846 KaxDuration &dur = *(KaxDuration*)l;
848 i_duration = mtime_t( double( dur ) );
850 msg_Dbg( &sys.demuxer, "| | + Duration=%" PRId64,
853 else if( MKV_IS_ID( l, KaxMuxingApp ) )
855 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
857 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
859 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
860 psz_muxing_application );
862 else if( MKV_IS_ID( l, KaxWritingApp ) )
864 KaxWritingApp &wapp = *(KaxWritingApp*)l;
866 psz_writing_application = ToUTF8( UTFstring( wapp ) );
868 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
869 psz_writing_application );
871 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
873 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
875 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
877 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
878 psz_segment_filename );
880 else if( MKV_IS_ID( l, KaxTitle ) )
882 KaxTitle &title = *(KaxTitle*)l;
884 psz_title = ToUTF8( UTFstring( title ) );
886 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
888 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
890 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
892 families.push_back( new KaxSegmentFamily(*uid) );
894 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
896 else if( MKV_IS_ID( l, KaxDateUTC ) )
898 KaxDateUTC &date = *(KaxDateUTC*)l;
903 i_date = date.GetEpochDate();
904 if( gmtime_r( &i_date, &tmres ) &&
905 strftime( buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y",
908 psz_date_utc = strdup( buffer );
909 msg_Dbg( &sys.demuxer, "| | + Date=%s", buffer );
912 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
914 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
917 if( unlikely( p_trans->GetSize() >= SIZE_MAX ) )
919 msg_Err( &sys.demuxer, "Chapter translate too big, aborting" );
923 p_trans->Read( es, EBML_CONTEXT(p_trans), i_upper_level, el, true );
924 chapter_translation_c *p_translate = new chapter_translation_c();
926 for( size_t j = 0; j < p_trans->ListSize(); j++ )
928 EbmlElement *l = (*p_trans)[j];
930 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
932 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
934 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
936 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
938 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
940 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
944 translations.push_back( p_translate );
948 msg_Err( &sys.demuxer, "Error while reading Chapter Tranlate");
953 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
957 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
958 i_duration = mtime_t(f_dur);
959 if( !i_duration ) i_duration = -1;
963 /*****************************************************************************
965 *****************************************************************************/
966 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
968 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
969 for( size_t i = 0; i < ca->ListSize(); i++ )
971 EbmlElement *l = (*ca)[i];
973 if( MKV_IS_ID( l, KaxChapterUID ) )
975 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
976 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %" PRIu64, chapters.i_uid );
978 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
980 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
981 chapters.b_display_seekpoint = uint8( flag ) == 0;
983 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
985 else if( MKV_IS_ID( l, KaxChapterSegmentUID ) )
987 chapters.p_segment_uid = new KaxChapterSegmentUID( *static_cast<KaxChapterSegmentUID*>(l) );
988 b_ref_external_segments = true;
989 msg_Dbg( &sys.demuxer, "| | | | + ChapterSegmentUID= %u", *(uint32*)chapters.p_segment_uid->GetBuffer() );
991 else if( MKV_IS_ID( l, KaxChapterSegmentEditionUID ) )
993 chapters.p_segment_edition_uid = new KaxChapterSegmentEditionUID( *static_cast<KaxChapterSegmentEditionUID*>(l) );
994 msg_Dbg( &sys.demuxer, "| | | | + ChapterSegmentEditionUID= %u",
995 #if LIBMATROSKA_VERSION < 0x010300
996 *(uint32*)chapters.p_segment_edition_uid->GetBuffer()
998 *(uint32*)chapters.p_segment_edition_uid
1002 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
1004 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
1005 chapters.i_start_time = uint64( start ) / INT64_C(1000);
1007 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %" PRId64, chapters.i_start_time );
1009 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
1011 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
1012 chapters.i_end_time = uint64( end ) / INT64_C(1000);
1014 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %" PRId64, chapters.i_end_time );
1016 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
1018 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
1020 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
1021 for( size_t j = 0; j < cd->ListSize(); j++ )
1023 EbmlElement *l= (*cd)[j];
1025 if( MKV_IS_ID( l, KaxChapterString ) )
1027 KaxChapterString &name =*(KaxChapterString*)l;
1028 for ( int k = 0; k < i_level; k++)
1029 chapters.psz_name += '+';
1030 chapters.psz_name += ' ';
1031 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
1032 chapters.psz_name += psz_tmp_utf8;
1033 chapters.b_user_display = true;
1035 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
1036 free( psz_tmp_utf8 );
1038 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
1040 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
1041 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'",
1042 string( lang ).c_str() );
1044 else if( MKV_IS_ID( l, KaxChapterCountry ) )
1046 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
1047 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'",
1048 string( ct ).c_str() );
1052 else if( MKV_IS_ID( l, KaxChapterProcess ) )
1054 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
1056 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
1057 chapter_codec_cmds_c *p_ccodec = NULL;
1059 for( size_t j = 0; j < cp->ListSize(); j++ )
1061 EbmlElement *k= (*cp)[j];
1063 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
1065 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
1066 if ( uint32(*p_codec_id) == 0 )
1067 p_ccodec = new matroska_script_codec_c( sys );
1068 else if ( uint32(*p_codec_id) == 1 )
1069 p_ccodec = new dvd_chapter_codec_c( sys );
1074 if ( p_ccodec != NULL )
1076 for( size_t j = 0; j < cp->ListSize(); j++ )
1078 EbmlElement *k= (*cp)[j];
1080 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
1082 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
1083 p_ccodec->SetPrivate( *p_private );
1085 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
1087 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
1090 chapters.codecs.push_back( p_ccodec );
1093 else if( MKV_IS_ID( l, KaxChapterAtom ) )
1095 chapter_item_c *new_sub_chapter = new chapter_item_c();
1096 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1097 new_sub_chapter->p_parent = &chapters;
1098 chapters.sub_chapters.push_back( new_sub_chapter );
1103 /*****************************************************************************
1105 *****************************************************************************/
1106 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
1109 int i_upper_level = 0;
1111 if( unlikely( attachments->GetSize() >= SIZE_MAX ) )
1113 msg_Err( &sys.demuxer, "Attachments too big, aborting" );
1118 attachments->Read( es, EBML_CONTEXT(attachments), i_upper_level, el, true );
1122 msg_Err( &sys.demuxer, "Error while reading attachments" );
1126 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
1128 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
1130 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
1131 char *psz_tmp_utf8 = ToUTF8( UTFstring( GetChild<KaxFileName>( *attachedFile ) ) );
1132 std::string attached_filename(psz_tmp_utf8);
1134 attachment_c *new_attachment = new attachment_c( attached_filename,
1135 GetChild<KaxMimeType>( *attachedFile ),
1136 img_data.GetSize() );
1138 msg_Dbg( &sys.demuxer, "| | - %s (%s)", new_attachment->fileName(), new_attachment->mimeType() );
1140 if( new_attachment->init() )
1142 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
1143 sys.stored_attachments.push_back( new_attachment );
1144 if( !strncmp( new_attachment->mimeType(), "image/", 6 ) )
1147 if( asprintf( &psz_url, "attachment://%s",
1148 new_attachment->fileName() ) == -1 )
1151 sys.meta = vlc_meta_New();
1152 vlc_meta_SetArtURL( sys.meta, psz_url );
1158 delete new_attachment;
1161 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
1165 /*****************************************************************************
1167 *****************************************************************************/
1168 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
1171 int i_upper_level = 0;
1173 /* Master elements */
1174 if( unlikely( chapters->GetSize() >= SIZE_MAX ) )
1176 msg_Err( &sys.demuxer, "Chapters too big, aborting" );
1181 chapters->Read( es, EBML_CONTEXT(chapters), i_upper_level, el, true );
1185 msg_Err( &sys.demuxer, "Error while reading chapters" );
1189 for( size_t i = 0; i < chapters->ListSize(); i++ )
1191 EbmlElement *l = (*chapters)[i];
1193 if( MKV_IS_ID( l, KaxEditionEntry ) )
1195 chapter_edition_c *p_edition = new chapter_edition_c();
1197 EbmlMaster *E = static_cast<EbmlMaster *>(l );
1198 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
1199 for( size_t j = 0; j < E->ListSize(); j++ )
1201 EbmlElement *l = (*E)[j];
1203 if( MKV_IS_ID( l, KaxChapterAtom ) )
1205 chapter_item_c *new_sub_chapter = new chapter_item_c();
1206 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1207 p_edition->sub_chapters.push_back( new_sub_chapter );
1209 else if( MKV_IS_ID( l, KaxEditionUID ) )
1211 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
1213 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
1215 p_edition->b_ordered = var_InheritBool( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
1217 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
1219 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
1220 i_default_edition = stored_editions.size();
1222 else if( MKV_IS_ID( l, KaxEditionFlagHidden ) )
1224 // FIXME to implement
1228 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
1231 stored_editions.push_back( p_edition );
1235 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
1240 void matroska_segment_c::ParseCluster( bool b_update_start_time )
1244 int i_upper_level = 0;
1246 /* Master elements */
1247 m = static_cast<EbmlMaster *>( cluster );
1248 if( unlikely( m->GetSize() >= SIZE_MAX ) )
1250 msg_Err( &sys.demuxer, "Cluster too big, aborting" );
1255 m->Read( es, EBML_CONTEXT(cluster), i_upper_level, el, true );
1259 msg_Err( &sys.demuxer, "Error while reading cluster" );
1263 for( unsigned int i = 0; i < m->ListSize(); i++ )
1265 EbmlElement *l = (*m)[i];
1267 if( MKV_IS_ID( l, KaxClusterTimecode ) )
1269 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
1271 cluster->InitTimecode( uint64( ctc ), i_timescale );
1276 if( b_update_start_time )
1277 i_start_time = cluster->GlobalTimecode() / 1000;
1281 int32_t matroska_segment_c::TrackInit( mkv_track_t * p_tk )
1283 es_format_t *p_fmt = &p_tk->fmt;
1285 if( p_tk->psz_codec == NULL )
1287 msg_Err( &sys.demuxer, "Empty codec id" );
1288 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1292 if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
1294 if( p_tk->i_extra_data < (int)sizeof( VLC_BITMAPINFOHEADER ) )
1296 msg_Err( &sys.demuxer, "missing/invalid VLC_BITMAPINFOHEADER" );
1297 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1301 VLC_BITMAPINFOHEADER *p_bih = (VLC_BITMAPINFOHEADER*)p_tk->p_extra_data;
1303 p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1304 p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1305 p_tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1307 p_tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( VLC_BITMAPINFOHEADER );
1308 if( p_tk->fmt.i_extra > 0 )
1310 /* Very unlikely yet possible: bug #5659*/
1311 size_t maxlen = p_tk->i_extra_data - sizeof( VLC_BITMAPINFOHEADER );
1312 p_tk->fmt.i_extra = ( (unsigned)p_tk->fmt.i_extra < maxlen )?
1313 p_tk->fmt.i_extra : maxlen;
1315 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1316 memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
1319 p_tk->b_dts_only = true;
1321 else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
1322 !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
1324 p_tk->fmt.i_codec = VLC_CODEC_MPGV;
1325 if( p_tk->i_extra_data )
1326 fill_extra_data( p_tk, 0 );
1328 else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
1330 p_tk->fmt.i_codec = VLC_CODEC_THEORA;
1331 fill_extra_data( p_tk, 0 );
1332 p_tk->b_pts_only = true;
1334 else if( !strncmp( p_tk->psz_codec, "V_REAL/RV", 9 ) )
1336 uint8_t *p = p_tk->p_extra_data;
1338 if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
1339 p_fmt->i_codec = VLC_CODEC_RV10;
1340 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
1341 p_fmt->i_codec = VLC_CODEC_RV20;
1342 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
1343 p_fmt->i_codec = VLC_CODEC_RV30;
1344 else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
1345 p_fmt->i_codec = VLC_CODEC_RV40;
1347 /* Extract the framerate from the header */
1348 if( p_tk->i_extra_data >= 26 &&
1349 p[4] == 'V' && p[5] == 'I' && p[6] == 'D' && p[7] == 'O' &&
1350 p[8] == 'R' && p[9] == 'V' &&
1351 (p[10] == '3' || p[10] == '4') && p[11] == '0' )
1353 p_tk->fmt.video.i_frame_rate =
1354 p[22] << 24 | p[23] << 16 | p[24] << 8 | p[25] << 0;
1355 p_tk->fmt.video.i_frame_rate_base = 65536;
1358 fill_extra_data( p_tk, 26 );
1359 p_tk->b_dts_only = true;
1361 else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
1363 p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
1365 else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
1367 p_tk->fmt.i_codec = VLC_CODEC_VP8;
1368 p_tk->b_pts_only = true;
1370 else if( !strncmp( p_tk->psz_codec, "V_VP9", 5 ) )
1372 p_tk->fmt.i_codec = VLC_CODEC_VP9;
1373 fill_extra_data( p_tk, 0 );
1375 else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
1377 if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
1379 p_tk->fmt.i_codec = VLC_CODEC_DIV3;
1381 else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
1383 /* A MPEG 4 codec, SP, ASP, AP or AVC */
1384 if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1385 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1387 p_tk->fmt.i_codec = VLC_CODEC_MP4V;
1388 fill_extra_data( p_tk, 0 );
1391 else if( !strncmp( p_tk->psz_codec, "V_MPEGH/ISO/HEVC", 16) )
1393 p_tk->fmt.i_codec = VLC_CODEC_HEVC;
1394 fill_extra_data( p_tk, 0 );
1396 else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
1398 MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
1399 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1403 if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
1404 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
1406 p_tk->fmt.i_codec = p_box->i_type;
1407 uint32_t i_width = p_box->data.p_sample_vide->i_width;
1408 uint32_t i_height = p_box->data.p_sample_vide->i_height;
1409 if( i_width && i_height )
1411 p_tk->fmt.video.i_width = i_width;
1412 p_tk->fmt.video.i_height = i_height;
1414 p_tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1415 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1416 memcpy( p_tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, p_tk->fmt.i_extra );
1417 MP4_FreeBox_sample_vide( p_box );
1423 stream_Delete( p_mp4_stream );
1425 else if( !strcmp( p_tk->psz_codec, "V_MJPEG" ) )
1427 p_tk->fmt.i_codec = VLC_CODEC_MJPG;
1429 else if( !strcmp( p_tk->psz_codec, "A_MS/ACM" ) )
1431 if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1433 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1434 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1438 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
1440 p_tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1441 p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1442 p_tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1443 p_tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1444 p_tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1446 p_tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1447 if( p_tk->fmt.i_extra > 0 )
1449 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1450 if( p_tk->fmt.p_extra )
1451 memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
1453 p_tk->fmt.i_extra = 0;
1456 if( p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
1457 p_tk->i_extra_data >= sizeof(WAVEFORMATEXTENSIBLE) )
1459 WAVEFORMATEXTENSIBLE * p_wext = (WAVEFORMATEXTENSIBLE*) p_wf;
1460 sf_tag_to_fourcc( &p_wext->SubFormat, &p_tk->fmt.i_codec, NULL);
1461 /* FIXME should we use Samples */
1463 if( p_tk->fmt.audio.i_channels > 2 &&
1464 ( p_tk->fmt.i_codec != VLC_FOURCC( 'u', 'n', 'd', 'f' ) ) )
1466 uint32_t wfextcm = GetDWLE( &p_wext->dwChannelMask );
1468 unsigned i_channel_mask = getChannelMask( &wfextcm,
1469 p_tk->fmt.audio.i_channels,
1471 p_tk->fmt.i_codec = vlc_fourcc_GetCodecAudio( p_tk->fmt.i_codec,
1472 p_tk->fmt.audio.i_bitspersample );
1473 if( i_channel_mask )
1475 p_tk->i_chans_to_reorder = aout_CheckChannelReorder(
1476 pi_channels_aout, NULL,
1478 p_tk->pi_chan_table );
1480 p_tk->fmt.audio.i_physical_channels =
1481 p_tk->fmt.audio.i_original_channels = i_channel_mask;
1486 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
1488 if( p_tk->fmt.i_codec == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
1489 msg_Err( &sys.demuxer, "Unrecognized wf tag: 0x%x", GetWLE( &p_wf->wFormatTag ) );
1492 else if( !strcmp( p_tk->psz_codec, "A_MPEG/L3" ) ||
1493 !strcmp( p_tk->psz_codec, "A_MPEG/L2" ) ||
1494 !strcmp( p_tk->psz_codec, "A_MPEG/L1" ) )
1496 p_tk->fmt.i_codec = VLC_CODEC_MPGA;
1498 else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
1500 p_tk->fmt.i_codec = VLC_CODEC_A52;
1502 else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
1504 p_tk->fmt.i_codec = VLC_CODEC_EAC3;
1506 else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
1508 p_tk->fmt.i_codec = VLC_CODEC_DTS;
1510 else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
1512 p_tk->fmt.i_codec = VLC_CODEC_MLP;
1514 else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
1516 /* FIXME when more samples arrive */
1517 p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
1518 p_fmt->b_packetized = false;
1520 else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
1522 p_tk->fmt.i_codec = VLC_CODEC_FLAC;
1523 fill_extra_data( p_tk, 8 );
1525 else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
1527 p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
1528 fill_extra_data( p_tk, 0 );
1530 else if( !strncmp( p_tk->psz_codec, "A_OPUS", 6 ) )
1532 p_tk->fmt.i_codec = VLC_CODEC_OPUS;
1533 if( !p_tk->fmt.audio.i_rate )
1535 msg_Err( &sys.demuxer,"No sampling rate, defaulting to 48kHz");
1536 p_tk->fmt.audio.i_rate = 48000;
1538 const uint8_t tags[16] = {'O','p','u','s','T','a','g','s',
1539 0, 0, 0, 0, 0, 0, 0, 0};
1540 unsigned ps[2] = { p_tk->i_extra_data, 16 };
1541 const void *pkt[2] = { (const void *)p_tk->p_extra_data,
1542 (const void *) tags };
1544 if( xiph_PackHeaders( &p_tk->fmt.i_extra,
1547 msg_Err( &sys.demuxer, "Couldn't pack OPUS headers");
1550 else if( !strncmp( p_tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1551 !strncmp( p_tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1553 int i_profile, i_srate, sbr = 0;
1554 static const unsigned int i_sample_rates[] =
1556 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1557 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1560 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1561 /* create data for faad (MP4DecSpecificDescrTag)*/
1563 if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
1567 else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
1571 else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
1575 else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
1585 for( i_srate = 0; i_srate < 13; i_srate++ )
1587 if( i_sample_rates[i_srate] == p_tk->i_original_rate )
1592 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1594 p_tk->fmt.i_extra = sbr ? 5 : 2;
1595 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1596 ((uint8_t*)p_tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1597 ((uint8_t*)p_tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (p_tk->fmt.audio.i_channels << 3);
1600 int syncExtensionType = 0x2B7;
1602 for (iDSRI=0; iDSRI<13; iDSRI++)
1603 if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
1605 ((uint8_t*)p_tk->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
1606 ((uint8_t*)p_tk->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
1607 ((uint8_t*)p_tk->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
1610 else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
1612 p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1613 fill_extra_data( p_tk, 0 );
1615 else if( !strcmp( p_tk->psz_codec, "A_ALAC" ) )
1617 p_tk->fmt.i_codec = VLC_CODEC_ALAC;
1618 fill_extra_data( p_tk, 0 );
1620 else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
1622 p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
1623 fill_extra_data( p_tk, 0 );
1625 else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
1627 p_fmt->i_codec = VLC_CODEC_TTA;
1628 if( p_tk->i_extra_data > 0 )
1630 fill_extra_data( p_tk, 0 );
1634 p_fmt->i_extra = 30;
1635 p_fmt->p_extra = xmalloc( p_fmt->i_extra );
1636 uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
1637 memcpy( &p_extra[ 0], "TTA1", 4 );
1638 SetWLE( &p_extra[ 4], 1 );
1639 SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
1640 SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
1641 SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
1642 SetDWLE( &p_extra[14], 0xffffffff );
1643 memset( &p_extra[18], 0, 30 - 18 );
1646 else if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) ||
1647 !strcmp( p_tk->psz_codec, "A_PCM/INT/LIT" ) ||
1648 !strcmp( p_tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1650 if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
1652 p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1656 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1658 p_tk->fmt.audio.i_blockalign = ( p_tk->fmt.audio.i_bitspersample + 7 ) / 8 * p_tk->fmt.audio.i_channels;
1660 else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
1662 if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1664 p_fmt->i_codec = VLC_CODEC_RA_144;
1665 p_fmt->audio.i_channels = 1;
1666 p_fmt->audio.i_rate = 8000;
1667 p_fmt->audio.i_blockalign = 0x14;
1669 else if( p_tk->i_extra_data > 28 )
1671 uint8_t *p = p_tk->p_extra_data;
1672 if( memcmp( p, ".ra", 3 ) ) {
1673 msg_Err( &sys.demuxer, "Invalid Real ExtraData 0x%4.4s", (char *)p );
1674 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1678 real_audio_private * priv = (real_audio_private*) p_tk->p_extra_data;
1679 if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
1681 p_tk->fmt.i_codec = VLC_CODEC_COOK;
1682 p_tk->fmt.audio.i_blockalign = hton16(priv->sub_packet_size);
1684 else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
1685 p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
1686 else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
1687 p_tk->fmt.i_codec = VLC_CODEC_RA_288;
1688 /* FIXME RALF and SIPR */
1689 uint16_t version = (uint16_t) hton16(priv->version);
1691 new Cook_PrivateTrackData( hton16(priv->sub_packet_h),
1692 hton16(priv->frame_size),
1693 hton16(priv->sub_packet_size));
1694 if( unlikely( !p_tk->p_sys ) )
1697 if( unlikely( p_tk->p_sys->Init() ) )
1702 real_audio_private_v4 * v4 = (real_audio_private_v4*) priv;
1703 p_tk->fmt.audio.i_channels = hton16(v4->channels);
1704 p_tk->fmt.audio.i_bitspersample = hton16(v4->sample_size);
1705 p_tk->fmt.audio.i_rate = hton16(v4->sample_rate);
1707 else if( version == 5 )
1709 real_audio_private_v5 * v5 = (real_audio_private_v5*) priv;
1710 p_tk->fmt.audio.i_channels = hton16(v5->channels);
1711 p_tk->fmt.audio.i_bitspersample = hton16(v5->sample_size);
1712 p_tk->fmt.audio.i_rate = hton16(v5->sample_rate);
1714 msg_Dbg(&sys.demuxer, "%d channels %d bits %d Hz",p_tk->fmt.audio.i_channels, p_tk->fmt.audio.i_bitspersample, p_tk->fmt.audio.i_rate);
1716 fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 78);
1720 else if( !strncmp( p_tk->psz_codec, "A_QUICKTIME", 11 ) )
1722 p_tk->fmt.i_cat = AUDIO_ES;
1723 if ( !strncmp( p_tk->psz_codec+11, "/QDM2", 5 ) )
1724 p_tk->fmt.i_codec = VLC_CODEC_QDM2;
1725 else if( !strncmp( p_tk->psz_codec+11, "/QDMC", 5 ) )
1726 p_tk->fmt.i_codec = VLC_FOURCC('Q','D','M','C');
1727 else if( p_tk->i_extra_data >= 8)
1728 p_tk->fmt.i_codec = VLC_FOURCC(p_tk->p_extra_data[4],
1729 p_tk->p_extra_data[5],
1730 p_tk->p_extra_data[6],
1731 p_tk->p_extra_data[7]);
1732 fill_extra_data( p_tk, 0 );
1734 else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
1736 p_tk->fmt.i_codec = VLC_CODEC_KATE;
1737 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1739 fill_extra_data( p_tk, 0 );
1741 else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
1743 p_fmt->i_codec = VLC_CODEC_SUBT;
1744 p_fmt->subs.psz_encoding = strdup( "ASCII" );
1746 else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
1748 p_tk->fmt.i_codec = VLC_CODEC_SUBT;
1749 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1751 else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
1753 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1754 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1755 if( p_tk->i_extra_data )
1756 fill_extra_data( p_tk, 0 );
1758 else if( !strcmp( p_tk->psz_codec, "S_TEXT/SSA" ) ||
1759 !strcmp( p_tk->psz_codec, "S_TEXT/ASS" ) ||
1760 !strcmp( p_tk->psz_codec, "S_SSA" ) ||
1761 !strcmp( p_tk->psz_codec, "S_ASS" ))
1763 p_tk->fmt.i_codec = VLC_CODEC_SSA;
1764 p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1765 if( p_tk->i_extra_data )
1766 fill_extra_data( p_tk, 0 );
1768 else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
1770 p_tk->fmt.i_codec = VLC_CODEC_SPU;
1771 p_tk->b_no_duration = true;
1772 if( p_tk->i_extra_data )
1775 char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
1776 if( psz_buf != NULL )
1778 memcpy( psz_buf, p_tk->p_extra_data , p_tk->i_extra_data );
1779 psz_buf[p_tk->i_extra_data] = '\0';
1781 psz_start = strstr( psz_buf, "size:" );
1783 vobsub_size_parse( psz_start,
1784 &p_tk->fmt.subs.spu.i_original_frame_width,
1785 &p_tk->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1787 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1788 p_tk->fmt.subs.spu.i_original_frame_width,
1789 p_tk->fmt.subs.spu.i_original_frame_height );
1793 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1796 psz_start = strstr( psz_buf, "palette:" );
1798 vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1800 p_tk->fmt.subs.spu.palette[0] = 0xBeef;
1801 msg_Dbg( &sys.demuxer, "vobsub palette read" );
1805 msg_Warn( &sys.demuxer, "reading original palette failed" );
1811 else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
1813 p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
1815 else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
1817 p_tk->fmt.i_cat = NAV_ES;
1821 msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
1822 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );