1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
32 # include <time.h> /* time() */
35 #include <vlc/input.h>
37 #include <codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
45 /* libebml and matroska */
46 #include "ebml/EbmlHead.h"
47 #include "ebml/EbmlSubHead.h"
48 #include "ebml/EbmlStream.h"
49 #include "ebml/EbmlContexts.h"
50 #include "ebml/EbmlVersion.h"
51 #include "ebml/EbmlVoid.h"
53 #include "matroska/FileKax.h"
54 #include "matroska/KaxAttachments.h"
55 #include "matroska/KaxBlock.h"
56 #include "matroska/KaxBlockData.h"
57 #include "matroska/KaxChapters.h"
58 #include "matroska/KaxCluster.h"
59 #include "matroska/KaxClusterData.h"
60 #include "matroska/KaxContexts.h"
61 #include "matroska/KaxCues.h"
62 #include "matroska/KaxCuesData.h"
63 #include "matroska/KaxInfo.h"
64 #include "matroska/KaxInfoData.h"
65 #include "matroska/KaxSeekHead.h"
66 #include "matroska/KaxSegment.h"
67 #include "matroska/KaxTag.h"
68 #include "matroska/KaxTags.h"
69 #include "matroska/KaxTagMulti.h"
70 #include "matroska/KaxTracks.h"
71 #include "matroska/KaxTrackAudio.h"
72 #include "matroska/KaxTrackVideo.h"
73 #include "matroska/KaxTrackEntryData.h"
74 #include "matroska/KaxContentEncoding.h"
76 #include "ebml/StdIOCallback.h"
79 #include "mp4/libmp4.h"
85 #define MATROSKA_COMPRESSION_NONE 0
86 #define MATROSKA_COMPRESSION_ZLIB 1
88 using namespace LIBMATROSKA_NAMESPACE;
91 /*****************************************************************************
93 *****************************************************************************/
94 static int Open ( vlc_object_t * );
95 static void Close( vlc_object_t * );
98 set_name( _("Matroska") );
99 set_description( _("Matroska stream demuxer" ) );
100 set_capability( "demux2", 50 );
101 set_callbacks( Open, Close );
102 set_category( CAT_INPUT );
103 set_subcategory( SUBCAT_INPUT_DEMUX );
105 add_bool( "mkv-seek-percent", 1, NULL,
106 N_("Seek based on percent not time"),
107 N_("Seek based on percent not time"), VLC_TRUE );
109 add_shortcut( "mka" );
110 add_shortcut( "mkv" );
113 /*****************************************************************************
115 *****************************************************************************/
116 static int Demux ( demux_t * );
117 static int Control( demux_t *, int, va_list );
118 static void Seek ( demux_t *, mtime_t i_date, int i_percent );
121 block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) {
122 int result, dstsize, n;
127 d_stream.zalloc = (alloc_func)0;
128 d_stream.zfree = (free_func)0;
129 d_stream.opaque = (voidpf)0;
130 result = inflateInit(&d_stream);
133 msg_Dbg( p_this, "inflateInit() failed. Result: %d", result );
137 d_stream.next_in = (Bytef *)p_in_block->p_buffer;
138 d_stream.avail_in = p_in_block->i_buffer;
140 p_block = block_New( p_this, 0 );
145 p_block = block_Realloc( p_block, 0, n * 1000 );
146 dst = (unsigned char *)p_block->p_buffer;
147 d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
148 d_stream.avail_out = 1000;
149 result = inflate(&d_stream, Z_NO_FLUSH);
150 if( ( result != Z_OK ) && ( result != Z_STREAM_END ) )
152 msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result );
156 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
157 ( result != Z_STREAM_END ) );
159 dstsize = d_stream.total_out;
160 inflateEnd( &d_stream );
162 p_block = block_Realloc( p_block, 0, dstsize );
163 p_block->i_buffer = dstsize;
164 block_Release( p_in_block );
171 * Helper function to print the mkv parse tree
173 static void MkvTree( demux_t *p_this, int i_level, char *psz_format, ... )
178 msg_Err( p_this, "too deep tree" );
181 va_start( args, psz_format );
182 static char *psz_foo = "| | | | | | | | | |";
183 char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) );
184 strncpy( psz_foo2, psz_foo, 4 * i_level );
185 psz_foo2[ 4 * i_level ] = '+';
186 psz_foo2[ 4 * i_level + 1 ] = ' ';
187 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
188 __msg_GenericVa( VLC_OBJECT(p_this), VLC_MSG_DBG, "mkv", psz_foo2, args );
193 /*****************************************************************************
195 *****************************************************************************/
196 class vlc_stream_io_callback: public IOCallback
203 vlc_stream_io_callback( stream_t * );
205 virtual uint32 read ( void *p_buffer, size_t i_size);
206 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
207 virtual size_t write ( const void *p_buffer, size_t i_size);
208 virtual uint64 getFilePointer ( void );
209 virtual void close ( void );
212 /*****************************************************************************
214 *****************************************************************************/
218 EbmlParser( EbmlStream *es, EbmlElement *el_start );
223 EbmlElement *Get( void );
226 int GetLevel( void );
231 EbmlElement *m_el[10];
240 /*****************************************************************************
241 * Some functions to manipulate memory
242 *****************************************************************************/
243 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
244 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
246 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
249 /*****************************************************************************
250 * definitions of structures and functions used by this plugins
251 *****************************************************************************/
254 vlc_bool_t b_default;
255 vlc_bool_t b_enabled;
259 uint8_t *p_extra_data;
263 uint64_t i_default_duration;
264 float f_timecodescale;
272 /* data to be send first */
274 uint8_t *p_data_init;
276 /* hack : it's for seek */
277 vlc_bool_t b_search_keyframe;
280 char *psz_codec_name;
281 char *psz_codec_settings;
282 char *psz_codec_info_url;
283 char *psz_codec_download_url;
285 /* encryption/compression */
286 int i_compression_type;
303 vlc_stream_io_callback *in;
308 uint64_t i_timescale;
310 /* duration of the segment */
318 int64_t i_cues_position;
319 int64_t i_chapters_position;
320 int64_t i_tags_position;
334 char *psz_muxing_application;
335 char *psz_writing_application;
336 char *psz_segment_filename;
342 input_title_t *title;
345 #define MKVD_TIMECODESCALE 1000000
347 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
349 static void IndexAppendCluster ( demux_t *p_demux, KaxCluster *cluster );
350 static char *UTF8ToStr ( const UTFstring &u );
351 static void LoadCues ( demux_t * );
352 static void InformationCreate ( demux_t * );
354 static void ParseInfo( demux_t *, EbmlElement *info );
355 static void ParseTracks( demux_t *, EbmlElement *tracks );
356 static void ParseSeekHead( demux_t *, EbmlElement *seekhead );
357 static void ParseChapters( demux_t *, EbmlElement *chapters );
359 /*****************************************************************************
360 * Open: initializes matroska demux structures
361 *****************************************************************************/
362 static int Open( vlc_object_t * p_this )
364 demux_t *p_demux = (demux_t*)p_this;
370 EbmlElement *el = NULL, *el1 = NULL;
372 /* peek the begining */
373 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
375 msg_Warn( p_demux, "cannot peek" );
379 /* is a valid file */
380 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
381 p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
383 msg_Warn( p_demux, "matroska module discarded "
384 "(invalid header 0x%.2x%.2x%.2x%.2x)",
385 p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
389 /* Set the demux function */
390 p_demux->pf_demux = Demux;
391 p_demux->pf_control = Control;
392 p_demux->p_sys = p_sys = (demux_sys_t*)malloc(sizeof( demux_sys_t ));
394 memset( p_sys, 0, sizeof( demux_sys_t ) );
395 p_sys->in = new vlc_stream_io_callback( p_demux->s );
396 p_sys->es = new EbmlStream( *p_sys->in );
397 p_sys->f_duration = -1;
398 p_sys->i_timescale = MKVD_TIMECODESCALE;
400 p_sys->track = (mkv_track_t*)malloc( sizeof( mkv_track_t ) );
402 p_sys->i_cues_position = -1;
403 p_sys->i_chapters_position = -1;
404 p_sys->i_tags_position = -1;
406 p_sys->b_cues = VLC_FALSE;
408 p_sys->i_index_max = 1024;
409 p_sys->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
410 p_sys->i_index_max );
412 p_sys->psz_muxing_application = NULL;
413 p_sys->psz_writing_application = NULL;
414 p_sys->psz_segment_filename = NULL;
415 p_sys->psz_title = NULL;
416 p_sys->psz_date_utc = NULL;;
420 if( p_sys->es == NULL )
422 msg_Err( p_demux, "failed to create EbmlStream" );
427 /* Find the EbmlHead element */
428 el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
431 msg_Err( p_demux, "cannot find EbmlHead" );
434 msg_Dbg( p_demux, "EbmlHead" );
436 el->SkipData( *p_sys->es, el->Generic().Context );
440 el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
443 msg_Err( p_demux, "cannot find KaxSegment" );
446 MkvTree( p_demux, 0, "Segment" );
447 p_sys->segment = (KaxSegment*)el;
448 p_sys->cluster = NULL;
450 p_sys->ep = new EbmlParser( p_sys->es, el );
452 while( ( el1 = p_sys->ep->Get() ) != NULL )
454 if( MKV_IS_ID( el1, KaxInfo ) )
456 ParseInfo( p_demux, el1 );
458 else if( MKV_IS_ID( el1, KaxTracks ) )
460 ParseTracks( p_demux, el1 );
462 else if( MKV_IS_ID( el1, KaxSeekHead ) )
464 ParseSeekHead( p_demux, el1 );
466 else if( MKV_IS_ID( el1, KaxCues ) )
468 msg_Dbg( p_demux, "| + Cues" );
470 else if( MKV_IS_ID( el1, KaxCluster ) )
472 msg_Dbg( p_demux, "| + Cluster" );
474 p_sys->cluster = (KaxCluster*)el1;
477 /* stop parsing the stream */
480 else if( MKV_IS_ID( el1, KaxAttachments ) )
482 msg_Dbg( p_demux, "| + Attachments FIXME TODO (but probably never supported)" );
484 else if( MKV_IS_ID( el1, KaxChapters ) )
486 msg_Dbg( p_demux, "| + Chapters" );
487 ParseChapters( p_demux, el1 );
489 else if( MKV_IS_ID( el1, KaxTag ) )
491 msg_Dbg( p_demux, "| + Tags FIXME TODO" );
495 msg_Dbg( p_demux, "| + Unknown (%s)", typeid(*el1).name() );
499 if( p_sys->cluster == NULL )
501 msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
505 /* *** Load the cue if found *** */
506 if( p_sys->i_cues_position >= 0 )
508 vlc_bool_t b_seekable;
510 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
517 if( !p_sys->b_cues || p_sys->i_index <= 0 )
519 msg_Warn( p_demux, "no cues/empty cues found->seek won't be precise" );
521 IndexAppendCluster( p_demux, p_sys->cluster );
523 p_sys->b_cues = VLC_FALSE;
527 msg_Dbg( p_demux, "found %d es", p_sys->i_track );
528 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
530 #define tk p_sys->track[i_track]
531 if( tk.fmt.i_cat == UNKNOWN_ES )
533 msg_Warn( p_demux, "invalid track[%d, n=%d]", i_track, tk.i_number );
538 if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) )
540 if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
542 msg_Err( p_demux, "missing/invalid BITMAPINFOHEADER" );
543 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
547 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
549 tk.fmt.video.i_width = GetDWLE( &p_bih->biWidth );
550 tk.fmt.video.i_height= GetDWLE( &p_bih->biHeight );
551 tk.fmt.i_codec = GetFOURCC( &p_bih->biCompression );
553 tk.fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
554 if( tk.fmt.i_extra > 0 )
556 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
557 memcpy( tk.fmt.p_extra, &p_bih[1], tk.fmt.i_extra );
561 else if( !strcmp( tk.psz_codec, "V_MPEG1" ) ||
562 !strcmp( tk.psz_codec, "V_MPEG2" ) )
564 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
566 else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) )
568 if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) )
570 tk.fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
574 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
577 else if( !strcmp( tk.psz_codec, "V_QUICKTIME" ) )
579 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
580 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(p_demux),
583 MP4_ReadBoxCommon( p_mp4_stream, p_box );
584 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
585 tk.fmt.i_codec = p_box->i_type;
586 tk.fmt.video.i_width = p_box->data.p_sample_vide->i_width;
587 tk.fmt.video.i_height = p_box->data.p_sample_vide->i_height;
588 tk.fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
589 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
590 memcpy( tk.fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tk.fmt.i_extra );
591 MP4_FreeBox_sample_vide( p_box );
592 stream_MemoryDelete( p_mp4_stream, VLC_TRUE );
594 else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
596 if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
598 msg_Err( p_demux, "missing/invalid WAVEFORMATEX" );
599 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
603 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;
605 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk.fmt.i_codec, NULL );
607 tk.fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
608 tk.fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
609 tk.fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
610 tk.fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
611 tk.fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
613 tk.fmt.i_extra = GetWLE( &p_wf->cbSize );
614 if( tk.fmt.i_extra > 0 )
616 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
617 memcpy( tk.fmt.p_extra, &p_wf[1], tk.fmt.i_extra );
621 else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
622 !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
623 !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
625 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
627 else if( !strcmp( tk.psz_codec, "A_AC3" ) )
629 tk.fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
631 else if( !strcmp( tk.psz_codec, "A_DTS" ) )
633 tk.fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
635 else if( !strcmp( tk.psz_codec, "A_FLAC" ) )
637 tk.fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
638 tk.fmt.i_extra = tk.i_extra_data;
639 tk.fmt.p_extra = malloc( tk.i_extra_data );
640 memcpy( tk.fmt.p_extra,tk.p_extra_data, tk.i_extra_data );
642 else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
644 int i, i_offset = 1, i_size[3], i_extra;
647 tk.fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
649 /* Split the 3 headers */
650 if( tk.p_extra_data[0] != 0x02 )
651 msg_Err( p_demux, "invalid vorbis header" );
653 for( i = 0; i < 2; i++ )
656 while( i_offset < tk.i_extra_data )
658 i_size[i] += tk.p_extra_data[i_offset];
659 if( tk.p_extra_data[i_offset++] != 0xff ) break;
663 i_size[0] = __MIN(i_size[0], tk.i_extra_data - i_offset);
664 i_size[1] = __MIN(i_size[1], tk.i_extra_data -i_offset -i_size[0]);
665 i_size[2] = tk.i_extra_data - i_offset - i_size[0] - i_size[1];
667 tk.fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
668 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
669 p_extra = (uint8_t *)tk.fmt.p_extra; i_extra = 0;
670 for( i = 0; i < 3; i++ )
672 *(p_extra++) = i_size[i] >> 8;
673 *(p_extra++) = i_size[i] & 0xFF;
674 memcpy( p_extra, tk.p_extra_data + i_offset + i_extra,
676 p_extra += i_size[i];
677 i_extra += i_size[i];
680 else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
681 !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
683 int i_profile, i_srate;
684 static unsigned int i_sample_rates[] =
686 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
687 16000, 12000, 11025, 8000, 7350, 0, 0, 0
690 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
691 /* create data for faad (MP4DecSpecificDescrTag)*/
693 if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
697 else if( !strcmp( &tk.psz_codec[12], "LC" ) )
701 else if( !strcmp( &tk.psz_codec[12], "SSR" ) )
710 for( i_srate = 0; i_srate < 13; i_srate++ )
712 if( i_sample_rates[i_srate] == tk.fmt.audio.i_rate )
717 msg_Dbg( p_demux, "profile=%d srate=%d", i_profile, i_srate );
720 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
721 ((uint8_t*)tk.fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
722 ((uint8_t*)tk.fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk.fmt.audio.i_channels << 3);
724 else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
725 !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
726 !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
728 if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
730 tk.fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
734 tk.fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
736 tk.fmt.audio.i_blockalign = ( tk.fmt.audio.i_bitspersample + 7 ) / 8 * tk.fmt.audio.i_channels;
738 else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
740 tk.fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
741 tk.fmt.subs.psz_encoding = strdup( "UTF-8" );
743 else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) ||
744 !strcmp( tk.psz_codec, "S_TEXT/ASS" ) ||
745 !strcmp( tk.psz_codec, "S_SSA" ) ||
746 !strcmp( tk.psz_codec, "S_ASS" ))
748 tk.fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
749 tk.fmt.subs.psz_encoding = strdup( "UTF-8" );
751 else if( !strcmp( tk.psz_codec, "S_VOBSUB" ) )
753 tk.fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
754 if( tk.i_extra_data )
757 char *p_buf = (char *)malloc( tk.i_extra_data + 1);
758 memcpy( p_buf, tk.p_extra_data , tk.i_extra_data );
759 p_buf[tk.i_extra_data] = '\0';
761 p_start = strstr( p_buf, "size:" );
762 if( sscanf( p_start, "size: %dx%d",
763 &tk.fmt.subs.spu.i_original_frame_width, &tk.fmt.subs.spu.i_original_frame_height ) == 2 )
765 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 );
769 msg_Warn( p_demux, "reading original frame size for vobsub failed" );
776 msg_Err( p_demux, "unknow codec id=`%s'", tk.psz_codec );
777 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
781 tk.fmt.i_priority = 1000;
784 tk.p_es = es_out_Add( p_demux->out, &tk.fmt );
788 /* add information */
789 InformationCreate( p_demux );
800 /*****************************************************************************
801 * Close: frees unused data
802 *****************************************************************************/
803 static void Close( vlc_object_t *p_this )
805 demux_t *p_demux = (demux_t*)p_this;
806 demux_sys_t *p_sys = p_demux->p_sys;
809 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
811 #define tk p_sys->track[i_track]
812 if( tk.fmt.psz_description )
814 free( tk.fmt.psz_description );
818 free( tk.psz_codec );
820 if( tk.fmt.psz_language )
822 free( tk.fmt.psz_language );
826 free( p_sys->track );
828 if( p_sys->psz_writing_application )
830 free( p_sys->psz_writing_application );
832 if( p_sys->psz_muxing_application )
834 free( p_sys->psz_muxing_application );
837 delete p_sys->segment;
846 /*****************************************************************************
848 *****************************************************************************/
849 static int Control( demux_t *p_demux, int i_query, va_list args )
851 demux_sys_t *p_sys = p_demux->p_sys;
855 vlc_meta_t **pp_meta;
860 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
861 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
864 case DEMUX_GET_LENGTH:
865 pi64 = (int64_t*)va_arg( args, int64_t * );
866 if( p_sys->f_duration > 0.0 )
868 *pi64 = (int64_t)(p_sys->f_duration * 1000);
873 case DEMUX_GET_POSITION:
874 pf = (double*)va_arg( args, double * );
875 *pf = (double)p_sys->in->getFilePointer() / (double)stream_Size( p_demux->s );
878 case DEMUX_SET_POSITION:
879 f = (double)va_arg( args, double );
880 Seek( p_demux, -1, (int)(100.0 * f) );
884 pi64 = (int64_t*)va_arg( args, int64_t * );
885 if( p_sys->f_duration > 0.0 )
887 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
890 *pi64 = (mtime_t)1000000 *
892 (mtime_t)p_sys->in->getFilePointer() /
893 (mtime_t)stream_Size( p_demux->s );
898 case DEMUX_GET_TITLE_INFO:
899 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
901 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
902 int *pi_int = (int*)va_arg( args, int* );
905 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) );
907 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->title );
913 case DEMUX_SET_TITLE:
914 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
920 case DEMUX_SET_SEEKPOINT:
921 /* FIXME do a better implementation */
922 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
924 int i_skp = (int)va_arg( args, int );
926 Seek( p_demux, (int64_t)p_sys->title->seekpoint[i_skp]->i_time_offset, -1);
939 static int BlockGet( demux_t *p_demux, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
941 demux_sys_t *p_sys = p_demux->p_sys;
957 el = p_sys->ep->Get();
958 i_level = p_sys->ep->GetLevel();
960 if( el == NULL && *pp_block != NULL )
962 /* update the index */
963 #define idx p_sys->index[p_sys->i_index - 1]
964 if( p_sys->i_index > 0 && idx.i_time == -1 )
966 idx.i_time = (*pp_block)->GlobalTimecode() / (mtime_t)1000;
967 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
975 if( p_sys->ep->GetLevel() > 1 )
980 msg_Warn( p_demux, "EOF" );
987 if( MKV_IS_ID( el, KaxCluster ) )
989 p_sys->cluster = (KaxCluster*)el;
991 /* add it to the index */
992 if( p_sys->i_index == 0 ||
993 ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
995 IndexAppendCluster( p_demux, p_sys->cluster );
1000 else if( MKV_IS_ID( el, KaxCues ) )
1002 msg_Warn( p_demux, "find KaxCues FIXME" );
1003 return VLC_EGENERIC;
1007 msg_Dbg( p_demux, "unknown (%s)", typeid( el ).name() );
1010 else if( i_level == 2 )
1012 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1014 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1016 ctc.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
1017 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
1019 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1024 else if( i_level == 3 )
1026 if( MKV_IS_ID( el, KaxBlock ) )
1028 *pp_block = (KaxBlock*)el;
1030 (*pp_block)->ReadData( p_sys->es->I_O() );
1031 (*pp_block)->SetParent( *p_sys->cluster );
1035 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1037 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1039 dur.ReadData( p_sys->es->I_O() );
1040 *pi_duration = uint64( dur );
1042 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1044 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1046 ref.ReadData( p_sys->es->I_O() );
1047 if( *pi_ref1 == -1 )
1049 *pi_ref1 = int64( ref );
1053 *pi_ref2 = int64( ref );
1059 msg_Err( p_demux, "invalid level = %d", i_level );
1060 return VLC_EGENERIC;
1065 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1068 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1069 memcpy( p_block->p_buffer, p_mem, i_mem );
1070 //p_block->i_rate = p_input->stream.control.i_rate;
1074 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1075 mtime_t i_duration )
1077 demux_sys_t *p_sys = p_demux->p_sys;
1083 #define tk p_sys->track[i_track]
1084 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1086 if( tk.i_number == block->TrackNum() )
1092 if( i_track >= p_sys->i_track )
1094 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1098 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk.p_es, &b );
1101 tk.b_inited = VLC_FALSE;
1105 /* First send init data */
1106 if( !tk.b_inited && tk.i_data_init > 0 )
1110 msg_Dbg( p_demux, "sending header (%d bytes)", tk.i_data_init );
1111 p_init = MemToBlock( p_demux, tk.p_data_init, tk.i_data_init );
1112 if( p_init ) es_out_Send( p_demux->out, tk.p_es, p_init );
1114 tk.b_inited = VLC_TRUE;
1117 for( i = 0; i < block->NumberFrames(); i++ )
1120 DataBuffer &data = block->GetBuffer(i);
1122 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1124 if( p_block == NULL )
1129 #if defined(HAVE_ZLIB_H)
1130 if( tk.i_compression_type )
1132 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
1136 if( tk.fmt.i_cat != VIDEO_ES )
1137 p_block->i_dts = p_block->i_pts = i_pts;
1140 p_block->i_dts = i_pts;
1144 if( tk.fmt.i_cat == SPU_ES && strcmp( tk.psz_codec, "S_VOBSUB" ) )
1146 p_block->i_length = i_duration * 1000;
1148 es_out_Send( p_demux->out, tk.p_es, p_block );
1150 /* use time stamp only for first block */
1157 static void Seek( demux_t *p_demux, mtime_t i_date, int i_percent)
1159 demux_sys_t *p_sys = p_demux->p_sys;
1162 int64_t i_block_duration;
1163 int64_t i_block_ref1;
1164 int64_t i_block_ref2;
1167 int i_track_skipping;
1170 msg_Dbg( p_demux, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1171 if( i_date < 0 && i_percent < 0 )
1175 if( i_percent > 100 ) i_percent = 100;
1178 p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
1179 p_sys->cluster = NULL;
1181 /* seek without index or without date */
1182 if( config_GetInt( p_demux, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
1184 int64_t i_pos = i_percent * stream_Size( p_demux->s ) / 100;
1186 msg_Dbg( p_demux, "inacurate way of seeking" );
1187 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1189 if( p_sys->index[i_index].i_position >= i_pos)
1194 if( i_index == p_sys->i_index )
1199 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1202 if( p_sys->index[i_index].i_position < i_pos )
1206 msg_Warn( p_demux, "searching for cluster, could take some time" );
1208 /* search a cluster */
1209 while( ( el = p_sys->ep->Get() ) != NULL )
1211 if( MKV_IS_ID( el, KaxCluster ) )
1213 KaxCluster *cluster = (KaxCluster*)el;
1215 /* add it to the index */
1216 IndexAppendCluster( p_demux, cluster );
1218 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1220 p_sys->cluster = cluster;
1230 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1232 if( p_sys->index[i_index].i_time >= i_date )
1243 msg_Dbg( p_demux, "seek got "I64Fd" (%d%%)",
1244 p_sys->index[i_index].i_time,
1245 (int)( 100 * p_sys->index[i_index].i_position /
1246 stream_Size( p_demux->s ) ) );
1248 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1252 /* now parse until key frame */
1253 #define tk p_sys->track[i_track]
1254 i_track_skipping = 0;
1255 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1257 if( tk.fmt.i_cat == VIDEO_ES )
1259 tk.b_search_keyframe = VLC_TRUE;
1264 while( i_track_skipping > 0 )
1266 if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1268 msg_Warn( p_demux, "cannot get block EOF?" );
1273 p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000 + 1;
1275 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1277 if( tk.i_number == block->TrackNum() )
1283 if( i_track < p_sys->i_track )
1285 if( tk.fmt.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1287 tk.b_search_keyframe = VLC_FALSE;
1290 if( tk.fmt.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1292 BlockDecode( p_demux, block, 0, 0 );
1301 /*****************************************************************************
1302 * Demux: reads and demuxes data packets
1303 *****************************************************************************
1304 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1305 *****************************************************************************/
1306 static int Demux( demux_t *p_demux)
1308 demux_sys_t *p_sys = p_demux->p_sys;
1309 mtime_t i_start_pts;
1310 int i_block_count = 0;
1313 int64_t i_block_duration;
1314 int64_t i_block_ref1;
1315 int64_t i_block_ref2;
1321 if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1323 msg_Warn( p_demux, "cannot get block EOF?" );
1328 p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000 + 1;
1330 if( p_sys->i_pts > 0 )
1332 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
1335 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
1340 if( i_start_pts == -1 )
1342 i_start_pts = p_sys->i_pts;
1344 else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1353 /*****************************************************************************
1355 *****************************************************************************/
1356 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1362 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1364 if( i_size <= 0 || mb_eof )
1369 return stream_Read( s, p_buffer, i_size );
1371 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1377 case seek_beginning:
1381 i_pos = stream_Size( s ) - i_offset;
1384 i_pos= stream_Tell( s ) + i_offset;
1388 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1395 if( stream_Seek( s, i_pos ) )
1401 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1405 uint64 vlc_stream_io_callback::getFilePointer( void )
1407 return stream_Tell( s );
1409 void vlc_stream_io_callback::close( void )
1415 /*****************************************************************************
1416 * Ebml Stream parser
1417 *****************************************************************************/
1418 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1426 for( i = 1; i < 6; i++ )
1432 mb_keep = VLC_FALSE;
1435 EbmlParser::~EbmlParser( void )
1439 for( i = 1; i < mi_level; i++ )
1445 mb_keep = VLC_FALSE;
1449 void EbmlParser::Up( void )
1451 if( mi_user_level == mi_level )
1453 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1459 void EbmlParser::Down( void )
1465 void EbmlParser::Keep( void )
1470 int EbmlParser::GetLevel( void )
1472 return mi_user_level;
1475 EbmlElement *EbmlParser::Get( void )
1479 if( mi_user_level != mi_level )
1485 EbmlElement *ret = m_got;
1491 if( m_el[mi_level] )
1493 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
1496 delete m_el[mi_level];
1498 mb_keep = VLC_FALSE;
1501 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
1512 delete m_el[mi_level - 1];
1513 m_got = m_el[mi_level -1] = m_el[mi_level];
1514 m_el[mi_level] = NULL;
1521 else if( m_el[mi_level] == NULL )
1523 fprintf( stderr," m_el[mi_level] == NULL\n" );
1526 return m_el[mi_level];
1530 /*****************************************************************************
1532 * * LoadCues : load the cues element and update index
1534 * * LoadTags : load ... the tags element
1536 * * InformationCreate : create all information, load tags if present
1538 *****************************************************************************/
1539 static void LoadCues( demux_t *p_demux )
1541 demux_sys_t *p_sys = p_demux->p_sys;
1542 int64_t i_sav_position = p_sys->in->getFilePointer();
1544 EbmlElement *el, *cues;
1546 msg_Dbg( p_demux, "loading cues" );
1547 p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
1548 cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
1552 msg_Err( p_demux, "cannot load cues (broken seekhead or file)" );
1553 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1557 ep = new EbmlParser( p_sys->es, cues );
1558 while( ( el = ep->Get() ) != NULL )
1560 if( MKV_IS_ID( el, KaxCuePoint ) )
1562 #define idx p_sys->index[p_sys->i_index]
1565 idx.i_block_number= -1;
1566 idx.i_position = -1;
1568 idx.b_key = VLC_TRUE;
1571 while( ( el = ep->Get() ) != NULL )
1573 if( MKV_IS_ID( el, KaxCueTime ) )
1575 KaxCueTime &ctime = *(KaxCueTime*)el;
1577 ctime.ReadData( p_sys->es->I_O() );
1579 idx.i_time = uint64( ctime ) * p_sys->i_timescale / (mtime_t)1000;
1581 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
1584 while( ( el = ep->Get() ) != NULL )
1586 if( MKV_IS_ID( el, KaxCueTrack ) )
1588 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
1590 ctrack.ReadData( p_sys->es->I_O() );
1591 idx.i_track = uint16( ctrack );
1593 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
1595 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
1597 ccpos.ReadData( p_sys->es->I_O() );
1598 idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
1600 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
1602 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
1604 cbnum.ReadData( p_sys->es->I_O() );
1605 idx.i_block_number = uint32( cbnum );
1609 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1616 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1622 msg_Dbg( p_demux, " * added time="I64Fd" pos="I64Fd
1623 " track=%d bnum=%d", idx.i_time, idx.i_position,
1624 idx.i_track, idx.i_block_number );
1628 if( p_sys->i_index >= p_sys->i_index_max )
1630 p_sys->i_index_max += 1024;
1631 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
1637 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1643 p_sys->b_cues = VLC_TRUE;
1645 msg_Dbg( p_demux, "loading cues done." );
1646 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1649 static void LoadTags( demux_t *p_demux )
1651 demux_sys_t *p_sys = p_demux->p_sys;
1652 int64_t i_sav_position = p_sys->in->getFilePointer();
1654 EbmlElement *el, *tags;
1656 msg_Dbg( p_demux, "loading tags" );
1657 p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
1658 tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
1662 msg_Err( p_demux, "cannot load tags (broken seekhead or file)" );
1663 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1667 msg_Dbg( p_demux, "Tags" );
1668 ep = new EbmlParser( p_sys->es, tags );
1669 while( ( el = ep->Get() ) != NULL )
1671 if( MKV_IS_ID( el, KaxTag ) )
1673 msg_Dbg( p_demux, "+ Tag" );
1675 while( ( el = ep->Get() ) != NULL )
1677 if( MKV_IS_ID( el, KaxTagTargets ) )
1679 msg_Dbg( p_demux, "| + Targets" );
1681 while( ( el = ep->Get() ) != NULL )
1683 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1687 else if( MKV_IS_ID( el, KaxTagGeneral ) )
1689 msg_Dbg( p_demux, "| + General" );
1691 while( ( el = ep->Get() ) != NULL )
1693 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1697 else if( MKV_IS_ID( el, KaxTagGenres ) )
1699 msg_Dbg( p_demux, "| + Genres" );
1701 while( ( el = ep->Get() ) != NULL )
1703 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1707 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
1709 msg_Dbg( p_demux, "| + Audio Specific" );
1711 while( ( el = ep->Get() ) != NULL )
1713 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1717 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
1719 msg_Dbg( p_demux, "| + Images Specific" );
1721 while( ( el = ep->Get() ) != NULL )
1723 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1727 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
1729 msg_Dbg( p_demux, "| + Multi Comment" );
1731 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
1733 msg_Dbg( p_demux, "| + Multi Commercial" );
1735 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
1737 msg_Dbg( p_demux, "| + Multi Date" );
1739 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
1741 msg_Dbg( p_demux, "| + Multi Entity" );
1743 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
1745 msg_Dbg( p_demux, "| + Multi Identifier" );
1747 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
1749 msg_Dbg( p_demux, "| + Multi Legal" );
1751 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
1753 msg_Dbg( p_demux, "| + Multi Title" );
1757 msg_Dbg( p_demux, "| + Unknown (%s)", typeid( *el ).name() );
1764 msg_Dbg( p_demux, "+ Unknown (%s)", typeid( *el ).name() );
1770 msg_Dbg( p_demux, "loading tags done." );
1771 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1774 /*****************************************************************************
1776 *****************************************************************************/
1777 static void ParseSeekHead( demux_t *p_demux, EbmlElement *seekhead )
1779 demux_sys_t *p_sys = p_demux->p_sys;
1783 int i_upper_level = 0;
1785 msg_Dbg( p_demux, "| + Seek head" );
1787 /* Master elements */
1788 m = static_cast<EbmlMaster *>(seekhead);
1789 m->Read( *p_sys->es, seekhead->Generic().Context, i_upper_level, el, true );
1791 for( i = 0; i < m->ListSize(); i++ )
1793 EbmlElement *l = (*m)[i];
1795 if( MKV_IS_ID( l, KaxSeek ) )
1797 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
1798 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
1803 for( j = 0; j < sk->ListSize(); j++ )
1805 EbmlElement *l = (*sk)[j];
1807 if( MKV_IS_ID( l, KaxSeekID ) )
1809 KaxSeekID &sid = *(KaxSeekID*)l;
1810 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
1812 else if( MKV_IS_ID( l, KaxSeekPosition ) )
1814 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
1815 i_pos = uint64( spos );
1819 msg_Dbg( p_demux, "| | | + Unknown (%s)", typeid(*l).name() );
1825 if( id == KaxCues::ClassInfos.GlobalId )
1827 msg_Dbg( p_demux, "| | | = cues at "I64Fd, i_pos );
1828 p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
1830 else if( id == KaxChapters::ClassInfos.GlobalId )
1832 msg_Dbg( p_demux, "| | | = chapters at "I64Fd, i_pos );
1833 p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
1835 else if( id == KaxTags::ClassInfos.GlobalId )
1837 msg_Dbg( p_demux, "| | | = tags at "I64Fd, i_pos );
1838 p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
1844 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
1849 /*****************************************************************************
1851 *****************************************************************************/
1852 static void ParseTrackEntry( demux_t *p_demux, EbmlMaster *m )
1854 demux_sys_t *p_sys = p_demux->p_sys;
1859 msg_Dbg( p_demux, "| | + Track Entry" );
1862 p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
1864 /* Init the track */
1865 tk = &p_sys->track[p_sys->i_track - 1];
1867 memset( tk, 0, sizeof( mkv_track_t ) );
1869 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
1870 tk->fmt.psz_language = strdup("English");
1871 tk->fmt.psz_description = NULL;
1873 tk->b_default = VLC_TRUE;
1874 tk->b_enabled = VLC_TRUE;
1875 tk->i_number = p_sys->i_track - 1;
1876 tk->i_extra_data = 0;
1877 tk->p_extra_data = NULL;
1878 tk->psz_codec = NULL;
1879 tk->i_default_duration = 0;
1880 tk->f_timecodescale = 1.0;
1882 tk->b_inited = VLC_FALSE;
1883 tk->i_data_init = 0;
1884 tk->p_data_init = NULL;
1886 tk->psz_codec_name = NULL;
1887 tk->psz_codec_settings = NULL;
1888 tk->psz_codec_info_url = NULL;
1889 tk->psz_codec_download_url = NULL;
1891 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
1893 for( i = 0; i < m->ListSize(); i++ )
1895 EbmlElement *l = (*m)[i];
1897 if( MKV_IS_ID( l, KaxTrackNumber ) )
1899 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
1901 tk->i_number = uint32( tnum );
1902 msg_Dbg( p_demux, "| | | + Track Number=%u", uint32( tnum ) );
1904 else if( MKV_IS_ID( l, KaxTrackUID ) )
1906 KaxTrackUID &tuid = *(KaxTrackUID*)l;
1908 msg_Dbg( p_demux, "| | | + Track UID=%u", uint32( tuid ) );
1910 else if( MKV_IS_ID( l, KaxTrackType ) )
1913 KaxTrackType &ttype = *(KaxTrackType*)l;
1915 switch( uint8(ttype) )
1919 tk->fmt.i_cat = AUDIO_ES;
1923 tk->fmt.i_cat = VIDEO_ES;
1925 case track_subtitle:
1926 psz_type = "subtitle";
1927 tk->fmt.i_cat = SPU_ES;
1930 psz_type = "unknown";
1931 tk->fmt.i_cat = UNKNOWN_ES;
1935 msg_Dbg( p_demux, "| | | + Track Type=%s", psz_type );
1937 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
1939 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
1941 // tk->b_enabled = uint32( fenb );
1942 // msg_Dbg( p_demux, "| | | + Track Enabled=%u",
1943 // uint32( fenb ) );
1945 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
1947 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
1949 tk->b_default = uint32( fdef );
1950 msg_Dbg( p_demux, "| | | + Track Default=%u", uint32( fdef ) );
1952 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
1954 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
1956 msg_Dbg( p_demux, "| | | + Track Lacing=%d", uint32( lac ) );
1958 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
1960 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
1962 msg_Dbg( p_demux, "| | | + Track MinCache=%d", uint32( cmin ) );
1964 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
1966 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
1968 msg_Dbg( p_demux, "| | | + Track MaxCache=%d", uint32( cmax ) );
1970 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
1972 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
1974 tk->i_default_duration = uint64(defd);
1975 msg_Dbg( p_demux, "| | | + Track Default Duration="I64Fd, uint64(defd) );
1977 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
1979 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
1981 tk->f_timecodescale = float( ttcs );
1982 msg_Dbg( p_demux, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
1984 else if( MKV_IS_ID( l, KaxTrackName ) )
1986 KaxTrackName &tname = *(KaxTrackName*)l;
1988 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
1989 msg_Dbg( p_demux, "| | | + Track Name=%s", tk->fmt.psz_description );
1991 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
1993 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
1995 tk->fmt.psz_language = strdup( string( lang ).c_str() );
1997 "| | | + Track Language=`%s'", tk->fmt.psz_language );
1999 else if( MKV_IS_ID( l, KaxCodecID ) )
2001 KaxCodecID &codecid = *(KaxCodecID*)l;
2003 tk->psz_codec = strdup( string( codecid ).c_str() );
2004 msg_Dbg( p_demux, "| | | + Track CodecId=%s", string( codecid ).c_str() );
2006 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
2008 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
2010 tk->i_extra_data = cpriv.GetSize();
2011 if( tk->i_extra_data > 0 )
2013 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
2014 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
2016 msg_Dbg( p_demux, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
2018 else if( MKV_IS_ID( l, KaxCodecName ) )
2020 KaxCodecName &cname = *(KaxCodecName*)l;
2022 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
2023 msg_Dbg( p_demux, "| | | + Track Codec Name=%s", tk->psz_codec_name );
2025 else if( MKV_IS_ID( l, KaxContentEncodings ) )
2027 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
2028 MkvTree( p_demux, 3, "Content Encodings" );
2029 for( unsigned int i = 0; i < cencs->ListSize(); i++ )
2031 EbmlElement *l2 = (*cencs)[i];
2032 if( MKV_IS_ID( l2, KaxContentEncoding ) )
2034 MkvTree( p_demux, 4, "Content Encoding" );
2035 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
2036 for( unsigned int i = 0; i < cenc->ListSize(); i++ )
2038 EbmlElement *l3 = (*cenc)[i];
2039 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
2041 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
2042 MkvTree( p_demux, 5, "Order: %i", uint32( encord ) );
2044 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
2046 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
2047 MkvTree( p_demux, 5, "Scope: %i", uint32( encscope ) );
2049 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
2051 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
2052 MkvTree( p_demux, 5, "Type: %i", uint32( enctype ) );
2054 else if( MKV_IS_ID( l3, KaxContentCompression ) )
2056 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
2057 MkvTree( p_demux, 5, "Content Compression" );
2058 for( unsigned int i = 0; i < compr->ListSize(); i++ )
2060 EbmlElement *l4 = (*compr)[i];
2061 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
2063 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
2064 MkvTree( p_demux, 6, "Compression Algorithm: %i", uint32(compalg) );
2065 if( uint32( compalg ) == 0 )
2067 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
2072 MkvTree( p_demux, 6, "Unknown (%s)", typeid(*l4).name() );
2079 MkvTree( p_demux, 5, "Unknown (%s)", typeid(*l3).name() );
2086 MkvTree( p_demux, 4, "Unknown (%s)", typeid(*l2).name() );
2091 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
2093 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
2095 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
2096 // msg_Dbg( p_demux, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
2098 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
2100 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
2102 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
2103 // msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
2105 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
2107 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
2109 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
2110 // msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
2112 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
2114 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
2116 // msg_Dbg( p_demux, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
2118 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
2120 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
2122 // msg_Dbg( p_demux, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
2124 else if( MKV_IS_ID( l, KaxTrackVideo ) )
2126 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
2129 msg_Dbg( p_demux, "| | | + Track Video" );
2132 for( j = 0; j < tkv->ListSize(); j++ )
2134 EbmlElement *l = (*tkv)[j];
2135 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
2137 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
2139 // msg_Dbg( p_demux, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
2141 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
2143 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
2145 // msg_Dbg( p_demux, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
2148 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
2150 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
2152 tk->fmt.video.i_width = uint16( vwidth );
2153 msg_Dbg( p_demux, "| | | | + width=%d", uint16( vwidth ) );
2155 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
2157 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
2159 tk->fmt.video.i_height = uint16( vheight );
2160 msg_Dbg( p_demux, "| | | | + height=%d", uint16( vheight ) );
2162 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
2164 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
2166 tk->fmt.video.i_visible_width = uint16( vwidth );
2167 msg_Dbg( p_demux, "| | | | + display width=%d", uint16( vwidth ) );
2169 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
2171 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
2173 tk->fmt.video.i_visible_height = uint16( vheight );
2174 msg_Dbg( p_demux, "| | | | + display height=%d", uint16( vheight ) );
2176 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
2178 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
2180 tk->f_fps = float( vfps );
2181 msg_Dbg( p_demux, " | | | + fps=%f", float( vfps ) );
2183 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
2185 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
2187 // msg_Dbg( p_demux, "| | | | + Track Video Display Unit=%s",
2188 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
2190 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
2192 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
2194 // msg_Dbg( p_demux, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
2196 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
2198 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
2200 // msg_Dbg( p_demux, " | | | + fps=%f", float( gamma ) );
2204 msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*l).name() );
2208 else if( MKV_IS_ID( l, KaxTrackAudio ) )
2210 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
2213 msg_Dbg( p_demux, "| | | + Track Audio" );
2215 for( j = 0; j < tka->ListSize(); j++ )
2217 EbmlElement *l = (*tka)[j];
2219 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
2221 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
2223 tk->fmt.audio.i_rate = (int)float( afreq );
2224 msg_Dbg( p_demux, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
2226 else if( MKV_IS_ID( l, KaxAudioChannels ) )
2228 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
2230 tk->fmt.audio.i_channels = uint8( achan );
2231 msg_Dbg( p_demux, "| | | | + achan=%u", uint8( achan ) );
2233 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
2235 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
2237 tk->fmt.audio.i_bitspersample = uint8( abits );
2238 msg_Dbg( p_demux, "| | | | + abits=%u", uint8( abits ) );
2242 msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*l).name() );
2248 msg_Dbg( p_demux, "| | | + Unknown (%s)",
2249 typeid(*l).name() );
2254 static void ParseTracks( demux_t *p_demux, EbmlElement *tracks )
2256 demux_sys_t *p_sys = p_demux->p_sys;
2260 int i_upper_level = 0;
2262 msg_Dbg( p_demux, "| + Tracks" );
2264 /* Master elements */
2265 m = static_cast<EbmlMaster *>(tracks);
2266 m->Read( *p_sys->es, tracks->Generic().Context, i_upper_level, el, true );
2268 for( i = 0; i < m->ListSize(); i++ )
2270 EbmlElement *l = (*m)[i];
2272 if( MKV_IS_ID( l, KaxTrackEntry ) )
2274 ParseTrackEntry( p_demux, static_cast<EbmlMaster *>(l) );
2278 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2283 /*****************************************************************************
2285 *****************************************************************************/
2286 static void ParseInfo( demux_t *p_demux, EbmlElement *info )
2288 demux_sys_t *p_sys = p_demux->p_sys;
2292 int i_upper_level = 0;
2294 msg_Dbg( p_demux, "| + Information" );
2296 /* Master elements */
2297 m = static_cast<EbmlMaster *>(info);
2298 m->Read( *p_sys->es, info->Generic().Context, i_upper_level, el, true );
2300 for( i = 0; i < m->ListSize(); i++ )
2302 EbmlElement *l = (*m)[i];
2304 if( MKV_IS_ID( l, KaxSegmentUID ) )
2306 KaxSegmentUID &uid = *(KaxSegmentUID*)l;
2308 msg_Dbg( p_demux, "| | + UID=%d", uint32(uid) );
2310 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
2312 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
2314 p_sys->i_timescale = uint64(tcs);
2316 msg_Dbg( p_demux, "| | + TimecodeScale="I64Fd,
2317 p_sys->i_timescale );
2319 else if( MKV_IS_ID( l, KaxDuration ) )
2321 KaxDuration &dur = *(KaxDuration*)l;
2323 p_sys->f_duration = float(dur);
2325 msg_Dbg( p_demux, "| | + Duration=%f",
2326 p_sys->f_duration );
2328 else if( MKV_IS_ID( l, KaxMuxingApp ) )
2330 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
2332 p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
2334 msg_Dbg( p_demux, "| | + Muxing Application=%s",
2335 p_sys->psz_muxing_application );
2337 else if( MKV_IS_ID( l, KaxWritingApp ) )
2339 KaxWritingApp &wapp = *(KaxWritingApp*)l;
2341 p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
2343 msg_Dbg( p_demux, "| | + Writing Application=%s",
2344 p_sys->psz_writing_application );
2346 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
2348 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
2350 p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
2352 msg_Dbg( p_demux, "| | + Segment Filename=%s",
2353 p_sys->psz_segment_filename );
2355 else if( MKV_IS_ID( l, KaxTitle ) )
2357 KaxTitle &title = *(KaxTitle*)l;
2359 p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
2361 msg_Dbg( p_demux, "| | + Title=%s", p_sys->psz_title );
2363 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
2364 else if( MKV_IS_ID( l, KaxDateUTC ) )
2366 KaxDateUTC &date = *(KaxDateUTC*)l;
2371 i_date = date.GetEpochDate();
2372 memset( buffer, 0, 256 );
2373 if( gmtime_r( &i_date, &tmres ) &&
2374 asctime_r( &tmres, buffer ) )
2376 buffer[strlen( buffer)-1]= '\0';
2377 p_sys->psz_date_utc = strdup( buffer );
2378 msg_Dbg( p_demux, "| | + Date=%s", p_sys->psz_date_utc );
2384 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2388 p_sys->f_duration = p_sys->f_duration * p_sys->i_timescale / 1000000.0;
2392 /*****************************************************************************
2394 *****************************************************************************/
2395 static void ParseChapterAtom( demux_t *p_demux, int i_level, EbmlMaster *ca )
2397 demux_sys_t *p_sys = p_demux->p_sys;
2401 if( p_sys->title == NULL )
2403 p_sys->title = vlc_input_title_New();
2405 sk = vlc_seekpoint_New();
2407 msg_Dbg( p_demux, "| | | + ChapterAtom (level=%d)", i_level );
2408 for( i = 0; i < ca->ListSize(); i++ )
2410 EbmlElement *l = (*ca)[i];
2412 if( MKV_IS_ID( l, KaxChapterUID ) )
2414 KaxChapterUID &uid = *(KaxChapterUID*)l;
2415 uint32_t i_uid = uint32( uid );
2416 msg_Dbg( p_demux, "| | | | + ChapterUID: 0x%x", i_uid );
2418 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
2420 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
2421 sk->i_time_offset = uint64( start ) / I64C(1000);
2423 msg_Dbg( p_demux, "| | | | + ChapterTimeStart: %lld", sk->i_time_offset );
2425 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
2427 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
2428 int64_t i_end = uint64( end );
2430 msg_Dbg( p_demux, "| | | | + ChapterTimeEnd: %lld", i_end );
2432 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
2434 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
2437 msg_Dbg( p_demux, "| | | | + ChapterDisplay" );
2438 for( j = 0; j < cd->ListSize(); j++ )
2440 EbmlElement *l= (*cd)[j];
2442 if( MKV_IS_ID( l, KaxChapterString ) )
2444 KaxChapterString &name =*(KaxChapterString*)l;
2445 char *psz = UTF8ToStr( UTFstring( name ) );
2446 sk->psz_name = strdup( psz );
2447 msg_Dbg( p_demux, "| | | | | + ChapterString '%s'", psz );
2449 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
2451 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
2452 const char *psz = string( lang ).c_str();
2454 msg_Dbg( p_demux, "| | | | | + ChapterLanguage '%s'", psz );
2456 else if( MKV_IS_ID( l, KaxChapterCountry ) )
2458 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
2459 const char *psz = string( ct ).c_str();
2461 msg_Dbg( p_demux, "| | | | | + ChapterCountry '%s'", psz );
2465 else if( MKV_IS_ID( l, KaxChapterAtom ) )
2467 ParseChapterAtom( p_demux, i_level+1, static_cast<EbmlMaster *>(l) );
2470 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
2471 p_sys->title->i_seekpoint++;
2472 p_sys->title->seekpoint = (seekpoint_t**)realloc( p_sys->title->seekpoint, p_sys->title->i_seekpoint * sizeof( seekpoint_t* ) );
2473 p_sys->title->seekpoint[p_sys->title->i_seekpoint-1] = sk;
2476 /*****************************************************************************
2478 *****************************************************************************/
2479 static void ParseChapters( demux_t *p_demux, EbmlElement *chapters )
2481 demux_sys_t *p_sys = p_demux->p_sys;
2485 int i_upper_level = 0;
2488 /* Master elements */
2489 m = static_cast<EbmlMaster *>(chapters);
2490 m->Read( *p_sys->es, chapters->Generic().Context, i_upper_level, el, true );
2492 for( i = 0; i < m->ListSize(); i++ )
2494 EbmlElement *l = (*m)[i];
2496 if( MKV_IS_ID( l, KaxEditionEntry ) )
2498 EbmlMaster *E = static_cast<EbmlMaster *>(l );
2500 msg_Dbg( p_demux, "| | + EditionEntry" );
2501 for( j = 0; j < E->ListSize(); j++ )
2503 EbmlElement *l = (*E)[j];
2505 if( MKV_IS_ID( l, KaxChapterAtom ) )
2507 ParseChapterAtom( p_demux, 0, static_cast<EbmlMaster *>(l) );
2511 msg_Dbg( p_demux, "| | | + Unknown (%s)", typeid(*l).name() );
2517 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2522 /*****************************************************************************
2523 * InformationCreate:
2524 *****************************************************************************/
2525 static void InformationCreate( demux_t *p_demux )
2527 demux_sys_t *p_sys = p_demux->p_sys;
2530 p_sys->meta = vlc_meta_New();
2532 if( p_sys->psz_title )
2534 vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_sys->psz_title );
2536 if( p_sys->psz_date_utc )
2538 vlc_meta_Add( p_sys->meta, VLC_META_DATE, p_sys->psz_date_utc );
2540 if( p_sys->psz_segment_filename )
2542 vlc_meta_Add( p_sys->meta, _("Segment filename"), p_sys->psz_segment_filename );
2544 if( p_sys->psz_muxing_application )
2546 vlc_meta_Add( p_sys->meta, _("Muxing application"), p_sys->psz_muxing_application );
2548 if( p_sys->psz_writing_application )
2550 vlc_meta_Add( p_sys->meta, _("Writing application"), p_sys->psz_writing_application );
2553 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
2555 mkv_track_t *tk = &p_sys->track[i_track];
2556 vlc_meta_t *mtk = vlc_meta_New();
2558 p_sys->meta->track = (vlc_meta_t**)realloc( p_sys->meta->track,
2559 sizeof( vlc_meta_t * ) * ( p_sys->meta->i_track + 1 ) );
2560 p_sys->meta->track[p_sys->meta->i_track++] = mtk;
2562 if( tk->fmt.psz_description )
2564 vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
2566 if( tk->psz_codec_name )
2568 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
2570 if( tk->psz_codec_settings )
2572 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, tk->psz_codec_settings );
2574 if( tk->psz_codec_info_url )
2576 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
2578 if( tk->psz_codec_download_url )
2580 vlc_meta_Add( p_sys->meta, VLC_META_URL, tk->psz_codec_download_url );
2584 if( p_sys->i_tags_position >= 0 )
2586 vlc_bool_t b_seekable;
2588 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
2591 LoadTags( p_demux );
2597 /*****************************************************************************
2599 *****************************************************************************/
2601 static void IndexAppendCluster( demux_t *p_demux, KaxCluster *cluster )
2603 demux_sys_t *p_sys = p_demux->p_sys;
2605 #define idx p_sys->index[p_sys->i_index]
2607 idx.i_block_number= -1;
2608 idx.i_position = cluster->GetElementPosition();
2610 idx.b_key = VLC_TRUE;
2613 if( p_sys->i_index >= p_sys->i_index_max )
2615 p_sys->i_index_max += 1024;
2616 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2621 static char * UTF8ToStr( const UTFstring &u )
2630 p = dst = (char*)malloc( i_src + 1);