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.

332 lines
8.3KB

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