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.

293 lines
6.6KB

  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 INTERNAL_H
  25. #define INTERNAL_H
  26. #ifndef attribute_used
  27. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  28. # define attribute_used __attribute__((used))
  29. #else
  30. # define attribute_used
  31. #endif
  32. #endif
  33. #ifndef attribute_unused
  34. #if defined(__GNUC__)
  35. # define attribute_unused __attribute__((unused))
  36. #else
  37. # define attribute_unused
  38. #endif
  39. #endif
  40. #ifndef M_PI
  41. #define M_PI 3.14159265358979323846
  42. #endif
  43. #ifndef INT16_MIN
  44. #define INT16_MIN (-0x7fff-1)
  45. #endif
  46. #ifndef INT16_MAX
  47. #define INT16_MAX 0x7fff
  48. #endif
  49. #ifndef INT32_MIN
  50. #define INT32_MIN (-0x7fffffff-1)
  51. #endif
  52. #ifndef INT32_MAX
  53. #define INT32_MAX 0x7fffffff
  54. #endif
  55. #ifndef UINT32_MAX
  56. #define UINT32_MAX 0xffffffff
  57. #endif
  58. #ifndef INT64_MIN
  59. #define INT64_MIN (-0x7fffffffffffffffLL-1)
  60. #endif
  61. #ifndef INT64_MAX
  62. #define INT64_MAX INT64_C(9223372036854775807)
  63. #endif
  64. #ifndef UINT64_MAX
  65. #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
  66. #endif
  67. #ifndef INT_BIT
  68. # if INT_MAX != 2147483647
  69. # define INT_BIT 64
  70. # else
  71. # define INT_BIT 32
  72. # endif
  73. #endif
  74. #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
  75. # define PIC
  76. #endif
  77. #include "intreadwrite.h"
  78. #include "bswap.h"
  79. #include <stddef.h>
  80. #ifndef offsetof
  81. # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
  82. #endif
  83. #ifdef __MINGW32__
  84. # ifdef _DEBUG
  85. # define DEBUG
  86. # endif
  87. # define snprintf _snprintf
  88. # define vsnprintf _vsnprintf
  89. # ifdef CONFIG_WINCE
  90. # define perror(a)
  91. # define abort()
  92. # endif
  93. /* __MINGW32__ end */
  94. #elif defined (CONFIG_OS2)
  95. /* OS/2 EMX */
  96. # include <float.h>
  97. #endif /* !__MINGW32__ && CONFIG_OS2 */
  98. #ifdef USE_FASTMEMCPY
  99. # include "libvo/fastmemcpy.h"
  100. #endif
  101. // Use rip-relative addressing if compiling PIC code on x86-64.
  102. #if defined(__MINGW32__) || defined(__CYGWIN__) || \
  103. defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
  104. # if defined(ARCH_X86_64) && defined(PIC)
  105. # define MANGLE(a) "_" #a"(%%rip)"
  106. # else
  107. # define MANGLE(a) "_" #a
  108. # endif
  109. #else
  110. # if defined(ARCH_X86_64) && defined(PIC)
  111. # define MANGLE(a) #a"(%%rip)"
  112. # elif defined(CONFIG_DARWIN)
  113. # define MANGLE(a) "_" #a
  114. # else
  115. # define MANGLE(a) #a
  116. # endif
  117. #endif
  118. /* debug stuff */
  119. #if !defined(DEBUG) && !defined(NDEBUG)
  120. # define NDEBUG
  121. #endif
  122. #include <assert.h>
  123. /* dprintf macros */
  124. #ifdef DEBUG
  125. # define dprintf(...) av_log(NULL, AV_LOG_DEBUG, __VA_ARGS__)
  126. #else
  127. # define dprintf(...)
  128. #endif
  129. #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  130. /* math */
  131. extern const uint32_t ff_inverse[256];
  132. #if defined(ARCH_X86)
  133. # define FASTDIV(a,b) \
  134. ({\
  135. int ret,dmy;\
  136. asm volatile(\
  137. "mull %3"\
  138. :"=d"(ret),"=a"(dmy)\
  139. :"1"(a),"g"(ff_inverse[b])\
  140. );\
  141. ret;\
  142. })
  143. #elif defined(ARCH_ARMV4L)
  144. # define FASTDIV(a,b) \
  145. ({\
  146. int ret,dmy;\
  147. asm volatile(\
  148. "umull %1, %0, %2, %3"\
  149. :"=&r"(ret),"=&r"(dmy)\
  150. :"r"(a),"r"(ff_inverse[b])\
  151. );\
  152. ret;\
  153. })
  154. #elif defined(CONFIG_FASTDIV)
  155. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*ff_inverse[b])>>32))
  156. #else
  157. # define FASTDIV(a,b) ((a)/(b))
  158. #endif
  159. extern const uint8_t ff_sqrt_tab[128];
  160. static inline int ff_sqrt(int a)
  161. {
  162. int ret=0;
  163. int s;
  164. int ret_sq=0;
  165. if(a<128) return ff_sqrt_tab[a];
  166. for(s=15; s>=0; s--){
  167. int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
  168. if(b<=a){
  169. ret_sq=b;
  170. ret+= 1<<s;
  171. }
  172. }
  173. return ret;
  174. }
  175. #if defined(ARCH_X86)
  176. #define MASK_ABS(mask, level)\
  177. asm volatile(\
  178. "cdq \n\t"\
  179. "xorl %1, %0 \n\t"\
  180. "subl %1, %0 \n\t"\
  181. : "+a" (level), "=&d" (mask)\
  182. );
  183. #else
  184. #define MASK_ABS(mask, level)\
  185. mask= level>>31;\
  186. level= (level^mask)-mask;
  187. #endif
  188. #ifdef HAVE_CMOV
  189. #define COPY3_IF_LT(x,y,a,b,c,d)\
  190. asm volatile (\
  191. "cmpl %0, %3 \n\t"\
  192. "cmovl %3, %0 \n\t"\
  193. "cmovl %4, %1 \n\t"\
  194. "cmovl %5, %2 \n\t"\
  195. : "+r" (x), "+r" (a), "+r" (c)\
  196. : "r" (y), "r" (b), "r" (d)\
  197. );
  198. #else
  199. #define COPY3_IF_LT(x,y,a,b,c,d)\
  200. if((y)<(x)){\
  201. (x)=(y);\
  202. (a)=(b);\
  203. (c)=(d);\
  204. }
  205. #endif
  206. /* avoid usage of various functions */
  207. #define malloc please_use_av_malloc
  208. #define free please_use_av_free
  209. #define realloc please_use_av_realloc
  210. #define time time_is_forbidden_due_to_security_issues
  211. #define rand rand_is_forbidden_due_to_state_trashing
  212. #define srand srand_is_forbidden_due_to_state_trashing
  213. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  214. #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
  215. #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
  216. #define printf please_use_av_log
  217. #define fprintf please_use_av_log
  218. #endif
  219. #define CHECKED_ALLOCZ(p, size)\
  220. {\
  221. p= av_mallocz(size);\
  222. if(p==NULL && (size)!=0){\
  223. perror("malloc");\
  224. goto fail;\
  225. }\
  226. }
  227. #ifndef HAVE_LRINTF
  228. /* XXX: add ISOC specific test to avoid specific BSD testing. */
  229. /* better than nothing implementation. */
  230. /* btw, rintf() is existing on fbsd too -- alex */
  231. static av_always_inline long int lrintf(float x)
  232. {
  233. #ifdef __MINGW32__
  234. # ifdef ARCH_X86_32
  235. int32_t i;
  236. asm volatile(
  237. "fistpl %0\n\t"
  238. : "=m" (i) : "t" (x) : "st"
  239. );
  240. return i;
  241. # else
  242. /* XXX: incorrect, but make it compile */
  243. return (int)(x + (x < 0 ? -0.5 : 0.5));
  244. # endif /* ARCH_X86_32 */
  245. #else
  246. return (int)(rint(x));
  247. #endif /* __MINGW32__ */
  248. }
  249. #endif /* HAVE_LRINTF */
  250. #endif /* INTERNAL_H */