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.

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