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.

305 lines
7.0KB

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