1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mkv.cpp,v 1.26 2003/09/07 22:48:29 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;
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Open ( vlc_object_t * );
91 static void Close( vlc_object_t * );
94 add_category_hint( N_("mkv-demuxer"), NULL, VLC_TRUE );
95 add_bool( "mkv-seek-percent", 1, NULL,
96 N_("Seek based on percent not time"),
97 N_("Seek based on percent not time"), VLC_TRUE );
99 set_description( _("mka/mkv stream demuxer" ) );
100 set_capability( "demux", 50 );
101 set_callbacks( Open, Close );
102 add_shortcut( "mka" );
103 add_shortcut( "mkv" );
106 /*****************************************************************************
108 *****************************************************************************/
109 static int Demux ( input_thread_t * );
110 static void Seek ( input_thread_t *, mtime_t i_date, int i_percent );
113 /*****************************************************************************
115 *****************************************************************************/
116 class vlc_stream_io_callback: public IOCallback
123 vlc_stream_io_callback( stream_t * );
125 virtual uint32_t read ( void *p_buffer, size_t i_size);
126 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
127 virtual size_t write ( const void *p_buffer, size_t i_size);
128 virtual uint64_t getFilePointer ( void );
129 virtual void close ( void );
132 /*****************************************************************************
134 *****************************************************************************/
138 EbmlParser( EbmlStream *es, EbmlElement *el_start );
143 EbmlElement *Get( void );
146 int GetLevel( void );
151 EbmlElement *m_el[6];
160 /*****************************************************************************
161 * Some functions to manipulate memory
162 *****************************************************************************/
163 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
164 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
166 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
169 /*****************************************************************************
170 * definitions of structures and functions used by this plugins
171 *****************************************************************************/
175 vlc_bool_t b_default;
176 vlc_bool_t b_enabled;
180 uint8_t *p_extra_data;
185 vlc_fourcc_t i_codec;
187 uint64_t i_default_duration;
188 float f_timecodescale;
193 int i_display_height;
202 es_descriptor_t *p_es;
207 /* data to be send first */
209 uint8_t *p_data_init;
211 /* hack : it's for seek */
212 vlc_bool_t b_search_keyframe;
216 char *psz_codec_name;
217 char *psz_codec_settings;
218 char *psz_codec_info_url;
219 char *psz_codec_download_url;
238 vlc_stream_io_callback *in;
243 uint64_t i_timescale;
245 /* duration of the segment */
253 int64_t i_cues_position;
254 int64_t i_chapters_position;
255 int64_t i_tags_position;
269 char *psz_muxing_application;
270 char *psz_writing_application;
271 char *psz_segment_filename;
276 #define MKVD_TIMECODESCALE 1000000
278 static void IndexAppendCluster ( input_thread_t *p_input, KaxCluster *cluster );
279 static char *UTF8ToStr ( const UTFstring &u );
280 static void LoadCues ( input_thread_t *);
281 static void InformationsCreate ( input_thread_t *p_input );
283 static char *LanguageGetName ( const char *psz_code );
285 /*****************************************************************************
286 * Open: initializes matroska demux structures
287 *****************************************************************************/
288 static int Open( vlc_object_t * p_this )
290 input_thread_t *p_input = (input_thread_t *)p_this;
295 vlc_bool_t b_audio_selected;
296 int i_spu_channel, i_audio_channel;
298 EbmlElement *el = NULL, *el1 = NULL, *el2 = NULL, *el3 = NULL, *el4 = NULL;
300 /* Set the demux function */
301 p_input->pf_demux = Demux;
302 p_input->pf_demux_control = demux_vaControlDefault;
304 /* peek the begining */
305 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
307 msg_Warn( p_input, "cannot peek" );
311 /* is a valid file */
312 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
313 p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
315 msg_Warn( p_input, "matroska module discarded "
316 "(invalid header 0x%.2x%.2x%.2x%.2x)",
317 p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
321 p_input->p_demux_data = p_sys = (demux_sys_t*)malloc(sizeof( demux_sys_t ));
322 memset( p_sys, 0, sizeof( demux_sys_t ) );
324 if( ( p_sys->s = stream_OpenInput( p_input ) ) == NULL )
326 msg_Err( p_input, "cannot create stream" );
330 p_sys->in = new vlc_stream_io_callback( p_sys->s );
331 p_sys->es = new EbmlStream( *p_sys->in );
332 p_sys->f_duration = -1;
333 p_sys->i_timescale = MKVD_TIMECODESCALE;
335 p_sys->track = (mkv_track_t*)malloc( sizeof( mkv_track_t ) );
337 p_sys->i_cues_position = -1;
338 p_sys->i_chapters_position = -1;
339 p_sys->i_tags_position = -1;
341 p_sys->b_cues = VLC_FALSE;
343 p_sys->i_index_max = 1024;
344 p_sys->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
345 p_sys->i_index_max );
347 p_sys->psz_muxing_application = NULL;
348 p_sys->psz_writing_application = NULL;
349 p_sys->psz_segment_filename = NULL;
350 p_sys->psz_title = NULL;
351 p_sys->psz_date_utc = NULL;;
353 if( p_sys->es == NULL )
355 msg_Err( p_input, "failed to create EbmlStream" );
357 stream_Release( p_sys->s );
361 /* Find the EbmlHead element */
362 el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
365 msg_Err( p_input, "cannot find EbmlHead" );
368 msg_Dbg( p_input, "EbmlHead" );
370 el->SkipData( *p_sys->es, el->Generic().Context );
374 el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
377 msg_Err( p_input, "cannot find KaxSegment" );
380 msg_Dbg( p_input, "+ Segment" );
381 p_sys->segment = (KaxSegment*)el;
382 p_sys->cluster = NULL;
384 p_sys->ep = new EbmlParser( p_sys->es, el );
386 while( ( el1 = p_sys->ep->Get() ) != NULL )
388 if( EbmlId( *el1 ) == KaxInfo::ClassInfos.GlobalId )
390 msg_Dbg( p_input, "| + Informations" );
393 while( ( el2 = p_sys->ep->Get() ) != NULL )
395 if( EbmlId( *el2 ) == KaxTimecodeScale::ClassInfos.GlobalId )
397 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)el2;
399 tcs.ReadData( p_sys->es->I_O() );
400 p_sys->i_timescale = uint64(tcs);
402 msg_Dbg( p_input, "| | + TimecodeScale="I64Fd,
403 p_sys->i_timescale );
405 else if( EbmlId( *el2 ) == KaxDuration::ClassInfos.GlobalId )
407 KaxDuration &dur = *(KaxDuration*)el2;
409 dur.ReadData( p_sys->es->I_O() );
410 p_sys->f_duration = float(dur);
412 msg_Dbg( p_input, "| | + Duration=%f",
415 else if( EbmlId( *el2 ) == KaxMuxingApp::ClassInfos.GlobalId )
417 KaxMuxingApp &mapp = *(KaxMuxingApp*)el2;
419 mapp.ReadData( p_sys->es->I_O() );
421 p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
423 msg_Dbg( p_input, "| | + Muxing Application=%s",
424 p_sys->psz_muxing_application );
426 else if( EbmlId( *el2 ) == KaxWritingApp::ClassInfos.GlobalId )
428 KaxWritingApp &wapp = *(KaxWritingApp*)el2;
430 wapp.ReadData( p_sys->es->I_O() );
432 p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
434 msg_Dbg( p_input, "| | + Wrinting Application=%s",
435 p_sys->psz_writing_application );
437 else if( EbmlId( *el2 ) == KaxSegmentFilename::ClassInfos.GlobalId )
439 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)el2;
441 sfn.ReadData( p_sys->es->I_O() );
443 p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
445 msg_Dbg( p_input, "| | + Segment Filename=%s",
446 p_sys->psz_segment_filename );
448 else if( EbmlId( *el2 ) == KaxTitle::ClassInfos.GlobalId )
450 KaxTitle &title = *(KaxTitle*)el2;
452 title.ReadData( p_sys->es->I_O() );
454 p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
456 msg_Dbg( p_input, "| | + Title=%s", p_sys->psz_title );
459 else if( EbmlId( *el2 ) == KaxDateUTC::ClassInfos.GlobalId )
461 KaxDateUTC &date = *(KaxDateUTC*)el2;
466 date.ReadData( p_sys->es->I_O() );
468 i_date = date.GetEpochDate();
469 memset( buffer, 0, 256 );
470 if( gmtime_r( &i_date, &tmres ) &&
471 asctime_r( &tmres, buffer ) )
473 buffer[strlen( buffer)-1]= '\0';
474 p_sys->psz_date_utc = strdup( buffer );
475 msg_Dbg( p_input, "| | + Date=%s", p_sys->psz_date_utc );
481 msg_Dbg( p_input, "| | + Unknown (%s)", typeid(*el2).name() );
486 else if( EbmlId( *el1 ) == KaxTracks::ClassInfos.GlobalId )
488 msg_Dbg( p_input, "| + Tracks" );
491 while( ( el2 = p_sys->ep->Get() ) != NULL )
493 if( EbmlId( *el2 ) == KaxTrackEntry::ClassInfos.GlobalId )
495 msg_Dbg( p_input, "| | + Track Entry" );
498 p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
499 #define tk p_sys->track[p_sys->i_track - 1]
500 memset( &tk, 0, sizeof( mkv_track_t ) );
501 tk.i_cat = UNKNOWN_ES;
502 tk.b_default = VLC_TRUE;
503 tk.b_enabled = VLC_TRUE;
504 tk.i_number = p_sys->i_track - 1;
506 tk.p_extra_data = NULL;
509 tk.psz_language = NULL;
510 tk.i_default_duration = 0;
511 tk.f_timecodescale = 1.0;
513 tk.b_inited = VLC_FALSE;
515 tk.p_data_init = NULL;
518 tk.psz_codec_name = NULL;
519 tk.psz_codec_settings = NULL;
520 tk.psz_codec_info_url = NULL;
521 tk.psz_codec_download_url = NULL;
525 while( ( el3 = p_sys->ep->Get() ) != NULL )
527 if( EbmlId( *el3 ) == KaxTrackNumber::ClassInfos.GlobalId )
529 KaxTrackNumber &tnum = *(KaxTrackNumber*)el3;
530 tnum.ReadData( p_sys->es->I_O() );
532 tk.i_number = uint32( tnum );
533 msg_Dbg( p_input, "| | | + Track Number=%u",
536 else if( EbmlId( *el3 ) == KaxTrackUID::ClassInfos.GlobalId )
538 KaxTrackUID &tuid = *(KaxTrackUID*)el3;
539 tuid.ReadData( p_sys->es->I_O() );
541 msg_Dbg( p_input, "| | | + Track UID=%u",
544 else if( EbmlId( *el3 ) == KaxTrackType::ClassInfos.GlobalId )
547 KaxTrackType &ttype = *(KaxTrackType*)el3;
548 ttype.ReadData( p_sys->es->I_O() );
549 switch( uint8(ttype) )
560 psz_type = "subtitle";
564 psz_type = "unknown";
565 tk.i_cat = UNKNOWN_ES;
569 msg_Dbg( p_input, "| | | + Track Type=%s",
572 else if( EbmlId( *el3 ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
574 KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)el3;
575 fenb.ReadData( p_sys->es->I_O() );
577 tk.b_enabled = uint32( fenb );
578 msg_Dbg( p_input, "| | | + Track Enabled=%u",
581 else if( EbmlId( *el3 ) == KaxTrackFlagDefault::ClassInfos.GlobalId )
583 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)el3;
584 fdef.ReadData( p_sys->es->I_O() );
586 tk.b_default = uint32( fdef );
587 msg_Dbg( p_input, "| | | + Track Default=%u",
590 else if( EbmlId( *el3 ) == KaxTrackFlagLacing::ClassInfos.GlobalId )
592 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)el3;
593 lac.ReadData( p_sys->es->I_O() );
595 msg_Dbg( p_input, "| | | + Track Lacing=%d",
598 else if( EbmlId( *el3 ) == KaxTrackMinCache::ClassInfos.GlobalId )
600 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)el3;
601 cmin.ReadData( p_sys->es->I_O() );
603 msg_Dbg( p_input, "| | | + Track MinCache=%d",
606 else if( EbmlId( *el3 ) == KaxTrackMaxCache::ClassInfos.GlobalId )
608 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)el3;
609 cmax.ReadData( p_sys->es->I_O() );
611 msg_Dbg( p_input, "| | | + Track MaxCache=%d",
614 else if( EbmlId( *el3 ) == KaxTrackDefaultDuration::ClassInfos.GlobalId )
616 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)el3;
617 defd.ReadData( p_sys->es->I_O() );
619 tk.i_default_duration = uint64(defd);
620 msg_Dbg( p_input, "| | | + Track Default Duration="I64Fd, uint64(defd) );
622 else if( EbmlId( *el3 ) == KaxTrackTimecodeScale::ClassInfos.GlobalId )
624 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)el3;
625 ttcs.ReadData( p_sys->es->I_O() );
627 tk.f_timecodescale = float( ttcs );
628 msg_Dbg( p_input, "| | | + Track TimeCodeScale=%f", tk.f_timecodescale );
630 else if( EbmlId( *el3 ) == KaxTrackName::ClassInfos.GlobalId )
632 KaxTrackName &tname = *(KaxTrackName*)el3;
633 tname.ReadData( p_sys->es->I_O() );
635 tk.psz_name = UTF8ToStr( UTFstring( tname ) );
636 msg_Dbg( p_input, "| | | + Track Name=%s",
639 else if( EbmlId( *el3 ) == KaxTrackLanguage::ClassInfos.GlobalId )
641 KaxTrackLanguage &lang = *(KaxTrackLanguage*)el3;
642 lang.ReadData( p_sys->es->I_O() );
645 LanguageGetName( string( lang ).c_str() );
647 "| | | + Track Language=`%s'(%s) ",
648 tk.psz_language, string( lang ).c_str() );
650 else if( EbmlId( *el3 ) == KaxCodecID::ClassInfos.GlobalId )
652 KaxCodecID &codecid = *(KaxCodecID*)el3;
653 codecid.ReadData( p_sys->es->I_O() );
655 tk.psz_codec = strdup( string( codecid ).c_str() );
656 msg_Dbg( p_input, "| | | + Track CodecId=%s",
657 string( codecid ).c_str() );
659 else if( EbmlId( *el3 ) == KaxCodecPrivate::ClassInfos.GlobalId )
661 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)el3;
662 cpriv.ReadData( p_sys->es->I_O() );
664 tk.i_extra_data = cpriv.GetSize();
665 if( tk.i_extra_data > 0 )
667 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
668 memcpy( tk.p_extra_data, cpriv.GetBuffer(), tk.i_extra_data );
670 msg_Dbg( p_input, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
672 else if( EbmlId( *el3 ) == KaxCodecName::ClassInfos.GlobalId )
674 KaxCodecName &cname = *(KaxCodecName*)el3;
675 cname.ReadData( p_sys->es->I_O() );
677 tk.psz_codec_name = UTF8ToStr( UTFstring( cname ) );
678 msg_Dbg( p_input, "| | | + Track Codec Name=%s", tk.psz_codec_name );
680 else if( EbmlId( *el3 ) == KaxCodecSettings::ClassInfos.GlobalId )
682 KaxCodecSettings &cset = *(KaxCodecSettings*)el3;
683 cset.ReadData( p_sys->es->I_O() );
685 tk.psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
686 msg_Dbg( p_input, "| | | + Track Codec Settings=%s", tk.psz_codec_settings );
688 else if( EbmlId( *el3 ) == KaxCodecInfoURL::ClassInfos.GlobalId )
690 KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)el3;
691 ciurl.ReadData( p_sys->es->I_O() );
693 tk.psz_codec_info_url = strdup( string( ciurl ).c_str() );
694 msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_info_url );
696 else if( EbmlId( *el3 ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
698 KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)el3;
699 cdurl.ReadData( p_sys->es->I_O() );
701 tk.psz_codec_download_url = strdup( string( cdurl ).c_str() );
702 msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_download_url );
704 else if( EbmlId( *el3 ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
706 KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)el3;
707 cdall.ReadData( p_sys->es->I_O() );
709 msg_Dbg( p_input, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
711 else if( EbmlId( *el3 ) == KaxTrackOverlay::ClassInfos.GlobalId )
713 KaxTrackOverlay &tovr = *(KaxTrackOverlay*)el3;
714 tovr.ReadData( p_sys->es->I_O() );
716 msg_Dbg( p_input, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
718 else if( EbmlId( *el3 ) == KaxTrackVideo::ClassInfos.GlobalId )
720 msg_Dbg( p_input, "| | | + Track Video" );
723 tk.i_display_width = 0;
724 tk.i_display_height = 0;
729 while( ( el4 = p_sys->ep->Get() ) != NULL )
731 if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
733 KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
734 fint.ReadData( p_sys->es->I_O() );
736 msg_Dbg( p_input, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
738 else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
740 KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
741 stereo.ReadData( p_sys->es->I_O() );
743 msg_Dbg( p_input, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
745 else if( EbmlId( *el4 ) == KaxVideoPixelWidth::ClassInfos.GlobalId )
747 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)el4;
748 vwidth.ReadData( p_sys->es->I_O() );
750 tk.i_width = uint16( vwidth );
751 msg_Dbg( p_input, "| | | | + width=%d", uint16( vwidth ) );
753 else if( EbmlId( *el4 ) == KaxVideoPixelHeight::ClassInfos.GlobalId )
755 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)el4;
756 vheight.ReadData( p_sys->es->I_O() );
758 tk.i_height = uint16( vheight );
759 msg_Dbg( p_input, "| | | | + height=%d", uint16( vheight ) );
761 else if( EbmlId( *el4 ) == KaxVideoDisplayWidth::ClassInfos.GlobalId )
763 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)el4;
764 vwidth.ReadData( p_sys->es->I_O() );
766 tk.i_display_width = uint16( vwidth );
767 msg_Dbg( p_input, "| | | | + display width=%d", uint16( vwidth ) );
769 else if( EbmlId( *el4 ) == KaxVideoDisplayHeight::ClassInfos.GlobalId )
771 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)el4;
772 vheight.ReadData( p_sys->es->I_O() );
774 tk.i_display_height = uint16( vheight );
775 msg_Dbg( p_input, "| | | | + display height=%d", uint16( vheight ) );
777 else if( EbmlId( *el4 ) == KaxVideoFrameRate::ClassInfos.GlobalId )
779 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)el4;
780 vfps.ReadData( p_sys->es->I_O() );
782 tk.f_fps = float( vfps );
783 msg_Dbg( p_input, " | | | + fps=%f", float( vfps ) );
785 else if( EbmlId( *el4 ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
787 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)el4;
788 vdmode.ReadData( p_sys->es->I_O() );
790 msg_Dbg( p_input, "| | | | + Track Video Display Unit=%s",
791 uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
793 else if( EbmlId( *el4 ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
795 KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)el4;
796 ratio.ReadData( p_sys->es->I_O() );
798 msg_Dbg( p_input, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
800 else if( EbmlId( *el4 ) == KaxVideoGamma::ClassInfos.GlobalId )
802 KaxVideoGamma &gamma = *(KaxVideoGamma*)el4;
803 gamma.ReadData( p_sys->es->I_O() );
805 msg_Dbg( p_input, " | | | + fps=%f", float( gamma ) );
809 msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() );
814 else if( EbmlId( *el3 ) == KaxTrackAudio::ClassInfos.GlobalId )
816 msg_Dbg( p_input, "| | | + Track Audio" );
819 tk.i_bitspersample = 0;
823 while( ( el4 = p_sys->ep->Get() ) != NULL )
825 if( EbmlId( *el4 ) == KaxAudioSamplingFreq::ClassInfos.GlobalId )
827 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)el4;
828 afreq.ReadData( p_sys->es->I_O() );
830 tk.i_samplerate = (int)float( afreq );
831 msg_Dbg( p_input, "| | | | + afreq=%d", tk.i_samplerate );
833 else if( EbmlId( *el4 ) == KaxAudioChannels::ClassInfos.GlobalId )
835 KaxAudioChannels &achan = *(KaxAudioChannels*)el4;
836 achan.ReadData( p_sys->es->I_O() );
838 tk.i_channels = uint8( achan );
839 msg_Dbg( p_input, "| | | | + achan=%u", uint8( achan ) );
841 else if( EbmlId( *el4 ) == KaxAudioBitDepth::ClassInfos.GlobalId )
843 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)el4;
844 abits.ReadData( p_sys->es->I_O() );
846 tk.i_bitspersample = uint8( abits );
847 msg_Dbg( p_input, "| | | | + abits=%u", uint8( abits ) );
851 msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() );
858 msg_Dbg( p_input, "| | | + Unknown (%s)",
859 typeid(*el3).name() );
866 msg_Dbg( p_input, "| | + Unknown (%s)",
867 typeid(*el2).name() );
873 else if( EbmlId( *el1 ) == KaxSeekHead::ClassInfos.GlobalId )
875 msg_Dbg( p_input, "| + Seek head" );
877 while( ( el = p_sys->ep->Get() ) != NULL )
879 if( EbmlId( *el ) == KaxSeek::ClassInfos.GlobalId )
881 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
884 //msg_Dbg( p_input, "| | + Seek" );
886 while( ( el = p_sys->ep->Get() ) != NULL )
888 if( EbmlId( *el ) == KaxSeekID::ClassInfos.GlobalId )
890 KaxSeekID &sid = *(KaxSeekID*)el;
892 sid.ReadData( p_sys->es->I_O() );
894 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
896 else if( EbmlId( *el ) == KaxSeekPosition::ClassInfos.GlobalId )
898 KaxSeekPosition &spos = *(KaxSeekPosition*)el;
900 spos.ReadData( p_sys->es->I_O() );
902 i_pos = uint64( spos );
906 msg_Dbg( p_input, "| | | + Unknown (%s)",
907 typeid(*el).name() );
914 if( id == KaxCues::ClassInfos.GlobalId )
916 msg_Dbg( p_input, "| | | = cues at "I64Fd,
918 p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
920 else if( id == KaxChapters::ClassInfos.GlobalId )
922 msg_Dbg( p_input, "| | | = chapters at "I64Fd,
924 p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
926 else if( id == KaxTags::ClassInfos.GlobalId )
928 msg_Dbg( p_input, "| | | = tags at "I64Fd,
930 p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
937 msg_Dbg( p_input, "| | + Unknown (%s)",
938 typeid(*el).name() );
943 else if( EbmlId( *el1 ) == KaxCues::ClassInfos.GlobalId )
945 msg_Dbg( p_input, "| + Cues" );
947 else if( EbmlId( *el1 ) == KaxCluster::ClassInfos.GlobalId )
949 msg_Dbg( p_input, "| + Cluster" );
951 p_sys->cluster = (KaxCluster*)el1;
954 /* stop parsing the stream */
957 #ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
958 else if( EbmlId( *el1 ) == KaxAttachments::ClassInfos.GlobalId )
960 else if( EbmlId( *el1 ) == KaxAttachements::ClassInfos.GlobalId )
963 msg_Dbg( p_input, "| + Attachments FIXME TODO (but probably never supported)" );
965 else if( EbmlId( *el1 ) == KaxChapters::ClassInfos.GlobalId )
967 msg_Dbg( p_input, "| + Chapters FIXME TODO" );
969 else if( EbmlId( *el1 ) == KaxTag::ClassInfos.GlobalId )
971 msg_Dbg( p_input, "| + Tags FIXME TODO" );
975 msg_Dbg( p_input, "| + Unknown (%s)", typeid(*el1).name() );
979 if( p_sys->cluster == NULL )
981 msg_Err( p_input, "cannot find any cluster, damaged file ?" );
985 if( p_sys->i_chapters_position >= 0 )
987 msg_Warn( p_input, "chapters unsupported" );
990 /* *** Load the cue if found *** */
991 if( p_sys->i_cues_position >= 0 )
993 vlc_bool_t b_seekable;
995 stream_Control( p_sys->s, STREAM_CAN_FASTSEEK, &b_seekable );
1002 if( !p_sys->b_cues || p_sys->i_index <= 0 )
1004 msg_Warn( p_input, "no cues/empty cues found->seek won't be precise" );
1006 IndexAppendCluster( p_input, p_sys->cluster );
1008 p_sys->b_cues = VLC_FALSE;
1011 /* Create one program */
1012 vlc_mutex_lock( &p_input->stream.stream_lock );
1013 if( input_InitStream( p_input, 0 ) == -1)
1015 vlc_mutex_unlock( &p_input->stream.stream_lock );
1016 msg_Err( p_input, "cannot init stream" );
1019 if( input_AddProgram( p_input, 0, 0) == NULL )
1021 vlc_mutex_unlock( &p_input->stream.stream_lock );
1022 msg_Err( p_input, "cannot add program" );
1025 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1026 p_input->stream.i_mux_rate = 0;
1027 vlc_mutex_unlock( &p_input->stream.stream_lock );
1029 if( p_sys->f_duration > 1001.0 )
1031 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000.0 );
1032 p_input->stream.i_mux_rate = stream_Size( p_sys->s ) / 50 / i_duration;
1036 msg_Dbg( p_input, "found %d es", p_sys->i_track );
1037 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1039 #define tk p_sys->track[i_track]
1040 if( tk.i_cat == UNKNOWN_ES )
1042 msg_Warn( p_input, "invalid track[%d, n=%d]", i_track, tk.i_number );
1046 tk.p_es = input_AddES( p_input,
1047 p_input->stream.p_selected_program,
1050 tk.psz_language, 0 );
1051 if( tk.i_cat == SPU_ES )
1054 val.psz_string = "UTF-8";
1055 #if defined(HAVE_ICONV)
1056 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1058 var_Set( p_input, "subsdec-encoding", val );
1060 if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) )
1062 if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
1064 msg_Err( p_input, "missing/invalid BITMAPINFOHEADER" );
1065 tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1069 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
1071 p_bih->biSize = GetDWLE( &p_bih->biSize );
1072 p_bih->biWidth = GetDWLE( &p_bih->biWidth );
1073 p_bih->biHeight = GetDWLE( &p_bih->biHeight );
1074 p_bih->biPlanes = GetWLE( &p_bih->biPlanes );
1075 p_bih->biBitCount = GetWLE( &p_bih->biBitCount );
1076 p_bih->biCompression = GetFOURCC( &p_bih->biCompression );
1077 p_bih->biSizeImage = GetDWLE( &p_bih->biSizeImage );
1078 p_bih->biXPelsPerMeter = GetDWLE( &p_bih->biXPelsPerMeter );
1079 p_bih->biYPelsPerMeter = GetDWLE( &p_bih->biYPelsPerMeter );
1080 p_bih->biClrUsed = GetDWLE( &p_bih->biClrUsed );
1081 p_bih->biClrImportant = GetDWLE( &p_bih->biClrImportant );
1084 tk.i_codec = p_bih->biCompression;
1085 tk.p_es->p_bitmapinfoheader = p_bih;
1088 else if( !strcmp( tk.psz_codec, "V_MPEG1" ) ||
1089 !strcmp( tk.psz_codec, "V_MPEG2" ) )
1091 tk.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1093 else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) )
1095 BITMAPINFOHEADER *p_bih;
1097 tk.i_extra_data = sizeof( BITMAPINFOHEADER );
1098 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
1100 p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
1101 memset( p_bih, 0, sizeof( BITMAPINFOHEADER ) );
1102 p_bih->biSize = sizeof( BITMAPINFOHEADER );
1103 p_bih->biWidth = tk.i_width;
1104 p_bih->biHeight= tk.i_height;
1106 if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) )
1108 tk.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1112 tk.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1115 else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
1117 if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1119 msg_Err( p_input, "missing/invalid WAVEFORMATEX" );
1120 tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1124 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;
1126 p_wf->wFormatTag = GetWLE( &p_wf->wFormatTag );
1127 p_wf->nChannels = GetWLE( &p_wf->nChannels );
1128 p_wf->nSamplesPerSec = GetDWLE( &p_wf->nSamplesPerSec );
1129 p_wf->nAvgBytesPerSec = GetDWLE( &p_wf->nAvgBytesPerSec );
1130 p_wf->nBlockAlign = GetWLE( &p_wf->nBlockAlign );
1131 p_wf->wBitsPerSample = GetWLE( &p_wf->wBitsPerSample );
1132 p_wf->cbSize = GetWLE( &p_wf->cbSize );
1134 wf_tag_to_fourcc( p_wf->wFormatTag, &tk.i_codec, NULL );
1135 tk.p_es->p_waveformatex = p_wf;
1138 else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
1139 !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
1140 !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
1142 tk.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1144 else if( !strcmp( tk.psz_codec, "A_AC3" ) )
1146 tk.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1148 else if( !strcmp( tk.psz_codec, "A_DTS" ) )
1150 tk.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
1152 else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
1154 tk.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
1155 tk.i_data_init = tk.i_extra_data;
1156 tk.p_data_init = tk.p_extra_data;
1158 else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1159 !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1161 int i_profile, i_srate;
1162 static int i_sample_rates[] =
1164 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1165 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1169 tk.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1170 /* create data for faad (MP4DecSpecificDescrTag)*/
1172 if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
1176 else if( !strcmp( &tk.psz_codec[12], "LC" ) )
1180 else if( !strcmp( &tk.psz_codec[12], "SSR" ) )
1189 for( i_srate = 0; i_srate < 13; i_srate++ )
1191 if( i_sample_rates[i_srate] == tk.i_samplerate )
1196 msg_Dbg( p_input, "profile=%d srate=%d", i_profile, i_srate );
1198 tk.i_extra_data = sizeof( WAVEFORMATEX ) + 2;
1199 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
1200 p_wf = (WAVEFORMATEX*)tk.p_extra_data;
1202 p_wf->wFormatTag = WAVE_FORMAT_UNKNOWN;
1203 p_wf->nChannels = tk.i_channels;
1204 p_wf->nSamplesPerSec = tk.i_samplerate;
1205 p_wf->nAvgBytesPerSec = 0;
1206 p_wf->nBlockAlign = 0;
1207 p_wf->wBitsPerSample = 0;
1210 tk.p_extra_data[sizeof( WAVEFORMATEX )+ 0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1211 tk.p_extra_data[sizeof( WAVEFORMATEX )+ 1] = ((i_srate & 0x1) << 7) | (tk.i_channels << 3);
1213 tk.p_es->p_waveformatex = p_wf;
1215 else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
1216 !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
1217 !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
1221 tk.i_extra_data = sizeof( WAVEFORMATEX );
1222 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
1224 p_wf = (WAVEFORMATEX*)tk.p_extra_data;
1226 if( !strncmp( &tk.psz_codec[6], "INT", 3 ) )
1228 p_wf->wFormatTag = WAVE_FORMAT_PCM;
1232 p_wf->wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
1234 p_wf->nChannels = tk.i_channels;
1235 p_wf->nSamplesPerSec = tk.i_samplerate;
1236 p_wf->nAvgBytesPerSec = 0;
1237 p_wf->nBlockAlign = ( tk.i_bitspersample + 7 ) / 8 * tk.i_channels;
1238 p_wf->wBitsPerSample = tk.i_bitspersample;
1241 tk.p_es->p_waveformatex = p_wf;
1243 if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
1245 tk.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1249 tk.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1252 else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
1254 tk.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1256 else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) )
1258 tk.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1262 msg_Err( p_input, "unknow codec id=`%s'", tk.psz_codec );
1263 tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1266 tk.p_es->i_fourcc = tk.i_codec;
1270 /* select track all video, one audio, no spu TODO : improve */
1271 b_audio_selected = VLC_FALSE;
1272 i_audio_channel = 0;
1274 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1276 #define tk p_sys->track[i_track]
1280 vlc_mutex_lock( &p_input->stream.stream_lock );
1281 input_SelectES( p_input, tk.p_es );
1282 vlc_mutex_unlock( &p_input->stream.stream_lock );
1286 if( ( !b_audio_selected && config_GetInt( p_input, "audio-channel" ) < 0 ) ||
1287 i_audio_channel == config_GetInt( p_input, "audio-channel" ) )
1289 vlc_mutex_lock( &p_input->stream.stream_lock );
1290 input_SelectES( p_input, tk.p_es );
1291 vlc_mutex_unlock( &p_input->stream.stream_lock );
1293 b_audio_selected = tk.p_es->p_decoder_fifo ? VLC_TRUE : VLC_FALSE;
1298 if( i_spu_channel == config_GetInt( p_input, "spu-channel" ) )
1300 vlc_mutex_lock( &p_input->stream.stream_lock );
1301 input_SelectES( p_input, tk.p_es );
1302 vlc_mutex_unlock( &p_input->stream.stream_lock );
1310 if( !b_audio_selected )
1312 msg_Warn( p_input, "cannot find/select audio track" );
1315 /* add informations */
1316 InformationsCreate( p_input );
1323 stream_Release( p_sys->s );
1325 return VLC_EGENERIC;
1328 /*****************************************************************************
1329 * Close: frees unused data
1330 *****************************************************************************/
1331 static void Close( vlc_object_t *p_this )
1333 input_thread_t *p_input = (input_thread_t *)p_this;
1334 demux_sys_t *p_sys = p_input->p_demux_data;
1338 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1340 #define tk p_sys->track[i_track]
1343 free( tk.psz_codec );
1345 if( tk.psz_language )
1347 free( tk.psz_language );
1351 free( p_sys->track );
1353 if( p_sys->psz_writing_application )
1355 free( p_sys->psz_writing_application );
1357 if( p_sys->psz_muxing_application )
1359 free( p_sys->psz_muxing_application );
1362 delete p_sys->segment;
1367 stream_Release( p_sys->s );
1372 static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1374 demux_sys_t *p_sys = p_input->p_demux_data;
1385 if( p_input->b_die )
1387 return VLC_EGENERIC;
1390 el = p_sys->ep->Get();
1391 i_level = p_sys->ep->GetLevel();
1393 if( el == NULL && *pp_block != NULL )
1395 /* update the index */
1396 #define idx p_sys->index[p_sys->i_index - 1]
1397 if( p_sys->i_index > 0 && idx.i_time == -1 )
1399 idx.i_time = (*pp_block)->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1400 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1408 if( p_sys->ep->GetLevel() > 1 )
1413 msg_Warn( p_input, "EOF" );
1414 return VLC_EGENERIC;
1420 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1422 p_sys->cluster = (KaxCluster*)el;
1424 /* add it to the index */
1425 if( p_sys->i_index == 0 ||
1426 ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
1428 IndexAppendCluster( p_input, p_sys->cluster );
1433 else if( EbmlId( *el ) == KaxCues::ClassInfos.GlobalId )
1435 msg_Warn( p_input, "find KaxCues FIXME" );
1436 return VLC_EGENERIC;
1440 msg_Dbg( p_input, "unknown (%s)", typeid( el ).name() );
1443 else if( i_level == 2 )
1445 if( EbmlId( *el ) == KaxClusterTimecode::ClassInfos.GlobalId )
1447 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1449 ctc.ReadData( p_sys->es->I_O() );
1450 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
1452 else if( EbmlId( *el ) == KaxBlockGroup::ClassInfos.GlobalId )
1457 else if( i_level == 3 )
1459 if( EbmlId( *el ) == KaxBlock::ClassInfos.GlobalId )
1461 *pp_block = (KaxBlock*)el;
1463 (*pp_block)->ReadData( p_sys->es->I_O() );
1464 (*pp_block)->SetParent( *p_sys->cluster );
1468 else if( EbmlId( *el ) == KaxBlockDuration::ClassInfos.GlobalId )
1470 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1472 dur.ReadData( p_sys->es->I_O() );
1473 *pi_duration = uint64( dur );
1475 else if( EbmlId( *el ) == KaxReferenceBlock::ClassInfos.GlobalId )
1477 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1479 ref.ReadData( p_sys->es->I_O() );
1480 if( *pi_ref1 == -1 )
1482 *pi_ref1 = int64( ref );
1486 *pi_ref2 = int64( ref );
1492 msg_Err( p_input, "invalid level = %d", i_level );
1493 return VLC_EGENERIC;
1498 static pes_packet_t *MemToPES( input_thread_t *p_input, uint8_t *p_mem, int i_mem )
1500 pes_packet_t *p_pes;
1501 data_packet_t *p_data;
1503 if( ( p_pes = input_NewPES( p_input->p_method_data ) ) == NULL )
1508 p_data = input_NewPacket( p_input->p_method_data, i_mem);
1510 memcpy( p_data->p_payload_start, p_mem, i_mem );
1511 p_data->p_payload_end = p_data->p_payload_start + i_mem;
1513 p_pes->p_first = p_pes->p_last = p_data;
1514 p_pes->i_nb_data = 1;
1515 p_pes->i_pes_size = i_mem;
1520 static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts, mtime_t i_duration )
1522 demux_sys_t *p_sys = p_input->p_demux_data;
1527 #define tk p_sys->track[i_track]
1528 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1530 if( tk.i_number == block->TrackNum() )
1536 if( i_track >= p_sys->i_track )
1538 msg_Err( p_input, "invalid track number=%d", block->TrackNum() );
1542 if( tk.p_es->p_decoder_fifo == NULL )
1544 tk.b_inited = VLC_FALSE;
1548 if( tk.i_cat == AUDIO_ES && p_input->stream.control.b_mute )
1553 /* First send init data */
1554 if( !tk.b_inited && tk.i_data_init > 0 )
1556 pes_packet_t *p_init;
1558 msg_Dbg( p_input, "sending header (%d bytes)", tk.i_data_init );
1560 if( tk.i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
1566 /* XXX hack split the 3 headers */
1567 if( tk.p_data_init[0] != 0x02 )
1569 msg_Err( p_input, "invalid vorbis header" );
1572 for( i = 0; i < 2; i++ )
1575 while( i_offset < tk.i_data_init )
1577 i_size[i] += tk.p_data_init[i_offset];
1578 if( tk.p_data_init[i_offset++] != 0xff )
1584 i_size[0] = __MIN( i_size[0], tk.i_data_init - i_offset );
1585 i_size[1] = __MIN( i_size[1], tk.i_data_init - i_offset - i_size[0] );
1586 i_size[2] = tk.i_data_init - i_offset - i_size[0] - i_size[1];
1588 p_init = MemToPES( p_input, &tk.p_data_init[i_offset], i_size[0] );
1591 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1593 p_init = MemToPES( p_input, &tk.p_data_init[i_offset+i_size[0]], i_size[1] );
1596 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1598 p_init = MemToPES( p_input, &tk.p_data_init[i_offset+i_size[0]+i_size[1]], i_size[2] );
1601 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1606 p_init = MemToPES( p_input, tk.p_data_init, tk.i_data_init );
1609 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1613 tk.b_inited = VLC_TRUE;
1616 for( i = 0; i < block->NumberFrames(); i++ )
1618 pes_packet_t *p_pes;
1619 DataBuffer &data = block->GetBuffer(i);
1621 p_pes = MemToPES( p_input, data.Buffer(), data.Size() );
1627 p_pes->i_pts = i_pts;
1628 p_pes->i_dts = i_pts;
1630 if( tk.i_cat == SPU_ES )
1632 if( i_duration > 0 )
1634 /* FIXME not sure about that */
1635 p_pes->i_dts += i_duration * 1000;// * (mtime_t) 1000 / p_sys->i_timescale;
1642 if( p_pes->p_first && p_pes->i_pes_size > 0 )
1644 p_pes->p_first->p_payload_end[-1] = '\0';
1646 if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) )
1648 /* remove all fields before text for now */
1649 char *start = (char*)p_pes->p_first->p_payload_start;
1652 while( *start && i_comma < 8 )
1654 if( *start++ == ',' )
1659 memmove( p_pes->p_first->p_payload_start, start,
1660 strlen( start) + 1 );
1664 input_DecodePES( tk.p_es->p_decoder_fifo, p_pes );
1666 /* use time stamp only for first block */
1673 static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent)
1675 demux_sys_t *p_sys = p_input->p_demux_data;
1678 int64_t i_block_duration;
1679 int64_t i_block_ref1;
1680 int64_t i_block_ref2;
1683 int i_track_skipping;
1686 msg_Dbg( p_input, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1687 if( i_date < 0 && i_percent < 0 )
1693 p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
1694 p_sys->cluster = NULL;
1696 /* seek without index or without date */
1697 if( config_GetInt( p_input, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
1699 int64_t i_pos = i_percent * stream_Size( p_sys->s ) / 100;
1701 msg_Dbg( p_input, "imprecise way of seeking" );
1702 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1704 if( p_sys->index[i_index].i_position >= i_pos)
1709 if( i_index == p_sys->i_index )
1714 p_sys->in->setFilePointer( p_sys->index[i_index].i_position, seek_beginning );
1716 if( p_sys->index[i_index].i_position < i_pos )
1720 msg_Warn( p_input, "searching for cluster, could take some time" );
1722 /* search a cluster */
1723 while( ( el = p_sys->ep->Get() ) != NULL )
1725 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1727 KaxCluster *cluster = (KaxCluster*)el;
1729 /* add it to the index */
1730 IndexAppendCluster( p_input, cluster );
1732 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1734 p_sys->cluster = cluster;
1744 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1746 if( p_sys->index[i_index].i_time >= i_date )
1757 msg_Dbg( p_input, "seek got "I64Fd" (%d%%)",
1758 p_sys->index[i_index].i_time,
1759 (int)( 100 * p_sys->index[i_index].i_position /
1760 stream_Size( p_sys->s ) ) );
1762 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1766 /* now parse until key frame */
1767 #define tk p_sys->track[i_track]
1768 i_track_skipping = 0;
1769 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1771 if( tk.i_cat == VIDEO_ES )
1773 tk.b_search_keyframe = VLC_TRUE;
1778 while( i_track_skipping > 0 )
1780 if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1782 msg_Warn( p_input, "cannot get block EOF?" );
1787 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1789 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1791 if( tk.i_number == block->TrackNum() )
1797 if( i_track < p_sys->i_track )
1799 if( tk.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1801 tk.b_search_keyframe = VLC_FALSE;
1804 if( tk.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1806 BlockDecode( p_input, block, 0, 0 );
1815 /*****************************************************************************
1816 * Demux: reads and demuxes data packets
1817 *****************************************************************************
1818 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1819 *****************************************************************************/
1820 static int Demux( input_thread_t * p_input )
1822 demux_sys_t *p_sys = p_input->p_demux_data;
1823 mtime_t i_start_pts;
1824 int i_block_count = 0;
1827 int64_t i_block_duration;
1828 int64_t i_block_ref1;
1829 int64_t i_block_ref2;
1831 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1833 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
1834 mtime_t i_date = -1;
1837 if( i_duration > 0 )
1839 i_date = (mtime_t)1000000 *
1840 (mtime_t)i_duration*
1841 (mtime_t)p_sys->in->getFilePointer() /
1842 (mtime_t)stream_Size( p_sys->s );
1844 if( stream_Size( p_sys->s ) > 0 )
1846 i_percent = 100 * p_sys->in->getFilePointer() /
1847 stream_Size( p_sys->s );
1850 Seek( p_input, i_date, i_percent);
1859 if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1861 msg_Warn( p_input, "cannot get block EOF?" );
1866 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1868 if( p_sys->i_pts > 0 )
1870 input_ClockManageRef( p_input,
1871 p_input->stream.p_selected_program,
1872 p_sys->i_pts * 9 / 100 );
1875 i_pts = input_ClockGetTS( p_input,
1876 p_input->stream.p_selected_program,
1877 p_sys->i_pts * 9 / 100 );
1881 BlockDecode( p_input, block, i_pts, i_block_duration );
1886 if( i_start_pts == -1 )
1888 i_start_pts = p_sys->i_pts;
1890 else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1899 /*****************************************************************************
1901 *****************************************************************************/
1902 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1908 uint32_t vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1910 if( i_size <= 0 || mb_eof )
1915 return stream_Read( s, p_buffer, i_size );
1917 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1923 case seek_beginning:
1927 i_pos = stream_Size( s ) - i_offset;
1930 i_pos= stream_Tell( s ) + i_offset;
1934 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1941 if( stream_Seek( s, i_pos ) )
1947 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1951 uint64_t vlc_stream_io_callback::getFilePointer( void )
1953 return stream_Tell( s );
1955 void vlc_stream_io_callback::close( void )
1961 /*****************************************************************************
1962 * Ebml Stream parser
1963 *****************************************************************************/
1964 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1972 for( i = 1; i < 6; i++ )
1978 mb_keep = VLC_FALSE;
1981 EbmlParser::~EbmlParser( void )
1985 for( i = 1; i < mi_level; i++ )
1991 mb_keep = VLC_FALSE;
1995 void EbmlParser::Up( void )
1997 if( mi_user_level == mi_level )
1999 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
2005 void EbmlParser::Down( void )
2011 void EbmlParser::Keep( void )
2016 int EbmlParser::GetLevel( void )
2018 return mi_user_level;
2021 EbmlElement *EbmlParser::Get( void )
2025 if( mi_user_level != mi_level )
2031 EbmlElement *ret = m_got;
2037 if( m_el[mi_level] )
2039 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
2042 delete m_el[mi_level];
2044 mb_keep = VLC_FALSE;
2047 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
2058 delete m_el[mi_level - 1];
2059 m_got = m_el[mi_level -1] = m_el[mi_level];
2060 m_el[mi_level] = NULL;
2067 else if( m_el[mi_level] == NULL )
2069 fprintf( stderr," m_el[mi_level] == NULL\n" );
2072 return m_el[mi_level];
2076 /*****************************************************************************
2078 * * LoadCues : load the cues element and update index
2080 * * LoadTags : load ... the tags element
2082 * * InformationsCreate : create all informations, load tags if present
2084 *****************************************************************************/
2085 static void LoadCues( input_thread_t *p_input )
2087 demux_sys_t *p_sys = p_input->p_demux_data;
2088 int64_t i_sav_position = p_sys->in->getFilePointer();
2090 EbmlElement *el, *cues;
2092 msg_Dbg( p_input, "loading cues" );
2093 p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
2094 cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
2098 msg_Err( p_input, "cannot load cues (broken seekhead or file)" );
2102 ep = new EbmlParser( p_sys->es, cues );
2103 while( ( el = ep->Get() ) != NULL )
2105 if( EbmlId( *el ) == KaxCuePoint::ClassInfos.GlobalId )
2107 #define idx p_sys->index[p_sys->i_index]
2110 idx.i_block_number= -1;
2111 idx.i_position = -1;
2113 idx.b_key = VLC_TRUE;
2116 while( ( el = ep->Get() ) != NULL )
2118 if( EbmlId( *el ) == KaxCueTime::ClassInfos.GlobalId )
2120 KaxCueTime &ctime = *(KaxCueTime*)el;
2122 ctime.ReadData( p_sys->es->I_O() );
2124 idx.i_time = uint64( ctime ) * (mtime_t)1000000000 / p_sys->i_timescale;
2126 else if( EbmlId( *el ) == KaxCueTrackPositions::ClassInfos.GlobalId )
2129 while( ( el = ep->Get() ) != NULL )
2131 if( EbmlId( *el ) == KaxCueTrack::ClassInfos.GlobalId )
2133 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
2135 ctrack.ReadData( p_sys->es->I_O() );
2136 idx.i_track = uint16( ctrack );
2138 else if( EbmlId( *el ) == KaxCueClusterPosition::ClassInfos.GlobalId )
2140 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
2142 ccpos.ReadData( p_sys->es->I_O() );
2143 idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
2145 else if( EbmlId( *el ) == KaxCueBlockNumber::ClassInfos.GlobalId )
2147 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2149 cbnum.ReadData( p_sys->es->I_O() );
2150 idx.i_block_number = uint32( cbnum );
2154 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2161 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2166 msg_Dbg( p_input, " * added time="I64Fd" pos="I64Fd
2167 " track=%d bnum=%d", idx.i_time, idx.i_position,
2168 idx.i_track, idx.i_block_number );
2171 if( p_sys->i_index >= p_sys->i_index_max )
2173 p_sys->i_index_max += 1024;
2174 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2180 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2186 p_sys->b_cues = VLC_TRUE;
2188 msg_Dbg( p_input, "loading cues done." );
2189 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2192 static void LoadTags( input_thread_t *p_input )
2194 demux_sys_t *p_sys = p_input->p_demux_data;
2195 int64_t i_sav_position = p_sys->in->getFilePointer();
2197 EbmlElement *el, *tags;
2199 msg_Dbg( p_input, "loading tags" );
2200 p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
2201 tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2205 msg_Err( p_input, "cannot load tags (broken seekhead or file)" );
2209 msg_Dbg( p_input, "Tags" );
2210 ep = new EbmlParser( p_sys->es, tags );
2211 while( ( el = ep->Get() ) != NULL )
2213 if( EbmlId( *el ) == KaxTag::ClassInfos.GlobalId )
2215 msg_Dbg( p_input, "+ Tag" );
2217 while( ( el = ep->Get() ) != NULL )
2219 if( EbmlId( *el ) == KaxTagTargets::ClassInfos.GlobalId )
2221 msg_Dbg( p_input, "| + Targets" );
2223 while( ( el = ep->Get() ) != NULL )
2225 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2229 else if( EbmlId( *el ) == KaxTagGeneral::ClassInfos.GlobalId )
2231 msg_Dbg( p_input, "| + General" );
2233 while( ( el = ep->Get() ) != NULL )
2235 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2239 else if( EbmlId( *el ) == KaxTagGenres::ClassInfos.GlobalId )
2241 msg_Dbg( p_input, "| + Genres" );
2243 while( ( el = ep->Get() ) != NULL )
2245 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2249 else if( EbmlId( *el ) == KaxTagAudioSpecific::ClassInfos.GlobalId )
2251 msg_Dbg( p_input, "| + Audio Specific" );
2253 while( ( el = ep->Get() ) != NULL )
2255 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2259 else if( EbmlId( *el ) == KaxTagImageSpecific::ClassInfos.GlobalId )
2261 msg_Dbg( p_input, "| + Images Specific" );
2263 while( ( el = ep->Get() ) != NULL )
2265 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2269 else if( EbmlId( *el ) == KaxTagMultiComment::ClassInfos.GlobalId )
2271 msg_Dbg( p_input, "| + Multi Comment" );
2273 else if( EbmlId( *el ) == KaxTagMultiCommercial::ClassInfos.GlobalId )
2275 msg_Dbg( p_input, "| + Multi Commercial" );
2277 else if( EbmlId( *el ) == KaxTagMultiDate::ClassInfos.GlobalId )
2279 msg_Dbg( p_input, "| + Multi Date" );
2281 else if( EbmlId( *el ) == KaxTagMultiEntity::ClassInfos.GlobalId )
2283 msg_Dbg( p_input, "| + Multi Entity" );
2285 else if( EbmlId( *el ) == KaxTagMultiIdentifier::ClassInfos.GlobalId )
2287 msg_Dbg( p_input, "| + Multi Identifier" );
2289 else if( EbmlId( *el ) == KaxTagMultiLegal::ClassInfos.GlobalId )
2291 msg_Dbg( p_input, "| + Multi Legal" );
2293 else if( EbmlId( *el ) == KaxTagMultiTitle::ClassInfos.GlobalId )
2295 msg_Dbg( p_input, "| + Multi Title" );
2299 msg_Dbg( p_input, "| + Unknown (%s)", typeid( *el ).name() );
2306 msg_Dbg( p_input, "+ Unknown (%s)", typeid( *el ).name() );
2312 msg_Dbg( p_input, "loading tags done." );
2313 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2316 static void InformationsCreate( input_thread_t *p_input )
2318 demux_sys_t *p_sys = p_input->p_demux_data;
2319 input_info_category_t *p_cat;
2322 p_cat = input_InfoCategory( p_input, "Matroska" );
2323 if( p_sys->f_duration > 1000.1 )
2325 int64_t i_sec = (int64_t)p_sys->f_duration / 1000;
2329 m = ( i_sec / 60 ) % 60;
2332 input_AddInfo( p_cat, _("Duration"), "%d:%2.2d:%2.2d" , h, m, s );
2335 if( p_sys->psz_title )
2337 input_AddInfo( p_cat, _("Title"), "%s" ,p_sys->psz_title );
2339 if( p_sys->psz_date_utc )
2341 input_AddInfo( p_cat, _("Date UTC"), "%s" ,p_sys->psz_date_utc );
2343 if( p_sys->psz_segment_filename )
2345 input_AddInfo( p_cat, _("Segment Filename"), "%s" ,p_sys->psz_segment_filename );
2347 if( p_sys->psz_muxing_application )
2349 input_AddInfo( p_cat, _("Muxing Application"), "%s" ,p_sys->psz_muxing_application );
2351 if( p_sys->psz_writing_application )
2353 input_AddInfo( p_cat, _("Writing Application"), "%s" ,p_sys->psz_writing_application );
2355 input_AddInfo( p_cat, _("Number of streams"), "%d" , p_sys->i_track );
2357 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
2359 char psz_cat[strlen( "Stream " ) + 10];
2360 #define tk p_sys->track[i_track]
2362 sprintf( psz_cat, "Stream %d", i_track );
2363 p_cat = input_InfoCategory( p_input, psz_cat);
2366 input_AddInfo( p_cat, _("Name"), "%s", tk.psz_name );
2368 if( tk.psz_codec_name )
2370 input_AddInfo( p_cat, _("Codec Name"), "%s", tk.psz_codec_name );
2372 if( tk.psz_codec_settings )
2374 input_AddInfo( p_cat, _("Codec Setting"), "%s", tk.psz_codec_settings );
2376 if( tk.psz_codec_info_url )
2378 input_AddInfo( p_cat, _("Codec Info"), "%s", tk.psz_codec_info_url );
2380 if( tk.psz_codec_download_url )
2382 input_AddInfo( p_cat, _("Codec Download"), "%s", tk.psz_codec_download_url );
2388 input_AddInfo( p_cat, _("Type"), _("Audio") );
2389 input_AddInfo( p_cat, _("Codec"), "%.4s (%s)", (char*)&tk.i_codec, tk.psz_codec );
2390 if( tk.i_channels > 0 )
2392 input_AddInfo( p_cat, _("Channels"), "%d", tk.i_channels );
2394 if( tk.i_samplerate > 0 )
2396 input_AddInfo( p_cat, _("Sample Rate"), "%d", tk.i_samplerate );
2398 if( tk.i_bitspersample )
2400 input_AddInfo( p_cat, _("Bits Per Sample"), "%d", tk.i_bitspersample );
2404 input_AddInfo( p_cat, _("Type"), _("Video") );
2405 input_AddInfo( p_cat, _("Codec"), "%.4s (%s)", (char*)&tk.i_codec, tk.psz_codec );
2406 if( tk.i_width > 0 && tk.i_height )
2408 input_AddInfo( p_cat, _("Resolution"), "%dx%d", tk.i_width, tk.i_height );
2410 if( tk.i_display_width > 0 && tk.i_display_height )
2412 input_AddInfo( p_cat, _("Display Resolution"), "%dx%d", tk.i_display_width, tk.i_display_height );
2414 if( tk.f_fps > 0.1 )
2416 input_AddInfo( p_cat, _("Frame Per Second"), "%.3f", tk.f_fps );
2420 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
2421 input_AddInfo( p_cat, _("Codec"), "%s", tk.psz_codec );
2428 if( p_sys->i_tags_position >= 0 )
2430 vlc_bool_t b_seekable;
2432 stream_Control( p_sys->s, STREAM_CAN_FASTSEEK, &b_seekable );
2435 LoadTags( p_input );
2441 /*****************************************************************************
2443 *****************************************************************************/
2445 static void IndexAppendCluster( input_thread_t *p_input, KaxCluster *cluster )
2447 demux_sys_t *p_sys = p_input->p_demux_data;
2449 #define idx p_sys->index[p_sys->i_index]
2451 idx.i_block_number= -1;
2452 idx.i_position = cluster->GetElementPosition();
2454 idx.b_key = VLC_TRUE;
2457 if( p_sys->i_index >= p_sys->i_index_max )
2459 p_sys->i_index_max += 1024;
2460 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2465 static char * UTF8ToStr( const UTFstring &u )
2474 p = dst = (char*)malloc( i_src + 1);
2493 static char *LanguageGetName ( const char *psz_code )
2495 const iso639_lang_t *pl;
2497 if( strlen( psz_code ) == 2 )
2499 pl = GetLang_1( psz_code );
2501 else if( strlen( psz_code ) == 3 )
2503 pl = GetLang_2B( psz_code );
2504 if( !strcmp( pl->psz_iso639_1, "??" ) )
2506 pl = GetLang_2T( psz_code );
2511 return strdup( psz_code );
2514 if( !strcmp( pl->psz_iso639_1, "??" ) )
2516 return strdup( psz_code );
2520 if( *pl->psz_native_name )
2522 return strdup( pl->psz_native_name );
2524 return strdup( pl->psz_eng_name );