1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Steve Lhomme <steve.lhomme@free.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
33 # include <time.h> /* time() */
36 #include <vlc/input.h>
38 #include <codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
53 /* libebml and matroska */
54 #include "ebml/EbmlHead.h"
55 #include "ebml/EbmlSubHead.h"
56 #include "ebml/EbmlStream.h"
57 #include "ebml/EbmlContexts.h"
58 #include "ebml/EbmlVoid.h"
59 #include "ebml/EbmlVersion.h"
60 #include "ebml/StdIOCallback.h"
62 #include "matroska/KaxAttachments.h"
63 #include "matroska/KaxBlock.h"
64 #include "matroska/KaxBlockData.h"
65 #include "matroska/KaxChapters.h"
66 #include "matroska/KaxCluster.h"
67 #include "matroska/KaxClusterData.h"
68 #include "matroska/KaxContexts.h"
69 #include "matroska/KaxCues.h"
70 #include "matroska/KaxCuesData.h"
71 #include "matroska/KaxInfo.h"
72 #include "matroska/KaxInfoData.h"
73 #include "matroska/KaxSeekHead.h"
74 #include "matroska/KaxSegment.h"
75 #include "matroska/KaxTag.h"
76 #include "matroska/KaxTags.h"
77 #include "matroska/KaxTagMulti.h"
78 #include "matroska/KaxTracks.h"
79 #include "matroska/KaxTrackAudio.h"
80 #include "matroska/KaxTrackVideo.h"
81 #include "matroska/KaxTrackEntryData.h"
82 #include "matroska/KaxContentEncoding.h"
83 #include "matroska/KaxVersion.h"
85 #include "ebml/StdIOCallback.h"
88 #include "mp4/libmp4.h"
94 #define MATROSKA_COMPRESSION_NONE 0
95 #define MATROSKA_COMPRESSION_ZLIB 1
97 #define MKVD_TIMECODESCALE 1000000
100 * What's between a directory and a filename?
103 #define DIRECTORY_SEPARATOR '\\'
105 #define DIRECTORY_SEPARATOR '/'
108 using namespace LIBMATROSKA_NAMESPACE;
111 /*****************************************************************************
113 *****************************************************************************/
114 static int Open ( vlc_object_t * );
115 static void Close( vlc_object_t * );
118 set_shortname( _("Matroska") );
119 set_description( _("Matroska stream demuxer" ) );
120 set_capability( "demux2", 50 );
121 set_callbacks( Open, Close );
122 set_category( CAT_INPUT );
123 set_subcategory( SUBCAT_INPUT_DEMUX );
125 add_bool( "mkv-use-ordered-chapters", 1, NULL,
126 N_("Ordered chapters"),
127 N_("Play ordered chapters as specified in the segment"), VLC_TRUE );
129 add_bool( "mkv-use-chapter-codec", 1, NULL,
130 N_("Chapter codecs"),
131 N_("Use chapter codecs found in the segment"), VLC_TRUE );
133 add_bool( "mkv-seek-percent", 0, NULL,
134 N_("Seek based on percent not time"),
135 N_("Seek based on percent not time"), VLC_TRUE );
137 add_bool( "mkv-use-dummy", 0, NULL,
138 N_("Dummy Elements"),
139 N_("Read and discard unknown EBML elements (not good for broken files)"), VLC_TRUE );
141 add_shortcut( "mka" );
142 add_shortcut( "mkv" );
145 /*****************************************************************************
147 *****************************************************************************/
149 block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) {
150 int result, dstsize, n;
155 d_stream.zalloc = (alloc_func)0;
156 d_stream.zfree = (free_func)0;
157 d_stream.opaque = (voidpf)0;
158 result = inflateInit(&d_stream);
161 msg_Dbg( p_this, "inflateInit() failed. Result: %d", result );
165 d_stream.next_in = (Bytef *)p_in_block->p_buffer;
166 d_stream.avail_in = p_in_block->i_buffer;
168 p_block = block_New( p_this, 0 );
173 p_block = block_Realloc( p_block, 0, n * 1000 );
174 dst = (unsigned char *)p_block->p_buffer;
175 d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
176 d_stream.avail_out = 1000;
177 result = inflate(&d_stream, Z_NO_FLUSH);
178 if( ( result != Z_OK ) && ( result != Z_STREAM_END ) )
180 msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result );
184 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
185 ( result != Z_STREAM_END ) );
187 dstsize = d_stream.total_out;
188 inflateEnd( &d_stream );
190 p_block = block_Realloc( p_block, 0, dstsize );
191 p_block->i_buffer = dstsize;
192 block_Release( p_in_block );
199 * Helper function to print the mkv parse tree
201 static void MkvTree( demux_t & demuxer, int i_level, char *psz_format, ... )
206 msg_Err( &demuxer, "too deep tree" );
209 va_start( args, psz_format );
210 static char *psz_foo = "| | | | | | | | | |";
211 char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) );
212 strncpy( psz_foo2, psz_foo, 4 * i_level );
213 psz_foo2[ 4 * i_level ] = '+';
214 psz_foo2[ 4 * i_level + 1 ] = ' ';
215 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
216 __msg_GenericVa( VLC_OBJECT(&demuxer), VLC_MSG_DBG, "mkv", psz_foo2, args );
221 /*****************************************************************************
223 *****************************************************************************/
224 class vlc_stream_io_callback: public IOCallback
231 vlc_stream_io_callback( stream_t * );
233 virtual uint32 read ( void *p_buffer, size_t i_size);
234 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
235 virtual size_t write ( const void *p_buffer, size_t i_size);
236 virtual uint64 getFilePointer ( void );
237 virtual void close ( void );
240 /*****************************************************************************
242 *****************************************************************************/
246 EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux );
247 virtual ~EbmlParser( void );
251 void Reset( demux_t *p_demux );
252 EbmlElement *Get( void );
255 int GetLevel( void );
260 EbmlElement *m_el[10];
261 int64_t mi_remain_size[10];
271 /*****************************************************************************
272 * Some functions to manipulate memory
273 *****************************************************************************/
274 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
275 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
277 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
280 /*****************************************************************************
281 * definitions of structures and functions used by this plugins
282 *****************************************************************************/
285 vlc_bool_t b_default;
286 vlc_bool_t b_enabled;
287 unsigned int i_number;
290 uint8_t *p_extra_data;
294 uint64_t i_default_duration;
295 float f_timecodescale;
303 /* data to be send first */
305 uint8_t *p_data_init;
307 /* hack : it's for seek */
308 vlc_bool_t b_search_keyframe;
312 char *psz_codec_name;
313 char *psz_codec_settings;
314 char *psz_codec_info_url;
315 char *psz_codec_download_url;
317 /* encryption/compression */
318 int i_compression_type;
335 class chapter_codec_cmds_c
338 chapter_codec_cmds_c( int codec_id = -1)
339 :i_codec_id( codec_id )
342 virtual ~chapter_codec_cmds_c() {}
344 void SetPrivate( const KaxChapterProcessPrivate & private_data )
346 m_private_data = *( new KaxChapterProcessPrivate( private_data ) );
349 void AddCommand( const KaxChapterProcessCommand & command );
351 virtual bool Enter() { return true; }
352 virtual bool Leave() { return true; }
353 virtual std::string GetCodecName() const { return ""; }
355 KaxChapterProcessPrivate m_private_data;
358 std::vector<KaxChapterProcessData> enter_cmds;
359 std::vector<KaxChapterProcessData> during_cmds;
360 std::vector<KaxChapterProcessData> leave_cmds;
365 class dvd_command_interpretor_c
368 dvd_command_interpretor_c( demux_sys_t & demuxer )
371 memset( p_GPRM, 0, sizeof(p_GPRM) );
372 memset( p_SPRM, 0, sizeof(p_SPRM) );
379 p_SPRM[ 16 ] = 0xFFFFu;
380 p_SPRM[ 18 ] = 0xFFFFu;
383 bool Interpret( const binary * p_command, size_t i_size = 8 );
386 uint16 GetGPRM( size_t index ) const
388 if ( index >= 0 && index < 16 )
389 return p_GPRM[ index ];
393 uint16 GetSPRM( size_t index ) const
395 // 21,22,23 reserved for future use
396 if ( index >= 0 && index < 21 )
397 return p_SPRM[ index ];
401 bool SetGPRM( size_t index, uint16 value )
403 if ( index >= 0 && index < 16 )
405 p_GPRM[ index ] = value;
411 bool SetSPRM( size_t index, uint16 value )
413 if ( index > 0 && index <= 13 && index != 12 )
415 p_SPRM[ index ] = value;
426 static const uint16 CMD_JUMP_TT = 0x3002;
427 static const uint16 CMD_CALLSS_VTSM = 0x3008;
429 // callbacks when browsing inside CodecPrivate
430 static bool MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
433 class dvd_chapter_codec_c : public chapter_codec_cmds_c
436 dvd_chapter_codec_c( demux_sys_t & sys )
437 :chapter_codec_cmds_c( 1 )
443 std::string GetCodecName() const;
446 dvd_command_interpretor_c interpretor;
449 class matroska_script_codec_c : public chapter_codec_cmds_c
453 class chapter_translation_c
456 KaxChapterTranslateID translated;
457 unsigned int codec_id;
458 std::vector<uint64_t> editions;
467 ,i_user_start_time(-1)
470 ,b_display_seekpoint(true)
474 virtual ~chapter_item_c()
476 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
477 while ( index != codecs.end() )
482 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
483 while ( index_ != sub_chapters.end() )
490 int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time );
491 void PublishChapters( input_title_t & title, int i_level );
492 chapter_item_c * FindTimecode( mtime_t i_timecode );
493 void Append( const chapter_item_c & edition );
494 chapter_item_c * FindChapter( const chapter_item_c & chapter );
495 virtual chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
496 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
497 const void *p_cookie,
498 size_t i_cookie_size );
499 std::string GetCodecName() const;
501 int64_t i_start_time, i_end_time;
502 int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */
503 std::vector<chapter_item_c*> sub_chapters;
506 bool b_display_seekpoint;
507 std::string psz_name;
508 chapter_item_c *psz_parent;
510 std::vector<chapter_codec_cmds_c*> codecs;
512 bool operator<( const chapter_item_c & item ) const
514 return ( i_user_start_time < item.i_user_start_time || (i_user_start_time == item.i_user_start_time && i_user_end_time < item.i_user_end_time) );
521 class chapter_edition_c : public chapter_item_c
528 void RefreshChapters( );
529 mtime_t Duration() const;
530 void PublishChapters( input_title_t & title );
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
1815 void chapter_edition_c::PublishChapters( input_title_t & title )
1817 chapter_item_c::PublishChapters( title, 0 );
1820 void chapter_item_c::PublishChapters( input_title_t & title, int i_level )
1822 // add support for meta-elements from codec like DVD Titles
1823 if ( !b_display_seekpoint || psz_name == "" )
1825 psz_name = GetCodecName();
1826 if ( psz_name != "" )
1827 b_display_seekpoint = true;
1830 if (b_display_seekpoint)
1832 seekpoint_t *sk = vlc_seekpoint_New();
1834 sk->i_level = i_level;
1835 sk->i_time_offset = i_start_time;
1836 sk->psz_name = strdup( psz_name.c_str() );
1838 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
1839 title.i_seekpoint++;
1840 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
1841 title.seekpoint[title.i_seekpoint-1] = sk;
1843 i_seekpoint_num = title.i_seekpoint;
1846 for ( size_t i=0; i<sub_chapters.size() ; i++)
1848 sub_chapters[i]->PublishChapters( title, i_level+1 );
1852 void virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
1854 demux_sys_t & sys = *demux.p_sys;
1855 chapter_item_c *psz_curr_chapter;
1857 /* update current chapter/seekpoint */
1858 if ( p_editions->size() )
1860 /* 1st, we need to know in which chapter we are */
1861 psz_curr_chapter = (*p_editions)[i_current_edition]->FindTimecode( sys.i_pts );
1863 /* we have moved to a new chapter */
1864 if (psz_curr_chapter != NULL && psz_current_chapter != psz_curr_chapter)
1866 if ( psz_current_chapter != NULL )
1867 psz_current_chapter->Leave();
1869 if ( psz_curr_chapter->i_seekpoint_num > 0 )
1871 demux.info.i_update |= INPUT_UPDATE_SEEKPOINT;
1872 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
1875 if ( (*p_editions)[i_current_edition]->b_ordered )
1877 psz_curr_chapter->Enter();
1879 // only seek if necessary
1880 if ( psz_current_chapter == NULL || (psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time) )
1881 Seek( demux, sys.i_pts, 0, psz_curr_chapter );
1884 psz_current_chapter = psz_curr_chapter;
1888 chapter_item_c *virtual_segment_c::BrowseCodecPrivate( unsigned int codec_id,
1889 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1890 const void *p_cookie,
1891 size_t i_cookie_size )
1893 // FIXME don't assume it is the first edition
1894 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
1895 if ( index != p_editions->end() )
1897 chapter_item_c *p_result = (*index)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
1898 if ( p_result != NULL )
1904 chapter_item_c *chapter_item_c::BrowseCodecPrivate( unsigned int codec_id,
1905 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1906 const void *p_cookie,
1907 size_t i_cookie_size )
1910 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
1911 while ( index != codecs.end() )
1913 if ( match( **index ,p_cookie, i_cookie_size ) )
1919 chapter_item_c *p_result = NULL;
1920 std::vector<chapter_item_c*>::const_iterator index2 = sub_chapters.begin();
1921 while ( index2 != sub_chapters.end() )
1923 p_result = (*index2)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
1924 if ( p_result != NULL )
1932 void chapter_item_c::Append( const chapter_item_c & chapter )
1934 // we are appending content for the same chapter UID
1936 chapter_item_c *p_chapter;
1938 for ( i=0; i<chapter.sub_chapters.size(); i++ )
1940 p_chapter = FindChapter( *chapter.sub_chapters[i] );
1941 if ( p_chapter != NULL )
1943 p_chapter->Append( *chapter.sub_chapters[i] );
1947 sub_chapters.push_back( chapter.sub_chapters[i] );
1951 i_user_start_time = min( i_user_start_time, chapter.i_user_start_time );
1952 i_user_end_time = max( i_user_end_time, chapter.i_user_end_time );
1955 chapter_item_c * chapter_item_c::FindChapter( const chapter_item_c & chapter )
1958 for ( i=0; i<sub_chapters.size(); i++)
1960 if ( sub_chapters[i]->i_uid == chapter.i_uid )
1961 return sub_chapters[i];
1966 std::string chapter_item_c::GetCodecName() const
1970 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
1971 while ( index != codecs.end() )
1973 result = (*index)->GetCodecName();
1982 std::string dvd_chapter_codec_c::GetCodecName() const
1985 if ( m_private_data.GetSize() >= 3)
1987 const binary* p_data = m_private_data.GetBuffer();
1988 if ( p_data[0] == 0x28 )
1990 uint16_t i_title = (p_data[1] << 8) + p_data[2];
1992 sprintf( psz_str, " %d ---", i_title );
1993 result = N_("--- DVD Title");
1996 else if ( p_data[0] == 0x2A )
1999 sprintf( psz_str, " (%c%c) ---", p_data[1], p_data[2] );
2000 result = N_("--- DVD Menu");
2003 else if ( p_data[0] == 0x30 )
2005 if ( p_data[1] == 0x00 )
2006 result = N_("First Played");
2007 else if ( p_data[1] == 0xC0 )
2008 result = N_("Video Manager");
2009 else if ( p_data[1] == 0x80 )
2011 uint16_t i_title = (p_data[2] << 8) + p_data[3];
2013 sprintf( psz_str, " %d -----", i_title );
2014 result = N_("----- Title");
2023 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter )
2025 demux_sys_t *p_sys = p_demux->p_sys;
2026 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
2027 matroska_segment_c *p_segment = p_vsegment->Segment();
2028 mtime_t i_time_offset = 0;
2032 msg_Dbg( p_demux, "seek request to "I64Fd" (%f%%)", i_date, f_percent );
2033 if( i_date < 0 && f_percent < 0 )
2035 msg_Warn( p_demux, "cannot seek nowhere !" );
2038 if( f_percent > 1.0 )
2040 msg_Warn( p_demux, "cannot seek so far !" );
2044 /* seek without index or without date */
2045 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
2047 if (p_sys->f_duration >= 0)
2049 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
2053 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
2055 msg_Dbg( p_demux, "inacurate way of seeking" );
2056 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
2058 if( p_segment->index[i_index].i_position >= i_pos)
2063 if( i_index == p_segment->i_index )
2068 i_date = p_segment->index[i_index].i_time;
2071 if( p_segment->index[i_index].i_position < i_pos )
2075 msg_Warn( p_demux, "searching for cluster, could take some time" );
2077 /* search a cluster */
2078 while( ( el = p_sys->ep->Get() ) != NULL )
2080 if( MKV_IS_ID( el, KaxCluster ) )
2082 KaxCluster *cluster = (KaxCluster*)el;
2084 /* add it to the index */
2085 p_segment->IndexAppendCluster( cluster );
2087 if( (int64_t)cluster->GetElementPosition() >= i_pos )
2089 p_sys->cluster = cluster;
2100 p_vsegment->Seek( *p_demux, i_date, i_time_offset, psz_chapter );
2103 /*****************************************************************************
2104 * Demux: reads and demuxes data packets
2105 *****************************************************************************
2106 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
2107 *****************************************************************************/
2108 static int Demux( demux_t *p_demux)
2110 demux_sys_t *p_sys = p_demux->p_sys;
2111 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
2112 matroska_segment_c *p_segmet = p_vsegment->Segment();
2113 if ( p_segmet == NULL ) return 0;
2114 int i_block_count = 0;
2117 int64_t i_block_duration;
2118 int64_t i_block_ref1;
2119 int64_t i_block_ref2;
2123 if ( p_sys->demuxer.b_die )
2126 if( p_sys->i_pts >= p_sys->i_start_pts )
2127 p_vsegment->UpdateCurrentToChapter( *p_demux );
2129 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
2131 /* nothing left to read in this ordered edition */
2132 if ( !p_vsegment->SelectNext() )
2134 p_segmet->UnSelect( );
2136 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
2138 /* switch to the next segment */
2139 p_segmet = p_vsegment->Segment();
2140 if ( !p_segmet->Select( 0 ) )
2142 msg_Err( p_demux, "Failed to select new segment" );
2149 if( p_segmet->BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
2151 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered )
2153 const chapter_item_c *p_chap = p_vsegment->CurrentChapter();
2154 // check if there are more chapters to read
2155 if ( p_chap != NULL )
2157 /* TODO handle successive chapters with the same user_start_time/user_end_time
2158 if ( p_chap->i_user_start_time == p_chap->i_user_start_time )
2159 p_vsegment->SelectNext();
2161 p_sys->i_pts = p_chap->i_user_end_time;
2162 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
2169 msg_Warn( p_demux, "cannot get block EOF?" );
2170 p_segmet->UnSelect( );
2172 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
2174 /* switch to the next segment */
2175 if ( !p_vsegment->SelectNext() )
2176 // no more segments in this stream
2178 p_segmet = p_vsegment->Segment();
2179 if ( !p_segmet->Select( 0 ) )
2181 msg_Err( p_demux, "Failed to select new segment" );
2188 p_sys->i_pts = p_sys->i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
2190 if( p_sys->i_pts >= p_sys->i_start_pts )
2192 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
2195 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
2200 // TODO optimize when there is need to leave or when seeking has been called
2201 if( i_block_count > 5 )
2210 /*****************************************************************************
2212 *****************************************************************************/
2213 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
2219 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
2221 if( i_size <= 0 || mb_eof )
2226 return stream_Read( s, p_buffer, i_size );
2228 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
2234 case seek_beginning:
2238 i_pos = stream_Size( s ) - i_offset;
2241 i_pos= stream_Tell( s ) + i_offset;
2245 if( i_pos < 0 || i_pos >= stream_Size( s ) )
2252 if( stream_Seek( s, i_pos ) )
2258 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
2262 uint64 vlc_stream_io_callback::getFilePointer( void )
2264 return stream_Tell( s );
2266 void vlc_stream_io_callback::close( void )
2272 /*****************************************************************************
2273 * Ebml Stream parser
2274 *****************************************************************************/
2275 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux )
2282 mi_remain_size[0] = el_start->GetSize();
2284 for( i = 1; i < 6; i++ )
2290 mb_keep = VLC_FALSE;
2291 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
2294 EbmlParser::~EbmlParser( void )
2298 for( i = 1; i < mi_level; i++ )
2304 mb_keep = VLC_FALSE;
2308 void EbmlParser::Up( void )
2310 if( mi_user_level == mi_level )
2312 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
2318 void EbmlParser::Down( void )
2324 void EbmlParser::Keep( void )
2329 int EbmlParser::GetLevel( void )
2331 return mi_user_level;
2334 void EbmlParser::Reset( demux_t *p_demux )
2336 while ( mi_level > 0)
2338 delete m_el[mi_level];
2339 m_el[mi_level] = NULL;
2342 mi_user_level = mi_level = 1;
2343 #if LIBEBML_VERSION >= 0x000704
2344 // a little faster and cleaner
2345 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
2347 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
2349 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
2352 EbmlElement *EbmlParser::Get( void )
2356 if( mi_user_level != mi_level )
2362 EbmlElement *ret = m_got;
2368 if( m_el[mi_level] )
2370 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
2373 delete m_el[mi_level];
2375 mb_keep = VLC_FALSE;
2378 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, mb_dummy, 1 );
2379 // mi_remain_size[mi_level] = m_el[mi_level]->GetSize();
2390 delete m_el[mi_level - 1];
2391 m_got = m_el[mi_level -1] = m_el[mi_level];
2392 m_el[mi_level] = NULL;
2399 else if( m_el[mi_level] == NULL )
2401 fprintf( stderr," m_el[mi_level] == NULL\n" );
2404 return m_el[mi_level];
2408 /*****************************************************************************
2410 * * LoadCues : load the cues element and update index
2412 * * LoadTags : load ... the tags element
2414 * * InformationCreate : create all information, load tags if present
2416 *****************************************************************************/
2417 void matroska_segment_c::LoadCues( )
2419 int64_t i_sav_position = es.I_O().getFilePointer();
2421 EbmlElement *el, *cues;
2423 /* *** Load the cue if found *** */
2424 if( i_cues_position < 0 )
2426 msg_Warn( &sys.demuxer, "no cues/empty cues found->seek won't be precise" );
2428 // IndexAppendCluster( cluster );
2431 vlc_bool_t b_seekable;
2433 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
2437 msg_Dbg( &sys.demuxer, "loading cues" );
2438 es.I_O().setFilePointer( i_cues_position, seek_beginning );
2439 cues = es.FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
2443 msg_Err( &sys.demuxer, "cannot load cues (broken seekhead or file)" );
2444 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2448 ep = new EbmlParser( &es, cues, &sys.demuxer );
2449 while( ( el = ep->Get() ) != NULL )
2451 if( MKV_IS_ID( el, KaxCuePoint ) )
2453 #define idx index[i_index]
2456 idx.i_block_number= -1;
2457 idx.i_position = -1;
2459 idx.b_key = VLC_TRUE;
2462 while( ( el = ep->Get() ) != NULL )
2464 if( MKV_IS_ID( el, KaxCueTime ) )
2466 KaxCueTime &ctime = *(KaxCueTime*)el;
2468 ctime.ReadData( es.I_O() );
2470 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
2472 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
2475 while( ( el = ep->Get() ) != NULL )
2477 if( MKV_IS_ID( el, KaxCueTrack ) )
2479 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
2481 ctrack.ReadData( es.I_O() );
2482 idx.i_track = uint16( ctrack );
2484 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
2486 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
2488 ccpos.ReadData( es.I_O() );
2489 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
2491 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
2493 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2495 cbnum.ReadData( es.I_O() );
2496 idx.i_block_number = uint32( cbnum );
2500 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2507 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2513 msg_Dbg( &sys.demuxer, " * added time="I64Fd" pos="I64Fd
2514 " track=%d bnum=%d", idx.i_time, idx.i_position,
2515 idx.i_track, idx.i_block_number );
2519 if( i_index >= i_index_max )
2521 i_index_max += 1024;
2522 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
2528 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
2536 msg_Dbg( &sys.demuxer, "loading cues done." );
2537 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2540 void matroska_segment_c::LoadTags( )
2542 int64_t i_sav_position = es.I_O().getFilePointer();
2544 EbmlElement *el, *tags;
2546 msg_Dbg( &sys.demuxer, "loading tags" );
2547 es.I_O().setFilePointer( i_tags_position, seek_beginning );
2548 tags = es.FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2552 msg_Err( &sys.demuxer, "cannot load tags (broken seekhead or file)" );
2553 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2557 msg_Dbg( &sys.demuxer, "Tags" );
2558 ep = new EbmlParser( &es, tags, &sys.demuxer );
2559 while( ( el = ep->Get() ) != NULL )
2561 if( MKV_IS_ID( el, KaxTag ) )
2563 msg_Dbg( &sys.demuxer, "+ Tag" );
2565 while( ( el = ep->Get() ) != NULL )
2567 if( MKV_IS_ID( el, KaxTagTargets ) )
2569 msg_Dbg( &sys.demuxer, "| + Targets" );
2571 while( ( el = ep->Get() ) != NULL )
2573 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2577 else if( MKV_IS_ID( el, KaxTagGeneral ) )
2579 msg_Dbg( &sys.demuxer, "| + General" );
2581 while( ( el = ep->Get() ) != NULL )
2583 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2587 else if( MKV_IS_ID( el, KaxTagGenres ) )
2589 msg_Dbg( &sys.demuxer, "| + Genres" );
2591 while( ( el = ep->Get() ) != NULL )
2593 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2597 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
2599 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
2601 while( ( el = ep->Get() ) != NULL )
2603 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2607 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
2609 msg_Dbg( &sys.demuxer, "| + Images Specific" );
2611 while( ( el = ep->Get() ) != NULL )
2613 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
2617 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
2619 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
2621 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
2623 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
2625 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
2627 msg_Dbg( &sys.demuxer, "| + Multi Date" );
2629 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
2631 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
2633 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
2635 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
2637 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
2639 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
2641 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
2643 msg_Dbg( &sys.demuxer, "| + Multi Title" );
2647 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid( *el ).name() );
2654 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
2660 msg_Dbg( &sys.demuxer, "loading tags done." );
2661 es.I_O().setFilePointer( i_sav_position, seek_beginning );
2664 /*****************************************************************************
2666 *****************************************************************************/
2667 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
2671 int i_upper_level = 0;
2673 msg_Dbg( &sys.demuxer, "| + Seek head" );
2675 /* Master elements */
2676 seekhead->Read( es, seekhead->Generic().Context, i_upper_level, el, true );
2678 for( i = 0; i < seekhead->ListSize(); i++ )
2680 EbmlElement *l = (*seekhead)[i];
2682 if( MKV_IS_ID( l, KaxSeek ) )
2684 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
2685 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
2688 for( j = 0; j < sk->ListSize(); j++ )
2690 EbmlElement *l = (*sk)[j];
2692 if( MKV_IS_ID( l, KaxSeekID ) )
2694 KaxSeekID &sid = *(KaxSeekID*)l;
2695 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
2697 else if( MKV_IS_ID( l, KaxSeekPosition ) )
2699 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
2700 i_pos = uint64( spos );
2704 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
2710 if( id == KaxCues::ClassInfos.GlobalId )
2712 msg_Dbg( &sys.demuxer, "| | | = cues at "I64Fd, i_pos );
2713 i_cues_position = segment->GetGlobalPosition( i_pos );
2715 else if( id == KaxChapters::ClassInfos.GlobalId )
2717 msg_Dbg( &sys.demuxer, "| | | = chapters at "I64Fd, i_pos );
2718 i_chapters_position = segment->GetGlobalPosition( i_pos );
2720 else if( id == KaxTags::ClassInfos.GlobalId )
2722 msg_Dbg( &sys.demuxer, "| | | = tags at "I64Fd, i_pos );
2723 i_tags_position = segment->GetGlobalPosition( i_pos );
2729 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
2734 /*****************************************************************************
2736 *****************************************************************************/
2737 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
2743 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
2745 tk = new mkv_track_t();
2746 tracks.push_back( tk );
2748 /* Init the track */
2749 memset( tk, 0, sizeof( mkv_track_t ) );
2751 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
2752 tk->fmt.psz_language = strdup("English");
2753 tk->fmt.psz_description = NULL;
2755 tk->b_default = VLC_TRUE;
2756 tk->b_enabled = VLC_TRUE;
2757 tk->b_silent = VLC_FALSE;
2758 tk->i_number = tracks.size() - 1;
2759 tk->i_extra_data = 0;
2760 tk->p_extra_data = NULL;
2761 tk->psz_codec = NULL;
2762 tk->i_default_duration = 0;
2763 tk->f_timecodescale = 1.0;
2765 tk->b_inited = VLC_FALSE;
2766 tk->i_data_init = 0;
2767 tk->p_data_init = NULL;
2769 tk->psz_codec_name = NULL;
2770 tk->psz_codec_settings = NULL;
2771 tk->psz_codec_info_url = NULL;
2772 tk->psz_codec_download_url = NULL;
2774 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
2776 for( i = 0; i < m->ListSize(); i++ )
2778 EbmlElement *l = (*m)[i];
2780 if( MKV_IS_ID( l, KaxTrackNumber ) )
2782 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
2784 tk->i_number = uint32( tnum );
2785 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
2787 else if( MKV_IS_ID( l, KaxTrackUID ) )
2789 KaxTrackUID &tuid = *(KaxTrackUID*)l;
2791 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
2793 else if( MKV_IS_ID( l, KaxTrackType ) )
2796 KaxTrackType &ttype = *(KaxTrackType*)l;
2798 switch( uint8(ttype) )
2802 tk->fmt.i_cat = AUDIO_ES;
2806 tk->fmt.i_cat = VIDEO_ES;
2808 case track_subtitle:
2809 psz_type = "subtitle";
2810 tk->fmt.i_cat = SPU_ES;
2813 psz_type = "unknown";
2814 tk->fmt.i_cat = UNKNOWN_ES;
2818 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
2820 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
2822 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
2824 // tk->b_enabled = uint32( fenb );
2825 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
2826 // uint32( fenb ) );
2828 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
2830 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
2832 tk->b_default = uint32( fdef );
2833 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
2835 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
2837 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
2839 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
2841 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
2843 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
2845 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
2847 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
2849 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
2851 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
2853 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
2855 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
2857 tk->i_default_duration = uint64(defd);
2858 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration="I64Fd, uint64(defd) );
2860 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
2862 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
2864 tk->f_timecodescale = float( ttcs );
2865 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
2867 else if( MKV_IS_ID( l, KaxTrackName ) )
2869 KaxTrackName &tname = *(KaxTrackName*)l;
2871 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
2872 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
2874 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
2876 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
2878 tk->fmt.psz_language = strdup( string( lang ).c_str() );
2879 msg_Dbg( &sys.demuxer,
2880 "| | | + Track Language=`%s'", tk->fmt.psz_language );
2882 else if( MKV_IS_ID( l, KaxCodecID ) )
2884 KaxCodecID &codecid = *(KaxCodecID*)l;
2886 tk->psz_codec = strdup( string( codecid ).c_str() );
2887 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
2889 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
2891 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
2893 tk->i_extra_data = cpriv.GetSize();
2894 if( tk->i_extra_data > 0 )
2896 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
2897 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
2899 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
2901 else if( MKV_IS_ID( l, KaxCodecName ) )
2903 KaxCodecName &cname = *(KaxCodecName*)l;
2905 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
2906 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
2908 else if( MKV_IS_ID( l, KaxContentEncodings ) )
2910 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
2911 MkvTree( sys.demuxer, 3, "Content Encodings" );
2912 for( j = 0; j < cencs->ListSize(); j++ )
2914 EbmlElement *l2 = (*cencs)[j];
2915 if( MKV_IS_ID( l2, KaxContentEncoding ) )
2917 MkvTree( sys.demuxer, 4, "Content Encoding" );
2918 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
2919 for( k = 0; k < cenc->ListSize(); k++ )
2921 EbmlElement *l3 = (*cenc)[k];
2922 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
2924 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
2925 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
2927 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
2929 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
2930 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
2932 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
2934 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
2935 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
2937 else if( MKV_IS_ID( l3, KaxContentCompression ) )
2939 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
2940 MkvTree( sys.demuxer, 5, "Content Compression" );
2941 for( n = 0; n < compr->ListSize(); n++ )
2943 EbmlElement *l4 = (*compr)[n];
2944 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
2946 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
2947 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
2948 if( uint32( compalg ) == 0 )
2950 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
2955 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
2962 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
2969 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
2974 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
2976 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
2978 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
2979 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
2981 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
2983 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
2985 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
2986 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
2988 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
2990 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
2992 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
2993 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
2995 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
2997 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
2999 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
3001 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
3003 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
3005 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
3007 else if( MKV_IS_ID( l, KaxTrackVideo ) )
3009 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
3012 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
3015 for( j = 0; j < tkv->ListSize(); j++ )
3017 EbmlElement *l = (*tkv)[j];
3018 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
3020 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
3022 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
3024 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
3026 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
3028 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
3031 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
3033 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
3035 tk->fmt.video.i_width = uint16( vwidth );
3036 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
3038 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
3040 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
3042 tk->fmt.video.i_height = uint16( vheight );
3043 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
3045 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
3047 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
3049 tk->fmt.video.i_visible_width = uint16( vwidth );
3050 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
3052 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
3054 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
3056 tk->fmt.video.i_visible_height = uint16( vheight );
3057 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
3059 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
3061 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
3063 tk->f_fps = float( vfps );
3064 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
3066 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
3068 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
3070 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
3071 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
3073 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
3075 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
3077 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
3079 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
3081 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
3083 // msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( gamma ) );
3087 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
3090 if ( tk->fmt.video.i_visible_height && tk->fmt.video.i_visible_width )
3091 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * tk->fmt.video.i_visible_width / tk->fmt.video.i_visible_height;
3093 else if( MKV_IS_ID( l, KaxTrackAudio ) )
3095 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
3098 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
3100 for( j = 0; j < tka->ListSize(); j++ )
3102 EbmlElement *l = (*tka)[j];
3104 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
3106 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
3108 tk->fmt.audio.i_rate = (int)float( afreq );
3109 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
3111 else if( MKV_IS_ID( l, KaxAudioChannels ) )
3113 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
3115 tk->fmt.audio.i_channels = uint8( achan );
3116 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
3118 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
3120 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
3122 tk->fmt.audio.i_bitspersample = uint8( abits );
3123 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
3127 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
3133 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
3134 typeid(*l).name() );
3139 /*****************************************************************************
3141 *****************************************************************************/
3142 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
3146 int i_upper_level = 0;
3148 msg_Dbg( &sys.demuxer, "| + Tracks" );
3150 /* Master elements */
3151 tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
3153 for( i = 0; i < tracks->ListSize(); i++ )
3155 EbmlElement *l = (*tracks)[i];
3157 if( MKV_IS_ID( l, KaxTrackEntry ) )
3159 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
3163 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3168 /*****************************************************************************
3170 *****************************************************************************/
3171 void matroska_segment_c::ParseInfo( KaxInfo *info )
3176 int i_upper_level = 0;
3178 msg_Dbg( &sys.demuxer, "| + Information" );
3180 /* Master elements */
3181 m = static_cast<EbmlMaster *>(info);
3182 m->Read( es, info->Generic().Context, i_upper_level, el, true );
3184 for( i = 0; i < m->ListSize(); i++ )
3186 EbmlElement *l = (*m)[i];
3188 if( MKV_IS_ID( l, KaxSegmentUID ) )
3190 segment_uid = *(new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l)));
3192 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)segment_uid.GetBuffer() );
3194 else if( MKV_IS_ID( l, KaxPrevUID ) )
3196 prev_segment_uid = *(new KaxPrevUID(*static_cast<KaxPrevUID*>(l)));
3198 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)prev_segment_uid.GetBuffer() );
3200 else if( MKV_IS_ID( l, KaxNextUID ) )
3202 next_segment_uid = *(new KaxNextUID(*static_cast<KaxNextUID*>(l)));
3204 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)next_segment_uid.GetBuffer() );
3206 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
3208 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
3210 i_timescale = uint64(tcs);
3212 msg_Dbg( &sys.demuxer, "| | + TimecodeScale="I64Fd,
3215 else if( MKV_IS_ID( l, KaxDuration ) )
3217 KaxDuration &dur = *(KaxDuration*)l;
3219 i_duration = mtime_t( double( dur ) );
3221 msg_Dbg( &sys.demuxer, "| | + Duration="I64Fd,
3224 else if( MKV_IS_ID( l, KaxMuxingApp ) )
3226 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
3228 psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
3230 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
3231 psz_muxing_application );
3233 else if( MKV_IS_ID( l, KaxWritingApp ) )
3235 KaxWritingApp &wapp = *(KaxWritingApp*)l;
3237 psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
3239 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
3240 psz_writing_application );
3242 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
3244 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
3246 psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
3248 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
3249 psz_segment_filename );
3251 else if( MKV_IS_ID( l, KaxTitle ) )
3253 KaxTitle &title = *(KaxTitle*)l;
3255 psz_title = UTF8ToStr( UTFstring( title ) );
3257 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
3259 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
3261 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
3263 families.push_back(*uid);
3265 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
3267 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
3268 else if( MKV_IS_ID( l, KaxDateUTC ) )
3270 KaxDateUTC &date = *(KaxDateUTC*)l;
3275 i_date = date.GetEpochDate();
3276 memset( buffer, 0, 256 );
3277 if( gmtime_r( &i_date, &tmres ) &&
3278 asctime_r( &tmres, buffer ) )
3280 buffer[strlen( buffer)-1]= '\0';
3281 psz_date_utc = strdup( buffer );
3282 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
3286 #if LIBMATROSKA_VERSION >= 0x000704
3287 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
3289 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
3290 chapter_translation_c translated;
3292 p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
3293 for( j = 0; j < p_trans->ListSize(); j++ )
3295 EbmlElement *l = (*p_trans)[j];
3297 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
3299 translated.editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
3301 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
3303 translated.codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
3305 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
3307 translated.translated = *( new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) ) );
3311 translations.push_back( translated );
3316 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3320 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
3321 i_duration = mtime_t(f_dur);
3325 /*****************************************************************************
3327 *****************************************************************************/
3328 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
3332 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
3333 for( i = 0; i < ca->ListSize(); i++ )
3335 EbmlElement *l = (*ca)[i];
3337 if( MKV_IS_ID( l, KaxChapterUID ) )
3339 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
3340 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
3342 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
3344 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
3345 chapters.b_display_seekpoint = uint8( flag ) == 0;
3347 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
3349 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
3351 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
3352 chapters.i_start_time = uint64( start ) / I64C(1000);
3354 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
3356 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
3358 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
3359 chapters.i_end_time = uint64( end ) / I64C(1000);
3361 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
3363 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
3365 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
3367 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
3368 for( j = 0; j < cd->ListSize(); j++ )
3370 EbmlElement *l= (*cd)[j];
3372 if( MKV_IS_ID( l, KaxChapterString ) )
3376 KaxChapterString &name =*(KaxChapterString*)l;
3377 for (k = 0; k < i_level; k++)
3378 chapters.psz_name += '+';
3379 chapters.psz_name += ' ';
3380 chapters.psz_name += UTF8ToStr( UTFstring( name ) );
3382 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", UTF8ToStr(UTFstring(name)) );
3384 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
3386 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
3387 const char *psz = string( lang ).c_str();
3389 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
3391 else if( MKV_IS_ID( l, KaxChapterCountry ) )
3393 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
3394 const char *psz = string( ct ).c_str();
3396 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
3400 else if( MKV_IS_ID( l, KaxChapterProcess ) )
3402 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
3404 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
3405 chapter_codec_cmds_c *p_ccodec = NULL;
3407 for( j = 0; j < cp->ListSize(); j++ )
3409 EbmlElement *k= (*cp)[j];
3411 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
3413 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
3414 if ( uint32(*p_codec_id) == 0 )
3415 p_ccodec = new matroska_script_codec_c();
3416 else if ( uint32(*p_codec_id) == 1 )
3417 p_ccodec = new dvd_chapter_codec_c( sys );
3422 if ( p_ccodec != NULL )
3424 for( j = 0; j < cp->ListSize(); j++ )
3426 EbmlElement *k= (*cp)[j];
3428 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
3430 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
3431 p_ccodec->SetPrivate( *p_private );
3433 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
3435 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
3438 chapters.codecs.push_back( p_ccodec );
3441 else if( MKV_IS_ID( l, KaxChapterAtom ) )
3443 chapter_item_c *new_sub_chapter = new chapter_item_c();
3444 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
3445 new_sub_chapter->psz_parent = &chapters;
3446 chapters.sub_chapters.push_back( new_sub_chapter );
3451 /*****************************************************************************
3453 *****************************************************************************/
3454 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
3458 int i_upper_level = 0;
3461 /* Master elements */
3462 chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
3464 for( i = 0; i < chapters->ListSize(); i++ )
3466 EbmlElement *l = (*chapters)[i];
3468 if( MKV_IS_ID( l, KaxEditionEntry ) )
3470 chapter_edition_c *p_edition = new chapter_edition_c();
3472 EbmlMaster *E = static_cast<EbmlMaster *>(l );
3474 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
3475 for( j = 0; j < E->ListSize(); j++ )
3477 EbmlElement *l = (*E)[j];
3479 if( MKV_IS_ID( l, KaxChapterAtom ) )
3481 chapter_item_c *new_sub_chapter = new chapter_item_c();
3482 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
3483 p_edition->sub_chapters.push_back( new_sub_chapter );
3485 else if( MKV_IS_ID( l, KaxEditionUID ) )
3487 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
3489 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
3491 p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
3493 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
3495 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
3496 i_default_edition = stored_editions.size();
3500 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
3503 stored_editions.push_back( p_edition );
3507 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3511 for( i = 0; i < stored_editions.size(); i++ )
3513 stored_editions[i]->RefreshChapters( );
3516 if ( stored_editions[i_default_edition]->b_ordered )
3518 /* update the duration of the segment according to the sum of all sub chapters */
3519 i_dur = stored_editions[i_default_edition]->Duration() / I64C(1000);
3525 void matroska_segment_c::ParseCluster( )
3530 int i_upper_level = 0;
3532 /* Master elements */
3533 m = static_cast<EbmlMaster *>( cluster );
3534 m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
3536 for( i = 0; i < m->ListSize(); i++ )
3538 EbmlElement *l = (*m)[i];
3540 if( MKV_IS_ID( l, KaxClusterTimecode ) )
3542 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
3544 cluster->InitTimecode( uint64( ctc ), i_timescale );
3549 i_start_time = cluster->GlobalTimecode() / 1000;
3552 /*****************************************************************************
3553 * InformationCreate:
3554 *****************************************************************************/
3555 void matroska_segment_c::InformationCreate( )
3559 sys.meta = vlc_meta_New();
3563 vlc_meta_Add( sys.meta, VLC_META_TITLE, psz_title );
3567 vlc_meta_Add( sys.meta, VLC_META_DATE, psz_date_utc );
3569 if( psz_segment_filename )
3571 vlc_meta_Add( sys.meta, _("Segment filename"), psz_segment_filename );
3573 if( psz_muxing_application )
3575 vlc_meta_Add( sys.meta, _("Muxing application"), psz_muxing_application );
3577 if( psz_writing_application )
3579 vlc_meta_Add( sys.meta, _("Writing application"), psz_writing_application );
3582 for( i_track = 0; i_track < tracks.size(); i_track++ )
3584 mkv_track_t *tk = tracks[i_track];
3585 vlc_meta_t *mtk = vlc_meta_New();
3587 sys.meta->track = (vlc_meta_t**)realloc( sys.meta->track,
3588 sizeof( vlc_meta_t * ) * ( sys.meta->i_track + 1 ) );
3589 sys.meta->track[sys.meta->i_track++] = mtk;
3591 if( tk->fmt.psz_description )
3593 vlc_meta_Add( sys.meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
3595 if( tk->psz_codec_name )
3597 vlc_meta_Add( sys.meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
3599 if( tk->psz_codec_settings )
3601 vlc_meta_Add( sys.meta, VLC_META_SETTING, tk->psz_codec_settings );
3603 if( tk->psz_codec_info_url )
3605 vlc_meta_Add( sys.meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
3607 if( tk->psz_codec_download_url )
3609 vlc_meta_Add( sys.meta, VLC_META_URL, tk->psz_codec_download_url );
3613 if( i_tags_position >= 0 )
3615 vlc_bool_t b_seekable;
3617 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
3626 /*****************************************************************************
3628 *****************************************************************************/
3630 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
3632 #define idx index[i_index]
3634 idx.i_block_number= -1;
3635 idx.i_position = cluster->GetElementPosition();
3637 idx.b_key = VLC_TRUE;
3640 if( i_index >= i_index_max )
3642 i_index_max += 1024;
3643 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
3648 static char * UTF8ToStr( const UTFstring &u )
3657 p = dst = (char*)malloc( i_src + 1);
3676 void chapter_edition_c::RefreshChapters( )
3678 chapter_item_c::RefreshChapters( b_ordered, -1 );
3679 b_display_seekpoint = false;
3682 int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_time )
3684 int64_t i_user_time = i_prev_user_time;
3686 // first the sub-chapters, and then ourself
3687 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
3688 while ( index != sub_chapters.end() )
3690 i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
3696 // the ordered chapters always start at zero
3697 if ( i_prev_user_time == -1 )
3699 if ( i_user_time == -1 )
3701 i_prev_user_time = 0;
3704 i_user_start_time = i_prev_user_time;
3705 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
3707 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
3711 i_user_end_time = i_user_time;
3716 std::sort( sub_chapters.begin(), sub_chapters.end() );
3717 i_user_start_time = i_start_time;
3718 if ( i_end_time != -1 )
3719 i_user_end_time = i_end_time;
3720 else if ( i_user_time != -1 )
3721 i_user_end_time = i_user_time;
3723 i_user_end_time = i_user_start_time;
3726 return i_user_end_time;
3729 mtime_t chapter_edition_c::Duration() const
3731 mtime_t i_result = 0;
3733 if ( sub_chapters.size() )
3735 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
3737 i_result = (*index)->i_user_end_time;
3743 chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode )
3745 chapter_item_c *psz_result = NULL;
3747 if ( i_user_timecode >= i_user_start_time &&
3748 ( i_user_timecode < i_user_end_time ||
3749 ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
3751 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
3752 while ( index != sub_chapters.end() && psz_result == NULL )
3754 psz_result = (*index)->FindTimecode( i_user_timecode );
3758 if ( psz_result == NULL )
3765 void demux_sys_t::PreloadFamily( const matroska_segment_c & of_segment )
3767 for (size_t i=0; i<opened_segments.size(); i++)
3769 opened_segments[i]->PreloadFamily( of_segment );
3772 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
3777 for (size_t i=0; i<families.size(); i++)
3779 for (size_t j=0; j<of_segment.families.size(); j++)
3781 if ( families[i] == of_segment.families[j] )
3789 // preload all the linked segments for all preloaded segments
3790 void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
3792 size_t i_preloaded, i, j;
3793 virtual_segment_c *p_seg;
3795 p_current_segment = VirtualFromSegments( p_segment );
3797 used_segments.push_back( p_current_segment );
3799 // create all the other virtual segments of the family
3802 for ( i=0; i< opened_segments.size(); i++ )
3804 if ( opened_segments[i]->b_preloaded && !IsUsedSegment( *opened_segments[i] ) )
3806 p_seg = VirtualFromSegments( opened_segments[i] );
3807 used_segments.push_back( p_seg );
3811 } while ( i_preloaded ); // worst case: will stop when all segments are found as family related
3813 // publish all editions of all usable segment
3814 for ( i=0; i< used_segments.size(); i++ )
3816 p_seg = used_segments[i];
3817 if ( p_seg->p_editions != NULL )
3819 std::string sz_name;
3820 input_title_t *p_title = vlc_input_title_New();
3821 p_seg->i_sys_title = i;
3823 for ( j=0; j<p_seg->p_editions->size(); j++ )
3825 if ( p_title->psz_name == NULL )
3827 sz_name = (*p_seg->p_editions)[j]->GetMainName();
3828 if ( sz_name != "" )
3829 p_title->psz_name = strdup( sz_name.c_str() );
3832 chapter_edition_c *p_edition = (*p_seg->p_editions)[j];
3834 p_edition->PublishChapters( *p_title );
3837 // create a name if there is none
3838 if ( p_title->psz_name == NULL )
3840 sz_name = N_("Segment ");
3842 sprintf( psz_str, "%d", i );
3844 p_title->psz_name = strdup( sz_name.c_str() );
3847 titles.push_back( *p_title );
3852 bool demux_sys_t::IsUsedSegment( matroska_segment_c &segment ) const
3854 for ( size_t i=0; i< used_segments.size(); i++ )
3856 if ( used_segments[i]->FindUID( segment.segment_uid ) )
3862 virtual_segment_c *demux_sys_t::VirtualFromSegments( matroska_segment_c *p_segment ) const
3864 size_t i_preloaded, i;
3866 virtual_segment_c *p_result = new virtual_segment_c( p_segment );
3868 // fill our current virtual segment with all hard linked segments
3871 for ( i=0; i< opened_segments.size(); i++ )
3873 i_preloaded += p_result->AddSegment( opened_segments[i] );
3875 } while ( i_preloaded ); // worst case: will stop when all segments are found as linked
3879 p_result->PreloadLinked( );
3881 p_result->PrepareChapters( );
3886 bool demux_sys_t::PreparePlayback( )
3888 p_current_segment->LoadCues();
3889 f_duration = p_current_segment->Duration();
3891 /* add information */
3892 p_current_segment->Segment()->InformationCreate( );
3894 p_current_segment->Segment()->Select( 0 );
3899 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
3901 EbmlBinary * p_itema = (EbmlBinary *)(&p_item_a->segment_uid);
3902 if ( *p_itema == p_item_b->prev_segment_uid )
3905 p_itema = (EbmlBinary *)(&p_item_a->next_segment_uid);
3906 if ( *p_itema == p_item_b->segment_uid )
3909 if ( *p_itema == p_item_b->prev_segment_uid )
3915 bool matroska_segment_c::Preload( )
3920 EbmlElement *el = NULL;
3922 ep->Reset( &sys.demuxer );
3924 while( ( el = ep->Get() ) != NULL )
3926 if( MKV_IS_ID( el, KaxInfo ) )
3928 ParseInfo( static_cast<KaxInfo*>( el ) );
3930 else if( MKV_IS_ID( el, KaxTracks ) )
3932 ParseTracks( static_cast<KaxTracks*>( el ) );
3934 else if( MKV_IS_ID( el, KaxSeekHead ) )
3936 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
3938 else if( MKV_IS_ID( el, KaxCues ) )
3940 msg_Dbg( &sys.demuxer, "| + Cues" );
3942 else if( MKV_IS_ID( el, KaxCluster ) )
3944 msg_Dbg( &sys.demuxer, "| + Cluster" );
3946 cluster = (KaxCluster*)el;
3948 i_start_pos = cluster->GetElementPosition();
3952 /* stop parsing the stream */
3955 else if( MKV_IS_ID( el, KaxAttachments ) )
3957 msg_Dbg( &sys.demuxer, "| + Attachments FIXME (but probably never supported)" );
3959 else if( MKV_IS_ID( el, KaxChapters ) )
3961 msg_Dbg( &sys.demuxer, "| + Chapters" );
3962 ParseChapters( static_cast<KaxChapters*>( el ) );
3964 else if( MKV_IS_ID( el, KaxTag ) )
3966 msg_Dbg( &sys.demuxer, "| + Tags FIXME TODO" );
3970 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid(*el).name() );
3979 matroska_segment_c *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
3981 for (size_t i=0; i<opened_segments.size(); i++)
3983 if ( opened_segments[i]->segment_uid == uid )
3984 return opened_segments[i];
3989 chapter_item_c *demux_sys_t::BrowseCodecPrivate( unsigned int codec_id,
3990 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3991 const void *p_cookie,
3992 size_t i_cookie_size,
3993 virtual_segment_c * &p_segment_found )
3995 chapter_item_c *p_result = NULL;
3996 for (size_t i=0; i<opened_segments.size(); i++)
3998 p_result = used_segments[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3999 if ( p_result != NULL )
4001 p_segment_found = used_segments[i];
4008 void virtual_segment_c::Sort()
4010 // keep the current segment index
4011 matroska_segment_c *p_segment = linked_segments[i_current_segment];
4013 std::sort( linked_segments.begin(), linked_segments.end(), matroska_segment_c::CompareSegmentUIDs );
4015 for ( i_current_segment=0; i_current_segment<linked_segments.size(); i_current_segment++)
4016 if ( linked_segments[i_current_segment] == p_segment )
4020 size_t virtual_segment_c::AddSegment( matroska_segment_c *p_segment )
4023 // check if it's not already in here
4024 for ( i=0; i<linked_segments.size(); i++ )
4026 if ( p_segment->segment_uid == linked_segments[i]->segment_uid )
4030 // find possible mates
4031 for ( i=0; i<linked_uids.size(); i++ )
4033 if ( p_segment->segment_uid == linked_uids[i]
4034 || p_segment->prev_segment_uid == linked_uids[i]
4035 || p_segment->next_segment_uid == linked_uids[i] )
4037 linked_segments.push_back( p_segment );
4039 AppendUID( p_segment->prev_segment_uid );
4040 AppendUID( p_segment->next_segment_uid );
4048 void virtual_segment_c::PreloadLinked( )
4050 for ( size_t i=0; i<linked_segments.size(); i++ )
4052 linked_segments[i]->Preload( );
4054 i_current_edition = linked_segments[0]->i_default_edition;
4057 mtime_t virtual_segment_c::Duration() const
4060 if ( linked_segments.size() == 0 )
4063 matroska_segment_c *p_last_segment = linked_segments[linked_segments.size()-1];
4064 // p_last_segment->ParseCluster( );
4066 i_duration = p_last_segment->i_start_time / 1000 + p_last_segment->i_duration;
4071 void virtual_segment_c::LoadCues( )
4073 for ( size_t i=0; i<linked_segments.size(); i++ )
4075 linked_segments[i]->LoadCues();
4079 void virtual_segment_c::AppendUID( const EbmlBinary & UID )
4081 if ( UID.GetBuffer() == NULL )
4084 for (size_t i=0; i<linked_uids.size(); i++)
4086 if ( UID == linked_uids[i] )
4089 linked_uids.push_back( *(KaxSegmentUID*)(&UID) );
4092 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset )
4095 int i_track_skipping;
4096 int64_t i_block_duration;
4097 int64_t i_block_ref1;
4098 int64_t i_block_ref2;
4100 int64_t i_seek_position = i_start_pos;
4101 int64_t i_seek_time = i_start_time;
4107 for( ; i_idx < i_index; i_idx++ )
4109 if( index[i_idx].i_time + i_time_offset > i_date )
4120 i_seek_position = index[i_idx].i_position;
4121 i_seek_time = index[i_idx].i_time;
4124 msg_Dbg( &sys.demuxer, "seek got "I64Fd" (%d%%)",
4125 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
4127 es.I_O().setFilePointer( i_seek_position, seek_beginning );
4130 ep = new EbmlParser( &es, segment, &sys.demuxer );
4133 sys.i_start_pts = i_date;
4135 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
4137 /* now parse until key frame */
4138 #define tk tracks[i_track]
4139 i_track_skipping = 0;
4140 for( i_track = 0; i_track < tracks.size(); i_track++ )
4142 if( tk->fmt.i_cat == VIDEO_ES )
4144 tk->b_search_keyframe = VLC_TRUE;
4147 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_date );
4151 while( i_track_skipping > 0 )
4153 if( BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
4155 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
4160 for( i_track = 0; i_track < tracks.size(); i_track++ )
4162 if( tk->i_number == block->TrackNum() )
4168 sys.i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
4170 if( i_track < tracks.size() )
4172 if( sys.i_pts >= sys.i_start_pts )
4174 BlockDecode( &sys.demuxer, block, sys.i_pts, 0 );
4175 i_track_skipping = 0;
4177 else if( tk->fmt.i_cat == VIDEO_ES )
4179 if( i_block_ref1 == -1 && tk->b_search_keyframe )
4181 tk->b_search_keyframe = VLC_FALSE;
4184 if( !tk->b_search_keyframe )
4186 BlockDecode( &sys.demuxer, block, sys.i_pts, 0 );
4196 void virtual_segment_c::Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter )
4198 demux_sys_t *p_sys = demuxer.p_sys;
4201 // find the actual time for an ordered edition
4202 if ( psz_chapter == NULL )
4204 if ( Edition() && Edition()->b_ordered )
4206 /* 1st, we need to know in which chapter we are */
4207 psz_chapter = (*p_editions)[i_current_edition]->FindTimecode( i_date );
4211 if ( psz_chapter != NULL )
4213 psz_current_chapter = psz_chapter;
4214 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
4215 demuxer.info.i_update |= INPUT_UPDATE_SEEKPOINT;
4216 demuxer.info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
4219 // find the best matching segment
4220 for ( i=0; i<linked_segments.size(); i++ )
4222 if ( i_date < linked_segments[i]->i_start_time )
4229 if ( i_current_segment != i )
4231 linked_segments[i_current_segment]->UnSelect();
4232 linked_segments[i]->Select( i_date );
4233 i_current_segment = i;
4236 linked_segments[i]->Seek( i_date, i_time_offset );
4239 void chapter_codec_cmds_c::AddCommand( const KaxChapterProcessCommand & command )
4243 uint32 codec_time = uint32(-1);
4244 for( i = 0; i < command.ListSize(); i++ )
4246 const EbmlElement *k = command[i];
4248 if( MKV_IS_ID( k, KaxChapterProcessTime ) )
4250 codec_time = uint32( *static_cast<const KaxChapterProcessTime*>( k ) );
4255 for( i = 0; i < command.ListSize(); i++ )
4257 const EbmlElement *k = command[i];
4259 if( MKV_IS_ID( k, KaxChapterProcessData ) )
4261 KaxChapterProcessData *p_data = new KaxChapterProcessData( *static_cast<const KaxChapterProcessData*>( k ) );
4262 switch ( codec_time )
4265 during_cmds.push_back( *p_data );
4268 enter_cmds.push_back( *p_data );
4271 leave_cmds.push_back( *p_data );
4280 bool chapter_item_c::Enter()
4282 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
4283 while ( index != codecs.end() )
4288 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
4289 while ( index_ != sub_chapters.end() )
4297 bool chapter_item_c::Leave()
4299 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
4300 while ( index != codecs.end() )
4305 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
4306 while ( index_ != sub_chapters.end() )
4314 bool dvd_chapter_codec_c::Enter()
4316 std::vector<KaxChapterProcessData>::iterator index = enter_cmds.begin();
4317 while ( index != enter_cmds.end() )
4319 if ( (*index).GetSize() )
4321 binary *p_data = (*index).GetBuffer();
4322 size_t i_size = *p_data++;
4323 // avoid reading too much from the buffer
4324 i_size = min( i_size, ((*index).GetSize() - 1) >> 3 );
4325 for ( ; i_size > 0; i_size--, p_data += 8 )
4327 interpretor.Interpret( p_data );
4335 bool dvd_chapter_codec_c::Leave()
4337 std::vector<KaxChapterProcessData>::iterator index = leave_cmds.begin();
4338 while ( index != leave_cmds.end() )
4340 if ( (*index).GetSize() )
4342 binary *p_data = (*index).GetBuffer();
4343 size_t i_size = *p_data++;
4344 // avoid reading too much from the buffer
4345 i_size = min( i_size, ((*index).GetSize() - 1) >> 3 );
4346 for ( ; i_size > 0; i_size--, p_data += 8 )
4348 interpretor.Interpret( p_data );
4356 bool dvd_command_interpretor_c::Interpret( const binary * p_command, size_t i_size )
4361 uint16 i_command = ( p_command[0] << 8 ) + p_command[1];
4363 switch ( i_command )
4367 uint8 i_title = p_command[5];
4368 msg_Dbg( &sys.demuxer, "DVD command: JumpTT %d", i_title );
4370 // find in the ChapProcessPrivate matching this Title level
4371 virtual_segment_c *p_segment;
4372 chapter_item_c *p_chapter;
4373 p_chapter = sys.BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title), p_segment );
4374 if ( p_chapter != NULL )
4376 // if the segment is not part of the current segment, select the new one
4377 if ( p_segment != sys.p_current_segment )
4379 sys.p_current_segment = p_segment;
4380 sys.i_current_title = p_segment->i_sys_title + 1;
4381 sys.PreparePlayback();
4386 // jump to the location in the found segment
4387 p_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
4392 case CMD_CALLSS_VTSM:
4394 msg_Dbg( &sys.demuxer, "DVD command: CallSS VTSM" );
4399 msg_Dbg( &sys.demuxer, "DVD command: unsupported %02X %02X %02X %02X %02X %02X %02X %02X"
4415 bool dvd_command_interpretor_c::MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
4417 if ( i_cookie_size != 1 || data.m_private_data.GetSize() < 4 )
4420 if ( data.m_private_data.GetBuffer()[0] != 0x28 )
4423 uint16 i_gtitle = (data.m_private_data.GetBuffer()[1] << 8 ) + data.m_private_data.GetBuffer()[2];
4424 uint8 i_title = *(uint8*)p_cookie;
4426 return (i_gtitle == i_title);