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 #include "vlc_keys.h"
104 #include "mp4/libmp4.h"
110 /*****************************************************************************
112 *****************************************************************************/
113 static int Open ( vlc_object_t * );
114 static void Close( vlc_object_t * );
117 set_shortname( "Matroska" );
118 set_description( N_("Matroska stream demuxer" ) );
119 set_capability( "demux", 50 );
120 set_callbacks( Open, Close );
121 set_category( CAT_INPUT );
122 set_subcategory( SUBCAT_INPUT_DEMUX );
124 add_bool( "mkv-use-ordered-chapters", 1, NULL,
125 N_("Ordered chapters"),
126 N_("Play ordered chapters as specified in the segment."), true );
128 add_bool( "mkv-use-chapter-codec", 1, NULL,
129 N_("Chapter codecs"),
130 N_("Use chapter codecs found in the segment."), true );
132 add_bool( "mkv-preload-local-dir", 1, NULL,
133 N_("Preload Directory"),
134 N_("Preload matroska files from the same family in the same directory (not good for broken files)."), true );
136 add_bool( "mkv-seek-percent", 0, NULL,
137 N_("Seek based on percent not time"),
138 N_("Seek based on percent not time."), true );
140 add_bool( "mkv-use-dummy", 0, NULL,
141 N_("Dummy Elements"),
142 N_("Read and discard unknown EBML elements (not good for broken files)."), true );
144 add_shortcut( "mka" );
145 add_shortcut( "mkv" );
150 #define MATROSKA_COMPRESSION_NONE -1
151 #define MATROSKA_COMPRESSION_ZLIB 0
152 #define MATROSKA_COMPRESSION_BLIB 1
153 #define MATROSKA_COMPRESSION_LZOX 2
154 #define MATROSKA_COMPRESSION_HEADER 3
156 #define MKVD_TIMECODESCALE 1000000
158 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
159 #undef ATTRIBUTE_PACKED
160 #undef PRAGMA_PACK_BEGIN
161 #undef PRAGMA_PACK_END
163 #if defined(__GNUC__)
164 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
165 #define ATTRIBUTE_PACKED __attribute__ ((packed))
166 #define PRAGMA_PACK 0
170 #if !defined(ATTRIBUTE_PACKED)
171 #define ATTRIBUTE_PACKED
172 #define PRAGMA_PACK 1
179 /*************************************
180 * taken from libdvdnav / libdvdread
181 **************************************/
184 * DVD Time Information.
190 uint8_t frame_u; /* The two high bits are the frame rate. */
191 } ATTRIBUTE_PACKED dvd_time_t;
197 #ifdef WORDS_BIGENDIAN
198 unsigned char zero : 7; /* 25-31 */
199 unsigned char video_pres_mode_change : 1; /* 24 */
201 unsigned char karaoke_audio_pres_mode_change : 1; /* 23 */
202 unsigned char angle_change : 1;
203 unsigned char subpic_stream_change : 1;
204 unsigned char audio_stream_change : 1;
205 unsigned char pause_on : 1;
206 unsigned char still_off : 1;
207 unsigned char button_select_or_activate : 1;
208 unsigned char resume : 1; /* 16 */
210 unsigned char chapter_menu_call : 1; /* 15 */
211 unsigned char angle_menu_call : 1;
212 unsigned char audio_menu_call : 1;
213 unsigned char subpic_menu_call : 1;
214 unsigned char root_menu_call : 1;
215 unsigned char title_menu_call : 1;
216 unsigned char backward_scan : 1;
217 unsigned char forward_scan : 1; /* 8 */
219 unsigned char next_pg_search : 1; /* 7 */
220 unsigned char prev_or_top_pg_search : 1;
221 unsigned char time_or_chapter_search : 1;
222 unsigned char go_up : 1;
223 unsigned char stop : 1;
224 unsigned char title_play : 1;
225 unsigned char chapter_search_or_play : 1;
226 unsigned char title_or_time_play : 1; /* 0 */
228 unsigned char video_pres_mode_change : 1; /* 24 */
229 unsigned char zero : 7; /* 25-31 */
231 unsigned char resume : 1; /* 16 */
232 unsigned char button_select_or_activate : 1;
233 unsigned char still_off : 1;
234 unsigned char pause_on : 1;
235 unsigned char audio_stream_change : 1;
236 unsigned char subpic_stream_change : 1;
237 unsigned char angle_change : 1;
238 unsigned char karaoke_audio_pres_mode_change : 1; /* 23 */
240 unsigned char forward_scan : 1; /* 8 */
241 unsigned char backward_scan : 1;
242 unsigned char title_menu_call : 1;
243 unsigned char root_menu_call : 1;
244 unsigned char subpic_menu_call : 1;
245 unsigned char audio_menu_call : 1;
246 unsigned char angle_menu_call : 1;
247 unsigned char chapter_menu_call : 1; /* 15 */
249 unsigned char title_or_time_play : 1; /* 0 */
250 unsigned char chapter_search_or_play : 1;
251 unsigned char title_play : 1;
252 unsigned char stop : 1;
253 unsigned char go_up : 1;
254 unsigned char time_or_chapter_search : 1;
255 unsigned char prev_or_top_pg_search : 1;
256 unsigned char next_pg_search : 1; /* 7 */
258 } ATTRIBUTE_PACKED user_ops_t;
261 * Type to store per-command data.
265 } ATTRIBUTE_PACKED vm_cmd_t;
266 #define COMMAND_DATA_SIZE 8
269 * PCI General Information
272 uint32_t nv_pck_lbn; /**< sector address of this nav pack */
273 uint16_t vobu_cat; /**< 'category' of vobu */
274 uint16_t zero1; /**< reserved */
275 user_ops_t vobu_uop_ctl; /**< UOP of vobu */
276 uint32_t vobu_s_ptm; /**< start presentation time of vobu */
277 uint32_t vobu_e_ptm; /**< end presentation time of vobu */
278 uint32_t vobu_se_e_ptm; /**< end ptm of sequence end in vobu */
279 dvd_time_t e_eltm; /**< Cell elapsed time */
281 } ATTRIBUTE_PACKED pci_gi_t;
284 * Non Seamless Angle Information
287 uint32_t nsml_agl_dsta[9]; /**< address of destination vobu in AGL_C#n */
288 } ATTRIBUTE_PACKED nsml_agli_t;
291 * Highlight General Information
293 * For btngrX_dsp_ty the bits have the following meaning:
294 * 000b: normal 4/3 only buttons
295 * XX1b: wide (16/9) buttons
296 * X1Xb: letterbox buttons
297 * 1XXb: pan&scan buttons
300 uint16_t hli_ss; /**< status, only low 2 bits 0: no buttons, 1: different 2: equal 3: eual except for button cmds */
301 uint32_t hli_s_ptm; /**< start ptm of hli */
302 uint32_t hli_e_ptm; /**< end ptm of hli */
303 uint32_t btn_se_e_ptm; /**< end ptm of button select */
304 #ifdef WORDS_BIGENDIAN
305 unsigned char zero1 : 2; /**< reserved */
306 unsigned char btngr_ns : 2; /**< number of button groups 1, 2 or 3 with 36/18/12 buttons */
307 unsigned char zero2 : 1; /**< reserved */
308 unsigned char btngr1_dsp_ty : 3; /**< display type of subpic stream for button group 1 */
309 unsigned char zero3 : 1; /**< reserved */
310 unsigned char btngr2_dsp_ty : 3; /**< display type of subpic stream for button group 2 */
311 unsigned char zero4 : 1; /**< reserved */
312 unsigned char btngr3_dsp_ty : 3; /**< display type of subpic stream for button group 3 */
314 unsigned char btngr1_dsp_ty : 3;
315 unsigned char zero2 : 1;
316 unsigned char btngr_ns : 2;
317 unsigned char zero1 : 2;
318 unsigned char btngr3_dsp_ty : 3;
319 unsigned char zero4 : 1;
320 unsigned char btngr2_dsp_ty : 3;
321 unsigned char zero3 : 1;
323 uint8_t btn_ofn; /**< button offset number range 0-255 */
324 uint8_t btn_ns; /**< number of valid buttons <= 36/18/12 (low 6 bits) */
325 uint8_t nsl_btn_ns; /**< number of buttons selectable by U_BTNNi (low 6 bits) nsl_btn_ns <= btn_ns */
326 uint8_t zero5; /**< reserved */
327 uint8_t fosl_btnn; /**< forcedly selected button (low 6 bits) */
328 uint8_t foac_btnn; /**< forcedly activated button (low 6 bits) */
329 } ATTRIBUTE_PACKED hl_gi_t;
333 * Button Color Information Table
334 * Each entry beeing a 32bit word that contains the color indexs and alpha
335 * values to use. They are all represented by 4 bit number and stored
336 * like this [Ci3, Ci2, Ci1, Ci0, A3, A2, A1, A0]. The actual palette
337 * that the indexes reference is in the PGC.
338 * \todo split the uint32_t into a struct
341 uint32_t btn_coli[3][2]; /**< [button color number-1][select:0/action:1] */
342 } ATTRIBUTE_PACKED btn_colit_t;
347 * NOTE: I've had to change the structure from the disk layout to get
348 * the packing to work with Sun's Forte C compiler.
349 * The 4 and 7 bytes are 'rotated' was: ABC DEF GHIJ is: ABCG DEFH IJ
352 #ifdef WORDS_BIGENDIAN
353 uint32 btn_coln : 2; /**< button color number */
354 uint32 x_start : 10; /**< x start offset within the overlay */
355 uint32 zero1 : 2; /**< reserved */
356 uint32 x_end : 10; /**< x end offset within the overlay */
358 uint32 zero3 : 2; /**< reserved */
359 uint32 up : 6; /**< button index when pressing up */
361 uint32 auto_action_mode : 2; /**< 0: no, 1: activated if selected */
362 uint32 y_start : 10; /**< y start offset within the overlay */
363 uint32 zero2 : 2; /**< reserved */
364 uint32 y_end : 10; /**< y end offset within the overlay */
366 uint32 zero4 : 2; /**< reserved */
367 uint32 down : 6; /**< button index when pressing down */
368 unsigned char zero5 : 2; /**< reserved */
369 unsigned char left : 6; /**< button index when pressing left */
370 unsigned char zero6 : 2; /**< reserved */
371 unsigned char right : 6; /**< button index when pressing right */
384 uint32 auto_action_mode : 2;
388 unsigned char left : 6;
389 unsigned char zero5 : 2;
390 unsigned char right : 6;
391 unsigned char zero6 : 2;
394 } ATTRIBUTE_PACKED btni_t;
397 * Highlight Information
401 btn_colit_t btn_colit;
403 } ATTRIBUTE_PACKED hli_t;
410 nsml_agli_t nsml_agli;
413 } ATTRIBUTE_PACKED pci_t;
419 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
423 * What's between a directory and a filename?
426 #define DIRECTORY_SEPARATOR '\\'
428 #define DIRECTORY_SEPARATOR '/'
431 using namespace LIBMATROSKA_NAMESPACE;
434 /*****************************************************************************
436 *****************************************************************************/
438 block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) {
439 int result, dstsize, n;
444 d_stream.zalloc = (alloc_func)0;
445 d_stream.zfree = (free_func)0;
446 d_stream.opaque = (voidpf)0;
447 result = inflateInit(&d_stream);
450 msg_Dbg( p_this, "inflateInit() failed. Result: %d", result );
454 d_stream.next_in = (Bytef *)p_in_block->p_buffer;
455 d_stream.avail_in = p_in_block->i_buffer;
457 p_block = block_New( p_this, 0 );
462 p_block = block_Realloc( p_block, 0, n * 1000 );
463 dst = (unsigned char *)p_block->p_buffer;
464 d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
465 d_stream.avail_out = 1000;
466 result = inflate(&d_stream, Z_NO_FLUSH);
467 if( ( result != Z_OK ) && ( result != Z_STREAM_END ) )
469 msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result );
473 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
474 ( result != Z_STREAM_END ) );
476 dstsize = d_stream.total_out;
477 inflateEnd( &d_stream );
479 p_block = block_Realloc( p_block, 0, dstsize );
480 p_block->i_buffer = dstsize;
481 block_Release( p_in_block );
488 * Helper function to print the mkv parse tree
490 static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
495 msg_Err( &demuxer, "too deep tree" );
498 va_start( args, psz_format );
499 static const char psz_foo[] = "| | | | | | | | | |";
500 char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) );
501 strncpy( psz_foo2, psz_foo, 4 * i_level );
502 psz_foo2[ 4 * i_level ] = '+';
503 psz_foo2[ 4 * i_level + 1 ] = ' ';
504 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
505 __msg_GenericVa( VLC_OBJECT(&demuxer),VLC_MSG_DBG, "mkv", psz_foo2, args );
510 /*****************************************************************************
512 *****************************************************************************/
513 class vlc_stream_io_callback: public IOCallback
521 vlc_stream_io_callback( stream_t *, bool );
523 virtual ~vlc_stream_io_callback()
529 virtual uint32 read ( void *p_buffer, size_t i_size);
530 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
531 virtual size_t write ( const void *p_buffer, size_t i_size);
532 virtual uint64 getFilePointer ( void );
533 virtual void close ( void );
536 /*****************************************************************************
538 *****************************************************************************/
542 EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux );
543 virtual ~EbmlParser( void );
547 void Reset( demux_t *p_demux );
548 EbmlElement *Get( void );
550 EbmlElement *UnGet( uint64 i_block_pos, uint64 i_cluster_pos );
552 int GetLevel( void );
554 /* Is the provided element presents in our upper elements */
555 bool IsTopPresent( EbmlElement * );
560 EbmlElement *m_el[10];
561 int64_t mi_remain_size[10];
571 /*****************************************************************************
572 * Some functions to manipulate memory
573 *****************************************************************************/
574 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
575 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
577 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
580 /*****************************************************************************
581 * definitions of structures and functions used by this plugins
582 *****************************************************************************/
589 unsigned int i_number;
592 uint8_t *p_extra_data;
596 uint64_t i_default_duration;
597 float f_timecodescale;
606 unsigned int i_original_rate;
609 /* data to be send first */
611 uint8_t *p_data_init;
613 /* hack : it's for seek */
614 bool b_search_keyframe;
618 const char *psz_codec_name;
619 const char *psz_codec_settings;
620 const char *psz_codec_info_url;
621 const char *psz_codec_download_url;
623 /* encryption/compression */
624 int i_compression_type;
625 KaxContentCompSettings *p_compression_data;
642 const binary MATROSKA_DVD_LEVEL_SS = 0x30;
643 const binary MATROSKA_DVD_LEVEL_LU = 0x2A;
644 const binary MATROSKA_DVD_LEVEL_TT = 0x28;
645 const binary MATROSKA_DVD_LEVEL_PGC = 0x20;
646 const binary MATROSKA_DVD_LEVEL_PG = 0x18;
647 const binary MATROSKA_DVD_LEVEL_PTT = 0x10;
648 const binary MATROSKA_DVD_LEVEL_CN = 0x08;
650 class chapter_codec_cmds_c
653 chapter_codec_cmds_c( demux_sys_t & demuxer, int codec_id = -1)
654 :p_private_data(NULL)
655 ,i_codec_id( codec_id )
659 virtual ~chapter_codec_cmds_c()
661 delete p_private_data;
662 std::vector<KaxChapterProcessData*>::iterator indexe = enter_cmds.begin();
663 while ( indexe != enter_cmds.end() )
668 std::vector<KaxChapterProcessData*>::iterator indexl = leave_cmds.begin();
669 while ( indexl != leave_cmds.end() )
674 std::vector<KaxChapterProcessData*>::iterator indexd = during_cmds.begin();
675 while ( indexd != during_cmds.end() )
682 void SetPrivate( const KaxChapterProcessPrivate & private_data )
684 p_private_data = new KaxChapterProcessPrivate( private_data );
687 void AddCommand( const KaxChapterProcessCommand & command );
689 /// \return wether the codec has seeked in the files or not
690 virtual bool Enter() { return false; }
691 virtual bool Leave() { return false; }
692 virtual std::string GetCodecName( bool f_for_title = false ) const { return ""; }
693 virtual int16 GetTitleNumber() { return -1; }
695 KaxChapterProcessPrivate *p_private_data;
698 std::vector<KaxChapterProcessData*> enter_cmds;
699 std::vector<KaxChapterProcessData*> during_cmds;
700 std::vector<KaxChapterProcessData*> leave_cmds;
706 class dvd_command_interpretor_c
709 dvd_command_interpretor_c( demux_sys_t & demuxer )
712 memset( p_PRMs, 0, sizeof(p_PRMs) );
713 p_PRMs[ 0x80 + 1 ] = 15;
714 p_PRMs[ 0x80 + 2 ] = 62;
715 p_PRMs[ 0x80 + 3 ] = 1;
716 p_PRMs[ 0x80 + 4 ] = 1;
717 p_PRMs[ 0x80 + 7 ] = 1;
718 p_PRMs[ 0x80 + 8 ] = 1;
719 p_PRMs[ 0x80 + 16 ] = 0xFFFFu;
720 p_PRMs[ 0x80 + 18 ] = 0xFFFFu;
723 bool Interpret( const binary * p_command, size_t i_size = 8 );
725 uint16 GetPRM( size_t index ) const
728 return p_PRMs[ index ];
732 uint16 GetGPRM( size_t index ) const
734 if ( index >= 0 && index < 16 )
735 return p_PRMs[ index ];
739 uint16 GetSPRM( size_t index ) const
741 // 21,22,23 reserved for future use
742 if ( index >= 0x80 && index < 0x95 )
743 return p_PRMs[ index ];
747 bool SetPRM( size_t index, uint16 value )
749 if ( index >= 0 && index < 16 )
751 p_PRMs[ index ] = value;
757 bool SetGPRM( size_t index, uint16 value )
759 if ( index >= 0 && index < 16 )
761 p_PRMs[ index ] = value;
767 bool SetSPRM( size_t index, uint16 value )
769 if ( index > 0x80 && index <= 0x8D && index != 0x8C )
771 p_PRMs[ index ] = value;
778 std::string GetRegTypeName( bool b_value, uint16 value ) const
781 char s_value[6], s_reg_value[6];
782 sprintf( s_value, "%.5d", value );
790 else if ( value < 0x80 )
792 sprintf( s_reg_value, "%.5d", GetPRM( value ) );
796 result += s_reg_value;
801 sprintf( s_reg_value, "%.5d", GetPRM( value ) );
805 result += s_reg_value;
817 // wether it's a comparison on the value or register
818 static const uint16 CMD_DVD_TEST_VALUE = 0x80;
819 static const uint16 CMD_DVD_IF_GPREG_AND = (1 << 4);
820 static const uint16 CMD_DVD_IF_GPREG_EQUAL = (2 << 4);
821 static const uint16 CMD_DVD_IF_GPREG_NOT_EQUAL = (3 << 4);
822 static const uint16 CMD_DVD_IF_GPREG_SUP_EQUAL = (4 << 4);
823 static const uint16 CMD_DVD_IF_GPREG_SUP = (5 << 4);
824 static const uint16 CMD_DVD_IF_GPREG_INF_EQUAL = (6 << 4);
825 static const uint16 CMD_DVD_IF_GPREG_INF = (7 << 4);
827 static const uint16 CMD_DVD_NOP = 0x0000;
828 static const uint16 CMD_DVD_GOTO_LINE = 0x0001;
829 static const uint16 CMD_DVD_BREAK = 0x0002;
831 static const uint16 CMD_DVD_NOP2 = 0x2001;
832 static const uint16 CMD_DVD_LINKPGCN = 0x2004;
833 static const uint16 CMD_DVD_LINKPGN = 0x2006;
834 static const uint16 CMD_DVD_LINKCN = 0x2007;
835 static const uint16 CMD_DVD_JUMP_TT = 0x3002;
836 static const uint16 CMD_DVD_JUMPVTS_TT = 0x3003;
837 static const uint16 CMD_DVD_JUMPVTS_PTT = 0x3005;
838 static const uint16 CMD_DVD_JUMP_SS = 0x3006;
839 static const uint16 CMD_DVD_CALLSS_VTSM1 = 0x3008;
841 static const uint16 CMD_DVD_SET_HL_BTNN2 = 0x4600;
842 static const uint16 CMD_DVD_SET_HL_BTNN_LINKPGCN1 = 0x4604;
843 static const uint16 CMD_DVD_SET_STREAM = 0x5100;
844 static const uint16 CMD_DVD_SET_GPRMMD = 0x5300;
845 static const uint16 CMD_DVD_SET_HL_BTNN1 = 0x5600;
846 static const uint16 CMD_DVD_SET_HL_BTNN_LINKPGCN2 = 0x5604;
847 static const uint16 CMD_DVD_SET_HL_BTNN_LINKCN = 0x5607;
849 static const uint16 CMD_DVD_MOV_SPREG_PREG = 0x6100;
850 static const uint16 CMD_DVD_GPREG_MOV_VALUE = 0x7100;
851 static const uint16 CMD_DVD_SUB_GPREG = 0x7400;
852 static const uint16 CMD_DVD_MULT_GPREG = 0x7500;
853 static const uint16 CMD_DVD_GPREG_DIV_VALUE = 0x7600;
854 static const uint16 CMD_DVD_GPREG_AND_VALUE = 0x7900;
856 // callbacks when browsing inside CodecPrivate
857 static bool MatchIsDomain ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
858 static bool MatchIsVMG ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
859 static bool MatchVTSNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
860 static bool MatchVTSMNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
861 static bool MatchTitleNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
862 static bool MatchPgcType ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
863 static bool MatchPgcNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
864 static bool MatchChapterNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
865 static bool MatchCellNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
868 class dvd_chapter_codec_c : public chapter_codec_cmds_c
871 dvd_chapter_codec_c( demux_sys_t & sys )
872 :chapter_codec_cmds_c( sys, 1 )
877 std::string GetCodecName( bool f_for_title = false ) const;
878 int16 GetTitleNumber();
881 class matroska_script_interpretor_c
884 matroska_script_interpretor_c( demux_sys_t & demuxer )
888 bool Interpret( const binary * p_command, size_t i_size );
891 static const std::string CMD_MS_GOTO_AND_PLAY;
897 const std::string matroska_script_interpretor_c::CMD_MS_GOTO_AND_PLAY = "GotoAndPlay";
900 class matroska_script_codec_c : public chapter_codec_cmds_c
903 matroska_script_codec_c( demux_sys_t & sys )
904 :chapter_codec_cmds_c( sys, 0 )
912 matroska_script_interpretor_c interpretor;
915 class chapter_translation_c
918 chapter_translation_c()
922 ~chapter_translation_c()
927 KaxChapterTranslateID *p_translated;
928 unsigned int codec_id;
929 std::vector<uint64_t> editions;
938 ,i_user_start_time(-1)
941 ,b_display_seekpoint(true)
942 ,b_user_display(false)
947 virtual ~chapter_item_c()
949 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
950 while ( index != codecs.end() )
955 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
956 while ( index_ != sub_chapters.end() )
963 int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time );
964 int PublishChapters( input_title_t & title, int & i_user_chapters, int i_level = 0 );
965 virtual chapter_item_c * FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current, bool & b_found );
966 void Append( const chapter_item_c & edition );
967 chapter_item_c * FindChapter( int64_t i_find_uid );
968 virtual chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
969 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
970 const void *p_cookie,
971 size_t i_cookie_size );
972 std::string GetCodecName( bool f_for_title = false ) const;
973 bool ParentOf( const chapter_item_c & item ) const;
974 int16 GetTitleNumber( ) const;
976 int64_t i_start_time, i_end_time;
977 int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */
978 std::vector<chapter_item_c*> sub_chapters;
981 bool b_display_seekpoint;
983 std::string psz_name;
984 chapter_item_c *psz_parent;
987 std::vector<chapter_codec_cmds_c*> codecs;
989 static bool CompareTimecode( const chapter_item_c * itemA, const chapter_item_c * itemB )
991 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) );
994 bool Enter( bool b_do_subchapters );
995 bool Leave( bool b_do_subchapters );
996 bool EnterAndLeave( chapter_item_c *p_item, bool b_enter = true );
999 class chapter_edition_c : public chapter_item_c
1006 void RefreshChapters( );
1007 mtime_t Duration() const;
1008 std::string GetMainName() const;
1009 chapter_item_c * FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current );
1014 class matroska_segment_c
1017 matroska_segment_c( demux_sys_t & demuxer, EbmlStream & estream )
1020 ,i_timescale(MKVD_TIMECODESCALE)
1023 ,i_cues_position(-1)
1024 ,i_info_position(-1)
1025 ,i_chapters_position(-1)
1026 ,i_tags_position(-1)
1027 ,i_tracks_position(-1)
1028 ,i_attachments_position(-1)
1029 ,i_seekhead_position(-1)
1030 ,i_seekhead_count(0)
1035 ,p_segment_uid(NULL)
1036 ,p_prev_segment_uid(NULL)
1037 ,p_next_segment_uid(NULL)
1041 ,psz_muxing_application(NULL)
1042 ,psz_writing_application(NULL)
1043 ,psz_segment_filename(NULL)
1046 ,i_default_edition(0)
1051 p_indexes = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
1054 virtual ~matroska_segment_c()
1056 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1058 delete tracks[i_track]->p_compression_data;
1059 es_format_Clean( &tracks[i_track]->fmt );
1060 free( tracks[i_track]->p_extra_data );
1061 free( tracks[i_track]->psz_codec );
1062 delete tracks[i_track];
1065 free( psz_writing_application );
1066 free( psz_muxing_application );
1067 free( psz_segment_filename );
1069 free( psz_date_utc );
1074 delete p_segment_uid;
1075 delete p_prev_segment_uid;
1076 delete p_next_segment_uid;
1078 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
1079 while ( index != stored_editions.end() )
1084 std::vector<chapter_translation_c*>::iterator indext = translations.begin();
1085 while ( indext != translations.end() )
1090 std::vector<KaxSegmentFamily*>::iterator indexf = families.begin();
1091 while ( indexf != families.end() )
1098 KaxSegment *segment;
1102 uint64_t i_timescale;
1104 /* duration of the segment */
1106 mtime_t i_start_time;
1109 std::vector<mkv_track_t*> tracks;
1112 int i_seekhead_count;
1113 int64_t i_seekhead_position;
1114 int64_t i_cues_position;
1115 int64_t i_tracks_position;
1116 int64_t i_info_position;
1117 int64_t i_chapters_position;
1118 int64_t i_tags_position;
1119 int64_t i_attachments_position;
1121 KaxCluster *cluster;
1123 uint64 i_cluster_pos;
1124 int64_t i_start_pos;
1125 KaxSegmentUID *p_segment_uid;
1126 KaxPrevUID *p_prev_segment_uid;
1127 KaxNextUID *p_next_segment_uid;
1132 mkv_index_t *p_indexes;
1135 char *psz_muxing_application;
1136 char *psz_writing_application;
1137 char *psz_segment_filename;
1141 /* !!!!! GCC 3.3 bug on Darwin !!!!! */
1142 /* when you remove this variable the compiler issues an atomicity error */
1143 /* this variable only works when using std::vector<chapter_edition_c> */
1144 std::vector<chapter_edition_c*> stored_editions;
1145 int i_default_edition;
1147 std::vector<chapter_translation_c*> translations;
1148 std::vector<KaxSegmentFamily*> families;
1155 bool LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position );
1156 bool PreloadFamily( const matroska_segment_c & segment );
1157 void ParseInfo( KaxInfo *info );
1158 void ParseAttachments( KaxAttachments *attachments );
1159 void ParseChapters( KaxChapters *chapters );
1160 void ParseSeekHead( KaxSeekHead *seekhead );
1161 void ParseTracks( KaxTracks *tracks );
1162 void ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters );
1163 void ParseTrackEntry( KaxTrackEntry *m );
1164 void ParseCluster( );
1165 void IndexAppendCluster( KaxCluster *cluster );
1166 void LoadCues( KaxCues *cues );
1167 void LoadTags( KaxTags *tags );
1168 void InformationCreate( );
1169 void Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_global_position );
1170 int BlockGet( KaxBlock * &, KaxSimpleBlock * &, int64_t *, int64_t *, int64_t *);
1172 int BlockFindTrackIndex( size_t *pi_track,
1173 const KaxBlock *, const KaxSimpleBlock * );
1176 bool Select( mtime_t i_start_time );
1179 static bool CompareSegmentUIDs( const matroska_segment_c * item_a, const matroska_segment_c * item_b );
1182 // class holding hard-linked segment together in the playback order
1183 class virtual_segment_c
1186 virtual_segment_c( matroska_segment_c *p_segment )
1189 ,i_current_segment(0)
1190 ,i_current_edition(-1)
1191 ,psz_current_chapter(NULL)
1193 linked_segments.push_back( p_segment );
1195 AppendUID( p_segment->p_segment_uid );
1196 AppendUID( p_segment->p_prev_segment_uid );
1197 AppendUID( p_segment->p_next_segment_uid );
1201 size_t AddSegment( matroska_segment_c *p_segment );
1202 void PreloadLinked( );
1203 mtime_t Duration( ) const;
1204 void Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter, int64_t i_global_position );
1206 inline chapter_edition_c *Edition()
1208 if ( i_current_edition >= 0 && size_t(i_current_edition) < p_editions->size() )
1209 return (*p_editions)[i_current_edition];
1213 matroska_segment_c * Segment() const
1215 if ( linked_segments.size() == 0 || i_current_segment >= linked_segments.size() )
1217 return linked_segments[i_current_segment];
1220 inline chapter_item_c *CurrentChapter() {
1221 return psz_current_chapter;
1226 if ( i_current_segment < linked_segments.size()-1 )
1228 i_current_segment++;
1234 bool FindUID( KaxSegmentUID & uid ) const
1236 for ( size_t i=0; i<linked_uids.size(); i++ )
1238 if ( linked_uids[i] == uid )
1244 bool UpdateCurrentToChapter( demux_t & demux );
1245 void PrepareChapters( );
1247 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
1248 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1249 const void *p_cookie,
1250 size_t i_cookie_size );
1251 chapter_item_c *FindChapter( int64_t i_find_uid );
1253 std::vector<chapter_edition_c*> *p_editions;
1257 std::vector<matroska_segment_c*> linked_segments;
1258 std::vector<KaxSegmentUID> linked_uids;
1259 size_t i_current_segment;
1261 int i_current_edition;
1262 chapter_item_c *psz_current_chapter;
1264 void AppendUID( const EbmlBinary * UID );
1267 class matroska_stream_c
1270 matroska_stream_c( demux_sys_t & demuxer )
1276 virtual ~matroska_stream_c()
1285 std::vector<matroska_segment_c*> segments;
1311 virtual ~attachment_c()
1316 std::string psz_file_name;
1317 std::string psz_mime_type;
1325 demux_sys_t( demux_t & demux )
1332 ,p_current_segment(NULL)
1333 ,dvd_interpretor( *this )
1337 ,b_pci_packet_set(false)
1340 vlc_mutex_init( &lock_demuxer );
1343 virtual ~demux_sys_t()
1347 for ( i=0; i<streams.size(); i++ )
1349 for ( i=0; i<opened_segments.size(); i++ )
1350 delete opened_segments[i];
1351 for ( i=0; i<used_segments.size(); i++ )
1352 delete used_segments[i];
1353 for ( i=0; i<stored_attachments.size(); i++ )
1354 delete stored_attachments[i];
1355 if( meta ) vlc_meta_Delete( meta );
1357 while( titles.size() )
1358 { vlc_input_title_Delete( titles.back() ); titles.pop_back();}
1360 vlc_mutex_destroy( &lock_demuxer );
1367 mtime_t i_start_pts;
1368 mtime_t i_chapter_time;
1372 std::vector<input_title_t*> titles; // matroska editions
1373 size_t i_current_title;
1375 std::vector<matroska_stream_c*> streams;
1376 std::vector<attachment_c*> stored_attachments;
1377 std::vector<matroska_segment_c*> opened_segments;
1378 std::vector<virtual_segment_c*> used_segments;
1379 virtual_segment_c *p_current_segment;
1381 dvd_command_interpretor_c dvd_interpretor;
1383 /* duration of the stream */
1386 matroska_segment_c *FindSegment( const EbmlBinary & uid ) const;
1387 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
1388 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1389 const void *p_cookie,
1390 size_t i_cookie_size,
1391 virtual_segment_c * & p_segment_found );
1392 chapter_item_c *FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found );
1394 void PreloadFamily( const matroska_segment_c & of_segment );
1395 void PreloadLinked( matroska_segment_c *p_segment );
1396 bool PreparePlayback( virtual_segment_c *p_new_segment );
1397 matroska_stream_c *AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial = false );
1398 void JumpTo( virtual_segment_c & p_segment, chapter_item_c * p_chapter );
1400 void StartUiThread();
1401 void StopUiThread();
1403 inline void SwapButtons();
1405 /* for spu variables */
1406 input_thread_t *p_input;
1408 bool b_pci_packet_set;
1409 uint8_t palette[4][4];
1410 vlc_mutex_t lock_demuxer;
1413 event_thread_t *p_ev;
1414 static void * EventThread( vlc_object_t *p_this );
1415 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1416 vlc_value_t oldval, vlc_value_t newval, void *p_data );
1417 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1418 vlc_value_t oldval, vlc_value_t newval, void *p_data );
1423 virtual_segment_c *VirtualFromSegments( matroska_segment_c *p_segment ) const;
1424 bool IsUsedSegment( matroska_segment_c &p_segment ) const;
1427 static int Demux ( demux_t * );
1428 static int Control( demux_t *, int, va_list );
1429 static void Seek ( demux_t *, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter );
1431 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
1433 static inline char * ToUTF8( const UTFstring &u )
1435 return strdup( u.GetUTF8().c_str() );
1438 /*****************************************************************************
1439 * Open: initializes matroska demux structures
1440 *****************************************************************************/
1441 static int Open( vlc_object_t * p_this )
1443 demux_t *p_demux = (demux_t*)p_this;
1445 matroska_stream_c *p_stream;
1446 matroska_segment_c *p_segment;
1447 const uint8_t *p_peek;
1448 std::string s_path, s_filename;
1449 vlc_stream_io_callback *p_io_callback;
1450 EbmlStream *p_io_stream;
1452 /* peek the begining */
1453 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
1455 /* is a valid file */
1456 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
1457 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
1459 /* Set the demux function */
1460 p_demux->pf_demux = Demux;
1461 p_demux->pf_control = Control;
1462 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
1464 p_io_callback = new vlc_stream_io_callback( p_demux->s, false );
1465 p_io_stream = new EbmlStream( *p_io_callback );
1467 if( p_io_stream == NULL )
1469 msg_Err( p_demux, "failed to create EbmlStream" );
1470 delete p_io_callback;
1472 return VLC_EGENERIC;
1475 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_io_stream, true );
1476 if( p_stream == NULL )
1478 msg_Err( p_demux, "cannot find KaxSegment" );
1481 p_sys->streams.push_back( p_stream );
1483 p_stream->p_in = p_io_callback;
1484 p_stream->p_es = p_io_stream;
1486 for (size_t i=0; i<p_stream->segments.size(); i++)
1488 p_stream->segments[i]->Preload();
1491 p_segment = p_stream->segments[0];
1492 if( p_segment->cluster == NULL )
1494 msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
1498 if (config_GetInt( p_demux, "mkv-preload-local-dir" ))
1500 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
1501 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
1503 // assume it's a regular file
1504 // get the directory path
1505 s_path = p_demux->psz_path;
1506 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
1508 s_path = s_path.substr(0,s_path.length()-1);
1512 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
1514 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
1518 DIR *p_src_dir = utf8_opendir(s_path.c_str());
1520 if (p_src_dir != NULL)
1523 while ((psz_file = utf8_readdir(p_src_dir)) != NULL)
1525 if (strlen(psz_file) > 4)
1527 s_filename = s_path + DIRECTORY_SEPARATOR + psz_file;
1530 if (!strcasecmp(s_filename.c_str(), p_demux->psz_path))
1532 if (!s_filename.compare(p_demux->psz_path))
1536 continue; // don't reuse the original opened file
1539 #if defined(__GNUC__) && (__GNUC__ < 3)
1540 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
1541 !s_filename.compare("mka", s_filename.length() - 3, 3))
1543 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
1544 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
1547 // test wether this file belongs to our family
1548 const uint8_t *p_peek;
1549 bool file_ok = false;
1550 stream_t *p_file_stream = stream_UrlNew(
1552 s_filename.c_str());
1553 /* peek the begining */
1554 if( p_file_stream &&
1555 stream_Peek( p_file_stream, &p_peek, 4 ) >= 4
1556 && p_peek[0] == 0x1a && p_peek[1] == 0x45 &&
1557 p_peek[2] == 0xdf && p_peek[3] == 0xa3 ) file_ok = true;
1561 vlc_stream_io_callback *p_file_io = new vlc_stream_io_callback( p_file_stream, true );
1562 EbmlStream *p_estream = new EbmlStream(*p_file_io);
1564 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_estream );
1566 if ( p_stream == NULL )
1568 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
1574 p_stream->p_in = p_file_io;
1575 p_stream->p_es = p_estream;
1576 p_sys->streams.push_back( p_stream );
1581 if( p_file_stream ) {
1582 stream_Delete( p_file_stream );
1584 msg_Dbg( p_demux, "the file '%s' cannot be opened", s_filename.c_str() );
1590 closedir( p_src_dir );
1594 p_sys->PreloadFamily( *p_segment );
1597 p_sys->PreloadLinked( p_segment );
1599 if ( !p_sys->PreparePlayback( NULL ) )
1601 msg_Err( p_demux, "cannot use the segment" );
1605 p_sys->StartUiThread();
1611 return VLC_EGENERIC;
1614 /*****************************************************************************
1615 * Close: frees unused data
1616 *****************************************************************************/
1617 static void Close( vlc_object_t *p_this )
1619 demux_t *p_demux = (demux_t*)p_this;
1620 demux_sys_t *p_sys = p_demux->p_sys;
1625 /*****************************************************************************
1627 *****************************************************************************/
1628 static int Control( demux_t *p_demux, int i_query, va_list args )
1630 demux_sys_t *p_sys = p_demux->p_sys;
1637 input_attachment_t ***ppp_attach;
1643 case DEMUX_GET_ATTACHMENTS:
1644 ppp_attach = (input_attachment_t***)va_arg( args, input_attachment_t*** );
1645 pi_int = (int*)va_arg( args, int * );
1647 if( p_sys->stored_attachments.size() <= 0 )
1648 return VLC_EGENERIC;
1650 *pi_int = p_sys->stored_attachments.size();
1651 *ppp_attach = (input_attachment_t**)malloc( sizeof(input_attachment_t**) *
1652 p_sys->stored_attachments.size() );
1653 if( !(*ppp_attach) )
1655 for( i = 0; i < p_sys->stored_attachments.size(); i++ )
1657 attachment_c *a = p_sys->stored_attachments[i];
1658 (*ppp_attach)[i] = vlc_input_attachment_New( a->psz_file_name.c_str(), a->psz_mime_type.c_str(), NULL,
1659 a->p_data, a->i_size );
1663 case DEMUX_GET_META:
1664 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1665 vlc_meta_Merge( p_meta, p_sys->meta );
1668 case DEMUX_GET_LENGTH:
1669 pi64 = (int64_t*)va_arg( args, int64_t * );
1670 if( p_sys->f_duration > 0.0 )
1672 *pi64 = (int64_t)(p_sys->f_duration * 1000);
1675 return VLC_EGENERIC;
1677 case DEMUX_GET_POSITION:
1678 pf = (double*)va_arg( args, double * );
1679 if ( p_sys->f_duration > 0.0 )
1680 *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);
1683 case DEMUX_SET_POSITION:
1684 f = (double)va_arg( args, double );
1685 Seek( p_demux, -1, f, NULL );
1688 case DEMUX_GET_TIME:
1689 pi64 = (int64_t*)va_arg( args, int64_t * );
1690 *pi64 = p_sys->i_pts;
1693 case DEMUX_GET_TITLE_INFO:
1694 if( p_sys->titles.size() > 1 || ( p_sys->titles.size() == 1 && p_sys->titles[0]->i_seekpoint > 0 ) )
1696 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1697 int *pi_int = (int*)va_arg( args, int* );
1699 *pi_int = p_sys->titles.size();
1700 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->titles.size() );
1702 for( size_t i = 0; i < p_sys->titles.size(); i++ )
1704 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->titles[i] );
1708 return VLC_EGENERIC;
1710 case DEMUX_SET_TITLE:
1711 /* TODO handle editions as titles */
1712 i_idx = (int)va_arg( args, int );
1713 if( i_idx < p_sys->used_segments.size() )
1715 p_sys->JumpTo( *p_sys->used_segments[i_idx], NULL );
1718 return VLC_EGENERIC;
1720 case DEMUX_SET_SEEKPOINT:
1721 i_skp = (int)va_arg( args, int );
1723 // TODO change the way it works with the << & >> buttons on the UI (+1/-1 instead of a number)
1724 if( p_sys->titles.size() && i_skp < p_sys->titles[p_sys->i_current_title]->i_seekpoint)
1726 Seek( p_demux, (int64_t)p_sys->titles[p_sys->i_current_title]->seekpoint[i_skp]->i_time_offset, -1, NULL);
1727 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
1728 p_demux->info.i_seekpoint = i_skp;
1731 return VLC_EGENERIC;
1733 case DEMUX_SET_TIME:
1736 return VLC_EGENERIC;
1740 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1742 pp_simpleblock = NULL;
1749 EbmlElement *el = NULL;
1753 return VLC_EGENERIC;
1755 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1757 /* Check blocks validity to protect againts broken files */
1758 if( BlockFindTrackIndex( NULL, pp_block , pp_simpleblock ) )
1761 pp_simpleblock = NULL;
1766 /* update the index */
1767 #define idx p_indexes[i_index - 1]
1768 if( i_index > 0 && idx.i_time == -1 )
1770 if ( pp_simpleblock != NULL )
1771 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1773 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1774 idx.b_key = *pi_ref1 == 0 ? true : false;
1780 i_level = ep->GetLevel();
1789 msg_Warn( &sys.demuxer, "EOF" );
1790 return VLC_EGENERIC;
1793 /* Verify that we are still inside our cluster
1794 * It can happens whith broken files and when seeking
1798 if( cluster && !ep->IsTopPresent( cluster ) )
1800 msg_Warn( &sys.demuxer, "Unexpected escape from current cluster" );
1811 if( MKV_IS_ID( el, KaxCluster ) )
1813 cluster = (KaxCluster*)el;
1814 i_cluster_pos = cluster->GetElementPosition();
1816 /* add it to the index */
1818 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1820 IndexAppendCluster( cluster );
1823 // reset silent tracks
1824 for (size_t i=0; i<tracks.size(); i++)
1826 tracks[i]->b_silent = false;
1831 else if( MKV_IS_ID( el, KaxCues ) )
1833 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1834 return VLC_EGENERIC;
1838 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1842 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1844 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1846 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1847 cluster->InitTimecode( uint64( ctc ), i_timescale );
1849 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1853 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1855 i_block_pos = el->GetElementPosition();
1858 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1860 pp_simpleblock = (KaxSimpleBlock*)el;
1862 pp_simpleblock->ReadData( es.I_O() );
1863 pp_simpleblock->SetParent( *cluster );
1867 if( MKV_IS_ID( el, KaxBlock ) )
1869 pp_block = (KaxBlock*)el;
1871 pp_block->ReadData( es.I_O() );
1872 pp_block->SetParent( *cluster );
1876 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1878 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1880 dur.ReadData( es.I_O() );
1881 *pi_duration = uint64( dur );
1883 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1885 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1887 ref.ReadData( es.I_O() );
1890 *pi_ref1 = int64( ref ) * cluster->GlobalTimecodeScale();
1892 else if( *pi_ref2 == 0 )
1894 *pi_ref2 = int64( ref ) * cluster->GlobalTimecodeScale();
1897 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1899 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1900 track_num.ReadData( es.I_O() );
1902 for (size_t i=0; i<tracks.size(); i++)
1904 if ( tracks[i]->i_number == uint32(track_num))
1906 tracks[i]->b_silent = true;
1913 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1914 return VLC_EGENERIC;
1919 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem, size_t offset)
1922 if( !(p_block = block_New( p_demux, i_mem + offset ) ) ) return NULL;
1923 memcpy( p_block->p_buffer + offset, p_mem, i_mem );
1924 //p_block->i_rate = p_input->stream.control.i_rate;
1928 static void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
1929 mtime_t i_pts, mtime_t i_duration, bool f_mandatory )
1931 demux_sys_t *p_sys = p_demux->p_sys;
1932 matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
1938 if( p_segment->BlockFindTrackIndex( &i_track, block, simpleblock ) )
1940 msg_Err( p_demux, "invalid track number" );
1944 mkv_track_t *tk = p_segment->tracks[i_track];
1946 if( tk->fmt.i_cat != NAV_ES && tk->p_es == NULL )
1948 msg_Err( p_demux, "unknown track number" );
1951 if( i_pts + i_duration < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1953 return; /* discard audio packets that shouldn't be rendered */
1956 if ( tk->fmt.i_cat != NAV_ES )
1958 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1962 tk->b_inited = false;
1968 /* First send init data */
1969 if( !tk->b_inited && tk->i_data_init > 0 )
1973 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
1974 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init, 0 );
1975 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
1977 tk->b_inited = true;
1981 (block != NULL && i < block->NumberFrames()) || (simpleblock != NULL && i < simpleblock->NumberFrames());
1986 if( simpleblock != NULL )
1988 data = &simpleblock->GetBuffer(i);
1989 // condition when the DTS is correct (keyframe or B frame == NOT P frame)
1990 f_mandatory = simpleblock->IsDiscardable() || simpleblock->IsKeyframe();
1994 data = &block->GetBuffer(i);
1997 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER && tk->p_compression_data != NULL )
1998 p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), tk->p_compression_data->GetSize() );
2000 p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), 0 );
2002 if( p_block == NULL )
2007 #if defined(HAVE_ZLIB_H)
2008 if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB )
2010 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
2014 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER )
2016 memcpy( p_block->p_buffer, tk->p_compression_data->GetBuffer(), tk->p_compression_data->GetSize() );
2019 if ( tk->fmt.i_cat == NAV_ES )
2021 // TODO handle the start/stop times of this packet
2022 if ( p_sys->b_ui_hooked )
2024 vlc_mutex_lock( &p_sys->p_ev->lock );
2025 memcpy( &p_sys->pci_packet, &p_block->p_buffer[1], sizeof(pci_t) );
2026 p_sys->SwapButtons();
2027 p_sys->b_pci_packet_set = true;
2028 vlc_mutex_unlock( &p_sys->p_ev->lock );
2029 block_Release( p_block );
2033 // correct timestamping when B frames are used
2034 if( tk->fmt.i_cat != VIDEO_ES )
2036 p_block->i_dts = p_block->i_pts = i_pts;
2040 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
2042 // in VFW we have no idea about B frames
2044 p_block->i_dts = i_pts;
2048 p_block->i_pts = i_pts;
2050 p_block->i_dts = p_block->i_pts;
2052 p_block->i_dts = min( i_pts, tk->i_last_dts + (mtime_t)(tk->i_default_duration >> 10));
2053 p_sys->i_pts = p_block->i_dts;
2056 tk->i_last_dts = p_block->i_dts;
2059 msg_Dbg( p_demux, "block i_dts: %"PRId64" / i_pts: %"PRId64, p_block->i_dts, p_block->i_pts);
2061 if( strcmp( tk->psz_codec, "S_VOBSUB" ) )
2063 p_block->i_length = i_duration * 1000;
2066 es_out_Send( p_demux->out, tk->p_es, p_block );
2068 /* use time stamp only for first block */
2073 matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial )
2075 int i_upper_lvl = 0;
2077 EbmlElement *p_l0, *p_l1, *p_l2;
2078 bool b_keep_stream = false, b_keep_segment;
2080 // verify the EBML Header
2081 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
2084 msg_Err( p_demux, "No EBML header found" );
2088 // verify we can read this Segment, we only support Matroska version 1 for now
2089 p_l0->Read(*p_estream, EbmlHead::ClassInfos.Context, i_upper_lvl, p_l0, true);
2091 EDocType doc_type = GetChild<EDocType>(*static_cast<EbmlHead*>(p_l0));
2092 if (std::string(doc_type) != "matroska")
2094 msg_Err( p_demux, "Not a Matroska file : DocType = %s ", std::string(doc_type).c_str());
2098 EDocTypeReadVersion doc_read_version = GetChild<EDocTypeReadVersion>(*static_cast<EbmlHead*>(p_l0));
2099 if (uint64(doc_read_version) > 2)
2101 msg_Err( p_demux, "This matroska file is needs version %"PRId64" and this VLC only supports version 1 & 2", uint64(doc_read_version));
2108 // find all segments in this file
2109 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFFLL);
2115 matroska_stream_c *p_stream1 = new matroska_stream_c( *this );
2119 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
2122 matroska_segment_c *p_segment1 = new matroska_segment_c( *this, *p_estream );
2123 b_keep_segment = b_initial;
2125 ep = new EbmlParser(p_estream, p_l0, &demuxer );
2126 p_segment1->ep = ep;
2127 p_segment1->segment = (KaxSegment*)p_l0;
2129 while ((p_l1 = ep->Get()))
2131 if (MKV_IS_ID(p_l1, KaxInfo))
2133 // find the families of this segment
2134 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
2136 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
2137 for( i = 0; i < p_info->ListSize(); i++ )
2139 EbmlElement *l = (*p_info)[i];
2141 if( MKV_IS_ID( l, KaxSegmentUID ) )
2143 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
2144 b_keep_segment = (FindSegment( *p_uid ) == NULL);
2145 if ( !b_keep_segment )
2146 break; // this segment is already known
2147 opened_segments.push_back( p_segment1 );
2148 delete p_segment1->p_segment_uid;
2149 p_segment1->p_segment_uid = new KaxSegmentUID(*p_uid);
2151 else if( MKV_IS_ID( l, KaxPrevUID ) )
2153 p_segment1->p_prev_segment_uid = new KaxPrevUID( *static_cast<KaxPrevUID*>(l) );
2155 else if( MKV_IS_ID( l, KaxNextUID ) )
2157 p_segment1->p_next_segment_uid = new KaxNextUID( *static_cast<KaxNextUID*>(l) );
2159 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
2161 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
2162 p_segment1->families.push_back( p_fam );
2168 if ( b_keep_segment )
2170 b_keep_stream = true;
2171 p_stream1->segments.push_back( p_segment1 );
2175 p_segment1->segment = NULL;
2179 if (p_l0->IsFiniteSize() )
2181 p_l0->SkipData(*p_estream, KaxMatroska_Context);
2182 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
2185 p_l0 = p_l0->SkipData(*p_estream, KaxSegment_Context);
2188 if ( !b_keep_stream )
2197 bool matroska_segment_c::Select( mtime_t i_start_time )
2202 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
2203 sys.b_pci_packet_set = false;
2205 for( i_track = 0; i_track < tracks.size(); i_track++ )
2207 if( tracks[i_track]->fmt.i_cat == UNKNOWN_ES )
2209 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, tracks[i_track]->i_number );
2210 tracks[i_track]->p_es = NULL;
2214 if( !strcmp( tracks[i_track]->psz_codec, "V_MS/VFW/FOURCC" ) )
2216 if( tracks[i_track]->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
2218 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
2219 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2223 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tracks[i_track]->p_extra_data;
2225 tracks[i_track]->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
2226 tracks[i_track]->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
2227 tracks[i_track]->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
2229 tracks[i_track]->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
2230 if( tracks[i_track]->fmt.i_extra > 0 )
2232 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2233 memcpy( tracks[i_track]->fmt.p_extra, &p_bih[1], tracks[i_track]->fmt.i_extra );
2237 else if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG1" ) ||
2238 !strcmp( tracks[i_track]->psz_codec, "V_MPEG2" ) )
2240 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
2242 else if( !strncmp( tracks[i_track]->psz_codec, "V_THEORA", 8 ) )
2244 uint8_t *p_data = tracks[i_track]->p_extra_data;
2245 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
2246 if( tracks[i_track]->i_extra_data >= 4 ) {
2247 if( p_data[0] == 2 ) {
2249 int i_size1 = 0, i_size2 = 0;
2251 /* read size of first header packet */
2252 while( *p_data == 0xFF &&
2253 i < tracks[i_track]->i_extra_data )
2262 msg_Dbg( &sys.demuxer, "first theora header size %d", i_size1 );
2263 /* read size of second header packet */
2264 while( *p_data == 0xFF &&
2265 i < tracks[i_track]->i_extra_data )
2274 int i_size3 = tracks[i_track]->i_extra_data - i - i_size1
2276 msg_Dbg( &sys.demuxer, "second theora header size %d", i_size2 );
2277 msg_Dbg( &sys.demuxer, "third theora header size %d", i_size3 );
2278 tracks[i_track]->fmt.i_extra = i_size1 + i_size2 + i_size3
2280 if( i_size1 > 0 && i_size2 > 0 && i_size3 > 0 ) {
2281 tracks[i_track]->fmt.p_extra =
2282 malloc( tracks[i_track]->fmt.i_extra );
2283 uint8_t *p_out = (uint8_t*)tracks[i_track]->fmt.p_extra;
2284 *p_out++ = (i_size1>>8) & 0xFF;
2285 *p_out++ = i_size1 & 0xFF;
2286 memcpy( p_out, p_data, i_size1 );
2290 *p_out++ = (i_size2>>8) & 0xFF;
2291 *p_out++ = i_size2 & 0xFF;
2292 memcpy( p_out, p_data, i_size2 );
2296 *p_out++ = (i_size3>>8) & 0xFF;
2297 *p_out++ = i_size3 & 0xFF;
2298 memcpy( p_out, p_data, i_size3 );
2304 msg_Err( &sys.demuxer, "inconsistant theora extradata" );
2308 msg_Err( &sys.demuxer, "Wrong number of ogg packets with theora headers (%d)", p_data[0] + 1 );
2312 else if( !strncmp( tracks[i_track]->psz_codec, "V_REAL/RV", 9 ) )
2314 if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV10" ) )
2315 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '1', '0' );
2316 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV20" ) )
2317 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '2', '0' );
2318 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV30" ) )
2319 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '3', '0' );
2320 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV40" ) )
2321 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '4', '0' );
2323 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4", 7 ) )
2325 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/MS/V3" ) )
2327 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
2329 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO", 11 ) )
2331 /* A MPEG 4 codec, SP, ASP, AP or AVC */
2332 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO/AVC" ) )
2333 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
2335 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
2336 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2337 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2338 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2341 else if( !strcmp( tracks[i_track]->psz_codec, "V_QUICKTIME" ) )
2343 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
2344 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
2345 tracks[i_track]->p_extra_data,
2346 tracks[i_track]->i_extra_data,
2348 MP4_ReadBoxCommon( p_mp4_stream, p_box );
2349 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
2350 tracks[i_track]->fmt.i_codec = p_box->i_type;
2351 tracks[i_track]->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
2352 tracks[i_track]->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
2353 tracks[i_track]->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
2354 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2355 memcpy( tracks[i_track]->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tracks[i_track]->fmt.i_extra );
2356 MP4_FreeBox_sample_vide( p_box );
2357 stream_Delete( p_mp4_stream );
2359 else if( !strcmp( tracks[i_track]->psz_codec, "A_MS/ACM" ) )
2361 if( tracks[i_track]->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
2363 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
2364 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2368 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tracks[i_track]->p_extra_data;
2370 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tracks[i_track]->fmt.i_codec, NULL );
2372 tracks[i_track]->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
2373 tracks[i_track]->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
2374 tracks[i_track]->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
2375 tracks[i_track]->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
2376 tracks[i_track]->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
2378 tracks[i_track]->fmt.i_extra = GetWLE( &p_wf->cbSize );
2379 if( tracks[i_track]->fmt.i_extra > 0 )
2381 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2382 memcpy( tracks[i_track]->fmt.p_extra, &p_wf[1], tracks[i_track]->fmt.i_extra );
2386 else if( !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L3" ) ||
2387 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L2" ) ||
2388 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L1" ) )
2390 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
2392 else if( !strcmp( tracks[i_track]->psz_codec, "A_AC3" ) )
2394 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2396 else if( !strcmp( tracks[i_track]->psz_codec, "A_DTS" ) )
2398 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2400 else if( !strcmp( tracks[i_track]->psz_codec, "A_FLAC" ) )
2402 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
2403 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2404 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2405 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2407 else if( !strcmp( tracks[i_track]->psz_codec, "A_VORBIS" ) )
2409 int i, i_offset = 1, i_size[3], i_extra;
2412 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
2414 /* Split the 3 headers */
2415 if( tracks[i_track]->p_extra_data[0] != 0x02 )
2416 msg_Err( &sys.demuxer, "invalid vorbis header" );
2418 for( i = 0; i < 2; i++ )
2421 while( i_offset < tracks[i_track]->i_extra_data )
2423 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
2424 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
2428 i_size[0] = __MIN(i_size[0], tracks[i_track]->i_extra_data - i_offset);
2429 i_size[1] = __MIN(i_size[1], tracks[i_track]->i_extra_data -i_offset -i_size[0]);
2430 i_size[2] = tracks[i_track]->i_extra_data - i_offset - i_size[0] - i_size[1];
2432 tracks[i_track]->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
2433 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2434 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra; i_extra = 0;
2435 for( i = 0; i < 3; i++ )
2437 *(p_extra++) = i_size[i] >> 8;
2438 *(p_extra++) = i_size[i] & 0xFF;
2439 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra,
2441 p_extra += i_size[i];
2442 i_extra += i_size[i];
2445 else if( !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
2446 !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
2448 int i_profile, i_srate, sbr = 0;
2449 static const unsigned int i_sample_rates[] =
2451 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
2452 16000, 12000, 11025, 8000, 7350, 0, 0, 0
2455 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
2456 /* create data for faad (MP4DecSpecificDescrTag)*/
2458 if( !strcmp( &tracks[i_track]->psz_codec[12], "MAIN" ) )
2462 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC" ) )
2466 else if( !strcmp( &tracks[i_track]->psz_codec[12], "SSR" ) )
2470 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC/SBR" ) )
2480 for( i_srate = 0; i_srate < 13; i_srate++ )
2482 if( i_sample_rates[i_srate] == tracks[i_track]->i_original_rate )
2487 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
2489 tracks[i_track]->fmt.i_extra = sbr ? 5 : 2;
2490 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2491 ((uint8_t*)tracks[i_track]->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
2492 ((uint8_t*)tracks[i_track]->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tracks[i_track]->fmt.audio.i_channels << 3);
2495 int syncExtensionType = 0x2B7;
2497 for (iDSRI=0; iDSRI<13; iDSRI++)
2498 if( i_sample_rates[iDSRI] == tracks[i_track]->fmt.audio.i_rate )
2500 ((uint8_t*)tracks[i_track]->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
2501 ((uint8_t*)tracks[i_track]->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
2502 ((uint8_t*)tracks[i_track]->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
2505 else if( !strcmp( tracks[i_track]->psz_codec, "A_AAC" ) )
2507 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
2508 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2509 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2510 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2512 else if( !strcmp( tracks[i_track]->psz_codec, "A_WAVPACK4" ) )
2514 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'W', 'V', 'P', 'K' );
2515 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2516 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2517 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2519 else if( !strcmp( tracks[i_track]->psz_codec, "A_TTA1" ) )
2521 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'T', 'T', 'A', '1' );
2522 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2523 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2524 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2526 else if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) ||
2527 !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/LIT" ) ||
2528 !strcmp( tracks[i_track]->psz_codec, "A_PCM/FLOAT/IEEE" ) )
2530 if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) )
2532 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
2536 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
2538 tracks[i_track]->fmt.audio.i_blockalign = ( tracks[i_track]->fmt.audio.i_bitspersample + 7 ) / 8 * tracks[i_track]->fmt.audio.i_channels;
2540 /* disabled due to the potential "S_KATE" namespace issue */
2541 else if( !strcmp( tracks[i_track]->psz_codec, "S_KATE" ) )
2543 int i, i_offset = 1, *i_size, i_extra, num_headers, size_so_far;
2546 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'k', 'a', 't', 'e' );
2547 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2549 /* Recover the number of headers to expect */
2550 num_headers = tracks[i_track]->p_extra_data[0]+1;
2551 msg_Dbg( &sys.demuxer, "kate in mkv detected: %d headers in %u bytes",
2552 num_headers, tracks[i_track]->i_extra_data);
2554 /* this won't overflow the stack as is can allocate only 1020 bytes max */
2555 i_size = (int*)alloca(num_headers*sizeof(int));
2557 /* Split the headers */
2559 for( i = 0; i < num_headers-1; i++ )
2562 while( i_offset < tracks[i_track]->i_extra_data )
2564 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
2565 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
2567 msg_Dbg( &sys.demuxer, "kate header %d is %d bytes", i, i_size[i]);
2568 size_so_far += i_size[i];
2570 i_size[num_headers-1] = tracks[i_track]->i_extra_data - (size_so_far+i_offset);
2571 msg_Dbg( &sys.demuxer, "kate last header (%d) is %d bytes", num_headers-1, i_size[num_headers-1]);
2573 tracks[i_track]->fmt.i_extra = 1 + num_headers * 2 + size_so_far + i_size[num_headers-1];
2574 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2576 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra;
2578 *(p_extra++) = num_headers;
2580 for( i = 0; i < num_headers; i++ )
2582 *(p_extra++) = i_size[i] >> 8;
2583 *(p_extra++) = i_size[i] & 0xFF;
2584 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra-1,
2586 p_extra += i_size[i];
2587 i_extra += i_size[i];
2590 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/UTF8" ) )
2592 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
2593 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2595 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/USF" ) )
2597 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
2598 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2599 if( tracks[i_track]->i_extra_data )
2601 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2602 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2603 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2606 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/SSA" ) ||
2607 !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASS" ) ||
2608 !strcmp( tracks[i_track]->psz_codec, "S_SSA" ) ||
2609 !strcmp( tracks[i_track]->psz_codec, "S_ASS" ))
2611 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
2612 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2613 if( tracks[i_track]->i_extra_data )
2615 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2616 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2617 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2620 else if( !strcmp( tracks[i_track]->psz_codec, "S_VOBSUB" ) )
2622 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
2623 if( tracks[i_track]->i_extra_data )
2626 char *p_buf = (char *)malloc( tracks[i_track]->i_extra_data + 1);
2627 memcpy( p_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
2628 p_buf[tracks[i_track]->i_extra_data] = '\0';
2630 p_start = strstr( p_buf, "size:" );
2631 if( sscanf( p_start, "size: %dx%d",
2632 &tracks[i_track]->fmt.subs.spu.i_original_frame_width, &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) == 2 )
2634 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 );
2638 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
2643 else if( !strcmp( tracks[i_track]->psz_codec, "B_VOBBTN" ) )
2645 tracks[i_track]->fmt.i_cat = NAV_ES;
2650 msg_Err( &sys.demuxer, "unknown codec id=`%s'", tracks[i_track]->psz_codec );
2651 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2653 if( tracks[i_track]->b_default )
2655 tracks[i_track]->fmt.i_priority = 1000;
2658 tracks[i_track]->p_es = es_out_Add( sys.demuxer.out, &tracks[i_track]->fmt );
2660 /* Turn on a subtitles track if it has been flagged as default -
2661 * but only do this if no subtitles track has already been engaged,
2662 * either by an earlier 'default track' (??) or by default
2663 * language choice behaviour.
2665 if( tracks[i_track]->b_default )
2667 es_out_Control( sys.demuxer.out,
2669 tracks[i_track]->p_es );
2672 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, i_start_time );
2675 sys.i_start_pts = i_start_time;
2676 // reset the stream reading to the first cluster of the segment used
2677 es.I_O().setFilePointer( i_start_pos );
2680 ep = new EbmlParser( &es, segment, &sys.demuxer );
2685 void demux_sys_t::StartUiThread()
2689 msg_Dbg( &demuxer, "Starting the UI Hook" );
2691 /* FIXME hack hack hack hack FIXME */
2692 /* Get p_input and create variable */
2693 p_input = (input_thread_t *) vlc_object_find( &demuxer, VLC_OBJECT_INPUT, FIND_PARENT );
2694 var_Create( p_input, "x-start", VLC_VAR_INTEGER );
2695 var_Create( p_input, "y-start", VLC_VAR_INTEGER );
2696 var_Create( p_input, "x-end", VLC_VAR_INTEGER );
2697 var_Create( p_input, "y-end", VLC_VAR_INTEGER );
2698 var_Create( p_input, "color", VLC_VAR_ADDRESS );
2699 var_Create( p_input, "menu-palette", VLC_VAR_ADDRESS );
2700 var_Create( p_input, "highlight", VLC_VAR_BOOL );
2701 var_Create( p_input, "highlight-mutex", VLC_VAR_MUTEX );
2703 /* Now create our event thread catcher */
2704 p_ev = (event_thread_t *) vlc_object_create( &demuxer, sizeof( event_thread_t ) );
2705 p_ev->p_demux = &demuxer;
2706 p_ev->b_die = false;
2707 vlc_mutex_init( &p_ev->lock );
2708 vlc_thread_create( p_ev, "mkv event thread handler", EventThread,
2709 VLC_THREAD_PRIORITY_LOW, false );
2713 void demux_sys_t::StopUiThread()
2717 vlc_object_kill( p_ev );
2718 vlc_thread_join( p_ev );
2719 vlc_object_release( p_ev );
2723 var_Destroy( p_input, "highlight-mutex" );
2724 var_Destroy( p_input, "highlight" );
2725 var_Destroy( p_input, "x-start" );
2726 var_Destroy( p_input, "x-end" );
2727 var_Destroy( p_input, "y-start" );
2728 var_Destroy( p_input, "y-end" );
2729 var_Destroy( p_input, "color" );
2730 var_Destroy( p_input, "menu-palette" );
2732 vlc_object_release( p_input );
2734 msg_Dbg( &demuxer, "Stopping the UI Hook" );
2736 b_ui_hooked = false;
2739 int demux_sys_t::EventMouse( vlc_object_t *p_this, char const *psz_var,
2740 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2742 event_thread_t *p_ev = (event_thread_t *) p_data;
2743 vlc_mutex_lock( &p_ev->lock );
2744 if( psz_var[6] == 'c' )
2746 p_ev->b_clicked = true;
2747 msg_Dbg( p_this, "Event Mouse: clicked");
2749 else if( psz_var[6] == 'm' )
2750 p_ev->b_moved = true;
2751 vlc_mutex_unlock( &p_ev->lock );
2756 int demux_sys_t::EventKey( vlc_object_t *p_this, char const *,
2757 vlc_value_t, vlc_value_t newval, void *p_data )
2759 event_thread_t *p_ev = (event_thread_t *) p_data;
2760 vlc_mutex_lock( &p_ev->lock );
2761 p_ev->i_key_action = newval.i_int;
2762 vlc_mutex_unlock( &p_ev->lock );
2763 msg_Dbg( p_this, "Event Key");
2768 void * demux_sys_t::EventThread( vlc_object_t *p_this )
2770 event_thread_t *p_ev = (event_thread_t*)p_this;
2771 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
2772 vlc_object_t *p_vout = NULL;
2774 p_ev->b_moved = false;
2775 p_ev->b_clicked = false;
2776 p_ev->i_key_action = 0;
2778 /* catch all key event */
2779 var_AddCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
2782 while( vlc_object_alive (p_ev) )
2784 if ( !p_sys->b_pci_packet_set )
2791 bool b_activated = false;
2794 if( p_ev->i_key_action )
2798 msg_Dbg( p_ev->p_demux, "Handle Key Event");
2800 vlc_mutex_lock( &p_ev->lock );
2802 pci_t *pci = (pci_t *) &p_sys->pci_packet;
2804 uint16 i_curr_button = p_sys->dvd_interpretor.GetSPRM( 0x88 );
2806 switch( p_ev->i_key_action )
2808 case ACTIONID_NAV_LEFT:
2809 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2811 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2812 if ( p_button_ptr->left > 0 && p_button_ptr->left <= pci->hli.hl_gi.btn_ns )
2814 i_curr_button = p_button_ptr->left;
2815 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2816 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2817 if ( button_ptr.auto_action_mode )
2819 vlc_mutex_unlock( &p_ev->lock );
2820 vlc_mutex_lock( &p_sys->lock_demuxer );
2822 // process the button action
2823 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2825 vlc_mutex_unlock( &p_sys->lock_demuxer );
2826 vlc_mutex_lock( &p_ev->lock );
2831 case ACTIONID_NAV_RIGHT:
2832 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2834 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2835 if ( p_button_ptr->right > 0 && p_button_ptr->right <= pci->hli.hl_gi.btn_ns )
2837 i_curr_button = p_button_ptr->right;
2838 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2839 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2840 if ( button_ptr.auto_action_mode )
2842 vlc_mutex_unlock( &p_ev->lock );
2843 vlc_mutex_lock( &p_sys->lock_demuxer );
2845 // process the button action
2846 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2848 vlc_mutex_unlock( &p_sys->lock_demuxer );
2849 vlc_mutex_lock( &p_ev->lock );
2854 case ACTIONID_NAV_UP:
2855 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2857 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2858 if ( p_button_ptr->up > 0 && p_button_ptr->up <= pci->hli.hl_gi.btn_ns )
2860 i_curr_button = p_button_ptr->up;
2861 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2862 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2863 if ( button_ptr.auto_action_mode )
2865 vlc_mutex_unlock( &p_ev->lock );
2866 vlc_mutex_lock( &p_sys->lock_demuxer );
2868 // process the button action
2869 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2871 vlc_mutex_unlock( &p_sys->lock_demuxer );
2872 vlc_mutex_lock( &p_ev->lock );
2877 case ACTIONID_NAV_DOWN:
2878 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2880 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2881 if ( p_button_ptr->down > 0 && p_button_ptr->down <= pci->hli.hl_gi.btn_ns )
2883 i_curr_button = p_button_ptr->down;
2884 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2885 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2886 if ( button_ptr.auto_action_mode )
2888 vlc_mutex_unlock( &p_ev->lock );
2889 vlc_mutex_lock( &p_sys->lock_demuxer );
2891 // process the button action
2892 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2894 vlc_mutex_unlock( &p_sys->lock_demuxer );
2895 vlc_mutex_lock( &p_ev->lock );
2900 case ACTIONID_NAV_ACTIVATE:
2903 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2905 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2907 vlc_mutex_unlock( &p_ev->lock );
2908 vlc_mutex_lock( &p_sys->lock_demuxer );
2910 // process the button action
2911 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2913 vlc_mutex_unlock( &p_sys->lock_demuxer );
2914 vlc_mutex_lock( &p_ev->lock );
2920 p_ev->i_key_action = 0;
2921 vlc_mutex_unlock( &p_ev->lock );
2925 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
2927 vlc_value_t valx, valy;
2929 vlc_mutex_lock( &p_ev->lock );
2930 pci_t *pci = (pci_t *) &p_sys->pci_packet;
2931 var_Get( p_vout, "mouse-x", &valx );
2932 var_Get( p_vout, "mouse-y", &valy );
2934 if( p_ev->b_clicked )
2937 int32_t best,dist,d;
2938 int32_t mx,my,dx,dy;
2940 msg_Dbg( p_ev->p_demux, "Handle Mouse Event: Mouse clicked x(%d)*y(%d)", (unsigned)valx.i_int, (unsigned)valy.i_int);
2943 // get current button
2945 dist = 0x08000000; /* >> than (720*720)+(567*567); */
2946 for(button = 1; button <= pci->hli.hl_gi.btn_ns; button++)
2948 btni_t *button_ptr = &(pci->hli.btnit[button-1]);
2950 if(((unsigned)valx.i_int >= button_ptr->x_start)
2951 && ((unsigned)valx.i_int <= button_ptr->x_end)
2952 && ((unsigned)valy.i_int >= button_ptr->y_start)
2953 && ((unsigned)valy.i_int <= button_ptr->y_end))
2955 mx = (button_ptr->x_start + button_ptr->x_end)/2;
2956 my = (button_ptr->y_start + button_ptr->y_end)/2;
2957 dx = mx - valx.i_int;
2958 dy = my - valy.i_int;
2959 d = (dx*dx) + (dy*dy);
2960 /* If the mouse is within the button and the mouse is closer
2961 * to the center of this button then it is the best choice. */
2971 btni_t button_ptr = pci->hli.btnit[best-1];
2972 uint16 i_curr_button = p_sys->dvd_interpretor.GetSPRM( 0x88 );
2974 msg_Dbg( &p_sys->demuxer, "Clicked button %d", best );
2975 vlc_mutex_unlock( &p_ev->lock );
2976 vlc_mutex_lock( &p_sys->lock_demuxer );
2978 // process the button action
2979 p_sys->dvd_interpretor.SetSPRM( 0x88, best );
2980 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2982 msg_Dbg( &p_sys->demuxer, "Processed button %d", best );
2984 // select new button
2985 if ( best != i_curr_button )
2989 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
2991 vlc_mutex_t *p_mutex = (vlc_mutex_t *) val.p_address;
2994 if(button_ptr.btn_coln != 0) {
2995 i_palette = pci->hli.btn_colit.btn_coli[button_ptr.btn_coln-1][1];
3000 for( int i = 0; i < 4; i++ )
3002 uint32_t i_yuv = 0xFF;//p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
3003 uint8_t i_alpha = (i_palette>>(i*4))&0x0f;
3004 i_alpha = i_alpha == 0xf ? 0xff : i_alpha << 4;
3006 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
3007 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
3008 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
3009 p_sys->palette[i][3] = i_alpha;
3012 vlc_mutex_lock( p_mutex );
3013 val.i_int = button_ptr.x_start; var_Set( p_sys->p_input, "x-start", val );
3014 val.i_int = button_ptr.x_end; var_Set( p_sys->p_input, "x-end", val );
3015 val.i_int = button_ptr.y_start; var_Set( p_sys->p_input, "y-start", val );
3016 val.i_int = button_ptr.y_end; var_Set( p_sys->p_input, "y-end", val );
3018 val.p_address = (void *)p_sys->palette;
3019 var_Set( p_sys->p_input, "menu-palette", val );
3021 val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
3022 vlc_mutex_unlock( p_mutex );
3025 vlc_mutex_unlock( &p_sys->lock_demuxer );
3026 vlc_mutex_lock( &p_ev->lock );
3029 else if( p_ev->b_moved )
3031 // dvdnav_mouse_select( NULL, pci, valx.i_int, valy.i_int );
3034 p_ev->b_moved = false;
3035 p_ev->b_clicked = false;
3036 vlc_mutex_unlock( &p_ev->lock );
3040 if( p_vout && !vlc_object_alive (p_vout) )
3042 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3043 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3044 vlc_object_release( p_vout );
3048 else if( p_vout == NULL )
3050 p_vout = (vlc_object_t*) vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
3054 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3055 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3059 /* Wait a bit, 10ms */
3063 /* Release callback */
3066 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3067 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3068 vlc_object_release( p_vout );
3070 var_DelCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
3072 vlc_mutex_destroy( &p_ev->lock );
3077 void matroska_segment_c::UnSelect( )
3081 for( i_track = 0; i_track < tracks.size(); i_track++ )
3083 if ( tracks[i_track]->p_es != NULL )
3085 // es_format_Clean( &tracks[i_track]->fmt );
3086 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
3087 tracks[i_track]->p_es = NULL;
3094 void virtual_segment_c::PrepareChapters( )
3096 if ( linked_segments.size() == 0 )
3099 // !!! should be called only once !!!
3100 matroska_segment_c *p_segment;
3103 // copy editions from the first segment
3104 p_segment = linked_segments[0];
3105 p_editions = &p_segment->stored_editions;
3107 for ( i=1 ; i<linked_segments.size(); i++ )
3109 p_segment = linked_segments[i];
3110 // FIXME assume we have the same editions in all segments
3111 for (j=0; j<p_segment->stored_editions.size(); j++)
3113 if( j >= p_editions->size() ) /* Protect against broken files (?) */
3115 (*p_editions)[j]->Append( *p_segment->stored_editions[j] );
3120 std::string chapter_edition_c::GetMainName() const
3122 if ( sub_chapters.size() )
3124 return sub_chapters[0]->GetCodecName( true );
3129 int chapter_item_c::PublishChapters( input_title_t & title, int & i_user_chapters, int i_level )
3131 // add support for meta-elements from codec like DVD Titles
3132 if ( !b_display_seekpoint || psz_name == "" )
3134 psz_name = GetCodecName();
3135 if ( psz_name != "" )
3136 b_display_seekpoint = true;
3139 if (b_display_seekpoint)
3141 seekpoint_t *sk = vlc_seekpoint_New();
3143 sk->i_level = i_level;
3144 sk->i_time_offset = i_start_time;
3145 sk->psz_name = strdup( psz_name.c_str() );
3147 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
3148 title.i_seekpoint++;
3149 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
3150 title.seekpoint[title.i_seekpoint-1] = sk;
3152 if ( b_user_display )
3156 for ( size_t i=0; i<sub_chapters.size() ; i++)
3158 sub_chapters[i]->PublishChapters( title, i_user_chapters, i_level+1 );
3161 i_seekpoint_num = i_user_chapters;
3163 return i_user_chapters;
3166 bool virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
3168 demux_sys_t & sys = *demux.p_sys;
3169 chapter_item_c *psz_curr_chapter;
3170 bool b_has_seeked = false;
3172 /* update current chapter/seekpoint */
3173 if ( p_editions->size() )
3175 /* 1st, we need to know in which chapter we are */
3176 psz_curr_chapter = (*p_editions)[i_current_edition]->FindTimecode( sys.i_pts, psz_current_chapter );
3178 /* we have moved to a new chapter */
3179 if (psz_curr_chapter != NULL && psz_current_chapter != psz_curr_chapter)
3181 if ( (*p_editions)[i_current_edition]->b_ordered )
3183 // Leave/Enter up to the link point
3184 b_has_seeked = psz_curr_chapter->EnterAndLeave( psz_current_chapter );
3185 if ( !b_has_seeked )
3187 // only physically seek if necessary
3188 if ( psz_current_chapter == NULL || (psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time) )
3189 Seek( demux, sys.i_pts, 0, psz_curr_chapter, -1 );
3193 if ( !b_has_seeked )
3195 psz_current_chapter = psz_curr_chapter;
3196 if ( psz_curr_chapter->i_seekpoint_num > 0 )
3198 demux.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
3199 demux.info.i_title = sys.i_current_title = i_sys_title;
3200 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
3206 else if (psz_curr_chapter == NULL)
3208 // out of the scope of the data described by chapters, leave the edition
3209 if ( (*p_editions)[i_current_edition]->b_ordered && psz_current_chapter != NULL )
3211 if ( !(*p_editions)[i_current_edition]->EnterAndLeave( psz_current_chapter, false ) )
3212 psz_current_chapter = NULL;
3221 chapter_item_c *virtual_segment_c::BrowseCodecPrivate( unsigned int codec_id,
3222 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3223 const void *p_cookie,
3224 size_t i_cookie_size )
3226 // FIXME don't assume it is the first edition
3227 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
3228 if ( index != p_editions->end() )
3230 chapter_item_c *p_result = (*index)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3231 if ( p_result != NULL )
3237 chapter_item_c *virtual_segment_c::FindChapter( int64_t i_find_uid )
3239 // FIXME don't assume it is the first edition
3240 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
3241 if ( index != p_editions->end() )
3243 chapter_item_c *p_result = (*index)->FindChapter( i_find_uid );
3244 if ( p_result != NULL )
3250 chapter_item_c *chapter_item_c::BrowseCodecPrivate( unsigned int codec_id,
3251 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3252 const void *p_cookie,
3253 size_t i_cookie_size )
3256 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3257 while ( index != codecs.end() )
3259 if ( match( **index ,p_cookie, i_cookie_size ) )
3265 chapter_item_c *p_result = NULL;
3266 std::vector<chapter_item_c*>::const_iterator index2 = sub_chapters.begin();
3267 while ( index2 != sub_chapters.end() )
3269 p_result = (*index2)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3270 if ( p_result != NULL )
3278 void chapter_item_c::Append( const chapter_item_c & chapter )
3280 // we are appending content for the same chapter UID
3282 chapter_item_c *p_chapter;
3284 for ( i=0; i<chapter.sub_chapters.size(); i++ )
3286 p_chapter = FindChapter( chapter.sub_chapters[i]->i_uid );
3287 if ( p_chapter != NULL )
3289 p_chapter->Append( *chapter.sub_chapters[i] );
3293 sub_chapters.push_back( chapter.sub_chapters[i] );
3297 i_user_start_time = min( i_user_start_time, chapter.i_user_start_time );
3298 i_user_end_time = max( i_user_end_time, chapter.i_user_end_time );
3301 chapter_item_c * chapter_item_c::FindChapter( int64_t i_find_uid )
3304 chapter_item_c *p_result = NULL;
3306 if ( i_uid == i_find_uid )
3309 for ( i=0; i<sub_chapters.size(); i++)
3311 p_result = sub_chapters[i]->FindChapter( i_find_uid );
3312 if ( p_result != NULL )
3318 std::string chapter_item_c::GetCodecName( bool f_for_title ) const
3322 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3323 while ( index != codecs.end() )
3325 result = (*index)->GetCodecName( f_for_title );
3334 std::string dvd_chapter_codec_c::GetCodecName( bool f_for_title ) const
3337 if ( p_private_data->GetSize() >= 3)
3339 const binary* p_data = p_private_data->GetBuffer();
3340 /* if ( p_data[0] == MATROSKA_DVD_LEVEL_TT )
3342 uint16_t i_title = (p_data[1] << 8) + p_data[2];
3344 sprintf( psz_str, " %d ---", i_title );
3345 result = N_("--- DVD Title");
3348 else */ if ( p_data[0] == MATROSKA_DVD_LEVEL_LU )
3351 sprintf( psz_str, " (%c%c) ---", p_data[1], p_data[2] );
3352 result = N_("--- DVD Menu");
3355 else if ( p_data[0] == MATROSKA_DVD_LEVEL_SS && f_for_title )
3357 if ( p_data[1] == 0x00 )
3358 result = N_("First Played");
3359 else if ( p_data[1] == 0xC0 )
3360 result = N_("Video Manager");
3361 else if ( p_data[1] == 0x80 )
3363 uint16_t i_title = (p_data[2] << 8) + p_data[3];
3365 sprintf( psz_str, " %d -----", i_title );
3366 result = N_("----- Title");
3375 int16 chapter_item_c::GetTitleNumber( ) const
3379 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3380 while ( index != codecs.end() )
3382 result = (*index)->GetTitleNumber( );
3391 int16 dvd_chapter_codec_c::GetTitleNumber()
3393 if ( p_private_data->GetSize() >= 3)
3395 const binary* p_data = p_private_data->GetBuffer();
3396 if ( p_data[0] == MATROSKA_DVD_LEVEL_SS )
3398 return int16( (p_data[2] << 8) + p_data[3] );
3404 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter )
3406 demux_sys_t *p_sys = p_demux->p_sys;
3407 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
3408 matroska_segment_c *p_segment = p_vsegment->Segment();
3409 mtime_t i_time_offset = 0;
3410 int64_t i_global_position = -1;
3414 msg_Dbg( p_demux, "seek request to %"PRId64" (%f%%)", i_date, f_percent );
3415 if( i_date < 0 && f_percent < 0 )
3417 msg_Warn( p_demux, "cannot seek nowhere !" );
3420 if( f_percent > 1.0 )
3422 msg_Warn( p_demux, "cannot seek so far !" );
3426 /* seek without index or without date */
3427 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
3429 if( p_sys->f_duration >= 0 && p_segment->b_cues )
3431 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
3435 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
3437 msg_Dbg( p_demux, "inaccurate way of seeking for pos:%"PRId64, i_pos );
3438 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
3440 if( p_segment->b_cues && p_segment->p_indexes[i_index].i_position < i_pos )
3442 if( !p_segment->b_cues && p_segment->p_indexes[i_index].i_position >= i_pos && p_segment->p_indexes[i_index].i_time > 0 )
3445 if( i_index == p_segment->i_index )
3450 i_date = p_segment->p_indexes[i_index].i_time;
3452 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 ))
3454 msg_Dbg( p_demux, "no cues, seek request to global pos: %"PRId64, i_pos );
3455 i_global_position = i_pos;
3460 p_vsegment->Seek( *p_demux, i_date, i_time_offset, psz_chapter, i_global_position );
3463 /*****************************************************************************
3464 * Demux: reads and demuxes data packets
3465 *****************************************************************************
3466 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
3467 *****************************************************************************/
3468 static int Demux( demux_t *p_demux)
3470 demux_sys_t *p_sys = p_demux->p_sys;
3472 vlc_mutex_lock( &p_sys->lock_demuxer );
3474 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
3475 matroska_segment_c *p_segment = p_vsegment->Segment();
3476 if ( p_segment == NULL ) return 0;
3477 int i_block_count = 0;
3482 if ( p_sys->demuxer.b_die )
3485 if( p_sys->i_pts >= p_sys->i_start_pts )
3486 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
3492 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
3494 /* nothing left to read in this ordered edition */
3495 if ( !p_vsegment->SelectNext() )
3497 p_segment->UnSelect( );
3499 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3501 /* switch to the next segment */
3502 p_segment = p_vsegment->Segment();
3503 if ( !p_segment->Select( 0 ) )
3505 msg_Err( p_demux, "Failed to select new segment" );
3512 KaxSimpleBlock *simpleblock;
3513 int64_t i_block_duration = 0;
3514 int64_t i_block_ref1;
3515 int64_t i_block_ref2;
3517 if( p_segment->BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
3519 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered )
3521 const chapter_item_c *p_chap = p_vsegment->CurrentChapter();
3522 // check if there are more chapters to read
3523 if ( p_chap != NULL )
3525 /* TODO handle successive chapters with the same user_start_time/user_end_time
3526 if ( p_chap->i_user_start_time == p_chap->i_user_start_time )
3527 p_vsegment->SelectNext();
3529 p_sys->i_pts = p_chap->i_user_end_time;
3530 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
3539 msg_Warn( p_demux, "cannot get block EOF?" );
3540 p_segment->UnSelect( );
3542 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3544 /* switch to the next segment */
3545 if ( !p_vsegment->SelectNext() )
3546 // no more segments in this stream
3548 p_segment = p_vsegment->Segment();
3549 if ( !p_segment->Select( 0 ) )
3551 msg_Err( p_demux, "Failed to select new segment" );
3559 if( simpleblock != NULL )
3560 p_sys->i_pts = (p_sys->i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
3562 p_sys->i_pts = (p_sys->i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
3564 if( p_sys->i_pts >= p_sys->i_start_pts )
3566 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
3568 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
3576 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
3578 /* nothing left to read in this ordered edition */
3579 if ( !p_vsegment->SelectNext() )
3584 p_segment->UnSelect( );
3586 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3588 /* switch to the next segment */
3589 p_segment = p_vsegment->Segment();
3590 if ( !p_segment->Select( 0 ) )
3592 msg_Err( p_demux, "Failed to select new segment" );
3600 BlockDecode( p_demux, block, simpleblock, p_sys->i_pts, i_block_duration, i_block_ref1 >= 0 || i_block_ref2 > 0 );
3605 // TODO optimize when there is need to leave or when seeking has been called
3606 if( i_block_count > 5 )
3613 vlc_mutex_unlock( &p_sys->lock_demuxer );
3620 /*****************************************************************************
3622 *****************************************************************************/
3623 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_, bool b_owner_ )
3630 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
3632 if( i_size <= 0 || mb_eof )
3637 return stream_Read( s, p_buffer, i_size );
3639 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
3645 case seek_beginning:
3649 i_pos = stream_Size( s ) - i_offset;
3652 i_pos= stream_Tell( s ) + i_offset;
3656 if( i_pos < 0 || i_pos >= stream_Size( s ) )
3663 if( stream_Seek( s, i_pos ) )
3669 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
3673 uint64 vlc_stream_io_callback::getFilePointer( void )
3677 return stream_Tell( s );
3679 void vlc_stream_io_callback::close( void )
3685 /*****************************************************************************
3686 * Ebml Stream parser
3687 *****************************************************************************/
3688 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux )
3695 mi_remain_size[0] = el_start->GetSize();
3697 for( i = 1; i < 6; i++ )
3704 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
3707 EbmlParser::~EbmlParser( void )
3711 for( i = 1; i < mi_level; i++ )
3721 EbmlElement* EbmlParser::UnGet( uint64 i_block_pos, uint64 i_cluster_pos )
3723 if ( mi_user_level > mi_level )
3725 while ( mi_user_level != mi_level )
3727 delete m_el[mi_user_level];
3728 m_el[mi_user_level] = NULL;
3734 if ( m_el[1]->GetElementPosition() == i_cluster_pos )
3736 m_es->I_O().setFilePointer( i_block_pos, seek_beginning );
3737 return (EbmlMaster*) m_el[1];
3741 // seek to the previous Cluster
3742 m_es->I_O().setFilePointer( i_cluster_pos, seek_beginning );
3745 delete m_el[mi_level];
3746 m_el[mi_level] = NULL;
3751 void EbmlParser::Up( void )
3753 if( mi_user_level == mi_level )
3755 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
3761 void EbmlParser::Down( void )
3767 void EbmlParser::Keep( void )
3772 int EbmlParser::GetLevel( void )
3774 return mi_user_level;
3777 void EbmlParser::Reset( demux_t *p_demux )
3779 while ( mi_level > 0)
3781 delete m_el[mi_level];
3782 m_el[mi_level] = NULL;
3785 mi_user_level = mi_level = 1;
3786 #if LIBEBML_VERSION >= 0x000704
3787 // a little faster and cleaner
3788 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
3790 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
3792 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
3795 /* This function workarounds a bug in KaxBlockVirtual implementation */
3796 class KaxBlockVirtualWorkaround : public KaxBlockVirtual
3801 if( Data == DataBlock )
3802 SetBuffer( NULL, 0 );
3806 EbmlElement *EbmlParser::Get( void )
3810 if( mi_user_level != mi_level )
3816 EbmlElement *ret = m_got;
3822 if( m_el[mi_level] )
3824 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
3827 if( MKV_IS_ID( m_el[mi_level], KaxBlockVirtual ) )
3828 static_cast<KaxBlockVirtualWorkaround*>(m_el[mi_level])->Fix();
3829 delete m_el[mi_level];
3834 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, mb_dummy != 0, 1 );
3835 // mi_remain_size[mi_level] = m_el[mi_level]->GetSize();
3846 delete m_el[mi_level - 1];
3847 m_got = m_el[mi_level -1] = m_el[mi_level];
3848 m_el[mi_level] = NULL;
3855 else if( m_el[mi_level] == NULL )
3857 fprintf( stderr," m_el[mi_level] == NULL\n" );
3860 return m_el[mi_level];
3863 bool EbmlParser::IsTopPresent( EbmlElement *el )
3865 for( int i = 0; i < mi_level; i++ )
3867 if( m_el[i] && m_el[i] == el )
3873 /*****************************************************************************
3875 * * LoadCues : load the cues element and update index
3877 * * LoadTags : load ... the tags element
3879 * * InformationCreate : create all information, load tags if present
3881 *****************************************************************************/
3882 void matroska_segment_c::LoadCues( KaxCues *cues )
3890 msg_Err( &sys.demuxer, "There can be only 1 Cues per section." );
3894 ep = new EbmlParser( &es, cues, &sys.demuxer );
3895 while( ( el = ep->Get() ) != NULL )
3897 if( MKV_IS_ID( el, KaxCuePoint ) )
3899 #define idx p_indexes[i_index]
3902 idx.i_block_number= -1;
3903 idx.i_position = -1;
3908 while( ( el = ep->Get() ) != NULL )
3910 if( MKV_IS_ID( el, KaxCueTime ) )
3912 KaxCueTime &ctime = *(KaxCueTime*)el;
3914 ctime.ReadData( es.I_O() );
3916 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
3918 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
3921 while( ( el = ep->Get() ) != NULL )
3923 if( MKV_IS_ID( el, KaxCueTrack ) )
3925 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
3927 ctrack.ReadData( es.I_O() );
3928 idx.i_track = uint16( ctrack );
3930 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
3932 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
3934 ccpos.ReadData( es.I_O() );
3935 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
3937 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
3939 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
3941 cbnum.ReadData( es.I_O() );
3942 idx.i_block_number = uint32( cbnum );
3946 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
3953 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
3959 msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
3960 " track=%d bnum=%d", idx.i_time, idx.i_position,
3961 idx.i_track, idx.i_block_number );
3965 if( i_index >= i_index_max )
3967 i_index_max += 1024;
3968 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
3974 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
3979 msg_Dbg( &sys.demuxer, "| - loading cues done." );
3982 void matroska_segment_c::LoadTags( KaxTags *tags )
3988 /* Master elements */
3989 ep = new EbmlParser( &es, tags, &sys.demuxer );
3991 while( ( el = ep->Get() ) != NULL )
3993 if( MKV_IS_ID( el, KaxTag ) )
3995 msg_Dbg( &sys.demuxer, "+ Tag" );
3997 while( ( el = ep->Get() ) != NULL )
3999 if( MKV_IS_ID( el, KaxTagTargets ) )
4001 msg_Dbg( &sys.demuxer, "| + Targets" );
4003 while( ( el = ep->Get() ) != NULL )
4005 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4009 else if( MKV_IS_ID( el, KaxTagGeneral ) )
4011 msg_Dbg( &sys.demuxer, "| + General" );
4013 while( ( el = ep->Get() ) != NULL )
4015 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4019 else if( MKV_IS_ID( el, KaxTagGenres ) )
4021 msg_Dbg( &sys.demuxer, "| + Genres" );
4023 while( ( el = ep->Get() ) != NULL )
4025 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4029 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
4031 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
4033 while( ( el = ep->Get() ) != NULL )
4035 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4039 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
4041 msg_Dbg( &sys.demuxer, "| + Images Specific" );
4043 while( ( el = ep->Get() ) != NULL )
4045 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4049 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
4051 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
4053 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
4055 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
4057 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
4059 msg_Dbg( &sys.demuxer, "| + Multi Date" );
4061 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
4063 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
4065 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
4067 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
4069 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
4071 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
4073 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
4075 msg_Dbg( &sys.demuxer, "| + Multi Title" );
4079 msg_Dbg( &sys.demuxer, "| + LoadTag Unknown (%s)", typeid( *el ).name() );
4086 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
4091 msg_Dbg( &sys.demuxer, "loading tags done." );
4094 /*****************************************************************************
4096 *****************************************************************************/
4097 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
4102 int i_upper_level = 0;
4107 stream_Control( sys.demuxer.s, STREAM_CAN_SEEK, &b_seekable );
4111 ep = new EbmlParser( &es, seekhead, &sys.demuxer );
4113 while( ( l = ep->Get() ) != NULL )
4115 if( MKV_IS_ID( l, KaxSeek ) )
4117 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
4120 msg_Dbg( &sys.demuxer, "| | + Seek" );
4122 while( ( l = ep->Get() ) != NULL )
4124 if( MKV_IS_ID( l, KaxSeekID ) )
4126 KaxSeekID &sid = *(KaxSeekID*)l;
4127 sid.ReadData( es.I_O() );
4128 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
4130 else if( MKV_IS_ID( l, KaxSeekPosition ) )
4132 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
4133 spos.ReadData( es.I_O() );
4134 i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) );
4138 /* Many mkvmerge files hit this case. It seems to be a broken SeekHead */
4139 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4146 if( id == KaxCues::ClassInfos.GlobalId )
4148 msg_Dbg( &sys.demuxer, "| - cues at %"PRId64, i_pos );
4149 LoadSeekHeadItem( KaxCues::ClassInfos, i_pos );
4151 else if( id == KaxInfo::ClassInfos.GlobalId )
4153 msg_Dbg( &sys.demuxer, "| - info at %"PRId64, i_pos );
4154 LoadSeekHeadItem( KaxInfo::ClassInfos, i_pos );
4156 else if( id == KaxChapters::ClassInfos.GlobalId )
4158 msg_Dbg( &sys.demuxer, "| - chapters at %"PRId64, i_pos );
4159 LoadSeekHeadItem( KaxChapters::ClassInfos, i_pos );
4161 else if( id == KaxTags::ClassInfos.GlobalId )
4163 msg_Dbg( &sys.demuxer, "| - tags at %"PRId64, i_pos );
4164 LoadSeekHeadItem( KaxTags::ClassInfos, i_pos );
4166 else if( id == KaxSeekHead::ClassInfos.GlobalId )
4168 msg_Dbg( &sys.demuxer, "| - chained seekhead at %"PRId64, i_pos );
4169 LoadSeekHeadItem( KaxSeekHead::ClassInfos, i_pos );
4171 else if( id == KaxTracks::ClassInfos.GlobalId )
4173 msg_Dbg( &sys.demuxer, "| - tracks at %"PRId64, i_pos );
4174 LoadSeekHeadItem( KaxTracks::ClassInfos, i_pos );
4176 else if( id == KaxAttachments::ClassInfos.GlobalId )
4178 msg_Dbg( &sys.demuxer, "| - attachments at %"PRId64, i_pos );
4179 LoadSeekHeadItem( KaxAttachments::ClassInfos, i_pos );
4182 msg_Dbg( &sys.demuxer, "| - unknown seekhead reference at %"PRId64, i_pos );
4186 msg_Dbg( &sys.demuxer, "| | + ParseSeekHead Unknown (%s)", typeid(*l).name() );
4191 /*****************************************************************************
4193 *****************************************************************************/
4194 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
4197 bool bSupported = true;
4201 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
4203 tk = new mkv_track_t();
4205 /* Init the track */
4206 memset( tk, 0, sizeof( mkv_track_t ) );
4208 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
4209 tk->fmt.psz_language = strdup("English");
4210 tk->fmt.psz_description = NULL;
4212 tk->b_default = true;
4213 tk->b_enabled = true;
4214 tk->b_silent = false;
4215 tk->i_number = tracks.size() - 1;
4216 tk->i_extra_data = 0;
4217 tk->p_extra_data = NULL;
4218 tk->psz_codec = NULL;
4219 tk->i_default_duration = 0;
4220 tk->f_timecodescale = 1.0;
4222 tk->b_inited = false;
4223 tk->i_data_init = 0;
4224 tk->p_data_init = NULL;
4226 tk->psz_codec_name = NULL;
4227 tk->psz_codec_settings = NULL;
4228 tk->psz_codec_info_url = NULL;
4229 tk->psz_codec_download_url = NULL;
4231 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
4232 tk->p_compression_data = NULL;
4234 for( i = 0; i < m->ListSize(); i++ )
4236 EbmlElement *l = (*m)[i];
4238 if( MKV_IS_ID( l, KaxTrackNumber ) )
4240 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
4242 tk->i_number = uint32( tnum );
4243 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
4245 else if( MKV_IS_ID( l, KaxTrackUID ) )
4247 KaxTrackUID &tuid = *(KaxTrackUID*)l;
4249 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
4251 else if( MKV_IS_ID( l, KaxTrackType ) )
4253 const char *psz_type;
4254 KaxTrackType &ttype = *(KaxTrackType*)l;
4256 switch( uint8(ttype) )
4260 tk->fmt.i_cat = AUDIO_ES;
4264 tk->fmt.i_cat = VIDEO_ES;
4266 case track_subtitle:
4267 psz_type = "subtitle";
4268 tk->fmt.i_cat = SPU_ES;
4271 psz_type = "buttons";
4272 tk->fmt.i_cat = SPU_ES;
4275 psz_type = "unknown";
4276 tk->fmt.i_cat = UNKNOWN_ES;
4280 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
4282 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
4284 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
4286 // tk->b_enabled = uint32( fenb );
4287 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
4288 // uint32( fenb ) );
4290 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
4292 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
4294 tk->b_default = uint32( fdef );
4295 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
4297 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
4299 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
4301 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
4303 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
4305 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
4307 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
4309 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
4311 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
4313 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
4315 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
4317 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
4319 tk->i_default_duration = uint64(defd);
4320 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%"PRId64, uint64(defd) );
4322 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
4324 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
4326 tk->f_timecodescale = float( ttcs );
4327 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
4329 else if( MKV_IS_ID( l, KaxTrackName ) )
4331 KaxTrackName &tname = *(KaxTrackName*)l;
4333 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
4334 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
4336 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
4338 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
4340 if ( tk->fmt.psz_language != NULL )
4341 free( tk->fmt.psz_language );
4342 tk->fmt.psz_language = strdup( string( lang ).c_str() );
4343 msg_Dbg( &sys.demuxer,
4344 "| | | + Track Language=`%s'", tk->fmt.psz_language );
4346 else if( MKV_IS_ID( l, KaxCodecID ) )
4348 KaxCodecID &codecid = *(KaxCodecID*)l;
4350 tk->psz_codec = strdup( string( codecid ).c_str() );
4351 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
4353 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
4355 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
4357 tk->i_extra_data = cpriv.GetSize();
4358 if( tk->i_extra_data > 0 )
4360 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
4361 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
4363 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
4365 else if( MKV_IS_ID( l, KaxCodecName ) )
4367 KaxCodecName &cname = *(KaxCodecName*)l;
4369 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
4370 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
4372 else if( MKV_IS_ID( l, KaxContentEncodings ) )
4374 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
4375 MkvTree( sys.demuxer, 3, "Content Encodings" );
4376 if ( cencs->ListSize() > 1 )
4378 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
4381 for( j = 0; j < cencs->ListSize(); j++ )
4383 EbmlElement *l2 = (*cencs)[j];
4384 if( MKV_IS_ID( l2, KaxContentEncoding ) )
4386 MkvTree( sys.demuxer, 4, "Content Encoding" );
4387 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
4388 for( k = 0; k < cenc->ListSize(); k++ )
4390 EbmlElement *l3 = (*cenc)[k];
4391 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
4393 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
4394 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
4396 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
4398 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
4399 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
4401 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
4403 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
4404 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
4406 else if( MKV_IS_ID( l3, KaxContentCompression ) )
4408 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
4409 MkvTree( sys.demuxer, 5, "Content Compression" );
4410 for( n = 0; n < compr->ListSize(); n++ )
4412 EbmlElement *l4 = (*compr)[n];
4413 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
4415 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
4416 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
4417 tk->i_compression_type = uint32( compalg );
4418 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
4419 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
4421 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
4425 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
4427 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
4431 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
4437 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
4443 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
4447 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
4449 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
4451 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
4452 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
4454 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
4456 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
4458 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
4459 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
4461 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
4463 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
4465 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
4466 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
4468 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
4470 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
4472 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
4474 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
4476 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
4478 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
4480 else if( MKV_IS_ID( l, KaxTrackVideo ) )
4482 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
4484 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
4485 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
4487 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
4490 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
4491 tk->fmt.video.i_frame_rate = 1000000;
4493 for( j = 0; j < tkv->ListSize(); j++ )
4495 EbmlElement *l = (*tkv)[j];
4496 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
4498 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
4500 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
4502 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
4504 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
4506 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
4509 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
4511 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
4513 tk->fmt.video.i_width += uint16( vwidth );
4514 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
4516 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
4518 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
4520 tk->fmt.video.i_height += uint16( vheight );
4521 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
4523 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
4525 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
4527 i_display_width = uint16( vwidth );
4528 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
4530 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
4532 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
4534 i_display_height = uint16( vheight );
4535 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
4537 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
4539 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
4541 i_crop_bottom = uint16( cropval );
4542 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
4544 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
4546 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
4548 i_crop_top = uint16( cropval );
4549 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
4551 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
4553 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
4555 i_crop_right = uint16( cropval );
4556 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
4558 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
4560 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
4562 i_crop_left = uint16( cropval );
4563 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
4565 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
4567 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
4569 tk->f_fps = float( vfps );
4570 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
4572 else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
4574 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
4576 i_display_unit = uint8( vdmode );
4577 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
4578 uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
4580 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
4582 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
4584 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
4586 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
4588 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
4590 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
4594 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
4597 if( i_display_height && i_display_width )
4598 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * i_display_width / i_display_height;
4599 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
4601 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
4602 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
4603 tk->fmt.video.i_x_offset = i_crop_left;
4604 tk->fmt.video.i_y_offset = i_crop_top;
4605 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
4606 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
4608 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
4609 we do not support this atm */
4611 else if( MKV_IS_ID( l, KaxTrackAudio ) )
4613 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
4616 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
4618 for( j = 0; j < tka->ListSize(); j++ )
4620 EbmlElement *l = (*tka)[j];
4622 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
4624 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
4626 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
4627 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
4629 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
4631 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
4633 tk->fmt.audio.i_rate = (int)float( afreq );
4634 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
4636 else if( MKV_IS_ID( l, KaxAudioChannels ) )
4638 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
4640 tk->fmt.audio.i_channels = uint8( achan );
4641 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
4643 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
4645 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
4647 tk->fmt.audio.i_bitspersample = uint8( abits );
4648 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
4652 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
4658 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
4659 typeid(*l).name() );
4665 tracks.push_back( tk );
4669 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
4674 /*****************************************************************************
4676 *****************************************************************************/
4677 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
4681 int i_upper_level = 0;
4683 /* Master elements */
4684 tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
4686 for( i = 0; i < tracks->ListSize(); i++ )
4688 EbmlElement *l = (*tracks)[i];
4690 if( MKV_IS_ID( l, KaxTrackEntry ) )
4692 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
4696 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4701 /*****************************************************************************
4703 *****************************************************************************/
4704 void matroska_segment_c::ParseInfo( KaxInfo *info )
4709 int i_upper_level = 0;
4711 /* Master elements */
4712 m = static_cast<EbmlMaster *>(info);
4713 m->Read( es, info->Generic().Context, i_upper_level, el, true );
4715 for( i = 0; i < m->ListSize(); i++ )
4717 EbmlElement *l = (*m)[i];
4719 if( MKV_IS_ID( l, KaxSegmentUID ) )
4721 if ( p_segment_uid == NULL )
4722 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
4724 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
4726 else if( MKV_IS_ID( l, KaxPrevUID ) )
4728 if ( p_prev_segment_uid == NULL )
4729 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
4731 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
4733 else if( MKV_IS_ID( l, KaxNextUID ) )
4735 if ( p_next_segment_uid == NULL )
4736 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
4738 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
4740 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
4742 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
4744 i_timescale = uint64(tcs);
4746 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%"PRId64,
4749 else if( MKV_IS_ID( l, KaxDuration ) )
4751 KaxDuration &dur = *(KaxDuration*)l;
4753 i_duration = mtime_t( double( dur ) );
4755 msg_Dbg( &sys.demuxer, "| | + Duration=%"PRId64,
4758 else if( MKV_IS_ID( l, KaxMuxingApp ) )
4760 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
4762 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
4764 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
4765 psz_muxing_application );
4767 else if( MKV_IS_ID( l, KaxWritingApp ) )
4769 KaxWritingApp &wapp = *(KaxWritingApp*)l;
4771 psz_writing_application = ToUTF8( UTFstring( wapp ) );
4773 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
4774 psz_writing_application );
4776 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
4778 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
4780 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
4782 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
4783 psz_segment_filename );
4785 else if( MKV_IS_ID( l, KaxTitle ) )
4787 KaxTitle &title = *(KaxTitle*)l;
4789 psz_title = ToUTF8( UTFstring( title ) );
4791 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
4793 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
4795 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
4797 families.push_back( new KaxSegmentFamily(*uid) );
4799 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
4801 #if defined( HAVE_GMTIME_R )
4802 else if( MKV_IS_ID( l, KaxDateUTC ) )
4804 KaxDateUTC &date = *(KaxDateUTC*)l;
4809 i_date = date.GetEpochDate();
4810 memset( buffer, 0, 256 );
4811 if( gmtime_r( &i_date, &tmres ) &&
4812 asctime_r( &tmres, buffer ) )
4814 buffer[strlen( buffer)-1]= '\0';
4815 psz_date_utc = strdup( buffer );
4816 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
4820 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
4822 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
4823 chapter_translation_c *p_translate = new chapter_translation_c();
4825 p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
4826 for( j = 0; j < p_trans->ListSize(); j++ )
4828 EbmlElement *l = (*p_trans)[j];
4830 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
4832 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
4834 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
4836 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
4838 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
4840 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
4844 translations.push_back( p_translate );
4848 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4852 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
4853 i_duration = mtime_t(f_dur);
4857 /*****************************************************************************
4859 *****************************************************************************/
4860 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
4864 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
4865 for( i = 0; i < ca->ListSize(); i++ )
4867 EbmlElement *l = (*ca)[i];
4869 if( MKV_IS_ID( l, KaxChapterUID ) )
4871 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
4872 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
4874 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
4876 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
4877 chapters.b_display_seekpoint = uint8( flag ) == 0;
4879 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
4881 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
4883 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
4884 chapters.i_start_time = uint64( start ) / INT64_C(1000);
4886 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
4888 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
4890 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
4891 chapters.i_end_time = uint64( end ) / INT64_C(1000);
4893 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
4895 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
4897 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
4899 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
4900 for( j = 0; j < cd->ListSize(); j++ )
4902 EbmlElement *l= (*cd)[j];
4904 if( MKV_IS_ID( l, KaxChapterString ) )
4908 KaxChapterString &name =*(KaxChapterString*)l;
4909 for (k = 0; k < i_level; k++)
4910 chapters.psz_name += '+';
4911 chapters.psz_name += ' ';
4912 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
4913 chapters.psz_name += psz_tmp_utf8;
4914 chapters.b_user_display = true;
4916 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
4917 free( psz_tmp_utf8 );
4919 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
4921 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
4922 const char *psz = string( lang ).c_str();
4924 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
4926 else if( MKV_IS_ID( l, KaxChapterCountry ) )
4928 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
4929 const char *psz = string( ct ).c_str();
4931 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
4935 else if( MKV_IS_ID( l, KaxChapterProcess ) )
4937 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
4939 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
4940 chapter_codec_cmds_c *p_ccodec = NULL;
4942 for( j = 0; j < cp->ListSize(); j++ )
4944 EbmlElement *k= (*cp)[j];
4946 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
4948 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
4949 if ( uint32(*p_codec_id) == 0 )
4950 p_ccodec = new matroska_script_codec_c( sys );
4951 else if ( uint32(*p_codec_id) == 1 )
4952 p_ccodec = new dvd_chapter_codec_c( sys );
4957 if ( p_ccodec != NULL )
4959 for( j = 0; j < cp->ListSize(); j++ )
4961 EbmlElement *k= (*cp)[j];
4963 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
4965 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
4966 p_ccodec->SetPrivate( *p_private );
4968 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
4970 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
4973 chapters.codecs.push_back( p_ccodec );
4976 else if( MKV_IS_ID( l, KaxChapterAtom ) )
4978 chapter_item_c *new_sub_chapter = new chapter_item_c();
4979 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
4980 new_sub_chapter->psz_parent = &chapters;
4981 chapters.sub_chapters.push_back( new_sub_chapter );
4986 /*****************************************************************************
4988 *****************************************************************************/
4989 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
4992 int i_upper_level = 0;
4994 attachments->Read( es, attachments->Generic().Context, i_upper_level, el, true );
4996 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
4998 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
5000 std::string psz_mime_type = GetChild<KaxMimeType>( *attachedFile );
5001 KaxFileName &file_name = GetChild<KaxFileName>( *attachedFile );
5002 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
5004 attachment_c *new_attachment = new attachment_c();
5006 if( new_attachment )
5008 new_attachment->psz_file_name = ToUTF8( UTFstring( file_name ) );
5009 new_attachment->psz_mime_type = psz_mime_type;
5010 new_attachment->i_size = img_data.GetSize();
5011 new_attachment->p_data = malloc( img_data.GetSize() );
5013 if( new_attachment->p_data )
5015 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
5016 sys.stored_attachments.push_back( new_attachment );
5020 delete new_attachment;
5024 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
5028 /*****************************************************************************
5030 *****************************************************************************/
5031 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
5035 int i_upper_level = 0;
5038 /* Master elements */
5039 chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
5041 for( i = 0; i < chapters->ListSize(); i++ )
5043 EbmlElement *l = (*chapters)[i];
5045 if( MKV_IS_ID( l, KaxEditionEntry ) )
5047 chapter_edition_c *p_edition = new chapter_edition_c();
5049 EbmlMaster *E = static_cast<EbmlMaster *>(l );
5051 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
5052 for( j = 0; j < E->ListSize(); j++ )
5054 EbmlElement *l = (*E)[j];
5056 if( MKV_IS_ID( l, KaxChapterAtom ) )
5058 chapter_item_c *new_sub_chapter = new chapter_item_c();
5059 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
5060 p_edition->sub_chapters.push_back( new_sub_chapter );
5062 else if( MKV_IS_ID( l, KaxEditionUID ) )
5064 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
5066 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
5068 p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
5070 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
5072 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
5073 i_default_edition = stored_editions.size();
5077 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
5080 stored_editions.push_back( p_edition );
5084 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
5088 for( i = 0; i < stored_editions.size(); i++ )
5090 stored_editions[i]->RefreshChapters( );
5093 if ( stored_editions.size() != 0 && stored_editions[i_default_edition]->b_ordered )
5095 /* update the duration of the segment according to the sum of all sub chapters */
5096 i_dur = stored_editions[i_default_edition]->Duration() / INT64_C(1000);
5102 void matroska_segment_c::ParseCluster( )
5107 int i_upper_level = 0;
5109 /* Master elements */
5110 m = static_cast<EbmlMaster *>( cluster );
5111 m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
5113 for( i = 0; i < m->ListSize(); i++ )
5115 EbmlElement *l = (*m)[i];
5117 if( MKV_IS_ID( l, KaxClusterTimecode ) )
5119 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
5121 cluster->InitTimecode( uint64( ctc ), i_timescale );
5126 i_start_time = cluster->GlobalTimecode() / 1000;
5129 /*****************************************************************************
5130 * InformationCreate:
5131 *****************************************************************************/
5132 void matroska_segment_c::InformationCreate( )
5134 sys.meta = vlc_meta_New();
5138 vlc_meta_SetTitle( sys.meta, psz_title );
5142 vlc_meta_SetDate( sys.meta, psz_date_utc );
5145 if( psz_segment_filename )
5147 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5149 if( psz_muxing_application )
5151 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5153 if( psz_writing_application )
5155 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5158 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
5160 // mkv_track_t *tk = tracks[i_track];
5161 // vlc_meta_t *mtk = vlc_meta_New();
5162 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
5166 if( i_tags_position >= 0 )
5170 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
5180 /*****************************************************************************
5182 *****************************************************************************/
5184 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
5186 #define idx p_indexes[i_index]
5188 idx.i_block_number= -1;
5189 idx.i_position = cluster->GetElementPosition();
5194 if( i_index >= i_index_max )
5196 i_index_max += 1024;
5197 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
5202 void chapter_edition_c::RefreshChapters( )
5204 chapter_item_c::RefreshChapters( b_ordered, -1 );
5205 b_display_seekpoint = false;
5208 int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_time )
5210 int64_t i_user_time = i_prev_user_time;
5212 // first the sub-chapters, and then ourself
5213 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
5214 while ( index != sub_chapters.end() )
5216 i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
5222 // the ordered chapters always start at zero
5223 if ( i_prev_user_time == -1 )
5225 if ( i_user_time == -1 )
5227 i_prev_user_time = 0;
5230 i_user_start_time = i_prev_user_time;
5231 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
5233 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
5237 i_user_end_time = i_user_time;
5242 if ( sub_chapters.begin() != sub_chapters.end() )
5243 std::sort( sub_chapters.begin(), sub_chapters.end(), chapter_item_c::CompareTimecode );
5244 i_user_start_time = i_start_time;
5245 if ( i_end_time != -1 )
5246 i_user_end_time = i_end_time;
5247 else if ( i_user_time != -1 )
5248 i_user_end_time = i_user_time;
5250 i_user_end_time = i_user_start_time;
5253 return i_user_end_time;
5256 mtime_t chapter_edition_c::Duration() const
5258 mtime_t i_result = 0;
5260 if ( sub_chapters.size() )
5262 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
5264 i_result = (*index)->i_user_end_time;
5270 chapter_item_c * chapter_edition_c::FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current )
5274 bool b_found_current = false;
5275 return chapter_item_c::FindTimecode( i_timecode, p_current, b_found_current );
5278 chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode, const chapter_item_c * p_current, bool & b_found )
5280 chapter_item_c *psz_result = NULL;
5282 if ( p_current == this )
5285 if ( i_user_timecode >= i_user_start_time &&
5286 ( i_user_timecode < i_user_end_time ||
5287 ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
5289 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
5290 while ( index != sub_chapters.end() && ((p_current == NULL && psz_result == NULL) || (p_current != NULL && (!b_found || psz_result == NULL))))
5292 psz_result = (*index)->FindTimecode( i_user_timecode, p_current, b_found );
5296 if ( psz_result == NULL )
5303 bool chapter_item_c::ParentOf( const chapter_item_c & item ) const
5305 if ( &item == this )
5308 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.begin();
5309 while ( index != sub_chapters.end() )
5311 if ( (*index)->ParentOf( item ) )
5319 void demux_sys_t::PreloadFamily( const matroska_segment_c & of_segment )
5321 for (size_t i=0; i<opened_segments.size(); i++)
5323 opened_segments[i]->PreloadFamily( of_segment );
5326 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
5331 for (size_t i=0; i<families.size(); i++)
5333 for (size_t j=0; j<of_segment.families.size(); j++)
5335 if ( *(families[i]) == *(of_segment.families[j]) )
5343 // preload all the linked segments for all preloaded segments
5344 void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
5346 size_t i_preloaded, i, j;
5347 virtual_segment_c *p_seg;
5349 p_current_segment = VirtualFromSegments( p_segment );
5351 used_segments.push_back( p_current_segment );
5353 // create all the other virtual segments of the family
5356 for ( i=0; i< opened_segments.size(); i++ )
5358 if ( opened_segments[i]->b_preloaded && !IsUsedSegment( *opened_segments[i] ) )
5360 p_seg = VirtualFromSegments( opened_segments[i] );
5361 used_segments.push_back( p_seg );
5365 } while ( i_preloaded ); // worst case: will stop when all segments are found as family related
5367 // publish all editions of all usable segment
5368 for ( i=0; i< used_segments.size(); i++ )
5370 p_seg = used_segments[i];
5371 if ( p_seg->p_editions != NULL )
5373 std::string sz_name;
5374 input_title_t *p_title = vlc_input_title_New();
5375 p_seg->i_sys_title = i;
5378 // TODO use a name for each edition, let the TITLE deal with a codec name
5379 for ( j=0; j<p_seg->p_editions->size(); j++ )
5381 if ( p_title->psz_name == NULL )
5383 sz_name = (*p_seg->p_editions)[j]->GetMainName();
5384 if ( sz_name != "" )
5385 p_title->psz_name = strdup( sz_name.c_str() );
5388 chapter_edition_c *p_edition = (*p_seg->p_editions)[j];
5391 p_edition->PublishChapters( *p_title, i_chapters, 0 );
5394 // create a name if there is none
5395 if ( p_title->psz_name == NULL )
5397 sz_name = N_("Segment");
5399 sprintf( psz_str, " %d", (int)i );
5401 p_title->psz_name = strdup( sz_name.c_str() );
5404 titles.push_back( p_title );
5408 // TODO decide which segment should be first used (VMG for DVD)
5411 bool demux_sys_t::IsUsedSegment( matroska_segment_c &segment ) const
5413 for ( size_t i=0; i< used_segments.size(); i++ )
5415 if ( used_segments[i]->FindUID( *segment.p_segment_uid ) )
5421 virtual_segment_c *demux_sys_t::VirtualFromSegments( matroska_segment_c *p_segment ) const
5423 size_t i_preloaded, i;
5425 virtual_segment_c *p_result = new virtual_segment_c( p_segment );
5427 // fill our current virtual segment with all hard linked segments
5430 for ( i=0; i< opened_segments.size(); i++ )
5432 i_preloaded += p_result->AddSegment( opened_segments[i] );
5434 } while ( i_preloaded ); // worst case: will stop when all segments are found as linked
5438 p_result->PreloadLinked( );
5440 p_result->PrepareChapters( );
5445 bool demux_sys_t::PreparePlayback( virtual_segment_c *p_new_segment )
5447 if ( p_new_segment != NULL && p_new_segment != p_current_segment )
5449 if ( p_current_segment != NULL && p_current_segment->Segment() != NULL )
5450 p_current_segment->Segment()->UnSelect();
5452 p_current_segment = p_new_segment;
5453 i_current_title = p_new_segment->i_sys_title;
5455 if( !p_current_segment->Segment()->b_cues )
5456 msg_Warn( &p_current_segment->Segment()->sys.demuxer, "no cues/empty cues found->seek won't be precise" );
5458 f_duration = p_current_segment->Duration();
5460 /* add information */
5461 p_current_segment->Segment()->InformationCreate( );
5462 p_current_segment->Segment()->Select( 0 );
5467 void demux_sys_t::JumpTo( virtual_segment_c & vsegment, chapter_item_c * p_chapter )
5469 // if the segment is not part of the current segment, select the new one
5470 if ( &vsegment != p_current_segment )
5472 PreparePlayback( &vsegment );
5475 if ( p_chapter != NULL )
5477 if ( !p_chapter->Enter( true ) )
5479 // jump to the location in the found segment
5480 vsegment.Seek( demuxer, p_chapter->i_user_start_time, -1, p_chapter, -1 );
5486 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
5490 if ( p_item_a == NULL || p_item_b == NULL )
5493 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
5494 if ( p_item_b->p_prev_segment_uid != NULL
5495 && *p_tmp == *p_item_b->p_prev_segment_uid )
5498 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
5502 if ( p_item_b->p_segment_uid != NULL
5503 && *p_tmp == *p_item_b->p_segment_uid )
5506 if ( p_item_b->p_prev_segment_uid != NULL
5507 && *p_tmp == *p_item_b->p_prev_segment_uid )
5513 bool matroska_segment_c::Preload( )
5518 EbmlElement *el = NULL;
5520 ep->Reset( &sys.demuxer );
5522 while( ( el = ep->Get() ) != NULL )
5524 if( MKV_IS_ID( el, KaxSeekHead ) )
5526 /* Multiple allowed */
5527 /* We bail at 10, to prevent possible recursion */
5528 msg_Dbg( &sys.demuxer, "| + Seek head" );
5529 if( i_seekhead_count < 10 )
5531 i_seekhead_position = (int64_t) es.I_O().getFilePointer();
5532 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
5535 else if( MKV_IS_ID( el, KaxInfo ) )
5537 /* Multiple allowed, mandatory */
5538 msg_Dbg( &sys.demuxer, "| + Information" );
5539 if( i_info_position < 0 ) // FIXME
5540 ParseInfo( static_cast<KaxInfo*>( el ) );
5541 i_info_position = (int64_t) es.I_O().getFilePointer();
5543 else if( MKV_IS_ID( el, KaxTracks ) )
5545 /* Multiple allowed */
5546 msg_Dbg( &sys.demuxer, "| + Tracks" );
5547 if( i_tracks_position < 0 ) // FIXME
5548 ParseTracks( static_cast<KaxTracks*>( el ) );
5549 if ( tracks.size() == 0 )
5551 msg_Err( &sys.demuxer, "No tracks supported" );
5554 i_tracks_position = (int64_t) es.I_O().getFilePointer();
5556 else if( MKV_IS_ID( el, KaxCues ) )
5558 msg_Dbg( &sys.demuxer, "| + Cues" );
5559 if( i_cues_position < 0 )
5560 LoadCues( static_cast<KaxCues*>( el ) );
5561 i_cues_position = (int64_t) es.I_O().getFilePointer();
5563 else if( MKV_IS_ID( el, KaxCluster ) )
5565 msg_Dbg( &sys.demuxer, "| + Cluster" );
5567 cluster = (KaxCluster*)el;
5569 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
5573 /* stop pre-parsing the stream */
5576 else if( MKV_IS_ID( el, KaxAttachments ) )
5578 msg_Dbg( &sys.demuxer, "| + Attachments" );
5579 if( i_attachments_position < 0 )
5580 ParseAttachments( static_cast<KaxAttachments*>( el ) );
5581 i_attachments_position = (int64_t) es.I_O().getFilePointer();
5583 else if( MKV_IS_ID( el, KaxChapters ) )
5585 msg_Dbg( &sys.demuxer, "| + Chapters" );
5586 if( i_chapters_position < 0 )
5587 ParseChapters( static_cast<KaxChapters*>( el ) );
5588 i_chapters_position = (int64_t) es.I_O().getFilePointer();
5590 else if( MKV_IS_ID( el, KaxTag ) )
5592 msg_Dbg( &sys.demuxer, "| + Tags" );
5593 if( i_tags_position < 0) // FIXME
5594 ;//LoadTags( static_cast<KaxTags*>( el ) );
5595 i_tags_position = (int64_t) es.I_O().getFilePointer();
5598 msg_Dbg( &sys.demuxer, "| + Preload Unknown (%s)", typeid(*el).name() );
5606 /* Here we try to load elements that were found in Seek Heads, but not yet parsed */
5607 bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int64_t i_element_position )
5609 int64_t i_sav_position = (int64_t)es.I_O().getFilePointer();
5612 es.I_O().setFilePointer( i_element_position, seek_beginning );
5613 el = es.FindNextID( ClassInfos, 0xFFFFFFFFL);
5617 msg_Err( &sys.demuxer, "cannot load some cues/chapters/tags etc. (broken seekhead or file)" );
5618 es.I_O().setFilePointer( i_sav_position, seek_beginning );
5622 if( MKV_IS_ID( el, KaxSeekHead ) )
5624 /* Multiple allowed */
5625 msg_Dbg( &sys.demuxer, "| + Seek head" );
5626 if( i_seekhead_count < 10 )
5628 i_seekhead_position = i_element_position;
5629 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
5632 else if( MKV_IS_ID( el, KaxInfo ) ) // FIXME
5634 /* Multiple allowed, mandatory */
5635 msg_Dbg( &sys.demuxer, "| + Information" );
5636 if( i_info_position < 0 )
5637 ParseInfo( static_cast<KaxInfo*>( el ) );
5638 i_info_position = i_element_position;
5640 else if( MKV_IS_ID( el, KaxTracks ) ) // FIXME
5642 /* Multiple allowed */
5643 msg_Dbg( &sys.demuxer, "| + Tracks" );
5644 if( i_tracks_position < 0 )
5645 ParseTracks( static_cast<KaxTracks*>( el ) );
5646 if ( tracks.size() == 0 )
5648 msg_Err( &sys.demuxer, "No tracks supported" );
5650 es.I_O().setFilePointer( i_sav_position, seek_beginning );
5653 i_tracks_position = i_element_position;
5655 else if( MKV_IS_ID( el, KaxCues ) )
5657 msg_Dbg( &sys.demuxer, "| + Cues" );
5658 if( i_cues_position < 0 )
5659 LoadCues( static_cast<KaxCues*>( el ) );
5660 i_cues_position = i_element_position;
5662 else if( MKV_IS_ID( el, KaxAttachments ) )
5664 msg_Dbg( &sys.demuxer, "| + Attachments" );
5665 if( i_attachments_position < 0 )
5666 ParseAttachments( static_cast<KaxAttachments*>( el ) );
5667 i_attachments_position = i_element_position;
5669 else if( MKV_IS_ID( el, KaxChapters ) )
5671 msg_Dbg( &sys.demuxer, "| + Chapters" );
5672 if( i_chapters_position < 0 )
5673 ParseChapters( static_cast<KaxChapters*>( el ) );
5674 i_chapters_position = i_element_position;
5676 else if( MKV_IS_ID( el, KaxTag ) ) // FIXME
5678 msg_Dbg( &sys.demuxer, "| + Tags" );
5679 if( i_tags_position < 0 )
5680 ;//LoadTags( static_cast<KaxTags*>( el ) );
5681 i_tags_position = i_element_position;
5685 msg_Dbg( &sys.demuxer, "| + LoadSeekHeadItem Unknown (%s)", typeid(*el).name() );
5689 es.I_O().setFilePointer( i_sav_position, seek_beginning );
5693 matroska_segment_c *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
5695 for (size_t i=0; i<opened_segments.size(); i++)
5697 if ( *opened_segments[i]->p_segment_uid == uid )
5698 return opened_segments[i];
5703 chapter_item_c *demux_sys_t::BrowseCodecPrivate( unsigned int codec_id,
5704 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
5705 const void *p_cookie,
5706 size_t i_cookie_size,
5707 virtual_segment_c * &p_segment_found )
5709 chapter_item_c *p_result = NULL;
5710 for (size_t i=0; i<used_segments.size(); i++)
5712 p_result = used_segments[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
5713 if ( p_result != NULL )
5715 p_segment_found = used_segments[i];
5722 chapter_item_c *demux_sys_t::FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found )
5724 chapter_item_c *p_result = NULL;
5725 for (size_t i=0; i<used_segments.size(); i++)
5727 p_result = used_segments[i]->FindChapter( i_find_uid );
5728 if ( p_result != NULL )
5730 p_segment_found = used_segments[i];
5737 void virtual_segment_c::Sort()
5739 // keep the current segment index
5740 matroska_segment_c *p_segment = linked_segments[i_current_segment];
5742 std::sort( linked_segments.begin(), linked_segments.end(), matroska_segment_c::CompareSegmentUIDs );
5744 for ( i_current_segment=0; i_current_segment<linked_segments.size(); i_current_segment++)
5745 if ( linked_segments[i_current_segment] == p_segment )
5749 size_t virtual_segment_c::AddSegment( matroska_segment_c *p_segment )
5752 // check if it's not already in here
5753 for ( i=0; i<linked_segments.size(); i++ )
5755 if ( linked_segments[i]->p_segment_uid != NULL
5756 && p_segment->p_segment_uid != NULL
5757 && *p_segment->p_segment_uid == *linked_segments[i]->p_segment_uid )
5761 // find possible mates
5762 for ( i=0; i<linked_uids.size(); i++ )
5764 if ( (p_segment->p_segment_uid != NULL && *p_segment->p_segment_uid == linked_uids[i])
5765 || (p_segment->p_prev_segment_uid != NULL && *p_segment->p_prev_segment_uid == linked_uids[i])
5766 || (p_segment->p_next_segment_uid !=NULL && *p_segment->p_next_segment_uid == linked_uids[i]) )
5768 linked_segments.push_back( p_segment );
5770 AppendUID( p_segment->p_prev_segment_uid );
5771 AppendUID( p_segment->p_next_segment_uid );
5779 void virtual_segment_c::PreloadLinked( )
5781 for ( size_t i=0; i<linked_segments.size(); i++ )
5783 linked_segments[i]->Preload( );
5785 i_current_edition = linked_segments[0]->i_default_edition;
5788 mtime_t virtual_segment_c::Duration() const
5791 if ( linked_segments.size() == 0 )
5794 matroska_segment_c *p_last_segment = linked_segments[linked_segments.size()-1];
5795 // p_last_segment->ParseCluster( );
5797 i_duration = p_last_segment->i_start_time / 1000 + p_last_segment->i_duration;
5802 void virtual_segment_c::AppendUID( const EbmlBinary * p_UID )
5804 if ( p_UID == NULL )
5806 if ( p_UID->GetBuffer() == NULL )
5809 for (size_t i=0; i<linked_uids.size(); i++)
5811 if ( *p_UID == linked_uids[i] )
5814 linked_uids.push_back( *(KaxSegmentUID*)(p_UID) );
5817 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_global_position )
5820 KaxSimpleBlock *simpleblock;
5821 int i_track_skipping;
5822 int64_t i_block_duration;
5823 int64_t i_block_ref1;
5824 int64_t i_block_ref2;
5826 int64_t i_seek_position = i_start_pos;
5827 int64_t i_seek_time = i_start_time;
5829 if( i_global_position >= 0 )
5831 /* Special case for seeking in files with no cues */
5832 EbmlElement *el = NULL;
5833 es.I_O().setFilePointer( i_start_pos, seek_beginning );
5835 ep = new EbmlParser( &es, segment, &sys.demuxer );
5838 while( ( el = ep->Get() ) != NULL )
5840 if( MKV_IS_ID( el, KaxCluster ) )
5842 cluster = (KaxCluster *)el;
5843 i_cluster_pos = cluster->GetElementPosition();
5845 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
5847 IndexAppendCluster( cluster );
5849 if( es.I_O().getFilePointer() >= i_global_position )
5852 msg_Dbg( &sys.demuxer, "we found a cluster that is in the neighbourhood" );
5853 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
5858 msg_Err( &sys.demuxer, "This file has no cues, and we were unable to seek to the requested position by parsing." );
5866 for( ; i_idx < i_index; i_idx++ )
5868 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
5879 i_seek_position = p_indexes[i_idx].i_position;
5880 i_seek_time = p_indexes[i_idx].i_time;
5883 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
5884 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
5886 es.I_O().setFilePointer( i_seek_position, seek_beginning );
5889 ep = new EbmlParser( &es, segment, &sys.demuxer );
5892 sys.i_start_pts = i_date;
5894 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
5896 /* now parse until key frame */
5897 i_track_skipping = 0;
5898 for( i_track = 0; i_track < tracks.size(); i_track++ )
5900 if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
5902 tracks[i_track]->b_search_keyframe = true;
5905 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, i_date );
5908 while( i_track_skipping > 0 )
5910 if( BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
5912 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
5918 for( i_track = 0; i_track < tracks.size(); i_track++ )
5920 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
5921 (block && tracks[i_track]->i_number == block->TrackNum()) )
5928 sys.i_pts = (sys.i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
5930 sys.i_pts = (sys.i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
5932 if( i_track < tracks.size() )
5934 if( sys.i_pts >= sys.i_start_pts )
5936 cluster = static_cast<KaxCluster*>(ep->UnGet( i_block_pos, i_cluster_pos ));
5937 i_track_skipping = 0;
5939 else if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
5941 if( i_block_ref1 == 0 && tracks[i_track]->b_search_keyframe )
5943 tracks[i_track]->b_search_keyframe = false;
5946 if( !tracks[i_track]->b_search_keyframe )
5949 //es_out_Control( sys.demuxer.out, ES_OUT_SET_PCR, sys.i_pts );
5950 BlockDecode( &sys.demuxer, block, simpleblock, sys.i_pts, 0, i_block_ref1 >= 0 || i_block_ref2 > 0 );
5958 /* FIXME current ES_OUT_SET_NEXT_DISPLAY_TIME does not work that well if
5959 * the delay is too high. */
5960 if( sys.i_pts + 500*1000 < sys.i_start_pts )
5962 sys.i_start_pts = sys.i_pts;
5964 for( i_track = 0; i_track < tracks.size(); i_track++ )
5965 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, sys.i_start_pts );
5969 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
5970 const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
5976 for( i_track = 0; i_track < tracks.size(); i_track++ )
5978 const mkv_track_t *tk = tracks[i_track];
5980 if( ( p_block != NULL && tk->i_number == p_block->TrackNum() ) ||
5981 ( p_simpleblock != NULL && tk->i_number == p_simpleblock->TrackNum() ) )
5987 if( i_track >= tracks.size() )
5988 return VLC_EGENERIC;
5991 *pi_track = i_track;
5995 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 )
5997 demux_sys_t *p_sys = demuxer.p_sys;
6000 // find the actual time for an ordered edition
6001 if ( psz_chapter == NULL )
6003 if ( Edition() && Edition()->b_ordered )
6005 /* 1st, we need to know in which chapter we are */
6006 psz_chapter = (*p_editions)[i_current_edition]->FindTimecode( i_date, psz_current_chapter );
6010 if ( psz_chapter != NULL )
6012 psz_current_chapter = psz_chapter;
6013 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
6014 if ( psz_chapter->i_seekpoint_num > 0 )
6016 demuxer.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
6017 demuxer.info.i_title = p_sys->i_current_title = i_sys_title;
6018 demuxer.info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
6022 // find the best matching segment
6023 for ( i=0; i<linked_segments.size(); i++ )
6025 if ( i_date < linked_segments[i]->i_start_time )
6032 if ( i_current_segment != i )
6034 linked_segments[i_current_segment]->UnSelect();
6035 linked_segments[i]->Select( i_date );
6036 i_current_segment = i;
6039 linked_segments[i]->Seek( i_date, i_time_offset, i_global_position );
6042 void chapter_codec_cmds_c::AddCommand( const KaxChapterProcessCommand & command )
6046 uint32 codec_time = uint32(-1);
6047 for( i = 0; i < command.ListSize(); i++ )
6049 const EbmlElement *k = command[i];
6051 if( MKV_IS_ID( k, KaxChapterProcessTime ) )
6053 codec_time = uint32( *static_cast<const KaxChapterProcessTime*>( k ) );
6058 for( i = 0; i < command.ListSize(); i++ )
6060 const EbmlElement *k = command[i];
6062 if( MKV_IS_ID( k, KaxChapterProcessData ) )
6064 KaxChapterProcessData *p_data = new KaxChapterProcessData( *static_cast<const KaxChapterProcessData*>( k ) );
6065 switch ( codec_time )
6068 during_cmds.push_back( p_data );
6071 enter_cmds.push_back( p_data );
6074 leave_cmds.push_back( p_data );
6083 bool chapter_item_c::Enter( bool b_do_subs )
6085 bool f_result = false;
6086 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
6087 while ( index != codecs.end() )
6089 f_result |= (*index)->Enter();
6096 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
6097 while ( index_ != sub_chapters.end() )
6099 f_result |= (*index_)->Enter( true );
6106 bool chapter_item_c::Leave( bool b_do_subs )
6108 bool f_result = false;
6109 b_is_leaving = true;
6110 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
6111 while ( index != codecs.end() )
6113 f_result |= (*index)->Leave();
6120 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
6121 while ( index_ != sub_chapters.end() )
6123 f_result |= (*index_)->Leave( true );
6127 b_is_leaving = false;
6131 bool chapter_item_c::EnterAndLeave( chapter_item_c *p_item, bool b_final_enter )
6133 chapter_item_c *p_common_parent = p_item;
6135 // leave, up to a common parent
6136 while ( p_common_parent != NULL && !p_common_parent->ParentOf( *this ) )
6138 if ( !p_common_parent->b_is_leaving && p_common_parent->Leave( false ) )
6140 p_common_parent = p_common_parent->psz_parent;
6143 // enter from the parent to <this>
6144 if ( p_common_parent != NULL )
6148 if ( p_common_parent == this )
6149 return Enter( true );
6151 for ( size_t i = 0; i<p_common_parent->sub_chapters.size(); i++ )
6153 if ( p_common_parent->sub_chapters[i]->ParentOf( *this ) )
6155 p_common_parent = p_common_parent->sub_chapters[i];
6156 if ( p_common_parent != this )
6157 if ( p_common_parent->Enter( false ) )
6166 if ( b_final_enter )
6167 return Enter( true );
6172 bool dvd_chapter_codec_c::Enter()
6174 bool f_result = false;
6175 std::vector<KaxChapterProcessData*>::iterator index = enter_cmds.begin();
6176 while ( index != enter_cmds.end() )
6178 if ( (*index)->GetSize() )
6180 binary *p_data = (*index)->GetBuffer();
6181 size_t i_size = *p_data++;
6182 // avoid reading too much from the buffer
6183 i_size = __MIN( i_size, ((*index)->GetSize() - 1) >> 3 );
6184 for ( ; i_size > 0; i_size--, p_data += 8 )
6186 msg_Dbg( &sys.demuxer, "Matroska DVD enter command" );
6187 f_result |= sys.dvd_interpretor.Interpret( p_data );
6195 bool dvd_chapter_codec_c::Leave()
6197 bool f_result = false;
6198 std::vector<KaxChapterProcessData*>::iterator index = leave_cmds.begin();
6199 while ( index != leave_cmds.end() )
6201 if ( (*index)->GetSize() )
6203 binary *p_data = (*index)->GetBuffer();
6204 size_t i_size = *p_data++;
6205 // avoid reading too much from the buffer
6206 i_size = __MIN( i_size, ((*index)->GetSize() - 1) >> 3 );
6207 for ( ; i_size > 0; i_size--, p_data += 8 )
6209 msg_Dbg( &sys.demuxer, "Matroska DVD leave command" );
6210 f_result |= sys.dvd_interpretor.Interpret( p_data );
6218 // see http://www.dvd-replica.com/DVD/vmcmdset.php for a description of DVD commands
6219 bool dvd_command_interpretor_c::Interpret( const binary * p_command, size_t i_size )
6224 virtual_segment_c *p_segment = NULL;
6225 chapter_item_c *p_chapter = NULL;
6226 bool f_result = false;
6227 uint16 i_command = ( p_command[0] << 8 ) + p_command[1];
6229 // handle register tests if there are some
6230 if ( (i_command & 0xF0) != 0 )
6232 bool b_test_positive = true;//(i_command & CMD_DVD_IF_NOT) == 0;
6233 bool b_test_value = (i_command & CMD_DVD_TEST_VALUE) != 0;
6234 uint8 i_test = i_command & 0x70;
6237 // see http://dvd.sourceforge.net/dvdinfo/vmi.html
6240 switch ( i_command >> 12 )
6243 i_cr1 = p_command[3];
6244 i_cr2 = (p_command[4] << 8) + p_command[5];
6249 i_cr1 = p_command[6];
6250 i_cr2 = p_command[7];
6251 b_test_value = false;
6255 if ( ((p_command[1] >> 4) & 0x7) == 0)
6257 i_cr1 = p_command[2];
6258 i_cr2 = (p_command[6] << 8) + p_command[7];
6262 i_cr1 = p_command[2];
6263 i_cr2 = (p_command[6] << 8) + p_command[7];
6271 i_value = GetPRM( i_cr2 );
6275 case CMD_DVD_IF_GPREG_EQUAL:
6277 msg_Dbg( &sys.demuxer, "IF %s EQUALS %s", GetRegTypeName( false, i_cr1 ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6278 if (!( GetPRM( i_cr1 ) == i_value ))
6280 b_test_positive = false;
6283 case CMD_DVD_IF_GPREG_NOT_EQUAL:
6285 msg_Dbg( &sys.demuxer, "IF %s NOT EQUALS %s", GetRegTypeName( false, i_cr1 ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6286 if (!( GetPRM( i_cr1 ) != i_value ))
6288 b_test_positive = false;
6291 case CMD_DVD_IF_GPREG_INF:
6293 msg_Dbg( &sys.demuxer, "IF %s < %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6294 if (!( GetPRM( i_cr1 ) < i_value ))
6296 b_test_positive = false;
6299 case CMD_DVD_IF_GPREG_INF_EQUAL:
6300 // if inferior or equal
6301 msg_Dbg( &sys.demuxer, "IF %s < %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6302 if (!( GetPRM( i_cr1 ) <= i_value ))
6304 b_test_positive = false;
6307 case CMD_DVD_IF_GPREG_AND:
6309 msg_Dbg( &sys.demuxer, "IF %s & %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6310 if (!( GetPRM( i_cr1 ) & i_value ))
6312 b_test_positive = false;
6315 case CMD_DVD_IF_GPREG_SUP:
6317 msg_Dbg( &sys.demuxer, "IF %s >= %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6318 if (!( GetPRM( i_cr1 ) > i_value ))
6320 b_test_positive = false;
6323 case CMD_DVD_IF_GPREG_SUP_EQUAL:
6324 // if superior or equal
6325 msg_Dbg( &sys.demuxer, "IF %s >= %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6326 if (!( GetPRM( i_cr1 ) >= i_value ))
6328 b_test_positive = false;
6333 if ( !b_test_positive )
6337 // strip the test command
6338 i_command &= 0xFF0F;
6340 switch ( i_command )
6345 msg_Dbg( &sys.demuxer, "NOP" );
6350 msg_Dbg( &sys.demuxer, "Break" );
6354 case CMD_DVD_JUMP_TT:
6356 uint8 i_title = p_command[5];
6357 msg_Dbg( &sys.demuxer, "JumpTT %d", i_title );
6359 // find in the ChapProcessPrivate matching this Title level
6360 p_chapter = sys.BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title), p_segment );
6361 if ( p_segment != NULL )
6363 sys.JumpTo( *p_segment, p_chapter );
6369 case CMD_DVD_CALLSS_VTSM1:
6371 msg_Dbg( &sys.demuxer, "CallSS" );
6373 switch( (p_command[6] & 0xC0) >> 6 ) {
6375 p_type = p_command[5] & 0x0F;
6379 msg_Dbg( &sys.demuxer, "CallSS PGC (rsm_cell %x)", p_command[4]);
6382 msg_Dbg( &sys.demuxer, "CallSS Title Entry (rsm_cell %x)", p_command[4]);
6385 msg_Dbg( &sys.demuxer, "CallSS Root Menu (rsm_cell %x)", p_command[4]);
6388 msg_Dbg( &sys.demuxer, "CallSS Subpicture Menu (rsm_cell %x)", p_command[4]);
6391 msg_Dbg( &sys.demuxer, "CallSS Audio Menu (rsm_cell %x)", p_command[4]);
6394 msg_Dbg( &sys.demuxer, "CallSS Angle Menu (rsm_cell %x)", p_command[4]);
6397 msg_Dbg( &sys.demuxer, "CallSS Chapter Menu (rsm_cell %x)", p_command[4]);
6400 msg_Dbg( &sys.demuxer, "CallSS <unknown> (rsm_cell %x)", p_command[4]);
6403 p_chapter = sys.BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1, p_segment );
6404 if ( p_segment != NULL )
6406 sys.JumpTo( *p_segment, p_chapter );
6411 msg_Dbg( &sys.demuxer, "CallSS VMGM (menu %d, rsm_cell %x)", p_command[5] & 0x0F, p_command[4]);
6414 msg_Dbg( &sys.demuxer, "CallSS VTSM (menu %d, rsm_cell %x)", p_command[5] & 0x0F, p_command[4]);
6417 msg_Dbg( &sys.demuxer, "CallSS VMGM (pgc %d, rsm_cell %x)", (p_command[2] << 8) + p_command[3], p_command[4]);
6422 case CMD_DVD_JUMP_SS:
6424 msg_Dbg( &sys.demuxer, "JumpSS");
6426 switch( (p_command[5] & 0xC0) >> 6 ) {
6428 msg_Dbg( &sys.demuxer, "JumpSS FP");
6431 p_type = p_command[5] & 0x0F;
6435 msg_Dbg( &sys.demuxer, "JumpSS VMGM Title Entry");
6438 msg_Dbg( &sys.demuxer, "JumpSS VMGM Root Menu");
6441 msg_Dbg( &sys.demuxer, "JumpSS VMGM Subpicture Menu");
6444 msg_Dbg( &sys.demuxer, "JumpSS VMGM Audio Menu");
6447 msg_Dbg( &sys.demuxer, "JumpSS VMGM Angle Menu");
6450 msg_Dbg( &sys.demuxer, "JumpSS VMGM Chapter Menu");
6453 msg_Dbg( &sys.demuxer, "JumpSS <unknown>");
6457 p_chapter = sys.BrowseCodecPrivate( 1, MatchIsVMG, NULL, 0, p_segment );
6458 if ( p_segment != NULL )
6460 p_chapter = p_segment->BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1 );
6461 if ( p_chapter != NULL )
6463 sys.JumpTo( *p_segment, p_chapter );
6469 p_type = p_command[5] & 0x0F;
6473 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Title Entry", p_command[4], p_command[3]);
6476 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Root Menu", p_command[4], p_command[3]);
6479 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Subpicture Menu", p_command[4], p_command[3]);
6482 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Audio Menu", p_command[4], p_command[3]);
6485 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Angle Menu", p_command[4], p_command[3]);
6488 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Chapter Menu", p_command[4], p_command[3]);
6491 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) <unknown>", p_command[4], p_command[3]);
6495 p_chapter = sys.BrowseCodecPrivate( 1, MatchVTSMNumber, &p_command[4], 1, p_segment );
6497 if ( p_segment != NULL && p_chapter != NULL )
6499 // find the title in the VTS
6500 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchTitleNumber, &p_command[3], 1 );
6501 if ( p_chapter != NULL )
6503 // find the specified menu in the VTSM
6504 p_chapter = p_segment->BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1 );
6505 if ( p_chapter != NULL )
6507 sys.JumpTo( *p_segment, p_chapter );
6512 msg_Dbg( &sys.demuxer, "Title (%d) does not exist in this VTS", p_command[3] );
6515 msg_Dbg( &sys.demuxer, "DVD Domain VTS (%d) not found", p_command[4] );
6518 msg_Dbg( &sys.demuxer, "JumpSS VMGM (pgc %d)", (p_command[2] << 8) + p_command[3]);
6523 case CMD_DVD_JUMPVTS_PTT:
6525 uint8 i_title = p_command[5];
6526 uint8 i_ptt = p_command[3];
6528 msg_Dbg( &sys.demuxer, "JumpVTS Title (%d) PTT (%d)", i_title, i_ptt);
6530 // find the current VTS content segment
6531 p_chapter = sys.p_current_segment->BrowseCodecPrivate( 1, MatchIsDomain, NULL, 0 );
6532 if ( p_chapter != NULL )
6534 int16 i_curr_title = p_chapter->GetTitleNumber( );
6535 if ( i_curr_title > 0 )
6537 p_chapter = sys.BrowseCodecPrivate( 1, MatchVTSNumber, &i_curr_title, sizeof(i_curr_title), p_segment );
6539 if ( p_segment != NULL && p_chapter != NULL )
6541 // find the title in the VTS
6542 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title) );
6543 if ( p_chapter != NULL )
6545 // find the chapter in the title
6546 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchChapterNumber, &i_ptt, sizeof(i_ptt) );
6547 if ( p_chapter != NULL )
6549 sys.JumpTo( *p_segment, p_chapter );
6554 msg_Dbg( &sys.demuxer, "Title (%d) does not exist in this VTS", i_title );
6557 msg_Dbg( &sys.demuxer, "DVD Domain VTS (%d) not found", i_curr_title );
6560 msg_Dbg( &sys.demuxer, "JumpVTS_PTT command found but not in a VTS(M)");
6563 msg_Dbg( &sys.demuxer, "JumpVTS_PTT command but the DVD domain wasn't found");
6566 case CMD_DVD_SET_GPRMMD:
6568 msg_Dbg( &sys.demuxer, "Set GPRMMD [%d]=%d", (p_command[4] << 8) + p_command[5], (p_command[2] << 8) + p_command[3]);
6570 if ( !SetGPRM( (p_command[4] << 8) + p_command[5], (p_command[2] << 8) + p_command[3] ) )
6571 msg_Dbg( &sys.demuxer, "Set GPRMMD failed" );
6574 case CMD_DVD_LINKPGCN:
6576 uint16 i_pgcn = (p_command[6] << 8) + p_command[7];
6578 msg_Dbg( &sys.demuxer, "Link PGCN(%d)", i_pgcn );
6579 p_chapter = sys.p_current_segment->BrowseCodecPrivate( 1, MatchPgcNumber, &i_pgcn, 2 );
6580 if ( p_chapter != NULL )
6582 if ( !p_chapter->Enter( true ) )
6583 // jump to the location in the found segment
6584 sys.p_current_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter, -1 );
6590 case CMD_DVD_LINKCN:
6592 uint8 i_cn = p_command[7];
6594 p_chapter = sys.p_current_segment->CurrentChapter();
6596 msg_Dbg( &sys.demuxer, "LinkCN (cell %d)", i_cn );
6597 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchCellNumber, &i_cn, 1 );
6598 if ( p_chapter != NULL )
6600 if ( !p_chapter->Enter( true ) )
6601 // jump to the location in the found segment
6602 sys.p_current_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter, -1 );
6608 case CMD_DVD_GOTO_LINE:
6610 msg_Dbg( &sys.demuxer, "GotoLine (%d)", (p_command[6] << 8) + p_command[7] );
6614 case CMD_DVD_SET_HL_BTNN1:
6616 msg_Dbg( &sys.demuxer, "SetHL_BTN (%d)", p_command[4] );
6617 SetSPRM( 0x88, p_command[4] );
6622 msg_Dbg( &sys.demuxer, "unsupported command : %02X %02X %02X %02X %02X %02X %02X %02X"
6638 bool dvd_command_interpretor_c::MatchIsDomain( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6640 return ( data.p_private_data != NULL && data.p_private_data->GetBuffer()[0] == MATROSKA_DVD_LEVEL_SS );
6643 bool dvd_command_interpretor_c::MatchIsVMG( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6645 if ( data.p_private_data == NULL || data.p_private_data->GetSize() < 2 )
6648 return ( data.p_private_data->GetBuffer()[0] == MATROSKA_DVD_LEVEL_SS && data.p_private_data->GetBuffer()[1] == 0xC0);
6651 bool dvd_command_interpretor_c::MatchVTSNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6653 if ( i_cookie_size != 2 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6656 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_SS || data.p_private_data->GetBuffer()[1] != 0x80 )
6659 uint16 i_gtitle = (data.p_private_data->GetBuffer()[2] << 8 ) + data.p_private_data->GetBuffer()[3];
6660 uint16 i_title = *(uint16*)p_cookie;
6662 return (i_gtitle == i_title);
6665 bool dvd_command_interpretor_c::MatchVTSMNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6667 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6670 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_SS || data.p_private_data->GetBuffer()[1] != 0x40 )
6673 uint8 i_gtitle = data.p_private_data->GetBuffer()[3];
6674 uint8 i_title = *(uint8*)p_cookie;
6676 return (i_gtitle == i_title);
6679 bool dvd_command_interpretor_c::MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6681 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6684 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_TT )
6687 uint16 i_gtitle = (data.p_private_data->GetBuffer()[1] << 8 ) + data.p_private_data->GetBuffer()[2];
6688 uint8 i_title = *(uint8*)p_cookie;
6690 return (i_gtitle == i_title);
6693 bool dvd_command_interpretor_c::MatchPgcType( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6695 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 8 )
6698 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PGC )
6701 uint8 i_pgc_type = data.p_private_data->GetBuffer()[3] & 0x0F;
6702 uint8 i_pgc = *(uint8*)p_cookie;
6704 return (i_pgc_type == i_pgc);
6707 bool dvd_command_interpretor_c::MatchPgcNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6709 if ( i_cookie_size != 2 || data.p_private_data == NULL || data.p_private_data->GetSize() < 8 )
6712 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PGC )
6715 uint16 *i_pgc_n = (uint16 *)p_cookie;
6716 uint16 i_pgc_num = (data.p_private_data->GetBuffer()[1] << 8) + data.p_private_data->GetBuffer()[2];
6718 return (i_pgc_num == *i_pgc_n);
6721 bool dvd_command_interpretor_c::MatchChapterNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6723 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 2 )
6726 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PTT )
6729 uint8 i_chapter = data.p_private_data->GetBuffer()[1];
6730 uint8 i_ptt = *(uint8*)p_cookie;
6732 return (i_chapter == i_ptt);
6735 bool dvd_command_interpretor_c::MatchCellNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6737 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 5 )
6740 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_CN )
6743 uint8 *i_cell_n = (uint8 *)p_cookie;
6744 uint8 i_cell_num = data.p_private_data->GetBuffer()[3];
6746 return (i_cell_num == *i_cell_n);
6749 bool matroska_script_codec_c::Enter()
6751 bool f_result = false;
6752 std::vector<KaxChapterProcessData*>::iterator index = enter_cmds.begin();
6753 while ( index != enter_cmds.end() )
6755 if ( (*index)->GetSize() )
6757 msg_Dbg( &sys.demuxer, "Matroska Script enter command" );
6758 f_result |= interpretor.Interpret( (*index)->GetBuffer(), (*index)->GetSize() );
6765 bool matroska_script_codec_c::Leave()
6767 bool f_result = false;
6768 std::vector<KaxChapterProcessData*>::iterator index = leave_cmds.begin();
6769 while ( index != leave_cmds.end() )
6771 if ( (*index)->GetSize() )
6773 msg_Dbg( &sys.demuxer, "Matroska Script leave command" );
6774 f_result |= interpretor.Interpret( (*index)->GetBuffer(), (*index)->GetSize() );
6781 // see http://www.matroska.org/technical/specs/chapters/index.html#mscript
6782 // for a description of existing commands
6783 bool matroska_script_interpretor_c::Interpret( const binary * p_command, size_t i_size )
6785 bool b_result = false;
6787 char *psz_str = (char*) malloc( i_size + 1 );
6788 memcpy( psz_str, p_command, i_size );
6789 psz_str[ i_size ] = '\0';
6791 std::string sz_command = psz_str;
6794 msg_Dbg( &sys.demuxer, "command : %s", sz_command.c_str() );
6796 #if defined(__GNUC__) && (__GNUC__ < 3)
6797 if ( sz_command.compare( CMD_MS_GOTO_AND_PLAY, 0, CMD_MS_GOTO_AND_PLAY.size() ) == 0 )
6799 if ( sz_command.compare( 0, CMD_MS_GOTO_AND_PLAY.size(), CMD_MS_GOTO_AND_PLAY ) == 0 )
6805 for ( i=CMD_MS_GOTO_AND_PLAY.size(); i<sz_command.size(); i++)
6807 if ( sz_command[i] == '(' )
6814 for ( j=i; j<sz_command.size(); j++)
6816 if ( sz_command[j] == ')' )
6823 std::string st = sz_command.substr( i+1, j-i-1 );
6824 int64_t i_chapter_uid = atoi( st.c_str() );
6826 virtual_segment_c *p_segment;
6827 chapter_item_c *p_chapter = sys.FindChapter( i_chapter_uid, p_segment );
6829 if ( p_chapter == NULL )
6830 msg_Dbg( &sys.demuxer, "Chapter %"PRId64" not found", i_chapter_uid);
6833 if ( !p_chapter->EnterAndLeave( sys.p_current_segment->CurrentChapter() ) )
6834 p_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter, -1 );
6842 void demux_sys_t::SwapButtons()
6844 #ifndef WORDS_BIGENDIAN
6845 uint8_t button, i, j;
6847 for( button = 1; button <= pci_packet.hli.hl_gi.btn_ns; button++) {
6848 btni_t *button_ptr = &(pci_packet.hli.btnit[button-1]);
6849 binary *p_data = (binary*) button_ptr;
6851 uint16 i_x_start = ((p_data[0] & 0x3F) << 4 ) + ( p_data[1] >> 4 );
6852 uint16 i_x_end = ((p_data[1] & 0x03) << 8 ) + p_data[2];
6853 uint16 i_y_start = ((p_data[3] & 0x3F) << 4 ) + ( p_data[4] >> 4 );
6854 uint16 i_y_end = ((p_data[4] & 0x03) << 8 ) + p_data[5];
6855 button_ptr->x_start = i_x_start;
6856 button_ptr->x_end = i_x_end;
6857 button_ptr->y_start = i_y_start;
6858 button_ptr->y_end = i_y_end;
6861 for ( i = 0; i<3; i++ )
6863 for ( j = 0; j<2; j++ )
6865 pci_packet.hli.btn_colit.btn_coli[i][j] = U32_AT( &pci_packet.hli.btn_colit.btn_coli[i][j] );