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
3131 #include "libavutil/lfg.h"
3232 #include "libavutil/sfc64.h"
3333 #include "libavutil/frame.h"
34+#include "libavutil/opt.h"
35+#include "libavutil/time.h"
3436 #include "libavutil/pixfmt.h"
3537 #include "libavutil/avassert.h"
3638 #include "libavutil/macros.h"
3739
3840 #include "libswscale/swscale.h"
3941
40−enum {
41− WIDTH = 96,
42− HEIGHT = 96,
43−};
44−
4542 struct options {
4643 enum AVPixelFormat src_fmt;
4744 enum AVPixelFormat dst_fmt;
4845 double prob;
46+ int w, h;
47+ int threads;
48+ int iters;
49+ int bench;
4950 };
5051
5152 struct mode {
@@ -53,9 +54,6 @@ struct mode { · 1 unmodified line
5354 SwsDither dither;
5455 };
5556
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−
5957 const struct mode modes[] = {
6058 { SWS_FAST_BILINEAR },
6159 { SWS_BILINEAR },
@@ -114,7 +112,8 @@ static void get_mse(int mse[4], const AVFrame *a, const AVFrame *b, int comps) · 52 unmodified lines
114112 }
115113 }
116114
117−static int scale_legacy(AVFrame *dst, const AVFrame *src, struct mode mode)
115+static int scale_legacy(AVFrame *dst, const AVFrame *src, struct mode mode,
116+ struct options opts)
118117 {
119118 SwsContext *sws_legacy;
120119 int ret;
@@ -131,23 +130,28 @@ static int scale_legacy(AVFrame *dst, const AVFrame *src, struct mode mode) · 10 unmodified lines
131130 sws_legacy->dst_format = dst->format;
132131 sws_legacy->flags = mode.flags;
133132 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;
134137
135− ret = sws_init_context(sws_legacy, NULL, NULL);
136− if (!ret)
138+ for (int i = 0; !ret && i < opts.iters; i++)
137139 ret = sws_scale_frame(sws_legacy, dst, src);
138140
141+error:
139142 sws_freeContext(sws_legacy);
140143 return ret;
141144 }
142145
143146 /* Runs a series of ref -> src -> dst -> out, and compares out vs ref */
144147 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])
148+ int dst_w, int dst_h, struct mode mode, struct options opts,
149+ const AVFrame *ref, const int mse_ref[4])
147150 {
148151 AVFrame *src = NULL, *dst = NULL, *out = NULL;
149152 int mse[4], mse_sws[4], ret = -1;
150153 const int comps = fmt_comps(src_fmt) & fmt_comps(dst_fmt);
154+ int64_t time, time_ref = 0;
151155
152156 src = av_frame_alloc();
153157 dst = av_frame_alloc();
@@ -174,12 +178,20 @@ static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt, · 20 unmodified lines
174178
175179 sws[1]->flags = mode.flags;
176180 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;
181+ sws[1]->threads = opts.threads;
182+
183+ time = av_gettime_relative();
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+ }
181191 }
182192
193+ time = av_gettime_relative() - time;
194+
183195 if (sws_scale_frame(sws[2], out, dst) < 0) {
184196 fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(dst->format),
185197 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
196208
197209 if (!mse_ref) {
198210 /* 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) {
200213 fprintf(stderr, "Failed ref %s ---> %s\n", av_get_pix_fmt_name(src->format),
201214 av_get_pix_fmt_name(dst->format));
202215 goto error;
203216 }
217+ time_ref = av_gettime_relative() - time_ref;
204218
205219 if (sws_scale_frame(sws[2], out, dst) < 0)
206220 goto error;
@@ -221,6 +235,15 @@ static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt, · 14 unmodified lines
221235 }
222236 }
223237
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+
224247 fflush(stdout);
225248 ret = 0; /* fall through */
226249 error:
@@ -232,6 +255,9 @@ static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt, · 5 unmodified lines
232255
233256 static int run_self_tests(const AVFrame *ref, struct options opts)
234257 {
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+
235261 enum AVPixelFormat src_fmt, dst_fmt,
236262 src_fmt_min = 0,
237263 dst_fmt_min = 0,
@@ -254,8 +280,9 @@ static int run_self_tests(const AVFrame *ref, struct options opts) · 16 unmodified lines
254280 for (int m = 0; m < FF_ARRAY_ELEMS(modes); m++) {
255281 if (ff_sfc64_get(&prng_state) > UINT64_MAX * opts.prob)
256282 continue;
283+
257284 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)
259286 return -1;
260287 }
261288 }
@@ -300,7 +327,7 @@ static int run_file_tests(const AVFrame *ref, FILE *fp, struct options opts) · 38 unmodified lines
300327 opts.dst_fmt != AV_PIX_FMT_NONE && dst_fmt != opts.dst_fmt)
301328 continue;
302329
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)
304331 return -1;
305332 }
306333
@@ -312,7 +339,11 @@ int main(int argc, char **argv) · 5 unmodified lines
312339 struct options opts = {
313340 .src_fmt = AV_PIX_FMT_NONE,
314341 .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,
316347 };
317348
318349 AVFrame *rgb = NULL, *ref = NULL;
@@ -335,6 +366,10 @@ int main(int argc, char **argv) · 16 unmodified lines
335366 " Only test the specified destination pixel format\n"
336367 " -src <pixfmt>\n"
337368 " 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"
338373 " -cpuflags <cpuflags>\n"
339374 " Uses the specified cpuflags in the tests\n"
340375 );
@@ -368,6 +403,14 @@ int main(int argc, char **argv) · 27 unmodified lines
368403 fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]);
369404 goto error;
370405 }
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]);
371414 } else if (!strcmp(argv[i], "-p")) {
372415 opts.prob = atof(argv[i + 1]);
373416 } else {
@@ -390,8 +433,8 @@ bad_option: · 16 unmodified lines
390433 rgb = av_frame_alloc();
391434 if (!rgb)
392435 goto error;
393− rgb->width = WIDTH / 12;
394− rgb->height = HEIGHT / 12;
436+ rgb->width = opts.w / 12;
437+ rgb->height = opts.h / 12;
395438 rgb->format = AV_PIX_FMT_RGBA;
396439 if (av_frame_get_buffer(rgb, 32) < 0)
397440 goto error;
@@ -406,8 +449,8 @@ bad_option: · 8 unmodified lines
406449 ref = av_frame_alloc();
407450 if (!ref)
408451 goto error;
409− ref->width = WIDTH;
410− ref->height = HEIGHT;
452+ ref->width = opts.w;
453+ ref->height = opts.h;
411454 ref->format = AV_PIX_FMT_YUVA420P;
412455
413456 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