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.

247 lines
5.6KB

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