internal.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 > 1100) && 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. #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
  79. # define PIC
  80. #endif
  81. #ifndef offsetof
  82. # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
  83. #endif
  84. // Use rip-relative addressing if compiling PIC code on x86-64.
  85. #if ARCH_X86_64 && defined(PIC)
  86. # define LOCAL_MANGLE(a) #a "(%%rip)"
  87. #else
  88. # define LOCAL_MANGLE(a) #a
  89. #endif
  90. #define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)
  91. /* debug stuff */
  92. /* dprintf macros */
  93. #ifdef DEBUG
  94. # define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
  95. #else
  96. # define dprintf(pctx, ...)
  97. #endif
  98. #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  99. /* math */
  100. extern const uint32_t ff_inverse[256];
  101. #if ARCH_X86
  102. # define FASTDIV(a,b) \
  103. ({\
  104. int ret,dmy;\
  105. __asm__ volatile(\
  106. "mull %3"\
  107. :"=d"(ret),"=a"(dmy)\
  108. :"1"(a),"g"(ff_inverse[b])\
  109. );\
  110. ret;\
  111. })
  112. #elif HAVE_ARMV6
  113. static inline av_const int FASTDIV(int a, int b)
  114. {
  115. int r, t;
  116. __asm__ volatile("cmp %3, #2 \n\t"
  117. "ldr %1, [%4, %3, lsl #2] \n\t"
  118. "lsrle %0, %2, #1 \n\t"
  119. "smmulgt %0, %1, %2 \n\t"
  120. : "=&r"(r), "=&r"(t) : "r"(a), "r"(b), "r"(ff_inverse));
  121. return r;
  122. }
  123. #elif ARCH_ARM
  124. static inline av_const int FASTDIV(int a, int b)
  125. {
  126. int r, t;
  127. __asm__ volatile ("umull %1, %0, %2, %3"
  128. : "=&r"(r), "=&r"(t) : "r"(a), "r"(ff_inverse[b]));
  129. return r;
  130. }
  131. #elif CONFIG_FASTDIV
  132. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*ff_inverse[b])>>32))
  133. #else
  134. # define FASTDIV(a,b) ((a)/(b))
  135. #endif
  136. extern const uint8_t ff_sqrt_tab[256];
  137. static inline av_const unsigned int ff_sqrt(unsigned int a)
  138. {
  139. unsigned int b;
  140. if(a<255) return (ff_sqrt_tab[a+1]-1)>>4;
  141. else if(a<(1<<12)) b= ff_sqrt_tab[a>>4 ]>>2;
  142. #if !CONFIG_SMALL
  143. else if(a<(1<<14)) b= ff_sqrt_tab[a>>6 ]>>1;
  144. else if(a<(1<<16)) b= ff_sqrt_tab[a>>8 ] ;
  145. #endif
  146. else{
  147. int s= av_log2_16bit(a>>16)>>1;
  148. unsigned int c= a>>(s+2);
  149. b= ff_sqrt_tab[c>>(s+8)];
  150. b= FASTDIV(c,b) + (b<<s);
  151. }
  152. return b - (a<b*b);
  153. }
  154. #if ARCH_X86
  155. #define MASK_ABS(mask, level)\
  156. __asm__ volatile(\
  157. "cltd \n\t"\
  158. "xorl %1, %0 \n\t"\
  159. "subl %1, %0 \n\t"\
  160. : "+a" (level), "=&d" (mask)\
  161. );
  162. #else
  163. #define MASK_ABS(mask, level)\
  164. mask= level>>31;\
  165. level= (level^mask)-mask;
  166. #endif
  167. #if HAVE_CMOV
  168. #define COPY3_IF_LT(x,y,a,b,c,d)\
  169. __asm__ volatile (\
  170. "cmpl %0, %3 \n\t"\
  171. "cmovl %3, %0 \n\t"\
  172. "cmovl %4, %1 \n\t"\
  173. "cmovl %5, %2 \n\t"\
  174. : "+&r" (x), "+&r" (a), "+r" (c)\
  175. : "r" (y), "r" (b), "r" (d)\
  176. );
  177. #else
  178. #define COPY3_IF_LT(x,y,a,b,c,d)\
  179. if((y)<(x)){\
  180. (x)=(y);\
  181. (a)=(b);\
  182. (c)=(d);\
  183. }
  184. #endif
  185. /* avoid usage of dangerous/inappropriate system functions */
  186. #undef malloc
  187. #define malloc please_use_av_malloc
  188. #undef free
  189. #define free please_use_av_free
  190. #undef realloc
  191. #define realloc please_use_av_realloc
  192. #undef time
  193. #define time time_is_forbidden_due_to_security_issues
  194. #undef rand
  195. #define rand rand_is_forbidden_due_to_state_trashing_use_av_random
  196. #undef srand
  197. #define srand srand_is_forbidden_due_to_state_trashing_use_av_random_init
  198. #undef random
  199. #define random random_is_forbidden_due_to_state_trashing_use_av_random
  200. #undef sprintf
  201. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  202. #undef strcat
  203. #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
  204. #undef exit
  205. #define exit exit_is_forbidden
  206. #ifndef LIBAVFORMAT_BUILD
  207. #undef printf
  208. #define printf please_use_av_log_instead_of_printf
  209. #undef fprintf
  210. #define fprintf please_use_av_log_instead_of_fprintf
  211. #undef puts
  212. #define puts please_use_av_log_instead_of_puts
  213. #undef perror
  214. #define perror please_use_av_log_instead_of_perror
  215. #endif
  216. #define CHECKED_ALLOCZ(p, size)\
  217. {\
  218. p= av_mallocz(size);\
  219. if(p==NULL && (size)!=0){\
  220. av_log(NULL, AV_LOG_ERROR, "Cannot allocate memory.");\
  221. goto fail;\
  222. }\
  223. }
  224. #if defined(__ICC) || defined(__SUNPRO_C)
  225. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  226. #define DECLARE_ASM_CONST(n,t,v) const t __attribute__ ((aligned (n))) v
  227. #elif defined(__GNUC__)
  228. #define DECLARE_ALIGNED(n,t,v) t __attribute__ ((aligned (n))) v
  229. #define DECLARE_ASM_CONST(n,t,v) static const t attribute_used __attribute__ ((aligned (n))) v
  230. #elif defined(_MSC_VER)
  231. #define DECLARE_ALIGNED(n,t,v) __declspec(align(n)) t v
  232. #define DECLARE_ASM_CONST(n,t,v) __declspec(align(n)) static const t v
  233. #elif HAVE_INLINE_ASM
  234. #error The asm code needs alignment, but we do not know how to do it for this compiler.
  235. #else
  236. #define DECLARE_ALIGNED(n,t,v) t v
  237. #define DECLARE_ASM_CONST(n,t,v) static const t v
  238. #endif
  239. #if !HAVE_LLRINT
  240. static av_always_inline av_const long long llrint(double x)
  241. {
  242. return rint(x);
  243. }
  244. #endif /* HAVE_LLRINT */
  245. #if !HAVE_LRINT
  246. static av_always_inline av_const long int lrint(double x)
  247. {
  248. return rint(x);
  249. }
  250. #endif /* HAVE_LRINT */
  251. #if !HAVE_LRINTF
  252. static av_always_inline av_const long int lrintf(float x)
  253. {
  254. return (int)(rint(x));
  255. }
  256. #endif /* HAVE_LRINTF */
  257. #if !HAVE_ROUND
  258. static av_always_inline av_const double round(double x)
  259. {
  260. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  261. }
  262. #endif /* HAVE_ROUND */
  263. #if !HAVE_ROUNDF
  264. static av_always_inline av_const float roundf(float x)
  265. {
  266. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  267. }
  268. #endif /* HAVE_ROUNDF */
  269. #if !HAVE_TRUNCF
  270. static av_always_inline av_const float truncf(float x)
  271. {
  272. return (x > 0) ? floor(x) : ceil(x);
  273. }
  274. #endif /* HAVE_TRUNCF */
  275. /**
  276. * Returns NULL if CONFIG_SMALL is true, otherwise the argument
  277. * without modification. Used to disable the definition of strings
  278. * (for example AVCodec long_names).
  279. */
  280. #if CONFIG_SMALL
  281. # define NULL_IF_CONFIG_SMALL(x) NULL
  282. #else
  283. # define NULL_IF_CONFIG_SMALL(x) x
  284. #endif
  285. #endif /* AVUTIL_INTERNAL_H */