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 if ( tracks[i_track]->p_compression_data )
1056 delete tracks[i_track]->p_compression_data;
1058 es_format_Clean( &tracks[i_track]->fmt );
1059 free( tracks[i_track]->p_extra_data );
1060 free( tracks[i_track]->psz_codec );
1061 delete tracks[i_track];
1064 free( psz_writing_application );
1065 free( psz_muxing_application );
1066 free( psz_segment_filename );
1068 free( psz_date_utc );
1073 delete p_segment_uid;
1074 delete p_prev_segment_uid;
1075 delete p_next_segment_uid;
1077 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
1078 while ( index != stored_editions.end() )
1083 std::vector<chapter_translation_c*>::iterator indext = translations.begin();
1084 while ( indext != translations.end() )
1089 std::vector<KaxSegmentFamily*>::iterator indexf = families.begin();
1090 while ( indexf != families.end() )
1097 KaxSegment *segment;
1101 uint64_t i_timescale;
1103 /* duration of the segment */
1105 mtime_t i_start_time;
1108 std::vector<mkv_track_t*> tracks;
1111 int64_t i_cues_position;
1112 int64_t i_chapters_position;
1113 int64_t i_tags_position;
1115 KaxCluster *cluster;
1117 uint64 i_cluster_pos;
1118 int64_t i_start_pos;
1119 KaxSegmentUID *p_segment_uid;
1120 KaxPrevUID *p_prev_segment_uid;
1121 KaxNextUID *p_next_segment_uid;
1126 mkv_index_t *p_indexes;
1129 char *psz_muxing_application;
1130 char *psz_writing_application;
1131 char *psz_segment_filename;
1135 /* !!!!! GCC 3.3 bug on Darwin !!!!! */
1136 /* when you remove this variable the compiler issues an atomicity error */
1137 /* this variable only works when using std::vector<chapter_edition_c> */
1138 std::vector<chapter_edition_c*> stored_editions;
1139 int i_default_edition;
1141 std::vector<chapter_translation_c*> translations;
1142 std::vector<KaxSegmentFamily*> families;
1149 bool PreloadFamily( const matroska_segment_c & segment );
1150 void ParseInfo( KaxInfo *info );
1151 void ParseAttachments( KaxAttachments *attachments );
1152 void ParseChapters( KaxChapters *chapters );
1153 void ParseSeekHead( KaxSeekHead *seekhead );
1154 void ParseTracks( KaxTracks *tracks );
1155 void ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters );
1156 void ParseTrackEntry( KaxTrackEntry *m );
1157 void ParseCluster( );
1158 void IndexAppendCluster( KaxCluster *cluster );
1161 void InformationCreate( );
1162 void Seek( mtime_t i_date, mtime_t i_time_offset );
1163 #if LIBMATROSKA_VERSION >= 0x000800
1164 int BlockGet( KaxBlock * &, KaxSimpleBlock * &, int64_t *, int64_t *, int64_t *);
1166 int BlockGet( KaxBlock * &, int64_t *, int64_t *, int64_t *);
1168 bool Select( mtime_t i_start_time );
1171 static bool CompareSegmentUIDs( const matroska_segment_c * item_a, const matroska_segment_c * item_b );
1174 // class holding hard-linked segment together in the playback order
1175 class virtual_segment_c
1178 virtual_segment_c( matroska_segment_c *p_segment )
1181 ,i_current_segment(0)
1182 ,i_current_edition(-1)
1183 ,psz_current_chapter(NULL)
1185 linked_segments.push_back( p_segment );
1187 AppendUID( p_segment->p_segment_uid );
1188 AppendUID( p_segment->p_prev_segment_uid );
1189 AppendUID( p_segment->p_next_segment_uid );
1193 size_t AddSegment( matroska_segment_c *p_segment );
1194 void PreloadLinked( );
1195 mtime_t Duration( ) const;
1197 void Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter );
1199 inline chapter_edition_c *Edition()
1201 if ( i_current_edition >= 0 && size_t(i_current_edition) < p_editions->size() )
1202 return (*p_editions)[i_current_edition];
1206 matroska_segment_c * Segment() const
1208 if ( linked_segments.size() == 0 || i_current_segment >= linked_segments.size() )
1210 return linked_segments[i_current_segment];
1213 inline chapter_item_c *CurrentChapter() {
1214 return psz_current_chapter;
1219 if ( i_current_segment < linked_segments.size()-1 )
1221 i_current_segment++;
1227 bool FindUID( KaxSegmentUID & uid ) const
1229 for ( size_t i=0; i<linked_uids.size(); i++ )
1231 if ( linked_uids[i] == uid )
1237 bool UpdateCurrentToChapter( demux_t & demux );
1238 void PrepareChapters( );
1240 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
1241 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1242 const void *p_cookie,
1243 size_t i_cookie_size );
1244 chapter_item_c *FindChapter( int64_t i_find_uid );
1246 std::vector<chapter_edition_c*> *p_editions;
1250 std::vector<matroska_segment_c*> linked_segments;
1251 std::vector<KaxSegmentUID> linked_uids;
1252 size_t i_current_segment;
1254 int i_current_edition;
1255 chapter_item_c *psz_current_chapter;
1257 void AppendUID( const EbmlBinary * UID );
1260 class matroska_stream_c
1263 matroska_stream_c( demux_sys_t & demuxer )
1269 virtual ~matroska_stream_c()
1278 std::vector<matroska_segment_c*> segments;
1304 virtual ~attachment_c()
1309 std::string psz_file_name;
1310 std::string psz_mime_type;
1318 demux_sys_t( demux_t & demux )
1325 ,p_current_segment(NULL)
1326 ,dvd_interpretor( *this )
1330 ,b_pci_packet_set(false)
1333 vlc_mutex_init( &lock_demuxer );
1336 virtual ~demux_sys_t()
1340 for ( i=0; i<streams.size(); i++ )
1342 for ( i=0; i<opened_segments.size(); i++ )
1343 delete opened_segments[i];
1344 for ( i=0; i<used_segments.size(); i++ )
1345 delete used_segments[i];
1346 for ( i=0; i<stored_attachments.size(); i++ )
1347 delete stored_attachments[i];
1348 if( meta ) vlc_meta_Delete( meta );
1350 while( titles.size() )
1351 { vlc_input_title_Delete( titles.back() ); titles.pop_back();}
1353 vlc_mutex_destroy( &lock_demuxer );
1360 mtime_t i_start_pts;
1361 mtime_t i_chapter_time;
1365 std::vector<input_title_t*> titles; // matroska editions
1366 size_t i_current_title;
1368 std::vector<matroska_stream_c*> streams;
1369 std::vector<attachment_c*> stored_attachments;
1370 std::vector<matroska_segment_c*> opened_segments;
1371 std::vector<virtual_segment_c*> used_segments;
1372 virtual_segment_c *p_current_segment;
1374 dvd_command_interpretor_c dvd_interpretor;
1376 /* duration of the stream */
1379 matroska_segment_c *FindSegment( const EbmlBinary & uid ) const;
1380 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
1381 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1382 const void *p_cookie,
1383 size_t i_cookie_size,
1384 virtual_segment_c * & p_segment_found );
1385 chapter_item_c *FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found );
1387 void PreloadFamily( const matroska_segment_c & of_segment );
1388 void PreloadLinked( matroska_segment_c *p_segment );
1389 bool PreparePlayback( virtual_segment_c *p_new_segment );
1390 matroska_stream_c *AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial = false );
1391 void JumpTo( virtual_segment_c & p_segment, chapter_item_c * p_chapter );
1393 void StartUiThread();
1394 void StopUiThread();
1396 inline void SwapButtons();
1398 /* for spu variables */
1399 input_thread_t *p_input;
1401 bool b_pci_packet_set;
1402 uint8_t palette[4][4];
1403 vlc_mutex_t lock_demuxer;
1406 event_thread_t *p_ev;
1407 static int EventThread( vlc_object_t *p_this );
1408 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1409 vlc_value_t oldval, vlc_value_t newval, void *p_data );
1410 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1411 vlc_value_t oldval, vlc_value_t newval, void *p_data );
1416 virtual_segment_c *VirtualFromSegments( matroska_segment_c *p_segment ) const;
1417 bool IsUsedSegment( matroska_segment_c &p_segment ) const;
1420 static int Demux ( demux_t * );
1421 static int Control( demux_t *, int, va_list );
1422 static void Seek ( demux_t *, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter );
1424 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
1426 static inline char * ToUTF8( const UTFstring &u )
1428 return strdup( u.GetUTF8().c_str() );
1431 /*****************************************************************************
1432 * Open: initializes matroska demux structures
1433 *****************************************************************************/
1434 static int Open( vlc_object_t * p_this )
1436 demux_t *p_demux = (demux_t*)p_this;
1438 matroska_stream_c *p_stream;
1439 matroska_segment_c *p_segment;
1440 const uint8_t *p_peek;
1441 std::string s_path, s_filename;
1442 vlc_stream_io_callback *p_io_callback;
1443 EbmlStream *p_io_stream;
1445 /* peek the begining */
1446 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
1448 /* is a valid file */
1449 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
1450 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
1452 /* Set the demux function */
1453 p_demux->pf_demux = Demux;
1454 p_demux->pf_control = Control;
1455 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
1457 p_io_callback = new vlc_stream_io_callback( p_demux->s, false );
1458 p_io_stream = new EbmlStream( *p_io_callback );
1460 if( p_io_stream == NULL )
1462 msg_Err( p_demux, "failed to create EbmlStream" );
1463 delete p_io_callback;
1465 return VLC_EGENERIC;
1468 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_io_stream, true );
1469 if( p_stream == NULL )
1471 msg_Err( p_demux, "cannot find KaxSegment" );
1474 p_sys->streams.push_back( p_stream );
1476 p_stream->p_in = p_io_callback;
1477 p_stream->p_es = p_io_stream;
1479 for (size_t i=0; i<p_stream->segments.size(); i++)
1481 p_stream->segments[i]->Preload();
1484 p_segment = p_stream->segments[0];
1485 if( p_segment->cluster != NULL )
1487 msg_Warn( p_demux, "cannot find any cluster, damaged file ?" );
1489 // reset the stream reading to the first cluster of the segment used
1490 p_stream->p_in->setFilePointer( p_segment->cluster->GetElementPosition() );
1493 if (config_GetInt( p_demux, "mkv-preload-local-dir" ))
1495 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
1496 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
1498 // assume it's a regular file
1499 // get the directory path
1500 s_path = p_demux->psz_path;
1501 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
1503 s_path = s_path.substr(0,s_path.length()-1);
1507 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
1509 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
1513 DIR *p_src_dir = utf8_opendir(s_path.c_str());
1515 if (p_src_dir != NULL)
1518 while ((psz_file = utf8_readdir(p_src_dir)) != NULL)
1520 if (strlen(psz_file) > 4)
1522 s_filename = s_path + DIRECTORY_SEPARATOR + psz_file;
1525 if (!strcasecmp(s_filename.c_str(), p_demux->psz_path))
1527 if (!s_filename.compare(p_demux->psz_path))
1531 continue; // don't reuse the original opened file
1534 #if defined(__GNUC__) && (__GNUC__ < 3)
1535 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
1536 !s_filename.compare("mka", s_filename.length() - 3, 3))
1538 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
1539 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
1542 // test wether this file belongs to our family
1543 const uint8_t *p_peek;
1544 bool file_ok = false;
1545 stream_t *p_file_stream = stream_UrlNew(
1547 s_filename.c_str());
1548 /* peek the begining */
1549 if( p_file_stream &&
1550 stream_Peek( p_file_stream, &p_peek, 4 ) >= 4
1551 && p_peek[0] == 0x1a && p_peek[1] == 0x45 &&
1552 p_peek[2] == 0xdf && p_peek[3] == 0xa3 ) file_ok = true;
1556 vlc_stream_io_callback *p_file_io = new vlc_stream_io_callback( p_file_stream, true );
1557 EbmlStream *p_estream = new EbmlStream(*p_file_io);
1559 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_estream );
1561 if ( p_stream == NULL )
1563 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
1569 p_stream->p_in = p_file_io;
1570 p_stream->p_es = p_estream;
1571 p_sys->streams.push_back( p_stream );
1576 if( p_file_stream ) {
1577 stream_Delete( p_file_stream );
1579 msg_Dbg( p_demux, "the file '%s' cannot be opened", s_filename.c_str() );
1585 closedir( p_src_dir );
1589 p_sys->PreloadFamily( *p_segment );
1592 p_sys->PreloadLinked( p_segment );
1594 if ( !p_sys->PreparePlayback( NULL ) )
1596 msg_Err( p_demux, "cannot use the segment" );
1600 p_sys->StartUiThread();
1606 return VLC_EGENERIC;
1609 /*****************************************************************************
1610 * Close: frees unused data
1611 *****************************************************************************/
1612 static void Close( vlc_object_t *p_this )
1614 demux_t *p_demux = (demux_t*)p_this;
1615 demux_sys_t *p_sys = p_demux->p_sys;
1620 /*****************************************************************************
1622 *****************************************************************************/
1623 static int Control( demux_t *p_demux, int i_query, va_list args )
1625 demux_sys_t *p_sys = p_demux->p_sys;
1632 input_attachment_t ***ppp_attach;
1638 case DEMUX_GET_ATTACHMENTS:
1639 ppp_attach = (input_attachment_t***)va_arg( args, input_attachment_t*** );
1640 pi_int = (int*)va_arg( args, int * );
1642 if( p_sys->stored_attachments.size() <= 0 )
1643 return VLC_EGENERIC;
1645 *pi_int = p_sys->stored_attachments.size();
1646 *ppp_attach = (input_attachment_t**)malloc( sizeof(input_attachment_t**) *
1647 p_sys->stored_attachments.size() );
1648 if( !(*ppp_attach) )
1650 for( i = 0; i < p_sys->stored_attachments.size(); i++ )
1652 attachment_c *a = p_sys->stored_attachments[i];
1653 (*ppp_attach)[i] = vlc_input_attachment_New( a->psz_file_name.c_str(), a->psz_mime_type.c_str(), NULL,
1654 a->p_data, a->i_size );
1658 case DEMUX_GET_META:
1659 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1660 vlc_meta_Merge( p_meta, p_sys->meta );
1663 case DEMUX_GET_LENGTH:
1664 pi64 = (int64_t*)va_arg( args, int64_t * );
1665 if( p_sys->f_duration > 0.0 )
1667 *pi64 = (int64_t)(p_sys->f_duration * 1000);
1670 return VLC_EGENERIC;
1672 case DEMUX_GET_POSITION:
1673 pf = (double*)va_arg( args, double * );
1674 if ( p_sys->f_duration > 0.0 )
1675 *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);
1678 case DEMUX_SET_POSITION:
1679 f = (double)va_arg( args, double );
1680 Seek( p_demux, -1, f, NULL );
1683 case DEMUX_GET_TIME:
1684 pi64 = (int64_t*)va_arg( args, int64_t * );
1685 *pi64 = p_sys->i_pts;
1688 case DEMUX_GET_TITLE_INFO:
1689 if( p_sys->titles.size() )
1691 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1692 int *pi_int = (int*)va_arg( args, int* );
1694 *pi_int = p_sys->titles.size();
1695 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->titles.size() );
1697 for( size_t i = 0; i < p_sys->titles.size(); i++ )
1699 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->titles[i] );
1704 return VLC_EGENERIC;
1706 case DEMUX_SET_TITLE:
1707 /* TODO handle editions as titles */
1708 i_idx = (int)va_arg( args, int );
1709 if( i_idx < p_sys->used_segments.size() )
1711 p_sys->JumpTo( *p_sys->used_segments[i_idx], NULL );
1714 return VLC_EGENERIC;
1716 case DEMUX_SET_SEEKPOINT:
1717 i_skp = (int)va_arg( args, int );
1719 // TODO change the way it works with the << & >> buttons on the UI (+1/-1 instead of a number)
1720 if( p_sys->titles.size() && i_skp < p_sys->titles[p_sys->i_current_title]->i_seekpoint)
1722 Seek( p_demux, (int64_t)p_sys->titles[p_sys->i_current_title]->seekpoint[i_skp]->i_time_offset, -1, NULL);
1723 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
1724 p_demux->info.i_seekpoint = i_skp;
1727 return VLC_EGENERIC;
1729 case DEMUX_SET_TIME:
1732 return VLC_EGENERIC;
1736 #if LIBMATROSKA_VERSION >= 0x000800
1737 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1739 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1742 #if LIBMATROSKA_VERSION >= 0x000800
1743 pp_simpleblock = NULL;
1751 EbmlElement *el = NULL;
1755 return VLC_EGENERIC;
1757 #if LIBMATROSKA_VERSION >= 0x000800
1758 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1760 if( (el = ep->Get()) == NULL && pp_block != NULL )
1763 /* update the index */
1764 #define idx p_indexes[i_index - 1]
1765 if( i_index > 0 && idx.i_time == -1 )
1767 #if LIBMATROSKA_VERSION >= 0x000800
1768 if ( pp_simpleblock != NULL )
1769 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1772 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1773 idx.b_key = *pi_ref1 == 0 ? true : false;
1779 i_level = ep->GetLevel();
1788 msg_Warn( &sys.demuxer, "EOF" );
1789 return VLC_EGENERIC;
1796 if( MKV_IS_ID( el, KaxCluster ) )
1798 cluster = (KaxCluster*)el;
1799 i_cluster_pos = cluster->GetElementPosition();
1801 /* add it to the index */
1803 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1805 IndexAppendCluster( cluster );
1808 // reset silent tracks
1809 for (size_t i=0; i<tracks.size(); i++)
1811 tracks[i]->b_silent = false;
1816 else if( MKV_IS_ID( el, KaxCues ) )
1818 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1819 return VLC_EGENERIC;
1823 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1827 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1829 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1831 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1832 cluster->InitTimecode( uint64( ctc ), i_timescale );
1834 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1838 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1840 i_block_pos = el->GetElementPosition();
1843 #if LIBMATROSKA_VERSION >= 0x000800
1844 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1846 pp_simpleblock = (KaxSimpleBlock*)el;
1848 pp_simpleblock->ReadData( es.I_O() );
1849 pp_simpleblock->SetParent( *cluster );
1854 if( MKV_IS_ID( el, KaxBlock ) )
1856 pp_block = (KaxBlock*)el;
1858 pp_block->ReadData( es.I_O() );
1859 pp_block->SetParent( *cluster );
1863 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1865 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1867 dur.ReadData( es.I_O() );
1868 *pi_duration = uint64( dur );
1870 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1872 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1874 ref.ReadData( es.I_O() );
1877 *pi_ref1 = int64( ref ) * cluster->GlobalTimecodeScale();
1879 else if( *pi_ref2 == 0 )
1881 *pi_ref2 = int64( ref ) * cluster->GlobalTimecodeScale();
1884 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1886 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1887 track_num.ReadData( es.I_O() );
1889 for (size_t i=0; i<tracks.size(); i++)
1891 if ( tracks[i]->i_number == uint32(track_num))
1893 tracks[i]->b_silent = true;
1900 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1901 return VLC_EGENERIC;
1906 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem, size_t offset)
1909 if( !(p_block = block_New( p_demux, i_mem + offset ) ) ) return NULL;
1910 memcpy( p_block->p_buffer + offset, p_mem, i_mem );
1911 //p_block->i_rate = p_input->stream.control.i_rate;
1915 #if LIBMATROSKA_VERSION >= 0x000800
1916 static void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
1917 mtime_t i_pts, mtime_t i_duration, bool f_mandatory )
1919 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1920 mtime_t i_duration, bool f_mandatory )
1923 demux_sys_t *p_sys = p_demux->p_sys;
1924 matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
1930 #define tk p_segment->tracks[i_track]
1931 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1933 #if LIBMATROSKA_VERSION >= 0x000800
1934 if( (block != NULL && tk->i_number == block->TrackNum()) ||
1935 (simpleblock != NULL && tk->i_number == simpleblock->TrackNum()))
1937 if( tk->i_number == block->TrackNum() )
1944 if( i_track >= p_segment->tracks.size() )
1946 msg_Err( p_demux, "invalid track number" );
1949 if( tk->fmt.i_cat != NAV_ES && tk->p_es == NULL )
1951 msg_Err( p_demux, "unknown track number" );
1954 if( i_pts + i_duration < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1956 return; /* discard audio packets that shouldn't be rendered */
1959 if ( tk->fmt.i_cat != NAV_ES )
1961 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1965 tk->b_inited = false;
1971 /* First send init data */
1972 if( !tk->b_inited && tk->i_data_init > 0 )
1976 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
1977 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init, 0 );
1978 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
1980 tk->b_inited = true;
1983 #if LIBMATROSKA_VERSION >= 0x000800
1985 (block != NULL && i < block->NumberFrames()) || (simpleblock != NULL && i < simpleblock->NumberFrames());
1988 for( i = 0; i < block->NumberFrames(); i++ )
1993 #if LIBMATROSKA_VERSION >= 0x000800
1994 if ( simpleblock != NULL )
1996 data = &simpleblock->GetBuffer(i);
1997 // condition when the DTS is correct (keyframe or B frame == NOT P frame)
1998 f_mandatory = simpleblock->IsDiscardable() || simpleblock->IsKeyframe();
2002 data = &block->GetBuffer(i);
2004 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER && tk->p_compression_data != NULL )
2005 p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), tk->p_compression_data->GetSize() );
2007 p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), 0 );
2009 if( p_block == NULL )
2014 #if defined(HAVE_ZLIB_H)
2015 if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB )
2017 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
2021 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER )
2023 memcpy( p_block->p_buffer, tk->p_compression_data->GetBuffer(), tk->p_compression_data->GetSize() );
2026 if ( tk->fmt.i_cat == NAV_ES )
2028 // TODO handle the start/stop times of this packet
2029 if ( p_sys->b_ui_hooked )
2031 vlc_mutex_lock( &p_sys->p_ev->lock );
2032 memcpy( &p_sys->pci_packet, &p_block->p_buffer[1], sizeof(pci_t) );
2033 p_sys->SwapButtons();
2034 p_sys->b_pci_packet_set = true;
2035 vlc_mutex_unlock( &p_sys->p_ev->lock );
2036 block_Release( p_block );
2040 // correct timestamping when B frames are used
2041 if( tk->fmt.i_cat != VIDEO_ES )
2043 p_block->i_dts = p_block->i_pts = i_pts;
2047 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
2049 // in VFW we have no idea about B frames
2051 p_block->i_dts = i_pts;
2055 p_block->i_pts = i_pts;
2057 p_block->i_dts = p_block->i_pts;
2059 p_block->i_dts = min( i_pts, tk->i_last_dts + (mtime_t)(tk->i_default_duration >> 10));
2060 p_sys->i_pts = p_block->i_dts;
2063 tk->i_last_dts = p_block->i_dts;
2066 msg_Dbg( p_demux, "block i_dts: %"PRId64" / i_pts: %"PRId64, p_block->i_dts, p_block->i_pts);
2068 if( strcmp( tk->psz_codec, "S_VOBSUB" ) )
2070 p_block->i_length = i_duration * 1000;
2073 es_out_Send( p_demux->out, tk->p_es, p_block );
2075 /* use time stamp only for first block */
2082 matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial )
2084 int i_upper_lvl = 0;
2086 EbmlElement *p_l0, *p_l1, *p_l2;
2087 bool b_keep_stream = false, b_keep_segment;
2089 // verify the EBML Header
2090 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
2093 msg_Err( p_demux, "No EBML header found" );
2097 // verify we can read this Segment, we only support Matroska version 1 for now
2098 p_l0->Read(*p_estream, EbmlHead::ClassInfos.Context, i_upper_lvl, p_l0, true);
2100 EDocType doc_type = GetChild<EDocType>(*static_cast<EbmlHead*>(p_l0));
2101 if (std::string(doc_type) != "matroska")
2103 msg_Err( p_demux, "Not a Matroska file : DocType = %s ", std::string(doc_type).c_str());
2107 EDocTypeReadVersion doc_read_version = GetChild<EDocTypeReadVersion>(*static_cast<EbmlHead*>(p_l0));
2108 #if LIBMATROSKA_VERSION >= 0x000800
2109 if (uint64(doc_read_version) > 2)
2111 msg_Err( p_demux, "This matroska file is needs version %"PRId64" and this VLC only supports version 1 & 2", uint64(doc_read_version));
2115 if (uint64(doc_read_version) != 1)
2117 msg_Err( p_demux, "This matroska file is needs version %"PRId64" and this VLC only supports version 1", uint64(doc_read_version));
2125 // find all segments in this file
2126 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFFLL);
2132 matroska_stream_c *p_stream1 = new matroska_stream_c( *this );
2136 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
2139 matroska_segment_c *p_segment1 = new matroska_segment_c( *this, *p_estream );
2140 b_keep_segment = b_initial;
2142 ep = new EbmlParser(p_estream, p_l0, &demuxer );
2143 p_segment1->ep = ep;
2144 p_segment1->segment = (KaxSegment*)p_l0;
2146 while ((p_l1 = ep->Get()))
2148 if (MKV_IS_ID(p_l1, KaxInfo))
2150 // find the families of this segment
2151 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
2153 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
2154 for( i = 0; i < p_info->ListSize(); i++ )
2156 EbmlElement *l = (*p_info)[i];
2158 if( MKV_IS_ID( l, KaxSegmentUID ) )
2160 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
2161 b_keep_segment = (FindSegment( *p_uid ) == NULL);
2162 if ( !b_keep_segment )
2163 break; // this segment is already known
2164 opened_segments.push_back( p_segment1 );
2165 delete p_segment1->p_segment_uid;
2166 p_segment1->p_segment_uid = new KaxSegmentUID(*p_uid);
2168 else if( MKV_IS_ID( l, KaxPrevUID ) )
2170 p_segment1->p_prev_segment_uid = new KaxPrevUID( *static_cast<KaxPrevUID*>(l) );
2172 else if( MKV_IS_ID( l, KaxNextUID ) )
2174 p_segment1->p_next_segment_uid = new KaxNextUID( *static_cast<KaxNextUID*>(l) );
2176 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
2178 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
2179 p_segment1->families.push_back( p_fam );
2185 if ( b_keep_segment )
2187 b_keep_stream = true;
2188 p_stream1->segments.push_back( p_segment1 );
2192 p_segment1->segment = NULL;
2196 if (p_l0->IsFiniteSize() )
2198 p_l0->SkipData(*p_estream, KaxMatroska_Context);
2199 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
2202 p_l0 = p_l0->SkipData(*p_estream, KaxSegment_Context);
2205 if ( !b_keep_stream )
2214 bool matroska_segment_c::Select( mtime_t i_start_time )
2219 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
2220 sys.b_pci_packet_set = false;
2222 for( i_track = 0; i_track < tracks.size(); i_track++ )
2224 if( tracks[i_track]->fmt.i_cat == UNKNOWN_ES )
2226 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, tracks[i_track]->i_number );
2227 tracks[i_track]->p_es = NULL;
2231 if( !strcmp( tracks[i_track]->psz_codec, "V_MS/VFW/FOURCC" ) )
2233 if( tracks[i_track]->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
2235 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
2236 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2240 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tracks[i_track]->p_extra_data;
2242 tracks[i_track]->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
2243 tracks[i_track]->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
2244 tracks[i_track]->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
2246 tracks[i_track]->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
2247 if( tracks[i_track]->fmt.i_extra > 0 )
2249 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2250 memcpy( tracks[i_track]->fmt.p_extra, &p_bih[1], tracks[i_track]->fmt.i_extra );
2254 else if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG1" ) ||
2255 !strcmp( tracks[i_track]->psz_codec, "V_MPEG2" ) )
2257 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
2259 else if( !strncmp( tracks[i_track]->psz_codec, "V_THEORA", 8 ) )
2261 uint8_t *p_data = tracks[i_track]->p_extra_data;
2262 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
2263 if( tracks[i_track]->i_extra_data >= 4 ) {
2264 if( p_data[0] == 2 ) {
2266 int i_size1 = 0, i_size2 = 0;
2268 /* read size of first header packet */
2269 while( *p_data == 0xFF &&
2270 i < tracks[i_track]->i_extra_data )
2279 msg_Dbg( &sys.demuxer, "first theora header size %d", i_size1 );
2280 /* read size of second header packet */
2281 while( *p_data == 0xFF &&
2282 i < tracks[i_track]->i_extra_data )
2291 int i_size3 = tracks[i_track]->i_extra_data - i - i_size1
2293 msg_Dbg( &sys.demuxer, "second theora header size %d", i_size2 );
2294 msg_Dbg( &sys.demuxer, "third theora header size %d", i_size3 );
2295 tracks[i_track]->fmt.i_extra = i_size1 + i_size2 + i_size3
2297 if( i_size1 > 0 && i_size2 > 0 && i_size3 > 0 ) {
2298 tracks[i_track]->fmt.p_extra =
2299 malloc( tracks[i_track]->fmt.i_extra );
2300 uint8_t *p_out = (uint8_t*)tracks[i_track]->fmt.p_extra;
2301 *p_out++ = (i_size1>>8) & 0xFF;
2302 *p_out++ = i_size1 & 0xFF;
2303 memcpy( p_out, p_data, i_size1 );
2307 *p_out++ = (i_size2>>8) & 0xFF;
2308 *p_out++ = i_size2 & 0xFF;
2309 memcpy( p_out, p_data, i_size2 );
2313 *p_out++ = (i_size3>>8) & 0xFF;
2314 *p_out++ = i_size3 & 0xFF;
2315 memcpy( p_out, p_data, i_size3 );
2321 msg_Err( &sys.demuxer, "inconsistant theora extradata" );
2325 msg_Err( &sys.demuxer, "Wrong number of ogg packets with theora headers (%d)", p_data[0] + 1 );
2329 else if( !strncmp( tracks[i_track]->psz_codec, "V_REAL/RV", 9 ) )
2331 if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV10" ) )
2332 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '1', '0' );
2333 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV20" ) )
2334 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '2', '0' );
2335 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV30" ) )
2336 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '3', '0' );
2337 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV40" ) )
2338 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '4', '0' );
2340 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4", 7 ) )
2342 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/MS/V3" ) )
2344 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
2346 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO", 11 ) )
2348 /* A MPEG 4 codec, SP, ASP, AP or AVC */
2349 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO/AVC" ) )
2350 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
2352 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
2353 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2354 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2355 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2358 else if( !strcmp( tracks[i_track]->psz_codec, "V_QUICKTIME" ) )
2360 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
2361 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
2362 tracks[i_track]->p_extra_data,
2363 tracks[i_track]->i_extra_data,
2365 MP4_ReadBoxCommon( p_mp4_stream, p_box );
2366 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
2367 tracks[i_track]->fmt.i_codec = p_box->i_type;
2368 tracks[i_track]->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
2369 tracks[i_track]->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
2370 tracks[i_track]->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
2371 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2372 memcpy( tracks[i_track]->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tracks[i_track]->fmt.i_extra );
2373 MP4_FreeBox_sample_vide( p_box );
2374 stream_Delete( p_mp4_stream );
2376 else if( !strcmp( tracks[i_track]->psz_codec, "A_MS/ACM" ) )
2378 if( tracks[i_track]->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
2380 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
2381 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2385 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tracks[i_track]->p_extra_data;
2387 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tracks[i_track]->fmt.i_codec, NULL );
2389 tracks[i_track]->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
2390 tracks[i_track]->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
2391 tracks[i_track]->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
2392 tracks[i_track]->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
2393 tracks[i_track]->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
2395 tracks[i_track]->fmt.i_extra = GetWLE( &p_wf->cbSize );
2396 if( tracks[i_track]->fmt.i_extra > 0 )
2398 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2399 memcpy( tracks[i_track]->fmt.p_extra, &p_wf[1], tracks[i_track]->fmt.i_extra );
2403 else if( !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L3" ) ||
2404 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L2" ) ||
2405 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L1" ) )
2407 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
2409 else if( !strcmp( tracks[i_track]->psz_codec, "A_AC3" ) )
2411 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2413 else if( !strcmp( tracks[i_track]->psz_codec, "A_DTS" ) )
2415 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2417 else if( !strcmp( tracks[i_track]->psz_codec, "A_FLAC" ) )
2419 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
2420 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2421 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2422 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2424 else if( !strcmp( tracks[i_track]->psz_codec, "A_VORBIS" ) )
2426 int i, i_offset = 1, i_size[3], i_extra;
2429 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
2431 /* Split the 3 headers */
2432 if( tracks[i_track]->p_extra_data[0] != 0x02 )
2433 msg_Err( &sys.demuxer, "invalid vorbis header" );
2435 for( i = 0; i < 2; i++ )
2438 while( i_offset < tracks[i_track]->i_extra_data )
2440 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
2441 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
2445 i_size[0] = __MIN(i_size[0], tracks[i_track]->i_extra_data - i_offset);
2446 i_size[1] = __MIN(i_size[1], tracks[i_track]->i_extra_data -i_offset -i_size[0]);
2447 i_size[2] = tracks[i_track]->i_extra_data - i_offset - i_size[0] - i_size[1];
2449 tracks[i_track]->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
2450 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2451 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra; i_extra = 0;
2452 for( i = 0; i < 3; i++ )
2454 *(p_extra++) = i_size[i] >> 8;
2455 *(p_extra++) = i_size[i] & 0xFF;
2456 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra,
2458 p_extra += i_size[i];
2459 i_extra += i_size[i];
2462 else if( !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
2463 !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
2465 int i_profile, i_srate, sbr = 0;
2466 static unsigned int i_sample_rates[] =
2468 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
2469 16000, 12000, 11025, 8000, 7350, 0, 0, 0
2472 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
2473 /* create data for faad (MP4DecSpecificDescrTag)*/
2475 if( !strcmp( &tracks[i_track]->psz_codec[12], "MAIN" ) )
2479 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC" ) )
2483 else if( !strcmp( &tracks[i_track]->psz_codec[12], "SSR" ) )
2487 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC/SBR" ) )
2497 for( i_srate = 0; i_srate < 13; i_srate++ )
2499 if( i_sample_rates[i_srate] == tracks[i_track]->i_original_rate )
2504 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
2506 tracks[i_track]->fmt.i_extra = sbr ? 5 : 2;
2507 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2508 ((uint8_t*)tracks[i_track]->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
2509 ((uint8_t*)tracks[i_track]->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tracks[i_track]->fmt.audio.i_channels << 3);
2512 int syncExtensionType = 0x2B7;
2514 for (iDSRI=0; iDSRI<13; iDSRI++)
2515 if( i_sample_rates[iDSRI] == tracks[i_track]->fmt.audio.i_rate )
2517 ((uint8_t*)tracks[i_track]->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
2518 ((uint8_t*)tracks[i_track]->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
2519 ((uint8_t*)tracks[i_track]->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
2522 else if( !strcmp( tracks[i_track]->psz_codec, "A_AAC" ) )
2524 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
2525 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2526 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2527 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2529 else if( !strcmp( tracks[i_track]->psz_codec, "A_WAVPACK4" ) )
2531 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'W', 'V', 'P', 'K' );
2532 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2533 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2534 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2536 else if( !strcmp( tracks[i_track]->psz_codec, "A_TTA1" ) )
2538 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'T', 'T', 'A', '1' );
2539 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2540 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2541 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2543 else if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) ||
2544 !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/LIT" ) ||
2545 !strcmp( tracks[i_track]->psz_codec, "A_PCM/FLOAT/IEEE" ) )
2547 if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) )
2549 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
2553 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
2555 tracks[i_track]->fmt.audio.i_blockalign = ( tracks[i_track]->fmt.audio.i_bitspersample + 7 ) / 8 * tracks[i_track]->fmt.audio.i_channels;
2557 /* disabled due to the potential "S_KATE" namespace issue */
2558 else if( !strcmp( tracks[i_track]->psz_codec, "S_KATE" ) )
2560 int i, i_offset = 1, *i_size, i_extra, num_headers, size_so_far;
2563 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'k', 'a', 't', 'e' );
2564 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2566 /* Recover the number of headers to expect */
2567 num_headers = tracks[i_track]->p_extra_data[0]+1;
2568 msg_Dbg( &sys.demuxer, "kate in mkv detected: %d headers in %u bytes",
2569 num_headers, tracks[i_track]->i_extra_data);
2571 /* this won't overflow the stack as is can allocate only 1020 bytes max */
2572 i_size = (int*)alloca(num_headers*sizeof(int));
2574 /* Split the headers */
2576 for( i = 0; i < num_headers-1; i++ )
2579 while( i_offset < tracks[i_track]->i_extra_data )
2581 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
2582 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
2584 msg_Dbg( &sys.demuxer, "kate header %d is %d bytes", i, i_size[i]);
2585 size_so_far += i_size[i];
2587 i_size[num_headers-1] = tracks[i_track]->i_extra_data - (size_so_far+i_offset);
2588 msg_Dbg( &sys.demuxer, "kate last header (%d) is %d bytes", num_headers-1, i_size[num_headers-1]);
2590 tracks[i_track]->fmt.i_extra = 1 + num_headers * 2 + size_so_far + i_size[num_headers-1];
2591 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2593 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra;
2595 *(p_extra++) = num_headers;
2597 for( i = 0; i < num_headers; i++ )
2599 *(p_extra++) = i_size[i] >> 8;
2600 *(p_extra++) = i_size[i] & 0xFF;
2601 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra-1,
2603 p_extra += i_size[i];
2604 i_extra += i_size[i];
2607 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/UTF8" ) )
2609 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
2610 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2612 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/USF" ) )
2614 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
2615 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2616 if( tracks[i_track]->i_extra_data )
2618 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2619 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2620 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2623 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/SSA" ) ||
2624 !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASS" ) ||
2625 !strcmp( tracks[i_track]->psz_codec, "S_SSA" ) ||
2626 !strcmp( tracks[i_track]->psz_codec, "S_ASS" ))
2628 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
2629 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2630 if( tracks[i_track]->i_extra_data )
2632 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2633 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2634 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2637 else if( !strcmp( tracks[i_track]->psz_codec, "S_VOBSUB" ) )
2639 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
2640 if( tracks[i_track]->i_extra_data )
2643 char *p_buf = (char *)malloc( tracks[i_track]->i_extra_data + 1);
2644 memcpy( p_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
2645 p_buf[tracks[i_track]->i_extra_data] = '\0';
2647 p_start = strstr( p_buf, "size:" );
2648 if( sscanf( p_start, "size: %dx%d",
2649 &tracks[i_track]->fmt.subs.spu.i_original_frame_width, &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) == 2 )
2651 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 );
2655 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
2660 else if( !strcmp( tracks[i_track]->psz_codec, "B_VOBBTN" ) )
2662 tracks[i_track]->fmt.i_cat = NAV_ES;
2667 msg_Err( &sys.demuxer, "unknown codec id=`%s'", tracks[i_track]->psz_codec );
2668 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2670 if( tracks[i_track]->b_default )
2672 tracks[i_track]->fmt.i_priority = 1000;
2675 tracks[i_track]->p_es = es_out_Add( sys.demuxer.out, &tracks[i_track]->fmt );
2677 /* Turn on a subtitles track if it has been flagged as default -
2678 * but only do this if no subtitles track has already been engaged,
2679 * either by an earlier 'default track' (??) or by default
2680 * language choice behaviour.
2682 if( tracks[i_track]->b_default )
2684 es_out_Control( sys.demuxer.out,
2686 tracks[i_track]->p_es );
2689 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, i_start_time );
2692 sys.i_start_pts = i_start_time;
2693 // reset the stream reading to the first cluster of the segment used
2694 es.I_O().setFilePointer( i_start_pos );
2697 ep = new EbmlParser( &es, segment, &sys.demuxer );
2702 void demux_sys_t::StartUiThread()
2706 msg_Dbg( &demuxer, "Starting the UI Hook" );
2708 /* FIXME hack hack hack hack FIXME */
2709 /* Get p_input and create variable */
2710 p_input = (input_thread_t *) vlc_object_find( &demuxer, VLC_OBJECT_INPUT, FIND_PARENT );
2711 var_Create( p_input, "x-start", VLC_VAR_INTEGER );
2712 var_Create( p_input, "y-start", VLC_VAR_INTEGER );
2713 var_Create( p_input, "x-end", VLC_VAR_INTEGER );
2714 var_Create( p_input, "y-end", VLC_VAR_INTEGER );
2715 var_Create( p_input, "color", VLC_VAR_ADDRESS );
2716 var_Create( p_input, "menu-palette", VLC_VAR_ADDRESS );
2717 var_Create( p_input, "highlight", VLC_VAR_BOOL );
2718 var_Create( p_input, "highlight-mutex", VLC_VAR_MUTEX );
2720 /* Now create our event thread catcher */
2721 p_ev = (event_thread_t *) vlc_object_create( &demuxer, sizeof( event_thread_t ) );
2722 p_ev->p_demux = &demuxer;
2723 p_ev->b_die = false;
2724 vlc_mutex_init( &p_ev->lock );
2725 vlc_thread_create( p_ev, "mkv event thread handler", EventThread,
2726 VLC_THREAD_PRIORITY_LOW, false );
2730 void demux_sys_t::StopUiThread()
2734 vlc_object_kill( p_ev );
2735 vlc_thread_join( p_ev );
2736 vlc_object_release( p_ev );
2740 var_Destroy( p_input, "highlight-mutex" );
2741 var_Destroy( p_input, "highlight" );
2742 var_Destroy( p_input, "x-start" );
2743 var_Destroy( p_input, "x-end" );
2744 var_Destroy( p_input, "y-start" );
2745 var_Destroy( p_input, "y-end" );
2746 var_Destroy( p_input, "color" );
2747 var_Destroy( p_input, "menu-palette" );
2749 vlc_object_release( p_input );
2751 msg_Dbg( &demuxer, "Stopping the UI Hook" );
2753 b_ui_hooked = false;
2756 int demux_sys_t::EventMouse( vlc_object_t *p_this, char const *psz_var,
2757 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2759 event_thread_t *p_ev = (event_thread_t *) p_data;
2760 vlc_mutex_lock( &p_ev->lock );
2761 if( psz_var[6] == 'c' )
2763 p_ev->b_clicked = true;
2764 msg_Dbg( p_this, "Event Mouse: clicked");
2766 else if( psz_var[6] == 'm' )
2767 p_ev->b_moved = true;
2768 vlc_mutex_unlock( &p_ev->lock );
2773 int demux_sys_t::EventKey( vlc_object_t *p_this, char const *,
2774 vlc_value_t, vlc_value_t newval, void *p_data )
2776 event_thread_t *p_ev = (event_thread_t *) p_data;
2777 vlc_mutex_lock( &p_ev->lock );
2778 p_ev->i_key_action = newval.i_int;
2779 vlc_mutex_unlock( &p_ev->lock );
2780 msg_Dbg( p_this, "Event Key");
2785 int demux_sys_t::EventThread( vlc_object_t *p_this )
2787 event_thread_t *p_ev = (event_thread_t*)p_this;
2788 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
2789 vlc_object_t *p_vout = NULL;
2791 p_ev->b_moved = false;
2792 p_ev->b_clicked = false;
2793 p_ev->i_key_action = 0;
2795 /* catch all key event */
2796 var_AddCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
2799 while( vlc_object_alive (p_ev) )
2801 if ( !p_sys->b_pci_packet_set )
2808 bool b_activated = false;
2811 if( p_ev->i_key_action )
2815 msg_Dbg( p_ev->p_demux, "Handle Key Event");
2817 vlc_mutex_lock( &p_ev->lock );
2819 pci_t *pci = (pci_t *) &p_sys->pci_packet;
2821 uint16 i_curr_button = p_sys->dvd_interpretor.GetSPRM( 0x88 );
2823 switch( p_ev->i_key_action )
2825 case ACTIONID_NAV_LEFT:
2826 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2828 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2829 if ( p_button_ptr->left > 0 && p_button_ptr->left <= pci->hli.hl_gi.btn_ns )
2831 i_curr_button = p_button_ptr->left;
2832 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2833 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2834 if ( button_ptr.auto_action_mode )
2836 vlc_mutex_unlock( &p_ev->lock );
2837 vlc_mutex_lock( &p_sys->lock_demuxer );
2839 // process the button action
2840 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2842 vlc_mutex_unlock( &p_sys->lock_demuxer );
2843 vlc_mutex_lock( &p_ev->lock );
2848 case ACTIONID_NAV_RIGHT:
2849 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2851 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2852 if ( p_button_ptr->right > 0 && p_button_ptr->right <= pci->hli.hl_gi.btn_ns )
2854 i_curr_button = p_button_ptr->right;
2855 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2856 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2857 if ( button_ptr.auto_action_mode )
2859 vlc_mutex_unlock( &p_ev->lock );
2860 vlc_mutex_lock( &p_sys->lock_demuxer );
2862 // process the button action
2863 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2865 vlc_mutex_unlock( &p_sys->lock_demuxer );
2866 vlc_mutex_lock( &p_ev->lock );
2871 case ACTIONID_NAV_UP:
2872 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2874 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2875 if ( p_button_ptr->up > 0 && p_button_ptr->up <= pci->hli.hl_gi.btn_ns )
2877 i_curr_button = p_button_ptr->up;
2878 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2879 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2880 if ( button_ptr.auto_action_mode )
2882 vlc_mutex_unlock( &p_ev->lock );
2883 vlc_mutex_lock( &p_sys->lock_demuxer );
2885 // process the button action
2886 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2888 vlc_mutex_unlock( &p_sys->lock_demuxer );
2889 vlc_mutex_lock( &p_ev->lock );
2894 case ACTIONID_NAV_DOWN:
2895 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2897 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2898 if ( p_button_ptr->down > 0 && p_button_ptr->down <= pci->hli.hl_gi.btn_ns )
2900 i_curr_button = p_button_ptr->down;
2901 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2902 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2903 if ( button_ptr.auto_action_mode )
2905 vlc_mutex_unlock( &p_ev->lock );
2906 vlc_mutex_lock( &p_sys->lock_demuxer );
2908 // process the button action
2909 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2911 vlc_mutex_unlock( &p_sys->lock_demuxer );
2912 vlc_mutex_lock( &p_ev->lock );
2917 case ACTIONID_NAV_ACTIVATE:
2920 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2922 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2924 vlc_mutex_unlock( &p_ev->lock );
2925 vlc_mutex_lock( &p_sys->lock_demuxer );
2927 // process the button action
2928 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2930 vlc_mutex_unlock( &p_sys->lock_demuxer );
2931 vlc_mutex_lock( &p_ev->lock );
2937 p_ev->i_key_action = 0;
2938 vlc_mutex_unlock( &p_ev->lock );
2942 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
2944 vlc_value_t valx, valy;
2946 vlc_mutex_lock( &p_ev->lock );
2947 pci_t *pci = (pci_t *) &p_sys->pci_packet;
2948 var_Get( p_vout, "mouse-x", &valx );
2949 var_Get( p_vout, "mouse-y", &valy );
2951 if( p_ev->b_clicked )
2954 int32_t best,dist,d;
2955 int32_t mx,my,dx,dy;
2957 msg_Dbg( p_ev->p_demux, "Handle Mouse Event: Mouse clicked x(%d)*y(%d)", (unsigned)valx.i_int, (unsigned)valy.i_int);
2960 // get current button
2962 dist = 0x08000000; /* >> than (720*720)+(567*567); */
2963 for(button = 1; button <= pci->hli.hl_gi.btn_ns; button++)
2965 btni_t *button_ptr = &(pci->hli.btnit[button-1]);
2967 if(((unsigned)valx.i_int >= button_ptr->x_start)
2968 && ((unsigned)valx.i_int <= button_ptr->x_end)
2969 && ((unsigned)valy.i_int >= button_ptr->y_start)
2970 && ((unsigned)valy.i_int <= button_ptr->y_end))
2972 mx = (button_ptr->x_start + button_ptr->x_end)/2;
2973 my = (button_ptr->y_start + button_ptr->y_end)/2;
2974 dx = mx - valx.i_int;
2975 dy = my - valy.i_int;
2976 d = (dx*dx) + (dy*dy);
2977 /* If the mouse is within the button and the mouse is closer
2978 * to the center of this button then it is the best choice. */
2988 btni_t button_ptr = pci->hli.btnit[best-1];
2989 uint16 i_curr_button = p_sys->dvd_interpretor.GetSPRM( 0x88 );
2991 msg_Dbg( &p_sys->demuxer, "Clicked button %d", best );
2992 vlc_mutex_unlock( &p_ev->lock );
2993 vlc_mutex_lock( &p_sys->lock_demuxer );
2995 // process the button action
2996 p_sys->dvd_interpretor.SetSPRM( 0x88, best );
2997 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2999 msg_Dbg( &p_sys->demuxer, "Processed button %d", best );
3001 // select new button
3002 if ( best != i_curr_button )
3006 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
3008 vlc_mutex_t *p_mutex = (vlc_mutex_t *) val.p_address;
3011 if(button_ptr.btn_coln != 0) {
3012 i_palette = pci->hli.btn_colit.btn_coli[button_ptr.btn_coln-1][1];
3017 for( int i = 0; i < 4; i++ )
3019 uint32_t i_yuv = 0xFF;//p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
3020 uint8_t i_alpha = (i_palette>>(i*4))&0x0f;
3021 i_alpha = i_alpha == 0xf ? 0xff : i_alpha << 4;
3023 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
3024 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
3025 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
3026 p_sys->palette[i][3] = i_alpha;
3029 vlc_mutex_lock( p_mutex );
3030 val.i_int = button_ptr.x_start; var_Set( p_sys->p_input, "x-start", val );
3031 val.i_int = button_ptr.x_end; var_Set( p_sys->p_input, "x-end", val );
3032 val.i_int = button_ptr.y_start; var_Set( p_sys->p_input, "y-start", val );
3033 val.i_int = button_ptr.y_end; var_Set( p_sys->p_input, "y-end", val );
3035 val.p_address = (void *)p_sys->palette;
3036 var_Set( p_sys->p_input, "menu-palette", val );
3038 val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
3039 vlc_mutex_unlock( p_mutex );
3042 vlc_mutex_unlock( &p_sys->lock_demuxer );
3043 vlc_mutex_lock( &p_ev->lock );
3046 else if( p_ev->b_moved )
3048 // dvdnav_mouse_select( NULL, pci, valx.i_int, valy.i_int );
3051 p_ev->b_moved = false;
3052 p_ev->b_clicked = false;
3053 vlc_mutex_unlock( &p_ev->lock );
3057 if( p_vout && !vlc_object_alive (p_vout) )
3059 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3060 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3061 vlc_object_release( p_vout );
3065 else if( p_vout == NULL )
3067 p_vout = (vlc_object_t*) vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
3071 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3072 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3076 /* Wait a bit, 10ms */
3080 /* Release callback */
3083 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3084 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3085 vlc_object_release( p_vout );
3087 var_DelCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
3089 vlc_mutex_destroy( &p_ev->lock );
3094 void matroska_segment_c::UnSelect( )
3098 for( i_track = 0; i_track < tracks.size(); i_track++ )
3100 if ( tracks[i_track]->p_es != NULL )
3102 // es_format_Clean( &tracks[i_track]->fmt );
3103 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
3104 tracks[i_track]->p_es = NULL;
3111 void virtual_segment_c::PrepareChapters( )
3113 if ( linked_segments.size() == 0 )
3116 // !!! should be called only once !!!
3117 matroska_segment_c *p_segment;
3120 // copy editions from the first segment
3121 p_segment = linked_segments[0];
3122 p_editions = &p_segment->stored_editions;
3124 for ( i=1 ; i<linked_segments.size(); i++ )
3126 p_segment = linked_segments[i];
3127 // FIXME assume we have the same editions in all segments
3128 for (j=0; j<p_segment->stored_editions.size(); j++)
3130 if( j >= p_editions->size() ) /* Protect against broken files (?) */
3132 (*p_editions)[j]->Append( *p_segment->stored_editions[j] );
3137 std::string chapter_edition_c::GetMainName() const
3139 if ( sub_chapters.size() )
3141 return sub_chapters[0]->GetCodecName( true );
3146 int chapter_item_c::PublishChapters( input_title_t & title, int & i_user_chapters, int i_level )
3148 // add support for meta-elements from codec like DVD Titles
3149 if ( !b_display_seekpoint || psz_name == "" )
3151 psz_name = GetCodecName();
3152 if ( psz_name != "" )
3153 b_display_seekpoint = true;
3156 if (b_display_seekpoint)
3158 seekpoint_t *sk = vlc_seekpoint_New();
3160 sk->i_level = i_level;
3161 sk->i_time_offset = i_start_time;
3162 sk->psz_name = strdup( psz_name.c_str() );
3164 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
3165 title.i_seekpoint++;
3166 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
3167 title.seekpoint[title.i_seekpoint-1] = sk;
3169 if ( b_user_display )
3173 for ( size_t i=0; i<sub_chapters.size() ; i++)
3175 sub_chapters[i]->PublishChapters( title, i_user_chapters, i_level+1 );
3178 i_seekpoint_num = i_user_chapters;
3180 return i_user_chapters;
3183 bool virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
3185 demux_sys_t & sys = *demux.p_sys;
3186 chapter_item_c *psz_curr_chapter;
3187 bool b_has_seeked = false;
3189 /* update current chapter/seekpoint */
3190 if ( p_editions->size() )
3192 /* 1st, we need to know in which chapter we are */
3193 psz_curr_chapter = (*p_editions)[i_current_edition]->FindTimecode( sys.i_pts, psz_current_chapter );
3195 /* we have moved to a new chapter */
3196 if (psz_curr_chapter != NULL && psz_current_chapter != psz_curr_chapter)
3198 if ( (*p_editions)[i_current_edition]->b_ordered )
3200 // Leave/Enter up to the link point
3201 b_has_seeked = psz_curr_chapter->EnterAndLeave( psz_current_chapter );
3202 if ( !b_has_seeked )
3204 // only physically seek if necessary
3205 if ( psz_current_chapter == NULL || (psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time) )
3206 Seek( demux, sys.i_pts, 0, psz_curr_chapter );
3210 if ( !b_has_seeked )
3212 psz_current_chapter = psz_curr_chapter;
3213 if ( psz_curr_chapter->i_seekpoint_num > 0 )
3215 demux.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
3216 demux.info.i_title = sys.i_current_title = i_sys_title;
3217 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
3223 else if (psz_curr_chapter == NULL)
3225 // out of the scope of the data described by chapters, leave the edition
3226 if ( (*p_editions)[i_current_edition]->b_ordered && psz_current_chapter != NULL )
3228 if ( !(*p_editions)[i_current_edition]->EnterAndLeave( psz_current_chapter, false ) )
3229 psz_current_chapter = NULL;
3238 chapter_item_c *virtual_segment_c::BrowseCodecPrivate( unsigned int codec_id,
3239 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3240 const void *p_cookie,
3241 size_t i_cookie_size )
3243 // FIXME don't assume it is the first edition
3244 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
3245 if ( index != p_editions->end() )
3247 chapter_item_c *p_result = (*index)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3248 if ( p_result != NULL )
3254 chapter_item_c *virtual_segment_c::FindChapter( int64_t i_find_uid )
3256 // FIXME don't assume it is the first edition
3257 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
3258 if ( index != p_editions->end() )
3260 chapter_item_c *p_result = (*index)->FindChapter( i_find_uid );
3261 if ( p_result != NULL )
3267 chapter_item_c *chapter_item_c::BrowseCodecPrivate( unsigned int codec_id,
3268 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3269 const void *p_cookie,
3270 size_t i_cookie_size )
3273 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3274 while ( index != codecs.end() )
3276 if ( match( **index ,p_cookie, i_cookie_size ) )
3282 chapter_item_c *p_result = NULL;
3283 std::vector<chapter_item_c*>::const_iterator index2 = sub_chapters.begin();
3284 while ( index2 != sub_chapters.end() )
3286 p_result = (*index2)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3287 if ( p_result != NULL )
3295 void chapter_item_c::Append( const chapter_item_c & chapter )
3297 // we are appending content for the same chapter UID
3299 chapter_item_c *p_chapter;
3301 for ( i=0; i<chapter.sub_chapters.size(); i++ )
3303 p_chapter = FindChapter( chapter.sub_chapters[i]->i_uid );
3304 if ( p_chapter != NULL )
3306 p_chapter->Append( *chapter.sub_chapters[i] );
3310 sub_chapters.push_back( chapter.sub_chapters[i] );
3314 i_user_start_time = min( i_user_start_time, chapter.i_user_start_time );
3315 i_user_end_time = max( i_user_end_time, chapter.i_user_end_time );
3318 chapter_item_c * chapter_item_c::FindChapter( int64_t i_find_uid )
3321 chapter_item_c *p_result = NULL;
3323 if ( i_uid == i_find_uid )
3326 for ( i=0; i<sub_chapters.size(); i++)
3328 p_result = sub_chapters[i]->FindChapter( i_find_uid );
3329 if ( p_result != NULL )
3335 std::string chapter_item_c::GetCodecName( bool f_for_title ) const
3339 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3340 while ( index != codecs.end() )
3342 result = (*index)->GetCodecName( f_for_title );
3351 std::string dvd_chapter_codec_c::GetCodecName( bool f_for_title ) const
3354 if ( p_private_data->GetSize() >= 3)
3356 const binary* p_data = p_private_data->GetBuffer();
3357 /* if ( p_data[0] == MATROSKA_DVD_LEVEL_TT )
3359 uint16_t i_title = (p_data[1] << 8) + p_data[2];
3361 sprintf( psz_str, " %d ---", i_title );
3362 result = N_("--- DVD Title");
3365 else */ if ( p_data[0] == MATROSKA_DVD_LEVEL_LU )
3368 sprintf( psz_str, " (%c%c) ---", p_data[1], p_data[2] );
3369 result = N_("--- DVD Menu");
3372 else if ( p_data[0] == MATROSKA_DVD_LEVEL_SS && f_for_title )
3374 if ( p_data[1] == 0x00 )
3375 result = N_("First Played");
3376 else if ( p_data[1] == 0xC0 )
3377 result = N_("Video Manager");
3378 else if ( p_data[1] == 0x80 )
3380 uint16_t i_title = (p_data[2] << 8) + p_data[3];
3382 sprintf( psz_str, " %d -----", i_title );
3383 result = N_("----- Title");
3392 int16 chapter_item_c::GetTitleNumber( ) const
3396 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3397 while ( index != codecs.end() )
3399 result = (*index)->GetTitleNumber( );
3408 int16 dvd_chapter_codec_c::GetTitleNumber()
3410 if ( p_private_data->GetSize() >= 3)
3412 const binary* p_data = p_private_data->GetBuffer();
3413 if ( p_data[0] == MATROSKA_DVD_LEVEL_SS )
3415 return int16( (p_data[2] << 8) + p_data[3] );
3421 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter )
3423 demux_sys_t *p_sys = p_demux->p_sys;
3424 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
3425 matroska_segment_c *p_segment = p_vsegment->Segment();
3426 mtime_t i_time_offset = 0;
3430 msg_Dbg( p_demux, "seek request to %"PRId64" (%f%%)", i_date, f_percent );
3431 if( i_date < 0 && f_percent < 0 )
3433 msg_Warn( p_demux, "cannot seek nowhere !" );
3436 if( f_percent > 1.0 )
3438 msg_Warn( p_demux, "cannot seek so far !" );
3442 /* seek without index or without date */
3443 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
3445 if (p_sys->f_duration >= 0)
3447 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
3451 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
3453 msg_Dbg( p_demux, "inacurate way of seeking" );
3454 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
3456 if( p_segment->p_indexes[i_index].i_position >= i_pos)
3461 if( i_index == p_segment->i_index )
3466 i_date = p_segment->p_indexes[i_index].i_time;
3469 if( p_segment->p_indexes[i_index].i_position < i_pos )
3473 msg_Warn( p_demux, "searching for cluster, could take some time" );
3475 /* search a cluster */
3476 while( ( el = p_sys->ep->Get() ) != NULL )
3478 if( MKV_IS_ID( el, KaxCluster ) )
3480 KaxCluster *cluster = (KaxCluster*)el;
3482 /* add it to the index */
3483 p_segment->IndexAppendCluster( cluster );
3485 if( (int64_t)cluster->GetElementPosition() >= i_pos )
3487 p_sys->cluster = cluster;
3498 p_vsegment->Seek( *p_demux, i_date, i_time_offset, psz_chapter );
3501 /*****************************************************************************
3502 * Demux: reads and demuxes data packets
3503 *****************************************************************************
3504 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
3505 *****************************************************************************/
3506 static int Demux( demux_t *p_demux)
3508 demux_sys_t *p_sys = p_demux->p_sys;
3510 vlc_mutex_lock( &p_sys->lock_demuxer );
3512 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
3513 matroska_segment_c *p_segment = p_vsegment->Segment();
3514 if ( p_segment == NULL ) return 0;
3515 int i_block_count = 0;
3520 if ( p_sys->demuxer.b_die )
3523 if( p_sys->i_pts >= p_sys->i_start_pts )
3524 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
3530 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
3532 /* nothing left to read in this ordered edition */
3533 if ( !p_vsegment->SelectNext() )
3535 p_segment->UnSelect( );
3537 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3539 /* switch to the next segment */
3540 p_segment = p_vsegment->Segment();
3541 if ( !p_segment->Select( 0 ) )
3543 msg_Err( p_demux, "Failed to select new segment" );
3550 int64_t i_block_duration = 0;
3551 int64_t i_block_ref1;
3552 int64_t i_block_ref2;
3554 #if LIBMATROSKA_VERSION >= 0x000800
3555 KaxSimpleBlock *simpleblock;
3557 if( p_segment->BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
3559 if( p_segment->BlockGet( block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
3562 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered )
3564 const chapter_item_c *p_chap = p_vsegment->CurrentChapter();
3565 // check if there are more chapters to read
3566 if ( p_chap != NULL )
3568 /* TODO handle successive chapters with the same user_start_time/user_end_time
3569 if ( p_chap->i_user_start_time == p_chap->i_user_start_time )
3570 p_vsegment->SelectNext();
3572 p_sys->i_pts = p_chap->i_user_end_time;
3573 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
3582 msg_Warn( p_demux, "cannot get block EOF?" );
3583 p_segment->UnSelect( );
3585 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3587 /* switch to the next segment */
3588 if ( !p_vsegment->SelectNext() )
3589 // no more segments in this stream
3591 p_segment = p_vsegment->Segment();
3592 if ( !p_segment->Select( 0 ) )
3594 msg_Err( p_demux, "Failed to select new segment" );
3602 #if LIBMATROSKA_VERSION >= 0x000800
3603 if ( simpleblock != NULL )
3604 p_sys->i_pts = (p_sys->i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
3607 p_sys->i_pts = (p_sys->i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
3609 if( p_sys->i_pts >= p_sys->i_start_pts )
3611 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
3613 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
3621 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
3623 /* nothing left to read in this ordered edition */
3624 if ( !p_vsegment->SelectNext() )
3629 p_segment->UnSelect( );
3631 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3633 /* switch to the next segment */
3634 p_segment = p_vsegment->Segment();
3635 if ( !p_segment->Select( 0 ) )
3637 msg_Err( p_demux, "Failed to select new segment" );
3645 #if LIBMATROSKA_VERSION >= 0x000800
3646 BlockDecode( p_demux, block, simpleblock, p_sys->i_pts, i_block_duration, i_block_ref1 >= 0 || i_block_ref2 > 0 );
3648 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration, i_block_ref1 >= 0 || i_block_ref2 > 0 );
3654 // TODO optimize when there is need to leave or when seeking has been called
3655 if( i_block_count > 5 )
3662 vlc_mutex_unlock( &p_sys->lock_demuxer );
3669 /*****************************************************************************
3671 *****************************************************************************/
3672 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_, bool b_owner_ )
3679 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
3681 if( i_size <= 0 || mb_eof )
3686 return stream_Read( s, p_buffer, i_size );
3688 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
3694 case seek_beginning:
3698 i_pos = stream_Size( s ) - i_offset;
3701 i_pos= stream_Tell( s ) + i_offset;
3705 if( i_pos < 0 || i_pos >= stream_Size( s ) )
3712 if( stream_Seek( s, i_pos ) )
3718 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
3722 uint64 vlc_stream_io_callback::getFilePointer( void )
3726 return stream_Tell( s );
3728 void vlc_stream_io_callback::close( void )
3734 /*****************************************************************************
3735 * Ebml Stream parser
3736 *****************************************************************************/
3737 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux )
3744 mi_remain_size[0] = el_start->GetSize();
3746 for( i = 1; i < 6; i++ )
3753 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
3756 EbmlParser::~EbmlParser( void )
3760 for( i = 1; i < mi_level; i++ )
3770 EbmlElement* EbmlParser::UnGet( uint64 i_block_pos, uint64 i_cluster_pos )
3772 if ( mi_user_level > mi_level )
3774 while ( mi_user_level != mi_level )
3776 delete m_el[mi_user_level];
3777 m_el[mi_user_level] = NULL;
3783 if ( m_el[1]->GetElementPosition() == i_cluster_pos )
3785 m_es->I_O().setFilePointer( i_block_pos, seek_beginning );
3786 return (EbmlMaster*) m_el[1];
3790 // seek to the previous Cluster
3791 m_es->I_O().setFilePointer( i_cluster_pos, seek_beginning );
3794 delete m_el[mi_level];
3795 m_el[mi_level] = NULL;
3800 void EbmlParser::Up( void )
3802 if( mi_user_level == mi_level )
3804 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
3810 void EbmlParser::Down( void )
3816 void EbmlParser::Keep( void )
3821 int EbmlParser::GetLevel( void )
3823 return mi_user_level;
3826 void EbmlParser::Reset( demux_t *p_demux )
3828 while ( mi_level > 0)
3830 delete m_el[mi_level];
3831 m_el[mi_level] = NULL;
3834 mi_user_level = mi_level = 1;
3835 #if LIBEBML_VERSION >= 0x000704
3836 // a little faster and cleaner
3837 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
3839 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
3841 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
3844 EbmlElement *EbmlParser::Get( void )
3848 if( mi_user_level != mi_level )
3854 EbmlElement *ret = m_got;
3860 if( m_el[mi_level] )
3862 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
3865 delete m_el[mi_level];
3870 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, mb_dummy != 0, 1 );
3871 // mi_remain_size[mi_level] = m_el[mi_level]->GetSize();
3882 delete m_el[mi_level - 1];
3883 m_got = m_el[mi_level -1] = m_el[mi_level];
3884 m_el[mi_level] = NULL;
3891 else if( m_el[mi_level] == NULL )
3893 fprintf( stderr," m_el[mi_level] == NULL\n" );
3896 return m_el[mi_level];
3900 /*****************************************************************************
3902 * * LoadCues : load the cues element and update index
3904 * * LoadTags : load ... the tags element
3906 * * InformationCreate : create all information, load tags if present
3908 *****************************************************************************/
3909 void matroska_segment_c::LoadCues( )
3911 int64_t i_sav_position = es.I_O().getFilePointer();
3913 EbmlElement *el, *cues;
3915 /* *** Load the cue if found *** */
3916 if( i_cues_position < 0 )
3918 msg_Warn( &sys.demuxer, "no cues/empty cues found->seek won't be precise" );
3920 // IndexAppendCluster( cluster );
3925 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
3929 msg_Dbg( &sys.demuxer, "loading cues" );
3930 es.I_O().setFilePointer( i_cues_position, seek_beginning );
3931 cues = es.FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
3935 msg_Err( &sys.demuxer, "cannot load cues (broken seekhead or file)" );
3936 es.I_O().setFilePointer( i_sav_position, seek_beginning );
3940 ep = new EbmlParser( &es, cues, &sys.demuxer );
3941 while( ( el = ep->Get() ) != NULL )
3943 if( MKV_IS_ID( el, KaxCuePoint ) )
3945 #define idx p_indexes[i_index]
3948 idx.i_block_number= -1;
3949 idx.i_position = -1;
3954 while( ( el = ep->Get() ) != NULL )
3956 if( MKV_IS_ID( el, KaxCueTime ) )
3958 KaxCueTime &ctime = *(KaxCueTime*)el;
3960 ctime.ReadData( es.I_O() );
3962 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
3964 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
3967 while( ( el = ep->Get() ) != NULL )
3969 if( MKV_IS_ID( el, KaxCueTrack ) )
3971 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
3973 ctrack.ReadData( es.I_O() );
3974 idx.i_track = uint16( ctrack );
3976 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
3978 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
3980 ccpos.ReadData( es.I_O() );
3981 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
3983 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
3985 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
3987 cbnum.ReadData( es.I_O() );
3988 idx.i_block_number = uint32( cbnum );
3992 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
3999 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
4005 msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
4006 " track=%d bnum=%d", idx.i_time, idx.i_position,
4007 idx.i_track, idx.i_block_number );
4011 if( i_index >= i_index_max )
4013 i_index_max += 1024;
4014 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
4020 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
4028 msg_Dbg( &sys.demuxer, "loading cues done." );
4029 es.I_O().setFilePointer( i_sav_position, seek_beginning );
4032 void matroska_segment_c::LoadTags( )
4034 int64_t i_sav_position = es.I_O().getFilePointer();
4036 EbmlElement *el, *tags;
4038 msg_Dbg( &sys.demuxer, "loading tags" );
4039 es.I_O().setFilePointer( i_tags_position, seek_beginning );
4040 tags = es.FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
4044 msg_Err( &sys.demuxer, "cannot load tags (broken seekhead or file)" );
4045 es.I_O().setFilePointer( i_sav_position, seek_beginning );
4049 msg_Dbg( &sys.demuxer, "Tags" );
4050 ep = new EbmlParser( &es, tags, &sys.demuxer );
4051 while( ( el = ep->Get() ) != NULL )
4053 if( MKV_IS_ID( el, KaxTag ) )
4055 msg_Dbg( &sys.demuxer, "+ Tag" );
4057 while( ( el = ep->Get() ) != NULL )
4059 if( MKV_IS_ID( el, KaxTagTargets ) )
4061 msg_Dbg( &sys.demuxer, "| + Targets" );
4063 while( ( el = ep->Get() ) != NULL )
4065 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4069 else if( MKV_IS_ID( el, KaxTagGeneral ) )
4071 msg_Dbg( &sys.demuxer, "| + General" );
4073 while( ( el = ep->Get() ) != NULL )
4075 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4079 else if( MKV_IS_ID( el, KaxTagGenres ) )
4081 msg_Dbg( &sys.demuxer, "| + Genres" );
4083 while( ( el = ep->Get() ) != NULL )
4085 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4089 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
4091 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
4093 while( ( el = ep->Get() ) != NULL )
4095 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4099 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
4101 msg_Dbg( &sys.demuxer, "| + Images Specific" );
4103 while( ( el = ep->Get() ) != NULL )
4105 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4109 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
4111 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
4113 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
4115 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
4117 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
4119 msg_Dbg( &sys.demuxer, "| + Multi Date" );
4121 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
4123 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
4125 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
4127 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
4129 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
4131 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
4133 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
4135 msg_Dbg( &sys.demuxer, "| + Multi Title" );
4139 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid( *el ).name() );
4146 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
4152 msg_Dbg( &sys.demuxer, "loading tags done." );
4153 es.I_O().setFilePointer( i_sav_position, seek_beginning );
4156 /*****************************************************************************
4158 *****************************************************************************/
4159 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
4163 int i_upper_level = 0;
4165 msg_Dbg( &sys.demuxer, "| + Seek head" );
4167 /* Master elements */
4168 seekhead->Read( es, seekhead->Generic().Context, i_upper_level, el, true );
4170 for( i = 0; i < seekhead->ListSize(); i++ )
4172 EbmlElement *l = (*seekhead)[i];
4174 if( MKV_IS_ID( l, KaxSeek ) )
4176 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
4177 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
4180 for( j = 0; j < sk->ListSize(); j++ )
4182 EbmlElement *l = (*sk)[j];
4184 if( MKV_IS_ID( l, KaxSeekID ) )
4186 KaxSeekID &sid = *(KaxSeekID*)l;
4187 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
4189 else if( MKV_IS_ID( l, KaxSeekPosition ) )
4191 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
4192 i_pos = uint64( spos );
4196 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
4202 if( id == KaxCues::ClassInfos.GlobalId )
4204 msg_Dbg( &sys.demuxer, "| | | = cues at %"PRId64, i_pos );
4205 i_cues_position = segment->GetGlobalPosition( i_pos );
4207 else if( id == KaxChapters::ClassInfos.GlobalId )
4209 msg_Dbg( &sys.demuxer, "| | | = chapters at %"PRId64, i_pos );
4210 i_chapters_position = segment->GetGlobalPosition( i_pos );
4212 else if( id == KaxTags::ClassInfos.GlobalId )
4214 msg_Dbg( &sys.demuxer, "| | | = tags at %"PRId64, i_pos );
4215 i_tags_position = segment->GetGlobalPosition( i_pos );
4221 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4226 /*****************************************************************************
4228 *****************************************************************************/
4229 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
4232 bool bSupported = true;
4236 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
4238 tk = new mkv_track_t();
4240 /* Init the track */
4241 memset( tk, 0, sizeof( mkv_track_t ) );
4243 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
4244 tk->fmt.psz_language = strdup("English");
4245 tk->fmt.psz_description = NULL;
4247 tk->b_default = true;
4248 tk->b_enabled = true;
4249 tk->b_silent = false;
4250 tk->i_number = tracks.size() - 1;
4251 tk->i_extra_data = 0;
4252 tk->p_extra_data = NULL;
4253 tk->psz_codec = NULL;
4254 tk->i_default_duration = 0;
4255 tk->f_timecodescale = 1.0;
4257 tk->b_inited = false;
4258 tk->i_data_init = 0;
4259 tk->p_data_init = NULL;
4261 tk->psz_codec_name = NULL;
4262 tk->psz_codec_settings = NULL;
4263 tk->psz_codec_info_url = NULL;
4264 tk->psz_codec_download_url = NULL;
4266 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
4267 tk->p_compression_data = NULL;
4269 for( i = 0; i < m->ListSize(); i++ )
4271 EbmlElement *l = (*m)[i];
4273 if( MKV_IS_ID( l, KaxTrackNumber ) )
4275 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
4277 tk->i_number = uint32( tnum );
4278 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
4280 else if( MKV_IS_ID( l, KaxTrackUID ) )
4282 KaxTrackUID &tuid = *(KaxTrackUID*)l;
4284 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
4286 else if( MKV_IS_ID( l, KaxTrackType ) )
4288 const char *psz_type;
4289 KaxTrackType &ttype = *(KaxTrackType*)l;
4291 switch( uint8(ttype) )
4295 tk->fmt.i_cat = AUDIO_ES;
4299 tk->fmt.i_cat = VIDEO_ES;
4301 case track_subtitle:
4302 psz_type = "subtitle";
4303 tk->fmt.i_cat = SPU_ES;
4306 psz_type = "buttons";
4307 tk->fmt.i_cat = SPU_ES;
4310 psz_type = "unknown";
4311 tk->fmt.i_cat = UNKNOWN_ES;
4315 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
4317 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
4319 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
4321 // tk->b_enabled = uint32( fenb );
4322 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
4323 // uint32( fenb ) );
4325 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
4327 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
4329 tk->b_default = uint32( fdef );
4330 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
4332 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
4334 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
4336 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
4338 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
4340 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
4342 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
4344 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
4346 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
4348 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
4350 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
4352 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
4354 tk->i_default_duration = uint64(defd);
4355 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%"PRId64, uint64(defd) );
4357 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
4359 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
4361 tk->f_timecodescale = float( ttcs );
4362 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
4364 else if( MKV_IS_ID( l, KaxTrackName ) )
4366 KaxTrackName &tname = *(KaxTrackName*)l;
4368 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
4369 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
4371 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
4373 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
4375 if ( tk->fmt.psz_language != NULL )
4376 free( tk->fmt.psz_language );
4377 tk->fmt.psz_language = strdup( string( lang ).c_str() );
4378 msg_Dbg( &sys.demuxer,
4379 "| | | + Track Language=`%s'", tk->fmt.psz_language );
4381 else if( MKV_IS_ID( l, KaxCodecID ) )
4383 KaxCodecID &codecid = *(KaxCodecID*)l;
4385 tk->psz_codec = strdup( string( codecid ).c_str() );
4386 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
4388 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
4390 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
4392 tk->i_extra_data = cpriv.GetSize();
4393 if( tk->i_extra_data > 0 )
4395 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
4396 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
4398 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
4400 else if( MKV_IS_ID( l, KaxCodecName ) )
4402 KaxCodecName &cname = *(KaxCodecName*)l;
4404 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
4405 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
4407 else if( MKV_IS_ID( l, KaxContentEncodings ) )
4409 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
4410 MkvTree( sys.demuxer, 3, "Content Encodings" );
4411 if ( cencs->ListSize() > 1 )
4413 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
4416 for( j = 0; j < cencs->ListSize(); j++ )
4418 EbmlElement *l2 = (*cencs)[j];
4419 if( MKV_IS_ID( l2, KaxContentEncoding ) )
4421 MkvTree( sys.demuxer, 4, "Content Encoding" );
4422 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
4423 for( k = 0; k < cenc->ListSize(); k++ )
4425 EbmlElement *l3 = (*cenc)[k];
4426 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
4428 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
4429 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
4431 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
4433 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
4434 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
4436 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
4438 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
4439 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
4441 else if( MKV_IS_ID( l3, KaxContentCompression ) )
4443 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
4444 MkvTree( sys.demuxer, 5, "Content Compression" );
4445 for( n = 0; n < compr->ListSize(); n++ )
4447 EbmlElement *l4 = (*compr)[n];
4448 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
4450 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
4451 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
4452 tk->i_compression_type = uint32( compalg );
4453 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
4454 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
4456 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
4460 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
4462 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
4466 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
4472 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
4478 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
4482 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
4484 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
4486 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
4487 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
4489 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
4491 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
4493 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
4494 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
4496 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
4498 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
4500 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
4501 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
4503 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
4505 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
4507 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
4509 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
4511 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
4513 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
4515 else if( MKV_IS_ID( l, KaxTrackVideo ) )
4517 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
4519 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
4520 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
4522 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
4525 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
4526 tk->fmt.video.i_frame_rate = 1000000;
4528 for( j = 0; j < tkv->ListSize(); j++ )
4530 EbmlElement *l = (*tkv)[j];
4531 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
4533 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
4535 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
4537 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
4539 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
4541 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
4544 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
4546 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
4548 tk->fmt.video.i_width += uint16( vwidth );
4549 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
4551 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
4553 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
4555 tk->fmt.video.i_height += uint16( vheight );
4556 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
4558 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
4560 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
4562 i_display_width = uint16( vwidth );
4563 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
4565 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
4567 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
4569 i_display_height = uint16( vheight );
4570 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
4572 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
4574 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
4576 i_crop_bottom = uint16( cropval );
4577 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
4579 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
4581 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
4583 i_crop_top = uint16( cropval );
4584 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
4586 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
4588 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
4590 i_crop_right = uint16( cropval );
4591 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
4593 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
4595 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
4597 i_crop_left = uint16( cropval );
4598 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
4600 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
4602 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
4604 tk->f_fps = float( vfps );
4605 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
4607 else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
4609 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
4611 i_display_unit = uint8( vdmode );
4612 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
4613 uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
4615 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
4617 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
4619 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
4621 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
4623 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
4625 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
4629 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
4632 if( i_display_height && i_display_width )
4633 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * i_display_width / i_display_height;
4634 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
4636 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
4637 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
4638 tk->fmt.video.i_x_offset = i_crop_left;
4639 tk->fmt.video.i_y_offset = i_crop_top;
4640 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
4641 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
4643 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
4644 we do not support this atm */
4646 else if( MKV_IS_ID( l, KaxTrackAudio ) )
4648 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
4651 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
4653 for( j = 0; j < tka->ListSize(); j++ )
4655 EbmlElement *l = (*tka)[j];
4657 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
4659 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
4661 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
4662 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
4664 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
4666 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
4668 tk->fmt.audio.i_rate = (int)float( afreq );
4669 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
4671 else if( MKV_IS_ID( l, KaxAudioChannels ) )
4673 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
4675 tk->fmt.audio.i_channels = uint8( achan );
4676 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
4678 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
4680 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
4682 tk->fmt.audio.i_bitspersample = uint8( abits );
4683 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
4687 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
4693 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
4694 typeid(*l).name() );
4700 tracks.push_back( tk );
4704 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
4709 /*****************************************************************************
4711 *****************************************************************************/
4712 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
4716 int i_upper_level = 0;
4718 msg_Dbg( &sys.demuxer, "| + Tracks" );
4720 /* Master elements */
4721 tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
4723 for( i = 0; i < tracks->ListSize(); i++ )
4725 EbmlElement *l = (*tracks)[i];
4727 if( MKV_IS_ID( l, KaxTrackEntry ) )
4729 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
4733 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4738 /*****************************************************************************
4740 *****************************************************************************/
4741 void matroska_segment_c::ParseInfo( KaxInfo *info )
4746 int i_upper_level = 0;
4748 msg_Dbg( &sys.demuxer, "| + Information" );
4750 /* Master elements */
4751 m = static_cast<EbmlMaster *>(info);
4752 m->Read( es, info->Generic().Context, i_upper_level, el, true );
4754 for( i = 0; i < m->ListSize(); i++ )
4756 EbmlElement *l = (*m)[i];
4758 if( MKV_IS_ID( l, KaxSegmentUID ) )
4760 if ( p_segment_uid == NULL )
4761 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
4763 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
4765 else if( MKV_IS_ID( l, KaxPrevUID ) )
4767 if ( p_prev_segment_uid == NULL )
4768 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
4770 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
4772 else if( MKV_IS_ID( l, KaxNextUID ) )
4774 if ( p_next_segment_uid == NULL )
4775 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
4777 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
4779 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
4781 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
4783 i_timescale = uint64(tcs);
4785 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%"PRId64,
4788 else if( MKV_IS_ID( l, KaxDuration ) )
4790 KaxDuration &dur = *(KaxDuration*)l;
4792 i_duration = mtime_t( double( dur ) );
4794 msg_Dbg( &sys.demuxer, "| | + Duration=%"PRId64,
4797 else if( MKV_IS_ID( l, KaxMuxingApp ) )
4799 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
4801 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
4803 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
4804 psz_muxing_application );
4806 else if( MKV_IS_ID( l, KaxWritingApp ) )
4808 KaxWritingApp &wapp = *(KaxWritingApp*)l;
4810 psz_writing_application = ToUTF8( UTFstring( wapp ) );
4812 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
4813 psz_writing_application );
4815 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
4817 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
4819 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
4821 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
4822 psz_segment_filename );
4824 else if( MKV_IS_ID( l, KaxTitle ) )
4826 KaxTitle &title = *(KaxTitle*)l;
4828 psz_title = ToUTF8( UTFstring( title ) );
4830 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
4832 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
4834 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
4836 families.push_back( new KaxSegmentFamily(*uid) );
4838 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
4840 #if defined( HAVE_GMTIME_R ) && !defined( __APPLE__ )
4841 else if( MKV_IS_ID( l, KaxDateUTC ) )
4843 KaxDateUTC &date = *(KaxDateUTC*)l;
4848 i_date = date.GetEpochDate();
4849 memset( buffer, 0, 256 );
4850 if( gmtime_r( &i_date, &tmres ) &&
4851 asctime_r( &tmres, buffer ) )
4853 buffer[strlen( buffer)-1]= '\0';
4854 psz_date_utc = strdup( buffer );
4855 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
4859 #if LIBMATROSKA_VERSION >= 0x000704
4860 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
4862 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
4863 chapter_translation_c *p_translate = new chapter_translation_c();
4865 p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
4866 for( j = 0; j < p_trans->ListSize(); j++ )
4868 EbmlElement *l = (*p_trans)[j];
4870 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
4872 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
4874 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
4876 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
4878 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
4880 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
4884 translations.push_back( p_translate );
4889 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4893 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
4894 i_duration = mtime_t(f_dur);
4898 /*****************************************************************************
4900 *****************************************************************************/
4901 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
4905 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
4906 for( i = 0; i < ca->ListSize(); i++ )
4908 EbmlElement *l = (*ca)[i];
4910 if( MKV_IS_ID( l, KaxChapterUID ) )
4912 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
4913 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
4915 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
4917 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
4918 chapters.b_display_seekpoint = uint8( flag ) == 0;
4920 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
4922 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
4924 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
4925 chapters.i_start_time = uint64( start ) / INT64_C(1000);
4927 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
4929 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
4931 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
4932 chapters.i_end_time = uint64( end ) / INT64_C(1000);
4934 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
4936 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
4938 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
4940 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
4941 for( j = 0; j < cd->ListSize(); j++ )
4943 EbmlElement *l= (*cd)[j];
4945 if( MKV_IS_ID( l, KaxChapterString ) )
4949 KaxChapterString &name =*(KaxChapterString*)l;
4950 for (k = 0; k < i_level; k++)
4951 chapters.psz_name += '+';
4952 chapters.psz_name += ' ';
4953 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
4954 chapters.psz_name += psz_tmp_utf8;
4955 chapters.b_user_display = true;
4957 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
4958 free( psz_tmp_utf8 );
4960 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
4962 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
4963 const char *psz = string( lang ).c_str();
4965 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
4967 else if( MKV_IS_ID( l, KaxChapterCountry ) )
4969 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
4970 const char *psz = string( ct ).c_str();
4972 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
4976 else if( MKV_IS_ID( l, KaxChapterProcess ) )
4978 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
4980 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
4981 chapter_codec_cmds_c *p_ccodec = NULL;
4983 for( j = 0; j < cp->ListSize(); j++ )
4985 EbmlElement *k= (*cp)[j];
4987 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
4989 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
4990 if ( uint32(*p_codec_id) == 0 )
4991 p_ccodec = new matroska_script_codec_c( sys );
4992 else if ( uint32(*p_codec_id) == 1 )
4993 p_ccodec = new dvd_chapter_codec_c( sys );
4998 if ( p_ccodec != NULL )
5000 for( j = 0; j < cp->ListSize(); j++ )
5002 EbmlElement *k= (*cp)[j];
5004 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
5006 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
5007 p_ccodec->SetPrivate( *p_private );
5009 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
5011 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
5014 chapters.codecs.push_back( p_ccodec );
5017 else if( MKV_IS_ID( l, KaxChapterAtom ) )
5019 chapter_item_c *new_sub_chapter = new chapter_item_c();
5020 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
5021 new_sub_chapter->psz_parent = &chapters;
5022 chapters.sub_chapters.push_back( new_sub_chapter );
5027 /*****************************************************************************
5029 *****************************************************************************/
5030 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
5033 int i_upper_level = 0;
5035 attachments->Read( es, attachments->Generic().Context, i_upper_level, el, true );
5037 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
5039 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
5041 std::string psz_mime_type = GetChild<KaxMimeType>( *attachedFile );
5042 KaxFileName &file_name = GetChild<KaxFileName>( *attachedFile );
5043 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
5045 attachment_c *new_attachment = new attachment_c();
5047 if( new_attachment )
5049 new_attachment->psz_file_name = ToUTF8( UTFstring( file_name ) );
5050 new_attachment->psz_mime_type = psz_mime_type;
5051 new_attachment->i_size = img_data.GetSize();
5052 new_attachment->p_data = malloc( img_data.GetSize() );
5054 if( new_attachment->p_data )
5056 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
5057 sys.stored_attachments.push_back( new_attachment );
5061 delete new_attachment;
5065 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
5069 /*****************************************************************************
5071 *****************************************************************************/
5072 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
5076 int i_upper_level = 0;
5079 /* Master elements */
5080 chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
5082 for( i = 0; i < chapters->ListSize(); i++ )
5084 EbmlElement *l = (*chapters)[i];
5086 if( MKV_IS_ID( l, KaxEditionEntry ) )
5088 chapter_edition_c *p_edition = new chapter_edition_c();
5090 EbmlMaster *E = static_cast<EbmlMaster *>(l );
5092 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
5093 for( j = 0; j < E->ListSize(); j++ )
5095 EbmlElement *l = (*E)[j];
5097 if( MKV_IS_ID( l, KaxChapterAtom ) )
5099 chapter_item_c *new_sub_chapter = new chapter_item_c();
5100 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
5101 p_edition->sub_chapters.push_back( new_sub_chapter );
5103 else if( MKV_IS_ID( l, KaxEditionUID ) )
5105 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
5107 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
5109 p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
5111 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
5113 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
5114 i_default_edition = stored_editions.size();
5118 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
5121 stored_editions.push_back( p_edition );
5125 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
5129 for( i = 0; i < stored_editions.size(); i++ )
5131 stored_editions[i]->RefreshChapters( );
5134 if ( stored_editions.size() != 0 && stored_editions[i_default_edition]->b_ordered )
5136 /* update the duration of the segment according to the sum of all sub chapters */
5137 i_dur = stored_editions[i_default_edition]->Duration() / INT64_C(1000);
5143 void matroska_segment_c::ParseCluster( )
5148 int i_upper_level = 0;
5150 /* Master elements */
5151 m = static_cast<EbmlMaster *>( cluster );
5152 m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
5154 for( i = 0; i < m->ListSize(); i++ )
5156 EbmlElement *l = (*m)[i];
5158 if( MKV_IS_ID( l, KaxClusterTimecode ) )
5160 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
5162 cluster->InitTimecode( uint64( ctc ), i_timescale );
5167 i_start_time = cluster->GlobalTimecode() / 1000;
5170 /*****************************************************************************
5171 * InformationCreate:
5172 *****************************************************************************/
5173 void matroska_segment_c::InformationCreate( )
5175 sys.meta = vlc_meta_New();
5179 vlc_meta_SetTitle( sys.meta, psz_title );
5183 vlc_meta_SetDate( sys.meta, psz_date_utc );
5186 if( psz_segment_filename )
5188 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5190 if( psz_muxing_application )
5192 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5194 if( psz_writing_application )
5196 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5199 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
5201 // mkv_track_t *tk = tracks[i_track];
5202 // vlc_meta_t *mtk = vlc_meta_New();
5203 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
5207 if( i_tags_position >= 0 )
5211 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
5220 /*****************************************************************************
5222 *****************************************************************************/
5224 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
5226 #define idx p_indexes[i_index]
5228 idx.i_block_number= -1;
5229 idx.i_position = cluster->GetElementPosition();
5234 if( i_index >= i_index_max )
5236 i_index_max += 1024;
5237 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
5242 void chapter_edition_c::RefreshChapters( )
5244 chapter_item_c::RefreshChapters( b_ordered, -1 );
5245 b_display_seekpoint = false;
5248 int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_time )
5250 int64_t i_user_time = i_prev_user_time;
5252 // first the sub-chapters, and then ourself
5253 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
5254 while ( index != sub_chapters.end() )
5256 i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
5262 // the ordered chapters always start at zero
5263 if ( i_prev_user_time == -1 )
5265 if ( i_user_time == -1 )
5267 i_prev_user_time = 0;
5270 i_user_start_time = i_prev_user_time;
5271 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
5273 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
5277 i_user_end_time = i_user_time;
5282 if ( sub_chapters.begin() != sub_chapters.end() )
5283 std::sort( sub_chapters.begin(), sub_chapters.end(), chapter_item_c::CompareTimecode );
5284 i_user_start_time = i_start_time;
5285 if ( i_end_time != -1 )
5286 i_user_end_time = i_end_time;
5287 else if ( i_user_time != -1 )
5288 i_user_end_time = i_user_time;
5290 i_user_end_time = i_user_start_time;
5293 return i_user_end_time;
5296 mtime_t chapter_edition_c::Duration() const
5298 mtime_t i_result = 0;
5300 if ( sub_chapters.size() )
5302 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
5304 i_result = (*index)->i_user_end_time;
5310 chapter_item_c * chapter_edition_c::FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current )
5314 bool b_found_current = false;
5315 return chapter_item_c::FindTimecode( i_timecode, p_current, b_found_current );
5318 chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode, const chapter_item_c * p_current, bool & b_found )
5320 chapter_item_c *psz_result = NULL;
5322 if ( p_current == this )
5325 if ( i_user_timecode >= i_user_start_time &&
5326 ( i_user_timecode < i_user_end_time ||
5327 ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
5329 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
5330 while ( index != sub_chapters.end() && ((p_current == NULL && psz_result == NULL) || (p_current != NULL && (!b_found || psz_result == NULL))))
5332 psz_result = (*index)->FindTimecode( i_user_timecode, p_current, b_found );
5336 if ( psz_result == NULL )
5343 bool chapter_item_c::ParentOf( const chapter_item_c & item ) const
5345 if ( &item == this )
5348 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.begin();
5349 while ( index != sub_chapters.end() )
5351 if ( (*index)->ParentOf( item ) )
5359 void demux_sys_t::PreloadFamily( const matroska_segment_c & of_segment )
5361 for (size_t i=0; i<opened_segments.size(); i++)
5363 opened_segments[i]->PreloadFamily( of_segment );
5366 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
5371 for (size_t i=0; i<families.size(); i++)
5373 for (size_t j=0; j<of_segment.families.size(); j++)
5375 if ( *(families[i]) == *(of_segment.families[j]) )
5383 // preload all the linked segments for all preloaded segments
5384 void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
5386 size_t i_preloaded, i, j;
5387 virtual_segment_c *p_seg;
5389 p_current_segment = VirtualFromSegments( p_segment );
5391 used_segments.push_back( p_current_segment );
5393 // create all the other virtual segments of the family
5396 for ( i=0; i< opened_segments.size(); i++ )
5398 if ( opened_segments[i]->b_preloaded && !IsUsedSegment( *opened_segments[i] ) )
5400 p_seg = VirtualFromSegments( opened_segments[i] );
5401 used_segments.push_back( p_seg );
5405 } while ( i_preloaded ); // worst case: will stop when all segments are found as family related
5407 // publish all editions of all usable segment
5408 for ( i=0; i< used_segments.size(); i++ )
5410 p_seg = used_segments[i];
5411 if ( p_seg->p_editions != NULL )
5413 std::string sz_name;
5414 input_title_t *p_title = vlc_input_title_New();
5415 p_seg->i_sys_title = i;
5418 // TODO use a name for each edition, let the TITLE deal with a codec name
5419 for ( j=0; j<p_seg->p_editions->size(); j++ )
5421 if ( p_title->psz_name == NULL )
5423 sz_name = (*p_seg->p_editions)[j]->GetMainName();
5424 if ( sz_name != "" )
5425 p_title->psz_name = strdup( sz_name.c_str() );
5428 chapter_edition_c *p_edition = (*p_seg->p_editions)[j];
5431 p_edition->PublishChapters( *p_title, i_chapters, 0 );
5434 // create a name if there is none
5435 if ( p_title->psz_name == NULL )
5437 sz_name = N_("Segment");
5439 sprintf( psz_str, " %d", (int)i );
5441 p_title->psz_name = strdup( sz_name.c_str() );
5444 titles.push_back( p_title );
5448 // TODO decide which segment should be first used (VMG for DVD)
5451 bool demux_sys_t::IsUsedSegment( matroska_segment_c &segment ) const
5453 for ( size_t i=0; i< used_segments.size(); i++ )
5455 if ( used_segments[i]->FindUID( *segment.p_segment_uid ) )
5461 virtual_segment_c *demux_sys_t::VirtualFromSegments( matroska_segment_c *p_segment ) const
5463 size_t i_preloaded, i;
5465 virtual_segment_c *p_result = new virtual_segment_c( p_segment );
5467 // fill our current virtual segment with all hard linked segments
5470 for ( i=0; i< opened_segments.size(); i++ )
5472 i_preloaded += p_result->AddSegment( opened_segments[i] );
5474 } while ( i_preloaded ); // worst case: will stop when all segments are found as linked
5478 p_result->PreloadLinked( );
5480 p_result->PrepareChapters( );
5485 bool demux_sys_t::PreparePlayback( virtual_segment_c *p_new_segment )
5487 if ( p_new_segment != NULL && p_new_segment != p_current_segment )
5489 if ( p_current_segment != NULL && p_current_segment->Segment() != NULL )
5490 p_current_segment->Segment()->UnSelect();
5492 p_current_segment = p_new_segment;
5493 i_current_title = p_new_segment->i_sys_title;
5496 p_current_segment->LoadCues();
5497 f_duration = p_current_segment->Duration();
5499 /* add information */
5500 p_current_segment->Segment()->InformationCreate( );
5502 p_current_segment->Segment()->Select( 0 );
5507 void demux_sys_t::JumpTo( virtual_segment_c & vsegment, chapter_item_c * p_chapter )
5509 // if the segment is not part of the current segment, select the new one
5510 if ( &vsegment != p_current_segment )
5512 PreparePlayback( &vsegment );
5515 if ( p_chapter != NULL )
5517 if ( !p_chapter->Enter( true ) )
5519 // jump to the location in the found segment
5520 vsegment.Seek( demuxer, p_chapter->i_user_start_time, -1, p_chapter );
5526 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
5530 if ( p_item_a == NULL || p_item_b == NULL )
5533 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
5534 if ( p_item_b->p_prev_segment_uid != NULL
5535 && *p_tmp == *p_item_b->p_prev_segment_uid )
5538 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
5542 if ( p_item_b->p_segment_uid != NULL
5543 && *p_tmp == *p_item_b->p_segment_uid )
5546 if ( p_item_b->p_prev_segment_uid != NULL
5547 && *p_tmp == *p_item_b->p_prev_segment_uid )
5553 bool matroska_segment_c::Preload( )
5558 EbmlElement *el = NULL;
5560 ep->Reset( &sys.demuxer );
5562 while( ( el = ep->Get() ) != NULL )
5564 if( MKV_IS_ID( el, KaxInfo ) )
5566 ParseInfo( static_cast<KaxInfo*>( el ) );
5568 else if( MKV_IS_ID( el, KaxTracks ) )
5570 ParseTracks( static_cast<KaxTracks*>( el ) );
5571 if ( tracks.size() == 0 )
5573 msg_Err( &sys.demuxer, "No tracks supported" );
5577 else if( MKV_IS_ID( el, KaxSeekHead ) )
5579 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
5581 else if( MKV_IS_ID( el, KaxCues ) )
5583 msg_Dbg( &sys.demuxer, "| + Cues" );
5585 else if( MKV_IS_ID( el, KaxCluster ) )
5587 msg_Dbg( &sys.demuxer, "| + Cluster" );
5589 cluster = (KaxCluster*)el;
5591 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
5595 /* stop parsing the stream */
5598 else if( MKV_IS_ID( el, KaxAttachments ) )
5600 msg_Dbg( &sys.demuxer, "| + Attachments" );
5601 ParseAttachments( static_cast<KaxAttachments*>( el ) );
5603 else if( MKV_IS_ID( el, KaxChapters ) )
5605 msg_Dbg( &sys.demuxer, "| + Chapters" );
5606 ParseChapters( static_cast<KaxChapters*>( el ) );
5608 else if( MKV_IS_ID( el, KaxTag ) )
5610 msg_Dbg( &sys.demuxer, "| + Tags FIXME TODO" );
5614 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid(*el).name() );
5623 matroska_segment_c *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
5625 for (size_t i=0; i<opened_segments.size(); i++)
5627 if ( *opened_segments[i]->p_segment_uid == uid )
5628 return opened_segments[i];
5633 chapter_item_c *demux_sys_t::BrowseCodecPrivate( unsigned int codec_id,
5634 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
5635 const void *p_cookie,
5636 size_t i_cookie_size,
5637 virtual_segment_c * &p_segment_found )
5639 chapter_item_c *p_result = NULL;
5640 for (size_t i=0; i<used_segments.size(); i++)
5642 p_result = used_segments[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
5643 if ( p_result != NULL )
5645 p_segment_found = used_segments[i];
5652 chapter_item_c *demux_sys_t::FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found )
5654 chapter_item_c *p_result = NULL;
5655 for (size_t i=0; i<used_segments.size(); i++)
5657 p_result = used_segments[i]->FindChapter( i_find_uid );
5658 if ( p_result != NULL )
5660 p_segment_found = used_segments[i];
5667 void virtual_segment_c::Sort()
5669 // keep the current segment index
5670 matroska_segment_c *p_segment = linked_segments[i_current_segment];
5672 std::sort( linked_segments.begin(), linked_segments.end(), matroska_segment_c::CompareSegmentUIDs );
5674 for ( i_current_segment=0; i_current_segment<linked_segments.size(); i_current_segment++)
5675 if ( linked_segments[i_current_segment] == p_segment )
5679 size_t virtual_segment_c::AddSegment( matroska_segment_c *p_segment )
5682 // check if it's not already in here
5683 for ( i=0; i<linked_segments.size(); i++ )
5685 if ( linked_segments[i]->p_segment_uid != NULL
5686 && p_segment->p_segment_uid != NULL
5687 && *p_segment->p_segment_uid == *linked_segments[i]->p_segment_uid )
5691 // find possible mates
5692 for ( i=0; i<linked_uids.size(); i++ )
5694 if ( (p_segment->p_segment_uid != NULL && *p_segment->p_segment_uid == linked_uids[i])
5695 || (p_segment->p_prev_segment_uid != NULL && *p_segment->p_prev_segment_uid == linked_uids[i])
5696 || (p_segment->p_next_segment_uid !=NULL && *p_segment->p_next_segment_uid == linked_uids[i]) )
5698 linked_segments.push_back( p_segment );
5700 AppendUID( p_segment->p_prev_segment_uid );
5701 AppendUID( p_segment->p_next_segment_uid );
5709 void virtual_segment_c::PreloadLinked( )
5711 for ( size_t i=0; i<linked_segments.size(); i++ )
5713 linked_segments[i]->Preload( );
5715 i_current_edition = linked_segments[0]->i_default_edition;
5718 mtime_t virtual_segment_c::Duration() const
5721 if ( linked_segments.size() == 0 )
5724 matroska_segment_c *p_last_segment = linked_segments[linked_segments.size()-1];
5725 // p_last_segment->ParseCluster( );
5727 i_duration = p_last_segment->i_start_time / 1000 + p_last_segment->i_duration;
5732 void virtual_segment_c::LoadCues( )
5734 for ( size_t i=0; i<linked_segments.size(); i++ )
5736 linked_segments[i]->LoadCues();
5740 void virtual_segment_c::AppendUID( const EbmlBinary * p_UID )
5742 if ( p_UID == NULL )
5744 if ( p_UID->GetBuffer() == NULL )
5747 for (size_t i=0; i<linked_uids.size(); i++)
5749 if ( *p_UID == linked_uids[i] )
5752 linked_uids.push_back( *(KaxSegmentUID*)(p_UID) );
5755 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset )
5758 #if LIBMATROSKA_VERSION >= 0x000800
5759 KaxSimpleBlock *simpleblock;
5761 int i_track_skipping;
5762 int64_t i_block_duration;
5763 int64_t i_block_ref1;
5764 int64_t i_block_ref2;
5766 int64_t i_seek_position = i_start_pos;
5767 int64_t i_seek_time = i_start_time;
5773 for( ; i_idx < i_index; i_idx++ )
5775 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
5786 i_seek_position = p_indexes[i_idx].i_position;
5787 i_seek_time = p_indexes[i_idx].i_time;
5790 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
5791 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
5793 es.I_O().setFilePointer( i_seek_position, seek_beginning );
5796 ep = new EbmlParser( &es, segment, &sys.demuxer );
5799 sys.i_start_pts = i_date;
5801 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
5803 /* now parse until key frame */
5804 i_track_skipping = 0;
5805 for( i_track = 0; i_track < tracks.size(); i_track++ )
5807 if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
5809 tracks[i_track]->b_search_keyframe = true;
5812 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, i_date );
5816 while( i_track_skipping > 0 )
5818 #if LIBMATROSKA_VERSION >= 0x000800
5819 if( BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
5821 if( BlockGet( block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
5824 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
5830 for( i_track = 0; i_track < tracks.size(); i_track++ )
5832 #if LIBMATROSKA_VERSION >= 0x000800
5833 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
5834 (block && tracks[i_track]->i_number == block->TrackNum()) )
5836 if( tracks[i_track]->i_number == block->TrackNum() )
5843 #if LIBMATROSKA_VERSION >= 0x000800
5845 sys.i_pts = (sys.i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
5848 sys.i_pts = (sys.i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
5850 if( i_track < tracks.size() )
5852 if( sys.i_pts >= sys.i_start_pts )
5854 cluster = static_cast<KaxCluster*>(ep->UnGet( i_block_pos, i_cluster_pos ));
5855 i_track_skipping = 0;
5857 else if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
5859 if( i_block_ref1 == 0 && tracks[i_track]->b_search_keyframe )
5861 tracks[i_track]->b_search_keyframe = false;
5864 if( !tracks[i_track]->b_search_keyframe )
5866 #if LIBMATROSKA_VERSION >= 0x000800
5867 BlockDecode( &sys.demuxer, block, simpleblock, sys.i_pts, 0, i_block_ref1 >= 0 || i_block_ref2 > 0 );
5869 BlockDecode( &sys.demuxer, block, sys.i_pts, 0, i_block_ref1 >= 0 || i_block_ref2 > 0 );
5879 void virtual_segment_c::Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter )
5881 demux_sys_t *p_sys = demuxer.p_sys;
5884 // find the actual time for an ordered edition
5885 if ( psz_chapter == NULL )
5887 if ( Edition() && Edition()->b_ordered )
5889 /* 1st, we need to know in which chapter we are */
5890 psz_chapter = (*p_editions)[i_current_edition]->FindTimecode( i_date, psz_current_chapter );
5894 if ( psz_chapter != NULL )
5896 psz_current_chapter = psz_chapter;
5897 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
5898 if ( psz_chapter->i_seekpoint_num > 0 )
5900 demuxer.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
5901 demuxer.info.i_title = p_sys->i_current_title = i_sys_title;
5902 demuxer.info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
5906 // find the best matching segment
5907 for ( i=0; i<linked_segments.size(); i++ )
5909 if ( i_date < linked_segments[i]->i_start_time )
5916 if ( i_current_segment != i )
5918 linked_segments[i_current_segment]->UnSelect();
5919 linked_segments[i]->Select( i_date );
5920 i_current_segment = i;
5923 linked_segments[i]->Seek( i_date, i_time_offset );
5926 void chapter_codec_cmds_c::AddCommand( const KaxChapterProcessCommand & command )
5930 uint32 codec_time = uint32(-1);
5931 for( i = 0; i < command.ListSize(); i++ )
5933 const EbmlElement *k = command[i];
5935 if( MKV_IS_ID( k, KaxChapterProcessTime ) )
5937 codec_time = uint32( *static_cast<const KaxChapterProcessTime*>( k ) );
5942 for( i = 0; i < command.ListSize(); i++ )
5944 const EbmlElement *k = command[i];
5946 if( MKV_IS_ID( k, KaxChapterProcessData ) )
5948 KaxChapterProcessData *p_data = new KaxChapterProcessData( *static_cast<const KaxChapterProcessData*>( k ) );
5949 switch ( codec_time )
5952 during_cmds.push_back( p_data );
5955 enter_cmds.push_back( p_data );
5958 leave_cmds.push_back( p_data );
5967 bool chapter_item_c::Enter( bool b_do_subs )
5969 bool f_result = false;
5970 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
5971 while ( index != codecs.end() )
5973 f_result |= (*index)->Enter();
5980 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
5981 while ( index_ != sub_chapters.end() )
5983 f_result |= (*index_)->Enter( true );
5990 bool chapter_item_c::Leave( bool b_do_subs )
5992 bool f_result = false;
5993 b_is_leaving = true;
5994 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
5995 while ( index != codecs.end() )
5997 f_result |= (*index)->Leave();
6004 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
6005 while ( index_ != sub_chapters.end() )
6007 f_result |= (*index_)->Leave( true );
6011 b_is_leaving = false;
6015 bool chapter_item_c::EnterAndLeave( chapter_item_c *p_item, bool b_final_enter )
6017 chapter_item_c *p_common_parent = p_item;
6019 // leave, up to a common parent
6020 while ( p_common_parent != NULL && !p_common_parent->ParentOf( *this ) )
6022 if ( !p_common_parent->b_is_leaving && p_common_parent->Leave( false ) )
6024 p_common_parent = p_common_parent->psz_parent;
6027 // enter from the parent to <this>
6028 if ( p_common_parent != NULL )
6032 if ( p_common_parent == this )
6033 return Enter( true );
6035 for ( size_t i = 0; i<p_common_parent->sub_chapters.size(); i++ )
6037 if ( p_common_parent->sub_chapters[i]->ParentOf( *this ) )
6039 p_common_parent = p_common_parent->sub_chapters[i];
6040 if ( p_common_parent != this )
6041 if ( p_common_parent->Enter( false ) )
6050 if ( b_final_enter )
6051 return Enter( true );
6056 bool dvd_chapter_codec_c::Enter()
6058 bool f_result = false;
6059 std::vector<KaxChapterProcessData*>::iterator index = enter_cmds.begin();
6060 while ( index != enter_cmds.end() )
6062 if ( (*index)->GetSize() )
6064 binary *p_data = (*index)->GetBuffer();
6065 size_t i_size = *p_data++;
6066 // avoid reading too much from the buffer
6067 i_size = __MIN( i_size, ((*index)->GetSize() - 1) >> 3 );
6068 for ( ; i_size > 0; i_size--, p_data += 8 )
6070 msg_Dbg( &sys.demuxer, "Matroska DVD enter command" );
6071 f_result |= sys.dvd_interpretor.Interpret( p_data );
6079 bool dvd_chapter_codec_c::Leave()
6081 bool f_result = false;
6082 std::vector<KaxChapterProcessData*>::iterator index = leave_cmds.begin();
6083 while ( index != leave_cmds.end() )
6085 if ( (*index)->GetSize() )
6087 binary *p_data = (*index)->GetBuffer();
6088 size_t i_size = *p_data++;
6089 // avoid reading too much from the buffer
6090 i_size = __MIN( i_size, ((*index)->GetSize() - 1) >> 3 );
6091 for ( ; i_size > 0; i_size--, p_data += 8 )
6093 msg_Dbg( &sys.demuxer, "Matroska DVD leave command" );
6094 f_result |= sys.dvd_interpretor.Interpret( p_data );
6102 // see http://www.dvd-replica.com/DVD/vmcmdset.php for a description of DVD commands
6103 bool dvd_command_interpretor_c::Interpret( const binary * p_command, size_t i_size )
6108 virtual_segment_c *p_segment = NULL;
6109 chapter_item_c *p_chapter = NULL;
6110 bool f_result = false;
6111 uint16 i_command = ( p_command[0] << 8 ) + p_command[1];
6113 // handle register tests if there are some
6114 if ( (i_command & 0xF0) != 0 )
6116 bool b_test_positive = true;//(i_command & CMD_DVD_IF_NOT) == 0;
6117 bool b_test_value = (i_command & CMD_DVD_TEST_VALUE) != 0;
6118 uint8 i_test = i_command & 0x70;
6121 // see http://dvd.sourceforge.net/dvdinfo/vmi.html
6124 switch ( i_command >> 12 )
6127 i_cr1 = p_command[3];
6128 i_cr2 = (p_command[4] << 8) + p_command[5];
6133 i_cr1 = p_command[6];
6134 i_cr2 = p_command[7];
6135 b_test_value = false;
6139 if ( ((p_command[1] >> 4) & 0x7) == 0)
6141 i_cr1 = p_command[2];
6142 i_cr2 = (p_command[6] << 8) + p_command[7];
6146 i_cr1 = p_command[2];
6147 i_cr2 = (p_command[6] << 8) + p_command[7];
6155 i_value = GetPRM( i_cr2 );
6159 case CMD_DVD_IF_GPREG_EQUAL:
6161 msg_Dbg( &sys.demuxer, "IF %s EQUALS %s", GetRegTypeName( false, i_cr1 ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6162 if (!( GetPRM( i_cr1 ) == i_value ))
6164 b_test_positive = false;
6167 case CMD_DVD_IF_GPREG_NOT_EQUAL:
6169 msg_Dbg( &sys.demuxer, "IF %s NOT EQUALS %s", GetRegTypeName( false, i_cr1 ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6170 if (!( GetPRM( i_cr1 ) != i_value ))
6172 b_test_positive = false;
6175 case CMD_DVD_IF_GPREG_INF:
6177 msg_Dbg( &sys.demuxer, "IF %s < %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6178 if (!( GetPRM( i_cr1 ) < i_value ))
6180 b_test_positive = false;
6183 case CMD_DVD_IF_GPREG_INF_EQUAL:
6184 // if inferior or equal
6185 msg_Dbg( &sys.demuxer, "IF %s < %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6186 if (!( GetPRM( i_cr1 ) <= i_value ))
6188 b_test_positive = false;
6191 case CMD_DVD_IF_GPREG_AND:
6193 msg_Dbg( &sys.demuxer, "IF %s & %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6194 if (!( GetPRM( i_cr1 ) & i_value ))
6196 b_test_positive = false;
6199 case CMD_DVD_IF_GPREG_SUP:
6201 msg_Dbg( &sys.demuxer, "IF %s >= %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6202 if (!( GetPRM( i_cr1 ) > i_value ))
6204 b_test_positive = false;
6207 case CMD_DVD_IF_GPREG_SUP_EQUAL:
6208 // if superior or equal
6209 msg_Dbg( &sys.demuxer, "IF %s >= %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6210 if (!( GetPRM( i_cr1 ) >= i_value ))
6212 b_test_positive = false;
6217 if ( !b_test_positive )
6221 // strip the test command
6222 i_command &= 0xFF0F;
6224 switch ( i_command )
6229 msg_Dbg( &sys.demuxer, "NOP" );
6234 msg_Dbg( &sys.demuxer, "Break" );
6238 case CMD_DVD_JUMP_TT:
6240 uint8 i_title = p_command[5];
6241 msg_Dbg( &sys.demuxer, "JumpTT %d", i_title );
6243 // find in the ChapProcessPrivate matching this Title level
6244 p_chapter = sys.BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title), p_segment );
6245 if ( p_segment != NULL )
6247 sys.JumpTo( *p_segment, p_chapter );
6253 case CMD_DVD_CALLSS_VTSM1:
6255 msg_Dbg( &sys.demuxer, "CallSS" );
6257 switch( (p_command[6] & 0xC0) >> 6 ) {
6259 p_type = p_command[5] & 0x0F;
6263 msg_Dbg( &sys.demuxer, "CallSS PGC (rsm_cell %x)", p_command[4]);
6266 msg_Dbg( &sys.demuxer, "CallSS Title Entry (rsm_cell %x)", p_command[4]);
6269 msg_Dbg( &sys.demuxer, "CallSS Root Menu (rsm_cell %x)", p_command[4]);
6272 msg_Dbg( &sys.demuxer, "CallSS Subpicture Menu (rsm_cell %x)", p_command[4]);
6275 msg_Dbg( &sys.demuxer, "CallSS Audio Menu (rsm_cell %x)", p_command[4]);
6278 msg_Dbg( &sys.demuxer, "CallSS Angle Menu (rsm_cell %x)", p_command[4]);
6281 msg_Dbg( &sys.demuxer, "CallSS Chapter Menu (rsm_cell %x)", p_command[4]);
6284 msg_Dbg( &sys.demuxer, "CallSS <unknown> (rsm_cell %x)", p_command[4]);
6287 p_chapter = sys.BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1, p_segment );
6288 if ( p_segment != NULL )
6290 sys.JumpTo( *p_segment, p_chapter );
6295 msg_Dbg( &sys.demuxer, "CallSS VMGM (menu %d, rsm_cell %x)", p_command[5] & 0x0F, p_command[4]);
6298 msg_Dbg( &sys.demuxer, "CallSS VTSM (menu %d, rsm_cell %x)", p_command[5] & 0x0F, p_command[4]);
6301 msg_Dbg( &sys.demuxer, "CallSS VMGM (pgc %d, rsm_cell %x)", (p_command[2] << 8) + p_command[3], p_command[4]);
6306 case CMD_DVD_JUMP_SS:
6308 msg_Dbg( &sys.demuxer, "JumpSS");
6310 switch( (p_command[5] & 0xC0) >> 6 ) {
6312 msg_Dbg( &sys.demuxer, "JumpSS FP");
6315 p_type = p_command[5] & 0x0F;
6319 msg_Dbg( &sys.demuxer, "JumpSS VMGM Title Entry");
6322 msg_Dbg( &sys.demuxer, "JumpSS VMGM Root Menu");
6325 msg_Dbg( &sys.demuxer, "JumpSS VMGM Subpicture Menu");
6328 msg_Dbg( &sys.demuxer, "JumpSS VMGM Audio Menu");
6331 msg_Dbg( &sys.demuxer, "JumpSS VMGM Angle Menu");
6334 msg_Dbg( &sys.demuxer, "JumpSS VMGM Chapter Menu");
6337 msg_Dbg( &sys.demuxer, "JumpSS <unknown>");
6341 p_chapter = sys.BrowseCodecPrivate( 1, MatchIsVMG, NULL, 0, p_segment );
6342 if ( p_segment != NULL )
6344 p_chapter = p_segment->BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1 );
6345 if ( p_chapter != NULL )
6347 sys.JumpTo( *p_segment, p_chapter );
6353 p_type = p_command[5] & 0x0F;
6357 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Title Entry", p_command[4], p_command[3]);
6360 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Root Menu", p_command[4], p_command[3]);
6363 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Subpicture Menu", p_command[4], p_command[3]);
6366 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Audio Menu", p_command[4], p_command[3]);
6369 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Angle Menu", p_command[4], p_command[3]);
6372 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Chapter Menu", p_command[4], p_command[3]);
6375 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) <unknown>", p_command[4], p_command[3]);
6379 p_chapter = sys.BrowseCodecPrivate( 1, MatchVTSMNumber, &p_command[4], 1, p_segment );
6381 if ( p_segment != NULL && p_chapter != NULL )
6383 // find the title in the VTS
6384 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchTitleNumber, &p_command[3], 1 );
6385 if ( p_chapter != NULL )
6387 // find the specified menu in the VTSM
6388 p_chapter = p_segment->BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1 );
6389 if ( p_chapter != NULL )
6391 sys.JumpTo( *p_segment, p_chapter );
6396 msg_Dbg( &sys.demuxer, "Title (%d) does not exist in this VTS", p_command[3] );
6399 msg_Dbg( &sys.demuxer, "DVD Domain VTS (%d) not found", p_command[4] );
6402 msg_Dbg( &sys.demuxer, "JumpSS VMGM (pgc %d)", (p_command[2] << 8) + p_command[3]);
6407 case CMD_DVD_JUMPVTS_PTT:
6409 uint8 i_title = p_command[5];
6410 uint8 i_ptt = p_command[3];
6412 msg_Dbg( &sys.demuxer, "JumpVTS Title (%d) PTT (%d)", i_title, i_ptt);
6414 // find the current VTS content segment
6415 p_chapter = sys.p_current_segment->BrowseCodecPrivate( 1, MatchIsDomain, NULL, 0 );
6416 if ( p_chapter != NULL )
6418 int16 i_curr_title = p_chapter->GetTitleNumber( );
6419 if ( i_curr_title > 0 )
6421 p_chapter = sys.BrowseCodecPrivate( 1, MatchVTSNumber, &i_curr_title, sizeof(i_curr_title), p_segment );
6423 if ( p_segment != NULL && p_chapter != NULL )
6425 // find the title in the VTS
6426 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title) );
6427 if ( p_chapter != NULL )
6429 // find the chapter in the title
6430 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchChapterNumber, &i_ptt, sizeof(i_ptt) );
6431 if ( p_chapter != NULL )
6433 sys.JumpTo( *p_segment, p_chapter );
6438 msg_Dbg( &sys.demuxer, "Title (%d) does not exist in this VTS", i_title );
6441 msg_Dbg( &sys.demuxer, "DVD Domain VTS (%d) not found", i_curr_title );
6444 msg_Dbg( &sys.demuxer, "JumpVTS_PTT command found but not in a VTS(M)");
6447 msg_Dbg( &sys.demuxer, "JumpVTS_PTT command but the DVD domain wasn't found");
6450 case CMD_DVD_SET_GPRMMD:
6452 msg_Dbg( &sys.demuxer, "Set GPRMMD [%d]=%d", (p_command[4] << 8) + p_command[5], (p_command[2] << 8) + p_command[3]);
6454 if ( !SetGPRM( (p_command[4] << 8) + p_command[5], (p_command[2] << 8) + p_command[3] ) )
6455 msg_Dbg( &sys.demuxer, "Set GPRMMD failed" );
6458 case CMD_DVD_LINKPGCN:
6460 uint16 i_pgcn = (p_command[6] << 8) + p_command[7];
6462 msg_Dbg( &sys.demuxer, "Link PGCN(%d)", i_pgcn );
6463 p_chapter = sys.p_current_segment->BrowseCodecPrivate( 1, MatchPgcNumber, &i_pgcn, 2 );
6464 if ( p_chapter != NULL )
6466 if ( !p_chapter->Enter( true ) )
6467 // jump to the location in the found segment
6468 sys.p_current_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
6474 case CMD_DVD_LINKCN:
6476 uint8 i_cn = p_command[7];
6478 p_chapter = sys.p_current_segment->CurrentChapter();
6480 msg_Dbg( &sys.demuxer, "LinkCN (cell %d)", i_cn );
6481 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchCellNumber, &i_cn, 1 );
6482 if ( p_chapter != NULL )
6484 if ( !p_chapter->Enter( true ) )
6485 // jump to the location in the found segment
6486 sys.p_current_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
6492 case CMD_DVD_GOTO_LINE:
6494 msg_Dbg( &sys.demuxer, "GotoLine (%d)", (p_command[6] << 8) + p_command[7] );
6498 case CMD_DVD_SET_HL_BTNN1:
6500 msg_Dbg( &sys.demuxer, "SetHL_BTN (%d)", p_command[4] );
6501 SetSPRM( 0x88, p_command[4] );
6506 msg_Dbg( &sys.demuxer, "unsupported command : %02X %02X %02X %02X %02X %02X %02X %02X"
6522 bool dvd_command_interpretor_c::MatchIsDomain( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6524 return ( data.p_private_data != NULL && data.p_private_data->GetBuffer()[0] == MATROSKA_DVD_LEVEL_SS );
6527 bool dvd_command_interpretor_c::MatchIsVMG( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6529 if ( data.p_private_data == NULL || data.p_private_data->GetSize() < 2 )
6532 return ( data.p_private_data->GetBuffer()[0] == MATROSKA_DVD_LEVEL_SS && data.p_private_data->GetBuffer()[1] == 0xC0);
6535 bool dvd_command_interpretor_c::MatchVTSNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6537 if ( i_cookie_size != 2 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6540 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_SS || data.p_private_data->GetBuffer()[1] != 0x80 )
6543 uint16 i_gtitle = (data.p_private_data->GetBuffer()[2] << 8 ) + data.p_private_data->GetBuffer()[3];
6544 uint16 i_title = *(uint16*)p_cookie;
6546 return (i_gtitle == i_title);
6549 bool dvd_command_interpretor_c::MatchVTSMNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6551 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6554 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_SS || data.p_private_data->GetBuffer()[1] != 0x40 )
6557 uint8 i_gtitle = data.p_private_data->GetBuffer()[3];
6558 uint8 i_title = *(uint8*)p_cookie;
6560 return (i_gtitle == i_title);
6563 bool dvd_command_interpretor_c::MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6565 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6568 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_TT )
6571 uint16 i_gtitle = (data.p_private_data->GetBuffer()[1] << 8 ) + data.p_private_data->GetBuffer()[2];
6572 uint8 i_title = *(uint8*)p_cookie;
6574 return (i_gtitle == i_title);
6577 bool dvd_command_interpretor_c::MatchPgcType( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6579 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 8 )
6582 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PGC )
6585 uint8 i_pgc_type = data.p_private_data->GetBuffer()[3] & 0x0F;
6586 uint8 i_pgc = *(uint8*)p_cookie;
6588 return (i_pgc_type == i_pgc);
6591 bool dvd_command_interpretor_c::MatchPgcNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6593 if ( i_cookie_size != 2 || data.p_private_data == NULL || data.p_private_data->GetSize() < 8 )
6596 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PGC )
6599 uint16 *i_pgc_n = (uint16 *)p_cookie;
6600 uint16 i_pgc_num = (data.p_private_data->GetBuffer()[1] << 8) + data.p_private_data->GetBuffer()[2];
6602 return (i_pgc_num == *i_pgc_n);
6605 bool dvd_command_interpretor_c::MatchChapterNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6607 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 2 )
6610 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PTT )
6613 uint8 i_chapter = data.p_private_data->GetBuffer()[1];
6614 uint8 i_ptt = *(uint8*)p_cookie;
6616 return (i_chapter == i_ptt);
6619 bool dvd_command_interpretor_c::MatchCellNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6621 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 5 )
6624 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_CN )
6627 uint8 *i_cell_n = (uint8 *)p_cookie;
6628 uint8 i_cell_num = data.p_private_data->GetBuffer()[3];
6630 return (i_cell_num == *i_cell_n);
6633 bool matroska_script_codec_c::Enter()
6635 bool f_result = false;
6636 std::vector<KaxChapterProcessData*>::iterator index = enter_cmds.begin();
6637 while ( index != enter_cmds.end() )
6639 if ( (*index)->GetSize() )
6641 msg_Dbg( &sys.demuxer, "Matroska Script enter command" );
6642 f_result |= interpretor.Interpret( (*index)->GetBuffer(), (*index)->GetSize() );
6649 bool matroska_script_codec_c::Leave()
6651 bool f_result = false;
6652 std::vector<KaxChapterProcessData*>::iterator index = leave_cmds.begin();
6653 while ( index != leave_cmds.end() )
6655 if ( (*index)->GetSize() )
6657 msg_Dbg( &sys.demuxer, "Matroska Script leave command" );
6658 f_result |= interpretor.Interpret( (*index)->GetBuffer(), (*index)->GetSize() );
6665 // see http://www.matroska.org/technical/specs/chapters/index.html#mscript
6666 // for a description of existing commands
6667 bool matroska_script_interpretor_c::Interpret( const binary * p_command, size_t i_size )
6669 bool b_result = false;
6671 char *psz_str = (char*) malloc( i_size + 1 );
6672 memcpy( psz_str, p_command, i_size );
6673 psz_str[ i_size ] = '\0';
6675 std::string sz_command = psz_str;
6678 msg_Dbg( &sys.demuxer, "command : %s", sz_command.c_str() );
6680 #if defined(__GNUC__) && (__GNUC__ < 3)
6681 if ( sz_command.compare( CMD_MS_GOTO_AND_PLAY, 0, CMD_MS_GOTO_AND_PLAY.size() ) == 0 )
6683 if ( sz_command.compare( 0, CMD_MS_GOTO_AND_PLAY.size(), CMD_MS_GOTO_AND_PLAY ) == 0 )
6689 for ( i=CMD_MS_GOTO_AND_PLAY.size(); i<sz_command.size(); i++)
6691 if ( sz_command[i] == '(' )
6698 for ( j=i; j<sz_command.size(); j++)
6700 if ( sz_command[j] == ')' )
6707 std::string st = sz_command.substr( i+1, j-i-1 );
6708 int64_t i_chapter_uid = atoi( st.c_str() );
6710 virtual_segment_c *p_segment;
6711 chapter_item_c *p_chapter = sys.FindChapter( i_chapter_uid, p_segment );
6713 if ( p_chapter == NULL )
6714 msg_Dbg( &sys.demuxer, "Chapter %"PRId64" not found", i_chapter_uid);
6717 if ( !p_chapter->EnterAndLeave( sys.p_current_segment->CurrentChapter() ) )
6718 p_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
6726 void demux_sys_t::SwapButtons()
6728 #ifndef WORDS_BIGENDIAN
6729 uint8_t button, i, j;
6731 for( button = 1; button <= pci_packet.hli.hl_gi.btn_ns; button++) {
6732 btni_t *button_ptr = &(pci_packet.hli.btnit[button-1]);
6733 binary *p_data = (binary*) button_ptr;
6735 uint16 i_x_start = ((p_data[0] & 0x3F) << 4 ) + ( p_data[1] >> 4 );
6736 uint16 i_x_end = ((p_data[1] & 0x03) << 8 ) + p_data[2];
6737 uint16 i_y_start = ((p_data[3] & 0x3F) << 4 ) + ( p_data[4] >> 4 );
6738 uint16 i_y_end = ((p_data[4] & 0x03) << 8 ) + p_data[5];
6739 button_ptr->x_start = i_x_start;
6740 button_ptr->x_end = i_x_end;
6741 button_ptr->y_start = i_y_start;
6742 button_ptr->y_end = i_y_end;
6745 for ( i = 0; i<3; i++ )
6747 for ( j = 0; j<2; j++ )
6749 pci_packet.hli.btn_colit.btn_coli[i][j] = U32_AT( &pci_packet.hli.btn_colit.btn_coli[i][j] );