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.

282 lines
6.4KB

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