| 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 |
#ifndef SkHdrMetadata_DEFINED |
| 9 |
#define SkHdrMetadata_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkColorSpace.h" |
| 12 |
#include "include/core/SkData.h" |
| 13 |
#include "include/private/base/SkAPI.h" |
| 14 |
|
| 15 |
#include <memory> |
| 16 |
#include <optional> |
| 17 |
#include <vector> |
| 18 |
|
| 19 |
class SkColorFilter; |
| 20 |
class SkData; |
| 21 |
class SkString; |
| 22 |
|
| 23 |
namespace skhdr { |
| 24 |
|
| 25 |
/** |
| 26 |
* Content light level metadata. |
| 27 |
* The semantics of this metadata is defined in: |
| 28 |
* ANSI/CTA-861-H A DTV Profile for Uncompressed High Speed Digital Interfaces |
| 29 |
* Annex P Calculation of MaxCLL and MaxFALL |
| 30 |
* Slightly different semantics for this metadata are defined in: |
| 31 |
* Portable Network Graphics (PNG) Specification (Third Edition) |
| 32 |
* 11.3.2.8 cLLI Content Light Level Information |
| 33 |
* https://www.w3.org/TR/png-3/#cLLI-chunk |
| 34 |
* This metadata should only be used in ways that work with both semantics. |
| 35 |
*/ |
| 36 |
struct SK_API ContentLightLevelInformation { |
| 37 |
float fMaxCLL = 0.f; |
| 38 |
float fMaxFALL = 0.f; |
| 39 |
|
| 40 |
/** |
| 41 |
* Decode from the binary encoding listed at: |
| 42 |
* AV1 Bitstream & Decoding Process Specification Version 1.0.0 Errata 1 |
| 43 |
* https://aomediacodec.github.io/av1-spec/av1-spec.pdf |
| 44 |
* 5.8.3 Metadata high dynamic range content light level syntax |
| 45 |
* This encoding is equivalent to: |
| 46 |
* ITU-T H.265 (V10) (07/2024) |
| 47 |
* D.2.35 Content light level information SEI message syntax |
| 48 |
* Return false if parsing fails. |
| 49 |
*/ |
| 50 |
bool parse(const SkData* data); |
| 51 |
|
| 52 |
/** |
| 53 |
* Serialize to the encoding used by parse(). |
| 54 |
*/ |
| 55 |
sk_sp<SkData> serialize() const; |
| 56 |
|
| 57 |
/** |
| 58 |
* Decode from the binary encoding listed at: |
| 59 |
* Portable Network Graphics (PNG) Specification (Third Edition) |
| 60 |
* 11.3.2.8 cLLI Content Light Level Information |
| 61 |
* https://www.w3.org/TR/png-3/#cLLI-chunk |
| 62 |
* This encoding is not equivalent to the encoding used by parse(). |
| 63 |
* Return false if parsing fails. |
| 64 |
*/ |
| 65 |
bool parsePngChunk(const SkData* data); |
| 66 |
|
| 67 |
/** |
| 68 |
* Serialize to the encoding used by parsePngChunk(). |
| 69 |
*/ |
| 70 |
sk_sp<SkData> serializePngChunk() const; |
| 71 |
|
| 72 |
/** |
| 73 |
* Return a human-readable description. |
| 74 |
*/ |
| 75 |
SkString toString() const; |
| 76 |
|
| 77 |
bool operator==(const ContentLightLevelInformation& other) const; |
| 78 |
bool operator!=(const ContentLightLevelInformation& other) const { |
| 79 |
return !(*this == other); |
| 80 |
} |
| 81 |
}; |
| 82 |
|
| 83 |
/** |
| 84 |
* Mastering display color volume metadata. |
| 85 |
* The semantics of this metadata is defined in: |
| 86 |
* SMPTE ST 2086:2018 Mastering Display Color Volume Metadata Supporting |
| 87 |
* High Luminance and Wide Color Gamut Images |
| 88 |
*/ |
| 89 |
struct SK_API MasteringDisplayColorVolume { |
| 90 |
SkColorSpacePrimaries fDisplayPrimaries = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}; |
| 91 |
float fMaximumDisplayMasteringLuminance = 0.f; |
| 92 |
float fMinimumDisplayMasteringLuminance = 0.f; |
| 93 |
|
| 94 |
/** |
| 95 |
* The encoding as defined in: |
| 96 |
* AV1 Bitstream & Decoding Process Specification Version 1.0.0 Errata 1 |
| 97 |
* https://aomediacodec.github.io/av1-spec/av1-spec.pdf |
| 98 |
* 5.8.4 Metadata high dynamic range mastering display color volume syntax |
| 99 |
* This encoding is equivalent to: |
| 100 |
* ITU-T H.265 (V10) (07/2024) |
| 101 |
* D.2.35 Content light level information SEI message syntax |
| 102 |
* This encoding is also equivalent to: |
| 103 |
* Portable Network Graphics (PNG) Specification (Third Edition) |
| 104 |
* 11.3.2.7 mDCV Mastering Display Color Volume |
| 105 |
* https://www.w3.org/TR/png-3/#mDCV-chunk |
| 106 |
* Return false if parsing fails. |
| 107 |
*/ |
| 108 |
bool parse(const SkData* data); |
| 109 |
|
| 110 |
/** |
| 111 |
* Serialize to the encoding used by parse(). |
| 112 |
*/ |
| 113 |
sk_sp<SkData> serialize() const; |
| 114 |
|
| 115 |
/** |
| 116 |
* Return a human-readable description. |
| 117 |
*/ |
| 118 |
SkString toString() const; |
| 119 |
|
| 120 |
bool operator==(const MasteringDisplayColorVolume& other) const; |
| 121 |
bool operator!=(const MasteringDisplayColorVolume& other) const { |
| 122 |
return !(*this == other); |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
/** |
| 127 |
* Adaptive Global Tone Map |
| 128 |
* This structure contains the metadata items from the ColorVolumeTransform metadata group |
| 129 |
* in Clause 7.1: Metadata set of SMPTE ST 2094-50: Dynamic metadata for color volume transform |
| 130 |
* Application #5 |
| 131 |
* https://github.com/SMPTE/st2094-50 |
| 132 |
* WARNING: This specification is still a DRAFT and is subject to change before publication. |
| 133 |
*/ |
| 134 |
struct SK_API AdaptiveGlobalToneMap { |
| 135 |
// A GainCurve metadata group. |
| 136 |
struct SK_API GainCurve { |
| 137 |
// Structure holding one entry of the GainCurveControlPointX, GainCurveControlPointY, and |
| 138 |
// GainCurveControlPointM metadata items. |
| 139 |
struct ControlPoint { |
| 140 |
float fX = 0.f; |
| 141 |
float fY = 0.f; |
| 142 |
float fM = 0.f; |
| 143 |
|
| 144 |
bool operator==(const ControlPoint& other) const { |
| 145 |
return fX == other.fX && fY == other.fY && fM == other.fM; |
| 146 |
} |
| 147 |
}; |
| 148 |
|
| 149 |
// The size of this vector is the value of the GainCurveNumControlPoints metadata item. |
| 150 |
static constexpr size_t kMinNumControlPoints = 1u; |
| 151 |
static constexpr size_t kMaxNumControlPoints = 32u; |
| 152 |
std::vector<ControlPoint> fControlPoints = {}; |
| 153 |
|
| 154 |
bool operator==(const GainCurve& other) const { |
| 155 |
return fControlPoints == other.fControlPoints; |
| 156 |
} |
| 157 |
}; |
| 158 |
|
| 159 |
// A ComponentMix metadata group. |
| 160 |
struct SK_API ComponentMixingFunction { |
| 161 |
// The ComponentMixRed/Green/Blue/Max/Min/Component metadata items. |
| 162 |
float fRed = 0.f; |
| 163 |
float fGreen = 0.f; |
| 164 |
float fBlue = 0.f; |
| 165 |
float fMax = 0.f; |
| 166 |
float fMin = 0.f; |
| 167 |
float fComponent = 0.f; |
| 168 |
|
| 169 |
bool operator==(const ComponentMixingFunction& other) const { |
| 170 |
return fRed == other.fRed && fGreen == other.fGreen && fBlue == other.fBlue && |
| 171 |
fMax == other.fMax && fMin == other.fMin && fComponent == other.fComponent; |
| 172 |
} |
| 173 |
}; |
| 174 |
|
| 175 |
// A ColorGainFunction metadata group. |
| 176 |
struct SK_API ColorGainFunction { |
| 177 |
// The ComponentMix metadata group. |
| 178 |
ComponentMixingFunction fComponentMixing = {}; |
| 179 |
|
| 180 |
// The GainCurve metadata group. |
| 181 |
GainCurve fGainCurve = {}; |
| 182 |
|
| 183 |
bool operator==(const ColorGainFunction& other) const { |
| 184 |
return fComponentMixing == other.fComponentMixing && fGainCurve == other.fGainCurve; |
| 185 |
} |
| 186 |
}; |
| 187 |
|
| 188 |
// Structure holding the metadata items and groups for an alternate image. |
| 189 |
struct SK_API AlternateImage { |
| 190 |
// The AlternateHdrHeadroom metadata item. |
| 191 |
float fHdrHeadroom = 0.f; |
| 192 |
|
| 193 |
// The ColorGainFunction metadata group. |
| 194 |
ColorGainFunction fColorGainFunction = {}; |
| 195 |
|
| 196 |
bool operator==(const AlternateImage& other) const { |
| 197 |
return fHdrHeadroom == other.fHdrHeadroom && fColorGainFunction == other.fColorGainFunction; |
| 198 |
} |
| 199 |
}; |
| 200 |
|
| 201 |
// HeadroomAdaptiveToneMap metadata group. |
| 202 |
struct SK_API HeadroomAdaptiveToneMap { |
| 203 |
// The BaselineHdrHeadroom metadata item. |
| 204 |
float fBaselineHdrHeadroom = 0.f; |
| 205 |
|
| 206 |
// The GainApplicationSpaceColorPrimaries metadata item. |
| 207 |
SkColorSpacePrimaries fGainApplicationSpacePrimaries = |
| 208 |
{0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f}; |
| 209 |
|
| 210 |
// The size of this vector is the NumAlternateImages metadata item. |
| 211 |
static constexpr size_t kMaxNumAlternateImages = 4u; |
| 212 |
std::vector<AlternateImage> fAlternateImages = {}; |
| 213 |
|
| 214 |
bool operator==(const HeadroomAdaptiveToneMap& other) const { |
| 215 |
return fBaselineHdrHeadroom == other.fBaselineHdrHeadroom && |
| 216 |
fGainApplicationSpacePrimaries == other.fGainApplicationSpacePrimaries && |
| 217 |
fAlternateImages == other.fAlternateImages; |
| 218 |
} |
| 219 |
}; |
| 220 |
|
| 221 |
// The default value for the HdrReferenceWhite metadata item. |
| 222 |
static constexpr float kDefaultHdrReferenceWhite = 203.f; |
| 223 |
|
| 224 |
// The HdrReferenceWhite metadata item. |
| 225 |
float fHdrReferenceWhite = kDefaultHdrReferenceWhite; |
| 226 |
|
| 227 |
// The HeadroomAdaptiveToneMap metadata group. |
| 228 |
std::optional<HeadroomAdaptiveToneMap> fHeadroomAdaptiveToneMap = std::nullopt; |
| 229 |
|
| 230 |
/** |
| 231 |
* Decode from the binary encoding in Annex C. |
| 232 |
*/ |
| 233 |
bool parse(const SkData* data); |
| 234 |
|
| 235 |
/** |
| 236 |
* Serialize to the encoding used by parse(). |
| 237 |
*/ |
| 238 |
sk_sp<SkData> serialize() const; |
| 239 |
|
| 240 |
/** |
| 241 |
* Return a human-readable description. |
| 242 |
*/ |
| 243 |
SkString toString() const; |
| 244 |
|
| 245 |
bool operator==(const AdaptiveGlobalToneMap& other) const { |
| 246 |
return fHdrReferenceWhite == other.fHdrReferenceWhite && |
| 247 |
fHeadroomAdaptiveToneMap == other.fHeadroomAdaptiveToneMap; |
| 248 |
} |
| 249 |
}; |
| 250 |
|
| 251 |
/** |
| 252 |
* Structure containing all HDR metadata that can be attached to an image or video frame. |
| 253 |
*/ |
| 254 |
class SK_API Metadata { |
| 255 |
public: |
| 256 |
/** |
| 257 |
* Return a container with no metadata. |
| 258 |
*/ |
| 259 |
static Metadata MakeEmpty(); |
| 260 |
|
| 261 |
/** |
| 262 |
* If there does not exists Content Light Level Information metadata, then return false. |
| 263 |
* Otherwise return true and if `clli` is non-nullptr then write the metadata to `clli`. |
| 264 |
*/ |
| 265 |
bool getContentLightLevelInformation(ContentLightLevelInformation* clli) const; |
| 266 |
|
| 267 |
/** |
| 268 |
* Set the Content Light Level Information metadata. |
| 269 |
*/ |
| 270 |
void setContentLightLevelInformation(const ContentLightLevelInformation& clli); |
| 271 |
|
| 272 |
/** |
| 273 |
* If there does not exists Mastering Display Color Volume metadata, then return false. |
| 274 |
* Otherwise return true and if `mdcv` is non-nullptr then write the metadata to `mdcv`. |
| 275 |
*/ |
| 276 |
bool getMasteringDisplayColorVolume(MasteringDisplayColorVolume* mdcv) const; |
| 277 |
|
| 278 |
/** |
| 279 |
* Set the Mastering Display Color Volume metadata. |
| 280 |
*/ |
| 281 |
void setMasteringDisplayColorVolume(const MasteringDisplayColorVolume& mdcv); |
| 282 |
|
| 283 |
/** |
| 284 |
* If there does not exists Adaptive Global Tone Map metadata, then return false. |
| 285 |
* Otherwise return true and if `agtm` is non-nullptr then write the metadata to `agtm`. |
| 286 |
*/ |
| 287 |
bool getAdaptiveGlobalToneMap(AdaptiveGlobalToneMap* agtm) const; |
| 288 |
|
| 289 |
/** |
| 290 |
* Set the Adaptive Global Tone Map metadata. |
| 291 |
*/ |
| 292 |
void setAdaptiveGlobalToneMap(const AdaptiveGlobalToneMap& agtm); |
| 293 |
|
| 294 |
/** |
| 295 |
* Return the serialized Adaptive Global Tone Mapping metadata, or nullptr if none has been set. |
| 296 |
*/ |
| 297 |
sk_sp<const SkData> getSerializedAgtm() const; |
| 298 |
|
| 299 |
/** |
| 300 |
* Set the serialized Adaptive Global Tone Mapping metadata. |
| 301 |
*/ |
| 302 |
void setSerializedAgtm(sk_sp<const SkData>); |
| 303 |
|
| 304 |
/** |
| 305 |
* Return a human-readable description. |
| 306 |
*/ |
| 307 |
SkString toString() const; |
| 308 |
|
| 309 |
/** |
| 310 |
* Return the SkColorFilter to tone map to the specified targeted HDR headroom. |
| 311 |
* |
| 312 |
* The `inputColorSpace` parameter is the color space of the input image that will be |
| 313 |
* used with the image. If it is PQ or HLG, then the color filter will effectively |
| 314 |
* reinterpret the image as having the HDR reference white parameter indicated in the |
| 315 |
* metadata. |
| 316 |
* |
| 317 |
* If `inputColorSpace` is PQ or HLG, then a default tone mapping will be provided, |
| 318 |
* inferring the baseline HDR headroom from the CLLI or MDCV metadata, if present. |
| 319 |
*/ |
| 320 |
sk_sp<SkColorFilter> makeToneMapColorFilter( |
| 321 |
float targetedHdrHeadroom, const SkColorSpace* inputColorSpace = nullptr) const; |
| 322 |
|
| 323 |
bool operator==(const Metadata& other) const; |
| 324 |
bool operator!=(const Metadata& other) const { |
| 325 |
return !(*this == other); |
| 326 |
} |
| 327 |
|
| 328 |
private: |
| 329 |
std::optional<ContentLightLevelInformation> fContentLightLevelInformation; |
| 330 |
std::optional<MasteringDisplayColorVolume> fMasteringDisplayColorVolume; |
| 331 |
std::optional<AdaptiveGlobalToneMap> fAdaptiveGlobalToneMap; |
| 332 |
}; |
| 333 |
|
| 334 |
} // namespace skhdr |
| 335 |
|
| 336 |
#endif |