From 45c3e0f0d0c836158ab38db53156bb6461ad7437 Mon Sep 17 00:00:00 2001 From: Niklas Haas Date: Mon, 30 May 2016 19:56:58 +0200 Subject: vo_opengl: refactor HDR mechanism Instead of doing HDR tone mapping on an ad-hoc basis inside pass_colormanage, the reference peak of an image is now part of the image params (alongside colorspace, gamma, etc.) and tone mapping is done whenever peak_src != peak_dst. To get sensible behavior when mixing HDR and SDR content and displays, target-brightness is a generic filler for "the assumed brightness of SDR content". This gets rid of the weird display_scaled hack, sets the framework for multiple HDR functions with difference reference peaks, and allows us to (in a future commit) autodetect the right source peak from the HDR metadata. (Apart from metadata, the source peak can also be controlled via vf_format. For HDR content this adjusts the overall image brightness, for SDR content it's like simulating a different exposure) --- video/out/opengl/video.c | 50 ++++++++++++++++++++++++++++------------ video/out/opengl/video_shaders.c | 17 +++++--------- video/out/opengl/video_shaders.h | 2 +- 3 files changed, 42 insertions(+), 27 deletions(-) (limited to 'video/out') diff --git a/video/out/opengl/video.c b/video/out/opengl/video.c index 380e768126..f0a2401172 100644 --- a/video/out/opengl/video.c +++ b/video/out/opengl/video.c @@ -2198,13 +2198,14 @@ static void pass_scale_main(struct gl_video *p) // Adapts the colors from the given color space to the display device's native // gamut. -static void pass_colormanage(struct gl_video *p, bool display_scaled, +static void pass_colormanage(struct gl_video *p, float peak_src, enum mp_csp_prim prim_src, enum mp_csp_trc trc_src) { GLSLF("// color management\n"); enum mp_csp_trc trc_dst = p->opts.target_trc; enum mp_csp_prim prim_dst = p->opts.target_prim; + float peak_dst = p->opts.target_brightness; if (p->use_lut_3d) { // The 3DLUT is always generated against the original source space @@ -2241,21 +2242,31 @@ static void pass_colormanage(struct gl_video *p, bool display_scaled, if (trc_dst == MP_CSP_TRC_LINEAR || trc_dst == MP_CSP_TRC_SMPTE_ST2084) trc_dst = MP_CSP_TRC_GAMMA22; } + if (!peak_src) { + // If the source has no information known, it's display-referred + // (and should be treated relative to the specified desired peak_dst) + peak_src = peak_dst; + } - bool need_gamma = trc_src != trc_dst || prim_src != prim_dst; + // All operations from here on require linear light as a starting point, + // so we linearize even if trc_src == trc_dst when one of the other + // operations needs it + bool need_gamma = trc_src != trc_dst || prim_src != prim_dst || + peak_src != peak_dst; if (need_gamma) pass_linearize(p->sc, trc_src); - // For HDR, the assumption of reference brightness = display brightness - // is discontinued. Instead, we have to tone map the brightness to - // the display using some algorithm. - if (p->image_params.gamma == MP_CSP_TRC_SMPTE_ST2084 && - trc_dst != MP_CSP_TRC_SMPTE_ST2084 && !display_scaled) + // Adapt and tone map for a different reference peak brightness + if (peak_src != peak_dst) { GLSLF("// HDR tone mapping\n"); - int reference_brightness = 10000; // As per SMPTE ST.2084 - pass_tone_map(p->sc, reference_brightness, p->opts.target_brightness, - p->opts.hdr_tone_mapping, p->opts.tone_mapping_param); + float rel_peak = peak_src / peak_dst; + // Normalize such that 1 is the target brightness (and values above + // 1 are out of range) + GLSLF("color.rgb *= vec3(%f);\n", rel_peak); + // Tone map back down to the range [0,1] + pass_tone_map(p->sc, rel_peak, p->opts.hdr_tone_mapping, + p->opts.tone_mapping_param); } // Adapt to the right colorspace if necessary @@ -2268,8 +2279,14 @@ static void pass_colormanage(struct gl_video *p, bool display_scaled, GLSL(color.rgb = cms_matrix * color.rgb;) } - if (need_gamma) + if (need_gamma) { + // If the target encoding function has a fixed peak, we need to + // un-normalize back to the encoding signal range + if (trc_dst == MP_CSP_TRC_SMPTE_ST2084) + GLSLF("color.rgb *= vec3(%f);\n", peak_dst / 10000); + pass_delinearize(p->sc, trc_dst); + } if (p->use_lut_3d) { gl_sc_uniform_sampler(p->sc, "lut_3d", GL_TEXTURE_3D, TEXUNIT_3DLUT); @@ -2410,9 +2427,12 @@ static void pass_draw_osd(struct gl_video *p, int draw_flags, double pts, default: abort(); } - // Subtitle color management, they're assumed to be sRGB by default - if (cms) - pass_colormanage(p, true, MP_CSP_PRIM_BT_709, MP_CSP_TRC_SRGB); + // Subtitle color management, they're assumed to be display-referred + // sRGB by default + if (cms) { + pass_colormanage(p, p->opts.target_brightness, + MP_CSP_PRIM_BT_709, MP_CSP_TRC_SRGB); + } gl_sc_set_vao(p->sc, mpgl_osd_get_vao(p->osd)); gl_sc_gen_shader_and_reset(p->sc); mpgl_osd_draw_part(p->osd, vp_w, vp_h, n); @@ -2533,7 +2553,7 @@ static void pass_draw_to_screen(struct gl_video *p, int fbo) GLSL(color.rgb = pow(color.rgb, vec3(user_gamma));) } - pass_colormanage(p, false, p->image_params.primaries, + pass_colormanage(p, p->image_params.peak, p->image_params.primaries, p->use_linear ? MP_CSP_TRC_LINEAR : p->image_params.gamma); // Draw checkerboard pattern to indicate transparency diff --git a/video/out/opengl/video_shaders.c b/video/out/opengl/video_shaders.c index d940c415e8..1f37f4fed1 100644 --- a/video/out/opengl/video_shaders.c +++ b/video/out/opengl/video_shaders.c @@ -313,15 +313,10 @@ void pass_delinearize(struct gl_shader_cache *sc, enum mp_csp_trc trc) } } -// Tone map from one brightness to another -void pass_tone_map(struct gl_shader_cache *sc, float peak_src, float peak_dst, +// Tone map from a known peak brightness to the range [0,1] +void pass_tone_map(struct gl_shader_cache *sc, float peak, enum tone_mapping algo, float param) { - // First we renormalize to the output range - float scale = peak_src / peak_dst; - GLSLF("color.rgb *= vec3(%f);\n", scale); - - // Then we use some algorithm to map back to [0,1] switch (algo) { case TONE_MAPPING_CLIP: GLSL(color.rgb = clamp(color.rgb, 0.0, 1.0);) @@ -331,7 +326,7 @@ void pass_tone_map(struct gl_shader_cache *sc, float peak_src, float peak_dst, float contrast = isnan(param) ? 0.5 : param, offset = (1.0 - contrast) / contrast; GLSLF("color.rgb = color.rgb / (color.rgb + vec3(%f));\n", offset); - GLSLF("color.rgb *= vec3(%f);\n", (scale + offset) / scale); + GLSLF("color.rgb *= vec3(%f);\n", (peak + offset) / peak); break; } @@ -342,20 +337,20 @@ void pass_tone_map(struct gl_shader_cache *sc, float peak_src, float peak_dst, A, C*B, D*E, A, B, D*F, E/F); GLSLHF("}\n"); - GLSLF("color.rgb = hable(color.rgb) / hable(vec3(%f));\n", scale); + GLSLF("color.rgb = hable(color.rgb) / hable(vec3(%f));\n", peak); break; } case TONE_MAPPING_GAMMA: { float gamma = isnan(param) ? 1.8 : param; GLSLF("color.rgb = pow(color.rgb / vec3(%f), vec3(%f));\n", - scale, 1.0/gamma); + peak, 1.0/gamma); break; } case TONE_MAPPING_LINEAR: { float coeff = isnan(param) ? 1.0 : param; - GLSLF("color.rgb = vec3(%f) * color.rgb;\n", coeff / scale); + GLSLF("color.rgb = vec3(%f) * color.rgb;\n", coeff / peak); break; } diff --git a/video/out/opengl/video_shaders.h b/video/out/opengl/video_shaders.h index e43efadeb4..0ee3d81fb5 100644 --- a/video/out/opengl/video_shaders.h +++ b/video/out/opengl/video_shaders.h @@ -38,7 +38,7 @@ void pass_sample_oversample(struct gl_shader_cache *sc, struct scaler *scaler, void pass_linearize(struct gl_shader_cache *sc, enum mp_csp_trc trc); void pass_delinearize(struct gl_shader_cache *sc, enum mp_csp_trc trc); -void pass_tone_map(struct gl_shader_cache *sc, float peak_src, float peak_dst, +void pass_tone_map(struct gl_shader_cache *sc, float peak, enum tone_mapping algo, float param); void pass_sample_deband(struct gl_shader_cache *sc, struct deband_opts *opts, -- cgit v1.2.3