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.

291 lines
6.8KB

  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
  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. extern const uint8_t ff_sqrt_tab[256];
  107. static inline av_const unsigned int ff_sqrt(unsigned int a)
  108. {
  109. unsigned int b;
  110. if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
  111. else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2;
  112. #if !CONFIG_SMALL
  113. else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1;
  114. else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ;
  115. #endif
  116. else {
  117. int s = av_log2_16bit(a >> 16) >> 1;
  118. unsigned int c = a >> (s + 2);
  119. b = ff_sqrt_tab[c >> (s + 8)];
  120. b = FASTDIV(c,b) + (b << s);
  121. }
  122. return b - (a < b * b);
  123. }
  124. #if ARCH_X86
  125. #define MASK_ABS(mask, level)\
  126. __asm__ volatile(\
  127. "cltd \n\t"\
  128. "xorl %1, %0 \n\t"\
  129. "subl %1, %0 \n\t"\
  130. : "+a" (level), "=&d" (mask)\
  131. );
  132. #else
  133. #define MASK_ABS(mask, level)\
  134. mask = level >> 31;\
  135. level = (level ^ mask) - mask;
  136. #endif
  137. /* avoid usage of dangerous/inappropriate system functions */
  138. #undef malloc
  139. #define malloc please_use_av_malloc
  140. #undef free
  141. #define free please_use_av_free
  142. #undef realloc
  143. #define realloc please_use_av_realloc
  144. #undef time
  145. #define time time_is_forbidden_due_to_security_issues
  146. #undef rand
  147. #define rand rand_is_forbidden_due_to_state_trashing_use_av_lfg_get
  148. #undef srand
  149. #define srand srand_is_forbidden_due_to_state_trashing_use_av_lfg_init
  150. #undef random
  151. #define random random_is_forbidden_due_to_state_trashing_use_av_lfg_get
  152. #undef sprintf
  153. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  154. #undef strcat
  155. #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
  156. #undef exit
  157. #define exit exit_is_forbidden
  158. #ifndef LIBAVFORMAT_BUILD
  159. #undef printf
  160. #define printf please_use_av_log_instead_of_printf
  161. #undef fprintf
  162. #define fprintf please_use_av_log_instead_of_fprintf
  163. #undef puts
  164. #define puts please_use_av_log_instead_of_puts
  165. #undef perror
  166. #define perror please_use_av_log_instead_of_perror
  167. #endif
  168. #define FF_ALLOC_OR_GOTO(ctx, p, size, label)\
  169. {\
  170. p = av_malloc(size);\
  171. if (p == NULL && (size) != 0) {\
  172. av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
  173. goto label;\
  174. }\
  175. }
  176. #define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)\
  177. {\
  178. p = av_mallocz(size);\
  179. if (p == NULL && (size) != 0) {\
  180. av_log(ctx, AV_LOG_ERROR, "Cannot allocate memory.\n");\
  181. goto label;\
  182. }\
  183. }
  184. #if !HAVE_EXP2
  185. #undef exp2
  186. #define exp2(x) exp((x) * 0.693147180559945)
  187. #endif /* HAVE_EXP2 */
  188. #if !HAVE_EXP2F
  189. #undef exp2f
  190. #define exp2f(x) exp2(x)
  191. #endif /* HAVE_EXP2F */
  192. #if !HAVE_LLRINT
  193. static av_always_inline av_const long long llrint(double x)
  194. {
  195. return rint(x);
  196. }
  197. #endif /* HAVE_LLRINT */
  198. #if !HAVE_LOG2
  199. #undef log2
  200. #define log2(x) (log(x) * 1.44269504088896340736)
  201. #endif /* HAVE_LOG2 */
  202. #if !HAVE_LOG2F
  203. #undef log2f
  204. #define log2f(x) log2(x)
  205. #endif /* HAVE_LOG2F */
  206. #if !HAVE_LRINT
  207. static av_always_inline av_const long int lrint(double x)
  208. {
  209. return rint(x);
  210. }
  211. #endif /* HAVE_LRINT */
  212. #if !HAVE_LRINTF
  213. static av_always_inline av_const long int lrintf(float x)
  214. {
  215. return (int)(rint(x));
  216. }
  217. #endif /* HAVE_LRINTF */
  218. #if !HAVE_ROUND
  219. static av_always_inline av_const double round(double x)
  220. {
  221. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  222. }
  223. #endif /* HAVE_ROUND */
  224. #if !HAVE_ROUNDF
  225. static av_always_inline av_const float roundf(float x)
  226. {
  227. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  228. }
  229. #endif /* HAVE_ROUNDF */
  230. #if !HAVE_TRUNCF
  231. static av_always_inline av_const float truncf(float x)
  232. {
  233. return (x > 0) ? floor(x) : ceil(x);
  234. }
  235. #endif /* HAVE_TRUNCF */
  236. /**
  237. * Returns NULL if CONFIG_SMALL is true, otherwise the argument
  238. * without modification. Used to disable the definition of strings
  239. * (for example AVCodec long_names).
  240. */
  241. #if CONFIG_SMALL
  242. # define NULL_IF_CONFIG_SMALL(x) NULL
  243. #else
  244. # define NULL_IF_CONFIG_SMALL(x) x
  245. #endif
  246. #endif /* AVUTIL_INTERNAL_H */