FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mathops.h
Date: 2026-01-23 14:02:51
Exec Total Coverage
Lines: 35 35 100.0%
Functions: 9 9 100.0%
Branches: 14 14 100.0%

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