FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/tests/checkasm/sw_ops.c
Date: 2026-11-08 03:48:22
Exec Total Coverage
Lines: 402 421 95.5%
Functions: 29 29 100.0%
Branches: 161 203 79.3%

Line Branch Exec Source
1 /**
2 * Copyright (C) 2025 Niklas Haas
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include <string.h>
22
23 #include "libavutil/avassert.h"
24 #include "libavutil/mem_internal.h"
25 #include "libavutil/refstruct.h"
26
27 #include "libswscale/filters.h"
28 #include "libswscale/ops_dispatch.h"
29 #include "libswscale/uops.h"
30 #include "libswscale/uops_macros.h"
31
32 #include "checkasm.h"
33
34 enum {
35 MAX_UOPS = 3, /* read, op, write */
36 NB_PLANES = 4,
37 PIXELS = 64,
38 LINES = 16,
39 };
40
41 typedef struct Test {
42 const char *name;
43 SwsUOp uops[MAX_UOPS];
44 int num_uops;
45
46 /* Extra metadata for the test harness */
47 SwsPixelType type_in; /* data format to fill */
48 SwsPixelType type_out; /* data format to compare */
49 SwsCompMask planes_in; /* planes to fill */
50 SwsCompMask planes_out; /* planes to compare */
51 int pixel_bits_in; /* read pixel stride */
52 int pixel_bits_out; /* write pixel stride */
53 unsigned ranges[NB_PLANES]; /* pixel range to fill */
54 } Test;
55
56 #define FMT(fmt, ...) tprintf((char[256]) {0}, 256, fmt, __VA_ARGS__)
57 4063 static const char *tprintf(char buf[], size_t size, const char *fmt, ...)
58 {
59 va_list ap;
60 4063 va_start(ap, fmt);
61 4063 vsnprintf(buf, size, fmt, ap);
62 4063 va_end(ap);
63 4063 return buf;
64 }
65
66 4315832 static float rndf(void)
67 {
68 union { uint32_t u; float f; } x;
69 do {
70 4349852 x.u = rnd();
71
2/2
✓ Branch 0 taken 34020 times.
✓ Branch 1 taken 4315832 times.
4349852 } while (!isnormal(x.f));
72 4315832 return x.f;
73 }
74
75 5698 static SwsPixel constpx(SwsPixelType type, int val)
76 {
77
4/5
✓ Branch 0 taken 952 times.
✓ Branch 1 taken 910 times.
✓ Branch 2 taken 686 times.
✓ Branch 3 taken 3150 times.
✗ Branch 4 not taken.
5698 switch (type) {
78 952 case SWS_PIXEL_U8: return (SwsPixel) { .u8 = val };
79 910 case SWS_PIXEL_U16: return (SwsPixel) { .u16 = val };
80 686 case SWS_PIXEL_U32: return (SwsPixel) { .u32 = val };
81 3150 case SWS_PIXEL_F32: return (SwsPixel) { .f32 = val };
82 ✗ default: return (SwsPixel) {0};
83 }
84 }
85
86 91210 static SwsPixel rndpx(SwsPixelType type)
87 {
88
4/5
✓ Branch 0 taken 966 times.
✓ Branch 1 taken 910 times.
✓ Branch 2 taken 574 times.
✓ Branch 3 taken 88760 times.
✗ Branch 4 not taken.
91210 switch (type) {
89 966 case SWS_PIXEL_U8: return (SwsPixel) { .u8 = rnd() & 0xFF };
90 910 case SWS_PIXEL_U16: return (SwsPixel) { .u16 = rnd() & 0xFFFF };
91 574 case SWS_PIXEL_U32: return (SwsPixel) { .u32 = rnd() };
92 88760 case SWS_PIXEL_F32: return (SwsPixel) { .f32 = rndf() };
93 ✗ default: return (SwsPixel) {0};
94 }
95 }
96
97 1121 static void fill32f(float *line, int num, unsigned range)
98 {
99 1121 const float scale = (float) range / UINT32_MAX;
100
2/2
✓ Branch 0 taken 4591616 times.
✓ Branch 1 taken 1121 times.
4592737 for (int i = 0; i < num; i++)
101
2/2
✓ Branch 0 taken 364544 times.
✓ Branch 1 taken 4227072 times.
4591616 line[i] = range ? scale * rnd() : rndf();
102 1121 }
103
104 2831 static void fill32(uint32_t *line, int num, unsigned range)
105 {
106
2/2
✓ Branch 0 taken 11595776 times.
✓ Branch 1 taken 2831 times.
11598607 for (int i = 0; i < num; i++)
107
4/4
✓ Branch 0 taken 425984 times.
✓ Branch 1 taken 11169792 times.
✓ Branch 2 taken 393216 times.
✓ Branch 3 taken 32768 times.
11595776 line[i] = (range && range < UINT_MAX) ? rnd() % (range + 1) : rnd();
108 2831 }
109
110 1198 static void fill16(uint16_t *line, int num, unsigned range)
111 {
112
2/2
✓ Branch 0 taken 1096 times.
✓ Branch 1 taken 102 times.
1198 if (!range) {
113 1096 fill32((uint32_t *) line, AV_CEIL_RSHIFT(num, 1), 0);
114 } else {
115
2/2
✓ Branch 0 taken 835584 times.
✓ Branch 1 taken 102 times.
835686 for (int i = 0; i < num; i++)
116 835584 line[i] = rnd() % (range + 1);
117 }
118 1198 }
119
120 1344 static void fill8(uint8_t *line, int num, unsigned range)
121 {
122
2/2
✓ Branch 0 taken 1221 times.
✓ Branch 1 taken 123 times.
1344 if (!range) {
123 1221 fill32((uint32_t *) line, AV_CEIL_RSHIFT(num, 2), 0);
124 } else {
125
2/2
✓ Branch 0 taken 2015232 times.
✓ Branch 1 taken 123 times.
2015355 for (int i = 0; i < num; i++)
126 2015232 line[i] = rnd() % (range + 1);
127 }
128 1344 }
129
130 13580 static SwsPixelType pixel_type_to_int(const SwsPixelType type)
131 {
132
3/4
✓ Branch 0 taken 2296 times.
✓ Branch 1 taken 2688 times.
✓ Branch 2 taken 8596 times.
✗ Branch 3 not taken.
13580 switch (ff_sws_pixel_type_size(type)) {
133 2296 case 1: return SWS_PIXEL_U8;
134 2688 case 2: return SWS_PIXEL_U16;
135 8596 case 4: return SWS_PIXEL_U32;
136 ✗ default: break;
137 }
138
139 ✗ av_unreachable("Invalid pixel type!");
140 return SWS_PIXEL_NONE;
141 }
142
143 10355 static void check_compiled(const Test *test,
144 const SwsCompiledOp *comp_ref,
145 const SwsCompiledOp *comp_new)
146 {
147 /**
148 * We can't use `check_func()` alone because the actual function pointer
149 * may be a wrapper or entry point shared by multiple implementations.
150 * Solve it by hashing in the active CPU flags as well.
151 */
152 10355 uintptr_t id = (uintptr_t) comp_new->func;
153 10355 id ^= (id << 6) + (id >> 2) + 0x9e3779b97f4a7c15 + comp_new->cpu_flags;
154
2/2
✓ Branch 1 taken 8976 times.
✓ Branch 2 taken 1379 times.
10355 if (!check_key(id, "%s", test->name))
155 8976 return;
156
157 1379 declare_func(void, const SwsOpExec *, const void *, int bx, int y, int bx_end, int y_end);
158
159 static DECLARE_ALIGNED_64(char, src0)[NB_PLANES][LINES][PIXELS * sizeof(uint32_t[4])];
160 static DECLARE_ALIGNED_64(char, src1)[NB_PLANES][LINES][PIXELS * sizeof(uint32_t[4])];
161 /* All output planes are stacked vertically in a single padded buffer */
162 1379 static BUF_RECT(uint8_t, dst0, PIXELS * sizeof(uint32_t[4]), NB_PLANES * LINES);
163 1379 static BUF_RECT(uint8_t, dst1, PIXELS * sizeof(uint32_t[4]), NB_PLANES * LINES);
164
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1379 times.
1379 av_assert0(PIXELS % comp_new->block_size == 0);
166
2/2
✓ Branch 0 taken 5516 times.
✓ Branch 1 taken 1379 times.
6895 for (int p = 0; p < NB_PLANES; p++) {
167 5516 void *plane = src0[p];
168
2/2
✓ Branch 0 taken 1339 times.
✓ Branch 1 taken 4177 times.
5516 if (!SWS_COMP_TEST(test->planes_in, p)) {
169 1339 memset(plane, 0, sizeof(src0[p]));
170 1339 continue;
171 }
172
173
4/5
✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 1198 times.
✓ Branch 2 taken 514 times.
✓ Branch 3 taken 1121 times.
✗ Branch 4 not taken.
4177 switch (test->type_in) {
174 1344 case SWS_PIXEL_U8:
175 1344 fill8(plane, sizeof(src0[p]) / sizeof(uint8_t), test->ranges[p]);
176 1344 break;
177 1198 case SWS_PIXEL_U16:
178 1198 fill16(plane, sizeof(src0[p]) / sizeof(uint16_t), test->ranges[p]);
179 1198 break;
180 514 case SWS_PIXEL_U32:
181 514 fill32(plane, sizeof(src0[p]) / sizeof(uint32_t), test->ranges[p]);
182 514 break;
183 1121 case SWS_PIXEL_F32:
184 1121 fill32f(plane, sizeof(src0[p]) / sizeof(uint32_t), test->ranges[p]);
185 1121 break;
186 }
187 }
188
189 1379 memcpy(src1, src0, sizeof(src0));
190 1379 CLEAR_BUF_RECT(dst0);
191 1379 CLEAR_BUF_RECT(dst1);
192
193 1379 const int read_size = PIXELS * test->pixel_bits_in >> 3;
194 1379 const int write_size = PIXELS * test->pixel_bits_out >> 3;
195
196 1379 SwsOpExec exec = {0};
197 1379 exec.width = PIXELS;
198 1379 exec.height = exec.slice_h = LINES;
199
2/2
✓ Branch 0 taken 5516 times.
✓ Branch 1 taken 1379 times.
6895 for (int i = 0; i < NB_PLANES; i++) {
200 5516 exec.in_stride[i] = sizeof(src0[i][0]);
201 5516 exec.out_stride[i] = dst0_stride;
202 5516 exec.in_bump[i] = exec.in_stride[i] - read_size;
203 5516 exec.out_bump[i] = exec.out_stride[i] - write_size;
204 }
205
206 int32_t in_bump_y[LINES];
207 int32_t in_offset_x[PIXELS];
208 1379 const SwsUOp *read_op = &test->uops[0];
209
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 240 times.
✓ Branch 2 taken 336 times.
✓ Branch 3 taken 800 times.
1379 switch (read_op->uop) {
210 3 case SWS_UOP_READ_PALETTE:
211 3 exec.in_bump[1] = exec.in_stride[1] = 0;
212 static_assert(sizeof(src0[1]) >= sizeof(uint32_t[256]), "palette plane too small");
213 3 break;
214 240 case SWS_UOP_READ_PLANAR_FV: {
215 240 const int *offsets = read_op->data.kernel->offsets;
216
2/2
✓ Branch 0 taken 3600 times.
✓ Branch 1 taken 240 times.
3840 for (int y = 0; y < LINES - 1; y++)
217 3600 in_bump_y[y] = offsets[y + 1] - offsets[y] - 1;
218 240 in_bump_y[LINES - 1] = 0;
219 240 exec.in_bump_y = in_bump_y;
220 240 break;
221 }
222 336 case SWS_UOP_READ_PLANAR_FH: {
223 336 const int *offsets = read_op->data.kernel->offsets;
224
2/2
✓ Branch 0 taken 21504 times.
✓ Branch 1 taken 336 times.
21840 for (int x = 0; x < PIXELS; x++)
225 21504 in_offset_x[x] = offsets[x] * test->pixel_bits_in >> 3;
226 336 exec.in_offset_x = in_offset_x;
227 }
228 }
229
230
2/2
✓ Branch 0 taken 5516 times.
✓ Branch 1 taken 1379 times.
6895 for (int i = 0; i < NB_PLANES; i++) {
231 5516 exec.in[i] = (void *) src0[i];
232 5516 exec.out[i] = dst0 + i * LINES * dst0_stride;
233 5516 exec.block_size_in[i] = comp_ref->block_size * test->pixel_bits_in >> 3;
234 5516 exec.block_size_out[i] = comp_ref->block_size * test->pixel_bits_out >> 3;
235 }
236
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1379 times.
1379 checkasm_call(comp_ref->func, &exec, comp_ref->priv, 0, 0, PIXELS / comp_ref->block_size, LINES);
237
238
2/2
✓ Branch 0 taken 5516 times.
✓ Branch 1 taken 1379 times.
6895 for (int i = 0; i < NB_PLANES; i++) {
239 5516 exec.in[i] = (void *) src1[i];
240 5516 exec.out[i] = dst1 + i * LINES * dst1_stride;
241 5516 exec.block_size_in[i] = comp_new->block_size * test->pixel_bits_in >> 3;
242 5516 exec.block_size_out[i] = comp_new->block_size * test->pixel_bits_out >> 3;
243 }
244 1379 checkasm_call_checked(comp_new->func, &exec, comp_new->priv, 0, 0, PIXELS / comp_new->block_size, LINES);
245
246
2/2
✓ Branch 0 taken 5516 times.
✓ Branch 1 taken 1379 times.
6895 for (int i = 0; i < NB_PLANES; i++) {
247
2/2
✓ Branch 0 taken 1453 times.
✓ Branch 1 taken 4063 times.
5516 if (!SWS_COMP_TEST(test->planes_out, i))
248 1453 continue;
249
250 /* checkasm_check_padded() assumes a power of two alignment, but our
251 * overwrite may be an arbitrary number of bytes. Smooth out the
252 * difference by manually copying over the padding from the reference
253 * to the output buffer, and extending the checked padding past it. */
254 4063 const int elem_size = ff_sws_pixel_type_size(test->type_out);
255 4063 const int over_write = comp_new->over_write[i];
256 4063 const int padding = (over_write + elem_size - 1) / elem_size + 8;
257 4063 const int w = write_size / elem_size;
258 4063 const uint8_t *out_ref = dst0 + i * LINES * dst0_stride;
259 4063 uint8_t *out_new = exec.out[i];
260
2/2
✓ Branch 0 taken 65008 times.
✓ Branch 1 taken 4063 times.
69071 for (int y = 0; y < LINES; y++) {
261 65008 memcpy(&out_new[y * dst1_stride + write_size],
262 65008 &out_ref[y * dst0_stride + write_size], over_write);
263 }
264
265 4063 const char *desc = FMT("%s[%d]", test->name, i);
266
4/5
✓ Branch 0 taken 762 times.
✓ Branch 1 taken 744 times.
✓ Branch 2 taken 475 times.
✓ Branch 3 taken 2082 times.
✗ Branch 4 not taken.
4063 switch (test->type_out) {
267 762 case SWS_PIXEL_U8:
268 762 checkasm_check_padded(uint8_t, out_ref, dst0_stride, out_new, dst1_stride,
269 w, LINES, desc, 1, 0, padding);
270 762 break;
271 744 case SWS_PIXEL_U16:
272 744 checkasm_check_padded(uint16_t, (void *) out_ref, dst0_stride,
273 (void *) out_new, dst1_stride,
274 w, LINES, desc, 1, 0, padding);
275 744 break;
276 475 case SWS_PIXEL_U32:
277 475 checkasm_check_padded(uint32_t, (void *) out_ref, dst0_stride,
278 (void *) out_new, dst1_stride,
279 w, LINES, desc, 1, 0, padding);
280 475 break;
281 2082 case SWS_PIXEL_F32:
282 2082 checkasm_check_padded(float_ulp, (void *) out_ref, dst0_stride,
283 (void *) out_new, dst1_stride,
284 w, LINES, desc, 0, 1, 0, padding);
285 2082 break;
286 }
287 }
288
289
1/18
✗ Branch 1 not taken.
✓ Branch 2 taken 1379 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 41 not taken.
✗ Branch 42 not taken.
✗ Branch 43 not taken.
✗ Branch 44 not taken.
✗ Branch 45 not taken.
✗ Branch 46 not taken.
✗ Branch 47 not taken.
✗ Branch 48 not taken.
✗ Branch 51 not taken.
✗ Branch 52 not taken.
✗ Branch 54 not taken.
✗ Branch 55 not taken.
✗ Branch 59 not taken.
✗ Branch 60 not taken.
1379 bench(comp_new->func, &exec, comp_new->priv, 0, 0, PIXELS / comp_new->block_size, LINES);
290 }
291
292 9128 static void run_test(const Test *test)
293 {
294 9128 SwsContext *ctx = sws_alloc_context();
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9128 times.
9128 if (!ctx)
296 ✗ return;
297 9128 ctx->flags = SWS_BITEXACT;
298
299 /* Generate dummy uop list on-stack */
300 9128 SwsUOpList oplist = {
301 9128 .ops = (SwsUOp *) test->uops,
302 9128 .num_ops = test->num_uops,
303
304 /* Less efficient, but works universally */
305 .planes_in = SWS_COMP_ALL,
306 .planes_out = SWS_COMP_ALL,
307 };
308
309
2/2
✓ Branch 0 taken 22708 times.
✓ Branch 1 taken 9128 times.
31836 for (int i = 0; i < test->num_uops; i++) {
310 22708 const int pixel_size = ff_sws_pixel_type_size(test->uops[i].type);
311
2/2
✓ Branch 0 taken 12208 times.
✓ Branch 1 taken 10500 times.
22708 if (pixel_size > oplist.pixel_size_max)
312 12208 oplist.pixel_size_max = pixel_size;
313 }
314
315 static const SwsOpBackend *backend_ref;
316
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9127 times.
9128 if (!backend_ref) {
317
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 for (int n = 0; ff_sws_op_backends[n]; n++) {
318
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (!strcmp(ff_sws_op_backends[n]->name, "c")) {
319 1 backend_ref = ff_sws_op_backends[n];
320 1 break;
321 }
322 }
323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(backend_ref);
324 }
325
326 /* Always compile `ops` using the C backend as a reference */
327 9128 SwsCompiledOp comp_ref = {0};
328 9128 int ret = backend_ref->compile_uops(ctx, &oplist, &comp_ref);
329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9128 times.
9128 if (ret < 0) {
330 ✗ av_assert0(ret != AVERROR(ENOTSUP));
331 ✗ fail();
332 ✗ goto done;
333 }
334
335 /* Check with the C backend to establish a reference */
336 9128 check_compiled(test, &comp_ref, &comp_ref);
337
338 /* Iterate over every other backend, and test it against the C reference */
339
2/2
✓ Branch 0 taken 27384 times.
✓ Branch 1 taken 9128 times.
36512 for (int n = 0; ff_sws_op_backends[n]; n++) {
340 27384 const SwsOpBackend *backend = ff_sws_op_backends[n];
341
3/4
✓ Branch 0 taken 27384 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9128 times.
✓ Branch 3 taken 18256 times.
27384 if (backend->hw_format != AV_PIX_FMT_NONE || backend == backend_ref)
342 26157 continue;
343
2/2
✓ Branch 0 taken 9128 times.
✓ Branch 1 taken 9128 times.
18256 if (!backend->compile_uops)
344 9128 continue;
345
346 9128 SwsCompiledOp comp_new = {0};
347 9128 int ret = backend->compile_uops(ctx, &oplist, &comp_new);
348
2/2
✓ Branch 0 taken 7901 times.
✓ Branch 1 taken 1227 times.
9128 if (ret == AVERROR(ENOTSUP)) {
349 7901 continue;
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1227 times.
1227 } else if (ret < 0) {
351 ✗ fail();
352 ✗ goto done;
353 }
354
355 /* Distinguish backends from each other even with same CPU flags */
356 1227 checkasm_set_func_variant("%s_%s", backend->name, checkasm_get_cpu_suffix());
357 1227 check_compiled(test, &comp_ref, &comp_new);
358 1227 ff_sws_compiled_op_unref(&comp_new);
359 }
360
361 9128 done:
362 9128 ff_sws_compiled_op_unref(&comp_ref);
363 9128 sws_free_context(&ctx);
364 }
365
366 9128 static void check_uop(const char *name, const SwsUOp *uop,
367 SwsCompMask mask_in, SwsCompMask mask_out,
368 const unsigned ranges[NB_PLANES])
369 {
370 9128 Test t = {
371 .name = name,
372 9128 .type_in = uop->type,
373 9128 .type_out = uop->type,
374 };
375
376
4/4
✓ Branch 0 taken 7770 times.
✓ Branch 1 taken 7574 times.
✓ Branch 2 taken 6216 times.
✓ Branch 3 taken 1554 times.
15344 for (int i = 0; ranges && i < NB_PLANES; i++)
377 6216 t.ranges[i] = ranges[i];
378
379 /* uops with non-standard output type conversions */
380
7/8
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 196 times.
✓ Branch 2 taken 126 times.
✓ Branch 3 taken 266 times.
✓ Branch 4 taken 70 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4032 times.
✓ Branch 7 taken 4340 times.
9128 switch (uop->uop) {
381 98 case SWS_UOP_TO_U8: t.type_out = SWS_PIXEL_U8; break;
382 196 case SWS_UOP_TO_U16: t.type_out = SWS_PIXEL_U16; break;
383 126 case SWS_UOP_TO_U32: t.type_out = SWS_PIXEL_U32; break;
384 266 case SWS_UOP_TO_F32: t.type_out = SWS_PIXEL_F32; break;
385 70 case SWS_UOP_EXPAND_PAIR: t.type_out = SWS_PIXEL_U16; break;
386 ✗ case SWS_UOP_EXPAND_QUAD: t.type_out = SWS_PIXEL_U32; break;
387 4032 case SWS_UOP_READ_PLANAR_FH:
388 case SWS_UOP_READ_PLANAR_FV:
389 4032 t.type_out = uop->par.filter.type;
390 4032 break;
391 }
392
393 /* Set up read/write metadata for rw uops */
394 9128 const int bits_in = ff_sws_pixel_type_size(t.type_in) * 8;
395 9128 const int bits_out = ff_sws_pixel_type_size(t.type_out) * 8;
396
10/10
✓ Branch 0 taken 4186 times.
✓ Branch 1 taken 168 times.
✓ Branch 2 taken 126 times.
✓ Branch 3 taken 126 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 14 times.
✓ Branch 6 taken 14 times.
✓ Branch 7 taken 14 times.
✓ Branch 8 taken 14 times.
✓ Branch 9 taken 4452 times.
9128 switch (uop->uop) {
397 4186 case SWS_UOP_READ_PLANAR:
398 case SWS_UOP_READ_PLANAR_FH:
399 case SWS_UOP_READ_PLANAR_FV:
400 4186 t.planes_in = uop->mask;
401 4186 t.pixel_bits_in = bits_in;
402 4186 break;
403 168 case SWS_UOP_WRITE_PLANAR:
404 168 t.planes_out = uop->mask;
405 168 t.pixel_bits_out = bits_out;
406 168 break;
407 126 case SWS_UOP_READ_PACKED:
408 126 t.planes_in = SWS_COMP_ELEMS(1);
409 126 t.pixel_bits_in = bits_in * SWS_COMP_COUNT(uop->mask);
410 126 break;
411 126 case SWS_UOP_WRITE_PACKED:
412 126 t.planes_out = SWS_COMP_ELEMS(1);
413 126 t.pixel_bits_out = bits_out * SWS_COMP_COUNT(uop->mask);
414 126 break;
415 14 case SWS_UOP_READ_NIBBLE:
416 14 t.planes_in = SWS_COMP_ELEMS(1);
417 14 t.pixel_bits_in = 4;
418 14 break;
419 14 case SWS_UOP_WRITE_NIBBLE:
420 14 t.planes_out = SWS_COMP_ELEMS(1);
421 14 t.pixel_bits_out = 4;
422 14 break;
423 14 case SWS_UOP_READ_BIT:
424 14 t.planes_in = SWS_COMP_ELEMS(1);
425 14 t.pixel_bits_in = 1;
426 14 break;
427 14 case SWS_UOP_WRITE_BIT:
428 14 t.planes_out = SWS_COMP_ELEMS(1);
429 14 t.pixel_bits_out = 1;
430 14 break;
431 14 case SWS_UOP_READ_PALETTE:
432 14 t.planes_in = SWS_COMP_ELEMS(2);
433 14 t.pixel_bits_in = 8; /* size of index */
434 14 break;
435 }
436
437 /* Add read uop if needed */
438
2/2
✓ Branch 0 taken 4774 times.
✓ Branch 1 taken 4354 times.
9128 if (!t.planes_in) {
439 4774 t.planes_in = mask_in;
440 4774 t.pixel_bits_in = bits_in;
441 4774 t.uops[t.num_uops++] = (SwsUOp) {
442 4774 .type = pixel_type_to_int(t.type_in),
443 .uop = SWS_UOP_READ_PLANAR,
444 .mask = SWS_COMP_ALL, /* extra planes are zero'd */
445 };
446 }
447
448 /* Add the UOp itself */
449 9128 t.uops[t.num_uops++] = *uop;
450
451 /* Add write uop if needed */
452
2/2
✓ Branch 0 taken 8806 times.
✓ Branch 1 taken 322 times.
9128 if (!t.planes_out) {
453 8806 t.planes_out = mask_out;
454 8806 t.pixel_bits_out = bits_out;
455 8806 t.uops[t.num_uops++] = (SwsUOp) {
456 8806 .type = pixel_type_to_int(t.type_out),
457 .uop = SWS_UOP_WRITE_PLANAR,
458 .mask = SWS_COMP_ALL, /* extra planes are ignored */
459 };
460 }
461
462 9128 run_test(&t);
463 9128 }
464
465 2212 static void check_range(const char *name, const SwsUOp *uop,
466 const unsigned ranges[NB_PLANES])
467 {
468 /* Test all planes to ensure data remains untouched */
469 2212 check_uop(name, uop, SWS_COMP_ALL, SWS_COMP_ALL, ranges);
470 2212 }
471
472 1988 static void check_simple(const char *name, const SwsUOp *uop)
473 {
474 1988 check_range(name, uop, NULL);
475 1988 }
476
477 336 static void check_vec4(const char *name, SwsUOp *uop)
478 {
479
2/2
✓ Branch 0 taken 1344 times.
✓ Branch 1 taken 336 times.
1680 for (int i = 0; i < 4; i++)
480 1344 uop->data.vec4[i] = rndpx(uop->type);
481
482 336 check_simple(name, uop);
483 336 }
484
485 462 static void check_clear(const char *name, SwsUOp *uop)
486 {
487 /* Clear values are always expanded to 32 bits, see SwsClearUOp */
488
2/2
✓ Branch 0 taken 1848 times.
✓ Branch 1 taken 462 times.
2310 for (int i = 0; i < 4; i++)
489 1848 uop->data.vec4[i] = ff_sws_pixel_expand32(uop->type, rndpx(uop->type));
490
491 462 check_simple(name, uop);
492 462 }
493
494 #define MK_RANGES(R) ((const unsigned[]) { R, R, R, R })
495
496 4354 static void check_read(const char *name, SwsUOp *uop)
497 {
498 4354 check_uop(name, uop, uop->mask, uop->mask, NULL);
499 4354 }
500
501 322 static void check_write(const char *name, const SwsUOp *uop)
502 {
503
2/2
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 14 times.
322 const int frac = uop->uop == SWS_UOP_WRITE_BIT ? 3 :
504 308 uop->uop == SWS_UOP_WRITE_NIBBLE ? 1 : 0;
505 322 const int bits = 8 >> frac;
506 322 const unsigned range = (1 << bits) - 1;
507 322 check_uop(name, uop, uop->mask, uop->mask, MK_RANGES(range));
508 322 }
509
510 336 static void check_filter(const char *name, SwsUOp *uop)
511 {
512 336 const bool is_vert = uop->uop == SWS_UOP_READ_PLANAR_FV;
513
514 672 SwsFilterParams par = {
515 .scaler_params = { SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT },
516
2/2
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 168 times.
336 .dst_size = is_vert ? LINES : PIXELS,
517 };
518
519 336 const SwsScaler scalers[] = {
520 SWS_SCALE_POINT,
521 SWS_SCALE_SINC,
522 };
523
524
2/2
✓ Branch 0 taken 672 times.
✓ Branch 1 taken 336 times.
1008 for (int s = 0; s < FF_ARRAY_ELEMS(scalers); s++) {
525 672 par.scaler = scalers[s];
526
527
2/2
✓ Branch 0 taken 4032 times.
✓ Branch 1 taken 672 times.
4704 for (par.src_size = 1; par.src_size <= par.dst_size; par.src_size <<= 1) {
528
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4032 times.
4032 if (ff_sws_filter_generate(NULL, &par, &uop->data.kernel) < 0) {
529 ✗ fail();
530 ✗ return;
531 }
532
533 char desc[256];
534 4032 snprintf(desc, sizeof(desc), "%s_%s_%d", name,
535 4032 uop->data.kernel->name, par.src_size);
536
537 4032 check_read(desc, uop);
538 4032 av_refstruct_unref(&uop->data.kernel);
539 }
540 }
541 }
542
543 1232 static void check_swizzle(const char *name, const SwsUOp *uop)
544 {
545 /* Only check data equality in needed components; since the others
546 * could either remain untouched or contain garbage */
547 1232 check_uop(name, uop, SWS_COMP_ALL, uop->mask, NULL);
548 1232 }
549
550 28 static void check_expand_bit(const char *name, const SwsUOp *uop)
551 {
552 28 check_range(name, uop, MK_RANGES(1));
553 28 }
554
555 756 static void check_cast(const char *name, const SwsUOp *uop)
556 {
557 SwsPixelType dst;
558
5/7
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 196 times.
✓ Branch 2 taken 126 times.
✓ Branch 3 taken 266 times.
✓ Branch 4 taken 70 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
756 switch (uop->uop) {
559 98 case SWS_UOP_TO_U8: dst = SWS_PIXEL_U8; break;
560 196 case SWS_UOP_TO_U16: dst = SWS_PIXEL_U16; break;
561 126 case SWS_UOP_TO_U32: dst = SWS_PIXEL_U32; break;
562 266 case SWS_UOP_TO_F32: dst = SWS_PIXEL_F32; break;
563 70 case SWS_UOP_EXPAND_PAIR: dst = SWS_PIXEL_U16; break;
564 ✗ case SWS_UOP_EXPAND_QUAD: dst = SWS_PIXEL_U32; break;
565 ✗ default: return;
566 }
567
568 756 const int isize = ff_sws_pixel_type_size(uop->type);
569 756 const int osize = ff_sws_pixel_type_size(dst);
570 756 unsigned range = UINT32_MAX >> (32 - osize * 8);
571
4/4
✓ Branch 0 taken 364 times.
✓ Branch 1 taken 392 times.
✓ Branch 2 taken 56 times.
✓ Branch 3 taken 308 times.
756 if (isize < osize || !ff_sws_pixel_type_is_int(dst))
572 448 range = 0;
573
574 756 check_uop(name, uop, uop->mask, uop->mask, MK_RANGES(range));
575 }
576
577 140 static void check_scale(const char *name, SwsUOp *uop)
578 {
579 140 const int bits = ff_sws_pixel_type_size(uop->type) * 8;
580 140 const unsigned max = UINT32_MAX >> (32 - bits);
581 140 SwsPixel scale = uop->data.scalar = rndpx(uop->type);
582 140 unsigned range = 0;
583
584 /* Ensure the result won't exceed the value range */
585
4/4
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 28 times.
✓ Branch 3 taken 70 times.
140 switch (uop->type) {
586
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 case SWS_PIXEL_U8: range = max / (scale.u8 ? scale.u8 : 1); break;
587
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 case SWS_PIXEL_U16: range = max / (scale.u16 ? scale.u16 : 1); break;
588
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 case SWS_PIXEL_U32: range = max / (scale.u32 ? scale.u32 : 1); break;
589 }
590
591 140 check_range(name, uop, MK_RANGES(range));
592 140 }
593
594 140 static void check_unpack(const char *name, const SwsUOp *uop)
595 {
596 140 const uint8_t *pat = uop->par.pack.pattern;
597 140 const int total = pat[0] + pat[1] + pat[2] + pat[3];
598 140 const unsigned range = UINT32_MAX >> (32 - total);
599
600 140 check_uop(name, uop, SWS_COMP(0), uop->mask, MK_RANGES(range));
601 140 }
602
603 112 static void check_pack(const char *name, const SwsUOp *uop)
604 {
605 112 const uint8_t *pat = uop->par.pack.pattern;
606 112 const unsigned ranges[4] = {
607 112 (1 << pat[0]) - 1, (1 << pat[1]) - 1,
608 112 (1 << pat[2]) - 1, (1 << pat[3]) - 1,
609 };
610
611 112 check_uop(name, uop, uop->mask, SWS_COMP(0), ranges);
612 112 }
613
614 378 static void check_linear(const char *name, SwsUOp *uop)
615 {
616 378 const SwsLinearUOp *par = &uop->par.lin;
617
2/2
✓ Branch 0 taken 1512 times.
✓ Branch 1 taken 378 times.
1890 for (int i = 0; i < 4; i++) {
618
2/2
✓ Branch 0 taken 7560 times.
✓ Branch 1 taken 1512 times.
9072 for (int j = 0; j < 5; j++) {
619
2/2
✓ Branch 0 taken 5054 times.
✓ Branch 1 taken 2506 times.
7560 if (par->zero & SWS_MASK(i, j))
620 5054 uop->data.mat4x5[i][j] = constpx(uop->type, 0);
621
2/2
✓ Branch 0 taken 644 times.
✓ Branch 1 taken 1862 times.
2506 else if (par->one & SWS_MASK(i, j))
622 644 uop->data.mat4x5[i][j] = constpx(uop->type, 1);
623 else
624 1862 uop->data.mat4x5[i][j] = rndpx(uop->type);
625 }
626 }
627
628 378 check_simple(name, uop);
629 378 }
630
631 336 static void check_dither(const char *name, SwsUOp *uop)
632 {
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
336 av_assert0(!ff_sws_pixel_type_is_int(uop->type));
634
635 336 const int size = 1 << uop->par.dither.size_log2;
636 336 const int height = ff_sws_dither_height(&uop->par.dither);
637 336 SwsPixel *matrix = av_refstruct_allocz(size * height * sizeof(*matrix));
638
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 336 times.
336 if (!matrix) {
639 ✗ fail();
640 ✗ return;
641 }
642
643
2/2
✓ Branch 0 taken 86016 times.
✓ Branch 1 taken 336 times.
86352 for (int i = 0; i < size * size; i++)
644 86016 matrix[i] = rndpx(uop->type);
645 336 memcpy(matrix + size * size, matrix,
646 336 size * (height - size) * sizeof(*matrix));
647
648 336 uop->data.ptr = matrix;
649 336 check_simple(name, uop);
650
651 336 av_refstruct_unref(&matrix);
652 }
653
654 56 static void check_lut_3d(const char *name, SwsUOp *uop)
655 {
656 56 SwsLut3D *lut3d = ff_sws_lut3d_alloc();
657
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 if (!lut3d) {
658 ✗ fail();
659 ✗ return;
660 }
661
662 56 checkasm_init(&lut3d->input, sizeof(lut3d->input));
663
664
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 28 times.
56 if (uop->par.lut3d.dynamic) {
665 28 checkasm_init(&lut3d->tone_map, sizeof(lut3d->tone_map));
666 28 checkasm_init(&lut3d->output, sizeof(lut3d->output));
667 28 lut3d->dynamic = true;
668
669 /* Prevent out-of-bounds read from IPT values with abs(PT) > 0.5 */
670
2/2
✓ Branch 0 taken 7224 times.
✓ Branch 1 taken 28 times.
7252 for (int i = 0; i < FF_ARRAY_ELEMS(lut3d->tone_map); i++)
671 7224 lut3d->tone_map[i].y = FFMIN(lut3d->tone_map[i].y, 1 << 15);
672 }
673
674 56 uop->data.lut3d = lut3d;
675 56 check_range(name, uop, MK_RANGES(INPUT_LUT_SIZE - 1));
676
677 56 av_refstruct_unref(&lut3d);
678 }
679
680 #define CHECK_FUNCTION(CHECK, NAME, ...) \
681 CHECK(#NAME, &(SwsUOp) { __VA_ARGS__ });
682
683 #define CHECK_FOR(UOP, CHECK) \
684 SWS_FOR_STRUCT(U8, UOP, CHECK_FUNCTION, CHECK) \
685 SWS_FOR_STRUCT(U16, UOP, CHECK_FUNCTION, CHECK) \
686 SWS_FOR_STRUCT(U32, UOP, CHECK_FUNCTION, CHECK) \
687 SWS_FOR_STRUCT(F32, UOP, CHECK_FUNCTION, CHECK) \
688 report(#UOP)
689
690 14 void checkasm_check_sw_ops(void)
691 {
692 14 CHECK_FOR(READ_PLANAR, check_read);
693 14 CHECK_FOR(READ_PLANAR_FH, check_filter);
694 14 CHECK_FOR(READ_PLANAR_FV, check_filter);
695 14 CHECK_FOR(READ_PACKED, check_read);
696 14 CHECK_FOR(READ_NIBBLE, check_read);
697 14 CHECK_FOR(READ_BIT, check_read);
698 14 CHECK_FOR(READ_PALETTE, check_read);
699 14 CHECK_FOR(WRITE_PLANAR, check_write);
700 14 CHECK_FOR(WRITE_PACKED, check_write);
701 14 CHECK_FOR(WRITE_NIBBLE, check_write);
702 14 CHECK_FOR(WRITE_BIT, check_write);
703 14 CHECK_FOR(PERMUTE, check_swizzle);
704 14 CHECK_FOR(COPY, check_swizzle);
705 14 CHECK_FOR(SWAP_BYTES, check_simple);
706 14 CHECK_FOR(EXPAND_BIT, check_expand_bit);
707 14 CHECK_FOR(EXPAND_PAIR, check_cast);
708 14 CHECK_FOR(EXPAND_QUAD, check_cast);
709 14 CHECK_FOR(TO_U8, check_cast);
710 14 CHECK_FOR(TO_U16, check_cast);
711 14 CHECK_FOR(TO_U32, check_cast);
712 14 CHECK_FOR(TO_F32, check_cast);
713 14 CHECK_FOR(SCALE, check_scale);
714 14 CHECK_FOR(ADD, check_vec4);
715 14 CHECK_FOR(MIN, check_vec4);
716 14 CHECK_FOR(MAX, check_vec4);
717 14 CHECK_FOR(UNPACK, check_unpack);
718 14 CHECK_FOR(PACK, check_pack);
719 14 CHECK_FOR(LSHIFT, check_simple);
720 14 CHECK_FOR(RSHIFT, check_simple);
721 14 CHECK_FOR(CLEAR, check_clear);
722 14 CHECK_FOR(LINEAR, check_linear);
723 14 CHECK_FOR(DITHER, check_dither);
724 14 CHECK_FOR(LUT_3D, check_lut_3d);
725 14 }
726