With the ability to set the thread count as well. This benchmark includes the constant overhead of context initialization.
Unified · Split
| @@ -31,21 +31,22 @@ · 30 unmodified lines | ||
| 31 | 31 | #include "libavutil/lfg.h" |
| 32 | 32 | #include "libavutil/sfc64.h" |
| 33 | 33 | #include "libavutil/frame.h" |
| 34 | +#include "libavutil/opt.h" | |
| 35 | +#include "libavutil/time.h" | |
| 34 | 36 | #include "libavutil/pixfmt.h" |
| 35 | 37 | #include "libavutil/avassert.h" |
| 36 | 38 | #include "libavutil/macros.h" |
| 37 | 39 | |
| 38 | 40 | #include "libswscale/swscale.h" |
| 39 | 41 | |
| 40 | −enum { | |
| 41 | − WIDTH = 96, | |
| 42 | − HEIGHT = 96, | |
| 43 | −}; | |
| 44 | − | |
| 45 | 42 | struct options { |
| 46 | 43 | enum AVPixelFormat src_fmt; |
| 47 | 44 | enum AVPixelFormat dst_fmt; |
| 48 | 45 | double prob; |
| 46 | + int w, h; | |
| 47 | + int threads; | |
| 48 | + int iters; | |
| 49 | + int bench; | |
| 49 | 50 | }; |
| 50 | 51 | |
| 51 | 52 | struct mode { |
| @@ -53,9 +54,6 @@ struct mode { · 1 unmodified line | ||
| 53 | 54 | SwsDither dither; |
| 54 | 55 | }; |
| 55 | 56 | |
| 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 | 57 | const struct mode modes[] = { |
| 60 | 58 | { SWS_FAST_BILINEAR }, |
| 61 | 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 | |
| 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) | |
| 118 | 117 | { |
| 119 | 118 | SwsContext *sws_legacy; |
| 120 | 119 | int ret; |
| @@ -131,23 +130,28 @@ static int scale_legacy(AVFrame *dst, const AVFrame *src, struct mode mode) · 10 unmodified lines | ||
| 131 | 130 | sws_legacy->dst_format = dst->format; |
| 132 | 131 | sws_legacy->flags = mode.flags; |
| 133 | 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); | |
| 136 | − if (!ret) | |
| 138 | + for (int i = 0; !ret && i < opts.iters; i++) | |
| 137 | 139 | ret = sws_scale_frame(sws_legacy, dst, src); |
| 138 | 140 | |
| 141 | +error: | |
| 139 | 142 | sws_freeContext(sws_legacy); |
| 140 | 143 | return ret; |
| 141 | 144 | } |
| 142 | 145 | |
| 143 | 146 | /* Runs a series of ref -> src -> dst -> out, and compares out vs ref */ |
| 144 | 147 | 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]) | |
| 147 | 150 | { |
| 148 | 151 | AVFrame *src = NULL, *dst = NULL, *out = NULL; |
| 149 | 152 | int mse[4], mse_sws[4], ret = -1; |
| 150 | 153 | const int comps = fmt_comps(src_fmt) & fmt_comps(dst_fmt); |
| 154 | + int64_t time, time_ref = 0; | |
| 151 | 155 | |
| 152 | 156 | src = av_frame_alloc(); |
| 153 | 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 | 179 | sws[1]->flags = mode.flags; |
| 176 | 180 | 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 | + } | |
| 181 | 191 | } |
| 182 | 192 | |
| 193 | + time = av_gettime_relative() - time; | |
| 194 | + | |
| 183 | 195 | if (sws_scale_frame(sws[2], out, dst) < 0) { |
| 184 | 196 | fprintf(stderr, "Failed %s ---> %s\n", av_get_pix_fmt_name(dst->format), |
| 185 | 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 | 209 | if (!mse_ref) { |
| 198 | 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 | 213 | fprintf(stderr, "Failed ref %s ---> %s\n", av_get_pix_fmt_name(src->format), |
| 201 | 214 | av_get_pix_fmt_name(dst->format)); |
| 202 | 215 | goto error; |
| 203 | 216 | } |
| 217 | + time_ref = av_gettime_relative() - time_ref; | |
| 204 | 218 | |
| 205 | 219 | if (sws_scale_frame(sws[2], out, dst) < 0) |
| 206 | 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 | 247 | fflush(stdout); |
| 225 | 248 | ret = 0; /* fall through */ |
| 226 | 249 | error: |
| @@ -232,6 +255,9 @@ static int run_test(enum AVPixelFormat src_fmt, enum AVPixelFormat dst_fmt, · 5 unmodified lines | ||
| 232 | 255 | |
| 233 | 256 | static 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 | 261 | enum AVPixelFormat src_fmt, dst_fmt, |
| 236 | 262 | src_fmt_min = 0, |
| 237 | 263 | dst_fmt_min = 0, |
| @@ -254,8 +280,9 @@ static int run_self_tests(const AVFrame *ref, struct options opts) · 16 unmodified lines | ||
| 254 | 280 | for (int m = 0; m < FF_ARRAY_ELEMS(modes); m++) { |
| 255 | 281 | if (ff_sfc64_get(&prng_state) > UINT64_MAX * opts.prob) |
| 256 | 282 | continue; |
| 283 | + | |
| 257 | 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 | 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 | 327 | opts.dst_fmt != AV_PIX_FMT_NONE && dst_fmt != opts.dst_fmt) |
| 301 | 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 | 331 | return -1; |
| 305 | 332 | } |
| 306 | 333 | |
| @@ -312,7 +339,11 @@ int main(int argc, char **argv) · 5 unmodified lines | ||
| 312 | 339 | struct options opts = { |
| 313 | 340 | .src_fmt = AV_PIX_FMT_NONE, |
| 314 | 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 | 349 | AVFrame *rgb = NULL, *ref = NULL; |
| @@ -335,6 +366,10 @@ int main(int argc, char **argv) · 16 unmodified lines | ||
| 335 | 366 | " Only test the specified destination pixel format\n" |
| 336 | 367 | " -src <pixfmt>\n" |
| 337 | 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 | 373 | " -cpuflags <cpuflags>\n" |
| 339 | 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 | 403 | fprintf(stderr, "invalid pixel format %s\n", argv[i + 1]); |
| 369 | 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 | 414 | } else if (!strcmp(argv[i], "-p")) { |
| 372 | 415 | opts.prob = atof(argv[i + 1]); |
| 373 | 416 | } else { |
| @@ -390,8 +433,8 @@ bad_option: · 16 unmodified lines | ||
| 390 | 433 | rgb = av_frame_alloc(); |
| 391 | 434 | if (!rgb) |
| 392 | 435 | goto error; |
| 393 | − rgb->width = WIDTH / 12; | |
| 394 | − rgb->height = HEIGHT / 12; | |
| 436 | + rgb->width = opts.w / 12; | |
| 437 | + rgb->height = opts.h / 12; | |
| 395 | 438 | rgb->format = AV_PIX_FMT_RGBA; |
| 396 | 439 | if (av_frame_get_buffer(rgb, 32) < 0) |
| 397 | 440 | goto error; |
| @@ -406,8 +449,8 @@ bad_option: · 8 unmodified lines | ||
| 406 | 449 | ref = av_frame_alloc(); |
| 407 | 450 | if (!ref) |
| 408 | 451 | goto error; |
| 409 | − ref->width = WIDTH; | |
| 410 | − ref->height = HEIGHT; | |
| 452 | + ref->width = opts.w; | |
| 453 | + ref->height = opts.h; | |
| 411 | 454 | ref->format = AV_PIX_FMT_YUVA420P; |
| 412 | 455 | |
| 413 | 456 | if (sws_scale_frame(sws[0], ref, rgb) < 0) |