History

tests/swscale: rewrite on top of new API

This rewrite cleans up the code to use AVFrames and the new swscale API. The log format has also been simplified and expanded to account for the new options. (Not yet implemented) The self testing code path has also been expanded to test the new swscale implementation against the old one, to serve as an unchanging reference. This does not accomplish much yet, but serves as a framework for future work.

Niklas Haas committed 2 years ago

Commit 59c39a71 parent 2a091d41 file · +284 −381Browse files

Unified · Split

libswscale/tests/swscale.c+284−381
@@ -1,4 +1,5 @@
11 /*
2+ * Copyright (C) 2024 Nikles Haas
23 * Copyright (C) 2003-2011 Michael Niedermayer <michaelni@gmx.at>
34 *
45 * This file is part of FFmpeg.
@@ -26,424 +27,307 @@ · 21 unmodified lines
2627
2728 #undef HAVE_AV_CONFIG_H
2829 #include "libavutil/cpu.h"
29−#include "libavutil/imgutils.h"
30−#include "libavutil/mem.h"
31−#include "libavutil/avutil.h"
32−#include "libavutil/crc.h"
33−#include "libavutil/opt.h"
3430 #include "libavutil/pixdesc.h"
3531 #include "libavutil/lfg.h"
3632 #include "libavutil/sfc64.h"
33+#include "libavutil/frame.h"
34+#include "libavutil/pixfmt.h"
35+#include "libavutil/avassert.h"
36+#include "libavutil/macros.h"
3737
3838 #include "libswscale/swscale.h"
3939
40−/* HACK Duplicated from swscale_internal.h.
41− * Should be removed when a cleaner pixel format system exists. */
42−#define isGray(x) \
43− ((x) == AV_PIX_FMT_GRAY8 || \
44− (x) == AV_PIX_FMT_YA8 || \
45− (x) == AV_PIX_FMT_GRAY16BE || \
46− (x) == AV_PIX_FMT_GRAY16LE || \
47− (x) == AV_PIX_FMT_YA16BE || \
48− (x) == AV_PIX_FMT_YA16LE)
49−#define hasChroma(x) \
50− (!(isGray(x) || \
51− (x) == AV_PIX_FMT_MONOBLACK || \
52− (x) == AV_PIX_FMT_MONOWHITE))
53−
54−static av_always_inline int isALPHA(enum AVPixelFormat pix_fmt)
55−{
56− const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
57− return desc->flags & AV_PIX_FMT_FLAG_ALPHA;
58−}
40+enum {
41+ WIDTH = 96,
42+ HEIGHT = 96,
43+};
5944
60−static double prob = 1;
61−FFSFC64 prng_state;
45+struct options {
46+ enum AVPixelFormat src_fmt;
47+ enum AVPixelFormat dst_fmt;
48+ double prob;
49+};
6250
63−static uint64_t getSSD(const uint8_t *src1, const uint8_t *src2,
64− int stride1, int stride2, int w, int h)
65−{
66− int x, y;
67− uint64_t ssd = 0;
51+struct mode {
52+ SwsFlags flags;
53+ SwsDither dither;
54+};
6855
69− for (y = 0; y < h; y++) {
70− for (x = 0; x < w; x++) {
71− int d = src1[x + y * stride1] - src2[x + y * stride2];
72− ssd += d * d;
73− }
74− }
75− return ssd;
76−}
56+const int dst_w[] = { WIDTH, WIDTH - WIDTH / 3, WIDTH + WIDTH / 3 };
57+const int dst_h[] = { HEIGHT, HEIGHT - HEIGHT / 3, HEIGHT + HEIGHT / 3 };
58+
59+const struct mode modes[] = {
60+ { SWS_FAST_BILINEAR },
61+ { SWS_BILINEAR },
62+ { SWS_BICUBIC },
63+ { SWS_X | SWS_BITEXACT },
64+ { SWS_POINT },
65+ { SWS_AREA | SWS_ACCURATE_RND },
66+ { SWS_BICUBIC | SWS_FULL_CHR_H_INT | SWS_FULL_CHR_H_INP },
67+ {0}, // test defaults
68+};
7769
78−static uint64_t getSSD0(int ref, const uint8_t *src1, int stride1,
79− int w, int h)
80−{
81− int x, y;
82− uint64_t ssd = 0;
70+static FFSFC64 prng_state;
71+static SwsContext *sws[3]; /* reused between tests for efficiency */
8372
84− for (y = 0; y < h; y++) {
85− for (x = 0; x < w; x++) {
86− int d = src1[x + y * stride1] - ref;
87− ssd += d * d;
88− }
89− }
90− return ssd;
73+static int fmt_comps(enum AVPixelFormat fmt)
74+{
75+ const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(fmt);
76+ int comps = desc->nb_components >= 3 ? 0b111 : 0b1;
77+ if (desc->flags & AV_PIX_FMT_FLAG_ALPHA)
78+ comps |= 0b1000;
79+ return comps;
9180 }
9281
93−struct Results {
94− uint64_t ssdY;
95− uint64_t ssdU;
96− uint64_t ssdV;
97− uint64_t ssdA;
98− uint32_t crc;
99−};
100−
101−// test by ref -> src -> dst -> out & compare out against ref
102−// ref & out are YV12
103−static int doTest(const uint8_t * const ref[4], int refStride[4], int w, int h,
104− enum AVPixelFormat srcFormat, enum AVPixelFormat dstFormat,
105− int srcW, int srcH, int dstW, int dstH, int flags,
106− struct Results *r)
82+static void get_mse(int mse[4], const AVFrame *a, const AVFrame *b, int comps)
10783 {
108− const AVPixFmtDescriptor *desc_yuva420p = av_pix_fmt_desc_get(AV_PIX_FMT_YUVA420P);
109− const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(srcFormat);
110− const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(dstFormat);
111− static enum AVPixelFormat cur_srcFormat;
112− static int cur_srcW, cur_srcH;
113− static const uint8_t *src[4];
114− static int srcStride[4];
115− uint8_t *dst[4] = { 0 };
116− uint8_t *out[4] = { 0 };
117− int dstStride[4] = {0};
118− int i;
119− uint64_t ssdY, ssdU = 0, ssdV = 0, ssdA = 0;
120− SwsContext *dstContext = NULL, *outContext = NULL;
121− uint32_t crc = 0;
122− int res = 0;
123−
124− if (ff_sfc64_get(&prng_state) > UINT64_MAX * prob)
125− return 0;
126−
127− if (cur_srcFormat != srcFormat || cur_srcW != srcW || cur_srcH != srcH) {
128− SwsContext *srcContext = NULL;
129− int p;
130−
131− for (p = 0; p < 4; p++)
132− av_freep(&src[p]);
133−
134− res = av_image_fill_linesizes(srcStride, srcFormat, srcW);
135− if (res < 0) {
136− fprintf(stderr, "av_image_fill_linesizes failed\n");
137− goto end;
138− }
139− for (p = 0; p < 4; p++) {
140− srcStride[p] = FFALIGN(srcStride[p], 16);
141− if (srcStride[p])
142− src[p] = av_mallocz(srcStride[p] * srcH + 16);
143− if (srcStride[p] && !src[p]) {
144− perror("Malloc");
145− res = -1;
146− goto end;
84+ av_assert1(a->format == AV_PIX_FMT_YUVA420P);
85+ av_assert1(b->format == a->format);
86+ av_assert1(b->width == a->width && b->height == a->height);
87+
88+ for (int p = 0; p < 4; p++) {
89+ const int is_chroma = p == 1 || p == 2;
90+ const int stride_a = a->linesize[p];
91+ const int stride_b = b->linesize[p];
92+ const int w = (a->width + is_chroma) >> is_chroma;
93+ const int h = (a->height + is_chroma) >> is_chroma;
94+ uint64_t sum = 0;
95+
96+ if (comps & (1 << p)) {
97+ for (int y = 0; y < h; y++) {
98+ for (int x = 0; x < w; x++) {
99+ int d = a->data[p][y * stride_a + x] - b->data[p][y * stride_b + x];
100+ sum += d * d;
101+ }
102+ }
103+ } else {
104+ const int ref = is_chroma ? 128 : 0xFF;
105+ for (int y = 0; y < h; y++) {
106+ for (int x = 0; x < w; x++) {
107+ int d = a->data[p][y * stride_a + x] - ref;
108+ sum += d * d;
109+ }
147110 }
148111 }
149− srcContext = sws_getContext(w, h, AV_PIX_FMT_YUVA420P, srcW, srcH,
150− srcFormat, SWS_BILINEAR, NULL, NULL, NULL);
151− if (!srcContext) {
152− fprintf(stderr, "Failed to get %s ---> %s\n",
153− desc_yuva420p->name,
154− desc_src->name);
155− res = -1;
156− goto end;
157− }
158− sws_scale(srcContext, ref, refStride, 0, h,
159− (uint8_t * const *) src, srcStride);
160− sws_freeContext(srcContext);
161112
162− cur_srcFormat = srcFormat;
163− cur_srcW = srcW;
164− cur_srcH = srcH;
113+ mse[p] = sum / (w * h);
165114 }
115+}
166116
167− res = av_image_fill_linesizes(dstStride, dstFormat, dstW);
168− if (res < 0) {
169− fprintf(stderr, "av_image_fill_linesizes failed\n");
170− goto end;
171− }
117+static int scale_legacy(AVFrame *dst, const AVFrame *src, struct mode mode)
118+{
119+ SwsContext *sws_legacy;
120+ int ret;
172121
173− for (i = 0; i < 4; i++) {
174− /* Image buffers passed into libswscale can be allocated any way you
175− * prefer, as long as they're aligned enough for the architecture, and
176− * they're freed appropriately (such as using av_free for buffers
177− * allocated with av_malloc). */
178− /* An extra 16 bytes is being allocated because some scalers may write
179− * out of bounds. */
180− dstStride[i] = FFALIGN(dstStride[i], 16);
181− if (dstStride[i])
182− dst[i] = av_mallocz(dstStride[i] * dstH + 16);
183− if (dstStride[i] && !dst[i]) {
184− perror("Malloc");
185− res = -1;
186−
187− goto end;
188− }
189− }
122+ sws_legacy = sws_alloc_context();
123+ if (!sws_legacy)
124+ return -1;
125+
126+ sws_legacy->src_w = src->width;
127+ sws_legacy->src_h = src->height;
128+ sws_legacy->src_format = src->format;
129+ sws_legacy->dst_w = dst->width;
130+ sws_legacy->dst_h = dst->height;
131+ sws_legacy->dst_format = dst->format;
132+ sws_legacy->flags = mode.flags;
133+ sws_legacy->dither = mode.dither;
134+
135+ ret = sws_init_context(sws_legacy, NULL, NULL);
136+ if (!ret)
137+ ret = sws_scale_frame(sws_legacy, dst, src);
138+
139+ sws_freeContext(sws_legacy);
140+ return ret;
141+}
142+
143+/* Runs a series of ref -> src -> dst -> out, and compares out vs ref */
144+static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt,
145+ int dst_w, int dst_h, struct mode mode, const AVFrame *ref,
146+ const int mse_ref[4])
147+{
148+ AVFrame *src = NULL, *dst = NULL, *out = NULL;
149+ int mse[4], mse_sws[4], ret = -1;
150+ const int comps = fmt_comps(src_fmt) & fmt_comps(dst_fmt);
151+
152+ src = av_frame_alloc();
153+ dst = av_frame_alloc();
154+ out = av_frame_alloc();
155+ if (!src || !dst || !out)
156+ goto error;
190157
191− dstContext = sws_alloc_context();
192− if (!dstContext) {
193− fprintf(stderr, "Failed to alloc %s ---> %s\n",
194− desc_src->name, desc_dst->name);
195− res = -1;
196− goto end;
158+ av_frame_copy_props(src, ref);
159+ av_frame_copy_props(dst, ref);
160+ av_frame_copy_props(out, ref);
161+ src->width = out->width = ref->width;
162+ src->height = out->height = ref->height;
163+ out->format = ref->format;
164+ src->format = src_fmt;
165+ dst->format = dst_fmt;
166+ dst->width = dst_w;
167+ dst->height = dst_h;
168+
169+ if (sws_scale_frame(sws[0], src, ref) < 0) {
170+ fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(ref->format),
171+ av_get_pix_fmt_name(src->format));
172+ goto error;
197173 }
198174
199− av_opt_set_int(dstContext, "sws_flags", flags, 0);
200− av_opt_set_int(dstContext, "srcw", srcW, 0);
201− av_opt_set_int(dstContext, "srch", srcH, 0);
202− av_opt_set_int(dstContext, "dstw", dstW, 0);
203− av_opt_set_int(dstContext, "dsth", dstH, 0);
204− av_opt_set_int(dstContext, "src_format", srcFormat, 0);
205− av_opt_set_int(dstContext, "dst_format", dstFormat, 0);
206− av_opt_set(dstContext, "alphablend", "none", 0);
207−
208− if (sws_init_context(dstContext, NULL, NULL) < 0) {
209− sws_freeContext(dstContext);
210− fprintf(stderr, "Failed to init %s ---> %s\n",
211− desc_src->name, desc_dst->name);
212− res = -1;
213− goto end;
175+ sws[1]->flags = mode.flags;
176+ sws[1]->dither = mode.dither;
177+ if (sws_scale_frame(sws[1], dst, src) < 0) {
178+ fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(src->format),
179+ av_get_pix_fmt_name(dst->format));
180+ goto error;
214181 }
215182
216− printf(" %s %dx%d -> %s %3dx%3d flags=%2d",
217− desc_src->name, srcW, srcH,
218− desc_dst->name, dstW, dstH,
219− flags);
220− fflush(stdout);
183+ if (sws_scale_frame(sws[2], out, dst) < 0) {
184+ fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(dst->format),
185+ av_get_pix_fmt_name(out->format));
186+ goto error;
187+ }
221188
222− sws_scale(dstContext, (const uint8_t * const*)src, srcStride, 0, srcH, dst, dstStride);
223−
224− for (i = 0; i < 4 && dstStride[i]; i++)
225− crc = av_crc(av_crc_get_table(AV_CRC_32_IEEE), crc, dst[i],
226− dstStride[i] * dstH);
227−
228− if (r && crc == r->crc) {
229− ssdY = r->ssdY;
230− ssdU = r->ssdU;
231− ssdV = r->ssdV;
232− ssdA = r->ssdA;
233− } else {
234− for (i = 0; i < 4; i++) {
235− refStride[i] = FFALIGN(refStride[i], 16);
236− if (refStride[i])
237− out[i] = av_mallocz(refStride[i] * h);
238− if (refStride[i] && !out[i]) {
239− perror("Malloc");
240− res = -1;
241− goto end;
242− }
243− }
244− outContext = sws_getContext(dstW, dstH, dstFormat, w, h,
245− AV_PIX_FMT_YUVA420P, SWS_BILINEAR,
246− NULL, NULL, NULL);
247− if (!outContext) {
248− fprintf(stderr, "Failed to get %s ---> %s\n",
249− desc_dst->name,
250− desc_yuva420p->name);
251− res = -1;
252− goto end;
253− }
254− sws_scale(outContext, (const uint8_t * const *) dst, dstStride, 0, dstH,
255− out, refStride);
256−
257− ssdY = getSSD(ref[0], out[0], refStride[0], refStride[0], w, h);
258− if (hasChroma(srcFormat) && hasChroma(dstFormat)) {
259− //FIXME check that output is really gray
260− ssdU = getSSD(ref[1], out[1], refStride[1], refStride[1],
261− (w + 1) >> 1, (h + 1) >> 1);
262− ssdV = getSSD(ref[2], out[2], refStride[2], refStride[2],
263− (w + 1) >> 1, (h + 1) >> 1);
264− } else {
265− ssdU = getSSD0(128, out[1], refStride[1],
266− (w + 1) >> 1, (h + 1) >> 1);
267− ssdV = getSSD0(128, out[2], refStride[2],
268− (w + 1) >> 1, (h + 1) >> 1);
269− }
270− if (isALPHA(srcFormat) && isALPHA(dstFormat)) {
271− ssdA = getSSD(ref[3], out[3], refStride[3], refStride[3], w, h);
272− } else {
273− ssdA = getSSD0(0xFF, out[3], refStride[3], w, h);
189+ get_mse(mse, out, ref, comps);
190+ printf("%s %dx%d -> %s %3dx%3d, flags=%u dither=%u, "
191+ "MSE={%5d %5d %5d %5d}\n",
192+ av_get_pix_fmt_name(src->format), src->width, src->height,
193+ av_get_pix_fmt_name(dst->format), dst->width, dst->height,
194+ mode.flags, mode.dither,
195+ mse[0], mse[1], mse[2], mse[3]);
196+
197+ if (!mse_ref) {
198+ /* Compare against the legacy swscale API as a reference */
199+ if (scale_legacy(dst, src, mode) < 0) {
200+ fprintf(stderr, "Failed ref %s ---> %s\n", av_get_pix_fmt_name(src->format),
201+ av_get_pix_fmt_name(dst->format));
202+ goto error;
274203 }
275204
276− ssdY /= w * h;
277− ssdU /= w * h / 4;
278− ssdV /= w * h / 4;
279− ssdA /= w * h;
280−
281− sws_freeContext(outContext);
205+ if (sws_scale_frame(sws[2], out, dst) < 0)
206+ goto error;
282207
283− for (i = 0; i < 4; i++)
284− if (refStride[i])
285− av_free(out[i]);
208+ get_mse(mse_sws, out, ref, comps);
209+ mse_ref = mse_sws;
286210 }
287211
288− if(r){
289− if(ssdY>r->ssdY*1.02+1 || ssdU>r->ssdU*1.02+1 || ssdV>r->ssdV*1.02+1|| ssdA>r->ssdA*1.02+1)
290− printf("WORSE SSD=%5"PRId64",%5"PRId64",%5"PRId64",%5"PRId64"",
291− r->ssdY, r->ssdU, r->ssdV, r->ssdA);
292− else if(ssdY>r->ssdY || ssdU>r->ssdU || ssdV>r->ssdV|| ssdA>r->ssdA)
293− printf("worse SSD=%5"PRId64",%5"PRId64",%5"PRId64",%5"PRId64"",
294− r->ssdY, r->ssdU, r->ssdV, r->ssdA);
212+ for (int i = 0; i < 4; i++) {
213+ if (mse[i] > mse_ref[i]) {
214+ int bad = mse[i] > mse_ref[i] * 1.02 + 1;
215+ printf("\033[1;31m %s, ref MSE={%5d %5d %5d %5d}\033[0m\n",
216+ bad ? "WORSE" : "worse",
217+ mse_ref[0], mse_ref[1], mse_ref[2], mse_ref[3]);
218+ if (bad)
219+ goto error;
220+ break;
221+ }
295222 }
296223
297− printf(" CRC=%08x SSD=%5"PRId64 ",%5"PRId64 ",%5"PRId64 ",%5"PRId64 "\n",
298− crc, ssdY, ssdU, ssdV, ssdA);
299−
300−end:
301− sws_freeContext(dstContext);
302−
303− for (i = 0; i < 4; i++)
304− if (dstStride[i])
305− av_free(dst[i]);
306−
307− return !!res;
224+ fflush(stdout);
225+ ret = 0; /* fall through */
226+ error:
227+ av_frame_free(&src);
228+ av_frame_free(&dst);
229+ av_frame_free(&out);
230+ return ret;
308231 }
309232
310−static void selfTest(const uint8_t * const ref[4], int refStride[4],
311− int w, int h,
312− enum AVPixelFormat srcFormat_in,
313− enum AVPixelFormat dstFormat_in)
233+static int run_self_tests(const AVFrame *ref, struct options opts)
314234 {
315− const int flags[] = { SWS_FAST_BILINEAR,
316− SWS_BILINEAR, SWS_BICUBIC,
317− SWS_X|SWS_BITEXACT , SWS_POINT , SWS_AREA|SWS_ACCURATE_RND,
318− SWS_BICUBIC|SWS_FULL_CHR_H_INT|SWS_FULL_CHR_H_INP, 0};
319− const int srcW = w;
320− const int srcH = h;
321− const int dstW[] = { srcW - srcW / 3, srcW, srcW + srcW / 3, 0 };
322− const int dstH[] = { srcH - srcH / 3, srcH, srcH + srcH / 3, 0 };
323− enum AVPixelFormat srcFormat, dstFormat;
324− const AVPixFmtDescriptor *desc_src, *desc_dst;
325−
326− for (srcFormat = srcFormat_in != AV_PIX_FMT_NONE ? srcFormat_in : 0;
327− srcFormat < AV_PIX_FMT_NB; srcFormat++) {
328− if (!sws_isSupportedInput(srcFormat) ||
329− !sws_isSupportedOutput(srcFormat))
235+ enum AVPixelFormat src_fmt, dst_fmt,
236+ src_fmt_min = 0,
237+ dst_fmt_min = 0,
238+ src_fmt_max = AV_PIX_FMT_NB - 1,
239+ dst_fmt_max = AV_PIX_FMT_NB - 1;
240+
241+ if (opts.src_fmt != AV_PIX_FMT_NONE)
242+ src_fmt_min = src_fmt_max = opts.src_fmt;
243+ if (opts.dst_fmt != AV_PIX_FMT_NONE)
244+ dst_fmt_min = dst_fmt_max = opts.dst_fmt;
245+
246+ for (src_fmt = src_fmt_min; src_fmt <= src_fmt_max; src_fmt++) {
247+ if (!sws_test_format(src_fmt, 0) || !sws_test_format(src_fmt, 1))
330248 continue;
331−
332− desc_src = av_pix_fmt_desc_get(srcFormat);
333−
334− for (dstFormat = dstFormat_in != AV_PIX_FMT_NONE ? dstFormat_in : 0;
335− dstFormat < AV_PIX_FMT_NB; dstFormat++) {
336− int i, j, k;
337− int res = 0;
338−
339− if (!sws_isSupportedInput(dstFormat) ||
340− !sws_isSupportedOutput(dstFormat))
249+ for (dst_fmt = dst_fmt_min; dst_fmt <= dst_fmt_max; dst_fmt++) {
250+ if (!sws_test_format(dst_fmt, 0) || !sws_test_format(dst_fmt, 1))
341251 continue;
342−
343− desc_dst = av_pix_fmt_desc_get(dstFormat);
344−
345− printf("%s -> %s\n", desc_src->name, desc_dst->name);
346− fflush(stdout);
347−
348− for (k = 0; flags[k] && !res; k++)
349− for (i = 0; dstW[i] && !res; i++)
350− for (j = 0; dstH[j] && !res; j++)
351− res = doTest(ref, refStride, w, h,
352− srcFormat, dstFormat,
353− srcW, srcH, dstW[i], dstH[j], flags[k],
354− NULL);
355− if (dstFormat_in != AV_PIX_FMT_NONE)
356− break;
252+ for (int h = 0; h < FF_ARRAY_ELEMS(dst_h); h++)
253+ for (int w = 0; w < FF_ARRAY_ELEMS(dst_w); w++)
254+ for (int m = 0; m < FF_ARRAY_ELEMS(modes); m++) {
255+ if (ff_sfc64_get(&prng_state) > UINT64_MAX * opts.prob)
256+ continue;
257+ if (run_test(src_fmt, dst_fmt, dst_w[w], dst_h[h],
258+ modes[m], ref, NULL) < 0)
259+ return -1;
260+ }
357261 }
358− if (srcFormat_in != AV_PIX_FMT_NONE)
359− break;
360262 }
263+
264+ return 0;
361265 }
362266
363−static int fileTest(const uint8_t * const ref[4], int refStride[4],
364− int w, int h, FILE *fp,
365− enum AVPixelFormat srcFormat_in,
366− enum AVPixelFormat dstFormat_in)
267+static int run_file_tests(const AVFrame *ref, FILE *fp, struct options opts)
367268 {
368269 char buf[256];
270+ int ret;
369271
370272 while (fgets(buf, sizeof(buf), fp)) {
371− struct Results r;
372− enum AVPixelFormat srcFormat;
373− char srcStr[21];
374− int srcW = 0, srcH = 0;
375− enum AVPixelFormat dstFormat;
376− char dstStr[21];
377− int dstW = 0, dstH = 0;
378− int flags;
379− int ret;
273+ char src_fmt_str[20], dst_fmt_str[20];
274+ enum AVPixelFormat src_fmt;
275+ enum AVPixelFormat dst_fmt;
276+ int sw, sh, dw, dh, mse[4];
277+ struct mode mode;
380278
381279 ret = sscanf(buf,
382− " %20s %dx%d -> %20s %dx%d flags=%d CRC=%x"
383− " SSD=%"SCNu64 ", %"SCNu64 ", %"SCNu64 ", %"SCNu64 "\n",
384− srcStr, &srcW, &srcH, dstStr, &dstW, &dstH,
385− &flags, &r.crc, &r.ssdY, &r.ssdU, &r.ssdV, &r.ssdA);
386− if (ret != 12) {
387− srcStr[0] = dstStr[0] = 0;
388− ret = sscanf(buf, "%20s -> %20s\n", srcStr, dstStr);
280+ " %20s %dx%d -> %20s %dx%d, flags=%u dither=%u, "
281+ "MSE={%d %d %d %d}\n",
282+ src_fmt_str, &sw, &sh, dst_fmt_str, &dw, &dh,
283+ &mode.flags, &mode.dither,
284+ &mse[0], &mse[1], &mse[2], &mse[3]);
285+ if (ret != 13) {
286+ printf("%s", buf);
287+ continue;
389288 }
390289
391− srcFormat = av_get_pix_fmt(srcStr);
392− dstFormat = av_get_pix_fmt(dstStr);
393−
394− if (srcFormat == AV_PIX_FMT_NONE || dstFormat == AV_PIX_FMT_NONE ||
395− srcW > 8192U || srcH > 8192U || dstW > 8192U || dstH > 8192U) {
290+ src_fmt = av_get_pix_fmt(src_fmt_str);
291+ dst_fmt = av_get_pix_fmt(dst_fmt_str);
292+ if (src_fmt == AV_PIX_FMT_NONE || dst_fmt == AV_PIX_FMT_NONE ||
293+ sw != ref->width || sh != ref->height || dw > 8192 || dh > 8192 ||
294+ mode.dither >= SWS_DITHER_NB) {
396295 fprintf(stderr, "malformed input file\n");
397296 return -1;
398297 }
399− if ((srcFormat_in != AV_PIX_FMT_NONE && srcFormat_in != srcFormat) ||
400− (dstFormat_in != AV_PIX_FMT_NONE && dstFormat_in != dstFormat))
401− continue;
402− if (ret != 12) {
403− printf("%s", buf);
298+
299+ if (opts.src_fmt != AV_PIX_FMT_NONE && src_fmt != opts.src_fmt ||
300+ opts.dst_fmt != AV_PIX_FMT_NONE && dst_fmt != opts.dst_fmt)
404301 continue;
405− }
406302
407− doTest(ref, refStride, w, h,
408− srcFormat, dstFormat,
409− srcW, srcH, dstW, dstH, flags,
410− &r);
303+ if (run_test(src_fmt, dst_fmt, dw, dh, mode, ref, mse) < 0)
304+ return -1;
411305 }
412306
413307 return 0;
414308 }
415309
416−#define W 96
417−#define H 96
418−
419310 int main(int argc, char **argv)
420311 {
421− enum AVPixelFormat srcFormat = AV_PIX_FMT_NONE;
422− enum AVPixelFormat dstFormat = AV_PIX_FMT_NONE;
423− uint8_t *rgb_data = av_malloc(W * H * 4);
424− const uint8_t * const rgb_src[4] = { rgb_data, NULL, NULL, NULL };
425− int rgb_stride[4] = { 4 * W, 0, 0, 0 };
426− uint8_t *data = av_malloc(4 * W * H);
427− const uint8_t * const src[4] = { data, data + W * H, data + W * H * 2, data + W * H * 3 };
428− int stride[4] = { W, W, W, W };
429− int x, y;
430− SwsContext *sws;
431− AVLFG rand;
432− int res = -1;
433− int i;
434− FILE *fp = NULL;
312+ struct options opts = {
313+ .src_fmt = AV_PIX_FMT_NONE,
314+ .dst_fmt = AV_PIX_FMT_NONE,
315+ .prob = 1.0,
316+ };
435317
436− if (!rgb_data || !data)
437− return -1;
318+ AVFrame *rgb = NULL, *ref = NULL;
319+ FILE *fp = NULL;
320+ AVLFG rand;
321+ int ret = -1;
438322
439− for (i = 1; i < argc; i += 2) {
323+ for (int i = 1; i < argc; i += 2) {
440324 if (!strcmp(argv[i], "-help") || !strcmp(argv[i], "--help")) {
441325 fprintf(stderr,
442326 "swscale [options...]\n"
443327 " -help\n"
444328 " This text\n"
445329 " -ref <file>\n"
446− " Uses file as reference to compae tests againsts. Tests that have become worse will contain the string worse or WORSE\n"
330+ " Uses file as reference to compare tests againsts. Tests that have become worse will contain the string worse or WORSE\n"
447331 " -p <number between 0.0 and 1.0>\n"
448332 " The percentage of tests or comparisons to perform. Doing all tests will take long and generate over a hundred MB text output\n"
449333 " It is often convenient to perform a random subset\n"
@@ -454,7 +338,7 @@ int main(int argc, char **argv) · 4 unmodified lines
454338 " -cpuflags <cpuflags>\n"
455339 " Uses the specified cpuflags in the tests\n"
456340 );
457− goto error;
341+ return 0;
458342 }
459343 if (argv[i][0] != '-' || i + 1 == argc)
460344 goto bad_option;
@@ -466,26 +350,26 @@ int main(int argc, char **argv) · 5 unmodified lines
466350 }
467351 } else if (!strcmp(argv[i], "-cpuflags")) {
468352 unsigned flags = av_get_cpu_flags();
469− int ret = av_parse_cpu_caps(&flags, argv[i + 1]);
470− if (ret < 0) {
353+ int res = av_parse_cpu_caps(&flags, argv[i + 1]);
354+ if (res < 0) {
471355 fprintf(stderr, "invalid cpu flags %s\n", argv[i + 1]);
472− return ret;
356+ goto error;
473357 }
474358 av_force_cpu_flags(flags);
475359 } else if (!strcmp(argv[i], "-src")) {
476− srcFormat = av_get_pix_fmt(argv[i + 1]);
477− if (srcFormat == AV_PIX_FMT_NONE) {
360+ opts.src_fmt = av_get_pix_fmt(argv[i + 1]);
361+ if (opts.src_fmt == AV_PIX_FMT_NONE) {
478362 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
479− return -1;
363+ goto error;
480364 }
481365 } else if (!strcmp(argv[i], "-dst")) {
482− dstFormat = av_get_pix_fmt(argv[i + 1]);
483− if (dstFormat == AV_PIX_FMT_NONE) {
366+ opts.dst_fmt = av_get_pix_fmt(argv[i + 1]);
367+ if (opts.dst_fmt == AV_PIX_FMT_NONE) {
484368 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
485− return -1;
369+ goto error;
486370 }
487371 } else if (!strcmp(argv[i], "-p")) {
488− prob = atof(argv[i + 1]);
372+ opts.prob = atof(argv[i + 1]);
489373 } else {
490374 bad_option:
491375 fprintf(stderr, "bad option or argument missing (%s) see -help\n", argv[i]);
@@ -494,32 +378,51 @@ bad_option: · 2 unmodified lines
494378 }
495379
496380 ff_sfc64_init(&prng_state, 0, 0, 0, 12);
497−
498− sws = sws_getContext(W / 12, H / 12, AV_PIX_FMT_RGB32, W, H,
499− AV_PIX_FMT_YUVA420P, SWS_BILINEAR, NULL, NULL, NULL);
500−
501381 av_lfg_init(&rand, 1);
502382
503− for (y = 0; y < H; y++)
504− for (x = 0; x < W * 4; x++)
505− rgb_data[ x + y * 4 * W] = av_lfg_get(&rand);
506− res = sws_scale(sws, rgb_src, rgb_stride, 0, H / 12, (uint8_t * const *) src, stride);
507− if (res < 0 || res != H) {
508− res = -1;
509− goto error;
383+ for (int i = 0; i < 3; i++) {
384+ sws[i] = sws_alloc_context();
385+ if (!sws[i])
386+ goto error;
387+ sws[i]->flags = SWS_BILINEAR;
510388 }
511− sws_freeContext(sws);
512− av_free(rgb_data);
513389
514− if(fp) {
515− res = fileTest(src, stride, W, H, fp, srcFormat, dstFormat);
516− fclose(fp);
517− } else {
518− selfTest(src, stride, W, H, srcFormat, dstFormat);
519− res = 0;
390+ rgb = av_frame_alloc();
391+ if (!rgb)
392+ goto error;
393+ rgb->width = WIDTH / 12;
394+ rgb->height = HEIGHT / 12;
395+ rgb->format = AV_PIX_FMT_RGBA;
396+ if (av_frame_get_buffer(rgb, 32) < 0)
397+ goto error;
398+
399+ for (int y = 0; y < rgb->height; y++) {
400+ for (int x = 0; x < rgb->width; x++) {
401+ for (int c = 0; c < 4; c++)
402+ rgb->data[0][y * rgb->linesize[0] + x * 4 + c] = av_lfg_get(&rand);
403+ }
520404 }
521−error:
522− av_free(data);
523405
524− return res;
406+ ref = av_frame_alloc();
407+ if (!ref)
408+ goto error;
409+ ref->width = WIDTH;
410+ ref->height = HEIGHT;
411+ ref->format = AV_PIX_FMT_YUVA420P;
412+
413+ if (sws_scale_frame(sws[0], ref, rgb) < 0)
414+ goto error;
415+
416+ ret = fp ? run_file_tests(ref, fp, opts)
417+ : run_self_tests(ref, opts);
418+
419+ /* fall through */
420+error:
421+ for (int i = 0; i < 3; i++)
422+ sws_free_context(&sws[i]);
423+ av_frame_free(&rgb);
424+ av_frame_free(&ref);
425+ if (fp)
426+ fclose(fp);
427+ return ret;
525428 }

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t