1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Steve Lhomme <steve.lhomme@free.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
33 # include <time.h> /* time() */
36 #include <vlc/input.h>
38 #include <codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
53 /* libebml and matroska */
54 #include "ebml/EbmlHead.h"
55 #include "ebml/EbmlSubHead.h"
56 #include "ebml/EbmlStream.h"
57 #include "ebml/EbmlContexts.h"
58 #include "ebml/EbmlVoid.h"
59 #include "ebml/StdIOCallback.h"
61 #include "matroska/KaxAttachments.h"
62 #include "matroska/KaxBlock.h"
63 #include "matroska/KaxBlockData.h"
64 #include "matroska/KaxChapters.h"
65 #include "matroska/KaxCluster.h"
66 #include "matroska/KaxClusterData.h"
67 #include "matroska/KaxContexts.h"
68 #include "matroska/KaxCues.h"
69 #include "matroska/KaxCuesData.h"
70 #include "matroska/KaxInfo.h"
71 #include "matroska/KaxInfoData.h"
72 #include "matroska/KaxSeekHead.h"
73 #include "matroska/KaxSegment.h"
74 #include "matroska/KaxTag.h"
75 #include "matroska/KaxTags.h"
76 #include "matroska/KaxTagMulti.h"
77 #include "matroska/KaxTracks.h"
78 #include "matroska/KaxTrackAudio.h"
79 #include "matroska/KaxTrackVideo.h"
80 #include "matroska/KaxTrackEntryData.h"
81 #include "matroska/KaxContentEncoding.h"
83 #include "ebml/StdIOCallback.h"
86 #include "mp4/libmp4.h"
92 #define MATROSKA_COMPRESSION_NONE 0
93 #define MATROSKA_COMPRESSION_ZLIB 1
95 #define MKVD_TIMECODESCALE 1000000
98 * What's between a directory and a filename?
101 #define DIRECTORY_SEPARATOR '\\'
103 #define DIRECTORY_SEPARATOR '/'
106 using namespace LIBMATROSKA_NAMESPACE;
109 /*****************************************************************************
111 *****************************************************************************/
112 static int Open ( vlc_object_t * );
113 static void Close( vlc_object_t * );
116 set_shortname( _("Matroska") );
117 set_description( _("Matroska stream demuxer" ) );
118 set_capability( "demux2", 50 );
119 set_callbacks( Open, Close );
120 set_category( CAT_INPUT );
121 set_subcategory( SUBCAT_INPUT_DEMUX );
123 add_bool( "mkv-seek-percent", 1, NULL,
124 N_("Seek based on percent not time"),
125 N_("Seek based on percent not time"), VLC_TRUE );
127 add_shortcut( "mka" );
128 add_shortcut( "mkv" );
131 /*****************************************************************************
133 *****************************************************************************/
135 block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) {
136 int result, dstsize, n;
141 d_stream.zalloc = (alloc_func)0;
142 d_stream.zfree = (free_func)0;
143 d_stream.opaque = (voidpf)0;
144 result = inflateInit(&d_stream);
147 msg_Dbg( p_this, "inflateInit() failed. Result: %d", result );
151 d_stream.next_in = (Bytef *)p_in_block->p_buffer;
152 d_stream.avail_in = p_in_block->i_buffer;
154 p_block = block_New( p_this, 0 );
159 p_block = block_Realloc( p_block, 0, n * 1000 );
160 dst = (unsigned char *)p_block->p_buffer;
161 d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
162 d_stream.avail_out = 1000;
163 result = inflate(&d_stream, Z_NO_FLUSH);
164 if( ( result != Z_OK ) && ( result != Z_STREAM_END ) )
166 msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result );
170 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
171 ( result != Z_STREAM_END ) );
173 dstsize = d_stream.total_out;
174 inflateEnd( &d_stream );
176 p_block = block_Realloc( p_block, 0, dstsize );
177 p_block->i_buffer = dstsize;
178 block_Release( p_in_block );
185 * Helper function to print the mkv parse tree
187 static void MkvTree( demux_t *p_this, int i_level, char *psz_format, ... )
192 msg_Err( p_this, "too deep tree" );
195 va_start( args, psz_format );
196 static char *psz_foo = "| | | | | | | | | |";
197 char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) );
198 strncpy( psz_foo2, psz_foo, 4 * i_level );
199 psz_foo2[ 4 * i_level ] = '+';
200 psz_foo2[ 4 * i_level + 1 ] = ' ';
201 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
202 __msg_GenericVa( VLC_OBJECT(p_this), VLC_MSG_DBG, "mkv", psz_foo2, args );
207 /*****************************************************************************
209 *****************************************************************************/
210 class vlc_stream_io_callback: public IOCallback
217 vlc_stream_io_callback( stream_t * );
219 virtual uint32 read ( void *p_buffer, size_t i_size);
220 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
221 virtual size_t write ( const void *p_buffer, size_t i_size);
222 virtual uint64 getFilePointer ( void );
223 virtual void close ( void );
226 /*****************************************************************************
228 *****************************************************************************/
232 EbmlParser( EbmlStream *es, EbmlElement *el_start );
237 EbmlElement *Get( void );
240 int GetLevel( void );
245 EbmlElement *m_el[10];
254 /*****************************************************************************
255 * Some functions to manipulate memory
256 *****************************************************************************/
257 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
258 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
260 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
263 /*****************************************************************************
264 * definitions of structures and functions used by this plugins
265 *****************************************************************************/
268 vlc_bool_t b_default;
269 vlc_bool_t b_enabled;
273 uint8_t *p_extra_data;
277 uint64_t i_default_duration;
278 float f_timecodescale;
286 /* data to be send first */
288 uint8_t *p_data_init;
290 /* hack : it's for seek */
291 vlc_bool_t b_search_keyframe;
294 char *psz_codec_name;
295 char *psz_codec_settings;
296 char *psz_codec_info_url;
297 char *psz_codec_download_url;
299 /* encryption/compression */
300 int i_compression_type;
321 ,i_user_start_time(-1)
324 ,b_display_seekpoint(true)
328 int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time, input_title_t & title );
329 const chapter_item_t * FindTimecode( mtime_t i_timecode ) const;
331 int64_t i_start_time, i_end_time;
332 int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */
333 std::vector<chapter_item_t> sub_chapters;
336 bool b_display_seekpoint;
337 std::string psz_name;
338 chapter_item_t *psz_parent;
340 bool operator<( const chapter_item_t & item ) const
342 return ( i_user_start_time < item.i_user_start_time || (i_user_start_time == item.i_user_start_time && i_user_end_time < item.i_user_end_time) );
350 class chapter_edition_t
358 void RefreshChapters( input_title_t & title );
359 double Duration() const;
360 const chapter_item_t * FindTimecode( mtime_t i_timecode ) const;
362 std::vector<chapter_item_t> chapters;
369 class matroska_segment_t
372 matroska_segment_t( demux_sys_t *p_demuxer )
374 ,i_timescale(MKVD_TIMECODESCALE)
377 ,i_chapters_position(0)
384 ,psz_muxing_application(NULL)
385 ,psz_writing_application(NULL)
386 ,psz_segment_filename(NULL)
389 ,i_current_edition(-1)
390 ,psz_current_chapter(NULL)
396 ~matroska_segment_t()
398 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
400 #define tk tracks[i_track]
401 if( tk->fmt.psz_description )
403 free( tk->fmt.psz_description );
407 free( tk->psz_codec );
409 if( tk->fmt.psz_language )
411 free( tk->fmt.psz_language );
417 if( psz_writing_application )
419 free( psz_writing_application );
421 if( psz_muxing_application )
423 free( psz_muxing_application );
425 if( psz_segment_filename )
427 free( psz_segment_filename );
435 free( psz_date_utc );
444 uint64_t i_timescale;
446 /* duration of the segment */
450 std::vector<mkv_track_t*> tracks;
453 int64_t i_cues_position;
454 int64_t i_chapters_position;
455 int64_t i_tags_position;
458 KaxSegmentUID segment_uid;
459 KaxPrevUID prev_segment_uid;
460 KaxNextUID next_segment_uid;
468 char *psz_muxing_application;
469 char *psz_writing_application;
470 char *psz_segment_filename;
474 std::vector<chapter_edition_t> editions;
475 int i_current_edition;
476 const chapter_item_t *psz_current_chapter;
478 std::vector<KaxSegmentFamily> families;
484 inline chapter_edition_t *Edition()
486 if ( i_current_edition >= 0 && size_t(i_current_edition) < editions.size() )
487 return &editions[i_current_edition];
491 bool Preload( demux_t *p_demux );
492 bool PreloadFamily( demux_t *p_demux, const matroska_segment_t & segment );
493 size_t PreloadLinked( demux_t *p_demux, const demux_sys_t & of_sys );
496 class matroska_stream_t
499 matroska_stream_t( demux_sys_t *p_demuxer )
502 ,i_current_segment(-1)
508 for ( size_t i=0; i<segments.size(); i++ )
514 vlc_stream_io_callback *in;
517 std::vector<matroska_segment_t*> segments;
518 int i_current_segment;
522 inline matroska_segment_t *Segment()
524 if ( i_current_segment >= 0 && size_t(i_current_segment) < segments.size() )
525 return segments[i_current_segment];
529 matroska_segment_t *FindSegment( EbmlBinary & uid ) const;
531 void PreloadFamily( demux_t *p_demux, const matroska_segment_t & segment );
532 size_t PreloadLinked( demux_t *p_demux, const demux_sys_t & of_sys );
544 ,i_current_stream(-1)
549 for (size_t i=0; i<streams.size(); i++)
556 mtime_t i_chapter_time;
560 input_title_t *title;
562 std::vector<matroska_stream_t*> streams;
563 int i_current_stream;
565 inline matroska_stream_t *Stream()
567 if ( i_current_stream >= 0 && size_t(i_current_stream) < streams.size() )
568 return streams[i_current_stream];
572 matroska_segment_t *FindSegment( EbmlBinary & uid ) const;
573 void PreloadFamily( demux_t *p_demux );
574 void PreloadLinked( demux_t *p_demux );
575 bool AnalyseAllSegmentsFound( EbmlStream *p_estream, const matroska_segment_t *p_segment );
578 static int Demux ( demux_t * );
579 static int Control( demux_t *, int, va_list );
580 static void Seek ( demux_t *, mtime_t i_date, double f_percent, const chapter_item_t *psz_chapter );
582 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
584 static void IndexAppendCluster ( demux_t *p_demux, KaxCluster *cluster );
585 static char *UTF8ToStr ( const UTFstring &u );
586 static void LoadCues ( demux_t * );
587 static void InformationCreate ( demux_t * );
589 static void ParseInfo( demux_t *, EbmlElement *info );
590 static void ParseTracks( demux_t *, EbmlElement *tracks );
591 static void ParseSeekHead( demux_t *, EbmlElement *seekhead );
592 static void ParseChapters( demux_t *, EbmlElement *chapters );
594 /*****************************************************************************
595 * Open: initializes matroska demux structures
596 *****************************************************************************/
597 static int Open( vlc_object_t * p_this )
599 demux_t *p_demux = (demux_t*)p_this;
601 matroska_stream_t *p_stream;
602 matroska_segment_t *p_segment;
603 mkv_track_t *p_track;
605 std::string s_path, s_filename;
608 EbmlElement *el = NULL;
610 /* peek the begining */
611 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
613 /* is a valid file */
614 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
615 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
617 /* Set the demux function */
618 p_demux->pf_demux = Demux;
619 p_demux->pf_control = Control;
620 p_demux->p_sys = p_sys = new demux_sys_t();
622 p_stream = new matroska_stream_t( p_sys );
623 p_segment = new matroska_segment_t( p_sys );
625 p_sys->streams.push_back( p_stream );
626 p_sys->i_current_stream = 0;
628 p_stream->segments.push_back( p_segment );
629 p_stream->i_current_segment = 0;
631 p_stream->in = new vlc_stream_io_callback( p_demux->s );
632 p_stream->es = new EbmlStream( *p_stream->in );
633 p_track = new mkv_track_t();
634 p_segment->tracks.push_back( p_track );
636 p_segment->i_cues_position = -1;
637 p_segment->i_chapters_position = -1;
638 p_segment->i_tags_position = -1;
640 p_segment->b_cues = VLC_FALSE;
641 p_segment->i_index = 0;
642 p_segment->i_index_max = 1024;
643 p_segment->index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
644 p_segment->i_index_max );
649 if( p_stream->es == NULL )
651 msg_Err( p_demux, "failed to create EbmlStream" );
655 /* Find the EbmlHead element */
656 el = p_stream->es->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
659 msg_Err( p_demux, "cannot find EbmlHead" );
662 msg_Dbg( p_demux, "EbmlHead" );
664 el->SkipData( *p_stream->es, el->Generic().Context );
668 el = p_stream->es->FindNextID( KaxSegment::ClassInfos, 0xFFFFFFFFL);
671 msg_Err( p_demux, "cannot find KaxSegment" );
674 MkvTree( p_demux, 0, "Segment" );
675 p_segment->segment = (KaxSegment*)el;
676 p_segment->cluster = NULL;
678 p_segment->ep = new EbmlParser( p_stream->es, el );
680 p_segment->Preload( p_demux );
682 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
683 /* _todo_ handle multi-segment files */
684 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
686 // assume it's a regular file
687 // get the directory path
688 s_path = p_demux->psz_path;
689 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
691 s_path = s_path.substr(0,s_path.length()-1);
695 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
697 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
701 struct dirent *p_file_item;
702 DIR *p_src_dir = opendir(s_path.c_str());
704 if (p_src_dir != NULL)
706 while ((p_file_item = (dirent *) readdir(p_src_dir)))
708 if (strlen(p_file_item->d_name) > 4)
710 s_filename = s_path + DIRECTORY_SEPARATOR + p_file_item->d_name;
712 if (!s_filename.compare(p_demux->psz_path))
715 #if defined(__GNUC__) && (__GNUC__ < 3)
716 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
717 !s_filename.compare("mka", s_filename.length() - 3, 3))
719 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
720 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
723 // test wether this file belongs to the our family
724 StdIOCallback *p_file_io = new StdIOCallback(s_filename.c_str(), MODE_READ);
725 EbmlStream *p_estream = new EbmlStream(*p_file_io);
727 if ( !p_sys->AnalyseAllSegmentsFound( p_estream, p_segment ))
735 closedir( p_src_dir );
739 if( p_segment->cluster == NULL )
741 msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
745 p_sys->PreloadFamily( p_demux );
746 p_sys->PreloadLinked( p_demux );
748 /* *** Load the cue if found *** */
749 if( p_segment->i_cues_position >= 0 )
751 vlc_bool_t b_seekable;
753 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
760 if( !p_segment->b_cues || p_segment->i_index <= 0 )
762 msg_Warn( p_demux, "no cues/empty cues found->seek won't be precise" );
764 IndexAppendCluster( p_demux, p_segment->cluster );
766 p_segment->b_cues = VLC_FALSE;
770 msg_Dbg( p_demux, "found %d es", p_segment->tracks.size() );
771 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
773 #define tk p_segment->tracks[i_track]
774 if( tk->fmt.i_cat == UNKNOWN_ES )
776 msg_Warn( p_demux, "invalid track[%d, n=%d]", i_track, tk->i_number );
781 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
783 if( tk->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
785 msg_Err( p_demux, "missing/invalid BITMAPINFOHEADER" );
786 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
790 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk->p_extra_data;
792 tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
793 tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
794 tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
796 tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
797 if( tk->fmt.i_extra > 0 )
799 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
800 memcpy( tk->fmt.p_extra, &p_bih[1], tk->fmt.i_extra );
804 else if( !strcmp( tk->psz_codec, "V_MPEG1" ) ||
805 !strcmp( tk->psz_codec, "V_MPEG2" ) )
807 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
809 else if( !strncmp( tk->psz_codec, "V_MPEG4", 7 ) )
811 if( !strcmp( tk->psz_codec, "V_MPEG4/MS/V3" ) )
813 tk->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
815 else if( !strcmp( tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
817 tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
818 tk->fmt.b_packetized = VLC_FALSE;
819 tk->fmt.i_extra = tk->i_extra_data;
820 tk->fmt.p_extra = malloc( tk->i_extra_data );
821 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
825 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
828 else if( !strcmp( tk->psz_codec, "V_QUICKTIME" ) )
830 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
831 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(p_demux),
834 MP4_ReadBoxCommon( p_mp4_stream, p_box );
835 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
836 tk->fmt.i_codec = p_box->i_type;
837 tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
838 tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
839 tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
840 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
841 memcpy( tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tk->fmt.i_extra );
842 MP4_FreeBox_sample_vide( p_box );
843 stream_MemoryDelete( p_mp4_stream, VLC_TRUE );
845 else if( !strcmp( tk->psz_codec, "A_MS/ACM" ) )
847 if( tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
849 msg_Err( p_demux, "missing/invalid WAVEFORMATEX" );
850 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
854 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk->p_extra_data;
856 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk->fmt.i_codec, NULL );
858 tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
859 tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
860 tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
861 tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
862 tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
864 tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
865 if( tk->fmt.i_extra > 0 )
867 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
868 memcpy( tk->fmt.p_extra, &p_wf[1], tk->fmt.i_extra );
872 else if( !strcmp( tk->psz_codec, "A_MPEG/L3" ) ||
873 !strcmp( tk->psz_codec, "A_MPEG/L2" ) ||
874 !strcmp( tk->psz_codec, "A_MPEG/L1" ) )
876 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
878 else if( !strcmp( tk->psz_codec, "A_AC3" ) )
880 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
882 else if( !strcmp( tk->psz_codec, "A_DTS" ) )
884 tk->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
886 else if( !strcmp( tk->psz_codec, "A_FLAC" ) )
888 tk->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
889 tk->fmt.i_extra = tk->i_extra_data;
890 tk->fmt.p_extra = malloc( tk->i_extra_data );
891 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
893 else if( !strcmp( tk->psz_codec, "A_VORBIS" ) )
895 int i, i_offset = 1, i_size[3], i_extra;
898 tk->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
900 /* Split the 3 headers */
901 if( tk->p_extra_data[0] != 0x02 )
902 msg_Err( p_demux, "invalid vorbis header" );
904 for( i = 0; i < 2; i++ )
907 while( i_offset < tk->i_extra_data )
909 i_size[i] += tk->p_extra_data[i_offset];
910 if( tk->p_extra_data[i_offset++] != 0xff ) break;
914 i_size[0] = __MIN(i_size[0], tk->i_extra_data - i_offset);
915 i_size[1] = __MIN(i_size[1], tk->i_extra_data -i_offset -i_size[0]);
916 i_size[2] = tk->i_extra_data - i_offset - i_size[0] - i_size[1];
918 tk->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
919 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
920 p_extra = (uint8_t *)tk->fmt.p_extra; i_extra = 0;
921 for( i = 0; i < 3; i++ )
923 *(p_extra++) = i_size[i] >> 8;
924 *(p_extra++) = i_size[i] & 0xFF;
925 memcpy( p_extra, tk->p_extra_data + i_offset + i_extra,
927 p_extra += i_size[i];
928 i_extra += i_size[i];
931 else if( !strncmp( tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
932 !strncmp( tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
934 int i_profile, i_srate;
935 static unsigned int i_sample_rates[] =
937 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
938 16000, 12000, 11025, 8000, 7350, 0, 0, 0
941 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
942 /* create data for faad (MP4DecSpecificDescrTag)*/
944 if( !strcmp( &tk->psz_codec[12], "MAIN" ) )
948 else if( !strcmp( &tk->psz_codec[12], "LC" ) )
952 else if( !strcmp( &tk->psz_codec[12], "SSR" ) )
961 for( i_srate = 0; i_srate < 13; i_srate++ )
963 if( i_sample_rates[i_srate] == tk->fmt.audio.i_rate )
968 msg_Dbg( p_demux, "profile=%d srate=%d", i_profile, i_srate );
971 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
972 ((uint8_t*)tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
973 ((uint8_t*)tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk->fmt.audio.i_channels << 3);
975 else if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) ||
976 !strcmp( tk->psz_codec, "A_PCM/INT/LIT" ) ||
977 !strcmp( tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
979 if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) )
981 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
985 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
987 tk->fmt.audio.i_blockalign = ( tk->fmt.audio.i_bitspersample + 7 ) / 8 * tk->fmt.audio.i_channels;
989 else if( !strcmp( tk->psz_codec, "A_TTA1" ) )
991 /* FIXME: support this codec */
992 msg_Err( p_demux, "TTA not supported yet[%d, n=%d]", i_track, tk->i_number );
993 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
995 else if( !strcmp( tk->psz_codec, "A_WAVPACK4" ) )
997 /* FIXME: support this codec */
998 msg_Err( p_demux, "Wavpack not supported yet[%d, n=%d]", i_track, tk->i_number );
999 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1001 else if( !strcmp( tk->psz_codec, "S_TEXT/UTF8" ) )
1003 tk->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1004 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1006 else if( !strcmp( tk->psz_codec, "S_TEXT/SSA" ) ||
1007 !strcmp( tk->psz_codec, "S_TEXT/ASS" ) ||
1008 !strcmp( tk->psz_codec, "S_SSA" ) ||
1009 !strcmp( tk->psz_codec, "S_ASS" ))
1011 tk->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1012 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1014 else if( !strcmp( tk->psz_codec, "S_VOBSUB" ) )
1016 tk->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1017 if( tk->i_extra_data )
1020 char *p_buf = (char *)malloc( tk->i_extra_data + 1);
1021 memcpy( p_buf, tk->p_extra_data , tk->i_extra_data );
1022 p_buf[tk->i_extra_data] = '\0';
1024 p_start = strstr( p_buf, "size:" );
1025 if( sscanf( p_start, "size: %dx%d",
1026 &tk->fmt.subs.spu.i_original_frame_width, &tk->fmt.subs.spu.i_original_frame_height ) == 2 )
1028 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 );
1032 msg_Warn( p_demux, "reading original frame size for vobsub failed" );
1037 else if( !strcmp( tk->psz_codec, "B_VOBBTN" ) )
1039 /* FIXME: support this codec */
1040 msg_Err( p_demux, "Vob Buttons not supported yet[%d, n=%d]", i_track, tk->i_number );
1041 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1045 msg_Err( p_demux, "unknow codec id=`%s'", tk->psz_codec );
1046 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1050 tk->fmt.i_priority = 1000;
1053 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1057 /* add information */
1058 InformationCreate( p_demux );
1064 return VLC_EGENERIC;
1067 /*****************************************************************************
1068 * Close: frees unused data
1069 *****************************************************************************/
1070 static void Close( vlc_object_t *p_this )
1072 demux_t *p_demux = (demux_t*)p_this;
1073 demux_sys_t *p_sys = p_demux->p_sys;
1074 matroska_stream_t *p_stream = p_sys->Stream();
1075 matroska_segment_t *p_segment = p_stream->Segment();
1077 /* TODO close everything ? */
1079 delete p_segment->segment;
1084 /*****************************************************************************
1086 *****************************************************************************/
1087 static int Control( demux_t *p_demux, int i_query, va_list args )
1089 demux_sys_t *p_sys = p_demux->p_sys;
1090 matroska_stream_t *p_stream = p_sys->Stream();
1091 matroska_segment_t *p_segment = p_stream->Segment();
1096 vlc_meta_t **pp_meta;
1100 case DEMUX_GET_META:
1101 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1102 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1105 case DEMUX_GET_LENGTH:
1106 pi64 = (int64_t*)va_arg( args, int64_t * );
1107 if( p_segment->f_duration > 0.0 )
1109 *pi64 = (int64_t)(p_segment->f_duration * 1000);
1112 return VLC_EGENERIC;
1114 case DEMUX_GET_POSITION:
1115 pf = (double*)va_arg( args, double * );
1116 *pf = (double)p_sys->i_pts / (1000.0 * p_segment->f_duration);
1119 case DEMUX_SET_POSITION:
1120 f = (double)va_arg( args, double );
1121 Seek( p_demux, -1, f, NULL );
1124 case DEMUX_GET_TIME:
1125 pi64 = (int64_t*)va_arg( args, int64_t * );
1126 *pi64 = p_sys->i_pts;
1129 case DEMUX_GET_TITLE_INFO:
1130 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
1132 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1133 int *pi_int = (int*)va_arg( args, int* );
1136 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) );
1138 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->title );
1142 return VLC_EGENERIC;
1144 case DEMUX_SET_TITLE:
1145 /* TODO handle editions as titles & DVD titles as well */
1146 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
1150 return VLC_EGENERIC;
1152 case DEMUX_SET_SEEKPOINT:
1153 /* FIXME do a better implementation */
1154 i_skp = (int)va_arg( args, int );
1156 if( p_sys->title && i_skp < p_sys->title->i_seekpoint)
1158 Seek( p_demux, (int64_t)p_sys->title->seekpoint[i_skp]->i_time_offset, -1, NULL);
1159 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
1160 p_demux->info.i_seekpoint = i_skp;
1163 return VLC_EGENERIC;
1165 case DEMUX_SET_TIME:
1168 return VLC_EGENERIC;
1172 static int BlockGet( demux_t *p_demux, KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1174 demux_sys_t *p_sys = p_demux->p_sys;
1175 matroska_stream_t *p_stream = p_sys->Stream();
1176 matroska_segment_t *p_segment = p_stream->Segment();
1187 if( p_demux->b_die )
1189 return VLC_EGENERIC;
1192 el = p_segment->ep->Get();
1193 i_level = p_segment->ep->GetLevel();
1195 if( el == NULL && *pp_block != NULL )
1197 /* update the index */
1198 #define idx p_segment->index[p_segment->i_index - 1]
1199 if( p_segment->i_index > 0 && idx.i_time == -1 )
1201 idx.i_time = (*pp_block)->GlobalTimecode() / (mtime_t)1000;
1202 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1210 if( p_segment->ep->GetLevel() > 1 )
1212 p_segment->ep->Up();
1215 msg_Warn( p_demux, "EOF" );
1216 return VLC_EGENERIC;
1222 if( MKV_IS_ID( el, KaxCluster ) )
1224 p_segment->cluster = (KaxCluster*)el;
1226 /* add it to the index */
1227 if( p_segment->i_index == 0 ||
1228 ( p_segment->i_index > 0 && p_segment->index[p_segment->i_index - 1].i_position < (int64_t)p_segment->cluster->GetElementPosition() ) )
1230 IndexAppendCluster( p_demux, p_segment->cluster );
1233 p_segment->ep->Down();
1235 else if( MKV_IS_ID( el, KaxCues ) )
1237 msg_Warn( p_demux, "find KaxCues FIXME" );
1238 return VLC_EGENERIC;
1242 msg_Dbg( p_demux, "unknown (%s)", typeid( el ).name() );
1245 else if( i_level == 2 )
1247 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1249 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1251 ctc.ReadData( p_stream->es->I_O(), SCOPE_ALL_DATA );
1252 p_segment->cluster->InitTimecode( uint64( ctc ), p_segment->i_timescale );
1254 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1256 p_segment->ep->Down();
1259 else if( i_level == 3 )
1261 if( MKV_IS_ID( el, KaxBlock ) )
1263 *pp_block = (KaxBlock*)el;
1265 (*pp_block)->ReadData( p_stream->es->I_O() );
1266 (*pp_block)->SetParent( *p_segment->cluster );
1268 p_segment->ep->Keep();
1270 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1272 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1274 dur.ReadData( p_stream->es->I_O() );
1275 *pi_duration = uint64( dur );
1277 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1279 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1281 ref.ReadData( p_stream->es->I_O() );
1282 if( *pi_ref1 == -1 )
1284 *pi_ref1 = int64( ref );
1288 *pi_ref2 = int64( ref );
1294 msg_Err( p_demux, "invalid level = %d", i_level );
1295 return VLC_EGENERIC;
1300 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1303 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1304 memcpy( p_block->p_buffer, p_mem, i_mem );
1305 //p_block->i_rate = p_input->stream.control.i_rate;
1309 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1310 mtime_t i_duration )
1312 demux_sys_t *p_sys = p_demux->p_sys;
1313 matroska_stream_t *p_stream = p_sys->Stream();
1314 matroska_segment_t *p_segment = p_stream->Segment();
1320 #define tk p_segment->tracks[i_track]
1321 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1323 if( tk->i_number == block->TrackNum() )
1329 if( i_track >= p_segment->tracks.size() )
1331 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1334 if( tk->p_es == NULL )
1336 msg_Err( p_demux, "unknown track number=%d", block->TrackNum() );
1339 if( i_pts < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1341 return; /* discard audio packets that shouldn't be rendered */
1344 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1347 tk->b_inited = VLC_FALSE;
1351 /* First send init data */
1352 if( !tk->b_inited && tk->i_data_init > 0 )
1356 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
1357 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init );
1358 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
1360 tk->b_inited = VLC_TRUE;
1363 for( i = 0; i < block->NumberFrames(); i++ )
1366 DataBuffer &data = block->GetBuffer(i);
1368 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1370 if( p_block == NULL )
1375 #if defined(HAVE_ZLIB_H)
1376 if( tk->i_compression_type )
1378 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
1382 // TODO implement correct timestamping when B frames are used
1383 if( tk->fmt.i_cat != VIDEO_ES )
1385 p_block->i_dts = p_block->i_pts = i_pts;
1389 p_block->i_dts = i_pts;
1393 if( tk->fmt.i_cat == SPU_ES && strcmp( tk->psz_codec, "S_VOBSUB" ) )
1395 p_block->i_length = i_duration * 1000;
1397 es_out_Send( p_demux->out, tk->p_es, p_block );
1399 /* use time stamp only for first block */
1406 bool demux_sys_t::AnalyseAllSegmentsFound( EbmlStream *p_estream, const matroska_segment_t *p_segment )
1409 int i_upper_lvl = 0;
1411 EbmlElement *p_l0, *p_l1, *p_l2;
1413 // verify the EBML Header
1414 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
1420 matroska_stream_t *p_stream1 = new matroska_stream_t( this );
1421 streams.push_back( p_stream1 );
1423 p_l0->SkipData(*p_estream, EbmlHead_Context);
1426 // find all segments in this file
1427 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1435 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
1438 matroska_segment_t *p_segment1 = new matroska_segment_t( this );
1440 p_stream1->segments.push_back( p_segment1 );
1442 ep = new EbmlParser(p_estream, p_l0);
1443 p_segment1->ep = ep;
1445 while ((p_l1 = ep->Get()))
1447 if (MKV_IS_ID(p_l1, KaxInfo))
1449 // find the families of this segment
1450 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
1452 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
1453 for( i = 0; i < p_info->ListSize(); i++ )
1455 EbmlElement *l = (*p_info)[i];
1457 if( MKV_IS_ID( l, KaxSegmentUID ) )
1459 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
1460 if (p_segment && p_segment->segment_uid == *p_uid)
1462 p_segment1->segment_uid = *( new KaxSegmentUID(*p_uid) );
1464 else if( MKV_IS_ID( l, KaxPrevUID ) )
1466 p_segment1->prev_segment_uid = *( new KaxPrevUID( *static_cast<KaxPrevUID*>(l) ) );
1468 else if( MKV_IS_ID( l, KaxNextUID ) )
1470 p_segment1->next_segment_uid = *( new KaxNextUID( *static_cast<KaxNextUID*>(l) ) );
1472 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
1474 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
1475 std::vector<KaxSegmentFamily>::iterator iter;
1476 p_segment1->families.push_back( *p_fam );
1484 p_l0->SkipData(*p_estream, EbmlHead_Context);
1486 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1492 static void UpdateCurrentToChapter( demux_t & demux )
1494 demux_sys_t & sys = *demux.p_sys;
1495 matroska_stream_t *p_stream = sys.Stream();
1496 matroska_segment_t *p_segment = p_stream->Segment();
1497 const chapter_item_t *psz_curr_chapter;
1499 /* update current chapter/seekpoint */
1500 if ( p_segment->editions.size())
1502 /* 1st, we need to know in which chapter we are */
1503 psz_curr_chapter = p_segment->editions[p_segment->i_current_edition].FindTimecode( sys.i_pts );
1505 /* we have moved to a new chapter */
1506 if (p_segment->psz_current_chapter != NULL && psz_curr_chapter != NULL && p_segment->psz_current_chapter != psz_curr_chapter)
1508 if (p_segment->psz_current_chapter->i_seekpoint_num != psz_curr_chapter->i_seekpoint_num && psz_curr_chapter->i_seekpoint_num > 0)
1510 demux.info.i_update |= INPUT_UPDATE_SEEKPOINT;
1511 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
1514 if (p_segment->editions[p_segment->i_current_edition].b_ordered )
1516 /* TODO check if we need to silently seek to a new location in the stream (switch to another chapter) */
1517 if (p_segment->psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time)
1518 Seek(&demux, sys.i_pts, -1, psz_curr_chapter);
1519 /* count the last duration time found for each track in a table (-1 not found, -2 silent) */
1520 /* only seek after each duration >= end timecode of the current chapter */
1523 // p_segment->i_user_time = psz_curr_chapter->i_user_start_time - psz_curr_chapter->i_start_time;
1524 // p_segment->i_start_pts = psz_curr_chapter->i_user_start_time;
1526 p_segment->psz_current_chapter = psz_curr_chapter;
1530 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, const chapter_item_t *psz_chapter)
1532 demux_sys_t *p_sys = p_demux->p_sys;
1533 matroska_stream_t *p_stream = p_sys->Stream();
1534 matroska_segment_t *p_segment = p_stream->Segment();
1535 mtime_t i_time_offset = 0;
1538 int64_t i_block_duration;
1539 int64_t i_block_ref1;
1540 int64_t i_block_ref2;
1543 int i_track_skipping;
1546 msg_Dbg( p_demux, "seek request to "I64Fd" (%f%%)", i_date, f_percent );
1547 if( i_date < 0 && f_percent < 0 )
1549 msg_Warn( p_demux, "cannot seek nowhere !" );
1552 if( f_percent > 1.0 )
1554 msg_Warn( p_demux, "cannot seek so far !" );
1558 delete p_segment->ep;
1559 p_segment->ep = new EbmlParser( p_stream->es, p_segment->segment );
1560 p_segment->cluster = NULL;
1562 /* seek without index or without date */
1563 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
1565 if (p_segment->f_duration >= 0)
1567 i_date = int64_t( f_percent * p_segment->f_duration * 1000.0 );
1571 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
1573 msg_Dbg( p_demux, "inacurate way of seeking" );
1574 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
1576 if( p_segment->index[i_index].i_position >= i_pos)
1581 if( i_index == p_segment->i_index )
1586 i_date = p_segment->index[i_index].i_time;
1589 if( p_segment->index[i_index].i_position < i_pos )
1593 msg_Warn( p_demux, "searching for cluster, could take some time" );
1595 /* search a cluster */
1596 while( ( el = p_sys->ep->Get() ) != NULL )
1598 if( MKV_IS_ID( el, KaxCluster ) )
1600 KaxCluster *cluster = (KaxCluster*)el;
1602 /* add it to the index */
1603 IndexAppendCluster( p_demux, cluster );
1605 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1607 p_sys->cluster = cluster;
1618 // find the actual time for an ordered edition
1619 if ( psz_chapter == NULL )
1621 if ( p_segment->editions.size() && p_segment->editions[p_segment->i_current_edition].b_ordered )
1623 /* 1st, we need to know in which chapter we are */
1624 psz_chapter = p_segment->editions[p_segment->i_current_edition].FindTimecode( i_date );
1628 if ( psz_chapter != NULL )
1630 p_segment->psz_current_chapter = psz_chapter;
1631 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
1632 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1633 p_demux->info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
1636 for( ; i_index < p_segment->i_index; i_index++ )
1638 if( p_segment->index[i_index].i_time + i_time_offset > i_date )
1649 msg_Dbg( p_demux, "seek got "I64Fd" (%d%%)",
1650 p_segment->index[i_index].i_time,
1651 (int)( 100 * p_segment->index[i_index].i_position /
1652 stream_Size( p_demux->s ) ) );
1654 p_stream->in->setFilePointer( p_segment->index[i_index].i_position,
1657 p_sys->i_start_pts = i_date;
1659 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1661 /* now parse until key frame */
1662 #define tk p_segment->tracks[i_track]
1663 i_track_skipping = 0;
1664 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1666 if( tk->fmt.i_cat == VIDEO_ES )
1668 tk->b_search_keyframe = VLC_TRUE;
1671 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_date );
1675 while( i_track_skipping > 0 )
1677 if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1679 msg_Warn( p_demux, "cannot get block EOF?" );
1684 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1686 if( tk->i_number == block->TrackNum() )
1692 p_sys->i_pts = p_sys->i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
1694 if( i_track < p_segment->tracks.size() )
1696 if( tk->fmt.i_cat == VIDEO_ES )
1698 if( i_block_ref1 == -1 && tk->b_search_keyframe )
1700 tk->b_search_keyframe = VLC_FALSE;
1703 if( !tk->b_search_keyframe )
1705 BlockDecode( p_demux, block, p_sys->i_pts, 0 );
1715 /*****************************************************************************
1716 * Demux: reads and demuxes data packets
1717 *****************************************************************************
1718 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1719 *****************************************************************************/
1720 static int Demux( demux_t *p_demux)
1722 demux_sys_t *p_sys = p_demux->p_sys;
1723 matroska_stream_t *p_stream = p_sys->Stream();
1724 matroska_segment_t *p_segment = p_stream->Segment();
1725 int i_block_count = 0;
1728 int64_t i_block_duration;
1729 int64_t i_block_ref1;
1730 int64_t i_block_ref2;
1734 if( p_sys->i_pts >= p_sys->i_start_pts )
1735 UpdateCurrentToChapter( *p_demux );
1737 if ( p_segment->editions.size() && p_segment->editions[p_segment->i_current_edition].b_ordered && p_segment->psz_current_chapter == NULL )
1739 /* nothing left to read in this ordered edition */
1743 if( BlockGet( p_demux, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1745 if ( p_segment->editions.size() && p_segment->editions[p_segment->i_current_edition].b_ordered )
1747 // check if there are more chapters to read
1748 if ( p_segment->psz_current_chapter != NULL )
1750 p_sys->i_pts = p_segment->psz_current_chapter->i_user_end_time;
1756 msg_Warn( p_demux, "cannot get block EOF?" );
1761 p_sys->i_pts = p_sys->i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
1763 if( p_sys->i_pts >= p_sys->i_start_pts )
1765 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
1768 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
1773 // TODO optimize when there is need to leave or when seeking has been called
1774 if( i_block_count > 5 )
1783 /*****************************************************************************
1785 *****************************************************************************/
1786 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1792 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1794 if( i_size <= 0 || mb_eof )
1799 return stream_Read( s, p_buffer, i_size );
1801 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1807 case seek_beginning:
1811 i_pos = stream_Size( s ) - i_offset;
1814 i_pos= stream_Tell( s ) + i_offset;
1818 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1825 if( stream_Seek( s, i_pos ) )
1831 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1835 uint64 vlc_stream_io_callback::getFilePointer( void )
1837 return stream_Tell( s );
1839 void vlc_stream_io_callback::close( void )
1845 /*****************************************************************************
1846 * Ebml Stream parser
1847 *****************************************************************************/
1848 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1856 for( i = 1; i < 6; i++ )
1862 mb_keep = VLC_FALSE;
1865 EbmlParser::~EbmlParser( void )
1869 for( i = 1; i < mi_level; i++ )
1875 mb_keep = VLC_FALSE;
1879 void EbmlParser::Up( void )
1881 if( mi_user_level == mi_level )
1883 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1889 void EbmlParser::Down( void )
1895 void EbmlParser::Keep( void )
1900 int EbmlParser::GetLevel( void )
1902 return mi_user_level;
1905 EbmlElement *EbmlParser::Get( void )
1909 if( mi_user_level != mi_level )
1915 EbmlElement *ret = m_got;
1921 if( m_el[mi_level] )
1923 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
1926 delete m_el[mi_level];
1928 mb_keep = VLC_FALSE;
1931 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
1942 delete m_el[mi_level - 1];
1943 m_got = m_el[mi_level -1] = m_el[mi_level];
1944 m_el[mi_level] = NULL;
1951 else if( m_el[mi_level] == NULL )
1953 fprintf( stderr," m_el[mi_level] == NULL\n" );
1956 return m_el[mi_level];
1960 /*****************************************************************************
1962 * * LoadCues : load the cues element and update index
1964 * * LoadTags : load ... the tags element
1966 * * InformationCreate : create all information, load tags if present
1968 *****************************************************************************/
1969 static void LoadCues( demux_t *p_demux )
1971 demux_sys_t *p_sys = p_demux->p_sys;
1972 matroska_stream_t *p_stream = p_sys->Stream();
1973 matroska_segment_t *p_segment = p_stream->Segment();
1974 int64_t i_sav_position = p_stream->in->getFilePointer();
1976 EbmlElement *el, *cues;
1978 msg_Dbg( p_demux, "loading cues" );
1979 p_stream->in->setFilePointer( p_segment->i_cues_position, seek_beginning );
1980 cues = p_stream->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
1984 msg_Err( p_demux, "cannot load cues (broken seekhead or file)" );
1985 p_stream->in->setFilePointer( i_sav_position, seek_beginning );
1989 ep = new EbmlParser( p_stream->es, cues );
1990 while( ( el = ep->Get() ) != NULL )
1992 if( MKV_IS_ID( el, KaxCuePoint ) )
1994 #define idx p_segment->index[p_segment->i_index]
1997 idx.i_block_number= -1;
1998 idx.i_position = -1;
2000 idx.b_key = VLC_TRUE;
2003 while( ( el = ep->Get() ) != NULL )
2005 if( MKV_IS_ID( el, KaxCueTime ) )
2007 KaxCueTime &ctime = *(KaxCueTime*)el;
2009 ctime.ReadData( p_stream->es->I_O() );
2011 idx.i_time = uint64( ctime ) * p_segment->i_timescale / (mtime_t)1000;
2013 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
2016 while( ( el = ep->Get() ) != NULL )
2018 if( MKV_IS_ID( el, KaxCueTrack ) )
2020 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
2022 ctrack.ReadData( p_stream->es->I_O() );
2023 idx.i_track = uint16( ctrack );
2025 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
2027 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
2029 ccpos.ReadData( p_stream->es->I_O() );
2030 idx.i_position = p_segment->segment->GetGlobalPosition( uint64( ccpos ) );
2032 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
2034 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2036 cbnum.ReadData( p_stream->es->I_O() );
2037 idx.i_block_number = uint32( cbnum );
2041 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2048 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2054 msg_Dbg( p_demux, " * added time="I64Fd" pos="I64Fd
2055 " track=%d bnum=%d", idx.i_time, idx.i_position,
2056 idx.i_track, idx.i_block_number );
2059 p_segment->i_index++;
2060 if( p_segment->i_index >= p_segment->i_index_max )
2062 p_segment->i_index_max += 1024;
2063 p_segment->index = (mkv_index_t*)realloc( p_segment->index, sizeof( mkv_index_t ) * p_segment->i_index_max );
2069 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2075 p_segment->b_cues = VLC_TRUE;
2077 msg_Dbg( p_demux, "loading cues done." );
2078 p_stream->in->setFilePointer( i_sav_position, seek_beginning );
2081 static void LoadTags( demux_t *p_demux )
2083 demux_sys_t *p_sys = p_demux->p_sys;
2084 matroska_stream_t *p_stream = p_sys->Stream();
2085 matroska_segment_t *p_segment = p_stream->Segment();
2086 int64_t i_sav_position = p_stream->in->getFilePointer();
2088 EbmlElement *el, *tags;
2090 msg_Dbg( p_demux, "loading tags" );
2091 p_stream->in->setFilePointer( p_segment->i_tags_position, seek_beginning );
2092 tags = p_stream->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2096 msg_Err( p_demux, "cannot load tags (broken seekhead or file)" );
2097 p_stream->in->setFilePointer( i_sav_position, seek_beginning );
2101 msg_Dbg( p_demux, "Tags" );
2102 ep = new EbmlParser( p_stream->es, tags );
2103 while( ( el = ep->Get() ) != NULL )
2105 if( MKV_IS_ID( el, KaxTag ) )
2107 msg_Dbg( p_demux, "+ Tag" );
2109 while( ( el = ep->Get() ) != NULL )
2111 if( MKV_IS_ID( el, KaxTagTargets ) )
2113 msg_Dbg( p_demux, "| + Targets" );
2115 while( ( el = ep->Get() ) != NULL )
2117 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2121 else if( MKV_IS_ID( el, KaxTagGeneral ) )
2123 msg_Dbg( p_demux, "| + General" );
2125 while( ( el = ep->Get() ) != NULL )
2127 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2131 else if( MKV_IS_ID( el, KaxTagGenres ) )
2133 msg_Dbg( p_demux, "| + Genres" );
2135 while( ( el = ep->Get() ) != NULL )
2137 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2141 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
2143 msg_Dbg( p_demux, "| + Audio Specific" );
2145 while( ( el = ep->Get() ) != NULL )
2147 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2151 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
2153 msg_Dbg( p_demux, "| + Images Specific" );
2155 while( ( el = ep->Get() ) != NULL )
2157 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2161 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
2163 msg_Dbg( p_demux, "| + Multi Comment" );
2165 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
2167 msg_Dbg( p_demux, "| + Multi Commercial" );
2169 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
2171 msg_Dbg( p_demux, "| + Multi Date" );
2173 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
2175 msg_Dbg( p_demux, "| + Multi Entity" );
2177 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
2179 msg_Dbg( p_demux, "| + Multi Identifier" );
2181 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
2183 msg_Dbg( p_demux, "| + Multi Legal" );
2185 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
2187 msg_Dbg( p_demux, "| + Multi Title" );
2191 msg_Dbg( p_demux, "| + Unknown (%s)", typeid( *el ).name() );
2198 msg_Dbg( p_demux, "+ Unknown (%s)", typeid( *el ).name() );
2204 msg_Dbg( p_demux, "loading tags done." );
2205 p_stream->in->setFilePointer( i_sav_position, seek_beginning );
2208 /*****************************************************************************
2210 *****************************************************************************/
2211 static void ParseSeekHead( demux_t *p_demux, EbmlElement *seekhead )
2213 demux_sys_t *p_sys = p_demux->p_sys;
2214 matroska_stream_t *p_stream = p_sys->Stream();
2215 matroska_segment_t *p_segment = p_stream->Segment();
2219 int i_upper_level = 0;
2221 msg_Dbg( p_demux, "| + Seek head" );
2223 /* Master elements */
2224 m = static_cast<EbmlMaster *>(seekhead);
2225 m->Read( *p_stream->es, seekhead->Generic().Context, i_upper_level, el, true );
2227 for( i = 0; i < m->ListSize(); i++ )
2229 EbmlElement *l = (*m)[i];
2231 if( MKV_IS_ID( l, KaxSeek ) )
2233 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
2234 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
2239 for( j = 0; j < sk->ListSize(); j++ )
2241 EbmlElement *l = (*sk)[j];
2243 if( MKV_IS_ID( l, KaxSeekID ) )
2245 KaxSeekID &sid = *(KaxSeekID*)l;
2246 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
2248 else if( MKV_IS_ID( l, KaxSeekPosition ) )
2250 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
2251 i_pos = uint64( spos );
2255 msg_Dbg( p_demux, "| | | + Unknown (%s)", typeid(*l).name() );
2261 if( id == KaxCues::ClassInfos.GlobalId )
2263 msg_Dbg( p_demux, "| | | = cues at "I64Fd, i_pos );
2264 p_segment->i_cues_position = p_segment->segment->GetGlobalPosition( i_pos );
2266 else if( id == KaxChapters::ClassInfos.GlobalId )
2268 msg_Dbg( p_demux, "| | | = chapters at "I64Fd, i_pos );
2269 p_segment->i_chapters_position = p_segment->segment->GetGlobalPosition( i_pos );
2271 else if( id == KaxTags::ClassInfos.GlobalId )
2273 msg_Dbg( p_demux, "| | | = tags at "I64Fd, i_pos );
2274 p_segment->i_tags_position = p_segment->segment->GetGlobalPosition( i_pos );
2280 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2285 /*****************************************************************************
2287 *****************************************************************************/
2288 static void ParseTrackEntry( demux_t *p_demux, EbmlMaster *m )
2290 demux_sys_t *p_sys = p_demux->p_sys;
2291 matroska_stream_t *p_stream = p_sys->Stream();
2292 matroska_segment_t *p_segment = p_stream->Segment();
2297 msg_Dbg( p_demux, "| | + Track Entry" );
2299 tk = new mkv_track_t();
2300 p_segment->tracks.push_back( tk );
2302 /* Init the track */
2303 memset( tk, 0, sizeof( mkv_track_t ) );
2305 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
2306 tk->fmt.psz_language = strdup("English");
2307 tk->fmt.psz_description = NULL;
2309 tk->b_default = VLC_TRUE;
2310 tk->b_enabled = VLC_TRUE;
2311 tk->i_number = p_segment->tracks.size() - 1;
2312 tk->i_extra_data = 0;
2313 tk->p_extra_data = NULL;
2314 tk->psz_codec = NULL;
2315 tk->i_default_duration = 0;
2316 tk->f_timecodescale = 1.0;
2318 tk->b_inited = VLC_FALSE;
2319 tk->i_data_init = 0;
2320 tk->p_data_init = NULL;
2322 tk->psz_codec_name = NULL;
2323 tk->psz_codec_settings = NULL;
2324 tk->psz_codec_info_url = NULL;
2325 tk->psz_codec_download_url = NULL;
2327 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
2329 for( i = 0; i < m->ListSize(); i++ )
2331 EbmlElement *l = (*m)[i];
2333 if( MKV_IS_ID( l, KaxTrackNumber ) )
2335 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
2337 tk->i_number = uint32( tnum );
2338 msg_Dbg( p_demux, "| | | + Track Number=%u", uint32( tnum ) );
2340 else if( MKV_IS_ID( l, KaxTrackUID ) )
2342 KaxTrackUID &tuid = *(KaxTrackUID*)l;
2344 msg_Dbg( p_demux, "| | | + Track UID=%u", uint32( tuid ) );
2346 else if( MKV_IS_ID( l, KaxTrackType ) )
2349 KaxTrackType &ttype = *(KaxTrackType*)l;
2351 switch( uint8(ttype) )
2355 tk->fmt.i_cat = AUDIO_ES;
2359 tk->fmt.i_cat = VIDEO_ES;
2361 case track_subtitle:
2362 psz_type = "subtitle";
2363 tk->fmt.i_cat = SPU_ES;
2366 psz_type = "unknown";
2367 tk->fmt.i_cat = UNKNOWN_ES;
2371 msg_Dbg( p_demux, "| | | + Track Type=%s", psz_type );
2373 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
2375 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
2377 // tk->b_enabled = uint32( fenb );
2378 // msg_Dbg( p_demux, "| | | + Track Enabled=%u",
2379 // uint32( fenb ) );
2381 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
2383 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
2385 tk->b_default = uint32( fdef );
2386 msg_Dbg( p_demux, "| | | + Track Default=%u", uint32( fdef ) );
2388 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
2390 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
2392 msg_Dbg( p_demux, "| | | + Track Lacing=%d", uint32( lac ) );
2394 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
2396 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
2398 msg_Dbg( p_demux, "| | | + Track MinCache=%d", uint32( cmin ) );
2400 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
2402 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
2404 msg_Dbg( p_demux, "| | | + Track MaxCache=%d", uint32( cmax ) );
2406 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
2408 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
2410 tk->i_default_duration = uint64(defd);
2411 msg_Dbg( p_demux, "| | | + Track Default Duration="I64Fd, uint64(defd) );
2413 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
2415 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
2417 tk->f_timecodescale = float( ttcs );
2418 msg_Dbg( p_demux, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
2420 else if( MKV_IS_ID( l, KaxTrackName ) )
2422 KaxTrackName &tname = *(KaxTrackName*)l;
2424 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
2425 msg_Dbg( p_demux, "| | | + Track Name=%s", tk->fmt.psz_description );
2427 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
2429 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
2431 tk->fmt.psz_language = strdup( string( lang ).c_str() );
2433 "| | | + Track Language=`%s'", tk->fmt.psz_language );
2435 else if( MKV_IS_ID( l, KaxCodecID ) )
2437 KaxCodecID &codecid = *(KaxCodecID*)l;
2439 tk->psz_codec = strdup( string( codecid ).c_str() );
2440 msg_Dbg( p_demux, "| | | + Track CodecId=%s", string( codecid ).c_str() );
2442 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
2444 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
2446 tk->i_extra_data = cpriv.GetSize();
2447 if( tk->i_extra_data > 0 )
2449 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
2450 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
2452 msg_Dbg( p_demux, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
2454 else if( MKV_IS_ID( l, KaxCodecName ) )
2456 KaxCodecName &cname = *(KaxCodecName*)l;
2458 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
2459 msg_Dbg( p_demux, "| | | + Track Codec Name=%s", tk->psz_codec_name );
2461 else if( MKV_IS_ID( l, KaxContentEncodings ) )
2463 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
2464 MkvTree( p_demux, 3, "Content Encodings" );
2465 for( unsigned int i = 0; i < cencs->ListSize(); i++ )
2467 EbmlElement *l2 = (*cencs)[i];
2468 if( MKV_IS_ID( l2, KaxContentEncoding ) )
2470 MkvTree( p_demux, 4, "Content Encoding" );
2471 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
2472 for( unsigned int i = 0; i < cenc->ListSize(); i++ )
2474 EbmlElement *l3 = (*cenc)[i];
2475 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
2477 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
2478 MkvTree( p_demux, 5, "Order: %i", uint32( encord ) );
2480 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
2482 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
2483 MkvTree( p_demux, 5, "Scope: %i", uint32( encscope ) );
2485 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
2487 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
2488 MkvTree( p_demux, 5, "Type: %i", uint32( enctype ) );
2490 else if( MKV_IS_ID( l3, KaxContentCompression ) )
2492 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
2493 MkvTree( p_demux, 5, "Content Compression" );
2494 for( unsigned int i = 0; i < compr->ListSize(); i++ )
2496 EbmlElement *l4 = (*compr)[i];
2497 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
2499 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
2500 MkvTree( p_demux, 6, "Compression Algorithm: %i", uint32(compalg) );
2501 if( uint32( compalg ) == 0 )
2503 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
2508 MkvTree( p_demux, 6, "Unknown (%s)", typeid(*l4).name() );
2515 MkvTree( p_demux, 5, "Unknown (%s)", typeid(*l3).name() );
2522 MkvTree( p_demux, 4, "Unknown (%s)", typeid(*l2).name() );
2527 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
2529 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
2531 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
2532 // msg_Dbg( p_demux, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
2534 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
2536 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
2538 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
2539 // msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
2541 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
2543 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
2545 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
2546 // msg_Dbg( p_demux, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
2548 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
2550 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
2552 // msg_Dbg( p_demux, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
2554 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
2556 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
2558 // msg_Dbg( p_demux, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
2560 else if( MKV_IS_ID( l, KaxTrackVideo ) )
2562 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
2565 msg_Dbg( p_demux, "| | | + Track Video" );
2568 for( j = 0; j < tkv->ListSize(); j++ )
2570 EbmlElement *l = (*tkv)[j];
2571 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
2573 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
2575 // msg_Dbg( p_demux, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
2577 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
2579 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
2581 // msg_Dbg( p_demux, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
2584 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
2586 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
2588 tk->fmt.video.i_width = uint16( vwidth );
2589 msg_Dbg( p_demux, "| | | | + width=%d", uint16( vwidth ) );
2591 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
2593 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
2595 tk->fmt.video.i_height = uint16( vheight );
2596 msg_Dbg( p_demux, "| | | | + height=%d", uint16( vheight ) );
2598 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
2600 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
2602 tk->fmt.video.i_visible_width = uint16( vwidth );
2603 msg_Dbg( p_demux, "| | | | + display width=%d", uint16( vwidth ) );
2605 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
2607 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
2609 tk->fmt.video.i_visible_height = uint16( vheight );
2610 msg_Dbg( p_demux, "| | | | + display height=%d", uint16( vheight ) );
2612 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
2614 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
2616 tk->f_fps = float( vfps );
2617 msg_Dbg( p_demux, " | | | + fps=%f", float( vfps ) );
2619 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
2621 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
2623 // msg_Dbg( p_demux, "| | | | + Track Video Display Unit=%s",
2624 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
2626 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
2628 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
2630 // msg_Dbg( p_demux, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
2632 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
2634 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
2636 // msg_Dbg( p_demux, " | | | + fps=%f", float( gamma ) );
2640 msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*l).name() );
2643 if ( tk->fmt.video.i_visible_height && tk->fmt.video.i_visible_width )
2644 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * tk->fmt.video.i_visible_width / tk->fmt.video.i_visible_height;
2646 else if( MKV_IS_ID( l, KaxTrackAudio ) )
2648 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
2651 msg_Dbg( p_demux, "| | | + Track Audio" );
2653 for( j = 0; j < tka->ListSize(); j++ )
2655 EbmlElement *l = (*tka)[j];
2657 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
2659 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
2661 tk->fmt.audio.i_rate = (int)float( afreq );
2662 msg_Dbg( p_demux, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
2664 else if( MKV_IS_ID( l, KaxAudioChannels ) )
2666 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
2668 tk->fmt.audio.i_channels = uint8( achan );
2669 msg_Dbg( p_demux, "| | | | + achan=%u", uint8( achan ) );
2671 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
2673 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
2675 tk->fmt.audio.i_bitspersample = uint8( abits );
2676 msg_Dbg( p_demux, "| | | | + abits=%u", uint8( abits ) );
2680 msg_Dbg( p_demux, "| | | | + Unknown (%s)", typeid(*l).name() );
2686 msg_Dbg( p_demux, "| | | + Unknown (%s)",
2687 typeid(*l).name() );
2692 static void ParseTracks( demux_t *p_demux, EbmlElement *tracks )
2694 demux_sys_t *p_sys = p_demux->p_sys;
2695 matroska_stream_t *p_stream = p_sys->Stream();
2699 int i_upper_level = 0;
2701 msg_Dbg( p_demux, "| + Tracks" );
2703 /* Master elements */
2704 m = static_cast<EbmlMaster *>(tracks);
2705 m->Read( *p_stream->es, tracks->Generic().Context, i_upper_level, el, true );
2707 for( i = 0; i < m->ListSize(); i++ )
2709 EbmlElement *l = (*m)[i];
2711 if( MKV_IS_ID( l, KaxTrackEntry ) )
2713 ParseTrackEntry( p_demux, static_cast<EbmlMaster *>(l) );
2717 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2722 /*****************************************************************************
2724 *****************************************************************************/
2725 static void ParseInfo( demux_t *p_demux, EbmlElement *info )
2727 demux_sys_t *p_sys = p_demux->p_sys;
2728 matroska_stream_t *p_stream = p_sys->Stream();
2729 matroska_segment_t *p_segment = p_stream->Segment();
2733 int i_upper_level = 0;
2735 msg_Dbg( p_demux, "| + Information" );
2737 /* Master elements */
2738 m = static_cast<EbmlMaster *>(info);
2739 m->Read( *p_stream->es, info->Generic().Context, i_upper_level, el, true );
2741 for( i = 0; i < m->ListSize(); i++ )
2743 EbmlElement *l = (*m)[i];
2745 if( MKV_IS_ID( l, KaxSegmentUID ) )
2747 p_segment->segment_uid = *(new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l)));
2749 msg_Dbg( p_demux, "| | + UID=%d", *(uint32*)p_segment->segment_uid.GetBuffer() );
2751 else if( MKV_IS_ID( l, KaxPrevUID ) )
2753 p_segment->prev_segment_uid = *(new KaxPrevUID(*static_cast<KaxPrevUID*>(l)));
2755 msg_Dbg( p_demux, "| | + PrevUID=%d", *(uint32*)p_segment->prev_segment_uid.GetBuffer() );
2757 else if( MKV_IS_ID( l, KaxNextUID ) )
2759 p_segment->next_segment_uid = *(new KaxNextUID(*static_cast<KaxNextUID*>(l)));
2761 msg_Dbg( p_demux, "| | + NextUID=%d", *(uint32*)p_segment->next_segment_uid.GetBuffer() );
2763 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
2765 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
2767 p_segment->i_timescale = uint64(tcs);
2769 msg_Dbg( p_demux, "| | + TimecodeScale="I64Fd,
2770 p_segment->i_timescale );
2772 else if( MKV_IS_ID( l, KaxDuration ) )
2774 KaxDuration &dur = *(KaxDuration*)l;
2776 p_segment->f_duration = float(dur);
2778 msg_Dbg( p_demux, "| | + Duration=%f",
2779 p_segment->f_duration );
2781 else if( MKV_IS_ID( l, KaxMuxingApp ) )
2783 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
2785 p_segment->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
2787 msg_Dbg( p_demux, "| | + Muxing Application=%s",
2788 p_segment->psz_muxing_application );
2790 else if( MKV_IS_ID( l, KaxWritingApp ) )
2792 KaxWritingApp &wapp = *(KaxWritingApp*)l;
2794 p_segment->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
2796 msg_Dbg( p_demux, "| | + Writing Application=%s",
2797 p_segment->psz_writing_application );
2799 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
2801 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
2803 p_segment->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
2805 msg_Dbg( p_demux, "| | + Segment Filename=%s",
2806 p_segment->psz_segment_filename );
2808 else if( MKV_IS_ID( l, KaxTitle ) )
2810 KaxTitle &title = *(KaxTitle*)l;
2812 p_segment->psz_title = UTF8ToStr( UTFstring( title ) );
2814 msg_Dbg( p_demux, "| | + Title=%s", p_segment->psz_title );
2816 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
2818 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
2820 p_segment->families.push_back(*uid);
2822 msg_Dbg( p_demux, "| | + family=%d", *(uint32*)uid->GetBuffer() );
2824 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
2825 else if( MKV_IS_ID( l, KaxDateUTC ) )
2827 KaxDateUTC &date = *(KaxDateUTC*)l;
2832 i_date = date.GetEpochDate();
2833 memset( buffer, 0, 256 );
2834 if( gmtime_r( &i_date, &tmres ) &&
2835 asctime_r( &tmres, buffer ) )
2837 buffer[strlen( buffer)-1]= '\0';
2838 p_segment->psz_date_utc = strdup( buffer );
2839 msg_Dbg( p_demux, "| | + Date=%s", p_segment->psz_date_utc );
2845 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
2849 p_segment->f_duration *= p_segment->i_timescale / 1000000.0;
2853 /*****************************************************************************
2855 *****************************************************************************/
2856 static void ParseChapterAtom( demux_t *p_demux, int i_level, EbmlMaster *ca, chapter_item_t & chapters )
2858 demux_sys_t *p_sys = p_demux->p_sys;
2861 if( p_sys->title == NULL )
2863 p_sys->title = vlc_input_title_New();
2866 msg_Dbg( p_demux, "| | | + ChapterAtom (level=%d)", i_level );
2867 for( i = 0; i < ca->ListSize(); i++ )
2869 EbmlElement *l = (*ca)[i];
2871 if( MKV_IS_ID( l, KaxChapterUID ) )
2873 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
2874 msg_Dbg( p_demux, "| | | | + ChapterUID: %lld", chapters.i_uid );
2876 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
2878 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
2879 chapters.b_display_seekpoint = uint8( flag ) == 0;
2881 msg_Dbg( p_demux, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
2883 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
2885 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
2886 chapters.i_start_time = uint64( start ) / I64C(1000);
2888 msg_Dbg( p_demux, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
2890 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
2892 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
2893 chapters.i_end_time = uint64( end ) / I64C(1000);
2895 msg_Dbg( p_demux, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
2897 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
2899 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
2902 msg_Dbg( p_demux, "| | | | + ChapterDisplay" );
2903 for( j = 0; j < cd->ListSize(); j++ )
2905 EbmlElement *l= (*cd)[j];
2907 if( MKV_IS_ID( l, KaxChapterString ) )
2911 KaxChapterString &name =*(KaxChapterString*)l;
2912 for (k = 0; k < i_level; k++)
2913 chapters.psz_name += '+';
2914 chapters.psz_name += ' ';
2915 chapters.psz_name += UTF8ToStr( UTFstring( name ) );
2917 msg_Dbg( p_demux, "| | | | | + ChapterString '%s'", UTF8ToStr(UTFstring(name)) );
2919 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
2921 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
2922 const char *psz = string( lang ).c_str();
2924 msg_Dbg( p_demux, "| | | | | + ChapterLanguage '%s'", psz );
2926 else if( MKV_IS_ID( l, KaxChapterCountry ) )
2928 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
2929 const char *psz = string( ct ).c_str();
2931 msg_Dbg( p_demux, "| | | | | + ChapterCountry '%s'", psz );
2935 else if( MKV_IS_ID( l, KaxChapterAtom ) )
2937 chapter_item_t new_sub_chapter;
2938 ParseChapterAtom( p_demux, i_level+1, static_cast<EbmlMaster *>(l), new_sub_chapter );
2939 new_sub_chapter.psz_parent = &chapters;
2940 chapters.sub_chapters.push_back( new_sub_chapter );
2945 /*****************************************************************************
2947 *****************************************************************************/
2948 static void ParseChapters( demux_t *p_demux, EbmlElement *chapters )
2950 demux_sys_t *p_sys = p_demux->p_sys;
2951 matroska_stream_t *p_stream = p_sys->Stream();
2952 matroska_segment_t *p_segment = p_stream->Segment();
2956 int i_upper_level = 0;
2957 int i_default_edition = 0;
2960 /* Master elements */
2961 m = static_cast<EbmlMaster *>(chapters);
2962 m->Read( *p_stream->es, chapters->Generic().Context, i_upper_level, el, true );
2964 for( i = 0; i < m->ListSize(); i++ )
2966 EbmlElement *l = (*m)[i];
2968 if( MKV_IS_ID( l, KaxEditionEntry ) )
2970 chapter_edition_t edition;
2972 EbmlMaster *E = static_cast<EbmlMaster *>(l );
2974 msg_Dbg( p_demux, "| | + EditionEntry" );
2975 for( j = 0; j < E->ListSize(); j++ )
2977 EbmlElement *l = (*E)[j];
2979 if( MKV_IS_ID( l, KaxChapterAtom ) )
2981 chapter_item_t new_sub_chapter;
2982 ParseChapterAtom( p_demux, 0, static_cast<EbmlMaster *>(l), new_sub_chapter );
2983 edition.chapters.push_back( new_sub_chapter );
2985 else if( MKV_IS_ID( l, KaxEditionUID ) )
2987 edition.i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
2989 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
2991 edition.b_ordered = uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0;
2993 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
2995 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
2996 i_default_edition = p_segment->editions.size();
3000 msg_Dbg( p_demux, "| | | + Unknown (%s)", typeid(*l).name() );
3003 p_segment->editions.push_back( edition );
3007 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid(*l).name() );
3011 for( i = 0; i < p_segment->editions.size(); i++ )
3013 p_segment->editions[i].RefreshChapters( *p_sys->title );
3016 p_segment->i_current_edition = i_default_edition;
3018 if ( p_segment->editions[i_default_edition].b_ordered )
3020 /* update the duration of the segment according to the sum of all sub chapters */
3021 f_duration = p_segment->editions[i_default_edition].Duration() / I64C(1000);
3022 if (f_duration > 0.0)
3023 p_segment->f_duration = f_duration;
3027 /*****************************************************************************
3028 * InformationCreate:
3029 *****************************************************************************/
3030 static void InformationCreate( demux_t *p_demux )
3032 demux_sys_t *p_sys = p_demux->p_sys;
3033 matroska_stream_t *p_stream = p_sys->Stream();
3034 matroska_segment_t *p_segment = p_stream->Segment();
3037 p_sys->meta = vlc_meta_New();
3039 if( p_segment->psz_title )
3041 vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_segment->psz_title );
3043 if( p_segment->psz_date_utc )
3045 vlc_meta_Add( p_sys->meta, VLC_META_DATE, p_segment->psz_date_utc );
3047 if( p_segment->psz_segment_filename )
3049 vlc_meta_Add( p_sys->meta, _("Segment filename"), p_segment->psz_segment_filename );
3051 if( p_segment->psz_muxing_application )
3053 vlc_meta_Add( p_sys->meta, _("Muxing application"), p_segment->psz_muxing_application );
3055 if( p_segment->psz_writing_application )
3057 vlc_meta_Add( p_sys->meta, _("Writing application"), p_segment->psz_writing_application );
3060 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
3062 mkv_track_t *tk = p_segment->tracks[i_track];
3063 vlc_meta_t *mtk = vlc_meta_New();
3065 p_sys->meta->track = (vlc_meta_t**)realloc( p_sys->meta->track,
3066 sizeof( vlc_meta_t * ) * ( p_sys->meta->i_track + 1 ) );
3067 p_sys->meta->track[p_sys->meta->i_track++] = mtk;
3069 if( tk->fmt.psz_description )
3071 vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
3073 if( tk->psz_codec_name )
3075 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
3077 if( tk->psz_codec_settings )
3079 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, tk->psz_codec_settings );
3081 if( tk->psz_codec_info_url )
3083 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
3085 if( tk->psz_codec_download_url )
3087 vlc_meta_Add( p_sys->meta, VLC_META_URL, tk->psz_codec_download_url );
3091 if( p_segment->i_tags_position >= 0 )
3093 vlc_bool_t b_seekable;
3095 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
3098 LoadTags( p_demux );
3104 /*****************************************************************************
3106 *****************************************************************************/
3108 static void IndexAppendCluster( demux_t *p_demux, KaxCluster *cluster )
3110 demux_sys_t *p_sys = p_demux->p_sys;
3111 matroska_stream_t *p_stream = p_sys->Stream();
3112 matroska_segment_t *p_segment = p_stream->Segment();
3114 #define idx p_segment->index[p_segment->i_index]
3116 idx.i_block_number= -1;
3117 idx.i_position = cluster->GetElementPosition();
3119 idx.b_key = VLC_TRUE;
3121 p_segment->i_index++;
3122 if( p_segment->i_index >= p_segment->i_index_max )
3124 p_segment->i_index_max += 1024;
3125 p_segment->index = (mkv_index_t*)realloc( p_segment->index, sizeof( mkv_index_t ) * p_segment->i_index_max );
3130 static char * UTF8ToStr( const UTFstring &u )
3139 p = dst = (char*)malloc( i_src + 1);
3158 void chapter_edition_t::RefreshChapters( input_title_t & title )
3160 int64_t i_prev_user_time = 0;
3161 std::vector<chapter_item_t>::iterator index = chapters.begin();
3163 while ( index != chapters.end() )
3165 i_prev_user_time = (*index).RefreshChapters( b_ordered, i_prev_user_time, title );
3170 int64_t chapter_item_t::RefreshChapters( bool b_ordered, int64_t i_prev_user_time, input_title_t & title )
3172 int64_t i_user_time = i_prev_user_time;
3174 // first the sub-chapters, and then ourself
3175 std::vector<chapter_item_t>::iterator index = sub_chapters.begin();
3176 while ( index != sub_chapters.end() )
3178 i_user_time = (*index).RefreshChapters( b_ordered, i_user_time, title );
3184 i_user_start_time = i_prev_user_time;
3185 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
3187 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
3191 i_user_end_time = i_user_time;
3196 std::sort( sub_chapters.begin(), sub_chapters.end() );
3197 i_user_start_time = i_start_time;
3198 i_user_end_time = i_end_time;
3201 if (b_display_seekpoint)
3203 seekpoint_t *sk = vlc_seekpoint_New();
3205 // sk->i_level = i_level;
3206 sk->i_time_offset = i_start_time;
3207 sk->psz_name = strdup( psz_name.c_str() );
3209 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
3210 title.i_seekpoint++;
3211 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
3212 title.seekpoint[title.i_seekpoint-1] = sk;
3215 i_seekpoint_num = title.i_seekpoint;
3217 return i_user_end_time;
3220 double chapter_edition_t::Duration() const
3222 double f_result = 0.0;
3224 if ( chapters.size() )
3226 std::vector<chapter_item_t>::const_iterator index = chapters.end();
3228 f_result = (*index).i_user_end_time;
3234 const chapter_item_t *chapter_item_t::FindTimecode( mtime_t i_user_timecode ) const
3236 const chapter_item_t *psz_result = NULL;
3238 if (i_user_timecode >= i_user_start_time && i_user_timecode < i_user_end_time)
3240 std::vector<chapter_item_t>::const_iterator index = sub_chapters.begin();
3241 while ( index != sub_chapters.end() && psz_result == NULL )
3243 psz_result = (*index).FindTimecode( i_user_timecode );
3247 if ( psz_result == NULL )
3254 const chapter_item_t *chapter_edition_t::FindTimecode( mtime_t i_user_timecode ) const
3256 const chapter_item_t *psz_result = NULL;
3258 std::vector<chapter_item_t>::const_iterator index = chapters.begin();
3259 while ( index != chapters.end() && psz_result == NULL )
3261 psz_result = (*index).FindTimecode( i_user_timecode );
3268 void demux_sys_t::PreloadFamily( demux_t *p_demux )
3270 matroska_stream_t *p_stream = Stream();
3273 matroska_segment_t *p_segment = p_stream->Segment();
3276 for (size_t i=0; i<streams.size(); i++)
3278 streams[i]->PreloadFamily( p_demux, *p_segment );
3284 void matroska_stream_t::PreloadFamily( demux_t *p_demux, const matroska_segment_t & of_segment )
3286 for (size_t i=0; i<segments.size(); i++)
3288 segments[i]->PreloadFamily( p_demux, of_segment );
3292 bool matroska_segment_t::PreloadFamily( demux_t *p_demux, const matroska_segment_t & of_segment )
3297 for (size_t i=0; i<families.size(); i++)
3299 for (size_t j=0; j<of_segment.families.size(); j++)
3301 if ( families[i] == of_segment.families[j] )
3302 return Preload( p_demux );
3309 // preload all the linked segments for all preloaded segments
3310 void demux_sys_t::PreloadLinked( demux_t *p_demux )
3312 size_t i_prealoaded;
3315 for (size_t i=0; i<streams.size(); i++)
3317 i_prealoaded += streams[i]->PreloadLinked( p_demux, *this );
3319 } while ( i_prealoaded ); // worst case: will stop when all segments are preloaded
3322 size_t matroska_stream_t::PreloadLinked( demux_t *p_demux, const demux_sys_t & of_sys )
3324 size_t i_result = 0;
3325 for (size_t i=0; i<segments.size(); i++)
3327 i_result += segments[i]->PreloadLinked( p_demux, of_sys );
3332 size_t matroska_segment_t::PreloadLinked( demux_t *p_demux, const demux_sys_t & of_sys )
3334 size_t i_result = 0;
3335 if ( prev_segment_uid.GetBuffer() )
3337 matroska_segment_t *p_segment = of_sys.FindSegment( prev_segment_uid );
3340 i_result += p_segment->Preload( p_demux ) ? 1 : 0;
3343 if ( next_segment_uid.GetBuffer() )
3345 matroska_segment_t *p_segment = of_sys.FindSegment( next_segment_uid );
3348 i_result += p_segment->Preload( p_demux ) ? 1 : 0;
3354 bool matroska_segment_t::Preload( demux_t *p_demux )
3359 EbmlElement *el = NULL;
3361 while( ( el = ep->Get() ) != NULL )
3363 if( MKV_IS_ID( el, KaxInfo ) )
3365 ParseInfo( p_demux, el );
3367 else if( MKV_IS_ID( el, KaxTracks ) )
3369 ParseTracks( p_demux, el );
3371 else if( MKV_IS_ID( el, KaxSeekHead ) )
3373 ParseSeekHead( p_demux, el );
3375 else if( MKV_IS_ID( el, KaxCues ) )
3377 msg_Dbg( p_demux, "| + Cues" );
3379 else if( MKV_IS_ID( el, KaxCluster ) )
3381 msg_Dbg( p_demux, "| + Cluster" );
3383 cluster = (KaxCluster*)el;
3386 /* stop parsing the stream */
3389 else if( MKV_IS_ID( el, KaxAttachments ) )
3391 msg_Dbg( p_demux, "| + Attachments FIXME TODO (but probably never supported)" );
3393 else if( MKV_IS_ID( el, KaxChapters ) )
3395 msg_Dbg( p_demux, "| + Chapters" );
3396 ParseChapters( p_demux, el );
3398 else if( MKV_IS_ID( el, KaxTag ) )
3400 msg_Dbg( p_demux, "| + Tags FIXME TODO" );
3404 msg_Dbg( p_demux, "| + Unknown (%s)", typeid(*el).name() );
3413 matroska_segment_t *demux_sys_t::FindSegment( EbmlBinary & uid ) const
3415 matroska_segment_t *p_segment = NULL;
3416 for (size_t i=0; i<streams.size() && p_segment == NULL; i++)
3418 p_segment = streams[i]->FindSegment( uid );
3423 matroska_segment_t *matroska_stream_t::FindSegment( EbmlBinary & uid ) const
3425 for (size_t i=0; i<segments.size(); i++)
3427 if ( segments[i]->segment_uid == uid )