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# if !defined(unreachable)
225# define unreachable() __builtin_unreachable()
226# endif
227#elif defined(_MSC_VER)
228# define likely(p) (!!(p))
229# define unlikely(p) (!!(p))
230# define unreachable() (__assume(0))
231#else
232/**
233 * Predicted true condition
234 *
235 * This macro indicates that the condition is expected most often true.
236 * The compiler may optimize the code assuming that this condition is usually
237 * met.
238 */
239# define likely(p) (!!(p))
240
241/**
242 * Predicted false condition
243 *
244 * This macro indicates that the condition is expected most often false.
245 * The compiler may optimize the code assuming that this condition is rarely
246 * met.
247 */
248# define unlikely(p) (!!(p))
249
250/**
251 * Impossible branch
252 *
253 * This macro indicates that the branch cannot be reached at run-time, and
254 * represents undefined behaviour.
255 * The compiler may optimize the code assuming that the call flow will never
256 * logically reach the point where this macro is expanded.
257 *
258 * See also \ref vlc_assert_unreachable.
259 */
260# define unreachable() ((void)0)
261#endif
262
263/**
264 * Impossible branch assertion
265 *
266 * This macro asserts that the branch cannot be reached at run-time.
267 *
268 * If the branch is reached in a debug build, it will trigger an assertion
269 * failure and abnormal program termination.
270 *
271 * If the branch is reached in a non-debug build, this macro is equivalent to
272 * \ref unreachable and the behaviour is undefined.
273 */
274#define vlc_assert_unreachable() (vlc_assert(!"unreachable"), unreachable())
275
276/**
277 * Run-time assertion
278 *
279 * This macro performs a run-time assertion if C assertions are enabled
280 * and the following preprocessor symbol is defined:
281 * @verbatim LIBVLC_INTERNAL_ @endverbatim
282 * That restriction ensures that assertions in public header files are not
283 * unwittingly <i>leaked</i> to externally-compiled plug-ins
284 * including those header files.
285 *
286 * Within the LibVLC code base, this is exactly the same as assert(), which can
287 * and probably should be used directly instead.
288 */
289#ifdef LIBVLC_INTERNAL_
290# define vlc_assert(pred) assert(pred)
291#else
292# define vlc_assert(pred) ((void)0)
293#endif
294
295/* Linkage */
296#ifdef __cplusplus
297# define VLC_EXTERN extern "C"
298#else
299# define VLC_EXTERN
300#endif
301
302#if defined (_WIN32) && defined (VLC_DLL_EXPORT)
303# define VLC_EXPORT __declspec(dllexport)
304#elif defined (__GNUC__)
305# define VLC_EXPORT __attribute__((visibility("default")))
306#else
307# define VLC_EXPORT
308#endif
309
310/**
311 * Exported API call annotation
312 *
313 * This macro is placed before a function declaration to indicate that the
314 * function is an API call of the LibVLC plugin API.
315 */
316#define VLC_API VLC_EXTERN VLC_EXPORT
317
318/** @} */
319
320/*****************************************************************************
321 * Basic types definitions
322 *****************************************************************************/
323/**
324 * The vlc_fourcc_t type.
325 *
326 * See http://www.webartz.com/fourcc/ for a very detailed list.
327 */
328typedef uint32_t vlc_fourcc_t;
329
330#ifdef WORDS_BIGENDIAN
331# define VLC_FOURCC( a, b, c, d ) \
332 ( ((uint32_t)d) | ( ((uint32_t)c) << 8 ) \
333 | ( ((uint32_t)b) << 16 ) | ( ((uint32_t)a) << 24 ) )
334# define VLC_TWOCC( a, b ) \
335 ( (uint16_t)(b) | ( (uint16_t)(a) << 8 ) )
336
337#else
338# define VLC_FOURCC( a, b, c, d ) \
339 ( ((uint32_t)a) | ( ((uint32_t)b) << 8 ) \
340 | ( ((uint32_t)c) << 16 ) | ( ((uint32_t)d) << 24 ) )
341# define VLC_TWOCC( a, b ) \
342 ( (uint16_t)(a) | ( (uint16_t)(b) << 8 ) )
343
344#endif
345
346/**
347 * Translate a vlc_fourcc into its string representation. This function
348 * assumes there is enough room in psz_fourcc to store 4 characters in.
349 *
350 * \param fcc a vlc_fourcc_t
351 * \param psz_fourcc string to store string representation of vlc_fourcc in
352 */
353static inline void vlc_fourcc_to_char( vlc_fourcc_t fcc, char *psz_fourcc )
354{
355 memcpy( psz_fourcc, &fcc, 4 );
356}
357
358/*****************************************************************************
359 * Classes declaration
360 *****************************************************************************/
361
362/* Internal types */
365typedef struct date_t date_t;
366
367/* Playlist */
368
372
373/* Modules */
374typedef struct module_t module_t;
376
378
379/* Input */
383typedef struct stream_t stream_t;
384typedef struct stream_t demux_t;
385typedef struct es_out_t es_out_t;
388typedef struct info_t info_t;
391
392/* Format */
399
400/* Audio */
403
404/* Video */
407
409typedef struct picture_t picture_t;
410
411/* Subpictures */
412typedef struct spu_t spu_t;
415
417
418/* Stream output */
421
423
424typedef struct sout_mux_t sout_mux_t;
425
427
430
431/* Decoders */
432typedef struct decoder_t decoder_t;
433
434/* Encoders */
435typedef struct encoder_t encoder_t;
436
437/* Filters */
438typedef struct filter_t filter_t;
439
440/* Network */
441typedef struct vlc_url_t vlc_url_t;
442
443/* Misc */
445
446/* block */
447typedef struct vlc_frame_t block_t;
448typedef struct vlc_fifo_t vlc_fifo_t;
450
451/* Hashing */
453
454/* XML */
455typedef struct xml_t xml_t;
457
458/* vod server */
459typedef struct vod_t vod_t;
461
462/* VLM */
463typedef struct vlm_t vlm_t;
465
466/* misc */
467typedef struct vlc_meta_t vlc_meta_t;
470
471/* Update */
472typedef struct update_t update_t;
473
474/**
475 * \defgroup errors Error codes
476 * \ingroup cext
477 * @{
478 */
479/** No error */
480#define VLC_SUCCESS 0
481/** Unspecified error */
482#define VLC_EGENERIC (-2 * (1 << (sizeof (int) * 8 - 2))) /* INT_MIN */
483/** Not enough memory */
484#define VLC_ENOMEM (-ENOMEM)
485/** Timeout */
486#define VLC_ETIMEOUT (-ETIMEDOUT)
487/** Not found */
488#define VLC_ENOENT (-ENOENT)
489/** Bad variable value */
490#define VLC_EINVAL (-EINVAL)
491/** Operation forbidden */
492#define VLC_EACCES (-EACCES)
493/** Operation not supported */
494#define VLC_ENOTSUP (-ENOTSUP)
495
496/** @} */
497
498/*****************************************************************************
499 * OS-specific headers and thread types
500 *****************************************************************************/
501#if defined( _WIN32 )
502# include <malloc.h>
503#endif
504
505#ifdef __APPLE__
506#include <sys/syslimits.h>
507#include <AvailabilityMacros.h>
508#endif
509
510#ifdef __OS2__
511# define OS2EMX_PLAIN_CHAR
512# define INCL_BASE
513# define INCL_PM
514# include <os2safe.h>
515# include <os2.h>
516#endif
517
518/**
519 * \defgroup intops Integer operations
520 * \ingroup cext
521 *
522 * Common integer functions.
523 * @{
524 */
525/* __MAX and __MIN: self explanatory */
526#ifndef __MAX
527# define __MAX(a, b) ( ((a) > (b)) ? (a) : (b) )
528#endif
529#ifndef __MIN
530# define __MIN(a, b) ( ((a) < (b)) ? (a) : (b) )
531#endif
532
533/* clip v in [min, max] */
534#define VLC_CLIP(v, min, max) __MIN(__MAX((v), (min)), (max))
535
536/**
537 * Make integer v a multiple of align
538 *
539 * \note align must be a power of 2
540 */
542static inline size_t vlc_align(size_t v, size_t align)
543{
544 return (v + (align - 1)) & ~(align - 1);
545}
546
547#if defined __has_attribute
548# if __has_attribute(diagnose_if)
549static inline size_t vlc_align(size_t v, size_t align)
550 __attribute__((diagnose_if(((align & (align - 1)) || (align == 0)),
551 "align must be power of 2", "error")));
552# endif
553#endif
554
555/** Greatest common divisor */
557static inline int64_t GCD ( int64_t a, int64_t b )
558{
559 while( b )
560 {
561 int64_t c = a % b;
562 a = b;
563 b = c;
564 }
565 return a;
566}
567
568/* function imported from libavutil/common.h */
570static inline uint8_t clip_uint8_vlc( int32_t a )
571{
572 if( a&(~255) ) return (-a)>>31;
573 else return a;
574}
575
576/**
577 * \defgroup bitops Bit operations
578 * @{
579 */
580
581#define VLC_INT_FUNC(basename) \
582 VLC_INT_FUNC_TYPE(basename, unsigned, ) \
583 VLC_INT_FUNC_TYPE(basename, unsigned long, l) \
584 VLC_INT_FUNC_TYPE(basename, unsigned long long, ll)
585
586#if defined (__GNUC__) || defined (__clang__)
587# define VLC_INT_FUNC_TYPE(basename,type,suffix) \
588VLC_USED static inline int vlc_##basename##suffix(type x) \
589{ \
590 return __builtin_##basename##suffix(x); \
591}
592#else
593VLC_USED static inline int vlc_ctz_generic(unsigned long long x)
594{
595 unsigned i = sizeof (x) * 8;
596
597 while (x)
598 {
599 x <<= 1;
600 i--;
601 }
602 return i;
603}
604
605VLC_USED static inline int vlc_parity_generic(unsigned long long x)
606{
607 for (unsigned i = 4 * sizeof (x); i > 0; i /= 2)
608 x ^= x >> i;
609 return x & 1;
610}
611
612VLC_USED static inline int vlc_popcount_generic(unsigned long long x)
613{
614 int count = 0;
615 while (x)
616 {
617 count += x & 1;
618 x = x >> 1;
619 }
620 return count;
621}
622
623# define VLC_INT_FUNC_TYPE(basename,type,suffix) \
624VLC_USED static inline int vlc_##basename##suffix(type x) \
625{ \
626 return vlc_##basename##_generic(x); \
627}
628#endif
629
633
634#ifndef __cplusplus
635# define VLC_INT_GENERIC(func,x) \
636 _Generic((x), \
637 unsigned char: func(x), \
638 signed char: func(x), \
639 unsigned short: func(x), \
640 signed short: func(x), \
641 unsigned int: func(x), \
642 signed int: func(x), \
643 unsigned long: func##l(x), \
644 signed long: func##l(x), \
645 unsigned long long: func##ll(x), \
646 signed long long: func##ll(x))
647
648/**
649 * Count trailing zeroes
650 *
651 * This function counts the number of consecutive zero bits
652 * up from the lowest order bit in an unsigned integer.
653 *
654 * \param x a non-zero integer
655 * \note This function assumes that CHAR_BIT equals 8.
656 * \return The number of trailing zero bits in x.
657 */
658# define ctz(x) VLC_INT_GENERIC(vlc_ctz, x)
659
660/**
661 * Parity
662 *
663 * This function determines the parity of an integer.
664 * \retval 0 if x has an even number of set bits.
665 * \retval 1 if x has an odd number of set bits.
666 */
667# define parity(x) VLC_INT_GENERIC(vlc_parity, x)
668
669/**
670 * Bit weight / population count
671 *
672 * This function counts the number of non-zero bits in an integer.
673 *
674 * \return The count of non-zero bits.
675 */
676# define vlc_popcount(x) \
677 _Generic((x), \
678 signed char: vlc_popcount((unsigned char)(x)), \
679 signed short: vlc_popcount((unsigned short)(x)), \
680 default: VLC_INT_GENERIC(vlc_popcount ,x))
681#else
682VLC_USED static inline int vlc_popcount(unsigned char x)
683{
684 return vlc_popcount((unsigned)x);
685}
686
687VLC_USED static inline int vlc_popcount(unsigned short x)
688{
689 return vlc_popcount((unsigned)x);
690}
691
692VLC_USED static inline int vlc_popcount(unsigned long x)
693{
694 return vlc_popcountl(x);
695}
696
697VLC_USED static inline int vlc_popcount(unsigned long long x)
698{
699 return vlc_popcountll(x);
700}
701#endif
702
703/** Byte swap (16 bits) */
705static inline uint16_t vlc_bswap16(uint16_t x)
706{
707 return (x << 8) | (x >> 8);
708}
709
710/** Byte swap (32 bits) */
712static inline uint32_t vlc_bswap32(uint32_t x)
713{
714#if defined (__GNUC__) || defined(__clang__)
715 return __builtin_bswap32 (x);
716#else
717 return ((x & 0x000000FF) << 24)
718 | ((x & 0x0000FF00) << 8)
719 | ((x & 0x00FF0000) >> 8)
720 | ((x & 0xFF000000) >> 24);
721#endif
722}
723
724/** Byte swap (64 bits) */
726static inline uint64_t vlc_bswap64(uint64_t x)
727{
728#if defined (__GNUC__) || defined(__clang__)
729 return __builtin_bswap64 (x);
730#elif !defined (__cplusplus)
731 return ((x & 0x00000000000000FF) << 56)
732 | ((x & 0x000000000000FF00) << 40)
733 | ((x & 0x0000000000FF0000) << 24)
734 | ((x & 0x00000000FF000000) << 8)
735 | ((x & 0x000000FF00000000) >> 8)
736 | ((x & 0x0000FF0000000000) >> 24)
737 | ((x & 0x00FF000000000000) >> 40)
738 | ((x & 0xFF00000000000000) >> 56);
739#else
740 return ((x & 0x00000000000000FFULL) << 56)
741 | ((x & 0x000000000000FF00ULL) << 40)
742 | ((x & 0x0000000000FF0000ULL) << 24)
743 | ((x & 0x00000000FF000000ULL) << 8)
744 | ((x & 0x000000FF00000000ULL) >> 8)
745 | ((x & 0x0000FF0000000000ULL) >> 24)
746 | ((x & 0x00FF000000000000ULL) >> 40)
747 | ((x & 0xFF00000000000000ULL) >> 56);
748#endif
749}
750/** @} */
751
752/**
753 * \defgroup overflow Overflowing arithmetic
754 * @{
755 */
756static inline bool uadd_overflow(unsigned a, unsigned b, unsigned *res)
757{
758#if defined(__GNUC__) || defined(__clang__)
759 return __builtin_uadd_overflow(a, b, res);
760#else
761 *res = a + b;
762 return (a + b) < a;
763#endif
764}
765
766static inline bool uaddl_overflow(unsigned long a, unsigned long b,
767 unsigned long *res)
768{
769#if defined(__GNUC__) || defined(__clang__)
770 return __builtin_uaddl_overflow(a, b, res);
771#else
772 *res = a + b;
773 return (a + b) < a;
774#endif
775}
776
777static inline bool uaddll_overflow(unsigned long long a, unsigned long long b,
778 unsigned long long *res)
779{
780#if defined(__GNUC__) || defined(__clang__)
781 return __builtin_uaddll_overflow(a, b, res);
782#else
783 *res = a + b;
784 return (a + b) < a;
785#endif
786}
787
788#ifndef __cplusplus
789/**
790 * Overflowing addition
791 *
792 * Converts \p a and \p b to the type of \p *r.
793 * Then computes the sum of both conversions while checking for overflow.
794 * Finally stores the result in \p *r.
795 *
796 * \param a an integer
797 * \param b an integer
798 * \param r a pointer to an integer [OUT]
799 * \retval false The sum did not overflow.
800 * \retval true The sum overflowed.
801 */
802# define add_overflow(a,b,r) \
803 _Generic(*(r), \
804 unsigned: uadd_overflow(a, b, (unsigned *)(r)), \
805 unsigned long: uaddl_overflow(a, b, (unsigned long *)(r)), \
806 unsigned long long: uaddll_overflow(a, b, (unsigned long long *)(r)))
807#else
808static inline bool add_overflow(unsigned a, unsigned b, unsigned *res)
809{
810 return uadd_overflow(a, b, res);
811}
812
813static inline bool add_overflow(unsigned long a, unsigned long b,
814 unsigned long *res)
815{
816 return uaddl_overflow(a, b, res);
817}
818
819static inline bool add_overflow(unsigned long long a, unsigned long long b,
820 unsigned long long *res)
821{
822 return uaddll_overflow(a, b, res);
823}
824#endif
825
826#if !(defined(__GNUC__) || defined(__clang__))
827# include <limits.h>
828#endif
829
830static inline bool umul_overflow(unsigned a, unsigned b, unsigned *res)
831{
832#if defined(__GNUC__) || defined(__clang__)
833 return __builtin_umul_overflow(a, b, res);
834#else
835 *res = a * b;
836 return b > 0 && a > (UINT_MAX / b);
837#endif
838}
839
840static inline bool umull_overflow(unsigned long a, unsigned long b,
841 unsigned long *res)
842{
843#if defined(__GNUC__) || defined(__clang__)
844 return __builtin_umull_overflow(a, b, res);
845#else
846 *res = a * b;
847 return b > 0 && a > (ULONG_MAX / b);
848#endif
849}
850
851static inline bool umulll_overflow(unsigned long long a, unsigned long long b,
852 unsigned long long *res)
853{
854#if defined(__GNUC__) || defined(__clang__)
855 return __builtin_umulll_overflow(a, b, res);
856#else
857 *res = a * b;
858 return b > 0 && a > (ULLONG_MAX / b);
859#endif
860}
861
862#ifndef __cplusplus
863/**
864 * Overflowing multiplication
865 *
866 * Converts \p a and \p b to the type of \p *r.
867 * Then computes the product of both conversions while checking for overflow.
868 * Finally stores the result in \p *r.
869 *
870 * \param a an integer
871 * \param b an integer
872 * \param r a pointer to an integer [OUT]
873 * \retval false The product did not overflow.
874 * \retval true The product overflowed.
875 */
876#define mul_overflow(a,b,r) \
877 _Generic(*(r), \
878 unsigned: umul_overflow(a, b, (unsigned *)(r)), \
879 unsigned long: umull_overflow(a, b, (unsigned long *)(r)), \
880 unsigned long long: umulll_overflow(a, b, (unsigned long long *)(r)))
881#else
882static inline bool mul_overflow(unsigned a, unsigned b, unsigned *res)
883{
884 return umul_overflow(a, b, res);
885}
886
887static inline bool mul_overflow(unsigned long a, unsigned long b,
888 unsigned long *res)
889{
890 return umull_overflow(a, b, res);
891}
892
893static inline bool mul_overflow(unsigned long long a, unsigned long long b,
894 unsigned long long *res)
895{
896 return umulll_overflow(a, b, res);
897}
898#endif
899/** @} */
900/** @} */
901
902/* Free and set set the variable to NULL */
903#define FREENULL(a) do { free( a ); a = NULL; } while(0)
904
905#define EMPTY_STR(str) (!str || !*str)
906
907#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
908
909/* MSB (big endian)/LSB (little endian) conversions - network order is always
910 * MSB, and should be used for both network communications and files. */
911
912#ifdef WORDS_BIGENDIAN
913# define hton16(i) ((uint16_t)(i))
914# define hton32(i) ((uint32_t)(i))
915# define hton64(i) ((uint64_t)(i))
916#else
917# define hton16(i) vlc_bswap16(i)
918# define hton32(i) vlc_bswap32(i)
919# define hton64(i) vlc_bswap64(i)
920#endif
921#define ntoh16(i) hton16(i)
922#define ntoh32(i) hton32(i)
923#define ntoh64(i) hton64(i)
924
925/** Reads 16 bits in network byte order */
927static inline uint16_t U16_AT (const void *p)
928{
929 uint16_t x;
930
931 memcpy (&x, p, sizeof (x));
932 return ntoh16 (x);
933}
934
935/** Reads 32 bits in network byte order */
937static inline uint32_t U32_AT (const void *p)
938{
939 uint32_t x;
940
941 memcpy (&x, p, sizeof (x));
942 return ntoh32 (x);
943}
944
945/** Reads 64 bits in network byte order */
947static inline uint64_t U64_AT (const void *p)
948{
949 uint64_t x;
950
951 memcpy (&x, p, sizeof (x));
952 return ntoh64 (x);
953}
954
955#define GetWBE(p) U16_AT(p)
956#define GetDWBE(p) U32_AT(p)
957#define GetQWBE(p) U64_AT(p)
958
959/** Reads 16 bits in little-endian order */
961static inline uint16_t GetWLE (const void *p)
962{
963 uint16_t x;
964
965 memcpy (&x, p, sizeof (x));
966#ifdef WORDS_BIGENDIAN
967 x = vlc_bswap16 (x);
968#endif
969 return x;
970}
971
972/** Reads 32 bits in little-endian order */
974static inline uint32_t GetDWLE (const void *p)
975{
976 uint32_t x;
977
978 memcpy (&x, p, sizeof (x));
979#ifdef WORDS_BIGENDIAN
980 x = vlc_bswap32 (x);
981#endif
982 return x;
983}
984
985/** Reads 64 bits in little-endian order */
987static inline uint64_t GetQWLE (const void *p)
988{
989 uint64_t x;
990
991 memcpy (&x, p, sizeof (x));
992#ifdef WORDS_BIGENDIAN
993 x = vlc_bswap64 (x);
994#endif
995 return x;
996}
997
998/** Writes 16 bits in network byte order */
999static inline void SetWBE (void *p, uint16_t w)
1000{
1001 w = hton16 (w);
1002 memcpy (p, &w, sizeof (w));
1003}
1004
1005/** Writes 32 bits in network byte order */
1006static inline void SetDWBE (void *p, uint32_t dw)
1007{
1008 dw = hton32 (dw);
1009 memcpy (p, &dw, sizeof (dw));
1010}
1011
1012/** Writes 64 bits in network byte order */
1013static inline void SetQWBE (void *p, uint64_t qw)
1014{
1015 qw = hton64 (qw);
1016 memcpy (p, &qw, sizeof (qw));
1017}
1018
1019/** Writes 16 bits in little endian order */
1020static inline void SetWLE (void *p, uint16_t w)
1021{
1022#ifdef WORDS_BIGENDIAN
1023 w = vlc_bswap16 (w);
1024#endif
1025 memcpy (p, &w, sizeof (w));
1026}
1027
1028/** Writes 32 bits in little endian order */
1029static inline void SetDWLE (void *p, uint32_t dw)
1030{
1031#ifdef WORDS_BIGENDIAN
1032 dw = vlc_bswap32 (dw);
1033#endif
1034 memcpy (p, &dw, sizeof (dw));
1035}
1036
1037/** Writes 64 bits in little endian order */
1038static inline void SetQWLE (void *p, uint64_t qw)
1039{
1040#ifdef WORDS_BIGENDIAN
1041 qw = vlc_bswap64 (qw);
1042#endif
1043 memcpy (p, &qw, sizeof (qw));
1044}
1045
1046/* */
1047#define VLC_UNUSED(x) (void)(x)
1048
1049/* Stuff defined in src/extras/libc.c */
1050
1051#if defined(_WIN32)
1052/* several type definitions */
1053# ifndef O_NONBLOCK
1054# define O_NONBLOCK 0
1055# endif
1056
1057/* the mingw32 swab() and win32 _swab() prototypes expect a char* instead of a
1058 const void* */
1059# define swab(a,b,c) _swab((char*) (a), (char*) (b), (c))
1060
1061#endif /* _WIN32 */
1062
1063typedef struct {
1064 unsigned num, den;
1066
1067VLC_API bool vlc_ureduce( unsigned *, unsigned *, uint64_t, uint64_t, uint64_t );
1068
1069#define container_of(ptr, type, member) \
1070 ((type *)(((char *)(ptr)) - offsetof(type, member)))
1071
1073static inline void *vlc_alloc(size_t count, size_t size)
1074{
1075 return mul_overflow(count, size, &size) ? NULL : malloc(size);
1076}
1077
1079static inline void *vlc_reallocarray(void *ptr, size_t count, size_t size)
1080{
1081 return mul_overflow(count, size, &size) ? NULL : realloc(ptr, size);
1082}
1083
1084/*****************************************************************************
1085 * I18n stuff
1086 *****************************************************************************/
1087VLC_API const char *vlc_gettext(const char *msgid) VLC_FORMAT_ARG(1);
1088VLC_API const char *vlc_ngettext(const char *s, const char *p, unsigned long n)
1090
1091#define vlc_pgettext( ctx, id ) \
1092 vlc_pgettext_aux( ctx "\004" id, id )
1093
1095static inline const char *vlc_pgettext_aux( const char *ctx, const char *id )
1096{
1097 const char *tr = vlc_gettext( ctx );
1098 return (tr == ctx) ? id : tr;
1099}
1100
1101/*****************************************************************************
1102 * Loosy memory allocation functions. Do not use in new code.
1103 *****************************************************************************/
1104static inline void *xmalloc(size_t len)
1105{
1106 void *ptr = malloc(len);
1107 if (unlikely(ptr == NULL && len > 0))
1108 abort();
1109 return ptr;
1110}
1111
1112static inline void *xrealloc(void *ptr, size_t len)
1113{
1114 void *nptr = realloc(ptr, len);
1115 if (unlikely(nptr == NULL && len > 0))
1116 abort();
1117 return nptr;
1118}
1119
1120static inline char *xstrdup (const char *str)
1121{
1122 char *ptr = strdup (str);
1123 if (unlikely(ptr == NULL))
1124 abort ();
1125 return ptr;
1126}
1127
1128/*****************************************************************************
1129 * libvlc features
1130 *****************************************************************************/
1131VLC_API const char * VLC_CompileBy( void ) VLC_USED;
1132VLC_API const char * VLC_CompileHost( void ) VLC_USED;
1133VLC_API const char * VLC_Compiler( void ) VLC_USED;
1134
1135/*****************************************************************************
1136 * Additional vlc stuff
1137 *****************************************************************************/
1138#include "vlc_messages.h"
1139#include "vlc_objects.h"
1140#include "vlc_variables.h"
1141
1142#if defined( _WIN32 ) || defined( __OS2__ )
1143# define DIR_SEP_CHAR '\\'
1144# define DIR_SEP "\\"
1145# define PATH_SEP_CHAR ';'
1146# define PATH_SEP ";"
1147#else
1148# define DIR_SEP_CHAR '/'
1149# define DIR_SEP "/"
1150# define PATH_SEP_CHAR ':'
1151# define PATH_SEP ":"
1152#endif
1153
1154#define LICENSE_MSG \
1155 _("This program comes with NO WARRANTY, to the extent permitted by " \
1156 "law.\nYou may redistribute it under the terms of the GNU General " \
1157 "Public License;\nsee the file named COPYING for details.\n" \
1158 "Written by the VideoLAN team; see the AUTHORS file.\n")
1159
1160#if defined(__cplusplus) || defined(_MSC_VER)
1161#define ARRAY_STATIC_SIZE
1162#else
1163#define ARRAY_STATIC_SIZE static
1164#endif
1165
1166#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:712
#define ctz(x)
Count trailing zeroes.
Definition vlc_common.h:658
#define vlc_popcount(x)
Bit weight / population count.
Definition vlc_common.h:676
static int vlc_popcountl(unsigned long x)
Definition vlc_common.h:632
#define parity(x)
Parity.
Definition vlc_common.h:667
static uint64_t vlc_bswap64(uint64_t x)
Byte swap (64 bits)
Definition vlc_common.h:726
static int vlc_parity_generic(unsigned long long x)
Definition vlc_common.h:605
static int vlc_popcountll(unsigned long long x)
Definition vlc_common.h:632
static int vlc_popcount_generic(unsigned long long x)
Definition vlc_common.h:612
static int vlc_ctz_generic(unsigned long long x)
Definition vlc_common.h:593
static uint16_t vlc_bswap16(uint16_t x)
Byte swap (16 bits)
Definition vlc_common.h:705
#define VLC_INT_FUNC(basename)
Definition vlc_common.h:581
#define unlikely(p)
Predicted false condition.
Definition vlc_common.h:248
#define VLC_USED
Definition vlc_common.h:156
#define VLC_API
Exported API call annotation.
Definition vlc_common.h:316
#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:557
static uint8_t clip_uint8_vlc(int32_t a)
Definition vlc_common.h:570
static size_t vlc_align(size_t v, size_t align)
Make integer v a multiple of align.
Definition vlc_common.h:542
static bool umulll_overflow(unsigned long long a, unsigned long long b, unsigned long long *res)
Definition vlc_common.h:851
static bool uaddll_overflow(unsigned long long a, unsigned long long b, unsigned long long *res)
Definition vlc_common.h:777
static bool umull_overflow(unsigned long a, unsigned long b, unsigned long *res)
Definition vlc_common.h:840
#define add_overflow(a, b, r)
Overflowing addition.
Definition vlc_common.h:802
static bool umul_overflow(unsigned a, unsigned b, unsigned *res)
Definition vlc_common.h:830
static bool uadd_overflow(unsigned a, unsigned b, unsigned *res)
Definition vlc_common.h:756
#define mul_overflow(a, b, r)
Overflowing multiplication.
Definition vlc_common.h:876
static bool uaddl_overflow(unsigned long a, unsigned long b, unsigned long *res)
Definition vlc_common.h:766
Definition vlc_addons.h:73
audio format description
Definition vlc_es.h:64
Audio output object.
Definition vlc_aout.h:155
Definition vlc_config_cat.h:152
Definition vlc_configuration.h:340
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:614
Definition es_out.c:116
Definition vlc_es_out.h:140
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:168
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:128
Definition vlc_input.h:51
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:51
stream_t definition
Definition vlc_stream.h:135
Video subtitle region.
Definition vlc_subpicture.h:71
Video subtitle.
Definition vlc_subpicture.h:248
subtitles format description
Definition vlc_es.h:548
The update object.
Definition update.h:159
video format description
Definition vlc_es.h:337
Definition vlc_es.h:46
Opaque structure representing an ES (Elementary Stream) track.
Definition es_out.c:105
Internal state for block queues.
Definition fifo.c:39
Definition vlc_frame.h:122
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:1112
static void SetDWBE(void *p, uint32_t dw)
Writes 32 bits in network byte order.
Definition vlc_common.h:1006
static uint32_t GetDWLE(const void *p)
Reads 32 bits in little-endian order.
Definition vlc_common.h:974
static void SetWLE(void *p, uint16_t w)
Writes 16 bits in little endian order.
Definition vlc_common.h:1020
#define hton16(i)
Definition vlc_common.h:917
const char * VLC_CompileHost(void)
Definition version.c:44
audio_format_t audio_sample_format_t
Definition vlc_common.h:402
static uint32_t U32_AT(const void *p)
Reads 32 bits in network byte order.
Definition vlc_common.h:937
static uint16_t U16_AT(const void *p)
Reads 16 bits in network byte order.
Definition vlc_common.h:927
static char * xstrdup(const char *str)
Definition vlc_common.h:1120
static void SetDWLE(void *p, uint32_t dw)
Writes 32 bits in little endian order.
Definition vlc_common.h:1029
#define hton64(i)
Definition vlc_common.h:919
#define ntoh32(i)
Definition vlc_common.h:922
struct vod_t vod_t
Definition vlc_common.h:459
static void SetWBE(void *p, uint16_t w)
Writes 16 bits in network byte order.
Definition vlc_common.h:999
static void * vlc_reallocarray(void *ptr, size_t count, size_t size)
Definition vlc_common.h:1079
static uint64_t U64_AT(const void *p)
Reads 64 bits in network byte order.
Definition vlc_common.h:947
const char * VLC_Compiler(void)
Definition version.c:45
struct vod_media_t vod_media_t
Definition vlc_common.h:460
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:987
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:1013
static void * xmalloc(size_t len)
Definition vlc_common.h:1104
video_format_t video_frame_format_t
Definition vlc_common.h:408
static void * vlc_alloc(size_t count, size_t size)
Definition vlc_common.h:1073
static void SetQWLE(void *p, uint64_t qw)
Writes 64 bits in little endian order.
Definition vlc_common.h:1038
#define hton32(i)
Definition vlc_common.h:918
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:1095
const char * VLC_CompileBy(void)
Definition version.c:43
#define ntoh64(i)
Definition vlc_common.h:923
uint32_t vlc_fourcc_t
The vlc_fourcc_t type.
Definition vlc_common.h:328
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:353
static uint16_t GetWLE(const void *p)
Reads 16 bits in little-endian order.
Definition vlc_common.h:961
#define ntoh16(i)
Definition vlc_common.h:921
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.