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.

238 lines
5.8KB

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