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.

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