File: hdr_metadata_agtm.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (336 lines) | stat: -rw-r--r-- 10,630 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/gfx/hdr_metadata_agtm.h"

#include "base/json/json_reader.h"
#include "base/logging.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkData.h"
#include "ui/gfx/hdr_metadata.h"

namespace gfx {

namespace {

bool ReadFloat(const base::DictValue* dict, const char* key, float& value) {
  if (!dict) {
    return false;
  }
  if (auto v = dict->FindDouble(key)) {
    value = v.value();
    return true;
  }
  return false;
}

// Read a piecewise cubic into a sampled SkImage
bool ReadPiecewiseCubic(const base::DictValue* dict, sk_sp<SkImage>& curve) {
  if (!dict) {
    DVLOG(1) << "Piecewise cubic is missing";
    return false;
  }
  // The parameters for the piecewise cubic segment.
  float m_min = 0.f, m_span = 0.f;
  float x0 = -1.f, y0 = 0.f, m0 = 0.f;
  float x1 = -1.f, y1 = 0.f, m1 = 0.f;

  // The output SkBitmap.
  const size_t kSamples = 1024;
  SkBitmap bm;
  if (!bm.tryAllocPixels(SkImageInfo::Make(kSamples, 1, kA16_unorm_SkColorType,
                                           kUnpremul_SkAlphaType))) {
    DVLOG(1) << "Failed to allocate pixels for gain curve";
    return false;
  }

  // The sample x value that is being written into `bm`.
  size_t xi = 0;

  // Function to evalue the current piecewise cubic segment.
  auto eval_piecewise_cubic = [&](float x) {
    // If x0 >= x1, then hold the y0 value. This can happen for repeated control
    // points, or initializing values beyond the control points.
    if (x0 >= x1) {
      return y0;
    }
    const float m0p = (m_min + m_span * m0) * (x1 - x0);
    const float m1p = (m_min + m_span * m1) * (x1 - x0);
    const float c3 = (2.0 * y0 + m0p - 2.0 * y1 + m1p);
    const float c2 = (-3.0 * y0 + 3.0 * y1 - 2.0 * m0p - m1p);
    const float c1 = m0p;
    const float c0 = y0;
    const float t = (x - x0) / (x1 - x0);
    return c0 + t * (c1 + t * (c2 + t * c3));
  };

  // Function to increment `xi` until it is past `x1`, writing values into `bm`
  // along the way.
  auto write_samples_through_x1 = [&]() {
    while (xi < kSamples && xi / (kSamples - 1.f) < x1) {
      const float y = eval_piecewise_cubic(xi / (kSamples - 1.f));
      *bm.pixmap().writable_addr16(xi, 0) =
          std::round(65535.f * std::clamp(y, 0.f, 1.f));
      xi += 1;
    }
  };

  // Read the parameters and control points, writing to `bm` along the way.
  if (!ReadFloat(dict, "m_min", m_min) && !ReadFloat(dict, "m_span", m_span)) {
    DVLOG(1) << "Missing slope minimum or span";
    return false;
  }
  const auto* cp_list = dict->FindList("control_points");
  if (!cp_list || cp_list->size() < 1 || cp_list->size() > 64) {
    DVLOG(1) << "Control point list missing or incorrect size";
    return false;
  }
  bool is_first_control_point = true;
  for (const auto& control_point : *cp_list) {
    x0 = x1;
    y0 = y1;
    m0 = m1;
    const auto* control_point_dict = control_point.GetIfDict();
    if (!ReadFloat(control_point_dict, "x", x1) ||
        !ReadFloat(control_point_dict, "y", y1) ||
        !ReadFloat(control_point_dict, "m", m1)) {
      DVLOG(1) << "Control point missing x, y, or m value";
      return false;
    }
    // Repeat the first control point, so that the piecewise cubic segment
    // function will evaluate to a constant.
    if (is_first_control_point) {
      is_first_control_point = false;
      x0 = x1;
      y0 = y1;
      m0 = m1;
    }
    // Samples must be sorted in increasing order.
    if (x0 > x1) {
      DVLOG(1) << "Sample x values not sorted";
      return false;
    }
    // The function must have C0 continuity.
    if (x0 == x1 && y0 != y1) {
      DVLOG(1) << "Function not continuous";
      return false;
    }
    // Write and increment xi until we need to read past x1.
    write_samples_through_x1();
  }

  // Write all samples past the last control point. Repeat the last control
  // point, so that the piecewise cubic segment function will evaluate to a
  // constant.
  x0 = x1;
  y0 = y1;
  m0 = m1;
  write_samples_through_x1();

  curve = SkImages::RasterFromPixmapCopy(bm.pixmap());
  return true;
}

bool ReadAgtmAlternate(const base::DictValue* dict,
                       HdrMetadataAgtmParsed::Alternate& altr) {
  if (!ReadFloat(dict, "hdr_headroom", altr.hdr_headroom)) {
    DVLOG(1) << "Alternate representation missing HDR headroom";
    return false;
  }
  if (const auto* v = dict->FindDict("component_mix_params")) {
    if (!ReadFloat(v, "red", altr.mix_rgbx[0]) &&
        !ReadFloat(v, "green", altr.mix_rgbx[1]) &&
        !ReadFloat(v, "blue", altr.mix_rgbx[2]) &&
        !ReadFloat(v, "max", altr.mix_Mmcx[0]) &&
        !ReadFloat(v, "min", altr.mix_Mmcx[1]) &&
        !ReadFloat(v, "component", altr.mix_Mmcx[2])) {
      DVLOG(1) << "Alternate missing component mix params";
      return false;
    }
  } else {
    DVLOG(1) << "Alternate missing component mix dictionary";
    return false;
  }
  if (!ReadPiecewiseCubic(dict->FindDict("piecewise_cubic"), altr.curve)) {
    DVLOG(1) << "Failed to piecewise cubic";
    return false;
  }
  return true;
}

bool ReadAgtmRoot(const base::Value& value, HdrMetadataAgtmParsed& params) {
  const auto* dict = value.GetIfDict();
  if (!dict) {
    DVLOG(1) << "Agtm root is not dictionary";
    return false;
  }
  if (!ReadFloat(dict, "hdr_reference_white", params.hdr_reference_white) ||
      !ReadFloat(dict, "baseline_hdr_headroom", params.baseline_hdr_headroom) ||
      !ReadFloat(dict, "baseline_max_component",
                 params.baseline_max_component) ||
      !ReadFloat(dict, "gain_min", params.gain_min) ||
      !ReadFloat(dict, "gain_span", params.gain_span) ||
      !ReadFloat(dict, "gain_application_offset",
                 params.gain_application_offset)) {
    DVLOG(1) << "Required values are absent";
    return false;
  }
  if (auto v = dict->FindInt("gain_application_space_primaries")) {
    params.gain_application_color_space =
        SkColorSpace::MakeCICP(static_cast<SkNamedPrimaries::CicpId>(v.value()),
                               SkNamedTransferFn::CicpId::kLinear);
  }
  if (!params.gain_application_color_space) {
    DVLOG(1) << "Invalid or absent gain application space primaries";
    return false;
  }
  const auto* altr_list = dict->FindList("alternates");
  if (altr_list) {
    if (altr_list->size() > 4) {
      DVLOG(1) << "Too many alternates";
      return false;
    }
    for (const auto& altr_value : *altr_list) {
      HdrMetadataAgtmParsed::Alternate altr;
      if (!ReadAgtmAlternate(altr_value.GetIfDict(), altr)) {
        DVLOG(1) << "Failed to read alternate parameters";
        return false;
      }
      params.alternates.push_back(altr);
    }
  }
  return true;
}

}  // namespace

HdrMetadataAgtmParsed::HdrMetadataAgtmParsed() = default;
HdrMetadataAgtmParsed::~HdrMetadataAgtmParsed() = default;

bool HdrMetadataAgtmParsed::Parse(const HdrMetadataAgtm& agtm) {
  if (!HdrMetadataAgtm::IsEnabled()) {
    return false;
  }
  if (!agtm.payload) {
    DVLOG(1) << "Empty AGTM payload";
    return false;
  }
  auto value = base::JSONReader::Read(
      std::string_view(reinterpret_cast<const char*>(agtm.payload->data()),
                       agtm.payload->size()));
  if (!value) {
    DVLOG(1) << "Failed to parse AGTM metadata JSON";
    return false;
  }
  if (!ReadAgtmRoot(value.value(), *this)) {
    return false;
  }
  return true;
}

void HdrMetadataAgtmParsed::ComputeAlternateWeights(float H_target,
                                                    size_t& i,
                                                    float& w_i,
                                                    size_t& j,
                                                    float& w_j) const {
  const float H_base = baseline_hdr_headroom;

  // Let H_i and H_j be the HDR headrooms of the ith and jth representation.
  // Set them to the same value to indicate that the ith representation should
  // be used in full.
  i = j = kBaselineIndex;
  float H_i = H_base;
  float H_j = H_base;

  // Special-case the absence of any alternate representations.
  if (alternates.empty()) {
    w_i = w_j = 0.f;
    return;
  }

  for (i = 0; i < alternates.size(); ++i) {
    j = kBaselineIndex;
    H_i = alternates[i].hdr_headroom;
    H_j = H_base;

    // Mix of i = 0 and potentially baseline
    if (H_target <= H_i) {
      DCHECK_EQ(i, 0u);
      j = kBaselineIndex;
      if (H_base <= H_i) {
        H_j = H_base;
      } else {
        H_j = H_i;
      }
      break;
    }

    // Mix of i = N-1 and potentially baseline.
    if (i == alternates.size() - 1) {
      DCHECK_GT(H_target, H_i);
      j = kBaselineIndex;
      if (H_base >= H_i) {
        H_j = H_base;
      } else {
        H_j = H_i;
      }
      break;
    }

    // Consider the interval between alternate representations i and i+1 only if
    // H_target is in that interval.
    j = i + 1;
    H_j = alternates[j].hdr_headroom;
    if (H_i <= H_target && H_target <= H_j) {
      // If it's the case that i < target < base < i+1, then we will only mix
      // i and base.
      if (H_i <= H_target && H_target <= H_base && H_base <= H_j) {
        j = kBaselineIndex;
        H_j = H_base;
      }

      // If it's the case that i < base < target < i+1, then we will only mix
      // i+1 and base.
      if (H_i <= H_base && H_base <= H_target && H_target <= H_j) {
        i = j;
        H_i = H_j;
        j = kBaselineIndex;
        H_j = H_base;
        break;
      }

      // Otherwise, it's the case that i < target < i+1 and base isn't in that
      // interval.
      DCHECK(H_i <= H_target && H_target <= H_j);
      break;
    }
  }

  // Compute the weights for the two representations.
  if (H_j == H_i) {
    w_i = 1.f;
  } else {
    w_i = std::min(std::max((H_target - H_j) / (H_i - H_j), 0.f), 1.f);
  }
  w_j = 1.f - w_i;

  // Zero out baseline weights.
  if (i == kBaselineIndex) {
    w_i = 0.f;
  }
  if (j == kBaselineIndex) {
    w_j = 0.f;
  }
}

HdrMetadataAgtmParsed::Alternate::Alternate() = default;
HdrMetadataAgtmParsed::Alternate::Alternate(const Alternate&) = default;
HdrMetadataAgtmParsed::Alternate::Alternate(Alternate&&) = default;
HdrMetadataAgtmParsed::Alternate& HdrMetadataAgtmParsed::Alternate::operator=(
    const Alternate&) = default;
HdrMetadataAgtmParsed::Alternate& HdrMetadataAgtmParsed::Alternate::operator=(
    Alternate&&) = default;
HdrMetadataAgtmParsed::Alternate::~Alternate() = default;

}  // namespace gfx