History

tests/swscale: add a benchmarking mode

With the ability to set the thread count as well. This benchmark includes the constant overhead of context initialization.

Niklas Haas committed 2 years ago

Commit 3edd1e41 parent 59c39a71 file · +68 −25Browse files

Unified · Split

libswscale/tests/swscale.c+68−25
@@ -31,21 +31,22 @@ · 30 unmodified lines
31#include "libavutil/lfg.h"31#include "libavutil/lfg.h"
32#include "libavutil/sfc64.h"32#include "libavutil/sfc64.h"
33#include "libavutil/frame.h"33#include "libavutil/frame.h"
34#include "libavutil/opt.h"
35#include "libavutil/time.h"
34#include "libavutil/pixfmt.h"36#include "libavutil/pixfmt.h"
35#include "libavutil/avassert.h"37#include "libavutil/avassert.h"
36#include "libavutil/macros.h"38#include "libavutil/macros.h"
37 39
38#include "libswscale/swscale.h"40#include "libswscale/swscale.h"
39 41
40enum {
41 WIDTH = 96,
42 HEIGHT = 96,
43};
44
45struct options {42struct options {
46 enum AVPixelFormat src_fmt;43 enum AVPixelFormat src_fmt;
47 enum AVPixelFormat dst_fmt;44 enum AVPixelFormat dst_fmt;
48 double prob;45 double prob;
46 int w, h;
47 int threads;
48 int iters;
49 int bench;
49};50};
50 51
51struct mode {52struct mode {
@@ -53,9 +54,6 @@ struct mode { · 1 unmodified line
53 SwsDither dither;54 SwsDither dither;
54};55};
55 56
56const int dst_w[] = { WIDTH, WIDTH - WIDTH / 3, WIDTH + WIDTH / 3 };
57const int dst_h[] = { HEIGHT, HEIGHT - HEIGHT / 3, HEIGHT + HEIGHT / 3 };
58
59const struct mode modes[] = {57const struct mode modes[] = {
60 { SWS_FAST_BILINEAR },58 { SWS_FAST_BILINEAR },
61 { SWS_BILINEAR },59 { SWS_BILINEAR },
@@ -114,7 +112,8 @@ static void get_mse(int mse[4], const AVFrame *a, const AVFrame *b, int comps) · 52 unmodified lines
114 }112 }
115}113}
116 114
117static int scale_legacy(AVFrame *dst, const AVFrame *src, struct mode mode)115static int scale_legacy(AVFrame *dst, const AVFrame *src, struct mode mode,
116 struct options opts)
118{117{
119 SwsContext *sws_legacy;118 SwsContext *sws_legacy;
120 int ret;119 int ret;
@@ -131,23 +130,28 @@ static int scale_legacy(AVFrame *dst, const AVFrame *src, struct mode mode) · 10 unmodified lines
131 sws_legacy->dst_format = dst->format;130 sws_legacy->dst_format = dst->format;
132 sws_legacy->flags = mode.flags;131 sws_legacy->flags = mode.flags;
133 sws_legacy->dither = mode.dither;132 sws_legacy->dither = mode.dither;
133 sws_legacy->threads = opts.threads;
134
135 if ((ret = sws_init_context(sws_legacy, NULL, NULL)) < 0)
136 goto error;
134 137
135 ret = sws_init_context(sws_legacy, NULL, NULL);138 for (int i = 0; !ret && i < opts.iters; i++)
136 if (!ret)
137 ret = sws_scale_frame(sws_legacy, dst, src);139 ret = sws_scale_frame(sws_legacy, dst, src);
138 140
141error:
139 sws_freeContext(sws_legacy);142 sws_freeContext(sws_legacy);
140 return ret;143 return ret;
141}144}
142 145
143/* Runs a series of ref -> src -> dst -> out, and compares out vs ref */146/* Runs a series of ref -> src -> dst -> out, and compares out vs ref */
144static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt,147static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt,
145 int dst_w, int dst_h, struct mode mode, const AVFrame *ref,148 int dst_w, int dst_h, struct mode mode, struct options opts,
146 const int mse_ref[4])149 const AVFrame *ref, const int mse_ref[4])
147{150{
148 AVFrame *src = NULL, *dst = NULL, *out = NULL;151 AVFrame *src = NULL, *dst = NULL, *out = NULL;
149 int mse[4], mse_sws[4], ret = -1;152 int mse[4], mse_sws[4], ret = -1;
150 const int comps = fmt_comps(src_fmt) & fmt_comps(dst_fmt);153 const int comps = fmt_comps(src_fmt) & fmt_comps(dst_fmt);
154 int64_t time, time_ref = 0;
151 155
152 src = av_frame_alloc();156 src = av_frame_alloc();
153 dst = av_frame_alloc();157 dst = av_frame_alloc();
@@ -174,12 +178,20 @@ static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt, · 20 unmodified lines
174 178
175 sws[1]->flags = mode.flags;179 sws[1]->flags = mode.flags;
176 sws[1]->dither = mode.dither;180 sws[1]->dither = mode.dither;
177 if (sws_scale_frame(sws[1], dst, src) < 0) {181 sws[1]->threads = opts.threads;
178 fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(src->format),182
179 av_get_pix_fmt_name(dst->format));183 time = av_gettime_relative();
180 goto error;184
185 for (int i = 0; i < opts.iters; i++) {
186 if (sws_scale_frame(sws[1], dst, src) < 0) {
187 fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(src->format),
188 av_get_pix_fmt_name(dst->format));
189 goto error;
190 }
181 }191 }
182 192
193 time = av_gettime_relative() - time;
194
183 if (sws_scale_frame(sws[2], out, dst) < 0) {195 if (sws_scale_frame(sws[2], out, dst) < 0) {
184 fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(dst->format),196 fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(dst->format),
185 av_get_pix_fmt_name(out->format));197 av_get_pix_fmt_name(out->format));
@@ -196,11 +208,13 @@ static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt, · 10 unmodified lines
196 208
197 if (!mse_ref) {209 if (!mse_ref) {
198 /* Compare against the legacy swscale API as a reference */210 /* Compare against the legacy swscale API as a reference */
199 if (scale_legacy(dst, src, mode) < 0) {211 time_ref = av_gettime_relative();
212 if (scale_legacy(dst, src, mode, opts) < 0) {
200 fprintf(stderr, "Failed ref %s ---> %s\n", av_get_pix_fmt_name(src->format),213 fprintf(stderr, "Failed ref %s ---> %s\n", av_get_pix_fmt_name(src->format),
201 av_get_pix_fmt_name(dst->format));214 av_get_pix_fmt_name(dst->format));
202 goto error;215 goto error;
203 }216 }
217 time_ref = av_gettime_relative() - time_ref;
204 218
205 if (sws_scale_frame(sws[2], out, dst) < 0)219 if (sws_scale_frame(sws[2], out, dst) < 0)
206 goto error;220 goto error;
@@ -221,6 +235,15 @@ static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt, · 14 unmodified lines
221 }235 }
222 }236 }
223 237
238 if (opts.bench && time_ref) {
239 printf(" time=%"PRId64" us, ref=%"PRId64" us, speedup=%.3fx %s\n",
240 time / opts.iters, time_ref / opts.iters,
241 (double) time_ref / time,
242 time <= time_ref ? "faster" : "\033[1;33mslower\033[0m");
243 } else if (opts.bench) {
244 printf(" time=%"PRId64" us\n", time / opts.iters);
245 }
246
224 fflush(stdout);247 fflush(stdout);
225 ret = 0; /* fall through */248 ret = 0; /* fall through */
226 error:249 error:
@@ -232,6 +255,9 @@ static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt, · 5 unmodified lines
232 255
233static int run_self_tests(const AVFrame *ref, struct options opts)256static int run_self_tests(const AVFrame *ref, struct options opts)
234{257{
258 const int dst_w[] = { opts.w, opts.w - opts.w / 3, opts.w + opts.w / 3 };
259 const int dst_h[] = { opts.h, opts.h - opts.h / 3, opts.h + opts.h / 3 };
260
235 enum AVPixelFormat src_fmt, dst_fmt,261 enum AVPixelFormat src_fmt, dst_fmt,
236 src_fmt_min = 0,262 src_fmt_min = 0,
237 dst_fmt_min = 0,263 dst_fmt_min = 0,
@@ -254,8 +280,9 @@ static int run_self_tests(const AVFrame *ref, struct options opts) · 16 unmodified lines
254 for (int m = 0; m < FF_ARRAY_ELEMS(modes); m++) {280 for (int m = 0; m < FF_ARRAY_ELEMS(modes); m++) {
255 if (ff_sfc64_get(&prng_state) > UINT64_MAX * opts.prob)281 if (ff_sfc64_get(&prng_state) > UINT64_MAX * opts.prob)
256 continue;282 continue;
283
257 if (run_test(src_fmt, dst_fmt, dst_w[w], dst_h[h],284 if (run_test(src_fmt, dst_fmt, dst_w[w], dst_h[h],
258 modes[m], ref, NULL) < 0)285 modes[m], opts, ref, NULL) < 0)
259 return -1;286 return -1;
260 }287 }
261 }288 }
@@ -300,7 +327,7 @@ static int run_file_tests(const AVFrame *ref, FILE *fp, struct options opts) · 38 unmodified lines
300 opts.dst_fmt != AV_PIX_FMT_NONE && dst_fmt != opts.dst_fmt)327 opts.dst_fmt != AV_PIX_FMT_NONE && dst_fmt != opts.dst_fmt)
301 continue;328 continue;
302 329
303 if (run_test(src_fmt, dst_fmt, dw, dh, mode, ref, mse) < 0)330 if (run_test(src_fmt, dst_fmt, dw, dh, mode, opts, ref, mse) < 0)
304 return -1;331 return -1;
305 }332 }
306 333
@@ -312,7 +339,11 @@ int main(int argc, char **argv) · 5 unmodified lines
312 struct options opts = {339 struct options opts = {
313 .src_fmt = AV_PIX_FMT_NONE,340 .src_fmt = AV_PIX_FMT_NONE,
314 .dst_fmt = AV_PIX_FMT_NONE,341 .dst_fmt = AV_PIX_FMT_NONE,
315 .prob = 1.0,342 .w = 96,
343 .h = 96,
344 .threads = 1,
345 .iters = 1,
346 .prob = 1.0,
316 };347 };
317 348
318 AVFrame *rgb = NULL, *ref = NULL;349 AVFrame *rgb = NULL, *ref = NULL;
@@ -335,6 +366,10 @@ int main(int argc, char **argv) · 16 unmodified lines
335 " Only test the specified destination pixel format\n"366 " Only test the specified destination pixel format\n"
336 " -src <pixfmt>\n"367 " -src <pixfmt>\n"
337 " Only test the specified source pixel format\n"368 " Only test the specified source pixel format\n"
369 " -bench <iters>\n"
370 " Run benchmarks with the specified number of iterations. This mode also increases the size of the test images\n"
371 " -threads <threads>\n"
372 " Use the specified number of threads\n"
338 " -cpuflags <cpuflags>\n"373 " -cpuflags <cpuflags>\n"
339 " Uses the specified cpuflags in the tests\n"374 " Uses the specified cpuflags in the tests\n"
340 );375 );
@@ -368,6 +403,14 @@ int main(int argc, char **argv) · 27 unmodified lines
368 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);403 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
369 goto error;404 goto error;
370 }405 }
406 } else if (!strcmp(argv[i], "-bench")) {
407 opts.bench = 1;
408 opts.iters = atoi(argv[i + 1]);
409 opts.iters = FFMAX(opts.iters, 1);
410 opts.w = 1920;
411 opts.h = 1080;
412 } else if (!strcmp(argv[i], "-threads")) {
413 opts.threads = atoi(argv[i + 1]);
371 } else if (!strcmp(argv[i], "-p")) {414 } else if (!strcmp(argv[i], "-p")) {
372 opts.prob = atof(argv[i + 1]);415 opts.prob = atof(argv[i + 1]);
373 } else {416 } else {
@@ -390,8 +433,8 @@ bad_option: · 16 unmodified lines
390 rgb = av_frame_alloc();433 rgb = av_frame_alloc();
391 if (!rgb)434 if (!rgb)
392 goto error;435 goto error;
393 rgb->width = WIDTH / 12;436 rgb->width = opts.w / 12;
394 rgb->height = HEIGHT / 12;437 rgb->height = opts.h / 12;
395 rgb->format = AV_PIX_FMT_RGBA;438 rgb->format = AV_PIX_FMT_RGBA;
396 if (av_frame_get_buffer(rgb, 32) < 0)439 if (av_frame_get_buffer(rgb, 32) < 0)
397 goto error;440 goto error;
@@ -406,8 +449,8 @@ bad_option: · 8 unmodified lines
406 ref = av_frame_alloc();449 ref = av_frame_alloc();
407 if (!ref)450 if (!ref)
408 goto error;451 goto error;
409 ref->width = WIDTH;452 ref->width = opts.w;
410 ref->height = HEIGHT;453 ref->height = opts.h;
411 ref->format = AV_PIX_FMT_YUVA420P;454 ref->format = AV_PIX_FMT_YUVA420P;
412 455
413 if (sws_scale_frame(sws[0], ref, rgb) < 0)456 if (sws_scale_frame(sws[0], ref, rgb) < 0)

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