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;
426 static const uint16 CMD_CALLSS_VTSM = 0x3008;
428 // callbacks when browsing inside CodecPrivate
429 static bool MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
432 class dvd_chapter_codec_c : public chapter_codec_cmds_c
435 dvd_chapter_codec_c( demux_sys_t & sys )
436 :chapter_codec_cmds_c( 1 )
444 dvd_command_interpretor_c interpretor;
447 class matroska_script_codec_c : public chapter_codec_cmds_c
451 class chapter_translation_c
454 KaxChapterTranslateID translated;
455 unsigned int codec_id;
456 std::vector<uint64_t> editions;
465 ,i_user_start_time(-1)
468 ,b_display_seekpoint(true)
472 virtual ~chapter_item_c()
474 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
475 while ( index != codecs.end() )
480 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
481 while ( index_ != sub_chapters.end() )
488 int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time );
489 void PublishChapters( input_title_t & title, int i_level );
490 chapter_item_c * FindTimecode( mtime_t i_timecode );
491 void Append( const chapter_item_c & edition );
492 chapter_item_c * FindChapter( const chapter_item_c & chapter );
493 virtual chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
494 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
495 const void *p_cookie,
496 size_t i_cookie_size );
498 int64_t i_start_time, i_end_time;
499 int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */
500 std::vector<chapter_item_c*> sub_chapters;
503 bool b_display_seekpoint;
504 std::string psz_name;
505 chapter_item_c *psz_parent;
507 std::vector<chapter_codec_cmds_c*> codecs;
509 bool operator<( const chapter_item_c & item ) const
511 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) );
518 class chapter_edition_c : public chapter_item_c
525 void RefreshChapters( );
526 mtime_t Duration() const;
527 void PublishChapters( input_title_t & title );
532 class matroska_segment_c
535 matroska_segment_c( demux_sys_t & demuxer, EbmlStream & estream )
538 ,i_timescale(MKVD_TIMECODESCALE)
542 ,i_chapters_position(-1)
549 ,psz_muxing_application(NULL)
550 ,psz_writing_application(NULL)
551 ,psz_segment_filename(NULL)
554 ,i_default_edition(0)
559 index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
562 virtual ~matroska_segment_c()
564 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
566 #define tk tracks[i_track]
567 if( tk->fmt.psz_description )
569 free( tk->fmt.psz_description );
573 free( tk->psz_codec );
575 if( tk->fmt.psz_language )
577 free( tk->fmt.psz_language );
583 if( psz_writing_application )
585 free( psz_writing_application );
587 if( psz_muxing_application )
589 free( psz_muxing_application );
591 if( psz_segment_filename )
593 free( psz_segment_filename );
601 free( psz_date_utc );
608 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
609 while ( index != stored_editions.end() )
620 uint64_t i_timescale;
622 /* duration of the segment */
624 mtime_t i_start_time;
627 std::vector<mkv_track_t*> tracks;
630 int64_t i_cues_position;
631 int64_t i_chapters_position;
632 int64_t i_tags_position;
636 KaxSegmentUID segment_uid;
637 KaxPrevUID prev_segment_uid;
638 KaxNextUID next_segment_uid;
646 char *psz_muxing_application;
647 char *psz_writing_application;
648 char *psz_segment_filename;
652 /* !!!!! GCC 3.3 bug on Darwin !!!!! */
653 /* when you remove this variable the compiler issues an atomicity error */
654 /* this variable only works when using std::vector<chapter_edition_c> */
655 std::vector<chapter_edition_c*> stored_editions;
656 int i_default_edition;
658 std::vector<chapter_translation_c> translations;
659 std::vector<KaxSegmentFamily> families;
666 bool PreloadFamily( const matroska_segment_c & segment );
667 void ParseInfo( KaxInfo *info );
668 void ParseChapters( KaxChapters *chapters );
669 void ParseSeekHead( KaxSeekHead *seekhead );
670 void ParseTracks( KaxTracks *tracks );
671 void ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters );
672 void ParseTrackEntry( KaxTrackEntry *m );
673 void ParseCluster( );
674 void IndexAppendCluster( KaxCluster *cluster );
677 void InformationCreate( );
678 void Seek( mtime_t i_date, mtime_t i_time_offset );
679 int BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration );
680 bool Select( mtime_t i_start_time );
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 bool FindUID( KaxSegmentUID & uid ) const
745 for ( size_t i=0; i<linked_uids.size(); i++ )
747 if ( linked_uids[i] == uid )
753 void UpdateCurrentToChapter( demux_t & demux );
754 void PrepareChapters( );
755 bool Select( input_title_t & title );
757 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
758 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
759 const void *p_cookie,
760 size_t i_cookie_size );
762 std::vector<matroska_segment_c*> linked_segments;
763 std::vector<KaxSegmentUID> linked_uids;
764 size_t i_current_segment;
766 std::vector<chapter_edition_c*> *p_editions;
767 int i_current_edition;
768 chapter_item_c *psz_current_chapter;
770 void AppendUID( const EbmlBinary & UID );
773 class matroska_stream_c
776 matroska_stream_c( demux_sys_t & demuxer )
782 virtual ~matroska_stream_c()
791 std::vector<matroska_segment_c*> segments;
799 demux_sys_t( demux_t & demux )
806 ,p_current_segment(NULL)
810 virtual ~demux_sys_t()
813 for ( i=0; i<streams.size(); i++ )
815 for ( i=0; i<opened_segments.size(); i++ )
816 delete opened_segments[i];
817 for ( i=0; i<used_segments.size(); i++ )
818 delete used_segments[i];
826 mtime_t i_chapter_time;
830 input_title_t *title;
832 std::vector<matroska_stream_c*> streams;
833 std::vector<matroska_segment_c*> opened_segments;
834 std::vector<virtual_segment_c*> used_segments;
835 virtual_segment_c *p_current_segment;
837 /* duration of the stream */
840 matroska_segment_c *FindSegment( const EbmlBinary & uid ) const;
841 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
842 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
843 const void *p_cookie,
844 size_t i_cookie_size,
845 virtual_segment_c * & p_segment_found );
847 void PreloadFamily( const matroska_segment_c & of_segment );
848 void PreloadLinked( matroska_segment_c *p_segment );
849 bool PreparePlayback( );
850 matroska_stream_c *AnalyseAllSegmentsFound( EbmlStream *p_estream );
853 virtual_segment_c *VirtualFromSegments( matroska_segment_c *p_segment ) const;
854 bool IsUsedSegment( matroska_segment_c &p_segment ) const;
857 static int Demux ( demux_t * );
858 static int Control( demux_t *, int, va_list );
859 static void Seek ( demux_t *, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter );
861 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
863 static char *UTF8ToStr ( const UTFstring &u );
865 /*****************************************************************************
866 * Open: initializes matroska demux structures
867 *****************************************************************************/
868 static int Open( vlc_object_t * p_this )
870 demux_t *p_demux = (demux_t*)p_this;
872 matroska_stream_c *p_stream;
873 matroska_segment_c *p_segment;
875 std::string s_path, s_filename;
876 vlc_stream_io_callback *p_io_callback;
877 EbmlStream *p_io_stream;
879 /* peek the begining */
880 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
882 /* is a valid file */
883 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
884 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
886 /* Set the demux function */
887 p_demux->pf_demux = Demux;
888 p_demux->pf_control = Control;
889 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
891 p_io_callback = new vlc_stream_io_callback( p_demux->s );
892 p_io_stream = new EbmlStream( *p_io_callback );
894 if( p_io_stream == NULL )
896 msg_Err( p_demux, "failed to create EbmlStream" );
897 delete p_io_callback;
902 p_stream = p_sys->AnalyseAllSegmentsFound( p_io_stream );
903 if( p_stream == NULL )
905 msg_Err( p_demux, "cannot find KaxSegment" );
908 p_sys->streams.push_back( p_stream );
910 p_stream->p_in = p_io_callback;
911 p_stream->p_es = p_io_stream;
913 for (size_t i=0; i<p_stream->segments.size(); i++)
915 p_stream->segments[i]->Preload();
918 p_segment = p_stream->segments[0];
919 if( p_segment->cluster != NULL )
921 msg_Warn( p_demux, "cannot find any cluster, damaged file ?" );
923 // reset the stream reading to the first cluster of the segment used
924 p_stream->p_in->setFilePointer( p_segment->cluster->GetElementPosition() );
927 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
928 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
930 // assume it's a regular file
931 // get the directory path
932 s_path = p_demux->psz_path;
933 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
935 s_path = s_path.substr(0,s_path.length()-1);
939 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
941 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
945 struct dirent *p_file_item;
946 DIR *p_src_dir = opendir(s_path.c_str());
948 if (p_src_dir != NULL)
950 while ((p_file_item = (dirent *) readdir(p_src_dir)))
952 if (strlen(p_file_item->d_name) > 4)
954 s_filename = s_path + DIRECTORY_SEPARATOR + p_file_item->d_name;
956 if (!s_filename.compare(p_demux->psz_path))
957 continue; // don't reuse the original opened file
959 #if defined(__GNUC__) && (__GNUC__ < 3)
960 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
961 !s_filename.compare("mka", s_filename.length() - 3, 3))
963 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
964 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
967 // test wether this file belongs to the our family
968 StdIOCallback *p_file_io = new StdIOCallback(s_filename.c_str(), MODE_READ);
969 EbmlStream *p_estream = new EbmlStream(*p_file_io);
971 p_stream = p_sys->AnalyseAllSegmentsFound( p_estream );
972 if ( p_stream == NULL )
974 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
980 p_stream->p_in = p_file_io;
981 p_stream->p_es = p_estream;
982 p_sys->streams.push_back( p_stream );
987 closedir( p_src_dir );
991 p_sys->PreloadFamily( *p_segment );
992 p_sys->PreloadLinked( p_segment );
993 if ( !p_sys->PreparePlayback( ) )
995 msg_Err( p_demux, "cannot use the segment" );
1003 return VLC_EGENERIC;
1006 /*****************************************************************************
1007 * Close: frees unused data
1008 *****************************************************************************/
1009 static void Close( vlc_object_t *p_this )
1011 demux_t *p_demux = (demux_t*)p_this;
1012 demux_sys_t *p_sys = p_demux->p_sys;
1017 /*****************************************************************************
1019 *****************************************************************************/
1020 static int Control( demux_t *p_demux, int i_query, va_list args )
1022 demux_sys_t *p_sys = p_demux->p_sys;
1027 vlc_meta_t **pp_meta;
1031 case DEMUX_GET_META:
1032 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1033 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1036 case DEMUX_GET_LENGTH:
1037 pi64 = (int64_t*)va_arg( args, int64_t * );
1038 if( p_sys->f_duration > 0.0 )
1040 *pi64 = (int64_t)(p_sys->f_duration * 1000);
1043 return VLC_EGENERIC;
1045 case DEMUX_GET_POSITION:
1046 pf = (double*)va_arg( args, double * );
1047 if ( p_sys->f_duration > 0.0 )
1048 *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);
1051 case DEMUX_SET_POSITION:
1052 f = (double)va_arg( args, double );
1053 Seek( p_demux, -1, f, NULL );
1056 case DEMUX_GET_TIME:
1057 pi64 = (int64_t*)va_arg( args, int64_t * );
1058 *pi64 = p_sys->i_pts;
1061 case DEMUX_GET_TITLE_INFO:
1062 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
1064 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1065 int *pi_int = (int*)va_arg( args, int* );
1068 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) );
1070 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->title );
1074 return VLC_EGENERIC;
1076 case DEMUX_SET_TITLE:
1077 /* TODO handle editions as titles & DVD titles as well */
1078 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
1082 return VLC_EGENERIC;
1084 case DEMUX_SET_SEEKPOINT:
1085 i_skp = (int)va_arg( args, int );
1087 if( p_sys->title && i_skp < p_sys->title->i_seekpoint)
1089 Seek( p_demux, (int64_t)p_sys->title->seekpoint[i_skp]->i_time_offset, -1, NULL);
1090 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
1091 p_demux->info.i_seekpoint = i_skp;
1094 return VLC_EGENERIC;
1096 case DEMUX_SET_TIME:
1099 return VLC_EGENERIC;
1103 int matroska_segment_c::BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1115 i_level = ep->GetLevel();
1117 if( el == NULL && *pp_block != NULL )
1119 /* update the index */
1120 #define idx index[i_index - 1]
1121 if( i_index > 0 && idx.i_time == -1 )
1123 idx.i_time = (*pp_block)->GlobalTimecode() / (mtime_t)1000;
1124 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1132 if( ep->GetLevel() > 1 )
1137 msg_Warn( &sys.demuxer, "EOF" );
1138 return VLC_EGENERIC;
1144 if( MKV_IS_ID( el, KaxCluster ) )
1146 cluster = (KaxCluster*)el;
1148 /* add it to the index */
1150 ( i_index > 0 && index[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1152 IndexAppendCluster( cluster );
1155 // reset silent tracks
1156 for (size_t i=0; i<tracks.size(); i++)
1158 tracks[i]->b_silent = VLC_FALSE;
1163 else if( MKV_IS_ID( el, KaxCues ) )
1165 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1166 return VLC_EGENERIC;
1170 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1173 else if( i_level == 2 )
1175 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1177 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1179 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1180 cluster->InitTimecode( uint64( ctc ), i_timescale );
1182 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1186 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1191 else if( i_level == 3 )
1193 if( MKV_IS_ID( el, KaxBlock ) )
1195 *pp_block = (KaxBlock*)el;
1197 (*pp_block)->ReadData( es.I_O() );
1198 (*pp_block)->SetParent( *cluster );
1202 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1204 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1206 dur.ReadData( es.I_O() );
1207 *pi_duration = uint64( dur );
1209 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1211 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1213 ref.ReadData( es.I_O() );
1214 if( *pi_ref1 == -1 )
1216 *pi_ref1 = int64( ref );
1220 *pi_ref2 = int64( ref );
1223 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1225 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1226 track_num.ReadData( es.I_O() );
1228 for (size_t i=0; i<tracks.size(); i++)
1230 if ( tracks[i]->i_number == uint32(track_num))
1232 tracks[i]->b_silent = VLC_TRUE;
1240 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1241 return VLC_EGENERIC;
1246 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1249 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1250 memcpy( p_block->p_buffer, p_mem, i_mem );
1251 //p_block->i_rate = p_input->stream.control.i_rate;
1255 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1256 mtime_t i_duration )
1258 demux_sys_t *p_sys = p_demux->p_sys;
1259 matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
1265 #define tk p_segment->tracks[i_track]
1266 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1268 if( tk->i_number == block->TrackNum() )
1274 if( i_track >= p_segment->tracks.size() )
1276 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1279 if( tk->p_es == NULL )
1281 msg_Err( p_demux, "unknown track number=%d", block->TrackNum() );
1284 if( i_pts < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1286 return; /* discard audio packets that shouldn't be rendered */
1289 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1292 tk->b_inited = VLC_FALSE;
1296 /* First send init data */
1297 if( !tk->b_inited && tk->i_data_init > 0 )
1301 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
1302 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init );
1303 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
1305 tk->b_inited = VLC_TRUE;
1308 for( i = 0; i < block->NumberFrames(); i++ )
1311 DataBuffer &data = block->GetBuffer(i);
1313 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1315 if( p_block == NULL )
1320 #if defined(HAVE_ZLIB_H)
1321 if( tk->i_compression_type )
1323 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
1327 // TODO implement correct timestamping when B frames are used
1328 if( tk->fmt.i_cat != VIDEO_ES )
1330 p_block->i_dts = p_block->i_pts = i_pts;
1334 p_block->i_dts = i_pts;
1338 if( tk->fmt.i_cat == SPU_ES && strcmp( tk->psz_codec, "S_VOBSUB" ) )
1340 p_block->i_length = i_duration * 1000;
1343 es_out_Send( p_demux->out, tk->p_es, p_block );
1345 /* use time stamp only for first block */
1352 matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( EbmlStream *p_estream )
1354 int i_upper_lvl = 0;
1356 EbmlElement *p_l0, *p_l1, *p_l2;
1357 bool b_keep_stream = false, b_keep_segment;
1359 // verify the EBML Header
1360 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
1365 p_l0->SkipData(*p_estream, EbmlHead_Context);
1368 // find all segments in this file
1369 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1375 matroska_stream_c *p_stream1 = new matroska_stream_c( *this );
1379 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
1382 matroska_segment_c *p_segment1 = new matroska_segment_c( *this, *p_estream );
1383 b_keep_segment = false;
1385 ep = new EbmlParser(p_estream, p_l0, &demuxer );
1386 p_segment1->ep = ep;
1387 p_segment1->segment = (KaxSegment*)p_l0;
1389 while ((p_l1 = ep->Get()))
1391 if (MKV_IS_ID(p_l1, KaxInfo))
1393 // find the families of this segment
1394 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
1396 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
1397 for( i = 0; i < p_info->ListSize(); i++ )
1399 EbmlElement *l = (*p_info)[i];
1401 if( MKV_IS_ID( l, KaxSegmentUID ) )
1403 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
1404 b_keep_segment = (FindSegment( *p_uid ) == NULL);
1405 if ( !b_keep_segment )
1406 break; // this segment is already known
1407 opened_segments.push_back( p_segment1 );
1408 p_segment1->segment_uid = *( new KaxSegmentUID(*p_uid) );
1410 else if( MKV_IS_ID( l, KaxPrevUID ) )
1412 p_segment1->prev_segment_uid = *( new KaxPrevUID( *static_cast<KaxPrevUID*>(l) ) );
1414 else if( MKV_IS_ID( l, KaxNextUID ) )
1416 p_segment1->next_segment_uid = *( new KaxNextUID( *static_cast<KaxNextUID*>(l) ) );
1418 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
1420 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
1421 p_segment1->families.push_back( *p_fam );
1427 if ( b_keep_segment )
1429 b_keep_stream = true;
1430 p_stream1->segments.push_back( p_segment1 );
1436 p_l0->SkipData(*p_estream, EbmlHead_Context);
1437 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1440 if ( !b_keep_stream )
1449 bool matroska_segment_c::Select( mtime_t i_start_time )
1454 msg_Dbg( &sys.demuxer, "found %d es", tracks.size() );
1455 for( i_track = 0; i_track < tracks.size(); i_track++ )
1457 #define tk tracks[i_track]
1458 if( tk->fmt.i_cat == UNKNOWN_ES )
1460 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", i_track, tk->i_number );
1465 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
1467 if( tk->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
1469 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
1470 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1474 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk->p_extra_data;
1476 tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1477 tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1478 tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1480 tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
1481 if( tk->fmt.i_extra > 0 )
1483 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1484 memcpy( tk->fmt.p_extra, &p_bih[1], tk->fmt.i_extra );
1488 else if( !strcmp( tk->psz_codec, "V_MPEG1" ) ||
1489 !strcmp( tk->psz_codec, "V_MPEG2" ) )
1491 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1493 else if( !strncmp( tk->psz_codec, "V_MPEG4", 7 ) )
1495 if( !strcmp( tk->psz_codec, "V_MPEG4/MS/V3" ) )
1497 tk->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1499 else if( !strcmp( tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1501 tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1502 tk->fmt.b_packetized = VLC_FALSE;
1503 tk->fmt.i_extra = tk->i_extra_data;
1504 tk->fmt.p_extra = malloc( tk->i_extra_data );
1505 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
1509 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1512 else if( !strcmp( tk->psz_codec, "V_QUICKTIME" ) )
1514 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
1516 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1520 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1525 MP4_ReadBoxCommon( p_mp4_stream, p_box );
1526 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
1527 tk->fmt.i_codec = p_box->i_type;
1528 tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1529 tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1530 tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1531 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1532 memcpy( tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tk->fmt.i_extra );
1533 MP4_FreeBox_sample_vide( p_box );
1535 stream_MemoryDelete( p_mp4_stream, VLC_TRUE );
1537 stream_Delete( p_mp4_stream );
1540 else if( !strcmp( tk->psz_codec, "A_MS/ACM" ) )
1542 if( tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1544 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1545 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1549 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk->p_extra_data;
1551 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk->fmt.i_codec, NULL );
1553 tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1554 tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1555 tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1556 tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1557 tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1559 tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1560 if( tk->fmt.i_extra > 0 )
1562 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1563 memcpy( tk->fmt.p_extra, &p_wf[1], tk->fmt.i_extra );
1567 else if( !strcmp( tk->psz_codec, "A_MPEG/L3" ) ||
1568 !strcmp( tk->psz_codec, "A_MPEG/L2" ) ||
1569 !strcmp( tk->psz_codec, "A_MPEG/L1" ) )
1571 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1573 else if( !strcmp( tk->psz_codec, "A_AC3" ) )
1575 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1577 else if( !strcmp( tk->psz_codec, "A_DTS" ) )
1579 tk->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
1581 else if( !strcmp( tk->psz_codec, "A_FLAC" ) )
1583 tk->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
1584 tk->fmt.i_extra = tk->i_extra_data;
1585 tk->fmt.p_extra = malloc( tk->i_extra_data );
1586 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
1588 else if( !strcmp( tk->psz_codec, "A_VORBIS" ) )
1590 int i, i_offset = 1, i_size[3], i_extra;
1593 tk->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
1595 /* Split the 3 headers */
1596 if( tk->p_extra_data[0] != 0x02 )
1597 msg_Err( &sys.demuxer, "invalid vorbis header" );
1599 for( i = 0; i < 2; i++ )
1602 while( i_offset < tk->i_extra_data )
1604 i_size[i] += tk->p_extra_data[i_offset];
1605 if( tk->p_extra_data[i_offset++] != 0xff ) break;
1609 i_size[0] = __MIN(i_size[0], tk->i_extra_data - i_offset);
1610 i_size[1] = __MIN(i_size[1], tk->i_extra_data -i_offset -i_size[0]);
1611 i_size[2] = tk->i_extra_data - i_offset - i_size[0] - i_size[1];
1613 tk->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
1614 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1615 p_extra = (uint8_t *)tk->fmt.p_extra; i_extra = 0;
1616 for( i = 0; i < 3; i++ )
1618 *(p_extra++) = i_size[i] >> 8;
1619 *(p_extra++) = i_size[i] & 0xFF;
1620 memcpy( p_extra, tk->p_extra_data + i_offset + i_extra,
1622 p_extra += i_size[i];
1623 i_extra += i_size[i];
1626 else if( !strncmp( tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1627 !strncmp( tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1629 int i_profile, i_srate;
1630 static unsigned int i_sample_rates[] =
1632 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1633 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1636 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1637 /* create data for faad (MP4DecSpecificDescrTag)*/
1639 if( !strcmp( &tk->psz_codec[12], "MAIN" ) )
1643 else if( !strcmp( &tk->psz_codec[12], "LC" ) )
1647 else if( !strcmp( &tk->psz_codec[12], "SSR" ) )
1656 for( i_srate = 0; i_srate < 13; i_srate++ )
1658 if( i_sample_rates[i_srate] == tk->fmt.audio.i_rate )
1663 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1665 tk->fmt.i_extra = 2;
1666 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1667 ((uint8_t*)tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1668 ((uint8_t*)tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk->fmt.audio.i_channels << 3);
1670 else if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) ||
1671 !strcmp( tk->psz_codec, "A_PCM/INT/LIT" ) ||
1672 !strcmp( tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1674 if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) )
1676 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1680 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1682 tk->fmt.audio.i_blockalign = ( tk->fmt.audio.i_bitspersample + 7 ) / 8 * tk->fmt.audio.i_channels;
1684 else if( !strcmp( tk->psz_codec, "A_TTA1" ) )
1686 /* FIXME: support this codec */
1687 msg_Err( &sys.demuxer, "TTA not supported yet[%d, n=%d]", i_track, tk->i_number );
1688 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1690 else if( !strcmp( tk->psz_codec, "A_WAVPACK4" ) )
1692 /* FIXME: support this codec */
1693 msg_Err( &sys.demuxer, "Wavpack not supported yet[%d, n=%d]", i_track, tk->i_number );
1694 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1696 else if( !strcmp( tk->psz_codec, "S_TEXT/UTF8" ) )
1698 tk->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1699 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1701 else if( !strcmp( tk->psz_codec, "S_TEXT/SSA" ) ||
1702 !strcmp( tk->psz_codec, "S_TEXT/ASS" ) ||
1703 !strcmp( tk->psz_codec, "S_SSA" ) ||
1704 !strcmp( tk->psz_codec, "S_ASS" ))
1706 tk->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1707 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1709 else if( !strcmp( tk->psz_codec, "S_VOBSUB" ) )
1711 tk->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1712 if( tk->i_extra_data )
1715 char *p_buf = (char *)malloc( tk->i_extra_data + 1);
1716 memcpy( p_buf, tk->p_extra_data , tk->i_extra_data );
1717 p_buf[tk->i_extra_data] = '\0';
1719 p_start = strstr( p_buf, "size:" );
1720 if( sscanf( p_start, "size: %dx%d",
1721 &tk->fmt.subs.spu.i_original_frame_width, &tk->fmt.subs.spu.i_original_frame_height ) == 2 )
1723 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 );
1727 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1732 else if( !strcmp( tk->psz_codec, "B_VOBBTN" ) )
1734 /* FIXME: support this codec */
1735 msg_Err( &sys.demuxer, "Vob Buttons not supported yet[%d, n=%d]", i_track, tk->i_number );
1736 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1740 msg_Err( &sys.demuxer, "unknow codec id=`%s'", tk->psz_codec );
1741 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1745 tk->fmt.i_priority = 1000;
1748 tk->p_es = es_out_Add( sys.demuxer.out, &tk->fmt );
1750 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_start_time );
1754 sys.i_start_pts = i_start_time;
1755 // reset the stream reading to the first cluster of the segment used
1756 es.I_O().setFilePointer( i_start_pos );
1759 ep = new EbmlParser( &es, segment, &sys.demuxer );
1764 void matroska_segment_c::UnSelect( )
1768 for( i_track = 0; i_track < tracks.size(); i_track++ )
1770 #define tk tracks[i_track]
1771 if ( tk->p_es != NULL )
1773 es_out_Del( sys.demuxer.out, tk->p_es );
1782 void virtual_segment_c::PrepareChapters( )
1784 if ( linked_segments.size() == 0 )
1787 // !!! should be called only once !!!
1788 matroska_segment_c *p_segment;
1791 // copy editions from the first segment
1792 p_segment = linked_segments[0];
1793 p_editions = &p_segment->stored_editions;
1795 for ( i=1 ; i<linked_segments.size(); i++ )
1797 p_segment = linked_segments[i];
1798 // FIXME assume we have the same editions in all segments
1799 for (j=0; j<p_segment->stored_editions.size(); j++)
1800 (*p_editions)[j]->Append( *p_segment->stored_editions[j] );
1804 bool virtual_segment_c::Select( input_title_t & title )
1806 if ( Edition() != NULL )
1807 Edition()->PublishChapters( title );
1812 void chapter_edition_c::PublishChapters( input_title_t & title )
1814 if ( title.seekpoint != NULL )
1815 free( title.seekpoint );
1817 title.i_seekpoint = 0;
1818 title.seekpoint = NULL;
1820 chapter_item_c::PublishChapters( title, 0 );
1823 void chapter_item_c::PublishChapters( input_title_t & title, int i_level )
1825 if (b_display_seekpoint)
1827 seekpoint_t *sk = vlc_seekpoint_New();
1829 sk->i_level = i_level;
1830 sk->i_time_offset = i_start_time;
1831 sk->psz_name = strdup( psz_name.c_str() );
1833 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
1834 title.i_seekpoint++;
1835 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
1836 title.seekpoint[title.i_seekpoint-1] = sk;
1838 i_seekpoint_num = title.i_seekpoint;
1841 for ( size_t i=0; i<sub_chapters.size() ; i++)
1843 sub_chapters[i]->PublishChapters( title, i_level+1 );
1847 void virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
1849 demux_sys_t & sys = *demux.p_sys;
1850 chapter_item_c *psz_curr_chapter;
1852 /* update current chapter/seekpoint */
1853 if ( p_editions->size() )
1855 /* 1st, we need to know in which chapter we are */
1856 psz_curr_chapter = (*p_editions)[i_current_edition]->FindTimecode( sys.i_pts );
1858 /* we have moved to a new chapter */
1859 if (psz_curr_chapter != NULL && psz_current_chapter != psz_curr_chapter)
1861 if ( psz_current_chapter != NULL )
1862 psz_current_chapter->Leave();
1864 if ( psz_curr_chapter->i_seekpoint_num > 0 )
1866 demux.info.i_update |= INPUT_UPDATE_SEEKPOINT;
1867 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
1870 if ( (*p_editions)[i_current_edition]->b_ordered )
1872 psz_curr_chapter->Enter();
1874 // only seek if necessary
1875 if ( psz_current_chapter == NULL || (psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time) )
1876 Seek( demux, sys.i_pts, 0, psz_curr_chapter );
1879 psz_current_chapter = psz_curr_chapter;
1883 chapter_item_c *virtual_segment_c::BrowseCodecPrivate( unsigned int codec_id,
1884 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1885 const void *p_cookie,
1886 size_t i_cookie_size )
1888 // FIXME don't assume it is the first edition
1889 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
1890 if ( index != p_editions->end() )
1892 chapter_item_c *p_result = (*index)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
1893 if ( p_result != NULL )
1899 chapter_item_c *chapter_item_c::BrowseCodecPrivate( unsigned int codec_id,
1900 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1901 const void *p_cookie,
1902 size_t i_cookie_size )
1905 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
1906 while ( index != codecs.end() )
1908 if ( match( **index ,p_cookie, i_cookie_size ) )
1914 chapter_item_c *p_result = NULL;
1915 std::vector<chapter_item_c*>::const_iterator index2 = sub_chapters.begin();
1916 while ( index2 != sub_chapters.end() )
1918 p_result = (*index2)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
1919 if ( p_result != NULL )
1927 void chapter_item_c::Append( const chapter_item_c & chapter )
1929 // we are appending content for the same chapter UID
1931 chapter_item_c *p_chapter;
1933 for ( i=0; i<chapter.sub_chapters.size(); i++ )
1935 p_chapter = FindChapter( *chapter.sub_chapters[i] );
1936 if ( p_chapter != NULL )
1938 p_chapter->Append( *chapter.sub_chapters[i] );
1942 sub_chapters.push_back( chapter.sub_chapters[i] );
1946 i_user_start_time = min( i_user_start_time, chapter.i_user_start_time );
1947 i_user_end_time = max( i_user_end_time, chapter.i_user_end_time );
1950 chapter_item_c * chapter_item_c::FindChapter( const chapter_item_c & chapter )
1953 for ( i=0; i<sub_chapters.size(); i++)
1955 if ( sub_chapters[i]->i_uid == chapter.i_uid )
1956 return sub_chapters[i];
1961 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter )
1963 demux_sys_t *p_sys = p_demux->p_sys;
1964 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
1965 matroska_segment_c *p_segment = p_vsegment->Segment();
1966 mtime_t i_time_offset = 0;
1970 msg_Dbg( p_demux, "seek request to "I64Fd" (%f%%)", i_date, f_percent );
1971 if( i_date < 0 && f_percent < 0 )
1973 msg_Warn( p_demux, "cannot seek nowhere !" );
1976 if( f_percent > 1.0 )
1978 msg_Warn( p_demux, "cannot seek so far !" );
1982 /* seek without index or without date */
1983 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
1985 if (p_sys->f_duration >= 0)
1987 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
1991 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
1993 msg_Dbg( p_demux, "inacurate way of seeking" );
1994 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
1996 if( p_segment->index[i_index].i_position >= i_pos)
2001 if( i_index == p_segment->i_index )
2006 i_date = p_segment->index[i_index].i_time;
2009 if( p_segment->index[i_index].i_position < i_pos )
2013 msg_Warn( p_demux, "searching for cluster, could take some time" );
2015 /* search a cluster */
2016 while( ( el = p_sys->ep->Get() ) != NULL )
2018 if( MKV_IS_ID( el, KaxCluster ) )
2020 KaxCluster *cluster = (KaxCluster*)el;
2022 /* add it to the index */
2023 p_segment->IndexAppendCluster( cluster );
2025 if( (int64_t)cluster->GetElementPosition() >= i_pos )
2027 p_sys->cluster = cluster;
2038 p_vsegment->Seek( *p_demux, i_date, i_time_offset, psz_chapter );
2041 /*****************************************************************************
2042 * Demux: reads and demuxes data packets
2043 *****************************************************************************
2044 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
2045 *****************************************************************************/
2046 static int Demux( demux_t *p_demux)
2048 demux_sys_t *p_sys = p_demux->p_sys;
2049 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
2050 matroska_segment_c *p_segmet = p_vsegment->Segment();
2051 if ( p_segmet == NULL ) return 0;
2052 int i_block_count = 0;
2055 int64_t i_block_duration;
2056 int64_t i_block_ref1;
2057 int64_t i_block_ref2;
2061 if ( p_sys->demuxer.b_die )
2064 if( p_sys->i_pts >= p_sys->i_start_pts )
2065 p_vsegment->UpdateCurrentToChapter( *p_demux );
2067 if ( p_vsegment->EditionIsOrdered() && p_vsegment->CurrentChapter() == NULL )
2069 /* nothing left to read in this ordered edition */
2070 if ( !p_vsegment->SelectNext() )
2072 p_segmet->UnSelect( );
2074 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
2076 /* switch to the next segment */
2077 p_segmet = p_vsegment->Segment();
2078 if ( !p_segmet->Select( 0 ) )
2080 msg_Err( p_demux, "Failed to select new segment" );
2087 if( p_segmet->BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
2089 if ( p_vsegment->EditionIsOrdered() )
2091 const chapter_item_c *p_chap = p_vsegment->CurrentChapter();
2092 // check if there are more chapters to read
2093 if ( p_chap != NULL )
2095 /* TODO handle successive chapters with the same user_start_time/user_end_time
2096 if ( p_chap->i_user_start_time == p_chap->i_user_start_time )
2097 p_vsegment->SelectNext();
2099 p_sys->i_pts = p_chap->i_user_end_time;
2100 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
2107 msg_Warn( p_demux, "cannot get block EOF?" );
2108 p_segmet->UnSelect( );
2110 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
2112 /* switch to the next segment */
2113 if ( !p_vsegment->SelectNext() )
2114 // no more segments in this stream
2116 p_segmet = p_vsegment->Segment();
2117 if ( !p_segmet->Select( 0 ) )
2119 msg_Err( p_demux, "Failed to select new segment" );
2126 p_sys->i_pts = p_sys->i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
2128 if( p_sys->i_pts >= p_sys->i_start_pts )
2130 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
2133 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
2138 // TODO optimize when there is need to leave or when seeking has been called
2139 if( i_block_count > 5 )
2148 /*****************************************************************************
2150 *****************************************************************************/
2151 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
2157 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
2159 if( i_size <= 0 || mb_eof )
2164 return stream_Read( s, p_buffer, i_size );
2166 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
2172 case seek_beginning:
2176 i_pos = stream_Size( s ) - i_offset;
2179 i_pos= stream_Tell( s ) + i_offset;
2183 if( i_pos < 0 || i_pos >= stream_Size( s ) )
2190 if( stream_Seek( s, i_pos ) )
2196 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
2200 uint64 vlc_stream_io_callback::getFilePointer( void )
2202 return stream_Tell( s );
2204 void vlc_stream_io_callback::close( void )
2210 /*****************************************************************************
2211 * Ebml Stream parser
2212 *****************************************************************************/
2213 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux )
2220 mi_remain_size[0] = el_start->GetSize();
2222 for( i = 1; i < 6; i++ )
2228 mb_keep = VLC_FALSE;
2229 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
2232 EbmlParser::~EbmlParser( void )
2236 for( i = 1; i < mi_level; i++ )
2242 mb_keep = VLC_FALSE;
2246 void EbmlParser::Up( void )
2248 if( mi_user_level == mi_level )
2250 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
2256 void EbmlParser::Down( void )
2262 void EbmlParser::Keep( void )
2267 int EbmlParser::GetLevel( void )
2269 return mi_user_level;
2272 void EbmlParser::Reset( demux_t *p_demux )
2274 while ( mi_level > 0)
2276 delete m_el[mi_level];
2277 m_el[mi_level] = NULL;
2280 mi_user_level = mi_level = 1;
2281 #if LIBEBML_VERSION >= 0x000704
2282 // a little faster and cleaner
2283 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
2285 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
2287 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
2290 EbmlElement *EbmlParser::Get( void )
2294 if( mi_user_level != mi_level )
2300 EbmlElement *ret = m_got;
2306 if( m_el[mi_level] )
2308 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
2311 delete m_el[mi_level];
2313 mb_keep = VLC_FALSE;
2316 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, mb_dummy, 1 );
2317 // mi_remain_size[mi_level] = m_el[mi_level]->GetSize();
2328 delete m_el[mi_level - 1];
2329 m_got = m_el[mi_level -1] = m_el[mi_level];
2330 m_el[mi_level] = NULL;
2337 else if( m_el[mi_level] == NULL )
2339 fprintf( stderr," m_el[mi_level] == NULL\n" );
2342 return m_el[mi_level];
2346 /*****************************************************************************
2348 * * LoadCues : load the cues element and update index
2350 * * LoadTags : load ... the tags element
2352 * * InformationCreate : create all information, load tags if present
2354 *****************************************************************************/
2355 void matroska_segment_c::LoadCues( )
2357 int64_t i_sav_position = es.I_O().getFilePointer();
2359 EbmlElement *el, *cues;
2361 /* *** Load the cue if found *** */
2362 if( i_cues_position < 0 )
2364 msg_Warn( &sys.demuxer, "no cues/empty cues found->seek won't be precise" );
2366 // IndexAppendCluster( cluster );
2369 vlc_bool_t b_seekable;
2371 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
2375 msg_Dbg( &sys.demuxer, "loading cues" );
2376 es.I_O().setFilePointer( i_cues_position, seek_beginning );
2377 cues = es.FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
2381 msg_Err( &sys.demuxer, "cannot load cues (broken seekhead or file)" );
2382 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2386 ep = new EbmlParser( &es, cues, &sys.demuxer );
2387 while( ( el = ep->Get() ) != NULL )
2389 if( MKV_IS_ID( el, KaxCuePoint ) )
2391 #define idx index[i_index]
2394 idx.i_block_number= -1;
2395 idx.i_position = -1;
2397 idx.b_key = VLC_TRUE;
2400 while( ( el = ep->Get() ) != NULL )
2402 if( MKV_IS_ID( el, KaxCueTime ) )
2404 KaxCueTime &ctime = *(KaxCueTime*)el;
2406 ctime.ReadData( es.I_O() );
2408 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
2410 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
2413 while( ( el = ep->Get() ) != NULL )
2415 if( MKV_IS_ID( el, KaxCueTrack ) )
2417 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
2419 ctrack.ReadData( es.I_O() );
2420 idx.i_track = uint16( ctrack );
2422 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
2424 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
2426 ccpos.ReadData( es.I_O() );
2427 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
2429 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
2431 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2433 cbnum.ReadData( es.I_O() );
2434 idx.i_block_number = uint32( cbnum );
2438 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2445 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2451 msg_Dbg( &sys.demuxer, " * added time="I64Fd" pos="I64Fd
2452 " track=%d bnum=%d", idx.i_time, idx.i_position,
2453 idx.i_track, idx.i_block_number );
2457 if( i_index >= i_index_max )
2459 i_index_max += 1024;
2460 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
2466 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2474 msg_Dbg( &sys.demuxer, "loading cues done." );
2475 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2478 void matroska_segment_c::LoadTags( )
2480 int64_t i_sav_position = es.I_O().getFilePointer();
2482 EbmlElement *el, *tags;
2484 msg_Dbg( &sys.demuxer, "loading tags" );
2485 es.I_O().setFilePointer( i_tags_position, seek_beginning );
2486 tags = es.FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2490 msg_Err( &sys.demuxer, "cannot load tags (broken seekhead or file)" );
2491 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2495 msg_Dbg( &sys.demuxer, "Tags" );
2496 ep = new EbmlParser( &es, tags, &sys.demuxer );
2497 while( ( el = ep->Get() ) != NULL )
2499 if( MKV_IS_ID( el, KaxTag ) )
2501 msg_Dbg( &sys.demuxer, "+ Tag" );
2503 while( ( el = ep->Get() ) != NULL )
2505 if( MKV_IS_ID( el, KaxTagTargets ) )
2507 msg_Dbg( &sys.demuxer, "| + Targets" );
2509 while( ( el = ep->Get() ) != NULL )
2511 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2515 else if( MKV_IS_ID( el, KaxTagGeneral ) )
2517 msg_Dbg( &sys.demuxer, "| + General" );
2519 while( ( el = ep->Get() ) != NULL )
2521 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2525 else if( MKV_IS_ID( el, KaxTagGenres ) )
2527 msg_Dbg( &sys.demuxer, "| + Genres" );
2529 while( ( el = ep->Get() ) != NULL )
2531 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2535 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
2537 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
2539 while( ( el = ep->Get() ) != NULL )
2541 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2545 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
2547 msg_Dbg( &sys.demuxer, "| + Images Specific" );
2549 while( ( el = ep->Get() ) != NULL )
2551 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2555 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
2557 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
2559 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
2561 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
2563 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
2565 msg_Dbg( &sys.demuxer, "| + Multi Date" );
2567 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
2569 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
2571 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
2573 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
2575 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
2577 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
2579 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
2581 msg_Dbg( &sys.demuxer, "| + Multi Title" );
2585 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid( *el ).name() );
2592 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
2598 msg_Dbg( &sys.demuxer, "loading tags done." );
2599 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2602 /*****************************************************************************
2604 *****************************************************************************/
2605 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
2609 int i_upper_level = 0;
2611 msg_Dbg( &sys.demuxer, "| + Seek head" );
2613 /* Master elements */
2614 seekhead->Read( es, seekhead->Generic().Context, i_upper_level, el, true );
2616 for( i = 0; i < seekhead->ListSize(); i++ )
2618 EbmlElement *l = (*seekhead)[i];
2620 if( MKV_IS_ID( l, KaxSeek ) )
2622 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
2623 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
2626 for( j = 0; j < sk->ListSize(); j++ )
2628 EbmlElement *l = (*sk)[j];
2630 if( MKV_IS_ID( l, KaxSeekID ) )
2632 KaxSeekID &sid = *(KaxSeekID*)l;
2633 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
2635 else if( MKV_IS_ID( l, KaxSeekPosition ) )
2637 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
2638 i_pos = uint64( spos );
2642 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
2648 if( id == KaxCues::ClassInfos.GlobalId )
2650 msg_Dbg( &sys.demuxer, "| | | = cues at "I64Fd, i_pos );
2651 i_cues_position = segment->GetGlobalPosition( i_pos );
2653 else if( id == KaxChapters::ClassInfos.GlobalId )
2655 msg_Dbg( &sys.demuxer, "| | | = chapters at "I64Fd, i_pos );
2656 i_chapters_position = segment->GetGlobalPosition( i_pos );
2658 else if( id == KaxTags::ClassInfos.GlobalId )
2660 msg_Dbg( &sys.demuxer, "| | | = tags at "I64Fd, i_pos );
2661 i_tags_position = segment->GetGlobalPosition( i_pos );
2667 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
2672 /*****************************************************************************
2674 *****************************************************************************/
2675 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
2681 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
2683 tk = new mkv_track_t();
2684 tracks.push_back( tk );
2686 /* Init the track */
2687 memset( tk, 0, sizeof( mkv_track_t ) );
2689 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
2690 tk->fmt.psz_language = strdup("English");
2691 tk->fmt.psz_description = NULL;
2693 tk->b_default = VLC_TRUE;
2694 tk->b_enabled = VLC_TRUE;
2695 tk->b_silent = VLC_FALSE;
2696 tk->i_number = tracks.size() - 1;
2697 tk->i_extra_data = 0;
2698 tk->p_extra_data = NULL;
2699 tk->psz_codec = NULL;
2700 tk->i_default_duration = 0;
2701 tk->f_timecodescale = 1.0;
2703 tk->b_inited = VLC_FALSE;
2704 tk->i_data_init = 0;
2705 tk->p_data_init = NULL;
2707 tk->psz_codec_name = NULL;
2708 tk->psz_codec_settings = NULL;
2709 tk->psz_codec_info_url = NULL;
2710 tk->psz_codec_download_url = NULL;
2712 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
2714 for( i = 0; i < m->ListSize(); i++ )
2716 EbmlElement *l = (*m)[i];
2718 if( MKV_IS_ID( l, KaxTrackNumber ) )
2720 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
2722 tk->i_number = uint32( tnum );
2723 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
2725 else if( MKV_IS_ID( l, KaxTrackUID ) )
2727 KaxTrackUID &tuid = *(KaxTrackUID*)l;
2729 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
2731 else if( MKV_IS_ID( l, KaxTrackType ) )
2734 KaxTrackType &ttype = *(KaxTrackType*)l;
2736 switch( uint8(ttype) )
2740 tk->fmt.i_cat = AUDIO_ES;
2744 tk->fmt.i_cat = VIDEO_ES;
2746 case track_subtitle:
2747 psz_type = "subtitle";
2748 tk->fmt.i_cat = SPU_ES;
2751 psz_type = "unknown";
2752 tk->fmt.i_cat = UNKNOWN_ES;
2756 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
2758 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
2760 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
2762 // tk->b_enabled = uint32( fenb );
2763 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
2764 // uint32( fenb ) );
2766 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
2768 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
2770 tk->b_default = uint32( fdef );
2771 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
2773 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
2775 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
2777 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
2779 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
2781 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
2783 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
2785 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
2787 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
2789 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
2791 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
2793 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
2795 tk->i_default_duration = uint64(defd);
2796 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration="I64Fd, uint64(defd) );
2798 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
2800 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
2802 tk->f_timecodescale = float( ttcs );
2803 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
2805 else if( MKV_IS_ID( l, KaxTrackName ) )
2807 KaxTrackName &tname = *(KaxTrackName*)l;
2809 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
2810 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
2812 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
2814 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
2816 tk->fmt.psz_language = strdup( string( lang ).c_str() );
2817 msg_Dbg( &sys.demuxer,
2818 "| | | + Track Language=`%s'", tk->fmt.psz_language );
2820 else if( MKV_IS_ID( l, KaxCodecID ) )
2822 KaxCodecID &codecid = *(KaxCodecID*)l;
2824 tk->psz_codec = strdup( string( codecid ).c_str() );
2825 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
2827 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
2829 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
2831 tk->i_extra_data = cpriv.GetSize();
2832 if( tk->i_extra_data > 0 )
2834 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
2835 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
2837 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
2839 else if( MKV_IS_ID( l, KaxCodecName ) )
2841 KaxCodecName &cname = *(KaxCodecName*)l;
2843 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
2844 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
2846 else if( MKV_IS_ID( l, KaxContentEncodings ) )
2848 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
2849 MkvTree( sys.demuxer, 3, "Content Encodings" );
2850 for( j = 0; j < cencs->ListSize(); j++ )
2852 EbmlElement *l2 = (*cencs)[j];
2853 if( MKV_IS_ID( l2, KaxContentEncoding ) )
2855 MkvTree( sys.demuxer, 4, "Content Encoding" );
2856 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
2857 for( k = 0; k < cenc->ListSize(); k++ )
2859 EbmlElement *l3 = (*cenc)[k];
2860 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
2862 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
2863 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
2865 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
2867 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
2868 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
2870 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
2872 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
2873 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
2875 else if( MKV_IS_ID( l3, KaxContentCompression ) )
2877 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
2878 MkvTree( sys.demuxer, 5, "Content Compression" );
2879 for( n = 0; n < compr->ListSize(); n++ )
2881 EbmlElement *l4 = (*compr)[n];
2882 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
2884 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
2885 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
2886 if( uint32( compalg ) == 0 )
2888 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
2893 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
2900 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
2907 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
2912 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
2914 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
2916 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
2917 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
2919 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
2921 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
2923 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
2924 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
2926 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
2928 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
2930 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
2931 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
2933 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
2935 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
2937 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
2939 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
2941 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
2943 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
2945 else if( MKV_IS_ID( l, KaxTrackVideo ) )
2947 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
2950 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
2953 for( j = 0; j < tkv->ListSize(); j++ )
2955 EbmlElement *l = (*tkv)[j];
2956 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
2958 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
2960 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
2962 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
2964 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
2966 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
2969 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
2971 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
2973 tk->fmt.video.i_width = uint16( vwidth );
2974 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
2976 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
2978 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
2980 tk->fmt.video.i_height = uint16( vheight );
2981 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
2983 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
2985 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
2987 tk->fmt.video.i_visible_width = uint16( vwidth );
2988 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
2990 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
2992 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
2994 tk->fmt.video.i_visible_height = uint16( vheight );
2995 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
2997 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
2999 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
3001 tk->f_fps = float( vfps );
3002 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
3004 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
3006 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
3008 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
3009 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
3011 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
3013 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
3015 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
3017 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
3019 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
3021 // msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( gamma ) );
3025 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
3028 if ( tk->fmt.video.i_visible_height && tk->fmt.video.i_visible_width )
3029 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * tk->fmt.video.i_visible_width / tk->fmt.video.i_visible_height;
3031 else if( MKV_IS_ID( l, KaxTrackAudio ) )
3033 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
3036 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
3038 for( j = 0; j < tka->ListSize(); j++ )
3040 EbmlElement *l = (*tka)[j];
3042 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
3044 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
3046 tk->fmt.audio.i_rate = (int)float( afreq );
3047 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
3049 else if( MKV_IS_ID( l, KaxAudioChannels ) )
3051 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
3053 tk->fmt.audio.i_channels = uint8( achan );
3054 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
3056 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
3058 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
3060 tk->fmt.audio.i_bitspersample = uint8( abits );
3061 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
3065 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
3071 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
3072 typeid(*l).name() );
3077 /*****************************************************************************
3079 *****************************************************************************/
3080 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
3084 int i_upper_level = 0;
3086 msg_Dbg( &sys.demuxer, "| + Tracks" );
3088 /* Master elements */
3089 tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
3091 for( i = 0; i < tracks->ListSize(); i++ )
3093 EbmlElement *l = (*tracks)[i];
3095 if( MKV_IS_ID( l, KaxTrackEntry ) )
3097 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
3101 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3106 /*****************************************************************************
3108 *****************************************************************************/
3109 void matroska_segment_c::ParseInfo( KaxInfo *info )
3114 int i_upper_level = 0;
3116 msg_Dbg( &sys.demuxer, "| + Information" );
3118 /* Master elements */
3119 m = static_cast<EbmlMaster *>(info);
3120 m->Read( es, info->Generic().Context, i_upper_level, el, true );
3122 for( i = 0; i < m->ListSize(); i++ )
3124 EbmlElement *l = (*m)[i];
3126 if( MKV_IS_ID( l, KaxSegmentUID ) )
3128 segment_uid = *(new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l)));
3130 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)segment_uid.GetBuffer() );
3132 else if( MKV_IS_ID( l, KaxPrevUID ) )
3134 prev_segment_uid = *(new KaxPrevUID(*static_cast<KaxPrevUID*>(l)));
3136 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)prev_segment_uid.GetBuffer() );
3138 else if( MKV_IS_ID( l, KaxNextUID ) )
3140 next_segment_uid = *(new KaxNextUID(*static_cast<KaxNextUID*>(l)));
3142 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)next_segment_uid.GetBuffer() );
3144 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
3146 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
3148 i_timescale = uint64(tcs);
3150 msg_Dbg( &sys.demuxer, "| | + TimecodeScale="I64Fd,
3153 else if( MKV_IS_ID( l, KaxDuration ) )
3155 KaxDuration &dur = *(KaxDuration*)l;
3157 i_duration = mtime_t( double( dur ) );
3159 msg_Dbg( &sys.demuxer, "| | + Duration="I64Fd,
3162 else if( MKV_IS_ID( l, KaxMuxingApp ) )
3164 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
3166 psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
3168 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
3169 psz_muxing_application );
3171 else if( MKV_IS_ID( l, KaxWritingApp ) )
3173 KaxWritingApp &wapp = *(KaxWritingApp*)l;
3175 psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
3177 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
3178 psz_writing_application );
3180 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
3182 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
3184 psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
3186 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
3187 psz_segment_filename );
3189 else if( MKV_IS_ID( l, KaxTitle ) )
3191 KaxTitle &title = *(KaxTitle*)l;
3193 psz_title = UTF8ToStr( UTFstring( title ) );
3195 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
3197 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
3199 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
3201 families.push_back(*uid);
3203 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
3205 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
3206 else if( MKV_IS_ID( l, KaxDateUTC ) )
3208 KaxDateUTC &date = *(KaxDateUTC*)l;
3213 i_date = date.GetEpochDate();
3214 memset( buffer, 0, 256 );
3215 if( gmtime_r( &i_date, &tmres ) &&
3216 asctime_r( &tmres, buffer ) )
3218 buffer[strlen( buffer)-1]= '\0';
3219 psz_date_utc = strdup( buffer );
3220 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
3224 #if LIBMATROSKA_VERSION >= 0x000704
3225 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
3227 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
3228 chapter_translation_c translated;
3230 p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
3231 for( j = 0; j < p_trans->ListSize(); j++ )
3233 EbmlElement *l = (*p_trans)[j];
3235 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
3237 translated.editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
3239 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
3241 translated.codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
3243 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
3245 translated.translated = *( new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) ) );
3249 translations.push_back( translated );
3254 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3258 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
3259 i_duration = mtime_t(f_dur);
3263 /*****************************************************************************
3265 *****************************************************************************/
3266 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
3270 if( sys.title == NULL )
3272 sys.title = vlc_input_title_New();
3275 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
3276 for( i = 0; i < ca->ListSize(); i++ )
3278 EbmlElement *l = (*ca)[i];
3280 if( MKV_IS_ID( l, KaxChapterUID ) )
3282 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
3283 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
3285 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
3287 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
3288 chapters.b_display_seekpoint = uint8( flag ) == 0;
3290 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
3292 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
3294 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
3295 chapters.i_start_time = uint64( start ) / I64C(1000);
3297 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
3299 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
3301 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
3302 chapters.i_end_time = uint64( end ) / I64C(1000);
3304 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
3306 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
3308 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
3310 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
3311 for( j = 0; j < cd->ListSize(); j++ )
3313 EbmlElement *l= (*cd)[j];
3315 if( MKV_IS_ID( l, KaxChapterString ) )
3319 KaxChapterString &name =*(KaxChapterString*)l;
3320 for (k = 0; k < i_level; k++)
3321 chapters.psz_name += '+';
3322 chapters.psz_name += ' ';
3323 chapters.psz_name += UTF8ToStr( UTFstring( name ) );
3325 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", UTF8ToStr(UTFstring(name)) );
3327 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
3329 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
3330 const char *psz = string( lang ).c_str();
3332 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
3334 else if( MKV_IS_ID( l, KaxChapterCountry ) )
3336 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
3337 const char *psz = string( ct ).c_str();
3339 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
3343 else if( MKV_IS_ID( l, KaxChapterProcess ) )
3345 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
3347 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
3348 chapter_codec_cmds_c *p_ccodec = NULL;
3350 for( j = 0; j < cp->ListSize(); j++ )
3352 EbmlElement *k= (*cp)[j];
3354 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
3356 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
3357 if ( uint32(*p_codec_id) == 0 )
3358 p_ccodec = new matroska_script_codec_c();
3359 else if ( uint32(*p_codec_id) == 1 )
3360 p_ccodec = new dvd_chapter_codec_c( sys );
3365 if ( p_ccodec != NULL )
3367 for( j = 0; j < cp->ListSize(); j++ )
3369 EbmlElement *k= (*cp)[j];
3371 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
3373 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
3374 p_ccodec->SetPrivate( *p_private );
3376 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
3378 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
3381 chapters.codecs.push_back( p_ccodec );
3384 else if( MKV_IS_ID( l, KaxChapterAtom ) )
3386 chapter_item_c *new_sub_chapter = new chapter_item_c();
3387 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
3388 new_sub_chapter->psz_parent = &chapters;
3389 chapters.sub_chapters.push_back( new_sub_chapter );
3394 /*****************************************************************************
3396 *****************************************************************************/
3397 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
3401 int i_upper_level = 0;
3404 /* Master elements */
3405 chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
3407 for( i = 0; i < chapters->ListSize(); i++ )
3409 EbmlElement *l = (*chapters)[i];
3411 if( MKV_IS_ID( l, KaxEditionEntry ) )
3413 chapter_edition_c *p_edition = new chapter_edition_c();
3415 EbmlMaster *E = static_cast<EbmlMaster *>(l );
3417 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
3418 for( j = 0; j < E->ListSize(); j++ )
3420 EbmlElement *l = (*E)[j];
3422 if( MKV_IS_ID( l, KaxChapterAtom ) )
3424 chapter_item_c *new_sub_chapter = new chapter_item_c();
3425 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
3426 p_edition->sub_chapters.push_back( new_sub_chapter );
3428 else if( MKV_IS_ID( l, KaxEditionUID ) )
3430 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
3432 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
3434 p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
3436 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
3438 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
3439 i_default_edition = stored_editions.size();
3443 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
3446 stored_editions.push_back( p_edition );
3450 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3454 for( i = 0; i < stored_editions.size(); i++ )
3456 stored_editions[i]->RefreshChapters( );
3459 if ( stored_editions[i_default_edition]->b_ordered )
3461 /* update the duration of the segment according to the sum of all sub chapters */
3462 i_dur = stored_editions[i_default_edition]->Duration() / I64C(1000);
3468 void matroska_segment_c::ParseCluster( )
3473 int i_upper_level = 0;
3475 /* Master elements */
3476 m = static_cast<EbmlMaster *>( cluster );
3477 m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
3479 for( i = 0; i < m->ListSize(); i++ )
3481 EbmlElement *l = (*m)[i];
3483 if( MKV_IS_ID( l, KaxClusterTimecode ) )
3485 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
3487 cluster->InitTimecode( uint64( ctc ), i_timescale );
3492 i_start_time = cluster->GlobalTimecode() / 1000;
3495 /*****************************************************************************
3496 * InformationCreate:
3497 *****************************************************************************/
3498 void matroska_segment_c::InformationCreate( )
3502 sys.meta = vlc_meta_New();
3506 vlc_meta_Add( sys.meta, VLC_META_TITLE, psz_title );
3510 vlc_meta_Add( sys.meta, VLC_META_DATE, psz_date_utc );
3512 if( psz_segment_filename )
3514 vlc_meta_Add( sys.meta, _("Segment filename"), psz_segment_filename );
3516 if( psz_muxing_application )
3518 vlc_meta_Add( sys.meta, _("Muxing application"), psz_muxing_application );
3520 if( psz_writing_application )
3522 vlc_meta_Add( sys.meta, _("Writing application"), psz_writing_application );
3525 for( i_track = 0; i_track < tracks.size(); i_track++ )
3527 mkv_track_t *tk = tracks[i_track];
3528 vlc_meta_t *mtk = vlc_meta_New();
3530 sys.meta->track = (vlc_meta_t**)realloc( sys.meta->track,
3531 sizeof( vlc_meta_t * ) * ( sys.meta->i_track + 1 ) );
3532 sys.meta->track[sys.meta->i_track++] = mtk;
3534 if( tk->fmt.psz_description )
3536 vlc_meta_Add( sys.meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
3538 if( tk->psz_codec_name )
3540 vlc_meta_Add( sys.meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
3542 if( tk->psz_codec_settings )
3544 vlc_meta_Add( sys.meta, VLC_META_SETTING, tk->psz_codec_settings );
3546 if( tk->psz_codec_info_url )
3548 vlc_meta_Add( sys.meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
3550 if( tk->psz_codec_download_url )
3552 vlc_meta_Add( sys.meta, VLC_META_URL, tk->psz_codec_download_url );
3556 if( i_tags_position >= 0 )
3558 vlc_bool_t b_seekable;
3560 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
3569 /*****************************************************************************
3571 *****************************************************************************/
3573 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
3575 #define idx index[i_index]
3577 idx.i_block_number= -1;
3578 idx.i_position = cluster->GetElementPosition();
3580 idx.b_key = VLC_TRUE;
3583 if( i_index >= i_index_max )
3585 i_index_max += 1024;
3586 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
3591 static char * UTF8ToStr( const UTFstring &u )
3600 p = dst = (char*)malloc( i_src + 1);
3619 void chapter_edition_c::RefreshChapters( )
3621 chapter_item_c::RefreshChapters( b_ordered, -1 );
3622 b_display_seekpoint = false;
3625 int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_time )
3627 int64_t i_user_time = i_prev_user_time;
3629 // first the sub-chapters, and then ourself
3630 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
3631 while ( index != sub_chapters.end() )
3633 i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
3639 // the ordered chapters always start at zero
3640 if ( i_prev_user_time == -1 )
3642 if ( i_user_time == -1 )
3644 i_prev_user_time = 0;
3647 i_user_start_time = i_prev_user_time;
3648 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
3650 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
3654 i_user_end_time = i_user_time;
3659 std::sort( sub_chapters.begin(), sub_chapters.end() );
3660 i_user_start_time = i_start_time;
3661 if ( i_end_time != -1 )
3662 i_user_end_time = i_end_time;
3663 else if ( i_user_time != -1 )
3664 i_user_end_time = i_user_time;
3666 i_user_end_time = i_user_start_time;
3669 return i_user_end_time;
3672 mtime_t chapter_edition_c::Duration() const
3674 mtime_t i_result = 0;
3676 if ( sub_chapters.size() )
3678 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
3680 i_result = (*index)->i_user_end_time;
3686 chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode )
3688 chapter_item_c *psz_result = NULL;
3690 if ( i_user_timecode >= i_user_start_time &&
3691 ( i_user_timecode < i_user_end_time ||
3692 ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
3694 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
3695 while ( index != sub_chapters.end() && psz_result == NULL )
3697 psz_result = (*index)->FindTimecode( i_user_timecode );
3701 if ( psz_result == NULL )
3708 void demux_sys_t::PreloadFamily( const matroska_segment_c & of_segment )
3710 for (size_t i=0; i<opened_segments.size(); i++)
3712 opened_segments[i]->PreloadFamily( of_segment );
3715 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
3720 for (size_t i=0; i<families.size(); i++)
3722 for (size_t j=0; j<of_segment.families.size(); j++)
3724 if ( families[i] == of_segment.families[j] )
3732 // preload all the linked segments for all preloaded segments
3733 void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
3735 size_t i_preloaded, i;
3736 virtual_segment_c *p_seg;
3738 p_current_segment = VirtualFromSegments( p_segment );
3740 used_segments.push_back( p_current_segment );
3742 // create all the other virtual segments of the family
3745 for ( i=0; i< opened_segments.size(); i++ )
3747 if ( opened_segments[i]->b_preloaded && !IsUsedSegment( *opened_segments[i] ) )
3749 p_seg = VirtualFromSegments( opened_segments[i] );
3750 used_segments.push_back( p_seg );
3754 } while ( i_preloaded ); // worst case: will stop when all segments are found as family related
3757 bool demux_sys_t::IsUsedSegment( matroska_segment_c &segment ) const
3759 for ( size_t i=0; i< used_segments.size(); i++ )
3761 if ( used_segments[i]->FindUID( segment.segment_uid ) )
3767 virtual_segment_c *demux_sys_t::VirtualFromSegments( matroska_segment_c *p_segment ) const
3769 size_t i_preloaded, i;
3771 virtual_segment_c *p_result = new virtual_segment_c( p_segment );
3773 // fill our current virtual segment with all hard linked segments
3776 for ( i=0; i< opened_segments.size(); i++ )
3778 i_preloaded += p_result->AddSegment( opened_segments[i] );
3780 } while ( i_preloaded ); // worst case: will stop when all segments are found as linked
3784 p_result->PreloadLinked( );
3786 p_result->PrepareChapters( );
3791 bool demux_sys_t::PreparePlayback( )
3793 p_current_segment->LoadCues();
3794 f_duration = p_current_segment->Duration();
3796 /* add information */
3797 p_current_segment->Segment()->InformationCreate( );
3799 p_current_segment->Segment()->Select( 0 );
3801 return p_current_segment->Select( *title );
3804 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
3806 EbmlBinary * p_itema = (EbmlBinary *)(&p_item_a->segment_uid);
3807 if ( *p_itema == p_item_b->prev_segment_uid )
3810 p_itema = (EbmlBinary *)(&p_item_a->next_segment_uid);
3811 if ( *p_itema == p_item_b->segment_uid )
3814 if ( *p_itema == p_item_b->prev_segment_uid )
3820 bool matroska_segment_c::Preload( )
3825 EbmlElement *el = NULL;
3827 ep->Reset( &sys.demuxer );
3829 while( ( el = ep->Get() ) != NULL )
3831 if( MKV_IS_ID( el, KaxInfo ) )
3833 ParseInfo( static_cast<KaxInfo*>( el ) );
3835 else if( MKV_IS_ID( el, KaxTracks ) )
3837 ParseTracks( static_cast<KaxTracks*>( el ) );
3839 else if( MKV_IS_ID( el, KaxSeekHead ) )
3841 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
3843 else if( MKV_IS_ID( el, KaxCues ) )
3845 msg_Dbg( &sys.demuxer, "| + Cues" );
3847 else if( MKV_IS_ID( el, KaxCluster ) )
3849 msg_Dbg( &sys.demuxer, "| + Cluster" );
3851 cluster = (KaxCluster*)el;
3853 i_start_pos = cluster->GetElementPosition();
3857 /* stop parsing the stream */
3860 else if( MKV_IS_ID( el, KaxAttachments ) )
3862 msg_Dbg( &sys.demuxer, "| + Attachments FIXME (but probably never supported)" );
3864 else if( MKV_IS_ID( el, KaxChapters ) )
3866 msg_Dbg( &sys.demuxer, "| + Chapters" );
3867 ParseChapters( static_cast<KaxChapters*>( el ) );
3869 else if( MKV_IS_ID( el, KaxTag ) )
3871 msg_Dbg( &sys.demuxer, "| + Tags FIXME TODO" );
3875 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid(*el).name() );
3884 matroska_segment_c *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
3886 for (size_t i=0; i<opened_segments.size(); i++)
3888 if ( opened_segments[i]->segment_uid == uid )
3889 return opened_segments[i];
3894 chapter_item_c *demux_sys_t::BrowseCodecPrivate( unsigned int codec_id,
3895 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3896 const void *p_cookie,
3897 size_t i_cookie_size,
3898 virtual_segment_c * &p_segment_found )
3900 chapter_item_c *p_result = NULL;
3901 for (size_t i=0; i<opened_segments.size(); i++)
3903 p_result = used_segments[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3904 if ( p_result != NULL )
3906 p_segment_found = used_segments[i];
3913 void virtual_segment_c::Sort()
3915 // keep the current segment index
3916 matroska_segment_c *p_segment = linked_segments[i_current_segment];
3918 std::sort( linked_segments.begin(), linked_segments.end(), matroska_segment_c::CompareSegmentUIDs );
3920 for ( i_current_segment=0; i_current_segment<linked_segments.size(); i_current_segment++)
3921 if ( linked_segments[i_current_segment] == p_segment )
3925 size_t virtual_segment_c::AddSegment( matroska_segment_c *p_segment )
3928 // check if it's not already in here
3929 for ( i=0; i<linked_segments.size(); i++ )
3931 if ( p_segment->segment_uid == linked_segments[i]->segment_uid )
3935 // find possible mates
3936 for ( i=0; i<linked_uids.size(); i++ )
3938 if ( p_segment->segment_uid == linked_uids[i]
3939 || p_segment->prev_segment_uid == linked_uids[i]
3940 || p_segment->next_segment_uid == linked_uids[i] )
3942 linked_segments.push_back( p_segment );
3944 AppendUID( p_segment->prev_segment_uid );
3945 AppendUID( p_segment->next_segment_uid );
3953 void virtual_segment_c::PreloadLinked( )
3955 for ( size_t i=0; i<linked_segments.size(); i++ )
3957 linked_segments[i]->Preload( );
3959 i_current_edition = linked_segments[0]->i_default_edition;
3962 mtime_t virtual_segment_c::Duration() const
3965 if ( linked_segments.size() == 0 )
3968 matroska_segment_c *p_last_segment = linked_segments[linked_segments.size()-1];
3969 // p_last_segment->ParseCluster( );
3971 i_duration = p_last_segment->i_start_time / 1000 + p_last_segment->i_duration;
3976 void virtual_segment_c::LoadCues( )
3978 for ( size_t i=0; i<linked_segments.size(); i++ )
3980 linked_segments[i]->LoadCues();
3984 void virtual_segment_c::AppendUID( const EbmlBinary & UID )
3986 if ( UID.GetBuffer() == NULL )
3989 for (size_t i=0; i<linked_uids.size(); i++)
3991 if ( UID == linked_uids[i] )
3994 linked_uids.push_back( *(KaxSegmentUID*)(&UID) );
3997 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset )
4000 int i_track_skipping;
4001 int64_t i_block_duration;
4002 int64_t i_block_ref1;
4003 int64_t i_block_ref2;
4005 int64_t i_seek_position = i_start_pos;
4006 int64_t i_seek_time = i_start_time;
4012 for( ; i_idx < i_index; i_idx++ )
4014 if( index[i_idx].i_time + i_time_offset > i_date )
4025 i_seek_position = index[i_idx].i_position;
4026 i_seek_time = index[i_idx].i_time;
4029 msg_Dbg( &sys.demuxer, "seek got "I64Fd" (%d%%)",
4030 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
4032 es.I_O().setFilePointer( i_seek_position, seek_beginning );
4035 ep = new EbmlParser( &es, segment, &sys.demuxer );
4038 sys.i_start_pts = i_date;
4040 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
4042 /* now parse until key frame */
4043 #define tk tracks[i_track]
4044 i_track_skipping = 0;
4045 for( i_track = 0; i_track < tracks.size(); i_track++ )
4047 if( tk->fmt.i_cat == VIDEO_ES )
4049 tk->b_search_keyframe = VLC_TRUE;
4052 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_date );
4056 while( i_track_skipping > 0 )
4058 if( BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
4060 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
4065 for( i_track = 0; i_track < tracks.size(); i_track++ )
4067 if( tk->i_number == block->TrackNum() )
4073 sys.i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
4075 if( i_track < tracks.size() )
4077 if( sys.i_pts >= sys.i_start_pts )
4079 BlockDecode( &sys.demuxer, block, sys.i_pts, 0 );
4080 i_track_skipping = 0;
4082 else if( tk->fmt.i_cat == VIDEO_ES )
4084 if( i_block_ref1 == -1 && tk->b_search_keyframe )
4086 tk->b_search_keyframe = VLC_FALSE;
4089 if( !tk->b_search_keyframe )
4091 BlockDecode( &sys.demuxer, block, sys.i_pts, 0 );
4101 void virtual_segment_c::Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter )
4103 demux_sys_t *p_sys = demuxer.p_sys;
4106 // find the actual time for an ordered edition
4107 if ( psz_chapter == NULL )
4109 if ( EditionIsOrdered() )
4111 /* 1st, we need to know in which chapter we are */
4112 psz_chapter = (*p_editions)[i_current_edition]->FindTimecode( i_date );
4116 if ( psz_chapter != NULL )
4118 psz_current_chapter = psz_chapter;
4119 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
4120 demuxer.info.i_update |= INPUT_UPDATE_SEEKPOINT;
4121 demuxer.info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
4124 // find the best matching segment
4125 for ( i=0; i<linked_segments.size(); i++ )
4127 if ( i_date < linked_segments[i]->i_start_time )
4134 if ( i_current_segment != i )
4136 linked_segments[i_current_segment]->UnSelect();
4137 linked_segments[i]->Select( i_date );
4138 i_current_segment = i;
4141 linked_segments[i]->Seek( i_date, i_time_offset );
4144 void chapter_codec_cmds_c::AddCommand( const KaxChapterProcessCommand & command )
4148 uint32 codec_time = uint32(-1);
4149 for( i = 0; i < command.ListSize(); i++ )
4151 const EbmlElement *k = command[i];
4153 if( MKV_IS_ID( k, KaxChapterProcessTime ) )
4155 codec_time = uint32( *static_cast<const KaxChapterProcessTime*>( k ) );
4160 for( i = 0; i < command.ListSize(); i++ )
4162 const EbmlElement *k = command[i];
4164 if( MKV_IS_ID( k, KaxChapterProcessData ) )
4166 KaxChapterProcessData *p_data = new KaxChapterProcessData( *static_cast<const KaxChapterProcessData*>( k ) );
4167 switch ( codec_time )
4170 during_cmds.push_back( *p_data );
4173 enter_cmds.push_back( *p_data );
4176 leave_cmds.push_back( *p_data );
4185 bool chapter_item_c::Enter()
4187 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
4188 while ( index != codecs.end() )
4193 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
4194 while ( index_ != sub_chapters.end() )
4202 bool chapter_item_c::Leave()
4204 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
4205 while ( index != codecs.end() )
4210 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
4211 while ( index_ != sub_chapters.end() )
4219 bool dvd_chapter_codec_c::Enter()
4221 std::vector<KaxChapterProcessData>::iterator index = enter_cmds.begin();
4222 while ( index != enter_cmds.end() )
4224 if ( (*index).GetSize() )
4226 binary *p_data = (*index).GetBuffer();
4227 size_t i_size = *p_data++;
4228 // avoid reading too much from the buffer
4229 i_size = min( i_size, ((*index).GetSize() - 1) >> 3 );
4230 for ( ; i_size > 0; i_size--, p_data += 8 )
4232 interpretor.Interpret( p_data );
4240 bool dvd_chapter_codec_c::Leave()
4242 std::vector<KaxChapterProcessData>::iterator index = leave_cmds.begin();
4243 while ( index != leave_cmds.end() )
4245 if ( (*index).GetSize() )
4247 binary *p_data = (*index).GetBuffer();
4248 size_t i_size = *p_data++;
4249 // avoid reading too much from the buffer
4250 i_size = min( i_size, ((*index).GetSize() - 1) >> 3 );
4251 for ( ; i_size > 0; i_size--, p_data += 8 )
4253 interpretor.Interpret( p_data );
4261 bool dvd_command_interpretor_c::Interpret( const binary * p_command, size_t i_size )
4266 uint16 i_command = ( p_command[0] << 8 ) + p_command[1];
4268 switch ( i_command )
4272 uint8 i_title = p_command[5];
4273 msg_Dbg( &sys.demuxer, "DVD command: JumpTT %d", i_title );
4275 // find in the ChapProcessPrivate matching this Title level
4276 virtual_segment_c *p_segment;
4277 chapter_item_c *p_chapter;
4278 p_chapter = sys.BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title), p_segment );
4279 if ( p_chapter != NULL )
4281 // if the segment is not part of the current segment, select the new one
4282 if ( p_segment != sys.p_current_segment )
4284 sys.p_current_segment = p_segment;
4285 sys.PreparePlayback();
4290 // jump to the location in the found segment
4291 p_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
4296 case CMD_CALLSS_VTSM:
4298 msg_Dbg( &sys.demuxer, "DVD command: CallSS VTSM" );
4303 msg_Dbg( &sys.demuxer, "DVD command: unsupported %02X %02X %02X %02X %02X %02X %02X %02X"
4319 bool dvd_command_interpretor_c::MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
4321 if ( i_cookie_size != 1 || data.m_private_data.GetSize() < 4 )
4324 if ( data.m_private_data.GetBuffer()[0] != 0x28 )
4327 uint16 i_gtitle = (data.m_private_data.GetBuffer()[1] << 8 ) + data.m_private_data.GetBuffer()[2];
4328 uint8 i_title = *(uint8*)p_cookie;
4330 return (i_gtitle == i_title);