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