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
38 #include <vlc_plugin.h>
41 # include <time.h> /* time() */
45 #include <vlc_codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
48 #include <vlc_charset.h>
49 #include <vlc_input.h>
50 #include <vlc_demux.h>
63 /* libebml and matroska */
64 #include "ebml/EbmlHead.h"
65 #include "ebml/EbmlSubHead.h"
66 #include "ebml/EbmlStream.h"
67 #include "ebml/EbmlContexts.h"
68 #include "ebml/EbmlVoid.h"
69 #include "ebml/EbmlVersion.h"
70 #include "ebml/StdIOCallback.h"
72 #include "matroska/KaxAttachments.h"
73 #include "matroska/KaxAttached.h"
74 #include "matroska/KaxBlock.h"
75 #include "matroska/KaxBlockData.h"
76 #include "matroska/KaxChapters.h"
77 #include "matroska/KaxCluster.h"
78 #include "matroska/KaxClusterData.h"
79 #include "matroska/KaxContexts.h"
80 #include "matroska/KaxCues.h"
81 #include "matroska/KaxCuesData.h"
82 #include "matroska/KaxInfo.h"
83 #include "matroska/KaxInfoData.h"
84 #include "matroska/KaxSeekHead.h"
85 #include "matroska/KaxSegment.h"
86 #include "matroska/KaxTag.h"
87 #include "matroska/KaxTags.h"
88 #include "matroska/KaxTagMulti.h"
89 #include "matroska/KaxTracks.h"
90 #include "matroska/KaxTrackAudio.h"
91 #include "matroska/KaxTrackVideo.h"
92 #include "matroska/KaxTrackEntryData.h"
93 #include "matroska/KaxContentEncoding.h"
94 #include "matroska/KaxVersion.h"
96 #include "ebml/StdIOCallback.h"
98 #if LIBMATROSKA_VERSION < 0x000706
99 START_LIBMATROSKA_NAMESPACE
100 extern const EbmlSemanticContext MATROSKA_DLL_API KaxMatroska_Context;
101 END_LIBMATROSKA_NAMESPACE
104 #include "vlc_keys.h"
107 #include "mp4/libmp4.h"
113 #define MATROSKA_COMPRESSION_NONE -1
114 #define MATROSKA_COMPRESSION_ZLIB 0
115 #define MATROSKA_COMPRESSION_BLIB 1
116 #define MATROSKA_COMPRESSION_LZOX 2
117 #define MATROSKA_COMPRESSION_HEADER 3
119 #define MKVD_TIMECODESCALE 1000000
121 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
122 #undef ATTRIBUTE_PACKED
123 #undef PRAGMA_PACK_BEGIN
124 #undef PRAGMA_PACK_END
126 #if defined(__GNUC__)
127 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
128 #define ATTRIBUTE_PACKED __attribute__ ((packed))
129 #define PRAGMA_PACK 0
133 #if !defined(ATTRIBUTE_PACKED)
134 #define ATTRIBUTE_PACKED
135 #define PRAGMA_PACK 1
142 /*************************************
143 * taken from libdvdnav / libdvdread
144 **************************************/
147 * DVD Time Information.
153 uint8_t frame_u; /* The two high bits are the frame rate. */
154 } ATTRIBUTE_PACKED dvd_time_t;
160 #ifdef WORDS_BIGENDIAN
161 unsigned char zero : 7; /* 25-31 */
162 unsigned char video_pres_mode_change : 1; /* 24 */
164 unsigned char karaoke_audio_pres_mode_change : 1; /* 23 */
165 unsigned char angle_change : 1;
166 unsigned char subpic_stream_change : 1;
167 unsigned char audio_stream_change : 1;
168 unsigned char pause_on : 1;
169 unsigned char still_off : 1;
170 unsigned char button_select_or_activate : 1;
171 unsigned char resume : 1; /* 16 */
173 unsigned char chapter_menu_call : 1; /* 15 */
174 unsigned char angle_menu_call : 1;
175 unsigned char audio_menu_call : 1;
176 unsigned char subpic_menu_call : 1;
177 unsigned char root_menu_call : 1;
178 unsigned char title_menu_call : 1;
179 unsigned char backward_scan : 1;
180 unsigned char forward_scan : 1; /* 8 */
182 unsigned char next_pg_search : 1; /* 7 */
183 unsigned char prev_or_top_pg_search : 1;
184 unsigned char time_or_chapter_search : 1;
185 unsigned char go_up : 1;
186 unsigned char stop : 1;
187 unsigned char title_play : 1;
188 unsigned char chapter_search_or_play : 1;
189 unsigned char title_or_time_play : 1; /* 0 */
191 unsigned char video_pres_mode_change : 1; /* 24 */
192 unsigned char zero : 7; /* 25-31 */
194 unsigned char resume : 1; /* 16 */
195 unsigned char button_select_or_activate : 1;
196 unsigned char still_off : 1;
197 unsigned char pause_on : 1;
198 unsigned char audio_stream_change : 1;
199 unsigned char subpic_stream_change : 1;
200 unsigned char angle_change : 1;
201 unsigned char karaoke_audio_pres_mode_change : 1; /* 23 */
203 unsigned char forward_scan : 1; /* 8 */
204 unsigned char backward_scan : 1;
205 unsigned char title_menu_call : 1;
206 unsigned char root_menu_call : 1;
207 unsigned char subpic_menu_call : 1;
208 unsigned char audio_menu_call : 1;
209 unsigned char angle_menu_call : 1;
210 unsigned char chapter_menu_call : 1; /* 15 */
212 unsigned char title_or_time_play : 1; /* 0 */
213 unsigned char chapter_search_or_play : 1;
214 unsigned char title_play : 1;
215 unsigned char stop : 1;
216 unsigned char go_up : 1;
217 unsigned char time_or_chapter_search : 1;
218 unsigned char prev_or_top_pg_search : 1;
219 unsigned char next_pg_search : 1; /* 7 */
221 } ATTRIBUTE_PACKED user_ops_t;
224 * Type to store per-command data.
228 } ATTRIBUTE_PACKED vm_cmd_t;
229 #define COMMAND_DATA_SIZE 8
232 * PCI General Information
235 uint32_t nv_pck_lbn; /**< sector address of this nav pack */
236 uint16_t vobu_cat; /**< 'category' of vobu */
237 uint16_t zero1; /**< reserved */
238 user_ops_t vobu_uop_ctl; /**< UOP of vobu */
239 uint32_t vobu_s_ptm; /**< start presentation time of vobu */
240 uint32_t vobu_e_ptm; /**< end presentation time of vobu */
241 uint32_t vobu_se_e_ptm; /**< end ptm of sequence end in vobu */
242 dvd_time_t e_eltm; /**< Cell elapsed time */
244 } ATTRIBUTE_PACKED pci_gi_t;
247 * Non Seamless Angle Information
250 uint32_t nsml_agl_dsta[9]; /**< address of destination vobu in AGL_C#n */
251 } ATTRIBUTE_PACKED nsml_agli_t;
254 * Highlight General Information
256 * For btngrX_dsp_ty the bits have the following meaning:
257 * 000b: normal 4/3 only buttons
258 * XX1b: wide (16/9) buttons
259 * X1Xb: letterbox buttons
260 * 1XXb: pan&scan buttons
263 uint16_t hli_ss; /**< status, only low 2 bits 0: no buttons, 1: different 2: equal 3: eual except for button cmds */
264 uint32_t hli_s_ptm; /**< start ptm of hli */
265 uint32_t hli_e_ptm; /**< end ptm of hli */
266 uint32_t btn_se_e_ptm; /**< end ptm of button select */
267 #ifdef WORDS_BIGENDIAN
268 unsigned char zero1 : 2; /**< reserved */
269 unsigned char btngr_ns : 2; /**< number of button groups 1, 2 or 3 with 36/18/12 buttons */
270 unsigned char zero2 : 1; /**< reserved */
271 unsigned char btngr1_dsp_ty : 3; /**< display type of subpic stream for button group 1 */
272 unsigned char zero3 : 1; /**< reserved */
273 unsigned char btngr2_dsp_ty : 3; /**< display type of subpic stream for button group 2 */
274 unsigned char zero4 : 1; /**< reserved */
275 unsigned char btngr3_dsp_ty : 3; /**< display type of subpic stream for button group 3 */
277 unsigned char btngr1_dsp_ty : 3;
278 unsigned char zero2 : 1;
279 unsigned char btngr_ns : 2;
280 unsigned char zero1 : 2;
281 unsigned char btngr3_dsp_ty : 3;
282 unsigned char zero4 : 1;
283 unsigned char btngr2_dsp_ty : 3;
284 unsigned char zero3 : 1;
286 uint8_t btn_ofn; /**< button offset number range 0-255 */
287 uint8_t btn_ns; /**< number of valid buttons <= 36/18/12 (low 6 bits) */
288 uint8_t nsl_btn_ns; /**< number of buttons selectable by U_BTNNi (low 6 bits) nsl_btn_ns <= btn_ns */
289 uint8_t zero5; /**< reserved */
290 uint8_t fosl_btnn; /**< forcedly selected button (low 6 bits) */
291 uint8_t foac_btnn; /**< forcedly activated button (low 6 bits) */
292 } ATTRIBUTE_PACKED hl_gi_t;
296 * Button Color Information Table
297 * Each entry beeing a 32bit word that contains the color indexs and alpha
298 * values to use. They are all represented by 4 bit number and stored
299 * like this [Ci3, Ci2, Ci1, Ci0, A3, A2, A1, A0]. The actual palette
300 * that the indexes reference is in the PGC.
301 * \todo split the uint32_t into a struct
304 uint32_t btn_coli[3][2]; /**< [button color number-1][select:0/action:1] */
305 } ATTRIBUTE_PACKED btn_colit_t;
310 * NOTE: I've had to change the structure from the disk layout to get
311 * the packing to work with Sun's Forte C compiler.
312 * The 4 and 7 bytes are 'rotated' was: ABC DEF GHIJ is: ABCG DEFH IJ
315 #ifdef WORDS_BIGENDIAN
316 uint32 btn_coln : 2; /**< button color number */
317 uint32 x_start : 10; /**< x start offset within the overlay */
318 uint32 zero1 : 2; /**< reserved */
319 uint32 x_end : 10; /**< x end offset within the overlay */
321 uint32 zero3 : 2; /**< reserved */
322 uint32 up : 6; /**< button index when pressing up */
324 uint32 auto_action_mode : 2; /**< 0: no, 1: activated if selected */
325 uint32 y_start : 10; /**< y start offset within the overlay */
326 uint32 zero2 : 2; /**< reserved */
327 uint32 y_end : 10; /**< y end offset within the overlay */
329 uint32 zero4 : 2; /**< reserved */
330 uint32 down : 6; /**< button index when pressing down */
331 unsigned char zero5 : 2; /**< reserved */
332 unsigned char left : 6; /**< button index when pressing left */
333 unsigned char zero6 : 2; /**< reserved */
334 unsigned char right : 6; /**< button index when pressing right */
347 uint32 auto_action_mode : 2;
351 unsigned char left : 6;
352 unsigned char zero5 : 2;
353 unsigned char right : 6;
354 unsigned char zero6 : 2;
357 } ATTRIBUTE_PACKED btni_t;
360 * Highlight Information
364 btn_colit_t btn_colit;
366 } ATTRIBUTE_PACKED hli_t;
373 nsml_agli_t nsml_agli;
376 } ATTRIBUTE_PACKED pci_t;
382 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
386 * What's between a directory and a filename?
389 #define DIRECTORY_SEPARATOR '\\'
391 #define DIRECTORY_SEPARATOR '/'
394 using namespace LIBMATROSKA_NAMESPACE;
397 /*****************************************************************************
399 *****************************************************************************/
400 static int Open ( vlc_object_t * );
401 static void Close( vlc_object_t * );
404 set_shortname( "Matroska" );
405 set_description( N_("Matroska stream demuxer" ) );
406 set_capability( "demux", 50 );
407 set_callbacks( Open, Close );
408 set_category( CAT_INPUT );
409 set_subcategory( SUBCAT_INPUT_DEMUX );
411 add_bool( "mkv-use-ordered-chapters", 1, NULL,
412 N_("Ordered chapters"),
413 N_("Play ordered chapters as specified in the segment."), true );
415 add_bool( "mkv-use-chapter-codec", 1, NULL,
416 N_("Chapter codecs"),
417 N_("Use chapter codecs found in the segment."), true );
419 add_bool( "mkv-preload-local-dir", 1, NULL,
420 N_("Preload Directory"),
421 N_("Preload matroska files from the same family in the same directory (not good for broken files)."), true );
423 add_bool( "mkv-seek-percent", 0, NULL,
424 N_("Seek based on percent not time"),
425 N_("Seek based on percent not time."), true );
427 add_bool( "mkv-use-dummy", 0, NULL,
428 N_("Dummy Elements"),
429 N_("Read and discard unknown EBML elements (not good for broken files)."), true );
431 add_shortcut( "mka" );
432 add_shortcut( "mkv" );
435 /*****************************************************************************
437 *****************************************************************************/
439 block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) {
440 int result, dstsize, n;
445 d_stream.zalloc = (alloc_func)0;
446 d_stream.zfree = (free_func)0;
447 d_stream.opaque = (voidpf)0;
448 result = inflateInit(&d_stream);
451 msg_Dbg( p_this, "inflateInit() failed. Result: %d", result );
455 d_stream.next_in = (Bytef *)p_in_block->p_buffer;
456 d_stream.avail_in = p_in_block->i_buffer;
458 p_block = block_New( p_this, 0 );
463 p_block = block_Realloc( p_block, 0, n * 1000 );
464 dst = (unsigned char *)p_block->p_buffer;
465 d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
466 d_stream.avail_out = 1000;
467 result = inflate(&d_stream, Z_NO_FLUSH);
468 if( ( result != Z_OK ) && ( result != Z_STREAM_END ) )
470 msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result );
474 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
475 ( result != Z_STREAM_END ) );
477 dstsize = d_stream.total_out;
478 inflateEnd( &d_stream );
480 p_block = block_Realloc( p_block, 0, dstsize );
481 p_block->i_buffer = dstsize;
482 block_Release( p_in_block );
489 * Helper function to print the mkv parse tree
491 static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ... )
496 msg_Err( &demuxer, "too deep tree" );
499 va_start( args, psz_format );
500 static const char psz_foo[] = "| | | | | | | | | |";
501 char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) );
502 strncpy( psz_foo2, psz_foo, 4 * i_level );
503 psz_foo2[ 4 * i_level ] = '+';
504 psz_foo2[ 4 * i_level + 1 ] = ' ';
505 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
506 __msg_GenericVa( VLC_OBJECT(&demuxer),VLC_MSG_DBG, "mkv", psz_foo2, args );
511 /*****************************************************************************
513 *****************************************************************************/
514 class vlc_stream_io_callback: public IOCallback
522 vlc_stream_io_callback( stream_t *, bool );
524 virtual ~vlc_stream_io_callback()
530 virtual uint32 read ( void *p_buffer, size_t i_size);
531 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
532 virtual size_t write ( const void *p_buffer, size_t i_size);
533 virtual uint64 getFilePointer ( void );
534 virtual void close ( void );
537 /*****************************************************************************
539 *****************************************************************************/
543 EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux );
544 virtual ~EbmlParser( void );
548 void Reset( demux_t *p_demux );
549 EbmlElement *Get( void );
551 EbmlElement *UnGet( uint64 i_block_pos, uint64 i_cluster_pos );
553 int GetLevel( void );
558 EbmlElement *m_el[10];
559 int64_t mi_remain_size[10];
569 /*****************************************************************************
570 * Some functions to manipulate memory
571 *****************************************************************************/
572 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
573 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
575 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
578 /*****************************************************************************
579 * definitions of structures and functions used by this plugins
580 *****************************************************************************/
587 unsigned int i_number;
590 uint8_t *p_extra_data;
594 uint64_t i_default_duration;
595 float f_timecodescale;
604 unsigned int i_original_rate;
607 /* data to be send first */
609 uint8_t *p_data_init;
611 /* hack : it's for seek */
612 bool b_search_keyframe;
616 const char *psz_codec_name;
617 const char *psz_codec_settings;
618 const char *psz_codec_info_url;
619 const char *psz_codec_download_url;
621 /* encryption/compression */
622 int i_compression_type;
623 KaxContentCompSettings *p_compression_data;
640 const binary MATROSKA_DVD_LEVEL_SS = 0x30;
641 const binary MATROSKA_DVD_LEVEL_LU = 0x2A;
642 const binary MATROSKA_DVD_LEVEL_TT = 0x28;
643 const binary MATROSKA_DVD_LEVEL_PGC = 0x20;
644 const binary MATROSKA_DVD_LEVEL_PG = 0x18;
645 const binary MATROSKA_DVD_LEVEL_PTT = 0x10;
646 const binary MATROSKA_DVD_LEVEL_CN = 0x08;
648 class chapter_codec_cmds_c
651 chapter_codec_cmds_c( demux_sys_t & demuxer, int codec_id = -1)
652 :p_private_data(NULL)
653 ,i_codec_id( codec_id )
657 virtual ~chapter_codec_cmds_c()
659 delete p_private_data;
660 std::vector<KaxChapterProcessData*>::iterator indexe = enter_cmds.begin();
661 while ( indexe != enter_cmds.end() )
666 std::vector<KaxChapterProcessData*>::iterator indexl = leave_cmds.begin();
667 while ( indexl != leave_cmds.end() )
672 std::vector<KaxChapterProcessData*>::iterator indexd = during_cmds.begin();
673 while ( indexd != during_cmds.end() )
680 void SetPrivate( const KaxChapterProcessPrivate & private_data )
682 p_private_data = new KaxChapterProcessPrivate( private_data );
685 void AddCommand( const KaxChapterProcessCommand & command );
687 /// \return wether the codec has seeked in the files or not
688 virtual bool Enter() { return false; }
689 virtual bool Leave() { return false; }
690 virtual std::string GetCodecName( bool f_for_title = false ) const { return ""; }
691 virtual int16 GetTitleNumber() { return -1; }
693 KaxChapterProcessPrivate *p_private_data;
696 std::vector<KaxChapterProcessData*> enter_cmds;
697 std::vector<KaxChapterProcessData*> during_cmds;
698 std::vector<KaxChapterProcessData*> leave_cmds;
704 class dvd_command_interpretor_c
707 dvd_command_interpretor_c( demux_sys_t & demuxer )
710 memset( p_PRMs, 0, sizeof(p_PRMs) );
711 p_PRMs[ 0x80 + 1 ] = 15;
712 p_PRMs[ 0x80 + 2 ] = 62;
713 p_PRMs[ 0x80 + 3 ] = 1;
714 p_PRMs[ 0x80 + 4 ] = 1;
715 p_PRMs[ 0x80 + 7 ] = 1;
716 p_PRMs[ 0x80 + 8 ] = 1;
717 p_PRMs[ 0x80 + 16 ] = 0xFFFFu;
718 p_PRMs[ 0x80 + 18 ] = 0xFFFFu;
721 bool Interpret( const binary * p_command, size_t i_size = 8 );
723 uint16 GetPRM( size_t index ) const
726 return p_PRMs[ index ];
730 uint16 GetGPRM( size_t index ) const
732 if ( index >= 0 && index < 16 )
733 return p_PRMs[ index ];
737 uint16 GetSPRM( size_t index ) const
739 // 21,22,23 reserved for future use
740 if ( index >= 0x80 && index < 0x95 )
741 return p_PRMs[ index ];
745 bool SetPRM( size_t index, uint16 value )
747 if ( index >= 0 && index < 16 )
749 p_PRMs[ index ] = value;
755 bool SetGPRM( size_t index, uint16 value )
757 if ( index >= 0 && index < 16 )
759 p_PRMs[ index ] = value;
765 bool SetSPRM( size_t index, uint16 value )
767 if ( index > 0x80 && index <= 0x8D && index != 0x8C )
769 p_PRMs[ index ] = value;
776 std::string GetRegTypeName( bool b_value, uint16 value ) const
779 char s_value[6], s_reg_value[6];
780 sprintf( s_value, "%.5d", value );
788 else if ( value < 0x80 )
790 sprintf( s_reg_value, "%.5d", GetPRM( value ) );
794 result += s_reg_value;
799 sprintf( s_reg_value, "%.5d", GetPRM( value ) );
803 result += s_reg_value;
815 // wether it's a comparison on the value or register
816 static const uint16 CMD_DVD_TEST_VALUE = 0x80;
817 static const uint16 CMD_DVD_IF_GPREG_AND = (1 << 4);
818 static const uint16 CMD_DVD_IF_GPREG_EQUAL = (2 << 4);
819 static const uint16 CMD_DVD_IF_GPREG_NOT_EQUAL = (3 << 4);
820 static const uint16 CMD_DVD_IF_GPREG_SUP_EQUAL = (4 << 4);
821 static const uint16 CMD_DVD_IF_GPREG_SUP = (5 << 4);
822 static const uint16 CMD_DVD_IF_GPREG_INF_EQUAL = (6 << 4);
823 static const uint16 CMD_DVD_IF_GPREG_INF = (7 << 4);
825 static const uint16 CMD_DVD_NOP = 0x0000;
826 static const uint16 CMD_DVD_GOTO_LINE = 0x0001;
827 static const uint16 CMD_DVD_BREAK = 0x0002;
829 static const uint16 CMD_DVD_NOP2 = 0x2001;
830 static const uint16 CMD_DVD_LINKPGCN = 0x2004;
831 static const uint16 CMD_DVD_LINKPGN = 0x2006;
832 static const uint16 CMD_DVD_LINKCN = 0x2007;
833 static const uint16 CMD_DVD_JUMP_TT = 0x3002;
834 static const uint16 CMD_DVD_JUMPVTS_TT = 0x3003;
835 static const uint16 CMD_DVD_JUMPVTS_PTT = 0x3005;
836 static const uint16 CMD_DVD_JUMP_SS = 0x3006;
837 static const uint16 CMD_DVD_CALLSS_VTSM1 = 0x3008;
839 static const uint16 CMD_DVD_SET_HL_BTNN2 = 0x4600;
840 static const uint16 CMD_DVD_SET_HL_BTNN_LINKPGCN1 = 0x4604;
841 static const uint16 CMD_DVD_SET_STREAM = 0x5100;
842 static const uint16 CMD_DVD_SET_GPRMMD = 0x5300;
843 static const uint16 CMD_DVD_SET_HL_BTNN1 = 0x5600;
844 static const uint16 CMD_DVD_SET_HL_BTNN_LINKPGCN2 = 0x5604;
845 static const uint16 CMD_DVD_SET_HL_BTNN_LINKCN = 0x5607;
847 static const uint16 CMD_DVD_MOV_SPREG_PREG = 0x6100;
848 static const uint16 CMD_DVD_GPREG_MOV_VALUE = 0x7100;
849 static const uint16 CMD_DVD_SUB_GPREG = 0x7400;
850 static const uint16 CMD_DVD_MULT_GPREG = 0x7500;
851 static const uint16 CMD_DVD_GPREG_DIV_VALUE = 0x7600;
852 static const uint16 CMD_DVD_GPREG_AND_VALUE = 0x7900;
854 // callbacks when browsing inside CodecPrivate
855 static bool MatchIsDomain ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
856 static bool MatchIsVMG ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
857 static bool MatchVTSNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
858 static bool MatchVTSMNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
859 static bool MatchTitleNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
860 static bool MatchPgcType ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
861 static bool MatchPgcNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
862 static bool MatchChapterNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
863 static bool MatchCellNumber ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
866 class dvd_chapter_codec_c : public chapter_codec_cmds_c
869 dvd_chapter_codec_c( demux_sys_t & sys )
870 :chapter_codec_cmds_c( sys, 1 )
875 std::string GetCodecName( bool f_for_title = false ) const;
876 int16 GetTitleNumber();
879 class matroska_script_interpretor_c
882 matroska_script_interpretor_c( demux_sys_t & demuxer )
886 bool Interpret( const binary * p_command, size_t i_size );
889 static const std::string CMD_MS_GOTO_AND_PLAY;
895 const std::string matroska_script_interpretor_c::CMD_MS_GOTO_AND_PLAY = "GotoAndPlay";
898 class matroska_script_codec_c : public chapter_codec_cmds_c
901 matroska_script_codec_c( demux_sys_t & sys )
902 :chapter_codec_cmds_c( sys, 0 )
910 matroska_script_interpretor_c interpretor;
913 class chapter_translation_c
916 chapter_translation_c()
920 ~chapter_translation_c()
925 KaxChapterTranslateID *p_translated;
926 unsigned int codec_id;
927 std::vector<uint64_t> editions;
936 ,i_user_start_time(-1)
939 ,b_display_seekpoint(true)
940 ,b_user_display(false)
945 virtual ~chapter_item_c()
947 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
948 while ( index != codecs.end() )
953 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
954 while ( index_ != sub_chapters.end() )
961 int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time );
962 int PublishChapters( input_title_t & title, int & i_user_chapters, int i_level = 0 );
963 virtual chapter_item_c * FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current, bool & b_found );
964 void Append( const chapter_item_c & edition );
965 chapter_item_c * FindChapter( int64_t i_find_uid );
966 virtual chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
967 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
968 const void *p_cookie,
969 size_t i_cookie_size );
970 std::string GetCodecName( bool f_for_title = false ) const;
971 bool ParentOf( const chapter_item_c & item ) const;
972 int16 GetTitleNumber( ) const;
974 int64_t i_start_time, i_end_time;
975 int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */
976 std::vector<chapter_item_c*> sub_chapters;
979 bool b_display_seekpoint;
981 std::string psz_name;
982 chapter_item_c *psz_parent;
985 std::vector<chapter_codec_cmds_c*> codecs;
987 static bool CompareTimecode( const chapter_item_c * itemA, const chapter_item_c * itemB )
989 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) );
992 bool Enter( bool b_do_subchapters );
993 bool Leave( bool b_do_subchapters );
994 bool EnterAndLeave( chapter_item_c *p_item, bool b_enter = true );
997 class chapter_edition_c : public chapter_item_c
1004 void RefreshChapters( );
1005 mtime_t Duration() const;
1006 std::string GetMainName() const;
1007 chapter_item_c * FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current );
1012 class matroska_segment_c
1015 matroska_segment_c( demux_sys_t & demuxer, EbmlStream & estream )
1018 ,i_timescale(MKVD_TIMECODESCALE)
1021 ,i_cues_position(-1)
1022 ,i_chapters_position(-1)
1023 ,i_tags_position(-1)
1028 ,p_segment_uid(NULL)
1029 ,p_prev_segment_uid(NULL)
1030 ,p_next_segment_uid(NULL)
1034 ,psz_muxing_application(NULL)
1035 ,psz_writing_application(NULL)
1036 ,psz_segment_filename(NULL)
1039 ,i_default_edition(0)
1044 p_indexes = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
1047 virtual ~matroska_segment_c()
1049 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
1051 if ( tracks[i_track]->p_compression_data )
1053 delete tracks[i_track]->p_compression_data;
1055 es_format_Clean( &tracks[i_track]->fmt );
1056 free( tracks[i_track]->p_extra_data );
1057 free( tracks[i_track]->psz_codec );
1058 delete tracks[i_track];
1061 free( psz_writing_application );
1062 free( psz_muxing_application );
1063 free( psz_segment_filename );
1065 free( psz_date_utc );
1070 delete p_segment_uid;
1071 delete p_prev_segment_uid;
1072 delete p_next_segment_uid;
1074 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
1075 while ( index != stored_editions.end() )
1080 std::vector<chapter_translation_c*>::iterator indext = translations.begin();
1081 while ( indext != translations.end() )
1086 std::vector<KaxSegmentFamily*>::iterator indexf = families.begin();
1087 while ( indexf != families.end() )
1094 KaxSegment *segment;
1098 uint64_t i_timescale;
1100 /* duration of the segment */
1102 mtime_t i_start_time;
1105 std::vector<mkv_track_t*> tracks;
1108 int64_t i_cues_position;
1109 int64_t i_chapters_position;
1110 int64_t i_tags_position;
1112 KaxCluster *cluster;
1114 uint64 i_cluster_pos;
1115 int64_t i_start_pos;
1116 KaxSegmentUID *p_segment_uid;
1117 KaxPrevUID *p_prev_segment_uid;
1118 KaxNextUID *p_next_segment_uid;
1123 mkv_index_t *p_indexes;
1126 char *psz_muxing_application;
1127 char *psz_writing_application;
1128 char *psz_segment_filename;
1132 /* !!!!! GCC 3.3 bug on Darwin !!!!! */
1133 /* when you remove this variable the compiler issues an atomicity error */
1134 /* this variable only works when using std::vector<chapter_edition_c> */
1135 std::vector<chapter_edition_c*> stored_editions;
1136 int i_default_edition;
1138 std::vector<chapter_translation_c*> translations;
1139 std::vector<KaxSegmentFamily*> families;
1146 bool PreloadFamily( const matroska_segment_c & segment );
1147 void ParseInfo( KaxInfo *info );
1148 void ParseAttachments( KaxAttachments *attachments );
1149 void ParseChapters( KaxChapters *chapters );
1150 void ParseSeekHead( KaxSeekHead *seekhead );
1151 void ParseTracks( KaxTracks *tracks );
1152 void ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters );
1153 void ParseTrackEntry( KaxTrackEntry *m );
1154 void ParseCluster( );
1155 void IndexAppendCluster( KaxCluster *cluster );
1158 void InformationCreate( );
1159 void Seek( mtime_t i_date, mtime_t i_time_offset );
1160 #if LIBMATROSKA_VERSION >= 0x000800
1161 int BlockGet( KaxBlock * &, KaxSimpleBlock * &, int64_t *, int64_t *, int64_t *);
1163 int BlockGet( KaxBlock * &, int64_t *, int64_t *, int64_t *);
1165 bool Select( mtime_t i_start_time );
1168 static bool CompareSegmentUIDs( const matroska_segment_c * item_a, const matroska_segment_c * item_b );
1171 // class holding hard-linked segment together in the playback order
1172 class virtual_segment_c
1175 virtual_segment_c( matroska_segment_c *p_segment )
1178 ,i_current_segment(0)
1179 ,i_current_edition(-1)
1180 ,psz_current_chapter(NULL)
1182 linked_segments.push_back( p_segment );
1184 AppendUID( p_segment->p_segment_uid );
1185 AppendUID( p_segment->p_prev_segment_uid );
1186 AppendUID( p_segment->p_next_segment_uid );
1190 size_t AddSegment( matroska_segment_c *p_segment );
1191 void PreloadLinked( );
1192 mtime_t Duration( ) const;
1194 void Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter );
1196 inline chapter_edition_c *Edition()
1198 if ( i_current_edition >= 0 && size_t(i_current_edition) < p_editions->size() )
1199 return (*p_editions)[i_current_edition];
1203 matroska_segment_c * Segment() const
1205 if ( linked_segments.size() == 0 || i_current_segment >= linked_segments.size() )
1207 return linked_segments[i_current_segment];
1210 inline chapter_item_c *CurrentChapter() {
1211 return psz_current_chapter;
1216 if ( i_current_segment < linked_segments.size()-1 )
1218 i_current_segment++;
1224 bool FindUID( KaxSegmentUID & uid ) const
1226 for ( size_t i=0; i<linked_uids.size(); i++ )
1228 if ( linked_uids[i] == uid )
1234 bool UpdateCurrentToChapter( demux_t & demux );
1235 void PrepareChapters( );
1237 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
1238 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1239 const void *p_cookie,
1240 size_t i_cookie_size );
1241 chapter_item_c *FindChapter( int64_t i_find_uid );
1243 std::vector<chapter_edition_c*> *p_editions;
1247 std::vector<matroska_segment_c*> linked_segments;
1248 std::vector<KaxSegmentUID> linked_uids;
1249 size_t i_current_segment;
1251 int i_current_edition;
1252 chapter_item_c *psz_current_chapter;
1254 void AppendUID( const EbmlBinary * UID );
1257 class matroska_stream_c
1260 matroska_stream_c( demux_sys_t & demuxer )
1266 virtual ~matroska_stream_c()
1275 std::vector<matroska_segment_c*> segments;
1301 virtual ~attachment_c()
1306 std::string psz_file_name;
1307 std::string psz_mime_type;
1315 demux_sys_t( demux_t & demux )
1322 ,p_current_segment(NULL)
1323 ,dvd_interpretor( *this )
1327 ,b_pci_packet_set(false)
1330 vlc_mutex_init( &lock_demuxer );
1333 virtual ~demux_sys_t()
1337 for ( i=0; i<streams.size(); i++ )
1339 for ( i=0; i<opened_segments.size(); i++ )
1340 delete opened_segments[i];
1341 for ( i=0; i<used_segments.size(); i++ )
1342 delete used_segments[i];
1343 for ( i=0; i<stored_attachments.size(); i++ )
1344 delete stored_attachments[i];
1345 if( meta ) vlc_meta_Delete( meta );
1347 while( titles.size() )
1348 { vlc_input_title_Delete( titles.back() ); titles.pop_back();}
1350 vlc_mutex_destroy( &lock_demuxer );
1357 mtime_t i_start_pts;
1358 mtime_t i_chapter_time;
1362 std::vector<input_title_t*> titles; // matroska editions
1363 size_t i_current_title;
1365 std::vector<matroska_stream_c*> streams;
1366 std::vector<attachment_c*> stored_attachments;
1367 std::vector<matroska_segment_c*> opened_segments;
1368 std::vector<virtual_segment_c*> used_segments;
1369 virtual_segment_c *p_current_segment;
1371 dvd_command_interpretor_c dvd_interpretor;
1373 /* duration of the stream */
1376 matroska_segment_c *FindSegment( const EbmlBinary & uid ) const;
1377 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
1378 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1379 const void *p_cookie,
1380 size_t i_cookie_size,
1381 virtual_segment_c * & p_segment_found );
1382 chapter_item_c *FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found );
1384 void PreloadFamily( const matroska_segment_c & of_segment );
1385 void PreloadLinked( matroska_segment_c *p_segment );
1386 bool PreparePlayback( virtual_segment_c *p_new_segment );
1387 matroska_stream_c *AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial = false );
1388 void JumpTo( virtual_segment_c & p_segment, chapter_item_c * p_chapter );
1390 void StartUiThread();
1391 void StopUiThread();
1393 inline void SwapButtons();
1395 /* for spu variables */
1396 input_thread_t *p_input;
1398 bool b_pci_packet_set;
1399 uint8_t palette[4][4];
1400 vlc_mutex_t lock_demuxer;
1403 event_thread_t *p_ev;
1404 static int EventThread( vlc_object_t *p_this );
1405 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1406 vlc_value_t oldval, vlc_value_t newval, void *p_data );
1407 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1408 vlc_value_t oldval, vlc_value_t newval, void *p_data );
1413 virtual_segment_c *VirtualFromSegments( matroska_segment_c *p_segment ) const;
1414 bool IsUsedSegment( matroska_segment_c &p_segment ) const;
1417 static int Demux ( demux_t * );
1418 static int Control( demux_t *, int, va_list );
1419 static void Seek ( demux_t *, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter );
1421 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
1423 static inline char * ToUTF8( const UTFstring &u )
1425 return strdup( u.GetUTF8().c_str() );
1428 /*****************************************************************************
1429 * Open: initializes matroska demux structures
1430 *****************************************************************************/
1431 static int Open( vlc_object_t * p_this )
1433 demux_t *p_demux = (demux_t*)p_this;
1435 matroska_stream_c *p_stream;
1436 matroska_segment_c *p_segment;
1437 const uint8_t *p_peek;
1438 std::string s_path, s_filename;
1439 vlc_stream_io_callback *p_io_callback;
1440 EbmlStream *p_io_stream;
1442 /* peek the begining */
1443 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
1445 /* is a valid file */
1446 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
1447 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
1449 /* Set the demux function */
1450 p_demux->pf_demux = Demux;
1451 p_demux->pf_control = Control;
1452 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
1454 p_io_callback = new vlc_stream_io_callback( p_demux->s, false );
1455 p_io_stream = new EbmlStream( *p_io_callback );
1457 if( p_io_stream == NULL )
1459 msg_Err( p_demux, "failed to create EbmlStream" );
1460 delete p_io_callback;
1462 return VLC_EGENERIC;
1465 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_io_stream, true );
1466 if( p_stream == NULL )
1468 msg_Err( p_demux, "cannot find KaxSegment" );
1471 p_sys->streams.push_back( p_stream );
1473 p_stream->p_in = p_io_callback;
1474 p_stream->p_es = p_io_stream;
1476 for (size_t i=0; i<p_stream->segments.size(); i++)
1478 p_stream->segments[i]->Preload();
1481 p_segment = p_stream->segments[0];
1482 if( p_segment->cluster != NULL )
1484 msg_Warn( p_demux, "cannot find any cluster, damaged file ?" );
1486 // reset the stream reading to the first cluster of the segment used
1487 p_stream->p_in->setFilePointer( p_segment->cluster->GetElementPosition() );
1490 if (config_GetInt( p_demux, "mkv-preload-local-dir" ))
1492 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
1493 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
1495 // assume it's a regular file
1496 // get the directory path
1497 s_path = p_demux->psz_path;
1498 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
1500 s_path = s_path.substr(0,s_path.length()-1);
1504 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
1506 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
1510 DIR *p_src_dir = utf8_opendir(s_path.c_str());
1512 if (p_src_dir != NULL)
1515 while ((psz_file = utf8_readdir(p_src_dir)) != NULL)
1517 if (strlen(psz_file) > 4)
1519 s_filename = s_path + DIRECTORY_SEPARATOR + psz_file;
1522 if (!strcasecmp(s_filename.c_str(), p_demux->psz_path))
1524 if (!s_filename.compare(p_demux->psz_path))
1528 continue; // don't reuse the original opened file
1531 #if defined(__GNUC__) && (__GNUC__ < 3)
1532 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
1533 !s_filename.compare("mka", s_filename.length() - 3, 3))
1535 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
1536 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
1539 // test wether this file belongs to our family
1540 const uint8_t *p_peek;
1541 bool file_ok = false;
1542 stream_t *p_file_stream = stream_UrlNew(
1544 s_filename.c_str());
1545 /* peek the begining */
1546 if( p_file_stream &&
1547 stream_Peek( p_file_stream, &p_peek, 4 ) >= 4
1548 && p_peek[0] == 0x1a && p_peek[1] == 0x45 &&
1549 p_peek[2] == 0xdf && p_peek[3] == 0xa3 ) file_ok = true;
1553 vlc_stream_io_callback *p_file_io = new vlc_stream_io_callback( p_file_stream, true );
1554 EbmlStream *p_estream = new EbmlStream(*p_file_io);
1556 p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_estream );
1558 if ( p_stream == NULL )
1560 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
1566 p_stream->p_in = p_file_io;
1567 p_stream->p_es = p_estream;
1568 p_sys->streams.push_back( p_stream );
1573 if( p_file_stream ) {
1574 stream_Delete( p_file_stream );
1576 msg_Dbg( p_demux, "the file '%s' cannot be opened", s_filename.c_str() );
1582 closedir( p_src_dir );
1586 p_sys->PreloadFamily( *p_segment );
1589 p_sys->PreloadLinked( p_segment );
1591 if ( !p_sys->PreparePlayback( NULL ) )
1593 msg_Err( p_demux, "cannot use the segment" );
1597 p_sys->StartUiThread();
1603 return VLC_EGENERIC;
1606 /*****************************************************************************
1607 * Close: frees unused data
1608 *****************************************************************************/
1609 static void Close( vlc_object_t *p_this )
1611 demux_t *p_demux = (demux_t*)p_this;
1612 demux_sys_t *p_sys = p_demux->p_sys;
1617 /*****************************************************************************
1619 *****************************************************************************/
1620 static int Control( demux_t *p_demux, int i_query, va_list args )
1622 demux_sys_t *p_sys = p_demux->p_sys;
1629 input_attachment_t ***ppp_attach;
1635 case DEMUX_GET_ATTACHMENTS:
1636 ppp_attach = (input_attachment_t***)va_arg( args, input_attachment_t*** );
1637 pi_int = (int*)va_arg( args, int * );
1639 if( p_sys->stored_attachments.size() <= 0 )
1640 return VLC_EGENERIC;
1642 *pi_int = p_sys->stored_attachments.size();
1643 *ppp_attach = (input_attachment_t**)malloc( sizeof(input_attachment_t**) *
1644 p_sys->stored_attachments.size() );
1645 if( !(*ppp_attach) )
1647 for( i = 0; i < p_sys->stored_attachments.size(); i++ )
1649 attachment_c *a = p_sys->stored_attachments[i];
1650 (*ppp_attach)[i] = vlc_input_attachment_New( a->psz_file_name.c_str(), a->psz_mime_type.c_str(), NULL,
1651 a->p_data, a->i_size );
1655 case DEMUX_GET_META:
1656 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1657 vlc_meta_Merge( p_meta, p_sys->meta );
1660 case DEMUX_GET_LENGTH:
1661 pi64 = (int64_t*)va_arg( args, int64_t * );
1662 if( p_sys->f_duration > 0.0 )
1664 *pi64 = (int64_t)(p_sys->f_duration * 1000);
1667 return VLC_EGENERIC;
1669 case DEMUX_GET_POSITION:
1670 pf = (double*)va_arg( args, double * );
1671 if ( p_sys->f_duration > 0.0 )
1672 *pf = (double)(p_sys->i_pts >= p_sys->i_start_pts ? p_sys->i_pts : p_sys->i_start_pts ) / (1000.0 * p_sys->f_duration);
1675 case DEMUX_SET_POSITION:
1676 f = (double)va_arg( args, double );
1677 Seek( p_demux, -1, f, NULL );
1680 case DEMUX_GET_TIME:
1681 pi64 = (int64_t*)va_arg( args, int64_t * );
1682 *pi64 = p_sys->i_pts;
1685 case DEMUX_GET_TITLE_INFO:
1686 if( p_sys->titles.size() )
1688 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1689 int *pi_int = (int*)va_arg( args, int* );
1691 *pi_int = p_sys->titles.size();
1692 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->titles.size() );
1694 for( size_t i = 0; i < p_sys->titles.size(); i++ )
1696 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->titles[i] );
1701 return VLC_EGENERIC;
1703 case DEMUX_SET_TITLE:
1704 /* TODO handle editions as titles */
1705 i_idx = (int)va_arg( args, int );
1706 if( i_idx < p_sys->used_segments.size() )
1708 p_sys->JumpTo( *p_sys->used_segments[i_idx], NULL );
1711 return VLC_EGENERIC;
1713 case DEMUX_SET_SEEKPOINT:
1714 i_skp = (int)va_arg( args, int );
1716 // TODO change the way it works with the << & >> buttons on the UI (+1/-1 instead of a number)
1717 if( p_sys->titles.size() && i_skp < p_sys->titles[p_sys->i_current_title]->i_seekpoint)
1719 Seek( p_demux, (int64_t)p_sys->titles[p_sys->i_current_title]->seekpoint[i_skp]->i_time_offset, -1, NULL);
1720 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
1721 p_demux->info.i_seekpoint = i_skp;
1724 return VLC_EGENERIC;
1726 case DEMUX_SET_TIME:
1729 return VLC_EGENERIC;
1733 #if LIBMATROSKA_VERSION >= 0x000800
1734 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1736 int matroska_segment_c::BlockGet( KaxBlock * & pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1739 #if LIBMATROSKA_VERSION >= 0x000800
1740 pp_simpleblock = NULL;
1748 EbmlElement *el = NULL;
1752 return VLC_EGENERIC;
1754 #if LIBMATROSKA_VERSION >= 0x000800
1755 if( pp_simpleblock != NULL || ((el = ep->Get()) == NULL && pp_block != NULL) )
1757 if( (el = ep->Get()) == NULL && pp_block != NULL )
1760 /* update the index */
1761 #define idx p_indexes[i_index - 1]
1762 if( i_index > 0 && idx.i_time == -1 )
1764 #if LIBMATROSKA_VERSION >= 0x000800
1765 if ( pp_simpleblock != NULL )
1766 idx.i_time = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
1769 idx.i_time = (*pp_block).GlobalTimecode() / (mtime_t)1000;
1770 idx.b_key = *pi_ref1 == 0 ? true : false;
1776 i_level = ep->GetLevel();
1785 msg_Warn( &sys.demuxer, "EOF" );
1786 return VLC_EGENERIC;
1793 if( MKV_IS_ID( el, KaxCluster ) )
1795 cluster = (KaxCluster*)el;
1796 i_cluster_pos = cluster->GetElementPosition();
1798 /* add it to the index */
1800 ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1802 IndexAppendCluster( cluster );
1805 // reset silent tracks
1806 for (size_t i=0; i<tracks.size(); i++)
1808 tracks[i]->b_silent = false;
1813 else if( MKV_IS_ID( el, KaxCues ) )
1815 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1816 return VLC_EGENERIC;
1820 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1824 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1826 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1828 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1829 cluster->InitTimecode( uint64( ctc ), i_timescale );
1831 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1835 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1837 i_block_pos = el->GetElementPosition();
1840 #if LIBMATROSKA_VERSION >= 0x000800
1841 else if( MKV_IS_ID( el, KaxSimpleBlock ) )
1843 pp_simpleblock = (KaxSimpleBlock*)el;
1845 pp_simpleblock->ReadData( es.I_O() );
1846 pp_simpleblock->SetParent( *cluster );
1851 if( MKV_IS_ID( el, KaxBlock ) )
1853 pp_block = (KaxBlock*)el;
1855 pp_block->ReadData( es.I_O() );
1856 pp_block->SetParent( *cluster );
1860 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1862 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1864 dur.ReadData( es.I_O() );
1865 *pi_duration = uint64( dur );
1867 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1869 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1871 ref.ReadData( es.I_O() );
1874 *pi_ref1 = int64( ref ) * cluster->GlobalTimecodeScale();
1876 else if( *pi_ref2 == 0 )
1878 *pi_ref2 = int64( ref ) * cluster->GlobalTimecodeScale();
1881 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1883 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1884 track_num.ReadData( es.I_O() );
1886 for (size_t i=0; i<tracks.size(); i++)
1888 if ( tracks[i]->i_number == uint32(track_num))
1890 tracks[i]->b_silent = true;
1897 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1898 return VLC_EGENERIC;
1903 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem, size_t offset)
1906 if( !(p_block = block_New( p_demux, i_mem + offset ) ) ) return NULL;
1907 memcpy( p_block->p_buffer + offset, p_mem, i_mem );
1908 //p_block->i_rate = p_input->stream.control.i_rate;
1912 #if LIBMATROSKA_VERSION >= 0x000800
1913 static void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
1914 mtime_t i_pts, mtime_t i_duration, bool f_mandatory )
1916 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1917 mtime_t i_duration, bool f_mandatory )
1920 demux_sys_t *p_sys = p_demux->p_sys;
1921 matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
1927 #define tk p_segment->tracks[i_track]
1928 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1930 #if LIBMATROSKA_VERSION >= 0x000800
1931 if( (block != NULL && tk->i_number == block->TrackNum()) ||
1932 (simpleblock != NULL && tk->i_number == simpleblock->TrackNum()))
1934 if( tk->i_number == block->TrackNum() )
1941 if( i_track >= p_segment->tracks.size() )
1943 msg_Err( p_demux, "invalid track number" );
1946 if( tk->fmt.i_cat != NAV_ES && tk->p_es == NULL )
1948 msg_Err( p_demux, "unknown track number" );
1951 if( i_pts + i_duration < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1953 return; /* discard audio packets that shouldn't be rendered */
1956 if ( tk->fmt.i_cat != NAV_ES )
1958 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1962 tk->b_inited = false;
1968 /* First send init data */
1969 if( !tk->b_inited && tk->i_data_init > 0 )
1973 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
1974 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init, 0 );
1975 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
1977 tk->b_inited = true;
1980 #if LIBMATROSKA_VERSION >= 0x000800
1982 (block != NULL && i < block->NumberFrames()) || (simpleblock != NULL && i < simpleblock->NumberFrames());
1985 for( i = 0; i < block->NumberFrames(); i++ )
1990 #if LIBMATROSKA_VERSION >= 0x000800
1991 if ( simpleblock != NULL )
1993 data = &simpleblock->GetBuffer(i);
1994 // condition when the DTS is correct (keyframe or B frame == NOT P frame)
1995 f_mandatory = simpleblock->IsDiscardable() || simpleblock->IsKeyframe();
1999 data = &block->GetBuffer(i);
2001 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER && tk->p_compression_data != NULL )
2002 p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), tk->p_compression_data->GetSize() );
2004 p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), 0 );
2006 if( p_block == NULL )
2011 #if defined(HAVE_ZLIB_H)
2012 if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB )
2014 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
2018 if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER )
2020 memcpy( p_block->p_buffer, tk->p_compression_data->GetBuffer(), tk->p_compression_data->GetSize() );
2023 if ( tk->fmt.i_cat == NAV_ES )
2025 // TODO handle the start/stop times of this packet
2026 if ( p_sys->b_ui_hooked )
2028 vlc_mutex_lock( &p_sys->p_ev->lock );
2029 memcpy( &p_sys->pci_packet, &p_block->p_buffer[1], sizeof(pci_t) );
2030 p_sys->SwapButtons();
2031 p_sys->b_pci_packet_set = true;
2032 vlc_mutex_unlock( &p_sys->p_ev->lock );
2033 block_Release( p_block );
2037 // correct timestamping when B frames are used
2038 if( tk->fmt.i_cat != VIDEO_ES )
2040 p_block->i_dts = p_block->i_pts = i_pts;
2044 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
2046 // in VFW we have no idea about B frames
2048 p_block->i_dts = i_pts;
2052 p_block->i_pts = i_pts;
2054 p_block->i_dts = p_block->i_pts;
2056 p_block->i_dts = min( i_pts, tk->i_last_dts + (mtime_t)(tk->i_default_duration >> 10));
2057 p_sys->i_pts = p_block->i_dts;
2060 tk->i_last_dts = p_block->i_dts;
2063 msg_Dbg( p_demux, "block i_dts: %"PRId64" / i_pts: %"PRId64, p_block->i_dts, p_block->i_pts);
2065 if( strcmp( tk->psz_codec, "S_VOBSUB" ) )
2067 p_block->i_length = i_duration * 1000;
2070 es_out_Send( p_demux->out, tk->p_es, p_block );
2072 /* use time stamp only for first block */
2079 matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial )
2081 int i_upper_lvl = 0;
2083 EbmlElement *p_l0, *p_l1, *p_l2;
2084 bool b_keep_stream = false, b_keep_segment;
2086 // verify the EBML Header
2087 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
2090 msg_Err( p_demux, "No EBML header found" );
2094 // verify we can read this Segment, we only support Matroska version 1 for now
2095 p_l0->Read(*p_estream, EbmlHead::ClassInfos.Context, i_upper_lvl, p_l0, true);
2097 EDocType doc_type = GetChild<EDocType>(*static_cast<EbmlHead*>(p_l0));
2098 if (std::string(doc_type) != "matroska")
2100 msg_Err( p_demux, "Not a Matroska file : DocType = %s ", std::string(doc_type).c_str());
2104 EDocTypeReadVersion doc_read_version = GetChild<EDocTypeReadVersion>(*static_cast<EbmlHead*>(p_l0));
2105 #if LIBMATROSKA_VERSION >= 0x000800
2106 if (uint64(doc_read_version) > 2)
2108 msg_Err( p_demux, "This matroska file is needs version %"PRId64" and this VLC only supports version 1 & 2", uint64(doc_read_version));
2112 if (uint64(doc_read_version) != 1)
2114 msg_Err( p_demux, "This matroska file is needs version %"PRId64" and this VLC only supports version 1", uint64(doc_read_version));
2122 // find all segments in this file
2123 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFFLL);
2129 matroska_stream_c *p_stream1 = new matroska_stream_c( *this );
2133 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
2136 matroska_segment_c *p_segment1 = new matroska_segment_c( *this, *p_estream );
2137 b_keep_segment = b_initial;
2139 ep = new EbmlParser(p_estream, p_l0, &demuxer );
2140 p_segment1->ep = ep;
2141 p_segment1->segment = (KaxSegment*)p_l0;
2143 while ((p_l1 = ep->Get()))
2145 if (MKV_IS_ID(p_l1, KaxInfo))
2147 // find the families of this segment
2148 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
2150 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
2151 for( i = 0; i < p_info->ListSize(); i++ )
2153 EbmlElement *l = (*p_info)[i];
2155 if( MKV_IS_ID( l, KaxSegmentUID ) )
2157 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
2158 b_keep_segment = (FindSegment( *p_uid ) == NULL);
2159 if ( !b_keep_segment )
2160 break; // this segment is already known
2161 opened_segments.push_back( p_segment1 );
2162 delete p_segment1->p_segment_uid;
2163 p_segment1->p_segment_uid = new KaxSegmentUID(*p_uid);
2165 else if( MKV_IS_ID( l, KaxPrevUID ) )
2167 p_segment1->p_prev_segment_uid = new KaxPrevUID( *static_cast<KaxPrevUID*>(l) );
2169 else if( MKV_IS_ID( l, KaxNextUID ) )
2171 p_segment1->p_next_segment_uid = new KaxNextUID( *static_cast<KaxNextUID*>(l) );
2173 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
2175 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
2176 p_segment1->families.push_back( p_fam );
2182 if ( b_keep_segment )
2184 b_keep_stream = true;
2185 p_stream1->segments.push_back( p_segment1 );
2189 p_segment1->segment = NULL;
2193 if (p_l0->IsFiniteSize() )
2195 p_l0->SkipData(*p_estream, KaxMatroska_Context);
2196 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
2199 p_l0 = p_l0->SkipData(*p_estream, KaxSegment_Context);
2202 if ( !b_keep_stream )
2211 bool matroska_segment_c::Select( mtime_t i_start_time )
2216 msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
2217 sys.b_pci_packet_set = false;
2219 for( i_track = 0; i_track < tracks.size(); i_track++ )
2221 if( tracks[i_track]->fmt.i_cat == UNKNOWN_ES )
2223 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, tracks[i_track]->i_number );
2224 tracks[i_track]->p_es = NULL;
2228 if( !strcmp( tracks[i_track]->psz_codec, "V_MS/VFW/FOURCC" ) )
2230 if( tracks[i_track]->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
2232 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
2233 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2237 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tracks[i_track]->p_extra_data;
2239 tracks[i_track]->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
2240 tracks[i_track]->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
2241 tracks[i_track]->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
2243 tracks[i_track]->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
2244 if( tracks[i_track]->fmt.i_extra > 0 )
2246 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2247 memcpy( tracks[i_track]->fmt.p_extra, &p_bih[1], tracks[i_track]->fmt.i_extra );
2251 else if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG1" ) ||
2252 !strcmp( tracks[i_track]->psz_codec, "V_MPEG2" ) )
2254 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
2256 else if( !strncmp( tracks[i_track]->psz_codec, "V_THEORA", 8 ) )
2258 uint8_t *p_data = tracks[i_track]->p_extra_data;
2259 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'h', 'e', 'o' );
2260 if( tracks[i_track]->i_extra_data >= 4 ) {
2261 if( p_data[0] == 2 ) {
2263 int i_size1 = 0, i_size2 = 0;
2265 /* read size of first header packet */
2266 while( *p_data == 0xFF &&
2267 i < tracks[i_track]->i_extra_data )
2276 msg_Dbg( &sys.demuxer, "first theora header size %d", i_size1 );
2277 /* read size of second header packet */
2278 while( *p_data == 0xFF &&
2279 i < tracks[i_track]->i_extra_data )
2288 int i_size3 = tracks[i_track]->i_extra_data - i - i_size1
2290 msg_Dbg( &sys.demuxer, "second theora header size %d", i_size2 );
2291 msg_Dbg( &sys.demuxer, "third theora header size %d", i_size3 );
2292 tracks[i_track]->fmt.i_extra = i_size1 + i_size2 + i_size3
2294 if( i_size1 > 0 && i_size2 > 0 && i_size3 > 0 ) {
2295 tracks[i_track]->fmt.p_extra =
2296 malloc( tracks[i_track]->fmt.i_extra );
2297 uint8_t *p_out = (uint8_t*)tracks[i_track]->fmt.p_extra;
2298 *p_out++ = (i_size1>>8) & 0xFF;
2299 *p_out++ = i_size1 & 0xFF;
2300 memcpy( p_out, p_data, i_size1 );
2304 *p_out++ = (i_size2>>8) & 0xFF;
2305 *p_out++ = i_size2 & 0xFF;
2306 memcpy( p_out, p_data, i_size2 );
2310 *p_out++ = (i_size3>>8) & 0xFF;
2311 *p_out++ = i_size3 & 0xFF;
2312 memcpy( p_out, p_data, i_size3 );
2318 msg_Err( &sys.demuxer, "inconsistant theora extradata" );
2322 msg_Err( &sys.demuxer, "Wrong number of ogg packets with theora headers (%d)", p_data[0] + 1 );
2326 else if( !strncmp( tracks[i_track]->psz_codec, "V_REAL/RV", 9 ) )
2328 if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV10" ) )
2329 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '1', '0' );
2330 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV20" ) )
2331 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '2', '0' );
2332 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV30" ) )
2333 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '3', '0' );
2334 else if( !strcmp( tracks[i_track]->psz_codec, "V_REAL/RV40" ) )
2335 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'R', 'V', '4', '0' );
2337 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4", 7 ) )
2339 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/MS/V3" ) )
2341 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
2343 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO", 11 ) )
2345 /* A MPEG 4 codec, SP, ASP, AP or AVC */
2346 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO/AVC" ) )
2347 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
2349 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
2350 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2351 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2352 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2355 else if( !strcmp( tracks[i_track]->psz_codec, "V_QUICKTIME" ) )
2357 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
2358 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
2359 tracks[i_track]->p_extra_data,
2360 tracks[i_track]->i_extra_data,
2362 MP4_ReadBoxCommon( p_mp4_stream, p_box );
2363 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
2364 tracks[i_track]->fmt.i_codec = p_box->i_type;
2365 tracks[i_track]->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
2366 tracks[i_track]->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
2367 tracks[i_track]->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
2368 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2369 memcpy( tracks[i_track]->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tracks[i_track]->fmt.i_extra );
2370 MP4_FreeBox_sample_vide( p_box );
2371 stream_Delete( p_mp4_stream );
2373 else if( !strcmp( tracks[i_track]->psz_codec, "A_MS/ACM" ) )
2375 if( tracks[i_track]->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
2377 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
2378 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2382 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tracks[i_track]->p_extra_data;
2384 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tracks[i_track]->fmt.i_codec, NULL );
2386 tracks[i_track]->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
2387 tracks[i_track]->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
2388 tracks[i_track]->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
2389 tracks[i_track]->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
2390 tracks[i_track]->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
2392 tracks[i_track]->fmt.i_extra = GetWLE( &p_wf->cbSize );
2393 if( tracks[i_track]->fmt.i_extra > 0 )
2395 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2396 memcpy( tracks[i_track]->fmt.p_extra, &p_wf[1], tracks[i_track]->fmt.i_extra );
2400 else if( !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L3" ) ||
2401 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L2" ) ||
2402 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L1" ) )
2404 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
2406 else if( !strcmp( tracks[i_track]->psz_codec, "A_AC3" ) )
2408 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2410 else if( !strcmp( tracks[i_track]->psz_codec, "A_DTS" ) )
2412 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2414 else if( !strcmp( tracks[i_track]->psz_codec, "A_FLAC" ) )
2416 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
2417 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2418 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2419 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2421 else if( !strcmp( tracks[i_track]->psz_codec, "A_VORBIS" ) )
2423 int i, i_offset = 1, i_size[3], i_extra;
2426 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
2428 /* Split the 3 headers */
2429 if( tracks[i_track]->p_extra_data[0] != 0x02 )
2430 msg_Err( &sys.demuxer, "invalid vorbis header" );
2432 for( i = 0; i < 2; i++ )
2435 while( i_offset < tracks[i_track]->i_extra_data )
2437 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
2438 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
2442 i_size[0] = __MIN(i_size[0], tracks[i_track]->i_extra_data - i_offset);
2443 i_size[1] = __MIN(i_size[1], tracks[i_track]->i_extra_data -i_offset -i_size[0]);
2444 i_size[2] = tracks[i_track]->i_extra_data - i_offset - i_size[0] - i_size[1];
2446 tracks[i_track]->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
2447 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2448 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra; i_extra = 0;
2449 for( i = 0; i < 3; i++ )
2451 *(p_extra++) = i_size[i] >> 8;
2452 *(p_extra++) = i_size[i] & 0xFF;
2453 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra,
2455 p_extra += i_size[i];
2456 i_extra += i_size[i];
2459 else if( !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
2460 !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
2462 int i_profile, i_srate, sbr = 0;
2463 static unsigned int i_sample_rates[] =
2465 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
2466 16000, 12000, 11025, 8000, 7350, 0, 0, 0
2469 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
2470 /* create data for faad (MP4DecSpecificDescrTag)*/
2472 if( !strcmp( &tracks[i_track]->psz_codec[12], "MAIN" ) )
2476 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC" ) )
2480 else if( !strcmp( &tracks[i_track]->psz_codec[12], "SSR" ) )
2484 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC/SBR" ) )
2494 for( i_srate = 0; i_srate < 13; i_srate++ )
2496 if( i_sample_rates[i_srate] == tracks[i_track]->i_original_rate )
2501 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
2503 tracks[i_track]->fmt.i_extra = sbr ? 5 : 2;
2504 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2505 ((uint8_t*)tracks[i_track]->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
2506 ((uint8_t*)tracks[i_track]->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tracks[i_track]->fmt.audio.i_channels << 3);
2509 int syncExtensionType = 0x2B7;
2511 for (iDSRI=0; iDSRI<13; iDSRI++)
2512 if( i_sample_rates[iDSRI] == tracks[i_track]->fmt.audio.i_rate )
2514 ((uint8_t*)tracks[i_track]->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
2515 ((uint8_t*)tracks[i_track]->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
2516 ((uint8_t*)tracks[i_track]->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
2519 else if( !strcmp( tracks[i_track]->psz_codec, "A_AAC" ) )
2521 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
2522 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2523 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2524 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2526 else if( !strcmp( tracks[i_track]->psz_codec, "A_WAVPACK4" ) )
2528 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'W', 'V', 'P', 'K' );
2529 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2530 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2531 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2533 else if( !strcmp( tracks[i_track]->psz_codec, "A_TTA1" ) )
2535 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'T', 'T', 'A', '1' );
2536 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2537 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2538 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2540 else if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) ||
2541 !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/LIT" ) ||
2542 !strcmp( tracks[i_track]->psz_codec, "A_PCM/FLOAT/IEEE" ) )
2544 if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) )
2546 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
2550 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
2552 tracks[i_track]->fmt.audio.i_blockalign = ( tracks[i_track]->fmt.audio.i_bitspersample + 7 ) / 8 * tracks[i_track]->fmt.audio.i_channels;
2555 /* disabled due to the potential "S_KATE" namespace issue */
2556 else if( !strcmp( tracks[i_track]->psz_codec, "S_KATE" ) )
2558 int i, i_offset = 1, *i_size, i_extra, num_headers, size_so_far;
2561 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'k', 'a', 't', 'e' );
2562 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2564 /* Recover the number of headers to expect */
2565 num_headers = tracks[i_track]->p_extra_data[0]+1;
2566 msg_Dbg( &sys.demuxer, "kate in mkv detected: %d headers in %u bytes",
2567 num_headers, tracks[i_track]->i_extra_data);
2569 /* this won't overflow the stack as is can allocate only 1020 bytes max */
2570 i_size = (int*)alloca(num_headers*sizeof(int));
2572 /* Split the headers */
2574 for( i = 0; i < num_headers-1; i++ )
2577 while( i_offset < tracks[i_track]->i_extra_data )
2579 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
2580 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
2582 msg_Dbg( &sys.demuxer, "kate header %d is %d bytes", i, i_size[i]);
2583 size_so_far += i_size[i];
2585 i_size[num_headers-1] = tracks[i_track]->i_extra_data - (size_so_far+i_offset);
2586 msg_Dbg( &sys.demuxer, "kate last header (%d) is %d bytes", num_headers-1, i_size[num_headers-1]);
2588 tracks[i_track]->fmt.i_extra = 1 + num_headers * 2 + size_so_far + i_size[num_headers-1];
2589 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2591 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra;
2593 *(p_extra++) = num_headers;
2595 for( i = 0; i < num_headers; i++ )
2597 *(p_extra++) = i_size[i] >> 8;
2598 *(p_extra++) = i_size[i] & 0xFF;
2599 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra-1,
2601 p_extra += i_size[i];
2602 i_extra += i_size[i];
2606 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/UTF8" ) )
2608 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
2609 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2611 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/USF" ) )
2613 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
2614 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2615 if( tracks[i_track]->i_extra_data )
2617 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2618 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2619 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2622 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/SSA" ) ||
2623 !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASS" ) ||
2624 !strcmp( tracks[i_track]->psz_codec, "S_SSA" ) ||
2625 !strcmp( tracks[i_track]->psz_codec, "S_ASS" ))
2627 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
2628 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2629 if( tracks[i_track]->i_extra_data )
2631 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2632 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2633 memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2636 else if( !strcmp( tracks[i_track]->psz_codec, "S_VOBSUB" ) )
2638 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
2639 if( tracks[i_track]->i_extra_data )
2642 char *p_buf = (char *)malloc( tracks[i_track]->i_extra_data + 1);
2643 memcpy( p_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
2644 p_buf[tracks[i_track]->i_extra_data] = '\0';
2646 p_start = strstr( p_buf, "size:" );
2647 if( sscanf( p_start, "size: %dx%d",
2648 &tracks[i_track]->fmt.subs.spu.i_original_frame_width, &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) == 2 )
2650 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 );
2654 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
2659 else if( !strcmp( tracks[i_track]->psz_codec, "B_VOBBTN" ) )
2661 tracks[i_track]->fmt.i_cat = NAV_ES;
2666 msg_Err( &sys.demuxer, "unknown codec id=`%s'", tracks[i_track]->psz_codec );
2667 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2669 if( tracks[i_track]->b_default )
2671 tracks[i_track]->fmt.i_priority = 1000;
2674 tracks[i_track]->p_es = es_out_Add( sys.demuxer.out, &tracks[i_track]->fmt );
2676 /* Turn on a subtitles track if it has been flagged as default -
2677 * but only do this if no subtitles track has already been engaged,
2678 * either by an earlier 'default track' (??) or by default
2679 * language choice behaviour.
2681 if( tracks[i_track]->b_default )
2683 es_out_Control( sys.demuxer.out,
2685 tracks[i_track]->p_es );
2688 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, i_start_time );
2691 sys.i_start_pts = i_start_time;
2692 // reset the stream reading to the first cluster of the segment used
2693 es.I_O().setFilePointer( i_start_pos );
2696 ep = new EbmlParser( &es, segment, &sys.demuxer );
2701 void demux_sys_t::StartUiThread()
2705 msg_Dbg( &demuxer, "Starting the UI Hook" );
2707 /* FIXME hack hack hack hack FIXME */
2708 /* Get p_input and create variable */
2709 p_input = (input_thread_t *) vlc_object_find( &demuxer, VLC_OBJECT_INPUT, FIND_PARENT );
2710 var_Create( p_input, "x-start", VLC_VAR_INTEGER );
2711 var_Create( p_input, "y-start", VLC_VAR_INTEGER );
2712 var_Create( p_input, "x-end", VLC_VAR_INTEGER );
2713 var_Create( p_input, "y-end", VLC_VAR_INTEGER );
2714 var_Create( p_input, "color", VLC_VAR_ADDRESS );
2715 var_Create( p_input, "menu-palette", VLC_VAR_ADDRESS );
2716 var_Create( p_input, "highlight", VLC_VAR_BOOL );
2717 var_Create( p_input, "highlight-mutex", VLC_VAR_MUTEX );
2719 /* Now create our event thread catcher */
2720 p_ev = (event_thread_t *) vlc_object_create( &demuxer, sizeof( event_thread_t ) );
2721 p_ev->p_demux = &demuxer;
2722 p_ev->b_die = false;
2723 vlc_mutex_init( &p_ev->lock );
2724 vlc_thread_create( p_ev, "mkv event thread handler", EventThread,
2725 VLC_THREAD_PRIORITY_LOW, false );
2729 void demux_sys_t::StopUiThread()
2733 vlc_object_kill( p_ev );
2734 vlc_thread_join( p_ev );
2735 vlc_object_release( p_ev );
2739 var_Destroy( p_input, "highlight-mutex" );
2740 var_Destroy( p_input, "highlight" );
2741 var_Destroy( p_input, "x-start" );
2742 var_Destroy( p_input, "x-end" );
2743 var_Destroy( p_input, "y-start" );
2744 var_Destroy( p_input, "y-end" );
2745 var_Destroy( p_input, "color" );
2746 var_Destroy( p_input, "menu-palette" );
2748 vlc_object_release( p_input );
2750 msg_Dbg( &demuxer, "Stopping the UI Hook" );
2752 b_ui_hooked = false;
2755 int demux_sys_t::EventMouse( vlc_object_t *p_this, char const *psz_var,
2756 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2758 event_thread_t *p_ev = (event_thread_t *) p_data;
2759 vlc_mutex_lock( &p_ev->lock );
2760 if( psz_var[6] == 'c' )
2762 p_ev->b_clicked = true;
2763 msg_Dbg( p_this, "Event Mouse: clicked");
2765 else if( psz_var[6] == 'm' )
2766 p_ev->b_moved = true;
2767 vlc_mutex_unlock( &p_ev->lock );
2772 int demux_sys_t::EventKey( vlc_object_t *p_this, char const *,
2773 vlc_value_t, vlc_value_t newval, void *p_data )
2775 event_thread_t *p_ev = (event_thread_t *) p_data;
2776 vlc_mutex_lock( &p_ev->lock );
2777 p_ev->i_key_action = newval.i_int;
2778 vlc_mutex_unlock( &p_ev->lock );
2779 msg_Dbg( p_this, "Event Key");
2784 int demux_sys_t::EventThread( vlc_object_t *p_this )
2786 event_thread_t *p_ev = (event_thread_t*)p_this;
2787 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
2788 vlc_object_t *p_vout = NULL;
2790 p_ev->b_moved = false;
2791 p_ev->b_clicked = false;
2792 p_ev->i_key_action = 0;
2794 /* catch all key event */
2795 var_AddCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
2798 while( !p_ev->b_die )
2800 if ( !p_sys->b_pci_packet_set )
2807 bool b_activated = false;
2810 if( p_ev->i_key_action )
2814 msg_Dbg( p_ev->p_demux, "Handle Key Event");
2816 vlc_mutex_lock( &p_ev->lock );
2818 pci_t *pci = (pci_t *) &p_sys->pci_packet;
2820 uint16 i_curr_button = p_sys->dvd_interpretor.GetSPRM( 0x88 );
2822 switch( p_ev->i_key_action )
2824 case ACTIONID_NAV_LEFT:
2825 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2827 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2828 if ( p_button_ptr->left > 0 && p_button_ptr->left <= pci->hli.hl_gi.btn_ns )
2830 i_curr_button = p_button_ptr->left;
2831 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2832 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2833 if ( button_ptr.auto_action_mode )
2835 vlc_mutex_unlock( &p_ev->lock );
2836 vlc_mutex_lock( &p_sys->lock_demuxer );
2838 // process the button action
2839 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2841 vlc_mutex_unlock( &p_sys->lock_demuxer );
2842 vlc_mutex_lock( &p_ev->lock );
2847 case ACTIONID_NAV_RIGHT:
2848 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2850 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2851 if ( p_button_ptr->right > 0 && p_button_ptr->right <= pci->hli.hl_gi.btn_ns )
2853 i_curr_button = p_button_ptr->right;
2854 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2855 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2856 if ( button_ptr.auto_action_mode )
2858 vlc_mutex_unlock( &p_ev->lock );
2859 vlc_mutex_lock( &p_sys->lock_demuxer );
2861 // process the button action
2862 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2864 vlc_mutex_unlock( &p_sys->lock_demuxer );
2865 vlc_mutex_lock( &p_ev->lock );
2870 case ACTIONID_NAV_UP:
2871 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2873 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2874 if ( p_button_ptr->up > 0 && p_button_ptr->up <= pci->hli.hl_gi.btn_ns )
2876 i_curr_button = p_button_ptr->up;
2877 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2878 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2879 if ( button_ptr.auto_action_mode )
2881 vlc_mutex_unlock( &p_ev->lock );
2882 vlc_mutex_lock( &p_sys->lock_demuxer );
2884 // process the button action
2885 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2887 vlc_mutex_unlock( &p_sys->lock_demuxer );
2888 vlc_mutex_lock( &p_ev->lock );
2893 case ACTIONID_NAV_DOWN:
2894 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2896 btni_t *p_button_ptr = &(pci->hli.btnit[i_curr_button-1]);
2897 if ( p_button_ptr->down > 0 && p_button_ptr->down <= pci->hli.hl_gi.btn_ns )
2899 i_curr_button = p_button_ptr->down;
2900 p_sys->dvd_interpretor.SetSPRM( 0x88, i_curr_button );
2901 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2902 if ( button_ptr.auto_action_mode )
2904 vlc_mutex_unlock( &p_ev->lock );
2905 vlc_mutex_lock( &p_sys->lock_demuxer );
2907 // process the button action
2908 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2910 vlc_mutex_unlock( &p_sys->lock_demuxer );
2911 vlc_mutex_lock( &p_ev->lock );
2916 case ACTIONID_NAV_ACTIVATE:
2919 if ( i_curr_button > 0 && i_curr_button <= pci->hli.hl_gi.btn_ns )
2921 btni_t button_ptr = pci->hli.btnit[i_curr_button-1];
2923 vlc_mutex_unlock( &p_ev->lock );
2924 vlc_mutex_lock( &p_sys->lock_demuxer );
2926 // process the button action
2927 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2929 vlc_mutex_unlock( &p_sys->lock_demuxer );
2930 vlc_mutex_lock( &p_ev->lock );
2936 p_ev->i_key_action = 0;
2937 vlc_mutex_unlock( &p_ev->lock );
2941 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
2943 vlc_value_t valx, valy;
2945 vlc_mutex_lock( &p_ev->lock );
2946 pci_t *pci = (pci_t *) &p_sys->pci_packet;
2947 var_Get( p_vout, "mouse-x", &valx );
2948 var_Get( p_vout, "mouse-y", &valy );
2950 if( p_ev->b_clicked )
2953 int32_t best,dist,d;
2954 int32_t mx,my,dx,dy;
2956 msg_Dbg( p_ev->p_demux, "Handle Mouse Event: Mouse clicked x(%d)*y(%d)", (unsigned)valx.i_int, (unsigned)valy.i_int);
2959 // get current button
2961 dist = 0x08000000; /* >> than (720*720)+(567*567); */
2962 for(button = 1; button <= pci->hli.hl_gi.btn_ns; button++)
2964 btni_t *button_ptr = &(pci->hli.btnit[button-1]);
2966 if(((unsigned)valx.i_int >= button_ptr->x_start)
2967 && ((unsigned)valx.i_int <= button_ptr->x_end)
2968 && ((unsigned)valy.i_int >= button_ptr->y_start)
2969 && ((unsigned)valy.i_int <= button_ptr->y_end))
2971 mx = (button_ptr->x_start + button_ptr->x_end)/2;
2972 my = (button_ptr->y_start + button_ptr->y_end)/2;
2973 dx = mx - valx.i_int;
2974 dy = my - valy.i_int;
2975 d = (dx*dx) + (dy*dy);
2976 /* If the mouse is within the button and the mouse is closer
2977 * to the center of this button then it is the best choice. */
2987 btni_t button_ptr = pci->hli.btnit[best-1];
2988 uint16 i_curr_button = p_sys->dvd_interpretor.GetSPRM( 0x88 );
2990 msg_Dbg( &p_sys->demuxer, "Clicked button %d", best );
2991 vlc_mutex_unlock( &p_ev->lock );
2992 vlc_mutex_lock( &p_sys->lock_demuxer );
2994 // process the button action
2995 p_sys->dvd_interpretor.SetSPRM( 0x88, best );
2996 p_sys->dvd_interpretor.Interpret( button_ptr.cmd.bytes, 8 );
2998 msg_Dbg( &p_sys->demuxer, "Processed button %d", best );
3000 // select new button
3001 if ( best != i_curr_button )
3005 if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
3007 vlc_mutex_t *p_mutex = (vlc_mutex_t *) val.p_address;
3010 if(button_ptr.btn_coln != 0) {
3011 i_palette = pci->hli.btn_colit.btn_coli[button_ptr.btn_coln-1][1];
3016 for( int i = 0; i < 4; i++ )
3018 uint32_t i_yuv = 0xFF;//p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
3019 uint8_t i_alpha = (i_palette>>(i*4))&0x0f;
3020 i_alpha = i_alpha == 0xf ? 0xff : i_alpha << 4;
3022 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
3023 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
3024 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
3025 p_sys->palette[i][3] = i_alpha;
3028 vlc_mutex_lock( p_mutex );
3029 val.i_int = button_ptr.x_start; var_Set( p_sys->p_input, "x-start", val );
3030 val.i_int = button_ptr.x_end; var_Set( p_sys->p_input, "x-end", val );
3031 val.i_int = button_ptr.y_start; var_Set( p_sys->p_input, "y-start", val );
3032 val.i_int = button_ptr.y_end; var_Set( p_sys->p_input, "y-end", val );
3034 val.p_address = (void *)p_sys->palette;
3035 var_Set( p_sys->p_input, "menu-palette", val );
3037 val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
3038 vlc_mutex_unlock( p_mutex );
3041 vlc_mutex_unlock( &p_sys->lock_demuxer );
3042 vlc_mutex_lock( &p_ev->lock );
3045 else if( p_ev->b_moved )
3047 // dvdnav_mouse_select( NULL, pci, valx.i_int, valy.i_int );
3050 p_ev->b_moved = false;
3051 p_ev->b_clicked = false;
3052 vlc_mutex_unlock( &p_ev->lock );
3056 if( p_vout && p_vout->b_die )
3058 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3059 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3060 vlc_object_release( p_vout );
3064 else if( p_vout == NULL )
3066 p_vout = (vlc_object_t*) vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
3070 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3071 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3075 /* Wait a bit, 10ms */
3079 /* Release callback */
3082 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
3083 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
3084 vlc_object_release( p_vout );
3086 var_DelCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
3088 vlc_mutex_destroy( &p_ev->lock );
3093 void matroska_segment_c::UnSelect( )
3097 for( i_track = 0; i_track < tracks.size(); i_track++ )
3099 if ( tracks[i_track]->p_es != NULL )
3101 // es_format_Clean( &tracks[i_track]->fmt );
3102 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
3103 tracks[i_track]->p_es = NULL;
3110 void virtual_segment_c::PrepareChapters( )
3112 if ( linked_segments.size() == 0 )
3115 // !!! should be called only once !!!
3116 matroska_segment_c *p_segment;
3119 // copy editions from the first segment
3120 p_segment = linked_segments[0];
3121 p_editions = &p_segment->stored_editions;
3123 for ( i=1 ; i<linked_segments.size(); i++ )
3125 p_segment = linked_segments[i];
3126 // FIXME assume we have the same editions in all segments
3127 for (j=0; j<p_segment->stored_editions.size(); j++)
3129 if( j >= p_editions->size() ) /* Protect against broken files (?) */
3131 (*p_editions)[j]->Append( *p_segment->stored_editions[j] );
3136 std::string chapter_edition_c::GetMainName() const
3138 if ( sub_chapters.size() )
3140 return sub_chapters[0]->GetCodecName( true );
3145 int chapter_item_c::PublishChapters( input_title_t & title, int & i_user_chapters, int i_level )
3147 // add support for meta-elements from codec like DVD Titles
3148 if ( !b_display_seekpoint || psz_name == "" )
3150 psz_name = GetCodecName();
3151 if ( psz_name != "" )
3152 b_display_seekpoint = true;
3155 if (b_display_seekpoint)
3157 seekpoint_t *sk = vlc_seekpoint_New();
3159 sk->i_level = i_level;
3160 sk->i_time_offset = i_start_time;
3161 sk->psz_name = strdup( psz_name.c_str() );
3163 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
3164 title.i_seekpoint++;
3165 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
3166 title.seekpoint[title.i_seekpoint-1] = sk;
3168 if ( b_user_display )
3172 for ( size_t i=0; i<sub_chapters.size() ; i++)
3174 sub_chapters[i]->PublishChapters( title, i_user_chapters, i_level+1 );
3177 i_seekpoint_num = i_user_chapters;
3179 return i_user_chapters;
3182 bool virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
3184 demux_sys_t & sys = *demux.p_sys;
3185 chapter_item_c *psz_curr_chapter;
3186 bool b_has_seeked = false;
3188 /* update current chapter/seekpoint */
3189 if ( p_editions->size() )
3191 /* 1st, we need to know in which chapter we are */
3192 psz_curr_chapter = (*p_editions)[i_current_edition]->FindTimecode( sys.i_pts, psz_current_chapter );
3194 /* we have moved to a new chapter */
3195 if (psz_curr_chapter != NULL && psz_current_chapter != psz_curr_chapter)
3197 if ( (*p_editions)[i_current_edition]->b_ordered )
3199 // Leave/Enter up to the link point
3200 b_has_seeked = psz_curr_chapter->EnterAndLeave( psz_current_chapter );
3201 if ( !b_has_seeked )
3203 // only physically seek if necessary
3204 if ( psz_current_chapter == NULL || (psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time) )
3205 Seek( demux, sys.i_pts, 0, psz_curr_chapter );
3209 if ( !b_has_seeked )
3211 psz_current_chapter = psz_curr_chapter;
3212 if ( psz_curr_chapter->i_seekpoint_num > 0 )
3214 demux.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
3215 demux.info.i_title = sys.i_current_title = i_sys_title;
3216 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
3222 else if (psz_curr_chapter == NULL)
3224 // out of the scope of the data described by chapters, leave the edition
3225 if ( (*p_editions)[i_current_edition]->b_ordered && psz_current_chapter != NULL )
3227 if ( !(*p_editions)[i_current_edition]->EnterAndLeave( psz_current_chapter, false ) )
3228 psz_current_chapter = NULL;
3237 chapter_item_c *virtual_segment_c::BrowseCodecPrivate( unsigned int codec_id,
3238 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3239 const void *p_cookie,
3240 size_t i_cookie_size )
3242 // FIXME don't assume it is the first edition
3243 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
3244 if ( index != p_editions->end() )
3246 chapter_item_c *p_result = (*index)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3247 if ( p_result != NULL )
3253 chapter_item_c *virtual_segment_c::FindChapter( int64_t i_find_uid )
3255 // FIXME don't assume it is the first edition
3256 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
3257 if ( index != p_editions->end() )
3259 chapter_item_c *p_result = (*index)->FindChapter( i_find_uid );
3260 if ( p_result != NULL )
3266 chapter_item_c *chapter_item_c::BrowseCodecPrivate( unsigned int codec_id,
3267 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
3268 const void *p_cookie,
3269 size_t i_cookie_size )
3272 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3273 while ( index != codecs.end() )
3275 if ( match( **index ,p_cookie, i_cookie_size ) )
3281 chapter_item_c *p_result = NULL;
3282 std::vector<chapter_item_c*>::const_iterator index2 = sub_chapters.begin();
3283 while ( index2 != sub_chapters.end() )
3285 p_result = (*index2)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
3286 if ( p_result != NULL )
3294 void chapter_item_c::Append( const chapter_item_c & chapter )
3296 // we are appending content for the same chapter UID
3298 chapter_item_c *p_chapter;
3300 for ( i=0; i<chapter.sub_chapters.size(); i++ )
3302 p_chapter = FindChapter( chapter.sub_chapters[i]->i_uid );
3303 if ( p_chapter != NULL )
3305 p_chapter->Append( *chapter.sub_chapters[i] );
3309 sub_chapters.push_back( chapter.sub_chapters[i] );
3313 i_user_start_time = min( i_user_start_time, chapter.i_user_start_time );
3314 i_user_end_time = max( i_user_end_time, chapter.i_user_end_time );
3317 chapter_item_c * chapter_item_c::FindChapter( int64_t i_find_uid )
3320 chapter_item_c *p_result = NULL;
3322 if ( i_uid == i_find_uid )
3325 for ( i=0; i<sub_chapters.size(); i++)
3327 p_result = sub_chapters[i]->FindChapter( i_find_uid );
3328 if ( p_result != NULL )
3334 std::string chapter_item_c::GetCodecName( bool f_for_title ) const
3338 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3339 while ( index != codecs.end() )
3341 result = (*index)->GetCodecName( f_for_title );
3350 std::string dvd_chapter_codec_c::GetCodecName( bool f_for_title ) const
3353 if ( p_private_data->GetSize() >= 3)
3355 const binary* p_data = p_private_data->GetBuffer();
3356 /* if ( p_data[0] == MATROSKA_DVD_LEVEL_TT )
3358 uint16_t i_title = (p_data[1] << 8) + p_data[2];
3360 sprintf( psz_str, " %d ---", i_title );
3361 result = N_("--- DVD Title");
3364 else */ if ( p_data[0] == MATROSKA_DVD_LEVEL_LU )
3367 sprintf( psz_str, " (%c%c) ---", p_data[1], p_data[2] );
3368 result = N_("--- DVD Menu");
3371 else if ( p_data[0] == MATROSKA_DVD_LEVEL_SS && f_for_title )
3373 if ( p_data[1] == 0x00 )
3374 result = N_("First Played");
3375 else if ( p_data[1] == 0xC0 )
3376 result = N_("Video Manager");
3377 else if ( p_data[1] == 0x80 )
3379 uint16_t i_title = (p_data[2] << 8) + p_data[3];
3381 sprintf( psz_str, " %d -----", i_title );
3382 result = N_("----- Title");
3391 int16 chapter_item_c::GetTitleNumber( ) const
3395 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
3396 while ( index != codecs.end() )
3398 result = (*index)->GetTitleNumber( );
3407 int16 dvd_chapter_codec_c::GetTitleNumber()
3409 if ( p_private_data->GetSize() >= 3)
3411 const binary* p_data = p_private_data->GetBuffer();
3412 if ( p_data[0] == MATROSKA_DVD_LEVEL_SS )
3414 return int16( (p_data[2] << 8) + p_data[3] );
3420 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter )
3422 demux_sys_t *p_sys = p_demux->p_sys;
3423 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
3424 matroska_segment_c *p_segment = p_vsegment->Segment();
3425 mtime_t i_time_offset = 0;
3429 msg_Dbg( p_demux, "seek request to %"PRId64" (%f%%)", i_date, f_percent );
3430 if( i_date < 0 && f_percent < 0 )
3432 msg_Warn( p_demux, "cannot seek nowhere !" );
3435 if( f_percent > 1.0 )
3437 msg_Warn( p_demux, "cannot seek so far !" );
3441 /* seek without index or without date */
3442 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
3444 if (p_sys->f_duration >= 0)
3446 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
3450 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
3452 msg_Dbg( p_demux, "inacurate way of seeking" );
3453 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
3455 if( p_segment->p_indexes[i_index].i_position >= i_pos)
3460 if( i_index == p_segment->i_index )
3465 i_date = p_segment->p_indexes[i_index].i_time;
3468 if( p_segment->p_indexes[i_index].i_position < i_pos )
3472 msg_Warn( p_demux, "searching for cluster, could take some time" );
3474 /* search a cluster */
3475 while( ( el = p_sys->ep->Get() ) != NULL )
3477 if( MKV_IS_ID( el, KaxCluster ) )
3479 KaxCluster *cluster = (KaxCluster*)el;
3481 /* add it to the index */
3482 p_segment->IndexAppendCluster( cluster );
3484 if( (int64_t)cluster->GetElementPosition() >= i_pos )
3486 p_sys->cluster = cluster;
3497 p_vsegment->Seek( *p_demux, i_date, i_time_offset, psz_chapter );
3500 /*****************************************************************************
3501 * Demux: reads and demuxes data packets
3502 *****************************************************************************
3503 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
3504 *****************************************************************************/
3505 static int Demux( demux_t *p_demux)
3507 demux_sys_t *p_sys = p_demux->p_sys;
3509 vlc_mutex_lock( &p_sys->lock_demuxer );
3511 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
3512 matroska_segment_c *p_segment = p_vsegment->Segment();
3513 if ( p_segment == NULL ) return 0;
3514 int i_block_count = 0;
3519 if ( p_sys->demuxer.b_die )
3522 if( p_sys->i_pts >= p_sys->i_start_pts )
3523 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
3529 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
3531 /* nothing left to read in this ordered edition */
3532 if ( !p_vsegment->SelectNext() )
3534 p_segment->UnSelect( );
3536 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3538 /* switch to the next segment */
3539 p_segment = p_vsegment->Segment();
3540 if ( !p_segment->Select( 0 ) )
3542 msg_Err( p_demux, "Failed to select new segment" );
3549 int64_t i_block_duration = 0;
3550 int64_t i_block_ref1;
3551 int64_t i_block_ref2;
3553 #if LIBMATROSKA_VERSION >= 0x000800
3554 KaxSimpleBlock *simpleblock;
3556 if( p_segment->BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
3558 if( p_segment->BlockGet( block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
3561 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered )
3563 const chapter_item_c *p_chap = p_vsegment->CurrentChapter();
3564 // check if there are more chapters to read
3565 if ( p_chap != NULL )
3567 /* TODO handle successive chapters with the same user_start_time/user_end_time
3568 if ( p_chap->i_user_start_time == p_chap->i_user_start_time )
3569 p_vsegment->SelectNext();
3571 p_sys->i_pts = p_chap->i_user_end_time;
3572 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
3581 msg_Warn( p_demux, "cannot get block EOF?" );
3582 p_segment->UnSelect( );
3584 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3586 /* switch to the next segment */
3587 if ( !p_vsegment->SelectNext() )
3588 // no more segments in this stream
3590 p_segment = p_vsegment->Segment();
3591 if ( !p_segment->Select( 0 ) )
3593 msg_Err( p_demux, "Failed to select new segment" );
3601 #if LIBMATROSKA_VERSION >= 0x000800
3602 if ( simpleblock != NULL )
3603 p_sys->i_pts = (p_sys->i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
3606 p_sys->i_pts = (p_sys->i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
3608 if( p_sys->i_pts >= p_sys->i_start_pts )
3610 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
3612 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
3620 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
3622 /* nothing left to read in this ordered edition */
3623 if ( !p_vsegment->SelectNext() )
3628 p_segment->UnSelect( );
3630 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
3632 /* switch to the next segment */
3633 p_segment = p_vsegment->Segment();
3634 if ( !p_segment->Select( 0 ) )
3636 msg_Err( p_demux, "Failed to select new segment" );
3644 #if LIBMATROSKA_VERSION >= 0x000800
3645 BlockDecode( p_demux, block, simpleblock, p_sys->i_pts, i_block_duration, i_block_ref1 >= 0 || i_block_ref2 > 0 );
3647 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration, i_block_ref1 >= 0 || i_block_ref2 > 0 );
3653 // TODO optimize when there is need to leave or when seeking has been called
3654 if( i_block_count > 5 )
3661 vlc_mutex_unlock( &p_sys->lock_demuxer );
3668 /*****************************************************************************
3670 *****************************************************************************/
3671 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_, bool b_owner_ )
3678 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
3680 if( i_size <= 0 || mb_eof )
3685 return stream_Read( s, p_buffer, i_size );
3687 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
3693 case seek_beginning:
3697 i_pos = stream_Size( s ) - i_offset;
3700 i_pos= stream_Tell( s ) + i_offset;
3704 if( i_pos < 0 || i_pos >= stream_Size( s ) )
3711 if( stream_Seek( s, i_pos ) )
3717 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
3721 uint64 vlc_stream_io_callback::getFilePointer( void )
3725 return stream_Tell( s );
3727 void vlc_stream_io_callback::close( void )
3733 /*****************************************************************************
3734 * Ebml Stream parser
3735 *****************************************************************************/
3736 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux )
3743 mi_remain_size[0] = el_start->GetSize();
3745 for( i = 1; i < 6; i++ )
3752 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
3755 EbmlParser::~EbmlParser( void )
3759 for( i = 1; i < mi_level; i++ )
3769 EbmlElement* EbmlParser::UnGet( uint64 i_block_pos, uint64 i_cluster_pos )
3771 if ( mi_user_level > mi_level )
3773 while ( mi_user_level != mi_level )
3775 delete m_el[mi_user_level];
3776 m_el[mi_user_level] = NULL;
3782 if ( m_el[1]->GetElementPosition() == i_cluster_pos )
3784 m_es->I_O().setFilePointer( i_block_pos, seek_beginning );
3785 return (EbmlMaster*) m_el[1];
3789 // seek to the previous Cluster
3790 m_es->I_O().setFilePointer( i_cluster_pos, seek_beginning );
3793 delete m_el[mi_level];
3794 m_el[mi_level] = NULL;
3799 void EbmlParser::Up( void )
3801 if( mi_user_level == mi_level )
3803 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
3809 void EbmlParser::Down( void )
3815 void EbmlParser::Keep( void )
3820 int EbmlParser::GetLevel( void )
3822 return mi_user_level;
3825 void EbmlParser::Reset( demux_t *p_demux )
3827 while ( mi_level > 0)
3829 delete m_el[mi_level];
3830 m_el[mi_level] = NULL;
3833 mi_user_level = mi_level = 1;
3834 #if LIBEBML_VERSION >= 0x000704
3835 // a little faster and cleaner
3836 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
3838 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
3840 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
3843 EbmlElement *EbmlParser::Get( void )
3847 if( mi_user_level != mi_level )
3853 EbmlElement *ret = m_got;
3859 if( m_el[mi_level] )
3861 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
3864 delete m_el[mi_level];
3869 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, mb_dummy != 0, 1 );
3870 // mi_remain_size[mi_level] = m_el[mi_level]->GetSize();
3881 delete m_el[mi_level - 1];
3882 m_got = m_el[mi_level -1] = m_el[mi_level];
3883 m_el[mi_level] = NULL;
3890 else if( m_el[mi_level] == NULL )
3892 fprintf( stderr," m_el[mi_level] == NULL\n" );
3895 return m_el[mi_level];
3899 /*****************************************************************************
3901 * * LoadCues : load the cues element and update index
3903 * * LoadTags : load ... the tags element
3905 * * InformationCreate : create all information, load tags if present
3907 *****************************************************************************/
3908 void matroska_segment_c::LoadCues( )
3910 int64_t i_sav_position = es.I_O().getFilePointer();
3912 EbmlElement *el, *cues;
3914 /* *** Load the cue if found *** */
3915 if( i_cues_position < 0 )
3917 msg_Warn( &sys.demuxer, "no cues/empty cues found->seek won't be precise" );
3919 // IndexAppendCluster( cluster );
3924 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
3928 msg_Dbg( &sys.demuxer, "loading cues" );
3929 es.I_O().setFilePointer( i_cues_position, seek_beginning );
3930 cues = es.FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
3934 msg_Err( &sys.demuxer, "cannot load cues (broken seekhead or file)" );
3935 es.I_O().setFilePointer( i_sav_position, seek_beginning );
3939 ep = new EbmlParser( &es, cues, &sys.demuxer );
3940 while( ( el = ep->Get() ) != NULL )
3942 if( MKV_IS_ID( el, KaxCuePoint ) )
3944 #define idx p_indexes[i_index]
3947 idx.i_block_number= -1;
3948 idx.i_position = -1;
3953 while( ( el = ep->Get() ) != NULL )
3955 if( MKV_IS_ID( el, KaxCueTime ) )
3957 KaxCueTime &ctime = *(KaxCueTime*)el;
3959 ctime.ReadData( es.I_O() );
3961 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
3963 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
3966 while( ( el = ep->Get() ) != NULL )
3968 if( MKV_IS_ID( el, KaxCueTrack ) )
3970 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
3972 ctrack.ReadData( es.I_O() );
3973 idx.i_track = uint16( ctrack );
3975 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
3977 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
3979 ccpos.ReadData( es.I_O() );
3980 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
3982 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
3984 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
3986 cbnum.ReadData( es.I_O() );
3987 idx.i_block_number = uint32( cbnum );
3991 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
3998 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
4004 msg_Dbg( &sys.demuxer, " * added time=%"PRId64" pos=%"PRId64
4005 " track=%d bnum=%d", idx.i_time, idx.i_position,
4006 idx.i_track, idx.i_block_number );
4010 if( i_index >= i_index_max )
4012 i_index_max += 1024;
4013 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
4019 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
4027 msg_Dbg( &sys.demuxer, "loading cues done." );
4028 es.I_O().setFilePointer( i_sav_position, seek_beginning );
4031 void matroska_segment_c::LoadTags( )
4033 int64_t i_sav_position = es.I_O().getFilePointer();
4035 EbmlElement *el, *tags;
4037 msg_Dbg( &sys.demuxer, "loading tags" );
4038 es.I_O().setFilePointer( i_tags_position, seek_beginning );
4039 tags = es.FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
4043 msg_Err( &sys.demuxer, "cannot load tags (broken seekhead or file)" );
4044 es.I_O().setFilePointer( i_sav_position, seek_beginning );
4048 msg_Dbg( &sys.demuxer, "Tags" );
4049 ep = new EbmlParser( &es, tags, &sys.demuxer );
4050 while( ( el = ep->Get() ) != NULL )
4052 if( MKV_IS_ID( el, KaxTag ) )
4054 msg_Dbg( &sys.demuxer, "+ Tag" );
4056 while( ( el = ep->Get() ) != NULL )
4058 if( MKV_IS_ID( el, KaxTagTargets ) )
4060 msg_Dbg( &sys.demuxer, "| + Targets" );
4062 while( ( el = ep->Get() ) != NULL )
4064 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4068 else if( MKV_IS_ID( el, KaxTagGeneral ) )
4070 msg_Dbg( &sys.demuxer, "| + General" );
4072 while( ( el = ep->Get() ) != NULL )
4074 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4078 else if( MKV_IS_ID( el, KaxTagGenres ) )
4080 msg_Dbg( &sys.demuxer, "| + Genres" );
4082 while( ( el = ep->Get() ) != NULL )
4084 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4088 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
4090 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
4092 while( ( el = ep->Get() ) != NULL )
4094 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4098 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
4100 msg_Dbg( &sys.demuxer, "| + Images Specific" );
4102 while( ( el = ep->Get() ) != NULL )
4104 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
4108 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
4110 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
4112 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
4114 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
4116 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
4118 msg_Dbg( &sys.demuxer, "| + Multi Date" );
4120 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
4122 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
4124 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
4126 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
4128 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
4130 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
4132 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
4134 msg_Dbg( &sys.demuxer, "| + Multi Title" );
4138 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid( *el ).name() );
4145 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
4151 msg_Dbg( &sys.demuxer, "loading tags done." );
4152 es.I_O().setFilePointer( i_sav_position, seek_beginning );
4155 /*****************************************************************************
4157 *****************************************************************************/
4158 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
4162 int i_upper_level = 0;
4164 msg_Dbg( &sys.demuxer, "| + Seek head" );
4166 /* Master elements */
4167 seekhead->Read( es, seekhead->Generic().Context, i_upper_level, el, true );
4169 for( i = 0; i < seekhead->ListSize(); i++ )
4171 EbmlElement *l = (*seekhead)[i];
4173 if( MKV_IS_ID( l, KaxSeek ) )
4175 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
4176 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
4179 for( j = 0; j < sk->ListSize(); j++ )
4181 EbmlElement *l = (*sk)[j];
4183 if( MKV_IS_ID( l, KaxSeekID ) )
4185 KaxSeekID &sid = *(KaxSeekID*)l;
4186 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
4188 else if( MKV_IS_ID( l, KaxSeekPosition ) )
4190 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
4191 i_pos = uint64( spos );
4195 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
4201 if( id == KaxCues::ClassInfos.GlobalId )
4203 msg_Dbg( &sys.demuxer, "| | | = cues at %"PRId64, i_pos );
4204 i_cues_position = segment->GetGlobalPosition( i_pos );
4206 else if( id == KaxChapters::ClassInfos.GlobalId )
4208 msg_Dbg( &sys.demuxer, "| | | = chapters at %"PRId64, i_pos );
4209 i_chapters_position = segment->GetGlobalPosition( i_pos );
4211 else if( id == KaxTags::ClassInfos.GlobalId )
4213 msg_Dbg( &sys.demuxer, "| | | = tags at %"PRId64, i_pos );
4214 i_tags_position = segment->GetGlobalPosition( i_pos );
4220 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4225 /*****************************************************************************
4227 *****************************************************************************/
4228 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
4231 bool bSupported = true;
4235 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
4237 tk = new mkv_track_t();
4239 /* Init the track */
4240 memset( tk, 0, sizeof( mkv_track_t ) );
4242 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
4243 tk->fmt.psz_language = strdup("English");
4244 tk->fmt.psz_description = NULL;
4246 tk->b_default = true;
4247 tk->b_enabled = true;
4248 tk->b_silent = false;
4249 tk->i_number = tracks.size() - 1;
4250 tk->i_extra_data = 0;
4251 tk->p_extra_data = NULL;
4252 tk->psz_codec = NULL;
4253 tk->i_default_duration = 0;
4254 tk->f_timecodescale = 1.0;
4256 tk->b_inited = false;
4257 tk->i_data_init = 0;
4258 tk->p_data_init = NULL;
4260 tk->psz_codec_name = NULL;
4261 tk->psz_codec_settings = NULL;
4262 tk->psz_codec_info_url = NULL;
4263 tk->psz_codec_download_url = NULL;
4265 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
4266 tk->p_compression_data = NULL;
4268 for( i = 0; i < m->ListSize(); i++ )
4270 EbmlElement *l = (*m)[i];
4272 if( MKV_IS_ID( l, KaxTrackNumber ) )
4274 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
4276 tk->i_number = uint32( tnum );
4277 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
4279 else if( MKV_IS_ID( l, KaxTrackUID ) )
4281 KaxTrackUID &tuid = *(KaxTrackUID*)l;
4283 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
4285 else if( MKV_IS_ID( l, KaxTrackType ) )
4287 const char *psz_type;
4288 KaxTrackType &ttype = *(KaxTrackType*)l;
4290 switch( uint8(ttype) )
4294 tk->fmt.i_cat = AUDIO_ES;
4298 tk->fmt.i_cat = VIDEO_ES;
4300 case track_subtitle:
4301 psz_type = "subtitle";
4302 tk->fmt.i_cat = SPU_ES;
4305 psz_type = "buttons";
4306 tk->fmt.i_cat = SPU_ES;
4309 psz_type = "unknown";
4310 tk->fmt.i_cat = UNKNOWN_ES;
4314 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
4316 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
4318 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
4320 // tk->b_enabled = uint32( fenb );
4321 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
4322 // uint32( fenb ) );
4324 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
4326 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
4328 tk->b_default = uint32( fdef );
4329 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
4331 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
4333 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
4335 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
4337 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
4339 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
4341 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
4343 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
4345 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
4347 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
4349 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
4351 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
4353 tk->i_default_duration = uint64(defd);
4354 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration=%"PRId64, uint64(defd) );
4356 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
4358 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
4360 tk->f_timecodescale = float( ttcs );
4361 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
4363 else if( MKV_IS_ID( l, KaxTrackName ) )
4365 KaxTrackName &tname = *(KaxTrackName*)l;
4367 tk->fmt.psz_description = ToUTF8( UTFstring( tname ) );
4368 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
4370 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
4372 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
4374 if ( tk->fmt.psz_language != NULL )
4375 free( tk->fmt.psz_language );
4376 tk->fmt.psz_language = strdup( string( lang ).c_str() );
4377 msg_Dbg( &sys.demuxer,
4378 "| | | + Track Language=`%s'", tk->fmt.psz_language );
4380 else if( MKV_IS_ID( l, KaxCodecID ) )
4382 KaxCodecID &codecid = *(KaxCodecID*)l;
4384 tk->psz_codec = strdup( string( codecid ).c_str() );
4385 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
4387 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
4389 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
4391 tk->i_extra_data = cpriv.GetSize();
4392 if( tk->i_extra_data > 0 )
4394 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
4395 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
4397 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
4399 else if( MKV_IS_ID( l, KaxCodecName ) )
4401 KaxCodecName &cname = *(KaxCodecName*)l;
4403 tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
4404 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
4406 else if( MKV_IS_ID( l, KaxContentEncodings ) )
4408 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
4409 MkvTree( sys.demuxer, 3, "Content Encodings" );
4410 if ( cencs->ListSize() > 1 )
4412 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
4415 for( j = 0; j < cencs->ListSize(); j++ )
4417 EbmlElement *l2 = (*cencs)[j];
4418 if( MKV_IS_ID( l2, KaxContentEncoding ) )
4420 MkvTree( sys.demuxer, 4, "Content Encoding" );
4421 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
4422 for( k = 0; k < cenc->ListSize(); k++ )
4424 EbmlElement *l3 = (*cenc)[k];
4425 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
4427 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
4428 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
4430 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
4432 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
4433 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
4435 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
4437 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
4438 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
4440 else if( MKV_IS_ID( l3, KaxContentCompression ) )
4442 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
4443 MkvTree( sys.demuxer, 5, "Content Compression" );
4444 for( n = 0; n < compr->ListSize(); n++ )
4446 EbmlElement *l4 = (*compr)[n];
4447 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
4449 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
4450 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
4451 tk->i_compression_type = uint32( compalg );
4452 if ( ( tk->i_compression_type != MATROSKA_COMPRESSION_ZLIB ) &&
4453 ( tk->i_compression_type != MATROSKA_COMPRESSION_HEADER ) )
4455 msg_Err( &sys.demuxer, "Track Compression method %d not supported", tk->i_compression_type );
4459 else if( MKV_IS_ID( l4, KaxContentCompSettings ) )
4461 tk->p_compression_data = new KaxContentCompSettings( *(KaxContentCompSettings*)l4 );
4465 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
4471 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
4477 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
4481 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
4483 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
4485 // tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
4486 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
4488 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
4490 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
4492 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
4493 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
4495 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
4497 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
4499 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
4500 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
4502 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
4504 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
4506 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
4508 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
4510 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
4512 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
4514 else if( MKV_IS_ID( l, KaxTrackVideo ) )
4516 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
4518 unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
4519 unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
4521 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
4524 tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
4525 tk->fmt.video.i_frame_rate = 1000000;
4527 for( j = 0; j < tkv->ListSize(); j++ )
4529 EbmlElement *l = (*tkv)[j];
4530 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
4532 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
4534 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
4536 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
4538 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
4540 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
4543 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
4545 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
4547 tk->fmt.video.i_width += uint16( vwidth );
4548 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
4550 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
4552 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
4554 tk->fmt.video.i_height += uint16( vheight );
4555 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
4557 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
4559 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
4561 i_display_width = uint16( vwidth );
4562 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
4564 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
4566 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
4568 i_display_height = uint16( vheight );
4569 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
4571 else if( MKV_IS_ID( l, KaxVideoPixelCropBottom ) )
4573 KaxVideoPixelCropBottom &cropval = *(KaxVideoPixelCropBottom*)l;
4575 i_crop_bottom = uint16( cropval );
4576 msg_Dbg( &sys.demuxer, "| | | | + crop pixel bottom=%d", uint16( cropval ) );
4578 else if( MKV_IS_ID( l, KaxVideoPixelCropTop ) )
4580 KaxVideoPixelCropTop &cropval = *(KaxVideoPixelCropTop*)l;
4582 i_crop_top = uint16( cropval );
4583 msg_Dbg( &sys.demuxer, "| | | | + crop pixel top=%d", uint16( cropval ) );
4585 else if( MKV_IS_ID( l, KaxVideoPixelCropRight ) )
4587 KaxVideoPixelCropRight &cropval = *(KaxVideoPixelCropRight*)l;
4589 i_crop_right = uint16( cropval );
4590 msg_Dbg( &sys.demuxer, "| | | | + crop pixel right=%d", uint16( cropval ) );
4592 else if( MKV_IS_ID( l, KaxVideoPixelCropLeft ) )
4594 KaxVideoPixelCropLeft &cropval = *(KaxVideoPixelCropLeft*)l;
4596 i_crop_left = uint16( cropval );
4597 msg_Dbg( &sys.demuxer, "| | | | + crop pixel left=%d", uint16( cropval ) );
4599 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
4601 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
4603 tk->f_fps = float( vfps );
4604 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
4606 else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
4608 KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
4610 i_display_unit = uint8( vdmode );
4611 msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
4612 uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
4614 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
4616 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
4618 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
4620 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
4622 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
4624 // msg_Dbg( &sys.demuxer, " | | | + gamma=%f", float( gamma ) );
4628 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
4631 if( i_display_height && i_display_width )
4632 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * i_display_width / i_display_height;
4633 if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
4635 tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
4636 tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
4637 tk->fmt.video.i_x_offset = i_crop_left;
4638 tk->fmt.video.i_y_offset = i_crop_top;
4639 tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
4640 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
4642 /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
4643 we do not support this atm */
4645 else if( MKV_IS_ID( l, KaxTrackAudio ) )
4647 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
4650 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
4652 for( j = 0; j < tka->ListSize(); j++ )
4654 EbmlElement *l = (*tka)[j];
4656 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
4658 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
4660 tk->i_original_rate = tk->fmt.audio.i_rate = (int)float( afreq );
4661 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
4663 else if( MKV_IS_ID( l, KaxAudioOutputSamplingFreq ) )
4665 KaxAudioOutputSamplingFreq &afreq = *(KaxAudioOutputSamplingFreq*)l;
4667 tk->fmt.audio.i_rate = (int)float( afreq );
4668 msg_Dbg( &sys.demuxer, "| | | | + aoutfreq=%d", tk->fmt.audio.i_rate );
4670 else if( MKV_IS_ID( l, KaxAudioChannels ) )
4672 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
4674 tk->fmt.audio.i_channels = uint8( achan );
4675 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
4677 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
4679 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
4681 tk->fmt.audio.i_bitspersample = uint8( abits );
4682 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
4686 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
4692 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
4693 typeid(*l).name() );
4699 tracks.push_back( tk );
4703 msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
4708 /*****************************************************************************
4710 *****************************************************************************/
4711 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
4715 int i_upper_level = 0;
4717 msg_Dbg( &sys.demuxer, "| + Tracks" );
4719 /* Master elements */
4720 tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
4722 for( i = 0; i < tracks->ListSize(); i++ )
4724 EbmlElement *l = (*tracks)[i];
4726 if( MKV_IS_ID( l, KaxTrackEntry ) )
4728 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
4732 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4737 /*****************************************************************************
4739 *****************************************************************************/
4740 void matroska_segment_c::ParseInfo( KaxInfo *info )
4745 int i_upper_level = 0;
4747 msg_Dbg( &sys.demuxer, "| + Information" );
4749 /* Master elements */
4750 m = static_cast<EbmlMaster *>(info);
4751 m->Read( es, info->Generic().Context, i_upper_level, el, true );
4753 for( i = 0; i < m->ListSize(); i++ )
4755 EbmlElement *l = (*m)[i];
4757 if( MKV_IS_ID( l, KaxSegmentUID ) )
4759 if ( p_segment_uid == NULL )
4760 p_segment_uid = new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l));
4762 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)p_segment_uid->GetBuffer() );
4764 else if( MKV_IS_ID( l, KaxPrevUID ) )
4766 if ( p_prev_segment_uid == NULL )
4767 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
4769 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
4771 else if( MKV_IS_ID( l, KaxNextUID ) )
4773 if ( p_next_segment_uid == NULL )
4774 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
4776 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
4778 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
4780 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
4782 i_timescale = uint64(tcs);
4784 msg_Dbg( &sys.demuxer, "| | + TimecodeScale=%"PRId64,
4787 else if( MKV_IS_ID( l, KaxDuration ) )
4789 KaxDuration &dur = *(KaxDuration*)l;
4791 i_duration = mtime_t( double( dur ) );
4793 msg_Dbg( &sys.demuxer, "| | + Duration=%"PRId64,
4796 else if( MKV_IS_ID( l, KaxMuxingApp ) )
4798 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
4800 psz_muxing_application = ToUTF8( UTFstring( mapp ) );
4802 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
4803 psz_muxing_application );
4805 else if( MKV_IS_ID( l, KaxWritingApp ) )
4807 KaxWritingApp &wapp = *(KaxWritingApp*)l;
4809 psz_writing_application = ToUTF8( UTFstring( wapp ) );
4811 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
4812 psz_writing_application );
4814 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
4816 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
4818 psz_segment_filename = ToUTF8( UTFstring( sfn ) );
4820 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
4821 psz_segment_filename );
4823 else if( MKV_IS_ID( l, KaxTitle ) )
4825 KaxTitle &title = *(KaxTitle*)l;
4827 psz_title = ToUTF8( UTFstring( title ) );
4829 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
4831 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
4833 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
4835 families.push_back( new KaxSegmentFamily(*uid) );
4837 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
4839 #if defined( HAVE_GMTIME_R ) && !defined( __APPLE__ )
4840 else if( MKV_IS_ID( l, KaxDateUTC ) )
4842 KaxDateUTC &date = *(KaxDateUTC*)l;
4847 i_date = date.GetEpochDate();
4848 memset( buffer, 0, 256 );
4849 if( gmtime_r( &i_date, &tmres ) &&
4850 asctime_r( &tmres, buffer ) )
4852 buffer[strlen( buffer)-1]= '\0';
4853 psz_date_utc = strdup( buffer );
4854 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
4858 #if LIBMATROSKA_VERSION >= 0x000704
4859 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
4861 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
4862 chapter_translation_c *p_translate = new chapter_translation_c();
4864 p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
4865 for( j = 0; j < p_trans->ListSize(); j++ )
4867 EbmlElement *l = (*p_trans)[j];
4869 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
4871 p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
4873 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
4875 p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
4877 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
4879 p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
4883 translations.push_back( p_translate );
4888 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4892 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
4893 i_duration = mtime_t(f_dur);
4897 /*****************************************************************************
4899 *****************************************************************************/
4900 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
4904 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
4905 for( i = 0; i < ca->ListSize(); i++ )
4907 EbmlElement *l = (*ca)[i];
4909 if( MKV_IS_ID( l, KaxChapterUID ) )
4911 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
4912 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
4914 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
4916 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
4917 chapters.b_display_seekpoint = uint8( flag ) == 0;
4919 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
4921 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
4923 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
4924 chapters.i_start_time = uint64( start ) / INT64_C(1000);
4926 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
4928 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
4930 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
4931 chapters.i_end_time = uint64( end ) / INT64_C(1000);
4933 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
4935 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
4937 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
4939 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
4940 for( j = 0; j < cd->ListSize(); j++ )
4942 EbmlElement *l= (*cd)[j];
4944 if( MKV_IS_ID( l, KaxChapterString ) )
4948 KaxChapterString &name =*(KaxChapterString*)l;
4949 for (k = 0; k < i_level; k++)
4950 chapters.psz_name += '+';
4951 chapters.psz_name += ' ';
4952 char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
4953 chapters.psz_name += psz_tmp_utf8;
4954 chapters.b_user_display = true;
4956 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", psz_tmp_utf8 );
4957 free( psz_tmp_utf8 );
4959 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
4961 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
4962 const char *psz = string( lang ).c_str();
4964 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
4966 else if( MKV_IS_ID( l, KaxChapterCountry ) )
4968 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
4969 const char *psz = string( ct ).c_str();
4971 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
4975 else if( MKV_IS_ID( l, KaxChapterProcess ) )
4977 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
4979 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
4980 chapter_codec_cmds_c *p_ccodec = NULL;
4982 for( j = 0; j < cp->ListSize(); j++ )
4984 EbmlElement *k= (*cp)[j];
4986 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
4988 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
4989 if ( uint32(*p_codec_id) == 0 )
4990 p_ccodec = new matroska_script_codec_c( sys );
4991 else if ( uint32(*p_codec_id) == 1 )
4992 p_ccodec = new dvd_chapter_codec_c( sys );
4997 if ( p_ccodec != NULL )
4999 for( j = 0; j < cp->ListSize(); j++ )
5001 EbmlElement *k= (*cp)[j];
5003 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
5005 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
5006 p_ccodec->SetPrivate( *p_private );
5008 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
5010 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
5013 chapters.codecs.push_back( p_ccodec );
5016 else if( MKV_IS_ID( l, KaxChapterAtom ) )
5018 chapter_item_c *new_sub_chapter = new chapter_item_c();
5019 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
5020 new_sub_chapter->psz_parent = &chapters;
5021 chapters.sub_chapters.push_back( new_sub_chapter );
5026 /*****************************************************************************
5028 *****************************************************************************/
5029 void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
5032 int i_upper_level = 0;
5034 attachments->Read( es, attachments->Generic().Context, i_upper_level, el, true );
5036 KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
5038 while( attachedFile && ( attachedFile->GetSize() > 0 ) )
5040 std::string psz_mime_type = GetChild<KaxMimeType>( *attachedFile );
5041 KaxFileName &file_name = GetChild<KaxFileName>( *attachedFile );
5042 KaxFileData &img_data = GetChild<KaxFileData>( *attachedFile );
5044 attachment_c *new_attachment = new attachment_c();
5046 if( new_attachment )
5048 new_attachment->psz_file_name = ToUTF8( UTFstring( file_name ) );
5049 new_attachment->psz_mime_type = psz_mime_type;
5050 new_attachment->i_size = img_data.GetSize();
5051 new_attachment->p_data = malloc( img_data.GetSize() );
5053 if( new_attachment->p_data )
5055 memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
5056 sys.stored_attachments.push_back( new_attachment );
5060 delete new_attachment;
5064 attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
5068 /*****************************************************************************
5070 *****************************************************************************/
5071 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
5075 int i_upper_level = 0;
5078 /* Master elements */
5079 chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
5081 for( i = 0; i < chapters->ListSize(); i++ )
5083 EbmlElement *l = (*chapters)[i];
5085 if( MKV_IS_ID( l, KaxEditionEntry ) )
5087 chapter_edition_c *p_edition = new chapter_edition_c();
5089 EbmlMaster *E = static_cast<EbmlMaster *>(l );
5091 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
5092 for( j = 0; j < E->ListSize(); j++ )
5094 EbmlElement *l = (*E)[j];
5096 if( MKV_IS_ID( l, KaxChapterAtom ) )
5098 chapter_item_c *new_sub_chapter = new chapter_item_c();
5099 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
5100 p_edition->sub_chapters.push_back( new_sub_chapter );
5102 else if( MKV_IS_ID( l, KaxEditionUID ) )
5104 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
5106 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
5108 p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
5110 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
5112 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
5113 i_default_edition = stored_editions.size();
5117 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
5120 stored_editions.push_back( p_edition );
5124 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
5128 for( i = 0; i < stored_editions.size(); i++ )
5130 stored_editions[i]->RefreshChapters( );
5133 if ( stored_editions.size() != 0 && stored_editions[i_default_edition]->b_ordered )
5135 /* update the duration of the segment according to the sum of all sub chapters */
5136 i_dur = stored_editions[i_default_edition]->Duration() / INT64_C(1000);
5142 void matroska_segment_c::ParseCluster( )
5147 int i_upper_level = 0;
5149 /* Master elements */
5150 m = static_cast<EbmlMaster *>( cluster );
5151 m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
5153 for( i = 0; i < m->ListSize(); i++ )
5155 EbmlElement *l = (*m)[i];
5157 if( MKV_IS_ID( l, KaxClusterTimecode ) )
5159 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
5161 cluster->InitTimecode( uint64( ctc ), i_timescale );
5166 i_start_time = cluster->GlobalTimecode() / 1000;
5169 /*****************************************************************************
5170 * InformationCreate:
5171 *****************************************************************************/
5172 void matroska_segment_c::InformationCreate( )
5174 sys.meta = vlc_meta_New();
5178 vlc_meta_SetTitle( sys.meta, psz_title );
5182 vlc_meta_SetDate( sys.meta, psz_date_utc );
5185 if( psz_segment_filename )
5187 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5189 if( psz_muxing_application )
5191 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5193 if( psz_writing_application )
5195 fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
5198 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
5200 // mkv_track_t *tk = tracks[i_track];
5201 // vlc_meta_t *mtk = vlc_meta_New();
5202 fprintf( stderr, "***** WARNING: Unhandled child meta\n");
5206 if( i_tags_position >= 0 )
5210 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
5219 /*****************************************************************************
5221 *****************************************************************************/
5223 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
5225 #define idx p_indexes[i_index]
5227 idx.i_block_number= -1;
5228 idx.i_position = cluster->GetElementPosition();
5233 if( i_index >= i_index_max )
5235 i_index_max += 1024;
5236 p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
5241 void chapter_edition_c::RefreshChapters( )
5243 chapter_item_c::RefreshChapters( b_ordered, -1 );
5244 b_display_seekpoint = false;
5247 int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_time )
5249 int64_t i_user_time = i_prev_user_time;
5251 // first the sub-chapters, and then ourself
5252 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
5253 while ( index != sub_chapters.end() )
5255 i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
5261 // the ordered chapters always start at zero
5262 if ( i_prev_user_time == -1 )
5264 if ( i_user_time == -1 )
5266 i_prev_user_time = 0;
5269 i_user_start_time = i_prev_user_time;
5270 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
5272 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
5276 i_user_end_time = i_user_time;
5281 if ( sub_chapters.begin() != sub_chapters.end() )
5282 std::sort( sub_chapters.begin(), sub_chapters.end(), chapter_item_c::CompareTimecode );
5283 i_user_start_time = i_start_time;
5284 if ( i_end_time != -1 )
5285 i_user_end_time = i_end_time;
5286 else if ( i_user_time != -1 )
5287 i_user_end_time = i_user_time;
5289 i_user_end_time = i_user_start_time;
5292 return i_user_end_time;
5295 mtime_t chapter_edition_c::Duration() const
5297 mtime_t i_result = 0;
5299 if ( sub_chapters.size() )
5301 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
5303 i_result = (*index)->i_user_end_time;
5309 chapter_item_c * chapter_edition_c::FindTimecode( mtime_t i_timecode, const chapter_item_c * p_current )
5313 bool b_found_current = false;
5314 return chapter_item_c::FindTimecode( i_timecode, p_current, b_found_current );
5317 chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode, const chapter_item_c * p_current, bool & b_found )
5319 chapter_item_c *psz_result = NULL;
5321 if ( p_current == this )
5324 if ( i_user_timecode >= i_user_start_time &&
5325 ( i_user_timecode < i_user_end_time ||
5326 ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
5328 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
5329 while ( index != sub_chapters.end() && ((p_current == NULL && psz_result == NULL) || (p_current != NULL && (!b_found || psz_result == NULL))))
5331 psz_result = (*index)->FindTimecode( i_user_timecode, p_current, b_found );
5335 if ( psz_result == NULL )
5342 bool chapter_item_c::ParentOf( const chapter_item_c & item ) const
5344 if ( &item == this )
5347 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.begin();
5348 while ( index != sub_chapters.end() )
5350 if ( (*index)->ParentOf( item ) )
5358 void demux_sys_t::PreloadFamily( const matroska_segment_c & of_segment )
5360 for (size_t i=0; i<opened_segments.size(); i++)
5362 opened_segments[i]->PreloadFamily( of_segment );
5365 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
5370 for (size_t i=0; i<families.size(); i++)
5372 for (size_t j=0; j<of_segment.families.size(); j++)
5374 if ( *(families[i]) == *(of_segment.families[j]) )
5382 // preload all the linked segments for all preloaded segments
5383 void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
5385 size_t i_preloaded, i, j;
5386 virtual_segment_c *p_seg;
5388 p_current_segment = VirtualFromSegments( p_segment );
5390 used_segments.push_back( p_current_segment );
5392 // create all the other virtual segments of the family
5395 for ( i=0; i< opened_segments.size(); i++ )
5397 if ( opened_segments[i]->b_preloaded && !IsUsedSegment( *opened_segments[i] ) )
5399 p_seg = VirtualFromSegments( opened_segments[i] );
5400 used_segments.push_back( p_seg );
5404 } while ( i_preloaded ); // worst case: will stop when all segments are found as family related
5406 // publish all editions of all usable segment
5407 for ( i=0; i< used_segments.size(); i++ )
5409 p_seg = used_segments[i];
5410 if ( p_seg->p_editions != NULL )
5412 std::string sz_name;
5413 input_title_t *p_title = vlc_input_title_New();
5414 p_seg->i_sys_title = i;
5417 // TODO use a name for each edition, let the TITLE deal with a codec name
5418 for ( j=0; j<p_seg->p_editions->size(); j++ )
5420 if ( p_title->psz_name == NULL )
5422 sz_name = (*p_seg->p_editions)[j]->GetMainName();
5423 if ( sz_name != "" )
5424 p_title->psz_name = strdup( sz_name.c_str() );
5427 chapter_edition_c *p_edition = (*p_seg->p_editions)[j];
5430 p_edition->PublishChapters( *p_title, i_chapters, 0 );
5433 // create a name if there is none
5434 if ( p_title->psz_name == NULL )
5436 sz_name = N_("Segment");
5438 sprintf( psz_str, " %d", (int)i );
5440 p_title->psz_name = strdup( sz_name.c_str() );
5443 titles.push_back( p_title );
5447 // TODO decide which segment should be first used (VMG for DVD)
5450 bool demux_sys_t::IsUsedSegment( matroska_segment_c &segment ) const
5452 for ( size_t i=0; i< used_segments.size(); i++ )
5454 if ( used_segments[i]->FindUID( *segment.p_segment_uid ) )
5460 virtual_segment_c *demux_sys_t::VirtualFromSegments( matroska_segment_c *p_segment ) const
5462 size_t i_preloaded, i;
5464 virtual_segment_c *p_result = new virtual_segment_c( p_segment );
5466 // fill our current virtual segment with all hard linked segments
5469 for ( i=0; i< opened_segments.size(); i++ )
5471 i_preloaded += p_result->AddSegment( opened_segments[i] );
5473 } while ( i_preloaded ); // worst case: will stop when all segments are found as linked
5477 p_result->PreloadLinked( );
5479 p_result->PrepareChapters( );
5484 bool demux_sys_t::PreparePlayback( virtual_segment_c *p_new_segment )
5486 if ( p_new_segment != NULL && p_new_segment != p_current_segment )
5488 if ( p_current_segment != NULL && p_current_segment->Segment() != NULL )
5489 p_current_segment->Segment()->UnSelect();
5491 p_current_segment = p_new_segment;
5492 i_current_title = p_new_segment->i_sys_title;
5495 p_current_segment->LoadCues();
5496 f_duration = p_current_segment->Duration();
5498 /* add information */
5499 p_current_segment->Segment()->InformationCreate( );
5501 p_current_segment->Segment()->Select( 0 );
5506 void demux_sys_t::JumpTo( virtual_segment_c & vsegment, chapter_item_c * p_chapter )
5508 // if the segment is not part of the current segment, select the new one
5509 if ( &vsegment != p_current_segment )
5511 PreparePlayback( &vsegment );
5514 if ( p_chapter != NULL )
5516 if ( !p_chapter->Enter( true ) )
5518 // jump to the location in the found segment
5519 vsegment.Seek( demuxer, p_chapter->i_user_start_time, -1, p_chapter );
5525 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
5529 if ( p_item_a == NULL || p_item_b == NULL )
5532 p_tmp = (EbmlBinary *)p_item_a->p_segment_uid;
5533 if ( p_item_b->p_prev_segment_uid != NULL
5534 && *p_tmp == *p_item_b->p_prev_segment_uid )
5537 p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
5541 if ( p_item_b->p_segment_uid != NULL
5542 && *p_tmp == *p_item_b->p_segment_uid )
5545 if ( p_item_b->p_prev_segment_uid != NULL
5546 && *p_tmp == *p_item_b->p_prev_segment_uid )
5552 bool matroska_segment_c::Preload( )
5557 EbmlElement *el = NULL;
5559 ep->Reset( &sys.demuxer );
5561 while( ( el = ep->Get() ) != NULL )
5563 if( MKV_IS_ID( el, KaxInfo ) )
5565 ParseInfo( static_cast<KaxInfo*>( el ) );
5567 else if( MKV_IS_ID( el, KaxTracks ) )
5569 ParseTracks( static_cast<KaxTracks*>( el ) );
5570 if ( tracks.size() == 0 )
5572 msg_Err( &sys.demuxer, "No tracks supported" );
5576 else if( MKV_IS_ID( el, KaxSeekHead ) )
5578 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
5580 else if( MKV_IS_ID( el, KaxCues ) )
5582 msg_Dbg( &sys.demuxer, "| + Cues" );
5584 else if( MKV_IS_ID( el, KaxCluster ) )
5586 msg_Dbg( &sys.demuxer, "| + Cluster" );
5588 cluster = (KaxCluster*)el;
5590 i_cluster_pos = i_start_pos = cluster->GetElementPosition();
5594 /* stop parsing the stream */
5597 else if( MKV_IS_ID( el, KaxAttachments ) )
5599 msg_Dbg( &sys.demuxer, "| + Attachments" );
5600 ParseAttachments( static_cast<KaxAttachments*>( el ) );
5602 else if( MKV_IS_ID( el, KaxChapters ) )
5604 msg_Dbg( &sys.demuxer, "| + Chapters" );
5605 ParseChapters( static_cast<KaxChapters*>( el ) );
5607 else if( MKV_IS_ID( el, KaxTag ) )
5609 msg_Dbg( &sys.demuxer, "| + Tags FIXME TODO" );
5613 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid(*el).name() );
5622 matroska_segment_c *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
5624 for (size_t i=0; i<opened_segments.size(); i++)
5626 if ( *opened_segments[i]->p_segment_uid == uid )
5627 return opened_segments[i];
5632 chapter_item_c *demux_sys_t::BrowseCodecPrivate( unsigned int codec_id,
5633 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
5634 const void *p_cookie,
5635 size_t i_cookie_size,
5636 virtual_segment_c * &p_segment_found )
5638 chapter_item_c *p_result = NULL;
5639 for (size_t i=0; i<used_segments.size(); i++)
5641 p_result = used_segments[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
5642 if ( p_result != NULL )
5644 p_segment_found = used_segments[i];
5651 chapter_item_c *demux_sys_t::FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found )
5653 chapter_item_c *p_result = NULL;
5654 for (size_t i=0; i<used_segments.size(); i++)
5656 p_result = used_segments[i]->FindChapter( i_find_uid );
5657 if ( p_result != NULL )
5659 p_segment_found = used_segments[i];
5666 void virtual_segment_c::Sort()
5668 // keep the current segment index
5669 matroska_segment_c *p_segment = linked_segments[i_current_segment];
5671 std::sort( linked_segments.begin(), linked_segments.end(), matroska_segment_c::CompareSegmentUIDs );
5673 for ( i_current_segment=0; i_current_segment<linked_segments.size(); i_current_segment++)
5674 if ( linked_segments[i_current_segment] == p_segment )
5678 size_t virtual_segment_c::AddSegment( matroska_segment_c *p_segment )
5681 // check if it's not already in here
5682 for ( i=0; i<linked_segments.size(); i++ )
5684 if ( linked_segments[i]->p_segment_uid != NULL
5685 && p_segment->p_segment_uid != NULL
5686 && *p_segment->p_segment_uid == *linked_segments[i]->p_segment_uid )
5690 // find possible mates
5691 for ( i=0; i<linked_uids.size(); i++ )
5693 if ( (p_segment->p_segment_uid != NULL && *p_segment->p_segment_uid == linked_uids[i])
5694 || (p_segment->p_prev_segment_uid != NULL && *p_segment->p_prev_segment_uid == linked_uids[i])
5695 || (p_segment->p_next_segment_uid !=NULL && *p_segment->p_next_segment_uid == linked_uids[i]) )
5697 linked_segments.push_back( p_segment );
5699 AppendUID( p_segment->p_prev_segment_uid );
5700 AppendUID( p_segment->p_next_segment_uid );
5708 void virtual_segment_c::PreloadLinked( )
5710 for ( size_t i=0; i<linked_segments.size(); i++ )
5712 linked_segments[i]->Preload( );
5714 i_current_edition = linked_segments[0]->i_default_edition;
5717 mtime_t virtual_segment_c::Duration() const
5720 if ( linked_segments.size() == 0 )
5723 matroska_segment_c *p_last_segment = linked_segments[linked_segments.size()-1];
5724 // p_last_segment->ParseCluster( );
5726 i_duration = p_last_segment->i_start_time / 1000 + p_last_segment->i_duration;
5731 void virtual_segment_c::LoadCues( )
5733 for ( size_t i=0; i<linked_segments.size(); i++ )
5735 linked_segments[i]->LoadCues();
5739 void virtual_segment_c::AppendUID( const EbmlBinary * p_UID )
5741 if ( p_UID == NULL )
5743 if ( p_UID->GetBuffer() == NULL )
5746 for (size_t i=0; i<linked_uids.size(); i++)
5748 if ( *p_UID == linked_uids[i] )
5751 linked_uids.push_back( *(KaxSegmentUID*)(p_UID) );
5754 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset )
5757 #if LIBMATROSKA_VERSION >= 0x000800
5758 KaxSimpleBlock *simpleblock;
5760 int i_track_skipping;
5761 int64_t i_block_duration;
5762 int64_t i_block_ref1;
5763 int64_t i_block_ref2;
5765 int64_t i_seek_position = i_start_pos;
5766 int64_t i_seek_time = i_start_time;
5772 for( ; i_idx < i_index; i_idx++ )
5774 if( p_indexes[i_idx].i_time + i_time_offset > i_date )
5785 i_seek_position = p_indexes[i_idx].i_position;
5786 i_seek_time = p_indexes[i_idx].i_time;
5789 msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
5790 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
5792 es.I_O().setFilePointer( i_seek_position, seek_beginning );
5795 ep = new EbmlParser( &es, segment, &sys.demuxer );
5798 sys.i_start_pts = i_date;
5800 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
5802 /* now parse until key frame */
5803 i_track_skipping = 0;
5804 for( i_track = 0; i_track < tracks.size(); i_track++ )
5806 if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
5808 tracks[i_track]->b_search_keyframe = true;
5811 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, i_date );
5815 while( i_track_skipping > 0 )
5817 #if LIBMATROSKA_VERSION >= 0x000800
5818 if( BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
5820 if( BlockGet( block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
5823 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
5829 for( i_track = 0; i_track < tracks.size(); i_track++ )
5831 #if LIBMATROSKA_VERSION >= 0x000800
5832 if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
5833 (block && tracks[i_track]->i_number == block->TrackNum()) )
5835 if( tracks[i_track]->i_number == block->TrackNum() )
5842 #if LIBMATROSKA_VERSION >= 0x000800
5844 sys.i_pts = (sys.i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
5847 sys.i_pts = (sys.i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
5849 if( i_track < tracks.size() )
5851 if( sys.i_pts >= sys.i_start_pts )
5853 cluster = static_cast<KaxCluster*>(ep->UnGet( i_block_pos, i_cluster_pos ));
5854 i_track_skipping = 0;
5856 else if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
5858 if( i_block_ref1 == 0 && tracks[i_track]->b_search_keyframe )
5860 tracks[i_track]->b_search_keyframe = false;
5863 if( !tracks[i_track]->b_search_keyframe )
5865 #if LIBMATROSKA_VERSION >= 0x000800
5866 BlockDecode( &sys.demuxer, block, simpleblock, sys.i_pts, 0, i_block_ref1 >= 0 || i_block_ref2 > 0 );
5868 BlockDecode( &sys.demuxer, block, sys.i_pts, 0, i_block_ref1 >= 0 || i_block_ref2 > 0 );
5878 void virtual_segment_c::Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter )
5880 demux_sys_t *p_sys = demuxer.p_sys;
5883 // find the actual time for an ordered edition
5884 if ( psz_chapter == NULL )
5886 if ( Edition() && Edition()->b_ordered )
5888 /* 1st, we need to know in which chapter we are */
5889 psz_chapter = (*p_editions)[i_current_edition]->FindTimecode( i_date, psz_current_chapter );
5893 if ( psz_chapter != NULL )
5895 psz_current_chapter = psz_chapter;
5896 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
5897 if ( psz_chapter->i_seekpoint_num > 0 )
5899 demuxer.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
5900 demuxer.info.i_title = p_sys->i_current_title = i_sys_title;
5901 demuxer.info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
5905 // find the best matching segment
5906 for ( i=0; i<linked_segments.size(); i++ )
5908 if ( i_date < linked_segments[i]->i_start_time )
5915 if ( i_current_segment != i )
5917 linked_segments[i_current_segment]->UnSelect();
5918 linked_segments[i]->Select( i_date );
5919 i_current_segment = i;
5922 linked_segments[i]->Seek( i_date, i_time_offset );
5925 void chapter_codec_cmds_c::AddCommand( const KaxChapterProcessCommand & command )
5929 uint32 codec_time = uint32(-1);
5930 for( i = 0; i < command.ListSize(); i++ )
5932 const EbmlElement *k = command[i];
5934 if( MKV_IS_ID( k, KaxChapterProcessTime ) )
5936 codec_time = uint32( *static_cast<const KaxChapterProcessTime*>( k ) );
5941 for( i = 0; i < command.ListSize(); i++ )
5943 const EbmlElement *k = command[i];
5945 if( MKV_IS_ID( k, KaxChapterProcessData ) )
5947 KaxChapterProcessData *p_data = new KaxChapterProcessData( *static_cast<const KaxChapterProcessData*>( k ) );
5948 switch ( codec_time )
5951 during_cmds.push_back( p_data );
5954 enter_cmds.push_back( p_data );
5957 leave_cmds.push_back( p_data );
5966 bool chapter_item_c::Enter( bool b_do_subs )
5968 bool f_result = false;
5969 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
5970 while ( index != codecs.end() )
5972 f_result |= (*index)->Enter();
5979 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
5980 while ( index_ != sub_chapters.end() )
5982 f_result |= (*index_)->Enter( true );
5989 bool chapter_item_c::Leave( bool b_do_subs )
5991 bool f_result = false;
5992 b_is_leaving = true;
5993 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
5994 while ( index != codecs.end() )
5996 f_result |= (*index)->Leave();
6003 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
6004 while ( index_ != sub_chapters.end() )
6006 f_result |= (*index_)->Leave( true );
6010 b_is_leaving = false;
6014 bool chapter_item_c::EnterAndLeave( chapter_item_c *p_item, bool b_final_enter )
6016 chapter_item_c *p_common_parent = p_item;
6018 // leave, up to a common parent
6019 while ( p_common_parent != NULL && !p_common_parent->ParentOf( *this ) )
6021 if ( !p_common_parent->b_is_leaving && p_common_parent->Leave( false ) )
6023 p_common_parent = p_common_parent->psz_parent;
6026 // enter from the parent to <this>
6027 if ( p_common_parent != NULL )
6031 if ( p_common_parent == this )
6032 return Enter( true );
6034 for ( size_t i = 0; i<p_common_parent->sub_chapters.size(); i++ )
6036 if ( p_common_parent->sub_chapters[i]->ParentOf( *this ) )
6038 p_common_parent = p_common_parent->sub_chapters[i];
6039 if ( p_common_parent != this )
6040 if ( p_common_parent->Enter( false ) )
6049 if ( b_final_enter )
6050 return Enter( true );
6055 bool dvd_chapter_codec_c::Enter()
6057 bool f_result = false;
6058 std::vector<KaxChapterProcessData*>::iterator index = enter_cmds.begin();
6059 while ( index != enter_cmds.end() )
6061 if ( (*index)->GetSize() )
6063 binary *p_data = (*index)->GetBuffer();
6064 size_t i_size = *p_data++;
6065 // avoid reading too much from the buffer
6066 i_size = __MIN( i_size, ((*index)->GetSize() - 1) >> 3 );
6067 for ( ; i_size > 0; i_size--, p_data += 8 )
6069 msg_Dbg( &sys.demuxer, "Matroska DVD enter command" );
6070 f_result |= sys.dvd_interpretor.Interpret( p_data );
6078 bool dvd_chapter_codec_c::Leave()
6080 bool f_result = false;
6081 std::vector<KaxChapterProcessData*>::iterator index = leave_cmds.begin();
6082 while ( index != leave_cmds.end() )
6084 if ( (*index)->GetSize() )
6086 binary *p_data = (*index)->GetBuffer();
6087 size_t i_size = *p_data++;
6088 // avoid reading too much from the buffer
6089 i_size = __MIN( i_size, ((*index)->GetSize() - 1) >> 3 );
6090 for ( ; i_size > 0; i_size--, p_data += 8 )
6092 msg_Dbg( &sys.demuxer, "Matroska DVD leave command" );
6093 f_result |= sys.dvd_interpretor.Interpret( p_data );
6101 // see http://www.dvd-replica.com/DVD/vmcmdset.php for a description of DVD commands
6102 bool dvd_command_interpretor_c::Interpret( const binary * p_command, size_t i_size )
6107 virtual_segment_c *p_segment = NULL;
6108 chapter_item_c *p_chapter = NULL;
6109 bool f_result = false;
6110 uint16 i_command = ( p_command[0] << 8 ) + p_command[1];
6112 // handle register tests if there are some
6113 if ( (i_command & 0xF0) != 0 )
6115 bool b_test_positive = true;//(i_command & CMD_DVD_IF_NOT) == 0;
6116 bool b_test_value = (i_command & CMD_DVD_TEST_VALUE) != 0;
6117 uint8 i_test = i_command & 0x70;
6120 // see http://dvd.sourceforge.net/dvdinfo/vmi.html
6123 switch ( i_command >> 12 )
6126 i_cr1 = p_command[3];
6127 i_cr2 = (p_command[4] << 8) + p_command[5];
6132 i_cr1 = p_command[6];
6133 i_cr2 = p_command[7];
6134 b_test_value = false;
6138 if ( ((p_command[1] >> 4) & 0x7) == 0)
6140 i_cr1 = p_command[2];
6141 i_cr2 = (p_command[6] << 8) + p_command[7];
6145 i_cr1 = p_command[2];
6146 i_cr2 = (p_command[6] << 8) + p_command[7];
6154 i_value = GetPRM( i_cr2 );
6158 case CMD_DVD_IF_GPREG_EQUAL:
6160 msg_Dbg( &sys.demuxer, "IF %s EQUALS %s", GetRegTypeName( false, i_cr1 ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6161 if (!( GetPRM( i_cr1 ) == i_value ))
6163 b_test_positive = false;
6166 case CMD_DVD_IF_GPREG_NOT_EQUAL:
6168 msg_Dbg( &sys.demuxer, "IF %s NOT EQUALS %s", GetRegTypeName( false, i_cr1 ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6169 if (!( GetPRM( i_cr1 ) != i_value ))
6171 b_test_positive = false;
6174 case CMD_DVD_IF_GPREG_INF:
6176 msg_Dbg( &sys.demuxer, "IF %s < %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6177 if (!( GetPRM( i_cr1 ) < i_value ))
6179 b_test_positive = false;
6182 case CMD_DVD_IF_GPREG_INF_EQUAL:
6183 // if inferior or equal
6184 msg_Dbg( &sys.demuxer, "IF %s < %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6185 if (!( GetPRM( i_cr1 ) <= i_value ))
6187 b_test_positive = false;
6190 case CMD_DVD_IF_GPREG_AND:
6192 msg_Dbg( &sys.demuxer, "IF %s & %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6193 if (!( GetPRM( i_cr1 ) & i_value ))
6195 b_test_positive = false;
6198 case CMD_DVD_IF_GPREG_SUP:
6200 msg_Dbg( &sys.demuxer, "IF %s >= %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6201 if (!( GetPRM( i_cr1 ) > i_value ))
6203 b_test_positive = false;
6206 case CMD_DVD_IF_GPREG_SUP_EQUAL:
6207 // if superior or equal
6208 msg_Dbg( &sys.demuxer, "IF %s >= %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
6209 if (!( GetPRM( i_cr1 ) >= i_value ))
6211 b_test_positive = false;
6216 if ( !b_test_positive )
6220 // strip the test command
6221 i_command &= 0xFF0F;
6223 switch ( i_command )
6228 msg_Dbg( &sys.demuxer, "NOP" );
6233 msg_Dbg( &sys.demuxer, "Break" );
6237 case CMD_DVD_JUMP_TT:
6239 uint8 i_title = p_command[5];
6240 msg_Dbg( &sys.demuxer, "JumpTT %d", i_title );
6242 // find in the ChapProcessPrivate matching this Title level
6243 p_chapter = sys.BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title), p_segment );
6244 if ( p_segment != NULL )
6246 sys.JumpTo( *p_segment, p_chapter );
6252 case CMD_DVD_CALLSS_VTSM1:
6254 msg_Dbg( &sys.demuxer, "CallSS" );
6256 switch( (p_command[6] & 0xC0) >> 6 ) {
6258 p_type = p_command[5] & 0x0F;
6262 msg_Dbg( &sys.demuxer, "CallSS PGC (rsm_cell %x)", p_command[4]);
6265 msg_Dbg( &sys.demuxer, "CallSS Title Entry (rsm_cell %x)", p_command[4]);
6268 msg_Dbg( &sys.demuxer, "CallSS Root Menu (rsm_cell %x)", p_command[4]);
6271 msg_Dbg( &sys.demuxer, "CallSS Subpicture Menu (rsm_cell %x)", p_command[4]);
6274 msg_Dbg( &sys.demuxer, "CallSS Audio Menu (rsm_cell %x)", p_command[4]);
6277 msg_Dbg( &sys.demuxer, "CallSS Angle Menu (rsm_cell %x)", p_command[4]);
6280 msg_Dbg( &sys.demuxer, "CallSS Chapter Menu (rsm_cell %x)", p_command[4]);
6283 msg_Dbg( &sys.demuxer, "CallSS <unknown> (rsm_cell %x)", p_command[4]);
6286 p_chapter = sys.BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1, p_segment );
6287 if ( p_segment != NULL )
6289 sys.JumpTo( *p_segment, p_chapter );
6294 msg_Dbg( &sys.demuxer, "CallSS VMGM (menu %d, rsm_cell %x)", p_command[5] & 0x0F, p_command[4]);
6297 msg_Dbg( &sys.demuxer, "CallSS VTSM (menu %d, rsm_cell %x)", p_command[5] & 0x0F, p_command[4]);
6300 msg_Dbg( &sys.demuxer, "CallSS VMGM (pgc %d, rsm_cell %x)", (p_command[2] << 8) + p_command[3], p_command[4]);
6305 case CMD_DVD_JUMP_SS:
6307 msg_Dbg( &sys.demuxer, "JumpSS");
6309 switch( (p_command[5] & 0xC0) >> 6 ) {
6311 msg_Dbg( &sys.demuxer, "JumpSS FP");
6314 p_type = p_command[5] & 0x0F;
6318 msg_Dbg( &sys.demuxer, "JumpSS VMGM Title Entry");
6321 msg_Dbg( &sys.demuxer, "JumpSS VMGM Root Menu");
6324 msg_Dbg( &sys.demuxer, "JumpSS VMGM Subpicture Menu");
6327 msg_Dbg( &sys.demuxer, "JumpSS VMGM Audio Menu");
6330 msg_Dbg( &sys.demuxer, "JumpSS VMGM Angle Menu");
6333 msg_Dbg( &sys.demuxer, "JumpSS VMGM Chapter Menu");
6336 msg_Dbg( &sys.demuxer, "JumpSS <unknown>");
6340 p_chapter = sys.BrowseCodecPrivate( 1, MatchIsVMG, NULL, 0, p_segment );
6341 if ( p_segment != NULL )
6343 p_chapter = p_segment->BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1 );
6344 if ( p_chapter != NULL )
6346 sys.JumpTo( *p_segment, p_chapter );
6352 p_type = p_command[5] & 0x0F;
6356 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Title Entry", p_command[4], p_command[3]);
6359 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Root Menu", p_command[4], p_command[3]);
6362 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Subpicture Menu", p_command[4], p_command[3]);
6365 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Audio Menu", p_command[4], p_command[3]);
6368 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Angle Menu", p_command[4], p_command[3]);
6371 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) Chapter Menu", p_command[4], p_command[3]);
6374 msg_Dbg( &sys.demuxer, "JumpSS VTSM (vts %d, ttn %d) <unknown>", p_command[4], p_command[3]);
6378 p_chapter = sys.BrowseCodecPrivate( 1, MatchVTSMNumber, &p_command[4], 1, p_segment );
6380 if ( p_segment != NULL && p_chapter != NULL )
6382 // find the title in the VTS
6383 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchTitleNumber, &p_command[3], 1 );
6384 if ( p_chapter != NULL )
6386 // find the specified menu in the VTSM
6387 p_chapter = p_segment->BrowseCodecPrivate( 1, MatchPgcType, &p_type, 1 );
6388 if ( p_chapter != NULL )
6390 sys.JumpTo( *p_segment, p_chapter );
6395 msg_Dbg( &sys.demuxer, "Title (%d) does not exist in this VTS", p_command[3] );
6398 msg_Dbg( &sys.demuxer, "DVD Domain VTS (%d) not found", p_command[4] );
6401 msg_Dbg( &sys.demuxer, "JumpSS VMGM (pgc %d)", (p_command[2] << 8) + p_command[3]);
6406 case CMD_DVD_JUMPVTS_PTT:
6408 uint8 i_title = p_command[5];
6409 uint8 i_ptt = p_command[3];
6411 msg_Dbg( &sys.demuxer, "JumpVTS Title (%d) PTT (%d)", i_title, i_ptt);
6413 // find the current VTS content segment
6414 p_chapter = sys.p_current_segment->BrowseCodecPrivate( 1, MatchIsDomain, NULL, 0 );
6415 if ( p_chapter != NULL )
6417 int16 i_curr_title = p_chapter->GetTitleNumber( );
6418 if ( i_curr_title > 0 )
6420 p_chapter = sys.BrowseCodecPrivate( 1, MatchVTSNumber, &i_curr_title, sizeof(i_curr_title), p_segment );
6422 if ( p_segment != NULL && p_chapter != NULL )
6424 // find the title in the VTS
6425 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title) );
6426 if ( p_chapter != NULL )
6428 // find the chapter in the title
6429 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchChapterNumber, &i_ptt, sizeof(i_ptt) );
6430 if ( p_chapter != NULL )
6432 sys.JumpTo( *p_segment, p_chapter );
6437 msg_Dbg( &sys.demuxer, "Title (%d) does not exist in this VTS", i_title );
6440 msg_Dbg( &sys.demuxer, "DVD Domain VTS (%d) not found", i_curr_title );
6443 msg_Dbg( &sys.demuxer, "JumpVTS_PTT command found but not in a VTS(M)");
6446 msg_Dbg( &sys.demuxer, "JumpVTS_PTT command but the DVD domain wasn't found");
6449 case CMD_DVD_SET_GPRMMD:
6451 msg_Dbg( &sys.demuxer, "Set GPRMMD [%d]=%d", (p_command[4] << 8) + p_command[5], (p_command[2] << 8) + p_command[3]);
6453 if ( !SetGPRM( (p_command[4] << 8) + p_command[5], (p_command[2] << 8) + p_command[3] ) )
6454 msg_Dbg( &sys.demuxer, "Set GPRMMD failed" );
6457 case CMD_DVD_LINKPGCN:
6459 uint16 i_pgcn = (p_command[6] << 8) + p_command[7];
6461 msg_Dbg( &sys.demuxer, "Link PGCN(%d)", i_pgcn );
6462 p_chapter = sys.p_current_segment->BrowseCodecPrivate( 1, MatchPgcNumber, &i_pgcn, 2 );
6463 if ( p_chapter != NULL )
6465 if ( !p_chapter->Enter( true ) )
6466 // jump to the location in the found segment
6467 sys.p_current_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
6473 case CMD_DVD_LINKCN:
6475 uint8 i_cn = p_command[7];
6477 p_chapter = sys.p_current_segment->CurrentChapter();
6479 msg_Dbg( &sys.demuxer, "LinkCN (cell %d)", i_cn );
6480 p_chapter = p_chapter->BrowseCodecPrivate( 1, MatchCellNumber, &i_cn, 1 );
6481 if ( p_chapter != NULL )
6483 if ( !p_chapter->Enter( true ) )
6484 // jump to the location in the found segment
6485 sys.p_current_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
6491 case CMD_DVD_GOTO_LINE:
6493 msg_Dbg( &sys.demuxer, "GotoLine (%d)", (p_command[6] << 8) + p_command[7] );
6497 case CMD_DVD_SET_HL_BTNN1:
6499 msg_Dbg( &sys.demuxer, "SetHL_BTN (%d)", p_command[4] );
6500 SetSPRM( 0x88, p_command[4] );
6505 msg_Dbg( &sys.demuxer, "unsupported command : %02X %02X %02X %02X %02X %02X %02X %02X"
6521 bool dvd_command_interpretor_c::MatchIsDomain( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6523 return ( data.p_private_data != NULL && data.p_private_data->GetBuffer()[0] == MATROSKA_DVD_LEVEL_SS );
6526 bool dvd_command_interpretor_c::MatchIsVMG( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6528 if ( data.p_private_data == NULL || data.p_private_data->GetSize() < 2 )
6531 return ( data.p_private_data->GetBuffer()[0] == MATROSKA_DVD_LEVEL_SS && data.p_private_data->GetBuffer()[1] == 0xC0);
6534 bool dvd_command_interpretor_c::MatchVTSNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6536 if ( i_cookie_size != 2 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6539 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_SS || data.p_private_data->GetBuffer()[1] != 0x80 )
6542 uint16 i_gtitle = (data.p_private_data->GetBuffer()[2] << 8 ) + data.p_private_data->GetBuffer()[3];
6543 uint16 i_title = *(uint16*)p_cookie;
6545 return (i_gtitle == i_title);
6548 bool dvd_command_interpretor_c::MatchVTSMNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6550 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6553 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_SS || data.p_private_data->GetBuffer()[1] != 0x40 )
6556 uint8 i_gtitle = data.p_private_data->GetBuffer()[3];
6557 uint8 i_title = *(uint8*)p_cookie;
6559 return (i_gtitle == i_title);
6562 bool dvd_command_interpretor_c::MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6564 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 4 )
6567 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_TT )
6570 uint16 i_gtitle = (data.p_private_data->GetBuffer()[1] << 8 ) + data.p_private_data->GetBuffer()[2];
6571 uint8 i_title = *(uint8*)p_cookie;
6573 return (i_gtitle == i_title);
6576 bool dvd_command_interpretor_c::MatchPgcType( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6578 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 8 )
6581 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PGC )
6584 uint8 i_pgc_type = data.p_private_data->GetBuffer()[3] & 0x0F;
6585 uint8 i_pgc = *(uint8*)p_cookie;
6587 return (i_pgc_type == i_pgc);
6590 bool dvd_command_interpretor_c::MatchPgcNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6592 if ( i_cookie_size != 2 || data.p_private_data == NULL || data.p_private_data->GetSize() < 8 )
6595 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PGC )
6598 uint16 *i_pgc_n = (uint16 *)p_cookie;
6599 uint16 i_pgc_num = (data.p_private_data->GetBuffer()[1] << 8) + data.p_private_data->GetBuffer()[2];
6601 return (i_pgc_num == *i_pgc_n);
6604 bool dvd_command_interpretor_c::MatchChapterNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6606 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 2 )
6609 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_PTT )
6612 uint8 i_chapter = data.p_private_data->GetBuffer()[1];
6613 uint8 i_ptt = *(uint8*)p_cookie;
6615 return (i_chapter == i_ptt);
6618 bool dvd_command_interpretor_c::MatchCellNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
6620 if ( i_cookie_size != 1 || data.p_private_data == NULL || data.p_private_data->GetSize() < 5 )
6623 if ( data.p_private_data->GetBuffer()[0] != MATROSKA_DVD_LEVEL_CN )
6626 uint8 *i_cell_n = (uint8 *)p_cookie;
6627 uint8 i_cell_num = data.p_private_data->GetBuffer()[3];
6629 return (i_cell_num == *i_cell_n);
6632 bool matroska_script_codec_c::Enter()
6634 bool f_result = false;
6635 std::vector<KaxChapterProcessData*>::iterator index = enter_cmds.begin();
6636 while ( index != enter_cmds.end() )
6638 if ( (*index)->GetSize() )
6640 msg_Dbg( &sys.demuxer, "Matroska Script enter command" );
6641 f_result |= interpretor.Interpret( (*index)->GetBuffer(), (*index)->GetSize() );
6648 bool matroska_script_codec_c::Leave()
6650 bool f_result = false;
6651 std::vector<KaxChapterProcessData*>::iterator index = leave_cmds.begin();
6652 while ( index != leave_cmds.end() )
6654 if ( (*index)->GetSize() )
6656 msg_Dbg( &sys.demuxer, "Matroska Script leave command" );
6657 f_result |= interpretor.Interpret( (*index)->GetBuffer(), (*index)->GetSize() );
6664 // see http://www.matroska.org/technical/specs/chapters/index.html#mscript
6665 // for a description of existing commands
6666 bool matroska_script_interpretor_c::Interpret( const binary * p_command, size_t i_size )
6668 bool b_result = false;
6670 char *psz_str = (char*) malloc( i_size + 1 );
6671 memcpy( psz_str, p_command, i_size );
6672 psz_str[ i_size ] = '\0';
6674 std::string sz_command = psz_str;
6677 msg_Dbg( &sys.demuxer, "command : %s", sz_command.c_str() );
6679 #if defined(__GNUC__) && (__GNUC__ < 3)
6680 if ( sz_command.compare( CMD_MS_GOTO_AND_PLAY, 0, CMD_MS_GOTO_AND_PLAY.size() ) == 0 )
6682 if ( sz_command.compare( 0, CMD_MS_GOTO_AND_PLAY.size(), CMD_MS_GOTO_AND_PLAY ) == 0 )
6688 for ( i=CMD_MS_GOTO_AND_PLAY.size(); i<sz_command.size(); i++)
6690 if ( sz_command[i] == '(' )
6697 for ( j=i; j<sz_command.size(); j++)
6699 if ( sz_command[j] == ')' )
6706 std::string st = sz_command.substr( i+1, j-i-1 );
6707 int64_t i_chapter_uid = atoi( st.c_str() );
6709 virtual_segment_c *p_segment;
6710 chapter_item_c *p_chapter = sys.FindChapter( i_chapter_uid, p_segment );
6712 if ( p_chapter == NULL )
6713 msg_Dbg( &sys.demuxer, "Chapter %"PRId64" not found", i_chapter_uid);
6716 if ( !p_chapter->EnterAndLeave( sys.p_current_segment->CurrentChapter() ) )
6717 p_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
6725 void demux_sys_t::SwapButtons()
6727 #ifndef WORDS_BIGENDIAN
6728 uint8_t button, i, j;
6730 for( button = 1; button <= pci_packet.hli.hl_gi.btn_ns; button++) {
6731 btni_t *button_ptr = &(pci_packet.hli.btnit[button-1]);
6732 binary *p_data = (binary*) button_ptr;
6734 uint16 i_x_start = ((p_data[0] & 0x3F) << 4 ) + ( p_data[1] >> 4 );
6735 uint16 i_x_end = ((p_data[1] & 0x03) << 8 ) + p_data[2];
6736 uint16 i_y_start = ((p_data[3] & 0x3F) << 4 ) + ( p_data[4] >> 4 );
6737 uint16 i_y_end = ((p_data[4] & 0x03) << 8 ) + p_data[5];
6738 button_ptr->x_start = i_x_start;
6739 button_ptr->x_end = i_x_end;
6740 button_ptr->y_start = i_y_start;
6741 button_ptr->y_end = i_y_end;
6744 for ( i = 0; i<3; i++ )
6746 for ( j = 0; j<2; j++ )
6748 pci_packet.hli.btn_colit.btn_coli[i][j] = U32_AT( &pci_packet.hli.btn_colit.btn_coli[i][j] );