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.2KB

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