internal.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. 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) ((float)exp2(x))
  191. #endif /* HAVE_EXP2F */
  192. #if !HAVE_LLRINT
  193. #undef llrint
  194. #define llrint(x) ((long long)rint(x))
  195. #endif /* HAVE_LLRINT */
  196. #if !HAVE_LOG2
  197. #undef log2
  198. #define log2(x) (log(x) * 1.44269504088896340736)
  199. #endif /* HAVE_LOG2 */
  200. #if !HAVE_LOG2F
  201. #undef log2f
  202. #define log2f(x) ((float)log2(x))
  203. #endif /* HAVE_LOG2F */
  204. #if !HAVE_LRINT
  205. static av_always_inline av_const long int lrint(double x)
  206. {
  207. return rint(x);
  208. }
  209. #endif /* HAVE_LRINT */
  210. #if !HAVE_LRINTF
  211. static av_always_inline av_const long int lrintf(float x)
  212. {
  213. return (int)(rint(x));
  214. }
  215. #endif /* HAVE_LRINTF */
  216. #if !HAVE_ROUND
  217. static av_always_inline av_const double round(double x)
  218. {
  219. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  220. }
  221. #endif /* HAVE_ROUND */
  222. #if !HAVE_ROUNDF
  223. static av_always_inline av_const float roundf(float x)
  224. {
  225. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  226. }
  227. #endif /* HAVE_ROUNDF */
  228. #if !HAVE_TRUNCF
  229. static av_always_inline av_const float truncf(float x)
  230. {
  231. return (x > 0) ? floor(x) : ceil(x);
  232. }
  233. #endif /* HAVE_TRUNCF */
  234. /**
  235. * Returns NULL if CONFIG_SMALL is true, otherwise the argument
  236. * without modification. Used to disable the definition of strings
  237. * (for example AVCodec long_names).
  238. */
  239. #if CONFIG_SMALL
  240. # define NULL_IF_CONFIG_SMALL(x) NULL
  241. #else
  242. # define NULL_IF_CONFIG_SMALL(x) x
  243. #endif
  244. #endif /* AVUTIL_INTERNAL_H */