1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
33 # include <time.h> /* time() */
36 #include <vlc/input.h>
38 #include <codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
53 /* libebml and matroska */
54 #include "ebml/EbmlHead.h"
55 #include "ebml/EbmlSubHead.h"
56 #include "ebml/EbmlStream.h"
57 #include "ebml/EbmlContexts.h"
58 #include "ebml/EbmlVoid.h"
59 #include "ebml/EbmlVersion.h"
60 #include "ebml/StdIOCallback.h"
62 #include "matroska/KaxAttachments.h"
63 #include "matroska/KaxBlock.h"
64 #include "matroska/KaxBlockData.h"
65 #include "matroska/KaxChapters.h"
66 #include "matroska/KaxCluster.h"
67 #include "matroska/KaxClusterData.h"
68 #include "matroska/KaxContexts.h"
69 #include "matroska/KaxCues.h"
70 #include "matroska/KaxCuesData.h"
71 #include "matroska/KaxInfo.h"
72 #include "matroska/KaxInfoData.h"
73 #include "matroska/KaxSeekHead.h"
74 #include "matroska/KaxSegment.h"
75 #include "matroska/KaxTag.h"
76 #include "matroska/KaxTags.h"
77 #include "matroska/KaxTagMulti.h"
78 #include "matroska/KaxTracks.h"
79 #include "matroska/KaxTrackAudio.h"
80 #include "matroska/KaxTrackVideo.h"
81 #include "matroska/KaxTrackEntryData.h"
82 #include "matroska/KaxContentEncoding.h"
83 #include "matroska/KaxVersion.h"
85 #include "ebml/StdIOCallback.h"
90 #include "mp4/libmp4.h"
96 #define MATROSKA_COMPRESSION_NONE 0
97 #define MATROSKA_COMPRESSION_ZLIB 1
99 #define MKVD_TIMECODESCALE 1000000
101 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
102 #undef ATTRIBUTE_PACKED
103 #undef PRAGMA_PACK_BEGIN
104 #undef PRAGMA_PACK_END
106 #if defined(__GNUC__)
107 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
108 #define ATTRIBUTE_PACKED __attribute__ ((packed))
109 #define PRAGMA_PACK 0
113 #if !defined(ATTRIBUTE_PACKED)
114 #define ATTRIBUTE_PACKED
115 #define PRAGMA_PACK 1
122 /*************************************
123 * taken from libdvdnav / libdvdread
124 **************************************/
127 * DVD Time Information.
133 uint8_t frame_u; /* The two high bits are the frame rate. */
134 } ATTRIBUTE_PACKED dvd_time_t;
140 #ifdef WORDS_BIGENDIAN
141 unsigned int zero : 7; /* 25-31 */
142 unsigned int video_pres_mode_change : 1; /* 24 */
144 unsigned int karaoke_audio_pres_mode_change : 1; /* 23 */
145 unsigned int angle_change : 1;
146 unsigned int subpic_stream_change : 1;
147 unsigned int audio_stream_change : 1;
148 unsigned int pause_on : 1;
149 unsigned int still_off : 1;
150 unsigned int button_select_or_activate : 1;
151 unsigned int resume : 1; /* 16 */
153 unsigned int chapter_menu_call : 1; /* 15 */
154 unsigned int angle_menu_call : 1;
155 unsigned int audio_menu_call : 1;
156 unsigned int subpic_menu_call : 1;
157 unsigned int root_menu_call : 1;
158 unsigned int title_menu_call : 1;
159 unsigned int backward_scan : 1;
160 unsigned int forward_scan : 1; /* 8 */
162 unsigned int next_pg_search : 1; /* 7 */
163 unsigned int prev_or_top_pg_search : 1;
164 unsigned int time_or_chapter_search : 1;
165 unsigned int go_up : 1;
166 unsigned int stop : 1;
167 unsigned int title_play : 1;
168 unsigned int chapter_search_or_play : 1;
169 unsigned int title_or_time_play : 1; /* 0 */
171 unsigned int video_pres_mode_change : 1; /* 24 */
172 unsigned int zero : 7; /* 25-31 */
174 unsigned int resume : 1; /* 16 */
175 unsigned int button_select_or_activate : 1;
176 unsigned int still_off : 1;
177 unsigned int pause_on : 1;
178 unsigned int audio_stream_change : 1;
179 unsigned int subpic_stream_change : 1;
180 unsigned int angle_change : 1;
181 unsigned int karaoke_audio_pres_mode_change : 1; /* 23 */
183 unsigned int forward_scan : 1; /* 8 */
184 unsigned int backward_scan : 1;
185 unsigned int title_menu_call : 1;
186 unsigned int root_menu_call : 1;
187 unsigned int subpic_menu_call : 1;
188 unsigned int audio_menu_call : 1;
189 unsigned int angle_menu_call : 1;
190 unsigned int chapter_menu_call : 1; /* 15 */
192 unsigned int title_or_time_play : 1; /* 0 */
193 unsigned int chapter_search_or_play : 1;
194 unsigned int title_play : 1;
195 unsigned int stop : 1;
196 unsigned int go_up : 1;
197 unsigned int time_or_chapter_search : 1;
198 unsigned int prev_or_top_pg_search : 1;
199 unsigned int next_pg_search : 1; /* 7 */
201 } ATTRIBUTE_PACKED user_ops_t;
204 * Type to store per-command data.
208 } ATTRIBUTE_PACKED vm_cmd_t;
209 #define COMMAND_DATA_SIZE 8
212 * PCI General Information
215 uint32_t nv_pck_lbn; /**< sector address of this nav pack */
216 uint16_t vobu_cat; /**< 'category' of vobu */
217 uint16_t zero1; /**< reserved */
218 user_ops_t vobu_uop_ctl; /**< UOP of vobu */
219 uint32_t vobu_s_ptm; /**< start presentation time of vobu */
220 uint32_t vobu_e_ptm; /**< end presentation time of vobu */
221 uint32_t vobu_se_e_ptm; /**< end ptm of sequence end in vobu */
222 dvd_time_t e_eltm; /**< Cell elapsed time */
224 } ATTRIBUTE_PACKED pci_gi_t;
227 * Non Seamless Angle Information
230 uint32_t nsml_agl_dsta[9]; /**< address of destination vobu in AGL_C#n */
231 } ATTRIBUTE_PACKED nsml_agli_t;
234 * Highlight General Information
236 * For btngrX_dsp_ty the bits have the following meaning:
237 * 000b: normal 4/3 only buttons
238 * XX1b: wide (16/9) buttons
239 * X1Xb: letterbox buttons
240 * 1XXb: pan&scan buttons
243 uint16_t hli_ss; /**< status, only low 2 bits 0: no buttons, 1: different 2: equal 3: eual except for button cmds */
244 uint32_t hli_s_ptm; /**< start ptm of hli */
245 uint32_t hli_e_ptm; /**< end ptm of hli */
246 uint32_t btn_se_e_ptm; /**< end ptm of button select */
247 #ifdef WORDS_BIGENDIAN
248 unsigned char zero1 : 2; /**< reserved */
249 unsigned char btngr_ns : 2; /**< number of button groups 1, 2 or 3 with 36/18/12 buttons */
250 unsigned char zero2 : 1; /**< reserved */
251 unsigned char btngr1_dsp_ty : 3; /**< display type of subpic stream for button group 1 */
252 unsigned char zero3 : 1; /**< reserved */
253 unsigned char btngr2_dsp_ty : 3; /**< display type of subpic stream for button group 2 */
254 unsigned char zero4 : 1; /**< reserved */
255 unsigned char btngr3_dsp_ty : 3; /**< display type of subpic stream for button group 3 */
257 unsigned char btngr1_dsp_ty : 3;
258 unsigned char zero2 : 1;
259 unsigned char btngr_ns : 2;
260 unsigned char zero1 : 2;
261 unsigned char btngr3_dsp_ty : 3;
262 unsigned char zero4 : 1;
263 unsigned char btngr2_dsp_ty : 3;
264 unsigned char zero3 : 1;
266 uint8_t btn_ofn; /**< button offset number range 0-255 */
267 uint8_t btn_ns; /**< number of valid buttons <= 36/18/12 (low 6 bits) */
268 uint8_t nsl_btn_ns; /**< number of buttons selectable by U_BTNNi (low 6 bits) nsl_btn_ns <= btn_ns */
269 uint8_t zero5; /**< reserved */
270 uint8_t fosl_btnn; /**< forcedly selected button (low 6 bits) */
271 uint8_t foac_btnn; /**< forcedly activated button (low 6 bits) */
272 } ATTRIBUTE_PACKED hl_gi_t;
276 * Button Color Information Table
277 * Each entry beeing a 32bit word that contains the color indexs and alpha
278 * values to use. They are all represented by 4 bit number and stored
279 * like this [Ci3, Ci2, Ci1, Ci0, A3, A2, A1, A0]. The actual palette
280 * that the indexes reference is in the PGC.
281 * @TODO split the uint32_t into a struct
284 uint32_t btn_coli[3][2]; /**< [button color number-1][select:0/action:1] */
285 } ATTRIBUTE_PACKED btn_colit_t;
290 * NOTE: I've had to change the structure from the disk layout to get
291 * the packing to work with Sun's Forte C compiler.
292 * The 4 and 7 bytes are 'rotated' was: ABC DEF GHIJ is: ABCG DEFH IJ
295 #ifdef WORDS_BIGENDIAN
296 unsigned int btn_coln : 2; /**< button color number */
297 unsigned int x_start : 10; /**< x start offset within the overlay */
298 unsigned int zero1 : 2; /**< reserved */
299 unsigned int x_end : 10; /**< x end offset within the overlay */
301 unsigned int zero3 : 2; /**< reserved */
302 unsigned int up : 6; /**< button index when pressing up */
304 unsigned int auto_action_mode : 2; /**< 0: no, 1: activated if selected */
305 unsigned int y_start : 10; /**< y start offset within the overlay */
306 unsigned int zero2 : 2; /**< reserved */
307 unsigned int y_end : 10; /**< y end offset within the overlay */
309 unsigned int zero4 : 2; /**< reserved */
310 unsigned int down : 6; /**< button index when pressing down */
311 unsigned char zero5 : 2; /**< reserved */
312 unsigned char left : 6; /**< button index when pressing left */
313 unsigned char zero6 : 2; /**< reserved */
314 unsigned char right : 6; /**< button index when pressing right */
316 unsigned int x_end : 10;
317 unsigned int zero1 : 2;
318 unsigned int x_start : 10;
319 unsigned int btn_coln : 2;
322 unsigned int zero3 : 2;
324 unsigned int y_end : 10;
325 unsigned int zero2 : 2;
326 unsigned int y_start : 10;
327 unsigned int auto_action_mode : 2;
329 unsigned int down : 6;
330 unsigned int zero4 : 2;
331 unsigned char left : 6;
332 unsigned char zero5 : 2;
333 unsigned char right : 6;
334 unsigned char zero6 : 2;
337 } ATTRIBUTE_PACKED btni_t;
340 * Highlight Information
344 btn_colit_t btn_colit;
346 } ATTRIBUTE_PACKED hli_t;
353 nsml_agli_t nsml_agli;
356 } ATTRIBUTE_PACKED pci_t;
363 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
367 * What's between a directory and a filename?
370 #define DIRECTORY_SEPARATOR '\\'
372 #define DIRECTORY_SEPARATOR '/'
375 using namespace LIBMATROSKA_NAMESPACE;
378 /*****************************************************************************
380 *****************************************************************************/
381 static int Open ( vlc_object_t * );
382 static void Close( vlc_object_t * );
385 set_shortname( _("Matroska") );
386 set_description( _("Matroska stream demuxer" ) );
387 set_capability( "demux2", 50 );
388 set_callbacks( Open, Close );
389 set_category( CAT_INPUT );
390 set_subcategory( SUBCAT_INPUT_DEMUX );
392 add_bool( "mkv-use-ordered-chapters", 1, NULL,
393 N_("Ordered chapters"),
394 N_("Play ordered chapters as specified in the segment"), VLC_TRUE );
396 add_bool( "mkv-use-chapter-codec", 1, NULL,
397 N_("Chapter codecs"),
398 N_("Use chapter codecs found in the segment"), VLC_TRUE );
400 add_bool( "mkv-seek-percent", 0, NULL,
401 N_("Seek based on percent not time"),
402 N_("Seek based on percent not time"), VLC_TRUE );
404 add_bool( "mkv-use-dummy", 0, NULL,
405 N_("Dummy Elements"),
406 N_("Read and discard unknown EBML elements (not good for broken files)"), VLC_TRUE );
408 add_shortcut( "mka" );
409 add_shortcut( "mkv" );
412 /*****************************************************************************
414 *****************************************************************************/
416 block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) {
417 int result, dstsize, n;
422 d_stream.zalloc = (alloc_func)0;
423 d_stream.zfree = (free_func)0;
424 d_stream.opaque = (voidpf)0;
425 result = inflateInit(&d_stream);
428 msg_Dbg( p_this, "inflateInit() failed. Result: %d", result );
432 d_stream.next_in = (Bytef *)p_in_block->p_buffer;
433 d_stream.avail_in = p_in_block->i_buffer;
435 p_block = block_New( p_this, 0 );
440 p_block = block_Realloc( p_block, 0, n * 1000 );
441 dst = (unsigned char *)p_block->p_buffer;
442 d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
443 d_stream.avail_out = 1000;
444 result = inflate(&d_stream, Z_NO_FLUSH);
445 if( ( result != Z_OK ) && ( result != Z_STREAM_END ) )
447 msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result );
451 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
452 ( result != Z_STREAM_END ) );
454 dstsize = d_stream.total_out;
455 inflateEnd( &d_stream );
457 p_block = block_Realloc( p_block, 0, dstsize );
458 p_block->i_buffer = dstsize;
459 block_Release( p_in_block );
466 * Helper function to print the mkv parse tree
468 static void MkvTree( demux_t & demuxer, int i_level, char *psz_format, ... )
473 msg_Err( &demuxer, "too deep tree" );
476 va_start( args, psz_format );
477 static char *psz_foo = "| | | | | | | | | |";
478 char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) );
479 strncpy( psz_foo2, psz_foo, 4 * i_level );
480 psz_foo2[ 4 * i_level ] = '+';
481 psz_foo2[ 4 * i_level + 1 ] = ' ';
482 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
483 __msg_GenericVa( VLC_OBJECT(&demuxer), VLC_MSG_DBG, "mkv", psz_foo2, args );
488 /*****************************************************************************
490 *****************************************************************************/
491 class vlc_stream_io_callback: public IOCallback
498 vlc_stream_io_callback( stream_t * );
500 virtual uint32 read ( void *p_buffer, size_t i_size);
501 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
502 virtual size_t write ( const void *p_buffer, size_t i_size);
503 virtual uint64 getFilePointer ( void );
504 virtual void close ( void );
507 /*****************************************************************************
509 *****************************************************************************/
513 EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux );
514 virtual ~EbmlParser( void );
518 void Reset( demux_t *p_demux );
519 EbmlElement *Get( void );
522 int GetLevel( void );
527 EbmlElement *m_el[10];
528 int64_t mi_remain_size[10];
538 /*****************************************************************************
539 * Some functions to manipulate memory
540 *****************************************************************************/
541 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
542 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
544 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
547 /*****************************************************************************
548 * definitions of structures and functions used by this plugins
549 *****************************************************************************/
552 vlc_bool_t b_default;
553 vlc_bool_t b_enabled;
554 unsigned int i_number;
557 uint8_t *p_extra_data;
561 uint64_t i_default_duration;
562 float f_timecodescale;
570 /* data to be send first */
572 uint8_t *p_data_init;
574 /* hack : it's for seek */
575 vlc_bool_t b_search_keyframe;
579 char *psz_codec_name;
580 char *psz_codec_settings;
581 char *psz_codec_info_url;
582 char *psz_codec_download_url;
584 /* encryption/compression */
585 int i_compression_type;
602 const binary MATROSKA_DVD_LEVEL_SS = 0x30;
603 const binary MATROSKA_DVD_LEVEL_LU = 0x2A;
604 const binary MATROSKA_DVD_LEVEL_TT = 0x28;
605 const binary MATROSKA_DVD_LEVEL_PGC = 0x20;
606 const binary MATROSKA_DVD_LEVEL_PG = 0x18;
607 const binary MATROSKA_DVD_LEVEL_PTT = 0x10;
608 const binary MATROSKA_DVD_LEVEL_CN = 0x08;
610 class chapter_codec_cmds_c
613 chapter_codec_cmds_c( demux_sys_t & demuxer, int codec_id = -1)
614 :i_codec_id( codec_id )
618 virtual ~chapter_codec_cmds_c() {}
620 void SetPrivate( const KaxChapterProcessPrivate & private_data )
622 m_private_data = *( new KaxChapterProcessPrivate( private_data ) );
625 void AddCommand( const KaxChapterProcessCommand & command );
627 /// \return wether the codec has seeked in the files or not
628 virtual bool Enter() { return false; }
629 virtual bool Leave() { return false; }
630 virtual std::string GetCodecName( bool f_for_title = false ) const { return ""; }
632 KaxChapterProcessPrivate m_private_data;
635 std::vector<KaxChapterProcessData> enter_cmds;
636 std::vector<KaxChapterProcessData> during_cmds;
637 std::vector<KaxChapterProcessData> leave_cmds;
643 class dvd_command_interpretor_c
646 dvd_command_interpretor_c( demux_sys_t & demuxer )
649 memset( p_PRMs, 0, sizeof(p_PRMs) );
650 p_PRMs[ 0x80 + 1 ] = 15;
651 p_PRMs[ 0x80 + 2 ] = 62;
652 p_PRMs[ 0x80 + 3 ] = 1;
653 p_PRMs[ 0x80 + 4 ] = 1;
654 p_PRMs[ 0x80 + 7 ] = 1;
655 p_PRMs[ 0x80 + 8 ] = 1;
656 p_PRMs[ 0x80 + 16 ] = 0xFFFFu;
657 p_PRMs[ 0x80 + 18 ] = 0xFFFFu;
660 bool Interpret( const binary * p_command, size_t i_size = 8 );
663 uint16 GetPRM( size_t index ) const
666 return p_PRMs[ index ];
670 uint16 GetGPRM( size_t index ) const
672 if ( index >= 0 && index < 16 )
673 return p_PRMs[ index ];
677 uint16 GetSPRM( size_t index ) const
679 // 21,22,23 reserved for future use
680 if ( index >= 0x80 && index < 0x95 )
681 return p_PRMs[ index ];
685 bool SetPRM( size_t index, uint16 value )
687 if ( index >= 0 && index < 16 )
689 p_PRMs[ index ] = value;
695 bool SetGPRM( size_t index, uint16 value )
697 if ( index >= 0 && index < 16 )
699 p_PRMs[ index ] = value;
705 bool SetSPRM( size_t index, uint16 value )
707 if ( index > 0x80 && index <= 0x8D && index != 0x8C )
709 p_PRMs[ index ] = value;
715 std::string GetRegTypeName( bool b_value, uint16 value ) const
718 char s_value[6], s_reg_value[6];
719 sprintf( s_value, "%.5d", value );
727 else if ( value < 0x80 )
729 sprintf( s_reg_value, "%.5d", GetPRM( value ) );
733 result += s_reg_value;
738 sprintf( s_reg_value, "%.5d", GetPRM( value ) );
742 result += s_reg_value;
754 // wether the test has to be positive or not
755 static const uint16 CMD_DVD_IF_NOT = 0x10;
756 // wether it's a comparison on the value or register
757 static const uint16 CMD_DVD_TEST_VALUE = 0x80;
758 static const uint16 CMD_DVD_IF_GPREG_AND = (0 << 5);
759 static const uint16 CMD_DVD_IF_GPREG_EQUAL = (1 << 5);
760 static const uint16 CMD_DVD_IF_GPREG_SUP_EQUAL = (2 << 5);
761 static const uint16 CMD_DVD_IF_GPREG_INF = (3 << 5);
763 static const uint16 CMD_DVD_NOP = 0x0000;
764 static const uint16 CMD_DVD_GOTO_LINE = 0x0001;
765 static const uint16 CMD_DVD_BREAK = 0x0002;
767 static const uint16 CMD_DVD_NOP2 = 0x2001;
768 static const uint16 CMD_DVD_LINKPGCN = 0x2004;
769 static const uint16 CMD_DVD_LINKPGN = 0x2006;
770 static const uint16 CMD_DVD_LINKCN = 0x2007;
771 static const uint16 CMD_DVD_JUMP_TT = 0x3002;
772 static const uint16 CMD_DVD_JUMPVTS_TT = 0x3003;
773 static const uint16 CMD_DVD_JUMP_SS = 0x3006;
774 static const uint16 CMD_DVD_CALLSS_VTSM1 = 0x3008;
776 static const uint16 CMD_DVD_SET_HL_BTNN2 = 0x4600;
777 static const uint16 CMD_DVD_SET_HL_BTNN_LINKPGCN1 = 0x4604;
778 static const uint16 CMD_DVD_SET_AUDIO = 0x5100;
779 static const uint16 CMD_DVD_SET_GPRMMD = 0x5300;
780 static const uint16 CMD_DVD_SET_HL_BTNN1 = 0x5600;
781 static const uint16 CMD_DVD_SET_HL_BTNN_LINKPGCN2 = 0x5604;
782 static const uint16 CMD_DVD_SET_HL_BTNN_LINKCN = 0x5607;
784 static const uint16 CMD_DVD_MOV_SPREG_PREG = 0x6100;
785 static const uint16 CMD_DVD_GPREG_MOV_VALUE = 0x7100;
786 static const uint16 CMD_DVD_SUB_GPREG = 0x7400;
787 static const uint16 CMD_DVD_MULT_GPREG = 0x7500;
788 static const uint16 CMD_DVD_GPREG_DIV_VALUE = 0x7600;
789 static const uint16 CMD_DVD_GPREG_AND_VALUE = 0x7900;
791 // callbacks when browsing inside CodecPrivate
792 static bool MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
793 static bool MatchPgcType ( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size );
796 class dvd_chapter_codec_c : public chapter_codec_cmds_c
799 dvd_chapter_codec_c( demux_sys_t & sys )
800 :chapter_codec_cmds_c( sys, 1 )
806 std::string GetCodecName( bool f_for_title = false ) const;
809 dvd_command_interpretor_c interpretor;
812 class matroska_script_interpretor_c
815 matroska_script_interpretor_c( demux_sys_t & demuxer )
819 bool Interpret( const binary * p_command, size_t i_size );
822 static const std::string CMD_MS_GOTO_AND_PLAY;
828 const std::string matroska_script_interpretor_c::CMD_MS_GOTO_AND_PLAY = "GotoAndPlay";
831 class matroska_script_codec_c : public chapter_codec_cmds_c
834 matroska_script_codec_c( demux_sys_t & sys )
835 :chapter_codec_cmds_c( sys, 0 )
843 matroska_script_interpretor_c interpretor;
846 class chapter_translation_c
849 KaxChapterTranslateID translated;
850 unsigned int codec_id;
851 std::vector<uint64_t> editions;
860 ,i_user_start_time(-1)
863 ,b_display_seekpoint(true)
864 ,b_user_display(false)
869 virtual ~chapter_item_c()
871 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
872 while ( index != codecs.end() )
877 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
878 while ( index_ != sub_chapters.end() )
885 int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time );
886 int PublishChapters( input_title_t & title, int & i_user_chapters, int i_level = 0 );
887 chapter_item_c * FindTimecode( mtime_t i_timecode );
888 void Append( const chapter_item_c & edition );
889 chapter_item_c * FindChapter( int64_t i_find_uid );
890 virtual chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
891 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
892 const void *p_cookie,
893 size_t i_cookie_size );
894 std::string GetCodecName( bool f_for_title = false ) const;
895 bool ParentOf( const chapter_item_c & item ) const;
897 int64_t i_start_time, i_end_time;
898 int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */
899 std::vector<chapter_item_c*> sub_chapters;
902 bool b_display_seekpoint;
904 std::string psz_name;
905 chapter_item_c *psz_parent;
908 std::vector<chapter_codec_cmds_c*> codecs;
910 bool operator<( const chapter_item_c & item ) const
912 return ( i_user_start_time < item.i_user_start_time || (i_user_start_time == item.i_user_start_time && i_user_end_time < item.i_user_end_time) );
915 bool Enter( bool b_do_subchapters );
916 bool Leave( bool b_do_subchapters );
917 bool EnterAndLeave( chapter_item_c *p_item );
920 class chapter_edition_c : public chapter_item_c
927 void RefreshChapters( );
928 mtime_t Duration() const;
929 std::string GetMainName() const;
934 class matroska_segment_c
937 matroska_segment_c( demux_sys_t & demuxer, EbmlStream & estream )
940 ,i_timescale(MKVD_TIMECODESCALE)
944 ,i_chapters_position(-1)
951 ,psz_muxing_application(NULL)
952 ,psz_writing_application(NULL)
953 ,psz_segment_filename(NULL)
956 ,i_default_edition(0)
961 index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
964 virtual ~matroska_segment_c()
966 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
968 if( tracks[i_track]->fmt.psz_description )
970 free( tracks[i_track]->fmt.psz_description );
972 if( tracks[i_track]->psz_codec )
974 free( tracks[i_track]->psz_codec );
976 if( tracks[i_track]->fmt.psz_language )
978 free( tracks[i_track]->fmt.psz_language );
980 delete tracks[i_track];
983 if( psz_writing_application )
985 free( psz_writing_application );
987 if( psz_muxing_application )
989 free( psz_muxing_application );
991 if( psz_segment_filename )
993 free( psz_segment_filename );
1001 free( psz_date_utc );
1008 std::vector<chapter_edition_c*>::iterator index = stored_editions.begin();
1009 while ( index != stored_editions.end() )
1016 KaxSegment *segment;
1020 uint64_t i_timescale;
1022 /* duration of the segment */
1024 mtime_t i_start_time;
1027 std::vector<mkv_track_t*> tracks;
1030 int64_t i_cues_position;
1031 int64_t i_chapters_position;
1032 int64_t i_tags_position;
1034 KaxCluster *cluster;
1035 int64_t i_start_pos;
1036 KaxSegmentUID segment_uid;
1037 KaxPrevUID prev_segment_uid;
1038 KaxNextUID next_segment_uid;
1046 char *psz_muxing_application;
1047 char *psz_writing_application;
1048 char *psz_segment_filename;
1052 /* !!!!! GCC 3.3 bug on Darwin !!!!! */
1053 /* when you remove this variable the compiler issues an atomicity error */
1054 /* this variable only works when using std::vector<chapter_edition_c> */
1055 std::vector<chapter_edition_c*> stored_editions;
1056 int i_default_edition;
1058 std::vector<chapter_translation_c> translations;
1059 std::vector<KaxSegmentFamily> families;
1066 bool PreloadFamily( const matroska_segment_c & segment );
1067 void ParseInfo( KaxInfo *info );
1068 void ParseChapters( KaxChapters *chapters );
1069 void ParseSeekHead( KaxSeekHead *seekhead );
1070 void ParseTracks( KaxTracks *tracks );
1071 void ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters );
1072 void ParseTrackEntry( KaxTrackEntry *m );
1073 void ParseCluster( );
1074 void IndexAppendCluster( KaxCluster *cluster );
1077 void InformationCreate( );
1078 void Seek( mtime_t i_date, mtime_t i_time_offset );
1079 int BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration );
1080 bool Select( mtime_t i_start_time );
1083 static bool CompareSegmentUIDs( const matroska_segment_c * item_a, const matroska_segment_c * item_b );
1086 // class holding hard-linked segment together in the playback order
1087 class virtual_segment_c
1090 virtual_segment_c( matroska_segment_c *p_segment )
1093 ,i_current_segment(0)
1094 ,i_current_edition(-1)
1095 ,psz_current_chapter(NULL)
1097 linked_segments.push_back( p_segment );
1099 AppendUID( p_segment->segment_uid );
1100 AppendUID( p_segment->prev_segment_uid );
1101 AppendUID( p_segment->next_segment_uid );
1105 size_t AddSegment( matroska_segment_c *p_segment );
1106 void PreloadLinked( );
1107 mtime_t Duration( ) const;
1109 void Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter );
1111 inline chapter_edition_c *Edition()
1113 if ( i_current_edition >= 0 && size_t(i_current_edition) < p_editions->size() )
1114 return (*p_editions)[i_current_edition];
1118 matroska_segment_c * Segment() const
1120 if ( linked_segments.size() == 0 || i_current_segment >= linked_segments.size() )
1122 return linked_segments[i_current_segment];
1125 inline chapter_item_c *CurrentChapter() {
1126 return psz_current_chapter;
1131 if ( i_current_segment < linked_segments.size()-1 )
1133 i_current_segment++;
1139 bool FindUID( KaxSegmentUID & uid ) const
1141 for ( size_t i=0; i<linked_uids.size(); i++ )
1143 if ( linked_uids[i] == uid )
1149 bool UpdateCurrentToChapter( demux_t & demux );
1150 void PrepareChapters( );
1152 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
1153 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1154 const void *p_cookie,
1155 size_t i_cookie_size );
1156 chapter_item_c *FindChapter( int64_t i_find_uid );
1158 std::vector<chapter_edition_c*> *p_editions;
1162 std::vector<matroska_segment_c*> linked_segments;
1163 std::vector<KaxSegmentUID> linked_uids;
1164 size_t i_current_segment;
1166 int i_current_edition;
1167 chapter_item_c *psz_current_chapter;
1169 void AppendUID( const EbmlBinary & UID );
1172 class matroska_stream_c
1175 matroska_stream_c( demux_sys_t & demuxer )
1181 virtual ~matroska_stream_c()
1190 std::vector<matroska_segment_c*> segments;
1203 vlc_bool_t b_clicked;
1211 demux_sys_t( demux_t & demux )
1218 ,p_current_segment(NULL)
1226 virtual ~demux_sys_t()
1230 for ( i=0; i<streams.size(); i++ )
1232 for ( i=0; i<opened_segments.size(); i++ )
1233 delete opened_segments[i];
1234 for ( i=0; i<used_segments.size(); i++ )
1235 delete used_segments[i];
1242 mtime_t i_start_pts;
1243 mtime_t i_chapter_time;
1247 std::vector<input_title_t> titles; // matroska editions
1248 size_t i_current_title;
1250 std::vector<matroska_stream_c*> streams;
1251 std::vector<matroska_segment_c*> opened_segments;
1252 std::vector<virtual_segment_c*> used_segments;
1253 virtual_segment_c *p_current_segment;
1255 /* duration of the stream */
1258 matroska_segment_c *FindSegment( const EbmlBinary & uid ) const;
1259 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
1260 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
1261 const void *p_cookie,
1262 size_t i_cookie_size,
1263 virtual_segment_c * & p_segment_found );
1264 chapter_item_c *FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found );
1266 void PreloadFamily( const matroska_segment_c & of_segment );
1267 void PreloadLinked( matroska_segment_c *p_segment );
1268 bool PreparePlayback( virtual_segment_c *p_new_segment );
1269 matroska_stream_c *AnalyseAllSegmentsFound( EbmlStream *p_estream );
1271 void StartUiThread();
1272 void StopUiThread();
1275 /* for spu variables */
1276 input_thread_t *p_input;
1277 block_t *p_pci_packet;
1280 event_thread_t *p_ev;
1281 static int EventThread( vlc_object_t *p_this );
1282 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1283 vlc_value_t oldval, vlc_value_t newval, void *p_data );
1284 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1285 vlc_value_t oldval, vlc_value_t newval, void *p_data );
1290 virtual_segment_c *VirtualFromSegments( matroska_segment_c *p_segment ) const;
1291 bool IsUsedSegment( matroska_segment_c &p_segment ) const;
1294 static int Demux ( demux_t * );
1295 static int Control( demux_t *, int, va_list );
1296 static void Seek ( demux_t *, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter );
1298 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
1300 static char *UTF8ToStr ( const UTFstring &u );
1302 /*****************************************************************************
1303 * Open: initializes matroska demux structures
1304 *****************************************************************************/
1305 static int Open( vlc_object_t * p_this )
1307 demux_t *p_demux = (demux_t*)p_this;
1309 matroska_stream_c *p_stream;
1310 matroska_segment_c *p_segment;
1312 std::string s_path, s_filename;
1313 vlc_stream_io_callback *p_io_callback;
1314 EbmlStream *p_io_stream;
1316 /* peek the begining */
1317 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
1319 /* is a valid file */
1320 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
1321 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
1323 /* Set the demux function */
1324 p_demux->pf_demux = Demux;
1325 p_demux->pf_control = Control;
1326 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
1328 p_io_callback = new vlc_stream_io_callback( p_demux->s );
1329 p_io_stream = new EbmlStream( *p_io_callback );
1331 if( p_io_stream == NULL )
1333 msg_Err( p_demux, "failed to create EbmlStream" );
1334 delete p_io_callback;
1336 return VLC_EGENERIC;
1339 p_stream = p_sys->AnalyseAllSegmentsFound( p_io_stream );
1340 if( p_stream == NULL )
1342 msg_Err( p_demux, "cannot find KaxSegment" );
1345 p_sys->streams.push_back( p_stream );
1347 p_stream->p_in = p_io_callback;
1348 p_stream->p_es = p_io_stream;
1350 for (size_t i=0; i<p_stream->segments.size(); i++)
1352 p_stream->segments[i]->Preload();
1355 p_segment = p_stream->segments[0];
1356 if( p_segment->cluster != NULL )
1358 msg_Warn( p_demux, "cannot find any cluster, damaged file ?" );
1360 // reset the stream reading to the first cluster of the segment used
1361 p_stream->p_in->setFilePointer( p_segment->cluster->GetElementPosition() );
1364 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
1365 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
1367 // assume it's a regular file
1368 // get the directory path
1369 s_path = p_demux->psz_path;
1370 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
1372 s_path = s_path.substr(0,s_path.length()-1);
1376 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
1378 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
1382 struct dirent *p_file_item;
1383 DIR *p_src_dir = opendir(s_path.c_str());
1385 if (p_src_dir != NULL)
1387 while ((p_file_item = (dirent *) readdir(p_src_dir)))
1389 if (strlen(p_file_item->d_name) > 4)
1391 s_filename = s_path + DIRECTORY_SEPARATOR + p_file_item->d_name;
1393 if (!s_filename.compare(p_demux->psz_path))
1394 continue; // don't reuse the original opened file
1396 #if defined(__GNUC__) && (__GNUC__ < 3)
1397 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
1398 !s_filename.compare("mka", s_filename.length() - 3, 3))
1400 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
1401 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
1404 // test wether this file belongs to the our family
1405 StdIOCallback *p_file_io = new StdIOCallback(s_filename.c_str(), MODE_READ);
1406 EbmlStream *p_estream = new EbmlStream(*p_file_io);
1408 p_stream = p_sys->AnalyseAllSegmentsFound( p_estream );
1409 if ( p_stream == NULL )
1411 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
1417 p_stream->p_in = p_file_io;
1418 p_stream->p_es = p_estream;
1419 p_sys->streams.push_back( p_stream );
1424 closedir( p_src_dir );
1428 p_sys->PreloadFamily( *p_segment );
1429 p_sys->PreloadLinked( p_segment );
1430 if ( !p_sys->PreparePlayback( NULL ) )
1432 msg_Err( p_demux, "cannot use the segment" );
1440 return VLC_EGENERIC;
1443 /*****************************************************************************
1444 * Close: frees unused data
1445 *****************************************************************************/
1446 static void Close( vlc_object_t *p_this )
1448 demux_t *p_demux = (demux_t*)p_this;
1449 demux_sys_t *p_sys = p_demux->p_sys;
1454 /*****************************************************************************
1456 *****************************************************************************/
1457 static int Control( demux_t *p_demux, int i_query, va_list args )
1459 demux_sys_t *p_sys = p_demux->p_sys;
1465 vlc_meta_t **pp_meta;
1469 case DEMUX_GET_META:
1470 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1471 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1474 case DEMUX_GET_LENGTH:
1475 pi64 = (int64_t*)va_arg( args, int64_t * );
1476 if( p_sys->f_duration > 0.0 )
1478 *pi64 = (int64_t)(p_sys->f_duration * 1000);
1481 return VLC_EGENERIC;
1483 case DEMUX_GET_POSITION:
1484 pf = (double*)va_arg( args, double * );
1485 if ( p_sys->f_duration > 0.0 )
1486 *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);
1489 case DEMUX_SET_POSITION:
1490 f = (double)va_arg( args, double );
1491 Seek( p_demux, -1, f, NULL );
1494 case DEMUX_GET_TIME:
1495 pi64 = (int64_t*)va_arg( args, int64_t * );
1496 *pi64 = p_sys->i_pts;
1499 case DEMUX_GET_TITLE_INFO:
1500 if( p_sys->titles.size() )
1502 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1503 int *pi_int = (int*)va_arg( args, int* );
1505 *pi_int = p_sys->titles.size();
1506 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->titles.size() );
1508 for( size_t i = 0; i < p_sys->titles.size(); i++ )
1510 (*ppp_title)[i] = vlc_input_title_Duplicate( &p_sys->titles[i] );
1515 return VLC_EGENERIC;
1517 case DEMUX_SET_TITLE:
1518 /* TODO handle editions as titles */
1519 i_idx = (int)va_arg( args, int );
1520 if( i_idx < p_sys->used_segments.size() )
1522 p_sys->PreparePlayback( p_sys->used_segments[i_idx] );
1525 return VLC_EGENERIC;
1527 case DEMUX_SET_SEEKPOINT:
1528 i_skp = (int)va_arg( args, int );
1530 // TODO change the way it works with the << & >> buttons on the UI (+1/-1 instead of a number)
1531 if( p_sys->titles.size() && i_skp < p_sys->titles[p_sys->i_current_title].i_seekpoint)
1533 Seek( p_demux, (int64_t)p_sys->titles[p_sys->i_current_title].seekpoint[i_skp]->i_time_offset, -1, NULL);
1534 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
1535 p_demux->info.i_seekpoint = i_skp;
1538 return VLC_EGENERIC;
1540 case DEMUX_SET_TIME:
1543 return VLC_EGENERIC;
1547 int matroska_segment_c::BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1559 return VLC_EGENERIC;
1562 i_level = ep->GetLevel();
1564 if( el == NULL && *pp_block != NULL )
1566 /* update the index */
1567 #define idx index[i_index - 1]
1568 if( i_index > 0 && idx.i_time == -1 )
1570 idx.i_time = (*pp_block)->GlobalTimecode() / (mtime_t)1000;
1571 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1579 if( ep->GetLevel() > 1 )
1584 msg_Warn( &sys.demuxer, "EOF" );
1585 return VLC_EGENERIC;
1591 if( MKV_IS_ID( el, KaxCluster ) )
1593 cluster = (KaxCluster*)el;
1595 /* add it to the index */
1597 ( i_index > 0 && index[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1599 IndexAppendCluster( cluster );
1602 // reset silent tracks
1603 for (size_t i=0; i<tracks.size(); i++)
1605 tracks[i]->b_silent = VLC_FALSE;
1610 else if( MKV_IS_ID( el, KaxCues ) )
1612 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1613 return VLC_EGENERIC;
1617 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1620 else if( i_level == 2 )
1622 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1624 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1626 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1627 cluster->InitTimecode( uint64( ctc ), i_timescale );
1629 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1633 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1638 else if( i_level == 3 )
1640 if( MKV_IS_ID( el, KaxBlock ) )
1642 *pp_block = (KaxBlock*)el;
1644 (*pp_block)->ReadData( es.I_O() );
1645 (*pp_block)->SetParent( *cluster );
1649 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1651 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1653 dur.ReadData( es.I_O() );
1654 *pi_duration = uint64( dur );
1656 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1658 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1660 ref.ReadData( es.I_O() );
1661 if( *pi_ref1 == -1 )
1663 *pi_ref1 = int64( ref );
1667 *pi_ref2 = int64( ref );
1670 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1672 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1673 track_num.ReadData( es.I_O() );
1675 for (size_t i=0; i<tracks.size(); i++)
1677 if ( tracks[i]->i_number == uint32(track_num))
1679 tracks[i]->b_silent = VLC_TRUE;
1687 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1688 return VLC_EGENERIC;
1693 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1696 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1697 memcpy( p_block->p_buffer, p_mem, i_mem );
1698 //p_block->i_rate = p_input->stream.control.i_rate;
1702 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1703 mtime_t i_duration )
1705 demux_sys_t *p_sys = p_demux->p_sys;
1706 matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
1712 #define tk p_segment->tracks[i_track]
1713 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1715 if( tk->i_number == block->TrackNum() )
1721 if( i_track >= p_segment->tracks.size() )
1723 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1726 if( tk->p_es == NULL )
1728 msg_Err( p_demux, "unknown track number=%d", block->TrackNum() );
1731 if( i_pts < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1733 return; /* discard audio packets that shouldn't be rendered */
1736 if ( tk->fmt.i_cat != SPU_ES || strcmp( tk->psz_codec, "B_VOBBTN" ) )
1738 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1742 tk->b_inited = VLC_FALSE;
1748 /* First send init data */
1749 if( !tk->b_inited && tk->i_data_init > 0 )
1753 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
1754 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init );
1755 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
1757 tk->b_inited = VLC_TRUE;
1760 for( i = 0; i < block->NumberFrames(); i++ )
1763 DataBuffer &data = block->GetBuffer(i);
1765 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1767 if( p_block == NULL )
1772 #if defined(HAVE_ZLIB_H)
1773 if( tk->i_compression_type )
1775 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
1779 // TODO implement correct timestamping when B frames are used
1780 if( tk->fmt.i_cat != VIDEO_ES )
1782 p_block->i_dts = p_block->i_pts = i_pts;
1786 p_block->i_dts = i_pts;
1790 if ( tk->fmt.i_cat == SPU_ES )
1792 if ( !strcmp( tk->psz_codec, "B_VOBBTN" ) )
1794 // TODO handle the start/stop times of this packet
1795 if ( p_sys->b_ui_hooked )
1797 vlc_mutex_lock( &p_sys->p_ev->lock );
1798 if ( p_sys->p_pci_packet != NULL )
1799 block_Release( p_sys->p_pci_packet );
1800 p_sys->p_pci_packet = p_block;
1801 vlc_mutex_unlock( &p_sys->p_ev->lock );
1806 else if ( strcmp( tk->psz_codec, "S_VOBSUB" ) )
1808 p_block->i_length = i_duration * 1000;
1812 es_out_Send( p_demux->out, tk->p_es, p_block );
1814 /* use time stamp only for first block */
1821 matroska_stream_c *demux_sys_t::AnalyseAllSegmentsFound( EbmlStream *p_estream )
1823 int i_upper_lvl = 0;
1825 EbmlElement *p_l0, *p_l1, *p_l2;
1826 bool b_keep_stream = false, b_keep_segment;
1828 // verify the EBML Header
1829 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
1834 p_l0->SkipData(*p_estream, EbmlHead_Context);
1837 // find all segments in this file
1838 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1844 matroska_stream_c *p_stream1 = new matroska_stream_c( *this );
1848 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
1851 matroska_segment_c *p_segment1 = new matroska_segment_c( *this, *p_estream );
1852 b_keep_segment = false;
1854 ep = new EbmlParser(p_estream, p_l0, &demuxer );
1855 p_segment1->ep = ep;
1856 p_segment1->segment = (KaxSegment*)p_l0;
1858 while ((p_l1 = ep->Get()))
1860 if (MKV_IS_ID(p_l1, KaxInfo))
1862 // find the families of this segment
1863 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
1865 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
1866 for( i = 0; i < p_info->ListSize(); i++ )
1868 EbmlElement *l = (*p_info)[i];
1870 if( MKV_IS_ID( l, KaxSegmentUID ) )
1872 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
1873 b_keep_segment = (FindSegment( *p_uid ) == NULL);
1874 if ( !b_keep_segment )
1875 break; // this segment is already known
1876 opened_segments.push_back( p_segment1 );
1877 p_segment1->segment_uid = *( new KaxSegmentUID(*p_uid) );
1879 else if( MKV_IS_ID( l, KaxPrevUID ) )
1881 p_segment1->prev_segment_uid = *( new KaxPrevUID( *static_cast<KaxPrevUID*>(l) ) );
1883 else if( MKV_IS_ID( l, KaxNextUID ) )
1885 p_segment1->next_segment_uid = *( new KaxNextUID( *static_cast<KaxNextUID*>(l) ) );
1887 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
1889 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
1890 p_segment1->families.push_back( *p_fam );
1896 if ( b_keep_segment )
1898 b_keep_stream = true;
1899 p_stream1->segments.push_back( p_segment1 );
1905 p_l0->SkipData(*p_estream, EbmlHead_Context);
1906 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1909 if ( !b_keep_stream )
1918 bool matroska_segment_c::Select( mtime_t i_start_time )
1923 msg_Dbg( &sys.demuxer, "found %d es", tracks.size() );
1925 for( i_track = 0; i_track < tracks.size(); i_track++ )
1927 if( tracks[i_track]->fmt.i_cat == UNKNOWN_ES )
1929 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", i_track, tracks[i_track]->i_number );
1930 tracks[i_track]->p_es = NULL;
1934 if( !strcmp( tracks[i_track]->psz_codec, "V_MS/VFW/FOURCC" ) )
1936 if( tracks[i_track]->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
1938 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
1939 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1943 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tracks[i_track]->p_extra_data;
1945 tracks[i_track]->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1946 tracks[i_track]->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1947 tracks[i_track]->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1949 tracks[i_track]->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
1950 if( tracks[i_track]->fmt.i_extra > 0 )
1952 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
1953 memcpy( tracks[i_track]->fmt.p_extra, &p_bih[1], tracks[i_track]->fmt.i_extra );
1957 else if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG1" ) ||
1958 !strcmp( tracks[i_track]->psz_codec, "V_MPEG2" ) )
1960 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1962 else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4", 7 ) )
1964 if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/MS/V3" ) )
1966 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1968 else if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO/AVC" ) )
1970 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1971 tracks[i_track]->fmt.b_packetized = VLC_FALSE;
1972 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
1973 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
1974 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
1978 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1981 else if( !strcmp( tracks[i_track]->psz_codec, "V_QUICKTIME" ) )
1983 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
1985 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1986 tracks[i_track]->p_extra_data,
1987 tracks[i_track]->i_extra_data );
1989 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1990 tracks[i_track]->p_extra_data,
1991 tracks[i_track]->i_extra_data,
1994 MP4_ReadBoxCommon( p_mp4_stream, p_box );
1995 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
1996 tracks[i_track]->fmt.i_codec = p_box->i_type;
1997 tracks[i_track]->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1998 tracks[i_track]->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1999 tracks[i_track]->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
2000 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2001 memcpy( tracks[i_track]->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tracks[i_track]->fmt.i_extra );
2002 MP4_FreeBox_sample_vide( p_box );
2004 stream_MemoryDelete( p_mp4_stream, VLC_TRUE );
2006 stream_Delete( p_mp4_stream );
2009 else if( !strcmp( tracks[i_track]->psz_codec, "A_MS/ACM" ) )
2011 if( tracks[i_track]->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
2013 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
2014 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2018 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tracks[i_track]->p_extra_data;
2020 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tracks[i_track]->fmt.i_codec, NULL );
2022 tracks[i_track]->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
2023 tracks[i_track]->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
2024 tracks[i_track]->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
2025 tracks[i_track]->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
2026 tracks[i_track]->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
2028 tracks[i_track]->fmt.i_extra = GetWLE( &p_wf->cbSize );
2029 if( tracks[i_track]->fmt.i_extra > 0 )
2031 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2032 memcpy( tracks[i_track]->fmt.p_extra, &p_wf[1], tracks[i_track]->fmt.i_extra );
2036 else if( !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L3" ) ||
2037 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L2" ) ||
2038 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L1" ) )
2040 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
2042 else if( !strcmp( tracks[i_track]->psz_codec, "A_AC3" ) )
2044 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2046 else if( !strcmp( tracks[i_track]->psz_codec, "A_DTS" ) )
2048 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2050 else if( !strcmp( tracks[i_track]->psz_codec, "A_FLAC" ) )
2052 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
2053 tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
2054 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
2055 memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
2057 else if( !strcmp( tracks[i_track]->psz_codec, "A_VORBIS" ) )
2059 int i, i_offset = 1, i_size[3], i_extra;
2062 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
2064 /* Split the 3 headers */
2065 if( tracks[i_track]->p_extra_data[0] != 0x02 )
2066 msg_Err( &sys.demuxer, "invalid vorbis header" );
2068 for( i = 0; i < 2; i++ )
2071 while( i_offset < tracks[i_track]->i_extra_data )
2073 i_size[i] += tracks[i_track]->p_extra_data[i_offset];
2074 if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
2078 i_size[0] = __MIN(i_size[0], tracks[i_track]->i_extra_data - i_offset);
2079 i_size[1] = __MIN(i_size[1], tracks[i_track]->i_extra_data -i_offset -i_size[0]);
2080 i_size[2] = tracks[i_track]->i_extra_data - i_offset - i_size[0] - i_size[1];
2082 tracks[i_track]->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
2083 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2084 p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra; i_extra = 0;
2085 for( i = 0; i < 3; i++ )
2087 *(p_extra++) = i_size[i] >> 8;
2088 *(p_extra++) = i_size[i] & 0xFF;
2089 memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra,
2091 p_extra += i_size[i];
2092 i_extra += i_size[i];
2095 else if( !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
2096 !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
2098 int i_profile, i_srate;
2099 static unsigned int i_sample_rates[] =
2101 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
2102 16000, 12000, 11025, 8000, 7350, 0, 0, 0
2105 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
2106 /* create data for faad (MP4DecSpecificDescrTag)*/
2108 if( !strcmp( &tracks[i_track]->psz_codec[12], "MAIN" ) )
2112 else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC" ) )
2116 else if( !strcmp( &tracks[i_track]->psz_codec[12], "SSR" ) )
2125 for( i_srate = 0; i_srate < 13; i_srate++ )
2127 if( i_sample_rates[i_srate] == tracks[i_track]->fmt.audio.i_rate )
2132 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
2134 tracks[i_track]->fmt.i_extra = 2;
2135 tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
2136 ((uint8_t*)tracks[i_track]->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
2137 ((uint8_t*)tracks[i_track]->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tracks[i_track]->fmt.audio.i_channels << 3);
2139 else if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) ||
2140 !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/LIT" ) ||
2141 !strcmp( tracks[i_track]->psz_codec, "A_PCM/FLOAT/IEEE" ) )
2143 if( !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) )
2145 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
2149 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
2151 tracks[i_track]->fmt.audio.i_blockalign = ( tracks[i_track]->fmt.audio.i_bitspersample + 7 ) / 8 * tracks[i_track]->fmt.audio.i_channels;
2153 else if( !strcmp( tracks[i_track]->psz_codec, "A_TTA1" ) )
2155 /* FIXME: support this codec */
2156 msg_Err( &sys.demuxer, "TTA not supported yet[%d, n=%d]", i_track, tracks[i_track]->i_number );
2157 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2159 else if( !strcmp( tracks[i_track]->psz_codec, "A_WAVPACK4" ) )
2161 /* FIXME: support this codec */
2162 msg_Err( &sys.demuxer, "Wavpack not supported yet[%d, n=%d]", i_track, tracks[i_track]->i_number );
2163 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2165 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/UTF8" ) )
2167 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
2168 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2170 else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/SSA" ) ||
2171 !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASS" ) ||
2172 !strcmp( tracks[i_track]->psz_codec, "S_SSA" ) ||
2173 !strcmp( tracks[i_track]->psz_codec, "S_ASS" ))
2175 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
2176 tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
2178 else if( !strcmp( tracks[i_track]->psz_codec, "S_VOBSUB" ) )
2180 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
2181 if( tracks[i_track]->i_extra_data )
2184 char *p_buf = (char *)malloc( tracks[i_track]->i_extra_data + 1);
2185 memcpy( p_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
2186 p_buf[tracks[i_track]->i_extra_data] = '\0';
2188 p_start = strstr( p_buf, "size:" );
2189 if( sscanf( p_start, "size: %dx%d",
2190 &tracks[i_track]->fmt.subs.spu.i_original_frame_width, &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) == 2 )
2192 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 );
2196 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
2201 else if( !strcmp( tracks[i_track]->psz_codec, "B_VOBBTN" ) )
2203 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
2204 sys.StartUiThread();
2208 msg_Err( &sys.demuxer, "unknow codec id=`%s'", tracks[i_track]->psz_codec );
2209 tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
2211 if( tracks[i_track]->b_default )
2213 tracks[i_track]->fmt.i_priority = 1000;
2216 tracks[i_track]->p_es = es_out_Add( sys.demuxer.out, &tracks[i_track]->fmt );
2218 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, i_start_time );
2221 sys.i_start_pts = i_start_time;
2222 // reset the stream reading to the first cluster of the segment used
2223 es.I_O().setFilePointer( i_start_pos );
2226 ep = new EbmlParser( &es, segment, &sys.demuxer );
2231 void demux_sys_t::StartUiThread()
2235 msg_Dbg( &demuxer, "Starting the UI Hook" );
2237 /* FIXME hack hack hack hack FIXME */
2238 /* Get p_input and create variable */
2239 p_input = (input_thread_t *) vlc_object_find( &demuxer, VLC_OBJECT_INPUT, FIND_PARENT );
2240 var_Create( p_input, "x-start", VLC_VAR_INTEGER );
2241 var_Create( p_input, "y-start", VLC_VAR_INTEGER );
2242 var_Create( p_input, "x-end", VLC_VAR_INTEGER );
2243 var_Create( p_input, "y-end", VLC_VAR_INTEGER );
2244 var_Create( p_input, "color", VLC_VAR_ADDRESS );
2245 var_Create( p_input, "menu-contrast", VLC_VAR_ADDRESS );
2246 var_Create( p_input, "highlight", VLC_VAR_BOOL );
2247 var_Create( p_input, "highlight-mutex", VLC_VAR_MUTEX );
2249 /* Now create our event thread catcher */
2250 p_ev = (event_thread_t *) vlc_object_create( &demuxer, sizeof( event_thread_t ) );
2251 p_ev->p_demux = &demuxer;
2252 vlc_mutex_init( p_ev, &p_ev->lock );
2253 vlc_thread_create( p_ev, "mkv event thread handler", EventThread,
2254 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
2258 void demux_sys_t::StopUiThread()
2262 p_ev->b_die = VLC_TRUE;
2264 var_Destroy( p_input, "highlight-mutex" );
2265 var_Destroy( p_input, "highlight" );
2266 var_Destroy( p_input, "x-start" );
2267 var_Destroy( p_input, "x-end" );
2268 var_Destroy( p_input, "y-start" );
2269 var_Destroy( p_input, "y-end" );
2270 var_Destroy( p_input, "color" );
2271 var_Destroy( p_input, "menu-contrast" );
2273 vlc_thread_join( p_ev );
2274 vlc_object_destroy( p_ev );
2276 msg_Dbg( &demuxer, "Stopping the UI Hook" );
2278 b_ui_hooked = false;
2281 int demux_sys_t::EventMouse( vlc_object_t *p_this, char const *psz_var,
2282 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2284 event_thread_t *p_ev = (event_thread_t *) p_data;
2285 vlc_mutex_lock( &p_ev->lock );
2286 if( psz_var[6] == 'c' )
2287 p_ev->b_clicked = VLC_TRUE;
2288 else if( psz_var[6] == 'm' )
2289 p_ev->b_moved = VLC_TRUE;
2290 vlc_mutex_unlock( &p_ev->lock );
2295 int demux_sys_t::EventKey( vlc_object_t *p_this, char const *psz_var,
2296 vlc_value_t oldval, vlc_value_t newval, void *p_data )
2298 event_thread_t *p_ev = (event_thread_t *) p_data;
2299 vlc_mutex_lock( &p_ev->lock );
2300 p_ev->b_key = VLC_TRUE;
2301 vlc_mutex_unlock( &p_ev->lock );
2306 int demux_sys_t::EventThread( vlc_object_t *p_this )
2308 event_thread_t *p_ev = (event_thread_t*)p_this;
2309 demux_sys_t *p_sys = p_ev->p_demux->p_sys;
2310 vlc_object_t *p_vout = NULL;
2312 p_ev->b_moved = VLC_FALSE;
2313 p_ev->b_clicked = VLC_FALSE;
2314 p_ev->b_key = VLC_FALSE;
2316 /* catch all key event */
2317 var_AddCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
2320 while( !p_ev->b_die )
2322 vlc_bool_t b_activated = VLC_FALSE;
2328 struct vlc_t::hotkey *p_hotkeys = p_ev->p_vlc->p_hotkeys;
2329 int i, i_action = -1;
2331 vlc_mutex_lock( &p_ev->lock );
2333 pci_t *pci = (pci_t *) &p_sys->p_pci_packet->p_buffer[1];
2335 var_Get( p_ev->p_vlc, "key-pressed", &valk );
2336 for( i = 0; p_hotkeys[i].psz_action != NULL; i++ )
2338 if( p_hotkeys[i].i_key == valk.i_int )
2340 i_action = p_hotkeys[i].i_action;
2346 case ACTIONID_NAV_LEFT:
2347 // dvdnav_left_button_select( NULL, pci );
2349 case ACTIONID_NAV_RIGHT:
2350 // dvdnav_right_button_select( NULL, pci );
2352 case ACTIONID_NAV_UP:
2353 // dvdnav_upper_button_select( NULL, pci );
2355 case ACTIONID_NAV_DOWN:
2356 // dvdnav_lower_button_select( NULL, pci );
2358 case ACTIONID_NAV_ACTIVATE:
2359 b_activated = VLC_TRUE;
2360 // dvdnav_button_activate( NULL, pci );
2365 p_ev->b_key = VLC_FALSE;
2366 vlc_mutex_unlock( &p_ev->lock );
2370 if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
2372 vlc_value_t valx, valy;
2374 vlc_mutex_lock( &p_ev->lock );
2375 pci_t *pci = (pci_t *) &p_sys->p_pci_packet->p_buffer[1];
2376 var_Get( p_vout, "mouse-x", &valx );
2377 var_Get( p_vout, "mouse-y", &valy );
2381 // dvdnav_mouse_select( NULL, pci, valx.i_int, valy.i_int );
2384 if( p_ev->b_clicked )
2386 b_activated = VLC_TRUE;
2387 // dvdnav_mouse_activate( NULL, pci, valx.i_int, valy.i_int );
2390 p_ev->b_moved = VLC_FALSE;
2391 p_ev->b_clicked = VLC_FALSE;
2392 vlc_mutex_unlock( &p_ev->lock );
2394 if( p_vout && p_vout->b_die )
2396 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
2397 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
2398 vlc_object_release( p_vout );
2401 if( p_vout == NULL )
2403 p_vout = (vlc_object_t*) vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
2407 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
2408 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
2416 /* Release callback */
2419 var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
2420 var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
2421 vlc_object_release( p_vout );
2423 var_DelCallback( p_ev->p_vlc, "key-pressed", EventKey, p_ev );
2425 vlc_mutex_destroy( &p_ev->lock );
2430 void matroska_segment_c::UnSelect( )
2436 for( i_track = 0; i_track < tracks.size(); i_track++ )
2438 if ( tracks[i_track]->p_es != NULL )
2440 es_out_Del( sys.demuxer.out, tracks[i_track]->p_es );
2441 tracks[i_track]->p_es = NULL;
2448 void virtual_segment_c::PrepareChapters( )
2450 if ( linked_segments.size() == 0 )
2453 // !!! should be called only once !!!
2454 matroska_segment_c *p_segment;
2457 // copy editions from the first segment
2458 p_segment = linked_segments[0];
2459 p_editions = &p_segment->stored_editions;
2461 for ( i=1 ; i<linked_segments.size(); i++ )
2463 p_segment = linked_segments[i];
2464 // FIXME assume we have the same editions in all segments
2465 for (j=0; j<p_segment->stored_editions.size(); j++)
2466 (*p_editions)[j]->Append( *p_segment->stored_editions[j] );
2470 std::string chapter_edition_c::GetMainName() const
2472 if ( sub_chapters.size() )
2474 return sub_chapters[0]->GetCodecName( true );
2479 int chapter_item_c::PublishChapters( input_title_t & title, int & i_user_chapters, int i_level )
2481 // add support for meta-elements from codec like DVD Titles
2482 if ( !b_display_seekpoint || psz_name == "" )
2484 psz_name = GetCodecName();
2485 if ( psz_name != "" )
2486 b_display_seekpoint = true;
2489 if (b_display_seekpoint)
2491 seekpoint_t *sk = vlc_seekpoint_New();
2493 sk->i_level = i_level;
2494 sk->i_time_offset = i_start_time;
2495 sk->psz_name = strdup( psz_name.c_str() );
2497 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
2498 title.i_seekpoint++;
2499 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
2500 title.seekpoint[title.i_seekpoint-1] = sk;
2502 if ( b_user_display )
2506 for ( size_t i=0; i<sub_chapters.size() ; i++)
2508 sub_chapters[i]->PublishChapters( title, i_user_chapters, i_level+1 );
2511 i_seekpoint_num = i_user_chapters;
2513 return i_user_chapters;
2516 bool virtual_segment_c::UpdateCurrentToChapter( demux_t & demux )
2518 demux_sys_t & sys = *demux.p_sys;
2519 chapter_item_c *psz_curr_chapter;
2521 /* update current chapter/seekpoint */
2522 if ( p_editions->size() )
2524 /* 1st, we need to know in which chapter we are */
2525 psz_curr_chapter = (*p_editions)[i_current_edition]->FindTimecode( sys.i_pts );
2527 /* we have moved to a new chapter */
2528 if (psz_curr_chapter != NULL && psz_current_chapter != psz_curr_chapter)
2530 if ( (*p_editions)[i_current_edition]->b_ordered )
2532 // Leave/Enter up to the link point
2533 if ( !psz_curr_chapter->EnterAndLeave( psz_current_chapter ) )
2535 // only seek if necessary
2536 if ( psz_current_chapter == NULL || (psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time) )
2537 Seek( demux, sys.i_pts, 0, psz_curr_chapter );
2538 psz_current_chapter = psz_curr_chapter;
2541 else if ( psz_curr_chapter->i_seekpoint_num > 0 )
2543 demux.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
2544 demux.info.i_title = sys.i_current_title = i_sys_title;
2545 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
2546 psz_current_chapter = psz_curr_chapter;
2550 psz_current_chapter = psz_curr_chapter;
2559 chapter_item_c *virtual_segment_c::BrowseCodecPrivate( unsigned int codec_id,
2560 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
2561 const void *p_cookie,
2562 size_t i_cookie_size )
2564 // FIXME don't assume it is the first edition
2565 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
2566 if ( index != p_editions->end() )
2568 chapter_item_c *p_result = (*index)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
2569 if ( p_result != NULL )
2575 chapter_item_c *virtual_segment_c::FindChapter( int64_t i_find_uid )
2577 // FIXME don't assume it is the first edition
2578 std::vector<chapter_edition_c*>::iterator index = p_editions->begin();
2579 if ( index != p_editions->end() )
2581 chapter_item_c *p_result = (*index)->FindChapter( i_find_uid );
2582 if ( p_result != NULL )
2588 chapter_item_c *chapter_item_c::BrowseCodecPrivate( unsigned int codec_id,
2589 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
2590 const void *p_cookie,
2591 size_t i_cookie_size )
2594 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
2595 while ( index != codecs.end() )
2597 if ( match( **index ,p_cookie, i_cookie_size ) )
2603 chapter_item_c *p_result = NULL;
2604 std::vector<chapter_item_c*>::const_iterator index2 = sub_chapters.begin();
2605 while ( index2 != sub_chapters.end() )
2607 p_result = (*index2)->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
2608 if ( p_result != NULL )
2616 void chapter_item_c::Append( const chapter_item_c & chapter )
2618 // we are appending content for the same chapter UID
2620 chapter_item_c *p_chapter;
2622 for ( i=0; i<chapter.sub_chapters.size(); i++ )
2624 p_chapter = FindChapter( chapter.sub_chapters[i]->i_uid );
2625 if ( p_chapter != NULL )
2627 p_chapter->Append( *chapter.sub_chapters[i] );
2631 sub_chapters.push_back( chapter.sub_chapters[i] );
2635 i_user_start_time = min( i_user_start_time, chapter.i_user_start_time );
2636 i_user_end_time = max( i_user_end_time, chapter.i_user_end_time );
2639 chapter_item_c * chapter_item_c::FindChapter( int64_t i_find_uid )
2642 chapter_item_c *p_result = NULL;
2644 if ( i_uid == i_find_uid )
2647 for ( i=0; i<sub_chapters.size(); i++)
2649 p_result = sub_chapters[i]->FindChapter( i_find_uid );
2650 if ( p_result != NULL )
2656 std::string chapter_item_c::GetCodecName( bool f_for_title ) const
2660 std::vector<chapter_codec_cmds_c*>::const_iterator index = codecs.begin();
2661 while ( index != codecs.end() )
2663 result = (*index)->GetCodecName( f_for_title );
2672 std::string dvd_chapter_codec_c::GetCodecName( bool f_for_title ) const
2675 if ( m_private_data.GetSize() >= 3)
2677 const binary* p_data = m_private_data.GetBuffer();
2678 /* if ( p_data[0] == MATROSKA_DVD_LEVEL_TT )
2680 uint16_t i_title = (p_data[1] << 8) + p_data[2];
2682 sprintf( psz_str, " %d ---", i_title );
2683 result = N_("--- DVD Title");
2686 else */ if ( p_data[0] == MATROSKA_DVD_LEVEL_LU )
2689 sprintf( psz_str, " (%c%c) ---", p_data[1], p_data[2] );
2690 result = N_("--- DVD Menu");
2693 else if ( p_data[0] == MATROSKA_DVD_LEVEL_SS && f_for_title )
2695 if ( p_data[1] == 0x00 )
2696 result = N_("First Played");
2697 else if ( p_data[1] == 0xC0 )
2698 result = N_("Video Manager");
2699 else if ( p_data[1] == 0x80 )
2701 uint16_t i_title = (p_data[2] << 8) + p_data[3];
2703 sprintf( psz_str, " %d -----", i_title );
2704 result = N_("----- Title");
2713 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter )
2715 demux_sys_t *p_sys = p_demux->p_sys;
2716 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
2717 matroska_segment_c *p_segment = p_vsegment->Segment();
2718 mtime_t i_time_offset = 0;
2722 msg_Dbg( p_demux, "seek request to "I64Fd" (%f%%)", i_date, f_percent );
2723 if( i_date < 0 && f_percent < 0 )
2725 msg_Warn( p_demux, "cannot seek nowhere !" );
2728 if( f_percent > 1.0 )
2730 msg_Warn( p_demux, "cannot seek so far !" );
2734 /* seek without index or without date */
2735 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
2737 if (p_sys->f_duration >= 0)
2739 i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
2743 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
2745 msg_Dbg( p_demux, "inacurate way of seeking" );
2746 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
2748 if( p_segment->index[i_index].i_position >= i_pos)
2753 if( i_index == p_segment->i_index )
2758 i_date = p_segment->index[i_index].i_time;
2761 if( p_segment->index[i_index].i_position < i_pos )
2765 msg_Warn( p_demux, "searching for cluster, could take some time" );
2767 /* search a cluster */
2768 while( ( el = p_sys->ep->Get() ) != NULL )
2770 if( MKV_IS_ID( el, KaxCluster ) )
2772 KaxCluster *cluster = (KaxCluster*)el;
2774 /* add it to the index */
2775 p_segment->IndexAppendCluster( cluster );
2777 if( (int64_t)cluster->GetElementPosition() >= i_pos )
2779 p_sys->cluster = cluster;
2790 p_vsegment->Seek( *p_demux, i_date, i_time_offset, psz_chapter );
2793 /*****************************************************************************
2794 * Demux: reads and demuxes data packets
2795 *****************************************************************************
2796 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
2797 *****************************************************************************/
2798 static int Demux( demux_t *p_demux)
2800 demux_sys_t *p_sys = p_demux->p_sys;
2801 virtual_segment_c *p_vsegment = p_sys->p_current_segment;
2802 matroska_segment_c *p_segmet = p_vsegment->Segment();
2803 if ( p_segmet == NULL ) return 0;
2804 int i_block_count = 0;
2807 int64_t i_block_duration;
2808 int64_t i_block_ref1;
2809 int64_t i_block_ref2;
2813 if ( p_sys->demuxer.b_die )
2816 if( p_sys->i_pts >= p_sys->i_start_pts )
2817 if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
2820 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
2822 /* nothing left to read in this ordered edition */
2823 if ( !p_vsegment->SelectNext() )
2825 p_segmet->UnSelect( );
2827 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
2829 /* switch to the next segment */
2830 p_segmet = p_vsegment->Segment();
2831 if ( !p_segmet->Select( 0 ) )
2833 msg_Err( p_demux, "Failed to select new segment" );
2840 if( p_segmet->BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
2842 if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered )
2844 const chapter_item_c *p_chap = p_vsegment->CurrentChapter();
2845 // check if there are more chapters to read
2846 if ( p_chap != NULL )
2848 /* TODO handle successive chapters with the same user_start_time/user_end_time
2849 if ( p_chap->i_user_start_time == p_chap->i_user_start_time )
2850 p_vsegment->SelectNext();
2852 p_sys->i_pts = p_chap->i_user_end_time;
2853 p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
2860 msg_Warn( p_demux, "cannot get block EOF?" );
2861 p_segmet->UnSelect( );
2863 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
2865 /* switch to the next segment */
2866 if ( !p_vsegment->SelectNext() )
2867 // no more segments in this stream
2869 p_segmet = p_vsegment->Segment();
2870 if ( !p_segmet->Select( 0 ) )
2872 msg_Err( p_demux, "Failed to select new segment" );
2879 p_sys->i_pts = p_sys->i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
2881 if( p_sys->i_pts >= p_sys->i_start_pts )
2883 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
2886 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
2891 // TODO optimize when there is need to leave or when seeking has been called
2892 if( i_block_count > 5 )
2901 /*****************************************************************************
2903 *****************************************************************************/
2904 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
2910 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
2912 if( i_size <= 0 || mb_eof )
2917 return stream_Read( s, p_buffer, i_size );
2919 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
2925 case seek_beginning:
2929 i_pos = stream_Size( s ) - i_offset;
2932 i_pos= stream_Tell( s ) + i_offset;
2936 if( i_pos < 0 || i_pos >= stream_Size( s ) )
2943 if( stream_Seek( s, i_pos ) )
2949 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
2953 uint64 vlc_stream_io_callback::getFilePointer( void )
2955 return stream_Tell( s );
2957 void vlc_stream_io_callback::close( void )
2963 /*****************************************************************************
2964 * Ebml Stream parser
2965 *****************************************************************************/
2966 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start, demux_t *p_demux )
2973 mi_remain_size[0] = el_start->GetSize();
2975 for( i = 1; i < 6; i++ )
2981 mb_keep = VLC_FALSE;
2982 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
2985 EbmlParser::~EbmlParser( void )
2989 for( i = 1; i < mi_level; i++ )
2995 mb_keep = VLC_FALSE;
2999 void EbmlParser::Up( void )
3001 if( mi_user_level == mi_level )
3003 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
3009 void EbmlParser::Down( void )
3015 void EbmlParser::Keep( void )
3020 int EbmlParser::GetLevel( void )
3022 return mi_user_level;
3025 void EbmlParser::Reset( demux_t *p_demux )
3027 while ( mi_level > 0)
3029 delete m_el[mi_level];
3030 m_el[mi_level] = NULL;
3033 mi_user_level = mi_level = 1;
3034 #if LIBEBML_VERSION >= 0x000704
3035 // a little faster and cleaner
3036 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
3038 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
3040 mb_dummy = config_GetInt( p_demux, "mkv-use-dummy" );
3043 EbmlElement *EbmlParser::Get( void )
3047 if( mi_user_level != mi_level )
3053 EbmlElement *ret = m_got;
3059 if( m_el[mi_level] )
3061 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
3064 delete m_el[mi_level];
3066 mb_keep = VLC_FALSE;
3069 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, mb_dummy, 1 );
3070 // mi_remain_size[mi_level] = m_el[mi_level]->GetSize();
3081 delete m_el[mi_level - 1];
3082 m_got = m_el[mi_level -1] = m_el[mi_level];
3083 m_el[mi_level] = NULL;
3090 else if( m_el[mi_level] == NULL )
3092 fprintf( stderr," m_el[mi_level] == NULL\n" );
3095 return m_el[mi_level];
3099 /*****************************************************************************
3101 * * LoadCues : load the cues element and update index
3103 * * LoadTags : load ... the tags element
3105 * * InformationCreate : create all information, load tags if present
3107 *****************************************************************************/
3108 void matroska_segment_c::LoadCues( )
3110 int64_t i_sav_position = es.I_O().getFilePointer();
3112 EbmlElement *el, *cues;
3114 /* *** Load the cue if found *** */
3115 if( i_cues_position < 0 )
3117 msg_Warn( &sys.demuxer, "no cues/empty cues found->seek won't be precise" );
3119 // IndexAppendCluster( cluster );
3122 vlc_bool_t b_seekable;
3124 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
3128 msg_Dbg( &sys.demuxer, "loading cues" );
3129 es.I_O().setFilePointer( i_cues_position, seek_beginning );
3130 cues = es.FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
3134 msg_Err( &sys.demuxer, "cannot load cues (broken seekhead or file)" );
3135 es.I_O().setFilePointer( i_sav_position, seek_beginning );
3139 ep = new EbmlParser( &es, cues, &sys.demuxer );
3140 while( ( el = ep->Get() ) != NULL )
3142 if( MKV_IS_ID( el, KaxCuePoint ) )
3144 #define idx index[i_index]
3147 idx.i_block_number= -1;
3148 idx.i_position = -1;
3150 idx.b_key = VLC_TRUE;
3153 while( ( el = ep->Get() ) != NULL )
3155 if( MKV_IS_ID( el, KaxCueTime ) )
3157 KaxCueTime &ctime = *(KaxCueTime*)el;
3159 ctime.ReadData( es.I_O() );
3161 idx.i_time = uint64( ctime ) * i_timescale / (mtime_t)1000;
3163 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
3166 while( ( el = ep->Get() ) != NULL )
3168 if( MKV_IS_ID( el, KaxCueTrack ) )
3170 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
3172 ctrack.ReadData( es.I_O() );
3173 idx.i_track = uint16( ctrack );
3175 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
3177 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
3179 ccpos.ReadData( es.I_O() );
3180 idx.i_position = segment->GetGlobalPosition( uint64( ccpos ) );
3182 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
3184 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
3186 cbnum.ReadData( es.I_O() );
3187 idx.i_block_number = uint32( cbnum );
3191 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
3198 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
3204 msg_Dbg( &sys.demuxer, " * added time="I64Fd" pos="I64Fd
3205 " track=%d bnum=%d", idx.i_time, idx.i_position,
3206 idx.i_track, idx.i_block_number );
3210 if( i_index >= i_index_max )
3212 i_index_max += 1024;
3213 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
3219 msg_Dbg( &sys.demuxer, " * Unknown (%s)", typeid(*el).name() );
3227 msg_Dbg( &sys.demuxer, "loading cues done." );
3228 es.I_O().setFilePointer( i_sav_position, seek_beginning );
3231 void matroska_segment_c::LoadTags( )
3233 int64_t i_sav_position = es.I_O().getFilePointer();
3235 EbmlElement *el, *tags;
3237 msg_Dbg( &sys.demuxer, "loading tags" );
3238 es.I_O().setFilePointer( i_tags_position, seek_beginning );
3239 tags = es.FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
3243 msg_Err( &sys.demuxer, "cannot load tags (broken seekhead or file)" );
3244 es.I_O().setFilePointer( i_sav_position, seek_beginning );
3248 msg_Dbg( &sys.demuxer, "Tags" );
3249 ep = new EbmlParser( &es, tags, &sys.demuxer );
3250 while( ( el = ep->Get() ) != NULL )
3252 if( MKV_IS_ID( el, KaxTag ) )
3254 msg_Dbg( &sys.demuxer, "+ Tag" );
3256 while( ( el = ep->Get() ) != NULL )
3258 if( MKV_IS_ID( el, KaxTagTargets ) )
3260 msg_Dbg( &sys.demuxer, "| + Targets" );
3262 while( ( el = ep->Get() ) != NULL )
3264 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
3268 else if( MKV_IS_ID( el, KaxTagGeneral ) )
3270 msg_Dbg( &sys.demuxer, "| + General" );
3272 while( ( el = ep->Get() ) != NULL )
3274 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
3278 else if( MKV_IS_ID( el, KaxTagGenres ) )
3280 msg_Dbg( &sys.demuxer, "| + Genres" );
3282 while( ( el = ep->Get() ) != NULL )
3284 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
3288 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
3290 msg_Dbg( &sys.demuxer, "| + Audio Specific" );
3292 while( ( el = ep->Get() ) != NULL )
3294 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
3298 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
3300 msg_Dbg( &sys.demuxer, "| + Images Specific" );
3302 while( ( el = ep->Get() ) != NULL )
3304 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid( *el ).name() );
3308 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
3310 msg_Dbg( &sys.demuxer, "| + Multi Comment" );
3312 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
3314 msg_Dbg( &sys.demuxer, "| + Multi Commercial" );
3316 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
3318 msg_Dbg( &sys.demuxer, "| + Multi Date" );
3320 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
3322 msg_Dbg( &sys.demuxer, "| + Multi Entity" );
3324 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
3326 msg_Dbg( &sys.demuxer, "| + Multi Identifier" );
3328 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
3330 msg_Dbg( &sys.demuxer, "| + Multi Legal" );
3332 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
3334 msg_Dbg( &sys.demuxer, "| + Multi Title" );
3338 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid( *el ).name() );
3345 msg_Dbg( &sys.demuxer, "+ Unknown (%s)", typeid( *el ).name() );
3351 msg_Dbg( &sys.demuxer, "loading tags done." );
3352 es.I_O().setFilePointer( i_sav_position, seek_beginning );
3355 /*****************************************************************************
3357 *****************************************************************************/
3358 void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
3362 int i_upper_level = 0;
3364 msg_Dbg( &sys.demuxer, "| + Seek head" );
3366 /* Master elements */
3367 seekhead->Read( es, seekhead->Generic().Context, i_upper_level, el, true );
3369 for( i = 0; i < seekhead->ListSize(); i++ )
3371 EbmlElement *l = (*seekhead)[i];
3373 if( MKV_IS_ID( l, KaxSeek ) )
3375 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
3376 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
3379 for( j = 0; j < sk->ListSize(); j++ )
3381 EbmlElement *l = (*sk)[j];
3383 if( MKV_IS_ID( l, KaxSeekID ) )
3385 KaxSeekID &sid = *(KaxSeekID*)l;
3386 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
3388 else if( MKV_IS_ID( l, KaxSeekPosition ) )
3390 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
3391 i_pos = uint64( spos );
3395 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
3401 if( id == KaxCues::ClassInfos.GlobalId )
3403 msg_Dbg( &sys.demuxer, "| | | = cues at "I64Fd, i_pos );
3404 i_cues_position = segment->GetGlobalPosition( i_pos );
3406 else if( id == KaxChapters::ClassInfos.GlobalId )
3408 msg_Dbg( &sys.demuxer, "| | | = chapters at "I64Fd, i_pos );
3409 i_chapters_position = segment->GetGlobalPosition( i_pos );
3411 else if( id == KaxTags::ClassInfos.GlobalId )
3413 msg_Dbg( &sys.demuxer, "| | | = tags at "I64Fd, i_pos );
3414 i_tags_position = segment->GetGlobalPosition( i_pos );
3420 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3425 /*****************************************************************************
3427 *****************************************************************************/
3428 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
3434 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
3436 tk = new mkv_track_t();
3437 tracks.push_back( tk );
3439 /* Init the track */
3440 memset( tk, 0, sizeof( mkv_track_t ) );
3442 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
3443 tk->fmt.psz_language = strdup("English");
3444 tk->fmt.psz_description = NULL;
3446 tk->b_default = VLC_TRUE;
3447 tk->b_enabled = VLC_TRUE;
3448 tk->b_silent = VLC_FALSE;
3449 tk->i_number = tracks.size() - 1;
3450 tk->i_extra_data = 0;
3451 tk->p_extra_data = NULL;
3452 tk->psz_codec = NULL;
3453 tk->i_default_duration = 0;
3454 tk->f_timecodescale = 1.0;
3456 tk->b_inited = VLC_FALSE;
3457 tk->i_data_init = 0;
3458 tk->p_data_init = NULL;
3460 tk->psz_codec_name = NULL;
3461 tk->psz_codec_settings = NULL;
3462 tk->psz_codec_info_url = NULL;
3463 tk->psz_codec_download_url = NULL;
3465 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
3467 for( i = 0; i < m->ListSize(); i++ )
3469 EbmlElement *l = (*m)[i];
3471 if( MKV_IS_ID( l, KaxTrackNumber ) )
3473 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
3475 tk->i_number = uint32( tnum );
3476 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
3478 else if( MKV_IS_ID( l, KaxTrackUID ) )
3480 KaxTrackUID &tuid = *(KaxTrackUID*)l;
3482 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
3484 else if( MKV_IS_ID( l, KaxTrackType ) )
3487 KaxTrackType &ttype = *(KaxTrackType*)l;
3489 switch( uint8(ttype) )
3493 tk->fmt.i_cat = AUDIO_ES;
3497 tk->fmt.i_cat = VIDEO_ES;
3499 case track_subtitle:
3500 psz_type = "subtitle";
3501 tk->fmt.i_cat = SPU_ES;
3504 psz_type = "buttons";
3505 tk->fmt.i_cat = SPU_ES;
3508 psz_type = "unknown";
3509 tk->fmt.i_cat = UNKNOWN_ES;
3513 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
3515 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
3517 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
3519 // tk->b_enabled = uint32( fenb );
3520 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
3521 // uint32( fenb ) );
3523 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
3525 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
3527 tk->b_default = uint32( fdef );
3528 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
3530 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
3532 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
3534 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
3536 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
3538 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
3540 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
3542 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
3544 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
3546 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
3548 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
3550 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
3552 tk->i_default_duration = uint64(defd);
3553 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration="I64Fd, uint64(defd) );
3555 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
3557 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
3559 tk->f_timecodescale = float( ttcs );
3560 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
3562 else if( MKV_IS_ID( l, KaxTrackName ) )
3564 KaxTrackName &tname = *(KaxTrackName*)l;
3566 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
3567 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
3569 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
3571 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
3573 tk->fmt.psz_language = strdup( string( lang ).c_str() );
3574 msg_Dbg( &sys.demuxer,
3575 "| | | + Track Language=`%s'", tk->fmt.psz_language );
3577 else if( MKV_IS_ID( l, KaxCodecID ) )
3579 KaxCodecID &codecid = *(KaxCodecID*)l;
3581 tk->psz_codec = strdup( string( codecid ).c_str() );
3582 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
3584 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
3586 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
3588 tk->i_extra_data = cpriv.GetSize();
3589 if( tk->i_extra_data > 0 )
3591 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
3592 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
3594 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
3596 else if( MKV_IS_ID( l, KaxCodecName ) )
3598 KaxCodecName &cname = *(KaxCodecName*)l;
3600 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
3601 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
3603 else if( MKV_IS_ID( l, KaxContentEncodings ) )
3605 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
3606 MkvTree( sys.demuxer, 3, "Content Encodings" );
3607 for( j = 0; j < cencs->ListSize(); j++ )
3609 EbmlElement *l2 = (*cencs)[j];
3610 if( MKV_IS_ID( l2, KaxContentEncoding ) )
3612 MkvTree( sys.demuxer, 4, "Content Encoding" );
3613 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
3614 for( k = 0; k < cenc->ListSize(); k++ )
3616 EbmlElement *l3 = (*cenc)[k];
3617 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
3619 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
3620 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
3622 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
3624 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
3625 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
3627 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
3629 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
3630 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
3632 else if( MKV_IS_ID( l3, KaxContentCompression ) )
3634 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
3635 MkvTree( sys.demuxer, 5, "Content Compression" );
3636 for( n = 0; n < compr->ListSize(); n++ )
3638 EbmlElement *l4 = (*compr)[n];
3639 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
3641 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
3642 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
3643 if( uint32( compalg ) == 0 )
3645 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
3650 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
3657 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
3664 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
3669 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
3671 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
3673 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
3674 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
3676 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
3678 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
3680 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
3681 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
3683 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
3685 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
3687 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
3688 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
3690 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
3692 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
3694 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
3696 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
3698 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
3700 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
3702 else if( MKV_IS_ID( l, KaxTrackVideo ) )
3704 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
3707 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
3710 for( j = 0; j < tkv->ListSize(); j++ )
3712 EbmlElement *l = (*tkv)[j];
3713 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
3715 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
3717 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
3719 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
3721 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
3723 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
3726 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
3728 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
3730 tk->fmt.video.i_width = uint16( vwidth );
3731 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
3733 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
3735 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
3737 tk->fmt.video.i_height = uint16( vheight );
3738 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
3740 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
3742 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
3744 tk->fmt.video.i_visible_width = uint16( vwidth );
3745 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
3747 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
3749 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
3751 tk->fmt.video.i_visible_height = uint16( vheight );
3752 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
3754 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
3756 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
3758 tk->f_fps = float( vfps );
3759 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
3761 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
3763 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
3765 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
3766 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
3768 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
3770 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
3772 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
3774 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
3776 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
3778 // msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( gamma ) );
3782 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
3785 if ( tk->fmt.video.i_visible_height && tk->fmt.video.i_visible_width )
3786 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * tk->fmt.video.i_visible_width / tk->fmt.video.i_visible_height;
3788 else if( MKV_IS_ID( l, KaxTrackAudio ) )
3790 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
3793 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
3795 for( j = 0; j < tka->ListSize(); j++ )
3797 EbmlElement *l = (*tka)[j];
3799 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
3801 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
3803 tk->fmt.audio.i_rate = (int)float( afreq );
3804 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
3806 else if( MKV_IS_ID( l, KaxAudioChannels ) )
3808 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
3810 tk->fmt.audio.i_channels = uint8( achan );
3811 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
3813 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
3815 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
3817 tk->fmt.audio.i_bitspersample = uint8( abits );
3818 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
3822 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
3828 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
3829 typeid(*l).name() );
3834 /*****************************************************************************
3836 *****************************************************************************/
3837 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
3841 int i_upper_level = 0;
3843 msg_Dbg( &sys.demuxer, "| + Tracks" );
3845 /* Master elements */
3846 tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
3848 for( i = 0; i < tracks->ListSize(); i++ )
3850 EbmlElement *l = (*tracks)[i];
3852 if( MKV_IS_ID( l, KaxTrackEntry ) )
3854 ParseTrackEntry( static_cast<KaxTrackEntry *>(l) );
3858 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3863 /*****************************************************************************
3865 *****************************************************************************/
3866 void matroska_segment_c::ParseInfo( KaxInfo *info )
3871 int i_upper_level = 0;
3873 msg_Dbg( &sys.demuxer, "| + Information" );
3875 /* Master elements */
3876 m = static_cast<EbmlMaster *>(info);
3877 m->Read( es, info->Generic().Context, i_upper_level, el, true );
3879 for( i = 0; i < m->ListSize(); i++ )
3881 EbmlElement *l = (*m)[i];
3883 if( MKV_IS_ID( l, KaxSegmentUID ) )
3885 segment_uid = *(new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l)));
3887 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)segment_uid.GetBuffer() );
3889 else if( MKV_IS_ID( l, KaxPrevUID ) )
3891 prev_segment_uid = *(new KaxPrevUID(*static_cast<KaxPrevUID*>(l)));
3893 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)prev_segment_uid.GetBuffer() );
3895 else if( MKV_IS_ID( l, KaxNextUID ) )
3897 next_segment_uid = *(new KaxNextUID(*static_cast<KaxNextUID*>(l)));
3899 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)next_segment_uid.GetBuffer() );
3901 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
3903 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
3905 i_timescale = uint64(tcs);
3907 msg_Dbg( &sys.demuxer, "| | + TimecodeScale="I64Fd,
3910 else if( MKV_IS_ID( l, KaxDuration ) )
3912 KaxDuration &dur = *(KaxDuration*)l;
3914 i_duration = mtime_t( double( dur ) );
3916 msg_Dbg( &sys.demuxer, "| | + Duration="I64Fd,
3919 else if( MKV_IS_ID( l, KaxMuxingApp ) )
3921 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
3923 psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
3925 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
3926 psz_muxing_application );
3928 else if( MKV_IS_ID( l, KaxWritingApp ) )
3930 KaxWritingApp &wapp = *(KaxWritingApp*)l;
3932 psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
3934 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
3935 psz_writing_application );
3937 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
3939 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
3941 psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
3943 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
3944 psz_segment_filename );
3946 else if( MKV_IS_ID( l, KaxTitle ) )
3948 KaxTitle &title = *(KaxTitle*)l;
3950 psz_title = UTF8ToStr( UTFstring( title ) );
3952 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
3954 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
3956 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
3958 families.push_back(*uid);
3960 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
3962 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
3963 else if( MKV_IS_ID( l, KaxDateUTC ) )
3965 KaxDateUTC &date = *(KaxDateUTC*)l;
3970 i_date = date.GetEpochDate();
3971 memset( buffer, 0, 256 );
3972 if( gmtime_r( &i_date, &tmres ) &&
3973 asctime_r( &tmres, buffer ) )
3975 buffer[strlen( buffer)-1]= '\0';
3976 psz_date_utc = strdup( buffer );
3977 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
3981 #if LIBMATROSKA_VERSION >= 0x000704
3982 else if( MKV_IS_ID( l, KaxChapterTranslate ) )
3984 KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
3985 chapter_translation_c translated;
3987 p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
3988 for( j = 0; j < p_trans->ListSize(); j++ )
3990 EbmlElement *l = (*p_trans)[j];
3992 if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
3994 translated.editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
3996 else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
3998 translated.codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
4000 else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
4002 translated.translated = *( new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) ) );
4006 translations.push_back( translated );
4011 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4015 double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
4016 i_duration = mtime_t(f_dur);
4020 /*****************************************************************************
4022 *****************************************************************************/
4023 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
4027 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
4028 for( i = 0; i < ca->ListSize(); i++ )
4030 EbmlElement *l = (*ca)[i];
4032 if( MKV_IS_ID( l, KaxChapterUID ) )
4034 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
4035 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
4037 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
4039 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
4040 chapters.b_display_seekpoint = uint8( flag ) == 0;
4042 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
4044 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
4046 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
4047 chapters.i_start_time = uint64( start ) / I64C(1000);
4049 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
4051 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
4053 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
4054 chapters.i_end_time = uint64( end ) / I64C(1000);
4056 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
4058 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
4060 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
4062 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
4063 for( j = 0; j < cd->ListSize(); j++ )
4065 EbmlElement *l= (*cd)[j];
4067 if( MKV_IS_ID( l, KaxChapterString ) )
4071 KaxChapterString &name =*(KaxChapterString*)l;
4072 for (k = 0; k < i_level; k++)
4073 chapters.psz_name += '+';
4074 chapters.psz_name += ' ';
4075 chapters.psz_name += UTF8ToStr( UTFstring( name ) );
4076 chapters.b_user_display = true;
4078 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", UTF8ToStr(UTFstring(name)) );
4080 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
4082 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
4083 const char *psz = string( lang ).c_str();
4085 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
4087 else if( MKV_IS_ID( l, KaxChapterCountry ) )
4089 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
4090 const char *psz = string( ct ).c_str();
4092 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
4096 else if( MKV_IS_ID( l, KaxChapterProcess ) )
4098 msg_Dbg( &sys.demuxer, "| | | | + ChapterProcess" );
4100 KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
4101 chapter_codec_cmds_c *p_ccodec = NULL;
4103 for( j = 0; j < cp->ListSize(); j++ )
4105 EbmlElement *k= (*cp)[j];
4107 if( MKV_IS_ID( k, KaxChapterProcessCodecID ) )
4109 KaxChapterProcessCodecID *p_codec_id = static_cast<KaxChapterProcessCodecID*>( k );
4110 if ( uint32(*p_codec_id) == 0 )
4111 p_ccodec = new matroska_script_codec_c( sys );
4112 else if ( uint32(*p_codec_id) == 1 )
4113 p_ccodec = new dvd_chapter_codec_c( sys );
4118 if ( p_ccodec != NULL )
4120 for( j = 0; j < cp->ListSize(); j++ )
4122 EbmlElement *k= (*cp)[j];
4124 if( MKV_IS_ID( k, KaxChapterProcessPrivate ) )
4126 KaxChapterProcessPrivate * p_private = static_cast<KaxChapterProcessPrivate*>( k );
4127 p_ccodec->SetPrivate( *p_private );
4129 else if( MKV_IS_ID( k, KaxChapterProcessCommand ) )
4131 p_ccodec->AddCommand( *static_cast<KaxChapterProcessCommand*>( k ) );
4134 chapters.codecs.push_back( p_ccodec );
4137 else if( MKV_IS_ID( l, KaxChapterAtom ) )
4139 chapter_item_c *new_sub_chapter = new chapter_item_c();
4140 ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
4141 new_sub_chapter->psz_parent = &chapters;
4142 chapters.sub_chapters.push_back( new_sub_chapter );
4147 /*****************************************************************************
4149 *****************************************************************************/
4150 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
4154 int i_upper_level = 0;
4157 /* Master elements */
4158 chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
4160 for( i = 0; i < chapters->ListSize(); i++ )
4162 EbmlElement *l = (*chapters)[i];
4164 if( MKV_IS_ID( l, KaxEditionEntry ) )
4166 chapter_edition_c *p_edition = new chapter_edition_c();
4168 EbmlMaster *E = static_cast<EbmlMaster *>(l );
4170 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
4171 for( j = 0; j < E->ListSize(); j++ )
4173 EbmlElement *l = (*E)[j];
4175 if( MKV_IS_ID( l, KaxChapterAtom ) )
4177 chapter_item_c *new_sub_chapter = new chapter_item_c();
4178 ParseChapterAtom( 0, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
4179 p_edition->sub_chapters.push_back( new_sub_chapter );
4181 else if( MKV_IS_ID( l, KaxEditionUID ) )
4183 p_edition->i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
4185 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
4187 p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
4189 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
4191 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
4192 i_default_edition = stored_editions.size();
4196 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
4199 stored_editions.push_back( p_edition );
4203 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
4207 for( i = 0; i < stored_editions.size(); i++ )
4209 stored_editions[i]->RefreshChapters( );
4212 if ( stored_editions[i_default_edition]->b_ordered )
4214 /* update the duration of the segment according to the sum of all sub chapters */
4215 i_dur = stored_editions[i_default_edition]->Duration() / I64C(1000);
4221 void matroska_segment_c::ParseCluster( )
4226 int i_upper_level = 0;
4228 /* Master elements */
4229 m = static_cast<EbmlMaster *>( cluster );
4230 m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
4232 for( i = 0; i < m->ListSize(); i++ )
4234 EbmlElement *l = (*m)[i];
4236 if( MKV_IS_ID( l, KaxClusterTimecode ) )
4238 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)l;
4240 cluster->InitTimecode( uint64( ctc ), i_timescale );
4245 i_start_time = cluster->GlobalTimecode() / 1000;
4248 /*****************************************************************************
4249 * InformationCreate:
4250 *****************************************************************************/
4251 void matroska_segment_c::InformationCreate( )
4255 sys.meta = vlc_meta_New();
4259 vlc_meta_Add( sys.meta, VLC_META_TITLE, psz_title );
4263 vlc_meta_Add( sys.meta, VLC_META_DATE, psz_date_utc );
4265 if( psz_segment_filename )
4267 vlc_meta_Add( sys.meta, _("Segment filename"), psz_segment_filename );
4269 if( psz_muxing_application )
4271 vlc_meta_Add( sys.meta, _("Muxing application"), psz_muxing_application );
4273 if( psz_writing_application )
4275 vlc_meta_Add( sys.meta, _("Writing application"), psz_writing_application );
4278 for( i_track = 0; i_track < tracks.size(); i_track++ )
4280 mkv_track_t *tk = tracks[i_track];
4281 vlc_meta_t *mtk = vlc_meta_New();
4283 sys.meta->track = (vlc_meta_t**)realloc( sys.meta->track,
4284 sizeof( vlc_meta_t * ) * ( sys.meta->i_track + 1 ) );
4285 sys.meta->track[sys.meta->i_track++] = mtk;
4287 if( tk->fmt.psz_description )
4289 vlc_meta_Add( sys.meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
4291 if( tk->psz_codec_name )
4293 vlc_meta_Add( sys.meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
4295 if( tk->psz_codec_settings )
4297 vlc_meta_Add( sys.meta, VLC_META_SETTING, tk->psz_codec_settings );
4299 if( tk->psz_codec_info_url )
4301 vlc_meta_Add( sys.meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
4303 if( tk->psz_codec_download_url )
4305 vlc_meta_Add( sys.meta, VLC_META_URL, tk->psz_codec_download_url );
4309 if( i_tags_position >= 0 )
4311 vlc_bool_t b_seekable;
4313 stream_Control( sys.demuxer.s, STREAM_CAN_FASTSEEK, &b_seekable );
4322 /*****************************************************************************
4324 *****************************************************************************/
4326 void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
4328 #define idx index[i_index]
4330 idx.i_block_number= -1;
4331 idx.i_position = cluster->GetElementPosition();
4333 idx.b_key = VLC_TRUE;
4336 if( i_index >= i_index_max )
4338 i_index_max += 1024;
4339 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
4344 static char * UTF8ToStr( const UTFstring &u )
4353 p = dst = (char*)malloc( i_src + 1);
4372 void chapter_edition_c::RefreshChapters( )
4374 chapter_item_c::RefreshChapters( b_ordered, -1 );
4375 b_display_seekpoint = false;
4378 int64_t chapter_item_c::RefreshChapters( bool b_ordered, int64_t i_prev_user_time )
4380 int64_t i_user_time = i_prev_user_time;
4382 // first the sub-chapters, and then ourself
4383 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
4384 while ( index != sub_chapters.end() )
4386 i_user_time = (*index)->RefreshChapters( b_ordered, i_user_time );
4392 // the ordered chapters always start at zero
4393 if ( i_prev_user_time == -1 )
4395 if ( i_user_time == -1 )
4397 i_prev_user_time = 0;
4400 i_user_start_time = i_prev_user_time;
4401 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
4403 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
4407 i_user_end_time = i_user_time;
4412 std::sort( sub_chapters.begin(), sub_chapters.end() );
4413 i_user_start_time = i_start_time;
4414 if ( i_end_time != -1 )
4415 i_user_end_time = i_end_time;
4416 else if ( i_user_time != -1 )
4417 i_user_end_time = i_user_time;
4419 i_user_end_time = i_user_start_time;
4422 return i_user_end_time;
4425 mtime_t chapter_edition_c::Duration() const
4427 mtime_t i_result = 0;
4429 if ( sub_chapters.size() )
4431 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.end();
4433 i_result = (*index)->i_user_end_time;
4439 chapter_item_c *chapter_item_c::FindTimecode( mtime_t i_user_timecode )
4441 chapter_item_c *psz_result = NULL;
4443 if ( i_user_timecode >= i_user_start_time &&
4444 ( i_user_timecode < i_user_end_time ||
4445 ( i_user_start_time == i_user_end_time && i_user_timecode == i_user_end_time )))
4447 std::vector<chapter_item_c*>::iterator index = sub_chapters.begin();
4448 while ( index != sub_chapters.end() && psz_result == NULL )
4450 psz_result = (*index)->FindTimecode( i_user_timecode );
4454 if ( psz_result == NULL )
4461 bool chapter_item_c::ParentOf( const chapter_item_c & item ) const
4463 if ( &item == this )
4466 std::vector<chapter_item_c*>::const_iterator index = sub_chapters.begin();
4467 while ( index != sub_chapters.end() )
4469 if ( (*index)->ParentOf( item ) )
4477 void demux_sys_t::PreloadFamily( const matroska_segment_c & of_segment )
4479 for (size_t i=0; i<opened_segments.size(); i++)
4481 opened_segments[i]->PreloadFamily( of_segment );
4484 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
4489 for (size_t i=0; i<families.size(); i++)
4491 for (size_t j=0; j<of_segment.families.size(); j++)
4493 if ( families[i] == of_segment.families[j] )
4501 // preload all the linked segments for all preloaded segments
4502 void demux_sys_t::PreloadLinked( matroska_segment_c *p_segment )
4504 size_t i_preloaded, i, j;
4505 virtual_segment_c *p_seg;
4507 p_current_segment = VirtualFromSegments( p_segment );
4509 used_segments.push_back( p_current_segment );
4511 // create all the other virtual segments of the family
4514 for ( i=0; i< opened_segments.size(); i++ )
4516 if ( opened_segments[i]->b_preloaded && !IsUsedSegment( *opened_segments[i] ) )
4518 p_seg = VirtualFromSegments( opened_segments[i] );
4519 used_segments.push_back( p_seg );
4523 } while ( i_preloaded ); // worst case: will stop when all segments are found as family related
4525 // publish all editions of all usable segment
4526 for ( i=0; i< used_segments.size(); i++ )
4528 p_seg = used_segments[i];
4529 if ( p_seg->p_editions != NULL )
4531 std::string sz_name;
4532 input_title_t *p_title = vlc_input_title_New();
4533 p_seg->i_sys_title = i;
4536 // TODO use a name for each edition, let the TITLE deal with a codec name
4537 for ( j=0; j<p_seg->p_editions->size(); j++ )
4539 if ( p_title->psz_name == NULL )
4541 sz_name = (*p_seg->p_editions)[j]->GetMainName();
4542 if ( sz_name != "" )
4543 p_title->psz_name = strdup( sz_name.c_str() );
4546 chapter_edition_c *p_edition = (*p_seg->p_editions)[j];
4549 p_edition->PublishChapters( *p_title, i_chapters, 0 );
4552 // create a name if there is none
4553 if ( p_title->psz_name == NULL )
4555 sz_name = N_("Segment ");
4557 sprintf( psz_str, "%d", i );
4559 p_title->psz_name = strdup( sz_name.c_str() );
4562 titles.push_back( *p_title );
4567 bool demux_sys_t::IsUsedSegment( matroska_segment_c &segment ) const
4569 for ( size_t i=0; i< used_segments.size(); i++ )
4571 if ( used_segments[i]->FindUID( segment.segment_uid ) )
4577 virtual_segment_c *demux_sys_t::VirtualFromSegments( matroska_segment_c *p_segment ) const
4579 size_t i_preloaded, i;
4581 virtual_segment_c *p_result = new virtual_segment_c( p_segment );
4583 // fill our current virtual segment with all hard linked segments
4586 for ( i=0; i< opened_segments.size(); i++ )
4588 i_preloaded += p_result->AddSegment( opened_segments[i] );
4590 } while ( i_preloaded ); // worst case: will stop when all segments are found as linked
4594 p_result->PreloadLinked( );
4596 p_result->PrepareChapters( );
4601 bool demux_sys_t::PreparePlayback( virtual_segment_c *p_new_segment )
4603 if ( p_new_segment != NULL && p_new_segment != p_current_segment )
4605 if ( p_current_segment != NULL && p_current_segment->Segment() != NULL )
4606 p_current_segment->Segment()->UnSelect();
4608 p_current_segment = p_new_segment;
4609 i_current_title = p_new_segment->i_sys_title;
4612 p_current_segment->LoadCues();
4613 f_duration = p_current_segment->Duration();
4615 /* add information */
4616 p_current_segment->Segment()->InformationCreate( );
4618 p_current_segment->Segment()->Select( 0 );
4623 bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a, const matroska_segment_c * p_item_b )
4625 EbmlBinary * p_itema = (EbmlBinary *)(&p_item_a->segment_uid);
4626 if ( *p_itema == p_item_b->prev_segment_uid )
4629 p_itema = (EbmlBinary *)(&p_item_a->next_segment_uid);
4630 if ( *p_itema == p_item_b->segment_uid )
4633 if ( *p_itema == p_item_b->prev_segment_uid )
4639 bool matroska_segment_c::Preload( )
4644 EbmlElement *el = NULL;
4646 ep->Reset( &sys.demuxer );
4648 while( ( el = ep->Get() ) != NULL )
4650 if( MKV_IS_ID( el, KaxInfo ) )
4652 ParseInfo( static_cast<KaxInfo*>( el ) );
4654 else if( MKV_IS_ID( el, KaxTracks ) )
4656 ParseTracks( static_cast<KaxTracks*>( el ) );
4658 else if( MKV_IS_ID( el, KaxSeekHead ) )
4660 ParseSeekHead( static_cast<KaxSeekHead*>( el ) );
4662 else if( MKV_IS_ID( el, KaxCues ) )
4664 msg_Dbg( &sys.demuxer, "| + Cues" );
4666 else if( MKV_IS_ID( el, KaxCluster ) )
4668 msg_Dbg( &sys.demuxer, "| + Cluster" );
4670 cluster = (KaxCluster*)el;
4672 i_start_pos = cluster->GetElementPosition();
4676 /* stop parsing the stream */
4679 else if( MKV_IS_ID( el, KaxAttachments ) )
4681 msg_Dbg( &sys.demuxer, "| + Attachments FIXME (but probably never supported)" );
4683 else if( MKV_IS_ID( el, KaxChapters ) )
4685 msg_Dbg( &sys.demuxer, "| + Chapters" );
4686 ParseChapters( static_cast<KaxChapters*>( el ) );
4688 else if( MKV_IS_ID( el, KaxTag ) )
4690 msg_Dbg( &sys.demuxer, "| + Tags FIXME TODO" );
4694 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid(*el).name() );
4703 matroska_segment_c *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
4705 for (size_t i=0; i<opened_segments.size(); i++)
4707 if ( opened_segments[i]->segment_uid == uid )
4708 return opened_segments[i];
4713 chapter_item_c *demux_sys_t::BrowseCodecPrivate( unsigned int codec_id,
4714 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
4715 const void *p_cookie,
4716 size_t i_cookie_size,
4717 virtual_segment_c * &p_segment_found )
4719 chapter_item_c *p_result = NULL;
4720 for (size_t i=0; i<used_segments.size(); i++)
4722 p_result = used_segments[i]->BrowseCodecPrivate( codec_id, match, p_cookie, i_cookie_size );
4723 if ( p_result != NULL )
4725 p_segment_found = used_segments[i];
4732 chapter_item_c *demux_sys_t::FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found )
4734 chapter_item_c *p_result = NULL;
4735 for (size_t i=0; i<used_segments.size(); i++)
4737 p_result = used_segments[i]->FindChapter( i_find_uid );
4738 if ( p_result != NULL )
4740 p_segment_found = used_segments[i];
4747 void virtual_segment_c::Sort()
4749 // keep the current segment index
4750 matroska_segment_c *p_segment = linked_segments[i_current_segment];
4752 std::sort( linked_segments.begin(), linked_segments.end(), matroska_segment_c::CompareSegmentUIDs );
4754 for ( i_current_segment=0; i_current_segment<linked_segments.size(); i_current_segment++)
4755 if ( linked_segments[i_current_segment] == p_segment )
4759 size_t virtual_segment_c::AddSegment( matroska_segment_c *p_segment )
4762 // check if it's not already in here
4763 for ( i=0; i<linked_segments.size(); i++ )
4765 if ( p_segment->segment_uid == linked_segments[i]->segment_uid )
4769 // find possible mates
4770 for ( i=0; i<linked_uids.size(); i++ )
4772 if ( p_segment->segment_uid == linked_uids[i]
4773 || p_segment->prev_segment_uid == linked_uids[i]
4774 || p_segment->next_segment_uid == linked_uids[i] )
4776 linked_segments.push_back( p_segment );
4778 AppendUID( p_segment->prev_segment_uid );
4779 AppendUID( p_segment->next_segment_uid );
4787 void virtual_segment_c::PreloadLinked( )
4789 for ( size_t i=0; i<linked_segments.size(); i++ )
4791 linked_segments[i]->Preload( );
4793 i_current_edition = linked_segments[0]->i_default_edition;
4796 mtime_t virtual_segment_c::Duration() const
4799 if ( linked_segments.size() == 0 )
4802 matroska_segment_c *p_last_segment = linked_segments[linked_segments.size()-1];
4803 // p_last_segment->ParseCluster( );
4805 i_duration = p_last_segment->i_start_time / 1000 + p_last_segment->i_duration;
4810 void virtual_segment_c::LoadCues( )
4812 for ( size_t i=0; i<linked_segments.size(); i++ )
4814 linked_segments[i]->LoadCues();
4818 void virtual_segment_c::AppendUID( const EbmlBinary & UID )
4820 if ( UID.GetBuffer() == NULL )
4823 for (size_t i=0; i<linked_uids.size(); i++)
4825 if ( UID == linked_uids[i] )
4828 linked_uids.push_back( *(KaxSegmentUID*)(&UID) );
4831 void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset )
4834 int i_track_skipping;
4835 int64_t i_block_duration;
4836 int64_t i_block_ref1;
4837 int64_t i_block_ref2;
4839 int64_t i_seek_position = i_start_pos;
4840 int64_t i_seek_time = i_start_time;
4846 for( ; i_idx < i_index; i_idx++ )
4848 if( index[i_idx].i_time + i_time_offset > i_date )
4859 i_seek_position = index[i_idx].i_position;
4860 i_seek_time = index[i_idx].i_time;
4863 msg_Dbg( &sys.demuxer, "seek got "I64Fd" (%d%%)",
4864 i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
4866 es.I_O().setFilePointer( i_seek_position, seek_beginning );
4869 ep = new EbmlParser( &es, segment, &sys.demuxer );
4872 sys.i_start_pts = i_date;
4874 es_out_Control( sys.demuxer.out, ES_OUT_RESET_PCR );
4876 /* now parse until key frame */
4877 i_track_skipping = 0;
4878 for( i_track = 0; i_track < tracks.size(); i_track++ )
4880 if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
4882 tracks[i_track]->b_search_keyframe = VLC_TRUE;
4885 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tracks[i_track]->p_es, i_date );
4889 while( i_track_skipping > 0 )
4891 if( BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
4893 msg_Warn( &sys.demuxer, "cannot get block EOF?" );
4898 for( i_track = 0; i_track < tracks.size(); i_track++ )
4900 if( tracks[i_track]->i_number == block->TrackNum() )
4906 sys.i_pts = sys.i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
4908 if( i_track < tracks.size() )
4910 if( sys.i_pts >= sys.i_start_pts )
4912 BlockDecode( &sys.demuxer, block, sys.i_pts, 0 );
4913 i_track_skipping = 0;
4915 else if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
4917 if( i_block_ref1 == -1 && tracks[i_track]->b_search_keyframe )
4919 tracks[i_track]->b_search_keyframe = VLC_FALSE;
4922 if( !tracks[i_track]->b_search_keyframe )
4924 BlockDecode( &sys.demuxer, block, sys.i_pts, 0 );
4933 void virtual_segment_c::Seek( demux_t & demuxer, mtime_t i_date, mtime_t i_time_offset, chapter_item_c *psz_chapter )
4935 demux_sys_t *p_sys = demuxer.p_sys;
4938 // find the actual time for an ordered edition
4939 if ( psz_chapter == NULL )
4941 if ( Edition() && Edition()->b_ordered )
4943 /* 1st, we need to know in which chapter we are */
4944 psz_chapter = (*p_editions)[i_current_edition]->FindTimecode( i_date );
4948 if ( psz_chapter != NULL )
4950 psz_current_chapter = psz_chapter;
4951 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
4952 if ( psz_chapter->i_seekpoint_num > 0 )
4954 demuxer.info.i_update |= INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
4955 demuxer.info.i_title = p_sys->i_current_title = i_sys_title;
4956 demuxer.info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
4960 // find the best matching segment
4961 for ( i=0; i<linked_segments.size(); i++ )
4963 if ( i_date < linked_segments[i]->i_start_time )
4970 if ( i_current_segment != i )
4972 linked_segments[i_current_segment]->UnSelect();
4973 linked_segments[i]->Select( i_date );
4974 i_current_segment = i;
4977 linked_segments[i]->Seek( i_date, i_time_offset );
4980 void chapter_codec_cmds_c::AddCommand( const KaxChapterProcessCommand & command )
4984 uint32 codec_time = uint32(-1);
4985 for( i = 0; i < command.ListSize(); i++ )
4987 const EbmlElement *k = command[i];
4989 if( MKV_IS_ID( k, KaxChapterProcessTime ) )
4991 codec_time = uint32( *static_cast<const KaxChapterProcessTime*>( k ) );
4996 for( i = 0; i < command.ListSize(); i++ )
4998 const EbmlElement *k = command[i];
5000 if( MKV_IS_ID( k, KaxChapterProcessData ) )
5002 KaxChapterProcessData *p_data = new KaxChapterProcessData( *static_cast<const KaxChapterProcessData*>( k ) );
5003 switch ( codec_time )
5006 during_cmds.push_back( *p_data );
5009 enter_cmds.push_back( *p_data );
5012 leave_cmds.push_back( *p_data );
5021 bool chapter_item_c::Enter( bool b_do_subs )
5023 bool f_result = false;
5024 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
5025 while ( index != codecs.end() )
5027 f_result |= (*index)->Enter();
5034 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
5035 while ( index_ != sub_chapters.end() )
5037 f_result |= (*index_)->Enter( true );
5044 bool chapter_item_c::Leave( bool b_do_subs )
5046 bool f_result = false;
5047 b_is_leaving = true;
5048 std::vector<chapter_codec_cmds_c*>::iterator index = codecs.begin();
5049 while ( index != codecs.end() )
5051 f_result |= (*index)->Leave();
5058 std::vector<chapter_item_c*>::iterator index_ = sub_chapters.begin();
5059 while ( index_ != sub_chapters.end() )
5061 f_result |= (*index_)->Leave( true );
5065 b_is_leaving = false;
5069 bool chapter_item_c::EnterAndLeave( chapter_item_c *p_item )
5071 chapter_item_c *p_common_parent = p_item;
5073 // leave, up to a common parent
5074 while ( p_common_parent != NULL && !p_common_parent->ParentOf( *this ) )
5076 if ( !p_common_parent->b_is_leaving && p_common_parent->Leave( false ) )
5078 p_common_parent = p_common_parent->psz_parent;
5081 // enter from the parent to <this>
5082 if ( p_common_parent != NULL )
5086 if ( p_common_parent == this )
5087 return Enter( true );
5089 for ( size_t i = 0; i<p_common_parent->sub_chapters.size(); i++ )
5091 if ( p_common_parent->sub_chapters[i]->ParentOf( *this ) )
5093 p_common_parent = p_common_parent->sub_chapters[i];
5094 if ( p_common_parent != this )
5095 if ( p_common_parent->Enter( false ) )
5104 return Enter( true );
5107 bool dvd_chapter_codec_c::Enter()
5109 bool f_result = false;
5110 std::vector<KaxChapterProcessData>::iterator index = enter_cmds.begin();
5111 while ( index != enter_cmds.end() )
5113 if ( (*index).GetSize() )
5115 binary *p_data = (*index).GetBuffer();
5116 size_t i_size = *p_data++;
5117 // avoid reading too much from the buffer
5118 i_size = min( i_size, ((*index).GetSize() - 1) >> 3 );
5119 for ( ; i_size > 0; i_size--, p_data += 8 )
5121 msg_Dbg( &sys.demuxer, "Matroska DVD enter command" );
5122 f_result |= interpretor.Interpret( p_data );
5130 bool dvd_chapter_codec_c::Leave()
5132 bool f_result = false;
5133 std::vector<KaxChapterProcessData>::iterator index = leave_cmds.begin();
5134 while ( index != leave_cmds.end() )
5136 if ( (*index).GetSize() )
5138 binary *p_data = (*index).GetBuffer();
5139 size_t i_size = *p_data++;
5140 // avoid reading too much from the buffer
5141 i_size = min( i_size, ((*index).GetSize() - 1) >> 3 );
5142 for ( ; i_size > 0; i_size--, p_data += 8 )
5144 msg_Dbg( &sys.demuxer, "Matroska DVD leave command" );
5145 f_result |= interpretor.Interpret( p_data );
5153 // see http://www.dvd-replica.com/DVD/vmcmdset.php for a description of DVD commands
5154 bool dvd_command_interpretor_c::Interpret( const binary * p_command, size_t i_size )
5159 virtual_segment_c *p_segment;
5160 chapter_item_c *p_chapter;
5161 bool f_result = false;
5162 uint16 i_command = ( p_command[0] << 8 ) + p_command[1];
5164 // handle register tests if there are some
5165 if ( (i_command & 0xF0) != 0 )
5167 bool b_test_positive = (i_command & CMD_DVD_IF_NOT) == 0;
5168 bool b_test_value = (i_command & CMD_DVD_TEST_VALUE) != 0;
5169 uint8 i_test = i_command & 0x70;
5173 i_value = ( p_command[4] << 8 ) + p_command[5];
5175 i_value = GetPRM( p_command[7] );
5179 case CMD_DVD_IF_GPREG_EQUAL:
5181 msg_Dbg( &sys.demuxer, "IF %s EQUALS %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
5182 if (!( GetPRM( p_command[3] ) == i_value ))
5184 b_test_positive = !b_test_positive;
5187 case CMD_DVD_IF_GPREG_INF:
5189 msg_Dbg( &sys.demuxer, "IF %s < %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
5190 if (!( GetPRM( p_command[3] ) < i_value ))
5192 b_test_positive = !b_test_positive;
5195 case CMD_DVD_IF_GPREG_AND:
5197 msg_Dbg( &sys.demuxer, "IF %s & %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
5198 if (!( GetPRM( p_command[3] ) & i_value ))
5200 b_test_positive = !b_test_positive;
5203 case CMD_DVD_IF_GPREG_SUP_EQUAL:
5204 // if superior or equal
5205 msg_Dbg( &sys.demuxer, "IF %s >= %s", GetRegTypeName( false, p_command[3] ).c_str(), GetRegTypeName( b_test_value, i_value ).c_str() );
5206 if (!( GetPRM( p_command[3] ) >= i_value ))
5208 b_test_positive = !b_test_positive;
5213 if ( !b_test_positive )
5217 // strip the test command
5218 i_command &= 0xFF0F;
5220 switch ( i_command )
5225 msg_Dbg( &sys.demuxer, "NOP" );
5230 msg_Dbg( &sys.demuxer, "Break" );
5234 case CMD_DVD_JUMP_TT:
5236 uint8 i_title = p_command[5];
5237 msg_Dbg( &sys.demuxer, "JumpTT %d", i_title );
5239 // find in the ChapProcessPrivate matching this Title level
5240 p_chapter = sys.BrowseCodecPrivate( 1, MatchTitleNumber, &i_title, sizeof(i_title), p_segment );
5241 if ( p_chapter != NULL )
5243 // if the segment is not part of the current segment, select the new one
5244 if ( p_segment != sys.p_current_segment )
5246 sys.PreparePlayback( p_segment );
5249 // jump to the location in the found segment
5250 p_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, NULL );
5251 p_chapter->Enter( true );
5258 case CMD_DVD_CALLSS_VTSM1:
5260 msg_Dbg( &sys.demuxer, "CallSS VTSM" );
5261 switch( (p_command[6] & 0xC0) >> 6 ) {
5263 switch ( p_command[5] )
5266 msg_Dbg( &sys.demuxer, "CallSS PGC (rsm_cell %x)", p_command[5]);
5269 msg_Dbg( &sys.demuxer, "CallSS Title Entry (rsm_cell %x)", p_command[5]);
5272 msg_Dbg( &sys.demuxer, "CallSS Root Menu (rsm_cell %x)", p_command[5]);
5275 msg_Dbg( &sys.demuxer, "CallSS Subpicture Menu (rsm_cell %x)", p_command[5]);
5278 msg_Dbg( &sys.demuxer, "CallSS Audio Menu (rsm_cell %x)", p_command[5]);
5281 msg_Dbg( &sys.demuxer, "CallSS Angle Menu (rsm_cell %x)", p_command[5]);
5284 msg_Dbg( &sys.demuxer, "CallSS Chapter Menu (rsm_cell %x)", p_command[5]);
5287 msg_Dbg( &sys.demuxer, "CallSS <unknown> (rsm_cell %x)", p_command[5]);
5290 p_chapter = sys.BrowseCodecPrivate( 1, MatchPgcType, &p_command[5], 1, p_segment );
5291 if ( p_chapter != NULL )
5293 // if the segment is not part of the current segment, select the new one
5294 if ( p_segment != sys.p_current_segment )
5296 sys.PreparePlayback( p_segment );
5299 p_chapter->Enter( true );
5301 // jump to the location in the found segment
5302 p_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );
5307 msg_Dbg( &sys.demuxer, "CallSS VMGM (menu %d, rsm_cell %x)", p_command[6] & 0x0F, p_command[5]);
5310 msg_Dbg( &sys.demuxer, "CallSS VTSM (menu %d, rsm_cell %x)", p_command[6] & 0x0F, p_command[5]);
5313 msg_Dbg( &sys.demuxer, "CallSS VMGM (pgc %d, rsm_cell %x)", (p_command[3] << 8) + p_command[4], p_command[5]);
5318 case CMD_DVD_SET_GPRMMD:
5320 msg_Dbg( &sys.demuxer, "Set GPRMMD [%d]=%d", (p_command[4] << 8) + p_command[5], (p_command[2] << 8) + p_command[3]);
5322 if ( !SetGPRM( (p_command[4] << 8) + p_command[5], (p_command[2] << 8) + p_command[3] ) )
5323 msg_Dbg( &sys.demuxer, "Set GPRMMD failed" );
5326 case CMD_DVD_LINKPGCN:
5328 uint16 i_pgcn = (p_command[6] << 8) + p_command[7];
5330 msg_Dbg( &sys.demuxer, "Link PGCN(%d)", i_pgcn );
5334 case CMD_DVD_LINKCN:
5336 msg_Dbg( &sys.demuxer, "LinkCN (cell %d)", (p_command[6] << 8) + p_command[7] );
5341 case CMD_DVD_GOTO_LINE:
5343 msg_Dbg( &sys.demuxer, "GotoLine (%d)", (p_command[6] << 8) + p_command[7] );
5349 msg_Dbg( &sys.demuxer, "unsupported command : %02X %02X %02X %02X %02X %02X %02X %02X"
5365 bool dvd_command_interpretor_c::MatchTitleNumber( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
5367 if ( i_cookie_size != 1 || data.m_private_data.GetSize() < 4 )
5370 if ( data.m_private_data.GetBuffer()[0] != MATROSKA_DVD_LEVEL_TT )
5373 uint16 i_gtitle = (data.m_private_data.GetBuffer()[1] << 8 ) + data.m_private_data.GetBuffer()[2];
5374 uint8 i_title = *(uint8*)p_cookie;
5376 return (i_gtitle == i_title);
5379 bool dvd_command_interpretor_c::MatchPgcType( const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size )
5381 if ( i_cookie_size != 1 || data.m_private_data.GetSize() < 7 )
5384 if ( data.m_private_data.GetBuffer()[0] != MATROSKA_DVD_LEVEL_PGC )
5387 uint8 i_pgc_type = data.m_private_data.GetBuffer()[3];
5388 uint8 i_pgc = *(uint8*)p_cookie;
5390 return (i_pgc_type == i_pgc);
5393 bool matroska_script_codec_c::Enter()
5395 bool f_result = false;
5396 std::vector<KaxChapterProcessData>::iterator index = enter_cmds.begin();
5397 while ( index != enter_cmds.end() )
5399 if ( (*index).GetSize() )
5401 msg_Dbg( &sys.demuxer, "Matroska Script enter command" );
5402 f_result |= interpretor.Interpret( (*index).GetBuffer(), (*index).GetSize() );
5409 bool matroska_script_codec_c::Leave()
5411 bool f_result = false;
5412 std::vector<KaxChapterProcessData>::iterator index = leave_cmds.begin();
5413 while ( index != leave_cmds.end() )
5415 if ( (*index).GetSize() )
5417 msg_Dbg( &sys.demuxer, "Matroska Script leave command" );
5418 f_result |= interpretor.Interpret( (*index).GetBuffer(), (*index).GetSize() );
5425 // see http://www.matroska.org/technical/specs/chapters/index.html#mscript
5426 // for a description of existing commands
5427 bool matroska_script_interpretor_c::Interpret( const binary * p_command, size_t i_size )
5429 bool b_result = false;
5431 char *psz_str = (char*) malloc( i_size + 1 );
5432 memcpy( psz_str, p_command, i_size );
5433 psz_str[ i_size ] = '\0';
5435 std::string sz_command = psz_str;
5438 msg_Dbg( &sys.demuxer, "command : %s", sz_command.c_str() );
5440 if ( sz_command.compare( 0, CMD_MS_GOTO_AND_PLAY.size(), CMD_MS_GOTO_AND_PLAY ) == 0 )
5445 for ( i=CMD_MS_GOTO_AND_PLAY.size(); i<sz_command.size(); i++)
5447 if ( sz_command[i] == '(' )
5454 for ( j=i; j<sz_command.size(); j++)
5456 if ( sz_command[j] == ')' )
5463 std::string st = sz_command.substr( i+1, j-i-1 );
5464 int64_t i_chapter_uid = atoi( st.c_str() );
5466 virtual_segment_c *p_segment;
5467 chapter_item_c *p_chapter = sys.FindChapter( i_chapter_uid, p_segment );
5469 if ( p_chapter == NULL )
5470 msg_Dbg( &sys.demuxer, "Chapter "I64Fd" not found", i_chapter_uid);
5473 if ( !p_chapter->EnterAndLeave( sys.p_current_segment->CurrentChapter() ) )
5474 p_segment->Seek( sys.demuxer, p_chapter->i_user_start_time, -1, p_chapter );