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