1 /*****************************************************************************
2 * matroska_segment_parse.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"
31 #include "Ebml_parser.hpp"
33 /*****************************************************************************
34 * Some functions to manipulate memory
35 *****************************************************************************/
36 static inline char * ToUTF8( const UTFstring &u )
38 return strdup( u.GetUTF8().c_str() );
41 /*****************************************************************************
43 *****************************************************************************/
44 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
52 stream_Control( sys.demuxer.s, STREAM_CAN_SEEK, &b_seekable );
56 ep = new EbmlParser( &es, seekhead, &sys.demuxer );
58 while( ( l = ep->Get() ) != NULL )
60 if( MKV_IS_ID( l, KaxSeek ) )
62 EbmlId id = EBML_ID(EbmlVoid);
66 msg_Dbg( &sys.demuxer, "| | + Seek" );
69 while( ( l = ep->Get() ) != NULL )
71 if( MKV_IS_ID( l, KaxSeekID ) )
73 KaxSeekID &sid = *(KaxSeekID*)l;
74 sid.ReadData( es.I_O() );
75 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
77 else if( MKV_IS_ID( l, KaxSeekPosition ) )
79 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
80 spos.ReadData( es.I_O() );
81 i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) );
85 /* Many mkvmerge files hit this case. It seems to be a broken SeekHead */
86 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
93 if( id == EBML_ID(KaxCues) )
95 msg_Dbg( &sys.demuxer, "| - cues at %"PRId64, i_pos );
96 LoadSeekHeadItem( EBML_INFO(KaxCues), i_pos );
98 else if( id == EBML_ID(KaxInfo) )
100 msg_Dbg( &sys.demuxer, "| - info at %"PRId64, i_pos );
101 LoadSeekHeadItem( EBML_INFO(KaxInfo), i_pos );
103 else if( id == EBML_ID(KaxChapters) )
105 msg_Dbg( &sys.demuxer, "| - chapters at %"PRId64, i_pos );
106 LoadSeekHeadItem( EBML_INFO(KaxChapters), i_pos );
108 else if( id == EBML_ID(KaxTags) )
110 msg_Dbg( &sys.demuxer, "| - tags at %"PRId64, i_pos );
111 LoadSeekHeadItem( EBML_INFO(KaxTags), i_pos );
113 else if( id == EBML_ID(KaxSeekHead) )
115 msg_Dbg( &sys.demuxer, "| - chained seekhead at %"PRId64, i_pos );
116 LoadSeekHeadItem( EBML_INFO(KaxSeekHead), i_pos );
118 else if( id == EBML_ID(KaxTracks) )
120 msg_Dbg( &sys.demuxer, "| - tracks at %"PRId64, i_pos );
121 LoadSeekHeadItem( EBML_INFO(KaxTracks), i_pos );
123 else if( id == EBML_ID(KaxAttachments) )
125 msg_Dbg( &sys.demuxer, "| - attachments at %"PRId64, i_pos );
126 LoadSeekHeadItem( EBML_INFO(KaxAttachments), i_pos );
130 msg_Dbg( &sys.demuxer, "| - unknown seekhead reference at %"PRId64, i_pos );
135 msg_Dbg( &sys.demuxer, "| | + ParseSeekHead Unknown (%s)", typeid(*l).name() );
142 * Helper function to print the mkv parse tree
144 static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
149 msg_Err( &demuxer, "MKV tree is too deep" );
152 va_start( args, psz_format );
153 static const char psz_foo[] = "| | | | | | | | | |";
154 char *psz_foo2 = (char*)malloc( i_level * 4 + 3 + strlen( psz_format ) );
155 strncpy( psz_foo2, psz_foo, 4 * i_level );
156 psz_foo2[ 4 * i_level ] = '+';
157 psz_foo2[ 4 * i_level + 1 ] = ' ';
158 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
159 msg_GenericVa( &demuxer,VLC_MSG_DBG, "mkv", psz_foo2, args );
165 /*****************************************************************************
167 *****************************************************************************/
168 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
170 bool bSupported = true;
173 mkv_track_t *tk = new mkv_track_t();
174 memset( tk, 0, sizeof( mkv_track_t ) );
176 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
177 tk->fmt.psz_language = strdup("English");
178 tk->fmt.psz_description = NULL;
180 tk->b_default = true;
181 tk->b_enabled = true;
182 tk->b_forced = false;
183 tk->b_silent = false;
184 tk->i_number = tracks.size() - 1;
185 tk->i_extra_data = 0;
186 tk->p_extra_data = NULL;
187 tk->psz_codec = NULL;
188 tk->b_dts_only = false;
189 tk->i_default_duration = 0;
190 tk->f_timecodescale = 1.0;
192 tk->b_inited = false;
194 tk->p_data_init = NULL;
196 tk->psz_codec_name = NULL;
197 tk->psz_codec_settings = NULL;
198 tk->psz_codec_info_url = NULL;
199 tk->psz_codec_download_url = NULL;
201 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
202 tk->p_compression_data = NULL;
204 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
206 for( size_t i = 0; i < m->ListSize(); i++ )
208 EbmlElement *l = (*m)[i];
210 if( MKV_IS_ID( l, KaxTrackNumber ) )
212 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
214 tk->i_number = uint32( tnum );
215 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
217 else if( MKV_IS_ID( l, KaxTrackUID ) )
219 KaxTrackUID &tuid = *(KaxTrackUID*)l;
221 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
223 else if( MKV_IS_ID( l, KaxTrackType ) )
225 const char *psz_type;
226 KaxTrackType &ttype = *(KaxTrackType*)l;
228 switch( uint8(ttype) )
232 tk->fmt.i_cat = AUDIO_ES;
236 tk->fmt.i_cat = VIDEO_ES;
239 psz_type = "subtitle";
240 tk->fmt.i_cat = SPU_ES;
243 psz_type = "buttons";
244 tk->fmt.i_cat = SPU_ES;
247 psz_type = "unknown";
248 tk->fmt.i_cat = UNKNOWN_ES;
252 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
254 else if( MKV_IS_ID( l, KaxTrackFlagEnabled ) ) // UNUSED
256 KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
258 tk->b_enabled = uint32( fenb );
259 msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u", uint32( fenb ) );
261 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
263 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
265 tk->b_default = uint32( fdef );
266 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
268 else if( MKV_IS_ID( l, KaxTrackFlagForced ) ) // UNUSED
270 KaxTrackFlagForced &ffor = *(KaxTrackFlagForced*)l;
271 tk->b_forced = uint32( ffor );
273 msg_Dbg( &sys.demuxer, "| | | + Track Forced=%u", uint32( ffor ) );
275 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) ) // UNUSED
277 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
279 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
281 else if( MKV_IS_ID( l, KaxTrackMinCache ) ) // UNUSED
283 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
285 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
287 else if( MKV_IS_ID( l, KaxTrackMaxCache ) ) // UNUSED
289 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
291 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
293 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
295 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
297 tk->i_default_duration = uint64(defd);
298 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%"PRId64, uint64(defd) );
300 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
302 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
304 tk->f_timecodescale = float( ttcs );
305 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
307 else if( MKV_IS_ID( l, KaxMaxBlockAdditionID ) ) // UNUSED
309 KaxMaxBlockAdditionID &mbl = *(KaxMaxBlockAdditionID*)l;
311 msg_Dbg( &sys.demuxer, "| | | + Track Max BlockAdditionID=%d", uint32( mbl ) );
313 else if( MKV_IS_ID( l, KaxTrackName ) )
315 KaxTrackName &tname = *(KaxTrackName*)l;
317 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
318 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
320 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
322 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
324 free( tk->fmt.psz_language );
325 tk->fmt.psz_language = strdup( string( lang ).c_str() );
326 msg_Dbg( &sys.demuxer,
327 "| | | + Track Language=`%s'", tk->fmt.psz_language );
329 else if( MKV_IS_ID( l, KaxCodecID ) )
331 KaxCodecID &codecid = *(KaxCodecID*)l;
333 tk->psz_codec = strdup( string( codecid ).c_str() );
334 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
336 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
338 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
340 tk->i_extra_data = cpriv.GetSize();
341 if( tk->i_extra_data > 0 )
343 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
344 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
346 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
348 else if( MKV_IS_ID( l, KaxCodecName ) )
350 KaxCodecName &cname = *(KaxCodecName*)l;
352 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
353 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
356 else if( MKV_IS_ID( l, KaxCodecDecodeAll ) ) // UNUSED
358 KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
360 msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u", uint8( cdall ) );
362 else if( MKV_IS_ID( l, KaxTrackOverlay ) ) // UNUSED
364 KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
366 msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u", uint32( tovr ) );
368 else if( MKV_IS_ID( l, KaxContentEncodings ) )
370 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
371 MkvTree( sys.demuxer, 3, "Content Encodings" );
372 if ( cencs->ListSize() > 1 )
374 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
377 for( size_t j = 0; j < cencs->ListSize(); j++ )
379 EbmlElement *l2 = (*cencs)[j];
380 if( MKV_IS_ID( l2, KaxContentEncoding ) )
382 MkvTree( sys.demuxer, 4, "Content Encoding" );
383 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
384 for( size_t k = 0; k < cenc->ListSize(); k++ )
386 EbmlElement *l3 = (*cenc)[k];
387 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
389 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
390 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
392 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
394 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
395 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
397 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
399 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
400 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
402 else if( MKV_IS_ID( l3, KaxContentCompression ) )
404 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
405 MkvTree( sys.demuxer, 5, "Content Compression" );
406 //Default compression type is 0 (Zlib)
407 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
408 for( size_t n = 0; n < compr->ListSize(); n++ )
410 EbmlElement *l4 = (*compr)[n];
411 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
413 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
414 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
415 tk->i_compression_type = uint32( compalg );
416 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
417 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
419 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
423 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
425 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
429 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
433 // ContentEncryption Unsupported
436 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
442 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
446 // else if( MKV_IS_ID( l, KaxCodecSettings) ) DEPRECATED by matroska
448 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
450 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
451 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
453 // else if( MKV_IS_ID( l, KaxCodecInfoURL) ) DEPRECATED by matroska
455 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
457 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
458 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
460 // else if( MKV_IS_ID( l, KaxCodecDownloadURL) ) DEPRECATED by matroska
462 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
464 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
465 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
467 else if( MKV_IS_ID( l, KaxTrackVideo ) )
469 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
470 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
471 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
473 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
476 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
477 tk->fmt.video.i_frame_rate = 1000000;
479 for( unsigned int j = 0; j < tkv->ListSize(); j++ )
481 EbmlElement *l = (*tkv)[j];
482 if( MKV_IS_ID( l, KaxVideoFlagInterlaced ) ) // UNUSED
484 KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)l;
486 msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
488 else if( MKV_IS_ID( l, KaxVideoStereoMode ) ) // UNUSED
490 KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)l;
492 msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
494 else if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
496 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
498 tk->fmt.video.i_width += uint16( vwidth );
499 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
501 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
503 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
505 tk->fmt.video.i_height += uint16( vheight );
506 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
508 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
510 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
512 i_display_width = uint16( vwidth );
513 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
515 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
517 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
519 i_display_height = uint16( vheight );
520 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
522 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
524 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
526 i_crop_bottom = uint16( cropval );
527 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
529 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
531 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
533 i_crop_top = uint16( cropval );
534 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
536 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
538 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
540 i_crop_right = uint16( cropval );
541 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
543 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
545 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
547 i_crop_left = uint16( cropval );
548 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
550 else if( MKV_IS_ID( l, KaxVideoDisplayUnit ) )
552 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
554 i_display_unit = uint8( vdmode );
555 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
556 i_display_unit == 0 ? "pixels" : ( i_display_unit == 1 ? "centimeters": "inches" ) );
558 else if( MKV_IS_ID( l, KaxVideoAspectRatio ) ) // UNUSED
560 KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
562 msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
564 // ColourSpace UNUSED
565 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
567 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
569 tk->f_fps = float( vfps );
570 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
572 // else if( MKV_IS_ID( l, KaxVideoGamma) ) //DEPRECATED by Matroska
574 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
576 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
580 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
583 if( i_display_height && i_display_width )
585 tk->fmt.video.i_sar_num = i_display_width * tk->fmt.video.i_height;
586 tk->fmt.video.i_sar_den = i_display_height * tk->fmt.video.i_width;
588 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
590 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
591 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
592 tk->fmt.video.i_x_offset = i_crop_left;
593 tk->fmt.video.i_y_offset = i_crop_top;
594 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
595 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
597 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
598 we do not support this atm */
600 else if( MKV_IS_ID( l, KaxTrackAudio ) )
602 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
604 /* Initialize default values */
605 tk->fmt.audio.i_channels = 1;
606 tk->fmt.audio.i_rate = 8000;
608 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
610 for( unsigned int j = 0; j < tka->ListSize(); j++ )
612 EbmlElement *l = (*tka)[j];
614 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
616 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
618 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
619 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
621 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
623 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
625 tk->fmt.audio.i_rate = (int)float( afreq );
626 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
628 else if( MKV_IS_ID( l, KaxAudioChannels ) )
630 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
632 tk->fmt.audio.i_channels = uint8( achan );
633 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
635 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
637 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
639 tk->fmt.audio.i_bitspersample = uint8( abits );
640 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
644 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
650 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
657 tracks.push_back( tk );
661 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
666 /*****************************************************************************
668 *****************************************************************************/
669 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
672 int i_upper_level = 0;
674 /* Master elements */
675 tracks->Read( es, EBML_CONTEXT(tracks), i_upper_level, el, true );
677 for( size_t i = 0; i < tracks->ListSize(); i++ )
679 EbmlElement *l = (*tracks)[i];
681 if( MKV_IS_ID( l, KaxTrackEntry ) )
683 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
687 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
692 /*****************************************************************************
694 *****************************************************************************/
695 void matroska_segment_c::ParseInfo( KaxInfo *info )
699 int i_upper_level = 0;
701 /* Master elements */
702 m = static_cast<EbmlMaster *>(info);
703 m->Read( es, EBML_CONTEXT(info), i_upper_level, el, true );
705 for( size_t i = 0; i < m->ListSize(); i++ )
707 EbmlElement *l = (*m)[i];
709 if( MKV_IS_ID( l, KaxSegmentUID ) )
711 if ( p_segment_uid == NULL )
712 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
714 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
716 else if( MKV_IS_ID( l, KaxPrevUID ) )
718 if ( p_prev_segment_uid == NULL )
720 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
721 b_ref_external_segments = true;
724 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
726 else if( MKV_IS_ID( l, KaxNextUID ) )
728 if ( p_next_segment_uid == NULL )
730 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
731 b_ref_external_segments = true;
734 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
736 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
738 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
740 i_timescale = uint64(tcs);
742 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%"PRId64,
745 else if( MKV_IS_ID( l, KaxDuration ) )
747 KaxDuration &dur = *(KaxDuration*)l;
749 i_duration = mtime_t( double( dur ) );
751 msg_Dbg( &sys.demuxer, "| | + Duration=%"PRId64,
754 else if( MKV_IS_ID( l, KaxMuxingApp ) )
756 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
758 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
760 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
761 psz_muxing_application );
763 else if( MKV_IS_ID( l, KaxWritingApp ) )
765 KaxWritingApp &wapp = *(KaxWritingApp*)l;
767 psz_writing_application = ToUTF8( UTFstring( wapp ) );
769 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
770 psz_writing_application );
772 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
774 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
776 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
778 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
779 psz_segment_filename );
781 else if( MKV_IS_ID( l, KaxTitle ) )
783 KaxTitle &title = *(KaxTitle*)l;
785 psz_title = ToUTF8( UTFstring( title ) );
787 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
789 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
791 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
793 families.push_back( new KaxSegmentFamily(*uid) );
795 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
797 else if( MKV_IS_ID( l, KaxDateUTC ) )
799 KaxDateUTC &date = *(KaxDateUTC*)l;
804 i_date = date.GetEpochDate();
805 if( gmtime_r( &i_date, &tmres ) &&
806 strftime( buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y",
809 psz_date_utc = strdup( buffer );
810 msg_Dbg( &sys.demuxer, "| | + Date=%s", buffer );
813 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
815 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
816 chapter_translation_c *p_translate = new chapter_translation_c();
818 p_trans->Read( es, EBML_CONTEXT(p_trans), i_upper_level, el, true );
819 for( size_t j = 0; j < p_trans->ListSize(); j++ )
821 EbmlElement *l = (*p_trans)[j];
823 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
825 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
827 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
829 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
831 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
833 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
837 translations.push_back( p_translate );
841 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
845 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
846 i_duration = mtime_t(f_dur);
847 if( !i_duration ) i_duration = -1;
851 /*****************************************************************************
853 *****************************************************************************/
854 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
856 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
857 for( size_t i = 0; i < ca->ListSize(); i++ )
859 EbmlElement *l = (*ca)[i];
861 if( MKV_IS_ID( l, KaxChapterUID ) )
863 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
864 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %"PRIu64"", chapters.i_uid );
866 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
868 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
869 chapters.b_display_seekpoint = uint8( flag ) == 0;
871 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
873 else if( MKV_IS_ID( l, KaxChapterSegmentUID ) )
875 chapters.p_segment_uid = new KaxChapterSegmentUID( *static_cast<KaxChapterSegmentUID*>(l) );
876 b_ref_external_segments = true;
877 msg_Dbg( &sys.demuxer, "| | | | + ChapterSegmentUID= %u", *(uint32*)chapters.p_segment_uid->GetBuffer() );
879 else if( MKV_IS_ID( l, KaxChapterSegmentEditionUID ) )
881 chapters.p_segment_edition_uid = new KaxChapterSegmentEditionUID( *static_cast<KaxChapterSegmentEditionUID*>(l) );
882 msg_Dbg( &sys.demuxer, "| | | | + ChapterSegmentEditionUID= %u",
883 #if LIBMATROSKA_VERSION < 0x010300
884 *(uint32*)chapters.p_segment_edition_uid->GetBuffer()
886 *(uint32*)chapters.p_segment_edition_uid
890 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
892 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
893 chapters.i_start_time = uint64( start ) / INT64_C(1000);
895 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %"PRId64"", chapters.i_start_time );
897 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
899 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
900 chapters.i_end_time = uint64( end ) / INT64_C(1000);
902 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %"PRId64"", chapters.i_end_time );
904 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
906 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
908 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
909 for( size_t j = 0; j < cd->ListSize(); j++ )
911 EbmlElement *l= (*cd)[j];
913 if( MKV_IS_ID( l, KaxChapterString ) )
915 KaxChapterString &name =*(KaxChapterString*)l;
916 for ( int k = 0; k < i_level; k++)
917 chapters.psz_name += '+';
918 chapters.psz_name += ' ';
919 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
920 chapters.psz_name += psz_tmp_utf8;
921 chapters.b_user_display = true;
923 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
924 free( psz_tmp_utf8 );
926 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
928 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
929 const char *psz = string( lang ).c_str();
931 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
933 else if( MKV_IS_ID( l, KaxChapterCountry ) )
935 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
936 const char *psz = string( ct ).c_str();
938 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
942 else if( MKV_IS_ID( l, KaxChapterProcess ) )
944 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
946 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
947 chapter_codec_cmds_c *p_ccodec = NULL;
949 for( size_t j = 0; j < cp->ListSize(); j++ )
951 EbmlElement *k= (*cp)[j];
953 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
955 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
956 if ( uint32(*p_codec_id) == 0 )
957 p_ccodec = new matroska_script_codec_c( sys );
958 else if ( uint32(*p_codec_id) == 1 )
959 p_ccodec = new dvd_chapter_codec_c( sys );
964 if ( p_ccodec != NULL )
966 for( size_t j = 0; j < cp->ListSize(); j++ )
968 EbmlElement *k= (*cp)[j];
970 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
972 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
973 p_ccodec->SetPrivate( *p_private );
975 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
977 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
980 chapters.codecs.push_back( p_ccodec );
983 else if( MKV_IS_ID( l, KaxChapterAtom ) )
985 chapter_item_c *new_sub_chapter = new chapter_item_c();
986 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
987 new_sub_chapter->p_parent = &chapters;
988 chapters.sub_chapters.push_back( new_sub_chapter );
993 /*****************************************************************************
995 *****************************************************************************/
996 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
999 int i_upper_level = 0;
1001 attachments->Read( es, EBML_CONTEXT(attachments), i_upper_level, el, true );
1003 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
1005 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
1007 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
1008 char *psz_tmp_utf8 = ToUTF8( UTFstring( GetChild<KaxFileName>( *attachedFile ) ) );
1009 std::string attached_filename(psz_tmp_utf8);
1011 attachment_c *new_attachment = new attachment_c( attached_filename,
1012 GetChild<KaxMimeType>( *attachedFile ),
1013 img_data.GetSize() );
1015 msg_Dbg( &sys.demuxer, "| | - %s (%s)", new_attachment->fileName(), new_attachment->mimeType() );
1017 if( new_attachment->init() )
1019 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
1020 sys.stored_attachments.push_back( new_attachment );
1021 if( !strncmp( new_attachment->mimeType(), "image/", 6 ) )
1024 if( asprintf( &psz_url, "attachment://%s",
1025 new_attachment->fileName() ) == -1 )
1028 sys.meta = vlc_meta_New();
1029 vlc_meta_SetArtURL( sys.meta, psz_url );
1035 delete new_attachment;
1038 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
1042 /*****************************************************************************
1044 *****************************************************************************/
1045 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
1048 int i_upper_level = 0;
1050 /* Master elements */
1051 chapters->Read( es, EBML_CONTEXT(chapters), i_upper_level, el, true );
1053 for( size_t i = 0; i < chapters->ListSize(); i++ )
1055 EbmlElement *l = (*chapters)[i];
1057 if( MKV_IS_ID( l, KaxEditionEntry ) )
1059 chapter_edition_c *p_edition = new chapter_edition_c();
1061 EbmlMaster *E = static_cast<EbmlMaster *>(l );
1062 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
1063 for( size_t j = 0; j < E->ListSize(); j++ )
1065 EbmlElement *l = (*E)[j];
1067 if( MKV_IS_ID( l, KaxChapterAtom ) )
1069 chapter_item_c *new_sub_chapter = new chapter_item_c();
1070 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1071 p_edition->sub_chapters.push_back( new_sub_chapter );
1073 else if( MKV_IS_ID( l, KaxEditionUID ) )
1075 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
1077 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
1079 p_edition->b_ordered = var_InheritBool( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
1081 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
1083 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
1084 i_default_edition = stored_editions.size();
1086 else if( MKV_IS_ID( l, KaxEditionFlagHidden ) )
1088 // FIXME to implement
1092 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
1095 stored_editions.push_back( p_edition );
1099 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
1104 void matroska_segment_c::ParseCluster( bool b_update_start_time )
1108 int i_upper_level = 0;
1110 /* Master elements */
1111 m = static_cast<EbmlMaster *>( cluster );
1112 m->Read( es, EBML_CONTEXT(cluster), i_upper_level, el, true );
1114 for( unsigned int i = 0; i < m->ListSize(); i++ )
1116 EbmlElement *l = (*m)[i];
1118 if( MKV_IS_ID( l, KaxClusterTimecode ) )
1120 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
1122 cluster->InitTimecode( uint64( ctc ), i_timescale );
1127 if( b_update_start_time )
1128 i_start_time = cluster->GlobalTimecode() / 1000;