]> git.sesse.net Git - vlc/blob - modules/demux/mkv/matroska_segment_parse.cpp
MKV: code simplification by scope reduction
[vlc] / modules / demux / mkv / matroska_segment_parse.cpp
1 /*****************************************************************************
2  * mkv.cpp : matroska demuxer
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Steve Lhomme <steve.lhomme@free.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #include "matroska_segment.hpp"
26
27 #include "chapters.hpp"
28
29 #include "demux.hpp"
30
31 /*****************************************************************************
32  * Some functions to manipulate memory
33  *****************************************************************************/
34 static inline char * ToUTF8( const UTFstring &u )
35 {
36     return strdup( u.GetUTF8().c_str() );
37 }
38
39 /*****************************************************************************
40  * ParseSeekHead:
41  *****************************************************************************/
42 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
43 {
44     EbmlParser  *ep;
45     EbmlElement *l;
46     bool b_seekable;
47
48     i_seekhead_count++;
49
50     stream_Control( sys.demuxer.s, STREAM_CAN_SEEK, &b_seekable );
51     if( !b_seekable )
52         return;
53
54     ep = new EbmlParser( &es, seekhead, &sys.demuxer );
55
56     while( ( l = ep->Get() ) != NULL )
57     {
58         if( MKV_IS_ID( l, KaxSeek ) )
59         {
60             EbmlId id = EbmlVoid::ClassInfos.GlobalId;
61             int64_t i_pos = -1;
62
63             msg_Dbg( &sys.demuxer, "|   |   + Seek" );
64             ep->Down();
65             while( ( l = ep->Get() ) != NULL )
66             {
67                 if( MKV_IS_ID( l, KaxSeekID ) )
68                 {
69                     KaxSeekID &sid = *(KaxSeekID*)l;
70                     sid.ReadData( es.I_O() );
71                     id = EbmlId( sid.GetBuffer(), sid.GetSize() );
72                 }
73                 else if( MKV_IS_ID( l, KaxSeekPosition ) )
74                 {
75                     KaxSeekPosition &spos = *(KaxSeekPosition*)l;
76                     spos.ReadData( es.I_O() );
77                     i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) );
78                 }
79                 else
80                 {
81                     /* Many mkvmerge files hit this case. It seems to be a broken SeekHead */
82                     msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name()  );
83                 }
84             }
85             ep->Up();
86
87             if( i_pos >= 0 )
88             {
89                 if( id == KaxCues::ClassInfos.GlobalId )
90                 {
91                     msg_Dbg( &sys.demuxer, "|   - cues at %"PRId64, i_pos );
92                     LoadSeekHeadItem( KaxCues::ClassInfos, i_pos );
93                 }
94                 else if( id == KaxInfo::ClassInfos.GlobalId )
95                 {
96                     msg_Dbg( &sys.demuxer, "|   - info at %"PRId64, i_pos );
97                     LoadSeekHeadItem( KaxInfo::ClassInfos, i_pos );
98                 }
99                 else if( id == KaxChapters::ClassInfos.GlobalId )
100                 {
101                     msg_Dbg( &sys.demuxer, "|   - chapters at %"PRId64, i_pos );
102                     LoadSeekHeadItem( KaxChapters::ClassInfos, i_pos );
103                 }
104                 else if( id == KaxTags::ClassInfos.GlobalId )
105                 {
106                     msg_Dbg( &sys.demuxer, "|   - tags at %"PRId64, i_pos );
107                     LoadSeekHeadItem( KaxTags::ClassInfos, i_pos );
108                 }
109                 else if( id == KaxSeekHead::ClassInfos.GlobalId )
110                 {
111                     msg_Dbg( &sys.demuxer, "|   - chained seekhead at %"PRId64, i_pos );
112                     LoadSeekHeadItem( KaxSeekHead::ClassInfos, i_pos );
113                 }
114                 else if( id == KaxTracks::ClassInfos.GlobalId )
115                 {
116                     msg_Dbg( &sys.demuxer, "|   - tracks at %"PRId64, i_pos );
117                     LoadSeekHeadItem( KaxTracks::ClassInfos, i_pos );
118                 }
119                 else if( id == KaxAttachments::ClassInfos.GlobalId )
120                 {
121                     msg_Dbg( &sys.demuxer, "|   - attachments at %"PRId64, i_pos );
122                     LoadSeekHeadItem( KaxAttachments::ClassInfos, i_pos );
123                 }
124                 else
125                     msg_Dbg( &sys.demuxer, "|   - unknown seekhead reference at %"PRId64, i_pos );
126             }
127         }
128         else
129             msg_Dbg( &sys.demuxer, "|   |   + ParseSeekHead Unknown (%s)", typeid(*l).name() );
130     }
131     delete ep;
132 }
133
134
135 /**
136  * Helper function to print the mkv parse tree
137  */
138 static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
139 {
140     va_list args;
141     if( i_level > 9 )
142     {
143         msg_Err( &demuxer, "too deep tree" );
144         return;
145     }
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 );
154     free( psz_foo2 );
155     va_end( args );
156 }
157
158
159 /*****************************************************************************
160  * ParseTrackEntry:
161  *****************************************************************************/
162 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
163 {
164     bool bSupported = true;
165
166     mkv_track_t *tk;
167
168     msg_Dbg( &sys.demuxer, "|   |   + Track Entry" );
169
170     tk = new mkv_track_t();
171
172     /* Init the track */
173     memset( tk, 0, sizeof( mkv_track_t ) );
174
175     es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
176     tk->fmt.psz_language = strdup("English");
177     tk->fmt.psz_description = NULL;
178
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;
189
190     tk->b_inited = false;
191     tk->i_data_init = 0;
192     tk->p_data_init = NULL;
193
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;
198
199     tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
200     tk->p_compression_data = NULL;
201
202     for( size_t i = 0; i < m->ListSize(); i++ )
203     {
204         EbmlElement *l = (*m)[i];
205
206         if( MKV_IS_ID( l, KaxTrackNumber ) )
207         {
208             KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
209
210             tk->i_number = uint32( tnum );
211             msg_Dbg( &sys.demuxer, "|   |   |   + Track Number=%u", uint32( tnum ) );
212         }
213         else  if( MKV_IS_ID( l, KaxTrackUID ) )
214         {
215             KaxTrackUID &tuid = *(KaxTrackUID*)l;
216
217             msg_Dbg( &sys.demuxer, "|   |   |   + Track UID=%u",  uint32( tuid ) );
218         }
219         else  if( MKV_IS_ID( l, KaxTrackType ) )
220         {
221             const char *psz_type;
222             KaxTrackType &ttype = *(KaxTrackType*)l;
223
224             switch( uint8(ttype) )
225             {
226                 case track_audio:
227                     psz_type = "audio";
228                     tk->fmt.i_cat = AUDIO_ES;
229                     break;
230                 case track_video:
231                     psz_type = "video";
232                     tk->fmt.i_cat = VIDEO_ES;
233                     break;
234                 case track_subtitle:
235                     psz_type = "subtitle";
236                     tk->fmt.i_cat = SPU_ES;
237                     break;
238                 case track_buttons:
239                     psz_type = "buttons";
240                     tk->fmt.i_cat = SPU_ES;
241                     break;
242                 default:
243                     psz_type = "unknown";
244                     tk->fmt.i_cat = UNKNOWN_ES;
245                     break;
246             }
247
248             msg_Dbg( &sys.demuxer, "|   |   |   + Track Type=%s", psz_type );
249         }
250         else  if( MKV_IS_ID( l, KaxTrackFlagEnabled ) ) // UNUSED
251         {
252             KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
253
254             // tk->b_enabled = uint32( fenb );
255             msg_Dbg( &sys.demuxer, "|   |   |   + Track Enabled=%u", uint32( fenb ) );
256         }
257         else  if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
258         {
259             KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
260
261             tk->b_default = uint32( fdef );
262             msg_Dbg( &sys.demuxer, "|   |   |   + Track Default=%u", uint32( fdef ) );
263         }
264         else  if( MKV_IS_ID( l, KaxTrackFlagForced ) ) // UNUSED
265         {
266             KaxTrackFlagForced &ffor = *(KaxTrackFlagForced*)l;
267
268             msg_Dbg( &sys.demuxer, "|   |   |   + Track Forced=%u", uint32( ffor ) );
269         }
270         else  if( MKV_IS_ID( l, KaxTrackFlagLacing ) ) // UNUSED
271         {
272             KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
273
274             msg_Dbg( &sys.demuxer, "|   |   |   + Track Lacing=%d", uint32( lac ) );
275         }
276         else  if( MKV_IS_ID( l, KaxTrackMinCache ) ) // UNUSED
277         {
278             KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
279
280             msg_Dbg( &sys.demuxer, "|   |   |   + Track MinCache=%d", uint32( cmin ) );
281         }
282         else  if( MKV_IS_ID( l, KaxTrackMaxCache ) ) // UNUSED
283         {
284             KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
285
286             msg_Dbg( &sys.demuxer, "|   |   |   + Track MaxCache=%d", uint32( cmax ) );
287         }
288         else  if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
289         {
290             KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
291
292             tk->i_default_duration = uint64(defd);
293             msg_Dbg( &sys.demuxer, "|   |   |   + Track Default Duration=%"PRId64, uint64(defd) );
294         }
295         else  if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
296         {
297             KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
298
299             tk->f_timecodescale = float( ttcs );
300             msg_Dbg( &sys.demuxer, "|   |   |   + Track TimeCodeScale=%f", tk->f_timecodescale );
301         }
302         else  if( MKV_IS_ID( l, KaxMaxBlockAdditionID ) ) // UNUSED
303         {
304             KaxMaxBlockAdditionID &mbl = *(KaxMaxBlockAdditionID*)l;
305
306             msg_Dbg( &sys.demuxer, "|   |   |   + Track Max BlockAdditionID=%d", uint32( mbl ) );
307         }
308         else if( MKV_IS_ID( l, KaxTrackName ) )
309         {
310             KaxTrackName &tname = *(KaxTrackName*)l;
311
312             tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
313             msg_Dbg( &sys.demuxer, "|   |   |   + Track Name=%s", tk->fmt.psz_description );
314         }
315         else  if( MKV_IS_ID( l, KaxTrackLanguage ) )
316         {
317             KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
318
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 );
323         }
324         else  if( MKV_IS_ID( l, KaxCodecID ) )
325         {
326             KaxCodecID &codecid = *(KaxCodecID*)l;
327
328             tk->psz_codec = strdup( string( codecid ).c_str() );
329             msg_Dbg( &sys.demuxer, "|   |   |   + Track CodecId=%s", string( codecid ).c_str() );
330         }
331         else  if( MKV_IS_ID( l, KaxCodecPrivate ) )
332         {
333             KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
334
335             tk->i_extra_data = cpriv.GetSize();
336             if( tk->i_extra_data > 0 )
337             {
338                 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
339                 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
340             }
341             msg_Dbg( &sys.demuxer, "|   |   |   + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
342         }
343         else if( MKV_IS_ID( l, KaxCodecName ) )
344         {
345             KaxCodecName &cname = *(KaxCodecName*)l;
346
347             tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
348             msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Name=%s", tk->psz_codec_name );
349         }
350         //AttachmentLink
351         else if( MKV_IS_ID( l, KaxCodecDecodeAll ) ) // UNUSED
352         {
353             KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
354
355             msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Decode All=%u", uint8( cdall ) );
356         }
357         else if( MKV_IS_ID( l, KaxTrackOverlay ) ) // UNUSED
358         {
359             KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
360
361             msg_Dbg( &sys.demuxer, "|   |   |   + Track Overlay=%u", uint32( tovr ) );
362         }
363         else if( MKV_IS_ID( l, KaxContentEncodings ) )
364         {
365             EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
366             MkvTree( sys.demuxer, 3, "Content Encodings" );
367             if ( cencs->ListSize() > 1 )
368             {
369                 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
370                 bSupported = false;
371             }
372             for( size_t j = 0; j < cencs->ListSize(); j++ )
373             {
374                 EbmlElement *l2 = (*cencs)[j];
375                 if( MKV_IS_ID( l2, KaxContentEncoding ) )
376                 {
377                     MkvTree( sys.demuxer, 4, "Content Encoding" );
378                     EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
379                     for( size_t k = 0; k < cenc->ListSize(); k++ )
380                     {
381                         EbmlElement *l3 = (*cenc)[k];
382                         if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
383                         {
384                             KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
385                             MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
386                         }
387                         else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
388                         {
389                             KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
390                             MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
391                         }
392                         else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
393                         {
394                             KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
395                             MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
396                         }
397                         else if( MKV_IS_ID( l3, KaxContentCompression ) )
398                         {
399                             EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
400                             MkvTree( sys.demuxer, 5, "Content Compression" );
401                             for( size_t n = 0; n < compr->ListSize(); n++ )
402                             {
403                                 EbmlElement *l4 = (*compr)[n];
404                                 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
405                                 {
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 ) )
411                                     {
412                                         msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
413                                         bSupported = false;
414                                     }
415                                 }
416                                 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
417                                 {
418                                     tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
419                                 }
420                                 else
421                                 {
422                                     MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
423                                 }
424                             }
425                         }
426                         // ContentEncryption Unsupported
427                         else
428                         {
429                             MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
430                         }
431                     }
432                 }
433                 else
434                 {
435                     MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
436                 }
437             }
438         }
439 //        else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId ) DEPRECATED by matroska
440 //        {
441 //            KaxCodecSettings &cset = *(KaxCodecSettings*)l;
442
443 //            tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
444 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Settings=%s", tk->psz_codec_settings );
445 //        }
446 //        else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId ) DEPRECATED by matroska
447 //        {
448 //            KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
449
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 );
452 //        }
453 //        else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId ) DEPRECATED by matroska
454 //        {
455 //            KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
456
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 );
459 //        }
460         else  if( MKV_IS_ID( l, KaxTrackVideo ) )
461         {
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;
465
466             msg_Dbg( &sys.demuxer, "|   |   |   + Track Video" );
467             tk->f_fps = 0.0;
468
469             tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
470             tk->fmt.video.i_frame_rate = 1000000;
471
472             for( unsigned int j = 0; j < tkv->ListSize(); j++ )
473             {
474                 EbmlElement *l = (*tkv)[j];
475                 if( MKV_IS_ID( l, KaxVideoFlagInterlaced ) ) // UNUSED
476                 {
477                     KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)l;
478
479                     msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Interlaced=%u", uint8( fint ) );
480                 }
481                 else if( MKV_IS_ID( l, KaxVideoStereoMode ) ) // UNUSED
482                 {
483                     KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)l;
484
485                     msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Stereo Mode=%u", uint8( stereo ) );
486                 }
487                 else if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
488                 {
489                     KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
490
491                     tk->fmt.video.i_width += uint16( vwidth );
492                     msg_Dbg( &sys.demuxer, "|   |   |   |   + width=%d", uint16( vwidth ) );
493                 }
494                 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
495                 {
496                     KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
497
498                     tk->fmt.video.i_height += uint16( vheight );
499                     msg_Dbg( &sys.demuxer, "|   |   |   |   + height=%d", uint16( vheight ) );
500                 }
501                 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
502                 {
503                     KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
504
505                     i_display_width = uint16( vwidth );
506                     msg_Dbg( &sys.demuxer, "|   |   |   |   + display width=%d", uint16( vwidth ) );
507                 }
508                 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
509                 {
510                     KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
511
512                     i_display_height = uint16( vheight );
513                     msg_Dbg( &sys.demuxer, "|   |   |   |   + display height=%d", uint16( vheight ) );
514                 }
515                 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
516                 {
517                     KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
518
519                     i_crop_bottom = uint16( cropval );
520                     msg_Dbg( &sys.demuxer, "|   |   |   |   + crop pixel bottom=%d", uint16( cropval ) );
521                 }
522                 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
523                 {
524                     KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
525
526                     i_crop_top = uint16( cropval );
527                     msg_Dbg( &sys.demuxer, "|   |   |   |   + crop pixel top=%d", uint16( cropval ) );
528                 }
529                 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
530                 {
531                     KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
532
533                     i_crop_right = uint16( cropval );
534                     msg_Dbg( &sys.demuxer, "|   |   |   |   + crop pixel right=%d", uint16( cropval ) );
535                 }
536                 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
537                 {
538                     KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
539
540                     i_crop_left = uint16( cropval );
541                     msg_Dbg( &sys.demuxer, "|   |   |   |   + crop pixel left=%d", uint16( cropval ) );
542                 }
543                 else if( MKV_IS_ID( l, KaxVideoDisplayUnit ) )
544                 {
545                     KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
546
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" ) );
550                 }
551                 else if( MKV_IS_ID( l, KaxVideoAspectRatio ) ) // UNUSED
552                 {
553                     KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
554
555                     msg_Dbg( &sys.demuxer, "   |   |   |   + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
556                 }
557                 // ColourSpace UNUSED
558                 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
559                 {
560                     KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
561
562                     tk->f_fps = float( vfps );
563                     msg_Dbg( &sys.demuxer, "   |   |   |   + fps=%f", float( vfps ) );
564                 }
565 //                else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId ) //DEPRECATED by Matroska
566 //                {
567 //                    KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
568
569 //                    msg_Dbg( &sys.demuxer, "   |   |   |   + gamma=%f", float( gamma ) );
570 //                }
571                 else
572                 {
573                     msg_Dbg( &sys.demuxer, "|   |   |   |   + Unknown (%s)", typeid(*l).name() );
574                 }
575             }
576             if( i_display_height && i_display_width )
577             {
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;
580             }
581             if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
582             {
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;
589             }
590             /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
591                we do not support this atm */
592         }
593         else  if( MKV_IS_ID( l, KaxTrackAudio ) )
594         {
595             EbmlMaster *tka = static_cast<EbmlMaster*>(l);
596
597             msg_Dbg( &sys.demuxer, "|   |   |   + Track Audio" );
598
599             for( unsigned int j = 0; j < tka->ListSize(); j++ )
600             {
601                 EbmlElement *l = (*tka)[j];
602
603                 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
604                 {
605                     KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
606
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 );
609                 }
610                 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
611                 {
612                     KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
613
614                     tk->fmt.audio.i_rate = (int)float( afreq );
615                     msg_Dbg( &sys.demuxer, "|   |   |   |   + aoutfreq=%d", tk->fmt.audio.i_rate );
616                 }
617                 else if( MKV_IS_ID( l, KaxAudioChannels ) )
618                 {
619                     KaxAudioChannels &achan = *(KaxAudioChannels*)l;
620
621                     tk->fmt.audio.i_channels = uint8( achan );
622                     msg_Dbg( &sys.demuxer, "|   |   |   |   + achan=%u", uint8( achan ) );
623                 }
624                 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
625                 {
626                     KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
627
628                     tk->fmt.audio.i_bitspersample = uint8( abits );
629                     msg_Dbg( &sys.demuxer, "|   |   |   |   + abits=%u", uint8( abits ) );
630                 }
631                 else
632                 {
633                     msg_Dbg( &sys.demuxer, "|   |   |   |   + Unknown (%s)", typeid(*l).name() );
634                 }
635             }
636         }
637         else
638         {
639             msg_Dbg( &sys.demuxer, "|   |   |   + Unknown (%s)",
640                      typeid(*l).name() );
641         }
642     }
643
644     if ( bSupported )
645     {
646         tracks.push_back( tk );
647     }
648     else
649     {
650         msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
651         delete tk;
652     }
653 }
654
655 /*****************************************************************************
656  * ParseTracks:
657  *****************************************************************************/
658 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
659 {
660     EbmlElement *el;
661     int i_upper_level = 0;
662
663     /* Master elements */
664     tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
665
666     for( size_t i = 0; i < tracks->ListSize(); i++ )
667     {
668         EbmlElement *l = (*tracks)[i];
669
670         if( MKV_IS_ID( l, KaxTrackEntry ) )
671         {
672             ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
673         }
674         else
675         {
676             msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name() );
677         }
678     }
679 }
680
681 /*****************************************************************************
682  * ParseInfo:
683  *****************************************************************************/
684 void matroska_segment_c::ParseInfo( KaxInfo *info )
685 {
686     EbmlElement *el;
687     EbmlMaster  *m;
688     int i_upper_level = 0;
689
690     /* Master elements */
691     m = static_cast<EbmlMaster *>(info);
692     m->Read( es, info->Generic().Context, i_upper_level, el, true );
693
694     for( size_t i = 0; i < m->ListSize(); i++ )
695     {
696         EbmlElement *l = (*m)[i];
697
698         if( MKV_IS_ID( l, KaxSegmentUID ) )
699         {
700             if ( p_segment_uid == NULL )
701                 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
702
703             msg_Dbg( &sys.demuxer, "|   |   + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
704         }
705         else if( MKV_IS_ID( l, KaxPrevUID ) )
706         {
707             if ( p_prev_segment_uid == NULL )
708                 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
709
710             msg_Dbg( &sys.demuxer, "|   |   + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
711         }
712         else if( MKV_IS_ID( l, KaxNextUID ) )
713         {
714             if ( p_next_segment_uid == NULL )
715                 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
716
717             msg_Dbg( &sys.demuxer, "|   |   + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
718         }
719         else if( MKV_IS_ID( l, KaxTimecodeScale ) )
720         {
721             KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
722
723             i_timescale = uint64(tcs);
724
725             msg_Dbg( &sys.demuxer, "|   |   + TimecodeScale=%"PRId64,
726                      i_timescale );
727         }
728         else if( MKV_IS_ID( l, KaxDuration ) )
729         {
730             KaxDuration &dur = *(KaxDuration*)l;
731
732             i_duration = mtime_t( double( dur ) );
733
734             msg_Dbg( &sys.demuxer, "|   |   + Duration=%"PRId64,
735                      i_duration );
736         }
737         else if( MKV_IS_ID( l, KaxMuxingApp ) )
738         {
739             KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
740
741             psz_muxing_application = ToUTF8( UTFstring( mapp ) );
742
743             msg_Dbg( &sys.demuxer, "|   |   + Muxing Application=%s",
744                      psz_muxing_application );
745         }
746         else if( MKV_IS_ID( l, KaxWritingApp ) )
747         {
748             KaxWritingApp &wapp = *(KaxWritingApp*)l;
749
750             psz_writing_application = ToUTF8( UTFstring( wapp ) );
751
752             msg_Dbg( &sys.demuxer, "|   |   + Writing Application=%s",
753                      psz_writing_application );
754         }
755         else if( MKV_IS_ID( l, KaxSegmentFilename ) )
756         {
757             KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
758
759             psz_segment_filename = ToUTF8( UTFstring( sfn ) );
760
761             msg_Dbg( &sys.demuxer, "|   |   + Segment Filename=%s",
762                      psz_segment_filename );
763         }
764         else if( MKV_IS_ID( l, KaxTitle ) )
765         {
766             KaxTitle &title = *(KaxTitle*)l;
767
768             psz_title = ToUTF8( UTFstring( title ) );
769
770             msg_Dbg( &sys.demuxer, "|   |   + Title=%s", psz_title );
771         }
772         else if( MKV_IS_ID( l, KaxSegmentFamily ) )
773         {
774             KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
775
776             families.push_back( new KaxSegmentFamily(*uid) );
777
778             msg_Dbg( &sys.demuxer, "|   |   + family=%d", *(uint32*)uid->GetBuffer() );
779         }
780         else if( MKV_IS_ID( l, KaxDateUTC ) )
781         {
782             KaxDateUTC &date = *(KaxDateUTC*)l;
783             time_t i_date;
784             struct tm tmres;
785             char   buffer[25];
786
787             i_date = date.GetEpochDate();
788             if( gmtime_r( &i_date, &tmres ) &&
789                 strftime( buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y",
790                           &tmres ) )
791             {
792                 psz_date_utc = strdup( buffer );
793                 msg_Dbg( &sys.demuxer, "|   |   + Date=%s", buffer );
794             }
795         }
796         else if( MKV_IS_ID( l, KaxChapterTranslate ) )
797         {
798             KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
799             chapter_translation_c *p_translate = new chapter_translation_c();
800
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++ )
803             {
804                 EbmlElement *l = (*p_trans)[j];
805
806                 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
807                 {
808                     p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
809                 }
810                 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
811                 {
812                     p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
813                 }
814                 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
815                 {
816                     p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
817                 }
818             }
819
820             translations.push_back( p_translate );
821         }
822         else
823         {
824             msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name() );
825         }
826     }
827
828     double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
829     i_duration = mtime_t(f_dur);
830 }
831
832
833 /*****************************************************************************
834  * ParseChapterAtom
835  *****************************************************************************/
836 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
837 {
838     msg_Dbg( &sys.demuxer, "|   |   |   + ChapterAtom (level=%d)", i_level );
839     for( size_t i = 0; i < ca->ListSize(); i++ )
840     {
841         EbmlElement *l = (*ca)[i];
842
843         if( MKV_IS_ID( l, KaxChapterUID ) )
844         {
845             chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
846             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterUID: %"PRId64"", chapters.i_uid );
847         }
848         else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
849         {
850             KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
851             chapters.b_display_seekpoint = uint8( flag ) == 0;
852
853             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
854         }
855         else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
856         {
857             KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
858             chapters.i_start_time = uint64( start ) / INT64_C(1000);
859
860             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterTimeStart: %"PRId64"", chapters.i_start_time );
861         }
862         else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
863         {
864             KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
865             chapters.i_end_time = uint64( end ) / INT64_C(1000);
866
867             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterTimeEnd: %"PRId64"", chapters.i_end_time );
868         }
869         else if( MKV_IS_ID( l, KaxChapterDisplay ) )
870         {
871             EbmlMaster *cd = static_cast<EbmlMaster *>(l);
872
873             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterDisplay" );
874             for( size_t j = 0; j < cd->ListSize(); j++ )
875             {
876                 EbmlElement *l= (*cd)[j];
877
878                 if( MKV_IS_ID( l, KaxChapterString ) )
879                 {
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;
887
888                     msg_Dbg( &sys.demuxer, "|   |   |   |   |    + ChapterString '%s'", psz_tmp_utf8 );
889                     free( psz_tmp_utf8 );
890                 }
891                 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
892                 {
893                     KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
894                     const char *psz = string( lang ).c_str();
895
896                     msg_Dbg( &sys.demuxer, "|   |   |   |   |    + ChapterLanguage '%s'", psz );
897                 }
898                 else if( MKV_IS_ID( l, KaxChapterCountry ) )
899                 {
900                     KaxChapterCountry &ct =*(KaxChapterCountry*)l;
901                     const char *psz = string( ct ).c_str();
902
903                     msg_Dbg( &sys.demuxer, "|   |   |   |   |    + ChapterCountry '%s'", psz );
904                 }
905             }
906         }
907         else if( MKV_IS_ID( l, KaxChapterProcess ) )
908         {
909             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterProcess" );
910
911             KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
912             chapter_codec_cmds_c *p_ccodec = NULL;
913
914             for( size_t j = 0; j < cp->ListSize(); j++ )
915             {
916                 EbmlElement *k= (*cp)[j];
917
918                 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
919                 {
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 );
925                     break;
926                 }
927             }
928
929             if ( p_ccodec != NULL )
930             {
931                 for( size_t j = 0; j < cp->ListSize(); j++ )
932                 {
933                     EbmlElement *k= (*cp)[j];
934
935                     if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
936                     {
937                         KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
938                         p_ccodec->SetPrivate( *p_private );
939                     }
940                     else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
941                     {
942                         p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
943                     }
944                 }
945                 chapters.codecs.push_back( p_ccodec );
946             }
947         }
948         else if( MKV_IS_ID( l, KaxChapterAtom ) )
949         {
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 );
954         }
955     }
956 }
957
958 /*****************************************************************************
959  * ParseAttachments:
960  *****************************************************************************/
961 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
962 {
963     EbmlElement *el;
964     int i_upper_level = 0;
965
966     attachments->Read( es, attachments->Generic().Context, i_upper_level, el, true );
967
968     KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
969
970     while( attachedFile && ( attachedFile->GetSize() > 0 ) )
971     {
972         std::string psz_mime_type  = GetChild<KaxMimeType>( *attachedFile );
973         KaxFileName  &file_name    = GetChild<KaxFileName>( *attachedFile );
974         KaxFileData  &img_data     = GetChild<KaxFileData>( *attachedFile );
975
976         attachment_c *new_attachment = new attachment_c();
977
978         if( new_attachment )
979         {
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() );
984
985             if( new_attachment->p_data )
986             {
987                 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
988                 sys.stored_attachments.push_back( new_attachment );
989             }
990             else
991             {
992                 delete new_attachment;
993             }
994         }
995
996         attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
997     }
998 }
999
1000 /*****************************************************************************
1001  * ParseChapters:
1002  *****************************************************************************/
1003 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
1004 {
1005     EbmlElement *el;
1006     int i_upper_level = 0;
1007     mtime_t i_dur;
1008
1009     /* Master elements */
1010     chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
1011
1012     for( size_t i = 0; i < chapters->ListSize(); i++ )
1013     {
1014         EbmlElement *l = (*chapters)[i];
1015
1016         if( MKV_IS_ID( l, KaxEditionEntry ) )
1017         {
1018             chapter_edition_c *p_edition = new chapter_edition_c();
1019
1020             EbmlMaster *E = static_cast<EbmlMaster *>(l );
1021             msg_Dbg( &sys.demuxer, "|   |   + EditionEntry" );
1022             for( size_t j = 0; j < E->ListSize(); j++ )
1023             {
1024                 EbmlElement *l = (*E)[j];
1025
1026                 if( MKV_IS_ID( l, KaxChapterAtom ) )
1027                 {
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 );
1031                 }
1032                 else if( MKV_IS_ID( l, KaxEditionUID ) )
1033                 {
1034                     p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
1035                 }
1036                 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
1037                 {
1038                     p_edition->b_ordered = var_InheritInteger( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
1039                 }
1040                 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
1041                 {
1042                     if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
1043                         i_default_edition = stored_editions.size();
1044                 }
1045                 else if( MKV_IS_ID( l, KaxEditionFlagHidden ) )
1046                 {
1047                     // FIXME to implement
1048                 }
1049                 else
1050                 {
1051                     msg_Dbg( &sys.demuxer, "|   |   |   + Unknown (%s)", typeid(*l).name() );
1052                 }
1053             }
1054             stored_editions.push_back( p_edition );
1055         }
1056         else
1057         {
1058             msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name() );
1059         }
1060     }
1061
1062     for( size_t i = 0; i < stored_editions.size(); i++ )
1063     {
1064         stored_editions[i]->RefreshChapters( );
1065     }
1066
1067     if ( stored_editions.size() != 0 && stored_editions[i_default_edition]->b_ordered )
1068     {
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);
1071         if (i_dur > 0)
1072             i_duration = i_dur;
1073     }
1074 }
1075
1076 void matroska_segment_c::ParseCluster( )
1077 {
1078     EbmlElement *el;
1079     EbmlMaster  *m;
1080     int i_upper_level = 0;
1081
1082     /* Master elements */
1083     m = static_cast<EbmlMaster *>( cluster );
1084     m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
1085
1086     for( unsigned int i = 0; i < m->ListSize(); i++ )
1087     {
1088         EbmlElement *l = (*m)[i];
1089
1090         if( MKV_IS_ID( l, KaxClusterTimecode ) )
1091         {
1092             KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
1093
1094             cluster->InitTimecode( uint64( ctc ), i_timescale );
1095             break;
1096         }
1097     }
1098
1099     i_start_time = cluster->GlobalTimecode() / 1000;
1100 }
1101