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.

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