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