Mkv: kill warnings.
[vlc.git] / 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( VLC_OBJECT(&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     size_t i, j, k, n;
165     bool bSupported = true;
166
167     mkv_track_t *tk;
168
169     msg_Dbg( &sys.demuxer, "|   |   + Track Entry" );
170
171     tk = new mkv_track_t();
172
173     /* Init the track */
174     memset( tk, 0, sizeof( mkv_track_t ) );
175
176     es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
177     tk->fmt.psz_language = strdup("English");
178     tk->fmt.psz_description = NULL;
179
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->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( 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( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
251 //        {
252 //            KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
253
254 //            tk->b_enabled = uint32( fenb );
255 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Enabled=%u",
256 //                     uint32( fenb )  );
257 //        }
258         else  if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
259         {
260             KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
261
262             tk->b_default = uint32( fdef );
263             msg_Dbg( &sys.demuxer, "|   |   |   + Track Default=%u", uint32( fdef )  );
264         }
265         else  if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
266         {
267             KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
268
269             msg_Dbg( &sys.demuxer, "|   |   |   + Track Lacing=%d", uint32( lac ) );
270         }
271         else  if( MKV_IS_ID( l, KaxTrackMinCache ) )
272         {
273             KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
274
275             msg_Dbg( &sys.demuxer, "|   |   |   + Track MinCache=%d", uint32( cmin ) );
276         }
277         else  if( MKV_IS_ID( l, KaxTrackMaxCache ) )
278         {
279             KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
280
281             msg_Dbg( &sys.demuxer, "|   |   |   + Track MaxCache=%d", uint32( cmax ) );
282         }
283         else  if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
284         {
285             KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
286
287             tk->i_default_duration = uint64(defd);
288             msg_Dbg( &sys.demuxer, "|   |   |   + Track Default Duration=%"PRId64, uint64(defd) );
289         }
290         else  if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
291         {
292             KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
293
294             tk->f_timecodescale = float( ttcs );
295             msg_Dbg( &sys.demuxer, "|   |   |   + Track TimeCodeScale=%f", tk->f_timecodescale );
296         }
297         else if( MKV_IS_ID( l, KaxTrackName ) )
298         {
299             KaxTrackName &tname = *(KaxTrackName*)l;
300
301             tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
302             msg_Dbg( &sys.demuxer, "|   |   |   + Track Name=%s", tk->fmt.psz_description );
303         }
304         else  if( MKV_IS_ID( l, KaxTrackLanguage ) )
305         {
306             KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
307
308             if ( tk->fmt.psz_language != NULL )
309                 free( tk->fmt.psz_language );
310             tk->fmt.psz_language = strdup( string( lang ).c_str() );
311             msg_Dbg( &sys.demuxer,
312                      "|   |   |   + Track Language=`%s'", tk->fmt.psz_language );
313         }
314         else  if( MKV_IS_ID( l, KaxCodecID ) )
315         {
316             KaxCodecID &codecid = *(KaxCodecID*)l;
317
318             tk->psz_codec = strdup( string( codecid ).c_str() );
319             msg_Dbg( &sys.demuxer, "|   |   |   + Track CodecId=%s", string( codecid ).c_str() );
320         }
321         else  if( MKV_IS_ID( l, KaxCodecPrivate ) )
322         {
323             KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
324
325             tk->i_extra_data = cpriv.GetSize();
326             if( tk->i_extra_data > 0 )
327             {
328                 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
329                 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
330             }
331             msg_Dbg( &sys.demuxer, "|   |   |   + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
332         }
333         else if( MKV_IS_ID( l, KaxCodecName ) )
334         {
335             KaxCodecName &cname = *(KaxCodecName*)l;
336
337             tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
338             msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Name=%s", tk->psz_codec_name );
339         }
340         else if( MKV_IS_ID( l, KaxContentEncodings ) )
341         {
342             EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
343             MkvTree( sys.demuxer, 3, "Content Encodings" );
344             if ( cencs->ListSize() > 1 )
345             {
346                 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
347                 bSupported = false;
348             }
349             for( j = 0; j < cencs->ListSize(); j++ )
350             {
351                 EbmlElement *l2 = (*cencs)[j];
352                 if( MKV_IS_ID( l2, KaxContentEncoding ) )
353                 {
354                     MkvTree( sys.demuxer, 4, "Content Encoding" );
355                     EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
356                     for( k = 0; k < cenc->ListSize(); k++ )
357                     {
358                         EbmlElement *l3 = (*cenc)[k];
359                         if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
360                         {
361                             KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
362                             MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
363                         }
364                         else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
365                         {
366                             KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
367                             MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
368                         }
369                         else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
370                         {
371                             KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
372                             MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
373                         }
374                         else if( MKV_IS_ID( l3, KaxContentCompression ) )
375                         {
376                             EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
377                             MkvTree( sys.demuxer, 5, "Content Compression" );
378                             for( n = 0; n < compr->ListSize(); n++ )
379                             {
380                                 EbmlElement *l4 = (*compr)[n];
381                                 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
382                                 {
383                                     KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
384                                     MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
385                                     tk->i_compression_type = uint32( compalg );
386                                     if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
387                                          ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
388                                     {
389                                         msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
390                                         bSupported = false;
391                                     }
392                                 }
393                                 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
394                                 {
395                                     tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
396                                 }
397                                 else
398                                 {
399                                     MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
400                                 }
401                             }
402                         }
403                         else
404                         {
405                             MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
406                         }
407                     }
408                 }
409                 else
410                 {
411                     MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
412                 }
413             }
414         }
415 //        else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
416 //        {
417 //            KaxCodecSettings &cset = *(KaxCodecSettings*)l;
418
419 //            tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
420 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Settings=%s", tk->psz_codec_settings );
421 //        }
422 //        else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
423 //        {
424 //            KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
425
426 //            tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
427 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Info URL=%s", tk->psz_codec_info_url );
428 //        }
429 //        else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
430 //        {
431 //            KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
432
433 //            tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
434 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Info URL=%s", tk->psz_codec_download_url );
435 //        }
436 //        else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
437 //        {
438 //            KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
439
440 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
441 //        }
442 //        else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
443 //        {
444 //            KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
445
446 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Overlay=%u <== UNUSED", uint32( tovr ) );
447 //        }
448         else  if( MKV_IS_ID( l, KaxTrackVideo ) )
449         {
450             EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
451             unsigned int j;
452             unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
453             unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
454
455             msg_Dbg( &sys.demuxer, "|   |   |   + Track Video" );
456             tk->f_fps = 0.0;
457
458             tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
459             tk->fmt.video.i_frame_rate = 1000000;
460  
461             for( j = 0; j < tkv->ListSize(); j++ )
462             {
463                 EbmlElement *l = (*tkv)[j];
464 //                if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
465 //                {
466 //                    KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
467
468 //                    msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Interlaced=%u", uint8( fint ) );
469 //                }
470 //                else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
471 //                {
472 //                    KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
473
474 //                    msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Stereo Mode=%u", uint8( stereo ) );
475 //                }
476 //                else
477                 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
478                 {
479                     KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
480
481                     tk->fmt.video.i_width += uint16( vwidth );
482                     msg_Dbg( &sys.demuxer, "|   |   |   |   + width=%d", uint16( vwidth ) );
483                 }
484                 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
485                 {
486                     KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
487
488                     tk->fmt.video.i_height += uint16( vheight );
489                     msg_Dbg( &sys.demuxer, "|   |   |   |   + height=%d", uint16( vheight ) );
490                 }
491                 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
492                 {
493                     KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
494
495                     i_display_width = uint16( vwidth );
496                     msg_Dbg( &sys.demuxer, "|   |   |   |   + display width=%d", uint16( vwidth ) );
497                 }
498                 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
499                 {
500                     KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
501
502                     i_display_height = uint16( vheight );
503                     msg_Dbg( &sys.demuxer, "|   |   |   |   + display height=%d", uint16( vheight ) );
504                 }
505                 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
506                 {
507                     KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
508
509                     i_crop_bottom = uint16( cropval );
510                     msg_Dbg( &sys.demuxer, "|   |   |   |   + crop pixel bottom=%d", uint16( cropval ) );
511                 }
512                 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
513                 {
514                     KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
515
516                     i_crop_top = uint16( cropval );
517                     msg_Dbg( &sys.demuxer, "|   |   |   |   + crop pixel top=%d", uint16( cropval ) );
518                 }
519                 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
520                 {
521                     KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
522
523                     i_crop_right = uint16( cropval );
524                     msg_Dbg( &sys.demuxer, "|   |   |   |   + crop pixel right=%d", uint16( cropval ) );
525                 }
526                 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
527                 {
528                     KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
529
530                     i_crop_left = uint16( cropval );
531                     msg_Dbg( &sys.demuxer, "|   |   |   |   + crop pixel left=%d", uint16( cropval ) );
532                 }
533                 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
534                 {
535                     KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
536
537                     tk->f_fps = float( vfps );
538                     msg_Dbg( &sys.demuxer, "   |   |   |   + fps=%f", float( vfps ) );
539                 }
540                 else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
541                 {
542                     KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
543
544                     i_display_unit = uint8( vdmode );
545                     msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Display Unit=%s",
546                              uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
547                 }
548 //                else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
549 //                {
550 //                    KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
551
552 //                    msg_Dbg( &sys.demuxer, "   |   |   |   + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
553 //                }
554 //                else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
555 //                {
556 //                    KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
557
558 //                    msg_Dbg( &sys.demuxer, "   |   |   |   + gamma=%f", float( gamma ) );
559 //                }
560                 else
561                 {
562                     msg_Dbg( &sys.demuxer, "|   |   |   |   + Unknown (%s)", typeid(*l).name() );
563                 }
564             }
565             if( i_display_height && i_display_width )
566                 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * i_display_width / i_display_height;
567             if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
568             {
569                 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
570                 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
571                 tk->fmt.video.i_x_offset = i_crop_left;
572                 tk->fmt.video.i_y_offset = i_crop_top;
573                 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
574                 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
575             }
576             /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
577                we do not support this atm */
578         }
579         else  if( MKV_IS_ID( l, KaxTrackAudio ) )
580         {
581             EbmlMaster *tka = static_cast<EbmlMaster*>(l);
582             unsigned int j;
583
584             msg_Dbg( &sys.demuxer, "|   |   |   + Track Audio" );
585
586             for( j = 0; j < tka->ListSize(); j++ )
587             {
588                 EbmlElement *l = (*tka)[j];
589
590                 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
591                 {
592                     KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
593
594                     tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
595                     msg_Dbg( &sys.demuxer, "|   |   |   |   + afreq=%d", tk->fmt.audio.i_rate );
596                 }
597                 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
598                 {
599                     KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
600
601                     tk->fmt.audio.i_rate = (int)float( afreq );
602                     msg_Dbg( &sys.demuxer, "|   |   |   |   + aoutfreq=%d", tk->fmt.audio.i_rate );
603                 }
604                 else if( MKV_IS_ID( l, KaxAudioChannels ) )
605                 {
606                     KaxAudioChannels &achan = *(KaxAudioChannels*)l;
607
608                     tk->fmt.audio.i_channels = uint8( achan );
609                     msg_Dbg( &sys.demuxer, "|   |   |   |   + achan=%u", uint8( achan ) );
610                 }
611                 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
612                 {
613                     KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
614
615                     tk->fmt.audio.i_bitspersample = uint8( abits );
616                     msg_Dbg( &sys.demuxer, "|   |   |   |   + abits=%u", uint8( abits ) );
617                 }
618                 else
619                 {
620                     msg_Dbg( &sys.demuxer, "|   |   |   |   + Unknown (%s)", typeid(*l).name() );
621                 }
622             }
623         }
624         else
625         {
626             msg_Dbg( &sys.demuxer, "|   |   |   + Unknown (%s)",
627                      typeid(*l).name() );
628         }
629     }
630
631     if ( bSupported )
632     {
633         tracks.push_back( tk );
634     }
635     else
636     {
637         msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
638         delete tk;
639     }
640 }
641
642 /*****************************************************************************
643  * ParseTracks:
644  *****************************************************************************/
645 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
646 {
647     EbmlElement *el;
648     unsigned int i;
649     int i_upper_level = 0;
650
651     /* Master elements */
652     tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
653
654     for( i = 0; i < tracks->ListSize(); i++ )
655     {
656         EbmlElement *l = (*tracks)[i];
657
658         if( MKV_IS_ID( l, KaxTrackEntry ) )
659         {
660             ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
661         }
662         else
663         {
664             msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name() );
665         }
666     }
667 }
668
669 /*****************************************************************************
670  * ParseInfo:
671  *****************************************************************************/
672 void matroska_segment_c::ParseInfo( KaxInfo *info )
673 {
674     EbmlElement *el;
675     EbmlMaster  *m;
676     size_t i, j;
677     int i_upper_level = 0;
678
679     /* Master elements */
680     m = static_cast<EbmlMaster *>(info);
681     m->Read( es, info->Generic().Context, i_upper_level, el, true );
682
683     for( i = 0; i < m->ListSize(); i++ )
684     {
685         EbmlElement *l = (*m)[i];
686
687         if( MKV_IS_ID( l, KaxSegmentUID ) )
688         {
689             if ( p_segment_uid == NULL )
690                 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
691
692             msg_Dbg( &sys.demuxer, "|   |   + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
693         }
694         else if( MKV_IS_ID( l, KaxPrevUID ) )
695         {
696             if ( p_prev_segment_uid == NULL )
697                 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
698
699             msg_Dbg( &sys.demuxer, "|   |   + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
700         }
701         else if( MKV_IS_ID( l, KaxNextUID ) )
702         {
703             if ( p_next_segment_uid == NULL )
704                 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
705
706             msg_Dbg( &sys.demuxer, "|   |   + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
707         }
708         else if( MKV_IS_ID( l, KaxTimecodeScale ) )
709         {
710             KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
711
712             i_timescale = uint64(tcs);
713
714             msg_Dbg( &sys.demuxer, "|   |   + TimecodeScale=%"PRId64,
715                      i_timescale );
716         }
717         else if( MKV_IS_ID( l, KaxDuration ) )
718         {
719             KaxDuration &dur = *(KaxDuration*)l;
720
721             i_duration = mtime_t( double( dur ) );
722
723             msg_Dbg( &sys.demuxer, "|   |   + Duration=%"PRId64,
724                      i_duration );
725         }
726         else if( MKV_IS_ID( l, KaxMuxingApp ) )
727         {
728             KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
729
730             psz_muxing_application = ToUTF8( UTFstring( mapp ) );
731
732             msg_Dbg( &sys.demuxer, "|   |   + Muxing Application=%s",
733                      psz_muxing_application );
734         }
735         else if( MKV_IS_ID( l, KaxWritingApp ) )
736         {
737             KaxWritingApp &wapp = *(KaxWritingApp*)l;
738
739             psz_writing_application = ToUTF8( UTFstring( wapp ) );
740
741             msg_Dbg( &sys.demuxer, "|   |   + Writing Application=%s",
742                      psz_writing_application );
743         }
744         else if( MKV_IS_ID( l, KaxSegmentFilename ) )
745         {
746             KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
747
748             psz_segment_filename = ToUTF8( UTFstring( sfn ) );
749
750             msg_Dbg( &sys.demuxer, "|   |   + Segment Filename=%s",
751                      psz_segment_filename );
752         }
753         else if( MKV_IS_ID( l, KaxTitle ) )
754         {
755             KaxTitle &title = *(KaxTitle*)l;
756
757             psz_title = ToUTF8( UTFstring( title ) );
758
759             msg_Dbg( &sys.demuxer, "|   |   + Title=%s", psz_title );
760         }
761         else if( MKV_IS_ID( l, KaxSegmentFamily ) )
762         {
763             KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
764
765             families.push_back( new KaxSegmentFamily(*uid) );
766
767             msg_Dbg( &sys.demuxer, "|   |   + family=%d", *(uint32*)uid->GetBuffer() );
768         }
769 #if defined( HAVE_GMTIME_R )
770         else if( MKV_IS_ID( l, KaxDateUTC ) )
771         {
772             KaxDateUTC &date = *(KaxDateUTC*)l;
773             time_t i_date;
774             struct tm tmres;
775             char   buffer[256];
776
777             i_date = date.GetEpochDate();
778             memset( buffer, 0, 256 );
779             if( gmtime_r( &i_date, &tmres ) &&
780                 asctime_r( &tmres, buffer ) )
781             {
782                 buffer[strlen( buffer)-1]= '\0';
783                 psz_date_utc = strdup( buffer );
784                 msg_Dbg( &sys.demuxer, "|   |   + Date=%s", psz_date_utc );
785             }
786         }
787 #endif
788         else if( MKV_IS_ID( l, KaxChapterTranslate ) )
789         {
790             KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
791             chapter_translation_c *p_translate = new chapter_translation_c();
792
793             p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
794             for( j = 0; j < p_trans->ListSize(); j++ )
795             {
796                 EbmlElement *l = (*p_trans)[j];
797
798                 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
799                 {
800                     p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
801                 }
802                 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
803                 {
804                     p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
805                 }
806                 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
807                 {
808                     p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
809                 }
810             }
811
812             translations.push_back( p_translate );
813         }
814         else
815         {
816             msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name() );
817         }
818     }
819
820     double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
821     i_duration = mtime_t(f_dur);
822 }
823
824
825 /*****************************************************************************
826  * ParseChapterAtom
827  *****************************************************************************/
828 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
829 {
830     size_t i, j;
831
832     msg_Dbg( &sys.demuxer, "|   |   |   + ChapterAtom (level=%d)", i_level );
833     for( i = 0; i < ca->ListSize(); i++ )
834     {
835         EbmlElement *l = (*ca)[i];
836
837         if( MKV_IS_ID( l, KaxChapterUID ) )
838         {
839             chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
840             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterUID: %lld", chapters.i_uid );
841         }
842         else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
843         {
844             KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
845             chapters.b_display_seekpoint = uint8( flag ) == 0;
846
847             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
848         }
849         else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
850         {
851             KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
852             chapters.i_start_time = uint64( start ) / INT64_C(1000);
853
854             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterTimeStart: %lld", chapters.i_start_time );
855         }
856         else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
857         {
858             KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
859             chapters.i_end_time = uint64( end ) / INT64_C(1000);
860
861             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterTimeEnd: %lld", chapters.i_end_time );
862         }
863         else if( MKV_IS_ID( l, KaxChapterDisplay ) )
864         {
865             EbmlMaster *cd = static_cast<EbmlMaster *>(l);
866
867             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterDisplay" );
868             for( j = 0; j < cd->ListSize(); j++ )
869             {
870                 EbmlElement *l= (*cd)[j];
871
872                 if( MKV_IS_ID( l, KaxChapterString ) )
873                 {
874                     int k;
875
876                     KaxChapterString &name =*(KaxChapterString*)l;
877                     for (k = 0; k < i_level; k++)
878                         chapters.psz_name += '+';
879                     chapters.psz_name += ' ';
880                     char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
881                     chapters.psz_name += psz_tmp_utf8;
882                     chapters.b_user_display = true;
883
884                     msg_Dbg( &sys.demuxer, "|   |   |   |   |    + ChapterString '%s'", psz_tmp_utf8 );
885                     free( psz_tmp_utf8 );
886                 }
887                 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
888                 {
889                     KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
890                     const char *psz = string( lang ).c_str();
891
892                     msg_Dbg( &sys.demuxer, "|   |   |   |   |    + ChapterLanguage '%s'", psz );
893                 }
894                 else if( MKV_IS_ID( l, KaxChapterCountry ) )
895                 {
896                     KaxChapterCountry &ct =*(KaxChapterCountry*)l;
897                     const char *psz = string( ct ).c_str();
898
899                     msg_Dbg( &sys.demuxer, "|   |   |   |   |    + ChapterCountry '%s'", psz );
900                 }
901             }
902         }
903         else if( MKV_IS_ID( l, KaxChapterProcess ) )
904         {
905             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterProcess" );
906
907             KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
908             chapter_codec_cmds_c *p_ccodec = NULL;
909
910             for( j = 0; j < cp->ListSize(); j++ )
911             {
912                 EbmlElement *k= (*cp)[j];
913
914                 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
915                 {
916                     KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
917                     if ( uint32(*p_codec_id) == 0 )
918                         p_ccodec = new matroska_script_codec_c( sys );
919                     else if ( uint32(*p_codec_id) == 1 )
920                         p_ccodec = new dvd_chapter_codec_c( sys );
921                     break;
922                 }
923             }
924
925             if ( p_ccodec != NULL )
926             {
927                 for( j = 0; j < cp->ListSize(); j++ )
928                 {
929                     EbmlElement *k= (*cp)[j];
930
931                     if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
932                     {
933                         KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
934                         p_ccodec->SetPrivate( *p_private );
935                     }
936                     else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
937                     {
938                         p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
939                     }
940                 }
941                 chapters.codecs.push_back( p_ccodec );
942             }
943         }
944         else if( MKV_IS_ID( l, KaxChapterAtom ) )
945         {
946             chapter_item_c *new_sub_chapter = new chapter_item_c();
947             ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
948             new_sub_chapter->psz_parent = &chapters;
949             chapters.sub_chapters.push_back( new_sub_chapter );
950         }
951     }
952 }
953
954 /*****************************************************************************
955  * ParseAttachments:
956  *****************************************************************************/
957 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
958 {
959     EbmlElement *el;
960     int i_upper_level = 0;
961
962     attachments->Read( es, attachments->Generic().Context, i_upper_level, el, true );
963
964     KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
965
966     while( attachedFile && ( attachedFile->GetSize() > 0 ) )
967     {
968         std::string psz_mime_type  = GetChild<KaxMimeType>( *attachedFile );
969         KaxFileName  &file_name    = GetChild<KaxFileName>( *attachedFile );
970         KaxFileData  &img_data     = GetChild<KaxFileData>( *attachedFile );
971
972         attachment_c *new_attachment = new attachment_c();
973
974         if( new_attachment )
975         {
976             new_attachment->psz_file_name  = ToUTF8( UTFstring( file_name ) );
977             new_attachment->psz_mime_type  = psz_mime_type;
978             new_attachment->i_size         = img_data.GetSize();
979             new_attachment->p_data         = malloc( img_data.GetSize() );
980
981             if( new_attachment->p_data )
982             {
983                 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
984                 sys.stored_attachments.push_back( new_attachment );
985             }
986             else
987             {
988                 delete new_attachment;
989             }
990         }
991
992         attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
993     }
994 }
995
996 /*****************************************************************************
997  * ParseChapters:
998  *****************************************************************************/
999 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
1000 {
1001     EbmlElement *el;
1002     size_t i;
1003     int i_upper_level = 0;
1004     mtime_t i_dur;
1005
1006     /* Master elements */
1007     chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
1008
1009     for( i = 0; i < chapters->ListSize(); i++ )
1010     {
1011         EbmlElement *l = (*chapters)[i];
1012
1013         if( MKV_IS_ID( l, KaxEditionEntry ) )
1014         {
1015             chapter_edition_c *p_edition = new chapter_edition_c();
1016  
1017             EbmlMaster *E = static_cast<EbmlMaster *>(l );
1018             size_t j;
1019             msg_Dbg( &sys.demuxer, "|   |   + EditionEntry" );
1020             for( j = 0; j < E->ListSize(); j++ )
1021             {
1022                 EbmlElement *l = (*E)[j];
1023
1024                 if( MKV_IS_ID( l, KaxChapterAtom ) )
1025                 {
1026                     chapter_item_c *new_sub_chapter = new chapter_item_c();
1027                     ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1028                     p_edition->sub_chapters.push_back( new_sub_chapter );
1029                 }
1030                 else if( MKV_IS_ID( l, KaxEditionUID ) )
1031                 {
1032                     p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
1033                 }
1034                 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
1035                 {
1036                     p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
1037                 }
1038                 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
1039                 {
1040                     if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
1041                         i_default_edition = stored_editions.size();
1042                 }
1043                 else
1044                 {
1045                     msg_Dbg( &sys.demuxer, "|   |   |   + Unknown (%s)", typeid(*l).name() );
1046                 }
1047             }
1048             stored_editions.push_back( p_edition );
1049         }
1050         else
1051         {
1052             msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name() );
1053         }
1054     }
1055
1056     for( i = 0; i < stored_editions.size(); i++ )
1057     {
1058         stored_editions[i]->RefreshChapters( );
1059     }
1060  
1061     if ( stored_editions.size() != 0 && stored_editions[i_default_edition]->b_ordered )
1062     {
1063         /* update the duration of the segment according to the sum of all sub chapters */
1064         i_dur = stored_editions[i_default_edition]->Duration() / INT64_C(1000);
1065         if (i_dur > 0)
1066             i_duration = i_dur;
1067     }
1068 }
1069
1070 void matroska_segment_c::ParseCluster( )
1071 {
1072     EbmlElement *el;
1073     EbmlMaster  *m;
1074     unsigned int i;
1075     int i_upper_level = 0;
1076
1077     /* Master elements */
1078     m = static_cast<EbmlMaster *>( cluster );
1079     m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
1080
1081     for( i = 0; i < m->ListSize(); i++ )
1082     {
1083         EbmlElement *l = (*m)[i];
1084
1085         if( MKV_IS_ID( l, KaxClusterTimecode ) )
1086         {
1087             KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
1088
1089             cluster->InitTimecode( uint64( ctc ), i_timescale );
1090             break;
1091         }
1092     }
1093
1094     i_start_time = cluster->GlobalTimecode() / 1000;
1095 }
1096
1097