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.

225 lines
5.5KB

  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file internal.h
  20. * common internal api header.
  21. */
  22. #ifndef INTERNAL_H
  23. #define INTERNAL_H
  24. #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
  25. # define PIC
  26. #endif
  27. # ifndef ENODATA
  28. # define ENODATA 61
  29. # endif
  30. #include "bswap.h"
  31. #include <stddef.h>
  32. #ifndef offsetof
  33. # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
  34. #endif
  35. #ifdef __MINGW32__
  36. # ifdef _DEBUG
  37. # define DEBUG
  38. # endif
  39. # define snprintf _snprintf
  40. # define vsnprintf _vsnprintf
  41. # ifdef CONFIG_WINCE
  42. # define perror(a)
  43. # endif
  44. /* __MINGW32__ end */
  45. #elif defined (CONFIG_OS2)
  46. /* OS/2 EMX */
  47. #include <float.h>
  48. #endif /* !__MINGW32__ && CONFIG_OS2 */
  49. # ifdef USE_FASTMEMCPY
  50. # include "libvo/fastmemcpy.h"
  51. # endif
  52. // Use rip-relative addressing if compiling PIC code on x86-64.
  53. # if defined(__MINGW32__) || defined(__CYGWIN__) || \
  54. defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
  55. # if defined(ARCH_X86_64) && defined(PIC)
  56. # define MANGLE(a) "_" #a"(%%rip)"
  57. # else
  58. # define MANGLE(a) "_" #a
  59. # endif
  60. # else
  61. # if defined(ARCH_X86_64) && defined(PIC)
  62. # define MANGLE(a) #a"(%%rip)"
  63. # elif defined(CONFIG_DARWIN)
  64. # define MANGLE(a) "_" #a
  65. # else
  66. # define MANGLE(a) #a
  67. # endif
  68. # endif
  69. /* debug stuff */
  70. # if !defined(DEBUG) && !defined(NDEBUG)
  71. # define NDEBUG
  72. # endif
  73. # include <assert.h>
  74. /* dprintf macros */
  75. # ifdef DEBUG
  76. # define dprintf(fmt,...) av_log(NULL, AV_LOG_DEBUG, fmt, __VA_ARGS__)
  77. # else
  78. # define dprintf(fmt,...)
  79. # endif
  80. # ifdef CONFIG_WINCE
  81. # define abort()
  82. # endif
  83. # define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  84. extern const uint32_t inverse[256];
  85. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  86. # define FASTDIV(a,b) \
  87. ({\
  88. int ret,dmy;\
  89. asm volatile(\
  90. "mull %3"\
  91. :"=d"(ret),"=a"(dmy)\
  92. :"1"(a),"g"(inverse[b])\
  93. );\
  94. ret;\
  95. })
  96. #elif defined(CONFIG_FASTDIV)
  97. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
  98. #else
  99. # define FASTDIV(a,b) ((a)/(b))
  100. #endif
  101. /* math */
  102. extern FF_IMPORT_ATTR const uint8_t ff_sqrt_tab[128];
  103. static inline int ff_sqrt(int a)
  104. {
  105. int ret=0;
  106. int s;
  107. int ret_sq=0;
  108. if(a<128) return ff_sqrt_tab[a];
  109. for(s=15; s>=0; s--){
  110. int b= ret_sq + (1<<(s*2)) + (ret<<s)*2;
  111. if(b<=a){
  112. ret_sq=b;
  113. ret+= 1<<s;
  114. }
  115. }
  116. return ret;
  117. }
  118. #if defined(ARCH_X86) || defined(ARCH_X86_64)
  119. #define MASK_ABS(mask, level)\
  120. asm volatile(\
  121. "cdq \n\t"\
  122. "xorl %1, %0 \n\t"\
  123. "subl %1, %0 \n\t"\
  124. : "+a" (level), "=&d" (mask)\
  125. );
  126. #else
  127. #define MASK_ABS(mask, level)\
  128. mask= level>>31;\
  129. level= (level^mask)-mask;
  130. #endif
  131. #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
  132. #define COPY3_IF_LT(x,y,a,b,c,d)\
  133. asm volatile (\
  134. "cmpl %0, %3 \n\t"\
  135. "cmovl %3, %0 \n\t"\
  136. "cmovl %4, %1 \n\t"\
  137. "cmovl %5, %2 \n\t"\
  138. : "+r" (x), "+r" (a), "+r" (c)\
  139. : "r" (y), "r" (b), "r" (d)\
  140. );
  141. #else
  142. #define COPY3_IF_LT(x,y,a,b,c,d)\
  143. if((y)<(x)){\
  144. (x)=(y);\
  145. (a)=(b);\
  146. (c)=(d);\
  147. }
  148. #endif
  149. /* avoid usage of various functions */
  150. #define malloc please_use_av_malloc
  151. #define free please_use_av_free
  152. #define realloc please_use_av_realloc
  153. #define time time_is_forbidden_due_to_security_issues
  154. #define rand rand_is_forbidden_due_to_state_trashing
  155. #define srand srand_is_forbidden_due_to_state_trashing
  156. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  157. #define strcat strcat_is_forbidden_due_to_security_issues_use_pstrcat
  158. #if !(defined(LIBAVFORMAT_BUILD) || defined(_FRAMEHOOK_H))
  159. #define printf please_use_av_log
  160. #define fprintf please_use_av_log
  161. #endif
  162. #define CHECKED_ALLOCZ(p, size)\
  163. {\
  164. p= av_mallocz(size);\
  165. if(p==NULL && (size)!=0){\
  166. perror("malloc");\
  167. goto fail;\
  168. }\
  169. }
  170. #ifndef HAVE_LRINTF
  171. /* XXX: add ISOC specific test to avoid specific BSD testing. */
  172. /* better than nothing implementation. */
  173. /* btw, rintf() is existing on fbsd too -- alex */
  174. static always_inline long int lrintf(float x)
  175. {
  176. #ifdef __MINGW32__
  177. # ifdef ARCH_X86
  178. int32_t i;
  179. asm volatile(
  180. "fistpl %0\n\t"
  181. : "=m" (i) : "t" (x) : "st"
  182. );
  183. return i;
  184. # else
  185. /* XXX: incorrect, but make it compile */
  186. return (int)(x + (x < 0 ? -0.5 : 0.5));
  187. # endif /* ARCH_X86 */
  188. #else
  189. return (int)(rint(x));
  190. #endif /* __MINGW32__ */
  191. }
  192. #endif /* HAVE_LRINTF */
  193. #endif /* INTERNAL_H */