1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mkv.cpp,v 1.48 2003/12/02 01:54:30 rocky 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 *****************************************************************************/
174 vlc_bool_t b_default;
175 vlc_bool_t b_enabled;
179 uint8_t *p_extra_data;
183 uint64_t i_default_duration;
184 float f_timecodescale;
192 /* data to be send first */
194 uint8_t *p_data_init;
196 /* hack : it's for seek */
197 vlc_bool_t b_search_keyframe;
200 char *psz_codec_name;
201 char *psz_codec_settings;
202 char *psz_codec_info_url;
203 char *psz_codec_download_url;
220 vlc_stream_io_callback *in;
225 uint64_t i_timescale;
227 /* duration of the segment */
235 int64_t i_cues_position;
236 int64_t i_chapters_position;
237 int64_t i_tags_position;
251 char *psz_muxing_application;
252 char *psz_writing_application;
253 char *psz_segment_filename;
258 #define MKVD_TIMECODESCALE 1000000
260 static void IndexAppendCluster ( input_thread_t *p_input, KaxCluster *cluster );
261 static char *UTF8ToStr ( const UTFstring &u );
262 static void LoadCues ( input_thread_t *);
263 static void InformationsCreate ( input_thread_t *p_input );
265 static char *LanguageGetName ( const char *psz_code );
267 /*****************************************************************************
268 * Open: initializes matroska demux structures
269 *****************************************************************************/
270 static int Open( vlc_object_t * p_this )
272 input_thread_t *p_input = (input_thread_t *)p_this;
278 EbmlElement *el = NULL, *el1 = NULL, *el2 = NULL, *el3 = NULL, *el4 = NULL;
280 /* Set the demux function */
281 p_input->pf_demux = Demux;
282 p_input->pf_demux_control = demux_vaControlDefault;
284 /* peek the begining */
285 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
287 msg_Warn( p_input, "cannot peek" );
291 /* is a valid file */
292 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
293 p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
295 msg_Warn( p_input, "matroska module discarded "
296 "(invalid header 0x%.2x%.2x%.2x%.2x)",
297 p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
301 p_input->p_demux_data = p_sys = (demux_sys_t*)malloc(sizeof( demux_sys_t ));
302 memset( p_sys, 0, sizeof( demux_sys_t ) );
304 p_sys->in = new vlc_stream_io_callback( p_input->s );
305 p_sys->es = new EbmlStream( *p_sys->in );
306 p_sys->f_duration = -1;
307 p_sys->i_timescale = MKVD_TIMECODESCALE;
309 p_sys->track = (mkv_track_t*)malloc( sizeof( mkv_track_t ) );
311 p_sys->i_cues_position = -1;
312 p_sys->i_chapters_position = -1;
313 p_sys->i_tags_position = -1;
315 p_sys->b_cues = VLC_FALSE;
317 p_sys->i_index_max = 1024;
318 p_sys->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
319 p_sys->i_index_max );
321 p_sys->psz_muxing_application = NULL;
322 p_sys->psz_writing_application = NULL;
323 p_sys->psz_segment_filename = NULL;
324 p_sys->psz_title = NULL;
325 p_sys->psz_date_utc = NULL;;
327 if( p_sys->es == NULL )
329 msg_Err( p_input, "failed to create EbmlStream" );
334 /* Find the EbmlHead element */
335 el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
338 msg_Err( p_input, "cannot find EbmlHead" );
341 msg_Dbg( p_input, "EbmlHead" );
343 el->SkipData( *p_sys->es, el->Generic().Context );
347 el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
350 msg_Err( p_input, "cannot find KaxSegment" );
353 msg_Dbg( p_input, "+ Segment" );
354 p_sys->segment = (KaxSegment*)el;
355 p_sys->cluster = NULL;
357 p_sys->ep = new EbmlParser( p_sys->es, el );
359 while( ( el1 = p_sys->ep->Get() ) != NULL )
361 if( EbmlId( *el1 ) == KaxInfo::ClassInfos.GlobalId )
363 msg_Dbg( p_input, "| + Informations" );
366 while( ( el2 = p_sys->ep->Get() ) != NULL )
368 if( EbmlId( *el2 ) == KaxTimecodeScale::ClassInfos.GlobalId )
370 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)el2;
372 tcs.ReadData( p_sys->es->I_O() );
373 p_sys->i_timescale = uint64(tcs);
375 msg_Dbg( p_input, "| | + TimecodeScale="I64Fd,
376 p_sys->i_timescale );
378 else if( EbmlId( *el2 ) == KaxDuration::ClassInfos.GlobalId )
380 KaxDuration &dur = *(KaxDuration*)el2;
382 dur.ReadData( p_sys->es->I_O() );
383 p_sys->f_duration = float(dur);
385 msg_Dbg( p_input, "| | + Duration=%f",
388 else if( EbmlId( *el2 ) == KaxMuxingApp::ClassInfos.GlobalId )
390 KaxMuxingApp &mapp = *(KaxMuxingApp*)el2;
392 mapp.ReadData( p_sys->es->I_O() );
394 p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
396 msg_Dbg( p_input, "| | + Muxing Application=%s",
397 p_sys->psz_muxing_application );
399 else if( EbmlId( *el2 ) == KaxWritingApp::ClassInfos.GlobalId )
401 KaxWritingApp &wapp = *(KaxWritingApp*)el2;
403 wapp.ReadData( p_sys->es->I_O() );
405 p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
407 msg_Dbg( p_input, "| | + Wrinting Application=%s",
408 p_sys->psz_writing_application );
410 else if( EbmlId( *el2 ) == KaxSegmentFilename::ClassInfos.GlobalId )
412 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)el2;
414 sfn.ReadData( p_sys->es->I_O() );
416 p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
418 msg_Dbg( p_input, "| | + Segment Filename=%s",
419 p_sys->psz_segment_filename );
421 else if( EbmlId( *el2 ) == KaxTitle::ClassInfos.GlobalId )
423 KaxTitle &title = *(KaxTitle*)el2;
425 title.ReadData( p_sys->es->I_O() );
427 p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
429 msg_Dbg( p_input, "| | + Title=%s", p_sys->psz_title );
432 else if( EbmlId( *el2 ) == KaxDateUTC::ClassInfos.GlobalId )
434 KaxDateUTC &date = *(KaxDateUTC*)el2;
439 date.ReadData( p_sys->es->I_O() );
441 i_date = date.GetEpochDate();
442 memset( buffer, 0, 256 );
443 if( gmtime_r( &i_date, &tmres ) &&
444 asctime_r( &tmres, buffer ) )
446 buffer[strlen( buffer)-1]= '\0';
447 p_sys->psz_date_utc = strdup( buffer );
448 msg_Dbg( p_input, "| | + Date=%s", p_sys->psz_date_utc );
454 msg_Dbg( p_input, "| | + Unknown (%s)", typeid(*el2).name() );
459 else if( EbmlId( *el1 ) == KaxTracks::ClassInfos.GlobalId )
461 msg_Dbg( p_input, "| + Tracks" );
464 while( ( el2 = p_sys->ep->Get() ) != NULL )
466 if( EbmlId( *el2 ) == KaxTrackEntry::ClassInfos.GlobalId )
468 msg_Dbg( p_input, "| | + Track Entry" );
471 p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
472 #define tk p_sys->track[p_sys->i_track - 1]
473 memset( &tk, 0, sizeof( mkv_track_t ) );
475 es_format_Init( &tk.fmt, UNKNOWN_ES, 0 );
476 tk.fmt.psz_language = strdup("English");
477 tk.fmt.psz_description = NULL;
479 tk.b_default = VLC_TRUE;
480 tk.b_enabled = VLC_TRUE;
481 tk.i_number = p_sys->i_track - 1;
483 tk.p_extra_data = NULL;
485 tk.i_default_duration = 0;
486 tk.f_timecodescale = 1.0;
488 tk.b_inited = VLC_FALSE;
490 tk.p_data_init = NULL;
492 tk.psz_codec_name = NULL;
493 tk.psz_codec_settings = NULL;
494 tk.psz_codec_info_url = NULL;
495 tk.psz_codec_download_url = NULL;
499 while( ( el3 = p_sys->ep->Get() ) != NULL )
501 if( EbmlId( *el3 ) == KaxTrackNumber::ClassInfos.GlobalId )
503 KaxTrackNumber &tnum = *(KaxTrackNumber*)el3;
504 tnum.ReadData( p_sys->es->I_O() );
506 tk.i_number = uint32( tnum );
507 msg_Dbg( p_input, "| | | + Track Number=%u",
510 else if( EbmlId( *el3 ) == KaxTrackUID::ClassInfos.GlobalId )
512 KaxTrackUID &tuid = *(KaxTrackUID*)el3;
513 tuid.ReadData( p_sys->es->I_O() );
515 msg_Dbg( p_input, "| | | + Track UID=%u",
518 else if( EbmlId( *el3 ) == KaxTrackType::ClassInfos.GlobalId )
521 KaxTrackType &ttype = *(KaxTrackType*)el3;
522 ttype.ReadData( p_sys->es->I_O() );
523 switch( uint8(ttype) )
527 tk.fmt.i_cat = AUDIO_ES;
531 tk.fmt.i_cat = VIDEO_ES;
534 psz_type = "subtitle";
535 tk.fmt.i_cat = SPU_ES;
538 psz_type = "unknown";
539 tk.fmt.i_cat = UNKNOWN_ES;
543 msg_Dbg( p_input, "| | | + Track Type=%s",
546 else if( EbmlId( *el3 ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
548 KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)el3;
549 fenb.ReadData( p_sys->es->I_O() );
551 tk.b_enabled = uint32( fenb );
552 msg_Dbg( p_input, "| | | + Track Enabled=%u",
555 else if( EbmlId( *el3 ) == KaxTrackFlagDefault::ClassInfos.GlobalId )
557 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)el3;
558 fdef.ReadData( p_sys->es->I_O() );
560 tk.b_default = uint32( fdef );
561 msg_Dbg( p_input, "| | | + Track Default=%u",
564 else if( EbmlId( *el3 ) == KaxTrackFlagLacing::ClassInfos.GlobalId )
566 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)el3;
567 lac.ReadData( p_sys->es->I_O() );
569 msg_Dbg( p_input, "| | | + Track Lacing=%d",
572 else if( EbmlId( *el3 ) == KaxTrackMinCache::ClassInfos.GlobalId )
574 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)el3;
575 cmin.ReadData( p_sys->es->I_O() );
577 msg_Dbg( p_input, "| | | + Track MinCache=%d",
580 else if( EbmlId( *el3 ) == KaxTrackMaxCache::ClassInfos.GlobalId )
582 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)el3;
583 cmax.ReadData( p_sys->es->I_O() );
585 msg_Dbg( p_input, "| | | + Track MaxCache=%d",
588 else if( EbmlId( *el3 ) == KaxTrackDefaultDuration::ClassInfos.GlobalId )
590 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)el3;
591 defd.ReadData( p_sys->es->I_O() );
593 tk.i_default_duration = uint64(defd);
594 msg_Dbg( p_input, "| | | + Track Default Duration="I64Fd, uint64(defd) );
596 else if( EbmlId( *el3 ) == KaxTrackTimecodeScale::ClassInfos.GlobalId )
598 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)el3;
599 ttcs.ReadData( p_sys->es->I_O() );
601 tk.f_timecodescale = float( ttcs );
602 msg_Dbg( p_input, "| | | + Track TimeCodeScale=%f", tk.f_timecodescale );
604 else if( EbmlId( *el3 ) == KaxTrackName::ClassInfos.GlobalId )
606 KaxTrackName &tname = *(KaxTrackName*)el3;
607 tname.ReadData( p_sys->es->I_O() );
609 tk.fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
610 msg_Dbg( p_input, "| | | + Track Name=%s",
611 tk.fmt.psz_description );
613 else if( EbmlId( *el3 ) == KaxTrackLanguage::ClassInfos.GlobalId )
615 KaxTrackLanguage &lang = *(KaxTrackLanguage*)el3;
616 lang.ReadData( p_sys->es->I_O() );
618 tk.fmt.psz_language =
619 LanguageGetName( string( lang ).c_str() );
621 "| | | + Track Language=`%s'(%s) ",
622 tk.fmt.psz_language, string( lang ).c_str() );
624 else if( EbmlId( *el3 ) == KaxCodecID::ClassInfos.GlobalId )
626 KaxCodecID &codecid = *(KaxCodecID*)el3;
627 codecid.ReadData( p_sys->es->I_O() );
629 tk.psz_codec = strdup( string( codecid ).c_str() );
630 msg_Dbg( p_input, "| | | + Track CodecId=%s",
631 string( codecid ).c_str() );
633 else if( EbmlId( *el3 ) == KaxCodecPrivate::ClassInfos.GlobalId )
635 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)el3;
636 cpriv.ReadData( p_sys->es->I_O() );
638 tk.i_extra_data = cpriv.GetSize();
639 if( tk.i_extra_data > 0 )
641 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
642 memcpy( tk.p_extra_data, cpriv.GetBuffer(), tk.i_extra_data );
644 msg_Dbg( p_input, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
646 else if( EbmlId( *el3 ) == KaxCodecName::ClassInfos.GlobalId )
648 KaxCodecName &cname = *(KaxCodecName*)el3;
649 cname.ReadData( p_sys->es->I_O() );
651 tk.psz_codec_name = UTF8ToStr( UTFstring( cname ) );
652 msg_Dbg( p_input, "| | | + Track Codec Name=%s", tk.psz_codec_name );
654 else if( EbmlId( *el3 ) == KaxCodecSettings::ClassInfos.GlobalId )
656 KaxCodecSettings &cset = *(KaxCodecSettings*)el3;
657 cset.ReadData( p_sys->es->I_O() );
659 tk.psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
660 msg_Dbg( p_input, "| | | + Track Codec Settings=%s", tk.psz_codec_settings );
662 else if( EbmlId( *el3 ) == KaxCodecInfoURL::ClassInfos.GlobalId )
664 KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)el3;
665 ciurl.ReadData( p_sys->es->I_O() );
667 tk.psz_codec_info_url = strdup( string( ciurl ).c_str() );
668 msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_info_url );
670 else if( EbmlId( *el3 ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
672 KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)el3;
673 cdurl.ReadData( p_sys->es->I_O() );
675 tk.psz_codec_download_url = strdup( string( cdurl ).c_str() );
676 msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_download_url );
678 else if( EbmlId( *el3 ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
680 KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)el3;
681 cdall.ReadData( p_sys->es->I_O() );
683 msg_Dbg( p_input, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
685 else if( EbmlId( *el3 ) == KaxTrackOverlay::ClassInfos.GlobalId )
687 KaxTrackOverlay &tovr = *(KaxTrackOverlay*)el3;
688 tovr.ReadData( p_sys->es->I_O() );
690 msg_Dbg( p_input, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
692 else if( EbmlId( *el3 ) == KaxTrackVideo::ClassInfos.GlobalId )
694 msg_Dbg( p_input, "| | | + Track Video" );
699 while( ( el4 = p_sys->ep->Get() ) != NULL )
701 if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
703 KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
704 fint.ReadData( p_sys->es->I_O() );
706 msg_Dbg( p_input, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
708 else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
710 KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
711 stereo.ReadData( p_sys->es->I_O() );
713 msg_Dbg( p_input, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
715 else if( EbmlId( *el4 ) == KaxVideoPixelWidth::ClassInfos.GlobalId )
717 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)el4;
718 vwidth.ReadData( p_sys->es->I_O() );
720 tk.fmt.video.i_width = uint16( vwidth );
721 msg_Dbg( p_input, "| | | | + width=%d", uint16( vwidth ) );
723 else if( EbmlId( *el4 ) == KaxVideoPixelHeight::ClassInfos.GlobalId )
725 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)el4;
726 vheight.ReadData( p_sys->es->I_O() );
728 tk.fmt.video.i_height = uint16( vheight );
729 msg_Dbg( p_input, "| | | | + height=%d", uint16( vheight ) );
731 else if( EbmlId( *el4 ) == KaxVideoDisplayWidth::ClassInfos.GlobalId )
733 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)el4;
734 vwidth.ReadData( p_sys->es->I_O() );
736 tk.fmt.video.i_visible_width = uint16( vwidth );
737 msg_Dbg( p_input, "| | | | + display width=%d", uint16( vwidth ) );
739 else if( EbmlId( *el4 ) == KaxVideoDisplayHeight::ClassInfos.GlobalId )
741 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)el4;
742 vheight.ReadData( p_sys->es->I_O() );
744 tk.fmt.video.i_visible_height = uint16( vheight );
745 msg_Dbg( p_input, "| | | | + display height=%d", uint16( vheight ) );
747 else if( EbmlId( *el4 ) == KaxVideoFrameRate::ClassInfos.GlobalId )
749 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)el4;
750 vfps.ReadData( p_sys->es->I_O() );
752 tk.f_fps = float( vfps );
753 msg_Dbg( p_input, " | | | + fps=%f", float( vfps ) );
755 else if( EbmlId( *el4 ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
757 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)el4;
758 vdmode.ReadData( p_sys->es->I_O() );
760 msg_Dbg( p_input, "| | | | + Track Video Display Unit=%s",
761 uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
763 else if( EbmlId( *el4 ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
765 KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)el4;
766 ratio.ReadData( p_sys->es->I_O() );
768 msg_Dbg( p_input, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
770 else if( EbmlId( *el4 ) == KaxVideoGamma::ClassInfos.GlobalId )
772 KaxVideoGamma &gamma = *(KaxVideoGamma*)el4;
773 gamma.ReadData( p_sys->es->I_O() );
775 msg_Dbg( p_input, " | | | + fps=%f", float( gamma ) );
779 msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() );
784 else if( EbmlId( *el3 ) == KaxTrackAudio::ClassInfos.GlobalId )
786 msg_Dbg( p_input, "| | | + Track Audio" );
790 while( ( el4 = p_sys->ep->Get() ) != NULL )
792 if( EbmlId( *el4 ) == KaxAudioSamplingFreq::ClassInfos.GlobalId )
794 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)el4;
795 afreq.ReadData( p_sys->es->I_O() );
797 tk.fmt.audio.i_rate = (int)float( afreq );
798 msg_Dbg( p_input, "| | | | + afreq=%d", tk.fmt.audio.i_rate );
800 else if( EbmlId( *el4 ) == KaxAudioChannels::ClassInfos.GlobalId )
802 KaxAudioChannels &achan = *(KaxAudioChannels*)el4;
803 achan.ReadData( p_sys->es->I_O() );
805 tk.fmt.audio.i_channels = uint8( achan );
806 msg_Dbg( p_input, "| | | | + achan=%u", uint8( achan ) );
808 else if( EbmlId( *el4 ) == KaxAudioBitDepth::ClassInfos.GlobalId )
810 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)el4;
811 abits.ReadData( p_sys->es->I_O() );
813 tk.fmt.audio.i_bitspersample = uint8( abits );
814 msg_Dbg( p_input, "| | | | + abits=%u", uint8( abits ) );
818 msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() );
825 msg_Dbg( p_input, "| | | + Unknown (%s)",
826 typeid(*el3).name() );
833 msg_Dbg( p_input, "| | + Unknown (%s)",
834 typeid(*el2).name() );
840 else if( EbmlId( *el1 ) == KaxSeekHead::ClassInfos.GlobalId )
842 msg_Dbg( p_input, "| + Seek head" );
844 while( ( el = p_sys->ep->Get() ) != NULL )
846 if( EbmlId( *el ) == KaxSeek::ClassInfos.GlobalId )
848 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
851 //msg_Dbg( p_input, "| | + Seek" );
853 while( ( el = p_sys->ep->Get() ) != NULL )
855 if( EbmlId( *el ) == KaxSeekID::ClassInfos.GlobalId )
857 KaxSeekID &sid = *(KaxSeekID*)el;
859 sid.ReadData( p_sys->es->I_O() );
861 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
863 else if( EbmlId( *el ) == KaxSeekPosition::ClassInfos.GlobalId )
865 KaxSeekPosition &spos = *(KaxSeekPosition*)el;
867 spos.ReadData( p_sys->es->I_O() );
869 i_pos = uint64( spos );
873 msg_Dbg( p_input, "| | | + Unknown (%s)",
874 typeid(*el).name() );
881 if( id == KaxCues::ClassInfos.GlobalId )
883 msg_Dbg( p_input, "| | | = cues at "I64Fd,
885 p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
887 else if( id == KaxChapters::ClassInfos.GlobalId )
889 msg_Dbg( p_input, "| | | = chapters at "I64Fd,
891 p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
893 else if( id == KaxTags::ClassInfos.GlobalId )
895 msg_Dbg( p_input, "| | | = tags at "I64Fd,
897 p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
904 msg_Dbg( p_input, "| | + Unknown (%s)",
905 typeid(*el).name() );
910 else if( EbmlId( *el1 ) == KaxCues::ClassInfos.GlobalId )
912 msg_Dbg( p_input, "| + Cues" );
914 else if( EbmlId( *el1 ) == KaxCluster::ClassInfos.GlobalId )
916 msg_Dbg( p_input, "| + Cluster" );
918 p_sys->cluster = (KaxCluster*)el1;
921 /* stop parsing the stream */
924 #ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
925 else if( EbmlId( *el1 ) == KaxAttachments::ClassInfos.GlobalId )
927 else if( EbmlId( *el1 ) == KaxAttachements::ClassInfos.GlobalId )
930 msg_Dbg( p_input, "| + Attachments FIXME TODO (but probably never supported)" );
932 else if( EbmlId( *el1 ) == KaxChapters::ClassInfos.GlobalId )
934 msg_Dbg( p_input, "| + Chapters FIXME TODO" );
936 else if( EbmlId( *el1 ) == KaxTag::ClassInfos.GlobalId )
938 msg_Dbg( p_input, "| + Tags FIXME TODO" );
942 msg_Dbg( p_input, "| + Unknown (%s)", typeid(*el1).name() );
946 if( p_sys->cluster == NULL )
948 msg_Err( p_input, "cannot find any cluster, damaged file ?" );
952 if( p_sys->i_chapters_position >= 0 )
954 msg_Warn( p_input, "chapters unsupported" );
957 /* *** Load the cue if found *** */
958 if( p_sys->i_cues_position >= 0 )
960 vlc_bool_t b_seekable;
962 stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
969 if( !p_sys->b_cues || p_sys->i_index <= 0 )
971 msg_Warn( p_input, "no cues/empty cues found->seek won't be precise" );
973 IndexAppendCluster( p_input, p_sys->cluster );
975 p_sys->b_cues = VLC_FALSE;
978 /* Create one program */
979 vlc_mutex_lock( &p_input->stream.stream_lock );
980 if( input_InitStream( p_input, 0 ) == -1)
982 vlc_mutex_unlock( &p_input->stream.stream_lock );
983 msg_Err( p_input, "cannot init stream" );
986 p_input->stream.i_mux_rate = 0;
987 vlc_mutex_unlock( &p_input->stream.stream_lock );
989 if( p_sys->f_duration > 1001.0 )
991 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000.0 );
992 p_input->stream.i_mux_rate = stream_Size( p_input->s )/50 / i_duration;
996 msg_Dbg( p_input, "found %d es", p_sys->i_track );
997 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
999 #define tk p_sys->track[i_track]
1000 if( tk.fmt.i_cat == UNKNOWN_ES )
1002 msg_Warn( p_input, "invalid track[%d, n=%d]", i_track, tk.i_number );
1007 if( tk.fmt.i_cat == SPU_ES )
1010 val.psz_string = "UTF-8";
1011 #if defined(HAVE_ICONV)
1012 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1013 var_Set( p_input, "subsdec-encoding", val );
1016 if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) )
1018 if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
1020 msg_Err( p_input, "missing/invalid BITMAPINFOHEADER" );
1021 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1025 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
1027 tk.fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1028 tk.fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1029 tk.fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1031 tk.fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
1032 if( tk.fmt.i_extra > 0 )
1034 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1035 memcpy( tk.fmt.p_extra, &p_bih[1], tk.fmt.i_extra );
1039 else if( !strcmp( tk.psz_codec, "V_MPEG1" ) ||
1040 !strcmp( tk.psz_codec, "V_MPEG2" ) )
1042 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1044 else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) )
1046 if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) )
1048 tk.fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1052 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1055 else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
1057 if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1059 msg_Err( p_input, "missing/invalid WAVEFORMATEX" );
1060 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1064 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;
1066 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk.fmt.i_codec, NULL );
1068 tk.fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1069 tk.fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1070 tk.fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1071 tk.fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1072 tk.fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1074 tk.fmt.i_extra = GetWLE( &p_wf->cbSize );
1075 if( tk.fmt.i_extra > 0 )
1077 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1078 memcpy( tk.fmt.p_extra, &p_wf[1], tk.fmt.i_extra );
1082 else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
1083 !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
1084 !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
1086 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1088 else if( !strcmp( tk.psz_codec, "A_AC3" ) )
1090 tk.fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1092 else if( !strcmp( tk.psz_codec, "A_DTS" ) )
1094 tk.fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
1096 else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
1098 tk.fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
1099 tk.i_data_init = tk.i_extra_data;
1100 tk.p_data_init = tk.p_extra_data;
1102 else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1103 !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1105 int i_profile, i_srate;
1106 static unsigned int i_sample_rates[] =
1108 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1109 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1112 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1113 /* create data for faad (MP4DecSpecificDescrTag)*/
1115 if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
1119 else if( !strcmp( &tk.psz_codec[12], "LC" ) )
1123 else if( !strcmp( &tk.psz_codec[12], "SSR" ) )
1132 for( i_srate = 0; i_srate < 13; i_srate++ )
1134 if( i_sample_rates[i_srate] == tk.fmt.audio.i_rate )
1139 msg_Dbg( p_input, "profile=%d srate=%d", i_profile, i_srate );
1142 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1143 ((uint8_t*)tk.fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1144 ((uint8_t*)tk.fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk.fmt.audio.i_channels << 3);
1146 else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
1147 !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
1148 !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
1150 if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
1152 tk.fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1156 tk.fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1158 tk.fmt.audio.i_blockalign = ( tk.fmt.audio.i_bitspersample + 7 ) / 8 * tk.fmt.audio.i_channels;
1160 else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
1162 tk.fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1164 else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) ||
1165 !strcmp( tk.psz_codec, "S_SSA" ) ||
1166 !strcmp( tk.psz_codec, "S_ASS" ))
1168 tk.fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1171 tk.fmt.i_extra = sizeof( subtitle_data_t );
1172 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1173 ((es_sys_t*)tk->fmt.p_extra)->psz_header = strdup( (char *)tk.p_extra_data );
1176 else if( !strcmp( tk.psz_codec, "S_VOBSUB" ) )
1178 tk.fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1182 msg_Err( p_input, "unknow codec id=`%s'", tk.psz_codec );
1183 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1186 tk.p_es = es_out_Add( p_input->p_es_out, &tk.fmt );
1190 /* add informations */
1191 InformationsCreate( p_input );
1199 return VLC_EGENERIC;
1202 /*****************************************************************************
1203 * Close: frees unused data
1204 *****************************************************************************/
1205 static void Close( vlc_object_t *p_this )
1207 input_thread_t *p_input = (input_thread_t *)p_this;
1208 demux_sys_t *p_sys = p_input->p_demux_data;
1212 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1214 #define tk p_sys->track[i_track]
1215 if( tk.fmt.psz_description )
1217 free( tk.fmt.psz_language );
1221 free( tk.psz_codec );
1223 if( tk.fmt.psz_language )
1225 free( tk.fmt.psz_language );
1229 free( p_sys->track );
1231 if( p_sys->psz_writing_application )
1233 free( p_sys->psz_writing_application );
1235 if( p_sys->psz_muxing_application )
1237 free( p_sys->psz_muxing_application );
1240 delete p_sys->segment;
1249 static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1251 demux_sys_t *p_sys = p_input->p_demux_data;
1262 if( p_input->b_die )
1264 return VLC_EGENERIC;
1267 el = p_sys->ep->Get();
1268 i_level = p_sys->ep->GetLevel();
1270 if( el == NULL && *pp_block != NULL )
1272 /* update the index */
1273 #define idx p_sys->index[p_sys->i_index - 1]
1274 if( p_sys->i_index > 0 && idx.i_time == -1 )
1276 idx.i_time = (*pp_block)->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1277 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1285 if( p_sys->ep->GetLevel() > 1 )
1290 msg_Warn( p_input, "EOF" );
1291 return VLC_EGENERIC;
1297 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1299 p_sys->cluster = (KaxCluster*)el;
1301 /* add it to the index */
1302 if( p_sys->i_index == 0 ||
1303 ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
1305 IndexAppendCluster( p_input, p_sys->cluster );
1310 else if( EbmlId( *el ) == KaxCues::ClassInfos.GlobalId )
1312 msg_Warn( p_input, "find KaxCues FIXME" );
1313 return VLC_EGENERIC;
1317 msg_Dbg( p_input, "unknown (%s)", typeid( el ).name() );
1320 else if( i_level == 2 )
1322 if( EbmlId( *el ) == KaxClusterTimecode::ClassInfos.GlobalId )
1324 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1326 ctc.ReadData( p_sys->es->I_O() );
1327 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
1329 else if( EbmlId( *el ) == KaxBlockGroup::ClassInfos.GlobalId )
1334 else if( i_level == 3 )
1336 if( EbmlId( *el ) == KaxBlock::ClassInfos.GlobalId )
1338 *pp_block = (KaxBlock*)el;
1340 (*pp_block)->ReadData( p_sys->es->I_O() );
1341 (*pp_block)->SetParent( *p_sys->cluster );
1345 else if( EbmlId( *el ) == KaxBlockDuration::ClassInfos.GlobalId )
1347 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1349 dur.ReadData( p_sys->es->I_O() );
1350 *pi_duration = uint64( dur );
1352 else if( EbmlId( *el ) == KaxReferenceBlock::ClassInfos.GlobalId )
1354 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1356 ref.ReadData( p_sys->es->I_O() );
1357 if( *pi_ref1 == -1 )
1359 *pi_ref1 = int64( ref );
1363 *pi_ref2 = int64( ref );
1369 msg_Err( p_input, "invalid level = %d", i_level );
1370 return VLC_EGENERIC;
1375 static block_t *MemToBlock( input_thread_t *p_input, uint8_t *p_mem, int i_mem)
1378 if( !(p_block = block_New( p_input, i_mem ) ) ) return NULL;
1379 memcpy( p_block->p_buffer, p_mem, i_mem );
1380 //p_block->i_rate = p_input->stream.control.i_rate;
1384 static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts, mtime_t i_duration )
1386 demux_sys_t *p_sys = p_input->p_demux_data;
1392 #define tk p_sys->track[i_track]
1393 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1395 if( tk.i_number == block->TrackNum() )
1401 if( i_track >= p_sys->i_track )
1403 msg_Err( p_input, "invalid track number=%d", block->TrackNum() );
1407 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk.p_es, &b );
1410 tk.b_inited = VLC_FALSE;
1414 if( tk.fmt.i_cat == AUDIO_ES && p_input->stream.control.b_mute )
1419 /* First send init data */
1420 if( !tk.b_inited && tk.i_data_init > 0 )
1424 msg_Dbg( p_input, "sending header (%d bytes)", tk.i_data_init );
1426 if( tk.fmt.i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
1432 /* XXX hack split the 3 headers */
1433 if( tk.p_data_init[0] != 0x02 )
1435 msg_Err( p_input, "invalid vorbis header" );
1438 for( i = 0; i < 2; i++ )
1441 while( i_offset < tk.i_data_init )
1443 i_size[i] += tk.p_data_init[i_offset];
1444 if( tk.p_data_init[i_offset++] != 0xff )
1450 i_size[0] = __MIN( i_size[0], tk.i_data_init - i_offset );
1451 i_size[1] = __MIN( i_size[1], tk.i_data_init - i_offset - i_size[0] );
1452 i_size[2] = tk.i_data_init - i_offset - i_size[0] - i_size[1];
1454 p_init = MemToBlock( p_input, &tk.p_data_init[i_offset], i_size[0] );
1457 es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1459 p_init = MemToBlock( p_input, &tk.p_data_init[i_offset+i_size[0]], i_size[1] );
1462 es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1464 p_init = MemToBlock( p_input, &tk.p_data_init[i_offset+i_size[0]+i_size[1]], i_size[2] );
1467 es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1472 p_init = MemToBlock( p_input, tk.p_data_init, tk.i_data_init );
1475 es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1479 tk.b_inited = VLC_TRUE;
1482 for( i = 0; i < block->NumberFrames(); i++ )
1485 DataBuffer &data = block->GetBuffer(i);
1487 p_block = MemToBlock( p_input, data.Buffer(), data.Size() );
1488 if( p_block == NULL )
1493 if( tk.fmt.i_cat != VIDEO_ES )
1494 p_block->i_dts = p_block->i_pts = i_pts;
1497 p_block->i_dts = i_pts;
1501 if( tk.fmt.i_cat == SPU_ES && strcmp( tk.psz_codec, "S_VOBSUB" ) )
1503 if( i_duration > 0 )
1505 p_block->i_dts += i_duration * 1000;
1512 es_out_Send( p_input->p_es_out, tk.p_es, p_block );
1514 /* use time stamp only for first block */
1521 static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent)
1523 demux_sys_t *p_sys = p_input->p_demux_data;
1526 int64_t i_block_duration;
1527 int64_t i_block_ref1;
1528 int64_t i_block_ref2;
1531 int i_track_skipping;
1534 msg_Dbg( p_input, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1535 if( i_date < 0 && i_percent < 0 )
1541 p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
1542 p_sys->cluster = NULL;
1544 /* seek without index or without date */
1545 if( config_GetInt( p_input, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
1547 int64_t i_pos = i_percent * stream_Size( p_input->s ) / 100;
1549 msg_Dbg( p_input, "imprecise way of seeking" );
1550 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1552 if( p_sys->index[i_index].i_position >= i_pos)
1557 if( i_index == p_sys->i_index )
1562 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1565 if( p_sys->index[i_index].i_position < i_pos )
1569 msg_Warn( p_input, "searching for cluster, could take some time" );
1571 /* search a cluster */
1572 while( ( el = p_sys->ep->Get() ) != NULL )
1574 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1576 KaxCluster *cluster = (KaxCluster*)el;
1578 /* add it to the index */
1579 IndexAppendCluster( p_input, cluster );
1581 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1583 p_sys->cluster = cluster;
1593 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1595 if( p_sys->index[i_index].i_time >= i_date )
1606 msg_Dbg( p_input, "seek got "I64Fd" (%d%%)",
1607 p_sys->index[i_index].i_time,
1608 (int)( 100 * p_sys->index[i_index].i_position /
1609 stream_Size( p_input->s ) ) );
1611 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1615 /* now parse until key frame */
1616 #define tk p_sys->track[i_track]
1617 i_track_skipping = 0;
1618 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1620 if( tk.fmt.i_cat == VIDEO_ES )
1622 tk.b_search_keyframe = VLC_TRUE;
1627 while( i_track_skipping > 0 )
1629 if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1631 msg_Warn( p_input, "cannot get block EOF?" );
1636 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1638 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1640 if( tk.i_number == block->TrackNum() )
1646 if( i_track < p_sys->i_track )
1648 if( tk.fmt.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1650 tk.b_search_keyframe = VLC_FALSE;
1653 if( tk.fmt.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1655 BlockDecode( p_input, block, 0, 0 );
1664 /*****************************************************************************
1665 * Demux: reads and demuxes data packets
1666 *****************************************************************************
1667 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1668 *****************************************************************************/
1669 static int Demux( input_thread_t * p_input )
1671 demux_sys_t *p_sys = p_input->p_demux_data;
1672 mtime_t i_start_pts;
1673 int i_block_count = 0;
1676 int64_t i_block_duration;
1677 int64_t i_block_ref1;
1678 int64_t i_block_ref2;
1680 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1682 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
1683 mtime_t i_date = -1;
1686 if( i_duration > 0 )
1688 i_date = (mtime_t)1000000 *
1689 (mtime_t)i_duration*
1690 (mtime_t)p_sys->in->getFilePointer() /
1691 (mtime_t)stream_Size( p_input->s );
1693 if( stream_Size( p_input->s ) > 0 )
1695 i_percent = 100 * p_sys->in->getFilePointer() /
1696 stream_Size( p_input->s );
1699 Seek( p_input, i_date, i_percent);
1708 if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1710 msg_Warn( p_input, "cannot get block EOF?" );
1715 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1717 if( p_sys->i_pts > 0 )
1719 input_ClockManageRef( p_input,
1720 p_input->stream.p_selected_program,
1721 p_sys->i_pts * 9 / 100 );
1724 i_pts = input_ClockGetTS( p_input,
1725 p_input->stream.p_selected_program,
1726 p_sys->i_pts * 9 / 100 );
1730 BlockDecode( p_input, block, i_pts, i_block_duration );
1735 if( i_start_pts == -1 )
1737 i_start_pts = p_sys->i_pts;
1739 else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1748 /*****************************************************************************
1750 *****************************************************************************/
1751 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1757 uint32_t vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1759 if( i_size <= 0 || mb_eof )
1764 return stream_Read( s, p_buffer, i_size );
1766 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1772 case seek_beginning:
1776 i_pos = stream_Size( s ) - i_offset;
1779 i_pos= stream_Tell( s ) + i_offset;
1783 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1790 if( stream_Seek( s, i_pos ) )
1796 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1800 uint64_t vlc_stream_io_callback::getFilePointer( void )
1802 return stream_Tell( s );
1804 void vlc_stream_io_callback::close( void )
1810 /*****************************************************************************
1811 * Ebml Stream parser
1812 *****************************************************************************/
1813 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1821 for( i = 1; i < 6; i++ )
1827 mb_keep = VLC_FALSE;
1830 EbmlParser::~EbmlParser( void )
1834 for( i = 1; i < mi_level; i++ )
1840 mb_keep = VLC_FALSE;
1844 void EbmlParser::Up( void )
1846 if( mi_user_level == mi_level )
1848 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1854 void EbmlParser::Down( void )
1860 void EbmlParser::Keep( void )
1865 int EbmlParser::GetLevel( void )
1867 return mi_user_level;
1870 EbmlElement *EbmlParser::Get( void )
1874 if( mi_user_level != mi_level )
1880 EbmlElement *ret = m_got;
1886 if( m_el[mi_level] )
1888 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
1891 delete m_el[mi_level];
1893 mb_keep = VLC_FALSE;
1896 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
1907 delete m_el[mi_level - 1];
1908 m_got = m_el[mi_level -1] = m_el[mi_level];
1909 m_el[mi_level] = NULL;
1916 else if( m_el[mi_level] == NULL )
1918 fprintf( stderr," m_el[mi_level] == NULL\n" );
1921 return m_el[mi_level];
1925 /*****************************************************************************
1927 * * LoadCues : load the cues element and update index
1929 * * LoadTags : load ... the tags element
1931 * * InformationsCreate : create all informations, load tags if present
1933 *****************************************************************************/
1934 static void LoadCues( input_thread_t *p_input )
1936 demux_sys_t *p_sys = p_input->p_demux_data;
1937 int64_t i_sav_position = p_sys->in->getFilePointer();
1939 EbmlElement *el, *cues;
1941 msg_Dbg( p_input, "loading cues" );
1942 p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
1943 cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
1947 msg_Err( p_input, "cannot load cues (broken seekhead or file)" );
1951 ep = new EbmlParser( p_sys->es, cues );
1952 while( ( el = ep->Get() ) != NULL )
1954 if( EbmlId( *el ) == KaxCuePoint::ClassInfos.GlobalId )
1956 #define idx p_sys->index[p_sys->i_index]
1959 idx.i_block_number= -1;
1960 idx.i_position = -1;
1962 idx.b_key = VLC_TRUE;
1965 while( ( el = ep->Get() ) != NULL )
1967 if( EbmlId( *el ) == KaxCueTime::ClassInfos.GlobalId )
1969 KaxCueTime &ctime = *(KaxCueTime*)el;
1971 ctime.ReadData( p_sys->es->I_O() );
1973 idx.i_time = uint64( ctime ) * (mtime_t)1000000000 / p_sys->i_timescale;
1975 else if( EbmlId( *el ) == KaxCueTrackPositions::ClassInfos.GlobalId )
1978 while( ( el = ep->Get() ) != NULL )
1980 if( EbmlId( *el ) == KaxCueTrack::ClassInfos.GlobalId )
1982 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
1984 ctrack.ReadData( p_sys->es->I_O() );
1985 idx.i_track = uint16( ctrack );
1987 else if( EbmlId( *el ) == KaxCueClusterPosition::ClassInfos.GlobalId )
1989 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
1991 ccpos.ReadData( p_sys->es->I_O() );
1992 idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
1994 else if( EbmlId( *el ) == KaxCueBlockNumber::ClassInfos.GlobalId )
1996 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
1998 cbnum.ReadData( p_sys->es->I_O() );
1999 idx.i_block_number = uint32( cbnum );
2003 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2010 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2015 msg_Dbg( p_input, " * added time="I64Fd" pos="I64Fd
2016 " track=%d bnum=%d", idx.i_time, idx.i_position,
2017 idx.i_track, idx.i_block_number );
2020 if( p_sys->i_index >= p_sys->i_index_max )
2022 p_sys->i_index_max += 1024;
2023 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2029 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2035 p_sys->b_cues = VLC_TRUE;
2037 msg_Dbg( p_input, "loading cues done." );
2038 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2041 static void LoadTags( input_thread_t *p_input )
2043 demux_sys_t *p_sys = p_input->p_demux_data;
2044 int64_t i_sav_position = p_sys->in->getFilePointer();
2046 EbmlElement *el, *tags;
2048 msg_Dbg( p_input, "loading tags" );
2049 p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
2050 tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2054 msg_Err( p_input, "cannot load tags (broken seekhead or file)" );
2058 msg_Dbg( p_input, "Tags" );
2059 ep = new EbmlParser( p_sys->es, tags );
2060 while( ( el = ep->Get() ) != NULL )
2062 if( EbmlId( *el ) == KaxTag::ClassInfos.GlobalId )
2064 msg_Dbg( p_input, "+ Tag" );
2066 while( ( el = ep->Get() ) != NULL )
2068 if( EbmlId( *el ) == KaxTagTargets::ClassInfos.GlobalId )
2070 msg_Dbg( p_input, "| + Targets" );
2072 while( ( el = ep->Get() ) != NULL )
2074 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2078 else if( EbmlId( *el ) == KaxTagGeneral::ClassInfos.GlobalId )
2080 msg_Dbg( p_input, "| + General" );
2082 while( ( el = ep->Get() ) != NULL )
2084 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2088 else if( EbmlId( *el ) == KaxTagGenres::ClassInfos.GlobalId )
2090 msg_Dbg( p_input, "| + Genres" );
2092 while( ( el = ep->Get() ) != NULL )
2094 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2098 else if( EbmlId( *el ) == KaxTagAudioSpecific::ClassInfos.GlobalId )
2100 msg_Dbg( p_input, "| + Audio Specific" );
2102 while( ( el = ep->Get() ) != NULL )
2104 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2108 else if( EbmlId( *el ) == KaxTagImageSpecific::ClassInfos.GlobalId )
2110 msg_Dbg( p_input, "| + Images Specific" );
2112 while( ( el = ep->Get() ) != NULL )
2114 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2118 else if( EbmlId( *el ) == KaxTagMultiComment::ClassInfos.GlobalId )
2120 msg_Dbg( p_input, "| + Multi Comment" );
2122 else if( EbmlId( *el ) == KaxTagMultiCommercial::ClassInfos.GlobalId )
2124 msg_Dbg( p_input, "| + Multi Commercial" );
2126 else if( EbmlId( *el ) == KaxTagMultiDate::ClassInfos.GlobalId )
2128 msg_Dbg( p_input, "| + Multi Date" );
2130 else if( EbmlId( *el ) == KaxTagMultiEntity::ClassInfos.GlobalId )
2132 msg_Dbg( p_input, "| + Multi Entity" );
2134 else if( EbmlId( *el ) == KaxTagMultiIdentifier::ClassInfos.GlobalId )
2136 msg_Dbg( p_input, "| + Multi Identifier" );
2138 else if( EbmlId( *el ) == KaxTagMultiLegal::ClassInfos.GlobalId )
2140 msg_Dbg( p_input, "| + Multi Legal" );
2142 else if( EbmlId( *el ) == KaxTagMultiTitle::ClassInfos.GlobalId )
2144 msg_Dbg( p_input, "| + Multi Title" );
2148 msg_Dbg( p_input, "| + Unknown (%s)", typeid( *el ).name() );
2155 msg_Dbg( p_input, "+ Unknown (%s)", typeid( *el ).name() );
2161 msg_Dbg( p_input, "loading tags done." );
2162 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2165 static void InformationsCreate( input_thread_t *p_input )
2167 demux_sys_t *p_sys = p_input->p_demux_data;
2168 input_info_category_t *p_cat;
2171 p_cat = input_InfoCategory( p_input, "Matroska" );
2172 if( p_sys->f_duration > 1000.1 )
2174 char psz_buffer[MSTRTIME_MAX_SIZE];
2175 input_AddInfo( p_cat, _("Duration"),
2176 msecstotimestr( psz_buffer, p_sys->f_duration ) );
2179 if( p_sys->psz_title )
2181 input_AddInfo( p_cat, _("Title"), "%s" ,p_sys->psz_title );
2183 if( p_sys->psz_date_utc )
2185 input_AddInfo( p_cat, _("Date UTC"), "%s" ,p_sys->psz_date_utc );
2187 if( p_sys->psz_segment_filename )
2189 input_AddInfo( p_cat, _("Segment Filename"), "%s" ,p_sys->psz_segment_filename );
2191 if( p_sys->psz_muxing_application )
2193 input_AddInfo( p_cat, _("Muxing Application"), "%s" ,p_sys->psz_muxing_application );
2195 if( p_sys->psz_writing_application )
2197 input_AddInfo( p_cat, _("Writing Application"), "%s" ,p_sys->psz_writing_application );
2199 input_AddInfo( p_cat, _("Number of streams"), "%d" , p_sys->i_track );
2201 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
2203 char psz_cat[strlen( "Stream " ) + 10];
2204 #define tk p_sys->track[i_track]
2206 sprintf( psz_cat, "Stream %d", i_track );
2207 p_cat = input_InfoCategory( p_input, psz_cat);
2208 if( tk.fmt.psz_description )
2210 input_AddInfo( p_cat, _("Name"), "%s", tk.fmt.psz_description );
2212 if( tk.fmt.psz_language )
2214 input_AddInfo( p_cat, _("Language"), "%s", tk.fmt.psz_language );
2216 if( tk.psz_codec_name )
2218 input_AddInfo( p_cat, _("Codec Name"), "%s", tk.psz_codec_name );
2220 if( tk.psz_codec_settings )
2222 input_AddInfo( p_cat, _("Codec Setting"), "%s", tk.psz_codec_settings );
2224 if( tk.psz_codec_info_url )
2226 input_AddInfo( p_cat, _("Codec Info"), "%s", tk.psz_codec_info_url );
2228 if( tk.psz_codec_download_url )
2230 input_AddInfo( p_cat, _("Codec Download"), "%s", tk.psz_codec_download_url );
2235 if( p_sys->i_tags_position >= 0 )
2237 vlc_bool_t b_seekable;
2239 stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
2242 LoadTags( p_input );
2248 /*****************************************************************************
2250 *****************************************************************************/
2252 static void IndexAppendCluster( input_thread_t *p_input, KaxCluster *cluster )
2254 demux_sys_t *p_sys = p_input->p_demux_data;
2256 #define idx p_sys->index[p_sys->i_index]
2258 idx.i_block_number= -1;
2259 idx.i_position = cluster->GetElementPosition();
2261 idx.b_key = VLC_TRUE;
2264 if( p_sys->i_index >= p_sys->i_index_max )
2266 p_sys->i_index_max += 1024;
2267 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2272 static char * UTF8ToStr( const UTFstring &u )
2281 p = dst = (char*)malloc( i_src + 1);
2300 static char *LanguageGetName ( const char *psz_code )
2302 const iso639_lang_t *pl;
2304 if( strlen( psz_code ) == 2 )
2306 pl = GetLang_1( psz_code );
2308 else if( strlen( psz_code ) == 3 )
2310 pl = GetLang_2B( psz_code );
2311 if( !strcmp( pl->psz_iso639_1, "??" ) )
2313 pl = GetLang_2T( psz_code );
2318 return strdup( psz_code );
2321 if( !strcmp( pl->psz_iso639_1, "??" ) )
2323 return strdup( psz_code );
2327 if( *pl->psz_native_name )
2329 return strdup( pl->psz_native_name );
2331 return strdup( pl->psz_eng_name );