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