mathops.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. * simple math operations
  3. * Copyright (c) 2001, 2002 Fabrice Bellard
  4. * Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at> et al
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #ifndef AVCODEC_MATHOPS_H
  23. #define AVCODEC_MATHOPS_H
  24. #include <stdint.h>
  25. #include "libavutil/common.h"
  26. #include "libavutil/reverse.h"
  27. #include "config.h"
  28. #define MAX_NEG_CROP 1024
  29. extern const uint32_t ff_inverse[257];
  30. extern const uint8_t ff_sqrt_tab[256];
  31. extern const uint8_t ff_crop_tab[256 + 2 * MAX_NEG_CROP];
  32. extern const uint8_t ff_zigzag_direct[64];
  33. extern const uint8_t ff_zigzag_scan[16+1];
  34. #if ARCH_ARM
  35. # include "arm/mathops.h"
  36. #elif ARCH_AVR32
  37. # include "avr32/mathops.h"
  38. #elif ARCH_MIPS
  39. # include "mips/mathops.h"
  40. #elif ARCH_PPC
  41. # include "ppc/mathops.h"
  42. #elif ARCH_X86
  43. # include "x86/mathops.h"
  44. #endif
  45. /* generic implementation */
  46. #ifndef MUL64
  47. # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
  48. #endif
  49. #ifndef MULL
  50. # define MULL(a,b,s) (MUL64(a, b) >> (s))
  51. #endif
  52. #ifndef MULH
  53. static av_always_inline int MULH(int a, int b){
  54. return MUL64(a, b) >> 32;
  55. }
  56. #endif
  57. #ifndef UMULH
  58. static av_always_inline unsigned UMULH(unsigned a, unsigned b){
  59. return ((uint64_t)(a) * (uint64_t)(b))>>32;
  60. }
  61. #endif
  62. #ifndef MAC64
  63. # define MAC64(d, a, b) ((d) += MUL64(a, b))
  64. #endif
  65. #ifndef MLS64
  66. # define MLS64(d, a, b) ((d) -= MUL64(a, b))
  67. #endif
  68. /* signed 16x16 -> 32 multiply add accumulate */
  69. #ifndef MAC16
  70. # define MAC16(rt, ra, rb) rt += (ra) * (rb)
  71. #endif
  72. /* signed 16x16 -> 32 multiply */
  73. #ifndef MUL16
  74. # define MUL16(ra, rb) ((ra) * (rb))
  75. #endif
  76. #ifndef MLS16
  77. # define MLS16(rt, ra, rb) ((rt) -= (ra) * (rb))
  78. #endif
  79. /* median of 3 */
  80. #ifndef mid_pred
  81. #define mid_pred mid_pred
  82. static inline av_const int mid_pred(int a, int b, int c)
  83. {
  84. if(a>b){
  85. if(c>b){
  86. if(c>a) b=a;
  87. else b=c;
  88. }
  89. }else{
  90. if(b>c){
  91. if(c>a) b=c;
  92. else b=a;
  93. }
  94. }
  95. return b;
  96. }
  97. #endif
  98. #ifndef median4
  99. #define median4 median4
  100. static inline av_const int median4(int a, int b, int c, int d)
  101. {
  102. if (a < b) {
  103. if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
  104. else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
  105. } else {
  106. if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
  107. else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
  108. }
  109. }
  110. #endif
  111. #ifndef sign_extend
  112. static inline av_const int sign_extend(int val, unsigned bits)
  113. {
  114. unsigned shift = 8 * sizeof(int) - bits;
  115. union { unsigned u; int s; } v = { (unsigned) val << shift };
  116. return v.s >> shift;
  117. }
  118. #endif
  119. #ifndef zero_extend
  120. static inline av_const unsigned zero_extend(unsigned val, unsigned bits)
  121. {
  122. return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
  123. }
  124. #endif
  125. #ifndef COPY3_IF_LT
  126. #define COPY3_IF_LT(x, y, a, b, c, d)\
  127. if ((y) < (x)) {\
  128. (x) = (y);\
  129. (a) = (b);\
  130. (c) = (d);\
  131. }
  132. #endif
  133. #ifndef MASK_ABS
  134. #define MASK_ABS(mask, level) do { \
  135. mask = level >> 31; \
  136. level = (level ^ mask) - mask; \
  137. } while (0)
  138. #endif
  139. #ifndef NEG_SSR32
  140. # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
  141. #endif
  142. #ifndef NEG_USR32
  143. # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
  144. #endif
  145. #if HAVE_BIGENDIAN
  146. # ifndef PACK_2U8
  147. # define PACK_2U8(a,b) (((a) << 8) | (b))
  148. # endif
  149. # ifndef PACK_4U8
  150. # define PACK_4U8(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
  151. # endif
  152. # ifndef PACK_2U16
  153. # define PACK_2U16(a,b) (((a) << 16) | (b))
  154. # endif
  155. #else
  156. # ifndef PACK_2U8
  157. # define PACK_2U8(a,b) (((b) << 8) | (a))
  158. # endif
  159. # ifndef PACK_4U2
  160. # define PACK_4U8(a,b,c,d) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a))
  161. # endif
  162. # ifndef PACK_2U16
  163. # define PACK_2U16(a,b) (((b) << 16) | (a))
  164. # endif
  165. #endif
  166. #ifndef PACK_2S8
  167. # define PACK_2S8(a,b) PACK_2U8((a)&255, (b)&255)
  168. #endif
  169. #ifndef PACK_4S8
  170. # define PACK_4S8(a,b,c,d) PACK_4U8((a)&255, (b)&255, (c)&255, (d)&255)
  171. #endif
  172. #ifndef PACK_2S16
  173. # define PACK_2S16(a,b) PACK_2U16((a)&0xffff, (b)&0xffff)
  174. #endif
  175. #ifndef FASTDIV
  176. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32))
  177. #endif /* FASTDIV */
  178. #ifndef ff_sqrt
  179. #define ff_sqrt ff_sqrt
  180. static inline av_const unsigned int ff_sqrt(unsigned int a)
  181. {
  182. unsigned int b;
  183. if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
  184. else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2;
  185. #if !CONFIG_SMALL
  186. else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1;
  187. else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ;
  188. #endif
  189. else {
  190. int s = av_log2_16bit(a >> 16) >> 1;
  191. unsigned int c = a >> (s + 2);
  192. b = ff_sqrt_tab[c >> (s + 8)];
  193. b = FASTDIV(c,b) + (b << s);
  194. }
  195. return b - (a < b * b);
  196. }
  197. #endif
  198. static inline av_const float ff_sqrf(float a)
  199. {
  200. return a*a;
  201. }
  202. static inline int8_t ff_u8_to_s8(uint8_t a)
  203. {
  204. union {
  205. uint8_t u8;
  206. int8_t s8;
  207. } b;
  208. b.u8 = a;
  209. return b.s8;
  210. }
  211. static av_always_inline uint32_t bitswap_32(uint32_t x)
  212. {
  213. return (uint32_t)ff_reverse[ x & 0xFF] << 24 |
  214. (uint32_t)ff_reverse[(x >> 8) & 0xFF] << 16 |
  215. (uint32_t)ff_reverse[(x >> 16) & 0xFF] << 8 |
  216. (uint32_t)ff_reverse[ x >> 24];
  217. }
  218. #endif /* AVCODEC_MATHOPS_H */