VLC 4.0.0-dev
vlc_common.h
Go to the documentation of this file.
1/*****************************************************************************
2 * vlc_common.h: common definitions
3 * Collection of useful common types and macros definitions
4 *****************************************************************************
5 * Copyright (C) 1998-2011 VLC authors and VideoLAN
6 *
7 * Authors: Samuel Hocevar <sam@via.ecp.fr>
8 * Vincent Seguin <seguin@via.ecp.fr>
9 * Gildas Bazin <gbazin@videolan.org>
10 * RĂ©mi Denis-Courmont
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
26
27#ifndef VLC_COMMON_H
28# define VLC_COMMON_H 1
29
30/**
31 * \defgroup vlc VLC plug-in programming interface
32 * \file
33 * \ingroup vlc
34 * This file is a collection of common definitions and types
35 */
36
37/*****************************************************************************
38 * Required vlc headers
39 *****************************************************************************/
40#include "vlc_config.h"
41
42/*****************************************************************************
43 * Required system headers
44 *****************************************************************************/
45#include <stdlib.h>
46#include <stdarg.h>
47#include <errno.h>
48
49#include <string.h>
50#include <stdio.h>
51#include <inttypes.h>
52#include <stddef.h>
53
54#ifndef __cplusplus
55# include <stdbool.h>
56#endif
57
58/**
59 * \defgroup cext C programming language extensions
60 * \ingroup vlc
61 *
62 * This section defines a number of macros and inline functions extending the
63 * C language. Most extensions are implemented by GCC and LLVM/Clang, and have
64 * unoptimized fallbacks for other C11/C++11 conforming compilers.
65 * @{
66 */
67#ifdef __GNUC__
68# define VLC_GCC_VERSION(maj,min) \
69 ((__GNUC__ > (maj)) || (__GNUC__ == (maj) && __GNUC_MINOR__ >= (min)))
70#else
71/** GCC version check */
72# define VLC_GCC_VERSION(maj,min) (0)
73#endif
74
75/* Function attributes for compiler warnings */
76#if defined __has_attribute
77# if __has_attribute(warning)
78# define VLC_WARN_CALL(w) VLC_NOINLINE_FUNC __attribute__((warning((w))))
79# else
80# define VLC_WARN_CALL(w)
81# endif
82
83# if __has_attribute(error)
84# define VLC_ERROR_CALL(e) VLC_NOINLINE_FUNC __attribute__((error((e))))
85# else
86# define VLC_ERROR_CALL(e)
87# endif
88
89# if __has_attribute(unused)
90# define VLC_UNUSED_FUNC __attribute__((unused))
91# else
92# define VLC_UNUSED_FUNC
93# endif
94
95# if __has_attribute(noinline)
96# define VLC_NOINLINE_FUNC __attribute__((noinline))
97# else
98# define VLC_NOINLINE_FUNC
99# endif
100
101# if __has_attribute(deprecated)
102# define VLC_DEPRECATED __attribute__((deprecated))
103# else
104/**
105 * Deprecated functions or compound members annotation
106 *
107 * Use this macro in front of a function declaration or compound member
108 * within a compound type declaration.
109 * The compiler may emit a warning every time the function or member is used.
110 *
111 * Use \ref VLC_DEPRECATED_ENUM instead for enumeration members.
112 */
113# define VLC_DEPRECATED
114# endif
115
116# if __has_attribute(malloc)
117# define VLC_MALLOC __attribute__((malloc))
118# else
119/**
120 * Heap allocated result function annotation
121 *
122 * Use this macro to annotate a function that returns a pointer to memory that
123 * cannot alias any other valid pointer.
124 *
125 * This is primarily used for functions that return a pointer to heap-allocated
126 * memory, but it can be used for other applicable purposes.
127 *
128 * \warning Do not use this annotation if the returned pointer can in any way
129 * alias a valid pointer at the time the function exits. This could lead to
130 * very weird memory corruption bugs.
131 */
132# define VLC_MALLOC
133# endif
134
135# if __has_attribute(warn_unused_result)
136# define VLC_USED __attribute__((warn_unused_result))
137# else
138/**
139 * Used result function annotation
140 *
141 * Use this macro to annotate a function whose result must be used.
142 *
143 * There are several cases where this is useful:
144 * - If a function has no side effects (or no useful side effects), such that
145 * the only useful purpose of calling said function is to obtain its
146 * return value.
147 * - If ignoring the function return value would lead to a resource leak
148 * (including but not limited to a memory leak).
149 * - If a function cannot be used correctly without checking its return value.
150 * For instance, if the function can fail at any time.
151 *
152 * The compiler may warn if the return value of a function call is ignored.
153 */
154# define VLC_USED
155# endif
156#elif defined(_MSC_VER)
157# define VLC_USED _Check_return_
158// # define VLC_MALLOC __declspec(allocator)
159# define VLC_MALLOC
160// # define VLC_DEPRECATED __declspec(deprecated)
161# define VLC_DEPRECATED
162#else // !GCC && !MSVC
163# define VLC_USED
164# define VLC_MALLOC
165# define VLC_DEPRECATED
166#endif
167
168
169#ifdef __GNUC__
170# if VLC_GCC_VERSION(6,0)
171# define VLC_DEPRECATED_ENUM __attribute__((deprecated))
172# else
173# define VLC_DEPRECATED_ENUM
174# endif
175
176# if defined( _WIN32 ) && !defined( __clang__ )
177# define VLC_FORMAT(x,y) __attribute__ ((format(gnu_printf,x,y)))
178# else
179# define VLC_FORMAT(x,y) __attribute__ ((format(printf,x,y)))
180# endif
181# define VLC_FORMAT_ARG(x) __attribute__ ((format_arg(x)))
182
183#else
184/**
185 * Deprecated enum member annotation
186 *
187 * Use this macro after an enumerated type member declaration.
188 * The compiler may emit a warning every time the enumeration member is used.
189 *
190 * See also \ref VLC_DEPRECATED.
191 */
192# define VLC_DEPRECATED_ENUM
193
194/**
195 * String format function annotation
196 *
197 * Use this macro after a function prototype/declaration if the function
198 * expects a standard C format string. This helps compiler diagnostics.
199 *
200 * @param x the position (starting from 1) of the format string argument
201 * @param y the first position (also starting from 1) of the variable arguments
202 * following the format string (usually but not always \p x+1).
203 */
204# define VLC_FORMAT(x,y)
205
206/**
207 * Format string translation function annotation
208 *
209 * Use this macro after a function prototype/declaration if the function
210 * expects a format string as input and returns another format string as output
211 * to another function.
212 *
213 * This is primarily intended for localization functions such as gettext().
214 */
215# define VLC_FORMAT_ARG(x)
216#endif
217
218#if defined (__ELF__) || defined (__MACH__) || defined (__wasm__)
219# define VLC_WEAK __attribute__((weak))
220#else
221/**
222 * Weak symbol annotation
223 *
224 * Use this macro before an external identifier \b definition to mark it as a
225 * weak symbol. A weak symbol can be overridden by another symbol of the same
226 * name at the link time.
227 */
228# define VLC_WEAK
229#endif
230
231/* Branch prediction */
232#if defined (__GNUC__) || defined (__clang__)
233# define likely(p) __builtin_expect(!!(p), 1)
234# define unlikely(p) __builtin_expect(!!(p), 0)
235# define unreachable() __builtin_unreachable()
236#elif defined(_MSC_VER)
237# define likely(p) (!!(p))
238# define unlikely(p) (!!(p))
239# define unreachable() (__assume(0))
240#else
241/**
242 * Predicted true condition
243 *
244 * This macro indicates that the condition is expected most often true.
245 * The compiler may optimize the code assuming that this condition is usually
246 * met.
247 */
248# define likely(p) (!!(p))
249
250/**
251 * Predicted false condition
252 *
253 * This macro indicates that the condition is expected most often false.
254 * The compiler may optimize the code assuming that this condition is rarely
255 * met.
256 */
257# define unlikely(p) (!!(p))
258
259/**
260 * Impossible branch
261 *
262 * This macro indicates that the branch cannot be reached at run-time, and
263 * represents undefined behaviour.
264 * The compiler may optimize the code assuming that the call flow will never
265 * logically reach the point where this macro is expanded.
266 *
267 * See also \ref vlc_assert_unreachable.
268 */
269# define unreachable() ((void)0)
270#endif
271
272/**
273 * Impossible branch assertion
274 *
275 * This macro asserts that the branch cannot be reached at run-time.
276 *
277 * If the branch is reached in a debug build, it will trigger an assertion
278 * failure and abnormal program termination.
279 *
280 * If the branch is reached in a non-debug build, this macro is equivalent to
281 * \ref unreachable and the behaviour is undefined.
282 */
283#define vlc_assert_unreachable() (vlc_assert(!"unreachable"), unreachable())
284
285/**
286 * Run-time assertion
287 *
288 * This macro performs a run-time assertion if C assertions are enabled
289 * and the following preprocessor symbol is defined:
290 * @verbatim LIBVLC_INTERNAL_ @endverbatim
291 * That restriction ensures that assertions in public header files are not
292 * unwittingly <i>leaked</i> to externally-compiled plug-ins
293 * including those header files.
294 *
295 * Within the LibVLC code base, this is exactly the same as assert(), which can
296 * and probably should be used directly instead.
297 */
298#ifdef LIBVLC_INTERNAL_
299# define vlc_assert(pred) assert(pred)
300#else
301# define vlc_assert(pred) ((void)0)
302#endif
303
304/* Linkage */
305#ifdef __cplusplus
306# define VLC_EXTERN extern "C"
307#else
308# define VLC_EXTERN
309#endif
310
311#if defined (_WIN32) && defined (VLC_DLL_EXPORT)
312# define VLC_EXPORT __declspec(dllexport)
313#elif defined (__GNUC__)
314# define VLC_EXPORT __attribute__((visibility("default")))
315#else
316# define VLC_EXPORT
317#endif
318
319/**
320 * Exported API call annotation
321 *
322 * This macro is placed before a function declaration to indicate that the
323 * function is an API call of the LibVLC plugin API.
324 */
325#define VLC_API VLC_EXTERN VLC_EXPORT
326
327/** @} */
328
329/*****************************************************************************
330 * Basic types definitions
331 *****************************************************************************/
332/**
333 * The vlc_fourcc_t type.
334 *
335 * See http://www.webartz.com/fourcc/ for a very detailed list.
336 */
337typedef uint32_t vlc_fourcc_t;
338
339#ifdef WORDS_BIGENDIAN
340# define VLC_FOURCC( a, b, c, d ) \
341 ( ((uint32_t)d) | ( ((uint32_t)c) << 8 ) \
342 | ( ((uint32_t)b) << 16 ) | ( ((uint32_t)a) << 24 ) )
343# define VLC_TWOCC( a, b ) \
344 ( (uint16_t)(b) | ( (uint16_t)(a) << 8 ) )
345
346#else
347# define VLC_FOURCC( a, b, c, d ) \
348 ( ((uint32_t)a) | ( ((uint32_t)b) << 8 ) \
349 | ( ((uint32_t)c) << 16 ) | ( ((uint32_t)d) << 24 ) )
350# define VLC_TWOCC( a, b ) \
351 ( (uint16_t)(a) | ( (uint16_t)(b) << 8 ) )
352
353#endif
354
355/**
356 * Translate a vlc_fourcc into its string representation. This function
357 * assumes there is enough room in psz_fourcc to store 4 characters in.
358 *
359 * \param fcc a vlc_fourcc_t
360 * \param psz_fourcc string to store string representation of vlc_fourcc in
361 */
362static inline void vlc_fourcc_to_char( vlc_fourcc_t fcc, char *psz_fourcc )
363{
364 memcpy( psz_fourcc, &fcc, 4 );
365}
366
367/*****************************************************************************
368 * Classes declaration
369 *****************************************************************************/
370
371/* Internal types */
374typedef struct date_t date_t;
375
376/* Playlist */
377
381
382/* Modules */
383typedef struct module_t module_t;
385
387
388/* Input */
392typedef struct stream_t stream_t;
393typedef struct stream_t demux_t;
394typedef struct es_out_t es_out_t;
397typedef struct info_t info_t;
400
401/* Format */
408
409/* Audio */
412
413/* Video */
416
418typedef struct picture_t picture_t;
419
420/* Subpictures */
421typedef struct spu_t spu_t;
424
426
427/* Stream output */
430
432
433typedef struct sout_mux_t sout_mux_t;
434
436
439
440/* Decoders */
441typedef struct decoder_t decoder_t;
442
443/* Encoders */
444typedef struct encoder_t encoder_t;
445
446/* Filters */
447typedef struct filter_t filter_t;
448
449/* Network */
450typedef struct vlc_url_t vlc_url_t;
451
452/* Misc */
454
455/* block */
456typedef struct vlc_frame_t block_t;
457typedef struct vlc_fifo_t vlc_fifo_t;
459
460/* Hashing */
462
463/* XML */
464typedef struct xml_t xml_t;
466
467/* vod server */
468typedef struct vod_t vod_t;
470
471/* VLM */
472typedef struct vlm_t vlm_t;
474
475/* misc */
476typedef struct vlc_meta_t vlc_meta_t;
479
480/* Update */
481typedef struct update_t update_t;
482
483/**
484 * VLC value structure
485 */
486typedef union
487{
488 int64_t i_int;
489 bool b_bool;
490 float f_float;
492 void * p_address;
493 struct { int32_t x; int32_t y; } coords;
494
496
497/**
498 * \defgroup errors Error codes
499 * \ingroup cext
500 * @{
501 */
502/** No error */
503#define VLC_SUCCESS 0
504/** Unspecified error */
505#define VLC_EGENERIC (-2 * (1 << (sizeof (int) * 8 - 2))) /* INT_MIN */
506/** Not enough memory */
507#define VLC_ENOMEM (-ENOMEM)
508/** Timeout */
509#define VLC_ETIMEOUT (-ETIMEDOUT)
510/** Not found */
511#define VLC_ENOENT (-ENOENT)
512/** Bad variable value */
513#define VLC_EINVAL (-EINVAL)
514/** Operation forbidden */
515#define VLC_EACCES (-EACCES)
516/** Operation not supported */
517#define VLC_ENOTSUP (-ENOTSUP)
518
519/** @} */
520
521/*****************************************************************************
522 * Variable callbacks: called when the value is modified
523 *****************************************************************************/
524typedef int ( * vlc_callback_t ) ( vlc_object_t *, /* variable's object */
525 char const *, /* variable name */
526 vlc_value_t, /* old value */
527 vlc_value_t, /* new value */
528 void * ); /* callback data */
529
530/*****************************************************************************
531 * List callbacks: called when elements are added/removed from the list
532 *****************************************************************************/
533typedef int ( * vlc_list_callback_t ) ( vlc_object_t *, /* variable's object */
534 char const *, /* variable name */
535 int, /* VLC_VAR_* action */
536 vlc_value_t *, /* new/deleted value */
537 void *); /* callback data */
538
539/*****************************************************************************
540 * OS-specific headers and thread types
541 *****************************************************************************/
542#if defined( _WIN32 )
543# include <malloc.h>
544# include <windows.h>
545#endif
546
547#ifdef __APPLE__
548#include <sys/syslimits.h>
549#include <AvailabilityMacros.h>
550#endif
551
552#ifdef __OS2__
553# define OS2EMX_PLAIN_CHAR
554# define INCL_BASE
555# define INCL_PM
556# include <os2safe.h>
557# include <os2.h>
558#endif
559
560/**
561 * \defgroup intops Integer operations
562 * \ingroup cext
563 *
564 * Common integer functions.
565 * @{
566 */
567/* __MAX and __MIN: self explanatory */
568#ifndef __MAX
569# define __MAX(a, b) ( ((a) > (b)) ? (a) : (b) )
570#endif
571#ifndef __MIN
572# define __MIN(a, b) ( ((a) < (b)) ? (a) : (b) )
573#endif
574
575/* clip v in [min, max] */
576#define VLC_CLIP(v, min, max) __MIN(__MAX((v), (min)), (max))
577
578/**
579 * Make integer v a multiple of align
580 *
581 * \note align must be a power of 2
582 */
584static inline size_t vlc_align(size_t v, size_t align)
585{
586 return (v + (align - 1)) & ~(align - 1);
587}
588
589#if defined __has_attribute
590# if __has_attribute(diagnose_if)
591static inline size_t vlc_align(size_t v, size_t align)
592 __attribute__((diagnose_if(((align & (align - 1)) || (align == 0)),
593 "align must be power of 2", "error")));
594# endif
595#endif
596
597/** Greatest common divisor */
599static inline int64_t GCD ( int64_t a, int64_t b )
600{
601 while( b )
602 {
603 int64_t c = a % b;
604 a = b;
605 b = c;
606 }
607 return a;
608}
609
610/* function imported from libavutil/common.h */
612static inline uint8_t clip_uint8_vlc( int32_t a )
613{
614 if( a&(~255) ) return (-a)>>31;
615 else return a;
616}
617
618/**
619 * \defgroup bitops Bit operations
620 * @{
621 */
622
623#define VLC_INT_FUNC(basename) \
624 VLC_INT_FUNC_TYPE(basename, unsigned, ) \
625 VLC_INT_FUNC_TYPE(basename, unsigned long, l) \
626 VLC_INT_FUNC_TYPE(basename, unsigned long long, ll)
627
628#if defined (__GNUC__) || defined (__clang__)
629# define VLC_INT_FUNC_TYPE(basename,type,suffix) \
630VLC_USED static inline int vlc_##basename##suffix(type x) \
631{ \
632 return __builtin_##basename##suffix(x); \
633}
634
636#else
637VLC_USED static inline int vlc_clzll(unsigned long long x)
638{
639 int i = sizeof (x) * 8;
640
641 while (x)
642 {
643 x >>= 1;
644 i--;
645 }
646 return i;
647}
648
649VLC_USED static inline int vlc_clzl(unsigned long x)
650{
651 return vlc_clzll(x) - ((sizeof (long long) - sizeof (long)) * 8);
652}
653
654VLC_USED static inline int vlc_clz(unsigned x)
655{
656 return vlc_clzll(x) - ((sizeof (long long) - sizeof (int)) * 8);
657}
658
659VLC_USED static inline int vlc_ctz_generic(unsigned long long x)
660{
661 unsigned i = sizeof (x) * 8;
662
663 while (x)
664 {
665 x <<= 1;
666 i--;
667 }
668 return i;
669}
670
671VLC_USED static inline int vlc_parity_generic(unsigned long long x)
672{
673 for (unsigned i = 4 * sizeof (x); i > 0; i /= 2)
674 x ^= x >> i;
675 return x & 1;
676}
677
678VLC_USED static inline int vlc_popcount_generic(unsigned long long x)
679{
680 int count = 0;
681 while (x)
682 {
683 count += x & 1;
684 x = x >> 1;
685 }
686 return count;
687}
688
689# define VLC_INT_FUNC_TYPE(basename,type,suffix) \
690VLC_USED static inline int vlc_##basename##suffix(type x) \
691{ \
692 return vlc_##basename##_generic(x); \
693}
694#endif
695
698VLC_INT_FUNC(popcount)
700#ifndef __cplusplus
701# define VLC_INT_GENERIC(func,x) \
702 _Generic((x), \
703 unsigned char: func(x), \
704 signed char: func(x), \
705 unsigned short: func(x), \
706 signed short: func(x), \
707 unsigned int: func(x), \
708 signed int: func(x), \
709 unsigned long: func##l(x), \
710 signed long: func##l(x), \
711 unsigned long long: func##ll(x), \
712 signed long long: func##ll(x))
713
714/**
715 * Count leading zeroes
716 *
717 * This function counts the number of consecutive zero (clear) bits
718 * down from the highest order bit in an unsigned integer.
719 *
720 * \param x a non-zero integer
721 * \note This macro assumes that CHAR_BIT equals 8.
722 * \warning By definition, the result depends on the (width of the) type of x.
723 * \return The number of leading zero bits in x.
724 */
725# define clz(x) \
726 _Generic((x), \
727 unsigned char: (vlc_clz(x) - (sizeof (unsigned) - 1) * 8), \
728 unsigned short: (vlc_clz(x) \
729 - (sizeof (unsigned) - sizeof (unsigned short)) * 8), \
730 unsigned: vlc_clz(x), \
731 unsigned long: vlc_clzl(x), \
732 unsigned long long: vlc_clzll(x))
733
734/**
735 * Count trailing zeroes
736 *
737 * This function counts the number of consecutive zero bits
738 * up from the lowest order bit in an unsigned integer.
739 *
740 * \param x a non-zero integer
741 * \note This function assumes that CHAR_BIT equals 8.
742 * \return The number of trailing zero bits in x.
743 */
744# define ctz(x) VLC_INT_GENERIC(vlc_ctz, x)
745
746/**
747 * Parity
748 *
749 * This function determines the parity of an integer.
750 * \retval 0 if x has an even number of set bits.
751 * \retval 1 if x has an odd number of set bits.
752 */
753# define parity(x) VLC_INT_GENERIC(vlc_parity, x)
754
755/**
756 * Bit weight / population count
757 *
758 * This function counts the number of non-zero bits in an integer.
759 *
760 * \return The count of non-zero bits.
761 */
762# define vlc_popcount(x) \
763 _Generic((x), \
764 signed char: vlc_popcount((unsigned char)(x)), \
765 signed short: vlc_popcount((unsigned short)(x)), \
766 default: VLC_INT_GENERIC(vlc_popcount ,x))
767#else
768VLC_USED static inline int vlc_popcount(unsigned char x)
769{
770 return vlc_popcount((unsigned)x);
771}
772
773VLC_USED static inline int vlc_popcount(unsigned short x)
774{
775 return vlc_popcount((unsigned)x);
776}
777
778VLC_USED static inline int vlc_popcount(unsigned long x)
779{
780 return vlc_popcountl(x);
781}
782
783VLC_USED static inline int vlc_popcount(unsigned long long x)
784{
785 return vlc_popcountll(x);
786}
787#endif
788
789/** Byte swap (16 bits) */
791static inline uint16_t vlc_bswap16(uint16_t x)
792{
793 return (x << 8) | (x >> 8);
794}
795
796/** Byte swap (32 bits) */
798static inline uint32_t vlc_bswap32(uint32_t x)
799{
800#if defined (__GNUC__) || defined(__clang__)
801 return __builtin_bswap32 (x);
802#else
803 return ((x & 0x000000FF) << 24)
804 | ((x & 0x0000FF00) << 8)
805 | ((x & 0x00FF0000) >> 8)
806 | ((x & 0xFF000000) >> 24);
807#endif
808}
809
810/** Byte swap (64 bits) */
812static inline uint64_t vlc_bswap64(uint64_t x)
813{
814#if defined (__GNUC__) || defined(__clang__)
815 return __builtin_bswap64 (x);
816#elif !defined (__cplusplus)
817 return ((x & 0x00000000000000FF) << 56)
818 | ((x & 0x000000000000FF00) << 40)
819 | ((x & 0x0000000000FF0000) << 24)
820 | ((x & 0x00000000FF000000) << 8)
821 | ((x & 0x000000FF00000000) >> 8)
822 | ((x & 0x0000FF0000000000) >> 24)
823 | ((x & 0x00FF000000000000) >> 40)
824 | ((x & 0xFF00000000000000) >> 56);
825#else
826 return ((x & 0x00000000000000FFULL) << 56)
827 | ((x & 0x000000000000FF00ULL) << 40)
828 | ((x & 0x0000000000FF0000ULL) << 24)
829 | ((x & 0x00000000FF000000ULL) << 8)
830 | ((x & 0x000000FF00000000ULL) >> 8)
831 | ((x & 0x0000FF0000000000ULL) >> 24)
832 | ((x & 0x00FF000000000000ULL) >> 40)
833 | ((x & 0xFF00000000000000ULL) >> 56);
834#endif
835}
836/** @} */
837
838/**
839 * \defgroup overflow Overflowing arithmetic
840 * @{
841 */
842static inline bool uadd_overflow(unsigned a, unsigned b, unsigned *res)
843{
844#if VLC_GCC_VERSION(5,0) || defined(__clang__)
845 return __builtin_uadd_overflow(a, b, res);
846#else
847 *res = a + b;
848 return (a + b) < a;
849#endif
851
852static inline bool uaddl_overflow(unsigned long a, unsigned long b,
853 unsigned long *res)
854{
855#if VLC_GCC_VERSION(5,0) || defined(__clang__)
856 return __builtin_uaddl_overflow(a, b, res);
857#else
858 *res = a + b;
859 return (a + b) < a;
860#endif
862
863static inline bool uaddll_overflow(unsigned long long a, unsigned long long b,
864 unsigned long long *res)
865{
866#if VLC_GCC_VERSION(5,0) || defined(__clang__)
867 return __builtin_uaddll_overflow(a, b, res);
868#else
869 *res = a + b;
870 return (a + b) < a;
871#endif
872}
873
874#ifndef __cplusplus
875/**
876 * Overflowing addition
877 *
878 * Converts \p a and \p b to the type of \p *r.
879 * Then computes the sum of both conversions while checking for overflow.
880 * Finally stores the result in \p *r.
881 *
882 * \param a an integer
883 * \param b an integer
884 * \param r a pointer to an integer [OUT]
885 * \retval false The sum did not overflow.
886 * \retval true The sum overflowed.
887 */
888# define add_overflow(a,b,r) \
889 _Generic(*(r), \
890 unsigned: uadd_overflow(a, b, (unsigned *)(r)), \
891 unsigned long: uaddl_overflow(a, b, (unsigned long *)(r)), \
892 unsigned long long: uaddll_overflow(a, b, (unsigned long long *)(r)))
893#else
894static inline bool add_overflow(unsigned a, unsigned b, unsigned *res)
895{
896 return uadd_overflow(a, b, res);
897}
898
899static inline bool add_overflow(unsigned long a, unsigned long b,
900 unsigned long *res)
901{
902 return uaddl_overflow(a, b, res);
903}
904
905static inline bool add_overflow(unsigned long long a, unsigned long long b,
906 unsigned long long *res)
907{
908 return uaddll_overflow(a, b, res);
909}
910#endif
911
912#if !(VLC_GCC_VERSION(5,0) || defined(__clang__))
913# include <limits.h>
914#endif
915
916static inline bool umul_overflow(unsigned a, unsigned b, unsigned *res)
917{
918#if VLC_GCC_VERSION(5,0) || defined(__clang__)
919 return __builtin_umul_overflow(a, b, res);
920#else
921 *res = a * b;
922 return b > 0 && a > (UINT_MAX / b);
923#endif
925
926static inline bool umull_overflow(unsigned long a, unsigned long b,
927 unsigned long *res)
928{
929#if VLC_GCC_VERSION(5,0) || defined(__clang__)
930 return __builtin_umull_overflow(a, b, res);
931#else
932 *res = a * b;
933 return b > 0 && a > (ULONG_MAX / b);
934#endif
936
937static inline bool umulll_overflow(unsigned long long a, unsigned long long b,
938 unsigned long long *res)
939{
940#if VLC_GCC_VERSION(5,0) || defined(__clang__)
941 return __builtin_umulll_overflow(a, b, res);
942#else
943 *res = a * b;
944 return b > 0 && a > (ULLONG_MAX / b);
945#endif
946}
947
948#ifndef __cplusplus
949/**
950 * Overflowing multiplication
951 *
952 * Converts \p a and \p b to the type of \p *r.
953 * Then computes the product of both conversions while checking for overflow.
954 * Finally stores the result in \p *r.
955 *
956 * \param a an integer
957 * \param b an integer
958 * \param r a pointer to an integer [OUT]
959 * \retval false The product did not overflow.
960 * \retval true The product overflowed.
961 */
962#define mul_overflow(a,b,r) \
963 _Generic(*(r), \
964 unsigned: umul_overflow(a, b, (unsigned *)(r)), \
965 unsigned long: umull_overflow(a, b, (unsigned long *)(r)), \
966 unsigned long long: umulll_overflow(a, b, (unsigned long long *)(r)))
967#else
968static inline bool mul_overflow(unsigned a, unsigned b, unsigned *res)
969{
970 return umul_overflow(a, b, res);
971}
972
973static inline bool mul_overflow(unsigned long a, unsigned long b,
974 unsigned long *res)
975{
976 return umull_overflow(a, b, res);
977}
978
979static inline bool mul_overflow(unsigned long long a, unsigned long long b,
980 unsigned long long *res)
981{
982 return umulll_overflow(a, b, res);
983}
984#endif
985/** @} */
986/** @} */
988/* Free and set set the variable to NULL */
989#define FREENULL(a) do { free( a ); a = NULL; } while(0)
990
991#define EMPTY_STR(str) (!str || !*str)
992
993#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
994
995/* MSB (big endian)/LSB (little endian) conversions - network order is always
996 * MSB, and should be used for both network communications and files. */
997
998#ifdef WORDS_BIGENDIAN
999# define hton16(i) ((uint16_t)(i))
1000# define hton32(i) ((uint32_t)(i))
1001# define hton64(i) ((uint64_t)(i))
1002#else
1003# define hton16(i) vlc_bswap16(i)
1004# define hton32(i) vlc_bswap32(i)
1005# define hton64(i) vlc_bswap64(i)
1006#endif
1007#define ntoh16(i) hton16(i)
1008#define ntoh32(i) hton32(i)
1009#define ntoh64(i) hton64(i)
1010
1011/** Reads 16 bits in network byte order */
1013static inline uint16_t U16_AT (const void *p)
1014{
1015 uint16_t x;
1016
1017 memcpy (&x, p, sizeof (x));
1018 return ntoh16 (x);
1019}
1020
1021/** Reads 32 bits in network byte order */
1023static inline uint32_t U32_AT (const void *p)
1024{
1025 uint32_t x;
1026
1027 memcpy (&x, p, sizeof (x));
1028 return ntoh32 (x);
1029}
1030
1031/** Reads 64 bits in network byte order */
1033static inline uint64_t U64_AT (const void *p)
1034{
1035 uint64_t x;
1036
1037 memcpy (&x, p, sizeof (x));
1038 return ntoh64 (x);
1041#define GetWBE(p) U16_AT(p)
1042#define GetDWBE(p) U32_AT(p)
1043#define GetQWBE(p) U64_AT(p)
1044
1045/** Reads 16 bits in little-endian order */
1047static inline uint16_t GetWLE (const void *p)
1048{
1049 uint16_t x;
1050
1051 memcpy (&x, p, sizeof (x));
1052#ifdef WORDS_BIGENDIAN
1053 x = vlc_bswap16 (x);
1054#endif
1055 return x;
1056}
1057
1058/** Reads 32 bits in little-endian order */
1060static inline uint32_t GetDWLE (const void *p)
1061{
1062 uint32_t x;
1063
1064 memcpy (&x, p, sizeof (x));
1065#ifdef WORDS_BIGENDIAN
1066 x = vlc_bswap32 (x);
1067#endif
1068 return x;
1069}
1070
1071/** Reads 64 bits in little-endian order */
1073static inline uint64_t GetQWLE (const void *p)
1074{
1075 uint64_t x;
1076
1077 memcpy (&x, p, sizeof (x));
1078#ifdef WORDS_BIGENDIAN
1079 x = vlc_bswap64 (x);
1080#endif
1081 return x;
1082}
1084/** Writes 16 bits in network byte order */
1085static inline void SetWBE (void *p, uint16_t w)
1086{
1087 w = hton16 (w);
1088 memcpy (p, &w, sizeof (w));
1089}
1091/** Writes 32 bits in network byte order */
1092static inline void SetDWBE (void *p, uint32_t dw)
1093{
1094 dw = hton32 (dw);
1095 memcpy (p, &dw, sizeof (dw));
1096}
1098/** Writes 64 bits in network byte order */
1099static inline void SetQWBE (void *p, uint64_t qw)
1100{
1101 qw = hton64 (qw);
1102 memcpy (p, &qw, sizeof (qw));
1103}
1105/** Writes 16 bits in little endian order */
1106static inline void SetWLE (void *p, uint16_t w)
1107{
1108#ifdef WORDS_BIGENDIAN
1109 w = vlc_bswap16 (w);
1110#endif
1111 memcpy (p, &w, sizeof (w));
1112}
1114/** Writes 32 bits in little endian order */
1115static inline void SetDWLE (void *p, uint32_t dw)
1116{
1117#ifdef WORDS_BIGENDIAN
1118 dw = vlc_bswap32 (dw);
1119#endif
1120 memcpy (p, &dw, sizeof (dw));
1121}
1123/** Writes 64 bits in little endian order */
1124static inline void SetQWLE (void *p, uint64_t qw)
1125{
1126#ifdef WORDS_BIGENDIAN
1127 qw = vlc_bswap64 (qw);
1128#endif
1129 memcpy (p, &qw, sizeof (qw));
1130}
1132/* */
1133#define VLC_UNUSED(x) (void)(x)
1134
1135/* Stuff defined in src/extras/libc.c */
1136
1137#if defined(_WIN32)
1138/* several type definitions */
1139# ifndef O_NONBLOCK
1140# define O_NONBLOCK 0
1141# endif
1142
1143/* the mingw32 swab() and win32 _swab() prototypes expect a char* instead of a
1144 const void* */
1145# define swab(a,b,c) _swab((char*) (a), (char*) (b), (c))
1146
1147#endif /* _WIN32 */
1148
1149typedef struct {
1150 unsigned num, den;
1152
1153VLC_API bool vlc_ureduce( unsigned *, unsigned *, uint64_t, uint64_t, uint64_t );
1154
1155#define container_of(ptr, type, member) \
1156 ((type *)(((char *)(ptr)) - offsetof(type, member)))
1159static inline void *vlc_alloc(size_t count, size_t size)
1160{
1161 return mul_overflow(count, size, &size) ? NULL : malloc(size);
1162}
1165static inline void *vlc_reallocarray(void *ptr, size_t count, size_t size)
1166{
1167 return mul_overflow(count, size, &size) ? NULL : realloc(ptr, size);
1168}
1169
1170/*****************************************************************************
1171 * I18n stuff
1172 *****************************************************************************/
1173VLC_API const char *vlc_gettext(const char *msgid) VLC_FORMAT_ARG(1);
1174VLC_API const char *vlc_ngettext(const char *s, const char *p, unsigned long n)
1176
1177#define vlc_pgettext( ctx, id ) \
1178 vlc_pgettext_aux( ctx "\004" id, id )
1181static inline const char *vlc_pgettext_aux( const char *ctx, const char *id )
1182{
1183 const char *tr = vlc_gettext( ctx );
1184 return (tr == ctx) ? id : tr;
1185}
1186
1187/*****************************************************************************
1188 * Loosy memory allocation functions. Do not use in new code.
1189 *****************************************************************************/
1190static inline void *xmalloc(size_t len)
1191{
1192 void *ptr = malloc(len);
1193 if (unlikely(ptr == NULL && len > 0))
1194 abort();
1195 return ptr;
1197
1198static inline void *xrealloc(void *ptr, size_t len)
1199{
1200 void *nptr = realloc(ptr, len);
1201 if (unlikely(nptr == NULL && len > 0))
1202 abort();
1203 return nptr;
1205
1206static inline char *xstrdup (const char *str)
1207{
1208 char *ptr = strdup (str);
1209 if (unlikely(ptr == NULL))
1210 abort ();
1211 return ptr;
1212}
1213
1214/*****************************************************************************
1215 * libvlc features
1216 *****************************************************************************/
1217VLC_API const char * VLC_CompileBy( void ) VLC_USED;
1218VLC_API const char * VLC_CompileHost( void ) VLC_USED;
1219VLC_API const char * VLC_Compiler( void ) VLC_USED;
1220
1221/*****************************************************************************
1222 * Additional vlc stuff
1223 *****************************************************************************/
1224#include "vlc_messages.h"
1225#include "vlc_objects.h"
1226#include "vlc_variables.h"
1227
1228#if defined( _WIN32 ) || defined( __OS2__ )
1229# define DIR_SEP_CHAR '\\'
1230# define DIR_SEP "\\"
1231# define PATH_SEP_CHAR ';'
1232# define PATH_SEP ";"
1233#else
1234# define DIR_SEP_CHAR '/'
1235# define DIR_SEP "/"
1236# define PATH_SEP_CHAR ':'
1237# define PATH_SEP ":"
1238#endif
1239
1240#define LICENSE_MSG \
1241 _("This program comes with NO WARRANTY, to the extent permitted by " \
1242 "law.\nYou may redistribute it under the terms of the GNU General " \
1243 "Public License;\nsee the file named COPYING for details.\n" \
1244 "Written by the VideoLAN team; see the AUTHORS file.\n")
1245
1246#if defined(__cplusplus) || defined(_MSC_VER)
1247#define ARRAY_STATIC_SIZE
1248#else
1249#define ARRAY_STATIC_SIZE static
1250#endif
1251
1252#endif /* !VLC_COMMON_H */
size_t count
Definition: core.c:403
#define p(t)
uint32_t vlc_fourcc_t
Definition: fourcc_gen.c:33
static uint32_t vlc_bswap32(uint32_t x)
Byte swap (32 bits)
Definition: vlc_common.h:796
static int vlc_clzll(unsigned long long x)
Definition: vlc_common.h:637
#define ctz(x)
Count trailing zeroes.
Definition: vlc_common.h:742
#define vlc_popcount(x)
Bit weight / population count.
Definition: vlc_common.h:760
static int vlc_popcountl(unsigned long x)
Definition: vlc_common.h:696
#define parity(x)
Parity.
Definition: vlc_common.h:751
#define clz(x)
Count leading zeroes.
Definition: vlc_common.h:723
static uint64_t vlc_bswap64(uint64_t x)
Byte swap (64 bits)
Definition: vlc_common.h:810
static int vlc_parity_generic(unsigned long long x)
Definition: vlc_common.h:671
static int vlc_clzl(unsigned long x)
Definition: vlc_common.h:649
static int vlc_clz(unsigned x)
Definition: vlc_common.h:654
static int vlc_popcountll(unsigned long long x)
Definition: vlc_common.h:696
static int vlc_popcount_generic(unsigned long long x)
Definition: vlc_common.h:678
static int vlc_ctz_generic(unsigned long long x)
Definition: vlc_common.h:659
static uint16_t vlc_bswap16(uint16_t x)
Byte swap (16 bits)
Definition: vlc_common.h:789
#define VLC_INT_FUNC(basename)
Definition: vlc_common.h:623
#define unlikely(p)
Predicted false condition.
Definition: vlc_common.h:257
#define VLC_USED
Definition: vlc_common.h:163
#define VLC_API
Exported API call annotation.
Definition: vlc_common.h:325
#define VLC_MALLOC
Definition: vlc_common.h:164
#define VLC_FORMAT_ARG(x)
Format string translation function annotation.
Definition: vlc_common.h:215
static int64_t GCD(int64_t a, int64_t b)
Greatest common divisor.
Definition: vlc_common.h:599
static uint8_t clip_uint8_vlc(int32_t a)
Definition: vlc_common.h:612
static size_t vlc_align(size_t v, size_t align)
Make integer v a multiple of align.
Definition: vlc_common.h:584
static bool umulll_overflow(unsigned long long a, unsigned long long b, unsigned long long *res)
Definition: vlc_common.h:935
static bool uaddll_overflow(unsigned long long a, unsigned long long b, unsigned long long *res)
Definition: vlc_common.h:861
static bool umull_overflow(unsigned long a, unsigned long b, unsigned long *res)
Definition: vlc_common.h:924
#define add_overflow(a, b, r)
Overflowing addition.
Definition: vlc_common.h:886
static bool umul_overflow(unsigned a, unsigned b, unsigned *res)
Definition: vlc_common.h:914
static bool uadd_overflow(unsigned a, unsigned b, unsigned *res)
Definition: vlc_common.h:840
#define mul_overflow(a, b, r)
Overflowing multiplication.
Definition: vlc_common.h:960
static bool uaddl_overflow(unsigned long a, unsigned long b, unsigned long *res)
Definition: vlc_common.h:850
Definition: vlc_addons.h:73
audio format description
Definition: vlc_es.h:82
Audio output object.
Definition: vlc_aout.h:155
Definition: vlc_config_cat.h:150
Definition: vlc_configuration.h:319
Timestamps without long-term rounding errors.
Definition: vlc_tick.h:235
Definition: vlc_codec.h:102
Definition: vlc_codec.h:255
Definition: vlc_es.h:630
Definition: es_out.c:109
Definition: vlc_es_out.h:148
Structure describing a filter.
Definition: vlc_filter.h:216
Definition: vlc_image.h:40
Definition: vlc_input_item.h:54
Definition: vlc_input_item.h:45
Definition: vlc_input.h:161
Definition: vlc_input_item.h:205
Describes an input and is used to spawn input_thread_t objects.
Definition: vlc_input_item.h:89
Definition: input_internal.h:358
Definition: vlc_input_item.h:518
Definition: vlc_iso_lang.h:31
Definition: vlc_objects.h:91
Configuration item.
Definition: vlc_configuration.h:70
Internal module descriptor.
Definition: modules.h:76
Video picture.
Definition: vlc_picture.h:130
Definition: vlc_input.h:52
Main service discovery structure to build a SD module.
Definition: vlc_services_discovery.h:60
Definition: sap.c:49
Stream output access_output.
Definition: vlc_sout.h:56
Definition: vlc_sout.h:143
Muxer structure.
Definition: vlc_sout.h:104
Definition: stream_output.c:118
Definition: vlc_sout.h:188
Subpicture unit descriptor.
Definition: vlc_spu.h:48
stream_t definition
Definition: vlc_stream.h:133
Video subtitle region.
Definition: vlc_subpicture.h:60
Video subtitle.
Definition: vlc_subpicture.h:167
subtitles format description
Definition: vlc_es.h:563
The update object.
Definition: update.h:159
video format description
Definition: vlc_es.h:352
Definition: vlc_es.h:44
Opaque structure representing an ES (Elementary Stream) track.
Definition: es_out.c:98
Internal state for block queues.
Definition: fifo.c:39
Definition: vlc_frame.h:123
MD5 hash context.
Definition: vlc_hash.h:86
Definition: meta.c:40
VLC object common members.
Definition: vlc_objects.h:45
Definition: fourcc_gen.c:34
Definition: vlc_renderer_discovery.h:166
Definition: renderer_discovery.c:36
Definition: vlc_url.h:146
Viewpoints.
Definition: vlc_viewpoint.h:41
Definition: vlc_vlm.h:178
Definition: vlm_internal.h:78
Video output thread descriptor.
Definition: vlc_vout.h:55
Definition: vlc_xml.h:67
Definition: vlc_xml.h:38
VLC value structure.
Definition: vlc_common.h:487
int64_t i_int
Definition: vlc_common.h:488
void * p_address
Definition: vlc_common.h:492
bool b_bool
Definition: vlc_common.h:489
char * psz_string
Definition: vlc_common.h:491
float f_float
Definition: vlc_common.h:490
static void * xrealloc(void *ptr, size_t len)
Definition: vlc_common.h:1196
static void SetDWBE(void *p, uint32_t dw)
Writes 32 bits in network byte order.
Definition: vlc_common.h:1090
static uint32_t GetDWLE(const void *p)
Reads 32 bits in little-endian order.
Definition: vlc_common.h:1058
static void SetWLE(void *p, uint16_t w)
Writes 16 bits in little endian order.
Definition: vlc_common.h:1104
int(* vlc_list_callback_t)(vlc_object_t *, char const *, int, vlc_value_t *, void *)
Definition: vlc_common.h:533
#define hton16(i)
Definition: vlc_common.h:1001
const char * VLC_CompileHost(void)
Definition: version.c:44
audio_format_t audio_sample_format_t
Definition: vlc_common.h:411
static uint32_t U32_AT(const void *p)
Reads 32 bits in network byte order.
Definition: vlc_common.h:1021
static uint16_t U16_AT(const void *p)
Reads 16 bits in network byte order.
Definition: vlc_common.h:1011
static char * xstrdup(const char *str)
Definition: vlc_common.h:1204
static void SetDWLE(void *p, uint32_t dw)
Writes 32 bits in little endian order.
Definition: vlc_common.h:1113
#define hton64(i)
Definition: vlc_common.h:1003
#define ntoh32(i)
Definition: vlc_common.h:1006
struct vod_t vod_t
Definition: vlc_common.h:468
int(* vlc_callback_t)(vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void *)
Definition: vlc_common.h:524
static void SetWBE(void *p, uint16_t w)
Writes 16 bits in network byte order.
Definition: vlc_common.h:1083
static void * vlc_reallocarray(void *ptr, size_t count, size_t size)
Definition: vlc_common.h:1163
static uint64_t U64_AT(const void *p)
Reads 64 bits in network byte order.
Definition: vlc_common.h:1031
const char * VLC_Compiler(void)
Definition: version.c:45
struct vod_media_t vod_media_t
Definition: vlc_common.h:469
const char * vlc_gettext(const char *msgid)
In-tree plugins share their gettext domain with LibVLC.
Definition: textdomain.c:80
static uint64_t GetQWLE(const void *p)
Reads 64 bits in little-endian order.
Definition: vlc_common.h:1071
bool vlc_ureduce(unsigned *, unsigned *, uint64_t, uint64_t, uint64_t)
static void SetQWBE(void *p, uint64_t qw)
Writes 64 bits in network byte order.
Definition: vlc_common.h:1097
static void * xmalloc(size_t len)
Definition: vlc_common.h:1188
video_format_t video_frame_format_t
Definition: vlc_common.h:417
static void * vlc_alloc(size_t count, size_t size)
Definition: vlc_common.h:1157
static void SetQWLE(void *p, uint64_t qw)
Writes 64 bits in little endian order.
Definition: vlc_common.h:1122
#define hton32(i)
Definition: vlc_common.h:1002
const char * vlc_ngettext(const char *s, const char *p, unsigned long n)
Definition: textdomain.c:89
static const char * vlc_pgettext_aux(const char *ctx, const char *id)
Definition: vlc_common.h:1179
const char * VLC_CompileBy(void)
Definition: version.c:43
#define ntoh64(i)
Definition: vlc_common.h:1007
uint32_t vlc_fourcc_t
The vlc_fourcc_t type.
Definition: vlc_common.h:337
static void vlc_fourcc_to_char(vlc_fourcc_t fcc, char *psz_fourcc)
Translate a vlc_fourcc into its string representation.
Definition: vlc_common.h:362
static uint16_t GetWLE(const void *p)
Reads 16 bits in little-endian order.
Definition: vlc_common.h:1045
#define ntoh16(i)
Definition: vlc_common.h:1005
This file defines of values used in interface, vout, aout and vlc core functions.
char * strdup(const char *)
Logging functions.
Common VLC object definitions.
VLC object variables and callbacks interface.