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.8 2002/07/05 11:18:56 sam Exp $
8 * Authors: Samuel Hocevar <sam@via.ecp.fr>
9 * Vincent Seguin <seguin@via.ecp.fr>
10 * Gildas Bazin <gbazin@netcourrier.com>
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
28 * Compiler-specific workarounds
29 *****************************************************************************/
30 #if defined( __BORLANDC__ )
31 # undef HAVE_VARIADIC_MACROS
35 /*****************************************************************************
36 * Required system headers
37 *****************************************************************************/
39 # include <string.h> /* strerror() */
42 #ifdef HAVE_SYS_TYPES_H
43 # include <sys/types.h>
46 /*****************************************************************************
47 * Basic types definitions
48 *****************************************************************************/
63 typedef unsigned char u8;
64 typedef signed char s8;
66 typedef unsigned short u16;
67 typedef signed short s16;
69 typedef unsigned int u32;
70 typedef signed int s32;
72 # if defined( _MSC_VER ) || ( defined( WIN32 ) && !defined( __MINGW32__ ) )
73 typedef unsigned __int64 u64;
74 typedef signed __int64 s64;
76 typedef unsigned long long u64;
77 typedef signed long long s64;
83 /* ptrdiff_t definition */
90 /* Not portable in a 64-bit environment. */
91 typedef int ptrdiff_t;
99 /* Counter for statistics and profiling */
100 typedef unsigned long count_t;
102 /* DCT elements types */
103 typedef s16 dctelem_t;
105 /* Video buffer types */
106 typedef u8 yuv_data_t;
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 *****************************************************************************/
120 /*****************************************************************************
121 * Classes declaration
122 *****************************************************************************/
125 VLC_DECLARE_STRUCT(msg_bank)
126 VLC_DECLARE_STRUCT(msg_subscription)
129 VLC_DECLARE_STRUCT(playlist)
130 VLC_DECLARE_STRUCT(playlist_item)
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)
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)
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)
155 VLC_DECLARE_STRUCT(aout_thread)
156 VLC_DECLARE_STRUCT(aout_sys)
157 VLC_DECLARE_STRUCT(aout_fifo)
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)
171 VLC_DECLARE_STRUCT(decoder_fifo)
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)
191 /*****************************************************************************
193 *****************************************************************************/
195 # define VLC_EXPORT( type, name, args ) type name args;
197 # define VLC_EXPORT( type, name, args ) ;
198 extern module_symbols_t* p_symbols;
201 /*****************************************************************************
202 * OS-specific headers and thread types
203 *****************************************************************************/
205 # define WIN32_LEAN_AND_MEAN
206 # include <windows.h>
209 #include "vlc_threads.h"
211 /*****************************************************************************
212 * Common structure members
213 *****************************************************************************/
215 /* VLC_COMMON_MEMBERS : members common to all basic vlc objects */
216 #define VLC_COMMON_MEMBERS \
219 char *psz_object_type; \
220 char *psz_object_name; \
222 /* Thread properties, if any */ \
223 vlc_bool_t b_thread; \
224 vlc_thread_t thread_id; \
226 /* Object access lock */ \
227 vlc_mutex_t object_lock; \
228 vlc_cond_t object_wait; \
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 */ \
234 vlc_t * p_vlc; /* root of all evil */ \
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; \
242 /* Just a reminder so that people don't cast garbage */ \
243 int be_sure_to_add_VLC_COMMON_MEMBERS_to_struct; \
245 /* The real vlc_object_t type. Yes, it's that simple :-) */
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
255 /*****************************************************************************
256 * Macros and inline functions
257 *****************************************************************************/
258 #ifdef NTOHL_IN_SYS_PARAM_H
259 # include <sys/param.h>
262 /* Swap bytes in 16 bit value. */
263 # define __bswap_constant_16(x) \
264 ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
266 # if defined __GNUC__ && __GNUC__ >= 2
267 # define __bswap_16(x) \
269 ({ register unsigned short int __v; \
270 if (__builtin_constant_p (x)) \
271 __v = __bswap_constant_16 (x); \
273 __asm__ __volatile__ ("rorw $8, %w0" \
275 : "0" ((unsigned short int) (x)) \
279 /* This is better than nothing. */
280 # define __bswap_16(x) __bswap_constant_16 (x)
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))
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) \
294 ({ register unsigned int __v; \
295 if (__builtin_constant_p (x)) \
296 __v = __bswap_constant_32 (x); \
298 __asm__ __volatile__ ("rorw $8, %w0;" \
302 : "0" ((unsigned int) (x)) \
306 # define __bswap_32(x) \
308 ({ register unsigned int __v; \
309 if (__builtin_constant_p (x)) \
310 __v = __bswap_constant_32 (x); \
312 __asm__ __volatile__ ("bswap %0" \
314 : "0" ((unsigned int) (x))); \
318 # define __bswap_32(x) __bswap_constant_32 (x)
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))
333 # define __bswap_64(x) \
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); \
342 __r.__l[0] = __bswap_32 (__w.__l[1]); \
343 __r.__l[1] = __bswap_32 (__w.__l[0]); \
347 # define __bswap_64(i) \
348 (u64)((__bswap_32((i) & 0xffffffff) << 32) | \
349 __bswap_32(((i) >> 32) & 0xffffffff ))
352 #else /* NTOHL_IN_SYS_PARAM_H || WIN32 */
353 # include <netinet/in.h>
355 #endif /* NTOHL_IN_SYS_PARAM_H || WIN32 */
357 /* CEIL: division with round to nearest greater integer */
358 #define CEIL(n, d) ( ((n) / (d)) + ( ((n) % (d)) ? 1 : 0) )
360 /* PAD: PAD(n, d) = CEIL(n ,d) * d */
361 #define PAD(n, d) ( ((n) % (d)) ? ((((n) / (d)) + 1) * (d)) : (n) )
363 /* __MAX and __MIN: self explanatory */
365 # define __MAX(a, b) ( ((a) > (b)) ? (a) : (b) )
368 # define __MIN(a, b) ( ((a) < (b)) ? (a) : (b) )
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 )
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 )
385 # define hton16 htons
386 # define hton32 htonl
387 static inline u64 __hton64( u64 i )
389 return ((u64)(htonl((i) & 0xffffffff)) << 32)
390 | htonl(((i) >> 32) & 0xffffffff );
392 # define hton64(i) __hton64( i )
393 # define ntoh16 ntohs
394 # define ntoh32 ntohl
395 # define ntoh64 hton64
397 #endif /* !defined( WIN32 ) */
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) ) ) )
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)))
408 # define ATTR_ALIGN(align)
411 /* Alignment of critical dynamic data structure
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.
418 #ifdef HAVE_POSIX_MEMALIGN
419 # define vlc_memalign(align,size,pp_orig) \
420 ( !posix_memalign( pp_orig, align, size ) ? *(pp_orig) : NULL )
424 /* Some systems have memalign() but no declaration for it */
425 void * memalign( size_t align, size_t size );
427 # define vlc_memalign(pp_orig,align,size) \
428 ( *(pp_orig) = memalign( align, size ) )
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)) ) \
439 /* strndup (defined in src/misc/extras.c) */
441 char * strndup( const char *s, size_t n );
445 #define I64C(x) x##LL
449 /* The ntoh* and hton* bytes swapping functions are provided by winsock
450 * but for conveniency and speed reasons it is better to implement them
451 * ourselves. ( several plugins use them and it is too much hassle to link
452 * winsock with each of them ;-)
454 # ifdef WORDS_BIGENDIAN
455 # define ntoh32(x) (x)
456 # define ntoh16(x) (x)
457 # define ntoh64(x) (x)
458 # define hton32(x) (x)
459 # define hton16(x) (x)
460 # define hton64(x) (x)
462 # define ntoh32(x) __bswap_32 (x)
463 # define ntoh16(x) __bswap_16 (x)
464 # define ntoh64(x) __bswap_32 (x)
465 # define hton32(x) __bswap_32 (x)
466 # define hton16(x) __bswap_16 (x)
467 # define hton64(x) __bswap_64 (x)
470 /* win32, cl and icl support */
471 # if defined( _MSC_VER ) || !defined( __MINGW32__ )
472 # define __attribute__(x)
473 # define __inline__ __inline
474 # define strncasecmp strnicmp
475 # define strcasecmp stricmp
476 # define S_IFBLK 0x3000 /* Block */
477 # define S_ISBLK(m) (0)
478 # define S_ISCHR(m) (0)
479 # define S_ISFIFO(m) (((m)&_S_IFMT) == _S_IFIFO)
480 # define S_ISREG(m) (((m)&_S_IFMT) == _S_IFREG)
482 # define I64C(x) x##i64
485 /* several type definitions */
486 # if defined( __MINGW32__ )
487 # if !defined( _OFF_T_ )
488 typedef long long _off_t;
489 typedef _off_t off_t;
492 # define off_t long long
496 # if defined( _MSC_VER )
497 # if !defined( _OFF_T_DEFINED )
498 typedef __int64 off_t;
499 # define _OFF_T_DEFINED
501 # define off_t __int64
505 # if defined( __BORLANDC__ )
507 # define off_t unsigned __int64
511 # define O_NONBLOCK 0
515 # define snprintf _snprintf /* snprintf not defined in mingw32 (bug?) */
520 /*****************************************************************************
522 *****************************************************************************/
523 #define CPU_CAPABILITY_NONE 0
524 #define CPU_CAPABILITY_486 (1<<0)
525 #define CPU_CAPABILITY_586 (1<<1)
526 #define CPU_CAPABILITY_PPRO (1<<2)
527 #define CPU_CAPABILITY_MMX (1<<3)
528 #define CPU_CAPABILITY_3DNOW (1<<4)
529 #define CPU_CAPABILITY_MMXEXT (1<<5)
530 #define CPU_CAPABILITY_SSE (1<<6)
531 #define CPU_CAPABILITY_ALTIVEC (1<<16)
532 #define CPU_CAPABILITY_FPU (1<<31)
534 /*****************************************************************************
536 *****************************************************************************/
537 #ifndef PACKAGE /* Borland C++ uses this ! */
538 #define PACKAGE VLC_PACKAGE
540 #define VERSION VLC_VERSION
542 #if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT ) && !defined( __BORLANDC__ ) && !defined( MODULE_NAME_IS_gnome )
543 # include <libintl.h>
545 # define _(String) dgettext (PACKAGE, String)
547 # define N_(String) gettext_noop (String)
549 # define N_(String) (String)
551 #elif !defined( MODULE_NAME_IS_gnome )
552 # define _(String) (String)
553 # define N_(String) (String)
556 /*****************************************************************************
558 *****************************************************************************/
559 #include "vlc_symbols.h"