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.

278 lines
6.3KB

  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 __MINGW32__
  79. # ifdef _DEBUG
  80. # define DEBUG
  81. # endif
  82. # define snprintf _snprintf
  83. # define vsnprintf _vsnprintf
  84. #endif /* !__MINGW32__ */
  85. #ifdef USE_FASTMEMCPY
  86. # include "libvo/fastmemcpy.h"
  87. # define memcpy(a,b,c) fast_memcpy(a,b,c)
  88. #endif
  89. // Use rip-relative addressing if compiling PIC code on x86-64.
  90. #if defined(__MINGW32__) || defined(__CYGWIN__) || \
  91. defined(__OS2__) || (defined (__OpenBSD__) && !defined(__ELF__))
  92. # if defined(ARCH_X86_64) && defined(PIC)
  93. # define MANGLE(a) "_" #a"(%%rip)"
  94. # else
  95. # define MANGLE(a) "_" #a
  96. # endif
  97. #else
  98. # if defined(ARCH_X86_64) && defined(PIC)
  99. # define MANGLE(a) #a"(%%rip)"
  100. # elif defined(CONFIG_DARWIN)
  101. # define MANGLE(a) "_" #a
  102. # else
  103. # define MANGLE(a) #a
  104. # endif
  105. #endif
  106. /* debug stuff */
  107. #if !defined(DEBUG) && !defined(NDEBUG)
  108. # define NDEBUG
  109. #endif
  110. #include <assert.h>
  111. /* dprintf macros */
  112. #ifdef DEBUG
  113. # define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
  114. #else
  115. # define dprintf(pctx, ...)
  116. #endif
  117. #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  118. /* math */
  119. extern const uint32_t ff_inverse[256];
  120. #if defined(ARCH_X86)
  121. # define FASTDIV(a,b) \
  122. ({\
  123. int ret,dmy;\
  124. asm volatile(\
  125. "mull %3"\
  126. :"=d"(ret),"=a"(dmy)\
  127. :"1"(a),"g"(ff_inverse[b])\
  128. );\
  129. ret;\
  130. })
  131. #elif defined(ARCH_ARMV4L)
  132. # define FASTDIV(a,b) \
  133. ({\
  134. int ret,dmy;\
  135. asm volatile(\
  136. "umull %1, %0, %2, %3"\
  137. :"=&r"(ret),"=&r"(dmy)\
  138. :"r"(a),"r"(ff_inverse[b])\
  139. );\
  140. ret;\
  141. })
  142. #elif defined(CONFIG_FASTDIV)
  143. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*ff_inverse[b])>>32))
  144. #else
  145. # define FASTDIV(a,b) ((a)/(b))
  146. #endif
  147. extern const uint8_t ff_sqrt_tab[128];
  148. static inline int ff_sqrt(int a)
  149. {
  150. int ret=0;
  151. int s, b;
  152. if(a<128) return ff_sqrt_tab[a];
  153. for(s=30; s>=0; s-=2){
  154. ret+=ret;
  155. b= (1+2*ret)<<s;
  156. if(b<=a){
  157. a-=b;
  158. ret++;
  159. }
  160. }
  161. return ret;
  162. }
  163. #if defined(ARCH_X86)
  164. #define MASK_ABS(mask, level)\
  165. asm volatile(\
  166. "cdq \n\t"\
  167. "xorl %1, %0 \n\t"\
  168. "subl %1, %0 \n\t"\
  169. : "+a" (level), "=&d" (mask)\
  170. );
  171. #else
  172. #define MASK_ABS(mask, level)\
  173. mask= level>>31;\
  174. level= (level^mask)-mask;
  175. #endif
  176. #ifdef HAVE_CMOV
  177. #define COPY3_IF_LT(x,y,a,b,c,d)\
  178. asm volatile (\
  179. "cmpl %0, %3 \n\t"\
  180. "cmovl %3, %0 \n\t"\
  181. "cmovl %4, %1 \n\t"\
  182. "cmovl %5, %2 \n\t"\
  183. : "+&r" (x), "+&r" (a), "+r" (c)\
  184. : "r" (y), "r" (b), "r" (d)\
  185. );
  186. #else
  187. #define COPY3_IF_LT(x,y,a,b,c,d)\
  188. if((y)<(x)){\
  189. (x)=(y);\
  190. (a)=(b);\
  191. (c)=(d);\
  192. }
  193. #endif
  194. /* avoid usage of various functions */
  195. #undef malloc
  196. #define malloc please_use_av_malloc
  197. #undef free
  198. #define free please_use_av_free
  199. #undef realloc
  200. #define realloc please_use_av_realloc
  201. #undef time
  202. #define time time_is_forbidden_due_to_security_issues
  203. #undef rand
  204. #define rand rand_is_forbidden_due_to_state_trashing_use_av_random
  205. #undef srand
  206. #define srand srand_is_forbidden_due_to_state_trashing_use_av_init_random
  207. #undef random
  208. #define random random_is_forbidden_due_to_state_trashing_use_av_random
  209. #undef sprintf
  210. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  211. #undef strcat
  212. #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
  213. #undef exit
  214. #define exit exit_is_forbidden
  215. #if !(defined(LIBAVFORMAT_BUILD) || defined(FRAMEHOOK_H))
  216. #undef printf
  217. #define printf please_use_av_log
  218. #undef fprintf
  219. #define fprintf please_use_av_log
  220. #endif
  221. #define CHECKED_ALLOCZ(p, size)\
  222. {\
  223. p= av_mallocz(size);\
  224. if(p==NULL && (size)!=0){\
  225. perror("malloc");\
  226. goto fail;\
  227. }\
  228. }
  229. #ifndef HAVE_LRINTF
  230. /* XXX: add ISOC specific test to avoid specific BSD testing. */
  231. /* better than nothing implementation. */
  232. /* btw, rintf() is existing on fbsd too -- alex */
  233. static av_always_inline long int lrintf(float x)
  234. {
  235. return (int)(rint(x));
  236. }
  237. #endif /* HAVE_LRINTF */
  238. #endif /* INTERNAL_H */