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/EbmlVersion.h"
60 #include "ebml/StdIOCallback.h"
62 #include "matroska/KaxAttachments.h"
63 #include "matroska/KaxBlock.h"
64 #include "matroska/KaxBlockData.h"
65 #include "matroska/KaxChapters.h"
66 #include "matroska/KaxCluster.h"
67 #include "matroska/KaxClusterData.h"
68 #include "matroska/KaxContexts.h"
69 #include "matroska/KaxCues.h"
70 #include "matroska/KaxCuesData.h"
71 #include "matroska/KaxInfo.h"
72 #include "matroska/KaxInfoData.h"
73 #include "matroska/KaxSeekHead.h"
74 #include "matroska/KaxSegment.h"
75 #include "matroska/KaxTag.h"
76 #include "matroska/KaxTags.h"
77 #include "matroska/KaxTagMulti.h"
78 #include "matroska/KaxTracks.h"
79 #include "matroska/KaxTrackAudio.h"
80 #include "matroska/KaxTrackVideo.h"
81 #include "matroska/KaxTrackEntryData.h"
82 #include "matroska/KaxContentEncoding.h"
83 #include "matroska/KaxVersion.h"
85 #include "ebml/StdIOCallback.h"
88 #include "mp4/libmp4.h"
94 #define MATROSKA_COMPRESSION_NONE 0
95 #define MATROSKA_COMPRESSION_ZLIB 1
97 #define MKVD_TIMECODESCALE 1000000
100 * What's between a directory and a filename?
103 #define DIRECTORY_SEPARATOR '\\'
105 #define DIRECTORY_SEPARATOR '/'
108 using namespace LIBMATROSKA_NAMESPACE;
111 /*****************************************************************************
113 *****************************************************************************/
114 static int Open ( vlc_object_t * );
115 static void Close( vlc_object_t * );
118 set_shortname( _("Matroska") );
119 set_description( _("Matroska stream demuxer" ) );
120 set_capability( "demux2", 50 );
121 set_callbacks( Open, Close );
122 set_category( CAT_INPUT );
123 set_subcategory( SUBCAT_INPUT_DEMUX );
125 add_bool( "mkv-use-ordered-chapters", 1, NULL,
126 N_("Ordered chapters"),
127 N_("Play ordered chapters as specified in the segment"), VLC_TRUE );
129 add_bool( "mkv-use-chapter-codec", 1, NULL,
130 N_("Chapter codecs"),
131 N_("Use chapter codecs found in the segment"), VLC_TRUE );
133 add_bool( "mkv-seek-percent", 0, NULL,
134 N_("Seek based on percent not time"),
135 N_("Seek based on percent not time"), VLC_TRUE );
137 add_bool( "mkv-use-dummy", 0, NULL,
138 N_("Dummy Elements"),
139 N_("Read and discard unknown EBML elements (not good for broken files)"), VLC_TRUE );
141 add_shortcut( "mka" );
142 add_shortcut( "mkv" );
145 /*****************************************************************************
147 *****************************************************************************/
149 block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) {
150 int result, dstsize, n;
155 d_stream.zalloc = (alloc_func)0;
156 d_stream.zfree = (free_func)0;
157 d_stream.opaque = (voidpf)0;
158 result = inflateInit(&d_stream);
161 msg_Dbg( p_this, "inflateInit() failed. Result: %d", result );
165 d_stream.next_in = (Bytef *)p_in_block->p_buffer;
166 d_stream.avail_in = p_in_block->i_buffer;
168 p_block = block_New( p_this, 0 );
173 p_block = block_Realloc( p_block, 0, n * 1000 );
174 dst = (unsigned char *)p_block->p_buffer;
175 d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
176 d_stream.avail_out = 1000;
177 result = inflate(&d_stream, Z_NO_FLUSH);
178 if( ( result != Z_OK ) && ( result != Z_STREAM_END ) )
180 msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result );
184 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
185 ( result != Z_STREAM_END ) );
187 dstsize = d_stream.total_out;
188 inflateEnd( &d_stream );
190 p_block = block_Realloc( p_block, 0, dstsize );
191 p_block->i_buffer = dstsize;
192 block_Release( p_in_block );
199 * Helper function to print the mkv parse tree
201 static void MkvTree( demux_t & demuxer, int i_level, char *psz_format, ... )
206 msg_Err( &demuxer, "too deep tree" );
209 va_start( args, psz_format );
210 static char *psz_foo = "| | | | | | | | | |";
211 char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) );
212 strncpy( psz_foo2, psz_foo, 4 * i_level );
213 psz_foo2[ 4 * i_level ] = '+';
214 psz_foo2[ 4 * i_level + 1 ] = ' ';
215 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
216 __msg_GenericVa( VLC_OBJECT(&demuxer), VLC_MSG_DBG, "mkv", psz_foo2, args );
221 /*****************************************************************************
223 *****************************************************************************/
224 class vlc_stream_io_callback: public IOCallback
231 vlc_stream_io_callback( stream_t * );
233 virtual uint32 read ( void *p_buffer, size_t i_size);
234 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
235 virtual size_t write ( const void *p_buffer, size_t i_size);
236 virtual uint64 getFilePointer ( void );
237 virtual void close ( void );
240 /*****************************************************************************
242 *****************************************************************************/
246 EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux );
247 virtual ~EbmlParser( void );
251 void Reset( demux_t *p_demux );
252 EbmlElement *Get( void );
255 int GetLevel( void );
260 EbmlElement *m_el[10];
261 int64_t mi_remain_size[10];
271 /*****************************************************************************
272 * Some functions to manipulate memory
273 *****************************************************************************/
274 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
275 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
277 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
280 /*****************************************************************************
281 * definitions of structures and functions used by this plugins
282 *****************************************************************************/
285 vlc_bool_t b_default;
286 vlc_bool_t b_enabled;
287 unsigned int i_number;
290 uint8_t *p_extra_data;
294 uint64_t i_default_duration;
295 float f_timecodescale;
303 /* data to be send first */
305 uint8_t *p_data_init;
307 /* hack : it's for seek */
308 vlc_bool_t b_search_keyframe;
312 char *psz_codec_name;
313 char *psz_codec_settings;
314 char *psz_codec_info_url;
315 char *psz_codec_download_url;
317 /* encryption/compression */
318 int i_compression_type;
335 class chapter_codec_cmds_c
338 chapter_codec_cmds_c( int codec_id = -1)
339 :i_codec_id( codec_id )
342 virtual ~chapter_codec_cmds_c() {}
344 void SetPrivate( const KaxChapterProcessPrivate & private_data )
346 m_private_data = *( new KaxChapterProcessPrivate( private_data ) );
349 void AddCommand( const KaxChapterProcessCommand & command );
351 virtual bool Enter() { return true; }
352 virtual bool Leave() { return true; }
354 KaxChapterProcessPrivate m_private_data;
357 std::vector<KaxChapterProcessData> enter_cmds;
358 std::vector<KaxChapterProcessData> during_cmds;
359 std::vector<KaxChapterProcessData> leave_cmds;
364 class dvd_command_interpretor_c
367 dvd_command_interpretor_c( demux_sys_t & demuxer )
370 memset( p_GPRM, 0, sizeof(p_GPRM) );
371 memset( p_SPRM, 0, sizeof(p_SPRM) );
378 p_SPRM[ 16 ] = 0xFFFFu;
379 p_SPRM[ 18 ] = 0xFFFFu;
382 bool Interpret( const binary * p_command, size_t i_size = 8 );
385 uint16 GetGPRM( size_t index ) const
387 if ( index >= 0 && index < 16 )
388 return p_GPRM[ index ];
392 uint16 GetSPRM( size_t index ) const
394 // 21,22,23 reserved for future use
395 if ( index >= 0 && index < 21 )
396 return p_SPRM[ index ];
400 bool SetGPRM( size_t index, uint16 value )
402 if ( index >= 0 && index < 16 )
404 p_GPRM[ index ] = value;
410 bool SetSPRM( size_t index, uint16 value )
412 if ( index > 0 && index <= 13 && index != 12 )
414 p_SPRM[ index ] = value;
425 static const uint16 CMD_JUMP_TT = 0x3002;
427 // callbacks when browsing inside CodecPrivate
428 static bool MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
431 class dvd_chapter_codec_c : public chapter_codec_cmds_c
434 dvd_chapter_codec_c( demux_sys_t & sys )
435 :chapter_codec_cmds_c( 1 )
443 dvd_command_interpretor_c interpretor;
446 class matroska_script_codec_c : public chapter_codec_cmds_c
450 class chapter_translation_c
453 KaxChapterTranslateID translated;
454 unsigned int codec_id;
455 std::vector<uint64_t> editions;
464 ,i_user_start_time(-1)
467 ,b_display_seekpoint(true)
473 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
474 while ( index != codecs.end() )
479 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
480 while ( index_ != sub_chapters.end() )
487 int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time );
488 void PublishChapters( input_title_t & title, int i_level );
489 chapter_item_c * FindTimecode( mtime_t i_timecode );
490 void Append( const chapter_item_c & edition );
491 chapter_item_c * FindChapter( const chapter_item_c & chapter );
492 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
493 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
494 const void *p_cookie,
495 size_t i_cookie_size );
497 int64_t i_start_time, i_end_time;
498 int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */
499 std::vector<chapter_item_c*> sub_chapters;
502 bool b_display_seekpoint;
503 std::string psz_name;
504 chapter_item_c *psz_parent;
506 std::vector<chapter_codec_cmds_c*> codecs;
508 bool operator<( const chapter_item_c & item ) const
510 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) );
517 class chapter_edition_c : public chapter_item_c
524 void RefreshChapters( );
525 mtime_t Duration() const;
526 void PublishChapters( input_title_t & title );
531 class matroska_segment_c
534 matroska_segment_c( demux_sys_t & demuxer, EbmlStream & estream )
537 ,i_timescale(MKVD_TIMECODESCALE)
541 ,i_chapters_position(-1)
548 ,psz_muxing_application(NULL)
549 ,psz_writing_application(NULL)
550 ,psz_segment_filename(NULL)
553 ,i_default_edition(0)
558 index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
561 virtual ~matroska_segment_c()
563 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
565 #define tk tracks[i_track]
566 if( tk->fmt.psz_description )
568 free( tk->fmt.psz_description );
572 free( tk->psz_codec );
574 if( tk->fmt.psz_language )
576 free( tk->fmt.psz_language );
582 if( psz_writing_application )
584 free( psz_writing_application );
586 if( psz_muxing_application )
588 free( psz_muxing_application );
590 if( psz_segment_filename )
592 free( psz_segment_filename );
600 free( psz_date_utc );
607 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
608 while ( index != stored_editions.end() )
619 uint64_t i_timescale;
621 /* duration of the segment */
623 mtime_t i_start_time;
626 std::vector<mkv_track_t*> tracks;
629 int64_t i_cues_position;
630 int64_t i_chapters_position;
631 int64_t i_tags_position;
635 KaxSegmentUID segment_uid;
636 KaxPrevUID prev_segment_uid;
637 KaxNextUID next_segment_uid;
645 char *psz_muxing_application;
646 char *psz_writing_application;
647 char *psz_segment_filename;
651 std::vector<chapter_edition_c*> stored_editions;
652 int i_default_edition;
654 std::vector<chapter_translation_c> translations;
655 std::vector<KaxSegmentFamily> families;
662 bool PreloadFamily( const matroska_segment_c & segment );
663 void ParseInfo( KaxInfo *info );
664 void ParseChapters( KaxChapters *chapters );
665 void ParseSeekHead( KaxSeekHead *seekhead );
666 void ParseTracks( KaxTracks *tracks );
667 void ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters );
668 void ParseTrackEntry( KaxTrackEntry *m );
669 void ParseCluster( );
670 void IndexAppendCluster( KaxCluster *cluster );
673 void InformationCreate( );
674 void Seek( mtime_t i_date, mtime_t i_time_offset );
675 int BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration );
676 bool Select( mtime_t i_start_time );
678 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
679 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
680 const void *p_cookie,
681 size_t i_cookie_size );
683 static bool CompareSegmentUIDs( const matroska_segment_c * item_a, const matroska_segment_c * item_b );
686 // class holding hard-linked segment together in the playback order
687 class virtual_segment_c
690 virtual_segment_c( matroska_segment_c *p_segment )
691 :i_current_segment(0)
693 ,i_current_edition(-1)
694 ,psz_current_chapter(NULL)
696 linked_segments.push_back( p_segment );
698 AppendUID( p_segment->segment_uid );
699 AppendUID( p_segment->prev_segment_uid );
700 AppendUID( p_segment->next_segment_uid );
704 size_t AddSegment( matroska_segment_c *p_segment );
705 void PreloadLinked( );
706 mtime_t Duration( ) const;
708 void Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter );
710 inline chapter_edition_c *Edition()
712 if ( i_current_edition >= 0 && size_t(i_current_edition) < p_editions->size() )
713 return (*p_editions)[i_current_edition];
717 inline bool EditionIsOrdered() const
719 return (p_editions->size() != 0 && i_current_edition >= 0 && (*p_editions)[i_current_edition]->b_ordered);
722 matroska_segment_c * Segment() const
724 if ( linked_segments.size() == 0 || i_current_segment >= linked_segments.size() )
726 return linked_segments[i_current_segment];
729 inline const chapter_item_c *CurrentChapter() const {
730 return psz_current_chapter;
735 if ( i_current_segment < linked_segments.size()-1 )
743 void UpdateCurrentToChapter( demux_t & demux );
744 bool Select( input_title_t & title );
747 std::vector<matroska_segment_c*> linked_segments;
748 std::vector<KaxSegmentUID> linked_uids;
749 size_t i_current_segment;
751 std::vector<chapter_edition_c*> *p_editions;
752 int i_current_edition;
753 chapter_item_c *psz_current_chapter;
755 void AppendUID( const EbmlBinary & UID );
758 class matroska_stream_c
761 matroska_stream_c( demux_sys_t & demuxer )
767 virtual ~matroska_stream_c()
776 std::vector<matroska_segment_c*> segments;
784 demux_sys_t( demux_t & demux )
791 ,p_current_segment(NULL)
795 virtual ~demux_sys_t()
797 for (size_t i=0; i<streams.size(); i++)
799 for ( size_t i=0; i<opened_segments.size(); i++ )
800 delete opened_segments[i];
808 mtime_t i_chapter_time;
812 input_title_t *title;
814 std::vector<matroska_stream_c*> streams;
815 std::vector<matroska_segment_c*> opened_segments;
816 virtual_segment_c *p_current_segment;
818 /* duration of the stream */
821 matroska_segment_c *FindSegment( const EbmlBinary & uid ) const;
822 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
823 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
824 const void *p_cookie,
825 size_t i_cookie_size,
826 matroska_segment_c * & p_segment_found );
828 void PreloadFamily( const matroska_segment_c & of_segment );
829 void PreloadLinked( matroska_segment_c *p_segment );
830 bool PreparePlayback( );
831 matroska_stream_c *AnalyseAllSegmentsFound( EbmlStream *p_estream );
834 static int Demux ( demux_t * );
835 static int Control( demux_t *, int, va_list );
836 static void Seek ( demux_t *, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter );
838 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
840 static char *UTF8ToStr ( const UTFstring &u );
842 /*****************************************************************************
843 * Open: initializes matroska demux structures
844 *****************************************************************************/
845 static int Open( vlc_object_t * p_this )
847 demux_t *p_demux = (demux_t*)p_this;
849 matroska_stream_c *p_stream;
850 matroska_segment_c *p_segment;
852 std::string s_path, s_filename;
853 vlc_stream_io_callback *p_io_callback;
854 EbmlStream *p_io_stream;
856 /* peek the begining */
857 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
859 /* is a valid file */
860 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
861 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
863 /* Set the demux function */
864 p_demux->pf_demux = Demux;
865 p_demux->pf_control = Control;
866 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
868 p_io_callback = new vlc_stream_io_callback( p_demux->s );
869 p_io_stream = new EbmlStream( *p_io_callback );
871 if( p_io_stream == NULL )
873 msg_Err( p_demux, "failed to create EbmlStream" );
874 delete p_io_callback;
879 p_stream = p_sys->AnalyseAllSegmentsFound( p_io_stream );
880 if( p_stream == NULL )
882 msg_Err( p_demux, "cannot find KaxSegment" );
885 p_sys->streams.push_back( p_stream );
887 p_stream->p_in = p_io_callback;
888 p_stream->p_es = p_io_stream;
890 for (size_t i=0; i<p_stream->segments.size(); i++)
892 p_stream->segments[i]->Preload();
895 p_segment = p_stream->segments[0];
896 if( p_segment->cluster != NULL )
898 msg_Warn( p_demux, "cannot find any cluster, damaged file ?" );
900 // reset the stream reading to the first cluster of the segment used
901 p_stream->p_in->setFilePointer( p_segment->cluster->GetElementPosition() );
904 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
905 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
907 // assume it's a regular file
908 // get the directory path
909 s_path = p_demux->psz_path;
910 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
912 s_path = s_path.substr(0,s_path.length()-1);
916 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
918 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
922 struct dirent *p_file_item;
923 DIR *p_src_dir = opendir(s_path.c_str());
925 if (p_src_dir != NULL)
927 while ((p_file_item = (dirent *) readdir(p_src_dir)))
929 if (strlen(p_file_item->d_name) > 4)
931 s_filename = s_path + DIRECTORY_SEPARATOR + p_file_item->d_name;
933 if (!s_filename.compare(p_demux->psz_path))
934 continue; // don't reuse the original opened file
936 #if defined(__GNUC__) && (__GNUC__ < 3)
937 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
938 !s_filename.compare("mka", s_filename.length() - 3, 3))
940 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
941 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
944 // test wether this file belongs to the our family
945 StdIOCallback *p_file_io = new StdIOCallback(s_filename.c_str(), MODE_READ);
946 EbmlStream *p_estream = new EbmlStream(*p_file_io);
948 p_stream = p_sys->AnalyseAllSegmentsFound( p_estream );
949 if ( p_stream == NULL )
951 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
957 p_stream->p_in = p_file_io;
958 p_stream->p_es = p_estream;
959 p_sys->streams.push_back( p_stream );
964 closedir( p_src_dir );
968 p_sys->PreloadFamily( *p_segment );
969 p_sys->PreloadLinked( p_segment );
970 if ( !p_sys->PreparePlayback( ) )
972 msg_Err( p_demux, "cannot use the segment" );
983 /*****************************************************************************
984 * Close: frees unused data
985 *****************************************************************************/
986 static void Close( vlc_object_t *p_this )
988 demux_t *p_demux = (demux_t*)p_this;
989 demux_sys_t *p_sys = p_demux->p_sys;
994 /*****************************************************************************
996 *****************************************************************************/
997 static int Control( demux_t *p_demux, int i_query, va_list args )
999 demux_sys_t *p_sys = p_demux->p_sys;
1004 vlc_meta_t **pp_meta;
1008 case DEMUX_GET_META:
1009 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1010 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1013 case DEMUX_GET_LENGTH:
1014 pi64 = (int64_t*)va_arg( args, int64_t * );
1015 if( p_sys->f_duration > 0.0 )
1017 *pi64 = (int64_t)(p_sys->f_duration * 1000);
1020 return VLC_EGENERIC;
1022 case DEMUX_GET_POSITION:
1023 pf = (double*)va_arg( args, double * );
1024 if ( p_sys->f_duration > 0.0 )
1025 *pf = (double)(p_sys->i_pts >= p_sys->i_start_pts ? p_sys->i_pts : p_sys->i_start_pts ) / (1000.0 * p_sys->f_duration);
1028 case DEMUX_SET_POSITION:
1029 f = (double)va_arg( args, double );
1030 Seek( p_demux, -1, f, NULL );
1033 case DEMUX_GET_TIME:
1034 pi64 = (int64_t*)va_arg( args, int64_t * );
1035 *pi64 = p_sys->i_pts;
1038 case DEMUX_GET_TITLE_INFO:
1039 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
1041 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1042 int *pi_int = (int*)va_arg( args, int* );
1045 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) );
1047 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->title );
1051 return VLC_EGENERIC;
1053 case DEMUX_SET_TITLE:
1054 /* TODO handle editions as titles & DVD titles as well */
1055 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
1059 return VLC_EGENERIC;
1061 case DEMUX_SET_SEEKPOINT:
1062 i_skp = (int)va_arg( args, int );
1064 if( p_sys->title && i_skp < p_sys->title->i_seekpoint)
1066 Seek( p_demux, (int64_t)p_sys->title->seekpoint[i_skp]->i_time_offset, -1, NULL);
1067 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
1068 p_demux->info.i_seekpoint = i_skp;
1071 return VLC_EGENERIC;
1073 case DEMUX_SET_TIME:
1076 return VLC_EGENERIC;
1080 int matroska_segment_c::BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1092 i_level = ep->GetLevel();
1094 if( el == NULL && *pp_block != NULL )
1096 /* update the index */
1097 #define idx index[i_index - 1]
1098 if( i_index > 0 && idx.i_time == -1 )
1100 idx.i_time = (*pp_block)->GlobalTimecode() / (mtime_t)1000;
1101 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1109 if( ep->GetLevel() > 1 )
1114 msg_Warn( &sys.demuxer, "EOF" );
1115 return VLC_EGENERIC;
1121 if( MKV_IS_ID( el, KaxCluster ) )
1123 cluster = (KaxCluster*)el;
1125 /* add it to the index */
1127 ( i_index > 0 && index[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1129 IndexAppendCluster( cluster );
1132 // reset silent tracks
1133 for (size_t i=0; i<tracks.size(); i++)
1135 tracks[i]->b_silent = VLC_FALSE;
1140 else if( MKV_IS_ID( el, KaxCues ) )
1142 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1143 return VLC_EGENERIC;
1147 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1150 else if( i_level == 2 )
1152 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1154 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1156 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1157 cluster->InitTimecode( uint64( ctc ), i_timescale );
1159 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1163 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1168 else if( i_level == 3 )
1170 if( MKV_IS_ID( el, KaxBlock ) )
1172 *pp_block = (KaxBlock*)el;
1174 (*pp_block)->ReadData( es.I_O() );
1175 (*pp_block)->SetParent( *cluster );
1179 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1181 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1183 dur.ReadData( es.I_O() );
1184 *pi_duration = uint64( dur );
1186 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1188 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1190 ref.ReadData( es.I_O() );
1191 if( *pi_ref1 == -1 )
1193 *pi_ref1 = int64( ref );
1197 *pi_ref2 = int64( ref );
1200 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1202 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1203 track_num.ReadData( es.I_O() );
1205 for (size_t i=0; i<tracks.size(); i++)
1207 if ( tracks[i]->i_number == uint32(track_num))
1209 tracks[i]->b_silent = VLC_TRUE;
1217 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1218 return VLC_EGENERIC;
1223 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1226 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1227 memcpy( p_block->p_buffer, p_mem, i_mem );
1228 //p_block->i_rate = p_input->stream.control.i_rate;
1232 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1233 mtime_t i_duration )
1235 demux_sys_t *p_sys = p_demux->p_sys;
1236 matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
1242 #define tk p_segment->tracks[i_track]
1243 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1245 if( tk->i_number == block->TrackNum() )
1251 if( i_track >= p_segment->tracks.size() )
1253 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1256 if( tk->p_es == NULL )
1258 msg_Err( p_demux, "unknown track number=%d", block->TrackNum() );
1261 if( i_pts < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1263 return; /* discard audio packets that shouldn't be rendered */
1266 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1269 tk->b_inited = VLC_FALSE;
1273 /* First send init data */
1274 if( !tk->b_inited && tk->i_data_init > 0 )
1278 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
1279 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init );
1280 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
1282 tk->b_inited = VLC_TRUE;
1285 for( i = 0; i < block->NumberFrames(); i++ )
1288 DataBuffer &data = block->GetBuffer(i);
1290 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1292 if( p_block == NULL )
1297 #if defined(HAVE_ZLIB_H)
1298 if( tk->i_compression_type )
1300 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
1304 // TODO implement correct timestamping when B frames are used
1305 if( tk->fmt.i_cat != VIDEO_ES )
1307 p_block->i_dts = p_block->i_pts = i_pts;
1311 p_block->i_dts = i_pts;
1315 if( tk->fmt.i_cat == SPU_ES && strcmp( tk->psz_codec, "S_VOBSUB" ) )
1317 p_block->i_length = i_duration * 1000;
1320 es_out_Send( p_demux->out, tk->p_es, p_block );
1322 /* use time stamp only for first block */
1329 matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( EbmlStream *p_estream )
1331 int i_upper_lvl = 0;
1333 EbmlElement *p_l0, *p_l1, *p_l2;
1334 bool b_keep_stream = false, b_keep_segment;
1336 // verify the EBML Header
1337 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
1342 p_l0->SkipData(*p_estream, EbmlHead_Context);
1345 // find all segments in this file
1346 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1352 matroska_stream_c *p_stream1 = new matroska_stream_c( *this );
1356 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
1359 matroska_segment_c *p_segment1 = new matroska_segment_c( *this, *p_estream );
1360 b_keep_segment = false;
1362 ep = new EbmlParser(p_estream, p_l0, &demuxer );
1363 p_segment1->ep = ep;
1364 p_segment1->segment = (KaxSegment*)p_l0;
1366 while ((p_l1 = ep->Get()))
1368 if (MKV_IS_ID(p_l1, KaxInfo))
1370 // find the families of this segment
1371 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
1373 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
1374 for( i = 0; i < p_info->ListSize(); i++ )
1376 EbmlElement *l = (*p_info)[i];
1378 if( MKV_IS_ID( l, KaxSegmentUID ) )
1380 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
1381 b_keep_segment = (FindSegment( *p_uid ) == NULL);
1382 if ( !b_keep_segment )
1383 break; // this segment is already known
1384 opened_segments.push_back( p_segment1 );
1385 p_segment1->segment_uid = *( new KaxSegmentUID(*p_uid) );
1387 else if( MKV_IS_ID( l, KaxPrevUID ) )
1389 p_segment1->prev_segment_uid = *( new KaxPrevUID( *static_cast<KaxPrevUID*>(l) ) );
1391 else if( MKV_IS_ID( l, KaxNextUID ) )
1393 p_segment1->next_segment_uid = *( new KaxNextUID( *static_cast<KaxNextUID*>(l) ) );
1395 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
1397 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
1398 p_segment1->families.push_back( *p_fam );
1404 if ( b_keep_segment )
1406 b_keep_stream = true;
1407 p_stream1->segments.push_back( p_segment1 );
1413 p_l0->SkipData(*p_estream, EbmlHead_Context);
1414 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1417 if ( !b_keep_stream )
1426 bool matroska_segment_c::Select( mtime_t i_start_time )
1431 msg_Dbg( &sys.demuxer, "found %d es", tracks.size() );
1432 for( i_track = 0; i_track < tracks.size(); i_track++ )
1434 #define tk tracks[i_track]
1435 if( tk->fmt.i_cat == UNKNOWN_ES )
1437 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", i_track, tk->i_number );
1442 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
1444 if( tk->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
1446 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
1447 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1451 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk->p_extra_data;
1453 tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1454 tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1455 tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1457 tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
1458 if( tk->fmt.i_extra > 0 )
1460 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1461 memcpy( tk->fmt.p_extra, &p_bih[1], tk->fmt.i_extra );
1465 else if( !strcmp( tk->psz_codec, "V_MPEG1" ) ||
1466 !strcmp( tk->psz_codec, "V_MPEG2" ) )
1468 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1470 else if( !strncmp( tk->psz_codec, "V_MPEG4", 7 ) )
1472 if( !strcmp( tk->psz_codec, "V_MPEG4/MS/V3" ) )
1474 tk->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1476 else if( !strcmp( tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1478 tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1479 tk->fmt.b_packetized = VLC_FALSE;
1480 tk->fmt.i_extra = tk->i_extra_data;
1481 tk->fmt.p_extra = malloc( tk->i_extra_data );
1482 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
1486 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1489 else if( !strcmp( tk->psz_codec, "V_QUICKTIME" ) )
1491 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
1493 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1497 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1502 MP4_ReadBoxCommon( p_mp4_stream, p_box );
1503 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
1504 tk->fmt.i_codec = p_box->i_type;
1505 tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1506 tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1507 tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1508 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1509 memcpy( tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tk->fmt.i_extra );
1510 MP4_FreeBox_sample_vide( p_box );
1512 stream_MemoryDelete( p_mp4_stream, VLC_TRUE );
1514 stream_Delete( p_mp4_stream );
1517 else if( !strcmp( tk->psz_codec, "A_MS/ACM" ) )
1519 if( tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1521 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1522 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1526 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk->p_extra_data;
1528 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk->fmt.i_codec, NULL );
1530 tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1531 tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1532 tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1533 tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1534 tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1536 tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1537 if( tk->fmt.i_extra > 0 )
1539 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1540 memcpy( tk->fmt.p_extra, &p_wf[1], tk->fmt.i_extra );
1544 else if( !strcmp( tk->psz_codec, "A_MPEG/L3" ) ||
1545 !strcmp( tk->psz_codec, "A_MPEG/L2" ) ||
1546 !strcmp( tk->psz_codec, "A_MPEG/L1" ) )
1548 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1550 else if( !strcmp( tk->psz_codec, "A_AC3" ) )
1552 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1554 else if( !strcmp( tk->psz_codec, "A_DTS" ) )
1556 tk->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
1558 else if( !strcmp( tk->psz_codec, "A_FLAC" ) )
1560 tk->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
1561 tk->fmt.i_extra = tk->i_extra_data;
1562 tk->fmt.p_extra = malloc( tk->i_extra_data );
1563 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
1565 else if( !strcmp( tk->psz_codec, "A_VORBIS" ) )
1567 int i, i_offset = 1, i_size[3], i_extra;
1570 tk->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
1572 /* Split the 3 headers */
1573 if( tk->p_extra_data[0] != 0x02 )
1574 msg_Err( &sys.demuxer, "invalid vorbis header" );
1576 for( i = 0; i < 2; i++ )
1579 while( i_offset < tk->i_extra_data )
1581 i_size[i] += tk->p_extra_data[i_offset];
1582 if( tk->p_extra_data[i_offset++] != 0xff ) break;
1586 i_size[0] = __MIN(i_size[0], tk->i_extra_data - i_offset);
1587 i_size[1] = __MIN(i_size[1], tk->i_extra_data -i_offset -i_size[0]);
1588 i_size[2] = tk->i_extra_data - i_offset - i_size[0] - i_size[1];
1590 tk->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
1591 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1592 p_extra = (uint8_t *)tk->fmt.p_extra; i_extra = 0;
1593 for( i = 0; i < 3; i++ )
1595 *(p_extra++) = i_size[i] >> 8;
1596 *(p_extra++) = i_size[i] & 0xFF;
1597 memcpy( p_extra, tk->p_extra_data + i_offset + i_extra,
1599 p_extra += i_size[i];
1600 i_extra += i_size[i];
1603 else if( !strncmp( tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1604 !strncmp( tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1606 int i_profile, i_srate;
1607 static unsigned int i_sample_rates[] =
1609 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1610 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1613 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1614 /* create data for faad (MP4DecSpecificDescrTag)*/
1616 if( !strcmp( &tk->psz_codec[12], "MAIN" ) )
1620 else if( !strcmp( &tk->psz_codec[12], "LC" ) )
1624 else if( !strcmp( &tk->psz_codec[12], "SSR" ) )
1633 for( i_srate = 0; i_srate < 13; i_srate++ )
1635 if( i_sample_rates[i_srate] == tk->fmt.audio.i_rate )
1640 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1642 tk->fmt.i_extra = 2;
1643 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1644 ((uint8_t*)tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1645 ((uint8_t*)tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk->fmt.audio.i_channels << 3);
1647 else if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) ||
1648 !strcmp( tk->psz_codec, "A_PCM/INT/LIT" ) ||
1649 !strcmp( tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1651 if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) )
1653 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1657 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1659 tk->fmt.audio.i_blockalign = ( tk->fmt.audio.i_bitspersample + 7 ) / 8 * tk->fmt.audio.i_channels;
1661 else if( !strcmp( tk->psz_codec, "A_TTA1" ) )
1663 /* FIXME: support this codec */
1664 msg_Err( &sys.demuxer, "TTA not supported yet[%d, n=%d]", i_track, tk->i_number );
1665 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1667 else if( !strcmp( tk->psz_codec, "A_WAVPACK4" ) )
1669 /* FIXME: support this codec */
1670 msg_Err( &sys.demuxer, "Wavpack not supported yet[%d, n=%d]", i_track, tk->i_number );
1671 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1673 else if( !strcmp( tk->psz_codec, "S_TEXT/UTF8" ) )
1675 tk->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1676 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1678 else if( !strcmp( tk->psz_codec, "S_TEXT/SSA" ) ||
1679 !strcmp( tk->psz_codec, "S_TEXT/ASS" ) ||
1680 !strcmp( tk->psz_codec, "S_SSA" ) ||
1681 !strcmp( tk->psz_codec, "S_ASS" ))
1683 tk->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1684 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1686 else if( !strcmp( tk->psz_codec, "S_VOBSUB" ) )
1688 tk->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1689 if( tk->i_extra_data )
1692 char *p_buf = (char *)malloc( tk->i_extra_data + 1);
1693 memcpy( p_buf, tk->p_extra_data , tk->i_extra_data );
1694 p_buf[tk->i_extra_data] = '\0';
1696 p_start = strstr( p_buf, "size:" );
1697 if( sscanf( p_start, "size: %dx%d",
1698 &tk->fmt.subs.spu.i_original_frame_width, &tk->fmt.subs.spu.i_original_frame_height ) == 2 )
1700 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d", tk->fmt.subs.spu.i_original_frame_width, tk->fmt.subs.spu.i_original_frame_height );
1704 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1709 else if( !strcmp( tk->psz_codec, "B_VOBBTN" ) )
1711 /* FIXME: support this codec */
1712 msg_Err( &sys.demuxer, "Vob Buttons not supported yet[%d, n=%d]", i_track, tk->i_number );
1713 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1717 msg_Err( &sys.demuxer, "unknow codec id=`%s'", tk->psz_codec );
1718 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1722 tk->fmt.i_priority = 1000;
1725 tk->p_es = es_out_Add( sys.demuxer.out, &tk->fmt );
1727 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_start_time );
1731 sys.i_start_pts = i_start_time;
1732 // reset the stream reading to the first cluster of the segment used
1733 es.I_O().setFilePointer( i_start_pos );
1736 ep = new EbmlParser( &es, segment, &sys.demuxer );
1741 void matroska_segment_c::UnSelect( )
1745 for( i_track = 0; i_track < tracks.size(); i_track++ )
1747 #define tk tracks[i_track]
1748 if ( tk->p_es != NULL )
1750 es_out_Del( sys.demuxer.out, tk->p_es );
1759 bool virtual_segment_c::Select( input_title_t & title )
1761 if ( linked_segments.size() == 0 )
1764 // !!! should be called only once !!!
1765 matroska_segment_c *p_segment;
1768 // copy editions from the first segment
1769 p_segment = linked_segments[0];
1770 p_editions = &p_segment->stored_editions;
1772 for ( i=1 ; i<linked_segments.size(); i++ )
1774 p_segment = linked_segments[i];
1775 // FIXME assume we have the same editions in all segments
1776 for (j=0; j<p_segment->stored_editions.size(); j++)
1777 (*p_editions)[j]->Append( *p_segment->stored_editions[j] );
1780 if ( Edition() != NULL )
1781 Edition()->PublishChapters( title );
1786 void chapter_edition_c::PublishChapters( input_title_t & title )
1788 title.i_seekpoint = 0;
1789 if ( title.seekpoint != NULL )
1790 free( title.seekpoint );
1791 chapter_item_c::PublishChapters( title, 0 );
1794 void chapter_item_c::PublishChapters( input_title_t & title, int i_level )
1796 if (b_display_seekpoint)
1798 seekpoint_t *sk = vlc_seekpoint_New();
1800 sk->i_level = i_level;
1801 sk->i_time_offset = i_start_time;
1802 sk->psz_name = strdup( psz_name.c_str() );
1804 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
1805 title.i_seekpoint++;
1806 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
1807 title.seekpoint[title.i_seekpoint-1] = sk;
1809 i_seekpoint_num = title.i_seekpoint;
1812 for ( size_t i=0; i<sub_chapters.size() ; i++)
1814 sub_chapters[i]->PublishChapters( title, i_level+1 );
1818 void virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
1820 demux_sys_t & sys = *demux.p_sys;
1821 chapter_item_c *psz_curr_chapter;
1823 /* update current chapter/seekpoint */
1824 if ( p_editions->size() )
1826 /* 1st, we need to know in which chapter we are */
1827 psz_curr_chapter = (*p_editions)[i_current_edition]->FindTimecode( sys.i_pts );
1829 /* we have moved to a new chapter */
1830 if (psz_curr_chapter != NULL && psz_current_chapter != psz_curr_chapter)
1832 if ( psz_current_chapter != NULL )
1833 psz_current_chapter->Leave();
1835 if ( psz_curr_chapter->i_seekpoint_num > 0 )
1837 demux.info.i_update |= INPUT_UPDATE_SEEKPOINT;
1838 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
1841 if ( (*p_editions)[i_current_edition]->b_ordered )
1843 psz_curr_chapter->Enter();
1845 // only seek if necessary
1846 if ( psz_current_chapter == NULL || (psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time) )
1847 Seek( demux, sys.i_pts, 0, psz_curr_chapter );
1850 psz_current_chapter = psz_curr_chapter;
1854 chapter_item_c *matroska_segment_c::BrowseCodecPrivate( unsigned int codec_id,
1855 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1856 const void *p_cookie,
1857 size_t i_cookie_size )
1859 // FIXME don't assume it is the first edition
1860 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
1861 if ( index != stored_editions.end() )
1863 chapter_item_c *p_result = (*index)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
1864 if ( p_result != NULL )
1870 chapter_item_c *chapter_item_c::BrowseCodecPrivate( unsigned int codec_id,
1871 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1872 const void *p_cookie,
1873 size_t i_cookie_size )
1876 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
1877 while ( index != codecs.end() )
1879 if ( match( **index ,p_cookie, i_cookie_size ) )
1885 chapter_item_c *p_result = NULL;
1886 std::vector<chapter_item_c*>::const_iterator index2 = sub_chapters.begin();
1887 while ( index2 != sub_chapters.end() )
1889 p_result = (*index2)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
1890 if ( p_result != NULL )
1898 void chapter_item_c::Append( const chapter_item_c & chapter )
1900 // we are appending content for the same chapter UID
1902 chapter_item_c *p_chapter;
1904 for ( i=0; i<chapter.sub_chapters.size(); i++ )
1906 p_chapter = FindChapter( *chapter.sub_chapters[i] );
1907 if ( p_chapter != NULL )
1909 p_chapter->Append( *chapter.sub_chapters[i] );
1913 sub_chapters.push_back( chapter.sub_chapters[i] );
1917 i_user_start_time = min( i_user_start_time, chapter.i_user_start_time );
1918 i_user_end_time = max( i_user_end_time, chapter.i_user_end_time );
1921 chapter_item_c * chapter_item_c::FindChapter( const chapter_item_c & chapter )
1924 for ( i=0; i<sub_chapters.size(); i++)
1926 if ( sub_chapters[i]->i_uid == chapter.i_uid )
1927 return sub_chapters[i];
1932 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter )
1934 demux_sys_t *p_sys = p_demux->p_sys;
1935 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
1936 matroska_segment_c *p_segment = p_vsegment->Segment();
1937 mtime_t i_time_offset = 0;
1941 msg_Dbg( p_demux, "seek request to "I64Fd" (%f%%)", i_date, f_percent );
1942 if( i_date < 0 && f_percent < 0 )
1944 msg_Warn( p_demux, "cannot seek nowhere !" );
1947 if( f_percent > 1.0 )
1949 msg_Warn( p_demux, "cannot seek so far !" );
1953 /* seek without index or without date */
1954 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
1956 if (p_sys->f_duration >= 0)
1958 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
1962 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
1964 msg_Dbg( p_demux, "inacurate way of seeking" );
1965 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
1967 if( p_segment->index[i_index].i_position >= i_pos)
1972 if( i_index == p_segment->i_index )
1977 i_date = p_segment->index[i_index].i_time;
1980 if( p_segment->index[i_index].i_position < i_pos )
1984 msg_Warn( p_demux, "searching for cluster, could take some time" );
1986 /* search a cluster */
1987 while( ( el = p_sys->ep->Get() ) != NULL )
1989 if( MKV_IS_ID( el, KaxCluster ) )
1991 KaxCluster *cluster = (KaxCluster*)el;
1993 /* add it to the index */
1994 p_segment->IndexAppendCluster( cluster );
1996 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1998 p_sys->cluster = cluster;
2009 p_vsegment->Seek( *p_demux, i_date, i_time_offset, psz_chapter );
2012 /*****************************************************************************
2013 * Demux: reads and demuxes data packets
2014 *****************************************************************************
2015 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
2016 *****************************************************************************/
2017 static int Demux( demux_t *p_demux)
2019 demux_sys_t *p_sys = p_demux->p_sys;
2020 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
2021 matroska_segment_c *p_segmet = p_vsegment->Segment();
2022 if ( p_segmet == NULL ) return 0;
2023 int i_block_count = 0;
2026 int64_t i_block_duration;
2027 int64_t i_block_ref1;
2028 int64_t i_block_ref2;
2032 if ( p_sys->demuxer.b_die )
2035 if( p_sys->i_pts >= p_sys->i_start_pts )
2036 p_vsegment->UpdateCurrentToChapter( *p_demux );
2038 if ( p_vsegment->EditionIsOrdered() && p_vsegment->CurrentChapter() == NULL )
2040 /* nothing left to read in this ordered edition */
2041 if ( !p_vsegment->SelectNext() )
2043 p_segmet->UnSelect( );
2045 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
2047 /* switch to the next segment */
2048 p_segmet = p_vsegment->Segment();
2049 if ( !p_segmet->Select( 0 ) )
2051 msg_Err( p_demux, "Failed to select new segment" );
2058 if( p_segmet->BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
2060 if ( p_vsegment->EditionIsOrdered() )
2062 const chapter_item_c *p_chap = p_vsegment->CurrentChapter();
2063 // check if there are more chapters to read
2064 if ( p_chap != NULL )
2066 /* TODO handle successive chapters with the same user_start_time/user_end_time
2067 if ( p_chap->i_user_start_time == p_chap->i_user_start_time )
2068 p_vsegment->SelectNext();
2070 p_sys->i_pts = p_chap->i_user_end_time;
2071 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
2078 msg_Warn( p_demux, "cannot get block EOF?" );
2079 p_segmet->UnSelect( );
2081 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
2083 /* switch to the next segment */
2084 if ( !p_vsegment->SelectNext() )
2085 // no more segments in this stream
2087 p_segmet = p_vsegment->Segment();
2088 if ( !p_segmet->Select( 0 ) )
2090 msg_Err( p_demux, "Failed to select new segment" );
2097 p_sys->i_pts = p_sys->i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
2099 if( p_sys->i_pts >= p_sys->i_start_pts )
2101 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
2104 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
2109 // TODO optimize when there is need to leave or when seeking has been called
2110 if( i_block_count > 5 )
2119 /*****************************************************************************
2121 *****************************************************************************/
2122 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
2128 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
2130 if( i_size <= 0 || mb_eof )
2135 return stream_Read( s, p_buffer, i_size );
2137 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
2143 case seek_beginning:
2147 i_pos = stream_Size( s ) - i_offset;
2150 i_pos= stream_Tell( s ) + i_offset;
2154 if( i_pos < 0 || i_pos >= stream_Size( s ) )
2161 if( stream_Seek( s, i_pos ) )
2167 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
2171 uint64 vlc_stream_io_callback::getFilePointer( void )
2173 return stream_Tell( s );
2175 void vlc_stream_io_callback::close( void )
2181 /*****************************************************************************
2182 * Ebml Stream parser
2183 *****************************************************************************/
2184 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux )
2191 mi_remain_size[0] = el_start->GetSize();
2193 for( i = 1; i < 6; i++ )
2199 mb_keep = VLC_FALSE;
2200 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
2203 EbmlParser::~EbmlParser( void )
2207 for( i = 1; i < mi_level; i++ )
2213 mb_keep = VLC_FALSE;
2217 void EbmlParser::Up( void )
2219 if( mi_user_level == mi_level )
2221 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
2227 void EbmlParser::Down( void )
2233 void EbmlParser::Keep( void )
2238 int EbmlParser::GetLevel( void )
2240 return mi_user_level;
2243 void EbmlParser::Reset( demux_t *p_demux )
2245 while ( mi_level > 0)
2247 delete m_el[mi_level];
2248 m_el[mi_level] = NULL;
2251 mi_user_level = mi_level = 1;
2252 #if LIBEBML_VERSION >= 0x000704
2253 // a little faster and cleaner
2254 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
2256 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
2258 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
2261 EbmlElement *EbmlParser::Get( void )
2265 if( mi_user_level != mi_level )
2271 EbmlElement *ret = m_got;
2277 if( m_el[mi_level] )
2279 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
2282 delete m_el[mi_level];
2284 mb_keep = VLC_FALSE;
2287 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, mb_dummy, 1 );
2288 // mi_remain_size[mi_level] = m_el[mi_level]->GetSize();
2299 delete m_el[mi_level - 1];
2300 m_got = m_el[mi_level -1] = m_el[mi_level];
2301 m_el[mi_level] = NULL;
2308 else if( m_el[mi_level] == NULL )
2310 fprintf( stderr," m_el[mi_level] == NULL\n" );
2313 return m_el[mi_level];
2317 /*****************************************************************************
2319 * * LoadCues : load the cues element and update index
2321 * * LoadTags : load ... the tags element
2323 * * InformationCreate : create all information, load tags if present
2325 *****************************************************************************/
2326 void matroska_segment_c::LoadCues( )
2328 int64_t i_sav_position = es.I_O().getFilePointer();
2330 EbmlElement *el, *cues;
2332 /* *** Load the cue if found *** */
2333 if( i_cues_position < 0 )
2335 msg_Warn( &sys.demuxer, "no cues/empty cues found->seek won't be precise" );
2337 // IndexAppendCluster( cluster );
2340 vlc_bool_t b_seekable;
2342 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
2346 msg_Dbg( &sys.demuxer, "loading cues" );
2347 es.I_O().setFilePointer( i_cues_position, seek_beginning );
2348 cues = es.FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
2352 msg_Err( &sys.demuxer, "cannot load cues (broken seekhead or file)" );
2353 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2357 ep = new EbmlParser( &es, cues, &sys.demuxer );
2358 while( ( el = ep->Get() ) != NULL )
2360 if( MKV_IS_ID( el, KaxCuePoint ) )
2362 #define idx index[i_index]
2365 idx.i_block_number= -1;
2366 idx.i_position = -1;
2368 idx.b_key = VLC_TRUE;
2371 while( ( el = ep->Get() ) != NULL )
2373 if( MKV_IS_ID( el, KaxCueTime ) )
2375 KaxCueTime &ctime = *(KaxCueTime*)el;
2377 ctime.ReadData( es.I_O() );
2379 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
2381 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
2384 while( ( el = ep->Get() ) != NULL )
2386 if( MKV_IS_ID( el, KaxCueTrack ) )
2388 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
2390 ctrack.ReadData( es.I_O() );
2391 idx.i_track = uint16( ctrack );
2393 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
2395 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
2397 ccpos.ReadData( es.I_O() );
2398 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
2400 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
2402 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2404 cbnum.ReadData( es.I_O() );
2405 idx.i_block_number = uint32( cbnum );
2409 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2416 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2422 msg_Dbg( &sys.demuxer, " * added time="I64Fd" pos="I64Fd
2423 " track=%d bnum=%d", idx.i_time, idx.i_position,
2424 idx.i_track, idx.i_block_number );
2428 if( i_index >= i_index_max )
2430 i_index_max += 1024;
2431 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
2437 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2445 msg_Dbg( &sys.demuxer, "loading cues done." );
2446 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2449 void matroska_segment_c::LoadTags( )
2451 int64_t i_sav_position = es.I_O().getFilePointer();
2453 EbmlElement *el, *tags;
2455 msg_Dbg( &sys.demuxer, "loading tags" );
2456 es.I_O().setFilePointer( i_tags_position, seek_beginning );
2457 tags = es.FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2461 msg_Err( &sys.demuxer, "cannot load tags (broken seekhead or file)" );
2462 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2466 msg_Dbg( &sys.demuxer, "Tags" );
2467 ep = new EbmlParser( &es, tags, &sys.demuxer );
2468 while( ( el = ep->Get() ) != NULL )
2470 if( MKV_IS_ID( el, KaxTag ) )
2472 msg_Dbg( &sys.demuxer, "+ Tag" );
2474 while( ( el = ep->Get() ) != NULL )
2476 if( MKV_IS_ID( el, KaxTagTargets ) )
2478 msg_Dbg( &sys.demuxer, "| + Targets" );
2480 while( ( el = ep->Get() ) != NULL )
2482 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2486 else if( MKV_IS_ID( el, KaxTagGeneral ) )
2488 msg_Dbg( &sys.demuxer, "| + General" );
2490 while( ( el = ep->Get() ) != NULL )
2492 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2496 else if( MKV_IS_ID( el, KaxTagGenres ) )
2498 msg_Dbg( &sys.demuxer, "| + Genres" );
2500 while( ( el = ep->Get() ) != NULL )
2502 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2506 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
2508 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
2510 while( ( el = ep->Get() ) != NULL )
2512 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2516 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
2518 msg_Dbg( &sys.demuxer, "| + Images Specific" );
2520 while( ( el = ep->Get() ) != NULL )
2522 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2526 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
2528 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
2530 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
2532 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
2534 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
2536 msg_Dbg( &sys.demuxer, "| + Multi Date" );
2538 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
2540 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
2542 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
2544 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
2546 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
2548 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
2550 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
2552 msg_Dbg( &sys.demuxer, "| + Multi Title" );
2556 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid( *el ).name() );
2563 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
2569 msg_Dbg( &sys.demuxer, "loading tags done." );
2570 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2573 /*****************************************************************************
2575 *****************************************************************************/
2576 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
2580 int i_upper_level = 0;
2582 msg_Dbg( &sys.demuxer, "| + Seek head" );
2584 /* Master elements */
2585 seekhead->Read( es, seekhead->Generic().Context, i_upper_level, el, true );
2587 for( i = 0; i < seekhead->ListSize(); i++ )
2589 EbmlElement *l = (*seekhead)[i];
2591 if( MKV_IS_ID( l, KaxSeek ) )
2593 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
2594 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
2597 for( j = 0; j < sk->ListSize(); j++ )
2599 EbmlElement *l = (*sk)[j];
2601 if( MKV_IS_ID( l, KaxSeekID ) )
2603 KaxSeekID &sid = *(KaxSeekID*)l;
2604 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
2606 else if( MKV_IS_ID( l, KaxSeekPosition ) )
2608 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
2609 i_pos = uint64( spos );
2613 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
2619 if( id == KaxCues::ClassInfos.GlobalId )
2621 msg_Dbg( &sys.demuxer, "| | | = cues at "I64Fd, i_pos );
2622 i_cues_position = segment->GetGlobalPosition( i_pos );
2624 else if( id == KaxChapters::ClassInfos.GlobalId )
2626 msg_Dbg( &sys.demuxer, "| | | = chapters at "I64Fd, i_pos );
2627 i_chapters_position = segment->GetGlobalPosition( i_pos );
2629 else if( id == KaxTags::ClassInfos.GlobalId )
2631 msg_Dbg( &sys.demuxer, "| | | = tags at "I64Fd, i_pos );
2632 i_tags_position = segment->GetGlobalPosition( i_pos );
2638 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
2643 /*****************************************************************************
2645 *****************************************************************************/
2646 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
2652 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
2654 tk = new mkv_track_t();
2655 tracks.push_back( tk );
2657 /* Init the track */
2658 memset( tk, 0, sizeof( mkv_track_t ) );
2660 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
2661 tk->fmt.psz_language = strdup("English");
2662 tk->fmt.psz_description = NULL;
2664 tk->b_default = VLC_TRUE;
2665 tk->b_enabled = VLC_TRUE;
2666 tk->b_silent = VLC_FALSE;
2667 tk->i_number = tracks.size() - 1;
2668 tk->i_extra_data = 0;
2669 tk->p_extra_data = NULL;
2670 tk->psz_codec = NULL;
2671 tk->i_default_duration = 0;
2672 tk->f_timecodescale = 1.0;
2674 tk->b_inited = VLC_FALSE;
2675 tk->i_data_init = 0;
2676 tk->p_data_init = NULL;
2678 tk->psz_codec_name = NULL;
2679 tk->psz_codec_settings = NULL;
2680 tk->psz_codec_info_url = NULL;
2681 tk->psz_codec_download_url = NULL;
2683 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
2685 for( i = 0; i < m->ListSize(); i++ )
2687 EbmlElement *l = (*m)[i];
2689 if( MKV_IS_ID( l, KaxTrackNumber ) )
2691 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
2693 tk->i_number = uint32( tnum );
2694 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
2696 else if( MKV_IS_ID( l, KaxTrackUID ) )
2698 KaxTrackUID &tuid = *(KaxTrackUID*)l;
2700 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
2702 else if( MKV_IS_ID( l, KaxTrackType ) )
2705 KaxTrackType &ttype = *(KaxTrackType*)l;
2707 switch( uint8(ttype) )
2711 tk->fmt.i_cat = AUDIO_ES;
2715 tk->fmt.i_cat = VIDEO_ES;
2717 case track_subtitle:
2718 psz_type = "subtitle";
2719 tk->fmt.i_cat = SPU_ES;
2722 psz_type = "unknown";
2723 tk->fmt.i_cat = UNKNOWN_ES;
2727 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
2729 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
2731 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
2733 // tk->b_enabled = uint32( fenb );
2734 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
2735 // uint32( fenb ) );
2737 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
2739 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
2741 tk->b_default = uint32( fdef );
2742 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
2744 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
2746 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
2748 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
2750 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
2752 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
2754 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
2756 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
2758 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
2760 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
2762 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
2764 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
2766 tk->i_default_duration = uint64(defd);
2767 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration="I64Fd, uint64(defd) );
2769 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
2771 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
2773 tk->f_timecodescale = float( ttcs );
2774 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
2776 else if( MKV_IS_ID( l, KaxTrackName ) )
2778 KaxTrackName &tname = *(KaxTrackName*)l;
2780 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
2781 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
2783 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
2785 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
2787 tk->fmt.psz_language = strdup( string( lang ).c_str() );
2788 msg_Dbg( &sys.demuxer,
2789 "| | | + Track Language=`%s'", tk->fmt.psz_language );
2791 else if( MKV_IS_ID( l, KaxCodecID ) )
2793 KaxCodecID &codecid = *(KaxCodecID*)l;
2795 tk->psz_codec = strdup( string( codecid ).c_str() );
2796 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
2798 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
2800 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
2802 tk->i_extra_data = cpriv.GetSize();
2803 if( tk->i_extra_data > 0 )
2805 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
2806 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
2808 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
2810 else if( MKV_IS_ID( l, KaxCodecName ) )
2812 KaxCodecName &cname = *(KaxCodecName*)l;
2814 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
2815 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
2817 else if( MKV_IS_ID( l, KaxContentEncodings ) )
2819 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
2820 MkvTree( sys.demuxer, 3, "Content Encodings" );
2821 for( j = 0; j < cencs->ListSize(); j++ )
2823 EbmlElement *l2 = (*cencs)[j];
2824 if( MKV_IS_ID( l2, KaxContentEncoding ) )
2826 MkvTree( sys.demuxer, 4, "Content Encoding" );
2827 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
2828 for( k = 0; k < cenc->ListSize(); k++ )
2830 EbmlElement *l3 = (*cenc)[k];
2831 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
2833 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
2834 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
2836 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
2838 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
2839 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
2841 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
2843 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
2844 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
2846 else if( MKV_IS_ID( l3, KaxContentCompression ) )
2848 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
2849 MkvTree( sys.demuxer, 5, "Content Compression" );
2850 for( n = 0; n < compr->ListSize(); n++ )
2852 EbmlElement *l4 = (*compr)[n];
2853 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
2855 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
2856 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
2857 if( uint32( compalg ) == 0 )
2859 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
2864 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
2871 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
2878 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
2883 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
2885 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
2887 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
2888 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
2890 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
2892 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
2894 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
2895 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
2897 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
2899 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
2901 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
2902 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
2904 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
2906 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
2908 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
2910 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
2912 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
2914 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
2916 else if( MKV_IS_ID( l, KaxTrackVideo ) )
2918 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
2921 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
2924 for( j = 0; j < tkv->ListSize(); j++ )
2926 EbmlElement *l = (*tkv)[j];
2927 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
2929 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
2931 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
2933 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
2935 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
2937 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
2940 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
2942 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
2944 tk->fmt.video.i_width = uint16( vwidth );
2945 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
2947 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
2949 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
2951 tk->fmt.video.i_height = uint16( vheight );
2952 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
2954 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
2956 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
2958 tk->fmt.video.i_visible_width = uint16( vwidth );
2959 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
2961 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
2963 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
2965 tk->fmt.video.i_visible_height = uint16( vheight );
2966 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
2968 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
2970 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
2972 tk->f_fps = float( vfps );
2973 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
2975 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
2977 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
2979 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
2980 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
2982 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
2984 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
2986 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
2988 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
2990 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
2992 // msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( gamma ) );
2996 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
2999 if ( tk->fmt.video.i_visible_height && tk->fmt.video.i_visible_width )
3000 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * tk->fmt.video.i_visible_width / tk->fmt.video.i_visible_height;
3002 else if( MKV_IS_ID( l, KaxTrackAudio ) )
3004 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
3007 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
3009 for( j = 0; j < tka->ListSize(); j++ )
3011 EbmlElement *l = (*tka)[j];
3013 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
3015 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
3017 tk->fmt.audio.i_rate = (int)float( afreq );
3018 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
3020 else if( MKV_IS_ID( l, KaxAudioChannels ) )
3022 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
3024 tk->fmt.audio.i_channels = uint8( achan );
3025 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
3027 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
3029 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
3031 tk->fmt.audio.i_bitspersample = uint8( abits );
3032 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
3036 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
3042 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
3043 typeid(*l).name() );
3048 /*****************************************************************************
3050 *****************************************************************************/
3051 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
3055 int i_upper_level = 0;
3057 msg_Dbg( &sys.demuxer, "| + Tracks" );
3059 /* Master elements */
3060 tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
3062 for( i = 0; i < tracks->ListSize(); i++ )
3064 EbmlElement *l = (*tracks)[i];
3066 if( MKV_IS_ID( l, KaxTrackEntry ) )
3068 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
3072 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3077 /*****************************************************************************
3079 *****************************************************************************/
3080 void matroska_segment_c::ParseInfo( KaxInfo *info )
3085 int i_upper_level = 0;
3087 msg_Dbg( &sys.demuxer, "| + Information" );
3089 /* Master elements */
3090 m = static_cast<EbmlMaster *>(info);
3091 m->Read( es, info->Generic().Context, i_upper_level, el, true );
3093 for( i = 0; i < m->ListSize(); i++ )
3095 EbmlElement *l = (*m)[i];
3097 if( MKV_IS_ID( l, KaxSegmentUID ) )
3099 segment_uid = *(new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l)));
3101 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)segment_uid.GetBuffer() );
3103 else if( MKV_IS_ID( l, KaxPrevUID ) )
3105 prev_segment_uid = *(new KaxPrevUID(*static_cast<KaxPrevUID*>(l)));
3107 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)prev_segment_uid.GetBuffer() );
3109 else if( MKV_IS_ID( l, KaxNextUID ) )
3111 next_segment_uid = *(new KaxNextUID(*static_cast<KaxNextUID*>(l)));
3113 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)next_segment_uid.GetBuffer() );
3115 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
3117 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
3119 i_timescale = uint64(tcs);
3121 msg_Dbg( &sys.demuxer, "| | + TimecodeScale="I64Fd,
3124 else if( MKV_IS_ID( l, KaxDuration ) )
3126 KaxDuration &dur = *(KaxDuration*)l;
3128 i_duration = mtime_t( double( dur ) );
3130 msg_Dbg( &sys.demuxer, "| | + Duration="I64Fd,
3133 else if( MKV_IS_ID( l, KaxMuxingApp ) )
3135 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
3137 psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
3139 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
3140 psz_muxing_application );
3142 else if( MKV_IS_ID( l, KaxWritingApp ) )
3144 KaxWritingApp &wapp = *(KaxWritingApp*)l;
3146 psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
3148 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
3149 psz_writing_application );
3151 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
3153 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
3155 psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
3157 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
3158 psz_segment_filename );
3160 else if( MKV_IS_ID( l, KaxTitle ) )
3162 KaxTitle &title = *(KaxTitle*)l;
3164 psz_title = UTF8ToStr( UTFstring( title ) );
3166 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
3168 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
3170 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
3172 families.push_back(*uid);
3174 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
3176 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
3177 else if( MKV_IS_ID( l, KaxDateUTC ) )
3179 KaxDateUTC &date = *(KaxDateUTC*)l;
3184 i_date = date.GetEpochDate();
3185 memset( buffer, 0, 256 );
3186 if( gmtime_r( &i_date, &tmres ) &&
3187 asctime_r( &tmres, buffer ) )
3189 buffer[strlen( buffer)-1]= '\0';
3190 psz_date_utc = strdup( buffer );
3191 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
3195 #if LIBMATROSKA_VERSION >= 0x000704
3196 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
3198 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
3199 chapter_translation_c translated;
3201 p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
3202 for( j = 0; j < p_trans->ListSize(); j++ )
3204 EbmlElement *l = (*p_trans)[j];
3206 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
3208 translated.editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
3210 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
3212 translated.codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
3214 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
3216 translated.translated = *( new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) ) );
3220 translations.push_back( translated );
3225 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3229 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
3230 i_duration = mtime_t(f_dur);
3234 /*****************************************************************************
3236 *****************************************************************************/
3237 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
3241 if( sys.title == NULL )
3243 sys.title = vlc_input_title_New();
3246 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
3247 for( i = 0; i < ca->ListSize(); i++ )
3249 EbmlElement *l = (*ca)[i];
3251 if( MKV_IS_ID( l, KaxChapterUID ) )
3253 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
3254 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
3256 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
3258 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
3259 chapters.b_display_seekpoint = uint8( flag ) == 0;
3261 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
3263 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
3265 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
3266 chapters.i_start_time = uint64( start ) / I64C(1000);
3268 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
3270 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
3272 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
3273 chapters.i_end_time = uint64( end ) / I64C(1000);
3275 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
3277 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
3279 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
3281 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
3282 for( j = 0; j < cd->ListSize(); j++ )
3284 EbmlElement *l= (*cd)[j];
3286 if( MKV_IS_ID( l, KaxChapterString ) )
3290 KaxChapterString &name =*(KaxChapterString*)l;
3291 for (k = 0; k < i_level; k++)
3292 chapters.psz_name += '+';
3293 chapters.psz_name += ' ';
3294 chapters.psz_name += UTF8ToStr( UTFstring( name ) );
3296 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", UTF8ToStr(UTFstring(name)) );
3298 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
3300 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
3301 const char *psz = string( lang ).c_str();
3303 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
3305 else if( MKV_IS_ID( l, KaxChapterCountry ) )
3307 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
3308 const char *psz = string( ct ).c_str();
3310 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
3314 else if( MKV_IS_ID( l, KaxChapterProcess ) )
3316 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
3318 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
3319 chapter_codec_cmds_c *p_ccodec = NULL;
3321 for( j = 0; j < cp->ListSize(); j++ )
3323 EbmlElement *k= (*cp)[j];
3325 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
3327 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
3328 if ( uint32(*p_codec_id) == 0 )
3329 p_ccodec = new matroska_script_codec_c();
3330 else if ( uint32(*p_codec_id) == 1 )
3331 p_ccodec = new dvd_chapter_codec_c( sys );
3336 if ( p_ccodec != NULL )
3338 for( j = 0; j < cp->ListSize(); j++ )
3340 EbmlElement *k= (*cp)[j];
3342 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
3344 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
3345 p_ccodec->SetPrivate( *p_private );
3347 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
3349 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
3352 chapters.codecs.push_back( p_ccodec );
3355 else if( MKV_IS_ID( l, KaxChapterAtom ) )
3357 chapter_item_c *new_sub_chapter = new chapter_item_c();
3358 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
3359 new_sub_chapter->psz_parent = &chapters;
3360 chapters.sub_chapters.push_back( new_sub_chapter );
3365 /*****************************************************************************
3367 *****************************************************************************/
3368 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
3372 int i_upper_level = 0;
3375 /* Master elements */
3376 chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
3378 for( i = 0; i < chapters->ListSize(); i++ )
3380 EbmlElement *l = (*chapters)[i];
3382 if( MKV_IS_ID( l, KaxEditionEntry ) )
3384 chapter_edition_c *p_edition = new chapter_edition_c();
3386 EbmlMaster *E = static_cast<EbmlMaster *>(l );
3388 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
3389 for( j = 0; j < E->ListSize(); j++ )
3391 EbmlElement *l = (*E)[j];
3393 if( MKV_IS_ID( l, KaxChapterAtom ) )
3395 chapter_item_c *new_sub_chapter = new chapter_item_c();
3396 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
3397 p_edition->sub_chapters.push_back( new_sub_chapter );
3399 else if( MKV_IS_ID( l, KaxEditionUID ) )
3401 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
3403 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
3405 p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
3407 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
3409 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
3410 i_default_edition = stored_editions.size();
3414 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
3417 stored_editions.push_back( p_edition );
3421 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3425 for( i = 0; i < stored_editions.size(); i++ )
3427 stored_editions[i]->RefreshChapters( );
3430 if ( stored_editions[i_default_edition]->b_ordered )
3432 /* update the duration of the segment according to the sum of all sub chapters */
3433 i_dur = stored_editions[i_default_edition]->Duration() / I64C(1000);
3439 void matroska_segment_c::ParseCluster( )
3444 int i_upper_level = 0;
3446 /* Master elements */
3447 m = static_cast<EbmlMaster *>( cluster );
3448 m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
3450 for( i = 0; i < m->ListSize(); i++ )
3452 EbmlElement *l = (*m)[i];
3454 if( MKV_IS_ID( l, KaxClusterTimecode ) )
3456 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
3458 cluster->InitTimecode( uint64( ctc ), i_timescale );
3463 i_start_time = cluster->GlobalTimecode() / 1000;
3466 /*****************************************************************************
3467 * InformationCreate:
3468 *****************************************************************************/
3469 void matroska_segment_c::InformationCreate( )
3473 sys.meta = vlc_meta_New();
3477 vlc_meta_Add( sys.meta, VLC_META_TITLE, psz_title );
3481 vlc_meta_Add( sys.meta, VLC_META_DATE, psz_date_utc );
3483 if( psz_segment_filename )
3485 vlc_meta_Add( sys.meta, _("Segment filename"), psz_segment_filename );
3487 if( psz_muxing_application )
3489 vlc_meta_Add( sys.meta, _("Muxing application"), psz_muxing_application );
3491 if( psz_writing_application )
3493 vlc_meta_Add( sys.meta, _("Writing application"), psz_writing_application );
3496 for( i_track = 0; i_track < tracks.size(); i_track++ )
3498 mkv_track_t *tk = tracks[i_track];
3499 vlc_meta_t *mtk = vlc_meta_New();
3501 sys.meta->track = (vlc_meta_t**)realloc( sys.meta->track,
3502 sizeof( vlc_meta_t * ) * ( sys.meta->i_track + 1 ) );
3503 sys.meta->track[sys.meta->i_track++] = mtk;
3505 if( tk->fmt.psz_description )
3507 vlc_meta_Add( sys.meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
3509 if( tk->psz_codec_name )
3511 vlc_meta_Add( sys.meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
3513 if( tk->psz_codec_settings )
3515 vlc_meta_Add( sys.meta, VLC_META_SETTING, tk->psz_codec_settings );
3517 if( tk->psz_codec_info_url )
3519 vlc_meta_Add( sys.meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
3521 if( tk->psz_codec_download_url )
3523 vlc_meta_Add( sys.meta, VLC_META_URL, tk->psz_codec_download_url );
3527 if( i_tags_position >= 0 )
3529 vlc_bool_t b_seekable;
3531 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
3540 /*****************************************************************************
3542 *****************************************************************************/
3544 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
3546 #define idx index[i_index]
3548 idx.i_block_number= -1;
3549 idx.i_position = cluster->GetElementPosition();
3551 idx.b_key = VLC_TRUE;
3554 if( i_index >= i_index_max )
3556 i_index_max += 1024;
3557 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
3562 static char * UTF8ToStr( const UTFstring &u )
3571 p = dst = (char*)malloc( i_src + 1);
3590 void chapter_edition_c::RefreshChapters( )
3592 chapter_item_c::RefreshChapters( b_ordered, -1 );
3593 b_display_seekpoint = false;
3596 int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_time )
3598 int64_t i_user_time = i_prev_user_time;
3600 // first the sub-chapters, and then ourself
3601 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
3602 while ( index != sub_chapters.end() )
3604 i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
3610 // the ordered chapters always start at zero
3611 if ( i_prev_user_time == -1 )
3613 if ( i_user_time == -1 )
3615 i_prev_user_time = 0;
3618 i_user_start_time = i_prev_user_time;
3619 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
3621 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
3625 i_user_end_time = i_user_time;
3630 std::sort( sub_chapters.begin(), sub_chapters.end() );
3631 i_user_start_time = i_start_time;
3632 if ( i_end_time != -1 )
3633 i_user_end_time = i_end_time;
3634 else if ( i_user_time != -1 )
3635 i_user_end_time = i_user_time;
3637 i_user_end_time = i_user_start_time;
3640 return i_user_end_time;
3643 mtime_t chapter_edition_c::Duration() const
3645 mtime_t i_result = 0;
3647 if ( sub_chapters.size() )
3649 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
3651 i_result = (*index)->i_user_end_time;
3657 chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode )
3659 chapter_item_c *psz_result = NULL;
3661 if ( i_user_timecode >= i_user_start_time &&
3662 ( i_user_timecode < i_user_end_time ||
3663 ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
3665 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
3666 while ( index != sub_chapters.end() && psz_result == NULL )
3668 psz_result = (*index)->FindTimecode( i_user_timecode );
3672 if ( psz_result == NULL )
3679 void demux_sys_t::PreloadFamily( const matroska_segment_c & of_segment )
3681 for (size_t i=0; i<opened_segments.size(); i++)
3683 opened_segments[i]->PreloadFamily( of_segment );
3686 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
3691 for (size_t i=0; i<families.size(); i++)
3693 for (size_t j=0; j<of_segment.families.size(); j++)
3695 if ( families[i] == of_segment.families[j] )
3703 // preload all the linked segments for all preloaded segments
3704 void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
3706 size_t i_preloaded, i;
3708 delete p_current_segment;
3709 p_current_segment = new virtual_segment_c( p_segment );
3711 // fill our current virtual segment with all hard linked segments
3714 for ( i=0; i< opened_segments.size(); i++ )
3716 i_preloaded += p_current_segment->AddSegment( opened_segments[i] );
3718 } while ( i_preloaded ); // worst case: will stop when all segments are found as linked
3720 p_current_segment->Sort( );
3722 p_current_segment->PreloadLinked( );
3725 bool demux_sys_t::PreparePlayback( )
3727 p_current_segment->LoadCues();
3728 f_duration = p_current_segment->Duration();
3730 /* add information */
3731 p_current_segment->Segment()->InformationCreate( );
3733 p_current_segment->Segment()->Select( 0 );
3735 return p_current_segment->Select( *title );
3738 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
3740 EbmlBinary * p_itema = (EbmlBinary *)(&p_item_a->segment_uid);
3741 if ( *p_itema == p_item_b->prev_segment_uid )
3744 p_itema = (EbmlBinary *)(&p_item_a->next_segment_uid);
3745 if ( *p_itema == p_item_b->segment_uid )
3748 if ( *p_itema == p_item_b->prev_segment_uid )
3754 bool matroska_segment_c::Preload( )
3759 EbmlElement *el = NULL;
3761 ep->Reset( &sys.demuxer );
3763 while( ( el = ep->Get() ) != NULL )
3765 if( MKV_IS_ID( el, KaxInfo ) )
3767 ParseInfo( static_cast<KaxInfo*>( el ) );
3769 else if( MKV_IS_ID( el, KaxTracks ) )
3771 ParseTracks( static_cast<KaxTracks*>( el ) );
3773 else if( MKV_IS_ID( el, KaxSeekHead ) )
3775 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
3777 else if( MKV_IS_ID( el, KaxCues ) )
3779 msg_Dbg( &sys.demuxer, "| + Cues" );
3781 else if( MKV_IS_ID( el, KaxCluster ) )
3783 msg_Dbg( &sys.demuxer, "| + Cluster" );
3785 cluster = (KaxCluster*)el;
3787 i_start_pos = cluster->GetElementPosition();
3791 /* stop parsing the stream */
3794 else if( MKV_IS_ID( el, KaxAttachments ) )
3796 msg_Dbg( &sys.demuxer, "| + Attachments FIXME (but probably never supported)" );
3798 else if( MKV_IS_ID( el, KaxChapters ) )
3800 msg_Dbg( &sys.demuxer, "| + Chapters" );
3801 ParseChapters( static_cast<KaxChapters*>( el ) );
3803 else if( MKV_IS_ID( el, KaxTag ) )
3805 msg_Dbg( &sys.demuxer, "| + Tags FIXME TODO" );
3809 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid(*el).name() );
3818 matroska_segment_c *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
3820 for (size_t i=0; i<opened_segments.size(); i++)
3822 if ( opened_segments[i]->segment_uid == uid )
3823 return opened_segments[i];
3828 chapter_item_c *demux_sys_t::BrowseCodecPrivate( unsigned int codec_id,
3829 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3830 const void *p_cookie,
3831 size_t i_cookie_size,
3832 matroska_segment_c * &p_segment_found )
3834 chapter_item_c *p_result = NULL;
3835 for (size_t i=0; i<opened_segments.size(); i++)
3837 p_result = opened_segments[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3838 if ( p_result != NULL )
3840 p_segment_found = opened_segments[i];
3847 void virtual_segment_c::Sort()
3849 // keep the current segment index
3850 matroska_segment_c *p_segment = linked_segments[i_current_segment];
3852 std::sort( linked_segments.begin(), linked_segments.end(), matroska_segment_c::CompareSegmentUIDs );
3854 for ( i_current_segment=0; i_current_segment<linked_segments.size(); i_current_segment++)
3855 if ( linked_segments[i_current_segment] == p_segment )
3859 size_t virtual_segment_c::AddSegment( matroska_segment_c *p_segment )
3862 // check if it's not already in here
3863 for ( i=0; i<linked_segments.size(); i++ )
3865 if ( p_segment->segment_uid == linked_segments[i]->segment_uid )
3869 // find possible mates
3870 for ( i=0; i<linked_uids.size(); i++ )
3872 if ( p_segment->segment_uid == linked_uids[i]
3873 || p_segment->prev_segment_uid == linked_uids[i]
3874 || p_segment->next_segment_uid == linked_uids[i] )
3876 linked_segments.push_back( p_segment );
3878 AppendUID( p_segment->prev_segment_uid );
3879 AppendUID( p_segment->next_segment_uid );
3887 void virtual_segment_c::PreloadLinked( )
3889 for ( size_t i=0; i<linked_segments.size(); i++ )
3891 linked_segments[i]->Preload( );
3893 i_current_edition = linked_segments[0]->i_default_edition;
3896 mtime_t virtual_segment_c::Duration() const
3899 if ( linked_segments.size() == 0 )
3902 matroska_segment_c *p_last_segment = linked_segments[linked_segments.size()-1];
3903 // p_last_segment->ParseCluster( );
3905 i_duration = p_last_segment->i_start_time / 1000 + p_last_segment->i_duration;
3910 void virtual_segment_c::LoadCues( )
3912 for ( size_t i=0; i<linked_segments.size(); i++ )
3914 linked_segments[i]->LoadCues();
3918 void virtual_segment_c::AppendUID( const EbmlBinary & UID )
3920 if ( UID.GetBuffer() == NULL )
3923 for (size_t i=0; i<linked_uids.size(); i++)
3925 if ( UID == linked_uids[i] )
3928 linked_uids.push_back( *(KaxSegmentUID*)(&UID) );
3931 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset )
3934 int i_track_skipping;
3935 int64_t i_block_duration;
3936 int64_t i_block_ref1;
3937 int64_t i_block_ref2;
3939 int64_t i_seek_position = i_start_pos;
3940 int64_t i_seek_time = i_start_time;
3946 for( ; i_idx < i_index; i_idx++ )
3948 if( index[i_idx].i_time + i_time_offset > i_date )
3959 i_seek_position = index[i_idx].i_position;
3960 i_seek_time = index[i_idx].i_time;
3963 msg_Dbg( &sys.demuxer, "seek got "I64Fd" (%d%%)",
3964 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
3966 es.I_O().setFilePointer( i_seek_position, seek_beginning );
3969 ep = new EbmlParser( &es, segment, &sys.demuxer );
3972 sys.i_start_pts = i_date;
3974 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
3976 /* now parse until key frame */
3977 #define tk tracks[i_track]
3978 i_track_skipping = 0;
3979 for( i_track = 0; i_track < tracks.size(); i_track++ )
3981 if( tk->fmt.i_cat == VIDEO_ES )
3983 tk->b_search_keyframe = VLC_TRUE;
3986 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_date );
3990 while( i_track_skipping > 0 )
3992 if( BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
3994 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
3999 for( i_track = 0; i_track < tracks.size(); i_track++ )
4001 if( tk->i_number == block->TrackNum() )
4007 sys.i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
4009 if( i_track < tracks.size() )
4011 if( sys.i_pts >= sys.i_start_pts )
4013 BlockDecode( &sys.demuxer, block, sys.i_pts, 0 );
4014 i_track_skipping = 0;
4016 else if( tk->fmt.i_cat == VIDEO_ES )
4018 if( i_block_ref1 == -1 && tk->b_search_keyframe )
4020 tk->b_search_keyframe = VLC_FALSE;
4023 if( !tk->b_search_keyframe )
4025 BlockDecode( &sys.demuxer, block, sys.i_pts, 0 );
4035 void virtual_segment_c::Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter )
4037 demux_sys_t *p_sys = demuxer.p_sys;
4040 // find the actual time for an ordered edition
4041 if ( psz_chapter == NULL )
4043 if ( EditionIsOrdered() )
4045 /* 1st, we need to know in which chapter we are */
4046 psz_chapter = (*p_editions)[i_current_edition]->FindTimecode( i_date );
4050 if ( psz_chapter != NULL )
4052 psz_current_chapter = psz_chapter;
4053 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
4054 demuxer.info.i_update |= INPUT_UPDATE_SEEKPOINT;
4055 demuxer.info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
4058 // find the best matching segment
4059 for ( i=0; i<linked_segments.size(); i++ )
4061 if ( i_date < linked_segments[i]->i_start_time )
4068 if ( i_current_segment != i )
4070 linked_segments[i_current_segment]->UnSelect();
4071 linked_segments[i]->Select( i_date );
4072 i_current_segment = i;
4075 linked_segments[i]->Seek( i_date, i_time_offset );
4078 void chapter_codec_cmds_c::AddCommand( const KaxChapterProcessCommand & command )
4082 uint32 codec_time = uint32(-1);
4083 for( i = 0; i < command.ListSize(); i++ )
4085 const EbmlElement *k = command[i];
4087 if( MKV_IS_ID( k, KaxChapterProcessTime ) )
4089 codec_time = uint32( *static_cast<const KaxChapterProcessTime*>( k ) );
4094 for( i = 0; i < command.ListSize(); i++ )
4096 const EbmlElement *k = command[i];
4098 if( MKV_IS_ID( k, KaxChapterProcessData ) )
4100 KaxChapterProcessData *p_data = new KaxChapterProcessData( *static_cast<const KaxChapterProcessData*>( k ) );
4101 switch ( codec_time )
4104 during_cmds.push_back( *p_data );
4107 enter_cmds.push_back( *p_data );
4110 leave_cmds.push_back( *p_data );
4119 bool chapter_item_c::Enter()
4121 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
4122 while ( index != codecs.end() )
4130 bool chapter_item_c::Leave()
4132 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
4133 while ( index != codecs.end() )
4141 bool dvd_chapter_codec_c::Enter()
4143 std::vector<KaxChapterProcessData>::iterator index = enter_cmds.begin();
4144 while ( index != enter_cmds.end() )
4146 if ( (*index).GetSize() )
4148 binary *p_data = (*index).GetBuffer();
4149 size_t i_size = *p_data++;
4150 // avoid reading too much from the buffer
4151 i_size = min( i_size, ((*index).GetSize() - 1) >> 3 );
4152 for ( ; i_size > 0; i_size--, p_data += 8 )
4154 interpretor.Interpret( p_data );
4162 bool dvd_chapter_codec_c::Leave()
4164 std::vector<KaxChapterProcessData>::iterator index = leave_cmds.begin();
4165 while ( index != leave_cmds.end() )
4167 if ( (*index).GetSize() )
4169 binary *p_data = (*index).GetBuffer();
4170 size_t i_size = *p_data++;
4171 // avoid reading too much from the buffer
4172 i_size = min( i_size, ((*index).GetSize() - 1) >> 3 );
4173 for ( ; i_size > 0; i_size--, p_data += 8 )
4175 interpretor.Interpret( p_data );
4183 bool dvd_command_interpretor_c::Interpret( const binary * p_command, size_t i_size )
4188 uint16 i_command = ( p_command[0] << 8 ) + p_command[1];
4190 switch ( i_command )
4194 uint8 i_title = p_command[5];
4195 // find in the ChapProcessPrivate matching this Title level
4196 matroska_segment_c *p_segment;
4197 chapter_item_c *p_chapter;
4198 p_chapter = sys.BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title), p_segment );
4199 // TODO if the segment is not part of the current segment, select the new one
4200 // TODO jump to the location in the found segment
4208 bool dvd_command_interpretor_c::MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
4210 if ( i_cookie_size != 1 || data.m_private_data.GetSize() < 4 )
4213 if ( data.m_private_data.GetBuffer()[0] != 0x28 )
4216 uint16 i_gtitle = (data.m_private_data.GetBuffer()[1] << 8 ) + data.m_private_data.GetBuffer()[2];
4217 uint8 i_title = *(uint8*)p_cookie;
4219 return (i_gtitle == i_title);