You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
6.3KB

  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 libavutil/internal.h
  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 "common.h"
  35. #include "mem.h"
  36. #include "timer.h"
  37. #ifndef attribute_align_arg
  38. #if (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(4,2)
  39. # define attribute_align_arg __attribute__((force_align_arg_pointer))
  40. #else
  41. # define attribute_align_arg
  42. #endif
  43. #endif
  44. #ifndef attribute_used
  45. #if AV_GCC_VERSION_AT_LEAST(3,1)
  46. # define attribute_used __attribute__((used))
  47. #else
  48. # define attribute_used
  49. #endif
  50. #endif
  51. #ifndef av_alias
  52. #if HAVE_ATTRIBUTE_MAY_ALIAS && (!defined(__ICC) || __ICC > 1110) && AV_GCC_VERSION_AT_LEAST(3,3)
  53. # define av_alias __attribute__((may_alias))
  54. #else
  55. # define av_alias
  56. #endif
  57. #endif
  58. #ifndef INT16_MIN
  59. #define INT16_MIN (-0x7fff - 1)
  60. #endif
  61. #ifndef INT16_MAX
  62. #define INT16_MAX 0x7fff
  63. #endif
  64. #ifndef INT32_MIN
  65. #define INT32_MIN (-0x7fffffff - 1)
  66. #endif
  67. #ifndef INT32_MAX
  68. #define INT32_MAX 0x7fffffff
  69. #endif
  70. #ifndef UINT32_MAX
  71. #define UINT32_MAX 0xffffffff
  72. #endif
  73. #ifndef INT64_MIN
  74. #define INT64_MIN (-0x7fffffffffffffffLL - 1)
  75. #endif
  76. #ifndef INT64_MAX
  77. #define INT64_MAX INT64_C(9223372036854775807)
  78. #endif
  79. #ifndef UINT64_MAX
  80. #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
  81. #endif
  82. #ifndef INT_BIT
  83. # define INT_BIT (CHAR_BIT * sizeof(int))
  84. #endif
  85. #ifndef offsetof
  86. # define offsetof(T, F) ((unsigned int)((char *)&((T *)0)->F))
  87. #endif
  88. /* Use to export labels from asm. */
  89. #define LABEL_MANGLE(a) EXTERN_PREFIX #a
  90. // Use rip-relative addressing if compiling PIC code on x86-64.
  91. #if ARCH_X86_64 && defined(PIC)
  92. # define LOCAL_MANGLE(a) #a "(%%rip)"
  93. #else
  94. # define LOCAL_MANGLE(a) #a
  95. #endif
  96. #define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)
  97. /* debug stuff */
  98. /* dprintf macros */
  99. #ifdef DEBUG
  100. # define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
  101. #else
  102. # define dprintf(pctx, ...)
  103. #endif
  104. #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  105. /* math */
  106. #if ARCH_X86
  107. #define MASK_ABS(mask, level)\
  108. __asm__ volatile(\
  109. "cltd \n\t"\
  110. "xorl %1, %0 \n\t"\
  111. "subl %1, %0 \n\t"\
  112. : "+a" (level), "=&d" (mask)\
  113. );
  114. #else
  115. #define MASK_ABS(mask, level)\
  116. mask = level >> 31;\
  117. level = (level ^ mask) - mask;
  118. #endif
  119. /* avoid usage of dangerous/inappropriate system functions */
  120. #undef malloc
  121. #define malloc please_use_av_malloc
  122. #undef free
  123. #define free please_use_av_free
  124. #undef realloc
  125. #define realloc please_use_av_realloc
  126. #undef time
  127. #define time time_is_forbidden_due_to_security_issues
  128. #undef rand
  129. #define rand rand_is_forbidden_due_to_state_trashing_use_av_lfg_get
  130. #undef srand
  131. #define srand srand_is_forbidden_due_to_state_trashing_use_av_lfg_init
  132. #undef random
  133. #define random random_is_forbidden_due_to_state_trashing_use_av_lfg_get
  134. #undef sprintf
  135. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  136. #undef strcat
  137. #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
  138. #undef exit
  139. #define exit exit_is_forbidden
  140. #ifndef LIBAVFORMAT_BUILD
  141. #undef printf
  142. #define printf please_use_av_log_instead_of_printf
  143. #undef fprintf
  144. #define fprintf please_use_av_log_instead_of_fprintf
  145. #undef puts
  146. #define puts please_use_av_log_instead_of_puts
  147. #undef perror
  148. #define perror please_use_av_log_instead_of_perror
  149. #endif
  150. #define FF_ALLOC_OR_GOTO(ctx, p, size, label)\
  151. {\
  152. p = av_malloc(size);\
  153. if (p == NULL && (size) != 0) {\
  154. av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
  155. goto label;\
  156. }\
  157. }
  158. #define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)\
  159. {\
  160. p = av_mallocz(size);\
  161. if (p == NULL && (size) != 0) {\
  162. av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
  163. goto label;\
  164. }\
  165. }
  166. #if !HAVE_EXP2
  167. #undef exp2
  168. #define exp2(x) exp((x) * 0.693147180559945)
  169. #endif /* HAVE_EXP2 */
  170. #if !HAVE_EXP2F
  171. #undef exp2f
  172. #define exp2f(x) ((float)exp2(x))
  173. #endif /* HAVE_EXP2F */
  174. #if !HAVE_LLRINT
  175. #undef llrint
  176. #define llrint(x) ((long long)rint(x))
  177. #endif /* HAVE_LLRINT */
  178. #if !HAVE_LOG2
  179. #undef log2
  180. #define log2(x) (log(x) * 1.44269504088896340736)
  181. #endif /* HAVE_LOG2 */
  182. #if !HAVE_LOG2F
  183. #undef log2f
  184. #define log2f(x) ((float)log2(x))
  185. #endif /* HAVE_LOG2F */
  186. #if !HAVE_LRINT
  187. static av_always_inline av_const long int lrint(double x)
  188. {
  189. return rint(x);
  190. }
  191. #endif /* HAVE_LRINT */
  192. #if !HAVE_LRINTF
  193. static av_always_inline av_const long int lrintf(float x)
  194. {
  195. return (int)(rint(x));
  196. }
  197. #endif /* HAVE_LRINTF */
  198. #if !HAVE_ROUND
  199. static av_always_inline av_const double round(double x)
  200. {
  201. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  202. }
  203. #endif /* HAVE_ROUND */
  204. #if !HAVE_ROUNDF
  205. static av_always_inline av_const float roundf(float x)
  206. {
  207. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  208. }
  209. #endif /* HAVE_ROUNDF */
  210. #if !HAVE_TRUNCF
  211. static av_always_inline av_const float truncf(float x)
  212. {
  213. return (x > 0) ? floor(x) : ceil(x);
  214. }
  215. #endif /* HAVE_TRUNCF */
  216. /**
  217. * Returns NULL if CONFIG_SMALL is true, otherwise the argument
  218. * without modification. Used to disable the definition of strings
  219. * (for example AVCodec long_names).
  220. */
  221. #if CONFIG_SMALL
  222. # define NULL_IF_CONFIG_SMALL(x) NULL
  223. #else
  224. # define NULL_IF_CONFIG_SMALL(x) x
  225. #endif
  226. #endif /* AVUTIL_INTERNAL_H */