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