FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/mathops.h
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 31 31 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 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 48344509 static av_always_inline int MULH(int a, int b){
64 48344509 return MUL64(a, b) >> 32;
65 }
66 #endif
67
68 #ifndef UMULH
69 499 static av_always_inline unsigned UMULH(unsigned a, unsigned b){
70 499 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 73476 static inline av_const int median4(int a, int b, int c, int d)
119 {
120
2/2
✓ Branch 0 taken 10273 times.
✓ Branch 1 taken 63203 times.
73476 if (a < b) {
121
2/2
✓ Branch 0 taken 2945 times.
✓ Branch 1 taken 7328 times.
10273 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
122 7328 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
123 } else {
124
2/2
✓ Branch 0 taken 5273 times.
✓ Branch 1 taken 57930 times.
63203 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
125 57930 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 356642855 static inline av_const int sign_extend(int val, unsigned bits)
134 {
135 356642855 unsigned shift = 8 * sizeof(int) - bits;
136 356642855 union { unsigned u; int s; } v = { (unsigned) val << shift };
137 356642855 return v.s >> shift;
138 }
139 #endif
140
141 #ifndef zero_extend
142 79026834 static inline av_const unsigned zero_extend(unsigned val, unsigned bits)
143 {
144 79026834 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 4811499 static inline av_const unsigned int ff_sqrt(unsigned int a)
211 {
212 unsigned int b;
213
214
2/2
✓ Branch 0 taken 397389 times.
✓ Branch 1 taken 4414110 times.
4811499 if (a < 255) return (ff_sqrt_tab[a + 1] - 1) >> 4;
215
2/2
✓ Branch 0 taken 601808 times.
✓ Branch 1 taken 3812302 times.
4414110 else if (a < (1 << 12)) b = ff_sqrt_tab[a >> 4] >> 2;
216 #if !CONFIG_SMALL
217
2/2
✓ Branch 0 taken 862417 times.
✓ Branch 1 taken 2949885 times.
3812302 else if (a < (1 << 14)) b = ff_sqrt_tab[a >> 6] >> 1;
218
2/2
✓ Branch 0 taken 859882 times.
✓ Branch 1 taken 2090003 times.
2949885 else if (a < (1 << 16)) b = ff_sqrt_tab[a >> 8] ;
219 #endif
220 else {
221 2090003 int s = av_log2_16bit(a >> 16) >> 1;
222 2090003 unsigned int c = a >> (s + 2);
223 2090003 b = ff_sqrt_tab[c >> (s + 8)];
224 2090003 b = FASTDIV(c,b) + (b << s);
225 }
226
227 4414110 return b - (a < b * b);
228 }
229 #endif
230
231 30243 static inline av_const float ff_sqrf(float a)
232 {
233 30243 return a*a;
234 }
235
236 393216 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 393216 b.u8 = a;
243 393216 return b.s8;
244 }
245
246 #endif /* AVCODEC_MATHOPS_H */
247