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.

327 lines
7.9KB

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