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 *****************************************************************************/
33 #define __STDC_FORMAT_MACROS 1
34 #define __STDC_CONSTANT_MACROS 1
40 # include <time.h> /* time() */
44 #include <vlc_codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
47 #include <vlc_charset.h>
48 #include <vlc_input.h>
49 #include <vlc_demux.h>
62 /* libebml and matroska */
63 #include "ebml/EbmlHead.h"
64 #include "ebml/EbmlSubHead.h"
65 #include "ebml/EbmlStream.h"
66 #include "ebml/EbmlContexts.h"
67 #include "ebml/EbmlVoid.h"
68 #include "ebml/EbmlVersion.h"
69 #include "ebml/StdIOCallback.h"
71 #include "matroska/KaxAttachments.h"
72 #include "matroska/KaxAttached.h"
73 #include "matroska/KaxBlock.h"
74 #include "matroska/KaxBlockData.h"
75 #include "matroska/KaxChapters.h"
76 #include "matroska/KaxCluster.h"
77 #include "matroska/KaxClusterData.h"
78 #include "matroska/KaxContexts.h"
79 #include "matroska/KaxCues.h"
80 #include "matroska/KaxCuesData.h"
81 #include "matroska/KaxInfo.h"
82 #include "matroska/KaxInfoData.h"
83 #include "matroska/KaxSeekHead.h"
84 #include "matroska/KaxSegment.h"
85 #include "matroska/KaxTag.h"
86 #include "matroska/KaxTags.h"
87 #include "matroska/KaxTagMulti.h"
88 #include "matroska/KaxTracks.h"
89 #include "matroska/KaxTrackAudio.h"
90 #include "matroska/KaxTrackVideo.h"
91 #include "matroska/KaxTrackEntryData.h"
92 #include "matroska/KaxContentEncoding.h"
93 #include "matroska/KaxVersion.h"
95 #include "ebml/StdIOCallback.h"
97 #if LIBMATROSKA_VERSION < 0x000706
98 START_LIBMATROSKA_NAMESPACE
99 extern const EbmlSemanticContext MATROSKA_DLL_API KaxMatroska_Context;
100 END_LIBMATROSKA_NAMESPACE
103 #include "vlc_keys.h"
106 #include "mp4/libmp4.h"
112 #define MATROSKA_COMPRESSION_NONE -1
113 #define MATROSKA_COMPRESSION_ZLIB 0
114 #define MATROSKA_COMPRESSION_BLIB 1
115 #define MATROSKA_COMPRESSION_LZOX 2
116 #define MATROSKA_COMPRESSION_HEADER 3
118 #define MKVD_TIMECODESCALE 1000000
120 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
121 #undef ATTRIBUTE_PACKED
122 #undef PRAGMA_PACK_BEGIN
123 #undef PRAGMA_PACK_END
125 #if defined(__GNUC__)
126 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
127 #define ATTRIBUTE_PACKED __attribute__ ((packed))
128 #define PRAGMA_PACK 0
132 #if !defined(ATTRIBUTE_PACKED)
133 #define ATTRIBUTE_PACKED
134 #define PRAGMA_PACK 1
141 /*************************************
142 * taken from libdvdnav / libdvdread
143 **************************************/
146 * DVD Time Information.
152 uint8_t frame_u; /* The two high bits are the frame rate. */
153 } ATTRIBUTE_PACKED dvd_time_t;
159 #ifdef WORDS_BIGENDIAN
160 unsigned char zero : 7; /* 25-31 */
161 unsigned char video_pres_mode_change : 1; /* 24 */
163 unsigned char karaoke_audio_pres_mode_change : 1; /* 23 */
164 unsigned char angle_change : 1;
165 unsigned char subpic_stream_change : 1;
166 unsigned char audio_stream_change : 1;
167 unsigned char pause_on : 1;
168 unsigned char still_off : 1;
169 unsigned char button_select_or_activate : 1;
170 unsigned char resume : 1; /* 16 */
172 unsigned char chapter_menu_call : 1; /* 15 */
173 unsigned char angle_menu_call : 1;
174 unsigned char audio_menu_call : 1;
175 unsigned char subpic_menu_call : 1;
176 unsigned char root_menu_call : 1;
177 unsigned char title_menu_call : 1;
178 unsigned char backward_scan : 1;
179 unsigned char forward_scan : 1; /* 8 */
181 unsigned char next_pg_search : 1; /* 7 */
182 unsigned char prev_or_top_pg_search : 1;
183 unsigned char time_or_chapter_search : 1;
184 unsigned char go_up : 1;
185 unsigned char stop : 1;
186 unsigned char title_play : 1;
187 unsigned char chapter_search_or_play : 1;
188 unsigned char title_or_time_play : 1; /* 0 */
190 unsigned char video_pres_mode_change : 1; /* 24 */
191 unsigned char zero : 7; /* 25-31 */
193 unsigned char resume : 1; /* 16 */
194 unsigned char button_select_or_activate : 1;
195 unsigned char still_off : 1;
196 unsigned char pause_on : 1;
197 unsigned char audio_stream_change : 1;
198 unsigned char subpic_stream_change : 1;
199 unsigned char angle_change : 1;
200 unsigned char karaoke_audio_pres_mode_change : 1; /* 23 */
202 unsigned char forward_scan : 1; /* 8 */
203 unsigned char backward_scan : 1;
204 unsigned char title_menu_call : 1;
205 unsigned char root_menu_call : 1;
206 unsigned char subpic_menu_call : 1;
207 unsigned char audio_menu_call : 1;
208 unsigned char angle_menu_call : 1;
209 unsigned char chapter_menu_call : 1; /* 15 */
211 unsigned char title_or_time_play : 1; /* 0 */
212 unsigned char chapter_search_or_play : 1;
213 unsigned char title_play : 1;
214 unsigned char stop : 1;
215 unsigned char go_up : 1;
216 unsigned char time_or_chapter_search : 1;
217 unsigned char prev_or_top_pg_search : 1;
218 unsigned char next_pg_search : 1; /* 7 */
220 } ATTRIBUTE_PACKED user_ops_t;
223 * Type to store per-command data.
227 } ATTRIBUTE_PACKED vm_cmd_t;
228 #define COMMAND_DATA_SIZE 8
231 * PCI General Information
234 uint32_t nv_pck_lbn; /**< sector address of this nav pack */
235 uint16_t vobu_cat; /**< 'category' of vobu */
236 uint16_t zero1; /**< reserved */
237 user_ops_t vobu_uop_ctl; /**< UOP of vobu */
238 uint32_t vobu_s_ptm; /**< start presentation time of vobu */
239 uint32_t vobu_e_ptm; /**< end presentation time of vobu */
240 uint32_t vobu_se_e_ptm; /**< end ptm of sequence end in vobu */
241 dvd_time_t e_eltm; /**< Cell elapsed time */
243 } ATTRIBUTE_PACKED pci_gi_t;
246 * Non Seamless Angle Information
249 uint32_t nsml_agl_dsta[9]; /**< address of destination vobu in AGL_C#n */
250 } ATTRIBUTE_PACKED nsml_agli_t;
253 * Highlight General Information
255 * For btngrX_dsp_ty the bits have the following meaning:
256 * 000b: normal 4/3 only buttons
257 * XX1b: wide (16/9) buttons
258 * X1Xb: letterbox buttons
259 * 1XXb: pan&scan buttons
262 uint16_t hli_ss; /**< status, only low 2 bits 0: no buttons, 1: different 2: equal 3: eual except for button cmds */
263 uint32_t hli_s_ptm; /**< start ptm of hli */
264 uint32_t hli_e_ptm; /**< end ptm of hli */
265 uint32_t btn_se_e_ptm; /**< end ptm of button select */
266 #ifdef WORDS_BIGENDIAN
267 unsigned char zero1 : 2; /**< reserved */
268 unsigned char btngr_ns : 2; /**< number of button groups 1, 2 or 3 with 36/18/12 buttons */
269 unsigned char zero2 : 1; /**< reserved */
270 unsigned char btngr1_dsp_ty : 3; /**< display type of subpic stream for button group 1 */
271 unsigned char zero3 : 1; /**< reserved */
272 unsigned char btngr2_dsp_ty : 3; /**< display type of subpic stream for button group 2 */
273 unsigned char zero4 : 1; /**< reserved */
274 unsigned char btngr3_dsp_ty : 3; /**< display type of subpic stream for button group 3 */
276 unsigned char btngr1_dsp_ty : 3;
277 unsigned char zero2 : 1;
278 unsigned char btngr_ns : 2;
279 unsigned char zero1 : 2;
280 unsigned char btngr3_dsp_ty : 3;
281 unsigned char zero4 : 1;
282 unsigned char btngr2_dsp_ty : 3;
283 unsigned char zero3 : 1;
285 uint8_t btn_ofn; /**< button offset number range 0-255 */
286 uint8_t btn_ns; /**< number of valid buttons <= 36/18/12 (low 6 bits) */
287 uint8_t nsl_btn_ns; /**< number of buttons selectable by U_BTNNi (low 6 bits) nsl_btn_ns <= btn_ns */
288 uint8_t zero5; /**< reserved */
289 uint8_t fosl_btnn; /**< forcedly selected button (low 6 bits) */
290 uint8_t foac_btnn; /**< forcedly activated button (low 6 bits) */
291 } ATTRIBUTE_PACKED hl_gi_t;
295 * Button Color Information Table
296 * Each entry beeing a 32bit word that contains the color indexs and alpha
297 * values to use. They are all represented by 4 bit number and stored
298 * like this [Ci3, Ci2, Ci1, Ci0, A3, A2, A1, A0]. The actual palette
299 * that the indexes reference is in the PGC.
300 * \todo split the uint32_t into a struct
303 uint32_t btn_coli[3][2]; /**< [button color number-1][select:0/action:1] */
304 } ATTRIBUTE_PACKED btn_colit_t;
309 * NOTE: I've had to change the structure from the disk layout to get
310 * the packing to work with Sun's Forte C compiler.
311 * The 4 and 7 bytes are 'rotated' was: ABC DEF GHIJ is: ABCG DEFH IJ
314 #ifdef WORDS_BIGENDIAN
315 uint32 btn_coln : 2; /**< button color number */
316 uint32 x_start : 10; /**< x start offset within the overlay */
317 uint32 zero1 : 2; /**< reserved */
318 uint32 x_end : 10; /**< x end offset within the overlay */
320 uint32 zero3 : 2; /**< reserved */
321 uint32 up : 6; /**< button index when pressing up */
323 uint32 auto_action_mode : 2; /**< 0: no, 1: activated if selected */
324 uint32 y_start : 10; /**< y start offset within the overlay */
325 uint32 zero2 : 2; /**< reserved */
326 uint32 y_end : 10; /**< y end offset within the overlay */
328 uint32 zero4 : 2; /**< reserved */
329 uint32 down : 6; /**< button index when pressing down */
330 unsigned char zero5 : 2; /**< reserved */
331 unsigned char left : 6; /**< button index when pressing left */
332 unsigned char zero6 : 2; /**< reserved */
333 unsigned char right : 6; /**< button index when pressing right */
346 uint32 auto_action_mode : 2;
350 unsigned char left : 6;
351 unsigned char zero5 : 2;
352 unsigned char right : 6;
353 unsigned char zero6 : 2;
356 } ATTRIBUTE_PACKED btni_t;
359 * Highlight Information
363 btn_colit_t btn_colit;
365 } ATTRIBUTE_PACKED hli_t;
372 nsml_agli_t nsml_agli;
375 } ATTRIBUTE_PACKED pci_t;
381 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
385 * What's between a directory and a filename?
388 #define DIRECTORY_SEPARATOR '\\'
390 #define DIRECTORY_SEPARATOR '/'
393 using namespace LIBMATROSKA_NAMESPACE;
396 /*****************************************************************************
398 *****************************************************************************/
399 static int Open ( vlc_object_t * );
400 static void Close( vlc_object_t * );
403 set_shortname( "Matroska" );
404 set_description( _("Matroska stream demuxer" ) );
405 set_capability( "demux", 50 );
406 set_callbacks( Open, Close );
407 set_category( CAT_INPUT );
408 set_subcategory( SUBCAT_INPUT_DEMUX );
410 add_bool( "mkv-use-ordered-chapters", 1, NULL,
411 N_("Ordered chapters"),
412 N_("Play ordered chapters as specified in the segment."), true );
414 add_bool( "mkv-use-chapter-codec", 1, NULL,
415 N_("Chapter codecs"),
416 N_("Use chapter codecs found in the segment."), true );
418 add_bool( "mkv-preload-local-dir", 1, NULL,
419 N_("Preload Directory"),
420 N_("Preload matroska files from the same family in the same directory (not good for broken files)."), true );
422 add_bool( "mkv-seek-percent", 0, NULL,
423 N_("Seek based on percent not time"),
424 N_("Seek based on percent not time."), true );
426 add_bool( "mkv-use-dummy", 0, NULL,
427 N_("Dummy Elements"),
428 N_("Read and discard unknown EBML elements (not good for broken files)."), true );
430 add_shortcut( "mka" );
431 add_shortcut( "mkv" );
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), MSG_QUEUE_NORMAL, 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 );
557 EbmlElement *m_el[10];
558 int64_t mi_remain_size[10];
568 /*****************************************************************************
569 * Some functions to manipulate memory
570 *****************************************************************************/
571 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
572 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
574 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
577 /*****************************************************************************
578 * definitions of structures and functions used by this plugins
579 *****************************************************************************/
586 unsigned int i_number;
589 uint8_t *p_extra_data;
593 uint64_t i_default_duration;
594 float f_timecodescale;
603 unsigned int i_original_rate;
606 /* data to be send first */
608 uint8_t *p_data_init;
610 /* hack : it's for seek */
611 bool b_search_keyframe;
615 const char *psz_codec_name;
616 const char *psz_codec_settings;
617 const char *psz_codec_info_url;
618 const char *psz_codec_download_url;
620 /* encryption/compression */
621 int i_compression_type;
622 KaxContentCompSettings *p_compression_data;
639 const binary MATROSKA_DVD_LEVEL_SS = 0x30;
640 const binary MATROSKA_DVD_LEVEL_LU = 0x2A;
641 const binary MATROSKA_DVD_LEVEL_TT = 0x28;
642 const binary MATROSKA_DVD_LEVEL_PGC = 0x20;
643 const binary MATROSKA_DVD_LEVEL_PG = 0x18;
644 const binary MATROSKA_DVD_LEVEL_PTT = 0x10;
645 const binary MATROSKA_DVD_LEVEL_CN = 0x08;
647 class chapter_codec_cmds_c
650 chapter_codec_cmds_c( demux_sys_t & demuxer, int codec_id = -1)
651 :p_private_data(NULL)
652 ,i_codec_id( codec_id )
656 virtual ~chapter_codec_cmds_c()
658 delete p_private_data;
659 std::vector<KaxChapterProcessData*>::iterator indexe = enter_cmds.begin();
660 while ( indexe != enter_cmds.end() )
665 std::vector<KaxChapterProcessData*>::iterator indexl = leave_cmds.begin();
666 while ( indexl != leave_cmds.end() )
671 std::vector<KaxChapterProcessData*>::iterator indexd = during_cmds.begin();
672 while ( indexd != during_cmds.end() )
679 void SetPrivate( const KaxChapterProcessPrivate & private_data )
681 p_private_data = new KaxChapterProcessPrivate( private_data );
684 void AddCommand( const KaxChapterProcessCommand & command );
686 /// \return wether the codec has seeked in the files or not
687 virtual bool Enter() { return false; }
688 virtual bool Leave() { return false; }
689 virtual std::string GetCodecName( bool f_for_title = false ) const { return ""; }
690 virtual int16 GetTitleNumber() { return -1; }
692 KaxChapterProcessPrivate *p_private_data;
695 std::vector<KaxChapterProcessData*> enter_cmds;
696 std::vector<KaxChapterProcessData*> during_cmds;
697 std::vector<KaxChapterProcessData*> leave_cmds;
703 class dvd_command_interpretor_c
706 dvd_command_interpretor_c( demux_sys_t & demuxer )
709 memset( p_PRMs, 0, sizeof(p_PRMs) );
710 p_PRMs[ 0x80 + 1 ] = 15;
711 p_PRMs[ 0x80 + 2 ] = 62;
712 p_PRMs[ 0x80 + 3 ] = 1;
713 p_PRMs[ 0x80 + 4 ] = 1;
714 p_PRMs[ 0x80 + 7 ] = 1;
715 p_PRMs[ 0x80 + 8 ] = 1;
716 p_PRMs[ 0x80 + 16 ] = 0xFFFFu;
717 p_PRMs[ 0x80 + 18 ] = 0xFFFFu;
720 bool Interpret( const binary * p_command, size_t i_size = 8 );
722 uint16 GetPRM( size_t index ) const
725 return p_PRMs[ index ];
729 uint16 GetGPRM( size_t index ) const
731 if ( index >= 0 && index < 16 )
732 return p_PRMs[ index ];
736 uint16 GetSPRM( size_t index ) const
738 // 21,22,23 reserved for future use
739 if ( index >= 0x80 && index < 0x95 )
740 return p_PRMs[ index ];
744 bool SetPRM( size_t index, uint16 value )
746 if ( index >= 0 && index < 16 )
748 p_PRMs[ index ] = value;
754 bool SetGPRM( size_t index, uint16 value )
756 if ( index >= 0 && index < 16 )
758 p_PRMs[ index ] = value;
764 bool SetSPRM( size_t index, uint16 value )
766 if ( index > 0x80 && index <= 0x8D && index != 0x8C )
768 p_PRMs[ index ] = value;
775 std::string GetRegTypeName( bool b_value, uint16 value ) const
778 char s_value[6], s_reg_value[6];
779 sprintf( s_value, "%.5d", value );
787 else if ( value < 0x80 )
789 sprintf( s_reg_value, "%.5d", GetPRM( value ) );
793 result += s_reg_value;
798 sprintf( s_reg_value, "%.5d", GetPRM( value ) );
802 result += s_reg_value;
814 // wether it's a comparison on the value or register
815 static const uint16 CMD_DVD_TEST_VALUE = 0x80;
816 static const uint16 CMD_DVD_IF_GPREG_AND = (1 << 4);
817 static const uint16 CMD_DVD_IF_GPREG_EQUAL = (2 << 4);
818 static const uint16 CMD_DVD_IF_GPREG_NOT_EQUAL = (3 << 4);
819 static const uint16 CMD_DVD_IF_GPREG_SUP_EQUAL = (4 << 4);
820 static const uint16 CMD_DVD_IF_GPREG_SUP = (5 << 4);
821 static const uint16 CMD_DVD_IF_GPREG_INF_EQUAL = (6 << 4);
822 static const uint16 CMD_DVD_IF_GPREG_INF = (7 << 4);
824 static const uint16 CMD_DVD_NOP = 0x0000;
825 static const uint16 CMD_DVD_GOTO_LINE = 0x0001;
826 static const uint16 CMD_DVD_BREAK = 0x0002;
828 static const uint16 CMD_DVD_NOP2 = 0x2001;
829 static const uint16 CMD_DVD_LINKPGCN = 0x2004;
830 static const uint16 CMD_DVD_LINKPGN = 0x2006;
831 static const uint16 CMD_DVD_LINKCN = 0x2007;
832 static const uint16 CMD_DVD_JUMP_TT = 0x3002;
833 static const uint16 CMD_DVD_JUMPVTS_TT = 0x3003;
834 static const uint16 CMD_DVD_JUMPVTS_PTT = 0x3005;
835 static const uint16 CMD_DVD_JUMP_SS = 0x3006;
836 static const uint16 CMD_DVD_CALLSS_VTSM1 = 0x3008;
838 static const uint16 CMD_DVD_SET_HL_BTNN2 = 0x4600;
839 static const uint16 CMD_DVD_SET_HL_BTNN_LINKPGCN1 = 0x4604;
840 static const uint16 CMD_DVD_SET_STREAM = 0x5100;
841 static const uint16 CMD_DVD_SET_GPRMMD = 0x5300;
842 static const uint16 CMD_DVD_SET_HL_BTNN1 = 0x5600;
843 static const uint16 CMD_DVD_SET_HL_BTNN_LINKPGCN2 = 0x5604;
844 static const uint16 CMD_DVD_SET_HL_BTNN_LINKCN = 0x5607;
846 static const uint16 CMD_DVD_MOV_SPREG_PREG = 0x6100;
847 static const uint16 CMD_DVD_GPREG_MOV_VALUE = 0x7100;
848 static const uint16 CMD_DVD_SUB_GPREG = 0x7400;
849 static const uint16 CMD_DVD_MULT_GPREG = 0x7500;
850 static const uint16 CMD_DVD_GPREG_DIV_VALUE = 0x7600;
851 static const uint16 CMD_DVD_GPREG_AND_VALUE = 0x7900;
853 // callbacks when browsing inside CodecPrivate
854 static bool MatchIsDomain ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
855 static bool MatchIsVMG ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
856 static bool MatchVTSNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
857 static bool MatchVTSMNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
858 static bool MatchTitleNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
859 static bool MatchPgcType ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
860 static bool MatchPgcNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
861 static bool MatchChapterNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
862 static bool MatchCellNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
865 class dvd_chapter_codec_c : public chapter_codec_cmds_c
868 dvd_chapter_codec_c( demux_sys_t & sys )
869 :chapter_codec_cmds_c( sys, 1 )
874 std::string GetCodecName( bool f_for_title = false ) const;
875 int16 GetTitleNumber();
878 class matroska_script_interpretor_c
881 matroska_script_interpretor_c( demux_sys_t & demuxer )
885 bool Interpret( const binary * p_command, size_t i_size );
888 static const std::string CMD_MS_GOTO_AND_PLAY;
894 const std::string matroska_script_interpretor_c::CMD_MS_GOTO_AND_PLAY = "GotoAndPlay";
897 class matroska_script_codec_c : public chapter_codec_cmds_c
900 matroska_script_codec_c( demux_sys_t & sys )
901 :chapter_codec_cmds_c( sys, 0 )
909 matroska_script_interpretor_c interpretor;
912 class chapter_translation_c
915 chapter_translation_c()
919 ~chapter_translation_c()
924 KaxChapterTranslateID *p_translated;
925 unsigned int codec_id;
926 std::vector<uint64_t> editions;
935 ,i_user_start_time(-1)
938 ,b_display_seekpoint(true)
939 ,b_user_display(false)
944 virtual ~chapter_item_c()
946 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
947 while ( index != codecs.end() )
952 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
953 while ( index_ != sub_chapters.end() )
960 int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time );
961 int PublishChapters( input_title_t & title, int & i_user_chapters, int i_level = 0 );
962 virtual chapter_item_c * FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current, bool & b_found );
963 void Append( const chapter_item_c & edition );
964 chapter_item_c * FindChapter( int64_t i_find_uid );
965 virtual chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
966 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
967 const void *p_cookie,
968 size_t i_cookie_size );
969 std::string GetCodecName( bool f_for_title = false ) const;
970 bool ParentOf( const chapter_item_c & item ) const;
971 int16 GetTitleNumber( ) const;
973 int64_t i_start_time, i_end_time;
974 int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */
975 std::vector<chapter_item_c*> sub_chapters;
978 bool b_display_seekpoint;
980 std::string psz_name;
981 chapter_item_c *psz_parent;
984 std::vector<chapter_codec_cmds_c*> codecs;
986 static bool CompareTimecode( const chapter_item_c * itemA, const chapter_item_c * itemB )
988 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) );
991 bool Enter( bool b_do_subchapters );
992 bool Leave( bool b_do_subchapters );
993 bool EnterAndLeave( chapter_item_c *p_item, bool b_enter = true );
996 class chapter_edition_c : public chapter_item_c
1003 void RefreshChapters( );
1004 mtime_t Duration() const;
1005 std::string GetMainName() const;
1006 chapter_item_c * FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current );
1011 class matroska_segment_c
1014 matroska_segment_c( demux_sys_t & demuxer, EbmlStream & estream )
1017 ,i_timescale(MKVD_TIMECODESCALE)
1020 ,i_cues_position(-1)
1021 ,i_chapters_position(-1)
1022 ,i_tags_position(-1)
1027 ,p_segment_uid(NULL)
1028 ,p_prev_segment_uid(NULL)
1029 ,p_next_segment_uid(NULL)
1033 ,psz_muxing_application(NULL)
1034 ,psz_writing_application(NULL)
1035 ,psz_segment_filename(NULL)
1038 ,i_default_edition(0)
1043 p_indexes = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
1046 virtual ~matroska_segment_c()
1048 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1050 if ( tracks[i_track]->p_compression_data )
1052 delete tracks[i_track]->p_compression_data;
1054 es_format_Clean( &tracks[i_track]->fmt );
1055 free( tracks[i_track]->p_extra_data );
1056 free( tracks[i_track]->psz_codec );
1057 delete tracks[i_track];
1060 free( psz_writing_application );
1061 free( psz_muxing_application );
1062 free( psz_segment_filename );
1064 free( psz_date_utc );
1069 delete p_segment_uid;
1070 delete p_prev_segment_uid;
1071 delete p_next_segment_uid;
1073 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
1074 while ( index != stored_editions.end() )
1079 std::vector<chapter_translation_c*>::iterator indext = translations.begin();
1080 while ( indext != translations.end() )
1085 std::vector<KaxSegmentFamily*>::iterator indexf = families.begin();
1086 while ( indexf != families.end() )
1093 KaxSegment *segment;
1097 uint64_t i_timescale;
1099 /* duration of the segment */
1101 mtime_t i_start_time;
1104 std::vector<mkv_track_t*> tracks;
1107 int64_t i_cues_position;
1108 int64_t i_chapters_position;
1109 int64_t i_tags_position;
1111 KaxCluster *cluster;
1113 uint64 i_cluster_pos;
1114 int64_t i_start_pos;
1115 KaxSegmentUID *p_segment_uid;
1116 KaxPrevUID *p_prev_segment_uid;
1117 KaxNextUID *p_next_segment_uid;
1122 mkv_index_t *p_indexes;
1125 char *psz_muxing_application;
1126 char *psz_writing_application;
1127 char *psz_segment_filename;
1131 /* !!!!! GCC 3.3 bug on Darwin !!!!! */
1132 /* when you remove this variable the compiler issues an atomicity error */
1133 /* this variable only works when using std::vector<chapter_edition_c> */
1134 std::vector<chapter_edition_c*> stored_editions;
1135 int i_default_edition;
1137 std::vector<chapter_translation_c*> translations;
1138 std::vector<KaxSegmentFamily*> families;
1145 bool PreloadFamily( const matroska_segment_c & segment );
1146 void ParseInfo( KaxInfo *info );
1147 void ParseAttachments( KaxAttachments *attachments );
1148 void ParseChapters( KaxChapters *chapters );
1149 void ParseSeekHead( KaxSeekHead *seekhead );
1150 void ParseTracks( KaxTracks *tracks );
1151 void ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters );
1152 void ParseTrackEntry( KaxTrackEntry *m );
1153 void ParseCluster( );
1154 void IndexAppendCluster( KaxCluster *cluster );
1157 void InformationCreate( );
1158 void Seek( mtime_t i_date, mtime_t i_time_offset );
1159 #if LIBMATROSKA_VERSION >= 0x000800
1160 int BlockGet( KaxBlock * &, KaxSimpleBlock * &, int64_t *, int64_t *, int64_t *);
1162 int BlockGet( KaxBlock * &, int64_t *, int64_t *, int64_t *);
1164 bool Select( mtime_t i_start_time );
1167 static bool CompareSegmentUIDs( const matroska_segment_c * item_a, const matroska_segment_c * item_b );
1170 // class holding hard-linked segment together in the playback order
1171 class virtual_segment_c
1174 virtual_segment_c( matroska_segment_c *p_segment )
1177 ,i_current_segment(0)
1178 ,i_current_edition(-1)
1179 ,psz_current_chapter(NULL)
1181 linked_segments.push_back( p_segment );
1183 AppendUID( p_segment->p_segment_uid );
1184 AppendUID( p_segment->p_prev_segment_uid );
1185 AppendUID( p_segment->p_next_segment_uid );
1189 size_t AddSegment( matroska_segment_c *p_segment );
1190 void PreloadLinked( );
1191 mtime_t Duration( ) const;
1193 void Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter );
1195 inline chapter_edition_c *Edition()
1197 if ( i_current_edition >= 0 && size_t(i_current_edition) < p_editions->size() )
1198 return (*p_editions)[i_current_edition];
1202 matroska_segment_c * Segment() const
1204 if ( linked_segments.size() == 0 || i_current_segment >= linked_segments.size() )
1206 return linked_segments[i_current_segment];
1209 inline chapter_item_c *CurrentChapter() {
1210 return psz_current_chapter;
1215 if ( i_current_segment < linked_segments.size()-1 )
1217 i_current_segment++;
1223 bool FindUID( KaxSegmentUID & uid ) const
1225 for ( size_t i=0; i<linked_uids.size(); i++ )
1227 if ( linked_uids[i] == uid )
1233 bool UpdateCurrentToChapter( demux_t & demux );
1234 void PrepareChapters( );
1236 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
1237 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1238 const void *p_cookie,
1239 size_t i_cookie_size );
1240 chapter_item_c *FindChapter( int64_t i_find_uid );
1242 std::vector<chapter_edition_c*> *p_editions;
1246 std::vector<matroska_segment_c*> linked_segments;
1247 std::vector<KaxSegmentUID> linked_uids;
1248 size_t i_current_segment;
1250 int i_current_edition;
1251 chapter_item_c *psz_current_chapter;
1253 void AppendUID( const EbmlBinary * UID );
1256 class matroska_stream_c
1259 matroska_stream_c( demux_sys_t & demuxer )
1265 virtual ~matroska_stream_c()
1274 std::vector<matroska_segment_c*> segments;
1300 virtual ~attachment_c()
1305 std::string psz_file_name;
1306 std::string psz_mime_type;
1314 demux_sys_t( demux_t & demux )
1321 ,p_current_segment(NULL)
1322 ,dvd_interpretor( *this )
1326 ,b_pci_packet_set(false)
1329 vlc_mutex_init( &demuxer, &lock_demuxer );
1332 virtual ~demux_sys_t()
1336 for ( i=0; i<streams.size(); i++ )
1338 for ( i=0; i<opened_segments.size(); i++ )
1339 delete opened_segments[i];
1340 for ( i=0; i<used_segments.size(); i++ )
1341 delete used_segments[i];
1342 for ( i=0; i<stored_attachments.size(); i++ )
1343 delete stored_attachments[i];
1344 if( meta ) vlc_meta_Delete( meta );
1346 while( titles.size() )
1347 { vlc_input_title_Delete( titles.back() ); titles.pop_back();}
1349 vlc_mutex_destroy( &lock_demuxer );
1356 mtime_t i_start_pts;
1357 mtime_t i_chapter_time;
1361 std::vector<input_title_t*> titles; // matroska editions
1362 size_t i_current_title;
1364 std::vector<matroska_stream_c*> streams;
1365 std::vector<attachment_c*> stored_attachments;
1366 std::vector<matroska_segment_c*> opened_segments;
1367 std::vector<virtual_segment_c*> used_segments;
1368 virtual_segment_c *p_current_segment;
1370 dvd_command_interpretor_c dvd_interpretor;
1372 /* duration of the stream */
1375 matroska_segment_c *FindSegment( const EbmlBinary & uid ) const;
1376 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
1377 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1378 const void *p_cookie,
1379 size_t i_cookie_size,
1380 virtual_segment_c * & p_segment_found );
1381 chapter_item_c *FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found );
1383 void PreloadFamily( const matroska_segment_c & of_segment );
1384 void PreloadLinked( matroska_segment_c *p_segment );
1385 bool PreparePlayback( virtual_segment_c *p_new_segment );
1386 matroska_stream_c *AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial = false );
1387 void JumpTo( virtual_segment_c & p_segment, chapter_item_c * p_chapter );
1389 void StartUiThread();
1390 void StopUiThread();
1392 inline void SwapButtons();
1394 /* for spu variables */
1395 input_thread_t *p_input;
1397 bool b_pci_packet_set;
1398 uint8_t palette[4][4];
1399 vlc_mutex_t lock_demuxer;
1402 event_thread_t *p_ev;
1403 static int EventThread( vlc_object_t *p_this );
1404 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1405 vlc_value_t oldval, vlc_value_t newval, void *p_data );
1406 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1407 vlc_value_t oldval, vlc_value_t newval, void *p_data );
1412 virtual_segment_c *VirtualFromSegments( matroska_segment_c *p_segment ) const;
1413 bool IsUsedSegment( matroska_segment_c &p_segment ) const;
1416 static int Demux ( demux_t * );
1417 static int Control( demux_t *, int, va_list );
1418 static void Seek ( demux_t *, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter );
1420 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
1422 static inline char * ToUTF8( const UTFstring &u )
1424 return strdup( u.GetUTF8().c_str() );
1427 /*****************************************************************************
1428 * Open: initializes matroska demux structures
1429 *****************************************************************************/
1430 static int Open( vlc_object_t * p_this )
1432 demux_t *p_demux = (demux_t*)p_this;
1434 matroska_stream_c *p_stream;
1435 matroska_segment_c *p_segment;
1436 const uint8_t *p_peek;
1437 std::string s_path, s_filename;
1438 vlc_stream_io_callback *p_io_callback;
1439 EbmlStream *p_io_stream;
1441 /* peek the begining */
1442 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
1444 /* is a valid file */
1445 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
1446 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
1448 /* Set the demux function */
1449 p_demux->pf_demux = Demux;
1450 p_demux->pf_control = Control;
1451 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
1453 p_io_callback = new vlc_stream_io_callback( p_demux->s, false );
1454 p_io_stream = new EbmlStream( *p_io_callback );
1456 if( p_io_stream == NULL )
1458 msg_Err( p_demux, "failed to create EbmlStream" );
1459 delete p_io_callback;
1461 return VLC_EGENERIC;
1464 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_io_stream, true );
1465 if( p_stream == NULL )
1467 msg_Err( p_demux, "cannot find KaxSegment" );
1470 p_sys->streams.push_back( p_stream );
1472 p_stream->p_in = p_io_callback;
1473 p_stream->p_es = p_io_stream;
1475 for (size_t i=0; i<p_stream->segments.size(); i++)
1477 p_stream->segments[i]->Preload();
1480 p_segment = p_stream->segments[0];
1481 if( p_segment->cluster != NULL )
1483 msg_Warn( p_demux, "cannot find any cluster, damaged file ?" );
1485 // reset the stream reading to the first cluster of the segment used
1486 p_stream->p_in->setFilePointer( p_segment->cluster->GetElementPosition() );
1489 if (config_GetInt( p_demux, "mkv-preload-local-dir" ))
1491 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
1492 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
1494 // assume it's a regular file
1495 // get the directory path
1496 s_path = p_demux->psz_path;
1497 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
1499 s_path = s_path.substr(0,s_path.length()-1);
1503 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
1505 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
1509 DIR *p_src_dir = utf8_opendir(s_path.c_str());
1511 if (p_src_dir != NULL)
1514 while ((psz_file = utf8_readdir(p_src_dir)) != NULL)
1516 if (strlen(psz_file) > 4)
1518 s_filename = s_path + DIRECTORY_SEPARATOR + psz_file;
1521 if (!strcasecmp(s_filename.c_str(), p_demux->psz_path))
1523 if (!s_filename.compare(p_demux->psz_path))
1527 continue; // don't reuse the original opened file
1530 #if defined(__GNUC__) && (__GNUC__ < 3)
1531 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
1532 !s_filename.compare("mka", s_filename.length() - 3, 3))
1534 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
1535 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
1538 // test wether this file belongs to our family
1539 const uint8_t *p_peek;
1540 bool file_ok = false;
1541 stream_t *p_file_stream = stream_UrlNew(
1543 s_filename.c_str());
1544 /* peek the begining */
1545 if( p_file_stream &&
1546 stream_Peek( p_file_stream, &p_peek, 4 ) >= 4
1547 && p_peek[0] == 0x1a && p_peek[1] == 0x45 &&
1548 p_peek[2] == 0xdf && p_peek[3] == 0xa3 ) file_ok = true;
1552 vlc_stream_io_callback *p_file_io = new vlc_stream_io_callback( p_file_stream, true );
1553 EbmlStream *p_estream = new EbmlStream(*p_file_io);
1555 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_estream );
1557 if ( p_stream == NULL )
1559 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
1565 p_stream->p_in = p_file_io;
1566 p_stream->p_es = p_estream;
1567 p_sys->streams.push_back( p_stream );
1572 if( p_file_stream ) {
1573 stream_Delete( p_file_stream );
1575 msg_Dbg( p_demux, "the file '%s' cannot be opened", s_filename.c_str() );
1581 closedir( p_src_dir );
1585 p_sys->PreloadFamily( *p_segment );
1588 p_sys->PreloadLinked( p_segment );
1590 if ( !p_sys->PreparePlayback( NULL ) )
1592 msg_Err( p_demux, "cannot use the segment" );
1596 p_sys->StartUiThread();
1602 return VLC_EGENERIC;
1605 /*****************************************************************************
1606 * Close: frees unused data
1607 *****************************************************************************/
1608 static void Close( vlc_object_t *p_this )
1610 demux_t *p_demux = (demux_t*)p_this;
1611 demux_sys_t *p_sys = p_demux->p_sys;
1616 /*****************************************************************************
1618 *****************************************************************************/
1619 static int Control( demux_t *p_demux, int i_query, va_list args )
1621 demux_sys_t *p_sys = p_demux->p_sys;
1628 input_attachment_t ***ppp_attach;
1634 case DEMUX_GET_ATTACHMENTS:
1635 ppp_attach = (input_attachment_t***)va_arg( args, input_attachment_t*** );
1636 pi_int = (int*)va_arg( args, int * );
1638 if( p_sys->stored_attachments.size() <= 0 )
1639 return VLC_EGENERIC;
1641 *pi_int = p_sys->stored_attachments.size();
1642 *ppp_attach = (input_attachment_t**)malloc( sizeof(input_attachment_t**) *
1643 p_sys->stored_attachments.size() );
1644 if( !(*ppp_attach) )
1646 for( i = 0; i < p_sys->stored_attachments.size(); i++ )
1648 attachment_c *a = p_sys->stored_attachments[i];
1649 (*ppp_attach)[i] = vlc_input_attachment_New( a->psz_file_name.c_str(), a->psz_mime_type.c_str(), NULL,
1650 a->p_data, a->i_size );
1654 case DEMUX_GET_META:
1655 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1656 vlc_meta_Merge( p_meta, p_sys->meta );
1659 case DEMUX_GET_LENGTH:
1660 pi64 = (int64_t*)va_arg( args, int64_t * );
1661 if( p_sys->f_duration > 0.0 )
1663 *pi64 = (int64_t)(p_sys->f_duration * 1000);
1666 return VLC_EGENERIC;
1668 case DEMUX_GET_POSITION:
1669 pf = (double*)va_arg( args, double * );
1670 if ( p_sys->f_duration > 0.0 )
1671 *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);
1674 case DEMUX_SET_POSITION:
1675 f = (double)va_arg( args, double );
1676 Seek( p_demux, -1, f, NULL );
1679 case DEMUX_GET_TIME:
1680 pi64 = (int64_t*)va_arg( args, int64_t * );
1681 *pi64 = p_sys->i_pts;
1684 case DEMUX_GET_TITLE_INFO:
1685 if( p_sys->titles.size() )
1687 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1688 int *pi_int = (int*)va_arg( args, int* );
1690 *pi_int = p_sys->titles.size();
1691 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->titles.size() );
1693 for( size_t i = 0; i < p_sys->titles.size(); i++ )
1695 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->titles[i] );
1700 return VLC_EGENERIC;
1702 case DEMUX_SET_TITLE:
1703 /* TODO handle editions as titles */
1704 i_idx = (int)va_arg( args, int );
1705 if( i_idx < p_sys->used_segments.size() )
1707 p_sys->JumpTo( *p_sys->used_segments[i_idx], NULL );
1710 return VLC_EGENERIC;
1712 case DEMUX_SET_SEEKPOINT:
1713 i_skp = (int)va_arg( args, int );
1715 // TODO change the way it works with the << & >> buttons on the UI (+1/-1 instead of a number)
1716 if( p_sys->titles.size() && i_skp < p_sys->titles[p_sys->i_current_title]->i_seekpoint)
1718 Seek( p_demux, (int64_t)p_sys->titles[p_sys->i_current_title]->seekpoint[i_skp]->i_time_offset, -1, NULL);
1719 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
1720 p_demux->info.i_seekpoint = i_skp;
1723 return VLC_EGENERIC;
1725 case DEMUX_SET_TIME:
1728 return VLC_EGENERIC;
1732 #if LIBMATROSKA_VERSION >= 0x000800
1733 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1735 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1738 #if LIBMATROSKA_VERSION >= 0x000800
1739 pp_simpleblock = NULL;
1747 EbmlElement *el = NULL;
1751 return VLC_EGENERIC;
1753 #if LIBMATROSKA_VERSION >= 0x000800
1754 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1756 if( (el = ep->Get()) == NULL && pp_block != NULL )
1759 /* update the index */
1760 #define idx p_indexes[i_index - 1]
1761 if( i_index > 0 && idx.i_time == -1 )
1763 #if LIBMATROSKA_VERSION >= 0x000800
1764 if ( pp_simpleblock != NULL )
1765 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1768 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1769 idx.b_key = *pi_ref1 == 0 ? true : false;
1775 i_level = ep->GetLevel();
1784 msg_Warn( &sys.demuxer, "EOF" );
1785 return VLC_EGENERIC;
1792 if( MKV_IS_ID( el, KaxCluster ) )
1794 cluster = (KaxCluster*)el;
1795 i_cluster_pos = cluster->GetElementPosition();
1797 /* add it to the index */
1799 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1801 IndexAppendCluster( cluster );
1804 // reset silent tracks
1805 for (size_t i=0; i<tracks.size(); i++)
1807 tracks[i]->b_silent = false;
1812 else if( MKV_IS_ID( el, KaxCues ) )
1814 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1815 return VLC_EGENERIC;
1819 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1823 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1825 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1827 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1828 cluster->InitTimecode( uint64( ctc ), i_timescale );
1830 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1834 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1836 i_block_pos = el->GetElementPosition();
1839 #if LIBMATROSKA_VERSION >= 0x000800
1840 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1842 pp_simpleblock = (KaxSimpleBlock*)el;
1844 pp_simpleblock->ReadData( es.I_O() );
1845 pp_simpleblock->SetParent( *cluster );
1850 if( MKV_IS_ID( el, KaxBlock ) )
1852 pp_block = (KaxBlock*)el;
1854 pp_block->ReadData( es.I_O() );
1855 pp_block->SetParent( *cluster );
1859 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1861 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1863 dur.ReadData( es.I_O() );
1864 *pi_duration = uint64( dur );
1866 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1868 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1870 ref.ReadData( es.I_O() );
1873 *pi_ref1 = int64( ref ) * cluster->GlobalTimecodeScale();
1875 else if( *pi_ref2 == 0 )
1877 *pi_ref2 = int64( ref ) * cluster->GlobalTimecodeScale();
1880 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1882 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1883 track_num.ReadData( es.I_O() );
1885 for (size_t i=0; i<tracks.size(); i++)
1887 if ( tracks[i]->i_number == uint32(track_num))
1889 tracks[i]->b_silent = true;
1896 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1897 return VLC_EGENERIC;
1902 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem, size_t offset)
1905 if( !(p_block = block_New( p_demux, i_mem + offset ) ) ) return NULL;
1906 memcpy( p_block->p_buffer + offset, p_mem, i_mem );
1907 //p_block->i_rate = p_input->stream.control.i_rate;
1911 #if LIBMATROSKA_VERSION >= 0x000800
1912 static void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
1913 mtime_t i_pts, mtime_t i_duration, bool f_mandatory )
1915 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1916 mtime_t i_duration, bool f_mandatory )
1919 demux_sys_t *p_sys = p_demux->p_sys;
1920 matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
1926 #define tk p_segment->tracks[i_track]
1927 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1929 #if LIBMATROSKA_VERSION >= 0x000800
1930 if( (block != NULL && tk->i_number == block->TrackNum()) ||
1931 (simpleblock != NULL && tk->i_number == simpleblock->TrackNum()))
1933 if( tk->i_number == block->TrackNum() )
1940 if( i_track >= p_segment->tracks.size() )
1942 msg_Err( p_demux, "invalid track number" );
1945 if( tk->fmt.i_cat != NAV_ES && tk->p_es == NULL )
1947 msg_Err( p_demux, "unknown track number" );
1950 if( i_pts + i_duration < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1952 return; /* discard audio packets that shouldn't be rendered */
1955 if ( tk->fmt.i_cat != NAV_ES )
1957 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1961 tk->b_inited = false;
1967 /* First send init data */
1968 if( !tk->b_inited && tk->i_data_init > 0 )
1972 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
1973 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init, 0 );
1974 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
1976 tk->b_inited = true;
1979 #if LIBMATROSKA_VERSION >= 0x000800
1981 (block != NULL && i < block->NumberFrames()) || (simpleblock != NULL && i < simpleblock->NumberFrames());
1984 for( i = 0; i < block->NumberFrames(); i++ )
1989 #if LIBMATROSKA_VERSION >= 0x000800
1990 if ( simpleblock != NULL )
1992 data = &simpleblock->GetBuffer(i);
1993 // condition when the DTS is correct (keyframe or B frame == NOT P frame)
1994 f_mandatory = simpleblock->IsDiscardable() || simpleblock->IsKeyframe();
1998 data = &block->GetBuffer(i);
2000 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER && tk->p_compression_data != NULL )
2001 p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), tk->p_compression_data->GetSize() );
2003 p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), 0 );
2005 if( p_block == NULL )
2010 #if defined(HAVE_ZLIB_H)
2011 if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB )
2013 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
2017 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER )
2019 memcpy( p_block->p_buffer, tk->p_compression_data->GetBuffer(), tk->p_compression_data->GetSize() );
2022 if ( tk->fmt.i_cat == NAV_ES )
2024 // TODO handle the start/stop times of this packet
2025 if ( p_sys->b_ui_hooked )
2027 vlc_mutex_lock( &p_sys->p_ev->lock );
2028 memcpy( &p_sys->pci_packet, &p_block->p_buffer[1], sizeof(pci_t) );
2029 p_sys->SwapButtons();
2030 p_sys->b_pci_packet_set = true;
2031 vlc_mutex_unlock( &p_sys->p_ev->lock );
2032 block_Release( p_block );
2036 // correct timestamping when B frames are used
2037 if( tk->fmt.i_cat != VIDEO_ES )
2039 p_block->i_dts = p_block->i_pts = i_pts;
2043 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
2045 // in VFW we have no idea about B frames
2047 p_block->i_dts = i_pts;
2051 p_block->i_pts = i_pts;
2053 p_block->i_dts = p_block->i_pts;
2055 p_block->i_dts = min( i_pts, tk->i_last_dts + (mtime_t)(tk->i_default_duration >> 10));
2056 p_sys->i_pts = p_block->i_dts;
2059 tk->i_last_dts = p_block->i_dts;
2062 msg_Dbg( p_demux, "block i_dts: %"PRId64" / i_pts: %"PRId64, p_block->i_dts, p_block->i_pts);
2064 if( strcmp( tk->psz_codec, "S_VOBSUB" ) )
2066 p_block->i_length = i_duration * 1000;
2069 es_out_Send( p_demux->out, tk->p_es, p_block );
2071 /* use time stamp only for first block */
2078 matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial )
2080 int i_upper_lvl = 0;
2082 EbmlElement *p_l0, *p_l1, *p_l2;
2083 bool b_keep_stream = false, b_keep_segment;
2085 // verify the EBML Header
2086 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
2089 msg_Err( p_demux, "No EBML header found" );
2093 // verify we can read this Segment, we only support Matroska version 1 for now
2094 p_l0->Read(*p_estream, EbmlHead::ClassInfos.Context, i_upper_lvl, p_l0, true);
2096 EDocType doc_type = GetChild<EDocType>(*static_cast<EbmlHead*>(p_l0));
2097 if (std::string(doc_type) != "matroska")
2099 msg_Err( p_demux, "Not a Matroska file : DocType = %s ", std::string(doc_type).c_str());
2103 EDocTypeReadVersion doc_read_version = GetChild<EDocTypeReadVersion>(*static_cast<EbmlHead*>(p_l0));
2104 #if LIBMATROSKA_VERSION >= 0x000800
2105 if (uint64(doc_read_version) > 2)
2107 msg_Err( p_demux, "This matroska file is needs version %"PRId64" and this VLC only supports version 1 & 2", uint64(doc_read_version));
2111 if (uint64(doc_read_version) != 1)
2113 msg_Err( p_demux, "This matroska file is needs version %"PRId64" and this VLC only supports version 1", uint64(doc_read_version));
2121 // find all segments in this file
2122 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFFLL);
2128 matroska_stream_c *p_stream1 = new matroska_stream_c( *this );
2132 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
2135 matroska_segment_c *p_segment1 = new matroska_segment_c( *this, *p_estream );
2136 b_keep_segment = b_initial;
2138 ep = new EbmlParser(p_estream, p_l0, &demuxer );
2139 p_segment1->ep = ep;
2140 p_segment1->segment = (KaxSegment*)p_l0;
2142 while ((p_l1 = ep->Get()))
2144 if (MKV_IS_ID(p_l1, KaxInfo))
2146 // find the families of this segment
2147 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
2149 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
2150 for( i = 0; i < p_info->ListSize(); i++ )
2152 EbmlElement *l = (*p_info)[i];
2154 if( MKV_IS_ID( l, KaxSegmentUID ) )
2156 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
2157 b_keep_segment = (FindSegment( *p_uid ) == NULL);
2158 if ( !b_keep_segment )
2159 break; // this segment is already known
2160 opened_segments.push_back( p_segment1 );
2161 delete p_segment1->p_segment_uid;
2162 p_segment1->p_segment_uid = new KaxSegmentUID(*p_uid);
2164 else if( MKV_IS_ID( l, KaxPrevUID ) )
2166 p_segment1->p_prev_segment_uid = new KaxPrevUID( *static_cast<KaxPrevUID*>(l) );
2168 else if( MKV_IS_ID( l, KaxNextUID ) )
2170 p_segment1->p_next_segment_uid = new KaxNextUID( *static_cast<KaxNextUID*>(l) );
2172 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
2174 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
2175 p_segment1->families.push_back( p_fam );
2181 if ( b_keep_segment )
2183 b_keep_stream = true;
2184 p_stream1->segments.push_back( p_segment1 );
2188 p_segment1->segment = NULL;
2192 if (p_l0->IsFiniteSize() )
2194 p_l0->SkipData(*p_estream, KaxMatroska_Context);
2195 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
2198 p_l0 = p_l0->SkipData(*p_estream, KaxSegment_Context);
2201 if ( !b_keep_stream )
2210 bool matroska_segment_c::Select( mtime_t i_start_time )
2215 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
2216 sys.b_pci_packet_set = false;
2218 for( i_track = 0; i_track < tracks.size(); i_track++ )
2220 if( tracks[i_track]->fmt.i_cat == UNKNOWN_ES )
2222 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, tracks[i_track]->i_number );
2223 tracks[i_track]->p_es = NULL;
2227 if( !strcmp( tracks[i_track]->psz_codec, "V_MS/VFW/FOURCC" ) )
2229 if( tracks[i_track]->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
2231 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
2232 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2236 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tracks[i_track]->p_extra_data;
2238 tracks[i_track]->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
2239 tracks[i_track]->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
2240 tracks[i_track]->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
2242 tracks[i_track]->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
2243 if( tracks[i_track]->fmt.i_extra > 0 )
2245 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2246 memcpy( tracks[i_track]->fmt.p_extra, &p_bih[1], tracks[i_track]->fmt.i_extra );
2250 else if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG1" ) ||
2251 !strcmp( tracks[i_track]->psz_codec, "V_MPEG2" ) )
2253 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
2255 else if( !strncmp( tracks[i_track]->psz_codec, "V_THEORA", 8 ) )
2257 uint8_t *p_data = tracks[i_track]->p_extra_data;
2258 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
2259 if( tracks[i_track]->i_extra_data >= 4 ) {
2260 if( p_data[0] == 2 ) {
2262 int i_size1 = 0, i_size2 = 0;
2264 /* read size of first header packet */
2265 while( *p_data == 0xFF &&
2266 i < tracks[i_track]->i_extra_data )
2275 msg_Dbg( &sys.demuxer, "first theora header size %d", i_size1 );
2276 /* read size of second header packet */
2277 while( *p_data == 0xFF &&
2278 i < tracks[i_track]->i_extra_data )
2287 int i_size3 = tracks[i_track]->i_extra_data - i - i_size1
2289 msg_Dbg( &sys.demuxer, "second theora header size %d", i_size2 );
2290 msg_Dbg( &sys.demuxer, "third theora header size %d", i_size3 );
2291 tracks[i_track]->fmt.i_extra = i_size1 + i_size2 + i_size3
2293 if( i_size1 > 0 && i_size2 > 0 && i_size3 > 0 ) {
2294 tracks[i_track]->fmt.p_extra =
2295 malloc( tracks[i_track]->fmt.i_extra );
2296 uint8_t *p_out = (uint8_t*)tracks[i_track]->fmt.p_extra;
2297 *p_out++ = (i_size1>>8) & 0xFF;
2298 *p_out++ = i_size1 & 0xFF;
2299 memcpy( p_out, p_data, i_size1 );
2303 *p_out++ = (i_size2>>8) & 0xFF;
2304 *p_out++ = i_size2 & 0xFF;
2305 memcpy( p_out, p_data, i_size2 );
2309 *p_out++ = (i_size3>>8) & 0xFF;
2310 *p_out++ = i_size3 & 0xFF;
2311 memcpy( p_out, p_data, i_size3 );
2317 msg_Err( &sys.demuxer, "inconsistant theora extradata" );
2321 msg_Err( &sys.demuxer, "Wrong number of ogg packets with theora headers (%d)", p_data[0] + 1 );
2325 else if( !strncmp( tracks[i_track]->psz_codec, "V_REAL/RV", 9 ) )
2327 if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV10" ) )
2328 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '1', '0' );
2329 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV20" ) )
2330 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '2', '0' );
2331 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV30" ) )
2332 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '3', '0' );
2333 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV40" ) )
2334 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '4', '0' );
2336 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4", 7 ) )
2338 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/MS/V3" ) )
2340 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
2342 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO", 11 ) )
2344 /* A MPEG 4 codec, SP, ASP, AP or AVC */
2345 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO/AVC" ) )
2346 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
2348 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
2349 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2350 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2351 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2354 else if( !strcmp( tracks[i_track]->psz_codec, "V_QUICKTIME" ) )
2356 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
2357 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
2358 tracks[i_track]->p_extra_data,
2359 tracks[i_track]->i_extra_data,
2361 MP4_ReadBoxCommon( p_mp4_stream, p_box );
2362 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
2363 tracks[i_track]->fmt.i_codec = p_box->i_type;
2364 tracks[i_track]->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
2365 tracks[i_track]->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
2366 tracks[i_track]->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
2367 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2368 memcpy( tracks[i_track]->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tracks[i_track]->fmt.i_extra );
2369 MP4_FreeBox_sample_vide( p_box );
2370 stream_Delete( p_mp4_stream );
2372 else if( !strcmp( tracks[i_track]->psz_codec, "A_MS/ACM" ) )
2374 if( tracks[i_track]->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
2376 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
2377 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2381 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tracks[i_track]->p_extra_data;
2383 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tracks[i_track]->fmt.i_codec, NULL );
2385 tracks[i_track]->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
2386 tracks[i_track]->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
2387 tracks[i_track]->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
2388 tracks[i_track]->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
2389 tracks[i_track]->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
2391 tracks[i_track]->fmt.i_extra = GetWLE( &p_wf->cbSize );
2392 if( tracks[i_track]->fmt.i_extra > 0 )
2394 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2395 memcpy( tracks[i_track]->fmt.p_extra, &p_wf[1], tracks[i_track]->fmt.i_extra );
2399 else if( !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L3" ) ||
2400 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L2" ) ||
2401 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L1" ) )
2403 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
2405 else if( !strcmp( tracks[i_track]->psz_codec, "A_AC3" ) )
2407 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2409 else if( !strcmp( tracks[i_track]->psz_codec, "A_DTS" ) )
2411 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2413 else if( !strcmp( tracks[i_track]->psz_codec, "A_FLAC" ) )
2415 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
2416 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2417 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2418 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2420 else if( !strcmp( tracks[i_track]->psz_codec, "A_VORBIS" ) )
2422 int i, i_offset = 1, i_size[3], i_extra;
2425 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
2427 /* Split the 3 headers */
2428 if( tracks[i_track]->p_extra_data[0] != 0x02 )
2429 msg_Err( &sys.demuxer, "invalid vorbis header" );
2431 for( i = 0; i < 2; i++ )
2434 while( i_offset < tracks[i_track]->i_extra_data )
2436 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
2437 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
2441 i_size[0] = __MIN(i_size[0], tracks[i_track]->i_extra_data - i_offset);
2442 i_size[1] = __MIN(i_size[1], tracks[i_track]->i_extra_data -i_offset -i_size[0]);
2443 i_size[2] = tracks[i_track]->i_extra_data - i_offset - i_size[0] - i_size[1];
2445 tracks[i_track]->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
2446 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2447 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra; i_extra = 0;
2448 for( i = 0; i < 3; i++ )
2450 *(p_extra++) = i_size[i] >> 8;
2451 *(p_extra++) = i_size[i] & 0xFF;
2452 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra,
2454 p_extra += i_size[i];
2455 i_extra += i_size[i];
2458 else if( !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
2459 !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
2461 int i_profile, i_srate, sbr = 0;
2462 static unsigned int i_sample_rates[] =
2464 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
2465 16000, 12000, 11025, 8000, 7350, 0, 0, 0
2468 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
2469 /* create data for faad (MP4DecSpecificDescrTag)*/
2471 if( !strcmp( &tracks[i_track]->psz_codec[12], "MAIN" ) )
2475 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC" ) )
2479 else if( !strcmp( &tracks[i_track]->psz_codec[12], "SSR" ) )
2483 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC/SBR" ) )
2493 for( i_srate = 0; i_srate < 13; i_srate++ )
2495 if( i_sample_rates[i_srate] == tracks[i_track]->i_original_rate )
2500 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
2502 tracks[i_track]->fmt.i_extra = sbr ? 5 : 2;
2503 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2504 ((uint8_t*)tracks[i_track]->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
2505 ((uint8_t*)tracks[i_track]->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tracks[i_track]->fmt.audio.i_channels << 3);
2508 int syncExtensionType = 0x2B7;
2510 for (iDSRI=0; iDSRI<13; iDSRI++)
2511 if( i_sample_rates[iDSRI] == tracks[i_track]->fmt.audio.i_rate )
2513 ((uint8_t*)tracks[i_track]->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
2514 ((uint8_t*)tracks[i_track]->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
2515 ((uint8_t*)tracks[i_track]->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
2518 else if( !strcmp( tracks[i_track]->psz_codec, "A_AAC" ) )
2520 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
2521 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2522 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2523 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2525 else if( !strcmp( tracks[i_track]->psz_codec, "A_WAVPACK4" ) )
2527 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'W', 'V', 'P', 'K' );
2528 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2529 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2530 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2532 else if( !strcmp( tracks[i_track]->psz_codec, "A_TTA1" ) )
2534 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'T', 'T', 'A', '1' );
2535 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2536 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2537 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2539 else if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) ||
2540 !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/LIT" ) ||
2541 !strcmp( tracks[i_track]->psz_codec, "A_PCM/FLOAT/IEEE" ) )
2543 if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) )
2545 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
2549 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
2551 tracks[i_track]->fmt.audio.i_blockalign = ( tracks[i_track]->fmt.audio.i_bitspersample + 7 ) / 8 * tracks[i_track]->fmt.audio.i_channels;
2553 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/UTF8" ) )
2555 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
2556 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2558 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/USF" ) )
2560 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
2561 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2562 if( tracks[i_track]->i_extra_data )
2564 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2565 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2566 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2569 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/SSA" ) ||
2570 !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASS" ) ||
2571 !strcmp( tracks[i_track]->psz_codec, "S_SSA" ) ||
2572 !strcmp( tracks[i_track]->psz_codec, "S_ASS" ))
2574 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
2575 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2576 if( tracks[i_track]->i_extra_data )
2578 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2579 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2580 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2583 else if( !strcmp( tracks[i_track]->psz_codec, "S_VOBSUB" ) )
2585 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
2586 if( tracks[i_track]->i_extra_data )
2589 char *p_buf = (char *)malloc( tracks[i_track]->i_extra_data + 1);
2590 memcpy( p_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
2591 p_buf[tracks[i_track]->i_extra_data] = '\0';
2593 p_start = strstr( p_buf, "size:" );
2594 if( sscanf( p_start, "size: %dx%d",
2595 &tracks[i_track]->fmt.subs.spu.i_original_frame_width, &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) == 2 )
2597 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 );
2601 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
2606 else if( !strcmp( tracks[i_track]->psz_codec, "B_VOBBTN" ) )
2608 tracks[i_track]->fmt.i_cat = NAV_ES;
2613 msg_Err( &sys.demuxer, "unknown codec id=`%s'", tracks[i_track]->psz_codec );
2614 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2616 if( tracks[i_track]->b_default )
2618 tracks[i_track]->fmt.i_priority = 1000;
2621 tracks[i_track]->p_es = es_out_Add( sys.demuxer.out, &tracks[i_track]->fmt );
2623 /* Turn on a subtitles track if it has been flagged as default -
2624 * but only do this if no subtitles track has already been engaged,
2625 * either by an earlier 'default track' (??) or by default
2626 * language choice behaviour.
2628 if( tracks[i_track]->b_default )
2630 es_out_Control( sys.demuxer.out,
2632 tracks[i_track]->p_es );
2635 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, i_start_time );
2638 sys.i_start_pts = i_start_time;
2639 // reset the stream reading to the first cluster of the segment used
2640 es.I_O().setFilePointer( i_start_pos );
2643 ep = new EbmlParser( &es, segment, &sys.demuxer );
2648 void demux_sys_t::StartUiThread()
2652 msg_Dbg( &demuxer, "Starting the UI Hook" );
2654 /* FIXME hack hack hack hack FIXME */
2655 /* Get p_input and create variable */
2656 p_input = (input_thread_t *) vlc_object_find( &demuxer, VLC_OBJECT_INPUT, FIND_PARENT );
2657 var_Create( p_input, "x-start", VLC_VAR_INTEGER );
2658 var_Create( p_input, "y-start", VLC_VAR_INTEGER );
2659 var_Create( p_input, "x-end", VLC_VAR_INTEGER );
2660 var_Create( p_input, "y-end", VLC_VAR_INTEGER );
2661 var_Create( p_input, "color", VLC_VAR_ADDRESS );
2662 var_Create( p_input, "menu-palette", VLC_VAR_ADDRESS );
2663 var_Create( p_input, "highlight", VLC_VAR_BOOL );
2664 var_Create( p_input, "highlight-mutex", VLC_VAR_MUTEX );
2666 /* Now create our event thread catcher */
2667 p_ev = (event_thread_t *) vlc_object_create( &demuxer, sizeof( event_thread_t ) );
2668 p_ev->p_demux = &demuxer;
2669 p_ev->b_die = false;
2670 vlc_mutex_init( p_ev, &p_ev->lock );
2671 vlc_thread_create( p_ev, "mkv event thread handler", EventThread,
2672 VLC_THREAD_PRIORITY_LOW, false );
2676 void demux_sys_t::StopUiThread()
2680 vlc_object_kill( p_ev );
2681 vlc_thread_join( p_ev );
2682 vlc_object_release( p_ev );
2686 var_Destroy( p_input, "highlight-mutex" );
2687 var_Destroy( p_input, "highlight" );
2688 var_Destroy( p_input, "x-start" );
2689 var_Destroy( p_input, "x-end" );
2690 var_Destroy( p_input, "y-start" );
2691 var_Destroy( p_input, "y-end" );
2692 var_Destroy( p_input, "color" );
2693 var_Destroy( p_input, "menu-palette" );
2695 vlc_object_release( p_input );
2697 msg_Dbg( &demuxer, "Stopping the UI Hook" );
2699 b_ui_hooked = false;
2702 int demux_sys_t::EventMouse( vlc_object_t *p_this, char const *psz_var,
2703 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2705 event_thread_t *p_ev = (event_thread_t *) p_data;
2706 vlc_mutex_lock( &p_ev->lock );
2707 if( psz_var[6] == 'c' )
2709 p_ev->b_clicked = true;
2710 msg_Dbg( p_this, "Event Mouse: clicked");
2712 else if( psz_var[6] == 'm' )
2713 p_ev->b_moved = true;
2714 vlc_mutex_unlock( &p_ev->lock );
2719 int demux_sys_t::EventKey( vlc_object_t *p_this, char const *,
2720 vlc_value_t, vlc_value_t newval, void *p_data )
2722 event_thread_t *p_ev = (event_thread_t *) p_data;
2723 vlc_mutex_lock( &p_ev->lock );
2724 p_ev->i_key_action = newval.i_int;
2725 vlc_mutex_unlock( &p_ev->lock );
2726 msg_Dbg( p_this, "Event Key");
2731 int demux_sys_t::EventThread( vlc_object_t *p_this )
2733 event_thread_t *p_ev = (event_thread_t*)p_this;
2734 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
2735 vlc_object_t *p_vout = NULL;
2737 p_ev->b_moved = false;
2738 p_ev->b_clicked = false;
2739 p_ev->i_key_action = 0;
2741 /* catch all key event */
2742 var_AddCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
2745 while( !p_ev->b_die )
2747 if ( !p_sys->b_pci_packet_set )
2754 bool b_activated = false;
2757 if( p_ev->i_key_action )
2761 msg_Dbg( p_ev->p_demux, "Handle Key Event");
2763 vlc_mutex_lock( &p_ev->lock );
2765 pci_t *pci = (pci_t *) &p_sys->pci_packet;
2767 uint16 i_curr_button = p_sys->dvd_interpretor.GetSPRM( 0x88 );
2769 switch( p_ev->i_key_action )
2771 case ACTIONID_NAV_LEFT:
2772 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2774 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2775 if ( p_button_ptr->left > 0 && p_button_ptr->left <= pci->hli.hl_gi.btn_ns )
2777 i_curr_button = p_button_ptr->left;
2778 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2779 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2780 if ( button_ptr.auto_action_mode )
2782 vlc_mutex_unlock( &p_ev->lock );
2783 vlc_mutex_lock( &p_sys->lock_demuxer );
2785 // process the button action
2786 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2788 vlc_mutex_unlock( &p_sys->lock_demuxer );
2789 vlc_mutex_lock( &p_ev->lock );
2794 case ACTIONID_NAV_RIGHT:
2795 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2797 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2798 if ( p_button_ptr->right > 0 && p_button_ptr->right <= pci->hli.hl_gi.btn_ns )
2800 i_curr_button = p_button_ptr->right;
2801 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2802 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2803 if ( button_ptr.auto_action_mode )
2805 vlc_mutex_unlock( &p_ev->lock );
2806 vlc_mutex_lock( &p_sys->lock_demuxer );
2808 // process the button action
2809 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2811 vlc_mutex_unlock( &p_sys->lock_demuxer );
2812 vlc_mutex_lock( &p_ev->lock );
2817 case ACTIONID_NAV_UP:
2818 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2820 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2821 if ( p_button_ptr->up > 0 && p_button_ptr->up <= pci->hli.hl_gi.btn_ns )
2823 i_curr_button = p_button_ptr->up;
2824 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2825 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2826 if ( button_ptr.auto_action_mode )
2828 vlc_mutex_unlock( &p_ev->lock );
2829 vlc_mutex_lock( &p_sys->lock_demuxer );
2831 // process the button action
2832 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2834 vlc_mutex_unlock( &p_sys->lock_demuxer );
2835 vlc_mutex_lock( &p_ev->lock );
2840 case ACTIONID_NAV_DOWN:
2841 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2843 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2844 if ( p_button_ptr->down > 0 && p_button_ptr->down <= pci->hli.hl_gi.btn_ns )
2846 i_curr_button = p_button_ptr->down;
2847 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2848 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2849 if ( button_ptr.auto_action_mode )
2851 vlc_mutex_unlock( &p_ev->lock );
2852 vlc_mutex_lock( &p_sys->lock_demuxer );
2854 // process the button action
2855 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2857 vlc_mutex_unlock( &p_sys->lock_demuxer );
2858 vlc_mutex_lock( &p_ev->lock );
2863 case ACTIONID_NAV_ACTIVATE:
2866 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2868 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2870 vlc_mutex_unlock( &p_ev->lock );
2871 vlc_mutex_lock( &p_sys->lock_demuxer );
2873 // process the button action
2874 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2876 vlc_mutex_unlock( &p_sys->lock_demuxer );
2877 vlc_mutex_lock( &p_ev->lock );
2883 p_ev->i_key_action = 0;
2884 vlc_mutex_unlock( &p_ev->lock );
2888 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
2890 vlc_value_t valx, valy;
2892 vlc_mutex_lock( &p_ev->lock );
2893 pci_t *pci = (pci_t *) &p_sys->pci_packet;
2894 var_Get( p_vout, "mouse-x", &valx );
2895 var_Get( p_vout, "mouse-y", &valy );
2897 if( p_ev->b_clicked )
2900 int32_t best,dist,d;
2901 int32_t mx,my,dx,dy;
2903 msg_Dbg( p_ev->p_demux, "Handle Mouse Event: Mouse clicked x(%d)*y(%d)", (unsigned)valx.i_int, (unsigned)valy.i_int);
2906 // get current button
2908 dist = 0x08000000; /* >> than (720*720)+(567*567); */
2909 for(button = 1; button <= pci->hli.hl_gi.btn_ns; button++)
2911 btni_t *button_ptr = &(pci->hli.btnit[button-1]);
2913 if(((unsigned)valx.i_int >= button_ptr->x_start)
2914 && ((unsigned)valx.i_int <= button_ptr->x_end)
2915 && ((unsigned)valy.i_int >= button_ptr->y_start)
2916 && ((unsigned)valy.i_int <= button_ptr->y_end))
2918 mx = (button_ptr->x_start + button_ptr->x_end)/2;
2919 my = (button_ptr->y_start + button_ptr->y_end)/2;
2920 dx = mx - valx.i_int;
2921 dy = my - valy.i_int;
2922 d = (dx*dx) + (dy*dy);
2923 /* If the mouse is within the button and the mouse is closer
2924 * to the center of this button then it is the best choice. */
2934 btni_t button_ptr = pci->hli.btnit[best-1];
2935 uint16 i_curr_button = p_sys->dvd_interpretor.GetSPRM( 0x88 );
2937 msg_Dbg( &p_sys->demuxer, "Clicked button %d", best );
2938 vlc_mutex_unlock( &p_ev->lock );
2939 vlc_mutex_lock( &p_sys->lock_demuxer );
2941 // process the button action
2942 p_sys->dvd_interpretor.SetSPRM( 0x88, best );
2943 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2945 msg_Dbg( &p_sys->demuxer, "Processed button %d", best );
2947 // select new button
2948 if ( best != i_curr_button )
2952 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
2954 vlc_mutex_t *p_mutex = (vlc_mutex_t *) val.p_address;
2957 if(button_ptr.btn_coln != 0) {
2958 i_palette = pci->hli.btn_colit.btn_coli[button_ptr.btn_coln-1][1];
2963 for( int i = 0; i < 4; i++ )
2965 uint32_t i_yuv = 0xFF;//p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
2966 uint8_t i_alpha = (i_palette>>(i*4))&0x0f;
2967 i_alpha = i_alpha == 0xf ? 0xff : i_alpha << 4;
2969 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
2970 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
2971 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
2972 p_sys->palette[i][3] = i_alpha;
2975 vlc_mutex_lock( p_mutex );
2976 val.i_int = button_ptr.x_start; var_Set( p_sys->p_input, "x-start", val );
2977 val.i_int = button_ptr.x_end; var_Set( p_sys->p_input, "x-end", val );
2978 val.i_int = button_ptr.y_start; var_Set( p_sys->p_input, "y-start", val );
2979 val.i_int = button_ptr.y_end; var_Set( p_sys->p_input, "y-end", val );
2981 val.p_address = (void *)p_sys->palette;
2982 var_Set( p_sys->p_input, "menu-palette", val );
2984 val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
2985 vlc_mutex_unlock( p_mutex );
2988 vlc_mutex_unlock( &p_sys->lock_demuxer );
2989 vlc_mutex_lock( &p_ev->lock );
2992 else if( p_ev->b_moved )
2994 // dvdnav_mouse_select( NULL, pci, valx.i_int, valy.i_int );
2997 p_ev->b_moved = false;
2998 p_ev->b_clicked = false;
2999 vlc_mutex_unlock( &p_ev->lock );
3003 if( p_vout && p_vout->b_die )
3005 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3006 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3007 vlc_object_release( p_vout );
3011 else if( p_vout == NULL )
3013 p_vout = (vlc_object_t*) vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
3017 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3018 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3022 /* Wait a bit, 10ms */
3026 /* Release callback */
3029 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3030 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3031 vlc_object_release( p_vout );
3033 var_DelCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
3035 vlc_mutex_destroy( &p_ev->lock );
3040 void matroska_segment_c::UnSelect( )
3044 for( i_track = 0; i_track < tracks.size(); i_track++ )
3046 if ( tracks[i_track]->p_es != NULL )
3048 // es_format_Clean( &tracks[i_track]->fmt );
3049 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
3050 tracks[i_track]->p_es = NULL;
3057 void virtual_segment_c::PrepareChapters( )
3059 if ( linked_segments.size() == 0 )
3062 // !!! should be called only once !!!
3063 matroska_segment_c *p_segment;
3066 // copy editions from the first segment
3067 p_segment = linked_segments[0];
3068 p_editions = &p_segment->stored_editions;
3070 for ( i=1 ; i<linked_segments.size(); i++ )
3072 p_segment = linked_segments[i];
3073 // FIXME assume we have the same editions in all segments
3074 for (j=0; j<p_segment->stored_editions.size(); j++)
3076 if( j >= p_editions->size() ) /* Protect against broken files (?) */
3078 (*p_editions)[j]->Append( *p_segment->stored_editions[j] );
3083 std::string chapter_edition_c::GetMainName() const
3085 if ( sub_chapters.size() )
3087 return sub_chapters[0]->GetCodecName( true );
3092 int chapter_item_c::PublishChapters( input_title_t & title, int & i_user_chapters, int i_level )
3094 // add support for meta-elements from codec like DVD Titles
3095 if ( !b_display_seekpoint || psz_name == "" )
3097 psz_name = GetCodecName();
3098 if ( psz_name != "" )
3099 b_display_seekpoint = true;
3102 if (b_display_seekpoint)
3104 seekpoint_t *sk = vlc_seekpoint_New();
3106 sk->i_level = i_level;
3107 sk->i_time_offset = i_start_time;
3108 sk->psz_name = strdup( psz_name.c_str() );
3110 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
3111 title.i_seekpoint++;
3112 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
3113 title.seekpoint[title.i_seekpoint-1] = sk;
3115 if ( b_user_display )
3119 for ( size_t i=0; i<sub_chapters.size() ; i++)
3121 sub_chapters[i]->PublishChapters( title, i_user_chapters, i_level+1 );
3124 i_seekpoint_num = i_user_chapters;
3126 return i_user_chapters;
3129 bool virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
3131 demux_sys_t & sys = *demux.p_sys;
3132 chapter_item_c *psz_curr_chapter;
3133 bool b_has_seeked = false;
3135 /* update current chapter/seekpoint */
3136 if ( p_editions->size() )
3138 /* 1st, we need to know in which chapter we are */
3139 psz_curr_chapter = (*p_editions)[i_current_edition]->FindTimecode( sys.i_pts, psz_current_chapter );
3141 /* we have moved to a new chapter */
3142 if (psz_curr_chapter != NULL && psz_current_chapter != psz_curr_chapter)
3144 if ( (*p_editions)[i_current_edition]->b_ordered )
3146 // Leave/Enter up to the link point
3147 b_has_seeked = psz_curr_chapter->EnterAndLeave( psz_current_chapter );
3148 if ( !b_has_seeked )
3150 // only physically seek if necessary
3151 if ( psz_current_chapter == NULL || (psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time) )
3152 Seek( demux, sys.i_pts, 0, psz_curr_chapter );
3156 if ( !b_has_seeked )
3158 psz_current_chapter = psz_curr_chapter;
3159 if ( psz_curr_chapter->i_seekpoint_num > 0 )
3161 demux.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
3162 demux.info.i_title = sys.i_current_title = i_sys_title;
3163 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
3169 else if (psz_curr_chapter == NULL)
3171 // out of the scope of the data described by chapters, leave the edition
3172 if ( (*p_editions)[i_current_edition]->b_ordered && psz_current_chapter != NULL )
3174 if ( !(*p_editions)[i_current_edition]->EnterAndLeave( psz_current_chapter, false ) )
3175 psz_current_chapter = NULL;
3184 chapter_item_c *virtual_segment_c::BrowseCodecPrivate( unsigned int codec_id,
3185 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3186 const void *p_cookie,
3187 size_t i_cookie_size )
3189 // FIXME don't assume it is the first edition
3190 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
3191 if ( index != p_editions->end() )
3193 chapter_item_c *p_result = (*index)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3194 if ( p_result != NULL )
3200 chapter_item_c *virtual_segment_c::FindChapter( int64_t i_find_uid )
3202 // FIXME don't assume it is the first edition
3203 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
3204 if ( index != p_editions->end() )
3206 chapter_item_c *p_result = (*index)->FindChapter( i_find_uid );
3207 if ( p_result != NULL )
3213 chapter_item_c *chapter_item_c::BrowseCodecPrivate( unsigned int codec_id,
3214 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3215 const void *p_cookie,
3216 size_t i_cookie_size )
3219 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3220 while ( index != codecs.end() )
3222 if ( match( **index ,p_cookie, i_cookie_size ) )
3228 chapter_item_c *p_result = NULL;
3229 std::vector<chapter_item_c*>::const_iterator index2 = sub_chapters.begin();
3230 while ( index2 != sub_chapters.end() )
3232 p_result = (*index2)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3233 if ( p_result != NULL )
3241 void chapter_item_c::Append( const chapter_item_c & chapter )
3243 // we are appending content for the same chapter UID
3245 chapter_item_c *p_chapter;
3247 for ( i=0; i<chapter.sub_chapters.size(); i++ )
3249 p_chapter = FindChapter( chapter.sub_chapters[i]->i_uid );
3250 if ( p_chapter != NULL )
3252 p_chapter->Append( *chapter.sub_chapters[i] );
3256 sub_chapters.push_back( chapter.sub_chapters[i] );
3260 i_user_start_time = min( i_user_start_time, chapter.i_user_start_time );
3261 i_user_end_time = max( i_user_end_time, chapter.i_user_end_time );
3264 chapter_item_c * chapter_item_c::FindChapter( int64_t i_find_uid )
3267 chapter_item_c *p_result = NULL;
3269 if ( i_uid == i_find_uid )
3272 for ( i=0; i<sub_chapters.size(); i++)
3274 p_result = sub_chapters[i]->FindChapter( i_find_uid );
3275 if ( p_result != NULL )
3281 std::string chapter_item_c::GetCodecName( bool f_for_title ) const
3285 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3286 while ( index != codecs.end() )
3288 result = (*index)->GetCodecName( f_for_title );
3297 std::string dvd_chapter_codec_c::GetCodecName( bool f_for_title ) const
3300 if ( p_private_data->GetSize() >= 3)
3302 const binary* p_data = p_private_data->GetBuffer();
3303 /* if ( p_data[0] == MATROSKA_DVD_LEVEL_TT )
3305 uint16_t i_title = (p_data[1] << 8) + p_data[2];
3307 sprintf( psz_str, " %d ---", i_title );
3308 result = N_("--- DVD Title");
3311 else */ if ( p_data[0] == MATROSKA_DVD_LEVEL_LU )
3314 sprintf( psz_str, " (%c%c) ---", p_data[1], p_data[2] );
3315 result = N_("--- DVD Menu");
3318 else if ( p_data[0] == MATROSKA_DVD_LEVEL_SS && f_for_title )
3320 if ( p_data[1] == 0x00 )
3321 result = N_("First Played");
3322 else if ( p_data[1] == 0xC0 )
3323 result = N_("Video Manager");
3324 else if ( p_data[1] == 0x80 )
3326 uint16_t i_title = (p_data[2] << 8) + p_data[3];
3328 sprintf( psz_str, " %d -----", i_title );
3329 result = N_("----- Title");
3338 int16 chapter_item_c::GetTitleNumber( ) const
3342 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3343 while ( index != codecs.end() )
3345 result = (*index)->GetTitleNumber( );
3354 int16 dvd_chapter_codec_c::GetTitleNumber()
3356 if ( p_private_data->GetSize() >= 3)
3358 const binary* p_data = p_private_data->GetBuffer();
3359 if ( p_data[0] == MATROSKA_DVD_LEVEL_SS )
3361 return int16( (p_data[2] << 8) + p_data[3] );
3367 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter )
3369 demux_sys_t *p_sys = p_demux->p_sys;
3370 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
3371 matroska_segment_c *p_segment = p_vsegment->Segment();
3372 mtime_t i_time_offset = 0;
3376 msg_Dbg( p_demux, "seek request to %"PRId64" (%f%%)", i_date, f_percent );
3377 if( i_date < 0 && f_percent < 0 )
3379 msg_Warn( p_demux, "cannot seek nowhere !" );
3382 if( f_percent > 1.0 )
3384 msg_Warn( p_demux, "cannot seek so far !" );
3388 /* seek without index or without date */
3389 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
3391 if (p_sys->f_duration >= 0)
3393 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
3397 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
3399 msg_Dbg( p_demux, "inacurate way of seeking" );
3400 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
3402 if( p_segment->p_indexes[i_index].i_position >= i_pos)
3407 if( i_index == p_segment->i_index )
3412 i_date = p_segment->p_indexes[i_index].i_time;
3415 if( p_segment->p_indexes[i_index].i_position < i_pos )
3419 msg_Warn( p_demux, "searching for cluster, could take some time" );
3421 /* search a cluster */
3422 while( ( el = p_sys->ep->Get() ) != NULL )
3424 if( MKV_IS_ID( el, KaxCluster ) )
3426 KaxCluster *cluster = (KaxCluster*)el;
3428 /* add it to the index */
3429 p_segment->IndexAppendCluster( cluster );
3431 if( (int64_t)cluster->GetElementPosition() >= i_pos )
3433 p_sys->cluster = cluster;
3444 p_vsegment->Seek( *p_demux, i_date, i_time_offset, psz_chapter );
3447 /*****************************************************************************
3448 * Demux: reads and demuxes data packets
3449 *****************************************************************************
3450 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
3451 *****************************************************************************/
3452 static int Demux( demux_t *p_demux)
3454 demux_sys_t *p_sys = p_demux->p_sys;
3456 vlc_mutex_lock( &p_sys->lock_demuxer );
3458 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
3459 matroska_segment_c *p_segment = p_vsegment->Segment();
3460 if ( p_segment == NULL ) return 0;
3461 int i_block_count = 0;
3466 if ( p_sys->demuxer.b_die )
3469 if( p_sys->i_pts >= p_sys->i_start_pts )
3470 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
3476 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
3478 /* nothing left to read in this ordered edition */
3479 if ( !p_vsegment->SelectNext() )
3481 p_segment->UnSelect( );
3483 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3485 /* switch to the next segment */
3486 p_segment = p_vsegment->Segment();
3487 if ( !p_segment->Select( 0 ) )
3489 msg_Err( p_demux, "Failed to select new segment" );
3496 int64_t i_block_duration = 0;
3497 int64_t i_block_ref1;
3498 int64_t i_block_ref2;
3500 #if LIBMATROSKA_VERSION >= 0x000800
3501 KaxSimpleBlock *simpleblock;
3503 if( p_segment->BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
3505 if( p_segment->BlockGet( block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
3508 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered )
3510 const chapter_item_c *p_chap = p_vsegment->CurrentChapter();
3511 // check if there are more chapters to read
3512 if ( p_chap != NULL )
3514 /* TODO handle successive chapters with the same user_start_time/user_end_time
3515 if ( p_chap->i_user_start_time == p_chap->i_user_start_time )
3516 p_vsegment->SelectNext();
3518 p_sys->i_pts = p_chap->i_user_end_time;
3519 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
3528 msg_Warn( p_demux, "cannot get block EOF?" );
3529 p_segment->UnSelect( );
3531 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3533 /* switch to the next segment */
3534 if ( !p_vsegment->SelectNext() )
3535 // no more segments in this stream
3537 p_segment = p_vsegment->Segment();
3538 if ( !p_segment->Select( 0 ) )
3540 msg_Err( p_demux, "Failed to select new segment" );
3548 #if LIBMATROSKA_VERSION >= 0x000800
3549 if ( simpleblock != NULL )
3550 p_sys->i_pts = (p_sys->i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
3553 p_sys->i_pts = (p_sys->i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
3555 if( p_sys->i_pts >= p_sys->i_start_pts )
3557 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
3559 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
3567 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
3569 /* nothing left to read in this ordered edition */
3570 if ( !p_vsegment->SelectNext() )
3575 p_segment->UnSelect( );
3577 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3579 /* switch to the next segment */
3580 p_segment = p_vsegment->Segment();
3581 if ( !p_segment->Select( 0 ) )
3583 msg_Err( p_demux, "Failed to select new segment" );
3591 #if LIBMATROSKA_VERSION >= 0x000800
3592 BlockDecode( p_demux, block, simpleblock, p_sys->i_pts, i_block_duration, i_block_ref1 >= 0 || i_block_ref2 > 0 );
3594 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration, i_block_ref1 >= 0 || i_block_ref2 > 0 );
3600 // TODO optimize when there is need to leave or when seeking has been called
3601 if( i_block_count > 5 )
3608 vlc_mutex_unlock( &p_sys->lock_demuxer );
3615 /*****************************************************************************
3617 *****************************************************************************/
3618 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_, bool b_owner_ )
3625 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
3627 if( i_size <= 0 || mb_eof )
3632 return stream_Read( s, p_buffer, i_size );
3634 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
3640 case seek_beginning:
3644 i_pos = stream_Size( s ) - i_offset;
3647 i_pos= stream_Tell( s ) + i_offset;
3651 if( i_pos < 0 || i_pos >= stream_Size( s ) )
3658 if( stream_Seek( s, i_pos ) )
3664 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
3668 uint64 vlc_stream_io_callback::getFilePointer( void )
3672 return stream_Tell( s );
3674 void vlc_stream_io_callback::close( void )
3680 /*****************************************************************************
3681 * Ebml Stream parser
3682 *****************************************************************************/
3683 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux )
3690 mi_remain_size[0] = el_start->GetSize();
3692 for( i = 1; i < 6; i++ )
3699 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
3702 EbmlParser::~EbmlParser( void )
3706 for( i = 1; i < mi_level; i++ )
3716 EbmlElement* EbmlParser::UnGet( uint64 i_block_pos, uint64 i_cluster_pos )
3718 if ( mi_user_level > mi_level )
3720 while ( mi_user_level != mi_level )
3722 delete m_el[mi_user_level];
3723 m_el[mi_user_level] = NULL;
3729 if ( m_el[1]->GetElementPosition() == i_cluster_pos )
3731 m_es->I_O().setFilePointer( i_block_pos, seek_beginning );
3732 return (EbmlMaster*) m_el[1];
3736 // seek to the previous Cluster
3737 m_es->I_O().setFilePointer( i_cluster_pos, seek_beginning );
3740 delete m_el[mi_level];
3741 m_el[mi_level] = NULL;
3746 void EbmlParser::Up( void )
3748 if( mi_user_level == mi_level )
3750 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
3756 void EbmlParser::Down( void )
3762 void EbmlParser::Keep( void )
3767 int EbmlParser::GetLevel( void )
3769 return mi_user_level;
3772 void EbmlParser::Reset( demux_t *p_demux )
3774 while ( mi_level > 0)
3776 delete m_el[mi_level];
3777 m_el[mi_level] = NULL;
3780 mi_user_level = mi_level = 1;
3781 #if LIBEBML_VERSION >= 0x000704
3782 // a little faster and cleaner
3783 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
3785 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
3787 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
3790 EbmlElement *EbmlParser::Get( void )
3794 if( mi_user_level != mi_level )
3800 EbmlElement *ret = m_got;
3806 if( m_el[mi_level] )
3808 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
3811 delete m_el[mi_level];
3816 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, mb_dummy != 0, 1 );
3817 // mi_remain_size[mi_level] = m_el[mi_level]->GetSize();
3828 delete m_el[mi_level - 1];
3829 m_got = m_el[mi_level -1] = m_el[mi_level];
3830 m_el[mi_level] = NULL;
3837 else if( m_el[mi_level] == NULL )
3839 fprintf( stderr," m_el[mi_level] == NULL\n" );
3842 return m_el[mi_level];
3846 /*****************************************************************************
3848 * * LoadCues : load the cues element and update index
3850 * * LoadTags : load ... the tags element
3852 * * InformationCreate : create all information, load tags if present
3854 *****************************************************************************/
3855 void matroska_segment_c::LoadCues( )
3857 int64_t i_sav_position = es.I_O().getFilePointer();
3859 EbmlElement *el, *cues;
3861 /* *** Load the cue if found *** */
3862 if( i_cues_position < 0 )
3864 msg_Warn( &sys.demuxer, "no cues/empty cues found->seek won't be precise" );
3866 // IndexAppendCluster( cluster );
3871 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
3875 msg_Dbg( &sys.demuxer, "loading cues" );
3876 es.I_O().setFilePointer( i_cues_position, seek_beginning );
3877 cues = es.FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
3881 msg_Err( &sys.demuxer, "cannot load cues (broken seekhead or file)" );
3882 es.I_O().setFilePointer( i_sav_position, seek_beginning );
3886 ep = new EbmlParser( &es, cues, &sys.demuxer );
3887 while( ( el = ep->Get() ) != NULL )
3889 if( MKV_IS_ID( el, KaxCuePoint ) )
3891 #define idx p_indexes[i_index]
3894 idx.i_block_number= -1;
3895 idx.i_position = -1;
3900 while( ( el = ep->Get() ) != NULL )
3902 if( MKV_IS_ID( el, KaxCueTime ) )
3904 KaxCueTime &ctime = *(KaxCueTime*)el;
3906 ctime.ReadData( es.I_O() );
3908 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
3910 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
3913 while( ( el = ep->Get() ) != NULL )
3915 if( MKV_IS_ID( el, KaxCueTrack ) )
3917 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
3919 ctrack.ReadData( es.I_O() );
3920 idx.i_track = uint16( ctrack );
3922 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
3924 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
3926 ccpos.ReadData( es.I_O() );
3927 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
3929 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
3931 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
3933 cbnum.ReadData( es.I_O() );
3934 idx.i_block_number = uint32( cbnum );
3938 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
3945 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
3951 msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
3952 " track=%d bnum=%d", idx.i_time, idx.i_position,
3953 idx.i_track, idx.i_block_number );
3957 if( i_index >= i_index_max )
3959 i_index_max += 1024;
3960 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
3966 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
3974 msg_Dbg( &sys.demuxer, "loading cues done." );
3975 es.I_O().setFilePointer( i_sav_position, seek_beginning );
3978 void matroska_segment_c::LoadTags( )
3980 int64_t i_sav_position = es.I_O().getFilePointer();
3982 EbmlElement *el, *tags;
3984 msg_Dbg( &sys.demuxer, "loading tags" );
3985 es.I_O().setFilePointer( i_tags_position, seek_beginning );
3986 tags = es.FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
3990 msg_Err( &sys.demuxer, "cannot load tags (broken seekhead or file)" );
3991 es.I_O().setFilePointer( i_sav_position, seek_beginning );
3995 msg_Dbg( &sys.demuxer, "Tags" );
3996 ep = new EbmlParser( &es, tags, &sys.demuxer );
3997 while( ( el = ep->Get() ) != NULL )
3999 if( MKV_IS_ID( el, KaxTag ) )
4001 msg_Dbg( &sys.demuxer, "+ Tag" );
4003 while( ( el = ep->Get() ) != NULL )
4005 if( MKV_IS_ID( el, KaxTagTargets ) )
4007 msg_Dbg( &sys.demuxer, "| + Targets" );
4009 while( ( el = ep->Get() ) != NULL )
4011 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4015 else if( MKV_IS_ID( el, KaxTagGeneral ) )
4017 msg_Dbg( &sys.demuxer, "| + General" );
4019 while( ( el = ep->Get() ) != NULL )
4021 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4025 else if( MKV_IS_ID( el, KaxTagGenres ) )
4027 msg_Dbg( &sys.demuxer, "| + Genres" );
4029 while( ( el = ep->Get() ) != NULL )
4031 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4035 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
4037 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
4039 while( ( el = ep->Get() ) != NULL )
4041 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4045 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
4047 msg_Dbg( &sys.demuxer, "| + Images Specific" );
4049 while( ( el = ep->Get() ) != NULL )
4051 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4055 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
4057 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
4059 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
4061 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
4063 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
4065 msg_Dbg( &sys.demuxer, "| + Multi Date" );
4067 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
4069 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
4071 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
4073 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
4075 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
4077 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
4079 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
4081 msg_Dbg( &sys.demuxer, "| + Multi Title" );
4085 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid( *el ).name() );
4092 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
4098 msg_Dbg( &sys.demuxer, "loading tags done." );
4099 es.I_O().setFilePointer( i_sav_position, seek_beginning );
4102 /*****************************************************************************
4104 *****************************************************************************/
4105 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
4109 int i_upper_level = 0;
4111 msg_Dbg( &sys.demuxer, "| + Seek head" );
4113 /* Master elements */
4114 seekhead->Read( es, seekhead->Generic().Context, i_upper_level, el, true );
4116 for( i = 0; i < seekhead->ListSize(); i++ )
4118 EbmlElement *l = (*seekhead)[i];
4120 if( MKV_IS_ID( l, KaxSeek ) )
4122 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
4123 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
4126 for( j = 0; j < sk->ListSize(); j++ )
4128 EbmlElement *l = (*sk)[j];
4130 if( MKV_IS_ID( l, KaxSeekID ) )
4132 KaxSeekID &sid = *(KaxSeekID*)l;
4133 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
4135 else if( MKV_IS_ID( l, KaxSeekPosition ) )
4137 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
4138 i_pos = uint64( spos );
4142 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
4148 if( id == KaxCues::ClassInfos.GlobalId )
4150 msg_Dbg( &sys.demuxer, "| | | = cues at %"PRId64, i_pos );
4151 i_cues_position = segment->GetGlobalPosition( i_pos );
4153 else if( id == KaxChapters::ClassInfos.GlobalId )
4155 msg_Dbg( &sys.demuxer, "| | | = chapters at %"PRId64, i_pos );
4156 i_chapters_position = segment->GetGlobalPosition( i_pos );
4158 else if( id == KaxTags::ClassInfos.GlobalId )
4160 msg_Dbg( &sys.demuxer, "| | | = tags at %"PRId64, i_pos );
4161 i_tags_position = segment->GetGlobalPosition( i_pos );
4167 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4172 /*****************************************************************************
4174 *****************************************************************************/
4175 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
4178 bool bSupported = true;
4182 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
4184 tk = new mkv_track_t();
4186 /* Init the track */
4187 memset( tk, 0, sizeof( mkv_track_t ) );
4189 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
4190 tk->fmt.psz_language = strdup("English");
4191 tk->fmt.psz_description = NULL;
4193 tk->b_default = true;
4194 tk->b_enabled = true;
4195 tk->b_silent = false;
4196 tk->i_number = tracks.size() - 1;
4197 tk->i_extra_data = 0;
4198 tk->p_extra_data = NULL;
4199 tk->psz_codec = NULL;
4200 tk->i_default_duration = 0;
4201 tk->f_timecodescale = 1.0;
4203 tk->b_inited = false;
4204 tk->i_data_init = 0;
4205 tk->p_data_init = NULL;
4207 tk->psz_codec_name = NULL;
4208 tk->psz_codec_settings = NULL;
4209 tk->psz_codec_info_url = NULL;
4210 tk->psz_codec_download_url = NULL;
4212 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
4213 tk->p_compression_data = NULL;
4215 for( i = 0; i < m->ListSize(); i++ )
4217 EbmlElement *l = (*m)[i];
4219 if( MKV_IS_ID( l, KaxTrackNumber ) )
4221 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
4223 tk->i_number = uint32( tnum );
4224 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
4226 else if( MKV_IS_ID( l, KaxTrackUID ) )
4228 KaxTrackUID &tuid = *(KaxTrackUID*)l;
4230 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
4232 else if( MKV_IS_ID( l, KaxTrackType ) )
4234 const char *psz_type;
4235 KaxTrackType &ttype = *(KaxTrackType*)l;
4237 switch( uint8(ttype) )
4241 tk->fmt.i_cat = AUDIO_ES;
4245 tk->fmt.i_cat = VIDEO_ES;
4247 case track_subtitle:
4248 psz_type = "subtitle";
4249 tk->fmt.i_cat = SPU_ES;
4252 psz_type = "buttons";
4253 tk->fmt.i_cat = SPU_ES;
4256 psz_type = "unknown";
4257 tk->fmt.i_cat = UNKNOWN_ES;
4261 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
4263 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
4265 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
4267 // tk->b_enabled = uint32( fenb );
4268 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
4269 // uint32( fenb ) );
4271 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
4273 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
4275 tk->b_default = uint32( fdef );
4276 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
4278 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
4280 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
4282 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
4284 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
4286 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
4288 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
4290 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
4292 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
4294 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
4296 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
4298 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
4300 tk->i_default_duration = uint64(defd);
4301 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%"PRId64, uint64(defd) );
4303 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
4305 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
4307 tk->f_timecodescale = float( ttcs );
4308 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
4310 else if( MKV_IS_ID( l, KaxTrackName ) )
4312 KaxTrackName &tname = *(KaxTrackName*)l;
4314 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
4315 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
4317 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
4319 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
4321 if ( tk->fmt.psz_language != NULL )
4322 free( tk->fmt.psz_language );
4323 tk->fmt.psz_language = strdup( string( lang ).c_str() );
4324 msg_Dbg( &sys.demuxer,
4325 "| | | + Track Language=`%s'", tk->fmt.psz_language );
4327 else if( MKV_IS_ID( l, KaxCodecID ) )
4329 KaxCodecID &codecid = *(KaxCodecID*)l;
4331 tk->psz_codec = strdup( string( codecid ).c_str() );
4332 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
4334 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
4336 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
4338 tk->i_extra_data = cpriv.GetSize();
4339 if( tk->i_extra_data > 0 )
4341 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
4342 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
4344 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
4346 else if( MKV_IS_ID( l, KaxCodecName ) )
4348 KaxCodecName &cname = *(KaxCodecName*)l;
4350 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
4351 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
4353 else if( MKV_IS_ID( l, KaxContentEncodings ) )
4355 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
4356 MkvTree( sys.demuxer, 3, "Content Encodings" );
4357 if ( cencs->ListSize() > 1 )
4359 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
4362 for( j = 0; j < cencs->ListSize(); j++ )
4364 EbmlElement *l2 = (*cencs)[j];
4365 if( MKV_IS_ID( l2, KaxContentEncoding ) )
4367 MkvTree( sys.demuxer, 4, "Content Encoding" );
4368 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
4369 for( k = 0; k < cenc->ListSize(); k++ )
4371 EbmlElement *l3 = (*cenc)[k];
4372 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
4374 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
4375 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
4377 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
4379 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
4380 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
4382 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
4384 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
4385 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
4387 else if( MKV_IS_ID( l3, KaxContentCompression ) )
4389 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
4390 MkvTree( sys.demuxer, 5, "Content Compression" );
4391 for( n = 0; n < compr->ListSize(); n++ )
4393 EbmlElement *l4 = (*compr)[n];
4394 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
4396 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
4397 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
4398 tk->i_compression_type = uint32( compalg );
4399 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
4400 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
4402 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
4406 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
4408 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
4412 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
4418 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
4424 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
4428 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
4430 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
4432 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
4433 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
4435 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
4437 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
4439 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
4440 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
4442 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
4444 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
4446 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
4447 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
4449 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
4451 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
4453 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
4455 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
4457 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
4459 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
4461 else if( MKV_IS_ID( l, KaxTrackVideo ) )
4463 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
4465 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
4466 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
4468 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
4471 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
4472 tk->fmt.video.i_frame_rate = 1000000;
4474 for( j = 0; j < tkv->ListSize(); j++ )
4476 EbmlElement *l = (*tkv)[j];
4477 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
4479 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
4481 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
4483 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
4485 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
4487 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
4490 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
4492 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
4494 tk->fmt.video.i_width += uint16( vwidth );
4495 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
4497 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
4499 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
4501 tk->fmt.video.i_height += uint16( vheight );
4502 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
4504 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
4506 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
4508 i_display_width = uint16( vwidth );
4509 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
4511 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
4513 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
4515 i_display_height = uint16( vheight );
4516 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
4518 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
4520 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
4522 i_crop_bottom = uint16( cropval );
4523 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
4525 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
4527 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
4529 i_crop_top = uint16( cropval );
4530 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
4532 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
4534 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
4536 i_crop_right = uint16( cropval );
4537 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
4539 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
4541 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
4543 i_crop_left = uint16( cropval );
4544 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
4546 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
4548 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
4550 tk->f_fps = float( vfps );
4551 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
4553 else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
4555 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
4557 i_display_unit = uint8( vdmode );
4558 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
4559 uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
4561 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
4563 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
4565 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
4567 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
4569 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
4571 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
4575 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
4578 if( i_display_height && i_display_width )
4579 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * i_display_width / i_display_height;
4580 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
4582 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
4583 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
4584 tk->fmt.video.i_x_offset = i_crop_left;
4585 tk->fmt.video.i_y_offset = i_crop_top;
4586 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
4587 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
4589 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
4590 we do not support this atm */
4592 else if( MKV_IS_ID( l, KaxTrackAudio ) )
4594 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
4597 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
4599 for( j = 0; j < tka->ListSize(); j++ )
4601 EbmlElement *l = (*tka)[j];
4603 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
4605 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
4607 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
4608 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
4610 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
4612 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
4614 tk->fmt.audio.i_rate = (int)float( afreq );
4615 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
4617 else if( MKV_IS_ID( l, KaxAudioChannels ) )
4619 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
4621 tk->fmt.audio.i_channels = uint8( achan );
4622 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
4624 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
4626 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
4628 tk->fmt.audio.i_bitspersample = uint8( abits );
4629 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
4633 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
4639 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
4640 typeid(*l).name() );
4646 tracks.push_back( tk );
4650 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
4655 /*****************************************************************************
4657 *****************************************************************************/
4658 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
4662 int i_upper_level = 0;
4664 msg_Dbg( &sys.demuxer, "| + Tracks" );
4666 /* Master elements */
4667 tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
4669 for( i = 0; i < tracks->ListSize(); i++ )
4671 EbmlElement *l = (*tracks)[i];
4673 if( MKV_IS_ID( l, KaxTrackEntry ) )
4675 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
4679 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4684 /*****************************************************************************
4686 *****************************************************************************/
4687 void matroska_segment_c::ParseInfo( KaxInfo *info )
4692 int i_upper_level = 0;
4694 msg_Dbg( &sys.demuxer, "| + Information" );
4696 /* Master elements */
4697 m = static_cast<EbmlMaster *>(info);
4698 m->Read( es, info->Generic().Context, i_upper_level, el, true );
4700 for( i = 0; i < m->ListSize(); i++ )
4702 EbmlElement *l = (*m)[i];
4704 if( MKV_IS_ID( l, KaxSegmentUID ) )
4706 if ( p_segment_uid == NULL )
4707 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
4709 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
4711 else if( MKV_IS_ID( l, KaxPrevUID ) )
4713 if ( p_prev_segment_uid == NULL )
4714 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
4716 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
4718 else if( MKV_IS_ID( l, KaxNextUID ) )
4720 if ( p_next_segment_uid == NULL )
4721 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
4723 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
4725 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
4727 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
4729 i_timescale = uint64(tcs);
4731 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%"PRId64,
4734 else if( MKV_IS_ID( l, KaxDuration ) )
4736 KaxDuration &dur = *(KaxDuration*)l;
4738 i_duration = mtime_t( double( dur ) );
4740 msg_Dbg( &sys.demuxer, "| | + Duration=%"PRId64,
4743 else if( MKV_IS_ID( l, KaxMuxingApp ) )
4745 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
4747 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
4749 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
4750 psz_muxing_application );
4752 else if( MKV_IS_ID( l, KaxWritingApp ) )
4754 KaxWritingApp &wapp = *(KaxWritingApp*)l;
4756 psz_writing_application = ToUTF8( UTFstring( wapp ) );
4758 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
4759 psz_writing_application );
4761 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
4763 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
4765 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
4767 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
4768 psz_segment_filename );
4770 else if( MKV_IS_ID( l, KaxTitle ) )
4772 KaxTitle &title = *(KaxTitle*)l;
4774 psz_title = ToUTF8( UTFstring( title ) );
4776 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
4778 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
4780 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
4782 families.push_back( new KaxSegmentFamily(*uid) );
4784 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
4786 #if defined( HAVE_GMTIME_R ) && !defined( __APPLE__ )
4787 else if( MKV_IS_ID( l, KaxDateUTC ) )
4789 KaxDateUTC &date = *(KaxDateUTC*)l;
4794 i_date = date.GetEpochDate();
4795 memset( buffer, 0, 256 );
4796 if( gmtime_r( &i_date, &tmres ) &&
4797 asctime_r( &tmres, buffer ) )
4799 buffer[strlen( buffer)-1]= '\0';
4800 psz_date_utc = strdup( buffer );
4801 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
4805 #if LIBMATROSKA_VERSION >= 0x000704
4806 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
4808 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
4809 chapter_translation_c *p_translate = new chapter_translation_c();
4811 p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
4812 for( j = 0; j < p_trans->ListSize(); j++ )
4814 EbmlElement *l = (*p_trans)[j];
4816 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
4818 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
4820 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
4822 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
4824 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
4826 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
4830 translations.push_back( p_translate );
4835 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4839 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
4840 i_duration = mtime_t(f_dur);
4844 /*****************************************************************************
4846 *****************************************************************************/
4847 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
4851 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
4852 for( i = 0; i < ca->ListSize(); i++ )
4854 EbmlElement *l = (*ca)[i];
4856 if( MKV_IS_ID( l, KaxChapterUID ) )
4858 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
4859 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
4861 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
4863 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
4864 chapters.b_display_seekpoint = uint8( flag ) == 0;
4866 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
4868 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
4870 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
4871 chapters.i_start_time = uint64( start ) / INT64_C(1000);
4873 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
4875 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
4877 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
4878 chapters.i_end_time = uint64( end ) / INT64_C(1000);
4880 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
4882 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
4884 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
4886 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
4887 for( j = 0; j < cd->ListSize(); j++ )
4889 EbmlElement *l= (*cd)[j];
4891 if( MKV_IS_ID( l, KaxChapterString ) )
4895 KaxChapterString &name =*(KaxChapterString*)l;
4896 for (k = 0; k < i_level; k++)
4897 chapters.psz_name += '+';
4898 chapters.psz_name += ' ';
4899 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
4900 chapters.psz_name += psz_tmp_utf8;
4901 chapters.b_user_display = true;
4903 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
4904 free( psz_tmp_utf8 );
4906 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
4908 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
4909 const char *psz = string( lang ).c_str();
4911 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
4913 else if( MKV_IS_ID( l, KaxChapterCountry ) )
4915 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
4916 const char *psz = string( ct ).c_str();
4918 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
4922 else if( MKV_IS_ID( l, KaxChapterProcess ) )
4924 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
4926 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
4927 chapter_codec_cmds_c *p_ccodec = NULL;
4929 for( j = 0; j < cp->ListSize(); j++ )
4931 EbmlElement *k= (*cp)[j];
4933 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
4935 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
4936 if ( uint32(*p_codec_id) == 0 )
4937 p_ccodec = new matroska_script_codec_c( sys );
4938 else if ( uint32(*p_codec_id) == 1 )
4939 p_ccodec = new dvd_chapter_codec_c( sys );
4944 if ( p_ccodec != NULL )
4946 for( j = 0; j < cp->ListSize(); j++ )
4948 EbmlElement *k= (*cp)[j];
4950 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
4952 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
4953 p_ccodec->SetPrivate( *p_private );
4955 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
4957 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
4960 chapters.codecs.push_back( p_ccodec );
4963 else if( MKV_IS_ID( l, KaxChapterAtom ) )
4965 chapter_item_c *new_sub_chapter = new chapter_item_c();
4966 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
4967 new_sub_chapter->psz_parent = &chapters;
4968 chapters.sub_chapters.push_back( new_sub_chapter );
4973 /*****************************************************************************
4975 *****************************************************************************/
4976 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
4979 int i_upper_level = 0;
4981 attachments->Read( es, attachments->Generic().Context, i_upper_level, el, true );
4983 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
4985 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
4987 std::string psz_mime_type = GetChild<KaxMimeType>( *attachedFile );
4988 KaxFileName &file_name = GetChild<KaxFileName>( *attachedFile );
4989 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
4991 attachment_c *new_attachment = new attachment_c();
4993 if( new_attachment )
4995 new_attachment->psz_file_name = ToUTF8( UTFstring( file_name ) );
4996 new_attachment->psz_mime_type = psz_mime_type;
4997 new_attachment->i_size = img_data.GetSize();
4998 new_attachment->p_data = malloc( img_data.GetSize() );
5000 if( new_attachment->p_data )
5002 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
5003 sys.stored_attachments.push_back( new_attachment );
5007 delete new_attachment;
5011 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
5015 /*****************************************************************************
5017 *****************************************************************************/
5018 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
5022 int i_upper_level = 0;
5025 /* Master elements */
5026 chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
5028 for( i = 0; i < chapters->ListSize(); i++ )
5030 EbmlElement *l = (*chapters)[i];
5032 if( MKV_IS_ID( l, KaxEditionEntry ) )
5034 chapter_edition_c *p_edition = new chapter_edition_c();
5036 EbmlMaster *E = static_cast<EbmlMaster *>(l );
5038 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
5039 for( j = 0; j < E->ListSize(); j++ )
5041 EbmlElement *l = (*E)[j];
5043 if( MKV_IS_ID( l, KaxChapterAtom ) )
5045 chapter_item_c *new_sub_chapter = new chapter_item_c();
5046 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
5047 p_edition->sub_chapters.push_back( new_sub_chapter );
5049 else if( MKV_IS_ID( l, KaxEditionUID ) )
5051 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
5053 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
5055 p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
5057 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
5059 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
5060 i_default_edition = stored_editions.size();
5064 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
5067 stored_editions.push_back( p_edition );
5071 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
5075 for( i = 0; i < stored_editions.size(); i++ )
5077 stored_editions[i]->RefreshChapters( );
5080 if ( stored_editions.size() != 0 && stored_editions[i_default_edition]->b_ordered )
5082 /* update the duration of the segment according to the sum of all sub chapters */
5083 i_dur = stored_editions[i_default_edition]->Duration() / INT64_C(1000);
5089 void matroska_segment_c::ParseCluster( )
5094 int i_upper_level = 0;
5096 /* Master elements */
5097 m = static_cast<EbmlMaster *>( cluster );
5098 m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
5100 for( i = 0; i < m->ListSize(); i++ )
5102 EbmlElement *l = (*m)[i];
5104 if( MKV_IS_ID( l, KaxClusterTimecode ) )
5106 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
5108 cluster->InitTimecode( uint64( ctc ), i_timescale );
5113 i_start_time = cluster->GlobalTimecode() / 1000;
5116 /*****************************************************************************
5117 * InformationCreate:
5118 *****************************************************************************/
5119 void matroska_segment_c::InformationCreate( )
5121 sys.meta = vlc_meta_New();
5125 vlc_meta_SetTitle( sys.meta, psz_title );
5129 vlc_meta_SetDate( sys.meta, psz_date_utc );
5132 if( psz_segment_filename )
5134 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5136 if( psz_muxing_application )
5138 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5140 if( psz_writing_application )
5142 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5145 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
5147 // mkv_track_t *tk = tracks[i_track];
5148 // vlc_meta_t *mtk = vlc_meta_New();
5149 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
5153 if( i_tags_position >= 0 )
5157 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
5166 /*****************************************************************************
5168 *****************************************************************************/
5170 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
5172 #define idx p_indexes[i_index]
5174 idx.i_block_number= -1;
5175 idx.i_position = cluster->GetElementPosition();
5180 if( i_index >= i_index_max )
5182 i_index_max += 1024;
5183 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
5188 void chapter_edition_c::RefreshChapters( )
5190 chapter_item_c::RefreshChapters( b_ordered, -1 );
5191 b_display_seekpoint = false;
5194 int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_time )
5196 int64_t i_user_time = i_prev_user_time;
5198 // first the sub-chapters, and then ourself
5199 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
5200 while ( index != sub_chapters.end() )
5202 i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
5208 // the ordered chapters always start at zero
5209 if ( i_prev_user_time == -1 )
5211 if ( i_user_time == -1 )
5213 i_prev_user_time = 0;
5216 i_user_start_time = i_prev_user_time;
5217 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
5219 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
5223 i_user_end_time = i_user_time;
5228 if ( sub_chapters.begin() != sub_chapters.end() )
5229 std::sort( sub_chapters.begin(), sub_chapters.end(), chapter_item_c::CompareTimecode );
5230 i_user_start_time = i_start_time;
5231 if ( i_end_time != -1 )
5232 i_user_end_time = i_end_time;
5233 else if ( i_user_time != -1 )
5234 i_user_end_time = i_user_time;
5236 i_user_end_time = i_user_start_time;
5239 return i_user_end_time;
5242 mtime_t chapter_edition_c::Duration() const
5244 mtime_t i_result = 0;
5246 if ( sub_chapters.size() )
5248 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
5250 i_result = (*index)->i_user_end_time;
5256 chapter_item_c * chapter_edition_c::FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current )
5260 bool b_found_current = false;
5261 return chapter_item_c::FindTimecode( i_timecode, p_current, b_found_current );
5264 chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode, const chapter_item_c * p_current, bool & b_found )
5266 chapter_item_c *psz_result = NULL;
5268 if ( p_current == this )
5271 if ( i_user_timecode >= i_user_start_time &&
5272 ( i_user_timecode < i_user_end_time ||
5273 ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
5275 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
5276 while ( index != sub_chapters.end() && ((p_current == NULL && psz_result == NULL) || (p_current != NULL && (!b_found || psz_result == NULL))))
5278 psz_result = (*index)->FindTimecode( i_user_timecode, p_current, b_found );
5282 if ( psz_result == NULL )
5289 bool chapter_item_c::ParentOf( const chapter_item_c & item ) const
5291 if ( &item == this )
5294 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.begin();
5295 while ( index != sub_chapters.end() )
5297 if ( (*index)->ParentOf( item ) )
5305 void demux_sys_t::PreloadFamily( const matroska_segment_c & of_segment )
5307 for (size_t i=0; i<opened_segments.size(); i++)
5309 opened_segments[i]->PreloadFamily( of_segment );
5312 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
5317 for (size_t i=0; i<families.size(); i++)
5319 for (size_t j=0; j<of_segment.families.size(); j++)
5321 if ( *(families[i]) == *(of_segment.families[j]) )
5329 // preload all the linked segments for all preloaded segments
5330 void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
5332 size_t i_preloaded, i, j;
5333 virtual_segment_c *p_seg;
5335 p_current_segment = VirtualFromSegments( p_segment );
5337 used_segments.push_back( p_current_segment );
5339 // create all the other virtual segments of the family
5342 for ( i=0; i< opened_segments.size(); i++ )
5344 if ( opened_segments[i]->b_preloaded && !IsUsedSegment( *opened_segments[i] ) )
5346 p_seg = VirtualFromSegments( opened_segments[i] );
5347 used_segments.push_back( p_seg );
5351 } while ( i_preloaded ); // worst case: will stop when all segments are found as family related
5353 // publish all editions of all usable segment
5354 for ( i=0; i< used_segments.size(); i++ )
5356 p_seg = used_segments[i];
5357 if ( p_seg->p_editions != NULL )
5359 std::string sz_name;
5360 input_title_t *p_title = vlc_input_title_New();
5361 p_seg->i_sys_title = i;
5364 // TODO use a name for each edition, let the TITLE deal with a codec name
5365 for ( j=0; j<p_seg->p_editions->size(); j++ )
5367 if ( p_title->psz_name == NULL )
5369 sz_name = (*p_seg->p_editions)[j]->GetMainName();
5370 if ( sz_name != "" )
5371 p_title->psz_name = strdup( sz_name.c_str() );
5374 chapter_edition_c *p_edition = (*p_seg->p_editions)[j];
5377 p_edition->PublishChapters( *p_title, i_chapters, 0 );
5380 // create a name if there is none
5381 if ( p_title->psz_name == NULL )
5383 sz_name = N_("Segment");
5385 sprintf( psz_str, " %d", (int)i );
5387 p_title->psz_name = strdup( sz_name.c_str() );
5390 titles.push_back( p_title );
5394 // TODO decide which segment should be first used (VMG for DVD)
5397 bool demux_sys_t::IsUsedSegment( matroska_segment_c &segment ) const
5399 for ( size_t i=0; i< used_segments.size(); i++ )
5401 if ( used_segments[i]->FindUID( *segment.p_segment_uid ) )
5407 virtual_segment_c *demux_sys_t::VirtualFromSegments( matroska_segment_c *p_segment ) const
5409 size_t i_preloaded, i;
5411 virtual_segment_c *p_result = new virtual_segment_c( p_segment );
5413 // fill our current virtual segment with all hard linked segments
5416 for ( i=0; i< opened_segments.size(); i++ )
5418 i_preloaded += p_result->AddSegment( opened_segments[i] );
5420 } while ( i_preloaded ); // worst case: will stop when all segments are found as linked
5424 p_result->PreloadLinked( );
5426 p_result->PrepareChapters( );
5431 bool demux_sys_t::PreparePlayback( virtual_segment_c *p_new_segment )
5433 if ( p_new_segment != NULL && p_new_segment != p_current_segment )
5435 if ( p_current_segment != NULL && p_current_segment->Segment() != NULL )
5436 p_current_segment->Segment()->UnSelect();
5438 p_current_segment = p_new_segment;
5439 i_current_title = p_new_segment->i_sys_title;
5442 p_current_segment->LoadCues();
5443 f_duration = p_current_segment->Duration();
5445 /* add information */
5446 p_current_segment->Segment()->InformationCreate( );
5448 p_current_segment->Segment()->Select( 0 );
5453 void demux_sys_t::JumpTo( virtual_segment_c & vsegment, chapter_item_c * p_chapter )
5455 // if the segment is not part of the current segment, select the new one
5456 if ( &vsegment != p_current_segment )
5458 PreparePlayback( &vsegment );
5461 if ( p_chapter != NULL )
5463 if ( !p_chapter->Enter( true ) )
5465 // jump to the location in the found segment
5466 vsegment.Seek( demuxer, p_chapter->i_user_start_time, -1, p_chapter );
5472 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
5476 if ( p_item_a == NULL || p_item_b == NULL )
5479 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
5480 if ( p_item_b->p_prev_segment_uid != NULL
5481 && *p_tmp == *p_item_b->p_prev_segment_uid )
5484 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
5488 if ( p_item_b->p_segment_uid != NULL
5489 && *p_tmp == *p_item_b->p_segment_uid )
5492 if ( p_item_b->p_prev_segment_uid != NULL
5493 && *p_tmp == *p_item_b->p_prev_segment_uid )
5499 bool matroska_segment_c::Preload( )
5504 EbmlElement *el = NULL;
5506 ep->Reset( &sys.demuxer );
5508 while( ( el = ep->Get() ) != NULL )
5510 if( MKV_IS_ID( el, KaxInfo ) )
5512 ParseInfo( static_cast<KaxInfo*>( el ) );
5514 else if( MKV_IS_ID( el, KaxTracks ) )
5516 ParseTracks( static_cast<KaxTracks*>( el ) );
5517 if ( tracks.size() == 0 )
5519 msg_Err( &sys.demuxer, "No tracks supported" );
5523 else if( MKV_IS_ID( el, KaxSeekHead ) )
5525 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
5527 else if( MKV_IS_ID( el, KaxCues ) )
5529 msg_Dbg( &sys.demuxer, "| + Cues" );
5531 else if( MKV_IS_ID( el, KaxCluster ) )
5533 msg_Dbg( &sys.demuxer, "| + Cluster" );
5535 cluster = (KaxCluster*)el;
5537 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
5541 /* stop parsing the stream */
5544 else if( MKV_IS_ID( el, KaxAttachments ) )
5546 msg_Dbg( &sys.demuxer, "| + Attachments" );
5547 ParseAttachments( static_cast<KaxAttachments*>( el ) );
5549 else if( MKV_IS_ID( el, KaxChapters ) )
5551 msg_Dbg( &sys.demuxer, "| + Chapters" );
5552 ParseChapters( static_cast<KaxChapters*>( el ) );
5554 else if( MKV_IS_ID( el, KaxTag ) )
5556 msg_Dbg( &sys.demuxer, "| + Tags FIXME TODO" );
5560 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid(*el).name() );
5569 matroska_segment_c *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
5571 for (size_t i=0; i<opened_segments.size(); i++)
5573 if ( *opened_segments[i]->p_segment_uid == uid )
5574 return opened_segments[i];
5579 chapter_item_c *demux_sys_t::BrowseCodecPrivate( unsigned int codec_id,
5580 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
5581 const void *p_cookie,
5582 size_t i_cookie_size,
5583 virtual_segment_c * &p_segment_found )
5585 chapter_item_c *p_result = NULL;
5586 for (size_t i=0; i<used_segments.size(); i++)
5588 p_result = used_segments[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
5589 if ( p_result != NULL )
5591 p_segment_found = used_segments[i];
5598 chapter_item_c *demux_sys_t::FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found )
5600 chapter_item_c *p_result = NULL;
5601 for (size_t i=0; i<used_segments.size(); i++)
5603 p_result = used_segments[i]->FindChapter( i_find_uid );
5604 if ( p_result != NULL )
5606 p_segment_found = used_segments[i];
5613 void virtual_segment_c::Sort()
5615 // keep the current segment index
5616 matroska_segment_c *p_segment = linked_segments[i_current_segment];
5618 std::sort( linked_segments.begin(), linked_segments.end(), matroska_segment_c::CompareSegmentUIDs );
5620 for ( i_current_segment=0; i_current_segment<linked_segments.size(); i_current_segment++)
5621 if ( linked_segments[i_current_segment] == p_segment )
5625 size_t virtual_segment_c::AddSegment( matroska_segment_c *p_segment )
5628 // check if it's not already in here
5629 for ( i=0; i<linked_segments.size(); i++ )
5631 if ( linked_segments[i]->p_segment_uid != NULL
5632 && p_segment->p_segment_uid != NULL
5633 && *p_segment->p_segment_uid == *linked_segments[i]->p_segment_uid )
5637 // find possible mates
5638 for ( i=0; i<linked_uids.size(); i++ )
5640 if ( (p_segment->p_segment_uid != NULL && *p_segment->p_segment_uid == linked_uids[i])
5641 || (p_segment->p_prev_segment_uid != NULL && *p_segment->p_prev_segment_uid == linked_uids[i])
5642 || (p_segment->p_next_segment_uid !=NULL && *p_segment->p_next_segment_uid == linked_uids[i]) )
5644 linked_segments.push_back( p_segment );
5646 AppendUID( p_segment->p_prev_segment_uid );
5647 AppendUID( p_segment->p_next_segment_uid );
5655 void virtual_segment_c::PreloadLinked( )
5657 for ( size_t i=0; i<linked_segments.size(); i++ )
5659 linked_segments[i]->Preload( );
5661 i_current_edition = linked_segments[0]->i_default_edition;
5664 mtime_t virtual_segment_c::Duration() const
5667 if ( linked_segments.size() == 0 )
5670 matroska_segment_c *p_last_segment = linked_segments[linked_segments.size()-1];
5671 // p_last_segment->ParseCluster( );
5673 i_duration = p_last_segment->i_start_time / 1000 + p_last_segment->i_duration;
5678 void virtual_segment_c::LoadCues( )
5680 for ( size_t i=0; i<linked_segments.size(); i++ )
5682 linked_segments[i]->LoadCues();
5686 void virtual_segment_c::AppendUID( const EbmlBinary * p_UID )
5688 if ( p_UID == NULL )
5690 if ( p_UID->GetBuffer() == NULL )
5693 for (size_t i=0; i<linked_uids.size(); i++)
5695 if ( *p_UID == linked_uids[i] )
5698 linked_uids.push_back( *(KaxSegmentUID*)(p_UID) );
5701 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset )
5704 #if LIBMATROSKA_VERSION >= 0x000800
5705 KaxSimpleBlock *simpleblock;
5707 int i_track_skipping;
5708 int64_t i_block_duration;
5709 int64_t i_block_ref1;
5710 int64_t i_block_ref2;
5712 int64_t i_seek_position = i_start_pos;
5713 int64_t i_seek_time = i_start_time;
5719 for( ; i_idx < i_index; i_idx++ )
5721 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
5732 i_seek_position = p_indexes[i_idx].i_position;
5733 i_seek_time = p_indexes[i_idx].i_time;
5736 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
5737 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
5739 es.I_O().setFilePointer( i_seek_position, seek_beginning );
5742 ep = new EbmlParser( &es, segment, &sys.demuxer );
5745 sys.i_start_pts = i_date;
5747 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
5749 /* now parse until key frame */
5750 i_track_skipping = 0;
5751 for( i_track = 0; i_track < tracks.size(); i_track++ )
5753 if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
5755 tracks[i_track]->b_search_keyframe = true;
5758 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, i_date );
5762 while( i_track_skipping > 0 )
5764 #if LIBMATROSKA_VERSION >= 0x000800
5765 if( BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
5767 if( BlockGet( block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
5770 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
5776 for( i_track = 0; i_track < tracks.size(); i_track++ )
5778 #if LIBMATROSKA_VERSION >= 0x000800
5779 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
5780 (block && tracks[i_track]->i_number == block->TrackNum()) )
5782 if( tracks[i_track]->i_number == block->TrackNum() )
5789 #if LIBMATROSKA_VERSION >= 0x000800
5791 sys.i_pts = (sys.i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
5794 sys.i_pts = (sys.i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
5796 if( i_track < tracks.size() )
5798 if( sys.i_pts >= sys.i_start_pts )
5800 cluster = static_cast<KaxCluster*>(ep->UnGet( i_block_pos, i_cluster_pos ));
5801 i_track_skipping = 0;
5803 else if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
5805 if( i_block_ref1 == 0 && tracks[i_track]->b_search_keyframe )
5807 tracks[i_track]->b_search_keyframe = false;
5810 if( !tracks[i_track]->b_search_keyframe )
5812 #if LIBMATROSKA_VERSION >= 0x000800
5813 BlockDecode( &sys.demuxer, block, simpleblock, sys.i_pts, 0, i_block_ref1 >= 0 || i_block_ref2 > 0 );
5815 BlockDecode( &sys.demuxer, block, sys.i_pts, 0, i_block_ref1 >= 0 || i_block_ref2 > 0 );
5825 void virtual_segment_c::Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter )
5827 demux_sys_t *p_sys = demuxer.p_sys;
5830 // find the actual time for an ordered edition
5831 if ( psz_chapter == NULL )
5833 if ( Edition() && Edition()->b_ordered )
5835 /* 1st, we need to know in which chapter we are */
5836 psz_chapter = (*p_editions)[i_current_edition]->FindTimecode( i_date, psz_current_chapter );
5840 if ( psz_chapter != NULL )
5842 psz_current_chapter = psz_chapter;
5843 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
5844 if ( psz_chapter->i_seekpoint_num > 0 )
5846 demuxer.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
5847 demuxer.info.i_title = p_sys->i_current_title = i_sys_title;
5848 demuxer.info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
5852 // find the best matching segment
5853 for ( i=0; i<linked_segments.size(); i++ )
5855 if ( i_date < linked_segments[i]->i_start_time )
5862 if ( i_current_segment != i )
5864 linked_segments[i_current_segment]->UnSelect();
5865 linked_segments[i]->Select( i_date );
5866 i_current_segment = i;
5869 linked_segments[i]->Seek( i_date, i_time_offset );
5872 void chapter_codec_cmds_c::AddCommand( const KaxChapterProcessCommand & command )
5876 uint32 codec_time = uint32(-1);
5877 for( i = 0; i < command.ListSize(); i++ )
5879 const EbmlElement *k = command[i];
5881 if( MKV_IS_ID( k, KaxChapterProcessTime ) )
5883 codec_time = uint32( *static_cast<const KaxChapterProcessTime*>( k ) );
5888 for( i = 0; i < command.ListSize(); i++ )
5890 const EbmlElement *k = command[i];
5892 if( MKV_IS_ID( k, KaxChapterProcessData ) )
5894 KaxChapterProcessData *p_data = new KaxChapterProcessData( *static_cast<const KaxChapterProcessData*>( k ) );
5895 switch ( codec_time )
5898 during_cmds.push_back( p_data );
5901 enter_cmds.push_back( p_data );
5904 leave_cmds.push_back( p_data );
5913 bool chapter_item_c::Enter( bool b_do_subs )
5915 bool f_result = false;
5916 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
5917 while ( index != codecs.end() )
5919 f_result |= (*index)->Enter();
5926 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
5927 while ( index_ != sub_chapters.end() )
5929 f_result |= (*index_)->Enter( true );
5936 bool chapter_item_c::Leave( bool b_do_subs )
5938 bool f_result = false;
5939 b_is_leaving = true;
5940 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
5941 while ( index != codecs.end() )
5943 f_result |= (*index)->Leave();
5950 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
5951 while ( index_ != sub_chapters.end() )
5953 f_result |= (*index_)->Leave( true );
5957 b_is_leaving = false;
5961 bool chapter_item_c::EnterAndLeave( chapter_item_c *p_item, bool b_final_enter )
5963 chapter_item_c *p_common_parent = p_item;
5965 // leave, up to a common parent
5966 while ( p_common_parent != NULL && !p_common_parent->ParentOf( *this ) )
5968 if ( !p_common_parent->b_is_leaving && p_common_parent->Leave( false ) )
5970 p_common_parent = p_common_parent->psz_parent;
5973 // enter from the parent to <this>
5974 if ( p_common_parent != NULL )
5978 if ( p_common_parent == this )
5979 return Enter( true );
5981 for ( size_t i = 0; i<p_common_parent->sub_chapters.size(); i++ )
5983 if ( p_common_parent->sub_chapters[i]->ParentOf( *this ) )
5985 p_common_parent = p_common_parent->sub_chapters[i];
5986 if ( p_common_parent != this )
5987 if ( p_common_parent->Enter( false ) )
5996 if ( b_final_enter )
5997 return Enter( true );
6002 bool dvd_chapter_codec_c::Enter()
6004 bool f_result = false;
6005 std::vector<KaxChapterProcessData*>::iterator index = enter_cmds.begin();
6006 while ( index != enter_cmds.end() )
6008 if ( (*index)->GetSize() )
6010 binary *p_data = (*index)->GetBuffer();
6011 size_t i_size = *p_data++;
6012 // avoid reading too much from the buffer
6013 i_size = __MIN( i_size, ((*index)->GetSize() - 1) >> 3 );
6014 for ( ; i_size > 0; i_size--, p_data += 8 )
6016 msg_Dbg( &sys.demuxer, "Matroska DVD enter command" );
6017 f_result |= sys.dvd_interpretor.Interpret( p_data );
6025 bool dvd_chapter_codec_c::Leave()
6027 bool f_result = false;
6028 std::vector<KaxChapterProcessData*>::iterator index = leave_cmds.begin();
6029 while ( index != leave_cmds.end() )
6031 if ( (*index)->GetSize() )
6033 binary *p_data = (*index)->GetBuffer();
6034 size_t i_size = *p_data++;
6035 // avoid reading too much from the buffer
6036 i_size = __MIN( i_size, ((*index)->GetSize() - 1) >> 3 );
6037 for ( ; i_size > 0; i_size--, p_data += 8 )
6039 msg_Dbg( &sys.demuxer, "Matroska DVD leave command" );
6040 f_result |= sys.dvd_interpretor.Interpret( p_data );
6048 // see http://www.dvd-replica.com/DVD/vmcmdset.php for a description of DVD commands
6049 bool dvd_command_interpretor_c::Interpret( const binary * p_command, size_t i_size )
6054 virtual_segment_c *p_segment = NULL;
6055 chapter_item_c *p_chapter = NULL;
6056 bool f_result = false;
6057 uint16 i_command = ( p_command[0] << 8 ) + p_command[1];
6059 // handle register tests if there are some
6060 if ( (i_command & 0xF0) != 0 )
6062 bool b_test_positive = true;//(i_command & CMD_DVD_IF_NOT) == 0;
6063 bool b_test_value = (i_command & CMD_DVD_TEST_VALUE) != 0;
6064 uint8 i_test = i_command & 0x70;
6067 // see http://dvd.sourceforge.net/dvdinfo/vmi.html
6070 switch ( i_command >> 12 )
6073 i_cr1 = p_command[3];
6074 i_cr2 = (p_command[4] << 8) + p_command[5];
6079 i_cr1 = p_command[6];
6080 i_cr2 = p_command[7];
6081 b_test_value = false;
6085 if ( ((p_command[1] >> 4) & 0x7) == 0)
6087 i_cr1 = p_command[2];
6088 i_cr2 = (p_command[6] << 8) + p_command[7];
6092 i_cr1 = p_command[2];
6093 i_cr2 = (p_command[6] << 8) + p_command[7];
6101 i_value = GetPRM( i_cr2 );
6105 case CMD_DVD_IF_GPREG_EQUAL:
6107 msg_Dbg( &sys.demuxer, "IF %s EQUALS %s", GetRegTypeName( false, i_cr1 ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6108 if (!( GetPRM( i_cr1 ) == i_value ))
6110 b_test_positive = false;
6113 case CMD_DVD_IF_GPREG_NOT_EQUAL:
6115 msg_Dbg( &sys.demuxer, "IF %s NOT EQUALS %s", GetRegTypeName( false, i_cr1 ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6116 if (!( GetPRM( i_cr1 ) != i_value ))
6118 b_test_positive = false;
6121 case CMD_DVD_IF_GPREG_INF:
6123 msg_Dbg( &sys.demuxer, "IF %s < %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6124 if (!( GetPRM( i_cr1 ) < i_value ))
6126 b_test_positive = false;
6129 case CMD_DVD_IF_GPREG_INF_EQUAL:
6130 // if inferior or equal
6131 msg_Dbg( &sys.demuxer, "IF %s < %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6132 if (!( GetPRM( i_cr1 ) <= i_value ))
6134 b_test_positive = false;
6137 case CMD_DVD_IF_GPREG_AND:
6139 msg_Dbg( &sys.demuxer, "IF %s & %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6140 if (!( GetPRM( i_cr1 ) & i_value ))
6142 b_test_positive = false;
6145 case CMD_DVD_IF_GPREG_SUP:
6147 msg_Dbg( &sys.demuxer, "IF %s >= %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6148 if (!( GetPRM( i_cr1 ) > i_value ))
6150 b_test_positive = false;
6153 case CMD_DVD_IF_GPREG_SUP_EQUAL:
6154 // if superior or equal
6155 msg_Dbg( &sys.demuxer, "IF %s >= %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6156 if (!( GetPRM( i_cr1 ) >= i_value ))
6158 b_test_positive = false;
6163 if ( !b_test_positive )
6167 // strip the test command
6168 i_command &= 0xFF0F;
6170 switch ( i_command )
6175 msg_Dbg( &sys.demuxer, "NOP" );
6180 msg_Dbg( &sys.demuxer, "Break" );
6184 case CMD_DVD_JUMP_TT:
6186 uint8 i_title = p_command[5];
6187 msg_Dbg( &sys.demuxer, "JumpTT %d", i_title );
6189 // find in the ChapProcessPrivate matching this Title level
6190 p_chapter = sys.BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title), p_segment );
6191 if ( p_segment != NULL )
6193 sys.JumpTo( *p_segment, p_chapter );
6199 case CMD_DVD_CALLSS_VTSM1:
6201 msg_Dbg( &sys.demuxer, "CallSS" );
6203 switch( (p_command[6] & 0xC0) >> 6 ) {
6205 p_type = p_command[5] & 0x0F;
6209 msg_Dbg( &sys.demuxer, "CallSS PGC (rsm_cell %x)", p_command[4]);
6212 msg_Dbg( &sys.demuxer, "CallSS Title Entry (rsm_cell %x)", p_command[4]);
6215 msg_Dbg( &sys.demuxer, "CallSS Root Menu (rsm_cell %x)", p_command[4]);
6218 msg_Dbg( &sys.demuxer, "CallSS Subpicture Menu (rsm_cell %x)", p_command[4]);
6221 msg_Dbg( &sys.demuxer, "CallSS Audio Menu (rsm_cell %x)", p_command[4]);
6224 msg_Dbg( &sys.demuxer, "CallSS Angle Menu (rsm_cell %x)", p_command[4]);
6227 msg_Dbg( &sys.demuxer, "CallSS Chapter Menu (rsm_cell %x)", p_command[4]);
6230 msg_Dbg( &sys.demuxer, "CallSS <unknown> (rsm_cell %x)", p_command[4]);
6233 p_chapter = sys.BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1, p_segment );
6234 if ( p_segment != NULL )
6236 sys.JumpTo( *p_segment, p_chapter );
6241 msg_Dbg( &sys.demuxer, "CallSS VMGM (menu %d, rsm_cell %x)", p_command[5] & 0x0F, p_command[4]);
6244 msg_Dbg( &sys.demuxer, "CallSS VTSM (menu %d, rsm_cell %x)", p_command[5] & 0x0F, p_command[4]);
6247 msg_Dbg( &sys.demuxer, "CallSS VMGM (pgc %d, rsm_cell %x)", (p_command[2] << 8) + p_command[3], p_command[4]);
6252 case CMD_DVD_JUMP_SS:
6254 msg_Dbg( &sys.demuxer, "JumpSS");
6256 switch( (p_command[5] & 0xC0) >> 6 ) {
6258 msg_Dbg( &sys.demuxer, "JumpSS FP");
6261 p_type = p_command[5] & 0x0F;
6265 msg_Dbg( &sys.demuxer, "JumpSS VMGM Title Entry");
6268 msg_Dbg( &sys.demuxer, "JumpSS VMGM Root Menu");
6271 msg_Dbg( &sys.demuxer, "JumpSS VMGM Subpicture Menu");
6274 msg_Dbg( &sys.demuxer, "JumpSS VMGM Audio Menu");
6277 msg_Dbg( &sys.demuxer, "JumpSS VMGM Angle Menu");
6280 msg_Dbg( &sys.demuxer, "JumpSS VMGM Chapter Menu");
6283 msg_Dbg( &sys.demuxer, "JumpSS <unknown>");
6287 p_chapter = sys.BrowseCodecPrivate( 1, MatchIsVMG, NULL, 0, p_segment );
6288 if ( p_segment != NULL )
6290 p_chapter = p_segment->BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1 );
6291 if ( p_chapter != NULL )
6293 sys.JumpTo( *p_segment, p_chapter );
6299 p_type = p_command[5] & 0x0F;
6303 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Title Entry", p_command[4], p_command[3]);
6306 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Root Menu", p_command[4], p_command[3]);
6309 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Subpicture Menu", p_command[4], p_command[3]);
6312 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Audio Menu", p_command[4], p_command[3]);
6315 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Angle Menu", p_command[4], p_command[3]);
6318 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Chapter Menu", p_command[4], p_command[3]);
6321 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) <unknown>", p_command[4], p_command[3]);
6325 p_chapter = sys.BrowseCodecPrivate( 1, MatchVTSMNumber, &p_command[4], 1, p_segment );
6327 if ( p_segment != NULL && p_chapter != NULL )
6329 // find the title in the VTS
6330 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchTitleNumber, &p_command[3], 1 );
6331 if ( p_chapter != NULL )
6333 // find the specified menu in the VTSM
6334 p_chapter = p_segment->BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1 );
6335 if ( p_chapter != NULL )
6337 sys.JumpTo( *p_segment, p_chapter );
6342 msg_Dbg( &sys.demuxer, "Title (%d) does not exist in this VTS", p_command[3] );
6345 msg_Dbg( &sys.demuxer, "DVD Domain VTS (%d) not found", p_command[4] );
6348 msg_Dbg( &sys.demuxer, "JumpSS VMGM (pgc %d)", (p_command[2] << 8) + p_command[3]);
6353 case CMD_DVD_JUMPVTS_PTT:
6355 uint8 i_title = p_command[5];
6356 uint8 i_ptt = p_command[3];
6358 msg_Dbg( &sys.demuxer, "JumpVTS Title (%d) PTT (%d)", i_title, i_ptt);
6360 // find the current VTS content segment
6361 p_chapter = sys.p_current_segment->BrowseCodecPrivate( 1, MatchIsDomain, NULL, 0 );
6362 if ( p_chapter != NULL )
6364 int16 i_curr_title = p_chapter->GetTitleNumber( );
6365 if ( i_curr_title > 0 )
6367 p_chapter = sys.BrowseCodecPrivate( 1, MatchVTSNumber, &i_curr_title, sizeof(i_curr_title), p_segment );
6369 if ( p_segment != NULL && p_chapter != NULL )
6371 // find the title in the VTS
6372 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title) );
6373 if ( p_chapter != NULL )
6375 // find the chapter in the title
6376 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchChapterNumber, &i_ptt, sizeof(i_ptt) );
6377 if ( p_chapter != NULL )
6379 sys.JumpTo( *p_segment, p_chapter );
6384 msg_Dbg( &sys.demuxer, "Title (%d) does not exist in this VTS", i_title );
6387 msg_Dbg( &sys.demuxer, "DVD Domain VTS (%d) not found", i_curr_title );
6390 msg_Dbg( &sys.demuxer, "JumpVTS_PTT command found but not in a VTS(M)");
6393 msg_Dbg( &sys.demuxer, "JumpVTS_PTT command but the DVD domain wasn't found");
6396 case CMD_DVD_SET_GPRMMD:
6398 msg_Dbg( &sys.demuxer, "Set GPRMMD [%d]=%d", (p_command[4] << 8) + p_command[5], (p_command[2] << 8) + p_command[3]);
6400 if ( !SetGPRM( (p_command[4] << 8) + p_command[5], (p_command[2] << 8) + p_command[3] ) )
6401 msg_Dbg( &sys.demuxer, "Set GPRMMD failed" );
6404 case CMD_DVD_LINKPGCN:
6406 uint16 i_pgcn = (p_command[6] << 8) + p_command[7];
6408 msg_Dbg( &sys.demuxer, "Link PGCN(%d)", i_pgcn );
6409 p_chapter = sys.p_current_segment->BrowseCodecPrivate( 1, MatchPgcNumber, &i_pgcn, 2 );
6410 if ( p_chapter != NULL )
6412 if ( !p_chapter->Enter( true ) )
6413 // jump to the location in the found segment
6414 sys.p_current_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
6420 case CMD_DVD_LINKCN:
6422 uint8 i_cn = p_command[7];
6424 p_chapter = sys.p_current_segment->CurrentChapter();
6426 msg_Dbg( &sys.demuxer, "LinkCN (cell %d)", i_cn );
6427 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchCellNumber, &i_cn, 1 );
6428 if ( p_chapter != NULL )
6430 if ( !p_chapter->Enter( true ) )
6431 // jump to the location in the found segment
6432 sys.p_current_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
6438 case CMD_DVD_GOTO_LINE:
6440 msg_Dbg( &sys.demuxer, "GotoLine (%d)", (p_command[6] << 8) + p_command[7] );
6444 case CMD_DVD_SET_HL_BTNN1:
6446 msg_Dbg( &sys.demuxer, "SetHL_BTN (%d)", p_command[4] );
6447 SetSPRM( 0x88, p_command[4] );
6452 msg_Dbg( &sys.demuxer, "unsupported command : %02X %02X %02X %02X %02X %02X %02X %02X"
6468 bool dvd_command_interpretor_c::MatchIsDomain( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6470 return ( data.p_private_data != NULL && data.p_private_data->GetBuffer()[0] == MATROSKA_DVD_LEVEL_SS );
6473 bool dvd_command_interpretor_c::MatchIsVMG( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6475 if ( data.p_private_data == NULL || data.p_private_data->GetSize() < 2 )
6478 return ( data.p_private_data->GetBuffer()[0] == MATROSKA_DVD_LEVEL_SS && data.p_private_data->GetBuffer()[1] == 0xC0);
6481 bool dvd_command_interpretor_c::MatchVTSNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6483 if ( i_cookie_size != 2 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6486 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_SS || data.p_private_data->GetBuffer()[1] != 0x80 )
6489 uint16 i_gtitle = (data.p_private_data->GetBuffer()[2] << 8 ) + data.p_private_data->GetBuffer()[3];
6490 uint16 i_title = *(uint16*)p_cookie;
6492 return (i_gtitle == i_title);
6495 bool dvd_command_interpretor_c::MatchVTSMNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6497 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6500 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_SS || data.p_private_data->GetBuffer()[1] != 0x40 )
6503 uint8 i_gtitle = data.p_private_data->GetBuffer()[3];
6504 uint8 i_title = *(uint8*)p_cookie;
6506 return (i_gtitle == i_title);
6509 bool dvd_command_interpretor_c::MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6511 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6514 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_TT )
6517 uint16 i_gtitle = (data.p_private_data->GetBuffer()[1] << 8 ) + data.p_private_data->GetBuffer()[2];
6518 uint8 i_title = *(uint8*)p_cookie;
6520 return (i_gtitle == i_title);
6523 bool dvd_command_interpretor_c::MatchPgcType( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6525 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 8 )
6528 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PGC )
6531 uint8 i_pgc_type = data.p_private_data->GetBuffer()[3] & 0x0F;
6532 uint8 i_pgc = *(uint8*)p_cookie;
6534 return (i_pgc_type == i_pgc);
6537 bool dvd_command_interpretor_c::MatchPgcNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6539 if ( i_cookie_size != 2 || data.p_private_data == NULL || data.p_private_data->GetSize() < 8 )
6542 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PGC )
6545 uint16 *i_pgc_n = (uint16 *)p_cookie;
6546 uint16 i_pgc_num = (data.p_private_data->GetBuffer()[1] << 8) + data.p_private_data->GetBuffer()[2];
6548 return (i_pgc_num == *i_pgc_n);
6551 bool dvd_command_interpretor_c::MatchChapterNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6553 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 2 )
6556 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PTT )
6559 uint8 i_chapter = data.p_private_data->GetBuffer()[1];
6560 uint8 i_ptt = *(uint8*)p_cookie;
6562 return (i_chapter == i_ptt);
6565 bool dvd_command_interpretor_c::MatchCellNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6567 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 5 )
6570 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_CN )
6573 uint8 *i_cell_n = (uint8 *)p_cookie;
6574 uint8 i_cell_num = data.p_private_data->GetBuffer()[3];
6576 return (i_cell_num == *i_cell_n);
6579 bool matroska_script_codec_c::Enter()
6581 bool f_result = false;
6582 std::vector<KaxChapterProcessData*>::iterator index = enter_cmds.begin();
6583 while ( index != enter_cmds.end() )
6585 if ( (*index)->GetSize() )
6587 msg_Dbg( &sys.demuxer, "Matroska Script enter command" );
6588 f_result |= interpretor.Interpret( (*index)->GetBuffer(), (*index)->GetSize() );
6595 bool matroska_script_codec_c::Leave()
6597 bool f_result = false;
6598 std::vector<KaxChapterProcessData*>::iterator index = leave_cmds.begin();
6599 while ( index != leave_cmds.end() )
6601 if ( (*index)->GetSize() )
6603 msg_Dbg( &sys.demuxer, "Matroska Script leave command" );
6604 f_result |= interpretor.Interpret( (*index)->GetBuffer(), (*index)->GetSize() );
6611 // see http://www.matroska.org/technical/specs/chapters/index.html#mscript
6612 // for a description of existing commands
6613 bool matroska_script_interpretor_c::Interpret( const binary * p_command, size_t i_size )
6615 bool b_result = false;
6617 char *psz_str = (char*) malloc( i_size + 1 );
6618 memcpy( psz_str, p_command, i_size );
6619 psz_str[ i_size ] = '\0';
6621 std::string sz_command = psz_str;
6624 msg_Dbg( &sys.demuxer, "command : %s", sz_command.c_str() );
6626 #if defined(__GNUC__) && (__GNUC__ < 3)
6627 if ( sz_command.compare( CMD_MS_GOTO_AND_PLAY, 0, CMD_MS_GOTO_AND_PLAY.size() ) == 0 )
6629 if ( sz_command.compare( 0, CMD_MS_GOTO_AND_PLAY.size(), CMD_MS_GOTO_AND_PLAY ) == 0 )
6635 for ( i=CMD_MS_GOTO_AND_PLAY.size(); i<sz_command.size(); i++)
6637 if ( sz_command[i] == '(' )
6644 for ( j=i; j<sz_command.size(); j++)
6646 if ( sz_command[j] == ')' )
6653 std::string st = sz_command.substr( i+1, j-i-1 );
6654 int64_t i_chapter_uid = atoi( st.c_str() );
6656 virtual_segment_c *p_segment;
6657 chapter_item_c *p_chapter = sys.FindChapter( i_chapter_uid, p_segment );
6659 if ( p_chapter == NULL )
6660 msg_Dbg( &sys.demuxer, "Chapter %"PRId64" not found", i_chapter_uid);
6663 if ( !p_chapter->EnterAndLeave( sys.p_current_segment->CurrentChapter() ) )
6664 p_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
6672 void demux_sys_t::SwapButtons()
6674 #ifndef WORDS_BIGENDIAN
6675 uint8_t button, i, j;
6677 for( button = 1; button <= pci_packet.hli.hl_gi.btn_ns; button++) {
6678 btni_t *button_ptr = &(pci_packet.hli.btnit[button-1]);
6679 binary *p_data = (binary*) button_ptr;
6681 uint16 i_x_start = ((p_data[0] & 0x3F) << 4 ) + ( p_data[1] >> 4 );
6682 uint16 i_x_end = ((p_data[1] & 0x03) << 8 ) + p_data[2];
6683 uint16 i_y_start = ((p_data[3] & 0x3F) << 4 ) + ( p_data[4] >> 4 );
6684 uint16 i_y_end = ((p_data[4] & 0x03) << 8 ) + p_data[5];
6685 button_ptr->x_start = i_x_start;
6686 button_ptr->x_end = i_x_end;
6687 button_ptr->y_start = i_y_start;
6688 button_ptr->y_end = i_y_end;
6691 for ( i = 0; i<3; i++ )
6693 for ( j = 0; j<2; j++ )
6695 pci_packet.hli.btn_colit.btn_coli[i][j] = U32_AT( &pci_packet.hli.btn_colit.btn_coli[i][j] );