2012-10-24 17:00:49 +00:00
|
|
|
/*
|
2015-04-13 07:36:54 +00:00
|
|
|
* This file is part of mpv.
|
2012-10-24 17:00:49 +00:00
|
|
|
*
|
2017-09-21 11:50:18 +00:00
|
|
|
* mpv is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2012-10-24 17:00:49 +00:00
|
|
|
*
|
2015-04-13 07:36:54 +00:00
|
|
|
* mpv is distributed in the hope that it will be useful,
|
2012-10-24 17:00:49 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-09-21 11:50:18 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2012-10-24 17:00:49 +00:00
|
|
|
*
|
2017-09-21 11:50:18 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2012-10-24 17:00:49 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2013-07-18 11:46:05 +00:00
|
|
|
#include <libswscale/swscale.h>
|
2013-07-25 21:02:23 +00:00
|
|
|
#include <libavcodec/avcodec.h>
|
2015-02-06 22:20:41 +00:00
|
|
|
#include <libavutil/bswap.h>
|
2012-10-27 16:06:09 +00:00
|
|
|
#include <libavutil/opt.h>
|
2022-11-29 19:22:23 +00:00
|
|
|
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 37, 100)
|
|
|
|
#include <libavutil/pixdesc.h>
|
|
|
|
#endif
|
2012-10-27 16:06:09 +00:00
|
|
|
|
2013-12-18 16:12:21 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "sws_utils.h"
|
2012-10-24 17:00:49 +00:00
|
|
|
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/common.h"
|
2018-01-16 10:49:23 +00:00
|
|
|
#include "options/m_config.h"
|
2014-06-10 20:41:14 +00:00
|
|
|
#include "options/m_option.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "video/mp_image.h"
|
|
|
|
#include "video/img_format.h"
|
2012-10-24 17:00:49 +00:00
|
|
|
#include "fmt-conversion.h"
|
2012-11-09 00:06:43 +00:00
|
|
|
#include "csputils.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2015-02-06 22:20:41 +00:00
|
|
|
#include "osdep/endian.h"
|
2012-10-24 17:00:49 +00:00
|
|
|
|
2019-10-19 23:57:47 +00:00
|
|
|
#if HAVE_ZIMG
|
|
|
|
#include "zimg.h"
|
|
|
|
#endif
|
|
|
|
|
2012-10-24 17:00:49 +00:00
|
|
|
//global sws_flags from the command line
|
2014-06-10 20:41:14 +00:00
|
|
|
struct sws_opts {
|
|
|
|
int scaler;
|
|
|
|
float lum_gblur;
|
|
|
|
float chr_gblur;
|
|
|
|
int chr_vshift;
|
|
|
|
int chr_hshift;
|
|
|
|
float chr_sharpen;
|
|
|
|
float lum_sharpen;
|
sws_utils, zimg: destroy vo_x11 and vo_drm performance
Raise swscale and zimg default parameters. This restores screenshot
quality settings (maybe) unset in the commit before. Also expose some
more libswscale and zimg options.
Since these options are also used for VOs like x11 and drm, this will
make x11/drm/etc. much slower. For compensation, provide a profile that
sets the old option values: sw-fast. I'm also enabling zimg here, just
as an experiment.
The core problem is that we have a single set of command line options
which control the settings used for most swscale/zimg uses. This was
done in the previous commit. It cannot differentiate between the VOs,
which need to be realtime and may accept/require lower quality options,
and things like screenshots or vo_image, which can be slower, but should
not sacrifice quality by default.
Should this have two sets of options or something similar to do the
right thing depending on the code which calls libswscale? Maybe. Or
should I just ignore the problem, make it someone else's problem (users
who want to use software conversion VOs), provide a sub-optimal
solution, and call it a day? Definitely, sounds good, pushing to master,
goodbye.
2019-10-31 15:45:28 +00:00
|
|
|
int fast;
|
|
|
|
int bitexact;
|
2019-10-19 23:57:47 +00:00
|
|
|
int zimg;
|
2014-06-10 20:41:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define OPT_BASE_STRUCT struct sws_opts
|
|
|
|
const struct m_sub_options sws_conf = {
|
|
|
|
.opts = (const m_option_t[]) {
|
options: change option macros and all option declarations
Change all OPT_* macros such that they don't define the entire m_option
initializer, and instead expand only to a part of it, which sets certain
fields. This requires changing almost every option declaration, because
they all use these macros. A declaration now always starts with
{"name", ...
followed by designated initializers only (possibly wrapped in macros).
The OPT_* macros now initialize the .offset and .type fields only,
sometimes also .priv and others.
I think this change makes the option macros less tricky. The old code
had to stuff everything into macro arguments (and attempted to allow
setting arbitrary fields by letting the user pass designated
initializers in the vararg parts). Some of this was made messy due to
C99 and C11 not allowing 0-sized varargs with ',' removal. It's also
possible that this change is pointless, other than cosmetic preferences.
Not too happy about some things. For example, the OPT_CHOICE()
indentation I applied looks a bit ugly.
Much of this change was done with regex search&replace, but some places
required manual editing. In particular, code in "obscure" areas (which I
didn't include in compilation) might be broken now.
In wayland_common.c the author of some option declarations confused the
flags parameter with the default value (though the default value was
also properly set below). I fixed this with this change.
2020-03-14 20:28:01 +00:00
|
|
|
{"scaler", OPT_CHOICE(scaler,
|
|
|
|
{"fast-bilinear", SWS_FAST_BILINEAR},
|
|
|
|
{"bilinear", SWS_BILINEAR},
|
|
|
|
{"bicubic", SWS_BICUBIC},
|
|
|
|
{"x", SWS_X},
|
|
|
|
{"point", SWS_POINT},
|
|
|
|
{"area", SWS_AREA},
|
|
|
|
{"bicublin", SWS_BICUBLIN},
|
|
|
|
{"gauss", SWS_GAUSS},
|
|
|
|
{"sinc", SWS_SINC},
|
|
|
|
{"lanczos", SWS_LANCZOS},
|
|
|
|
{"spline", SWS_SPLINE})},
|
|
|
|
{"lgb", OPT_FLOAT(lum_gblur), M_RANGE(0, 100.0)},
|
|
|
|
{"cgb", OPT_FLOAT(chr_gblur), M_RANGE(0, 100.0)},
|
|
|
|
{"cvs", OPT_INT(chr_vshift)},
|
|
|
|
{"chs", OPT_INT(chr_hshift)},
|
|
|
|
{"ls", OPT_FLOAT(lum_sharpen), M_RANGE(-100.0, 100.0)},
|
|
|
|
{"cs", OPT_FLOAT(chr_sharpen), M_RANGE(-100.0, 100.0)},
|
|
|
|
{"fast", OPT_FLAG(fast)},
|
|
|
|
{"bitexact", OPT_FLAG(bitexact)},
|
|
|
|
{"allow-zimg", OPT_FLAG(zimg)},
|
2014-06-10 20:41:14 +00:00
|
|
|
{0}
|
|
|
|
},
|
|
|
|
.size = sizeof(struct sws_opts),
|
|
|
|
.defaults = &(const struct sws_opts){
|
sws_utils, zimg: destroy vo_x11 and vo_drm performance
Raise swscale and zimg default parameters. This restores screenshot
quality settings (maybe) unset in the commit before. Also expose some
more libswscale and zimg options.
Since these options are also used for VOs like x11 and drm, this will
make x11/drm/etc. much slower. For compensation, provide a profile that
sets the old option values: sw-fast. I'm also enabling zimg here, just
as an experiment.
The core problem is that we have a single set of command line options
which control the settings used for most swscale/zimg uses. This was
done in the previous commit. It cannot differentiate between the VOs,
which need to be realtime and may accept/require lower quality options,
and things like screenshots or vo_image, which can be slower, but should
not sacrifice quality by default.
Should this have two sets of options or something similar to do the
right thing depending on the code which calls libswscale? Maybe. Or
should I just ignore the problem, make it someone else's problem (users
who want to use software conversion VOs), provide a sub-optimal
solution, and call it a day? Definitely, sounds good, pushing to master,
goodbye.
2019-10-31 15:45:28 +00:00
|
|
|
.scaler = SWS_LANCZOS,
|
2020-02-12 17:06:53 +00:00
|
|
|
.zimg = 1,
|
2014-06-10 20:41:14 +00:00
|
|
|
},
|
|
|
|
};
|
2012-10-24 17:00:49 +00:00
|
|
|
|
2013-07-18 11:48:57 +00:00
|
|
|
// Highest quality, but also slowest.
|
sws_utils, zimg: destroy vo_x11 and vo_drm performance
Raise swscale and zimg default parameters. This restores screenshot
quality settings (maybe) unset in the commit before. Also expose some
more libswscale and zimg options.
Since these options are also used for VOs like x11 and drm, this will
make x11/drm/etc. much slower. For compensation, provide a profile that
sets the old option values: sw-fast. I'm also enabling zimg here, just
as an experiment.
The core problem is that we have a single set of command line options
which control the settings used for most swscale/zimg uses. This was
done in the previous commit. It cannot differentiate between the VOs,
which need to be realtime and may accept/require lower quality options,
and things like screenshots or vo_image, which can be slower, but should
not sacrifice quality by default.
Should this have two sets of options or something similar to do the
right thing depending on the code which calls libswscale? Maybe. Or
should I just ignore the problem, make it someone else's problem (users
who want to use software conversion VOs), provide a sub-optimal
solution, and call it a day? Definitely, sounds good, pushing to master,
goodbye.
2019-10-31 15:45:28 +00:00
|
|
|
static const int mp_sws_hq_flags = SWS_FULL_CHR_H_INT | SWS_FULL_CHR_H_INP |
|
|
|
|
SWS_ACCURATE_RND;
|
2012-10-24 17:00:49 +00:00
|
|
|
|
2013-08-09 22:27:41 +00:00
|
|
|
// Fast, lossy.
|
|
|
|
const int mp_sws_fast_flags = SWS_BILINEAR;
|
|
|
|
|
2013-07-18 11:17:56 +00:00
|
|
|
// Set ctx parameters to global command line flags.
|
2019-10-31 14:18:57 +00:00
|
|
|
static void mp_sws_update_from_cmdline(struct mp_sws_context *ctx)
|
2013-07-18 11:17:56 +00:00
|
|
|
{
|
2019-10-31 14:18:57 +00:00
|
|
|
m_config_cache_update(ctx->opts_cache);
|
|
|
|
struct sws_opts *opts = ctx->opts_cache->opts;
|
2018-01-16 10:49:23 +00:00
|
|
|
|
2013-07-18 11:17:56 +00:00
|
|
|
sws_freeFilter(ctx->src_filter);
|
2014-06-10 20:41:14 +00:00
|
|
|
ctx->src_filter = sws_getDefaultFilter(opts->lum_gblur, opts->chr_gblur,
|
|
|
|
opts->lum_sharpen, opts->chr_sharpen,
|
|
|
|
opts->chr_hshift, opts->chr_vshift, 0);
|
2013-07-18 11:17:56 +00:00
|
|
|
ctx->force_reload = true;
|
|
|
|
|
|
|
|
ctx->flags = SWS_PRINT_INFO;
|
2014-06-10 20:41:14 +00:00
|
|
|
ctx->flags |= opts->scaler;
|
sws_utils, zimg: destroy vo_x11 and vo_drm performance
Raise swscale and zimg default parameters. This restores screenshot
quality settings (maybe) unset in the commit before. Also expose some
more libswscale and zimg options.
Since these options are also used for VOs like x11 and drm, this will
make x11/drm/etc. much slower. For compensation, provide a profile that
sets the old option values: sw-fast. I'm also enabling zimg here, just
as an experiment.
The core problem is that we have a single set of command line options
which control the settings used for most swscale/zimg uses. This was
done in the previous commit. It cannot differentiate between the VOs,
which need to be realtime and may accept/require lower quality options,
and things like screenshots or vo_image, which can be slower, but should
not sacrifice quality by default.
Should this have two sets of options or something similar to do the
right thing depending on the code which calls libswscale? Maybe. Or
should I just ignore the problem, make it someone else's problem (users
who want to use software conversion VOs), provide a sub-optimal
solution, and call it a day? Definitely, sounds good, pushing to master,
goodbye.
2019-10-31 15:45:28 +00:00
|
|
|
if (!opts->fast)
|
|
|
|
ctx->flags |= mp_sws_hq_flags;
|
|
|
|
if (opts->bitexact)
|
|
|
|
ctx->flags |= SWS_BITEXACT;
|
2018-01-16 10:49:23 +00:00
|
|
|
|
2019-10-31 14:18:57 +00:00
|
|
|
ctx->allow_zimg = opts->zimg;
|
2013-07-18 11:17:56 +00:00
|
|
|
}
|
|
|
|
|
2012-10-19 17:11:58 +00:00
|
|
|
bool mp_sws_supported_format(int imgfmt)
|
|
|
|
{
|
2013-11-29 16:39:57 +00:00
|
|
|
enum AVPixelFormat av_format = imgfmt2pixfmt(imgfmt);
|
2012-10-19 17:11:58 +00:00
|
|
|
|
2013-11-29 16:39:57 +00:00
|
|
|
return av_format != AV_PIX_FMT_NONE && sws_isSupportedInput(av_format)
|
2012-10-19 17:11:58 +00:00
|
|
|
&& sws_isSupportedOutput(av_format);
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:55:35 +00:00
|
|
|
static bool allow_zimg(struct mp_sws_context *ctx)
|
|
|
|
{
|
|
|
|
return ctx->force_scaler == MP_SWS_ZIMG ||
|
|
|
|
(ctx->force_scaler == MP_SWS_AUTO && ctx->allow_zimg);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool allow_sws(struct mp_sws_context *ctx)
|
|
|
|
{
|
|
|
|
return ctx->force_scaler == MP_SWS_SWS || ctx->force_scaler == MP_SWS_AUTO;
|
|
|
|
}
|
|
|
|
|
2019-10-20 17:36:40 +00:00
|
|
|
bool mp_sws_supports_formats(struct mp_sws_context *ctx,
|
|
|
|
int imgfmt_out, int imgfmt_in)
|
|
|
|
{
|
|
|
|
#if HAVE_ZIMG
|
2020-04-12 19:55:35 +00:00
|
|
|
if (allow_zimg(ctx)) {
|
2019-10-20 17:36:40 +00:00
|
|
|
if (mp_zimg_supports_in_format(imgfmt_in) &&
|
|
|
|
mp_zimg_supports_out_format(imgfmt_out))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-04-12 19:55:35 +00:00
|
|
|
return allow_sws(ctx) &&
|
|
|
|
sws_isSupportedInput(imgfmt2pixfmt(imgfmt_in)) &&
|
2019-10-20 17:36:40 +00:00
|
|
|
sws_isSupportedOutput(imgfmt2pixfmt(imgfmt_out));
|
|
|
|
}
|
|
|
|
|
2012-10-27 16:06:09 +00:00
|
|
|
static int mp_csp_to_sws_colorspace(enum mp_csp csp)
|
|
|
|
{
|
2015-05-03 23:27:55 +00:00
|
|
|
// The SWS_CS_* macros are just convenience redefinitions of the
|
|
|
|
// AVCOL_SPC_* macros, inside swscale.h.
|
|
|
|
return mp_csp_to_avcol_spc(csp);
|
2012-10-27 16:06:09 +00:00
|
|
|
}
|
|
|
|
|
2013-07-18 11:17:56 +00:00
|
|
|
static bool cache_valid(struct mp_sws_context *ctx)
|
|
|
|
{
|
|
|
|
struct mp_sws_context *old = ctx->cached;
|
|
|
|
if (ctx->force_reload)
|
|
|
|
return false;
|
2014-06-17 21:30:16 +00:00
|
|
|
return mp_image_params_equal(&ctx->src, &old->src) &&
|
|
|
|
mp_image_params_equal(&ctx->dst, &old->dst) &&
|
2013-07-18 11:17:56 +00:00
|
|
|
ctx->flags == old->flags &&
|
2019-10-31 14:18:57 +00:00
|
|
|
ctx->allow_zimg == old->allow_zimg &&
|
2020-04-12 19:55:35 +00:00
|
|
|
ctx->force_scaler == old->force_scaler &&
|
2019-10-31 14:18:57 +00:00
|
|
|
(!ctx->opts_cache || !m_config_cache_update(ctx->opts_cache));
|
draw_bmp: add RGB rendering to fix image quality issues
As pointed out in commit ed01df, the quality loss due to frequent
conversion between RGB and YUV is too much when drawing OSD and
subtitles.
Fix this by staying in the same colorspace when drawing subtitles.
Render directly to RGB, without converting to YUV first.
The bad thing about packed RGB is that there are many pixel formats,
which would all require special code for blending. It's also completely
incompatible to planar YUV. Use planar RGB instead, which allows us to
reuse all code originally written for planar YUV. The only thing that
needs to be changed is the color conversion in the libass case. (In
exchange for simpler code, the image has to be copied, but this is
still much better than converting to YUV.)
Unfortunately, libswscale doesn't support planar RGB output. Add a hack
to sws_utils.c to handle conversion to planar RGB. In the common case,
when converting 32 bit per pixel RGB, calling swscale can be avoided
entirely.
The change in mp_image.c is needed to allocate GBRP images correctly.
(The issue with vo_x11 could be easily solved by always backing up the
same bounding box as the bitmap drawing RGB<->YUV conversion does, but
this commit is probably the better fix.)
2012-11-22 12:30:16 +00:00
|
|
|
}
|
|
|
|
|
2013-10-12 23:16:30 +00:00
|
|
|
static void free_mp_sws(void *p)
|
2013-07-18 11:17:56 +00:00
|
|
|
{
|
|
|
|
struct mp_sws_context *ctx = p;
|
|
|
|
sws_freeContext(ctx->sws);
|
|
|
|
sws_freeFilter(ctx->src_filter);
|
|
|
|
sws_freeFilter(ctx->dst_filter);
|
2020-09-17 13:20:08 +00:00
|
|
|
TA_FREEP(&ctx->aligned_src);
|
|
|
|
TA_FREEP(&ctx->aligned_dst);
|
2013-07-18 11:17:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// You're supposed to set your scaling parameters on the returned context.
|
|
|
|
// Free the context with talloc_free().
|
2013-11-01 16:33:11 +00:00
|
|
|
struct mp_sws_context *mp_sws_alloc(void *talloc_ctx)
|
2013-07-18 11:17:56 +00:00
|
|
|
{
|
2013-11-01 16:33:11 +00:00
|
|
|
struct mp_sws_context *ctx = talloc_ptrtype(talloc_ctx, ctx);
|
2013-07-18 11:17:56 +00:00
|
|
|
*ctx = (struct mp_sws_context) {
|
2013-12-21 16:57:10 +00:00
|
|
|
.log = mp_null_log,
|
2013-07-18 11:17:56 +00:00
|
|
|
.flags = SWS_BILINEAR,
|
|
|
|
.force_reload = true,
|
2013-07-22 12:41:33 +00:00
|
|
|
.params = {SWS_PARAM_DEFAULT, SWS_PARAM_DEFAULT},
|
2013-07-18 11:17:56 +00:00
|
|
|
.cached = talloc_zero(ctx, struct mp_sws_context),
|
|
|
|
};
|
|
|
|
talloc_set_destructor(ctx, free_mp_sws);
|
2019-10-19 23:57:47 +00:00
|
|
|
|
|
|
|
#if HAVE_ZIMG
|
|
|
|
ctx->zimg = mp_zimg_alloc();
|
|
|
|
talloc_steal(ctx, ctx->zimg);
|
|
|
|
#endif
|
|
|
|
|
2013-07-18 11:17:56 +00:00
|
|
|
return ctx;
|
|
|
|
}
|
draw_bmp: add RGB rendering to fix image quality issues
As pointed out in commit ed01df, the quality loss due to frequent
conversion between RGB and YUV is too much when drawing OSD and
subtitles.
Fix this by staying in the same colorspace when drawing subtitles.
Render directly to RGB, without converting to YUV first.
The bad thing about packed RGB is that there are many pixel formats,
which would all require special code for blending. It's also completely
incompatible to planar YUV. Use planar RGB instead, which allows us to
reuse all code originally written for planar YUV. The only thing that
needs to be changed is the color conversion in the libass case. (In
exchange for simpler code, the image has to be copied, but this is
still much better than converting to YUV.)
Unfortunately, libswscale doesn't support planar RGB output. Add a hack
to sws_utils.c to handle conversion to planar RGB. In the common case,
when converting 32 bit per pixel RGB, calling swscale can be avoided
entirely.
The change in mp_image.c is needed to allocate GBRP images correctly.
(The issue with vo_x11 could be easily solved by always backing up the
same bounding box as the bitmap drawing RGB<->YUV conversion does, but
this commit is probably the better fix.)
2012-11-22 12:30:16 +00:00
|
|
|
|
2019-10-31 14:18:57 +00:00
|
|
|
// Enable auto-update of parameters from command line. Don't try to set custom
|
|
|
|
// options (other than possibly .src/.dst), because they might be overwritten
|
|
|
|
// if the user changes any options.
|
|
|
|
void mp_sws_enable_cmdline_opts(struct mp_sws_context *ctx, struct mpv_global *g)
|
|
|
|
{
|
|
|
|
if (ctx->opts_cache)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ctx->opts_cache = m_config_cache_alloc(ctx, g, &sws_conf);
|
|
|
|
ctx->force_reload = true;
|
2019-11-02 16:50:24 +00:00
|
|
|
mp_sws_update_from_cmdline(ctx);
|
2019-10-31 14:18:57 +00:00
|
|
|
|
|
|
|
#if HAVE_ZIMG
|
|
|
|
mp_zimg_enable_cmdline_opts(ctx->zimg, g);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-07-18 11:17:56 +00:00
|
|
|
// Reinitialize (if needed) - return error code.
|
|
|
|
// Optional, but possibly useful to avoid having to handle mp_sws_scale errors.
|
|
|
|
int mp_sws_reinit(struct mp_sws_context *ctx)
|
2012-11-24 23:06:16 +00:00
|
|
|
{
|
2020-07-08 20:41:31 +00:00
|
|
|
struct mp_image_params src = ctx->src;
|
|
|
|
struct mp_image_params dst = ctx->dst;
|
2013-08-24 17:37:34 +00:00
|
|
|
|
2013-07-18 11:17:56 +00:00
|
|
|
if (cache_valid(ctx))
|
|
|
|
return 0;
|
|
|
|
|
2019-10-31 14:18:57 +00:00
|
|
|
if (ctx->opts_cache)
|
|
|
|
mp_sws_update_from_cmdline(ctx);
|
|
|
|
|
2013-07-18 11:17:56 +00:00
|
|
|
sws_freeContext(ctx->sws);
|
2019-10-19 23:57:47 +00:00
|
|
|
ctx->sws = NULL;
|
|
|
|
ctx->zimg_ok = false;
|
2020-09-17 13:20:08 +00:00
|
|
|
TA_FREEP(&ctx->aligned_src);
|
|
|
|
TA_FREEP(&ctx->aligned_dst);
|
2019-10-19 23:57:47 +00:00
|
|
|
|
|
|
|
#if HAVE_ZIMG
|
2020-04-12 19:55:35 +00:00
|
|
|
if (allow_zimg(ctx)) {
|
2019-10-19 23:57:47 +00:00
|
|
|
ctx->zimg->log = ctx->log;
|
2020-07-08 20:41:31 +00:00
|
|
|
ctx->zimg->src = src;
|
|
|
|
ctx->zimg->dst = dst;
|
2020-05-09 15:56:21 +00:00
|
|
|
if (ctx->zimg_opts)
|
|
|
|
ctx->zimg->opts = *ctx->zimg_opts;
|
2019-10-19 23:57:47 +00:00
|
|
|
if (mp_zimg_config(ctx->zimg)) {
|
|
|
|
ctx->zimg_ok = true;
|
2019-10-20 21:22:21 +00:00
|
|
|
MP_VERBOSE(ctx, "Using zimg.\n");
|
2019-10-19 23:57:47 +00:00
|
|
|
goto success;
|
|
|
|
}
|
2019-10-20 21:22:21 +00:00
|
|
|
MP_WARN(ctx, "Not using zimg, falling back to swscale.\n");
|
2019-10-19 23:57:47 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-04-12 19:55:35 +00:00
|
|
|
if (!allow_sws(ctx)) {
|
|
|
|
MP_ERR(ctx, "No scaler.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-07-18 11:17:56 +00:00
|
|
|
ctx->sws = sws_alloc_context();
|
|
|
|
if (!ctx->sws)
|
|
|
|
return -1;
|
|
|
|
|
2020-07-08 20:41:31 +00:00
|
|
|
mp_image_params_guess_csp(&src); // sanitize colorspace/colorlevels
|
|
|
|
mp_image_params_guess_csp(&dst);
|
2013-07-18 11:17:56 +00:00
|
|
|
|
2020-07-08 20:41:31 +00:00
|
|
|
enum AVPixelFormat s_fmt = imgfmt2pixfmt(src.imgfmt);
|
2013-12-21 16:57:10 +00:00
|
|
|
if (s_fmt == AV_PIX_FMT_NONE || sws_isSupportedInput(s_fmt) < 1) {
|
|
|
|
MP_ERR(ctx, "Input image format %s not supported by libswscale.\n",
|
2020-07-08 20:41:31 +00:00
|
|
|
mp_imgfmt_to_name(src.imgfmt));
|
2013-07-18 11:17:56 +00:00
|
|
|
return -1;
|
2013-12-21 16:57:10 +00:00
|
|
|
}
|
2012-10-27 16:06:09 +00:00
|
|
|
|
2020-07-08 20:41:31 +00:00
|
|
|
enum AVPixelFormat d_fmt = imgfmt2pixfmt(dst.imgfmt);
|
2013-12-21 16:57:10 +00:00
|
|
|
if (d_fmt == AV_PIX_FMT_NONE || sws_isSupportedOutput(d_fmt) < 1) {
|
|
|
|
MP_ERR(ctx, "Output image format %s not supported by libswscale.\n",
|
2020-07-08 20:41:31 +00:00
|
|
|
mp_imgfmt_to_name(dst.imgfmt));
|
2013-07-18 11:17:56 +00:00
|
|
|
return -1;
|
2013-12-21 16:57:10 +00:00
|
|
|
}
|
2013-07-18 11:17:56 +00:00
|
|
|
|
2020-07-08 20:41:31 +00:00
|
|
|
int s_csp = mp_csp_to_sws_colorspace(src.color.space);
|
|
|
|
int s_range = src.color.levels == MP_CSP_LEVELS_PC;
|
2013-07-18 11:17:56 +00:00
|
|
|
|
2020-07-08 20:41:31 +00:00
|
|
|
int d_csp = mp_csp_to_sws_colorspace(dst.color.space);
|
|
|
|
int d_range = dst.color.levels == MP_CSP_LEVELS_PC;
|
2012-10-27 16:06:09 +00:00
|
|
|
|
2013-07-18 11:17:56 +00:00
|
|
|
av_opt_set_int(ctx->sws, "sws_flags", ctx->flags, 0);
|
2012-10-27 16:06:09 +00:00
|
|
|
|
2020-07-08 20:41:31 +00:00
|
|
|
av_opt_set_int(ctx->sws, "srcw", src.w, 0);
|
|
|
|
av_opt_set_int(ctx->sws, "srch", src.h, 0);
|
2013-07-18 11:17:56 +00:00
|
|
|
av_opt_set_int(ctx->sws, "src_format", s_fmt, 0);
|
2012-10-27 16:06:09 +00:00
|
|
|
|
2020-07-08 20:41:31 +00:00
|
|
|
av_opt_set_int(ctx->sws, "dstw", dst.w, 0);
|
|
|
|
av_opt_set_int(ctx->sws, "dsth", dst.h, 0);
|
2013-07-18 11:17:56 +00:00
|
|
|
av_opt_set_int(ctx->sws, "dst_format", d_fmt, 0);
|
2012-10-27 16:06:09 +00:00
|
|
|
|
2013-07-22 12:41:33 +00:00
|
|
|
av_opt_set_double(ctx->sws, "param0", ctx->params[0], 0);
|
|
|
|
av_opt_set_double(ctx->sws, "param1", ctx->params[1], 0);
|
|
|
|
|
2020-07-08 20:41:31 +00:00
|
|
|
int cr_src = mp_chroma_location_to_av(src.chroma_location);
|
|
|
|
int cr_dst = mp_chroma_location_to_av(dst.chroma_location);
|
2013-07-25 21:02:23 +00:00
|
|
|
int cr_xpos, cr_ypos;
|
2022-11-29 19:22:23 +00:00
|
|
|
#if LIBAVUTIL_VERSION_INT >= AV_VERSION_INT(57, 37, 100)
|
|
|
|
if (av_chroma_location_enum_to_pos(&cr_xpos, &cr_ypos, cr_src) >= 0) {
|
|
|
|
av_opt_set_int(ctx->sws, "src_h_chr_pos", cr_xpos, 0);
|
|
|
|
av_opt_set_int(ctx->sws, "src_v_chr_pos", cr_ypos, 0);
|
|
|
|
}
|
|
|
|
if (av_chroma_location_enum_to_pos(&cr_xpos, &cr_ypos, cr_dst) >= 0) {
|
|
|
|
av_opt_set_int(ctx->sws, "dst_h_chr_pos", cr_xpos, 0);
|
|
|
|
av_opt_set_int(ctx->sws, "dst_v_chr_pos", cr_ypos, 0);
|
|
|
|
}
|
|
|
|
#else
|
2013-07-25 21:02:23 +00:00
|
|
|
if (avcodec_enum_to_chroma_pos(&cr_xpos, &cr_ypos, cr_src) >= 0) {
|
|
|
|
av_opt_set_int(ctx->sws, "src_h_chr_pos", cr_xpos, 0);
|
|
|
|
av_opt_set_int(ctx->sws, "src_v_chr_pos", cr_ypos, 0);
|
|
|
|
}
|
|
|
|
if (avcodec_enum_to_chroma_pos(&cr_xpos, &cr_ypos, cr_dst) >= 0) {
|
|
|
|
av_opt_set_int(ctx->sws, "dst_h_chr_pos", cr_xpos, 0);
|
|
|
|
av_opt_set_int(ctx->sws, "dst_v_chr_pos", cr_ypos, 0);
|
|
|
|
}
|
2022-11-29 19:22:23 +00:00
|
|
|
#endif
|
2013-07-25 21:02:23 +00:00
|
|
|
|
2013-07-18 11:17:56 +00:00
|
|
|
// This can fail even with normal operation, e.g. if a conversion path
|
|
|
|
// simply does not support these settings.
|
2015-03-01 21:31:35 +00:00
|
|
|
int r =
|
|
|
|
sws_setColorspaceDetails(ctx->sws, sws_getCoefficients(s_csp), s_range,
|
|
|
|
sws_getCoefficients(d_csp), d_range,
|
2020-04-24 12:31:11 +00:00
|
|
|
0, 1 << 16, 1 << 16);
|
2015-03-01 21:31:35 +00:00
|
|
|
ctx->supports_csp = r >= 0;
|
2013-07-18 11:17:56 +00:00
|
|
|
|
|
|
|
if (sws_init_context(ctx->sws, ctx->src_filter, ctx->dst_filter) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2019-10-19 23:57:47 +00:00
|
|
|
success:
|
2013-07-18 11:17:56 +00:00
|
|
|
ctx->force_reload = false;
|
|
|
|
*ctx->cached = *ctx;
|
|
|
|
return 1;
|
2012-11-24 23:06:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-17 13:20:08 +00:00
|
|
|
static struct mp_image *check_alignment(struct mp_log *log,
|
|
|
|
struct mp_image **alloc,
|
|
|
|
struct mp_image *img)
|
|
|
|
{
|
|
|
|
// It's completely unclear which alignment libswscale wants (for performance)
|
|
|
|
// or requires (for avoiding crashes and memory corruption).
|
|
|
|
// Is it av_cpu_max_align()? Is it the hardcoded AVFrame "default" of 32
|
|
|
|
// in get_video_buffer()? Is it whatever avcodec_align_dimensions2()
|
|
|
|
// determines? It's like you can't win if you try to prevent libswscale from
|
|
|
|
// corrupting memory...
|
|
|
|
// So use 32, a value that has been experimentally determined to be safe,
|
|
|
|
// and which in most cases is not larger than decoder output. It is smaller
|
|
|
|
// or equal to what most image allocators in mpv/ffmpeg use.
|
|
|
|
size_t align = 32;
|
|
|
|
assert(align <= MP_IMAGE_BYTE_ALIGN); // or mp_image_alloc will not cut it
|
|
|
|
|
|
|
|
bool is_aligned = true;
|
|
|
|
for (int p = 0; p < img->num_planes; p++) {
|
|
|
|
is_aligned &= MP_IS_ALIGNED((uintptr_t)img->planes[p], align);
|
|
|
|
is_aligned &= MP_IS_ALIGNED(labs(img->stride[p]), align);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_aligned)
|
|
|
|
return img;
|
|
|
|
|
|
|
|
if (!*alloc) {
|
|
|
|
mp_verbose(log, "unaligned libswscale parameter; using slow copy.\n");
|
|
|
|
*alloc = mp_image_alloc(img->imgfmt, img->w, img->h);
|
|
|
|
if (!*alloc)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mp_image_copy_attributes(*alloc, img);
|
|
|
|
return *alloc;
|
|
|
|
}
|
|
|
|
|
2013-07-18 11:17:56 +00:00
|
|
|
// Scale from src to dst - if src/dst have different parameters from previous
|
|
|
|
// calls, the context is reinitialized. Return error code. (It can fail if
|
|
|
|
// reinitialization was necessary, and swscale returned an error.)
|
|
|
|
int mp_sws_scale(struct mp_sws_context *ctx, struct mp_image *dst,
|
|
|
|
struct mp_image *src)
|
2012-11-24 23:06:16 +00:00
|
|
|
{
|
2014-06-17 21:26:53 +00:00
|
|
|
ctx->src = src->params;
|
|
|
|
ctx->dst = dst->params;
|
2012-10-27 16:06:09 +00:00
|
|
|
|
2013-07-18 11:17:56 +00:00
|
|
|
int r = mp_sws_reinit(ctx);
|
|
|
|
if (r < 0) {
|
2013-12-21 16:57:10 +00:00
|
|
|
MP_ERR(ctx, "libswscale initialization failed.\n");
|
2013-07-18 11:17:56 +00:00
|
|
|
return r;
|
|
|
|
}
|
2012-10-24 17:11:42 +00:00
|
|
|
|
2019-10-19 23:57:47 +00:00
|
|
|
#if HAVE_ZIMG
|
|
|
|
if (ctx->zimg_ok)
|
|
|
|
return mp_zimg_convert(ctx->zimg, dst, src) ? 0 : -1;
|
|
|
|
#endif
|
|
|
|
|
2020-09-17 13:20:08 +00:00
|
|
|
struct mp_image *a_src = check_alignment(ctx->log, &ctx->aligned_src, src);
|
|
|
|
struct mp_image *a_dst = check_alignment(ctx->log, &ctx->aligned_dst, dst);
|
|
|
|
if (!a_src || !a_dst) {
|
|
|
|
MP_ERR(ctx, "image allocation failed.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (a_src != src)
|
|
|
|
mp_image_copy(a_src, src);
|
|
|
|
|
|
|
|
sws_scale(ctx->sws, (const uint8_t *const *) a_src->planes, a_src->stride,
|
|
|
|
0, a_src->h, a_dst->planes, a_dst->stride);
|
|
|
|
|
|
|
|
if (a_dst != dst)
|
|
|
|
mp_image_copy(dst, a_dst);
|
|
|
|
|
2013-07-18 11:17:56 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:10:08 +00:00
|
|
|
int mp_image_swscale(struct mp_image *dst, struct mp_image *src,
|
|
|
|
int my_sws_flags)
|
2013-07-18 11:17:56 +00:00
|
|
|
{
|
|
|
|
struct mp_sws_context *ctx = mp_sws_alloc(NULL);
|
|
|
|
ctx->flags = my_sws_flags;
|
2015-01-15 19:10:08 +00:00
|
|
|
int res = mp_sws_scale(ctx, dst, src);
|
2013-07-18 11:17:56 +00:00
|
|
|
talloc_free(ctx);
|
2015-01-15 19:10:08 +00:00
|
|
|
return res;
|
2012-10-24 17:11:42 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 19:10:08 +00:00
|
|
|
int mp_image_sw_blur_scale(struct mp_image *dst, struct mp_image *src,
|
|
|
|
float gblur)
|
2012-11-24 23:06:16 +00:00
|
|
|
{
|
2013-07-18 11:17:56 +00:00
|
|
|
struct mp_sws_context *ctx = mp_sws_alloc(NULL);
|
sws_utils, zimg: destroy vo_x11 and vo_drm performance
Raise swscale and zimg default parameters. This restores screenshot
quality settings (maybe) unset in the commit before. Also expose some
more libswscale and zimg options.
Since these options are also used for VOs like x11 and drm, this will
make x11/drm/etc. much slower. For compensation, provide a profile that
sets the old option values: sw-fast. I'm also enabling zimg here, just
as an experiment.
The core problem is that we have a single set of command line options
which control the settings used for most swscale/zimg uses. This was
done in the previous commit. It cannot differentiate between the VOs,
which need to be realtime and may accept/require lower quality options,
and things like screenshots or vo_image, which can be slower, but should
not sacrifice quality by default.
Should this have two sets of options or something similar to do the
right thing depending on the code which calls libswscale? Maybe. Or
should I just ignore the problem, make it someone else's problem (users
who want to use software conversion VOs), provide a sub-optimal
solution, and call it a day? Definitely, sounds good, pushing to master,
goodbye.
2019-10-31 15:45:28 +00:00
|
|
|
ctx->flags = SWS_LANCZOS | mp_sws_hq_flags;
|
2013-07-18 11:17:56 +00:00
|
|
|
ctx->src_filter = sws_getDefaultFilter(gblur, gblur, 0, 0, 0, 0, 0);
|
|
|
|
ctx->force_reload = true;
|
2015-01-15 19:10:08 +00:00
|
|
|
int res = mp_sws_scale(ctx, dst, src);
|
2013-07-18 11:17:56 +00:00
|
|
|
talloc_free(ctx);
|
2015-01-15 19:10:08 +00:00
|
|
|
return res;
|
2012-11-24 23:06:16 +00:00
|
|
|
}
|
|
|
|
|
2015-02-06 22:20:41 +00:00
|
|
|
static const int endian_swaps[][2] = {
|
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
|
|
|
#if defined(AV_PIX_FMT_YA16) && defined(AV_PIX_FMT_RGBA64)
|
|
|
|
{AV_PIX_FMT_YA16BE, AV_PIX_FMT_YA16LE},
|
|
|
|
{AV_PIX_FMT_RGBA64BE, AV_PIX_FMT_RGBA64LE},
|
|
|
|
{AV_PIX_FMT_GRAY16BE, AV_PIX_FMT_GRAY16LE},
|
|
|
|
{AV_PIX_FMT_RGB48BE, AV_PIX_FMT_RGB48LE},
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
{AV_PIX_FMT_NONE, AV_PIX_FMT_NONE}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Swap _some_ non-native endian formats to native. We do this specifically
|
|
|
|
// for pixel formats used by PNG, to avoid going through libswscale, which
|
|
|
|
// might reduce the effective bit depth in some cases.
|
|
|
|
struct mp_image *mp_img_swap_to_native(struct mp_image *img)
|
|
|
|
{
|
2020-05-20 16:36:39 +00:00
|
|
|
int avfmt = imgfmt2pixfmt(img->imgfmt);
|
2015-02-06 22:20:41 +00:00
|
|
|
int to = AV_PIX_FMT_NONE;
|
|
|
|
for (int n = 0; endian_swaps[n][0] != AV_PIX_FMT_NONE; n++) {
|
2020-05-20 16:36:39 +00:00
|
|
|
if (endian_swaps[n][0] == avfmt)
|
2015-02-06 22:20:41 +00:00
|
|
|
to = endian_swaps[n][1];
|
|
|
|
}
|
|
|
|
if (to == AV_PIX_FMT_NONE || !mp_image_make_writeable(img))
|
|
|
|
return img;
|
2020-05-17 12:57:13 +00:00
|
|
|
int elems = img->fmt.bpp[0] / 8 / 2 * img->w;
|
2015-02-06 22:20:41 +00:00
|
|
|
for (int y = 0; y < img->h; y++) {
|
|
|
|
uint16_t *p = (uint16_t *)(img->planes[0] + y * img->stride[0]);
|
|
|
|
for (int i = 0; i < elems; i++)
|
|
|
|
p[i] = av_be2ne16(p[i]);
|
|
|
|
}
|
|
|
|
mp_image_setfmt(img, pixfmt2imgfmt(to));
|
|
|
|
return img;
|
|
|
|
}
|
|
|
|
|
2012-10-24 17:00:49 +00:00
|
|
|
// vim: ts=4 sw=4 et tw=80
|