1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
5 * $Id: mkv.cpp,v 1.58 2004/03/02 13:53:14 kuehne 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 set_description( _("Matroska stream demuxer" ) );
95 set_capability( "demux", 50 );
96 set_callbacks( Open, Close );
98 add_bool( "mkv-seek-percent", 1, NULL,
99 N_("Seek based on percent not time"),
100 N_("Seek based on percent not time"), VLC_TRUE );
102 add_shortcut( "mka" );
103 add_shortcut( "mkv" );
106 /*****************************************************************************
108 *****************************************************************************/
109 static int Demux ( input_thread_t * );
110 static void Seek ( input_thread_t *, mtime_t i_date, int i_percent );
113 /*****************************************************************************
115 *****************************************************************************/
116 class vlc_stream_io_callback: public IOCallback
123 vlc_stream_io_callback( stream_t * );
125 virtual uint32_t read ( void *p_buffer, size_t i_size);
126 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
127 virtual size_t write ( const void *p_buffer, size_t i_size);
128 virtual uint64_t getFilePointer ( void );
129 virtual void close ( void );
132 /*****************************************************************************
134 *****************************************************************************/
138 EbmlParser( EbmlStream *es, EbmlElement *el_start );
143 EbmlElement *Get( void );
146 int GetLevel( void );
151 EbmlElement *m_el[6];
160 /*****************************************************************************
161 * Some functions to manipulate memory
162 *****************************************************************************/
163 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
164 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
166 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
169 /*****************************************************************************
170 * definitions of structures and functions used by this plugins
171 *****************************************************************************/
174 vlc_bool_t b_default;
175 vlc_bool_t b_enabled;
179 uint8_t *p_extra_data;
183 uint64_t i_default_duration;
184 float f_timecodescale;
192 /* data to be send first */
194 uint8_t *p_data_init;
196 /* hack : it's for seek */
197 vlc_bool_t b_search_keyframe;
200 char *psz_codec_name;
201 char *psz_codec_settings;
202 char *psz_codec_info_url;
203 char *psz_codec_download_url;
220 vlc_stream_io_callback *in;
225 uint64_t i_timescale;
227 /* duration of the segment */
235 int64_t i_cues_position;
236 int64_t i_chapters_position;
237 int64_t i_tags_position;
251 char *psz_muxing_application;
252 char *psz_writing_application;
253 char *psz_segment_filename;
258 #define MKVD_TIMECODESCALE 1000000
260 static void IndexAppendCluster ( input_thread_t *p_input, KaxCluster *cluster );
261 static char *UTF8ToStr ( const UTFstring &u );
262 static void LoadCues ( input_thread_t *);
263 static void InformationsCreate ( input_thread_t *p_input );
265 /*****************************************************************************
266 * Open: initializes matroska demux structures
267 *****************************************************************************/
268 static int Open( vlc_object_t * p_this )
270 input_thread_t *p_input = (input_thread_t *)p_this;
276 EbmlElement *el = NULL, *el1 = NULL, *el2 = NULL, *el3 = NULL, *el4 = NULL;
278 /* Set the demux function */
279 p_input->pf_demux = Demux;
280 p_input->pf_demux_control = demux_vaControlDefault;
282 /* peek the begining */
283 if( input_Peek( p_input, &p_peek, 4 ) < 4 )
285 msg_Warn( p_input, "cannot peek" );
289 /* is a valid file */
290 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
291 p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
293 msg_Warn( p_input, "matroska module discarded "
294 "(invalid header 0x%.2x%.2x%.2x%.2x)",
295 p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
299 p_input->p_demux_data = p_sys = (demux_sys_t*)malloc(sizeof( demux_sys_t ));
300 memset( p_sys, 0, sizeof( demux_sys_t ) );
302 p_sys->in = new vlc_stream_io_callback( p_input->s );
303 p_sys->es = new EbmlStream( *p_sys->in );
304 p_sys->f_duration = -1;
305 p_sys->i_timescale = MKVD_TIMECODESCALE;
307 p_sys->track = (mkv_track_t*)malloc( sizeof( mkv_track_t ) );
309 p_sys->i_cues_position = -1;
310 p_sys->i_chapters_position = -1;
311 p_sys->i_tags_position = -1;
313 p_sys->b_cues = VLC_FALSE;
315 p_sys->i_index_max = 1024;
316 p_sys->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
317 p_sys->i_index_max );
319 p_sys->psz_muxing_application = NULL;
320 p_sys->psz_writing_application = NULL;
321 p_sys->psz_segment_filename = NULL;
322 p_sys->psz_title = NULL;
323 p_sys->psz_date_utc = NULL;;
325 if( p_sys->es == NULL )
327 msg_Err( p_input, "failed to create EbmlStream" );
332 /* Find the EbmlHead element */
333 el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
336 msg_Err( p_input, "cannot find EbmlHead" );
339 msg_Dbg( p_input, "EbmlHead" );
341 el->SkipData( *p_sys->es, el->Generic().Context );
345 el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
348 msg_Err( p_input, "cannot find KaxSegment" );
351 msg_Dbg( p_input, "+ Segment" );
352 p_sys->segment = (KaxSegment*)el;
353 p_sys->cluster = NULL;
355 p_sys->ep = new EbmlParser( p_sys->es, el );
357 while( ( el1 = p_sys->ep->Get() ) != NULL )
359 if( EbmlId( *el1 ) == KaxInfo::ClassInfos.GlobalId )
361 msg_Dbg( p_input, "| + Informations" );
364 while( ( el2 = p_sys->ep->Get() ) != NULL )
366 if( EbmlId( *el2 ) == KaxTimecodeScale::ClassInfos.GlobalId )
368 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)el2;
370 tcs.ReadData( p_sys->es->I_O() );
371 p_sys->i_timescale = uint64(tcs);
373 msg_Dbg( p_input, "| | + TimecodeScale="I64Fd,
374 p_sys->i_timescale );
376 else if( EbmlId( *el2 ) == KaxDuration::ClassInfos.GlobalId )
378 KaxDuration &dur = *(KaxDuration*)el2;
380 dur.ReadData( p_sys->es->I_O() );
381 p_sys->f_duration = float(dur);
383 msg_Dbg( p_input, "| | + Duration=%f",
386 else if( EbmlId( *el2 ) == KaxMuxingApp::ClassInfos.GlobalId )
388 KaxMuxingApp &mapp = *(KaxMuxingApp*)el2;
390 mapp.ReadData( p_sys->es->I_O() );
392 p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
394 msg_Dbg( p_input, "| | + Muxing Application=%s",
395 p_sys->psz_muxing_application );
397 else if( EbmlId( *el2 ) == KaxWritingApp::ClassInfos.GlobalId )
399 KaxWritingApp &wapp = *(KaxWritingApp*)el2;
401 wapp.ReadData( p_sys->es->I_O() );
403 p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
405 msg_Dbg( p_input, "| | + Wrinting Application=%s",
406 p_sys->psz_writing_application );
408 else if( EbmlId( *el2 ) == KaxSegmentFilename::ClassInfos.GlobalId )
410 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)el2;
412 sfn.ReadData( p_sys->es->I_O() );
414 p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
416 msg_Dbg( p_input, "| | + Segment Filename=%s",
417 p_sys->psz_segment_filename );
419 else if( EbmlId( *el2 ) == KaxTitle::ClassInfos.GlobalId )
421 KaxTitle &title = *(KaxTitle*)el2;
423 title.ReadData( p_sys->es->I_O() );
425 p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
427 msg_Dbg( p_input, "| | + Title=%s", p_sys->psz_title );
430 else if( EbmlId( *el2 ) == KaxDateUTC::ClassInfos.GlobalId )
432 KaxDateUTC &date = *(KaxDateUTC*)el2;
437 date.ReadData( p_sys->es->I_O() );
439 i_date = date.GetEpochDate();
440 memset( buffer, 0, 256 );
441 if( gmtime_r( &i_date, &tmres ) &&
442 asctime_r( &tmres, buffer ) )
444 buffer[strlen( buffer)-1]= '\0';
445 p_sys->psz_date_utc = strdup( buffer );
446 msg_Dbg( p_input, "| | + Date=%s", p_sys->psz_date_utc );
452 msg_Dbg( p_input, "| | + Unknown (%s)", typeid(*el2).name() );
457 else if( EbmlId( *el1 ) == KaxTracks::ClassInfos.GlobalId )
459 msg_Dbg( p_input, "| + Tracks" );
462 while( ( el2 = p_sys->ep->Get() ) != NULL )
464 if( EbmlId( *el2 ) == KaxTrackEntry::ClassInfos.GlobalId )
466 msg_Dbg( p_input, "| | + Track Entry" );
469 p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
470 #define tk p_sys->track[p_sys->i_track - 1]
471 memset( &tk, 0, sizeof( mkv_track_t ) );
473 es_format_Init( &tk.fmt, UNKNOWN_ES, 0 );
474 tk.fmt.psz_language = strdup("English");
475 tk.fmt.psz_description = NULL;
477 tk.b_default = VLC_TRUE;
478 tk.b_enabled = VLC_TRUE;
479 tk.i_number = p_sys->i_track - 1;
481 tk.p_extra_data = NULL;
483 tk.i_default_duration = 0;
484 tk.f_timecodescale = 1.0;
486 tk.b_inited = VLC_FALSE;
488 tk.p_data_init = NULL;
490 tk.psz_codec_name = NULL;
491 tk.psz_codec_settings = NULL;
492 tk.psz_codec_info_url = NULL;
493 tk.psz_codec_download_url = NULL;
497 while( ( el3 = p_sys->ep->Get() ) != NULL )
499 if( EbmlId( *el3 ) == KaxTrackNumber::ClassInfos.GlobalId )
501 KaxTrackNumber &tnum = *(KaxTrackNumber*)el3;
502 tnum.ReadData( p_sys->es->I_O() );
504 tk.i_number = uint32( tnum );
505 msg_Dbg( p_input, "| | | + Track Number=%u",
508 else if( EbmlId( *el3 ) == KaxTrackUID::ClassInfos.GlobalId )
510 KaxTrackUID &tuid = *(KaxTrackUID*)el3;
511 tuid.ReadData( p_sys->es->I_O() );
513 msg_Dbg( p_input, "| | | + Track UID=%u",
516 else if( EbmlId( *el3 ) == KaxTrackType::ClassInfos.GlobalId )
519 KaxTrackType &ttype = *(KaxTrackType*)el3;
520 ttype.ReadData( p_sys->es->I_O() );
521 switch( uint8(ttype) )
525 tk.fmt.i_cat = AUDIO_ES;
529 tk.fmt.i_cat = VIDEO_ES;
532 psz_type = "subtitle";
533 tk.fmt.i_cat = SPU_ES;
536 psz_type = "unknown";
537 tk.fmt.i_cat = UNKNOWN_ES;
541 msg_Dbg( p_input, "| | | + Track Type=%s",
544 // else if( EbmlId( *el3 ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
546 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)el3;
547 // fenb.ReadData( p_sys->es->I_O() );
549 // tk.b_enabled = uint32( fenb );
550 // msg_Dbg( p_input, "| | | + Track Enabled=%u",
553 else if( EbmlId( *el3 ) == KaxTrackFlagDefault::ClassInfos.GlobalId )
555 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)el3;
556 fdef.ReadData( p_sys->es->I_O() );
558 tk.b_default = uint32( fdef );
559 msg_Dbg( p_input, "| | | + Track Default=%u",
562 else if( EbmlId( *el3 ) == KaxTrackFlagLacing::ClassInfos.GlobalId )
564 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)el3;
565 lac.ReadData( p_sys->es->I_O() );
567 msg_Dbg( p_input, "| | | + Track Lacing=%d",
570 else if( EbmlId( *el3 ) == KaxTrackMinCache::ClassInfos.GlobalId )
572 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)el3;
573 cmin.ReadData( p_sys->es->I_O() );
575 msg_Dbg( p_input, "| | | + Track MinCache=%d",
578 else if( EbmlId( *el3 ) == KaxTrackMaxCache::ClassInfos.GlobalId )
580 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)el3;
581 cmax.ReadData( p_sys->es->I_O() );
583 msg_Dbg( p_input, "| | | + Track MaxCache=%d",
586 else if( EbmlId( *el3 ) == KaxTrackDefaultDuration::ClassInfos.GlobalId )
588 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)el3;
589 defd.ReadData( p_sys->es->I_O() );
591 tk.i_default_duration = uint64(defd);
592 msg_Dbg( p_input, "| | | + Track Default Duration="I64Fd, uint64(defd) );
594 else if( EbmlId( *el3 ) == KaxTrackTimecodeScale::ClassInfos.GlobalId )
596 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)el3;
597 ttcs.ReadData( p_sys->es->I_O() );
599 tk.f_timecodescale = float( ttcs );
600 msg_Dbg( p_input, "| | | + Track TimeCodeScale=%f", tk.f_timecodescale );
602 else if( EbmlId( *el3 ) == KaxTrackName::ClassInfos.GlobalId )
604 KaxTrackName &tname = *(KaxTrackName*)el3;
605 tname.ReadData( p_sys->es->I_O() );
607 tk.fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
608 msg_Dbg( p_input, "| | | + Track Name=%s",
609 tk.fmt.psz_description );
611 else if( EbmlId( *el3 ) == KaxTrackLanguage::ClassInfos.GlobalId )
613 KaxTrackLanguage &lang = *(KaxTrackLanguage*)el3;
614 lang.ReadData( p_sys->es->I_O() );
616 tk.fmt.psz_language = strdup( string( lang ).c_str() );
618 "| | | + Track Language=`%s'",
619 tk.fmt.psz_language );
621 else if( EbmlId( *el3 ) == KaxCodecID::ClassInfos.GlobalId )
623 KaxCodecID &codecid = *(KaxCodecID*)el3;
624 codecid.ReadData( p_sys->es->I_O() );
626 tk.psz_codec = strdup( string( codecid ).c_str() );
627 msg_Dbg( p_input, "| | | + Track CodecId=%s",
628 string( codecid ).c_str() );
630 else if( EbmlId( *el3 ) == KaxCodecPrivate::ClassInfos.GlobalId )
632 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)el3;
633 cpriv.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
635 tk.i_extra_data = cpriv.GetSize();
636 if( tk.i_extra_data > 0 )
638 tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
639 memcpy( tk.p_extra_data, cpriv.GetBuffer(), tk.i_extra_data );
641 msg_Dbg( p_input, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
643 else if( EbmlId( *el3 ) == KaxCodecName::ClassInfos.GlobalId )
645 KaxCodecName &cname = *(KaxCodecName*)el3;
646 cname.ReadData( p_sys->es->I_O() );
648 tk.psz_codec_name = UTF8ToStr( UTFstring( cname ) );
649 msg_Dbg( p_input, "| | | + Track Codec Name=%s", tk.psz_codec_name );
651 // else if( EbmlId( *el3 ) == KaxCodecSettings::ClassInfos.GlobalId )
653 // KaxCodecSettings &cset = *(KaxCodecSettings*)el3;
654 // cset.ReadData( p_sys->es->I_O() );
656 // tk.psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
657 // msg_Dbg( p_input, "| | | + Track Codec Settings=%s", tk.psz_codec_settings );
659 // else if( EbmlId( *el3 ) == KaxCodecInfoURL::ClassInfos.GlobalId )
661 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)el3;
662 // ciurl.ReadData( p_sys->es->I_O() );
664 // tk.psz_codec_info_url = strdup( string( ciurl ).c_str() );
665 // msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_info_url );
667 // else if( EbmlId( *el3 ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
669 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)el3;
670 // cdurl.ReadData( p_sys->es->I_O() );
672 // tk.psz_codec_download_url = strdup( string( cdurl ).c_str() );
673 // msg_Dbg( p_input, "| | | + Track Codec Info URL=%s", tk.psz_codec_download_url );
675 // else if( EbmlId( *el3 ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
677 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)el3;
678 // cdall.ReadData( p_sys->es->I_O() );
680 // msg_Dbg( p_input, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
682 // else if( EbmlId( *el3 ) == KaxTrackOverlay::ClassInfos.GlobalId )
684 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)el3;
685 // tovr.ReadData( p_sys->es->I_O() );
687 // msg_Dbg( p_input, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
689 else if( EbmlId( *el3 ) == KaxTrackVideo::ClassInfos.GlobalId )
691 msg_Dbg( p_input, "| | | + Track Video" );
696 while( ( el4 = p_sys->ep->Get() ) != NULL )
698 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
700 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
701 // fint.ReadData( p_sys->es->I_O() );
703 // msg_Dbg( p_input, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
705 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
707 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
708 // stereo.ReadData( p_sys->es->I_O() );
710 // msg_Dbg( p_input, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
713 if( EbmlId( *el4 ) == KaxVideoPixelWidth::ClassInfos.GlobalId )
715 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)el4;
716 vwidth.ReadData( p_sys->es->I_O() );
718 tk.fmt.video.i_width = uint16( vwidth );
719 msg_Dbg( p_input, "| | | | + width=%d", uint16( vwidth ) );
721 else if( EbmlId( *el4 ) == KaxVideoPixelHeight::ClassInfos.GlobalId )
723 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)el4;
724 vheight.ReadData( p_sys->es->I_O() );
726 tk.fmt.video.i_height = uint16( vheight );
727 msg_Dbg( p_input, "| | | | + height=%d", uint16( vheight ) );
729 else if( EbmlId( *el4 ) == KaxVideoDisplayWidth::ClassInfos.GlobalId )
731 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)el4;
732 vwidth.ReadData( p_sys->es->I_O() );
734 tk.fmt.video.i_visible_width = uint16( vwidth );
735 msg_Dbg( p_input, "| | | | + display width=%d", uint16( vwidth ) );
737 else if( EbmlId( *el4 ) == KaxVideoDisplayHeight::ClassInfos.GlobalId )
739 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)el4;
740 vheight.ReadData( p_sys->es->I_O() );
742 tk.fmt.video.i_visible_height = uint16( vheight );
743 msg_Dbg( p_input, "| | | | + display height=%d", uint16( vheight ) );
745 else if( EbmlId( *el4 ) == KaxVideoFrameRate::ClassInfos.GlobalId )
747 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)el4;
748 vfps.ReadData( p_sys->es->I_O() );
750 tk.f_fps = float( vfps );
751 msg_Dbg( p_input, " | | | + fps=%f", float( vfps ) );
753 // else if( EbmlId( *el4 ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
755 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)el4;
756 // vdmode.ReadData( p_sys->es->I_O() );
758 // msg_Dbg( p_input, "| | | | + Track Video Display Unit=%s",
759 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
761 // else if( EbmlId( *el4 ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
763 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)el4;
764 // ratio.ReadData( p_sys->es->I_O() );
766 // msg_Dbg( p_input, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
768 // else if( EbmlId( *el4 ) == KaxVideoGamma::ClassInfos.GlobalId )
770 // KaxVideoGamma &gamma = *(KaxVideoGamma*)el4;
771 // gamma.ReadData( p_sys->es->I_O() );
773 // msg_Dbg( p_input, " | | | + fps=%f", float( gamma ) );
777 msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() );
782 else if( EbmlId( *el3 ) == KaxTrackAudio::ClassInfos.GlobalId )
784 msg_Dbg( p_input, "| | | + Track Audio" );
788 while( ( el4 = p_sys->ep->Get() ) != NULL )
790 if( EbmlId( *el4 ) == KaxAudioSamplingFreq::ClassInfos.GlobalId )
792 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)el4;
793 afreq.ReadData( p_sys->es->I_O() );
795 tk.fmt.audio.i_rate = (int)float( afreq );
796 msg_Dbg( p_input, "| | | | + afreq=%d", tk.fmt.audio.i_rate );
798 else if( EbmlId( *el4 ) == KaxAudioChannels::ClassInfos.GlobalId )
800 KaxAudioChannels &achan = *(KaxAudioChannels*)el4;
801 achan.ReadData( p_sys->es->I_O() );
803 tk.fmt.audio.i_channels = uint8( achan );
804 msg_Dbg( p_input, "| | | | + achan=%u", uint8( achan ) );
806 else if( EbmlId( *el4 ) == KaxAudioBitDepth::ClassInfos.GlobalId )
808 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)el4;
809 abits.ReadData( p_sys->es->I_O() );
811 tk.fmt.audio.i_bitspersample = uint8( abits );
812 msg_Dbg( p_input, "| | | | + abits=%u", uint8( abits ) );
816 msg_Dbg( p_input, "| | | | + Unknown (%s)", typeid(*el4).name() );
823 msg_Dbg( p_input, "| | | + Unknown (%s)",
824 typeid(*el3).name() );
831 msg_Dbg( p_input, "| | + Unknown (%s)",
832 typeid(*el2).name() );
838 else if( EbmlId( *el1 ) == KaxSeekHead::ClassInfos.GlobalId )
840 msg_Dbg( p_input, "| + Seek head" );
842 while( ( el = p_sys->ep->Get() ) != NULL )
844 if( EbmlId( *el ) == KaxSeek::ClassInfos.GlobalId )
846 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
849 //msg_Dbg( p_input, "| | + Seek" );
851 while( ( el = p_sys->ep->Get() ) != NULL )
853 if( EbmlId( *el ) == KaxSeekID::ClassInfos.GlobalId )
855 KaxSeekID &sid = *(KaxSeekID*)el;
857 sid.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
859 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
861 else if( EbmlId( *el ) == KaxSeekPosition::ClassInfos.GlobalId )
863 KaxSeekPosition &spos = *(KaxSeekPosition*)el;
865 spos.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
867 i_pos = uint64( spos );
871 msg_Dbg( p_input, "| | | + Unknown (%s)",
872 typeid(*el).name() );
879 if( id == KaxCues::ClassInfos.GlobalId )
881 msg_Dbg( p_input, "| | | = cues at "I64Fd,
883 p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
885 else if( id == KaxChapters::ClassInfos.GlobalId )
887 msg_Dbg( p_input, "| | | = chapters at "I64Fd,
889 p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
891 else if( id == KaxTags::ClassInfos.GlobalId )
893 msg_Dbg( p_input, "| | | = tags at "I64Fd,
895 p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
902 msg_Dbg( p_input, "| | + Unknown (%s)",
903 typeid(*el).name() );
908 else if( EbmlId( *el1 ) == KaxCues::ClassInfos.GlobalId )
910 msg_Dbg( p_input, "| + Cues" );
912 else if( EbmlId( *el1 ) == KaxCluster::ClassInfos.GlobalId )
914 msg_Dbg( p_input, "| + Cluster" );
916 p_sys->cluster = (KaxCluster*)el1;
919 /* stop parsing the stream */
922 #ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
923 else if( EbmlId( *el1 ) == KaxAttachments::ClassInfos.GlobalId )
925 else if( EbmlId( *el1 ) == KaxAttachements::ClassInfos.GlobalId )
928 msg_Dbg( p_input, "| + Attachments FIXME TODO (but probably never supported)" );
930 else if( EbmlId( *el1 ) == KaxChapters::ClassInfos.GlobalId )
932 msg_Dbg( p_input, "| + Chapters FIXME TODO" );
934 else if( EbmlId( *el1 ) == KaxTag::ClassInfos.GlobalId )
936 msg_Dbg( p_input, "| + Tags FIXME TODO" );
940 msg_Dbg( p_input, "| + Unknown (%s)", typeid(*el1).name() );
944 if( p_sys->cluster == NULL )
946 msg_Err( p_input, "cannot find any cluster, damaged file ?" );
950 if( p_sys->i_chapters_position >= 0 )
952 msg_Warn( p_input, "chapters unsupported" );
955 /* *** Load the cue if found *** */
956 if( p_sys->i_cues_position >= 0 )
958 vlc_bool_t b_seekable;
960 stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
967 if( !p_sys->b_cues || p_sys->i_index <= 0 )
969 msg_Warn( p_input, "no cues/empty cues found->seek won't be precise" );
971 IndexAppendCluster( p_input, p_sys->cluster );
973 p_sys->b_cues = VLC_FALSE;
976 /* Create one program */
977 vlc_mutex_lock( &p_input->stream.stream_lock );
978 if( input_InitStream( p_input, 0 ) == -1)
980 vlc_mutex_unlock( &p_input->stream.stream_lock );
981 msg_Err( p_input, "cannot init stream" );
984 p_input->stream.i_mux_rate = 0;
985 vlc_mutex_unlock( &p_input->stream.stream_lock );
987 if( p_sys->f_duration > 1001.0 )
989 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000.0 );
990 p_input->stream.i_mux_rate = stream_Size( p_input->s )/50 / i_duration;
994 msg_Dbg( p_input, "found %d es", p_sys->i_track );
995 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
997 #define tk p_sys->track[i_track]
998 if( tk.fmt.i_cat == UNKNOWN_ES )
1000 msg_Warn( p_input, "invalid track[%d, n=%d]", i_track, tk.i_number );
1005 if( tk.fmt.i_cat == SPU_ES )
1008 val.psz_string = "UTF-8";
1009 #if defined(HAVE_ICONV)
1010 var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1011 var_Set( p_input, "subsdec-encoding", val );
1014 if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) )
1016 if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
1018 msg_Err( p_input, "missing/invalid BITMAPINFOHEADER" );
1019 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1023 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
1025 tk.fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1026 tk.fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1027 tk.fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1029 tk.fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
1030 if( tk.fmt.i_extra > 0 )
1032 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1033 memcpy( tk.fmt.p_extra, &p_bih[1], tk.fmt.i_extra );
1037 else if( !strcmp( tk.psz_codec, "V_MPEG1" ) ||
1038 !strcmp( tk.psz_codec, "V_MPEG2" ) )
1040 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1042 else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) )
1044 if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) )
1046 tk.fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1050 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1053 else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
1055 if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1057 msg_Err( p_input, "missing/invalid WAVEFORMATEX" );
1058 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1062 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;
1064 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk.fmt.i_codec, NULL );
1066 tk.fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1067 tk.fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1068 tk.fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1069 tk.fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1070 tk.fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1072 tk.fmt.i_extra = GetWLE( &p_wf->cbSize );
1073 if( tk.fmt.i_extra > 0 )
1075 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1076 memcpy( tk.fmt.p_extra, &p_wf[1], tk.fmt.i_extra );
1080 else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
1081 !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
1082 !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
1084 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1086 else if( !strcmp( tk.psz_codec, "A_AC3" ) )
1088 tk.fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1090 else if( !strcmp( tk.psz_codec, "A_DTS" ) )
1092 tk.fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
1094 else if( !strcmp( tk.psz_codec, "A_FLAC" ) )
1096 tk.fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
1097 tk.fmt.i_extra = tk.i_extra_data;
1098 tk.fmt.p_extra = malloc( tk.i_extra_data );
1099 memcpy( tk.fmt.p_extra,tk.p_extra_data, tk.i_extra_data );
1101 else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
1103 tk.fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
1104 tk.i_data_init = tk.i_extra_data;
1105 tk.p_data_init = tk.p_extra_data;
1107 else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1108 !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1110 int i_profile, i_srate;
1111 static unsigned int i_sample_rates[] =
1113 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1114 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1117 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1118 /* create data for faad (MP4DecSpecificDescrTag)*/
1120 if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
1124 else if( !strcmp( &tk.psz_codec[12], "LC" ) )
1128 else if( !strcmp( &tk.psz_codec[12], "SSR" ) )
1137 for( i_srate = 0; i_srate < 13; i_srate++ )
1139 if( i_sample_rates[i_srate] == tk.fmt.audio.i_rate )
1144 msg_Dbg( p_input, "profile=%d srate=%d", i_profile, i_srate );
1147 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1148 ((uint8_t*)tk.fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1149 ((uint8_t*)tk.fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk.fmt.audio.i_channels << 3);
1151 else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
1152 !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
1153 !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
1155 if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
1157 tk.fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1161 tk.fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1163 tk.fmt.audio.i_blockalign = ( tk.fmt.audio.i_bitspersample + 7 ) / 8 * tk.fmt.audio.i_channels;
1165 else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
1167 tk.fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1169 else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) ||
1170 !strcmp( tk.psz_codec, "S_SSA" ) ||
1171 !strcmp( tk.psz_codec, "S_ASS" ))
1173 tk.fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1176 tk.fmt.i_extra = sizeof( subtitle_data_t );
1177 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
1178 ((es_sys_t*)tk->fmt.p_extra)->psz_header = strdup( (char *)tk.p_extra_data );
1181 else if( !strcmp( tk.psz_codec, "S_VOBSUB" ) )
1183 tk.fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1187 msg_Err( p_input, "unknow codec id=`%s'", tk.psz_codec );
1188 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1191 tk.p_es = es_out_Add( p_input->p_es_out, &tk.fmt );
1195 /* add informations */
1196 InformationsCreate( p_input );
1204 return VLC_EGENERIC;
1207 /*****************************************************************************
1208 * Close: frees unused data
1209 *****************************************************************************/
1210 static void Close( vlc_object_t *p_this )
1212 input_thread_t *p_input = (input_thread_t *)p_this;
1213 demux_sys_t *p_sys = p_input->p_demux_data;
1217 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1219 #define tk p_sys->track[i_track]
1220 if( tk.fmt.psz_description )
1222 free( tk.fmt.psz_description );
1226 free( tk.psz_codec );
1228 if( tk.fmt.psz_language )
1230 free( tk.fmt.psz_language );
1234 free( p_sys->track );
1236 if( p_sys->psz_writing_application )
1238 free( p_sys->psz_writing_application );
1240 if( p_sys->psz_muxing_application )
1242 free( p_sys->psz_muxing_application );
1245 delete p_sys->segment;
1254 static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1256 demux_sys_t *p_sys = p_input->p_demux_data;
1267 if( p_input->b_die )
1269 return VLC_EGENERIC;
1272 el = p_sys->ep->Get();
1273 i_level = p_sys->ep->GetLevel();
1275 if( el == NULL && *pp_block != NULL )
1277 /* update the index */
1278 #define idx p_sys->index[p_sys->i_index - 1]
1279 if( p_sys->i_index > 0 && idx.i_time == -1 )
1281 idx.i_time = (*pp_block)->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1282 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1290 if( p_sys->ep->GetLevel() > 1 )
1295 msg_Warn( p_input, "EOF" );
1296 return VLC_EGENERIC;
1302 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1304 p_sys->cluster = (KaxCluster*)el;
1306 /* add it to the index */
1307 if( p_sys->i_index == 0 ||
1308 ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
1310 IndexAppendCluster( p_input, p_sys->cluster );
1315 else if( EbmlId( *el ) == KaxCues::ClassInfos.GlobalId )
1317 msg_Warn( p_input, "find KaxCues FIXME" );
1318 return VLC_EGENERIC;
1322 msg_Dbg( p_input, "unknown (%s)", typeid( el ).name() );
1325 else if( i_level == 2 )
1327 if( EbmlId( *el ) == KaxClusterTimecode::ClassInfos.GlobalId )
1329 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1331 ctc.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
1332 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
1334 else if( EbmlId( *el ) == KaxBlockGroup::ClassInfos.GlobalId )
1339 else if( i_level == 3 )
1341 if( EbmlId( *el ) == KaxBlock::ClassInfos.GlobalId )
1343 *pp_block = (KaxBlock*)el;
1345 (*pp_block)->ReadData( p_sys->es->I_O() );
1346 (*pp_block)->SetParent( *p_sys->cluster );
1350 else if( EbmlId( *el ) == KaxBlockDuration::ClassInfos.GlobalId )
1352 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1354 dur.ReadData( p_sys->es->I_O() );
1355 *pi_duration = uint64( dur );
1357 else if( EbmlId( *el ) == KaxReferenceBlock::ClassInfos.GlobalId )
1359 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1361 ref.ReadData( p_sys->es->I_O() );
1362 if( *pi_ref1 == -1 )
1364 *pi_ref1 = int64( ref );
1368 *pi_ref2 = int64( ref );
1374 msg_Err( p_input, "invalid level = %d", i_level );
1375 return VLC_EGENERIC;
1380 static block_t *MemToBlock( input_thread_t *p_input, uint8_t *p_mem, int i_mem)
1383 if( !(p_block = block_New( p_input, i_mem ) ) ) return NULL;
1384 memcpy( p_block->p_buffer, p_mem, i_mem );
1385 //p_block->i_rate = p_input->stream.control.i_rate;
1389 static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts, mtime_t i_duration )
1391 demux_sys_t *p_sys = p_input->p_demux_data;
1397 #define tk p_sys->track[i_track]
1398 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1400 if( tk.i_number == block->TrackNum() )
1406 if( i_track >= p_sys->i_track )
1408 msg_Err( p_input, "invalid track number=%d", block->TrackNum() );
1412 es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk.p_es, &b );
1415 tk.b_inited = VLC_FALSE;
1419 if( tk.fmt.i_cat == AUDIO_ES && p_input->stream.control.b_mute )
1424 /* First send init data */
1425 if( !tk.b_inited && tk.i_data_init > 0 )
1429 msg_Dbg( p_input, "sending header (%d bytes)", tk.i_data_init );
1431 if( tk.fmt.i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
1437 /* XXX hack split the 3 headers */
1438 if( tk.p_data_init[0] != 0x02 )
1440 msg_Err( p_input, "invalid vorbis header" );
1443 for( i = 0; i < 2; i++ )
1446 while( i_offset < tk.i_data_init )
1448 i_size[i] += tk.p_data_init[i_offset];
1449 if( tk.p_data_init[i_offset++] != 0xff )
1455 i_size[0] = __MIN( i_size[0], tk.i_data_init - i_offset );
1456 i_size[1] = __MIN( i_size[1], tk.i_data_init - i_offset - i_size[0] );
1457 i_size[2] = tk.i_data_init - i_offset - i_size[0] - i_size[1];
1459 p_init = MemToBlock( p_input, &tk.p_data_init[i_offset], i_size[0] );
1462 es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1464 p_init = MemToBlock( p_input, &tk.p_data_init[i_offset+i_size[0]], i_size[1] );
1467 es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1469 p_init = MemToBlock( p_input, &tk.p_data_init[i_offset+i_size[0]+i_size[1]], i_size[2] );
1472 es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1477 p_init = MemToBlock( p_input, tk.p_data_init, tk.i_data_init );
1480 es_out_Send( p_input->p_es_out, tk.p_es, p_init );
1484 tk.b_inited = VLC_TRUE;
1487 for( i = 0; i < block->NumberFrames(); i++ )
1490 DataBuffer &data = block->GetBuffer(i);
1492 p_block = MemToBlock( p_input, data.Buffer(), data.Size() );
1493 if( p_block == NULL )
1498 if( tk.fmt.i_cat != VIDEO_ES )
1499 p_block->i_dts = p_block->i_pts = i_pts;
1502 p_block->i_dts = i_pts;
1506 if( tk.fmt.i_cat == SPU_ES && strcmp( tk.psz_codec, "S_VOBSUB" ) )
1508 if( i_duration > 0 )
1510 p_block->i_dts += i_duration * 1000;
1517 es_out_Send( p_input->p_es_out, tk.p_es, p_block );
1519 /* use time stamp only for first block */
1526 static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent)
1528 demux_sys_t *p_sys = p_input->p_demux_data;
1531 int64_t i_block_duration;
1532 int64_t i_block_ref1;
1533 int64_t i_block_ref2;
1536 int i_track_skipping;
1539 msg_Dbg( p_input, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1540 if( i_date < 0 && i_percent < 0 )
1546 p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
1547 p_sys->cluster = NULL;
1549 /* seek without index or without date */
1550 if( config_GetInt( p_input, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
1552 int64_t i_pos = i_percent * stream_Size( p_input->s ) / 100;
1554 msg_Dbg( p_input, "inacurate way of seeking" );
1555 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1557 if( p_sys->index[i_index].i_position >= i_pos)
1562 if( i_index == p_sys->i_index )
1567 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1570 if( p_sys->index[i_index].i_position < i_pos )
1574 msg_Warn( p_input, "searching for cluster, could take some time" );
1576 /* search a cluster */
1577 while( ( el = p_sys->ep->Get() ) != NULL )
1579 if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
1581 KaxCluster *cluster = (KaxCluster*)el;
1583 /* add it to the index */
1584 IndexAppendCluster( p_input, cluster );
1586 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1588 p_sys->cluster = cluster;
1598 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1600 if( p_sys->index[i_index].i_time >= i_date )
1611 msg_Dbg( p_input, "seek got "I64Fd" (%d%%)",
1612 p_sys->index[i_index].i_time,
1613 (int)( 100 * p_sys->index[i_index].i_position /
1614 stream_Size( p_input->s ) ) );
1616 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1620 /* now parse until key frame */
1621 #define tk p_sys->track[i_track]
1622 i_track_skipping = 0;
1623 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1625 if( tk.fmt.i_cat == VIDEO_ES )
1627 tk.b_search_keyframe = VLC_TRUE;
1632 while( i_track_skipping > 0 )
1634 if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1636 msg_Warn( p_input, "cannot get block EOF?" );
1641 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1643 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1645 if( tk.i_number == block->TrackNum() )
1651 if( i_track < p_sys->i_track )
1653 if( tk.fmt.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1655 tk.b_search_keyframe = VLC_FALSE;
1658 if( tk.fmt.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1660 BlockDecode( p_input, block, 0, 0 );
1669 /*****************************************************************************
1670 * Demux: reads and demuxes data packets
1671 *****************************************************************************
1672 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1673 *****************************************************************************/
1674 static int Demux( input_thread_t * p_input )
1676 demux_sys_t *p_sys = p_input->p_demux_data;
1677 mtime_t i_start_pts;
1678 int i_block_count = 0;
1681 int64_t i_block_duration;
1682 int64_t i_block_ref1;
1683 int64_t i_block_ref2;
1685 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
1687 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
1688 mtime_t i_date = -1;
1691 if( i_duration > 0 )
1693 i_date = (mtime_t)1000000 *
1694 (mtime_t)i_duration*
1695 (mtime_t)p_sys->in->getFilePointer() /
1696 (mtime_t)stream_Size( p_input->s );
1698 if( stream_Size( p_input->s ) > 0 )
1700 i_percent = 100 * p_sys->in->getFilePointer() /
1701 stream_Size( p_input->s );
1704 Seek( p_input, i_date, i_percent);
1713 if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1715 msg_Warn( p_input, "cannot get block EOF?" );
1720 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
1722 if( p_sys->i_pts > 0 )
1724 input_ClockManageRef( p_input,
1725 p_input->stream.p_selected_program,
1726 p_sys->i_pts * 9 / 100 );
1729 i_pts = input_ClockGetTS( p_input,
1730 p_input->stream.p_selected_program,
1731 p_sys->i_pts * 9 / 100 );
1735 BlockDecode( p_input, block, i_pts, i_block_duration );
1740 if( i_start_pts == -1 )
1742 i_start_pts = p_sys->i_pts;
1744 else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1753 /*****************************************************************************
1755 *****************************************************************************/
1756 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1762 uint32_t vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1764 if( i_size <= 0 || mb_eof )
1769 return stream_Read( s, p_buffer, i_size );
1771 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1777 case seek_beginning:
1781 i_pos = stream_Size( s ) - i_offset;
1784 i_pos= stream_Tell( s ) + i_offset;
1788 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1795 if( stream_Seek( s, i_pos ) )
1801 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1805 uint64_t vlc_stream_io_callback::getFilePointer( void )
1807 return stream_Tell( s );
1809 void vlc_stream_io_callback::close( void )
1815 /*****************************************************************************
1816 * Ebml Stream parser
1817 *****************************************************************************/
1818 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1826 for( i = 1; i < 6; i++ )
1832 mb_keep = VLC_FALSE;
1835 EbmlParser::~EbmlParser( void )
1839 for( i = 1; i < mi_level; i++ )
1845 mb_keep = VLC_FALSE;
1849 void EbmlParser::Up( void )
1851 if( mi_user_level == mi_level )
1853 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1859 void EbmlParser::Down( void )
1865 void EbmlParser::Keep( void )
1870 int EbmlParser::GetLevel( void )
1872 return mi_user_level;
1875 EbmlElement *EbmlParser::Get( void )
1879 if( mi_user_level != mi_level )
1885 EbmlElement *ret = m_got;
1891 if( m_el[mi_level] )
1893 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
1896 delete m_el[mi_level];
1898 mb_keep = VLC_FALSE;
1901 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
1912 delete m_el[mi_level - 1];
1913 m_got = m_el[mi_level -1] = m_el[mi_level];
1914 m_el[mi_level] = NULL;
1921 else if( m_el[mi_level] == NULL )
1923 fprintf( stderr," m_el[mi_level] == NULL\n" );
1926 return m_el[mi_level];
1930 /*****************************************************************************
1932 * * LoadCues : load the cues element and update index
1934 * * LoadTags : load ... the tags element
1936 * * InformationsCreate : create all informations, load tags if present
1938 *****************************************************************************/
1939 static void LoadCues( input_thread_t *p_input )
1941 demux_sys_t *p_sys = p_input->p_demux_data;
1942 int64_t i_sav_position = p_sys->in->getFilePointer();
1944 EbmlElement *el, *cues;
1946 msg_Dbg( p_input, "loading cues" );
1947 p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
1948 cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
1952 msg_Err( p_input, "cannot load cues (broken seekhead or file)" );
1956 ep = new EbmlParser( p_sys->es, cues );
1957 while( ( el = ep->Get() ) != NULL )
1959 if( EbmlId( *el ) == KaxCuePoint::ClassInfos.GlobalId )
1961 #define idx p_sys->index[p_sys->i_index]
1964 idx.i_block_number= -1;
1965 idx.i_position = -1;
1967 idx.b_key = VLC_TRUE;
1970 while( ( el = ep->Get() ) != NULL )
1972 if( EbmlId( *el ) == KaxCueTime::ClassInfos.GlobalId )
1974 KaxCueTime &ctime = *(KaxCueTime*)el;
1976 ctime.ReadData( p_sys->es->I_O() );
1978 idx.i_time = uint64( ctime ) * (mtime_t)1000000000 / p_sys->i_timescale;
1980 else if( EbmlId( *el ) == KaxCueTrackPositions::ClassInfos.GlobalId )
1983 while( ( el = ep->Get() ) != NULL )
1985 if( EbmlId( *el ) == KaxCueTrack::ClassInfos.GlobalId )
1987 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
1989 ctrack.ReadData( p_sys->es->I_O() );
1990 idx.i_track = uint16( ctrack );
1992 else if( EbmlId( *el ) == KaxCueClusterPosition::ClassInfos.GlobalId )
1994 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
1996 ccpos.ReadData( p_sys->es->I_O() );
1997 idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
1999 else if( EbmlId( *el ) == KaxCueBlockNumber::ClassInfos.GlobalId )
2001 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2003 cbnum.ReadData( p_sys->es->I_O() );
2004 idx.i_block_number = uint32( cbnum );
2008 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2015 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2021 msg_Dbg( p_input, " * added time="I64Fd" pos="I64Fd
2022 " track=%d bnum=%d", idx.i_time, idx.i_position,
2023 idx.i_track, idx.i_block_number );
2027 if( p_sys->i_index >= p_sys->i_index_max )
2029 p_sys->i_index_max += 1024;
2030 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2036 msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
2042 p_sys->b_cues = VLC_TRUE;
2044 msg_Dbg( p_input, "loading cues done." );
2045 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2048 static void LoadTags( input_thread_t *p_input )
2050 demux_sys_t *p_sys = p_input->p_demux_data;
2051 int64_t i_sav_position = p_sys->in->getFilePointer();
2053 EbmlElement *el, *tags;
2055 msg_Dbg( p_input, "loading tags" );
2056 p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
2057 tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2061 msg_Err( p_input, "cannot load tags (broken seekhead or file)" );
2065 msg_Dbg( p_input, "Tags" );
2066 ep = new EbmlParser( p_sys->es, tags );
2067 while( ( el = ep->Get() ) != NULL )
2069 if( EbmlId( *el ) == KaxTag::ClassInfos.GlobalId )
2071 msg_Dbg( p_input, "+ Tag" );
2073 while( ( el = ep->Get() ) != NULL )
2075 if( EbmlId( *el ) == KaxTagTargets::ClassInfos.GlobalId )
2077 msg_Dbg( p_input, "| + Targets" );
2079 while( ( el = ep->Get() ) != NULL )
2081 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2085 else if( EbmlId( *el ) == KaxTagGeneral::ClassInfos.GlobalId )
2087 msg_Dbg( p_input, "| + General" );
2089 while( ( el = ep->Get() ) != NULL )
2091 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2095 else if( EbmlId( *el ) == KaxTagGenres::ClassInfos.GlobalId )
2097 msg_Dbg( p_input, "| + Genres" );
2099 while( ( el = ep->Get() ) != NULL )
2101 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2105 else if( EbmlId( *el ) == KaxTagAudioSpecific::ClassInfos.GlobalId )
2107 msg_Dbg( p_input, "| + Audio Specific" );
2109 while( ( el = ep->Get() ) != NULL )
2111 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2115 else if( EbmlId( *el ) == KaxTagImageSpecific::ClassInfos.GlobalId )
2117 msg_Dbg( p_input, "| + Images Specific" );
2119 while( ( el = ep->Get() ) != NULL )
2121 msg_Dbg( p_input, "| | + Unknown (%s)", typeid( *el ).name() );
2125 else if( EbmlId( *el ) == KaxTagMultiComment::ClassInfos.GlobalId )
2127 msg_Dbg( p_input, "| + Multi Comment" );
2129 else if( EbmlId( *el ) == KaxTagMultiCommercial::ClassInfos.GlobalId )
2131 msg_Dbg( p_input, "| + Multi Commercial" );
2133 else if( EbmlId( *el ) == KaxTagMultiDate::ClassInfos.GlobalId )
2135 msg_Dbg( p_input, "| + Multi Date" );
2137 else if( EbmlId( *el ) == KaxTagMultiEntity::ClassInfos.GlobalId )
2139 msg_Dbg( p_input, "| + Multi Entity" );
2141 else if( EbmlId( *el ) == KaxTagMultiIdentifier::ClassInfos.GlobalId )
2143 msg_Dbg( p_input, "| + Multi Identifier" );
2145 else if( EbmlId( *el ) == KaxTagMultiLegal::ClassInfos.GlobalId )
2147 msg_Dbg( p_input, "| + Multi Legal" );
2149 else if( EbmlId( *el ) == KaxTagMultiTitle::ClassInfos.GlobalId )
2151 msg_Dbg( p_input, "| + Multi Title" );
2155 msg_Dbg( p_input, "| + Unknown (%s)", typeid( *el ).name() );
2162 msg_Dbg( p_input, "+ Unknown (%s)", typeid( *el ).name() );
2168 msg_Dbg( p_input, "loading tags done." );
2169 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2172 static void InformationsCreate( input_thread_t *p_input )
2174 demux_sys_t *p_sys = p_input->p_demux_data;
2175 input_info_category_t *p_cat;
2176 playlist_t *p_playlist;
2179 p_cat = input_InfoCategory( p_input, "Matroska" );
2180 if( p_sys->f_duration > 1000.1 )
2182 char psz_buffer[MSTRTIME_MAX_SIZE];
2183 input_AddInfo( p_cat, _("Duration"),
2184 msecstotimestr( psz_buffer, (int)p_sys->f_duration ) );
2187 if( p_sys->psz_title )
2189 input_AddInfo( p_cat, _("Title"), "%s" ,p_sys->psz_title );
2191 if( p_sys->psz_date_utc )
2193 input_AddInfo( p_cat, _("UTC date"), "%s" ,p_sys->psz_date_utc );
2195 if( p_sys->psz_segment_filename )
2197 input_AddInfo( p_cat, _("Segment filename"), "%s" ,p_sys->psz_segment_filename );
2199 if( p_sys->psz_muxing_application )
2201 input_AddInfo( p_cat, _("Muxing application"), "%s" ,p_sys->psz_muxing_application );
2203 if( p_sys->psz_writing_application )
2205 input_AddInfo( p_cat, _("Writing application"), "%s" ,p_sys->psz_writing_application );
2207 input_AddInfo( p_cat, _("Number of streams"), "%d" , p_sys->i_track );
2209 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
2211 char psz_cat[strlen( "Stream " ) + 10];
2212 #define tk p_sys->track[i_track]
2214 sprintf( psz_cat, "Stream %d", i_track );
2215 p_cat = input_InfoCategory( p_input, psz_cat);
2216 if( tk.fmt.psz_description )
2218 input_AddInfo( p_cat, _("Name"), "%s", tk.fmt.psz_description );
2220 if( tk.fmt.psz_language )
2222 input_AddInfo( p_cat, _("Language"), "%s", tk.fmt.psz_language );
2224 if( tk.psz_codec_name )
2226 input_AddInfo( p_cat, _("Codec name"), "%s", tk.psz_codec_name );
2228 if( tk.psz_codec_settings )
2230 input_AddInfo( p_cat, _("Codec setting"), "%s", tk.psz_codec_settings );
2232 if( tk.psz_codec_info_url )
2234 input_AddInfo( p_cat, _("Codec info"), "%s", tk.psz_codec_info_url );
2236 if( tk.psz_codec_download_url )
2238 input_AddInfo( p_cat, _("Codec download"), "%s", tk.psz_codec_download_url );
2243 if( p_sys->i_tags_position >= 0 )
2245 vlc_bool_t b_seekable;
2247 stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
2250 LoadTags( p_input );
2256 /*****************************************************************************
2258 *****************************************************************************/
2260 static void IndexAppendCluster( input_thread_t *p_input, KaxCluster *cluster )
2262 demux_sys_t *p_sys = p_input->p_demux_data;
2264 #define idx p_sys->index[p_sys->i_index]
2266 idx.i_block_number= -1;
2267 idx.i_position = cluster->GetElementPosition();
2269 idx.b_key = VLC_TRUE;
2272 if( p_sys->i_index >= p_sys->i_index_max )
2274 p_sys->i_index_max += 1024;
2275 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2280 static char * UTF8ToStr( const UTFstring &u )
2289 p = dst = (char*)malloc( i_src + 1);