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.

333 lines
7.2KB

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