00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef AVCODEC_MATHOPS_H
00023 #define AVCODEC_MATHOPS_H
00024
00025 #include <stdint.h>
00026
00027 #include "libavutil/common.h"
00028 #include "config.h"
00029
00030 extern const uint32_t ff_inverse[257];
00031 extern const uint8_t ff_reverse[256];
00032 extern const uint8_t ff_sqrt_tab[256];
00033
00034 #if ARCH_ARM
00035 # include "arm/mathops.h"
00036 #elif ARCH_AVR32
00037 # include "avr32/mathops.h"
00038 #elif ARCH_BFIN
00039 # include "bfin/mathops.h"
00040 #elif ARCH_MIPS
00041 # include "mips/mathops.h"
00042 #elif ARCH_PPC
00043 # include "ppc/mathops.h"
00044 #elif ARCH_X86
00045 # include "x86/mathops.h"
00046 #endif
00047
00048
00049
00050 #ifndef MUL64
00051 # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
00052 #endif
00053
00054 #ifndef MULL
00055 # define MULL(a,b,s) (MUL64(a, b) >> (s))
00056 #endif
00057
00058 #ifndef MULH
00059 static av_always_inline int MULH(int a, int b){
00060 return MUL64(a, b) >> 32;
00061 }
00062 #endif
00063
00064 #ifndef UMULH
00065 static av_always_inline unsigned UMULH(unsigned a, unsigned b){
00066 return ((uint64_t)(a) * (uint64_t)(b))>>32;
00067 }
00068 #endif
00069
00070 #ifndef MAC64
00071 # define MAC64(d, a, b) ((d) += MUL64(a, b))
00072 #endif
00073
00074 #ifndef MLS64
00075 # define MLS64(d, a, b) ((d) -= MUL64(a, b))
00076 #endif
00077
00078
00079 #ifndef MAC16
00080 # define MAC16(rt, ra, rb) rt += (ra) * (rb)
00081 #endif
00082
00083
00084 #ifndef MUL16
00085 # define MUL16(ra, rb) ((ra) * (rb))
00086 #endif
00087
00088 #ifndef MLS16
00089 # define MLS16(rt, ra, rb) ((rt) -= (ra) * (rb))
00090 #endif
00091
00092
00093 #ifndef mid_pred
00094 #define mid_pred mid_pred
00095 static inline av_const int mid_pred(int a, int b, int c)
00096 {
00097 #if 0
00098 int t= (a-b)&((a-b)>>31);
00099 a-=t;
00100 b+=t;
00101 b-= (b-c)&((b-c)>>31);
00102 b+= (a-b)&((a-b)>>31);
00103
00104 return b;
00105 #else
00106 if(a>b){
00107 if(c>b){
00108 if(c>a) b=a;
00109 else b=c;
00110 }
00111 }else{
00112 if(b>c){
00113 if(c>a) b=c;
00114 else b=a;
00115 }
00116 }
00117 return b;
00118 #endif
00119 }
00120 #endif
00121
00122 #ifndef sign_extend
00123 static inline av_const int sign_extend(int val, unsigned bits)
00124 {
00125 unsigned shift = 8 * sizeof(int) - bits;
00126 union { unsigned u; int s; } v = { (unsigned) val << shift };
00127 return v.s >> shift;
00128 }
00129 #endif
00130
00131 #ifndef zero_extend
00132 static inline av_const unsigned zero_extend(unsigned val, unsigned bits)
00133 {
00134 return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
00135 }
00136 #endif
00137
00138 #ifndef COPY3_IF_LT
00139 #define COPY3_IF_LT(x, y, a, b, c, d)\
00140 if ((y) < (x)) {\
00141 (x) = (y);\
00142 (a) = (b);\
00143 (c) = (d);\
00144 }
00145 #endif
00146
00147 #ifndef MASK_ABS
00148 #define MASK_ABS(mask, level) do { \
00149 mask = level >> 31; \
00150 level = (level ^ mask) - mask; \
00151 } while (0)
00152 #endif
00153
00154 #ifndef NEG_SSR32
00155 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
00156 #endif
00157
00158 #ifndef NEG_USR32
00159 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
00160 #endif
00161
00162 #if HAVE_BIGENDIAN
00163 # ifndef PACK_2U8
00164 # define PACK_2U8(a,b) (((a) << 8) | (b))
00165 # endif
00166 # ifndef PACK_4U8
00167 # define PACK_4U8(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
00168 # endif
00169 # ifndef PACK_2U16
00170 # define PACK_2U16(a,b) (((a) << 16) | (b))
00171 # endif
00172 #else
00173 # ifndef PACK_2U8
00174 # define PACK_2U8(a,b) (((b) << 8) | (a))
00175 # endif
00176 # ifndef PACK_4U2
00177 # define PACK_4U8(a,b,c,d) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a))
00178 # endif
00179 # ifndef PACK_2U16
00180 # define PACK_2U16(a,b) (((b) << 16) | (a))
00181 # endif
00182 #endif
00183
00184 #ifndef PACK_2S8
00185 # define PACK_2S8(a,b) PACK_2U8((a)&255, (b)&255)
00186 #endif
00187 #ifndef PACK_4S8
00188 # define PACK_4S8(a,b,c,d) PACK_4U8((a)&255, (b)&255, (c)&255, (d)&255)
00189 #endif
00190 #ifndef PACK_2S16
00191 # define PACK_2S16(a,b) PACK_2U16((a)&0xffff, (b)&0xffff)
00192 #endif
00193
00194 #ifndef FASTDIV
00195 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32))
00196 #endif
00197
00198 static inline av_const unsigned int ff_sqrt(unsigned int a)
00199 {
00200 unsigned int b;
00201
00202 if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
00203 else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2;
00204 #if !CONFIG_SMALL
00205 else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1;
00206 else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ;
00207 #endif
00208 else {
00209 int s = av_log2_16bit(a >> 16) >> 1;
00210 unsigned int c = a >> (s + 2);
00211 b = ff_sqrt_tab[c >> (s + 8)];
00212 b = FASTDIV(c,b) + (b << s);
00213 }
00214
00215 return b - (a < b * b);
00216 }
00217
00218 #endif