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.

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