internal.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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 && (!defined(__ICC) || __ICC > 1200) && 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. /**
  44. * Mark a variable as used and prevent the compiler from optimizing it away.
  45. * This is useful for asm that accesses varibles in ways that the compiler does not
  46. * understand
  47. */
  48. #ifndef attribute_used
  49. #if AV_GCC_VERSION_AT_LEAST(3,1)
  50. # define attribute_used __attribute__((used))
  51. #else
  52. # define attribute_used
  53. #endif
  54. #endif
  55. #ifndef INT16_MIN
  56. #define INT16_MIN (-0x7fff - 1)
  57. #endif
  58. #ifndef INT16_MAX
  59. #define INT16_MAX 0x7fff
  60. #endif
  61. #ifndef INT32_MIN
  62. #define INT32_MIN (-0x7fffffff - 1)
  63. #endif
  64. #ifndef INT32_MAX
  65. #define INT32_MAX 0x7fffffff
  66. #endif
  67. #ifndef UINT32_MAX
  68. #define UINT32_MAX 0xffffffff
  69. #endif
  70. #ifndef INT64_MIN
  71. #define INT64_MIN (-0x7fffffffffffffffLL - 1)
  72. #endif
  73. #ifndef INT64_MAX
  74. #define INT64_MAX INT64_C(9223372036854775807)
  75. #endif
  76. #ifndef UINT64_MAX
  77. #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
  78. #endif
  79. #ifndef INT_BIT
  80. # define INT_BIT (CHAR_BIT * sizeof(int))
  81. #endif
  82. #ifndef offsetof
  83. # define offsetof(T, F) ((unsigned int)((char *)&((T *)0)->F))
  84. #endif
  85. /* Use to export labels from asm. */
  86. #define LABEL_MANGLE(a) EXTERN_PREFIX #a
  87. // Use rip-relative addressing if compiling PIC code on x86-64.
  88. #if ARCH_X86_64 && defined(PIC)
  89. # define LOCAL_MANGLE(a) #a "(%%rip)"
  90. #else
  91. # define LOCAL_MANGLE(a) #a
  92. #endif
  93. #define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)
  94. /* debug stuff */
  95. /* dprintf macros */
  96. #ifdef DEBUG
  97. # define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
  98. #else
  99. # define dprintf(pctx, ...)
  100. #endif
  101. #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  102. /* math */
  103. #if ARCH_X86
  104. #define MASK_ABS(mask, level)\
  105. __asm__ volatile(\
  106. "cltd \n\t"\
  107. "xorl %1, %0 \n\t"\
  108. "subl %1, %0 \n\t"\
  109. : "+a" (level), "=&d" (mask)\
  110. );
  111. #else
  112. #define MASK_ABS(mask, level)\
  113. mask = level >> 31;\
  114. level = (level ^ mask) - mask;
  115. #endif
  116. /* avoid usage of dangerous/inappropriate system functions */
  117. #undef malloc
  118. #define malloc please_use_av_malloc
  119. #undef free
  120. #define free please_use_av_free
  121. #undef realloc
  122. #define realloc please_use_av_realloc
  123. #undef time
  124. #define time time_is_forbidden_due_to_security_issues
  125. #undef rand
  126. #define rand rand_is_forbidden_due_to_state_trashing_use_av_lfg_get
  127. #undef srand
  128. #define srand srand_is_forbidden_due_to_state_trashing_use_av_lfg_init
  129. #undef random
  130. #define random random_is_forbidden_due_to_state_trashing_use_av_lfg_get
  131. #undef sprintf
  132. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  133. #undef strcat
  134. #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
  135. #undef exit
  136. #define exit exit_is_forbidden
  137. #ifndef LIBAVFORMAT_BUILD
  138. #undef printf
  139. #define printf please_use_av_log_instead_of_printf
  140. #undef fprintf
  141. #define fprintf please_use_av_log_instead_of_fprintf
  142. #undef puts
  143. #define puts please_use_av_log_instead_of_puts
  144. #undef perror
  145. #define perror please_use_av_log_instead_of_perror
  146. #endif
  147. #define FF_ALLOC_OR_GOTO(ctx, p, size, label)\
  148. {\
  149. p = av_malloc(size);\
  150. if (p == NULL && (size) != 0) {\
  151. av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
  152. goto label;\
  153. }\
  154. }
  155. #define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)\
  156. {\
  157. p = av_mallocz(size);\
  158. if (p == NULL && (size) != 0) {\
  159. av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
  160. goto label;\
  161. }\
  162. }
  163. #include "libm.h"
  164. /**
  165. * Return NULL if CONFIG_SMALL is true, otherwise the argument
  166. * without modification. Used to disable the definition of strings
  167. * (for example AVCodec long_names).
  168. */
  169. #if CONFIG_SMALL
  170. # define NULL_IF_CONFIG_SMALL(x) NULL
  171. #else
  172. # define NULL_IF_CONFIG_SMALL(x) x
  173. #endif
  174. /**
  175. * Define a function with only the non-default version specified.
  176. *
  177. * On systems with ELF shared libraries, all symbols exported from
  178. * FFmpeg libraries are tagged with the name and major version of the
  179. * library to which they belong. If a function is moved from one
  180. * library to another, a wrapper must be retained in the original
  181. * location to preserve binary compatibility.
  182. *
  183. * Functions defined with this macro will never be used to resolve
  184. * symbols by the build-time linker.
  185. *
  186. * @param type return type of function
  187. * @param name name of function
  188. * @param args argument list of function
  189. * @param ver version tag to assign function
  190. */
  191. #if HAVE_SYMVER_ASM_LABEL
  192. # define FF_SYMVER(type, name, args, ver) \
  193. type ff_##name args __asm__ (EXTERN_PREFIX #name "@" ver); \
  194. type ff_##name args
  195. #elif HAVE_SYMVER_GNU_ASM
  196. # define FF_SYMVER(type, name, args, ver) \
  197. __asm__ (".symver ff_" #name "," EXTERN_PREFIX #name "@" ver); \
  198. type ff_##name args; \
  199. type ff_##name args
  200. #endif
  201. #endif /* AVUTIL_INTERNAL_H */