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.8KB

  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(ARCH_ARMV4L)
  97. # define FASTDIV(a,b) \
  98. ({\
  99. int ret,dmy;\
  100. asm volatile(\
  101. "umull %1, %0, %2, %3"\
  102. :"=&r"(ret),"=&r"(dmy)\
  103. :"r"(a),"r"(inverse[b])\
  104. );\
  105. ret;\
  106. })
  107. #elif defined(CONFIG_FASTDIV)
  108. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*inverse[b])>>32))
  109. #else
  110. # define FASTDIV(a,b) ((a)/(b))
  111. #endif
  112. /* math */
  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) || defined(ARCH_X86_64)
  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. #if __CPU__ >= 686 && !defined(RUNTIME_CPUDETECT)
  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
  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 */
  199. #else
  200. return (int)(rint(x));
  201. #endif /* __MINGW32__ */
  202. }
  203. #endif /* HAVE_LRINTF */
  204. #endif /* INTERNAL_H */