1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Steve Lhomme <steve.lhomme@free.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
33 # include <time.h> /* time() */
36 #include <vlc/input.h>
38 #include <codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
51 # include "../../src/extras/dirent.h"
54 /* libebml and matroska */
55 #include "ebml/EbmlHead.h"
56 #include "ebml/EbmlSubHead.h"
57 #include "ebml/EbmlStream.h"
58 #include "ebml/EbmlContexts.h"
59 #include "ebml/EbmlVoid.h"
60 #include "ebml/StdIOCallback.h"
62 #include "matroska/KaxAttachments.h"
63 #include "matroska/KaxBlock.h"
64 #include "matroska/KaxBlockData.h"
65 #include "matroska/KaxChapters.h"
66 #include "matroska/KaxCluster.h"
67 #include "matroska/KaxClusterData.h"
68 #include "matroska/KaxContexts.h"
69 #include "matroska/KaxCues.h"
70 #include "matroska/KaxCuesData.h"
71 #include "matroska/KaxInfo.h"
72 #include "matroska/KaxInfoData.h"
73 #include "matroska/KaxSeekHead.h"
74 #include "matroska/KaxSegment.h"
75 #include "matroska/KaxTag.h"
76 #include "matroska/KaxTags.h"
77 #include "matroska/KaxTagMulti.h"
78 #include "matroska/KaxTracks.h"
79 #include "matroska/KaxTrackAudio.h"
80 #include "matroska/KaxTrackVideo.h"
81 #include "matroska/KaxTrackEntryData.h"
82 #include "matroska/KaxContentEncoding.h"
84 #include "ebml/StdIOCallback.h"
87 #include "mp4/libmp4.h"
93 #define MATROSKA_COMPRESSION_NONE 0
94 #define MATROSKA_COMPRESSION_ZLIB 1
97 * What's between a directory and a filename?
100 #define DIRECTORY_SEPARATOR '\\'
102 #define DIRECTORY_SEPARATOR '/'
105 using namespace LIBMATROSKA_NAMESPACE;
108 /*****************************************************************************
110 *****************************************************************************/
111 static int Open ( vlc_object_t * );
112 static void Close( vlc_object_t * );
115 set_shortname( _("Matroska") );
116 set_description( _("Matroska stream demuxer" ) );
117 set_capability( "demux2", 50 );
118 set_callbacks( Open, Close );
119 set_category( CAT_INPUT );
120 set_subcategory( SUBCAT_INPUT_DEMUX );
122 add_bool( "mkv-seek-percent", 1, NULL,
123 N_("Seek based on percent not time"),
124 N_("Seek based on percent not time"), VLC_TRUE );
126 add_shortcut( "mka" );
127 add_shortcut( "mkv" );
130 /*****************************************************************************
132 *****************************************************************************/
133 static int Demux ( demux_t * );
134 static int Control( demux_t *, int, va_list );
135 static void Seek ( demux_t *, mtime_t i_date, int i_percent );
138 block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) {
139 int result, dstsize, n;
144 d_stream.zalloc = (alloc_func)0;
145 d_stream.zfree = (free_func)0;
146 d_stream.opaque = (voidpf)0;
147 result = inflateInit(&d_stream);
150 msg_Dbg( p_this, "inflateInit() failed. Result: %d", result );
154 d_stream.next_in = (Bytef *)p_in_block->p_buffer;
155 d_stream.avail_in = p_in_block->i_buffer;
157 p_block = block_New( p_this, 0 );
162 p_block = block_Realloc( p_block, 0, n * 1000 );
163 dst = (unsigned char *)p_block->p_buffer;
164 d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
165 d_stream.avail_out = 1000;
166 result = inflate(&d_stream, Z_NO_FLUSH);
167 if( ( result != Z_OK ) && ( result != Z_STREAM_END ) )
169 msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result );
173 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
174 ( result != Z_STREAM_END ) );
176 dstsize = d_stream.total_out;
177 inflateEnd( &d_stream );
179 p_block = block_Realloc( p_block, 0, dstsize );
180 p_block->i_buffer = dstsize;
181 block_Release( p_in_block );
188 * Helper function to print the mkv parse tree
190 static void MkvTree( demux_t *p_this, int i_level, char *psz_format, ... )
195 msg_Err( p_this, "too deep tree" );
198 va_start( args, psz_format );
199 static char *psz_foo = "| | | | | | | | | |";
200 char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) );
201 strncpy( psz_foo2, psz_foo, 4 * i_level );
202 psz_foo2[ 4 * i_level ] = '+';
203 psz_foo2[ 4 * i_level + 1 ] = ' ';
204 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
205 __msg_GenericVa( VLC_OBJECT(p_this), VLC_MSG_DBG, "mkv", psz_foo2, args );
210 /*****************************************************************************
212 *****************************************************************************/
213 class vlc_stream_io_callback: public IOCallback
220 vlc_stream_io_callback( stream_t * );
222 virtual uint32 read ( void *p_buffer, size_t i_size);
223 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
224 virtual size_t write ( const void *p_buffer, size_t i_size);
225 virtual uint64 getFilePointer ( void );
226 virtual void close ( void );
229 /*****************************************************************************
231 *****************************************************************************/
235 EbmlParser( EbmlStream *es, EbmlElement *el_start );
240 EbmlElement *Get( void );
243 int GetLevel( void );
248 EbmlElement *m_el[10];
257 /*****************************************************************************
258 * Some functions to manipulate memory
259 *****************************************************************************/
260 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
261 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
263 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
266 /*****************************************************************************
267 * definitions of structures and functions used by this plugins
268 *****************************************************************************/
271 vlc_bool_t b_default;
272 vlc_bool_t b_enabled;
276 uint8_t *p_extra_data;
280 uint64_t i_default_duration;
281 float f_timecodescale;
289 /* data to be send first */
291 uint8_t *p_data_init;
293 /* hack : it's for seek */
294 vlc_bool_t b_search_keyframe;
297 char *psz_codec_name;
298 char *psz_codec_settings;
299 char *psz_codec_info_url;
300 char *psz_codec_download_url;
302 /* encryption/compression */
303 int i_compression_type;
330 ,i_chapters_position(0)
340 ,psz_muxing_application(NULL)
341 ,psz_writing_application(NULL)
342 ,psz_segment_filename(NULL)
349 vlc_stream_io_callback *in;
354 uint64_t i_timescale;
356 /* duration of the segment */
364 int64_t i_cues_position;
365 int64_t i_chapters_position;
366 int64_t i_tags_position;
371 KaxSegmentUID segment_uid;
382 char *psz_muxing_application;
383 char *psz_writing_application;
384 char *psz_segment_filename;
390 input_title_t *title;
392 std::vector<KaxSegmentFamily> families;
393 std::vector<KaxSegment*> family_members;
396 bool edition_ordered;
399 #define MKVD_TIMECODESCALE 1000000
401 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
403 static void IndexAppendCluster ( demux_t *p_demux, KaxCluster *cluster );
404 static char *UTF8ToStr ( const UTFstring &u );
405 static void LoadCues ( demux_t * );
406 static void InformationCreate ( demux_t * );
408 static void ParseInfo( demux_t *, EbmlElement *info );
409 static void ParseTracks( demux_t *, EbmlElement *tracks );
410 static void ParseSeekHead( demux_t *, EbmlElement *seekhead );
411 static void ParseChapters( demux_t *, EbmlElement *chapters );
413 /*****************************************************************************
414 * Open: initializes matroska demux structures
415 *****************************************************************************/
416 static int Open( vlc_object_t * p_this )
418 demux_t *p_demux = (demux_t*)p_this;
421 std::string s_path, s_filename;
427 EbmlElement *el = NULL, *el1 = NULL;
429 /* peek the begining */
430 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
432 msg_Warn( p_demux, "cannot peek" );
436 /* is a valid file */
437 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
438 p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
440 msg_Warn( p_demux, "matroska module discarded "
441 "(invalid header 0x%.2x%.2x%.2x%.2x)",
442 p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
446 /* Set the demux function */
447 p_demux->pf_demux = Demux;
448 p_demux->pf_control = Control;
449 p_demux->p_sys = p_sys = new demux_sys_t;
451 p_sys->in = new vlc_stream_io_callback( p_demux->s );
452 p_sys->es = new EbmlStream( *p_sys->in );
453 p_sys->f_duration = -1;
454 p_sys->i_timescale = MKVD_TIMECODESCALE;
456 p_sys->track = (mkv_track_t*)malloc( sizeof( mkv_track_t ) );
458 p_sys->i_cues_position = -1;
459 p_sys->i_chapters_position = -1;
460 p_sys->i_tags_position = -1;
462 p_sys->b_cues = VLC_FALSE;
464 p_sys->i_index_max = 1024;
465 p_sys->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
466 p_sys->i_index_max );
468 p_sys->psz_muxing_application = NULL;
469 p_sys->psz_writing_application = NULL;
470 p_sys->psz_segment_filename = NULL;
471 p_sys->psz_title = NULL;
472 p_sys->psz_date_utc = NULL;;
476 if( p_sys->es == NULL )
478 msg_Err( p_demux, "failed to create EbmlStream" );
483 /* Find the EbmlHead element */
484 el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
487 msg_Err( p_demux, "cannot find EbmlHead" );
490 msg_Dbg( p_demux, "EbmlHead" );
492 el->SkipData( *p_sys->es, el->Generic().Context );
496 el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
499 msg_Err( p_demux, "cannot find KaxSegment" );
502 MkvTree( p_demux, 0, "Segment" );
503 p_sys->segment = (KaxSegment*)el;
504 p_sys->cluster = NULL;
506 p_sys->ep = new EbmlParser( p_sys->es, el );
508 while( ( el1 = p_sys->ep->Get() ) != NULL )
510 if( MKV_IS_ID( el1, KaxInfo ) )
512 ParseInfo( p_demux, el1 );
514 else if( MKV_IS_ID( el1, KaxTracks ) )
516 ParseTracks( p_demux, el1 );
518 else if( MKV_IS_ID( el1, KaxSeekHead ) )
520 ParseSeekHead( p_demux, el1 );
522 else if( MKV_IS_ID( el1, KaxCues ) )
524 msg_Dbg( p_demux, "| + Cues" );
526 else if( MKV_IS_ID( el1, KaxCluster ) )
528 msg_Dbg( p_demux, "| + Cluster" );
530 p_sys->cluster = (KaxCluster*)el1;
533 /* stop parsing the stream */
536 else if( MKV_IS_ID( el1, KaxAttachments ) )
538 msg_Dbg( p_demux, "| + Attachments FIXME TODO (but probably never supported)" );
540 else if( MKV_IS_ID( el1, KaxChapters ) )
542 msg_Dbg( p_demux, "| + Chapters" );
543 ParseChapters( p_demux, el1 );
545 else if( MKV_IS_ID( el1, KaxTag ) )
547 msg_Dbg( p_demux, "| + Tags FIXME TODO" );
551 msg_Dbg( p_demux, "| + Unknown (%s)", typeid(*el1).name() );
555 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
556 /* _todo_ handle multi-segment files */
557 if (p_demux->psz_path[0] != '\0' && (!strcmp(p_demux->psz_access, "") || !strcmp(p_demux->psz_access, "")))
559 // assume it's a regular file
560 // get the directory path
561 s_path = p_demux->psz_path;
562 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
564 s_path = s_path.substr(0,s_path.length()-1);
568 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
570 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
574 struct dirent *p_file_item;
575 DIR *p_src_dir = opendir(s_path.c_str());
577 if (p_src_dir != NULL)
579 while ((p_file_item = readdir(p_src_dir)))
581 if (strlen(p_file_item->d_name) > 4)
583 s_filename = s_path + DIRECTORY_SEPARATOR + p_file_item->d_name;
585 if (!s_filename.compare(p_demux->psz_path))
588 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
589 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
591 // test wether this file belongs to the our family
592 bool b_keep_file_opened = false;
593 StdIOCallback *p_file_io = new StdIOCallback(s_filename.c_str(), MODE_READ);
594 EbmlStream *p_stream = new EbmlStream(*p_file_io);
595 EbmlElement *p_l0, *p_l1, *p_l2;
597 // verify the EBML Header
598 p_l0 = p_stream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
606 p_l0->SkipData(*p_stream, EbmlHead_Context);
609 // find all segments in this file
610 p_l0 = p_stream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
622 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
625 KaxSegmentUID *p_uid = NULL;
627 ep = new EbmlParser(p_stream, p_l0);
628 bool b_this_segment_matches = false;
629 while ((p_l1 = ep->Get()))
631 if (MKV_IS_ID(p_l1, KaxInfo))
633 // find the families of this segment
634 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
636 p_info->Read(*p_stream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
637 for( i = 0; i < p_info->ListSize() && !b_this_segment_matches; i++ )
639 EbmlElement *l = (*p_info)[i];
641 if( MKV_IS_ID( l, KaxSegmentUID ) )
643 p_uid = static_cast<KaxSegmentUID*>(l);
644 if (p_sys->segment_uid == *p_uid)
647 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
649 KaxSegmentFamily *p_fam = static_cast<KaxSegmentFamily*>(l);
650 for (j=0; j<p_sys->families.size(); j++)
652 if (p_sys->families.at(j) == *p_fam)
654 b_this_segment_matches = true;
664 if (b_this_segment_matches)
666 b_keep_file_opened = true;
670 p_l0->SkipData(*p_stream, EbmlHead_Context);
672 p_l0 = p_stream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
675 if (!b_keep_file_opened)
683 closedir( p_src_dir );
688 if( p_sys->cluster == NULL )
690 msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
694 /* *** Load the cue if found *** */
695 if( p_sys->i_cues_position >= 0 )
697 vlc_bool_t b_seekable;
699 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
706 if( !p_sys->b_cues || p_sys->i_index <= 0 )
708 msg_Warn( p_demux, "no cues/empty cues found->seek won't be precise" );
710 IndexAppendCluster( p_demux, p_sys->cluster );
712 p_sys->b_cues = VLC_FALSE;
716 msg_Dbg( p_demux, "found %d es", p_sys->i_track );
717 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
719 #define tk p_sys->track[i_track]
720 if( tk.fmt.i_cat == UNKNOWN_ES )
722 msg_Warn( p_demux, "invalid track[%d, n=%d]", i_track, tk.i_number );
727 if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) )
729 if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
731 msg_Err( p_demux, "missing/invalid BITMAPINFOHEADER" );
732 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
736 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
738 tk.fmt.video.i_width = GetDWLE( &p_bih->biWidth );
739 tk.fmt.video.i_height= GetDWLE( &p_bih->biHeight );
740 tk.fmt.i_codec = GetFOURCC( &p_bih->biCompression );
742 tk.fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
743 if( tk.fmt.i_extra > 0 )
745 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
746 memcpy( tk.fmt.p_extra, &p_bih[1], tk.fmt.i_extra );
750 else if( !strcmp( tk.psz_codec, "V_MPEG1" ) ||
751 !strcmp( tk.psz_codec, "V_MPEG2" ) )
753 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
755 else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) )
757 if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) )
759 tk.fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
761 else if( !strcmp( tk.psz_codec, "V_MPEG4/ISO/AVC" ) )
763 tk.fmt.i_codec = VLC_FOURCC( 'h', '2', '6', '4' );
764 tk.fmt.b_packetized = VLC_FALSE;
765 tk.fmt.i_extra = tk.i_extra_data;
766 tk.fmt.p_extra = malloc( tk.i_extra_data );
767 memcpy( tk.fmt.p_extra,tk.p_extra_data, tk.i_extra_data );
771 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
774 else if( !strcmp( tk.psz_codec, "V_QUICKTIME" ) )
776 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
777 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(p_demux),
780 MP4_ReadBoxCommon( p_mp4_stream, p_box );
781 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
782 tk.fmt.i_codec = p_box->i_type;
783 tk.fmt.video.i_width = p_box->data.p_sample_vide->i_width;
784 tk.fmt.video.i_height = p_box->data.p_sample_vide->i_height;
785 tk.fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
786 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
787 memcpy( tk.fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tk.fmt.i_extra );
788 MP4_FreeBox_sample_vide( p_box );
789 stream_MemoryDelete( p_mp4_stream, VLC_TRUE );
791 else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
793 if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
795 msg_Err( p_demux, "missing/invalid WAVEFORMATEX" );
796 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
800 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;
802 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk.fmt.i_codec, NULL );
804 tk.fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
805 tk.fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
806 tk.fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
807 tk.fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
808 tk.fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
810 tk.fmt.i_extra = GetWLE( &p_wf->cbSize );
811 if( tk.fmt.i_extra > 0 )
813 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
814 memcpy( tk.fmt.p_extra, &p_wf[1], tk.fmt.i_extra );
818 else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
819 !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
820 !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
822 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
824 else if( !strcmp( tk.psz_codec, "A_AC3" ) )
826 tk.fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
828 else if( !strcmp( tk.psz_codec, "A_DTS" ) )
830 tk.fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
832 else if( !strcmp( tk.psz_codec, "A_FLAC" ) )
834 tk.fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
835 tk.fmt.i_extra = tk.i_extra_data;
836 tk.fmt.p_extra = malloc( tk.i_extra_data );
837 memcpy( tk.fmt.p_extra,tk.p_extra_data, tk.i_extra_data );
839 else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
841 int i, i_offset = 1, i_size[3], i_extra;
844 tk.fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
846 /* Split the 3 headers */
847 if( tk.p_extra_data[0] != 0x02 )
848 msg_Err( p_demux, "invalid vorbis header" );
850 for( i = 0; i < 2; i++ )
853 while( i_offset < tk.i_extra_data )
855 i_size[i] += tk.p_extra_data[i_offset];
856 if( tk.p_extra_data[i_offset++] != 0xff ) break;
860 i_size[0] = __MIN(i_size[0], tk.i_extra_data - i_offset);
861 i_size[1] = __MIN(i_size[1], tk.i_extra_data -i_offset -i_size[0]);
862 i_size[2] = tk.i_extra_data - i_offset - i_size[0] - i_size[1];
864 tk.fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
865 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
866 p_extra = (uint8_t *)tk.fmt.p_extra; i_extra = 0;
867 for( i = 0; i < 3; i++ )
869 *(p_extra++) = i_size[i] >> 8;
870 *(p_extra++) = i_size[i] & 0xFF;
871 memcpy( p_extra, tk.p_extra_data + i_offset + i_extra,
873 p_extra += i_size[i];
874 i_extra += i_size[i];
877 else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
878 !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
880 int i_profile, i_srate;
881 static unsigned int i_sample_rates[] =
883 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
884 16000, 12000, 11025, 8000, 7350, 0, 0, 0
887 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
888 /* create data for faad (MP4DecSpecificDescrTag)*/
890 if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
894 else if( !strcmp( &tk.psz_codec[12], "LC" ) )
898 else if( !strcmp( &tk.psz_codec[12], "SSR" ) )
907 for( i_srate = 0; i_srate < 13; i_srate++ )
909 if( i_sample_rates[i_srate] == tk.fmt.audio.i_rate )
914 msg_Dbg( p_demux, "profile=%d srate=%d", i_profile, i_srate );
917 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
918 ((uint8_t*)tk.fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
919 ((uint8_t*)tk.fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk.fmt.audio.i_channels << 3);
921 else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
922 !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
923 !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
925 if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
927 tk.fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
931 tk.fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
933 tk.fmt.audio.i_blockalign = ( tk.fmt.audio.i_bitspersample + 7 ) / 8 * tk.fmt.audio.i_channels;
935 else if( !strcmp( tk.psz_codec, "A_TTA1" ) )
937 /* FIXME: support this codec */
938 msg_Err( p_demux, "TTA not supported yet[%d, n=%d]", i_track, tk.i_number );
939 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
941 else if( !strcmp( tk.psz_codec, "A_WAVPACK4" ) )
943 /* FIXME: support this codec */
944 msg_Err( p_demux, "Wavpack not supported yet[%d, n=%d]", i_track, tk.i_number );
945 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
947 else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
949 tk.fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
950 tk.fmt.subs.psz_encoding = strdup( "UTF-8" );
952 else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) ||
953 !strcmp( tk.psz_codec, "S_TEXT/ASS" ) ||
954 !strcmp( tk.psz_codec, "S_SSA" ) ||
955 !strcmp( tk.psz_codec, "S_ASS" ))
957 tk.fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
958 tk.fmt.subs.psz_encoding = strdup( "UTF-8" );
960 else if( !strcmp( tk.psz_codec, "S_VOBSUB" ) )
962 tk.fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
963 if( tk.i_extra_data )
966 char *p_buf = (char *)malloc( tk.i_extra_data + 1);
967 memcpy( p_buf, tk.p_extra_data , tk.i_extra_data );
968 p_buf[tk.i_extra_data] = '\0';
970 p_start = strstr( p_buf, "size:" );
971 if( sscanf( p_start, "size: %dx%d",
972 &tk.fmt.subs.spu.i_original_frame_width, &tk.fmt.subs.spu.i_original_frame_height ) == 2 )
974 msg_Dbg( p_demux, "original frame size vobsubs: %dx%d", tk.fmt.subs.spu.i_original_frame_width, tk.fmt.subs.spu.i_original_frame_height );
978 msg_Warn( p_demux, "reading original frame size for vobsub failed" );
983 else if( !strcmp( tk.psz_codec, "B_VOBBTN" ) )
985 /* FIXME: support this codec */
986 msg_Err( p_demux, "Vob Buttons not supported yet[%d, n=%d]", i_track, tk.i_number );
987 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
991 msg_Err( p_demux, "unknow codec id=`%s'", tk.psz_codec );
992 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
996 tk.fmt.i_priority = 1000;
999 tk.p_es = es_out_Add( p_demux->out, &tk.fmt );
1003 /* add information */
1004 InformationCreate( p_demux );
1012 return VLC_EGENERIC;
1015 /*****************************************************************************
1016 * Close: frees unused data
1017 *****************************************************************************/
1018 static void Close( vlc_object_t *p_this )
1020 demux_t *p_demux = (demux_t*)p_this;
1021 demux_sys_t *p_sys = p_demux->p_sys;
1024 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1026 #define tk p_sys->track[i_track]
1027 if( tk.fmt.psz_description )
1029 free( tk.fmt.psz_description );
1033 free( tk.psz_codec );
1035 if( tk.fmt.psz_language )
1037 free( tk.fmt.psz_language );
1041 free( p_sys->track );
1043 if( p_sys->psz_writing_application )
1045 free( p_sys->psz_writing_application );
1047 if( p_sys->psz_muxing_application )
1049 free( p_sys->psz_muxing_application );
1052 delete p_sys->segment;
1060 /*****************************************************************************
1062 *****************************************************************************/
1063 static int Control( demux_t *p_demux, int i_query, va_list args )
1065 demux_sys_t *p_sys = p_demux->p_sys;
1070 vlc_meta_t **pp_meta;
1074 case DEMUX_GET_META:
1075 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1076 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1079 case DEMUX_GET_LENGTH:
1080 pi64 = (int64_t*)va_arg( args, int64_t * );
1081 if( p_sys->f_duration > 0.0 )
1083 *pi64 = (int64_t)(p_sys->f_duration * 1000);
1086 return VLC_EGENERIC;
1088 case DEMUX_GET_POSITION:
1089 pf = (double*)va_arg( args, double * );
1090 *pf = (double)p_sys->in->getFilePointer() / (double)stream_Size( p_demux->s );
1093 case DEMUX_SET_POSITION:
1094 f = (double)va_arg( args, double );
1095 Seek( p_demux, -1, (int)(100.0 * f) );
1098 case DEMUX_GET_TIME:
1099 pi64 = (int64_t*)va_arg( args, int64_t * );
1100 if( p_sys->f_duration > 0.0 )
1102 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
1105 *pi64 = (mtime_t)1000000 *
1106 (mtime_t)i_duration*
1107 (mtime_t)p_sys->in->getFilePointer() /
1108 (mtime_t)stream_Size( p_demux->s );
1111 return VLC_EGENERIC;
1113 case DEMUX_GET_TITLE_INFO:
1114 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
1116 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1117 int *pi_int = (int*)va_arg( args, int* );
1120 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) );
1122 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->title );
1126 return VLC_EGENERIC;
1128 case DEMUX_SET_TITLE:
1129 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
1133 return VLC_EGENERIC;
1135 case DEMUX_SET_SEEKPOINT:
1136 /* FIXME do a better implementation */
1137 i_skp = (int)va_arg( args, int );
1139 if( p_sys->title && i_skp < p_sys->title->i_seekpoint)
1141 Seek( p_demux, (int64_t)p_sys->title->seekpoint[i_skp]->i_time_offset, -1);
1142 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
1143 p_demux->info.i_seekpoint = i_skp;
1146 return VLC_EGENERIC;
1149 case DEMUX_SET_TIME:
1152 return VLC_EGENERIC;
1156 static int BlockGet( demux_t *p_demux, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1158 demux_sys_t *p_sys = p_demux->p_sys;
1169 if( p_demux->b_die )
1171 return VLC_EGENERIC;
1174 el = p_sys->ep->Get();
1175 i_level = p_sys->ep->GetLevel();
1177 if( el == NULL && *pp_block != NULL )
1179 /* update the index */
1180 #define idx p_sys->index[p_sys->i_index - 1]
1181 if( p_sys->i_index > 0 && idx.i_time == -1 )
1183 idx.i_time = (*pp_block)->GlobalTimecode() / (mtime_t)1000;
1184 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1192 if( p_sys->ep->GetLevel() > 1 )
1197 msg_Warn( p_demux, "EOF" );
1198 return VLC_EGENERIC;
1204 if( MKV_IS_ID( el, KaxCluster ) )
1206 p_sys->cluster = (KaxCluster*)el;
1208 /* add it to the index */
1209 if( p_sys->i_index == 0 ||
1210 ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
1212 IndexAppendCluster( p_demux, p_sys->cluster );
1217 else if( MKV_IS_ID( el, KaxCues ) )
1219 msg_Warn( p_demux, "find KaxCues FIXME" );
1220 return VLC_EGENERIC;
1224 msg_Dbg( p_demux, "unknown (%s)", typeid( el ).name() );
1227 else if( i_level == 2 )
1229 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1231 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1233 ctc.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
1234 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
1236 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1241 else if( i_level == 3 )
1243 if( MKV_IS_ID( el, KaxBlock ) )
1245 *pp_block = (KaxBlock*)el;
1247 (*pp_block)->ReadData( p_sys->es->I_O() );
1248 (*pp_block)->SetParent( *p_sys->cluster );
1252 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1254 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1256 dur.ReadData( p_sys->es->I_O() );
1257 *pi_duration = uint64( dur );
1259 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1261 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1263 ref.ReadData( p_sys->es->I_O() );
1264 if( *pi_ref1 == -1 )
1266 *pi_ref1 = int64( ref );
1270 *pi_ref2 = int64( ref );
1276 msg_Err( p_demux, "invalid level = %d", i_level );
1277 return VLC_EGENERIC;
1282 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1285 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1286 memcpy( p_block->p_buffer, p_mem, i_mem );
1287 //p_block->i_rate = p_input->stream.control.i_rate;
1291 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1292 mtime_t i_duration )
1294 demux_sys_t *p_sys = p_demux->p_sys;
1300 #define tk p_sys->track[i_track]
1301 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1303 if( tk.i_number == block->TrackNum() )
1309 if( i_track >= p_sys->i_track )
1311 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1314 if( tk.p_es == NULL )
1316 msg_Err( p_demux, "unknown track number=%d", block->TrackNum() );
1320 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk.p_es, &b );
1323 tk.b_inited = VLC_FALSE;
1327 /* First send init data */
1328 if( !tk.b_inited && tk.i_data_init > 0 )
1332 msg_Dbg( p_demux, "sending header (%d bytes)", tk.i_data_init );
1333 p_init = MemToBlock( p_demux, tk.p_data_init, tk.i_data_init );
1334 if( p_init ) es_out_Send( p_demux->out, tk.p_es, p_init );
1336 tk.b_inited = VLC_TRUE;
1339 for( i = 0; i < block->NumberFrames(); i++ )
1342 DataBuffer &data = block->GetBuffer(i);
1344 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1346 if( p_block == NULL )
1351 #if defined(HAVE_ZLIB_H)
1352 if( tk.i_compression_type )
1354 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
1358 if (i_pts < p_sys->i_start_pts)
1360 p_block->i_pts = -1;
1361 p_block->i_dts = -1;
1362 /* p_block->i_flags |= BLOCK_FLAG_DISCONTINUITY;*/
1364 else if( tk.fmt.i_cat != VIDEO_ES )
1366 p_block->i_dts = p_block->i_pts = i_pts;
1370 p_block->i_dts = i_pts;
1374 if( tk.fmt.i_cat == SPU_ES && strcmp( tk.psz_codec, "S_VOBSUB" ) )
1376 p_block->i_length = i_duration * 1000;
1378 es_out_Send( p_demux->out, tk.p_es, p_block );
1380 /* use time stamp only for first block */
1387 static void Seek( demux_t *p_demux, mtime_t i_date, int i_percent)
1389 demux_sys_t *p_sys = p_demux->p_sys;
1392 int64_t i_block_duration;
1393 int64_t i_block_ref1;
1394 int64_t i_block_ref2;
1397 int i_track_skipping;
1400 msg_Dbg( p_demux, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1401 if( i_date < 0 && i_percent < 0 )
1405 if( i_percent > 100 ) i_percent = 100;
1408 p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
1409 p_sys->cluster = NULL;
1411 /* seek without index or without date */
1412 if( i_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 ))
1414 int64_t i_pos = i_percent * stream_Size( p_demux->s ) / 100;
1416 msg_Dbg( p_demux, "inacurate way of seeking" );
1417 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1419 if( p_sys->index[i_index].i_position >= i_pos)
1424 if( i_index == p_sys->i_index )
1429 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1432 if( p_sys->index[i_index].i_position < i_pos )
1436 msg_Warn( p_demux, "searching for cluster, could take some time" );
1438 /* search a cluster */
1439 while( ( el = p_sys->ep->Get() ) != NULL )
1441 if( MKV_IS_ID( el, KaxCluster ) )
1443 KaxCluster *cluster = (KaxCluster*)el;
1445 /* add it to the index */
1446 IndexAppendCluster( p_demux, cluster );
1448 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1450 p_sys->cluster = cluster;
1460 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1462 if( p_sys->index[i_index].i_time >= i_date )
1473 msg_Dbg( p_demux, "seek got "I64Fd" (%d%%)",
1474 p_sys->index[i_index].i_time,
1475 (int)( 100 * p_sys->index[i_index].i_position /
1476 stream_Size( p_demux->s ) ) );
1478 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1482 /* now parse until key frame */
1483 #define tk p_sys->track[i_track]
1484 i_track_skipping = 0;
1485 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1487 if( tk.fmt.i_cat == VIDEO_ES )
1489 tk.b_search_keyframe = VLC_TRUE;
1494 p_sys->i_start_pts = i_date;
1496 while( i_track_skipping > 0 )
1498 if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1500 msg_Warn( p_demux, "cannot get block EOF?" );
1505 p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000;
1507 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1509 if( tk.i_number == block->TrackNum() )
1515 if( i_track < p_sys->i_track )
1517 if( tk.fmt.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1519 tk.b_search_keyframe = VLC_FALSE;
1522 if( tk.fmt.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1524 BlockDecode( p_demux, block, 0, 0 );
1533 /*****************************************************************************
1534 * Demux: reads and demuxes data packets
1535 *****************************************************************************
1536 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1537 *****************************************************************************/
1538 static int Demux( demux_t *p_demux)
1540 demux_sys_t *p_sys = p_demux->p_sys;
1541 mtime_t i_start_pts;
1542 int i_block_count = 0;
1545 int64_t i_block_duration;
1546 int64_t i_block_ref1;
1547 int64_t i_block_ref2;
1553 if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1555 msg_Warn( p_demux, "cannot get block EOF?" );
1560 p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000;
1562 if( p_sys->i_pts > 0 )
1564 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
1567 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
1572 if( i_start_pts == -1 )
1574 i_start_pts = p_sys->i_pts;
1576 else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1585 /*****************************************************************************
1587 *****************************************************************************/
1588 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1594 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1596 if( i_size <= 0 || mb_eof )
1601 return stream_Read( s, p_buffer, i_size );
1603 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1609 case seek_beginning:
1613 i_pos = stream_Size( s ) - i_offset;
1616 i_pos= stream_Tell( s ) + i_offset;
1620 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1627 if( stream_Seek( s, i_pos ) )
1633 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1637 uint64 vlc_stream_io_callback::getFilePointer( void )
1639 return stream_Tell( s );
1641 void vlc_stream_io_callback::close( void )
1647 /*****************************************************************************
1648 * Ebml Stream parser
1649 *****************************************************************************/
1650 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1658 for( i = 1; i < 6; i++ )
1664 mb_keep = VLC_FALSE;
1667 EbmlParser::~EbmlParser( void )
1671 for( i = 1; i < mi_level; i++ )
1677 mb_keep = VLC_FALSE;
1681 void EbmlParser::Up( void )
1683 if( mi_user_level == mi_level )
1685 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1691 void EbmlParser::Down( void )
1697 void EbmlParser::Keep( void )
1702 int EbmlParser::GetLevel( void )
1704 return mi_user_level;
1707 EbmlElement *EbmlParser::Get( void )
1711 if( mi_user_level != mi_level )
1717 EbmlElement *ret = m_got;
1723 if( m_el[mi_level] )
1725 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
1728 delete m_el[mi_level];
1730 mb_keep = VLC_FALSE;
1733 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
1744 delete m_el[mi_level - 1];
1745 m_got = m_el[mi_level -1] = m_el[mi_level];
1746 m_el[mi_level] = NULL;
1753 else if( m_el[mi_level] == NULL )
1755 fprintf( stderr," m_el[mi_level] == NULL\n" );
1758 return m_el[mi_level];
1762 /*****************************************************************************
1764 * * LoadCues : load the cues element and update index
1766 * * LoadTags : load ... the tags element
1768 * * InformationCreate : create all information, load tags if present
1770 *****************************************************************************/
1771 static void LoadCues( demux_t *p_demux )
1773 demux_sys_t *p_sys = p_demux->p_sys;
1774 int64_t i_sav_position = p_sys->in->getFilePointer();
1776 EbmlElement *el, *cues;
1778 msg_Dbg( p_demux, "loading cues" );
1779 p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
1780 cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
1784 msg_Err( p_demux, "cannot load cues (broken seekhead or file)" );
1785 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1789 ep = new EbmlParser( p_sys->es, cues );
1790 while( ( el = ep->Get() ) != NULL )
1792 if( MKV_IS_ID( el, KaxCuePoint ) )
1794 #define idx p_sys->index[p_sys->i_index]
1797 idx.i_block_number= -1;
1798 idx.i_position = -1;
1800 idx.b_key = VLC_TRUE;
1803 while( ( el = ep->Get() ) != NULL )
1805 if( MKV_IS_ID( el, KaxCueTime ) )
1807 KaxCueTime &ctime = *(KaxCueTime*)el;
1809 ctime.ReadData( p_sys->es->I_O() );
1811 idx.i_time = uint64( ctime ) * p_sys->i_timescale / (mtime_t)1000;
1813 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
1816 while( ( el = ep->Get() ) != NULL )
1818 if( MKV_IS_ID( el, KaxCueTrack ) )
1820 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
1822 ctrack.ReadData( p_sys->es->I_O() );
1823 idx.i_track = uint16( ctrack );
1825 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
1827 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
1829 ccpos.ReadData( p_sys->es->I_O() );
1830 idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
1832 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
1834 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
1836 cbnum.ReadData( p_sys->es->I_O() );
1837 idx.i_block_number = uint32( cbnum );
1841 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1848 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1854 msg_Dbg( p_demux, " * added time="I64Fd" pos="I64Fd
1855 " track=%d bnum=%d", idx.i_time, idx.i_position,
1856 idx.i_track, idx.i_block_number );
1860 if( p_sys->i_index >= p_sys->i_index_max )
1862 p_sys->i_index_max += 1024;
1863 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
1869 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1875 p_sys->b_cues = VLC_TRUE;
1877 msg_Dbg( p_demux, "loading cues done." );
1878 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1881 static void LoadTags( demux_t *p_demux )
1883 demux_sys_t *p_sys = p_demux->p_sys;
1884 int64_t i_sav_position = p_sys->in->getFilePointer();
1886 EbmlElement *el, *tags;
1888 msg_Dbg( p_demux, "loading tags" );
1889 p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
1890 tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
1894 msg_Err( p_demux, "cannot load tags (broken seekhead or file)" );
1895 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1899 msg_Dbg( p_demux, "Tags" );
1900 ep = new EbmlParser( p_sys->es, tags );
1901 while( ( el = ep->Get() ) != NULL )
1903 if( MKV_IS_ID( el, KaxTag ) )
1905 msg_Dbg( p_demux, "+ Tag" );
1907 while( ( el = ep->Get() ) != NULL )
1909 if( MKV_IS_ID( el, KaxTagTargets ) )
1911 msg_Dbg( p_demux, "| + Targets" );
1913 while( ( el = ep->Get() ) != NULL )
1915 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1919 else if( MKV_IS_ID( el, KaxTagGeneral ) )
1921 msg_Dbg( p_demux, "| + General" );
1923 while( ( el = ep->Get() ) != NULL )
1925 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1929 else if( MKV_IS_ID( el, KaxTagGenres ) )
1931 msg_Dbg( p_demux, "| + Genres" );
1933 while( ( el = ep->Get() ) != NULL )
1935 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1939 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
1941 msg_Dbg( p_demux, "| + Audio Specific" );
1943 while( ( el = ep->Get() ) != NULL )
1945 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1949 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
1951 msg_Dbg( p_demux, "| + Images Specific" );
1953 while( ( el = ep->Get() ) != NULL )
1955 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1959 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
1961 msg_Dbg( p_demux, "| + Multi Comment" );
1963 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
1965 msg_Dbg( p_demux, "| + Multi Commercial" );
1967 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
1969 msg_Dbg( p_demux, "| + Multi Date" );
1971 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
1973 msg_Dbg( p_demux, "| + Multi Entity" );
1975 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
1977 msg_Dbg( p_demux, "| + Multi Identifier" );
1979 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
1981 msg_Dbg( p_demux, "| + Multi Legal" );
1983 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
1985 msg_Dbg( p_demux, "| + Multi Title" );
1989 msg_Dbg( p_demux, "| + Unknown (%s)", typeid( *el ).name() );
1996 msg_Dbg( p_demux, "+ Unknown (%s)", typeid( *el ).name() );
2002 msg_Dbg( p_demux, "loading tags done." );
2003 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
2006 /*****************************************************************************
2008 *****************************************************************************/
2009 static void ParseSeekHead( demux_t *p_demux, EbmlElement *seekhead )
2011 demux_sys_t *p_sys = p_demux->p_sys;
2015 int i_upper_level = 0;
2017 msg_Dbg( p_demux, "| + Seek head" );
2019 /* Master elements */
2020 m = static_cast<EbmlMaster *>(seekhead);
2021 m->Read( *p_sys->es, seekhead->Generic().Context, i_upper_level, el, true );
2023 for( i = 0; i < m->ListSize(); i++ )
2025 EbmlElement *l = (*m)[i];
2027 if( MKV_IS_ID( l, KaxSeek ) )
2029 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
2030 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
2035 for( j = 0; j < sk->ListSize(); j++ )
2037 EbmlElement *l = (*sk)[j];
2039 if( MKV_IS_ID( l, KaxSeekID ) )
2041 KaxSeekID &sid = *(KaxSeekID*)l;
2042 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
2044 else if( MKV_IS_ID( l, KaxSeekPosition ) )
2046 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
2047 i_pos = uint64( spos );
2051 msg_Dbg( p_demux, "| | | + Unknown (%s)", typeid(*l).name() );
2057 if( id == KaxCues::ClassInfos.GlobalId )
2059 msg_Dbg( p_demux, "| | | = cues at "I64Fd, i_pos );
2060 p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
2062 else if( id == KaxChapters::ClassInfos.GlobalId )
2064 msg_Dbg( p_demux, "| | | = chapters at "I64Fd, i_pos );
2065 p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
2067 else if( id == KaxTags::ClassInfos.GlobalId )
2069 msg_Dbg( p_demux, "| | | = tags at "I64Fd, i_pos );
2070 p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
2076 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2081 /*****************************************************************************
2083 *****************************************************************************/
2084 static void ParseTrackEntry( demux_t *p_demux, EbmlMaster *m )
2086 demux_sys_t *p_sys = p_demux->p_sys;
2091 msg_Dbg( p_demux, "| | + Track Entry" );
2094 p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
2096 /* Init the track */
2097 tk = &p_sys->track[p_sys->i_track - 1];
2099 memset( tk, 0, sizeof( mkv_track_t ) );
2101 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
2102 tk->fmt.psz_language = strdup("English");
2103 tk->fmt.psz_description = NULL;
2105 tk->b_default = VLC_TRUE;
2106 tk->b_enabled = VLC_TRUE;
2107 tk->i_number = p_sys->i_track - 1;
2108 tk->i_extra_data = 0;
2109 tk->p_extra_data = NULL;
2110 tk->psz_codec = NULL;
2111 tk->i_default_duration = 0;
2112 tk->f_timecodescale = 1.0;
2114 tk->b_inited = VLC_FALSE;
2115 tk->i_data_init = 0;
2116 tk->p_data_init = NULL;
2118 tk->psz_codec_name = NULL;
2119 tk->psz_codec_settings = NULL;
2120 tk->psz_codec_info_url = NULL;
2121 tk->psz_codec_download_url = NULL;
2123 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
2125 for( i = 0; i < m->ListSize(); i++ )
2127 EbmlElement *l = (*m)[i];
2129 if( MKV_IS_ID( l, KaxTrackNumber ) )
2131 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
2133 tk->i_number = uint32( tnum );
2134 msg_Dbg( p_demux, "| | | + Track Number=%u", uint32( tnum ) );
2136 else if( MKV_IS_ID( l, KaxTrackUID ) )
2138 KaxTrackUID &tuid = *(KaxTrackUID*)l;
2140 msg_Dbg( p_demux, "| | | + Track UID=%u", uint32( tuid ) );
2142 else if( MKV_IS_ID( l, KaxTrackType ) )
2145 KaxTrackType &ttype = *(KaxTrackType*)l;
2147 switch( uint8(ttype) )
2151 tk->fmt.i_cat = AUDIO_ES;
2155 tk->fmt.i_cat = VIDEO_ES;
2157 case track_subtitle:
2158 psz_type = "subtitle";
2159 tk->fmt.i_cat = SPU_ES;
2162 psz_type = "unknown";
2163 tk->fmt.i_cat = UNKNOWN_ES;
2167 msg_Dbg( p_demux, "| | | + Track Type=%s", psz_type );
2169 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
2171 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
2173 // tk->b_enabled = uint32( fenb );
2174 // msg_Dbg( p_demux, "| | | + Track Enabled=%u",
2175 // uint32( fenb ) );
2177 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
2179 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
2181 tk->b_default = uint32( fdef );
2182 msg_Dbg( p_demux, "| | | + Track Default=%u", uint32( fdef ) );
2184 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
2186 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
2188 msg_Dbg( p_demux, "| | | + Track Lacing=%d", uint32( lac ) );
2190 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
2192 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
2194 msg_Dbg( p_demux, "| | | + Track MinCache=%d", uint32( cmin ) );
2196 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
2198 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
2200 msg_Dbg( p_demux, "| | | + Track MaxCache=%d", uint32( cmax ) );
2202 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
2204 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
2206 tk->i_default_duration = uint64(defd);
2207 msg_Dbg( p_demux, "| | | + Track Default Duration="I64Fd, uint64(defd) );
2209 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
2211 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
2213 tk->f_timecodescale = float( ttcs );
2214 msg_Dbg( p_demux, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
2216 else if( MKV_IS_ID( l, KaxTrackName ) )
2218 KaxTrackName &tname = *(KaxTrackName*)l;
2220 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
2221 msg_Dbg( p_demux, "| | | + Track Name=%s", tk->fmt.psz_description );
2223 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
2225 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
2227 tk->fmt.psz_language = strdup( string( lang ).c_str() );
2229 "| | | + Track Language=`%s'", tk->fmt.psz_language );
2231 else if( MKV_IS_ID( l, KaxCodecID ) )
2233 KaxCodecID &codecid = *(KaxCodecID*)l;
2235 tk->psz_codec = strdup( string( codecid ).c_str() );
2236 msg_Dbg( p_demux, "| | | + Track CodecId=%s", string( codecid ).c_str() );
2238 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
2240 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
2242 tk->i_extra_data = cpriv.GetSize();
2243 if( tk->i_extra_data > 0 )
2245 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
2246 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
2248 msg_Dbg( p_demux, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
2250 else if( MKV_IS_ID( l, KaxCodecName ) )
2252 KaxCodecName &cname = *(KaxCodecName*)l;
2254 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
2255 msg_Dbg( p_demux, "| | | + Track Codec Name=%s", tk->psz_codec_name );
2257 else if( MKV_IS_ID( l, KaxContentEncodings ) )
2259 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
2260 MkvTree( p_demux, 3, "Content Encodings" );
2261 for( unsigned int i = 0; i < cencs->ListSize(); i++ )
2263 EbmlElement *l2 = (*cencs)[i];
2264 if( MKV_IS_ID( l2, KaxContentEncoding ) )
2266 MkvTree( p_demux, 4, "Content Encoding" );
2267 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
2268 for( unsigned int i = 0; i < cenc->ListSize(); i++ )
2270 EbmlElement *l3 = (*cenc)[i];
2271 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
2273 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
2274 MkvTree( p_demux, 5, "Order: %i", uint32( encord ) );
2276 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
2278 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
2279 MkvTree( p_demux, 5, "Scope: %i", uint32( encscope ) );
2281 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
2283 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
2284 MkvTree( p_demux, 5, "Type: %i", uint32( enctype ) );
2286 else if( MKV_IS_ID( l3, KaxContentCompression ) )
2288 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
2289 MkvTree( p_demux, 5, "Content Compression" );
2290 for( unsigned int i = 0; i < compr->ListSize(); i++ )
2292 EbmlElement *l4 = (*compr)[i];
2293 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
2295 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
2296 MkvTree( p_demux, 6, "Compression Algorithm: %i", uint32(compalg) );
2297 if( uint32( compalg ) == 0 )
2299 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
2304 MkvTree( p_demux, 6, "Unknown (%s)", typeid(*l4).name() );
2311 MkvTree( p_demux, 5, "Unknown (%s)", typeid(*l3).name() );
2318 MkvTree( p_demux, 4, "Unknown (%s)", typeid(*l2).name() );
2323 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
2325 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
2327 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
2328 // msg_Dbg( p_demux, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
2330 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
2332 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
2334 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
2335 // msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
2337 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
2339 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
2341 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
2342 // msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
2344 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
2346 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
2348 // msg_Dbg( p_demux, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
2350 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
2352 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
2354 // msg_Dbg( p_demux, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
2356 else if( MKV_IS_ID( l, KaxTrackVideo ) )
2358 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
2361 msg_Dbg( p_demux, "| | | + Track Video" );
2364 for( j = 0; j < tkv->ListSize(); j++ )
2366 EbmlElement *l = (*tkv)[j];
2367 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
2369 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
2371 // msg_Dbg( p_demux, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
2373 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
2375 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
2377 // msg_Dbg( p_demux, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
2380 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
2382 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
2384 tk->fmt.video.i_width = uint16( vwidth );
2385 msg_Dbg( p_demux, "| | | | + width=%d", uint16( vwidth ) );
2387 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
2389 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
2391 tk->fmt.video.i_height = uint16( vheight );
2392 msg_Dbg( p_demux, "| | | | + height=%d", uint16( vheight ) );
2394 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
2396 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
2398 tk->fmt.video.i_visible_width = uint16( vwidth );
2399 msg_Dbg( p_demux, "| | | | + display width=%d", uint16( vwidth ) );
2401 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
2403 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
2405 tk->fmt.video.i_visible_height = uint16( vheight );
2406 msg_Dbg( p_demux, "| | | | + display height=%d", uint16( vheight ) );
2408 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
2410 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
2412 tk->f_fps = float( vfps );
2413 msg_Dbg( p_demux, " | | | + fps=%f", float( vfps ) );
2415 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
2417 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
2419 // msg_Dbg( p_demux, "| | | | + Track Video Display Unit=%s",
2420 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
2422 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
2424 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
2426 // msg_Dbg( p_demux, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
2428 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
2430 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
2432 // msg_Dbg( p_demux, " | | | + fps=%f", float( gamma ) );
2436 msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*l).name() );
2440 else if( MKV_IS_ID( l, KaxTrackAudio ) )
2442 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
2445 msg_Dbg( p_demux, "| | | + Track Audio" );
2447 for( j = 0; j < tka->ListSize(); j++ )
2449 EbmlElement *l = (*tka)[j];
2451 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
2453 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
2455 tk->fmt.audio.i_rate = (int)float( afreq );
2456 msg_Dbg( p_demux, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
2458 else if( MKV_IS_ID( l, KaxAudioChannels ) )
2460 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
2462 tk->fmt.audio.i_channels = uint8( achan );
2463 msg_Dbg( p_demux, "| | | | + achan=%u", uint8( achan ) );
2465 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
2467 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
2469 tk->fmt.audio.i_bitspersample = uint8( abits );
2470 msg_Dbg( p_demux, "| | | | + abits=%u", uint8( abits ) );
2474 msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*l).name() );
2480 msg_Dbg( p_demux, "| | | + Unknown (%s)",
2481 typeid(*l).name() );
2486 static void ParseTracks( demux_t *p_demux, EbmlElement *tracks )
2488 demux_sys_t *p_sys = p_demux->p_sys;
2492 int i_upper_level = 0;
2494 msg_Dbg( p_demux, "| + Tracks" );
2496 /* Master elements */
2497 m = static_cast<EbmlMaster *>(tracks);
2498 m->Read( *p_sys->es, tracks->Generic().Context, i_upper_level, el, true );
2500 for( i = 0; i < m->ListSize(); i++ )
2502 EbmlElement *l = (*m)[i];
2504 if( MKV_IS_ID( l, KaxTrackEntry ) )
2506 ParseTrackEntry( p_demux, static_cast<EbmlMaster *>(l) );
2510 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2515 /*****************************************************************************
2517 *****************************************************************************/
2518 static void ParseInfo( demux_t *p_demux, EbmlElement *info )
2520 demux_sys_t *p_sys = p_demux->p_sys;
2524 int i_upper_level = 0;
2526 msg_Dbg( p_demux, "| + Information" );
2528 /* Master elements */
2529 m = static_cast<EbmlMaster *>(info);
2530 m->Read( *p_sys->es, info->Generic().Context, i_upper_level, el, true );
2532 for( i = 0; i < m->ListSize(); i++ )
2534 EbmlElement *l = (*m)[i];
2536 if( MKV_IS_ID( l, KaxSegmentUID ) )
2538 p_sys->segment_uid = *(new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l)));
2540 msg_Dbg( p_demux, "| | + UID=%d", *(uint32*)p_sys->segment_uid.GetBuffer() );
2542 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
2544 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
2546 p_sys->i_timescale = uint64(tcs);
2548 msg_Dbg( p_demux, "| | + TimecodeScale="I64Fd,
2549 p_sys->i_timescale );
2551 else if( MKV_IS_ID( l, KaxDuration ) )
2553 KaxDuration &dur = *(KaxDuration*)l;
2555 p_sys->f_duration = float(dur);
2557 msg_Dbg( p_demux, "| | + Duration=%f",
2558 p_sys->f_duration );
2560 else if( MKV_IS_ID( l, KaxMuxingApp ) )
2562 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
2564 p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
2566 msg_Dbg( p_demux, "| | + Muxing Application=%s",
2567 p_sys->psz_muxing_application );
2569 else if( MKV_IS_ID( l, KaxWritingApp ) )
2571 KaxWritingApp &wapp = *(KaxWritingApp*)l;
2573 p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
2575 msg_Dbg( p_demux, "| | + Writing Application=%s",
2576 p_sys->psz_writing_application );
2578 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
2580 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
2582 p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
2584 msg_Dbg( p_demux, "| | + Segment Filename=%s",
2585 p_sys->psz_segment_filename );
2587 else if( MKV_IS_ID( l, KaxTitle ) )
2589 KaxTitle &title = *(KaxTitle*)l;
2591 p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
2593 msg_Dbg( p_demux, "| | + Title=%s", p_sys->psz_title );
2595 if( MKV_IS_ID( l, KaxSegmentFamily ) )
2597 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
2599 p_sys->families.push_back(*uid);
2601 msg_Dbg( p_demux, "| | + family=%d", *(uint32*)uid->GetBuffer() );
2603 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
2604 else if( MKV_IS_ID( l, KaxDateUTC ) )
2606 KaxDateUTC &date = *(KaxDateUTC*)l;
2611 i_date = date.GetEpochDate();
2612 memset( buffer, 0, 256 );
2613 if( gmtime_r( &i_date, &tmres ) &&
2614 asctime_r( &tmres, buffer ) )
2616 buffer[strlen( buffer)-1]= '\0';
2617 p_sys->psz_date_utc = strdup( buffer );
2618 msg_Dbg( p_demux, "| | + Date=%s", p_sys->psz_date_utc );
2624 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2628 p_sys->f_duration = p_sys->f_duration * p_sys->i_timescale / 1000000.0;
2632 /*****************************************************************************
2634 *****************************************************************************/
2635 static void ParseChapterAtom( demux_t *p_demux, int i_level, EbmlMaster *ca )
2637 demux_sys_t *p_sys = p_demux->p_sys;
2640 bool b_display_seekpoint = true;
2642 if( p_sys->title == NULL )
2644 p_sys->title = vlc_input_title_New();
2646 sk = vlc_seekpoint_New();
2648 sk->i_level = i_level;
2650 msg_Dbg( p_demux, "| | | + ChapterAtom (level=%d)", i_level );
2651 for( i = 0; i < ca->ListSize(); i++ )
2653 EbmlElement *l = (*ca)[i];
2655 if( MKV_IS_ID( l, KaxChapterUID ) )
2657 KaxChapterUID &uid = *(KaxChapterUID*)l;
2658 uint32_t i_uid = uint32( uid );
2659 msg_Dbg( p_demux, "| | | | + ChapterUID: 0x%x", i_uid );
2661 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
2663 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
2664 b_display_seekpoint = uint8( flag ) == 0;
2666 msg_Dbg( p_demux, "| | | | + ChapterFlagHidden: %s", b_display_seekpoint ? "no":"yes" );
2668 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
2670 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
2671 sk->i_time_offset = uint64( start ) / I64C(1000);
2673 msg_Dbg( p_demux, "| | | | + ChapterTimeStart: %lld", sk->i_time_offset );
2675 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
2677 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
2678 int64_t i_end = uint64( end );
2680 msg_Dbg( p_demux, "| | | | + ChapterTimeEnd: %lld", i_end );
2682 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
2684 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
2687 msg_Dbg( p_demux, "| | | | + ChapterDisplay" );
2688 for( j = 0; j < cd->ListSize(); j++ )
2690 EbmlElement *l= (*cd)[j];
2692 if( MKV_IS_ID( l, KaxChapterString ) )
2697 KaxChapterString &name =*(KaxChapterString*)l;
2698 for (k = 0; k < i_level; k++)
2701 psz += UTF8ToStr( UTFstring( name ) );
2702 sk->psz_name = strdup( psz.c_str() );
2703 msg_Dbg( p_demux, "| | | | | + ChapterString '%s'", UTF8ToStr(UTFstring(name)) );
2705 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
2707 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
2708 const char *psz = string( lang ).c_str();
2710 msg_Dbg( p_demux, "| | | | | + ChapterLanguage '%s'", psz );
2712 else if( MKV_IS_ID( l, KaxChapterCountry ) )
2714 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
2715 const char *psz = string( ct ).c_str();
2717 msg_Dbg( p_demux, "| | | | | + ChapterCountry '%s'", psz );
2721 else if( MKV_IS_ID( l, KaxChapterAtom ) )
2723 ParseChapterAtom( p_demux, i_level+1, static_cast<EbmlMaster *>(l) );
2727 if (b_display_seekpoint)
2729 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
2730 p_sys->title->i_seekpoint++;
2731 p_sys->title->seekpoint = (seekpoint_t**)realloc( p_sys->title->seekpoint, p_sys->title->i_seekpoint * sizeof( seekpoint_t* ) );
2732 p_sys->title->seekpoint[p_sys->title->i_seekpoint-1] = sk;
2736 vlc_seekpoint_Delete(sk);
2740 /*****************************************************************************
2742 *****************************************************************************/
2743 static void ParseChapters( demux_t *p_demux, EbmlElement *chapters )
2745 demux_sys_t *p_sys = p_demux->p_sys;
2749 int i_upper_level = 0;
2752 /* Master elements */
2753 m = static_cast<EbmlMaster *>(chapters);
2754 m->Read( *p_sys->es, chapters->Generic().Context, i_upper_level, el, true );
2756 for( i = 0; i < m->ListSize(); i++ )
2758 EbmlElement *l = (*m)[i];
2760 if( MKV_IS_ID( l, KaxEditionEntry ) )
2762 EbmlMaster *E = static_cast<EbmlMaster *>(l );
2764 msg_Dbg( p_demux, "| | + EditionEntry" );
2765 p_sys->edition_ordered = false;
2766 for( j = 0; j < E->ListSize(); j++ )
2768 EbmlElement *l = (*E)[j];
2770 if( MKV_IS_ID( l, KaxChapterAtom ) )
2772 ParseChapterAtom( p_demux, 0, static_cast<EbmlMaster *>(l) );
2774 else if( MKV_IS_ID( l, KaxEditionUID ) )
2776 p_sys->edition_uid = uint64(*static_cast<KaxEditionUID *>(l));
2778 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
2780 p_sys->edition_ordered = uint8(*static_cast<KaxEditionFlagOrdered *>(l)) != 0;
2784 msg_Dbg( p_demux, "| | | + Unknown (%s)", typeid(*l).name() );
2790 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2795 /*****************************************************************************
2796 * InformationCreate:
2797 *****************************************************************************/
2798 static void InformationCreate( demux_t *p_demux )
2800 demux_sys_t *p_sys = p_demux->p_sys;
2803 p_sys->meta = vlc_meta_New();
2805 if( p_sys->psz_title )
2807 vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_sys->psz_title );
2809 if( p_sys->psz_date_utc )
2811 vlc_meta_Add( p_sys->meta, VLC_META_DATE, p_sys->psz_date_utc );
2813 if( p_sys->psz_segment_filename )
2815 vlc_meta_Add( p_sys->meta, _("Segment filename"), p_sys->psz_segment_filename );
2817 if( p_sys->psz_muxing_application )
2819 vlc_meta_Add( p_sys->meta, _("Muxing application"), p_sys->psz_muxing_application );
2821 if( p_sys->psz_writing_application )
2823 vlc_meta_Add( p_sys->meta, _("Writing application"), p_sys->psz_writing_application );
2826 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
2828 mkv_track_t *tk = &p_sys->track[i_track];
2829 vlc_meta_t *mtk = vlc_meta_New();
2831 p_sys->meta->track = (vlc_meta_t**)realloc( p_sys->meta->track,
2832 sizeof( vlc_meta_t * ) * ( p_sys->meta->i_track + 1 ) );
2833 p_sys->meta->track[p_sys->meta->i_track++] = mtk;
2835 if( tk->fmt.psz_description )
2837 vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
2839 if( tk->psz_codec_name )
2841 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
2843 if( tk->psz_codec_settings )
2845 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, tk->psz_codec_settings );
2847 if( tk->psz_codec_info_url )
2849 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
2851 if( tk->psz_codec_download_url )
2853 vlc_meta_Add( p_sys->meta, VLC_META_URL, tk->psz_codec_download_url );
2857 if( p_sys->i_tags_position >= 0 )
2859 vlc_bool_t b_seekable;
2861 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
2864 LoadTags( p_demux );
2870 /*****************************************************************************
2872 *****************************************************************************/
2874 static void IndexAppendCluster( demux_t *p_demux, KaxCluster *cluster )
2876 demux_sys_t *p_sys = p_demux->p_sys;
2878 #define idx p_sys->index[p_sys->i_index]
2880 idx.i_block_number= -1;
2881 idx.i_position = cluster->GetElementPosition();
2883 idx.b_key = VLC_TRUE;
2886 if( p_sys->i_index >= p_sys->i_index_max )
2888 p_sys->i_index_max += 1024;
2889 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2894 static char * UTF8ToStr( const UTFstring &u )
2903 p = dst = (char*)malloc( i_src + 1);