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.

231 lines
5.2KB

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