FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
mathops.h
Go to the documentation of this file.
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 
25 #include <stdint.h>
26 
27 #include "libavutil/common.h"
28 #include "config.h"
29 
30 #define MAX_NEG_CROP 1024
31 
32 extern const uint32_t ff_inverse[257];
33 extern const uint8_t ff_sqrt_tab[256];
34 extern const uint8_t ff_crop_tab[256 + 2 * MAX_NEG_CROP];
35 extern const uint8_t ff_zigzag_direct[64];
36 
37 #if ARCH_ARM
38 # include "arm/mathops.h"
39 #elif ARCH_AVR32
40 # include "avr32/mathops.h"
41 #elif ARCH_MIPS
42 # include "mips/mathops.h"
43 #elif ARCH_PPC
44 # include "ppc/mathops.h"
45 #elif ARCH_X86
46 # include "x86/mathops.h"
47 #endif
48 
49 /* generic implementation */
50 
51 #ifndef MUL64
52 # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
53 #endif
54 
55 #ifndef MULL
56 # define MULL(a,b,s) (MUL64(a, b) >> (s))
57 #endif
58 
59 #ifndef MULH
60 static av_always_inline int MULH(int a, int b){
61  return MUL64(a, b) >> 32;
62 }
63 #endif
64 
65 #ifndef UMULH
66 static av_always_inline unsigned UMULH(unsigned a, unsigned b){
67  return ((uint64_t)(a) * (uint64_t)(b))>>32;
68 }
69 #endif
70 
71 #ifndef MAC64
72 # define MAC64(d, a, b) ((d) += MUL64(a, b))
73 #endif
74 
75 #ifndef MLS64
76 # define MLS64(d, a, b) ((d) -= MUL64(a, b))
77 #endif
78 
79 /* signed 16x16 -> 32 multiply add accumulate */
80 #ifndef MAC16
81 # define MAC16(rt, ra, rb) rt += (ra) * (rb)
82 #endif
83 
84 /* signed 16x16 -> 32 multiply */
85 #ifndef MUL16
86 # define MUL16(ra, rb) ((ra) * (rb))
87 #endif
88 
89 #ifndef MLS16
90 # define MLS16(rt, ra, rb) ((rt) -= (ra) * (rb))
91 #endif
92 
93 /* median of 3 */
94 #ifndef mid_pred
95 #define mid_pred mid_pred
96 static inline av_const int mid_pred(int a, int b, int c)
97 {
98 #if 0
99  int t= (a-b)&((a-b)>>31);
100  a-=t;
101  b+=t;
102  b-= (b-c)&((b-c)>>31);
103  b+= (a-b)&((a-b)>>31);
104 
105  return b;
106 #else
107  if(a>b){
108  if(c>b){
109  if(c>a) b=a;
110  else b=c;
111  }
112  }else{
113  if(b>c){
114  if(c>a) b=c;
115  else b=a;
116  }
117  }
118  return b;
119 #endif
120 }
121 #endif
122 
123 #ifndef median4
124 #define median4 median4
125 static inline av_const int median4(int a, int b, int c, int d)
126 {
127  if (a < b) {
128  if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
129  else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
130  } else {
131  if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
132  else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
133  }
134 }
135 #endif
136 
137 #ifndef sign_extend
138 static inline av_const int sign_extend(int val, unsigned bits)
139 {
140  unsigned shift = 8 * sizeof(int) - bits;
141  union { unsigned u; int s; } v = { (unsigned) val << shift };
142  return v.s >> shift;
143 }
144 #endif
145 
146 #ifndef zero_extend
147 static inline av_const unsigned zero_extend(unsigned val, unsigned bits)
148 {
149  return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
150 }
151 #endif
152 
153 #ifndef COPY3_IF_LT
154 #define COPY3_IF_LT(x, y, a, b, c, d)\
155 if ((y) < (x)) {\
156  (x) = (y);\
157  (a) = (b);\
158  (c) = (d);\
159 }
160 #endif
161 
162 #ifndef MASK_ABS
163 #define MASK_ABS(mask, level) do { \
164  mask = level >> 31; \
165  level = (level ^ mask) - mask; \
166  } while (0)
167 #endif
168 
169 #ifndef NEG_SSR32
170 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
171 #endif
172 
173 #ifndef NEG_USR32
174 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
175 #endif
176 
177 #if HAVE_BIGENDIAN
178 # ifndef PACK_2U8
179 # define PACK_2U8(a,b) (((a) << 8) | (b))
180 # endif
181 # ifndef PACK_4U8
182 # define PACK_4U8(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
183 # endif
184 # ifndef PACK_2U16
185 # define PACK_2U16(a,b) (((a) << 16) | (b))
186 # endif
187 #else
188 # ifndef PACK_2U8
189 # define PACK_2U8(a,b) (((b) << 8) | (a))
190 # endif
191 # ifndef PACK_4U2
192 # define PACK_4U8(a,b,c,d) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a))
193 # endif
194 # ifndef PACK_2U16
195 # define PACK_2U16(a,b) (((b) << 16) | (a))
196 # endif
197 #endif
198 
199 #ifndef PACK_2S8
200 # define PACK_2S8(a,b) PACK_2U8((a)&255, (b)&255)
201 #endif
202 #ifndef PACK_4S8
203 # define PACK_4S8(a,b,c,d) PACK_4U8((a)&255, (b)&255, (c)&255, (d)&255)
204 #endif
205 #ifndef PACK_2S16
206 # define PACK_2S16(a,b) PACK_2U16((a)&0xffff, (b)&0xffff)
207 #endif
208 
209 #ifndef FASTDIV
210 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32))
211 #endif /* FASTDIV */
212 
213 #ifndef ff_sqrt
214 #define ff_sqrt ff_sqrt
215 static inline av_const unsigned int ff_sqrt(unsigned int a)
216 {
217  unsigned int b;
218 
219  if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
220  else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2;
221 #if !CONFIG_SMALL
222  else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1;
223  else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ;
224 #endif
225  else {
226  int s = av_log2_16bit(a >> 16) >> 1;
227  unsigned int c = a >> (s + 2);
228  b = ff_sqrt_tab[c >> (s + 8)];
229  b = FASTDIV(c,b) + (b << s);
230  }
231 
232  return b - (a < b * b);
233 }
234 #endif
235 
236 static inline av_const float ff_sqrf(float a)
237 {
238  return a*a;
239 }
240 
241 static inline int8_t ff_u8_to_s8(uint8_t a)
242 {
243  union {
244  uint8_t u8;
245  int8_t s8;
246  } b;
247  b.u8 = a;
248  return b.s8;
249 }
250 
251 #endif /* AVCODEC_MATHOPS_H */
#define MUL64(a, b)
Definition: mathops.h:52
#define av_const
Definition: attributes.h:76
const char const char void * val
Definition: avisynth_c.h:634
const char * s
Definition: avisynth_c.h:631
static int shift(int a, int b)
Definition: sonic.c:82
const char * b
Definition: vf_curves.c:109
#define MAX_NEG_CROP
Definition: mathops.h:30
static int8_t ff_u8_to_s8(uint8_t a)
Definition: mathops.h:241
int av_log2_16bit(unsigned v)
Definition: intmath.c:31
static av_const unsigned zero_extend(unsigned val, unsigned bits)
Definition: mathops.h:147
const uint32_t ff_inverse[257]
Definition: mathtables.c:27
uint8_t bits
Definition: crc.c:295
uint8_t
#define ff_sqrt
Definition: mathops.h:214
#define median4
Definition: mathops.h:124
const uint8_t ff_sqrt_tab[256]
Definition: mathtables.c:63
#define FFMAX(a, b)
Definition: common.h:94
#define FFMIN(a, b)
Definition: common.h:96
static av_always_inline unsigned UMULH(unsigned a, unsigned b)
Definition: mathops.h:66
static av_const float ff_sqrf(float a)
Definition: mathops.h:236
#define FASTDIV(a, b)
Definition: mathops.h:210
const uint8_t ff_crop_tab[256+2 *MAX_NEG_CROP]
Definition: mathtables.c:77
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
#define mid_pred
Definition: mathops.h:95
static av_always_inline int MULH(int a, int b)
Definition: mathops.h:60
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:138
common internal and external API header
static double c[64]
#define av_always_inline
Definition: attributes.h:39