1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: mkv.cpp,v 1.37 2003/11/05 00:39:16 gbazin Exp $
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
32 # include <time.h> /* time() */
35 #include <vlc/input.h>
37 #include <codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
48 /* libebml and matroska */
49 #include "ebml/EbmlHead.h"
50 #include "ebml/EbmlSubHead.h"
51 #include "ebml/EbmlStream.h"
52 #include "ebml/EbmlContexts.h"
53 #include "ebml/EbmlVersion.h"
54 #include "ebml/EbmlVoid.h"
56 #include "matroska/FileKax.h"
57 #ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
58 #include "matroska/KaxAttachments.h"
60 #include "matroska/KaxAttachements.h"
62 #include "matroska/KaxBlock.h"
63 #include "matroska/KaxBlockData.h"
64 #include "matroska/KaxChapters.h"
65 #include "matroska/KaxCluster.h"
66 #include "matroska/KaxClusterData.h"
67 #include "matroska/KaxContexts.h"
68 #include "matroska/KaxCues.h"
69 #include "matroska/KaxCuesData.h"
70 #include "matroska/KaxInfo.h"
71 #include "matroska/KaxInfoData.h"
72 #include "matroska/KaxSeekHead.h"
73 #include "matroska/KaxSegment.h"
74 #include "matroska/KaxTag.h"
75 #include "matroska/KaxTags.h"
76 #include "matroska/KaxTagMulti.h"
77 #include "matroska/KaxTracks.h"
78 #include "matroska/KaxTrackAudio.h"
79 #include "matroska/KaxTrackVideo.h"
80 #include "matroska/KaxTrackEntryData.h"
82 #include "ebml/StdIOCallback.h"
84 using namespace LIBMATROSKA_NAMESPACE;
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Open ( vlc_object_t * );
91 static void Close( vlc_object_t * );
94 add_category_hint( N_("mkv-demuxer"), NULL, VLC_TRUE );
95 add_bool( "mkv-seek-percent", 1, NULL,
96 N_("Seek based on percent not time"),
97 N_("Seek based on percent not time"), VLC_TRUE );
99 set_description( _("mka/mkv stream demuxer" ) );
100 set_capability( "demux", 50 );
101 set_callbacks( Open, Close );
102 add_shortcut( "mka" );
103 add_shortcut( "mkv" );
106 /*****************************************************************************
108 *****************************************************************************/
109 static int Demux ( input_thread_t * );
110 static void Seek ( input_thread_t *, mtime_t i_date, int i_percent );
113 /*****************************************************************************
115 *****************************************************************************/
116 class vlc_stream_io_callback: public IOCallback
123 vlc_stream_io_callback( stream_t * );
125 virtual uint32_t read ( void *p_buffer, size_t i_size);
126 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
127 virtual size_t write ( const void *p_buffer, size_t i_size);
128 virtual uint64_t getFilePointer ( void );
129 virtual void close ( void );
132 /*****************************************************************************
134 *****************************************************************************/
138 EbmlParser( EbmlStream *es, EbmlElement *el_start );
143 EbmlElement *Get( void );
146 int GetLevel( void );
151 EbmlElement *m_el[6];
160 /*****************************************************************************
161 * Some functions to manipulate memory
162 *****************************************************************************/
163 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
164 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
166 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
169 /*****************************************************************************
170 * definitions of structures and functions used by this plugins
171 *****************************************************************************/
175 vlc_bool_t b_default;
176 vlc_bool_t b_enabled;
180 uint8_t *p_extra_data;
185 vlc_fourcc_t i_codec;
187 uint64_t i_default_duration;
188 float f_timecodescale;
193 int i_display_height;
202 es_descriptor_t *p_es;
207 /* data to be send first */
209 uint8_t *p_data_init;
211 /* hack : it's for seek */
212 vlc_bool_t b_search_keyframe;
216 char *psz_codec_name;
217 char *psz_codec_settings;
218 char *psz_codec_info_url;
219 char *psz_codec_download_url;
236 vlc_stream_io_callback *in;
241 uint64_t i_timescale;
243 /* duration of the segment */
251 int64_t i_cues_position;
252 int64_t i_chapters_position;
253 int64_t i_tags_position;
267 char *psz_muxing_application;
268 char *psz_writing_application;
269 char *psz_segment_filename;
274 #define MKVD_TIMECODESCALE 1000000
276 static void IndexAppendCluster ( input_thread_t *p_input, KaxCluster *cluster );
277 static char *UTF8ToStr ( const UTFstring &u );
278 static void LoadCues ( input_thread_t *);
279 static void InformationsCreate ( input_thread_t *p_input );
281 static char *LanguageGetName ( const char *psz_code );
283 /*****************************************************************************
284 * Open: initializes matroska demux structures
285 *****************************************************************************/
286 static int Open( vlc_object_t * p_this )
288 input_thread_t *p_input = (input_thread_t *)p_this;
293 vlc_bool_t b_audio_selected;
294 int i_spu_channel, i_audio_channel;
296 EbmlElement *el = NULL, *el1 = NULL, *el2 = NULL, *el3 = NULL, *el4 = NULL;
298 /* Set the demux function */
299 p_input->pf_demux = Demux;
300 p_input->pf_demux_control = demux_vaControlDefault;
302 /* peek the begining */
303 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
305 msg_Warn( p_input, "cannot peek" );
309 /* is a valid file */
310 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
311 p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
313 msg_Warn( p_input, "matroska module discarded "
314 "(invalid header 0x%.2x%.2x%.2x%.2x)",
315 p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
319 p_input->p_demux_data = p_sys = (demux_sys_t*)malloc(sizeof( demux_sys_t ));
320 memset( p_sys, 0, sizeof( demux_sys_t ) );
322 p_sys->in = new vlc_stream_io_callback( p_input->s );
323 p_sys->es = new EbmlStream( *p_sys->in );
324 p_sys->f_duration = -1;
325 p_sys->i_timescale = MKVD_TIMECODESCALE;
327 p_sys->track = (mkv_track_t*)malloc( sizeof( mkv_track_t ) );
329 p_sys->i_cues_position = -1;
330 p_sys->i_chapters_position = -1;
331 p_sys->i_tags_position = -1;
333 p_sys->b_cues = VLC_FALSE;
335 p_sys->i_index_max = 1024;
336 p_sys->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
337 p_sys->i_index_max );
339 p_sys->psz_muxing_application = NULL;
340 p_sys->psz_writing_application = NULL;
341 p_sys->psz_segment_filename = NULL;
342 p_sys->psz_title = NULL;
343 p_sys->psz_date_utc = NULL;;
345 if( p_sys->es == NULL )
347 msg_Err( p_input, "failed to create EbmlStream" );
352 /* Find the EbmlHead element */
353 el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
356 msg_Err( p_input, "cannot find EbmlHead" );
359 msg_Dbg( p_input, "EbmlHead" );
361 el->SkipData( *p_sys->es, el->Generic().Context );
365 el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
368 msg_Err( p_input, "cannot find KaxSegment" );
371 msg_Dbg( p_input, "+ Segment" );
372 p_sys->segment = (KaxSegment*)el;
373 p_sys->cluster = NULL;
375 p_sys->ep = new EbmlParser( p_sys->es, el );
377 while( ( el1 = p_sys->ep->Get() ) != NULL )
379 if( EbmlId( *el1 ) == KaxInfo::ClassInfos.GlobalId )
381 msg_Dbg( p_input, "| + Informations" );
384 while( ( el2 = p_sys->ep->Get() ) != NULL )
386 if( EbmlId( *el2 ) == KaxTimecodeScale::ClassInfos.GlobalId )
388 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)el2;
390 tcs.ReadData( p_sys->es->I_O() );
391 p_sys->i_timescale = uint64(tcs);
393 msg_Dbg( p_input, "| | + TimecodeScale="I64Fd,
394 p_sys->i_timescale );
396 else if( EbmlId( *el2 ) == KaxDuration::ClassInfos.GlobalId )
398 KaxDuration &dur = *(KaxDuration*)el2;
400 dur.ReadData( p_sys->es->I_O() );
401 p_sys->f_duration = float(dur);
403 msg_Dbg( p_input, "| | + Duration=%f",
406 else if( EbmlId( *el2 ) == KaxMuxingApp::ClassInfos.GlobalId )
408 KaxMuxingApp &mapp = *(KaxMuxingApp*)el2;
410 mapp.ReadData( p_sys->es->I_O() );
412 p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
414 msg_Dbg( p_input, "| | + Muxing Application=%s",
415 p_sys->psz_muxing_application );
417 else if( EbmlId( *el2 ) == KaxWritingApp::ClassInfos.GlobalId )
419 KaxWritingApp &wapp = *(KaxWritingApp*)el2;
421 wapp.ReadData( p_sys->es->I_O() );
423 p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
425 msg_Dbg( p_input, "| | + Wrinting Application=%s",
426 p_sys->psz_writing_application );
428 else if( EbmlId( *el2 ) == KaxSegmentFilename::ClassInfos.GlobalId )
430 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)el2;
432 sfn.ReadData( p_sys->es->I_O() );
434 p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
436 msg_Dbg( p_input, "| | + Segment Filename=%s",
437 p_sys->psz_segment_filename );
439 else if( EbmlId( *el2 ) == KaxTitle::ClassInfos.GlobalId )
441 KaxTitle &title = *(KaxTitle*)el2;
443 title.ReadData( p_sys->es->I_O() );
445 p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
447 msg_Dbg( p_input, "| | + Title=%s", p_sys->psz_title );
450 else if( EbmlId( *el2 ) == KaxDateUTC::ClassInfos.GlobalId )
452 KaxDateUTC &date = *(KaxDateUTC*)el2;
457 date.ReadData( p_sys->es->I_O() );
459 i_date = date.GetEpochDate();
460 memset( buffer, 0, 256 );
461 if( gmtime_r( &i_date, &tmres ) &&
462 asctime_r( &tmres, buffer ) )
464 buffer[strlen( buffer)-1]= '\0';
465 p_sys->psz_date_utc = strdup( buffer );
466 msg_Dbg( p_input, "| | + Date=%s", p_sys->psz_date_utc );
472 msg_Dbg( p_input, "| | + Unknown (%s)", typeid(*el2).name() );
477 else if( EbmlId( *el1 ) == KaxTracks::ClassInfos.GlobalId )
479 msg_Dbg( p_input, "| + Tracks" );
482 while( ( el2 = p_sys->ep->Get() ) != NULL )
484 if( EbmlId( *el2 ) == KaxTrackEntry::ClassInfos.GlobalId )
486 msg_Dbg( p_input, "| | + Track Entry" );
489 p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
490 #define tk p_sys->track[p_sys->i_track - 1]
491 memset( &tk, 0, sizeof( mkv_track_t ) );
492 tk.i_cat = UNKNOWN_ES;
493 tk.b_default = VLC_TRUE;
494 tk.b_enabled = VLC_TRUE;
495 tk.i_number = p_sys->i_track - 1;
497 tk.p_extra_data = NULL;
500 tk.psz_language = strdup("English");
501 tk.i_default_duration = 0;
502 tk.f_timecodescale = 1.0;
504 tk.b_inited = VLC_FALSE;
506 tk.p_data_init = NULL;
509 tk.psz_codec_name = NULL;
510 tk.psz_codec_settings = NULL;
511 tk.psz_codec_info_url = NULL;
512 tk.psz_codec_download_url = NULL;
516 while( ( el3 = p_sys->ep->Get() ) != NULL )
518 if( EbmlId( *el3 ) == KaxTrackNumber::ClassInfos.GlobalId )
520 KaxTrackNumber &tnum = *(KaxTrackNumber*)el3;
521 tnum.ReadData( p_sys->es->I_O() );
523 tk.i_number = uint32( tnum );
524 msg_Dbg( p_input, "| | | + Track Number=%u",
527 else if( EbmlId( *el3 ) == KaxTrackUID::ClassInfos.GlobalId )
529 KaxTrackUID &tuid = *(KaxTrackUID*)el3;
530 tuid.ReadData( p_sys->es->I_O() );
532 msg_Dbg( p_input, "| | | + Track UID=%u",
535 else if( EbmlId( *el3 ) == KaxTrackType::ClassInfos.GlobalId )
538 KaxTrackType &ttype = *(KaxTrackType*)el3;
539 ttype.ReadData( p_sys->es->I_O() );
540 switch( uint8(ttype) )
551 psz_type = "subtitle";
555 psz_type = "unknown";
556 tk.i_cat = UNKNOWN_ES;
560 msg_Dbg( p_input, "| | | + Track Type=%s",
563 else if( EbmlId( *el3 ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
565 KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)el3;
566 fenb.ReadData( p_sys->es->I_O() );
568 tk.b_enabled = uint32( fenb );
569 msg_Dbg( p_input, "| | | + Track Enabled=%u",
572 else if( EbmlId( *el3 ) == KaxTrackFlagDefault::ClassInfos.GlobalId )
574 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)el3;
575 fdef.ReadData( p_sys->es->I_O() );
577 tk.b_default = uint32( fdef );
578 msg_Dbg( p_input, "| | | + Track Default=%u",
581 else if( EbmlId( *el3 ) == KaxTrackFlagLacing::ClassInfos.GlobalId )
583 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)el3;
584 lac.ReadData( p_sys->es->I_O() );
586 msg_Dbg( p_input, "| | | + Track Lacing=%d",
589 else if( EbmlId( *el3 ) == KaxTrackMinCache::ClassInfos.GlobalId )
591 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)el3;
592 cmin.ReadData( p_sys->es->I_O() );
594 msg_Dbg( p_input, "| | | + Track MinCache=%d",
597 else if( EbmlId( *el3 ) == KaxTrackMaxCache::ClassInfos.GlobalId )
599 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)el3;
600 cmax.ReadData( p_sys->es->I_O() );
602 msg_Dbg( p_input, "| | | + Track MaxCache=%d",
605 else if( EbmlId( *el3 ) == KaxTrackDefaultDuration::ClassInfos.GlobalId )
607 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)el3;
608 defd.ReadData( p_sys->es->I_O() );
610 tk.i_default_duration = uint64(defd);
611 msg_Dbg( p_input, "| | | + Track Default Duration="I64Fd, uint64(defd) );
613 else if( EbmlId( *el3 ) == KaxTrackTimecodeScale::ClassInfos.GlobalId )
615 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)el3;
616 ttcs.ReadData( p_sys->es->I_O() );
618 tk.f_timecodescale = float( ttcs );
619 msg_Dbg( p_input, "| | | + Track TimeCodeScale=%f", tk.f_timecodescale );
621 else if( EbmlId( *el3 ) == KaxTrackName::ClassInfos.GlobalId )
623 KaxTrackName &tname = *(KaxTrackName*)el3;
624 tname.ReadData( p_sys->es->I_O() );
626 tk.psz_name = UTF8ToStr( UTFstring( tname ) );
627 msg_Dbg( p_input, "| | | + Track Name=%s",
630 else if( EbmlId( *el3 ) == KaxTrackLanguage::ClassInfos.GlobalId )
632 KaxTrackLanguage &lang = *(KaxTrackLanguage*)el3;
633 lang.ReadData( p_sys->es->I_O() );
636 LanguageGetName( string( lang ).c_str() );
638 "| | | + Track Language=`%s'(%s) ",
639 tk.psz_language, string( lang ).c_str() );
641 else if( EbmlId( *el3 ) == KaxCodecID::ClassInfos.GlobalId )
643 KaxCodecID &codecid = *(KaxCodecID*)el3;
644 codecid.ReadData( p_sys->es->I_O() );
646 tk.psz_codec = strdup( string( codecid ).c_str() );
647 msg_Dbg( p_input, "| | | + Track CodecId=%s",
648 string( codecid ).c_str() );
650 else if( EbmlId( *el3 ) == KaxCodecPrivate::ClassInfos.GlobalId )
652 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)el3;
653 cpriv.ReadData( p_sys->es->I_O() );
655 tk.i_extra_data = cpriv.GetSize();
656 if( tk.i_extra_data > 0 )
658 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
659 memcpy( tk.p_extra_data, cpriv.GetBuffer(), tk.i_extra_data );
661 msg_Dbg( p_input, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
663 else if( EbmlId( *el3 ) == KaxCodecName::ClassInfos.GlobalId )
665 KaxCodecName &cname = *(KaxCodecName*)el3;
666 cname.ReadData( p_sys->es->I_O() );
668 tk.psz_codec_name = UTF8ToStr( UTFstring( cname ) );
669 msg_Dbg( p_input, "| | | + Track Codec Name=%s", tk.psz_codec_name );
671 else if( EbmlId( *el3 ) == KaxCodecSettings::ClassInfos.GlobalId )
673 KaxCodecSettings &cset = *(KaxCodecSettings*)el3;
674 cset.ReadData( p_sys->es->I_O() );
676 tk.psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
677 msg_Dbg( p_input, "| | | + Track Codec Settings=%s", tk.psz_codec_settings );
679 else if( EbmlId( *el3 ) == KaxCodecInfoURL::ClassInfos.GlobalId )
681 KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)el3;
682 ciurl.ReadData( p_sys->es->I_O() );
684 tk.psz_codec_info_url = strdup( string( ciurl ).c_str() );
685 msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_info_url );
687 else if( EbmlId( *el3 ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
689 KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)el3;
690 cdurl.ReadData( p_sys->es->I_O() );
692 tk.psz_codec_download_url = strdup( string( cdurl ).c_str() );
693 msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_download_url );
695 else if( EbmlId( *el3 ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
697 KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)el3;
698 cdall.ReadData( p_sys->es->I_O() );
700 msg_Dbg( p_input, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
702 else if( EbmlId( *el3 ) == KaxTrackOverlay::ClassInfos.GlobalId )
704 KaxTrackOverlay &tovr = *(KaxTrackOverlay*)el3;
705 tovr.ReadData( p_sys->es->I_O() );
707 msg_Dbg( p_input, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
709 else if( EbmlId( *el3 ) == KaxTrackVideo::ClassInfos.GlobalId )
711 msg_Dbg( p_input, "| | | + Track Video" );
714 tk.i_display_width = 0;
715 tk.i_display_height = 0;
720 while( ( el4 = p_sys->ep->Get() ) != NULL )
722 if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
724 KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
725 fint.ReadData( p_sys->es->I_O() );
727 msg_Dbg( p_input, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
729 else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
731 KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
732 stereo.ReadData( p_sys->es->I_O() );
734 msg_Dbg( p_input, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
736 else if( EbmlId( *el4 ) == KaxVideoPixelWidth::ClassInfos.GlobalId )
738 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)el4;
739 vwidth.ReadData( p_sys->es->I_O() );
741 tk.i_width = uint16( vwidth );
742 msg_Dbg( p_input, "| | | | + width=%d", uint16( vwidth ) );
744 else if( EbmlId( *el4 ) == KaxVideoPixelHeight::ClassInfos.GlobalId )
746 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)el4;
747 vheight.ReadData( p_sys->es->I_O() );
749 tk.i_height = uint16( vheight );
750 msg_Dbg( p_input, "| | | | + height=%d", uint16( vheight ) );
752 else if( EbmlId( *el4 ) == KaxVideoDisplayWidth::ClassInfos.GlobalId )
754 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)el4;
755 vwidth.ReadData( p_sys->es->I_O() );
757 tk.i_display_width = uint16( vwidth );
758 msg_Dbg( p_input, "| | | | + display width=%d", uint16( vwidth ) );
760 else if( EbmlId( *el4 ) == KaxVideoDisplayHeight::ClassInfos.GlobalId )
762 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)el4;
763 vheight.ReadData( p_sys->es->I_O() );
765 tk.i_display_height = uint16( vheight );
766 msg_Dbg( p_input, "| | | | + display height=%d", uint16( vheight ) );
768 else if( EbmlId( *el4 ) == KaxVideoFrameRate::ClassInfos.GlobalId )
770 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)el4;
771 vfps.ReadData( p_sys->es->I_O() );
773 tk.f_fps = float( vfps );
774 msg_Dbg( p_input, " | | | + fps=%f", float( vfps ) );
776 else if( EbmlId( *el4 ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
778 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)el4;
779 vdmode.ReadData( p_sys->es->I_O() );
781 msg_Dbg( p_input, "| | | | + Track Video Display Unit=%s",
782 uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
784 else if( EbmlId( *el4 ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
786 KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)el4;
787 ratio.ReadData( p_sys->es->I_O() );
789 msg_Dbg( p_input, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
791 else if( EbmlId( *el4 ) == KaxVideoGamma::ClassInfos.GlobalId )
793 KaxVideoGamma &gamma = *(KaxVideoGamma*)el4;
794 gamma.ReadData( p_sys->es->I_O() );
796 msg_Dbg( p_input, " | | | + fps=%f", float( gamma ) );
800 msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() );
805 else if( EbmlId( *el3 ) == KaxTrackAudio::ClassInfos.GlobalId )
807 msg_Dbg( p_input, "| | | + Track Audio" );
810 tk.i_bitspersample = 0;
814 while( ( el4 = p_sys->ep->Get() ) != NULL )
816 if( EbmlId( *el4 ) == KaxAudioSamplingFreq::ClassInfos.GlobalId )
818 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)el4;
819 afreq.ReadData( p_sys->es->I_O() );
821 tk.i_samplerate = (int)float( afreq );
822 msg_Dbg( p_input, "| | | | + afreq=%d", tk.i_samplerate );
824 else if( EbmlId( *el4 ) == KaxAudioChannels::ClassInfos.GlobalId )
826 KaxAudioChannels &achan = *(KaxAudioChannels*)el4;
827 achan.ReadData( p_sys->es->I_O() );
829 tk.i_channels = uint8( achan );
830 msg_Dbg( p_input, "| | | | + achan=%u", uint8( achan ) );
832 else if( EbmlId( *el4 ) == KaxAudioBitDepth::ClassInfos.GlobalId )
834 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)el4;
835 abits.ReadData( p_sys->es->I_O() );
837 tk.i_bitspersample = uint8( abits );
838 msg_Dbg( p_input, "| | | | + abits=%u", uint8( abits ) );
842 msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() );
849 msg_Dbg( p_input, "| | | + Unknown (%s)",
850 typeid(*el3).name() );
857 msg_Dbg( p_input, "| | + Unknown (%s)",
858 typeid(*el2).name() );
864 else if( EbmlId( *el1 ) == KaxSeekHead::ClassInfos.GlobalId )
866 msg_Dbg( p_input, "| + Seek head" );
868 while( ( el = p_sys->ep->Get() ) != NULL )
870 if( EbmlId( *el ) == KaxSeek::ClassInfos.GlobalId )
872 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
875 //msg_Dbg( p_input, "| | + Seek" );
877 while( ( el = p_sys->ep->Get() ) != NULL )
879 if( EbmlId( *el ) == KaxSeekID::ClassInfos.GlobalId )
881 KaxSeekID &sid = *(KaxSeekID*)el;
883 sid.ReadData( p_sys->es->I_O() );
885 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
887 else if( EbmlId( *el ) == KaxSeekPosition::ClassInfos.GlobalId )
889 KaxSeekPosition &spos = *(KaxSeekPosition*)el;
891 spos.ReadData( p_sys->es->I_O() );
893 i_pos = uint64( spos );
897 msg_Dbg( p_input, "| | | + Unknown (%s)",
898 typeid(*el).name() );
905 if( id == KaxCues::ClassInfos.GlobalId )
907 msg_Dbg( p_input, "| | | = cues at "I64Fd,
909 p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
911 else if( id == KaxChapters::ClassInfos.GlobalId )
913 msg_Dbg( p_input, "| | | = chapters at "I64Fd,
915 p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
917 else if( id == KaxTags::ClassInfos.GlobalId )
919 msg_Dbg( p_input, "| | | = tags at "I64Fd,
921 p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
928 msg_Dbg( p_input, "| | + Unknown (%s)",
929 typeid(*el).name() );
934 else if( EbmlId( *el1 ) == KaxCues::ClassInfos.GlobalId )
936 msg_Dbg( p_input, "| + Cues" );
938 else if( EbmlId( *el1 ) == KaxCluster::ClassInfos.GlobalId )
940 msg_Dbg( p_input, "| + Cluster" );
942 p_sys->cluster = (KaxCluster*)el1;
945 /* stop parsing the stream */
948 #ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
949 else if( EbmlId( *el1 ) == KaxAttachments::ClassInfos.GlobalId )
951 else if( EbmlId( *el1 ) == KaxAttachements::ClassInfos.GlobalId )
954 msg_Dbg( p_input, "| + Attachments FIXME TODO (but probably never supported)" );
956 else if( EbmlId( *el1 ) == KaxChapters::ClassInfos.GlobalId )
958 msg_Dbg( p_input, "| + Chapters FIXME TODO" );
960 else if( EbmlId( *el1 ) == KaxTag::ClassInfos.GlobalId )
962 msg_Dbg( p_input, "| + Tags FIXME TODO" );
966 msg_Dbg( p_input, "| + Unknown (%s)", typeid(*el1).name() );
970 if( p_sys->cluster == NULL )
972 msg_Err( p_input, "cannot find any cluster, damaged file ?" );
976 if( p_sys->i_chapters_position >= 0 )
978 msg_Warn( p_input, "chapters unsupported" );
981 /* *** Load the cue if found *** */
982 if( p_sys->i_cues_position >= 0 )
984 vlc_bool_t b_seekable;
986 stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
993 if( !p_sys->b_cues || p_sys->i_index <= 0 )
995 msg_Warn( p_input, "no cues/empty cues found->seek won't be precise" );
997 IndexAppendCluster( p_input, p_sys->cluster );
999 p_sys->b_cues = VLC_FALSE;
1002 /* Create one program */
1003 vlc_mutex_lock( &p_input->stream.stream_lock );
1004 if( input_InitStream( p_input, 0 ) == -1)
1006 vlc_mutex_unlock( &p_input->stream.stream_lock );
1007 msg_Err( p_input, "cannot init stream" );
1010 if( input_AddProgram( p_input, 0, 0) == NULL )
1012 vlc_mutex_unlock( &p_input->stream.stream_lock );
1013 msg_Err( p_input, "cannot add program" );
1016 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1017 p_input->stream.i_mux_rate = 0;
1018 vlc_mutex_unlock( &p_input->stream.stream_lock );
1020 if( p_sys->f_duration > 1001.0 )
1022 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000.0 );
1023 p_input->stream.i_mux_rate = stream_Size( p_input->s )/50 / i_duration;
1027 msg_Dbg( p_input, "found %d es", p_sys->i_track );
1028 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1030 #define tk p_sys->track[i_track]
1031 if( tk.i_cat == UNKNOWN_ES )
1033 msg_Warn( p_input, "invalid track[%d, n=%d]", i_track, tk.i_number );
1037 tk.p_es = input_AddES( p_input,
1038 p_input->stream.p_selected_program,
1041 tk.psz_language, 0 );
1042 if( tk.i_cat == SPU_ES )
1045 val.psz_string = "UTF-8";
1046 #if defined(HAVE_ICONV)
1047 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1048 var_Set( p_input, "subsdec-encoding", val );
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" ) ||
1248 !strcmp( tk.psz_codec, "S_SSA" ) ||
1249 !strcmp( tk.psz_codec, "S_ASS" ))
1251 tk.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1252 tk.p_es->p_demux_data = (es_sys_t *)malloc( sizeof( subtitle_data_t ) );
1253 tk.p_es->p_demux_data->psz_header = strdup( (char *)tk.p_extra_data );
1255 else if( !strcmp( tk.psz_codec, "S_VOBSUB" ) )
1257 tk.i_codec = VLC_FOURCC( 's','p','u',' ' );
1261 msg_Err( p_input, "unknow codec id=`%s'", tk.psz_codec );
1262 tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1265 tk.p_es->i_fourcc = tk.i_codec;
1269 /* select track all video, one audio, no spu TODO : improve */
1270 b_audio_selected = VLC_FALSE;
1271 i_audio_channel = 0;
1273 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1275 #define tk p_sys->track[i_track]
1279 vlc_mutex_lock( &p_input->stream.stream_lock );
1280 input_SelectES( p_input, tk.p_es );
1281 vlc_mutex_unlock( &p_input->stream.stream_lock );
1285 if( ( !b_audio_selected && config_GetInt( p_input, "audio-channel" ) < 0 ) ||
1286 i_audio_channel == config_GetInt( p_input, "audio-channel" ) )
1288 vlc_mutex_lock( &p_input->stream.stream_lock );
1289 input_SelectES( p_input, tk.p_es );
1290 vlc_mutex_unlock( &p_input->stream.stream_lock );
1292 b_audio_selected = tk.p_es->p_decoder_fifo ? VLC_TRUE : VLC_FALSE;
1297 if( i_spu_channel == config_GetInt( p_input, "spu-channel" ) )
1299 vlc_mutex_lock( &p_input->stream.stream_lock );
1300 input_SelectES( p_input, tk.p_es );
1301 vlc_mutex_unlock( &p_input->stream.stream_lock );
1309 if( !b_audio_selected )
1311 msg_Warn( p_input, "cannot find/select audio track" );
1314 /* add informations */
1315 InformationsCreate( p_input );
1323 return VLC_EGENERIC;
1326 /*****************************************************************************
1327 * Close: frees unused data
1328 *****************************************************************************/
1329 static void Close( vlc_object_t *p_this )
1331 input_thread_t *p_input = (input_thread_t *)p_this;
1332 demux_sys_t *p_sys = p_input->p_demux_data;
1336 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1338 #define tk p_sys->track[i_track]
1341 free( tk.psz_codec );
1343 if( tk.psz_language )
1345 free( tk.psz_language );
1349 free( p_sys->track );
1351 if( p_sys->psz_writing_application )
1353 free( p_sys->psz_writing_application );
1355 if( p_sys->psz_muxing_application )
1357 free( p_sys->psz_muxing_application );
1360 delete p_sys->segment;
1369 static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1371 demux_sys_t *p_sys = p_input->p_demux_data;
1382 if( p_input->b_die )
1384 return VLC_EGENERIC;
1387 el = p_sys->ep->Get();
1388 i_level = p_sys->ep->GetLevel();
1390 if( el == NULL && *pp_block != NULL )
1392 /* update the index */
1393 #define idx p_sys->index[p_sys->i_index - 1]
1394 if( p_sys->i_index > 0 && idx.i_time == -1 )
1396 idx.i_time = (*pp_block)->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1397 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1405 if( p_sys->ep->GetLevel() > 1 )
1410 msg_Warn( p_input, "EOF" );
1411 return VLC_EGENERIC;
1417 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1419 p_sys->cluster = (KaxCluster*)el;
1421 /* add it to the index */
1422 if( p_sys->i_index == 0 ||
1423 ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
1425 IndexAppendCluster( p_input, p_sys->cluster );
1430 else if( EbmlId( *el ) == KaxCues::ClassInfos.GlobalId )
1432 msg_Warn( p_input, "find KaxCues FIXME" );
1433 return VLC_EGENERIC;
1437 msg_Dbg( p_input, "unknown (%s)", typeid( el ).name() );
1440 else if( i_level == 2 )
1442 if( EbmlId( *el ) == KaxClusterTimecode::ClassInfos.GlobalId )
1444 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1446 ctc.ReadData( p_sys->es->I_O() );
1447 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
1449 else if( EbmlId( *el ) == KaxBlockGroup::ClassInfos.GlobalId )
1454 else if( i_level == 3 )
1456 if( EbmlId( *el ) == KaxBlock::ClassInfos.GlobalId )
1458 *pp_block = (KaxBlock*)el;
1460 (*pp_block)->ReadData( p_sys->es->I_O() );
1461 (*pp_block)->SetParent( *p_sys->cluster );
1465 else if( EbmlId( *el ) == KaxBlockDuration::ClassInfos.GlobalId )
1467 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1469 dur.ReadData( p_sys->es->I_O() );
1470 *pi_duration = uint64( dur );
1472 else if( EbmlId( *el ) == KaxReferenceBlock::ClassInfos.GlobalId )
1474 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1476 ref.ReadData( p_sys->es->I_O() );
1477 if( *pi_ref1 == -1 )
1479 *pi_ref1 = int64( ref );
1483 *pi_ref2 = int64( ref );
1489 msg_Err( p_input, "invalid level = %d", i_level );
1490 return VLC_EGENERIC;
1495 static pes_packet_t *MemToPES( input_thread_t *p_input, uint8_t *p_mem, int i_mem )
1497 pes_packet_t *p_pes;
1498 data_packet_t *p_data;
1500 if( ( p_pes = input_NewPES( p_input->p_method_data ) ) == NULL )
1505 p_data = input_NewPacket( p_input->p_method_data, i_mem);
1507 memcpy( p_data->p_payload_start, p_mem, i_mem );
1508 p_data->p_payload_end = p_data->p_payload_start + i_mem;
1510 p_pes->p_first = p_pes->p_last = p_data;
1511 p_pes->i_nb_data = 1;
1512 p_pes->i_pes_size = i_mem;
1513 p_pes->i_rate = p_input->stream.control.i_rate;
1518 static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts, mtime_t i_duration )
1520 demux_sys_t *p_sys = p_input->p_demux_data;
1525 #define tk p_sys->track[i_track]
1526 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1528 if( tk.i_number == block->TrackNum() )
1534 if( i_track >= p_sys->i_track )
1536 msg_Err( p_input, "invalid track number=%d", block->TrackNum() );
1540 if( tk.p_es->p_decoder_fifo == NULL )
1542 tk.b_inited = VLC_FALSE;
1546 if( tk.i_cat == AUDIO_ES && p_input->stream.control.b_mute )
1551 /* First send init data */
1552 if( !tk.b_inited && tk.i_data_init > 0 )
1554 pes_packet_t *p_init;
1556 msg_Dbg( p_input, "sending header (%d bytes)", tk.i_data_init );
1558 if( tk.i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
1564 /* XXX hack split the 3 headers */
1565 if( tk.p_data_init[0] != 0x02 )
1567 msg_Err( p_input, "invalid vorbis header" );
1570 for( i = 0; i < 2; i++ )
1573 while( i_offset < tk.i_data_init )
1575 i_size[i] += tk.p_data_init[i_offset];
1576 if( tk.p_data_init[i_offset++] != 0xff )
1582 i_size[0] = __MIN( i_size[0], tk.i_data_init - i_offset );
1583 i_size[1] = __MIN( i_size[1], tk.i_data_init - i_offset - i_size[0] );
1584 i_size[2] = tk.i_data_init - i_offset - i_size[0] - i_size[1];
1586 p_init = MemToPES( p_input, &tk.p_data_init[i_offset], i_size[0] );
1589 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1591 p_init = MemToPES( p_input, &tk.p_data_init[i_offset+i_size[0]], i_size[1] );
1594 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1596 p_init = MemToPES( p_input, &tk.p_data_init[i_offset+i_size[0]+i_size[1]], i_size[2] );
1599 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1604 p_init = MemToPES( p_input, tk.p_data_init, tk.i_data_init );
1607 input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
1611 tk.b_inited = VLC_TRUE;
1614 for( i = 0; i < block->NumberFrames(); i++ )
1616 pes_packet_t *p_pes;
1617 DataBuffer &data = block->GetBuffer(i);
1619 p_pes = MemToPES( p_input, data.Buffer(), data.Size() );
1625 p_pes->i_pts = i_pts;
1626 p_pes->i_dts = i_pts;
1628 if( tk.i_cat == SPU_ES && strcmp( tk.psz_codec, "S_VOBSUB" ) )
1630 if( i_duration > 0 )
1632 p_pes->i_dts += i_duration * 1000;
1639 if( p_pes->p_first && p_pes->i_pes_size > 0 )
1641 p_pes->p_first->p_payload_end[0] = '\0';
1645 input_DecodePES( tk.p_es->p_decoder_fifo, p_pes );
1647 /* use time stamp only for first block */
1654 static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent)
1656 demux_sys_t *p_sys = p_input->p_demux_data;
1659 int64_t i_block_duration;
1660 int64_t i_block_ref1;
1661 int64_t i_block_ref2;
1664 int i_track_skipping;
1667 msg_Dbg( p_input, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1668 if( i_date < 0 && i_percent < 0 )
1674 p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
1675 p_sys->cluster = NULL;
1677 /* seek without index or without date */
1678 if( config_GetInt( p_input, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
1680 int64_t i_pos = i_percent * stream_Size( p_input->s ) / 100;
1682 msg_Dbg( p_input, "imprecise way of seeking" );
1683 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1685 if( p_sys->index[i_index].i_position >= i_pos)
1690 if( i_index == p_sys->i_index )
1695 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1698 if( p_sys->index[i_index].i_position < i_pos )
1702 msg_Warn( p_input, "searching for cluster, could take some time" );
1704 /* search a cluster */
1705 while( ( el = p_sys->ep->Get() ) != NULL )
1707 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1709 KaxCluster *cluster = (KaxCluster*)el;
1711 /* add it to the index */
1712 IndexAppendCluster( p_input, cluster );
1714 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1716 p_sys->cluster = cluster;
1726 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1728 if( p_sys->index[i_index].i_time >= i_date )
1739 msg_Dbg( p_input, "seek got "I64Fd" (%d%%)",
1740 p_sys->index[i_index].i_time,
1741 (int)( 100 * p_sys->index[i_index].i_position /
1742 stream_Size( p_input->s ) ) );
1744 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1748 /* now parse until key frame */
1749 #define tk p_sys->track[i_track]
1750 i_track_skipping = 0;
1751 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1753 if( tk.i_cat == VIDEO_ES )
1755 tk.b_search_keyframe = VLC_TRUE;
1760 while( i_track_skipping > 0 )
1762 if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1764 msg_Warn( p_input, "cannot get block EOF?" );
1769 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1771 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1773 if( tk.i_number == block->TrackNum() )
1779 if( i_track < p_sys->i_track )
1781 if( tk.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1783 tk.b_search_keyframe = VLC_FALSE;
1786 if( tk.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1788 BlockDecode( p_input, block, 0, 0 );
1797 /*****************************************************************************
1798 * Demux: reads and demuxes data packets
1799 *****************************************************************************
1800 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1801 *****************************************************************************/
1802 static int Demux( input_thread_t * p_input )
1804 demux_sys_t *p_sys = p_input->p_demux_data;
1805 mtime_t i_start_pts;
1806 int i_block_count = 0;
1809 int64_t i_block_duration;
1810 int64_t i_block_ref1;
1811 int64_t i_block_ref2;
1813 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1815 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
1816 mtime_t i_date = -1;
1819 if( i_duration > 0 )
1821 i_date = (mtime_t)1000000 *
1822 (mtime_t)i_duration*
1823 (mtime_t)p_sys->in->getFilePointer() /
1824 (mtime_t)stream_Size( p_input->s );
1826 if( stream_Size( p_input->s ) > 0 )
1828 i_percent = 100 * p_sys->in->getFilePointer() /
1829 stream_Size( p_input->s );
1832 Seek( p_input, i_date, i_percent);
1841 if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1843 msg_Warn( p_input, "cannot get block EOF?" );
1848 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1850 if( p_sys->i_pts > 0 )
1852 input_ClockManageRef( p_input,
1853 p_input->stream.p_selected_program,
1854 p_sys->i_pts * 9 / 100 );
1857 i_pts = input_ClockGetTS( p_input,
1858 p_input->stream.p_selected_program,
1859 p_sys->i_pts * 9 / 100 );
1863 BlockDecode( p_input, block, i_pts, i_block_duration );
1868 if( i_start_pts == -1 )
1870 i_start_pts = p_sys->i_pts;
1872 else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1881 /*****************************************************************************
1883 *****************************************************************************/
1884 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1890 uint32_t vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1892 if( i_size <= 0 || mb_eof )
1897 return stream_Read( s, p_buffer, i_size );
1899 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1905 case seek_beginning:
1909 i_pos = stream_Size( s ) - i_offset;
1912 i_pos= stream_Tell( s ) + i_offset;
1916 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1923 if( stream_Seek( s, i_pos ) )
1929 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1933 uint64_t vlc_stream_io_callback::getFilePointer( void )
1935 return stream_Tell( s );
1937 void vlc_stream_io_callback::close( void )
1943 /*****************************************************************************
1944 * Ebml Stream parser
1945 *****************************************************************************/
1946 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1954 for( i = 1; i < 6; i++ )
1960 mb_keep = VLC_FALSE;
1963 EbmlParser::~EbmlParser( void )
1967 for( i = 1; i < mi_level; i++ )
1973 mb_keep = VLC_FALSE;
1977 void EbmlParser::Up( void )
1979 if( mi_user_level == mi_level )
1981 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1987 void EbmlParser::Down( void )
1993 void EbmlParser::Keep( void )
1998 int EbmlParser::GetLevel( void )
2000 return mi_user_level;
2003 EbmlElement *EbmlParser::Get( void )
2007 if( mi_user_level != mi_level )
2013 EbmlElement *ret = m_got;
2019 if( m_el[mi_level] )
2021 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
2024 delete m_el[mi_level];
2026 mb_keep = VLC_FALSE;
2029 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
2040 delete m_el[mi_level - 1];
2041 m_got = m_el[mi_level -1] = m_el[mi_level];
2042 m_el[mi_level] = NULL;
2049 else if( m_el[mi_level] == NULL )
2051 fprintf( stderr," m_el[mi_level] == NULL\n" );
2054 return m_el[mi_level];
2058 /*****************************************************************************
2060 * * LoadCues : load the cues element and update index
2062 * * LoadTags : load ... the tags element
2064 * * InformationsCreate : create all informations, load tags if present
2066 *****************************************************************************/
2067 static void LoadCues( input_thread_t *p_input )
2069 demux_sys_t *p_sys = p_input->p_demux_data;
2070 int64_t i_sav_position = p_sys->in->getFilePointer();
2072 EbmlElement *el, *cues;
2074 msg_Dbg( p_input, "loading cues" );
2075 p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
2076 cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
2080 msg_Err( p_input, "cannot load cues (broken seekhead or file)" );
2084 ep = new EbmlParser( p_sys->es, cues );
2085 while( ( el = ep->Get() ) != NULL )
2087 if( EbmlId( *el ) == KaxCuePoint::ClassInfos.GlobalId )
2089 #define idx p_sys->index[p_sys->i_index]
2092 idx.i_block_number= -1;
2093 idx.i_position = -1;
2095 idx.b_key = VLC_TRUE;
2098 while( ( el = ep->Get() ) != NULL )
2100 if( EbmlId( *el ) == KaxCueTime::ClassInfos.GlobalId )
2102 KaxCueTime &ctime = *(KaxCueTime*)el;
2104 ctime.ReadData( p_sys->es->I_O() );
2106 idx.i_time = uint64( ctime ) * (mtime_t)1000000000 / p_sys->i_timescale;
2108 else if( EbmlId( *el ) == KaxCueTrackPositions::ClassInfos.GlobalId )
2111 while( ( el = ep->Get() ) != NULL )
2113 if( EbmlId( *el ) == KaxCueTrack::ClassInfos.GlobalId )
2115 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
2117 ctrack.ReadData( p_sys->es->I_O() );
2118 idx.i_track = uint16( ctrack );
2120 else if( EbmlId( *el ) == KaxCueClusterPosition::ClassInfos.GlobalId )
2122 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
2124 ccpos.ReadData( p_sys->es->I_O() );
2125 idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
2127 else if( EbmlId( *el ) == KaxCueBlockNumber::ClassInfos.GlobalId )
2129 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2131 cbnum.ReadData( p_sys->es->I_O() );
2132 idx.i_block_number = uint32( cbnum );
2136 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2143 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2148 msg_Dbg( p_input, " * added time="I64Fd" pos="I64Fd
2149 " track=%d bnum=%d", idx.i_time, idx.i_position,
2150 idx.i_track, idx.i_block_number );
2153 if( p_sys->i_index >= p_sys->i_index_max )
2155 p_sys->i_index_max += 1024;
2156 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2162 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2168 p_sys->b_cues = VLC_TRUE;
2170 msg_Dbg( p_input, "loading cues done." );
2171 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2174 static void LoadTags( input_thread_t *p_input )
2176 demux_sys_t *p_sys = p_input->p_demux_data;
2177 int64_t i_sav_position = p_sys->in->getFilePointer();
2179 EbmlElement *el, *tags;
2181 msg_Dbg( p_input, "loading tags" );
2182 p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
2183 tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2187 msg_Err( p_input, "cannot load tags (broken seekhead or file)" );
2191 msg_Dbg( p_input, "Tags" );
2192 ep = new EbmlParser( p_sys->es, tags );
2193 while( ( el = ep->Get() ) != NULL )
2195 if( EbmlId( *el ) == KaxTag::ClassInfos.GlobalId )
2197 msg_Dbg( p_input, "+ Tag" );
2199 while( ( el = ep->Get() ) != NULL )
2201 if( EbmlId( *el ) == KaxTagTargets::ClassInfos.GlobalId )
2203 msg_Dbg( p_input, "| + Targets" );
2205 while( ( el = ep->Get() ) != NULL )
2207 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2211 else if( EbmlId( *el ) == KaxTagGeneral::ClassInfos.GlobalId )
2213 msg_Dbg( p_input, "| + General" );
2215 while( ( el = ep->Get() ) != NULL )
2217 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2221 else if( EbmlId( *el ) == KaxTagGenres::ClassInfos.GlobalId )
2223 msg_Dbg( p_input, "| + Genres" );
2225 while( ( el = ep->Get() ) != NULL )
2227 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2231 else if( EbmlId( *el ) == KaxTagAudioSpecific::ClassInfos.GlobalId )
2233 msg_Dbg( p_input, "| + Audio Specific" );
2235 while( ( el = ep->Get() ) != NULL )
2237 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2241 else if( EbmlId( *el ) == KaxTagImageSpecific::ClassInfos.GlobalId )
2243 msg_Dbg( p_input, "| + Images Specific" );
2245 while( ( el = ep->Get() ) != NULL )
2247 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2251 else if( EbmlId( *el ) == KaxTagMultiComment::ClassInfos.GlobalId )
2253 msg_Dbg( p_input, "| + Multi Comment" );
2255 else if( EbmlId( *el ) == KaxTagMultiCommercial::ClassInfos.GlobalId )
2257 msg_Dbg( p_input, "| + Multi Commercial" );
2259 else if( EbmlId( *el ) == KaxTagMultiDate::ClassInfos.GlobalId )
2261 msg_Dbg( p_input, "| + Multi Date" );
2263 else if( EbmlId( *el ) == KaxTagMultiEntity::ClassInfos.GlobalId )
2265 msg_Dbg( p_input, "| + Multi Entity" );
2267 else if( EbmlId( *el ) == KaxTagMultiIdentifier::ClassInfos.GlobalId )
2269 msg_Dbg( p_input, "| + Multi Identifier" );
2271 else if( EbmlId( *el ) == KaxTagMultiLegal::ClassInfos.GlobalId )
2273 msg_Dbg( p_input, "| + Multi Legal" );
2275 else if( EbmlId( *el ) == KaxTagMultiTitle::ClassInfos.GlobalId )
2277 msg_Dbg( p_input, "| + Multi Title" );
2281 msg_Dbg( p_input, "| + Unknown (%s)", typeid( *el ).name() );
2288 msg_Dbg( p_input, "+ Unknown (%s)", typeid( *el ).name() );
2294 msg_Dbg( p_input, "loading tags done." );
2295 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2298 static void InformationsCreate( input_thread_t *p_input )
2300 demux_sys_t *p_sys = p_input->p_demux_data;
2301 input_info_category_t *p_cat;
2304 p_cat = input_InfoCategory( p_input, "Matroska" );
2305 if( p_sys->f_duration > 1000.1 )
2307 int64_t i_sec = (int64_t)p_sys->f_duration / 1000;
2311 m = ( i_sec / 60 ) % 60;
2314 input_AddInfo( p_cat, _("Duration"), "%d:%2.2d:%2.2d" , h, m, s );
2317 if( p_sys->psz_title )
2319 input_AddInfo( p_cat, _("Title"), "%s" ,p_sys->psz_title );
2321 if( p_sys->psz_date_utc )
2323 input_AddInfo( p_cat, _("Date UTC"), "%s" ,p_sys->psz_date_utc );
2325 if( p_sys->psz_segment_filename )
2327 input_AddInfo( p_cat, _("Segment Filename"), "%s" ,p_sys->psz_segment_filename );
2329 if( p_sys->psz_muxing_application )
2331 input_AddInfo( p_cat, _("Muxing Application"), "%s" ,p_sys->psz_muxing_application );
2333 if( p_sys->psz_writing_application )
2335 input_AddInfo( p_cat, _("Writing Application"), "%s" ,p_sys->psz_writing_application );
2337 input_AddInfo( p_cat, _("Number of streams"), "%d" , p_sys->i_track );
2339 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
2341 char psz_cat[strlen( "Stream " ) + 10];
2342 #define tk p_sys->track[i_track]
2344 sprintf( psz_cat, "Stream %d", i_track );
2345 p_cat = input_InfoCategory( p_input, psz_cat);
2348 input_AddInfo( p_cat, _("Name"), "%s", tk.psz_name );
2350 if( tk.psz_codec_name )
2352 input_AddInfo( p_cat, _("Codec Name"), "%s", tk.psz_codec_name );
2354 if( tk.psz_codec_settings )
2356 input_AddInfo( p_cat, _("Codec Setting"), "%s", tk.psz_codec_settings );
2358 if( tk.psz_codec_info_url )
2360 input_AddInfo( p_cat, _("Codec Info"), "%s", tk.psz_codec_info_url );
2362 if( tk.psz_codec_download_url )
2364 input_AddInfo( p_cat, _("Codec Download"), "%s", tk.psz_codec_download_url );
2370 input_AddInfo( p_cat, _("Type"), _("Audio") );
2371 input_AddInfo( p_cat, _("Codec"), "%.4s (%s)", (char*)&tk.i_codec, tk.psz_codec );
2372 if( tk.i_channels > 0 )
2374 input_AddInfo( p_cat, _("Channels"), "%d", tk.i_channels );
2376 if( tk.i_samplerate > 0 )
2378 input_AddInfo( p_cat, _("Sample Rate"), "%d", tk.i_samplerate );
2380 if( tk.i_bitspersample )
2382 input_AddInfo( p_cat, _("Bits Per Sample"), "%d", tk.i_bitspersample );
2386 input_AddInfo( p_cat, _("Type"), _("Video") );
2387 input_AddInfo( p_cat, _("Codec"), "%.4s (%s)", (char*)&tk.i_codec, tk.psz_codec );
2388 if( tk.i_width > 0 && tk.i_height )
2390 input_AddInfo( p_cat, _("Resolution"), "%dx%d", tk.i_width, tk.i_height );
2392 if( tk.i_display_width > 0 && tk.i_display_height )
2394 input_AddInfo( p_cat, _("Display Resolution"), "%dx%d", tk.i_display_width, tk.i_display_height );
2396 if( tk.f_fps > 0.1 )
2398 input_AddInfo( p_cat, _("Frame Per Second"), "%.3f", tk.f_fps );
2402 input_AddInfo( p_cat, _("Type"), _("Subtitle") );
2403 input_AddInfo( p_cat, _("Codec"), "%s", tk.psz_codec );
2410 if( p_sys->i_tags_position >= 0 )
2412 vlc_bool_t b_seekable;
2414 stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
2417 LoadTags( p_input );
2423 /*****************************************************************************
2425 *****************************************************************************/
2427 static void IndexAppendCluster( input_thread_t *p_input, KaxCluster *cluster )
2429 demux_sys_t *p_sys = p_input->p_demux_data;
2431 #define idx p_sys->index[p_sys->i_index]
2433 idx.i_block_number= -1;
2434 idx.i_position = cluster->GetElementPosition();
2436 idx.b_key = VLC_TRUE;
2439 if( p_sys->i_index >= p_sys->i_index_max )
2441 p_sys->i_index_max += 1024;
2442 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2447 static char * UTF8ToStr( const UTFstring &u )
2456 p = dst = (char*)malloc( i_src + 1);
2475 static char *LanguageGetName ( const char *psz_code )
2477 const iso639_lang_t *pl;
2479 if( strlen( psz_code ) == 2 )
2481 pl = GetLang_1( psz_code );
2483 else if( strlen( psz_code ) == 3 )
2485 pl = GetLang_2B( psz_code );
2486 if( !strcmp( pl->psz_iso639_1, "??" ) )
2488 pl = GetLang_2T( psz_code );
2493 return strdup( psz_code );
2496 if( !strcmp( pl->psz_iso639_1, "??" ) )
2498 return strdup( psz_code );
2502 if( *pl->psz_native_name )
2504 return strdup( pl->psz_native_name );
2506 return strdup( pl->psz_eng_name );