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( "demux", 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 ( input_thread_t * );
111 static int Control( input_thread_t *, int, va_list );
112 static void Seek ( input_thread_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 ( input_thread_t *p_input, KaxCluster *cluster );
265 static char *UTF8ToStr ( const UTFstring &u );
266 static void LoadCues ( input_thread_t *);
267 static void InformationsCreate ( input_thread_t *p_input );
269 /*****************************************************************************
270 * Open: initializes matroska demux structures
271 *****************************************************************************/
272 static int Open( vlc_object_t * p_this )
274 input_thread_t *p_input = (input_thread_t *)p_this;
280 EbmlElement *el = NULL, *el1 = NULL, *el2 = NULL, *el3 = NULL, *el4 = NULL;
282 /* peek the begining */
283 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
285 msg_Warn( p_input, "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_input, "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_input->pf_demux = Demux;
301 p_input->pf_demux_control = Control;
303 p_input->p_demux_data = p_sys = (demux_sys_t*)malloc(sizeof( demux_sys_t ));
304 memset( p_sys, 0, sizeof( demux_sys_t ) );
306 p_sys->in = new vlc_stream_io_callback( p_input->s );
307 p_sys->es = new EbmlStream( *p_sys->in );
308 p_sys->f_duration = -1;
309 p_sys->i_timescale = MKVD_TIMECODESCALE;
311 p_sys->track = (mkv_track_t*)malloc( sizeof( mkv_track_t ) );
313 p_sys->i_cues_position = -1;
314 p_sys->i_chapters_position = -1;
315 p_sys->i_tags_position = -1;
317 p_sys->b_cues = VLC_FALSE;
319 p_sys->i_index_max = 1024;
320 p_sys->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
321 p_sys->i_index_max );
323 p_sys->psz_muxing_application = NULL;
324 p_sys->psz_writing_application = NULL;
325 p_sys->psz_segment_filename = NULL;
326 p_sys->psz_title = NULL;
327 p_sys->psz_date_utc = NULL;;
330 if( p_sys->es == NULL )
332 msg_Err( p_input, "failed to create EbmlStream" );
337 /* Find the EbmlHead element */
338 el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
341 msg_Err( p_input, "cannot find EbmlHead" );
344 msg_Dbg( p_input, "EbmlHead" );
346 el->SkipData( *p_sys->es, el->Generic().Context );
350 el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
353 msg_Err( p_input, "cannot find KaxSegment" );
356 msg_Dbg( p_input, "+ Segment" );
357 p_sys->segment = (KaxSegment*)el;
358 p_sys->cluster = NULL;
360 p_sys->ep = new EbmlParser( p_sys->es, el );
362 while( ( el1 = p_sys->ep->Get() ) != NULL )
364 if( EbmlId( *el1 ) == KaxInfo::ClassInfos.GlobalId )
366 msg_Dbg( p_input, "| + Informations" );
369 while( ( el2 = p_sys->ep->Get() ) != NULL )
371 if( EbmlId( *el2 ) == KaxTimecodeScale::ClassInfos.GlobalId )
373 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)el2;
375 tcs.ReadData( p_sys->es->I_O() );
376 p_sys->i_timescale = uint64(tcs);
378 msg_Dbg( p_input, "| | + TimecodeScale="I64Fd,
379 p_sys->i_timescale );
381 else if( EbmlId( *el2 ) == KaxDuration::ClassInfos.GlobalId )
383 KaxDuration &dur = *(KaxDuration*)el2;
385 dur.ReadData( p_sys->es->I_O() );
386 p_sys->f_duration = float(dur);
388 msg_Dbg( p_input, "| | + Duration=%f",
391 else if( EbmlId( *el2 ) == KaxMuxingApp::ClassInfos.GlobalId )
393 KaxMuxingApp &mapp = *(KaxMuxingApp*)el2;
395 mapp.ReadData( p_sys->es->I_O() );
397 p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
399 msg_Dbg( p_input, "| | + Muxing Application=%s",
400 p_sys->psz_muxing_application );
402 else if( EbmlId( *el2 ) == KaxWritingApp::ClassInfos.GlobalId )
404 KaxWritingApp &wapp = *(KaxWritingApp*)el2;
406 wapp.ReadData( p_sys->es->I_O() );
408 p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
410 msg_Dbg( p_input, "| | + Writing Application=%s",
411 p_sys->psz_writing_application );
413 else if( EbmlId( *el2 ) == KaxSegmentFilename::ClassInfos.GlobalId )
415 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)el2;
417 sfn.ReadData( p_sys->es->I_O() );
419 p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
421 msg_Dbg( p_input, "| | + Segment Filename=%s",
422 p_sys->psz_segment_filename );
424 else if( EbmlId( *el2 ) == KaxTitle::ClassInfos.GlobalId )
426 KaxTitle &title = *(KaxTitle*)el2;
428 title.ReadData( p_sys->es->I_O() );
430 p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
432 msg_Dbg( p_input, "| | + Title=%s", p_sys->psz_title );
434 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
435 else if( EbmlId( *el2 ) == KaxDateUTC::ClassInfos.GlobalId )
437 KaxDateUTC &date = *(KaxDateUTC*)el2;
442 date.ReadData( p_sys->es->I_O() );
444 i_date = date.GetEpochDate();
445 memset( buffer, 0, 256 );
446 if( gmtime_r( &i_date, &tmres ) &&
447 asctime_r( &tmres, buffer ) )
449 buffer[strlen( buffer)-1]= '\0';
450 p_sys->psz_date_utc = strdup( buffer );
451 msg_Dbg( p_input, "| | + Date=%s", p_sys->psz_date_utc );
457 msg_Dbg( p_input, "| | + Unknown (%s)", typeid(*el2).name() );
462 else if( EbmlId( *el1 ) == KaxTracks::ClassInfos.GlobalId )
464 msg_Dbg( p_input, "| + Tracks" );
467 while( ( el2 = p_sys->ep->Get() ) != NULL )
469 if( EbmlId( *el2 ) == KaxTrackEntry::ClassInfos.GlobalId )
471 msg_Dbg( p_input, "| | + Track Entry" );
474 p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
475 #define tk p_sys->track[p_sys->i_track - 1]
476 memset( &tk, 0, sizeof( mkv_track_t ) );
478 es_format_Init( &tk.fmt, UNKNOWN_ES, 0 );
479 tk.fmt.psz_language = strdup("English");
480 tk.fmt.psz_description = NULL;
482 tk.b_default = VLC_TRUE;
483 tk.b_enabled = VLC_TRUE;
484 tk.i_number = p_sys->i_track - 1;
486 tk.p_extra_data = NULL;
488 tk.i_default_duration = 0;
489 tk.f_timecodescale = 1.0;
491 tk.b_inited = VLC_FALSE;
493 tk.p_data_init = NULL;
495 tk.psz_codec_name = NULL;
496 tk.psz_codec_settings = NULL;
497 tk.psz_codec_info_url = NULL;
498 tk.psz_codec_download_url = NULL;
502 while( ( el3 = p_sys->ep->Get() ) != NULL )
504 if( EbmlId( *el3 ) == KaxTrackNumber::ClassInfos.GlobalId )
506 KaxTrackNumber &tnum = *(KaxTrackNumber*)el3;
507 tnum.ReadData( p_sys->es->I_O() );
509 tk.i_number = uint32( tnum );
510 msg_Dbg( p_input, "| | | + Track Number=%u",
513 else if( EbmlId( *el3 ) == KaxTrackUID::ClassInfos.GlobalId )
515 KaxTrackUID &tuid = *(KaxTrackUID*)el3;
516 tuid.ReadData( p_sys->es->I_O() );
518 msg_Dbg( p_input, "| | | + Track UID=%u",
521 else if( EbmlId( *el3 ) == KaxTrackType::ClassInfos.GlobalId )
524 KaxTrackType &ttype = *(KaxTrackType*)el3;
525 ttype.ReadData( p_sys->es->I_O() );
526 switch( uint8(ttype) )
530 tk.fmt.i_cat = AUDIO_ES;
534 tk.fmt.i_cat = VIDEO_ES;
537 psz_type = "subtitle";
538 tk.fmt.i_cat = SPU_ES;
541 psz_type = "unknown";
542 tk.fmt.i_cat = UNKNOWN_ES;
546 msg_Dbg( p_input, "| | | + Track Type=%s",
549 // else if( EbmlId( *el3 ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
551 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)el3;
552 // fenb.ReadData( p_sys->es->I_O() );
554 // tk.b_enabled = uint32( fenb );
555 // msg_Dbg( p_input, "| | | + Track Enabled=%u",
558 else if( EbmlId( *el3 ) == KaxTrackFlagDefault::ClassInfos.GlobalId )
560 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)el3;
561 fdef.ReadData( p_sys->es->I_O() );
563 tk.b_default = uint32( fdef );
564 msg_Dbg( p_input, "| | | + Track Default=%u",
567 else if( EbmlId( *el3 ) == KaxTrackFlagLacing::ClassInfos.GlobalId )
569 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)el3;
570 lac.ReadData( p_sys->es->I_O() );
572 msg_Dbg( p_input, "| | | + Track Lacing=%d",
575 else if( EbmlId( *el3 ) == KaxTrackMinCache::ClassInfos.GlobalId )
577 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)el3;
578 cmin.ReadData( p_sys->es->I_O() );
580 msg_Dbg( p_input, "| | | + Track MinCache=%d",
583 else if( EbmlId( *el3 ) == KaxTrackMaxCache::ClassInfos.GlobalId )
585 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)el3;
586 cmax.ReadData( p_sys->es->I_O() );
588 msg_Dbg( p_input, "| | | + Track MaxCache=%d",
591 else if( EbmlId( *el3 ) == KaxTrackDefaultDuration::ClassInfos.GlobalId )
593 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)el3;
594 defd.ReadData( p_sys->es->I_O() );
596 tk.i_default_duration = uint64(defd);
597 msg_Dbg( p_input, "| | | + Track Default Duration="I64Fd, uint64(defd) );
599 else if( EbmlId( *el3 ) == KaxTrackTimecodeScale::ClassInfos.GlobalId )
601 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)el3;
602 ttcs.ReadData( p_sys->es->I_O() );
604 tk.f_timecodescale = float( ttcs );
605 msg_Dbg( p_input, "| | | + Track TimeCodeScale=%f", tk.f_timecodescale );
607 else if( EbmlId( *el3 ) == KaxTrackName::ClassInfos.GlobalId )
609 KaxTrackName &tname = *(KaxTrackName*)el3;
610 tname.ReadData( p_sys->es->I_O() );
612 tk.fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
613 msg_Dbg( p_input, "| | | + Track Name=%s",
614 tk.fmt.psz_description );
616 else if( EbmlId( *el3 ) == KaxTrackLanguage::ClassInfos.GlobalId )
618 KaxTrackLanguage &lang = *(KaxTrackLanguage*)el3;
619 lang.ReadData( p_sys->es->I_O() );
621 tk.fmt.psz_language = strdup( string( lang ).c_str() );
623 "| | | + Track Language=`%s'",
624 tk.fmt.psz_language );
626 else if( EbmlId( *el3 ) == KaxCodecID::ClassInfos.GlobalId )
628 KaxCodecID &codecid = *(KaxCodecID*)el3;
629 codecid.ReadData( p_sys->es->I_O() );
631 tk.psz_codec = strdup( string( codecid ).c_str() );
632 msg_Dbg( p_input, "| | | + Track CodecId=%s",
633 string( codecid ).c_str() );
635 else if( EbmlId( *el3 ) == KaxCodecPrivate::ClassInfos.GlobalId )
637 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)el3;
638 cpriv.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
640 tk.i_extra_data = cpriv.GetSize();
641 if( tk.i_extra_data > 0 )
643 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
644 memcpy( tk.p_extra_data, cpriv.GetBuffer(), tk.i_extra_data );
646 msg_Dbg( p_input, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
648 else if( EbmlId( *el3 ) == KaxCodecName::ClassInfos.GlobalId )
650 KaxCodecName &cname = *(KaxCodecName*)el3;
651 cname.ReadData( p_sys->es->I_O() );
653 tk.psz_codec_name = UTF8ToStr( UTFstring( cname ) );
654 msg_Dbg( p_input, "| | | + Track Codec Name=%s", tk.psz_codec_name );
656 // else if( EbmlId( *el3 ) == KaxCodecSettings::ClassInfos.GlobalId )
658 // KaxCodecSettings &cset = *(KaxCodecSettings*)el3;
659 // cset.ReadData( p_sys->es->I_O() );
661 // tk.psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
662 // msg_Dbg( p_input, "| | | + Track Codec Settings=%s", tk.psz_codec_settings );
664 // else if( EbmlId( *el3 ) == KaxCodecInfoURL::ClassInfos.GlobalId )
666 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)el3;
667 // ciurl.ReadData( p_sys->es->I_O() );
669 // tk.psz_codec_info_url = strdup( string( ciurl ).c_str() );
670 // msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_info_url );
672 // else if( EbmlId( *el3 ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
674 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)el3;
675 // cdurl.ReadData( p_sys->es->I_O() );
677 // tk.psz_codec_download_url = strdup( string( cdurl ).c_str() );
678 // msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_download_url );
680 // else if( EbmlId( *el3 ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
682 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)el3;
683 // cdall.ReadData( p_sys->es->I_O() );
685 // msg_Dbg( p_input, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
687 // else if( EbmlId( *el3 ) == KaxTrackOverlay::ClassInfos.GlobalId )
689 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)el3;
690 // tovr.ReadData( p_sys->es->I_O() );
692 // msg_Dbg( p_input, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
694 else if( EbmlId( *el3 ) == KaxTrackVideo::ClassInfos.GlobalId )
696 msg_Dbg( p_input, "| | | + Track Video" );
701 while( ( el4 = p_sys->ep->Get() ) != NULL )
703 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
705 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
706 // fint.ReadData( p_sys->es->I_O() );
708 // msg_Dbg( p_input, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
710 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
712 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
713 // stereo.ReadData( p_sys->es->I_O() );
715 // msg_Dbg( p_input, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
718 if( EbmlId( *el4 ) == KaxVideoPixelWidth::ClassInfos.GlobalId )
720 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)el4;
721 vwidth.ReadData( p_sys->es->I_O() );
723 tk.fmt.video.i_width = uint16( vwidth );
724 msg_Dbg( p_input, "| | | | + width=%d", uint16( vwidth ) );
726 else if( EbmlId( *el4 ) == KaxVideoPixelHeight::ClassInfos.GlobalId )
728 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)el4;
729 vheight.ReadData( p_sys->es->I_O() );
731 tk.fmt.video.i_height = uint16( vheight );
732 msg_Dbg( p_input, "| | | | + height=%d", uint16( vheight ) );
734 else if( EbmlId( *el4 ) == KaxVideoDisplayWidth::ClassInfos.GlobalId )
736 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)el4;
737 vwidth.ReadData( p_sys->es->I_O() );
739 tk.fmt.video.i_visible_width = uint16( vwidth );
740 msg_Dbg( p_input, "| | | | + display width=%d", uint16( vwidth ) );
742 else if( EbmlId( *el4 ) == KaxVideoDisplayHeight::ClassInfos.GlobalId )
744 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)el4;
745 vheight.ReadData( p_sys->es->I_O() );
747 tk.fmt.video.i_visible_height = uint16( vheight );
748 msg_Dbg( p_input, "| | | | + display height=%d", uint16( vheight ) );
750 else if( EbmlId( *el4 ) == KaxVideoFrameRate::ClassInfos.GlobalId )
752 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)el4;
753 vfps.ReadData( p_sys->es->I_O() );
755 tk.f_fps = float( vfps );
756 msg_Dbg( p_input, " | | | + fps=%f", float( vfps ) );
758 // else if( EbmlId( *el4 ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
760 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)el4;
761 // vdmode.ReadData( p_sys->es->I_O() );
763 // msg_Dbg( p_input, "| | | | + Track Video Display Unit=%s",
764 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
766 // else if( EbmlId( *el4 ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
768 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)el4;
769 // ratio.ReadData( p_sys->es->I_O() );
771 // msg_Dbg( p_input, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
773 // else if( EbmlId( *el4 ) == KaxVideoGamma::ClassInfos.GlobalId )
775 // KaxVideoGamma &gamma = *(KaxVideoGamma*)el4;
776 // gamma.ReadData( p_sys->es->I_O() );
778 // msg_Dbg( p_input, " | | | + fps=%f", float( gamma ) );
782 msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() );
787 else if( EbmlId( *el3 ) == KaxTrackAudio::ClassInfos.GlobalId )
789 msg_Dbg( p_input, "| | | + Track Audio" );
793 while( ( el4 = p_sys->ep->Get() ) != NULL )
795 if( EbmlId( *el4 ) == KaxAudioSamplingFreq::ClassInfos.GlobalId )
797 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)el4;
798 afreq.ReadData( p_sys->es->I_O() );
800 tk.fmt.audio.i_rate = (int)float( afreq );
801 msg_Dbg( p_input, "| | | | + afreq=%d", tk.fmt.audio.i_rate );
803 else if( EbmlId( *el4 ) == KaxAudioChannels::ClassInfos.GlobalId )
805 KaxAudioChannels &achan = *(KaxAudioChannels*)el4;
806 achan.ReadData( p_sys->es->I_O() );
808 tk.fmt.audio.i_channels = uint8( achan );
809 msg_Dbg( p_input, "| | | | + achan=%u", uint8( achan ) );
811 else if( EbmlId( *el4 ) == KaxAudioBitDepth::ClassInfos.GlobalId )
813 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)el4;
814 abits.ReadData( p_sys->es->I_O() );
816 tk.fmt.audio.i_bitspersample = uint8( abits );
817 msg_Dbg( p_input, "| | | | + abits=%u", uint8( abits ) );
821 msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() );
828 msg_Dbg( p_input, "| | | + Unknown (%s)",
829 typeid(*el3).name() );
836 msg_Dbg( p_input, "| | + Unknown (%s)",
837 typeid(*el2).name() );
843 else if( EbmlId( *el1 ) == KaxSeekHead::ClassInfos.GlobalId )
845 msg_Dbg( p_input, "| + Seek head" );
847 while( ( el = p_sys->ep->Get() ) != NULL )
849 if( EbmlId( *el ) == KaxSeek::ClassInfos.GlobalId )
851 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
854 //msg_Dbg( p_input, "| | + Seek" );
856 while( ( el = p_sys->ep->Get() ) != NULL )
858 if( EbmlId( *el ) == KaxSeekID::ClassInfos.GlobalId )
860 KaxSeekID &sid = *(KaxSeekID*)el;
862 sid.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
864 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
866 else if( EbmlId( *el ) == KaxSeekPosition::ClassInfos.GlobalId )
868 KaxSeekPosition &spos = *(KaxSeekPosition*)el;
870 spos.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
872 i_pos = uint64( spos );
876 msg_Dbg( p_input, "| | | + Unknown (%s)",
877 typeid(*el).name() );
884 if( id == KaxCues::ClassInfos.GlobalId )
886 msg_Dbg( p_input, "| | | = cues at "I64Fd,
888 p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
890 else if( id == KaxChapters::ClassInfos.GlobalId )
892 msg_Dbg( p_input, "| | | = chapters at "I64Fd,
894 p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
896 else if( id == KaxTags::ClassInfos.GlobalId )
898 msg_Dbg( p_input, "| | | = tags at "I64Fd,
900 p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
907 msg_Dbg( p_input, "| | + Unknown (%s)",
908 typeid(*el).name() );
913 else if( EbmlId( *el1 ) == KaxCues::ClassInfos.GlobalId )
915 msg_Dbg( p_input, "| + Cues" );
917 else if( EbmlId( *el1 ) == KaxCluster::ClassInfos.GlobalId )
919 msg_Dbg( p_input, "| + Cluster" );
921 p_sys->cluster = (KaxCluster*)el1;
924 /* stop parsing the stream */
927 #ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
928 else if( EbmlId( *el1 ) == KaxAttachments::ClassInfos.GlobalId )
930 else if( EbmlId( *el1 ) == KaxAttachements::ClassInfos.GlobalId )
933 msg_Dbg( p_input, "| + Attachments FIXME TODO (but probably never supported)" );
935 else if( EbmlId( *el1 ) == KaxChapters::ClassInfos.GlobalId )
937 msg_Dbg( p_input, "| + Chapters FIXME TODO" );
939 else if( EbmlId( *el1 ) == KaxTag::ClassInfos.GlobalId )
941 msg_Dbg( p_input, "| + Tags FIXME TODO" );
945 msg_Dbg( p_input, "| + Unknown (%s)", typeid(*el1).name() );
949 if( p_sys->cluster == NULL )
951 msg_Err( p_input, "cannot find any cluster, damaged file ?" );
955 if( p_sys->i_chapters_position >= 0 )
957 msg_Warn( p_input, "chapters unsupported" );
960 /* *** Load the cue if found *** */
961 if( p_sys->i_cues_position >= 0 )
963 vlc_bool_t b_seekable;
965 stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
972 if( !p_sys->b_cues || p_sys->i_index <= 0 )
974 msg_Warn( p_input, "no cues/empty cues found->seek won't be precise" );
976 IndexAppendCluster( p_input, p_sys->cluster );
978 p_sys->b_cues = VLC_FALSE;
981 /* Create one program */
982 vlc_mutex_lock( &p_input->stream.stream_lock );
983 if( input_InitStream( p_input, 0 ) == -1)
985 vlc_mutex_unlock( &p_input->stream.stream_lock );
986 msg_Err( p_input, "cannot init stream" );
989 p_input->stream.i_mux_rate = 0;
990 vlc_mutex_unlock( &p_input->stream.stream_lock );
992 if( p_sys->f_duration > 1001.0 )
994 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000.0 );
995 p_input->stream.i_mux_rate = stream_Size( p_input->s )/50 / i_duration;
999 msg_Dbg( p_input, "found %d es", p_sys->i_track );
1000 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1002 #define tk p_sys->track[i_track]
1003 if( tk.fmt.i_cat == UNKNOWN_ES )
1005 msg_Warn( p_input, "invalid track[%d, n=%d]", i_track, tk.i_number );
1010 if( tk.fmt.i_cat == SPU_ES )
1013 val.psz_string = "UTF-8";
1014 #if defined(HAVE_ICONV)
1015 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1016 var_Set( p_input, "subsdec-encoding", val );
1019 if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) )
1021 if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
1023 msg_Err( p_input, "missing/invalid BITMAPINFOHEADER" );
1024 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1028 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
1030 tk.fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1031 tk.fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1032 tk.fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1034 tk.fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
1035 if( tk.fmt.i_extra > 0 )
1037 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1038 memcpy( tk.fmt.p_extra, &p_bih[1], tk.fmt.i_extra );
1042 else if( !strcmp( tk.psz_codec, "V_MPEG1" ) ||
1043 !strcmp( tk.psz_codec, "V_MPEG2" ) )
1045 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1047 else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) )
1049 if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) )
1051 tk.fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1055 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1058 else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
1060 if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1062 msg_Err( p_input, "missing/invalid WAVEFORMATEX" );
1063 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1067 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;
1069 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk.fmt.i_codec, NULL );
1071 tk.fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1072 tk.fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1073 tk.fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1074 tk.fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1075 tk.fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1077 tk.fmt.i_extra = GetWLE( &p_wf->cbSize );
1078 if( tk.fmt.i_extra > 0 )
1080 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1081 memcpy( tk.fmt.p_extra, &p_wf[1], tk.fmt.i_extra );
1085 else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
1086 !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
1087 !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
1089 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1091 else if( !strcmp( tk.psz_codec, "A_AC3" ) )
1093 tk.fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1095 else if( !strcmp( tk.psz_codec, "A_DTS" ) )
1097 tk.fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
1099 else if( !strcmp( tk.psz_codec, "A_FLAC" ) )
1101 tk.fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
1102 tk.fmt.i_extra = tk.i_extra_data;
1103 tk.fmt.p_extra = malloc( tk.i_extra_data );
1104 memcpy( tk.fmt.p_extra,tk.p_extra_data, tk.i_extra_data );
1106 else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
1108 tk.fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
1109 tk.i_data_init = tk.i_extra_data;
1110 tk.p_data_init = tk.p_extra_data;
1112 else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1113 !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1115 int i_profile, i_srate;
1116 static unsigned int i_sample_rates[] =
1118 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1119 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1122 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1123 /* create data for faad (MP4DecSpecificDescrTag)*/
1125 if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
1129 else if( !strcmp( &tk.psz_codec[12], "LC" ) )
1133 else if( !strcmp( &tk.psz_codec[12], "SSR" ) )
1142 for( i_srate = 0; i_srate < 13; i_srate++ )
1144 if( i_sample_rates[i_srate] == tk.fmt.audio.i_rate )
1149 msg_Dbg( p_input, "profile=%d srate=%d", i_profile, i_srate );
1152 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1153 ((uint8_t*)tk.fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1154 ((uint8_t*)tk.fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk.fmt.audio.i_channels << 3);
1156 else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
1157 !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
1158 !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
1160 if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
1162 tk.fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1166 tk.fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1168 tk.fmt.audio.i_blockalign = ( tk.fmt.audio.i_bitspersample + 7 ) / 8 * tk.fmt.audio.i_channels;
1170 else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
1172 tk.fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1174 else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) ||
1175 !strcmp( tk.psz_codec, "S_SSA" ) ||
1176 !strcmp( tk.psz_codec, "S_ASS" ))
1178 tk.fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1181 tk.fmt.i_extra = sizeof( subtitle_data_t );
1182 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1183 ((es_sys_t*)tk->fmt.p_extra)->psz_header = strdup( (char *)tk.p_extra_data );
1186 else if( !strcmp( tk.psz_codec, "S_VOBSUB" ) )
1188 tk.fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1192 msg_Err( p_input, "unknow codec id=`%s'", tk.psz_codec );
1193 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1196 tk.p_es = es_out_Add( p_input->p_es_out, &tk.fmt );
1200 /* add informations */
1201 InformationsCreate( p_input );
1209 return VLC_EGENERIC;
1212 /*****************************************************************************
1213 * Close: frees unused data
1214 *****************************************************************************/
1215 static void Close( vlc_object_t *p_this )
1217 input_thread_t *p_input = (input_thread_t *)p_this;
1218 demux_sys_t *p_sys = p_input->p_demux_data;
1222 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1224 #define tk p_sys->track[i_track]
1225 if( tk.fmt.psz_description )
1227 free( tk.fmt.psz_description );
1231 free( tk.psz_codec );
1233 if( tk.fmt.psz_language )
1235 free( tk.fmt.psz_language );
1239 free( p_sys->track );
1241 if( p_sys->psz_writing_application )
1243 free( p_sys->psz_writing_application );
1245 if( p_sys->psz_muxing_application )
1247 free( p_sys->psz_muxing_application );
1250 delete p_sys->segment;
1259 /*****************************************************************************
1261 *****************************************************************************/
1262 static int Control( input_thread_t *p_input, int i_query, va_list args )
1264 demux_sys_t *p_sys = p_input->p_demux_data;
1267 vlc_meta_t **pp_meta;
1271 case DEMUX_GET_META:
1272 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1273 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1276 case DEMUX_GET_LENGTH:
1277 pi64 = (int64_t*)va_arg( args, int64_t * );
1278 if( p_sys->f_duration > 0.0 )
1280 *pi64 = (int64_t)(p_sys->f_duration * 1000);
1283 return VLC_EGENERIC;
1286 return demux_vaControlDefault( p_input, i_query, args );
1290 static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1292 demux_sys_t *p_sys = p_input->p_demux_data;
1303 if( p_input->b_die )
1305 return VLC_EGENERIC;
1308 el = p_sys->ep->Get();
1309 i_level = p_sys->ep->GetLevel();
1311 if( el == NULL && *pp_block != NULL )
1313 /* update the index */
1314 #define idx p_sys->index[p_sys->i_index - 1]
1315 if( p_sys->i_index > 0 && idx.i_time == -1 )
1317 idx.i_time = (*pp_block)->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1318 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1326 if( p_sys->ep->GetLevel() > 1 )
1331 msg_Warn( p_input, "EOF" );
1332 return VLC_EGENERIC;
1338 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1340 p_sys->cluster = (KaxCluster*)el;
1342 /* add it to the index */
1343 if( p_sys->i_index == 0 ||
1344 ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
1346 IndexAppendCluster( p_input, p_sys->cluster );
1351 else if( EbmlId( *el ) == KaxCues::ClassInfos.GlobalId )
1353 msg_Warn( p_input, "find KaxCues FIXME" );
1354 return VLC_EGENERIC;
1358 msg_Dbg( p_input, "unknown (%s)", typeid( el ).name() );
1361 else if( i_level == 2 )
1363 if( EbmlId( *el ) == KaxClusterTimecode::ClassInfos.GlobalId )
1365 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1367 ctc.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
1368 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
1370 else if( EbmlId( *el ) == KaxBlockGroup::ClassInfos.GlobalId )
1375 else if( i_level == 3 )
1377 if( EbmlId( *el ) == KaxBlock::ClassInfos.GlobalId )
1379 *pp_block = (KaxBlock*)el;
1381 (*pp_block)->ReadData( p_sys->es->I_O() );
1382 (*pp_block)->SetParent( *p_sys->cluster );
1386 else if( EbmlId( *el ) == KaxBlockDuration::ClassInfos.GlobalId )
1388 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1390 dur.ReadData( p_sys->es->I_O() );
1391 *pi_duration = uint64( dur );
1393 else if( EbmlId( *el ) == KaxReferenceBlock::ClassInfos.GlobalId )
1395 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1397 ref.ReadData( p_sys->es->I_O() );
1398 if( *pi_ref1 == -1 )
1400 *pi_ref1 = int64( ref );
1404 *pi_ref2 = int64( ref );
1410 msg_Err( p_input, "invalid level = %d", i_level );
1411 return VLC_EGENERIC;
1416 static block_t *MemToBlock( input_thread_t *p_input, uint8_t *p_mem, int i_mem)
1419 if( !(p_block = block_New( p_input, i_mem ) ) ) return NULL;
1420 memcpy( p_block->p_buffer, p_mem, i_mem );
1421 //p_block->i_rate = p_input->stream.control.i_rate;
1425 static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts, mtime_t i_duration )
1427 demux_sys_t *p_sys = p_input->p_demux_data;
1433 #define tk p_sys->track[i_track]
1434 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1436 if( tk.i_number == block->TrackNum() )
1442 if( i_track >= p_sys->i_track )
1444 msg_Err( p_input, "invalid track number=%d", block->TrackNum() );
1448 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk.p_es, &b );
1451 tk.b_inited = VLC_FALSE;
1455 if( tk.fmt.i_cat == AUDIO_ES && p_input->stream.control.b_mute )
1460 /* First send init data */
1461 if( !tk.b_inited && tk.i_data_init > 0 )
1465 msg_Dbg( p_input, "sending header (%d bytes)", tk.i_data_init );
1467 if( tk.fmt.i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
1473 /* XXX hack split the 3 headers */
1474 if( tk.p_data_init[0] != 0x02 )
1476 msg_Err( p_input, "invalid vorbis header" );
1479 for( i = 0; i < 2; i++ )
1482 while( i_offset < tk.i_data_init )
1484 i_size[i] += tk.p_data_init[i_offset];
1485 if( tk.p_data_init[i_offset++] != 0xff )
1491 i_size[0] = __MIN( i_size[0], tk.i_data_init - i_offset );
1492 i_size[1] = __MIN( i_size[1], tk.i_data_init - i_offset - i_size[0] );
1493 i_size[2] = tk.i_data_init - i_offset - i_size[0] - i_size[1];
1495 p_init = MemToBlock( p_input, &tk.p_data_init[i_offset], i_size[0] );
1498 es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1500 p_init = MemToBlock( p_input, &tk.p_data_init[i_offset+i_size[0]], i_size[1] );
1503 es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1505 p_init = MemToBlock( p_input, &tk.p_data_init[i_offset+i_size[0]+i_size[1]], i_size[2] );
1508 es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1513 p_init = MemToBlock( p_input, tk.p_data_init, tk.i_data_init );
1516 es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1520 tk.b_inited = VLC_TRUE;
1523 for( i = 0; i < block->NumberFrames(); i++ )
1526 DataBuffer &data = block->GetBuffer(i);
1528 p_block = MemToBlock( p_input, data.Buffer(), data.Size() );
1529 if( p_block == NULL )
1534 if( tk.fmt.i_cat != VIDEO_ES )
1535 p_block->i_dts = p_block->i_pts = i_pts;
1538 p_block->i_dts = i_pts;
1542 if( tk.fmt.i_cat == SPU_ES && strcmp( tk.psz_codec, "S_VOBSUB" ) )
1544 if( i_duration > 0 )
1546 p_block->i_dts += i_duration * 1000;
1553 es_out_Send( p_input->p_es_out, tk.p_es, p_block );
1555 /* use time stamp only for first block */
1562 static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent)
1564 demux_sys_t *p_sys = p_input->p_demux_data;
1567 int64_t i_block_duration;
1568 int64_t i_block_ref1;
1569 int64_t i_block_ref2;
1572 int i_track_skipping;
1575 msg_Dbg( p_input, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1576 if( i_date < 0 && i_percent < 0 )
1582 p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
1583 p_sys->cluster = NULL;
1585 /* seek without index or without date */
1586 if( config_GetInt( p_input, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
1588 int64_t i_pos = i_percent * stream_Size( p_input->s ) / 100;
1590 msg_Dbg( p_input, "inacurate way of seeking" );
1591 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1593 if( p_sys->index[i_index].i_position >= i_pos)
1598 if( i_index == p_sys->i_index )
1603 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1606 if( p_sys->index[i_index].i_position < i_pos )
1610 msg_Warn( p_input, "searching for cluster, could take some time" );
1612 /* search a cluster */
1613 while( ( el = p_sys->ep->Get() ) != NULL )
1615 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1617 KaxCluster *cluster = (KaxCluster*)el;
1619 /* add it to the index */
1620 IndexAppendCluster( p_input, cluster );
1622 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1624 p_sys->cluster = cluster;
1634 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1636 if( p_sys->index[i_index].i_time >= i_date )
1647 msg_Dbg( p_input, "seek got "I64Fd" (%d%%)",
1648 p_sys->index[i_index].i_time,
1649 (int)( 100 * p_sys->index[i_index].i_position /
1650 stream_Size( p_input->s ) ) );
1652 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1656 /* now parse until key frame */
1657 #define tk p_sys->track[i_track]
1658 i_track_skipping = 0;
1659 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1661 if( tk.fmt.i_cat == VIDEO_ES )
1663 tk.b_search_keyframe = VLC_TRUE;
1668 while( i_track_skipping > 0 )
1670 if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1672 msg_Warn( p_input, "cannot get block EOF?" );
1677 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1679 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1681 if( tk.i_number == block->TrackNum() )
1687 if( i_track < p_sys->i_track )
1689 if( tk.fmt.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1691 tk.b_search_keyframe = VLC_FALSE;
1694 if( tk.fmt.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1696 BlockDecode( p_input, block, 0, 0 );
1705 /*****************************************************************************
1706 * Demux: reads and demuxes data packets
1707 *****************************************************************************
1708 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1709 *****************************************************************************/
1710 static int Demux( input_thread_t * p_input )
1712 demux_sys_t *p_sys = p_input->p_demux_data;
1713 mtime_t i_start_pts;
1714 int i_block_count = 0;
1717 int64_t i_block_duration;
1718 int64_t i_block_ref1;
1719 int64_t i_block_ref2;
1721 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1723 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
1724 mtime_t i_date = -1;
1727 if( i_duration > 0 )
1729 i_date = (mtime_t)1000000 *
1730 (mtime_t)i_duration*
1731 (mtime_t)p_sys->in->getFilePointer() /
1732 (mtime_t)stream_Size( p_input->s );
1734 if( stream_Size( p_input->s ) > 0 )
1736 i_percent = 100 * p_sys->in->getFilePointer() /
1737 stream_Size( p_input->s );
1740 Seek( p_input, i_date, i_percent);
1749 if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1751 msg_Warn( p_input, "cannot get block EOF?" );
1756 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1758 if( p_sys->i_pts > 0 )
1760 input_ClockManageRef( p_input,
1761 p_input->stream.p_selected_program,
1762 p_sys->i_pts * 9 / 100 );
1765 i_pts = input_ClockGetTS( p_input,
1766 p_input->stream.p_selected_program,
1767 p_sys->i_pts * 9 / 100 );
1771 BlockDecode( p_input, block, i_pts, i_block_duration );
1776 if( i_start_pts == -1 )
1778 i_start_pts = p_sys->i_pts;
1780 else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1789 /*****************************************************************************
1791 *****************************************************************************/
1792 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1798 uint32_t vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1800 if( i_size <= 0 || mb_eof )
1805 return stream_Read( s, p_buffer, i_size );
1807 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1813 case seek_beginning:
1817 i_pos = stream_Size( s ) - i_offset;
1820 i_pos= stream_Tell( s ) + i_offset;
1824 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1831 if( stream_Seek( s, i_pos ) )
1837 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1841 uint64_t vlc_stream_io_callback::getFilePointer( void )
1843 return stream_Tell( s );
1845 void vlc_stream_io_callback::close( void )
1851 /*****************************************************************************
1852 * Ebml Stream parser
1853 *****************************************************************************/
1854 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1862 for( i = 1; i < 6; i++ )
1868 mb_keep = VLC_FALSE;
1871 EbmlParser::~EbmlParser( void )
1875 for( i = 1; i < mi_level; i++ )
1881 mb_keep = VLC_FALSE;
1885 void EbmlParser::Up( void )
1887 if( mi_user_level == mi_level )
1889 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1895 void EbmlParser::Down( void )
1901 void EbmlParser::Keep( void )
1906 int EbmlParser::GetLevel( void )
1908 return mi_user_level;
1911 EbmlElement *EbmlParser::Get( void )
1915 if( mi_user_level != mi_level )
1921 EbmlElement *ret = m_got;
1927 if( m_el[mi_level] )
1929 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
1932 delete m_el[mi_level];
1934 mb_keep = VLC_FALSE;
1937 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
1948 delete m_el[mi_level - 1];
1949 m_got = m_el[mi_level -1] = m_el[mi_level];
1950 m_el[mi_level] = NULL;
1957 else if( m_el[mi_level] == NULL )
1959 fprintf( stderr," m_el[mi_level] == NULL\n" );
1962 return m_el[mi_level];
1966 /*****************************************************************************
1968 * * LoadCues : load the cues element and update index
1970 * * LoadTags : load ... the tags element
1972 * * InformationsCreate : create all informations, load tags if present
1974 *****************************************************************************/
1975 static void LoadCues( input_thread_t *p_input )
1977 demux_sys_t *p_sys = p_input->p_demux_data;
1978 int64_t i_sav_position = p_sys->in->getFilePointer();
1980 EbmlElement *el, *cues;
1982 msg_Dbg( p_input, "loading cues" );
1983 p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
1984 cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
1988 msg_Err( p_input, "cannot load cues (broken seekhead or file)" );
1989 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1993 ep = new EbmlParser( p_sys->es, cues );
1994 while( ( el = ep->Get() ) != NULL )
1996 if( EbmlId( *el ) == KaxCuePoint::ClassInfos.GlobalId )
1998 #define idx p_sys->index[p_sys->i_index]
2001 idx.i_block_number= -1;
2002 idx.i_position = -1;
2004 idx.b_key = VLC_TRUE;
2007 while( ( el = ep->Get() ) != NULL )
2009 if( EbmlId( *el ) == KaxCueTime::ClassInfos.GlobalId )
2011 KaxCueTime &ctime = *(KaxCueTime*)el;
2013 ctime.ReadData( p_sys->es->I_O() );
2015 idx.i_time = uint64( ctime ) * (mtime_t)1000000000 / p_sys->i_timescale;
2017 else if( EbmlId( *el ) == KaxCueTrackPositions::ClassInfos.GlobalId )
2020 while( ( el = ep->Get() ) != NULL )
2022 if( EbmlId( *el ) == KaxCueTrack::ClassInfos.GlobalId )
2024 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
2026 ctrack.ReadData( p_sys->es->I_O() );
2027 idx.i_track = uint16( ctrack );
2029 else if( EbmlId( *el ) == KaxCueClusterPosition::ClassInfos.GlobalId )
2031 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
2033 ccpos.ReadData( p_sys->es->I_O() );
2034 idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
2036 else if( EbmlId( *el ) == KaxCueBlockNumber::ClassInfos.GlobalId )
2038 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2040 cbnum.ReadData( p_sys->es->I_O() );
2041 idx.i_block_number = uint32( cbnum );
2045 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2052 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2058 msg_Dbg( p_input, " * added time="I64Fd" pos="I64Fd
2059 " track=%d bnum=%d", idx.i_time, idx.i_position,
2060 idx.i_track, idx.i_block_number );
2064 if( p_sys->i_index >= p_sys->i_index_max )
2066 p_sys->i_index_max += 1024;
2067 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2073 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2079 p_sys->b_cues = VLC_TRUE;
2081 msg_Dbg( p_input, "loading cues done." );
2082 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2085 static void LoadTags( input_thread_t *p_input )
2087 demux_sys_t *p_sys = p_input->p_demux_data;
2088 int64_t i_sav_position = p_sys->in->getFilePointer();
2090 EbmlElement *el, *tags;
2092 msg_Dbg( p_input, "loading tags" );
2093 p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
2094 tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2098 msg_Err( p_input, "cannot load tags (broken seekhead or file)" );
2099 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2103 msg_Dbg( p_input, "Tags" );
2104 ep = new EbmlParser( p_sys->es, tags );
2105 while( ( el = ep->Get() ) != NULL )
2107 if( EbmlId( *el ) == KaxTag::ClassInfos.GlobalId )
2109 msg_Dbg( p_input, "+ Tag" );
2111 while( ( el = ep->Get() ) != NULL )
2113 if( EbmlId( *el ) == KaxTagTargets::ClassInfos.GlobalId )
2115 msg_Dbg( p_input, "| + Targets" );
2117 while( ( el = ep->Get() ) != NULL )
2119 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2123 else if( EbmlId( *el ) == KaxTagGeneral::ClassInfos.GlobalId )
2125 msg_Dbg( p_input, "| + General" );
2127 while( ( el = ep->Get() ) != NULL )
2129 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2133 else if( EbmlId( *el ) == KaxTagGenres::ClassInfos.GlobalId )
2135 msg_Dbg( p_input, "| + Genres" );
2137 while( ( el = ep->Get() ) != NULL )
2139 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2143 else if( EbmlId( *el ) == KaxTagAudioSpecific::ClassInfos.GlobalId )
2145 msg_Dbg( p_input, "| + Audio Specific" );
2147 while( ( el = ep->Get() ) != NULL )
2149 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2153 else if( EbmlId( *el ) == KaxTagImageSpecific::ClassInfos.GlobalId )
2155 msg_Dbg( p_input, "| + Images Specific" );
2157 while( ( el = ep->Get() ) != NULL )
2159 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2163 else if( EbmlId( *el ) == KaxTagMultiComment::ClassInfos.GlobalId )
2165 msg_Dbg( p_input, "| + Multi Comment" );
2167 else if( EbmlId( *el ) == KaxTagMultiCommercial::ClassInfos.GlobalId )
2169 msg_Dbg( p_input, "| + Multi Commercial" );
2171 else if( EbmlId( *el ) == KaxTagMultiDate::ClassInfos.GlobalId )
2173 msg_Dbg( p_input, "| + Multi Date" );
2175 else if( EbmlId( *el ) == KaxTagMultiEntity::ClassInfos.GlobalId )
2177 msg_Dbg( p_input, "| + Multi Entity" );
2179 else if( EbmlId( *el ) == KaxTagMultiIdentifier::ClassInfos.GlobalId )
2181 msg_Dbg( p_input, "| + Multi Identifier" );
2183 else if( EbmlId( *el ) == KaxTagMultiLegal::ClassInfos.GlobalId )
2185 msg_Dbg( p_input, "| + Multi Legal" );
2187 else if( EbmlId( *el ) == KaxTagMultiTitle::ClassInfos.GlobalId )
2189 msg_Dbg( p_input, "| + Multi Title" );
2193 msg_Dbg( p_input, "| + Unknown (%s)", typeid( *el ).name() );
2200 msg_Dbg( p_input, "+ Unknown (%s)", typeid( *el ).name() );
2206 msg_Dbg( p_input, "loading tags done." );
2207 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2210 static void InformationsCreate( input_thread_t *p_input )
2212 demux_sys_t *p_sys = p_input->p_demux_data;
2215 p_sys->meta = vlc_meta_New();
2217 if( p_sys->psz_title )
2219 vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_sys->psz_title );
2221 if( p_sys->psz_date_utc )
2223 vlc_meta_Add( p_sys->meta, VLC_META_DATE, p_sys->psz_date_utc );
2225 if( p_sys->psz_segment_filename )
2227 vlc_meta_Add( p_sys->meta, _("Segment filename"), p_sys->psz_segment_filename );
2229 if( p_sys->psz_muxing_application )
2231 vlc_meta_Add( p_sys->meta, _("Muxing application"), p_sys->psz_muxing_application );
2233 if( p_sys->psz_writing_application )
2235 vlc_meta_Add( p_sys->meta, _("Writing application"), p_sys->psz_writing_application );
2238 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
2240 mkv_track_t *tk = &p_sys->track[i_track];
2241 vlc_meta_t *mtk = vlc_meta_New();
2243 TAB_APPEND( p_sys->meta->i_track, p_sys->meta->track, mtk );
2245 if( tk->fmt.psz_description )
2247 vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
2249 if( tk->psz_codec_name )
2251 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
2253 if( tk->psz_codec_settings )
2255 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, tk->psz_codec_settings );
2257 if( tk->psz_codec_info_url )
2259 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
2261 if( tk->psz_codec_download_url )
2263 vlc_meta_Add( p_sys->meta, VLC_META_URL, tk->psz_codec_download_url );
2267 if( p_sys->i_tags_position >= 0 )
2269 vlc_bool_t b_seekable;
2271 stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
2274 LoadTags( p_input );
2280 /*****************************************************************************
2282 *****************************************************************************/
2284 static void IndexAppendCluster( input_thread_t *p_input, KaxCluster *cluster )
2286 demux_sys_t *p_sys = p_input->p_demux_data;
2288 #define idx p_sys->index[p_sys->i_index]
2290 idx.i_block_number= -1;
2291 idx.i_position = cluster->GetElementPosition();
2293 idx.b_key = VLC_TRUE;
2296 if( p_sys->i_index >= p_sys->i_index_max )
2298 p_sys->i_index_max += 1024;
2299 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2304 static char * UTF8ToStr( const UTFstring &u )
2313 p = dst = (char*)malloc( i_src + 1);