FFmpeg
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 
28 #include "libavutil/common.h"
29 #include "config.h"
30 
31 #define MAX_NEG_CROP 1024
32 
33 extern const uint32_t ff_inverse[257];
34 extern const uint8_t ff_log2_run[41];
35 extern const uint8_t ff_sqrt_tab[256];
36 extern const uint8_t attribute_visibility_hidden ff_crop_tab[256 + 2 * MAX_NEG_CROP];
37 extern const uint8_t ff_zigzag_direct[64];
38 extern const uint8_t ff_zigzag_scan[16+1];
39 
40 #if ARCH_ARM
41 # include "arm/mathops.h"
42 #elif ARCH_AVR32
43 # include "avr32/mathops.h"
44 #elif ARCH_MIPS
45 # include "mips/mathops.h"
46 #elif ARCH_PPC
47 # include "ppc/mathops.h"
48 #elif ARCH_X86
49 # include "x86/mathops.h"
50 #endif
51 
52 /* generic implementation */
53 
54 #ifndef MUL64
55 # define MUL64(a,b) ((int64_t)(a) * (int64_t)(b))
56 #endif
57 
58 #ifndef MULL
59 # define MULL(a,b,s) (MUL64(a, b) >> (s))
60 #endif
61 
62 #ifndef MULH
63 static av_always_inline int MULH(int a, int b){
64  return MUL64(a, b) >> 32;
65 }
66 #endif
67 
68 #ifndef UMULH
69 static av_always_inline unsigned UMULH(unsigned a, unsigned b){
70  return ((uint64_t)(a) * (uint64_t)(b))>>32;
71 }
72 #endif
73 
74 #ifndef MAC64
75 # define MAC64(d, a, b) ((d) += MUL64(a, b))
76 #endif
77 
78 #ifndef MLS64
79 # define MLS64(d, a, b) ((d) -= MUL64(a, b))
80 #endif
81 
82 /* signed 16x16 -> 32 multiply add accumulate */
83 #ifndef MAC16
84 # define MAC16(rt, ra, rb) rt += (ra) * (rb)
85 #endif
86 
87 /* signed 16x16 -> 32 multiply */
88 #ifndef MUL16
89 # define MUL16(ra, rb) ((ra) * (rb))
90 #endif
91 
92 #ifndef MLS16
93 # define MLS16(rt, ra, rb) ((rt) -= (ra) * (rb))
94 #endif
95 
96 /* median of 3 */
97 #ifndef mid_pred
98 #define mid_pred mid_pred
99 static inline av_const int mid_pred(int a, int b, int c)
100 {
101  if(a>b){
102  if(c>b){
103  if(c>a) b=a;
104  else b=c;
105  }
106  }else{
107  if(b>c){
108  if(c>a) b=c;
109  else b=a;
110  }
111  }
112  return b;
113 }
114 #endif
115 
116 #ifndef median4
117 #define median4 median4
118 static inline av_const int median4(int a, int b, int c, int d)
119 {
120  if (a < b) {
121  if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
122  else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
123  } else {
124  if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
125  else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
126  }
127 }
128 #endif
129 
130 #define FF_SIGNBIT(x) ((x) >> CHAR_BIT * sizeof(x) - 1)
131 
132 #ifndef sign_extend
133 static inline av_const int sign_extend(int val, unsigned bits)
134 {
135  unsigned shift = 8 * sizeof(int) - bits;
136  union { unsigned u; int s; } v = { (unsigned) val << shift };
137  return v.s >> shift;
138 }
139 #endif
140 
141 #ifndef zero_extend
142 static inline av_const unsigned zero_extend(unsigned val, unsigned bits)
143 {
144  return (val << ((8 * sizeof(int)) - bits)) >> ((8 * sizeof(int)) - bits);
145 }
146 #endif
147 
148 #ifndef COPY3_IF_LT
149 #define COPY3_IF_LT(x, y, a, b, c, d)\
150 if ((y) < (x)) {\
151  (x) = (y);\
152  (a) = (b);\
153  (c) = (d);\
154 }
155 #endif
156 
157 #ifndef MASK_ABS
158 #define MASK_ABS(mask, level) do { \
159  mask = level >> 31; \
160  level = (level ^ mask) - mask; \
161  } while (0)
162 #endif
163 
164 #ifndef NEG_SSR32
165 # define NEG_SSR32(a,s) ((( int32_t)(a))>>(32-(s)))
166 #endif
167 
168 #ifndef NEG_USR32
169 # define NEG_USR32(a,s) (((uint32_t)(a))>>(32-(s)))
170 #endif
171 
172 #if HAVE_BIGENDIAN
173 # ifndef PACK_2U8
174 # define PACK_2U8(a,b) (((a) << 8) | (b))
175 # endif
176 # ifndef PACK_4U8
177 # define PACK_4U8(a,b,c,d) (((a) << 24) | ((b) << 16) | ((c) << 8) | (d))
178 # endif
179 # ifndef PACK_2U16
180 # define PACK_2U16(a,b) (((a) << 16) | (b))
181 # endif
182 #else
183 # ifndef PACK_2U8
184 # define PACK_2U8(a,b) (((b) << 8) | (a))
185 # endif
186 # ifndef PACK_4U2
187 # define PACK_4U8(a,b,c,d) (((d) << 24) | ((c) << 16) | ((b) << 8) | (a))
188 # endif
189 # ifndef PACK_2U16
190 # define PACK_2U16(a,b) (((b) << 16) | (a))
191 # endif
192 #endif
193 
194 #ifndef PACK_2S8
195 # define PACK_2S8(a,b) PACK_2U8((a)&255, (b)&255)
196 #endif
197 #ifndef PACK_4S8
198 # define PACK_4S8(a,b,c,d) PACK_4U8((a)&255, (b)&255, (c)&255, (d)&255)
199 #endif
200 #ifndef PACK_2S16
201 # define PACK_2S16(a,b) PACK_2U16((a)&0xffff, (b)&0xffff)
202 #endif
203 
204 #ifndef FASTDIV
205 # define FASTDIV(a,b) ((uint32_t)((((uint64_t)a) * ff_inverse[b]) >> 32))
206 #endif /* FASTDIV */
207 
208 #ifndef ff_sqrt
209 #define ff_sqrt ff_sqrt
210 static inline av_const unsigned int ff_sqrt(unsigned int a)
211 {
212  unsigned int b;
213 
214  if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
215  else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2;
216 #if !CONFIG_SMALL
217  else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1;
218  else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ;
219 #endif
220  else {
221  int s = av_log2_16bit(a >> 16) >> 1;
222  unsigned int c = a >> (s + 2);
223  b = ff_sqrt_tab[c >> (s + 8)];
224  b = FASTDIV(c,b) + (b << s);
225  }
226 
227  return b - (a < b * b);
228 }
229 #endif
230 
231 static inline av_const float ff_sqrf(float a)
232 {
233  return a*a;
234 }
235 
236 static inline int8_t ff_u8_to_s8(uint8_t a)
237 {
238  union {
239  uint8_t u8;
240  int8_t s8;
241  } b;
242  b.u8 = a;
243  return b.s8;
244 }
245 
246 #endif /* AVCODEC_MATHOPS_H */
u
#define u(width, name, range_min, range_max)
Definition: cbs_h2645.c:262
ff_u8_to_s8
static int8_t ff_u8_to_s8(uint8_t a)
Definition: mathops.h:236
av_log2_16bit
int av_log2_16bit(unsigned v)
Definition: intmath.c:31
av_const
#define av_const
Definition: attributes.h:84
b
#define b
Definition: input.c:41
zero_extend
static av_const unsigned zero_extend(unsigned val, unsigned bits)
Definition: mathops.h:142
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
attribute_visibility_hidden
#define attribute_visibility_hidden
Definition: attributes_internal.h:29
ff_inverse
const uint32_t ff_inverse[257]
Definition: mathtables.c:27
mathops.h
median4
#define median4
Definition: mathops.h:117
val
static double val(void *priv, double ch)
Definition: aeval.c:77
ff_sqrt
#define ff_sqrt
Definition: mathops.h:209
attributes_internal.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
bits
uint8_t bits
Definition: vp3data.h:128
UMULH
static av_always_inline unsigned UMULH(unsigned a, unsigned b)
Definition: mathops.h:69
FASTDIV
#define FASTDIV(a, b)
Definition: mathops.h:205
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ff_sqrt_tab
const uint8_t ff_sqrt_tab[256]
Definition: mathtables.c:63
ff_zigzag_scan
const uint8_t ff_zigzag_scan[16+1]
Definition: mathtables.c:109
shift
static int shift(int a, int b)
Definition: bonk.c:253
MULH
static av_always_inline int MULH(int a, int b)
Definition: mathops.h:63
ff_crop_tab
const uint8_t attribute_visibility_hidden ff_crop_tab[256+2 *MAX_NEG_CROP]
Definition: mathtables.c:77
mathops.h
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
common.h
av_always_inline
#define av_always_inline
Definition: attributes.h:49
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
ff_sqrf
static av_const float ff_sqrf(float a)
Definition: mathops.h:231
ff_zigzag_direct
const uint8_t ff_zigzag_direct[64]
Definition: mathtables.c:98
mid_pred
#define mid_pred
Definition: mathops.h:98
mathops.h
mathops.h
sign_extend
static av_const int sign_extend(int val, unsigned bits)
Definition: mathops.h:133
MUL64
#define MUL64(a, b)
Definition: mathops.h:55
mathops.h
d
d
Definition: ffmpeg_filter.c:156
ff_log2_run
const uint8_t ff_log2_run[41]
Definition: mathtables.c:116
MAX_NEG_CROP
#define MAX_NEG_CROP
Definition: mathops.h:31
int
int
Definition: ffmpeg_filter.c:156