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: common.h,v 1.92 2002/04/05 01:05:22 gbazin 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 *****************************************************************************/
30 #include <string.h> /* strerror() */
31 #include <sys/types.h>
33 /*****************************************************************************
34 * Basic types definitions
35 *****************************************************************************/
37 typedef unsigned char u8;
38 typedef signed char s8;
40 typedef unsigned short u16;
41 typedef signed short s16;
43 typedef unsigned int u32;
44 typedef signed int s32;
46 #if defined( _MSC_VER ) || ( defined( WIN32 ) && !defined( __MINGW32__ ) )
47 typedef unsigned __int64 u64;
48 typedef signed __int64 s64;
50 typedef unsigned long long u64;
51 typedef signed long long s64;
57 #ifdef BOOLEAN_T_IN_SYS_TYPES_H
58 /* <sys/types.h> already included */
59 #elif defined(BOOLEAN_T_IN_PTHREAD_H)
61 #elif defined(BOOLEAN_T_IN_CTHREADS_H)
62 # include <cthreads.h>
64 typedef int boolean_t;
67 # define _MACH_I386_BOOLEAN_H_
70 /* ptrdiff_t definition */
77 /* Not portable in a 64-bit environment. */
78 typedef int ptrdiff_t;
86 /* Counter for statistics and profiling */
87 typedef unsigned long count_t;
89 /* DCT elements types */
90 typedef s16 dctelem_t;
92 /* Video buffer types */
93 typedef u8 yuv_data_t;
95 /*****************************************************************************
96 * mtime_t: high precision date or time interval
97 *****************************************************************************
98 * Store an high precision date or time interval. The maximum precision is the
99 * micro-second, and a 64 bits integer is used to avoid any overflow (maximum
100 * time interval is then 292271 years, which should be long enough for any
101 * video). Date are stored as a time interval since a common date.
102 * Note that date and time intervals can be manipulated using regular
103 * arithmetic operators, and that no special functions are required.
104 *****************************************************************************/
107 /*****************************************************************************
108 * Classes declaration
109 *****************************************************************************/
114 typedef struct main_sys_s * p_main_sys_t;
117 struct plugin_bank_s;
118 struct plugin_info_s;
120 typedef struct plugin_bank_s * p_plugin_bank_t;
121 typedef struct plugin_info_s * p_plugin_info_t;
125 struct playlist_item_s;
127 struct module_config_s;
129 typedef struct playlist_s * p_playlist_t;
130 typedef struct playlist_item_s * p_playlist_item_t;
131 typedef struct module_s * p_module_t;
132 typedef struct module_config_s * p_module_config_t;
135 struct intf_thread_s;
137 struct intf_console_s;
139 struct intf_channel_s;
141 typedef struct intf_thread_s * p_intf_thread_t;
142 typedef struct intf_sys_s * p_intf_sys_t;
143 typedef struct intf_console_s * p_intf_console_t;
144 typedef struct intf_msg_s * p_intf_msg_t;
145 typedef struct intf_channel_s * p_intf_channel_t;
148 struct input_thread_s;
149 struct input_channel_s;
153 typedef struct input_thread_s * p_input_thread_t;
154 typedef struct input_channel_s * p_input_channel_t;
155 typedef struct input_cfg_s * p_input_cfg_t;
156 typedef struct input_area_s * p_input_area_t;
159 struct aout_thread_s;
162 typedef struct aout_thread_s * p_aout_thread_t;
163 typedef struct aout_sys_s * p_aout_sys_t;
166 struct vout_thread_s;
170 struct vdec_thread_s;
171 struct vpar_thread_s;
172 struct video_parser_s;
174 typedef struct vout_thread_s * p_vout_thread_t;
175 typedef struct vout_font_s * p_vout_font_t;
176 typedef struct vout_sys_s * p_vout_sys_t;
177 typedef struct chroma_sys_s * p_chroma_sys_t;
178 typedef struct vdec_thread_s * p_vdec_thread_t;
179 typedef struct vpar_thread_s * p_vpar_thread_t;
180 typedef struct video_parser_s * p_video_parser_t;
183 struct decoder_config_s;
184 struct decoder_fifo_s;
188 struct data_packet_s;
193 struct picture_sys_s;
194 struct picture_heap_s;
195 struct es_descriptor_s;
196 struct pgrm_descriptor_s;
200 struct input_buffers_s;
201 struct network_socket_s;
202 struct intf_subscription_s;
204 /*****************************************************************************
205 * Macros and inline functions
206 *****************************************************************************/
207 #ifdef NTOHL_IN_SYS_PARAM_H
208 # include <sys/param.h>
211 /* Swap bytes in 16 bit value. */
212 # define __bswap_constant_16(x) \
213 ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
215 # if defined __GNUC__ && __GNUC__ >= 2
216 # define __bswap_16(x) \
218 ({ register unsigned short int __v; \
219 if (__builtin_constant_p (x)) \
220 __v = __bswap_constant_16 (x); \
222 __asm__ __volatile__ ("rorw $8, %w0" \
224 : "0" ((unsigned short int) (x)) \
228 /* This is better than nothing. */
229 # define __bswap_16(x) __bswap_constant_16 (x)
232 /* Swap bytes in 32 bit value. */
233 # define __bswap_constant_32(x) \
234 ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
235 (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
237 # if defined __GNUC__ && __GNUC__ >= 2
238 /* To swap the bytes in a word the i486 processors and up provide the
239 `bswap' opcode. On i386 we have to use three instructions. */
240 # if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__
241 # define __bswap_32(x) \
243 ({ register unsigned int __v; \
244 if (__builtin_constant_p (x)) \
245 __v = __bswap_constant_32 (x); \
247 __asm__ __volatile__ ("rorw $8, %w0;" \
251 : "0" ((unsigned int) (x)) \
255 # define __bswap_32(x) \
257 ({ register unsigned int __v; \
258 if (__builtin_constant_p (x)) \
259 __v = __bswap_constant_32 (x); \
261 __asm__ __volatile__ ("bswap %0" \
263 : "0" ((unsigned int) (x))); \
267 # define __bswap_32(x) __bswap_constant_32 (x)
270 # if defined __GNUC__ && __GNUC__ >= 2
271 /* Swap bytes in 64 bit value. */
272 # define __bswap_constant_64(x) \
273 ((((x) & 0xff00000000000000ull) >> 56) \
274 | (((x) & 0x00ff000000000000ull) >> 40) \
275 | (((x) & 0x0000ff0000000000ull) >> 24) \
276 | (((x) & 0x000000ff00000000ull) >> 8) \
277 | (((x) & 0x00000000ff000000ull) << 8) \
278 | (((x) & 0x0000000000ff0000ull) << 24) \
279 | (((x) & 0x000000000000ff00ull) << 40) \
280 | (((x) & 0x00000000000000ffull) << 56))
282 # define __bswap_64(x) \
284 ({ union { __extension__ unsigned long long int __ll; \
285 unsigned long int __l[2]; } __w, __r; \
286 if (__builtin_constant_p (x)) \
287 __r.__ll = __bswap_constant_64 (x); \
291 __r.__l[0] = __bswap_32 (__w.__l[1]); \
292 __r.__l[1] = __bswap_32 (__w.__l[0]); \
296 # define __bswap_64(i) \
297 (u64)((__bswap_32((i) & 0xffffffff) << 32) | \
298 __bswap_32(((i) >> 32) & 0xffffffff ))
301 #else /* NTOHL_IN_SYS_PARAM_H || WIN32 */
302 # include <netinet/in.h>
304 #endif /* NTOHL_IN_SYS_PARAM_H || WIN32 */
306 /* CEIL: division with round to nearest greater integer */
307 #define CEIL(n, d) ( ((n) / (d)) + ( ((n) % (d)) ? 1 : 0) )
309 /* PAD: PAD(n, d) = CEIL(n ,d) * d */
310 #define PAD(n, d) ( ((n) % (d)) ? ((((n) / (d)) + 1) * (d)) : (n) )
312 /* __MAX and __MIN: self explanatory */
314 # define __MAX(a, b) ( ((a) > (b)) ? (a) : (b) )
317 # define __MIN(a, b) ( ((a) < (b)) ? (a) : (b) )
320 /* MSB (big endian)/LSB (little endian) conversions - network order is always
321 * MSB, and should be used for both network communications and files. Note that
322 * byte orders other than little and big endians are not supported, but only
323 * the VAX seems to have such exotic properties - note that these 'functions'
324 * needs <netinet/in.h> or the local equivalent. */
325 #if !defined( WIN32 )
327 # define hton16 htons
328 # define hton32 htonl
329 # define hton64(i) ( i )
330 # define ntoh16 ntohs
331 # define ntoh32 ntohl
332 # define ntoh64(i) ( i )
334 # define hton16 htons
335 # define hton32 htonl
336 static __inline__ u64 __hton64( u64 i )
338 return ((u64)(htonl((i) & 0xffffffff)) << 32)
339 | htonl(((i) >> 32) & 0xffffffff );
341 # define hton64(i) __hton64( i )
342 # define ntoh16 ntohs
343 # define ntoh32 ntohl
344 # define ntoh64 hton64
346 #endif /* !defined( WIN32 ) */
348 /* Macros with automatic casts */
349 #define U64_AT(p) ( ntoh64 ( *( (u64 *)(p) ) ) )
350 #define U32_AT(p) ( ntoh32 ( *( (u32 *)(p) ) ) )
351 #define U16_AT(p) ( ntoh16 ( *( (u16 *)(p) ) ) )
353 /* Alignment of critical static data structures */
354 #ifdef ATTRIBUTE_ALIGNED_MAX
355 # define ATTR_ALIGN(align) __attribute__ ((__aligned__ ((ATTRIBUTE_ALIGNED_MAX < align) ? ATTRIBUTE_ALIGNED_MAX : align)))
357 # define ATTR_ALIGN(align)
360 /* Alignment of critical dynamic data structure
362 * Not all platforms support memalign so we provide a vlc_memalign wrapper
363 * void *vlc_memalign( size_t align, size_t size, void **pp_orig )
364 * *pp_orig is the pointer that has to be freed afterwards.
367 #ifdef HAVE_POSIX_MEMALIGN
368 # define vlc_memalign(align,size,pp_orig) \
369 ( !posix_memalign( pp_orig, align, size ) ? *(pp_orig) : NULL )
373 /* Some systems have memalign() but no declaration for it */
374 void * memalign( size_t align, size_t size );
376 # define vlc_memalign(align,size,pp_orig) \
377 ( *(pp_orig) = memalign( align, size ) )
379 #else /* We don't have any choice but to align manually */
380 # define vlc_memalign(align,size,pp_orig) \
381 (( *(pp_orig) = malloc( size + align - 1 )) ? \
382 (void *)( (((unsigned long)*(pp_orig)) + 15) & ~0xFUL ) : NULL )
387 #define I64C(x) x##LL
391 /* The ntoh* and hton* bytes swapping functions are provided by winsock
392 * but for conveniency and speed reasons it is better to implement them
393 * ourselves. ( several plugins use them and it is too much hassle to link
394 * winsock with each of them ;-)
396 # ifdef WORDS_BIGENDIAN
397 # define ntoh32(x) (x)
398 # define ntoh16(x) (x)
399 # define ntoh64(x) (x)
400 # define hton32(x) (x)
401 # define hton16(x) (x)
402 # define hton64(x) (x)
404 # define ntoh32(x) __bswap_32 (x)
405 # define ntoh16(x) __bswap_16 (x)
406 # define ntoh64(x) __bswap_32 (x)
407 # define hton32(x) __bswap_32 (x)
408 # define hton16(x) __bswap_16 (x)
409 # define hton64(x) __bswap_64 (x)
412 /* win32, cl and icl support */
413 # if defined( _MSC_VER ) || !defined( __MINGW32__ )
414 # define __attribute__(x)
415 # define __inline__ __inline
416 # define strncasecmp strnicmp
417 # define strcasecmp stricmp
418 # define S_IFBLK 0x3000 /* Block */
419 # define S_ISBLK(m) (0)
420 # define S_ISCHR(m) (0)
421 # define S_ISFIFO(m) (((m)&_S_IFMT) == _S_IFIFO)
422 # define S_ISREG(m) (((m)&_S_IFMT) == _S_IFREG)
424 # define I64C(x) x##i64
427 /* several type definitions */
428 # if defined( __MINGW32__ )
429 # if !defined( _OFF_T_ )
430 typedef long long _off_t;
431 typedef _off_t off_t;
434 # define off_t long long
438 # if defined( _MSC_VER )
439 # if !defined( _OFF_T_DEFINED )
440 typedef __int64 off_t;
441 # define _OFF_T_DEFINED
443 # define off_t __int64
447 # if defined( __BORLANDC__ )
449 # define off_t unsigned __int64
453 # define O_NONBLOCK 0
457 # define snprintf _snprintf /* snprintf not defined in mingw32 (bug?) */
462 /*****************************************************************************
464 *****************************************************************************/
465 #define CPU_CAPABILITY_NONE 0
466 #define CPU_CAPABILITY_486 (1<<0)
467 #define CPU_CAPABILITY_586 (1<<1)
468 #define CPU_CAPABILITY_PPRO (1<<2)
469 #define CPU_CAPABILITY_MMX (1<<3)
470 #define CPU_CAPABILITY_3DNOW (1<<4)
471 #define CPU_CAPABILITY_MMXEXT (1<<5)
472 #define CPU_CAPABILITY_SSE (1<<6)
473 #define CPU_CAPABILITY_ALTIVEC (1<<16)
474 #define CPU_CAPABILITY_FPU (1<<31)
476 /*****************************************************************************
478 *****************************************************************************/
479 #if defined( ENABLE_NLS ) && defined ( HAVE_GETTEXT )
480 # include <libintl.h>
482 # define _(String) (String)
483 # define N_(String) (String)
486 /*****************************************************************************
488 *****************************************************************************/
489 typedef struct module_symbols_s
491 struct main_s* p_main;
492 struct main_sys_s* p_main_sys;
493 struct module_bank_s* p_module_bank;
494 struct input_bank_s* p_input_bank;
495 struct aout_bank_s* p_aout_bank;
496 struct vout_bank_s* p_vout_bank;
498 int ( * config_GetIntVariable ) ( const char * );
499 char * ( * config_GetPszVariable ) ( const char * );
500 void ( * config_PutIntVariable ) ( const char *, int );
501 void ( * config_PutPszVariable ) ( const char *, char * );
502 int ( * config_LoadConfigFile ) ( const char * );
503 int ( * config_SaveConfigFile ) ( const char * );
504 struct module_config_s * ( * config_FindConfig ) ( const char * );
505 struct module_config_s * ( * config_Duplicate ) ( struct module_s * );
507 struct intf_subscription_s * ( * intf_MsgSub ) ( void );
508 void ( * intf_MsgUnsub ) ( struct intf_subscription_s * );
510 void ( * intf_Msg ) ( char *, ... );
511 void ( * intf_ErrMsg ) ( char *, ... );
512 void ( * intf_StatMsg ) ( char *, ... );
513 void ( * intf_WarnMsg ) ( int, char *, ... );
515 int ( * intf_PlaylistAdd ) ( struct playlist_s *, int, const char* );
516 int ( * intf_PlaylistDelete ) ( struct playlist_s *, int );
517 void ( * intf_PlaylistNext ) ( struct playlist_s * );
518 void ( * intf_PlaylistPrev ) ( struct playlist_s * );
519 void ( * intf_PlaylistDestroy ) ( struct playlist_s * );
520 void ( * intf_PlaylistJumpto ) ( struct playlist_s *, int );
521 void ( * intf_UrlDecode ) ( char * );
522 int ( * intf_Eject ) ( const char * );
524 void ( * msleep ) ( mtime_t );
525 mtime_t ( * mdate ) ( void );
526 char * ( * mstrtime ) ( char *, mtime_t );
528 int ( * network_ChannelCreate )( void );
529 int ( * network_ChannelJoin ) ( int );
531 int ( * input_SetProgram ) ( struct input_thread_s *,
532 struct pgrm_descriptor_s * );
533 void ( * input_SetStatus ) ( struct input_thread_s *, int );
534 void ( * input_Seek ) ( struct input_thread_s *, off_t );
535 void ( * input_DumpStream ) ( struct input_thread_s * );
536 char * ( * input_OffsetToTime ) ( struct input_thread_s *, char *, off_t );
537 int ( * input_ChangeES ) ( struct input_thread_s *,
538 struct es_descriptor_s *, u8 );
539 int ( * input_ToggleES ) ( struct input_thread_s *,
540 struct es_descriptor_s *, boolean_t );
541 int ( * input_ChangeArea ) ( struct input_thread_s *,
542 struct input_area_s * );
543 int ( * input_ChangeProgram ) ( struct input_thread_s *, u16 );
544 struct es_descriptor_s * ( * input_FindES ) ( struct input_thread_s *,
546 struct es_descriptor_s * ( * input_AddES ) ( struct input_thread_s *,
547 struct pgrm_descriptor_s *, u16, size_t );
548 void ( * input_DelES ) ( struct input_thread_s *,
549 struct es_descriptor_s * );
550 int ( * input_SelectES ) ( struct input_thread_s *,
551 struct es_descriptor_s * );
552 int ( * input_UnselectES ) ( struct input_thread_s *,
553 struct es_descriptor_s * );
554 struct pgrm_descriptor_s* ( * input_AddProgram ) ( struct input_thread_s *,
556 void ( * input_DelProgram ) ( struct input_thread_s *,
557 struct pgrm_descriptor_s * );
558 struct input_area_s * ( * input_AddArea ) ( struct input_thread_s * );
559 void ( * input_DelArea ) ( struct input_thread_s *,
560 struct input_area_s * );
562 void ( * InitBitstream ) ( struct bit_stream_s *,
563 struct decoder_fifo_s *,
564 void ( * ) ( struct bit_stream_s *,
567 void ( * BitstreamNextDataPacket )( struct bit_stream_s * );
568 boolean_t ( * NextDataPacket ) ( struct decoder_fifo_s *,
569 struct data_packet_s ** );
570 void ( * DecoderError ) ( struct decoder_fifo_s * p_fifo );
571 int ( * input_InitStream ) ( struct input_thread_s *, size_t );
572 void ( * input_EndStream ) ( struct input_thread_s * );
574 void ( * input_ParsePES ) ( struct input_thread_s *,
575 struct es_descriptor_s * );
576 void ( * input_GatherPES ) ( struct input_thread_s *,
577 struct data_packet_s *,
578 struct es_descriptor_s *,
579 boolean_t, boolean_t );
580 void ( * input_DecodePES ) ( struct decoder_fifo_s *,
581 struct pes_packet_s * );
582 struct es_descriptor_s * ( * input_ParsePS ) ( struct input_thread_s *,
583 struct data_packet_s * );
584 ssize_t ( * input_ReadPS ) ( struct input_thread_s *,
585 struct data_packet_s ** );
586 void ( * input_DemuxPS ) ( struct input_thread_s *,
587 struct data_packet_s * );
588 ssize_t ( * input_ReadTS ) ( struct input_thread_s *,
589 struct data_packet_s ** );
590 void ( * input_DemuxTS ) ( struct input_thread_s *,
591 struct data_packet_s * );
592 void ( * input_DemuxPSI ) ( struct input_thread_s *,
593 struct data_packet_s *,
594 struct es_descriptor_s *,
595 boolean_t, boolean_t );
596 int ( * input_ClockManageControl ) ( struct input_thread_s *,
597 struct pgrm_descriptor_s *,
599 void ( * input_FDSeek ) ( struct input_thread_s *, off_t );
600 void ( * input_FDClose ) ( struct input_thread_s * );
601 ssize_t ( * input_FDRead ) ( struct input_thread_s *, byte_t *,
603 ssize_t ( * input_FDNetworkRead ) ( struct input_thread_s *, byte_t *,
606 void * ( * input_BuffersInit )( void );
607 void ( * input_BuffersEnd )( struct input_buffers_s * );
608 struct data_buffer_s * ( * input_NewBuffer )( struct input_buffers_s *, size_t );
609 void ( * input_ReleaseBuffer )( struct input_buffers_s *, struct data_buffer_s * );
610 struct data_packet_s * ( * input_ShareBuffer )( struct input_buffers_s *,
611 struct data_buffer_s * );
612 struct data_packet_s * ( * input_NewPacket )( struct input_buffers_s *, size_t );
613 void ( * input_DeletePacket )( struct input_buffers_s *, struct data_packet_s * );
614 struct pes_packet_s * ( * input_NewPES )( struct input_buffers_s * );
615 void ( * input_DeletePES )( struct input_buffers_s *, struct pes_packet_s * );
616 ssize_t ( * input_FillBuffer )( struct input_thread_s * );
617 ssize_t ( * input_Peek )( struct input_thread_s *, byte_t **, size_t );
618 ssize_t ( * input_SplitBuffer )( struct input_thread_s *, struct data_packet_s **, size_t );
619 int ( * input_AccessInit )( struct input_thread_s * );
620 void ( * input_AccessReinit )( struct input_thread_s * );
621 void ( * input_AccessEnd )( struct input_thread_s * );
623 struct aout_fifo_s * ( * aout_CreateFifo ) ( int, int, int, int, void * );
624 void ( * aout_DestroyFifo ) ( struct aout_fifo_s * );
626 struct vout_thread_s * (* vout_CreateThread) ( int *, int, int, u32, int );
627 void ( * vout_DestroyThread ) ( struct vout_thread_s *, int * );
629 struct picture_s * ( * vout_CreatePicture )
630 ( struct vout_thread_s *,
631 boolean_t, boolean_t, boolean_t );
632 void ( * vout_AllocatePicture )( struct picture_s *, int, int, u32 );
633 void ( * vout_DisplayPicture ) ( struct vout_thread_s *,
634 struct picture_s * );
635 void ( * vout_DestroyPicture ) ( struct vout_thread_s *,
636 struct picture_s * );
637 void ( * vout_LinkPicture ) ( struct vout_thread_s *,
638 struct picture_s * );
639 void ( * vout_UnlinkPicture ) ( struct vout_thread_s *,
640 struct picture_s * );
641 void ( * vout_DatePicture ) ( struct vout_thread_s *,
642 struct picture_s *, mtime_t );
643 void ( * vout_PlacePicture ) ( struct vout_thread_s *, int, int,
644 int *, int *, int *, int * );
646 struct subpicture_s * (* vout_CreateSubPicture)
647 ( struct vout_thread_s *, int, int );
648 void ( * vout_DestroySubPicture ) ( struct vout_thread_s *,
649 struct subpicture_s * );
650 void ( * vout_DisplaySubPicture ) ( struct vout_thread_s *,
651 struct subpicture_s * );
653 u32 ( * UnalignedShowBits ) ( struct bit_stream_s *, unsigned int );
654 void ( * UnalignedRemoveBits ) ( struct bit_stream_s * );
655 u32 ( * UnalignedGetBits ) ( struct bit_stream_s *, unsigned int );
656 void ( * CurrentPTS ) ( struct bit_stream_s *, mtime_t *,
659 char * ( * DecodeLanguage ) ( u16 );
661 struct module_s * ( * module_Need ) ( int, char *, void * );
662 void ( * module_Unneed ) ( struct module_s * );
667 extern module_symbols_t* p_symbols;