internal.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * common internal API header
  23. */
  24. #ifndef AVUTIL_INTERNAL_H
  25. #define AVUTIL_INTERNAL_H
  26. #if !defined(DEBUG) && !defined(NDEBUG)
  27. # define NDEBUG
  28. #endif
  29. #include <limits.h>
  30. #include <stdint.h>
  31. #include <stddef.h>
  32. #include <assert.h>
  33. #include "config.h"
  34. #include "attributes.h"
  35. #include "timer.h"
  36. #ifndef attribute_align_arg
  37. #if ARCH_X86_32 && AV_GCC_VERSION_AT_LEAST(4,2)
  38. # define attribute_align_arg __attribute__((force_align_arg_pointer))
  39. #else
  40. # define attribute_align_arg
  41. #endif
  42. #endif
  43. #ifndef INT16_MIN
  44. #define INT16_MIN (-0x7fff - 1)
  45. #endif
  46. #ifndef INT16_MAX
  47. #define INT16_MAX 0x7fff
  48. #endif
  49. #ifndef INT32_MIN
  50. #define INT32_MIN (-0x7fffffff - 1)
  51. #endif
  52. #ifndef INT32_MAX
  53. #define INT32_MAX 0x7fffffff
  54. #endif
  55. #ifndef UINT32_MAX
  56. #define UINT32_MAX 0xffffffff
  57. #endif
  58. #ifndef INT64_MIN
  59. #define INT64_MIN (-0x7fffffffffffffffLL - 1)
  60. #endif
  61. #ifndef INT64_MAX
  62. #define INT64_MAX INT64_C(9223372036854775807)
  63. #endif
  64. #ifndef UINT64_MAX
  65. #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
  66. #endif
  67. #ifndef INT_BIT
  68. # define INT_BIT (CHAR_BIT * sizeof(int))
  69. #endif
  70. #ifndef offsetof
  71. # define offsetof(T, F) ((unsigned int)((char *)&((T *)0)->F))
  72. #endif
  73. /* Use to export labels from asm. */
  74. #define LABEL_MANGLE(a) EXTERN_PREFIX #a
  75. // Use rip-relative addressing if compiling PIC code on x86-64.
  76. #if ARCH_X86_64 && defined(PIC)
  77. # define LOCAL_MANGLE(a) #a "(%%rip)"
  78. #else
  79. # define LOCAL_MANGLE(a) #a
  80. #endif
  81. #define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)
  82. /* debug stuff */
  83. #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  84. /* math */
  85. #if ARCH_X86
  86. #define MASK_ABS(mask, level)\
  87. __asm__ volatile(\
  88. "cltd \n\t"\
  89. "xorl %1, %0 \n\t"\
  90. "subl %1, %0 \n\t"\
  91. : "+a" (level), "=&d" (mask)\
  92. );
  93. #else
  94. #define MASK_ABS(mask, level)\
  95. mask = level >> 31;\
  96. level = (level ^ mask) - mask;
  97. #endif
  98. /* avoid usage of dangerous/inappropriate system functions */
  99. #undef malloc
  100. #define malloc please_use_av_malloc
  101. #undef free
  102. #define free please_use_av_free
  103. #undef realloc
  104. #define realloc please_use_av_realloc
  105. #undef time
  106. #define time time_is_forbidden_due_to_security_issues
  107. #undef rand
  108. #define rand rand_is_forbidden_due_to_state_trashing_use_av_lfg_get
  109. #undef srand
  110. #define srand srand_is_forbidden_due_to_state_trashing_use_av_lfg_init
  111. #undef random
  112. #define random random_is_forbidden_due_to_state_trashing_use_av_lfg_get
  113. #undef sprintf
  114. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  115. #undef strcat
  116. #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
  117. #undef exit
  118. #define exit exit_is_forbidden
  119. #ifndef LIBAVFORMAT_BUILD
  120. #undef printf
  121. #define printf please_use_av_log_instead_of_printf
  122. #undef fprintf
  123. #define fprintf please_use_av_log_instead_of_fprintf
  124. #undef puts
  125. #define puts please_use_av_log_instead_of_puts
  126. #undef perror
  127. #define perror please_use_av_log_instead_of_perror
  128. #endif
  129. #define FF_ALLOC_OR_GOTO(ctx, p, size, label)\
  130. {\
  131. p = av_malloc(size);\
  132. if (p == NULL && (size) != 0) {\
  133. av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
  134. goto label;\
  135. }\
  136. }
  137. #define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)\
  138. {\
  139. p = av_mallocz(size);\
  140. if (p == NULL && (size) != 0) {\
  141. av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
  142. goto label;\
  143. }\
  144. }
  145. #include "libm.h"
  146. /**
  147. * Return NULL if CONFIG_SMALL is true, otherwise the argument
  148. * without modification. Used to disable the definition of strings
  149. * (for example AVCodec long_names).
  150. */
  151. #if CONFIG_SMALL
  152. # define NULL_IF_CONFIG_SMALL(x) NULL
  153. #else
  154. # define NULL_IF_CONFIG_SMALL(x) x
  155. #endif
  156. /**
  157. * Define a function with only the non-default version specified.
  158. *
  159. * On systems with ELF shared libraries, all symbols exported from
  160. * Libav libraries are tagged with the name and major version of the
  161. * library to which they belong. If a function is moved from one
  162. * library to another, a wrapper must be retained in the original
  163. * location to preserve binary compatibility.
  164. *
  165. * Functions defined with this macro will never be used to resolve
  166. * symbols by the build-time linker.
  167. *
  168. * @param type return type of function
  169. * @param name name of function
  170. * @param args argument list of function
  171. * @param ver version tag to assign function
  172. */
  173. #if HAVE_SYMVER_ASM_LABEL
  174. # define FF_SYMVER(type, name, args, ver) \
  175. type ff_##name args __asm__ (EXTERN_PREFIX #name "@" ver); \
  176. type ff_##name args
  177. #elif HAVE_SYMVER_GNU_ASM
  178. # define FF_SYMVER(type, name, args, ver) \
  179. __asm__ (".symver ff_" #name "," EXTERN_PREFIX #name "@" ver); \
  180. type ff_##name args; \
  181. type ff_##name args
  182. #endif
  183. /**
  184. * Returns NULL if a threading library has not been enabled.
  185. * Used to disable threading functions in AVCodec definitions
  186. * when not needed.
  187. */
  188. #if HAVE_THREADS
  189. # define ONLY_IF_THREADS_ENABLED(x) x
  190. #else
  191. # define ONLY_IF_THREADS_ENABLED(x) NULL
  192. #endif
  193. #endif /* AVUTIL_INTERNAL_H */