| 1 |
/* |
| 2 |
* Copyright 2025 Google LLC. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "include/core/SkBitmap.h" |
| 9 |
#include "include/core/SkColorFilter.h" |
| 10 |
#include "include/effects/SkRuntimeEffect.h" |
| 11 |
#include "include/private/SkHdrMetadata.h" |
| 12 |
#include "src/codec/SkCodecPriv.h" |
| 13 |
#include "src/codec/SkHdrAgtmPriv.h" |
| 14 |
|
| 15 |
namespace { |
| 16 |
|
| 17 |
// The maximum and minimum HDR headroom values allowed by the specification. |
| 18 |
static constexpr float kMinHdrHeadroom = 0.f; |
| 19 |
static constexpr float kMaxHdrHeadroom = 6.f; |
| 20 |
|
| 21 |
// The maximum linear value is exp2(kMaxHdrHeadroom) = 64. |
| 22 |
static constexpr float kMaxLinearHdrHeadroom = 64.f; |
| 23 |
|
| 24 |
static bool in_unit_interval(float x) { |
| 25 |
return x >= 0.f && x <= 1.f; |
| 26 |
} |
| 27 |
|
| 28 |
// AGTM tone mapping shader. |
| 29 |
static constexpr char gAgtmSKSL[] = |
| 30 |
"uniform half scale_factor;" // The scale to apply in linear space |
| 31 |
"uniform shader curve_xym;" // The texture containing control points. |
| 32 |
"uniform half weight_i;" // The weight of gain curve "i" |
| 33 |
"uniform half4 mix_rgbx_i;" // The red,green,blue mixing coefficients. |
| 34 |
"uniform half4 mix_Mmcx_i;" // The max,min,component mixing coefficients. |
| 35 |
"uniform half curve_texcoord_y_i;" // The y texture coordinate at which to sample curve_xym. |
| 36 |
"uniform half curve_N_cp_i;" // The number of control points. |
| 37 |
"uniform half weight_j;" // All the same parameters, for gain curve "j" |
| 38 |
"uniform half4 mix_rgbx_j;" |
| 39 |
"uniform half4 mix_Mmcx_j;" |
| 40 |
"uniform half curve_texcoord_y_j;" |
| 41 |
"uniform half curve_N_cp_j;" |
| 42 |
|
| 43 |
// Shader equivalent of AgtmHelpers::EvaluateComponentMixingFunction. |
| 44 |
"half3 EvalComponentMixing(half3 color, half4 rgbx, half4 Mmcx) {" |
| 45 |
"half common = dot(rgbx.rgb, color) +" |
| 46 |
"Mmcx[0] * max(max(color.r, color.g), color.b) +" |
| 47 |
"Mmcx[1] * min(min(color.r, color.g), color.b);" |
| 48 |
"return Mmcx[2] * color + half3(common);" |
| 49 |
"}" |
| 50 |
|
| 51 |
// Shader equivalent of AgtmHelpers::EvaluateGainCurve. |
| 52 |
"half EvalGainCurve(half x, half curve_texcoord_y, half curve_N_cp) {" |
| 53 |
// Handle points to the left of the first control point. |
| 54 |
"half c_min = 0.0;" |
| 55 |
"half4 xym_min = curve_xym.eval(half2(c_min + 0.5, curve_texcoord_y));" |
| 56 |
"if (x <= xym_min.x) {" |
| 57 |
"return xym_min.y;" |
| 58 |
"}" |
| 59 |
// Handle points after the last control point. |
| 60 |
"half c_max = curve_N_cp - 1.0;" |
| 61 |
"half4 xym_max = curve_xym.eval(half2(c_max + 0.5, curve_texcoord_y));" |
| 62 |
"if (x >= xym_max.x) {" |
| 63 |
"return xym_max.y + log2(xym_max.x / x);" |
| 64 |
"}" |
| 65 |
// Binary search for the interval containing x. This will require sampling at most |
| 66 |
// log2(32)=5 more control points. |
| 67 |
"for (int step = 0; step < 5; ++step) {" |
| 68 |
// Early-out if we've already found the interval. |
| 69 |
"if (c_max - c_min <= 1.0) {" |
| 70 |
"break;" |
| 71 |
"}" |
| 72 |
// Test the midpoint and replace one of the endpoints with it. |
| 73 |
"half c_mid = ceil(0.5 * (c_min + c_max));" |
| 74 |
"half4 xym_mid = curve_xym.eval(half2(c_mid + 0.5, curve_texcoord_y));" |
| 75 |
"if (x == xym_mid.x) {" |
| 76 |
// If we hit a control point exactly, then just return. |
| 77 |
"return xym_mid.y;" |
| 78 |
"} else if (x < xym_mid.x) {" |
| 79 |
"c_max = c_mid;" |
| 80 |
"xym_max = xym_mid;" |
| 81 |
"} else {" |
| 82 |
"c_min = c_mid;" |
| 83 |
"xym_min = xym_mid;" |
| 84 |
"}" |
| 85 |
"}" |
| 86 |
// Evaluate the cubic. |
| 87 |
"half h = xym_max.x - xym_min.x;" |
| 88 |
"half mHat_min = xym_min.z * h;" |
| 89 |
"half mHat_max = xym_max.z * h;" |
| 90 |
"half c3 = 2.0 * xym_min.y + mHat_min - 2.0 * xym_max.y + mHat_max;" |
| 91 |
"half c2 = -3.0 * xym_min.y + 3.0 * xym_max.y - 2.0 * mHat_min - mHat_max;" |
| 92 |
"half c1 = mHat_min;" |
| 93 |
"half c0 = xym_min.y;" |
| 94 |
"half t = (x - xym_min.x) / h;" |
| 95 |
"return ((c3*t + c2)*t + c1)*t + c0;" |
| 96 |
"}" |
| 97 |
|
| 98 |
// Shader equivalent of AgtmHelpers::EvaluateColorGainFunction. |
| 99 |
"half3 EvalColorGainFunction(half3 color," |
| 100 |
"half4 mix_rgbx, half4 mix_Mmcx," |
| 101 |
"float curve_texcoord_y, float curve_N_cp) {" |
| 102 |
"half3 M = EvalComponentMixing(color, mix_rgbx, mix_Mmcx);" |
| 103 |
"if (mix_Mmcx.b == 0.0) {" |
| 104 |
// If the kComponent coefficient is zero, only evalute the curve once. |
| 105 |
"return half3(EvalGainCurve(M.r, curve_texcoord_y, curve_N_cp));" |
| 106 |
"}" |
| 107 |
"return half3(EvalGainCurve(M.r, curve_texcoord_y, curve_N_cp)," |
| 108 |
"EvalGainCurve(M.g, curve_texcoord_y, curve_N_cp)," |
| 109 |
"EvalGainCurve(M.b, curve_texcoord_y, curve_N_cp));" |
| 110 |
"}" |
| 111 |
|
| 112 |
// Shader equivalent of AgtmHelpers::ApplyGain. |
| 113 |
"half4 main(half4 color) {" |
| 114 |
"color.rgb *= scale_factor;" |
| 115 |
"if (weight_i > 0.0) {" |
| 116 |
// Unpremultiply alpha is needed. |
| 117 |
"float a_inv = (color.a == 0.0) ? 1.0 : 1.0 / color.a;" |
| 118 |
"half3 G = half3(0.0);" |
| 119 |
"G += weight_i * EvalColorGainFunction(color.rgb * a_inv," |
| 120 |
"mix_rgbx_i, mix_Mmcx_i," |
| 121 |
"curve_texcoord_y_i, curve_N_cp_i);" |
| 122 |
"if (weight_j > 0.0) {" |
| 123 |
"G += weight_j * EvalColorGainFunction(color.rgb * a_inv," |
| 124 |
"mix_rgbx_j, mix_Mmcx_j," |
| 125 |
"curve_texcoord_y_j, curve_N_cp_j);" |
| 126 |
"}" |
| 127 |
"color.rgb *= exp2(G);" |
| 128 |
"}" |
| 129 |
"return color;" |
| 130 |
"}"; |
| 131 |
|
| 132 |
static sk_sp<SkRuntimeEffect> agtm_runtime_effect() { |
| 133 |
auto init_lambda = []() { |
| 134 |
auto result = SkRuntimeEffect::MakeForColorFilter(SkString(gAgtmSKSL), {}); |
| 135 |
SkASSERTF(result.effect, "Agtm shader log:\n%s\n", result.errorText.c_str()); |
| 136 |
return result.effect.release(); |
| 137 |
}; |
| 138 |
static SkRuntimeEffect* effect = init_lambda(); |
| 139 |
return sk_ref_sp(effect); |
| 140 |
} |
| 141 |
|
| 142 |
} // namespace |
| 143 |
|
| 144 |
namespace skhdr { |
| 145 |
|
| 146 |
SkColor4f AgtmHelpers::EvaluateComponentMixingFunction( |
| 147 |
const AdaptiveGlobalToneMap::ComponentMixingFunction& mix, const SkColor4f& c) { |
| 148 |
// This implements that math in Formula (9). |
| 149 |
float common = mix.fRed * c.fR + mix.fGreen * c.fG + mix.fBlue * c.fB + |
| 150 |
mix.fMax * std::max(std::max(c.fR, c.fG), c.fB) + |
| 151 |
mix.fMin * std::min(std::max(c.fR, c.fG), c.fB); |
| 152 |
|
| 153 |
// Optimization for when all components are the same. |
| 154 |
if (mix.fComponent == 0.f) { |
| 155 |
return {common, common, common, c.fA}; |
| 156 |
} |
| 157 |
|
| 158 |
// Formula (10). |
| 159 |
return {mix.fComponent * c.fR + common, |
| 160 |
mix.fComponent * c.fG + common, |
| 161 |
mix.fComponent * c.fB + common, |
| 162 |
c.fA}; |
| 163 |
} |
| 164 |
|
| 165 |
namespace AgtmHelpers { |
| 166 |
|
| 167 |
float EvaluateGainCurve(const AdaptiveGlobalToneMap::GainCurve& gainCurve, float x) { |
| 168 |
auto& cp = gainCurve.fControlPoints; |
| 169 |
size_t N = cp.size(); |
| 170 |
|
| 171 |
// This implements that math in Formula (11). |
| 172 |
SkASSERT(N > 0 && N <= 32); |
| 173 |
|
| 174 |
// Handle points off of the left endpoint. |
| 175 |
size_t i = 0; |
| 176 |
if (x <= cp[i].fX) { |
| 177 |
return cp[i].fY; |
| 178 |
} |
| 179 |
|
| 180 |
// Handle points off of the right endpoint. |
| 181 |
size_t j = N - 1; |
| 182 |
if (x >= cp[j].fX) { |
| 183 |
return cp[j].fY + std::log2(cp[j].fX / x); |
| 184 |
} |
| 185 |
|
| 186 |
// Binary search for i, j bracket in which we find x. |
| 187 |
while (j - i > 1) { |
| 188 |
size_t m = (i + j) / 2; |
| 189 |
if (x < cp[m].fX) { |
| 190 |
j = m; |
| 191 |
} else { |
| 192 |
i = m; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
// Cache short names for the parameters for computing the cubic coefficients. |
| 197 |
const float x_i = cp[i].fX; |
| 198 |
const float y_i = cp[i].fY; |
| 199 |
const float x_j = cp[j].fX; |
| 200 |
const float y_j = cp[j].fY; |
| 201 |
const float h_i = x_j - x_i; |
| 202 |
const float mHat_i = cp[i].fM * h_i; |
| 203 |
const float mHat_j = cp[j].fM * h_i; |
| 204 |
|
| 205 |
// Handle intervals that are a point. |
| 206 |
if (h_i == 0.f) { |
| 207 |
return y_i; |
| 208 |
} |
| 209 |
|
| 210 |
// Compute the coefficients and evaluate the polynomial. |
| 211 |
const float c3 = 2.f * y_i + mHat_i - 2.f * y_j + mHat_j; |
| 212 |
const float c2 = -3.f * y_i + 3.f * y_j - 2.f * mHat_i - mHat_j; |
| 213 |
const float c1 = mHat_i; |
| 214 |
const float c0 = y_i; |
| 215 |
const float t = (x - x_i) / h_i; |
| 216 |
|
| 217 |
return ((c3*t + c2)*t + c1)*t + c0; |
| 218 |
} |
| 219 |
|
| 220 |
SkColor4f EvaluateColorGainFunction( |
| 221 |
const AdaptiveGlobalToneMap::ColorGainFunction& gain, const SkColor4f& c) { |
| 222 |
SkColor4f m = EvaluateComponentMixingFunction(gain.fComponentMixing, c); |
| 223 |
SkColor4f result = {0.f, 0.f, 0.f, c.fA}; |
| 224 |
result.fR = EvaluateGainCurve(gain.fGainCurve, m.fR); |
| 225 |
if (m.fR == m.fG && m.fG == m.fB) { |
| 226 |
result.fG = result.fR; |
| 227 |
result.fB = result.fR; |
| 228 |
} else { |
| 229 |
result.fG = EvaluateGainCurve(gain.fGainCurve, m.fG); |
| 230 |
result.fB = EvaluateGainCurve(gain.fGainCurve, m.fB); |
| 231 |
} |
| 232 |
return result; |
| 233 |
} |
| 234 |
|
| 235 |
void PopulateSlopeFromPCHIP(AdaptiveGlobalToneMap::GainCurve& gainCurve) { |
| 236 |
auto& cp = gainCurve.fControlPoints; |
| 237 |
size_t N = cp.size(); |
| 238 |
|
| 239 |
// Compute the interval width (h) and piecewise linear slope (s). |
| 240 |
float s[AdaptiveGlobalToneMap::GainCurve::kMaxNumControlPoints]; |
| 241 |
float h[AdaptiveGlobalToneMap::GainCurve::kMaxNumControlPoints]; |
| 242 |
for (size_t i = 0; i < N - 1; ++i) { |
| 243 |
h[i] = cp[i+1].fX - cp[i].fX; |
| 244 |
} |
| 245 |
for (size_t i = 0; i < N - 1; ++i) { |
| 246 |
s[i] = (cp[i+1].fY - cp[i].fY) / h[i]; |
| 247 |
} |
| 248 |
|
| 249 |
// Handle the left and right control points. |
| 250 |
if (N >= 3) { |
| 251 |
// Formula (C.7) and Formula (C.8). |
| 252 |
cp[0].fM = ((2 * h[0] + h[1] ) * s[0] - h[0] * s[1] ) / (h[0] + h[1] ); |
| 253 |
cp[N-1].fM = ((2 * h[N-2] + h[N-3]) * s[N-2] - h[N-2] * s[N-3]) / (h[N-2] + h[N-3]); |
| 254 |
} else if (N == 2) { |
| 255 |
cp[0].fM = s[0]; |
| 256 |
cp[N-1].fM = s[0]; |
| 257 |
} else { |
| 258 |
cp[0].fM = 0.f; |
| 259 |
cp[N-1].fM = 0.f; |
| 260 |
} |
| 261 |
|
| 262 |
// Populate internal control points. |
| 263 |
for (size_t i = 1; i <= N - 2; ++i) { |
| 264 |
// Formula (C.9). |
| 265 |
if (s[i-1] * s[i] < 0.f) { |
| 266 |
cp[i].fM = 0.f; |
| 267 |
} else { |
| 268 |
float num = 3 * (h[i-1] + h[i]) * s[i-1] * s[i]; |
| 269 |
float den = (2 * h[i-1] + h[i]) * s[i-1] + (h[i-1] + 2 * h[i]) * s[i]; |
| 270 |
cp[i].fM = num / den; |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
sk_sp<SkImage> |
| 276 |
MakeGainCurveXYMImage(const AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap& hatm) { |
| 277 |
if (hatm.fAlternateImages.empty()) { |
| 278 |
return nullptr; |
| 279 |
} |
| 280 |
size_t maxNumControlPoints = 1; |
| 281 |
for (const auto& alt : hatm.fAlternateImages) { |
| 282 |
maxNumControlPoints = std::max(maxNumControlPoints, |
| 283 |
alt.fColorGainFunction.fGainCurve.fControlPoints.size()); |
| 284 |
} |
| 285 |
|
| 286 |
// Write the X, Y, and M values of the control points into the colors of the rows. |
| 287 |
SkBitmap bm32; |
| 288 |
bm32.allocPixels(SkImageInfo::Make( |
| 289 |
AdaptiveGlobalToneMap::GainCurve::kMaxNumControlPoints, hatm.fAlternateImages.size(), |
| 290 |
kRGBA_F32_SkColorType, kPremul_SkAlphaType)); |
| 291 |
for (size_t a = 0; a < hatm.fAlternateImages.size(); ++a) { |
| 292 |
const auto& alt = hatm.fAlternateImages[a]; |
| 293 |
const auto& curve = alt.fColorGainFunction.fGainCurve; |
| 294 |
for (size_t c = 0; c < curve.fControlPoints.size(); ++c) { |
| 295 |
float* xymX = reinterpret_cast<float*>(bm32.getAddr(c, a)); |
| 296 |
xymX[0] = curve.fControlPoints[c].fX; |
| 297 |
xymX[1] = curve.fControlPoints[c].fY; |
| 298 |
xymX[2] = curve.fControlPoints[c].fM; |
| 299 |
xymX[3] = 1.f; |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
// Convert from F32 to F16 for use on the GPU. |
| 304 |
SkBitmap bm16; |
| 305 |
bm16.allocPixels(bm32.info().makeColorType(kRGBA_F16_SkColorType)); |
| 306 |
if (!bm32.readPixels(bm16.pixmap())) { |
| 307 |
return nullptr; |
| 308 |
} |
| 309 |
bm16.setImmutable(); |
| 310 |
return SkImages::RasterFromBitmap(bm16); |
| 311 |
} |
| 312 |
|
| 313 |
void PopulateUsingRwtmo(AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap& hatm) { |
| 314 |
hatm.fGainApplicationSpacePrimaries = SkNamedPrimaries::kRec2020; |
| 315 |
|
| 316 |
if (hatm.fBaselineHdrHeadroom == 0.f) { |
| 317 |
hatm.fAlternateImages.clear(); |
| 318 |
return; |
| 319 |
} |
| 320 |
|
| 321 |
// Set the two alternate image headrooms using Formula (C.1). |
| 322 |
hatm.fAlternateImages.resize(2); |
| 323 |
hatm.fAlternateImages[0].fHdrHeadroom = 0.f; |
| 324 |
hatm.fAlternateImages[1].fHdrHeadroom = |
| 325 |
std::log2(8.f / 3.f) * std::min(hatm.fBaselineHdrHeadroom / std::log2(1000/203.f), 1.f); |
| 326 |
|
| 327 |
for (size_t a = 0; a < hatm.fAlternateImages.size(); ++a) { |
| 328 |
auto& gain = hatm.fAlternateImages[a].fColorGainFunction; |
| 329 |
gain = AdaptiveGlobalToneMap::ColorGainFunction(); |
| 330 |
|
| 331 |
// Use maxRGB for applying the curve. |
| 332 |
gain.fComponentMixing.fMax = 1.f; |
| 333 |
|
| 334 |
// Compute the image of white under the tone mapping from Formula (C.2). |
| 335 |
const float yWhite = |
| 336 |
(a == 1) ? 1.f |
| 337 |
: 1.f - 0.5f * std::min(hatm.fBaselineHdrHeadroom / std::log2(1000/203.f), 1.f); |
| 338 |
|
| 339 |
// Compute the Bezier control points using Formula (C.3). |
| 340 |
const float kappa = 0.65f; |
| 341 |
const float xKnee = 1.f; |
| 342 |
const float yKnee = yWhite; |
| 343 |
const float xMax = std::exp2(hatm.fBaselineHdrHeadroom); |
| 344 |
const float yMax = std::exp2(hatm.fAlternateImages[a].fHdrHeadroom); |
| 345 |
const float xMid = (1.f - kappa) * xKnee + kappa * (xKnee * yMax / yKnee); |
| 346 |
const float yMid = (1.f - kappa) * yKnee + kappa * yMax; |
| 347 |
|
| 348 |
// Compute the cubic coefficients using Formula (C.5). |
| 349 |
const float xA = xKnee - 2.f * xMid + xMax; |
| 350 |
const float yA = yKnee - 2.f * yMid + yMax; |
| 351 |
const float xB = 2.f * xMid - 2.f * xKnee; |
| 352 |
const float yB = 2.f * yMid - 2.f * yKnee; |
| 353 |
const float xC = xKnee; |
| 354 |
const float yC = yKnee; |
| 355 |
|
| 356 |
auto& cubic = gain.fGainCurve; |
| 357 |
cubic.fControlPoints.resize(8); |
| 358 |
for (size_t c = 0; c < cubic.fControlPoints.size(); ++c) { |
| 359 |
// Compute the linear domain curve values using Formula (C.4). |
| 360 |
const float t = c / (cubic.fControlPoints.size() - 1.f); |
| 361 |
const float x = xC + t * (xB + t * xA); |
| 362 |
const float y = yC + t * (yB + t * yA); |
| 363 |
const float m = (2.f * yA * t + yB) / (2.f * xA * t + xB); |
| 364 |
|
| 365 |
// Compute the log domain curve values using Formula (C.6). |
| 366 |
cubic.fControlPoints[c].fX = x; |
| 367 |
cubic.fControlPoints[c].fY = std::log2(y / x); |
| 368 |
cubic.fControlPoints[c].fM = (x * m - y) / (std::log(2.f) * x * y); |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
Weighting ComputeWeighting(const AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap& hatm, |
| 374 |
float targetedHdrHeadroom) { |
| 375 |
Weighting result; |
| 376 |
|
| 377 |
// Create the list of HDR headrooms including the baseline image and all alternate images, as |
| 378 |
// described Clause 6.2.5 Computation of the headroom-adaptive tone map. |
| 379 |
|
| 380 |
// Let N be the length of the combined list. |
| 381 |
size_t N = 0; |
| 382 |
|
| 383 |
// Let H be the sorted list of HDR headrooms. |
| 384 |
float H[AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap::kMaxNumAlternateImages + 1]; |
| 385 |
|
| 386 |
// Let indices list the index of each entry of H in fAlternateHdrHeadroom. The index for |
| 387 |
// fBaselineHdrHeadroom is Weighting::kInvalidIndex. |
| 388 |
size_t indices[AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap::kMaxNumAlternateImages + 1]; |
| 389 |
for (size_t i = 0; i < hatm.fAlternateImages.size(); ++i) { |
| 390 |
if (N == i && hatm.fBaselineHdrHeadroom < hatm.fAlternateImages[i].fHdrHeadroom) { |
| 391 |
// Insert the baseline HDR headroom before the indices as they are visited. |
| 392 |
indices[N] = Weighting::kInvalidIndex; |
| 393 |
H[N++] = hatm.fBaselineHdrHeadroom; |
| 394 |
} |
| 395 |
indices[N] = i; |
| 396 |
H[N++] = hatm.fAlternateImages[i].fHdrHeadroom; |
| 397 |
} |
| 398 |
if (N == hatm.fAlternateImages.size()) { |
| 399 |
// Insert the baseline HDR headroom at the end if it has not yet been inserted. |
| 400 |
indices[N] = Weighting::kInvalidIndex; |
| 401 |
H[N++] = hatm.fBaselineHdrHeadroom; |
| 402 |
} |
| 403 |
|
| 404 |
// Find the indices for the contributing images. |
| 405 |
if (targetedHdrHeadroom <= H[0]) { |
| 406 |
// One case of Formula (2), for the left endpoint. |
| 407 |
result.fWeight[0] = 1.f; |
| 408 |
result.fAlternateImageIndex[0] = indices[0]; |
| 409 |
} else if (targetedHdrHeadroom >= H[N-1]) { |
| 410 |
// The other case of Formula (2), for the right endpoint. |
| 411 |
result.fWeight[0] = 1.f; |
| 412 |
result.fAlternateImageIndex[0] = indices[N-1]; |
| 413 |
} else { |
| 414 |
// The case of Formula (3). |
| 415 |
size_t i = 0; |
| 416 |
for (i = 0; i < N - 1; ++i) { |
| 417 |
if (H[i] <= targetedHdrHeadroom && targetedHdrHeadroom <= H[i+1]) { |
| 418 |
break; |
| 419 |
} |
| 420 |
} |
| 421 |
result.fWeight[0] = (targetedHdrHeadroom - H[i+1]) / (H[i] - H[i+1]); |
| 422 |
result.fWeight[1] = 1.f - result.fWeight[0]; |
| 423 |
result.fAlternateImageIndex[0] = indices[i]; |
| 424 |
result.fAlternateImageIndex[1] = indices[i+1]; |
| 425 |
} |
| 426 |
|
| 427 |
// The baseline image always has a gain of 0, so set the weight for the baseline image to 0. |
| 428 |
for (size_t i = 0; i < 2; ++i) { |
| 429 |
if (result.fAlternateImageIndex[i] == Weighting::kInvalidIndex) { |
| 430 |
result.fWeight[i] = 0; |
| 431 |
} else if (result.fWeight[i] == 0) { |
| 432 |
result.fAlternateImageIndex[i] = Weighting::kInvalidIndex; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
// Sort the weights so that the first weight is always greater. |
| 437 |
if (result.fWeight[1] > result.fWeight[0]) { |
| 438 |
std::swap(result.fWeight[0], result.fWeight[1]); |
| 439 |
std::swap(result.fAlternateImageIndex[0], result.fAlternateImageIndex[1]); |
| 440 |
} |
| 441 |
|
| 442 |
return result; |
| 443 |
} |
| 444 |
|
| 445 |
void ApplyGain(const AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap& hatm, |
| 446 |
SkSpan<SkColor4f> colors, |
| 447 |
float targetedHdrHeadroom) { |
| 448 |
SkASSERT(Validate(hatm)); |
| 449 |
|
| 450 |
// This function implements Formula (4). It creates special cases for one or both of the weights |
| 451 |
// being zero. |
| 452 |
const auto weighting = AgtmHelpers::ComputeWeighting(hatm, targetedHdrHeadroom); |
| 453 |
|
| 454 |
if (weighting.fWeight[0] == 0.f) { |
| 455 |
// If no weight is non-zero, then no gain will be applied. Leave the points unchanged. |
| 456 |
return; |
| 457 |
} else if (weighting.fWeight[1] == 0.f) { |
| 458 |
// Special case the case of there being only one weighted gain function. |
| 459 |
const auto& gain = |
| 460 |
hatm.fAlternateImages[weighting.fAlternateImageIndex[0]].fColorGainFunction; |
| 461 |
const float w = weighting.fWeight[0]; |
| 462 |
for (auto& C : colors) { |
| 463 |
SkColor4f G = AgtmHelpers::EvaluateColorGainFunction(gain, C); |
| 464 |
C = { |
| 465 |
C.fR * std::exp2(w * G.fR), |
| 466 |
C.fG * std::exp2(w * G.fG), |
| 467 |
C.fB * std::exp2(w * G.fB), |
| 468 |
C.fA, |
| 469 |
}; |
| 470 |
} |
| 471 |
} else { |
| 472 |
// The general case of two weighted gain functions. |
| 473 |
const auto& gain0 = |
| 474 |
hatm.fAlternateImages[weighting.fAlternateImageIndex[0]].fColorGainFunction; |
| 475 |
const float w0 = weighting.fWeight[0]; |
| 476 |
const auto& gain1 = |
| 477 |
hatm.fAlternateImages[weighting.fAlternateImageIndex[1]].fColorGainFunction; |
| 478 |
const float w1 = weighting.fWeight[1]; |
| 479 |
for (auto& C : colors) { |
| 480 |
SkColor4f G0 = AgtmHelpers::EvaluateColorGainFunction(gain0, C); |
| 481 |
SkColor4f G1 = AgtmHelpers::EvaluateColorGainFunction(gain1, C); |
| 482 |
C = { |
| 483 |
C.fR * std::exp2(w0 * G0.fR + w1 * G1.fR), |
| 484 |
C.fG * std::exp2(w0 * G0.fG + w1 * G1.fG), |
| 485 |
C.fB * std::exp2(w0 * G0.fB + w1 * G1.fB), |
| 486 |
C.fA, |
| 487 |
}; |
| 488 |
} |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
sk_sp<SkColorSpace> GetGainApplicationSpace( |
| 493 |
const AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap& hatm) { |
| 494 |
skcms_Matrix3x3 toXYZD50; |
| 495 |
if (!hatm.fGainApplicationSpacePrimaries.toXYZD50(&toXYZD50)) { |
| 496 |
return nullptr; |
| 497 |
} |
| 498 |
return SkColorSpace::MakeRGB(SkNamedTransferFn::kLinear, toXYZD50); |
| 499 |
} |
| 500 |
|
| 501 |
sk_sp<SkColorFilter> MakeColorFilter( |
| 502 |
const AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap& hatm, |
| 503 |
float targetedHdrHeadroom, |
| 504 |
float scaleFactor) { |
| 505 |
const auto weighting = ComputeWeighting(hatm, targetedHdrHeadroom); |
| 506 |
|
| 507 |
auto effect = agtm_runtime_effect(); |
| 508 |
if (!effect) { |
| 509 |
return nullptr; |
| 510 |
} |
| 511 |
SkRuntimeShaderBuilder builder(effect); |
| 512 |
builder.uniform("scale_factor") = scaleFactor; |
| 513 |
for (size_t a = 0; a < 2; ++a) { |
| 514 |
const char* weight_str[2] = {"weight_i", "weight_j"}; |
| 515 |
builder.uniform(weight_str[a]) = weighting.fWeight[a]; |
| 516 |
|
| 517 |
if (weighting.fWeight[a] == 0.f) { |
| 518 |
continue; |
| 519 |
} |
| 520 |
const auto& gain = hatm.fAlternateImages[ |
| 521 |
weighting.fAlternateImageIndex[a]].fColorGainFunction; |
| 522 |
|
| 523 |
const char* mix_rgbx_str[2] = {"mix_rgbx_i", "mix_rgbx_j"}; |
| 524 |
builder.uniform(mix_rgbx_str[a]) = SkColor4f({ |
| 525 |
gain.fComponentMixing.fRed, |
| 526 |
gain.fComponentMixing.fGreen, |
| 527 |
gain.fComponentMixing.fBlue, |
| 528 |
0.f, |
| 529 |
}); |
| 530 |
|
| 531 |
const char* mix_Mmcx_str[2] = {"mix_Mmcx_i", "mix_Mmcx_j"}; |
| 532 |
builder.uniform(mix_Mmcx_str[a]) = SkColor4f({ |
| 533 |
gain.fComponentMixing.fMax, |
| 534 |
gain.fComponentMixing.fMin, |
| 535 |
gain.fComponentMixing.fComponent, |
| 536 |
0.f, |
| 537 |
}); |
| 538 |
|
| 539 |
const char* curve_texcoord_y_str[2] = {"curve_texcoord_y_i", "curve_texcoord_y_j"}; |
| 540 |
builder.uniform(curve_texcoord_y_str[a]) = (weighting.fAlternateImageIndex[a] + 0.5f); |
| 541 |
|
| 542 |
const char* curve_N_cp_str[2] = {"curve_N_cp_i", "curve_N_cp_j"}; |
| 543 |
builder.uniform(curve_N_cp_str[a]) = static_cast<float>( |
| 544 |
gain.fGainCurve.fControlPoints.size()); |
| 545 |
} |
| 546 |
|
| 547 |
if (auto gainCurvesXYM = MakeGainCurveXYMImage(hatm)) { |
| 548 |
builder.child("curve_xym") = gainCurvesXYM->makeRawShader( |
| 549 |
SkSamplingOptions(SkFilterMode::kNearest)); |
| 550 |
} |
| 551 |
|
| 552 |
auto gainApplicationColorSpace = GetGainApplicationSpace(hatm); |
| 553 |
if (!gainApplicationColorSpace) { |
| 554 |
return nullptr; |
| 555 |
} |
| 556 |
|
| 557 |
auto filter = builder.makeColorFilter(); |
| 558 |
SkASSERT(filter); |
| 559 |
return filter->makeWithWorkingColorSpace(gainApplicationColorSpace); |
| 560 |
} |
| 561 |
|
| 562 |
// Return the maximum luminance from CLLI, MDCV, or a default. |
| 563 |
static float get_max_luminance(const Metadata& metadata) { |
| 564 |
if (metadata.getContentLightLevelInformation(nullptr)) { |
| 565 |
ContentLightLevelInformation clli; |
| 566 |
if (metadata.getContentLightLevelInformation(&clli) && clli.fMaxCLL > 0.f) { |
| 567 |
return clli.fMaxCLL; |
| 568 |
} |
| 569 |
} |
| 570 |
if (metadata.getMasteringDisplayColorVolume(nullptr)) { |
| 571 |
MasteringDisplayColorVolume mdcv; |
| 572 |
if (metadata.getMasteringDisplayColorVolume(&mdcv) && |
| 573 |
mdcv.fMaximumDisplayMasteringLuminance > 0.f) { |
| 574 |
return mdcv.fMaximumDisplayMasteringLuminance; |
| 575 |
} |
| 576 |
} |
| 577 |
return 1000.f; |
| 578 |
} |
| 579 |
|
| 580 |
bool PopulateToneMapAgtmParams(const Metadata& metadata, |
| 581 |
const SkColorSpace* inputColorSpace, |
| 582 |
AdaptiveGlobalToneMap* outAgtm, |
| 583 |
float* outScaleFactor) { |
| 584 |
// If `inputColorSpace` is HLG or PQ, find the HDR reference white value. When the shader |
| 585 |
// starts, this is the luminance that will have been mapped to 1.0. We will populate |
| 586 |
// `outScaleFactor` with a scale such that the AGTM HDR reference white luminance (if specified |
| 587 |
// will be mapped to 1.0). |
| 588 |
bool inputIsPqOrHlg = false; |
| 589 |
float inputPqOrHlgWhite = AdaptiveGlobalToneMap::kDefaultHdrReferenceWhite; |
| 590 |
if (inputColorSpace) { |
| 591 |
skcms_TransferFunction trfn; |
| 592 |
inputColorSpace->transferFn(&trfn); |
| 593 |
switch (skcms_TransferFunction_getType(&trfn)) { |
| 594 |
case skcms_TFType_PQ: |
| 595 |
case skcms_TFType_HLG: |
| 596 |
inputIsPqOrHlg = true; |
| 597 |
inputPqOrHlgWhite = trfn.a; |
| 598 |
break; |
| 599 |
default: |
| 600 |
break; |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
AdaptiveGlobalToneMap agtm; |
| 605 |
auto& hatm = agtm.fHeadroomAdaptiveToneMap; |
| 606 |
bool hadAgtmMetadata = metadata.getAdaptiveGlobalToneMap(&agtm); |
| 607 |
|
| 608 |
// SDR content that does not specify an inverse tone mapping will not have a default tone |
| 609 |
// mapping added. |
| 610 |
if (!inputIsPqOrHlg) { |
| 611 |
if (!hadAgtmMetadata || !hatm.has_value()) { |
| 612 |
return false; |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
// If no AGTM was specified, populate the HDR reference white from the input color space. |
| 617 |
if (!hadAgtmMetadata) { |
| 618 |
agtm.fHdrReferenceWhite = inputPqOrHlgWhite; |
| 619 |
} |
| 620 |
|
| 621 |
// If no tone mapping was specified, then use RWTMO with the baseline HDR headroom computed |
| 622 |
// from the CLLI and MDCV metadata. |
| 623 |
if (!hatm.has_value()) { |
| 624 |
hatm = {{ |
| 625 |
.fBaselineHdrHeadroom = std::log2( |
| 626 |
std::max(get_max_luminance(metadata) / agtm.fHdrReferenceWhite, 1.f)) |
| 627 |
}}; |
| 628 |
AgtmHelpers::PopulateUsingRwtmo(hatm.value()); |
| 629 |
} |
| 630 |
|
| 631 |
if (outAgtm) { |
| 632 |
*outAgtm = agtm; |
| 633 |
} |
| 634 |
if (outScaleFactor) { |
| 635 |
*outScaleFactor = inputIsPqOrHlg ? inputPqOrHlgWhite / agtm.fHdrReferenceWhite : 1.f; |
| 636 |
} |
| 637 |
return true; |
| 638 |
} |
| 639 |
|
| 640 |
bool Validate(const AdaptiveGlobalToneMap& agtm) { |
| 641 |
if (agtm.fHdrReferenceWhite < 0.f || agtm.fHdrReferenceWhite > 10000.f) { |
| 642 |
SkCodecPrintf("Agtm validation failed: HdrReferenceWhite invalid\n"); |
| 643 |
return false; |
| 644 |
} |
| 645 |
if (agtm.fHeadroomAdaptiveToneMap.has_value()) { |
| 646 |
if (!Validate(agtm.fHeadroomAdaptiveToneMap.value())) { |
| 647 |
return false; |
| 648 |
} |
| 649 |
} |
| 650 |
return true; |
| 651 |
} |
| 652 |
|
| 653 |
bool Validate(const AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap& hatm) { |
| 654 |
// Enforce the constraints listed in Clause 6.2.2. |
| 655 |
{ |
| 656 |
// The value of H_baseline shall be in the interval [0, 6]. |
| 657 |
if (hatm.fBaselineHdrHeadroom < kMinHdrHeadroom || |
| 658 |
hatm.fBaselineHdrHeadroom > kMaxHdrHeadroom) { |
| 659 |
SkCodecPrintf("Agtm validation failed: H_baseline invalid\n"); |
| 660 |
return false; |
| 661 |
} |
| 662 |
// The value of N_alt shall be in the set {0,1,2,3,4}. |
| 663 |
if (hatm.fAlternateImages.size() > |
| 664 |
AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap::kMaxNumAlternateImages) { |
| 665 |
SkCodecPrintf("Agtm validation failed: NumAlternateImages invalid\n"); |
| 666 |
return false; |
| 667 |
} |
| 668 |
// H_alt,i shall not equal H_baseline. |
| 669 |
for (const auto& alt : hatm.fAlternateImages) { |
| 670 |
if (alt.fHdrHeadroom == hatm.fBaselineHdrHeadroom) { |
| 671 |
SkCodecPrintf("Agtm validation failed: Alternate headroom equal to baseline\n"); |
| 672 |
return false; |
| 673 |
} |
| 674 |
} |
| 675 |
// H_{alt,i} shall be strictly less than H_{alt,i+1} |
| 676 |
for (size_t a = 1; a < hatm.fAlternateImages.size(); ++a) { |
| 677 |
if (hatm.fAlternateImages[a].fHdrHeadroom <= |
| 678 |
hatm.fAlternateImages[a - 1].fHdrHeadroom) { |
| 679 |
SkCodecPrintf("Agtm validation failed: Alternate headrooms not ascending\n"); |
| 680 |
return false; |
| 681 |
} |
| 682 |
} |
| 683 |
// Chromaticity coordinates must be in the unit interval. |
| 684 |
if (!in_unit_interval(hatm.fGainApplicationSpacePrimaries.fRX) || |
| 685 |
!in_unit_interval(hatm.fGainApplicationSpacePrimaries.fRY) || |
| 686 |
!in_unit_interval(hatm.fGainApplicationSpacePrimaries.fGX) || |
| 687 |
!in_unit_interval(hatm.fGainApplicationSpacePrimaries.fGY) || |
| 688 |
!in_unit_interval(hatm.fGainApplicationSpacePrimaries.fBX) || |
| 689 |
!in_unit_interval(hatm.fGainApplicationSpacePrimaries.fBY) || |
| 690 |
!in_unit_interval(hatm.fGainApplicationSpacePrimaries.fWX) || |
| 691 |
!in_unit_interval(hatm.fGainApplicationSpacePrimaries.fWY)) { |
| 692 |
SkCodecPrintf("Agtm validation failed: chromaticities not in unit interval\n"); |
| 693 |
return false; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
// Enforce the constraints listed in Clause 6.2.3 that are required for the metadata to be |
| 698 |
// encodable. The specification indicates that "implementations may adjust metadata items so |
| 699 |
// that they satisfy these constraints" for the other constraints. |
| 700 |
for (const auto& alt : hatm.fAlternateImages) { |
| 701 |
const auto& curve = alt.fColorGainFunction.fGainCurve; |
| 702 |
for (const auto& cp : curve.fControlPoints) { |
| 703 |
if (alt.fHdrHeadroom > hatm.fBaselineHdrHeadroom) { |
| 704 |
// If H_{alt,i} > H_baseline then GainCurve_i(x) >= 0. |
| 705 |
if (cp.fY < 0.f) { |
| 706 |
SkCodecPrintf("Agtm validation failed: negative Y value in inverse tone map\n"); |
| 707 |
return false; |
| 708 |
} |
| 709 |
} else { |
| 710 |
// If H_{alt,i} < H_baseline then GainCurve_i(x) <= 0. |
| 711 |
if (cp.fY > 0.f) { |
| 712 |
SkCodecPrintf("Agtm validation failed: positive Y value in tone map\n"); |
| 713 |
return false; |
| 714 |
} |
| 715 |
} |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
// Enforce the constraints listed in Clause 6.4.2. |
| 720 |
for (const auto& alt : hatm.fAlternateImages) { |
| 721 |
const auto& mix = alt.fColorGainFunction.fComponentMixing; |
| 722 |
// All components shall be in the interval [0, 1]. |
| 723 |
if (!in_unit_interval(mix.fRed) || !in_unit_interval(mix.fGreen) || |
| 724 |
!in_unit_interval(mix.fBlue) || !in_unit_interval(mix.fMax) || |
| 725 |
!in_unit_interval(mix.fMin) || !in_unit_interval(mix.fComponent)) { |
| 726 |
SkCodecPrintf("Agtm validation failed: mix coefficients not in unit interval\n"); |
| 727 |
return false; |
| 728 |
} |
| 729 |
// The sum of all components shall equal 1. The coefficients are encoded in steps of |
| 730 |
// 0.00001. Allow the sum to be at most one step away from 1. |
| 731 |
const float sum = mix.fRed + mix.fGreen + mix.fBlue + mix.fMax + mix.fMin + mix.fComponent; |
| 732 |
if (sum < 0.99999f || sum > 1.00001f) { |
| 733 |
SkCodecPrintf("Agtm validation failed: mix coefficients don't sum to 1\n"); |
| 734 |
return false; |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
// Enforce the constraints listed in Clause 6.5.2. |
| 739 |
for (const auto& alt : hatm.fAlternateImages) { |
| 740 |
const auto& curve = alt.fColorGainFunction.fGainCurve; |
| 741 |
// The value of N_cp must be in the set {1,...,32}. |
| 742 |
if (curve.fControlPoints.size() < AdaptiveGlobalToneMap::GainCurve::kMinNumControlPoints || |
| 743 |
curve.fControlPoints.size() > AdaptiveGlobalToneMap::GainCurve::kMaxNumControlPoints) { |
| 744 |
SkCodecPrintf("Agtm validation failed: invalid number of control points\n"); |
| 745 |
return false; |
| 746 |
} |
| 747 |
for (size_t c = 0; c < curve.fControlPoints.size(); ++c) { |
| 748 |
// The value of x_i shall be in the interval [0, 64]. |
| 749 |
if (curve.fControlPoints[c].fX < 0.f || |
| 750 |
curve.fControlPoints[c].fX > kMaxLinearHdrHeadroom) { |
| 751 |
SkCodecPrintf("Agtm validation failed: invalid X value\n"); |
| 752 |
return false; |
| 753 |
} |
| 754 |
// The value of y_i shall be in the interval [-6, 6]. |
| 755 |
if (curve.fControlPoints[c].fY < -kMaxHdrHeadroom || |
| 756 |
curve.fControlPoints[c].fY > kMaxHdrHeadroom) { |
| 757 |
SkCodecPrintf("Agtm validation failed: invalid Y value\n"); |
| 758 |
return false; |
| 759 |
} |
| 760 |
if (c > 0) { |
| 761 |
// It shall be the case that x_i <= x_{i+1}. |
| 762 |
if (curve.fControlPoints[c].fX < curve.fControlPoints[c - 1].fX) { |
| 763 |
SkCodecPrintf("Agtm validation failed: curve X is not non-decreasing\n"); |
| 764 |
return false; |
| 765 |
} |
| 766 |
// If x_i == x_{i+1} then it shall be the case that y_i == y_{i+1}. |
| 767 |
if (curve.fControlPoints[c].fX == curve.fControlPoints[c - 1].fX && |
| 768 |
curve.fControlPoints[c].fY != curve.fControlPoints[c - 1].fY) { |
| 769 |
SkCodecPrintf("Agtm validation failed: curve has Y discontinuity\n"); |
| 770 |
return false; |
| 771 |
} |
| 772 |
} |
| 773 |
} |
| 774 |
} |
| 775 |
return true; |
| 776 |
} |
| 777 |
|
| 778 |
} // namespace AgtmHelpers |
| 779 |
|
| 780 |
SkString AdaptiveGlobalToneMap::toString() const { |
| 781 |
SkString result = SkStringPrintf("{hdrReferenceWhite:%f", fHdrReferenceWhite); |
| 782 |
if (!fHeadroomAdaptiveToneMap.has_value()) { |
| 783 |
result += "}"; |
| 784 |
return result; |
| 785 |
} |
| 786 |
auto& hatm = fHeadroomAdaptiveToneMap.value(); |
| 787 |
|
| 788 |
result += SkStringPrintf(", baselineHdrHeadroom:%f", hatm.fBaselineHdrHeadroom); |
| 789 |
result += ", alternateHdrHeadrooms:["; |
| 790 |
for (size_t a = 0; a < hatm.fAlternateImages.size(); ++a) { |
| 791 |
result += SkStringPrintf("%f", hatm.fAlternateImages[a].fHdrHeadroom); |
| 792 |
if (a != hatm.fAlternateImages.size() - 1) { |
| 793 |
result += ", "; |
| 794 |
} |
| 795 |
} |
| 796 |
result += "]}"; |
| 797 |
return result; |
| 798 |
} |
| 799 |
|
| 800 |
} // namespace skhdr |
| 801 |
|