1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
29 /* config.h may include inttypes.h, so make sure we define that option
31 #define __STDC_FORMAT_MACROS 1
32 #define __STDC_CONSTANT_MACROS 1
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
44 # include <time.h> /* time() */
48 #include <vlc_codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
49 #include <vlc_iso_lang.h>
51 #include <vlc_charset.h>
52 #include <vlc_input.h>
53 #include <vlc_demux.h>
66 /* libebml and matroska */
67 #include "ebml/EbmlHead.h"
68 #include "ebml/EbmlSubHead.h"
69 #include "ebml/EbmlStream.h"
70 #include "ebml/EbmlContexts.h"
71 #include "ebml/EbmlVoid.h"
72 #include "ebml/EbmlVersion.h"
73 #include "ebml/StdIOCallback.h"
75 #include "matroska/KaxAttachments.h"
76 #include "matroska/KaxAttached.h"
77 #include "matroska/KaxBlock.h"
78 #include "matroska/KaxBlockData.h"
79 #include "matroska/KaxChapters.h"
80 #include "matroska/KaxCluster.h"
81 #include "matroska/KaxClusterData.h"
82 #include "matroska/KaxContexts.h"
83 #include "matroska/KaxCues.h"
84 #include "matroska/KaxCuesData.h"
85 #include "matroska/KaxInfo.h"
86 #include "matroska/KaxInfoData.h"
87 #include "matroska/KaxSeekHead.h"
88 #include "matroska/KaxSegment.h"
89 #include "matroska/KaxTag.h"
90 #include "matroska/KaxTags.h"
91 #include "matroska/KaxTagMulti.h"
92 #include "matroska/KaxTracks.h"
93 #include "matroska/KaxTrackAudio.h"
94 #include "matroska/KaxTrackVideo.h"
95 #include "matroska/KaxTrackEntryData.h"
96 #include "matroska/KaxContentEncoding.h"
97 #include "matroska/KaxVersion.h"
99 #include "ebml/StdIOCallback.h"
101 #if LIBMATROSKA_VERSION < 0x000706
102 START_LIBMATROSKA_NAMESPACE
103 extern const EbmlSemanticContext MATROSKA_DLL_API KaxMatroska_Context;
104 END_LIBMATROSKA_NAMESPACE
107 #include "vlc_keys.h"
110 #include "mp4/libmp4.h"
116 #define MATROSKA_COMPRESSION_NONE -1
117 #define MATROSKA_COMPRESSION_ZLIB 0
118 #define MATROSKA_COMPRESSION_BLIB 1
119 #define MATROSKA_COMPRESSION_LZOX 2
120 #define MATROSKA_COMPRESSION_HEADER 3
122 #define MKVD_TIMECODESCALE 1000000
124 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
125 #undef ATTRIBUTE_PACKED
126 #undef PRAGMA_PACK_BEGIN
127 #undef PRAGMA_PACK_END
129 #if defined(__GNUC__)
130 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
131 #define ATTRIBUTE_PACKED __attribute__ ((packed))
132 #define PRAGMA_PACK 0
136 #if !defined(ATTRIBUTE_PACKED)
137 #define ATTRIBUTE_PACKED
138 #define PRAGMA_PACK 1
145 /*************************************
146 * taken from libdvdnav / libdvdread
147 **************************************/
150 * DVD Time Information.
156 uint8_t frame_u; /* The two high bits are the frame rate. */
157 } ATTRIBUTE_PACKED dvd_time_t;
163 #ifdef WORDS_BIGENDIAN
164 unsigned char zero : 7; /* 25-31 */
165 unsigned char video_pres_mode_change : 1; /* 24 */
167 unsigned char karaoke_audio_pres_mode_change : 1; /* 23 */
168 unsigned char angle_change : 1;
169 unsigned char subpic_stream_change : 1;
170 unsigned char audio_stream_change : 1;
171 unsigned char pause_on : 1;
172 unsigned char still_off : 1;
173 unsigned char button_select_or_activate : 1;
174 unsigned char resume : 1; /* 16 */
176 unsigned char chapter_menu_call : 1; /* 15 */
177 unsigned char angle_menu_call : 1;
178 unsigned char audio_menu_call : 1;
179 unsigned char subpic_menu_call : 1;
180 unsigned char root_menu_call : 1;
181 unsigned char title_menu_call : 1;
182 unsigned char backward_scan : 1;
183 unsigned char forward_scan : 1; /* 8 */
185 unsigned char next_pg_search : 1; /* 7 */
186 unsigned char prev_or_top_pg_search : 1;
187 unsigned char time_or_chapter_search : 1;
188 unsigned char go_up : 1;
189 unsigned char stop : 1;
190 unsigned char title_play : 1;
191 unsigned char chapter_search_or_play : 1;
192 unsigned char title_or_time_play : 1; /* 0 */
194 unsigned char video_pres_mode_change : 1; /* 24 */
195 unsigned char zero : 7; /* 25-31 */
197 unsigned char resume : 1; /* 16 */
198 unsigned char button_select_or_activate : 1;
199 unsigned char still_off : 1;
200 unsigned char pause_on : 1;
201 unsigned char audio_stream_change : 1;
202 unsigned char subpic_stream_change : 1;
203 unsigned char angle_change : 1;
204 unsigned char karaoke_audio_pres_mode_change : 1; /* 23 */
206 unsigned char forward_scan : 1; /* 8 */
207 unsigned char backward_scan : 1;
208 unsigned char title_menu_call : 1;
209 unsigned char root_menu_call : 1;
210 unsigned char subpic_menu_call : 1;
211 unsigned char audio_menu_call : 1;
212 unsigned char angle_menu_call : 1;
213 unsigned char chapter_menu_call : 1; /* 15 */
215 unsigned char title_or_time_play : 1; /* 0 */
216 unsigned char chapter_search_or_play : 1;
217 unsigned char title_play : 1;
218 unsigned char stop : 1;
219 unsigned char go_up : 1;
220 unsigned char time_or_chapter_search : 1;
221 unsigned char prev_or_top_pg_search : 1;
222 unsigned char next_pg_search : 1; /* 7 */
224 } ATTRIBUTE_PACKED user_ops_t;
227 * Type to store per-command data.
231 } ATTRIBUTE_PACKED vm_cmd_t;
232 #define COMMAND_DATA_SIZE 8
235 * PCI General Information
238 uint32_t nv_pck_lbn; /**< sector address of this nav pack */
239 uint16_t vobu_cat; /**< 'category' of vobu */
240 uint16_t zero1; /**< reserved */
241 user_ops_t vobu_uop_ctl; /**< UOP of vobu */
242 uint32_t vobu_s_ptm; /**< start presentation time of vobu */
243 uint32_t vobu_e_ptm; /**< end presentation time of vobu */
244 uint32_t vobu_se_e_ptm; /**< end ptm of sequence end in vobu */
245 dvd_time_t e_eltm; /**< Cell elapsed time */
247 } ATTRIBUTE_PACKED pci_gi_t;
250 * Non Seamless Angle Information
253 uint32_t nsml_agl_dsta[9]; /**< address of destination vobu in AGL_C#n */
254 } ATTRIBUTE_PACKED nsml_agli_t;
257 * Highlight General Information
259 * For btngrX_dsp_ty the bits have the following meaning:
260 * 000b: normal 4/3 only buttons
261 * XX1b: wide (16/9) buttons
262 * X1Xb: letterbox buttons
263 * 1XXb: pan&scan buttons
266 uint16_t hli_ss; /**< status, only low 2 bits 0: no buttons, 1: different 2: equal 3: eual except for button cmds */
267 uint32_t hli_s_ptm; /**< start ptm of hli */
268 uint32_t hli_e_ptm; /**< end ptm of hli */
269 uint32_t btn_se_e_ptm; /**< end ptm of button select */
270 #ifdef WORDS_BIGENDIAN
271 unsigned char zero1 : 2; /**< reserved */
272 unsigned char btngr_ns : 2; /**< number of button groups 1, 2 or 3 with 36/18/12 buttons */
273 unsigned char zero2 : 1; /**< reserved */
274 unsigned char btngr1_dsp_ty : 3; /**< display type of subpic stream for button group 1 */
275 unsigned char zero3 : 1; /**< reserved */
276 unsigned char btngr2_dsp_ty : 3; /**< display type of subpic stream for button group 2 */
277 unsigned char zero4 : 1; /**< reserved */
278 unsigned char btngr3_dsp_ty : 3; /**< display type of subpic stream for button group 3 */
280 unsigned char btngr1_dsp_ty : 3;
281 unsigned char zero2 : 1;
282 unsigned char btngr_ns : 2;
283 unsigned char zero1 : 2;
284 unsigned char btngr3_dsp_ty : 3;
285 unsigned char zero4 : 1;
286 unsigned char btngr2_dsp_ty : 3;
287 unsigned char zero3 : 1;
289 uint8_t btn_ofn; /**< button offset number range 0-255 */
290 uint8_t btn_ns; /**< number of valid buttons <= 36/18/12 (low 6 bits) */
291 uint8_t nsl_btn_ns; /**< number of buttons selectable by U_BTNNi (low 6 bits) nsl_btn_ns <= btn_ns */
292 uint8_t zero5; /**< reserved */
293 uint8_t fosl_btnn; /**< forcedly selected button (low 6 bits) */
294 uint8_t foac_btnn; /**< forcedly activated button (low 6 bits) */
295 } ATTRIBUTE_PACKED hl_gi_t;
299 * Button Color Information Table
300 * Each entry beeing a 32bit word that contains the color indexs and alpha
301 * values to use. They are all represented by 4 bit number and stored
302 * like this [Ci3, Ci2, Ci1, Ci0, A3, A2, A1, A0]. The actual palette
303 * that the indexes reference is in the PGC.
304 * \todo split the uint32_t into a struct
307 uint32_t btn_coli[3][2]; /**< [button color number-1][select:0/action:1] */
308 } ATTRIBUTE_PACKED btn_colit_t;
313 * NOTE: I've had to change the structure from the disk layout to get
314 * the packing to work with Sun's Forte C compiler.
315 * The 4 and 7 bytes are 'rotated' was: ABC DEF GHIJ is: ABCG DEFH IJ
318 #ifdef WORDS_BIGENDIAN
319 uint32 btn_coln : 2; /**< button color number */
320 uint32 x_start : 10; /**< x start offset within the overlay */
321 uint32 zero1 : 2; /**< reserved */
322 uint32 x_end : 10; /**< x end offset within the overlay */
324 uint32 zero3 : 2; /**< reserved */
325 uint32 up : 6; /**< button index when pressing up */
327 uint32 auto_action_mode : 2; /**< 0: no, 1: activated if selected */
328 uint32 y_start : 10; /**< y start offset within the overlay */
329 uint32 zero2 : 2; /**< reserved */
330 uint32 y_end : 10; /**< y end offset within the overlay */
332 uint32 zero4 : 2; /**< reserved */
333 uint32 down : 6; /**< button index when pressing down */
334 unsigned char zero5 : 2; /**< reserved */
335 unsigned char left : 6; /**< button index when pressing left */
336 unsigned char zero6 : 2; /**< reserved */
337 unsigned char right : 6; /**< button index when pressing right */
350 uint32 auto_action_mode : 2;
354 unsigned char left : 6;
355 unsigned char zero5 : 2;
356 unsigned char right : 6;
357 unsigned char zero6 : 2;
360 } ATTRIBUTE_PACKED btni_t;
363 * Highlight Information
367 btn_colit_t btn_colit;
369 } ATTRIBUTE_PACKED hli_t;
376 nsml_agli_t nsml_agli;
379 } ATTRIBUTE_PACKED pci_t;
385 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
389 * What's between a directory and a filename?
392 #define DIRECTORY_SEPARATOR '\\'
394 #define DIRECTORY_SEPARATOR '/'
397 using namespace LIBMATROSKA_NAMESPACE;
400 /*****************************************************************************
402 *****************************************************************************/
403 static int Open ( vlc_object_t * );
404 static void Close( vlc_object_t * );
407 set_shortname( "Matroska" );
408 set_description( N_("Matroska stream demuxer" ) );
409 set_capability( "demux", 50 );
410 set_callbacks( Open, Close );
411 set_category( CAT_INPUT );
412 set_subcategory( SUBCAT_INPUT_DEMUX );
414 add_bool( "mkv-use-ordered-chapters", 1, NULL,
415 N_("Ordered chapters"),
416 N_("Play ordered chapters as specified in the segment."), true );
418 add_bool( "mkv-use-chapter-codec", 1, NULL,
419 N_("Chapter codecs"),
420 N_("Use chapter codecs found in the segment."), true );
422 add_bool( "mkv-preload-local-dir", 1, NULL,
423 N_("Preload Directory"),
424 N_("Preload matroska files from the same family in the same directory (not good for broken files)."), true );
426 add_bool( "mkv-seek-percent", 0, NULL,
427 N_("Seek based on percent not time"),
428 N_("Seek based on percent not time."), true );
430 add_bool( "mkv-use-dummy", 0, NULL,
431 N_("Dummy Elements"),
432 N_("Read and discard unknown EBML elements (not good for broken files)."), true );
434 add_shortcut( "mka" );
435 add_shortcut( "mkv" );
438 /*****************************************************************************
440 *****************************************************************************/
442 block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) {
443 int result, dstsize, n;
448 d_stream.zalloc = (alloc_func)0;
449 d_stream.zfree = (free_func)0;
450 d_stream.opaque = (voidpf)0;
451 result = inflateInit(&d_stream);
454 msg_Dbg( p_this, "inflateInit() failed. Result: %d", result );
458 d_stream.next_in = (Bytef *)p_in_block->p_buffer;
459 d_stream.avail_in = p_in_block->i_buffer;
461 p_block = block_New( p_this, 0 );
466 p_block = block_Realloc( p_block, 0, n * 1000 );
467 dst = (unsigned char *)p_block->p_buffer;
468 d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
469 d_stream.avail_out = 1000;
470 result = inflate(&d_stream, Z_NO_FLUSH);
471 if( ( result != Z_OK ) && ( result != Z_STREAM_END ) )
473 msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result );
477 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
478 ( result != Z_STREAM_END ) );
480 dstsize = d_stream.total_out;
481 inflateEnd( &d_stream );
483 p_block = block_Realloc( p_block, 0, dstsize );
484 p_block->i_buffer = dstsize;
485 block_Release( p_in_block );
492 * Helper function to print the mkv parse tree
494 static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
499 msg_Err( &demuxer, "too deep tree" );
502 va_start( args, psz_format );
503 static const char psz_foo[] = "| | | | | | | | | |";
504 char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) );
505 strncpy( psz_foo2, psz_foo, 4 * i_level );
506 psz_foo2[ 4 * i_level ] = '+';
507 psz_foo2[ 4 * i_level + 1 ] = ' ';
508 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
509 __msg_GenericVa( VLC_OBJECT(&demuxer),VLC_MSG_DBG, "mkv", psz_foo2, args );
514 /*****************************************************************************
516 *****************************************************************************/
517 class vlc_stream_io_callback: public IOCallback
525 vlc_stream_io_callback( stream_t *, bool );
527 virtual ~vlc_stream_io_callback()
533 virtual uint32 read ( void *p_buffer, size_t i_size);
534 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
535 virtual size_t write ( const void *p_buffer, size_t i_size);
536 virtual uint64 getFilePointer ( void );
537 virtual void close ( void );
540 /*****************************************************************************
542 *****************************************************************************/
546 EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux );
547 virtual ~EbmlParser( void );
551 void Reset( demux_t *p_demux );
552 EbmlElement *Get( void );
554 EbmlElement *UnGet( uint64 i_block_pos, uint64 i_cluster_pos );
556 int GetLevel( void );
561 EbmlElement *m_el[10];
562 int64_t mi_remain_size[10];
572 /*****************************************************************************
573 * Some functions to manipulate memory
574 *****************************************************************************/
575 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
576 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
578 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
581 /*****************************************************************************
582 * definitions of structures and functions used by this plugins
583 *****************************************************************************/
590 unsigned int i_number;
593 uint8_t *p_extra_data;
597 uint64_t i_default_duration;
598 float f_timecodescale;
607 unsigned int i_original_rate;
610 /* data to be send first */
612 uint8_t *p_data_init;
614 /* hack : it's for seek */
615 bool b_search_keyframe;
619 const char *psz_codec_name;
620 const char *psz_codec_settings;
621 const char *psz_codec_info_url;
622 const char *psz_codec_download_url;
624 /* encryption/compression */
625 int i_compression_type;
626 KaxContentCompSettings *p_compression_data;
643 const binary MATROSKA_DVD_LEVEL_SS = 0x30;
644 const binary MATROSKA_DVD_LEVEL_LU = 0x2A;
645 const binary MATROSKA_DVD_LEVEL_TT = 0x28;
646 const binary MATROSKA_DVD_LEVEL_PGC = 0x20;
647 const binary MATROSKA_DVD_LEVEL_PG = 0x18;
648 const binary MATROSKA_DVD_LEVEL_PTT = 0x10;
649 const binary MATROSKA_DVD_LEVEL_CN = 0x08;
651 class chapter_codec_cmds_c
654 chapter_codec_cmds_c( demux_sys_t & demuxer, int codec_id = -1)
655 :p_private_data(NULL)
656 ,i_codec_id( codec_id )
660 virtual ~chapter_codec_cmds_c()
662 delete p_private_data;
663 std::vector<KaxChapterProcessData*>::iterator indexe = enter_cmds.begin();
664 while ( indexe != enter_cmds.end() )
669 std::vector<KaxChapterProcessData*>::iterator indexl = leave_cmds.begin();
670 while ( indexl != leave_cmds.end() )
675 std::vector<KaxChapterProcessData*>::iterator indexd = during_cmds.begin();
676 while ( indexd != during_cmds.end() )
683 void SetPrivate( const KaxChapterProcessPrivate & private_data )
685 p_private_data = new KaxChapterProcessPrivate( private_data );
688 void AddCommand( const KaxChapterProcessCommand & command );
690 /// \return wether the codec has seeked in the files or not
691 virtual bool Enter() { return false; }
692 virtual bool Leave() { return false; }
693 virtual std::string GetCodecName( bool f_for_title = false ) const { return ""; }
694 virtual int16 GetTitleNumber() { return -1; }
696 KaxChapterProcessPrivate *p_private_data;
699 std::vector<KaxChapterProcessData*> enter_cmds;
700 std::vector<KaxChapterProcessData*> during_cmds;
701 std::vector<KaxChapterProcessData*> leave_cmds;
707 class dvd_command_interpretor_c
710 dvd_command_interpretor_c( demux_sys_t & demuxer )
713 memset( p_PRMs, 0, sizeof(p_PRMs) );
714 p_PRMs[ 0x80 + 1 ] = 15;
715 p_PRMs[ 0x80 + 2 ] = 62;
716 p_PRMs[ 0x80 + 3 ] = 1;
717 p_PRMs[ 0x80 + 4 ] = 1;
718 p_PRMs[ 0x80 + 7 ] = 1;
719 p_PRMs[ 0x80 + 8 ] = 1;
720 p_PRMs[ 0x80 + 16 ] = 0xFFFFu;
721 p_PRMs[ 0x80 + 18 ] = 0xFFFFu;
724 bool Interpret( const binary * p_command, size_t i_size = 8 );
726 uint16 GetPRM( size_t index ) const
729 return p_PRMs[ index ];
733 uint16 GetGPRM( size_t index ) const
735 if ( index >= 0 && index < 16 )
736 return p_PRMs[ index ];
740 uint16 GetSPRM( size_t index ) const
742 // 21,22,23 reserved for future use
743 if ( index >= 0x80 && index < 0x95 )
744 return p_PRMs[ index ];
748 bool SetPRM( size_t index, uint16 value )
750 if ( index >= 0 && index < 16 )
752 p_PRMs[ index ] = value;
758 bool SetGPRM( size_t index, uint16 value )
760 if ( index >= 0 && index < 16 )
762 p_PRMs[ index ] = value;
768 bool SetSPRM( size_t index, uint16 value )
770 if ( index > 0x80 && index <= 0x8D && index != 0x8C )
772 p_PRMs[ index ] = value;
779 std::string GetRegTypeName( bool b_value, uint16 value ) const
782 char s_value[6], s_reg_value[6];
783 sprintf( s_value, "%.5d", value );
791 else if ( value < 0x80 )
793 sprintf( s_reg_value, "%.5d", GetPRM( value ) );
797 result += s_reg_value;
802 sprintf( s_reg_value, "%.5d", GetPRM( value ) );
806 result += s_reg_value;
818 // wether it's a comparison on the value or register
819 static const uint16 CMD_DVD_TEST_VALUE = 0x80;
820 static const uint16 CMD_DVD_IF_GPREG_AND = (1 << 4);
821 static const uint16 CMD_DVD_IF_GPREG_EQUAL = (2 << 4);
822 static const uint16 CMD_DVD_IF_GPREG_NOT_EQUAL = (3 << 4);
823 static const uint16 CMD_DVD_IF_GPREG_SUP_EQUAL = (4 << 4);
824 static const uint16 CMD_DVD_IF_GPREG_SUP = (5 << 4);
825 static const uint16 CMD_DVD_IF_GPREG_INF_EQUAL = (6 << 4);
826 static const uint16 CMD_DVD_IF_GPREG_INF = (7 << 4);
828 static const uint16 CMD_DVD_NOP = 0x0000;
829 static const uint16 CMD_DVD_GOTO_LINE = 0x0001;
830 static const uint16 CMD_DVD_BREAK = 0x0002;
832 static const uint16 CMD_DVD_NOP2 = 0x2001;
833 static const uint16 CMD_DVD_LINKPGCN = 0x2004;
834 static const uint16 CMD_DVD_LINKPGN = 0x2006;
835 static const uint16 CMD_DVD_LINKCN = 0x2007;
836 static const uint16 CMD_DVD_JUMP_TT = 0x3002;
837 static const uint16 CMD_DVD_JUMPVTS_TT = 0x3003;
838 static const uint16 CMD_DVD_JUMPVTS_PTT = 0x3005;
839 static const uint16 CMD_DVD_JUMP_SS = 0x3006;
840 static const uint16 CMD_DVD_CALLSS_VTSM1 = 0x3008;
842 static const uint16 CMD_DVD_SET_HL_BTNN2 = 0x4600;
843 static const uint16 CMD_DVD_SET_HL_BTNN_LINKPGCN1 = 0x4604;
844 static const uint16 CMD_DVD_SET_STREAM = 0x5100;
845 static const uint16 CMD_DVD_SET_GPRMMD = 0x5300;
846 static const uint16 CMD_DVD_SET_HL_BTNN1 = 0x5600;
847 static const uint16 CMD_DVD_SET_HL_BTNN_LINKPGCN2 = 0x5604;
848 static const uint16 CMD_DVD_SET_HL_BTNN_LINKCN = 0x5607;
850 static const uint16 CMD_DVD_MOV_SPREG_PREG = 0x6100;
851 static const uint16 CMD_DVD_GPREG_MOV_VALUE = 0x7100;
852 static const uint16 CMD_DVD_SUB_GPREG = 0x7400;
853 static const uint16 CMD_DVD_MULT_GPREG = 0x7500;
854 static const uint16 CMD_DVD_GPREG_DIV_VALUE = 0x7600;
855 static const uint16 CMD_DVD_GPREG_AND_VALUE = 0x7900;
857 // callbacks when browsing inside CodecPrivate
858 static bool MatchIsDomain ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
859 static bool MatchIsVMG ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
860 static bool MatchVTSNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
861 static bool MatchVTSMNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
862 static bool MatchTitleNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
863 static bool MatchPgcType ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
864 static bool MatchPgcNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
865 static bool MatchChapterNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
866 static bool MatchCellNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
869 class dvd_chapter_codec_c : public chapter_codec_cmds_c
872 dvd_chapter_codec_c( demux_sys_t & sys )
873 :chapter_codec_cmds_c( sys, 1 )
878 std::string GetCodecName( bool f_for_title = false ) const;
879 int16 GetTitleNumber();
882 class matroska_script_interpretor_c
885 matroska_script_interpretor_c( demux_sys_t & demuxer )
889 bool Interpret( const binary * p_command, size_t i_size );
892 static const std::string CMD_MS_GOTO_AND_PLAY;
898 const std::string matroska_script_interpretor_c::CMD_MS_GOTO_AND_PLAY = "GotoAndPlay";
901 class matroska_script_codec_c : public chapter_codec_cmds_c
904 matroska_script_codec_c( demux_sys_t & sys )
905 :chapter_codec_cmds_c( sys, 0 )
913 matroska_script_interpretor_c interpretor;
916 class chapter_translation_c
919 chapter_translation_c()
923 ~chapter_translation_c()
928 KaxChapterTranslateID *p_translated;
929 unsigned int codec_id;
930 std::vector<uint64_t> editions;
939 ,i_user_start_time(-1)
942 ,b_display_seekpoint(true)
943 ,b_user_display(false)
948 virtual ~chapter_item_c()
950 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
951 while ( index != codecs.end() )
956 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
957 while ( index_ != sub_chapters.end() )
964 int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time );
965 int PublishChapters( input_title_t & title, int & i_user_chapters, int i_level = 0 );
966 virtual chapter_item_c * FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current, bool & b_found );
967 void Append( const chapter_item_c & edition );
968 chapter_item_c * FindChapter( int64_t i_find_uid );
969 virtual chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
970 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
971 const void *p_cookie,
972 size_t i_cookie_size );
973 std::string GetCodecName( bool f_for_title = false ) const;
974 bool ParentOf( const chapter_item_c & item ) const;
975 int16 GetTitleNumber( ) const;
977 int64_t i_start_time, i_end_time;
978 int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */
979 std::vector<chapter_item_c*> sub_chapters;
982 bool b_display_seekpoint;
984 std::string psz_name;
985 chapter_item_c *psz_parent;
988 std::vector<chapter_codec_cmds_c*> codecs;
990 static bool CompareTimecode( const chapter_item_c * itemA, const chapter_item_c * itemB )
992 return ( itemA->i_user_start_time < itemB->i_user_start_time || (itemA->i_user_start_time == itemB->i_user_start_time && itemA->i_user_end_time < itemB->i_user_end_time) );
995 bool Enter( bool b_do_subchapters );
996 bool Leave( bool b_do_subchapters );
997 bool EnterAndLeave( chapter_item_c *p_item, bool b_enter = true );
1000 class chapter_edition_c : public chapter_item_c
1007 void RefreshChapters( );
1008 mtime_t Duration() const;
1009 std::string GetMainName() const;
1010 chapter_item_c * FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current );
1015 class matroska_segment_c
1018 matroska_segment_c( demux_sys_t & demuxer, EbmlStream & estream )
1021 ,i_timescale(MKVD_TIMECODESCALE)
1024 ,i_cues_position(-1)
1025 ,i_chapters_position(-1)
1026 ,i_tags_position(-1)
1031 ,p_segment_uid(NULL)
1032 ,p_prev_segment_uid(NULL)
1033 ,p_next_segment_uid(NULL)
1037 ,psz_muxing_application(NULL)
1038 ,psz_writing_application(NULL)
1039 ,psz_segment_filename(NULL)
1042 ,i_default_edition(0)
1047 p_indexes = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
1050 virtual ~matroska_segment_c()
1052 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1054 delete tracks[i_track]->p_compression_data;
1055 es_format_Clean( &tracks[i_track]->fmt );
1056 free( tracks[i_track]->p_extra_data );
1057 free( tracks[i_track]->psz_codec );
1058 delete tracks[i_track];
1061 free( psz_writing_application );
1062 free( psz_muxing_application );
1063 free( psz_segment_filename );
1065 free( psz_date_utc );
1070 delete p_segment_uid;
1071 delete p_prev_segment_uid;
1072 delete p_next_segment_uid;
1074 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
1075 while ( index != stored_editions.end() )
1080 std::vector<chapter_translation_c*>::iterator indext = translations.begin();
1081 while ( indext != translations.end() )
1086 std::vector<KaxSegmentFamily*>::iterator indexf = families.begin();
1087 while ( indexf != families.end() )
1094 KaxSegment *segment;
1098 uint64_t i_timescale;
1100 /* duration of the segment */
1102 mtime_t i_start_time;
1105 std::vector<mkv_track_t*> tracks;
1108 int64_t i_cues_position;
1109 int64_t i_chapters_position;
1110 int64_t i_tags_position;
1112 KaxCluster *cluster;
1114 uint64 i_cluster_pos;
1115 int64_t i_start_pos;
1116 KaxSegmentUID *p_segment_uid;
1117 KaxPrevUID *p_prev_segment_uid;
1118 KaxNextUID *p_next_segment_uid;
1123 mkv_index_t *p_indexes;
1126 char *psz_muxing_application;
1127 char *psz_writing_application;
1128 char *psz_segment_filename;
1132 /* !!!!! GCC 3.3 bug on Darwin !!!!! */
1133 /* when you remove this variable the compiler issues an atomicity error */
1134 /* this variable only works when using std::vector<chapter_edition_c> */
1135 std::vector<chapter_edition_c*> stored_editions;
1136 int i_default_edition;
1138 std::vector<chapter_translation_c*> translations;
1139 std::vector<KaxSegmentFamily*> families;
1146 bool PreloadFamily( const matroska_segment_c & segment );
1147 void ParseInfo( KaxInfo *info );
1148 void ParseAttachments( KaxAttachments *attachments );
1149 void ParseChapters( KaxChapters *chapters );
1150 void ParseSeekHead( KaxSeekHead *seekhead );
1151 void ParseTracks( KaxTracks *tracks );
1152 void ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters );
1153 void ParseTrackEntry( KaxTrackEntry *m );
1154 void ParseCluster( );
1155 void IndexAppendCluster( KaxCluster *cluster );
1158 void InformationCreate( );
1159 void Seek( mtime_t i_date, mtime_t i_time_offset );
1160 #if LIBMATROSKA_VERSION >= 0x000800
1161 int BlockGet( KaxBlock * &, KaxSimpleBlock * &, int64_t *, int64_t *, int64_t *);
1163 int BlockGet( KaxBlock * &, int64_t *, int64_t *, int64_t *);
1165 bool Select( mtime_t i_start_time );
1168 static bool CompareSegmentUIDs( const matroska_segment_c * item_a, const matroska_segment_c * item_b );
1171 // class holding hard-linked segment together in the playback order
1172 class virtual_segment_c
1175 virtual_segment_c( matroska_segment_c *p_segment )
1178 ,i_current_segment(0)
1179 ,i_current_edition(-1)
1180 ,psz_current_chapter(NULL)
1182 linked_segments.push_back( p_segment );
1184 AppendUID( p_segment->p_segment_uid );
1185 AppendUID( p_segment->p_prev_segment_uid );
1186 AppendUID( p_segment->p_next_segment_uid );
1190 size_t AddSegment( matroska_segment_c *p_segment );
1191 void PreloadLinked( );
1192 mtime_t Duration( ) const;
1194 void Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter );
1196 inline chapter_edition_c *Edition()
1198 if ( i_current_edition >= 0 && size_t(i_current_edition) < p_editions->size() )
1199 return (*p_editions)[i_current_edition];
1203 matroska_segment_c * Segment() const
1205 if ( linked_segments.size() == 0 || i_current_segment >= linked_segments.size() )
1207 return linked_segments[i_current_segment];
1210 inline chapter_item_c *CurrentChapter() {
1211 return psz_current_chapter;
1216 if ( i_current_segment < linked_segments.size()-1 )
1218 i_current_segment++;
1224 bool FindUID( KaxSegmentUID & uid ) const
1226 for ( size_t i=0; i<linked_uids.size(); i++ )
1228 if ( linked_uids[i] == uid )
1234 bool UpdateCurrentToChapter( demux_t & demux );
1235 void PrepareChapters( );
1237 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
1238 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1239 const void *p_cookie,
1240 size_t i_cookie_size );
1241 chapter_item_c *FindChapter( int64_t i_find_uid );
1243 std::vector<chapter_edition_c*> *p_editions;
1247 std::vector<matroska_segment_c*> linked_segments;
1248 std::vector<KaxSegmentUID> linked_uids;
1249 size_t i_current_segment;
1251 int i_current_edition;
1252 chapter_item_c *psz_current_chapter;
1254 void AppendUID( const EbmlBinary * UID );
1257 class matroska_stream_c
1260 matroska_stream_c( demux_sys_t & demuxer )
1266 virtual ~matroska_stream_c()
1275 std::vector<matroska_segment_c*> segments;
1301 virtual ~attachment_c()
1306 std::string psz_file_name;
1307 std::string psz_mime_type;
1315 demux_sys_t( demux_t & demux )
1322 ,p_current_segment(NULL)
1323 ,dvd_interpretor( *this )
1327 ,b_pci_packet_set(false)
1330 vlc_mutex_init( &lock_demuxer );
1333 virtual ~demux_sys_t()
1337 for ( i=0; i<streams.size(); i++ )
1339 for ( i=0; i<opened_segments.size(); i++ )
1340 delete opened_segments[i];
1341 for ( i=0; i<used_segments.size(); i++ )
1342 delete used_segments[i];
1343 for ( i=0; i<stored_attachments.size(); i++ )
1344 delete stored_attachments[i];
1345 if( meta ) vlc_meta_Delete( meta );
1347 while( titles.size() )
1348 { vlc_input_title_Delete( titles.back() ); titles.pop_back();}
1350 vlc_mutex_destroy( &lock_demuxer );
1357 mtime_t i_start_pts;
1358 mtime_t i_chapter_time;
1362 std::vector<input_title_t*> titles; // matroska editions
1363 size_t i_current_title;
1365 std::vector<matroska_stream_c*> streams;
1366 std::vector<attachment_c*> stored_attachments;
1367 std::vector<matroska_segment_c*> opened_segments;
1368 std::vector<virtual_segment_c*> used_segments;
1369 virtual_segment_c *p_current_segment;
1371 dvd_command_interpretor_c dvd_interpretor;
1373 /* duration of the stream */
1376 matroska_segment_c *FindSegment( const EbmlBinary & uid ) const;
1377 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
1378 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1379 const void *p_cookie,
1380 size_t i_cookie_size,
1381 virtual_segment_c * & p_segment_found );
1382 chapter_item_c *FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found );
1384 void PreloadFamily( const matroska_segment_c & of_segment );
1385 void PreloadLinked( matroska_segment_c *p_segment );
1386 bool PreparePlayback( virtual_segment_c *p_new_segment );
1387 matroska_stream_c *AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial = false );
1388 void JumpTo( virtual_segment_c & p_segment, chapter_item_c * p_chapter );
1390 void StartUiThread();
1391 void StopUiThread();
1393 inline void SwapButtons();
1395 /* for spu variables */
1396 input_thread_t *p_input;
1398 bool b_pci_packet_set;
1399 uint8_t palette[4][4];
1400 vlc_mutex_t lock_demuxer;
1403 event_thread_t *p_ev;
1404 static int EventThread( vlc_object_t *p_this );
1405 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1406 vlc_value_t oldval, vlc_value_t newval, void *p_data );
1407 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1408 vlc_value_t oldval, vlc_value_t newval, void *p_data );
1413 virtual_segment_c *VirtualFromSegments( matroska_segment_c *p_segment ) const;
1414 bool IsUsedSegment( matroska_segment_c &p_segment ) const;
1417 static int Demux ( demux_t * );
1418 static int Control( demux_t *, int, va_list );
1419 static void Seek ( demux_t *, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter );
1421 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
1423 static inline char * ToUTF8( const UTFstring &u )
1425 return strdup( u.GetUTF8().c_str() );
1428 /*****************************************************************************
1429 * Open: initializes matroska demux structures
1430 *****************************************************************************/
1431 static int Open( vlc_object_t * p_this )
1433 demux_t *p_demux = (demux_t*)p_this;
1435 matroska_stream_c *p_stream;
1436 matroska_segment_c *p_segment;
1437 const uint8_t *p_peek;
1438 std::string s_path, s_filename;
1439 vlc_stream_io_callback *p_io_callback;
1440 EbmlStream *p_io_stream;
1442 /* peek the begining */
1443 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
1445 /* is a valid file */
1446 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
1447 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
1449 /* Set the demux function */
1450 p_demux->pf_demux = Demux;
1451 p_demux->pf_control = Control;
1452 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
1454 p_io_callback = new vlc_stream_io_callback( p_demux->s, false );
1455 p_io_stream = new EbmlStream( *p_io_callback );
1457 if( p_io_stream == NULL )
1459 msg_Err( p_demux, "failed to create EbmlStream" );
1460 delete p_io_callback;
1462 return VLC_EGENERIC;
1465 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_io_stream, true );
1466 if( p_stream == NULL )
1468 msg_Err( p_demux, "cannot find KaxSegment" );
1471 p_sys->streams.push_back( p_stream );
1473 p_stream->p_in = p_io_callback;
1474 p_stream->p_es = p_io_stream;
1476 for (size_t i=0; i<p_stream->segments.size(); i++)
1478 p_stream->segments[i]->Preload();
1481 p_segment = p_stream->segments[0];
1482 if( p_segment->cluster != NULL )
1484 msg_Warn( p_demux, "cannot find any cluster, damaged file ?" );
1486 // reset the stream reading to the first cluster of the segment used
1487 p_stream->p_in->setFilePointer( p_segment->cluster->GetElementPosition() );
1490 if (config_GetInt( p_demux, "mkv-preload-local-dir" ))
1492 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
1493 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
1495 // assume it's a regular file
1496 // get the directory path
1497 s_path = p_demux->psz_path;
1498 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
1500 s_path = s_path.substr(0,s_path.length()-1);
1504 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
1506 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
1510 DIR *p_src_dir = utf8_opendir(s_path.c_str());
1512 if (p_src_dir != NULL)
1515 while ((psz_file = utf8_readdir(p_src_dir)) != NULL)
1517 if (strlen(psz_file) > 4)
1519 s_filename = s_path + DIRECTORY_SEPARATOR + psz_file;
1522 if (!strcasecmp(s_filename.c_str(), p_demux->psz_path))
1524 if (!s_filename.compare(p_demux->psz_path))
1528 continue; // don't reuse the original opened file
1531 #if defined(__GNUC__) && (__GNUC__ < 3)
1532 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
1533 !s_filename.compare("mka", s_filename.length() - 3, 3))
1535 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
1536 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
1539 // test wether this file belongs to our family
1540 const uint8_t *p_peek;
1541 bool file_ok = false;
1542 stream_t *p_file_stream = stream_UrlNew(
1544 s_filename.c_str());
1545 /* peek the begining */
1546 if( p_file_stream &&
1547 stream_Peek( p_file_stream, &p_peek, 4 ) >= 4
1548 && p_peek[0] == 0x1a && p_peek[1] == 0x45 &&
1549 p_peek[2] == 0xdf && p_peek[3] == 0xa3 ) file_ok = true;
1553 vlc_stream_io_callback *p_file_io = new vlc_stream_io_callback( p_file_stream, true );
1554 EbmlStream *p_estream = new EbmlStream(*p_file_io);
1556 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_estream );
1558 if ( p_stream == NULL )
1560 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
1566 p_stream->p_in = p_file_io;
1567 p_stream->p_es = p_estream;
1568 p_sys->streams.push_back( p_stream );
1573 if( p_file_stream ) {
1574 stream_Delete( p_file_stream );
1576 msg_Dbg( p_demux, "the file '%s' cannot be opened", s_filename.c_str() );
1582 closedir( p_src_dir );
1586 p_sys->PreloadFamily( *p_segment );
1589 p_sys->PreloadLinked( p_segment );
1591 if ( !p_sys->PreparePlayback( NULL ) )
1593 msg_Err( p_demux, "cannot use the segment" );
1597 p_sys->StartUiThread();
1603 return VLC_EGENERIC;
1606 /*****************************************************************************
1607 * Close: frees unused data
1608 *****************************************************************************/
1609 static void Close( vlc_object_t *p_this )
1611 demux_t *p_demux = (demux_t*)p_this;
1612 demux_sys_t *p_sys = p_demux->p_sys;
1617 /*****************************************************************************
1619 *****************************************************************************/
1620 static int Control( demux_t *p_demux, int i_query, va_list args )
1622 demux_sys_t *p_sys = p_demux->p_sys;
1629 input_attachment_t ***ppp_attach;
1635 case DEMUX_GET_ATTACHMENTS:
1636 ppp_attach = (input_attachment_t***)va_arg( args, input_attachment_t*** );
1637 pi_int = (int*)va_arg( args, int * );
1639 if( p_sys->stored_attachments.size() <= 0 )
1640 return VLC_EGENERIC;
1642 *pi_int = p_sys->stored_attachments.size();
1643 *ppp_attach = (input_attachment_t**)malloc( sizeof(input_attachment_t**) *
1644 p_sys->stored_attachments.size() );
1645 if( !(*ppp_attach) )
1647 for( i = 0; i < p_sys->stored_attachments.size(); i++ )
1649 attachment_c *a = p_sys->stored_attachments[i];
1650 (*ppp_attach)[i] = vlc_input_attachment_New( a->psz_file_name.c_str(), a->psz_mime_type.c_str(), NULL,
1651 a->p_data, a->i_size );
1655 case DEMUX_GET_META:
1656 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1657 vlc_meta_Merge( p_meta, p_sys->meta );
1660 case DEMUX_GET_LENGTH:
1661 pi64 = (int64_t*)va_arg( args, int64_t * );
1662 if( p_sys->f_duration > 0.0 )
1664 *pi64 = (int64_t)(p_sys->f_duration * 1000);
1667 return VLC_EGENERIC;
1669 case DEMUX_GET_POSITION:
1670 pf = (double*)va_arg( args, double * );
1671 if ( p_sys->f_duration > 0.0 )
1672 *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);
1675 case DEMUX_SET_POSITION:
1676 f = (double)va_arg( args, double );
1677 Seek( p_demux, -1, f, NULL );
1680 case DEMUX_GET_TIME:
1681 pi64 = (int64_t*)va_arg( args, int64_t * );
1682 *pi64 = p_sys->i_pts;
1685 case DEMUX_GET_TITLE_INFO:
1686 if( p_sys->titles.size() > 1 || ( p_sys->titles.size() == 1 && p_sys->titles[0]->i_seekpoint > 0 ) )
1688 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1689 int *pi_int = (int*)va_arg( args, int* );
1691 *pi_int = p_sys->titles.size();
1692 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->titles.size() );
1694 for( size_t i = 0; i < p_sys->titles.size(); i++ )
1696 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->titles[i] );
1700 return VLC_EGENERIC;
1702 case DEMUX_SET_TITLE:
1703 /* TODO handle editions as titles */
1704 i_idx = (int)va_arg( args, int );
1705 if( i_idx < p_sys->used_segments.size() )
1707 p_sys->JumpTo( *p_sys->used_segments[i_idx], NULL );
1710 return VLC_EGENERIC;
1712 case DEMUX_SET_SEEKPOINT:
1713 i_skp = (int)va_arg( args, int );
1715 // TODO change the way it works with the << & >> buttons on the UI (+1/-1 instead of a number)
1716 if( p_sys->titles.size() && i_skp < p_sys->titles[p_sys->i_current_title]->i_seekpoint)
1718 Seek( p_demux, (int64_t)p_sys->titles[p_sys->i_current_title]->seekpoint[i_skp]->i_time_offset, -1, NULL);
1719 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
1720 p_demux->info.i_seekpoint = i_skp;
1723 return VLC_EGENERIC;
1725 case DEMUX_SET_TIME:
1728 return VLC_EGENERIC;
1732 #if LIBMATROSKA_VERSION >= 0x000800
1733 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1735 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1738 #if LIBMATROSKA_VERSION >= 0x000800
1739 pp_simpleblock = NULL;
1747 EbmlElement *el = NULL;
1751 return VLC_EGENERIC;
1753 #if LIBMATROSKA_VERSION >= 0x000800
1754 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1756 if( (el = ep->Get()) == NULL && pp_block != NULL )
1759 /* update the index */
1760 #define idx p_indexes[i_index - 1]
1761 if( i_index > 0 && idx.i_time == -1 )
1763 #if LIBMATROSKA_VERSION >= 0x000800
1764 if ( pp_simpleblock != NULL )
1765 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1768 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1769 idx.b_key = *pi_ref1 == 0 ? true : false;
1775 i_level = ep->GetLevel();
1784 msg_Warn( &sys.demuxer, "EOF" );
1785 return VLC_EGENERIC;
1792 if( MKV_IS_ID( el, KaxCluster ) )
1794 cluster = (KaxCluster*)el;
1795 i_cluster_pos = cluster->GetElementPosition();
1797 /* add it to the index */
1799 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1801 IndexAppendCluster( cluster );
1804 // reset silent tracks
1805 for (size_t i=0; i<tracks.size(); i++)
1807 tracks[i]->b_silent = false;
1812 else if( MKV_IS_ID( el, KaxCues ) )
1814 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1815 return VLC_EGENERIC;
1819 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1823 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1825 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1827 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1828 cluster->InitTimecode( uint64( ctc ), i_timescale );
1830 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1834 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1836 i_block_pos = el->GetElementPosition();
1839 #if LIBMATROSKA_VERSION >= 0x000800
1840 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1842 pp_simpleblock = (KaxSimpleBlock*)el;
1844 pp_simpleblock->ReadData( es.I_O() );
1845 pp_simpleblock->SetParent( *cluster );
1850 if( MKV_IS_ID( el, KaxBlock ) )
1852 pp_block = (KaxBlock*)el;
1854 pp_block->ReadData( es.I_O() );
1855 pp_block->SetParent( *cluster );
1859 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1861 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1863 dur.ReadData( es.I_O() );
1864 *pi_duration = uint64( dur );
1866 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1868 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1870 ref.ReadData( es.I_O() );
1873 *pi_ref1 = int64( ref ) * cluster->GlobalTimecodeScale();
1875 else if( *pi_ref2 == 0 )
1877 *pi_ref2 = int64( ref ) * cluster->GlobalTimecodeScale();
1880 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1882 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1883 track_num.ReadData( es.I_O() );
1885 for (size_t i=0; i<tracks.size(); i++)
1887 if ( tracks[i]->i_number == uint32(track_num))
1889 tracks[i]->b_silent = true;
1896 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1897 return VLC_EGENERIC;
1902 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem, size_t offset)
1905 if( !(p_block = block_New( p_demux, i_mem + offset ) ) ) return NULL;
1906 memcpy( p_block->p_buffer + offset, p_mem, i_mem );
1907 //p_block->i_rate = p_input->stream.control.i_rate;
1911 #if LIBMATROSKA_VERSION >= 0x000800
1912 static void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
1913 mtime_t i_pts, mtime_t i_duration, bool f_mandatory )
1915 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1916 mtime_t i_duration, bool f_mandatory )
1919 demux_sys_t *p_sys = p_demux->p_sys;
1920 matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
1926 #define tk p_segment->tracks[i_track]
1927 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1929 #if LIBMATROSKA_VERSION >= 0x000800
1930 if( (block != NULL && tk->i_number == block->TrackNum()) ||
1931 (simpleblock != NULL && tk->i_number == simpleblock->TrackNum()))
1933 if( tk->i_number == block->TrackNum() )
1940 if( i_track >= p_segment->tracks.size() )
1942 msg_Err( p_demux, "invalid track number" );
1945 if( tk->fmt.i_cat != NAV_ES && tk->p_es == NULL )
1947 msg_Err( p_demux, "unknown track number" );
1950 if( i_pts + i_duration < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1952 return; /* discard audio packets that shouldn't be rendered */
1955 if ( tk->fmt.i_cat != NAV_ES )
1957 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1961 tk->b_inited = false;
1967 /* First send init data */
1968 if( !tk->b_inited && tk->i_data_init > 0 )
1972 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
1973 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init, 0 );
1974 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
1976 tk->b_inited = true;
1979 #if LIBMATROSKA_VERSION >= 0x000800
1981 (block != NULL && i < block->NumberFrames()) || (simpleblock != NULL && i < simpleblock->NumberFrames());
1984 for( i = 0; i < block->NumberFrames(); i++ )
1989 #if LIBMATROSKA_VERSION >= 0x000800
1990 if ( simpleblock != NULL )
1992 data = &simpleblock->GetBuffer(i);
1993 // condition when the DTS is correct (keyframe or B frame == NOT P frame)
1994 f_mandatory = simpleblock->IsDiscardable() || simpleblock->IsKeyframe();
1998 data = &block->GetBuffer(i);
2000 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER && tk->p_compression_data != NULL )
2001 p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), tk->p_compression_data->GetSize() );
2003 p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), 0 );
2005 if( p_block == NULL )
2010 #if defined(HAVE_ZLIB_H)
2011 if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB )
2013 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
2017 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER )
2019 memcpy( p_block->p_buffer, tk->p_compression_data->GetBuffer(), tk->p_compression_data->GetSize() );
2022 if ( tk->fmt.i_cat == NAV_ES )
2024 // TODO handle the start/stop times of this packet
2025 if ( p_sys->b_ui_hooked )
2027 vlc_mutex_lock( &p_sys->p_ev->lock );
2028 memcpy( &p_sys->pci_packet, &p_block->p_buffer[1], sizeof(pci_t) );
2029 p_sys->SwapButtons();
2030 p_sys->b_pci_packet_set = true;
2031 vlc_mutex_unlock( &p_sys->p_ev->lock );
2032 block_Release( p_block );
2036 // correct timestamping when B frames are used
2037 if( tk->fmt.i_cat != VIDEO_ES )
2039 p_block->i_dts = p_block->i_pts = i_pts;
2043 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
2045 // in VFW we have no idea about B frames
2047 p_block->i_dts = i_pts;
2051 p_block->i_pts = i_pts;
2053 p_block->i_dts = p_block->i_pts;
2055 p_block->i_dts = min( i_pts, tk->i_last_dts + (mtime_t)(tk->i_default_duration >> 10));
2056 p_sys->i_pts = p_block->i_dts;
2059 tk->i_last_dts = p_block->i_dts;
2062 msg_Dbg( p_demux, "block i_dts: %"PRId64" / i_pts: %"PRId64, p_block->i_dts, p_block->i_pts);
2064 if( strcmp( tk->psz_codec, "S_VOBSUB" ) )
2066 p_block->i_length = i_duration * 1000;
2069 es_out_Send( p_demux->out, tk->p_es, p_block );
2071 /* use time stamp only for first block */
2078 matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial )
2080 int i_upper_lvl = 0;
2082 EbmlElement *p_l0, *p_l1, *p_l2;
2083 bool b_keep_stream = false, b_keep_segment;
2085 // verify the EBML Header
2086 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
2089 msg_Err( p_demux, "No EBML header found" );
2093 // verify we can read this Segment, we only support Matroska version 1 for now
2094 p_l0->Read(*p_estream, EbmlHead::ClassInfos.Context, i_upper_lvl, p_l0, true);
2096 EDocType doc_type = GetChild<EDocType>(*static_cast<EbmlHead*>(p_l0));
2097 if (std::string(doc_type) != "matroska")
2099 msg_Err( p_demux, "Not a Matroska file : DocType = %s ", std::string(doc_type).c_str());
2103 EDocTypeReadVersion doc_read_version = GetChild<EDocTypeReadVersion>(*static_cast<EbmlHead*>(p_l0));
2104 #if LIBMATROSKA_VERSION >= 0x000800
2105 if (uint64(doc_read_version) > 2)
2107 msg_Err( p_demux, "This matroska file is needs version %"PRId64" and this VLC only supports version 1 & 2", uint64(doc_read_version));
2111 if (uint64(doc_read_version) != 1)
2113 msg_Err( p_demux, "This matroska file is needs version %"PRId64" and this VLC only supports version 1", uint64(doc_read_version));
2121 // find all segments in this file
2122 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFFLL);
2128 matroska_stream_c *p_stream1 = new matroska_stream_c( *this );
2132 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
2135 matroska_segment_c *p_segment1 = new matroska_segment_c( *this, *p_estream );
2136 b_keep_segment = b_initial;
2138 ep = new EbmlParser(p_estream, p_l0, &demuxer );
2139 p_segment1->ep = ep;
2140 p_segment1->segment = (KaxSegment*)p_l0;
2142 while ((p_l1 = ep->Get()))
2144 if (MKV_IS_ID(p_l1, KaxInfo))
2146 // find the families of this segment
2147 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
2149 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
2150 for( i = 0; i < p_info->ListSize(); i++ )
2152 EbmlElement *l = (*p_info)[i];
2154 if( MKV_IS_ID( l, KaxSegmentUID ) )
2156 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
2157 b_keep_segment = (FindSegment( *p_uid ) == NULL);
2158 if ( !b_keep_segment )
2159 break; // this segment is already known
2160 opened_segments.push_back( p_segment1 );
2161 delete p_segment1->p_segment_uid;
2162 p_segment1->p_segment_uid = new KaxSegmentUID(*p_uid);
2164 else if( MKV_IS_ID( l, KaxPrevUID ) )
2166 p_segment1->p_prev_segment_uid = new KaxPrevUID( *static_cast<KaxPrevUID*>(l) );
2168 else if( MKV_IS_ID( l, KaxNextUID ) )
2170 p_segment1->p_next_segment_uid = new KaxNextUID( *static_cast<KaxNextUID*>(l) );
2172 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
2174 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
2175 p_segment1->families.push_back( p_fam );
2181 if ( b_keep_segment )
2183 b_keep_stream = true;
2184 p_stream1->segments.push_back( p_segment1 );
2188 p_segment1->segment = NULL;
2192 if (p_l0->IsFiniteSize() )
2194 p_l0->SkipData(*p_estream, KaxMatroska_Context);
2195 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
2198 p_l0 = p_l0->SkipData(*p_estream, KaxSegment_Context);
2201 if ( !b_keep_stream )
2210 bool matroska_segment_c::Select( mtime_t i_start_time )
2215 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
2216 sys.b_pci_packet_set = false;
2218 for( i_track = 0; i_track < tracks.size(); i_track++ )
2220 if( tracks[i_track]->fmt.i_cat == UNKNOWN_ES )
2222 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, tracks[i_track]->i_number );
2223 tracks[i_track]->p_es = NULL;
2227 if( !strcmp( tracks[i_track]->psz_codec, "V_MS/VFW/FOURCC" ) )
2229 if( tracks[i_track]->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
2231 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
2232 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2236 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tracks[i_track]->p_extra_data;
2238 tracks[i_track]->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
2239 tracks[i_track]->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
2240 tracks[i_track]->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
2242 tracks[i_track]->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
2243 if( tracks[i_track]->fmt.i_extra > 0 )
2245 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2246 memcpy( tracks[i_track]->fmt.p_extra, &p_bih[1], tracks[i_track]->fmt.i_extra );
2250 else if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG1" ) ||
2251 !strcmp( tracks[i_track]->psz_codec, "V_MPEG2" ) )
2253 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
2255 else if( !strncmp( tracks[i_track]->psz_codec, "V_THEORA", 8 ) )
2257 uint8_t *p_data = tracks[i_track]->p_extra_data;
2258 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
2259 if( tracks[i_track]->i_extra_data >= 4 ) {
2260 if( p_data[0] == 2 ) {
2262 int i_size1 = 0, i_size2 = 0;
2264 /* read size of first header packet */
2265 while( *p_data == 0xFF &&
2266 i < tracks[i_track]->i_extra_data )
2275 msg_Dbg( &sys.demuxer, "first theora header size %d", i_size1 );
2276 /* read size of second header packet */
2277 while( *p_data == 0xFF &&
2278 i < tracks[i_track]->i_extra_data )
2287 int i_size3 = tracks[i_track]->i_extra_data - i - i_size1
2289 msg_Dbg( &sys.demuxer, "second theora header size %d", i_size2 );
2290 msg_Dbg( &sys.demuxer, "third theora header size %d", i_size3 );
2291 tracks[i_track]->fmt.i_extra = i_size1 + i_size2 + i_size3
2293 if( i_size1 > 0 && i_size2 > 0 && i_size3 > 0 ) {
2294 tracks[i_track]->fmt.p_extra =
2295 malloc( tracks[i_track]->fmt.i_extra );
2296 uint8_t *p_out = (uint8_t*)tracks[i_track]->fmt.p_extra;
2297 *p_out++ = (i_size1>>8) & 0xFF;
2298 *p_out++ = i_size1 & 0xFF;
2299 memcpy( p_out, p_data, i_size1 );
2303 *p_out++ = (i_size2>>8) & 0xFF;
2304 *p_out++ = i_size2 & 0xFF;
2305 memcpy( p_out, p_data, i_size2 );
2309 *p_out++ = (i_size3>>8) & 0xFF;
2310 *p_out++ = i_size3 & 0xFF;
2311 memcpy( p_out, p_data, i_size3 );
2317 msg_Err( &sys.demuxer, "inconsistant theora extradata" );
2321 msg_Err( &sys.demuxer, "Wrong number of ogg packets with theora headers (%d)", p_data[0] + 1 );
2325 else if( !strncmp( tracks[i_track]->psz_codec, "V_REAL/RV", 9 ) )
2327 if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV10" ) )
2328 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '1', '0' );
2329 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV20" ) )
2330 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '2', '0' );
2331 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV30" ) )
2332 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '3', '0' );
2333 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV40" ) )
2334 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '4', '0' );
2336 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4", 7 ) )
2338 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/MS/V3" ) )
2340 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
2342 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO", 11 ) )
2344 /* A MPEG 4 codec, SP, ASP, AP or AVC */
2345 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO/AVC" ) )
2346 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
2348 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
2349 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2350 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2351 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2354 else if( !strcmp( tracks[i_track]->psz_codec, "V_QUICKTIME" ) )
2356 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
2357 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
2358 tracks[i_track]->p_extra_data,
2359 tracks[i_track]->i_extra_data,
2361 MP4_ReadBoxCommon( p_mp4_stream, p_box );
2362 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
2363 tracks[i_track]->fmt.i_codec = p_box->i_type;
2364 tracks[i_track]->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
2365 tracks[i_track]->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
2366 tracks[i_track]->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
2367 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2368 memcpy( tracks[i_track]->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tracks[i_track]->fmt.i_extra );
2369 MP4_FreeBox_sample_vide( p_box );
2370 stream_Delete( p_mp4_stream );
2372 else if( !strcmp( tracks[i_track]->psz_codec, "A_MS/ACM" ) )
2374 if( tracks[i_track]->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
2376 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
2377 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2381 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tracks[i_track]->p_extra_data;
2383 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tracks[i_track]->fmt.i_codec, NULL );
2385 tracks[i_track]->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
2386 tracks[i_track]->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
2387 tracks[i_track]->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
2388 tracks[i_track]->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
2389 tracks[i_track]->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
2391 tracks[i_track]->fmt.i_extra = GetWLE( &p_wf->cbSize );
2392 if( tracks[i_track]->fmt.i_extra > 0 )
2394 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2395 memcpy( tracks[i_track]->fmt.p_extra, &p_wf[1], tracks[i_track]->fmt.i_extra );
2399 else if( !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L3" ) ||
2400 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L2" ) ||
2401 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L1" ) )
2403 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
2405 else if( !strcmp( tracks[i_track]->psz_codec, "A_AC3" ) )
2407 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2409 else if( !strcmp( tracks[i_track]->psz_codec, "A_DTS" ) )
2411 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2413 else if( !strcmp( tracks[i_track]->psz_codec, "A_FLAC" ) )
2415 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
2416 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2417 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2418 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2420 else if( !strcmp( tracks[i_track]->psz_codec, "A_VORBIS" ) )
2422 int i, i_offset = 1, i_size[3], i_extra;
2425 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
2427 /* Split the 3 headers */
2428 if( tracks[i_track]->p_extra_data[0] != 0x02 )
2429 msg_Err( &sys.demuxer, "invalid vorbis header" );
2431 for( i = 0; i < 2; i++ )
2434 while( i_offset < tracks[i_track]->i_extra_data )
2436 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
2437 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
2441 i_size[0] = __MIN(i_size[0], tracks[i_track]->i_extra_data - i_offset);
2442 i_size[1] = __MIN(i_size[1], tracks[i_track]->i_extra_data -i_offset -i_size[0]);
2443 i_size[2] = tracks[i_track]->i_extra_data - i_offset - i_size[0] - i_size[1];
2445 tracks[i_track]->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
2446 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2447 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra; i_extra = 0;
2448 for( i = 0; i < 3; i++ )
2450 *(p_extra++) = i_size[i] >> 8;
2451 *(p_extra++) = i_size[i] & 0xFF;
2452 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra,
2454 p_extra += i_size[i];
2455 i_extra += i_size[i];
2458 else if( !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
2459 !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
2461 int i_profile, i_srate, sbr = 0;
2462 static unsigned int i_sample_rates[] =
2464 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
2465 16000, 12000, 11025, 8000, 7350, 0, 0, 0
2468 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
2469 /* create data for faad (MP4DecSpecificDescrTag)*/
2471 if( !strcmp( &tracks[i_track]->psz_codec[12], "MAIN" ) )
2475 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC" ) )
2479 else if( !strcmp( &tracks[i_track]->psz_codec[12], "SSR" ) )
2483 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC/SBR" ) )
2493 for( i_srate = 0; i_srate < 13; i_srate++ )
2495 if( i_sample_rates[i_srate] == tracks[i_track]->i_original_rate )
2500 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
2502 tracks[i_track]->fmt.i_extra = sbr ? 5 : 2;
2503 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2504 ((uint8_t*)tracks[i_track]->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
2505 ((uint8_t*)tracks[i_track]->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tracks[i_track]->fmt.audio.i_channels << 3);
2508 int syncExtensionType = 0x2B7;
2510 for (iDSRI=0; iDSRI<13; iDSRI++)
2511 if( i_sample_rates[iDSRI] == tracks[i_track]->fmt.audio.i_rate )
2513 ((uint8_t*)tracks[i_track]->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
2514 ((uint8_t*)tracks[i_track]->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
2515 ((uint8_t*)tracks[i_track]->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
2518 else if( !strcmp( tracks[i_track]->psz_codec, "A_AAC" ) )
2520 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
2521 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2522 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2523 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2525 else if( !strcmp( tracks[i_track]->psz_codec, "A_WAVPACK4" ) )
2527 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'W', 'V', 'P', 'K' );
2528 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2529 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2530 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2532 else if( !strcmp( tracks[i_track]->psz_codec, "A_TTA1" ) )
2534 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'T', 'T', 'A', '1' );
2535 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2536 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2537 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2539 else if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) ||
2540 !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/LIT" ) ||
2541 !strcmp( tracks[i_track]->psz_codec, "A_PCM/FLOAT/IEEE" ) )
2543 if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) )
2545 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
2549 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
2551 tracks[i_track]->fmt.audio.i_blockalign = ( tracks[i_track]->fmt.audio.i_bitspersample + 7 ) / 8 * tracks[i_track]->fmt.audio.i_channels;
2553 /* disabled due to the potential "S_KATE" namespace issue */
2554 else if( !strcmp( tracks[i_track]->psz_codec, "S_KATE" ) )
2556 int i, i_offset = 1, *i_size, i_extra, num_headers, size_so_far;
2559 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'k', 'a', 't', 'e' );
2560 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2562 /* Recover the number of headers to expect */
2563 num_headers = tracks[i_track]->p_extra_data[0]+1;
2564 msg_Dbg( &sys.demuxer, "kate in mkv detected: %d headers in %u bytes",
2565 num_headers, tracks[i_track]->i_extra_data);
2567 /* this won't overflow the stack as is can allocate only 1020 bytes max */
2568 i_size = (int*)alloca(num_headers*sizeof(int));
2570 /* Split the headers */
2572 for( i = 0; i < num_headers-1; i++ )
2575 while( i_offset < tracks[i_track]->i_extra_data )
2577 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
2578 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
2580 msg_Dbg( &sys.demuxer, "kate header %d is %d bytes", i, i_size[i]);
2581 size_so_far += i_size[i];
2583 i_size[num_headers-1] = tracks[i_track]->i_extra_data - (size_so_far+i_offset);
2584 msg_Dbg( &sys.demuxer, "kate last header (%d) is %d bytes", num_headers-1, i_size[num_headers-1]);
2586 tracks[i_track]->fmt.i_extra = 1 + num_headers * 2 + size_so_far + i_size[num_headers-1];
2587 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2589 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra;
2591 *(p_extra++) = num_headers;
2593 for( i = 0; i < num_headers; i++ )
2595 *(p_extra++) = i_size[i] >> 8;
2596 *(p_extra++) = i_size[i] & 0xFF;
2597 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra-1,
2599 p_extra += i_size[i];
2600 i_extra += i_size[i];
2603 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/UTF8" ) )
2605 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
2606 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2608 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/USF" ) )
2610 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
2611 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2612 if( tracks[i_track]->i_extra_data )
2614 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2615 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2616 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2619 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/SSA" ) ||
2620 !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASS" ) ||
2621 !strcmp( tracks[i_track]->psz_codec, "S_SSA" ) ||
2622 !strcmp( tracks[i_track]->psz_codec, "S_ASS" ))
2624 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
2625 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2626 if( tracks[i_track]->i_extra_data )
2628 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2629 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2630 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2633 else if( !strcmp( tracks[i_track]->psz_codec, "S_VOBSUB" ) )
2635 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
2636 if( tracks[i_track]->i_extra_data )
2639 char *p_buf = (char *)malloc( tracks[i_track]->i_extra_data + 1);
2640 memcpy( p_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
2641 p_buf[tracks[i_track]->i_extra_data] = '\0';
2643 p_start = strstr( p_buf, "size:" );
2644 if( sscanf( p_start, "size: %dx%d",
2645 &tracks[i_track]->fmt.subs.spu.i_original_frame_width, &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) == 2 )
2647 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d", tracks[i_track]->fmt.subs.spu.i_original_frame_width, tracks[i_track]->fmt.subs.spu.i_original_frame_height );
2651 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
2656 else if( !strcmp( tracks[i_track]->psz_codec, "B_VOBBTN" ) )
2658 tracks[i_track]->fmt.i_cat = NAV_ES;
2663 msg_Err( &sys.demuxer, "unknown codec id=`%s'", tracks[i_track]->psz_codec );
2664 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2666 if( tracks[i_track]->b_default )
2668 tracks[i_track]->fmt.i_priority = 1000;
2671 tracks[i_track]->p_es = es_out_Add( sys.demuxer.out, &tracks[i_track]->fmt );
2673 /* Turn on a subtitles track if it has been flagged as default -
2674 * but only do this if no subtitles track has already been engaged,
2675 * either by an earlier 'default track' (??) or by default
2676 * language choice behaviour.
2678 if( tracks[i_track]->b_default )
2680 es_out_Control( sys.demuxer.out,
2682 tracks[i_track]->p_es );
2685 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, i_start_time );
2688 sys.i_start_pts = i_start_time;
2689 // reset the stream reading to the first cluster of the segment used
2690 es.I_O().setFilePointer( i_start_pos );
2693 ep = new EbmlParser( &es, segment, &sys.demuxer );
2698 void demux_sys_t::StartUiThread()
2702 msg_Dbg( &demuxer, "Starting the UI Hook" );
2704 /* FIXME hack hack hack hack FIXME */
2705 /* Get p_input and create variable */
2706 p_input = (input_thread_t *) vlc_object_find( &demuxer, VLC_OBJECT_INPUT, FIND_PARENT );
2707 var_Create( p_input, "x-start", VLC_VAR_INTEGER );
2708 var_Create( p_input, "y-start", VLC_VAR_INTEGER );
2709 var_Create( p_input, "x-end", VLC_VAR_INTEGER );
2710 var_Create( p_input, "y-end", VLC_VAR_INTEGER );
2711 var_Create( p_input, "color", VLC_VAR_ADDRESS );
2712 var_Create( p_input, "menu-palette", VLC_VAR_ADDRESS );
2713 var_Create( p_input, "highlight", VLC_VAR_BOOL );
2714 var_Create( p_input, "highlight-mutex", VLC_VAR_MUTEX );
2716 /* Now create our event thread catcher */
2717 p_ev = (event_thread_t *) vlc_object_create( &demuxer, sizeof( event_thread_t ) );
2718 p_ev->p_demux = &demuxer;
2719 p_ev->b_die = false;
2720 vlc_mutex_init( &p_ev->lock );
2721 vlc_thread_create( p_ev, "mkv event thread handler", EventThread,
2722 VLC_THREAD_PRIORITY_LOW, false );
2726 void demux_sys_t::StopUiThread()
2730 vlc_object_kill( p_ev );
2731 vlc_thread_join( p_ev );
2732 vlc_object_release( p_ev );
2736 var_Destroy( p_input, "highlight-mutex" );
2737 var_Destroy( p_input, "highlight" );
2738 var_Destroy( p_input, "x-start" );
2739 var_Destroy( p_input, "x-end" );
2740 var_Destroy( p_input, "y-start" );
2741 var_Destroy( p_input, "y-end" );
2742 var_Destroy( p_input, "color" );
2743 var_Destroy( p_input, "menu-palette" );
2745 vlc_object_release( p_input );
2747 msg_Dbg( &demuxer, "Stopping the UI Hook" );
2749 b_ui_hooked = false;
2752 int demux_sys_t::EventMouse( vlc_object_t *p_this, char const *psz_var,
2753 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2755 event_thread_t *p_ev = (event_thread_t *) p_data;
2756 vlc_mutex_lock( &p_ev->lock );
2757 if( psz_var[6] == 'c' )
2759 p_ev->b_clicked = true;
2760 msg_Dbg( p_this, "Event Mouse: clicked");
2762 else if( psz_var[6] == 'm' )
2763 p_ev->b_moved = true;
2764 vlc_mutex_unlock( &p_ev->lock );
2769 int demux_sys_t::EventKey( vlc_object_t *p_this, char const *,
2770 vlc_value_t, vlc_value_t newval, void *p_data )
2772 event_thread_t *p_ev = (event_thread_t *) p_data;
2773 vlc_mutex_lock( &p_ev->lock );
2774 p_ev->i_key_action = newval.i_int;
2775 vlc_mutex_unlock( &p_ev->lock );
2776 msg_Dbg( p_this, "Event Key");
2781 int demux_sys_t::EventThread( vlc_object_t *p_this )
2783 event_thread_t *p_ev = (event_thread_t*)p_this;
2784 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
2785 vlc_object_t *p_vout = NULL;
2787 p_ev->b_moved = false;
2788 p_ev->b_clicked = false;
2789 p_ev->i_key_action = 0;
2791 /* catch all key event */
2792 var_AddCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
2795 while( vlc_object_alive (p_ev) )
2797 if ( !p_sys->b_pci_packet_set )
2804 bool b_activated = false;
2807 if( p_ev->i_key_action )
2811 msg_Dbg( p_ev->p_demux, "Handle Key Event");
2813 vlc_mutex_lock( &p_ev->lock );
2815 pci_t *pci = (pci_t *) &p_sys->pci_packet;
2817 uint16 i_curr_button = p_sys->dvd_interpretor.GetSPRM( 0x88 );
2819 switch( p_ev->i_key_action )
2821 case ACTIONID_NAV_LEFT:
2822 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2824 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2825 if ( p_button_ptr->left > 0 && p_button_ptr->left <= pci->hli.hl_gi.btn_ns )
2827 i_curr_button = p_button_ptr->left;
2828 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2829 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2830 if ( button_ptr.auto_action_mode )
2832 vlc_mutex_unlock( &p_ev->lock );
2833 vlc_mutex_lock( &p_sys->lock_demuxer );
2835 // process the button action
2836 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2838 vlc_mutex_unlock( &p_sys->lock_demuxer );
2839 vlc_mutex_lock( &p_ev->lock );
2844 case ACTIONID_NAV_RIGHT:
2845 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2847 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2848 if ( p_button_ptr->right > 0 && p_button_ptr->right <= pci->hli.hl_gi.btn_ns )
2850 i_curr_button = p_button_ptr->right;
2851 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2852 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2853 if ( button_ptr.auto_action_mode )
2855 vlc_mutex_unlock( &p_ev->lock );
2856 vlc_mutex_lock( &p_sys->lock_demuxer );
2858 // process the button action
2859 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2861 vlc_mutex_unlock( &p_sys->lock_demuxer );
2862 vlc_mutex_lock( &p_ev->lock );
2867 case ACTIONID_NAV_UP:
2868 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2870 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2871 if ( p_button_ptr->up > 0 && p_button_ptr->up <= pci->hli.hl_gi.btn_ns )
2873 i_curr_button = p_button_ptr->up;
2874 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2875 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2876 if ( button_ptr.auto_action_mode )
2878 vlc_mutex_unlock( &p_ev->lock );
2879 vlc_mutex_lock( &p_sys->lock_demuxer );
2881 // process the button action
2882 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2884 vlc_mutex_unlock( &p_sys->lock_demuxer );
2885 vlc_mutex_lock( &p_ev->lock );
2890 case ACTIONID_NAV_DOWN:
2891 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2893 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2894 if ( p_button_ptr->down > 0 && p_button_ptr->down <= pci->hli.hl_gi.btn_ns )
2896 i_curr_button = p_button_ptr->down;
2897 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2898 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2899 if ( button_ptr.auto_action_mode )
2901 vlc_mutex_unlock( &p_ev->lock );
2902 vlc_mutex_lock( &p_sys->lock_demuxer );
2904 // process the button action
2905 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2907 vlc_mutex_unlock( &p_sys->lock_demuxer );
2908 vlc_mutex_lock( &p_ev->lock );
2913 case ACTIONID_NAV_ACTIVATE:
2916 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2918 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2920 vlc_mutex_unlock( &p_ev->lock );
2921 vlc_mutex_lock( &p_sys->lock_demuxer );
2923 // process the button action
2924 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2926 vlc_mutex_unlock( &p_sys->lock_demuxer );
2927 vlc_mutex_lock( &p_ev->lock );
2933 p_ev->i_key_action = 0;
2934 vlc_mutex_unlock( &p_ev->lock );
2938 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
2940 vlc_value_t valx, valy;
2942 vlc_mutex_lock( &p_ev->lock );
2943 pci_t *pci = (pci_t *) &p_sys->pci_packet;
2944 var_Get( p_vout, "mouse-x", &valx );
2945 var_Get( p_vout, "mouse-y", &valy );
2947 if( p_ev->b_clicked )
2950 int32_t best,dist,d;
2951 int32_t mx,my,dx,dy;
2953 msg_Dbg( p_ev->p_demux, "Handle Mouse Event: Mouse clicked x(%d)*y(%d)", (unsigned)valx.i_int, (unsigned)valy.i_int);
2956 // get current button
2958 dist = 0x08000000; /* >> than (720*720)+(567*567); */
2959 for(button = 1; button <= pci->hli.hl_gi.btn_ns; button++)
2961 btni_t *button_ptr = &(pci->hli.btnit[button-1]);
2963 if(((unsigned)valx.i_int >= button_ptr->x_start)
2964 && ((unsigned)valx.i_int <= button_ptr->x_end)
2965 && ((unsigned)valy.i_int >= button_ptr->y_start)
2966 && ((unsigned)valy.i_int <= button_ptr->y_end))
2968 mx = (button_ptr->x_start + button_ptr->x_end)/2;
2969 my = (button_ptr->y_start + button_ptr->y_end)/2;
2970 dx = mx - valx.i_int;
2971 dy = my - valy.i_int;
2972 d = (dx*dx) + (dy*dy);
2973 /* If the mouse is within the button and the mouse is closer
2974 * to the center of this button then it is the best choice. */
2984 btni_t button_ptr = pci->hli.btnit[best-1];
2985 uint16 i_curr_button = p_sys->dvd_interpretor.GetSPRM( 0x88 );
2987 msg_Dbg( &p_sys->demuxer, "Clicked button %d", best );
2988 vlc_mutex_unlock( &p_ev->lock );
2989 vlc_mutex_lock( &p_sys->lock_demuxer );
2991 // process the button action
2992 p_sys->dvd_interpretor.SetSPRM( 0x88, best );
2993 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2995 msg_Dbg( &p_sys->demuxer, "Processed button %d", best );
2997 // select new button
2998 if ( best != i_curr_button )
3002 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
3004 vlc_mutex_t *p_mutex = (vlc_mutex_t *) val.p_address;
3007 if(button_ptr.btn_coln != 0) {
3008 i_palette = pci->hli.btn_colit.btn_coli[button_ptr.btn_coln-1][1];
3013 for( int i = 0; i < 4; i++ )
3015 uint32_t i_yuv = 0xFF;//p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
3016 uint8_t i_alpha = (i_palette>>(i*4))&0x0f;
3017 i_alpha = i_alpha == 0xf ? 0xff : i_alpha << 4;
3019 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
3020 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
3021 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
3022 p_sys->palette[i][3] = i_alpha;
3025 vlc_mutex_lock( p_mutex );
3026 val.i_int = button_ptr.x_start; var_Set( p_sys->p_input, "x-start", val );
3027 val.i_int = button_ptr.x_end; var_Set( p_sys->p_input, "x-end", val );
3028 val.i_int = button_ptr.y_start; var_Set( p_sys->p_input, "y-start", val );
3029 val.i_int = button_ptr.y_end; var_Set( p_sys->p_input, "y-end", val );
3031 val.p_address = (void *)p_sys->palette;
3032 var_Set( p_sys->p_input, "menu-palette", val );
3034 val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
3035 vlc_mutex_unlock( p_mutex );
3038 vlc_mutex_unlock( &p_sys->lock_demuxer );
3039 vlc_mutex_lock( &p_ev->lock );
3042 else if( p_ev->b_moved )
3044 // dvdnav_mouse_select( NULL, pci, valx.i_int, valy.i_int );
3047 p_ev->b_moved = false;
3048 p_ev->b_clicked = false;
3049 vlc_mutex_unlock( &p_ev->lock );
3053 if( p_vout && !vlc_object_alive (p_vout) )
3055 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3056 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3057 vlc_object_release( p_vout );
3061 else if( p_vout == NULL )
3063 p_vout = (vlc_object_t*) vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
3067 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3068 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3072 /* Wait a bit, 10ms */
3076 /* Release callback */
3079 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3080 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3081 vlc_object_release( p_vout );
3083 var_DelCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
3085 vlc_mutex_destroy( &p_ev->lock );
3090 void matroska_segment_c::UnSelect( )
3094 for( i_track = 0; i_track < tracks.size(); i_track++ )
3096 if ( tracks[i_track]->p_es != NULL )
3098 // es_format_Clean( &tracks[i_track]->fmt );
3099 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
3100 tracks[i_track]->p_es = NULL;
3107 void virtual_segment_c::PrepareChapters( )
3109 if ( linked_segments.size() == 0 )
3112 // !!! should be called only once !!!
3113 matroska_segment_c *p_segment;
3116 // copy editions from the first segment
3117 p_segment = linked_segments[0];
3118 p_editions = &p_segment->stored_editions;
3120 for ( i=1 ; i<linked_segments.size(); i++ )
3122 p_segment = linked_segments[i];
3123 // FIXME assume we have the same editions in all segments
3124 for (j=0; j<p_segment->stored_editions.size(); j++)
3126 if( j >= p_editions->size() ) /* Protect against broken files (?) */
3128 (*p_editions)[j]->Append( *p_segment->stored_editions[j] );
3133 std::string chapter_edition_c::GetMainName() const
3135 if ( sub_chapters.size() )
3137 return sub_chapters[0]->GetCodecName( true );
3142 int chapter_item_c::PublishChapters( input_title_t & title, int & i_user_chapters, int i_level )
3144 // add support for meta-elements from codec like DVD Titles
3145 if ( !b_display_seekpoint || psz_name == "" )
3147 psz_name = GetCodecName();
3148 if ( psz_name != "" )
3149 b_display_seekpoint = true;
3152 if (b_display_seekpoint)
3154 seekpoint_t *sk = vlc_seekpoint_New();
3156 sk->i_level = i_level;
3157 sk->i_time_offset = i_start_time;
3158 sk->psz_name = strdup( psz_name.c_str() );
3160 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
3161 title.i_seekpoint++;
3162 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
3163 title.seekpoint[title.i_seekpoint-1] = sk;
3165 if ( b_user_display )
3169 for ( size_t i=0; i<sub_chapters.size() ; i++)
3171 sub_chapters[i]->PublishChapters( title, i_user_chapters, i_level+1 );
3174 i_seekpoint_num = i_user_chapters;
3176 return i_user_chapters;
3179 bool virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
3181 demux_sys_t & sys = *demux.p_sys;
3182 chapter_item_c *psz_curr_chapter;
3183 bool b_has_seeked = false;
3185 /* update current chapter/seekpoint */
3186 if ( p_editions->size() )
3188 /* 1st, we need to know in which chapter we are */
3189 psz_curr_chapter = (*p_editions)[i_current_edition]->FindTimecode( sys.i_pts, psz_current_chapter );
3191 /* we have moved to a new chapter */
3192 if (psz_curr_chapter != NULL && psz_current_chapter != psz_curr_chapter)
3194 if ( (*p_editions)[i_current_edition]->b_ordered )
3196 // Leave/Enter up to the link point
3197 b_has_seeked = psz_curr_chapter->EnterAndLeave( psz_current_chapter );
3198 if ( !b_has_seeked )
3200 // only physically seek if necessary
3201 if ( psz_current_chapter == NULL || (psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time) )
3202 Seek( demux, sys.i_pts, 0, psz_curr_chapter );
3206 if ( !b_has_seeked )
3208 psz_current_chapter = psz_curr_chapter;
3209 if ( psz_curr_chapter->i_seekpoint_num > 0 )
3211 demux.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
3212 demux.info.i_title = sys.i_current_title = i_sys_title;
3213 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
3219 else if (psz_curr_chapter == NULL)
3221 // out of the scope of the data described by chapters, leave the edition
3222 if ( (*p_editions)[i_current_edition]->b_ordered && psz_current_chapter != NULL )
3224 if ( !(*p_editions)[i_current_edition]->EnterAndLeave( psz_current_chapter, false ) )
3225 psz_current_chapter = NULL;
3234 chapter_item_c *virtual_segment_c::BrowseCodecPrivate( unsigned int codec_id,
3235 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3236 const void *p_cookie,
3237 size_t i_cookie_size )
3239 // FIXME don't assume it is the first edition
3240 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
3241 if ( index != p_editions->end() )
3243 chapter_item_c *p_result = (*index)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3244 if ( p_result != NULL )
3250 chapter_item_c *virtual_segment_c::FindChapter( int64_t i_find_uid )
3252 // FIXME don't assume it is the first edition
3253 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
3254 if ( index != p_editions->end() )
3256 chapter_item_c *p_result = (*index)->FindChapter( i_find_uid );
3257 if ( p_result != NULL )
3263 chapter_item_c *chapter_item_c::BrowseCodecPrivate( unsigned int codec_id,
3264 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3265 const void *p_cookie,
3266 size_t i_cookie_size )
3269 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3270 while ( index != codecs.end() )
3272 if ( match( **index ,p_cookie, i_cookie_size ) )
3278 chapter_item_c *p_result = NULL;
3279 std::vector<chapter_item_c*>::const_iterator index2 = sub_chapters.begin();
3280 while ( index2 != sub_chapters.end() )
3282 p_result = (*index2)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3283 if ( p_result != NULL )
3291 void chapter_item_c::Append( const chapter_item_c & chapter )
3293 // we are appending content for the same chapter UID
3295 chapter_item_c *p_chapter;
3297 for ( i=0; i<chapter.sub_chapters.size(); i++ )
3299 p_chapter = FindChapter( chapter.sub_chapters[i]->i_uid );
3300 if ( p_chapter != NULL )
3302 p_chapter->Append( *chapter.sub_chapters[i] );
3306 sub_chapters.push_back( chapter.sub_chapters[i] );
3310 i_user_start_time = min( i_user_start_time, chapter.i_user_start_time );
3311 i_user_end_time = max( i_user_end_time, chapter.i_user_end_time );
3314 chapter_item_c * chapter_item_c::FindChapter( int64_t i_find_uid )
3317 chapter_item_c *p_result = NULL;
3319 if ( i_uid == i_find_uid )
3322 for ( i=0; i<sub_chapters.size(); i++)
3324 p_result = sub_chapters[i]->FindChapter( i_find_uid );
3325 if ( p_result != NULL )
3331 std::string chapter_item_c::GetCodecName( bool f_for_title ) const
3335 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3336 while ( index != codecs.end() )
3338 result = (*index)->GetCodecName( f_for_title );
3347 std::string dvd_chapter_codec_c::GetCodecName( bool f_for_title ) const
3350 if ( p_private_data->GetSize() >= 3)
3352 const binary* p_data = p_private_data->GetBuffer();
3353 /* if ( p_data[0] == MATROSKA_DVD_LEVEL_TT )
3355 uint16_t i_title = (p_data[1] << 8) + p_data[2];
3357 sprintf( psz_str, " %d ---", i_title );
3358 result = N_("--- DVD Title");
3361 else */ if ( p_data[0] == MATROSKA_DVD_LEVEL_LU )
3364 sprintf( psz_str, " (%c%c) ---", p_data[1], p_data[2] );
3365 result = N_("--- DVD Menu");
3368 else if ( p_data[0] == MATROSKA_DVD_LEVEL_SS && f_for_title )
3370 if ( p_data[1] == 0x00 )
3371 result = N_("First Played");
3372 else if ( p_data[1] == 0xC0 )
3373 result = N_("Video Manager");
3374 else if ( p_data[1] == 0x80 )
3376 uint16_t i_title = (p_data[2] << 8) + p_data[3];
3378 sprintf( psz_str, " %d -----", i_title );
3379 result = N_("----- Title");
3388 int16 chapter_item_c::GetTitleNumber( ) const
3392 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3393 while ( index != codecs.end() )
3395 result = (*index)->GetTitleNumber( );
3404 int16 dvd_chapter_codec_c::GetTitleNumber()
3406 if ( p_private_data->GetSize() >= 3)
3408 const binary* p_data = p_private_data->GetBuffer();
3409 if ( p_data[0] == MATROSKA_DVD_LEVEL_SS )
3411 return int16( (p_data[2] << 8) + p_data[3] );
3417 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter )
3419 demux_sys_t *p_sys = p_demux->p_sys;
3420 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
3421 matroska_segment_c *p_segment = p_vsegment->Segment();
3422 mtime_t i_time_offset = 0;
3426 msg_Dbg( p_demux, "seek request to %"PRId64" (%f%%)", i_date, f_percent );
3427 if( i_date < 0 && f_percent < 0 )
3429 msg_Warn( p_demux, "cannot seek nowhere !" );
3432 if( f_percent > 1.0 )
3434 msg_Warn( p_demux, "cannot seek so far !" );
3438 /* seek without index or without date */
3439 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
3441 if (p_sys->f_duration >= 0)
3443 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
3447 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
3449 msg_Dbg( p_demux, "inacurate way of seeking" );
3450 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
3452 if( p_segment->p_indexes[i_index].i_position >= i_pos)
3457 if( i_index == p_segment->i_index )
3462 i_date = p_segment->p_indexes[i_index].i_time;
3465 if( p_segment->p_indexes[i_index].i_position < i_pos )
3469 msg_Warn( p_demux, "searching for cluster, could take some time" );
3471 /* search a cluster */
3472 while( ( el = p_sys->ep->Get() ) != NULL )
3474 if( MKV_IS_ID( el, KaxCluster ) )
3476 KaxCluster *cluster = (KaxCluster*)el;
3478 /* add it to the index */
3479 p_segment->IndexAppendCluster( cluster );
3481 if( (int64_t)cluster->GetElementPosition() >= i_pos )
3483 p_sys->cluster = cluster;
3494 p_vsegment->Seek( *p_demux, i_date, i_time_offset, psz_chapter );
3497 /*****************************************************************************
3498 * Demux: reads and demuxes data packets
3499 *****************************************************************************
3500 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
3501 *****************************************************************************/
3502 static int Demux( demux_t *p_demux)
3504 demux_sys_t *p_sys = p_demux->p_sys;
3506 vlc_mutex_lock( &p_sys->lock_demuxer );
3508 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
3509 matroska_segment_c *p_segment = p_vsegment->Segment();
3510 if ( p_segment == NULL ) return 0;
3511 int i_block_count = 0;
3516 if ( p_sys->demuxer.b_die )
3519 if( p_sys->i_pts >= p_sys->i_start_pts )
3520 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
3526 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
3528 /* nothing left to read in this ordered edition */
3529 if ( !p_vsegment->SelectNext() )
3531 p_segment->UnSelect( );
3533 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3535 /* switch to the next segment */
3536 p_segment = p_vsegment->Segment();
3537 if ( !p_segment->Select( 0 ) )
3539 msg_Err( p_demux, "Failed to select new segment" );
3546 int64_t i_block_duration = 0;
3547 int64_t i_block_ref1;
3548 int64_t i_block_ref2;
3550 #if LIBMATROSKA_VERSION >= 0x000800
3551 KaxSimpleBlock *simpleblock;
3553 if( p_segment->BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
3555 if( p_segment->BlockGet( block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
3558 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered )
3560 const chapter_item_c *p_chap = p_vsegment->CurrentChapter();
3561 // check if there are more chapters to read
3562 if ( p_chap != NULL )
3564 /* TODO handle successive chapters with the same user_start_time/user_end_time
3565 if ( p_chap->i_user_start_time == p_chap->i_user_start_time )
3566 p_vsegment->SelectNext();
3568 p_sys->i_pts = p_chap->i_user_end_time;
3569 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
3578 msg_Warn( p_demux, "cannot get block EOF?" );
3579 p_segment->UnSelect( );
3581 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3583 /* switch to the next segment */
3584 if ( !p_vsegment->SelectNext() )
3585 // no more segments in this stream
3587 p_segment = p_vsegment->Segment();
3588 if ( !p_segment->Select( 0 ) )
3590 msg_Err( p_demux, "Failed to select new segment" );
3598 #if LIBMATROSKA_VERSION >= 0x000800
3599 if ( simpleblock != NULL )
3600 p_sys->i_pts = (p_sys->i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
3603 p_sys->i_pts = (p_sys->i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
3605 if( p_sys->i_pts >= p_sys->i_start_pts )
3607 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
3609 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
3617 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
3619 /* nothing left to read in this ordered edition */
3620 if ( !p_vsegment->SelectNext() )
3625 p_segment->UnSelect( );
3627 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3629 /* switch to the next segment */
3630 p_segment = p_vsegment->Segment();
3631 if ( !p_segment->Select( 0 ) )
3633 msg_Err( p_demux, "Failed to select new segment" );
3641 #if LIBMATROSKA_VERSION >= 0x000800
3642 BlockDecode( p_demux, block, simpleblock, p_sys->i_pts, i_block_duration, i_block_ref1 >= 0 || i_block_ref2 > 0 );
3644 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration, i_block_ref1 >= 0 || i_block_ref2 > 0 );
3650 // TODO optimize when there is need to leave or when seeking has been called
3651 if( i_block_count > 5 )
3658 vlc_mutex_unlock( &p_sys->lock_demuxer );
3665 /*****************************************************************************
3667 *****************************************************************************/
3668 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_, bool b_owner_ )
3675 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
3677 if( i_size <= 0 || mb_eof )
3682 return stream_Read( s, p_buffer, i_size );
3684 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
3690 case seek_beginning:
3694 i_pos = stream_Size( s ) - i_offset;
3697 i_pos= stream_Tell( s ) + i_offset;
3701 if( i_pos < 0 || i_pos >= stream_Size( s ) )
3708 if( stream_Seek( s, i_pos ) )
3714 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
3718 uint64 vlc_stream_io_callback::getFilePointer( void )
3722 return stream_Tell( s );
3724 void vlc_stream_io_callback::close( void )
3730 /*****************************************************************************
3731 * Ebml Stream parser
3732 *****************************************************************************/
3733 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux )
3740 mi_remain_size[0] = el_start->GetSize();
3742 for( i = 1; i < 6; i++ )
3749 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
3752 EbmlParser::~EbmlParser( void )
3756 for( i = 1; i < mi_level; i++ )
3766 EbmlElement* EbmlParser::UnGet( uint64 i_block_pos, uint64 i_cluster_pos )
3768 if ( mi_user_level > mi_level )
3770 while ( mi_user_level != mi_level )
3772 delete m_el[mi_user_level];
3773 m_el[mi_user_level] = NULL;
3779 if ( m_el[1]->GetElementPosition() == i_cluster_pos )
3781 m_es->I_O().setFilePointer( i_block_pos, seek_beginning );
3782 return (EbmlMaster*) m_el[1];
3786 // seek to the previous Cluster
3787 m_es->I_O().setFilePointer( i_cluster_pos, seek_beginning );
3790 delete m_el[mi_level];
3791 m_el[mi_level] = NULL;
3796 void EbmlParser::Up( void )
3798 if( mi_user_level == mi_level )
3800 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
3806 void EbmlParser::Down( void )
3812 void EbmlParser::Keep( void )
3817 int EbmlParser::GetLevel( void )
3819 return mi_user_level;
3822 void EbmlParser::Reset( demux_t *p_demux )
3824 while ( mi_level > 0)
3826 delete m_el[mi_level];
3827 m_el[mi_level] = NULL;
3830 mi_user_level = mi_level = 1;
3831 #if LIBEBML_VERSION >= 0x000704
3832 // a little faster and cleaner
3833 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
3835 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
3837 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
3840 EbmlElement *EbmlParser::Get( void )
3844 if( mi_user_level != mi_level )
3850 EbmlElement *ret = m_got;
3856 if( m_el[mi_level] )
3858 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
3861 delete m_el[mi_level];
3866 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, mb_dummy != 0, 1 );
3867 // mi_remain_size[mi_level] = m_el[mi_level]->GetSize();
3878 delete m_el[mi_level - 1];
3879 m_got = m_el[mi_level -1] = m_el[mi_level];
3880 m_el[mi_level] = NULL;
3887 else if( m_el[mi_level] == NULL )
3889 fprintf( stderr," m_el[mi_level] == NULL\n" );
3892 return m_el[mi_level];
3896 /*****************************************************************************
3898 * * LoadCues : load the cues element and update index
3900 * * LoadTags : load ... the tags element
3902 * * InformationCreate : create all information, load tags if present
3904 *****************************************************************************/
3905 void matroska_segment_c::LoadCues( )
3907 int64_t i_sav_position = es.I_O().getFilePointer();
3909 EbmlElement *el, *cues;
3911 /* *** Load the cue if found *** */
3912 if( i_cues_position < 0 )
3914 msg_Warn( &sys.demuxer, "no cues/empty cues found->seek won't be precise" );
3916 // IndexAppendCluster( cluster );
3921 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
3925 msg_Dbg( &sys.demuxer, "loading cues" );
3926 es.I_O().setFilePointer( i_cues_position, seek_beginning );
3927 cues = es.FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
3931 msg_Err( &sys.demuxer, "cannot load cues (broken seekhead or file)" );
3932 es.I_O().setFilePointer( i_sav_position, seek_beginning );
3936 ep = new EbmlParser( &es, cues, &sys.demuxer );
3937 while( ( el = ep->Get() ) != NULL )
3939 if( MKV_IS_ID( el, KaxCuePoint ) )
3941 #define idx p_indexes[i_index]
3944 idx.i_block_number= -1;
3945 idx.i_position = -1;
3950 while( ( el = ep->Get() ) != NULL )
3952 if( MKV_IS_ID( el, KaxCueTime ) )
3954 KaxCueTime &ctime = *(KaxCueTime*)el;
3956 ctime.ReadData( es.I_O() );
3958 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
3960 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
3963 while( ( el = ep->Get() ) != NULL )
3965 if( MKV_IS_ID( el, KaxCueTrack ) )
3967 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
3969 ctrack.ReadData( es.I_O() );
3970 idx.i_track = uint16( ctrack );
3972 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
3974 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
3976 ccpos.ReadData( es.I_O() );
3977 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
3979 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
3981 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
3983 cbnum.ReadData( es.I_O() );
3984 idx.i_block_number = uint32( cbnum );
3988 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
3995 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
4001 msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
4002 " track=%d bnum=%d", idx.i_time, idx.i_position,
4003 idx.i_track, idx.i_block_number );
4007 if( i_index >= i_index_max )
4009 i_index_max += 1024;
4010 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
4016 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
4024 msg_Dbg( &sys.demuxer, "loading cues done." );
4025 es.I_O().setFilePointer( i_sav_position, seek_beginning );
4028 void matroska_segment_c::LoadTags( )
4030 int64_t i_sav_position = es.I_O().getFilePointer();
4032 EbmlElement *el, *tags;
4034 msg_Dbg( &sys.demuxer, "loading tags" );
4035 es.I_O().setFilePointer( i_tags_position, seek_beginning );
4036 tags = es.FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
4040 msg_Err( &sys.demuxer, "cannot load tags (broken seekhead or file)" );
4041 es.I_O().setFilePointer( i_sav_position, seek_beginning );
4045 msg_Dbg( &sys.demuxer, "Tags" );
4046 ep = new EbmlParser( &es, tags, &sys.demuxer );
4047 while( ( el = ep->Get() ) != NULL )
4049 if( MKV_IS_ID( el, KaxTag ) )
4051 msg_Dbg( &sys.demuxer, "+ Tag" );
4053 while( ( el = ep->Get() ) != NULL )
4055 if( MKV_IS_ID( el, KaxTagTargets ) )
4057 msg_Dbg( &sys.demuxer, "| + Targets" );
4059 while( ( el = ep->Get() ) != NULL )
4061 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4065 else if( MKV_IS_ID( el, KaxTagGeneral ) )
4067 msg_Dbg( &sys.demuxer, "| + General" );
4069 while( ( el = ep->Get() ) != NULL )
4071 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4075 else if( MKV_IS_ID( el, KaxTagGenres ) )
4077 msg_Dbg( &sys.demuxer, "| + Genres" );
4079 while( ( el = ep->Get() ) != NULL )
4081 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4085 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
4087 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
4089 while( ( el = ep->Get() ) != NULL )
4091 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4095 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
4097 msg_Dbg( &sys.demuxer, "| + Images Specific" );
4099 while( ( el = ep->Get() ) != NULL )
4101 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4105 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
4107 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
4109 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
4111 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
4113 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
4115 msg_Dbg( &sys.demuxer, "| + Multi Date" );
4117 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
4119 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
4121 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
4123 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
4125 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
4127 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
4129 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
4131 msg_Dbg( &sys.demuxer, "| + Multi Title" );
4135 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid( *el ).name() );
4142 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
4148 msg_Dbg( &sys.demuxer, "loading tags done." );
4149 es.I_O().setFilePointer( i_sav_position, seek_beginning );
4152 /*****************************************************************************
4154 *****************************************************************************/
4155 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
4159 int i_upper_level = 0;
4161 msg_Dbg( &sys.demuxer, "| + Seek head" );
4163 /* Master elements */
4164 seekhead->Read( es, seekhead->Generic().Context, i_upper_level, el, true );
4166 for( i = 0; i < seekhead->ListSize(); i++ )
4168 EbmlElement *l = (*seekhead)[i];
4170 if( MKV_IS_ID( l, KaxSeek ) )
4172 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
4173 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
4176 for( j = 0; j < sk->ListSize(); j++ )
4178 EbmlElement *l = (*sk)[j];
4180 if( MKV_IS_ID( l, KaxSeekID ) )
4182 KaxSeekID &sid = *(KaxSeekID*)l;
4183 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
4185 else if( MKV_IS_ID( l, KaxSeekPosition ) )
4187 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
4188 i_pos = uint64( spos );
4192 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
4198 if( id == KaxCues::ClassInfos.GlobalId )
4200 msg_Dbg( &sys.demuxer, "| | | = cues at %"PRId64, i_pos );
4201 i_cues_position = segment->GetGlobalPosition( i_pos );
4203 else if( id == KaxChapters::ClassInfos.GlobalId )
4205 msg_Dbg( &sys.demuxer, "| | | = chapters at %"PRId64, i_pos );
4206 i_chapters_position = segment->GetGlobalPosition( i_pos );
4208 else if( id == KaxTags::ClassInfos.GlobalId )
4210 msg_Dbg( &sys.demuxer, "| | | = tags at %"PRId64, i_pos );
4211 i_tags_position = segment->GetGlobalPosition( i_pos );
4217 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4222 /*****************************************************************************
4224 *****************************************************************************/
4225 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
4228 bool bSupported = true;
4232 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
4234 tk = new mkv_track_t();
4236 /* Init the track */
4237 memset( tk, 0, sizeof( mkv_track_t ) );
4239 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
4240 tk->fmt.psz_language = strdup("English");
4241 tk->fmt.psz_description = NULL;
4243 tk->b_default = true;
4244 tk->b_enabled = true;
4245 tk->b_silent = false;
4246 tk->i_number = tracks.size() - 1;
4247 tk->i_extra_data = 0;
4248 tk->p_extra_data = NULL;
4249 tk->psz_codec = NULL;
4250 tk->i_default_duration = 0;
4251 tk->f_timecodescale = 1.0;
4253 tk->b_inited = false;
4254 tk->i_data_init = 0;
4255 tk->p_data_init = NULL;
4257 tk->psz_codec_name = NULL;
4258 tk->psz_codec_settings = NULL;
4259 tk->psz_codec_info_url = NULL;
4260 tk->psz_codec_download_url = NULL;
4262 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
4263 tk->p_compression_data = NULL;
4265 for( i = 0; i < m->ListSize(); i++ )
4267 EbmlElement *l = (*m)[i];
4269 if( MKV_IS_ID( l, KaxTrackNumber ) )
4271 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
4273 tk->i_number = uint32( tnum );
4274 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
4276 else if( MKV_IS_ID( l, KaxTrackUID ) )
4278 KaxTrackUID &tuid = *(KaxTrackUID*)l;
4280 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
4282 else if( MKV_IS_ID( l, KaxTrackType ) )
4284 const char *psz_type;
4285 KaxTrackType &ttype = *(KaxTrackType*)l;
4287 switch( uint8(ttype) )
4291 tk->fmt.i_cat = AUDIO_ES;
4295 tk->fmt.i_cat = VIDEO_ES;
4297 case track_subtitle:
4298 psz_type = "subtitle";
4299 tk->fmt.i_cat = SPU_ES;
4302 psz_type = "buttons";
4303 tk->fmt.i_cat = SPU_ES;
4306 psz_type = "unknown";
4307 tk->fmt.i_cat = UNKNOWN_ES;
4311 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
4313 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
4315 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
4317 // tk->b_enabled = uint32( fenb );
4318 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
4319 // uint32( fenb ) );
4321 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
4323 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
4325 tk->b_default = uint32( fdef );
4326 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
4328 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
4330 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
4332 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
4334 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
4336 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
4338 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
4340 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
4342 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
4344 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
4346 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
4348 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
4350 tk->i_default_duration = uint64(defd);
4351 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%"PRId64, uint64(defd) );
4353 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
4355 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
4357 tk->f_timecodescale = float( ttcs );
4358 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
4360 else if( MKV_IS_ID( l, KaxTrackName ) )
4362 KaxTrackName &tname = *(KaxTrackName*)l;
4364 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
4365 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
4367 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
4369 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
4371 if ( tk->fmt.psz_language != NULL )
4372 free( tk->fmt.psz_language );
4373 tk->fmt.psz_language = strdup( string( lang ).c_str() );
4374 msg_Dbg( &sys.demuxer,
4375 "| | | + Track Language=`%s'", tk->fmt.psz_language );
4377 else if( MKV_IS_ID( l, KaxCodecID ) )
4379 KaxCodecID &codecid = *(KaxCodecID*)l;
4381 tk->psz_codec = strdup( string( codecid ).c_str() );
4382 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
4384 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
4386 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
4388 tk->i_extra_data = cpriv.GetSize();
4389 if( tk->i_extra_data > 0 )
4391 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
4392 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
4394 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
4396 else if( MKV_IS_ID( l, KaxCodecName ) )
4398 KaxCodecName &cname = *(KaxCodecName*)l;
4400 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
4401 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
4403 else if( MKV_IS_ID( l, KaxContentEncodings ) )
4405 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
4406 MkvTree( sys.demuxer, 3, "Content Encodings" );
4407 if ( cencs->ListSize() > 1 )
4409 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
4412 for( j = 0; j < cencs->ListSize(); j++ )
4414 EbmlElement *l2 = (*cencs)[j];
4415 if( MKV_IS_ID( l2, KaxContentEncoding ) )
4417 MkvTree( sys.demuxer, 4, "Content Encoding" );
4418 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
4419 for( k = 0; k < cenc->ListSize(); k++ )
4421 EbmlElement *l3 = (*cenc)[k];
4422 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
4424 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
4425 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
4427 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
4429 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
4430 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
4432 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
4434 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
4435 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
4437 else if( MKV_IS_ID( l3, KaxContentCompression ) )
4439 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
4440 MkvTree( sys.demuxer, 5, "Content Compression" );
4441 for( n = 0; n < compr->ListSize(); n++ )
4443 EbmlElement *l4 = (*compr)[n];
4444 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
4446 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
4447 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
4448 tk->i_compression_type = uint32( compalg );
4449 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
4450 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
4452 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
4456 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
4458 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
4462 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
4468 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
4474 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
4478 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
4480 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
4482 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
4483 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
4485 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
4487 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
4489 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
4490 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
4492 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
4494 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
4496 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
4497 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
4499 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
4501 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
4503 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
4505 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
4507 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
4509 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
4511 else if( MKV_IS_ID( l, KaxTrackVideo ) )
4513 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
4515 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
4516 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
4518 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
4521 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
4522 tk->fmt.video.i_frame_rate = 1000000;
4524 for( j = 0; j < tkv->ListSize(); j++ )
4526 EbmlElement *l = (*tkv)[j];
4527 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
4529 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
4531 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
4533 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
4535 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
4537 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
4540 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
4542 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
4544 tk->fmt.video.i_width += uint16( vwidth );
4545 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
4547 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
4549 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
4551 tk->fmt.video.i_height += uint16( vheight );
4552 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
4554 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
4556 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
4558 i_display_width = uint16( vwidth );
4559 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
4561 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
4563 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
4565 i_display_height = uint16( vheight );
4566 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
4568 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
4570 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
4572 i_crop_bottom = uint16( cropval );
4573 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
4575 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
4577 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
4579 i_crop_top = uint16( cropval );
4580 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
4582 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
4584 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
4586 i_crop_right = uint16( cropval );
4587 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
4589 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
4591 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
4593 i_crop_left = uint16( cropval );
4594 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
4596 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
4598 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
4600 tk->f_fps = float( vfps );
4601 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
4603 else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
4605 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
4607 i_display_unit = uint8( vdmode );
4608 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
4609 uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
4611 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
4613 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
4615 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
4617 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
4619 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
4621 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
4625 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
4628 if( i_display_height && i_display_width )
4629 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * i_display_width / i_display_height;
4630 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
4632 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
4633 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
4634 tk->fmt.video.i_x_offset = i_crop_left;
4635 tk->fmt.video.i_y_offset = i_crop_top;
4636 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
4637 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
4639 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
4640 we do not support this atm */
4642 else if( MKV_IS_ID( l, KaxTrackAudio ) )
4644 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
4647 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
4649 for( j = 0; j < tka->ListSize(); j++ )
4651 EbmlElement *l = (*tka)[j];
4653 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
4655 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
4657 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
4658 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
4660 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
4662 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
4664 tk->fmt.audio.i_rate = (int)float( afreq );
4665 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
4667 else if( MKV_IS_ID( l, KaxAudioChannels ) )
4669 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
4671 tk->fmt.audio.i_channels = uint8( achan );
4672 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
4674 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
4676 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
4678 tk->fmt.audio.i_bitspersample = uint8( abits );
4679 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
4683 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
4689 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
4690 typeid(*l).name() );
4696 tracks.push_back( tk );
4700 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
4705 /*****************************************************************************
4707 *****************************************************************************/
4708 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
4712 int i_upper_level = 0;
4714 msg_Dbg( &sys.demuxer, "| + Tracks" );
4716 /* Master elements */
4717 tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
4719 for( i = 0; i < tracks->ListSize(); i++ )
4721 EbmlElement *l = (*tracks)[i];
4723 if( MKV_IS_ID( l, KaxTrackEntry ) )
4725 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
4729 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4734 /*****************************************************************************
4736 *****************************************************************************/
4737 void matroska_segment_c::ParseInfo( KaxInfo *info )
4742 int i_upper_level = 0;
4744 msg_Dbg( &sys.demuxer, "| + Information" );
4746 /* Master elements */
4747 m = static_cast<EbmlMaster *>(info);
4748 m->Read( es, info->Generic().Context, i_upper_level, el, true );
4750 for( i = 0; i < m->ListSize(); i++ )
4752 EbmlElement *l = (*m)[i];
4754 if( MKV_IS_ID( l, KaxSegmentUID ) )
4756 if ( p_segment_uid == NULL )
4757 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
4759 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
4761 else if( MKV_IS_ID( l, KaxPrevUID ) )
4763 if ( p_prev_segment_uid == NULL )
4764 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
4766 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
4768 else if( MKV_IS_ID( l, KaxNextUID ) )
4770 if ( p_next_segment_uid == NULL )
4771 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
4773 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
4775 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
4777 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
4779 i_timescale = uint64(tcs);
4781 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%"PRId64,
4784 else if( MKV_IS_ID( l, KaxDuration ) )
4786 KaxDuration &dur = *(KaxDuration*)l;
4788 i_duration = mtime_t( double( dur ) );
4790 msg_Dbg( &sys.demuxer, "| | + Duration=%"PRId64,
4793 else if( MKV_IS_ID( l, KaxMuxingApp ) )
4795 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
4797 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
4799 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
4800 psz_muxing_application );
4802 else if( MKV_IS_ID( l, KaxWritingApp ) )
4804 KaxWritingApp &wapp = *(KaxWritingApp*)l;
4806 psz_writing_application = ToUTF8( UTFstring( wapp ) );
4808 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
4809 psz_writing_application );
4811 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
4813 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
4815 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
4817 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
4818 psz_segment_filename );
4820 else if( MKV_IS_ID( l, KaxTitle ) )
4822 KaxTitle &title = *(KaxTitle*)l;
4824 psz_title = ToUTF8( UTFstring( title ) );
4826 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
4828 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
4830 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
4832 families.push_back( new KaxSegmentFamily(*uid) );
4834 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
4836 #if defined( HAVE_GMTIME_R ) && !defined( __APPLE__ )
4837 else if( MKV_IS_ID( l, KaxDateUTC ) )
4839 KaxDateUTC &date = *(KaxDateUTC*)l;
4844 i_date = date.GetEpochDate();
4845 memset( buffer, 0, 256 );
4846 if( gmtime_r( &i_date, &tmres ) &&
4847 asctime_r( &tmres, buffer ) )
4849 buffer[strlen( buffer)-1]= '\0';
4850 psz_date_utc = strdup( buffer );
4851 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
4855 #if LIBMATROSKA_VERSION >= 0x000704
4856 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
4858 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
4859 chapter_translation_c *p_translate = new chapter_translation_c();
4861 p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
4862 for( j = 0; j < p_trans->ListSize(); j++ )
4864 EbmlElement *l = (*p_trans)[j];
4866 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
4868 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
4870 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
4872 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
4874 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
4876 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
4880 translations.push_back( p_translate );
4885 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4889 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
4890 i_duration = mtime_t(f_dur);
4894 /*****************************************************************************
4896 *****************************************************************************/
4897 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
4901 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
4902 for( i = 0; i < ca->ListSize(); i++ )
4904 EbmlElement *l = (*ca)[i];
4906 if( MKV_IS_ID( l, KaxChapterUID ) )
4908 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
4909 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
4911 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
4913 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
4914 chapters.b_display_seekpoint = uint8( flag ) == 0;
4916 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
4918 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
4920 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
4921 chapters.i_start_time = uint64( start ) / INT64_C(1000);
4923 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
4925 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
4927 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
4928 chapters.i_end_time = uint64( end ) / INT64_C(1000);
4930 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
4932 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
4934 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
4936 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
4937 for( j = 0; j < cd->ListSize(); j++ )
4939 EbmlElement *l= (*cd)[j];
4941 if( MKV_IS_ID( l, KaxChapterString ) )
4945 KaxChapterString &name =*(KaxChapterString*)l;
4946 for (k = 0; k < i_level; k++)
4947 chapters.psz_name += '+';
4948 chapters.psz_name += ' ';
4949 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
4950 chapters.psz_name += psz_tmp_utf8;
4951 chapters.b_user_display = true;
4953 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
4954 free( psz_tmp_utf8 );
4956 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
4958 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
4959 const char *psz = string( lang ).c_str();
4961 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
4963 else if( MKV_IS_ID( l, KaxChapterCountry ) )
4965 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
4966 const char *psz = string( ct ).c_str();
4968 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
4972 else if( MKV_IS_ID( l, KaxChapterProcess ) )
4974 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
4976 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
4977 chapter_codec_cmds_c *p_ccodec = NULL;
4979 for( j = 0; j < cp->ListSize(); j++ )
4981 EbmlElement *k= (*cp)[j];
4983 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
4985 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
4986 if ( uint32(*p_codec_id) == 0 )
4987 p_ccodec = new matroska_script_codec_c( sys );
4988 else if ( uint32(*p_codec_id) == 1 )
4989 p_ccodec = new dvd_chapter_codec_c( sys );
4994 if ( p_ccodec != NULL )
4996 for( j = 0; j < cp->ListSize(); j++ )
4998 EbmlElement *k= (*cp)[j];
5000 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
5002 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
5003 p_ccodec->SetPrivate( *p_private );
5005 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
5007 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
5010 chapters.codecs.push_back( p_ccodec );
5013 else if( MKV_IS_ID( l, KaxChapterAtom ) )
5015 chapter_item_c *new_sub_chapter = new chapter_item_c();
5016 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
5017 new_sub_chapter->psz_parent = &chapters;
5018 chapters.sub_chapters.push_back( new_sub_chapter );
5023 /*****************************************************************************
5025 *****************************************************************************/
5026 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
5029 int i_upper_level = 0;
5031 attachments->Read( es, attachments->Generic().Context, i_upper_level, el, true );
5033 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
5035 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
5037 std::string psz_mime_type = GetChild<KaxMimeType>( *attachedFile );
5038 KaxFileName &file_name = GetChild<KaxFileName>( *attachedFile );
5039 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
5041 attachment_c *new_attachment = new attachment_c();
5043 if( new_attachment )
5045 new_attachment->psz_file_name = ToUTF8( UTFstring( file_name ) );
5046 new_attachment->psz_mime_type = psz_mime_type;
5047 new_attachment->i_size = img_data.GetSize();
5048 new_attachment->p_data = malloc( img_data.GetSize() );
5050 if( new_attachment->p_data )
5052 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
5053 sys.stored_attachments.push_back( new_attachment );
5057 delete new_attachment;
5061 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
5065 /*****************************************************************************
5067 *****************************************************************************/
5068 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
5072 int i_upper_level = 0;
5075 /* Master elements */
5076 chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
5078 for( i = 0; i < chapters->ListSize(); i++ )
5080 EbmlElement *l = (*chapters)[i];
5082 if( MKV_IS_ID( l, KaxEditionEntry ) )
5084 chapter_edition_c *p_edition = new chapter_edition_c();
5086 EbmlMaster *E = static_cast<EbmlMaster *>(l );
5088 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
5089 for( j = 0; j < E->ListSize(); j++ )
5091 EbmlElement *l = (*E)[j];
5093 if( MKV_IS_ID( l, KaxChapterAtom ) )
5095 chapter_item_c *new_sub_chapter = new chapter_item_c();
5096 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
5097 p_edition->sub_chapters.push_back( new_sub_chapter );
5099 else if( MKV_IS_ID( l, KaxEditionUID ) )
5101 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
5103 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
5105 p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
5107 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
5109 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
5110 i_default_edition = stored_editions.size();
5114 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
5117 stored_editions.push_back( p_edition );
5121 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
5125 for( i = 0; i < stored_editions.size(); i++ )
5127 stored_editions[i]->RefreshChapters( );
5130 if ( stored_editions.size() != 0 && stored_editions[i_default_edition]->b_ordered )
5132 /* update the duration of the segment according to the sum of all sub chapters */
5133 i_dur = stored_editions[i_default_edition]->Duration() / INT64_C(1000);
5139 void matroska_segment_c::ParseCluster( )
5144 int i_upper_level = 0;
5146 /* Master elements */
5147 m = static_cast<EbmlMaster *>( cluster );
5148 m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
5150 for( i = 0; i < m->ListSize(); i++ )
5152 EbmlElement *l = (*m)[i];
5154 if( MKV_IS_ID( l, KaxClusterTimecode ) )
5156 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
5158 cluster->InitTimecode( uint64( ctc ), i_timescale );
5163 i_start_time = cluster->GlobalTimecode() / 1000;
5166 /*****************************************************************************
5167 * InformationCreate:
5168 *****************************************************************************/
5169 void matroska_segment_c::InformationCreate( )
5171 sys.meta = vlc_meta_New();
5175 vlc_meta_SetTitle( sys.meta, psz_title );
5179 vlc_meta_SetDate( sys.meta, psz_date_utc );
5182 if( psz_segment_filename )
5184 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5186 if( psz_muxing_application )
5188 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5190 if( psz_writing_application )
5192 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5195 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
5197 // mkv_track_t *tk = tracks[i_track];
5198 // vlc_meta_t *mtk = vlc_meta_New();
5199 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
5203 if( i_tags_position >= 0 )
5207 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
5216 /*****************************************************************************
5218 *****************************************************************************/
5220 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
5222 #define idx p_indexes[i_index]
5224 idx.i_block_number= -1;
5225 idx.i_position = cluster->GetElementPosition();
5230 if( i_index >= i_index_max )
5232 i_index_max += 1024;
5233 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
5238 void chapter_edition_c::RefreshChapters( )
5240 chapter_item_c::RefreshChapters( b_ordered, -1 );
5241 b_display_seekpoint = false;
5244 int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_time )
5246 int64_t i_user_time = i_prev_user_time;
5248 // first the sub-chapters, and then ourself
5249 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
5250 while ( index != sub_chapters.end() )
5252 i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
5258 // the ordered chapters always start at zero
5259 if ( i_prev_user_time == -1 )
5261 if ( i_user_time == -1 )
5263 i_prev_user_time = 0;
5266 i_user_start_time = i_prev_user_time;
5267 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
5269 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
5273 i_user_end_time = i_user_time;
5278 if ( sub_chapters.begin() != sub_chapters.end() )
5279 std::sort( sub_chapters.begin(), sub_chapters.end(), chapter_item_c::CompareTimecode );
5280 i_user_start_time = i_start_time;
5281 if ( i_end_time != -1 )
5282 i_user_end_time = i_end_time;
5283 else if ( i_user_time != -1 )
5284 i_user_end_time = i_user_time;
5286 i_user_end_time = i_user_start_time;
5289 return i_user_end_time;
5292 mtime_t chapter_edition_c::Duration() const
5294 mtime_t i_result = 0;
5296 if ( sub_chapters.size() )
5298 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
5300 i_result = (*index)->i_user_end_time;
5306 chapter_item_c * chapter_edition_c::FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current )
5310 bool b_found_current = false;
5311 return chapter_item_c::FindTimecode( i_timecode, p_current, b_found_current );
5314 chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode, const chapter_item_c * p_current, bool & b_found )
5316 chapter_item_c *psz_result = NULL;
5318 if ( p_current == this )
5321 if ( i_user_timecode >= i_user_start_time &&
5322 ( i_user_timecode < i_user_end_time ||
5323 ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
5325 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
5326 while ( index != sub_chapters.end() && ((p_current == NULL && psz_result == NULL) || (p_current != NULL && (!b_found || psz_result == NULL))))
5328 psz_result = (*index)->FindTimecode( i_user_timecode, p_current, b_found );
5332 if ( psz_result == NULL )
5339 bool chapter_item_c::ParentOf( const chapter_item_c & item ) const
5341 if ( &item == this )
5344 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.begin();
5345 while ( index != sub_chapters.end() )
5347 if ( (*index)->ParentOf( item ) )
5355 void demux_sys_t::PreloadFamily( const matroska_segment_c & of_segment )
5357 for (size_t i=0; i<opened_segments.size(); i++)
5359 opened_segments[i]->PreloadFamily( of_segment );
5362 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
5367 for (size_t i=0; i<families.size(); i++)
5369 for (size_t j=0; j<of_segment.families.size(); j++)
5371 if ( *(families[i]) == *(of_segment.families[j]) )
5379 // preload all the linked segments for all preloaded segments
5380 void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
5382 size_t i_preloaded, i, j;
5383 virtual_segment_c *p_seg;
5385 p_current_segment = VirtualFromSegments( p_segment );
5387 used_segments.push_back( p_current_segment );
5389 // create all the other virtual segments of the family
5392 for ( i=0; i< opened_segments.size(); i++ )
5394 if ( opened_segments[i]->b_preloaded && !IsUsedSegment( *opened_segments[i] ) )
5396 p_seg = VirtualFromSegments( opened_segments[i] );
5397 used_segments.push_back( p_seg );
5401 } while ( i_preloaded ); // worst case: will stop when all segments are found as family related
5403 // publish all editions of all usable segment
5404 for ( i=0; i< used_segments.size(); i++ )
5406 p_seg = used_segments[i];
5407 if ( p_seg->p_editions != NULL )
5409 std::string sz_name;
5410 input_title_t *p_title = vlc_input_title_New();
5411 p_seg->i_sys_title = i;
5414 // TODO use a name for each edition, let the TITLE deal with a codec name
5415 for ( j=0; j<p_seg->p_editions->size(); j++ )
5417 if ( p_title->psz_name == NULL )
5419 sz_name = (*p_seg->p_editions)[j]->GetMainName();
5420 if ( sz_name != "" )
5421 p_title->psz_name = strdup( sz_name.c_str() );
5424 chapter_edition_c *p_edition = (*p_seg->p_editions)[j];
5427 p_edition->PublishChapters( *p_title, i_chapters, 0 );
5430 // create a name if there is none
5431 if ( p_title->psz_name == NULL )
5433 sz_name = N_("Segment");
5435 sprintf( psz_str, " %d", (int)i );
5437 p_title->psz_name = strdup( sz_name.c_str() );
5440 titles.push_back( p_title );
5444 // TODO decide which segment should be first used (VMG for DVD)
5447 bool demux_sys_t::IsUsedSegment( matroska_segment_c &segment ) const
5449 for ( size_t i=0; i< used_segments.size(); i++ )
5451 if ( used_segments[i]->FindUID( *segment.p_segment_uid ) )
5457 virtual_segment_c *demux_sys_t::VirtualFromSegments( matroska_segment_c *p_segment ) const
5459 size_t i_preloaded, i;
5461 virtual_segment_c *p_result = new virtual_segment_c( p_segment );
5463 // fill our current virtual segment with all hard linked segments
5466 for ( i=0; i< opened_segments.size(); i++ )
5468 i_preloaded += p_result->AddSegment( opened_segments[i] );
5470 } while ( i_preloaded ); // worst case: will stop when all segments are found as linked
5474 p_result->PreloadLinked( );
5476 p_result->PrepareChapters( );
5481 bool demux_sys_t::PreparePlayback( virtual_segment_c *p_new_segment )
5483 if ( p_new_segment != NULL && p_new_segment != p_current_segment )
5485 if ( p_current_segment != NULL && p_current_segment->Segment() != NULL )
5486 p_current_segment->Segment()->UnSelect();
5488 p_current_segment = p_new_segment;
5489 i_current_title = p_new_segment->i_sys_title;
5492 p_current_segment->LoadCues();
5493 f_duration = p_current_segment->Duration();
5495 /* add information */
5496 p_current_segment->Segment()->InformationCreate( );
5498 p_current_segment->Segment()->Select( 0 );
5503 void demux_sys_t::JumpTo( virtual_segment_c & vsegment, chapter_item_c * p_chapter )
5505 // if the segment is not part of the current segment, select the new one
5506 if ( &vsegment != p_current_segment )
5508 PreparePlayback( &vsegment );
5511 if ( p_chapter != NULL )
5513 if ( !p_chapter->Enter( true ) )
5515 // jump to the location in the found segment
5516 vsegment.Seek( demuxer, p_chapter->i_user_start_time, -1, p_chapter );
5522 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
5526 if ( p_item_a == NULL || p_item_b == NULL )
5529 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
5530 if ( p_item_b->p_prev_segment_uid != NULL
5531 && *p_tmp == *p_item_b->p_prev_segment_uid )
5534 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
5538 if ( p_item_b->p_segment_uid != NULL
5539 && *p_tmp == *p_item_b->p_segment_uid )
5542 if ( p_item_b->p_prev_segment_uid != NULL
5543 && *p_tmp == *p_item_b->p_prev_segment_uid )
5549 bool matroska_segment_c::Preload( )
5554 EbmlElement *el = NULL;
5556 ep->Reset( &sys.demuxer );
5558 while( ( el = ep->Get() ) != NULL )
5560 if( MKV_IS_ID( el, KaxInfo ) )
5562 ParseInfo( static_cast<KaxInfo*>( el ) );
5564 else if( MKV_IS_ID( el, KaxTracks ) )
5566 ParseTracks( static_cast<KaxTracks*>( el ) );
5567 if ( tracks.size() == 0 )
5569 msg_Err( &sys.demuxer, "No tracks supported" );
5573 else if( MKV_IS_ID( el, KaxSeekHead ) )
5575 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
5577 else if( MKV_IS_ID( el, KaxCues ) )
5579 msg_Dbg( &sys.demuxer, "| + Cues" );
5581 else if( MKV_IS_ID( el, KaxCluster ) )
5583 msg_Dbg( &sys.demuxer, "| + Cluster" );
5585 cluster = (KaxCluster*)el;
5587 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
5591 /* stop parsing the stream */
5594 else if( MKV_IS_ID( el, KaxAttachments ) )
5596 msg_Dbg( &sys.demuxer, "| + Attachments" );
5597 ParseAttachments( static_cast<KaxAttachments*>( el ) );
5599 else if( MKV_IS_ID( el, KaxChapters ) )
5601 msg_Dbg( &sys.demuxer, "| + Chapters" );
5602 ParseChapters( static_cast<KaxChapters*>( el ) );
5604 else if( MKV_IS_ID( el, KaxTag ) )
5606 msg_Dbg( &sys.demuxer, "| + Tags FIXME TODO" );
5610 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid(*el).name() );
5619 matroska_segment_c *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
5621 for (size_t i=0; i<opened_segments.size(); i++)
5623 if ( *opened_segments[i]->p_segment_uid == uid )
5624 return opened_segments[i];
5629 chapter_item_c *demux_sys_t::BrowseCodecPrivate( unsigned int codec_id,
5630 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
5631 const void *p_cookie,
5632 size_t i_cookie_size,
5633 virtual_segment_c * &p_segment_found )
5635 chapter_item_c *p_result = NULL;
5636 for (size_t i=0; i<used_segments.size(); i++)
5638 p_result = used_segments[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
5639 if ( p_result != NULL )
5641 p_segment_found = used_segments[i];
5648 chapter_item_c *demux_sys_t::FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found )
5650 chapter_item_c *p_result = NULL;
5651 for (size_t i=0; i<used_segments.size(); i++)
5653 p_result = used_segments[i]->FindChapter( i_find_uid );
5654 if ( p_result != NULL )
5656 p_segment_found = used_segments[i];
5663 void virtual_segment_c::Sort()
5665 // keep the current segment index
5666 matroska_segment_c *p_segment = linked_segments[i_current_segment];
5668 std::sort( linked_segments.begin(), linked_segments.end(), matroska_segment_c::CompareSegmentUIDs );
5670 for ( i_current_segment=0; i_current_segment<linked_segments.size(); i_current_segment++)
5671 if ( linked_segments[i_current_segment] == p_segment )
5675 size_t virtual_segment_c::AddSegment( matroska_segment_c *p_segment )
5678 // check if it's not already in here
5679 for ( i=0; i<linked_segments.size(); i++ )
5681 if ( linked_segments[i]->p_segment_uid != NULL
5682 && p_segment->p_segment_uid != NULL
5683 && *p_segment->p_segment_uid == *linked_segments[i]->p_segment_uid )
5687 // find possible mates
5688 for ( i=0; i<linked_uids.size(); i++ )
5690 if ( (p_segment->p_segment_uid != NULL && *p_segment->p_segment_uid == linked_uids[i])
5691 || (p_segment->p_prev_segment_uid != NULL && *p_segment->p_prev_segment_uid == linked_uids[i])
5692 || (p_segment->p_next_segment_uid !=NULL && *p_segment->p_next_segment_uid == linked_uids[i]) )
5694 linked_segments.push_back( p_segment );
5696 AppendUID( p_segment->p_prev_segment_uid );
5697 AppendUID( p_segment->p_next_segment_uid );
5705 void virtual_segment_c::PreloadLinked( )
5707 for ( size_t i=0; i<linked_segments.size(); i++ )
5709 linked_segments[i]->Preload( );
5711 i_current_edition = linked_segments[0]->i_default_edition;
5714 mtime_t virtual_segment_c::Duration() const
5717 if ( linked_segments.size() == 0 )
5720 matroska_segment_c *p_last_segment = linked_segments[linked_segments.size()-1];
5721 // p_last_segment->ParseCluster( );
5723 i_duration = p_last_segment->i_start_time / 1000 + p_last_segment->i_duration;
5728 void virtual_segment_c::LoadCues( )
5730 for ( size_t i=0; i<linked_segments.size(); i++ )
5732 linked_segments[i]->LoadCues();
5736 void virtual_segment_c::AppendUID( const EbmlBinary * p_UID )
5738 if ( p_UID == NULL )
5740 if ( p_UID->GetBuffer() == NULL )
5743 for (size_t i=0; i<linked_uids.size(); i++)
5745 if ( *p_UID == linked_uids[i] )
5748 linked_uids.push_back( *(KaxSegmentUID*)(p_UID) );
5751 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset )
5754 #if LIBMATROSKA_VERSION >= 0x000800
5755 KaxSimpleBlock *simpleblock;
5757 int i_track_skipping;
5758 int64_t i_block_duration;
5759 int64_t i_block_ref1;
5760 int64_t i_block_ref2;
5762 int64_t i_seek_position = i_start_pos;
5763 int64_t i_seek_time = i_start_time;
5769 for( ; i_idx < i_index; i_idx++ )
5771 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
5782 i_seek_position = p_indexes[i_idx].i_position;
5783 i_seek_time = p_indexes[i_idx].i_time;
5786 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
5787 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
5789 es.I_O().setFilePointer( i_seek_position, seek_beginning );
5792 ep = new EbmlParser( &es, segment, &sys.demuxer );
5795 sys.i_start_pts = i_date;
5797 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
5799 /* now parse until key frame */
5800 i_track_skipping = 0;
5801 for( i_track = 0; i_track < tracks.size(); i_track++ )
5803 if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
5805 tracks[i_track]->b_search_keyframe = true;
5808 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, i_date );
5812 while( i_track_skipping > 0 )
5814 #if LIBMATROSKA_VERSION >= 0x000800
5815 if( BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
5817 if( BlockGet( block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
5820 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
5826 for( i_track = 0; i_track < tracks.size(); i_track++ )
5828 #if LIBMATROSKA_VERSION >= 0x000800
5829 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
5830 (block && tracks[i_track]->i_number == block->TrackNum()) )
5832 if( tracks[i_track]->i_number == block->TrackNum() )
5839 #if LIBMATROSKA_VERSION >= 0x000800
5841 sys.i_pts = (sys.i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
5844 sys.i_pts = (sys.i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
5846 if( i_track < tracks.size() )
5848 if( sys.i_pts >= sys.i_start_pts )
5850 cluster = static_cast<KaxCluster*>(ep->UnGet( i_block_pos, i_cluster_pos ));
5851 i_track_skipping = 0;
5853 else if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
5855 if( i_block_ref1 == 0 && tracks[i_track]->b_search_keyframe )
5857 tracks[i_track]->b_search_keyframe = false;
5860 if( !tracks[i_track]->b_search_keyframe )
5862 #if LIBMATROSKA_VERSION >= 0x000800
5863 BlockDecode( &sys.demuxer, block, simpleblock, sys.i_pts, 0, i_block_ref1 >= 0 || i_block_ref2 > 0 );
5865 BlockDecode( &sys.demuxer, block, sys.i_pts, 0, i_block_ref1 >= 0 || i_block_ref2 > 0 );
5875 void virtual_segment_c::Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter )
5877 demux_sys_t *p_sys = demuxer.p_sys;
5880 // find the actual time for an ordered edition
5881 if ( psz_chapter == NULL )
5883 if ( Edition() && Edition()->b_ordered )
5885 /* 1st, we need to know in which chapter we are */
5886 psz_chapter = (*p_editions)[i_current_edition]->FindTimecode( i_date, psz_current_chapter );
5890 if ( psz_chapter != NULL )
5892 psz_current_chapter = psz_chapter;
5893 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
5894 if ( psz_chapter->i_seekpoint_num > 0 )
5896 demuxer.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
5897 demuxer.info.i_title = p_sys->i_current_title = i_sys_title;
5898 demuxer.info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
5902 // find the best matching segment
5903 for ( i=0; i<linked_segments.size(); i++ )
5905 if ( i_date < linked_segments[i]->i_start_time )
5912 if ( i_current_segment != i )
5914 linked_segments[i_current_segment]->UnSelect();
5915 linked_segments[i]->Select( i_date );
5916 i_current_segment = i;
5919 linked_segments[i]->Seek( i_date, i_time_offset );
5922 void chapter_codec_cmds_c::AddCommand( const KaxChapterProcessCommand & command )
5926 uint32 codec_time = uint32(-1);
5927 for( i = 0; i < command.ListSize(); i++ )
5929 const EbmlElement *k = command[i];
5931 if( MKV_IS_ID( k, KaxChapterProcessTime ) )
5933 codec_time = uint32( *static_cast<const KaxChapterProcessTime*>( k ) );
5938 for( i = 0; i < command.ListSize(); i++ )
5940 const EbmlElement *k = command[i];
5942 if( MKV_IS_ID( k, KaxChapterProcessData ) )
5944 KaxChapterProcessData *p_data = new KaxChapterProcessData( *static_cast<const KaxChapterProcessData*>( k ) );
5945 switch ( codec_time )
5948 during_cmds.push_back( p_data );
5951 enter_cmds.push_back( p_data );
5954 leave_cmds.push_back( p_data );
5963 bool chapter_item_c::Enter( bool b_do_subs )
5965 bool f_result = false;
5966 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
5967 while ( index != codecs.end() )
5969 f_result |= (*index)->Enter();
5976 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
5977 while ( index_ != sub_chapters.end() )
5979 f_result |= (*index_)->Enter( true );
5986 bool chapter_item_c::Leave( bool b_do_subs )
5988 bool f_result = false;
5989 b_is_leaving = true;
5990 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
5991 while ( index != codecs.end() )
5993 f_result |= (*index)->Leave();
6000 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
6001 while ( index_ != sub_chapters.end() )
6003 f_result |= (*index_)->Leave( true );
6007 b_is_leaving = false;
6011 bool chapter_item_c::EnterAndLeave( chapter_item_c *p_item, bool b_final_enter )
6013 chapter_item_c *p_common_parent = p_item;
6015 // leave, up to a common parent
6016 while ( p_common_parent != NULL && !p_common_parent->ParentOf( *this ) )
6018 if ( !p_common_parent->b_is_leaving && p_common_parent->Leave( false ) )
6020 p_common_parent = p_common_parent->psz_parent;
6023 // enter from the parent to <this>
6024 if ( p_common_parent != NULL )
6028 if ( p_common_parent == this )
6029 return Enter( true );
6031 for ( size_t i = 0; i<p_common_parent->sub_chapters.size(); i++ )
6033 if ( p_common_parent->sub_chapters[i]->ParentOf( *this ) )
6035 p_common_parent = p_common_parent->sub_chapters[i];
6036 if ( p_common_parent != this )
6037 if ( p_common_parent->Enter( false ) )
6046 if ( b_final_enter )
6047 return Enter( true );
6052 bool dvd_chapter_codec_c::Enter()
6054 bool f_result = false;
6055 std::vector<KaxChapterProcessData*>::iterator index = enter_cmds.begin();
6056 while ( index != enter_cmds.end() )
6058 if ( (*index)->GetSize() )
6060 binary *p_data = (*index)->GetBuffer();
6061 size_t i_size = *p_data++;
6062 // avoid reading too much from the buffer
6063 i_size = __MIN( i_size, ((*index)->GetSize() - 1) >> 3 );
6064 for ( ; i_size > 0; i_size--, p_data += 8 )
6066 msg_Dbg( &sys.demuxer, "Matroska DVD enter command" );
6067 f_result |= sys.dvd_interpretor.Interpret( p_data );
6075 bool dvd_chapter_codec_c::Leave()
6077 bool f_result = false;
6078 std::vector<KaxChapterProcessData*>::iterator index = leave_cmds.begin();
6079 while ( index != leave_cmds.end() )
6081 if ( (*index)->GetSize() )
6083 binary *p_data = (*index)->GetBuffer();
6084 size_t i_size = *p_data++;
6085 // avoid reading too much from the buffer
6086 i_size = __MIN( i_size, ((*index)->GetSize() - 1) >> 3 );
6087 for ( ; i_size > 0; i_size--, p_data += 8 )
6089 msg_Dbg( &sys.demuxer, "Matroska DVD leave command" );
6090 f_result |= sys.dvd_interpretor.Interpret( p_data );
6098 // see http://www.dvd-replica.com/DVD/vmcmdset.php for a description of DVD commands
6099 bool dvd_command_interpretor_c::Interpret( const binary * p_command, size_t i_size )
6104 virtual_segment_c *p_segment = NULL;
6105 chapter_item_c *p_chapter = NULL;
6106 bool f_result = false;
6107 uint16 i_command = ( p_command[0] << 8 ) + p_command[1];
6109 // handle register tests if there are some
6110 if ( (i_command & 0xF0) != 0 )
6112 bool b_test_positive = true;//(i_command & CMD_DVD_IF_NOT) == 0;
6113 bool b_test_value = (i_command & CMD_DVD_TEST_VALUE) != 0;
6114 uint8 i_test = i_command & 0x70;
6117 // see http://dvd.sourceforge.net/dvdinfo/vmi.html
6120 switch ( i_command >> 12 )
6123 i_cr1 = p_command[3];
6124 i_cr2 = (p_command[4] << 8) + p_command[5];
6129 i_cr1 = p_command[6];
6130 i_cr2 = p_command[7];
6131 b_test_value = false;
6135 if ( ((p_command[1] >> 4) & 0x7) == 0)
6137 i_cr1 = p_command[2];
6138 i_cr2 = (p_command[6] << 8) + p_command[7];
6142 i_cr1 = p_command[2];
6143 i_cr2 = (p_command[6] << 8) + p_command[7];
6151 i_value = GetPRM( i_cr2 );
6155 case CMD_DVD_IF_GPREG_EQUAL:
6157 msg_Dbg( &sys.demuxer, "IF %s EQUALS %s", GetRegTypeName( false, i_cr1 ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6158 if (!( GetPRM( i_cr1 ) == i_value ))
6160 b_test_positive = false;
6163 case CMD_DVD_IF_GPREG_NOT_EQUAL:
6165 msg_Dbg( &sys.demuxer, "IF %s NOT EQUALS %s", GetRegTypeName( false, i_cr1 ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6166 if (!( GetPRM( i_cr1 ) != i_value ))
6168 b_test_positive = false;
6171 case CMD_DVD_IF_GPREG_INF:
6173 msg_Dbg( &sys.demuxer, "IF %s < %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6174 if (!( GetPRM( i_cr1 ) < i_value ))
6176 b_test_positive = false;
6179 case CMD_DVD_IF_GPREG_INF_EQUAL:
6180 // if inferior or equal
6181 msg_Dbg( &sys.demuxer, "IF %s < %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6182 if (!( GetPRM( i_cr1 ) <= i_value ))
6184 b_test_positive = false;
6187 case CMD_DVD_IF_GPREG_AND:
6189 msg_Dbg( &sys.demuxer, "IF %s & %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6190 if (!( GetPRM( i_cr1 ) & i_value ))
6192 b_test_positive = false;
6195 case CMD_DVD_IF_GPREG_SUP:
6197 msg_Dbg( &sys.demuxer, "IF %s >= %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6198 if (!( GetPRM( i_cr1 ) > i_value ))
6200 b_test_positive = false;
6203 case CMD_DVD_IF_GPREG_SUP_EQUAL:
6204 // if superior or equal
6205 msg_Dbg( &sys.demuxer, "IF %s >= %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6206 if (!( GetPRM( i_cr1 ) >= i_value ))
6208 b_test_positive = false;
6213 if ( !b_test_positive )
6217 // strip the test command
6218 i_command &= 0xFF0F;
6220 switch ( i_command )
6225 msg_Dbg( &sys.demuxer, "NOP" );
6230 msg_Dbg( &sys.demuxer, "Break" );
6234 case CMD_DVD_JUMP_TT:
6236 uint8 i_title = p_command[5];
6237 msg_Dbg( &sys.demuxer, "JumpTT %d", i_title );
6239 // find in the ChapProcessPrivate matching this Title level
6240 p_chapter = sys.BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title), p_segment );
6241 if ( p_segment != NULL )
6243 sys.JumpTo( *p_segment, p_chapter );
6249 case CMD_DVD_CALLSS_VTSM1:
6251 msg_Dbg( &sys.demuxer, "CallSS" );
6253 switch( (p_command[6] & 0xC0) >> 6 ) {
6255 p_type = p_command[5] & 0x0F;
6259 msg_Dbg( &sys.demuxer, "CallSS PGC (rsm_cell %x)", p_command[4]);
6262 msg_Dbg( &sys.demuxer, "CallSS Title Entry (rsm_cell %x)", p_command[4]);
6265 msg_Dbg( &sys.demuxer, "CallSS Root Menu (rsm_cell %x)", p_command[4]);
6268 msg_Dbg( &sys.demuxer, "CallSS Subpicture Menu (rsm_cell %x)", p_command[4]);
6271 msg_Dbg( &sys.demuxer, "CallSS Audio Menu (rsm_cell %x)", p_command[4]);
6274 msg_Dbg( &sys.demuxer, "CallSS Angle Menu (rsm_cell %x)", p_command[4]);
6277 msg_Dbg( &sys.demuxer, "CallSS Chapter Menu (rsm_cell %x)", p_command[4]);
6280 msg_Dbg( &sys.demuxer, "CallSS <unknown> (rsm_cell %x)", p_command[4]);
6283 p_chapter = sys.BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1, p_segment );
6284 if ( p_segment != NULL )
6286 sys.JumpTo( *p_segment, p_chapter );
6291 msg_Dbg( &sys.demuxer, "CallSS VMGM (menu %d, rsm_cell %x)", p_command[5] & 0x0F, p_command[4]);
6294 msg_Dbg( &sys.demuxer, "CallSS VTSM (menu %d, rsm_cell %x)", p_command[5] & 0x0F, p_command[4]);
6297 msg_Dbg( &sys.demuxer, "CallSS VMGM (pgc %d, rsm_cell %x)", (p_command[2] << 8) + p_command[3], p_command[4]);
6302 case CMD_DVD_JUMP_SS:
6304 msg_Dbg( &sys.demuxer, "JumpSS");
6306 switch( (p_command[5] & 0xC0) >> 6 ) {
6308 msg_Dbg( &sys.demuxer, "JumpSS FP");
6311 p_type = p_command[5] & 0x0F;
6315 msg_Dbg( &sys.demuxer, "JumpSS VMGM Title Entry");
6318 msg_Dbg( &sys.demuxer, "JumpSS VMGM Root Menu");
6321 msg_Dbg( &sys.demuxer, "JumpSS VMGM Subpicture Menu");
6324 msg_Dbg( &sys.demuxer, "JumpSS VMGM Audio Menu");
6327 msg_Dbg( &sys.demuxer, "JumpSS VMGM Angle Menu");
6330 msg_Dbg( &sys.demuxer, "JumpSS VMGM Chapter Menu");
6333 msg_Dbg( &sys.demuxer, "JumpSS <unknown>");
6337 p_chapter = sys.BrowseCodecPrivate( 1, MatchIsVMG, NULL, 0, p_segment );
6338 if ( p_segment != NULL )
6340 p_chapter = p_segment->BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1 );
6341 if ( p_chapter != NULL )
6343 sys.JumpTo( *p_segment, p_chapter );
6349 p_type = p_command[5] & 0x0F;
6353 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Title Entry", p_command[4], p_command[3]);
6356 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Root Menu", p_command[4], p_command[3]);
6359 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Subpicture Menu", p_command[4], p_command[3]);
6362 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Audio Menu", p_command[4], p_command[3]);
6365 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Angle Menu", p_command[4], p_command[3]);
6368 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Chapter Menu", p_command[4], p_command[3]);
6371 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) <unknown>", p_command[4], p_command[3]);
6375 p_chapter = sys.BrowseCodecPrivate( 1, MatchVTSMNumber, &p_command[4], 1, p_segment );
6377 if ( p_segment != NULL && p_chapter != NULL )
6379 // find the title in the VTS
6380 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchTitleNumber, &p_command[3], 1 );
6381 if ( p_chapter != NULL )
6383 // find the specified menu in the VTSM
6384 p_chapter = p_segment->BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1 );
6385 if ( p_chapter != NULL )
6387 sys.JumpTo( *p_segment, p_chapter );
6392 msg_Dbg( &sys.demuxer, "Title (%d) does not exist in this VTS", p_command[3] );
6395 msg_Dbg( &sys.demuxer, "DVD Domain VTS (%d) not found", p_command[4] );
6398 msg_Dbg( &sys.demuxer, "JumpSS VMGM (pgc %d)", (p_command[2] << 8) + p_command[3]);
6403 case CMD_DVD_JUMPVTS_PTT:
6405 uint8 i_title = p_command[5];
6406 uint8 i_ptt = p_command[3];
6408 msg_Dbg( &sys.demuxer, "JumpVTS Title (%d) PTT (%d)", i_title, i_ptt);
6410 // find the current VTS content segment
6411 p_chapter = sys.p_current_segment->BrowseCodecPrivate( 1, MatchIsDomain, NULL, 0 );
6412 if ( p_chapter != NULL )
6414 int16 i_curr_title = p_chapter->GetTitleNumber( );
6415 if ( i_curr_title > 0 )
6417 p_chapter = sys.BrowseCodecPrivate( 1, MatchVTSNumber, &i_curr_title, sizeof(i_curr_title), p_segment );
6419 if ( p_segment != NULL && p_chapter != NULL )
6421 // find the title in the VTS
6422 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title) );
6423 if ( p_chapter != NULL )
6425 // find the chapter in the title
6426 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchChapterNumber, &i_ptt, sizeof(i_ptt) );
6427 if ( p_chapter != NULL )
6429 sys.JumpTo( *p_segment, p_chapter );
6434 msg_Dbg( &sys.demuxer, "Title (%d) does not exist in this VTS", i_title );
6437 msg_Dbg( &sys.demuxer, "DVD Domain VTS (%d) not found", i_curr_title );
6440 msg_Dbg( &sys.demuxer, "JumpVTS_PTT command found but not in a VTS(M)");
6443 msg_Dbg( &sys.demuxer, "JumpVTS_PTT command but the DVD domain wasn't found");
6446 case CMD_DVD_SET_GPRMMD:
6448 msg_Dbg( &sys.demuxer, "Set GPRMMD [%d]=%d", (p_command[4] << 8) + p_command[5], (p_command[2] << 8) + p_command[3]);
6450 if ( !SetGPRM( (p_command[4] << 8) + p_command[5], (p_command[2] << 8) + p_command[3] ) )
6451 msg_Dbg( &sys.demuxer, "Set GPRMMD failed" );
6454 case CMD_DVD_LINKPGCN:
6456 uint16 i_pgcn = (p_command[6] << 8) + p_command[7];
6458 msg_Dbg( &sys.demuxer, "Link PGCN(%d)", i_pgcn );
6459 p_chapter = sys.p_current_segment->BrowseCodecPrivate( 1, MatchPgcNumber, &i_pgcn, 2 );
6460 if ( p_chapter != NULL )
6462 if ( !p_chapter->Enter( true ) )
6463 // jump to the location in the found segment
6464 sys.p_current_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
6470 case CMD_DVD_LINKCN:
6472 uint8 i_cn = p_command[7];
6474 p_chapter = sys.p_current_segment->CurrentChapter();
6476 msg_Dbg( &sys.demuxer, "LinkCN (cell %d)", i_cn );
6477 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchCellNumber, &i_cn, 1 );
6478 if ( p_chapter != NULL )
6480 if ( !p_chapter->Enter( true ) )
6481 // jump to the location in the found segment
6482 sys.p_current_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
6488 case CMD_DVD_GOTO_LINE:
6490 msg_Dbg( &sys.demuxer, "GotoLine (%d)", (p_command[6] << 8) + p_command[7] );
6494 case CMD_DVD_SET_HL_BTNN1:
6496 msg_Dbg( &sys.demuxer, "SetHL_BTN (%d)", p_command[4] );
6497 SetSPRM( 0x88, p_command[4] );
6502 msg_Dbg( &sys.demuxer, "unsupported command : %02X %02X %02X %02X %02X %02X %02X %02X"
6518 bool dvd_command_interpretor_c::MatchIsDomain( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6520 return ( data.p_private_data != NULL && data.p_private_data->GetBuffer()[0] == MATROSKA_DVD_LEVEL_SS );
6523 bool dvd_command_interpretor_c::MatchIsVMG( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6525 if ( data.p_private_data == NULL || data.p_private_data->GetSize() < 2 )
6528 return ( data.p_private_data->GetBuffer()[0] == MATROSKA_DVD_LEVEL_SS && data.p_private_data->GetBuffer()[1] == 0xC0);
6531 bool dvd_command_interpretor_c::MatchVTSNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6533 if ( i_cookie_size != 2 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6536 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_SS || data.p_private_data->GetBuffer()[1] != 0x80 )
6539 uint16 i_gtitle = (data.p_private_data->GetBuffer()[2] << 8 ) + data.p_private_data->GetBuffer()[3];
6540 uint16 i_title = *(uint16*)p_cookie;
6542 return (i_gtitle == i_title);
6545 bool dvd_command_interpretor_c::MatchVTSMNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6547 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6550 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_SS || data.p_private_data->GetBuffer()[1] != 0x40 )
6553 uint8 i_gtitle = data.p_private_data->GetBuffer()[3];
6554 uint8 i_title = *(uint8*)p_cookie;
6556 return (i_gtitle == i_title);
6559 bool dvd_command_interpretor_c::MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6561 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6564 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_TT )
6567 uint16 i_gtitle = (data.p_private_data->GetBuffer()[1] << 8 ) + data.p_private_data->GetBuffer()[2];
6568 uint8 i_title = *(uint8*)p_cookie;
6570 return (i_gtitle == i_title);
6573 bool dvd_command_interpretor_c::MatchPgcType( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6575 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 8 )
6578 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PGC )
6581 uint8 i_pgc_type = data.p_private_data->GetBuffer()[3] & 0x0F;
6582 uint8 i_pgc = *(uint8*)p_cookie;
6584 return (i_pgc_type == i_pgc);
6587 bool dvd_command_interpretor_c::MatchPgcNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6589 if ( i_cookie_size != 2 || data.p_private_data == NULL || data.p_private_data->GetSize() < 8 )
6592 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PGC )
6595 uint16 *i_pgc_n = (uint16 *)p_cookie;
6596 uint16 i_pgc_num = (data.p_private_data->GetBuffer()[1] << 8) + data.p_private_data->GetBuffer()[2];
6598 return (i_pgc_num == *i_pgc_n);
6601 bool dvd_command_interpretor_c::MatchChapterNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6603 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 2 )
6606 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PTT )
6609 uint8 i_chapter = data.p_private_data->GetBuffer()[1];
6610 uint8 i_ptt = *(uint8*)p_cookie;
6612 return (i_chapter == i_ptt);
6615 bool dvd_command_interpretor_c::MatchCellNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6617 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 5 )
6620 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_CN )
6623 uint8 *i_cell_n = (uint8 *)p_cookie;
6624 uint8 i_cell_num = data.p_private_data->GetBuffer()[3];
6626 return (i_cell_num == *i_cell_n);
6629 bool matroska_script_codec_c::Enter()
6631 bool f_result = false;
6632 std::vector<KaxChapterProcessData*>::iterator index = enter_cmds.begin();
6633 while ( index != enter_cmds.end() )
6635 if ( (*index)->GetSize() )
6637 msg_Dbg( &sys.demuxer, "Matroska Script enter command" );
6638 f_result |= interpretor.Interpret( (*index)->GetBuffer(), (*index)->GetSize() );
6645 bool matroska_script_codec_c::Leave()
6647 bool f_result = false;
6648 std::vector<KaxChapterProcessData*>::iterator index = leave_cmds.begin();
6649 while ( index != leave_cmds.end() )
6651 if ( (*index)->GetSize() )
6653 msg_Dbg( &sys.demuxer, "Matroska Script leave command" );
6654 f_result |= interpretor.Interpret( (*index)->GetBuffer(), (*index)->GetSize() );
6661 // see http://www.matroska.org/technical/specs/chapters/index.html#mscript
6662 // for a description of existing commands
6663 bool matroska_script_interpretor_c::Interpret( const binary * p_command, size_t i_size )
6665 bool b_result = false;
6667 char *psz_str = (char*) malloc( i_size + 1 );
6668 memcpy( psz_str, p_command, i_size );
6669 psz_str[ i_size ] = '\0';
6671 std::string sz_command = psz_str;
6674 msg_Dbg( &sys.demuxer, "command : %s", sz_command.c_str() );
6676 #if defined(__GNUC__) && (__GNUC__ < 3)
6677 if ( sz_command.compare( CMD_MS_GOTO_AND_PLAY, 0, CMD_MS_GOTO_AND_PLAY.size() ) == 0 )
6679 if ( sz_command.compare( 0, CMD_MS_GOTO_AND_PLAY.size(), CMD_MS_GOTO_AND_PLAY ) == 0 )
6685 for ( i=CMD_MS_GOTO_AND_PLAY.size(); i<sz_command.size(); i++)
6687 if ( sz_command[i] == '(' )
6694 for ( j=i; j<sz_command.size(); j++)
6696 if ( sz_command[j] == ')' )
6703 std::string st = sz_command.substr( i+1, j-i-1 );
6704 int64_t i_chapter_uid = atoi( st.c_str() );
6706 virtual_segment_c *p_segment;
6707 chapter_item_c *p_chapter = sys.FindChapter( i_chapter_uid, p_segment );
6709 if ( p_chapter == NULL )
6710 msg_Dbg( &sys.demuxer, "Chapter %"PRId64" not found", i_chapter_uid);
6713 if ( !p_chapter->EnterAndLeave( sys.p_current_segment->CurrentChapter() ) )
6714 p_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
6722 void demux_sys_t::SwapButtons()
6724 #ifndef WORDS_BIGENDIAN
6725 uint8_t button, i, j;
6727 for( button = 1; button <= pci_packet.hli.hl_gi.btn_ns; button++) {
6728 btni_t *button_ptr = &(pci_packet.hli.btnit[button-1]);
6729 binary *p_data = (binary*) button_ptr;
6731 uint16 i_x_start = ((p_data[0] & 0x3F) << 4 ) + ( p_data[1] >> 4 );
6732 uint16 i_x_end = ((p_data[1] & 0x03) << 8 ) + p_data[2];
6733 uint16 i_y_start = ((p_data[3] & 0x3F) << 4 ) + ( p_data[4] >> 4 );
6734 uint16 i_y_end = ((p_data[4] & 0x03) << 8 ) + p_data[5];
6735 button_ptr->x_start = i_x_start;
6736 button_ptr->x_end = i_x_end;
6737 button_ptr->y_start = i_y_start;
6738 button_ptr->y_end = i_y_end;
6741 for ( i = 0; i<3; i++ )
6743 for ( j = 0; j<2; j++ )
6745 pci_packet.hli.btn_colit.btn_coli[i][j] = U32_AT( &pci_packet.hli.btn_colit.btn_coli[i][j] );