diff options
Diffstat (limited to 'video/out/opengl/lcms.c')
-rw-r--r-- | video/out/opengl/lcms.c | 153 |
1 files changed, 105 insertions, 48 deletions
diff --git a/video/out/opengl/lcms.c b/video/out/opengl/lcms.c index 7db8da6f70..eaeb86fa8d 100644 --- a/video/out/opengl/lcms.c +++ b/video/out/opengl/lcms.c @@ -16,6 +16,7 @@ */ #include <string.h> +#include <math.h> #include "mpv_talloc.h" @@ -25,10 +26,10 @@ #include "common/common.h" #include "misc/bstr.h" #include "common/msg.h" +#include "options/m_config.h" #include "options/m_option.h" #include "options/path.h" - -#include "video.h" +#include "video/csputils.h" #include "lcms.h" #include "osdep/io.h" @@ -42,14 +43,14 @@ struct gl_lcms { void *icc_data; size_t icc_size; - char *icc_path; + bool using_memory_profile; bool changed; enum mp_csp_prim prev_prim; enum mp_csp_trc prev_trc; struct mp_log *log; struct mpv_global *global; - struct mp_icc_opts opts; + struct mp_icc_opts *opts; }; static bool parse_3dlut_size(const char *arg, int *p1, int *p2, int *p3) @@ -80,6 +81,7 @@ const struct m_sub_options mp_icc_conf = { OPT_FLAG("icc-profile-auto", profile_auto, 0), OPT_STRING("icc-cache-dir", cache_dir, 0), OPT_INT("icc-intent", intent, 0), + OPT_INTRANGE("icc-contrast", contrast, 0, 0, 100000), OPT_STRING_VALIDATE("3dlut-size", size_str, 0, validate_3dlut_size_opt), OPT_REMOVED("icc-cache", "see icc-cache-dir"), @@ -99,25 +101,28 @@ static void lcms2_error_handler(cmsContext ctx, cmsUInt32Number code, MP_ERR(p, "lcms2: %s\n", msg); } -static bool load_profile(struct gl_lcms *p) +static void load_profile(struct gl_lcms *p) { - if (p->icc_data && p->icc_size) - return true; + talloc_free(p->icc_data); + p->icc_data = NULL; + p->icc_size = 0; + p->using_memory_profile = false; - if (!p->icc_path) - return false; + if (!p->opts->profile || !p->opts->profile[0]) + return; - char *fname = mp_get_user_path(NULL, p->global, p->icc_path); + char *fname = mp_get_user_path(NULL, p->global, p->opts->profile); MP_VERBOSE(p, "Opening ICC profile '%s'\n", fname); struct bstr iccdata = stream_read_file(fname, p, p->global, 100000000); // 100 MB talloc_free(fname); if (!iccdata.len) - return false; + return; + + talloc_free(p->icc_data); p->icc_data = iccdata.start; p->icc_size = iccdata.len; - return true; } struct gl_lcms *gl_lcms_init(void *talloc_ctx, struct mp_log *log, @@ -128,44 +133,55 @@ struct gl_lcms *gl_lcms_init(void *talloc_ctx, struct mp_log *log, .global = global, .log = log, .changed = true, + .opts = m_sub_options_copy(p, &mp_icc_conf, mp_icc_conf.defaults), }; return p; } void gl_lcms_set_options(struct gl_lcms *p, struct mp_icc_opts *opts) { - p->opts = *opts; - p->icc_path = talloc_strdup(p, p->opts.profile); - load_profile(p); + struct mp_icc_opts *old_opts = p->opts; + p->opts = m_sub_options_copy(p, &mp_icc_conf, opts); + + if ((p->using_memory_profile && !p->opts->profile_auto) || + !bstr_equals(bstr0(p->opts->profile), bstr0(old_opts->profile))) + { + load_profile(p); + } + p->changed = true; // probably + + talloc_free(old_opts); } // Warning: profile.start must point to a ta allocation, and the function // takes over ownership. -void gl_lcms_set_memory_profile(struct gl_lcms *p, bstr *profile) +// Returns whether the internal profile was changed. +bool gl_lcms_set_memory_profile(struct gl_lcms *p, bstr profile) { - if (!p->opts.profile_auto) { - talloc_free(profile->start); - return; + if (!p->opts->profile_auto || (p->opts->profile && p->opts->profile[0])) { + talloc_free(profile.start); + return false; } - if (!p->icc_path && p->icc_data && profile->start && - profile->len == p->icc_size && - memcmp(profile->start, p->icc_data, p->icc_size) == 0) + if (p->using_memory_profile && + p->icc_data && profile.start && + profile.len == p->icc_size && + memcmp(profile.start, p->icc_data, p->icc_size) == 0) { - talloc_free(profile->start); - return; + talloc_free(profile.start); + return false; } p->changed = true; - - talloc_free(p->icc_path); - p->icc_path = NULL; + p->using_memory_profile = true; talloc_free(p->icc_data); - p->icc_data = talloc_steal(p, profile->start); - p->icc_size = profile->len; + p->icc_data = talloc_steal(p, profile.start); + p->icc_size = profile.len; + + return true; } // Return and _reset_ whether the profile or config has changed since the last @@ -180,7 +196,15 @@ bool gl_lcms_has_changed(struct gl_lcms *p, enum mp_csp_prim prim, return change; } -static cmsHPROFILE get_vid_profile(cmsContext cms, cmsHPROFILE disp_profile, +// 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; +} + +static cmsHPROFILE get_vid_profile(struct gl_lcms *p, cmsContext cms, + cmsHPROFILE disp_profile, enum mp_csp_prim prim, enum mp_csp_trc trc) { // The input profile for the transformation is dependent on the video @@ -213,21 +237,47 @@ static cmsHPROFILE get_vid_profile(cmsContext cms, cmsHPROFILE disp_profile, case MP_CSP_TRC_BT_1886: { // To build an appropriate BT.1886 transformation we need access to - // the display's black point, so we use the reverse mappings + // the display's black point, so we 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 cmsToneCurve *linear = cmsBuildGamma(cms, 1.0); cmsHPROFILE rev_profile = cmsCreateRGBProfileTHR(cms, &wp_xyY, &prim_xyY, (cmsToneCurve*[3]){linear, linear, linear}); - cmsHTRANSFORM disp2src = cmsCreateTransformTHR(cms, - disp_profile, TYPE_RGB_16, rev_profile, TYPE_RGB_DBL, - INTENT_RELATIVE_COLORIMETRIC, 0); + cmsHPROFILE xyz_profile = cmsCreateXYZProfile(); + cmsHTRANSFORM xyz2src = cmsCreateTransformTHR(cms, + xyz_profile, TYPE_XYZ_DBL, rev_profile, TYPE_RGB_DBL, + intent, 0); cmsFreeToneCurve(linear); cmsCloseProfile(rev_profile); - if (!disp2src) + cmsCloseProfile(xyz_profile); + if (!xyz2src) return false; - uint64_t disp_black[3] = {0}; double src_black[3]; - cmsDoTransform(disp2src, disp_black, src_black, 1); + cmsDoTransform(xyz2src, &bp_XYZ, src_black, 1); + cmsDeleteTransform(xyz2src); + + // Contrast limiting + if (p->opts->contrast > 0) { + for (int i = 0; i < 3; i++) + src_black[i] = MPMAX(src_black[i], 1.0 / p->opts->contrast); + } + + // Built-in contrast failsafe + double contrast = 3.0 / (src_black[0] + src_black[1] + src_black[2]); + if (contrast > 100000) { + MP_WARN(p, "ICC profile detected contrast very high (>100000)," + " falling back to contrast 1000 for sanity. Set the" + " icc-contrast option to silence this warning.\n"); + src_black[0] = src_black[1] = src_black[2] = 1.0 / 1000; + } // Build the parametric BT.1886 transfer curve, one per channel for (int i = 0; i < 3; i++) { @@ -265,10 +315,10 @@ bool gl_lcms_get_lut3d(struct gl_lcms *p, struct lut3d **result_lut3d, int s_r, s_g, s_b; bool result = false; - if (!parse_3dlut_size(p->opts.size_str, &s_r, &s_g, &s_b)) + if (!parse_3dlut_size(p->opts->size_str, &s_r, &s_g, &s_b)) return false; - if (!p->icc_data && !p->icc_path) + if (!gl_lcms_has_profile(p)) return false; void *tmp = talloc_new(NULL); @@ -277,13 +327,14 @@ bool gl_lcms_get_lut3d(struct gl_lcms *p, struct lut3d **result_lut3d, cmsContext cms = NULL; char *cache_file = NULL; - if (p->opts.cache_dir && p->opts.cache_dir[0]) { + if (p->opts->cache_dir && p->opts->cache_dir[0]) { // Gamma is included in the header to help uniquely identify it, // because we may change the parameter in the future or make it // customizable, same for the primaries. char *cache_info = talloc_asprintf(tmp, - "ver=1.3, intent=%d, size=%dx%dx%d, prim=%d, trc=%d\n", - p->opts.intent, s_r, s_g, s_b, prim, trc); + "ver=1.3, intent=%d, size=%dx%dx%d, prim=%d, trc=%d, " + "contrast=%d\n", + p->opts->intent, s_r, s_g, s_b, prim, trc, p->opts->contrast); uint8_t hash[32]; struct AVSHA *sha = av_sha_alloc(); @@ -295,7 +346,7 @@ bool gl_lcms_get_lut3d(struct gl_lcms *p, struct lut3d **result_lut3d, av_sha_final(sha, hash); av_free(sha); - char *cache_dir = mp_get_user_path(tmp, p->global, p->opts.cache_dir); + char *cache_dir = mp_get_user_path(tmp, p->global, p->opts->cache_dir); cache_file = talloc_strdup(tmp, ""); for (int i = 0; i < sizeof(hash); i++) cache_file = talloc_asprintf_append(cache_file, "%02X", hash[i]); @@ -305,7 +356,7 @@ bool gl_lcms_get_lut3d(struct gl_lcms *p, struct lut3d **result_lut3d, } // check cache - if (cache_file) { + if (cache_file && stat(cache_file, &(struct stat){0}) == 0) { MP_VERBOSE(p, "Opening 3D LUT cache in file '%s'.\n", cache_file); struct bstr cachedata = stream_read_file(cache_file, tmp, p->global, 1000000000); // 1 GB @@ -327,7 +378,7 @@ bool gl_lcms_get_lut3d(struct gl_lcms *p, struct lut3d **result_lut3d, if (!profile) goto error_exit; - cmsHPROFILE vid_profile = get_vid_profile(cms, profile, prim, trc); + cmsHPROFILE vid_profile = get_vid_profile(p, cms, profile, prim, trc); if (!vid_profile) { cmsCloseProfile(profile); goto error_exit; @@ -335,8 +386,9 @@ bool gl_lcms_get_lut3d(struct gl_lcms *p, struct lut3d **result_lut3d, cmsHTRANSFORM trafo = cmsCreateTransformTHR(cms, vid_profile, TYPE_RGB_16, profile, TYPE_RGB_16, - p->opts.intent, - cmsFLAGS_HIGHRESPRECALC); + p->opts->intent, + cmsFLAGS_HIGHRESPRECALC | + cmsFLAGS_BLACKPOINTCOMPENSATION); cmsCloseProfile(profile); cmsCloseProfile(vid_profile); @@ -406,7 +458,7 @@ struct gl_lcms *gl_lcms_init(void *talloc_ctx, struct mp_log *log, } void gl_lcms_set_options(struct gl_lcms *p, struct mp_icc_opts *opts) { } -void gl_lcms_set_memory_profile(struct gl_lcms *p, bstr *profile) { } +bool gl_lcms_set_memory_profile(struct gl_lcms *p, bstr profile) {return false;} bool gl_lcms_has_changed(struct gl_lcms *p, enum mp_csp_prim prim, enum mp_csp_trc trc) @@ -414,6 +466,11 @@ bool gl_lcms_has_changed(struct gl_lcms *p, enum mp_csp_prim prim, return false; } +bool gl_lcms_has_profile(struct gl_lcms *p) +{ + return false; +} + bool gl_lcms_get_lut3d(struct gl_lcms *p, struct lut3d **result_lut3d, enum mp_csp_prim prim, enum mp_csp_trc trc) { |