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.

268 lines
6.1KB

  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. #include <stdint.h>
  27. #include <assert.h>
  28. #ifndef attribute_used
  29. #if defined(__GNUC__) && (__GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ > 0)
  30. # define attribute_used __attribute__((used))
  31. #else
  32. # define attribute_used
  33. #endif
  34. #endif
  35. #ifndef M_PI
  36. #define M_PI 3.14159265358979323846
  37. #endif
  38. #ifndef INT16_MIN
  39. #define INT16_MIN (-0x7fff-1)
  40. #endif
  41. #ifndef INT16_MAX
  42. #define INT16_MAX 0x7fff
  43. #endif
  44. #ifndef INT32_MIN
  45. #define INT32_MIN (-0x7fffffff-1)
  46. #endif
  47. #ifndef INT32_MAX
  48. #define INT32_MAX 0x7fffffff
  49. #endif
  50. #ifndef UINT32_MAX
  51. #define UINT32_MAX 0xffffffff
  52. #endif
  53. #ifndef INT64_MIN
  54. #define INT64_MIN (-0x7fffffffffffffffLL-1)
  55. #endif
  56. #ifndef INT64_MAX
  57. #define INT64_MAX INT64_C(9223372036854775807)
  58. #endif
  59. #ifndef UINT64_MAX
  60. #define UINT64_MAX UINT64_C(0xFFFFFFFFFFFFFFFF)
  61. #endif
  62. #ifndef INT_BIT
  63. # if INT_MAX != 2147483647
  64. # define INT_BIT 64
  65. # else
  66. # define INT_BIT 32
  67. # endif
  68. #endif
  69. #if ( defined(__PIC__) || defined(__pic__) ) && ! defined(PIC)
  70. # define PIC
  71. #endif
  72. #include "intreadwrite.h"
  73. #include "bswap.h"
  74. #include <stddef.h>
  75. #ifndef offsetof
  76. # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
  77. #endif
  78. #ifdef USE_FASTMEMCPY
  79. # include "libvo/fastmemcpy.h"
  80. # define memcpy(a,b,c) fast_memcpy(a,b,c)
  81. #endif
  82. // Use rip-relative addressing if compiling PIC code on x86-64.
  83. #if defined(__MINGW32__) || defined(__CYGWIN__) || \
  84. defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
  85. # if defined(ARCH_X86_64) && defined(PIC)
  86. # define MANGLE(a) "_" #a"(%%rip)"
  87. # else
  88. # define MANGLE(a) "_" #a
  89. # endif
  90. #else
  91. # if defined(ARCH_X86_64) && defined(PIC)
  92. # define MANGLE(a) #a"(%%rip)"
  93. # elif defined(CONFIG_DARWIN)
  94. # define MANGLE(a) "_" #a
  95. # else
  96. # define MANGLE(a) #a
  97. # endif
  98. #endif
  99. /* debug stuff */
  100. #if !defined(DEBUG) && !defined(NDEBUG)
  101. # define NDEBUG
  102. #endif
  103. #include <assert.h>
  104. /* dprintf macros */
  105. #ifdef DEBUG
  106. # define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
  107. #else
  108. # define dprintf(pctx, ...)
  109. #endif
  110. #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  111. /* math */
  112. extern const uint32_t ff_inverse[256];
  113. #if defined(ARCH_X86)
  114. # define FASTDIV(a,b) \
  115. ({\
  116. int ret,dmy;\
  117. asm volatile(\
  118. "mull %3"\
  119. :"=d"(ret),"=a"(dmy)\
  120. :"1"(a),"g"(ff_inverse[b])\
  121. );\
  122. ret;\
  123. })
  124. #elif defined(ARCH_ARMV4L)
  125. # define FASTDIV(a,b) \
  126. ({\
  127. int ret,dmy;\
  128. asm volatile(\
  129. "umull %1, %0, %2, %3"\
  130. :"=&r"(ret),"=&r"(dmy)\
  131. :"r"(a),"r"(ff_inverse[b])\
  132. );\
  133. ret;\
  134. })
  135. #elif defined(CONFIG_FASTDIV)
  136. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*ff_inverse[b])>>32))
  137. #else
  138. # define FASTDIV(a,b) ((a)/(b))
  139. #endif
  140. extern const uint8_t ff_sqrt_tab[128];
  141. static inline int ff_sqrt(int a)
  142. {
  143. int ret=0;
  144. int s, b;
  145. if(a<128) return ff_sqrt_tab[a];
  146. for(s=30; s>=0; s-=2){
  147. ret+=ret;
  148. b= (1+2*ret)<<s;
  149. if(b<=a){
  150. a-=b;
  151. ret++;
  152. }
  153. }
  154. return ret;
  155. }
  156. #if defined(ARCH_X86)
  157. #define MASK_ABS(mask, level)\
  158. asm volatile(\
  159. "cdq \n\t"\
  160. "xorl %1, %0 \n\t"\
  161. "subl %1, %0 \n\t"\
  162. : "+a" (level), "=&d" (mask)\
  163. );
  164. #else
  165. #define MASK_ABS(mask, level)\
  166. mask= level>>31;\
  167. level= (level^mask)-mask;
  168. #endif
  169. #ifdef HAVE_CMOV
  170. #define COPY3_IF_LT(x,y,a,b,c,d)\
  171. asm volatile (\
  172. "cmpl %0, %3 \n\t"\
  173. "cmovl %3, %0 \n\t"\
  174. "cmovl %4, %1 \n\t"\
  175. "cmovl %5, %2 \n\t"\
  176. : "+&r" (x), "+&r" (a), "+r" (c)\
  177. : "r" (y), "r" (b), "r" (d)\
  178. );
  179. #else
  180. #define COPY3_IF_LT(x,y,a,b,c,d)\
  181. if((y)<(x)){\
  182. (x)=(y);\
  183. (a)=(b);\
  184. (c)=(d);\
  185. }
  186. #endif
  187. /* avoid usage of various functions */
  188. #undef malloc
  189. #define malloc please_use_av_malloc
  190. #undef free
  191. #define free please_use_av_free
  192. #undef realloc
  193. #define realloc please_use_av_realloc
  194. #undef time
  195. #define time time_is_forbidden_due_to_security_issues
  196. #undef rand
  197. #define rand rand_is_forbidden_due_to_state_trashing_use_av_random
  198. #undef srand
  199. #define srand srand_is_forbidden_due_to_state_trashing_use_av_init_random
  200. #undef random
  201. #define random random_is_forbidden_due_to_state_trashing_use_av_random
  202. #undef sprintf
  203. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  204. #undef strcat
  205. #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
  206. #undef exit
  207. #define exit exit_is_forbidden
  208. #if !(defined(LIBAVFORMAT_BUILD) || defined(FRAMEHOOK_H))
  209. #undef printf
  210. #define printf please_use_av_log
  211. #undef fprintf
  212. #define fprintf please_use_av_log
  213. #endif
  214. #define CHECKED_ALLOCZ(p, size)\
  215. {\
  216. p= av_mallocz(size);\
  217. if(p==NULL && (size)!=0){\
  218. perror("malloc");\
  219. goto fail;\
  220. }\
  221. }
  222. #ifndef HAVE_LRINTF
  223. /* XXX: add ISOC specific test to avoid specific BSD testing. */
  224. /* better than nothing implementation. */
  225. /* btw, rintf() is existing on fbsd too -- alex */
  226. static av_always_inline long int lrintf(float x)
  227. {
  228. return (int)(rint(x));
  229. }
  230. #endif /* HAVE_LRINTF */
  231. #endif /* INTERNAL_H */