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.

281 lines
6.3KB

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