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.

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