FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/tests/swscale.c
Date: 2026-08-26 01:27:24
Exec Total Coverage
Lines: 313 674 46.4%
Functions: 18 26 69.2%
Branches: 187 482 38.8%

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 2 static void exit_handler(int sig)
123 {
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 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 2 exit(sig);
132 }
133
134 /* Estimate luma variance assuming uniform dither noise distribution */
135 119385 static float estimate_quantization_noise(enum AVPixelFormat fmt)
136 {
137 119385 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
138 119385 float variance = 1.0 / 12;
139
2/2
✓ Branch 0 taken 11937 times.
✓ Branch 1 taken 107448 times.
119385 if (desc->comp[0].depth < 8) {
140 /* Extra headroom for very low bit depth output */
141 11937 variance *= (8 - desc->comp[0].depth);
142 }
143
144
2/2
✓ Branch 0 taken 7287 times.
✓ Branch 1 taken 112098 times.
119385 if (desc->flags & AV_PIX_FMT_FLAG_FLOAT) {
145 7287 return 0.0;
146
2/2
✓ Branch 0 taken 38365 times.
✓ Branch 1 taken 73733 times.
112098 } else if (desc->flags & AV_PIX_FMT_FLAG_RGB) {
147 38365 const float r = 0.299 / (1 << desc->comp[0].depth);
148 38365 const float g = 0.587 / (1 << desc->comp[1].depth);
149 38365 const float b = 0.114 / (1 << desc->comp[2].depth);
150 38365 return (r * r + g * g + b * b) * variance;
151 } else {
152 73733 const float y = 1.0 / (1 << desc->comp[0].depth);
153 73733 return y * y * variance;
154 }
155 }
156
157 79590 static int fmt_comps(enum AVPixelFormat fmt)
158 {
159 79590 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
160
2/2
✓ Branch 0 taken 67518 times.
✓ Branch 1 taken 12072 times.
79590 int comps = desc->nb_components >= 3 ? 0x7 : 0x1;
161
2/2
✓ Branch 0 taken 24255 times.
✓ Branch 1 taken 55335 times.
79590 if (desc->flags & AV_PIX_FMT_FLAG_ALPHA)
162 24255 comps |= 0x8;
163 79590 return comps;
164 }
165
166 39791 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 159164 times.
✓ Branch 1 taken 39791 times.
198955 for (int p = 0; p < 4; p++) {
173 159164 const int stride_a = out->linesize[p];
174 159164 const int stride_b = ref->linesize[p];
175 159164 const int w = out->width;
176 159164 const int h = out->height;
177
178
4/4
✓ Branch 0 taken 119373 times.
✓ Branch 1 taken 39791 times.
✓ Branch 2 taken 39791 times.
✓ Branch 3 taken 79582 times.
159164 const int is_chroma = p == 1 || p == 2;
179
2/2
✓ Branch 0 taken 79582 times.
✓ Branch 1 taken 79582 times.
159164 const uint8_t def = is_chroma ? 128 : 0xFF;
180 159164 const int has_ref = comps & (1 << p);
181 159164 double sum = 0;
182 159164 int count = 0;
183
184 /* 4x4 SSIM */
185
2/2
✓ Branch 0 taken 3819936 times.
✓ Branch 1 taken 159164 times.
3979100 for (int y = 0; y < (h & ~3); y += 4) {
186
2/2
✓ Branch 0 taken 91678464 times.
✓ Branch 1 taken 3819936 times.
95498400 for (int x = 0; x < (w & ~3); x += 4) {
187 91678464 const float c1 = .01 * .01 * 255 * 255 * 64;
188 91678464 const float c2 = .03 * .03 * 255 * 255 * 64 * 63;
189 91678464 int s1 = 0, s2 = 0, ss = 0, s12 = 0, var, covar;
190
191
2/2
✓ Branch 0 taken 366713856 times.
✓ Branch 1 taken 91678464 times.
458392320 for (int yy = 0; yy < 4; yy++) {
192
2/2
✓ Branch 0 taken 1466855424 times.
✓ Branch 1 taken 366713856 times.
1833569280 for (int xx = 0; xx < 4; xx++) {
193 1466855424 int a = out->data[p][(y + yy) * stride_a + x + xx];
194
2/2
✓ Branch 0 taken 927304704 times.
✓ Branch 1 taken 539550720 times.
1466855424 int b = has_ref ? ref->data[p][(y + yy) * stride_b + x + xx] : def;
195 1466855424 s1 += a;
196 1466855424 s2 += b;
197 1466855424 ss += a * a + b * b;
198 1466855424 s12 += a * b;
199 }
200 }
201
202 91678464 var = ss * 64 - s1 * s1 - s2 * s2;
203 91678464 covar = s12 * 64 - s1 * s2;
204 91678464 sum += (2 * s1 * s2 + c1) * (2 * covar + c2) /
205 91678464 ((s1 * s1 + s2 * s2 + c1) * (var + c2));
206 91678464 count++;
207 }
208 }
209
210
1/2
✓ Branch 0 taken 159164 times.
✗ Branch 1 not taken.
159164 ssim[p] = count ? sum / count : 0.0;
211 }
212 39791 }
213
214 79586 static float get_loss(const float ssim[4])
215 {
216 79586 const float weights[3] = { 0.8, 0.1, 0.1 }; /* tuned for Y'CbCr */
217
218 79586 float sum = 0;
219
2/2
✓ Branch 0 taken 238758 times.
✓ Branch 1 taken 79586 times.
318344 for (int i = 0; i < 3; i++)
220 238758 sum += weights[i] * ssim[i];
221 79586 sum *= ssim[3]; /* ensure alpha errors get caught */
222
223 79586 return 1.0 - sum;
224 }
225
226 39791 static void unref_buffers(AVFrame *frame)
227 {
228
1/2
✓ Branch 0 taken 39791 times.
✗ Branch 1 not taken.
39791 for (int i = 0; i < FF_ARRAY_ELEMS(frame->buf); i++) {
229
1/2
✓ Branch 0 taken 39791 times.
✗ Branch 1 not taken.
39791 if (!frame->buf[i])
230 39791 break;
231 av_buffer_unref(&frame->buf[i]);
232 }
233
234 39791 memset(frame->data, 0, sizeof(frame->data));
235 39791 memset(frame->linesize, 0, sizeof(frame->linesize));
236 39791 }
237
238 79848 static int checked_sws_scale_frame(SwsContext *c, AVFrame *dst, const AVFrame *src)
239 {
240 79848 int ret = sws_scale_frame(c, dst, src);
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79848 times.
79848 if (ret < 0) {
242 av_log(NULL, AV_LOG_ERROR, "Failed %s ---> %s\n",
243 av_get_pix_fmt_name(src->format), av_get_pix_fmt_name(dst->format));
244 }
245 79848 return ret;
246 }
247
248 39795 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 39795 sws_src_dst->flags = mode->flags;
253 39795 sws_src_dst->dither = mode->dither;
254 39795 sws_src_dst->scaler = mode->scaler;
255 39795 sws_src_dst->scaler_sub = mode->scaler_sub;
256 39795 sws_src_dst->threads = opts->threads;
257 39795 sws_src_dst->backends = opts->backends;
258
259 39795 int ret = sws_frame_setup(sws_src_dst, dst, src);
260
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 39791 times.
39795 if (ret < 0) {
261 4 av_log(NULL, AV_LOG_ERROR, "Failed to setup %s ---> %s\n",
262 4 av_get_pix_fmt_name(src->format), av_get_pix_fmt_name(dst->format));
263 4 return ret;
264 }
265
266 39791 int64_t time = av_gettime_relative();
267
3/4
✓ Branch 0 taken 79582 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39791 times.
✓ Branch 3 taken 39791 times.
79582 for (int i = 0; ret >= 0 && i < opts->iters; i++) {
268 39791 unref_buffers(dst);
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39791 times.
39791 if (opts->align_dst) {
270 ret = av_frame_get_buffer(dst, opts->align_dst);
271 if (ret < 0)
272 return ret;
273 }
274
275 39791 ret = checked_sws_scale_frame(sws_src_dst, dst, src);
276 }
277 39791 *out_time = av_gettime_relative() - time;
278
279 39791 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 if (src->format == dst->format)
341 return AVERROR(ENOTSUP);
342
343 sws_hw = sws_alloc_context();
344 if (!sws_hw) {
345 ret = AVERROR(ENOMEM);
346 goto error;
347 }
348
349 sws_hw->flags = mode->flags;
350 sws_hw->dither = mode->dither;
351
352 in_ref = av_hwframe_ctx_alloc(hw_device_ctx);
353 if (!in_ref) {
354 ret = AVERROR(ENOMEM);
355 goto error;
356 }
357
358 in_ctx = (AVHWFramesContext *)in_ref->data;
359 in_ctx->format = AV_PIX_FMT_VULKAN;
360 in_ctx->sw_format = src->format;
361 in_ctx->width = src->width;
362 in_ctx->height = src->height;
363 ret = av_hwframe_ctx_init(in_ref);
364 if (ret < 0) {
365 if (ret != AVERROR(ENOTSUP))
366 av_log(NULL, AV_LOG_ERROR, "Failed to create input HW context: %s\n",
367 av_err2str(ret));
368 goto error;
369 }
370
371 out_ref = av_hwframe_ctx_alloc(hw_device_ctx);
372 if (!out_ref) {
373 ret = AVERROR(ENOMEM);
374 goto error;
375 }
376
377 out_ctx = (AVHWFramesContext *) out_ref->data;
378 out_ctx->format = AV_PIX_FMT_VULKAN;
379 out_ctx->sw_format = dst->format;
380 out_ctx->width = dst->width;
381 out_ctx->height = dst->height;
382 ret = av_hwframe_ctx_init(out_ref);
383 if (ret < 0) {
384 if (ret != AVERROR(ENOTSUP))
385 av_log(NULL, AV_LOG_ERROR, "Failed to create output HW context: %s\n",
386 av_err2str(ret));
387 goto error;
388 }
389
390 in_f = av_frame_alloc();
391 if (!in_f) {
392 ret = AVERROR(ENOMEM);
393 goto error;
394 }
395 in_f->width = src->width;
396 in_f->height = src->height;
397 in_f->format = AV_PIX_FMT_VULKAN;
398 ret = av_hwframe_get_buffer(in_ref, in_f, 0);
399 if (ret < 0) {
400 av_log(NULL, AV_LOG_ERROR, "Failed to allocate input HW frame\n");
401 goto error;
402 }
403
404 ret = av_hwframe_transfer_data(in_f, src, 0);
405 if (ret < 0) {
406 av_log(NULL, AV_LOG_ERROR, "Failed to upload HW frame\n");
407 goto error;
408 }
409
410 out_f = av_frame_alloc();
411 if (!out_f) {
412 ret = AVERROR(ENOMEM);
413 goto error;
414 }
415 out_f->width = dst->width;
416 out_f->height = dst->height;
417 out_f->format = AV_PIX_FMT_VULKAN;
418 ret = av_hwframe_get_buffer(out_ref, out_f, 0);
419 if (ret < 0) {
420 av_log(NULL, AV_LOG_ERROR, "Failed to allocate output HW frame\n");
421 goto error;
422 }
423
424 int64_t time = av_gettime_relative();
425 for (int i = 0; ret >= 0 && i < opts->iters; i++) {
426 ret = checked_sws_scale_frame(sws_hw, out_f, in_f);
427 if (ret < 0)
428 goto error;
429 }
430 *out_time = av_gettime_relative() - time;
431
432 ret = av_frame_get_buffer(dst, 0);
433 if (ret < 0)
434 goto error;
435
436 ret = av_hwframe_transfer_data(dst, out_f, 0);
437 if (ret < 0) {
438 av_log(NULL, AV_LOG_ERROR, "Failed to download HW frame\n");
439 goto error;
440 }
441
442 ret = 0;
443
444 error:
445 av_frame_free(&in_f);
446 av_frame_free(&out_f);
447 av_buffer_unref(&in_ref);
448 av_buffer_unref(&out_ref);
449 sws_free_context(&sws_hw);
450 return ret;
451 }
452
453 static void print_test_params(char *buf, size_t buf_size,
454 const AVFrame *src, const AVFrame *dst,
455 const struct mode *mode, const struct options *opts)
456 {
457 snprintf(buf, buf_size,
458 "%-*s %*dx%*d -> %-*s %*dx%*d, flags=0x%0*x dither=%u scaler=%d/%d",
459 opts->pretty ? 14 : 0, av_get_pix_fmt_name(src->format),
460 opts->pretty ? 4 : 0, src->width,
461 opts->pretty ? 4 : 0, src->height,
462 opts->pretty ? 14 : 0, av_get_pix_fmt_name(dst->format),
463 opts->pretty ? 4 : 0, dst->width,
464 opts->pretty ? 4 : 0, dst->height,
465 opts->pretty ? 8 : 0, mode->flags,
466 mode->dither,
467 mode->scaler,
468 mode->scaler_sub);
469 }
470
471 39791 static void print_results(const AVFrame *ref, const AVFrame *src, const AVFrame *dst,
472 int dst_w, int dst_h,
473 const struct mode *mode, const struct options *opts,
474 const struct test_results *r,
475 const struct test_results *ref_r,
476 float expected_loss)
477 {
478 char buf[128];
479
480
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 39791 times.
39791 if (av_log_get_level() >= AV_LOG_INFO) {
481 print_test_params(buf, sizeof(buf), src, dst, mode, opts);
482 printf("%s", buf);
483
484 if (!opts->bench || !ref_r) {
485 printf(", SSIM={Y=%f U=%f V=%f A=%f} loss=%e",
486 r->ssim[0], r->ssim[1], r->ssim[2], r->ssim[3],
487 r->loss);
488 if (ref_r)
489 printf(" (ref=%e)", ref_r->loss);
490 }
491
492 if (opts->bench) {
493 printf(", time=%*"PRId64"/%u us",
494 opts->pretty ? 7 : 0, r->time, opts->iters);
495 if (ref_r) {
496 double ratio = ((double) ref_r->time / ref_r->iters)
497 / ((double) r->time / opts->iters);
498 if (FFMIN(r->time, ref_r->time) > 100 /* don't pollute stats with low precision */) {
499 speedup_min = FFMIN(speedup_min, ratio);
500 speedup_max = FFMAX(speedup_max, ratio);
501 speedup_logavg += log(ratio);
502 speedup_count++;
503 }
504
505 printf(" (ref=%*"PRId64"/%u us), speedup=%*.3fx %s%s\033[0m",
506 opts->pretty ? 7 : 0, ref_r->time, ref_r->iters,
507 opts->pretty ? 6 : 0, ratio,
508 speedup_color(ratio), ratio >= 1.0 ? "faster" : "slower");
509 }
510 }
511 printf("\n");
512
513 fflush(stdout);
514 }
515
516
6/6
✓ Branch 0 taken 1765 times.
✓ Branch 1 taken 38026 times.
✓ Branch 2 taken 952 times.
✓ Branch 3 taken 813 times.
✓ Branch 4 taken 621 times.
✓ Branch 5 taken 331 times.
39791 if (r->loss - expected_loss > 1e-4 && dst_w >= ref->width && dst_h >= ref->height) {
517 621 const int bad = r->loss - expected_loss > 1e-2;
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 621 times.
621 const int level = bad ? AV_LOG_ERROR : AV_LOG_WARNING;
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 621 times.
621 const char *worse_str = bad ? "WORSE" : "worse";
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 621 times.
621 if (bad) {
521 print_test_params(buf, sizeof(buf), src, dst, mode, opts);
522 av_log(NULL, level, "%s\n", buf);
523 }
524 621 av_log(NULL, level,
525 " loss %e is %s by %e, expected loss %e\n",
526 621 r->loss, worse_str, r->loss - expected_loss, expected_loss);
527 }
528
529
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 39791 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
39791 if (ref_r && r->loss - ref_r->loss > 1e-4) {
530 /**
531 * The new scaling code does not (yet) perform error diffusion for
532 * low bit depth output, which impacts the SSIM score slightly for
533 * very low bit-depth formats (e.g. monow, monob). Since this is an
534 * expected result, drop the badness from an error to a warning for
535 * such cases. This can be removed again once error diffusion is
536 * implemented in the new ops code.
537 */
538 const int dst_bits = av_pix_fmt_desc_get(dst->format)->comp[0].depth;
539 const int bad = r->loss - ref_r->loss > 1e-2 && dst_bits > 1;
540 const int level = bad ? AV_LOG_ERROR : AV_LOG_WARNING;
541 const char *worse_str = bad ? "WORSE" : "worse";
542 if (bad) {
543 print_test_params(buf, sizeof(buf), src, dst, mode, opts);
544 av_log(NULL, level, "%s\n", buf);
545 }
546 av_log(NULL, level,
547 " loss %e is %s by %e, ref loss %e SSIM={Y=%f U=%f V=%f A=%f}\n",
548 r->loss, worse_str, r->loss - ref_r->loss, ref_r->loss,
549 ref_r->ssim[0], ref_r->ssim[1], ref_r->ssim[2], ref_r->ssim[3]);
550 }
551 39791 }
552
553 79850 static int init_frame(AVFrame **pframe, const AVFrame *ref,
554 int width, int height, enum AVPixelFormat format)
555 {
556 79850 AVFrame *frame = av_frame_alloc();
557
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 79850 times.
79850 if (!frame)
558 return AVERROR(ENOMEM);
559 79850 av_frame_copy_props(frame, ref);
560 79850 frame->width = width;
561 79850 frame->height = height;
562 79850 frame->format = format;
563 79850 *pframe = frame;
564 79850 return 0;
565 }
566
567 /* Runs a series of ref -> src -> dst -> out, and compares out vs ref */
568 39795 static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt,
569 int dst_w, int dst_h,
570 const struct mode *mode, const struct options *opts,
571 const AVFrame *ref, AVFrame **psrc,
572 const struct test_results *ref_r)
573 {
574 39795 AVFrame *src = *psrc;
575 39795 AVFrame *dst = NULL, *out = NULL;
576 39795 const int comps = fmt_comps(src_fmt) & fmt_comps(dst_fmt);
577 int ret;
578
579 /* Estimate the expected amount of loss from bit depth reduction */
580 39795 const float c1 = 0.01 * 0.01; /* stabilization constant */
581 39795 const float ref_var = 1.0 / 12.0; /* uniformly distributed signal */
582 39795 const float src_var = estimate_quantization_noise(src_fmt);
583 39795 const float dst_var = estimate_quantization_noise(dst_fmt);
584 39795 const float out_var = estimate_quantization_noise(ref->format);
585 39795 const float total_var = src_var + dst_var + out_var;
586 39795 const float ssim_luma = (2 * ref_var + c1) / (2 * ref_var + total_var + c1);
587 39795 const float ssim_expected[4] = { ssim_luma, 1, 1, 1 }; /* for simplicity */
588 39795 const float expected_loss = get_loss(ssim_expected);
589
590 39795 struct test_results r = { 0 };
591
592
4/4
✓ Branch 0 taken 39793 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 262 times.
✓ Branch 3 taken 39531 times.
39795 if (!src || src->format != src_fmt) {
593 264 av_frame_free(psrc);
594 264 ret = init_frame(&src, ref, ref->width, ref->height, src_fmt);
595
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 264 times.
264 if (ret < 0)
596 goto error;
597
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 264 times.
264 if (opts->align_src) {
598 ret = av_frame_get_buffer(src, opts->align_src);
599 if (ret < 0)
600 goto error;
601 }
602
603 264 ret = checked_sws_scale_frame(sws_ref_src, src, ref);
604
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 264 times.
264 if (ret < 0)
605 goto error;
606 264 *psrc = src;
607 }
608
609 39795 ret = init_frame(&dst, ref, dst_w, dst_h, dst_fmt);
610
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39795 times.
39795 if (ret < 0)
611 goto error;
612
613 ret = (opts->api == IMPL_LEGACY) ? scale_legacy(dst, src, mode, opts, &r.time)
614
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39795 times.
79590 : hw_device_ctx ? scale_hw(dst, src, mode, opts, &r.time)
615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39795 times.
39795 : scale_new(dst, src, mode, opts, &r.time);
616
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 39791 times.
39795 if (ret < 0) {
617
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (ret == AVERROR(ENOTSUP))
618 4 ret = 0;
619 4 goto error;
620 }
621
622 39791 ret = init_frame(&out, ref, ref->width, ref->height, ref->format);
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39791 times.
39791 if (ret < 0)
624 goto error;
625
626 39791 ret = checked_sws_scale_frame(sws_dst_out, out, dst);
627
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39791 times.
39791 if (ret < 0)
628 goto error;
629
630 39791 get_ssim(r.ssim, out, ref, comps);
631
632
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39791 times.
39791 if (opts->api == IMPL_LEGACY) {
633 /* Legacy swscale does not perform bit accurate upconversions of low
634 * bit depth RGB. This artificially improves the SSIM score because the
635 * resulting error deletes some of the input dither noise. This gives
636 * it an unfair advantage when compared against a bit exact reference.
637 * Work around this by ensuring that the resulting SSIM score is not
638 * higher than it theoretically "should" be. */
639 if (src_var > dst_var) {
640 const float src_loss = (2 * ref_var + c1) / (2 * ref_var + src_var + c1);
641 r.ssim[0] = FFMIN(r.ssim[0], src_loss);
642 }
643 }
644
645 39791 r.loss = get_loss(r.ssim);
646
2/8
✓ Branch 0 taken 39791 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 39791 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
39791 if ((opts->api != IMPL_LEGACY) && r.loss - expected_loss > 1e-2 && dst_w >= ref->width && dst_h >= ref->height) {
647 ret = -1;
648 goto bad_loss;
649 }
650
651
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 39791 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
39791 if (ref_r && r.loss - ref_r->loss > 1e-2) {
652 ret = -1;
653 goto bad_loss;
654 }
655
656 39791 ret = 0; /* fall through */
657
658 39791 bad_loss:
659 39791 print_results(ref, src, dst,
660 dst_w, dst_h,
661 mode, opts,
662 &r, ref_r,
663 expected_loss);
664
665 39795 error:
666 39795 av_frame_free(&dst);
667 39795 av_frame_free(&out);
668 39795 return ret;
669 }
670
671 50652 static inline int fmt_is_subsampled(enum AVPixelFormat fmt)
672 {
673
2/2
✓ Branch 1 taken 36666 times.
✓ Branch 2 taken 13986 times.
87318 return av_pix_fmt_desc_get(fmt)->log2_chroma_w != 0 ||
674
2/2
✓ Branch 1 taken 1134 times.
✓ Branch 2 taken 35532 times.
36666 av_pix_fmt_desc_get(fmt)->log2_chroma_h != 0;
675 }
676
677 static inline int fmt_is_supported_by_hw(enum AVPixelFormat fmt)
678 {
679 /* Semi-planar formats are only supported by the legacy path, which
680 * does not support hardware frames. */
681 if (fmt == AV_PIX_FMT_NV24 || fmt == AV_PIX_FMT_P410 ||
682 fmt == AV_PIX_FMT_P412 || fmt == AV_PIX_FMT_P416)
683 return 0;
684 for (int i = 0;
685 hw_device_constr->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
686 if (hw_device_constr->valid_sw_formats[i] == fmt)
687 return 1;
688 }
689 return 0;
690 }
691
692 122744 static inline int fmt_disabled(const struct options *opts, enum AVPixelFormat fmt)
693 {
694
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 122744 times.
122744 const int scaler_sub = opts->scaler_sub ? opts->scaler_sub : opts->scaler;
695
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 122744 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 50652 times.
✓ Branch 6 taken 72092 times.
173396 return (hw_device_constr && !fmt_is_supported_by_hw(fmt)) ||
696
2/2
✓ Branch 1 taken 15120 times.
✓ Branch 2 taken 35532 times.
50652 (scaler_sub < 0 && fmt_is_subsampled(fmt));
697 }
698
699 107168 static inline int test_formats(const struct options *opts,
700 enum AVPixelFormat src, enum AVPixelFormat dst)
701 {
702 /* Test auxiliary conversions */
703
4/4
✓ Branch 1 taken 84048 times.
✓ Branch 2 taken 23120 times.
✓ Branch 3 taken 8974 times.
✓ Branch 4 taken 75074 times.
191216 if (!ff_sws_test_pixfmt_backend(sws_ref_src->backends, src, 1) ||
704 84048 !ff_sws_test_pixfmt_backend(sws_dst_out->backends, dst, 0))
705 32094 return 0;
706
707 /* Test main conversion */
708
1/2
✓ Branch 0 taken 75074 times.
✗ Branch 1 not taken.
75074 enum SwsBackend backend = opts->backends ? opts->backends : SWS_BACKEND_STABLE;
709
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 75074 times.
75074 if (opts->api == IMPL_LEGACY)
710 backend = SWS_BACKEND_LEGACY; /* Legacy API forces legacy backend */
711
4/4
✓ Branch 1 taken 53724 times.
✓ Branch 2 taken 21350 times.
✓ Branch 3 taken 34848 times.
✓ Branch 4 taken 18876 times.
128798 return ff_sws_test_pixfmt_backend(backend, src, 0) &&
712 53724 ff_sws_test_pixfmt_backend(backend, dst, 1);
713 }
714
715 2 static int run_self_tests(const AVFrame *ref, const struct options *opts)
716 {
717 2 const int dst_w_values[] = { opts->w, opts->w - opts->w / 3, opts->w + opts->w / 3 };
718 2 const int dst_h_values[] = { opts->h, opts->h - opts->h / 3, opts->h + opts->h / 3 };
719
720 enum AVPixelFormat src_fmt, dst_fmt,
721 2 src_fmt_min = 0,
722 2 dst_fmt_min = 0,
723 2 src_fmt_max = AV_PIX_FMT_NB - 1,
724 2 dst_fmt_max = AV_PIX_FMT_NB - 1;
725
726 2 AVFrame *src = NULL;
727
728 2 int ret = 0;
729
730
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (opts->src_fmt != AV_PIX_FMT_NONE)
731 src_fmt_min = src_fmt_max = opts->src_fmt;
732
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (opts->dst_fmt != AV_PIX_FMT_NONE)
733 dst_fmt_min = dst_fmt_max = opts->dst_fmt;
734
735
2/2
✓ Branch 0 taken 536 times.
✓ Branch 1 taken 2 times.
538 for (src_fmt = src_fmt_min; src_fmt <= src_fmt_max; src_fmt++) {
736
2/2
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 456 times.
536 if (fmt_disabled(opts, src_fmt))
737 80 continue;
738
2/2
✓ Branch 0 taken 122208 times.
✓ Branch 1 taken 456 times.
122664 for (dst_fmt = dst_fmt_min; dst_fmt <= dst_fmt_max; dst_fmt++) {
739
2/2
✓ Branch 1 taken 15040 times.
✓ Branch 2 taken 107168 times.
122208 if (fmt_disabled(opts, dst_fmt))
740 15040 continue;
741
2/2
✓ Branch 1 taken 72320 times.
✓ Branch 2 taken 34848 times.
107168 if (!test_formats(opts, src_fmt, dst_fmt))
742 72320 continue;
743
2/2
✓ Branch 0 taken 69696 times.
✓ Branch 1 taken 17424 times.
87120 for (int h = 0; h < FF_ARRAY_ELEMS(dst_h_values); h++) {
744
2/2
✓ Branch 0 taken 174240 times.
✓ Branch 1 taken 52272 times.
226512 for (int w = 0; w < FF_ARRAY_ELEMS(dst_w_values); w++) {
745
2/2
✓ Branch 0 taken 1271952 times.
✓ Branch 1 taken 156816 times.
1428768 for (int f = 0; f < FF_ARRAY_ELEMS(flags); f++) {
746 2543904 struct mode mode = {
747
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1271952 times.
1271952 .flags = opts->flags >= 0 ? opts->flags : flags[f],
748
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1271952 times.
1271952 .dither = opts->dither >= 0 ? opts->dither : SWS_DITHER_AUTO,
749 1271952 .scaler = opts->scaler >= 0 ? opts->scaler : SWS_SCALE_AUTO,
750 1271952 .scaler_sub = opts->scaler_sub >= 0 ? opts->scaler_sub : SWS_SCALE_AUTO,
751 };
752
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1271952 times.
1271952 int dst_w = (opts->dst_w >= 0) ? opts->dst_w : dst_w_values[w];
753
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1271952 times.
1271952 int dst_h = (opts->dst_h >= 0) ? opts->dst_h : dst_h_values[h];
754
755
6/6
✓ Branch 0 taken 1254528 times.
✓ Branch 1 taken 17424 times.
✓ Branch 2 taken 418176 times.
✓ Branch 3 taken 836352 times.
✓ Branch 4 taken 139392 times.
✓ Branch 5 taken 278784 times.
1271952 if (opts->scaler >= 0 && opts->w == dst_w && opts->h == dst_h)
756 139392 continue;
757
758
2/2
✓ Branch 1 taken 39795 times.
✓ Branch 2 taken 1092765 times.
1132560 if (ff_sfc64_get(&prng_state) <= UINT64_MAX * opts->prob) {
759 39795 ret = run_test(src_fmt, dst_fmt, dst_w, dst_h,
760 &mode, opts, ref, &src, NULL);
761
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 39795 times.
39795 if (ret < 0)
762 goto error;
763 }
764
765
3/4
✓ Branch 0 taken 1132560 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1115136 times.
✓ Branch 3 taken 17424 times.
1132560 if (opts->flags >= 0 || opts->scaler != SWS_SCALE_AUTO)
766 break;
767 }
768
2/4
✓ Branch 0 taken 174240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 174240 times.
✗ Branch 3 not taken.
174240 if (opts->dst_w >= 0 || opts->dst_h >= 0)
769 break;
770
2/2
✓ Branch 0 taken 17424 times.
✓ Branch 1 taken 156816 times.
174240 if (opts->scaler < 0)
771 17424 break;
772 }
773
2/4
✓ Branch 0 taken 69696 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 69696 times.
✗ Branch 3 not taken.
69696 if (opts->dst_w >= 0 || opts->dst_h >= 0)
774 break;
775
2/2
✓ Branch 0 taken 17424 times.
✓ Branch 1 taken 52272 times.
69696 if (opts->scaler < 0)
776 17424 break;
777 }
778 }
779 }
780
781 2 ret = 0;
782
783 2 error:
784 2 av_frame_free(&src);
785 2 return ret;
786 }
787
788 static int run_file_tests(const AVFrame *ref, FILE *fp, const struct options *opts)
789 {
790 char buf[256];
791 int ret = 0;
792
793 AVFrame *src = NULL;
794
795 for (int line = 1; fgets(buf, sizeof(buf), fp); line++) {
796 char src_fmt_str[21], dst_fmt_str[21];
797 enum AVPixelFormat src_fmt;
798 enum AVPixelFormat dst_fmt;
799 int sw, sh, dw, dh;
800 struct test_results r = { 0 };
801 struct mode mode;
802 int n = 0;
803
804 ret = sscanf(buf,
805 "%20s %dx%d -> %20s %dx%d, flags=0x%x dither=%u scaler=%u/%u, "
806 "SSIM={Y=%f U=%f V=%f A=%f} loss=%e%n",
807 src_fmt_str, &sw, &sh, dst_fmt_str, &dw, &dh,
808 &mode.flags, &mode.dither,
809 &mode.scaler, &mode.scaler_sub,
810 &r.ssim[0], &r.ssim[1], &r.ssim[2], &r.ssim[3],
811 &r.loss, &n);
812 if (ret != 15) {
813 av_log(NULL, AV_LOG_FATAL,
814 "Malformed reference file in line %d\n", line);
815 goto error;
816 }
817 if (opts->bench) {
818 ret = sscanf(buf + n,
819 ", time=%"PRId64"/%u us",
820 &r.time, &r.iters);
821 if (ret != 2) {
822 av_log(NULL, AV_LOG_FATAL,
823 "Missing benchmarks from reference file in line %d\n",
824 line);
825 goto error;
826 }
827 }
828
829 src_fmt = av_get_pix_fmt(src_fmt_str);
830 dst_fmt = av_get_pix_fmt(dst_fmt_str);
831 if (src_fmt == AV_PIX_FMT_NONE || dst_fmt == AV_PIX_FMT_NONE) {
832 av_log(NULL, AV_LOG_FATAL,
833 "Unknown pixel formats (%s and/or %s) in line %d\n",
834 src_fmt_str, dst_fmt_str, line);
835 goto error;
836 }
837
838 if (sw != ref->width || sh != ref->height) {
839 av_log(NULL, AV_LOG_FATAL,
840 "Mismatching dimensions %dx%d (ref is %dx%d) in line %d\n",
841 sw, sh, ref->width, ref->height, line);
842 goto error;
843 }
844
845 if (opts->src_fmt != AV_PIX_FMT_NONE && src_fmt != opts->src_fmt ||
846 opts->dst_fmt != AV_PIX_FMT_NONE && dst_fmt != opts->dst_fmt)
847 continue;
848
849 ret = run_test(src_fmt, dst_fmt, dw, dh, &mode, opts, ref, &src, &r);
850 if (ret < 0)
851 goto error;
852 }
853
854 ret = 0;
855
856 error:
857 av_frame_free(&src);
858 return ret;
859 }
860
861 2 static int init_ref(AVFrame *ref, const struct options *opts)
862 {
863 2 SwsContext *ctx = sws_alloc_context();
864 2 AVFrame *rgb = av_frame_alloc();
865 AVLFG rand;
866 2 int ret = -1;
867
868
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 if (!ctx || !rgb)
869 goto error;
870
871
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 rgb->width = opts->w > 32 ? opts->w / 12 : opts->w;
872
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 rgb->height = opts->h > 32 ? opts->h / 12 : opts->h;
873 2 rgb->format = AV_PIX_FMT_RGBA;
874 2 ret = av_frame_get_buffer(rgb, 32);
875
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
876 goto error;
877
878 2 av_lfg_init(&rand, 1);
879
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 for (int y = 0; y < rgb->height; y++) {
880
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 16 times.
144 for (int x = 0; x < rgb->width; x++) {
881
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 128 times.
640 for (int c = 0; c < 4; c++)
882 512 rgb->data[0][y * rgb->linesize[0] + x * 4 + c] = av_lfg_get(&rand);
883 }
884 }
885
886 2 ctx->flags = SWS_BILINEAR | SWS_BITEXACT | SWS_ACCURATE_RND;
887 2 ret = checked_sws_scale_frame(ctx, ref, rgb);
888
889 2 error:
890 2 sws_free_context(&ctx);
891 2 av_frame_free(&rgb);
892 2 return ret;
893 }
894
895 static int parse_size(struct options *opts, const char *str, char **pbuf)
896 {
897 int ret = av_parse_video_size(&opts->w, &opts->h, str);
898 if (ret < 0 && strchr(str, ':')) {
899 av_freep(pbuf);
900 char *buf = av_strdup(str);
901 if (!buf)
902 return AVERROR(ENOMEM);
903 *pbuf = buf;
904 char *saveptr = NULL;
905 char *s = av_strtok(buf, ":", &saveptr);
906 if (s) {
907 ret = av_parse_video_size(&opts->w, &opts->h, s);
908 if (ret >= 0) {
909 s = av_strtok(NULL, ":", &saveptr);
910 if (s) {
911 ret = av_parse_video_size(&opts->dst_w, &opts->dst_h, s);
912 }
913 }
914 }
915 }
916 return ret;
917 }
918
919 static int parse_implementation(const char *str)
920 {
921 if (!strcmp(str, "legacy"))
922 return IMPL_LEGACY;
923 if (!strcmp(str, "new"))
924 return IMPL_NEW;
925 return -1;
926 }
927
928 2 static int parse_options(int argc, char **argv, struct options *opts, FILE **fp)
929 {
930 2 SwsContext *dummy = sws_alloc_context();
931 2 char *buf = NULL;
932 int ret;
933
934
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (int i = 1; i < argc; i += 2) {
935
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "--help")) {
936 fprintf(stderr,
937 "swscale [options...]\n"
938 " -help\n"
939 " This text\n"
940 " -ref <file>\n"
941 " Uses file as reference to compare tests against. Tests that have become worse will contain the string worse or WORSE\n"
942 " -p <number between 0.0 and 1.0>\n"
943 " The proportion of tests or comparisons to perform.\n"
944 " It is often convenient to perform a random subset\n"
945 " -dst <pixfmt>\n"
946 " Only test the specified destination pixel format\n"
947 " -src <pixfmt>\n"
948 " Only test the specified source pixel format\n"
949 " -s <size>[:<size>]\n"
950 " Set frame size (WxH or abbreviation)\n"
951 " Optionally set destination frame size (after a ':' separator character).\n"
952 " -bench <iters>\n"
953 " Run benchmarks with the specified number of iterations. This mode also sets the frame size to 1920x1080 (unless -s is specified)\n"
954 " -flags <flags>\n"
955 " Test with a specific combination of flags\n"
956 " -dither <mode>\n"
957 " Test with a specific dither mode\n"
958 " -backends <backends>\n"
959 " Restrict to the given set of allowed swscale backends\n"
960 " -scaler <algorithm>\n"
961 " Test with a specified scaler algorithm\n"
962 " If 'none', test only conversions that do not involve scaling\n"
963 " -scaler_sub <algorithm>\n"
964 " Test with a specified scaler algorithm for chroma planes\n"
965 " -align_src <alignment>\n"
966 " If nonzero, allocate source buffers with a custom stride alignment\n"
967 " -align_dst <alignment>\n"
968 " If nonzero, allocate destination buffers with a custom stride alignment\n"
969 " -api <new or legacy>\n"
970 " Use selected swscale API for the main conversion (default: new)\n"
971 " -hw <device>\n"
972 " Use Vulkan hardware acceleration on the specified device for the main conversion\n"
973 " -threads <threads>\n"
974 " Use the specified number of threads\n"
975 " -cpuflags <cpuflags>\n"
976 " Uses the specified cpuflags in the tests\n"
977 " -pretty <1 or 0>\n"
978 " Align fields while printing results\n"
979 " -v <level>\n"
980 " Enable log verbosity at given level\n"
981 );
982 exit(0);
983 }
984
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 if (argv[i][0] != '-' || i + 1 == argc)
985 goto bad_option;
986
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (!strcmp(argv[i], "-ref")) {
987 *fp = fopen(argv[i + 1], "r");
988 if (!*fp) {
989 fprintf(stderr, "could not open '%s'\n", argv[i + 1]);
990 ret = AVERROR(errno);
991 goto end;
992 }
993
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } else if (!strcmp(argv[i], "-cpuflags")) {
994 unsigned flags = av_get_cpu_flags();
995 ret = av_parse_cpu_caps(&flags, argv[i + 1]);
996 if (ret < 0) {
997 fprintf(stderr, "invalid cpu flags %s\n", argv[i + 1]);
998 goto end;
999 }
1000 av_force_cpu_flags(flags);
1001
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } else if (!strcmp(argv[i], "-src")) {
1002 opts->src_fmt = av_get_pix_fmt(argv[i + 1]);
1003 if (opts->src_fmt == AV_PIX_FMT_NONE) {
1004 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
1005 ret = AVERROR(EINVAL);
1006 goto end;
1007 }
1008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } else if (!strcmp(argv[i], "-dst")) {
1009 opts->dst_fmt = av_get_pix_fmt(argv[i + 1]);
1010 if (opts->dst_fmt == AV_PIX_FMT_NONE) {
1011 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
1012 ret = AVERROR(EINVAL);
1013 goto end;
1014 }
1015
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } else if (!strcmp(argv[i], "-s")) {
1016 ret = parse_size(opts, argv[i + 1], &buf);
1017 if (ret < 0) {
1018 fprintf(stderr, "invalid frame size %s\n", argv[i + 1]);
1019 goto end;
1020 }
1021
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } else if (!strcmp(argv[i], "-bench")) {
1022 int iters = atoi(argv[i + 1]);
1023 if (iters <= 0) {
1024 opts->bench = 0;
1025 opts->iters = 1;
1026 } else {
1027 opts->bench = 1;
1028 opts->iters = iters;
1029 }
1030
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 } else if (!strcmp(argv[i], "-flags")) {
1031 const AVOption *flags_opt = av_opt_find(dummy, "sws_flags", NULL, 0, 0);
1032 ret = av_opt_eval_flags(dummy, flags_opt, argv[i + 1], &opts->flags);
1033 if (ret < 0) {
1034 fprintf(stderr, "invalid flags %s\n", argv[i + 1]);
1035 goto end;
1036 }
1037
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 } else if (!strcmp(argv[i], "-backends")) {
1038 2 const AVOption *backends_opt = av_opt_find(dummy, "sws_backends", NULL, 0, 0);
1039 2 ret = av_opt_eval_flags(dummy, backends_opt, argv[i + 1], &opts->backends);
1040
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0) {
1041 fprintf(stderr, "invalid backends %s\n", argv[i + 1]);
1042 goto end;
1043 }
1044
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 } else if (!strcmp(argv[i], "-dither")) {
1045 opts->dither = atoi(argv[i + 1]);
1046
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
4 } else if (!strcmp(argv[i], "-scaler") || !strcmp(argv[i], "-scaler_sub")) {
1047 1 int opt_is_scaler = !strcmp(argv[i], "-scaler");
1048
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (opt_is_scaler && !strcmp(argv[i + 1], "none")) {
1049 1 opts->scaler = -1;
1050 1 continue;
1051 }
1052 const AVOption *scaler_opt = av_opt_find(dummy, "scaler", NULL, 0, 0);
1053 ret = av_opt_eval_int(dummy, scaler_opt, argv[i + 1], opt_is_scaler ? &opts->scaler : &opts->scaler_sub);
1054 if (ret < 0) {
1055 fprintf(stderr, "invalid scaler algorithm %s\n", argv[i + 1]);
1056 goto end;
1057 }
1058
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (!strcmp(argv[i], "-align_src")) {
1059 opts->align_src = atoi(argv[i + 1]);
1060 if (opts->align_src < 0 || (opts->align_src & (opts->align_src - 1))) {
1061 fprintf(stderr, "invalid alignment %s\n", argv[i + 1]);
1062 return -1;
1063 }
1064
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (!strcmp(argv[i], "-align_dst")) {
1065 opts->align_dst = atoi(argv[i + 1]);
1066 if (opts->align_dst < 0 || (opts->align_dst & (opts->align_dst - 1))) {
1067 fprintf(stderr, "invalid alignment %s\n", argv[i + 1]);
1068 return -1;
1069 }
1070
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (!strcmp(argv[i], "-api")) {
1071 ret = parse_implementation(argv[i + 1]);
1072 if (ret < 0) {
1073 fprintf(stderr, "invalid api %s\n", argv[i + 1]);
1074 goto end;
1075 }
1076 opts->api = ret;
1077
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (!strcmp(argv[i], "-hw")) {
1078 ret = av_hwdevice_ctx_create(&hw_device_ctx,
1079 AV_HWDEVICE_TYPE_VULKAN,
1080 argv[i + 1], NULL, 0);
1081 if (ret < 0) {
1082 fprintf(stderr, "Failed to create Vulkan device '%s'\n",
1083 argv[i + 1]);
1084 goto end;
1085 }
1086 hw_device_constr = av_hwdevice_get_hwframe_constraints(hw_device_ctx,
1087 NULL);
1088 if (!hw_device_constr) {
1089 fprintf(stderr, "Failed to retrieve Vulkan device constraints '%s'\n",
1090 argv[i + 1]);
1091 ret = AVERROR(ENOMEM);
1092 goto end;
1093 }
1094
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 } else if (!strcmp(argv[i], "-threads")) {
1095 opts->threads = atoi(argv[i + 1]);
1096
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 } else if (!strcmp(argv[i], "-p")) {
1097 1 opts->prob = atof(argv[i + 1]);
1098
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } else if (!strcmp(argv[i], "-pretty")) {
1099 opts->pretty = atoi(argv[i + 1]);
1100
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (!strcmp(argv[i], "-v")) {
1101 2 av_log_set_level(atoi(argv[i + 1]));
1102 } else {
1103 bad_option:
1104 fprintf(stderr, "bad option or argument missing (%s) see -help\n", argv[i]);
1105 ret = AVERROR(EINVAL);
1106 goto end;
1107 }
1108 }
1109
1110
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (opts->w < 0 || opts->h < 0) {
1111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 opts->w = opts->bench ? 1920 : 96;
1112
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 opts->h = opts->bench ? 1080 : 96;
1113 }
1114
1115 2 ret = 0;
1116
1117 2 end:
1118 2 sws_free_context(&dummy);
1119 2 av_freep(&buf);
1120 2 return ret;
1121 }
1122
1123 2 int main(int argc, char **argv)
1124 {
1125 2 struct options opts = {
1126 .src_fmt = AV_PIX_FMT_NONE,
1127 .dst_fmt = AV_PIX_FMT_NONE,
1128 .w = -1,
1129 .h = -1,
1130 .dst_w = -1,
1131 .dst_h = -1,
1132 .threads = 1,
1133 .iters = 1,
1134 .prob = 1.0,
1135 .flags = -1,
1136 .dither = -1,
1137 .scaler = SWS_SCALE_AUTO,
1138 .scaler_sub = SWS_SCALE_AUTO,
1139 };
1140
1141 2 AVFrame *ref = NULL;
1142 2 FILE *fp = NULL;
1143 2 int ret = -1;
1144
1145
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (parse_options(argc, argv, &opts, &fp) < 0)
1146 goto error;
1147
1148 2 ff_sfc64_init(&prng_state, 0, 0, 0, 12);
1149 2 signal(SIGINT, exit_handler);
1150
1151 2 sws_ref_src = sws_alloc_context();
1152 2 sws_src_dst = sws_alloc_context();
1153 2 sws_dst_out = sws_alloc_context();
1154
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 if (!sws_ref_src || !sws_src_dst || !sws_dst_out)
1155 goto error;
1156 2 sws_ref_src->flags = SWS_BILINEAR | SWS_BITEXACT | SWS_ACCURATE_RND;
1157 2 sws_dst_out->flags = SWS_BILINEAR | SWS_BITEXACT | SWS_ACCURATE_RND;
1158 2 sws_ref_src->backends = SWS_BACKEND_ALL;
1159 2 sws_dst_out->backends = SWS_BACKEND_ALL;
1160
1161 2 ref = av_frame_alloc();
1162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!ref)
1163 goto error;
1164 2 ref->width = opts.w;
1165 2 ref->height = opts.h;
1166 2 ref->format = AV_PIX_FMT_YUVA444P;
1167
1168 2 ret = init_ref(ref, &opts);
1169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
1170 goto error;
1171
1172 2 ret = fp ? run_file_tests(ref, fp, &opts)
1173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 : run_self_tests(ref, &opts);
1174
1175 /* fall through */
1176 2 error:
1177 2 sws_free_context(&sws_ref_src);
1178 2 sws_free_context(&sws_src_dst);
1179 2 sws_free_context(&sws_dst_out);
1180 2 av_buffer_unref(&hw_device_ctx);
1181 2 av_hwframe_constraints_free(&hw_device_constr);
1182 2 av_frame_free(&ref);
1183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (fp)
1184 fclose(fp);
1185 2 exit_handler(ret);
1186 }
1187