1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Steve Lhomme <steve.lhomme@free.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "matroska_segment.hpp"
27 #include "chapters.hpp"
31 /*****************************************************************************
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 = EbmlVoid::ClassInfos.GlobalId;
63 msg_Dbg( &sys.demuxer, "| | + Seek" );
65 while( ( l = ep->Get() ) != NULL )
67 if( MKV_IS_ID( l, KaxSeekID ) )
69 KaxSeekID &sid = *(KaxSeekID*)l;
70 sid.ReadData( es.I_O() );
71 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
73 else if( MKV_IS_ID( l, KaxSeekPosition ) )
75 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
76 spos.ReadData( es.I_O() );
77 i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) );
81 /* Many mkvmerge files hit this case. It seems to be a broken SeekHead */
82 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
89 if( id == KaxCues::ClassInfos.GlobalId )
91 msg_Dbg( &sys.demuxer, "| - cues at %"PRId64, i_pos );
92 LoadSeekHeadItem( KaxCues::ClassInfos, i_pos );
94 else if( id == KaxInfo::ClassInfos.GlobalId )
96 msg_Dbg( &sys.demuxer, "| - info at %"PRId64, i_pos );
97 LoadSeekHeadItem( KaxInfo::ClassInfos, i_pos );
99 else if( id == KaxChapters::ClassInfos.GlobalId )
101 msg_Dbg( &sys.demuxer, "| - chapters at %"PRId64, i_pos );
102 LoadSeekHeadItem( KaxChapters::ClassInfos, i_pos );
104 else if( id == KaxTags::ClassInfos.GlobalId )
106 msg_Dbg( &sys.demuxer, "| - tags at %"PRId64, i_pos );
107 LoadSeekHeadItem( KaxTags::ClassInfos, i_pos );
109 else if( id == KaxSeekHead::ClassInfos.GlobalId )
111 msg_Dbg( &sys.demuxer, "| - chained seekhead at %"PRId64, i_pos );
112 LoadSeekHeadItem( KaxSeekHead::ClassInfos, i_pos );
114 else if( id == KaxTracks::ClassInfos.GlobalId )
116 msg_Dbg( &sys.demuxer, "| - tracks at %"PRId64, i_pos );
117 LoadSeekHeadItem( KaxTracks::ClassInfos, i_pos );
119 else if( id == KaxAttachments::ClassInfos.GlobalId )
121 msg_Dbg( &sys.demuxer, "| - attachments at %"PRId64, i_pos );
122 LoadSeekHeadItem( KaxAttachments::ClassInfos, i_pos );
125 msg_Dbg( &sys.demuxer, "| - unknown seekhead reference at %"PRId64, i_pos );
129 msg_Dbg( &sys.demuxer, "| | + ParseSeekHead Unknown (%s)", typeid(*l).name() );
136 * Helper function to print the mkv parse tree
138 static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
143 msg_Err( &demuxer, "too deep tree" );
146 va_start( args, psz_format );
147 static const char psz_foo[] = "| | | | | | | | | |";
148 char *psz_foo2 = (char*)malloc( i_level * 4 + 3 + strlen( psz_format ) );
149 strncpy( psz_foo2, psz_foo, 4 * i_level );
150 psz_foo2[ 4 * i_level ] = '+';
151 psz_foo2[ 4 * i_level + 1 ] = ' ';
152 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
153 __msg_GenericVa( VLC_OBJECT(&demuxer),VLC_MSG_DBG, "mkv", psz_foo2, args );
159 /*****************************************************************************
161 *****************************************************************************/
162 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
165 bool bSupported = true;
169 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
171 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_silent = false;
183 tk->i_number = tracks.size() - 1;
184 tk->i_extra_data = 0;
185 tk->p_extra_data = NULL;
186 tk->psz_codec = NULL;
187 tk->b_dts_only = false;
188 tk->i_default_duration = 0;
189 tk->f_timecodescale = 1.0;
191 tk->b_inited = false;
193 tk->p_data_init = NULL;
195 tk->psz_codec_name = NULL;
196 tk->psz_codec_settings = NULL;
197 tk->psz_codec_info_url = NULL;
198 tk->psz_codec_download_url = NULL;
200 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
201 tk->p_compression_data = NULL;
203 for( 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( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
253 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
255 // tk->b_enabled = uint32( fenb );
256 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
259 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
261 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
263 tk->b_default = uint32( fdef );
264 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
266 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
268 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
270 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
272 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
274 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
276 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
278 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
280 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
282 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
284 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
286 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
288 tk->i_default_duration = uint64(defd);
289 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%"PRId64, uint64(defd) );
291 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
293 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
295 tk->f_timecodescale = float( ttcs );
296 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
298 else if( MKV_IS_ID( l, KaxTrackName ) )
300 KaxTrackName &tname = *(KaxTrackName*)l;
302 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
303 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
305 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
307 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
309 if ( tk->fmt.psz_language != NULL )
310 free( tk->fmt.psz_language );
311 tk->fmt.psz_language = strdup( string( lang ).c_str() );
312 msg_Dbg( &sys.demuxer,
313 "| | | + Track Language=`%s'", tk->fmt.psz_language );
315 else if( MKV_IS_ID( l, KaxCodecID ) )
317 KaxCodecID &codecid = *(KaxCodecID*)l;
319 tk->psz_codec = strdup( string( codecid ).c_str() );
320 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
322 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
324 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
326 tk->i_extra_data = cpriv.GetSize();
327 if( tk->i_extra_data > 0 )
329 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
330 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
332 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
334 else if( MKV_IS_ID( l, KaxCodecName ) )
336 KaxCodecName &cname = *(KaxCodecName*)l;
338 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
339 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
341 else if( MKV_IS_ID( l, KaxContentEncodings ) )
343 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
344 MkvTree( sys.demuxer, 3, "Content Encodings" );
345 if ( cencs->ListSize() > 1 )
347 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
350 for( j = 0; j < cencs->ListSize(); j++ )
352 EbmlElement *l2 = (*cencs)[j];
353 if( MKV_IS_ID( l2, KaxContentEncoding ) )
355 MkvTree( sys.demuxer, 4, "Content Encoding" );
356 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
357 for( k = 0; k < cenc->ListSize(); k++ )
359 EbmlElement *l3 = (*cenc)[k];
360 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
362 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
363 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
365 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
367 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
368 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
370 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
372 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
373 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
375 else if( MKV_IS_ID( l3, KaxContentCompression ) )
377 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
378 MkvTree( sys.demuxer, 5, "Content Compression" );
379 for( n = 0; n < compr->ListSize(); n++ )
381 EbmlElement *l4 = (*compr)[n];
382 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
384 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
385 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
386 tk->i_compression_type = uint32( compalg );
387 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
388 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
390 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
394 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
396 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
400 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
406 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
412 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
416 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
418 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
420 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
421 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
423 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
425 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
427 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
428 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
430 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
432 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
434 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
435 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
437 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
439 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
441 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
443 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
445 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
447 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
449 else if( MKV_IS_ID( l, KaxTrackVideo ) )
451 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
453 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
454 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
456 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
459 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
460 tk->fmt.video.i_frame_rate = 1000000;
462 for( j = 0; j < tkv->ListSize(); j++ )
464 EbmlElement *l = (*tkv)[j];
465 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
467 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
469 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
471 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
473 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
475 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
478 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
480 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
482 tk->fmt.video.i_width += uint16( vwidth );
483 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
485 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
487 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
489 tk->fmt.video.i_height += uint16( vheight );
490 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
492 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
494 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
496 i_display_width = uint16( vwidth );
497 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
499 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
501 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
503 i_display_height = uint16( vheight );
504 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
506 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
508 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
510 i_crop_bottom = uint16( cropval );
511 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
513 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
515 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
517 i_crop_top = uint16( cropval );
518 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
520 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
522 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
524 i_crop_right = uint16( cropval );
525 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
527 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
529 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
531 i_crop_left = uint16( cropval );
532 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
534 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
536 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
538 tk->f_fps = float( vfps );
539 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
541 else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
543 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
545 i_display_unit = uint8( vdmode );
546 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
547 uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
549 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
551 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
553 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
555 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
557 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
559 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
563 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
566 if( i_display_height && i_display_width )
567 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * i_display_width / i_display_height;
568 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
570 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
571 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
572 tk->fmt.video.i_x_offset = i_crop_left;
573 tk->fmt.video.i_y_offset = i_crop_top;
574 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
575 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
577 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
578 we do not support this atm */
580 else if( MKV_IS_ID( l, KaxTrackAudio ) )
582 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
585 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
587 for( j = 0; j < tka->ListSize(); j++ )
589 EbmlElement *l = (*tka)[j];
591 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
593 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
595 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
596 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
598 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
600 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
602 tk->fmt.audio.i_rate = (int)float( afreq );
603 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
605 else if( MKV_IS_ID( l, KaxAudioChannels ) )
607 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
609 tk->fmt.audio.i_channels = uint8( achan );
610 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
612 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
614 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
616 tk->fmt.audio.i_bitspersample = uint8( abits );
617 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
621 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
627 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
634 tracks.push_back( tk );
638 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
643 /*****************************************************************************
645 *****************************************************************************/
646 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
650 int i_upper_level = 0;
652 /* Master elements */
653 tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
655 for( i = 0; i < tracks->ListSize(); i++ )
657 EbmlElement *l = (*tracks)[i];
659 if( MKV_IS_ID( l, KaxTrackEntry ) )
661 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
665 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
670 /*****************************************************************************
672 *****************************************************************************/
673 void matroska_segment_c::ParseInfo( KaxInfo *info )
678 int i_upper_level = 0;
680 /* Master elements */
681 m = static_cast<EbmlMaster *>(info);
682 m->Read( es, info->Generic().Context, i_upper_level, el, true );
684 for( i = 0; i < m->ListSize(); i++ )
686 EbmlElement *l = (*m)[i];
688 if( MKV_IS_ID( l, KaxSegmentUID ) )
690 if ( p_segment_uid == NULL )
691 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
693 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
695 else if( MKV_IS_ID( l, KaxPrevUID ) )
697 if ( p_prev_segment_uid == NULL )
698 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
700 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
702 else if( MKV_IS_ID( l, KaxNextUID ) )
704 if ( p_next_segment_uid == NULL )
705 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
707 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
709 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
711 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
713 i_timescale = uint64(tcs);
715 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%"PRId64,
718 else if( MKV_IS_ID( l, KaxDuration ) )
720 KaxDuration &dur = *(KaxDuration*)l;
722 i_duration = mtime_t( double( dur ) );
724 msg_Dbg( &sys.demuxer, "| | + Duration=%"PRId64,
727 else if( MKV_IS_ID( l, KaxMuxingApp ) )
729 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
731 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
733 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
734 psz_muxing_application );
736 else if( MKV_IS_ID( l, KaxWritingApp ) )
738 KaxWritingApp &wapp = *(KaxWritingApp*)l;
740 psz_writing_application = ToUTF8( UTFstring( wapp ) );
742 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
743 psz_writing_application );
745 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
747 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
749 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
751 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
752 psz_segment_filename );
754 else if( MKV_IS_ID( l, KaxTitle ) )
756 KaxTitle &title = *(KaxTitle*)l;
758 psz_title = ToUTF8( UTFstring( title ) );
760 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
762 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
764 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
766 families.push_back( new KaxSegmentFamily(*uid) );
768 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
770 else if( MKV_IS_ID( l, KaxDateUTC ) )
772 KaxDateUTC &date = *(KaxDateUTC*)l;
777 i_date = date.GetEpochDate();
778 if( gmtime_r( &i_date, &tmres ) &&
779 strftime( buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y",
782 psz_date_utc = strdup( buffer );
783 msg_Dbg( &sys.demuxer, "| | + Date=%s", buffer );
786 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
788 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
789 chapter_translation_c *p_translate = new chapter_translation_c();
791 p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
792 for( j = 0; j < p_trans->ListSize(); j++ )
794 EbmlElement *l = (*p_trans)[j];
796 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
798 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
800 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
802 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
804 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
806 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
810 translations.push_back( p_translate );
814 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
818 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
819 i_duration = mtime_t(f_dur);
823 /*****************************************************************************
825 *****************************************************************************/
826 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
830 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
831 for( i = 0; i < ca->ListSize(); i++ )
833 EbmlElement *l = (*ca)[i];
835 if( MKV_IS_ID( l, KaxChapterUID ) )
837 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
838 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
840 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
842 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
843 chapters.b_display_seekpoint = uint8( flag ) == 0;
845 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
847 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
849 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
850 chapters.i_start_time = uint64( start ) / INT64_C(1000);
852 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
854 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
856 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
857 chapters.i_end_time = uint64( end ) / INT64_C(1000);
859 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
861 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
863 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
865 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
866 for( j = 0; j < cd->ListSize(); j++ )
868 EbmlElement *l= (*cd)[j];
870 if( MKV_IS_ID( l, KaxChapterString ) )
874 KaxChapterString &name =*(KaxChapterString*)l;
875 for (k = 0; k < i_level; k++)
876 chapters.psz_name += '+';
877 chapters.psz_name += ' ';
878 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
879 chapters.psz_name += psz_tmp_utf8;
880 chapters.b_user_display = true;
882 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
883 free( psz_tmp_utf8 );
885 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
887 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
888 const char *psz = string( lang ).c_str();
890 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
892 else if( MKV_IS_ID( l, KaxChapterCountry ) )
894 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
895 const char *psz = string( ct ).c_str();
897 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
901 else if( MKV_IS_ID( l, KaxChapterProcess ) )
903 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
905 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
906 chapter_codec_cmds_c *p_ccodec = NULL;
908 for( j = 0; j < cp->ListSize(); j++ )
910 EbmlElement *k= (*cp)[j];
912 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
914 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
915 if ( uint32(*p_codec_id) == 0 )
916 p_ccodec = new matroska_script_codec_c( sys );
917 else if ( uint32(*p_codec_id) == 1 )
918 p_ccodec = new dvd_chapter_codec_c( sys );
923 if ( p_ccodec != NULL )
925 for( j = 0; j < cp->ListSize(); j++ )
927 EbmlElement *k= (*cp)[j];
929 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
931 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
932 p_ccodec->SetPrivate( *p_private );
934 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
936 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
939 chapters.codecs.push_back( p_ccodec );
942 else if( MKV_IS_ID( l, KaxChapterAtom ) )
944 chapter_item_c *new_sub_chapter = new chapter_item_c();
945 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
946 new_sub_chapter->psz_parent = &chapters;
947 chapters.sub_chapters.push_back( new_sub_chapter );
952 /*****************************************************************************
954 *****************************************************************************/
955 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
958 int i_upper_level = 0;
960 attachments->Read( es, attachments->Generic().Context, i_upper_level, el, true );
962 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
964 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
966 std::string psz_mime_type = GetChild<KaxMimeType>( *attachedFile );
967 KaxFileName &file_name = GetChild<KaxFileName>( *attachedFile );
968 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
970 attachment_c *new_attachment = new attachment_c();
974 new_attachment->psz_file_name = ToUTF8( UTFstring( file_name ) );
975 new_attachment->psz_mime_type = psz_mime_type;
976 new_attachment->i_size = img_data.GetSize();
977 new_attachment->p_data = malloc( img_data.GetSize() );
979 if( new_attachment->p_data )
981 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
982 sys.stored_attachments.push_back( new_attachment );
986 delete new_attachment;
990 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
994 /*****************************************************************************
996 *****************************************************************************/
997 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
1001 int i_upper_level = 0;
1004 /* Master elements */
1005 chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
1007 for( i = 0; i < chapters->ListSize(); i++ )
1009 EbmlElement *l = (*chapters)[i];
1011 if( MKV_IS_ID( l, KaxEditionEntry ) )
1013 chapter_edition_c *p_edition = new chapter_edition_c();
1015 EbmlMaster *E = static_cast<EbmlMaster *>(l );
1017 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
1018 for( j = 0; j < E->ListSize(); j++ )
1020 EbmlElement *l = (*E)[j];
1022 if( MKV_IS_ID( l, KaxChapterAtom ) )
1024 chapter_item_c *new_sub_chapter = new chapter_item_c();
1025 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1026 p_edition->sub_chapters.push_back( new_sub_chapter );
1028 else if( MKV_IS_ID( l, KaxEditionUID ) )
1030 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
1032 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
1034 p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
1036 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
1038 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
1039 i_default_edition = stored_editions.size();
1043 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
1046 stored_editions.push_back( p_edition );
1050 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
1054 for( i = 0; i < stored_editions.size(); i++ )
1056 stored_editions[i]->RefreshChapters( );
1059 if ( stored_editions.size() != 0 && stored_editions[i_default_edition]->b_ordered )
1061 /* update the duration of the segment according to the sum of all sub chapters */
1062 i_dur = stored_editions[i_default_edition]->Duration() / INT64_C(1000);
1068 void matroska_segment_c::ParseCluster( )
1073 int i_upper_level = 0;
1075 /* Master elements */
1076 m = static_cast<EbmlMaster *>( cluster );
1077 m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
1079 for( i = 0; i < m->ListSize(); i++ )
1081 EbmlElement *l = (*m)[i];
1083 if( MKV_IS_ID( l, KaxClusterTimecode ) )
1085 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
1087 cluster->InitTimecode( uint64( ctc ), i_timescale );
1092 i_start_time = cluster->GlobalTimecode() / 1000;