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"
31 /*****************************************************************************
32 * Some functions to manipulate memory
33 *****************************************************************************/
34 static inline char * ToUTF8( const UTFstring &u )
36 return strdup( u.GetUTF8().c_str() );
39 /*****************************************************************************
41 *****************************************************************************/
42 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
50 stream_Control( sys.demuxer.s, STREAM_CAN_SEEK, &b_seekable );
54 ep = new EbmlParser( &es, seekhead, &sys.demuxer );
56 while( ( l = ep->Get() ) != NULL )
58 if( MKV_IS_ID( l, KaxSeek ) )
60 EbmlId id = EBML_ID(EbmlVoid);
64 msg_Dbg( &sys.demuxer, "| | + Seek" );
67 while( ( l = ep->Get() ) != NULL )
69 if( MKV_IS_ID( l, KaxSeekID ) )
71 KaxSeekID &sid = *(KaxSeekID*)l;
72 sid.ReadData( es.I_O() );
73 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
75 else if( MKV_IS_ID( l, KaxSeekPosition ) )
77 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
78 spos.ReadData( es.I_O() );
79 i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) );
83 /* Many mkvmerge files hit this case. It seems to be a broken SeekHead */
84 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
91 if( id == EBML_ID(KaxCues) )
93 msg_Dbg( &sys.demuxer, "| - cues at %"PRId64, i_pos );
94 LoadSeekHeadItem( EBML_INFO(KaxCues), i_pos );
96 else if( id == EBML_ID(KaxInfo) )
98 msg_Dbg( &sys.demuxer, "| - info at %"PRId64, i_pos );
99 LoadSeekHeadItem( EBML_INFO(KaxInfo), i_pos );
101 else if( id == EBML_ID(KaxChapters) )
103 msg_Dbg( &sys.demuxer, "| - chapters at %"PRId64, i_pos );
104 LoadSeekHeadItem( EBML_INFO(KaxChapters), i_pos );
106 else if( id == EBML_ID(KaxTags) )
108 msg_Dbg( &sys.demuxer, "| - tags at %"PRId64, i_pos );
109 LoadSeekHeadItem( EBML_INFO(KaxTags), i_pos );
111 else if( id == EBML_ID(KaxSeekHead) )
113 msg_Dbg( &sys.demuxer, "| - chained seekhead at %"PRId64, i_pos );
114 LoadSeekHeadItem( EBML_INFO(KaxSeekHead), i_pos );
116 else if( id == EBML_ID(KaxTracks) )
118 msg_Dbg( &sys.demuxer, "| - tracks at %"PRId64, i_pos );
119 LoadSeekHeadItem( EBML_INFO(KaxTracks), i_pos );
121 else if( id == EBML_ID(KaxAttachments) )
123 msg_Dbg( &sys.demuxer, "| - attachments at %"PRId64, i_pos );
124 LoadSeekHeadItem( EBML_INFO(KaxAttachments), i_pos );
128 msg_Dbg( &sys.demuxer, "| - unknown seekhead reference at %"PRId64, i_pos );
133 msg_Dbg( &sys.demuxer, "| | + ParseSeekHead Unknown (%s)", typeid(*l).name() );
140 * Helper function to print the mkv parse tree
142 static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
147 msg_Err( &demuxer, "MKV tree is too deep" );
150 va_start( args, psz_format );
151 static const char psz_foo[] = "| | | | | | | | | |";
152 char *psz_foo2 = (char*)malloc( i_level * 4 + 3 + strlen( psz_format ) );
153 strncpy( psz_foo2, psz_foo, 4 * i_level );
154 psz_foo2[ 4 * i_level ] = '+';
155 psz_foo2[ 4 * i_level + 1 ] = ' ';
156 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
157 msg_GenericVa( &demuxer,VLC_MSG_DBG, "mkv", psz_foo2, args );
163 /*****************************************************************************
165 *****************************************************************************/
166 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
168 bool bSupported = true;
171 mkv_track_t *tk = new mkv_track_t();
172 memset( tk, 0, sizeof( mkv_track_t ) );
174 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
175 tk->fmt.psz_language = strdup("English");
176 tk->fmt.psz_description = NULL;
178 tk->b_default = true;
179 tk->b_enabled = true;
180 tk->b_silent = false;
181 tk->i_number = tracks.size() - 1;
182 tk->i_extra_data = 0;
183 tk->p_extra_data = NULL;
184 tk->psz_codec = NULL;
185 tk->b_dts_only = false;
186 tk->i_default_duration = 0;
187 tk->f_timecodescale = 1.0;
189 tk->b_inited = false;
191 tk->p_data_init = NULL;
193 tk->psz_codec_name = NULL;
194 tk->psz_codec_settings = NULL;
195 tk->psz_codec_info_url = NULL;
196 tk->psz_codec_download_url = NULL;
198 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
199 tk->p_compression_data = NULL;
201 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
203 for( size_t i = 0; i < m->ListSize(); i++ )
205 EbmlElement *l = (*m)[i];
207 if( MKV_IS_ID( l, KaxTrackNumber ) )
209 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
211 tk->i_number = uint32( tnum );
212 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
214 else if( MKV_IS_ID( l, KaxTrackUID ) )
216 KaxTrackUID &tuid = *(KaxTrackUID*)l;
218 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
220 else if( MKV_IS_ID( l, KaxTrackType ) )
222 const char *psz_type;
223 KaxTrackType &ttype = *(KaxTrackType*)l;
225 switch( uint8(ttype) )
229 tk->fmt.i_cat = AUDIO_ES;
233 tk->fmt.i_cat = VIDEO_ES;
236 psz_type = "subtitle";
237 tk->fmt.i_cat = SPU_ES;
240 psz_type = "buttons";
241 tk->fmt.i_cat = SPU_ES;
244 psz_type = "unknown";
245 tk->fmt.i_cat = UNKNOWN_ES;
249 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
251 else if( MKV_IS_ID( l, KaxTrackFlagEnabled ) ) // UNUSED
253 KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
255 // tk->b_enabled = uint32( fenb );
256 msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u", uint32( fenb ) );
258 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
260 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
262 tk->b_default = uint32( fdef );
263 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
265 else if( MKV_IS_ID( l, KaxTrackFlagForced ) ) // UNUSED
267 KaxTrackFlagForced &ffor = *(KaxTrackFlagForced*)l;
269 msg_Dbg( &sys.demuxer, "| | | + Track Forced=%u", uint32( ffor ) );
271 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) ) // UNUSED
273 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
275 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
277 else if( MKV_IS_ID( l, KaxTrackMinCache ) ) // UNUSED
279 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
281 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
283 else if( MKV_IS_ID( l, KaxTrackMaxCache ) ) // UNUSED
285 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
287 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
289 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
291 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
293 tk->i_default_duration = uint64(defd);
294 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%"PRId64, uint64(defd) );
296 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
298 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
300 tk->f_timecodescale = float( ttcs );
301 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
303 else if( MKV_IS_ID( l, KaxMaxBlockAdditionID ) ) // UNUSED
305 KaxMaxBlockAdditionID &mbl = *(KaxMaxBlockAdditionID*)l;
307 msg_Dbg( &sys.demuxer, "| | | + Track Max BlockAdditionID=%d", uint32( mbl ) );
309 else if( MKV_IS_ID( l, KaxTrackName ) )
311 KaxTrackName &tname = *(KaxTrackName*)l;
313 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
314 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
316 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
318 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
320 free( tk->fmt.psz_language );
321 tk->fmt.psz_language = strdup( string( lang ).c_str() );
322 msg_Dbg( &sys.demuxer,
323 "| | | + Track Language=`%s'", tk->fmt.psz_language );
325 else if( MKV_IS_ID( l, KaxCodecID ) )
327 KaxCodecID &codecid = *(KaxCodecID*)l;
329 tk->psz_codec = strdup( string( codecid ).c_str() );
330 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
332 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
334 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
336 tk->i_extra_data = cpriv.GetSize();
337 if( tk->i_extra_data > 0 )
339 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
340 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
342 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
344 else if( MKV_IS_ID( l, KaxCodecName ) )
346 KaxCodecName &cname = *(KaxCodecName*)l;
348 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
349 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
352 else if( MKV_IS_ID( l, KaxCodecDecodeAll ) ) // UNUSED
354 KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
356 msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u", uint8( cdall ) );
358 else if( MKV_IS_ID( l, KaxTrackOverlay ) ) // UNUSED
360 KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
362 msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u", uint32( tovr ) );
364 else if( MKV_IS_ID( l, KaxContentEncodings ) )
366 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
367 MkvTree( sys.demuxer, 3, "Content Encodings" );
368 if ( cencs->ListSize() > 1 )
370 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
373 for( size_t j = 0; j < cencs->ListSize(); j++ )
375 EbmlElement *l2 = (*cencs)[j];
376 if( MKV_IS_ID( l2, KaxContentEncoding ) )
378 MkvTree( sys.demuxer, 4, "Content Encoding" );
379 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
380 for( size_t k = 0; k < cenc->ListSize(); k++ )
382 EbmlElement *l3 = (*cenc)[k];
383 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
385 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
386 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
388 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
390 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
391 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
393 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
395 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
396 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
398 else if( MKV_IS_ID( l3, KaxContentCompression ) )
400 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
401 MkvTree( sys.demuxer, 5, "Content Compression" );
402 //Default compression type is 0 (Zlib)
403 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
404 for( size_t n = 0; n < compr->ListSize(); n++ )
406 EbmlElement *l4 = (*compr)[n];
407 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
409 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
410 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
411 tk->i_compression_type = uint32( compalg );
412 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
413 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
415 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
419 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
421 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
425 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
429 // ContentEncryption Unsupported
432 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
438 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
442 // else if( MKV_IS_ID( l, KaxCodecSettings) ) DEPRECATED by matroska
444 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
446 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
447 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
449 // else if( MKV_IS_ID( l, KaxCodecInfoURL) ) DEPRECATED by matroska
451 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
453 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
454 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
456 // else if( MKV_IS_ID( l, KaxCodecDownloadURL) ) DEPRECATED by matroska
458 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
460 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
461 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
463 else if( MKV_IS_ID( l, KaxTrackVideo ) )
465 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
466 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
467 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
469 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
472 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
473 tk->fmt.video.i_frame_rate = 1000000;
475 for( unsigned int j = 0; j < tkv->ListSize(); j++ )
477 EbmlElement *l = (*tkv)[j];
478 if( MKV_IS_ID( l, KaxVideoFlagInterlaced ) ) // UNUSED
480 KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)l;
482 msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
484 else if( MKV_IS_ID( l, KaxVideoStereoMode ) ) // UNUSED
486 KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)l;
488 msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
490 else if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
492 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
494 tk->fmt.video.i_width += uint16( vwidth );
495 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
497 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
499 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
501 tk->fmt.video.i_height += uint16( vheight );
502 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
504 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
506 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
508 i_display_width = uint16( vwidth );
509 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
511 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
513 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
515 i_display_height = uint16( vheight );
516 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
518 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
520 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
522 i_crop_bottom = uint16( cropval );
523 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
525 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
527 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
529 i_crop_top = uint16( cropval );
530 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
532 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
534 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
536 i_crop_right = uint16( cropval );
537 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
539 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
541 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
543 i_crop_left = uint16( cropval );
544 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
546 else if( MKV_IS_ID( l, KaxVideoDisplayUnit ) )
548 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
550 i_display_unit = uint8( vdmode );
551 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
552 uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
554 else if( MKV_IS_ID( l, KaxVideoAspectRatio ) ) // UNUSED
556 KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
558 msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
560 // ColourSpace UNUSED
561 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
563 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
565 tk->f_fps = float( vfps );
566 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
568 // else if( MKV_IS_ID( l, KaxVideoGamma) ) //DEPRECATED by Matroska
570 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
572 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
576 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
579 if( i_display_height && i_display_width )
581 tk->fmt.video.i_sar_num = i_display_width * tk->fmt.video.i_height;
582 tk->fmt.video.i_sar_den = i_display_height * tk->fmt.video.i_width;
584 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
586 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
587 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
588 tk->fmt.video.i_x_offset = i_crop_left;
589 tk->fmt.video.i_y_offset = i_crop_top;
590 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
591 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
593 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
594 we do not support this atm */
596 else if( MKV_IS_ID( l, KaxTrackAudio ) )
598 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
600 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
602 for( unsigned int j = 0; j < tka->ListSize(); j++ )
604 EbmlElement *l = (*tka)[j];
606 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
608 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
610 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
611 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
613 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
615 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
617 tk->fmt.audio.i_rate = (int)float( afreq );
618 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
620 else if( MKV_IS_ID( l, KaxAudioChannels ) )
622 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
624 tk->fmt.audio.i_channels = uint8( achan );
625 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
627 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
629 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
631 tk->fmt.audio.i_bitspersample = uint8( abits );
632 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
636 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
642 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
649 tracks.push_back( tk );
653 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
658 /*****************************************************************************
660 *****************************************************************************/
661 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
664 int i_upper_level = 0;
666 /* Master elements */
667 tracks->Read( es, EBML_CONTEXT(tracks), i_upper_level, el, true );
669 for( size_t i = 0; i < tracks->ListSize(); i++ )
671 EbmlElement *l = (*tracks)[i];
673 if( MKV_IS_ID( l, KaxTrackEntry ) )
675 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
679 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
684 /*****************************************************************************
686 *****************************************************************************/
687 void matroska_segment_c::ParseInfo( KaxInfo *info )
691 int i_upper_level = 0;
693 /* Master elements */
694 m = static_cast<EbmlMaster *>(info);
695 m->Read( es, EBML_CONTEXT(info), i_upper_level, el, true );
697 for( size_t i = 0; i < m->ListSize(); i++ )
699 EbmlElement *l = (*m)[i];
701 if( MKV_IS_ID( l, KaxSegmentUID ) )
703 if ( p_segment_uid == NULL )
704 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
706 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
708 else if( MKV_IS_ID( l, KaxPrevUID ) )
710 if ( p_prev_segment_uid == NULL )
711 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
713 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
715 else if( MKV_IS_ID( l, KaxNextUID ) )
717 if ( p_next_segment_uid == NULL )
718 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
720 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
722 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
724 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
726 i_timescale = uint64(tcs);
728 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%"PRId64,
731 else if( MKV_IS_ID( l, KaxDuration ) )
733 KaxDuration &dur = *(KaxDuration*)l;
735 i_duration = mtime_t( double( dur ) );
737 msg_Dbg( &sys.demuxer, "| | + Duration=%"PRId64,
740 else if( MKV_IS_ID( l, KaxMuxingApp ) )
742 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
744 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
746 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
747 psz_muxing_application );
749 else if( MKV_IS_ID( l, KaxWritingApp ) )
751 KaxWritingApp &wapp = *(KaxWritingApp*)l;
753 psz_writing_application = ToUTF8( UTFstring( wapp ) );
755 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
756 psz_writing_application );
758 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
760 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
762 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
764 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
765 psz_segment_filename );
767 else if( MKV_IS_ID( l, KaxTitle ) )
769 KaxTitle &title = *(KaxTitle*)l;
771 psz_title = ToUTF8( UTFstring( title ) );
773 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
775 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
777 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
779 families.push_back( new KaxSegmentFamily(*uid) );
781 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
783 else if( MKV_IS_ID( l, KaxDateUTC ) )
785 KaxDateUTC &date = *(KaxDateUTC*)l;
790 i_date = date.GetEpochDate();
791 if( gmtime_r( &i_date, &tmres ) &&
792 strftime( buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y",
795 psz_date_utc = strdup( buffer );
796 msg_Dbg( &sys.demuxer, "| | + Date=%s", buffer );
799 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
801 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
802 chapter_translation_c *p_translate = new chapter_translation_c();
804 p_trans->Read( es, EBML_CONTEXT(p_trans), i_upper_level, el, true );
805 for( size_t j = 0; j < p_trans->ListSize(); j++ )
807 EbmlElement *l = (*p_trans)[j];
809 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
811 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
813 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
815 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
817 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
819 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
823 translations.push_back( p_translate );
827 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
831 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
832 i_duration = mtime_t(f_dur);
836 /*****************************************************************************
838 *****************************************************************************/
839 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
841 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
842 for( size_t i = 0; i < ca->ListSize(); i++ )
844 EbmlElement *l = (*ca)[i];
846 if( MKV_IS_ID( l, KaxChapterUID ) )
848 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
849 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %"PRIu64"", chapters.i_uid );
851 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
853 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
854 chapters.b_display_seekpoint = uint8( flag ) == 0;
856 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
858 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
860 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
861 chapters.i_start_time = uint64( start ) / INT64_C(1000);
863 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %"PRId64"", chapters.i_start_time );
865 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
867 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
868 chapters.i_end_time = uint64( end ) / INT64_C(1000);
870 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %"PRId64"", chapters.i_end_time );
872 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
874 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
876 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
877 for( size_t j = 0; j < cd->ListSize(); j++ )
879 EbmlElement *l= (*cd)[j];
881 if( MKV_IS_ID( l, KaxChapterString ) )
883 KaxChapterString &name =*(KaxChapterString*)l;
884 for ( int k = 0; k < i_level; k++)
885 chapters.psz_name += '+';
886 chapters.psz_name += ' ';
887 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
888 chapters.psz_name += psz_tmp_utf8;
889 chapters.b_user_display = true;
891 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
892 free( psz_tmp_utf8 );
894 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
896 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
897 const char *psz = string( lang ).c_str();
899 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
901 else if( MKV_IS_ID( l, KaxChapterCountry ) )
903 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
904 const char *psz = string( ct ).c_str();
906 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
910 else if( MKV_IS_ID( l, KaxChapterProcess ) )
912 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
914 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
915 chapter_codec_cmds_c *p_ccodec = NULL;
917 for( size_t j = 0; j < cp->ListSize(); j++ )
919 EbmlElement *k= (*cp)[j];
921 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
923 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
924 if ( uint32(*p_codec_id) == 0 )
925 p_ccodec = new matroska_script_codec_c( sys );
926 else if ( uint32(*p_codec_id) == 1 )
927 p_ccodec = new dvd_chapter_codec_c( sys );
932 if ( p_ccodec != NULL )
934 for( size_t j = 0; j < cp->ListSize(); j++ )
936 EbmlElement *k= (*cp)[j];
938 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
940 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
941 p_ccodec->SetPrivate( *p_private );
943 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
945 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
948 chapters.codecs.push_back( p_ccodec );
951 else if( MKV_IS_ID( l, KaxChapterAtom ) )
953 chapter_item_c *new_sub_chapter = new chapter_item_c();
954 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
955 new_sub_chapter->psz_parent = &chapters;
956 chapters.sub_chapters.push_back( new_sub_chapter );
961 /*****************************************************************************
963 *****************************************************************************/
964 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
967 int i_upper_level = 0;
969 attachments->Read( es, EBML_CONTEXT(attachments), i_upper_level, el, true );
971 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
973 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
975 std::string psz_mime_type = GetChild<KaxMimeType>( *attachedFile );
976 KaxFileName &file_name = GetChild<KaxFileName>( *attachedFile );
977 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
979 attachment_c *new_attachment = new attachment_c();
983 new_attachment->psz_file_name = ToUTF8( UTFstring( file_name ) );
984 new_attachment->psz_mime_type = psz_mime_type;
985 new_attachment->i_size = img_data.GetSize();
986 new_attachment->p_data = malloc( img_data.GetSize() );
988 if( new_attachment->p_data )
990 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
991 sys.stored_attachments.push_back( new_attachment );
995 delete new_attachment;
999 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
1003 /*****************************************************************************
1005 *****************************************************************************/
1006 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
1009 int i_upper_level = 0;
1012 /* Master elements */
1013 chapters->Read( es, EBML_CONTEXT(chapters), i_upper_level, el, true );
1015 for( size_t i = 0; i < chapters->ListSize(); i++ )
1017 EbmlElement *l = (*chapters)[i];
1019 if( MKV_IS_ID( l, KaxEditionEntry ) )
1021 chapter_edition_c *p_edition = new chapter_edition_c();
1023 EbmlMaster *E = static_cast<EbmlMaster *>(l );
1024 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
1025 for( size_t j = 0; j < E->ListSize(); j++ )
1027 EbmlElement *l = (*E)[j];
1029 if( MKV_IS_ID( l, KaxChapterAtom ) )
1031 chapter_item_c *new_sub_chapter = new chapter_item_c();
1032 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1033 p_edition->sub_chapters.push_back( new_sub_chapter );
1035 else if( MKV_IS_ID( l, KaxEditionUID ) )
1037 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
1039 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
1041 p_edition->b_ordered = var_InheritBool( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
1043 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
1045 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
1046 i_default_edition = stored_editions.size();
1048 else if( MKV_IS_ID( l, KaxEditionFlagHidden ) )
1050 // FIXME to implement
1054 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
1057 stored_editions.push_back( p_edition );
1061 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
1065 for( size_t i = 0; i < stored_editions.size(); i++ )
1067 stored_editions[i]->RefreshChapters( );
1070 if ( stored_editions.size() != 0 && stored_editions[i_default_edition]->b_ordered )
1072 /* update the duration of the segment according to the sum of all sub chapters */
1073 i_dur = stored_editions[i_default_edition]->Duration() / INT64_C(1000);
1079 void matroska_segment_c::ParseCluster( )
1083 int i_upper_level = 0;
1085 /* Master elements */
1086 m = static_cast<EbmlMaster *>( cluster );
1087 m->Read( es, EBML_CONTEXT(cluster), i_upper_level, el, true );
1089 for( unsigned int i = 0; i < m->ListSize(); i++ )
1091 EbmlElement *l = (*m)[i];
1093 if( MKV_IS_ID( l, KaxClusterTimecode ) )
1095 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
1097 cluster->InitTimecode( uint64( ctc ), i_timescale );
1102 i_start_time = cluster->GlobalTimecode() / 1000;