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.

315 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 "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__) || defined(__DJGPP__) || \
  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(__APPLE__)
  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[256];
  153. static inline int av_log2_16bit(unsigned int v);
  154. static inline unsigned int ff_sqrt(unsigned int a)
  155. {
  156. unsigned int b;
  157. if(a<255) return (ff_sqrt_tab[a+1]-1)>>4;
  158. else if(a<(1<<12)) b= ff_sqrt_tab[a>>4 ]>>2;
  159. #ifndef CONFIG_SMALL
  160. else if(a<(1<<14)) b= ff_sqrt_tab[a>>6 ]>>1;
  161. else if(a<(1<<16)) b= ff_sqrt_tab[a>>8 ] ;
  162. #endif
  163. else{
  164. int s= av_log2_16bit(a>>16)>>1;
  165. unsigned int c= a>>(s+2);
  166. b= ff_sqrt_tab[c>>(s+8)];
  167. b= FASTDIV(c,b) + (b<<s);
  168. }
  169. return b - (a<b*b);
  170. }
  171. #if defined(ARCH_X86)
  172. #define MASK_ABS(mask, level)\
  173. asm volatile(\
  174. "cltd \n\t"\
  175. "xorl %1, %0 \n\t"\
  176. "subl %1, %0 \n\t"\
  177. : "+a" (level), "=&d" (mask)\
  178. );
  179. #else
  180. #define MASK_ABS(mask, level)\
  181. mask= level>>31;\
  182. level= (level^mask)-mask;
  183. #endif
  184. #ifdef HAVE_CMOV
  185. #define COPY3_IF_LT(x,y,a,b,c,d)\
  186. asm volatile (\
  187. "cmpl %0, %3 \n\t"\
  188. "cmovl %3, %0 \n\t"\
  189. "cmovl %4, %1 \n\t"\
  190. "cmovl %5, %2 \n\t"\
  191. : "+&r" (x), "+&r" (a), "+r" (c)\
  192. : "r" (y), "r" (b), "r" (d)\
  193. );
  194. #else
  195. #define COPY3_IF_LT(x,y,a,b,c,d)\
  196. if((y)<(x)){\
  197. (x)=(y);\
  198. (a)=(b);\
  199. (c)=(d);\
  200. }
  201. #endif
  202. /* avoid usage of various functions */
  203. #undef malloc
  204. #define malloc please_use_av_malloc
  205. #undef free
  206. #define free please_use_av_free
  207. #undef realloc
  208. #define realloc please_use_av_realloc
  209. #undef time
  210. #define time time_is_forbidden_due_to_security_issues
  211. #undef rand
  212. #define rand rand_is_forbidden_due_to_state_trashing_use_av_random
  213. #undef srand
  214. #define srand srand_is_forbidden_due_to_state_trashing_use_av_init_random
  215. #undef random
  216. #define random random_is_forbidden_due_to_state_trashing_use_av_random
  217. #undef sprintf
  218. #define sprintf sprintf_is_forbidden_due_to_security_issues_use_snprintf
  219. #undef strcat
  220. #define strcat strcat_is_forbidden_due_to_security_issues_use_av_strlcat
  221. #undef exit
  222. #define exit exit_is_forbidden
  223. #if !(defined(LIBAVFORMAT_BUILD) || defined(FFMPEG_FRAMEHOOK_H))
  224. #undef printf
  225. #define printf please_use_av_log
  226. #undef fprintf
  227. #define fprintf please_use_av_log
  228. #undef puts
  229. #define puts please_use_av_log
  230. #undef perror
  231. #define perror please_use_av_log_instead_of_perror
  232. #endif
  233. #define CHECKED_ALLOCZ(p, size)\
  234. {\
  235. p= av_mallocz(size);\
  236. if(p==NULL && (size)!=0){\
  237. av_log(NULL, AV_LOG_ERROR, "Cannot allocate memory.");\
  238. goto fail;\
  239. }\
  240. }
  241. #ifndef HAVE_LLRINT
  242. static av_always_inline long long llrint(double x)
  243. {
  244. return rint(x);
  245. }
  246. #endif /* HAVE_LLRINT */
  247. #ifndef HAVE_LRINT
  248. static av_always_inline long int lrint(double x)
  249. {
  250. return rint(x);
  251. }
  252. #endif /* HAVE_LRINT */
  253. #ifndef HAVE_LRINTF
  254. static av_always_inline long int lrintf(float x)
  255. {
  256. return (int)(rint(x));
  257. }
  258. #endif /* HAVE_LRINTF */
  259. #ifndef HAVE_ROUND
  260. static av_always_inline double round(double x)
  261. {
  262. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  263. }
  264. #endif /* HAVE_ROUND */
  265. #ifndef HAVE_ROUNDF
  266. static av_always_inline float roundf(float x)
  267. {
  268. return (x > 0) ? floor(x + 0.5) : ceil(x - 0.5);
  269. }
  270. #endif /* HAVE_ROUNDF */
  271. #endif /* FFMPEG_INTERNAL_H */