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.

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