]> git.sesse.net Git - vlc/blob - include/vlc_common.h
78ffca901c791f89e2db56c87ab62d2afadf3c3d
[vlc] / include / vlc_common.h
1 /*****************************************************************************
2  * common.h: common definitions
3  * Collection of useful common types and macros definitions
4  *****************************************************************************
5  * Copyright (C) 1998, 1999, 2000 VideoLAN
6  * $Id: vlc_common.h,v 1.15 2002/08/07 21:36:55 massiot Exp $
7  *
8  * Authors: Samuel Hocevar <sam@via.ecp.fr>
9  *          Vincent Seguin <seguin@via.ecp.fr>
10  *          Gildas Bazin <gbazin@netcourrier.com>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  * 
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Compiler-specific workarounds
29  *****************************************************************************/
30 #if defined( __BORLANDC__ )
31 #   undef HAVE_VARIADIC_MACROS
32 #   undef HAVE_STDINT_H
33 #endif
34
35 /*****************************************************************************
36  * Required system headers
37  *****************************************************************************/
38 #ifdef HAVE_STRING_H
39 #   include <string.h>                                         /* strerror() */
40 #endif
41
42 #ifdef HAVE_SYS_TYPES_H
43 #   include <sys/types.h>
44 #endif
45
46 /*****************************************************************************
47  * Basic types definitions
48  *****************************************************************************/
49 #ifdef HAVE_STDINT_H
50 #   include <stdint.h>
51     typedef uint8_t             u8;
52     typedef int8_t              s8;
53
54     typedef uint16_t            u16;
55     typedef int16_t             s16;
56
57     typedef uint32_t            u32;
58     typedef int32_t             s32;
59
60     typedef uint64_t            u64;
61     typedef int64_t             s64;
62 #else
63     typedef unsigned char       u8;
64     typedef signed char         s8;
65
66     typedef unsigned short      u16;
67     typedef signed short        s16;
68
69     typedef unsigned int        u32;
70     typedef signed int          s32;
71
72 #   if defined( _MSC_VER ) || ( defined( WIN32 ) && !defined( __MINGW32__ ) )
73     typedef unsigned __int64    u64;
74     typedef signed __int64      s64;
75 #   else
76     typedef unsigned long long  u64;
77     typedef signed long long    s64;
78 #   endif
79 #endif
80
81 typedef u8                      byte_t;
82
83 /* ptrdiff_t definition */
84 #ifdef HAVE_STDDEF_H
85 #   include <stddef.h>
86 #else
87 #   include <malloc.h>
88 #   ifndef _PTRDIFF_T
89 #       define _PTRDIFF_T
90 /* Not portable in a 64-bit environment. */
91 typedef int                 ptrdiff_t;
92 #   endif
93 #endif
94
95 #if defined( WIN32 )
96 typedef int                 ssize_t;
97 #endif
98
99 /* Counter for statistics and profiling */
100 typedef unsigned long       count_t;
101
102 /* DCT elements types */
103 typedef s16                 dctelem_t;
104
105 /* Video buffer types */
106 typedef u8                  yuv_data_t;
107
108 /*****************************************************************************
109  * mtime_t: high precision date or time interval
110  *****************************************************************************
111  * Store an high precision date or time interval. The maximum precision is the
112  * micro-second, and a 64 bits integer is used to avoid any overflow (maximum
113  * time interval is then 292271 years, which should be long enough for any
114  * video). Date are stored as a time interval since a common date.
115  * Note that date and time intervals can be manipulated using regular
116  * arithmetic operators, and that no special functions are required.
117  *****************************************************************************/
118 typedef s64 mtime_t;
119
120 /*****************************************************************************
121  * The vlc_fourcc_t type.
122  *****************************************************************************
123  * See http://www.webartz.com/fourcc/ for a very detailed list.
124  *****************************************************************************/
125 typedef u32 vlc_fourcc_t;
126
127 #define VLC_FOURCC( a, b, c, d ) \
128     ( ((u32)a) | ( ((u32)b) << 8 ) | ( ((u32)c) << 16 ) | ( ((u32)d) << 24 ) )
129
130 #define VLC_TWOCC( a, b ) \
131     ( (u16)(a) | ( (u16)(b) << 8 ) )
132
133 /*****************************************************************************
134  * Classes declaration
135  *****************************************************************************/
136
137 /* Messages */
138 VLC_DECLARE_STRUCT(msg_bank_t)
139 VLC_DECLARE_STRUCT(msg_subscription_t)
140
141 /* Playlist */
142 VLC_DECLARE_STRUCT(playlist_t)
143 VLC_DECLARE_STRUCT(playlist_item_t)
144
145 /* Modules */
146 VLC_DECLARE_STRUCT(module_bank_t)
147 VLC_DECLARE_STRUCT(module_t)
148 VLC_DECLARE_STRUCT(module_config_t)
149 VLC_DECLARE_STRUCT(module_symbols_t)
150
151 /* Interface */
152 VLC_DECLARE_STRUCT(intf_thread_t)
153 VLC_DECLARE_STRUCT(intf_sys_t)
154 VLC_DECLARE_STRUCT(intf_console_t)
155 VLC_DECLARE_STRUCT(intf_msg_t)
156 VLC_DECLARE_STRUCT(intf_channel_t)
157
158 /* Input */
159 VLC_DECLARE_STRUCT(input_thread_t)
160 VLC_DECLARE_STRUCT(input_channel_t)
161 VLC_DECLARE_STRUCT(input_area_t)
162 VLC_DECLARE_STRUCT(input_buffers_t)
163 VLC_DECLARE_STRUCT(input_socket_t)
164 VLC_DECLARE_STRUCT(input_info_t)
165 VLC_DECLARE_STRUCT(input_info_category_t)
166 VLC_DECLARE_STRUCT(access_sys_t)
167 VLC_DECLARE_STRUCT(demux_sys_t)
168 VLC_DECLARE_STRUCT(es_descriptor_t)
169 VLC_DECLARE_STRUCT(es_sys_t)
170 VLC_DECLARE_STRUCT(pgrm_descriptor_t)
171 VLC_DECLARE_STRUCT(pgrm_sys_t)
172 VLC_DECLARE_STRUCT(stream_descriptor_t)
173 VLC_DECLARE_STRUCT(stream_sys_t)
174
175 /* Audio */
176 VLC_DECLARE_STRUCT(aout_instance_t)
177 VLC_DECLARE_STRUCT(aout_input_t)
178 VLC_DECLARE_STRUCT(aout_buffer_t)
179 VLC_DECLARE_STRUCT(audio_sample_format_t)
180
181 /* Video */
182 VLC_DECLARE_STRUCT(vout_thread_t)
183 VLC_DECLARE_STRUCT(vout_font_t)
184 VLC_DECLARE_STRUCT(vout_sys_t)
185 VLC_DECLARE_STRUCT(chroma_sys_t)
186 VLC_DECLARE_STRUCT(picture_t)
187 VLC_DECLARE_STRUCT(picture_sys_t)
188 VLC_DECLARE_STRUCT(picture_heap_t)
189 VLC_DECLARE_STRUCT(subpicture_t)
190 VLC_DECLARE_STRUCT(subpicture_sys_t)
191
192 /* Decoders */
193 VLC_DECLARE_STRUCT(decoder_fifo_t)
194
195 /* Misc */
196 VLC_DECLARE_STRUCT(data_packet_t)
197 VLC_DECLARE_STRUCT(data_buffer_t)
198 VLC_DECLARE_STRUCT(stream_position_t)
199 VLC_DECLARE_STRUCT(stream_ctrl_t)
200 VLC_DECLARE_STRUCT(pes_packet_t)
201 VLC_DECLARE_STRUCT(bit_stream_t)
202 VLC_DECLARE_STRUCT(network_socket_t)
203 VLC_DECLARE_STRUCT(iso639_lang_t)
204
205 /*****************************************************************************
206  * Plug-in stuff
207  *****************************************************************************/
208 #ifndef __PLUGIN__
209 #   define VLC_EXPORT( type, name, args ) type name args;
210 #else
211 #   define VLC_EXPORT( type, name, args ) ;
212     extern module_symbols_t* p_symbols;
213 #endif
214
215 /*****************************************************************************
216  * OS-specific headers and thread types
217  *****************************************************************************/
218 #if defined( WIN32 )
219 #   define WIN32_LEAN_AND_MEAN
220 #   include <windows.h>
221 #endif
222
223 #include "vlc_threads.h"
224
225 /*****************************************************************************
226  * Common structure members
227  *****************************************************************************/
228
229 /* VLC_COMMON_MEMBERS : members common to all basic vlc objects */
230 #define VLC_COMMON_MEMBERS                                                  \
231     int   i_object_id;                                                      \
232     int   i_object_type;                                                    \
233     char *psz_object_type;                                                  \
234     char *psz_object_name;                                                  \
235                                                                             \
236     /* Thread properties, if any */                                         \
237     vlc_bool_t   b_thread;                                                  \
238     vlc_thread_t thread_id;                                                 \
239                                                                             \
240     /* Object access lock */                                                \
241     vlc_mutex_t  object_lock;                                               \
242     vlc_cond_t   object_wait;                                               \
243                                                                             \
244     volatile vlc_bool_t b_error;                    /* set by the object */ \
245     volatile vlc_bool_t b_die;                     /* set by the outside */ \
246     volatile vlc_bool_t b_dead;                     /* set by the object */ \
247                                                                             \
248     vlc_t *         p_vlc;                           /* root of all evil */ \
249                                                                             \
250     volatile int    i_refcount;                                             \
251     vlc_object_t ** pp_parents;                           /* our parents */ \
252     volatile int    i_parents;                                              \
253     vlc_object_t ** pp_children;                         /* our children */ \
254     volatile int    i_children;                                             \
255                                                                             \
256     /* Private data */                                                      \
257     void *          p_private;                                              \
258                                                                             \
259     /* Just a reminder so that people don't cast garbage */                 \
260     int be_sure_to_add_VLC_COMMON_MEMBERS_to_struct;                        \
261
262 /* The real vlc_object_t type. Yes, it's that simple :-) */
263 struct vlc_object_t
264 {
265     VLC_COMMON_MEMBERS
266 };
267
268 /* VLC_OBJECT: attempt at doing a clever cast */
269 #define VLC_OBJECT( x ) \
270     ((vlc_object_t *)(x))+0*(x)->be_sure_to_add_VLC_COMMON_MEMBERS_to_struct
271
272 /*****************************************************************************
273  * Macros and inline functions
274  *****************************************************************************/
275 #ifdef NTOHL_IN_SYS_PARAM_H
276 #   include <sys/param.h>
277
278 #elif !defined(WIN32) /* NTOHL_IN_SYS_PARAM_H || WIN32 */
279 #   include <netinet/in.h>
280
281 #endif /* NTOHL_IN_SYS_PARAM_H || WIN32 */
282
283 /* CEIL: division with round to nearest greater integer */
284 #define CEIL(n, d)  ( ((n) / (d)) + ( ((n) % (d)) ? 1 : 0) )
285
286 /* PAD: PAD(n, d) = CEIL(n ,d) * d */
287 #define PAD(n, d)   ( ((n) % (d)) ? ((((n) / (d)) + 1) * (d)) : (n) )
288
289 /* __MAX and __MIN: self explanatory */
290 #ifndef __MAX
291 #   define __MAX(a, b)   ( ((a) > (b)) ? (a) : (b) )
292 #endif
293 #ifndef __MIN
294 #   define __MIN(a, b)   ( ((a) < (b)) ? (a) : (b) )
295 #endif
296
297 /* MSB (big endian)/LSB (little endian) conversions - network order is always
298  * MSB, and should be used for both network communications and files. Note that
299  * byte orders other than little and big endians are not supported, but only
300  * the VAX seems to have such exotic properties. */
301 static inline u16 U16_AT( void * _p )
302 {
303     u8 * p = (u8 *)_p;
304     return ( ((u16)p[0] << 8) | p[1] );
305 }
306 static inline u32 U32_AT( void * _p )
307 {
308     u8 * p = (u8 *)_p;
309     return ( ((u32)p[0] << 24) | ((u32)p[1] << 16) | ((u32)p[2] << 8) | p[3] );
310 }
311 static inline u64 U64_AT( void * _p )
312 {
313     u8 * p = (u8 *)_p;
314     return ( ((u64)p[0] << 56) | ((u64)p[1] << 48) | ((u64)p[2] << 40) 
315               | ((u64)p[3] << 32) | ((u64)p[4] << 24) | ((u64)p[5] << 16)
316               | ((u64)p[6] << 8) | p[7] );
317 }
318 #if WORDS_BIGENDIAN
319 #   define hton16(i)   ( i )
320 #   define hton32(i)   ( i )
321 #   define hton64(i)   ( i )
322 #   define ntoh16(i)   ( i )
323 #   define ntoh32(i)   ( i )
324 #   define ntoh64(i)   ( i )
325 #else
326 #   define hton16(i)   U16_AT(&i)
327 #   define hton32(i)   U32_AT(&i)
328 #   define hton64(i)   U64_AT(&i)
329 #   define ntoh16(i)   U16_AT(&i)
330 #   define ntoh32(i)   U32_AT(&i)
331 #   define ntoh64(i)   U64_AT(&i)
332 #endif
333
334 /* Alignment of critical static data structures */
335 #ifdef ATTRIBUTE_ALIGNED_MAX
336 #   define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
337 #else
338 #   define ATTR_ALIGN(align)
339 #endif
340
341 /* Alignment of critical dynamic data structure
342  *
343  * Not all platforms support memalign so we provide a vlc_memalign wrapper
344  * void *vlc_memalign( size_t align, size_t size, void **pp_orig )
345  * *pp_orig is the pointer that has to be freed afterwards.
346  */
347 #if 0
348 #ifdef HAVE_POSIX_MEMALIGN
349 #   define vlc_memalign(align,size,pp_orig) \
350     ( !posix_memalign( pp_orig, align, size ) ? *(pp_orig) : NULL )
351 #endif
352 #endif
353 #ifdef HAVE_MEMALIGN
354     /* Some systems have memalign() but no declaration for it */
355     void * memalign( size_t align, size_t size );
356
357 #   define vlc_memalign(pp_orig,align,size) \
358     ( *(pp_orig) = memalign( align, size ) )
359
360 #else /* We don't have any choice but to align manually */
361 #   define vlc_memalign(pp_orig,align,size) \
362     (( *(pp_orig) = malloc( size + align - 1 )) \
363         ? (void *)( (((unsigned long)*(pp_orig)) + (unsigned long)(align-1) ) \
364                        & (~(unsigned long)(align-1)) ) \
365         : NULL )
366
367 #endif
368
369 /* strndup (defined in src/misc/extras.c) */
370 #ifndef HAVE_STRNDUP
371 char * strndup( const char *s, size_t n );
372 #endif
373
374
375 #define I64C(x)         x##LL
376
377 #ifdef WIN32
378 /* win32, cl and icl support */
379 #   if defined( _MSC_VER ) || !defined( __MINGW32__ )
380 #       define __attribute__(x)
381 #       define __inline__      __inline
382 #       define strncasecmp     strnicmp
383 #       define strcasecmp      stricmp
384 #       define S_IFBLK         0x3000  /* Block */
385 #       define S_ISBLK(m)      (0)
386 #       define S_ISCHR(m)      (0)
387 #       define S_ISFIFO(m)     (((m)&_S_IFMT) == _S_IFIFO)
388 #       define S_ISREG(m)      (((m)&_S_IFMT) == _S_IFREG)
389 #       undef I64C
390 #       define I64C(x)         x##i64
391 #   endif
392
393 /* several type definitions */
394 #   if defined( __MINGW32__ )
395 #       if !defined( _OFF_T_ )
396 typedef long long _off_t;
397 typedef _off_t off_t;
398 #           define _OFF_T_
399 #       else
400 #           define off_t long long
401 #       endif
402 #   endif
403
404 #   if defined( _MSC_VER )
405 #       if !defined( _OFF_T_DEFINED )
406 typedef __int64 off_t;
407 #           define _OFF_T_DEFINED
408 #       else
409 #           define off_t __int64
410 #       endif
411 #   endif
412
413 #   if defined( __BORLANDC__ )
414 #       undef off_t
415 #       define off_t unsigned __int64
416 #   endif
417
418 #   ifndef O_NONBLOCK
419 #       define O_NONBLOCK 0
420 #   endif
421
422 #   ifndef snprintf
423 #       define snprintf _snprintf  /* snprintf not defined in mingw32 (bug?) */
424 #   endif
425
426 #endif
427
428 /*****************************************************************************
429  * CPU capabilities
430  *****************************************************************************/
431 #define CPU_CAPABILITY_NONE    0
432 #define CPU_CAPABILITY_486     (1<<0)
433 #define CPU_CAPABILITY_586     (1<<1)
434 #define CPU_CAPABILITY_PPRO    (1<<2)
435 #define CPU_CAPABILITY_MMX     (1<<3)
436 #define CPU_CAPABILITY_3DNOW   (1<<4)
437 #define CPU_CAPABILITY_MMXEXT  (1<<5)
438 #define CPU_CAPABILITY_SSE     (1<<6)
439 #define CPU_CAPABILITY_ALTIVEC (1<<16)
440 #define CPU_CAPABILITY_FPU     (1<<31)
441
442 /*****************************************************************************
443  * I18n stuff
444  *****************************************************************************/
445 #ifndef PACKAGE /* Borland C++ uses this ! */
446 #define PACKAGE VLC_PACKAGE
447 #endif
448 #define VERSION VLC_VERSION
449
450 #if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT ) && !defined( __BORLANDC__ ) && !defined( MODULE_NAME_IS_gnome )
451 #   include <libintl.h>
452 #   undef _
453 #   define _(String) dgettext (PACKAGE, String)
454 #   ifdef gettext_noop
455 #       define N_(String) gettext_noop (String)
456 #   else
457 #       define N_(String) (String)
458 #   endif
459 #elif !defined( MODULE_NAME_IS_gnome )
460 #   define _(String) (String)
461 #   define N_(String) (String)
462 #endif
463
464 /*****************************************************************************
465  * Plug-in stuff
466  *****************************************************************************/
467 #include "vlc_symbols.h"