]> git.sesse.net Git - vlc/blob - modules/demux/mkv/matroska_segment.cpp
Modified the way xiph codecs headers are transported in VLC.
[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 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
608                                              const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
609 {
610     size_t          i_track;
611
612     for( i_track = 0; i_track < tracks.size(); i_track++ )
613     {
614         const mkv_track_t *tk = tracks[i_track];
615
616         if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
617             ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
618         {
619             break;
620         }
621     }
622
623     if( i_track >= tracks.size() )
624         return VLC_EGENERIC;
625
626     if( pi_track )
627         *pi_track = i_track;
628     return VLC_SUCCESS;
629 }
630
631 bool matroska_segment_c::Select( mtime_t i_start_time )
632 {
633     size_t i_track;
634
635     /* add all es */
636     msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
637     sys.b_pci_packet_set = false;
638
639     for( i_track = 0; i_track < tracks.size(); i_track++ )
640     {
641         mkv_track_t *p_tk = tracks[i_track];
642         es_format_t *p_fmt = &p_tk->fmt;
643
644         if( tracks[i_track]->fmt.i_cat == UNKNOWN_ES )
645         {
646             msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, tracks[i_track]->i_number );
647             tracks[i_track]->p_es = NULL;
648             continue;
649         }
650
651         if( !strcmp( tracks[i_track]->psz_codec, "V_MS/VFW/FOURCC" ) )
652         {
653             if( tracks[i_track]->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
654             {
655                 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
656                 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
657             }
658             else
659             {
660                 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tracks[i_track]->p_extra_data;
661
662                 tracks[i_track]->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
663                 tracks[i_track]->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
664                 tracks[i_track]->fmt.i_codec       = GetFOURCC( &p_bih->biCompression );
665
666                 tracks[i_track]->fmt.i_extra       = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
667                 if( tracks[i_track]->fmt.i_extra > 0 )
668                 {
669                     tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->fmt.i_extra );
670                     memcpy( tracks[i_track]->fmt.p_extra, &p_bih[1], tracks[i_track]->fmt.i_extra );
671                 }
672             }
673             p_tk->b_dts_only = true;
674         }
675         else if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG1" ) ||
676                  !strcmp( tracks[i_track]->psz_codec, "V_MPEG2" ) )
677         {
678             tracks[i_track]->fmt.i_codec = VLC_CODEC_MPGV;
679         }
680         else if( !strncmp( tracks[i_track]->psz_codec, "V_THEORA", 8 ) )
681         {
682             tracks[i_track]->fmt.i_codec = VLC_CODEC_THEORA;
683             tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
684             tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
685             memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
686         }
687         else if( !strncmp( tracks[i_track]->psz_codec, "V_REAL/RV", 9 ) )
688         {
689             if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
690                 p_fmt->i_codec = VLC_CODEC_RV10;
691             else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
692                 p_fmt->i_codec = VLC_CODEC_RV20;
693             else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
694                 p_fmt->i_codec = VLC_CODEC_RV30;
695             else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
696                 p_fmt->i_codec = VLC_CODEC_RV40;
697
698             if( p_tk->i_extra_data > 26 )
699             {
700                 p_fmt->p_extra = malloc( p_tk->i_extra_data - 26 );
701                 if( p_fmt->p_extra )
702                 {
703                     p_fmt->i_extra = p_tk->i_extra_data - 26;
704                     memcpy( p_fmt->p_extra, &p_tk->p_extra_data[26], p_fmt->i_extra );
705                 }
706             }
707             p_tk->b_dts_only = true;
708         }
709         else if( !strncmp( tracks[i_track]->psz_codec, "V_DIRAC", 7 ) )
710         {
711             tracks[i_track]->fmt.i_codec = VLC_CODEC_DIRAC;
712         }
713         else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4", 7 ) )
714         {
715             if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/MS/V3" ) )
716             {
717                 tracks[i_track]->fmt.i_codec = VLC_CODEC_DIV3;
718             }
719             else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO", 11 ) )
720             {
721                 /* A MPEG 4 codec, SP, ASP, AP or AVC */
722                 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO/AVC" ) )
723                     tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
724                 else
725                     tracks[i_track]->fmt.i_codec = VLC_CODEC_MP4V;
726                 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
727                 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
728                 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
729             }
730         }
731         else if( !strcmp( tracks[i_track]->psz_codec, "V_QUICKTIME" ) )
732         {
733             MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
734             stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
735                                                        tracks[i_track]->p_extra_data,
736                                                        tracks[i_track]->i_extra_data,
737                                                        true );
738             if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
739                 MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
740             {
741                 tracks[i_track]->fmt.i_codec = p_box->i_type;
742                 tracks[i_track]->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
743                 tracks[i_track]->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
744                 tracks[i_track]->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
745                 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->fmt.i_extra );
746                 memcpy( tracks[i_track]->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tracks[i_track]->fmt.i_extra );
747                 MP4_FreeBox_sample_vide( p_box );
748             }
749             else
750             {
751                 free( p_box );
752             }
753             stream_Delete( p_mp4_stream );
754         }
755         else if( !strcmp( tracks[i_track]->psz_codec, "A_MS/ACM" ) )
756         {
757             if( tracks[i_track]->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
758             {
759                 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
760                 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
761             }
762             else
763             {
764                 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tracks[i_track]->p_extra_data;
765
766                 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tracks[i_track]->fmt.i_codec, NULL );
767
768                 tracks[i_track]->fmt.audio.i_channels   = GetWLE( &p_wf->nChannels );
769                 tracks[i_track]->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
770                 tracks[i_track]->fmt.i_bitrate    = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
771                 tracks[i_track]->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
772                 tracks[i_track]->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
773
774                 tracks[i_track]->fmt.i_extra            = GetWLE( &p_wf->cbSize );
775                 if( tracks[i_track]->fmt.i_extra > 0 )
776                 {
777                     tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->fmt.i_extra );
778                     memcpy( tracks[i_track]->fmt.p_extra, &p_wf[1], tracks[i_track]->fmt.i_extra );
779                 }
780             }
781         }
782         else if( !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L3" ) ||
783                  !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L2" ) ||
784                  !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L1" ) )
785         {
786             tracks[i_track]->fmt.i_codec = VLC_CODEC_MPGA;
787         }
788         else if( !strcmp( tracks[i_track]->psz_codec, "A_AC3" ) )
789         {
790             tracks[i_track]->fmt.i_codec = VLC_CODEC_A52;
791         }
792         else if( !strcmp( tracks[i_track]->psz_codec, "A_EAC3" ) )
793         {
794             tracks[i_track]->fmt.i_codec = VLC_CODEC_EAC3;
795         }
796         else if( !strcmp( tracks[i_track]->psz_codec, "A_DTS" ) )
797         {
798             tracks[i_track]->fmt.i_codec = VLC_CODEC_DTS;
799         }
800         else if( !strcmp( tracks[i_track]->psz_codec, "A_MLP" ) )
801         {
802             tracks[i_track]->fmt.i_codec = VLC_CODEC_MLP;
803         }
804         else if( !strcmp( tracks[i_track]->psz_codec, "A_TRUEHD" ) )
805         {
806             /* FIXME when more samples arrive */
807             tracks[i_track]->fmt.i_codec = VLC_CODEC_TRUEHD;
808             p_fmt->b_packetized = false;
809         }
810         else if( !strcmp( tracks[i_track]->psz_codec, "A_FLAC" ) )
811         {
812             tracks[i_track]->fmt.i_codec = VLC_CODEC_FLAC;
813             tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
814             tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
815             memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
816         }
817         else if( !strcmp( tracks[i_track]->psz_codec, "A_VORBIS" ) )
818         {
819             tracks[i_track]->fmt.i_codec = VLC_CODEC_VORBIS;
820             tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
821             tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
822             memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
823         }
824         else if( !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
825                  !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
826         {
827             int i_profile, i_srate, sbr = 0;
828             static const unsigned int i_sample_rates[] =
829             {
830                     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
831                         16000, 12000, 11025, 8000,  7350,  0,     0,     0
832             };
833
834             tracks[i_track]->fmt.i_codec = VLC_CODEC_MP4A;
835             /* create data for faad (MP4DecSpecificDescrTag)*/
836
837             if( !strcmp( &tracks[i_track]->psz_codec[12], "MAIN" ) )
838             {
839                 i_profile = 0;
840             }
841             else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC" ) )
842             {
843                 i_profile = 1;
844             }
845             else if( !strcmp( &tracks[i_track]->psz_codec[12], "SSR" ) )
846             {
847                 i_profile = 2;
848             }
849             else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC/SBR" ) )
850             {
851                 i_profile = 1;
852                 sbr = 1;
853             }
854             else
855             {
856                 i_profile = 3;
857             }
858
859             for( i_srate = 0; i_srate < 13; i_srate++ )
860             {
861                 if( i_sample_rates[i_srate] == tracks[i_track]->i_original_rate )
862                 {
863                     break;
864                 }
865             }
866             msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
867
868             tracks[i_track]->fmt.i_extra = sbr ? 5 : 2;
869             tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->fmt.i_extra );
870             ((uint8_t*)tracks[i_track]->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
871             ((uint8_t*)tracks[i_track]->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tracks[i_track]->fmt.audio.i_channels << 3);
872             if (sbr != 0)
873             {
874                 int syncExtensionType = 0x2B7;
875                 int iDSRI;
876                 for (iDSRI=0; iDSRI<13; iDSRI++)
877                     if( i_sample_rates[iDSRI] == tracks[i_track]->fmt.audio.i_rate )
878                         break;
879                 ((uint8_t*)tracks[i_track]->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
880                 ((uint8_t*)tracks[i_track]->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
881                 ((uint8_t*)tracks[i_track]->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
882             }
883         }
884         else if( !strcmp( tracks[i_track]->psz_codec, "A_AAC" ) )
885         {
886             tracks[i_track]->fmt.i_codec = VLC_CODEC_MP4A;
887             tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
888             tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
889             memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
890         }
891         else if( !strcmp( tracks[i_track]->psz_codec, "A_WAVPACK4" ) )
892         {
893             tracks[i_track]->fmt.i_codec = VLC_CODEC_WAVPACK;
894             tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
895             tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
896             memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
897         }
898         else if( !strcmp( tracks[i_track]->psz_codec, "A_TTA1" ) )
899         {
900             p_fmt->i_codec = VLC_CODEC_TTA;
901             p_fmt->i_extra = p_tk->i_extra_data;
902             if( p_fmt->i_extra > 0 )
903             {
904                 p_fmt->p_extra = xmalloc( p_tk->i_extra_data );
905                 memcpy( p_fmt->p_extra, p_tk->p_extra_data, p_tk->i_extra_data );
906             }
907             else
908             {
909                 p_fmt->i_extra = 30;
910                 p_fmt->p_extra = xmalloc( p_fmt->i_extra );
911                 uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
912                 memcpy( &p_extra[ 0], "TTA1", 4 );
913                 SetWLE( &p_extra[ 4], 1 );
914                 SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
915                 SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
916                 SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
917                 SetDWLE( &p_extra[14], 0xffffffff );
918                 memset( &p_extra[18], 0, 30  - 18 );
919             }
920         }
921         else if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) ||
922                  !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/LIT" ) ||
923                  !strcmp( tracks[i_track]->psz_codec, "A_PCM/FLOAT/IEEE" ) )
924         {
925             if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) )
926             {
927                 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
928             }
929             else
930             {
931                 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
932             }
933             tracks[i_track]->fmt.audio.i_blockalign = ( tracks[i_track]->fmt.audio.i_bitspersample + 7 ) / 8 * tracks[i_track]->fmt.audio.i_channels;
934         }
935         /* disabled due to the potential "S_KATE" namespace issue */
936         else if( !strcmp( tracks[i_track]->psz_codec, "S_KATE" ) )
937         {
938             int i, i_offset = 1, i_extra, num_headers, size_so_far;
939             uint8_t *p_extra;
940
941             tracks[i_track]->fmt.i_codec = VLC_CODEC_KATE;
942             tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
943
944             tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
945             tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
946             memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
947         }
948         else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASCII" ) )
949         {
950             p_fmt->i_codec = VLC_CODEC_SUBT;
951             p_fmt->subs.psz_encoding = NULL; /* Is there a place where it is stored ? */
952         }
953         else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/UTF8" ) )
954         {
955             tracks[i_track]->fmt.i_codec = VLC_CODEC_SUBT;
956             tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
957         }
958         else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/USF" ) )
959         {
960             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
961             tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
962             if( tracks[i_track]->i_extra_data )
963             {
964                 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
965                 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
966                 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
967             }
968         }
969         else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/SSA" ) ||
970                  !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASS" ) ||
971                  !strcmp( tracks[i_track]->psz_codec, "S_SSA" ) ||
972                  !strcmp( tracks[i_track]->psz_codec, "S_ASS" ))
973         {
974             tracks[i_track]->fmt.i_codec = VLC_CODEC_SSA;
975             tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
976             if( tracks[i_track]->i_extra_data )
977             {
978                 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
979                 tracks[i_track]->fmt.p_extra = xmalloc( tracks[i_track]->i_extra_data );
980                 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
981             }
982         }
983         else if( !strcmp( tracks[i_track]->psz_codec, "S_VOBSUB" ) )
984         {
985             tracks[i_track]->fmt.i_codec = VLC_CODEC_SPU;
986             if( tracks[i_track]->i_extra_data )
987             {
988                 char *psz_start;
989                 char *psz_buf = (char *)malloc( tracks[i_track]->i_extra_data + 1);
990                 if( psz_buf != NULL )
991                 {
992                     memcpy( psz_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
993                     psz_buf[tracks[i_track]->i_extra_data] = '\0';
994
995                     psz_start = strstr( psz_buf, "size:" );
996                     if( psz_start &&
997                         vobsub_size_parse( psz_start,
998                                            &tracks[i_track]->fmt.subs.spu.i_original_frame_width,
999                                            &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
1000                     {
1001                         msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
1002                                  tracks[i_track]->fmt.subs.spu.i_original_frame_width,
1003                                  tracks[i_track]->fmt.subs.spu.i_original_frame_height );
1004                     }
1005                     else
1006                     {
1007                         msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1008                     }
1009
1010                     psz_start = strstr( psz_buf, "palette:" );
1011                     if( psz_start &&
1012                         vobsub_palette_parse( psz_start, &tracks[i_track]->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
1013                     {
1014                         tracks[i_track]->fmt.subs.spu.palette[0] =  0xBeef;
1015                         msg_Dbg( &sys.demuxer, "vobsub palette read" );
1016                     }
1017                     else
1018                     {
1019                         msg_Warn( &sys.demuxer, "reading original palette failed" );
1020                     }
1021                     free( psz_buf );
1022                 }
1023             }
1024         }
1025         else if( !strcmp( tracks[i_track]->psz_codec, "S_HDMV/PGS" ) )
1026         {
1027             tracks[i_track]->fmt.i_codec = VLC_CODEC_BD_PG;
1028         }
1029         else if( !strcmp( tracks[i_track]->psz_codec, "B_VOBBTN" ) )
1030         {
1031             tracks[i_track]->fmt.i_cat = NAV_ES;
1032             continue;
1033         }
1034         else if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
1035         {
1036             p_fmt->i_codec = VLC_CODEC_RA_144;
1037             p_fmt->audio.i_channels = 1;
1038             p_fmt->audio.i_rate = 8000;
1039             p_fmt->audio.i_blockalign = 0x14;
1040         }
1041         else
1042         {
1043             msg_Err( &sys.demuxer, "unknown codec id=`%s'", tracks[i_track]->psz_codec );
1044             tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1045         }
1046         if( tracks[i_track]->b_default )
1047         {
1048             tracks[i_track]->fmt.i_priority = 1000;
1049         }
1050
1051         tracks[i_track]->p_es = es_out_Add( sys.demuxer.out, &tracks[i_track]->fmt );
1052
1053         /* Turn on a subtitles track if it has been flagged as default -
1054          * but only do this if no subtitles track has already been engaged,
1055          * either by an earlier 'default track' (??) or by default
1056          * language choice behaviour.
1057          */
1058         if( tracks[i_track]->b_default )
1059         {
1060             es_out_Control( sys.demuxer.out,
1061                             ES_OUT_SET_ES_DEFAULT,
1062                             tracks[i_track]->p_es );
1063         }
1064     }
1065     es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
1066  
1067     sys.i_start_pts = i_start_time;
1068     // reset the stream reading to the first cluster of the segment used
1069     es.I_O().setFilePointer( i_start_pos );
1070
1071     delete ep;
1072     ep = new EbmlParser( &es, segment, &sys.demuxer );
1073
1074     return true;
1075 }
1076
1077 void matroska_segment_c::UnSelect( )
1078 {
1079     size_t i_track;
1080
1081     for( i_track = 0; i_track < tracks.size(); i_track++ )
1082     {
1083         if ( tracks[i_track]->p_es != NULL )
1084         {
1085 //            es_format_Clean( &tracks[i_track]->fmt );
1086             es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
1087             tracks[i_track]->p_es = NULL;
1088         }
1089     }
1090     delete ep;
1091     ep = NULL;
1092 }
1093
1094 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1095 {
1096     pp_simpleblock = NULL;
1097     pp_block = NULL;
1098     *pi_ref1  = 0;
1099     *pi_ref2  = 0;
1100
1101     for( ;; )
1102     {
1103         EbmlElement *el = NULL;
1104         int         i_level;
1105
1106         if ( ep == NULL )
1107             return VLC_EGENERIC;
1108
1109         if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1110         {
1111             /* Check blocks validity to protect againts broken files */
1112             if( BlockFindTrackIndex( NULL, pp_block , pp_simpleblock ) )
1113             {
1114                 delete pp_block;
1115                 pp_simpleblock = NULL;
1116                 pp_block = NULL;
1117                 continue;
1118             }
1119
1120             /* update the index */
1121 #define idx p_indexes[i_index - 1]
1122             if( i_index > 0 && idx.i_time == -1 )
1123             {
1124                 if ( pp_simpleblock != NULL )
1125                     idx.i_time        = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1126                 else
1127                     idx.i_time        = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1128                 idx.b_key         = *pi_ref1 == 0 ? true : false;
1129             }
1130 #undef idx
1131             return VLC_SUCCESS;
1132         }
1133
1134         i_level = ep->GetLevel();
1135
1136         if( el == NULL )
1137         {
1138             if( i_level > 1 )
1139             {
1140                 ep->Up();
1141                 continue;
1142             }
1143             msg_Warn( &sys.demuxer, "EOF" );
1144             return VLC_EGENERIC;
1145         }
1146
1147         /* Verify that we are still inside our cluster
1148          * It can happens whith broken files and when seeking
1149          * without index */
1150         if( i_level > 1 )
1151         {
1152             if( cluster && !ep->IsTopPresent( cluster ) )
1153             {
1154                 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1155                 cluster = NULL;
1156             }
1157             if( !cluster )
1158                 continue;
1159         }
1160
1161         /* do parsing */
1162         switch ( i_level )
1163         {
1164         case 1:
1165             if( MKV_IS_ID( el, KaxCluster ) )
1166             {
1167                 cluster = (KaxCluster*)el;
1168                 i_cluster_pos = cluster->GetElementPosition();
1169
1170                 /* add it to the index */
1171                 if( i_index == 0 ||
1172                     ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1173                 {
1174                     IndexAppendCluster( cluster );
1175                 }
1176
1177                 // reset silent tracks
1178                 for (size_t i=0; i<tracks.size(); i++)
1179                 {
1180                     tracks[i]->b_silent = false;
1181                 }
1182
1183                 ep->Down();
1184             }
1185             else if( MKV_IS_ID( el, KaxCues ) )
1186             {
1187                 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1188                 return VLC_EGENERIC;
1189             }
1190             else
1191             {
1192                 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1193             }
1194             break;
1195         case 2:
1196             if( MKV_IS_ID( el, KaxClusterTimecode ) )
1197             {
1198                 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1199
1200                 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1201                 cluster->InitTimecode( uint64( ctc ), i_timescale );
1202             }
1203             else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1204             {
1205                 ep->Down();
1206             }
1207             else if( MKV_IS_ID( el, KaxBlockGroup ) )
1208             {
1209                 i_block_pos = el->GetElementPosition();
1210                 ep->Down();
1211             }
1212             else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1213             {
1214                 pp_simpleblock = (KaxSimpleBlock*)el;
1215
1216                 pp_simpleblock->ReadData( es.I_O() );
1217                 pp_simpleblock->SetParent( *cluster );
1218             }
1219             break;
1220         case 3:
1221             if( MKV_IS_ID( el, KaxBlock ) )
1222             {
1223                 pp_block = (KaxBlock*)el;
1224
1225                 pp_block->ReadData( es.I_O() );
1226                 pp_block->SetParent( *cluster );
1227
1228                 ep->Keep();
1229             }
1230             else if( MKV_IS_ID( el, KaxBlockDuration ) )
1231             {
1232                 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1233
1234                 dur.ReadData( es.I_O() );
1235                 *pi_duration = uint64( dur );
1236             }
1237             else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1238             {
1239                 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1240
1241                 ref.ReadData( es.I_O() );
1242                 if( *pi_ref1 == 0 )
1243                 {
1244                     *pi_ref1 = int64( ref ) * cluster->GlobalTimecodeScale();
1245                 }
1246                 else if( *pi_ref2 == 0 )
1247                 {
1248                     *pi_ref2 = int64( ref ) * cluster->GlobalTimecodeScale();
1249                 }
1250             }
1251             else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1252             {
1253                 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1254                 track_num.ReadData( es.I_O() );
1255                 // find the track
1256                 for (size_t i=0; i<tracks.size(); i++)
1257                 {
1258                     if ( tracks[i]->i_number == uint32(track_num))
1259                     {
1260                         tracks[i]->b_silent = true;
1261                         break;
1262                     }
1263                 }
1264             }
1265             break;
1266         default:
1267             msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1268             return VLC_EGENERIC;
1269         }
1270     }
1271 }
1272