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"
75 #include "ebml/StdIOCallback.h"
77 using namespace LIBMATROSKA_NAMESPACE;
80 /*****************************************************************************
82 *****************************************************************************/
83 static int Open ( vlc_object_t * );
84 static void Close( vlc_object_t * );
87 set_description( _("Matroska stream demuxer" ) );
88 set_capability( "demux2", 50 );
89 set_callbacks( Open, Close );
91 add_bool( "mkv-seek-percent", 1, NULL,
92 N_("Seek based on percent not time"),
93 N_("Seek based on percent not time"), VLC_TRUE );
95 add_shortcut( "mka" );
96 add_shortcut( "mkv" );
99 /*****************************************************************************
101 *****************************************************************************/
102 static int Demux ( demux_t * );
103 static int Control( demux_t *, int, va_list );
104 static void Seek ( demux_t *, mtime_t i_date, int i_percent );
107 /*****************************************************************************
109 *****************************************************************************/
110 class vlc_stream_io_callback: public IOCallback
117 vlc_stream_io_callback( stream_t * );
119 virtual uint32 read ( void *p_buffer, size_t i_size);
120 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
121 virtual size_t write ( const void *p_buffer, size_t i_size);
122 virtual uint64 getFilePointer ( void );
123 virtual void close ( void );
126 /*****************************************************************************
128 *****************************************************************************/
132 EbmlParser( EbmlStream *es, EbmlElement *el_start );
137 EbmlElement *Get( void );
140 int GetLevel( void );
145 EbmlElement *m_el[10];
154 /*****************************************************************************
155 * Some functions to manipulate memory
156 *****************************************************************************/
157 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
158 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
160 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
163 /*****************************************************************************
164 * definitions of structures and functions used by this plugins
165 *****************************************************************************/
168 vlc_bool_t b_default;
169 vlc_bool_t b_enabled;
173 uint8_t *p_extra_data;
177 uint64_t i_default_duration;
178 float f_timecodescale;
186 /* data to be send first */
188 uint8_t *p_data_init;
190 /* hack : it's for seek */
191 vlc_bool_t b_search_keyframe;
194 char *psz_codec_name;
195 char *psz_codec_settings;
196 char *psz_codec_info_url;
197 char *psz_codec_download_url;
214 vlc_stream_io_callback *in;
219 uint64_t i_timescale;
221 /* duration of the segment */
229 int64_t i_cues_position;
230 int64_t i_chapters_position;
231 int64_t i_tags_position;
245 char *psz_muxing_application;
246 char *psz_writing_application;
247 char *psz_segment_filename;
253 input_title_t *title;
256 #define MKVD_TIMECODESCALE 1000000
258 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
260 static void IndexAppendCluster ( demux_t *p_demux, KaxCluster *cluster );
261 static char *UTF8ToStr ( const UTFstring &u );
262 static void LoadCues ( demux_t * );
263 static void InformationCreate ( demux_t * );
265 static void ParseInfo( demux_t *, EbmlElement *info );
266 static void ParseTracks( demux_t *, EbmlElement *tracks );
267 static void ParseSeekHead( demux_t *, EbmlElement *seekhead );
268 static void ParseChapters( demux_t *, EbmlElement *chapters );
270 /*****************************************************************************
271 * Open: initializes matroska demux structures
272 *****************************************************************************/
273 static int Open( vlc_object_t * p_this )
275 demux_t *p_demux = (demux_t*)p_this;
281 EbmlElement *el = NULL, *el1 = NULL;
283 /* peek the begining */
284 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
286 msg_Warn( p_demux, "cannot peek" );
290 /* is a valid file */
291 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
292 p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
294 msg_Warn( p_demux, "matroska module discarded "
295 "(invalid header 0x%.2x%.2x%.2x%.2x)",
296 p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
300 /* Set the demux function */
301 p_demux->pf_demux = Demux;
302 p_demux->pf_control = Control;
303 p_demux->p_sys = p_sys = (demux_sys_t*)malloc(sizeof( demux_sys_t ));
305 memset( p_sys, 0, sizeof( demux_sys_t ) );
306 p_sys->in = new vlc_stream_io_callback( p_demux->s );
307 p_sys->es = new EbmlStream( *p_sys->in );
308 p_sys->f_duration = -1;
309 p_sys->i_timescale = MKVD_TIMECODESCALE;
311 p_sys->track = (mkv_track_t*)malloc( sizeof( mkv_track_t ) );
313 p_sys->i_cues_position = -1;
314 p_sys->i_chapters_position = -1;
315 p_sys->i_tags_position = -1;
317 p_sys->b_cues = VLC_FALSE;
319 p_sys->i_index_max = 1024;
320 p_sys->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
321 p_sys->i_index_max );
323 p_sys->psz_muxing_application = NULL;
324 p_sys->psz_writing_application = NULL;
325 p_sys->psz_segment_filename = NULL;
326 p_sys->psz_title = NULL;
327 p_sys->psz_date_utc = NULL;;
331 if( p_sys->es == NULL )
333 msg_Err( p_demux, "failed to create EbmlStream" );
338 /* Find the EbmlHead element */
339 el = p_sys->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
342 msg_Err( p_demux, "cannot find EbmlHead" );
345 msg_Dbg( p_demux, "EbmlHead" );
347 el->SkipData( *p_sys->es, el->Generic().Context );
351 el = p_sys->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
354 msg_Err( p_demux, "cannot find KaxSegment" );
357 msg_Dbg( p_demux, "+ Segment" );
358 p_sys->segment = (KaxSegment*)el;
359 p_sys->cluster = NULL;
361 p_sys->ep = new EbmlParser( p_sys->es, el );
363 while( ( el1 = p_sys->ep->Get() ) != NULL )
365 if( MKV_IS_ID( el1, KaxInfo ) )
367 ParseInfo( p_demux, el1 );
369 else if( MKV_IS_ID( el1, KaxTracks ) )
371 ParseTracks( p_demux, el1 );
373 else if( MKV_IS_ID( el1, KaxSeekHead ) )
375 ParseSeekHead( p_demux, el1 );
377 else if( MKV_IS_ID( el1, KaxCues ) )
379 msg_Dbg( p_demux, "| + Cues" );
381 else if( MKV_IS_ID( el1, KaxCluster ) )
383 msg_Dbg( p_demux, "| + Cluster" );
385 p_sys->cluster = (KaxCluster*)el1;
388 /* stop parsing the stream */
391 else if( MKV_IS_ID( el1, KaxAttachments ) )
393 msg_Dbg( p_demux, "| + Attachments FIXME TODO (but probably never supported)" );
395 else if( MKV_IS_ID( el1, KaxChapters ) )
397 msg_Dbg( p_demux, "| + Chapters" );
398 ParseChapters( p_demux, el1 );
400 else if( MKV_IS_ID( el1, KaxTag ) )
402 msg_Dbg( p_demux, "| + Tags FIXME TODO" );
406 msg_Dbg( p_demux, "| + Unknown (%s)", typeid(*el1).name() );
410 if( p_sys->cluster == NULL )
412 msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
416 /* *** Load the cue if found *** */
417 if( p_sys->i_cues_position >= 0 )
419 vlc_bool_t b_seekable;
421 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
428 if( !p_sys->b_cues || p_sys->i_index <= 0 )
430 msg_Warn( p_demux, "no cues/empty cues found->seek won't be precise" );
432 IndexAppendCluster( p_demux, p_sys->cluster );
434 p_sys->b_cues = VLC_FALSE;
438 msg_Dbg( p_demux, "found %d es", p_sys->i_track );
439 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
441 #define tk p_sys->track[i_track]
442 if( tk.fmt.i_cat == UNKNOWN_ES )
444 msg_Warn( p_demux, "invalid track[%d, n=%d]", i_track, tk.i_number );
449 if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) )
451 if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
453 msg_Err( p_demux, "missing/invalid BITMAPINFOHEADER" );
454 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
458 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
460 tk.fmt.video.i_width = GetDWLE( &p_bih->biWidth );
461 tk.fmt.video.i_height= GetDWLE( &p_bih->biHeight );
462 tk.fmt.i_codec = GetFOURCC( &p_bih->biCompression );
464 tk.fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
465 if( tk.fmt.i_extra > 0 )
467 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
468 memcpy( tk.fmt.p_extra, &p_bih[1], tk.fmt.i_extra );
472 else if( !strcmp( tk.psz_codec, "V_MPEG1" ) ||
473 !strcmp( tk.psz_codec, "V_MPEG2" ) )
475 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
477 else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) )
479 if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) )
481 tk.fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
485 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
488 else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
490 if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
492 msg_Err( p_demux, "missing/invalid WAVEFORMATEX" );
493 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
497 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;
499 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk.fmt.i_codec, NULL );
501 tk.fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
502 tk.fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
503 tk.fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
504 tk.fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
505 tk.fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
507 tk.fmt.i_extra = GetWLE( &p_wf->cbSize );
508 if( tk.fmt.i_extra > 0 )
510 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
511 memcpy( tk.fmt.p_extra, &p_wf[1], tk.fmt.i_extra );
515 else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
516 !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
517 !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
519 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
521 else if( !strcmp( tk.psz_codec, "A_AC3" ) )
523 tk.fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
525 else if( !strcmp( tk.psz_codec, "A_DTS" ) )
527 tk.fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
529 else if( !strcmp( tk.psz_codec, "A_FLAC" ) )
531 tk.fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
532 tk.fmt.i_extra = tk.i_extra_data;
533 tk.fmt.p_extra = malloc( tk.i_extra_data );
534 memcpy( tk.fmt.p_extra,tk.p_extra_data, tk.i_extra_data );
536 else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
538 int i, i_offset = 1, i_size[3], i_extra;
541 tk.fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
543 /* Split the 3 headers */
544 if( tk.p_extra_data[0] != 0x02 )
545 msg_Err( p_demux, "invalid vorbis header" );
547 for( i = 0; i < 2; i++ )
550 while( i_offset < tk.i_extra_data )
552 i_size[i] += tk.p_extra_data[i_offset];
553 if( tk.p_extra_data[i_offset++] != 0xff ) break;
557 i_size[0] = __MIN(i_size[0], tk.i_extra_data - i_offset);
558 i_size[1] = __MIN(i_size[1], tk.i_extra_data -i_offset -i_size[0]);
559 i_size[2] = tk.i_extra_data - i_offset - i_size[0] - i_size[1];
561 tk.fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
562 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
563 p_extra = (uint8_t *)tk.fmt.p_extra; i_extra = 0;
564 for( i = 0; i < 3; i++ )
566 *(p_extra++) = i_size[i] >> 8;
567 *(p_extra++) = i_size[i] & 0xFF;
568 memcpy( p_extra, tk.p_extra_data + i_offset + i_extra,
570 p_extra += i_size[i];
571 i_extra += i_size[i];
574 else if( !strncmp( tk.psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
575 !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
577 int i_profile, i_srate;
578 static unsigned int i_sample_rates[] =
580 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
581 16000, 12000, 11025, 8000, 7350, 0, 0, 0
584 tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
585 /* create data for faad (MP4DecSpecificDescrTag)*/
587 if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
591 else if( !strcmp( &tk.psz_codec[12], "LC" ) )
595 else if( !strcmp( &tk.psz_codec[12], "SSR" ) )
604 for( i_srate = 0; i_srate < 13; i_srate++ )
606 if( i_sample_rates[i_srate] == tk.fmt.audio.i_rate )
611 msg_Dbg( p_demux, "profile=%d srate=%d", i_profile, i_srate );
614 tk.fmt.p_extra = malloc( tk.fmt.i_extra );
615 ((uint8_t*)tk.fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
616 ((uint8_t*)tk.fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk.fmt.audio.i_channels << 3);
618 else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
619 !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
620 !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
622 if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
624 tk.fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
628 tk.fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
630 tk.fmt.audio.i_blockalign = ( tk.fmt.audio.i_bitspersample + 7 ) / 8 * tk.fmt.audio.i_channels;
632 else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
634 tk.fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
635 tk.fmt.subs.psz_encoding = strdup( "UTF-8" );
637 else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) ||
638 !strcmp( tk.psz_codec, "S_TEXT/ASS" ) ||
639 !strcmp( tk.psz_codec, "S_SSA" ) ||
640 !strcmp( tk.psz_codec, "S_ASS" ))
642 tk.fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
643 tk.fmt.subs.psz_encoding = strdup( "UTF-8" );
645 else if( !strcmp( tk.psz_codec, "S_VOBSUB" ) )
647 tk.fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
651 msg_Err( p_demux, "unknow codec id=`%s'", tk.psz_codec );
652 tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
655 tk.p_es = es_out_Add( p_demux->out, &tk.fmt );
659 /* add information */
660 InformationCreate( p_demux );
671 /*****************************************************************************
672 * Close: frees unused data
673 *****************************************************************************/
674 static void Close( vlc_object_t *p_this )
676 demux_t *p_demux = (demux_t*)p_this;
677 demux_sys_t *p_sys = p_demux->p_sys;
680 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
682 #define tk p_sys->track[i_track]
683 if( tk.fmt.psz_description )
685 free( tk.fmt.psz_description );
689 free( tk.psz_codec );
691 if( tk.fmt.psz_language )
693 free( tk.fmt.psz_language );
697 free( p_sys->track );
699 if( p_sys->psz_writing_application )
701 free( p_sys->psz_writing_application );
703 if( p_sys->psz_muxing_application )
705 free( p_sys->psz_muxing_application );
708 delete p_sys->segment;
717 /*****************************************************************************
719 *****************************************************************************/
720 static int Control( demux_t *p_demux, int i_query, va_list args )
722 demux_sys_t *p_sys = p_demux->p_sys;
726 vlc_meta_t **pp_meta;
731 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
732 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
735 case DEMUX_GET_LENGTH:
736 pi64 = (int64_t*)va_arg( args, int64_t * );
737 if( p_sys->f_duration > 0.0 )
739 *pi64 = (int64_t)(p_sys->f_duration * 1000);
744 case DEMUX_GET_POSITION:
745 pf = (double*)va_arg( args, double * );
746 *pf = (double)p_sys->in->getFilePointer() / (double)stream_Size( p_demux->s );
749 case DEMUX_SET_POSITION:
750 f = (double)va_arg( args, double );
751 Seek( p_demux, -1, (int)(100.0 * f) );
755 pi64 = (int64_t*)va_arg( args, int64_t * );
756 if( p_sys->f_duration > 0.0 )
758 mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
761 *pi64 = (mtime_t)1000000 *
763 (mtime_t)p_sys->in->getFilePointer() /
764 (mtime_t)stream_Size( p_demux->s );
769 case DEMUX_GET_TITLE_INFO:
770 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
772 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
773 int *pi_int = (int*)va_arg( args, int* );
776 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) );
778 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->title );
784 case DEMUX_SET_TITLE:
785 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
791 case DEMUX_SET_SEEKPOINT:
792 /* FIXME do a better implementation */
793 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
795 int i_skp = (int)va_arg( args, int );
796 int64_t i_start = (int64_t)p_sys->title->seekpoint[i_skp]->i_time_offset;
798 if( p_sys->f_duration > 0.0 )
800 double f_pos = (double)i_start / (double)(1000.0 * p_sys->f_duration );
801 Seek( p_demux, -1, (int)(100 * f_pos+0.5));
815 static int BlockGet( demux_t *p_demux, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
817 demux_sys_t *p_sys = p_demux->p_sys;
833 el = p_sys->ep->Get();
834 i_level = p_sys->ep->GetLevel();
836 if( el == NULL && *pp_block != NULL )
838 /* update the index */
839 #define idx p_sys->index[p_sys->i_index - 1]
840 if( p_sys->i_index > 0 && idx.i_time == -1 )
842 idx.i_time = (*pp_block)->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
843 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
851 if( p_sys->ep->GetLevel() > 1 )
856 msg_Warn( p_demux, "EOF" );
863 if( MKV_IS_ID( el, KaxCluster ) )
865 p_sys->cluster = (KaxCluster*)el;
867 /* add it to the index */
868 if( p_sys->i_index == 0 ||
869 ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
871 IndexAppendCluster( p_demux, p_sys->cluster );
876 else if( MKV_IS_ID( el, KaxCues ) )
878 msg_Warn( p_demux, "find KaxCues FIXME" );
883 msg_Dbg( p_demux, "unknown (%s)", typeid( el ).name() );
886 else if( i_level == 2 )
888 if( MKV_IS_ID( el, KaxClusterTimecode ) )
890 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
892 ctc.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
893 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
895 else if( MKV_IS_ID( el, KaxBlockGroup ) )
900 else if( i_level == 3 )
902 if( MKV_IS_ID( el, KaxBlock ) )
904 *pp_block = (KaxBlock*)el;
906 (*pp_block)->ReadData( p_sys->es->I_O() );
907 (*pp_block)->SetParent( *p_sys->cluster );
911 else if( MKV_IS_ID( el, KaxBlockDuration ) )
913 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
915 dur.ReadData( p_sys->es->I_O() );
916 *pi_duration = uint64( dur );
918 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
920 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
922 ref.ReadData( p_sys->es->I_O() );
925 *pi_ref1 = int64( ref );
929 *pi_ref2 = int64( ref );
935 msg_Err( p_demux, "invalid level = %d", i_level );
941 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
944 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
945 memcpy( p_block->p_buffer, p_mem, i_mem );
946 //p_block->i_rate = p_input->stream.control.i_rate;
950 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
953 demux_sys_t *p_sys = p_demux->p_sys;
959 #define tk p_sys->track[i_track]
960 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
962 if( tk.i_number == block->TrackNum() )
968 if( i_track >= p_sys->i_track )
970 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
974 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk.p_es, &b );
977 tk.b_inited = VLC_FALSE;
981 /* First send init data */
982 if( !tk.b_inited && tk.i_data_init > 0 )
986 msg_Dbg( p_demux, "sending header (%d bytes)", tk.i_data_init );
987 p_init = MemToBlock( p_demux, tk.p_data_init, tk.i_data_init );
988 if( p_init ) es_out_Send( p_demux->out, tk.p_es, p_init );
990 tk.b_inited = VLC_TRUE;
993 for( i = 0; i < block->NumberFrames(); i++ )
996 DataBuffer &data = block->GetBuffer(i);
998 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
999 if( p_block == NULL )
1004 if( tk.fmt.i_cat != VIDEO_ES )
1005 p_block->i_dts = p_block->i_pts = i_pts;
1008 p_block->i_dts = i_pts;
1012 if( tk.fmt.i_cat == SPU_ES && strcmp( tk.psz_codec, "S_VOBSUB" ) )
1014 p_block->i_length = i_duration * 1000;
1016 es_out_Send( p_demux->out, tk.p_es, p_block );
1018 /* use time stamp only for first block */
1025 static void Seek( demux_t *p_demux, mtime_t i_date, int i_percent)
1027 demux_sys_t *p_sys = p_demux->p_sys;
1030 int64_t i_block_duration;
1031 int64_t i_block_ref1;
1032 int64_t i_block_ref2;
1035 int i_track_skipping;
1038 msg_Dbg( p_demux, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
1039 if( i_date < 0 && i_percent < 0 )
1043 if( i_percent > 100 ) i_percent = 100;
1046 p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
1047 p_sys->cluster = NULL;
1049 /* seek without index or without date */
1050 if( config_GetInt( p_demux, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
1052 int64_t i_pos = i_percent * stream_Size( p_demux->s ) / 100;
1054 msg_Dbg( p_demux, "inacurate way of seeking" );
1055 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1057 if( p_sys->index[i_index].i_position >= i_pos)
1062 if( i_index == p_sys->i_index )
1067 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1070 if( p_sys->index[i_index].i_position < i_pos )
1074 msg_Warn( p_demux, "searching for cluster, could take some time" );
1076 /* search a cluster */
1077 while( ( el = p_sys->ep->Get() ) != NULL )
1079 if( MKV_IS_ID( el, KaxCluster ) )
1081 KaxCluster *cluster = (KaxCluster*)el;
1083 /* add it to the index */
1084 IndexAppendCluster( p_demux, cluster );
1086 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1088 p_sys->cluster = cluster;
1098 for( i_index = 0; i_index < p_sys->i_index; i_index++ )
1100 if( p_sys->index[i_index].i_time >= i_date )
1111 msg_Dbg( p_demux, "seek got "I64Fd" (%d%%)",
1112 p_sys->index[i_index].i_time,
1113 (int)( 100 * p_sys->index[i_index].i_position /
1114 stream_Size( p_demux->s ) ) );
1116 p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
1120 /* now parse until key frame */
1121 #define tk p_sys->track[i_track]
1122 i_track_skipping = 0;
1123 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1125 if( tk.fmt.i_cat == VIDEO_ES )
1127 tk.b_search_keyframe = VLC_TRUE;
1132 while( i_track_skipping > 0 )
1134 if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1136 msg_Warn( p_demux, "cannot get block EOF?" );
1141 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale + 1;
1143 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1145 if( tk.i_number == block->TrackNum() )
1151 if( i_track < p_sys->i_track )
1153 if( tk.fmt.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
1155 tk.b_search_keyframe = VLC_FALSE;
1158 if( tk.fmt.i_cat == VIDEO_ES && !tk.b_search_keyframe )
1160 BlockDecode( p_demux, block, 0, 0 );
1169 /*****************************************************************************
1170 * Demux: reads and demuxes data packets
1171 *****************************************************************************
1172 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1173 *****************************************************************************/
1174 static int Demux( demux_t *p_demux)
1176 demux_sys_t *p_sys = p_demux->p_sys;
1177 mtime_t i_start_pts;
1178 int i_block_count = 0;
1181 int64_t i_block_duration;
1182 int64_t i_block_ref1;
1183 int64_t i_block_ref2;
1189 if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1191 msg_Warn( p_demux, "cannot get block EOF?" );
1196 p_sys->i_pts = block->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale + 1;
1198 if( p_sys->i_pts > 0 )
1200 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
1203 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
1208 if( i_start_pts == -1 )
1210 i_start_pts = p_sys->i_pts;
1212 else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
1221 /*****************************************************************************
1223 *****************************************************************************/
1224 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1230 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1232 if( i_size <= 0 || mb_eof )
1237 return stream_Read( s, p_buffer, i_size );
1239 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1245 case seek_beginning:
1249 i_pos = stream_Size( s ) - i_offset;
1252 i_pos= stream_Tell( s ) + i_offset;
1256 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1263 if( stream_Seek( s, i_pos ) )
1269 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1273 uint64 vlc_stream_io_callback::getFilePointer( void )
1275 return stream_Tell( s );
1277 void vlc_stream_io_callback::close( void )
1283 /*****************************************************************************
1284 * Ebml Stream parser
1285 *****************************************************************************/
1286 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1294 for( i = 1; i < 6; i++ )
1300 mb_keep = VLC_FALSE;
1303 EbmlParser::~EbmlParser( void )
1307 for( i = 1; i < mi_level; i++ )
1313 mb_keep = VLC_FALSE;
1317 void EbmlParser::Up( void )
1319 if( mi_user_level == mi_level )
1321 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1327 void EbmlParser::Down( void )
1333 void EbmlParser::Keep( void )
1338 int EbmlParser::GetLevel( void )
1340 return mi_user_level;
1343 EbmlElement *EbmlParser::Get( void )
1347 if( mi_user_level != mi_level )
1353 EbmlElement *ret = m_got;
1359 if( m_el[mi_level] )
1361 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
1364 delete m_el[mi_level];
1366 mb_keep = VLC_FALSE;
1369 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
1380 delete m_el[mi_level - 1];
1381 m_got = m_el[mi_level -1] = m_el[mi_level];
1382 m_el[mi_level] = NULL;
1389 else if( m_el[mi_level] == NULL )
1391 fprintf( stderr," m_el[mi_level] == NULL\n" );
1394 return m_el[mi_level];
1398 /*****************************************************************************
1400 * * LoadCues : load the cues element and update index
1402 * * LoadTags : load ... the tags element
1404 * * InformationCreate : create all information, load tags if present
1406 *****************************************************************************/
1407 static void LoadCues( demux_t *p_demux )
1409 demux_sys_t *p_sys = p_demux->p_sys;
1410 int64_t i_sav_position = p_sys->in->getFilePointer();
1412 EbmlElement *el, *cues;
1414 msg_Dbg( p_demux, "loading cues" );
1415 p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
1416 cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
1420 msg_Err( p_demux, "cannot load cues (broken seekhead or file)" );
1421 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1425 ep = new EbmlParser( p_sys->es, cues );
1426 while( ( el = ep->Get() ) != NULL )
1428 if( MKV_IS_ID( el, KaxCuePoint ) )
1430 #define idx p_sys->index[p_sys->i_index]
1433 idx.i_block_number= -1;
1434 idx.i_position = -1;
1436 idx.b_key = VLC_TRUE;
1439 while( ( el = ep->Get() ) != NULL )
1441 if( MKV_IS_ID( el, KaxCueTime ) )
1443 KaxCueTime &ctime = *(KaxCueTime*)el;
1445 ctime.ReadData( p_sys->es->I_O() );
1447 idx.i_time = uint64( ctime ) * (mtime_t)1000000000 / p_sys->i_timescale;
1449 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
1452 while( ( el = ep->Get() ) != NULL )
1454 if( MKV_IS_ID( el, KaxCueTrack ) )
1456 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
1458 ctrack.ReadData( p_sys->es->I_O() );
1459 idx.i_track = uint16( ctrack );
1461 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
1463 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
1465 ccpos.ReadData( p_sys->es->I_O() );
1466 idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
1468 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
1470 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
1472 cbnum.ReadData( p_sys->es->I_O() );
1473 idx.i_block_number = uint32( cbnum );
1477 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1484 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1490 msg_Dbg( p_demux, " * added time="I64Fd" pos="I64Fd
1491 " track=%d bnum=%d", idx.i_time, idx.i_position,
1492 idx.i_track, idx.i_block_number );
1496 if( p_sys->i_index >= p_sys->i_index_max )
1498 p_sys->i_index_max += 1024;
1499 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
1505 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
1511 p_sys->b_cues = VLC_TRUE;
1513 msg_Dbg( p_demux, "loading cues done." );
1514 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1517 static void LoadTags( demux_t *p_demux )
1519 demux_sys_t *p_sys = p_demux->p_sys;
1520 int64_t i_sav_position = p_sys->in->getFilePointer();
1522 EbmlElement *el, *tags;
1524 msg_Dbg( p_demux, "loading tags" );
1525 p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
1526 tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
1530 msg_Err( p_demux, "cannot load tags (broken seekhead or file)" );
1531 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1535 msg_Dbg( p_demux, "Tags" );
1536 ep = new EbmlParser( p_sys->es, tags );
1537 while( ( el = ep->Get() ) != NULL )
1539 if( MKV_IS_ID( el, KaxTag ) )
1541 msg_Dbg( p_demux, "+ Tag" );
1543 while( ( el = ep->Get() ) != NULL )
1545 if( MKV_IS_ID( el, KaxTagTargets ) )
1547 msg_Dbg( p_demux, "| + Targets" );
1549 while( ( el = ep->Get() ) != NULL )
1551 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1555 else if( MKV_IS_ID( el, KaxTagGeneral ) )
1557 msg_Dbg( p_demux, "| + General" );
1559 while( ( el = ep->Get() ) != NULL )
1561 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1565 else if( MKV_IS_ID( el, KaxTagGenres ) )
1567 msg_Dbg( p_demux, "| + Genres" );
1569 while( ( el = ep->Get() ) != NULL )
1571 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1575 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
1577 msg_Dbg( p_demux, "| + Audio Specific" );
1579 while( ( el = ep->Get() ) != NULL )
1581 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1585 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
1587 msg_Dbg( p_demux, "| + Images Specific" );
1589 while( ( el = ep->Get() ) != NULL )
1591 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
1595 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
1597 msg_Dbg( p_demux, "| + Multi Comment" );
1599 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
1601 msg_Dbg( p_demux, "| + Multi Commercial" );
1603 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
1605 msg_Dbg( p_demux, "| + Multi Date" );
1607 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
1609 msg_Dbg( p_demux, "| + Multi Entity" );
1611 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
1613 msg_Dbg( p_demux, "| + Multi Identifier" );
1615 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
1617 msg_Dbg( p_demux, "| + Multi Legal" );
1619 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
1621 msg_Dbg( p_demux, "| + Multi Title" );
1625 msg_Dbg( p_demux, "| + Unknown (%s)", typeid( *el ).name() );
1632 msg_Dbg( p_demux, "+ Unknown (%s)", typeid( *el ).name() );
1638 msg_Dbg( p_demux, "loading tags done." );
1639 p_sys->in->setFilePointer( i_sav_position, seek_beginning );
1642 /*****************************************************************************
1644 *****************************************************************************/
1645 static void ParseSeekHead( demux_t *p_demux, EbmlElement *seekhead )
1647 demux_sys_t *p_sys = p_demux->p_sys;
1651 int i_upper_level = 0;
1653 msg_Dbg( p_demux, "| + Seek head" );
1655 /* Master elements */
1656 m = static_cast<EbmlMaster *>(seekhead);
1657 m->Read( *p_sys->es, seekhead->Generic().Context, i_upper_level, el, true );
1659 for( i = 0; i < m->ListSize(); i++ )
1661 EbmlElement *l = (*m)[i];
1663 if( MKV_IS_ID( l, KaxSeek ) )
1665 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
1666 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
1671 for( j = 0; j < sk->ListSize(); j++ )
1673 EbmlElement *l = (*sk)[j];
1675 if( MKV_IS_ID( l, KaxSeekID ) )
1677 KaxSeekID &sid = *(KaxSeekID*)l;
1678 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
1680 else if( MKV_IS_ID( l, KaxSeekPosition ) )
1682 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
1683 i_pos = uint64( spos );
1687 msg_Dbg( p_demux, "| | | + Unknown (%s)", typeid(*l).name() );
1693 if( id == KaxCues::ClassInfos.GlobalId )
1695 msg_Dbg( p_demux, "| | | = cues at "I64Fd, i_pos );
1696 p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
1698 else if( id == KaxChapters::ClassInfos.GlobalId )
1700 msg_Dbg( p_demux, "| | | = chapters at "I64Fd, i_pos );
1701 p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
1703 else if( id == KaxTags::ClassInfos.GlobalId )
1705 msg_Dbg( p_demux, "| | | = tags at "I64Fd, i_pos );
1706 p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
1712 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
1717 /*****************************************************************************
1719 *****************************************************************************/
1720 static void ParseTrackEntry( demux_t *p_demux, EbmlMaster *m )
1722 demux_sys_t *p_sys = p_demux->p_sys;
1727 msg_Dbg( p_demux, "| | + Track Entry" );
1730 p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
1732 /* Init the track */
1733 tk = &p_sys->track[p_sys->i_track - 1];
1735 memset( tk, 0, sizeof( mkv_track_t ) );
1737 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
1738 tk->fmt.psz_language = strdup("English");
1739 tk->fmt.psz_description = NULL;
1741 tk->b_default = VLC_TRUE;
1742 tk->b_enabled = VLC_TRUE;
1743 tk->i_number = p_sys->i_track - 1;
1744 tk->i_extra_data = 0;
1745 tk->p_extra_data = NULL;
1746 tk->psz_codec = NULL;
1747 tk->i_default_duration = 0;
1748 tk->f_timecodescale = 1.0;
1750 tk->b_inited = VLC_FALSE;
1751 tk->i_data_init = 0;
1752 tk->p_data_init = NULL;
1754 tk->psz_codec_name = NULL;
1755 tk->psz_codec_settings = NULL;
1756 tk->psz_codec_info_url = NULL;
1757 tk->psz_codec_download_url = NULL;
1759 for( i = 0; i < m->ListSize(); i++ )
1761 EbmlElement *l = (*m)[i];
1763 if( MKV_IS_ID( l, KaxTrackNumber ) )
1765 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
1767 tk->i_number = uint32( tnum );
1768 msg_Dbg( p_demux, "| | | + Track Number=%u", uint32( tnum ) );
1770 else if( MKV_IS_ID( l, KaxTrackUID ) )
1772 KaxTrackUID &tuid = *(KaxTrackUID*)l;
1774 msg_Dbg( p_demux, "| | | + Track UID=%u", uint32( tuid ) );
1776 else if( MKV_IS_ID( l, KaxTrackType ) )
1779 KaxTrackType &ttype = *(KaxTrackType*)l;
1781 switch( uint8(ttype) )
1785 tk->fmt.i_cat = AUDIO_ES;
1789 tk->fmt.i_cat = VIDEO_ES;
1791 case track_subtitle:
1792 psz_type = "subtitle";
1793 tk->fmt.i_cat = SPU_ES;
1796 psz_type = "unknown";
1797 tk->fmt.i_cat = UNKNOWN_ES;
1801 msg_Dbg( p_demux, "| | | + Track Type=%s", psz_type );
1803 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
1805 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
1807 // tk->b_enabled = uint32( fenb );
1808 // msg_Dbg( p_demux, "| | | + Track Enabled=%u",
1809 // uint32( fenb ) );
1811 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
1813 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
1815 tk->b_default = uint32( fdef );
1816 msg_Dbg( p_demux, "| | | + Track Default=%u", uint32( fdef ) );
1818 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
1820 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
1822 msg_Dbg( p_demux, "| | | + Track Lacing=%d", uint32( lac ) );
1824 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
1826 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
1828 msg_Dbg( p_demux, "| | | + Track MinCache=%d", uint32( cmin ) );
1830 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
1832 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
1834 msg_Dbg( p_demux, "| | | + Track MaxCache=%d", uint32( cmax ) );
1836 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
1838 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
1840 tk->i_default_duration = uint64(defd);
1841 msg_Dbg( p_demux, "| | | + Track Default Duration="I64Fd, uint64(defd) );
1843 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
1845 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
1847 tk->f_timecodescale = float( ttcs );
1848 msg_Dbg( p_demux, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
1850 else if( MKV_IS_ID( l, KaxTrackName ) )
1852 KaxTrackName &tname = *(KaxTrackName*)l;
1854 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
1855 msg_Dbg( p_demux, "| | | + Track Name=%s", tk->fmt.psz_description );
1857 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
1859 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
1861 tk->fmt.psz_language = strdup( string( lang ).c_str() );
1863 "| | | + Track Language=`%s'", tk->fmt.psz_language );
1865 else if( MKV_IS_ID( l, KaxCodecID ) )
1867 KaxCodecID &codecid = *(KaxCodecID*)l;
1869 tk->psz_codec = strdup( string( codecid ).c_str() );
1870 msg_Dbg( p_demux, "| | | + Track CodecId=%s", string( codecid ).c_str() );
1872 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
1874 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
1876 tk->i_extra_data = cpriv.GetSize();
1877 if( tk->i_extra_data > 0 )
1879 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
1880 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
1882 msg_Dbg( p_demux, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
1884 else if( MKV_IS_ID( l, KaxCodecName ) )
1886 KaxCodecName &cname = *(KaxCodecName*)l;
1888 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
1889 msg_Dbg( p_demux, "| | | + Track Codec Name=%s", tk->psz_codec_name );
1891 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
1893 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
1895 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
1896 // msg_Dbg( p_demux, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
1898 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
1900 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
1902 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
1903 // msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
1905 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
1907 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
1909 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
1910 // msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
1912 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
1914 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
1916 // msg_Dbg( p_demux, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
1918 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
1920 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
1922 // msg_Dbg( p_demux, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
1924 else if( MKV_IS_ID( l, KaxTrackVideo ) )
1926 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
1929 msg_Dbg( p_demux, "| | | + Track Video" );
1932 for( j = 0; j < tkv->ListSize(); j++ )
1934 EbmlElement *l = (*tkv)[j];
1935 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
1937 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
1939 // msg_Dbg( p_demux, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
1941 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
1943 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
1945 // msg_Dbg( p_demux, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
1948 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
1950 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
1952 tk->fmt.video.i_width = uint16( vwidth );
1953 msg_Dbg( p_demux, "| | | | + width=%d", uint16( vwidth ) );
1955 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
1957 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
1959 tk->fmt.video.i_height = uint16( vheight );
1960 msg_Dbg( p_demux, "| | | | + height=%d", uint16( vheight ) );
1962 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
1964 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
1966 tk->fmt.video.i_visible_width = uint16( vwidth );
1967 msg_Dbg( p_demux, "| | | | + display width=%d", uint16( vwidth ) );
1969 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
1971 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
1973 tk->fmt.video.i_visible_height = uint16( vheight );
1974 msg_Dbg( p_demux, "| | | | + display height=%d", uint16( vheight ) );
1976 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
1978 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
1980 tk->f_fps = float( vfps );
1981 msg_Dbg( p_demux, " | | | + fps=%f", float( vfps ) );
1983 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
1985 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
1987 // msg_Dbg( p_demux, "| | | | + Track Video Display Unit=%s",
1988 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
1990 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
1992 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
1994 // msg_Dbg( p_demux, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
1996 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
1998 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
2000 // msg_Dbg( p_demux, " | | | + fps=%f", float( gamma ) );
2004 msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*l).name() );
2008 else if( MKV_IS_ID( l, KaxTrackAudio ) )
2010 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
2013 msg_Dbg( p_demux, "| | | + Track Audio" );
2015 for( j = 0; j < tka->ListSize(); j++ )
2017 EbmlElement *l = (*tka)[j];
2019 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
2021 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
2023 tk->fmt.audio.i_rate = (int)float( afreq );
2024 msg_Dbg( p_demux, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
2026 else if( MKV_IS_ID( l, KaxAudioChannels ) )
2028 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
2030 tk->fmt.audio.i_channels = uint8( achan );
2031 msg_Dbg( p_demux, "| | | | + achan=%u", uint8( achan ) );
2033 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
2035 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
2037 tk->fmt.audio.i_bitspersample = uint8( abits );
2038 msg_Dbg( p_demux, "| | | | + abits=%u", uint8( abits ) );
2042 msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*l).name() );
2048 msg_Dbg( p_demux, "| | | + Unknown (%s)",
2049 typeid(*l).name() );
2054 static void ParseTracks( demux_t *p_demux, EbmlElement *tracks )
2056 demux_sys_t *p_sys = p_demux->p_sys;
2060 int i_upper_level = 0;
2062 msg_Dbg( p_demux, "| + Tracks" );
2064 /* Master elements */
2065 m = static_cast<EbmlMaster *>(tracks);
2066 m->Read( *p_sys->es, tracks->Generic().Context, i_upper_level, el, true );
2068 for( i = 0; i < m->ListSize(); i++ )
2070 EbmlElement *l = (*m)[i];
2072 if( MKV_IS_ID( l, KaxTrackEntry ) )
2074 ParseTrackEntry( p_demux, static_cast<EbmlMaster *>(l) );
2078 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2083 /*****************************************************************************
2085 *****************************************************************************/
2086 static void ParseInfo( demux_t *p_demux, EbmlElement *info )
2088 demux_sys_t *p_sys = p_demux->p_sys;
2092 int i_upper_level = 0;
2094 msg_Dbg( p_demux, "| + Information" );
2096 /* Master elements */
2097 m = static_cast<EbmlMaster *>(info);
2098 m->Read( *p_sys->es, info->Generic().Context, i_upper_level, el, true );
2100 for( i = 0; i < m->ListSize(); i++ )
2102 EbmlElement *l = (*m)[i];
2104 if( MKV_IS_ID( l, KaxSegmentUID ) )
2106 KaxSegmentUID &uid = *(KaxSegmentUID*)l;
2108 msg_Dbg( p_demux, "| | + UID=%d", uint32(uid) );
2110 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
2112 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
2114 p_sys->i_timescale = uint64(tcs);
2116 msg_Dbg( p_demux, "| | + TimecodeScale="I64Fd,
2117 p_sys->i_timescale );
2119 else if( MKV_IS_ID( l, KaxDuration ) )
2121 KaxDuration &dur = *(KaxDuration*)l;
2123 p_sys->f_duration = float(dur);
2125 msg_Dbg( p_demux, "| | + Duration=%f",
2126 p_sys->f_duration );
2128 else if( MKV_IS_ID( l, KaxMuxingApp ) )
2130 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
2132 p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
2134 msg_Dbg( p_demux, "| | + Muxing Application=%s",
2135 p_sys->psz_muxing_application );
2137 else if( MKV_IS_ID( l, KaxWritingApp ) )
2139 KaxWritingApp &wapp = *(KaxWritingApp*)l;
2141 p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
2143 msg_Dbg( p_demux, "| | + Writing Application=%s",
2144 p_sys->psz_writing_application );
2146 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
2148 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
2150 p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
2152 msg_Dbg( p_demux, "| | + Segment Filename=%s",
2153 p_sys->psz_segment_filename );
2155 else if( MKV_IS_ID( l, KaxTitle ) )
2157 KaxTitle &title = *(KaxTitle*)l;
2159 p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
2161 msg_Dbg( p_demux, "| | + Title=%s", p_sys->psz_title );
2163 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
2164 else if( MKV_IS_ID( l, KaxDateUTC ) )
2166 KaxDateUTC &date = *(KaxDateUTC*)l;
2171 i_date = date.GetEpochDate();
2172 memset( buffer, 0, 256 );
2173 if( gmtime_r( &i_date, &tmres ) &&
2174 asctime_r( &tmres, buffer ) )
2176 buffer[strlen( buffer)-1]= '\0';
2177 p_sys->psz_date_utc = strdup( buffer );
2178 msg_Dbg( p_demux, "| | + Date=%s", p_sys->psz_date_utc );
2184 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2190 /*****************************************************************************
2192 *****************************************************************************/
2193 static void ParseChapterAtom( demux_t *p_demux, int i_level, EbmlMaster *ca )
2195 demux_sys_t *p_sys = p_demux->p_sys;
2199 if( p_sys->title == NULL )
2201 p_sys->title = vlc_input_title_New();
2203 sk = vlc_seekpoint_New();
2205 msg_Dbg( p_demux, "| | | + ChapterAtom (level=%d)", i_level );
2206 for( i = 0; i < ca->ListSize(); i++ )
2208 EbmlElement *l = (*ca)[i];
2210 if( MKV_IS_ID( l, KaxChapterUID ) )
2212 KaxChapterUID &uid = *(KaxChapterUID*)l;
2213 uint32_t i_uid = uint32( uid );
2214 msg_Dbg( p_demux, "| | | | + ChapterUID: 0x%x", i_uid );
2216 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
2218 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
2219 sk->i_time_offset = uint64( start ) / I64C(1000);
2221 msg_Dbg( p_demux, "| | | | + ChapterTimeStart: %lld", sk->i_time_offset );
2223 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
2225 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
2226 int64_t i_end = uint64( end );
2228 msg_Dbg( p_demux, "| | | | + ChapterTimeEnd: %lld", i_end );
2230 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
2232 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
2235 msg_Dbg( p_demux, "| | | | + ChapterDisplay" );
2236 for( j = 0; j < cd->ListSize(); j++ )
2238 EbmlElement *l= (*cd)[j];
2240 if( MKV_IS_ID( l, KaxChapterString ) )
2242 KaxChapterString &name =*(KaxChapterString*)l;
2243 char *psz = UTF8ToStr( UTFstring( name ) );
2244 sk->psz_name = strdup( psz );
2245 msg_Dbg( p_demux, "| | | | | + ChapterString '%s'", psz );
2247 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
2249 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
2250 const char *psz = string( lang ).c_str();
2252 msg_Dbg( p_demux, "| | | | | + ChapterLanguage '%s'", psz );
2254 else if( MKV_IS_ID( l, KaxChapterCountry ) )
2256 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
2257 const char *psz = string( ct ).c_str();
2259 msg_Dbg( p_demux, "| | | | | + ChapterCountry '%s'", psz );
2263 else if( MKV_IS_ID( l, KaxChapterAtom ) )
2265 ParseChapterAtom( p_demux, i_level+1, static_cast<EbmlMaster *>(l) );
2268 if( sk->i_time_offset > 0 )
2270 p_sys->title->i_seekpoint++;
2271 p_sys->title->seekpoint = (seekpoint_t**)realloc( p_sys->title->seekpoint, p_sys->title->i_seekpoint * sizeof( seekpoint_t* ) );
2272 p_sys->title->seekpoint[p_sys->title->i_seekpoint-1] = sk;
2276 vlc_seekpoint_Delete( sk );
2280 /*****************************************************************************
2282 *****************************************************************************/
2283 static void ParseChapters( demux_t *p_demux, EbmlElement *chapters )
2285 demux_sys_t *p_sys = p_demux->p_sys;
2289 int i_upper_level = 0;
2292 /* Master elements */
2293 m = static_cast<EbmlMaster *>(chapters);
2294 m->Read( *p_sys->es, chapters->Generic().Context, i_upper_level, el, true );
2296 for( i = 0; i < m->ListSize(); i++ )
2298 EbmlElement *l = (*m)[i];
2300 if( MKV_IS_ID( l, KaxEditionEntry ) )
2302 EbmlMaster *E = static_cast<EbmlMaster *>(l );
2304 msg_Dbg( p_demux, "| | + EditionEntry" );
2305 for( j = 0; j < E->ListSize(); j++ )
2307 EbmlElement *l = (*E)[j];
2309 if( MKV_IS_ID( l, KaxChapterAtom ) )
2311 ParseChapterAtom( p_demux, 0, static_cast<EbmlMaster *>(l) );
2315 msg_Dbg( p_demux, "| | | + Unknown (%s)", typeid(*l).name() );
2321 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2326 /*****************************************************************************
2327 * InformationCreate:
2328 *****************************************************************************/
2329 static void InformationCreate( demux_t *p_demux )
2331 demux_sys_t *p_sys = p_demux->p_sys;
2334 p_sys->meta = vlc_meta_New();
2336 if( p_sys->psz_title )
2338 vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_sys->psz_title );
2340 if( p_sys->psz_date_utc )
2342 vlc_meta_Add( p_sys->meta, VLC_META_DATE, p_sys->psz_date_utc );
2344 if( p_sys->psz_segment_filename )
2346 vlc_meta_Add( p_sys->meta, _("Segment filename"), p_sys->psz_segment_filename );
2348 if( p_sys->psz_muxing_application )
2350 vlc_meta_Add( p_sys->meta, _("Muxing application"), p_sys->psz_muxing_application );
2352 if( p_sys->psz_writing_application )
2354 vlc_meta_Add( p_sys->meta, _("Writing application"), p_sys->psz_writing_application );
2357 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
2359 mkv_track_t *tk = &p_sys->track[i_track];
2360 vlc_meta_t *mtk = vlc_meta_New();
2362 p_sys->meta->track = (vlc_meta_t**)realloc( p_sys->meta->track,
2363 sizeof( vlc_meta_t * ) * ( p_sys->meta->i_track + 1 ) );
2364 p_sys->meta->track[p_sys->meta->i_track++] = mtk;
2366 if( tk->fmt.psz_description )
2368 vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
2370 if( tk->psz_codec_name )
2372 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
2374 if( tk->psz_codec_settings )
2376 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, tk->psz_codec_settings );
2378 if( tk->psz_codec_info_url )
2380 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
2382 if( tk->psz_codec_download_url )
2384 vlc_meta_Add( p_sys->meta, VLC_META_URL, tk->psz_codec_download_url );
2388 if( p_sys->i_tags_position >= 0 )
2390 vlc_bool_t b_seekable;
2392 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
2395 LoadTags( p_demux );
2401 /*****************************************************************************
2403 *****************************************************************************/
2405 static void IndexAppendCluster( demux_t *p_demux, KaxCluster *cluster )
2407 demux_sys_t *p_sys = p_demux->p_sys;
2409 #define idx p_sys->index[p_sys->i_index]
2411 idx.i_block_number= -1;
2412 idx.i_position = cluster->GetElementPosition();
2414 idx.b_key = VLC_TRUE;
2417 if( p_sys->i_index >= p_sys->i_index_max )
2419 p_sys->i_index_max += 1024;
2420 p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
2425 static char * UTF8ToStr( const UTFstring &u )
2434 p = dst = (char*)malloc( i_src + 1);