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; }
353 virtual std::string GetCodecName() const { return ""; }
355 KaxChapterProcessPrivate m_private_data;
358 std::vector<KaxChapterProcessData> enter_cmds;
359 std::vector<KaxChapterProcessData> during_cmds;
360 std::vector<KaxChapterProcessData> leave_cmds;
365 class dvd_command_interpretor_c
368 dvd_command_interpretor_c( demux_sys_t & demuxer )
371 memset( p_GPRM, 0, sizeof(p_GPRM) );
372 memset( p_SPRM, 0, sizeof(p_SPRM) );
379 p_SPRM[ 16 ] = 0xFFFFu;
380 p_SPRM[ 18 ] = 0xFFFFu;
383 bool Interpret( const binary * p_command, size_t i_size = 8 );
386 uint16 GetGPRM( size_t index ) const
388 if ( index >= 0 && index < 16 )
389 return p_GPRM[ index ];
393 uint16 GetSPRM( size_t index ) const
395 // 21,22,23 reserved for future use
396 if ( index >= 0 && index < 21 )
397 return p_SPRM[ index ];
401 bool SetGPRM( size_t index, uint16 value )
403 if ( index >= 0 && index < 16 )
405 p_GPRM[ index ] = value;
411 bool SetSPRM( size_t index, uint16 value )
413 if ( index > 0 && index <= 13 && index != 12 )
415 p_SPRM[ index ] = value;
426 static const uint16 CMD_JUMP_TT = 0x3002;
427 static const uint16 CMD_CALLSS_VTSM = 0x3008;
429 // callbacks when browsing inside CodecPrivate
430 static bool MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
433 class dvd_chapter_codec_c : public chapter_codec_cmds_c
436 dvd_chapter_codec_c( demux_sys_t & sys )
437 :chapter_codec_cmds_c( 1 )
443 std::string GetCodecName() const;
446 dvd_command_interpretor_c interpretor;
449 class matroska_script_codec_c : public chapter_codec_cmds_c
453 class chapter_translation_c
456 KaxChapterTranslateID translated;
457 unsigned int codec_id;
458 std::vector<uint64_t> editions;
467 ,i_user_start_time(-1)
470 ,b_display_seekpoint(true)
474 virtual ~chapter_item_c()
476 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
477 while ( index != codecs.end() )
482 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
483 while ( index_ != sub_chapters.end() )
490 int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time );
491 void PublishChapters( input_title_t & title, int i_level );
492 chapter_item_c * FindTimecode( mtime_t i_timecode );
493 void Append( const chapter_item_c & edition );
494 chapter_item_c * FindChapter( const chapter_item_c & chapter );
495 virtual chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
496 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
497 const void *p_cookie,
498 size_t i_cookie_size );
499 std::string GetCodecName() const;
501 int64_t i_start_time, i_end_time;
502 int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */
503 std::vector<chapter_item_c*> sub_chapters;
506 bool b_display_seekpoint;
507 std::string psz_name;
508 chapter_item_c *psz_parent;
510 std::vector<chapter_codec_cmds_c*> codecs;
512 bool operator<( const chapter_item_c & item ) const
514 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) );
521 class chapter_edition_c : public chapter_item_c
528 void RefreshChapters( );
529 mtime_t Duration() const;
530 void PublishChapters( input_title_t & title );
535 class matroska_segment_c
538 matroska_segment_c( demux_sys_t & demuxer, EbmlStream & estream )
541 ,i_timescale(MKVD_TIMECODESCALE)
545 ,i_chapters_position(-1)
552 ,psz_muxing_application(NULL)
553 ,psz_writing_application(NULL)
554 ,psz_segment_filename(NULL)
557 ,i_default_edition(0)
562 index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
565 virtual ~matroska_segment_c()
567 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
569 #define tk tracks[i_track]
570 if( tk->fmt.psz_description )
572 free( tk->fmt.psz_description );
576 free( tk->psz_codec );
578 if( tk->fmt.psz_language )
580 free( tk->fmt.psz_language );
586 if( psz_writing_application )
588 free( psz_writing_application );
590 if( psz_muxing_application )
592 free( psz_muxing_application );
594 if( psz_segment_filename )
596 free( psz_segment_filename );
604 free( psz_date_utc );
611 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
612 while ( index != stored_editions.end() )
623 uint64_t i_timescale;
625 /* duration of the segment */
627 mtime_t i_start_time;
630 std::vector<mkv_track_t*> tracks;
633 int64_t i_cues_position;
634 int64_t i_chapters_position;
635 int64_t i_tags_position;
639 KaxSegmentUID segment_uid;
640 KaxPrevUID prev_segment_uid;
641 KaxNextUID next_segment_uid;
649 char *psz_muxing_application;
650 char *psz_writing_application;
651 char *psz_segment_filename;
655 /* !!!!! GCC 3.3 bug on Darwin !!!!! */
656 /* when you remove this variable the compiler issues an atomicity error */
657 /* this variable only works when using std::vector<chapter_edition_c> */
658 std::vector<chapter_edition_c*> stored_editions;
659 int i_default_edition;
661 std::vector<chapter_translation_c> translations;
662 std::vector<KaxSegmentFamily> families;
669 bool PreloadFamily( const matroska_segment_c & segment );
670 void ParseInfo( KaxInfo *info );
671 void ParseChapters( KaxChapters *chapters );
672 void ParseSeekHead( KaxSeekHead *seekhead );
673 void ParseTracks( KaxTracks *tracks );
674 void ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters );
675 void ParseTrackEntry( KaxTrackEntry *m );
676 void ParseCluster( );
677 void IndexAppendCluster( KaxCluster *cluster );
680 void InformationCreate( );
681 void Seek( mtime_t i_date, mtime_t i_time_offset );
682 int BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration );
683 bool Select( mtime_t i_start_time );
686 static bool CompareSegmentUIDs( const matroska_segment_c * item_a, const matroska_segment_c * item_b );
689 // class holding hard-linked segment together in the playback order
690 class virtual_segment_c
693 virtual_segment_c( matroska_segment_c *p_segment )
694 :i_current_segment(0)
696 ,i_current_edition(-1)
697 ,psz_current_chapter(NULL)
699 linked_segments.push_back( p_segment );
701 AppendUID( p_segment->segment_uid );
702 AppendUID( p_segment->prev_segment_uid );
703 AppendUID( p_segment->next_segment_uid );
707 size_t AddSegment( matroska_segment_c *p_segment );
708 void PreloadLinked( );
709 mtime_t Duration( ) const;
711 void Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter );
713 inline chapter_edition_c *Edition()
715 if ( i_current_edition >= 0 && size_t(i_current_edition) < p_editions->size() )
716 return (*p_editions)[i_current_edition];
720 inline bool EditionIsOrdered() const
722 return (p_editions->size() != 0 && i_current_edition >= 0 && (*p_editions)[i_current_edition]->b_ordered);
725 matroska_segment_c * Segment() const
727 if ( linked_segments.size() == 0 || i_current_segment >= linked_segments.size() )
729 return linked_segments[i_current_segment];
732 inline const chapter_item_c *CurrentChapter() const {
733 return psz_current_chapter;
738 if ( i_current_segment < linked_segments.size()-1 )
746 bool FindUID( KaxSegmentUID & uid ) const
748 for ( size_t i=0; i<linked_uids.size(); i++ )
750 if ( linked_uids[i] == uid )
756 void UpdateCurrentToChapter( demux_t & demux );
757 void PrepareChapters( );
758 bool Select( input_title_t & title );
760 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
761 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
762 const void *p_cookie,
763 size_t i_cookie_size );
765 std::vector<matroska_segment_c*> linked_segments;
766 std::vector<KaxSegmentUID> linked_uids;
767 size_t i_current_segment;
769 std::vector<chapter_edition_c*> *p_editions;
770 int i_current_edition;
771 chapter_item_c *psz_current_chapter;
773 void AppendUID( const EbmlBinary & UID );
776 class matroska_stream_c
779 matroska_stream_c( demux_sys_t & demuxer )
785 virtual ~matroska_stream_c()
794 std::vector<matroska_segment_c*> segments;
802 demux_sys_t( demux_t & demux )
809 ,p_current_segment(NULL)
813 virtual ~demux_sys_t()
816 for ( i=0; i<streams.size(); i++ )
818 for ( i=0; i<opened_segments.size(); i++ )
819 delete opened_segments[i];
820 for ( i=0; i<used_segments.size(); i++ )
821 delete used_segments[i];
829 mtime_t i_chapter_time;
833 input_title_t *title;
835 std::vector<matroska_stream_c*> streams;
836 std::vector<matroska_segment_c*> opened_segments;
837 std::vector<virtual_segment_c*> used_segments;
838 virtual_segment_c *p_current_segment;
840 /* duration of the stream */
843 matroska_segment_c *FindSegment( const EbmlBinary & uid ) const;
844 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
845 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
846 const void *p_cookie,
847 size_t i_cookie_size,
848 virtual_segment_c * & p_segment_found );
850 void PreloadFamily( const matroska_segment_c & of_segment );
851 void PreloadLinked( matroska_segment_c *p_segment );
852 bool PreparePlayback( );
853 matroska_stream_c *AnalyseAllSegmentsFound( EbmlStream *p_estream );
856 virtual_segment_c *VirtualFromSegments( matroska_segment_c *p_segment ) const;
857 bool IsUsedSegment( matroska_segment_c &p_segment ) const;
860 static int Demux ( demux_t * );
861 static int Control( demux_t *, int, va_list );
862 static void Seek ( demux_t *, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter );
864 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
866 static char *UTF8ToStr ( const UTFstring &u );
868 /*****************************************************************************
869 * Open: initializes matroska demux structures
870 *****************************************************************************/
871 static int Open( vlc_object_t * p_this )
873 demux_t *p_demux = (demux_t*)p_this;
875 matroska_stream_c *p_stream;
876 matroska_segment_c *p_segment;
878 std::string s_path, s_filename;
879 vlc_stream_io_callback *p_io_callback;
880 EbmlStream *p_io_stream;
882 /* peek the begining */
883 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
885 /* is a valid file */
886 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
887 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
889 /* Set the demux function */
890 p_demux->pf_demux = Demux;
891 p_demux->pf_control = Control;
892 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
894 p_io_callback = new vlc_stream_io_callback( p_demux->s );
895 p_io_stream = new EbmlStream( *p_io_callback );
897 if( p_io_stream == NULL )
899 msg_Err( p_demux, "failed to create EbmlStream" );
900 delete p_io_callback;
905 p_stream = p_sys->AnalyseAllSegmentsFound( p_io_stream );
906 if( p_stream == NULL )
908 msg_Err( p_demux, "cannot find KaxSegment" );
911 p_sys->streams.push_back( p_stream );
913 p_stream->p_in = p_io_callback;
914 p_stream->p_es = p_io_stream;
916 for (size_t i=0; i<p_stream->segments.size(); i++)
918 p_stream->segments[i]->Preload();
921 p_segment = p_stream->segments[0];
922 if( p_segment->cluster != NULL )
924 msg_Warn( p_demux, "cannot find any cluster, damaged file ?" );
926 // reset the stream reading to the first cluster of the segment used
927 p_stream->p_in->setFilePointer( p_segment->cluster->GetElementPosition() );
930 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
931 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
933 // assume it's a regular file
934 // get the directory path
935 s_path = p_demux->psz_path;
936 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
938 s_path = s_path.substr(0,s_path.length()-1);
942 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
944 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
948 struct dirent *p_file_item;
949 DIR *p_src_dir = opendir(s_path.c_str());
951 if (p_src_dir != NULL)
953 while ((p_file_item = (dirent *) readdir(p_src_dir)))
955 if (strlen(p_file_item->d_name) > 4)
957 s_filename = s_path + DIRECTORY_SEPARATOR + p_file_item->d_name;
959 if (!s_filename.compare(p_demux->psz_path))
960 continue; // don't reuse the original opened file
962 #if defined(__GNUC__) && (__GNUC__ < 3)
963 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
964 !s_filename.compare("mka", s_filename.length() - 3, 3))
966 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
967 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
970 // test wether this file belongs to the our family
971 StdIOCallback *p_file_io = new StdIOCallback(s_filename.c_str(), MODE_READ);
972 EbmlStream *p_estream = new EbmlStream(*p_file_io);
974 p_stream = p_sys->AnalyseAllSegmentsFound( p_estream );
975 if ( p_stream == NULL )
977 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
983 p_stream->p_in = p_file_io;
984 p_stream->p_es = p_estream;
985 p_sys->streams.push_back( p_stream );
990 closedir( p_src_dir );
994 p_sys->PreloadFamily( *p_segment );
995 p_sys->PreloadLinked( p_segment );
996 if ( !p_sys->PreparePlayback( ) )
998 msg_Err( p_demux, "cannot use the segment" );
1006 return VLC_EGENERIC;
1009 /*****************************************************************************
1010 * Close: frees unused data
1011 *****************************************************************************/
1012 static void Close( vlc_object_t *p_this )
1014 demux_t *p_demux = (demux_t*)p_this;
1015 demux_sys_t *p_sys = p_demux->p_sys;
1020 /*****************************************************************************
1022 *****************************************************************************/
1023 static int Control( demux_t *p_demux, int i_query, va_list args )
1025 demux_sys_t *p_sys = p_demux->p_sys;
1030 vlc_meta_t **pp_meta;
1034 case DEMUX_GET_META:
1035 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1036 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1039 case DEMUX_GET_LENGTH:
1040 pi64 = (int64_t*)va_arg( args, int64_t * );
1041 if( p_sys->f_duration > 0.0 )
1043 *pi64 = (int64_t)(p_sys->f_duration * 1000);
1046 return VLC_EGENERIC;
1048 case DEMUX_GET_POSITION:
1049 pf = (double*)va_arg( args, double * );
1050 if ( p_sys->f_duration > 0.0 )
1051 *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);
1054 case DEMUX_SET_POSITION:
1055 f = (double)va_arg( args, double );
1056 Seek( p_demux, -1, f, NULL );
1059 case DEMUX_GET_TIME:
1060 pi64 = (int64_t*)va_arg( args, int64_t * );
1061 *pi64 = p_sys->i_pts;
1064 case DEMUX_GET_TITLE_INFO:
1065 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
1067 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1068 int *pi_int = (int*)va_arg( args, int* );
1071 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) );
1073 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->title );
1077 return VLC_EGENERIC;
1079 case DEMUX_SET_TITLE:
1080 /* TODO handle editions as titles & DVD titles as well */
1081 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
1085 return VLC_EGENERIC;
1087 case DEMUX_SET_SEEKPOINT:
1088 i_skp = (int)va_arg( args, int );
1090 if( p_sys->title && i_skp < p_sys->title->i_seekpoint)
1092 Seek( p_demux, (int64_t)p_sys->title->seekpoint[i_skp]->i_time_offset, -1, NULL);
1093 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
1094 p_demux->info.i_seekpoint = i_skp;
1097 return VLC_EGENERIC;
1099 case DEMUX_SET_TIME:
1102 return VLC_EGENERIC;
1106 int matroska_segment_c::BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1118 i_level = ep->GetLevel();
1120 if( el == NULL && *pp_block != NULL )
1122 /* update the index */
1123 #define idx index[i_index - 1]
1124 if( i_index > 0 && idx.i_time == -1 )
1126 idx.i_time = (*pp_block)->GlobalTimecode() / (mtime_t)1000;
1127 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1135 if( ep->GetLevel() > 1 )
1140 msg_Warn( &sys.demuxer, "EOF" );
1141 return VLC_EGENERIC;
1147 if( MKV_IS_ID( el, KaxCluster ) )
1149 cluster = (KaxCluster*)el;
1151 /* add it to the index */
1153 ( i_index > 0 && index[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1155 IndexAppendCluster( cluster );
1158 // reset silent tracks
1159 for (size_t i=0; i<tracks.size(); i++)
1161 tracks[i]->b_silent = VLC_FALSE;
1166 else if( MKV_IS_ID( el, KaxCues ) )
1168 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1169 return VLC_EGENERIC;
1173 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1176 else if( i_level == 2 )
1178 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1180 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1182 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1183 cluster->InitTimecode( uint64( ctc ), i_timescale );
1185 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1189 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1194 else if( i_level == 3 )
1196 if( MKV_IS_ID( el, KaxBlock ) )
1198 *pp_block = (KaxBlock*)el;
1200 (*pp_block)->ReadData( es.I_O() );
1201 (*pp_block)->SetParent( *cluster );
1205 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1207 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1209 dur.ReadData( es.I_O() );
1210 *pi_duration = uint64( dur );
1212 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1214 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1216 ref.ReadData( es.I_O() );
1217 if( *pi_ref1 == -1 )
1219 *pi_ref1 = int64( ref );
1223 *pi_ref2 = int64( ref );
1226 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1228 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1229 track_num.ReadData( es.I_O() );
1231 for (size_t i=0; i<tracks.size(); i++)
1233 if ( tracks[i]->i_number == uint32(track_num))
1235 tracks[i]->b_silent = VLC_TRUE;
1243 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1244 return VLC_EGENERIC;
1249 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1252 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1253 memcpy( p_block->p_buffer, p_mem, i_mem );
1254 //p_block->i_rate = p_input->stream.control.i_rate;
1258 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1259 mtime_t i_duration )
1261 demux_sys_t *p_sys = p_demux->p_sys;
1262 matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
1268 #define tk p_segment->tracks[i_track]
1269 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1271 if( tk->i_number == block->TrackNum() )
1277 if( i_track >= p_segment->tracks.size() )
1279 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1282 if( tk->p_es == NULL )
1284 msg_Err( p_demux, "unknown track number=%d", block->TrackNum() );
1287 if( i_pts < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1289 return; /* discard audio packets that shouldn't be rendered */
1292 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1295 tk->b_inited = VLC_FALSE;
1299 /* First send init data */
1300 if( !tk->b_inited && tk->i_data_init > 0 )
1304 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
1305 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init );
1306 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
1308 tk->b_inited = VLC_TRUE;
1311 for( i = 0; i < block->NumberFrames(); i++ )
1314 DataBuffer &data = block->GetBuffer(i);
1316 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1318 if( p_block == NULL )
1323 #if defined(HAVE_ZLIB_H)
1324 if( tk->i_compression_type )
1326 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
1330 // TODO implement correct timestamping when B frames are used
1331 if( tk->fmt.i_cat != VIDEO_ES )
1333 p_block->i_dts = p_block->i_pts = i_pts;
1337 p_block->i_dts = i_pts;
1341 if( tk->fmt.i_cat == SPU_ES && strcmp( tk->psz_codec, "S_VOBSUB" ) )
1343 p_block->i_length = i_duration * 1000;
1346 es_out_Send( p_demux->out, tk->p_es, p_block );
1348 /* use time stamp only for first block */
1355 matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( EbmlStream *p_estream )
1357 int i_upper_lvl = 0;
1359 EbmlElement *p_l0, *p_l1, *p_l2;
1360 bool b_keep_stream = false, b_keep_segment;
1362 // verify the EBML Header
1363 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
1368 p_l0->SkipData(*p_estream, EbmlHead_Context);
1371 // find all segments in this file
1372 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1378 matroska_stream_c *p_stream1 = new matroska_stream_c( *this );
1382 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
1385 matroska_segment_c *p_segment1 = new matroska_segment_c( *this, *p_estream );
1386 b_keep_segment = false;
1388 ep = new EbmlParser(p_estream, p_l0, &demuxer );
1389 p_segment1->ep = ep;
1390 p_segment1->segment = (KaxSegment*)p_l0;
1392 while ((p_l1 = ep->Get()))
1394 if (MKV_IS_ID(p_l1, KaxInfo))
1396 // find the families of this segment
1397 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
1399 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
1400 for( i = 0; i < p_info->ListSize(); i++ )
1402 EbmlElement *l = (*p_info)[i];
1404 if( MKV_IS_ID( l, KaxSegmentUID ) )
1406 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
1407 b_keep_segment = (FindSegment( *p_uid ) == NULL);
1408 if ( !b_keep_segment )
1409 break; // this segment is already known
1410 opened_segments.push_back( p_segment1 );
1411 p_segment1->segment_uid = *( new KaxSegmentUID(*p_uid) );
1413 else if( MKV_IS_ID( l, KaxPrevUID ) )
1415 p_segment1->prev_segment_uid = *( new KaxPrevUID( *static_cast<KaxPrevUID*>(l) ) );
1417 else if( MKV_IS_ID( l, KaxNextUID ) )
1419 p_segment1->next_segment_uid = *( new KaxNextUID( *static_cast<KaxNextUID*>(l) ) );
1421 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
1423 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
1424 p_segment1->families.push_back( *p_fam );
1430 if ( b_keep_segment )
1432 b_keep_stream = true;
1433 p_stream1->segments.push_back( p_segment1 );
1439 p_l0->SkipData(*p_estream, EbmlHead_Context);
1440 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1443 if ( !b_keep_stream )
1452 bool matroska_segment_c::Select( mtime_t i_start_time )
1457 msg_Dbg( &sys.demuxer, "found %d es", tracks.size() );
1458 for( i_track = 0; i_track < tracks.size(); i_track++ )
1460 #define tk tracks[i_track]
1461 if( tk->fmt.i_cat == UNKNOWN_ES )
1463 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", i_track, tk->i_number );
1468 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
1470 if( tk->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
1472 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
1473 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1477 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk->p_extra_data;
1479 tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1480 tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1481 tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1483 tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
1484 if( tk->fmt.i_extra > 0 )
1486 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1487 memcpy( tk->fmt.p_extra, &p_bih[1], tk->fmt.i_extra );
1491 else if( !strcmp( tk->psz_codec, "V_MPEG1" ) ||
1492 !strcmp( tk->psz_codec, "V_MPEG2" ) )
1494 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1496 else if( !strncmp( tk->psz_codec, "V_MPEG4", 7 ) )
1498 if( !strcmp( tk->psz_codec, "V_MPEG4/MS/V3" ) )
1500 tk->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1502 else if( !strcmp( tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1504 tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1505 tk->fmt.b_packetized = VLC_FALSE;
1506 tk->fmt.i_extra = tk->i_extra_data;
1507 tk->fmt.p_extra = malloc( tk->i_extra_data );
1508 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
1512 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1515 else if( !strcmp( tk->psz_codec, "V_QUICKTIME" ) )
1517 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
1519 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1523 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1528 MP4_ReadBoxCommon( p_mp4_stream, p_box );
1529 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
1530 tk->fmt.i_codec = p_box->i_type;
1531 tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1532 tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1533 tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1534 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1535 memcpy( tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tk->fmt.i_extra );
1536 MP4_FreeBox_sample_vide( p_box );
1538 stream_MemoryDelete( p_mp4_stream, VLC_TRUE );
1540 stream_Delete( p_mp4_stream );
1543 else if( !strcmp( tk->psz_codec, "A_MS/ACM" ) )
1545 if( tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1547 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1548 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1552 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk->p_extra_data;
1554 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk->fmt.i_codec, NULL );
1556 tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1557 tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1558 tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1559 tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1560 tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1562 tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1563 if( tk->fmt.i_extra > 0 )
1565 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1566 memcpy( tk->fmt.p_extra, &p_wf[1], tk->fmt.i_extra );
1570 else if( !strcmp( tk->psz_codec, "A_MPEG/L3" ) ||
1571 !strcmp( tk->psz_codec, "A_MPEG/L2" ) ||
1572 !strcmp( tk->psz_codec, "A_MPEG/L1" ) )
1574 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1576 else if( !strcmp( tk->psz_codec, "A_AC3" ) )
1578 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1580 else if( !strcmp( tk->psz_codec, "A_DTS" ) )
1582 tk->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
1584 else if( !strcmp( tk->psz_codec, "A_FLAC" ) )
1586 tk->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
1587 tk->fmt.i_extra = tk->i_extra_data;
1588 tk->fmt.p_extra = malloc( tk->i_extra_data );
1589 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
1591 else if( !strcmp( tk->psz_codec, "A_VORBIS" ) )
1593 int i, i_offset = 1, i_size[3], i_extra;
1596 tk->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
1598 /* Split the 3 headers */
1599 if( tk->p_extra_data[0] != 0x02 )
1600 msg_Err( &sys.demuxer, "invalid vorbis header" );
1602 for( i = 0; i < 2; i++ )
1605 while( i_offset < tk->i_extra_data )
1607 i_size[i] += tk->p_extra_data[i_offset];
1608 if( tk->p_extra_data[i_offset++] != 0xff ) break;
1612 i_size[0] = __MIN(i_size[0], tk->i_extra_data - i_offset);
1613 i_size[1] = __MIN(i_size[1], tk->i_extra_data -i_offset -i_size[0]);
1614 i_size[2] = tk->i_extra_data - i_offset - i_size[0] - i_size[1];
1616 tk->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
1617 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1618 p_extra = (uint8_t *)tk->fmt.p_extra; i_extra = 0;
1619 for( i = 0; i < 3; i++ )
1621 *(p_extra++) = i_size[i] >> 8;
1622 *(p_extra++) = i_size[i] & 0xFF;
1623 memcpy( p_extra, tk->p_extra_data + i_offset + i_extra,
1625 p_extra += i_size[i];
1626 i_extra += i_size[i];
1629 else if( !strncmp( tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1630 !strncmp( tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1632 int i_profile, i_srate;
1633 static unsigned int i_sample_rates[] =
1635 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1636 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1639 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1640 /* create data for faad (MP4DecSpecificDescrTag)*/
1642 if( !strcmp( &tk->psz_codec[12], "MAIN" ) )
1646 else if( !strcmp( &tk->psz_codec[12], "LC" ) )
1650 else if( !strcmp( &tk->psz_codec[12], "SSR" ) )
1659 for( i_srate = 0; i_srate < 13; i_srate++ )
1661 if( i_sample_rates[i_srate] == tk->fmt.audio.i_rate )
1666 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1668 tk->fmt.i_extra = 2;
1669 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1670 ((uint8_t*)tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1671 ((uint8_t*)tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk->fmt.audio.i_channels << 3);
1673 else if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) ||
1674 !strcmp( tk->psz_codec, "A_PCM/INT/LIT" ) ||
1675 !strcmp( tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1677 if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) )
1679 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1683 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1685 tk->fmt.audio.i_blockalign = ( tk->fmt.audio.i_bitspersample + 7 ) / 8 * tk->fmt.audio.i_channels;
1687 else if( !strcmp( tk->psz_codec, "A_TTA1" ) )
1689 /* FIXME: support this codec */
1690 msg_Err( &sys.demuxer, "TTA not supported yet[%d, n=%d]", i_track, tk->i_number );
1691 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1693 else if( !strcmp( tk->psz_codec, "A_WAVPACK4" ) )
1695 /* FIXME: support this codec */
1696 msg_Err( &sys.demuxer, "Wavpack not supported yet[%d, n=%d]", i_track, tk->i_number );
1697 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1699 else if( !strcmp( tk->psz_codec, "S_TEXT/UTF8" ) )
1701 tk->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1702 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1704 else if( !strcmp( tk->psz_codec, "S_TEXT/SSA" ) ||
1705 !strcmp( tk->psz_codec, "S_TEXT/ASS" ) ||
1706 !strcmp( tk->psz_codec, "S_SSA" ) ||
1707 !strcmp( tk->psz_codec, "S_ASS" ))
1709 tk->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1710 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1712 else if( !strcmp( tk->psz_codec, "S_VOBSUB" ) )
1714 tk->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1715 if( tk->i_extra_data )
1718 char *p_buf = (char *)malloc( tk->i_extra_data + 1);
1719 memcpy( p_buf, tk->p_extra_data , tk->i_extra_data );
1720 p_buf[tk->i_extra_data] = '\0';
1722 p_start = strstr( p_buf, "size:" );
1723 if( sscanf( p_start, "size: %dx%d",
1724 &tk->fmt.subs.spu.i_original_frame_width, &tk->fmt.subs.spu.i_original_frame_height ) == 2 )
1726 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 );
1730 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1735 else if( !strcmp( tk->psz_codec, "B_VOBBTN" ) )
1737 /* FIXME: support this codec */
1738 msg_Err( &sys.demuxer, "Vob Buttons not supported yet[%d, n=%d]", i_track, tk->i_number );
1739 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1743 msg_Err( &sys.demuxer, "unknow codec id=`%s'", tk->psz_codec );
1744 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1748 tk->fmt.i_priority = 1000;
1751 tk->p_es = es_out_Add( sys.demuxer.out, &tk->fmt );
1753 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_start_time );
1757 sys.i_start_pts = i_start_time;
1758 // reset the stream reading to the first cluster of the segment used
1759 es.I_O().setFilePointer( i_start_pos );
1762 ep = new EbmlParser( &es, segment, &sys.demuxer );
1767 void matroska_segment_c::UnSelect( )
1771 for( i_track = 0; i_track < tracks.size(); i_track++ )
1773 #define tk tracks[i_track]
1774 if ( tk->p_es != NULL )
1776 es_out_Del( sys.demuxer.out, tk->p_es );
1785 void virtual_segment_c::PrepareChapters( )
1787 if ( linked_segments.size() == 0 )
1790 // !!! should be called only once !!!
1791 matroska_segment_c *p_segment;
1794 // copy editions from the first segment
1795 p_segment = linked_segments[0];
1796 p_editions = &p_segment->stored_editions;
1798 for ( i=1 ; i<linked_segments.size(); i++ )
1800 p_segment = linked_segments[i];
1801 // FIXME assume we have the same editions in all segments
1802 for (j=0; j<p_segment->stored_editions.size(); j++)
1803 (*p_editions)[j]->Append( *p_segment->stored_editions[j] );
1807 bool virtual_segment_c::Select( input_title_t & title )
1809 if ( Edition() != NULL )
1810 Edition()->PublishChapters( title );
1815 void chapter_edition_c::PublishChapters( input_title_t & title )
1817 if ( title.seekpoint != NULL )
1818 free( title.seekpoint );
1820 title.i_seekpoint = 0;
1821 title.seekpoint = NULL;
1823 chapter_item_c::PublishChapters( title, 0 );
1826 void chapter_item_c::PublishChapters( input_title_t & title, int i_level )
1828 // add support for meta-elements from codec like DVD Titles
1829 if ( psz_name == "" || !b_display_seekpoint )
1831 psz_name = GetCodecName();
1832 if ( psz_name != "" )
1833 b_display_seekpoint = true;
1836 if (b_display_seekpoint)
1838 seekpoint_t *sk = vlc_seekpoint_New();
1840 sk->i_level = i_level;
1841 sk->i_time_offset = i_start_time;
1842 sk->psz_name = strdup( psz_name.c_str() );
1844 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
1845 title.i_seekpoint++;
1846 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
1847 title.seekpoint[title.i_seekpoint-1] = sk;
1849 i_seekpoint_num = title.i_seekpoint;
1852 for ( size_t i=0; i<sub_chapters.size() ; i++)
1854 sub_chapters[i]->PublishChapters( title, i_level+1 );
1858 void virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
1860 demux_sys_t & sys = *demux.p_sys;
1861 chapter_item_c *psz_curr_chapter;
1863 /* update current chapter/seekpoint */
1864 if ( p_editions->size() )
1866 /* 1st, we need to know in which chapter we are */
1867 psz_curr_chapter = (*p_editions)[i_current_edition]->FindTimecode( sys.i_pts );
1869 /* we have moved to a new chapter */
1870 if (psz_curr_chapter != NULL && psz_current_chapter != psz_curr_chapter)
1872 if ( psz_current_chapter != NULL )
1873 psz_current_chapter->Leave();
1875 if ( psz_curr_chapter->i_seekpoint_num > 0 )
1877 demux.info.i_update |= INPUT_UPDATE_SEEKPOINT;
1878 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
1881 if ( (*p_editions)[i_current_edition]->b_ordered )
1883 psz_curr_chapter->Enter();
1885 // only seek if necessary
1886 if ( psz_current_chapter == NULL || (psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time) )
1887 Seek( demux, sys.i_pts, 0, psz_curr_chapter );
1890 psz_current_chapter = psz_curr_chapter;
1894 chapter_item_c *virtual_segment_c::BrowseCodecPrivate( unsigned int codec_id,
1895 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1896 const void *p_cookie,
1897 size_t i_cookie_size )
1899 // FIXME don't assume it is the first edition
1900 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
1901 if ( index != p_editions->end() )
1903 chapter_item_c *p_result = (*index)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
1904 if ( p_result != NULL )
1910 chapter_item_c *chapter_item_c::BrowseCodecPrivate( unsigned int codec_id,
1911 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1912 const void *p_cookie,
1913 size_t i_cookie_size )
1916 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
1917 while ( index != codecs.end() )
1919 if ( match( **index ,p_cookie, i_cookie_size ) )
1925 chapter_item_c *p_result = NULL;
1926 std::vector<chapter_item_c*>::const_iterator index2 = sub_chapters.begin();
1927 while ( index2 != sub_chapters.end() )
1929 p_result = (*index2)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
1930 if ( p_result != NULL )
1938 void chapter_item_c::Append( const chapter_item_c & chapter )
1940 // we are appending content for the same chapter UID
1942 chapter_item_c *p_chapter;
1944 for ( i=0; i<chapter.sub_chapters.size(); i++ )
1946 p_chapter = FindChapter( *chapter.sub_chapters[i] );
1947 if ( p_chapter != NULL )
1949 p_chapter->Append( *chapter.sub_chapters[i] );
1953 sub_chapters.push_back( chapter.sub_chapters[i] );
1957 i_user_start_time = min( i_user_start_time, chapter.i_user_start_time );
1958 i_user_end_time = max( i_user_end_time, chapter.i_user_end_time );
1961 chapter_item_c * chapter_item_c::FindChapter( const chapter_item_c & chapter )
1964 for ( i=0; i<sub_chapters.size(); i++)
1966 if ( sub_chapters[i]->i_uid == chapter.i_uid )
1967 return sub_chapters[i];
1972 std::string chapter_item_c::GetCodecName() const
1976 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
1977 while ( index != codecs.end() )
1979 result = (*index)->GetCodecName();
1988 std::string dvd_chapter_codec_c::GetCodecName() const
1991 if ( m_private_data.GetSize() >= 3)
1993 const binary* p_data = m_private_data.GetBuffer();
1994 if ( p_data[0] == 0x28 )
1996 uint16_t i_title = (p_data[1] << 8) + p_data[2];
1998 sprintf( psz_str, " %d", i_title );
1999 result = N_("DVD Title");
2007 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter )
2009 demux_sys_t *p_sys = p_demux->p_sys;
2010 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
2011 matroska_segment_c *p_segment = p_vsegment->Segment();
2012 mtime_t i_time_offset = 0;
2016 msg_Dbg( p_demux, "seek request to "I64Fd" (%f%%)", i_date, f_percent );
2017 if( i_date < 0 && f_percent < 0 )
2019 msg_Warn( p_demux, "cannot seek nowhere !" );
2022 if( f_percent > 1.0 )
2024 msg_Warn( p_demux, "cannot seek so far !" );
2028 /* seek without index or without date */
2029 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
2031 if (p_sys->f_duration >= 0)
2033 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
2037 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
2039 msg_Dbg( p_demux, "inacurate way of seeking" );
2040 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
2042 if( p_segment->index[i_index].i_position >= i_pos)
2047 if( i_index == p_segment->i_index )
2052 i_date = p_segment->index[i_index].i_time;
2055 if( p_segment->index[i_index].i_position < i_pos )
2059 msg_Warn( p_demux, "searching for cluster, could take some time" );
2061 /* search a cluster */
2062 while( ( el = p_sys->ep->Get() ) != NULL )
2064 if( MKV_IS_ID( el, KaxCluster ) )
2066 KaxCluster *cluster = (KaxCluster*)el;
2068 /* add it to the index */
2069 p_segment->IndexAppendCluster( cluster );
2071 if( (int64_t)cluster->GetElementPosition() >= i_pos )
2073 p_sys->cluster = cluster;
2084 p_vsegment->Seek( *p_demux, i_date, i_time_offset, psz_chapter );
2087 /*****************************************************************************
2088 * Demux: reads and demuxes data packets
2089 *****************************************************************************
2090 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
2091 *****************************************************************************/
2092 static int Demux( demux_t *p_demux)
2094 demux_sys_t *p_sys = p_demux->p_sys;
2095 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
2096 matroska_segment_c *p_segmet = p_vsegment->Segment();
2097 if ( p_segmet == NULL ) return 0;
2098 int i_block_count = 0;
2101 int64_t i_block_duration;
2102 int64_t i_block_ref1;
2103 int64_t i_block_ref2;
2107 if ( p_sys->demuxer.b_die )
2110 if( p_sys->i_pts >= p_sys->i_start_pts )
2111 p_vsegment->UpdateCurrentToChapter( *p_demux );
2113 if ( p_vsegment->EditionIsOrdered() && p_vsegment->CurrentChapter() == NULL )
2115 /* nothing left to read in this ordered edition */
2116 if ( !p_vsegment->SelectNext() )
2118 p_segmet->UnSelect( );
2120 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
2122 /* switch to the next segment */
2123 p_segmet = p_vsegment->Segment();
2124 if ( !p_segmet->Select( 0 ) )
2126 msg_Err( p_demux, "Failed to select new segment" );
2133 if( p_segmet->BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
2135 if ( p_vsegment->EditionIsOrdered() )
2137 const chapter_item_c *p_chap = p_vsegment->CurrentChapter();
2138 // check if there are more chapters to read
2139 if ( p_chap != NULL )
2141 /* TODO handle successive chapters with the same user_start_time/user_end_time
2142 if ( p_chap->i_user_start_time == p_chap->i_user_start_time )
2143 p_vsegment->SelectNext();
2145 p_sys->i_pts = p_chap->i_user_end_time;
2146 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
2153 msg_Warn( p_demux, "cannot get block EOF?" );
2154 p_segmet->UnSelect( );
2156 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
2158 /* switch to the next segment */
2159 if ( !p_vsegment->SelectNext() )
2160 // no more segments in this stream
2162 p_segmet = p_vsegment->Segment();
2163 if ( !p_segmet->Select( 0 ) )
2165 msg_Err( p_demux, "Failed to select new segment" );
2172 p_sys->i_pts = p_sys->i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
2174 if( p_sys->i_pts >= p_sys->i_start_pts )
2176 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
2179 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
2184 // TODO optimize when there is need to leave or when seeking has been called
2185 if( i_block_count > 5 )
2194 /*****************************************************************************
2196 *****************************************************************************/
2197 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
2203 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
2205 if( i_size <= 0 || mb_eof )
2210 return stream_Read( s, p_buffer, i_size );
2212 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
2218 case seek_beginning:
2222 i_pos = stream_Size( s ) - i_offset;
2225 i_pos= stream_Tell( s ) + i_offset;
2229 if( i_pos < 0 || i_pos >= stream_Size( s ) )
2236 if( stream_Seek( s, i_pos ) )
2242 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
2246 uint64 vlc_stream_io_callback::getFilePointer( void )
2248 return stream_Tell( s );
2250 void vlc_stream_io_callback::close( void )
2256 /*****************************************************************************
2257 * Ebml Stream parser
2258 *****************************************************************************/
2259 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux )
2266 mi_remain_size[0] = el_start->GetSize();
2268 for( i = 1; i < 6; i++ )
2274 mb_keep = VLC_FALSE;
2275 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
2278 EbmlParser::~EbmlParser( void )
2282 for( i = 1; i < mi_level; i++ )
2288 mb_keep = VLC_FALSE;
2292 void EbmlParser::Up( void )
2294 if( mi_user_level == mi_level )
2296 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
2302 void EbmlParser::Down( void )
2308 void EbmlParser::Keep( void )
2313 int EbmlParser::GetLevel( void )
2315 return mi_user_level;
2318 void EbmlParser::Reset( demux_t *p_demux )
2320 while ( mi_level > 0)
2322 delete m_el[mi_level];
2323 m_el[mi_level] = NULL;
2326 mi_user_level = mi_level = 1;
2327 #if LIBEBML_VERSION >= 0x000704
2328 // a little faster and cleaner
2329 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
2331 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
2333 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
2336 EbmlElement *EbmlParser::Get( void )
2340 if( mi_user_level != mi_level )
2346 EbmlElement *ret = m_got;
2352 if( m_el[mi_level] )
2354 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
2357 delete m_el[mi_level];
2359 mb_keep = VLC_FALSE;
2362 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, mb_dummy, 1 );
2363 // mi_remain_size[mi_level] = m_el[mi_level]->GetSize();
2374 delete m_el[mi_level - 1];
2375 m_got = m_el[mi_level -1] = m_el[mi_level];
2376 m_el[mi_level] = NULL;
2383 else if( m_el[mi_level] == NULL )
2385 fprintf( stderr," m_el[mi_level] == NULL\n" );
2388 return m_el[mi_level];
2392 /*****************************************************************************
2394 * * LoadCues : load the cues element and update index
2396 * * LoadTags : load ... the tags element
2398 * * InformationCreate : create all information, load tags if present
2400 *****************************************************************************/
2401 void matroska_segment_c::LoadCues( )
2403 int64_t i_sav_position = es.I_O().getFilePointer();
2405 EbmlElement *el, *cues;
2407 /* *** Load the cue if found *** */
2408 if( i_cues_position < 0 )
2410 msg_Warn( &sys.demuxer, "no cues/empty cues found->seek won't be precise" );
2412 // IndexAppendCluster( cluster );
2415 vlc_bool_t b_seekable;
2417 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
2421 msg_Dbg( &sys.demuxer, "loading cues" );
2422 es.I_O().setFilePointer( i_cues_position, seek_beginning );
2423 cues = es.FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
2427 msg_Err( &sys.demuxer, "cannot load cues (broken seekhead or file)" );
2428 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2432 ep = new EbmlParser( &es, cues, &sys.demuxer );
2433 while( ( el = ep->Get() ) != NULL )
2435 if( MKV_IS_ID( el, KaxCuePoint ) )
2437 #define idx index[i_index]
2440 idx.i_block_number= -1;
2441 idx.i_position = -1;
2443 idx.b_key = VLC_TRUE;
2446 while( ( el = ep->Get() ) != NULL )
2448 if( MKV_IS_ID( el, KaxCueTime ) )
2450 KaxCueTime &ctime = *(KaxCueTime*)el;
2452 ctime.ReadData( es.I_O() );
2454 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
2456 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
2459 while( ( el = ep->Get() ) != NULL )
2461 if( MKV_IS_ID( el, KaxCueTrack ) )
2463 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
2465 ctrack.ReadData( es.I_O() );
2466 idx.i_track = uint16( ctrack );
2468 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
2470 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
2472 ccpos.ReadData( es.I_O() );
2473 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
2475 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
2477 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2479 cbnum.ReadData( es.I_O() );
2480 idx.i_block_number = uint32( cbnum );
2484 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2491 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2497 msg_Dbg( &sys.demuxer, " * added time="I64Fd" pos="I64Fd
2498 " track=%d bnum=%d", idx.i_time, idx.i_position,
2499 idx.i_track, idx.i_block_number );
2503 if( i_index >= i_index_max )
2505 i_index_max += 1024;
2506 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
2512 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2520 msg_Dbg( &sys.demuxer, "loading cues done." );
2521 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2524 void matroska_segment_c::LoadTags( )
2526 int64_t i_sav_position = es.I_O().getFilePointer();
2528 EbmlElement *el, *tags;
2530 msg_Dbg( &sys.demuxer, "loading tags" );
2531 es.I_O().setFilePointer( i_tags_position, seek_beginning );
2532 tags = es.FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2536 msg_Err( &sys.demuxer, "cannot load tags (broken seekhead or file)" );
2537 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2541 msg_Dbg( &sys.demuxer, "Tags" );
2542 ep = new EbmlParser( &es, tags, &sys.demuxer );
2543 while( ( el = ep->Get() ) != NULL )
2545 if( MKV_IS_ID( el, KaxTag ) )
2547 msg_Dbg( &sys.demuxer, "+ Tag" );
2549 while( ( el = ep->Get() ) != NULL )
2551 if( MKV_IS_ID( el, KaxTagTargets ) )
2553 msg_Dbg( &sys.demuxer, "| + Targets" );
2555 while( ( el = ep->Get() ) != NULL )
2557 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2561 else if( MKV_IS_ID( el, KaxTagGeneral ) )
2563 msg_Dbg( &sys.demuxer, "| + General" );
2565 while( ( el = ep->Get() ) != NULL )
2567 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2571 else if( MKV_IS_ID( el, KaxTagGenres ) )
2573 msg_Dbg( &sys.demuxer, "| + Genres" );
2575 while( ( el = ep->Get() ) != NULL )
2577 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2581 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
2583 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
2585 while( ( el = ep->Get() ) != NULL )
2587 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2591 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
2593 msg_Dbg( &sys.demuxer, "| + Images Specific" );
2595 while( ( el = ep->Get() ) != NULL )
2597 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2601 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
2603 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
2605 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
2607 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
2609 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
2611 msg_Dbg( &sys.demuxer, "| + Multi Date" );
2613 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
2615 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
2617 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
2619 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
2621 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
2623 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
2625 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
2627 msg_Dbg( &sys.demuxer, "| + Multi Title" );
2631 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid( *el ).name() );
2638 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
2644 msg_Dbg( &sys.demuxer, "loading tags done." );
2645 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2648 /*****************************************************************************
2650 *****************************************************************************/
2651 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
2655 int i_upper_level = 0;
2657 msg_Dbg( &sys.demuxer, "| + Seek head" );
2659 /* Master elements */
2660 seekhead->Read( es, seekhead->Generic().Context, i_upper_level, el, true );
2662 for( i = 0; i < seekhead->ListSize(); i++ )
2664 EbmlElement *l = (*seekhead)[i];
2666 if( MKV_IS_ID( l, KaxSeek ) )
2668 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
2669 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
2672 for( j = 0; j < sk->ListSize(); j++ )
2674 EbmlElement *l = (*sk)[j];
2676 if( MKV_IS_ID( l, KaxSeekID ) )
2678 KaxSeekID &sid = *(KaxSeekID*)l;
2679 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
2681 else if( MKV_IS_ID( l, KaxSeekPosition ) )
2683 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
2684 i_pos = uint64( spos );
2688 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
2694 if( id == KaxCues::ClassInfos.GlobalId )
2696 msg_Dbg( &sys.demuxer, "| | | = cues at "I64Fd, i_pos );
2697 i_cues_position = segment->GetGlobalPosition( i_pos );
2699 else if( id == KaxChapters::ClassInfos.GlobalId )
2701 msg_Dbg( &sys.demuxer, "| | | = chapters at "I64Fd, i_pos );
2702 i_chapters_position = segment->GetGlobalPosition( i_pos );
2704 else if( id == KaxTags::ClassInfos.GlobalId )
2706 msg_Dbg( &sys.demuxer, "| | | = tags at "I64Fd, i_pos );
2707 i_tags_position = segment->GetGlobalPosition( i_pos );
2713 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
2718 /*****************************************************************************
2720 *****************************************************************************/
2721 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
2727 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
2729 tk = new mkv_track_t();
2730 tracks.push_back( tk );
2732 /* Init the track */
2733 memset( tk, 0, sizeof( mkv_track_t ) );
2735 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
2736 tk->fmt.psz_language = strdup("English");
2737 tk->fmt.psz_description = NULL;
2739 tk->b_default = VLC_TRUE;
2740 tk->b_enabled = VLC_TRUE;
2741 tk->b_silent = VLC_FALSE;
2742 tk->i_number = tracks.size() - 1;
2743 tk->i_extra_data = 0;
2744 tk->p_extra_data = NULL;
2745 tk->psz_codec = NULL;
2746 tk->i_default_duration = 0;
2747 tk->f_timecodescale = 1.0;
2749 tk->b_inited = VLC_FALSE;
2750 tk->i_data_init = 0;
2751 tk->p_data_init = NULL;
2753 tk->psz_codec_name = NULL;
2754 tk->psz_codec_settings = NULL;
2755 tk->psz_codec_info_url = NULL;
2756 tk->psz_codec_download_url = NULL;
2758 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
2760 for( i = 0; i < m->ListSize(); i++ )
2762 EbmlElement *l = (*m)[i];
2764 if( MKV_IS_ID( l, KaxTrackNumber ) )
2766 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
2768 tk->i_number = uint32( tnum );
2769 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
2771 else if( MKV_IS_ID( l, KaxTrackUID ) )
2773 KaxTrackUID &tuid = *(KaxTrackUID*)l;
2775 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
2777 else if( MKV_IS_ID( l, KaxTrackType ) )
2780 KaxTrackType &ttype = *(KaxTrackType*)l;
2782 switch( uint8(ttype) )
2786 tk->fmt.i_cat = AUDIO_ES;
2790 tk->fmt.i_cat = VIDEO_ES;
2792 case track_subtitle:
2793 psz_type = "subtitle";
2794 tk->fmt.i_cat = SPU_ES;
2797 psz_type = "unknown";
2798 tk->fmt.i_cat = UNKNOWN_ES;
2802 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
2804 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
2806 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
2808 // tk->b_enabled = uint32( fenb );
2809 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
2810 // uint32( fenb ) );
2812 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
2814 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
2816 tk->b_default = uint32( fdef );
2817 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
2819 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
2821 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
2823 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
2825 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
2827 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
2829 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
2831 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
2833 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
2835 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
2837 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
2839 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
2841 tk->i_default_duration = uint64(defd);
2842 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration="I64Fd, uint64(defd) );
2844 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
2846 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
2848 tk->f_timecodescale = float( ttcs );
2849 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
2851 else if( MKV_IS_ID( l, KaxTrackName ) )
2853 KaxTrackName &tname = *(KaxTrackName*)l;
2855 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
2856 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
2858 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
2860 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
2862 tk->fmt.psz_language = strdup( string( lang ).c_str() );
2863 msg_Dbg( &sys.demuxer,
2864 "| | | + Track Language=`%s'", tk->fmt.psz_language );
2866 else if( MKV_IS_ID( l, KaxCodecID ) )
2868 KaxCodecID &codecid = *(KaxCodecID*)l;
2870 tk->psz_codec = strdup( string( codecid ).c_str() );
2871 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
2873 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
2875 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
2877 tk->i_extra_data = cpriv.GetSize();
2878 if( tk->i_extra_data > 0 )
2880 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
2881 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
2883 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
2885 else if( MKV_IS_ID( l, KaxCodecName ) )
2887 KaxCodecName &cname = *(KaxCodecName*)l;
2889 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
2890 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
2892 else if( MKV_IS_ID( l, KaxContentEncodings ) )
2894 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
2895 MkvTree( sys.demuxer, 3, "Content Encodings" );
2896 for( j = 0; j < cencs->ListSize(); j++ )
2898 EbmlElement *l2 = (*cencs)[j];
2899 if( MKV_IS_ID( l2, KaxContentEncoding ) )
2901 MkvTree( sys.demuxer, 4, "Content Encoding" );
2902 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
2903 for( k = 0; k < cenc->ListSize(); k++ )
2905 EbmlElement *l3 = (*cenc)[k];
2906 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
2908 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
2909 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
2911 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
2913 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
2914 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
2916 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
2918 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
2919 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
2921 else if( MKV_IS_ID( l3, KaxContentCompression ) )
2923 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
2924 MkvTree( sys.demuxer, 5, "Content Compression" );
2925 for( n = 0; n < compr->ListSize(); n++ )
2927 EbmlElement *l4 = (*compr)[n];
2928 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
2930 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
2931 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
2932 if( uint32( compalg ) == 0 )
2934 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
2939 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
2946 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
2953 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
2958 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
2960 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
2962 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
2963 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
2965 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
2967 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
2969 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
2970 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
2972 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
2974 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
2976 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
2977 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
2979 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
2981 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
2983 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
2985 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
2987 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
2989 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
2991 else if( MKV_IS_ID( l, KaxTrackVideo ) )
2993 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
2996 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
2999 for( j = 0; j < tkv->ListSize(); j++ )
3001 EbmlElement *l = (*tkv)[j];
3002 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
3004 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
3006 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
3008 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
3010 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
3012 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
3015 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
3017 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
3019 tk->fmt.video.i_width = uint16( vwidth );
3020 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
3022 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
3024 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
3026 tk->fmt.video.i_height = uint16( vheight );
3027 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
3029 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
3031 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
3033 tk->fmt.video.i_visible_width = uint16( vwidth );
3034 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
3036 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
3038 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
3040 tk->fmt.video.i_visible_height = uint16( vheight );
3041 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
3043 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
3045 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
3047 tk->f_fps = float( vfps );
3048 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
3050 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
3052 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
3054 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
3055 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
3057 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
3059 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
3061 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
3063 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
3065 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
3067 // msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( gamma ) );
3071 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
3074 if ( tk->fmt.video.i_visible_height && tk->fmt.video.i_visible_width )
3075 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * tk->fmt.video.i_visible_width / tk->fmt.video.i_visible_height;
3077 else if( MKV_IS_ID( l, KaxTrackAudio ) )
3079 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
3082 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
3084 for( j = 0; j < tka->ListSize(); j++ )
3086 EbmlElement *l = (*tka)[j];
3088 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
3090 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
3092 tk->fmt.audio.i_rate = (int)float( afreq );
3093 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
3095 else if( MKV_IS_ID( l, KaxAudioChannels ) )
3097 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
3099 tk->fmt.audio.i_channels = uint8( achan );
3100 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
3102 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
3104 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
3106 tk->fmt.audio.i_bitspersample = uint8( abits );
3107 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
3111 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
3117 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
3118 typeid(*l).name() );
3123 /*****************************************************************************
3125 *****************************************************************************/
3126 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
3130 int i_upper_level = 0;
3132 msg_Dbg( &sys.demuxer, "| + Tracks" );
3134 /* Master elements */
3135 tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
3137 for( i = 0; i < tracks->ListSize(); i++ )
3139 EbmlElement *l = (*tracks)[i];
3141 if( MKV_IS_ID( l, KaxTrackEntry ) )
3143 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
3147 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3152 /*****************************************************************************
3154 *****************************************************************************/
3155 void matroska_segment_c::ParseInfo( KaxInfo *info )
3160 int i_upper_level = 0;
3162 msg_Dbg( &sys.demuxer, "| + Information" );
3164 /* Master elements */
3165 m = static_cast<EbmlMaster *>(info);
3166 m->Read( es, info->Generic().Context, i_upper_level, el, true );
3168 for( i = 0; i < m->ListSize(); i++ )
3170 EbmlElement *l = (*m)[i];
3172 if( MKV_IS_ID( l, KaxSegmentUID ) )
3174 segment_uid = *(new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l)));
3176 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)segment_uid.GetBuffer() );
3178 else if( MKV_IS_ID( l, KaxPrevUID ) )
3180 prev_segment_uid = *(new KaxPrevUID(*static_cast<KaxPrevUID*>(l)));
3182 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)prev_segment_uid.GetBuffer() );
3184 else if( MKV_IS_ID( l, KaxNextUID ) )
3186 next_segment_uid = *(new KaxNextUID(*static_cast<KaxNextUID*>(l)));
3188 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)next_segment_uid.GetBuffer() );
3190 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
3192 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
3194 i_timescale = uint64(tcs);
3196 msg_Dbg( &sys.demuxer, "| | + TimecodeScale="I64Fd,
3199 else if( MKV_IS_ID( l, KaxDuration ) )
3201 KaxDuration &dur = *(KaxDuration*)l;
3203 i_duration = mtime_t( double( dur ) );
3205 msg_Dbg( &sys.demuxer, "| | + Duration="I64Fd,
3208 else if( MKV_IS_ID( l, KaxMuxingApp ) )
3210 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
3212 psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
3214 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
3215 psz_muxing_application );
3217 else if( MKV_IS_ID( l, KaxWritingApp ) )
3219 KaxWritingApp &wapp = *(KaxWritingApp*)l;
3221 psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
3223 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
3224 psz_writing_application );
3226 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
3228 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
3230 psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
3232 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
3233 psz_segment_filename );
3235 else if( MKV_IS_ID( l, KaxTitle ) )
3237 KaxTitle &title = *(KaxTitle*)l;
3239 psz_title = UTF8ToStr( UTFstring( title ) );
3241 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
3243 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
3245 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
3247 families.push_back(*uid);
3249 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
3251 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
3252 else if( MKV_IS_ID( l, KaxDateUTC ) )
3254 KaxDateUTC &date = *(KaxDateUTC*)l;
3259 i_date = date.GetEpochDate();
3260 memset( buffer, 0, 256 );
3261 if( gmtime_r( &i_date, &tmres ) &&
3262 asctime_r( &tmres, buffer ) )
3264 buffer[strlen( buffer)-1]= '\0';
3265 psz_date_utc = strdup( buffer );
3266 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
3270 #if LIBMATROSKA_VERSION >= 0x000704
3271 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
3273 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
3274 chapter_translation_c translated;
3276 p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
3277 for( j = 0; j < p_trans->ListSize(); j++ )
3279 EbmlElement *l = (*p_trans)[j];
3281 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
3283 translated.editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
3285 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
3287 translated.codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
3289 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
3291 translated.translated = *( new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) ) );
3295 translations.push_back( translated );
3300 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3304 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
3305 i_duration = mtime_t(f_dur);
3309 /*****************************************************************************
3311 *****************************************************************************/
3312 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
3316 if( sys.title == NULL )
3318 sys.title = vlc_input_title_New();
3321 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
3322 for( i = 0; i < ca->ListSize(); i++ )
3324 EbmlElement *l = (*ca)[i];
3326 if( MKV_IS_ID( l, KaxChapterUID ) )
3328 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
3329 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
3331 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
3333 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
3334 chapters.b_display_seekpoint = uint8( flag ) == 0;
3336 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
3338 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
3340 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
3341 chapters.i_start_time = uint64( start ) / I64C(1000);
3343 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
3345 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
3347 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
3348 chapters.i_end_time = uint64( end ) / I64C(1000);
3350 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
3352 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
3354 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
3356 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
3357 for( j = 0; j < cd->ListSize(); j++ )
3359 EbmlElement *l= (*cd)[j];
3361 if( MKV_IS_ID( l, KaxChapterString ) )
3365 KaxChapterString &name =*(KaxChapterString*)l;
3366 for (k = 0; k < i_level; k++)
3367 chapters.psz_name += '+';
3368 chapters.psz_name += ' ';
3369 chapters.psz_name += UTF8ToStr( UTFstring( name ) );
3371 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", UTF8ToStr(UTFstring(name)) );
3373 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
3375 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
3376 const char *psz = string( lang ).c_str();
3378 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
3380 else if( MKV_IS_ID( l, KaxChapterCountry ) )
3382 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
3383 const char *psz = string( ct ).c_str();
3385 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
3389 else if( MKV_IS_ID( l, KaxChapterProcess ) )
3391 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
3393 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
3394 chapter_codec_cmds_c *p_ccodec = NULL;
3396 for( j = 0; j < cp->ListSize(); j++ )
3398 EbmlElement *k= (*cp)[j];
3400 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
3402 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
3403 if ( uint32(*p_codec_id) == 0 )
3404 p_ccodec = new matroska_script_codec_c();
3405 else if ( uint32(*p_codec_id) == 1 )
3406 p_ccodec = new dvd_chapter_codec_c( sys );
3411 if ( p_ccodec != NULL )
3413 for( j = 0; j < cp->ListSize(); j++ )
3415 EbmlElement *k= (*cp)[j];
3417 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
3419 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
3420 p_ccodec->SetPrivate( *p_private );
3422 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
3424 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
3427 chapters.codecs.push_back( p_ccodec );
3430 else if( MKV_IS_ID( l, KaxChapterAtom ) )
3432 chapter_item_c *new_sub_chapter = new chapter_item_c();
3433 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
3434 new_sub_chapter->psz_parent = &chapters;
3435 chapters.sub_chapters.push_back( new_sub_chapter );
3440 /*****************************************************************************
3442 *****************************************************************************/
3443 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
3447 int i_upper_level = 0;
3450 /* Master elements */
3451 chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
3453 for( i = 0; i < chapters->ListSize(); i++ )
3455 EbmlElement *l = (*chapters)[i];
3457 if( MKV_IS_ID( l, KaxEditionEntry ) )
3459 chapter_edition_c *p_edition = new chapter_edition_c();
3461 EbmlMaster *E = static_cast<EbmlMaster *>(l );
3463 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
3464 for( j = 0; j < E->ListSize(); j++ )
3466 EbmlElement *l = (*E)[j];
3468 if( MKV_IS_ID( l, KaxChapterAtom ) )
3470 chapter_item_c *new_sub_chapter = new chapter_item_c();
3471 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
3472 p_edition->sub_chapters.push_back( new_sub_chapter );
3474 else if( MKV_IS_ID( l, KaxEditionUID ) )
3476 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
3478 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
3480 p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
3482 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
3484 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
3485 i_default_edition = stored_editions.size();
3489 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
3492 stored_editions.push_back( p_edition );
3496 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3500 for( i = 0; i < stored_editions.size(); i++ )
3502 stored_editions[i]->RefreshChapters( );
3505 if ( stored_editions[i_default_edition]->b_ordered )
3507 /* update the duration of the segment according to the sum of all sub chapters */
3508 i_dur = stored_editions[i_default_edition]->Duration() / I64C(1000);
3514 void matroska_segment_c::ParseCluster( )
3519 int i_upper_level = 0;
3521 /* Master elements */
3522 m = static_cast<EbmlMaster *>( cluster );
3523 m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
3525 for( i = 0; i < m->ListSize(); i++ )
3527 EbmlElement *l = (*m)[i];
3529 if( MKV_IS_ID( l, KaxClusterTimecode ) )
3531 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
3533 cluster->InitTimecode( uint64( ctc ), i_timescale );
3538 i_start_time = cluster->GlobalTimecode() / 1000;
3541 /*****************************************************************************
3542 * InformationCreate:
3543 *****************************************************************************/
3544 void matroska_segment_c::InformationCreate( )
3548 sys.meta = vlc_meta_New();
3552 vlc_meta_Add( sys.meta, VLC_META_TITLE, psz_title );
3556 vlc_meta_Add( sys.meta, VLC_META_DATE, psz_date_utc );
3558 if( psz_segment_filename )
3560 vlc_meta_Add( sys.meta, _("Segment filename"), psz_segment_filename );
3562 if( psz_muxing_application )
3564 vlc_meta_Add( sys.meta, _("Muxing application"), psz_muxing_application );
3566 if( psz_writing_application )
3568 vlc_meta_Add( sys.meta, _("Writing application"), psz_writing_application );
3571 for( i_track = 0; i_track < tracks.size(); i_track++ )
3573 mkv_track_t *tk = tracks[i_track];
3574 vlc_meta_t *mtk = vlc_meta_New();
3576 sys.meta->track = (vlc_meta_t**)realloc( sys.meta->track,
3577 sizeof( vlc_meta_t * ) * ( sys.meta->i_track + 1 ) );
3578 sys.meta->track[sys.meta->i_track++] = mtk;
3580 if( tk->fmt.psz_description )
3582 vlc_meta_Add( sys.meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
3584 if( tk->psz_codec_name )
3586 vlc_meta_Add( sys.meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
3588 if( tk->psz_codec_settings )
3590 vlc_meta_Add( sys.meta, VLC_META_SETTING, tk->psz_codec_settings );
3592 if( tk->psz_codec_info_url )
3594 vlc_meta_Add( sys.meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
3596 if( tk->psz_codec_download_url )
3598 vlc_meta_Add( sys.meta, VLC_META_URL, tk->psz_codec_download_url );
3602 if( i_tags_position >= 0 )
3604 vlc_bool_t b_seekable;
3606 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
3615 /*****************************************************************************
3617 *****************************************************************************/
3619 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
3621 #define idx index[i_index]
3623 idx.i_block_number= -1;
3624 idx.i_position = cluster->GetElementPosition();
3626 idx.b_key = VLC_TRUE;
3629 if( i_index >= i_index_max )
3631 i_index_max += 1024;
3632 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
3637 static char * UTF8ToStr( const UTFstring &u )
3646 p = dst = (char*)malloc( i_src + 1);
3665 void chapter_edition_c::RefreshChapters( )
3667 chapter_item_c::RefreshChapters( b_ordered, -1 );
3668 b_display_seekpoint = false;
3671 int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_time )
3673 int64_t i_user_time = i_prev_user_time;
3675 // first the sub-chapters, and then ourself
3676 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
3677 while ( index != sub_chapters.end() )
3679 i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
3685 // the ordered chapters always start at zero
3686 if ( i_prev_user_time == -1 )
3688 if ( i_user_time == -1 )
3690 i_prev_user_time = 0;
3693 i_user_start_time = i_prev_user_time;
3694 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
3696 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
3700 i_user_end_time = i_user_time;
3705 std::sort( sub_chapters.begin(), sub_chapters.end() );
3706 i_user_start_time = i_start_time;
3707 if ( i_end_time != -1 )
3708 i_user_end_time = i_end_time;
3709 else if ( i_user_time != -1 )
3710 i_user_end_time = i_user_time;
3712 i_user_end_time = i_user_start_time;
3715 return i_user_end_time;
3718 mtime_t chapter_edition_c::Duration() const
3720 mtime_t i_result = 0;
3722 if ( sub_chapters.size() )
3724 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
3726 i_result = (*index)->i_user_end_time;
3732 chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode )
3734 chapter_item_c *psz_result = NULL;
3736 if ( i_user_timecode >= i_user_start_time &&
3737 ( i_user_timecode < i_user_end_time ||
3738 ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
3740 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
3741 while ( index != sub_chapters.end() && psz_result == NULL )
3743 psz_result = (*index)->FindTimecode( i_user_timecode );
3747 if ( psz_result == NULL )
3754 void demux_sys_t::PreloadFamily( const matroska_segment_c & of_segment )
3756 for (size_t i=0; i<opened_segments.size(); i++)
3758 opened_segments[i]->PreloadFamily( of_segment );
3761 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
3766 for (size_t i=0; i<families.size(); i++)
3768 for (size_t j=0; j<of_segment.families.size(); j++)
3770 if ( families[i] == of_segment.families[j] )
3778 // preload all the linked segments for all preloaded segments
3779 void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
3781 size_t i_preloaded, i;
3782 virtual_segment_c *p_seg;
3784 p_current_segment = VirtualFromSegments( p_segment );
3786 used_segments.push_back( p_current_segment );
3788 // create all the other virtual segments of the family
3791 for ( i=0; i< opened_segments.size(); i++ )
3793 if ( opened_segments[i]->b_preloaded && !IsUsedSegment( *opened_segments[i] ) )
3795 p_seg = VirtualFromSegments( opened_segments[i] );
3796 used_segments.push_back( p_seg );
3800 } while ( i_preloaded ); // worst case: will stop when all segments are found as family related
3802 // TODO publish all editions of all usable segment
3805 bool demux_sys_t::IsUsedSegment( matroska_segment_c &segment ) const
3807 for ( size_t i=0; i< used_segments.size(); i++ )
3809 if ( used_segments[i]->FindUID( segment.segment_uid ) )
3815 virtual_segment_c *demux_sys_t::VirtualFromSegments( matroska_segment_c *p_segment ) const
3817 size_t i_preloaded, i;
3819 virtual_segment_c *p_result = new virtual_segment_c( p_segment );
3821 // fill our current virtual segment with all hard linked segments
3824 for ( i=0; i< opened_segments.size(); i++ )
3826 i_preloaded += p_result->AddSegment( opened_segments[i] );
3828 } while ( i_preloaded ); // worst case: will stop when all segments are found as linked
3832 p_result->PreloadLinked( );
3834 p_result->PrepareChapters( );
3839 bool demux_sys_t::PreparePlayback( )
3841 p_current_segment->LoadCues();
3842 f_duration = p_current_segment->Duration();
3844 /* add information */
3845 p_current_segment->Segment()->InformationCreate( );
3847 p_current_segment->Segment()->Select( 0 );
3849 return p_current_segment->Select( *title );
3852 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
3854 EbmlBinary * p_itema = (EbmlBinary *)(&p_item_a->segment_uid);
3855 if ( *p_itema == p_item_b->prev_segment_uid )
3858 p_itema = (EbmlBinary *)(&p_item_a->next_segment_uid);
3859 if ( *p_itema == p_item_b->segment_uid )
3862 if ( *p_itema == p_item_b->prev_segment_uid )
3868 bool matroska_segment_c::Preload( )
3873 EbmlElement *el = NULL;
3875 ep->Reset( &sys.demuxer );
3877 while( ( el = ep->Get() ) != NULL )
3879 if( MKV_IS_ID( el, KaxInfo ) )
3881 ParseInfo( static_cast<KaxInfo*>( el ) );
3883 else if( MKV_IS_ID( el, KaxTracks ) )
3885 ParseTracks( static_cast<KaxTracks*>( el ) );
3887 else if( MKV_IS_ID( el, KaxSeekHead ) )
3889 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
3891 else if( MKV_IS_ID( el, KaxCues ) )
3893 msg_Dbg( &sys.demuxer, "| + Cues" );
3895 else if( MKV_IS_ID( el, KaxCluster ) )
3897 msg_Dbg( &sys.demuxer, "| + Cluster" );
3899 cluster = (KaxCluster*)el;
3901 i_start_pos = cluster->GetElementPosition();
3905 /* stop parsing the stream */
3908 else if( MKV_IS_ID( el, KaxAttachments ) )
3910 msg_Dbg( &sys.demuxer, "| + Attachments FIXME (but probably never supported)" );
3912 else if( MKV_IS_ID( el, KaxChapters ) )
3914 msg_Dbg( &sys.demuxer, "| + Chapters" );
3915 ParseChapters( static_cast<KaxChapters*>( el ) );
3917 else if( MKV_IS_ID( el, KaxTag ) )
3919 msg_Dbg( &sys.demuxer, "| + Tags FIXME TODO" );
3923 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid(*el).name() );
3932 matroska_segment_c *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
3934 for (size_t i=0; i<opened_segments.size(); i++)
3936 if ( opened_segments[i]->segment_uid == uid )
3937 return opened_segments[i];
3942 chapter_item_c *demux_sys_t::BrowseCodecPrivate( unsigned int codec_id,
3943 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3944 const void *p_cookie,
3945 size_t i_cookie_size,
3946 virtual_segment_c * &p_segment_found )
3948 chapter_item_c *p_result = NULL;
3949 for (size_t i=0; i<opened_segments.size(); i++)
3951 p_result = used_segments[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3952 if ( p_result != NULL )
3954 p_segment_found = used_segments[i];
3961 void virtual_segment_c::Sort()
3963 // keep the current segment index
3964 matroska_segment_c *p_segment = linked_segments[i_current_segment];
3966 std::sort( linked_segments.begin(), linked_segments.end(), matroska_segment_c::CompareSegmentUIDs );
3968 for ( i_current_segment=0; i_current_segment<linked_segments.size(); i_current_segment++)
3969 if ( linked_segments[i_current_segment] == p_segment )
3973 size_t virtual_segment_c::AddSegment( matroska_segment_c *p_segment )
3976 // check if it's not already in here
3977 for ( i=0; i<linked_segments.size(); i++ )
3979 if ( p_segment->segment_uid == linked_segments[i]->segment_uid )
3983 // find possible mates
3984 for ( i=0; i<linked_uids.size(); i++ )
3986 if ( p_segment->segment_uid == linked_uids[i]
3987 || p_segment->prev_segment_uid == linked_uids[i]
3988 || p_segment->next_segment_uid == linked_uids[i] )
3990 linked_segments.push_back( p_segment );
3992 AppendUID( p_segment->prev_segment_uid );
3993 AppendUID( p_segment->next_segment_uid );
4001 void virtual_segment_c::PreloadLinked( )
4003 for ( size_t i=0; i<linked_segments.size(); i++ )
4005 linked_segments[i]->Preload( );
4007 i_current_edition = linked_segments[0]->i_default_edition;
4010 mtime_t virtual_segment_c::Duration() const
4013 if ( linked_segments.size() == 0 )
4016 matroska_segment_c *p_last_segment = linked_segments[linked_segments.size()-1];
4017 // p_last_segment->ParseCluster( );
4019 i_duration = p_last_segment->i_start_time / 1000 + p_last_segment->i_duration;
4024 void virtual_segment_c::LoadCues( )
4026 for ( size_t i=0; i<linked_segments.size(); i++ )
4028 linked_segments[i]->LoadCues();
4032 void virtual_segment_c::AppendUID( const EbmlBinary & UID )
4034 if ( UID.GetBuffer() == NULL )
4037 for (size_t i=0; i<linked_uids.size(); i++)
4039 if ( UID == linked_uids[i] )
4042 linked_uids.push_back( *(KaxSegmentUID*)(&UID) );
4045 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset )
4048 int i_track_skipping;
4049 int64_t i_block_duration;
4050 int64_t i_block_ref1;
4051 int64_t i_block_ref2;
4053 int64_t i_seek_position = i_start_pos;
4054 int64_t i_seek_time = i_start_time;
4060 for( ; i_idx < i_index; i_idx++ )
4062 if( index[i_idx].i_time + i_time_offset > i_date )
4073 i_seek_position = index[i_idx].i_position;
4074 i_seek_time = index[i_idx].i_time;
4077 msg_Dbg( &sys.demuxer, "seek got "I64Fd" (%d%%)",
4078 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
4080 es.I_O().setFilePointer( i_seek_position, seek_beginning );
4083 ep = new EbmlParser( &es, segment, &sys.demuxer );
4086 sys.i_start_pts = i_date;
4088 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
4090 /* now parse until key frame */
4091 #define tk tracks[i_track]
4092 i_track_skipping = 0;
4093 for( i_track = 0; i_track < tracks.size(); i_track++ )
4095 if( tk->fmt.i_cat == VIDEO_ES )
4097 tk->b_search_keyframe = VLC_TRUE;
4100 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_date );
4104 while( i_track_skipping > 0 )
4106 if( BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
4108 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
4113 for( i_track = 0; i_track < tracks.size(); i_track++ )
4115 if( tk->i_number == block->TrackNum() )
4121 sys.i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
4123 if( i_track < tracks.size() )
4125 if( sys.i_pts >= sys.i_start_pts )
4127 BlockDecode( &sys.demuxer, block, sys.i_pts, 0 );
4128 i_track_skipping = 0;
4130 else if( tk->fmt.i_cat == VIDEO_ES )
4132 if( i_block_ref1 == -1 && tk->b_search_keyframe )
4134 tk->b_search_keyframe = VLC_FALSE;
4137 if( !tk->b_search_keyframe )
4139 BlockDecode( &sys.demuxer, block, sys.i_pts, 0 );
4149 void virtual_segment_c::Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter )
4151 demux_sys_t *p_sys = demuxer.p_sys;
4154 // find the actual time for an ordered edition
4155 if ( psz_chapter == NULL )
4157 if ( EditionIsOrdered() )
4159 /* 1st, we need to know in which chapter we are */
4160 psz_chapter = (*p_editions)[i_current_edition]->FindTimecode( i_date );
4164 if ( psz_chapter != NULL )
4166 psz_current_chapter = psz_chapter;
4167 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
4168 demuxer.info.i_update |= INPUT_UPDATE_SEEKPOINT;
4169 demuxer.info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
4172 // find the best matching segment
4173 for ( i=0; i<linked_segments.size(); i++ )
4175 if ( i_date < linked_segments[i]->i_start_time )
4182 if ( i_current_segment != i )
4184 linked_segments[i_current_segment]->UnSelect();
4185 linked_segments[i]->Select( i_date );
4186 i_current_segment = i;
4189 linked_segments[i]->Seek( i_date, i_time_offset );
4192 void chapter_codec_cmds_c::AddCommand( const KaxChapterProcessCommand & command )
4196 uint32 codec_time = uint32(-1);
4197 for( i = 0; i < command.ListSize(); i++ )
4199 const EbmlElement *k = command[i];
4201 if( MKV_IS_ID( k, KaxChapterProcessTime ) )
4203 codec_time = uint32( *static_cast<const KaxChapterProcessTime*>( k ) );
4208 for( i = 0; i < command.ListSize(); i++ )
4210 const EbmlElement *k = command[i];
4212 if( MKV_IS_ID( k, KaxChapterProcessData ) )
4214 KaxChapterProcessData *p_data = new KaxChapterProcessData( *static_cast<const KaxChapterProcessData*>( k ) );
4215 switch ( codec_time )
4218 during_cmds.push_back( *p_data );
4221 enter_cmds.push_back( *p_data );
4224 leave_cmds.push_back( *p_data );
4233 bool chapter_item_c::Enter()
4235 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
4236 while ( index != codecs.end() )
4241 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
4242 while ( index_ != sub_chapters.end() )
4250 bool chapter_item_c::Leave()
4252 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
4253 while ( index != codecs.end() )
4258 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
4259 while ( index_ != sub_chapters.end() )
4267 bool dvd_chapter_codec_c::Enter()
4269 std::vector<KaxChapterProcessData>::iterator index = enter_cmds.begin();
4270 while ( index != enter_cmds.end() )
4272 if ( (*index).GetSize() )
4274 binary *p_data = (*index).GetBuffer();
4275 size_t i_size = *p_data++;
4276 // avoid reading too much from the buffer
4277 i_size = min( i_size, ((*index).GetSize() - 1) >> 3 );
4278 for ( ; i_size > 0; i_size--, p_data += 8 )
4280 interpretor.Interpret( p_data );
4288 bool dvd_chapter_codec_c::Leave()
4290 std::vector<KaxChapterProcessData>::iterator index = leave_cmds.begin();
4291 while ( index != leave_cmds.end() )
4293 if ( (*index).GetSize() )
4295 binary *p_data = (*index).GetBuffer();
4296 size_t i_size = *p_data++;
4297 // avoid reading too much from the buffer
4298 i_size = min( i_size, ((*index).GetSize() - 1) >> 3 );
4299 for ( ; i_size > 0; i_size--, p_data += 8 )
4301 interpretor.Interpret( p_data );
4309 bool dvd_command_interpretor_c::Interpret( const binary * p_command, size_t i_size )
4314 uint16 i_command = ( p_command[0] << 8 ) + p_command[1];
4316 switch ( i_command )
4320 uint8 i_title = p_command[5];
4321 msg_Dbg( &sys.demuxer, "DVD command: JumpTT %d", i_title );
4323 // find in the ChapProcessPrivate matching this Title level
4324 virtual_segment_c *p_segment;
4325 chapter_item_c *p_chapter;
4326 p_chapter = sys.BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title), p_segment );
4327 if ( p_chapter != NULL )
4329 // if the segment is not part of the current segment, select the new one
4330 if ( p_segment != sys.p_current_segment )
4332 sys.p_current_segment = p_segment;
4333 sys.PreparePlayback();
4338 // jump to the location in the found segment
4339 p_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
4344 case CMD_CALLSS_VTSM:
4346 msg_Dbg( &sys.demuxer, "DVD command: CallSS VTSM" );
4351 msg_Dbg( &sys.demuxer, "DVD command: unsupported %02X %02X %02X %02X %02X %02X %02X %02X"
4367 bool dvd_command_interpretor_c::MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
4369 if ( i_cookie_size != 1 || data.m_private_data.GetSize() < 4 )
4372 if ( data.m_private_data.GetBuffer()[0] != 0x28 )
4375 uint16 i_gtitle = (data.m_private_data.GetBuffer()[1] << 8 ) + data.m_private_data.GetBuffer()[2];
4376 uint8 i_title = *(uint8*)p_cookie;
4378 return (i_gtitle == i_title);