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.

282 lines
6.5KB

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