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 MP4_Stream_t *p_mp4_stream = MP4_MemoryStream( p_demux->s,
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 );
590 free( p_mp4_stream );
592 else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
594 if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
596 msg_Err( p_demux, "missing/invalid WAVEFORMATEX" );
597 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
601 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;
603 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk.fmt.i_codec, NULL );
605 tk.fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
606 tk.fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
607 tk.fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
608 tk.fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
609 tk.fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
611 tk.fmt.i_extra = GetWLE( &p_wf->cbSize );
612 if( tk.fmt.i_extra > 0 )
614 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
615 memcpy( tk.fmt.p_extra, &p_wf[1], tk.fmt.i_extra );
619 else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
620 !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
621 !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
623 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
625 else if( !strcmp( tk.psz_codec, "A_AC3" ) )
627 tk.fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
629 else if( !strcmp( tk.psz_codec, "A_DTS" ) )
631 tk.fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
633 else if( !strcmp( tk.psz_codec, "A_FLAC" ) )
635 tk.fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
636 tk.fmt.i_extra = tk.i_extra_data;
637 tk.fmt.p_extra = malloc( tk.i_extra_data );
638 memcpy( tk.fmt.p_extra,tk.p_extra_data, tk.i_extra_data );
640 else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
642 int i, i_offset = 1, i_size[3], i_extra;
645 tk.fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
647 /* Split the 3 headers */
648 if( tk.p_extra_data[0] != 0x02 )
649 msg_Err( p_demux, "invalid vorbis header" );
651 for( i = 0; i < 2; i++ )
654 while( i_offset < tk.i_extra_data )
656 i_size[i] += tk.p_extra_data[i_offset];
657 if( tk.p_extra_data[i_offset++] != 0xff ) break;
661 i_size[0] = __MIN(i_size[0], tk.i_extra_data - i_offset);
662 i_size[1] = __MIN(i_size[1], tk.i_extra_data -i_offset -i_size[0]);
663 i_size[2] = tk.i_extra_data - i_offset - i_size[0] - i_size[1];
665 tk.fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
666 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
667 p_extra = (uint8_t *)tk.fmt.p_extra; i_extra = 0;
668 for( i = 0; i < 3; i++ )
670 *(p_extra++) = i_size[i] >> 8;
671 *(p_extra++) = i_size[i] & 0xFF;
672 memcpy( p_extra, tk.p_extra_data + i_offset + i_extra,
674 p_extra += i_size[i];
675 i_extra += i_size[i];
678 else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
679 !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
681 int i_profile, i_srate;
682 static unsigned int i_sample_rates[] =
684 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
685 16000, 12000, 11025, 8000, 7350, 0, 0, 0
688 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
689 /* create data for faad (MP4DecSpecificDescrTag)*/
691 if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
695 else if( !strcmp( &tk.psz_codec[12], "LC" ) )
699 else if( !strcmp( &tk.psz_codec[12], "SSR" ) )
708 for( i_srate = 0; i_srate < 13; i_srate++ )
710 if( i_sample_rates[i_srate] == tk.fmt.audio.i_rate )
715 msg_Dbg( p_demux, "profile=%d srate=%d", i_profile, i_srate );
718 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
719 ((uint8_t*)tk.fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
720 ((uint8_t*)tk.fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk.fmt.audio.i_channels << 3);
722 else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
723 !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
724 !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
726 if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
728 tk.fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
732 tk.fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
734 tk.fmt.audio.i_blockalign = ( tk.fmt.audio.i_bitspersample + 7 ) / 8 * tk.fmt.audio.i_channels;
736 else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
738 tk.fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
739 tk.fmt.subs.psz_encoding = strdup( "UTF-8" );
741 else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) ||
742 !strcmp( tk.psz_codec, "S_TEXT/ASS" ) ||
743 !strcmp( tk.psz_codec, "S_SSA" ) ||
744 !strcmp( tk.psz_codec, "S_ASS" ))
746 tk.fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
747 tk.fmt.subs.psz_encoding = strdup( "UTF-8" );
749 else if( !strcmp( tk.psz_codec, "S_VOBSUB" ) )
751 tk.fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
752 if( tk.i_extra_data )
755 char *p_buf = (char *)malloc( tk.i_extra_data + 1);
756 memcpy( p_buf, tk.p_extra_data , tk.i_extra_data );
757 p_buf[tk.i_extra_data] = '\0';
759 p_start = strstr( p_buf, "size:" );
760 if( sscanf( p_start, "size: %dx%d",
761 &tk.fmt.subs.spu.i_original_frame_width, &tk.fmt.subs.spu.i_original_frame_height ) == 2 )
763 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 );
767 msg_Warn( p_demux, "reading original frame size for vobsub failed" );
774 msg_Err( p_demux, "unknow codec id=`%s'", tk.psz_codec );
775 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
779 tk.fmt.i_priority = 1000;
782 tk.p_es = es_out_Add( p_demux->out, &tk.fmt );
786 /* add information */
787 InformationCreate( p_demux );
798 /*****************************************************************************
799 * Close: frees unused data
800 *****************************************************************************/
801 static void Close( vlc_object_t *p_this )
803 demux_t *p_demux = (demux_t*)p_this;
804 demux_sys_t *p_sys = p_demux->p_sys;
807 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
809 #define tk p_sys->track[i_track]
810 if( tk.fmt.psz_description )
812 free( tk.fmt.psz_description );
816 free( tk.psz_codec );
818 if( tk.fmt.psz_language )
820 free( tk.fmt.psz_language );
824 free( p_sys->track );
826 if( p_sys->psz_writing_application )
828 free( p_sys->psz_writing_application );
830 if( p_sys->psz_muxing_application )
832 free( p_sys->psz_muxing_application );
835 delete p_sys->segment;
844 /*****************************************************************************
846 *****************************************************************************/
847 static int Control( demux_t *p_demux, int i_query, va_list args )
849 demux_sys_t *p_sys = p_demux->p_sys;
853 vlc_meta_t **pp_meta;
858 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
859 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
862 case DEMUX_GET_LENGTH:
863 pi64 = (int64_t*)va_arg( args, int64_t * );
864 if( p_sys->f_duration > 0.0 )
866 *pi64 = (int64_t)(p_sys->f_duration * 1000);
871 case DEMUX_GET_POSITION:
872 pf = (double*)va_arg( args, double * );
873 *pf = (double)p_sys->in->getFilePointer() / (double)stream_Size( p_demux->s );
876 case DEMUX_SET_POSITION:
877 f = (double)va_arg( args, double );
878 Seek( p_demux, -1, (int)(100.0 * f) );
882 pi64 = (int64_t*)va_arg( args, int64_t * );
883 if( p_sys->f_duration > 0.0 )
885 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
888 *pi64 = (mtime_t)1000000 *
890 (mtime_t)p_sys->in->getFilePointer() /
891 (mtime_t)stream_Size( p_demux->s );
896 case DEMUX_GET_TITLE_INFO:
897 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
899 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
900 int *pi_int = (int*)va_arg( args, int* );
903 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) );
905 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->title );
911 case DEMUX_SET_TITLE:
912 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
918 case DEMUX_SET_SEEKPOINT:
919 /* FIXME do a better implementation */
920 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
922 int i_skp = (int)va_arg( args, int );
924 Seek( p_demux, (int64_t)p_sys->title->seekpoint[i_skp]->i_time_offset, -1);
937 static int BlockGet( demux_t *p_demux, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
939 demux_sys_t *p_sys = p_demux->p_sys;
955 el = p_sys->ep->Get();
956 i_level = p_sys->ep->GetLevel();
958 if( el == NULL && *pp_block != NULL )
960 /* update the index */
961 #define idx p_sys->index[p_sys->i_index - 1]
962 if( p_sys->i_index > 0 && idx.i_time == -1 )
964 idx.i_time = (*pp_block)->GlobalTimecode() / (mtime_t)1000;
965 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
973 if( p_sys->ep->GetLevel() > 1 )
978 msg_Warn( p_demux, "EOF" );
985 if( MKV_IS_ID( el, KaxCluster ) )
987 p_sys->cluster = (KaxCluster*)el;
989 /* add it to the index */
990 if( p_sys->i_index == 0 ||
991 ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
993 IndexAppendCluster( p_demux, p_sys->cluster );
998 else if( MKV_IS_ID( el, KaxCues ) )
1000 msg_Warn( p_demux, "find KaxCues FIXME" );
1001 return VLC_EGENERIC;
1005 msg_Dbg( p_demux, "unknown (%s)", typeid( el ).name() );
1008 else if( i_level == 2 )
1010 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1012 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1014 ctc.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
1015 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
1017 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1022 else if( i_level == 3 )
1024 if( MKV_IS_ID( el, KaxBlock ) )
1026 *pp_block = (KaxBlock*)el;
1028 (*pp_block)->ReadData( p_sys->es->I_O() );
1029 (*pp_block)->SetParent( *p_sys->cluster );
1033 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1035 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1037 dur.ReadData( p_sys->es->I_O() );
1038 *pi_duration = uint64( dur );
1040 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1042 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1044 ref.ReadData( p_sys->es->I_O() );
1045 if( *pi_ref1 == -1 )
1047 *pi_ref1 = int64( ref );
1051 *pi_ref2 = int64( ref );
1057 msg_Err( p_demux, "invalid level = %d", i_level );
1058 return VLC_EGENERIC;
1063 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1066 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1067 memcpy( p_block->p_buffer, p_mem, i_mem );
1068 //p_block->i_rate = p_input->stream.control.i_rate;
1072 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1073 mtime_t i_duration )
1075 demux_sys_t *p_sys = p_demux->p_sys;
1081 #define tk p_sys->track[i_track]
1082 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1084 if( tk.i_number == block->TrackNum() )
1090 if( i_track >= p_sys->i_track )
1092 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1096 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk.p_es, &b );
1099 tk.b_inited = VLC_FALSE;
1103 /* First send init data */
1104 if( !tk.b_inited && tk.i_data_init > 0 )
1108 msg_Dbg( p_demux, "sending header (%d bytes)", tk.i_data_init );
1109 p_init = MemToBlock( p_demux, tk.p_data_init, tk.i_data_init );
1110 if( p_init ) es_out_Send( p_demux->out, tk.p_es, p_init );
1112 tk.b_inited = VLC_TRUE;
1115 for( i = 0; i < block->NumberFrames(); i++ )
1118 DataBuffer &data = block->GetBuffer(i);
1120 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1122 if( p_block == NULL )
1127 #if defined(HAVE_ZLIB_H)
1128 if( tk.i_compression_type )
1130 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
1134 if( tk.fmt.i_cat != VIDEO_ES )
1135 p_block->i_dts = p_block->i_pts = i_pts;
1138 p_block->i_dts = i_pts;
1142 if( tk.fmt.i_cat == SPU_ES && strcmp( tk.psz_codec, "S_VOBSUB" ) )
1144 p_block->i_length = i_duration * 1000;
1146 es_out_Send( p_demux->out, tk.p_es, p_block );
1148 /* use time stamp only for first block */
1155 static void Seek( demux_t *p_demux, mtime_t i_date, int i_percent)
1157 demux_sys_t *p_sys = p_demux->p_sys;
1160 int64_t i_block_duration;
1161 int64_t i_block_ref1;
1162 int64_t i_block_ref2;
1165 int i_track_skipping;
1168 msg_Dbg( p_demux, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1169 if( i_date < 0 && i_percent < 0 )
1173 if( i_percent > 100 ) i_percent = 100;
1176 p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
1177 p_sys->cluster = NULL;
1179 /* seek without index or without date */
1180 if( config_GetInt( p_demux, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
1182 int64_t i_pos = i_percent * stream_Size( p_demux->s ) / 100;
1184 msg_Dbg( p_demux, "inacurate way of seeking" );
1185 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1187 if( p_sys->index[i_index].i_position >= i_pos)
1192 if( i_index == p_sys->i_index )
1197 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1200 if( p_sys->index[i_index].i_position < i_pos )
1204 msg_Warn( p_demux, "searching for cluster, could take some time" );
1206 /* search a cluster */
1207 while( ( el = p_sys->ep->Get() ) != NULL )
1209 if( MKV_IS_ID( el, KaxCluster ) )
1211 KaxCluster *cluster = (KaxCluster*)el;
1213 /* add it to the index */
1214 IndexAppendCluster( p_demux, cluster );
1216 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1218 p_sys->cluster = cluster;
1228 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1230 if( p_sys->index[i_index].i_time >= i_date )
1241 msg_Dbg( p_demux, "seek got "I64Fd" (%d%%)",
1242 p_sys->index[i_index].i_time,
1243 (int)( 100 * p_sys->index[i_index].i_position /
1244 stream_Size( p_demux->s ) ) );
1246 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1250 /* now parse until key frame */
1251 #define tk p_sys->track[i_track]
1252 i_track_skipping = 0;
1253 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1255 if( tk.fmt.i_cat == VIDEO_ES )
1257 tk.b_search_keyframe = VLC_TRUE;
1262 while( i_track_skipping > 0 )
1264 if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1266 msg_Warn( p_demux, "cannot get block EOF?" );
1271 p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000 + 1;
1273 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1275 if( tk.i_number == block->TrackNum() )
1281 if( i_track < p_sys->i_track )
1283 if( tk.fmt.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1285 tk.b_search_keyframe = VLC_FALSE;
1288 if( tk.fmt.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1290 BlockDecode( p_demux, block, 0, 0 );
1299 /*****************************************************************************
1300 * Demux: reads and demuxes data packets
1301 *****************************************************************************
1302 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1303 *****************************************************************************/
1304 static int Demux( demux_t *p_demux)
1306 demux_sys_t *p_sys = p_demux->p_sys;
1307 mtime_t i_start_pts;
1308 int i_block_count = 0;
1311 int64_t i_block_duration;
1312 int64_t i_block_ref1;
1313 int64_t i_block_ref2;
1319 if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1321 msg_Warn( p_demux, "cannot get block EOF?" );
1326 p_sys->i_pts = block->GlobalTimecode() / (mtime_t) 1000 + 1;
1328 if( p_sys->i_pts > 0 )
1330 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
1333 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
1338 if( i_start_pts == -1 )
1340 i_start_pts = p_sys->i_pts;
1342 else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1351 /*****************************************************************************
1353 *****************************************************************************/
1354 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1360 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1362 if( i_size <= 0 || mb_eof )
1367 return stream_Read( s, p_buffer, i_size );
1369 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1375 case seek_beginning:
1379 i_pos = stream_Size( s ) - i_offset;
1382 i_pos= stream_Tell( s ) + i_offset;
1386 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1393 if( stream_Seek( s, i_pos ) )
1399 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1403 uint64 vlc_stream_io_callback::getFilePointer( void )
1405 return stream_Tell( s );
1407 void vlc_stream_io_callback::close( void )
1413 /*****************************************************************************
1414 * Ebml Stream parser
1415 *****************************************************************************/
1416 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1424 for( i = 1; i < 6; i++ )
1430 mb_keep = VLC_FALSE;
1433 EbmlParser::~EbmlParser( void )
1437 for( i = 1; i < mi_level; i++ )
1443 mb_keep = VLC_FALSE;
1447 void EbmlParser::Up( void )
1449 if( mi_user_level == mi_level )
1451 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1457 void EbmlParser::Down( void )
1463 void EbmlParser::Keep( void )
1468 int EbmlParser::GetLevel( void )
1470 return mi_user_level;
1473 EbmlElement *EbmlParser::Get( void )
1477 if( mi_user_level != mi_level )
1483 EbmlElement *ret = m_got;
1489 if( m_el[mi_level] )
1491 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
1494 delete m_el[mi_level];
1496 mb_keep = VLC_FALSE;
1499 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
1510 delete m_el[mi_level - 1];
1511 m_got = m_el[mi_level -1] = m_el[mi_level];
1512 m_el[mi_level] = NULL;
1519 else if( m_el[mi_level] == NULL )
1521 fprintf( stderr," m_el[mi_level] == NULL\n" );
1524 return m_el[mi_level];
1528 /*****************************************************************************
1530 * * LoadCues : load the cues element and update index
1532 * * LoadTags : load ... the tags element
1534 * * InformationCreate : create all information, load tags if present
1536 *****************************************************************************/
1537 static void LoadCues( demux_t *p_demux )
1539 demux_sys_t *p_sys = p_demux->p_sys;
1540 int64_t i_sav_position = p_sys->in->getFilePointer();
1542 EbmlElement *el, *cues;
1544 msg_Dbg( p_demux, "loading cues" );
1545 p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
1546 cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
1550 msg_Err( p_demux, "cannot load cues (broken seekhead or file)" );
1551 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1555 ep = new EbmlParser( p_sys->es, cues );
1556 while( ( el = ep->Get() ) != NULL )
1558 if( MKV_IS_ID( el, KaxCuePoint ) )
1560 #define idx p_sys->index[p_sys->i_index]
1563 idx.i_block_number= -1;
1564 idx.i_position = -1;
1566 idx.b_key = VLC_TRUE;
1569 while( ( el = ep->Get() ) != NULL )
1571 if( MKV_IS_ID( el, KaxCueTime ) )
1573 KaxCueTime &ctime = *(KaxCueTime*)el;
1575 ctime.ReadData( p_sys->es->I_O() );
1577 idx.i_time = uint64( ctime ) * p_sys->i_timescale / (mtime_t)1000;
1579 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
1582 while( ( el = ep->Get() ) != NULL )
1584 if( MKV_IS_ID( el, KaxCueTrack ) )
1586 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
1588 ctrack.ReadData( p_sys->es->I_O() );
1589 idx.i_track = uint16( ctrack );
1591 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
1593 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
1595 ccpos.ReadData( p_sys->es->I_O() );
1596 idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
1598 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
1600 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
1602 cbnum.ReadData( p_sys->es->I_O() );
1603 idx.i_block_number = uint32( cbnum );
1607 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1614 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1620 msg_Dbg( p_demux, " * added time="I64Fd" pos="I64Fd
1621 " track=%d bnum=%d", idx.i_time, idx.i_position,
1622 idx.i_track, idx.i_block_number );
1626 if( p_sys->i_index >= p_sys->i_index_max )
1628 p_sys->i_index_max += 1024;
1629 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
1635 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1641 p_sys->b_cues = VLC_TRUE;
1643 msg_Dbg( p_demux, "loading cues done." );
1644 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1647 static void LoadTags( demux_t *p_demux )
1649 demux_sys_t *p_sys = p_demux->p_sys;
1650 int64_t i_sav_position = p_sys->in->getFilePointer();
1652 EbmlElement *el, *tags;
1654 msg_Dbg( p_demux, "loading tags" );
1655 p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
1656 tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
1660 msg_Err( p_demux, "cannot load tags (broken seekhead or file)" );
1661 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1665 msg_Dbg( p_demux, "Tags" );
1666 ep = new EbmlParser( p_sys->es, tags );
1667 while( ( el = ep->Get() ) != NULL )
1669 if( MKV_IS_ID( el, KaxTag ) )
1671 msg_Dbg( p_demux, "+ Tag" );
1673 while( ( el = ep->Get() ) != NULL )
1675 if( MKV_IS_ID( el, KaxTagTargets ) )
1677 msg_Dbg( p_demux, "| + Targets" );
1679 while( ( el = ep->Get() ) != NULL )
1681 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1685 else if( MKV_IS_ID( el, KaxTagGeneral ) )
1687 msg_Dbg( p_demux, "| + General" );
1689 while( ( el = ep->Get() ) != NULL )
1691 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1695 else if( MKV_IS_ID( el, KaxTagGenres ) )
1697 msg_Dbg( p_demux, "| + Genres" );
1699 while( ( el = ep->Get() ) != NULL )
1701 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1705 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
1707 msg_Dbg( p_demux, "| + Audio Specific" );
1709 while( ( el = ep->Get() ) != NULL )
1711 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1715 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
1717 msg_Dbg( p_demux, "| + Images Specific" );
1719 while( ( el = ep->Get() ) != NULL )
1721 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1725 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
1727 msg_Dbg( p_demux, "| + Multi Comment" );
1729 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
1731 msg_Dbg( p_demux, "| + Multi Commercial" );
1733 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
1735 msg_Dbg( p_demux, "| + Multi Date" );
1737 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
1739 msg_Dbg( p_demux, "| + Multi Entity" );
1741 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
1743 msg_Dbg( p_demux, "| + Multi Identifier" );
1745 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
1747 msg_Dbg( p_demux, "| + Multi Legal" );
1749 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
1751 msg_Dbg( p_demux, "| + Multi Title" );
1755 msg_Dbg( p_demux, "| + Unknown (%s)", typeid( *el ).name() );
1762 msg_Dbg( p_demux, "+ Unknown (%s)", typeid( *el ).name() );
1768 msg_Dbg( p_demux, "loading tags done." );
1769 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1772 /*****************************************************************************
1774 *****************************************************************************/
1775 static void ParseSeekHead( demux_t *p_demux, EbmlElement *seekhead )
1777 demux_sys_t *p_sys = p_demux->p_sys;
1781 int i_upper_level = 0;
1783 msg_Dbg( p_demux, "| + Seek head" );
1785 /* Master elements */
1786 m = static_cast<EbmlMaster *>(seekhead);
1787 m->Read( *p_sys->es, seekhead->Generic().Context, i_upper_level, el, true );
1789 for( i = 0; i < m->ListSize(); i++ )
1791 EbmlElement *l = (*m)[i];
1793 if( MKV_IS_ID( l, KaxSeek ) )
1795 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
1796 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
1801 for( j = 0; j < sk->ListSize(); j++ )
1803 EbmlElement *l = (*sk)[j];
1805 if( MKV_IS_ID( l, KaxSeekID ) )
1807 KaxSeekID &sid = *(KaxSeekID*)l;
1808 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
1810 else if( MKV_IS_ID( l, KaxSeekPosition ) )
1812 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
1813 i_pos = uint64( spos );
1817 msg_Dbg( p_demux, "| | | + Unknown (%s)", typeid(*l).name() );
1823 if( id == KaxCues::ClassInfos.GlobalId )
1825 msg_Dbg( p_demux, "| | | = cues at "I64Fd, i_pos );
1826 p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
1828 else if( id == KaxChapters::ClassInfos.GlobalId )
1830 msg_Dbg( p_demux, "| | | = chapters at "I64Fd, i_pos );
1831 p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
1833 else if( id == KaxTags::ClassInfos.GlobalId )
1835 msg_Dbg( p_demux, "| | | = tags at "I64Fd, i_pos );
1836 p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
1842 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
1847 /*****************************************************************************
1849 *****************************************************************************/
1850 static void ParseTrackEntry( demux_t *p_demux, EbmlMaster *m )
1852 demux_sys_t *p_sys = p_demux->p_sys;
1857 msg_Dbg( p_demux, "| | + Track Entry" );
1860 p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
1862 /* Init the track */
1863 tk = &p_sys->track[p_sys->i_track - 1];
1865 memset( tk, 0, sizeof( mkv_track_t ) );
1867 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
1868 tk->fmt.psz_language = strdup("English");
1869 tk->fmt.psz_description = NULL;
1871 tk->b_default = VLC_TRUE;
1872 tk->b_enabled = VLC_TRUE;
1873 tk->i_number = p_sys->i_track - 1;
1874 tk->i_extra_data = 0;
1875 tk->p_extra_data = NULL;
1876 tk->psz_codec = NULL;
1877 tk->i_default_duration = 0;
1878 tk->f_timecodescale = 1.0;
1880 tk->b_inited = VLC_FALSE;
1881 tk->i_data_init = 0;
1882 tk->p_data_init = NULL;
1884 tk->psz_codec_name = NULL;
1885 tk->psz_codec_settings = NULL;
1886 tk->psz_codec_info_url = NULL;
1887 tk->psz_codec_download_url = NULL;
1889 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
1891 for( i = 0; i < m->ListSize(); i++ )
1893 EbmlElement *l = (*m)[i];
1895 if( MKV_IS_ID( l, KaxTrackNumber ) )
1897 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
1899 tk->i_number = uint32( tnum );
1900 msg_Dbg( p_demux, "| | | + Track Number=%u", uint32( tnum ) );
1902 else if( MKV_IS_ID( l, KaxTrackUID ) )
1904 KaxTrackUID &tuid = *(KaxTrackUID*)l;
1906 msg_Dbg( p_demux, "| | | + Track UID=%u", uint32( tuid ) );
1908 else if( MKV_IS_ID( l, KaxTrackType ) )
1911 KaxTrackType &ttype = *(KaxTrackType*)l;
1913 switch( uint8(ttype) )
1917 tk->fmt.i_cat = AUDIO_ES;
1921 tk->fmt.i_cat = VIDEO_ES;
1923 case track_subtitle:
1924 psz_type = "subtitle";
1925 tk->fmt.i_cat = SPU_ES;
1928 psz_type = "unknown";
1929 tk->fmt.i_cat = UNKNOWN_ES;
1933 msg_Dbg( p_demux, "| | | + Track Type=%s", psz_type );
1935 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
1937 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
1939 // tk->b_enabled = uint32( fenb );
1940 // msg_Dbg( p_demux, "| | | + Track Enabled=%u",
1941 // uint32( fenb ) );
1943 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
1945 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
1947 tk->b_default = uint32( fdef );
1948 msg_Dbg( p_demux, "| | | + Track Default=%u", uint32( fdef ) );
1950 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
1952 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
1954 msg_Dbg( p_demux, "| | | + Track Lacing=%d", uint32( lac ) );
1956 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
1958 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
1960 msg_Dbg( p_demux, "| | | + Track MinCache=%d", uint32( cmin ) );
1962 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
1964 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
1966 msg_Dbg( p_demux, "| | | + Track MaxCache=%d", uint32( cmax ) );
1968 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
1970 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
1972 tk->i_default_duration = uint64(defd);
1973 msg_Dbg( p_demux, "| | | + Track Default Duration="I64Fd, uint64(defd) );
1975 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
1977 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
1979 tk->f_timecodescale = float( ttcs );
1980 msg_Dbg( p_demux, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
1982 else if( MKV_IS_ID( l, KaxTrackName ) )
1984 KaxTrackName &tname = *(KaxTrackName*)l;
1986 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
1987 msg_Dbg( p_demux, "| | | + Track Name=%s", tk->fmt.psz_description );
1989 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
1991 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
1993 tk->fmt.psz_language = strdup( string( lang ).c_str() );
1995 "| | | + Track Language=`%s'", tk->fmt.psz_language );
1997 else if( MKV_IS_ID( l, KaxCodecID ) )
1999 KaxCodecID &codecid = *(KaxCodecID*)l;
2001 tk->psz_codec = strdup( string( codecid ).c_str() );
2002 msg_Dbg( p_demux, "| | | + Track CodecId=%s", string( codecid ).c_str() );
2004 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
2006 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
2008 tk->i_extra_data = cpriv.GetSize();
2009 if( tk->i_extra_data > 0 )
2011 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
2012 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
2014 msg_Dbg( p_demux, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
2016 else if( MKV_IS_ID( l, KaxCodecName ) )
2018 KaxCodecName &cname = *(KaxCodecName*)l;
2020 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
2021 msg_Dbg( p_demux, "| | | + Track Codec Name=%s", tk->psz_codec_name );
2023 else if( MKV_IS_ID( l, KaxContentEncodings ) )
2025 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
2026 MkvTree( p_demux, 3, "Content Encodings" );
2027 for( unsigned int i = 0; i < cencs->ListSize(); i++ )
2029 EbmlElement *l2 = (*cencs)[i];
2030 if( MKV_IS_ID( l2, KaxContentEncoding ) )
2032 MkvTree( p_demux, 4, "Content Encoding" );
2033 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
2034 for( unsigned int i = 0; i < cenc->ListSize(); i++ )
2036 EbmlElement *l3 = (*cenc)[i];
2037 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
2039 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
2040 MkvTree( p_demux, 5, "Order: %i", uint32( encord ) );
2042 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
2044 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
2045 MkvTree( p_demux, 5, "Scope: %i", uint32( encscope ) );
2047 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
2049 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
2050 MkvTree( p_demux, 5, "Type: %i", uint32( enctype ) );
2052 else if( MKV_IS_ID( l3, KaxContentCompression ) )
2054 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
2055 MkvTree( p_demux, 5, "Content Compression" );
2056 for( unsigned int i = 0; i < compr->ListSize(); i++ )
2058 EbmlElement *l4 = (*compr)[i];
2059 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
2061 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
2062 MkvTree( p_demux, 6, "Compression Algorithm: %i", uint32(compalg) );
2063 if( uint32( compalg ) == 0 )
2065 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
2070 MkvTree( p_demux, 6, "Unknown (%s)", typeid(*l4).name() );
2077 MkvTree( p_demux, 5, "Unknown (%s)", typeid(*l3).name() );
2084 MkvTree( p_demux, 4, "Unknown (%s)", typeid(*l2).name() );
2089 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
2091 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
2093 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
2094 // msg_Dbg( p_demux, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
2096 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
2098 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
2100 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
2101 // msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
2103 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
2105 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
2107 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
2108 // msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
2110 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
2112 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
2114 // msg_Dbg( p_demux, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
2116 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
2118 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
2120 // msg_Dbg( p_demux, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
2122 else if( MKV_IS_ID( l, KaxTrackVideo ) )
2124 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
2127 msg_Dbg( p_demux, "| | | + Track Video" );
2130 for( j = 0; j < tkv->ListSize(); j++ )
2132 EbmlElement *l = (*tkv)[j];
2133 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
2135 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
2137 // msg_Dbg( p_demux, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
2139 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
2141 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
2143 // msg_Dbg( p_demux, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
2146 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
2148 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
2150 tk->fmt.video.i_width = uint16( vwidth );
2151 msg_Dbg( p_demux, "| | | | + width=%d", uint16( vwidth ) );
2153 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
2155 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
2157 tk->fmt.video.i_height = uint16( vheight );
2158 msg_Dbg( p_demux, "| | | | + height=%d", uint16( vheight ) );
2160 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
2162 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
2164 tk->fmt.video.i_visible_width = uint16( vwidth );
2165 msg_Dbg( p_demux, "| | | | + display width=%d", uint16( vwidth ) );
2167 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
2169 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
2171 tk->fmt.video.i_visible_height = uint16( vheight );
2172 msg_Dbg( p_demux, "| | | | + display height=%d", uint16( vheight ) );
2174 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
2176 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
2178 tk->f_fps = float( vfps );
2179 msg_Dbg( p_demux, " | | | + fps=%f", float( vfps ) );
2181 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
2183 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
2185 // msg_Dbg( p_demux, "| | | | + Track Video Display Unit=%s",
2186 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
2188 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
2190 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
2192 // msg_Dbg( p_demux, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
2194 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
2196 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
2198 // msg_Dbg( p_demux, " | | | + fps=%f", float( gamma ) );
2202 msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*l).name() );
2206 else if( MKV_IS_ID( l, KaxTrackAudio ) )
2208 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
2211 msg_Dbg( p_demux, "| | | + Track Audio" );
2213 for( j = 0; j < tka->ListSize(); j++ )
2215 EbmlElement *l = (*tka)[j];
2217 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
2219 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
2221 tk->fmt.audio.i_rate = (int)float( afreq );
2222 msg_Dbg( p_demux, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
2224 else if( MKV_IS_ID( l, KaxAudioChannels ) )
2226 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
2228 tk->fmt.audio.i_channels = uint8( achan );
2229 msg_Dbg( p_demux, "| | | | + achan=%u", uint8( achan ) );
2231 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
2233 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
2235 tk->fmt.audio.i_bitspersample = uint8( abits );
2236 msg_Dbg( p_demux, "| | | | + abits=%u", uint8( abits ) );
2240 msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*l).name() );
2246 msg_Dbg( p_demux, "| | | + Unknown (%s)",
2247 typeid(*l).name() );
2252 static void ParseTracks( demux_t *p_demux, EbmlElement *tracks )
2254 demux_sys_t *p_sys = p_demux->p_sys;
2258 int i_upper_level = 0;
2260 msg_Dbg( p_demux, "| + Tracks" );
2262 /* Master elements */
2263 m = static_cast<EbmlMaster *>(tracks);
2264 m->Read( *p_sys->es, tracks->Generic().Context, i_upper_level, el, true );
2266 for( i = 0; i < m->ListSize(); i++ )
2268 EbmlElement *l = (*m)[i];
2270 if( MKV_IS_ID( l, KaxTrackEntry ) )
2272 ParseTrackEntry( p_demux, static_cast<EbmlMaster *>(l) );
2276 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2281 /*****************************************************************************
2283 *****************************************************************************/
2284 static void ParseInfo( demux_t *p_demux, EbmlElement *info )
2286 demux_sys_t *p_sys = p_demux->p_sys;
2290 int i_upper_level = 0;
2292 msg_Dbg( p_demux, "| + Information" );
2294 /* Master elements */
2295 m = static_cast<EbmlMaster *>(info);
2296 m->Read( *p_sys->es, info->Generic().Context, i_upper_level, el, true );
2298 for( i = 0; i < m->ListSize(); i++ )
2300 EbmlElement *l = (*m)[i];
2302 if( MKV_IS_ID( l, KaxSegmentUID ) )
2304 KaxSegmentUID &uid = *(KaxSegmentUID*)l;
2306 msg_Dbg( p_demux, "| | + UID=%d", uint32(uid) );
2308 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
2310 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
2312 p_sys->i_timescale = uint64(tcs);
2314 msg_Dbg( p_demux, "| | + TimecodeScale="I64Fd,
2315 p_sys->i_timescale );
2317 else if( MKV_IS_ID( l, KaxDuration ) )
2319 KaxDuration &dur = *(KaxDuration*)l;
2321 p_sys->f_duration = float(dur);
2323 msg_Dbg( p_demux, "| | + Duration=%f",
2324 p_sys->f_duration );
2326 else if( MKV_IS_ID( l, KaxMuxingApp ) )
2328 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
2330 p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
2332 msg_Dbg( p_demux, "| | + Muxing Application=%s",
2333 p_sys->psz_muxing_application );
2335 else if( MKV_IS_ID( l, KaxWritingApp ) )
2337 KaxWritingApp &wapp = *(KaxWritingApp*)l;
2339 p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
2341 msg_Dbg( p_demux, "| | + Writing Application=%s",
2342 p_sys->psz_writing_application );
2344 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
2346 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
2348 p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
2350 msg_Dbg( p_demux, "| | + Segment Filename=%s",
2351 p_sys->psz_segment_filename );
2353 else if( MKV_IS_ID( l, KaxTitle ) )
2355 KaxTitle &title = *(KaxTitle*)l;
2357 p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
2359 msg_Dbg( p_demux, "| | + Title=%s", p_sys->psz_title );
2361 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
2362 else if( MKV_IS_ID( l, KaxDateUTC ) )
2364 KaxDateUTC &date = *(KaxDateUTC*)l;
2369 i_date = date.GetEpochDate();
2370 memset( buffer, 0, 256 );
2371 if( gmtime_r( &i_date, &tmres ) &&
2372 asctime_r( &tmres, buffer ) )
2374 buffer[strlen( buffer)-1]= '\0';
2375 p_sys->psz_date_utc = strdup( buffer );
2376 msg_Dbg( p_demux, "| | + Date=%s", p_sys->psz_date_utc );
2382 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2386 p_sys->f_duration = p_sys->f_duration * p_sys->i_timescale / 1000000.0;
2390 /*****************************************************************************
2392 *****************************************************************************/
2393 static void ParseChapterAtom( demux_t *p_demux, int i_level, EbmlMaster *ca )
2395 demux_sys_t *p_sys = p_demux->p_sys;
2399 if( p_sys->title == NULL )
2401 p_sys->title = vlc_input_title_New();
2403 sk = vlc_seekpoint_New();
2405 msg_Dbg( p_demux, "| | | + ChapterAtom (level=%d)", i_level );
2406 for( i = 0; i < ca->ListSize(); i++ )
2408 EbmlElement *l = (*ca)[i];
2410 if( MKV_IS_ID( l, KaxChapterUID ) )
2412 KaxChapterUID &uid = *(KaxChapterUID*)l;
2413 uint32_t i_uid = uint32( uid );
2414 msg_Dbg( p_demux, "| | | | + ChapterUID: 0x%x", i_uid );
2416 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
2418 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
2419 sk->i_time_offset = uint64( start ) / I64C(1000);
2421 msg_Dbg( p_demux, "| | | | + ChapterTimeStart: %lld", sk->i_time_offset );
2423 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
2425 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
2426 int64_t i_end = uint64( end );
2428 msg_Dbg( p_demux, "| | | | + ChapterTimeEnd: %lld", i_end );
2430 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
2432 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
2435 msg_Dbg( p_demux, "| | | | + ChapterDisplay" );
2436 for( j = 0; j < cd->ListSize(); j++ )
2438 EbmlElement *l= (*cd)[j];
2440 if( MKV_IS_ID( l, KaxChapterString ) )
2442 KaxChapterString &name =*(KaxChapterString*)l;
2443 char *psz = UTF8ToStr( UTFstring( name ) );
2444 sk->psz_name = strdup( psz );
2445 msg_Dbg( p_demux, "| | | | | + ChapterString '%s'", psz );
2447 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
2449 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
2450 const char *psz = string( lang ).c_str();
2452 msg_Dbg( p_demux, "| | | | | + ChapterLanguage '%s'", psz );
2454 else if( MKV_IS_ID( l, KaxChapterCountry ) )
2456 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
2457 const char *psz = string( ct ).c_str();
2459 msg_Dbg( p_demux, "| | | | | + ChapterCountry '%s'", psz );
2463 else if( MKV_IS_ID( l, KaxChapterAtom ) )
2465 ParseChapterAtom( p_demux, i_level+1, static_cast<EbmlMaster *>(l) );
2468 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
2469 p_sys->title->i_seekpoint++;
2470 p_sys->title->seekpoint = (seekpoint_t**)realloc( p_sys->title->seekpoint, p_sys->title->i_seekpoint * sizeof( seekpoint_t* ) );
2471 p_sys->title->seekpoint[p_sys->title->i_seekpoint-1] = sk;
2474 /*****************************************************************************
2476 *****************************************************************************/
2477 static void ParseChapters( demux_t *p_demux, EbmlElement *chapters )
2479 demux_sys_t *p_sys = p_demux->p_sys;
2483 int i_upper_level = 0;
2486 /* Master elements */
2487 m = static_cast<EbmlMaster *>(chapters);
2488 m->Read( *p_sys->es, chapters->Generic().Context, i_upper_level, el, true );
2490 for( i = 0; i < m->ListSize(); i++ )
2492 EbmlElement *l = (*m)[i];
2494 if( MKV_IS_ID( l, KaxEditionEntry ) )
2496 EbmlMaster *E = static_cast<EbmlMaster *>(l );
2498 msg_Dbg( p_demux, "| | + EditionEntry" );
2499 for( j = 0; j < E->ListSize(); j++ )
2501 EbmlElement *l = (*E)[j];
2503 if( MKV_IS_ID( l, KaxChapterAtom ) )
2505 ParseChapterAtom( p_demux, 0, static_cast<EbmlMaster *>(l) );
2509 msg_Dbg( p_demux, "| | | + Unknown (%s)", typeid(*l).name() );
2515 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2520 /*****************************************************************************
2521 * InformationCreate:
2522 *****************************************************************************/
2523 static void InformationCreate( demux_t *p_demux )
2525 demux_sys_t *p_sys = p_demux->p_sys;
2528 p_sys->meta = vlc_meta_New();
2530 if( p_sys->psz_title )
2532 vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_sys->psz_title );
2534 if( p_sys->psz_date_utc )
2536 vlc_meta_Add( p_sys->meta, VLC_META_DATE, p_sys->psz_date_utc );
2538 if( p_sys->psz_segment_filename )
2540 vlc_meta_Add( p_sys->meta, _("Segment filename"), p_sys->psz_segment_filename );
2542 if( p_sys->psz_muxing_application )
2544 vlc_meta_Add( p_sys->meta, _("Muxing application"), p_sys->psz_muxing_application );
2546 if( p_sys->psz_writing_application )
2548 vlc_meta_Add( p_sys->meta, _("Writing application"), p_sys->psz_writing_application );
2551 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
2553 mkv_track_t *tk = &p_sys->track[i_track];
2554 vlc_meta_t *mtk = vlc_meta_New();
2556 p_sys->meta->track = (vlc_meta_t**)realloc( p_sys->meta->track,
2557 sizeof( vlc_meta_t * ) * ( p_sys->meta->i_track + 1 ) );
2558 p_sys->meta->track[p_sys->meta->i_track++] = mtk;
2560 if( tk->fmt.psz_description )
2562 vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
2564 if( tk->psz_codec_name )
2566 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
2568 if( tk->psz_codec_settings )
2570 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, tk->psz_codec_settings );
2572 if( tk->psz_codec_info_url )
2574 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
2576 if( tk->psz_codec_download_url )
2578 vlc_meta_Add( p_sys->meta, VLC_META_URL, tk->psz_codec_download_url );
2582 if( p_sys->i_tags_position >= 0 )
2584 vlc_bool_t b_seekable;
2586 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
2589 LoadTags( p_demux );
2595 /*****************************************************************************
2597 *****************************************************************************/
2599 static void IndexAppendCluster( demux_t *p_demux, KaxCluster *cluster )
2601 demux_sys_t *p_sys = p_demux->p_sys;
2603 #define idx p_sys->index[p_sys->i_index]
2605 idx.i_block_number= -1;
2606 idx.i_position = cluster->GetElementPosition();
2608 idx.b_key = VLC_TRUE;
2611 if( p_sys->i_index >= p_sys->i_index_max )
2613 p_sys->i_index_max += 1024;
2614 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2619 static char * UTF8ToStr( const UTFstring &u )
2628 p = dst = (char*)malloc( i_src + 1);