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.

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