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