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_description( _("Matroska stream demuxer" ) );
99 set_capability( "demux2", 50 );
100 set_callbacks( Open, Close );
102 add_bool( "mkv-seek-percent", 1, NULL,
103 N_("Seek based on percent not time"),
104 N_("Seek based on percent not time"), VLC_TRUE );
106 add_shortcut( "mka" );
107 add_shortcut( "mkv" );
110 /*****************************************************************************
112 *****************************************************************************/
113 static int Demux ( demux_t * );
114 static int Control( demux_t *, int, va_list );
115 static void Seek ( demux_t *, mtime_t i_date, int i_percent );
118 block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) {
119 int result, dstsize, n;
124 d_stream.zalloc = (alloc_func)0;
125 d_stream.zfree = (free_func)0;
126 d_stream.opaque = (voidpf)0;
127 result = inflateInit(&d_stream);
130 msg_Dbg( p_this, "inflateInit() failed. Result: %d", result );
134 d_stream.next_in = (Bytef *)p_in_block->p_buffer;
135 d_stream.avail_in = p_in_block->i_buffer;
137 p_block = block_New( p_this, 0 );
142 p_block = block_Realloc( p_block, 0, n * 1000 );
143 dst = (unsigned char *)p_block->p_buffer;
144 d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
145 d_stream.avail_out = 1000;
146 result = inflate(&d_stream, Z_NO_FLUSH);
147 if( ( result != Z_OK ) && ( result != Z_STREAM_END ) )
149 msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result );
153 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
154 ( result != Z_STREAM_END ) );
156 dstsize = d_stream.total_out;
157 inflateEnd( &d_stream );
159 p_block = block_Realloc( p_block, 0, dstsize );
160 p_block->i_buffer = dstsize;
161 block_Release( p_in_block );
168 * Helper function to print the mkv parse tree
170 static void MkvTree( demux_t *p_this, int i_level, char *psz_format, ... )
175 msg_Err( p_this, "too deep tree" );
178 va_start( args, psz_format );
179 static char *psz_foo = "| | | | | | | | | |";
180 char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) );
181 strncpy( psz_foo2, psz_foo, 4 * i_level );
182 psz_foo2[ 4 * i_level ] = '+';
183 psz_foo2[ 4 * i_level + 1 ] = ' ';
184 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
185 __msg_GenericVa( VLC_OBJECT(p_this), VLC_MSG_DBG, "mkv", psz_foo2, args );
190 /*****************************************************************************
192 *****************************************************************************/
193 class vlc_stream_io_callback: public IOCallback
200 vlc_stream_io_callback( stream_t * );
202 virtual uint32 read ( void *p_buffer, size_t i_size);
203 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
204 virtual size_t write ( const void *p_buffer, size_t i_size);
205 virtual uint64 getFilePointer ( void );
206 virtual void close ( void );
209 /*****************************************************************************
211 *****************************************************************************/
215 EbmlParser( EbmlStream *es, EbmlElement *el_start );
220 EbmlElement *Get( void );
223 int GetLevel( void );
228 EbmlElement *m_el[10];
237 /*****************************************************************************
238 * Some functions to manipulate memory
239 *****************************************************************************/
240 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
241 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
243 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
246 /*****************************************************************************
247 * definitions of structures and functions used by this plugins
248 *****************************************************************************/
251 vlc_bool_t b_default;
252 vlc_bool_t b_enabled;
256 uint8_t *p_extra_data;
260 uint64_t i_default_duration;
261 float f_timecodescale;
269 /* data to be send first */
271 uint8_t *p_data_init;
273 /* hack : it's for seek */
274 vlc_bool_t b_search_keyframe;
277 char *psz_codec_name;
278 char *psz_codec_settings;
279 char *psz_codec_info_url;
280 char *psz_codec_download_url;
282 /* encryption/compression */
283 int i_compression_type;
300 vlc_stream_io_callback *in;
305 uint64_t i_timescale;
307 /* duration of the segment */
315 int64_t i_cues_position;
316 int64_t i_chapters_position;
317 int64_t i_tags_position;
331 char *psz_muxing_application;
332 char *psz_writing_application;
333 char *psz_segment_filename;
339 input_title_t *title;
342 #define MKVD_TIMECODESCALE 1000000
344 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
346 static void IndexAppendCluster ( demux_t *p_demux, KaxCluster *cluster );
347 static char *UTF8ToStr ( const UTFstring &u );
348 static void LoadCues ( demux_t * );
349 static void InformationCreate ( demux_t * );
351 static void ParseInfo( demux_t *, EbmlElement *info );
352 static void ParseTracks( demux_t *, EbmlElement *tracks );
353 static void ParseSeekHead( demux_t *, EbmlElement *seekhead );
354 static void ParseChapters( demux_t *, EbmlElement *chapters );
356 /*****************************************************************************
357 * Open: initializes matroska demux structures
358 *****************************************************************************/
359 static int Open( vlc_object_t * p_this )
361 demux_t *p_demux = (demux_t*)p_this;
367 EbmlElement *el = NULL, *el1 = NULL;
369 /* peek the begining */
370 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
372 msg_Warn( p_demux, "cannot peek" );
376 /* is a valid file */
377 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
378 p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
380 msg_Warn( p_demux, "matroska module discarded "
381 "(invalid header 0x%.2x%.2x%.2x%.2x)",
382 p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
386 /* Set the demux function */
387 p_demux->pf_demux = Demux;
388 p_demux->pf_control = Control;
389 p_demux->p_sys = p_sys = (demux_sys_t*)malloc(sizeof( demux_sys_t ));
391 memset( p_sys, 0, sizeof( demux_sys_t ) );
392 p_sys->in = new vlc_stream_io_callback( p_demux->s );
393 p_sys->es = new EbmlStream( *p_sys->in );
394 p_sys->f_duration = -1;
395 p_sys->i_timescale = MKVD_TIMECODESCALE;
397 p_sys->track = (mkv_track_t*)malloc( sizeof( mkv_track_t ) );
399 p_sys->i_cues_position = -1;
400 p_sys->i_chapters_position = -1;
401 p_sys->i_tags_position = -1;
403 p_sys->b_cues = VLC_FALSE;
405 p_sys->i_index_max = 1024;
406 p_sys->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
407 p_sys->i_index_max );
409 p_sys->psz_muxing_application = NULL;
410 p_sys->psz_writing_application = NULL;
411 p_sys->psz_segment_filename = NULL;
412 p_sys->psz_title = NULL;
413 p_sys->psz_date_utc = NULL;;
417 if( p_sys->es == NULL )
419 msg_Err( p_demux, "failed to create EbmlStream" );
424 /* Find the EbmlHead element */
425 el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
428 msg_Err( p_demux, "cannot find EbmlHead" );
431 msg_Dbg( p_demux, "EbmlHead" );
433 el->SkipData( *p_sys->es, el->Generic().Context );
437 el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
440 msg_Err( p_demux, "cannot find KaxSegment" );
443 MkvTree( p_demux, 0, "Segment" );
444 p_sys->segment = (KaxSegment*)el;
445 p_sys->cluster = NULL;
447 p_sys->ep = new EbmlParser( p_sys->es, el );
449 while( ( el1 = p_sys->ep->Get() ) != NULL )
451 if( MKV_IS_ID( el1, KaxInfo ) )
453 ParseInfo( p_demux, el1 );
455 else if( MKV_IS_ID( el1, KaxTracks ) )
457 ParseTracks( p_demux, el1 );
459 else if( MKV_IS_ID( el1, KaxSeekHead ) )
461 ParseSeekHead( p_demux, el1 );
463 else if( MKV_IS_ID( el1, KaxCues ) )
465 msg_Dbg( p_demux, "| + Cues" );
467 else if( MKV_IS_ID( el1, KaxCluster ) )
469 msg_Dbg( p_demux, "| + Cluster" );
471 p_sys->cluster = (KaxCluster*)el1;
474 /* stop parsing the stream */
477 else if( MKV_IS_ID( el1, KaxAttachments ) )
479 msg_Dbg( p_demux, "| + Attachments FIXME TODO (but probably never supported)" );
481 else if( MKV_IS_ID( el1, KaxChapters ) )
483 msg_Dbg( p_demux, "| + Chapters" );
484 ParseChapters( p_demux, el1 );
486 else if( MKV_IS_ID( el1, KaxTag ) )
488 msg_Dbg( p_demux, "| + Tags FIXME TODO" );
492 msg_Dbg( p_demux, "| + Unknown (%s)", typeid(*el1).name() );
496 if( p_sys->cluster == NULL )
498 msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
502 /* *** Load the cue if found *** */
503 if( p_sys->i_cues_position >= 0 )
505 vlc_bool_t b_seekable;
507 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
514 if( !p_sys->b_cues || p_sys->i_index <= 0 )
516 msg_Warn( p_demux, "no cues/empty cues found->seek won't be precise" );
518 IndexAppendCluster( p_demux, p_sys->cluster );
520 p_sys->b_cues = VLC_FALSE;
524 msg_Dbg( p_demux, "found %d es", p_sys->i_track );
525 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
527 #define tk p_sys->track[i_track]
528 if( tk.fmt.i_cat == UNKNOWN_ES )
530 msg_Warn( p_demux, "invalid track[%d, n=%d]", i_track, tk.i_number );
535 if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) )
537 if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
539 msg_Err( p_demux, "missing/invalid BITMAPINFOHEADER" );
540 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
544 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
546 tk.fmt.video.i_width = GetDWLE( &p_bih->biWidth );
547 tk.fmt.video.i_height= GetDWLE( &p_bih->biHeight );
548 tk.fmt.i_codec = GetFOURCC( &p_bih->biCompression );
550 tk.fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
551 if( tk.fmt.i_extra > 0 )
553 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
554 memcpy( tk.fmt.p_extra, &p_bih[1], tk.fmt.i_extra );
558 else if( !strcmp( tk.psz_codec, "V_MPEG1" ) ||
559 !strcmp( tk.psz_codec, "V_MPEG2" ) )
561 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
563 else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) )
565 if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) )
567 tk.fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
571 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
574 else if( !strcmp( tk.psz_codec, "V_QUICKTIME" ) )
576 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
577 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(p_demux),
580 MP4_ReadBoxCommon( p_mp4_stream, p_box );
581 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
582 tk.fmt.i_codec = p_box->i_type;
583 tk.fmt.video.i_width = p_box->data.p_sample_vide->i_width;
584 tk.fmt.video.i_height = p_box->data.p_sample_vide->i_height;
585 tk.fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
586 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
587 memcpy( tk.fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tk.fmt.i_extra );
588 MP4_FreeBox_sample_vide( p_box );
589 stream_MemoryDelete( p_mp4_stream, VLC_TRUE );
591 else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
593 if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
595 msg_Err( p_demux, "missing/invalid WAVEFORMATEX" );
596 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
600 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;
602 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk.fmt.i_codec, NULL );
604 tk.fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
605 tk.fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
606 tk.fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
607 tk.fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
608 tk.fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
610 tk.fmt.i_extra = GetWLE( &p_wf->cbSize );
611 if( tk.fmt.i_extra > 0 )
613 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
614 memcpy( tk.fmt.p_extra, &p_wf[1], tk.fmt.i_extra );
618 else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
619 !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
620 !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
622 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
624 else if( !strcmp( tk.psz_codec, "A_AC3" ) )
626 tk.fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
628 else if( !strcmp( tk.psz_codec, "A_DTS" ) )
630 tk.fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
632 else if( !strcmp( tk.psz_codec, "A_FLAC" ) )
634 tk.fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
635 tk.fmt.i_extra = tk.i_extra_data;
636 tk.fmt.p_extra = malloc( tk.i_extra_data );
637 memcpy( tk.fmt.p_extra,tk.p_extra_data, tk.i_extra_data );
639 else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
641 int i, i_offset = 1, i_size[3], i_extra;
644 tk.fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
646 /* Split the 3 headers */
647 if( tk.p_extra_data[0] != 0x02 )
648 msg_Err( p_demux, "invalid vorbis header" );
650 for( i = 0; i < 2; i++ )
653 while( i_offset < tk.i_extra_data )
655 i_size[i] += tk.p_extra_data[i_offset];
656 if( tk.p_extra_data[i_offset++] != 0xff ) break;
660 i_size[0] = __MIN(i_size[0], tk.i_extra_data - i_offset);
661 i_size[1] = __MIN(i_size[1], tk.i_extra_data -i_offset -i_size[0]);
662 i_size[2] = tk.i_extra_data - i_offset - i_size[0] - i_size[1];
664 tk.fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
665 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
666 p_extra = (uint8_t *)tk.fmt.p_extra; i_extra = 0;
667 for( i = 0; i < 3; i++ )
669 *(p_extra++) = i_size[i] >> 8;
670 *(p_extra++) = i_size[i] & 0xFF;
671 memcpy( p_extra, tk.p_extra_data + i_offset + i_extra,
673 p_extra += i_size[i];
674 i_extra += i_size[i];
677 else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
678 !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
680 int i_profile, i_srate;
681 static unsigned int i_sample_rates[] =
683 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
684 16000, 12000, 11025, 8000, 7350, 0, 0, 0
687 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
688 /* create data for faad (MP4DecSpecificDescrTag)*/
690 if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
694 else if( !strcmp( &tk.psz_codec[12], "LC" ) )
698 else if( !strcmp( &tk.psz_codec[12], "SSR" ) )
707 for( i_srate = 0; i_srate < 13; i_srate++ )
709 if( i_sample_rates[i_srate] == tk.fmt.audio.i_rate )
714 msg_Dbg( p_demux, "profile=%d srate=%d", i_profile, i_srate );
717 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
718 ((uint8_t*)tk.fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
719 ((uint8_t*)tk.fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk.fmt.audio.i_channels << 3);
721 else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
722 !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
723 !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
725 if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
727 tk.fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
731 tk.fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
733 tk.fmt.audio.i_blockalign = ( tk.fmt.audio.i_bitspersample + 7 ) / 8 * tk.fmt.audio.i_channels;
735 else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
737 tk.fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
738 tk.fmt.subs.psz_encoding = strdup( "UTF-8" );
740 else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) ||
741 !strcmp( tk.psz_codec, "S_TEXT/ASS" ) ||
742 !strcmp( tk.psz_codec, "S_SSA" ) ||
743 !strcmp( tk.psz_codec, "S_ASS" ))
745 tk.fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
746 tk.fmt.subs.psz_encoding = strdup( "UTF-8" );
748 else if( !strcmp( tk.psz_codec, "S_VOBSUB" ) )
750 tk.fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
751 if( tk.i_extra_data )
754 char *p_buf = (char *)malloc( tk.i_extra_data + 1);
755 memcpy( p_buf, tk.p_extra_data , tk.i_extra_data );
756 p_buf[tk.i_extra_data] = '\0';
758 p_start = strstr( p_buf, "size:" );
759 if( sscanf( p_start, "size: %dx%d",
760 &tk.fmt.subs.spu.i_original_frame_width, &tk.fmt.subs.spu.i_original_frame_height ) == 2 )
762 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 );
766 msg_Warn( p_demux, "reading original frame size for vobsub failed" );
773 msg_Err( p_demux, "unknow codec id=`%s'", tk.psz_codec );
774 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
778 tk.fmt.i_priority = 1000;
781 tk.p_es = es_out_Add( p_demux->out, &tk.fmt );
785 /* add information */
786 InformationCreate( p_demux );
797 /*****************************************************************************
798 * Close: frees unused data
799 *****************************************************************************/
800 static void Close( vlc_object_t *p_this )
802 demux_t *p_demux = (demux_t*)p_this;
803 demux_sys_t *p_sys = p_demux->p_sys;
806 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
808 #define tk p_sys->track[i_track]
809 if( tk.fmt.psz_description )
811 free( tk.fmt.psz_description );
815 free( tk.psz_codec );
817 if( tk.fmt.psz_language )
819 free( tk.fmt.psz_language );
823 free( p_sys->track );
825 if( p_sys->psz_writing_application )
827 free( p_sys->psz_writing_application );
829 if( p_sys->psz_muxing_application )
831 free( p_sys->psz_muxing_application );
834 delete p_sys->segment;
843 /*****************************************************************************
845 *****************************************************************************/
846 static int Control( demux_t *p_demux, int i_query, va_list args )
848 demux_sys_t *p_sys = p_demux->p_sys;
852 vlc_meta_t **pp_meta;
857 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
858 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
861 case DEMUX_GET_LENGTH:
862 pi64 = (int64_t*)va_arg( args, int64_t * );
863 if( p_sys->f_duration > 0.0 )
865 *pi64 = (int64_t)(p_sys->f_duration * 1000);
870 case DEMUX_GET_POSITION:
871 pf = (double*)va_arg( args, double * );
872 *pf = (double)p_sys->in->getFilePointer() / (double)stream_Size( p_demux->s );
875 case DEMUX_SET_POSITION:
876 f = (double)va_arg( args, double );
877 Seek( p_demux, -1, (int)(100.0 * f) );
881 pi64 = (int64_t*)va_arg( args, int64_t * );
882 if( p_sys->f_duration > 0.0 )
884 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
887 *pi64 = (mtime_t)1000000 *
889 (mtime_t)p_sys->in->getFilePointer() /
890 (mtime_t)stream_Size( p_demux->s );
895 case DEMUX_GET_TITLE_INFO:
896 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
898 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
899 int *pi_int = (int*)va_arg( args, int* );
902 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) );
904 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->title );
910 case DEMUX_SET_TITLE:
911 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
917 case DEMUX_SET_SEEKPOINT:
918 /* FIXME do a better implementation */
919 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
921 int i_skp = (int)va_arg( args, int );
923 Seek( p_demux, (int64_t)p_sys->title->seekpoint[i_skp]->i_time_offset, -1);
936 static int BlockGet( demux_t *p_demux, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
938 demux_sys_t *p_sys = p_demux->p_sys;
954 el = p_sys->ep->Get();
955 i_level = p_sys->ep->GetLevel();
957 if( el == NULL && *pp_block != NULL )
959 /* update the index */
960 #define idx p_sys->index[p_sys->i_index - 1]
961 if( p_sys->i_index > 0 && idx.i_time == -1 )
963 idx.i_time = (*pp_block)->GlobalTimecode() / (mtime_t)1000;
964 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
972 if( p_sys->ep->GetLevel() > 1 )
977 msg_Warn( p_demux, "EOF" );
984 if( MKV_IS_ID( el, KaxCluster ) )
986 p_sys->cluster = (KaxCluster*)el;
988 /* add it to the index */
989 if( p_sys->i_index == 0 ||
990 ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
992 IndexAppendCluster( p_demux, p_sys->cluster );
997 else if( MKV_IS_ID( el, KaxCues ) )
999 msg_Warn( p_demux, "find KaxCues FIXME" );
1000 return VLC_EGENERIC;
1004 msg_Dbg( p_demux, "unknown (%s)", typeid( el ).name() );
1007 else if( i_level == 2 )
1009 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1011 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1013 ctc.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
1014 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
1016 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1021 else if( i_level == 3 )
1023 if( MKV_IS_ID( el, KaxBlock ) )
1025 *pp_block = (KaxBlock*)el;
1027 (*pp_block)->ReadData( p_sys->es->I_O() );
1028 (*pp_block)->SetParent( *p_sys->cluster );
1032 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1034 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1036 dur.ReadData( p_sys->es->I_O() );
1037 *pi_duration = uint64( dur );
1039 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1041 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1043 ref.ReadData( p_sys->es->I_O() );
1044 if( *pi_ref1 == -1 )
1046 *pi_ref1 = int64( ref );
1050 *pi_ref2 = int64( ref );
1056 msg_Err( p_demux, "invalid level = %d", i_level );
1057 return VLC_EGENERIC;
1062 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1065 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1066 memcpy( p_block->p_buffer, p_mem, i_mem );
1067 //p_block->i_rate = p_input->stream.control.i_rate;
1071 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1072 mtime_t i_duration )
1074 demux_sys_t *p_sys = p_demux->p_sys;
1080 #define tk p_sys->track[i_track]
1081 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1083 if( tk.i_number == block->TrackNum() )
1089 if( i_track >= p_sys->i_track )
1091 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1095 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk.p_es, &b );
1098 tk.b_inited = VLC_FALSE;
1102 /* First send init data */
1103 if( !tk.b_inited && tk.i_data_init > 0 )
1107 msg_Dbg( p_demux, "sending header (%d bytes)", tk.i_data_init );
1108 p_init = MemToBlock( p_demux, tk.p_data_init, tk.i_data_init );
1109 if( p_init ) es_out_Send( p_demux->out, tk.p_es, p_init );
1111 tk.b_inited = VLC_TRUE;
1114 for( i = 0; i < block->NumberFrames(); i++ )
1117 DataBuffer &data = block->GetBuffer(i);
1119 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1121 if( p_block == NULL )
1126 #if defined(HAVE_ZLIB_H)
1127 if( tk.i_compression_type )
1129 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
1133 if( tk.fmt.i_cat != VIDEO_ES )
1134 p_block->i_dts = p_block->i_pts = i_pts;
1137 p_block->i_dts = i_pts;
1141 if( tk.fmt.i_cat == SPU_ES && strcmp( tk.psz_codec, "S_VOBSUB" ) )
1143 p_block->i_length = i_duration * 1000;
1145 es_out_Send( p_demux->out, tk.p_es, p_block );
1147 /* use time stamp only for first block */
1154 static void Seek( demux_t *p_demux, mtime_t i_date, int i_percent)
1156 demux_sys_t *p_sys = p_demux->p_sys;
1159 int64_t i_block_duration;
1160 int64_t i_block_ref1;
1161 int64_t i_block_ref2;
1164 int i_track_skipping;
1167 msg_Dbg( p_demux, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1168 if( i_date < 0 && i_percent < 0 )
1172 if( i_percent > 100 ) i_percent = 100;
1175 p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
1176 p_sys->cluster = NULL;
1178 /* seek without index or without date */
1179 if( config_GetInt( p_demux, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
1181 int64_t i_pos = i_percent * stream_Size( p_demux->s ) / 100;
1183 msg_Dbg( p_demux, "inacurate way of seeking" );
1184 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1186 if( p_sys->index[i_index].i_position >= i_pos)
1191 if( i_index == p_sys->i_index )
1196 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1199 if( p_sys->index[i_index].i_position < i_pos )
1203 msg_Warn( p_demux, "searching for cluster, could take some time" );
1205 /* search a cluster */
1206 while( ( el = p_sys->ep->Get() ) != NULL )
1208 if( MKV_IS_ID( el, KaxCluster ) )
1210 KaxCluster *cluster = (KaxCluster*)el;
1212 /* add it to the index */
1213 IndexAppendCluster( p_demux, cluster );
1215 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1217 p_sys->cluster = cluster;
1227 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1229 if( p_sys->index[i_index].i_time >= i_date )
1240 msg_Dbg( p_demux, "seek got "I64Fd" (%d%%)",
1241 p_sys->index[i_index].i_time,
1242 (int)( 100 * p_sys->index[i_index].i_position /
1243 stream_Size( p_demux->s ) ) );
1245 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1249 /* now parse until key frame */
1250 #define tk p_sys->track[i_track]
1251 i_track_skipping = 0;
1252 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1254 if( tk.fmt.i_cat == VIDEO_ES )
1256 tk.b_search_keyframe = VLC_TRUE;
1261 while( i_track_skipping > 0 )
1263 if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1265 msg_Warn( p_demux, "cannot get block EOF?" );
1270 p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000 + 1;
1272 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1274 if( tk.i_number == block->TrackNum() )
1280 if( i_track < p_sys->i_track )
1282 if( tk.fmt.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1284 tk.b_search_keyframe = VLC_FALSE;
1287 if( tk.fmt.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1289 BlockDecode( p_demux, block, 0, 0 );
1298 /*****************************************************************************
1299 * Demux: reads and demuxes data packets
1300 *****************************************************************************
1301 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1302 *****************************************************************************/
1303 static int Demux( demux_t *p_demux)
1305 demux_sys_t *p_sys = p_demux->p_sys;
1306 mtime_t i_start_pts;
1307 int i_block_count = 0;
1310 int64_t i_block_duration;
1311 int64_t i_block_ref1;
1312 int64_t i_block_ref2;
1318 if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1320 msg_Warn( p_demux, "cannot get block EOF?" );
1325 p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000 + 1;
1327 if( p_sys->i_pts > 0 )
1329 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
1332 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
1337 if( i_start_pts == -1 )
1339 i_start_pts = p_sys->i_pts;
1341 else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1350 /*****************************************************************************
1352 *****************************************************************************/
1353 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1359 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1361 if( i_size <= 0 || mb_eof )
1366 return stream_Read( s, p_buffer, i_size );
1368 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1374 case seek_beginning:
1378 i_pos = stream_Size( s ) - i_offset;
1381 i_pos= stream_Tell( s ) + i_offset;
1385 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1392 if( stream_Seek( s, i_pos ) )
1398 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1402 uint64 vlc_stream_io_callback::getFilePointer( void )
1404 return stream_Tell( s );
1406 void vlc_stream_io_callback::close( void )
1412 /*****************************************************************************
1413 * Ebml Stream parser
1414 *****************************************************************************/
1415 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1423 for( i = 1; i < 6; i++ )
1429 mb_keep = VLC_FALSE;
1432 EbmlParser::~EbmlParser( void )
1436 for( i = 1; i < mi_level; i++ )
1442 mb_keep = VLC_FALSE;
1446 void EbmlParser::Up( void )
1448 if( mi_user_level == mi_level )
1450 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1456 void EbmlParser::Down( void )
1462 void EbmlParser::Keep( void )
1467 int EbmlParser::GetLevel( void )
1469 return mi_user_level;
1472 EbmlElement *EbmlParser::Get( void )
1476 if( mi_user_level != mi_level )
1482 EbmlElement *ret = m_got;
1488 if( m_el[mi_level] )
1490 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
1493 delete m_el[mi_level];
1495 mb_keep = VLC_FALSE;
1498 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
1509 delete m_el[mi_level - 1];
1510 m_got = m_el[mi_level -1] = m_el[mi_level];
1511 m_el[mi_level] = NULL;
1518 else if( m_el[mi_level] == NULL )
1520 fprintf( stderr," m_el[mi_level] == NULL\n" );
1523 return m_el[mi_level];
1527 /*****************************************************************************
1529 * * LoadCues : load the cues element and update index
1531 * * LoadTags : load ... the tags element
1533 * * InformationCreate : create all information, load tags if present
1535 *****************************************************************************/
1536 static void LoadCues( demux_t *p_demux )
1538 demux_sys_t *p_sys = p_demux->p_sys;
1539 int64_t i_sav_position = p_sys->in->getFilePointer();
1541 EbmlElement *el, *cues;
1543 msg_Dbg( p_demux, "loading cues" );
1544 p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
1545 cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
1549 msg_Err( p_demux, "cannot load cues (broken seekhead or file)" );
1550 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1554 ep = new EbmlParser( p_sys->es, cues );
1555 while( ( el = ep->Get() ) != NULL )
1557 if( MKV_IS_ID( el, KaxCuePoint ) )
1559 #define idx p_sys->index[p_sys->i_index]
1562 idx.i_block_number= -1;
1563 idx.i_position = -1;
1565 idx.b_key = VLC_TRUE;
1568 while( ( el = ep->Get() ) != NULL )
1570 if( MKV_IS_ID( el, KaxCueTime ) )
1572 KaxCueTime &ctime = *(KaxCueTime*)el;
1574 ctime.ReadData( p_sys->es->I_O() );
1576 idx.i_time = uint64( ctime ) * p_sys->i_timescale / (mtime_t)1000;
1578 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
1581 while( ( el = ep->Get() ) != NULL )
1583 if( MKV_IS_ID( el, KaxCueTrack ) )
1585 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
1587 ctrack.ReadData( p_sys->es->I_O() );
1588 idx.i_track = uint16( ctrack );
1590 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
1592 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
1594 ccpos.ReadData( p_sys->es->I_O() );
1595 idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
1597 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
1599 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
1601 cbnum.ReadData( p_sys->es->I_O() );
1602 idx.i_block_number = uint32( cbnum );
1606 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1613 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1619 msg_Dbg( p_demux, " * added time="I64Fd" pos="I64Fd
1620 " track=%d bnum=%d", idx.i_time, idx.i_position,
1621 idx.i_track, idx.i_block_number );
1625 if( p_sys->i_index >= p_sys->i_index_max )
1627 p_sys->i_index_max += 1024;
1628 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
1634 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1640 p_sys->b_cues = VLC_TRUE;
1642 msg_Dbg( p_demux, "loading cues done." );
1643 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1646 static void LoadTags( demux_t *p_demux )
1648 demux_sys_t *p_sys = p_demux->p_sys;
1649 int64_t i_sav_position = p_sys->in->getFilePointer();
1651 EbmlElement *el, *tags;
1653 msg_Dbg( p_demux, "loading tags" );
1654 p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
1655 tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
1659 msg_Err( p_demux, "cannot load tags (broken seekhead or file)" );
1660 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1664 msg_Dbg( p_demux, "Tags" );
1665 ep = new EbmlParser( p_sys->es, tags );
1666 while( ( el = ep->Get() ) != NULL )
1668 if( MKV_IS_ID( el, KaxTag ) )
1670 msg_Dbg( p_demux, "+ Tag" );
1672 while( ( el = ep->Get() ) != NULL )
1674 if( MKV_IS_ID( el, KaxTagTargets ) )
1676 msg_Dbg( p_demux, "| + Targets" );
1678 while( ( el = ep->Get() ) != NULL )
1680 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1684 else if( MKV_IS_ID( el, KaxTagGeneral ) )
1686 msg_Dbg( p_demux, "| + General" );
1688 while( ( el = ep->Get() ) != NULL )
1690 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1694 else if( MKV_IS_ID( el, KaxTagGenres ) )
1696 msg_Dbg( p_demux, "| + Genres" );
1698 while( ( el = ep->Get() ) != NULL )
1700 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1704 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
1706 msg_Dbg( p_demux, "| + Audio Specific" );
1708 while( ( el = ep->Get() ) != NULL )
1710 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1714 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
1716 msg_Dbg( p_demux, "| + Images Specific" );
1718 while( ( el = ep->Get() ) != NULL )
1720 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1724 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
1726 msg_Dbg( p_demux, "| + Multi Comment" );
1728 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
1730 msg_Dbg( p_demux, "| + Multi Commercial" );
1732 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
1734 msg_Dbg( p_demux, "| + Multi Date" );
1736 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
1738 msg_Dbg( p_demux, "| + Multi Entity" );
1740 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
1742 msg_Dbg( p_demux, "| + Multi Identifier" );
1744 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
1746 msg_Dbg( p_demux, "| + Multi Legal" );
1748 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
1750 msg_Dbg( p_demux, "| + Multi Title" );
1754 msg_Dbg( p_demux, "| + Unknown (%s)", typeid( *el ).name() );
1761 msg_Dbg( p_demux, "+ Unknown (%s)", typeid( *el ).name() );
1767 msg_Dbg( p_demux, "loading tags done." );
1768 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1771 /*****************************************************************************
1773 *****************************************************************************/
1774 static void ParseSeekHead( demux_t *p_demux, EbmlElement *seekhead )
1776 demux_sys_t *p_sys = p_demux->p_sys;
1780 int i_upper_level = 0;
1782 msg_Dbg( p_demux, "| + Seek head" );
1784 /* Master elements */
1785 m = static_cast<EbmlMaster *>(seekhead);
1786 m->Read( *p_sys->es, seekhead->Generic().Context, i_upper_level, el, true );
1788 for( i = 0; i < m->ListSize(); i++ )
1790 EbmlElement *l = (*m)[i];
1792 if( MKV_IS_ID( l, KaxSeek ) )
1794 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
1795 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
1800 for( j = 0; j < sk->ListSize(); j++ )
1802 EbmlElement *l = (*sk)[j];
1804 if( MKV_IS_ID( l, KaxSeekID ) )
1806 KaxSeekID &sid = *(KaxSeekID*)l;
1807 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
1809 else if( MKV_IS_ID( l, KaxSeekPosition ) )
1811 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
1812 i_pos = uint64( spos );
1816 msg_Dbg( p_demux, "| | | + Unknown (%s)", typeid(*l).name() );
1822 if( id == KaxCues::ClassInfos.GlobalId )
1824 msg_Dbg( p_demux, "| | | = cues at "I64Fd, i_pos );
1825 p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
1827 else if( id == KaxChapters::ClassInfos.GlobalId )
1829 msg_Dbg( p_demux, "| | | = chapters at "I64Fd, i_pos );
1830 p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
1832 else if( id == KaxTags::ClassInfos.GlobalId )
1834 msg_Dbg( p_demux, "| | | = tags at "I64Fd, i_pos );
1835 p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
1841 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
1846 /*****************************************************************************
1848 *****************************************************************************/
1849 static void ParseTrackEntry( demux_t *p_demux, EbmlMaster *m )
1851 demux_sys_t *p_sys = p_demux->p_sys;
1856 msg_Dbg( p_demux, "| | + Track Entry" );
1859 p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
1861 /* Init the track */
1862 tk = &p_sys->track[p_sys->i_track - 1];
1864 memset( tk, 0, sizeof( mkv_track_t ) );
1866 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
1867 tk->fmt.psz_language = strdup("English");
1868 tk->fmt.psz_description = NULL;
1870 tk->b_default = VLC_TRUE;
1871 tk->b_enabled = VLC_TRUE;
1872 tk->i_number = p_sys->i_track - 1;
1873 tk->i_extra_data = 0;
1874 tk->p_extra_data = NULL;
1875 tk->psz_codec = NULL;
1876 tk->i_default_duration = 0;
1877 tk->f_timecodescale = 1.0;
1879 tk->b_inited = VLC_FALSE;
1880 tk->i_data_init = 0;
1881 tk->p_data_init = NULL;
1883 tk->psz_codec_name = NULL;
1884 tk->psz_codec_settings = NULL;
1885 tk->psz_codec_info_url = NULL;
1886 tk->psz_codec_download_url = NULL;
1888 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
1890 for( i = 0; i < m->ListSize(); i++ )
1892 EbmlElement *l = (*m)[i];
1894 if( MKV_IS_ID( l, KaxTrackNumber ) )
1896 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
1898 tk->i_number = uint32( tnum );
1899 msg_Dbg( p_demux, "| | | + Track Number=%u", uint32( tnum ) );
1901 else if( MKV_IS_ID( l, KaxTrackUID ) )
1903 KaxTrackUID &tuid = *(KaxTrackUID*)l;
1905 msg_Dbg( p_demux, "| | | + Track UID=%u", uint32( tuid ) );
1907 else if( MKV_IS_ID( l, KaxTrackType ) )
1910 KaxTrackType &ttype = *(KaxTrackType*)l;
1912 switch( uint8(ttype) )
1916 tk->fmt.i_cat = AUDIO_ES;
1920 tk->fmt.i_cat = VIDEO_ES;
1922 case track_subtitle:
1923 psz_type = "subtitle";
1924 tk->fmt.i_cat = SPU_ES;
1927 psz_type = "unknown";
1928 tk->fmt.i_cat = UNKNOWN_ES;
1932 msg_Dbg( p_demux, "| | | + Track Type=%s", psz_type );
1934 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
1936 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
1938 // tk->b_enabled = uint32( fenb );
1939 // msg_Dbg( p_demux, "| | | + Track Enabled=%u",
1940 // uint32( fenb ) );
1942 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
1944 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
1946 tk->b_default = uint32( fdef );
1947 msg_Dbg( p_demux, "| | | + Track Default=%u", uint32( fdef ) );
1949 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
1951 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
1953 msg_Dbg( p_demux, "| | | + Track Lacing=%d", uint32( lac ) );
1955 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
1957 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
1959 msg_Dbg( p_demux, "| | | + Track MinCache=%d", uint32( cmin ) );
1961 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
1963 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
1965 msg_Dbg( p_demux, "| | | + Track MaxCache=%d", uint32( cmax ) );
1967 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
1969 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
1971 tk->i_default_duration = uint64(defd);
1972 msg_Dbg( p_demux, "| | | + Track Default Duration="I64Fd, uint64(defd) );
1974 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
1976 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
1978 tk->f_timecodescale = float( ttcs );
1979 msg_Dbg( p_demux, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
1981 else if( MKV_IS_ID( l, KaxTrackName ) )
1983 KaxTrackName &tname = *(KaxTrackName*)l;
1985 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
1986 msg_Dbg( p_demux, "| | | + Track Name=%s", tk->fmt.psz_description );
1988 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
1990 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
1992 tk->fmt.psz_language = strdup( string( lang ).c_str() );
1994 "| | | + Track Language=`%s'", tk->fmt.psz_language );
1996 else if( MKV_IS_ID( l, KaxCodecID ) )
1998 KaxCodecID &codecid = *(KaxCodecID*)l;
2000 tk->psz_codec = strdup( string( codecid ).c_str() );
2001 msg_Dbg( p_demux, "| | | + Track CodecId=%s", string( codecid ).c_str() );
2003 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
2005 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
2007 tk->i_extra_data = cpriv.GetSize();
2008 if( tk->i_extra_data > 0 )
2010 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
2011 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
2013 msg_Dbg( p_demux, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
2015 else if( MKV_IS_ID( l, KaxCodecName ) )
2017 KaxCodecName &cname = *(KaxCodecName*)l;
2019 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
2020 msg_Dbg( p_demux, "| | | + Track Codec Name=%s", tk->psz_codec_name );
2022 else if( MKV_IS_ID( l, KaxContentEncodings ) )
2024 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
2025 MkvTree( p_demux, 3, "Content Encodings" );
2026 for( unsigned int i = 0; i < cencs->ListSize(); i++ )
2028 EbmlElement *l2 = (*cencs)[i];
2029 if( MKV_IS_ID( l2, KaxContentEncoding ) )
2031 MkvTree( p_demux, 4, "Content Encoding" );
2032 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
2033 for( unsigned int i = 0; i < cenc->ListSize(); i++ )
2035 EbmlElement *l3 = (*cenc)[i];
2036 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
2038 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
2039 MkvTree( p_demux, 5, "Order: %i", uint32( encord ) );
2041 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
2043 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
2044 MkvTree( p_demux, 5, "Scope: %i", uint32( encscope ) );
2046 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
2048 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
2049 MkvTree( p_demux, 5, "Type: %i", uint32( enctype ) );
2051 else if( MKV_IS_ID( l3, KaxContentCompression ) )
2053 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
2054 MkvTree( p_demux, 5, "Content Compression" );
2055 for( unsigned int i = 0; i < compr->ListSize(); i++ )
2057 EbmlElement *l4 = (*compr)[i];
2058 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
2060 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
2061 MkvTree( p_demux, 6, "Compression Algorithm: %i", uint32(compalg) );
2062 if( uint32( compalg ) == 0 )
2064 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
2069 MkvTree( p_demux, 6, "Unknown (%s)", typeid(*l4).name() );
2076 MkvTree( p_demux, 5, "Unknown (%s)", typeid(*l3).name() );
2083 MkvTree( p_demux, 4, "Unknown (%s)", typeid(*l2).name() );
2088 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
2090 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
2092 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
2093 // msg_Dbg( p_demux, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
2095 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
2097 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
2099 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
2100 // msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
2102 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
2104 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
2106 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
2107 // msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
2109 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
2111 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
2113 // msg_Dbg( p_demux, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
2115 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
2117 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
2119 // msg_Dbg( p_demux, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
2121 else if( MKV_IS_ID( l, KaxTrackVideo ) )
2123 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
2126 msg_Dbg( p_demux, "| | | + Track Video" );
2129 for( j = 0; j < tkv->ListSize(); j++ )
2131 EbmlElement *l = (*tkv)[j];
2132 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
2134 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
2136 // msg_Dbg( p_demux, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
2138 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
2140 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
2142 // msg_Dbg( p_demux, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
2145 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
2147 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
2149 tk->fmt.video.i_width = uint16( vwidth );
2150 msg_Dbg( p_demux, "| | | | + width=%d", uint16( vwidth ) );
2152 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
2154 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
2156 tk->fmt.video.i_height = uint16( vheight );
2157 msg_Dbg( p_demux, "| | | | + height=%d", uint16( vheight ) );
2159 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
2161 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
2163 tk->fmt.video.i_visible_width = uint16( vwidth );
2164 msg_Dbg( p_demux, "| | | | + display width=%d", uint16( vwidth ) );
2166 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
2168 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
2170 tk->fmt.video.i_visible_height = uint16( vheight );
2171 msg_Dbg( p_demux, "| | | | + display height=%d", uint16( vheight ) );
2173 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
2175 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
2177 tk->f_fps = float( vfps );
2178 msg_Dbg( p_demux, " | | | + fps=%f", float( vfps ) );
2180 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
2182 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
2184 // msg_Dbg( p_demux, "| | | | + Track Video Display Unit=%s",
2185 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
2187 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
2189 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
2191 // msg_Dbg( p_demux, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
2193 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
2195 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
2197 // msg_Dbg( p_demux, " | | | + fps=%f", float( gamma ) );
2201 msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*l).name() );
2205 else if( MKV_IS_ID( l, KaxTrackAudio ) )
2207 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
2210 msg_Dbg( p_demux, "| | | + Track Audio" );
2212 for( j = 0; j < tka->ListSize(); j++ )
2214 EbmlElement *l = (*tka)[j];
2216 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
2218 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
2220 tk->fmt.audio.i_rate = (int)float( afreq );
2221 msg_Dbg( p_demux, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
2223 else if( MKV_IS_ID( l, KaxAudioChannels ) )
2225 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
2227 tk->fmt.audio.i_channels = uint8( achan );
2228 msg_Dbg( p_demux, "| | | | + achan=%u", uint8( achan ) );
2230 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
2232 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
2234 tk->fmt.audio.i_bitspersample = uint8( abits );
2235 msg_Dbg( p_demux, "| | | | + abits=%u", uint8( abits ) );
2239 msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*l).name() );
2245 msg_Dbg( p_demux, "| | | + Unknown (%s)",
2246 typeid(*l).name() );
2251 static void ParseTracks( demux_t *p_demux, EbmlElement *tracks )
2253 demux_sys_t *p_sys = p_demux->p_sys;
2257 int i_upper_level = 0;
2259 msg_Dbg( p_demux, "| + Tracks" );
2261 /* Master elements */
2262 m = static_cast<EbmlMaster *>(tracks);
2263 m->Read( *p_sys->es, tracks->Generic().Context, i_upper_level, el, true );
2265 for( i = 0; i < m->ListSize(); i++ )
2267 EbmlElement *l = (*m)[i];
2269 if( MKV_IS_ID( l, KaxTrackEntry ) )
2271 ParseTrackEntry( p_demux, static_cast<EbmlMaster *>(l) );
2275 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2280 /*****************************************************************************
2282 *****************************************************************************/
2283 static void ParseInfo( demux_t *p_demux, EbmlElement *info )
2285 demux_sys_t *p_sys = p_demux->p_sys;
2289 int i_upper_level = 0;
2291 msg_Dbg( p_demux, "| + Information" );
2293 /* Master elements */
2294 m = static_cast<EbmlMaster *>(info);
2295 m->Read( *p_sys->es, info->Generic().Context, i_upper_level, el, true );
2297 for( i = 0; i < m->ListSize(); i++ )
2299 EbmlElement *l = (*m)[i];
2301 if( MKV_IS_ID( l, KaxSegmentUID ) )
2303 KaxSegmentUID &uid = *(KaxSegmentUID*)l;
2305 msg_Dbg( p_demux, "| | + UID=%d", uint32(uid) );
2307 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
2309 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
2311 p_sys->i_timescale = uint64(tcs);
2313 msg_Dbg( p_demux, "| | + TimecodeScale="I64Fd,
2314 p_sys->i_timescale );
2316 else if( MKV_IS_ID( l, KaxDuration ) )
2318 KaxDuration &dur = *(KaxDuration*)l;
2320 p_sys->f_duration = float(dur);
2322 msg_Dbg( p_demux, "| | + Duration=%f",
2323 p_sys->f_duration );
2325 else if( MKV_IS_ID( l, KaxMuxingApp ) )
2327 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
2329 p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
2331 msg_Dbg( p_demux, "| | + Muxing Application=%s",
2332 p_sys->psz_muxing_application );
2334 else if( MKV_IS_ID( l, KaxWritingApp ) )
2336 KaxWritingApp &wapp = *(KaxWritingApp*)l;
2338 p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
2340 msg_Dbg( p_demux, "| | + Writing Application=%s",
2341 p_sys->psz_writing_application );
2343 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
2345 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
2347 p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
2349 msg_Dbg( p_demux, "| | + Segment Filename=%s",
2350 p_sys->psz_segment_filename );
2352 else if( MKV_IS_ID( l, KaxTitle ) )
2354 KaxTitle &title = *(KaxTitle*)l;
2356 p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
2358 msg_Dbg( p_demux, "| | + Title=%s", p_sys->psz_title );
2360 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
2361 else if( MKV_IS_ID( l, KaxDateUTC ) )
2363 KaxDateUTC &date = *(KaxDateUTC*)l;
2368 i_date = date.GetEpochDate();
2369 memset( buffer, 0, 256 );
2370 if( gmtime_r( &i_date, &tmres ) &&
2371 asctime_r( &tmres, buffer ) )
2373 buffer[strlen( buffer)-1]= '\0';
2374 p_sys->psz_date_utc = strdup( buffer );
2375 msg_Dbg( p_demux, "| | + Date=%s", p_sys->psz_date_utc );
2381 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2385 p_sys->f_duration = p_sys->f_duration * p_sys->i_timescale / 1000000.0;
2389 /*****************************************************************************
2391 *****************************************************************************/
2392 static void ParseChapterAtom( demux_t *p_demux, int i_level, EbmlMaster *ca )
2394 demux_sys_t *p_sys = p_demux->p_sys;
2398 if( p_sys->title == NULL )
2400 p_sys->title = vlc_input_title_New();
2402 sk = vlc_seekpoint_New();
2404 msg_Dbg( p_demux, "| | | + ChapterAtom (level=%d)", i_level );
2405 for( i = 0; i < ca->ListSize(); i++ )
2407 EbmlElement *l = (*ca)[i];
2409 if( MKV_IS_ID( l, KaxChapterUID ) )
2411 KaxChapterUID &uid = *(KaxChapterUID*)l;
2412 uint32_t i_uid = uint32( uid );
2413 msg_Dbg( p_demux, "| | | | + ChapterUID: 0x%x", i_uid );
2415 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
2417 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
2418 sk->i_time_offset = uint64( start ) / I64C(1000);
2420 msg_Dbg( p_demux, "| | | | + ChapterTimeStart: %lld", sk->i_time_offset );
2422 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
2424 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
2425 int64_t i_end = uint64( end );
2427 msg_Dbg( p_demux, "| | | | + ChapterTimeEnd: %lld", i_end );
2429 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
2431 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
2434 msg_Dbg( p_demux, "| | | | + ChapterDisplay" );
2435 for( j = 0; j < cd->ListSize(); j++ )
2437 EbmlElement *l= (*cd)[j];
2439 if( MKV_IS_ID( l, KaxChapterString ) )
2441 KaxChapterString &name =*(KaxChapterString*)l;
2442 char *psz = UTF8ToStr( UTFstring( name ) );
2443 sk->psz_name = strdup( psz );
2444 msg_Dbg( p_demux, "| | | | | + ChapterString '%s'", psz );
2446 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
2448 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
2449 const char *psz = string( lang ).c_str();
2451 msg_Dbg( p_demux, "| | | | | + ChapterLanguage '%s'", psz );
2453 else if( MKV_IS_ID( l, KaxChapterCountry ) )
2455 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
2456 const char *psz = string( ct ).c_str();
2458 msg_Dbg( p_demux, "| | | | | + ChapterCountry '%s'", psz );
2462 else if( MKV_IS_ID( l, KaxChapterAtom ) )
2464 ParseChapterAtom( p_demux, i_level+1, static_cast<EbmlMaster *>(l) );
2467 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
2468 p_sys->title->i_seekpoint++;
2469 p_sys->title->seekpoint = (seekpoint_t**)realloc( p_sys->title->seekpoint, p_sys->title->i_seekpoint * sizeof( seekpoint_t* ) );
2470 p_sys->title->seekpoint[p_sys->title->i_seekpoint-1] = sk;
2473 /*****************************************************************************
2475 *****************************************************************************/
2476 static void ParseChapters( demux_t *p_demux, EbmlElement *chapters )
2478 demux_sys_t *p_sys = p_demux->p_sys;
2482 int i_upper_level = 0;
2485 /* Master elements */
2486 m = static_cast<EbmlMaster *>(chapters);
2487 m->Read( *p_sys->es, chapters->Generic().Context, i_upper_level, el, true );
2489 for( i = 0; i < m->ListSize(); i++ )
2491 EbmlElement *l = (*m)[i];
2493 if( MKV_IS_ID( l, KaxEditionEntry ) )
2495 EbmlMaster *E = static_cast<EbmlMaster *>(l );
2497 msg_Dbg( p_demux, "| | + EditionEntry" );
2498 for( j = 0; j < E->ListSize(); j++ )
2500 EbmlElement *l = (*E)[j];
2502 if( MKV_IS_ID( l, KaxChapterAtom ) )
2504 ParseChapterAtom( p_demux, 0, static_cast<EbmlMaster *>(l) );
2508 msg_Dbg( p_demux, "| | | + Unknown (%s)", typeid(*l).name() );
2514 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2519 /*****************************************************************************
2520 * InformationCreate:
2521 *****************************************************************************/
2522 static void InformationCreate( demux_t *p_demux )
2524 demux_sys_t *p_sys = p_demux->p_sys;
2527 p_sys->meta = vlc_meta_New();
2529 if( p_sys->psz_title )
2531 vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_sys->psz_title );
2533 if( p_sys->psz_date_utc )
2535 vlc_meta_Add( p_sys->meta, VLC_META_DATE, p_sys->psz_date_utc );
2537 if( p_sys->psz_segment_filename )
2539 vlc_meta_Add( p_sys->meta, _("Segment filename"), p_sys->psz_segment_filename );
2541 if( p_sys->psz_muxing_application )
2543 vlc_meta_Add( p_sys->meta, _("Muxing application"), p_sys->psz_muxing_application );
2545 if( p_sys->psz_writing_application )
2547 vlc_meta_Add( p_sys->meta, _("Writing application"), p_sys->psz_writing_application );
2550 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
2552 mkv_track_t *tk = &p_sys->track[i_track];
2553 vlc_meta_t *mtk = vlc_meta_New();
2555 p_sys->meta->track = (vlc_meta_t**)realloc( p_sys->meta->track,
2556 sizeof( vlc_meta_t * ) * ( p_sys->meta->i_track + 1 ) );
2557 p_sys->meta->track[p_sys->meta->i_track++] = mtk;
2559 if( tk->fmt.psz_description )
2561 vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
2563 if( tk->psz_codec_name )
2565 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
2567 if( tk->psz_codec_settings )
2569 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, tk->psz_codec_settings );
2571 if( tk->psz_codec_info_url )
2573 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
2575 if( tk->psz_codec_download_url )
2577 vlc_meta_Add( p_sys->meta, VLC_META_URL, tk->psz_codec_download_url );
2581 if( p_sys->i_tags_position >= 0 )
2583 vlc_bool_t b_seekable;
2585 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
2588 LoadTags( p_demux );
2594 /*****************************************************************************
2596 *****************************************************************************/
2598 static void IndexAppendCluster( demux_t *p_demux, KaxCluster *cluster )
2600 demux_sys_t *p_sys = p_demux->p_sys;
2602 #define idx p_sys->index[p_sys->i_index]
2604 idx.i_block_number= -1;
2605 idx.i_position = cluster->GetElementPosition();
2607 idx.b_key = VLC_TRUE;
2610 if( p_sys->i_index >= p_sys->i_index_max )
2612 p_sys->i_index_max += 1024;
2613 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2618 static char * UTF8ToStr( const UTFstring &u )
2627 p = dst = (char*)malloc( i_src + 1);