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.

283 lines
6.7KB

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