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.

234 lines
5.3KB

  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 Libav.
  7. *
  8. * Libav 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. * Libav 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 Libav; 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 "config.h"
  27. #define MAX_NEG_CROP 1024
  28. extern const uint32_t ff_inverse[257];
  29. extern const uint8_t ff_reverse[256];
  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];
  34. #if ARCH_ARM
  35. # include "arm/mathops.h"
  36. #elif ARCH_AVR32
  37. # include "avr32/mathops.h"
  38. #elif ARCH_BFIN
  39. # include "bfin/mathops.h"
  40. #elif ARCH_MIPS
  41. # include "mips/mathops.h"
  42. #elif ARCH_PPC
  43. # include "ppc/mathops.h"
  44. #elif ARCH_X86
  45. # include "x86/mathops.h"
  46. #endif
  47. /* generic implementation */
  48. #ifndef MUL64
  49. # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
  50. #endif
  51. #ifndef MULL
  52. # define MULL(a,b,s) (MUL64(a, b) >> (s))
  53. #endif
  54. #ifndef MULH
  55. static av_always_inline int MULH(int a, int b){
  56. return MUL64(a, b) >> 32;
  57. }
  58. #endif
  59. #ifndef UMULH
  60. static av_always_inline unsigned UMULH(unsigned a, unsigned b){
  61. return ((uint64_t)(a) * (uint64_t)(b))>>32;
  62. }
  63. #endif
  64. #ifndef MAC64
  65. # define MAC64(d, a, b) ((d) += MUL64(a, b))
  66. #endif
  67. #ifndef MLS64
  68. # define MLS64(d, a, b) ((d) -= MUL64(a, b))
  69. #endif
  70. /* signed 16x16 -> 32 multiply add accumulate */
  71. #ifndef MAC16
  72. # define MAC16(rt, ra, rb) rt += (ra) * (rb)
  73. #endif
  74. /* signed 16x16 -> 32 multiply */
  75. #ifndef MUL16
  76. # define MUL16(ra, rb) ((ra) * (rb))
  77. #endif
  78. #ifndef MLS16
  79. # define MLS16(rt, ra, rb) ((rt) -= (ra) * (rb))
  80. #endif
  81. /* median of 3 */
  82. #ifndef mid_pred
  83. #define mid_pred mid_pred
  84. static inline av_const int mid_pred(int a, int b, int c)
  85. {
  86. #if 0
  87. int t= (a-b)&((a-b)>>31);
  88. a-=t;
  89. b+=t;
  90. b-= (b-c)&((b-c)>>31);
  91. b+= (a-b)&((a-b)>>31);
  92. return b;
  93. #else
  94. if(a>b){
  95. if(c>b){
  96. if(c>a) b=a;
  97. else b=c;
  98. }
  99. }else{
  100. if(b>c){
  101. if(c>a) b=c;
  102. else b=a;
  103. }
  104. }
  105. return b;
  106. #endif
  107. }
  108. #endif
  109. #ifndef sign_extend
  110. static inline av_const int sign_extend(int val, unsigned bits)
  111. {
  112. unsigned shift = 8 * sizeof(int) - bits;
  113. union { unsigned u; int s; } v = { (unsigned) val << shift };
  114. return v.s >> shift;
  115. }
  116. #endif
  117. #ifndef zero_extend
  118. static inline av_const unsigned zero_extend(unsigned val, unsigned bits)
  119. {
  120. return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
  121. }
  122. #endif
  123. #ifndef COPY3_IF_LT
  124. #define COPY3_IF_LT(x, y, a, b, c, d)\
  125. if ((y) < (x)) {\
  126. (x) = (y);\
  127. (a) = (b);\
  128. (c) = (d);\
  129. }
  130. #endif
  131. #ifndef MASK_ABS
  132. #define MASK_ABS(mask, level) do { \
  133. mask = level >> 31; \
  134. level = (level ^ mask) - mask; \
  135. } while (0)
  136. #endif
  137. #ifndef NEG_SSR32
  138. # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
  139. #endif
  140. #ifndef NEG_USR32
  141. # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
  142. #endif
  143. #if HAVE_BIGENDIAN
  144. # ifndef PACK_2U8
  145. # define PACK_2U8(a,b) (((a) << 8) | (b))
  146. # endif
  147. # ifndef PACK_4U8
  148. # define PACK_4U8(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
  149. # endif
  150. # ifndef PACK_2U16
  151. # define PACK_2U16(a,b) (((a) << 16) | (b))
  152. # endif
  153. #else
  154. # ifndef PACK_2U8
  155. # define PACK_2U8(a,b) (((b) << 8) | (a))
  156. # endif
  157. # ifndef PACK_4U2
  158. # define PACK_4U8(a,b,c,d) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a))
  159. # endif
  160. # ifndef PACK_2U16
  161. # define PACK_2U16(a,b) (((b) << 16) | (a))
  162. # endif
  163. #endif
  164. #ifndef PACK_2S8
  165. # define PACK_2S8(a,b) PACK_2U8((a)&255, (b)&255)
  166. #endif
  167. #ifndef PACK_4S8
  168. # define PACK_4S8(a,b,c,d) PACK_4U8((a)&255, (b)&255, (c)&255, (d)&255)
  169. #endif
  170. #ifndef PACK_2S16
  171. # define PACK_2S16(a,b) PACK_2U16((a)&0xffff, (b)&0xffff)
  172. #endif
  173. #ifndef FASTDIV
  174. # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32))
  175. #endif /* FASTDIV */
  176. static inline av_const unsigned int ff_sqrt(unsigned int a)
  177. {
  178. unsigned int b;
  179. if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
  180. else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2;
  181. #if !CONFIG_SMALL
  182. else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1;
  183. else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ;
  184. #endif
  185. else {
  186. int s = av_log2_16bit(a >> 16) >> 1;
  187. unsigned int c = a >> (s + 2);
  188. b = ff_sqrt_tab[c >> (s + 8)];
  189. b = FASTDIV(c,b) + (b << s);
  190. }
  191. return b - (a < b * b);
  192. }
  193. static inline int8_t ff_u8_to_s8(uint8_t a)
  194. {
  195. union {
  196. uint8_t u8;
  197. int8_t s8;
  198. } b;
  199. b.u8 = a;
  200. return b.s8;
  201. }
  202. #endif /* AVCODEC_MATHOPS_H */