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.

236 lines
5.6KB

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