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.

313 lines
7.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 FFMPEG_INTERNAL_H
  25. #define FFMPEG_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 "config.h"
  90. #include "intreadwrite.h"
  91. #include "bswap.h"
  92. #ifndef offsetof
  93. # define offsetof(T,F) ((unsigned int)((char *)&((T *)0)->F))
  94. #endif
  95. #ifdef USE_FASTMEMCPY
  96. # include "libvo/fastmemcpy.h"
  97. # define memcpy(a,b,c) fast_memcpy(a,b,c)
  98. #endif
  99. // Use rip-relative addressing if compiling PIC code on x86-64.
  100. #if defined(__MINGW32__) || defined(__CYGWIN__) || defined(__DJGPP__) || \
  101. defined(__OS2__) || defined(__APPLE__) || \
  102. (defined (__OpenBSD__) && !defined(__ELF__))
  103. # if defined(ARCH_X86_64) && defined(PIC)
  104. # define MANGLE(a) "_" #a"(%%rip)"
  105. # else
  106. # define MANGLE(a) "_" #a
  107. # endif
  108. #elif defined(ARCH_X86_64) && defined(PIC)
  109. # define MANGLE(a) #a"(%%rip)"
  110. #else
  111. # define MANGLE(a) #a
  112. #endif
  113. /* debug stuff */
  114. /* dprintf macros */
  115. #ifdef DEBUG
  116. # define dprintf(pctx, ...) av_log(pctx, AV_LOG_DEBUG, __VA_ARGS__)
  117. #else
  118. # define dprintf(pctx, ...)
  119. #endif
  120. #define av_abort() do { av_log(NULL, AV_LOG_ERROR, "Abort at %s:%d\n", __FILE__, __LINE__); abort(); } while (0)
  121. /* math */
  122. extern const uint32_t ff_inverse[256];
  123. #if defined(ARCH_X86)
  124. # define FASTDIV(a,b) \
  125. ({\
  126. int ret,dmy;\
  127. asm volatile(\
  128. "mull %3"\
  129. :"=d"(ret),"=a"(dmy)\
  130. :"1"(a),"g"(ff_inverse[b])\
  131. );\
  132. ret;\
  133. })
  134. #elif defined(ARCH_ARMV4L)
  135. # define FASTDIV(a,b) \
  136. ({\
  137. int ret,dmy;\
  138. asm volatile(\
  139. "umull %1, %0, %2, %3"\
  140. :"=&r"(ret),"=&r"(dmy)\
  141. :"r"(a),"r"(ff_inverse[b])\
  142. );\
  143. ret;\
  144. })
  145. #elif defined(CONFIG_FASTDIV)
  146. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a)*ff_inverse[b])>>32))
  147. #else
  148. # define FASTDIV(a,b) ((a)/(b))
  149. #endif
  150. extern const uint8_t ff_sqrt_tab[256];
  151. static inline int av_log2_16bit(unsigned int v);
  152. static inline unsigned int ff_sqrt(unsigned int a)
  153. {
  154. unsigned int b;
  155. if(a<255) return (ff_sqrt_tab[a+1]-1)>>4;
  156. else if(a<(1<<12)) b= ff_sqrt_tab[a>>4 ]>>2;
  157. #ifndef CONFIG_SMALL
  158. else if(a<(1<<14)) b= ff_sqrt_tab[a>>6 ]>>1;
  159. else if(a<(1<<16)) b= ff_sqrt_tab[a>>8 ] ;
  160. #endif
  161. else{
  162. int s= av_log2_16bit(a>>16)>>1;
  163. unsigned int c= a>>(s+2);
  164. b= ff_sqrt_tab[c>>(s+8)];
  165. b= FASTDIV(c,b) + (b<<s);
  166. }
  167. return b - (a<b*b);
  168. }
  169. #if defined(ARCH_X86)
  170. #define MASK_ABS(mask, level)\
  171. asm volatile(\
  172. "cltd \n\t"\
  173. "xorl %1, %0 \n\t"\
  174. "subl %1, %0 \n\t"\
  175. : "+a" (level), "=&d" (mask)\
  176. );
  177. #else
  178. #define MASK_ABS(mask, level)\
  179. mask= level>>31;\
  180. level= (level^mask)-mask;
  181. #endif
  182. #ifdef HAVE_CMOV
  183. #define COPY3_IF_LT(x,y,a,b,c,d)\
  184. asm volatile (\
  185. "cmpl %0, %3 \n\t"\
  186. "cmovl %3, %0 \n\t"\
  187. "cmovl %4, %1 \n\t"\
  188. "cmovl %5, %2 \n\t"\
  189. : "+&r" (x), "+&r" (a), "+r" (c)\
  190. : "r" (y), "r" (b), "r" (d)\
  191. );
  192. #else
  193. #define COPY3_IF_LT(x,y,a,b,c,d)\
  194. if((y)<(x)){\
  195. (x)=(y);\
  196. (a)=(b);\
  197. (c)=(d);\
  198. }
  199. #endif
  200. /* avoid usage of various functions */
  201. #undef malloc
  202. #define malloc please_use_av_malloc
  203. #undef free
  204. #define free please_use_av_free
  205. #undef realloc
  206. #define realloc please_use_av_realloc
  207. #undef time
  208. #define time time_is_forbidden_due_to_security_issues
  209. #undef rand
  210. #define rand rand_is_forbidden_due_to_state_trashing_use_av_random
  211. #undef srand
  212. #define srand srand_is_forbidden_due_to_state_trashing_use_av_init_random
  213. #undef random
  214. #define random random_is_forbidden_due_to_state_trashing_use_av_random
  215. #undef sprintf
  216. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  217. #undef strcat
  218. #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
  219. #undef exit
  220. #define exit exit_is_forbidden
  221. #if !(defined(LIBAVFORMAT_BUILD) || defined(FFMPEG_FRAMEHOOK_H))
  222. #undef printf
  223. #define printf please_use_av_log
  224. #undef fprintf
  225. #define fprintf please_use_av_log
  226. #undef puts
  227. #define puts please_use_av_log
  228. #undef perror
  229. #define perror please_use_av_log_instead_of_perror
  230. #endif
  231. #define CHECKED_ALLOCZ(p, size)\
  232. {\
  233. p= av_mallocz(size);\
  234. if(p==NULL && (size)!=0){\
  235. av_log(NULL, AV_LOG_ERROR, "Cannot allocate memory.");\
  236. goto fail;\
  237. }\
  238. }
  239. #ifndef HAVE_LLRINT
  240. static av_always_inline long long llrint(double x)
  241. {
  242. return rint(x);
  243. }
  244. #endif /* HAVE_LLRINT */
  245. #ifndef HAVE_LRINT
  246. static av_always_inline long int lrint(double x)
  247. {
  248. return rint(x);
  249. }
  250. #endif /* HAVE_LRINT */
  251. #ifndef HAVE_LRINTF
  252. static av_always_inline long int lrintf(float x)
  253. {
  254. return (int)(rint(x));
  255. }
  256. #endif /* HAVE_LRINTF */
  257. #ifndef HAVE_ROUND
  258. static av_always_inline double round(double x)
  259. {
  260. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  261. }
  262. #endif /* HAVE_ROUND */
  263. #ifndef HAVE_ROUNDF
  264. static av_always_inline float roundf(float x)
  265. {
  266. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  267. }
  268. #endif /* HAVE_ROUNDF */
  269. #endif /* FFMPEG_INTERNAL_H */