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.

321 lines
6.9KB

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