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.1 2002/06/01 12:31:58 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 * Required system headers
29 *****************************************************************************/
31 # include <string.h> /* strerror() */
34 #ifdef HAVE_SYS_TYPES_H
35 # include <sys/types.h>
38 /*****************************************************************************
39 * Basic types definitions
40 *****************************************************************************/
55 typedef unsigned char u8;
56 typedef signed char s8;
58 typedef unsigned short u16;
59 typedef signed short s16;
61 typedef unsigned int u32;
62 typedef signed int s32;
64 # if defined( _MSC_VER ) || ( defined( WIN32 ) && !defined( __MINGW32__ ) )
65 typedef unsigned __int64 u64;
66 typedef signed __int64 s64;
68 typedef unsigned long long u64;
69 typedef signed long long s64;
75 /* ptrdiff_t definition */
82 /* Not portable in a 64-bit environment. */
83 typedef int ptrdiff_t;
91 /* Counter for statistics and profiling */
92 typedef unsigned long count_t;
94 /* DCT elements types */
95 typedef s16 dctelem_t;
97 /* Video buffer types */
98 typedef u8 yuv_data_t;
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 *****************************************************************************/
112 /*****************************************************************************
113 * Classes declaration
114 *****************************************************************************/
117 VLC_DECLARE_STRUCT(main_sys)
120 VLC_DECLARE_STRUCT(msg_bank)
121 VLC_DECLARE_STRUCT(msg_subscription)
124 VLC_DECLARE_STRUCT(playlist)
125 VLC_DECLARE_STRUCT(playlist_item)
128 VLC_DECLARE_STRUCT(module_bank)
129 VLC_DECLARE_STRUCT(module)
130 VLC_DECLARE_STRUCT(module_config)
131 VLC_DECLARE_STRUCT(module_symbols)
132 VLC_DECLARE_STRUCT(module_functions)
135 VLC_DECLARE_STRUCT(intf_thread)
136 VLC_DECLARE_STRUCT(intf_sys)
137 VLC_DECLARE_STRUCT(intf_console)
138 VLC_DECLARE_STRUCT(intf_msg)
139 VLC_DECLARE_STRUCT(intf_channel)
142 VLC_DECLARE_STRUCT(input_thread)
143 VLC_DECLARE_STRUCT(input_channel)
144 VLC_DECLARE_STRUCT(input_cfg)
145 VLC_DECLARE_STRUCT(input_area)
146 VLC_DECLARE_STRUCT(input_buffers)
147 VLC_DECLARE_STRUCT(input_socket)
150 VLC_DECLARE_STRUCT(aout_thread)
151 VLC_DECLARE_STRUCT(aout_sys)
152 VLC_DECLARE_STRUCT(aout_fifo)
155 VLC_DECLARE_STRUCT(vout_thread)
156 VLC_DECLARE_STRUCT(vout_font)
157 VLC_DECLARE_STRUCT(vout_sys)
158 VLC_DECLARE_STRUCT(chroma_sys)
159 VLC_DECLARE_STRUCT(picture)
160 VLC_DECLARE_STRUCT(picture_sys)
161 VLC_DECLARE_STRUCT(picture_heap)
162 VLC_DECLARE_STRUCT(subpicture)
163 VLC_DECLARE_STRUCT(subpicture_sys)
166 VLC_DECLARE_STRUCT(decoder_fifo)
169 VLC_DECLARE_STRUCT(macroblock)
170 VLC_DECLARE_STRUCT(data_packet)
171 VLC_DECLARE_STRUCT(data_buffer)
172 VLC_DECLARE_STRUCT(downmix)
173 VLC_DECLARE_STRUCT(imdct)
174 VLC_DECLARE_STRUCT(complex)
175 VLC_DECLARE_STRUCT(dm_par)
176 VLC_DECLARE_STRUCT(es_descriptor)
177 VLC_DECLARE_STRUCT(pgrm_descriptor)
178 VLC_DECLARE_STRUCT(stream_descriptor)
179 VLC_DECLARE_STRUCT(stream_position)
180 VLC_DECLARE_STRUCT(stream_ctrl)
181 VLC_DECLARE_STRUCT(pes_packet)
182 VLC_DECLARE_STRUCT(bit_stream)
183 VLC_DECLARE_STRUCT(network_socket)
184 VLC_DECLARE_STRUCT(iso639_lang)
186 /*****************************************************************************
187 * OS-specific headers and thread types
188 *****************************************************************************/
189 #include "os_specific.h"
190 #include "vlc_threads.h"
192 /*****************************************************************************
193 * Compiler-specific workarounds
194 *****************************************************************************/
195 #if defined( __BORLANDC__ )
196 # undef HAVE_VARIADIC_MACROS
199 /*****************************************************************************
200 * Common structure members
201 *****************************************************************************/
203 /* VLC_COMMON_MEMBERS : members common to all basic vlc objects */
204 #define VLC_COMMON_MEMBERS \
207 char *psz_object_type; \
208 char *psz_object_name; \
210 /* Thread properties, if any */ \
211 vlc_bool_t b_thread; \
212 vlc_thread_t thread_id; \
213 vlc_mutex_t thread_lock; \
214 vlc_cond_t thread_wait; \
216 volatile vlc_bool_t b_error; /* set by the object */ \
217 volatile vlc_bool_t b_die; /* set by the outside */ \
218 volatile vlc_bool_t b_dead; /* set by the object */ \
220 vlc_object_t * p_this; /* a pointer to ourselves */ \
221 vlc_t * p_vlc; /* root of all evil */ \
223 volatile int i_refcount; \
224 vlc_object_t ** pp_parents; /* our parents */ \
225 volatile int i_parents; \
226 vlc_object_t ** pp_children; /* our children */ \
227 volatile int i_children; \
229 /* Just a reminder so that people don't cast garbage */ \
230 int be_sure_to_add_VLC_COMMON_MEMBERS_to_struct; \
232 /* The real vlc_object_t type. Yes, it's that simple :-) */
238 /* CAST_TO_VLC_OBJECT: attempt at doing a clever cast */
239 #define CAST_TO_VLC_OBJECT( x ) \
240 ((vlc_object_t *)(x))+0*(x)->be_sure_to_add_VLC_COMMON_MEMBERS_to_struct
242 /*****************************************************************************
243 * Macros and inline functions
244 *****************************************************************************/
245 #ifdef NTOHL_IN_SYS_PARAM_H
246 # include <sys/param.h>
249 /* Swap bytes in 16 bit value. */
250 # define __bswap_constant_16(x) \
251 ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
253 # if defined __GNUC__ && __GNUC__ >= 2
254 # define __bswap_16(x) \
256 ({ register unsigned short int __v; \
257 if (__builtin_constant_p (x)) \
258 __v = __bswap_constant_16 (x); \
260 __asm__ __volatile__ ("rorw $8, %w0" \
262 : "0" ((unsigned short int) (x)) \
266 /* This is better than nothing. */
267 # define __bswap_16(x) __bswap_constant_16 (x)
270 /* Swap bytes in 32 bit value. */
271 # define __bswap_constant_32(x) \
272 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
273 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
275 # if defined __GNUC__ && __GNUC__ >= 2
276 /* To swap the bytes in a word the i486 processors and up provide the
277 `bswap' opcode. On i386 we have to use three instructions. */
278 # if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__
279 # define __bswap_32(x) \
281 ({ register unsigned int __v; \
282 if (__builtin_constant_p (x)) \
283 __v = __bswap_constant_32 (x); \
285 __asm__ __volatile__ ("rorw $8, %w0;" \
289 : "0" ((unsigned int) (x)) \
293 # define __bswap_32(x) \
295 ({ register unsigned int __v; \
296 if (__builtin_constant_p (x)) \
297 __v = __bswap_constant_32 (x); \
299 __asm__ __volatile__ ("bswap %0" \
301 : "0" ((unsigned int) (x))); \
305 # define __bswap_32(x) __bswap_constant_32 (x)
308 # if defined __GNUC__ && __GNUC__ >= 2
309 /* Swap bytes in 64 bit value. */
310 # define __bswap_constant_64(x) \
311 ((((x) & 0xff00000000000000ull) >> 56) \
312 | (((x) & 0x00ff000000000000ull) >> 40) \
313 | (((x) & 0x0000ff0000000000ull) >> 24) \
314 | (((x) & 0x000000ff00000000ull) >> 8) \
315 | (((x) & 0x00000000ff000000ull) << 8) \
316 | (((x) & 0x0000000000ff0000ull) << 24) \
317 | (((x) & 0x000000000000ff00ull) << 40) \
318 | (((x) & 0x00000000000000ffull) << 56))
320 # define __bswap_64(x) \
322 ({ union { __extension__ unsigned long long int __ll; \
323 unsigned long int __l[2]; } __w, __r; \
324 if (__builtin_constant_p (x)) \
325 __r.__ll = __bswap_constant_64 (x); \
329 __r.__l[0] = __bswap_32 (__w.__l[1]); \
330 __r.__l[1] = __bswap_32 (__w.__l[0]); \
334 # define __bswap_64(i) \
335 (u64)((__bswap_32((i) & 0xffffffff) << 32) | \
336 __bswap_32(((i) >> 32) & 0xffffffff ))
339 #else /* NTOHL_IN_SYS_PARAM_H || WIN32 */
340 # include <netinet/in.h>
342 #endif /* NTOHL_IN_SYS_PARAM_H || WIN32 */
344 /* CEIL: division with round to nearest greater integer */
345 #define CEIL(n, d) ( ((n) / (d)) + ( ((n) % (d)) ? 1 : 0) )
347 /* PAD: PAD(n, d) = CEIL(n ,d) * d */
348 #define PAD(n, d) ( ((n) % (d)) ? ((((n) / (d)) + 1) * (d)) : (n) )
350 /* __MAX and __MIN: self explanatory */
352 # define __MAX(a, b) ( ((a) > (b)) ? (a) : (b) )
355 # define __MIN(a, b) ( ((a) < (b)) ? (a) : (b) )
358 /* MSB (big endian)/LSB (little endian) conversions - network order is always
359 * MSB, and should be used for both network communications and files. Note that
360 * byte orders other than little and big endians are not supported, but only
361 * the VAX seems to have such exotic properties - note that these 'functions'
362 * needs <netinet/in.h> or the local equivalent. */
363 #if !defined( WIN32 )
365 # define hton16 htons
366 # define hton32 htonl
367 # define hton64(i) ( i )
368 # define ntoh16 ntohs
369 # define ntoh32 ntohl
370 # define ntoh64(i) ( i )
372 # define hton16 htons
373 # define hton32 htonl
374 static inline u64 __hton64( u64 i )
376 return ((u64)(htonl((i) & 0xffffffff)) << 32)
377 | htonl(((i) >> 32) & 0xffffffff );
379 # define hton64(i) __hton64( i )
380 # define ntoh16 ntohs
381 # define ntoh32 ntohl
382 # define ntoh64 hton64
384 #endif /* !defined( WIN32 ) */
386 /* Macros with automatic casts */
387 #define U64_AT(p) ( ntoh64 ( *( (u64 *)(p) ) ) )
388 #define U32_AT(p) ( ntoh32 ( *( (u32 *)(p) ) ) )
389 #define U16_AT(p) ( ntoh16 ( *( (u16 *)(p) ) ) )
391 /* Alignment of critical static data structures */
392 #ifdef ATTRIBUTE_ALIGNED_MAX
393 # define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
395 # define ATTR_ALIGN(align)
398 /* Alignment of critical dynamic data structure
400 * Not all platforms support memalign so we provide a vlc_memalign wrapper
401 * void *vlc_memalign( size_t align, size_t size, void **pp_orig )
402 * *pp_orig is the pointer that has to be freed afterwards.
405 #ifdef HAVE_POSIX_MEMALIGN
406 # define vlc_memalign(align,size,pp_orig) \
407 ( !posix_memalign( pp_orig, align, size ) ? *(pp_orig) : NULL )
411 /* Some systems have memalign() but no declaration for it */
412 void * memalign( size_t align, size_t size );
414 # define vlc_memalign(pp_orig,align,size) \
415 ( *(pp_orig) = memalign( align, size ) )
417 #else /* We don't have any choice but to align manually */
418 # define vlc_memalign(pp_orig,align,size) \
419 (( *(pp_orig) = malloc( size + align - 1 )) \
420 ? (void *)( (((unsigned long)*(pp_orig)) + (unsigned long)(align-1) ) \
421 & (~(unsigned long)(align-1)) ) \
427 #define I64C(x) x##LL
431 /* The ntoh* and hton* bytes swapping functions are provided by winsock
432 * but for conveniency and speed reasons it is better to implement them
433 * ourselves. ( several plugins use them and it is too much hassle to link
434 * winsock with each of them ;-)
436 # ifdef WORDS_BIGENDIAN
437 # define ntoh32(x) (x)
438 # define ntoh16(x) (x)
439 # define ntoh64(x) (x)
440 # define hton32(x) (x)
441 # define hton16(x) (x)
442 # define hton64(x) (x)
444 # define ntoh32(x) __bswap_32 (x)
445 # define ntoh16(x) __bswap_16 (x)
446 # define ntoh64(x) __bswap_32 (x)
447 # define hton32(x) __bswap_32 (x)
448 # define hton16(x) __bswap_16 (x)
449 # define hton64(x) __bswap_64 (x)
452 /* win32, cl and icl support */
453 # if defined( _MSC_VER ) || !defined( __MINGW32__ )
454 # define __attribute__(x)
455 # define __inline__ __inline
456 # define strncasecmp strnicmp
457 # define strcasecmp stricmp
458 # define S_IFBLK 0x3000 /* Block */
459 # define S_ISBLK(m) (0)
460 # define S_ISCHR(m) (0)
461 # define S_ISFIFO(m) (((m)&_S_IFMT) == _S_IFIFO)
462 # define S_ISREG(m) (((m)&_S_IFMT) == _S_IFREG)
464 # define I64C(x) x##i64
467 /* several type definitions */
468 # if defined( __MINGW32__ )
469 # if !defined( _OFF_T_ )
470 typedef long long _off_t;
471 typedef _off_t off_t;
474 # define off_t long long
478 # if defined( _MSC_VER )
479 # if !defined( _OFF_T_DEFINED )
480 typedef __int64 off_t;
481 # define _OFF_T_DEFINED
483 # define off_t __int64
487 # if defined( __BORLANDC__ )
489 # define off_t unsigned __int64
493 # define O_NONBLOCK 0
497 # define snprintf _snprintf /* snprintf not defined in mingw32 (bug?) */
502 /*****************************************************************************
504 *****************************************************************************/
505 #define CPU_CAPABILITY_NONE 0
506 #define CPU_CAPABILITY_486 (1<<0)
507 #define CPU_CAPABILITY_586 (1<<1)
508 #define CPU_CAPABILITY_PPRO (1<<2)
509 #define CPU_CAPABILITY_MMX (1<<3)
510 #define CPU_CAPABILITY_3DNOW (1<<4)
511 #define CPU_CAPABILITY_MMXEXT (1<<5)
512 #define CPU_CAPABILITY_SSE (1<<6)
513 #define CPU_CAPABILITY_ALTIVEC (1<<16)
514 #define CPU_CAPABILITY_FPU (1<<31)
516 /*****************************************************************************
518 *****************************************************************************/
519 #ifndef PACKAGE /* Borland C++ uses this ! */
520 #define PACKAGE VLC_PACKAGE
522 #define VERSION VLC_VERSION
524 #if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT ) && !defined( __BORLANDC__ ) && !defined( MODULE_NAME_IS_gnome )
525 # include <libintl.h>
527 # define _(String) dgettext (PACKAGE, String)
529 # define N_(String) gettext_noop (String)
531 # define N_(String) (String)
533 #elif !defined( MODULE_NAME_IS_gnome )
534 # define _(String) (String)
535 # define N_(String) (String)
538 /*****************************************************************************
540 *****************************************************************************/
542 # define VLC_EXPORT( type, name, args ) type name args;
544 # define VLC_EXPORT( type, name, args ) ;
545 extern module_symbols_t* p_symbols;
548 #include "vlc_symbols.h"