Debian Codegraph @

src/codec/SkHdrAgtmParse.cpp

raw
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/SkStream.h"
9 #include "include/private/base/SkFloatingPoint.h"
10 #include "src/codec/SkHdrAgtmPriv.h"
11 #include "src/core/SkStreamPriv.h"
12
13 namespace {
14
15 // Return x clamped to [a, b].
16 template<typename T, typename U, typename V>
17 T clamp(T x, U a, V b) {
18 return std::min(std::max(x, static_cast<T>(a)), static_cast<T>(b));
19 }
20
21 // Convert f by scaling by `scale`, offsetting by `offset`, and then clamping the result to
22 // [`clamp_min`, `clamp_max`].
23 uint16_t float_to_uint16(float f,
24 uint16_t clamp_min, uint16_t clamp_max,
25 uint16_t offset, float scale) {
26 int32_t v = static_cast<int32_t>(std::lround(f * scale)) + offset;
27 return clamp(v, clamp_min, clamp_max);
28 }
29
30 // The inverse of float_to_uint16's conversion.
31 float uint16_to_float(uint16_t v,
32 uint16_t clamp_min, uint16_t clamp_max,
33 uint16_t offset, float scale) {
34 return (static_cast<int32_t>(clamp(v, clamp_min, clamp_max)) - offset) / scale;
35 }
36
37 // Helper class to read individual fields of a uint8_t bitfield.
38 class BitfieldReader {
39 public:
40 bool readFromStream(SkMemoryStream& s) {
41 return s.readU8(&fBits);
42 }
43 uint8_t readBits(uint8_t bits) {
44 // Read the topmost `bits` bits, then shift them off.
45 uint8_t result = fBits >> (8 - bits);
46 fBits <<= bits;
47 fBitsRead += bits;
48 return result;
49 }
50 private:
51 uint8_t fBits = 0;
52 uint8_t fBitsRead = 0;
53 };
54
55 // Helper class to write individual fields of a uint8_t bitfield.
56 class BitfieldWriter {
57 public:
58 void writeBits(uint8_t value, uint8_t bits) {
59 // Ensure the value fits in `bits`.
60 SkASSERT(value <= (1 << bits) - 1);
61 fBits <<= bits;
62 fBits |= value;
63 fBitsWritten += bits;
64 }
65 void padAndWriteToStream(SkDynamicMemoryWStream& s) {
66 // Write the remaining bits as zero, then write the result.
67 SkASSERT(fBitsWritten <= 8);
68 fBits <<= (8 - fBitsWritten);
69 fBitsWritten = 8;
70 s.write8(fBits);
71 }
72 private:
73 uint8_t fBits = 0;
74 uint8_t fBitsWritten = 0;
75 };
76
77 // Wrapper to return false if the specified call returns false. Used to keep syntax parsing
78 // shorter.
79 #define RETURN_ON_FALSE(x) \
80 do { \
81 if (!(x)) { \
82 SkDebugf("AGTM parsing failed %s at %d\n", #x, __LINE__); \
83 return false; \
84 } \
85 } while (0)
86
87 // All syntax elements listed in Annex C.2 (excluding reserved_zero). Parsing and serializing of
88 // an skhdr::Agtm is done in two steps:
89 // * converting from the metadata items to/from syntax elements (from Annex C.3)
90 // * serializing the syntax elements to/from a stream (from Annex C.2)
91 struct AgtmSyntax {
92 static constexpr uint8_t kNumChromaticityValues = 8;
93 static constexpr uint8_t kMaxNumAlternateImages = 4;
94 static constexpr uint8_t kMaxNumControlPoints = 32;
95 static constexpr uint8_t kNumMixCoefficients = 6;
96
97 // Parse or write the syntax elements according to Annex C.2.
98 bool parse_application_info(SkMemoryStream& s);
99 void write_application_info(SkDynamicMemoryWStream& s);
100
101 // Parse or write according to Table C.1.
102 bool parse_color_volume_transform(SkMemoryStream& s);
103 void write_color_volume_transform(SkDynamicMemoryWStream& s);
104
105 // Parse or write according to Table C.3.
106 bool parse_adaptive_tone_map(SkMemoryStream& s);
107 void write_adaptive_tone_map(SkDynamicMemoryWStream& s);
108
109 // Parse or write according to Table C.4.
110 bool parse_component_mixing(uint8_t a, SkMemoryStream& s);
111 void write_component_mixing(uint8_t a, SkDynamicMemoryWStream& s);
112
113 // Parse or write according to Table C.5.
114 bool parse_gain_curve(uint8_t a, SkMemoryStream& s);
115 void write_gain_curve(uint8_t a, SkDynamicMemoryWStream& s);
116
117 // syntax elements of smpte_st_2094_50_application_info()
118 uint8_t application_version:3;
119 uint8_t minimum_application_version:3;
120
121 // syntax elements of smpte_st_2094_50_color_volume_transform()
122 uint8_t has_custom_hdr_reference_white_flag:1;
123 uint8_t has_adaptive_tone_map_flag:1;
124 uint16_t hdr_reference_white;
125
126 // syntax elements of smpte_st_2094_50_adaptive_tone_map()
127 uint16_t baseline_hdr_headroom;
128 uint8_t use_reference_white_tone_mapping_flag:1;
129 uint8_t num_alternate_images:3;
130 uint8_t gain_application_space_chromaticities_flag:2;
131 uint8_t has_common_component_mix_params_flag:1;
132 uint8_t has_common_curve_params_flag:1;
133 uint16_t gain_application_space_chromaticities[kNumChromaticityValues];
134 uint16_t alternate_hdr_headrooms[kMaxNumAlternateImages];
135
136 // syntax elements of smpte_st_2094_50_component_mixing()
137 uint8_t component_mixing_type[kMaxNumAlternateImages];
138 uint8_t has_component_mixing_coefficient_flag[kMaxNumAlternateImages][kNumMixCoefficients];
139 uint16_t component_mixing_coefficient[kMaxNumAlternateImages][kNumMixCoefficients];
140
141 // syntax elements of smpte_st_2094_50_gain_curve()
142 uint8_t gain_curve_num_control_points_minus_1[kMaxNumAlternateImages];
143 uint8_t gain_curve_use_pchip_slope_flag[kMaxNumAlternateImages];
144 uint16_t gain_curve_control_points_x[kMaxNumAlternateImages][kMaxNumControlPoints];
145 uint16_t gain_curve_control_points_y[kMaxNumAlternateImages][kMaxNumControlPoints];
146 uint16_t gain_curve_control_points_theta[kMaxNumAlternateImages][kMaxNumControlPoints];
147 };
148
149 static_assert(
150 AgtmSyntax::kMaxNumAlternateImages ==
151 skhdr::AdaptiveGlobalToneMap::HeadroomAdaptiveToneMap::kMaxNumAlternateImages);
152 static_assert(
153 AgtmSyntax::kMaxNumControlPoints ==
154 skhdr::AdaptiveGlobalToneMap::GainCurve::kMaxNumControlPoints);
155
156 // Parse according to Table C.1.
157 bool AgtmSyntax::parse_application_info(SkMemoryStream& s) {
158 BitfieldReader flags;
159 RETURN_ON_FALSE(flags.readFromStream(s));
160 application_version = flags.readBits(3);
161 minimum_application_version = flags.readBits(3);
162 if (minimum_application_version > 0) {
163 return false;
164 }
165 const auto reserved_zero = flags.readBits(2);
166 RETURN_ON_FALSE(reserved_zero == 0);
167 RETURN_ON_FALSE(parse_color_volume_transform(s));
168 return true;
169 }
170
171 void AgtmSyntax::write_application_info(SkDynamicMemoryWStream& s) {
172 BitfieldWriter flags;
173 flags.writeBits(application_version, 3);
174 flags.writeBits(minimum_application_version, 3);
175 flags.padAndWriteToStream(s);
176
177 write_color_volume_transform(s);
178 }
179
180 // Parse according to Table C.2.
181 bool AgtmSyntax::parse_color_volume_transform(SkMemoryStream& s) {
182 BitfieldReader flags;
183 RETURN_ON_FALSE(flags.readFromStream(s));
184 has_custom_hdr_reference_white_flag = flags.readBits(1);
185 has_adaptive_tone_map_flag = flags.readBits(1);
186 const auto reserved_zero = flags.readBits(6);
187 RETURN_ON_FALSE(reserved_zero == 0);
188 if (has_custom_hdr_reference_white_flag == 1) {
189 RETURN_ON_FALSE(SkStreamPriv::ReadU16BE(&s, &hdr_reference_white));
190 }
191 if (has_adaptive_tone_map_flag == 1) {
192 RETURN_ON_FALSE(parse_adaptive_tone_map(s));
193 }
194 return true;
195 }
196
197 void AgtmSyntax::write_color_volume_transform(SkDynamicMemoryWStream& s) {
198 BitfieldWriter flags;
199 flags.writeBits(has_custom_hdr_reference_white_flag, 1);
200 flags.writeBits(has_adaptive_tone_map_flag, 1);
201 flags.padAndWriteToStream(s);
202 if (has_custom_hdr_reference_white_flag) {
203 SkStreamPriv::WriteU16BE(&s, hdr_reference_white);
204 }
205 if (has_adaptive_tone_map_flag == 1) {
206 write_adaptive_tone_map(s);
207 }
208 }
209
210 bool AgtmSyntax::parse_adaptive_tone_map(SkMemoryStream& s) {
211 RETURN_ON_FALSE(SkStreamPriv::ReadU16BE(&s, &baseline_hdr_headroom));
212 BitfieldReader flags;
213 RETURN_ON_FALSE(flags.readFromStream(s) );
214 use_reference_white_tone_mapping_flag = flags.readBits(1);
215 if (use_reference_white_tone_mapping_flag == 0) {
216 num_alternate_images = flags.readBits(3);
217 gain_application_space_chromaticities_flag = flags.readBits(2);
218 has_common_component_mix_params_flag = flags.readBits(1);
219 has_common_curve_params_flag = flags.readBits(1);
220 if (gain_application_space_chromaticities_flag == 3) {
221 for (uint8_t r = 0; r < kNumChromaticityValues; ++r) {
222 RETURN_ON_FALSE(
223 SkStreamPriv::ReadU16BE(&s, &gain_application_space_chromaticities[r]));
224 }
225 }
226 for (uint8_t a = 0; a < clamp(num_alternate_images, 0u, kMaxNumAlternateImages); ++a) {
227 RETURN_ON_FALSE(SkStreamPriv::ReadU16BE(&s, &alternate_hdr_headrooms[a]));
228 RETURN_ON_FALSE(parse_component_mixing(a, s));
229 RETURN_ON_FALSE(parse_gain_curve(a, s));
230 }
231 } else {
232 const auto reserved_zero = flags.readBits(7);
233 RETURN_ON_FALSE(reserved_zero == 0);
234 }
235 return true;
236 }
237
238 void AgtmSyntax::write_adaptive_tone_map(SkDynamicMemoryWStream& s) {
239 SkStreamPriv::WriteU16BE(&s, baseline_hdr_headroom);
240 BitfieldWriter flags;
241 flags.writeBits(use_reference_white_tone_mapping_flag, 1);
242 if (use_reference_white_tone_mapping_flag == 0) {
243 flags.writeBits(num_alternate_images, 3);
244 flags.writeBits(gain_application_space_chromaticities_flag, 2);
245 flags.writeBits(has_common_component_mix_params_flag, 1);
246 flags.writeBits(has_common_curve_params_flag, 1);
247 flags.padAndWriteToStream(s);
248 if (gain_application_space_chromaticities_flag == 3) {
249 for (uint8_t r = 0; r < kNumChromaticityValues; ++r) {
250 SkStreamPriv::WriteU16BE(&s, gain_application_space_chromaticities[r]);
251 }
252 }
253 for (uint8_t a = 0; a < num_alternate_images; ++a) {
254 SkStreamPriv::WriteU16BE(&s, alternate_hdr_headrooms[a]);
255 write_component_mixing(a, s);
256 write_gain_curve(a, s);
257 }
258 } else {
259 flags.padAndWriteToStream(s);
260 }
261 }
262
263 bool AgtmSyntax::parse_component_mixing(uint8_t a, SkMemoryStream& s) {
264 if (a == 0 || has_common_component_mix_params_flag == 0) {
265 BitfieldReader flags;
266 RETURN_ON_FALSE(flags.readFromStream(s));
267 component_mixing_type[a] = flags.readBits(2);
268 if (component_mixing_type[a] != 3) {
269 const auto reserved_zero = flags.readBits(kNumMixCoefficients);
270 RETURN_ON_FALSE(reserved_zero == 0);
271 } else {
272 for (uint8_t k = 0; k < kNumMixCoefficients; ++k) {
273 has_component_mixing_coefficient_flag[a][k] = flags.readBits(1);
274 }
275 for (uint8_t k = 0; k < kNumMixCoefficients; ++k) {
276 if (has_component_mixing_coefficient_flag[a][k] == 1) {
277 RETURN_ON_FALSE(
278 SkStreamPriv::ReadU16BE(&s, &component_mixing_coefficient[a][k]));
279 } else {
280 component_mixing_coefficient[a][k] = 0;
281 }
282 }
283 }
284 } else {
285 component_mixing_type[a] = component_mixing_type[0];
286 if (component_mixing_type[a] == 3) {
287 for (uint8_t k = 0; k < kNumMixCoefficients; ++k) {
288 component_mixing_coefficient[a][k] = component_mixing_coefficient[0][k];
289 }
290 }
291 }
292 return true;
293 }
294
295 void AgtmSyntax::write_component_mixing(uint8_t a, SkDynamicMemoryWStream& s) {
296 if (a == 0 || has_common_component_mix_params_flag == 0) {
297 BitfieldWriter flags;
298 flags.writeBits(component_mixing_type[a], 2);
299 if (component_mixing_type[a] == 3) {
300 for (uint8_t k = 0; k < kNumMixCoefficients; ++k) {
301 flags.writeBits(has_component_mixing_coefficient_flag[a][k], 1);
302 }
303 flags.padAndWriteToStream(s);
304 for (uint8_t k = 0; k < kNumMixCoefficients; ++k) {
305 if (has_component_mixing_coefficient_flag[a][k] == 1) {
306 SkStreamPriv::WriteU16BE(&s, component_mixing_coefficient[a][k]);
307 }
308 }
309 } else {
310 flags.padAndWriteToStream(s);
311 }
312 }
313 }
314
315 bool AgtmSyntax::parse_gain_curve(uint8_t a, SkMemoryStream& s) {
316 if (a == 0 || has_common_curve_params_flag == 0) {
317 BitfieldReader flags;
318 RETURN_ON_FALSE(flags.readFromStream(s));
319 gain_curve_num_control_points_minus_1[a] = flags.readBits(5);
320 gain_curve_use_pchip_slope_flag[a] = flags.readBits(1);
321 const auto reserved_zero = flags.readBits(2);
322 RETURN_ON_FALSE(reserved_zero == 0);
323 for (uint8_t c = 0; c < gain_curve_num_control_points_minus_1[a] + 1u; ++c) {
324 RETURN_ON_FALSE(SkStreamPriv::ReadU16BE(&s, &gain_curve_control_points_x[a][c]));
325 }
326 } else {
327 gain_curve_num_control_points_minus_1[a] = gain_curve_num_control_points_minus_1[0];
328 gain_curve_use_pchip_slope_flag[a] = gain_curve_use_pchip_slope_flag[0];
329 for (uint8_t c = 0; c < gain_curve_num_control_points_minus_1[a] + 1u; ++c) {
330 gain_curve_control_points_x[a][c] = gain_curve_control_points_x[0][c];
331 }
332 }
333 for (uint8_t c = 0; c < gain_curve_num_control_points_minus_1[a] + 1u; ++c) {
334 RETURN_ON_FALSE(SkStreamPriv::ReadU16BE(&s, &gain_curve_control_points_y[a][c]));
335 }
336 if (gain_curve_use_pchip_slope_flag[a] == 0) {
337 for (uint8_t c = 0; c < gain_curve_num_control_points_minus_1[a] + 1u; ++c) {
338 RETURN_ON_FALSE(SkStreamPriv::ReadU16BE(&s, &gain_curve_control_points_theta[a][c]));
339 }
340 }
341 return true;
342 }
343
344 void AgtmSyntax::write_gain_curve(uint8_t a, SkDynamicMemoryWStream& s) {
345 if (a == 0 || has_common_component_mix_params_flag == 0) {
346 BitfieldWriter flags;
347 flags.writeBits(gain_curve_num_control_points_minus_1[a], 5);
348 flags.writeBits(gain_curve_use_pchip_slope_flag[a], 1);
349 flags.padAndWriteToStream(s);
350 for (uint8_t c = 0; c < gain_curve_num_control_points_minus_1[a] + 1u; ++c) {
351 SkStreamPriv::WriteU16BE(&s, gain_curve_control_points_x[a][c]);
352 }
353 }
354 for (uint8_t c = 0; c < gain_curve_num_control_points_minus_1[a] + 1u; ++c) {
355 SkStreamPriv::WriteU16BE(&s, gain_curve_control_points_y[a][c]);
356 }
357 if (gain_curve_use_pchip_slope_flag[a] == 0) {
358 for (uint8_t c = 0; c < gain_curve_num_control_points_minus_1[a] + 1u; ++c) {
359 SkStreamPriv::WriteU16BE(&s, gain_curve_control_points_theta[a][c]);
360 }
361 }
362 }
363
364 } // namespace
365
366 namespace skhdr {
367
368 bool AdaptiveGlobalToneMap::parse(const SkData* data) {
369 if (data == nullptr) {
370 return false;
371 }
372 SkMemoryStream s(data->data(), data->size());
373
374 // Parse the syntax according to Clause C.2.
375 AgtmSyntax syntax;
376 memset(&syntax, 0, sizeof(syntax));
377 if (!syntax.parse_application_info(s)) {
378 return false;
379 }
380
381 // Apply the semantics to map syntax elements to metadata items according to Clause C.3.3.
382 if (syntax.has_custom_hdr_reference_white_flag == 1) {
383 fHdrReferenceWhite = uint16_to_float(syntax.hdr_reference_white, 1u, 50000u, 0u, 5.f);
384 } else {
385 fHdrReferenceWhite = kDefaultHdrReferenceWhite;
386 }
387 if (syntax.has_adaptive_tone_map_flag == 0) {
388 return true;
389 }
390 auto& hatm = fHeadroomAdaptiveToneMap.emplace(HeadroomAdaptiveToneMap{});
391
392 // Semantics from Clause C.3.4.
393 hatm.fBaselineHdrHeadroom =
394 uint16_to_float(syntax.baseline_hdr_headroom, 0u, 60000u, 0u, 10000.f);
395 if (syntax.use_reference_white_tone_mapping_flag == 1) {
396 AgtmHelpers::PopulateUsingRwtmo(hatm);
397 return true;
398 }
399
400 const uint8_t numAlternateImages = clamp(
401 syntax.num_alternate_images, 0u, HeadroomAdaptiveToneMap::kMaxNumAlternateImages);
402 hatm.fAlternateImages.resize(numAlternateImages);
403 for (size_t a = 0; a < numAlternateImages; ++a) {
404 hatm.fAlternateImages[a].fHdrHeadroom = uint16_to_float(
405 syntax.alternate_hdr_headrooms[a], 0u, 60000u, 0u, 10000.f);
406 }
407
408 // Semantics from Clause C.3.5.
409 switch (syntax.gain_application_space_chromaticities_flag) {
410 case 0:
411 hatm.fGainApplicationSpacePrimaries = SkNamedPrimaries::kRec709;
412 break;
413 case 1:
414 hatm.fGainApplicationSpacePrimaries = SkNamedPrimaries::kSMPTE_EG_432_1;
415 break;
416 case 2:
417 hatm.fGainApplicationSpacePrimaries = SkNamedPrimaries::kRec2020;
418 break;
419 case 3: {
420 hatm.fGainApplicationSpacePrimaries = {
421 .fRX = uint16_to_float(
422 syntax.gain_application_space_chromaticities[0], 0u, 50000u, 0u, 50000.f),
423 .fRY = uint16_to_float(
424 syntax.gain_application_space_chromaticities[1], 0u, 50000u, 0u, 50000.f),
425 .fGX = uint16_to_float(
426 syntax.gain_application_space_chromaticities[2], 0u, 50000u, 0u, 50000.f),
427 .fGY = uint16_to_float(
428 syntax.gain_application_space_chromaticities[3], 0u, 50000u, 0u, 50000.f),
429 .fBX = uint16_to_float(
430 syntax.gain_application_space_chromaticities[4], 0u, 50000u, 0u, 50000.f),
431 .fBY = uint16_to_float(
432 syntax.gain_application_space_chromaticities[5], 0u, 50000u, 0u, 50000.f),
433 .fWX = uint16_to_float(
434 syntax.gain_application_space_chromaticities[6], 0u, 50000u, 0u, 50000.f),
435 .fWY = uint16_to_float(
436 syntax.gain_application_space_chromaticities[7], 0u, 50000u, 0u, 50000.f),
437 };
438 break;
439 }
440 default:
441 // This should never be hit because syntax.gain_application_space_chromaticities_flag
442 // is read as 2 bits.
443 SkUNREACHABLE;
444 break;
445 }
446
447 // Semantics from Clause C.3.6.
448 for (uint8_t a = 0; a < numAlternateImages; ++a) {
449 auto& mix = hatm.fAlternateImages[a].fColorGainFunction.fComponentMixing;
450 switch (syntax.component_mixing_type[a]) {
451 case 0:
452 mix = {.fMax = 1.f};
453 break;
454 case 1:
455 mix = {.fComponent = 1.f};
456 break;
457 case 2:
458 mix = {
459 .fRed = 1.f / 6.f,
460 .fGreen = 1.f / 6.f,
461 .fBlue = 1.f / 6.f,
462 .fMax = 1.f / 2.f,
463 };
464 break;
465 case 3:
466 mix.fRed = uint16_to_float(
467 syntax.component_mixing_coefficient[a][0], 0u, 50000u, 0u, 50000.f);
468 mix.fGreen = uint16_to_float(
469 syntax.component_mixing_coefficient[a][1], 0u, 50000u, 0u, 50000.f);
470 mix.fBlue = uint16_to_float(
471 syntax.component_mixing_coefficient[a][2], 0u, 50000u, 0u, 50000.f);
472 mix.fMax = uint16_to_float(
473 syntax.component_mixing_coefficient[a][3], 0u, 50000u, 0u, 50000.f);
474 mix.fMin = uint16_to_float(
475 syntax.component_mixing_coefficient[a][4], 0u, 50000u, 0u, 50000.f);
476 mix.fComponent = uint16_to_float(
477 syntax.component_mixing_coefficient[a][5], 0u, 50000u, 0u, 50000.f);
478 break;
479 }
480 }
481
482 // Semantics from Clause C.3.7.
483 for (uint8_t a = 0; a < numAlternateImages; ++a) {
484 auto& cubic = hatm.fAlternateImages[a].fColorGainFunction.fGainCurve;
485 const uint8_t numControlPoints = syntax.gain_curve_num_control_points_minus_1[a] + 1u;
486 cubic.fControlPoints.resize(numControlPoints);
487 const float sign = syntax.baseline_hdr_headroom < syntax.alternate_hdr_headrooms[a] ?
488 1.f : -1.f;
489 for (uint8_t c = 0; c < numControlPoints; ++c) {
490 cubic.fControlPoints[c].fX = uint16_to_float(
491 syntax.gain_curve_control_points_x[a][c], 0u, 64000u, 0u, 1000.f);
492 cubic.fControlPoints[c].fY = sign * uint16_to_float(
493 syntax.gain_curve_control_points_y[a][c], 0u, 60000u, 0, 10000.f);
494 }
495 if (syntax.gain_curve_use_pchip_slope_flag[a] == 0) {
496 for (uint8_t c = 0; c < numControlPoints; ++c) {
497 const float theta = uint16_to_float(
498 syntax.gain_curve_control_points_theta[a][c],
499 1u, 35999u, 18000u, 36000.f / SK_FloatPI);
500 cubic.fControlPoints[c].fM = std::tan(theta);
501 }
502 } else {
503 AgtmHelpers::PopulateSlopeFromPCHIP(cubic);
504 }
505 }
506
507 // Reject any metadata that violates the normative constraints.
508 if (!AgtmHelpers::Validate(*this)) {
509 return false;
510 }
511
512 return true;
513 }
514
515 sk_sp<SkData> AdaptiveGlobalToneMap::serialize() const {
516 // Reject requests to serialize invalid metadata.
517 if (!AgtmHelpers::Validate(*this)) {
518 return nullptr;
519 }
520
521 AgtmSyntax syntax;
522 memset(&syntax, 0, sizeof(syntax));
523
524 // Populate the rest of `syntax` according to the semantics in Clause C.3.
525 syntax.application_version = 0;
526 syntax.minimum_application_version = 0;
527 syntax.has_custom_hdr_reference_white_flag = fHdrReferenceWhite != kDefaultHdrReferenceWhite;
528 if (syntax.has_custom_hdr_reference_white_flag) {
529 syntax.hdr_reference_white = float_to_uint16(fHdrReferenceWhite, 1u, 50000u, 0u, 5.f);
530 }
531
532 if (fHeadroomAdaptiveToneMap.has_value()) {
533 const auto& hatm = fHeadroomAdaptiveToneMap.value();
534 syntax.has_adaptive_tone_map_flag = true;
535
536 // Semantics from Clause C.3.4.
537 syntax.baseline_hdr_headroom =
538 float_to_uint16(hatm.fBaselineHdrHeadroom, 0u, 60000u, 0u, 10000.f);
539
540 // If the headroom-adaptive tone map is equal to RWTMO, then mark it as such.
541 syntax.use_reference_white_tone_mapping_flag = false;
542 {
543 auto hatm_rwtmo = hatm;
544 AgtmHelpers::PopulateUsingRwtmo(hatm_rwtmo);
545 syntax.use_reference_white_tone_mapping_flag = hatm == hatm_rwtmo;
546 }
547 SkASSERT(hatm.fAlternateImages.size() <= HeadroomAdaptiveToneMap::kMaxNumAlternateImages);
548 syntax.num_alternate_images = hatm.fAlternateImages.size();
549 for (size_t a = 0; a < hatm.fAlternateImages.size(); ++a) {
550 syntax.alternate_hdr_headrooms[a] = float_to_uint16(
551 hatm.fAlternateImages[a].fHdrHeadroom, 0u, 60000u, 0u, 10000.f);
552 }
553
554 // Semantics from Clause C.3.5.
555 if (hatm.fGainApplicationSpacePrimaries == SkNamedPrimaries::kRec709) {
556 syntax.gain_application_space_chromaticities_flag = 0;
557 } else if (hatm.fGainApplicationSpacePrimaries == SkNamedPrimaries::kSMPTE_EG_432_1) {
558 syntax.gain_application_space_chromaticities_flag = 1;
559 } else if (hatm.fGainApplicationSpacePrimaries == SkNamedPrimaries::kRec2020) {
560 syntax.gain_application_space_chromaticities_flag = 2;
561 } else {
562 syntax.gain_application_space_chromaticities_flag = 3;
563 }
564 if (syntax.gain_application_space_chromaticities_flag == 3) {
565 syntax.gain_application_space_chromaticities[0] = float_to_uint16(
566 hatm.fGainApplicationSpacePrimaries.fRX, 0u, 50000u, 0u, 50000.f);
567 syntax.gain_application_space_chromaticities[1] = float_to_uint16(
568 hatm.fGainApplicationSpacePrimaries.fRY, 0u, 50000u, 0u, 50000.f);
569 syntax.gain_application_space_chromaticities[2] = float_to_uint16(
570 hatm.fGainApplicationSpacePrimaries.fGX, 0u, 50000u, 0u, 50000.f);
571 syntax.gain_application_space_chromaticities[3] = float_to_uint16(
572 hatm.fGainApplicationSpacePrimaries.fGY, 0u, 50000u, 0u, 50000.f);
573 syntax.gain_application_space_chromaticities[4] = float_to_uint16(
574 hatm.fGainApplicationSpacePrimaries.fBX, 0u, 50000u, 0u, 50000.f);
575 syntax.gain_application_space_chromaticities[5] = float_to_uint16(
576 hatm.fGainApplicationSpacePrimaries.fBY, 0u, 50000u, 0u, 50000.f);
577 syntax.gain_application_space_chromaticities[6] = float_to_uint16(
578 hatm.fGainApplicationSpacePrimaries.fWX, 0u, 50000u, 0u, 50000.f);
579 syntax.gain_application_space_chromaticities[7] = float_to_uint16(
580 hatm.fGainApplicationSpacePrimaries.fWY, 0u, 50000u, 0u, 50000.f);
581 }
582
583 // Semantics from Clause C.3.6.
584 syntax.has_common_component_mix_params_flag = 0;
585 for (size_t a = 0; a < hatm.fAlternateImages.size(); ++a) {
586 auto& mix = hatm.fAlternateImages[a].fColorGainFunction.fComponentMixing;
587 if (mix.fRed == 0.f && mix.fGreen == 0.f && mix.fBlue == 0.f &&
588 mix.fMax == 1.f && mix.fMin == 0.f && mix.fComponent == 0.f) {
589 syntax.component_mixing_type[a] = 0;
590 } else if (mix.fRed == 0.f && mix.fGreen == 0.f && mix.fBlue == 0.f &&
591 mix.fMax == 0.f && mix.fMin == 0.f && mix.fComponent == 1.f) {
592 syntax.component_mixing_type[a] = 1;
593 } else if (mix.fRed == 1.f/6.f && mix.fGreen == 1.f/6.f && mix.fBlue == 1.f/6.f &&
594 mix.fMax == 1.f/2.f && mix.fMin == 0.f && mix.fComponent == 0.f) {
595 syntax.component_mixing_type[a] = 2;
596 } else {
597 syntax.component_mixing_type[a] = 3;
598 syntax.component_mixing_coefficient[a][0] =
599 float_to_uint16(mix.fRed, 0u, 50000u, 0u, 50000.f);
600 syntax.component_mixing_coefficient[a][1] =
601 float_to_uint16(mix.fGreen, 0u, 50000u, 0u, 50000.f);
602 syntax.component_mixing_coefficient[a][2] =
603 float_to_uint16(mix.fBlue, 0u, 50000u, 0u, 50000.f);
604 syntax.component_mixing_coefficient[a][3] =
605 float_to_uint16(mix.fMax, 0u, 50000u, 0u, 50000.f);
606 syntax.component_mixing_coefficient[a][4] =
607 float_to_uint16(mix.fMin, 0u, 50000u, 0u, 50000.f);
608 syntax.component_mixing_coefficient[a][5] =
609 float_to_uint16(mix.fComponent, 0u, 50000u, 0u, 50000.f);
610 for (uint8_t k = 0; k < AgtmSyntax::kNumMixCoefficients; ++k) {
611 syntax.has_component_mixing_coefficient_flag[a][k] =
612 syntax.component_mixing_coefficient[a][k] != 0;
613 }
614 }
615 }
616
617 // Semantics from Clause C.3.7.
618 syntax.has_common_curve_params_flag = 0;
619 for (size_t a = 0; a < hatm.fAlternateImages.size(); ++a) {
620 const auto& alt = hatm.fAlternateImages[a];
621 const float sign = hatm.fBaselineHdrHeadroom < alt.fHdrHeadroom ? 1.f : -1.f;
622
623 const auto& cubic = alt.fColorGainFunction.fGainCurve;
624 SkASSERT(GainCurve::kMinNumControlPoints <= cubic.fControlPoints.size());
625 SkASSERT(cubic.fControlPoints.size() <= GainCurve::kMaxNumControlPoints);
626 syntax.gain_curve_num_control_points_minus_1[a] = cubic.fControlPoints.size() - 1u;
627 // TODO(https://crbug.com/395659818): Identify when slope is equal to PCHIP to further
628 // compress the serialization.
629 syntax.gain_curve_use_pchip_slope_flag[a] = 0;
630 for (size_t c = 0; c < cubic.fControlPoints.size(); ++c) {
631 syntax.gain_curve_control_points_x[a][c] =
632 float_to_uint16(cubic.fControlPoints[c].fX, 0u, 64000u, 0u, 1000.f);
633 }
634 for (size_t c = 0; c < cubic.fControlPoints.size(); ++c) {
635 syntax.gain_curve_control_points_y[a][c] =
636 float_to_uint16(sign * cubic.fControlPoints[c].fY, 0u, 60000u, 0u, 10000.f);
637 }
638 for (size_t c = 0; c < cubic.fControlPoints.size(); ++c) {
639 float theta = std::atan(cubic.fControlPoints[c].fM);
640 syntax.gain_curve_control_points_theta[a][c] =
641 float_to_uint16(theta, 1u, 35999u, 18000u, 36000.f / SK_FloatPI);
642 }
643 }
644 }
645
646 // Write the syntax according to Clause C.2.
647 SkDynamicMemoryWStream s;
648 syntax.write_application_info(s);
649 return s.detachAsData();
650 }
651
652 } // namespace skhdr
653