1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mkv.cpp,v 1.24 2003/08/26 18:11:02 fenrir Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
32 # include <time.h> /* time() */
35 #include <vlc/input.h>
37 #include <codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
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 add_category_hint( N_("mkv-demuxer"), NULL, VLC_TRUE );
96 add_bool( "mkv-seek-percent", 1, NULL,
97 N_("Seek based on percent not time"),
98 N_("Seek based on percent not time"), VLC_TRUE );
100 set_description( _("mka/mkv stream demuxer" ) );
101 set_capability( "demux", 50 );
102 set_callbacks( Open, Close );
103 add_shortcut( "mka" );
104 add_shortcut( "mkv" );
107 /*****************************************************************************
109 *****************************************************************************/
110 static int Demux ( input_thread_t * );
111 static void Seek ( input_thread_t *, mtime_t i_date, int i_percent );
114 /*****************************************************************************
116 *****************************************************************************/
117 class vlc_stream_io_callback: public IOCallback
124 vlc_stream_io_callback( stream_t * );
126 virtual uint32_t read ( void *p_buffer, size_t i_size);
127 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
128 virtual size_t write ( const void *p_buffer, size_t i_size);
129 virtual uint64_t getFilePointer ( void );
130 virtual void close ( void );
133 /*****************************************************************************
135 *****************************************************************************/
139 EbmlParser( EbmlStream *es, EbmlElement *el_start );
144 EbmlElement *Get( void );
147 int GetLevel( void );
152 EbmlElement *m_el[6];
161 /*****************************************************************************
162 * Some functions to manipulate memory
163 *****************************************************************************/
164 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
165 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
167 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
170 /*****************************************************************************
171 * definitions of structures and functions used by this plugins
172 *****************************************************************************/
176 vlc_bool_t b_default;
177 vlc_bool_t b_enabled;
181 uint8_t *p_extra_data;
186 vlc_fourcc_t i_codec;
188 uint64_t i_default_duration;
189 float f_timecodescale;
194 int i_display_height;
203 es_descriptor_t *p_es;
208 /* data to be send first */
210 uint8_t *p_data_init;
212 /* hack : it's for seek */
213 vlc_bool_t b_search_keyframe;
217 char *psz_codec_name;
218 char *psz_codec_settings;
219 char *psz_codec_info_url;
220 char *psz_codec_download_url;
239 vlc_stream_io_callback *in;
244 uint64_t i_timescale;
246 /* duration of the segment */
254 int64_t i_cues_position;
255 int64_t i_chapters_position;
256 int64_t i_tags_position;
270 char *psz_muxing_application;
271 char *psz_writing_application;
272 char *psz_segment_filename;
277 #define MKVD_TIMECODESCALE 1000000
279 static void IndexAppendCluster ( input_thread_t *p_input, KaxCluster *cluster );
280 static char *UTF8ToStr ( const UTFstring &u );
281 static void LoadCues ( input_thread_t *);
282 static void InformationsCreate ( input_thread_t *p_input );
284 static char *LanguageGetName ( const char *psz_code );
286 /*****************************************************************************
287 * Open: initializes matroska demux structures
288 *****************************************************************************/
289 static int Open( vlc_object_t * p_this )
291 input_thread_t *p_input = (input_thread_t *)p_this;
296 vlc_bool_t b_audio_selected;
297 int i_spu_channel, i_audio_channel;
299 EbmlElement *el = NULL, *el1 = NULL, *el2 = NULL, *el3 = NULL, *el4 = NULL;
301 /* Set the demux function */
302 p_input->pf_demux = Demux;
304 /* peek the begining */
305 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
307 msg_Warn( p_input, "cannot peek" );
311 /* is a valid file */
312 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
313 p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
315 msg_Warn( p_input, "matroska module discarded "
316 "(invalid header 0x%.2x%.2x%.2x%.2x)",
317 p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
321 p_input->p_demux_data = p_sys = (demux_sys_t*)malloc(sizeof( demux_sys_t ));
322 memset( p_sys, 0, sizeof( demux_sys_t ) );
324 if( ( p_sys->s = stream_OpenInput( p_input ) ) == NULL )
326 msg_Err( p_input, "cannot create stream" );
330 p_sys->in = new vlc_stream_io_callback( p_sys->s );
331 p_sys->es = new EbmlStream( *p_sys->in );
332 p_sys->f_duration = -1;
333 p_sys->i_timescale = MKVD_TIMECODESCALE;
335 p_sys->track = (mkv_track_t*)malloc( sizeof( mkv_track_t ) );
337 p_sys->i_cues_position = -1;
338 p_sys->i_chapters_position = -1;
339 p_sys->i_tags_position = -1;
341 p_sys->b_cues = VLC_FALSE;
343 p_sys->i_index_max = 1024;
344 p_sys->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
345 p_sys->i_index_max );
347 p_sys->psz_muxing_application = NULL;
348 p_sys->psz_writing_application = NULL;
349 p_sys->psz_segment_filename = NULL;
350 p_sys->psz_title = NULL;
351 p_sys->psz_date_utc = NULL;;
353 if( p_sys->es == NULL )
355 msg_Err( p_input, "failed to create EbmlStream" );
357 stream_Release( p_sys->s );
361 /* Find the EbmlHead element */
362 el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
365 msg_Err( p_input, "cannot find EbmlHead" );
368 msg_Dbg( p_input, "EbmlHead" );
370 el->SkipData( *p_sys->es, el->Generic().Context );
374 el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
377 msg_Err( p_input, "cannot find KaxSegment" );
380 msg_Dbg( p_input, "+ Segment" );
381 p_sys->segment = (KaxSegment*)el;
382 p_sys->cluster = NULL;
384 p_sys->ep = new EbmlParser( p_sys->es, el );
386 while( ( el1 = p_sys->ep->Get() ) != NULL )
388 if( EbmlId( *el1 ) == KaxInfo::ClassInfos.GlobalId )
390 msg_Dbg( p_input, "| + Informations" );
393 while( ( el2 = p_sys->ep->Get() ) != NULL )
395 if( EbmlId( *el2 ) == KaxTimecodeScale::ClassInfos.GlobalId )
397 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)el2;
399 tcs.ReadData( p_sys->es->I_O() );
400 p_sys->i_timescale = uint64(tcs);
402 msg_Dbg( p_input, "| | + TimecodeScale="I64Fd,
403 p_sys->i_timescale );
405 else if( EbmlId( *el2 ) == KaxDuration::ClassInfos.GlobalId )
407 KaxDuration &dur = *(KaxDuration*)el2;
409 dur.ReadData( p_sys->es->I_O() );
410 p_sys->f_duration = float(dur);
412 msg_Dbg( p_input, "| | + Duration=%f",
415 else if( EbmlId( *el2 ) == KaxMuxingApp::ClassInfos.GlobalId )
417 KaxMuxingApp &mapp = *(KaxMuxingApp*)el2;
419 mapp.ReadData( p_sys->es->I_O() );
421 p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
423 msg_Dbg( p_input, "| | + Muxing Application=%s",
424 p_sys->psz_muxing_application );
426 else if( EbmlId( *el2 ) == KaxWritingApp::ClassInfos.GlobalId )
428 KaxWritingApp &wapp = *(KaxWritingApp*)el2;
430 wapp.ReadData( p_sys->es->I_O() );
432 p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
434 msg_Dbg( p_input, "| | + Wrinting Application=%s",
435 p_sys->psz_writing_application );
437 else if( EbmlId( *el2 ) == KaxSegmentFilename::ClassInfos.GlobalId )
439 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)el2;
441 sfn.ReadData( p_sys->es->I_O() );
443 p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
445 msg_Dbg( p_input, "| | + Segment Filename=%s",
446 p_sys->psz_segment_filename );
448 else if( EbmlId( *el2 ) == KaxTitle::ClassInfos.GlobalId )
450 KaxTitle &title = *(KaxTitle*)el2;
452 title.ReadData( p_sys->es->I_O() );
454 p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
456 msg_Dbg( p_input, "| | + Title=%s", p_sys->psz_title );
459 else if( EbmlId( *el2 ) == KaxDateUTC::ClassInfos.GlobalId )
461 KaxDateUTC &date = *(KaxDateUTC*)el2;
466 date.ReadData( p_sys->es->I_O() );
468 i_date = date.GetEpochDate();
469 memset( buffer, 0, 256 );
470 if( gmtime_r( &i_date, &tmres ) &&
471 asctime_r( &tmres, buffer ) )
473 buffer[strlen( buffer)-1]= '\0';
474 p_sys->psz_date_utc = strdup( buffer );
475 msg_Dbg( p_input, "| | + Date=%s", p_sys->psz_date_utc );
481 msg_Dbg( p_input, "| | + Unknown (%s)", typeid(*el2).name() );
486 else if( EbmlId( *el1 ) == KaxTracks::ClassInfos.GlobalId )
488 msg_Dbg( p_input, "| + Tracks" );
491 while( ( el2 = p_sys->ep->Get() ) != NULL )
493 if( EbmlId( *el2 ) == KaxTrackEntry::ClassInfos.GlobalId )
495 msg_Dbg( p_input, "| | + Track Entry" );
498 p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
499 #define tk p_sys->track[p_sys->i_track - 1]
500 memset( &tk, 0, sizeof( mkv_track_t ) );
501 tk.i_cat = UNKNOWN_ES;
502 tk.b_default = VLC_TRUE;
503 tk.b_enabled = VLC_TRUE;
504 tk.i_number = p_sys->i_track - 1;
506 tk.p_extra_data = NULL;
509 tk.psz_language = NULL;
510 tk.i_default_duration = 0;
511 tk.f_timecodescale = 1.0;
513 tk.b_inited = VLC_FALSE;
515 tk.p_data_init = NULL;
518 tk.psz_codec_name = NULL;
519 tk.psz_codec_settings = NULL;
520 tk.psz_codec_info_url = NULL;
521 tk.psz_codec_download_url = NULL;
525 while( ( el3 = p_sys->ep->Get() ) != NULL )
527 if( EbmlId( *el3 ) == KaxTrackNumber::ClassInfos.GlobalId )
529 KaxTrackNumber &tnum = *(KaxTrackNumber*)el3;
530 tnum.ReadData( p_sys->es->I_O() );
532 tk.i_number = uint32( tnum );
533 msg_Dbg( p_input, "| | | + Track Number=%u",
536 else if( EbmlId( *el3 ) == KaxTrackUID::ClassInfos.GlobalId )
538 KaxTrackUID &tuid = *(KaxTrackUID*)el3;
539 tuid.ReadData( p_sys->es->I_O() );
541 msg_Dbg( p_input, "| | | + Track UID=%u",
544 else if( EbmlId( *el3 ) == KaxTrackType::ClassInfos.GlobalId )
547 KaxTrackType &ttype = *(KaxTrackType*)el3;
548 ttype.ReadData( p_sys->es->I_O() );
549 switch( uint8(ttype) )
560 psz_type = "subtitle";
564 psz_type = "unknown";
565 tk.i_cat = UNKNOWN_ES;
569 msg_Dbg( p_input, "| | | + Track Type=%s",
572 else if( EbmlId( *el3 ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
574 KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)el3;
575 fenb.ReadData( p_sys->es->I_O() );
577 tk.b_enabled = uint32( fenb );
578 msg_Dbg( p_input, "| | | + Track Enabled=%u",
581 else if( EbmlId( *el3 ) == KaxTrackFlagDefault::ClassInfos.GlobalId )
583 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)el3;
584 fdef.ReadData( p_sys->es->I_O() );
586 tk.b_default = uint32( fdef );
587 msg_Dbg( p_input, "| | | + Track Default=%u",
590 else if( EbmlId( *el3 ) == KaxTrackFlagLacing::ClassInfos.GlobalId )
592 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)el3;
593 lac.ReadData( p_sys->es->I_O() );
595 msg_Dbg( p_input, "| | | + Track Lacing=%d",
598 else if( EbmlId( *el3 ) == KaxTrackMinCache::ClassInfos.GlobalId )
600 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)el3;
601 cmin.ReadData( p_sys->es->I_O() );
603 msg_Dbg( p_input, "| | | + Track MinCache=%d",
606 else if( EbmlId( *el3 ) == KaxTrackMaxCache::ClassInfos.GlobalId )
608 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)el3;
609 cmax.ReadData( p_sys->es->I_O() );
611 msg_Dbg( p_input, "| | | + Track MaxCache=%d",
614 else if( EbmlId( *el3 ) == KaxTrackDefaultDuration::ClassInfos.GlobalId )
616 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)el3;
617 defd.ReadData( p_sys->es->I_O() );
619 tk.i_default_duration = uint64(defd);
620 msg_Dbg( p_input, "| | | + Track Default Duration="I64Fd, uint64(defd) );
622 else if( EbmlId( *el3 ) == KaxTrackTimecodeScale::ClassInfos.GlobalId )
624 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)el3;
625 ttcs.ReadData( p_sys->es->I_O() );
627 tk.f_timecodescale = float( ttcs );
628 msg_Dbg( p_input, "| | | + Track TimeCodeScale=%f", tk.f_timecodescale );
630 else if( EbmlId( *el3 ) == KaxTrackName::ClassInfos.GlobalId )
632 KaxTrackName &tname = *(KaxTrackName*)el3;
633 tname.ReadData( p_sys->es->I_O() );
635 tk.psz_name = UTF8ToStr( UTFstring( tname ) );
636 msg_Dbg( p_input, "| | | + Track Name=%s",
639 else if( EbmlId( *el3 ) == KaxTrackLanguage::ClassInfos.GlobalId )
641 KaxTrackLanguage &lang = *(KaxTrackLanguage*)el3;
642 lang.ReadData( p_sys->es->I_O() );
645 LanguageGetName( string( lang ).c_str() );
647 "| | | + Track Language=`%s'(%s) ",
648 tk.psz_language, string( lang ).c_str() );
650 else if( EbmlId( *el3 ) == KaxCodecID::ClassInfos.GlobalId )
652 KaxCodecID &codecid = *(KaxCodecID*)el3;
653 codecid.ReadData( p_sys->es->I_O() );
655 tk.psz_codec = strdup( string( codecid ).c_str() );
656 msg_Dbg( p_input, "| | | + Track CodecId=%s",
657 string( codecid ).c_str() );
659 else if( EbmlId( *el3 ) == KaxCodecPrivate::ClassInfos.GlobalId )
661 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)el3;
662 cpriv.ReadData( p_sys->es->I_O() );
664 tk.i_extra_data = cpriv.GetSize();
665 if( tk.i_extra_data > 0 )
667 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
668 memcpy( tk.p_extra_data, cpriv.GetBuffer(), tk.i_extra_data );
670 msg_Dbg( p_input, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
672 else if( EbmlId( *el3 ) == KaxCodecName::ClassInfos.GlobalId )
674 KaxCodecName &cname = *(KaxCodecName*)el3;
675 cname.ReadData( p_sys->es->I_O() );
677 tk.psz_codec_name = UTF8ToStr( UTFstring( cname ) );
678 msg_Dbg( p_input, "| | | + Track Codec Name=%s", tk.psz_codec_name );
680 else if( EbmlId( *el3 ) == KaxCodecSettings::ClassInfos.GlobalId )
682 KaxCodecSettings &cset = *(KaxCodecSettings*)el3;
683 cset.ReadData( p_sys->es->I_O() );
685 tk.psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
686 msg_Dbg( p_input, "| | | + Track Codec Settings=%s", tk.psz_codec_settings );
688 else if( EbmlId( *el3 ) == KaxCodecInfoURL::ClassInfos.GlobalId )
690 KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)el3;
691 ciurl.ReadData( p_sys->es->I_O() );
693 tk.psz_codec_info_url = strdup( string( ciurl ).c_str() );
694 msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_info_url );
696 else if( EbmlId( *el3 ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
698 KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)el3;
699 cdurl.ReadData( p_sys->es->I_O() );
701 tk.psz_codec_download_url = strdup( string( cdurl ).c_str() );
702 msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_download_url );
704 else if( EbmlId( *el3 ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
706 KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)el3;
707 cdall.ReadData( p_sys->es->I_O() );
709 msg_Dbg( p_input, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
711 else if( EbmlId( *el3 ) == KaxTrackOverlay::ClassInfos.GlobalId )
713 KaxTrackOverlay &tovr = *(KaxTrackOverlay*)el3;
714 tovr.ReadData( p_sys->es->I_O() );
716 msg_Dbg( p_input, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
718 else if( EbmlId( *el3 ) == KaxTrackVideo::ClassInfos.GlobalId )
720 msg_Dbg( p_input, "| | | + Track Video" );
723 tk.i_display_width = 0;
724 tk.i_display_height = 0;
729 while( ( el4 = p_sys->ep->Get() ) != NULL )
731 if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
733 KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
734 fint.ReadData( p_sys->es->I_O() );
736 msg_Dbg( p_input, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
738 else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
740 KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
741 stereo.ReadData( p_sys->es->I_O() );
743 msg_Dbg( p_input, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
745 else if( EbmlId( *el4 ) == KaxVideoPixelWidth::ClassInfos.GlobalId )
747 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)el4;
748 vwidth.ReadData( p_sys->es->I_O() );
750 tk.i_width = uint16( vwidth );
751 msg_Dbg( p_input, "| | | | + width=%d", uint16( vwidth ) );
753 else if( EbmlId( *el4 ) == KaxVideoPixelHeight::ClassInfos.GlobalId )
755 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)el4;
756 vheight.ReadData( p_sys->es->I_O() );
758 tk.i_height = uint16( vheight );
759 msg_Dbg( p_input, "| | | | + height=%d", uint16( vheight ) );
761 else if( EbmlId( *el4 ) == KaxVideoDisplayWidth::ClassInfos.GlobalId )
763 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)el4;
764 vwidth.ReadData( p_sys->es->I_O() );
766 tk.i_display_width = uint16( vwidth );
767 msg_Dbg( p_input, "| | | | + display width=%d", uint16( vwidth ) );
769 else if( EbmlId( *el4 ) == KaxVideoDisplayHeight::ClassInfos.GlobalId )
771 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)el4;
772 vheight.ReadData( p_sys->es->I_O() );
774 tk.i_display_height = uint16( vheight );
775 msg_Dbg( p_input, "| | | | + display height=%d", uint16( vheight ) );
777 else if( EbmlId( *el4 ) == KaxVideoFrameRate::ClassInfos.GlobalId )
779 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)el4;
780 vfps.ReadData( p_sys->es->I_O() );
782 tk.f_fps = float( vfps );
783 msg_Dbg( p_input, " | | | + fps=%f", float( vfps ) );
785 else if( EbmlId( *el4 ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
787 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)el4;
788 vdmode.ReadData( p_sys->es->I_O() );
790 msg_Dbg( p_input, "| | | | + Track Video Display Unit=%s",
791 uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
793 else if( EbmlId( *el4 ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
795 KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)el4;
796 ratio.ReadData( p_sys->es->I_O() );
798 msg_Dbg( p_input, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
800 else if( EbmlId( *el4 ) == KaxVideoGamma::ClassInfos.GlobalId )
802 KaxVideoGamma &gamma = *(KaxVideoGamma*)el4;
803 gamma.ReadData( p_sys->es->I_O() );
805 msg_Dbg( p_input, " | | | + fps=%f", float( gamma ) );
809 msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() );
814 else if( EbmlId( *el3 ) == KaxTrackAudio::ClassInfos.GlobalId )
816 msg_Dbg( p_input, "| | | + Track Audio" );
819 tk.i_bitspersample = 0;
823 while( ( el4 = p_sys->ep->Get() ) != NULL )
825 if( EbmlId( *el4 ) == KaxAudioSamplingFreq::ClassInfos.GlobalId )
827 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)el4;
828 afreq.ReadData( p_sys->es->I_O() );
830 tk.i_samplerate = (int)float( afreq );
831 msg_Dbg( p_input, "| | | | + afreq=%d", tk.i_samplerate );
833 else if( EbmlId( *el4 ) == KaxAudioChannels::ClassInfos.GlobalId )
835 KaxAudioChannels &achan = *(KaxAudioChannels*)el4;
836 achan.ReadData( p_sys->es->I_O() );
838 tk.i_channels = uint8( achan );
839 msg_Dbg( p_input, "| | | | + achan=%u", uint8( achan ) );
841 else if( EbmlId( *el4 ) == KaxAudioBitDepth::ClassInfos.GlobalId )
843 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)el4;
844 abits.ReadData( p_sys->es->I_O() );
846 tk.i_bitspersample = uint8( abits );
847 msg_Dbg( p_input, "| | | | + abits=%u", uint8( abits ) );
851 msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() );
858 msg_Dbg( p_input, "| | | + Unknown (%s)",
859 typeid(*el3).name() );
866 msg_Dbg( p_input, "| | + Unknown (%s)",
867 typeid(*el2).name() );
873 else if( EbmlId( *el1 ) == KaxSeekHead::ClassInfos.GlobalId )
875 msg_Dbg( p_input, "| + Seek head" );
877 while( ( el = p_sys->ep->Get() ) != NULL )
879 if( EbmlId( *el ) == KaxSeek::ClassInfos.GlobalId )
881 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
884 //msg_Dbg( p_input, "| | + Seek" );
886 while( ( el = p_sys->ep->Get() ) != NULL )
888 if( EbmlId( *el ) == KaxSeekID::ClassInfos.GlobalId )
890 KaxSeekID &sid = *(KaxSeekID*)el;
892 sid.ReadData( p_sys->es->I_O() );
894 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
896 else if( EbmlId( *el ) == KaxSeekPosition::ClassInfos.GlobalId )
898 KaxSeekPosition &spos = *(KaxSeekPosition*)el;
900 spos.ReadData( p_sys->es->I_O() );
902 i_pos = uint64( spos );
906 msg_Dbg( p_input, "| | | + Unknown (%s)",
907 typeid(*el).name() );
914 if( id == KaxCues::ClassInfos.GlobalId )
916 msg_Dbg( p_input, "| | | = cues at "I64Fd,
918 p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
920 else if( id == KaxChapters::ClassInfos.GlobalId )
922 msg_Dbg( p_input, "| | | = chapters at "I64Fd,
924 p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
926 else if( id == KaxTags::ClassInfos.GlobalId )
928 msg_Dbg( p_input, "| | | = tags at "I64Fd,
930 p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
937 msg_Dbg( p_input, "| | + Unknown (%s)",
938 typeid(*el).name() );
943 else if( EbmlId( *el1 ) == KaxCues::ClassInfos.GlobalId )
945 msg_Dbg( p_input, "| + Cues" );
947 else if( EbmlId( *el1 ) == KaxCluster::ClassInfos.GlobalId )
949 msg_Dbg( p_input, "| + Cluster" );
951 p_sys->cluster = (KaxCluster*)el1;
954 /* stop parsing the stream */
957 #ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
958 else if( EbmlId( *el1 ) == KaxAttachments::ClassInfos.GlobalId )
960 else if( EbmlId( *el1 ) == KaxAttachements::ClassInfos.GlobalId )
963 msg_Dbg( p_input, "| + Attachments FIXME TODO (but probably never supported)" );
965 else if( EbmlId( *el1 ) == KaxChapters::ClassInfos.GlobalId )
967 msg_Dbg( p_input, "| + Chapters FIXME TODO" );
969 else if( EbmlId( *el1 ) == KaxTag::ClassInfos.GlobalId )
971 msg_Dbg( p_input, "| + Tags FIXME TODO" );
975 msg_Dbg( p_input, "| + Unknown (%s)", typeid(*el1).name() );
979 if( p_sys->cluster == NULL )
981 msg_Err( p_input, "cannot find any cluster, damaged file ?" );
985 if( p_sys->i_chapters_position >= 0 )
987 msg_Warn( p_input, "chapters unsupported" );
990 /* *** Load the cue if found *** */
991 if( p_sys->i_cues_position >= 0 )
993 vlc_bool_t b_seekable;
995 stream_Control( p_sys->s, STREAM_CAN_FASTSEEK, &b_seekable );
1002 if( !p_sys->b_cues || p_sys->i_index <= 0 )
1004 msg_Warn( p_input, "no cues/empty cues found->seek won't be precise" );
1006 IndexAppendCluster( p_input, p_sys->cluster );
1008 p_sys->b_cues = VLC_FALSE;
1011 /* Create one program */
1012 vlc_mutex_lock( &p_input->stream.stream_lock );
1013 if( input_InitStream( p_input, 0 ) == -1)
1015 vlc_mutex_unlock( &p_input->stream.stream_lock );
1016 msg_Err( p_input, "cannot init stream" );
1019 if( input_AddProgram( p_input, 0, 0) == NULL )
1021 vlc_mutex_unlock( &p_input->stream.stream_lock );
1022 msg_Err( p_input, "cannot add program" );
1025 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1026 p_input->stream.i_mux_rate = 0;
1027 vlc_mutex_unlock( &p_input->stream.stream_lock );
1029 if( p_sys->f_duration > 1001.0 )
1031 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000.0 );
1032 p_input->stream.i_mux_rate = stream_Size( p_sys->s ) / 50 / i_duration;
1036 msg_Dbg( p_input, "found %d es", p_sys->i_track );
1037 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1039 #define tk p_sys->track[i_track]
1040 if( tk.i_cat == UNKNOWN_ES )
1042 msg_Warn( p_input, "invalid track[%d, n=%d]", i_track, tk.i_number );
1046 tk.p_es = input_AddES( p_input,
1047 p_input->stream.p_selected_program,
1050 tk.psz_language, 0 );
1051 if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) )
1053 if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
1055 msg_Err( p_input, "missing/invalid BITMAPINFOHEADER" );
1056 tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1060 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
1062 p_bih->biSize = GetDWLE( &p_bih->biSize );
1063 p_bih->biWidth = GetDWLE( &p_bih->biWidth );
1064 p_bih->biHeight = GetDWLE( &p_bih->biHeight );
1065 p_bih->biPlanes = GetWLE( &p_bih->biPlanes );
1066 p_bih->biBitCount = GetWLE( &p_bih->biBitCount );
1067 p_bih->biCompression = GetFOURCC( &p_bih->biCompression );
1068 p_bih->biSizeImage = GetDWLE( &p_bih->biSizeImage );
1069 p_bih->biXPelsPerMeter = GetDWLE( &p_bih->biXPelsPerMeter );
1070 p_bih->biYPelsPerMeter = GetDWLE( &p_bih->biYPelsPerMeter );
1071 p_bih->biClrUsed = GetDWLE( &p_bih->biClrUsed );
1072 p_bih->biClrImportant = GetDWLE( &p_bih->biClrImportant );
1075 tk.i_codec = p_bih->biCompression;
1076 tk.p_es->p_bitmapinfoheader = p_bih;
1079 else if( !strcmp( tk.psz_codec, "V_MPEG1" ) ||
1080 !strcmp( tk.psz_codec, "V_MPEG2" ) )
1082 tk.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1084 else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) )
1086 BITMAPINFOHEADER *p_bih;
1088 tk.i_extra_data = sizeof( BITMAPINFOHEADER );
1089 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
1091 p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
1092 memset( p_bih, 0, sizeof( BITMAPINFOHEADER ) );
1093 p_bih->biSize = sizeof( BITMAPINFOHEADER );
1094 p_bih->biWidth = tk.i_width;
1095 p_bih->biHeight= tk.i_height;
1097 if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) )
1099 tk.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1103 tk.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1106 else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
1108 if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1110 msg_Err( p_input, "missing/invalid WAVEFORMATEX" );
1111 tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1115 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;
1117 p_wf->wFormatTag = GetWLE( &p_wf->wFormatTag );
1118 p_wf->nChannels = GetWLE( &p_wf->nChannels );
1119 p_wf->nSamplesPerSec = GetDWLE( &p_wf->nSamplesPerSec );
1120 p_wf->nAvgBytesPerSec = GetDWLE( &p_wf->nAvgBytesPerSec );
1121 p_wf->nBlockAlign = GetWLE( &p_wf->nBlockAlign );
1122 p_wf->wBitsPerSample = GetWLE( &p_wf->wBitsPerSample );
1123 p_wf->cbSize = GetWLE( &p_wf->cbSize );
1125 wf_tag_to_fourcc( p_wf->wFormatTag, &tk.i_codec, NULL );
1126 tk.p_es->p_waveformatex = p_wf;
1129 else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
1130 !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
1131 !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
1133 tk.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1135 else if( !strcmp( tk.psz_codec, "A_AC3" ) )
1137 tk.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1139 else if( !strcmp( tk.psz_codec, "A_DTS" ) )
1141 tk.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
1143 else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
1145 tk.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
1146 tk.i_data_init = tk.i_extra_data;
1147 tk.p_data_init = tk.p_extra_data;
1149 else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1150 !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1152 int i_profile, i_srate;
1153 static int i_sample_rates[] =
1155 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1156 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1160 tk.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1161 /* create data for faad (MP4DecSpecificDescrTag)*/
1163 if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
1167 else if( !strcmp( &tk.psz_codec[12], "LC" ) )
1171 else if( !strcmp( &tk.psz_codec[12], "SSR" ) )
1180 for( i_srate = 0; i_srate < 13; i_srate++ )
1182 if( i_sample_rates[i_srate] == tk.i_samplerate )
1187 msg_Dbg( p_input, "profile=%d srate=%d", i_profile, i_srate );
1189 tk.i_extra_data = sizeof( WAVEFORMATEX ) + 2;
1190 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
1191 p_wf = (WAVEFORMATEX*)tk.p_extra_data;
1193 p_wf->wFormatTag = WAVE_FORMAT_UNKNOWN;
1194 p_wf->nChannels = tk.i_channels;
1195 p_wf->nSamplesPerSec = tk.i_samplerate;
1196 p_wf->nAvgBytesPerSec = 0;
1197 p_wf->nBlockAlign = 0;
1198 p_wf->wBitsPerSample = 0;
1201 tk.p_extra_data[sizeof( WAVEFORMATEX )+ 0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1202 tk.p_extra_data[sizeof( WAVEFORMATEX )+ 1] = ((i_srate & 0x1) << 7) | (tk.i_channels << 3);
1204 tk.p_es->p_waveformatex = p_wf;
1206 else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
1207 !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
1208 !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
1212 tk.i_extra_data = sizeof( WAVEFORMATEX );
1213 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
1215 p_wf = (WAVEFORMATEX*)tk.p_extra_data;
1217 if( !strncmp( &tk.psz_codec[6], "INT", 3 ) )
1219 p_wf->wFormatTag = WAVE_FORMAT_PCM;
1223 p_wf->wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
1225 p_wf->nChannels = tk.i_channels;
1226 p_wf->nSamplesPerSec = tk.i_samplerate;
1227 p_wf->nAvgBytesPerSec = 0;
1228 p_wf->nBlockAlign = ( tk.i_bitspersample + 7 ) / 8 * tk.i_channels;
1229 p_wf->wBitsPerSample = tk.i_bitspersample;
1232 tk.p_es->p_waveformatex = p_wf;
1234 if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
1236 tk.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1240 tk.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1243 else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
1245 tk.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1247 else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) )
1249 tk.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1253 msg_Err( p_input, "unknow codec id=`%s'", tk.psz_codec );
1254 tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1257 tk.p_es->i_fourcc = tk.i_codec;
1261 /* select track all video, one audio, no spu TODO : improve */
1262 b_audio_selected = VLC_FALSE;
1263 i_audio_channel = 0;
1265 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1267 #define tk p_sys->track[i_track]
1271 vlc_mutex_lock( &p_input->stream.stream_lock );
1272 input_SelectES( p_input, tk.p_es );
1273 vlc_mutex_unlock( &p_input->stream.stream_lock );
1277 if( ( !b_audio_selected && config_GetInt( p_input, "audio-channel" ) < 0 ) ||
1278 i_audio_channel == config_GetInt( p_input, "audio-channel" ) )
1280 vlc_mutex_lock( &p_input->stream.stream_lock );
1281 input_SelectES( p_input, tk.p_es );
1282 vlc_mutex_unlock( &p_input->stream.stream_lock );
1284 b_audio_selected = tk.p_es->p_decoder_fifo ? VLC_TRUE : VLC_FALSE;
1289 if( i_spu_channel == config_GetInt( p_input, "spu-channel" ) )
1291 vlc_mutex_lock( &p_input->stream.stream_lock );
1292 input_SelectES( p_input, tk.p_es );
1293 vlc_mutex_unlock( &p_input->stream.stream_lock );
1301 if( !b_audio_selected )
1303 msg_Warn( p_input, "cannot find/select audio track" );
1306 /* add informations */
1307 InformationsCreate( p_input );
1314 stream_Release( p_sys->s );
1316 return VLC_EGENERIC;
1319 /*****************************************************************************
1320 * Close: frees unused data
1321 *****************************************************************************/
1322 static void Close( vlc_object_t *p_this )
1324 input_thread_t *p_input = (input_thread_t *)p_this;
1325 demux_sys_t *p_sys = p_input->p_demux_data;
1329 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1331 #define tk p_sys->track[i_track]
1334 free( tk.psz_codec );
1336 if( tk.psz_language )
1338 free( tk.psz_language );
1342 free( p_sys->track );
1344 if( p_sys->psz_writing_application )
1346 free( p_sys->psz_writing_application );
1348 if( p_sys->psz_muxing_application )
1350 free( p_sys->psz_muxing_application );
1353 delete p_sys->segment;
1358 stream_Release( p_sys->s );
1363 static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1365 demux_sys_t *p_sys = p_input->p_demux_data;
1376 if( p_input->b_die )
1378 return VLC_EGENERIC;
1381 el = p_sys->ep->Get();
1382 i_level = p_sys->ep->GetLevel();
1384 if( el == NULL && *pp_block != NULL )
1386 /* update the index */
1387 #define idx p_sys->index[p_sys->i_index - 1]
1388 if( p_sys->i_index > 0 && idx.i_time == -1 )
1390 idx.i_time = (*pp_block)->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1391 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1399 if( p_sys->ep->GetLevel() > 1 )
1404 msg_Warn( p_input, "EOF" );
1405 return VLC_EGENERIC;
1411 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1413 p_sys->cluster = (KaxCluster*)el;
1415 /* add it to the index */
1416 if( p_sys->i_index == 0 ||
1417 ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
1419 IndexAppendCluster( p_input, p_sys->cluster );
1424 else if( EbmlId( *el ) == KaxCues::ClassInfos.GlobalId )
1426 msg_Warn( p_input, "find KaxCues FIXME" );
1427 return VLC_EGENERIC;
1431 msg_Dbg( p_input, "unknown (%s)", typeid( el ).name() );
1434 else if( i_level == 2 )
1436 if( EbmlId( *el ) == KaxClusterTimecode::ClassInfos.GlobalId )
1438 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1440 ctc.ReadData( p_sys->es->I_O() );
1441 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
1443 else if( EbmlId( *el ) == KaxBlockGroup::ClassInfos.GlobalId )
1448 else if( i_level == 3 )
1450 if( EbmlId( *el ) == KaxBlock::ClassInfos.GlobalId )
1452 *pp_block = (KaxBlock*)el;
1454 (*pp_block)->ReadData( p_sys->es->I_O() );
1455 (*pp_block)->SetParent( *p_sys->cluster );
1459 else if( EbmlId( *el ) == KaxBlockDuration::ClassInfos.GlobalId )
1461 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1463 dur.ReadData( p_sys->es->I_O() );
1464 *pi_duration = uint64( dur );
1466 else if( EbmlId( *el ) == KaxReferenceBlock::ClassInfos.GlobalId )
1468 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1470 ref.ReadData( p_sys->es->I_O() );
1471 if( *pi_ref1 == -1 )
1473 *pi_ref1 = int64( ref );
1477 *pi_ref2 = int64( ref );
1483 msg_Err( p_input, "invalid level = %d", i_level );
1484 return VLC_EGENERIC;
1489 static pes_packet_t *MemToPES( input_thread_t *p_input, uint8_t *p_mem, int i_mem )
1491 pes_packet_t *p_pes;
1492 data_packet_t *p_data;
1494 if( ( p_pes = input_NewPES( p_input->p_method_data ) ) == NULL )
1499 p_data = input_NewPacket( p_input->p_method_data, i_mem);
1501 memcpy( p_data->p_payload_start, p_mem, i_mem );
1502 p_data->p_payload_end = p_data->p_payload_start + i_mem;
1504 p_pes->p_first = p_pes->p_last = p_data;
1505 p_pes->i_nb_data = 1;
1506 p_pes->i_pes_size = i_mem;
1511 static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts, mtime_t i_duration )
1513 demux_sys_t *p_sys = p_input->p_demux_data;
1518 #define tk p_sys->track[i_track]
1519 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1521 if( tk.i_number == block->TrackNum() )
1527 if( i_track >= p_sys->i_track )
1529 msg_Err( p_input, "invalid track number=%d", block->TrackNum() );
1533 if( tk.p_es->p_decoder_fifo == NULL )
1535 tk.b_inited = VLC_FALSE;
1539 if( tk.i_cat == AUDIO_ES && p_input->stream.control.b_mute )
1544 /* First send init data */
1545 if( !tk.b_inited && tk.i_data_init > 0 )
1547 pes_packet_t *p_init;
1549 msg_Dbg( p_input, "sending header (%d bytes)", tk.i_data_init );
1551 if( tk.i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
1557 /* XXX hack split the 3 headers */
1558 if( tk.p_data_init[0] != 0x02 )
1560 msg_Err( p_input, "invalid vorbis header" );
1563 for( i = 0; i < 2; i++ )
1566 while( i_offset < tk.i_data_init )
1568 i_size[i] += tk.p_data_init[i_offset];
1569 if( tk.p_data_init[i_offset++] != 0xff )
1575 i_size[0] = __MIN( i_size[0], tk.i_data_init - i_offset );
1576 i_size[1] = __MIN( i_size[1], tk.i_data_init - i_offset - i_size[0] );
1577 i_size[2] = tk.i_data_init - i_offset - i_size[0] - i_size[1];
1579 p_init = MemToPES( p_input, &tk.p_data_init[i_offset], i_size[0] );
1582 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1584 p_init = MemToPES( p_input, &tk.p_data_init[i_offset+i_size[0]], i_size[1] );
1587 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1589 p_init = MemToPES( p_input, &tk.p_data_init[i_offset+i_size[0]+i_size[1]], i_size[2] );
1592 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1597 p_init = MemToPES( p_input, tk.p_data_init, tk.i_data_init );
1600 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1604 tk.b_inited = VLC_TRUE;
1607 for( i = 0; i < block->NumberFrames(); i++ )
1609 pes_packet_t *p_pes;
1610 DataBuffer &data = block->GetBuffer(i);
1612 p_pes = MemToPES( p_input, data.Buffer(), data.Size() );
1618 p_pes->i_pts = i_pts;
1619 p_pes->i_dts = i_pts;
1621 if( tk.i_cat == SPU_ES )
1623 if( i_duration > 0 )
1625 /* FIXME not sure about that */
1626 p_pes->i_dts += i_duration * 1000;// * (mtime_t) 1000 / p_sys->i_timescale;
1633 if( p_pes->p_first && p_pes->i_pes_size > 0 )
1635 p_pes->p_first->p_payload_end[-1] = '\0';
1637 if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) )
1639 /* remove all fields before text for now */
1640 char *start = (char*)p_pes->p_first->p_payload_start;
1643 while( *start && i_comma < 8 )
1645 if( *start++ == ',' )
1650 memmove( p_pes->p_first->p_payload_start, start,
1651 strlen( start) + 1 );
1655 input_DecodePES( tk.p_es->p_decoder_fifo, p_pes );
1657 /* use time stamp only for first block */
1664 static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent)
1666 demux_sys_t *p_sys = p_input->p_demux_data;
1669 int64_t i_block_duration;
1670 int64_t i_block_ref1;
1671 int64_t i_block_ref2;
1674 int i_track_skipping;
1677 msg_Dbg( p_input, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1678 if( i_date < 0 && i_percent < 0 )
1684 p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
1685 p_sys->cluster = NULL;
1687 /* seek without index or without date */
1688 if( config_GetInt( p_input, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
1690 int64_t i_pos = i_percent * stream_Size( p_sys->s ) / 100;
1692 msg_Dbg( p_input, "imprecise way of seeking" );
1693 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1695 if( p_sys->index[i_index].i_position >= i_pos)
1700 if( i_index == p_sys->i_index )
1705 p_sys->in->setFilePointer( p_sys->index[i_index].i_position, seek_beginning );
1707 if( p_sys->index[i_index].i_position < i_pos )
1711 msg_Warn( p_input, "searching for cluster, could take some time" );
1713 /* search a cluster */
1714 while( ( el = p_sys->ep->Get() ) != NULL )
1716 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1718 KaxCluster *cluster = (KaxCluster*)el;
1720 /* add it to the index */
1721 IndexAppendCluster( p_input, cluster );
1723 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1725 p_sys->cluster = cluster;
1735 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1737 if( p_sys->index[i_index].i_time >= i_date )
1748 msg_Dbg( p_input, "seek got "I64Fd" (%d%%)",
1749 p_sys->index[i_index].i_time,
1750 (int)( 100 * p_sys->index[i_index].i_position /
1751 stream_Size( p_sys->s ) ) );
1753 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1757 /* now parse until key frame */
1758 #define tk p_sys->track[i_track]
1759 i_track_skipping = 0;
1760 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1762 if( tk.i_cat == VIDEO_ES )
1764 tk.b_search_keyframe = VLC_TRUE;
1769 while( i_track_skipping > 0 )
1771 if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1773 msg_Warn( p_input, "cannot get block EOF?" );
1778 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1780 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1782 if( tk.i_number == block->TrackNum() )
1788 if( i_track < p_sys->i_track )
1790 if( tk.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1792 tk.b_search_keyframe = VLC_FALSE;
1795 if( tk.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1797 BlockDecode( p_input, block, 0, 0 );
1806 /*****************************************************************************
1807 * Demux: reads and demuxes data packets
1808 *****************************************************************************
1809 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1810 *****************************************************************************/
1811 static int Demux( input_thread_t * p_input )
1813 demux_sys_t *p_sys = p_input->p_demux_data;
1814 mtime_t i_start_pts;
1815 int i_block_count = 0;
1818 int64_t i_block_duration;
1819 int64_t i_block_ref1;
1820 int64_t i_block_ref2;
1822 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1824 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
1825 mtime_t i_date = -1;
1828 if( i_duration > 0 )
1830 i_date = (mtime_t)1000000 *
1831 (mtime_t)i_duration*
1832 (mtime_t)p_sys->in->getFilePointer() /
1833 (mtime_t)stream_Size( p_sys->s );
1835 if( stream_Size( p_sys->s ) > 0 )
1837 i_percent = 100 * p_sys->in->getFilePointer() /
1838 stream_Size( p_sys->s );
1841 Seek( p_input, i_date, i_percent);
1850 if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1852 msg_Warn( p_input, "cannot get block EOF?" );
1857 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1859 if( p_sys->i_pts > 0 )
1861 input_ClockManageRef( p_input,
1862 p_input->stream.p_selected_program,
1863 p_sys->i_pts * 9 / 100 );
1866 i_pts = input_ClockGetTS( p_input,
1867 p_input->stream.p_selected_program,
1868 p_sys->i_pts * 9 / 100 );
1872 BlockDecode( p_input, block, i_pts, i_block_duration );
1877 if( i_start_pts == -1 )
1879 i_start_pts = p_sys->i_pts;
1881 else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1890 /*****************************************************************************
1892 *****************************************************************************/
1893 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1899 uint32_t vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1901 if( i_size <= 0 || mb_eof )
1906 return stream_Read( s, p_buffer, i_size );
1908 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1914 case seek_beginning:
1918 i_pos = stream_Size( s ) - i_offset;
1921 i_pos= stream_Tell( s ) + i_offset;
1925 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1932 if( stream_Seek( s, i_pos ) )
1938 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1942 uint64_t vlc_stream_io_callback::getFilePointer( void )
1944 return stream_Tell( s );
1946 void vlc_stream_io_callback::close( void )
1952 /*****************************************************************************
1953 * Ebml Stream parser
1954 *****************************************************************************/
1955 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1963 for( i = 1; i < 6; i++ )
1969 mb_keep = VLC_FALSE;
1972 EbmlParser::~EbmlParser( void )
1976 for( i = 1; i < mi_level; i++ )
1982 mb_keep = VLC_FALSE;
1986 void EbmlParser::Up( void )
1988 if( mi_user_level == mi_level )
1990 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1996 void EbmlParser::Down( void )
2002 void EbmlParser::Keep( void )
2007 int EbmlParser::GetLevel( void )
2009 return mi_user_level;
2012 EbmlElement *EbmlParser::Get( void )
2016 if( mi_user_level != mi_level )
2022 EbmlElement *ret = m_got;
2028 if( m_el[mi_level] )
2030 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
2033 delete m_el[mi_level];
2035 mb_keep = VLC_FALSE;
2038 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
2049 delete m_el[mi_level - 1];
2050 m_got = m_el[mi_level -1] = m_el[mi_level];
2051 m_el[mi_level] = NULL;
2058 else if( m_el[mi_level] == NULL )
2060 fprintf( stderr," m_el[mi_level] == NULL\n" );
2063 return m_el[mi_level];
2067 /*****************************************************************************
2069 * * LoadCues : load the cues element and update index
2071 * * LoadTags : load ... the tags element
2073 * * InformationsCreate : create all informations, load tags if present
2075 *****************************************************************************/
2076 static void LoadCues( input_thread_t *p_input )
2078 demux_sys_t *p_sys = p_input->p_demux_data;
2079 int64_t i_sav_position = p_sys->in->getFilePointer();
2081 EbmlElement *el, *cues;
2083 msg_Dbg( p_input, "loading cues" );
2084 p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
2085 cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
2089 msg_Err( p_input, "cannot load cues (broken seekhead or file)" );
2093 ep = new EbmlParser( p_sys->es, cues );
2094 while( ( el = ep->Get() ) != NULL )
2096 if( EbmlId( *el ) == KaxCuePoint::ClassInfos.GlobalId )
2098 #define idx p_sys->index[p_sys->i_index]
2101 idx.i_block_number= -1;
2102 idx.i_position = -1;
2104 idx.b_key = VLC_TRUE;
2107 while( ( el = ep->Get() ) != NULL )
2109 if( EbmlId( *el ) == KaxCueTime::ClassInfos.GlobalId )
2111 KaxCueTime &ctime = *(KaxCueTime*)el;
2113 ctime.ReadData( p_sys->es->I_O() );
2115 idx.i_time = uint64( ctime ) * (mtime_t)1000000000 / p_sys->i_timescale;
2117 else if( EbmlId( *el ) == KaxCueTrackPositions::ClassInfos.GlobalId )
2120 while( ( el = ep->Get() ) != NULL )
2122 if( EbmlId( *el ) == KaxCueTrack::ClassInfos.GlobalId )
2124 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
2126 ctrack.ReadData( p_sys->es->I_O() );
2127 idx.i_track = uint16( ctrack );
2129 else if( EbmlId( *el ) == KaxCueClusterPosition::ClassInfos.GlobalId )
2131 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
2133 ccpos.ReadData( p_sys->es->I_O() );
2134 idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
2136 else if( EbmlId( *el ) == KaxCueBlockNumber::ClassInfos.GlobalId )
2138 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2140 cbnum.ReadData( p_sys->es->I_O() );
2141 idx.i_block_number = uint32( cbnum );
2145 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2152 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2157 msg_Dbg( p_input, " * added time="I64Fd" pos="I64Fd
2158 " track=%d bnum=%d", idx.i_time, idx.i_position,
2159 idx.i_track, idx.i_block_number );
2162 if( p_sys->i_index >= p_sys->i_index_max )
2164 p_sys->i_index_max += 1024;
2165 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2171 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2177 p_sys->b_cues = VLC_TRUE;
2179 msg_Dbg( p_input, "loading cues done." );
2180 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2183 static void LoadTags( input_thread_t *p_input )
2185 demux_sys_t *p_sys = p_input->p_demux_data;
2186 int64_t i_sav_position = p_sys->in->getFilePointer();
2188 EbmlElement *el, *tags;
2190 msg_Dbg( p_input, "loading tags" );
2191 p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
2192 tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2196 msg_Err( p_input, "cannot load tags (broken seekhead or file)" );
2200 msg_Dbg( p_input, "Tags" );
2201 ep = new EbmlParser( p_sys->es, tags );
2202 while( ( el = ep->Get() ) != NULL )
2204 if( EbmlId( *el ) == KaxTag::ClassInfos.GlobalId )
2206 msg_Dbg( p_input, "+ Tag" );
2208 while( ( el = ep->Get() ) != NULL )
2210 if( EbmlId( *el ) == KaxTagTargets::ClassInfos.GlobalId )
2212 msg_Dbg( p_input, "| + Targets" );
2214 while( ( el = ep->Get() ) != NULL )
2216 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2220 else if( EbmlId( *el ) == KaxTagGeneral::ClassInfos.GlobalId )
2222 msg_Dbg( p_input, "| + General" );
2224 while( ( el = ep->Get() ) != NULL )
2226 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2230 else if( EbmlId( *el ) == KaxTagGenres::ClassInfos.GlobalId )
2232 msg_Dbg( p_input, "| + Genres" );
2234 while( ( el = ep->Get() ) != NULL )
2236 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2240 else if( EbmlId( *el ) == KaxTagAudioSpecific::ClassInfos.GlobalId )
2242 msg_Dbg( p_input, "| + Audio Specific" );
2244 while( ( el = ep->Get() ) != NULL )
2246 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2250 else if( EbmlId( *el ) == KaxTagImageSpecific::ClassInfos.GlobalId )
2252 msg_Dbg( p_input, "| + Images Specific" );
2254 while( ( el = ep->Get() ) != NULL )
2256 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2260 else if( EbmlId( *el ) == KaxTagMultiComment::ClassInfos.GlobalId )
2262 msg_Dbg( p_input, "| + Multi Comment" );
2264 else if( EbmlId( *el ) == KaxTagMultiCommercial::ClassInfos.GlobalId )
2266 msg_Dbg( p_input, "| + Multi Commercial" );
2268 else if( EbmlId( *el ) == KaxTagMultiDate::ClassInfos.GlobalId )
2270 msg_Dbg( p_input, "| + Multi Date" );
2272 else if( EbmlId( *el ) == KaxTagMultiEntity::ClassInfos.GlobalId )
2274 msg_Dbg( p_input, "| + Multi Entity" );
2276 else if( EbmlId( *el ) == KaxTagMultiIdentifier::ClassInfos.GlobalId )
2278 msg_Dbg( p_input, "| + Multi Identifier" );
2280 else if( EbmlId( *el ) == KaxTagMultiLegal::ClassInfos.GlobalId )
2282 msg_Dbg( p_input, "| + Multi Legal" );
2284 else if( EbmlId( *el ) == KaxTagMultiTitle::ClassInfos.GlobalId )
2286 msg_Dbg( p_input, "| + Multi Title" );
2290 msg_Dbg( p_input, "| + Unknown (%s)", typeid( *el ).name() );
2297 msg_Dbg( p_input, "+ Unknown (%s)", typeid( *el ).name() );
2303 msg_Dbg( p_input, "loading tags done." );
2304 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2307 static void InformationsCreate( input_thread_t *p_input )
2309 demux_sys_t *p_sys = p_input->p_demux_data;
2310 input_info_category_t *p_cat;
2313 p_cat = input_InfoCategory( p_input, "Matroska" );
2314 if( p_sys->f_duration > 1000.1 )
2316 int64_t i_sec = (int64_t)p_sys->f_duration / 1000;
2320 m = ( i_sec / 60 ) % 60;
2323 input_AddInfo( p_cat, _("Duration"), "%d:%2.2d:%2.2d" , h, m, s );
2326 if( p_sys->psz_title )
2328 input_AddInfo( p_cat, _("Title"), "%s" ,p_sys->psz_title );
2330 if( p_sys->psz_date_utc )
2332 input_AddInfo( p_cat, _("Date UTC"), "%s" ,p_sys->psz_date_utc );
2334 if( p_sys->psz_segment_filename )
2336 input_AddInfo( p_cat, _("Segment Filename"), "%s" ,p_sys->psz_segment_filename );
2338 if( p_sys->psz_muxing_application )
2340 input_AddInfo( p_cat, _("Muxing Application"), "%s" ,p_sys->psz_muxing_application );
2342 if( p_sys->psz_writing_application )
2344 input_AddInfo( p_cat, _("Writing Application"), "%s" ,p_sys->psz_writing_application );
2346 input_AddInfo( p_cat, _("Number of streams"), "%d" , p_sys->i_track );
2348 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
2350 char psz_cat[strlen( "Stream " ) + 10];
2351 #define tk p_sys->track[i_track]
2353 sprintf( psz_cat, "Stream %d", i_track );
2354 p_cat = input_InfoCategory( p_input, psz_cat);
2357 input_AddInfo( p_cat, _("Name"), "%s", tk.psz_name );
2359 if( tk.psz_codec_name )
2361 input_AddInfo( p_cat, _("Codec Name"), "%s", tk.psz_codec_name );
2363 if( tk.psz_codec_settings )
2365 input_AddInfo( p_cat, _("Codec Setting"), "%s", tk.psz_codec_settings );
2367 if( tk.psz_codec_info_url )
2369 input_AddInfo( p_cat, _("Codec Info"), "%s", tk.psz_codec_info_url );
2371 if( tk.psz_codec_download_url )
2373 input_AddInfo( p_cat, _("Codec Download"), "%s", tk.psz_codec_download_url );
2379 input_AddInfo( p_cat, _("Type"), _("Audio") );
2380 input_AddInfo( p_cat, _("Codec"), "%.4s (%s)", (char*)&tk.i_codec, tk.psz_codec );
2381 if( tk.i_channels > 0 )
2383 input_AddInfo( p_cat, _("Channels"), "%d", tk.i_channels );
2385 if( tk.i_samplerate > 0 )
2387 input_AddInfo( p_cat, _("Sample Rate"), "%d", tk.i_samplerate );
2389 if( tk.i_bitspersample )
2391 input_AddInfo( p_cat, _("Bits Per Sample"), "%d", tk.i_bitspersample );
2395 input_AddInfo( p_cat, _("Type"), _("Video") );
2396 input_AddInfo( p_cat, _("Codec"), "%.4s (%s)", (char*)&tk.i_codec, tk.psz_codec );
2397 if( tk.i_width > 0 && tk.i_height )
2399 input_AddInfo( p_cat, _("Resolution"), "%dx%d", tk.i_width, tk.i_height );
2401 if( tk.i_display_width > 0 && tk.i_display_height )
2403 input_AddInfo( p_cat, _("Display Resolution"), "%dx%d", tk.i_display_width, tk.i_display_height );
2405 if( tk.f_fps > 0.1 )
2407 input_AddInfo( p_cat, _("Frame Per Second"), "%.3f", tk.f_fps );
2411 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
2412 input_AddInfo( p_cat, _("Codec"), "%s", tk.psz_codec );
2419 if( p_sys->i_tags_position >= 0 )
2421 vlc_bool_t b_seekable;
2423 stream_Control( p_sys->s, STREAM_CAN_FASTSEEK, &b_seekable );
2426 LoadTags( p_input );
2432 /*****************************************************************************
2434 *****************************************************************************/
2436 static void IndexAppendCluster( input_thread_t *p_input, KaxCluster *cluster )
2438 demux_sys_t *p_sys = p_input->p_demux_data;
2440 #define idx p_sys->index[p_sys->i_index]
2442 idx.i_block_number= -1;
2443 idx.i_position = cluster->GetElementPosition();
2445 idx.b_key = VLC_TRUE;
2448 if( p_sys->i_index >= p_sys->i_index_max )
2450 p_sys->i_index_max += 1024;
2451 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2456 static char * UTF8ToStr( const UTFstring &u )
2465 p = dst = (char*)malloc( i_src + 1);
2484 static char *LanguageGetName ( const char *psz_code )
2486 const iso639_lang_t *pl;
2488 if( strlen( psz_code ) == 2 )
2490 pl = GetLang_1( psz_code );
2492 else if( strlen( psz_code ) == 3 )
2494 pl = GetLang_2B( psz_code );
2495 if( !strcmp( pl->psz_iso639_1, "??" ) )
2497 pl = GetLang_2T( psz_code );
2502 return strdup( psz_code );
2505 if( !strcmp( pl->psz_iso639_1, "??" ) )
2507 return strdup( psz_code );
2511 if( *pl->psz_native_name )
2513 return strdup( pl->psz_native_name );
2515 return strdup( pl->psz_eng_name );