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.

307 lines
7.1KB

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