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