FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/tests/swscale.c
Date: 2026-10-19 01:23:10
Exec Total Coverage
Lines: 317 687 46.1%
Functions: 18 26 69.2%
Branches: 186 486 38.3%

Line Branch Exec Source
1 /*
2 * Copyright (C) 2024 Niklas Haas
3 * Copyright (C) 2003-2011 Michael Niedermayer <michaelni@gmx.at>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <inttypes.h>
26 #include <stdarg.h>
27 #include <signal.h>
28
29 #undef HAVE_AV_CONFIG_H
30 #include "libavutil/cpu.h"
31 #include "libavutil/avstring.h"
32 #include "libavutil/parseutils.h"
33 #include "libavutil/pixdesc.h"
34 #include "libavutil/lfg.h"
35 #include "libavutil/sfc64.h"
36 #include "libavutil/frame.h"
37 #include "libavutil/opt.h"
38 #include "libavutil/time.h"
39 #include "libavutil/pixfmt.h"
40 #include "libavutil/avassert.h"
41 #include "libavutil/macros.h"
42 #include "libavutil/hwcontext.h"
43
44 #include "libswscale/swscale.h"
45 #include "libswscale/format.h"
46
47 #define IMPL_NEW 0
48 #define IMPL_LEGACY 1
49
50 struct options {
51 enum AVPixelFormat src_fmt;
52 enum AVPixelFormat dst_fmt;
53 double prob;
54 int w, h;
55 int dst_w;
56 int dst_h;
57 int threads;
58 int iters;
59 int bench;
60 int flags;
61 int dither;
62 int scaler;
63 int scaler_sub;
64 int align_src;
65 int align_dst;
66 int api;
67 int pretty;
68 int backends;
69 };
70
71 struct mode {
72 SwsFlags flags;
73 SwsDither dither;
74 SwsScaler scaler;
75 SwsScaler scaler_sub;
76 };
77
78 struct test_results {
79 float ssim[4];
80 float loss;
81 int64_t time;
82 int iters;
83 };
84
85 const SwsFlags flags[] = {
86 0, // test defaults
87 SWS_FAST_BILINEAR,
88 SWS_BILINEAR,
89 SWS_BICUBIC,
90 SWS_X | SWS_BITEXACT,
91 SWS_POINT,
92 SWS_AREA | SWS_ACCURATE_RND,
93 SWS_BICUBIC | SWS_FULL_CHR_H_INT | SWS_FULL_CHR_H_INP,
94 };
95
96 static FFSFC64 prng_state;
97
98 /* reused between tests for efficiency */
99 static SwsContext *sws_ref_src;
100 static SwsContext *sws_src_dst;
101 static SwsContext *sws_dst_out;
102
103 static AVBufferRef *hw_device_ctx = NULL;
104 static AVHWFramesConstraints *hw_device_constr = NULL;
105
106 static double speedup_logavg;
107 static double speedup_min = 1e10;
108 static double speedup_max = 0;
109 static int speedup_count;
110
111 ✗ static const char *speedup_color(double ratio)
112 {
113 ✗ return ratio > 10.00 ? "\033[1;94m" : /* bold blue */
114 ✗ ratio > 2.00 ? "\033[1;32m" : /* bold green */
115 ✗ ratio > 1.02 ? "\033[32m" : /* green */
116 ✗ ratio > 0.98 ? "" : /* default */
117 ✗ ratio > 0.90 ? "\033[33m" : /* yellow */
118 ✗ ratio > 0.75 ? "\033[31m" : /* red */
119 "\033[1;31m"; /* bold red */
120 }
121
122 6 static void exit_handler(int sig)
123 {
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (speedup_count) {
125 ✗ double ratio = exp(speedup_logavg / speedup_count);
126 ✗ fprintf(stderr, "Overall speedup=%.3fx %s%s\033[0m, min=%.3fx max=%.3fx\n", ratio,
127 speedup_color(ratio), ratio >= 1.0 ? "faster" : "slower",
128 speedup_min, speedup_max);
129 }
130
131 6 exit(sig);
132 }
133
134 /* Estimate luma variance assuming uniform dither noise distribution */
135 395055 static float estimate_quantization_noise(enum AVPixelFormat fmt)
136 {
137 395055 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
138 395055 float variance = 1.0 / 12;
139
2/2
✓ Branch 0 taken 38937 times.
✓ Branch 1 taken 356118 times.
395055 if (desc->comp[0].depth < 8) {
140 /* Extra headroom for very low bit depth output */
141 38937 variance *= (8 - desc->comp[0].depth);
142 }
143
144
2/2
✓ Branch 0 taken 23709 times.
✓ Branch 1 taken 371346 times.
395055 if (desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
145 23709 return 0.0;
146
2/2
✓ Branch 0 taken 125808 times.
✓ Branch 1 taken 245538 times.
371346 } else if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
147 125808 const float r = 0.299 / (1 << desc->comp[0].depth);
148 125808 const float g = 0.587 / (1 << desc->comp[1].depth);
149 125808 const float b = 0.114 / (1 << desc->comp[2].depth);
150 125808 return (r * r + g * g + b * b) * variance;
151 } else {
152 245538 const float y = 1.0 / (1 << desc->comp[0].depth);
153 245538 return y * y * variance;
154 }
155 }
156
157 263370 static int fmt_comps(enum AVPixelFormat fmt)
158 {
159 263370 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
160
2/2
✓ Branch 0 taken 224277 times.
✓ Branch 1 taken 39093 times.
263370 int comps = desc->nb_components >= 3 ? 0x7 : 0x1;
161
2/2
✓ Branch 0 taken 79125 times.
✓ Branch 1 taken 184245 times.
263370 if (desc->flags & AV_PIX_FMT_FLAG_ALPHA)
162 79125 comps |= 0x8;
163 263370 return comps;
164 }
165
166 77396 static void get_ssim(float ssim[4], const AVFrame *out, const AVFrame *ref, int comps)
167 {
168 av_assert1(out->format == AV_PIX_FMT_YUVA444P);
169 av_assert1(ref->format == out->format);
170 av_assert1(ref->width == out->width && ref->height == out->height);
171
172
2/2
✓ Branch 0 taken 309584 times.
✓ Branch 1 taken 77396 times.
386980 for (int p = 0; p < 4; p++) {
173 309584 const int stride_a = out->linesize[p];
174 309584 const int stride_b = ref->linesize[p];
175 309584 const int w = out->width;
176 309584 const int h = out->height;
177
178
4/4
✓ Branch 0 taken 232188 times.
✓ Branch 1 taken 77396 times.
✓ Branch 2 taken 77396 times.
✓ Branch 3 taken 154792 times.
309584 const int is_chroma = p == 1 || p == 2;
179
2/2
✓ Branch 0 taken 154792 times.
✓ Branch 1 taken 154792 times.
309584 const uint8_t def = is_chroma ? 128 : 0xFF;
180 309584 const int has_ref = comps & (1 << p);
181 309584 double sum = 0;
182 309584 int count = 0;
183
184 /* 4x4 SSIM */
185
2/2
✓ Branch 0 taken 7430016 times.
✓ Branch 1 taken 309584 times.
7739600 for (int y = 0; y < (h & ~3); y += 4) {
186
2/2
✓ Branch 0 taken 178320384 times.
✓ Branch 1 taken 7430016 times.
185750400 for (int x = 0; x < (w & ~3); x += 4) {
187 178320384 const float c1 = .01 * .01 * 255 * 255 * 64;
188 178320384 const float c2 = .03 * .03 * 255 * 255 * 64 * 63;
189 178320384 int s1 = 0, s2 = 0, ss = 0, s12 = 0, var, covar;
190
191
2/2
✓ Branch 0 taken 713281536 times.
✓ Branch 1 taken 178320384 times.
891601920 for (int yy = 0; yy < 4; yy++) {
192
2/2
✓ Branch 0 taken 2853126144 times.
✓ Branch 1 taken 713281536 times.
3566407680 for (int xx = 0; xx < 4; xx++) {
193 2853126144 int a = out->data[p][(y + yy) * stride_a + x + xx];
194
2/2
✓ Branch 0 taken 1806170112 times.
✓ Branch 1 taken 1046956032 times.
2853126144 int b = has_ref ? ref->data[p][(y + yy) * stride_b + x + xx] : def;
195 2853126144 s1 += a;
196 2853126144 s2 += b;
197 2853126144 ss += a * a + b * b;
198 2853126144 s12 += a * b;
199 }
200 }
201
202 178320384 var = ss * 64 - s1 * s1 - s2 * s2;
203 178320384 covar = s12 * 64 - s1 * s2;
204 178320384 sum += (2 * s1 * s2 + c1) * (2 * covar + c2) /
205 178320384 ((s1 * s1 + s2 * s2 + c1) * (var + c2));
206 178320384 count++;
207 }
208 }
209
210
1/2
✓ Branch 0 taken 309584 times.
✗ Branch 1 not taken.
309584 ssim[p] = count ? sum / count : 0.0;
211 }
212 77396 }
213
214 209081 static float get_loss(const float ssim[4])
215 {
216 209081 const float weights[3] = { 0.8, 0.1, 0.1 }; /* tuned for Y'CbCr */
217
218 209081 float sum = 0;
219
2/2
✓ Branch 0 taken 627243 times.
✓ Branch 1 taken 209081 times.
836324 for (int i = 0; i < 3; i++)
220 627243 sum += weights[i] * ssim[i];
221 209081 sum *= ssim[3]; /* ensure alpha errors get caught */
222
223 209081 return 1.0 - sum;
224 }
225
226 77396 static void unref_buffers(AVFrame *frame)
227 {
228
1/2
✓ Branch 0 taken 77396 times.
✗ Branch 1 not taken.
77396 for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++) {
229
1/2
✓ Branch 0 taken 77396 times.
✗ Branch 1 not taken.
77396 if (!frame->buf[i])
230 77396 break;
231 ✗ av_buffer_unref(&frame->buf[i]);
232 }
233
234 77396 memset(frame->data, 0, sizeof(frame->data));
235 77396 memset(frame->linesize, 0, sizeof(frame->linesize));
236 77396 }
237
238 157483 static int checked_sws_scale_frame(SwsContext *c, AVFrame *dst, const AVFrame *src)
239 {
240 157483 int ret = sws_scale_frame(c, dst, src);
241
2/2
✓ Branch 0 taken 1893 times.
✓ Branch 1 taken 155590 times.
157483 if (ret < 0) {
242 1893 av_log(NULL, AV_LOG_ERROR, "Failed %s ---> %s\n",
243 1893 av_get_pix_fmt_name(src->format), av_get_pix_fmt_name(dst->format));
244 }
245 157483 return ret;
246 }
247
248 129792 static int scale_new(AVFrame *dst, const AVFrame *src,
249 const struct mode *mode, const struct options *opts,
250 int64_t *out_time)
251 {
252 129792 sws_src_dst->flags = mode->flags;
253 129792 sws_src_dst->dither = mode->dither;
254 129792 sws_src_dst->scaler = mode->scaler;
255 129792 sws_src_dst->scaler_sub = mode->scaler_sub;
256 129792 sws_src_dst->threads = opts->threads;
257 129792 sws_src_dst->backends = opts->backends;
258
259 129792 int ret = sws_frame_setup(sws_src_dst, dst, src);
260
2/2
✓ Branch 0 taken 52396 times.
✓ Branch 1 taken 77396 times.
129792 if (ret < 0) {
261 52396 av_log(NULL, AV_LOG_ERROR, "Failed to setup %s ---> %s\n",
262 52396 av_get_pix_fmt_name(src->format), av_get_pix_fmt_name(dst->format));
263 52396 return ret;
264 }
265
266 77396 int64_t time = av_gettime_relative();
267
3/4
✓ Branch 0 taken 154792 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 77396 times.
✓ Branch 3 taken 77396 times.
154792 for (int i = 0; ret >= 0 && i < opts->iters; i++) {
268 77396 unref_buffers(dst);
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77396 times.
77396 if (opts->align_dst) {
270 ✗ ret = av_frame_get_buffer(dst, opts->align_dst);
271 ✗ if (ret < 0)
272 ✗ return ret;
273 }
274
275 77396 ret = checked_sws_scale_frame(sws_src_dst, dst, src);
276 }
277 77396 *out_time = av_gettime_relative() - time;
278
279 77396 return ret;
280 }
281
282 ✗ static int scale_legacy(AVFrame *dst, const AVFrame *src,
283 const struct mode *mode, const struct options *opts,
284 int64_t *out_time)
285 {
286 SwsContext *sws_legacy;
287 int ret;
288
289 ✗ sws_legacy = sws_alloc_context();
290 ✗ if (!sws_legacy)
291 ✗ return -1;
292
293 ✗ sws_legacy->src_w = src->width;
294 ✗ sws_legacy->src_h = src->height;
295 ✗ sws_legacy->src_format = src->format;
296 ✗ sws_legacy->dst_w = dst->width;
297 ✗ sws_legacy->dst_h = dst->height;
298 ✗ sws_legacy->dst_format = dst->format;
299 ✗ sws_legacy->flags = mode->flags;
300 ✗ sws_legacy->dither = mode->dither;
301 ✗ sws_legacy->scaler = mode->scaler;
302 ✗ sws_legacy->scaler_sub = mode->scaler_sub;
303 ✗ sws_legacy->threads = opts->threads;
304
305 ✗ av_frame_unref(dst);
306 ✗ dst->width = sws_legacy->dst_w;
307 ✗ dst->height = sws_legacy->dst_h;
308 ✗ dst->format = sws_legacy->dst_format;
309 ✗ ret = av_frame_get_buffer(dst, opts->align_dst);
310 ✗ if (ret < 0)
311 ✗ goto error;
312
313 ✗ ret = sws_init_context(sws_legacy, NULL, NULL);
314 ✗ if (ret < 0)
315 ✗ goto error;
316
317 ✗ int64_t time = av_gettime_relative();
318 ✗ for (int i = 0; ret >= 0 && i < opts->iters; i++)
319 ✗ ret = checked_sws_scale_frame(sws_legacy, dst, src);
320 ✗ *out_time = av_gettime_relative() - time;
321
322 ✗ error:
323 ✗ sws_freeContext(sws_legacy);
324 ✗ return ret;
325 }
326
327 ✗ static int scale_hw(AVFrame *dst, const AVFrame *src,
328 const struct mode *mode, const struct options *opts,
329 int64_t *out_time)
330 {
331 ✗ SwsContext *sws_hw = NULL;
332 ✗ AVBufferRef *in_ref = NULL;
333 ✗ AVBufferRef *out_ref = NULL;
334 ✗ AVHWFramesContext *in_ctx = NULL;
335 ✗ AVHWFramesContext *out_ctx = NULL;
336 ✗ AVFrame *in_f = NULL;
337 ✗ AVFrame *out_f = NULL;
338 int ret;
339
340 ✗ sws_hw = sws_alloc_context();
341 ✗ if (!sws_hw) {
342 ✗ ret = AVERROR(ENOMEM);
343 ✗ goto error;
344 }
345
346 ✗ sws_hw->flags = mode->flags;
347 ✗ sws_hw->dither = mode->dither;
348 ✗ sws_hw->scaler = mode->scaler;
349 ✗ sws_hw->scaler_sub = mode->scaler_sub;
350 ✗ sws_hw->threads = opts->threads;
351 ✗ sws_hw->backends = opts->backends;
352
353 ✗ in_ref = av_hwframe_ctx_alloc(hw_device_ctx);
354 ✗ if (!in_ref) {
355 ✗ ret = AVERROR(ENOMEM);
356 ✗ goto error;
357 }
358
359 ✗ in_ctx = (AVHWFramesContext *)in_ref->data;
360 ✗ in_ctx->format = AV_PIX_FMT_VULKAN;
361 ✗ in_ctx->sw_format = src->format;
362 ✗ in_ctx->width = src->width;
363 ✗ in_ctx->height = src->height;
364 ✗ ret = av_hwframe_ctx_init(in_ref);
365 ✗ if (ret < 0) {
366 ✗ if (ret != AVERROR(ENOTSUP))
367 ✗ av_log(NULL, AV_LOG_ERROR, "Failed to create input HW context: %s\n",
368 ✗ av_err2str(ret));
369 ✗ goto error;
370 }
371
372 ✗ out_ref = av_hwframe_ctx_alloc(hw_device_ctx);
373 ✗ if (!out_ref) {
374 ✗ ret = AVERROR(ENOMEM);
375 ✗ goto error;
376 }
377
378 ✗ out_ctx = (AVHWFramesContext *) out_ref->data;
379 ✗ out_ctx->format = AV_PIX_FMT_VULKAN;
380 ✗ out_ctx->sw_format = dst->format;
381 ✗ out_ctx->width = dst->width;
382 ✗ out_ctx->height = dst->height;
383 ✗ ret = av_hwframe_ctx_init(out_ref);
384 ✗ if (ret < 0) {
385 ✗ if (ret != AVERROR(ENOTSUP))
386 ✗ av_log(NULL, AV_LOG_ERROR, "Failed to create output HW context: %s\n",
387 ✗ av_err2str(ret));
388 ✗ goto error;
389 }
390
391 ✗ in_f = av_frame_alloc();
392 ✗ if (!in_f) {
393 ✗ ret = AVERROR(ENOMEM);
394 ✗ goto error;
395 }
396 ✗ in_f->width = src->width;
397 ✗ in_f->height = src->height;
398 ✗ in_f->format = AV_PIX_FMT_VULKAN;
399 ✗ ret = av_hwframe_get_buffer(in_ref, in_f, 0);
400 ✗ if (ret < 0) {
401 ✗ av_log(NULL, AV_LOG_ERROR, "Failed to allocate input HW frame\n");
402 ✗ goto error;
403 }
404
405 ✗ ret = av_hwframe_transfer_data(in_f, src, 0);
406 ✗ if (ret < 0) {
407 ✗ av_log(NULL, AV_LOG_ERROR, "Failed to upload HW frame\n");
408 ✗ goto error;
409 }
410
411 ✗ out_f = av_frame_alloc();
412 ✗ if (!out_f) {
413 ✗ ret = AVERROR(ENOMEM);
414 ✗ goto error;
415 }
416 ✗ out_f->width = dst->width;
417 ✗ out_f->height = dst->height;
418 ✗ out_f->format = AV_PIX_FMT_VULKAN;
419 ✗ ret = av_hwframe_get_buffer(out_ref, out_f, 0);
420 ✗ if (ret < 0) {
421 ✗ av_log(NULL, AV_LOG_ERROR, "Failed to allocate output HW frame\n");
422 ✗ goto error;
423 }
424
425 ✗ int64_t time = av_gettime_relative();
426 ✗ for (int i = 0; ret >= 0 && i < opts->iters; i++) {
427 ✗ ret = checked_sws_scale_frame(sws_hw, out_f, in_f);
428 ✗ if (ret < 0)
429 ✗ goto error;
430 }
431 ✗ *out_time = av_gettime_relative() - time;
432
433 ✗ ret = av_frame_get_buffer(dst, 0);
434 ✗ if (ret < 0)
435 ✗ goto error;
436
437 ✗ ret = av_hwframe_transfer_data(dst, out_f, 0);
438 ✗ if (ret < 0) {
439 ✗ av_log(NULL, AV_LOG_ERROR, "Failed to download HW frame\n");
440 ✗ goto error;
441 }
442
443 ✗ ret = 0;
444
445 ✗ error:
446 ✗ av_frame_free(&in_f);
447 ✗ av_frame_free(&out_f);
448 ✗ av_buffer_unref(&in_ref);
449 ✗ av_buffer_unref(&out_ref);
450 ✗ sws_free_context(&sws_hw);
451 ✗ return ret;
452 }
453
454 ✗ static void print_test_params(char *buf, size_t buf_size,
455 const AVFrame *src, const AVFrame *dst,
456 const struct mode *mode, const struct options *opts)
457 {
458 ✗ snprintf(buf, buf_size,
459 "%-*s %*dx%*d -> %-*s %*dx%*d, flags=0x%0*x dither=%u scaler=%d/%d",
460 ✗ opts->pretty ? 14 : 0, av_get_pix_fmt_name(src->format),
461 ✗ opts->pretty ? 4 : 0, src->width,
462 ✗ opts->pretty ? 4 : 0, src->height,
463 ✗ opts->pretty ? 14 : 0, av_get_pix_fmt_name(dst->format),
464 ✗ opts->pretty ? 4 : 0, dst->width,
465 ✗ opts->pretty ? 4 : 0, dst->height,
466 ✗ opts->pretty ? 8 : 0, mode->flags,
467 ✗ mode->dither,
468 ✗ mode->scaler,
469 ✗ mode->scaler_sub);
470 ✗ }
471
472 77396 static void print_results(const AVFrame *ref, const AVFrame *src, const AVFrame *dst,
473 int dst_w, int dst_h,
474 const struct mode *mode, const struct options *opts,
475 const struct test_results *r,
476 const struct test_results *ref_r,
477 float expected_loss)
478 {
479 char buf[128];
480
481
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 77396 times.
77396 if (av_log_get_level() >= AV_LOG_INFO) {
482 ✗ print_test_params(buf, sizeof(buf), src, dst, mode, opts);
483 ✗ printf("%s", buf);
484
485 ✗ if (!opts->bench || !ref_r) {
486 ✗ printf(", SSIM={Y=%f U=%f V=%f A=%f} loss=%e",
487 ✗ r->ssim[0], r->ssim[1], r->ssim[2], r->ssim[3],
488 ✗ r->loss);
489 ✗ if (ref_r)
490 ✗ printf(" (ref=%e)", ref_r->loss);
491 }
492
493 ✗ if (opts->bench) {
494 ✗ printf(", time=%*"PRId64"/%u us",
495 ✗ opts->pretty ? 7 : 0, r->time, opts->iters);
496 ✗ if (ref_r) {
497 ✗ double ratio = ((double) ref_r->time / ref_r->iters)
498 ✗ / ((double) r->time / opts->iters);
499 ✗ if (FFMIN(r->time, ref_r->time) > 100 /* don't pollute stats with low precision */) {
500 ✗ speedup_min = FFMIN(speedup_min, ratio);
501 ✗ speedup_max = FFMAX(speedup_max, ratio);
502 ✗ speedup_logavg += log(ratio);
503 ✗ speedup_count++;
504 }
505
506 ✗ printf(" (ref=%*"PRId64"/%u us), speedup=%*.3fx %s%s\033[0m",
507 ✗ opts->pretty ? 7 : 0, ref_r->time, ref_r->iters,
508 ✗ opts->pretty ? 6 : 0, ratio,
509 speedup_color(ratio), ratio >= 1.0 ? "faster" : "slower");
510 }
511 }
512 ✗ printf("\n");
513
514 ✗ fflush(stdout);
515 }
516
517
6/6
✓ Branch 0 taken 3662 times.
✓ Branch 1 taken 73734 times.
✓ Branch 2 taken 1963 times.
✓ Branch 3 taken 1699 times.
✓ Branch 4 taken 1310 times.
✓ Branch 5 taken 653 times.
77396 if (r->loss - expected_loss > 1e-4 && dst_w >= ref->width && dst_h >= ref->height) {
518 1310 const int bad = r->loss - expected_loss > 1e-2;
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1310 times.
1310 const int level = bad ? AV_LOG_ERROR : AV_LOG_WARNING;
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1310 times.
1310 const char *worse_str = bad ? "WORSE" : "worse";
521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1310 times.
1310 if (bad) {
522 ✗ print_test_params(buf, sizeof(buf), src, dst, mode, opts);
523 ✗ av_log(NULL, level, "%s\n", buf);
524 }
525 1310 av_log(NULL, level,
526 " loss %e is %s by %e, expected loss %e\n",
527 1310 r->loss, worse_str, r->loss - expected_loss, expected_loss);
528 }
529
530
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 77396 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
77396 if (ref_r && r->loss - ref_r->loss > 1e-4) {
531 /**
532 * The new scaling code does not (yet) perform error diffusion for
533 * low bit depth output, which impacts the SSIM score slightly for
534 * very low bit-depth formats (e.g. monow, monob). Since this is an
535 * expected result, drop the badness from an error to a warning for
536 * such cases. This can be removed again once error diffusion is
537 * implemented in the new ops code.
538 */
539 ✗ const int dst_bits = av_pix_fmt_desc_get(dst->format)->comp[0].depth;
540 ✗ const int bad = r->loss - ref_r->loss > 1e-2 && dst_bits > 1;
541 ✗ const int level = bad ? AV_LOG_ERROR : AV_LOG_WARNING;
542 ✗ const char *worse_str = bad ? "WORSE" : "worse";
543 ✗ if (bad) {
544 ✗ print_test_params(buf, sizeof(buf), src, dst, mode, opts);
545 ✗ av_log(NULL, level, "%s\n", buf);
546 }
547 ✗ av_log(NULL, level,
548 " loss %e is %s by %e, ref loss %e SSIM={Y=%f U=%f V=%f A=%f}\n",
549 ✗ r->loss, worse_str, r->loss - ref_r->loss, ref_r->loss,
550 ✗ ref_r->ssim[0], ref_r->ssim[1], ref_r->ssim[2], ref_r->ssim[3]);
551 }
552 77396 }
553
554 209873 static int init_frame(AVFrame **pframe, const AVFrame *ref,
555 int width, int height, enum AVPixelFormat format)
556 {
557 209873 AVFrame *frame = av_frame_alloc();
558
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 209873 times.
209873 if (!frame)
559 ✗ return AVERROR(ENOMEM);
560 209873 av_frame_copy_props(frame, ref);
561 209873 frame->width = width;
562 209873 frame->height = height;
563 209873 frame->format = format;
564 209873 *pframe = frame;
565 209873 return 0;
566 }
567
568 /* Runs a series of ref -> src -> dst -> out, and compares out vs ref */
569 131685 static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt,
570 int dst_w, int dst_h,
571 const struct mode *mode, const struct options *opts,
572 const AVFrame *ref, AVFrame **psrc,
573 const struct test_results *ref_r)
574 {
575 131685 AVFrame *src = *psrc;
576 131685 AVFrame *dst = NULL, *out = NULL;
577 131685 const int comps = fmt_comps(src_fmt) & fmt_comps(dst_fmt);
578 int ret;
579
580 /* Estimate the expected amount of loss from bit depth reduction */
581 131685 const float c1 = 0.01 * 0.01; /* stabilization constant */
582 131685 const float ref_var = 1.0 / 12.0; /* uniformly distributed signal */
583 131685 const float src_var = estimate_quantization_noise(src_fmt);
584 131685 const float dst_var = estimate_quantization_noise(dst_fmt);
585 131685 const float out_var = estimate_quantization_noise(ref->format);
586 131685 const float total_var = src_var + dst_var + out_var;
587 131685 const float ssim_luma = (2 * ref_var + c1) / (2 * ref_var + total_var + c1);
588 131685 const float ssim_expected[4] = { ssim_luma, 1, 1, 1 }; /* for simplicity */
589 131685 const float expected_loss = get_loss(ssim_expected);
590
591 131685 struct test_results r = { 0 };
592
593
4/4
✓ Branch 0 taken 129792 times.
✓ Branch 1 taken 1893 times.
✓ Branch 2 taken 792 times.
✓ Branch 3 taken 129000 times.
131685 if (!src || src->format != src_fmt) {
594 2685 av_frame_free(psrc);
595 2685 ret = init_frame(&src, ref, ref->width, ref->height, src_fmt);
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2685 times.
2685 if (ret < 0)
597 ✗ goto error;
598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2685 times.
2685 if (opts->align_src) {
599 ✗ ret = av_frame_get_buffer(src, opts->align_src);
600 ✗ if (ret < 0) {
601 ✗ av_frame_free(&src);
602 ✗ goto error;
603 }
604 }
605
606 2685 ret = checked_sws_scale_frame(sws_ref_src, src, ref);
607
2/2
✓ Branch 0 taken 1893 times.
✓ Branch 1 taken 792 times.
2685 if (ret < 0) {
608 1893 av_frame_free(&src);
609 1893 goto error;
610 }
611 792 *psrc = src;
612 }
613
614 129792 ret = init_frame(&dst, ref, dst_w, dst_h, dst_fmt);
615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129792 times.
129792 if (ret < 0)
616 ✗ goto error;
617
618 ✗ ret = (opts->api == IMPL_LEGACY) ? scale_legacy(dst, src, mode, opts, &r.time)
619
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129792 times.
259584 : hw_device_ctx ? scale_hw(dst, src, mode, opts, &r.time)
620
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 129792 times.
129792 : scale_new(dst, src, mode, opts, &r.time);
621
2/2
✓ Branch 0 taken 52396 times.
✓ Branch 1 taken 77396 times.
129792 if (ret < 0)
622 52396 goto error;
623
624 77396 ret = init_frame(&out, ref, ref->width, ref->height, ref->format);
625
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77396 times.
77396 if (ret < 0)
626 ✗ goto error;
627
628 77396 ret = checked_sws_scale_frame(sws_dst_out, out, dst);
629
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77396 times.
77396 if (ret < 0)
630 ✗ goto error;
631
632 77396 get_ssim(r.ssim, out, ref, comps);
633
634
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 77396 times.
77396 if (opts->api == IMPL_LEGACY) {
635 /* Legacy swscale does not perform bit accurate upconversions of low
636 * bit depth RGB. This artificially improves the SSIM score because the
637 * resulting error deletes some of the input dither noise. This gives
638 * it an unfair advantage when compared against a bit exact reference.
639 * Work around this by ensuring that the resulting SSIM score is not
640 * higher than it theoretically "should" be. */
641 ✗ if (src_var > dst_var) {
642 ✗ const float src_loss = (2 * ref_var + c1) / (2 * ref_var + src_var + c1);
643 ✗ r.ssim[0] = FFMIN(r.ssim[0], src_loss);
644 }
645 }
646
647 77396 r.loss = get_loss(r.ssim);
648
2/8
✓ Branch 0 taken 77396 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 77396 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
77396 if ((opts->api != IMPL_LEGACY) && r.loss - expected_loss > 1e-2 && dst_w >= ref->width && dst_h >= ref->height) {
649 ✗ ret = -1;
650 ✗ goto bad_loss;
651 }
652
653
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 77396 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
77396 if (ref_r && r.loss - ref_r->loss > 1e-2) {
654 ✗ ret = -1;
655 ✗ goto bad_loss;
656 }
657
658 77396 ret = 0; /* fall through */
659
660 77396 bad_loss:
661 77396 print_results(ref, src, dst,
662 dst_w, dst_h,
663 mode, opts,
664 &r, ref_r,
665 expected_loss);
666
667 131685 error:
668 131685 av_frame_free(&dst);
669 131685 av_frame_free(&out);
670
2/2
✓ Branch 0 taken 54289 times.
✓ Branch 1 taken 77396 times.
131685 if (ret == AVERROR(ENOTSUP))
671 54289 ret = 0;
672 131685 return ret;
673 }
674
675 154710 static inline int fmt_is_subsampled(enum AVPixelFormat fmt)
676 {
677
2/2
✓ Branch 1 taken 112308 times.
✓ Branch 2 taken 42402 times.
267018 return av_pix_fmt_desc_get(fmt)->log2_chroma_w != 0 ||
678
2/2
✓ Branch 1 taken 3438 times.
✓ Branch 2 taken 108870 times.
112308 av_pix_fmt_desc_get(fmt)->log2_chroma_h != 0;
679 }
680
681 ✗ static inline int fmt_is_supported_by_hw(enum AVPixelFormat fmt)
682 {
683 /* Semi-planar formats are only supported by the legacy path, which
684 * does not support hardware frames. */
685 ✗ if (fmt == AV_PIX_FMT_NV24 || fmt == AV_PIX_FMT_P410 ||
686 ✗ fmt == AV_PIX_FMT_P412 || fmt == AV_PIX_FMT_P416)
687 ✗ return 0;
688 ✗ for (int i = 0;
689 ✗ hw_device_constr->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
690 ✗ if (hw_device_constr->valid_sw_formats[i] == fmt)
691 ✗ return 1;
692 }
693 ✗ return 0;
694 }
695
696 374220 static inline int fmt_disabled(const struct options *opts, enum AVPixelFormat fmt)
697 {
698
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 374220 times.
374220 const int scaler_sub = opts->scaler_sub ? opts->scaler_sub : opts->scaler;
699
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 374220 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 154710 times.
✓ Branch 6 taken 219510 times.
528930 return (hw_device_constr && !fmt_is_supported_by_hw(fmt)) ||
700
2/2
✓ Branch 1 taken 45840 times.
✓ Branch 2 taken 108870 times.
154710 (scaler_sub < 0 && fmt_is_subsampled(fmt));
701 }
702
703 327000 static inline int test_formats(const struct options *opts,
704 enum AVPixelFormat src, enum AVPixelFormat dst)
705 {
706 /* Test auxiliary conversions */
707
4/4
✓ Branch 1 taken 257040 times.
✓ Branch 2 taken 69960 times.
✓ Branch 3 taken 27216 times.
✓ Branch 4 taken 229824 times.
584040 if (!ff_sws_test_pixfmt_backend(sws_ref_src->backends, src, 1) ||
708 257040 !ff_sws_test_pixfmt_backend(sws_dst_out->backends, dst, 0))
709 97176 return 0;
710
711 /* Test main conversion */
712
1/2
✓ Branch 0 taken 229824 times.
✗ Branch 1 not taken.
229824 enum SwsBackend backend = opts->backends ? opts->backends : SWS_BACKEND_STABLE;
713
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 229824 times.
229824 if (opts->api == IMPL_LEGACY)
714 ✗ backend = SWS_BACKEND_LEGACY; /* Legacy API forces legacy backend */
715
4/4
✓ Branch 1 taken 165222 times.
✓ Branch 2 taken 64602 times.
✓ Branch 3 taken 107736 times.
✓ Branch 4 taken 57486 times.
395046 return ff_sws_test_pixfmt_backend(backend, src, 0) &&
716 165222 ff_sws_test_pixfmt_backend(backend, dst, 1);
717 }
718
719 6 static int run_self_tests(const AVFrame *ref, const struct options *opts)
720 {
721 6 const int dst_w_values[] = { opts->w, opts->w - opts->w / 3, opts->w + opts->w / 3 };
722 6 const int dst_h_values[] = { opts->h, opts->h - opts->h / 3, opts->h + opts->h / 3 };
723
724 enum AVPixelFormat src_fmt, dst_fmt,
725 6 src_fmt_min = 0,
726 6 dst_fmt_min = 0,
727 6 src_fmt_max = AV_PIX_FMT_NB - 1,
728 6 dst_fmt_max = AV_PIX_FMT_NB - 1;
729
730 6 AVFrame *src = NULL;
731
732 6 int ret = 0;
733
734
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (opts->src_fmt != AV_PIX_FMT_NONE)
735 ✗ src_fmt_min = src_fmt_max = opts->src_fmt;
736
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (opts->dst_fmt != AV_PIX_FMT_NONE)
737 ✗ dst_fmt_min = dst_fmt_max = opts->dst_fmt;
738
739
2/2
✓ Branch 0 taken 1620 times.
✓ Branch 1 taken 6 times.
1626 for (src_fmt = src_fmt_min; src_fmt <= src_fmt_max; src_fmt++) {
740
2/2
✓ Branch 1 taken 240 times.
✓ Branch 2 taken 1380 times.
1620 if (fmt_disabled(opts, src_fmt))
741 240 continue;
742
2/2
✓ Branch 0 taken 372600 times.
✓ Branch 1 taken 1380 times.
373980 for (dst_fmt = dst_fmt_min; dst_fmt <= dst_fmt_max; dst_fmt++) {
743
2/2
✓ Branch 1 taken 45600 times.
✓ Branch 2 taken 327000 times.
372600 if (fmt_disabled(opts, dst_fmt))
744 45600 continue;
745
2/2
✓ Branch 1 taken 219264 times.
✓ Branch 2 taken 107736 times.
327000 if (!test_formats(opts, src_fmt, dst_fmt))
746 219264 continue;
747
2/2
✓ Branch 0 taken 215472 times.
✓ Branch 1 taken 53868 times.
269340 for (int h = 0; h < FF_ARRAY_ELEMS(dst_h_values); h++) {
748
2/2
✓ Branch 0 taken 538680 times.
✓ Branch 1 taken 161604 times.
700284 for (int w = 0; w < FF_ARRAY_ELEMS(dst_w_values); w++) {
749
2/2
✓ Branch 0 taken 3932364 times.
✓ Branch 1 taken 484812 times.
4417176 for (int f = 0; f < FF_ARRAY_ELEMS(flags); f++) {
750 7864728 struct mode mode = {
751
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3932364 times.
3932364 .flags = opts->flags >= 0 ? opts->flags : flags[f],
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3932364 times.
3932364 .dither = opts->dither >= 0 ? opts->dither : SWS_DITHER_AUTO,
753 3932364 .scaler = opts->scaler >= 0 ? opts->scaler : SWS_SCALE_AUTO,
754 3932364 .scaler_sub = opts->scaler_sub >= 0 ? opts->scaler_sub : SWS_SCALE_AUTO,
755 };
756
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3932364 times.
3932364 int dst_w = (opts->dst_w >= 0) ? opts->dst_w : dst_w_values[w];
757
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3932364 times.
3932364 int dst_h = (opts->dst_h >= 0) ? opts->dst_h : dst_h_values[h];
758
759
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 3932364 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
3932364 if (opts->scaler > SWS_SCALE_AUTO && opts->w == dst_w && opts->h == dst_h)
760 ✗ continue;
761
762
2/2
✓ Branch 1 taken 131685 times.
✓ Branch 2 taken 3800679 times.
3932364 if (ff_sfc64_get(&prng_state) <= UINT64_MAX * opts->prob) {
763 131685 ret = run_test(src_fmt, dst_fmt, dst_w, dst_h,
764 &mode, opts, ref, &src, NULL);
765
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 131685 times.
131685 if (ret < 0)
766 ✗ goto error;
767 }
768
769
3/4
✓ Branch 0 taken 3932364 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3878496 times.
✓ Branch 3 taken 53868 times.
3932364 if (opts->flags >= 0 || opts->scaler != SWS_SCALE_AUTO)
770 break;
771 }
772
2/4
✓ Branch 0 taken 538680 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 538680 times.
✗ Branch 3 not taken.
538680 if (opts->dst_w >= 0 || opts->dst_h >= 0)
773 break;
774
2/2
✓ Branch 0 taken 53868 times.
✓ Branch 1 taken 484812 times.
538680 if (opts->scaler < 0)
775 53868 break;
776 }
777
2/4
✓ Branch 0 taken 215472 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 215472 times.
✗ Branch 3 not taken.
215472 if (opts->dst_w >= 0 || opts->dst_h >= 0)
778 break;
779
2/2
✓ Branch 0 taken 53868 times.
✓ Branch 1 taken 161604 times.
215472 if (opts->scaler < 0)
780 53868 break;
781 }
782 }
783 }
784
785 6 ret = 0;
786
787 6 error:
788 6 av_frame_free(&src);
789 6 return ret;
790 }
791
792 ✗ static int run_file_tests(const AVFrame *ref, FILE *fp, const struct options *opts)
793 {
794 char buf[256];
795 ✗ int ret = 0;
796
797 ✗ AVFrame *src = NULL;
798
799 ✗ for (int line = 1; fgets(buf, sizeof(buf), fp); line++) {
800 char src_fmt_str[21], dst_fmt_str[21];
801 enum AVPixelFormat src_fmt;
802 enum AVPixelFormat dst_fmt;
803 int sw, sh, dw, dh;
804 ✗ struct test_results r = { 0 };
805 struct mode mode;
806 ✗ int n = 0;
807
808 ✗ ret = sscanf(buf,
809 "%20s %dx%d -> %20s %dx%d, flags=0x%x dither=%u scaler=%u/%u, "
810 "SSIM={Y=%f U=%f V=%f A=%f} loss=%e%n",
811 src_fmt_str, &sw, &sh, dst_fmt_str, &dw, &dh,
812 &mode.flags, &mode.dither,
813 &mode.scaler, &mode.scaler_sub,
814 &r.ssim[0], &r.ssim[1], &r.ssim[2], &r.ssim[3],
815 &r.loss, &n);
816 ✗ if (ret != 15) {
817 ✗ av_log(NULL, AV_LOG_FATAL,
818 "Malformed reference file in line %d\n", line);
819 ✗ goto error;
820 }
821 ✗ if (opts->bench) {
822 ✗ ret = sscanf(buf + n,
823 ", time=%"PRId64"/%u us",
824 &r.time, &r.iters);
825 ✗ if (ret != 2) {
826 ✗ av_log(NULL, AV_LOG_FATAL,
827 "Missing benchmarks from reference file in line %d\n",
828 line);
829 ✗ goto error;
830 }
831 }
832
833 ✗ src_fmt = av_get_pix_fmt(src_fmt_str);
834 ✗ dst_fmt = av_get_pix_fmt(dst_fmt_str);
835 ✗ if (src_fmt == AV_PIX_FMT_NONE || dst_fmt == AV_PIX_FMT_NONE) {
836 ✗ av_log(NULL, AV_LOG_FATAL,
837 "Unknown pixel formats (%s and/or %s) in line %d\n",
838 src_fmt_str, dst_fmt_str, line);
839 ✗ goto error;
840 }
841
842 ✗ if (sw != ref->width || sh != ref->height) {
843 ✗ av_log(NULL, AV_LOG_FATAL,
844 "Mismatching dimensions %dx%d (ref is %dx%d) in line %d\n",
845 ✗ sw, sh, ref->width, ref->height, line);
846 ✗ goto error;
847 }
848
849 ✗ if (opts->src_fmt != AV_PIX_FMT_NONE && src_fmt != opts->src_fmt ||
850 ✗ opts->dst_fmt != AV_PIX_FMT_NONE && dst_fmt != opts->dst_fmt)
851 ✗ continue;
852
853 ✗ ret = run_test(src_fmt, dst_fmt, dw, dh, &mode, opts, ref, &src, &r);
854 ✗ if (ret < 0)
855 ✗ goto error;
856 }
857
858 ✗ ret = 0;
859
860 ✗ error:
861 ✗ av_frame_free(&src);
862 ✗ return ret;
863 }
864
865 6 static int init_ref(AVFrame *ref, const struct options *opts)
866 {
867 6 SwsContext *ctx = sws_alloc_context();
868 6 AVFrame *rgb = av_frame_alloc();
869 AVLFG rand;
870 6 int ret = -1;
871
872
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (!ctx || !rgb)
873 ✗ goto error;
874
875
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 rgb->width = opts->w > 32 ? opts->w / 12 : opts->w;
876
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 rgb->height = opts->h > 32 ? opts->h / 12 : opts->h;
877 6 rgb->format = AV_PIX_FMT_RGBA;
878 6 ret = av_frame_get_buffer(rgb, 32);
879
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
880 ✗ goto error;
881
882 6 av_lfg_init(&rand, 1);
883
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 6 times.
54 for (int y = 0; y < rgb->height; y++) {
884
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 48 times.
432 for (int x = 0; x < rgb->width; x++) {
885
2/2
✓ Branch 0 taken 1536 times.
✓ Branch 1 taken 384 times.
1920 for (int c = 0; c < 4; c++)
886 1536 rgb->data[0][y * rgb->linesize[0] + x * 4 + c] = av_lfg_get(&rand);
887 }
888 }
889
890 6 ctx->flags = SWS_BILINEAR | SWS_BITEXACT | SWS_ACCURATE_RND;
891 6 ret = checked_sws_scale_frame(ctx, ref, rgb);
892
893 6 error:
894 6 sws_free_context(&ctx);
895 6 av_frame_free(&rgb);
896 6 return ret;
897 }
898
899 ✗ static int parse_size(struct options *opts, const char *str, char **pbuf)
900 {
901 ✗ int ret = av_parse_video_size(&opts->w, &opts->h, str);
902 ✗ if (ret < 0 && strchr(str, ':')) {
903 ✗ av_freep(pbuf);
904 ✗ char *buf = av_strdup(str);
905 ✗ if (!buf)
906 ✗ return AVERROR(ENOMEM);
907 ✗ *pbuf = buf;
908 ✗ char *saveptr = NULL;
909 ✗ char *s = av_strtok(buf, ":", &saveptr);
910 ✗ if (s) {
911 ✗ ret = av_parse_video_size(&opts->w, &opts->h, s);
912 ✗ if (ret >= 0) {
913 ✗ s = av_strtok(NULL, ":", &saveptr);
914 ✗ if (s) {
915 ✗ ret = av_parse_video_size(&opts->dst_w, &opts->dst_h, s);
916 }
917 }
918 }
919 }
920 ✗ return ret;
921 }
922
923 ✗ static int parse_implementation(const char *str)
924 {
925 ✗ if (!strcmp(str, "legacy"))
926 ✗ return IMPL_LEGACY;
927 ✗ if (!strcmp(str, "new"))
928 ✗ return IMPL_NEW;
929 ✗ return -1;
930 }
931
932 6 static int parse_options(int argc, char **argv, struct options *opts, FILE **fp)
933 {
934 6 SwsContext *dummy = sws_alloc_context();
935 6 char *buf = NULL;
936 int ret;
937
938
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 6 times.
24 for (int i = 1; i < argc; i += 2) {
939
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "--help")) {
940 ✗ fprintf(stderr,
941 "swscale [options...]\n"
942 " -help\n"
943 " This text\n"
944 " -ref <file>\n"
945 " Uses file as reference to compare tests against. Tests that have become worse will contain the string worse or WORSE\n"
946 " -p <number between 0.0 and 1.0>\n"
947 " The proportion of tests or comparisons to perform.\n"
948 " It is often convenient to perform a random subset\n"
949 " -dst <pixfmt>\n"
950 " Only test the specified destination pixel format\n"
951 " -src <pixfmt>\n"
952 " Only test the specified source pixel format\n"
953 " -s <size>[:<size>]\n"
954 " Set frame size (WxH or abbreviation)\n"
955 " Optionally set destination frame size (after a ':' separator character).\n"
956 " -bench <iters>\n"
957 " Run benchmarks with the specified number of iterations. This mode also sets the frame size to 1920x1080 (unless -s is specified)\n"
958 " -flags <flags>\n"
959 " Test with a specific combination of flags\n"
960 " -dither <mode>\n"
961 " Test with a specific dither mode\n"
962 " -backends <backends>\n"
963 " Restrict to the given set of allowed swscale backends\n"
964 " -scaler <algorithm>\n"
965 " Test with a specified scaler algorithm\n"
966 " If 'none', test only conversions that do not involve scaling\n"
967 " -scaler_sub <algorithm>\n"
968 " Test with a specified scaler algorithm for chroma planes\n"
969 " -align_src <alignment>\n"
970 " If nonzero, allocate source buffers with a custom stride alignment\n"
971 " -align_dst <alignment>\n"
972 " If nonzero, allocate destination buffers with a custom stride alignment\n"
973 " -api <new or legacy>\n"
974 " Use selected swscale API for the main conversion (default: new)\n"
975 " -hw <device>\n"
976 " Use Vulkan hardware acceleration on the specified device for the main conversion\n"
977 " If 'default', use the default device, and skip all tests if none is available\n"
978 " -threads <threads>\n"
979 " Use the specified number of threads\n"
980 " -cpuflags <cpuflags>\n"
981 " Uses the specified cpuflags in the tests\n"
982 " -pretty <1 or 0>\n"
983 " Align fields while printing results\n"
984 " -v <level>\n"
985 " Enable log verbosity at given level\n"
986 );
987 ✗ exit(0);
988 }
989
2/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
18 if (argv[i][0] != '-' || i + 1 == argc)
990 ✗ goto bad_option;
991
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!strcmp(argv[i], "-ref")) {
992 ✗ *fp = fopen(argv[i + 1], "r");
993 ✗ if (!*fp) {
994 ✗ fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
995 ✗ ret = AVERROR(errno);
996 ✗ goto end;
997 }
998
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 } else if (!strcmp(argv[i], "-cpuflags")) {
999 ✗ unsigned flags = av_get_cpu_flags();
1000 ✗ ret = av_parse_cpu_caps(&flags, argv[i + 1]);
1001 ✗ if (ret < 0) {
1002 ✗ fprintf(stderr, "invalid cpu flags %s\n", argv[i + 1]);
1003 ✗ goto end;
1004 }
1005 ✗ av_force_cpu_flags(flags);
1006
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 } else if (!strcmp(argv[i], "-src")) {
1007 ✗ opts->src_fmt = av_get_pix_fmt(argv[i + 1]);
1008 ✗ if (opts->src_fmt == AV_PIX_FMT_NONE) {
1009 ✗ fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
1010 ✗ ret = AVERROR(EINVAL);
1011 ✗ goto end;
1012 }
1013
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 } else if (!strcmp(argv[i], "-dst")) {
1014 ✗ opts->dst_fmt = av_get_pix_fmt(argv[i + 1]);
1015 ✗ if (opts->dst_fmt == AV_PIX_FMT_NONE) {
1016 ✗ fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
1017 ✗ ret = AVERROR(EINVAL);
1018 ✗ goto end;
1019 }
1020
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 } else if (!strcmp(argv[i], "-s")) {
1021 ✗ ret = parse_size(opts, argv[i + 1], &buf);
1022 ✗ if (ret < 0) {
1023 ✗ fprintf(stderr, "invalid frame size %s\n", argv[i + 1]);
1024 ✗ goto end;
1025 }
1026
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 } else if (!strcmp(argv[i], "-bench")) {
1027 ✗ int iters = atoi(argv[i + 1]);
1028 ✗ if (iters <= 0) {
1029 ✗ opts->bench = 0;
1030 ✗ opts->iters = 1;
1031 } else {
1032 ✗ opts->bench = 1;
1033 ✗ opts->iters = iters;
1034 }
1035
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 } else if (!strcmp(argv[i], "-flags")) {
1036 ✗ const AVOption *flags_opt = av_opt_find(dummy, "sws_flags", NULL, 0, 0);
1037 ✗ ret = av_opt_eval_flags(dummy, flags_opt, argv[i + 1], &opts->flags);
1038 ✗ if (ret < 0) {
1039 ✗ fprintf(stderr, "invalid flags %s\n", argv[i + 1]);
1040 ✗ goto end;
1041 }
1042
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 12 times.
18 } else if (!strcmp(argv[i], "-backends")) {
1043 6 const AVOption *backends_opt = av_opt_find(dummy, "sws_backends", NULL, 0, 0);
1044 6 ret = av_opt_eval_flags(dummy, backends_opt, argv[i + 1], &opts->backends);
1045
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0) {
1046 ✗ fprintf(stderr, "invalid backends %s\n", argv[i + 1]);
1047 ✗ goto end;
1048 }
1049
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 } else if (!strcmp(argv[i], "-dither")) {
1050 ✗ opts->dither = atoi(argv[i + 1]);
1051
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
12 } else if (!strcmp(argv[i], "-scaler") || !strcmp(argv[i], "-scaler_sub")) {
1052 3 int opt_is_scaler = !strcmp(argv[i], "-scaler");
1053
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (opt_is_scaler && !strcmp(argv[i + 1], "none")) {
1054 3 opts->scaler = -1;
1055 3 continue;
1056 }
1057 ✗ const AVOption *scaler_opt = av_opt_find(dummy, "scaler", NULL, 0, 0);
1058 ✗ ret = av_opt_eval_int(dummy, scaler_opt, argv[i + 1], opt_is_scaler ? &opts->scaler : &opts->scaler_sub);
1059 ✗ if (ret < 0) {
1060 ✗ fprintf(stderr, "invalid scaler algorithm %s\n", argv[i + 1]);
1061 ✗ goto end;
1062 }
1063
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 } else if (!strcmp(argv[i], "-align_src")) {
1064 ✗ opts->align_src = atoi(argv[i + 1]);
1065 ✗ if (opts->align_src < 0 || (opts->align_src & (opts->align_src - 1))) {
1066 ✗ fprintf(stderr, "invalid alignment %s\n", argv[i + 1]);
1067 ✗ return -1;
1068 }
1069
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 } else if (!strcmp(argv[i], "-align_dst")) {
1070 ✗ opts->align_dst = atoi(argv[i + 1]);
1071 ✗ if (opts->align_dst < 0 || (opts->align_dst & (opts->align_dst - 1))) {
1072 ✗ fprintf(stderr, "invalid alignment %s\n", argv[i + 1]);
1073 ✗ return -1;
1074 }
1075
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 } else if (!strcmp(argv[i], "-api")) {
1076 ✗ ret = parse_implementation(argv[i + 1]);
1077 ✗ if (ret < 0) {
1078 ✗ fprintf(stderr, "invalid api %s\n", argv[i + 1]);
1079 ✗ goto end;
1080 }
1081 ✗ opts->api = ret;
1082
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 } else if (!strcmp(argv[i], "-hw")) {
1083 ✗ const int hw_default = !strcmp(argv[i + 1], "default");
1084 ✗ ret = av_hwdevice_ctx_create(&hw_device_ctx,
1085 AV_HWDEVICE_TYPE_VULKAN,
1086 ✗ hw_default ? NULL : argv[i + 1], NULL, 0);
1087 ✗ if (ret < 0) {
1088 ✗ if (hw_default) {
1089 ✗ fprintf(stderr, "No Vulkan device available, skipping.\n");
1090 ✗ ret = AVERROR(ENOTSUP);
1091 } else {
1092 ✗ fprintf(stderr, "Failed to create Vulkan device '%s'\n",
1093 ✗ argv[i + 1]);
1094 }
1095 ✗ goto end;
1096 }
1097 ✗ hw_device_constr = av_hwdevice_get_hwframe_constraints(hw_device_ctx,
1098 NULL);
1099 ✗ if (!hw_device_constr) {
1100 ✗ fprintf(stderr, "Failed to retrieve Vulkan device constraints '%s'\n",
1101 ✗ argv[i + 1]);
1102 ✗ ret = AVERROR(ENOMEM);
1103 ✗ goto end;
1104 }
1105
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 } else if (!strcmp(argv[i], "-threads")) {
1106 ✗ opts->threads = atoi(argv[i + 1]);
1107
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 } else if (!strcmp(argv[i], "-p")) {
1108 3 opts->prob = atof(argv[i + 1]);
1109
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } else if (!strcmp(argv[i], "-pretty")) {
1110 ✗ opts->pretty = atoi(argv[i + 1]);
1111
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 } else if (!strcmp(argv[i], "-v")) {
1112 6 av_log_set_level(atoi(argv[i + 1]));
1113 } else {
1114 ✗ bad_option:
1115 ✗ fprintf(stderr, "bad option or argument missing (%s) see -help\n", argv[i]);
1116 ✗ ret = AVERROR(EINVAL);
1117 ✗ goto end;
1118 }
1119 }
1120
1121
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
6 if (opts->w < 0 || opts->h < 0) {
1122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 opts->w = opts->bench ? 1920 : 96;
1123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 opts->h = opts->bench ? 1080 : 96;
1124 }
1125
1126 6 ret = 0;
1127
1128 6 end:
1129 6 sws_free_context(&dummy);
1130 6 av_freep(&buf);
1131 6 return ret;
1132 }
1133
1134 6 int main(int argc, char **argv)
1135 {
1136 6 struct options opts = {
1137 .src_fmt = AV_PIX_FMT_NONE,
1138 .dst_fmt = AV_PIX_FMT_NONE,
1139 .w = -1,
1140 .h = -1,
1141 .dst_w = -1,
1142 .dst_h = -1,
1143 .threads = 1,
1144 .iters = 1,
1145 .prob = 1.0,
1146 .flags = -1,
1147 .dither = -1,
1148 .scaler = SWS_SCALE_AUTO,
1149 .scaler_sub = SWS_SCALE_AUTO,
1150 };
1151
1152 6 AVFrame *ref = NULL;
1153 6 FILE *fp = NULL;
1154
1155 6 int ret = parse_options(argc, argv, &opts, &fp);
1156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret == AVERROR(ENOTSUP)) {
1157 ✗ ret = 0;
1158 ✗ goto error;
1159 }
1160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1161 ✗ goto error;
1162
1163 6 ff_sfc64_init(&prng_state, 0, 0, 0, 12);
1164 6 signal(SIGINT, exit_handler);
1165
1166 6 sws_ref_src = sws_alloc_context();
1167 6 sws_src_dst = sws_alloc_context();
1168 6 sws_dst_out = sws_alloc_context();
1169
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
6 if (!sws_ref_src || !sws_src_dst || !sws_dst_out) {
1170 ✗ ret = AVERROR(ENOMEM);
1171 ✗ goto error;
1172 }
1173 6 sws_ref_src->flags = SWS_BILINEAR | SWS_BITEXACT | SWS_ACCURATE_RND;
1174 6 sws_dst_out->flags = SWS_BILINEAR | SWS_BITEXACT | SWS_ACCURATE_RND;
1175 6 sws_ref_src->backends = SWS_BACKEND_ALL;
1176 6 sws_dst_out->backends = SWS_BACKEND_ALL;
1177
1178 6 ref = av_frame_alloc();
1179
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!ref) {
1180 ✗ ret = AVERROR(ENOMEM);
1181 ✗ goto error;
1182 }
1183 6 ref->width = opts.w;
1184 6 ref->height = opts.h;
1185 6 ref->format = AV_PIX_FMT_YUVA444P;
1186
1187 6 ret = init_ref(ref, &opts);
1188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (ret < 0)
1189 ✗ goto error;
1190
1191 6 ret = fp ? run_file_tests(ref, fp, &opts)
1192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 : run_self_tests(ref, &opts);
1193
1194 /* fall through */
1195 6 error:
1196 6 sws_free_context(&sws_ref_src);
1197 6 sws_free_context(&sws_src_dst);
1198 6 sws_free_context(&sws_dst_out);
1199 6 av_buffer_unref(&hw_device_ctx);
1200 6 av_hwframe_constraints_free(&hw_device_constr);
1201 6 av_frame_free(&ref);
1202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (fp)
1203 ✗ fclose(fp);
1204 6 exit_handler(ret);
1205 }
1206