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.

298 lines
7.0KB

  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 <stdint.h>
  30. #include <stddef.h>
  31. #include <assert.h>
  32. #include "common.h"
  33. #ifndef attribute_align_arg
  34. #if (!defined(__ICC) || __ICC > 1100) && AV_GCC_VERSION_AT_LEAST(4,2)
  35. # define attribute_align_arg __attribute__((force_align_arg_pointer))
  36. #else
  37. # define attribute_align_arg
  38. #endif
  39. #endif
  40. #ifndef attribute_used
  41. #if AV_GCC_VERSION_AT_LEAST(3,1)
  42. # define attribute_used __attribute__((used))
  43. #else
  44. # define attribute_used
  45. #endif
  46. #endif
  47. #ifndef INT16_MIN
  48. #define INT16_MIN (-0x7fff-1)
  49. #endif
  50. #ifndef INT16_MAX
  51. #define INT16_MAX 0x7fff
  52. #endif
  53. #ifndef INT32_MIN
  54. #define INT32_MIN (-0x7fffffff-1)
  55. #endif
  56. #ifndef INT32_MAX
  57. #define INT32_MAX 0x7fffffff
  58. #endif
  59. #ifndef UINT32_MAX
  60. #define UINT32_MAX 0xffffffff
  61. #endif
  62. #ifndef INT64_MIN
  63. #define INT64_MIN (-0x7fffffffffffffffLL-1)
  64. #endif
  65. #ifndef INT64_MAX
  66. #define INT64_MAX INT64_C(9223372036854775807)
  67. #endif
  68. #ifndef UINT64_MAX
  69. #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
  70. #endif
  71. #ifndef INT_BIT
  72. # if INT_MAX != 2147483647
  73. # define INT_BIT 64
  74. # else
  75. # define INT_BIT 32
  76. # endif
  77. #endif
  78. #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
  79. # define PIC
  80. #endif
  81. #include "config.h"
  82. #ifndef offsetof
  83. # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
  84. #endif
  85. // Use rip-relative addressing if compiling PIC code on x86-64.
  86. #if defined(ARCH_X86_64) && defined(PIC)
  87. # define LOCAL_MANGLE(a) #a "(%%rip)"
  88. #else
  89. # define LOCAL_MANGLE(a) #a
  90. #endif
  91. #define MANGLE(a) EXTERN_PREFIX LOCAL_MANGLE(a)
  92. /* debug stuff */
  93. /* dprintf macros */
  94. #ifdef DEBUG
  95. # define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
  96. #else
  97. # define dprintf(pctx, ...)
  98. #endif
  99. #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  100. /* math */
  101. extern const uint32_t ff_inverse[256];
  102. #if defined(ARCH_X86)
  103. # define FASTDIV(a,b) \
  104. ({\
  105. int ret,dmy;\
  106. __asm__ volatile(\
  107. "mull %3"\
  108. :"=d"(ret),"=a"(dmy)\
  109. :"1"(a),"g"(ff_inverse[b])\
  110. );\
  111. ret;\
  112. })
  113. #elif defined(HAVE_ARMV6)
  114. static inline av_const int FASTDIV(int a, int b)
  115. {
  116. int r, t;
  117. __asm__ volatile("cmp %3, #2 \n\t"
  118. "ldr %1, [%4, %3, lsl #2] \n\t"
  119. "lsrle %0, %2, #1 \n\t"
  120. "smmulgt %0, %1, %2 \n\t"
  121. : "=&r"(r), "=&r"(t) : "r"(a), "r"(b), "r"(ff_inverse));
  122. return r;
  123. }
  124. #elif defined(ARCH_ARM)
  125. static inline av_const int FASTDIV(int a, int b)
  126. {
  127. int r, t;
  128. __asm__ volatile ("umull %1, %0, %2, %3"
  129. : "=&r"(r), "=&r"(t) : "r"(a), "r"(ff_inverse[b]));
  130. return r;
  131. }
  132. #elif defined(CONFIG_FASTDIV)
  133. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*ff_inverse[b])>>32))
  134. #else
  135. # define FASTDIV(a,b) ((a)/(b))
  136. #endif
  137. extern const uint8_t ff_sqrt_tab[256];
  138. static inline int av_log2_16bit(unsigned int v);
  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. #ifndef 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 defined(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. #ifdef 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 various 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_random
  198. #undef srand
  199. #define srand srand_is_forbidden_due_to_state_trashing_use_av_init_random
  200. #undef random
  201. #define random random_is_forbidden_due_to_state_trashing_use_av_random
  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
  211. #undef fprintf
  212. #define fprintf please_use_av_log
  213. #undef puts
  214. #define puts please_use_av_log
  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. #ifndef HAVE_LLRINT
  227. static av_always_inline av_const long long llrint(double x)
  228. {
  229. return rint(x);
  230. }
  231. #endif /* HAVE_LLRINT */
  232. #ifndef HAVE_LRINT
  233. static av_always_inline av_const long int lrint(double x)
  234. {
  235. return rint(x);
  236. }
  237. #endif /* HAVE_LRINT */
  238. #ifndef HAVE_LRINTF
  239. static av_always_inline av_const long int lrintf(float x)
  240. {
  241. return (int)(rint(x));
  242. }
  243. #endif /* HAVE_LRINTF */
  244. #ifndef HAVE_ROUND
  245. static av_always_inline av_const double round(double x)
  246. {
  247. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  248. }
  249. #endif /* HAVE_ROUND */
  250. #ifndef HAVE_ROUNDF
  251. static av_always_inline av_const float roundf(float x)
  252. {
  253. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  254. }
  255. #endif /* HAVE_ROUNDF */
  256. #endif /* AVUTIL_INTERNAL_H */