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( &demuxer,VLC_MSG_DBG, "mkv", psz_foo2, args );
159 /*****************************************************************************
161 *****************************************************************************/
162 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
164 bool bSupported = true;
168 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
170 tk = new mkv_track_t();
173 memset( tk, 0, sizeof( mkv_track_t ) );
175 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
176 tk->fmt.psz_language = strdup("English");
177 tk->fmt.psz_description = NULL;
179 tk->b_default = true;
180 tk->b_enabled = true;
181 tk->b_silent = false;
182 tk->i_number = tracks.size() - 1;
183 tk->i_extra_data = 0;
184 tk->p_extra_data = NULL;
185 tk->psz_codec = NULL;
186 tk->b_dts_only = false;
187 tk->i_default_duration = 0;
188 tk->f_timecodescale = 1.0;
190 tk->b_inited = false;
192 tk->p_data_init = NULL;
194 tk->psz_codec_name = NULL;
195 tk->psz_codec_settings = NULL;
196 tk->psz_codec_info_url = NULL;
197 tk->psz_codec_download_url = NULL;
199 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
200 tk->p_compression_data = NULL;
202 for( size_t i = 0; i < m->ListSize(); i++ )
204 EbmlElement *l = (*m)[i];
206 if( MKV_IS_ID( l, KaxTrackNumber ) )
208 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
210 tk->i_number = uint32( tnum );
211 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
213 else if( MKV_IS_ID( l, KaxTrackUID ) )
215 KaxTrackUID &tuid = *(KaxTrackUID*)l;
217 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
219 else if( MKV_IS_ID( l, KaxTrackType ) )
221 const char *psz_type;
222 KaxTrackType &ttype = *(KaxTrackType*)l;
224 switch( uint8(ttype) )
228 tk->fmt.i_cat = AUDIO_ES;
232 tk->fmt.i_cat = VIDEO_ES;
235 psz_type = "subtitle";
236 tk->fmt.i_cat = SPU_ES;
239 psz_type = "buttons";
240 tk->fmt.i_cat = SPU_ES;
243 psz_type = "unknown";
244 tk->fmt.i_cat = UNKNOWN_ES;
248 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
250 else if( MKV_IS_ID( l, KaxTrackFlagEnabled ) ) // UNUSED
252 KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
254 // tk->b_enabled = uint32( fenb );
255 msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u", uint32( fenb ) );
257 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
259 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
261 tk->b_default = uint32( fdef );
262 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
264 else if( MKV_IS_ID( l, KaxTrackFlagForced ) ) // UNUSED
266 KaxTrackFlagForced &ffor = *(KaxTrackFlagForced*)l;
268 msg_Dbg( &sys.demuxer, "| | | + Track Forced=%u", uint32( ffor ) );
270 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) ) // UNUSED
272 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
274 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
276 else if( MKV_IS_ID( l, KaxTrackMinCache ) ) // UNUSED
278 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
280 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
282 else if( MKV_IS_ID( l, KaxTrackMaxCache ) ) // UNUSED
284 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
286 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
288 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
290 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
292 tk->i_default_duration = uint64(defd);
293 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%"PRId64, uint64(defd) );
295 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
297 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
299 tk->f_timecodescale = float( ttcs );
300 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
302 else if( MKV_IS_ID( l, KaxMaxBlockAdditionID ) ) // UNUSED
304 KaxMaxBlockAdditionID &mbl = *(KaxMaxBlockAdditionID*)l;
306 msg_Dbg( &sys.demuxer, "| | | + Track Max BlockAdditionID=%d", uint32( mbl ) );
308 else if( MKV_IS_ID( l, KaxTrackName ) )
310 KaxTrackName &tname = *(KaxTrackName*)l;
312 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
313 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
315 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
317 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
319 free( tk->fmt.psz_language );
320 tk->fmt.psz_language = strdup( string( lang ).c_str() );
321 msg_Dbg( &sys.demuxer,
322 "| | | + Track Language=`%s'", tk->fmt.psz_language );
324 else if( MKV_IS_ID( l, KaxCodecID ) )
326 KaxCodecID &codecid = *(KaxCodecID*)l;
328 tk->psz_codec = strdup( string( codecid ).c_str() );
329 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
331 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
333 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
335 tk->i_extra_data = cpriv.GetSize();
336 if( tk->i_extra_data > 0 )
338 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
339 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
341 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
343 else if( MKV_IS_ID( l, KaxCodecName ) )
345 KaxCodecName &cname = *(KaxCodecName*)l;
347 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
348 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
351 else if( MKV_IS_ID( l, KaxCodecDecodeAll ) ) // UNUSED
353 KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
355 msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u", uint8( cdall ) );
357 else if( MKV_IS_ID( l, KaxTrackOverlay ) ) // UNUSED
359 KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
361 msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u", uint32( tovr ) );
363 else if( MKV_IS_ID( l, KaxContentEncodings ) )
365 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
366 MkvTree( sys.demuxer, 3, "Content Encodings" );
367 if ( cencs->ListSize() > 1 )
369 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
372 for( size_t j = 0; j < cencs->ListSize(); j++ )
374 EbmlElement *l2 = (*cencs)[j];
375 if( MKV_IS_ID( l2, KaxContentEncoding ) )
377 MkvTree( sys.demuxer, 4, "Content Encoding" );
378 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
379 for( size_t k = 0; k < cenc->ListSize(); k++ )
381 EbmlElement *l3 = (*cenc)[k];
382 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
384 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
385 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
387 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
389 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
390 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
392 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
394 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
395 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
397 else if( MKV_IS_ID( l3, KaxContentCompression ) )
399 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
400 MkvTree( sys.demuxer, 5, "Content Compression" );
401 for( size_t n = 0; n < compr->ListSize(); n++ )
403 EbmlElement *l4 = (*compr)[n];
404 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
406 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
407 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
408 tk->i_compression_type = uint32( compalg );
409 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
410 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
412 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
416 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
418 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
422 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
426 // ContentEncryption Unsupported
429 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
435 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
439 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId ) DEPRECATED by matroska
441 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
443 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
444 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
446 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId ) DEPRECATED by matroska
448 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
450 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
451 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
453 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId ) DEPRECATED by matroska
455 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
457 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
458 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
460 else if( MKV_IS_ID( l, KaxTrackVideo ) )
462 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
463 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
464 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
466 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
469 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
470 tk->fmt.video.i_frame_rate = 1000000;
472 for( unsigned int j = 0; j < tkv->ListSize(); j++ )
474 EbmlElement *l = (*tkv)[j];
475 if( MKV_IS_ID( l, KaxVideoFlagInterlaced ) ) // UNUSED
477 KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)l;
479 msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
481 else if( MKV_IS_ID( l, KaxVideoStereoMode ) ) // UNUSED
483 KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)l;
485 msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
487 else if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
489 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
491 tk->fmt.video.i_width += uint16( vwidth );
492 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
494 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
496 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
498 tk->fmt.video.i_height += uint16( vheight );
499 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
501 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
503 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
505 i_display_width = uint16( vwidth );
506 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
508 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
510 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
512 i_display_height = uint16( vheight );
513 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
515 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
517 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
519 i_crop_bottom = uint16( cropval );
520 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
522 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
524 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
526 i_crop_top = uint16( cropval );
527 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
529 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
531 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
533 i_crop_right = uint16( cropval );
534 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
536 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
538 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
540 i_crop_left = uint16( cropval );
541 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
543 else if( MKV_IS_ID( l, KaxVideoDisplayUnit ) )
545 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
547 i_display_unit = uint8( vdmode );
548 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
549 uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
551 else if( MKV_IS_ID( l, KaxVideoAspectRatio ) ) // UNUSED
553 KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
555 msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
557 // ColourSpace UNUSED
558 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
560 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
562 tk->f_fps = float( vfps );
563 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
565 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId ) //DEPRECATED by Matroska
567 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
569 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
573 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
576 if( i_display_height && i_display_width )
578 tk->fmt.video.i_sar_num = i_display_width * tk->fmt.video.i_height;
579 tk->fmt.video.i_sar_den = i_display_height * tk->fmt.video.i_width;
581 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
583 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
584 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
585 tk->fmt.video.i_x_offset = i_crop_left;
586 tk->fmt.video.i_y_offset = i_crop_top;
587 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
588 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
590 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
591 we do not support this atm */
593 else if( MKV_IS_ID( l, KaxTrackAudio ) )
595 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
597 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
599 for( unsigned int j = 0; j < tka->ListSize(); j++ )
601 EbmlElement *l = (*tka)[j];
603 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
605 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
607 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
608 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
610 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
612 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
614 tk->fmt.audio.i_rate = (int)float( afreq );
615 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
617 else if( MKV_IS_ID( l, KaxAudioChannels ) )
619 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
621 tk->fmt.audio.i_channels = uint8( achan );
622 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
624 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
626 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
628 tk->fmt.audio.i_bitspersample = uint8( abits );
629 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
633 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
639 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
646 tracks.push_back( tk );
650 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
655 /*****************************************************************************
657 *****************************************************************************/
658 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
661 int i_upper_level = 0;
663 /* Master elements */
664 tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
666 for( size_t i = 0; i < tracks->ListSize(); i++ )
668 EbmlElement *l = (*tracks)[i];
670 if( MKV_IS_ID( l, KaxTrackEntry ) )
672 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
676 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
681 /*****************************************************************************
683 *****************************************************************************/
684 void matroska_segment_c::ParseInfo( KaxInfo *info )
688 int i_upper_level = 0;
690 /* Master elements */
691 m = static_cast<EbmlMaster *>(info);
692 m->Read( es, info->Generic().Context, i_upper_level, el, true );
694 for( size_t i = 0; i < m->ListSize(); i++ )
696 EbmlElement *l = (*m)[i];
698 if( MKV_IS_ID( l, KaxSegmentUID ) )
700 if ( p_segment_uid == NULL )
701 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
703 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
705 else if( MKV_IS_ID( l, KaxPrevUID ) )
707 if ( p_prev_segment_uid == NULL )
708 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
710 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
712 else if( MKV_IS_ID( l, KaxNextUID ) )
714 if ( p_next_segment_uid == NULL )
715 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
717 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
719 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
721 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
723 i_timescale = uint64(tcs);
725 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%"PRId64,
728 else if( MKV_IS_ID( l, KaxDuration ) )
730 KaxDuration &dur = *(KaxDuration*)l;
732 i_duration = mtime_t( double( dur ) );
734 msg_Dbg( &sys.demuxer, "| | + Duration=%"PRId64,
737 else if( MKV_IS_ID( l, KaxMuxingApp ) )
739 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
741 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
743 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
744 psz_muxing_application );
746 else if( MKV_IS_ID( l, KaxWritingApp ) )
748 KaxWritingApp &wapp = *(KaxWritingApp*)l;
750 psz_writing_application = ToUTF8( UTFstring( wapp ) );
752 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
753 psz_writing_application );
755 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
757 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
759 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
761 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
762 psz_segment_filename );
764 else if( MKV_IS_ID( l, KaxTitle ) )
766 KaxTitle &title = *(KaxTitle*)l;
768 psz_title = ToUTF8( UTFstring( title ) );
770 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
772 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
774 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
776 families.push_back( new KaxSegmentFamily(*uid) );
778 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
780 else if( MKV_IS_ID( l, KaxDateUTC ) )
782 KaxDateUTC &date = *(KaxDateUTC*)l;
787 i_date = date.GetEpochDate();
788 if( gmtime_r( &i_date, &tmres ) &&
789 strftime( buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y",
792 psz_date_utc = strdup( buffer );
793 msg_Dbg( &sys.demuxer, "| | + Date=%s", buffer );
796 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
798 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
799 chapter_translation_c *p_translate = new chapter_translation_c();
801 p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
802 for( size_t j = 0; j < p_trans->ListSize(); j++ )
804 EbmlElement *l = (*p_trans)[j];
806 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
808 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
810 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
812 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
814 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
816 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
820 translations.push_back( p_translate );
824 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
828 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
829 i_duration = mtime_t(f_dur);
833 /*****************************************************************************
835 *****************************************************************************/
836 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
838 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
839 for( size_t i = 0; i < ca->ListSize(); i++ )
841 EbmlElement *l = (*ca)[i];
843 if( MKV_IS_ID( l, KaxChapterUID ) )
845 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
846 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %"PRId64"", chapters.i_uid );
848 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
850 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
851 chapters.b_display_seekpoint = uint8( flag ) == 0;
853 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
855 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
857 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
858 chapters.i_start_time = uint64( start ) / INT64_C(1000);
860 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %"PRId64"", chapters.i_start_time );
862 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
864 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
865 chapters.i_end_time = uint64( end ) / INT64_C(1000);
867 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %"PRId64"", chapters.i_end_time );
869 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
871 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
873 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
874 for( size_t j = 0; j < cd->ListSize(); j++ )
876 EbmlElement *l= (*cd)[j];
878 if( MKV_IS_ID( l, KaxChapterString ) )
880 KaxChapterString &name =*(KaxChapterString*)l;
881 for ( int k = 0; k < i_level; k++)
882 chapters.psz_name += '+';
883 chapters.psz_name += ' ';
884 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
885 chapters.psz_name += psz_tmp_utf8;
886 chapters.b_user_display = true;
888 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
889 free( psz_tmp_utf8 );
891 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
893 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
894 const char *psz = string( lang ).c_str();
896 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
898 else if( MKV_IS_ID( l, KaxChapterCountry ) )
900 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
901 const char *psz = string( ct ).c_str();
903 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
907 else if( MKV_IS_ID( l, KaxChapterProcess ) )
909 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
911 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
912 chapter_codec_cmds_c *p_ccodec = NULL;
914 for( size_t j = 0; j < cp->ListSize(); j++ )
916 EbmlElement *k= (*cp)[j];
918 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
920 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
921 if ( uint32(*p_codec_id) == 0 )
922 p_ccodec = new matroska_script_codec_c( sys );
923 else if ( uint32(*p_codec_id) == 1 )
924 p_ccodec = new dvd_chapter_codec_c( sys );
929 if ( p_ccodec != NULL )
931 for( size_t j = 0; j < cp->ListSize(); j++ )
933 EbmlElement *k= (*cp)[j];
935 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
937 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
938 p_ccodec->SetPrivate( *p_private );
940 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
942 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
945 chapters.codecs.push_back( p_ccodec );
948 else if( MKV_IS_ID( l, KaxChapterAtom ) )
950 chapter_item_c *new_sub_chapter = new chapter_item_c();
951 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
952 new_sub_chapter->psz_parent = &chapters;
953 chapters.sub_chapters.push_back( new_sub_chapter );
958 /*****************************************************************************
960 *****************************************************************************/
961 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
964 int i_upper_level = 0;
966 attachments->Read( es, attachments->Generic().Context, i_upper_level, el, true );
968 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
970 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
972 std::string psz_mime_type = GetChild<KaxMimeType>( *attachedFile );
973 KaxFileName &file_name = GetChild<KaxFileName>( *attachedFile );
974 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
976 attachment_c *new_attachment = new attachment_c();
980 new_attachment->psz_file_name = ToUTF8( UTFstring( file_name ) );
981 new_attachment->psz_mime_type = psz_mime_type;
982 new_attachment->i_size = img_data.GetSize();
983 new_attachment->p_data = malloc( img_data.GetSize() );
985 if( new_attachment->p_data )
987 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
988 sys.stored_attachments.push_back( new_attachment );
992 delete new_attachment;
996 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
1000 /*****************************************************************************
1002 *****************************************************************************/
1003 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
1006 int i_upper_level = 0;
1009 /* Master elements */
1010 chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
1012 for( size_t i = 0; i < chapters->ListSize(); i++ )
1014 EbmlElement *l = (*chapters)[i];
1016 if( MKV_IS_ID( l, KaxEditionEntry ) )
1018 chapter_edition_c *p_edition = new chapter_edition_c();
1020 EbmlMaster *E = static_cast<EbmlMaster *>(l );
1021 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
1022 for( size_t j = 0; j < E->ListSize(); j++ )
1024 EbmlElement *l = (*E)[j];
1026 if( MKV_IS_ID( l, KaxChapterAtom ) )
1028 chapter_item_c *new_sub_chapter = new chapter_item_c();
1029 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1030 p_edition->sub_chapters.push_back( new_sub_chapter );
1032 else if( MKV_IS_ID( l, KaxEditionUID ) )
1034 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
1036 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
1038 p_edition->b_ordered = var_InheritInteger( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
1040 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
1042 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
1043 i_default_edition = stored_editions.size();
1045 else if( MKV_IS_ID( l, KaxEditionFlagHidden ) )
1047 // FIXME to implement
1051 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
1054 stored_editions.push_back( p_edition );
1058 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
1062 for( size_t i = 0; i < stored_editions.size(); i++ )
1064 stored_editions[i]->RefreshChapters( );
1067 if ( stored_editions.size() != 0 && stored_editions[i_default_edition]->b_ordered )
1069 /* update the duration of the segment according to the sum of all sub chapters */
1070 i_dur = stored_editions[i_default_edition]->Duration() / INT64_C(1000);
1076 void matroska_segment_c::ParseCluster( )
1080 int i_upper_level = 0;
1082 /* Master elements */
1083 m = static_cast<EbmlMaster *>( cluster );
1084 m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
1086 for( unsigned int i = 0; i < m->ListSize(); i++ )
1088 EbmlElement *l = (*m)[i];
1090 if( MKV_IS_ID( l, KaxClusterTimecode ) )
1092 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
1094 cluster->InitTimecode( uint64( ctc ), i_timescale );
1099 i_start_time = cluster->GlobalTimecode() / 1000;