1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
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 */
49 /* libebml and matroska */
50 #include "ebml/EbmlHead.h"
51 #include "ebml/EbmlSubHead.h"
52 #include "ebml/EbmlStream.h"
53 #include "ebml/EbmlContexts.h"
54 #include "ebml/EbmlVersion.h"
55 #include "ebml/EbmlVoid.h"
57 #include "matroska/FileKax.h"
58 #ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
59 #include "matroska/KaxAttachments.h"
61 #include "matroska/KaxAttachements.h"
63 #include "matroska/KaxBlock.h"
64 #include "matroska/KaxBlockData.h"
65 #include "matroska/KaxChapters.h"
66 #include "matroska/KaxCluster.h"
67 #include "matroska/KaxClusterData.h"
68 #include "matroska/KaxContexts.h"
69 #include "matroska/KaxCues.h"
70 #include "matroska/KaxCuesData.h"
71 #include "matroska/KaxInfo.h"
72 #include "matroska/KaxInfoData.h"
73 #include "matroska/KaxSeekHead.h"
74 #include "matroska/KaxSegment.h"
75 #include "matroska/KaxTag.h"
76 #include "matroska/KaxTags.h"
77 #include "matroska/KaxTagMulti.h"
78 #include "matroska/KaxTracks.h"
79 #include "matroska/KaxTrackAudio.h"
80 #include "matroska/KaxTrackVideo.h"
81 #include "matroska/KaxTrackEntryData.h"
83 #include "ebml/StdIOCallback.h"
85 using namespace LIBMATROSKA_NAMESPACE;
88 /*****************************************************************************
90 *****************************************************************************/
91 static int Open ( vlc_object_t * );
92 static void Close( vlc_object_t * );
95 set_description( _("Matroska stream demuxer" ) );
96 set_capability( "demux2", 50 );
97 set_callbacks( Open, Close );
99 add_bool( "mkv-seek-percent", 1, NULL,
100 N_("Seek based on percent not time"),
101 N_("Seek based on percent not time"), VLC_TRUE );
103 add_shortcut( "mka" );
104 add_shortcut( "mkv" );
107 /*****************************************************************************
109 *****************************************************************************/
110 static int Demux ( demux_t * );
111 static int Control( demux_t *, int, va_list );
112 static void Seek ( demux_t *, mtime_t i_date, int i_percent );
115 /*****************************************************************************
117 *****************************************************************************/
118 class vlc_stream_io_callback: public IOCallback
125 vlc_stream_io_callback( stream_t * );
127 virtual uint32_t read ( void *p_buffer, size_t i_size);
128 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
129 virtual size_t write ( const void *p_buffer, size_t i_size);
130 virtual uint64_t getFilePointer ( void );
131 virtual void close ( void );
134 /*****************************************************************************
136 *****************************************************************************/
140 EbmlParser( EbmlStream *es, EbmlElement *el_start );
145 EbmlElement *Get( void );
148 int GetLevel( void );
153 EbmlElement *m_el[6];
162 /*****************************************************************************
163 * Some functions to manipulate memory
164 *****************************************************************************/
165 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
166 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
168 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
171 /*****************************************************************************
172 * definitions of structures and functions used by this plugins
173 *****************************************************************************/
176 vlc_bool_t b_default;
177 vlc_bool_t b_enabled;
181 uint8_t *p_extra_data;
185 uint64_t i_default_duration;
186 float f_timecodescale;
194 /* data to be send first */
196 uint8_t *p_data_init;
198 /* hack : it's for seek */
199 vlc_bool_t b_search_keyframe;
202 char *psz_codec_name;
203 char *psz_codec_settings;
204 char *psz_codec_info_url;
205 char *psz_codec_download_url;
222 vlc_stream_io_callback *in;
227 uint64_t i_timescale;
229 /* duration of the segment */
237 int64_t i_cues_position;
238 int64_t i_chapters_position;
239 int64_t i_tags_position;
253 char *psz_muxing_application;
254 char *psz_writing_application;
255 char *psz_segment_filename;
262 #define MKVD_TIMECODESCALE 1000000
264 static void IndexAppendCluster ( demux_t *p_demux, KaxCluster *cluster );
265 static char *UTF8ToStr ( const UTFstring &u );
266 static void LoadCues ( demux_t * );
267 static void InformationsCreate ( demux_t * );
269 /*****************************************************************************
270 * Open: initializes matroska demux structures
271 *****************************************************************************/
272 static int Open( vlc_object_t * p_this )
274 demux_t *p_demux = (demux_t*)p_this;
280 EbmlElement *el = NULL, *el1 = NULL, *el2 = NULL, *el3 = NULL, *el4 = NULL;
282 /* peek the begining */
283 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
285 msg_Warn( p_demux, "cannot peek" );
289 /* is a valid file */
290 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
291 p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
293 msg_Warn( p_demux, "matroska module discarded "
294 "(invalid header 0x%.2x%.2x%.2x%.2x)",
295 p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
299 /* Set the demux function */
300 p_demux->pf_demux = Demux;
301 p_demux->pf_control = Control;
302 p_demux->p_sys = p_sys = (demux_sys_t*)malloc(sizeof( demux_sys_t ));
304 memset( p_sys, 0, sizeof( demux_sys_t ) );
305 p_sys->in = new vlc_stream_io_callback( p_demux->s );
306 p_sys->es = new EbmlStream( *p_sys->in );
307 p_sys->f_duration = -1;
308 p_sys->i_timescale = MKVD_TIMECODESCALE;
310 p_sys->track = (mkv_track_t*)malloc( sizeof( mkv_track_t ) );
312 p_sys->i_cues_position = -1;
313 p_sys->i_chapters_position = -1;
314 p_sys->i_tags_position = -1;
316 p_sys->b_cues = VLC_FALSE;
318 p_sys->i_index_max = 1024;
319 p_sys->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
320 p_sys->i_index_max );
322 p_sys->psz_muxing_application = NULL;
323 p_sys->psz_writing_application = NULL;
324 p_sys->psz_segment_filename = NULL;
325 p_sys->psz_title = NULL;
326 p_sys->psz_date_utc = NULL;;
329 if( p_sys->es == NULL )
331 msg_Err( p_demux, "failed to create EbmlStream" );
336 /* Find the EbmlHead element */
337 el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
340 msg_Err( p_demux, "cannot find EbmlHead" );
343 msg_Dbg( p_demux, "EbmlHead" );
345 el->SkipData( *p_sys->es, el->Generic().Context );
349 el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
352 msg_Err( p_demux, "cannot find KaxSegment" );
355 msg_Dbg( p_demux, "+ Segment" );
356 p_sys->segment = (KaxSegment*)el;
357 p_sys->cluster = NULL;
359 p_sys->ep = new EbmlParser( p_sys->es, el );
361 while( ( el1 = p_sys->ep->Get() ) != NULL )
363 if( EbmlId( *el1 ) == KaxInfo::ClassInfos.GlobalId )
365 msg_Dbg( p_demux, "| + Informations" );
368 while( ( el2 = p_sys->ep->Get() ) != NULL )
370 if( EbmlId( *el2 ) == KaxTimecodeScale::ClassInfos.GlobalId )
372 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)el2;
374 tcs.ReadData( p_sys->es->I_O() );
375 p_sys->i_timescale = uint64(tcs);
377 msg_Dbg( p_demux, "| | + TimecodeScale="I64Fd,
378 p_sys->i_timescale );
380 else if( EbmlId( *el2 ) == KaxDuration::ClassInfos.GlobalId )
382 KaxDuration &dur = *(KaxDuration*)el2;
384 dur.ReadData( p_sys->es->I_O() );
385 p_sys->f_duration = float(dur);
387 msg_Dbg( p_demux, "| | + Duration=%f",
390 else if( EbmlId( *el2 ) == KaxMuxingApp::ClassInfos.GlobalId )
392 KaxMuxingApp &mapp = *(KaxMuxingApp*)el2;
394 mapp.ReadData( p_sys->es->I_O() );
396 p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
398 msg_Dbg( p_demux, "| | + Muxing Application=%s",
399 p_sys->psz_muxing_application );
401 else if( EbmlId( *el2 ) == KaxWritingApp::ClassInfos.GlobalId )
403 KaxWritingApp &wapp = *(KaxWritingApp*)el2;
405 wapp.ReadData( p_sys->es->I_O() );
407 p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
409 msg_Dbg( p_demux, "| | + Writing Application=%s",
410 p_sys->psz_writing_application );
412 else if( EbmlId( *el2 ) == KaxSegmentFilename::ClassInfos.GlobalId )
414 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)el2;
416 sfn.ReadData( p_sys->es->I_O() );
418 p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
420 msg_Dbg( p_demux, "| | + Segment Filename=%s",
421 p_sys->psz_segment_filename );
423 else if( EbmlId( *el2 ) == KaxTitle::ClassInfos.GlobalId )
425 KaxTitle &title = *(KaxTitle*)el2;
427 title.ReadData( p_sys->es->I_O() );
429 p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
431 msg_Dbg( p_demux, "| | + Title=%s", p_sys->psz_title );
433 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
434 else if( EbmlId( *el2 ) == KaxDateUTC::ClassInfos.GlobalId )
436 KaxDateUTC &date = *(KaxDateUTC*)el2;
441 date.ReadData( p_sys->es->I_O() );
443 i_date = date.GetEpochDate();
444 memset( buffer, 0, 256 );
445 if( gmtime_r( &i_date, &tmres ) &&
446 asctime_r( &tmres, buffer ) )
448 buffer[strlen( buffer)-1]= '\0';
449 p_sys->psz_date_utc = strdup( buffer );
450 msg_Dbg( p_demux, "| | + Date=%s", p_sys->psz_date_utc );
456 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*el2).name() );
461 else if( EbmlId( *el1 ) == KaxTracks::ClassInfos.GlobalId )
463 msg_Dbg( p_demux, "| + Tracks" );
466 while( ( el2 = p_sys->ep->Get() ) != NULL )
468 if( EbmlId( *el2 ) == KaxTrackEntry::ClassInfos.GlobalId )
470 msg_Dbg( p_demux, "| | + Track Entry" );
473 p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
474 #define tk p_sys->track[p_sys->i_track - 1]
475 memset( &tk, 0, sizeof( mkv_track_t ) );
477 es_format_Init( &tk.fmt, UNKNOWN_ES, 0 );
478 tk.fmt.psz_language = strdup("English");
479 tk.fmt.psz_description = NULL;
481 tk.b_default = VLC_TRUE;
482 tk.b_enabled = VLC_TRUE;
483 tk.i_number = p_sys->i_track - 1;
485 tk.p_extra_data = NULL;
487 tk.i_default_duration = 0;
488 tk.f_timecodescale = 1.0;
490 tk.b_inited = VLC_FALSE;
492 tk.p_data_init = NULL;
494 tk.psz_codec_name = NULL;
495 tk.psz_codec_settings = NULL;
496 tk.psz_codec_info_url = NULL;
497 tk.psz_codec_download_url = NULL;
501 while( ( el3 = p_sys->ep->Get() ) != NULL )
503 if( EbmlId( *el3 ) == KaxTrackNumber::ClassInfos.GlobalId )
505 KaxTrackNumber &tnum = *(KaxTrackNumber*)el3;
506 tnum.ReadData( p_sys->es->I_O() );
508 tk.i_number = uint32( tnum );
509 msg_Dbg( p_demux, "| | | + Track Number=%u",
512 else if( EbmlId( *el3 ) == KaxTrackUID::ClassInfos.GlobalId )
514 KaxTrackUID &tuid = *(KaxTrackUID*)el3;
515 tuid.ReadData( p_sys->es->I_O() );
517 msg_Dbg( p_demux, "| | | + Track UID=%u",
520 else if( EbmlId( *el3 ) == KaxTrackType::ClassInfos.GlobalId )
523 KaxTrackType &ttype = *(KaxTrackType*)el3;
524 ttype.ReadData( p_sys->es->I_O() );
525 switch( uint8(ttype) )
529 tk.fmt.i_cat = AUDIO_ES;
533 tk.fmt.i_cat = VIDEO_ES;
536 psz_type = "subtitle";
537 tk.fmt.i_cat = SPU_ES;
540 psz_type = "unknown";
541 tk.fmt.i_cat = UNKNOWN_ES;
545 msg_Dbg( p_demux, "| | | + Track Type=%s",
548 // else if( EbmlId( *el3 ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
550 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)el3;
551 // fenb.ReadData( p_sys->es->I_O() );
553 // tk.b_enabled = uint32( fenb );
554 // msg_Dbg( p_demux, "| | | + Track Enabled=%u",
557 else if( EbmlId( *el3 ) == KaxTrackFlagDefault::ClassInfos.GlobalId )
559 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)el3;
560 fdef.ReadData( p_sys->es->I_O() );
562 tk.b_default = uint32( fdef );
563 msg_Dbg( p_demux, "| | | + Track Default=%u",
566 else if( EbmlId( *el3 ) == KaxTrackFlagLacing::ClassInfos.GlobalId )
568 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)el3;
569 lac.ReadData( p_sys->es->I_O() );
571 msg_Dbg( p_demux, "| | | + Track Lacing=%d",
574 else if( EbmlId( *el3 ) == KaxTrackMinCache::ClassInfos.GlobalId )
576 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)el3;
577 cmin.ReadData( p_sys->es->I_O() );
579 msg_Dbg( p_demux, "| | | + Track MinCache=%d",
582 else if( EbmlId( *el3 ) == KaxTrackMaxCache::ClassInfos.GlobalId )
584 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)el3;
585 cmax.ReadData( p_sys->es->I_O() );
587 msg_Dbg( p_demux, "| | | + Track MaxCache=%d",
590 else if( EbmlId( *el3 ) == KaxTrackDefaultDuration::ClassInfos.GlobalId )
592 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)el3;
593 defd.ReadData( p_sys->es->I_O() );
595 tk.i_default_duration = uint64(defd);
596 msg_Dbg( p_demux, "| | | + Track Default Duration="I64Fd, uint64(defd) );
598 else if( EbmlId( *el3 ) == KaxTrackTimecodeScale::ClassInfos.GlobalId )
600 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)el3;
601 ttcs.ReadData( p_sys->es->I_O() );
603 tk.f_timecodescale = float( ttcs );
604 msg_Dbg( p_demux, "| | | + Track TimeCodeScale=%f", tk.f_timecodescale );
606 else if( EbmlId( *el3 ) == KaxTrackName::ClassInfos.GlobalId )
608 KaxTrackName &tname = *(KaxTrackName*)el3;
609 tname.ReadData( p_sys->es->I_O() );
611 tk.fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
612 msg_Dbg( p_demux, "| | | + Track Name=%s",
613 tk.fmt.psz_description );
615 else if( EbmlId( *el3 ) == KaxTrackLanguage::ClassInfos.GlobalId )
617 KaxTrackLanguage &lang = *(KaxTrackLanguage*)el3;
618 lang.ReadData( p_sys->es->I_O() );
620 tk.fmt.psz_language = strdup( string( lang ).c_str() );
622 "| | | + Track Language=`%s'",
623 tk.fmt.psz_language );
625 else if( EbmlId( *el3 ) == KaxCodecID::ClassInfos.GlobalId )
627 KaxCodecID &codecid = *(KaxCodecID*)el3;
628 codecid.ReadData( p_sys->es->I_O() );
630 tk.psz_codec = strdup( string( codecid ).c_str() );
631 msg_Dbg( p_demux, "| | | + Track CodecId=%s",
632 string( codecid ).c_str() );
634 else if( EbmlId( *el3 ) == KaxCodecPrivate::ClassInfos.GlobalId )
636 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)el3;
637 cpriv.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
639 tk.i_extra_data = cpriv.GetSize();
640 if( tk.i_extra_data > 0 )
642 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
643 memcpy( tk.p_extra_data, cpriv.GetBuffer(), tk.i_extra_data );
645 msg_Dbg( p_demux, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
647 else if( EbmlId( *el3 ) == KaxCodecName::ClassInfos.GlobalId )
649 KaxCodecName &cname = *(KaxCodecName*)el3;
650 cname.ReadData( p_sys->es->I_O() );
652 tk.psz_codec_name = UTF8ToStr( UTFstring( cname ) );
653 msg_Dbg( p_demux, "| | | + Track Codec Name=%s", tk.psz_codec_name );
655 // else if( EbmlId( *el3 ) == KaxCodecSettings::ClassInfos.GlobalId )
657 // KaxCodecSettings &cset = *(KaxCodecSettings*)el3;
658 // cset.ReadData( p_sys->es->I_O() );
660 // tk.psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
661 // msg_Dbg( p_demux, "| | | + Track Codec Settings=%s", tk.psz_codec_settings );
663 // else if( EbmlId( *el3 ) == KaxCodecInfoURL::ClassInfos.GlobalId )
665 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)el3;
666 // ciurl.ReadData( p_sys->es->I_O() );
668 // tk.psz_codec_info_url = strdup( string( ciurl ).c_str() );
669 // msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk.psz_codec_info_url );
671 // else if( EbmlId( *el3 ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
673 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)el3;
674 // cdurl.ReadData( p_sys->es->I_O() );
676 // tk.psz_codec_download_url = strdup( string( cdurl ).c_str() );
677 // msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk.psz_codec_download_url );
679 // else if( EbmlId( *el3 ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
681 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)el3;
682 // cdall.ReadData( p_sys->es->I_O() );
684 // msg_Dbg( p_demux, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
686 // else if( EbmlId( *el3 ) == KaxTrackOverlay::ClassInfos.GlobalId )
688 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)el3;
689 // tovr.ReadData( p_sys->es->I_O() );
691 // msg_Dbg( p_demux, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
693 else if( EbmlId( *el3 ) == KaxTrackVideo::ClassInfos.GlobalId )
695 msg_Dbg( p_demux, "| | | + Track Video" );
700 while( ( el4 = p_sys->ep->Get() ) != NULL )
702 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
704 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
705 // fint.ReadData( p_sys->es->I_O() );
707 // msg_Dbg( p_demux, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
709 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
711 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
712 // stereo.ReadData( p_sys->es->I_O() );
714 // msg_Dbg( p_demux, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
717 if( EbmlId( *el4 ) == KaxVideoPixelWidth::ClassInfos.GlobalId )
719 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)el4;
720 vwidth.ReadData( p_sys->es->I_O() );
722 tk.fmt.video.i_width = uint16( vwidth );
723 msg_Dbg( p_demux, "| | | | + width=%d", uint16( vwidth ) );
725 else if( EbmlId( *el4 ) == KaxVideoPixelHeight::ClassInfos.GlobalId )
727 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)el4;
728 vheight.ReadData( p_sys->es->I_O() );
730 tk.fmt.video.i_height = uint16( vheight );
731 msg_Dbg( p_demux, "| | | | + height=%d", uint16( vheight ) );
733 else if( EbmlId( *el4 ) == KaxVideoDisplayWidth::ClassInfos.GlobalId )
735 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)el4;
736 vwidth.ReadData( p_sys->es->I_O() );
738 tk.fmt.video.i_visible_width = uint16( vwidth );
739 msg_Dbg( p_demux, "| | | | + display width=%d", uint16( vwidth ) );
741 else if( EbmlId( *el4 ) == KaxVideoDisplayHeight::ClassInfos.GlobalId )
743 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)el4;
744 vheight.ReadData( p_sys->es->I_O() );
746 tk.fmt.video.i_visible_height = uint16( vheight );
747 msg_Dbg( p_demux, "| | | | + display height=%d", uint16( vheight ) );
749 else if( EbmlId( *el4 ) == KaxVideoFrameRate::ClassInfos.GlobalId )
751 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)el4;
752 vfps.ReadData( p_sys->es->I_O() );
754 tk.f_fps = float( vfps );
755 msg_Dbg( p_demux, " | | | + fps=%f", float( vfps ) );
757 // else if( EbmlId( *el4 ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
759 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)el4;
760 // vdmode.ReadData( p_sys->es->I_O() );
762 // msg_Dbg( p_demux, "| | | | + Track Video Display Unit=%s",
763 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
765 // else if( EbmlId( *el4 ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
767 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)el4;
768 // ratio.ReadData( p_sys->es->I_O() );
770 // msg_Dbg( p_demux, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
772 // else if( EbmlId( *el4 ) == KaxVideoGamma::ClassInfos.GlobalId )
774 // KaxVideoGamma &gamma = *(KaxVideoGamma*)el4;
775 // gamma.ReadData( p_sys->es->I_O() );
777 // msg_Dbg( p_demux, " | | | + fps=%f", float( gamma ) );
781 msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*el4).name() );
786 else if( EbmlId( *el3 ) == KaxTrackAudio::ClassInfos.GlobalId )
788 msg_Dbg( p_demux, "| | | + Track Audio" );
792 while( ( el4 = p_sys->ep->Get() ) != NULL )
794 if( EbmlId( *el4 ) == KaxAudioSamplingFreq::ClassInfos.GlobalId )
796 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)el4;
797 afreq.ReadData( p_sys->es->I_O() );
799 tk.fmt.audio.i_rate = (int)float( afreq );
800 msg_Dbg( p_demux, "| | | | + afreq=%d", tk.fmt.audio.i_rate );
802 else if( EbmlId( *el4 ) == KaxAudioChannels::ClassInfos.GlobalId )
804 KaxAudioChannels &achan = *(KaxAudioChannels*)el4;
805 achan.ReadData( p_sys->es->I_O() );
807 tk.fmt.audio.i_channels = uint8( achan );
808 msg_Dbg( p_demux, "| | | | + achan=%u", uint8( achan ) );
810 else if( EbmlId( *el4 ) == KaxAudioBitDepth::ClassInfos.GlobalId )
812 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)el4;
813 abits.ReadData( p_sys->es->I_O() );
815 tk.fmt.audio.i_bitspersample = uint8( abits );
816 msg_Dbg( p_demux, "| | | | + abits=%u", uint8( abits ) );
820 msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*el4).name() );
827 msg_Dbg( p_demux, "| | | + Unknown (%s)",
828 typeid(*el3).name() );
835 msg_Dbg( p_demux, "| | + Unknown (%s)",
836 typeid(*el2).name() );
842 else if( EbmlId( *el1 ) == KaxSeekHead::ClassInfos.GlobalId )
844 msg_Dbg( p_demux, "| + Seek head" );
846 while( ( el = p_sys->ep->Get() ) != NULL )
848 if( EbmlId( *el ) == KaxSeek::ClassInfos.GlobalId )
850 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
853 //msg_Dbg( p_demux, "| | + Seek" );
855 while( ( el = p_sys->ep->Get() ) != NULL )
857 if( EbmlId( *el ) == KaxSeekID::ClassInfos.GlobalId )
859 KaxSeekID &sid = *(KaxSeekID*)el;
861 sid.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
863 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
865 else if( EbmlId( *el ) == KaxSeekPosition::ClassInfos.GlobalId )
867 KaxSeekPosition &spos = *(KaxSeekPosition*)el;
869 spos.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
871 i_pos = uint64( spos );
875 msg_Dbg( p_demux, "| | | + Unknown (%s)",
876 typeid(*el).name() );
883 if( id == KaxCues::ClassInfos.GlobalId )
885 msg_Dbg( p_demux, "| | | = cues at "I64Fd,
887 p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
889 else if( id == KaxChapters::ClassInfos.GlobalId )
891 msg_Dbg( p_demux, "| | | = chapters at "I64Fd,
893 p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
895 else if( id == KaxTags::ClassInfos.GlobalId )
897 msg_Dbg( p_demux, "| | | = tags at "I64Fd,
899 p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
906 msg_Dbg( p_demux, "| | + Unknown (%s)",
907 typeid(*el).name() );
912 else if( EbmlId( *el1 ) == KaxCues::ClassInfos.GlobalId )
914 msg_Dbg( p_demux, "| + Cues" );
916 else if( EbmlId( *el1 ) == KaxCluster::ClassInfos.GlobalId )
918 msg_Dbg( p_demux, "| + Cluster" );
920 p_sys->cluster = (KaxCluster*)el1;
923 /* stop parsing the stream */
926 #ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
927 else if( EbmlId( *el1 ) == KaxAttachments::ClassInfos.GlobalId )
929 else if( EbmlId( *el1 ) == KaxAttachements::ClassInfos.GlobalId )
932 msg_Dbg( p_demux, "| + Attachments FIXME TODO (but probably never supported)" );
934 else if( EbmlId( *el1 ) == KaxChapters::ClassInfos.GlobalId )
936 msg_Dbg( p_demux, "| + Chapters FIXME TODO" );
938 else if( EbmlId( *el1 ) == KaxTag::ClassInfos.GlobalId )
940 msg_Dbg( p_demux, "| + Tags FIXME TODO" );
944 msg_Dbg( p_demux, "| + Unknown (%s)", typeid(*el1).name() );
948 if( p_sys->cluster == NULL )
950 msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
954 if( p_sys->i_chapters_position >= 0 )
956 msg_Warn( p_demux, "chapters unsupported" );
959 /* *** Load the cue if found *** */
960 if( p_sys->i_cues_position >= 0 )
962 vlc_bool_t b_seekable;
964 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
971 if( !p_sys->b_cues || p_sys->i_index <= 0 )
973 msg_Warn( p_demux, "no cues/empty cues found->seek won't be precise" );
975 IndexAppendCluster( p_demux, p_sys->cluster );
977 p_sys->b_cues = VLC_FALSE;
981 msg_Dbg( p_demux, "found %d es", p_sys->i_track );
982 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
984 #define tk p_sys->track[i_track]
985 if( tk.fmt.i_cat == UNKNOWN_ES )
987 msg_Warn( p_demux, "invalid track[%d, n=%d]", i_track, tk.i_number );
992 if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) )
994 if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
996 msg_Err( p_demux, "missing/invalid BITMAPINFOHEADER" );
997 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1001 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
1003 tk.fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1004 tk.fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1005 tk.fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1007 tk.fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
1008 if( tk.fmt.i_extra > 0 )
1010 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1011 memcpy( tk.fmt.p_extra, &p_bih[1], tk.fmt.i_extra );
1015 else if( !strcmp( tk.psz_codec, "V_MPEG1" ) ||
1016 !strcmp( tk.psz_codec, "V_MPEG2" ) )
1018 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1020 else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) )
1022 if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) )
1024 tk.fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1028 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1031 else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
1033 if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1035 msg_Err( p_demux, "missing/invalid WAVEFORMATEX" );
1036 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1040 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;
1042 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk.fmt.i_codec, NULL );
1044 tk.fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1045 tk.fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1046 tk.fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1047 tk.fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1048 tk.fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1050 tk.fmt.i_extra = GetWLE( &p_wf->cbSize );
1051 if( tk.fmt.i_extra > 0 )
1053 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1054 memcpy( tk.fmt.p_extra, &p_wf[1], tk.fmt.i_extra );
1058 else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
1059 !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
1060 !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
1062 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1064 else if( !strcmp( tk.psz_codec, "A_AC3" ) )
1066 tk.fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1068 else if( !strcmp( tk.psz_codec, "A_DTS" ) )
1070 tk.fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
1072 else if( !strcmp( tk.psz_codec, "A_FLAC" ) )
1074 tk.fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
1075 tk.fmt.i_extra = tk.i_extra_data;
1076 tk.fmt.p_extra = malloc( tk.i_extra_data );
1077 memcpy( tk.fmt.p_extra,tk.p_extra_data, tk.i_extra_data );
1079 else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
1081 tk.fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
1082 tk.i_data_init = tk.i_extra_data;
1083 tk.p_data_init = tk.p_extra_data;
1085 else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1086 !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1088 int i_profile, i_srate;
1089 static unsigned int i_sample_rates[] =
1091 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1092 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1095 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1096 /* create data for faad (MP4DecSpecificDescrTag)*/
1098 if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
1102 else if( !strcmp( &tk.psz_codec[12], "LC" ) )
1106 else if( !strcmp( &tk.psz_codec[12], "SSR" ) )
1115 for( i_srate = 0; i_srate < 13; i_srate++ )
1117 if( i_sample_rates[i_srate] == tk.fmt.audio.i_rate )
1122 msg_Dbg( p_demux, "profile=%d srate=%d", i_profile, i_srate );
1125 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1126 ((uint8_t*)tk.fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1127 ((uint8_t*)tk.fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk.fmt.audio.i_channels << 3);
1129 else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
1130 !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
1131 !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
1133 if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
1135 tk.fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1139 tk.fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1141 tk.fmt.audio.i_blockalign = ( tk.fmt.audio.i_bitspersample + 7 ) / 8 * tk.fmt.audio.i_channels;
1143 else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
1145 tk.fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1146 tk.fmt.subs.psz_encoding = strdup( "UTF-8" );
1148 else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) ||
1149 !strcmp( tk.psz_codec, "S_SSA" ) ||
1150 !strcmp( tk.psz_codec, "S_ASS" ))
1152 tk.fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1153 tk.fmt.subs.psz_encoding = strdup( "UTF-8" );
1155 else if( !strcmp( tk.psz_codec, "S_VOBSUB" ) )
1157 tk.fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1161 msg_Err( p_demux, "unknow codec id=`%s'", tk.psz_codec );
1162 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1165 tk.p_es = es_out_Add( p_demux->out, &tk.fmt );
1169 /* add informations */
1170 InformationsCreate( p_demux );
1178 return VLC_EGENERIC;
1181 /*****************************************************************************
1182 * Close: frees unused data
1183 *****************************************************************************/
1184 static void Close( vlc_object_t *p_this )
1186 demux_t *p_demux = (demux_t*)p_this;
1187 demux_sys_t *p_sys = p_demux->p_sys;
1190 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1192 #define tk p_sys->track[i_track]
1193 if( tk.fmt.psz_description )
1195 free( tk.fmt.psz_description );
1199 free( tk.psz_codec );
1201 if( tk.fmt.psz_language )
1203 free( tk.fmt.psz_language );
1207 free( p_sys->track );
1209 if( p_sys->psz_writing_application )
1211 free( p_sys->psz_writing_application );
1213 if( p_sys->psz_muxing_application )
1215 free( p_sys->psz_muxing_application );
1218 delete p_sys->segment;
1227 /*****************************************************************************
1229 *****************************************************************************/
1230 static int Control( demux_t *p_demux, int i_query, va_list args )
1232 demux_sys_t *p_sys = p_demux->p_sys;
1236 vlc_meta_t **pp_meta;
1240 case DEMUX_GET_META:
1241 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1242 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1245 case DEMUX_GET_LENGTH:
1246 pi64 = (int64_t*)va_arg( args, int64_t * );
1247 if( p_sys->f_duration > 0.0 )
1249 *pi64 = (int64_t)(p_sys->f_duration * 1000);
1252 return VLC_EGENERIC;
1254 case DEMUX_GET_POSITION:
1255 pf = (double*)va_arg( args, double * );
1256 *pf = (double)p_sys->in->getFilePointer() / (double)stream_Size( p_demux->s );
1259 case DEMUX_SET_POSITION:
1260 f = (double)va_arg( args, double );
1261 Seek( p_demux, -1, (int)(100.0 * f) );
1262 return VLC_EGENERIC;
1264 case DEMUX_GET_TIME:
1265 pi64 = (int64_t*)va_arg( args, int64_t * );
1266 if( p_sys->f_duration > 0.0 )
1268 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
1271 *pi64 = (mtime_t)1000000 *
1272 (mtime_t)i_duration*
1273 (mtime_t)p_sys->in->getFilePointer() /
1274 (mtime_t)stream_Size( p_demux->s );
1277 return VLC_EGENERIC;
1279 case DEMUX_SET_TIME:
1282 return VLC_EGENERIC;
1286 static int BlockGet( demux_t *p_demux, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1288 demux_sys_t *p_sys = p_demux->p_sys;
1299 if( p_demux->b_die )
1301 return VLC_EGENERIC;
1304 el = p_sys->ep->Get();
1305 i_level = p_sys->ep->GetLevel();
1307 if( el == NULL && *pp_block != NULL )
1309 /* update the index */
1310 #define idx p_sys->index[p_sys->i_index - 1]
1311 if( p_sys->i_index > 0 && idx.i_time == -1 )
1313 idx.i_time = (*pp_block)->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1314 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1322 if( p_sys->ep->GetLevel() > 1 )
1327 msg_Warn( p_demux, "EOF" );
1328 return VLC_EGENERIC;
1334 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1336 p_sys->cluster = (KaxCluster*)el;
1338 /* add it to the index */
1339 if( p_sys->i_index == 0 ||
1340 ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
1342 IndexAppendCluster( p_demux, p_sys->cluster );
1347 else if( EbmlId( *el ) == KaxCues::ClassInfos.GlobalId )
1349 msg_Warn( p_demux, "find KaxCues FIXME" );
1350 return VLC_EGENERIC;
1354 msg_Dbg( p_demux, "unknown (%s)", typeid( el ).name() );
1357 else if( i_level == 2 )
1359 if( EbmlId( *el ) == KaxClusterTimecode::ClassInfos.GlobalId )
1361 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1363 ctc.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
1364 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
1366 else if( EbmlId( *el ) == KaxBlockGroup::ClassInfos.GlobalId )
1371 else if( i_level == 3 )
1373 if( EbmlId( *el ) == KaxBlock::ClassInfos.GlobalId )
1375 *pp_block = (KaxBlock*)el;
1377 (*pp_block)->ReadData( p_sys->es->I_O() );
1378 (*pp_block)->SetParent( *p_sys->cluster );
1382 else if( EbmlId( *el ) == KaxBlockDuration::ClassInfos.GlobalId )
1384 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1386 dur.ReadData( p_sys->es->I_O() );
1387 *pi_duration = uint64( dur );
1389 else if( EbmlId( *el ) == KaxReferenceBlock::ClassInfos.GlobalId )
1391 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1393 ref.ReadData( p_sys->es->I_O() );
1394 if( *pi_ref1 == -1 )
1396 *pi_ref1 = int64( ref );
1400 *pi_ref2 = int64( ref );
1406 msg_Err( p_demux, "invalid level = %d", i_level );
1407 return VLC_EGENERIC;
1412 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1415 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1416 memcpy( p_block->p_buffer, p_mem, i_mem );
1417 //p_block->i_rate = p_input->stream.control.i_rate;
1421 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts, mtime_t i_duration )
1423 demux_sys_t *p_sys = p_demux->p_sys;
1429 #define tk p_sys->track[i_track]
1430 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1432 if( tk.i_number == block->TrackNum() )
1438 if( i_track >= p_sys->i_track )
1440 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1444 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk.p_es, &b );
1447 tk.b_inited = VLC_FALSE;
1451 /* First send init data */
1452 if( !tk.b_inited && tk.i_data_init > 0 )
1456 msg_Dbg( p_demux, "sending header (%d bytes)", tk.i_data_init );
1458 if( tk.fmt.i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
1464 /* XXX hack split the 3 headers */
1465 if( tk.p_data_init[0] != 0x02 )
1467 msg_Err( p_demux, "invalid vorbis header" );
1470 for( i = 0; i < 2; i++ )
1473 while( i_offset < tk.i_data_init )
1475 i_size[i] += tk.p_data_init[i_offset];
1476 if( tk.p_data_init[i_offset++] != 0xff )
1482 i_size[0] = __MIN( i_size[0], tk.i_data_init - i_offset );
1483 i_size[1] = __MIN( i_size[1], tk.i_data_init - i_offset - i_size[0] );
1484 i_size[2] = tk.i_data_init - i_offset - i_size[0] - i_size[1];
1486 p_init = MemToBlock( p_demux, &tk.p_data_init[i_offset], i_size[0] );
1489 es_out_Send( p_demux->out, tk.p_es, p_init );
1491 p_init = MemToBlock( p_demux, &tk.p_data_init[i_offset+i_size[0]], i_size[1] );
1494 es_out_Send( p_demux->out, tk.p_es, p_init );
1496 p_init = MemToBlock( p_demux, &tk.p_data_init[i_offset+i_size[0]+i_size[1]], i_size[2] );
1499 es_out_Send( p_demux->out, tk.p_es, p_init );
1504 p_init = MemToBlock( p_demux, tk.p_data_init, tk.i_data_init );
1507 es_out_Send( p_demux->out, tk.p_es, p_init );
1511 tk.b_inited = VLC_TRUE;
1514 for( i = 0; i < block->NumberFrames(); i++ )
1517 DataBuffer &data = block->GetBuffer(i);
1519 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1520 if( p_block == NULL )
1525 if( tk.fmt.i_cat != VIDEO_ES )
1526 p_block->i_dts = p_block->i_pts = i_pts;
1529 p_block->i_dts = i_pts;
1533 if( tk.fmt.i_cat == SPU_ES && strcmp( tk.psz_codec, "S_VOBSUB" ) )
1535 p_block->i_length = i_duration * 1000;
1537 es_out_Send( p_demux->out, tk.p_es, p_block );
1539 /* use time stamp only for first block */
1546 static void Seek( demux_t *p_demux, mtime_t i_date, int i_percent)
1548 demux_sys_t *p_sys = p_demux->p_sys;
1551 int64_t i_block_duration;
1552 int64_t i_block_ref1;
1553 int64_t i_block_ref2;
1556 int i_track_skipping;
1559 msg_Dbg( p_demux, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1560 if( i_date < 0 && i_percent < 0 )
1566 p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
1567 p_sys->cluster = NULL;
1569 /* seek without index or without date */
1570 if( config_GetInt( p_demux, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
1572 int64_t i_pos = i_percent * stream_Size( p_demux->s ) / 100;
1574 msg_Dbg( p_demux, "inacurate way of seeking" );
1575 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1577 if( p_sys->index[i_index].i_position >= i_pos)
1582 if( i_index == p_sys->i_index )
1587 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1590 if( p_sys->index[i_index].i_position < i_pos )
1594 msg_Warn( p_demux, "searching for cluster, could take some time" );
1596 /* search a cluster */
1597 while( ( el = p_sys->ep->Get() ) != NULL )
1599 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1601 KaxCluster *cluster = (KaxCluster*)el;
1603 /* add it to the index */
1604 IndexAppendCluster( p_demux, cluster );
1606 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1608 p_sys->cluster = cluster;
1618 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1620 if( p_sys->index[i_index].i_time >= i_date )
1631 msg_Dbg( p_demux, "seek got "I64Fd" (%d%%)",
1632 p_sys->index[i_index].i_time,
1633 (int)( 100 * p_sys->index[i_index].i_position /
1634 stream_Size( p_demux->s ) ) );
1636 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1640 /* now parse until key frame */
1641 #define tk p_sys->track[i_track]
1642 i_track_skipping = 0;
1643 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1645 if( tk.fmt.i_cat == VIDEO_ES )
1647 tk.b_search_keyframe = VLC_TRUE;
1652 while( i_track_skipping > 0 )
1654 if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1656 msg_Warn( p_demux, "cannot get block EOF?" );
1661 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1663 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1665 if( tk.i_number == block->TrackNum() )
1671 if( i_track < p_sys->i_track )
1673 if( tk.fmt.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1675 tk.b_search_keyframe = VLC_FALSE;
1678 if( tk.fmt.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1680 BlockDecode( p_demux, block, 0, 0 );
1689 /*****************************************************************************
1690 * Demux: reads and demuxes data packets
1691 *****************************************************************************
1692 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1693 *****************************************************************************/
1694 static int Demux( demux_t *p_demux)
1696 demux_sys_t *p_sys = p_demux->p_sys;
1697 mtime_t i_start_pts;
1698 int i_block_count = 0;
1701 int64_t i_block_duration;
1702 int64_t i_block_ref1;
1703 int64_t i_block_ref2;
1709 if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1711 msg_Warn( p_demux, "cannot get block EOF?" );
1716 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1718 if( p_sys->i_pts > 0 )
1720 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
1723 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
1728 if( i_start_pts == -1 )
1730 i_start_pts = p_sys->i_pts;
1732 else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1741 /*****************************************************************************
1743 *****************************************************************************/
1744 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1750 uint32_t vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1752 if( i_size <= 0 || mb_eof )
1757 return stream_Read( s, p_buffer, i_size );
1759 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1765 case seek_beginning:
1769 i_pos = stream_Size( s ) - i_offset;
1772 i_pos= stream_Tell( s ) + i_offset;
1776 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1783 if( stream_Seek( s, i_pos ) )
1789 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1793 uint64_t vlc_stream_io_callback::getFilePointer( void )
1795 return stream_Tell( s );
1797 void vlc_stream_io_callback::close( void )
1803 /*****************************************************************************
1804 * Ebml Stream parser
1805 *****************************************************************************/
1806 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1814 for( i = 1; i < 6; i++ )
1820 mb_keep = VLC_FALSE;
1823 EbmlParser::~EbmlParser( void )
1827 for( i = 1; i < mi_level; i++ )
1833 mb_keep = VLC_FALSE;
1837 void EbmlParser::Up( void )
1839 if( mi_user_level == mi_level )
1841 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1847 void EbmlParser::Down( void )
1853 void EbmlParser::Keep( void )
1858 int EbmlParser::GetLevel( void )
1860 return mi_user_level;
1863 EbmlElement *EbmlParser::Get( void )
1867 if( mi_user_level != mi_level )
1873 EbmlElement *ret = m_got;
1879 if( m_el[mi_level] )
1881 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
1884 delete m_el[mi_level];
1886 mb_keep = VLC_FALSE;
1889 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
1900 delete m_el[mi_level - 1];
1901 m_got = m_el[mi_level -1] = m_el[mi_level];
1902 m_el[mi_level] = NULL;
1909 else if( m_el[mi_level] == NULL )
1911 fprintf( stderr," m_el[mi_level] == NULL\n" );
1914 return m_el[mi_level];
1918 /*****************************************************************************
1920 * * LoadCues : load the cues element and update index
1922 * * LoadTags : load ... the tags element
1924 * * InformationsCreate : create all informations, load tags if present
1926 *****************************************************************************/
1927 static void LoadCues( demux_t *p_demux )
1929 demux_sys_t *p_sys = p_demux->p_sys;
1930 int64_t i_sav_position = p_sys->in->getFilePointer();
1932 EbmlElement *el, *cues;
1934 msg_Dbg( p_demux, "loading cues" );
1935 p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
1936 cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
1940 msg_Err( p_demux, "cannot load cues (broken seekhead or file)" );
1941 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1945 ep = new EbmlParser( p_sys->es, cues );
1946 while( ( el = ep->Get() ) != NULL )
1948 if( EbmlId( *el ) == KaxCuePoint::ClassInfos.GlobalId )
1950 #define idx p_sys->index[p_sys->i_index]
1953 idx.i_block_number= -1;
1954 idx.i_position = -1;
1956 idx.b_key = VLC_TRUE;
1959 while( ( el = ep->Get() ) != NULL )
1961 if( EbmlId( *el ) == KaxCueTime::ClassInfos.GlobalId )
1963 KaxCueTime &ctime = *(KaxCueTime*)el;
1965 ctime.ReadData( p_sys->es->I_O() );
1967 idx.i_time = uint64( ctime ) * (mtime_t)1000000000 / p_sys->i_timescale;
1969 else if( EbmlId( *el ) == KaxCueTrackPositions::ClassInfos.GlobalId )
1972 while( ( el = ep->Get() ) != NULL )
1974 if( EbmlId( *el ) == KaxCueTrack::ClassInfos.GlobalId )
1976 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
1978 ctrack.ReadData( p_sys->es->I_O() );
1979 idx.i_track = uint16( ctrack );
1981 else if( EbmlId( *el ) == KaxCueClusterPosition::ClassInfos.GlobalId )
1983 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
1985 ccpos.ReadData( p_sys->es->I_O() );
1986 idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
1988 else if( EbmlId( *el ) == KaxCueBlockNumber::ClassInfos.GlobalId )
1990 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
1992 cbnum.ReadData( p_sys->es->I_O() );
1993 idx.i_block_number = uint32( cbnum );
1997 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2004 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2010 msg_Dbg( p_demux, " * added time="I64Fd" pos="I64Fd
2011 " track=%d bnum=%d", idx.i_time, idx.i_position,
2012 idx.i_track, idx.i_block_number );
2016 if( p_sys->i_index >= p_sys->i_index_max )
2018 p_sys->i_index_max += 1024;
2019 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2025 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2031 p_sys->b_cues = VLC_TRUE;
2033 msg_Dbg( p_demux, "loading cues done." );
2034 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2037 static void LoadTags( demux_t *p_demux )
2039 demux_sys_t *p_sys = p_demux->p_sys;
2040 int64_t i_sav_position = p_sys->in->getFilePointer();
2042 EbmlElement *el, *tags;
2044 msg_Dbg( p_demux, "loading tags" );
2045 p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
2046 tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2050 msg_Err( p_demux, "cannot load tags (broken seekhead or file)" );
2051 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2055 msg_Dbg( p_demux, "Tags" );
2056 ep = new EbmlParser( p_sys->es, tags );
2057 while( ( el = ep->Get() ) != NULL )
2059 if( EbmlId( *el ) == KaxTag::ClassInfos.GlobalId )
2061 msg_Dbg( p_demux, "+ Tag" );
2063 while( ( el = ep->Get() ) != NULL )
2065 if( EbmlId( *el ) == KaxTagTargets::ClassInfos.GlobalId )
2067 msg_Dbg( p_demux, "| + Targets" );
2069 while( ( el = ep->Get() ) != NULL )
2071 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2075 else if( EbmlId( *el ) == KaxTagGeneral::ClassInfos.GlobalId )
2077 msg_Dbg( p_demux, "| + General" );
2079 while( ( el = ep->Get() ) != NULL )
2081 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2085 else if( EbmlId( *el ) == KaxTagGenres::ClassInfos.GlobalId )
2087 msg_Dbg( p_demux, "| + Genres" );
2089 while( ( el = ep->Get() ) != NULL )
2091 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2095 else if( EbmlId( *el ) == KaxTagAudioSpecific::ClassInfos.GlobalId )
2097 msg_Dbg( p_demux, "| + Audio Specific" );
2099 while( ( el = ep->Get() ) != NULL )
2101 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2105 else if( EbmlId( *el ) == KaxTagImageSpecific::ClassInfos.GlobalId )
2107 msg_Dbg( p_demux, "| + Images Specific" );
2109 while( ( el = ep->Get() ) != NULL )
2111 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2115 else if( EbmlId( *el ) == KaxTagMultiComment::ClassInfos.GlobalId )
2117 msg_Dbg( p_demux, "| + Multi Comment" );
2119 else if( EbmlId( *el ) == KaxTagMultiCommercial::ClassInfos.GlobalId )
2121 msg_Dbg( p_demux, "| + Multi Commercial" );
2123 else if( EbmlId( *el ) == KaxTagMultiDate::ClassInfos.GlobalId )
2125 msg_Dbg( p_demux, "| + Multi Date" );
2127 else if( EbmlId( *el ) == KaxTagMultiEntity::ClassInfos.GlobalId )
2129 msg_Dbg( p_demux, "| + Multi Entity" );
2131 else if( EbmlId( *el ) == KaxTagMultiIdentifier::ClassInfos.GlobalId )
2133 msg_Dbg( p_demux, "| + Multi Identifier" );
2135 else if( EbmlId( *el ) == KaxTagMultiLegal::ClassInfos.GlobalId )
2137 msg_Dbg( p_demux, "| + Multi Legal" );
2139 else if( EbmlId( *el ) == KaxTagMultiTitle::ClassInfos.GlobalId )
2141 msg_Dbg( p_demux, "| + Multi Title" );
2145 msg_Dbg( p_demux, "| + Unknown (%s)", typeid( *el ).name() );
2152 msg_Dbg( p_demux, "+ Unknown (%s)", typeid( *el ).name() );
2158 msg_Dbg( p_demux, "loading tags done." );
2159 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2162 static void InformationsCreate( demux_t *p_demux )
2164 demux_sys_t *p_sys = p_demux->p_sys;
2167 p_sys->meta = vlc_meta_New();
2169 if( p_sys->psz_title )
2171 vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_sys->psz_title );
2173 if( p_sys->psz_date_utc )
2175 vlc_meta_Add( p_sys->meta, VLC_META_DATE, p_sys->psz_date_utc );
2177 if( p_sys->psz_segment_filename )
2179 vlc_meta_Add( p_sys->meta, _("Segment filename"), p_sys->psz_segment_filename );
2181 if( p_sys->psz_muxing_application )
2183 vlc_meta_Add( p_sys->meta, _("Muxing application"), p_sys->psz_muxing_application );
2185 if( p_sys->psz_writing_application )
2187 vlc_meta_Add( p_sys->meta, _("Writing application"), p_sys->psz_writing_application );
2190 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
2192 mkv_track_t *tk = &p_sys->track[i_track];
2193 vlc_meta_t *mtk = vlc_meta_New();
2195 p_sys->meta->track = (vlc_meta_t**)realloc( p_sys->meta->track,
2196 sizeof( vlc_meta_t * ) * ( p_sys->meta->i_track + 1 ) );
2197 p_sys->meta->track[p_sys->meta->i_track++] = mtk;
2199 if( tk->fmt.psz_description )
2201 vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
2203 if( tk->psz_codec_name )
2205 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
2207 if( tk->psz_codec_settings )
2209 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, tk->psz_codec_settings );
2211 if( tk->psz_codec_info_url )
2213 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
2215 if( tk->psz_codec_download_url )
2217 vlc_meta_Add( p_sys->meta, VLC_META_URL, tk->psz_codec_download_url );
2221 if( p_sys->i_tags_position >= 0 )
2223 vlc_bool_t b_seekable;
2225 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
2228 LoadTags( p_demux );
2234 /*****************************************************************************
2236 *****************************************************************************/
2238 static void IndexAppendCluster( demux_t *p_demux, KaxCluster *cluster )
2240 demux_sys_t *p_sys = p_demux->p_sys;
2242 #define idx p_sys->index[p_sys->i_index]
2244 idx.i_block_number= -1;
2245 idx.i_position = cluster->GetElementPosition();
2247 idx.b_key = VLC_TRUE;
2250 if( p_sys->i_index >= p_sys->i_index_max )
2252 p_sys->i_index_max += 1024;
2253 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2258 static char * UTF8ToStr( const UTFstring &u )
2267 p = dst = (char*)malloc( i_src + 1);