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( bool f_for_title = false ) 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( bool f_for_title = false ) 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( bool f_for_title = false ) 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 );
531 std::string GetMainName() const;
536 class matroska_segment_c
539 matroska_segment_c( demux_sys_t & demuxer, EbmlStream & estream )
542 ,i_timescale(MKVD_TIMECODESCALE)
546 ,i_chapters_position(-1)
553 ,psz_muxing_application(NULL)
554 ,psz_writing_application(NULL)
555 ,psz_segment_filename(NULL)
558 ,i_default_edition(0)
563 index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
566 virtual ~matroska_segment_c()
568 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
570 #define tk tracks[i_track]
571 if( tk->fmt.psz_description )
573 free( tk->fmt.psz_description );
577 free( tk->psz_codec );
579 if( tk->fmt.psz_language )
581 free( tk->fmt.psz_language );
587 if( psz_writing_application )
589 free( psz_writing_application );
591 if( psz_muxing_application )
593 free( psz_muxing_application );
595 if( psz_segment_filename )
597 free( psz_segment_filename );
605 free( psz_date_utc );
612 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
613 while ( index != stored_editions.end() )
624 uint64_t i_timescale;
626 /* duration of the segment */
628 mtime_t i_start_time;
631 std::vector<mkv_track_t*> tracks;
634 int64_t i_cues_position;
635 int64_t i_chapters_position;
636 int64_t i_tags_position;
640 KaxSegmentUID segment_uid;
641 KaxPrevUID prev_segment_uid;
642 KaxNextUID next_segment_uid;
650 char *psz_muxing_application;
651 char *psz_writing_application;
652 char *psz_segment_filename;
656 /* !!!!! GCC 3.3 bug on Darwin !!!!! */
657 /* when you remove this variable the compiler issues an atomicity error */
658 /* this variable only works when using std::vector<chapter_edition_c> */
659 std::vector<chapter_edition_c*> stored_editions;
660 int i_default_edition;
662 std::vector<chapter_translation_c> translations;
663 std::vector<KaxSegmentFamily> families;
670 bool PreloadFamily( const matroska_segment_c & segment );
671 void ParseInfo( KaxInfo *info );
672 void ParseChapters( KaxChapters *chapters );
673 void ParseSeekHead( KaxSeekHead *seekhead );
674 void ParseTracks( KaxTracks *tracks );
675 void ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters );
676 void ParseTrackEntry( KaxTrackEntry *m );
677 void ParseCluster( );
678 void IndexAppendCluster( KaxCluster *cluster );
681 void InformationCreate( );
682 void Seek( mtime_t i_date, mtime_t i_time_offset );
683 int BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration );
684 bool Select( mtime_t i_start_time );
687 static bool CompareSegmentUIDs( const matroska_segment_c * item_a, const matroska_segment_c * item_b );
690 // class holding hard-linked segment together in the playback order
691 class virtual_segment_c
694 virtual_segment_c( matroska_segment_c *p_segment )
695 :i_current_segment(0)
697 ,i_current_edition(-1)
698 ,psz_current_chapter(NULL)
701 linked_segments.push_back( p_segment );
703 AppendUID( p_segment->segment_uid );
704 AppendUID( p_segment->prev_segment_uid );
705 AppendUID( p_segment->next_segment_uid );
709 size_t AddSegment( matroska_segment_c *p_segment );
710 void PreloadLinked( );
711 mtime_t Duration( ) const;
713 void Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter );
715 inline chapter_edition_c *Edition()
717 if ( i_current_edition >= 0 && size_t(i_current_edition) < p_editions->size() )
718 return (*p_editions)[i_current_edition];
722 matroska_segment_c * Segment() const
724 if ( linked_segments.size() == 0 || i_current_segment >= linked_segments.size() )
726 return linked_segments[i_current_segment];
729 inline const chapter_item_c *CurrentChapter() const {
730 return psz_current_chapter;
735 if ( i_current_segment < linked_segments.size()-1 )
743 bool FindUID( KaxSegmentUID & uid ) const
745 for ( size_t i=0; i<linked_uids.size(); i++ )
747 if ( linked_uids[i] == uid )
753 void UpdateCurrentToChapter( demux_t & demux );
754 void PrepareChapters( );
756 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
757 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
758 const void *p_cookie,
759 size_t i_cookie_size );
761 std::vector<chapter_edition_c*> *p_editions;
765 std::vector<matroska_segment_c*> linked_segments;
766 std::vector<KaxSegmentUID> linked_uids;
767 size_t i_current_segment;
769 int i_current_edition;
770 chapter_item_c *psz_current_chapter;
772 void AppendUID( const EbmlBinary & UID );
775 class matroska_stream_c
778 matroska_stream_c( demux_sys_t & demuxer )
784 virtual ~matroska_stream_c()
793 std::vector<matroska_segment_c*> segments;
801 demux_sys_t( demux_t & demux )
808 ,p_current_segment(NULL)
812 virtual ~demux_sys_t()
815 for ( i=0; i<streams.size(); i++ )
817 for ( i=0; i<opened_segments.size(); i++ )
818 delete opened_segments[i];
819 for ( i=0; i<used_segments.size(); i++ )
820 delete used_segments[i];
828 mtime_t i_chapter_time;
832 std::vector<input_title_t> titles; // matroska editions
833 size_t i_current_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->titles.size() )
1067 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1068 int *pi_int = (int*)va_arg( args, int* );
1070 *pi_int = p_sys->titles.size();
1071 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->titles.size() );
1073 for( size_t i = 0; i < p_sys->titles.size(); i++ )
1075 (*ppp_title)[i] = vlc_input_title_Duplicate( &p_sys->titles[i] );
1080 return VLC_EGENERIC;
1082 case DEMUX_SET_TITLE:
1083 /* TODO handle editions as titles */
1084 if( p_sys->titles.size() && p_sys->titles[p_sys->i_current_title].i_seekpoint > 0 )
1088 return VLC_EGENERIC;
1090 case DEMUX_SET_SEEKPOINT:
1091 i_skp = (int)va_arg( args, int );
1093 if( p_sys->titles.size() && i_skp < p_sys->titles[p_sys->i_current_title].i_seekpoint)
1095 Seek( p_demux, (int64_t)p_sys->titles[p_sys->i_current_title].seekpoint[i_skp]->i_time_offset, -1, NULL);
1096 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
1097 p_demux->info.i_seekpoint = i_skp;
1100 return VLC_EGENERIC;
1102 case DEMUX_SET_TIME:
1105 return VLC_EGENERIC;
1109 int matroska_segment_c::BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1121 i_level = ep->GetLevel();
1123 if( el == NULL && *pp_block != NULL )
1125 /* update the index */
1126 #define idx index[i_index - 1]
1127 if( i_index > 0 && idx.i_time == -1 )
1129 idx.i_time = (*pp_block)->GlobalTimecode() / (mtime_t)1000;
1130 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1138 if( ep->GetLevel() > 1 )
1143 msg_Warn( &sys.demuxer, "EOF" );
1144 return VLC_EGENERIC;
1150 if( MKV_IS_ID( el, KaxCluster ) )
1152 cluster = (KaxCluster*)el;
1154 /* add it to the index */
1156 ( i_index > 0 && index[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1158 IndexAppendCluster( cluster );
1161 // reset silent tracks
1162 for (size_t i=0; i<tracks.size(); i++)
1164 tracks[i]->b_silent = VLC_FALSE;
1169 else if( MKV_IS_ID( el, KaxCues ) )
1171 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1172 return VLC_EGENERIC;
1176 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1179 else if( i_level == 2 )
1181 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1183 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1185 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1186 cluster->InitTimecode( uint64( ctc ), i_timescale );
1188 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1192 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1197 else if( i_level == 3 )
1199 if( MKV_IS_ID( el, KaxBlock ) )
1201 *pp_block = (KaxBlock*)el;
1203 (*pp_block)->ReadData( es.I_O() );
1204 (*pp_block)->SetParent( *cluster );
1208 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1210 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1212 dur.ReadData( es.I_O() );
1213 *pi_duration = uint64( dur );
1215 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1217 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1219 ref.ReadData( es.I_O() );
1220 if( *pi_ref1 == -1 )
1222 *pi_ref1 = int64( ref );
1226 *pi_ref2 = int64( ref );
1229 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1231 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1232 track_num.ReadData( es.I_O() );
1234 for (size_t i=0; i<tracks.size(); i++)
1236 if ( tracks[i]->i_number == uint32(track_num))
1238 tracks[i]->b_silent = VLC_TRUE;
1246 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1247 return VLC_EGENERIC;
1252 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1255 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1256 memcpy( p_block->p_buffer, p_mem, i_mem );
1257 //p_block->i_rate = p_input->stream.control.i_rate;
1261 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1262 mtime_t i_duration )
1264 demux_sys_t *p_sys = p_demux->p_sys;
1265 matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
1271 #define tk p_segment->tracks[i_track]
1272 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1274 if( tk->i_number == block->TrackNum() )
1280 if( i_track >= p_segment->tracks.size() )
1282 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1285 if( tk->p_es == NULL )
1287 msg_Err( p_demux, "unknown track number=%d", block->TrackNum() );
1290 if( i_pts < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1292 return; /* discard audio packets that shouldn't be rendered */
1295 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1298 tk->b_inited = VLC_FALSE;
1302 /* First send init data */
1303 if( !tk->b_inited && tk->i_data_init > 0 )
1307 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
1308 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init );
1309 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
1311 tk->b_inited = VLC_TRUE;
1314 for( i = 0; i < block->NumberFrames(); i++ )
1317 DataBuffer &data = block->GetBuffer(i);
1319 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1321 if( p_block == NULL )
1326 #if defined(HAVE_ZLIB_H)
1327 if( tk->i_compression_type )
1329 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
1333 // TODO implement correct timestamping when B frames are used
1334 if( tk->fmt.i_cat != VIDEO_ES )
1336 p_block->i_dts = p_block->i_pts = i_pts;
1340 p_block->i_dts = i_pts;
1344 if( tk->fmt.i_cat == SPU_ES && strcmp( tk->psz_codec, "S_VOBSUB" ) )
1346 p_block->i_length = i_duration * 1000;
1349 es_out_Send( p_demux->out, tk->p_es, p_block );
1351 /* use time stamp only for first block */
1358 matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( EbmlStream *p_estream )
1360 int i_upper_lvl = 0;
1362 EbmlElement *p_l0, *p_l1, *p_l2;
1363 bool b_keep_stream = false, b_keep_segment;
1365 // verify the EBML Header
1366 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
1371 p_l0->SkipData(*p_estream, EbmlHead_Context);
1374 // find all segments in this file
1375 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1381 matroska_stream_c *p_stream1 = new matroska_stream_c( *this );
1385 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
1388 matroska_segment_c *p_segment1 = new matroska_segment_c( *this, *p_estream );
1389 b_keep_segment = false;
1391 ep = new EbmlParser(p_estream, p_l0, &demuxer );
1392 p_segment1->ep = ep;
1393 p_segment1->segment = (KaxSegment*)p_l0;
1395 while ((p_l1 = ep->Get()))
1397 if (MKV_IS_ID(p_l1, KaxInfo))
1399 // find the families of this segment
1400 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
1402 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
1403 for( i = 0; i < p_info->ListSize(); i++ )
1405 EbmlElement *l = (*p_info)[i];
1407 if( MKV_IS_ID( l, KaxSegmentUID ) )
1409 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
1410 b_keep_segment = (FindSegment( *p_uid ) == NULL);
1411 if ( !b_keep_segment )
1412 break; // this segment is already known
1413 opened_segments.push_back( p_segment1 );
1414 p_segment1->segment_uid = *( new KaxSegmentUID(*p_uid) );
1416 else if( MKV_IS_ID( l, KaxPrevUID ) )
1418 p_segment1->prev_segment_uid = *( new KaxPrevUID( *static_cast<KaxPrevUID*>(l) ) );
1420 else if( MKV_IS_ID( l, KaxNextUID ) )
1422 p_segment1->next_segment_uid = *( new KaxNextUID( *static_cast<KaxNextUID*>(l) ) );
1424 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
1426 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
1427 p_segment1->families.push_back( *p_fam );
1433 if ( b_keep_segment )
1435 b_keep_stream = true;
1436 p_stream1->segments.push_back( p_segment1 );
1442 p_l0->SkipData(*p_estream, EbmlHead_Context);
1443 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1446 if ( !b_keep_stream )
1455 bool matroska_segment_c::Select( mtime_t i_start_time )
1460 msg_Dbg( &sys.demuxer, "found %d es", tracks.size() );
1461 for( i_track = 0; i_track < tracks.size(); i_track++ )
1463 #define tk tracks[i_track]
1464 if( tk->fmt.i_cat == UNKNOWN_ES )
1466 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", i_track, tk->i_number );
1471 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
1473 if( tk->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
1475 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
1476 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1480 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk->p_extra_data;
1482 tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1483 tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1484 tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1486 tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
1487 if( tk->fmt.i_extra > 0 )
1489 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1490 memcpy( tk->fmt.p_extra, &p_bih[1], tk->fmt.i_extra );
1494 else if( !strcmp( tk->psz_codec, "V_MPEG1" ) ||
1495 !strcmp( tk->psz_codec, "V_MPEG2" ) )
1497 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1499 else if( !strncmp( tk->psz_codec, "V_MPEG4", 7 ) )
1501 if( !strcmp( tk->psz_codec, "V_MPEG4/MS/V3" ) )
1503 tk->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1505 else if( !strcmp( tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1507 tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1508 tk->fmt.b_packetized = VLC_FALSE;
1509 tk->fmt.i_extra = tk->i_extra_data;
1510 tk->fmt.p_extra = malloc( tk->i_extra_data );
1511 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
1515 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1518 else if( !strcmp( tk->psz_codec, "V_QUICKTIME" ) )
1520 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
1522 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1526 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1531 MP4_ReadBoxCommon( p_mp4_stream, p_box );
1532 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
1533 tk->fmt.i_codec = p_box->i_type;
1534 tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1535 tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1536 tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1537 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1538 memcpy( tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tk->fmt.i_extra );
1539 MP4_FreeBox_sample_vide( p_box );
1541 stream_MemoryDelete( p_mp4_stream, VLC_TRUE );
1543 stream_Delete( p_mp4_stream );
1546 else if( !strcmp( tk->psz_codec, "A_MS/ACM" ) )
1548 if( tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1550 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1551 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1555 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk->p_extra_data;
1557 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk->fmt.i_codec, NULL );
1559 tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1560 tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1561 tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1562 tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1563 tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1565 tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1566 if( tk->fmt.i_extra > 0 )
1568 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1569 memcpy( tk->fmt.p_extra, &p_wf[1], tk->fmt.i_extra );
1573 else if( !strcmp( tk->psz_codec, "A_MPEG/L3" ) ||
1574 !strcmp( tk->psz_codec, "A_MPEG/L2" ) ||
1575 !strcmp( tk->psz_codec, "A_MPEG/L1" ) )
1577 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1579 else if( !strcmp( tk->psz_codec, "A_AC3" ) )
1581 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1583 else if( !strcmp( tk->psz_codec, "A_DTS" ) )
1585 tk->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
1587 else if( !strcmp( tk->psz_codec, "A_FLAC" ) )
1589 tk->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
1590 tk->fmt.i_extra = tk->i_extra_data;
1591 tk->fmt.p_extra = malloc( tk->i_extra_data );
1592 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
1594 else if( !strcmp( tk->psz_codec, "A_VORBIS" ) )
1596 int i, i_offset = 1, i_size[3], i_extra;
1599 tk->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
1601 /* Split the 3 headers */
1602 if( tk->p_extra_data[0] != 0x02 )
1603 msg_Err( &sys.demuxer, "invalid vorbis header" );
1605 for( i = 0; i < 2; i++ )
1608 while( i_offset < tk->i_extra_data )
1610 i_size[i] += tk->p_extra_data[i_offset];
1611 if( tk->p_extra_data[i_offset++] != 0xff ) break;
1615 i_size[0] = __MIN(i_size[0], tk->i_extra_data - i_offset);
1616 i_size[1] = __MIN(i_size[1], tk->i_extra_data -i_offset -i_size[0]);
1617 i_size[2] = tk->i_extra_data - i_offset - i_size[0] - i_size[1];
1619 tk->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
1620 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1621 p_extra = (uint8_t *)tk->fmt.p_extra; i_extra = 0;
1622 for( i = 0; i < 3; i++ )
1624 *(p_extra++) = i_size[i] >> 8;
1625 *(p_extra++) = i_size[i] & 0xFF;
1626 memcpy( p_extra, tk->p_extra_data + i_offset + i_extra,
1628 p_extra += i_size[i];
1629 i_extra += i_size[i];
1632 else if( !strncmp( tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1633 !strncmp( tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1635 int i_profile, i_srate;
1636 static unsigned int i_sample_rates[] =
1638 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1639 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1642 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1643 /* create data for faad (MP4DecSpecificDescrTag)*/
1645 if( !strcmp( &tk->psz_codec[12], "MAIN" ) )
1649 else if( !strcmp( &tk->psz_codec[12], "LC" ) )
1653 else if( !strcmp( &tk->psz_codec[12], "SSR" ) )
1662 for( i_srate = 0; i_srate < 13; i_srate++ )
1664 if( i_sample_rates[i_srate] == tk->fmt.audio.i_rate )
1669 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1671 tk->fmt.i_extra = 2;
1672 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1673 ((uint8_t*)tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1674 ((uint8_t*)tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk->fmt.audio.i_channels << 3);
1676 else if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) ||
1677 !strcmp( tk->psz_codec, "A_PCM/INT/LIT" ) ||
1678 !strcmp( tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1680 if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) )
1682 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1686 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1688 tk->fmt.audio.i_blockalign = ( tk->fmt.audio.i_bitspersample + 7 ) / 8 * tk->fmt.audio.i_channels;
1690 else if( !strcmp( tk->psz_codec, "A_TTA1" ) )
1692 /* FIXME: support this codec */
1693 msg_Err( &sys.demuxer, "TTA not supported yet[%d, n=%d]", i_track, tk->i_number );
1694 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1696 else if( !strcmp( tk->psz_codec, "A_WAVPACK4" ) )
1698 /* FIXME: support this codec */
1699 msg_Err( &sys.demuxer, "Wavpack not supported yet[%d, n=%d]", i_track, tk->i_number );
1700 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1702 else if( !strcmp( tk->psz_codec, "S_TEXT/UTF8" ) )
1704 tk->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1705 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1707 else if( !strcmp( tk->psz_codec, "S_TEXT/SSA" ) ||
1708 !strcmp( tk->psz_codec, "S_TEXT/ASS" ) ||
1709 !strcmp( tk->psz_codec, "S_SSA" ) ||
1710 !strcmp( tk->psz_codec, "S_ASS" ))
1712 tk->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1713 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1715 else if( !strcmp( tk->psz_codec, "S_VOBSUB" ) )
1717 tk->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1718 if( tk->i_extra_data )
1721 char *p_buf = (char *)malloc( tk->i_extra_data + 1);
1722 memcpy( p_buf, tk->p_extra_data , tk->i_extra_data );
1723 p_buf[tk->i_extra_data] = '\0';
1725 p_start = strstr( p_buf, "size:" );
1726 if( sscanf( p_start, "size: %dx%d",
1727 &tk->fmt.subs.spu.i_original_frame_width, &tk->fmt.subs.spu.i_original_frame_height ) == 2 )
1729 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 );
1733 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1738 else if( !strcmp( tk->psz_codec, "B_VOBBTN" ) )
1740 /* FIXME: support this codec */
1741 msg_Err( &sys.demuxer, "Vob Buttons not supported yet[%d, n=%d]", i_track, tk->i_number );
1742 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1746 msg_Err( &sys.demuxer, "unknow codec id=`%s'", tk->psz_codec );
1747 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1751 tk->fmt.i_priority = 1000;
1754 tk->p_es = es_out_Add( sys.demuxer.out, &tk->fmt );
1756 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_start_time );
1760 sys.i_start_pts = i_start_time;
1761 // reset the stream reading to the first cluster of the segment used
1762 es.I_O().setFilePointer( i_start_pos );
1765 ep = new EbmlParser( &es, segment, &sys.demuxer );
1770 void matroska_segment_c::UnSelect( )
1774 for( i_track = 0; i_track < tracks.size(); i_track++ )
1776 #define tk tracks[i_track]
1777 if ( tk->p_es != NULL )
1779 es_out_Del( sys.demuxer.out, tk->p_es );
1788 void virtual_segment_c::PrepareChapters( )
1790 if ( linked_segments.size() == 0 )
1793 // !!! should be called only once !!!
1794 matroska_segment_c *p_segment;
1797 // copy editions from the first segment
1798 p_segment = linked_segments[0];
1799 p_editions = &p_segment->stored_editions;
1801 for ( i=1 ; i<linked_segments.size(); i++ )
1803 p_segment = linked_segments[i];
1804 // FIXME assume we have the same editions in all segments
1805 for (j=0; j<p_segment->stored_editions.size(); j++)
1806 (*p_editions)[j]->Append( *p_segment->stored_editions[j] );
1810 std::string chapter_edition_c::GetMainName() const
1812 if ( sub_chapters.size() )
1814 return sub_chapters[0]->GetCodecName( true );
1819 void chapter_edition_c::PublishChapters( input_title_t & title )
1821 chapter_item_c::PublishChapters( title, 0 );
1824 void chapter_item_c::PublishChapters( input_title_t & title, int i_level )
1826 // add support for meta-elements from codec like DVD Titles
1827 if ( !b_display_seekpoint || psz_name == "" )
1829 psz_name = GetCodecName();
1830 if ( psz_name != "" )
1831 b_display_seekpoint = true;
1834 if (b_display_seekpoint)
1836 seekpoint_t *sk = vlc_seekpoint_New();
1838 sk->i_level = i_level;
1839 sk->i_time_offset = i_start_time;
1840 sk->psz_name = strdup( psz_name.c_str() );
1842 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
1843 title.i_seekpoint++;
1844 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
1845 title.seekpoint[title.i_seekpoint-1] = sk;
1847 i_seekpoint_num = title.i_seekpoint;
1850 for ( size_t i=0; i<sub_chapters.size() ; i++)
1852 sub_chapters[i]->PublishChapters( title, i_level+1 );
1856 void virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
1858 demux_sys_t & sys = *demux.p_sys;
1859 chapter_item_c *psz_curr_chapter;
1861 /* update current chapter/seekpoint */
1862 if ( p_editions->size() )
1864 /* 1st, we need to know in which chapter we are */
1865 psz_curr_chapter = (*p_editions)[i_current_edition]->FindTimecode( sys.i_pts );
1867 /* we have moved to a new chapter */
1868 if (psz_curr_chapter != NULL && psz_current_chapter != psz_curr_chapter)
1870 if ( psz_current_chapter != NULL )
1871 psz_current_chapter->Leave();
1873 if ( psz_curr_chapter->i_seekpoint_num > 0 )
1875 demux.info.i_update |= INPUT_UPDATE_SEEKPOINT;
1876 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
1879 if ( (*p_editions)[i_current_edition]->b_ordered )
1881 psz_curr_chapter->Enter();
1883 // only seek if necessary
1884 if ( psz_current_chapter == NULL || (psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time) )
1885 Seek( demux, sys.i_pts, 0, psz_curr_chapter );
1888 psz_current_chapter = psz_curr_chapter;
1892 chapter_item_c *virtual_segment_c::BrowseCodecPrivate( unsigned int codec_id,
1893 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1894 const void *p_cookie,
1895 size_t i_cookie_size )
1897 // FIXME don't assume it is the first edition
1898 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
1899 if ( index != p_editions->end() )
1901 chapter_item_c *p_result = (*index)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
1902 if ( p_result != NULL )
1908 chapter_item_c *chapter_item_c::BrowseCodecPrivate( unsigned int codec_id,
1909 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1910 const void *p_cookie,
1911 size_t i_cookie_size )
1914 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
1915 while ( index != codecs.end() )
1917 if ( match( **index ,p_cookie, i_cookie_size ) )
1923 chapter_item_c *p_result = NULL;
1924 std::vector<chapter_item_c*>::const_iterator index2 = sub_chapters.begin();
1925 while ( index2 != sub_chapters.end() )
1927 p_result = (*index2)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
1928 if ( p_result != NULL )
1936 void chapter_item_c::Append( const chapter_item_c & chapter )
1938 // we are appending content for the same chapter UID
1940 chapter_item_c *p_chapter;
1942 for ( i=0; i<chapter.sub_chapters.size(); i++ )
1944 p_chapter = FindChapter( *chapter.sub_chapters[i] );
1945 if ( p_chapter != NULL )
1947 p_chapter->Append( *chapter.sub_chapters[i] );
1951 sub_chapters.push_back( chapter.sub_chapters[i] );
1955 i_user_start_time = min( i_user_start_time, chapter.i_user_start_time );
1956 i_user_end_time = max( i_user_end_time, chapter.i_user_end_time );
1959 chapter_item_c * chapter_item_c::FindChapter( const chapter_item_c & chapter )
1962 for ( i=0; i<sub_chapters.size(); i++)
1964 if ( sub_chapters[i]->i_uid == chapter.i_uid )
1965 return sub_chapters[i];
1970 std::string chapter_item_c::GetCodecName( bool f_for_title ) const
1974 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
1975 while ( index != codecs.end() )
1977 result = (*index)->GetCodecName( f_for_title );
1986 std::string dvd_chapter_codec_c::GetCodecName( bool f_for_title ) const
1989 if ( m_private_data.GetSize() >= 3)
1991 const binary* p_data = m_private_data.GetBuffer();
1992 if ( p_data[0] == 0x28 )
1994 uint16_t i_title = (p_data[1] << 8) + p_data[2];
1996 sprintf( psz_str, " %d ---", i_title );
1997 result = N_("--- DVD Title");
2000 else if ( p_data[0] == 0x2A )
2003 sprintf( psz_str, " (%c%c) ---", p_data[1], p_data[2] );
2004 result = N_("--- DVD Menu");
2007 else if ( p_data[0] == 0x30 && f_for_title )
2009 if ( p_data[1] == 0x00 )
2010 result = N_("First Played");
2011 else if ( p_data[1] == 0xC0 )
2012 result = N_("Video Manager");
2013 else if ( p_data[1] == 0x80 )
2015 uint16_t i_title = (p_data[2] << 8) + p_data[3];
2017 sprintf( psz_str, " %d -----", i_title );
2018 result = N_("----- Title");
2027 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter )
2029 demux_sys_t *p_sys = p_demux->p_sys;
2030 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
2031 matroska_segment_c *p_segment = p_vsegment->Segment();
2032 mtime_t i_time_offset = 0;
2036 msg_Dbg( p_demux, "seek request to "I64Fd" (%f%%)", i_date, f_percent );
2037 if( i_date < 0 && f_percent < 0 )
2039 msg_Warn( p_demux, "cannot seek nowhere !" );
2042 if( f_percent > 1.0 )
2044 msg_Warn( p_demux, "cannot seek so far !" );
2048 /* seek without index or without date */
2049 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
2051 if (p_sys->f_duration >= 0)
2053 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
2057 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
2059 msg_Dbg( p_demux, "inacurate way of seeking" );
2060 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
2062 if( p_segment->index[i_index].i_position >= i_pos)
2067 if( i_index == p_segment->i_index )
2072 i_date = p_segment->index[i_index].i_time;
2075 if( p_segment->index[i_index].i_position < i_pos )
2079 msg_Warn( p_demux, "searching for cluster, could take some time" );
2081 /* search a cluster */
2082 while( ( el = p_sys->ep->Get() ) != NULL )
2084 if( MKV_IS_ID( el, KaxCluster ) )
2086 KaxCluster *cluster = (KaxCluster*)el;
2088 /* add it to the index */
2089 p_segment->IndexAppendCluster( cluster );
2091 if( (int64_t)cluster->GetElementPosition() >= i_pos )
2093 p_sys->cluster = cluster;
2104 p_vsegment->Seek( *p_demux, i_date, i_time_offset, psz_chapter );
2107 /*****************************************************************************
2108 * Demux: reads and demuxes data packets
2109 *****************************************************************************
2110 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
2111 *****************************************************************************/
2112 static int Demux( demux_t *p_demux)
2114 demux_sys_t *p_sys = p_demux->p_sys;
2115 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
2116 matroska_segment_c *p_segmet = p_vsegment->Segment();
2117 if ( p_segmet == NULL ) return 0;
2118 int i_block_count = 0;
2121 int64_t i_block_duration;
2122 int64_t i_block_ref1;
2123 int64_t i_block_ref2;
2127 if ( p_sys->demuxer.b_die )
2130 if( p_sys->i_pts >= p_sys->i_start_pts )
2131 p_vsegment->UpdateCurrentToChapter( *p_demux );
2133 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
2135 /* nothing left to read in this ordered edition */
2136 if ( !p_vsegment->SelectNext() )
2138 p_segmet->UnSelect( );
2140 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
2142 /* switch to the next segment */
2143 p_segmet = p_vsegment->Segment();
2144 if ( !p_segmet->Select( 0 ) )
2146 msg_Err( p_demux, "Failed to select new segment" );
2153 if( p_segmet->BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
2155 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered )
2157 const chapter_item_c *p_chap = p_vsegment->CurrentChapter();
2158 // check if there are more chapters to read
2159 if ( p_chap != NULL )
2161 /* TODO handle successive chapters with the same user_start_time/user_end_time
2162 if ( p_chap->i_user_start_time == p_chap->i_user_start_time )
2163 p_vsegment->SelectNext();
2165 p_sys->i_pts = p_chap->i_user_end_time;
2166 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
2173 msg_Warn( p_demux, "cannot get block EOF?" );
2174 p_segmet->UnSelect( );
2176 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
2178 /* switch to the next segment */
2179 if ( !p_vsegment->SelectNext() )
2180 // no more segments in this stream
2182 p_segmet = p_vsegment->Segment();
2183 if ( !p_segmet->Select( 0 ) )
2185 msg_Err( p_demux, "Failed to select new segment" );
2192 p_sys->i_pts = p_sys->i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
2194 if( p_sys->i_pts >= p_sys->i_start_pts )
2196 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
2199 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
2204 // TODO optimize when there is need to leave or when seeking has been called
2205 if( i_block_count > 5 )
2214 /*****************************************************************************
2216 *****************************************************************************/
2217 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
2223 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
2225 if( i_size <= 0 || mb_eof )
2230 return stream_Read( s, p_buffer, i_size );
2232 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
2238 case seek_beginning:
2242 i_pos = stream_Size( s ) - i_offset;
2245 i_pos= stream_Tell( s ) + i_offset;
2249 if( i_pos < 0 || i_pos >= stream_Size( s ) )
2256 if( stream_Seek( s, i_pos ) )
2262 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
2266 uint64 vlc_stream_io_callback::getFilePointer( void )
2268 return stream_Tell( s );
2270 void vlc_stream_io_callback::close( void )
2276 /*****************************************************************************
2277 * Ebml Stream parser
2278 *****************************************************************************/
2279 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux )
2286 mi_remain_size[0] = el_start->GetSize();
2288 for( i = 1; i < 6; i++ )
2294 mb_keep = VLC_FALSE;
2295 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
2298 EbmlParser::~EbmlParser( void )
2302 for( i = 1; i < mi_level; i++ )
2308 mb_keep = VLC_FALSE;
2312 void EbmlParser::Up( void )
2314 if( mi_user_level == mi_level )
2316 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
2322 void EbmlParser::Down( void )
2328 void EbmlParser::Keep( void )
2333 int EbmlParser::GetLevel( void )
2335 return mi_user_level;
2338 void EbmlParser::Reset( demux_t *p_demux )
2340 while ( mi_level > 0)
2342 delete m_el[mi_level];
2343 m_el[mi_level] = NULL;
2346 mi_user_level = mi_level = 1;
2347 #if LIBEBML_VERSION >= 0x000704
2348 // a little faster and cleaner
2349 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
2351 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
2353 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
2356 EbmlElement *EbmlParser::Get( void )
2360 if( mi_user_level != mi_level )
2366 EbmlElement *ret = m_got;
2372 if( m_el[mi_level] )
2374 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
2377 delete m_el[mi_level];
2379 mb_keep = VLC_FALSE;
2382 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, mb_dummy, 1 );
2383 // mi_remain_size[mi_level] = m_el[mi_level]->GetSize();
2394 delete m_el[mi_level - 1];
2395 m_got = m_el[mi_level -1] = m_el[mi_level];
2396 m_el[mi_level] = NULL;
2403 else if( m_el[mi_level] == NULL )
2405 fprintf( stderr," m_el[mi_level] == NULL\n" );
2408 return m_el[mi_level];
2412 /*****************************************************************************
2414 * * LoadCues : load the cues element and update index
2416 * * LoadTags : load ... the tags element
2418 * * InformationCreate : create all information, load tags if present
2420 *****************************************************************************/
2421 void matroska_segment_c::LoadCues( )
2423 int64_t i_sav_position = es.I_O().getFilePointer();
2425 EbmlElement *el, *cues;
2427 /* *** Load the cue if found *** */
2428 if( i_cues_position < 0 )
2430 msg_Warn( &sys.demuxer, "no cues/empty cues found->seek won't be precise" );
2432 // IndexAppendCluster( cluster );
2435 vlc_bool_t b_seekable;
2437 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
2441 msg_Dbg( &sys.demuxer, "loading cues" );
2442 es.I_O().setFilePointer( i_cues_position, seek_beginning );
2443 cues = es.FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
2447 msg_Err( &sys.demuxer, "cannot load cues (broken seekhead or file)" );
2448 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2452 ep = new EbmlParser( &es, cues, &sys.demuxer );
2453 while( ( el = ep->Get() ) != NULL )
2455 if( MKV_IS_ID( el, KaxCuePoint ) )
2457 #define idx index[i_index]
2460 idx.i_block_number= -1;
2461 idx.i_position = -1;
2463 idx.b_key = VLC_TRUE;
2466 while( ( el = ep->Get() ) != NULL )
2468 if( MKV_IS_ID( el, KaxCueTime ) )
2470 KaxCueTime &ctime = *(KaxCueTime*)el;
2472 ctime.ReadData( es.I_O() );
2474 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
2476 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
2479 while( ( el = ep->Get() ) != NULL )
2481 if( MKV_IS_ID( el, KaxCueTrack ) )
2483 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
2485 ctrack.ReadData( es.I_O() );
2486 idx.i_track = uint16( ctrack );
2488 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
2490 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
2492 ccpos.ReadData( es.I_O() );
2493 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
2495 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
2497 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2499 cbnum.ReadData( es.I_O() );
2500 idx.i_block_number = uint32( cbnum );
2504 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2511 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2517 msg_Dbg( &sys.demuxer, " * added time="I64Fd" pos="I64Fd
2518 " track=%d bnum=%d", idx.i_time, idx.i_position,
2519 idx.i_track, idx.i_block_number );
2523 if( i_index >= i_index_max )
2525 i_index_max += 1024;
2526 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
2532 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2540 msg_Dbg( &sys.demuxer, "loading cues done." );
2541 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2544 void matroska_segment_c::LoadTags( )
2546 int64_t i_sav_position = es.I_O().getFilePointer();
2548 EbmlElement *el, *tags;
2550 msg_Dbg( &sys.demuxer, "loading tags" );
2551 es.I_O().setFilePointer( i_tags_position, seek_beginning );
2552 tags = es.FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2556 msg_Err( &sys.demuxer, "cannot load tags (broken seekhead or file)" );
2557 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2561 msg_Dbg( &sys.demuxer, "Tags" );
2562 ep = new EbmlParser( &es, tags, &sys.demuxer );
2563 while( ( el = ep->Get() ) != NULL )
2565 if( MKV_IS_ID( el, KaxTag ) )
2567 msg_Dbg( &sys.demuxer, "+ Tag" );
2569 while( ( el = ep->Get() ) != NULL )
2571 if( MKV_IS_ID( el, KaxTagTargets ) )
2573 msg_Dbg( &sys.demuxer, "| + Targets" );
2575 while( ( el = ep->Get() ) != NULL )
2577 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2581 else if( MKV_IS_ID( el, KaxTagGeneral ) )
2583 msg_Dbg( &sys.demuxer, "| + General" );
2585 while( ( el = ep->Get() ) != NULL )
2587 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2591 else if( MKV_IS_ID( el, KaxTagGenres ) )
2593 msg_Dbg( &sys.demuxer, "| + Genres" );
2595 while( ( el = ep->Get() ) != NULL )
2597 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2601 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
2603 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
2605 while( ( el = ep->Get() ) != NULL )
2607 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2611 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
2613 msg_Dbg( &sys.demuxer, "| + Images Specific" );
2615 while( ( el = ep->Get() ) != NULL )
2617 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2621 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
2623 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
2625 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
2627 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
2629 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
2631 msg_Dbg( &sys.demuxer, "| + Multi Date" );
2633 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
2635 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
2637 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
2639 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
2641 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
2643 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
2645 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
2647 msg_Dbg( &sys.demuxer, "| + Multi Title" );
2651 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid( *el ).name() );
2658 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
2664 msg_Dbg( &sys.demuxer, "loading tags done." );
2665 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2668 /*****************************************************************************
2670 *****************************************************************************/
2671 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
2675 int i_upper_level = 0;
2677 msg_Dbg( &sys.demuxer, "| + Seek head" );
2679 /* Master elements */
2680 seekhead->Read( es, seekhead->Generic().Context, i_upper_level, el, true );
2682 for( i = 0; i < seekhead->ListSize(); i++ )
2684 EbmlElement *l = (*seekhead)[i];
2686 if( MKV_IS_ID( l, KaxSeek ) )
2688 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
2689 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
2692 for( j = 0; j < sk->ListSize(); j++ )
2694 EbmlElement *l = (*sk)[j];
2696 if( MKV_IS_ID( l, KaxSeekID ) )
2698 KaxSeekID &sid = *(KaxSeekID*)l;
2699 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
2701 else if( MKV_IS_ID( l, KaxSeekPosition ) )
2703 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
2704 i_pos = uint64( spos );
2708 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
2714 if( id == KaxCues::ClassInfos.GlobalId )
2716 msg_Dbg( &sys.demuxer, "| | | = cues at "I64Fd, i_pos );
2717 i_cues_position = segment->GetGlobalPosition( i_pos );
2719 else if( id == KaxChapters::ClassInfos.GlobalId )
2721 msg_Dbg( &sys.demuxer, "| | | = chapters at "I64Fd, i_pos );
2722 i_chapters_position = segment->GetGlobalPosition( i_pos );
2724 else if( id == KaxTags::ClassInfos.GlobalId )
2726 msg_Dbg( &sys.demuxer, "| | | = tags at "I64Fd, i_pos );
2727 i_tags_position = segment->GetGlobalPosition( i_pos );
2733 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
2738 /*****************************************************************************
2740 *****************************************************************************/
2741 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
2747 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
2749 tk = new mkv_track_t();
2750 tracks.push_back( tk );
2752 /* Init the track */
2753 memset( tk, 0, sizeof( mkv_track_t ) );
2755 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
2756 tk->fmt.psz_language = strdup("English");
2757 tk->fmt.psz_description = NULL;
2759 tk->b_default = VLC_TRUE;
2760 tk->b_enabled = VLC_TRUE;
2761 tk->b_silent = VLC_FALSE;
2762 tk->i_number = tracks.size() - 1;
2763 tk->i_extra_data = 0;
2764 tk->p_extra_data = NULL;
2765 tk->psz_codec = NULL;
2766 tk->i_default_duration = 0;
2767 tk->f_timecodescale = 1.0;
2769 tk->b_inited = VLC_FALSE;
2770 tk->i_data_init = 0;
2771 tk->p_data_init = NULL;
2773 tk->psz_codec_name = NULL;
2774 tk->psz_codec_settings = NULL;
2775 tk->psz_codec_info_url = NULL;
2776 tk->psz_codec_download_url = NULL;
2778 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
2780 for( i = 0; i < m->ListSize(); i++ )
2782 EbmlElement *l = (*m)[i];
2784 if( MKV_IS_ID( l, KaxTrackNumber ) )
2786 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
2788 tk->i_number = uint32( tnum );
2789 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
2791 else if( MKV_IS_ID( l, KaxTrackUID ) )
2793 KaxTrackUID &tuid = *(KaxTrackUID*)l;
2795 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
2797 else if( MKV_IS_ID( l, KaxTrackType ) )
2800 KaxTrackType &ttype = *(KaxTrackType*)l;
2802 switch( uint8(ttype) )
2806 tk->fmt.i_cat = AUDIO_ES;
2810 tk->fmt.i_cat = VIDEO_ES;
2812 case track_subtitle:
2813 psz_type = "subtitle";
2814 tk->fmt.i_cat = SPU_ES;
2817 psz_type = "unknown";
2818 tk->fmt.i_cat = UNKNOWN_ES;
2822 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
2824 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
2826 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
2828 // tk->b_enabled = uint32( fenb );
2829 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
2830 // uint32( fenb ) );
2832 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
2834 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
2836 tk->b_default = uint32( fdef );
2837 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
2839 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
2841 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
2843 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
2845 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
2847 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
2849 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
2851 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
2853 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
2855 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
2857 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
2859 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
2861 tk->i_default_duration = uint64(defd);
2862 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration="I64Fd, uint64(defd) );
2864 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
2866 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
2868 tk->f_timecodescale = float( ttcs );
2869 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
2871 else if( MKV_IS_ID( l, KaxTrackName ) )
2873 KaxTrackName &tname = *(KaxTrackName*)l;
2875 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
2876 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
2878 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
2880 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
2882 tk->fmt.psz_language = strdup( string( lang ).c_str() );
2883 msg_Dbg( &sys.demuxer,
2884 "| | | + Track Language=`%s'", tk->fmt.psz_language );
2886 else if( MKV_IS_ID( l, KaxCodecID ) )
2888 KaxCodecID &codecid = *(KaxCodecID*)l;
2890 tk->psz_codec = strdup( string( codecid ).c_str() );
2891 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
2893 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
2895 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
2897 tk->i_extra_data = cpriv.GetSize();
2898 if( tk->i_extra_data > 0 )
2900 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
2901 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
2903 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
2905 else if( MKV_IS_ID( l, KaxCodecName ) )
2907 KaxCodecName &cname = *(KaxCodecName*)l;
2909 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
2910 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
2912 else if( MKV_IS_ID( l, KaxContentEncodings ) )
2914 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
2915 MkvTree( sys.demuxer, 3, "Content Encodings" );
2916 for( j = 0; j < cencs->ListSize(); j++ )
2918 EbmlElement *l2 = (*cencs)[j];
2919 if( MKV_IS_ID( l2, KaxContentEncoding ) )
2921 MkvTree( sys.demuxer, 4, "Content Encoding" );
2922 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
2923 for( k = 0; k < cenc->ListSize(); k++ )
2925 EbmlElement *l3 = (*cenc)[k];
2926 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
2928 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
2929 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
2931 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
2933 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
2934 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
2936 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
2938 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
2939 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
2941 else if( MKV_IS_ID( l3, KaxContentCompression ) )
2943 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
2944 MkvTree( sys.demuxer, 5, "Content Compression" );
2945 for( n = 0; n < compr->ListSize(); n++ )
2947 EbmlElement *l4 = (*compr)[n];
2948 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
2950 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
2951 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
2952 if( uint32( compalg ) == 0 )
2954 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
2959 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
2966 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
2973 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
2978 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
2980 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
2982 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
2983 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
2985 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
2987 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
2989 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
2990 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
2992 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
2994 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
2996 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
2997 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
2999 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
3001 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
3003 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
3005 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
3007 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
3009 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
3011 else if( MKV_IS_ID( l, KaxTrackVideo ) )
3013 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
3016 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
3019 for( j = 0; j < tkv->ListSize(); j++ )
3021 EbmlElement *l = (*tkv)[j];
3022 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
3024 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
3026 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
3028 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
3030 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
3032 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
3035 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
3037 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
3039 tk->fmt.video.i_width = uint16( vwidth );
3040 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
3042 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
3044 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
3046 tk->fmt.video.i_height = uint16( vheight );
3047 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
3049 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
3051 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
3053 tk->fmt.video.i_visible_width = uint16( vwidth );
3054 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
3056 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
3058 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
3060 tk->fmt.video.i_visible_height = uint16( vheight );
3061 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
3063 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
3065 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
3067 tk->f_fps = float( vfps );
3068 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
3070 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
3072 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
3074 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
3075 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
3077 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
3079 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
3081 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
3083 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
3085 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
3087 // msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( gamma ) );
3091 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
3094 if ( tk->fmt.video.i_visible_height && tk->fmt.video.i_visible_width )
3095 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * tk->fmt.video.i_visible_width / tk->fmt.video.i_visible_height;
3097 else if( MKV_IS_ID( l, KaxTrackAudio ) )
3099 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
3102 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
3104 for( j = 0; j < tka->ListSize(); j++ )
3106 EbmlElement *l = (*tka)[j];
3108 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
3110 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
3112 tk->fmt.audio.i_rate = (int)float( afreq );
3113 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
3115 else if( MKV_IS_ID( l, KaxAudioChannels ) )
3117 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
3119 tk->fmt.audio.i_channels = uint8( achan );
3120 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
3122 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
3124 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
3126 tk->fmt.audio.i_bitspersample = uint8( abits );
3127 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
3131 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
3137 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
3138 typeid(*l).name() );
3143 /*****************************************************************************
3145 *****************************************************************************/
3146 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
3150 int i_upper_level = 0;
3152 msg_Dbg( &sys.demuxer, "| + Tracks" );
3154 /* Master elements */
3155 tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
3157 for( i = 0; i < tracks->ListSize(); i++ )
3159 EbmlElement *l = (*tracks)[i];
3161 if( MKV_IS_ID( l, KaxTrackEntry ) )
3163 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
3167 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3172 /*****************************************************************************
3174 *****************************************************************************/
3175 void matroska_segment_c::ParseInfo( KaxInfo *info )
3180 int i_upper_level = 0;
3182 msg_Dbg( &sys.demuxer, "| + Information" );
3184 /* Master elements */
3185 m = static_cast<EbmlMaster *>(info);
3186 m->Read( es, info->Generic().Context, i_upper_level, el, true );
3188 for( i = 0; i < m->ListSize(); i++ )
3190 EbmlElement *l = (*m)[i];
3192 if( MKV_IS_ID( l, KaxSegmentUID ) )
3194 segment_uid = *(new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l)));
3196 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)segment_uid.GetBuffer() );
3198 else if( MKV_IS_ID( l, KaxPrevUID ) )
3200 prev_segment_uid = *(new KaxPrevUID(*static_cast<KaxPrevUID*>(l)));
3202 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)prev_segment_uid.GetBuffer() );
3204 else if( MKV_IS_ID( l, KaxNextUID ) )
3206 next_segment_uid = *(new KaxNextUID(*static_cast<KaxNextUID*>(l)));
3208 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)next_segment_uid.GetBuffer() );
3210 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
3212 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
3214 i_timescale = uint64(tcs);
3216 msg_Dbg( &sys.demuxer, "| | + TimecodeScale="I64Fd,
3219 else if( MKV_IS_ID( l, KaxDuration ) )
3221 KaxDuration &dur = *(KaxDuration*)l;
3223 i_duration = mtime_t( double( dur ) );
3225 msg_Dbg( &sys.demuxer, "| | + Duration="I64Fd,
3228 else if( MKV_IS_ID( l, KaxMuxingApp ) )
3230 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
3232 psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
3234 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
3235 psz_muxing_application );
3237 else if( MKV_IS_ID( l, KaxWritingApp ) )
3239 KaxWritingApp &wapp = *(KaxWritingApp*)l;
3241 psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
3243 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
3244 psz_writing_application );
3246 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
3248 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
3250 psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
3252 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
3253 psz_segment_filename );
3255 else if( MKV_IS_ID( l, KaxTitle ) )
3257 KaxTitle &title = *(KaxTitle*)l;
3259 psz_title = UTF8ToStr( UTFstring( title ) );
3261 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
3263 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
3265 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
3267 families.push_back(*uid);
3269 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
3271 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
3272 else if( MKV_IS_ID( l, KaxDateUTC ) )
3274 KaxDateUTC &date = *(KaxDateUTC*)l;
3279 i_date = date.GetEpochDate();
3280 memset( buffer, 0, 256 );
3281 if( gmtime_r( &i_date, &tmres ) &&
3282 asctime_r( &tmres, buffer ) )
3284 buffer[strlen( buffer)-1]= '\0';
3285 psz_date_utc = strdup( buffer );
3286 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
3290 #if LIBMATROSKA_VERSION >= 0x000704
3291 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
3293 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
3294 chapter_translation_c translated;
3296 p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
3297 for( j = 0; j < p_trans->ListSize(); j++ )
3299 EbmlElement *l = (*p_trans)[j];
3301 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
3303 translated.editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
3305 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
3307 translated.codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
3309 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
3311 translated.translated = *( new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) ) );
3315 translations.push_back( translated );
3320 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3324 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
3325 i_duration = mtime_t(f_dur);
3329 /*****************************************************************************
3331 *****************************************************************************/
3332 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
3336 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
3337 for( i = 0; i < ca->ListSize(); i++ )
3339 EbmlElement *l = (*ca)[i];
3341 if( MKV_IS_ID( l, KaxChapterUID ) )
3343 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
3344 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
3346 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
3348 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
3349 chapters.b_display_seekpoint = uint8( flag ) == 0;
3351 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
3353 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
3355 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
3356 chapters.i_start_time = uint64( start ) / I64C(1000);
3358 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
3360 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
3362 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
3363 chapters.i_end_time = uint64( end ) / I64C(1000);
3365 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
3367 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
3369 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
3371 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
3372 for( j = 0; j < cd->ListSize(); j++ )
3374 EbmlElement *l= (*cd)[j];
3376 if( MKV_IS_ID( l, KaxChapterString ) )
3380 KaxChapterString &name =*(KaxChapterString*)l;
3381 for (k = 0; k < i_level; k++)
3382 chapters.psz_name += '+';
3383 chapters.psz_name += ' ';
3384 chapters.psz_name += UTF8ToStr( UTFstring( name ) );
3386 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", UTF8ToStr(UTFstring(name)) );
3388 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
3390 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
3391 const char *psz = string( lang ).c_str();
3393 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
3395 else if( MKV_IS_ID( l, KaxChapterCountry ) )
3397 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
3398 const char *psz = string( ct ).c_str();
3400 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
3404 else if( MKV_IS_ID( l, KaxChapterProcess ) )
3406 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
3408 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
3409 chapter_codec_cmds_c *p_ccodec = NULL;
3411 for( j = 0; j < cp->ListSize(); j++ )
3413 EbmlElement *k= (*cp)[j];
3415 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
3417 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
3418 if ( uint32(*p_codec_id) == 0 )
3419 p_ccodec = new matroska_script_codec_c();
3420 else if ( uint32(*p_codec_id) == 1 )
3421 p_ccodec = new dvd_chapter_codec_c( sys );
3426 if ( p_ccodec != NULL )
3428 for( j = 0; j < cp->ListSize(); j++ )
3430 EbmlElement *k= (*cp)[j];
3432 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
3434 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
3435 p_ccodec->SetPrivate( *p_private );
3437 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
3439 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
3442 chapters.codecs.push_back( p_ccodec );
3445 else if( MKV_IS_ID( l, KaxChapterAtom ) )
3447 chapter_item_c *new_sub_chapter = new chapter_item_c();
3448 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
3449 new_sub_chapter->psz_parent = &chapters;
3450 chapters.sub_chapters.push_back( new_sub_chapter );
3455 /*****************************************************************************
3457 *****************************************************************************/
3458 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
3462 int i_upper_level = 0;
3465 /* Master elements */
3466 chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
3468 for( i = 0; i < chapters->ListSize(); i++ )
3470 EbmlElement *l = (*chapters)[i];
3472 if( MKV_IS_ID( l, KaxEditionEntry ) )
3474 chapter_edition_c *p_edition = new chapter_edition_c();
3476 EbmlMaster *E = static_cast<EbmlMaster *>(l );
3478 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
3479 for( j = 0; j < E->ListSize(); j++ )
3481 EbmlElement *l = (*E)[j];
3483 if( MKV_IS_ID( l, KaxChapterAtom ) )
3485 chapter_item_c *new_sub_chapter = new chapter_item_c();
3486 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
3487 p_edition->sub_chapters.push_back( new_sub_chapter );
3489 else if( MKV_IS_ID( l, KaxEditionUID ) )
3491 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
3493 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
3495 p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
3497 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
3499 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
3500 i_default_edition = stored_editions.size();
3504 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
3507 stored_editions.push_back( p_edition );
3511 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3515 for( i = 0; i < stored_editions.size(); i++ )
3517 stored_editions[i]->RefreshChapters( );
3520 if ( stored_editions[i_default_edition]->b_ordered )
3522 /* update the duration of the segment according to the sum of all sub chapters */
3523 i_dur = stored_editions[i_default_edition]->Duration() / I64C(1000);
3529 void matroska_segment_c::ParseCluster( )
3534 int i_upper_level = 0;
3536 /* Master elements */
3537 m = static_cast<EbmlMaster *>( cluster );
3538 m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
3540 for( i = 0; i < m->ListSize(); i++ )
3542 EbmlElement *l = (*m)[i];
3544 if( MKV_IS_ID( l, KaxClusterTimecode ) )
3546 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
3548 cluster->InitTimecode( uint64( ctc ), i_timescale );
3553 i_start_time = cluster->GlobalTimecode() / 1000;
3556 /*****************************************************************************
3557 * InformationCreate:
3558 *****************************************************************************/
3559 void matroska_segment_c::InformationCreate( )
3563 sys.meta = vlc_meta_New();
3567 vlc_meta_Add( sys.meta, VLC_META_TITLE, psz_title );
3571 vlc_meta_Add( sys.meta, VLC_META_DATE, psz_date_utc );
3573 if( psz_segment_filename )
3575 vlc_meta_Add( sys.meta, _("Segment filename"), psz_segment_filename );
3577 if( psz_muxing_application )
3579 vlc_meta_Add( sys.meta, _("Muxing application"), psz_muxing_application );
3581 if( psz_writing_application )
3583 vlc_meta_Add( sys.meta, _("Writing application"), psz_writing_application );
3586 for( i_track = 0; i_track < tracks.size(); i_track++ )
3588 mkv_track_t *tk = tracks[i_track];
3589 vlc_meta_t *mtk = vlc_meta_New();
3591 sys.meta->track = (vlc_meta_t**)realloc( sys.meta->track,
3592 sizeof( vlc_meta_t * ) * ( sys.meta->i_track + 1 ) );
3593 sys.meta->track[sys.meta->i_track++] = mtk;
3595 if( tk->fmt.psz_description )
3597 vlc_meta_Add( sys.meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
3599 if( tk->psz_codec_name )
3601 vlc_meta_Add( sys.meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
3603 if( tk->psz_codec_settings )
3605 vlc_meta_Add( sys.meta, VLC_META_SETTING, tk->psz_codec_settings );
3607 if( tk->psz_codec_info_url )
3609 vlc_meta_Add( sys.meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
3611 if( tk->psz_codec_download_url )
3613 vlc_meta_Add( sys.meta, VLC_META_URL, tk->psz_codec_download_url );
3617 if( i_tags_position >= 0 )
3619 vlc_bool_t b_seekable;
3621 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
3630 /*****************************************************************************
3632 *****************************************************************************/
3634 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
3636 #define idx index[i_index]
3638 idx.i_block_number= -1;
3639 idx.i_position = cluster->GetElementPosition();
3641 idx.b_key = VLC_TRUE;
3644 if( i_index >= i_index_max )
3646 i_index_max += 1024;
3647 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
3652 static char * UTF8ToStr( const UTFstring &u )
3661 p = dst = (char*)malloc( i_src + 1);
3680 void chapter_edition_c::RefreshChapters( )
3682 chapter_item_c::RefreshChapters( b_ordered, -1 );
3683 b_display_seekpoint = false;
3686 int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_time )
3688 int64_t i_user_time = i_prev_user_time;
3690 // first the sub-chapters, and then ourself
3691 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
3692 while ( index != sub_chapters.end() )
3694 i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
3700 // the ordered chapters always start at zero
3701 if ( i_prev_user_time == -1 )
3703 if ( i_user_time == -1 )
3705 i_prev_user_time = 0;
3708 i_user_start_time = i_prev_user_time;
3709 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
3711 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
3715 i_user_end_time = i_user_time;
3720 std::sort( sub_chapters.begin(), sub_chapters.end() );
3721 i_user_start_time = i_start_time;
3722 if ( i_end_time != -1 )
3723 i_user_end_time = i_end_time;
3724 else if ( i_user_time != -1 )
3725 i_user_end_time = i_user_time;
3727 i_user_end_time = i_user_start_time;
3730 return i_user_end_time;
3733 mtime_t chapter_edition_c::Duration() const
3735 mtime_t i_result = 0;
3737 if ( sub_chapters.size() )
3739 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
3741 i_result = (*index)->i_user_end_time;
3747 chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode )
3749 chapter_item_c *psz_result = NULL;
3751 if ( i_user_timecode >= i_user_start_time &&
3752 ( i_user_timecode < i_user_end_time ||
3753 ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
3755 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
3756 while ( index != sub_chapters.end() && psz_result == NULL )
3758 psz_result = (*index)->FindTimecode( i_user_timecode );
3762 if ( psz_result == NULL )
3769 void demux_sys_t::PreloadFamily( const matroska_segment_c & of_segment )
3771 for (size_t i=0; i<opened_segments.size(); i++)
3773 opened_segments[i]->PreloadFamily( of_segment );
3776 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
3781 for (size_t i=0; i<families.size(); i++)
3783 for (size_t j=0; j<of_segment.families.size(); j++)
3785 if ( families[i] == of_segment.families[j] )
3793 // preload all the linked segments for all preloaded segments
3794 void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
3796 size_t i_preloaded, i, j;
3797 virtual_segment_c *p_seg;
3799 p_current_segment = VirtualFromSegments( p_segment );
3801 used_segments.push_back( p_current_segment );
3803 // create all the other virtual segments of the family
3806 for ( i=0; i< opened_segments.size(); i++ )
3808 if ( opened_segments[i]->b_preloaded && !IsUsedSegment( *opened_segments[i] ) )
3810 p_seg = VirtualFromSegments( opened_segments[i] );
3811 used_segments.push_back( p_seg );
3815 } while ( i_preloaded ); // worst case: will stop when all segments are found as family related
3817 // publish all editions of all usable segment
3818 for ( i=0; i< used_segments.size(); i++ )
3820 p_seg = used_segments[i];
3821 if ( p_seg->p_editions != NULL )
3823 std::string sz_name;
3824 input_title_t *p_title = vlc_input_title_New();
3825 p_seg->i_sys_title = i;
3827 // TODO use a name for each edition, let the TITLE deal with a codec name
3828 for ( j=0; j<p_seg->p_editions->size(); j++ )
3830 if ( p_title->psz_name == NULL )
3832 sz_name = (*p_seg->p_editions)[j]->GetMainName();
3833 if ( sz_name != "" )
3834 p_title->psz_name = strdup( sz_name.c_str() );
3837 chapter_edition_c *p_edition = (*p_seg->p_editions)[j];
3839 p_edition->PublishChapters( *p_title );
3842 // create a name if there is none
3843 if ( p_title->psz_name == NULL )
3845 sz_name = N_("Segment ");
3847 sprintf( psz_str, "%d", i );
3849 p_title->psz_name = strdup( sz_name.c_str() );
3852 titles.push_back( *p_title );
3857 bool demux_sys_t::IsUsedSegment( matroska_segment_c &segment ) const
3859 for ( size_t i=0; i< used_segments.size(); i++ )
3861 if ( used_segments[i]->FindUID( segment.segment_uid ) )
3867 virtual_segment_c *demux_sys_t::VirtualFromSegments( matroska_segment_c *p_segment ) const
3869 size_t i_preloaded, i;
3871 virtual_segment_c *p_result = new virtual_segment_c( p_segment );
3873 // fill our current virtual segment with all hard linked segments
3876 for ( i=0; i< opened_segments.size(); i++ )
3878 i_preloaded += p_result->AddSegment( opened_segments[i] );
3880 } while ( i_preloaded ); // worst case: will stop when all segments are found as linked
3884 p_result->PreloadLinked( );
3886 p_result->PrepareChapters( );
3891 bool demux_sys_t::PreparePlayback( )
3893 p_current_segment->LoadCues();
3894 f_duration = p_current_segment->Duration();
3896 /* add information */
3897 p_current_segment->Segment()->InformationCreate( );
3899 p_current_segment->Segment()->Select( 0 );
3904 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
3906 EbmlBinary * p_itema = (EbmlBinary *)(&p_item_a->segment_uid);
3907 if ( *p_itema == p_item_b->prev_segment_uid )
3910 p_itema = (EbmlBinary *)(&p_item_a->next_segment_uid);
3911 if ( *p_itema == p_item_b->segment_uid )
3914 if ( *p_itema == p_item_b->prev_segment_uid )
3920 bool matroska_segment_c::Preload( )
3925 EbmlElement *el = NULL;
3927 ep->Reset( &sys.demuxer );
3929 while( ( el = ep->Get() ) != NULL )
3931 if( MKV_IS_ID( el, KaxInfo ) )
3933 ParseInfo( static_cast<KaxInfo*>( el ) );
3935 else if( MKV_IS_ID( el, KaxTracks ) )
3937 ParseTracks( static_cast<KaxTracks*>( el ) );
3939 else if( MKV_IS_ID( el, KaxSeekHead ) )
3941 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
3943 else if( MKV_IS_ID( el, KaxCues ) )
3945 msg_Dbg( &sys.demuxer, "| + Cues" );
3947 else if( MKV_IS_ID( el, KaxCluster ) )
3949 msg_Dbg( &sys.demuxer, "| + Cluster" );
3951 cluster = (KaxCluster*)el;
3953 i_start_pos = cluster->GetElementPosition();
3957 /* stop parsing the stream */
3960 else if( MKV_IS_ID( el, KaxAttachments ) )
3962 msg_Dbg( &sys.demuxer, "| + Attachments FIXME (but probably never supported)" );
3964 else if( MKV_IS_ID( el, KaxChapters ) )
3966 msg_Dbg( &sys.demuxer, "| + Chapters" );
3967 ParseChapters( static_cast<KaxChapters*>( el ) );
3969 else if( MKV_IS_ID( el, KaxTag ) )
3971 msg_Dbg( &sys.demuxer, "| + Tags FIXME TODO" );
3975 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid(*el).name() );
3984 matroska_segment_c *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
3986 for (size_t i=0; i<opened_segments.size(); i++)
3988 if ( opened_segments[i]->segment_uid == uid )
3989 return opened_segments[i];
3994 chapter_item_c *demux_sys_t::BrowseCodecPrivate( unsigned int codec_id,
3995 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3996 const void *p_cookie,
3997 size_t i_cookie_size,
3998 virtual_segment_c * &p_segment_found )
4000 chapter_item_c *p_result = NULL;
4001 for (size_t i=0; i<opened_segments.size(); i++)
4003 p_result = used_segments[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
4004 if ( p_result != NULL )
4006 p_segment_found = used_segments[i];
4013 void virtual_segment_c::Sort()
4015 // keep the current segment index
4016 matroska_segment_c *p_segment = linked_segments[i_current_segment];
4018 std::sort( linked_segments.begin(), linked_segments.end(), matroska_segment_c::CompareSegmentUIDs );
4020 for ( i_current_segment=0; i_current_segment<linked_segments.size(); i_current_segment++)
4021 if ( linked_segments[i_current_segment] == p_segment )
4025 size_t virtual_segment_c::AddSegment( matroska_segment_c *p_segment )
4028 // check if it's not already in here
4029 for ( i=0; i<linked_segments.size(); i++ )
4031 if ( p_segment->segment_uid == linked_segments[i]->segment_uid )
4035 // find possible mates
4036 for ( i=0; i<linked_uids.size(); i++ )
4038 if ( p_segment->segment_uid == linked_uids[i]
4039 || p_segment->prev_segment_uid == linked_uids[i]
4040 || p_segment->next_segment_uid == linked_uids[i] )
4042 linked_segments.push_back( p_segment );
4044 AppendUID( p_segment->prev_segment_uid );
4045 AppendUID( p_segment->next_segment_uid );
4053 void virtual_segment_c::PreloadLinked( )
4055 for ( size_t i=0; i<linked_segments.size(); i++ )
4057 linked_segments[i]->Preload( );
4059 i_current_edition = linked_segments[0]->i_default_edition;
4062 mtime_t virtual_segment_c::Duration() const
4065 if ( linked_segments.size() == 0 )
4068 matroska_segment_c *p_last_segment = linked_segments[linked_segments.size()-1];
4069 // p_last_segment->ParseCluster( );
4071 i_duration = p_last_segment->i_start_time / 1000 + p_last_segment->i_duration;
4076 void virtual_segment_c::LoadCues( )
4078 for ( size_t i=0; i<linked_segments.size(); i++ )
4080 linked_segments[i]->LoadCues();
4084 void virtual_segment_c::AppendUID( const EbmlBinary & UID )
4086 if ( UID.GetBuffer() == NULL )
4089 for (size_t i=0; i<linked_uids.size(); i++)
4091 if ( UID == linked_uids[i] )
4094 linked_uids.push_back( *(KaxSegmentUID*)(&UID) );
4097 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset )
4100 int i_track_skipping;
4101 int64_t i_block_duration;
4102 int64_t i_block_ref1;
4103 int64_t i_block_ref2;
4105 int64_t i_seek_position = i_start_pos;
4106 int64_t i_seek_time = i_start_time;
4112 for( ; i_idx < i_index; i_idx++ )
4114 if( index[i_idx].i_time + i_time_offset > i_date )
4125 i_seek_position = index[i_idx].i_position;
4126 i_seek_time = index[i_idx].i_time;
4129 msg_Dbg( &sys.demuxer, "seek got "I64Fd" (%d%%)",
4130 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
4132 es.I_O().setFilePointer( i_seek_position, seek_beginning );
4135 ep = new EbmlParser( &es, segment, &sys.demuxer );
4138 sys.i_start_pts = i_date;
4140 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
4142 /* now parse until key frame */
4143 #define tk tracks[i_track]
4144 i_track_skipping = 0;
4145 for( i_track = 0; i_track < tracks.size(); i_track++ )
4147 if( tk->fmt.i_cat == VIDEO_ES )
4149 tk->b_search_keyframe = VLC_TRUE;
4152 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_date );
4156 while( i_track_skipping > 0 )
4158 if( BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
4160 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
4165 for( i_track = 0; i_track < tracks.size(); i_track++ )
4167 if( tk->i_number == block->TrackNum() )
4173 sys.i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
4175 if( i_track < tracks.size() )
4177 if( sys.i_pts >= sys.i_start_pts )
4179 BlockDecode( &sys.demuxer, block, sys.i_pts, 0 );
4180 i_track_skipping = 0;
4182 else if( tk->fmt.i_cat == VIDEO_ES )
4184 if( i_block_ref1 == -1 && tk->b_search_keyframe )
4186 tk->b_search_keyframe = VLC_FALSE;
4189 if( !tk->b_search_keyframe )
4191 BlockDecode( &sys.demuxer, block, sys.i_pts, 0 );
4201 void virtual_segment_c::Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter )
4203 demux_sys_t *p_sys = demuxer.p_sys;
4206 // find the actual time for an ordered edition
4207 if ( psz_chapter == NULL )
4209 if ( Edition() && Edition()->b_ordered )
4211 /* 1st, we need to know in which chapter we are */
4212 psz_chapter = (*p_editions)[i_current_edition]->FindTimecode( i_date );
4216 if ( psz_chapter != NULL )
4218 psz_current_chapter = psz_chapter;
4219 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
4220 demuxer.info.i_update |= INPUT_UPDATE_SEEKPOINT;
4221 demuxer.info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
4224 // find the best matching segment
4225 for ( i=0; i<linked_segments.size(); i++ )
4227 if ( i_date < linked_segments[i]->i_start_time )
4234 if ( i_current_segment != i )
4236 linked_segments[i_current_segment]->UnSelect();
4237 linked_segments[i]->Select( i_date );
4238 i_current_segment = i;
4241 linked_segments[i]->Seek( i_date, i_time_offset );
4244 void chapter_codec_cmds_c::AddCommand( const KaxChapterProcessCommand & command )
4248 uint32 codec_time = uint32(-1);
4249 for( i = 0; i < command.ListSize(); i++ )
4251 const EbmlElement *k = command[i];
4253 if( MKV_IS_ID( k, KaxChapterProcessTime ) )
4255 codec_time = uint32( *static_cast<const KaxChapterProcessTime*>( k ) );
4260 for( i = 0; i < command.ListSize(); i++ )
4262 const EbmlElement *k = command[i];
4264 if( MKV_IS_ID( k, KaxChapterProcessData ) )
4266 KaxChapterProcessData *p_data = new KaxChapterProcessData( *static_cast<const KaxChapterProcessData*>( k ) );
4267 switch ( codec_time )
4270 during_cmds.push_back( *p_data );
4273 enter_cmds.push_back( *p_data );
4276 leave_cmds.push_back( *p_data );
4285 bool chapter_item_c::Enter()
4287 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
4288 while ( index != codecs.end() )
4293 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
4294 while ( index_ != sub_chapters.end() )
4302 bool chapter_item_c::Leave()
4304 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
4305 while ( index != codecs.end() )
4310 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
4311 while ( index_ != sub_chapters.end() )
4319 bool dvd_chapter_codec_c::Enter()
4321 std::vector<KaxChapterProcessData>::iterator index = enter_cmds.begin();
4322 while ( index != enter_cmds.end() )
4324 if ( (*index).GetSize() )
4326 binary *p_data = (*index).GetBuffer();
4327 size_t i_size = *p_data++;
4328 // avoid reading too much from the buffer
4329 i_size = min( i_size, ((*index).GetSize() - 1) >> 3 );
4330 for ( ; i_size > 0; i_size--, p_data += 8 )
4332 interpretor.Interpret( p_data );
4340 bool dvd_chapter_codec_c::Leave()
4342 std::vector<KaxChapterProcessData>::iterator index = leave_cmds.begin();
4343 while ( index != leave_cmds.end() )
4345 if ( (*index).GetSize() )
4347 binary *p_data = (*index).GetBuffer();
4348 size_t i_size = *p_data++;
4349 // avoid reading too much from the buffer
4350 i_size = min( i_size, ((*index).GetSize() - 1) >> 3 );
4351 for ( ; i_size > 0; i_size--, p_data += 8 )
4353 interpretor.Interpret( p_data );
4361 bool dvd_command_interpretor_c::Interpret( const binary * p_command, size_t i_size )
4366 uint16 i_command = ( p_command[0] << 8 ) + p_command[1];
4368 switch ( i_command )
4372 uint8 i_title = p_command[5];
4373 msg_Dbg( &sys.demuxer, "DVD command: JumpTT %d", i_title );
4375 // find in the ChapProcessPrivate matching this Title level
4376 virtual_segment_c *p_segment;
4377 chapter_item_c *p_chapter;
4378 p_chapter = sys.BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title), p_segment );
4379 if ( p_chapter != NULL )
4381 // if the segment is not part of the current segment, select the new one
4382 if ( p_segment != sys.p_current_segment )
4384 sys.p_current_segment = p_segment;
4385 sys.i_current_title = p_segment->i_sys_title;
4386 sys.PreparePlayback();
4391 // jump to the location in the found segment
4392 p_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
4397 case CMD_CALLSS_VTSM:
4399 msg_Dbg( &sys.demuxer, "DVD command: CallSS VTSM" );
4404 msg_Dbg( &sys.demuxer, "DVD command: unsupported %02X %02X %02X %02X %02X %02X %02X %02X"
4420 bool dvd_command_interpretor_c::MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
4422 if ( i_cookie_size != 1 || data.m_private_data.GetSize() < 4 )
4425 if ( data.m_private_data.GetBuffer()[0] != 0x28 )
4428 uint16 i_gtitle = (data.m_private_data.GetBuffer()[1] << 8 ) + data.m_private_data.GetBuffer()[2];
4429 uint8 i_title = *(uint8*)p_cookie;
4431 return (i_gtitle == i_title);