]> git.sesse.net Git - vlc/blob - modules/demux/mkv/matroska_segment.cpp
Initialize codecs after track parsing and not before the selection.
[vlc] / modules / demux / mkv / matroska_segment.cpp
1 /*****************************************************************************
2  * matroska_segment.cpp : matroska demuxer
3  *****************************************************************************
4  * Copyright (C) 2003-2010 VLC authors and VideoLAN
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Steve Lhomme <steve.lhomme@free.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "matroska_segment.hpp"
26 #include "chapters.hpp"
27 #include "demux.hpp"
28 #include "util.hpp"
29 #include "Ebml_parser.hpp"
30
31 matroska_segment_c::matroska_segment_c( demux_sys_t & demuxer, EbmlStream & estream )
32     :segment(NULL)
33     ,es(estream)
34     ,i_timescale(MKVD_TIMECODESCALE)
35     ,i_duration(-1)
36     ,i_start_time(0)
37     ,i_seekhead_count(0)
38     ,i_seekhead_position(-1)
39     ,i_cues_position(-1)
40     ,i_tracks_position(-1)
41     ,i_info_position(-1)
42     ,i_chapters_position(-1)
43     ,i_tags_position(-1)
44     ,i_attachments_position(-1)
45     ,cluster(NULL)
46     ,i_block_pos(0)
47     ,i_cluster_pos(0)
48     ,i_start_pos(0)
49     ,p_segment_uid(NULL)
50     ,p_prev_segment_uid(NULL)
51     ,p_next_segment_uid(NULL)
52     ,b_cues(false)
53     ,i_index(0)
54     ,i_index_max(1024)
55     ,psz_muxing_application(NULL)
56     ,psz_writing_application(NULL)
57     ,psz_segment_filename(NULL)
58     ,psz_title(NULL)
59     ,psz_date_utc(NULL)
60     ,i_default_edition(0)
61     ,sys(demuxer)
62     ,ep(NULL)
63     ,b_preloaded(false)
64     ,b_ref_external_segments(false)
65 {
66     p_indexes = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
67 }
68
69 matroska_segment_c::~matroska_segment_c()
70 {
71     for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
72     {
73         delete tracks[i_track]->p_compression_data;
74         es_format_Clean( &tracks[i_track]->fmt );
75         delete tracks[i_track]->p_sys;
76         free( tracks[i_track]->p_extra_data );
77         free( tracks[i_track]->psz_codec );
78         delete tracks[i_track];
79     }
80
81     free( psz_writing_application );
82     free( psz_muxing_application );
83     free( psz_segment_filename );
84     free( psz_title );
85     free( psz_date_utc );
86     free( p_indexes );
87
88     delete ep;
89     delete segment;
90     delete p_segment_uid;
91     delete p_prev_segment_uid;
92     delete p_next_segment_uid;
93
94     vlc_delete_all( stored_editions );
95     vlc_delete_all( translations );
96     vlc_delete_all( families );
97 }
98
99
100 /*****************************************************************************
101  * Tools                                                                     *
102  *****************************************************************************
103  *  * LoadCues : load the cues element and update index
104  *  * LoadTags : load ... the tags element
105  *  * InformationCreate : create all information, load tags if present
106  *****************************************************************************/
107 void matroska_segment_c::LoadCues( KaxCues *cues )
108 {
109     EbmlParser  *ep;
110     EbmlElement *el;
111
112     if( b_cues )
113     {
114         msg_Err( &sys.demuxer, "There can be only 1 Cues per section." );
115         return;
116     }
117
118     ep = new EbmlParser( &es, cues, &sys.demuxer );
119     while( ( el = ep->Get() ) != NULL )
120     {
121         if( MKV_IS_ID( el, KaxCuePoint ) )
122         {
123 #define idx p_indexes[i_index]
124
125             idx.i_track       = -1;
126             idx.i_block_number= -1;
127             idx.i_position    = -1;
128             idx.i_time        = 0;
129             idx.b_key         = true;
130
131             ep->Down();
132             while( ( el = ep->Get() ) != NULL )
133             {
134                 if( MKV_IS_ID( el, KaxCueTime ) )
135                 {
136                     KaxCueTime &ctime = *(KaxCueTime*)el;
137
138                     ctime.ReadData( es.I_O() );
139
140                     idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
141                 }
142                 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
143                 {
144                     ep->Down();
145                     while( ( el = ep->Get() ) != NULL )
146                     {
147                         if( MKV_IS_ID( el, KaxCueTrack ) )
148                         {
149                             KaxCueTrack &ctrack = *(KaxCueTrack*)el;
150
151                             ctrack.ReadData( es.I_O() );
152                             idx.i_track = uint16( ctrack );
153                         }
154                         else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
155                         {
156                             KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
157
158                             ccpos.ReadData( es.I_O() );
159                             idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
160                         }
161                         else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
162                         {
163                             KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
164
165                             cbnum.ReadData( es.I_O() );
166                             idx.i_block_number = uint32( cbnum );
167                         }
168                         else
169                         {
170                             msg_Dbg( &sys.demuxer, "         * Unknown (%s)", typeid(*el).name() );
171                         }
172                     }
173                     ep->Up();
174                 }
175                 else
176                 {
177                     msg_Dbg( &sys.demuxer, "     * Unknown (%s)", typeid(*el).name() );
178                 }
179             }
180             ep->Up();
181
182 #if 0
183             msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
184                      " track=%d bnum=%d", idx.i_time, idx.i_position,
185                      idx.i_track, idx.i_block_number );
186 #endif
187
188             i_index++;
189             if( i_index >= i_index_max )
190             {
191                 i_index_max += 1024;
192                 p_indexes = (mkv_index_t*)xrealloc( p_indexes,
193                                         sizeof( mkv_index_t ) * i_index_max );
194             }
195 #undef idx
196         }
197         else
198         {
199             msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
200         }
201     }
202     delete ep;
203     b_cues = true;
204     msg_Dbg( &sys.demuxer, "|   - loading cues done." );
205 }
206
207
208 static const struct {
209     vlc_meta_type_t type;
210     const char *key;
211     int target_type; /* 0 is valid for all target_type */
212 } metadata_map[] = {
213                      {vlc_meta_Album,       "TITLE",         50},
214                      {vlc_meta_Title,       "TITLE",         0},
215                      {vlc_meta_Artist,      "ARTIST",        0},
216                      {vlc_meta_Genre,       "GENRE",         0},
217                      {vlc_meta_Copyright,   "COPYRIGHT",     0},
218                      {vlc_meta_TrackNumber, "PART_NUMBER",   0},
219                      {vlc_meta_Description, "DESCRIPTION",   0},
220                      {vlc_meta_Description, "COMMENT",       0},
221                      {vlc_meta_Rating,      "RATING",        0},
222                      {vlc_meta_Date,        "DATE_RELEASED", 0},
223                      {vlc_meta_Date,        "DATE_RELEASE",  0},
224                      {vlc_meta_Date,        "DATE_RECORDED", 0},
225                      {vlc_meta_URL,         "URL",           0},
226                      {vlc_meta_Publisher,   "PUBLISHER",     0},
227                      {vlc_meta_EncodedBy,   "ENCODED_BY",    0},
228                      {vlc_meta_TrackTotal,  "TOTAL_PARTS",   0},
229                      {vlc_meta_Title,       NULL,            0},
230 };
231
232 SimpleTag * matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag, int target_type )
233 {
234     EbmlElement *el;
235     EbmlParser *ep = new EbmlParser( &es, tag, &sys.demuxer );
236     SimpleTag * p_simple = new SimpleTag;
237
238     if( !p_simple )
239     {
240         msg_Err( &sys.demuxer, "Couldn't allocate memory for Simple Tag... ignoring it");
241         return NULL;
242     }
243
244     if( !sys.meta )
245         sys.meta = vlc_meta_New();
246
247     msg_Dbg( &sys.demuxer, "|   + Simple Tag ");
248     while( ( el = ep->Get() ) != NULL )
249     {
250         if( MKV_IS_ID( el, KaxTagName ) )
251         {
252             KaxTagName &key = *(KaxTagName*)el;
253             key.ReadData( es.I_O(), SCOPE_ALL_DATA );
254             p_simple->psz_tag_name = strdup( UTFstring( key ).GetUTF8().c_str() );
255         }
256         else if( MKV_IS_ID( el, KaxTagString ) )
257         {
258             KaxTagString &value = *(KaxTagString*)el;
259             value.ReadData( es.I_O(), SCOPE_ALL_DATA );
260             p_simple->p_value = strdup( UTFstring( value ).GetUTF8().c_str() );
261         }
262         else if(  MKV_IS_ID( el, KaxTagLangue ) )
263         {
264             KaxTagLangue &language = *(KaxTagLangue*) el;
265             language.ReadData( es.I_O(), SCOPE_ALL_DATA );
266             p_simple->psz_lang = strdup( string( language ).c_str());
267         }
268         else if(  MKV_IS_ID( el, KaxTagDefault ) )
269         {
270             KaxTagDefault & dft = *(KaxTagDefault*) el;
271             dft.ReadData( es.I_O(), SCOPE_ALL_DATA );
272             p_simple->b_default = (bool) uint8( dft );
273         }
274         /*Tags can be nested*/
275         else if( MKV_IS_ID( el, KaxTagSimple) )
276         {
277             SimpleTag * p_st = ParseSimpleTags( (KaxTagSimple*)el, target_type );
278             if( p_st )
279                 p_simple->sub_tags.push_back( p_st );
280         }
281         /*TODO Handle binary tags*/
282     }
283     delete ep;
284
285     if( !p_simple->psz_tag_name || !p_simple->p_value )
286     {
287         msg_Warn( &sys.demuxer, "Invalid MKV SimpleTag found.");
288         delete p_simple;
289         return NULL;
290     }
291
292     for( int i = 0; metadata_map[i].key; i++ )
293     {
294         if( !strcmp( p_simple->psz_tag_name, metadata_map[i].key ) &&
295             (metadata_map[i].target_type == 0 || target_type == metadata_map[i].target_type ) )
296         {
297             vlc_meta_Set( sys.meta, metadata_map[i].type, p_simple->p_value );
298             msg_Dbg( &sys.demuxer, "|   |   + Meta %s: %s", p_simple->psz_tag_name, p_simple->p_value);
299             goto done;
300         }
301     }
302     msg_Dbg( &sys.demuxer, "|   |   + Meta %s: %s", p_simple->psz_tag_name, p_simple->p_value);
303     vlc_meta_AddExtra( sys.meta, p_simple->psz_tag_name, p_simple->p_value);
304 done:
305     return p_simple;
306 }
307
308 #define PARSE_TAG( type ) \
309     do { \
310     msg_Dbg( &sys.demuxer, "|   + " type ); \
311     ep->Down();                             \
312     while( ( el = ep->Get() ) != NULL )     \
313     {                                       \
314         msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid( *el ).name() ); \
315     }                                      \
316     ep->Up(); } while( 0 )
317
318
319 void matroska_segment_c::LoadTags( KaxTags *tags )
320 {
321     /* Master elements */
322     EbmlParser *ep = new EbmlParser( &es, tags, &sys.demuxer );
323     EbmlElement *el;
324
325     while( ( el = ep->Get() ) != NULL )
326     {
327         if( MKV_IS_ID( el, KaxTag ) )
328         {
329             Tag * p_tag = new Tag;
330             if(!p_tag)
331             {
332                 msg_Err( &sys.demuxer,"Couldn't allocate memory for tag... ignoring it");
333                 continue;
334             }
335             msg_Dbg( &sys.demuxer, "+ Tag" );
336             ep->Down();
337             int target_type = 50;
338             while( ( el = ep->Get() ) != NULL )
339             {
340                 if( MKV_IS_ID( el, KaxTagTargets ) )
341                 {
342                     msg_Dbg( &sys.demuxer, "|   + Targets" );
343                     ep->Down();
344                     while( ( el = ep->Get() ) != NULL )
345                     {
346                         if( MKV_IS_ID( el, KaxTagTargetTypeValue ) )
347                         {
348                             KaxTagTargetTypeValue &value = *(KaxTagTargetTypeValue*)el;
349                             value.ReadData( es.I_O() );
350
351                             msg_Dbg( &sys.demuxer, "|   |   + TargetTypeValue: %u", uint32(value));
352                             target_type = uint32(value);
353                         }
354                         if( MKV_IS_ID( el, KaxTagTrackUID ) )
355                         {
356                             p_tag->i_tag_type = TRACK_UID;
357                             KaxTagTrackUID &uid = *(KaxTagTrackUID*) el;
358                             uid.ReadData( es.I_O() );
359                             p_tag->i_uid = uint64( uid );
360                             msg_Dbg( &sys.demuxer, "|   |   + TrackUID: %"PRIu64, p_tag->i_uid);
361
362                         }
363                         if( MKV_IS_ID( el, KaxTagEditionUID ) )
364                         {
365                             p_tag->i_tag_type = EDITION_UID;
366                             KaxTagEditionUID &uid = *(KaxTagEditionUID*) el;
367                             uid.ReadData( es.I_O() );
368                             p_tag->i_uid = uint64( uid );
369                             msg_Dbg( &sys.demuxer, "|   |   + EditionUID: %"PRIu64, p_tag->i_uid);
370                         }
371                         if( MKV_IS_ID( el, KaxTagChapterUID ) )
372                         {
373                             p_tag->i_tag_type = CHAPTER_UID;
374                             KaxTagChapterUID &uid = *(KaxTagChapterUID*) el;
375                             uid.ReadData( es.I_O() );
376                             p_tag->i_uid = uint64( uid );
377                             msg_Dbg( &sys.demuxer, "|   |   + ChapterUID: %"PRIu64, p_tag->i_uid);
378                         }
379                         if( MKV_IS_ID( el, KaxTagAttachmentUID ) )
380                         {
381                             p_tag->i_tag_type = ATTACHMENT_UID;
382                             KaxTagAttachmentUID &uid = *(KaxTagAttachmentUID*) el;
383                             uid.ReadData( es.I_O() );
384                             p_tag->i_uid = uint64( uid );
385                             msg_Dbg( &sys.demuxer, "|   |   + AttachmentUID: %"PRIu64, p_tag->i_uid);
386                         }
387                     }
388                     ep->Up();
389                 }
390                 else if( MKV_IS_ID( el, KaxTagSimple ) )
391                 {
392                     SimpleTag * p_simple =
393                         ParseSimpleTags( static_cast<KaxTagSimple*>( el ),
394                                          target_type );
395                     if( p_simple )
396                         p_tag->simple_tags.push_back( p_simple );
397                 }
398 #if 0 // not valid anymore
399                 else if( MKV_IS_ID( el, KaxTagGeneral ) )
400                     PARSE_TAG( "General" );
401                 else if( MKV_IS_ID( el, KaxTagGenres ) )
402                     PARSE_TAG( "Genres" );
403                 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
404                     PARSE_TAG( "Audio Specific" );
405                 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
406                     PARSE_TAG( "Images Specific" );
407                 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
408                 {
409                     msg_Dbg( &sys.demuxer, "|   + Multi Comment" );
410                 }
411                 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
412                 {
413                     msg_Dbg( &sys.demuxer, "|   + Multi Commercial" );
414                 }
415                 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
416                 {
417                     msg_Dbg( &sys.demuxer, "|   + Multi Date" );
418                 }
419                 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
420                 {
421                     msg_Dbg( &sys.demuxer, "|   + Multi Entity" );
422                 }
423                 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
424                 {
425                     msg_Dbg( &sys.demuxer, "|   + Multi Identifier" );
426                 }
427                 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
428                 {
429                     msg_Dbg( &sys.demuxer, "|   + Multi Legal" );
430                 }
431                 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
432                 {
433                     msg_Dbg( &sys.demuxer, "|   + Multi Title" );
434                 }
435 #endif
436                 else
437                 {
438                     msg_Dbg( &sys.demuxer, "|   + LoadTag Unknown (%s)", typeid( *el ).name() );
439                 }
440             }
441             ep->Up();
442             this->tags.push_back(p_tag);
443         }
444         else
445         {
446             msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
447         }
448     }
449     delete ep;
450
451     msg_Dbg( &sys.demuxer, "loading tags done." );
452 }
453 #undef PARSE_TAG
454
455 /*****************************************************************************
456  * InformationCreate:
457  *****************************************************************************/
458 void matroska_segment_c::InformationCreate( )
459 {
460     if( !sys.meta )
461         sys.meta = vlc_meta_New();
462
463     if( psz_title )
464     {
465         vlc_meta_SetTitle( sys.meta, psz_title );
466     }
467 #if 0
468     if( psz_date_utc )
469     {
470         vlc_meta_SetDate( sys.meta, psz_date_utc );
471     }
472
473     if( psz_segment_filename )
474     {
475         fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
476     }
477     if( psz_muxing_application )
478     {
479         fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
480     }
481     if( psz_writing_application )
482     {
483         fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
484     }
485
486     for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
487     {
488 //        mkv_track_t *tk = tracks[i_track];
489 //        vlc_meta_t *mtk = vlc_meta_New();
490         fprintf( stderr, "***** WARNING: Unhandled child meta\n");
491     }
492 #endif
493 #if 0
494     if( i_tags_position >= 0 )
495     {
496         bool b_seekable;
497
498         stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
499         if( b_seekable )
500         {
501             LoadTags( );
502         }
503     }
504 #endif
505 }
506
507
508 /*****************************************************************************
509  * Misc
510  *****************************************************************************/
511
512 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
513 {
514 #define idx p_indexes[i_index]
515     idx.i_track       = -1;
516     idx.i_block_number= -1;
517     idx.i_position    = cluster->GetElementPosition();
518     idx.i_time        = cluster->GlobalTimecode()/ (mtime_t) 1000;
519     idx.b_key         = true;
520
521     i_index++;
522     if( i_index >= i_index_max )
523     {
524         i_index_max += 1024;
525         p_indexes = (mkv_index_t*)xrealloc( p_indexes,
526                                         sizeof( mkv_index_t ) * i_index_max );
527     }
528 #undef idx
529 }
530
531 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
532 {
533     if ( b_preloaded )
534         return false;
535
536     for (size_t i=0; i<families.size(); i++)
537     {
538         for (size_t j=0; j<of_segment.families.size(); j++)
539         {
540             if ( *(families[i]) == *(of_segment.families[j]) )
541                 return Preload( );
542         }
543     }
544
545     return false;
546 }
547
548 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
549 {
550     EbmlBinary *p_tmp;
551
552     if ( p_item_a == NULL || p_item_b == NULL )
553         return false;
554
555     p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
556     if ( p_item_b->p_prev_segment_uid != NULL
557           && *p_tmp == *p_item_b->p_prev_segment_uid )
558         return true;
559
560     p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
561     if ( !p_tmp )
562         return false;
563
564     if ( p_item_b->p_segment_uid != NULL
565           && *p_tmp == *p_item_b->p_segment_uid )
566         return true;
567
568     if ( p_item_b->p_prev_segment_uid != NULL
569           && *p_tmp == *p_item_b->p_prev_segment_uid )
570         return true;
571
572     return false;
573 }
574
575 bool matroska_segment_c::Preload( )
576 {
577     if ( b_preloaded )
578         return false;
579
580     EbmlElement *el = NULL;
581
582     ep->Reset( &sys.demuxer );
583
584     while( ( el = ep->Get() ) != NULL )
585     {
586         if( MKV_IS_ID( el, KaxSeekHead ) )
587         {
588             /* Multiple allowed */
589             /* We bail at 10, to prevent possible recursion */
590             msg_Dbg(  &sys.demuxer, "|   + Seek head" );
591             if( i_seekhead_count < 10 )
592             {
593                 i_seekhead_position = (int64_t) es.I_O().getFilePointer();
594                 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
595             }
596         }
597         else if( MKV_IS_ID( el, KaxInfo ) )
598         {
599             /* Multiple allowed, mandatory */
600             msg_Dbg(  &sys.demuxer, "|   + Information" );
601             if( i_info_position < 0 ) // FIXME
602                 ParseInfo( static_cast<KaxInfo*>( el ) );
603             i_info_position = (int64_t) es.I_O().getFilePointer();
604         }
605         else if( MKV_IS_ID( el, KaxTracks ) )
606         {
607             /* Multiple allowed */
608             msg_Dbg(  &sys.demuxer, "|   + Tracks" );
609             if( i_tracks_position < 0 ) // FIXME
610                 ParseTracks( static_cast<KaxTracks*>( el ) );
611             if ( tracks.size() == 0 )
612             {
613                 msg_Err( &sys.demuxer, "No tracks supported" );
614                 return false;
615             }
616             i_tracks_position = (int64_t) es.I_O().getFilePointer();
617         }
618         else if( MKV_IS_ID( el, KaxCues ) )
619         {
620             msg_Dbg(  &sys.demuxer, "|   + Cues" );
621             if( i_cues_position < 0 )
622                 LoadCues( static_cast<KaxCues*>( el ) );
623             i_cues_position = (int64_t) es.I_O().getFilePointer();
624         }
625         else if( MKV_IS_ID( el, KaxCluster ) )
626         {
627             msg_Dbg( &sys.demuxer, "|   + Cluster" );
628
629             cluster = (KaxCluster*)el;
630
631             i_cluster_pos = i_start_pos = cluster->GetElementPosition();
632             ParseCluster( );
633
634             ep->Down();
635             /* stop pre-parsing the stream */
636             break;
637         }
638         else if( MKV_IS_ID( el, KaxAttachments ) )
639         {
640             msg_Dbg( &sys.demuxer, "|   + Attachments" );
641             if( i_attachments_position < 0 )
642                 ParseAttachments( static_cast<KaxAttachments*>( el ) );
643             i_attachments_position = (int64_t) es.I_O().getFilePointer();
644         }
645         else if( MKV_IS_ID( el, KaxChapters ) )
646         {
647             msg_Dbg( &sys.demuxer, "|   + Chapters" );
648             if( i_chapters_position < 0 )
649                 ParseChapters( static_cast<KaxChapters*>( el ) );
650             i_chapters_position = (int64_t) es.I_O().getFilePointer();
651         }
652         else if( MKV_IS_ID( el, KaxTag ) )
653         {
654             msg_Dbg( &sys.demuxer, "|   + Tags" );
655             /*FIXME if( i_tags_position < 0)
656                 LoadTags( static_cast<KaxTags*>( el ) );*/
657             i_tags_position = (int64_t) es.I_O().getFilePointer();
658         }
659         else if( MKV_IS_ID( el, EbmlVoid ) )
660             msg_Dbg( &sys.demuxer, "|   + Void" );
661         else
662             msg_Dbg( &sys.demuxer, "|   + Preload Unknown (%s)", typeid(*el).name() );
663     }
664
665     b_preloaded = true;
666
667     return true;
668 }
669
670 /* Here we try to load elements that were found in Seek Heads, but not yet parsed */
671 bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position )
672 {
673     int64_t     i_sav_position = (int64_t)es.I_O().getFilePointer();
674     EbmlElement *el;
675
676     es.I_O().setFilePointer( i_element_position, seek_beginning );
677     el = es.FindNextID( ClassInfos, 0xFFFFFFFFL);
678
679     if( el == NULL )
680     {
681         msg_Err( &sys.demuxer, "cannot load some cues/chapters/tags etc. (broken seekhead or file)" );
682         es.I_O().setFilePointer( i_sav_position, seek_beginning );
683         return false;
684     }
685
686     if( MKV_IS_ID( el, KaxSeekHead ) )
687     {
688         /* Multiple allowed */
689         msg_Dbg( &sys.demuxer, "|   + Seek head" );
690         if( i_seekhead_count < 10 )
691         {
692             i_seekhead_position = i_element_position;
693             ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
694         }
695     }
696     else if( MKV_IS_ID( el, KaxInfo ) ) // FIXME
697     {
698         /* Multiple allowed, mandatory */
699         msg_Dbg( &sys.demuxer, "|   + Information" );
700         if( i_info_position < 0 )
701             ParseInfo( static_cast<KaxInfo*>( el ) );
702         i_info_position = i_element_position;
703     }
704     else if( MKV_IS_ID( el, KaxTracks ) ) // FIXME
705     {
706         /* Multiple allowed */
707         msg_Dbg( &sys.demuxer, "|   + Tracks" );
708         if( i_tracks_position < 0 )
709             ParseTracks( static_cast<KaxTracks*>( el ) );
710         if ( tracks.size() == 0 )
711         {
712             msg_Err( &sys.demuxer, "No tracks supported" );
713             delete el;
714             es.I_O().setFilePointer( i_sav_position, seek_beginning );
715             return false;
716         }
717         i_tracks_position = i_element_position;
718     }
719     else if( MKV_IS_ID( el, KaxCues ) )
720     {
721         msg_Dbg( &sys.demuxer, "|   + Cues" );
722         if( i_cues_position < 0 )
723             LoadCues( static_cast<KaxCues*>( el ) );
724         i_cues_position = i_element_position;
725     }
726     else if( MKV_IS_ID( el, KaxAttachments ) )
727     {
728         msg_Dbg( &sys.demuxer, "|   + Attachments" );
729         if( i_attachments_position < 0 )
730             ParseAttachments( static_cast<KaxAttachments*>( el ) );
731         i_attachments_position = i_element_position;
732     }
733     else if( MKV_IS_ID( el, KaxChapters ) )
734     {
735         msg_Dbg( &sys.demuxer, "|   + Chapters" );
736         if( i_chapters_position < 0 )
737             ParseChapters( static_cast<KaxChapters*>( el ) );
738         i_chapters_position = i_element_position;
739     }
740     else if( MKV_IS_ID( el, KaxTags ) )
741     {
742         msg_Dbg( &sys.demuxer, "|   + Tags" );
743         if( i_tags_position < 0 )
744             LoadTags( static_cast<KaxTags*>( el ) );
745         i_tags_position = i_element_position;
746     }
747     else
748     {
749         msg_Dbg( &sys.demuxer, "|   + LoadSeekHeadItem Unknown (%s)", typeid(*el).name() );
750     }
751     delete el;
752
753     es.I_O().setFilePointer( i_sav_position, seek_beginning );
754     return true;
755 }
756
757 struct spoint
758 {
759     spoint(unsigned int tk, mtime_t date, int64_t pos, int64_t cpos):
760         i_track(tk),i_date(date), i_seek_pos(pos),
761         i_cluster_pos(cpos), p_next(NULL){}
762     unsigned int     i_track;
763     mtime_t i_date;
764     int64_t i_seek_pos;
765     int64_t i_cluster_pos;
766     spoint * p_next;
767 };
768
769 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_global_position )
770 {
771     KaxBlock    *block;
772     KaxSimpleBlock *simpleblock;
773     int64_t     i_block_duration;
774     size_t      i_track;
775     int64_t     i_seek_position = i_start_pos;
776     int64_t     i_seek_time = i_start_time;
777     mtime_t     i_pts = 0;
778     spoint *p_first = NULL;
779     spoint *p_last = NULL;
780     int i_cat;
781     bool b_has_key = false;
782
783     for( size_t i = 0; i < tracks.size(); i++)
784         tracks[i]->i_last_dts = VLC_TS_INVALID;
785
786     if( i_global_position >= 0 )
787     {
788         /* Special case for seeking in files with no cues */
789         EbmlElement *el = NULL;
790
791         /* Start from the last known index instead of the beginning eachtime */
792         if( i_index == 0)
793             es.I_O().setFilePointer( i_start_pos, seek_beginning );
794         else
795             es.I_O().setFilePointer( p_indexes[ i_index - 1 ].i_position,
796                                      seek_beginning );
797         delete ep;
798         ep = new EbmlParser( &es, segment, &sys.demuxer );
799         cluster = NULL;
800
801         while( ( el = ep->Get() ) != NULL )
802         {
803             if( MKV_IS_ID( el, KaxCluster ) )
804             {
805                 cluster = (KaxCluster *)el;
806                 i_cluster_pos = cluster->GetElementPosition();
807                 if( i_index == 0 ||
808                     ( i_index > 0 &&
809                       p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
810                 {
811                     ParseCluster(false);
812                     IndexAppendCluster( cluster );
813                 }
814                 if( es.I_O().getFilePointer() >= (unsigned) i_global_position )
815                     break;
816             }
817         }
818     }
819
820     /* Don't try complex seek if we seek to 0 */
821     if( i_date == 0 && i_time_offset == 0 )
822     {
823         es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 );
824         es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME,
825                         INT64_C(0) );
826         es.I_O().setFilePointer( i_start_pos );
827
828         delete ep;
829         ep = new EbmlParser( &es, segment, &sys.demuxer );
830         cluster = NULL;
831         sys.i_start_pts = 0;
832         sys.i_pts = 0;
833         sys.i_pcr = 0;
834         return;
835     }
836
837     int i_idx = 0;
838     if ( i_index > 0 )
839     {
840
841         for( ; i_idx < i_index; i_idx++ )
842             if( p_indexes[i_idx].i_time + i_time_offset > i_date )
843                 break;
844
845         if( i_idx > 0 )
846             i_idx--;
847
848         i_seek_position = p_indexes[i_idx].i_position;
849         i_seek_time = p_indexes[i_idx].i_time;
850     }
851
852     msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
853                 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
854
855     es.I_O().setFilePointer( i_seek_position, seek_beginning );
856
857     delete ep;
858     ep = new EbmlParser( &es, segment, &sys.demuxer );
859     cluster = NULL;
860
861     sys.i_start_pts = i_date;
862
863     /* now parse until key frame */
864     const int es_types[3] = { VIDEO_ES, AUDIO_ES, SPU_ES };
865     i_cat = es_types[0];
866     for( int i = 0; i < 2; i_cat = es_types[++i] )
867     {
868         for( i_track = 0; i_track < tracks.size(); i_track++ )
869         {
870             if( tracks[i_track]->fmt.i_cat == i_cat )
871             {
872                 spoint * seekpoint = new spoint(i_track, i_seek_time, i_seek_position, i_seek_position);
873                 if( unlikely( !seekpoint ) )
874                 {
875                     for( spoint * sp = p_first; sp; )
876                     {
877                         spoint * tmp = sp;
878                         sp = sp->p_next;
879                         delete tmp;
880                     }
881                     return;
882                 }
883                 if( unlikely( !p_first ) )
884                 {
885                     p_first = seekpoint;
886                     p_last = seekpoint;
887                 }
888                 else
889                 {
890                     p_last->p_next = seekpoint;
891                     p_last = seekpoint;
892                 }
893             }
894         }
895         if( likely( p_first ) )
896             break;
897     }
898     /*Neither video nor audio track... no seek further*/
899     if( unlikely( !p_first ) )
900     {
901         es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, i_date );
902         es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
903         return;
904     }
905
906     for(;;)
907     {
908         do
909         {
910             bool b_key_picture;
911             bool b_discardable_picture;
912             if( BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
913             {
914                 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
915                 return;
916             }
917
918             /* check if block's track is in our list */
919             for( i_track = 0; i_track < tracks.size(); i_track++ )
920             {
921                 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
922                     (block && tracks[i_track]->i_number == block->TrackNum()) )
923                     break;
924             }
925
926             if( simpleblock )
927                 i_pts = sys.i_chapter_time + simpleblock->GlobalTimecode() / (mtime_t) 1000;
928             else
929                 i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
930             if( i_track < tracks.size() )
931             {
932                 if( tracks[i_track]->fmt.i_cat == i_cat && b_key_picture )
933                 {
934                     /* get the seekpoint */
935                     spoint * sp;
936                     for( sp =  p_first; sp; sp = sp->p_next )
937                         if( sp->i_track == i_track )
938                             break;
939
940                     sp->i_date = i_pts;
941                     if( simpleblock )
942                         sp->i_seek_pos = simpleblock->GetElementPosition();
943                     else
944                         sp->i_seek_pos = i_block_pos;
945                     sp->i_cluster_pos = i_cluster_pos;
946                     b_has_key = true;
947                 }
948             }
949
950             delete block;
951         } while( i_pts < i_date );
952         if( b_has_key || !i_idx )
953             break;
954
955         /* No key picture was found in the cluster seek to previous seekpoint */
956         i_date = i_time_offset + p_indexes[i_idx].i_time;
957         i_idx--;
958         i_pts = 0;
959         es.I_O().setFilePointer( p_indexes[i_idx].i_position );
960         delete ep;
961         ep = new EbmlParser( &es, segment, &sys.demuxer );
962         cluster = NULL;
963     }
964
965     /* rewind to the last I img */
966     spoint * p_min;
967     for( p_min  = p_first, p_last = p_first; p_last; p_last = p_last->p_next )
968         if( p_last->i_date < p_min->i_date )
969             p_min = p_last;
970
971     sys.i_pcr = sys.i_pts = p_min->i_date;
972     es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, VLC_TS_0 + sys.i_pcr );
973     es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
974     cluster = (KaxCluster *) ep->UnGet( p_min->i_seek_pos, p_min->i_cluster_pos );
975
976     /* hack use BlockGet to get the cluster then goto the wanted block */
977     if ( !cluster )
978     {
979         bool b_key_picture;
980         bool b_discardable_picture;
981         BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration );
982         delete block;
983         cluster = (KaxCluster *) ep->UnGet( p_min->i_seek_pos, p_min->i_cluster_pos );
984     }
985
986     while( p_first )
987     {
988         p_min = p_first;
989         p_first = p_first->p_next;
990         delete p_min;
991     }
992 }
993
994 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
995                                              const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
996 {
997     size_t i_track;
998     for( i_track = 0; i_track < tracks.size(); i_track++ )
999     {
1000         const mkv_track_t *tk = tracks[i_track];
1001
1002         if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
1003             ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
1004         {
1005             break;
1006         }
1007     }
1008
1009     if( i_track >= tracks.size() )
1010         return VLC_EGENERIC;
1011
1012     if( pi_track )
1013         *pi_track = i_track;
1014     return VLC_SUCCESS;
1015 }
1016
1017 bool matroska_segment_c::Select( mtime_t i_start_time )
1018 {
1019     /* add all es */
1020     msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
1021
1022     bool b_has_default_video = false;
1023     bool b_has_default_audio = false;
1024     /* check for default */
1025     for(size_t i_track = 0; i_track < tracks.size(); i_track++)
1026     {
1027         mkv_track_t *p_tk = tracks[i_track];
1028         es_format_t *p_fmt = &p_tk->fmt;
1029         if( p_fmt->i_cat == VIDEO_ES )
1030             b_has_default_video |=
1031                 p_tk->b_enabled && ( p_tk->b_default || p_tk->b_forced );
1032         else if( p_fmt->i_cat == AUDIO_ES )
1033             b_has_default_audio |=
1034                 p_tk->b_enabled && ( p_tk->b_default || p_tk->b_forced );
1035     }
1036
1037     for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1038     {
1039         mkv_track_t *p_tk = tracks[i_track];
1040         es_format_t *p_fmt = &p_tk->fmt;
1041
1042         if( unlikely( p_fmt->i_cat == UNKNOWN_ES || !p_tk->psz_codec ) )
1043         {
1044             msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, p_tk->i_number );
1045             p_tk->p_es = NULL;
1046             continue;
1047         }
1048         else if( unlikely( !b_has_default_video && p_fmt->i_cat == VIDEO_ES ) )
1049         {
1050             p_tk->b_default = true;
1051             b_has_default_video = true;
1052         }
1053         else if( unlikely( !b_has_default_audio &&  p_fmt->i_cat == AUDIO_ES ) )
1054         {
1055             p_tk->b_default = true;
1056             b_has_default_audio = true;
1057         }
1058         if( unlikely( !p_tk->b_enabled ) )
1059             p_tk->fmt.i_priority = -2;
1060         else if( p_tk->b_forced )
1061             p_tk->fmt.i_priority = 2;
1062         else if( p_tk->b_default )
1063             p_tk->fmt.i_priority = 1;
1064         else
1065             p_tk->fmt.i_priority = 0;
1066
1067         /* Avoid multivideo tracks when unnecessary */
1068         if( p_tk->fmt.i_cat == VIDEO_ES )
1069             p_tk->fmt.i_priority--;
1070
1071         p_tk->p_es = es_out_Add( sys.demuxer.out, &p_tk->fmt );
1072
1073         /* Turn on a subtitles track if it has been flagged as default -
1074          * but only do this if no subtitles track has already been engaged,
1075          * either by an earlier 'default track' (??) or by default
1076          * language choice behaviour.
1077          */
1078         if( p_tk->b_default )
1079         {
1080             es_out_Control( sys.demuxer.out,
1081                             ES_OUT_SET_ES_DEFAULT,
1082                             p_tk->p_es );
1083         }
1084     }
1085     es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
1086
1087     sys.i_start_pts = i_start_time;
1088     // reset the stream reading to the first cluster of the segment used
1089     es.I_O().setFilePointer( i_start_pos );
1090
1091     delete ep;
1092     ep = new EbmlParser( &es, segment, &sys.demuxer );
1093
1094     return true;
1095 }
1096
1097 void matroska_segment_c::UnSelect( )
1098 {
1099     sys.p_ev->ResetPci();
1100     for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1101     {
1102         if ( tracks[i_track]->p_es != NULL )
1103         {
1104 //            es_format_Clean( &tracks[i_track]->fmt );
1105             es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
1106             tracks[i_track]->p_es = NULL;
1107         }
1108     }
1109     delete ep;
1110     ep = NULL;
1111 }
1112
1113 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, bool *pb_key_picture, bool *pb_discardable_picture, int64_t *pi_duration )
1114 {
1115     pp_simpleblock = NULL;
1116     pp_block = NULL;
1117
1118     *pb_key_picture         = true;
1119     *pb_discardable_picture = false;
1120     size_t i_tk;
1121
1122     for( ;; )
1123     {
1124         EbmlElement *el = NULL;
1125         int         i_level;
1126
1127         if ( ep == NULL )
1128             return VLC_EGENERIC;
1129
1130         if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1131         {
1132             /* Check blocks validity to protect againts broken files */
1133             if( BlockFindTrackIndex( &i_tk, pp_block , pp_simpleblock ) )
1134             {
1135                 delete pp_block;
1136                 pp_simpleblock = NULL;
1137                 pp_block = NULL;
1138                 continue;
1139             }
1140             if( pp_simpleblock != NULL )
1141             {
1142                 *pb_key_picture         = pp_simpleblock->IsKeyframe();
1143                 *pb_discardable_picture = pp_simpleblock->IsDiscardable();
1144             }
1145             /* We have block group let's check if the picture is a keyframe */
1146             else if( *pb_key_picture )
1147             {
1148                 switch(tracks[i_tk]->fmt.i_codec)
1149                 {
1150                     case VLC_CODEC_THEORA:
1151                     {
1152                         DataBuffer *p_data = &pp_block->GetBuffer(0);
1153                         size_t sz = p_data->Size();
1154                         const uint8_t * p_buff = p_data->Buffer();
1155                         /* if the second bit of a Theora frame is 1 
1156                            it's not a keyframe */
1157                         if( sz && p_buff )
1158                         {
1159                             if( p_buff[0] & 0x40 )
1160                                 *pb_key_picture = false;
1161                         }
1162                         else
1163                             *pb_key_picture = false;
1164                         break;
1165                     }
1166                 }
1167             }
1168
1169             /* update the index */
1170 #define idx p_indexes[i_index - 1]
1171             if( i_index > 0 && idx.i_time == -1 )
1172             {
1173                 if ( pp_simpleblock != NULL )
1174                     idx.i_time        = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1175                 else
1176                     idx.i_time        = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1177                 idx.b_key         = *pb_key_picture;
1178             }
1179 #undef idx
1180             return VLC_SUCCESS;
1181         }
1182
1183         i_level = ep->GetLevel();
1184
1185         if( el == NULL )
1186         {
1187             if( i_level > 1 )
1188             {
1189                 ep->Up();
1190                 continue;
1191             }
1192             msg_Warn( &sys.demuxer, "EOF" );
1193             return VLC_EGENERIC;
1194         }
1195
1196         /* Verify that we are still inside our cluster
1197          * It can happens whith broken files and when seeking
1198          * without index */
1199         if( i_level > 1 )
1200         {
1201             if( cluster && !ep->IsTopPresent( cluster ) )
1202             {
1203                 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1204                 cluster = NULL;
1205             }
1206             if( !cluster )
1207                 continue;
1208         }
1209
1210         /* do parsing */
1211         switch ( i_level )
1212         {
1213         case 1:
1214             if( MKV_IS_ID( el, KaxCluster ) )
1215             {
1216                 cluster = (KaxCluster*)el;
1217                 i_cluster_pos = cluster->GetElementPosition();
1218
1219                 // reset silent tracks
1220                 for (size_t i=0; i<tracks.size(); i++)
1221                 {
1222                     tracks[i]->b_silent = false;
1223                 }
1224
1225                 ep->Down();
1226             }
1227             else if( MKV_IS_ID( el, KaxCues ) )
1228             {
1229                 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1230                 return VLC_EGENERIC;
1231             }
1232             else
1233             {
1234                 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1235             }
1236             break;
1237         case 2:
1238             if( MKV_IS_ID( el, KaxClusterTimecode ) )
1239             {
1240                 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1241
1242                 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1243                 cluster->InitTimecode( uint64( ctc ), i_timescale );
1244
1245                 /* add it to the index */
1246                 if( i_index == 0 ||
1247                     ( i_index > 0 &&
1248                       p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1249                     IndexAppendCluster( cluster );
1250             }
1251             else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1252             {
1253                 ep->Down();
1254             }
1255             else if( MKV_IS_ID( el, KaxBlockGroup ) )
1256             {
1257                 i_block_pos = el->GetElementPosition();
1258                 ep->Down();
1259             }
1260             else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1261             {
1262                 pp_simpleblock = (KaxSimpleBlock*)el;
1263
1264                 pp_simpleblock->ReadData( es.I_O() );
1265                 pp_simpleblock->SetParent( *cluster );
1266             }
1267             break;
1268         case 3:
1269             if( MKV_IS_ID( el, KaxBlock ) )
1270             {
1271                 pp_block = (KaxBlock*)el;
1272
1273                 pp_block->ReadData( es.I_O() );
1274                 pp_block->SetParent( *cluster );
1275
1276                 ep->Keep();
1277             }
1278             else if( MKV_IS_ID( el, KaxBlockDuration ) )
1279             {
1280                 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1281
1282                 dur.ReadData( es.I_O() );
1283                 *pi_duration = uint64( dur );
1284             }
1285             else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1286             {
1287                 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1288
1289                 ref.ReadData( es.I_O() );
1290
1291                 if( *pb_key_picture )
1292                     *pb_key_picture = false;
1293                 else if( int64( ref ) > 0 )
1294                     *pb_discardable_picture = true;
1295             }
1296             else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1297             {
1298                 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1299                 track_num.ReadData( es.I_O() );
1300                 // find the track
1301                 for (size_t i=0; i<tracks.size(); i++)
1302                 {
1303                     if ( tracks[i]->i_number == uint32(track_num))
1304                     {
1305                         tracks[i]->b_silent = true;
1306                         break;
1307                     }
1308                 }
1309             }
1310             break;
1311         default:
1312             msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1313             return VLC_EGENERIC;
1314         }
1315     }
1316 }
1317
1318 SimpleTag::~SimpleTag()
1319 {
1320     free(psz_tag_name);
1321     free(psz_lang);
1322     free(p_value);
1323     for(size_t i = 0; i < sub_tags.size(); i++)
1324         delete sub_tags[i];
1325 }
1326
1327 Tag::~Tag()
1328 {
1329     for(size_t i = 0; i < simple_tags.size(); i++)
1330         delete simple_tags[i];
1331 }