From 51e141f7ba8a9207a752d295da59b01286e02c23 Mon Sep 17 00:00:00 2001 From: wm4 Date: Sun, 20 Oct 2019 01:57:47 +0200 Subject: sws_utils: hack in zimg redirection support Awful shit. I probably wouldn't accept this code from someone else, just so you know. The idea is that a sws_utils user can automatically use zimg without large code changes. Basically, laziness. Since zimg support is still very new, and I don't want that anything breaks just because zimg was enabled at build time, an option needs to be set to enable it. (I have especially especially obscure stuff in mind, which is all what libswscale is used in mpv.) This _still_ doesn't cause zimg to be used anywhere, because the sws_utils user has to opt-in by setting allow_zimg. This is because some users depend on certain libswscale features. --- video/sws_utils.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- video/sws_utils.h | 7 +++++-- 2 files changed, 49 insertions(+), 3 deletions(-) (limited to 'video') diff --git a/video/sws_utils.c b/video/sws_utils.c index 79017699c4..48fbef7582 100644 --- a/video/sws_utils.c +++ b/video/sws_utils.c @@ -36,6 +36,10 @@ #include "common/msg.h" #include "osdep/endian.h" +#if HAVE_ZIMG +#include "zimg.h" +#endif + //global sws_flags from the command line struct sws_opts { int scaler; @@ -45,6 +49,7 @@ struct sws_opts { int chr_hshift; float chr_sharpen; float lum_sharpen; + int zimg; }; #define OPT_BASE_STRUCT struct sws_opts @@ -68,6 +73,7 @@ const struct m_sub_options sws_conf = { OPT_INT("chs", chr_hshift, 0), OPT_FLOATRANGE("ls", lum_sharpen, 0, -100.0, 100.0), OPT_FLOATRANGE("cs", chr_sharpen, 0, -100.0, 100.0), + OPT_FLAG("allow-zimg", zimg, 0), {0} }, .size = sizeof(struct sws_opts), @@ -98,7 +104,13 @@ void mp_sws_set_from_cmdline(struct mp_sws_context *ctx, struct mpv_global *g) ctx->flags = SWS_PRINT_INFO; ctx->flags |= opts->scaler; + ctx->opts_allow_zimg = opts->zimg; + talloc_free(opts); + +#if HAVE_ZIMG + mp_zimg_set_from_cmdline(ctx->zimg, g); +#endif } bool mp_sws_supported_format(int imgfmt) @@ -126,7 +138,8 @@ static bool cache_valid(struct mp_sws_context *ctx) ctx->flags == old->flags && ctx->brightness == old->brightness && ctx->contrast == old->contrast && - ctx->saturation == old->saturation; + ctx->saturation == old->saturation && + ctx->allow_zimg == old->allow_zimg; } static void free_mp_sws(void *p) @@ -150,8 +163,15 @@ struct mp_sws_context *mp_sws_alloc(void *talloc_ctx) .force_reload = true, .params = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT}, .cached = talloc_zero(ctx, struct mp_sws_context), + .opts_allow_zimg = true, }; talloc_set_destructor(ctx, free_mp_sws); + +#if HAVE_ZIMG + ctx->zimg = mp_zimg_alloc(); + talloc_steal(ctx, ctx->zimg); +#endif + return ctx; } @@ -170,6 +190,23 @@ int mp_sws_reinit(struct mp_sws_context *ctx) return 0; sws_freeContext(ctx->sws); + ctx->sws = NULL; + ctx->zimg_ok = false; + +#if HAVE_ZIMG + if (ctx->allow_zimg && ctx->opts_allow_zimg) { + ctx->zimg->log = ctx->log; + ctx->zimg->src = *src; + ctx->zimg->dst = *dst; + if (mp_zimg_config(ctx->zimg)) { + ctx->zimg_ok = true; + MP_VERBOSE(ctx, "using zimg\n"); + goto success; + } + MP_VERBOSE(ctx, "falling back to swscale\n"); + } +#endif + ctx->sws = sws_alloc_context(); if (!ctx->sws) return -1; @@ -246,6 +283,7 @@ int mp_sws_reinit(struct mp_sws_context *ctx) if (sws_init_context(ctx->sws, ctx->src_filter, ctx->dst_filter) < 0) return -1; +success: ctx->force_reload = false; *ctx->cached = *ctx; return 1; @@ -266,6 +304,11 @@ int mp_sws_scale(struct mp_sws_context *ctx, struct mp_image *dst, return r; } +#if HAVE_ZIMG + if (ctx->zimg_ok) + return mp_zimg_convert(ctx->zimg, dst, src) ? 0 : -1; +#endif + sws_scale(ctx->sws, (const uint8_t *const *) src->planes, src->stride, 0, src->h, dst->planes, dst->stride); return 0; diff --git a/video/sws_utils.h b/video/sws_utils.h index 629e6be2dd..1d40ceab66 100644 --- a/video/sws_utils.h +++ b/video/sws_utils.h @@ -31,6 +31,7 @@ struct mp_sws_context { // mp_sws_scale() will handle the changes transparently. int flags; int brightness, contrast, saturation; + bool allow_zimg; // use zimg if available (ignores filters and all) bool force_reload; // These are also implicitly set by mp_sws_scale(), and thus optional. // Setting them before that call makes sense when using mp_sws_reinit(). @@ -46,8 +47,10 @@ struct mp_sws_context { struct SwsContext *sws; bool supports_csp; - // Contains parameters for which sws is valid - struct mp_sws_context *cached; + // Private. + struct mp_sws_context *cached; // contains parameters for which sws is valid + struct mp_zimg_context *zimg; + bool opts_allow_zimg, zimg_ok; }; struct mp_sws_context *mp_sws_alloc(void *talloc_ctx); -- cgit v1.2.3