]> git.sesse.net Git - vlc/blob - modules/demux/mkv/matroska_segment.cpp
macosx: fixed menubar appearance in fullscreen mode by partially reverting [46c93c9cc...
[vlc] / modules / demux / mkv / matroska_segment.cpp
1 /*****************************************************************************
2  * mkv.cpp : matroska demuxer
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Steve Lhomme <steve.lhomme@free.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "matroska_segment.hpp"
26
27 #include "chapters.hpp"
28
29 #include "demux.hpp"
30
31 /* GetFourCC helper */
32 #define GetFOURCC( p )  __GetFOURCC( (uint8_t*)p )
33 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
34 {
35     return VLC_FOURCC( p[0], p[1], p[2], p[3] );
36 }
37
38 /* Destructor */
39 matroska_segment_c::~matroska_segment_c()
40 {
41     for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
42     {
43         delete tracks[i_track]->p_compression_data;
44         es_format_Clean( &tracks[i_track]->fmt );
45         free( tracks[i_track]->p_extra_data );
46         free( tracks[i_track]->psz_codec );
47         delete tracks[i_track];
48     }
49
50     free( psz_writing_application );
51     free( psz_muxing_application );
52     free( psz_segment_filename );
53     free( psz_title );
54     free( psz_date_utc );
55     free( p_indexes );
56
57     delete ep;
58     delete segment;
59     delete p_segment_uid;
60     delete p_prev_segment_uid;
61     delete p_next_segment_uid;
62
63     std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
64     while ( index != stored_editions.end() )
65     {
66         delete (*index);
67         index++;
68     }
69     std::vector<chapter_translation_c*>::iterator indext = translations.begin();
70     while ( indext != translations.end() )
71     {
72         delete (*indext);
73         indext++;
74     }
75     std::vector<KaxSegmentFamily*>::iterator indexf = families.begin();
76     while ( indexf != families.end() )
77     {
78         delete (*indexf);
79         indexf++;
80    }
81 }
82
83
84 /*****************************************************************************
85  * Tools
86  *  * LoadCues : load the cues element and update index
87  *
88  *  * LoadTags : load ... the tags element
89  *
90  *  * InformationCreate : create all information, load tags if present
91  *
92  *****************************************************************************/
93 void matroska_segment_c::LoadCues( KaxCues *cues )
94 {
95     EbmlParser  *ep;
96     EbmlElement *el;
97
98     if( b_cues )
99     {
100         msg_Err( &sys.demuxer, "There can be only 1 Cues per section." );
101         return;
102     }
103
104     ep = new EbmlParser( &es, cues, &sys.demuxer );
105     while( ( el = ep->Get() ) != NULL )
106     {
107         if( MKV_IS_ID( el, KaxCuePoint ) )
108         {
109 #define idx p_indexes[i_index]
110
111             idx.i_track       = -1;
112             idx.i_block_number= -1;
113             idx.i_position    = -1;
114             idx.i_time        = 0;
115             idx.b_key         = true;
116
117             ep->Down();
118             while( ( el = ep->Get() ) != NULL )
119             {
120                 if( MKV_IS_ID( el, KaxCueTime ) )
121                 {
122                     KaxCueTime &ctime = *(KaxCueTime*)el;
123
124                     ctime.ReadData( es.I_O() );
125
126                     idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
127                 }
128                 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
129                 {
130                     ep->Down();
131                     while( ( el = ep->Get() ) != NULL )
132                     {
133                         if( MKV_IS_ID( el, KaxCueTrack ) )
134                         {
135                             KaxCueTrack &ctrack = *(KaxCueTrack*)el;
136
137                             ctrack.ReadData( es.I_O() );
138                             idx.i_track = uint16( ctrack );
139                         }
140                         else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
141                         {
142                             KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
143
144                             ccpos.ReadData( es.I_O() );
145                             idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
146                         }
147                         else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
148                         {
149                             KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
150
151                             cbnum.ReadData( es.I_O() );
152                             idx.i_block_number = uint32( cbnum );
153                         }
154                         else
155                         {
156                             msg_Dbg( &sys.demuxer, "         * Unknown (%s)", typeid(*el).name() );
157                         }
158                     }
159                     ep->Up();
160                 }
161                 else
162                 {
163                     msg_Dbg( &sys.demuxer, "     * Unknown (%s)", typeid(*el).name() );
164                 }
165             }
166             ep->Up();
167
168 #if 0
169             msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
170                      " track=%d bnum=%d", idx.i_time, idx.i_position,
171                      idx.i_track, idx.i_block_number );
172 #endif
173
174             i_index++;
175             if( i_index >= i_index_max )
176             {
177                 i_index_max += 1024;
178                 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
179             }
180 #undef idx
181         }
182         else
183         {
184             msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
185         }
186     }
187     delete ep;
188     b_cues = true;
189     msg_Dbg( &sys.demuxer, "|   - loading cues done." );
190 }
191
192 void matroska_segment_c::LoadTags( KaxTags *tags )
193 {
194     EbmlParser  *ep;
195     EbmlElement *el;
196
197     /* Master elements */
198     ep = new EbmlParser( &es, tags, &sys.demuxer );
199
200     while( ( el = ep->Get() ) != NULL )
201     {
202         if( MKV_IS_ID( el, KaxTag ) )
203         {
204             msg_Dbg( &sys.demuxer, "+ Tag" );
205             ep->Down();
206             while( ( el = ep->Get() ) != NULL )
207             {
208                 if( MKV_IS_ID( el, KaxTagTargets ) )
209                 {
210                     msg_Dbg( &sys.demuxer, "|   + Targets" );
211                     ep->Down();
212                     while( ( el = ep->Get() ) != NULL )
213                     {
214                         msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid( *el ).name() );
215                     }
216                     ep->Up();
217                 }
218                 else if( MKV_IS_ID( el, KaxTagGeneral ) )
219                 {
220                     msg_Dbg( &sys.demuxer, "|   + General" );
221                     ep->Down();
222                     while( ( el = ep->Get() ) != NULL )
223                     {
224                         msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid( *el ).name() );
225                     }
226                     ep->Up();
227                 }
228                 else if( MKV_IS_ID( el, KaxTagGenres ) )
229                 {
230                     msg_Dbg( &sys.demuxer, "|   + Genres" );
231                     ep->Down();
232                     while( ( el = ep->Get() ) != NULL )
233                     {
234                         msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid( *el ).name() );
235                     }
236                     ep->Up();
237                 }
238                 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
239                 {
240                     msg_Dbg( &sys.demuxer, "|   + Audio Specific" );
241                     ep->Down();
242                     while( ( el = ep->Get() ) != NULL )
243                     {
244                         msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid( *el ).name() );
245                     }
246                     ep->Up();
247                 }
248                 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
249                 {
250                     msg_Dbg( &sys.demuxer, "|   + Images Specific" );
251                     ep->Down();
252                     while( ( el = ep->Get() ) != NULL )
253                     {
254                         msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid( *el ).name() );
255                     }
256                     ep->Up();
257                 }
258                 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
259                 {
260                     msg_Dbg( &sys.demuxer, "|   + Multi Comment" );
261                 }
262                 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
263                 {
264                     msg_Dbg( &sys.demuxer, "|   + Multi Commercial" );
265                 }
266                 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
267                 {
268                     msg_Dbg( &sys.demuxer, "|   + Multi Date" );
269                 }
270                 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
271                 {
272                     msg_Dbg( &sys.demuxer, "|   + Multi Entity" );
273                 }
274                 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
275                 {
276                     msg_Dbg( &sys.demuxer, "|   + Multi Identifier" );
277                 }
278                 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
279                 {
280                     msg_Dbg( &sys.demuxer, "|   + Multi Legal" );
281                 }
282                 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
283                 {
284                     msg_Dbg( &sys.demuxer, "|   + Multi Title" );
285                 }
286                 else
287                 {
288                     msg_Dbg( &sys.demuxer, "|   + LoadTag Unknown (%s)", typeid( *el ).name() );
289                 }
290             }
291             ep->Up();
292         }
293         else
294         {
295             msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
296         }
297     }
298     delete ep;
299
300     msg_Dbg( &sys.demuxer, "loading tags done." );
301 }
302
303 /*****************************************************************************
304  * InformationCreate:
305  *****************************************************************************/
306 void matroska_segment_c::InformationCreate( )
307 {
308     sys.meta = vlc_meta_New();
309
310     if( psz_title )
311     {
312         vlc_meta_SetTitle( sys.meta, psz_title );
313     }
314     if( psz_date_utc )
315     {
316         vlc_meta_SetDate( sys.meta, psz_date_utc );
317     }
318 #if 0
319     if( psz_segment_filename )
320     {
321         fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
322     }
323     if( psz_muxing_application )
324     {
325         fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
326     }
327     if( psz_writing_application )
328     {
329         fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
330     }
331
332     for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
333     {
334 //        mkv_track_t *tk = tracks[i_track];
335 //        vlc_meta_t *mtk = vlc_meta_New();
336         fprintf( stderr, "***** WARNING: Unhandled child meta\n");
337     }
338 #endif
339 #if 0
340     if( i_tags_position >= 0 )
341     {
342         bool b_seekable;
343
344         stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
345         if( b_seekable )
346         {
347             LoadTags( );
348         }
349     }
350 #endif
351 }
352
353
354 /*****************************************************************************
355  * Misc
356  *****************************************************************************/
357
358 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
359 {
360 #define idx p_indexes[i_index]
361     idx.i_track       = -1;
362     idx.i_block_number= -1;
363     idx.i_position    = cluster->GetElementPosition();
364     idx.i_time        = -1;
365     idx.b_key         = true;
366
367     i_index++;
368     if( i_index >= i_index_max )
369     {
370         i_index_max += 1024;
371         p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
372     }
373 #undef idx
374 }
375
376
377 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
378 {
379     if ( b_preloaded )
380         return false;
381
382     for (size_t i=0; i<families.size(); i++)
383     {
384         for (size_t j=0; j<of_segment.families.size(); j++)
385         {
386             if ( *(families[i]) == *(of_segment.families[j]) )
387                 return Preload( );
388         }
389     }
390
391     return false;
392 }
393
394 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
395 {
396     EbmlBinary *p_tmp;
397
398     if ( p_item_a == NULL || p_item_b == NULL )
399         return false;
400
401     p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
402     if ( p_item_b->p_prev_segment_uid != NULL
403           && *p_tmp == *p_item_b->p_prev_segment_uid )
404         return true;
405
406     p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
407     if ( !p_tmp )
408         return false;
409  
410     if ( p_item_b->p_segment_uid != NULL
411           && *p_tmp == *p_item_b->p_segment_uid )
412         return true;
413
414     if ( p_item_b->p_prev_segment_uid != NULL
415           && *p_tmp == *p_item_b->p_prev_segment_uid )
416         return true;
417
418     return false;
419 }
420
421 bool matroska_segment_c::Preload( )
422 {
423     if ( b_preloaded )
424         return false;
425
426     EbmlElement *el = NULL;
427
428     ep->Reset( &sys.demuxer );
429
430     while( ( el = ep->Get() ) != NULL )
431     {
432         if( MKV_IS_ID( el, KaxSeekHead ) )
433         {
434             /* Multiple allowed */
435             /* We bail at 10, to prevent possible recursion */
436             msg_Dbg(  &sys.demuxer, "|   + Seek head" );
437             if( i_seekhead_count < 10 )
438             {
439                 i_seekhead_position = (int64_t) es.I_O().getFilePointer();
440                 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
441             }
442         }
443         else if( MKV_IS_ID( el, KaxInfo ) )
444         {
445             /* Multiple allowed, mandatory */
446             msg_Dbg(  &sys.demuxer, "|   + Information" );
447             if( i_info_position < 0 ) // FIXME
448                 ParseInfo( static_cast<KaxInfo*>( el ) );
449             i_info_position = (int64_t) es.I_O().getFilePointer();
450         }
451         else if( MKV_IS_ID( el, KaxTracks ) )
452         {
453             /* Multiple allowed */
454             msg_Dbg(  &sys.demuxer, "|   + Tracks" );
455             if( i_tracks_position < 0 ) // FIXME
456                 ParseTracks( static_cast<KaxTracks*>( el ) );
457             if ( tracks.size() == 0 )
458             {
459                 msg_Err( &sys.demuxer, "No tracks supported" );
460                 return false;
461             }
462             i_tracks_position = (int64_t) es.I_O().getFilePointer();
463         }
464         else if( MKV_IS_ID( el, KaxCues ) )
465         {
466             msg_Dbg(  &sys.demuxer, "|   + Cues" );
467             if( i_cues_position < 0 )
468                 LoadCues( static_cast<KaxCues*>( el ) );
469             i_cues_position = (int64_t) es.I_O().getFilePointer();
470         }
471         else if( MKV_IS_ID( el, KaxCluster ) )
472         {
473             msg_Dbg( &sys.demuxer, "|   + Cluster" );
474
475             cluster = (KaxCluster*)el;
476
477             i_cluster_pos = i_start_pos = cluster->GetElementPosition();
478             ParseCluster( );
479
480             ep->Down();
481             /* stop pre-parsing the stream */
482             break;
483         }
484         else if( MKV_IS_ID( el, KaxAttachments ) )
485         {
486             msg_Dbg( &sys.demuxer, "|   + Attachments" );
487             if( i_attachments_position < 0 )
488                 ParseAttachments( static_cast<KaxAttachments*>( el ) );
489             i_attachments_position = (int64_t) es.I_O().getFilePointer();
490         }
491         else if( MKV_IS_ID( el, KaxChapters ) )
492         {
493             msg_Dbg( &sys.demuxer, "|   + Chapters" );
494             if( i_chapters_position < 0 )
495                 ParseChapters( static_cast<KaxChapters*>( el ) );
496             i_chapters_position = (int64_t) es.I_O().getFilePointer();
497         }
498         else if( MKV_IS_ID( el, KaxTag ) )
499         {
500             msg_Dbg( &sys.demuxer, "|   + Tags" );
501             if( i_tags_position < 0) // FIXME
502                 ;//LoadTags( static_cast<KaxTags*>( el ) );
503             i_tags_position = (int64_t) es.I_O().getFilePointer();
504         }
505         else
506             msg_Dbg( &sys.demuxer, "|   + Preload Unknown (%s)", typeid(*el).name() );
507     }
508
509     b_preloaded = true;
510
511     return true;
512 }
513
514 /* Here we try to load elements that were found in Seek Heads, but not yet parsed */
515 bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position )
516 {
517     int64_t     i_sav_position = (int64_t)es.I_O().getFilePointer();
518     EbmlElement *el;
519
520     es.I_O().setFilePointer( i_element_position, seek_beginning );
521     el = es.FindNextID( ClassInfos, 0xFFFFFFFFL);
522
523     if( el == NULL )
524     {
525         msg_Err( &sys.demuxer, "cannot load some cues/chapters/tags etc. (broken seekhead or file)" );
526         es.I_O().setFilePointer( i_sav_position, seek_beginning );
527         return false;
528     }
529
530     if( MKV_IS_ID( el, KaxSeekHead ) )
531     {
532         /* Multiple allowed */
533         msg_Dbg( &sys.demuxer, "|   + Seek head" );
534         if( i_seekhead_count < 10 )
535         {
536             i_seekhead_position = i_element_position;
537             ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
538         }
539     }
540     else if( MKV_IS_ID( el, KaxInfo ) ) // FIXME
541     {
542         /* Multiple allowed, mandatory */
543         msg_Dbg( &sys.demuxer, "|   + Information" );
544         if( i_info_position < 0 )
545             ParseInfo( static_cast<KaxInfo*>( el ) );
546         i_info_position = i_element_position;
547     }
548     else if( MKV_IS_ID( el, KaxTracks ) ) // FIXME
549     {
550         /* Multiple allowed */
551         msg_Dbg( &sys.demuxer, "|   + Tracks" );
552         if( i_tracks_position < 0 )
553             ParseTracks( static_cast<KaxTracks*>( el ) );
554         if ( tracks.size() == 0 )
555         {
556             msg_Err( &sys.demuxer, "No tracks supported" );
557             delete el;
558             es.I_O().setFilePointer( i_sav_position, seek_beginning );
559             return false;
560         }
561         i_tracks_position = i_element_position;
562     }
563     else if( MKV_IS_ID( el, KaxCues ) )
564     {
565         msg_Dbg( &sys.demuxer, "|   + Cues" );
566         if( i_cues_position < 0 )
567             LoadCues( static_cast<KaxCues*>( el ) );
568         i_cues_position = i_element_position;
569     }
570     else if( MKV_IS_ID( el, KaxAttachments ) )
571     {
572         msg_Dbg( &sys.demuxer, "|   + Attachments" );
573         if( i_attachments_position < 0 )
574             ParseAttachments( static_cast<KaxAttachments*>( el ) );
575         i_attachments_position = i_element_position;
576     }
577     else if( MKV_IS_ID( el, KaxChapters ) )
578     {
579         msg_Dbg( &sys.demuxer, "|   + Chapters" );
580         if( i_chapters_position < 0 )
581             ParseChapters( static_cast<KaxChapters*>( el ) );
582         i_chapters_position = i_element_position;
583     }
584     else if( MKV_IS_ID( el, KaxTag ) ) // FIXME
585     {
586         msg_Dbg( &sys.demuxer, "|   + Tags" );
587         if( i_tags_position < 0 )
588             ;//LoadTags( static_cast<KaxTags*>( el ) );
589         i_tags_position = i_element_position;
590     }
591     else
592     {
593         msg_Dbg( &sys.demuxer, "|   + LoadSeekHeadItem Unknown (%s)", typeid(*el).name() );
594     }
595     delete el;
596
597     es.I_O().setFilePointer( i_sav_position, seek_beginning );
598     return true;
599 }
600
601 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
602                                              const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
603 {
604     size_t          i_track;
605
606     for( i_track = 0; i_track < tracks.size(); i_track++ )
607     {
608         const mkv_track_t *tk = tracks[i_track];
609
610         if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
611             ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
612         {
613             break;
614         }
615     }
616
617     if( i_track >= tracks.size() )
618         return VLC_EGENERIC;
619
620     if( pi_track )
621         *pi_track = i_track;
622     return VLC_SUCCESS;
623 }
624
625 bool matroska_segment_c::Select( mtime_t i_start_time )
626 {
627     size_t i_track;
628
629     /* add all es */
630     msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
631     sys.b_pci_packet_set = false;
632
633     for( i_track = 0; i_track < tracks.size(); i_track++ )
634     {
635         mkv_track_t *p_tk = tracks[i_track];
636         es_format_t *p_fmt = &p_tk->fmt;
637
638         if( tracks[i_track]->fmt.i_cat == UNKNOWN_ES )
639         {
640             msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, tracks[i_track]->i_number );
641             tracks[i_track]->p_es = NULL;
642             continue;
643         }
644
645         if( !strcmp( tracks[i_track]->psz_codec, "V_MS/VFW/FOURCC" ) )
646         {
647             if( tracks[i_track]->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
648             {
649                 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
650                 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
651             }
652             else
653             {
654                 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tracks[i_track]->p_extra_data;
655
656                 tracks[i_track]->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
657                 tracks[i_track]->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
658                 tracks[i_track]->fmt.i_codec       = GetFOURCC( &p_bih->biCompression );
659
660                 tracks[i_track]->fmt.i_extra       = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
661                 if( tracks[i_track]->fmt.i_extra > 0 )
662                 {
663                     tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
664                     memcpy( tracks[i_track]->fmt.p_extra, &p_bih[1], tracks[i_track]->fmt.i_extra );
665                 }
666             }
667             p_tk->b_dts_only = true;
668         }
669         else if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG1" ) ||
670                  !strcmp( tracks[i_track]->psz_codec, "V_MPEG2" ) )
671         {
672             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
673         }
674         else if( !strncmp( tracks[i_track]->psz_codec, "V_THEORA", 8 ) )
675         {
676             uint8_t *p_data = tracks[i_track]->p_extra_data;
677             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
678             if( tracks[i_track]->i_extra_data >= 4 ) {
679                 if( p_data[0] == 2 ) {
680                     int i = 1;
681                     int i_size1 = 0, i_size2 = 0;
682                     p_data++;
683                     /* read size of first header packet */
684                     while( *p_data == 0xFF &&
685                            i < tracks[i_track]->i_extra_data )
686                     {
687                         i_size1 += *p_data;
688                         p_data++;
689                         i++;
690                     }
691                     i_size1 += *p_data;
692                     p_data++;
693                     i++;
694                     msg_Dbg( &sys.demuxer, "first theora header size %d", i_size1 );
695                     /* read size of second header packet */
696                     while( *p_data == 0xFF &&
697                            i < tracks[i_track]->i_extra_data )
698                     {
699                         i_size2 += *p_data;
700                         p_data++;
701                         i++;
702                     }
703                     i_size2 += *p_data;
704                     p_data++;
705                     i++;
706                     int i_size3 = tracks[i_track]->i_extra_data - i - i_size1
707                         - i_size2;
708                     msg_Dbg( &sys.demuxer, "second theora header size %d", i_size2 );
709                     msg_Dbg( &sys.demuxer, "third theora header size %d", i_size3 );
710                     tracks[i_track]->fmt.i_extra = i_size1 + i_size2 + i_size3
711                         + 6;
712                     if( i_size1 > 0 && i_size2 > 0 && i_size3 > 0  ) {
713                         tracks[i_track]->fmt.p_extra =
714                             malloc( tracks[i_track]->fmt.i_extra );
715                         uint8_t *p_out = (uint8_t*)tracks[i_track]->fmt.p_extra;
716                         *p_out++ = (i_size1>>8) & 0xFF;
717                         *p_out++ = i_size1 & 0xFF;
718                         memcpy( p_out, p_data, i_size1 );
719                         p_data += i_size1;
720                         p_out += i_size1;
721  
722                         *p_out++ = (i_size2>>8) & 0xFF;
723                         *p_out++ = i_size2 & 0xFF;
724                         memcpy( p_out, p_data, i_size2 );
725                         p_data += i_size2;
726                         p_out += i_size2;
727
728                         *p_out++ = (i_size3>>8) & 0xFF;
729                         *p_out++ = i_size3 & 0xFF;
730                         memcpy( p_out, p_data, i_size3 );
731                         p_data += i_size3;
732                         p_out += i_size3;
733                     }
734                     else
735                     {
736                         msg_Err( &sys.demuxer, "inconsistant theora extradata" );
737                     }
738                 }
739                 else {
740                     msg_Err( &sys.demuxer, "Wrong number of ogg packets with theora headers (%d)", p_data[0] + 1 );
741                 }
742             }
743         }
744         else if( !strncmp( tracks[i_track]->psz_codec, "V_REAL/RV", 9 ) )
745         {
746             if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
747                 p_fmt->i_codec = VLC_FOURCC( 'R', 'V', '1', '0' );
748             else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
749                 p_fmt->i_codec = VLC_FOURCC( 'R', 'V', '2', '0' );
750             else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
751                 p_fmt->i_codec = VLC_FOURCC( 'R', 'V', '3', '0' );
752             else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
753                 p_fmt->i_codec = VLC_FOURCC( 'R', 'V', '4', '0' );
754
755             if( p_tk->i_extra_data > 26 )
756             {
757                 p_fmt->p_extra = malloc( p_tk->i_extra_data - 26 );
758                 if( p_fmt->p_extra )
759                 {
760                     p_fmt->i_extra = p_tk->i_extra_data - 26;
761                     memcpy( p_fmt->p_extra, &p_tk->p_extra_data[26], p_fmt->i_extra );
762                 }
763             }
764             p_tk->b_dts_only = true;
765         }
766         else if( !strncmp( tracks[i_track]->psz_codec, "V_DIRAC", 7 ) )
767         {
768             tracks[i_track]->fmt.i_codec = VLC_FOURCC('d', 'r', 'a', 'c' );
769         }
770         else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4", 7 ) )
771         {
772             if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/MS/V3" ) )
773             {
774                 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
775             }
776             else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO", 11 ) )
777             {
778                 /* A MPEG 4 codec, SP, ASP, AP or AVC */
779                 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO/AVC" ) )
780                     tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
781                 else
782                     tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
783                 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
784                 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
785                 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
786             }
787         }
788         else if( !strcmp( tracks[i_track]->psz_codec, "V_QUICKTIME" ) )
789         {
790             MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
791             stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
792                                                        tracks[i_track]->p_extra_data,
793                                                        tracks[i_track]->i_extra_data,
794                                                        true );
795             if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
796                 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
797             {
798                 tracks[i_track]->fmt.i_codec = p_box->i_type;
799                 tracks[i_track]->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
800                 tracks[i_track]->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
801                 tracks[i_track]->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
802                 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
803                 memcpy( tracks[i_track]->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tracks[i_track]->fmt.i_extra );
804                 MP4_FreeBox_sample_vide( p_box );
805             }
806             else
807             {
808                 free( p_box );
809             }
810             stream_Delete( p_mp4_stream );
811         }
812         else if( !strcmp( tracks[i_track]->psz_codec, "A_MS/ACM" ) )
813         {
814             if( tracks[i_track]->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
815             {
816                 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
817                 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
818             }
819             else
820             {
821                 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tracks[i_track]->p_extra_data;
822
823                 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tracks[i_track]->fmt.i_codec, NULL );
824
825                 tracks[i_track]->fmt.audio.i_channels   = GetWLE( &p_wf->nChannels );
826                 tracks[i_track]->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
827                 tracks[i_track]->fmt.i_bitrate    = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
828                 tracks[i_track]->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
829                 tracks[i_track]->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
830
831                 tracks[i_track]->fmt.i_extra            = GetWLE( &p_wf->cbSize );
832                 if( tracks[i_track]->fmt.i_extra > 0 )
833                 {
834                     tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
835                     memcpy( tracks[i_track]->fmt.p_extra, &p_wf[1], tracks[i_track]->fmt.i_extra );
836                 }
837             }
838         }
839         else if( !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L3" ) ||
840                  !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L2" ) ||
841                  !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L1" ) )
842         {
843             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
844         }
845         else if( !strcmp( tracks[i_track]->psz_codec, "A_AC3" ) )
846         {
847             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
848         }
849         else if( !strcmp( tracks[i_track]->psz_codec, "A_EAC3" ) )
850         {
851             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'e', 'a', 'c', '3' );
852         }
853         else if( !strcmp( tracks[i_track]->psz_codec, "A_DTS" ) )
854         {
855             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
856         }
857         else if( !strcmp( tracks[i_track]->psz_codec, "A_MLP" ) )
858         {
859             tracks[i_track]->fmt.i_codec = VLC_FOURCC('m', 'l', 'p', ' ' );
860         }
861         else if( !strcmp( tracks[i_track]->psz_codec, "A_TRUEHD" ) )
862         {
863             /* FIXME when more samples arrive */
864             tracks[i_track]->fmt.i_codec = VLC_FOURCC('t', 'r', 'h', 'd' );
865             p_fmt->b_packetized = false;
866         }
867         else if( !strcmp( tracks[i_track]->psz_codec, "A_FLAC" ) )
868         {
869             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
870             tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
871             tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
872             memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
873         }
874         else if( !strcmp( tracks[i_track]->psz_codec, "A_VORBIS" ) )
875         {
876             int i, i_offset = 1, i_size[3], i_extra;
877             uint8_t *p_extra;
878
879             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
880
881             /* Split the 3 headers */
882             if( tracks[i_track]->p_extra_data[0] != 0x02 )
883                 msg_Err( &sys.demuxer, "invalid vorbis header" );
884
885             for( i = 0; i < 2; i++ )
886             {
887                 i_size[i] = 0;
888                 while( i_offset < tracks[i_track]->i_extra_data )
889                 {
890                     i_size[i] += tracks[i_track]->p_extra_data[i_offset];
891                     if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
892                 }
893             }
894
895             i_size[0] = __MIN(i_size[0], tracks[i_track]->i_extra_data - i_offset);
896             i_size[1] = __MIN(i_size[1], tracks[i_track]->i_extra_data -i_offset -i_size[0]);
897             i_size[2] = tracks[i_track]->i_extra_data - i_offset - i_size[0] - i_size[1];
898
899             tracks[i_track]->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
900             tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
901             p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra; i_extra = 0;
902             for( i = 0; i < 3; i++ )
903             {
904                 *(p_extra++) = i_size[i] >> 8;
905                 *(p_extra++) = i_size[i] & 0xFF;
906                 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra,
907                         i_size[i] );
908                 p_extra += i_size[i];
909                 i_extra += i_size[i];
910             }
911         }
912         else if( !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
913                  !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
914         {
915             int i_profile, i_srate, sbr = 0;
916             static const unsigned int i_sample_rates[] =
917             {
918                     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
919                         16000, 12000, 11025, 8000,  7350,  0,     0,     0
920             };
921
922             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
923             /* create data for faad (MP4DecSpecificDescrTag)*/
924
925             if( !strcmp( &tracks[i_track]->psz_codec[12], "MAIN" ) )
926             {
927                 i_profile = 0;
928             }
929             else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC" ) )
930             {
931                 i_profile = 1;
932             }
933             else if( !strcmp( &tracks[i_track]->psz_codec[12], "SSR" ) )
934             {
935                 i_profile = 2;
936             }
937             else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC/SBR" ) )
938             {
939                 i_profile = 1;
940                 sbr = 1;
941             }
942             else
943             {
944                 i_profile = 3;
945             }
946
947             for( i_srate = 0; i_srate < 13; i_srate++ )
948             {
949                 if( i_sample_rates[i_srate] == tracks[i_track]->i_original_rate )
950                 {
951                     break;
952                 }
953             }
954             msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
955
956             tracks[i_track]->fmt.i_extra = sbr ? 5 : 2;
957             tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
958             ((uint8_t*)tracks[i_track]->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
959             ((uint8_t*)tracks[i_track]->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tracks[i_track]->fmt.audio.i_channels << 3);
960             if (sbr != 0)
961             {
962                 int syncExtensionType = 0x2B7;
963                 int iDSRI;
964                 for (iDSRI=0; iDSRI<13; iDSRI++)
965                     if( i_sample_rates[iDSRI] == tracks[i_track]->fmt.audio.i_rate )
966                         break;
967                 ((uint8_t*)tracks[i_track]->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
968                 ((uint8_t*)tracks[i_track]->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
969                 ((uint8_t*)tracks[i_track]->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
970             }
971         }
972         else if( !strcmp( tracks[i_track]->psz_codec, "A_AAC" ) )
973         {
974             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
975             tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
976             tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
977             memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
978         }
979         else if( !strcmp( tracks[i_track]->psz_codec, "A_WAVPACK4" ) )
980         {
981             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'W', 'V', 'P', 'K' );
982             tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
983             tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
984             memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
985         }
986         else if( !strcmp( tracks[i_track]->psz_codec, "A_TTA1" ) )
987         {
988             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'T', 'T', 'A', '1' );
989             tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
990             tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
991             memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
992         }
993         else if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) ||
994                  !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/LIT" ) ||
995                  !strcmp( tracks[i_track]->psz_codec, "A_PCM/FLOAT/IEEE" ) )
996         {
997             if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) )
998             {
999                 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1000             }
1001             else
1002             {
1003                 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1004             }
1005             tracks[i_track]->fmt.audio.i_blockalign = ( tracks[i_track]->fmt.audio.i_bitspersample + 7 ) / 8 * tracks[i_track]->fmt.audio.i_channels;
1006         }
1007         /* disabled due to the potential "S_KATE" namespace issue */
1008         else if( !strcmp( tracks[i_track]->psz_codec, "S_KATE" ) )
1009         {
1010             int i, i_offset = 1, i_extra, num_headers, size_so_far;
1011             uint8_t *p_extra;
1012
1013             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'k', 'a', 't', 'e' );
1014             tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
1015
1016             /* Recover the number of headers to expect */
1017             num_headers = tracks[i_track]->p_extra_data[0]+1;
1018             msg_Dbg( &sys.demuxer, "kate in mkv detected: %d headers in %u bytes",
1019                 num_headers, tracks[i_track]->i_extra_data);
1020
1021             /* this won't overflow the stack as is can allocate only 1020 bytes max */
1022             uint16_t pi_size[num_headers];
1023
1024             /* Split the headers */
1025             size_so_far = 0;
1026             for( i = 0; i < num_headers-1; i++ )
1027             {
1028                 pi_size[i] = 0;
1029                 while( i_offset < tracks[i_track]->i_extra_data )
1030                 {
1031                     pi_size[i] += tracks[i_track]->p_extra_data[i_offset];
1032                     if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
1033                 }
1034                 msg_Dbg( &sys.demuxer, "kate header %d is %d bytes", i, pi_size[i]);
1035                 size_so_far += pi_size[i];
1036             }
1037             pi_size[num_headers-1] = tracks[i_track]->i_extra_data - (size_so_far+i_offset);
1038             msg_Dbg( &sys.demuxer, "kate last header (%d) is %d bytes", num_headers-1, pi_size[num_headers-1]);
1039
1040             tracks[i_track]->fmt.i_extra = 1 + num_headers * 2 + size_so_far + pi_size[num_headers-1];
1041             tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
1042
1043             p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra;
1044             i_extra = 0;
1045             *(p_extra++) = num_headers;
1046             ++i_extra;
1047             for( i = 0; i < num_headers; i++ )
1048             {
1049                 *(p_extra++) = pi_size[i] >> 8;
1050                 *(p_extra++) = pi_size[i] & 0xFF;
1051                 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra-1,
1052                         pi_size[i] );
1053
1054                 p_extra += pi_size[i];
1055                 i_extra += pi_size[i];
1056             }
1057         }
1058         else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASCII" ) )
1059         {
1060             p_fmt->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1061             p_fmt->subs.psz_encoding = NULL; /* Is there a place where it is stored ? */
1062         }
1063         else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/UTF8" ) )
1064         {
1065             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1066             tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
1067         }
1068         else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/USF" ) )
1069         {
1070             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
1071             tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
1072             if( tracks[i_track]->i_extra_data )
1073             {
1074                 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
1075                 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
1076                 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
1077             }
1078         }
1079         else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/SSA" ) ||
1080                  !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASS" ) ||
1081                  !strcmp( tracks[i_track]->psz_codec, "S_SSA" ) ||
1082                  !strcmp( tracks[i_track]->psz_codec, "S_ASS" ))
1083         {
1084             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1085             tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
1086             if( tracks[i_track]->i_extra_data )
1087             {
1088                 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
1089                 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
1090                 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
1091             }
1092         }
1093         else if( !strcmp( tracks[i_track]->psz_codec, "S_VOBSUB" ) )
1094         {
1095             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1096             if( tracks[i_track]->i_extra_data )
1097             {
1098                 char *p_start;
1099                 char *p_buf = (char *)malloc( tracks[i_track]->i_extra_data + 1);
1100                 memcpy( p_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
1101                 p_buf[tracks[i_track]->i_extra_data] = '\0';
1102  
1103                 p_start = strstr( p_buf, "size:" );
1104                 if( sscanf( p_start, "size: %dx%d",
1105                         &tracks[i_track]->fmt.subs.spu.i_original_frame_width, &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) == 2 )
1106                 {
1107                     msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d", tracks[i_track]->fmt.subs.spu.i_original_frame_width, tracks[i_track]->fmt.subs.spu.i_original_frame_height );
1108                 }
1109                 else
1110                 {
1111                     msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1112                 }
1113                 free( p_buf );
1114             }
1115         }
1116         else if( !strcmp( tracks[i_track]->psz_codec, "B_VOBBTN" ) )
1117         {
1118             tracks[i_track]->fmt.i_cat = NAV_ES;
1119             continue;
1120         }
1121         else if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1122         {
1123             p_fmt->i_codec = VLC_FOURCC( '1', '4', '_', '4');
1124             p_fmt->audio.i_channels = 1;
1125             p_fmt->audio.i_rate = 8000;
1126             p_fmt->audio.i_blockalign = 0x14;
1127         }
1128         else
1129         {
1130             msg_Err( &sys.demuxer, "unknown codec id=`%s'", tracks[i_track]->psz_codec );
1131             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1132         }
1133         if( tracks[i_track]->b_default )
1134         {
1135             tracks[i_track]->fmt.i_priority = 1000;
1136         }
1137
1138         tracks[i_track]->p_es = es_out_Add( sys.demuxer.out, &tracks[i_track]->fmt );
1139
1140         /* Turn on a subtitles track if it has been flagged as default -
1141          * but only do this if no subtitles track has already been engaged,
1142          * either by an earlier 'default track' (??) or by default
1143          * language choice behaviour.
1144          */
1145         if( tracks[i_track]->b_default )
1146         {
1147             es_out_Control( sys.demuxer.out,
1148                             ES_OUT_SET_ES_DEFAULT,
1149                             tracks[i_track]->p_es );
1150         }
1151     }
1152     es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
1153  
1154     sys.i_start_pts = i_start_time;
1155     // reset the stream reading to the first cluster of the segment used
1156     es.I_O().setFilePointer( i_start_pos );
1157
1158     delete ep;
1159     ep = new EbmlParser( &es, segment, &sys.demuxer );
1160
1161     return true;
1162 }
1163
1164 void matroska_segment_c::UnSelect( )
1165 {
1166     size_t i_track;
1167
1168     for( i_track = 0; i_track < tracks.size(); i_track++ )
1169     {
1170         if ( tracks[i_track]->p_es != NULL )
1171         {
1172 //            es_format_Clean( &tracks[i_track]->fmt );
1173             es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
1174             tracks[i_track]->p_es = NULL;
1175         }
1176     }
1177     delete ep;
1178     ep = NULL;
1179 }
1180
1181 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1182 {
1183     pp_simpleblock = NULL;
1184     pp_block = NULL;
1185     *pi_ref1  = 0;
1186     *pi_ref2  = 0;
1187
1188     for( ;; )
1189     {
1190         EbmlElement *el = NULL;
1191         int         i_level;
1192
1193         if ( ep == NULL )
1194             return VLC_EGENERIC;
1195
1196         if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1197         {
1198             /* Check blocks validity to protect againts broken files */
1199             if( BlockFindTrackIndex( NULL, pp_block , pp_simpleblock ) )
1200             {
1201                 delete pp_block;
1202                 pp_simpleblock = NULL;
1203                 pp_block = NULL;
1204                 continue;
1205             }
1206
1207             /* update the index */
1208 #define idx p_indexes[i_index - 1]
1209             if( i_index > 0 && idx.i_time == -1 )
1210             {
1211                 if ( pp_simpleblock != NULL )
1212                     idx.i_time        = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1213                 else
1214                     idx.i_time        = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1215                 idx.b_key         = *pi_ref1 == 0 ? true : false;
1216             }
1217 #undef idx
1218             return VLC_SUCCESS;
1219         }
1220
1221         i_level = ep->GetLevel();
1222
1223         if( el == NULL )
1224         {
1225             if( i_level > 1 )
1226             {
1227                 ep->Up();
1228                 continue;
1229             }
1230             msg_Warn( &sys.demuxer, "EOF" );
1231             return VLC_EGENERIC;
1232         }
1233
1234         /* Verify that we are still inside our cluster
1235          * It can happens whith broken files and when seeking
1236          * without index */
1237         if( i_level > 1 )
1238         {
1239             if( cluster && !ep->IsTopPresent( cluster ) )
1240             {
1241                 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1242                 cluster = NULL;
1243             }
1244             if( !cluster )
1245                 continue;
1246         }
1247
1248         /* do parsing */
1249         switch ( i_level )
1250         {
1251         case 1:
1252             if( MKV_IS_ID( el, KaxCluster ) )
1253             {
1254                 cluster = (KaxCluster*)el;
1255                 i_cluster_pos = cluster->GetElementPosition();
1256
1257                 /* add it to the index */
1258                 if( i_index == 0 ||
1259                     ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1260                 {
1261                     IndexAppendCluster( cluster );
1262                 }
1263
1264                 // reset silent tracks
1265                 for (size_t i=0; i<tracks.size(); i++)
1266                 {
1267                     tracks[i]->b_silent = false;
1268                 }
1269
1270                 ep->Down();
1271             }
1272             else if( MKV_IS_ID( el, KaxCues ) )
1273             {
1274                 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1275                 return VLC_EGENERIC;
1276             }
1277             else
1278             {
1279                 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1280             }
1281             break;
1282         case 2:
1283             if( MKV_IS_ID( el, KaxClusterTimecode ) )
1284             {
1285                 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1286
1287                 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1288                 cluster->InitTimecode( uint64( ctc ), i_timescale );
1289             }
1290             else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1291             {
1292                 ep->Down();
1293             }
1294             else if( MKV_IS_ID( el, KaxBlockGroup ) )
1295             {
1296                 i_block_pos = el->GetElementPosition();
1297                 ep->Down();
1298             }
1299             else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1300             {
1301                 pp_simpleblock = (KaxSimpleBlock*)el;
1302
1303                 pp_simpleblock->ReadData( es.I_O() );
1304                 pp_simpleblock->SetParent( *cluster );
1305             }
1306             break;
1307         case 3:
1308             if( MKV_IS_ID( el, KaxBlock ) )
1309             {
1310                 pp_block = (KaxBlock*)el;
1311
1312                 pp_block->ReadData( es.I_O() );
1313                 pp_block->SetParent( *cluster );
1314
1315                 ep->Keep();
1316             }
1317             else if( MKV_IS_ID( el, KaxBlockDuration ) )
1318             {
1319                 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1320
1321                 dur.ReadData( es.I_O() );
1322                 *pi_duration = uint64( dur );
1323             }
1324             else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1325             {
1326                 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1327
1328                 ref.ReadData( es.I_O() );
1329                 if( *pi_ref1 == 0 )
1330                 {
1331                     *pi_ref1 = int64( ref ) * cluster->GlobalTimecodeScale();
1332                 }
1333                 else if( *pi_ref2 == 0 )
1334                 {
1335                     *pi_ref2 = int64( ref ) * cluster->GlobalTimecodeScale();
1336                 }
1337             }
1338             else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1339             {
1340                 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1341                 track_num.ReadData( es.I_O() );
1342                 // find the track
1343                 for (size_t i=0; i<tracks.size(); i++)
1344                 {
1345                     if ( tracks[i]->i_number == uint32(track_num))
1346                     {
1347                         tracks[i]->b_silent = true;
1348                         break;
1349                     }
1350                 }
1351             }
1352             break;
1353         default:
1354             msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1355             return VLC_EGENERIC;
1356         }
1357     }
1358 }
1359