2013-03-01 20:19:20 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
2016-01-19 17:36:34 +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.
|
2013-03-01 20:19:20 +00:00
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2016-01-19 17:36:34 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-03-01 20:19:20 +00:00
|
|
|
*
|
2016-01-19 17:36:34 +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/>.
|
2013-03-01 20:19:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
2016-04-15 11:58:41 +00:00
|
|
|
#include <math.h>
|
2013-03-01 20:19:20 +00:00
|
|
|
|
2016-01-11 18:03:40 +00:00
|
|
|
#include "mpv_talloc.h"
|
2013-03-01 20:19:20 +00:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "stream/stream.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/common.h"
|
2014-08-29 10:09:04 +00:00
|
|
|
#include "misc/bstr.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2013-12-17 01:02:25 +00:00
|
|
|
#include "options/m_option.h"
|
|
|
|
#include "options/path.h"
|
2016-06-03 18:06:29 +00:00
|
|
|
#include "video/csputils.h"
|
2015-08-29 02:12:56 +00:00
|
|
|
#include "lcms.h"
|
2013-03-01 20:19:20 +00:00
|
|
|
|
2015-05-02 14:35:53 +00:00
|
|
|
#include "osdep/io.h"
|
|
|
|
|
2013-07-16 11:28:28 +00:00
|
|
|
#if HAVE_LCMS2
|
2013-03-30 02:29:54 +00:00
|
|
|
|
|
|
|
#include <lcms2.h>
|
2015-04-29 16:09:22 +00:00
|
|
|
#include <libavutil/sha.h>
|
|
|
|
#include <libavutil/mem.h>
|
2013-03-30 02:29:54 +00:00
|
|
|
|
2015-01-07 17:47:27 +00:00
|
|
|
struct gl_lcms {
|
|
|
|
void *icc_data;
|
|
|
|
size_t icc_size;
|
2017-07-25 21:17:04 +00:00
|
|
|
struct AVBufferRef *vid_profile;
|
2016-09-06 09:11:36 +00:00
|
|
|
char *current_profile;
|
2016-06-04 15:52:10 +00:00
|
|
|
bool using_memory_profile;
|
2015-01-08 00:00:42 +00:00
|
|
|
bool changed;
|
2016-09-06 09:11:36 +00:00
|
|
|
enum mp_csp_prim current_prim;
|
|
|
|
enum mp_csp_trc current_trc;
|
2015-01-07 17:47:27 +00:00
|
|
|
|
|
|
|
struct mp_log *log;
|
|
|
|
struct mpv_global *global;
|
2016-06-04 15:52:10 +00:00
|
|
|
struct mp_icc_opts *opts;
|
2015-01-07 17:47:27 +00:00
|
|
|
};
|
|
|
|
|
2013-03-01 20:19:20 +00:00
|
|
|
static void lcms2_error_handler(cmsContext ctx, cmsUInt32Number code,
|
|
|
|
const char *msg)
|
|
|
|
{
|
2015-01-07 17:47:27 +00:00
|
|
|
struct gl_lcms *p = cmsGetContextUserData(ctx);
|
|
|
|
MP_ERR(p, "lcms2: %s\n", msg);
|
2013-03-01 20:19:20 +00:00
|
|
|
}
|
|
|
|
|
2016-06-04 15:52:10 +00:00
|
|
|
static void load_profile(struct gl_lcms *p)
|
2014-02-24 23:04:30 +00:00
|
|
|
{
|
2016-06-04 15:52:10 +00:00
|
|
|
talloc_free(p->icc_data);
|
|
|
|
p->icc_data = NULL;
|
|
|
|
p->icc_size = 0;
|
|
|
|
p->using_memory_profile = false;
|
2016-09-06 09:11:36 +00:00
|
|
|
talloc_free(p->current_profile);
|
|
|
|
p->current_profile = NULL;
|
2015-01-07 17:47:27 +00:00
|
|
|
|
2016-06-04 15:52:10 +00:00
|
|
|
if (!p->opts->profile || !p->opts->profile[0])
|
|
|
|
return;
|
2015-01-07 17:47:27 +00:00
|
|
|
|
2016-06-04 15:52:10 +00:00
|
|
|
char *fname = mp_get_user_path(NULL, p->global, p->opts->profile);
|
2015-05-03 12:58:18 +00:00
|
|
|
MP_VERBOSE(p, "Opening ICC profile '%s'\n", fname);
|
2015-03-27 12:27:40 +00:00
|
|
|
struct bstr iccdata = stream_read_file(fname, p, p->global,
|
|
|
|
100000000); // 100 MB
|
2015-05-03 12:58:18 +00:00
|
|
|
talloc_free(fname);
|
2015-01-07 17:47:27 +00:00
|
|
|
if (!iccdata.len)
|
2016-06-04 15:52:10 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
talloc_free(p->icc_data);
|
2015-01-07 17:47:27 +00:00
|
|
|
|
|
|
|
p->icc_data = iccdata.start;
|
|
|
|
p->icc_size = iccdata.len;
|
2016-09-06 09:11:36 +00:00
|
|
|
p->current_profile = talloc_strdup(p, p->opts->profile);
|
2015-01-07 17:47:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 21:17:04 +00:00
|
|
|
static void gl_lcms_destructor(void *ptr)
|
|
|
|
{
|
|
|
|
struct gl_lcms *p = ptr;
|
|
|
|
av_buffer_unref(&p->vid_profile);
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:47:27 +00:00
|
|
|
struct gl_lcms *gl_lcms_init(void *talloc_ctx, struct mp_log *log,
|
2016-09-06 09:11:36 +00:00
|
|
|
struct mpv_global *global,
|
|
|
|
struct mp_icc_opts *opts)
|
2015-01-07 17:47:27 +00:00
|
|
|
{
|
|
|
|
struct gl_lcms *p = talloc_ptrtype(talloc_ctx, p);
|
2017-07-25 21:17:04 +00:00
|
|
|
talloc_set_destructor(p, gl_lcms_destructor);
|
2015-01-07 17:47:27 +00:00
|
|
|
*p = (struct gl_lcms) {
|
|
|
|
.global = global,
|
|
|
|
.log = log,
|
2016-09-06 09:11:36 +00:00
|
|
|
.opts = opts,
|
2015-01-07 17:47:27 +00:00
|
|
|
};
|
2016-09-06 09:11:36 +00:00
|
|
|
gl_lcms_update_options(p);
|
2015-01-07 17:47:27 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2016-09-06 09:11:36 +00:00
|
|
|
void gl_lcms_update_options(struct gl_lcms *p)
|
2015-01-07 17:47:27 +00:00
|
|
|
{
|
2016-06-04 15:52:10 +00:00
|
|
|
if ((p->using_memory_profile && !p->opts->profile_auto) ||
|
2016-09-06 09:11:36 +00:00
|
|
|
!bstr_equals(bstr0(p->opts->profile), bstr0(p->current_profile)))
|
2016-06-04 15:52:10 +00:00
|
|
|
{
|
|
|
|
load_profile(p);
|
|
|
|
}
|
|
|
|
|
2015-01-08 00:00:42 +00:00
|
|
|
p->changed = true; // probably
|
2015-01-07 17:47:27 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 23:55:57 +00:00
|
|
|
// Warning: profile.start must point to a ta allocation, and the function
|
|
|
|
// takes over ownership.
|
2016-06-04 15:52:10 +00:00
|
|
|
// Returns whether the internal profile was changed.
|
|
|
|
bool gl_lcms_set_memory_profile(struct gl_lcms *p, bstr profile)
|
2015-01-07 17:47:27 +00:00
|
|
|
{
|
2016-06-04 15:52:10 +00:00
|
|
|
if (!p->opts->profile_auto || (p->opts->profile && p->opts->profile[0])) {
|
2016-06-04 12:50:32 +00:00
|
|
|
talloc_free(profile.start);
|
2016-06-04 15:52:10 +00:00
|
|
|
return false;
|
2015-01-26 01:11:14 +00:00
|
|
|
}
|
2015-01-07 17:47:27 +00:00
|
|
|
|
2016-06-04 15:52:10 +00:00
|
|
|
if (p->using_memory_profile &&
|
|
|
|
p->icc_data && profile.start &&
|
2016-06-04 12:50:32 +00:00
|
|
|
profile.len == p->icc_size &&
|
|
|
|
memcmp(profile.start, p->icc_data, p->icc_size) == 0)
|
2015-01-08 00:00:42 +00:00
|
|
|
{
|
2016-06-04 12:50:32 +00:00
|
|
|
talloc_free(profile.start);
|
2016-06-04 15:52:10 +00:00
|
|
|
return false;
|
2015-01-08 00:00:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p->changed = true;
|
2016-06-04 15:52:10 +00:00
|
|
|
p->using_memory_profile = true;
|
2015-01-07 17:47:27 +00:00
|
|
|
|
2015-01-07 23:55:57 +00:00
|
|
|
talloc_free(p->icc_data);
|
2015-01-07 17:47:27 +00:00
|
|
|
|
2016-06-04 12:50:32 +00:00
|
|
|
p->icc_data = talloc_steal(p, profile.start);
|
|
|
|
p->icc_size = profile.len;
|
2016-06-04 15:52:10 +00:00
|
|
|
|
|
|
|
return true;
|
2014-02-24 23:04:30 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 21:17:04 +00:00
|
|
|
// Guards against NULL and uses bstr_equals to short-circuit some special cases
|
|
|
|
static bool vid_profile_eq(struct AVBufferRef *a, struct AVBufferRef *b)
|
|
|
|
{
|
|
|
|
if (!a || !b)
|
|
|
|
return a == b;
|
|
|
|
|
|
|
|
return bstr_equals((struct bstr){ a->data, a->size },
|
|
|
|
(struct bstr){ b->data, b->size });
|
|
|
|
}
|
|
|
|
|
2016-09-06 09:11:36 +00:00
|
|
|
// Return whether the profile or config has changed since the last time it was
|
|
|
|
// retrieved. If it has changed, gl_lcms_get_lut3d() should be called.
|
2016-02-13 14:33:00 +00:00
|
|
|
bool gl_lcms_has_changed(struct gl_lcms *p, enum mp_csp_prim prim,
|
2017-07-25 21:17:04 +00:00
|
|
|
enum mp_csp_trc trc, struct AVBufferRef *vid_profile)
|
2015-01-08 00:00:42 +00:00
|
|
|
{
|
2017-07-25 21:17:04 +00:00
|
|
|
if (p->changed || p->current_prim != prim || p->current_trc != trc)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return !vid_profile_eq(p->vid_profile, vid_profile);
|
2015-01-08 00:00:42 +00:00
|
|
|
}
|
|
|
|
|
2016-06-04 15:52:10 +00:00
|
|
|
// Whether a profile is set. (gl_lcms_get_lut3d() is expected to return a lut,
|
|
|
|
// but it could still fail due to runtime errors, such as invalid icc data.)
|
|
|
|
bool gl_lcms_has_profile(struct gl_lcms *p)
|
|
|
|
{
|
|
|
|
return p->icc_size > 0;
|
|
|
|
}
|
|
|
|
|
2016-05-03 19:58:03 +00:00
|
|
|
static cmsHPROFILE get_vid_profile(struct gl_lcms *p, cmsContext cms,
|
|
|
|
cmsHPROFILE disp_profile,
|
2016-02-13 14:33:00 +00:00
|
|
|
enum mp_csp_prim prim, enum mp_csp_trc trc)
|
|
|
|
{
|
2017-07-25 21:17:04 +00:00
|
|
|
if (p->opts->use_embedded && p->vid_profile) {
|
|
|
|
// Try using the embedded ICC profile
|
|
|
|
cmsHPROFILE prof = cmsOpenProfileFromMemTHR(cms, p->vid_profile->data,
|
|
|
|
p->vid_profile->size);
|
|
|
|
if (prof) {
|
2017-08-04 08:30:40 +00:00
|
|
|
MP_VERBOSE(p, "Successfully opened embedded ICC profile\n");
|
2017-07-25 21:17:04 +00:00
|
|
|
return prof;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, warn the user and generate the profile as usual
|
2017-10-23 08:53:28 +00:00
|
|
|
MP_WARN(p, "Video contained an invalid ICC profile! Ignoring...\n");
|
2017-07-25 21:17:04 +00:00
|
|
|
}
|
|
|
|
|
2016-02-13 14:33:00 +00:00
|
|
|
// The input profile for the transformation is dependent on the video
|
|
|
|
// primaries and transfer characteristics
|
|
|
|
struct mp_csp_primaries csp = mp_get_csp_primaries(prim);
|
|
|
|
cmsCIExyY wp_xyY = {csp.white.x, csp.white.y, 1.0};
|
|
|
|
cmsCIExyYTRIPLE prim_xyY = {
|
|
|
|
.Red = {csp.red.x, csp.red.y, 1.0},
|
|
|
|
.Green = {csp.green.x, csp.green.y, 1.0},
|
|
|
|
.Blue = {csp.blue.x, csp.blue.y, 1.0},
|
|
|
|
};
|
|
|
|
|
2016-03-29 16:58:58 +00:00
|
|
|
cmsToneCurve *tonecurve[3] = {0};
|
2016-02-13 14:33:00 +00:00
|
|
|
switch (trc) {
|
2016-03-29 16:58:58 +00:00
|
|
|
case MP_CSP_TRC_LINEAR: tonecurve[0] = cmsBuildGamma(cms, 1.0); break;
|
|
|
|
case MP_CSP_TRC_GAMMA18: tonecurve[0] = cmsBuildGamma(cms, 1.8); break;
|
2018-08-22 12:39:40 +00:00
|
|
|
case MP_CSP_TRC_GAMMA20: tonecurve[0] = cmsBuildGamma(cms, 2.0); break;
|
2016-03-29 16:58:58 +00:00
|
|
|
case MP_CSP_TRC_GAMMA22: tonecurve[0] = cmsBuildGamma(cms, 2.2); break;
|
2018-08-22 12:39:40 +00:00
|
|
|
case MP_CSP_TRC_GAMMA24: tonecurve[0] = cmsBuildGamma(cms, 2.4); break;
|
|
|
|
case MP_CSP_TRC_GAMMA26: tonecurve[0] = cmsBuildGamma(cms, 2.6); break;
|
2016-03-29 16:58:58 +00:00
|
|
|
case MP_CSP_TRC_GAMMA28: tonecurve[0] = cmsBuildGamma(cms, 2.8); break;
|
2016-02-13 14:33:00 +00:00
|
|
|
|
|
|
|
case MP_CSP_TRC_SRGB:
|
|
|
|
// Values copied from Little-CMS
|
2016-03-29 16:58:58 +00:00
|
|
|
tonecurve[0] = cmsBuildParametricToneCurve(cms, 4,
|
2016-02-13 14:33:00 +00:00
|
|
|
(double[5]){2.40, 1/1.055, 0.055/1.055, 1/12.92, 0.04045});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_CSP_TRC_PRO_PHOTO:
|
2016-03-29 16:58:58 +00:00
|
|
|
tonecurve[0] = cmsBuildParametricToneCurve(cms, 4,
|
2016-02-13 14:33:00 +00:00
|
|
|
(double[5]){1.8, 1.0, 0.0, 1/16.0, 0.03125});
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MP_CSP_TRC_BT_1886: {
|
2016-03-29 16:58:58 +00:00
|
|
|
double src_black[3];
|
2021-04-26 16:31:16 +00:00
|
|
|
if (p->opts->contrast < 0) {
|
|
|
|
// User requested infinite contrast, return 2.4 profile
|
|
|
|
tonecurve[0] = cmsBuildGamma(cms, 2.4);
|
|
|
|
break;
|
|
|
|
} else if (p->opts->contrast > 0) {
|
|
|
|
MP_VERBOSE(p, "Using specified contrast: %d\n", p->opts->contrast);
|
2016-05-03 19:58:03 +00:00
|
|
|
for (int i = 0; i < 3; i++)
|
2021-04-26 16:31:16 +00:00
|
|
|
src_black[i] = 1.0 / p->opts->contrast;
|
|
|
|
} else {
|
|
|
|
// To build an appropriate BT.1886 transformation we need access to
|
|
|
|
// the display's black point, so we use LittleCMS' detection
|
|
|
|
// function. Relative colorimetric is used since we want to
|
|
|
|
// approximate the BT.1886 to the target device's actual black
|
|
|
|
// point even in e.g. perceptual mode
|
|
|
|
const int intent = MP_INTENT_RELATIVE_COLORIMETRIC;
|
|
|
|
cmsCIEXYZ bp_XYZ;
|
|
|
|
if (!cmsDetectBlackPoint(&bp_XYZ, disp_profile, intent, 0))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Map this XYZ value back into the (linear) source space
|
|
|
|
cmsHPROFILE rev_profile;
|
|
|
|
cmsToneCurve *linear = cmsBuildGamma(cms, 1.0);
|
|
|
|
rev_profile = cmsCreateRGBProfileTHR(cms, &wp_xyY, &prim_xyY,
|
|
|
|
(cmsToneCurve*[3]){linear, linear, linear});
|
|
|
|
cmsHPROFILE xyz_profile = cmsCreateXYZProfile();
|
|
|
|
cmsHTRANSFORM xyz2src = cmsCreateTransformTHR(cms,
|
|
|
|
xyz_profile, TYPE_XYZ_DBL, rev_profile, TYPE_RGB_DBL,
|
2022-07-11 11:26:27 +00:00
|
|
|
intent, cmsFLAGS_NOCACHE | cmsFLAGS_NOOPTIMIZE);
|
2021-04-26 16:31:16 +00:00
|
|
|
cmsFreeToneCurve(linear);
|
|
|
|
cmsCloseProfile(rev_profile);
|
|
|
|
cmsCloseProfile(xyz_profile);
|
|
|
|
if (!xyz2src)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
cmsDoTransform(xyz2src, &bp_XYZ, src_black, 1);
|
|
|
|
cmsDeleteTransform(xyz2src);
|
|
|
|
|
|
|
|
double contrast = 3.0 / (src_black[0] + src_black[1] + src_black[2]);
|
|
|
|
MP_VERBOSE(p, "Detected ICC profile contrast: %f\n", contrast);
|
2016-05-03 19:58:03 +00:00
|
|
|
}
|
2016-03-29 16:58:58 +00:00
|
|
|
|
|
|
|
// Build the parametric BT.1886 transfer curve, one per channel
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
const double gamma = 2.40;
|
|
|
|
double binv = pow(src_black[i], 1.0/gamma);
|
|
|
|
tonecurve[i] = cmsBuildParametricToneCurve(cms, 6,
|
|
|
|
(double[4]){gamma, 1.0 - binv, binv, 0.0});
|
|
|
|
}
|
2016-02-13 14:33:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2016-03-29 16:58:58 +00:00
|
|
|
if (!tonecurve[0])
|
2016-02-13 14:33:00 +00:00
|
|
|
return false;
|
|
|
|
|
2016-03-29 16:58:58 +00:00
|
|
|
if (!tonecurve[1]) tonecurve[1] = tonecurve[0];
|
|
|
|
if (!tonecurve[2]) tonecurve[2] = tonecurve[0];
|
|
|
|
|
2016-02-13 14:33:00 +00:00
|
|
|
cmsHPROFILE *vid_profile = cmsCreateRGBProfileTHR(cms, &wp_xyY, &prim_xyY,
|
2016-03-29 16:58:58 +00:00
|
|
|
tonecurve);
|
|
|
|
|
|
|
|
if (tonecurve[2] != tonecurve[0]) cmsFreeToneCurve(tonecurve[2]);
|
|
|
|
if (tonecurve[1] != tonecurve[0]) cmsFreeToneCurve(tonecurve[1]);
|
|
|
|
cmsFreeToneCurve(tonecurve[0]);
|
2016-02-13 14:33:00 +00:00
|
|
|
|
|
|
|
return vid_profile;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool gl_lcms_get_lut3d(struct gl_lcms *p, struct lut3d **result_lut3d,
|
2017-07-25 21:17:04 +00:00
|
|
|
enum mp_csp_prim prim, enum mp_csp_trc trc,
|
|
|
|
struct AVBufferRef *vid_profile)
|
2013-03-01 20:19:20 +00:00
|
|
|
{
|
|
|
|
int s_r, s_g, s_b;
|
2015-01-07 17:47:27 +00:00
|
|
|
bool result = false;
|
2013-03-01 20:19:20 +00:00
|
|
|
|
2016-09-06 09:11:36 +00:00
|
|
|
p->changed = false;
|
|
|
|
p->current_prim = prim;
|
|
|
|
p->current_trc = trc;
|
|
|
|
|
2017-07-25 21:17:04 +00:00
|
|
|
// We need to hold on to a reference to the video's ICC profile for as long
|
|
|
|
// as we still need to perform equality checking, so generate a new
|
|
|
|
// reference here
|
|
|
|
av_buffer_unref(&p->vid_profile);
|
|
|
|
if (vid_profile) {
|
2017-08-04 08:30:40 +00:00
|
|
|
MP_VERBOSE(p, "Got an embedded ICC profile.\n");
|
2017-07-25 21:17:04 +00:00
|
|
|
p->vid_profile = av_buffer_ref(vid_profile);
|
2023-01-10 17:59:21 +00:00
|
|
|
MP_HANDLE_OOM(p->vid_profile);
|
2017-07-25 21:17:04 +00:00
|
|
|
}
|
|
|
|
|
2021-04-18 09:48:35 +00:00
|
|
|
if (!gl_parse_3dlut_size(p->opts->size_str, &s_r, &s_g, &s_b))
|
2015-01-07 17:47:27 +00:00
|
|
|
return false;
|
|
|
|
|
2016-06-04 15:52:10 +00:00
|
|
|
if (!gl_lcms_has_profile(p))
|
2015-01-07 17:47:27 +00:00
|
|
|
return false;
|
2013-03-01 20:19:20 +00:00
|
|
|
|
|
|
|
void *tmp = talloc_new(NULL);
|
2017-08-27 11:40:59 +00:00
|
|
|
uint16_t *output = talloc_array(tmp, uint16_t, s_r * s_g * s_b * 4);
|
2013-12-21 16:55:19 +00:00
|
|
|
struct lut3d *lut = NULL;
|
2014-06-16 14:57:19 +00:00
|
|
|
cmsContext cms = NULL;
|
2013-03-01 20:19:20 +00:00
|
|
|
|
2015-04-29 16:09:22 +00:00
|
|
|
char *cache_file = NULL;
|
2016-06-04 15:52:10 +00:00
|
|
|
if (p->opts->cache_dir && p->opts->cache_dir[0]) {
|
vo_opengl: Simplify and clarify color correction code
This commit:
- Changes some of the #define and variable names for clarification and
adds comments where appropriate.
- Unifies :srgb and :icc-profile, making them fit into the same step of
the decoding process and removing the weird interactions between both
of them.
- Makes :icc-profile take precedence over :srgb (to significantly reduce
the number of confusing and useless special cases)
- Moves BT709 decompanding (approximate or actual) to the shader in all
cases, making it happen before upscaling (instead of the old 0.45
gamma function). This is the simpler and more proper way to do it.
- Enables the approx gamma function to work with :srgb as well due to
this (since they now share the gamma expansion code).
- Renames :icc-approx-gamma to :approx-gamma since it is no longer tied
to the ICC options or LittleCMS.
- Uses gamma 2.4 as input space for the actual 3DLUT, this is now a
pretty arbitrary factor but I picked 2.4 mainly because a higher pure
power value here seems to produce visually better results with wide
gamut profiles, rather then the previous 1.95 or BT.709.
- Adds the input gamma space to the 3dlut cache header in case we change
it more in the future, or even make it user customizable (though I
don't see why the latter would really be necessary).
- Fixes the OSD's gamma when using :srgb, which was previously still
using the old (0.45) approximation in all cases.
- Updates documentation on :srgb, it was still mentioning the old
behavior from circa a year ago.
This commit should serve to both open up and make the CMS/shader code much
more accessible and less confusing/error-prone and simultaneously also
improve the performance of 3DLUTs with wide gamut color spaces.
I would liked to have made it more modular but almost all of these
changes are interdependent, save for the documentation updates.
Note: Right now, the "3DLUT takes precedence over SRGB" logic is just
coded into gl_lcms.c's compile_shaders function. Ideally, this should be
done earlier, when parsing the options (by overriding the actual
opts.srgb flag) and output a warning to the user.
Note: I'm not sure how well this works together with real-world
subtitles that may need to be color corrected as well. I'm not sure
whether :approx-gamma needs to apply to subtitles as well. I'll need to
test this on proper files later.
Note: As of now, linear light scaling is still intrinsically tied to
either :srgb or :icc-profile. It would be thinkable to have this as an
extra option, :linear-scaling or similar, that could be used with or
without the two color management options.
2014-03-05 02:56:30 +00:00
|
|
|
// Gamma is included in the header to help uniquely identify it,
|
|
|
|
// because we may change the parameter in the future or make it
|
2014-03-26 00:46:38 +00:00
|
|
|
// customizable, same for the primaries.
|
2015-04-29 16:09:22 +00:00
|
|
|
char *cache_info = talloc_asprintf(tmp,
|
2017-08-27 11:40:59 +00:00
|
|
|
"ver=1.4, intent=%d, size=%dx%dx%d, prim=%d, trc=%d, "
|
2016-05-03 19:58:03 +00:00
|
|
|
"contrast=%d\n",
|
2016-06-04 15:52:10 +00:00
|
|
|
p->opts->intent, s_r, s_g, s_b, prim, trc, p->opts->contrast);
|
2015-04-29 16:09:22 +00:00
|
|
|
|
|
|
|
uint8_t hash[32];
|
|
|
|
struct AVSHA *sha = av_sha_alloc();
|
2023-01-10 17:59:21 +00:00
|
|
|
MP_HANDLE_OOM(sha);
|
2015-04-29 16:09:22 +00:00
|
|
|
av_sha_init(sha, 256);
|
|
|
|
av_sha_update(sha, cache_info, strlen(cache_info));
|
2017-07-25 21:17:04 +00:00
|
|
|
if (vid_profile)
|
|
|
|
av_sha_update(sha, vid_profile->data, vid_profile->size);
|
2015-04-29 16:09:22 +00:00
|
|
|
av_sha_update(sha, p->icc_data, p->icc_size);
|
|
|
|
av_sha_final(sha, hash);
|
|
|
|
av_free(sha);
|
|
|
|
|
2016-06-04 15:52:10 +00:00
|
|
|
char *cache_dir = mp_get_user_path(tmp, p->global, p->opts->cache_dir);
|
2015-05-02 14:29:30 +00:00
|
|
|
cache_file = talloc_strdup(tmp, "");
|
2015-04-29 16:09:22 +00:00
|
|
|
for (int i = 0; i < sizeof(hash); i++)
|
|
|
|
cache_file = talloc_asprintf_append(cache_file, "%02X", hash[i]);
|
2015-05-09 13:26:47 +00:00
|
|
|
cache_file = mp_path_join(tmp, cache_dir, cache_file);
|
2015-05-02 14:36:31 +00:00
|
|
|
|
2015-05-03 12:58:18 +00:00
|
|
|
mp_mkdirp(cache_dir);
|
2015-04-29 16:09:22 +00:00
|
|
|
}
|
2013-03-01 20:19:20 +00:00
|
|
|
|
|
|
|
// check cache
|
2016-05-03 22:19:07 +00:00
|
|
|
if (cache_file && stat(cache_file, &(struct stat){0}) == 0) {
|
2015-04-29 16:43:13 +00:00
|
|
|
MP_VERBOSE(p, "Opening 3D LUT cache in file '%s'.\n", cache_file);
|
2015-03-27 12:27:40 +00:00
|
|
|
struct bstr cachedata = stream_read_file(cache_file, tmp, p->global,
|
|
|
|
1000000000); // 1 GB
|
2015-04-29 16:09:22 +00:00
|
|
|
if (cachedata.len == talloc_get_size(output)) {
|
2013-03-01 20:19:20 +00:00
|
|
|
memcpy(output, cachedata.start, cachedata.len);
|
|
|
|
goto done;
|
|
|
|
} else {
|
2015-01-07 17:47:27 +00:00
|
|
|
MP_WARN(p, "3D LUT cache invalid!\n");
|
2013-03-01 20:19:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:47:27 +00:00
|
|
|
cms = cmsCreateContext(NULL, p);
|
2014-06-16 14:57:19 +00:00
|
|
|
if (!cms)
|
|
|
|
goto error_exit;
|
|
|
|
cmsSetLogErrorHandlerTHR(cms, lcms2_error_handler);
|
2013-03-01 20:19:20 +00:00
|
|
|
|
2015-01-07 17:47:27 +00:00
|
|
|
cmsHPROFILE profile =
|
|
|
|
cmsOpenProfileFromMemTHR(cms, p->icc_data, p->icc_size);
|
2013-03-01 20:19:20 +00:00
|
|
|
if (!profile)
|
|
|
|
goto error_exit;
|
|
|
|
|
2017-07-25 21:17:04 +00:00
|
|
|
cmsHPROFILE vid_hprofile = get_vid_profile(p, cms, profile, prim, trc);
|
|
|
|
if (!vid_hprofile) {
|
2016-02-13 14:33:00 +00:00
|
|
|
cmsCloseProfile(profile);
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2014-02-09 13:26:27 +00:00
|
|
|
|
2017-07-25 21:17:04 +00:00
|
|
|
cmsHTRANSFORM trafo = cmsCreateTransformTHR(cms, vid_hprofile, TYPE_RGB_16,
|
2017-08-27 11:40:59 +00:00
|
|
|
profile, TYPE_RGBA_16,
|
2016-06-04 15:52:10 +00:00
|
|
|
p->opts->intent,
|
2022-07-11 11:26:27 +00:00
|
|
|
cmsFLAGS_NOCACHE |
|
|
|
|
cmsFLAGS_NOOPTIMIZE |
|
2016-05-03 19:58:03 +00:00
|
|
|
cmsFLAGS_BLACKPOINTCOMPENSATION);
|
2013-03-01 20:19:20 +00:00
|
|
|
cmsCloseProfile(profile);
|
2017-07-25 21:17:04 +00:00
|
|
|
cmsCloseProfile(vid_hprofile);
|
2013-03-01 20:19:20 +00:00
|
|
|
|
|
|
|
if (!trafo)
|
|
|
|
goto error_exit;
|
|
|
|
|
|
|
|
// transform a (s_r)x(s_g)x(s_b) cube, with 3 components per channel
|
|
|
|
uint16_t *input = talloc_array(tmp, uint16_t, s_r * 3);
|
|
|
|
for (int b = 0; b < s_b; b++) {
|
|
|
|
for (int g = 0; g < s_g; g++) {
|
|
|
|
for (int r = 0; r < s_r; r++) {
|
|
|
|
input[r * 3 + 0] = r * 65535 / (s_r - 1);
|
|
|
|
input[r * 3 + 1] = g * 65535 / (s_g - 1);
|
|
|
|
input[r * 3 + 2] = b * 65535 / (s_b - 1);
|
|
|
|
}
|
2017-08-27 11:40:59 +00:00
|
|
|
size_t base = (b * s_r * s_g + g * s_r) * 4;
|
2013-03-01 20:19:20 +00:00
|
|
|
cmsDoTransform(trafo, input, output + base, s_r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cmsDeleteTransform(trafo);
|
|
|
|
|
2015-04-29 16:09:22 +00:00
|
|
|
if (cache_file) {
|
2015-05-03 12:58:18 +00:00
|
|
|
FILE *out = fopen(cache_file, "wb");
|
2013-03-01 20:19:20 +00:00
|
|
|
if (out) {
|
|
|
|
fwrite(output, talloc_get_size(output), 1, out);
|
|
|
|
fclose(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
done: ;
|
|
|
|
|
2013-12-21 16:55:19 +00:00
|
|
|
lut = talloc_ptrtype(NULL, lut);
|
2013-03-01 20:19:20 +00:00
|
|
|
*lut = (struct lut3d) {
|
|
|
|
.data = talloc_steal(lut, output),
|
|
|
|
.size = {s_r, s_g, s_b},
|
|
|
|
};
|
|
|
|
|
2015-01-07 17:47:27 +00:00
|
|
|
*result_lut3d = lut;
|
|
|
|
result = true;
|
|
|
|
|
2013-03-01 20:19:20 +00:00
|
|
|
error_exit:
|
2013-12-21 16:55:19 +00:00
|
|
|
|
2014-06-16 14:57:19 +00:00
|
|
|
if (cms)
|
|
|
|
cmsDeleteContext(cms);
|
2013-12-21 16:55:19 +00:00
|
|
|
|
|
|
|
if (!lut)
|
2015-01-07 17:47:27 +00:00
|
|
|
MP_FATAL(p, "Error loading ICC profile.\n");
|
2013-12-21 16:55:19 +00:00
|
|
|
|
2013-03-01 20:19:20 +00:00
|
|
|
talloc_free(tmp);
|
2015-01-07 17:47:27 +00:00
|
|
|
return result;
|
2013-03-01 20:19:20 +00:00
|
|
|
}
|
|
|
|
|
2013-07-16 11:28:28 +00:00
|
|
|
#else /* HAVE_LCMS2 */
|
2013-03-01 20:19:20 +00:00
|
|
|
|
2015-01-07 17:47:27 +00:00
|
|
|
struct gl_lcms *gl_lcms_init(void *talloc_ctx, struct mp_log *log,
|
2016-09-06 09:11:36 +00:00
|
|
|
struct mpv_global *global,
|
|
|
|
struct mp_icc_opts *opts)
|
2013-03-01 20:19:20 +00:00
|
|
|
{
|
2015-01-07 17:47:27 +00:00
|
|
|
return (struct gl_lcms *) talloc_new(talloc_ctx);
|
2013-03-01 20:19:20 +00:00
|
|
|
}
|
|
|
|
|
2016-09-06 09:11:36 +00:00
|
|
|
void gl_lcms_update_options(struct gl_lcms *p) { }
|
2016-06-04 15:52:10 +00:00
|
|
|
bool gl_lcms_set_memory_profile(struct gl_lcms *p, bstr profile) {return false;}
|
2016-02-13 14:33:00 +00:00
|
|
|
|
|
|
|
bool gl_lcms_has_changed(struct gl_lcms *p, enum mp_csp_prim prim,
|
2017-07-25 21:17:04 +00:00
|
|
|
enum mp_csp_trc trc, struct AVBufferRef *vid_profile)
|
2016-02-13 14:33:00 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-06-04 15:52:10 +00:00
|
|
|
bool gl_lcms_has_profile(struct gl_lcms *p)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-02-13 14:33:00 +00:00
|
|
|
bool gl_lcms_get_lut3d(struct gl_lcms *p, struct lut3d **result_lut3d,
|
2017-07-25 21:17:04 +00:00
|
|
|
enum mp_csp_prim prim, enum mp_csp_trc trc,
|
|
|
|
struct AVBufferRef *vid_profile)
|
2016-02-13 14:33:00 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2015-01-07 17:47:27 +00:00
|
|
|
|
2013-03-01 20:19:20 +00:00
|
|
|
#endif
|
2022-11-19 12:47:07 +00:00
|
|
|
|
|
|
|
static int validate_3dlut_size_opt(struct mp_log *log, const m_option_t *opt,
|
|
|
|
struct bstr name, const char **value)
|
|
|
|
{
|
|
|
|
int p1, p2, p3;
|
2022-11-21 16:38:25 +00:00
|
|
|
return gl_parse_3dlut_size(*value, &p1, &p2, &p3) ? 0 : M_OPT_INVALID;
|
2022-11-19 12:47:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define OPT_BASE_STRUCT struct mp_icc_opts
|
|
|
|
const struct m_sub_options mp_icc_conf = {
|
|
|
|
.opts = (const m_option_t[]) {
|
|
|
|
{"use-embedded-icc-profile", OPT_FLAG(use_embedded)},
|
|
|
|
{"icc-profile", OPT_STRING(profile), .flags = M_OPT_FILE},
|
|
|
|
{"icc-profile-auto", OPT_FLAG(profile_auto)},
|
|
|
|
{"icc-cache-dir", OPT_STRING(cache_dir), .flags = M_OPT_FILE},
|
|
|
|
{"icc-intent", OPT_INT(intent)},
|
|
|
|
{"icc-force-contrast", OPT_CHOICE(contrast, {"no", 0}, {"inf", -1}),
|
|
|
|
M_RANGE(0, 1000000)},
|
|
|
|
{"icc-3dlut-size", OPT_STRING_VALIDATE(size_str, validate_3dlut_size_opt)},
|
|
|
|
{"3dlut-size", OPT_REPLACED("icc-3dlut-size")},
|
|
|
|
{"icc-cache", OPT_REMOVED("see icc-cache-dir")},
|
|
|
|
{"icc-contrast", OPT_REMOVED("see icc-force-contrast")},
|
|
|
|
{0}
|
|
|
|
},
|
|
|
|
.size = sizeof(struct mp_icc_opts),
|
|
|
|
.defaults = &(const struct mp_icc_opts) {
|
|
|
|
.size_str = "64x64x64",
|
|
|
|
.intent = MP_INTENT_RELATIVE_COLORIMETRIC,
|
|
|
|
.use_embedded = true,
|
|
|
|
},
|
|
|
|
};
|