]> git.sesse.net Git - vlc/blob - modules/demux/mkv/matroska_segment_parse.cpp
Handle case where dwFormat is WAVE_FORMAT_EXTENSIBLE
[vlc] / modules / demux / mkv / matroska_segment_parse.cpp
1 /*****************************************************************************
2  * matroska_segment_parse.cpp : matroska demuxer
3  *****************************************************************************
4  * Copyright (C) 2003-2010 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24 #include "mkv.hpp"
25 #include "matroska_segment.hpp"
26 #include "chapters.hpp"
27 #include "demux.hpp"
28 #include "Ebml_parser.hpp"
29 #include "util.hpp"
30
31 extern "C" {
32 #include "../vobsub.h"
33 }
34
35 #include <vlc_codecs.h>
36
37 /* GetFourCC helper */
38 #define GetFOURCC( p )  __GetFOURCC( (uint8_t*)p )
39 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
40 {
41     return VLC_FOURCC( p[0], p[1], p[2], p[3] );
42 }
43
44 static inline void fill_extra_data( mkv_track_t *p_tk, unsigned int offset )
45 {
46     if(p_tk->i_extra_data <= offset) return;
47     p_tk->fmt.i_extra = p_tk->i_extra_data - offset;
48     p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
49     if(!p_tk->fmt.p_extra) { p_tk->fmt.i_extra = 0; return; };
50     memcpy( p_tk->fmt.p_extra, p_tk->p_extra_data + offset, p_tk->fmt.i_extra );
51 }
52
53 /*****************************************************************************
54  * Some functions to manipulate memory
55  *****************************************************************************/
56 static inline char * ToUTF8( const UTFstring &u )
57 {
58     return strdup( u.GetUTF8().c_str() );
59 }
60
61 /*****************************************************************************
62  * ParseSeekHead:
63  *****************************************************************************/
64 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
65 {
66     EbmlParser  *ep;
67     EbmlElement *l;
68     bool b_seekable;
69
70     i_seekhead_count++;
71
72     stream_Control( sys.demuxer.s, STREAM_CAN_SEEK, &b_seekable );
73     if( !b_seekable )
74         return;
75
76     ep = new EbmlParser( &es, seekhead, &sys.demuxer );
77
78     while( ( l = ep->Get() ) != NULL )
79     {
80         if( MKV_IS_ID( l, KaxSeek ) )
81         {
82             EbmlId id = EBML_ID(EbmlVoid);
83             int64_t i_pos = -1;
84
85 #ifdef MKV_DEBUG
86             msg_Dbg( &sys.demuxer, "|   |   + Seek" );
87 #endif
88             ep->Down();
89             while( ( l = ep->Get() ) != NULL )
90             {
91                 if( MKV_IS_ID( l, KaxSeekID ) )
92                 {
93                     KaxSeekID &sid = *(KaxSeekID*)l;
94                     sid.ReadData( es.I_O() );
95                     id = EbmlId( sid.GetBuffer(), sid.GetSize() );
96                 }
97                 else if( MKV_IS_ID( l, KaxSeekPosition ) )
98                 {
99                     KaxSeekPosition &spos = *(KaxSeekPosition*)l;
100                     spos.ReadData( es.I_O() );
101                     i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) );
102                 }
103                 else
104                 {
105                     /* Many mkvmerge files hit this case. It seems to be a broken SeekHead */
106                     msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name()  );
107                 }
108             }
109             ep->Up();
110
111             if( i_pos >= 0 )
112             {
113                 if( id == EBML_ID(KaxCues) )
114                 {
115                     msg_Dbg( &sys.demuxer, "|   - cues at %"PRId64, i_pos );
116                     LoadSeekHeadItem( EBML_INFO(KaxCues), i_pos );
117                 }
118                 else if( id == EBML_ID(KaxInfo) )
119                 {
120                     msg_Dbg( &sys.demuxer, "|   - info at %"PRId64, i_pos );
121                     LoadSeekHeadItem( EBML_INFO(KaxInfo), i_pos );
122                 }
123                 else if( id == EBML_ID(KaxChapters) )
124                 {
125                     msg_Dbg( &sys.demuxer, "|   - chapters at %"PRId64, i_pos );
126                     LoadSeekHeadItem( EBML_INFO(KaxChapters), i_pos );
127                 }
128                 else if( id == EBML_ID(KaxTags) )
129                 {
130                     msg_Dbg( &sys.demuxer, "|   - tags at %"PRId64, i_pos );
131                     LoadSeekHeadItem( EBML_INFO(KaxTags), i_pos );
132                 }
133                 else if( id == EBML_ID(KaxSeekHead) )
134                 {
135                     msg_Dbg( &sys.demuxer, "|   - chained seekhead at %"PRId64, i_pos );
136                     LoadSeekHeadItem( EBML_INFO(KaxSeekHead), i_pos );
137                 }
138                 else if( id == EBML_ID(KaxTracks) )
139                 {
140                     msg_Dbg( &sys.demuxer, "|   - tracks at %"PRId64, i_pos );
141                     LoadSeekHeadItem( EBML_INFO(KaxTracks), i_pos );
142                 }
143                 else if( id == EBML_ID(KaxAttachments) )
144                 {
145                     msg_Dbg( &sys.demuxer, "|   - attachments at %"PRId64, i_pos );
146                     LoadSeekHeadItem( EBML_INFO(KaxAttachments), i_pos );
147                 }
148 #ifdef MKV_DEBUG
149                 else
150                     msg_Dbg( &sys.demuxer, "|   - unknown seekhead reference at %"PRId64, i_pos );
151 #endif
152             }
153         }
154         else
155             msg_Dbg( &sys.demuxer, "|   |   + ParseSeekHead Unknown (%s)", typeid(*l).name() );
156     }
157     delete ep;
158 }
159
160
161 /**
162  * Helper function to print the mkv parse tree
163  */
164 static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
165 {
166     va_list args;
167     if( i_level > 9 )
168     {
169         msg_Err( &demuxer, "MKV tree is too deep" );
170         return;
171     }
172     va_start( args, psz_format );
173     static const char psz_foo[] = "|   |   |   |   |   |   |   |   |   |";
174     char *psz_foo2 = (char*)malloc( i_level * 4 + 3 + strlen( psz_format ) );
175     strncpy( psz_foo2, psz_foo, 4 * i_level );
176     psz_foo2[ 4 * i_level ] = '+';
177     psz_foo2[ 4 * i_level + 1 ] = ' ';
178     strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
179     msg_GenericVa( &demuxer,VLC_MSG_DBG, "mkv", psz_foo2, args );
180     free( psz_foo2 );
181     va_end( args );
182 }
183
184
185 /*****************************************************************************
186  * ParseTrackEntry:
187  *****************************************************************************/
188 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
189 {
190     bool bSupported = true;
191
192     /* Init the track */
193     mkv_track_t *tk = new mkv_track_t();
194     memset( tk, 0, sizeof( mkv_track_t ) );
195
196     es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
197     tk->p_es = NULL;
198     tk->fmt.psz_language       = strdup("English");
199     tk->fmt.psz_description    = NULL;
200
201     tk->b_default              = true;
202     tk->b_enabled              = true;
203     tk->b_forced               = false;
204     tk->b_silent               = false;
205     tk->i_number               = tracks.size() - 1;
206     tk->i_extra_data           = 0;
207     tk->p_extra_data           = NULL;
208     tk->psz_codec              = NULL;
209     tk->b_dts_only             = false;
210     tk->i_default_duration     = 0;
211     tk->f_timecodescale        = 1.0;
212
213     tk->b_inited               = false;
214     tk->i_data_init            = 0;
215     tk->p_data_init            = NULL;
216
217     tk->psz_codec_name         = NULL;
218     tk->psz_codec_settings     = NULL;
219     tk->psz_codec_info_url     = NULL;
220     tk->psz_codec_download_url = NULL;
221
222     tk->i_compression_type     = MATROSKA_COMPRESSION_NONE;
223     tk->i_encoding_scope       = MATROSKA_ENCODING_SCOPE_ALL_FRAMES;
224     tk->p_compression_data     = NULL;
225
226     msg_Dbg( &sys.demuxer, "|   |   + Track Entry" );
227
228     for( size_t i = 0; i < m->ListSize(); i++ )
229     {
230         EbmlElement *l = (*m)[i];
231
232         if( MKV_IS_ID( l, KaxTrackNumber ) )
233         {
234             KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
235
236             tk->i_number = uint32( tnum );
237             msg_Dbg( &sys.demuxer, "|   |   |   + Track Number=%u", uint32( tnum ) );
238         }
239         else  if( MKV_IS_ID( l, KaxTrackUID ) )
240         {
241             KaxTrackUID &tuid = *(KaxTrackUID*)l;
242
243             msg_Dbg( &sys.demuxer, "|   |   |   + Track UID=%u",  uint32( tuid ) );
244         }
245         else  if( MKV_IS_ID( l, KaxTrackType ) )
246         {
247             const char *psz_type;
248             KaxTrackType &ttype = *(KaxTrackType*)l;
249
250             switch( uint8(ttype) )
251             {
252                 case track_audio:
253                     psz_type = "audio";
254                     tk->fmt.i_cat = AUDIO_ES;
255                     tk->fmt.audio.i_channels = 1;
256                     tk->fmt.audio.i_rate = 8000;
257                     break;
258                 case track_video:
259                     psz_type = "video";
260                     tk->fmt.i_cat = VIDEO_ES;
261                     break;
262                 case track_subtitle:
263                     psz_type = "subtitle";
264                     tk->fmt.i_cat = SPU_ES;
265                     break;
266                 case track_buttons:
267                     psz_type = "buttons";
268                     tk->fmt.i_cat = SPU_ES;
269                     break;
270                 default:
271                     psz_type = "unknown";
272                     tk->fmt.i_cat = UNKNOWN_ES;
273                     break;
274             }
275
276             msg_Dbg( &sys.demuxer, "|   |   |   + Track Type=%s", psz_type );
277         }
278         else  if( MKV_IS_ID( l, KaxTrackFlagEnabled ) ) // UNUSED
279         {
280             KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
281
282             tk->b_enabled = uint32( fenb );
283             msg_Dbg( &sys.demuxer, "|   |   |   + Track Enabled=%u", uint32( fenb ) );
284         }
285         else  if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
286         {
287             KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
288
289             tk->b_default = uint32( fdef );
290             msg_Dbg( &sys.demuxer, "|   |   |   + Track Default=%u", uint32( fdef ) );
291         }
292         else  if( MKV_IS_ID( l, KaxTrackFlagForced ) ) // UNUSED
293         {
294             KaxTrackFlagForced &ffor = *(KaxTrackFlagForced*)l;
295             tk->b_forced = uint32( ffor );
296
297             msg_Dbg( &sys.demuxer, "|   |   |   + Track Forced=%u", uint32( ffor ) );
298         }
299         else  if( MKV_IS_ID( l, KaxTrackFlagLacing ) ) // UNUSED
300         {
301             KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
302
303             msg_Dbg( &sys.demuxer, "|   |   |   + Track Lacing=%d", uint32( lac ) );
304         }
305         else  if( MKV_IS_ID( l, KaxTrackMinCache ) ) // UNUSED
306         {
307             KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
308
309             msg_Dbg( &sys.demuxer, "|   |   |   + Track MinCache=%d", uint32( cmin ) );
310         }
311         else  if( MKV_IS_ID( l, KaxTrackMaxCache ) ) // UNUSED
312         {
313             KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
314
315             msg_Dbg( &sys.demuxer, "|   |   |   + Track MaxCache=%d", uint32( cmax ) );
316         }
317         else  if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
318         {
319             KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
320
321             tk->i_default_duration = uint64(defd);
322             msg_Dbg( &sys.demuxer, "|   |   |   + Track Default Duration=%"PRId64, uint64(defd) );
323         }
324         else  if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
325         {
326             KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
327
328             tk->f_timecodescale = float( ttcs );
329             msg_Dbg( &sys.demuxer, "|   |   |   + Track TimeCodeScale=%f", tk->f_timecodescale );
330         }
331         else  if( MKV_IS_ID( l, KaxMaxBlockAdditionID ) ) // UNUSED
332         {
333             KaxMaxBlockAdditionID &mbl = *(KaxMaxBlockAdditionID*)l;
334
335             msg_Dbg( &sys.demuxer, "|   |   |   + Track Max BlockAdditionID=%d", uint32( mbl ) );
336         }
337         else if( MKV_IS_ID( l, KaxTrackName ) )
338         {
339             KaxTrackName &tname = *(KaxTrackName*)l;
340
341             tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
342             msg_Dbg( &sys.demuxer, "|   |   |   + Track Name=%s", tk->fmt.psz_description );
343         }
344         else  if( MKV_IS_ID( l, KaxTrackLanguage ) )
345         {
346             KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
347
348             free( tk->fmt.psz_language );
349             tk->fmt.psz_language = strdup( string( lang ).c_str() );
350             msg_Dbg( &sys.demuxer,
351                      "|   |   |   + Track Language=`%s'", tk->fmt.psz_language );
352         }
353         else  if( MKV_IS_ID( l, KaxCodecID ) )
354         {
355             KaxCodecID &codecid = *(KaxCodecID*)l;
356
357             tk->psz_codec = strdup( string( codecid ).c_str() );
358             msg_Dbg( &sys.demuxer, "|   |   |   + Track CodecId=%s", string( codecid ).c_str() );
359         }
360         else  if( MKV_IS_ID( l, KaxCodecPrivate ) )
361         {
362             KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
363
364             tk->i_extra_data = cpriv.GetSize();
365             if( tk->i_extra_data > 0 )
366             {
367                 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
368                 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
369             }
370             msg_Dbg( &sys.demuxer, "|   |   |   + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
371         }
372         else if( MKV_IS_ID( l, KaxCodecName ) )
373         {
374             KaxCodecName &cname = *(KaxCodecName*)l;
375
376             tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
377             msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Name=%s", tk->psz_codec_name );
378         }
379         //AttachmentLink
380         else if( MKV_IS_ID( l, KaxCodecDecodeAll ) ) // UNUSED
381         {
382             KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
383
384             msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Decode All=%u", uint8( cdall ) );
385         }
386         else if( MKV_IS_ID( l, KaxTrackOverlay ) ) // UNUSED
387         {
388             KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
389
390             msg_Dbg( &sys.demuxer, "|   |   |   + Track Overlay=%u", uint32( tovr ) );
391         }
392         else if( MKV_IS_ID( l, KaxContentEncodings ) )
393         {
394             EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
395             MkvTree( sys.demuxer, 3, "Content Encodings" );
396             if ( cencs->ListSize() > 1 )
397             {
398                 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
399                 bSupported = false;
400             }
401             for( size_t j = 0; j < cencs->ListSize(); j++ )
402             {
403                 EbmlElement *l2 = (*cencs)[j];
404                 if( MKV_IS_ID( l2, KaxContentEncoding ) )
405                 {
406                     MkvTree( sys.demuxer, 4, "Content Encoding" );
407                     EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
408                     for( size_t k = 0; k < cenc->ListSize(); k++ )
409                     {
410                         EbmlElement *l3 = (*cenc)[k];
411                         if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
412                         {
413                             KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
414                             MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
415                         }
416                         else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
417                         {
418                             KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
419                             tk->i_encoding_scope = uint32( encscope );
420                             MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
421                         }
422                         else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
423                         {
424                             KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
425                             MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
426                         }
427                         else if( MKV_IS_ID( l3, KaxContentCompression ) )
428                         {
429                             EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
430                             MkvTree( sys.demuxer, 5, "Content Compression" );
431                             //Default compression type is 0 (Zlib)
432                             tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
433                             for( size_t n = 0; n < compr->ListSize(); n++ )
434                             {
435                                 EbmlElement *l4 = (*compr)[n];
436                                 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
437                                 {
438                                     KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
439                                     MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
440                                     tk->i_compression_type = uint32( compalg );
441                                     if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
442                                          ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
443                                     {
444                                         msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
445                                         bSupported = false;
446                                     }
447                                 }
448                                 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
449                                 {
450                                     tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
451                                 }
452                                 else
453                                 {
454                                     MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
455                                 }
456                             }
457                         }
458                         // ContentEncryption Unsupported
459                         else
460                         {
461                             MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
462                         }
463                     }
464                 }
465                 else
466                 {
467                     MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
468                 }
469             }
470         }
471 //        else if( MKV_IS_ID( l, KaxCodecSettings) ) DEPRECATED by matroska
472 //        {
473 //            KaxCodecSettings &cset = *(KaxCodecSettings*)l;
474
475 //            tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
476 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Settings=%s", tk->psz_codec_settings );
477 //        }
478 //        else if( MKV_IS_ID( l, KaxCodecInfoURL) ) DEPRECATED by matroska
479 //        {
480 //            KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
481
482 //            tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
483 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Info URL=%s", tk->psz_codec_info_url );
484 //        }
485 //        else if( MKV_IS_ID( l, KaxCodecDownloadURL) ) DEPRECATED by matroska
486 //        {
487 //            KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
488
489 //            tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
490 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Info URL=%s", tk->psz_codec_download_url );
491 //        }
492         else  if( MKV_IS_ID( l, KaxTrackVideo ) )
493         {
494             EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
495             unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
496             unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
497
498             msg_Dbg( &sys.demuxer, "|   |   |   + Track Video" );
499             tk->f_fps = 0.0;
500
501             tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
502             tk->fmt.video.i_frame_rate = 1000000;
503
504             for( unsigned int j = 0; j < tkv->ListSize(); j++ )
505             {
506                 EbmlElement *l = (*tkv)[j];
507                 if( MKV_IS_ID( l, KaxVideoFlagInterlaced ) ) // UNUSED
508                 {
509                     KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)l;
510
511                     msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Interlaced=%u", uint8( fint ) );
512                 }
513                 else if( MKV_IS_ID( l, KaxVideoStereoMode ) ) // UNUSED
514                 {
515                     KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)l;
516
517                     msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Stereo Mode=%u", uint8( stereo ) );
518                 }
519                 else if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
520                 {
521                     KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
522
523                     tk->fmt.video.i_width += uint16( vwidth );
524                     msg_Dbg( &sys.demuxer, "|   |   |   |   + width=%d", uint16( vwidth ) );
525                 }
526                 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
527                 {
528                     KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
529
530                     tk->fmt.video.i_height += uint16( vheight );
531                     msg_Dbg( &sys.demuxer, "|   |   |   |   + height=%d", uint16( vheight ) );
532                 }
533                 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
534                 {
535                     KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
536
537                     i_display_width = uint16( vwidth );
538                     msg_Dbg( &sys.demuxer, "|   |   |   |   + display width=%d", uint16( vwidth ) );
539                 }
540                 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
541                 {
542                     KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
543
544                     i_display_height = uint16( vheight );
545                     msg_Dbg( &sys.demuxer, "|   |   |   |   + display height=%d", uint16( vheight ) );
546                 }
547                 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
548                 {
549                     KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
550
551                     i_crop_bottom = uint16( cropval );
552                     msg_Dbg( &sys.demuxer, "|   |   |   |   + crop pixel bottom=%d", uint16( cropval ) );
553                 }
554                 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
555                 {
556                     KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
557
558                     i_crop_top = uint16( cropval );
559                     msg_Dbg( &sys.demuxer, "|   |   |   |   + crop pixel top=%d", uint16( cropval ) );
560                 }
561                 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
562                 {
563                     KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
564
565                     i_crop_right = uint16( cropval );
566                     msg_Dbg( &sys.demuxer, "|   |   |   |   + crop pixel right=%d", uint16( cropval ) );
567                 }
568                 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
569                 {
570                     KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
571
572                     i_crop_left = uint16( cropval );
573                     msg_Dbg( &sys.demuxer, "|   |   |   |   + crop pixel left=%d", uint16( cropval ) );
574                 }
575                 else if( MKV_IS_ID( l, KaxVideoDisplayUnit ) )
576                 {
577                     KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
578
579                     i_display_unit = uint8( vdmode );
580                     msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Display Unit=%s",
581                              i_display_unit == 0 ? "pixels" : ( i_display_unit == 1 ? "centimeters": "inches" ) );
582                 }
583                 else if( MKV_IS_ID( l, KaxVideoAspectRatio ) ) // UNUSED
584                 {
585                     KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
586
587                     msg_Dbg( &sys.demuxer, "   |   |   |   + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
588                 }
589                 // ColourSpace UNUSED
590                 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
591                 {
592                     KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
593
594                     tk->f_fps = float( vfps );
595                     msg_Dbg( &sys.demuxer, "   |   |   |   + fps=%f", float( vfps ) );
596                 }
597 //                else if( MKV_IS_ID( l, KaxVideoGamma) ) //DEPRECATED by Matroska
598 //                {
599 //                    KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
600
601 //                    msg_Dbg( &sys.demuxer, "   |   |   |   + gamma=%f", float( gamma ) );
602 //                }
603                 else
604                 {
605                     msg_Dbg( &sys.demuxer, "|   |   |   |   + Unknown (%s)", typeid(*l).name() );
606                 }
607             }
608             if( i_display_height && i_display_width )
609             {
610                 tk->fmt.video.i_sar_num = i_display_width  * tk->fmt.video.i_height;
611                 tk->fmt.video.i_sar_den = i_display_height * tk->fmt.video.i_width;
612             }
613             if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
614             {
615                 tk->fmt.video.i_visible_width   = tk->fmt.video.i_width;
616                 tk->fmt.video.i_visible_height  = tk->fmt.video.i_height;
617                 tk->fmt.video.i_x_offset        = i_crop_left;
618                 tk->fmt.video.i_y_offset        = i_crop_top;
619                 tk->fmt.video.i_visible_width  -= i_crop_left + i_crop_right;
620                 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
621             }
622             /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
623                we do not support this atm */
624         }
625         else  if( MKV_IS_ID( l, KaxTrackAudio ) )
626         {
627             EbmlMaster *tka = static_cast<EbmlMaster*>(l);
628
629             /* Initialize default values */
630             tk->fmt.audio.i_channels = 1;
631             tk->fmt.audio.i_rate = 8000;
632
633             msg_Dbg( &sys.demuxer, "|   |   |   + Track Audio" );
634
635             for( unsigned int j = 0; j < tka->ListSize(); j++ )
636             {
637                 EbmlElement *l = (*tka)[j];
638
639                 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
640                 {
641                     KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
642
643                     tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
644                     msg_Dbg( &sys.demuxer, "|   |   |   |   + afreq=%d", tk->fmt.audio.i_rate );
645                 }
646                 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
647                 {
648                     KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
649
650                     tk->fmt.audio.i_rate = (int)float( afreq );
651                     msg_Dbg( &sys.demuxer, "|   |   |   |   + aoutfreq=%d", tk->fmt.audio.i_rate );
652                 }
653                 else if( MKV_IS_ID( l, KaxAudioChannels ) )
654                 {
655                     KaxAudioChannels &achan = *(KaxAudioChannels*)l;
656
657                     tk->fmt.audio.i_channels = uint8( achan );
658                     msg_Dbg( &sys.demuxer, "|   |   |   |   + achan=%u", uint8( achan ) );
659                 }
660                 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
661                 {
662                     KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
663
664                     tk->fmt.audio.i_bitspersample = uint8( abits );
665                     msg_Dbg( &sys.demuxer, "|   |   |   |   + abits=%u", uint8( abits ) );
666                 }
667                 else
668                 {
669                     msg_Dbg( &sys.demuxer, "|   |   |   |   + Unknown (%s)", typeid(*l).name() );
670                 }
671             }
672         }
673         else
674         {
675             msg_Dbg( &sys.demuxer, "|   |   |   + Unknown (%s)",
676                      typeid(*l).name() );
677         }
678     }
679
680     if ( bSupported )
681     {
682 #ifdef HAVE_ZLIB_H
683         if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB &&
684             tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_PRIVATE &&
685             tk->i_extra_data && tk->p_extra_data &&
686             zlib_decompress_extra( &sys.demuxer, tk) )
687             return;
688 #endif
689         if( TrackInit( tk ) )
690         {
691             msg_Err(&sys.demuxer, "Couldn't init track %d", tk->i_number );
692             free(tk->p_extra_data);
693             delete tk;
694             return;
695         }
696
697         tracks.push_back( tk );
698     }
699     else
700     {
701         msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
702         free(tk->p_extra_data);
703         delete tk;
704     }
705 }
706
707 /*****************************************************************************
708  * ParseTracks:
709  *****************************************************************************/
710 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
711 {
712     EbmlElement *el;
713     int i_upper_level = 0;
714
715     /* Master elements */
716     tracks->Read( es, EBML_CONTEXT(tracks), i_upper_level, el, true );
717
718     for( size_t i = 0; i < tracks->ListSize(); i++ )
719     {
720         EbmlElement *l = (*tracks)[i];
721
722         if( MKV_IS_ID( l, KaxTrackEntry ) )
723         {
724             ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
725         }
726         else
727         {
728             msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name() );
729         }
730     }
731 }
732
733 /*****************************************************************************
734  * ParseInfo:
735  *****************************************************************************/
736 void matroska_segment_c::ParseInfo( KaxInfo *info )
737 {
738     EbmlElement *el;
739     EbmlMaster  *m;
740     int i_upper_level = 0;
741
742     /* Master elements */
743     m = static_cast<EbmlMaster *>(info);
744     m->Read( es, EBML_CONTEXT(info), i_upper_level, el, true );
745
746     for( size_t i = 0; i < m->ListSize(); i++ )
747     {
748         EbmlElement *l = (*m)[i];
749
750         if( MKV_IS_ID( l, KaxSegmentUID ) )
751         {
752             if ( p_segment_uid == NULL )
753                 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
754
755             msg_Dbg( &sys.demuxer, "|   |   + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
756         }
757         else if( MKV_IS_ID( l, KaxPrevUID ) )
758         {
759             if ( p_prev_segment_uid == NULL )
760             {
761                 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
762                 b_ref_external_segments = true;
763             }
764
765             msg_Dbg( &sys.demuxer, "|   |   + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
766         }
767         else if( MKV_IS_ID( l, KaxNextUID ) )
768         {
769             if ( p_next_segment_uid == NULL )
770             {
771                 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
772                 b_ref_external_segments = true;
773             }
774
775             msg_Dbg( &sys.demuxer, "|   |   + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
776         }
777         else if( MKV_IS_ID( l, KaxTimecodeScale ) )
778         {
779             KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
780
781             i_timescale = uint64(tcs);
782
783             msg_Dbg( &sys.demuxer, "|   |   + TimecodeScale=%"PRId64,
784                      i_timescale );
785         }
786         else if( MKV_IS_ID( l, KaxDuration ) )
787         {
788             KaxDuration &dur = *(KaxDuration*)l;
789
790             i_duration = mtime_t( double( dur ) );
791
792             msg_Dbg( &sys.demuxer, "|   |   + Duration=%"PRId64,
793                      i_duration );
794         }
795         else if( MKV_IS_ID( l, KaxMuxingApp ) )
796         {
797             KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
798
799             psz_muxing_application = ToUTF8( UTFstring( mapp ) );
800
801             msg_Dbg( &sys.demuxer, "|   |   + Muxing Application=%s",
802                      psz_muxing_application );
803         }
804         else if( MKV_IS_ID( l, KaxWritingApp ) )
805         {
806             KaxWritingApp &wapp = *(KaxWritingApp*)l;
807
808             psz_writing_application = ToUTF8( UTFstring( wapp ) );
809
810             msg_Dbg( &sys.demuxer, "|   |   + Writing Application=%s",
811                      psz_writing_application );
812         }
813         else if( MKV_IS_ID( l, KaxSegmentFilename ) )
814         {
815             KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
816
817             psz_segment_filename = ToUTF8( UTFstring( sfn ) );
818
819             msg_Dbg( &sys.demuxer, "|   |   + Segment Filename=%s",
820                      psz_segment_filename );
821         }
822         else if( MKV_IS_ID( l, KaxTitle ) )
823         {
824             KaxTitle &title = *(KaxTitle*)l;
825
826             psz_title = ToUTF8( UTFstring( title ) );
827
828             msg_Dbg( &sys.demuxer, "|   |   + Title=%s", psz_title );
829         }
830         else if( MKV_IS_ID( l, KaxSegmentFamily ) )
831         {
832             KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
833
834             families.push_back( new KaxSegmentFamily(*uid) );
835
836             msg_Dbg( &sys.demuxer, "|   |   + family=%d", *(uint32*)uid->GetBuffer() );
837         }
838         else if( MKV_IS_ID( l, KaxDateUTC ) )
839         {
840             KaxDateUTC &date = *(KaxDateUTC*)l;
841             time_t i_date;
842             struct tm tmres;
843             char   buffer[25];
844
845             i_date = date.GetEpochDate();
846             if( gmtime_r( &i_date, &tmres ) &&
847                 strftime( buffer, sizeof(buffer), "%a %b %d %H:%M:%S %Y",
848                           &tmres ) )
849             {
850                 psz_date_utc = strdup( buffer );
851                 msg_Dbg( &sys.demuxer, "|   |   + Date=%s", buffer );
852             }
853         }
854         else if( MKV_IS_ID( l, KaxChapterTranslate ) )
855         {
856             KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
857             chapter_translation_c *p_translate = new chapter_translation_c();
858
859             p_trans->Read( es, EBML_CONTEXT(p_trans), i_upper_level, el, true );
860             for( size_t j = 0; j < p_trans->ListSize(); j++ )
861             {
862                 EbmlElement *l = (*p_trans)[j];
863
864                 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
865                 {
866                     p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
867                 }
868                 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
869                 {
870                     p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
871                 }
872                 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
873                 {
874                     p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
875                 }
876             }
877
878             translations.push_back( p_translate );
879         }
880         else
881         {
882             msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name() );
883         }
884     }
885
886     double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
887     i_duration = mtime_t(f_dur);
888     if( !i_duration ) i_duration = -1;
889 }
890
891
892 /*****************************************************************************
893  * ParseChapterAtom
894  *****************************************************************************/
895 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
896 {
897     msg_Dbg( &sys.demuxer, "|   |   |   + ChapterAtom (level=%d)", i_level );
898     for( size_t i = 0; i < ca->ListSize(); i++ )
899     {
900         EbmlElement *l = (*ca)[i];
901
902         if( MKV_IS_ID( l, KaxChapterUID ) )
903         {
904             chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
905             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterUID: %"PRIu64"", chapters.i_uid );
906         }
907         else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
908         {
909             KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
910             chapters.b_display_seekpoint = uint8( flag ) == 0;
911
912             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
913         }
914         else if( MKV_IS_ID( l, KaxChapterSegmentUID ) )
915         {
916             chapters.p_segment_uid = new KaxChapterSegmentUID( *static_cast<KaxChapterSegmentUID*>(l) );
917             b_ref_external_segments = true;
918             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterSegmentUID= %u", *(uint32*)chapters.p_segment_uid->GetBuffer() );
919         }
920         else if( MKV_IS_ID( l, KaxChapterSegmentEditionUID ) )
921         {
922             chapters.p_segment_edition_uid = new KaxChapterSegmentEditionUID( *static_cast<KaxChapterSegmentEditionUID*>(l) );
923             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterSegmentEditionUID= %u",
924 #if LIBMATROSKA_VERSION < 0x010300
925                      *(uint32*)chapters.p_segment_edition_uid->GetBuffer()
926 #else
927                      *(uint32*)chapters.p_segment_edition_uid
928 #endif
929                    );
930         }
931         else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
932         {
933             KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
934             chapters.i_start_time = uint64( start ) / INT64_C(1000);
935
936             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterTimeStart: %"PRId64"", chapters.i_start_time );
937         }
938         else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
939         {
940             KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
941             chapters.i_end_time = uint64( end ) / INT64_C(1000);
942
943             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterTimeEnd: %"PRId64"", chapters.i_end_time );
944         }
945         else if( MKV_IS_ID( l, KaxChapterDisplay ) )
946         {
947             EbmlMaster *cd = static_cast<EbmlMaster *>(l);
948
949             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterDisplay" );
950             for( size_t j = 0; j < cd->ListSize(); j++ )
951             {
952                 EbmlElement *l= (*cd)[j];
953
954                 if( MKV_IS_ID( l, KaxChapterString ) )
955                 {
956                     KaxChapterString &name =*(KaxChapterString*)l;
957                     for ( int k = 0; k < i_level; k++)
958                         chapters.psz_name += '+';
959                     chapters.psz_name += ' ';
960                     char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
961                     chapters.psz_name += psz_tmp_utf8;
962                     chapters.b_user_display = true;
963
964                     msg_Dbg( &sys.demuxer, "|   |   |   |   |    + ChapterString '%s'", psz_tmp_utf8 );
965                     free( psz_tmp_utf8 );
966                 }
967                 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
968                 {
969                     KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
970                     const char *psz = string( lang ).c_str();
971
972                     msg_Dbg( &sys.demuxer, "|   |   |   |   |    + ChapterLanguage '%s'", psz );
973                 }
974                 else if( MKV_IS_ID( l, KaxChapterCountry ) )
975                 {
976                     KaxChapterCountry &ct =*(KaxChapterCountry*)l;
977                     const char *psz = string( ct ).c_str();
978
979                     msg_Dbg( &sys.demuxer, "|   |   |   |   |    + ChapterCountry '%s'", psz );
980                 }
981             }
982         }
983         else if( MKV_IS_ID( l, KaxChapterProcess ) )
984         {
985             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterProcess" );
986
987             KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
988             chapter_codec_cmds_c *p_ccodec = NULL;
989
990             for( size_t j = 0; j < cp->ListSize(); j++ )
991             {
992                 EbmlElement *k= (*cp)[j];
993
994                 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
995                 {
996                     KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
997                     if ( uint32(*p_codec_id) == 0 )
998                         p_ccodec = new matroska_script_codec_c( sys );
999                     else if ( uint32(*p_codec_id) == 1 )
1000                         p_ccodec = new dvd_chapter_codec_c( sys );
1001                     break;
1002                 }
1003             }
1004
1005             if ( p_ccodec != NULL )
1006             {
1007                 for( size_t j = 0; j < cp->ListSize(); j++ )
1008                 {
1009                     EbmlElement *k= (*cp)[j];
1010
1011                     if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
1012                     {
1013                         KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
1014                         p_ccodec->SetPrivate( *p_private );
1015                     }
1016                     else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
1017                     {
1018                         p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
1019                     }
1020                 }
1021                 chapters.codecs.push_back( p_ccodec );
1022             }
1023         }
1024         else if( MKV_IS_ID( l, KaxChapterAtom ) )
1025         {
1026             chapter_item_c *new_sub_chapter = new chapter_item_c();
1027             ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1028             new_sub_chapter->p_parent = &chapters;
1029             chapters.sub_chapters.push_back( new_sub_chapter );
1030         }
1031     }
1032 }
1033
1034 /*****************************************************************************
1035  * ParseAttachments:
1036  *****************************************************************************/
1037 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
1038 {
1039     EbmlElement *el;
1040     int i_upper_level = 0;
1041
1042     attachments->Read( es, EBML_CONTEXT(attachments), i_upper_level, el, true );
1043
1044     KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
1045
1046     while( attachedFile && ( attachedFile->GetSize() > 0 ) )
1047     {
1048         KaxFileData  &img_data     = GetChild<KaxFileData>( *attachedFile );
1049         char *psz_tmp_utf8 =  ToUTF8( UTFstring( GetChild<KaxFileName>( *attachedFile ) ) );
1050         std::string attached_filename(psz_tmp_utf8);
1051         free(psz_tmp_utf8);
1052         attachment_c *new_attachment = new attachment_c( attached_filename,
1053                                                          GetChild<KaxMimeType>( *attachedFile ),
1054                                                          img_data.GetSize() );
1055
1056         msg_Dbg( &sys.demuxer, "|   |   - %s (%s)", new_attachment->fileName(), new_attachment->mimeType() );
1057
1058         if( new_attachment->init() )
1059         {
1060             memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
1061             sys.stored_attachments.push_back( new_attachment );
1062             if( !strncmp( new_attachment->mimeType(), "image/", 6 ) )
1063             {
1064                 char *psz_url;
1065                 if( asprintf( &psz_url, "attachment://%s",
1066                               new_attachment->fileName() ) == -1 )
1067                     continue;
1068                 if( !sys.meta )
1069                     sys.meta = vlc_meta_New();
1070                 vlc_meta_SetArtURL( sys.meta, psz_url );
1071                 free( psz_url );
1072             }
1073         }
1074         else
1075         {
1076             delete new_attachment;
1077         }
1078
1079         attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
1080     }
1081 }
1082
1083 /*****************************************************************************
1084  * ParseChapters:
1085  *****************************************************************************/
1086 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
1087 {
1088     EbmlElement *el;
1089     int i_upper_level = 0;
1090
1091     /* Master elements */
1092     chapters->Read( es, EBML_CONTEXT(chapters), i_upper_level, el, true );
1093
1094     for( size_t i = 0; i < chapters->ListSize(); i++ )
1095     {
1096         EbmlElement *l = (*chapters)[i];
1097
1098         if( MKV_IS_ID( l, KaxEditionEntry ) )
1099         {
1100             chapter_edition_c *p_edition = new chapter_edition_c();
1101
1102             EbmlMaster *E = static_cast<EbmlMaster *>(l );
1103             msg_Dbg( &sys.demuxer, "|   |   + EditionEntry" );
1104             for( size_t j = 0; j < E->ListSize(); j++ )
1105             {
1106                 EbmlElement *l = (*E)[j];
1107
1108                 if( MKV_IS_ID( l, KaxChapterAtom ) )
1109                 {
1110                     chapter_item_c *new_sub_chapter = new chapter_item_c();
1111                     ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
1112                     p_edition->sub_chapters.push_back( new_sub_chapter );
1113                 }
1114                 else if( MKV_IS_ID( l, KaxEditionUID ) )
1115                 {
1116                     p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
1117                 }
1118                 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
1119                 {
1120                     p_edition->b_ordered = var_InheritBool( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
1121                 }
1122                 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
1123                 {
1124                     if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
1125                         i_default_edition = stored_editions.size();
1126                 }
1127                 else if( MKV_IS_ID( l, KaxEditionFlagHidden ) )
1128                 {
1129                     // FIXME to implement
1130                 }
1131                 else
1132                 {
1133                     msg_Dbg( &sys.demuxer, "|   |   |   + Unknown (%s)", typeid(*l).name() );
1134                 }
1135             }
1136             stored_editions.push_back( p_edition );
1137         }
1138         else
1139         {
1140             msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name() );
1141         }
1142     }
1143 }
1144
1145 void matroska_segment_c::ParseCluster( bool b_update_start_time )
1146 {
1147     EbmlElement *el;
1148     EbmlMaster  *m;
1149     int i_upper_level = 0;
1150
1151     /* Master elements */
1152     m = static_cast<EbmlMaster *>( cluster );
1153     m->Read( es, EBML_CONTEXT(cluster), i_upper_level, el, true );
1154
1155     for( unsigned int i = 0; i < m->ListSize(); i++ )
1156     {
1157         EbmlElement *l = (*m)[i];
1158
1159         if( MKV_IS_ID( l, KaxClusterTimecode ) )
1160         {
1161             KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
1162
1163             cluster->InitTimecode( uint64( ctc ), i_timescale );
1164             break;
1165         }
1166     }
1167
1168     if( b_update_start_time )
1169         i_start_time = cluster->GlobalTimecode() / 1000;
1170 }
1171
1172
1173 int32_t matroska_segment_c::TrackInit( mkv_track_t * p_tk )
1174 {
1175     es_format_t *p_fmt = &p_tk->fmt;
1176     if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
1177     {
1178         if( p_tk->i_extra_data < (int)sizeof( VLC_BITMAPINFOHEADER ) )
1179         {
1180             msg_Err( &sys.demuxer, "missing/invalid VLC_BITMAPINFOHEADER" );
1181             p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1182         }
1183         else
1184         {
1185             VLC_BITMAPINFOHEADER *p_bih = (VLC_BITMAPINFOHEADER*)p_tk->p_extra_data;
1186
1187             p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1188             p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1189             p_tk->fmt.i_codec       = GetFOURCC( &p_bih->biCompression );
1190
1191             p_tk->fmt.i_extra       = GetDWLE( &p_bih->biSize ) - sizeof( VLC_BITMAPINFOHEADER );
1192             if( p_tk->fmt.i_extra > 0 )
1193             {
1194                 /* Very unlikely yet possible: bug #5659*/
1195                 size_t maxlen = p_tk->i_extra_data - sizeof( VLC_BITMAPINFOHEADER );
1196                 p_tk->fmt.i_extra = ( (unsigned)p_tk->fmt.i_extra < maxlen )?
1197                     p_tk->fmt.i_extra : maxlen;
1198
1199                 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1200                 memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
1201             }
1202         }
1203         p_tk->b_dts_only = true;
1204     }
1205     else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
1206              !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
1207     {
1208         p_tk->fmt.i_codec = VLC_CODEC_MPGV;
1209         if( p_tk->i_extra_data )
1210             fill_extra_data( p_tk, 0 );
1211     }
1212     else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
1213     {
1214         p_tk->fmt.i_codec = VLC_CODEC_THEORA;
1215         fill_extra_data( p_tk, 0 );
1216         p_tk->b_pts_only = true;
1217     }
1218     else if( !strncmp( p_tk->psz_codec, "V_REAL/RV", 9 ) )
1219     {
1220         uint8_t *p = p_tk->p_extra_data;
1221
1222         if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
1223             p_fmt->i_codec = VLC_CODEC_RV10;
1224         else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
1225             p_fmt->i_codec = VLC_CODEC_RV20;
1226         else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
1227             p_fmt->i_codec = VLC_CODEC_RV30;
1228         else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
1229             p_fmt->i_codec = VLC_CODEC_RV40;
1230
1231         /* Extract the framerate from the header */
1232         if( p_tk->i_extra_data >= 26 &&
1233             p[4] == 'V' && p[5] == 'I' && p[6] == 'D' && p[7] == 'O' &&
1234             p[8] == 'R' && p[9] == 'V' &&
1235             (p[10] == '3' || p[10] == '4') && p[11] == '0' )
1236         {
1237             p_tk->fmt.video.i_frame_rate =
1238                 p[22] << 24 | p[23] << 16 | p[24] << 8 | p[25] << 0;
1239             p_tk->fmt.video.i_frame_rate_base = 65536;
1240         }
1241
1242         fill_extra_data( p_tk, 26 );
1243         p_tk->b_dts_only = true;
1244     }
1245     else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
1246     {
1247         p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
1248     }
1249     else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
1250     {
1251         p_tk->fmt.i_codec = VLC_CODEC_VP8;
1252         p_tk->b_pts_only = true;
1253     }
1254     else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
1255     {
1256         if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
1257         {
1258             p_tk->fmt.i_codec = VLC_CODEC_DIV3;
1259         }
1260         else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
1261         {
1262             /* A MPEG 4 codec, SP, ASP, AP or AVC */
1263             if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1264                 p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1265             else
1266                 p_tk->fmt.i_codec = VLC_CODEC_MP4V;
1267             fill_extra_data( p_tk, 0 );
1268         }
1269     }
1270     else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
1271     {
1272         MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
1273         stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1274                                                    p_tk->p_extra_data,
1275                                                    p_tk->i_extra_data,
1276                                                    true );
1277         if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
1278             MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
1279         {
1280             p_tk->fmt.i_codec = p_box->i_type;
1281             p_tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1282             p_tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1283             p_tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1284             p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1285             memcpy( p_tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, p_tk->fmt.i_extra );
1286             MP4_FreeBox_sample_vide( p_box );
1287         }
1288         else
1289         {
1290             free( p_box );
1291         }
1292         stream_Delete( p_mp4_stream );
1293     }
1294     else if( !strcmp( p_tk->psz_codec, "V_MJPEG" ) )
1295     {
1296         p_tk->fmt.i_codec = VLC_CODEC_MJPG;
1297     }
1298     else if( !strcmp( p_tk->psz_codec, "A_MS/ACM" ) )
1299     {
1300         if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1301         {
1302             msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1303             p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1304         }
1305         else
1306         {
1307             WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
1308             if( p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE && 
1309                 p_tk->i_extra_data >= sizeof(WAVEFORMATEXTENSIBLE) )
1310             {
1311                 WAVEFORMATEXTENSIBLE * p_wext = (WAVEFORMATEXTENSIBLE*) p_wf;
1312                 sf_tag_to_fourcc( &p_wext->SubFormat,  &p_tk->fmt.i_codec, NULL);
1313                 /* FIXME should we use Samples and dwChannelMask?*/
1314             }
1315             else
1316                 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
1317
1318             if( p_tk->fmt.i_codec == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
1319                 msg_Err( &sys.demuxer, "Unrecognized wf tag: 0x%x", GetWLE( &p_wf->wFormatTag ) );
1320             p_tk->fmt.audio.i_channels   = GetWLE( &p_wf->nChannels );
1321             p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1322             p_tk->fmt.i_bitrate    = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1323             p_tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1324             p_tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1325
1326             p_tk->fmt.i_extra            = GetWLE( &p_wf->cbSize );
1327             if( p_tk->fmt.i_extra > 0 )
1328             {
1329                 p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1330                 memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
1331             }
1332         }
1333     }
1334     else if( !strcmp( p_tk->psz_codec, "A_MPEG/L3" ) ||
1335              !strcmp( p_tk->psz_codec, "A_MPEG/L2" ) ||
1336              !strcmp( p_tk->psz_codec, "A_MPEG/L1" ) )
1337     {
1338         p_tk->fmt.i_codec = VLC_CODEC_MPGA;
1339     }
1340     else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
1341     {
1342         p_tk->fmt.i_codec = VLC_CODEC_A52;
1343     }
1344     else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
1345     {
1346         p_tk->fmt.i_codec = VLC_CODEC_EAC3;
1347     }
1348     else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
1349     {
1350         p_tk->fmt.i_codec = VLC_CODEC_DTS;
1351     }
1352     else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
1353     {
1354         p_tk->fmt.i_codec = VLC_CODEC_MLP;
1355     }
1356     else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
1357     {
1358         /* FIXME when more samples arrive */
1359         p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
1360         p_fmt->b_packetized = false;
1361     }
1362     else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
1363     {
1364         p_tk->fmt.i_codec = VLC_CODEC_FLAC;
1365         fill_extra_data( p_tk, 0 );
1366     }
1367     else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
1368     {
1369         p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
1370         fill_extra_data( p_tk, 0 );
1371     }
1372     else if( !strncmp( p_tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1373              !strncmp( p_tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1374     {
1375         int i_profile, i_srate, sbr = 0;
1376         static const unsigned int i_sample_rates[] =
1377         {
1378             96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1379             16000, 12000, 11025,  8000,  7350,     0,     0,     0
1380         };
1381
1382         p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1383         /* create data for faad (MP4DecSpecificDescrTag)*/
1384
1385         if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
1386         {
1387             i_profile = 0;
1388         }
1389         else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
1390         {
1391             i_profile = 1;
1392         }
1393         else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
1394         {
1395             i_profile = 2;
1396         }
1397         else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
1398         {
1399             i_profile = 1;
1400             sbr = 1;
1401         }
1402         else
1403         {
1404             i_profile = 3;
1405         }
1406
1407         for( i_srate = 0; i_srate < 13; i_srate++ )
1408         {
1409             if( i_sample_rates[i_srate] == p_tk->i_original_rate )
1410             {
1411                 break;
1412             }
1413         }
1414         msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1415
1416         p_tk->fmt.i_extra = sbr ? 5 : 2;
1417         p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
1418         ((uint8_t*)p_tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1419         ((uint8_t*)p_tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (p_tk->fmt.audio.i_channels << 3);
1420         if (sbr != 0)
1421         {
1422             int syncExtensionType = 0x2B7;
1423             int iDSRI;
1424             for (iDSRI=0; iDSRI<13; iDSRI++)
1425                 if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
1426                     break;
1427             ((uint8_t*)p_tk->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
1428             ((uint8_t*)p_tk->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
1429             ((uint8_t*)p_tk->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
1430         }
1431     }
1432     else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
1433     {
1434         p_tk->fmt.i_codec = VLC_CODEC_MP4A;
1435         fill_extra_data( p_tk, 0 );
1436     }
1437     else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
1438     {
1439         p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
1440         fill_extra_data( p_tk, 0 );
1441     }
1442     else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
1443     {
1444         p_fmt->i_codec = VLC_CODEC_TTA;
1445         if( p_tk->i_extra_data > 0 )
1446         {
1447             fill_extra_data( p_tk, 0 );
1448         }
1449         else
1450         {
1451             p_fmt->i_extra = 30;
1452             p_fmt->p_extra = xmalloc( p_fmt->i_extra );
1453             uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
1454             memcpy( &p_extra[ 0], "TTA1", 4 );
1455             SetWLE( &p_extra[ 4], 1 );
1456             SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
1457             SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
1458             SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
1459             SetDWLE( &p_extra[14], 0xffffffff );
1460             memset( &p_extra[18], 0, 30  - 18 );
1461         }
1462     }
1463     else if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) ||
1464              !strcmp( p_tk->psz_codec, "A_PCM/INT/LIT" ) ||
1465              !strcmp( p_tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1466     {
1467         if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
1468         {
1469             p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1470         }
1471         else
1472         {
1473             p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1474         }
1475         p_tk->fmt.audio.i_blockalign = ( p_tk->fmt.audio.i_bitspersample + 7 ) / 8 * p_tk->fmt.audio.i_channels;
1476     }
1477     else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
1478     {
1479         if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1480         {
1481             p_fmt->i_codec = VLC_CODEC_RA_144;
1482             p_fmt->audio.i_channels = 1;
1483             p_fmt->audio.i_rate = 8000;
1484             p_fmt->audio.i_blockalign = 0x14;
1485         }
1486         else if( p_tk->i_extra_data > 28 )
1487         {
1488             uint8_t *p = p_tk->p_extra_data;
1489             if( memcmp( p, ".ra", 3 ) ) {
1490                 msg_Err( &sys.demuxer, "Invalid Real ExtraData 0x%4.4s", (char *)p );
1491                 p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1492             }
1493             else
1494             {
1495                 real_audio_private * priv = (real_audio_private*) p_tk->p_extra_data;
1496                 if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
1497                 {
1498                     p_tk->fmt.i_codec = VLC_CODEC_COOK;
1499                     p_tk->fmt.audio.i_blockalign = hton16(priv->sub_packet_size);
1500                 }
1501                 else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
1502                     p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
1503                 else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
1504                     p_tk->fmt.i_codec = VLC_CODEC_RA_288;
1505                 /* FIXME RALF and SIPR */
1506                 uint16_t version = (uint16_t) hton16(priv->version);
1507                 p_tk->p_sys =
1508                     new Cook_PrivateTrackData( hton16(priv->sub_packet_h),
1509                                                hton16(priv->frame_size),
1510                                                hton16(priv->sub_packet_size));
1511                 if( unlikely( !p_tk->p_sys ) )
1512                     return 1;
1513
1514                 if( unlikely( p_tk->p_sys->Init() ) )
1515                     return 1;
1516
1517                 if( version == 4 )
1518                 {
1519                     real_audio_private_v4 * v4 = (real_audio_private_v4*) priv;
1520                     p_tk->fmt.audio.i_channels = hton16(v4->channels);
1521                     p_tk->fmt.audio.i_bitspersample = hton16(v4->sample_size);
1522                     p_tk->fmt.audio.i_rate = hton16(v4->sample_rate);
1523                 }
1524                 else if( version == 5 )
1525                 {
1526                     real_audio_private_v5 * v5 = (real_audio_private_v5*) priv;
1527                     p_tk->fmt.audio.i_channels = hton16(v5->channels);
1528                     p_tk->fmt.audio.i_bitspersample = hton16(v5->sample_size);
1529                     p_tk->fmt.audio.i_rate = hton16(v5->sample_rate);
1530                 }
1531                 msg_Dbg(&sys.demuxer, "%d channels %d bits %d Hz",p_tk->fmt.audio.i_channels, p_tk->fmt.audio.i_bitspersample, p_tk->fmt.audio.i_rate);
1532
1533                 fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 78);
1534             }
1535         }
1536     }
1537     else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
1538     {
1539         p_tk->fmt.i_codec = VLC_CODEC_KATE;
1540         p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1541
1542         fill_extra_data( p_tk, 0 );
1543     }
1544     else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
1545     {
1546         p_fmt->i_codec = VLC_CODEC_SUBT;
1547         p_fmt->subs.psz_encoding = strdup( "ASCII" );
1548     }
1549     else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
1550     {
1551         p_tk->fmt.i_codec = VLC_CODEC_SUBT;
1552         p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1553     }
1554     else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
1555     {
1556         p_tk->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1557         p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1558         if( p_tk->i_extra_data )
1559             fill_extra_data( p_tk, 0 );
1560     }
1561     else if( !strcmp( p_tk->psz_codec, "S_TEXT/SSA" ) ||
1562              !strcmp( p_tk->psz_codec, "S_TEXT/ASS" ) ||
1563              !strcmp( p_tk->psz_codec, "S_SSA" ) ||
1564              !strcmp( p_tk->psz_codec, "S_ASS" ))
1565     {
1566         p_tk->fmt.i_codec = VLC_CODEC_SSA;
1567         p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1568         if( p_tk->i_extra_data )
1569             fill_extra_data( p_tk, 0 );
1570     }
1571     else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
1572     {
1573         p_tk->fmt.i_codec = VLC_CODEC_SPU;
1574         if( p_tk->i_extra_data )
1575         {
1576             char *psz_start;
1577             char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
1578             if( psz_buf != NULL )
1579             {
1580                 memcpy( psz_buf, p_tk->p_extra_data , p_tk->i_extra_data );
1581                 psz_buf[p_tk->i_extra_data] = '\0';
1582
1583                 psz_start = strstr( psz_buf, "size:" );
1584                 if( psz_start &&
1585                     vobsub_size_parse( psz_start,
1586                                        &p_tk->fmt.subs.spu.i_original_frame_width,
1587                                        &p_tk->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1588                 {
1589                     msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1590                              p_tk->fmt.subs.spu.i_original_frame_width,
1591                              p_tk->fmt.subs.spu.i_original_frame_height );
1592                 }
1593                 else
1594                 {
1595                     msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1596                     return 1;
1597                 }
1598
1599                 psz_start = strstr( psz_buf, "palette:" );
1600                 if( psz_start &&
1601                     vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1602                 {
1603                     p_tk->fmt.subs.spu.palette[0] =  0xBeef;
1604                     msg_Dbg( &sys.demuxer, "vobsub palette read" );
1605                 }
1606                 else
1607                 {
1608                     msg_Warn( &sys.demuxer, "reading original palette failed" );
1609                 }
1610                 free( psz_buf );
1611             }
1612         }
1613     }
1614     else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
1615     {
1616         p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
1617     }
1618     else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
1619     {
1620         p_tk->fmt.i_cat = NAV_ES;
1621     }
1622     else
1623     {
1624         msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
1625         p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1626     }
1627     return 0;
1628 }