* ./src/misc/beos_specific.cpp: BeOS fixes, removed a static variable.
[vlc.git] / 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.2 2002/06/01 14:31:32 sam 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  * Required system headers
29  *****************************************************************************/
30 #ifdef HAVE_STRING_H
31 #   include <string.h>                                         /* strerror() */
32 #endif
33
34 #ifdef HAVE_SYS_TYPES_H
35 #   include <sys/types.h>
36 #endif
37
38 /*****************************************************************************
39  * Basic types definitions
40  *****************************************************************************/
41 #ifdef HAVE_STDINT_H
42 #   include <stdint.h>
43     typedef uint8_t             u8;
44     typedef int8_t              s8;
45
46     typedef uint16_t            u16;
47     typedef int16_t             s16;
48
49     typedef uint32_t            u32;
50     typedef int32_t             s32;
51
52     typedef uint64_t            u64;
53     typedef int64_t             s64;
54 #else
55     typedef unsigned char       u8;
56     typedef signed char         s8;
57
58     typedef unsigned short      u16;
59     typedef signed short        s16;
60
61     typedef unsigned int        u32;
62     typedef signed int          s32;
63
64 #   if defined( _MSC_VER ) || ( defined( WIN32 ) && !defined( __MINGW32__ ) )
65     typedef unsigned __int64    u64;
66     typedef signed __int64      s64;
67 #   else
68     typedef unsigned long long  u64;
69     typedef signed long long    s64;
70 #   endif
71 #endif
72
73 typedef u8                      byte_t;
74
75 /* ptrdiff_t definition */
76 #ifdef HAVE_STDDEF_H
77 #   include <stddef.h>
78 #else
79 #   include <malloc.h>
80 #   ifndef _PTRDIFF_T
81 #       define _PTRDIFF_T
82 /* Not portable in a 64-bit environment. */
83 typedef int                 ptrdiff_t;
84 #   endif
85 #endif
86
87 #if defined( WIN32 )
88 typedef int                 ssize_t;
89 #endif
90
91 /* Counter for statistics and profiling */
92 typedef unsigned long       count_t;
93
94 /* DCT elements types */
95 typedef s16                 dctelem_t;
96
97 /* Video buffer types */
98 typedef u8                  yuv_data_t;
99
100 /*****************************************************************************
101  * mtime_t: high precision date or time interval
102  *****************************************************************************
103  * Store an high precision date or time interval. The maximum precision is the
104  * micro-second, and a 64 bits integer is used to avoid any overflow (maximum
105  * time interval is then 292271 years, which should be long enough for any
106  * video). Date are stored as a time interval since a common date.
107  * Note that date and time intervals can be manipulated using regular
108  * arithmetic operators, and that no special functions are required.
109  *****************************************************************************/
110 typedef s64 mtime_t;
111
112 /*****************************************************************************
113  * Classes declaration
114  *****************************************************************************/
115
116 /* Messages */
117 VLC_DECLARE_STRUCT(msg_bank)
118 VLC_DECLARE_STRUCT(msg_subscription)
119
120 /* Playlist */
121 VLC_DECLARE_STRUCT(playlist)
122 VLC_DECLARE_STRUCT(playlist_item)
123
124 /* Modules */
125 VLC_DECLARE_STRUCT(module_bank)
126 VLC_DECLARE_STRUCT(module)
127 VLC_DECLARE_STRUCT(module_config)
128 VLC_DECLARE_STRUCT(module_symbols)
129 VLC_DECLARE_STRUCT(module_functions)
130
131 /* Interface */
132 VLC_DECLARE_STRUCT(intf_thread)
133 VLC_DECLARE_STRUCT(intf_sys)
134 VLC_DECLARE_STRUCT(intf_console)
135 VLC_DECLARE_STRUCT(intf_msg)
136 VLC_DECLARE_STRUCT(intf_channel)
137
138 /* Input */
139 VLC_DECLARE_STRUCT(input_thread)
140 VLC_DECLARE_STRUCT(input_channel)
141 VLC_DECLARE_STRUCT(input_cfg)
142 VLC_DECLARE_STRUCT(input_area)
143 VLC_DECLARE_STRUCT(input_buffers)
144 VLC_DECLARE_STRUCT(input_socket)
145
146 /* Audio */
147 VLC_DECLARE_STRUCT(aout_thread)
148 VLC_DECLARE_STRUCT(aout_sys)
149 VLC_DECLARE_STRUCT(aout_fifo)
150
151 /* Video */
152 VLC_DECLARE_STRUCT(vout_thread)
153 VLC_DECLARE_STRUCT(vout_font)
154 VLC_DECLARE_STRUCT(vout_sys)
155 VLC_DECLARE_STRUCT(chroma_sys)
156 VLC_DECLARE_STRUCT(picture)
157 VLC_DECLARE_STRUCT(picture_sys)
158 VLC_DECLARE_STRUCT(picture_heap)
159 VLC_DECLARE_STRUCT(subpicture)
160 VLC_DECLARE_STRUCT(subpicture_sys)
161
162 /* Decoders */
163 VLC_DECLARE_STRUCT(decoder_fifo)
164
165 /* Misc */
166 VLC_DECLARE_STRUCT(macroblock)
167 VLC_DECLARE_STRUCT(data_packet)
168 VLC_DECLARE_STRUCT(data_buffer)
169 VLC_DECLARE_STRUCT(downmix)
170 VLC_DECLARE_STRUCT(imdct)
171 VLC_DECLARE_STRUCT(complex)
172 VLC_DECLARE_STRUCT(dm_par)
173 VLC_DECLARE_STRUCT(es_descriptor)
174 VLC_DECLARE_STRUCT(pgrm_descriptor)
175 VLC_DECLARE_STRUCT(stream_descriptor)
176 VLC_DECLARE_STRUCT(stream_position)
177 VLC_DECLARE_STRUCT(stream_ctrl)
178 VLC_DECLARE_STRUCT(pes_packet)
179 VLC_DECLARE_STRUCT(bit_stream)
180 VLC_DECLARE_STRUCT(network_socket)
181 VLC_DECLARE_STRUCT(iso639_lang)
182
183 /*****************************************************************************
184  * OS-specific headers and thread types
185  *****************************************************************************/
186 #if defined( WIN32 )
187 #   define WIN32_LEAN_AND_MEAN
188 #   include <windows.h>
189     typedef BOOL (WINAPI *SIGNALOBJECTANDWAIT)( HANDLE, HANDLE, DWORD, BOOL );
190 #endif
191
192 #include "vlc_threads.h"
193
194 /*****************************************************************************
195  * Compiler-specific workarounds
196  *****************************************************************************/
197 #if defined( __BORLANDC__ )
198 #   undef HAVE_VARIADIC_MACROS
199 #endif
200
201 /*****************************************************************************
202  * Common structure members
203  *****************************************************************************/
204
205 /* VLC_COMMON_MEMBERS : members common to all basic vlc objects */
206 #define VLC_COMMON_MEMBERS                                                  \
207     int   i_object_id;                                                      \
208     int   i_object_type;                                                    \
209     char *psz_object_type;                                                  \
210     char *psz_object_name;                                                  \
211                                                                             \
212     /* Thread properties, if any */                                         \
213     vlc_bool_t   b_thread;                                                  \
214     vlc_thread_t thread_id;                                                 \
215     vlc_mutex_t  thread_lock;                                               \
216     vlc_cond_t   thread_wait;                                               \
217                                                                             \
218     volatile vlc_bool_t b_error;                    /* set by the object */ \
219     volatile vlc_bool_t b_die;                     /* set by the outside */ \
220     volatile vlc_bool_t b_dead;                     /* set by the object */ \
221                                                                             \
222     vlc_object_t *  p_this;                    /* a pointer to ourselves */ \
223     vlc_t *         p_vlc;                           /* root of all evil */ \
224                                                                             \
225     volatile int    i_refcount;                                             \
226     vlc_object_t ** pp_parents;                           /* our parents */ \
227     volatile int    i_parents;                                              \
228     vlc_object_t ** pp_children;                         /* our children */ \
229     volatile int    i_children;                                             \
230                                                                             \
231     /* Just a reminder so that people don't cast garbage */                 \
232     int be_sure_to_add_VLC_COMMON_MEMBERS_to_struct;                        \
233
234 /* The real vlc_object_t type. Yes, it's that simple :-) */
235 struct vlc_object_s
236 {
237     VLC_COMMON_MEMBERS
238 };
239
240 /* CAST_TO_VLC_OBJECT: attempt at doing a clever cast */
241 #define CAST_TO_VLC_OBJECT( x ) \
242     ((vlc_object_t *)(x))+0*(x)->be_sure_to_add_VLC_COMMON_MEMBERS_to_struct
243
244 /*****************************************************************************
245  * Macros and inline functions
246  *****************************************************************************/
247 #ifdef NTOHL_IN_SYS_PARAM_H
248 #   include <sys/param.h>
249
250 #elif defined(WIN32)
251 /* Swap bytes in 16 bit value.  */
252 #   define __bswap_constant_16(x) \
253      ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
254
255 #   if defined __GNUC__ && __GNUC__ >= 2
256 #       define __bswap_16(x) \
257             (__extension__                                                    \
258              ({ register unsigned short int __v;                              \
259                 if (__builtin_constant_p (x))                                 \
260                   __v = __bswap_constant_16 (x);                              \
261                 else                                                          \
262                   __asm__ __volatile__ ("rorw $8, %w0"                        \
263                                         : "=r" (__v)                          \
264                                         : "0" ((unsigned short int) (x))      \
265                                         : "cc");                              \
266                 __v; }))
267 #   else
268 /* This is better than nothing.  */
269 #       define __bswap_16(x) __bswap_constant_16 (x)
270 #   endif
271
272 /* Swap bytes in 32 bit value.  */
273 #   define __bswap_constant_32(x) \
274         ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |            \
275          (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
276
277 #   if defined __GNUC__ && __GNUC__ >= 2
278 /* To swap the bytes in a word the i486 processors and up provide the
279    `bswap' opcode.  On i386 we have to use three instructions.  */
280 #       if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__
281 #           define __bswap_32(x) \
282                 (__extension__                                                \
283                  ({ register unsigned int __v;                                \
284                     if (__builtin_constant_p (x))                             \
285                       __v = __bswap_constant_32 (x);                          \
286                     else                                                      \
287                       __asm__ __volatile__ ("rorw $8, %w0;"                   \
288                                             "rorl $16, %0;"                   \
289                                             "rorw $8, %w0"                    \
290                                             : "=r" (__v)                      \
291                                             : "0" ((unsigned int) (x))        \
292                                             : "cc");                          \
293                     __v; }))
294 #       else
295 #           define __bswap_32(x) \
296                 (__extension__                                                \
297                  ({ register unsigned int __v;                                \
298                     if (__builtin_constant_p (x))                             \
299                       __v = __bswap_constant_32 (x);                          \
300                     else                                                      \
301                       __asm__ __volatile__ ("bswap %0"                        \
302                                             : "=r" (__v)                      \
303                                             : "0" ((unsigned int) (x)));      \
304                     __v; }))
305 #       endif
306 #   else
307 #       define __bswap_32(x) __bswap_constant_32 (x)
308 #   endif
309
310 #   if defined __GNUC__ && __GNUC__ >= 2
311 /* Swap bytes in 64 bit value.  */
312 #       define __bswap_constant_64(x) \
313             ((((x) & 0xff00000000000000ull) >> 56)                            \
314              | (((x) & 0x00ff000000000000ull) >> 40)                          \
315              | (((x) & 0x0000ff0000000000ull) >> 24)                          \
316              | (((x) & 0x000000ff00000000ull) >> 8)                           \
317              | (((x) & 0x00000000ff000000ull) << 8)                           \
318              | (((x) & 0x0000000000ff0000ull) << 24)                          \
319              | (((x) & 0x000000000000ff00ull) << 40)                          \
320              | (((x) & 0x00000000000000ffull) << 56))
321
322 #       define __bswap_64(x) \
323             (__extension__                                                    \
324              ({ union { __extension__ unsigned long long int __ll;            \
325                         unsigned long int __l[2]; } __w, __r;                 \
326                 if (__builtin_constant_p (x))                                 \
327                   __r.__ll = __bswap_constant_64 (x);                         \
328                 else                                                          \
329                   {                                                           \
330                     __w.__ll = (x);                                           \
331                     __r.__l[0] = __bswap_32 (__w.__l[1]);                     \
332                     __r.__l[1] = __bswap_32 (__w.__l[0]);                     \
333                   }                                                           \
334                 __r.__ll; }))
335 #   else
336 #       define __bswap_64(i) \
337             (u64)((__bswap_32((i) & 0xffffffff) << 32) |                      \
338             __bswap_32(((i) >> 32) & 0xffffffff ))
339 #   endif
340
341 #else /* NTOHL_IN_SYS_PARAM_H || WIN32 */
342 #   include <netinet/in.h>
343
344 #endif /* NTOHL_IN_SYS_PARAM_H || WIN32 */
345
346 /* CEIL: division with round to nearest greater integer */
347 #define CEIL(n, d)  ( ((n) / (d)) + ( ((n) % (d)) ? 1 : 0) )
348
349 /* PAD: PAD(n, d) = CEIL(n ,d) * d */
350 #define PAD(n, d)   ( ((n) % (d)) ? ((((n) / (d)) + 1) * (d)) : (n) )
351
352 /* __MAX and __MIN: self explanatory */
353 #ifndef __MAX
354 #   define __MAX(a, b)   ( ((a) > (b)) ? (a) : (b) )
355 #endif
356 #ifndef __MIN
357 #   define __MIN(a, b)   ( ((a) < (b)) ? (a) : (b) )
358 #endif
359
360 /* MSB (big endian)/LSB (little endian) conversions - network order is always
361  * MSB, and should be used for both network communications and files. Note that
362  * byte orders other than little and big endians are not supported, but only
363  * the VAX seems to have such exotic properties - note that these 'functions'
364  * needs <netinet/in.h> or the local equivalent. */
365 #if !defined( WIN32 )
366 #if WORDS_BIGENDIAN
367 #   define hton16      htons
368 #   define hton32      htonl
369 #   define hton64(i)   ( i )
370 #   define ntoh16      ntohs
371 #   define ntoh32      ntohl
372 #   define ntoh64(i)   ( i )
373 #else
374 #   define hton16      htons
375 #   define hton32      htonl
376     static inline u64 __hton64( u64 i )
377     {
378         return ((u64)(htonl((i) & 0xffffffff)) << 32)
379                 | htonl(((i) >> 32) & 0xffffffff );
380     }
381 #   define hton64(i)   __hton64( i )
382 #   define ntoh16      ntohs
383 #   define ntoh32      ntohl
384 #   define ntoh64      hton64
385 #endif
386 #endif /* !defined( WIN32 ) */
387
388 /* Macros with automatic casts */
389 #define U64_AT(p)   ( ntoh64 ( *( (u64 *)(p) ) ) )
390 #define U32_AT(p)   ( ntoh32 ( *( (u32 *)(p) ) ) )
391 #define U16_AT(p)   ( ntoh16 ( *( (u16 *)(p) ) ) )
392
393 /* Alignment of critical static data structures */
394 #ifdef ATTRIBUTE_ALIGNED_MAX
395 #   define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
396 #else
397 #   define ATTR_ALIGN(align)
398 #endif
399
400 /* Alignment of critical dynamic data structure
401  *
402  * Not all platforms support memalign so we provide a vlc_memalign wrapper
403  * void *vlc_memalign( size_t align, size_t size, void **pp_orig )
404  * *pp_orig is the pointer that has to be freed afterwards.
405  */
406 #if 0
407 #ifdef HAVE_POSIX_MEMALIGN
408 #   define vlc_memalign(align,size,pp_orig) \
409     ( !posix_memalign( pp_orig, align, size ) ? *(pp_orig) : NULL )
410 #endif
411 #endif
412 #ifdef HAVE_MEMALIGN
413     /* Some systems have memalign() but no declaration for it */
414     void * memalign( size_t align, size_t size );
415
416 #   define vlc_memalign(pp_orig,align,size) \
417     ( *(pp_orig) = memalign( align, size ) )
418
419 #else /* We don't have any choice but to align manually */
420 #   define vlc_memalign(pp_orig,align,size) \
421     (( *(pp_orig) = malloc( size + align - 1 )) \
422         ? (void *)( (((unsigned long)*(pp_orig)) + (unsigned long)(align-1) ) \
423                        & (~(unsigned long)(align-1)) ) \
424         : NULL )
425
426 #endif
427
428
429 #define I64C(x)         x##LL
430
431
432 #if defined( WIN32 )
433 /* The ntoh* and hton* bytes swapping functions are provided by winsock
434  * but for conveniency and speed reasons it is better to implement them
435  * ourselves. ( several plugins use them and it is too much hassle to link
436  * winsock with each of them ;-)
437  */
438 #   ifdef WORDS_BIGENDIAN
439 #       define ntoh32(x)       (x)
440 #       define ntoh16(x)       (x)
441 #       define ntoh64(x)       (x)
442 #       define hton32(x)       (x)
443 #       define hton16(x)       (x)
444 #       define hton64(x)       (x)
445 #   else
446 #       define ntoh32(x)     __bswap_32 (x)
447 #       define ntoh16(x)     __bswap_16 (x)
448 #       define ntoh64(x)     __bswap_32 (x)
449 #       define hton32(x)     __bswap_32 (x)
450 #       define hton16(x)     __bswap_16 (x)
451 #       define hton64(x)     __bswap_64 (x)
452 #   endif
453
454 /* win32, cl and icl support */
455 #   if defined( _MSC_VER ) || !defined( __MINGW32__ )
456 #       define __attribute__(x)
457 #       define __inline__      __inline
458 #       define strncasecmp     strnicmp
459 #       define strcasecmp      stricmp
460 #       define S_IFBLK         0x3000  /* Block */
461 #       define S_ISBLK(m)      (0)
462 #       define S_ISCHR(m)      (0)
463 #       define S_ISFIFO(m)     (((m)&_S_IFMT) == _S_IFIFO)
464 #       define S_ISREG(m)      (((m)&_S_IFMT) == _S_IFREG)
465 #       undef I64C
466 #       define I64C(x)         x##i64
467 #   endif
468
469 /* several type definitions */
470 #   if defined( __MINGW32__ )
471 #       if !defined( _OFF_T_ )
472 typedef long long _off_t;
473 typedef _off_t off_t;
474 #           define _OFF_T_
475 #       else
476 #           define off_t long long
477 #       endif
478 #   endif
479
480 #   if defined( _MSC_VER )
481 #       if !defined( _OFF_T_DEFINED )
482 typedef __int64 off_t;
483 #           define _OFF_T_DEFINED
484 #       else
485 #           define off_t __int64
486 #       endif
487 #   endif
488
489 #   if defined( __BORLANDC__ )
490 #       undef off_t
491 #       define off_t unsigned __int64
492 #   endif
493
494 #   ifndef O_NONBLOCK
495 #       define O_NONBLOCK 0
496 #   endif
497
498 #   ifndef snprintf
499 #       define snprintf _snprintf  /* snprintf not defined in mingw32 (bug?) */
500 #   endif
501
502 #endif
503
504 /*****************************************************************************
505  * CPU capabilities
506  *****************************************************************************/
507 #define CPU_CAPABILITY_NONE    0
508 #define CPU_CAPABILITY_486     (1<<0)
509 #define CPU_CAPABILITY_586     (1<<1)
510 #define CPU_CAPABILITY_PPRO    (1<<2)
511 #define CPU_CAPABILITY_MMX     (1<<3)
512 #define CPU_CAPABILITY_3DNOW   (1<<4)
513 #define CPU_CAPABILITY_MMXEXT  (1<<5)
514 #define CPU_CAPABILITY_SSE     (1<<6)
515 #define CPU_CAPABILITY_ALTIVEC (1<<16)
516 #define CPU_CAPABILITY_FPU     (1<<31)
517
518 /*****************************************************************************
519  * I18n stuff
520  *****************************************************************************/
521 #ifndef PACKAGE /* Borland C++ uses this ! */
522 #define PACKAGE VLC_PACKAGE
523 #endif
524 #define VERSION VLC_VERSION
525
526 #if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT ) && !defined( __BORLANDC__ ) && !defined( MODULE_NAME_IS_gnome )
527 #   include <libintl.h>
528 #   undef _
529 #   define _(String) dgettext (PACKAGE, String)
530 #   ifdef gettext_noop
531 #       define N_(String) gettext_noop (String)
532 #   else
533 #       define N_(String) (String)
534 #   endif
535 #elif !defined( MODULE_NAME_IS_gnome )
536 #   define _(String) (String)
537 #   define N_(String) (String)
538 #endif
539
540 /*****************************************************************************
541  * Plug-in stuff
542  *****************************************************************************/
543 #ifndef __PLUGIN__
544 #   define VLC_EXPORT( type, name, args ) type name args;
545 #else
546 #   define VLC_EXPORT( type, name, args ) ;
547     extern module_symbols_t* p_symbols;
548 #endif
549
550 #include "vlc_symbols.h"
551