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.

289 lines
6.5KB

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