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.

333 lines
8.2KB

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