]> git.sesse.net Git - vlc/blob - modules/demux/mkv/demux.hpp
Fix crash in invalid mkv files
[vlc] / modules / demux / mkv / demux.hpp
1 /*****************************************************************************
2  * mkv.cpp : matroska demuxer
3  *****************************************************************************
4  * Copyright (C) 2003-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *          Steve Lhomme <steve.lhomme@free.fr>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifndef _DEMUX_SYS_H
26 #define _DEMUX_SYS_H
27
28 #include "mkv.hpp"
29
30 #include "chapter_command.hpp"
31 #include "virtual_segment.hpp"
32
33 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////
34 #undef ATTRIBUTE_PACKED
35 #undef PRAGMA_PACK_BEGIN
36 #undef PRAGMA_PACK_END
37
38 #if defined(__GNUC__)
39 #define ATTRIBUTE_PACKED __attribute__ ((packed))
40 #define PRAGMA_PACK 0
41 #endif
42
43 #if !defined(ATTRIBUTE_PACKED)
44 #define ATTRIBUTE_PACKED
45 #define PRAGMA_PACK 1
46 #endif
47
48 #if PRAGMA_PACK
49 #pragma pack(1)
50 #endif
51
52 /*************************************
53 *  taken from libdvdnav / libdvdread
54 **************************************/
55
56 /**
57  * DVD Time Information.
58  */
59 typedef struct {
60   uint8_t hour;
61   uint8_t minute;
62   uint8_t second;
63   uint8_t frame_u; /* The two high bits are the frame rate. */
64 } ATTRIBUTE_PACKED dvd_time_t;
65
66 /**
67  * User Operations.
68  */
69 typedef struct {
70 #ifdef WORDS_BIGENDIAN
71   unsigned char zero                           : 7; /* 25-31 */
72   unsigned char video_pres_mode_change         : 1; /* 24 */
73  
74   unsigned char karaoke_audio_pres_mode_change : 1; /* 23 */
75   unsigned char angle_change                   : 1;
76   unsigned char subpic_stream_change           : 1;
77   unsigned char audio_stream_change            : 1;
78   unsigned char pause_on                       : 1;
79   unsigned char still_off                      : 1;
80   unsigned char button_select_or_activate      : 1;
81   unsigned char resume                         : 1; /* 16 */
82  
83   unsigned char chapter_menu_call              : 1; /* 15 */
84   unsigned char angle_menu_call                : 1;
85   unsigned char audio_menu_call                : 1;
86   unsigned char subpic_menu_call               : 1;
87   unsigned char root_menu_call                 : 1;
88   unsigned char title_menu_call                : 1;
89   unsigned char backward_scan                  : 1;
90   unsigned char forward_scan                   : 1; /* 8 */
91  
92   unsigned char next_pg_search                 : 1; /* 7 */
93   unsigned char prev_or_top_pg_search          : 1;
94   unsigned char time_or_chapter_search         : 1;
95   unsigned char go_up                          : 1;
96   unsigned char stop                           : 1;
97   unsigned char title_play                     : 1;
98   unsigned char chapter_search_or_play         : 1;
99   unsigned char title_or_time_play             : 1; /* 0 */
100 #else
101   unsigned char video_pres_mode_change         : 1; /* 24 */
102   unsigned char zero                           : 7; /* 25-31 */
103  
104   unsigned char resume                         : 1; /* 16 */
105   unsigned char button_select_or_activate      : 1;
106   unsigned char still_off                      : 1;
107   unsigned char pause_on                       : 1;
108   unsigned char audio_stream_change            : 1;
109   unsigned char subpic_stream_change           : 1;
110   unsigned char angle_change                   : 1;
111   unsigned char karaoke_audio_pres_mode_change : 1; /* 23 */
112  
113   unsigned char forward_scan                   : 1; /* 8 */
114   unsigned char backward_scan                  : 1;
115   unsigned char title_menu_call                : 1;
116   unsigned char root_menu_call                 : 1;
117   unsigned char subpic_menu_call               : 1;
118   unsigned char audio_menu_call                : 1;
119   unsigned char angle_menu_call                : 1;
120   unsigned char chapter_menu_call              : 1; /* 15 */
121  
122   unsigned char title_or_time_play             : 1; /* 0 */
123   unsigned char chapter_search_or_play         : 1;
124   unsigned char title_play                     : 1;
125   unsigned char stop                           : 1;
126   unsigned char go_up                          : 1;
127   unsigned char time_or_chapter_search         : 1;
128   unsigned char prev_or_top_pg_search          : 1;
129   unsigned char next_pg_search                 : 1; /* 7 */
130 #endif
131 } ATTRIBUTE_PACKED user_ops_t;
132
133 /**
134  * Type to store per-command data.
135  */
136 typedef struct {
137   uint8_t bytes[8];
138 } ATTRIBUTE_PACKED vm_cmd_t;
139 #define COMMAND_DATA_SIZE 8
140
141 /**
142  * PCI General Information
143  */
144 typedef struct {
145   uint32_t nv_pck_lbn;      /**< sector address of this nav pack */
146   uint16_t vobu_cat;        /**< 'category' of vobu */
147   uint16_t zero1;           /**< reserved */
148   user_ops_t vobu_uop_ctl;  /**< UOP of vobu */
149   uint32_t vobu_s_ptm;      /**< start presentation time of vobu */
150   uint32_t vobu_e_ptm;      /**< end presentation time of vobu */
151   uint32_t vobu_se_e_ptm;   /**< end ptm of sequence end in vobu */
152   dvd_time_t e_eltm;        /**< Cell elapsed time */
153   char vobu_isrc[32];
154 } ATTRIBUTE_PACKED pci_gi_t;
155
156 /**
157  * Non Seamless Angle Information
158  */
159 typedef struct {
160   uint32_t nsml_agl_dsta[9];  /**< address of destination vobu in AGL_C#n */
161 } ATTRIBUTE_PACKED nsml_agli_t;
162
163 /**
164  * Highlight General Information
165  *
166  * For btngrX_dsp_ty the bits have the following meaning:
167  * 000b: normal 4/3 only buttons
168  * XX1b: wide (16/9) buttons
169  * X1Xb: letterbox buttons
170  * 1XXb: pan&scan buttons
171  */
172 typedef struct {
173   uint16_t hli_ss; /**< status, only low 2 bits 0: no buttons, 1: different 2: equal 3: eual except for button cmds */
174   uint32_t hli_s_ptm;              /**< start ptm of hli */
175   uint32_t hli_e_ptm;              /**< end ptm of hli */
176   uint32_t btn_se_e_ptm;           /**< end ptm of button select */
177 #ifdef WORDS_BIGENDIAN
178   unsigned char zero1 : 2;          /**< reserved */
179   unsigned char btngr_ns : 2;       /**< number of button groups 1, 2 or 3 with 36/18/12 buttons */
180   unsigned char zero2 : 1;          /**< reserved */
181   unsigned char btngr1_dsp_ty : 3;  /**< display type of subpic stream for button group 1 */
182   unsigned char zero3 : 1;          /**< reserved */
183   unsigned char btngr2_dsp_ty : 3;  /**< display type of subpic stream for button group 2 */
184   unsigned char zero4 : 1;          /**< reserved */
185   unsigned char btngr3_dsp_ty : 3;  /**< display type of subpic stream for button group 3 */
186 #else
187   unsigned char btngr1_dsp_ty : 3;
188   unsigned char zero2 : 1;
189   unsigned char btngr_ns : 2;
190   unsigned char zero1 : 2;
191   unsigned char btngr3_dsp_ty : 3;
192   unsigned char zero4 : 1;
193   unsigned char btngr2_dsp_ty : 3;
194   unsigned char zero3 : 1;
195 #endif
196   uint8_t btn_ofn;     /**< button offset number range 0-255 */
197   uint8_t btn_ns;      /**< number of valid buttons  <= 36/18/12 (low 6 bits) */
198   uint8_t nsl_btn_ns;  /**< number of buttons selectable by U_BTNNi (low 6 bits)   nsl_btn_ns <= btn_ns */
199   uint8_t zero5;       /**< reserved */
200   uint8_t fosl_btnn;   /**< forcedly selected button  (low 6 bits) */
201   uint8_t foac_btnn;   /**< forcedly activated button (low 6 bits) */
202 } ATTRIBUTE_PACKED hl_gi_t;
203
204
205 /**
206  * Button Color Information Table
207  * Each entry beeing a 32bit word that contains the color indexs and alpha
208  * values to use.  They are all represented by 4 bit number and stored
209  * like this [Ci3, Ci2, Ci1, Ci0, A3, A2, A1, A0].   The actual palette
210  * that the indexes reference is in the PGC.
211  * \todo split the uint32_t into a struct
212  */
213 typedef struct {
214   uint32_t btn_coli[3][2];  /**< [button color number-1][select:0/action:1] */
215 } ATTRIBUTE_PACKED btn_colit_t;
216
217 /**
218  * Button Information
219  *
220  * NOTE: I've had to change the structure from the disk layout to get
221  * the packing to work with Sun's Forte C compiler.
222  * The 4 and 7 bytes are 'rotated' was: ABC DEF GHIJ  is: ABCG DEFH IJ
223  */
224 typedef struct {
225 #ifdef WORDS_BIGENDIAN
226   uint32        btn_coln         : 2;  /**< button color number */
227   uint32        x_start          : 10; /**< x start offset within the overlay */
228   uint32        zero1            : 2;  /**< reserved */
229   uint32        x_end            : 10; /**< x end offset within the overlay */
230
231   uint32        zero3            : 2;  /**< reserved */
232   uint32        up               : 6;  /**< button index when pressing up */
233
234   uint32        auto_action_mode : 2;  /**< 0: no, 1: activated if selected */
235   uint32        y_start          : 10; /**< y start offset within the overlay */
236   uint32        zero2            : 2;  /**< reserved */
237   uint32        y_end            : 10; /**< y end offset within the overlay */
238
239   uint32        zero4            : 2;  /**< reserved */
240   uint32        down             : 6;  /**< button index when pressing down */
241   unsigned char zero5            : 2;  /**< reserved */
242   unsigned char left             : 6;  /**< button index when pressing left */
243   unsigned char zero6            : 2;  /**< reserved */
244   unsigned char right            : 6;  /**< button index when pressing right */
245 #else
246   uint32        x_end            : 10;
247   uint32        zero1            : 2;
248   uint32        x_start          : 10;
249   uint32        btn_coln         : 2;
250
251   uint32        up               : 6;
252   uint32        zero3            : 2;
253
254   uint32        y_end            : 10;
255   uint32        zero2            : 2;
256   uint32        y_start          : 10;
257   uint32        auto_action_mode : 2;
258
259   uint32        down             : 6;
260   uint32        zero4            : 2;
261   unsigned char left             : 6;
262   unsigned char zero5            : 2;
263   unsigned char right            : 6;
264   unsigned char zero6            : 2;
265 #endif
266   vm_cmd_t cmd;
267 } ATTRIBUTE_PACKED btni_t;
268
269 /**
270  * Highlight Information
271  */
272 typedef struct {
273   hl_gi_t     hl_gi;
274   btn_colit_t btn_colit;
275   btni_t      btnit[36];
276 } ATTRIBUTE_PACKED hli_t;
277
278 /**
279  * PCI packet
280  */
281 typedef struct {
282   pci_gi_t    pci_gi;
283   nsml_agli_t nsml_agli;
284   hli_t       hli;
285   uint8_t     zero1[189];
286 } ATTRIBUTE_PACKED pci_t;
287
288 #if PRAGMA_PACK
289 #pragma pack()
290 #endif
291 ////////////////////////////////////////
292
293 class virtual_segment_c;
294 class chapter_item_c;
295
296 class event_thread_t
297 {
298 public:
299     event_thread_t(demux_t *);
300     virtual ~event_thread_t();
301
302     void SetPci(const pci_t *data);
303     void ResetPci();
304
305 private:
306     void EventThread();
307     static void *EventThread(void *);
308
309     static int EventMouse( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
310     static int EventKey( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
311     static int EventInput( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
312
313     demux_t      *p_demux;
314
315     bool         is_running;
316     vlc_thread_t thread;
317
318     vlc_mutex_t  lock;
319     vlc_cond_t   wait;
320     bool         b_abort;
321     bool         b_moved;
322     bool         b_clicked;
323     int          i_key_action;
324     bool         b_vout;
325     pci_t        pci_packet;
326 };
327
328
329 class demux_sys_t
330 {
331 public:
332     demux_sys_t( demux_t & demux )
333         :demuxer(demux)
334         ,i_pts(0)
335         ,i_start_pts(0)
336         ,i_chapter_time(0)
337         ,meta(NULL)
338         ,i_current_title(0)
339         ,p_current_segment(NULL)
340         ,dvd_interpretor( *this )
341         ,f_duration(-1.0)
342         ,p_input(NULL)
343         ,p_ev(NULL)
344     {
345         vlc_mutex_init( &lock_demuxer );
346     }
347
348     virtual ~demux_sys_t();
349
350     /* current data */
351     demux_t                 & demuxer;
352
353     mtime_t                 i_pts;
354     mtime_t                 i_start_pts;
355     mtime_t                 i_chapter_time;
356
357     vlc_meta_t              *meta;
358
359     std::vector<input_title_t*>      titles; // matroska editions
360     size_t                           i_current_title;
361
362     std::vector<matroska_stream_c*>  streams;
363     std::vector<attachment_c*>       stored_attachments;
364     std::vector<matroska_segment_c*> opened_segments;
365     std::vector<virtual_segment_c*>  used_segments;
366     virtual_segment_c                *p_current_segment;
367
368     dvd_command_interpretor_c        dvd_interpretor;
369
370     /* duration of the stream */
371     float                   f_duration;
372
373     matroska_segment_c *FindSegment( const EbmlBinary & uid ) const;
374     virtual_chapter_c *BrowseCodecPrivate( unsigned int codec_id,
375                                         bool (*match)(const chapter_codec_cmds_c &data, const void *p_cookie, size_t i_cookie_size ),
376                                         const void *p_cookie,
377                                         size_t i_cookie_size,
378                                         virtual_segment_c * & p_segment_found );
379     virtual_chapter_c *FindChapter( int64_t i_find_uid, virtual_segment_c * & p_segment_found );
380
381     void PreloadFamily( const matroska_segment_c & of_segment );
382     bool PreloadLinked();
383     bool PreparePlayback( virtual_segment_c *p_new_segment );
384     matroska_stream_c *AnalyseAllSegmentsFound( demux_t *p_demux, EbmlStream *p_estream, bool b_initial = false );
385     void JumpTo( virtual_segment_c & p_segment, virtual_chapter_c * p_chapter );
386
387     void InitUi();
388     void CleanUi();
389
390     /* for spu variables */
391     input_thread_t *p_input;
392     uint8_t        palette[4][4];
393     vlc_mutex_t    lock_demuxer;
394
395     /* event */
396     event_thread_t *p_ev;
397
398 protected:
399     virtual_segment_c *VirtualFromSegments( std::vector<matroska_segment_c*> *p_segments ) const;
400 };
401
402
403 #endif