1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mkv.cpp,v 1.23 2003/08/25 20:47:41 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
32 # include <time.h> /* time() */
35 #include <vlc/input.h>
37 #include <codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
48 /* libebml and matroska */
49 #include "ebml/EbmlHead.h"
50 #include "ebml/EbmlSubHead.h"
51 #include "ebml/EbmlStream.h"
52 #include "ebml/EbmlContexts.h"
53 #include "ebml/EbmlVersion.h"
54 #include "ebml/EbmlVoid.h"
56 #include "matroska/FileKax.h"
57 #ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
58 #include "matroska/KaxAttachments.h"
60 #include "matroska/KaxAttachements.h"
62 #include "matroska/KaxBlock.h"
63 #include "matroska/KaxBlockData.h"
64 #include "matroska/KaxChapters.h"
65 #include "matroska/KaxCluster.h"
66 #include "matroska/KaxClusterData.h"
67 #include "matroska/KaxContexts.h"
68 #include "matroska/KaxCues.h"
69 #include "matroska/KaxCuesData.h"
70 #include "matroska/KaxInfo.h"
71 #include "matroska/KaxInfoData.h"
72 #include "matroska/KaxSeekHead.h"
73 #include "matroska/KaxSegment.h"
74 #include "matroska/KaxTag.h"
75 #include "matroska/KaxTags.h"
76 #include "matroska/KaxTagMulti.h"
77 #include "matroska/KaxTracks.h"
78 #include "matroska/KaxTrackAudio.h"
79 #include "matroska/KaxTrackVideo.h"
80 #include "matroska/KaxTrackEntryData.h"
82 #include "ebml/StdIOCallback.h"
84 using namespace LIBMATROSKA_NAMESPACE;
88 /*****************************************************************************
90 *****************************************************************************/
91 static int Activate ( vlc_object_t * );
92 static void Deactivate( vlc_object_t * );
93 static int Demux ( input_thread_t * );
95 /*****************************************************************************
97 *****************************************************************************/
99 add_category_hint( N_("mkv-demuxer"), NULL, VLC_TRUE );
100 add_bool( "mkv-seek-percent", 1, NULL,
101 N_("Seek based on percent not time"),
102 N_("Seek based on percent not time"), VLC_TRUE );
104 set_description( _("mka/mkv stream demuxer" ) );
105 set_capability( "demux", 50 );
106 set_callbacks( Activate, Deactivate );
107 add_shortcut( "mka" );
108 add_shortcut( "mkv" );
112 /*****************************************************************************
114 *****************************************************************************/
115 class vlc_stream_io_callback: public IOCallback
118 input_thread_t *p_input;
122 vlc_stream_io_callback( input_thread_t * );
124 virtual uint32_t read ( void *p_buffer, size_t i_size);
125 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
126 virtual size_t write ( const void *p_buffer, size_t i_size);
127 virtual uint64_t getFilePointer ( void );
128 virtual void close ( void );
131 /*****************************************************************************
133 *****************************************************************************/
137 EbmlParser( EbmlStream *es, EbmlElement *el_start );
142 EbmlElement *Get( void );
145 int GetLevel( void );
150 EbmlElement *m_el[6];
159 /*****************************************************************************
160 * Some functions to manipulate memory
161 *****************************************************************************/
162 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
163 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
165 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
168 /*****************************************************************************
169 * definitions of structures and functions used by this plugins
170 *****************************************************************************/
174 vlc_bool_t b_default;
175 vlc_bool_t b_enabled;
179 uint8_t *p_extra_data;
184 vlc_fourcc_t i_codec;
186 uint64_t i_default_duration;
187 float f_timecodescale;
192 int i_display_height;
201 es_descriptor_t *p_es;
206 /* data to be send first */
208 uint8_t *p_data_init;
210 /* hack : it's for seek */
211 vlc_bool_t b_search_keyframe;
215 char *psz_codec_name;
216 char *psz_codec_settings;
217 char *psz_codec_info_url;
218 char *psz_codec_download_url;
235 vlc_stream_io_callback *in;
240 uint64_t i_timescale;
242 /* duration of the segment */
250 int64_t i_cues_position;
251 int64_t i_chapters_position;
252 int64_t i_tags_position;
266 char *psz_muxing_application;
267 char *psz_writing_application;
268 char *psz_segment_filename;
273 #define MKVD_TIMECODESCALE 1000000
275 static void IndexAppendCluster ( input_thread_t *p_input, KaxCluster *cluster );
276 static char *UTF8ToStr ( const UTFstring &u );
277 static void LoadCues ( input_thread_t *);
278 static void InformationsCreate ( input_thread_t *p_input );
280 static char *LanguageGetName ( const char *psz_code );
282 /*****************************************************************************
283 * Activate: initializes matroska demux structures
284 *****************************************************************************/
285 static int Activate( vlc_object_t * p_this )
287 input_thread_t *p_input = (input_thread_t *)p_this;
292 vlc_bool_t b_audio_selected;
293 int i_spu_channel, i_audio_channel;
295 EbmlElement *el = NULL, *el1 = NULL, *el2 = NULL, *el3 = NULL, *el4 = NULL;
298 /* Initialize access plug-in structures. */
299 if( p_input->i_mtu == 0 )
302 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
305 /* Set the demux function */
306 p_input->pf_demux = Demux;
308 /* peek the begining */
309 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
311 msg_Warn( p_input, "cannot peek" );
315 /* is a valid file */
316 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 || p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
318 msg_Warn( p_input, "matroska module discarded "
319 "(invalid header 0x%.2x%.2x%.2x%.2x)",
320 p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
324 p_input->p_demux_data = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
325 memset( p_sys, 0, sizeof( demux_sys_t ) );
327 p_sys->in = new vlc_stream_io_callback( p_input );
328 p_sys->es = new EbmlStream( *p_sys->in );
329 p_sys->f_duration = -1;
330 p_sys->i_timescale = MKVD_TIMECODESCALE;
332 p_sys->track = (mkv_track_t*)malloc( sizeof( mkv_track_t ) );
334 p_sys->i_cues_position = -1;
335 p_sys->i_chapters_position = -1;
336 p_sys->i_tags_position = -1;
338 p_sys->b_cues = VLC_FALSE;
340 p_sys->i_index_max = 1024;
341 p_sys->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * p_sys->i_index_max );
343 p_sys->psz_muxing_application = NULL;
344 p_sys->psz_writing_application = NULL;
345 p_sys->psz_segment_filename = NULL;
346 p_sys->psz_title = NULL;
347 p_sys->psz_date_utc = NULL;;
349 if( p_sys->es == NULL )
351 msg_Err( p_input, "failed to create EbmlStream" );
356 /* Find the EbmlHead element */
357 el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
360 msg_Err( p_input, "cannot find EbmlHead" );
363 msg_Dbg( p_input, "EbmlHead" );
365 el->SkipData( *p_sys->es, el->Generic().Context );
369 el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
372 msg_Err( p_input, "cannot find KaxSegment" );
375 msg_Dbg( p_input, "+ Segment" );
376 p_sys->segment = (KaxSegment*)el;
377 p_sys->cluster = NULL;
379 p_sys->ep = new EbmlParser( p_sys->es, el );
381 while( ( el1 = p_sys->ep->Get() ) != NULL )
383 if( EbmlId( *el1 ) == KaxInfo::ClassInfos.GlobalId )
385 msg_Dbg( p_input, "| + Informations" );
388 while( ( el2 = p_sys->ep->Get() ) != NULL )
390 if( EbmlId( *el2 ) == KaxTimecodeScale::ClassInfos.GlobalId )
392 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)el2;
394 tcs.ReadData( p_sys->es->I_O() );
395 p_sys->i_timescale = uint64(tcs);
397 msg_Dbg( p_input, "| | + TimecodeScale="I64Fd,
398 p_sys->i_timescale );
400 else if( EbmlId( *el2 ) == KaxDuration::ClassInfos.GlobalId )
402 KaxDuration &dur = *(KaxDuration*)el2;
404 dur.ReadData( p_sys->es->I_O() );
405 p_sys->f_duration = float(dur);
407 msg_Dbg( p_input, "| | + Duration=%f", p_sys->f_duration );
409 else if( EbmlId( *el2 ) == KaxMuxingApp::ClassInfos.GlobalId )
411 KaxMuxingApp &mapp = *(KaxMuxingApp*)el2;
413 mapp.ReadData( p_sys->es->I_O() );
415 p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
417 msg_Dbg( p_input, "| | + Muxing Application=%s", p_sys->psz_muxing_application );
419 else if( EbmlId( *el2 ) == KaxWritingApp::ClassInfos.GlobalId )
421 KaxWritingApp &wapp = *(KaxWritingApp*)el2;
423 wapp.ReadData( p_sys->es->I_O() );
425 p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
427 msg_Dbg( p_input, "| | + Wrinting Application=%s", p_sys->psz_writing_application );
429 else if( EbmlId( *el2 ) == KaxSegmentFilename::ClassInfos.GlobalId )
431 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)el2;
433 sfn.ReadData( p_sys->es->I_O() );
435 p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
437 msg_Dbg( p_input, "| | + Segment Filename=%s", p_sys->psz_segment_filename );
439 else if( EbmlId( *el2 ) == KaxTitle::ClassInfos.GlobalId )
441 KaxTitle &title = *(KaxTitle*)el2;
443 title.ReadData( p_sys->es->I_O() );
445 p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
447 msg_Dbg( p_input, "| | + Title=%s", p_sys->psz_title );
450 else if( EbmlId( *el2 ) == KaxDateUTC::ClassInfos.GlobalId )
452 KaxDateUTC &date = *(KaxDateUTC*)el2;
457 date.ReadData( p_sys->es->I_O() );
459 i_date = date.GetEpochDate();
460 memset( buffer, 0, 256 );
461 if( gmtime_r( &i_date, &tmres ) &&
462 asctime_r( &tmres, buffer ) )
464 buffer[strlen( buffer)-1]= '\0';
465 p_sys->psz_date_utc = strdup( buffer );
466 msg_Dbg( p_input, "| | + Date=%s", p_sys->psz_date_utc );
472 msg_Dbg( p_input, "| | + Unknown (%s)", typeid(*el2).name() );
477 else if( EbmlId( *el1 ) == KaxTracks::ClassInfos.GlobalId )
479 msg_Dbg( p_input, "| + Tracks" );
482 while( ( el2 = p_sys->ep->Get() ) != NULL )
484 if( EbmlId( *el2 ) == KaxTrackEntry::ClassInfos.GlobalId )
486 msg_Dbg( p_input, "| | + Track Entry" );
489 p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
490 #define tk p_sys->track[p_sys->i_track - 1]
491 memset( &tk, 0, sizeof( mkv_track_t ) );
492 tk.i_cat = UNKNOWN_ES;
493 tk.b_default = VLC_TRUE;
494 tk.b_enabled = VLC_TRUE;
495 tk.i_number = p_sys->i_track - 1;
497 tk.p_extra_data = NULL;
500 tk.psz_language = NULL;
501 tk.i_default_duration = 0;
502 tk.f_timecodescale = 1.0;
504 tk.b_inited = VLC_FALSE;
506 tk.p_data_init = NULL;
509 tk.psz_codec_name = NULL;
510 tk.psz_codec_settings = NULL;
511 tk.psz_codec_info_url = NULL;
512 tk.psz_codec_download_url = NULL;
516 while( ( el3 = p_sys->ep->Get() ) != NULL )
518 if( EbmlId( *el3 ) == KaxTrackNumber::ClassInfos.GlobalId )
520 KaxTrackNumber &tnum = *(KaxTrackNumber*)el3;
521 tnum.ReadData( p_sys->es->I_O() );
523 tk.i_number = uint32( tnum );
524 msg_Dbg( p_input, "| | | + Track Number=%u", uint32( tnum ) );
526 else if( EbmlId( *el3 ) == KaxTrackUID::ClassInfos.GlobalId )
528 KaxTrackUID &tuid = *(KaxTrackUID*)el3;
529 tuid.ReadData( p_sys->es->I_O() );
531 msg_Dbg( p_input, "| | | + Track UID=%u", uint32( tuid ) );
533 else if( EbmlId( *el3 ) == KaxTrackType::ClassInfos.GlobalId )
536 KaxTrackType &ttype = *(KaxTrackType*)el3;
537 ttype.ReadData( p_sys->es->I_O() );
538 switch( uint8(ttype) )
549 psz_type = "subtitle";
553 psz_type = "unknown";
554 tk.i_cat = UNKNOWN_ES;
558 msg_Dbg( p_input, "| | | + Track Type=%s",
561 else if( EbmlId( *el3 ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
563 KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)el3;
564 fenb.ReadData( p_sys->es->I_O() );
566 tk.b_enabled = uint32( fenb );
567 msg_Dbg( p_input, "| | | + Track Enabled=%u",
570 else if( EbmlId( *el3 ) == KaxTrackFlagDefault::ClassInfos.GlobalId )
572 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)el3;
573 fdef.ReadData( p_sys->es->I_O() );
575 tk.b_default = uint32( fdef );
576 msg_Dbg( p_input, "| | | + Track Default=%u",
579 else if( EbmlId( *el3 ) == KaxTrackFlagLacing::ClassInfos.GlobalId )
581 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)el3;
582 lac.ReadData( p_sys->es->I_O() );
584 msg_Dbg( p_input, "| | | + Track Lacing=%d",
587 else if( EbmlId( *el3 ) == KaxTrackMinCache::ClassInfos.GlobalId )
589 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)el3;
590 cmin.ReadData( p_sys->es->I_O() );
592 msg_Dbg( p_input, "| | | + Track MinCache=%d",
595 else if( EbmlId( *el3 ) == KaxTrackMaxCache::ClassInfos.GlobalId )
597 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)el3;
598 cmax.ReadData( p_sys->es->I_O() );
600 msg_Dbg( p_input, "| | | + Track MaxCache=%d",
603 else if( EbmlId( *el3 ) == KaxTrackDefaultDuration::ClassInfos.GlobalId )
605 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)el3;
606 defd.ReadData( p_sys->es->I_O() );
608 tk.i_default_duration = uint64(defd);
609 msg_Dbg( p_input, "| | | + Track Default Duration="I64Fd, uint64(defd) );
611 else if( EbmlId( *el3 ) == KaxTrackTimecodeScale::ClassInfos.GlobalId )
613 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)el3;
614 ttcs.ReadData( p_sys->es->I_O() );
616 tk.f_timecodescale = float( ttcs );
617 msg_Dbg( p_input, "| | | + Track TimeCodeScale=%f", tk.f_timecodescale );
619 else if( EbmlId( *el3 ) == KaxTrackName::ClassInfos.GlobalId )
621 KaxTrackName &tname = *(KaxTrackName*)el3;
622 tname.ReadData( p_sys->es->I_O() );
624 tk.psz_name = UTF8ToStr( UTFstring( tname ) );
625 msg_Dbg( p_input, "| | | + Track Name=%s",
628 else if( EbmlId( *el3 ) == KaxTrackLanguage::ClassInfos.GlobalId )
630 KaxTrackLanguage &lang = *(KaxTrackLanguage*)el3;
631 lang.ReadData( p_sys->es->I_O() );
634 LanguageGetName( string( lang ).c_str() );
636 "| | | + Track Language=`%s'(%s) ",
637 tk.psz_language, string( lang ).c_str() );
639 else if( EbmlId( *el3 ) == KaxCodecID::ClassInfos.GlobalId )
641 KaxCodecID &codecid = *(KaxCodecID*)el3;
642 codecid.ReadData( p_sys->es->I_O() );
644 tk.psz_codec = strdup( string( codecid ).c_str() );
645 msg_Dbg( p_input, "| | | + Track CodecId=%s",
646 string( codecid ).c_str() );
648 else if( EbmlId( *el3 ) == KaxCodecPrivate::ClassInfos.GlobalId )
650 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)el3;
651 cpriv.ReadData( p_sys->es->I_O() );
653 tk.i_extra_data = cpriv.GetSize();
654 if( tk.i_extra_data > 0 )
656 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
657 memcpy( tk.p_extra_data, cpriv.GetBuffer(), tk.i_extra_data );
659 msg_Dbg( p_input, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
661 else if( EbmlId( *el3 ) == KaxCodecName::ClassInfos.GlobalId )
663 KaxCodecName &cname = *(KaxCodecName*)el3;
664 cname.ReadData( p_sys->es->I_O() );
666 tk.psz_codec_name = UTF8ToStr( UTFstring( cname ) );
667 msg_Dbg( p_input, "| | | + Track Codec Name=%s", tk.psz_codec_name );
669 else if( EbmlId( *el3 ) == KaxCodecSettings::ClassInfos.GlobalId )
671 KaxCodecSettings &cset = *(KaxCodecSettings*)el3;
672 cset.ReadData( p_sys->es->I_O() );
674 tk.psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
675 msg_Dbg( p_input, "| | | + Track Codec Settings=%s", tk.psz_codec_settings );
677 else if( EbmlId( *el3 ) == KaxCodecInfoURL::ClassInfos.GlobalId )
679 KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)el3;
680 ciurl.ReadData( p_sys->es->I_O() );
682 tk.psz_codec_info_url = strdup( string( ciurl ).c_str() );
683 msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_info_url );
685 else if( EbmlId( *el3 ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
687 KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)el3;
688 cdurl.ReadData( p_sys->es->I_O() );
690 tk.psz_codec_download_url = strdup( string( cdurl ).c_str() );
691 msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_download_url );
693 else if( EbmlId( *el3 ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
695 KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)el3;
696 cdall.ReadData( p_sys->es->I_O() );
698 msg_Dbg( p_input, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
700 else if( EbmlId( *el3 ) == KaxTrackOverlay::ClassInfos.GlobalId )
702 KaxTrackOverlay &tovr = *(KaxTrackOverlay*)el3;
703 tovr.ReadData( p_sys->es->I_O() );
705 msg_Dbg( p_input, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
707 else if( EbmlId( *el3 ) == KaxTrackVideo::ClassInfos.GlobalId )
709 msg_Dbg( p_input, "| | | + Track Video" );
712 tk.i_display_width = 0;
713 tk.i_display_height = 0;
718 while( ( el4 = p_sys->ep->Get() ) != NULL )
720 if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
722 KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
723 fint.ReadData( p_sys->es->I_O() );
725 msg_Dbg( p_input, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
727 else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
729 KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
730 stereo.ReadData( p_sys->es->I_O() );
732 msg_Dbg( p_input, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
734 else if( EbmlId( *el4 ) == KaxVideoPixelWidth::ClassInfos.GlobalId )
736 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)el4;
737 vwidth.ReadData( p_sys->es->I_O() );
739 tk.i_width = uint16( vwidth );
740 msg_Dbg( p_input, "| | | | + width=%d", uint16( vwidth ) );
742 else if( EbmlId( *el4 ) == KaxVideoPixelHeight::ClassInfos.GlobalId )
744 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)el4;
745 vheight.ReadData( p_sys->es->I_O() );
747 tk.i_height = uint16( vheight );
748 msg_Dbg( p_input, "| | | | + height=%d", uint16( vheight ) );
750 else if( EbmlId( *el4 ) == KaxVideoDisplayWidth::ClassInfos.GlobalId )
752 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)el4;
753 vwidth.ReadData( p_sys->es->I_O() );
755 tk.i_display_width = uint16( vwidth );
756 msg_Dbg( p_input, "| | | | + display width=%d", uint16( vwidth ) );
758 else if( EbmlId( *el4 ) == KaxVideoDisplayHeight::ClassInfos.GlobalId )
760 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)el4;
761 vheight.ReadData( p_sys->es->I_O() );
763 tk.i_display_height = uint16( vheight );
764 msg_Dbg( p_input, "| | | | + display height=%d", uint16( vheight ) );
766 else if( EbmlId( *el4 ) == KaxVideoFrameRate::ClassInfos.GlobalId )
768 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)el4;
769 vfps.ReadData( p_sys->es->I_O() );
771 tk.f_fps = float( vfps );
772 msg_Dbg( p_input, " | | | + fps=%f", float( vfps ) );
774 else if( EbmlId( *el4 ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
776 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)el4;
777 vdmode.ReadData( p_sys->es->I_O() );
779 msg_Dbg( p_input, "| | | | + Track Video Display Unit=%s",
780 uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
782 else if( EbmlId( *el4 ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
784 KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)el4;
785 ratio.ReadData( p_sys->es->I_O() );
787 msg_Dbg( p_input, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
789 else if( EbmlId( *el4 ) == KaxVideoGamma::ClassInfos.GlobalId )
791 KaxVideoGamma &gamma = *(KaxVideoGamma*)el4;
792 gamma.ReadData( p_sys->es->I_O() );
794 msg_Dbg( p_input, " | | | + fps=%f", float( gamma ) );
798 msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() );
803 else if( EbmlId( *el3 ) == KaxTrackAudio::ClassInfos.GlobalId )
805 msg_Dbg( p_input, "| | | + Track Audio" );
808 tk.i_bitspersample = 0;
812 while( ( el4 = p_sys->ep->Get() ) != NULL )
814 if( EbmlId( *el4 ) == KaxAudioSamplingFreq::ClassInfos.GlobalId )
816 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)el4;
817 afreq.ReadData( p_sys->es->I_O() );
819 tk.i_samplerate = (int)float( afreq );
820 msg_Dbg( p_input, "| | | | + afreq=%d", tk.i_samplerate );
822 else if( EbmlId( *el4 ) == KaxAudioChannels::ClassInfos.GlobalId )
824 KaxAudioChannels &achan = *(KaxAudioChannels*)el4;
825 achan.ReadData( p_sys->es->I_O() );
827 tk.i_channels = uint8( achan );
828 msg_Dbg( p_input, "| | | | + achan=%u", uint8( achan ) );
830 else if( EbmlId( *el4 ) == KaxAudioBitDepth::ClassInfos.GlobalId )
832 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)el4;
833 abits.ReadData( p_sys->es->I_O() );
835 tk.i_bitspersample = uint8( abits );
836 msg_Dbg( p_input, "| | | | + abits=%u", uint8( abits ) );
840 msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() );
847 msg_Dbg( p_input, "| | | + Unknown (%s)", typeid(*el3).name() );
854 msg_Dbg( p_input, "| | + Unknown (%s)", typeid(*el2).name() );
860 else if( EbmlId( *el1 ) == KaxSeekHead::ClassInfos.GlobalId )
862 msg_Dbg( p_input, "| + Seek head" );
864 while( ( el = p_sys->ep->Get() ) != NULL )
866 if( EbmlId( *el ) == KaxSeek::ClassInfos.GlobalId )
868 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
871 //msg_Dbg( p_input, "| | + Seek" );
873 while( ( el = p_sys->ep->Get() ) != NULL )
875 if( EbmlId( *el ) == KaxSeekID::ClassInfos.GlobalId )
877 KaxSeekID &sid = *(KaxSeekID*)el;
879 sid.ReadData( p_sys->es->I_O() );
881 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
883 else if( EbmlId( *el ) == KaxSeekPosition::ClassInfos.GlobalId )
885 KaxSeekPosition &spos = *(KaxSeekPosition*)el;
887 spos.ReadData( p_sys->es->I_O() );
889 i_pos = uint64( spos );
893 msg_Dbg( p_input, "| | | + Unknown (%s)", typeid(*el).name() );
900 if( id == KaxCues::ClassInfos.GlobalId )
902 msg_Dbg( p_input, "| | | = cues at "I64Fd, i_pos );
903 p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
905 else if( id == KaxChapters::ClassInfos.GlobalId )
907 msg_Dbg( p_input, "| | | = chapters at "I64Fd, i_pos );
908 p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
910 else if( id == KaxTags::ClassInfos.GlobalId )
912 msg_Dbg( p_input, "| | | = tags at "I64Fd, i_pos );
913 p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
920 msg_Dbg( p_input, "| | + Unknown (%s)", typeid(*el).name() );
925 else if( EbmlId( *el1 ) == KaxCues::ClassInfos.GlobalId )
927 msg_Dbg( p_input, "| + Cues" );
929 else if( EbmlId( *el1 ) == KaxCluster::ClassInfos.GlobalId )
931 msg_Dbg( p_input, "| + Cluster" );
933 p_sys->cluster = (KaxCluster*)el1;
936 /* stop parsing the stream */
939 #ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
940 else if( EbmlId( *el1 ) == KaxAttachments::ClassInfos.GlobalId )
942 else if( EbmlId( *el1 ) == KaxAttachements::ClassInfos.GlobalId )
945 msg_Dbg( p_input, "| + Attachments FIXME TODO (but probably never supported)" );
947 else if( EbmlId( *el1 ) == KaxChapters::ClassInfos.GlobalId )
949 msg_Dbg( p_input, "| + Chapters FIXME TODO" );
951 else if( EbmlId( *el1 ) == KaxTag::ClassInfos.GlobalId )
953 msg_Dbg( p_input, "| + Tags FIXME TODO" );
957 msg_Dbg( p_input, "| + Unknown (%s)", typeid(*el1).name() );
961 if( p_sys->cluster == NULL )
963 msg_Err( p_input, "cannot find any cluster, damaged file ?" );
967 if( p_sys->i_chapters_position >= 0 )
969 msg_Warn( p_input, "chapters unsupported" );
972 /* *** Load the cue if found *** */
973 if( p_sys->i_cues_position >= 0 && p_input->stream.b_seekable )
978 if( !p_sys->b_cues || p_sys->i_index <= 0 )
980 msg_Warn( p_input, "no cues/empty cues found -> seek won't be precise" );
982 IndexAppendCluster( p_input, p_sys->cluster );
984 p_sys->b_cues = VLC_FALSE;
987 /* Create one program */
988 vlc_mutex_lock( &p_input->stream.stream_lock );
989 if( input_InitStream( p_input, 0 ) == -1)
991 vlc_mutex_unlock( &p_input->stream.stream_lock );
992 msg_Err( p_input, "cannot init stream" );
995 if( input_AddProgram( p_input, 0, 0) == NULL )
997 vlc_mutex_unlock( &p_input->stream.stream_lock );
998 msg_Err( p_input, "cannot add program" );
1001 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1002 if( p_sys->f_duration > 1001.0 )
1004 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000.0 );
1005 p_input->stream.i_mux_rate = p_input->stream.p_selected_area->i_size / 50 / i_duration;
1009 p_input->stream.i_mux_rate = 0;
1011 vlc_mutex_unlock( &p_input->stream.stream_lock );
1014 msg_Dbg( p_input, "found %d es", p_sys->i_track );
1015 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1017 #define tk p_sys->track[i_track]
1018 if( tk.i_cat == UNKNOWN_ES )
1020 msg_Warn( p_input, "invalid track[%d, n=%d]", i_track, tk.i_number );
1024 tk.p_es = input_AddES( p_input,
1025 p_input->stream.p_selected_program,
1028 tk.psz_language, 0 );
1029 if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) )
1031 if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
1033 msg_Err( p_input, "missing/invalid BITMAPINFOHEADER" );
1034 tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1038 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
1040 p_bih->biSize = GetDWLE( &p_bih->biSize );
1041 p_bih->biWidth = GetDWLE( &p_bih->biWidth );
1042 p_bih->biHeight = GetDWLE( &p_bih->biHeight );
1043 p_bih->biPlanes = GetWLE( &p_bih->biPlanes );
1044 p_bih->biBitCount = GetWLE( &p_bih->biBitCount );
1045 p_bih->biCompression = GetFOURCC( &p_bih->biCompression );
1046 p_bih->biSizeImage = GetDWLE( &p_bih->biSizeImage );
1047 p_bih->biXPelsPerMeter = GetDWLE( &p_bih->biXPelsPerMeter );
1048 p_bih->biYPelsPerMeter = GetDWLE( &p_bih->biYPelsPerMeter );
1049 p_bih->biClrUsed = GetDWLE( &p_bih->biClrUsed );
1050 p_bih->biClrImportant = GetDWLE( &p_bih->biClrImportant );
1053 tk.i_codec = p_bih->biCompression;
1054 tk.p_es->p_bitmapinfoheader = p_bih;
1057 else if( !strcmp( tk.psz_codec, "V_MPEG1" ) ||
1058 !strcmp( tk.psz_codec, "V_MPEG2" ) )
1060 tk.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1062 else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) )
1064 BITMAPINFOHEADER *p_bih;
1066 tk.i_extra_data = sizeof( BITMAPINFOHEADER );
1067 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
1069 p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
1070 memset( p_bih, 0, sizeof( BITMAPINFOHEADER ) );
1071 p_bih->biSize = sizeof( BITMAPINFOHEADER );
1072 p_bih->biWidth = tk.i_width;
1073 p_bih->biHeight= tk.i_height;
1075 if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) )
1077 tk.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1081 tk.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1084 else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
1086 if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1088 msg_Err( p_input, "missing/invalid WAVEFORMATEX" );
1089 tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1093 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;
1095 p_wf->wFormatTag = GetWLE( &p_wf->wFormatTag );
1096 p_wf->nChannels = GetWLE( &p_wf->nChannels );
1097 p_wf->nSamplesPerSec = GetDWLE( &p_wf->nSamplesPerSec );
1098 p_wf->nAvgBytesPerSec = GetDWLE( &p_wf->nAvgBytesPerSec );
1099 p_wf->nBlockAlign = GetWLE( &p_wf->nBlockAlign );
1100 p_wf->wBitsPerSample = GetWLE( &p_wf->wBitsPerSample );
1101 p_wf->cbSize = GetWLE( &p_wf->cbSize );
1103 wf_tag_to_fourcc( p_wf->wFormatTag, &tk.i_codec, NULL );
1104 tk.p_es->p_waveformatex = p_wf;
1107 else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
1108 !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
1109 !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
1111 tk.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1113 else if( !strcmp( tk.psz_codec, "A_AC3" ) )
1115 tk.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1117 else if( !strcmp( tk.psz_codec, "A_DTS" ) )
1119 tk.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
1121 else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
1123 tk.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
1124 tk.i_data_init = tk.i_extra_data;
1125 tk.p_data_init = tk.p_extra_data;
1127 else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1128 !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1130 int i_profile, i_srate;
1131 static int i_sample_rates[] =
1133 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1134 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1138 tk.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1139 /* create data for faad (MP4DecSpecificDescrTag)*/
1141 if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
1145 else if( !strcmp( &tk.psz_codec[12], "LC" ) )
1149 else if( !strcmp( &tk.psz_codec[12], "SSR" ) )
1158 for( i_srate = 0; i_srate < 13; i_srate++ )
1160 if( i_sample_rates[i_srate] == tk.i_samplerate )
1165 msg_Dbg( p_input, "profile=%d srate=%d", i_profile, i_srate );
1167 tk.i_extra_data = sizeof( WAVEFORMATEX ) + 2;
1168 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
1169 p_wf = (WAVEFORMATEX*)tk.p_extra_data;
1171 p_wf->wFormatTag = WAVE_FORMAT_UNKNOWN;
1172 p_wf->nChannels = tk.i_channels;
1173 p_wf->nSamplesPerSec = tk.i_samplerate;
1174 p_wf->nAvgBytesPerSec = 0;
1175 p_wf->nBlockAlign = 0;
1176 p_wf->wBitsPerSample = 0;
1179 tk.p_extra_data[sizeof( WAVEFORMATEX )+ 0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1180 tk.p_extra_data[sizeof( WAVEFORMATEX )+ 1] = ((i_srate & 0x1) << 7) | (tk.i_channels << 3);
1182 tk.p_es->p_waveformatex = p_wf;
1184 else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
1185 !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
1186 !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
1190 tk.i_extra_data = sizeof( WAVEFORMATEX );
1191 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
1193 p_wf = (WAVEFORMATEX*)tk.p_extra_data;
1195 if( !strncmp( &tk.psz_codec[6], "INT", 3 ) )
1197 p_wf->wFormatTag = WAVE_FORMAT_PCM;
1201 p_wf->wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
1203 p_wf->nChannels = tk.i_channels;
1204 p_wf->nSamplesPerSec = tk.i_samplerate;
1205 p_wf->nAvgBytesPerSec = 0;
1206 p_wf->nBlockAlign = ( tk.i_bitspersample + 7 ) / 8 * tk.i_channels;
1207 p_wf->wBitsPerSample = tk.i_bitspersample;
1210 tk.p_es->p_waveformatex = p_wf;
1212 if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
1214 tk.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1218 tk.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1221 else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
1223 tk.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1225 else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) )
1227 tk.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1231 msg_Err( p_input, "unknow codec id=`%s'", tk.psz_codec );
1232 tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1235 tk.p_es->i_fourcc = tk.i_codec;
1239 /* select track all video, one audio, no spu TODO : improve */
1240 b_audio_selected = VLC_FALSE;
1241 i_audio_channel = 0;
1243 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1245 #define tk p_sys->track[i_track]
1249 vlc_mutex_lock( &p_input->stream.stream_lock );
1250 input_SelectES( p_input, tk.p_es );
1251 vlc_mutex_unlock( &p_input->stream.stream_lock );
1255 if( ( !b_audio_selected && config_GetInt( p_input, "audio-channel" ) < 0 ) ||
1256 i_audio_channel == config_GetInt( p_input, "audio-channel" ) )
1258 vlc_mutex_lock( &p_input->stream.stream_lock );
1259 input_SelectES( p_input, tk.p_es );
1260 vlc_mutex_unlock( &p_input->stream.stream_lock );
1262 b_audio_selected = tk.p_es->p_decoder_fifo ? VLC_TRUE : VLC_FALSE;
1267 if( i_spu_channel == config_GetInt( p_input, "spu-channel" ) )
1269 vlc_mutex_lock( &p_input->stream.stream_lock );
1270 input_SelectES( p_input, tk.p_es );
1271 vlc_mutex_unlock( &p_input->stream.stream_lock );
1279 if( !b_audio_selected )
1281 msg_Warn( p_input, "cannot find/select audio track" );
1284 /* add informations */
1285 InformationsCreate( p_input );
1293 return VLC_EGENERIC;
1296 /*****************************************************************************
1297 * Deactivate: frees unused data
1298 *****************************************************************************/
1299 static void Deactivate( vlc_object_t *p_this )
1301 input_thread_t *p_input = (input_thread_t *)p_this;
1302 demux_sys_t *p_sys = p_input->p_demux_data;
1306 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1308 #define tk p_sys->track[i_track]
1311 free( tk.psz_codec );
1313 if( tk.psz_language )
1315 free( tk.psz_language );
1319 free( p_sys->track );
1321 if( p_sys->psz_writing_application )
1323 free( p_sys->psz_writing_application );
1325 if( p_sys->psz_muxing_application )
1327 free( p_sys->psz_muxing_application );
1330 delete p_sys->segment;
1339 static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1341 demux_sys_t *p_sys = p_input->p_demux_data;
1352 if( p_input->b_die )
1354 return VLC_EGENERIC;
1357 el = p_sys->ep->Get();
1358 i_level = p_sys->ep->GetLevel();
1360 if( el == NULL && *pp_block != NULL )
1362 /* update the index */
1363 #define idx p_sys->index[p_sys->i_index - 1]
1364 if( p_sys->i_index > 0 && idx.i_time == -1 )
1366 idx.i_time = (*pp_block)->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1367 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1375 if( p_sys->ep->GetLevel() > 1 )
1380 msg_Warn( p_input, "EOF" );
1381 return VLC_EGENERIC;
1387 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1389 p_sys->cluster = (KaxCluster*)el;
1391 /* add it to the index */
1392 if( p_sys->i_index == 0 ||
1393 ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
1395 IndexAppendCluster( p_input, p_sys->cluster );
1400 else if( EbmlId( *el ) == KaxCues::ClassInfos.GlobalId )
1402 msg_Warn( p_input, "find KaxCues FIXME" );
1403 return VLC_EGENERIC;
1407 msg_Dbg( p_input, "unknown (%s)", typeid( el ).name() );
1410 else if( i_level == 2 )
1412 if( EbmlId( *el ) == KaxClusterTimecode::ClassInfos.GlobalId )
1414 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1416 ctc.ReadData( p_sys->es->I_O() );
1417 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
1419 else if( EbmlId( *el ) == KaxBlockGroup::ClassInfos.GlobalId )
1424 else if( i_level == 3 )
1426 if( EbmlId( *el ) == KaxBlock::ClassInfos.GlobalId )
1428 *pp_block = (KaxBlock*)el;
1430 (*pp_block)->ReadData( p_sys->es->I_O() );
1431 (*pp_block)->SetParent( *p_sys->cluster );
1435 else if( EbmlId( *el ) == KaxBlockDuration::ClassInfos.GlobalId )
1437 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1439 dur.ReadData( p_sys->es->I_O() );
1440 *pi_duration = uint64( dur );
1442 else if( EbmlId( *el ) == KaxReferenceBlock::ClassInfos.GlobalId )
1444 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1446 ref.ReadData( p_sys->es->I_O() );
1447 if( *pi_ref1 == -1 )
1449 *pi_ref1 = int64( ref );
1453 *pi_ref2 = int64( ref );
1459 msg_Err( p_input, "invalid level = %d", i_level );
1460 return VLC_EGENERIC;
1465 static pes_packet_t *MemToPES( input_thread_t *p_input, uint8_t *p_mem, int i_mem )
1467 pes_packet_t *p_pes;
1468 data_packet_t *p_data;
1470 if( ( p_pes = input_NewPES( p_input->p_method_data ) ) == NULL )
1475 p_data = input_NewPacket( p_input->p_method_data, i_mem);
1477 memcpy( p_data->p_payload_start, p_mem, i_mem );
1478 p_data->p_payload_end = p_data->p_payload_start + i_mem;
1480 p_pes->p_first = p_pes->p_last = p_data;
1481 p_pes->i_nb_data = 1;
1482 p_pes->i_pes_size = i_mem;
1487 static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts, mtime_t i_duration )
1489 demux_sys_t *p_sys = p_input->p_demux_data;
1494 #define tk p_sys->track[i_track]
1495 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1497 if( tk.i_number == block->TrackNum() )
1503 if( i_track >= p_sys->i_track )
1505 msg_Err( p_input, "invalid track number=%d", block->TrackNum() );
1509 if( tk.p_es->p_decoder_fifo == NULL )
1511 tk.b_inited = VLC_FALSE;
1515 if( tk.i_cat == AUDIO_ES && p_input->stream.control.b_mute )
1520 /* First send init data */
1521 if( !tk.b_inited && tk.i_data_init > 0 )
1523 pes_packet_t *p_init;
1525 msg_Dbg( p_input, "sending header (%d bytes)", tk.i_data_init );
1527 if( tk.i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
1533 /* XXX hack split the 3 headers */
1534 if( tk.p_data_init[0] != 0x02 )
1536 msg_Err( p_input, "invalid vorbis header" );
1539 for( i = 0; i < 2; i++ )
1542 while( i_offset < tk.i_data_init )
1544 i_size[i] += tk.p_data_init[i_offset];
1545 if( tk.p_data_init[i_offset++] != 0xff )
1551 i_size[0] = __MIN( i_size[0], tk.i_data_init - i_offset );
1552 i_size[1] = __MIN( i_size[1], tk.i_data_init - i_offset - i_size[0] );
1553 i_size[2] = tk.i_data_init - i_offset - i_size[0] - i_size[1];
1555 p_init = MemToPES( p_input, &tk.p_data_init[i_offset], i_size[0] );
1558 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1560 p_init = MemToPES( p_input, &tk.p_data_init[i_offset+i_size[0]], i_size[1] );
1563 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1565 p_init = MemToPES( p_input, &tk.p_data_init[i_offset+i_size[0]+i_size[1]], i_size[2] );
1568 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1573 p_init = MemToPES( p_input, tk.p_data_init, tk.i_data_init );
1576 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1580 tk.b_inited = VLC_TRUE;
1583 for( i = 0; i < block->NumberFrames(); i++ )
1585 pes_packet_t *p_pes;
1586 DataBuffer &data = block->GetBuffer(i);
1588 p_pes = MemToPES( p_input, data.Buffer(), data.Size() );
1594 p_pes->i_pts = i_pts;
1595 p_pes->i_dts = i_pts;
1597 if( tk.i_cat == SPU_ES )
1599 if( i_duration > 0 )
1601 /* FIXME not sure about that */
1602 p_pes->i_dts += i_duration * 1000;// * (mtime_t) 1000 / p_sys->i_timescale;
1609 if( p_pes->p_first && p_pes->i_pes_size > 0 )
1611 p_pes->p_first->p_payload_end[-1] = '\0';
1613 if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) )
1615 /* remove all fields before text for now */
1616 char *start = (char*)p_pes->p_first->p_payload_start;
1619 while( *start && i_comma < 8 )
1621 if( *start++ == ',' )
1626 memmove( p_pes->p_first->p_payload_start, start,
1627 strlen( start) + 1 );
1631 input_DecodePES( tk.p_es->p_decoder_fifo, p_pes );
1633 /* use time stamp only for first block */
1640 static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent)
1642 demux_sys_t *p_sys = p_input->p_demux_data;
1645 int64_t i_block_duration;
1646 int64_t i_block_ref1;
1647 int64_t i_block_ref2;
1650 int i_track_skipping;
1653 msg_Dbg( p_input, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1654 if( i_date < 0 && i_percent < 0 )
1660 p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
1661 p_sys->cluster = NULL;
1663 /* seek without index or without date */
1664 if( config_GetInt( p_input, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
1666 int64_t i_pos = i_percent * p_input->stream.p_selected_area->i_size / 100;
1668 msg_Dbg( p_input, "imprecise way of seeking" );
1669 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1671 if( p_sys->index[i_index].i_position >= i_pos)
1676 if( i_index == p_sys->i_index )
1681 p_sys->in->setFilePointer( p_sys->index[i_index].i_position, seek_beginning );
1683 if( p_sys->index[i_index].i_position < i_pos )
1687 msg_Warn( p_input, "searching for cluster, could take some time" );
1689 /* search a cluster */
1690 while( ( el = p_sys->ep->Get() ) != NULL )
1692 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1694 KaxCluster *cluster = (KaxCluster*)el;
1696 /* add it to the index */
1697 IndexAppendCluster( p_input, cluster );
1699 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1701 p_sys->cluster = cluster;
1711 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1713 if( p_sys->index[i_index].i_time >= i_date )
1724 msg_Dbg( p_input, "seek got "I64Fd" (%d%%)",
1725 p_sys->index[i_index].i_time, (int)(100 * p_sys->index[i_index].i_position /p_input->stream.p_selected_area->i_size ) );
1727 p_sys->in->setFilePointer( p_sys->index[i_index].i_position, seek_beginning );
1730 /* now parse until key frame */
1731 #define tk p_sys->track[i_track]
1732 i_track_skipping = 0;
1733 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1735 if( tk.i_cat == VIDEO_ES )
1737 tk.b_search_keyframe = VLC_TRUE;
1742 while( i_track_skipping > 0 )
1744 if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1746 msg_Warn( p_input, "cannot get block EOF?" );
1751 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1753 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1755 if( tk.i_number == block->TrackNum() )
1761 if( i_track < p_sys->i_track )
1763 if( tk.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1765 tk.b_search_keyframe = VLC_FALSE;
1768 if( tk.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1770 BlockDecode( p_input, block, 0, 0 );
1779 /*****************************************************************************
1780 * Demux: reads and demuxes data packets
1781 *****************************************************************************
1782 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1783 *****************************************************************************/
1784 static int Demux( input_thread_t * p_input )
1786 demux_sys_t *p_sys = p_input->p_demux_data;
1787 mtime_t i_start_pts;
1788 int i_block_count = 0;
1791 int64_t i_block_duration;
1792 int64_t i_block_ref1;
1793 int64_t i_block_ref2;
1795 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1797 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
1798 mtime_t i_date = -1;
1801 if( i_duration > 0 )
1803 i_date = (mtime_t)1000000 *
1804 (mtime_t)i_duration*
1805 (mtime_t)p_sys->in->getFilePointer() /
1806 (mtime_t)p_input->stream.p_selected_area->i_size;
1808 if( p_input->stream.p_selected_area->i_size > 0 )
1810 i_percent = 100 * p_sys->in->getFilePointer() /
1811 p_input->stream.p_selected_area->i_size;
1814 Seek( p_input, i_date, i_percent);
1823 if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1825 msg_Warn( p_input, "cannot get block EOF?" );
1830 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1832 if( p_sys->i_pts > 0 )
1834 input_ClockManageRef( p_input,
1835 p_input->stream.p_selected_program,
1836 p_sys->i_pts * 9 / 100 );
1839 i_pts = input_ClockGetTS( p_input,
1840 p_input->stream.p_selected_program,
1841 p_sys->i_pts * 9 / 100 );
1845 BlockDecode( p_input, block, i_pts, i_block_duration );
1850 if( i_start_pts == -1 )
1852 i_start_pts = p_sys->i_pts;
1854 else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1863 /*****************************************************************************
1865 *****************************************************************************/
1866 vlc_stream_io_callback::vlc_stream_io_callback( input_thread_t *p_input_ )
1871 uint32_t vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1873 data_packet_t *p_data;
1879 if( !i_size || mb_eof )
1886 i_count = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 10240 ) );
1891 memcpy( p_buffer, p_data->p_payload_start, i_count );
1892 input_DeletePacket( p_input->p_method_data, p_data );
1894 (uint8_t*)p_buffer += i_count;
1902 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1907 i_last = getFilePointer();
1909 vlc_mutex_lock( &p_input->stream.stream_lock );
1912 case seek_beginning:
1916 i_pos = p_input->stream.p_selected_area->i_size - i_offset;
1919 i_pos= i_last + i_offset;
1924 ( i_pos > p_input->stream.p_selected_area->i_size && p_input->stream.p_selected_area->i_size != 0 ) )
1926 msg_Err( p_input, "seeking to wrong place (i_pos="I64Fd")", i_pos );
1927 vlc_mutex_unlock( &p_input->stream.stream_lock );
1932 vlc_mutex_unlock( &p_input->stream.stream_lock );
1936 if( i_pos == i_last )
1941 msg_Dbg( p_input, "####################seek new="I64Fd" old="I64Fd,
1942 i_pos, getFilePointer() );
1944 if( p_input->stream.b_seekable &&
1945 ( /*p_input->stream.i_method == INPUT_METHOD_FILE ||*/ i_pos < i_last || i_pos - i_last > p_input->i_bufsize / 4 ) )
1947 input_AccessReinit( p_input );
1948 p_input->pf_seek( p_input, i_pos );
1950 else if( i_pos > i_last )
1952 data_packet_t *p_data;
1953 int i_skip = i_pos - i_last;
1957 msg_Warn( p_input, "will skip %d bytes, slow", i_skip );
1964 i_read = input_SplitBuffer( p_input, &p_data,
1965 __MIN( 4096, i_skip ) );
1968 msg_Err( p_input, "seek failed" );
1974 input_DeletePacket( p_input->p_method_data, p_data );
1975 if( i_read == 0 && i_skip > 0 )
1977 msg_Err( p_input, "seek failed" );
1985 msg_Err( p_input, "cannot seek or emulate seek to "I64Fd" from "I64Fd,
1990 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1994 uint64_t vlc_stream_io_callback::getFilePointer( void )
1998 vlc_mutex_lock( &p_input->stream.stream_lock );
1999 i_pos= p_input->stream.p_selected_area->i_tell;
2000 vlc_mutex_unlock( &p_input->stream.stream_lock );
2004 void vlc_stream_io_callback::close( void )
2010 /*****************************************************************************
2011 * Ebml Stream parser
2012 *****************************************************************************/
2013 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
2021 for( i = 1; i < 6; i++ )
2027 mb_keep = VLC_FALSE;
2030 EbmlParser::~EbmlParser( void )
2034 for( i = 1; i < mi_level; i++ )
2040 mb_keep = VLC_FALSE;
2044 void EbmlParser::Up( void )
2046 if( mi_user_level == mi_level )
2048 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
2054 void EbmlParser::Down( void )
2060 void EbmlParser::Keep( void )
2065 int EbmlParser::GetLevel( void )
2067 return mi_user_level;
2070 EbmlElement *EbmlParser::Get( void )
2074 if( mi_user_level != mi_level )
2080 EbmlElement *ret = m_got;
2086 if( m_el[mi_level] )
2088 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
2091 delete m_el[mi_level];
2093 mb_keep = VLC_FALSE;
2096 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
2107 delete m_el[mi_level - 1];
2108 m_got = m_el[mi_level -1] = m_el[mi_level];
2109 m_el[mi_level] = NULL;
2116 else if( m_el[mi_level] == NULL )
2118 fprintf( stderr," m_el[mi_level] == NULL\n" );
2121 return m_el[mi_level];
2125 /*****************************************************************************
2127 * * LoadCues : load the cues element and update index
2129 * * LoadTags : load ... the tags element
2131 * * InformationsCreate : create all informations, load tags if present
2133 *****************************************************************************/
2134 static void LoadCues( input_thread_t *p_input )
2136 demux_sys_t *p_sys = p_input->p_demux_data;
2137 int64_t i_sav_position = p_sys->in->getFilePointer();
2139 EbmlElement *el, *cues;
2141 msg_Dbg( p_input, "loading cues" );
2142 p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
2143 cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
2147 msg_Err( p_input, "cannot load cues (broken seekhead or file)" );
2151 ep = new EbmlParser( p_sys->es, cues );
2152 while( ( el = ep->Get() ) != NULL )
2154 if( EbmlId( *el ) == KaxCuePoint::ClassInfos.GlobalId )
2156 #define idx p_sys->index[p_sys->i_index]
2159 idx.i_block_number= -1;
2160 idx.i_position = -1;
2162 idx.b_key = VLC_TRUE;
2165 while( ( el = ep->Get() ) != NULL )
2167 if( EbmlId( *el ) == KaxCueTime::ClassInfos.GlobalId )
2169 KaxCueTime &ctime = *(KaxCueTime*)el;
2171 ctime.ReadData( p_sys->es->I_O() );
2173 idx.i_time = uint64( ctime ) * (mtime_t)1000000000 / p_sys->i_timescale;
2175 else if( EbmlId( *el ) == KaxCueTrackPositions::ClassInfos.GlobalId )
2178 while( ( el = ep->Get() ) != NULL )
2180 if( EbmlId( *el ) == KaxCueTrack::ClassInfos.GlobalId )
2182 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
2184 ctrack.ReadData( p_sys->es->I_O() );
2185 idx.i_track = uint16( ctrack );
2187 else if( EbmlId( *el ) == KaxCueClusterPosition::ClassInfos.GlobalId )
2189 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
2191 ccpos.ReadData( p_sys->es->I_O() );
2192 idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
2194 else if( EbmlId( *el ) == KaxCueBlockNumber::ClassInfos.GlobalId )
2196 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2198 cbnum.ReadData( p_sys->es->I_O() );
2199 idx.i_block_number = uint32( cbnum );
2203 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2210 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2215 msg_Dbg( p_input, " * added time="I64Fd" pos="I64Fd
2216 " track=%d bnum=%d", idx.i_time, idx.i_position,
2217 idx.i_track, idx.i_block_number );
2220 if( p_sys->i_index >= p_sys->i_index_max )
2222 p_sys->i_index_max += 1024;
2223 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2229 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2235 p_sys->b_cues = VLC_TRUE;
2237 msg_Dbg( p_input, "loading cues done." );
2238 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2241 static void LoadTags( input_thread_t *p_input )
2243 demux_sys_t *p_sys = p_input->p_demux_data;
2244 int64_t i_sav_position = p_sys->in->getFilePointer();
2246 EbmlElement *el, *tags;
2248 msg_Dbg( p_input, "loading tags" );
2249 p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
2250 tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2254 msg_Err( p_input, "cannot load tags (broken seekhead or file)" );
2258 msg_Dbg( p_input, "Tags" );
2259 ep = new EbmlParser( p_sys->es, tags );
2260 while( ( el = ep->Get() ) != NULL )
2262 if( EbmlId( *el ) == KaxTag::ClassInfos.GlobalId )
2264 msg_Dbg( p_input, "+ Tag" );
2266 while( ( el = ep->Get() ) != NULL )
2268 if( EbmlId( *el ) == KaxTagTargets::ClassInfos.GlobalId )
2270 msg_Dbg( p_input, "| + Targets" );
2272 while( ( el = ep->Get() ) != NULL )
2274 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2278 else if( EbmlId( *el ) == KaxTagGeneral::ClassInfos.GlobalId )
2280 msg_Dbg( p_input, "| + General" );
2282 while( ( el = ep->Get() ) != NULL )
2284 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2288 else if( EbmlId( *el ) == KaxTagGenres::ClassInfos.GlobalId )
2290 msg_Dbg( p_input, "| + Genres" );
2292 while( ( el = ep->Get() ) != NULL )
2294 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2298 else if( EbmlId( *el ) == KaxTagAudioSpecific::ClassInfos.GlobalId )
2300 msg_Dbg( p_input, "| + Audio Specific" );
2302 while( ( el = ep->Get() ) != NULL )
2304 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2308 else if( EbmlId( *el ) == KaxTagImageSpecific::ClassInfos.GlobalId )
2310 msg_Dbg( p_input, "| + Images Specific" );
2312 while( ( el = ep->Get() ) != NULL )
2314 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2318 else if( EbmlId( *el ) == KaxTagMultiComment::ClassInfos.GlobalId )
2320 msg_Dbg( p_input, "| + Multi Comment" );
2322 else if( EbmlId( *el ) == KaxTagMultiCommercial::ClassInfos.GlobalId )
2324 msg_Dbg( p_input, "| + Multi Commercial" );
2326 else if( EbmlId( *el ) == KaxTagMultiDate::ClassInfos.GlobalId )
2328 msg_Dbg( p_input, "| + Multi Date" );
2330 else if( EbmlId( *el ) == KaxTagMultiEntity::ClassInfos.GlobalId )
2332 msg_Dbg( p_input, "| + Multi Entity" );
2334 else if( EbmlId( *el ) == KaxTagMultiIdentifier::ClassInfos.GlobalId )
2336 msg_Dbg( p_input, "| + Multi Identifier" );
2338 else if( EbmlId( *el ) == KaxTagMultiLegal::ClassInfos.GlobalId )
2340 msg_Dbg( p_input, "| + Multi Legal" );
2342 else if( EbmlId( *el ) == KaxTagMultiTitle::ClassInfos.GlobalId )
2344 msg_Dbg( p_input, "| + Multi Title" );
2348 msg_Dbg( p_input, "| + Unknown (%s)", typeid( *el ).name() );
2355 msg_Dbg( p_input, "+ Unknown (%s)", typeid( *el ).name() );
2361 msg_Dbg( p_input, "loading tags done." );
2362 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2365 static void InformationsCreate( input_thread_t *p_input )
2367 demux_sys_t *p_sys = p_input->p_demux_data;
2368 input_info_category_t *p_cat;
2371 p_cat = input_InfoCategory( p_input, "Matroska" );
2372 if( p_sys->f_duration > 1000.1 )
2374 int64_t i_sec = (int64_t)p_sys->f_duration / 1000;
2378 m = ( i_sec / 60 ) % 60;
2381 input_AddInfo( p_cat, _("Duration"), "%d:%2.2d:%2.2d" , h, m, s );
2384 if( p_sys->psz_title )
2386 input_AddInfo( p_cat, _("Title"), "%s" ,p_sys->psz_title );
2388 if( p_sys->psz_date_utc )
2390 input_AddInfo( p_cat, _("Date UTC"), "%s" ,p_sys->psz_date_utc );
2392 if( p_sys->psz_segment_filename )
2394 input_AddInfo( p_cat, _("Segment Filename"), "%s" ,p_sys->psz_segment_filename );
2396 if( p_sys->psz_muxing_application )
2398 input_AddInfo( p_cat, _("Muxing Application"), "%s" ,p_sys->psz_muxing_application );
2400 if( p_sys->psz_writing_application )
2402 input_AddInfo( p_cat, _("Writing Application"), "%s" ,p_sys->psz_writing_application );
2404 input_AddInfo( p_cat, _("Number of streams"), "%d" , p_sys->i_track );
2406 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
2408 char psz_cat[strlen( "Stream " ) + 10];
2409 #define tk p_sys->track[i_track]
2411 sprintf( psz_cat, "Stream %d", i_track );
2412 p_cat = input_InfoCategory( p_input, psz_cat);
2415 input_AddInfo( p_cat, _("Name"), "%s", tk.psz_name );
2417 if( tk.psz_codec_name )
2419 input_AddInfo( p_cat, _("Codec Name"), "%s", tk.psz_codec_name );
2421 if( tk.psz_codec_settings )
2423 input_AddInfo( p_cat, _("Codec Setting"), "%s", tk.psz_codec_settings );
2425 if( tk.psz_codec_info_url )
2427 input_AddInfo( p_cat, _("Codec Info"), "%s", tk.psz_codec_info_url );
2429 if( tk.psz_codec_download_url )
2431 input_AddInfo( p_cat, _("Codec Download"), "%s", tk.psz_codec_download_url );
2437 input_AddInfo( p_cat, _("Type"), _("Audio") );
2438 input_AddInfo( p_cat, _("Codec"), "%.4s (%s)", (char*)&tk.i_codec, tk.psz_codec );
2439 if( tk.i_channels > 0 )
2441 input_AddInfo( p_cat, _("Channels"), "%d", tk.i_channels );
2443 if( tk.i_samplerate > 0 )
2445 input_AddInfo( p_cat, _("Sample Rate"), "%d", tk.i_samplerate );
2447 if( tk.i_bitspersample )
2449 input_AddInfo( p_cat, _("Bits Per Sample"), "%d", tk.i_bitspersample );
2453 input_AddInfo( p_cat, _("Type"), _("Video") );
2454 input_AddInfo( p_cat, _("Codec"), "%.4s (%s)", (char*)&tk.i_codec, tk.psz_codec );
2455 if( tk.i_width > 0 && tk.i_height )
2457 input_AddInfo( p_cat, _("Resolution"), "%dx%d", tk.i_width, tk.i_height );
2459 if( tk.i_display_width > 0 && tk.i_display_height )
2461 input_AddInfo( p_cat, _("Display Resolution"), "%dx%d", tk.i_display_width, tk.i_display_height );
2463 if( tk.f_fps > 0.1 )
2465 input_AddInfo( p_cat, _("Frame Per Second"), "%.3f", tk.f_fps );
2469 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
2470 input_AddInfo( p_cat, _("Codec"), "%s", tk.psz_codec );
2476 if( p_sys->i_tags_position >= 0 && p_input->stream.b_seekable )
2478 LoadTags( p_input );
2483 /*****************************************************************************
2485 *****************************************************************************/
2487 static void IndexAppendCluster( input_thread_t *p_input, KaxCluster *cluster )
2489 demux_sys_t *p_sys = p_input->p_demux_data;
2491 #define idx p_sys->index[p_sys->i_index]
2493 idx.i_block_number= -1;
2494 idx.i_position = cluster->GetElementPosition();
2496 idx.b_key = VLC_TRUE;
2499 if( p_sys->i_index >= p_sys->i_index_max )
2501 p_sys->i_index_max += 1024;
2502 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2507 static char * UTF8ToStr( const UTFstring &u )
2516 p = dst = (char*)malloc( i_src + 1);
2535 static char *LanguageGetName ( const char *psz_code )
2537 const iso639_lang_t *pl;
2539 if( strlen( psz_code ) == 2 )
2541 pl = GetLang_1( psz_code );
2543 else if( strlen( psz_code ) == 3 )
2545 pl = GetLang_2B( psz_code );
2546 if( !strcmp( pl->psz_iso639_1, "??" ) )
2548 pl = GetLang_2T( psz_code );
2553 return strdup( psz_code );
2556 if( !strcmp( pl->psz_iso639_1, "??" ) )
2558 return strdup( psz_code );
2562 if( *pl->psz_native_name )
2564 return strdup( pl->psz_native_name );
2566 return strdup( pl->psz_eng_name );