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
29
30 #define MAX_NEG_CROP 1024
31
36
37 #if ARCH_ARM
39 #elif ARCH_AVR32
41 #elif ARCH_MIPS
43 #elif ARCH_PPC
45 #elif ARCH_X86
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
61 return MUL64(a, b) >> 32;
62 }
63 #endif
64
65 #ifndef UMULH
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
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
106 #else
107 if(a>b){
108 if(c>b){
111 }
112 }else{
113 if(b>c){
116 }
117 }
119 #endif
120 }
121 #endif
122
123 #ifndef median4
124 #define median4 median4
126 {
127 if (a < b) {
128 if (c < d)
return (
FFMIN(b, d) +
FFMAX(a, c)) / 2;
130 } else {
131 if (c < d)
return (
FFMIN(a, d) +
FFMAX(b, c)) / 2;
133 }
134 }
135 #endif
136
137 #ifndef sign_extend
139 {
140 unsigned shift = 8 *
sizeof(int) - bits;
141 union {
unsigned u;
int s; } v = { (unsigned) val << shift };
143 }
144 #endif
145
146 #ifndef zero_extend
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
216 {
218
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;
224 #endif
225 else {
227 unsigned int c = a >> (s + 2);
230 }
231
232 return b - (a < b *
b);
233 }
234 #endif
235
237 {
239 }
240
242 {
243 union {
245 int8_t s8;
249 }
250
251 #endif /* AVCODEC_MATHOPS_H */
const char const char void * val
static int shift(int a, int b)
static int8_t ff_u8_to_s8(uint8_t a)
int av_log2_16bit(unsigned v)
static av_const unsigned zero_extend(unsigned val, unsigned bits)
const uint32_t ff_inverse[257]
const uint8_t ff_sqrt_tab[256]
static av_always_inline unsigned UMULH(unsigned a, unsigned b)
static av_const float ff_sqrf(float a)
const uint8_t ff_crop_tab[256+2 *MAX_NEG_CROP]
const uint8_t ff_zigzag_direct[64]
static av_always_inline int MULH(int a, int b)
static av_const int sign_extend(int val, unsigned bits)
common internal and external API header