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 /*****************************************************************************
118 *****************************************************************************/
119 static int Open ( vlc_object_t * );
120 static void Close( vlc_object_t * );
123 set_shortname( "Matroska" );
124 set_description( N_("Matroska stream demuxer" ) );
125 set_capability( "demux", 50 );
126 set_callbacks( Open, Close );
127 set_category( CAT_INPUT );
128 set_subcategory( SUBCAT_INPUT_DEMUX );
130 add_bool( "mkv-use-ordered-chapters", 1, NULL,
131 N_("Ordered chapters"),
132 N_("Play ordered chapters as specified in the segment."), true );
134 add_bool( "mkv-use-chapter-codec", 1, NULL,
135 N_("Chapter codecs"),
136 N_("Use chapter codecs found in the segment."), true );
138 add_bool( "mkv-preload-local-dir", 1, NULL,
139 N_("Preload Directory"),
140 N_("Preload matroska files from the same family in the same directory (not good for broken files)."), true );
142 add_bool( "mkv-seek-percent", 0, NULL,
143 N_("Seek based on percent not time"),
144 N_("Seek based on percent not time."), true );
146 add_bool( "mkv-use-dummy", 0, NULL,
147 N_("Dummy Elements"),
148 N_("Read and discard unknown EBML elements (not good for broken files)."), true );
150 add_shortcut( "mka" );
151 add_shortcut( "mkv" );
156 #define MATROSKA_COMPRESSION_NONE -1
157 #define MATROSKA_COMPRESSION_ZLIB 0
158 #define MATROSKA_COMPRESSION_BLIB 1
159 #define MATROSKA_COMPRESSION_LZOX 2
160 #define MATROSKA_COMPRESSION_HEADER 3
162 #define MKVD_TIMECODESCALE 1000000
164 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
165 #undef ATTRIBUTE_PACKED
166 #undef PRAGMA_PACK_BEGIN
167 #undef PRAGMA_PACK_END
169 #if defined(__GNUC__)
170 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
171 #define ATTRIBUTE_PACKED __attribute__ ((packed))
172 #define PRAGMA_PACK 0
176 #if !defined(ATTRIBUTE_PACKED)
177 #define ATTRIBUTE_PACKED
178 #define PRAGMA_PACK 1
185 /*************************************
186 * taken from libdvdnav / libdvdread
187 **************************************/
190 * DVD Time Information.
196 uint8_t frame_u; /* The two high bits are the frame rate. */
197 } ATTRIBUTE_PACKED dvd_time_t;
203 #ifdef WORDS_BIGENDIAN
204 unsigned char zero : 7; /* 25-31 */
205 unsigned char video_pres_mode_change : 1; /* 24 */
207 unsigned char karaoke_audio_pres_mode_change : 1; /* 23 */
208 unsigned char angle_change : 1;
209 unsigned char subpic_stream_change : 1;
210 unsigned char audio_stream_change : 1;
211 unsigned char pause_on : 1;
212 unsigned char still_off : 1;
213 unsigned char button_select_or_activate : 1;
214 unsigned char resume : 1; /* 16 */
216 unsigned char chapter_menu_call : 1; /* 15 */
217 unsigned char angle_menu_call : 1;
218 unsigned char audio_menu_call : 1;
219 unsigned char subpic_menu_call : 1;
220 unsigned char root_menu_call : 1;
221 unsigned char title_menu_call : 1;
222 unsigned char backward_scan : 1;
223 unsigned char forward_scan : 1; /* 8 */
225 unsigned char next_pg_search : 1; /* 7 */
226 unsigned char prev_or_top_pg_search : 1;
227 unsigned char time_or_chapter_search : 1;
228 unsigned char go_up : 1;
229 unsigned char stop : 1;
230 unsigned char title_play : 1;
231 unsigned char chapter_search_or_play : 1;
232 unsigned char title_or_time_play : 1; /* 0 */
234 unsigned char video_pres_mode_change : 1; /* 24 */
235 unsigned char zero : 7; /* 25-31 */
237 unsigned char resume : 1; /* 16 */
238 unsigned char button_select_or_activate : 1;
239 unsigned char still_off : 1;
240 unsigned char pause_on : 1;
241 unsigned char audio_stream_change : 1;
242 unsigned char subpic_stream_change : 1;
243 unsigned char angle_change : 1;
244 unsigned char karaoke_audio_pres_mode_change : 1; /* 23 */
246 unsigned char forward_scan : 1; /* 8 */
247 unsigned char backward_scan : 1;
248 unsigned char title_menu_call : 1;
249 unsigned char root_menu_call : 1;
250 unsigned char subpic_menu_call : 1;
251 unsigned char audio_menu_call : 1;
252 unsigned char angle_menu_call : 1;
253 unsigned char chapter_menu_call : 1; /* 15 */
255 unsigned char title_or_time_play : 1; /* 0 */
256 unsigned char chapter_search_or_play : 1;
257 unsigned char title_play : 1;
258 unsigned char stop : 1;
259 unsigned char go_up : 1;
260 unsigned char time_or_chapter_search : 1;
261 unsigned char prev_or_top_pg_search : 1;
262 unsigned char next_pg_search : 1; /* 7 */
264 } ATTRIBUTE_PACKED user_ops_t;
267 * Type to store per-command data.
271 } ATTRIBUTE_PACKED vm_cmd_t;
272 #define COMMAND_DATA_SIZE 8
275 * PCI General Information
278 uint32_t nv_pck_lbn; /**< sector address of this nav pack */
279 uint16_t vobu_cat; /**< 'category' of vobu */
280 uint16_t zero1; /**< reserved */
281 user_ops_t vobu_uop_ctl; /**< UOP of vobu */
282 uint32_t vobu_s_ptm; /**< start presentation time of vobu */
283 uint32_t vobu_e_ptm; /**< end presentation time of vobu */
284 uint32_t vobu_se_e_ptm; /**< end ptm of sequence end in vobu */
285 dvd_time_t e_eltm; /**< Cell elapsed time */
287 } ATTRIBUTE_PACKED pci_gi_t;
290 * Non Seamless Angle Information
293 uint32_t nsml_agl_dsta[9]; /**< address of destination vobu in AGL_C#n */
294 } ATTRIBUTE_PACKED nsml_agli_t;
297 * Highlight General Information
299 * For btngrX_dsp_ty the bits have the following meaning:
300 * 000b: normal 4/3 only buttons
301 * XX1b: wide (16/9) buttons
302 * X1Xb: letterbox buttons
303 * 1XXb: pan&scan buttons
306 uint16_t hli_ss; /**< status, only low 2 bits 0: no buttons, 1: different 2: equal 3: eual except for button cmds */
307 uint32_t hli_s_ptm; /**< start ptm of hli */
308 uint32_t hli_e_ptm; /**< end ptm of hli */
309 uint32_t btn_se_e_ptm; /**< end ptm of button select */
310 #ifdef WORDS_BIGENDIAN
311 unsigned char zero1 : 2; /**< reserved */
312 unsigned char btngr_ns : 2; /**< number of button groups 1, 2 or 3 with 36/18/12 buttons */
313 unsigned char zero2 : 1; /**< reserved */
314 unsigned char btngr1_dsp_ty : 3; /**< display type of subpic stream for button group 1 */
315 unsigned char zero3 : 1; /**< reserved */
316 unsigned char btngr2_dsp_ty : 3; /**< display type of subpic stream for button group 2 */
317 unsigned char zero4 : 1; /**< reserved */
318 unsigned char btngr3_dsp_ty : 3; /**< display type of subpic stream for button group 3 */
320 unsigned char btngr1_dsp_ty : 3;
321 unsigned char zero2 : 1;
322 unsigned char btngr_ns : 2;
323 unsigned char zero1 : 2;
324 unsigned char btngr3_dsp_ty : 3;
325 unsigned char zero4 : 1;
326 unsigned char btngr2_dsp_ty : 3;
327 unsigned char zero3 : 1;
329 uint8_t btn_ofn; /**< button offset number range 0-255 */
330 uint8_t btn_ns; /**< number of valid buttons <= 36/18/12 (low 6 bits) */
331 uint8_t nsl_btn_ns; /**< number of buttons selectable by U_BTNNi (low 6 bits) nsl_btn_ns <= btn_ns */
332 uint8_t zero5; /**< reserved */
333 uint8_t fosl_btnn; /**< forcedly selected button (low 6 bits) */
334 uint8_t foac_btnn; /**< forcedly activated button (low 6 bits) */
335 } ATTRIBUTE_PACKED hl_gi_t;
339 * Button Color Information Table
340 * Each entry beeing a 32bit word that contains the color indexs and alpha
341 * values to use. They are all represented by 4 bit number and stored
342 * like this [Ci3, Ci2, Ci1, Ci0, A3, A2, A1, A0]. The actual palette
343 * that the indexes reference is in the PGC.
344 * \todo split the uint32_t into a struct
347 uint32_t btn_coli[3][2]; /**< [button color number-1][select:0/action:1] */
348 } ATTRIBUTE_PACKED btn_colit_t;
353 * NOTE: I've had to change the structure from the disk layout to get
354 * the packing to work with Sun's Forte C compiler.
355 * The 4 and 7 bytes are 'rotated' was: ABC DEF GHIJ is: ABCG DEFH IJ
358 #ifdef WORDS_BIGENDIAN
359 uint32 btn_coln : 2; /**< button color number */
360 uint32 x_start : 10; /**< x start offset within the overlay */
361 uint32 zero1 : 2; /**< reserved */
362 uint32 x_end : 10; /**< x end offset within the overlay */
364 uint32 zero3 : 2; /**< reserved */
365 uint32 up : 6; /**< button index when pressing up */
367 uint32 auto_action_mode : 2; /**< 0: no, 1: activated if selected */
368 uint32 y_start : 10; /**< y start offset within the overlay */
369 uint32 zero2 : 2; /**< reserved */
370 uint32 y_end : 10; /**< y end offset within the overlay */
372 uint32 zero4 : 2; /**< reserved */
373 uint32 down : 6; /**< button index when pressing down */
374 unsigned char zero5 : 2; /**< reserved */
375 unsigned char left : 6; /**< button index when pressing left */
376 unsigned char zero6 : 2; /**< reserved */
377 unsigned char right : 6; /**< button index when pressing right */
390 uint32 auto_action_mode : 2;
394 unsigned char left : 6;
395 unsigned char zero5 : 2;
396 unsigned char right : 6;
397 unsigned char zero6 : 2;
400 } ATTRIBUTE_PACKED btni_t;
403 * Highlight Information
407 btn_colit_t btn_colit;
409 } ATTRIBUTE_PACKED hli_t;
416 nsml_agli_t nsml_agli;
419 } ATTRIBUTE_PACKED pci_t;
425 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
429 * What's between a directory and a filename?
432 #define DIRECTORY_SEPARATOR '\\'
434 #define DIRECTORY_SEPARATOR '/'
437 using namespace LIBMATROSKA_NAMESPACE;
440 /*****************************************************************************
442 *****************************************************************************/
444 block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) {
445 int result, dstsize, n;
450 d_stream.zalloc = (alloc_func)0;
451 d_stream.zfree = (free_func)0;
452 d_stream.opaque = (voidpf)0;
453 result = inflateInit(&d_stream);
456 msg_Dbg( p_this, "inflateInit() failed. Result: %d", result );
460 d_stream.next_in = (Bytef *)p_in_block->p_buffer;
461 d_stream.avail_in = p_in_block->i_buffer;
463 p_block = block_New( p_this, 0 );
468 p_block = block_Realloc( p_block, 0, n * 1000 );
469 dst = (unsigned char *)p_block->p_buffer;
470 d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
471 d_stream.avail_out = 1000;
472 result = inflate(&d_stream, Z_NO_FLUSH);
473 if( ( result != Z_OK ) && ( result != Z_STREAM_END ) )
475 msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result );
479 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
480 ( result != Z_STREAM_END ) );
482 dstsize = d_stream.total_out;
483 inflateEnd( &d_stream );
485 p_block = block_Realloc( p_block, 0, dstsize );
486 p_block->i_buffer = dstsize;
487 block_Release( p_in_block );
494 * Helper function to print the mkv parse tree
496 static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
501 msg_Err( &demuxer, "too deep tree" );
504 va_start( args, psz_format );
505 static const char psz_foo[] = "| | | | | | | | | |";
506 char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) );
507 strncpy( psz_foo2, psz_foo, 4 * i_level );
508 psz_foo2[ 4 * i_level ] = '+';
509 psz_foo2[ 4 * i_level + 1 ] = ' ';
510 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
511 __msg_GenericVa( VLC_OBJECT(&demuxer),VLC_MSG_DBG, "mkv", psz_foo2, args );
516 /*****************************************************************************
518 *****************************************************************************/
519 class vlc_stream_io_callback: public IOCallback
527 vlc_stream_io_callback( stream_t *, bool );
529 virtual ~vlc_stream_io_callback()
535 virtual uint32 read ( void *p_buffer, size_t i_size);
536 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
537 virtual size_t write ( const void *p_buffer, size_t i_size);
538 virtual uint64 getFilePointer ( void );
539 virtual void close ( void );
542 /*****************************************************************************
544 *****************************************************************************/
548 EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux );
549 virtual ~EbmlParser( void );
553 void Reset( demux_t *p_demux );
554 EbmlElement *Get( void );
556 EbmlElement *UnGet( uint64 i_block_pos, uint64 i_cluster_pos );
558 int GetLevel( void );
560 /* Is the provided element presents in our upper elements */
561 bool IsTopPresent( EbmlElement * );
566 EbmlElement *m_el[10];
567 int64_t mi_remain_size[10];
577 /*****************************************************************************
578 * Some functions to manipulate memory
579 *****************************************************************************/
580 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
581 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
583 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
586 /*****************************************************************************
587 * definitions of structures and functions used by this plugins
588 *****************************************************************************/
595 unsigned int i_number;
598 uint8_t *p_extra_data;
602 uint64_t i_default_duration;
603 float f_timecodescale;
612 unsigned int i_original_rate;
615 /* data to be send first */
617 uint8_t *p_data_init;
619 /* hack : it's for seek */
620 bool b_search_keyframe;
624 const char *psz_codec_name;
625 const char *psz_codec_settings;
626 const char *psz_codec_info_url;
627 const char *psz_codec_download_url;
629 /* encryption/compression */
630 int i_compression_type;
631 KaxContentCompSettings *p_compression_data;
648 const binary MATROSKA_DVD_LEVEL_SS = 0x30;
649 const binary MATROSKA_DVD_LEVEL_LU = 0x2A;
650 const binary MATROSKA_DVD_LEVEL_TT = 0x28;
651 const binary MATROSKA_DVD_LEVEL_PGC = 0x20;
652 const binary MATROSKA_DVD_LEVEL_PG = 0x18;
653 const binary MATROSKA_DVD_LEVEL_PTT = 0x10;
654 const binary MATROSKA_DVD_LEVEL_CN = 0x08;
656 class chapter_codec_cmds_c
659 chapter_codec_cmds_c( demux_sys_t & demuxer, int codec_id = -1)
660 :p_private_data(NULL)
661 ,i_codec_id( codec_id )
665 virtual ~chapter_codec_cmds_c()
667 delete p_private_data;
668 std::vector<KaxChapterProcessData*>::iterator indexe = enter_cmds.begin();
669 while ( indexe != enter_cmds.end() )
674 std::vector<KaxChapterProcessData*>::iterator indexl = leave_cmds.begin();
675 while ( indexl != leave_cmds.end() )
680 std::vector<KaxChapterProcessData*>::iterator indexd = during_cmds.begin();
681 while ( indexd != during_cmds.end() )
688 void SetPrivate( const KaxChapterProcessPrivate & private_data )
690 p_private_data = new KaxChapterProcessPrivate( private_data );
693 void AddCommand( const KaxChapterProcessCommand & command );
695 /// \return wether the codec has seeked in the files or not
696 virtual bool Enter() { return false; }
697 virtual bool Leave() { return false; }
698 virtual std::string GetCodecName( bool f_for_title = false ) const { return ""; }
699 virtual int16 GetTitleNumber() { return -1; }
701 KaxChapterProcessPrivate *p_private_data;
704 std::vector<KaxChapterProcessData*> enter_cmds;
705 std::vector<KaxChapterProcessData*> during_cmds;
706 std::vector<KaxChapterProcessData*> leave_cmds;
712 class dvd_command_interpretor_c
715 dvd_command_interpretor_c( demux_sys_t & demuxer )
718 memset( p_PRMs, 0, sizeof(p_PRMs) );
719 p_PRMs[ 0x80 + 1 ] = 15;
720 p_PRMs[ 0x80 + 2 ] = 62;
721 p_PRMs[ 0x80 + 3 ] = 1;
722 p_PRMs[ 0x80 + 4 ] = 1;
723 p_PRMs[ 0x80 + 7 ] = 1;
724 p_PRMs[ 0x80 + 8 ] = 1;
725 p_PRMs[ 0x80 + 16 ] = 0xFFFFu;
726 p_PRMs[ 0x80 + 18 ] = 0xFFFFu;
729 bool Interpret( const binary * p_command, size_t i_size = 8 );
731 uint16 GetPRM( size_t index ) const
734 return p_PRMs[ index ];
738 uint16 GetGPRM( size_t index ) const
740 if ( index >= 0 && index < 16 )
741 return p_PRMs[ index ];
745 uint16 GetSPRM( size_t index ) const
747 // 21,22,23 reserved for future use
748 if ( index >= 0x80 && index < 0x95 )
749 return p_PRMs[ index ];
753 bool SetPRM( size_t index, uint16 value )
755 if ( index >= 0 && index < 16 )
757 p_PRMs[ index ] = value;
763 bool SetGPRM( size_t index, uint16 value )
765 if ( index >= 0 && index < 16 )
767 p_PRMs[ index ] = value;
773 bool SetSPRM( size_t index, uint16 value )
775 if ( index > 0x80 && index <= 0x8D && index != 0x8C )
777 p_PRMs[ index ] = value;
784 std::string GetRegTypeName( bool b_value, uint16 value ) const
787 char s_value[6], s_reg_value[6];
788 sprintf( s_value, "%.5d", value );
796 else if ( value < 0x80 )
798 sprintf( s_reg_value, "%.5d", GetPRM( value ) );
802 result += s_reg_value;
807 sprintf( s_reg_value, "%.5d", GetPRM( value ) );
811 result += s_reg_value;
823 // wether it's a comparison on the value or register
824 static const uint16 CMD_DVD_TEST_VALUE = 0x80;
825 static const uint16 CMD_DVD_IF_GPREG_AND = (1 << 4);
826 static const uint16 CMD_DVD_IF_GPREG_EQUAL = (2 << 4);
827 static const uint16 CMD_DVD_IF_GPREG_NOT_EQUAL = (3 << 4);
828 static const uint16 CMD_DVD_IF_GPREG_SUP_EQUAL = (4 << 4);
829 static const uint16 CMD_DVD_IF_GPREG_SUP = (5 << 4);
830 static const uint16 CMD_DVD_IF_GPREG_INF_EQUAL = (6 << 4);
831 static const uint16 CMD_DVD_IF_GPREG_INF = (7 << 4);
833 static const uint16 CMD_DVD_NOP = 0x0000;
834 static const uint16 CMD_DVD_GOTO_LINE = 0x0001;
835 static const uint16 CMD_DVD_BREAK = 0x0002;
837 static const uint16 CMD_DVD_NOP2 = 0x2001;
838 static const uint16 CMD_DVD_LINKPGCN = 0x2004;
839 static const uint16 CMD_DVD_LINKPGN = 0x2006;
840 static const uint16 CMD_DVD_LINKCN = 0x2007;
841 static const uint16 CMD_DVD_JUMP_TT = 0x3002;
842 static const uint16 CMD_DVD_JUMPVTS_TT = 0x3003;
843 static const uint16 CMD_DVD_JUMPVTS_PTT = 0x3005;
844 static const uint16 CMD_DVD_JUMP_SS = 0x3006;
845 static const uint16 CMD_DVD_CALLSS_VTSM1 = 0x3008;
847 static const uint16 CMD_DVD_SET_HL_BTNN2 = 0x4600;
848 static const uint16 CMD_DVD_SET_HL_BTNN_LINKPGCN1 = 0x4604;
849 static const uint16 CMD_DVD_SET_STREAM = 0x5100;
850 static const uint16 CMD_DVD_SET_GPRMMD = 0x5300;
851 static const uint16 CMD_DVD_SET_HL_BTNN1 = 0x5600;
852 static const uint16 CMD_DVD_SET_HL_BTNN_LINKPGCN2 = 0x5604;
853 static const uint16 CMD_DVD_SET_HL_BTNN_LINKCN = 0x5607;
855 static const uint16 CMD_DVD_MOV_SPREG_PREG = 0x6100;
856 static const uint16 CMD_DVD_GPREG_MOV_VALUE = 0x7100;
857 static const uint16 CMD_DVD_SUB_GPREG = 0x7400;
858 static const uint16 CMD_DVD_MULT_GPREG = 0x7500;
859 static const uint16 CMD_DVD_GPREG_DIV_VALUE = 0x7600;
860 static const uint16 CMD_DVD_GPREG_AND_VALUE = 0x7900;
862 // callbacks when browsing inside CodecPrivate
863 static bool MatchIsDomain ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
864 static bool MatchIsVMG ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
865 static bool MatchVTSNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
866 static bool MatchVTSMNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
867 static bool MatchTitleNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
868 static bool MatchPgcType ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
869 static bool MatchPgcNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
870 static bool MatchChapterNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
871 static bool MatchCellNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
874 class dvd_chapter_codec_c : public chapter_codec_cmds_c
877 dvd_chapter_codec_c( demux_sys_t & sys )
878 :chapter_codec_cmds_c( sys, 1 )
883 std::string GetCodecName( bool f_for_title = false ) const;
884 int16 GetTitleNumber();
887 class matroska_script_interpretor_c
890 matroska_script_interpretor_c( demux_sys_t & demuxer )
894 bool Interpret( const binary * p_command, size_t i_size );
897 static const std::string CMD_MS_GOTO_AND_PLAY;
903 const std::string matroska_script_interpretor_c::CMD_MS_GOTO_AND_PLAY = "GotoAndPlay";
906 class matroska_script_codec_c : public chapter_codec_cmds_c
909 matroska_script_codec_c( demux_sys_t & sys )
910 :chapter_codec_cmds_c( sys, 0 )
918 matroska_script_interpretor_c interpretor;
921 class chapter_translation_c
924 chapter_translation_c()
928 ~chapter_translation_c()
933 KaxChapterTranslateID *p_translated;
934 unsigned int codec_id;
935 std::vector<uint64_t> editions;
944 ,i_user_start_time(-1)
947 ,b_display_seekpoint(true)
948 ,b_user_display(false)
953 virtual ~chapter_item_c()
955 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
956 while ( index != codecs.end() )
961 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
962 while ( index_ != sub_chapters.end() )
969 int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time );
970 int PublishChapters( input_title_t & title, int & i_user_chapters, int i_level = 0 );
971 virtual chapter_item_c * FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current, bool & b_found );
972 void Append( const chapter_item_c & edition );
973 chapter_item_c * FindChapter( int64_t i_find_uid );
974 virtual chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
975 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
976 const void *p_cookie,
977 size_t i_cookie_size );
978 std::string GetCodecName( bool f_for_title = false ) const;
979 bool ParentOf( const chapter_item_c & item ) const;
980 int16 GetTitleNumber( ) const;
982 int64_t i_start_time, i_end_time;
983 int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */
984 std::vector<chapter_item_c*> sub_chapters;
987 bool b_display_seekpoint;
989 std::string psz_name;
990 chapter_item_c *psz_parent;
993 std::vector<chapter_codec_cmds_c*> codecs;
995 static bool CompareTimecode( const chapter_item_c * itemA, const chapter_item_c * itemB )
997 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) );
1000 bool Enter( bool b_do_subchapters );
1001 bool Leave( bool b_do_subchapters );
1002 bool EnterAndLeave( chapter_item_c *p_item, bool b_enter = true );
1005 class chapter_edition_c : public chapter_item_c
1012 void RefreshChapters( );
1013 mtime_t Duration() const;
1014 std::string GetMainName() const;
1015 chapter_item_c * FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current );
1020 class matroska_segment_c
1023 matroska_segment_c( demux_sys_t & demuxer, EbmlStream & estream )
1026 ,i_timescale(MKVD_TIMECODESCALE)
1029 ,i_cues_position(-1)
1030 ,i_info_position(-1)
1031 ,i_chapters_position(-1)
1032 ,i_tags_position(-1)
1033 ,i_tracks_position(-1)
1034 ,i_attachments_position(-1)
1035 ,i_seekhead_position(-1)
1036 ,i_seekhead_count(0)
1041 ,p_segment_uid(NULL)
1042 ,p_prev_segment_uid(NULL)
1043 ,p_next_segment_uid(NULL)
1047 ,psz_muxing_application(NULL)
1048 ,psz_writing_application(NULL)
1049 ,psz_segment_filename(NULL)
1052 ,i_default_edition(0)
1057 p_indexes = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
1060 virtual ~matroska_segment_c()
1062 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1064 delete tracks[i_track]->p_compression_data;
1065 es_format_Clean( &tracks[i_track]->fmt );
1066 free( tracks[i_track]->p_extra_data );
1067 free( tracks[i_track]->psz_codec );
1068 delete tracks[i_track];
1071 free( psz_writing_application );
1072 free( psz_muxing_application );
1073 free( psz_segment_filename );
1075 free( psz_date_utc );
1080 delete p_segment_uid;
1081 delete p_prev_segment_uid;
1082 delete p_next_segment_uid;
1084 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
1085 while ( index != stored_editions.end() )
1090 std::vector<chapter_translation_c*>::iterator indext = translations.begin();
1091 while ( indext != translations.end() )
1096 std::vector<KaxSegmentFamily*>::iterator indexf = families.begin();
1097 while ( indexf != families.end() )
1104 KaxSegment *segment;
1108 uint64_t i_timescale;
1110 /* duration of the segment */
1112 mtime_t i_start_time;
1115 std::vector<mkv_track_t*> tracks;
1118 int i_seekhead_count;
1119 int64_t i_seekhead_position;
1120 int64_t i_cues_position;
1121 int64_t i_tracks_position;
1122 int64_t i_info_position;
1123 int64_t i_chapters_position;
1124 int64_t i_tags_position;
1125 int64_t i_attachments_position;
1127 KaxCluster *cluster;
1129 uint64 i_cluster_pos;
1130 int64_t i_start_pos;
1131 KaxSegmentUID *p_segment_uid;
1132 KaxPrevUID *p_prev_segment_uid;
1133 KaxNextUID *p_next_segment_uid;
1138 mkv_index_t *p_indexes;
1141 char *psz_muxing_application;
1142 char *psz_writing_application;
1143 char *psz_segment_filename;
1147 /* !!!!! GCC 3.3 bug on Darwin !!!!! */
1148 /* when you remove this variable the compiler issues an atomicity error */
1149 /* this variable only works when using std::vector<chapter_edition_c> */
1150 std::vector<chapter_edition_c*> stored_editions;
1151 int i_default_edition;
1153 std::vector<chapter_translation_c*> translations;
1154 std::vector<KaxSegmentFamily*> families;
1161 bool LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position );
1162 bool PreloadFamily( const matroska_segment_c & segment );
1163 void ParseInfo( KaxInfo *info );
1164 void ParseAttachments( KaxAttachments *attachments );
1165 void ParseChapters( KaxChapters *chapters );
1166 void ParseSeekHead( KaxSeekHead *seekhead );
1167 void ParseTracks( KaxTracks *tracks );
1168 void ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters );
1169 void ParseTrackEntry( KaxTrackEntry *m );
1170 void ParseCluster( );
1171 void IndexAppendCluster( KaxCluster *cluster );
1172 void LoadCues( KaxCues *cues );
1173 void LoadTags( KaxTags *tags );
1174 void InformationCreate( );
1175 void Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_global_position );
1176 #if LIBMATROSKA_VERSION >= 0x000800
1177 int BlockGet( KaxBlock * &, KaxSimpleBlock * &, int64_t *, int64_t *, int64_t *);
1179 int BlockGet( KaxBlock * &, int64_t *, int64_t *, int64_t *);
1182 int BlockFindTrackIndex( size_t *pi_track,
1183 const KaxBlock *p_block
1184 #if LIBMATROSKA_VERSION >= 0x000800
1185 , const KaxSimpleBlock *p_simpleblock
1190 bool Select( mtime_t i_start_time );
1193 static bool CompareSegmentUIDs( const matroska_segment_c * item_a, const matroska_segment_c * item_b );
1196 // class holding hard-linked segment together in the playback order
1197 class virtual_segment_c
1200 virtual_segment_c( matroska_segment_c *p_segment )
1203 ,i_current_segment(0)
1204 ,i_current_edition(-1)
1205 ,psz_current_chapter(NULL)
1207 linked_segments.push_back( p_segment );
1209 AppendUID( p_segment->p_segment_uid );
1210 AppendUID( p_segment->p_prev_segment_uid );
1211 AppendUID( p_segment->p_next_segment_uid );
1215 size_t AddSegment( matroska_segment_c *p_segment );
1216 void PreloadLinked( );
1217 mtime_t Duration( ) const;
1218 void Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter, int64_t i_global_position );
1220 inline chapter_edition_c *Edition()
1222 if ( i_current_edition >= 0 && size_t(i_current_edition) < p_editions->size() )
1223 return (*p_editions)[i_current_edition];
1227 matroska_segment_c * Segment() const
1229 if ( linked_segments.size() == 0 || i_current_segment >= linked_segments.size() )
1231 return linked_segments[i_current_segment];
1234 inline chapter_item_c *CurrentChapter() {
1235 return psz_current_chapter;
1240 if ( i_current_segment < linked_segments.size()-1 )
1242 i_current_segment++;
1248 bool FindUID( KaxSegmentUID & uid ) const
1250 for ( size_t i=0; i<linked_uids.size(); i++ )
1252 if ( linked_uids[i] == uid )
1258 bool UpdateCurrentToChapter( demux_t & demux );
1259 void PrepareChapters( );
1261 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
1262 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1263 const void *p_cookie,
1264 size_t i_cookie_size );
1265 chapter_item_c *FindChapter( int64_t i_find_uid );
1267 std::vector<chapter_edition_c*> *p_editions;
1271 std::vector<matroska_segment_c*> linked_segments;
1272 std::vector<KaxSegmentUID> linked_uids;
1273 size_t i_current_segment;
1275 int i_current_edition;
1276 chapter_item_c *psz_current_chapter;
1278 void AppendUID( const EbmlBinary * UID );
1281 class matroska_stream_c
1284 matroska_stream_c( demux_sys_t & demuxer )
1290 virtual ~matroska_stream_c()
1299 std::vector<matroska_segment_c*> segments;
1325 virtual ~attachment_c()
1330 std::string psz_file_name;
1331 std::string psz_mime_type;
1339 demux_sys_t( demux_t & demux )
1346 ,p_current_segment(NULL)
1347 ,dvd_interpretor( *this )
1351 ,b_pci_packet_set(false)
1354 vlc_mutex_init( &lock_demuxer );
1357 virtual ~demux_sys_t()
1361 for ( i=0; i<streams.size(); i++ )
1363 for ( i=0; i<opened_segments.size(); i++ )
1364 delete opened_segments[i];
1365 for ( i=0; i<used_segments.size(); i++ )
1366 delete used_segments[i];
1367 for ( i=0; i<stored_attachments.size(); i++ )
1368 delete stored_attachments[i];
1369 if( meta ) vlc_meta_Delete( meta );
1371 while( titles.size() )
1372 { vlc_input_title_Delete( titles.back() ); titles.pop_back();}
1374 vlc_mutex_destroy( &lock_demuxer );
1381 mtime_t i_start_pts;
1382 mtime_t i_chapter_time;
1386 std::vector<input_title_t*> titles; // matroska editions
1387 size_t i_current_title;
1389 std::vector<matroska_stream_c*> streams;
1390 std::vector<attachment_c*> stored_attachments;
1391 std::vector<matroska_segment_c*> opened_segments;
1392 std::vector<virtual_segment_c*> used_segments;
1393 virtual_segment_c *p_current_segment;
1395 dvd_command_interpretor_c dvd_interpretor;
1397 /* duration of the stream */
1400 matroska_segment_c *FindSegment( const EbmlBinary & uid ) const;
1401 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
1402 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1403 const void *p_cookie,
1404 size_t i_cookie_size,
1405 virtual_segment_c * & p_segment_found );
1406 chapter_item_c *FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found );
1408 void PreloadFamily( const matroska_segment_c & of_segment );
1409 void PreloadLinked( matroska_segment_c *p_segment );
1410 bool PreparePlayback( virtual_segment_c *p_new_segment );
1411 matroska_stream_c *AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial = false );
1412 void JumpTo( virtual_segment_c & p_segment, chapter_item_c * p_chapter );
1414 void StartUiThread();
1415 void StopUiThread();
1417 inline void SwapButtons();
1419 /* for spu variables */
1420 input_thread_t *p_input;
1422 bool b_pci_packet_set;
1423 uint8_t palette[4][4];
1424 vlc_mutex_t lock_demuxer;
1427 event_thread_t *p_ev;
1428 static void * EventThread( vlc_object_t *p_this );
1429 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1430 vlc_value_t oldval, vlc_value_t newval, void *p_data );
1431 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1432 vlc_value_t oldval, vlc_value_t newval, void *p_data );
1437 virtual_segment_c *VirtualFromSegments( matroska_segment_c *p_segment ) const;
1438 bool IsUsedSegment( matroska_segment_c &p_segment ) const;
1441 static int Demux ( demux_t * );
1442 static int Control( demux_t *, int, va_list );
1443 static void Seek ( demux_t *, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter );
1445 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
1447 static inline char * ToUTF8( const UTFstring &u )
1449 return strdup( u.GetUTF8().c_str() );
1452 /*****************************************************************************
1453 * Open: initializes matroska demux structures
1454 *****************************************************************************/
1455 static int Open( vlc_object_t * p_this )
1457 demux_t *p_demux = (demux_t*)p_this;
1459 matroska_stream_c *p_stream;
1460 matroska_segment_c *p_segment;
1461 const uint8_t *p_peek;
1462 std::string s_path, s_filename;
1463 vlc_stream_io_callback *p_io_callback;
1464 EbmlStream *p_io_stream;
1466 /* peek the begining */
1467 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
1469 /* is a valid file */
1470 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
1471 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
1473 /* Set the demux function */
1474 p_demux->pf_demux = Demux;
1475 p_demux->pf_control = Control;
1476 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
1478 p_io_callback = new vlc_stream_io_callback( p_demux->s, false );
1479 p_io_stream = new EbmlStream( *p_io_callback );
1481 if( p_io_stream == NULL )
1483 msg_Err( p_demux, "failed to create EbmlStream" );
1484 delete p_io_callback;
1486 return VLC_EGENERIC;
1489 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_io_stream, true );
1490 if( p_stream == NULL )
1492 msg_Err( p_demux, "cannot find KaxSegment" );
1495 p_sys->streams.push_back( p_stream );
1497 p_stream->p_in = p_io_callback;
1498 p_stream->p_es = p_io_stream;
1500 for (size_t i=0; i<p_stream->segments.size(); i++)
1502 p_stream->segments[i]->Preload();
1505 p_segment = p_stream->segments[0];
1506 if( p_segment->cluster == NULL )
1508 msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
1512 if (config_GetInt( p_demux, "mkv-preload-local-dir" ))
1514 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
1515 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
1517 // assume it's a regular file
1518 // get the directory path
1519 s_path = p_demux->psz_path;
1520 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
1522 s_path = s_path.substr(0,s_path.length()-1);
1526 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
1528 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
1532 DIR *p_src_dir = utf8_opendir(s_path.c_str());
1534 if (p_src_dir != NULL)
1537 while ((psz_file = utf8_readdir(p_src_dir)) != NULL)
1539 if (strlen(psz_file) > 4)
1541 s_filename = s_path + DIRECTORY_SEPARATOR + psz_file;
1544 if (!strcasecmp(s_filename.c_str(), p_demux->psz_path))
1546 if (!s_filename.compare(p_demux->psz_path))
1550 continue; // don't reuse the original opened file
1553 #if defined(__GNUC__) && (__GNUC__ < 3)
1554 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
1555 !s_filename.compare("mka", s_filename.length() - 3, 3))
1557 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
1558 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
1561 // test wether this file belongs to our family
1562 const uint8_t *p_peek;
1563 bool file_ok = false;
1564 stream_t *p_file_stream = stream_UrlNew(
1566 s_filename.c_str());
1567 /* peek the begining */
1568 if( p_file_stream &&
1569 stream_Peek( p_file_stream, &p_peek, 4 ) >= 4
1570 && p_peek[0] == 0x1a && p_peek[1] == 0x45 &&
1571 p_peek[2] == 0xdf && p_peek[3] == 0xa3 ) file_ok = true;
1575 vlc_stream_io_callback *p_file_io = new vlc_stream_io_callback( p_file_stream, true );
1576 EbmlStream *p_estream = new EbmlStream(*p_file_io);
1578 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_estream );
1580 if ( p_stream == NULL )
1582 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
1588 p_stream->p_in = p_file_io;
1589 p_stream->p_es = p_estream;
1590 p_sys->streams.push_back( p_stream );
1595 if( p_file_stream ) {
1596 stream_Delete( p_file_stream );
1598 msg_Dbg( p_demux, "the file '%s' cannot be opened", s_filename.c_str() );
1604 closedir( p_src_dir );
1608 p_sys->PreloadFamily( *p_segment );
1611 p_sys->PreloadLinked( p_segment );
1613 if ( !p_sys->PreparePlayback( NULL ) )
1615 msg_Err( p_demux, "cannot use the segment" );
1619 p_sys->StartUiThread();
1625 return VLC_EGENERIC;
1628 /*****************************************************************************
1629 * Close: frees unused data
1630 *****************************************************************************/
1631 static void Close( vlc_object_t *p_this )
1633 demux_t *p_demux = (demux_t*)p_this;
1634 demux_sys_t *p_sys = p_demux->p_sys;
1639 /*****************************************************************************
1641 *****************************************************************************/
1642 static int Control( demux_t *p_demux, int i_query, va_list args )
1644 demux_sys_t *p_sys = p_demux->p_sys;
1651 input_attachment_t ***ppp_attach;
1657 case DEMUX_GET_ATTACHMENTS:
1658 ppp_attach = (input_attachment_t***)va_arg( args, input_attachment_t*** );
1659 pi_int = (int*)va_arg( args, int * );
1661 if( p_sys->stored_attachments.size() <= 0 )
1662 return VLC_EGENERIC;
1664 *pi_int = p_sys->stored_attachments.size();
1665 *ppp_attach = (input_attachment_t**)malloc( sizeof(input_attachment_t**) *
1666 p_sys->stored_attachments.size() );
1667 if( !(*ppp_attach) )
1669 for( i = 0; i < p_sys->stored_attachments.size(); i++ )
1671 attachment_c *a = p_sys->stored_attachments[i];
1672 (*ppp_attach)[i] = vlc_input_attachment_New( a->psz_file_name.c_str(), a->psz_mime_type.c_str(), NULL,
1673 a->p_data, a->i_size );
1677 case DEMUX_GET_META:
1678 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1679 vlc_meta_Merge( p_meta, p_sys->meta );
1682 case DEMUX_GET_LENGTH:
1683 pi64 = (int64_t*)va_arg( args, int64_t * );
1684 if( p_sys->f_duration > 0.0 )
1686 *pi64 = (int64_t)(p_sys->f_duration * 1000);
1689 return VLC_EGENERIC;
1691 case DEMUX_GET_POSITION:
1692 pf = (double*)va_arg( args, double * );
1693 if ( p_sys->f_duration > 0.0 )
1694 *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);
1697 case DEMUX_SET_POSITION:
1698 f = (double)va_arg( args, double );
1699 Seek( p_demux, -1, f, NULL );
1702 case DEMUX_GET_TIME:
1703 pi64 = (int64_t*)va_arg( args, int64_t * );
1704 *pi64 = p_sys->i_pts;
1707 case DEMUX_GET_TITLE_INFO:
1708 if( p_sys->titles.size() > 1 || ( p_sys->titles.size() == 1 && p_sys->titles[0]->i_seekpoint > 0 ) )
1710 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1711 int *pi_int = (int*)va_arg( args, int* );
1713 *pi_int = p_sys->titles.size();
1714 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->titles.size() );
1716 for( size_t i = 0; i < p_sys->titles.size(); i++ )
1718 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->titles[i] );
1722 return VLC_EGENERIC;
1724 case DEMUX_SET_TITLE:
1725 /* TODO handle editions as titles */
1726 i_idx = (int)va_arg( args, int );
1727 if( i_idx < p_sys->used_segments.size() )
1729 p_sys->JumpTo( *p_sys->used_segments[i_idx], NULL );
1732 return VLC_EGENERIC;
1734 case DEMUX_SET_SEEKPOINT:
1735 i_skp = (int)va_arg( args, int );
1737 // TODO change the way it works with the << & >> buttons on the UI (+1/-1 instead of a number)
1738 if( p_sys->titles.size() && i_skp < p_sys->titles[p_sys->i_current_title]->i_seekpoint)
1740 Seek( p_demux, (int64_t)p_sys->titles[p_sys->i_current_title]->seekpoint[i_skp]->i_time_offset, -1, NULL);
1741 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
1742 p_demux->info.i_seekpoint = i_skp;
1745 return VLC_EGENERIC;
1747 case DEMUX_SET_TIME:
1750 return VLC_EGENERIC;
1754 #if LIBMATROSKA_VERSION >= 0x000800
1755 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1757 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1760 #if LIBMATROSKA_VERSION >= 0x000800
1761 pp_simpleblock = NULL;
1769 EbmlElement *el = NULL;
1773 return VLC_EGENERIC;
1775 #if LIBMATROSKA_VERSION >= 0x000800
1776 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1778 if( (el = ep->Get()) == NULL && pp_block != NULL )
1781 /* Check blocks validity to protect againts broken files */
1782 if( BlockFindTrackIndex( NULL, pp_block
1783 #if LIBMATROSKA_VERSION >= 0x000800
1789 #if LIBMATROSKA_VERSION >= 0x000800
1790 pp_simpleblock = NULL;
1796 /* update the index */
1797 #define idx p_indexes[i_index - 1]
1798 if( i_index > 0 && idx.i_time == -1 )
1800 #if LIBMATROSKA_VERSION >= 0x000800
1801 if ( pp_simpleblock != NULL )
1802 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1805 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1806 idx.b_key = *pi_ref1 == 0 ? true : false;
1812 i_level = ep->GetLevel();
1821 msg_Warn( &sys.demuxer, "EOF" );
1822 return VLC_EGENERIC;
1825 /* Verify that we are still inside our cluster
1826 * It can happens whith broken files and when seeking
1830 if( cluster && !ep->IsTopPresent( cluster ) )
1832 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1843 if( MKV_IS_ID( el, KaxCluster ) )
1845 cluster = (KaxCluster*)el;
1846 i_cluster_pos = cluster->GetElementPosition();
1848 /* add it to the index */
1850 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1852 IndexAppendCluster( cluster );
1855 // reset silent tracks
1856 for (size_t i=0; i<tracks.size(); i++)
1858 tracks[i]->b_silent = false;
1863 else if( MKV_IS_ID( el, KaxCues ) )
1865 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1866 return VLC_EGENERIC;
1870 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1874 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1876 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1878 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1879 cluster->InitTimecode( uint64( ctc ), i_timescale );
1881 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1885 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1887 i_block_pos = el->GetElementPosition();
1890 #if LIBMATROSKA_VERSION >= 0x000800
1891 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1893 pp_simpleblock = (KaxSimpleBlock*)el;
1895 pp_simpleblock->ReadData( es.I_O() );
1896 pp_simpleblock->SetParent( *cluster );
1901 if( MKV_IS_ID( el, KaxBlock ) )
1903 pp_block = (KaxBlock*)el;
1905 pp_block->ReadData( es.I_O() );
1906 pp_block->SetParent( *cluster );
1910 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1912 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1914 dur.ReadData( es.I_O() );
1915 *pi_duration = uint64( dur );
1917 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1919 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1921 ref.ReadData( es.I_O() );
1924 *pi_ref1 = int64( ref ) * cluster->GlobalTimecodeScale();
1926 else if( *pi_ref2 == 0 )
1928 *pi_ref2 = int64( ref ) * cluster->GlobalTimecodeScale();
1931 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1933 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1934 track_num.ReadData( es.I_O() );
1936 for (size_t i=0; i<tracks.size(); i++)
1938 if ( tracks[i]->i_number == uint32(track_num))
1940 tracks[i]->b_silent = true;
1947 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1948 return VLC_EGENERIC;
1953 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem, size_t offset)
1956 if( !(p_block = block_New( p_demux, i_mem + offset ) ) ) return NULL;
1957 memcpy( p_block->p_buffer + offset, p_mem, i_mem );
1958 //p_block->i_rate = p_input->stream.control.i_rate;
1962 #if LIBMATROSKA_VERSION >= 0x000800
1963 static void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
1964 mtime_t i_pts, mtime_t i_duration, bool f_mandatory )
1966 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1967 mtime_t i_duration, bool f_mandatory )
1970 demux_sys_t *p_sys = p_demux->p_sys;
1971 matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
1977 if( p_segment->BlockFindTrackIndex( &i_track, block
1978 #if LIBMATROSKA_VERSION >= 0x000800
1983 msg_Err( p_demux, "invalid track number" );
1987 mkv_track_t *tk = p_segment->tracks[i_track];
1989 if( tk->fmt.i_cat != NAV_ES && tk->p_es == NULL )
1991 msg_Err( p_demux, "unknown track number" );
1994 if( i_pts + i_duration < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1996 return; /* discard audio packets that shouldn't be rendered */
1999 if ( tk->fmt.i_cat != NAV_ES )
2001 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
2005 tk->b_inited = false;
2011 /* First send init data */
2012 if( !tk->b_inited && tk->i_data_init > 0 )
2016 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
2017 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init, 0 );
2018 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
2020 tk->b_inited = true;
2023 #if LIBMATROSKA_VERSION >= 0x000800
2025 (block != NULL && i < block->NumberFrames()) || (simpleblock != NULL && i < simpleblock->NumberFrames());
2028 for( i = 0; i < block->NumberFrames(); i++ )
2033 #if LIBMATROSKA_VERSION >= 0x000800
2034 if ( simpleblock != NULL )
2036 data = &simpleblock->GetBuffer(i);
2037 // condition when the DTS is correct (keyframe or B frame == NOT P frame)
2038 f_mandatory = simpleblock->IsDiscardable() || simpleblock->IsKeyframe();
2042 data = &block->GetBuffer(i);
2044 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER && tk->p_compression_data != NULL )
2045 p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), tk->p_compression_data->GetSize() );
2047 p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), 0 );
2049 if( p_block == NULL )
2054 #if defined(HAVE_ZLIB_H)
2055 if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB )
2057 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
2061 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER )
2063 memcpy( p_block->p_buffer, tk->p_compression_data->GetBuffer(), tk->p_compression_data->GetSize() );
2066 if ( tk->fmt.i_cat == NAV_ES )
2068 // TODO handle the start/stop times of this packet
2069 if ( p_sys->b_ui_hooked )
2071 vlc_mutex_lock( &p_sys->p_ev->lock );
2072 memcpy( &p_sys->pci_packet, &p_block->p_buffer[1], sizeof(pci_t) );
2073 p_sys->SwapButtons();
2074 p_sys->b_pci_packet_set = true;
2075 vlc_mutex_unlock( &p_sys->p_ev->lock );
2076 block_Release( p_block );
2080 // correct timestamping when B frames are used
2081 if( tk->fmt.i_cat != VIDEO_ES )
2083 p_block->i_dts = p_block->i_pts = i_pts;
2087 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
2089 // in VFW we have no idea about B frames
2091 p_block->i_dts = i_pts;
2095 p_block->i_pts = i_pts;
2097 p_block->i_dts = p_block->i_pts;
2099 p_block->i_dts = min( i_pts, tk->i_last_dts + (mtime_t)(tk->i_default_duration >> 10));
2100 p_sys->i_pts = p_block->i_dts;
2103 tk->i_last_dts = p_block->i_dts;
2106 msg_Dbg( p_demux, "block i_dts: %"PRId64" / i_pts: %"PRId64, p_block->i_dts, p_block->i_pts);
2108 if( strcmp( tk->psz_codec, "S_VOBSUB" ) )
2110 p_block->i_length = i_duration * 1000;
2113 es_out_Send( p_demux->out, tk->p_es, p_block );
2115 /* use time stamp only for first block */
2120 matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial )
2122 int i_upper_lvl = 0;
2124 EbmlElement *p_l0, *p_l1, *p_l2;
2125 bool b_keep_stream = false, b_keep_segment;
2127 // verify the EBML Header
2128 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
2131 msg_Err( p_demux, "No EBML header found" );
2135 // verify we can read this Segment, we only support Matroska version 1 for now
2136 p_l0->Read(*p_estream, EbmlHead::ClassInfos.Context, i_upper_lvl, p_l0, true);
2138 EDocType doc_type = GetChild<EDocType>(*static_cast<EbmlHead*>(p_l0));
2139 if (std::string(doc_type) != "matroska")
2141 msg_Err( p_demux, "Not a Matroska file : DocType = %s ", std::string(doc_type).c_str());
2145 EDocTypeReadVersion doc_read_version = GetChild<EDocTypeReadVersion>(*static_cast<EbmlHead*>(p_l0));
2146 #if LIBMATROSKA_VERSION >= 0x000800
2147 if (uint64(doc_read_version) > 2)
2149 msg_Err( p_demux, "This matroska file is needs version %"PRId64" and this VLC only supports version 1 & 2", uint64(doc_read_version));
2153 if (uint64(doc_read_version) != 1)
2155 msg_Err( p_demux, "This matroska file is needs version %"PRId64" and this VLC only supports version 1", uint64(doc_read_version));
2163 // find all segments in this file
2164 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFFLL);
2170 matroska_stream_c *p_stream1 = new matroska_stream_c( *this );
2174 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
2177 matroska_segment_c *p_segment1 = new matroska_segment_c( *this, *p_estream );
2178 b_keep_segment = b_initial;
2180 ep = new EbmlParser(p_estream, p_l0, &demuxer );
2181 p_segment1->ep = ep;
2182 p_segment1->segment = (KaxSegment*)p_l0;
2184 while ((p_l1 = ep->Get()))
2186 if (MKV_IS_ID(p_l1, KaxInfo))
2188 // find the families of this segment
2189 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
2191 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
2192 for( i = 0; i < p_info->ListSize(); i++ )
2194 EbmlElement *l = (*p_info)[i];
2196 if( MKV_IS_ID( l, KaxSegmentUID ) )
2198 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
2199 b_keep_segment = (FindSegment( *p_uid ) == NULL);
2200 if ( !b_keep_segment )
2201 break; // this segment is already known
2202 opened_segments.push_back( p_segment1 );
2203 delete p_segment1->p_segment_uid;
2204 p_segment1->p_segment_uid = new KaxSegmentUID(*p_uid);
2206 else if( MKV_IS_ID( l, KaxPrevUID ) )
2208 p_segment1->p_prev_segment_uid = new KaxPrevUID( *static_cast<KaxPrevUID*>(l) );
2210 else if( MKV_IS_ID( l, KaxNextUID ) )
2212 p_segment1->p_next_segment_uid = new KaxNextUID( *static_cast<KaxNextUID*>(l) );
2214 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
2216 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
2217 p_segment1->families.push_back( p_fam );
2223 if ( b_keep_segment )
2225 b_keep_stream = true;
2226 p_stream1->segments.push_back( p_segment1 );
2230 p_segment1->segment = NULL;
2234 if (p_l0->IsFiniteSize() )
2236 p_l0->SkipData(*p_estream, KaxMatroska_Context);
2237 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
2240 p_l0 = p_l0->SkipData(*p_estream, KaxSegment_Context);
2243 if ( !b_keep_stream )
2252 bool matroska_segment_c::Select( mtime_t i_start_time )
2257 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
2258 sys.b_pci_packet_set = false;
2260 for( i_track = 0; i_track < tracks.size(); i_track++ )
2262 if( tracks[i_track]->fmt.i_cat == UNKNOWN_ES )
2264 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, tracks[i_track]->i_number );
2265 tracks[i_track]->p_es = NULL;
2269 if( !strcmp( tracks[i_track]->psz_codec, "V_MS/VFW/FOURCC" ) )
2271 if( tracks[i_track]->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
2273 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
2274 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2278 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tracks[i_track]->p_extra_data;
2280 tracks[i_track]->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
2281 tracks[i_track]->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
2282 tracks[i_track]->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
2284 tracks[i_track]->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
2285 if( tracks[i_track]->fmt.i_extra > 0 )
2287 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2288 memcpy( tracks[i_track]->fmt.p_extra, &p_bih[1], tracks[i_track]->fmt.i_extra );
2292 else if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG1" ) ||
2293 !strcmp( tracks[i_track]->psz_codec, "V_MPEG2" ) )
2295 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
2297 else if( !strncmp( tracks[i_track]->psz_codec, "V_THEORA", 8 ) )
2299 uint8_t *p_data = tracks[i_track]->p_extra_data;
2300 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
2301 if( tracks[i_track]->i_extra_data >= 4 ) {
2302 if( p_data[0] == 2 ) {
2304 int i_size1 = 0, i_size2 = 0;
2306 /* read size of first header packet */
2307 while( *p_data == 0xFF &&
2308 i < tracks[i_track]->i_extra_data )
2317 msg_Dbg( &sys.demuxer, "first theora header size %d", i_size1 );
2318 /* read size of second header packet */
2319 while( *p_data == 0xFF &&
2320 i < tracks[i_track]->i_extra_data )
2329 int i_size3 = tracks[i_track]->i_extra_data - i - i_size1
2331 msg_Dbg( &sys.demuxer, "second theora header size %d", i_size2 );
2332 msg_Dbg( &sys.demuxer, "third theora header size %d", i_size3 );
2333 tracks[i_track]->fmt.i_extra = i_size1 + i_size2 + i_size3
2335 if( i_size1 > 0 && i_size2 > 0 && i_size3 > 0 ) {
2336 tracks[i_track]->fmt.p_extra =
2337 malloc( tracks[i_track]->fmt.i_extra );
2338 uint8_t *p_out = (uint8_t*)tracks[i_track]->fmt.p_extra;
2339 *p_out++ = (i_size1>>8) & 0xFF;
2340 *p_out++ = i_size1 & 0xFF;
2341 memcpy( p_out, p_data, i_size1 );
2345 *p_out++ = (i_size2>>8) & 0xFF;
2346 *p_out++ = i_size2 & 0xFF;
2347 memcpy( p_out, p_data, i_size2 );
2351 *p_out++ = (i_size3>>8) & 0xFF;
2352 *p_out++ = i_size3 & 0xFF;
2353 memcpy( p_out, p_data, i_size3 );
2359 msg_Err( &sys.demuxer, "inconsistant theora extradata" );
2363 msg_Err( &sys.demuxer, "Wrong number of ogg packets with theora headers (%d)", p_data[0] + 1 );
2367 else if( !strncmp( tracks[i_track]->psz_codec, "V_REAL/RV", 9 ) )
2369 if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV10" ) )
2370 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '1', '0' );
2371 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV20" ) )
2372 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '2', '0' );
2373 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV30" ) )
2374 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '3', '0' );
2375 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV40" ) )
2376 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '4', '0' );
2378 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4", 7 ) )
2380 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/MS/V3" ) )
2382 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
2384 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO", 11 ) )
2386 /* A MPEG 4 codec, SP, ASP, AP or AVC */
2387 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO/AVC" ) )
2388 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
2390 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
2391 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2392 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2393 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2396 else if( !strcmp( tracks[i_track]->psz_codec, "V_QUICKTIME" ) )
2398 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
2399 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
2400 tracks[i_track]->p_extra_data,
2401 tracks[i_track]->i_extra_data,
2403 MP4_ReadBoxCommon( p_mp4_stream, p_box );
2404 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
2405 tracks[i_track]->fmt.i_codec = p_box->i_type;
2406 tracks[i_track]->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
2407 tracks[i_track]->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
2408 tracks[i_track]->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
2409 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2410 memcpy( tracks[i_track]->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tracks[i_track]->fmt.i_extra );
2411 MP4_FreeBox_sample_vide( p_box );
2412 stream_Delete( p_mp4_stream );
2414 else if( !strcmp( tracks[i_track]->psz_codec, "A_MS/ACM" ) )
2416 if( tracks[i_track]->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
2418 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
2419 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2423 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tracks[i_track]->p_extra_data;
2425 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tracks[i_track]->fmt.i_codec, NULL );
2427 tracks[i_track]->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
2428 tracks[i_track]->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
2429 tracks[i_track]->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
2430 tracks[i_track]->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
2431 tracks[i_track]->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
2433 tracks[i_track]->fmt.i_extra = GetWLE( &p_wf->cbSize );
2434 if( tracks[i_track]->fmt.i_extra > 0 )
2436 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2437 memcpy( tracks[i_track]->fmt.p_extra, &p_wf[1], tracks[i_track]->fmt.i_extra );
2441 else if( !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L3" ) ||
2442 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L2" ) ||
2443 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L1" ) )
2445 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
2447 else if( !strcmp( tracks[i_track]->psz_codec, "A_AC3" ) )
2449 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2451 else if( !strcmp( tracks[i_track]->psz_codec, "A_DTS" ) )
2453 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2455 else if( !strcmp( tracks[i_track]->psz_codec, "A_FLAC" ) )
2457 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
2458 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2459 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2460 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2462 else if( !strcmp( tracks[i_track]->psz_codec, "A_VORBIS" ) )
2464 int i, i_offset = 1, i_size[3], i_extra;
2467 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
2469 /* Split the 3 headers */
2470 if( tracks[i_track]->p_extra_data[0] != 0x02 )
2471 msg_Err( &sys.demuxer, "invalid vorbis header" );
2473 for( i = 0; i < 2; i++ )
2476 while( i_offset < tracks[i_track]->i_extra_data )
2478 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
2479 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
2483 i_size[0] = __MIN(i_size[0], tracks[i_track]->i_extra_data - i_offset);
2484 i_size[1] = __MIN(i_size[1], tracks[i_track]->i_extra_data -i_offset -i_size[0]);
2485 i_size[2] = tracks[i_track]->i_extra_data - i_offset - i_size[0] - i_size[1];
2487 tracks[i_track]->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
2488 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2489 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra; i_extra = 0;
2490 for( i = 0; i < 3; i++ )
2492 *(p_extra++) = i_size[i] >> 8;
2493 *(p_extra++) = i_size[i] & 0xFF;
2494 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra,
2496 p_extra += i_size[i];
2497 i_extra += i_size[i];
2500 else if( !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
2501 !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
2503 int i_profile, i_srate, sbr = 0;
2504 static const unsigned int i_sample_rates[] =
2506 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
2507 16000, 12000, 11025, 8000, 7350, 0, 0, 0
2510 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
2511 /* create data for faad (MP4DecSpecificDescrTag)*/
2513 if( !strcmp( &tracks[i_track]->psz_codec[12], "MAIN" ) )
2517 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC" ) )
2521 else if( !strcmp( &tracks[i_track]->psz_codec[12], "SSR" ) )
2525 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC/SBR" ) )
2535 for( i_srate = 0; i_srate < 13; i_srate++ )
2537 if( i_sample_rates[i_srate] == tracks[i_track]->i_original_rate )
2542 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
2544 tracks[i_track]->fmt.i_extra = sbr ? 5 : 2;
2545 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2546 ((uint8_t*)tracks[i_track]->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
2547 ((uint8_t*)tracks[i_track]->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tracks[i_track]->fmt.audio.i_channels << 3);
2550 int syncExtensionType = 0x2B7;
2552 for (iDSRI=0; iDSRI<13; iDSRI++)
2553 if( i_sample_rates[iDSRI] == tracks[i_track]->fmt.audio.i_rate )
2555 ((uint8_t*)tracks[i_track]->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
2556 ((uint8_t*)tracks[i_track]->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
2557 ((uint8_t*)tracks[i_track]->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
2560 else if( !strcmp( tracks[i_track]->psz_codec, "A_AAC" ) )
2562 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
2563 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2564 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2565 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2567 else if( !strcmp( tracks[i_track]->psz_codec, "A_WAVPACK4" ) )
2569 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'W', 'V', 'P', 'K' );
2570 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2571 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2572 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2574 else if( !strcmp( tracks[i_track]->psz_codec, "A_TTA1" ) )
2576 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'T', 'T', 'A', '1' );
2577 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2578 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2579 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2581 else if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) ||
2582 !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/LIT" ) ||
2583 !strcmp( tracks[i_track]->psz_codec, "A_PCM/FLOAT/IEEE" ) )
2585 if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) )
2587 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
2591 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
2593 tracks[i_track]->fmt.audio.i_blockalign = ( tracks[i_track]->fmt.audio.i_bitspersample + 7 ) / 8 * tracks[i_track]->fmt.audio.i_channels;
2595 /* disabled due to the potential "S_KATE" namespace issue */
2596 else if( !strcmp( tracks[i_track]->psz_codec, "S_KATE" ) )
2598 int i, i_offset = 1, *i_size, i_extra, num_headers, size_so_far;
2601 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'k', 'a', 't', 'e' );
2602 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2604 /* Recover the number of headers to expect */
2605 num_headers = tracks[i_track]->p_extra_data[0]+1;
2606 msg_Dbg( &sys.demuxer, "kate in mkv detected: %d headers in %u bytes",
2607 num_headers, tracks[i_track]->i_extra_data);
2609 /* this won't overflow the stack as is can allocate only 1020 bytes max */
2610 i_size = (int*)alloca(num_headers*sizeof(int));
2612 /* Split the headers */
2614 for( i = 0; i < num_headers-1; i++ )
2617 while( i_offset < tracks[i_track]->i_extra_data )
2619 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
2620 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
2622 msg_Dbg( &sys.demuxer, "kate header %d is %d bytes", i, i_size[i]);
2623 size_so_far += i_size[i];
2625 i_size[num_headers-1] = tracks[i_track]->i_extra_data - (size_so_far+i_offset);
2626 msg_Dbg( &sys.demuxer, "kate last header (%d) is %d bytes", num_headers-1, i_size[num_headers-1]);
2628 tracks[i_track]->fmt.i_extra = 1 + num_headers * 2 + size_so_far + i_size[num_headers-1];
2629 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2631 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra;
2633 *(p_extra++) = num_headers;
2635 for( i = 0; i < num_headers; i++ )
2637 *(p_extra++) = i_size[i] >> 8;
2638 *(p_extra++) = i_size[i] & 0xFF;
2639 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra-1,
2641 p_extra += i_size[i];
2642 i_extra += i_size[i];
2645 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/UTF8" ) )
2647 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
2648 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2650 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/USF" ) )
2652 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
2653 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2654 if( tracks[i_track]->i_extra_data )
2656 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2657 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2658 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2661 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/SSA" ) ||
2662 !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASS" ) ||
2663 !strcmp( tracks[i_track]->psz_codec, "S_SSA" ) ||
2664 !strcmp( tracks[i_track]->psz_codec, "S_ASS" ))
2666 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
2667 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2668 if( tracks[i_track]->i_extra_data )
2670 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2671 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2672 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2675 else if( !strcmp( tracks[i_track]->psz_codec, "S_VOBSUB" ) )
2677 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
2678 if( tracks[i_track]->i_extra_data )
2681 char *p_buf = (char *)malloc( tracks[i_track]->i_extra_data + 1);
2682 memcpy( p_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
2683 p_buf[tracks[i_track]->i_extra_data] = '\0';
2685 p_start = strstr( p_buf, "size:" );
2686 if( sscanf( p_start, "size: %dx%d",
2687 &tracks[i_track]->fmt.subs.spu.i_original_frame_width, &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) == 2 )
2689 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 );
2693 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
2698 else if( !strcmp( tracks[i_track]->psz_codec, "B_VOBBTN" ) )
2700 tracks[i_track]->fmt.i_cat = NAV_ES;
2705 msg_Err( &sys.demuxer, "unknown codec id=`%s'", tracks[i_track]->psz_codec );
2706 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2708 if( tracks[i_track]->b_default )
2710 tracks[i_track]->fmt.i_priority = 1000;
2713 tracks[i_track]->p_es = es_out_Add( sys.demuxer.out, &tracks[i_track]->fmt );
2715 /* Turn on a subtitles track if it has been flagged as default -
2716 * but only do this if no subtitles track has already been engaged,
2717 * either by an earlier 'default track' (??) or by default
2718 * language choice behaviour.
2720 if( tracks[i_track]->b_default )
2722 es_out_Control( sys.demuxer.out,
2724 tracks[i_track]->p_es );
2727 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, i_start_time );
2730 sys.i_start_pts = i_start_time;
2731 // reset the stream reading to the first cluster of the segment used
2732 es.I_O().setFilePointer( i_start_pos );
2735 ep = new EbmlParser( &es, segment, &sys.demuxer );
2740 void demux_sys_t::StartUiThread()
2744 msg_Dbg( &demuxer, "Starting the UI Hook" );
2746 /* FIXME hack hack hack hack FIXME */
2747 /* Get p_input and create variable */
2748 p_input = (input_thread_t *) vlc_object_find( &demuxer, VLC_OBJECT_INPUT, FIND_PARENT );
2749 var_Create( p_input, "x-start", VLC_VAR_INTEGER );
2750 var_Create( p_input, "y-start", VLC_VAR_INTEGER );
2751 var_Create( p_input, "x-end", VLC_VAR_INTEGER );
2752 var_Create( p_input, "y-end", VLC_VAR_INTEGER );
2753 var_Create( p_input, "color", VLC_VAR_ADDRESS );
2754 var_Create( p_input, "menu-palette", VLC_VAR_ADDRESS );
2755 var_Create( p_input, "highlight", VLC_VAR_BOOL );
2756 var_Create( p_input, "highlight-mutex", VLC_VAR_MUTEX );
2758 /* Now create our event thread catcher */
2759 p_ev = (event_thread_t *) vlc_object_create( &demuxer, sizeof( event_thread_t ) );
2760 p_ev->p_demux = &demuxer;
2761 p_ev->b_die = false;
2762 vlc_mutex_init( &p_ev->lock );
2763 vlc_thread_create( p_ev, "mkv event thread handler", EventThread,
2764 VLC_THREAD_PRIORITY_LOW, false );
2768 void demux_sys_t::StopUiThread()
2772 vlc_object_kill( p_ev );
2773 vlc_thread_join( p_ev );
2774 vlc_object_release( p_ev );
2778 var_Destroy( p_input, "highlight-mutex" );
2779 var_Destroy( p_input, "highlight" );
2780 var_Destroy( p_input, "x-start" );
2781 var_Destroy( p_input, "x-end" );
2782 var_Destroy( p_input, "y-start" );
2783 var_Destroy( p_input, "y-end" );
2784 var_Destroy( p_input, "color" );
2785 var_Destroy( p_input, "menu-palette" );
2787 vlc_object_release( p_input );
2789 msg_Dbg( &demuxer, "Stopping the UI Hook" );
2791 b_ui_hooked = false;
2794 int demux_sys_t::EventMouse( vlc_object_t *p_this, char const *psz_var,
2795 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2797 event_thread_t *p_ev = (event_thread_t *) p_data;
2798 vlc_mutex_lock( &p_ev->lock );
2799 if( psz_var[6] == 'c' )
2801 p_ev->b_clicked = true;
2802 msg_Dbg( p_this, "Event Mouse: clicked");
2804 else if( psz_var[6] == 'm' )
2805 p_ev->b_moved = true;
2806 vlc_mutex_unlock( &p_ev->lock );
2811 int demux_sys_t::EventKey( vlc_object_t *p_this, char const *,
2812 vlc_value_t, vlc_value_t newval, void *p_data )
2814 event_thread_t *p_ev = (event_thread_t *) p_data;
2815 vlc_mutex_lock( &p_ev->lock );
2816 p_ev->i_key_action = newval.i_int;
2817 vlc_mutex_unlock( &p_ev->lock );
2818 msg_Dbg( p_this, "Event Key");
2823 void * demux_sys_t::EventThread( vlc_object_t *p_this )
2825 event_thread_t *p_ev = (event_thread_t*)p_this;
2826 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
2827 vlc_object_t *p_vout = NULL;
2829 p_ev->b_moved = false;
2830 p_ev->b_clicked = false;
2831 p_ev->i_key_action = 0;
2833 /* catch all key event */
2834 var_AddCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
2837 while( vlc_object_alive (p_ev) )
2839 if ( !p_sys->b_pci_packet_set )
2846 bool b_activated = false;
2849 if( p_ev->i_key_action )
2853 msg_Dbg( p_ev->p_demux, "Handle Key Event");
2855 vlc_mutex_lock( &p_ev->lock );
2857 pci_t *pci = (pci_t *) &p_sys->pci_packet;
2859 uint16 i_curr_button = p_sys->dvd_interpretor.GetSPRM( 0x88 );
2861 switch( p_ev->i_key_action )
2863 case ACTIONID_NAV_LEFT:
2864 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2866 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2867 if ( p_button_ptr->left > 0 && p_button_ptr->left <= pci->hli.hl_gi.btn_ns )
2869 i_curr_button = p_button_ptr->left;
2870 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2871 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2872 if ( button_ptr.auto_action_mode )
2874 vlc_mutex_unlock( &p_ev->lock );
2875 vlc_mutex_lock( &p_sys->lock_demuxer );
2877 // process the button action
2878 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2880 vlc_mutex_unlock( &p_sys->lock_demuxer );
2881 vlc_mutex_lock( &p_ev->lock );
2886 case ACTIONID_NAV_RIGHT:
2887 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2889 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2890 if ( p_button_ptr->right > 0 && p_button_ptr->right <= pci->hli.hl_gi.btn_ns )
2892 i_curr_button = p_button_ptr->right;
2893 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2894 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2895 if ( button_ptr.auto_action_mode )
2897 vlc_mutex_unlock( &p_ev->lock );
2898 vlc_mutex_lock( &p_sys->lock_demuxer );
2900 // process the button action
2901 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2903 vlc_mutex_unlock( &p_sys->lock_demuxer );
2904 vlc_mutex_lock( &p_ev->lock );
2909 case ACTIONID_NAV_UP:
2910 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2912 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2913 if ( p_button_ptr->up > 0 && p_button_ptr->up <= pci->hli.hl_gi.btn_ns )
2915 i_curr_button = p_button_ptr->up;
2916 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2917 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2918 if ( button_ptr.auto_action_mode )
2920 vlc_mutex_unlock( &p_ev->lock );
2921 vlc_mutex_lock( &p_sys->lock_demuxer );
2923 // process the button action
2924 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2926 vlc_mutex_unlock( &p_sys->lock_demuxer );
2927 vlc_mutex_lock( &p_ev->lock );
2932 case ACTIONID_NAV_DOWN:
2933 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2935 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2936 if ( p_button_ptr->down > 0 && p_button_ptr->down <= pci->hli.hl_gi.btn_ns )
2938 i_curr_button = p_button_ptr->down;
2939 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2940 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2941 if ( button_ptr.auto_action_mode )
2943 vlc_mutex_unlock( &p_ev->lock );
2944 vlc_mutex_lock( &p_sys->lock_demuxer );
2946 // process the button action
2947 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2949 vlc_mutex_unlock( &p_sys->lock_demuxer );
2950 vlc_mutex_lock( &p_ev->lock );
2955 case ACTIONID_NAV_ACTIVATE:
2958 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2960 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2962 vlc_mutex_unlock( &p_ev->lock );
2963 vlc_mutex_lock( &p_sys->lock_demuxer );
2965 // process the button action
2966 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2968 vlc_mutex_unlock( &p_sys->lock_demuxer );
2969 vlc_mutex_lock( &p_ev->lock );
2975 p_ev->i_key_action = 0;
2976 vlc_mutex_unlock( &p_ev->lock );
2980 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
2982 vlc_value_t valx, valy;
2984 vlc_mutex_lock( &p_ev->lock );
2985 pci_t *pci = (pci_t *) &p_sys->pci_packet;
2986 var_Get( p_vout, "mouse-x", &valx );
2987 var_Get( p_vout, "mouse-y", &valy );
2989 if( p_ev->b_clicked )
2992 int32_t best,dist,d;
2993 int32_t mx,my,dx,dy;
2995 msg_Dbg( p_ev->p_demux, "Handle Mouse Event: Mouse clicked x(%d)*y(%d)", (unsigned)valx.i_int, (unsigned)valy.i_int);
2998 // get current button
3000 dist = 0x08000000; /* >> than (720*720)+(567*567); */
3001 for(button = 1; button <= pci->hli.hl_gi.btn_ns; button++)
3003 btni_t *button_ptr = &(pci->hli.btnit[button-1]);
3005 if(((unsigned)valx.i_int >= button_ptr->x_start)
3006 && ((unsigned)valx.i_int <= button_ptr->x_end)
3007 && ((unsigned)valy.i_int >= button_ptr->y_start)
3008 && ((unsigned)valy.i_int <= button_ptr->y_end))
3010 mx = (button_ptr->x_start + button_ptr->x_end)/2;
3011 my = (button_ptr->y_start + button_ptr->y_end)/2;
3012 dx = mx - valx.i_int;
3013 dy = my - valy.i_int;
3014 d = (dx*dx) + (dy*dy);
3015 /* If the mouse is within the button and the mouse is closer
3016 * to the center of this button then it is the best choice. */
3026 btni_t button_ptr = pci->hli.btnit[best-1];
3027 uint16 i_curr_button = p_sys->dvd_interpretor.GetSPRM( 0x88 );
3029 msg_Dbg( &p_sys->demuxer, "Clicked button %d", best );
3030 vlc_mutex_unlock( &p_ev->lock );
3031 vlc_mutex_lock( &p_sys->lock_demuxer );
3033 // process the button action
3034 p_sys->dvd_interpretor.SetSPRM( 0x88, best );
3035 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
3037 msg_Dbg( &p_sys->demuxer, "Processed button %d", best );
3039 // select new button
3040 if ( best != i_curr_button )
3044 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
3046 vlc_mutex_t *p_mutex = (vlc_mutex_t *) val.p_address;
3049 if(button_ptr.btn_coln != 0) {
3050 i_palette = pci->hli.btn_colit.btn_coli[button_ptr.btn_coln-1][1];
3055 for( int i = 0; i < 4; i++ )
3057 uint32_t i_yuv = 0xFF;//p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
3058 uint8_t i_alpha = (i_palette>>(i*4))&0x0f;
3059 i_alpha = i_alpha == 0xf ? 0xff : i_alpha << 4;
3061 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
3062 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
3063 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
3064 p_sys->palette[i][3] = i_alpha;
3067 vlc_mutex_lock( p_mutex );
3068 val.i_int = button_ptr.x_start; var_Set( p_sys->p_input, "x-start", val );
3069 val.i_int = button_ptr.x_end; var_Set( p_sys->p_input, "x-end", val );
3070 val.i_int = button_ptr.y_start; var_Set( p_sys->p_input, "y-start", val );
3071 val.i_int = button_ptr.y_end; var_Set( p_sys->p_input, "y-end", val );
3073 val.p_address = (void *)p_sys->palette;
3074 var_Set( p_sys->p_input, "menu-palette", val );
3076 val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
3077 vlc_mutex_unlock( p_mutex );
3080 vlc_mutex_unlock( &p_sys->lock_demuxer );
3081 vlc_mutex_lock( &p_ev->lock );
3084 else if( p_ev->b_moved )
3086 // dvdnav_mouse_select( NULL, pci, valx.i_int, valy.i_int );
3089 p_ev->b_moved = false;
3090 p_ev->b_clicked = false;
3091 vlc_mutex_unlock( &p_ev->lock );
3095 if( p_vout && !vlc_object_alive (p_vout) )
3097 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3098 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3099 vlc_object_release( p_vout );
3103 else if( p_vout == NULL )
3105 p_vout = (vlc_object_t*) vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
3109 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3110 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3114 /* Wait a bit, 10ms */
3118 /* Release callback */
3121 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3122 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3123 vlc_object_release( p_vout );
3125 var_DelCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
3127 vlc_mutex_destroy( &p_ev->lock );
3132 void matroska_segment_c::UnSelect( )
3136 for( i_track = 0; i_track < tracks.size(); i_track++ )
3138 if ( tracks[i_track]->p_es != NULL )
3140 // es_format_Clean( &tracks[i_track]->fmt );
3141 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
3142 tracks[i_track]->p_es = NULL;
3149 void virtual_segment_c::PrepareChapters( )
3151 if ( linked_segments.size() == 0 )
3154 // !!! should be called only once !!!
3155 matroska_segment_c *p_segment;
3158 // copy editions from the first segment
3159 p_segment = linked_segments[0];
3160 p_editions = &p_segment->stored_editions;
3162 for ( i=1 ; i<linked_segments.size(); i++ )
3164 p_segment = linked_segments[i];
3165 // FIXME assume we have the same editions in all segments
3166 for (j=0; j<p_segment->stored_editions.size(); j++)
3168 if( j >= p_editions->size() ) /* Protect against broken files (?) */
3170 (*p_editions)[j]->Append( *p_segment->stored_editions[j] );
3175 std::string chapter_edition_c::GetMainName() const
3177 if ( sub_chapters.size() )
3179 return sub_chapters[0]->GetCodecName( true );
3184 int chapter_item_c::PublishChapters( input_title_t & title, int & i_user_chapters, int i_level )
3186 // add support for meta-elements from codec like DVD Titles
3187 if ( !b_display_seekpoint || psz_name == "" )
3189 psz_name = GetCodecName();
3190 if ( psz_name != "" )
3191 b_display_seekpoint = true;
3194 if (b_display_seekpoint)
3196 seekpoint_t *sk = vlc_seekpoint_New();
3198 sk->i_level = i_level;
3199 sk->i_time_offset = i_start_time;
3200 sk->psz_name = strdup( psz_name.c_str() );
3202 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
3203 title.i_seekpoint++;
3204 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
3205 title.seekpoint[title.i_seekpoint-1] = sk;
3207 if ( b_user_display )
3211 for ( size_t i=0; i<sub_chapters.size() ; i++)
3213 sub_chapters[i]->PublishChapters( title, i_user_chapters, i_level+1 );
3216 i_seekpoint_num = i_user_chapters;
3218 return i_user_chapters;
3221 bool virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
3223 demux_sys_t & sys = *demux.p_sys;
3224 chapter_item_c *psz_curr_chapter;
3225 bool b_has_seeked = false;
3227 /* update current chapter/seekpoint */
3228 if ( p_editions->size() )
3230 /* 1st, we need to know in which chapter we are */
3231 psz_curr_chapter = (*p_editions)[i_current_edition]->FindTimecode( sys.i_pts, psz_current_chapter );
3233 /* we have moved to a new chapter */
3234 if (psz_curr_chapter != NULL && psz_current_chapter != psz_curr_chapter)
3236 if ( (*p_editions)[i_current_edition]->b_ordered )
3238 // Leave/Enter up to the link point
3239 b_has_seeked = psz_curr_chapter->EnterAndLeave( psz_current_chapter );
3240 if ( !b_has_seeked )
3242 // only physically seek if necessary
3243 if ( psz_current_chapter == NULL || (psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time) )
3244 Seek( demux, sys.i_pts, 0, psz_curr_chapter, -1 );
3248 if ( !b_has_seeked )
3250 psz_current_chapter = psz_curr_chapter;
3251 if ( psz_curr_chapter->i_seekpoint_num > 0 )
3253 demux.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
3254 demux.info.i_title = sys.i_current_title = i_sys_title;
3255 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
3261 else if (psz_curr_chapter == NULL)
3263 // out of the scope of the data described by chapters, leave the edition
3264 if ( (*p_editions)[i_current_edition]->b_ordered && psz_current_chapter != NULL )
3266 if ( !(*p_editions)[i_current_edition]->EnterAndLeave( psz_current_chapter, false ) )
3267 psz_current_chapter = NULL;
3276 chapter_item_c *virtual_segment_c::BrowseCodecPrivate( unsigned int codec_id,
3277 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3278 const void *p_cookie,
3279 size_t i_cookie_size )
3281 // FIXME don't assume it is the first edition
3282 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
3283 if ( index != p_editions->end() )
3285 chapter_item_c *p_result = (*index)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3286 if ( p_result != NULL )
3292 chapter_item_c *virtual_segment_c::FindChapter( int64_t i_find_uid )
3294 // FIXME don't assume it is the first edition
3295 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
3296 if ( index != p_editions->end() )
3298 chapter_item_c *p_result = (*index)->FindChapter( i_find_uid );
3299 if ( p_result != NULL )
3305 chapter_item_c *chapter_item_c::BrowseCodecPrivate( unsigned int codec_id,
3306 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3307 const void *p_cookie,
3308 size_t i_cookie_size )
3311 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3312 while ( index != codecs.end() )
3314 if ( match( **index ,p_cookie, i_cookie_size ) )
3320 chapter_item_c *p_result = NULL;
3321 std::vector<chapter_item_c*>::const_iterator index2 = sub_chapters.begin();
3322 while ( index2 != sub_chapters.end() )
3324 p_result = (*index2)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3325 if ( p_result != NULL )
3333 void chapter_item_c::Append( const chapter_item_c & chapter )
3335 // we are appending content for the same chapter UID
3337 chapter_item_c *p_chapter;
3339 for ( i=0; i<chapter.sub_chapters.size(); i++ )
3341 p_chapter = FindChapter( chapter.sub_chapters[i]->i_uid );
3342 if ( p_chapter != NULL )
3344 p_chapter->Append( *chapter.sub_chapters[i] );
3348 sub_chapters.push_back( chapter.sub_chapters[i] );
3352 i_user_start_time = min( i_user_start_time, chapter.i_user_start_time );
3353 i_user_end_time = max( i_user_end_time, chapter.i_user_end_time );
3356 chapter_item_c * chapter_item_c::FindChapter( int64_t i_find_uid )
3359 chapter_item_c *p_result = NULL;
3361 if ( i_uid == i_find_uid )
3364 for ( i=0; i<sub_chapters.size(); i++)
3366 p_result = sub_chapters[i]->FindChapter( i_find_uid );
3367 if ( p_result != NULL )
3373 std::string chapter_item_c::GetCodecName( bool f_for_title ) const
3377 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3378 while ( index != codecs.end() )
3380 result = (*index)->GetCodecName( f_for_title );
3389 std::string dvd_chapter_codec_c::GetCodecName( bool f_for_title ) const
3392 if ( p_private_data->GetSize() >= 3)
3394 const binary* p_data = p_private_data->GetBuffer();
3395 /* if ( p_data[0] == MATROSKA_DVD_LEVEL_TT )
3397 uint16_t i_title = (p_data[1] << 8) + p_data[2];
3399 sprintf( psz_str, " %d ---", i_title );
3400 result = N_("--- DVD Title");
3403 else */ if ( p_data[0] == MATROSKA_DVD_LEVEL_LU )
3406 sprintf( psz_str, " (%c%c) ---", p_data[1], p_data[2] );
3407 result = N_("--- DVD Menu");
3410 else if ( p_data[0] == MATROSKA_DVD_LEVEL_SS && f_for_title )
3412 if ( p_data[1] == 0x00 )
3413 result = N_("First Played");
3414 else if ( p_data[1] == 0xC0 )
3415 result = N_("Video Manager");
3416 else if ( p_data[1] == 0x80 )
3418 uint16_t i_title = (p_data[2] << 8) + p_data[3];
3420 sprintf( psz_str, " %d -----", i_title );
3421 result = N_("----- Title");
3430 int16 chapter_item_c::GetTitleNumber( ) const
3434 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3435 while ( index != codecs.end() )
3437 result = (*index)->GetTitleNumber( );
3446 int16 dvd_chapter_codec_c::GetTitleNumber()
3448 if ( p_private_data->GetSize() >= 3)
3450 const binary* p_data = p_private_data->GetBuffer();
3451 if ( p_data[0] == MATROSKA_DVD_LEVEL_SS )
3453 return int16( (p_data[2] << 8) + p_data[3] );
3459 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter )
3461 demux_sys_t *p_sys = p_demux->p_sys;
3462 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
3463 matroska_segment_c *p_segment = p_vsegment->Segment();
3464 mtime_t i_time_offset = 0;
3465 int64_t i_global_position = -1;
3469 msg_Dbg( p_demux, "seek request to %"PRId64" (%f%%)", i_date, f_percent );
3470 if( i_date < 0 && f_percent < 0 )
3472 msg_Warn( p_demux, "cannot seek nowhere !" );
3475 if( f_percent > 1.0 )
3477 msg_Warn( p_demux, "cannot seek so far !" );
3481 /* seek without index or without date */
3482 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
3484 if( p_sys->f_duration >= 0 && p_segment->b_cues )
3486 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
3490 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
3492 msg_Dbg( p_demux, "inaccurate way of seeking for pos:%"PRId64, i_pos );
3493 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
3495 if( p_segment->b_cues && p_segment->p_indexes[i_index].i_position < i_pos )
3497 if( !p_segment->b_cues && p_segment->p_indexes[i_index].i_position >= i_pos && p_segment->p_indexes[i_index].i_time > 0 )
3500 if( i_index == p_segment->i_index )
3505 i_date = p_segment->p_indexes[i_index].i_time;
3507 if( !p_segment->b_cues && ( p_segment->p_indexes[i_index].i_position < i_pos || p_segment->p_indexes[i_index].i_position - i_pos > 2000000 ))
3509 msg_Dbg( p_demux, "no cues, seek request to global pos: %"PRId64, i_pos );
3510 i_global_position = i_pos;
3515 p_vsegment->Seek( *p_demux, i_date, i_time_offset, psz_chapter, i_global_position );
3518 /*****************************************************************************
3519 * Demux: reads and demuxes data packets
3520 *****************************************************************************
3521 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
3522 *****************************************************************************/
3523 static int Demux( demux_t *p_demux)
3525 demux_sys_t *p_sys = p_demux->p_sys;
3527 vlc_mutex_lock( &p_sys->lock_demuxer );
3529 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
3530 matroska_segment_c *p_segment = p_vsegment->Segment();
3531 if ( p_segment == NULL ) return 0;
3532 int i_block_count = 0;
3537 if ( p_sys->demuxer.b_die )
3540 if( p_sys->i_pts >= p_sys->i_start_pts )
3541 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
3547 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
3549 /* nothing left to read in this ordered edition */
3550 if ( !p_vsegment->SelectNext() )
3552 p_segment->UnSelect( );
3554 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3556 /* switch to the next segment */
3557 p_segment = p_vsegment->Segment();
3558 if ( !p_segment->Select( 0 ) )
3560 msg_Err( p_demux, "Failed to select new segment" );
3567 int64_t i_block_duration = 0;
3568 int64_t i_block_ref1;
3569 int64_t i_block_ref2;
3571 #if LIBMATROSKA_VERSION >= 0x000800
3572 KaxSimpleBlock *simpleblock;
3574 if( p_segment->BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
3576 if( p_segment->BlockGet( block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
3579 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered )
3581 const chapter_item_c *p_chap = p_vsegment->CurrentChapter();
3582 // check if there are more chapters to read
3583 if ( p_chap != NULL )
3585 /* TODO handle successive chapters with the same user_start_time/user_end_time
3586 if ( p_chap->i_user_start_time == p_chap->i_user_start_time )
3587 p_vsegment->SelectNext();
3589 p_sys->i_pts = p_chap->i_user_end_time;
3590 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
3599 msg_Warn( p_demux, "cannot get block EOF?" );
3600 p_segment->UnSelect( );
3602 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3604 /* switch to the next segment */
3605 if ( !p_vsegment->SelectNext() )
3606 // no more segments in this stream
3608 p_segment = p_vsegment->Segment();
3609 if ( !p_segment->Select( 0 ) )
3611 msg_Err( p_demux, "Failed to select new segment" );
3619 #if LIBMATROSKA_VERSION >= 0x000800
3620 if ( simpleblock != NULL )
3621 p_sys->i_pts = (p_sys->i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
3624 p_sys->i_pts = (p_sys->i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
3626 if( p_sys->i_pts >= p_sys->i_start_pts )
3628 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
3630 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
3638 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
3640 /* nothing left to read in this ordered edition */
3641 if ( !p_vsegment->SelectNext() )
3646 p_segment->UnSelect( );
3648 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3650 /* switch to the next segment */
3651 p_segment = p_vsegment->Segment();
3652 if ( !p_segment->Select( 0 ) )
3654 msg_Err( p_demux, "Failed to select new segment" );
3662 #if LIBMATROSKA_VERSION >= 0x000800
3663 BlockDecode( p_demux, block, simpleblock, p_sys->i_pts, i_block_duration, i_block_ref1 >= 0 || i_block_ref2 > 0 );
3665 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration, i_block_ref1 >= 0 || i_block_ref2 > 0 );
3671 // TODO optimize when there is need to leave or when seeking has been called
3672 if( i_block_count > 5 )
3679 vlc_mutex_unlock( &p_sys->lock_demuxer );
3686 /*****************************************************************************
3688 *****************************************************************************/
3689 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_, bool b_owner_ )
3696 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
3698 if( i_size <= 0 || mb_eof )
3703 return stream_Read( s, p_buffer, i_size );
3705 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
3711 case seek_beginning:
3715 i_pos = stream_Size( s ) - i_offset;
3718 i_pos= stream_Tell( s ) + i_offset;
3722 if( i_pos < 0 || i_pos >= stream_Size( s ) )
3729 if( stream_Seek( s, i_pos ) )
3735 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
3739 uint64 vlc_stream_io_callback::getFilePointer( void )
3743 return stream_Tell( s );
3745 void vlc_stream_io_callback::close( void )
3751 /*****************************************************************************
3752 * Ebml Stream parser
3753 *****************************************************************************/
3754 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux )
3761 mi_remain_size[0] = el_start->GetSize();
3763 for( i = 1; i < 6; i++ )
3770 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
3773 EbmlParser::~EbmlParser( void )
3777 for( i = 1; i < mi_level; i++ )
3787 EbmlElement* EbmlParser::UnGet( uint64 i_block_pos, uint64 i_cluster_pos )
3789 if ( mi_user_level > mi_level )
3791 while ( mi_user_level != mi_level )
3793 delete m_el[mi_user_level];
3794 m_el[mi_user_level] = NULL;
3800 if ( m_el[1]->GetElementPosition() == i_cluster_pos )
3802 m_es->I_O().setFilePointer( i_block_pos, seek_beginning );
3803 return (EbmlMaster*) m_el[1];
3807 // seek to the previous Cluster
3808 m_es->I_O().setFilePointer( i_cluster_pos, seek_beginning );
3811 delete m_el[mi_level];
3812 m_el[mi_level] = NULL;
3817 void EbmlParser::Up( void )
3819 if( mi_user_level == mi_level )
3821 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
3827 void EbmlParser::Down( void )
3833 void EbmlParser::Keep( void )
3838 int EbmlParser::GetLevel( void )
3840 return mi_user_level;
3843 void EbmlParser::Reset( demux_t *p_demux )
3845 while ( mi_level > 0)
3847 delete m_el[mi_level];
3848 m_el[mi_level] = NULL;
3851 mi_user_level = mi_level = 1;
3852 #if LIBEBML_VERSION >= 0x000704
3853 // a little faster and cleaner
3854 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
3856 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
3858 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
3861 #if LIBMATROSKA_VERSION >= 0x000800
3862 /* This function workarounds a bug in KaxBlockVirtual implementation */
3863 class KaxBlockVirtualWorkaround : public KaxBlockVirtual
3868 if( Data == DataBlock )
3869 SetBuffer( NULL, 0 );
3874 EbmlElement *EbmlParser::Get( void )
3878 if( mi_user_level != mi_level )
3884 EbmlElement *ret = m_got;
3890 if( m_el[mi_level] )
3892 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
3895 #if LIBMATROSKA_VERSION >= 0x000800
3896 if( MKV_IS_ID( m_el[mi_level], KaxBlockVirtual ) )
3897 static_cast<KaxBlockVirtualWorkaround*>(m_el[mi_level])->Fix();
3899 delete m_el[mi_level];
3904 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, mb_dummy != 0, 1 );
3905 // mi_remain_size[mi_level] = m_el[mi_level]->GetSize();
3916 delete m_el[mi_level - 1];
3917 m_got = m_el[mi_level -1] = m_el[mi_level];
3918 m_el[mi_level] = NULL;
3925 else if( m_el[mi_level] == NULL )
3927 fprintf( stderr," m_el[mi_level] == NULL\n" );
3930 return m_el[mi_level];
3933 bool EbmlParser::IsTopPresent( EbmlElement *el )
3935 for( int i = 0; i < mi_level; i++ )
3937 if( m_el[i] && m_el[i] == el )
3943 /*****************************************************************************
3945 * * LoadCues : load the cues element and update index
3947 * * LoadTags : load ... the tags element
3949 * * InformationCreate : create all information, load tags if present
3951 *****************************************************************************/
3952 void matroska_segment_c::LoadCues( KaxCues *cues )
3960 msg_Err( &sys.demuxer, "There can be only 1 Cues per section." );
3964 ep = new EbmlParser( &es, cues, &sys.demuxer );
3965 while( ( el = ep->Get() ) != NULL )
3967 if( MKV_IS_ID( el, KaxCuePoint ) )
3969 #define idx p_indexes[i_index]
3972 idx.i_block_number= -1;
3973 idx.i_position = -1;
3978 while( ( el = ep->Get() ) != NULL )
3980 if( MKV_IS_ID( el, KaxCueTime ) )
3982 KaxCueTime &ctime = *(KaxCueTime*)el;
3984 ctime.ReadData( es.I_O() );
3986 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
3988 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
3991 while( ( el = ep->Get() ) != NULL )
3993 if( MKV_IS_ID( el, KaxCueTrack ) )
3995 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
3997 ctrack.ReadData( es.I_O() );
3998 idx.i_track = uint16( ctrack );
4000 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
4002 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
4004 ccpos.ReadData( es.I_O() );
4005 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
4007 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
4009 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
4011 cbnum.ReadData( es.I_O() );
4012 idx.i_block_number = uint32( cbnum );
4016 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
4023 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
4029 msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
4030 " track=%d bnum=%d", idx.i_time, idx.i_position,
4031 idx.i_track, idx.i_block_number );
4035 if( i_index >= i_index_max )
4037 i_index_max += 1024;
4038 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
4044 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
4049 msg_Dbg( &sys.demuxer, "| - loading cues done." );
4052 void matroska_segment_c::LoadTags( KaxTags *tags )
4058 /* Master elements */
4059 ep = new EbmlParser( &es, tags, &sys.demuxer );
4061 while( ( el = ep->Get() ) != NULL )
4063 if( MKV_IS_ID( el, KaxTag ) )
4065 msg_Dbg( &sys.demuxer, "+ Tag" );
4067 while( ( el = ep->Get() ) != NULL )
4069 if( MKV_IS_ID( el, KaxTagTargets ) )
4071 msg_Dbg( &sys.demuxer, "| + Targets" );
4073 while( ( el = ep->Get() ) != NULL )
4075 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4079 else if( MKV_IS_ID( el, KaxTagGeneral ) )
4081 msg_Dbg( &sys.demuxer, "| + General" );
4083 while( ( el = ep->Get() ) != NULL )
4085 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4089 else if( MKV_IS_ID( el, KaxTagGenres ) )
4091 msg_Dbg( &sys.demuxer, "| + Genres" );
4093 while( ( el = ep->Get() ) != NULL )
4095 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4099 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
4101 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
4103 while( ( el = ep->Get() ) != NULL )
4105 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4109 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
4111 msg_Dbg( &sys.demuxer, "| + Images Specific" );
4113 while( ( el = ep->Get() ) != NULL )
4115 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4119 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
4121 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
4123 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
4125 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
4127 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
4129 msg_Dbg( &sys.demuxer, "| + Multi Date" );
4131 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
4133 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
4135 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
4137 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
4139 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
4141 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
4143 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
4145 msg_Dbg( &sys.demuxer, "| + Multi Title" );
4149 msg_Dbg( &sys.demuxer, "| + LoadTag Unknown (%s)", typeid( *el ).name() );
4156 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
4161 msg_Dbg( &sys.demuxer, "loading tags done." );
4164 /*****************************************************************************
4166 *****************************************************************************/
4167 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
4172 int i_upper_level = 0;
4177 stream_Control( sys.demuxer.s, STREAM_CAN_SEEK, &b_seekable );
4181 ep = new EbmlParser( &es, seekhead, &sys.demuxer );
4183 while( ( l = ep->Get() ) != NULL )
4185 if( MKV_IS_ID( l, KaxSeek ) )
4187 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
4190 msg_Dbg( &sys.demuxer, "| | + Seek" );
4192 while( ( l = ep->Get() ) != NULL )
4194 if( MKV_IS_ID( l, KaxSeekID ) )
4196 KaxSeekID &sid = *(KaxSeekID*)l;
4197 sid.ReadData( es.I_O() );
4198 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
4200 else if( MKV_IS_ID( l, KaxSeekPosition ) )
4202 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
4203 spos.ReadData( es.I_O() );
4204 i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) );
4208 /* Many mkvmerge files hit this case. It seems to be a broken SeekHead */
4209 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4216 if( id == KaxCues::ClassInfos.GlobalId )
4218 msg_Dbg( &sys.demuxer, "| - cues at %"PRId64, i_pos );
4219 LoadSeekHeadItem( KaxCues::ClassInfos, i_pos );
4221 else if( id == KaxInfo::ClassInfos.GlobalId )
4223 msg_Dbg( &sys.demuxer, "| - info at %"PRId64, i_pos );
4224 LoadSeekHeadItem( KaxInfo::ClassInfos, i_pos );
4226 else if( id == KaxChapters::ClassInfos.GlobalId )
4228 msg_Dbg( &sys.demuxer, "| - chapters at %"PRId64, i_pos );
4229 LoadSeekHeadItem( KaxChapters::ClassInfos, i_pos );
4231 else if( id == KaxTags::ClassInfos.GlobalId )
4233 msg_Dbg( &sys.demuxer, "| - tags at %"PRId64, i_pos );
4234 LoadSeekHeadItem( KaxTags::ClassInfos, i_pos );
4236 else if( id == KaxSeekHead::ClassInfos.GlobalId )
4238 msg_Dbg( &sys.demuxer, "| - chained seekhead at %"PRId64, i_pos );
4239 LoadSeekHeadItem( KaxSeekHead::ClassInfos, i_pos );
4241 else if( id == KaxTracks::ClassInfos.GlobalId )
4243 msg_Dbg( &sys.demuxer, "| - tracks at %"PRId64, i_pos );
4244 LoadSeekHeadItem( KaxTracks::ClassInfos, i_pos );
4246 else if( id == KaxAttachments::ClassInfos.GlobalId )
4248 msg_Dbg( &sys.demuxer, "| - attachments at %"PRId64, i_pos );
4249 LoadSeekHeadItem( KaxAttachments::ClassInfos, i_pos );
4252 msg_Dbg( &sys.demuxer, "| - unknown seekhead reference at %"PRId64, i_pos );
4256 msg_Dbg( &sys.demuxer, "| | + ParseSeekHead Unknown (%s)", typeid(*l).name() );
4261 /*****************************************************************************
4263 *****************************************************************************/
4264 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
4267 bool bSupported = true;
4271 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
4273 tk = new mkv_track_t();
4275 /* Init the track */
4276 memset( tk, 0, sizeof( mkv_track_t ) );
4278 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
4279 tk->fmt.psz_language = strdup("English");
4280 tk->fmt.psz_description = NULL;
4282 tk->b_default = true;
4283 tk->b_enabled = true;
4284 tk->b_silent = false;
4285 tk->i_number = tracks.size() - 1;
4286 tk->i_extra_data = 0;
4287 tk->p_extra_data = NULL;
4288 tk->psz_codec = NULL;
4289 tk->i_default_duration = 0;
4290 tk->f_timecodescale = 1.0;
4292 tk->b_inited = false;
4293 tk->i_data_init = 0;
4294 tk->p_data_init = NULL;
4296 tk->psz_codec_name = NULL;
4297 tk->psz_codec_settings = NULL;
4298 tk->psz_codec_info_url = NULL;
4299 tk->psz_codec_download_url = NULL;
4301 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
4302 tk->p_compression_data = NULL;
4304 for( i = 0; i < m->ListSize(); i++ )
4306 EbmlElement *l = (*m)[i];
4308 if( MKV_IS_ID( l, KaxTrackNumber ) )
4310 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
4312 tk->i_number = uint32( tnum );
4313 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
4315 else if( MKV_IS_ID( l, KaxTrackUID ) )
4317 KaxTrackUID &tuid = *(KaxTrackUID*)l;
4319 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
4321 else if( MKV_IS_ID( l, KaxTrackType ) )
4323 const char *psz_type;
4324 KaxTrackType &ttype = *(KaxTrackType*)l;
4326 switch( uint8(ttype) )
4330 tk->fmt.i_cat = AUDIO_ES;
4334 tk->fmt.i_cat = VIDEO_ES;
4336 case track_subtitle:
4337 psz_type = "subtitle";
4338 tk->fmt.i_cat = SPU_ES;
4341 psz_type = "buttons";
4342 tk->fmt.i_cat = SPU_ES;
4345 psz_type = "unknown";
4346 tk->fmt.i_cat = UNKNOWN_ES;
4350 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
4352 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
4354 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
4356 // tk->b_enabled = uint32( fenb );
4357 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
4358 // uint32( fenb ) );
4360 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
4362 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
4364 tk->b_default = uint32( fdef );
4365 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
4367 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
4369 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
4371 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
4373 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
4375 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
4377 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
4379 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
4381 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
4383 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
4385 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
4387 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
4389 tk->i_default_duration = uint64(defd);
4390 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%"PRId64, uint64(defd) );
4392 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
4394 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
4396 tk->f_timecodescale = float( ttcs );
4397 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
4399 else if( MKV_IS_ID( l, KaxTrackName ) )
4401 KaxTrackName &tname = *(KaxTrackName*)l;
4403 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
4404 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
4406 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
4408 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
4410 if ( tk->fmt.psz_language != NULL )
4411 free( tk->fmt.psz_language );
4412 tk->fmt.psz_language = strdup( string( lang ).c_str() );
4413 msg_Dbg( &sys.demuxer,
4414 "| | | + Track Language=`%s'", tk->fmt.psz_language );
4416 else if( MKV_IS_ID( l, KaxCodecID ) )
4418 KaxCodecID &codecid = *(KaxCodecID*)l;
4420 tk->psz_codec = strdup( string( codecid ).c_str() );
4421 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
4423 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
4425 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
4427 tk->i_extra_data = cpriv.GetSize();
4428 if( tk->i_extra_data > 0 )
4430 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
4431 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
4433 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
4435 else if( MKV_IS_ID( l, KaxCodecName ) )
4437 KaxCodecName &cname = *(KaxCodecName*)l;
4439 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
4440 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
4442 else if( MKV_IS_ID( l, KaxContentEncodings ) )
4444 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
4445 MkvTree( sys.demuxer, 3, "Content Encodings" );
4446 if ( cencs->ListSize() > 1 )
4448 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
4451 for( j = 0; j < cencs->ListSize(); j++ )
4453 EbmlElement *l2 = (*cencs)[j];
4454 if( MKV_IS_ID( l2, KaxContentEncoding ) )
4456 MkvTree( sys.demuxer, 4, "Content Encoding" );
4457 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
4458 for( k = 0; k < cenc->ListSize(); k++ )
4460 EbmlElement *l3 = (*cenc)[k];
4461 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
4463 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
4464 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
4466 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
4468 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
4469 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
4471 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
4473 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
4474 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
4476 else if( MKV_IS_ID( l3, KaxContentCompression ) )
4478 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
4479 MkvTree( sys.demuxer, 5, "Content Compression" );
4480 for( n = 0; n < compr->ListSize(); n++ )
4482 EbmlElement *l4 = (*compr)[n];
4483 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
4485 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
4486 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
4487 tk->i_compression_type = uint32( compalg );
4488 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
4489 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
4491 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
4495 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
4497 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
4501 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
4507 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
4513 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
4517 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
4519 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
4521 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
4522 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
4524 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
4526 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
4528 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
4529 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
4531 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
4533 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
4535 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
4536 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
4538 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
4540 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
4542 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
4544 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
4546 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
4548 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
4550 else if( MKV_IS_ID( l, KaxTrackVideo ) )
4552 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
4554 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
4555 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
4557 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
4560 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
4561 tk->fmt.video.i_frame_rate = 1000000;
4563 for( j = 0; j < tkv->ListSize(); j++ )
4565 EbmlElement *l = (*tkv)[j];
4566 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
4568 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
4570 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
4572 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
4574 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
4576 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
4579 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
4581 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
4583 tk->fmt.video.i_width += uint16( vwidth );
4584 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
4586 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
4588 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
4590 tk->fmt.video.i_height += uint16( vheight );
4591 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
4593 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
4595 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
4597 i_display_width = uint16( vwidth );
4598 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
4600 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
4602 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
4604 i_display_height = uint16( vheight );
4605 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
4607 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
4609 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
4611 i_crop_bottom = uint16( cropval );
4612 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
4614 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
4616 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
4618 i_crop_top = uint16( cropval );
4619 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
4621 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
4623 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
4625 i_crop_right = uint16( cropval );
4626 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
4628 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
4630 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
4632 i_crop_left = uint16( cropval );
4633 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
4635 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
4637 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
4639 tk->f_fps = float( vfps );
4640 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
4642 else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
4644 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
4646 i_display_unit = uint8( vdmode );
4647 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
4648 uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
4650 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
4652 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
4654 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
4656 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
4658 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
4660 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
4664 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
4667 if( i_display_height && i_display_width )
4668 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * i_display_width / i_display_height;
4669 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
4671 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
4672 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
4673 tk->fmt.video.i_x_offset = i_crop_left;
4674 tk->fmt.video.i_y_offset = i_crop_top;
4675 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
4676 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
4678 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
4679 we do not support this atm */
4681 else if( MKV_IS_ID( l, KaxTrackAudio ) )
4683 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
4686 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
4688 for( j = 0; j < tka->ListSize(); j++ )
4690 EbmlElement *l = (*tka)[j];
4692 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
4694 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
4696 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
4697 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
4699 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
4701 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
4703 tk->fmt.audio.i_rate = (int)float( afreq );
4704 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
4706 else if( MKV_IS_ID( l, KaxAudioChannels ) )
4708 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
4710 tk->fmt.audio.i_channels = uint8( achan );
4711 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
4713 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
4715 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
4717 tk->fmt.audio.i_bitspersample = uint8( abits );
4718 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
4722 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
4728 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
4729 typeid(*l).name() );
4735 tracks.push_back( tk );
4739 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
4744 /*****************************************************************************
4746 *****************************************************************************/
4747 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
4751 int i_upper_level = 0;
4753 /* Master elements */
4754 tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
4756 for( i = 0; i < tracks->ListSize(); i++ )
4758 EbmlElement *l = (*tracks)[i];
4760 if( MKV_IS_ID( l, KaxTrackEntry ) )
4762 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
4766 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4771 /*****************************************************************************
4773 *****************************************************************************/
4774 void matroska_segment_c::ParseInfo( KaxInfo *info )
4779 int i_upper_level = 0;
4781 /* Master elements */
4782 m = static_cast<EbmlMaster *>(info);
4783 m->Read( es, info->Generic().Context, i_upper_level, el, true );
4785 for( i = 0; i < m->ListSize(); i++ )
4787 EbmlElement *l = (*m)[i];
4789 if( MKV_IS_ID( l, KaxSegmentUID ) )
4791 if ( p_segment_uid == NULL )
4792 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
4794 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
4796 else if( MKV_IS_ID( l, KaxPrevUID ) )
4798 if ( p_prev_segment_uid == NULL )
4799 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
4801 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
4803 else if( MKV_IS_ID( l, KaxNextUID ) )
4805 if ( p_next_segment_uid == NULL )
4806 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
4808 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
4810 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
4812 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
4814 i_timescale = uint64(tcs);
4816 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%"PRId64,
4819 else if( MKV_IS_ID( l, KaxDuration ) )
4821 KaxDuration &dur = *(KaxDuration*)l;
4823 i_duration = mtime_t( double( dur ) );
4825 msg_Dbg( &sys.demuxer, "| | + Duration=%"PRId64,
4828 else if( MKV_IS_ID( l, KaxMuxingApp ) )
4830 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
4832 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
4834 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
4835 psz_muxing_application );
4837 else if( MKV_IS_ID( l, KaxWritingApp ) )
4839 KaxWritingApp &wapp = *(KaxWritingApp*)l;
4841 psz_writing_application = ToUTF8( UTFstring( wapp ) );
4843 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
4844 psz_writing_application );
4846 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
4848 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
4850 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
4852 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
4853 psz_segment_filename );
4855 else if( MKV_IS_ID( l, KaxTitle ) )
4857 KaxTitle &title = *(KaxTitle*)l;
4859 psz_title = ToUTF8( UTFstring( title ) );
4861 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
4863 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
4865 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
4867 families.push_back( new KaxSegmentFamily(*uid) );
4869 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
4871 #if defined( HAVE_GMTIME_R )
4872 else if( MKV_IS_ID( l, KaxDateUTC ) )
4874 KaxDateUTC &date = *(KaxDateUTC*)l;
4879 i_date = date.GetEpochDate();
4880 memset( buffer, 0, 256 );
4881 if( gmtime_r( &i_date, &tmres ) &&
4882 asctime_r( &tmres, buffer ) )
4884 buffer[strlen( buffer)-1]= '\0';
4885 psz_date_utc = strdup( buffer );
4886 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
4890 #if LIBMATROSKA_VERSION >= 0x000704
4891 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
4893 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
4894 chapter_translation_c *p_translate = new chapter_translation_c();
4896 p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
4897 for( j = 0; j < p_trans->ListSize(); j++ )
4899 EbmlElement *l = (*p_trans)[j];
4901 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
4903 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
4905 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
4907 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
4909 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
4911 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
4915 translations.push_back( p_translate );
4920 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4924 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
4925 i_duration = mtime_t(f_dur);
4929 /*****************************************************************************
4931 *****************************************************************************/
4932 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
4936 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
4937 for( i = 0; i < ca->ListSize(); i++ )
4939 EbmlElement *l = (*ca)[i];
4941 if( MKV_IS_ID( l, KaxChapterUID ) )
4943 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
4944 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
4946 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
4948 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
4949 chapters.b_display_seekpoint = uint8( flag ) == 0;
4951 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
4953 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
4955 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
4956 chapters.i_start_time = uint64( start ) / INT64_C(1000);
4958 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
4960 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
4962 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
4963 chapters.i_end_time = uint64( end ) / INT64_C(1000);
4965 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
4967 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
4969 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
4971 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
4972 for( j = 0; j < cd->ListSize(); j++ )
4974 EbmlElement *l= (*cd)[j];
4976 if( MKV_IS_ID( l, KaxChapterString ) )
4980 KaxChapterString &name =*(KaxChapterString*)l;
4981 for (k = 0; k < i_level; k++)
4982 chapters.psz_name += '+';
4983 chapters.psz_name += ' ';
4984 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
4985 chapters.psz_name += psz_tmp_utf8;
4986 chapters.b_user_display = true;
4988 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
4989 free( psz_tmp_utf8 );
4991 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
4993 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
4994 const char *psz = string( lang ).c_str();
4996 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
4998 else if( MKV_IS_ID( l, KaxChapterCountry ) )
5000 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
5001 const char *psz = string( ct ).c_str();
5003 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
5007 else if( MKV_IS_ID( l, KaxChapterProcess ) )
5009 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
5011 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
5012 chapter_codec_cmds_c *p_ccodec = NULL;
5014 for( j = 0; j < cp->ListSize(); j++ )
5016 EbmlElement *k= (*cp)[j];
5018 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
5020 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
5021 if ( uint32(*p_codec_id) == 0 )
5022 p_ccodec = new matroska_script_codec_c( sys );
5023 else if ( uint32(*p_codec_id) == 1 )
5024 p_ccodec = new dvd_chapter_codec_c( sys );
5029 if ( p_ccodec != NULL )
5031 for( j = 0; j < cp->ListSize(); j++ )
5033 EbmlElement *k= (*cp)[j];
5035 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
5037 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
5038 p_ccodec->SetPrivate( *p_private );
5040 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
5042 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
5045 chapters.codecs.push_back( p_ccodec );
5048 else if( MKV_IS_ID( l, KaxChapterAtom ) )
5050 chapter_item_c *new_sub_chapter = new chapter_item_c();
5051 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
5052 new_sub_chapter->psz_parent = &chapters;
5053 chapters.sub_chapters.push_back( new_sub_chapter );
5058 /*****************************************************************************
5060 *****************************************************************************/
5061 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
5064 int i_upper_level = 0;
5066 attachments->Read( es, attachments->Generic().Context, i_upper_level, el, true );
5068 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
5070 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
5072 std::string psz_mime_type = GetChild<KaxMimeType>( *attachedFile );
5073 KaxFileName &file_name = GetChild<KaxFileName>( *attachedFile );
5074 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
5076 attachment_c *new_attachment = new attachment_c();
5078 if( new_attachment )
5080 char* tmp = ToUTF8( UTFstring( file_name ) );
5081 new_attachment->psz_file_name = tmp;
5083 new_attachment->psz_mime_type = psz_mime_type;
5084 new_attachment->i_size = img_data.GetSize();
5085 new_attachment->p_data = malloc( img_data.GetSize() );
5087 if( new_attachment->p_data )
5089 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
5090 sys.stored_attachments.push_back( new_attachment );
5094 delete new_attachment;
5098 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
5102 /*****************************************************************************
5104 *****************************************************************************/
5105 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
5109 int i_upper_level = 0;
5112 /* Master elements */
5113 chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
5115 for( i = 0; i < chapters->ListSize(); i++ )
5117 EbmlElement *l = (*chapters)[i];
5119 if( MKV_IS_ID( l, KaxEditionEntry ) )
5121 chapter_edition_c *p_edition = new chapter_edition_c();
5123 EbmlMaster *E = static_cast<EbmlMaster *>(l );
5125 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
5126 for( j = 0; j < E->ListSize(); j++ )
5128 EbmlElement *l = (*E)[j];
5130 if( MKV_IS_ID( l, KaxChapterAtom ) )
5132 chapter_item_c *new_sub_chapter = new chapter_item_c();
5133 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
5134 p_edition->sub_chapters.push_back( new_sub_chapter );
5136 else if( MKV_IS_ID( l, KaxEditionUID ) )
5138 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
5140 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
5142 p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
5144 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
5146 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
5147 i_default_edition = stored_editions.size();
5151 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
5154 stored_editions.push_back( p_edition );
5158 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
5162 for( i = 0; i < stored_editions.size(); i++ )
5164 stored_editions[i]->RefreshChapters( );
5167 if ( stored_editions.size() != 0 && stored_editions[i_default_edition]->b_ordered )
5169 /* update the duration of the segment according to the sum of all sub chapters */
5170 i_dur = stored_editions[i_default_edition]->Duration() / INT64_C(1000);
5176 void matroska_segment_c::ParseCluster( )
5181 int i_upper_level = 0;
5183 /* Master elements */
5184 m = static_cast<EbmlMaster *>( cluster );
5185 m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
5187 for( i = 0; i < m->ListSize(); i++ )
5189 EbmlElement *l = (*m)[i];
5191 if( MKV_IS_ID( l, KaxClusterTimecode ) )
5193 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
5195 cluster->InitTimecode( uint64( ctc ), i_timescale );
5200 i_start_time = cluster->GlobalTimecode() / 1000;
5203 /*****************************************************************************
5204 * InformationCreate:
5205 *****************************************************************************/
5206 void matroska_segment_c::InformationCreate( )
5208 sys.meta = vlc_meta_New();
5212 vlc_meta_SetTitle( sys.meta, psz_title );
5216 vlc_meta_SetDate( sys.meta, psz_date_utc );
5219 if( psz_segment_filename )
5221 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5223 if( psz_muxing_application )
5225 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5227 if( psz_writing_application )
5229 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5232 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
5234 // mkv_track_t *tk = tracks[i_track];
5235 // vlc_meta_t *mtk = vlc_meta_New();
5236 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
5240 if( i_tags_position >= 0 )
5244 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
5254 /*****************************************************************************
5256 *****************************************************************************/
5258 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
5260 #define idx p_indexes[i_index]
5262 idx.i_block_number= -1;
5263 idx.i_position = cluster->GetElementPosition();
5268 if( i_index >= i_index_max )
5270 i_index_max += 1024;
5271 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
5276 void chapter_edition_c::RefreshChapters( )
5278 chapter_item_c::RefreshChapters( b_ordered, -1 );
5279 b_display_seekpoint = false;
5282 int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_time )
5284 int64_t i_user_time = i_prev_user_time;
5286 // first the sub-chapters, and then ourself
5287 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
5288 while ( index != sub_chapters.end() )
5290 i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
5296 // the ordered chapters always start at zero
5297 if ( i_prev_user_time == -1 )
5299 if ( i_user_time == -1 )
5301 i_prev_user_time = 0;
5304 i_user_start_time = i_prev_user_time;
5305 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
5307 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
5311 i_user_end_time = i_user_time;
5316 if ( sub_chapters.begin() != sub_chapters.end() )
5317 std::sort( sub_chapters.begin(), sub_chapters.end(), chapter_item_c::CompareTimecode );
5318 i_user_start_time = i_start_time;
5319 if ( i_end_time != -1 )
5320 i_user_end_time = i_end_time;
5321 else if ( i_user_time != -1 )
5322 i_user_end_time = i_user_time;
5324 i_user_end_time = i_user_start_time;
5327 return i_user_end_time;
5330 mtime_t chapter_edition_c::Duration() const
5332 mtime_t i_result = 0;
5334 if ( sub_chapters.size() )
5336 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
5338 i_result = (*index)->i_user_end_time;
5344 chapter_item_c * chapter_edition_c::FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current )
5348 bool b_found_current = false;
5349 return chapter_item_c::FindTimecode( i_timecode, p_current, b_found_current );
5352 chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode, const chapter_item_c * p_current, bool & b_found )
5354 chapter_item_c *psz_result = NULL;
5356 if ( p_current == this )
5359 if ( i_user_timecode >= i_user_start_time &&
5360 ( i_user_timecode < i_user_end_time ||
5361 ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
5363 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
5364 while ( index != sub_chapters.end() && ((p_current == NULL && psz_result == NULL) || (p_current != NULL && (!b_found || psz_result == NULL))))
5366 psz_result = (*index)->FindTimecode( i_user_timecode, p_current, b_found );
5370 if ( psz_result == NULL )
5377 bool chapter_item_c::ParentOf( const chapter_item_c & item ) const
5379 if ( &item == this )
5382 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.begin();
5383 while ( index != sub_chapters.end() )
5385 if ( (*index)->ParentOf( item ) )
5393 void demux_sys_t::PreloadFamily( const matroska_segment_c & of_segment )
5395 for (size_t i=0; i<opened_segments.size(); i++)
5397 opened_segments[i]->PreloadFamily( of_segment );
5400 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
5405 for (size_t i=0; i<families.size(); i++)
5407 for (size_t j=0; j<of_segment.families.size(); j++)
5409 if ( *(families[i]) == *(of_segment.families[j]) )
5417 // preload all the linked segments for all preloaded segments
5418 void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
5420 size_t i_preloaded, i, j;
5421 virtual_segment_c *p_seg;
5423 p_current_segment = VirtualFromSegments( p_segment );
5425 used_segments.push_back( p_current_segment );
5427 // create all the other virtual segments of the family
5430 for ( i=0; i< opened_segments.size(); i++ )
5432 if ( opened_segments[i]->b_preloaded && !IsUsedSegment( *opened_segments[i] ) )
5434 p_seg = VirtualFromSegments( opened_segments[i] );
5435 used_segments.push_back( p_seg );
5439 } while ( i_preloaded ); // worst case: will stop when all segments are found as family related
5441 // publish all editions of all usable segment
5442 for ( i=0; i< used_segments.size(); i++ )
5444 p_seg = used_segments[i];
5445 if ( p_seg->p_editions != NULL )
5447 std::string sz_name;
5448 input_title_t *p_title = vlc_input_title_New();
5449 p_seg->i_sys_title = i;
5452 // TODO use a name for each edition, let the TITLE deal with a codec name
5453 for ( j=0; j<p_seg->p_editions->size(); j++ )
5455 if ( p_title->psz_name == NULL )
5457 sz_name = (*p_seg->p_editions)[j]->GetMainName();
5458 if ( sz_name != "" )
5459 p_title->psz_name = strdup( sz_name.c_str() );
5462 chapter_edition_c *p_edition = (*p_seg->p_editions)[j];
5465 p_edition->PublishChapters( *p_title, i_chapters, 0 );
5468 // create a name if there is none
5469 if ( p_title->psz_name == NULL )
5471 sz_name = N_("Segment");
5473 sprintf( psz_str, " %d", (int)i );
5475 p_title->psz_name = strdup( sz_name.c_str() );
5478 titles.push_back( p_title );
5482 // TODO decide which segment should be first used (VMG for DVD)
5485 bool demux_sys_t::IsUsedSegment( matroska_segment_c &segment ) const
5487 for ( size_t i=0; i< used_segments.size(); i++ )
5489 if ( used_segments[i]->FindUID( *segment.p_segment_uid ) )
5495 virtual_segment_c *demux_sys_t::VirtualFromSegments( matroska_segment_c *p_segment ) const
5497 size_t i_preloaded, i;
5499 virtual_segment_c *p_result = new virtual_segment_c( p_segment );
5501 // fill our current virtual segment with all hard linked segments
5504 for ( i=0; i< opened_segments.size(); i++ )
5506 i_preloaded += p_result->AddSegment( opened_segments[i] );
5508 } while ( i_preloaded ); // worst case: will stop when all segments are found as linked
5512 p_result->PreloadLinked( );
5514 p_result->PrepareChapters( );
5519 bool demux_sys_t::PreparePlayback( virtual_segment_c *p_new_segment )
5521 if ( p_new_segment != NULL && p_new_segment != p_current_segment )
5523 if ( p_current_segment != NULL && p_current_segment->Segment() != NULL )
5524 p_current_segment->Segment()->UnSelect();
5526 p_current_segment = p_new_segment;
5527 i_current_title = p_new_segment->i_sys_title;
5529 if( !p_current_segment->Segment()->b_cues )
5530 msg_Warn( &p_current_segment->Segment()->sys.demuxer, "no cues/empty cues found->seek won't be precise" );
5532 f_duration = p_current_segment->Duration();
5534 /* add information */
5535 p_current_segment->Segment()->InformationCreate( );
5536 p_current_segment->Segment()->Select( 0 );
5541 void demux_sys_t::JumpTo( virtual_segment_c & vsegment, chapter_item_c * p_chapter )
5543 // if the segment is not part of the current segment, select the new one
5544 if ( &vsegment != p_current_segment )
5546 PreparePlayback( &vsegment );
5549 if ( p_chapter != NULL )
5551 if ( !p_chapter->Enter( true ) )
5553 // jump to the location in the found segment
5554 vsegment.Seek( demuxer, p_chapter->i_user_start_time, -1, p_chapter, -1 );
5560 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
5564 if ( p_item_a == NULL || p_item_b == NULL )
5567 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
5568 if ( p_item_b->p_prev_segment_uid != NULL
5569 && *p_tmp == *p_item_b->p_prev_segment_uid )
5572 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
5576 if ( p_item_b->p_segment_uid != NULL
5577 && *p_tmp == *p_item_b->p_segment_uid )
5580 if ( p_item_b->p_prev_segment_uid != NULL
5581 && *p_tmp == *p_item_b->p_prev_segment_uid )
5587 bool matroska_segment_c::Preload( )
5592 EbmlElement *el = NULL;
5594 ep->Reset( &sys.demuxer );
5596 while( ( el = ep->Get() ) != NULL )
5598 if( MKV_IS_ID( el, KaxSeekHead ) )
5600 /* Multiple allowed */
5601 /* We bail at 10, to prevent possible recursion */
5602 msg_Dbg( &sys.demuxer, "| + Seek head" );
5603 if( i_seekhead_count < 10 )
5605 i_seekhead_position = (int64_t) es.I_O().getFilePointer();
5606 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
5609 else if( MKV_IS_ID( el, KaxInfo ) )
5611 /* Multiple allowed, mandatory */
5612 msg_Dbg( &sys.demuxer, "| + Information" );
5613 if( i_info_position < 0 ) // FIXME
5614 ParseInfo( static_cast<KaxInfo*>( el ) );
5615 i_info_position = (int64_t) es.I_O().getFilePointer();
5617 else if( MKV_IS_ID( el, KaxTracks ) )
5619 /* Multiple allowed */
5620 msg_Dbg( &sys.demuxer, "| + Tracks" );
5621 if( i_tracks_position < 0 ) // FIXME
5622 ParseTracks( static_cast<KaxTracks*>( el ) );
5623 if ( tracks.size() == 0 )
5625 msg_Err( &sys.demuxer, "No tracks supported" );
5628 i_tracks_position = (int64_t) es.I_O().getFilePointer();
5630 else if( MKV_IS_ID( el, KaxCues ) )
5632 msg_Dbg( &sys.demuxer, "| + Cues" );
5633 if( i_cues_position < 0 )
5634 LoadCues( static_cast<KaxCues*>( el ) );
5635 i_cues_position = (int64_t) es.I_O().getFilePointer();
5637 else if( MKV_IS_ID( el, KaxCluster ) )
5639 msg_Dbg( &sys.demuxer, "| + Cluster" );
5641 cluster = (KaxCluster*)el;
5643 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
5647 /* stop pre-parsing the stream */
5650 else if( MKV_IS_ID( el, KaxAttachments ) )
5652 msg_Dbg( &sys.demuxer, "| + Attachments" );
5653 if( i_attachments_position < 0 )
5654 ParseAttachments( static_cast<KaxAttachments*>( el ) );
5655 i_attachments_position = (int64_t) es.I_O().getFilePointer();
5657 else if( MKV_IS_ID( el, KaxChapters ) )
5659 msg_Dbg( &sys.demuxer, "| + Chapters" );
5660 if( i_chapters_position < 0 )
5661 ParseChapters( static_cast<KaxChapters*>( el ) );
5662 i_chapters_position = (int64_t) es.I_O().getFilePointer();
5664 else if( MKV_IS_ID( el, KaxTag ) )
5666 msg_Dbg( &sys.demuxer, "| + Tags" );
5667 if( i_tags_position < 0) // FIXME
5668 ;//LoadTags( static_cast<KaxTags*>( el ) );
5669 i_tags_position = (int64_t) es.I_O().getFilePointer();
5672 msg_Dbg( &sys.demuxer, "| + Preload Unknown (%s)", typeid(*el).name() );
5680 /* Here we try to load elements that were found in Seek Heads, but not yet parsed */
5681 bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position )
5683 int64_t i_sav_position = (int64_t)es.I_O().getFilePointer();
5686 es.I_O().setFilePointer( i_element_position, seek_beginning );
5687 el = es.FindNextID( ClassInfos, 0xFFFFFFFFL);
5691 msg_Err( &sys.demuxer, "cannot load some cues/chapters/tags etc. (broken seekhead or file)" );
5692 es.I_O().setFilePointer( i_sav_position, seek_beginning );
5696 if( MKV_IS_ID( el, KaxSeekHead ) )
5698 /* Multiple allowed */
5699 msg_Dbg( &sys.demuxer, "| + Seek head" );
5700 if( i_seekhead_count < 10 )
5702 i_seekhead_position = i_element_position;
5703 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
5706 else if( MKV_IS_ID( el, KaxInfo ) ) // FIXME
5708 /* Multiple allowed, mandatory */
5709 msg_Dbg( &sys.demuxer, "| + Information" );
5710 if( i_info_position < 0 )
5711 ParseInfo( static_cast<KaxInfo*>( el ) );
5712 i_info_position = i_element_position;
5714 else if( MKV_IS_ID( el, KaxTracks ) ) // FIXME
5716 /* Multiple allowed */
5717 msg_Dbg( &sys.demuxer, "| + Tracks" );
5718 if( i_tracks_position < 0 )
5719 ParseTracks( static_cast<KaxTracks*>( el ) );
5720 if ( tracks.size() == 0 )
5722 msg_Err( &sys.demuxer, "No tracks supported" );
5724 es.I_O().setFilePointer( i_sav_position, seek_beginning );
5727 i_tracks_position = i_element_position;
5729 else if( MKV_IS_ID( el, KaxCues ) )
5731 msg_Dbg( &sys.demuxer, "| + Cues" );
5732 if( i_cues_position < 0 )
5733 LoadCues( static_cast<KaxCues*>( el ) );
5734 i_cues_position = i_element_position;
5736 else if( MKV_IS_ID( el, KaxAttachments ) )
5738 msg_Dbg( &sys.demuxer, "| + Attachments" );
5739 if( i_attachments_position < 0 )
5740 ParseAttachments( static_cast<KaxAttachments*>( el ) );
5741 i_attachments_position = i_element_position;
5743 else if( MKV_IS_ID( el, KaxChapters ) )
5745 msg_Dbg( &sys.demuxer, "| + Chapters" );
5746 if( i_chapters_position < 0 )
5747 ParseChapters( static_cast<KaxChapters*>( el ) );
5748 i_chapters_position = i_element_position;
5750 else if( MKV_IS_ID( el, KaxTag ) ) // FIXME
5752 msg_Dbg( &sys.demuxer, "| + Tags" );
5753 if( i_tags_position < 0 )
5754 ;//LoadTags( static_cast<KaxTags*>( el ) );
5755 i_tags_position = i_element_position;
5759 msg_Dbg( &sys.demuxer, "| + LoadSeekHeadItem Unknown (%s)", typeid(*el).name() );
5763 es.I_O().setFilePointer( i_sav_position, seek_beginning );
5767 matroska_segment_c *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
5769 for (size_t i=0; i<opened_segments.size(); i++)
5771 if ( *opened_segments[i]->p_segment_uid == uid )
5772 return opened_segments[i];
5777 chapter_item_c *demux_sys_t::BrowseCodecPrivate( unsigned int codec_id,
5778 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
5779 const void *p_cookie,
5780 size_t i_cookie_size,
5781 virtual_segment_c * &p_segment_found )
5783 chapter_item_c *p_result = NULL;
5784 for (size_t i=0; i<used_segments.size(); i++)
5786 p_result = used_segments[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
5787 if ( p_result != NULL )
5789 p_segment_found = used_segments[i];
5796 chapter_item_c *demux_sys_t::FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found )
5798 chapter_item_c *p_result = NULL;
5799 for (size_t i=0; i<used_segments.size(); i++)
5801 p_result = used_segments[i]->FindChapter( i_find_uid );
5802 if ( p_result != NULL )
5804 p_segment_found = used_segments[i];
5811 void virtual_segment_c::Sort()
5813 // keep the current segment index
5814 matroska_segment_c *p_segment = linked_segments[i_current_segment];
5816 std::sort( linked_segments.begin(), linked_segments.end(), matroska_segment_c::CompareSegmentUIDs );
5818 for ( i_current_segment=0; i_current_segment<linked_segments.size(); i_current_segment++)
5819 if ( linked_segments[i_current_segment] == p_segment )
5823 size_t virtual_segment_c::AddSegment( matroska_segment_c *p_segment )
5826 // check if it's not already in here
5827 for ( i=0; i<linked_segments.size(); i++ )
5829 if ( linked_segments[i]->p_segment_uid != NULL
5830 && p_segment->p_segment_uid != NULL
5831 && *p_segment->p_segment_uid == *linked_segments[i]->p_segment_uid )
5835 // find possible mates
5836 for ( i=0; i<linked_uids.size(); i++ )
5838 if ( (p_segment->p_segment_uid != NULL && *p_segment->p_segment_uid == linked_uids[i])
5839 || (p_segment->p_prev_segment_uid != NULL && *p_segment->p_prev_segment_uid == linked_uids[i])
5840 || (p_segment->p_next_segment_uid !=NULL && *p_segment->p_next_segment_uid == linked_uids[i]) )
5842 linked_segments.push_back( p_segment );
5844 AppendUID( p_segment->p_prev_segment_uid );
5845 AppendUID( p_segment->p_next_segment_uid );
5853 void virtual_segment_c::PreloadLinked( )
5855 for ( size_t i=0; i<linked_segments.size(); i++ )
5857 linked_segments[i]->Preload( );
5859 i_current_edition = linked_segments[0]->i_default_edition;
5862 mtime_t virtual_segment_c::Duration() const
5865 if ( linked_segments.size() == 0 )
5868 matroska_segment_c *p_last_segment = linked_segments[linked_segments.size()-1];
5869 // p_last_segment->ParseCluster( );
5871 i_duration = p_last_segment->i_start_time / 1000 + p_last_segment->i_duration;
5876 void virtual_segment_c::AppendUID( const EbmlBinary * p_UID )
5878 if ( p_UID == NULL )
5880 if ( p_UID->GetBuffer() == NULL )
5883 for (size_t i=0; i<linked_uids.size(); i++)
5885 if ( *p_UID == linked_uids[i] )
5888 linked_uids.push_back( *(KaxSegmentUID*)(p_UID) );
5891 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_global_position )
5894 #if LIBMATROSKA_VERSION >= 0x000800
5895 KaxSimpleBlock *simpleblock;
5897 int i_track_skipping;
5898 int64_t i_block_duration;
5899 int64_t i_block_ref1;
5900 int64_t i_block_ref2;
5902 int64_t i_seek_position = i_start_pos;
5903 int64_t i_seek_time = i_start_time;
5905 if( i_global_position >= 0 )
5907 /* Special case for seeking in files with no cues */
5908 EbmlElement *el = NULL;
5909 es.I_O().setFilePointer( i_start_pos, seek_beginning );
5911 ep = new EbmlParser( &es, segment, &sys.demuxer );
5914 while( ( el = ep->Get() ) != NULL )
5916 if( MKV_IS_ID( el, KaxCluster ) )
5918 cluster = (KaxCluster *)el;
5919 i_cluster_pos = cluster->GetElementPosition();
5921 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
5923 IndexAppendCluster( cluster );
5925 if( es.I_O().getFilePointer() >= i_global_position )
5928 msg_Dbg( &sys.demuxer, "we found a cluster that is in the neighbourhood" );
5929 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
5934 msg_Err( &sys.demuxer, "This file has no cues, and we were unable to seek to the requested position by parsing." );
5942 for( ; i_idx < i_index; i_idx++ )
5944 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
5955 i_seek_position = p_indexes[i_idx].i_position;
5956 i_seek_time = p_indexes[i_idx].i_time;
5959 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
5960 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
5962 es.I_O().setFilePointer( i_seek_position, seek_beginning );
5965 ep = new EbmlParser( &es, segment, &sys.demuxer );
5968 sys.i_start_pts = i_date;
5970 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
5972 /* now parse until key frame */
5973 i_track_skipping = 0;
5974 for( i_track = 0; i_track < tracks.size(); i_track++ )
5976 if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
5978 tracks[i_track]->b_search_keyframe = true;
5981 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, i_date );
5984 while( i_track_skipping > 0 )
5986 #if LIBMATROSKA_VERSION >= 0x000800
5987 if( BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
5989 if( BlockGet( block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
5992 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
5998 for( i_track = 0; i_track < tracks.size(); i_track++ )
6000 #if LIBMATROSKA_VERSION >= 0x000800
6001 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
6002 (block && tracks[i_track]->i_number == block->TrackNum()) )
6004 if( tracks[i_track]->i_number == block->TrackNum() )
6011 #if LIBMATROSKA_VERSION >= 0x000800
6013 sys.i_pts = (sys.i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
6016 sys.i_pts = (sys.i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
6018 if( i_track < tracks.size() )
6020 if( sys.i_pts >= sys.i_start_pts )
6022 cluster = static_cast<KaxCluster*>(ep->UnGet( i_block_pos, i_cluster_pos ));
6023 i_track_skipping = 0;
6025 else if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
6027 if( i_block_ref1 == 0 && tracks[i_track]->b_search_keyframe )
6029 tracks[i_track]->b_search_keyframe = false;
6032 if( !tracks[i_track]->b_search_keyframe )
6035 //es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, sys.i_pts );
6036 #if LIBMATROSKA_VERSION >= 0x000800
6037 BlockDecode( &sys.demuxer, block, simpleblock, sys.i_pts, 0, i_block_ref1 >= 0 || i_block_ref2 > 0 );
6039 BlockDecode( &sys.demuxer, block, sys.i_pts, 0, i_block_ref1 >= 0 || i_block_ref2 > 0 );
6048 /* FIXME current ES_OUT_SET_NEXT_DISPLAY_TIME does not work that well if
6049 * the delay is too high. */
6050 if( sys.i_pts + 500*1000 < sys.i_start_pts )
6052 sys.i_start_pts = sys.i_pts;
6054 for( i_track = 0; i_track < tracks.size(); i_track++ )
6055 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, sys.i_start_pts );
6059 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
6060 const KaxBlock *p_block
6061 #if LIBMATROSKA_VERSION >= 0x000800
6062 , const KaxSimpleBlock *p_simpleblock
6070 for( i_track = 0; i_track < tracks.size(); i_track++ )
6072 const mkv_track_t *tk = tracks[i_track];
6074 #if LIBMATROSKA_VERSION >= 0x000800
6075 if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
6076 ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
6078 if( tk->i_number == p_block->TrackNum() )
6085 if( i_track >= tracks.size() )
6086 return VLC_EGENERIC;
6089 *pi_track = i_track;
6093 void virtual_segment_c::Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter, int64_t i_global_position )
6095 demux_sys_t *p_sys = demuxer.p_sys;
6098 // find the actual time for an ordered edition
6099 if ( psz_chapter == NULL )
6101 if ( Edition() && Edition()->b_ordered )
6103 /* 1st, we need to know in which chapter we are */
6104 psz_chapter = (*p_editions)[i_current_edition]->FindTimecode( i_date, psz_current_chapter );
6108 if ( psz_chapter != NULL )
6110 psz_current_chapter = psz_chapter;
6111 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
6112 if ( psz_chapter->i_seekpoint_num > 0 )
6114 demuxer.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
6115 demuxer.info.i_title = p_sys->i_current_title = i_sys_title;
6116 demuxer.info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
6120 // find the best matching segment
6121 for ( i=0; i<linked_segments.size(); i++ )
6123 if ( i_date < linked_segments[i]->i_start_time )
6130 if ( i_current_segment != i )
6132 linked_segments[i_current_segment]->UnSelect();
6133 linked_segments[i]->Select( i_date );
6134 i_current_segment = i;
6137 linked_segments[i]->Seek( i_date, i_time_offset, i_global_position );
6140 void chapter_codec_cmds_c::AddCommand( const KaxChapterProcessCommand & command )
6144 uint32 codec_time = uint32(-1);
6145 for( i = 0; i < command.ListSize(); i++ )
6147 const EbmlElement *k = command[i];
6149 if( MKV_IS_ID( k, KaxChapterProcessTime ) )
6151 codec_time = uint32( *static_cast<const KaxChapterProcessTime*>( k ) );
6156 for( i = 0; i < command.ListSize(); i++ )
6158 const EbmlElement *k = command[i];
6160 if( MKV_IS_ID( k, KaxChapterProcessData ) )
6162 KaxChapterProcessData *p_data = new KaxChapterProcessData( *static_cast<const KaxChapterProcessData*>( k ) );
6163 switch ( codec_time )
6166 during_cmds.push_back( p_data );
6169 enter_cmds.push_back( p_data );
6172 leave_cmds.push_back( p_data );
6181 bool chapter_item_c::Enter( bool b_do_subs )
6183 bool f_result = false;
6184 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
6185 while ( index != codecs.end() )
6187 f_result |= (*index)->Enter();
6194 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
6195 while ( index_ != sub_chapters.end() )
6197 f_result |= (*index_)->Enter( true );
6204 bool chapter_item_c::Leave( bool b_do_subs )
6206 bool f_result = false;
6207 b_is_leaving = true;
6208 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
6209 while ( index != codecs.end() )
6211 f_result |= (*index)->Leave();
6218 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
6219 while ( index_ != sub_chapters.end() )
6221 f_result |= (*index_)->Leave( true );
6225 b_is_leaving = false;
6229 bool chapter_item_c::EnterAndLeave( chapter_item_c *p_item, bool b_final_enter )
6231 chapter_item_c *p_common_parent = p_item;
6233 // leave, up to a common parent
6234 while ( p_common_parent != NULL && !p_common_parent->ParentOf( *this ) )
6236 if ( !p_common_parent->b_is_leaving && p_common_parent->Leave( false ) )
6238 p_common_parent = p_common_parent->psz_parent;
6241 // enter from the parent to <this>
6242 if ( p_common_parent != NULL )
6246 if ( p_common_parent == this )
6247 return Enter( true );
6249 for ( size_t i = 0; i<p_common_parent->sub_chapters.size(); i++ )
6251 if ( p_common_parent->sub_chapters[i]->ParentOf( *this ) )
6253 p_common_parent = p_common_parent->sub_chapters[i];
6254 if ( p_common_parent != this )
6255 if ( p_common_parent->Enter( false ) )
6264 if ( b_final_enter )
6265 return Enter( true );
6270 bool dvd_chapter_codec_c::Enter()
6272 bool f_result = false;
6273 std::vector<KaxChapterProcessData*>::iterator index = enter_cmds.begin();
6274 while ( index != enter_cmds.end() )
6276 if ( (*index)->GetSize() )
6278 binary *p_data = (*index)->GetBuffer();
6279 size_t i_size = *p_data++;
6280 // avoid reading too much from the buffer
6281 i_size = __MIN( i_size, ((*index)->GetSize() - 1) >> 3 );
6282 for ( ; i_size > 0; i_size--, p_data += 8 )
6284 msg_Dbg( &sys.demuxer, "Matroska DVD enter command" );
6285 f_result |= sys.dvd_interpretor.Interpret( p_data );
6293 bool dvd_chapter_codec_c::Leave()
6295 bool f_result = false;
6296 std::vector<KaxChapterProcessData*>::iterator index = leave_cmds.begin();
6297 while ( index != leave_cmds.end() )
6299 if ( (*index)->GetSize() )
6301 binary *p_data = (*index)->GetBuffer();
6302 size_t i_size = *p_data++;
6303 // avoid reading too much from the buffer
6304 i_size = __MIN( i_size, ((*index)->GetSize() - 1) >> 3 );
6305 for ( ; i_size > 0; i_size--, p_data += 8 )
6307 msg_Dbg( &sys.demuxer, "Matroska DVD leave command" );
6308 f_result |= sys.dvd_interpretor.Interpret( p_data );
6316 // see http://www.dvd-replica.com/DVD/vmcmdset.php for a description of DVD commands
6317 bool dvd_command_interpretor_c::Interpret( const binary * p_command, size_t i_size )
6322 virtual_segment_c *p_segment = NULL;
6323 chapter_item_c *p_chapter = NULL;
6324 bool f_result = false;
6325 uint16 i_command = ( p_command[0] << 8 ) + p_command[1];
6327 // handle register tests if there are some
6328 if ( (i_command & 0xF0) != 0 )
6330 bool b_test_positive = true;//(i_command & CMD_DVD_IF_NOT) == 0;
6331 bool b_test_value = (i_command & CMD_DVD_TEST_VALUE) != 0;
6332 uint8 i_test = i_command & 0x70;
6335 // see http://dvd.sourceforge.net/dvdinfo/vmi.html
6338 switch ( i_command >> 12 )
6341 i_cr1 = p_command[3];
6342 i_cr2 = (p_command[4] << 8) + p_command[5];
6347 i_cr1 = p_command[6];
6348 i_cr2 = p_command[7];
6349 b_test_value = false;
6353 if ( ((p_command[1] >> 4) & 0x7) == 0)
6355 i_cr1 = p_command[2];
6356 i_cr2 = (p_command[6] << 8) + p_command[7];
6360 i_cr1 = p_command[2];
6361 i_cr2 = (p_command[6] << 8) + p_command[7];
6369 i_value = GetPRM( i_cr2 );
6373 case CMD_DVD_IF_GPREG_EQUAL:
6375 msg_Dbg( &sys.demuxer, "IF %s EQUALS %s", GetRegTypeName( false, i_cr1 ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6376 if (!( GetPRM( i_cr1 ) == i_value ))
6378 b_test_positive = false;
6381 case CMD_DVD_IF_GPREG_NOT_EQUAL:
6383 msg_Dbg( &sys.demuxer, "IF %s NOT EQUALS %s", GetRegTypeName( false, i_cr1 ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6384 if (!( GetPRM( i_cr1 ) != i_value ))
6386 b_test_positive = false;
6389 case CMD_DVD_IF_GPREG_INF:
6391 msg_Dbg( &sys.demuxer, "IF %s < %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6392 if (!( GetPRM( i_cr1 ) < i_value ))
6394 b_test_positive = false;
6397 case CMD_DVD_IF_GPREG_INF_EQUAL:
6398 // if inferior or equal
6399 msg_Dbg( &sys.demuxer, "IF %s < %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6400 if (!( GetPRM( i_cr1 ) <= i_value ))
6402 b_test_positive = false;
6405 case CMD_DVD_IF_GPREG_AND:
6407 msg_Dbg( &sys.demuxer, "IF %s & %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6408 if (!( GetPRM( i_cr1 ) & i_value ))
6410 b_test_positive = false;
6413 case CMD_DVD_IF_GPREG_SUP:
6415 msg_Dbg( &sys.demuxer, "IF %s >= %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6416 if (!( GetPRM( i_cr1 ) > i_value ))
6418 b_test_positive = false;
6421 case CMD_DVD_IF_GPREG_SUP_EQUAL:
6422 // if superior or equal
6423 msg_Dbg( &sys.demuxer, "IF %s >= %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6424 if (!( GetPRM( i_cr1 ) >= i_value ))
6426 b_test_positive = false;
6431 if ( !b_test_positive )
6435 // strip the test command
6436 i_command &= 0xFF0F;
6438 switch ( i_command )
6443 msg_Dbg( &sys.demuxer, "NOP" );
6448 msg_Dbg( &sys.demuxer, "Break" );
6452 case CMD_DVD_JUMP_TT:
6454 uint8 i_title = p_command[5];
6455 msg_Dbg( &sys.demuxer, "JumpTT %d", i_title );
6457 // find in the ChapProcessPrivate matching this Title level
6458 p_chapter = sys.BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title), p_segment );
6459 if ( p_segment != NULL )
6461 sys.JumpTo( *p_segment, p_chapter );
6467 case CMD_DVD_CALLSS_VTSM1:
6469 msg_Dbg( &sys.demuxer, "CallSS" );
6471 switch( (p_command[6] & 0xC0) >> 6 ) {
6473 p_type = p_command[5] & 0x0F;
6477 msg_Dbg( &sys.demuxer, "CallSS PGC (rsm_cell %x)", p_command[4]);
6480 msg_Dbg( &sys.demuxer, "CallSS Title Entry (rsm_cell %x)", p_command[4]);
6483 msg_Dbg( &sys.demuxer, "CallSS Root Menu (rsm_cell %x)", p_command[4]);
6486 msg_Dbg( &sys.demuxer, "CallSS Subpicture Menu (rsm_cell %x)", p_command[4]);
6489 msg_Dbg( &sys.demuxer, "CallSS Audio Menu (rsm_cell %x)", p_command[4]);
6492 msg_Dbg( &sys.demuxer, "CallSS Angle Menu (rsm_cell %x)", p_command[4]);
6495 msg_Dbg( &sys.demuxer, "CallSS Chapter Menu (rsm_cell %x)", p_command[4]);
6498 msg_Dbg( &sys.demuxer, "CallSS <unknown> (rsm_cell %x)", p_command[4]);
6501 p_chapter = sys.BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1, p_segment );
6502 if ( p_segment != NULL )
6504 sys.JumpTo( *p_segment, p_chapter );
6509 msg_Dbg( &sys.demuxer, "CallSS VMGM (menu %d, rsm_cell %x)", p_command[5] & 0x0F, p_command[4]);
6512 msg_Dbg( &sys.demuxer, "CallSS VTSM (menu %d, rsm_cell %x)", p_command[5] & 0x0F, p_command[4]);
6515 msg_Dbg( &sys.demuxer, "CallSS VMGM (pgc %d, rsm_cell %x)", (p_command[2] << 8) + p_command[3], p_command[4]);
6520 case CMD_DVD_JUMP_SS:
6522 msg_Dbg( &sys.demuxer, "JumpSS");
6524 switch( (p_command[5] & 0xC0) >> 6 ) {
6526 msg_Dbg( &sys.demuxer, "JumpSS FP");
6529 p_type = p_command[5] & 0x0F;
6533 msg_Dbg( &sys.demuxer, "JumpSS VMGM Title Entry");
6536 msg_Dbg( &sys.demuxer, "JumpSS VMGM Root Menu");
6539 msg_Dbg( &sys.demuxer, "JumpSS VMGM Subpicture Menu");
6542 msg_Dbg( &sys.demuxer, "JumpSS VMGM Audio Menu");
6545 msg_Dbg( &sys.demuxer, "JumpSS VMGM Angle Menu");
6548 msg_Dbg( &sys.demuxer, "JumpSS VMGM Chapter Menu");
6551 msg_Dbg( &sys.demuxer, "JumpSS <unknown>");
6555 p_chapter = sys.BrowseCodecPrivate( 1, MatchIsVMG, NULL, 0, p_segment );
6556 if ( p_segment != NULL )
6558 p_chapter = p_segment->BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1 );
6559 if ( p_chapter != NULL )
6561 sys.JumpTo( *p_segment, p_chapter );
6567 p_type = p_command[5] & 0x0F;
6571 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Title Entry", p_command[4], p_command[3]);
6574 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Root Menu", p_command[4], p_command[3]);
6577 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Subpicture Menu", p_command[4], p_command[3]);
6580 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Audio Menu", p_command[4], p_command[3]);
6583 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Angle Menu", p_command[4], p_command[3]);
6586 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Chapter Menu", p_command[4], p_command[3]);
6589 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) <unknown>", p_command[4], p_command[3]);
6593 p_chapter = sys.BrowseCodecPrivate( 1, MatchVTSMNumber, &p_command[4], 1, p_segment );
6595 if ( p_segment != NULL && p_chapter != NULL )
6597 // find the title in the VTS
6598 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchTitleNumber, &p_command[3], 1 );
6599 if ( p_chapter != NULL )
6601 // find the specified menu in the VTSM
6602 p_chapter = p_segment->BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1 );
6603 if ( p_chapter != NULL )
6605 sys.JumpTo( *p_segment, p_chapter );
6610 msg_Dbg( &sys.demuxer, "Title (%d) does not exist in this VTS", p_command[3] );
6613 msg_Dbg( &sys.demuxer, "DVD Domain VTS (%d) not found", p_command[4] );
6616 msg_Dbg( &sys.demuxer, "JumpSS VMGM (pgc %d)", (p_command[2] << 8) + p_command[3]);
6621 case CMD_DVD_JUMPVTS_PTT:
6623 uint8 i_title = p_command[5];
6624 uint8 i_ptt = p_command[3];
6626 msg_Dbg( &sys.demuxer, "JumpVTS Title (%d) PTT (%d)", i_title, i_ptt);
6628 // find the current VTS content segment
6629 p_chapter = sys.p_current_segment->BrowseCodecPrivate( 1, MatchIsDomain, NULL, 0 );
6630 if ( p_chapter != NULL )
6632 int16 i_curr_title = p_chapter->GetTitleNumber( );
6633 if ( i_curr_title > 0 )
6635 p_chapter = sys.BrowseCodecPrivate( 1, MatchVTSNumber, &i_curr_title, sizeof(i_curr_title), p_segment );
6637 if ( p_segment != NULL && p_chapter != NULL )
6639 // find the title in the VTS
6640 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title) );
6641 if ( p_chapter != NULL )
6643 // find the chapter in the title
6644 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchChapterNumber, &i_ptt, sizeof(i_ptt) );
6645 if ( p_chapter != NULL )
6647 sys.JumpTo( *p_segment, p_chapter );
6652 msg_Dbg( &sys.demuxer, "Title (%d) does not exist in this VTS", i_title );
6655 msg_Dbg( &sys.demuxer, "DVD Domain VTS (%d) not found", i_curr_title );
6658 msg_Dbg( &sys.demuxer, "JumpVTS_PTT command found but not in a VTS(M)");
6661 msg_Dbg( &sys.demuxer, "JumpVTS_PTT command but the DVD domain wasn't found");
6664 case CMD_DVD_SET_GPRMMD:
6666 msg_Dbg( &sys.demuxer, "Set GPRMMD [%d]=%d", (p_command[4] << 8) + p_command[5], (p_command[2] << 8) + p_command[3]);
6668 if ( !SetGPRM( (p_command[4] << 8) + p_command[5], (p_command[2] << 8) + p_command[3] ) )
6669 msg_Dbg( &sys.demuxer, "Set GPRMMD failed" );
6672 case CMD_DVD_LINKPGCN:
6674 uint16 i_pgcn = (p_command[6] << 8) + p_command[7];
6676 msg_Dbg( &sys.demuxer, "Link PGCN(%d)", i_pgcn );
6677 p_chapter = sys.p_current_segment->BrowseCodecPrivate( 1, MatchPgcNumber, &i_pgcn, 2 );
6678 if ( p_chapter != NULL )
6680 if ( !p_chapter->Enter( true ) )
6681 // jump to the location in the found segment
6682 sys.p_current_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter, -1 );
6688 case CMD_DVD_LINKCN:
6690 uint8 i_cn = p_command[7];
6692 p_chapter = sys.p_current_segment->CurrentChapter();
6694 msg_Dbg( &sys.demuxer, "LinkCN (cell %d)", i_cn );
6695 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchCellNumber, &i_cn, 1 );
6696 if ( p_chapter != NULL )
6698 if ( !p_chapter->Enter( true ) )
6699 // jump to the location in the found segment
6700 sys.p_current_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter, -1 );
6706 case CMD_DVD_GOTO_LINE:
6708 msg_Dbg( &sys.demuxer, "GotoLine (%d)", (p_command[6] << 8) + p_command[7] );
6712 case CMD_DVD_SET_HL_BTNN1:
6714 msg_Dbg( &sys.demuxer, "SetHL_BTN (%d)", p_command[4] );
6715 SetSPRM( 0x88, p_command[4] );
6720 msg_Dbg( &sys.demuxer, "unsupported command : %02X %02X %02X %02X %02X %02X %02X %02X"
6736 bool dvd_command_interpretor_c::MatchIsDomain( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6738 return ( data.p_private_data != NULL && data.p_private_data->GetBuffer()[0] == MATROSKA_DVD_LEVEL_SS );
6741 bool dvd_command_interpretor_c::MatchIsVMG( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6743 if ( data.p_private_data == NULL || data.p_private_data->GetSize() < 2 )
6746 return ( data.p_private_data->GetBuffer()[0] == MATROSKA_DVD_LEVEL_SS && data.p_private_data->GetBuffer()[1] == 0xC0);
6749 bool dvd_command_interpretor_c::MatchVTSNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6751 if ( i_cookie_size != 2 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6754 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_SS || data.p_private_data->GetBuffer()[1] != 0x80 )
6757 uint16 i_gtitle = (data.p_private_data->GetBuffer()[2] << 8 ) + data.p_private_data->GetBuffer()[3];
6758 uint16 i_title = *(uint16*)p_cookie;
6760 return (i_gtitle == i_title);
6763 bool dvd_command_interpretor_c::MatchVTSMNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6765 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6768 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_SS || data.p_private_data->GetBuffer()[1] != 0x40 )
6771 uint8 i_gtitle = data.p_private_data->GetBuffer()[3];
6772 uint8 i_title = *(uint8*)p_cookie;
6774 return (i_gtitle == i_title);
6777 bool dvd_command_interpretor_c::MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6779 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6782 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_TT )
6785 uint16 i_gtitle = (data.p_private_data->GetBuffer()[1] << 8 ) + data.p_private_data->GetBuffer()[2];
6786 uint8 i_title = *(uint8*)p_cookie;
6788 return (i_gtitle == i_title);
6791 bool dvd_command_interpretor_c::MatchPgcType( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6793 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 8 )
6796 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PGC )
6799 uint8 i_pgc_type = data.p_private_data->GetBuffer()[3] & 0x0F;
6800 uint8 i_pgc = *(uint8*)p_cookie;
6802 return (i_pgc_type == i_pgc);
6805 bool dvd_command_interpretor_c::MatchPgcNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6807 if ( i_cookie_size != 2 || data.p_private_data == NULL || data.p_private_data->GetSize() < 8 )
6810 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PGC )
6813 uint16 *i_pgc_n = (uint16 *)p_cookie;
6814 uint16 i_pgc_num = (data.p_private_data->GetBuffer()[1] << 8) + data.p_private_data->GetBuffer()[2];
6816 return (i_pgc_num == *i_pgc_n);
6819 bool dvd_command_interpretor_c::MatchChapterNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6821 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 2 )
6824 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PTT )
6827 uint8 i_chapter = data.p_private_data->GetBuffer()[1];
6828 uint8 i_ptt = *(uint8*)p_cookie;
6830 return (i_chapter == i_ptt);
6833 bool dvd_command_interpretor_c::MatchCellNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6835 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 5 )
6838 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_CN )
6841 uint8 *i_cell_n = (uint8 *)p_cookie;
6842 uint8 i_cell_num = data.p_private_data->GetBuffer()[3];
6844 return (i_cell_num == *i_cell_n);
6847 bool matroska_script_codec_c::Enter()
6849 bool f_result = false;
6850 std::vector<KaxChapterProcessData*>::iterator index = enter_cmds.begin();
6851 while ( index != enter_cmds.end() )
6853 if ( (*index)->GetSize() )
6855 msg_Dbg( &sys.demuxer, "Matroska Script enter command" );
6856 f_result |= interpretor.Interpret( (*index)->GetBuffer(), (*index)->GetSize() );
6863 bool matroska_script_codec_c::Leave()
6865 bool f_result = false;
6866 std::vector<KaxChapterProcessData*>::iterator index = leave_cmds.begin();
6867 while ( index != leave_cmds.end() )
6869 if ( (*index)->GetSize() )
6871 msg_Dbg( &sys.demuxer, "Matroska Script leave command" );
6872 f_result |= interpretor.Interpret( (*index)->GetBuffer(), (*index)->GetSize() );
6879 // see http://www.matroska.org/technical/specs/chapters/index.html#mscript
6880 // for a description of existing commands
6881 bool matroska_script_interpretor_c::Interpret( const binary * p_command, size_t i_size )
6883 bool b_result = false;
6885 char *psz_str = (char*) malloc( i_size + 1 );
6886 memcpy( psz_str, p_command, i_size );
6887 psz_str[ i_size ] = '\0';
6889 std::string sz_command = psz_str;
6892 msg_Dbg( &sys.demuxer, "command : %s", sz_command.c_str() );
6894 #if defined(__GNUC__) && (__GNUC__ < 3)
6895 if ( sz_command.compare( CMD_MS_GOTO_AND_PLAY, 0, CMD_MS_GOTO_AND_PLAY.size() ) == 0 )
6897 if ( sz_command.compare( 0, CMD_MS_GOTO_AND_PLAY.size(), CMD_MS_GOTO_AND_PLAY ) == 0 )
6903 for ( i=CMD_MS_GOTO_AND_PLAY.size(); i<sz_command.size(); i++)
6905 if ( sz_command[i] == '(' )
6912 for ( j=i; j<sz_command.size(); j++)
6914 if ( sz_command[j] == ')' )
6921 std::string st = sz_command.substr( i+1, j-i-1 );
6922 int64_t i_chapter_uid = atoi( st.c_str() );
6924 virtual_segment_c *p_segment;
6925 chapter_item_c *p_chapter = sys.FindChapter( i_chapter_uid, p_segment );
6927 if ( p_chapter == NULL )
6928 msg_Dbg( &sys.demuxer, "Chapter %"PRId64" not found", i_chapter_uid);
6931 if ( !p_chapter->EnterAndLeave( sys.p_current_segment->CurrentChapter() ) )
6932 p_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter, -1 );
6940 void demux_sys_t::SwapButtons()
6942 #ifndef WORDS_BIGENDIAN
6943 uint8_t button, i, j;
6945 for( button = 1; button <= pci_packet.hli.hl_gi.btn_ns; button++) {
6946 btni_t *button_ptr = &(pci_packet.hli.btnit[button-1]);
6947 binary *p_data = (binary*) button_ptr;
6949 uint16 i_x_start = ((p_data[0] & 0x3F) << 4 ) + ( p_data[1] >> 4 );
6950 uint16 i_x_end = ((p_data[1] & 0x03) << 8 ) + p_data[2];
6951 uint16 i_y_start = ((p_data[3] & 0x3F) << 4 ) + ( p_data[4] >> 4 );
6952 uint16 i_y_end = ((p_data[4] & 0x03) << 8 ) + p_data[5];
6953 button_ptr->x_start = i_x_start;
6954 button_ptr->x_end = i_x_end;
6955 button_ptr->y_start = i_y_start;
6956 button_ptr->y_end = i_y_end;
6959 for ( i = 0; i<3; i++ )
6961 for ( j = 0; j<2; j++ )
6963 pci_packet.hli.btn_colit.btn_coli[i][j] = U32_AT( &pci_packet.hli.btn_colit.btn_coli[i][j] );