]> git.sesse.net Git - vlc/blob - modules/demux/mkv.cpp
Improvements to preferences
[vlc] / modules / demux / mkv.cpp
1 /*****************************************************************************
2  * mkv.cpp : matroska demuxer
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30
31 #ifdef HAVE_TIME_H
32 #   include <time.h>                                               /* time() */
33 #endif
34
35 #include <vlc/input.h>
36
37 #include <codecs.h>                        /* BITMAPINFOHEADER, WAVEFORMATEX */
38 #include "iso_lang.h"
39 #include "vlc_meta.h"
40
41 #include <iostream>
42 #include <cassert>
43 #include <typeinfo>
44
45 /* libebml and matroska */
46 #include "ebml/EbmlHead.h"
47 #include "ebml/EbmlSubHead.h"
48 #include "ebml/EbmlStream.h"
49 #include "ebml/EbmlContexts.h"
50 #include "ebml/EbmlVersion.h"
51 #include "ebml/EbmlVoid.h"
52
53 #include "matroska/FileKax.h"
54 #include "matroska/KaxAttachments.h"
55 #include "matroska/KaxBlock.h"
56 #include "matroska/KaxBlockData.h"
57 #include "matroska/KaxChapters.h"
58 #include "matroska/KaxCluster.h"
59 #include "matroska/KaxClusterData.h"
60 #include "matroska/KaxContexts.h"
61 #include "matroska/KaxCues.h"
62 #include "matroska/KaxCuesData.h"
63 #include "matroska/KaxInfo.h"
64 #include "matroska/KaxInfoData.h"
65 #include "matroska/KaxSeekHead.h"
66 #include "matroska/KaxSegment.h"
67 #include "matroska/KaxTag.h"
68 #include "matroska/KaxTags.h"
69 #include "matroska/KaxTagMulti.h"
70 #include "matroska/KaxTracks.h"
71 #include "matroska/KaxTrackAudio.h"
72 #include "matroska/KaxTrackVideo.h"
73 #include "matroska/KaxTrackEntryData.h"
74 #include "matroska/KaxContentEncoding.h"
75
76 #include "ebml/StdIOCallback.h"
77
78 extern "C" {
79    #include "mp4/libmp4.h"
80 }
81 #ifdef HAVE_ZLIB_H
82 #   include <zlib.h>
83 #endif
84
85 #define MATROSKA_COMPRESSION_NONE 0
86 #define MATROSKA_COMPRESSION_ZLIB 1
87
88 using namespace LIBMATROSKA_NAMESPACE;
89 using namespace std;
90
91 /*****************************************************************************
92  * Module descriptor
93  *****************************************************************************/
94 static int  Open ( vlc_object_t * );
95 static void Close( vlc_object_t * );
96
97 vlc_module_begin();
98     set_name( _("Matroska") );
99     set_description( _("Matroska stream demuxer" ) );
100     set_capability( "demux2", 50 );
101     set_callbacks( Open, Close );
102     set_category( CAT_INPUT );
103     set_subcategory( SUBCAT_INPUT_DEMUX );
104
105     add_bool( "mkv-seek-percent", 1, NULL,
106             N_("Seek based on percent not time"),
107             N_("Seek based on percent not time"), VLC_TRUE );
108
109     add_shortcut( "mka" );
110     add_shortcut( "mkv" );
111 vlc_module_end();
112
113 /*****************************************************************************
114  * Local prototypes
115  *****************************************************************************/
116 static int  Demux  ( demux_t * );
117 static int  Control( demux_t *, int, va_list );
118 static void Seek   ( demux_t *, mtime_t i_date, int i_percent );
119
120 #ifdef HAVE_ZLIB_H
121 block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) {
122     int result, dstsize, n;
123     unsigned char *dst;
124     block_t *p_block;
125     z_stream d_stream;
126
127     d_stream.zalloc = (alloc_func)0;
128     d_stream.zfree = (free_func)0;
129     d_stream.opaque = (voidpf)0;
130     result = inflateInit(&d_stream);
131     if( result != Z_OK )
132     {
133         msg_Dbg( p_this, "inflateInit() failed. Result: %d", result );
134         return NULL;
135     }
136
137     d_stream.next_in = (Bytef *)p_in_block->p_buffer;
138     d_stream.avail_in = p_in_block->i_buffer;
139     n = 0;
140     p_block = block_New( p_this, 0 );
141     dst = NULL;
142     do
143     {
144         n++;
145         p_block = block_Realloc( p_block, 0, n * 1000 );
146         dst = (unsigned char *)p_block->p_buffer;
147         d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
148         d_stream.avail_out = 1000;
149         result = inflate(&d_stream, Z_NO_FLUSH);
150         if( ( result != Z_OK ) && ( result != Z_STREAM_END ) )
151         {
152             msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result );
153             return NULL;
154         }
155     }
156     while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
157            ( result != Z_STREAM_END ) );
158
159     dstsize = d_stream.total_out;
160     inflateEnd( &d_stream );
161
162     p_block = block_Realloc( p_block, 0, dstsize );
163     p_block->i_buffer = dstsize;
164     block_Release( p_in_block );
165
166     return p_block;
167 }
168 #endif
169
170 /**
171  * Helper function to print the mkv parse tree
172  */
173 static void MkvTree( demux_t *p_this, int i_level, char *psz_format, ... )
174 {
175     va_list args;
176     if( i_level > 9 )
177     {
178         msg_Err( p_this, "too deep tree" );
179         return;
180     }
181     va_start( args, psz_format );
182     static char *psz_foo = "|   |   |   |   |   |   |   |   |   |";
183     char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) );
184     strncpy( psz_foo2, psz_foo, 4 * i_level );
185     psz_foo2[ 4 * i_level ] = '+';
186     psz_foo2[ 4 * i_level + 1 ] = ' ';
187     strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
188     __msg_GenericVa( VLC_OBJECT(p_this), VLC_MSG_DBG, "mkv", psz_foo2, args );
189     free( psz_foo2 );
190     va_end( args );
191 }
192     
193 /*****************************************************************************
194  * Stream managment
195  *****************************************************************************/
196 class vlc_stream_io_callback: public IOCallback
197 {
198   private:
199     stream_t       *s;
200     vlc_bool_t     mb_eof;
201
202   public:
203     vlc_stream_io_callback( stream_t * );
204
205     virtual uint32   read            ( void *p_buffer, size_t i_size);
206     virtual void     setFilePointer  ( int64_t i_offset, seek_mode mode = seek_beginning );
207     virtual size_t   write           ( const void *p_buffer, size_t i_size);
208     virtual uint64   getFilePointer  ( void );
209     virtual void     close           ( void );
210 };
211
212 /*****************************************************************************
213  * Ebml Stream parser
214  *****************************************************************************/
215 class EbmlParser
216 {
217   public:
218     EbmlParser( EbmlStream *es, EbmlElement *el_start );
219     ~EbmlParser( void );
220
221     void Up( void );
222     void Down( void );
223     EbmlElement *Get( void );
224     void        Keep( void );
225
226     int GetLevel( void );
227
228   private:
229     EbmlStream  *m_es;
230     int         mi_level;
231     EbmlElement *m_el[10];
232
233     EbmlElement *m_got;
234
235     int         mi_user_level;
236     vlc_bool_t  mb_keep;
237 };
238
239
240 /*****************************************************************************
241  * Some functions to manipulate memory
242  *****************************************************************************/
243 #define GetFOURCC( p )  __GetFOURCC( (uint8_t*)p )
244 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
245 {
246     return VLC_FOURCC( p[0], p[1], p[2], p[3] );
247 }
248
249 /*****************************************************************************
250  * definitions of structures and functions used by this plugins
251  *****************************************************************************/
252 typedef struct
253 {
254     vlc_bool_t  b_default;
255     vlc_bool_t  b_enabled;
256     int         i_number;
257
258     int         i_extra_data;
259     uint8_t     *p_extra_data;
260
261     char         *psz_codec;
262
263     uint64_t     i_default_duration;
264     float        f_timecodescale;
265
266     /* video */
267     es_format_t fmt;
268     float       f_fps;
269     es_out_id_t *p_es;
270
271     vlc_bool_t      b_inited;
272     /* data to be send first */
273     int             i_data_init;
274     uint8_t         *p_data_init;
275
276     /* hack : it's for seek */
277     vlc_bool_t      b_search_keyframe;
278
279     /* informative */
280     char         *psz_codec_name;
281     char         *psz_codec_settings;
282     char         *psz_codec_info_url;
283     char         *psz_codec_download_url;
284     
285     /* encryption/compression */
286     int           i_compression_type;
287
288 } mkv_track_t;
289
290 typedef struct
291 {
292     int     i_track;
293     int     i_block_number;
294
295     int64_t i_position;
296     int64_t i_time;
297
298     vlc_bool_t b_key;
299 } mkv_index_t;
300
301 struct demux_sys_t
302 {
303     vlc_stream_io_callback  *in;
304     EbmlStream              *es;
305     EbmlParser              *ep;
306
307     /* time scale */
308     uint64_t                i_timescale;
309
310     /* duration of the segment */
311     float                   f_duration;
312
313     /* all tracks */
314     int                     i_track;
315     mkv_track_t             *track;
316
317     /* from seekhead */
318     int64_t                 i_cues_position;
319     int64_t                 i_chapters_position;
320     int64_t                 i_tags_position;
321
322     /* current data */
323     KaxSegment              *segment;
324     KaxCluster              *cluster;
325
326     mtime_t                 i_pts;
327
328     vlc_bool_t              b_cues;
329     int                     i_index;
330     int                     i_index_max;
331     mkv_index_t             *index;
332
333     /* info */
334     char                    *psz_muxing_application;
335     char                    *psz_writing_application;
336     char                    *psz_segment_filename;
337     char                    *psz_title;
338     char                    *psz_date_utc;
339
340     vlc_meta_t              *meta;
341
342     input_title_t           *title;
343 };
344
345 #define MKVD_TIMECODESCALE 1000000
346
347 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
348
349 static void IndexAppendCluster  ( demux_t *p_demux, KaxCluster *cluster );
350 static char *UTF8ToStr          ( const UTFstring &u );
351 static void LoadCues            ( demux_t * );
352 static void InformationCreate  ( demux_t * );
353
354 static void ParseInfo( demux_t *, EbmlElement *info );
355 static void ParseTracks( demux_t *, EbmlElement *tracks );
356 static void ParseSeekHead( demux_t *, EbmlElement *seekhead );
357 static void ParseChapters( demux_t *, EbmlElement *chapters );
358
359 /*****************************************************************************
360  * Open: initializes matroska demux structures
361  *****************************************************************************/
362 static int Open( vlc_object_t * p_this )
363 {
364     demux_t     *p_demux = (demux_t*)p_this;
365     demux_sys_t *p_sys;
366     uint8_t     *p_peek;
367
368     int          i_track;
369
370     EbmlElement *el = NULL, *el1 = NULL;
371
372     /* peek the begining */
373     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
374     {
375         msg_Warn( p_demux, "cannot peek" );
376         return VLC_EGENERIC;
377     }
378
379     /* is a valid file */
380     if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
381         p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
382     {
383         msg_Warn( p_demux, "matroska module discarded "
384                            "(invalid header 0x%.2x%.2x%.2x%.2x)",
385                            p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
386         return VLC_EGENERIC;
387     }
388
389     /* Set the demux function */
390     p_demux->pf_demux   = Demux;
391     p_demux->pf_control = Control;
392     p_demux->p_sys      = p_sys = (demux_sys_t*)malloc(sizeof( demux_sys_t ));
393
394     memset( p_sys, 0, sizeof( demux_sys_t ) );
395     p_sys->in = new vlc_stream_io_callback( p_demux->s );
396     p_sys->es = new EbmlStream( *p_sys->in );
397     p_sys->f_duration   = -1;
398     p_sys->i_timescale     = MKVD_TIMECODESCALE;
399     p_sys->i_track      = 0;
400     p_sys->track        = (mkv_track_t*)malloc( sizeof( mkv_track_t ) );
401     p_sys->i_pts   = 0;
402     p_sys->i_cues_position = -1;
403     p_sys->i_chapters_position = -1;
404     p_sys->i_tags_position = -1;
405
406     p_sys->b_cues       = VLC_FALSE;
407     p_sys->i_index      = 0;
408     p_sys->i_index_max  = 1024;
409     p_sys->index        = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
410                                                 p_sys->i_index_max );
411
412     p_sys->psz_muxing_application = NULL;
413     p_sys->psz_writing_application = NULL;
414     p_sys->psz_segment_filename = NULL;
415     p_sys->psz_title = NULL;
416     p_sys->psz_date_utc = NULL;;
417     p_sys->meta = NULL;
418     p_sys->title = NULL;
419
420     if( p_sys->es == NULL )
421     {
422         msg_Err( p_demux, "failed to create EbmlStream" );
423         delete p_sys->in;
424         free( p_sys );
425         return VLC_EGENERIC;
426     }
427     /* Find the EbmlHead element */
428     el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
429     if( el == NULL )
430     {
431         msg_Err( p_demux, "cannot find EbmlHead" );
432         goto error;
433     }
434     msg_Dbg( p_demux, "EbmlHead" );
435     /* skip it */
436     el->SkipData( *p_sys->es, el->Generic().Context );
437     delete el;
438
439     /* Find a segment */
440     el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
441     if( el == NULL )
442     {
443         msg_Err( p_demux, "cannot find KaxSegment" );
444         goto error;
445     }
446     MkvTree( p_demux, 0, "Segment" );
447     p_sys->segment = (KaxSegment*)el;
448     p_sys->cluster = NULL;
449
450     p_sys->ep = new EbmlParser( p_sys->es, el );
451
452     while( ( el1 = p_sys->ep->Get() ) != NULL )
453     {
454         if( MKV_IS_ID( el1, KaxInfo ) )
455         {
456             ParseInfo( p_demux, el1 );
457         }
458         else if( MKV_IS_ID( el1, KaxTracks ) )
459         {
460             ParseTracks( p_demux, el1 );
461         }
462         else if( MKV_IS_ID( el1, KaxSeekHead ) )
463         {
464             ParseSeekHead( p_demux, el1 );
465         }
466         else if( MKV_IS_ID( el1, KaxCues ) )
467         {
468             msg_Dbg( p_demux, "|   + Cues" );
469         }
470         else if( MKV_IS_ID( el1, KaxCluster ) )
471         {
472             msg_Dbg( p_demux, "|   + Cluster" );
473
474             p_sys->cluster = (KaxCluster*)el1;
475
476             p_sys->ep->Down();
477             /* stop parsing the stream */
478             break;
479         }
480         else if( MKV_IS_ID( el1, KaxAttachments ) )
481         {
482             msg_Dbg( p_demux, "|   + Attachments FIXME TODO (but probably never supported)" );
483         }
484         else if( MKV_IS_ID( el1, KaxChapters ) )
485         {
486             msg_Dbg( p_demux, "|   + Chapters" );
487             ParseChapters( p_demux, el1 );
488         }
489         else if( MKV_IS_ID( el1, KaxTag ) )
490         {
491             msg_Dbg( p_demux, "|   + Tags FIXME TODO" );
492         }
493         else
494         {
495             msg_Dbg( p_demux, "|   + Unknown (%s)", typeid(*el1).name() );
496         }
497     }
498
499     if( p_sys->cluster == NULL )
500     {
501         msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
502         goto error;
503     }
504
505     /* *** Load the cue if found *** */
506     if( p_sys->i_cues_position >= 0 )
507     {
508         vlc_bool_t b_seekable;
509
510         stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
511         if( b_seekable )
512         {
513             LoadCues( p_demux );
514         }
515     }
516
517     if( !p_sys->b_cues || p_sys->i_index <= 0 )
518     {
519         msg_Warn( p_demux, "no cues/empty cues found->seek won't be precise" );
520
521         IndexAppendCluster( p_demux, p_sys->cluster );
522
523         p_sys->b_cues = VLC_FALSE;
524     }
525
526     /* add all es */
527     msg_Dbg( p_demux, "found %d es", p_sys->i_track );
528     for( i_track = 0; i_track < p_sys->i_track; i_track++ )
529     {
530 #define tk  p_sys->track[i_track]
531         if( tk.fmt.i_cat == UNKNOWN_ES )
532         {
533             msg_Warn( p_demux, "invalid track[%d, n=%d]", i_track, tk.i_number );
534             tk.p_es = NULL;
535             continue;
536         }
537
538         if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) )
539         {
540             if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
541             {
542                 msg_Err( p_demux, "missing/invalid BITMAPINFOHEADER" );
543                 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
544             }
545             else
546             {
547                 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
548
549                 tk.fmt.video.i_width = GetDWLE( &p_bih->biWidth );
550                 tk.fmt.video.i_height= GetDWLE( &p_bih->biHeight );
551                 tk.fmt.i_codec       = GetFOURCC( &p_bih->biCompression );
552
553                 tk.fmt.i_extra       = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
554                 if( tk.fmt.i_extra > 0 )
555                 {
556                     tk.fmt.p_extra = malloc( tk.fmt.i_extra );
557                     memcpy( tk.fmt.p_extra, &p_bih[1], tk.fmt.i_extra );
558                 }
559             }
560         }
561         else if( !strcmp( tk.psz_codec, "V_MPEG1" ) ||
562                  !strcmp( tk.psz_codec, "V_MPEG2" ) )
563         {
564             tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
565         }
566         else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) )
567         {
568             if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) )
569             {
570                 tk.fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
571             }
572             else
573             {
574                 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
575             }
576         }
577         else if( !strcmp( tk.psz_codec, "V_QUICKTIME" ) )
578         {
579             MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
580             stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(p_demux),
581                                                        tk.p_extra_data,
582                                                        tk.i_extra_data );
583             MP4_ReadBoxCommon( p_mp4_stream, p_box );
584             MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
585             tk.fmt.i_codec = p_box->i_type;
586             tk.fmt.video.i_width = p_box->data.p_sample_vide->i_width;
587             tk.fmt.video.i_height = p_box->data.p_sample_vide->i_height;
588             tk.fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
589             tk.fmt.p_extra = malloc( tk.fmt.i_extra );
590             memcpy( tk.fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tk.fmt.i_extra );
591             MP4_FreeBox_sample_vide( p_box );
592             stream_MemoryDelete( p_mp4_stream, VLC_TRUE );
593         }
594         else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
595         {
596             if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
597             {
598                 msg_Err( p_demux, "missing/invalid WAVEFORMATEX" );
599                 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
600             }
601             else
602             {
603                 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;
604
605                 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk.fmt.i_codec, NULL );
606
607                 tk.fmt.audio.i_channels   = GetWLE( &p_wf->nChannels );
608                 tk.fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
609                 tk.fmt.i_bitrate    = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
610                 tk.fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
611                 tk.fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
612
613                 tk.fmt.i_extra            = GetWLE( &p_wf->cbSize );
614                 if( tk.fmt.i_extra > 0 )
615                 {
616                     tk.fmt.p_extra = malloc( tk.fmt.i_extra );
617                     memcpy( tk.fmt.p_extra, &p_wf[1], tk.fmt.i_extra );
618                 }
619             }
620         }
621         else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
622                  !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
623                  !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
624         {
625             tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
626         }
627         else if( !strcmp( tk.psz_codec, "A_AC3" ) )
628         {
629             tk.fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
630         }
631         else if( !strcmp( tk.psz_codec, "A_DTS" ) )
632         {
633             tk.fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
634         }
635         else if( !strcmp( tk.psz_codec, "A_FLAC" ) )
636         {
637             tk.fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
638             tk.fmt.i_extra = tk.i_extra_data;
639             tk.fmt.p_extra = malloc( tk.i_extra_data );
640             memcpy( tk.fmt.p_extra,tk.p_extra_data, tk.i_extra_data );
641         }
642         else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
643         {
644             int i, i_offset = 1, i_size[3], i_extra;
645             uint8_t *p_extra;
646
647             tk.fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
648
649             /* Split the 3 headers */
650             if( tk.p_extra_data[0] != 0x02 )
651                 msg_Err( p_demux, "invalid vorbis header" );
652
653             for( i = 0; i < 2; i++ )
654             {
655                 i_size[i] = 0;
656                 while( i_offset < tk.i_extra_data )
657                 {
658                     i_size[i] += tk.p_extra_data[i_offset];
659                     if( tk.p_extra_data[i_offset++] != 0xff ) break;
660                 }
661             }
662
663             i_size[0] = __MIN(i_size[0], tk.i_extra_data - i_offset);
664             i_size[1] = __MIN(i_size[1], tk.i_extra_data -i_offset -i_size[0]);
665             i_size[2] = tk.i_extra_data - i_offset - i_size[0] - i_size[1];
666
667             tk.fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
668             tk.fmt.p_extra = malloc( tk.fmt.i_extra );
669             p_extra = (uint8_t *)tk.fmt.p_extra; i_extra = 0;
670             for( i = 0; i < 3; i++ )
671             {
672                 *(p_extra++) = i_size[i] >> 8;
673                 *(p_extra++) = i_size[i] & 0xFF;
674                 memcpy( p_extra, tk.p_extra_data + i_offset + i_extra,
675                         i_size[i] );
676                 p_extra += i_size[i];
677                 i_extra += i_size[i];
678             }
679         }
680         else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
681                  !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
682         {
683             int i_profile, i_srate;
684             static unsigned int i_sample_rates[] =
685             {
686                     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
687                         16000, 12000, 11025, 8000,  7350,  0,     0,     0
688             };
689
690             tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
691             /* create data for faad (MP4DecSpecificDescrTag)*/
692
693             if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
694             {
695                 i_profile = 0;
696             }
697             else if( !strcmp( &tk.psz_codec[12], "LC" ) )
698             {
699                 i_profile = 1;
700             }
701             else if( !strcmp( &tk.psz_codec[12], "SSR" ) )
702             {
703                 i_profile = 2;
704             }
705             else
706             {
707                 i_profile = 3;
708             }
709
710             for( i_srate = 0; i_srate < 13; i_srate++ )
711             {
712                 if( i_sample_rates[i_srate] == tk.fmt.audio.i_rate )
713                 {
714                     break;
715                 }
716             }
717             msg_Dbg( p_demux, "profile=%d srate=%d", i_profile, i_srate );
718
719             tk.fmt.i_extra = 2;
720             tk.fmt.p_extra = malloc( tk.fmt.i_extra );
721             ((uint8_t*)tk.fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
722             ((uint8_t*)tk.fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk.fmt.audio.i_channels << 3);
723         }
724         else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
725                  !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
726                  !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
727         {
728             if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
729             {
730                 tk.fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
731             }
732             else
733             {
734                 tk.fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
735             }
736             tk.fmt.audio.i_blockalign = ( tk.fmt.audio.i_bitspersample + 7 ) / 8 * tk.fmt.audio.i_channels;
737         }
738         else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
739         {
740             tk.fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
741             tk.fmt.subs.psz_encoding = strdup( "UTF-8" );
742         }
743         else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) ||
744                  !strcmp( tk.psz_codec, "S_TEXT/ASS" ) ||
745                  !strcmp( tk.psz_codec, "S_SSA" ) ||
746                  !strcmp( tk.psz_codec, "S_ASS" ))
747         {
748             tk.fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
749             tk.fmt.subs.psz_encoding = strdup( "UTF-8" );
750         }
751         else if( !strcmp( tk.psz_codec, "S_VOBSUB" ) )
752         {
753             tk.fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
754             if( tk.i_extra_data )
755             {
756                 char *p_start;
757                 char *p_buf = (char *)malloc( tk.i_extra_data + 1);
758                 memcpy( p_buf, tk.p_extra_data , tk.i_extra_data );
759                 p_buf[tk.i_extra_data] = '\0';
760                 
761                 p_start = strstr( p_buf, "size:" );
762                 if( sscanf( p_start, "size: %dx%d",
763                         &tk.fmt.subs.spu.i_original_frame_width, &tk.fmt.subs.spu.i_original_frame_height ) == 2 )
764                 {
765                     msg_Dbg( p_demux, "original frame size vobsubs: %dx%d", tk.fmt.subs.spu.i_original_frame_width, tk.fmt.subs.spu.i_original_frame_height );
766                 }
767                 else
768                 {
769                     msg_Warn( p_demux, "reading original frame size for vobsub failed" );
770                 }
771                 free( p_buf );
772             }
773         }
774         else
775         {
776             msg_Err( p_demux, "unknow codec id=`%s'", tk.psz_codec );
777             tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
778         }
779         if( tk.b_default )
780         {
781             tk.fmt.i_priority = 1000;
782         }
783
784         tk.p_es = es_out_Add( p_demux->out, &tk.fmt );
785 #undef tk
786     }
787
788     /* add information */
789     InformationCreate( p_demux );
790
791     return VLC_SUCCESS;
792
793 error:
794     delete p_sys->es;
795     delete p_sys->in;
796     free( p_sys );
797     return VLC_EGENERIC;
798 }
799
800 /*****************************************************************************
801  * Close: frees unused data
802  *****************************************************************************/
803 static void Close( vlc_object_t *p_this )
804 {
805     demux_t     *p_demux = (demux_t*)p_this;
806     demux_sys_t *p_sys   = p_demux->p_sys;
807     int         i_track;
808
809     for( i_track = 0; i_track < p_sys->i_track; i_track++ )
810     {
811 #define tk  p_sys->track[i_track]
812         if( tk.fmt.psz_description )
813         {
814             free( tk.fmt.psz_description );
815         }
816         if( tk.psz_codec )
817         {
818             free( tk.psz_codec );
819         }
820         if( tk.fmt.psz_language )
821         {
822             free( tk.fmt.psz_language );
823         }
824 #undef tk
825     }
826     free( p_sys->track );
827
828     if( p_sys->psz_writing_application  )
829     {
830         free( p_sys->psz_writing_application );
831     }
832     if( p_sys->psz_muxing_application  )
833     {
834         free( p_sys->psz_muxing_application );
835     }
836
837     delete p_sys->segment;
838
839     delete p_sys->ep;
840     delete p_sys->es;
841     delete p_sys->in;
842
843     free( p_sys );
844 }
845
846 /*****************************************************************************
847  * Control:
848  *****************************************************************************/
849 static int Control( demux_t *p_demux, int i_query, va_list args )
850 {
851     demux_sys_t *p_sys = p_demux->p_sys;
852     int64_t     *pi64;
853     double      *pf, f;
854
855     vlc_meta_t **pp_meta;
856
857     switch( i_query )
858     {
859         case DEMUX_GET_META:
860             pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
861             *pp_meta = vlc_meta_Duplicate( p_sys->meta );
862             return VLC_SUCCESS;
863
864         case DEMUX_GET_LENGTH:
865             pi64 = (int64_t*)va_arg( args, int64_t * );
866             if( p_sys->f_duration > 0.0 )
867             {
868                 *pi64 = (int64_t)(p_sys->f_duration * 1000);
869                 return VLC_SUCCESS;
870             }
871             return VLC_EGENERIC;
872
873         case DEMUX_GET_POSITION:
874             pf = (double*)va_arg( args, double * );
875             *pf = (double)p_sys->in->getFilePointer() / (double)stream_Size( p_demux->s );
876             return VLC_SUCCESS;
877
878         case DEMUX_SET_POSITION:
879             f = (double)va_arg( args, double );
880             Seek( p_demux, -1, (int)(100.0 * f) );
881             return VLC_SUCCESS;
882
883         case DEMUX_GET_TIME:
884             pi64 = (int64_t*)va_arg( args, int64_t * );
885             if( p_sys->f_duration > 0.0 )
886             {
887                 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
888
889                 /* FIXME */
890                 *pi64 = (mtime_t)1000000 *
891                         (mtime_t)i_duration*
892                         (mtime_t)p_sys->in->getFilePointer() /
893                         (mtime_t)stream_Size( p_demux->s );
894                 return VLC_SUCCESS;
895             }
896             return VLC_EGENERIC;
897
898         case DEMUX_GET_TITLE_INFO:
899             if( p_sys->title && p_sys->title->i_seekpoint > 0 )
900             {
901                 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
902                 int *pi_int    = (int*)va_arg( args, int* );
903
904                 *pi_int = 1;
905                 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) );
906
907                 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->title );
908
909                 return VLC_SUCCESS;
910             }
911             return VLC_EGENERIC;
912
913         case DEMUX_SET_TITLE:
914             if( p_sys->title && p_sys->title->i_seekpoint > 0 )
915             {
916                 return VLC_SUCCESS;
917             }
918             return VLC_EGENERIC;
919
920         case DEMUX_SET_SEEKPOINT:
921             /* FIXME do a better implementation */
922             if( p_sys->title && p_sys->title->i_seekpoint > 0 )
923             {
924                 int i_skp = (int)va_arg( args, int );
925
926                 Seek( p_demux, (int64_t)p_sys->title->seekpoint[i_skp]->i_time_offset, -1);
927                 return VLC_SUCCESS;
928             }
929             return VLC_EGENERIC;
930
931
932         case DEMUX_SET_TIME:
933         case DEMUX_GET_FPS:
934         default:
935             return VLC_EGENERIC;
936     }
937 }
938
939 static int BlockGet( demux_t *p_demux, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
940 {
941     demux_sys_t *p_sys = p_demux->p_sys;
942
943     *pp_block = NULL;
944     *pi_ref1  = -1;
945     *pi_ref2  = -1;
946
947     for( ;; )
948     {
949         EbmlElement *el;
950         int         i_level;
951
952         if( p_demux->b_die )
953         {
954             return VLC_EGENERIC;
955         }
956
957         el = p_sys->ep->Get();
958         i_level = p_sys->ep->GetLevel();
959
960         if( el == NULL && *pp_block != NULL )
961         {
962             /* update the index */
963 #define idx p_sys->index[p_sys->i_index - 1]
964             if( p_sys->i_index > 0 && idx.i_time == -1 )
965             {
966                 idx.i_time        = (*pp_block)->GlobalTimecode() / (mtime_t)1000;
967                 idx.b_key         = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
968             }
969 #undef idx
970             return VLC_SUCCESS;
971         }
972
973         if( el == NULL )
974         {
975             if( p_sys->ep->GetLevel() > 1 )
976             {
977                 p_sys->ep->Up();
978                 continue;
979             }
980             msg_Warn( p_demux, "EOF" );
981             return VLC_EGENERIC;
982         }
983
984         /* do parsing */
985         if( i_level == 1 )
986         {
987             if( MKV_IS_ID( el, KaxCluster ) )
988             {
989                 p_sys->cluster = (KaxCluster*)el;
990
991                 /* add it to the index */
992                 if( p_sys->i_index == 0 ||
993                     ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
994                 {
995                     IndexAppendCluster( p_demux, p_sys->cluster );
996                 }
997
998                 p_sys->ep->Down();
999             }
1000             else if( MKV_IS_ID( el, KaxCues ) )
1001             {
1002                 msg_Warn( p_demux, "find KaxCues FIXME" );
1003                 return VLC_EGENERIC;
1004             }
1005             else
1006             {
1007                 msg_Dbg( p_demux, "unknown (%s)", typeid( el ).name() );
1008             }
1009         }
1010         else if( i_level == 2 )
1011         {
1012             if( MKV_IS_ID( el, KaxClusterTimecode ) )
1013             {
1014                 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1015
1016                 ctc.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
1017                 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
1018             }
1019             else if( MKV_IS_ID( el, KaxBlockGroup ) )
1020             {
1021                 p_sys->ep->Down();
1022             }
1023         }
1024         else if( i_level == 3 )
1025         {
1026             if( MKV_IS_ID( el, KaxBlock ) )
1027             {
1028                 *pp_block = (KaxBlock*)el;
1029
1030                 (*pp_block)->ReadData( p_sys->es->I_O() );
1031                 (*pp_block)->SetParent( *p_sys->cluster );
1032
1033                 p_sys->ep->Keep();
1034             }
1035             else if( MKV_IS_ID( el, KaxBlockDuration ) )
1036             {
1037                 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1038
1039                 dur.ReadData( p_sys->es->I_O() );
1040                 *pi_duration = uint64( dur );
1041             }
1042             else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1043             {
1044                 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1045
1046                 ref.ReadData( p_sys->es->I_O() );
1047                 if( *pi_ref1 == -1 )
1048                 {
1049                     *pi_ref1 = int64( ref );
1050                 }
1051                 else
1052                 {
1053                     *pi_ref2 = int64( ref );
1054                 }
1055             }
1056         }
1057         else
1058         {
1059             msg_Err( p_demux, "invalid level = %d", i_level );
1060             return VLC_EGENERIC;
1061         }
1062     }
1063 }
1064
1065 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1066 {
1067     block_t *p_block;
1068     if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1069     memcpy( p_block->p_buffer, p_mem, i_mem );
1070     //p_block->i_rate = p_input->stream.control.i_rate;
1071     return p_block;
1072 }
1073
1074 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1075                          mtime_t i_duration )
1076 {
1077     demux_sys_t *p_sys = p_demux->p_sys;
1078
1079     int             i_track;
1080     unsigned int    i;
1081     vlc_bool_t      b;
1082
1083 #define tk  p_sys->track[i_track]
1084     for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1085     {
1086         if( tk.i_number == block->TrackNum() )
1087         {
1088             break;
1089         }
1090     }
1091
1092     if( i_track >= p_sys->i_track )
1093     {
1094         msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1095         return;
1096     }
1097
1098     es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk.p_es, &b );
1099     if( !b )
1100     {
1101         tk.b_inited = VLC_FALSE;
1102         return;
1103     }
1104
1105     /* First send init data */
1106     if( !tk.b_inited && tk.i_data_init > 0 )
1107     {
1108         block_t *p_init;
1109
1110         msg_Dbg( p_demux, "sending header (%d bytes)", tk.i_data_init );
1111         p_init = MemToBlock( p_demux, tk.p_data_init, tk.i_data_init );
1112         if( p_init ) es_out_Send( p_demux->out, tk.p_es, p_init );
1113     }
1114     tk.b_inited = VLC_TRUE;
1115
1116
1117     for( i = 0; i < block->NumberFrames(); i++ )
1118     {
1119         block_t *p_block;
1120         DataBuffer &data = block->GetBuffer(i);
1121
1122         p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1123
1124         if( p_block == NULL )
1125         {
1126             break;
1127         }
1128
1129 #if defined(HAVE_ZLIB_H)
1130         if( tk.i_compression_type )
1131         {
1132             p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
1133         }
1134 #endif
1135
1136         if( tk.fmt.i_cat != VIDEO_ES )
1137             p_block->i_dts = p_block->i_pts = i_pts;
1138         else
1139         {
1140             p_block->i_dts = i_pts;
1141             p_block->i_pts = 0;
1142         }
1143
1144         if( tk.fmt.i_cat == SPU_ES && strcmp( tk.psz_codec, "S_VOBSUB" ) )
1145         {
1146             p_block->i_length = i_duration * 1000;
1147         }
1148         es_out_Send( p_demux->out, tk.p_es, p_block );
1149
1150         /* use time stamp only for first block */
1151         i_pts = 0;
1152     }
1153
1154 #undef tk
1155 }
1156
1157 static void Seek( demux_t *p_demux, mtime_t i_date, int i_percent)
1158 {
1159     demux_sys_t *p_sys = p_demux->p_sys;
1160
1161     KaxBlock    *block;
1162     int64_t     i_block_duration;
1163     int64_t     i_block_ref1;
1164     int64_t     i_block_ref2;
1165
1166     int         i_index;
1167     int         i_track_skipping;
1168     int         i_track;
1169
1170     msg_Dbg( p_demux, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1171     if( i_date < 0 && i_percent < 0 )
1172     {
1173         return;
1174     }
1175     if( i_percent > 100 ) i_percent = 100;
1176
1177     delete p_sys->ep;
1178     p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
1179     p_sys->cluster = NULL;
1180
1181     /* seek without index or without date */
1182     if( config_GetInt( p_demux, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
1183     {
1184         int64_t i_pos = i_percent * stream_Size( p_demux->s ) / 100;
1185
1186         msg_Dbg( p_demux, "inacurate way of seeking" );
1187         for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1188         {
1189             if( p_sys->index[i_index].i_position >= i_pos)
1190             {
1191                 break;
1192             }
1193         }
1194         if( i_index == p_sys->i_index )
1195         {
1196             i_index--;
1197         }
1198
1199         p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1200                                    seek_beginning );
1201
1202         if( p_sys->index[i_index].i_position < i_pos )
1203         {
1204             EbmlElement *el;
1205
1206             msg_Warn( p_demux, "searching for cluster, could take some time" );
1207
1208             /* search a cluster */
1209             while( ( el = p_sys->ep->Get() ) != NULL )
1210             {
1211                 if( MKV_IS_ID( el, KaxCluster ) )
1212                 {
1213                     KaxCluster *cluster = (KaxCluster*)el;
1214
1215                     /* add it to the index */
1216                     IndexAppendCluster( p_demux, cluster );
1217
1218                     if( (int64_t)cluster->GetElementPosition() >= i_pos )
1219                     {
1220                         p_sys->cluster = cluster;
1221                         p_sys->ep->Down();
1222                         break;
1223                     }
1224                 }
1225             }
1226         }
1227     }
1228     else
1229     {
1230         for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1231         {
1232             if( p_sys->index[i_index].i_time >= i_date )
1233             {
1234                 break;
1235             }
1236         }
1237
1238         if( i_index > 0 )
1239         {
1240             i_index--;
1241         }
1242
1243         msg_Dbg( p_demux, "seek got "I64Fd" (%d%%)",
1244                  p_sys->index[i_index].i_time,
1245                  (int)( 100 * p_sys->index[i_index].i_position /
1246                         stream_Size( p_demux->s ) ) );
1247
1248         p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1249                                    seek_beginning );
1250     }
1251
1252     /* now parse until key frame */
1253 #define tk  p_sys->track[i_track]
1254     i_track_skipping = 0;
1255     for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1256     {
1257         if( tk.fmt.i_cat == VIDEO_ES )
1258         {
1259             tk.b_search_keyframe = VLC_TRUE;
1260             i_track_skipping++;
1261         }
1262     }
1263
1264     while( i_track_skipping > 0 )
1265     {
1266         if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1267         {
1268             msg_Warn( p_demux, "cannot get block EOF?" );
1269
1270             return;
1271         }
1272
1273         p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000 + 1;
1274
1275         for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1276         {
1277             if( tk.i_number == block->TrackNum() )
1278             {
1279                 break;
1280             }
1281         }
1282
1283         if( i_track < p_sys->i_track )
1284         {
1285             if( tk.fmt.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1286             {
1287                 tk.b_search_keyframe = VLC_FALSE;
1288                 i_track_skipping--;
1289             }
1290             if( tk.fmt.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1291             {
1292                 BlockDecode( p_demux, block, 0, 0 );
1293             }
1294         }
1295
1296         delete block;
1297     }
1298 #undef tk
1299 }
1300
1301 /*****************************************************************************
1302  * Demux: reads and demuxes data packets
1303  *****************************************************************************
1304  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1305  *****************************************************************************/
1306 static int Demux( demux_t *p_demux)
1307 {
1308     demux_sys_t *p_sys = p_demux->p_sys;
1309     mtime_t        i_start_pts;
1310     int            i_block_count = 0;
1311
1312     KaxBlock *block;
1313     int64_t i_block_duration;
1314     int64_t i_block_ref1;
1315     int64_t i_block_ref2;
1316
1317     i_start_pts = -1;
1318
1319     for( ;; )
1320     {
1321         if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1322         {
1323             msg_Warn( p_demux, "cannot get block EOF?" );
1324
1325             return 0;
1326         }
1327
1328         p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000 + 1;
1329
1330         if( p_sys->i_pts > 0 )
1331         {
1332             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
1333         }
1334
1335         BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
1336
1337         delete block;
1338         i_block_count++;
1339
1340         if( i_start_pts == -1 )
1341         {
1342             i_start_pts = p_sys->i_pts;
1343         }
1344         else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1345         {
1346             return 1;
1347         }
1348     }
1349 }
1350
1351
1352
1353 /*****************************************************************************
1354  * Stream managment
1355  *****************************************************************************/
1356 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1357 {
1358     s = s_;
1359     mb_eof = VLC_FALSE;
1360 }
1361
1362 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1363 {
1364     if( i_size <= 0 || mb_eof )
1365     {
1366         return 0;
1367     }
1368
1369     return stream_Read( s, p_buffer, i_size );
1370 }
1371 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1372 {
1373     int64_t i_pos;
1374
1375     switch( mode )
1376     {
1377         case seek_beginning:
1378             i_pos = i_offset;
1379             break;
1380         case seek_end:
1381             i_pos = stream_Size( s ) - i_offset;
1382             break;
1383         default:
1384             i_pos= stream_Tell( s ) + i_offset;
1385             break;
1386     }
1387
1388     if( i_pos < 0 || i_pos >= stream_Size( s ) )
1389     {
1390         mb_eof = VLC_TRUE;
1391         return;
1392     }
1393
1394     mb_eof = VLC_FALSE;
1395     if( stream_Seek( s, i_pos ) )
1396     {
1397         mb_eof = VLC_TRUE;
1398     }
1399     return;
1400 }
1401 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1402 {
1403     return 0;
1404 }
1405 uint64 vlc_stream_io_callback::getFilePointer( void )
1406 {
1407     return stream_Tell( s );
1408 }
1409 void vlc_stream_io_callback::close( void )
1410 {
1411     return;
1412 }
1413
1414
1415 /*****************************************************************************
1416  * Ebml Stream parser
1417  *****************************************************************************/
1418 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1419 {
1420     int i;
1421
1422     m_es = es;
1423     m_got = NULL;
1424     m_el[0] = el_start;
1425
1426     for( i = 1; i < 6; i++ )
1427     {
1428         m_el[i] = NULL;
1429     }
1430     mi_level = 1;
1431     mi_user_level = 1;
1432     mb_keep = VLC_FALSE;
1433 }
1434
1435 EbmlParser::~EbmlParser( void )
1436 {
1437     int i;
1438
1439     for( i = 1; i < mi_level; i++ )
1440     {
1441         if( !mb_keep )
1442         {
1443             delete m_el[i];
1444         }
1445         mb_keep = VLC_FALSE;
1446     }
1447 }
1448
1449 void EbmlParser::Up( void )
1450 {
1451     if( mi_user_level == mi_level )
1452     {
1453         fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1454     }
1455
1456     mi_user_level--;
1457 }
1458
1459 void EbmlParser::Down( void )
1460 {
1461     mi_user_level++;
1462     mi_level++;
1463 }
1464
1465 void EbmlParser::Keep( void )
1466 {
1467     mb_keep = VLC_TRUE;
1468 }
1469
1470 int EbmlParser::GetLevel( void )
1471 {
1472     return mi_user_level;
1473 }
1474
1475 EbmlElement *EbmlParser::Get( void )
1476 {
1477     int i_ulev = 0;
1478
1479     if( mi_user_level != mi_level )
1480     {
1481         return NULL;
1482     }
1483     if( m_got )
1484     {
1485         EbmlElement *ret = m_got;
1486         m_got = NULL;
1487
1488         return ret;
1489     }
1490
1491     if( m_el[mi_level] )
1492     {
1493         m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
1494         if( !mb_keep )
1495         {
1496             delete m_el[mi_level];
1497         }
1498         mb_keep = VLC_FALSE;
1499     }
1500
1501     m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
1502     if( i_ulev > 0 )
1503     {
1504         while( i_ulev > 0 )
1505         {
1506             if( mi_level == 1 )
1507             {
1508                 mi_level = 0;
1509                 return NULL;
1510             }
1511
1512             delete m_el[mi_level - 1];
1513             m_got = m_el[mi_level -1] = m_el[mi_level];
1514             m_el[mi_level] = NULL;
1515
1516             mi_level--;
1517             i_ulev--;
1518         }
1519         return NULL;
1520     }
1521     else if( m_el[mi_level] == NULL )
1522     {
1523         fprintf( stderr," m_el[mi_level] == NULL\n" );
1524     }
1525
1526     return m_el[mi_level];
1527 }
1528
1529
1530 /*****************************************************************************
1531  * Tools
1532  *  * LoadCues : load the cues element and update index
1533  *
1534  *  * LoadTags : load ... the tags element
1535  *
1536  *  * InformationCreate : create all information, load tags if present
1537  *
1538  *****************************************************************************/
1539 static void LoadCues( demux_t *p_demux )
1540 {
1541     demux_sys_t *p_sys = p_demux->p_sys;
1542     int64_t     i_sav_position = p_sys->in->getFilePointer();
1543     EbmlParser  *ep;
1544     EbmlElement *el, *cues;
1545
1546     msg_Dbg( p_demux, "loading cues" );
1547     p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
1548     cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
1549
1550     if( cues == NULL )
1551     {
1552         msg_Err( p_demux, "cannot load cues (broken seekhead or file)" );
1553         p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1554         return;
1555     }
1556
1557     ep = new EbmlParser( p_sys->es, cues );
1558     while( ( el = ep->Get() ) != NULL )
1559     {
1560         if( MKV_IS_ID( el, KaxCuePoint ) )
1561         {
1562 #define idx p_sys->index[p_sys->i_index]
1563
1564             idx.i_track       = -1;
1565             idx.i_block_number= -1;
1566             idx.i_position    = -1;
1567             idx.i_time        = 0;
1568             idx.b_key         = VLC_TRUE;
1569
1570             ep->Down();
1571             while( ( el = ep->Get() ) != NULL )
1572             {
1573                 if( MKV_IS_ID( el, KaxCueTime ) )
1574                 {
1575                     KaxCueTime &ctime = *(KaxCueTime*)el;
1576
1577                     ctime.ReadData( p_sys->es->I_O() );
1578
1579                     idx.i_time = uint64( ctime ) * p_sys->i_timescale / (mtime_t)1000;
1580                 }
1581                 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
1582                 {
1583                     ep->Down();
1584                     while( ( el = ep->Get() ) != NULL )
1585                     {
1586                         if( MKV_IS_ID( el, KaxCueTrack ) )
1587                         {
1588                             KaxCueTrack &ctrack = *(KaxCueTrack*)el;
1589
1590                             ctrack.ReadData( p_sys->es->I_O() );
1591                             idx.i_track = uint16( ctrack );
1592                         }
1593                         else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
1594                         {
1595                             KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
1596
1597                             ccpos.ReadData( p_sys->es->I_O() );
1598                             idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
1599                         }
1600                         else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
1601                         {
1602                             KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
1603
1604                             cbnum.ReadData( p_sys->es->I_O() );
1605                             idx.i_block_number = uint32( cbnum );
1606                         }
1607                         else
1608                         {
1609                             msg_Dbg( p_demux, "         * Unknown (%s)", typeid(*el).name() );
1610                         }
1611                     }
1612                     ep->Up();
1613                 }
1614                 else
1615                 {
1616                     msg_Dbg( p_demux, "     * Unknown (%s)", typeid(*el).name() );
1617                 }
1618             }
1619             ep->Up();
1620
1621 #if 0
1622             msg_Dbg( p_demux, " * added time="I64Fd" pos="I64Fd
1623                      " track=%d bnum=%d", idx.i_time, idx.i_position,
1624                      idx.i_track, idx.i_block_number );
1625 #endif
1626
1627             p_sys->i_index++;
1628             if( p_sys->i_index >= p_sys->i_index_max )
1629             {
1630                 p_sys->i_index_max += 1024;
1631                 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
1632             }
1633 #undef idx
1634         }
1635         else
1636         {
1637             msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1638         }
1639     }
1640     delete ep;
1641     delete cues;
1642
1643     p_sys->b_cues = VLC_TRUE;
1644
1645     msg_Dbg( p_demux, "loading cues done." );
1646     p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1647 }
1648
1649 static void LoadTags( demux_t *p_demux )
1650 {
1651     demux_sys_t *p_sys = p_demux->p_sys;
1652     int64_t     i_sav_position = p_sys->in->getFilePointer();
1653     EbmlParser  *ep;
1654     EbmlElement *el, *tags;
1655
1656     msg_Dbg( p_demux, "loading tags" );
1657     p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
1658     tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
1659
1660     if( tags == NULL )
1661     {
1662         msg_Err( p_demux, "cannot load tags (broken seekhead or file)" );
1663         p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1664         return;
1665     }
1666
1667     msg_Dbg( p_demux, "Tags" );
1668     ep = new EbmlParser( p_sys->es, tags );
1669     while( ( el = ep->Get() ) != NULL )
1670     {
1671         if( MKV_IS_ID( el, KaxTag ) )
1672         {
1673             msg_Dbg( p_demux, "+ Tag" );
1674             ep->Down();
1675             while( ( el = ep->Get() ) != NULL )
1676             {
1677                 if( MKV_IS_ID( el, KaxTagTargets ) )
1678                 {
1679                     msg_Dbg( p_demux, "|   + Targets" );
1680                     ep->Down();
1681                     while( ( el = ep->Get() ) != NULL )
1682                     {
1683                         msg_Dbg( p_demux, "|   |   + Unknown (%s)", typeid( *el ).name() );
1684                     }
1685                     ep->Up();
1686                 }
1687                 else if( MKV_IS_ID( el, KaxTagGeneral ) )
1688                 {
1689                     msg_Dbg( p_demux, "|   + General" );
1690                     ep->Down();
1691                     while( ( el = ep->Get() ) != NULL )
1692                     {
1693                         msg_Dbg( p_demux, "|   |   + Unknown (%s)", typeid( *el ).name() );
1694                     }
1695                     ep->Up();
1696                 }
1697                 else if( MKV_IS_ID( el, KaxTagGenres ) )
1698                 {
1699                     msg_Dbg( p_demux, "|   + Genres" );
1700                     ep->Down();
1701                     while( ( el = ep->Get() ) != NULL )
1702                     {
1703                         msg_Dbg( p_demux, "|   |   + Unknown (%s)", typeid( *el ).name() );
1704                     }
1705                     ep->Up();
1706                 }
1707                 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
1708                 {
1709                     msg_Dbg( p_demux, "|   + Audio Specific" );
1710                     ep->Down();
1711                     while( ( el = ep->Get() ) != NULL )
1712                     {
1713                         msg_Dbg( p_demux, "|   |   + Unknown (%s)", typeid( *el ).name() );
1714                     }
1715                     ep->Up();
1716                 }
1717                 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
1718                 {
1719                     msg_Dbg( p_demux, "|   + Images Specific" );
1720                     ep->Down();
1721                     while( ( el = ep->Get() ) != NULL )
1722                     {
1723                         msg_Dbg( p_demux, "|   |   + Unknown (%s)", typeid( *el ).name() );
1724                     }
1725                     ep->Up();
1726                 }
1727                 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
1728                 {
1729                     msg_Dbg( p_demux, "|   + Multi Comment" );
1730                 }
1731                 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
1732                 {
1733                     msg_Dbg( p_demux, "|   + Multi Commercial" );
1734                 }
1735                 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
1736                 {
1737                     msg_Dbg( p_demux, "|   + Multi Date" );
1738                 }
1739                 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
1740                 {
1741                     msg_Dbg( p_demux, "|   + Multi Entity" );
1742                 }
1743                 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
1744                 {
1745                     msg_Dbg( p_demux, "|   + Multi Identifier" );
1746                 }
1747                 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
1748                 {
1749                     msg_Dbg( p_demux, "|   + Multi Legal" );
1750                 }
1751                 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
1752                 {
1753                     msg_Dbg( p_demux, "|   + Multi Title" );
1754                 }
1755                 else
1756                 {
1757                     msg_Dbg( p_demux, "|   + Unknown (%s)", typeid( *el ).name() );
1758                 }
1759             }
1760             ep->Up();
1761         }
1762         else
1763         {
1764             msg_Dbg( p_demux, "+ Unknown (%s)", typeid( *el ).name() );
1765         }
1766     }
1767     delete ep;
1768     delete tags;
1769
1770     msg_Dbg( p_demux, "loading tags done." );
1771     p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1772 }
1773
1774 /*****************************************************************************
1775  * ParseInfo:
1776  *****************************************************************************/
1777 static void ParseSeekHead( demux_t *p_demux, EbmlElement *seekhead )
1778 {
1779     demux_sys_t *p_sys = p_demux->p_sys;
1780     EbmlElement *el;
1781     EbmlMaster  *m;
1782     unsigned int i;
1783     int i_upper_level = 0;
1784
1785     msg_Dbg( p_demux, "|   + Seek head" );
1786
1787     /* Master elements */
1788     m = static_cast<EbmlMaster *>(seekhead);
1789     m->Read( *p_sys->es, seekhead->Generic().Context, i_upper_level, el, true );
1790
1791     for( i = 0; i < m->ListSize(); i++ )
1792     {
1793         EbmlElement *l = (*m)[i];
1794
1795         if( MKV_IS_ID( l, KaxSeek ) )
1796         {
1797             EbmlMaster *sk = static_cast<EbmlMaster *>(l);
1798             EbmlId id = EbmlVoid::ClassInfos.GlobalId;
1799             int64_t i_pos = -1;
1800
1801             unsigned int j;
1802
1803             for( j = 0; j < sk->ListSize(); j++ )
1804             {
1805                 EbmlElement *l = (*sk)[j];
1806
1807                 if( MKV_IS_ID( l, KaxSeekID ) )
1808                 {
1809                     KaxSeekID &sid = *(KaxSeekID*)l;
1810                     id = EbmlId( sid.GetBuffer(), sid.GetSize() );
1811                 }
1812                 else if( MKV_IS_ID( l, KaxSeekPosition ) )
1813                 {
1814                     KaxSeekPosition &spos = *(KaxSeekPosition*)l;
1815                     i_pos = uint64( spos );
1816                 }
1817                 else
1818                 {
1819                     msg_Dbg( p_demux, "|   |   |   + Unknown (%s)", typeid(*l).name() );
1820                 }
1821             }
1822
1823             if( i_pos >= 0 )
1824             {
1825                 if( id == KaxCues::ClassInfos.GlobalId )
1826                 {
1827                     msg_Dbg( p_demux, "|   |   |   = cues at "I64Fd, i_pos );
1828                     p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
1829                 }
1830                 else if( id == KaxChapters::ClassInfos.GlobalId )
1831                 {
1832                     msg_Dbg( p_demux, "|   |   |   = chapters at "I64Fd, i_pos );
1833                     p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
1834                 }
1835                 else if( id == KaxTags::ClassInfos.GlobalId )
1836                 {
1837                     msg_Dbg( p_demux, "|   |   |   = tags at "I64Fd, i_pos );
1838                     p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
1839                 }
1840             }
1841         }
1842         else
1843         {
1844             msg_Dbg( p_demux, "|   |   + Unknown (%s)", typeid(*l).name() );
1845         }
1846     }
1847 }
1848
1849 /*****************************************************************************
1850  * ParseTracks:
1851  *****************************************************************************/
1852 static void ParseTrackEntry( demux_t *p_demux, EbmlMaster *m )
1853 {
1854     demux_sys_t *p_sys = p_demux->p_sys;
1855     unsigned int i;
1856
1857     mkv_track_t *tk;
1858
1859     msg_Dbg( p_demux, "|   |   + Track Entry" );
1860
1861     p_sys->i_track++;
1862     p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
1863
1864     /* Init the track */
1865     tk = &p_sys->track[p_sys->i_track - 1];
1866
1867     memset( tk, 0, sizeof( mkv_track_t ) );
1868
1869     es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
1870     tk->fmt.psz_language = strdup("English");
1871     tk->fmt.psz_description = NULL;
1872
1873     tk->b_default = VLC_TRUE;
1874     tk->b_enabled = VLC_TRUE;
1875     tk->i_number = p_sys->i_track - 1;
1876     tk->i_extra_data = 0;
1877     tk->p_extra_data = NULL;
1878     tk->psz_codec = NULL;
1879     tk->i_default_duration = 0;
1880     tk->f_timecodescale = 1.0;
1881
1882     tk->b_inited = VLC_FALSE;
1883     tk->i_data_init = 0;
1884     tk->p_data_init = NULL;
1885
1886     tk->psz_codec_name = NULL;
1887     tk->psz_codec_settings = NULL;
1888     tk->psz_codec_info_url = NULL;
1889     tk->psz_codec_download_url = NULL;
1890     
1891     tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
1892
1893     for( i = 0; i < m->ListSize(); i++ )
1894     {
1895         EbmlElement *l = (*m)[i];
1896
1897         if( MKV_IS_ID( l, KaxTrackNumber ) )
1898         {
1899             KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
1900
1901             tk->i_number = uint32( tnum );
1902             msg_Dbg( p_demux, "|   |   |   + Track Number=%u", uint32( tnum ) );
1903         }
1904         else  if( MKV_IS_ID( l, KaxTrackUID ) )
1905         {
1906             KaxTrackUID &tuid = *(KaxTrackUID*)l;
1907
1908             msg_Dbg( p_demux, "|   |   |   + Track UID=%u",  uint32( tuid ) );
1909         }
1910         else  if( MKV_IS_ID( l, KaxTrackType ) )
1911         {
1912             char *psz_type;
1913             KaxTrackType &ttype = *(KaxTrackType*)l;
1914
1915             switch( uint8(ttype) )
1916             {
1917                 case track_audio:
1918                     psz_type = "audio";
1919                     tk->fmt.i_cat = AUDIO_ES;
1920                     break;
1921                 case track_video:
1922                     psz_type = "video";
1923                     tk->fmt.i_cat = VIDEO_ES;
1924                     break;
1925                 case track_subtitle:
1926                     psz_type = "subtitle";
1927                     tk->fmt.i_cat = SPU_ES;
1928                     break;
1929                 default:
1930                     psz_type = "unknown";
1931                     tk->fmt.i_cat = UNKNOWN_ES;
1932                     break;
1933             }
1934
1935             msg_Dbg( p_demux, "|   |   |   + Track Type=%s", psz_type );
1936         }
1937 //        else  if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
1938 //        {
1939 //            KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
1940
1941 //            tk->b_enabled = uint32( fenb );
1942 //            msg_Dbg( p_demux, "|   |   |   + Track Enabled=%u",
1943 //                     uint32( fenb )  );
1944 //        }
1945         else  if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
1946         {
1947             KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
1948
1949             tk->b_default = uint32( fdef );
1950             msg_Dbg( p_demux, "|   |   |   + Track Default=%u", uint32( fdef )  );
1951         }
1952         else  if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
1953         {
1954             KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
1955
1956             msg_Dbg( p_demux, "|   |   |   + Track Lacing=%d", uint32( lac ) );
1957         }
1958         else  if( MKV_IS_ID( l, KaxTrackMinCache ) )
1959         {
1960             KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
1961
1962             msg_Dbg( p_demux, "|   |   |   + Track MinCache=%d", uint32( cmin ) );
1963         }
1964         else  if( MKV_IS_ID( l, KaxTrackMaxCache ) )
1965         {
1966             KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
1967
1968             msg_Dbg( p_demux, "|   |   |   + Track MaxCache=%d", uint32( cmax ) );
1969         }
1970         else  if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
1971         {
1972             KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
1973
1974             tk->i_default_duration = uint64(defd);
1975             msg_Dbg( p_demux, "|   |   |   + Track Default Duration="I64Fd, uint64(defd) );
1976         }
1977         else  if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
1978         {
1979             KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
1980
1981             tk->f_timecodescale = float( ttcs );
1982             msg_Dbg( p_demux, "|   |   |   + Track TimeCodeScale=%f", tk->f_timecodescale );
1983         }
1984         else if( MKV_IS_ID( l, KaxTrackName ) )
1985         {
1986             KaxTrackName &tname = *(KaxTrackName*)l;
1987
1988             tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
1989             msg_Dbg( p_demux, "|   |   |   + Track Name=%s", tk->fmt.psz_description );
1990         }
1991         else  if( MKV_IS_ID( l, KaxTrackLanguage ) )
1992         {
1993             KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
1994
1995             tk->fmt.psz_language = strdup( string( lang ).c_str() );
1996             msg_Dbg( p_demux,
1997                      "|   |   |   + Track Language=`%s'", tk->fmt.psz_language );
1998         }
1999         else  if( MKV_IS_ID( l, KaxCodecID ) )
2000         {
2001             KaxCodecID &codecid = *(KaxCodecID*)l;
2002
2003             tk->psz_codec = strdup( string( codecid ).c_str() );
2004             msg_Dbg( p_demux, "|   |   |   + Track CodecId=%s", string( codecid ).c_str() );
2005         }
2006         else  if( MKV_IS_ID( l, KaxCodecPrivate ) )
2007         {
2008             KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
2009
2010             tk->i_extra_data = cpriv.GetSize();
2011             if( tk->i_extra_data > 0 )
2012             {
2013                 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
2014                 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
2015             }
2016             msg_Dbg( p_demux, "|   |   |   + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
2017         }
2018         else if( MKV_IS_ID( l, KaxCodecName ) )
2019         {
2020             KaxCodecName &cname = *(KaxCodecName*)l;
2021
2022             tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
2023             msg_Dbg( p_demux, "|   |   |   + Track Codec Name=%s", tk->psz_codec_name );
2024         }
2025         else if( MKV_IS_ID( l, KaxContentEncodings ) )
2026         {
2027             EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
2028             MkvTree( p_demux, 3, "Content Encodings" );
2029             for( unsigned int i = 0; i < cencs->ListSize(); i++ )
2030             {
2031                 EbmlElement *l2 = (*cencs)[i];
2032                 if( MKV_IS_ID( l2, KaxContentEncoding ) )
2033                 {
2034                     MkvTree( p_demux, 4, "Content Encoding" );
2035                     EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
2036                     for( unsigned int i = 0; i < cenc->ListSize(); i++ )
2037                     {
2038                         EbmlElement *l3 = (*cenc)[i];
2039                         if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
2040                         {
2041                             KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
2042                             MkvTree( p_demux, 5, "Order: %i", uint32( encord ) );
2043                         }
2044                         else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
2045                         {
2046                             KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
2047                             MkvTree( p_demux, 5, "Scope: %i", uint32( encscope ) );
2048                         }
2049                         else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
2050                         {
2051                             KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
2052                             MkvTree( p_demux, 5, "Type: %i", uint32( enctype ) );
2053                         }
2054                         else if( MKV_IS_ID( l3, KaxContentCompression ) )
2055                         {
2056                             EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
2057                             MkvTree( p_demux, 5, "Content Compression" );
2058                             for( unsigned int i = 0; i < compr->ListSize(); i++ )
2059                             {
2060                                 EbmlElement *l4 = (*compr)[i];
2061                                 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
2062                                 {
2063                                     KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
2064                                     MkvTree( p_demux, 6, "Compression Algorithm: %i", uint32(compalg) );
2065                                     if( uint32( compalg ) == 0 )
2066                                     {
2067                                         tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
2068                                     }
2069                                 }
2070                                 else
2071                                 {
2072                                     MkvTree( p_demux, 6, "Unknown (%s)", typeid(*l4).name() );
2073                                 }
2074                             }
2075                         }
2076
2077                         else
2078                         {
2079                             MkvTree( p_demux, 5, "Unknown (%s)", typeid(*l3).name() );
2080                         }
2081                     }
2082                     
2083                 }
2084                 else
2085                 {
2086                     MkvTree( p_demux, 4, "Unknown (%s)", typeid(*l2).name() );
2087                 }
2088             }
2089                 
2090         }
2091 //        else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
2092 //        {
2093 //            KaxCodecSettings &cset = *(KaxCodecSettings*)l;
2094
2095 //            tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
2096 //            msg_Dbg( p_demux, "|   |   |   + Track Codec Settings=%s", tk->psz_codec_settings );
2097 //        }
2098 //        else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
2099 //        {
2100 //            KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
2101
2102 //            tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
2103 //            msg_Dbg( p_demux, "|   |   |   + Track Codec Info URL=%s", tk->psz_codec_info_url );
2104 //        }
2105 //        else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
2106 //        {
2107 //            KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
2108
2109 //            tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
2110 //            msg_Dbg( p_demux, "|   |   |   + Track Codec Info URL=%s", tk->psz_codec_download_url );
2111 //        }
2112 //        else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
2113 //        {
2114 //            KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
2115
2116 //            msg_Dbg( p_demux, "|   |   |   + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
2117 //        }
2118 //        else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
2119 //        {
2120 //            KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
2121
2122 //            msg_Dbg( p_demux, "|   |   |   + Track Overlay=%u <== UNUSED", uint32( tovr ) );
2123 //        }
2124         else  if( MKV_IS_ID( l, KaxTrackVideo ) )
2125         {
2126             EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
2127             unsigned int j;
2128
2129             msg_Dbg( p_demux, "|   |   |   + Track Video" );
2130             tk->f_fps = 0.0;
2131
2132             for( j = 0; j < tkv->ListSize(); j++ )
2133             {
2134                 EbmlElement *l = (*tkv)[j];
2135 //                if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
2136 //                {
2137 //                    KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
2138
2139 //                    msg_Dbg( p_demux, "|   |   |   |   + Track Video Interlaced=%u", uint8( fint ) );
2140 //                }
2141 //                else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
2142 //                {
2143 //                    KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
2144
2145 //                    msg_Dbg( p_demux, "|   |   |   |   + Track Video Stereo Mode=%u", uint8( stereo ) );
2146 //                }
2147 //                else
2148                 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
2149                 {
2150                     KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
2151
2152                     tk->fmt.video.i_width = uint16( vwidth );
2153                     msg_Dbg( p_demux, "|   |   |   |   + width=%d", uint16( vwidth ) );
2154                 }
2155                 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
2156                 {
2157                     KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
2158
2159                     tk->fmt.video.i_height = uint16( vheight );
2160                     msg_Dbg( p_demux, "|   |   |   |   + height=%d", uint16( vheight ) );
2161                 }
2162                 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
2163                 {
2164                     KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
2165
2166                     tk->fmt.video.i_visible_width = uint16( vwidth );
2167                     msg_Dbg( p_demux, "|   |   |   |   + display width=%d", uint16( vwidth ) );
2168                 }
2169                 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
2170                 {
2171                     KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
2172
2173                     tk->fmt.video.i_visible_height = uint16( vheight );
2174                     msg_Dbg( p_demux, "|   |   |   |   + display height=%d", uint16( vheight ) );
2175                 }
2176                 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
2177                 {
2178                     KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
2179
2180                     tk->f_fps = float( vfps );
2181                     msg_Dbg( p_demux, "   |   |   |   + fps=%f", float( vfps ) );
2182                 }
2183 //                else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
2184 //                {
2185 //                     KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
2186
2187 //                    msg_Dbg( p_demux, "|   |   |   |   + Track Video Display Unit=%s",
2188 //                             uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
2189 //                }
2190 //                else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
2191 //                {
2192 //                    KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
2193
2194 //                    msg_Dbg( p_demux, "   |   |   |   + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
2195 //                }
2196 //                else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
2197 //                {
2198 //                    KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
2199
2200 //                    msg_Dbg( p_demux, "   |   |   |   + fps=%f", float( gamma ) );
2201 //                }
2202                 else
2203                 {
2204                     msg_Dbg( p_demux, "|   |   |   |   + Unknown (%s)", typeid(*l).name() );
2205                 }
2206             }
2207         }
2208         else  if( MKV_IS_ID( l, KaxTrackAudio ) )
2209         {
2210             EbmlMaster *tka = static_cast<EbmlMaster*>(l);
2211             unsigned int j;
2212
2213             msg_Dbg( p_demux, "|   |   |   + Track Audio" );
2214
2215             for( j = 0; j < tka->ListSize(); j++ )
2216             {
2217                 EbmlElement *l = (*tka)[j];
2218
2219                 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
2220                 {
2221                     KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
2222
2223                     tk->fmt.audio.i_rate = (int)float( afreq );
2224                     msg_Dbg( p_demux, "|   |   |   |   + afreq=%d", tk->fmt.audio.i_rate );
2225                 }
2226                 else if( MKV_IS_ID( l, KaxAudioChannels ) )
2227                 {
2228                     KaxAudioChannels &achan = *(KaxAudioChannels*)l;
2229
2230                     tk->fmt.audio.i_channels = uint8( achan );
2231                     msg_Dbg( p_demux, "|   |   |   |   + achan=%u", uint8( achan ) );
2232                 }
2233                 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
2234                 {
2235                     KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
2236
2237                     tk->fmt.audio.i_bitspersample = uint8( abits );
2238                     msg_Dbg( p_demux, "|   |   |   |   + abits=%u", uint8( abits ) );
2239                 }
2240                 else
2241                 {
2242                     msg_Dbg( p_demux, "|   |   |   |   + Unknown (%s)", typeid(*l).name() );
2243                 }
2244             }
2245         }
2246         else
2247         {
2248             msg_Dbg( p_demux, "|   |   |   + Unknown (%s)",
2249                      typeid(*l).name() );
2250         }
2251     }
2252 }
2253
2254 static void ParseTracks( demux_t *p_demux, EbmlElement *tracks )
2255 {
2256     demux_sys_t *p_sys = p_demux->p_sys;
2257     EbmlElement *el;
2258     EbmlMaster  *m;
2259     unsigned int i;
2260     int i_upper_level = 0;
2261
2262     msg_Dbg( p_demux, "|   + Tracks" );
2263
2264     /* Master elements */
2265     m = static_cast<EbmlMaster *>(tracks);
2266     m->Read( *p_sys->es, tracks->Generic().Context, i_upper_level, el, true );
2267
2268     for( i = 0; i < m->ListSize(); i++ )
2269     {
2270         EbmlElement *l = (*m)[i];
2271
2272         if( MKV_IS_ID( l, KaxTrackEntry ) )
2273         {
2274             ParseTrackEntry( p_demux, static_cast<EbmlMaster *>(l) );
2275         }
2276         else
2277         {
2278             msg_Dbg( p_demux, "|   |   + Unknown (%s)", typeid(*l).name() );
2279         }
2280     }
2281 }
2282
2283 /*****************************************************************************
2284  * ParseInfo:
2285  *****************************************************************************/
2286 static void ParseInfo( demux_t *p_demux, EbmlElement *info )
2287 {
2288     demux_sys_t *p_sys = p_demux->p_sys;
2289     EbmlElement *el;
2290     EbmlMaster  *m;
2291     unsigned int i;
2292     int i_upper_level = 0;
2293
2294     msg_Dbg( p_demux, "|   + Information" );
2295
2296     /* Master elements */
2297     m = static_cast<EbmlMaster *>(info);
2298     m->Read( *p_sys->es, info->Generic().Context, i_upper_level, el, true );
2299
2300     for( i = 0; i < m->ListSize(); i++ )
2301     {
2302         EbmlElement *l = (*m)[i];
2303
2304         if( MKV_IS_ID( l, KaxSegmentUID ) )
2305         {
2306             KaxSegmentUID &uid = *(KaxSegmentUID*)l;
2307
2308             msg_Dbg( p_demux, "|   |   + UID=%d", uint32(uid) );
2309         }
2310         else if( MKV_IS_ID( l, KaxTimecodeScale ) )
2311         {
2312             KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
2313
2314             p_sys->i_timescale = uint64(tcs);
2315
2316             msg_Dbg( p_demux, "|   |   + TimecodeScale="I64Fd,
2317                      p_sys->i_timescale );
2318         }
2319         else if( MKV_IS_ID( l, KaxDuration ) )
2320         {
2321             KaxDuration &dur = *(KaxDuration*)l;
2322
2323             p_sys->f_duration = float(dur);
2324
2325             msg_Dbg( p_demux, "|   |   + Duration=%f",
2326                      p_sys->f_duration );
2327         }
2328         else if( MKV_IS_ID( l, KaxMuxingApp ) )
2329         {
2330             KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
2331
2332             p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
2333
2334             msg_Dbg( p_demux, "|   |   + Muxing Application=%s",
2335                      p_sys->psz_muxing_application );
2336         }
2337         else if( MKV_IS_ID( l, KaxWritingApp ) )
2338         {
2339             KaxWritingApp &wapp = *(KaxWritingApp*)l;
2340
2341             p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
2342
2343             msg_Dbg( p_demux, "|   |   + Writing Application=%s",
2344                      p_sys->psz_writing_application );
2345         }
2346         else if( MKV_IS_ID( l, KaxSegmentFilename ) )
2347         {
2348             KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
2349
2350             p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
2351
2352             msg_Dbg( p_demux, "|   |   + Segment Filename=%s",
2353                      p_sys->psz_segment_filename );
2354         }
2355         else if( MKV_IS_ID( l, KaxTitle ) )
2356         {
2357             KaxTitle &title = *(KaxTitle*)l;
2358
2359             p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
2360
2361             msg_Dbg( p_demux, "|   |   + Title=%s", p_sys->psz_title );
2362         }
2363 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
2364         else if( MKV_IS_ID( l, KaxDateUTC ) )
2365         {
2366             KaxDateUTC &date = *(KaxDateUTC*)l;
2367             time_t i_date;
2368             struct tm tmres;
2369             char   buffer[256];
2370
2371             i_date = date.GetEpochDate();
2372             memset( buffer, 0, 256 );
2373             if( gmtime_r( &i_date, &tmres ) &&
2374                 asctime_r( &tmres, buffer ) )
2375             {
2376                 buffer[strlen( buffer)-1]= '\0';
2377                 p_sys->psz_date_utc = strdup( buffer );
2378                 msg_Dbg( p_demux, "|   |   + Date=%s", p_sys->psz_date_utc );
2379             }
2380         }
2381 #endif
2382         else
2383         {
2384             msg_Dbg( p_demux, "|   |   + Unknown (%s)", typeid(*l).name() );
2385         }
2386     }
2387
2388     p_sys->f_duration = p_sys->f_duration * p_sys->i_timescale / 1000000.0;
2389 }
2390
2391
2392 /*****************************************************************************
2393  * ParseChapterAtom
2394  *****************************************************************************/
2395 static void ParseChapterAtom( demux_t *p_demux, int i_level, EbmlMaster *ca )
2396 {
2397     demux_sys_t *p_sys = p_demux->p_sys;
2398     unsigned int i;
2399     seekpoint_t *sk;
2400
2401     if( p_sys->title == NULL )
2402     {
2403         p_sys->title = vlc_input_title_New();
2404     }
2405     sk = vlc_seekpoint_New();
2406
2407     msg_Dbg( p_demux, "|   |   |   + ChapterAtom (level=%d)", i_level );
2408     for( i = 0; i < ca->ListSize(); i++ )
2409     {
2410         EbmlElement *l = (*ca)[i];
2411
2412         if( MKV_IS_ID( l, KaxChapterUID ) )
2413         {
2414             KaxChapterUID &uid = *(KaxChapterUID*)l;
2415             uint32_t i_uid = uint32( uid );
2416             msg_Dbg( p_demux, "|   |   |   |   + ChapterUID: 0x%x", i_uid );
2417         }
2418         else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
2419         {
2420             KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
2421             sk->i_time_offset = uint64( start ) / I64C(1000);
2422
2423             msg_Dbg( p_demux, "|   |   |   |   + ChapterTimeStart: %lld", sk->i_time_offset );
2424         }
2425         else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
2426         {
2427             KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
2428             int64_t i_end = uint64( end );
2429
2430             msg_Dbg( p_demux, "|   |   |   |   + ChapterTimeEnd: %lld", i_end );
2431         }
2432         else if( MKV_IS_ID( l, KaxChapterDisplay ) )
2433         {
2434             EbmlMaster *cd = static_cast<EbmlMaster *>(l);
2435             unsigned int j;
2436
2437             msg_Dbg( p_demux, "|   |   |   |   + ChapterDisplay" );
2438             for( j = 0; j < cd->ListSize(); j++ )
2439             {
2440                 EbmlElement *l= (*cd)[j];
2441
2442                 if( MKV_IS_ID( l, KaxChapterString ) )
2443                 {
2444                     KaxChapterString &name =*(KaxChapterString*)l;
2445                     char *psz = UTF8ToStr( UTFstring( name ) );
2446                     sk->psz_name = strdup( psz );
2447                     msg_Dbg( p_demux, "|   |   |   |   |    + ChapterString '%s'", psz );
2448                 }
2449                 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
2450                 {
2451                     KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
2452                     const char *psz = string( lang ).c_str();
2453
2454                     msg_Dbg( p_demux, "|   |   |   |   |    + ChapterLanguage '%s'", psz );
2455                 }
2456                 else if( MKV_IS_ID( l, KaxChapterCountry ) )
2457                 {
2458                     KaxChapterCountry &ct =*(KaxChapterCountry*)l;
2459                     const char *psz = string( ct ).c_str();
2460
2461                     msg_Dbg( p_demux, "|   |   |   |   |    + ChapterCountry '%s'", psz );
2462                 }
2463             }
2464         }
2465         else if( MKV_IS_ID( l, KaxChapterAtom ) )
2466         {
2467             ParseChapterAtom( p_demux, i_level+1, static_cast<EbmlMaster *>(l) );
2468         }
2469     }
2470     // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
2471     p_sys->title->i_seekpoint++;
2472     p_sys->title->seekpoint = (seekpoint_t**)realloc( p_sys->title->seekpoint, p_sys->title->i_seekpoint * sizeof( seekpoint_t* ) );
2473     p_sys->title->seekpoint[p_sys->title->i_seekpoint-1] = sk;
2474 }
2475
2476 /*****************************************************************************
2477  * ParseChapters:
2478  *****************************************************************************/
2479 static void ParseChapters( demux_t *p_demux, EbmlElement *chapters )
2480 {
2481     demux_sys_t *p_sys = p_demux->p_sys;
2482     EbmlElement *el;
2483     EbmlMaster  *m;
2484     unsigned int i;
2485     int i_upper_level = 0;
2486
2487
2488     /* Master elements */
2489     m = static_cast<EbmlMaster *>(chapters);
2490     m->Read( *p_sys->es, chapters->Generic().Context, i_upper_level, el, true );
2491
2492     for( i = 0; i < m->ListSize(); i++ )
2493     {
2494         EbmlElement *l = (*m)[i];
2495
2496         if( MKV_IS_ID( l, KaxEditionEntry ) )
2497         {
2498             EbmlMaster *E = static_cast<EbmlMaster *>(l );
2499             unsigned int j;
2500             msg_Dbg( p_demux, "|   |   + EditionEntry" );
2501             for( j = 0; j < E->ListSize(); j++ )
2502             {
2503                 EbmlElement *l = (*E)[j];
2504
2505                 if( MKV_IS_ID( l, KaxChapterAtom ) )
2506                 {
2507                     ParseChapterAtom( p_demux, 0, static_cast<EbmlMaster *>(l) );
2508                 }
2509                 else
2510                 {
2511                     msg_Dbg( p_demux, "|   |   |   + Unknown (%s)", typeid(*l).name() );
2512                 }
2513             }
2514         }
2515         else
2516         {
2517             msg_Dbg( p_demux, "|   |   + Unknown (%s)", typeid(*l).name() );
2518         }
2519     }
2520 }
2521
2522 /*****************************************************************************
2523  * InformationCreate:
2524  *****************************************************************************/
2525 static void InformationCreate( demux_t *p_demux )
2526 {
2527     demux_sys_t *p_sys = p_demux->p_sys;
2528     int         i_track;
2529
2530     p_sys->meta = vlc_meta_New();
2531
2532     if( p_sys->psz_title )
2533     {
2534         vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_sys->psz_title );
2535     }
2536     if( p_sys->psz_date_utc )
2537     {
2538         vlc_meta_Add( p_sys->meta, VLC_META_DATE, p_sys->psz_date_utc );
2539     }
2540     if( p_sys->psz_segment_filename )
2541     {
2542         vlc_meta_Add( p_sys->meta, _("Segment filename"), p_sys->psz_segment_filename );
2543     }
2544     if( p_sys->psz_muxing_application )
2545     {
2546         vlc_meta_Add( p_sys->meta, _("Muxing application"), p_sys->psz_muxing_application );
2547     }
2548     if( p_sys->psz_writing_application )
2549     {
2550         vlc_meta_Add( p_sys->meta, _("Writing application"), p_sys->psz_writing_application );
2551     }
2552
2553     for( i_track = 0; i_track < p_sys->i_track; i_track++ )
2554     {
2555         mkv_track_t *tk = &p_sys->track[i_track];
2556         vlc_meta_t *mtk = vlc_meta_New();
2557
2558         p_sys->meta->track = (vlc_meta_t**)realloc( p_sys->meta->track,
2559                                                     sizeof( vlc_meta_t * ) * ( p_sys->meta->i_track + 1 ) );
2560         p_sys->meta->track[p_sys->meta->i_track++] = mtk;
2561
2562         if( tk->fmt.psz_description )
2563         {
2564             vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
2565         }
2566         if( tk->psz_codec_name )
2567         {
2568             vlc_meta_Add( p_sys->meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
2569         }
2570         if( tk->psz_codec_settings )
2571         {
2572             vlc_meta_Add( p_sys->meta, VLC_META_SETTING, tk->psz_codec_settings );
2573         }
2574         if( tk->psz_codec_info_url )
2575         {
2576             vlc_meta_Add( p_sys->meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
2577         }
2578         if( tk->psz_codec_download_url )
2579         {
2580             vlc_meta_Add( p_sys->meta, VLC_META_URL, tk->psz_codec_download_url );
2581         }
2582     }
2583
2584     if( p_sys->i_tags_position >= 0 )
2585     {
2586         vlc_bool_t b_seekable;
2587
2588         stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
2589         if( b_seekable )
2590         {
2591             LoadTags( p_demux );
2592         }
2593     }
2594 }
2595
2596
2597 /*****************************************************************************
2598  * Divers
2599  *****************************************************************************/
2600
2601 static void IndexAppendCluster( demux_t *p_demux, KaxCluster *cluster )
2602 {
2603     demux_sys_t *p_sys = p_demux->p_sys;
2604
2605 #define idx p_sys->index[p_sys->i_index]
2606     idx.i_track       = -1;
2607     idx.i_block_number= -1;
2608     idx.i_position    = cluster->GetElementPosition();
2609     idx.i_time        = -1;
2610     idx.b_key         = VLC_TRUE;
2611
2612     p_sys->i_index++;
2613     if( p_sys->i_index >= p_sys->i_index_max )
2614     {
2615         p_sys->i_index_max += 1024;
2616         p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2617     }
2618 #undef idx
2619 }
2620
2621 static char * UTF8ToStr( const UTFstring &u )
2622 {
2623     int     i_src;
2624     const wchar_t *src;
2625     char *dst, *p;
2626
2627     i_src = u.length();
2628     src   = u.c_str();
2629
2630     p = dst = (char*)malloc( i_src + 1);
2631     while( i_src > 0 )
2632     {
2633         if( *src < 255 )
2634         {
2635             *p++ = (char)*src;
2636         }
2637         else
2638         {
2639             *p++ = '?';
2640         }
2641         src++;
2642         i_src--;
2643     }
2644     *p++= '\0';
2645
2646     return dst;
2647 }
2648