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