1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2004 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Steve Lhomme <steve.lhomme@free.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
30 #include "chapter_command.hpp"
31 #include "virtual_segment.hpp"
33 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
34 #undef ATTRIBUTE_PACKED
35 #undef PRAGMA_PACK_BEGIN
36 #undef PRAGMA_PACK_END
39 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
40 #define ATTRIBUTE_PACKED __attribute__ ((packed))
45 #if !defined(ATTRIBUTE_PACKED)
46 #define ATTRIBUTE_PACKED
54 /*************************************
55 * taken from libdvdnav / libdvdread
56 **************************************/
59 * DVD Time Information.
65 uint8_t frame_u; /* The two high bits are the frame rate. */
66 } ATTRIBUTE_PACKED dvd_time_t;
72 #ifdef WORDS_BIGENDIAN
73 unsigned char zero : 7; /* 25-31 */
74 unsigned char video_pres_mode_change : 1; /* 24 */
76 unsigned char karaoke_audio_pres_mode_change : 1; /* 23 */
77 unsigned char angle_change : 1;
78 unsigned char subpic_stream_change : 1;
79 unsigned char audio_stream_change : 1;
80 unsigned char pause_on : 1;
81 unsigned char still_off : 1;
82 unsigned char button_select_or_activate : 1;
83 unsigned char resume : 1; /* 16 */
85 unsigned char chapter_menu_call : 1; /* 15 */
86 unsigned char angle_menu_call : 1;
87 unsigned char audio_menu_call : 1;
88 unsigned char subpic_menu_call : 1;
89 unsigned char root_menu_call : 1;
90 unsigned char title_menu_call : 1;
91 unsigned char backward_scan : 1;
92 unsigned char forward_scan : 1; /* 8 */
94 unsigned char next_pg_search : 1; /* 7 */
95 unsigned char prev_or_top_pg_search : 1;
96 unsigned char time_or_chapter_search : 1;
97 unsigned char go_up : 1;
98 unsigned char stop : 1;
99 unsigned char title_play : 1;
100 unsigned char chapter_search_or_play : 1;
101 unsigned char title_or_time_play : 1; /* 0 */
103 unsigned char video_pres_mode_change : 1; /* 24 */
104 unsigned char zero : 7; /* 25-31 */
106 unsigned char resume : 1; /* 16 */
107 unsigned char button_select_or_activate : 1;
108 unsigned char still_off : 1;
109 unsigned char pause_on : 1;
110 unsigned char audio_stream_change : 1;
111 unsigned char subpic_stream_change : 1;
112 unsigned char angle_change : 1;
113 unsigned char karaoke_audio_pres_mode_change : 1; /* 23 */
115 unsigned char forward_scan : 1; /* 8 */
116 unsigned char backward_scan : 1;
117 unsigned char title_menu_call : 1;
118 unsigned char root_menu_call : 1;
119 unsigned char subpic_menu_call : 1;
120 unsigned char audio_menu_call : 1;
121 unsigned char angle_menu_call : 1;
122 unsigned char chapter_menu_call : 1; /* 15 */
124 unsigned char title_or_time_play : 1; /* 0 */
125 unsigned char chapter_search_or_play : 1;
126 unsigned char title_play : 1;
127 unsigned char stop : 1;
128 unsigned char go_up : 1;
129 unsigned char time_or_chapter_search : 1;
130 unsigned char prev_or_top_pg_search : 1;
131 unsigned char next_pg_search : 1; /* 7 */
133 } ATTRIBUTE_PACKED user_ops_t;
136 * Type to store per-command data.
140 } ATTRIBUTE_PACKED vm_cmd_t;
141 #define COMMAND_DATA_SIZE 8
144 * PCI General Information
147 uint32_t nv_pck_lbn; /**< sector address of this nav pack */
148 uint16_t vobu_cat; /**< 'category' of vobu */
149 uint16_t zero1; /**< reserved */
150 user_ops_t vobu_uop_ctl; /**< UOP of vobu */
151 uint32_t vobu_s_ptm; /**< start presentation time of vobu */
152 uint32_t vobu_e_ptm; /**< end presentation time of vobu */
153 uint32_t vobu_se_e_ptm; /**< end ptm of sequence end in vobu */
154 dvd_time_t e_eltm; /**< Cell elapsed time */
156 } ATTRIBUTE_PACKED pci_gi_t;
159 * Non Seamless Angle Information
162 uint32_t nsml_agl_dsta[9]; /**< address of destination vobu in AGL_C#n */
163 } ATTRIBUTE_PACKED nsml_agli_t;
166 * Highlight General Information
168 * For btngrX_dsp_ty the bits have the following meaning:
169 * 000b: normal 4/3 only buttons
170 * XX1b: wide (16/9) buttons
171 * X1Xb: letterbox buttons
172 * 1XXb: pan&scan buttons
175 uint16_t hli_ss; /**< status, only low 2 bits 0: no buttons, 1: different 2: equal 3: eual except for button cmds */
176 uint32_t hli_s_ptm; /**< start ptm of hli */
177 uint32_t hli_e_ptm; /**< end ptm of hli */
178 uint32_t btn_se_e_ptm; /**< end ptm of button select */
179 #ifdef WORDS_BIGENDIAN
180 unsigned char zero1 : 2; /**< reserved */
181 unsigned char btngr_ns : 2; /**< number of button groups 1, 2 or 3 with 36/18/12 buttons */
182 unsigned char zero2 : 1; /**< reserved */
183 unsigned char btngr1_dsp_ty : 3; /**< display type of subpic stream for button group 1 */
184 unsigned char zero3 : 1; /**< reserved */
185 unsigned char btngr2_dsp_ty : 3; /**< display type of subpic stream for button group 2 */
186 unsigned char zero4 : 1; /**< reserved */
187 unsigned char btngr3_dsp_ty : 3; /**< display type of subpic stream for button group 3 */
189 unsigned char btngr1_dsp_ty : 3;
190 unsigned char zero2 : 1;
191 unsigned char btngr_ns : 2;
192 unsigned char zero1 : 2;
193 unsigned char btngr3_dsp_ty : 3;
194 unsigned char zero4 : 1;
195 unsigned char btngr2_dsp_ty : 3;
196 unsigned char zero3 : 1;
198 uint8_t btn_ofn; /**< button offset number range 0-255 */
199 uint8_t btn_ns; /**< number of valid buttons <= 36/18/12 (low 6 bits) */
200 uint8_t nsl_btn_ns; /**< number of buttons selectable by U_BTNNi (low 6 bits) nsl_btn_ns <= btn_ns */
201 uint8_t zero5; /**< reserved */
202 uint8_t fosl_btnn; /**< forcedly selected button (low 6 bits) */
203 uint8_t foac_btnn; /**< forcedly activated button (low 6 bits) */
204 } ATTRIBUTE_PACKED hl_gi_t;
208 * Button Color Information Table
209 * Each entry beeing a 32bit word that contains the color indexs and alpha
210 * values to use. They are all represented by 4 bit number and stored
211 * like this [Ci3, Ci2, Ci1, Ci0, A3, A2, A1, A0]. The actual palette
212 * that the indexes reference is in the PGC.
213 * \todo split the uint32_t into a struct
216 uint32_t btn_coli[3][2]; /**< [button color number-1][select:0/action:1] */
217 } ATTRIBUTE_PACKED btn_colit_t;
222 * NOTE: I've had to change the structure from the disk layout to get
223 * the packing to work with Sun's Forte C compiler.
224 * The 4 and 7 bytes are 'rotated' was: ABC DEF GHIJ is: ABCG DEFH IJ
227 #ifdef WORDS_BIGENDIAN
228 uint32 btn_coln : 2; /**< button color number */
229 uint32 x_start : 10; /**< x start offset within the overlay */
230 uint32 zero1 : 2; /**< reserved */
231 uint32 x_end : 10; /**< x end offset within the overlay */
233 uint32 zero3 : 2; /**< reserved */
234 uint32 up : 6; /**< button index when pressing up */
236 uint32 auto_action_mode : 2; /**< 0: no, 1: activated if selected */
237 uint32 y_start : 10; /**< y start offset within the overlay */
238 uint32 zero2 : 2; /**< reserved */
239 uint32 y_end : 10; /**< y end offset within the overlay */
241 uint32 zero4 : 2; /**< reserved */
242 uint32 down : 6; /**< button index when pressing down */
243 unsigned char zero5 : 2; /**< reserved */
244 unsigned char left : 6; /**< button index when pressing left */
245 unsigned char zero6 : 2; /**< reserved */
246 unsigned char right : 6; /**< button index when pressing right */
259 uint32 auto_action_mode : 2;
263 unsigned char left : 6;
264 unsigned char zero5 : 2;
265 unsigned char right : 6;
266 unsigned char zero6 : 2;
269 } ATTRIBUTE_PACKED btni_t;
272 * Highlight Information
276 btn_colit_t btn_colit;
278 } ATTRIBUTE_PACKED hli_t;
285 nsml_agli_t nsml_agli;
288 } ATTRIBUTE_PACKED pci_t;
293 ////////////////////////////////////////
295 class virtual_segment_c;
296 class chapter_item_c;
315 demux_sys_t( demux_t & demux )
322 ,p_current_segment(NULL)
323 ,dvd_interpretor( *this )
327 ,b_pci_packet_set(false)
330 vlc_mutex_init( &lock_demuxer );
333 virtual ~demux_sys_t();
340 mtime_t i_chapter_time;
344 std::vector<input_title_t*> titles; // matroska editions
345 size_t i_current_title;
347 std::vector<matroska_stream_c*> streams;
348 std::vector<attachment_c*> stored_attachments;
349 std::vector<matroska_segment_c*> opened_segments;
350 std::vector<virtual_segment_c*> used_segments;
351 virtual_segment_c *p_current_segment;
353 dvd_command_interpretor_c dvd_interpretor;
355 /* duration of the stream */
358 matroska_segment_c *FindSegment( const EbmlBinary & uid ) const;
359 chapter_item_c *BrowseCodecPrivate( unsigned int codec_id,
360 bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
361 const void *p_cookie,
362 size_t i_cookie_size,
363 virtual_segment_c * & p_segment_found );
364 chapter_item_c *FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found );
366 void PreloadFamily( const matroska_segment_c & of_segment );
367 void PreloadLinked( matroska_segment_c *p_segment );
368 bool PreparePlayback( virtual_segment_c *p_new_segment );
369 matroska_stream_c *AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial = false );
370 void JumpTo( virtual_segment_c & p_segment, chapter_item_c * p_chapter );
372 void StartUiThread();
377 /* for spu variables */
378 input_thread_t *p_input;
380 bool b_pci_packet_set;
381 uint8_t palette[4][4];
382 vlc_mutex_t lock_demuxer;
385 event_thread_t *p_ev;
386 static void * EventThread( vlc_object_t *p_this );
387 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
388 vlc_value_t oldval, vlc_value_t newval, void *p_data );
389 static int EventKey( vlc_object_t *p_this, char const *psz_var,
390 vlc_value_t oldval, vlc_value_t newval, void *p_data );
395 virtual_segment_c *VirtualFromSegments( matroska_segment_c *p_segment ) const;
396 bool IsUsedSegment( matroska_segment_c &p_segment ) const;