blob: e41b645cce073d4487976044197fd3a4b9aedc63 [file] [log] [blame]
Dichen Zhange46f9bb2023-02-23 19:34:53 +00001/*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Nick Deakin0db53ee2023-05-19 17:14:45 -040017#ifndef USE_BIG_ENDIAN
18#define USE_BIG_ENDIAN true
19#endif
20
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000021#include <ultrahdr/icc.h>
Dichen Zhange46f9bb2023-02-23 19:34:53 +000022#include <vector>
23#include <utils/Log.h>
24
25#ifndef FLT_MAX
26#define FLT_MAX 0x1.fffffep127f
27#endif
28
Dichen Zhangdbceb0e2023-04-14 19:03:18 +000029namespace android::ultrahdr {
Dichen Zhange46f9bb2023-02-23 19:34:53 +000030static void Matrix3x3_apply(const Matrix3x3* m, float* x) {
31 float y0 = x[0] * m->vals[0][0] + x[1] * m->vals[0][1] + x[2] * m->vals[0][2];
32 float y1 = x[0] * m->vals[1][0] + x[1] * m->vals[1][1] + x[2] * m->vals[1][2];
33 float y2 = x[0] * m->vals[2][0] + x[1] * m->vals[2][1] + x[2] * m->vals[2][2];
34 x[0] = y0;
35 x[1] = y1;
36 x[2] = y2;
37}
38
39bool Matrix3x3_invert(const Matrix3x3* src, Matrix3x3* dst) {
40 double a00 = src->vals[0][0],
41 a01 = src->vals[1][0],
42 a02 = src->vals[2][0],
43 a10 = src->vals[0][1],
44 a11 = src->vals[1][1],
45 a12 = src->vals[2][1],
46 a20 = src->vals[0][2],
47 a21 = src->vals[1][2],
48 a22 = src->vals[2][2];
49
50 double b0 = a00*a11 - a01*a10,
51 b1 = a00*a12 - a02*a10,
52 b2 = a01*a12 - a02*a11,
53 b3 = a20,
54 b4 = a21,
55 b5 = a22;
56
57 double determinant = b0*b5
58 - b1*b4
59 + b2*b3;
60
61 if (determinant == 0) {
62 return false;
63 }
64
65 double invdet = 1.0 / determinant;
66 if (invdet > +FLT_MAX || invdet < -FLT_MAX || !isfinitef_((float)invdet)) {
67 return false;
68 }
69
70 b0 *= invdet;
71 b1 *= invdet;
72 b2 *= invdet;
73 b3 *= invdet;
74 b4 *= invdet;
75 b5 *= invdet;
76
77 dst->vals[0][0] = (float)( a11*b5 - a12*b4 );
78 dst->vals[1][0] = (float)( a02*b4 - a01*b5 );
79 dst->vals[2][0] = (float)( + b2 );
80 dst->vals[0][1] = (float)( a12*b3 - a10*b5 );
81 dst->vals[1][1] = (float)( a00*b5 - a02*b3 );
82 dst->vals[2][1] = (float)( - b1 );
83 dst->vals[0][2] = (float)( a10*b4 - a11*b3 );
84 dst->vals[1][2] = (float)( a01*b3 - a00*b4 );
85 dst->vals[2][2] = (float)( + b0 );
86
87 for (int r = 0; r < 3; ++r)
88 for (int c = 0; c < 3; ++c) {
89 if (!isfinitef_(dst->vals[r][c])) {
90 return false;
91 }
92 }
93 return true;
94}
95
96static Matrix3x3 Matrix3x3_concat(const Matrix3x3* A, const Matrix3x3* B) {
97 Matrix3x3 m = { { { 0,0,0 },{ 0,0,0 },{ 0,0,0 } } };
98 for (int r = 0; r < 3; r++)
99 for (int c = 0; c < 3; c++) {
100 m.vals[r][c] = A->vals[r][0] * B->vals[0][c]
101 + A->vals[r][1] * B->vals[1][c]
102 + A->vals[r][2] * B->vals[2][c];
103 }
104 return m;
105}
106
107static void float_XYZD50_to_grid16_lab(const float* xyz_float, uint8_t* grid16_lab) {
108 float v[3] = {
109 xyz_float[0] / kD50_x,
110 xyz_float[1] / kD50_y,
111 xyz_float[2] / kD50_z,
112 };
113 for (size_t i = 0; i < 3; ++i) {
114 v[i] = v[i] > 0.008856f ? cbrtf(v[i]) : v[i] * 7.787f + (16 / 116.0f);
115 }
116 const float L = v[1] * 116.0f - 16.0f;
117 const float a = (v[0] - v[1]) * 500.0f;
118 const float b = (v[1] - v[2]) * 200.0f;
119 const float Lab_unorm[3] = {
120 L * (1 / 100.f),
121 (a + 128.0f) * (1 / 255.0f),
122 (b + 128.0f) * (1 / 255.0f),
123 };
124 // This will encode L=1 as 0xFFFF. This matches how skcms will interpret the
125 // table, but the spec appears to indicate that the value should be 0xFF00.
126 // https://crbug.com/skia/13807
127 for (size_t i = 0; i < 3; ++i) {
128 reinterpret_cast<uint16_t*>(grid16_lab)[i] =
129 Endian_SwapBE16(float_round_to_unorm16(Lab_unorm[i]));
130 }
131}
132
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000133std::string IccHelper::get_desc_string(const ultrahdr_transfer_function tf,
134 const ultrahdr_color_gamut gamut) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000135 std::string result;
136 switch (gamut) {
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000137 case ULTRAHDR_COLORGAMUT_BT709:
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000138 result += "sRGB";
139 break;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000140 case ULTRAHDR_COLORGAMUT_P3:
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000141 result += "Display P3";
142 break;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000143 case ULTRAHDR_COLORGAMUT_BT2100:
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000144 result += "Rec2020";
145 break;
146 default:
147 result += "Unknown";
148 break;
149 }
150 result += " Gamut with ";
151 switch (tf) {
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000152 case ULTRAHDR_TF_SRGB:
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000153 result += "sRGB";
154 break;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000155 case ULTRAHDR_TF_LINEAR:
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000156 result += "Linear";
157 break;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000158 case ULTRAHDR_TF_PQ:
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000159 result += "PQ";
160 break;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000161 case ULTRAHDR_TF_HLG:
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000162 result += "HLG";
163 break;
164 default:
165 result += "Unknown";
166 break;
167 }
168 result += " Transfer";
169 return result;
170}
171
172sp<DataStruct> IccHelper::write_text_tag(const char* text) {
173 uint32_t text_length = strlen(text);
174 uint32_t header[] = {
175 Endian_SwapBE32(kTAG_TextType), // Type signature
176 0, // Reserved
177 Endian_SwapBE32(1), // Number of records
178 Endian_SwapBE32(12), // Record size (must be 12)
179 Endian_SwapBE32(SetFourByteTag('e', 'n', 'U', 'S')), // English USA
180 Endian_SwapBE32(2 * text_length), // Length of string in bytes
181 Endian_SwapBE32(28), // Offset of string
182 };
183
184 uint32_t total_length = text_length * 2 + sizeof(header);
185 total_length = (((total_length + 2) >> 2) << 2); // 4 aligned
Ram Mohane69d9d22023-06-02 17:44:45 +0530186 sp<DataStruct> dataStruct = sp<DataStruct>::make(total_length);
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000187
188 if (!dataStruct->write(header, sizeof(header))) {
189 ALOGE("write_text_tag(): error in writing data");
190 return dataStruct;
191 }
192
193 for (size_t i = 0; i < text_length; i++) {
194 // Convert ASCII to big-endian UTF-16.
195 dataStruct->write8(0);
196 dataStruct->write8(text[i]);
197 }
198
199 return dataStruct;
200}
201
202sp<DataStruct> IccHelper::write_xyz_tag(float x, float y, float z) {
203 uint32_t data[] = {
204 Endian_SwapBE32(kXYZ_PCSSpace),
205 0,
206 static_cast<uint32_t>(Endian_SwapBE32(float_round_to_fixed(x))),
207 static_cast<uint32_t>(Endian_SwapBE32(float_round_to_fixed(y))),
208 static_cast<uint32_t>(Endian_SwapBE32(float_round_to_fixed(z))),
209 };
Ram Mohane69d9d22023-06-02 17:44:45 +0530210 sp<DataStruct> dataStruct = sp<DataStruct>::make(sizeof(data));
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000211 dataStruct->write(&data, sizeof(data));
212 return dataStruct;
213}
214
215sp<DataStruct> IccHelper::write_trc_tag(const int table_entries, const void* table_16) {
216 int total_length = 4 + 4 + 4 + table_entries * 2;
217 total_length = (((total_length + 2) >> 2) << 2); // 4 aligned
Ram Mohane69d9d22023-06-02 17:44:45 +0530218 sp<DataStruct> dataStruct = sp<DataStruct>::make(total_length);
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000219 dataStruct->write32(Endian_SwapBE32(kTAG_CurveType)); // Type
Dichen Zhang4d2b74a2023-06-14 22:58:09 +0000220 dataStruct->write32(0); // Reserved
221 dataStruct->write32(Endian_SwapBE32(table_entries)); // Value count
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000222 for (size_t i = 0; i < table_entries; ++i) {
223 uint16_t value = reinterpret_cast<const uint16_t*>(table_16)[i];
224 dataStruct->write16(value);
225 }
226 return dataStruct;
227}
228
Dichen Zhang4d2b74a2023-06-14 22:58:09 +0000229sp<DataStruct> IccHelper::write_trc_tag(const TransferFunction& fn) {
230 if (fn.a == 1.f && fn.b == 0.f && fn.c == 0.f
231 && fn.d == 0.f && fn.e == 0.f && fn.f == 0.f) {
232 int total_length = 16;
233 sp<DataStruct> dataStruct = new DataStruct(total_length);
234 dataStruct->write32(Endian_SwapBE32(kTAG_ParaCurveType)); // Type
235 dataStruct->write32(0); // Reserved
236 dataStruct->write32(Endian_SwapBE16(kExponential_ParaCurveType));
237 dataStruct->write32(Endian_SwapBE32(float_round_to_fixed(fn.g)));
238 return dataStruct;
239 }
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000240
Dichen Zhang4d2b74a2023-06-14 22:58:09 +0000241 int total_length = 40;
242 sp<DataStruct> dataStruct = new DataStruct(total_length);
243 dataStruct->write32(Endian_SwapBE32(kTAG_ParaCurveType)); // Type
244 dataStruct->write32(0); // Reserved
245 dataStruct->write32(Endian_SwapBE16(kGABCDEF_ParaCurveType));
246 dataStruct->write32(Endian_SwapBE32(float_round_to_fixed(fn.g)));
247 dataStruct->write32(Endian_SwapBE32(float_round_to_fixed(fn.a)));
248 dataStruct->write32(Endian_SwapBE32(float_round_to_fixed(fn.b)));
249 dataStruct->write32(Endian_SwapBE32(float_round_to_fixed(fn.c)));
250 dataStruct->write32(Endian_SwapBE32(float_round_to_fixed(fn.d)));
251 dataStruct->write32(Endian_SwapBE32(float_round_to_fixed(fn.e)));
252 dataStruct->write32(Endian_SwapBE32(float_round_to_fixed(fn.f)));
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000253 return dataStruct;
254}
255
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000256float IccHelper::compute_tone_map_gain(const ultrahdr_transfer_function tf, float L) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000257 if (L <= 0.f) {
258 return 1.f;
259 }
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000260 if (tf == ULTRAHDR_TF_PQ) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000261 // The PQ transfer function will map to the range [0, 1]. Linearly scale
262 // it up to the range [0, 10,000/203]. We will then tone map that back
263 // down to [0, 1].
264 constexpr float kInputMaxLuminance = 10000 / 203.f;
265 constexpr float kOutputMaxLuminance = 1.0;
266 L *= kInputMaxLuminance;
267
268 // Compute the tone map gain which will tone map from 10,000/203 to 1.0.
269 constexpr float kToneMapA = kOutputMaxLuminance / (kInputMaxLuminance * kInputMaxLuminance);
270 constexpr float kToneMapB = 1.f / kOutputMaxLuminance;
271 return kInputMaxLuminance * (1.f + kToneMapA * L) / (1.f + kToneMapB * L);
272 }
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000273 if (tf == ULTRAHDR_TF_HLG) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000274 // Let Lw be the brightness of the display in nits.
275 constexpr float Lw = 203.f;
276 const float gamma = 1.2f + 0.42f * std::log(Lw / 1000.f) / std::log(10.f);
277 return std::pow(L, gamma - 1.f);
278 }
279 return 1.f;
280}
281
282sp<DataStruct> IccHelper::write_cicp_tag(uint32_t color_primaries,
283 uint32_t transfer_characteristics) {
284 int total_length = 12; // 4 + 4 + 1 + 1 + 1 + 1
Ram Mohane69d9d22023-06-02 17:44:45 +0530285 sp<DataStruct> dataStruct = sp<DataStruct>::make(total_length);
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000286 dataStruct->write32(Endian_SwapBE32(kTAG_cicp)); // Type signature
287 dataStruct->write32(0); // Reserved
288 dataStruct->write8(color_primaries); // Color primaries
289 dataStruct->write8(transfer_characteristics); // Transfer characteristics
290 dataStruct->write8(0); // RGB matrix
291 dataStruct->write8(1); // Full range
292 return dataStruct;
293}
294
295void IccHelper::compute_lut_entry(const Matrix3x3& src_to_XYZD50, float rgb[3]) {
296 // Compute the matrices to convert from source to Rec2020, and from Rec2020 to XYZD50.
297 Matrix3x3 src_to_rec2020;
298 const Matrix3x3 rec2020_to_XYZD50 = kRec2020;
299 {
300 Matrix3x3 XYZD50_to_rec2020;
301 Matrix3x3_invert(&rec2020_to_XYZD50, &XYZD50_to_rec2020);
302 src_to_rec2020 = Matrix3x3_concat(&XYZD50_to_rec2020, &src_to_XYZD50);
303 }
304
305 // Convert the source signal to linear.
306 for (size_t i = 0; i < kNumChannels; ++i) {
307 rgb[i] = pqOetf(rgb[i]);
308 }
309
310 // Convert source gamut to Rec2020.
311 Matrix3x3_apply(&src_to_rec2020, rgb);
312
313 // Compute the luminance of the signal.
314 float L = bt2100Luminance({{{rgb[0], rgb[1], rgb[2]}}});
315
316 // Compute the tone map gain based on the luminance.
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000317 float tone_map_gain = compute_tone_map_gain(ULTRAHDR_TF_PQ, L);
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000318
319 // Apply the tone map gain.
320 for (size_t i = 0; i < kNumChannels; ++i) {
321 rgb[i] *= tone_map_gain;
322 }
323
324 // Convert from Rec2020-linear to XYZD50.
325 Matrix3x3_apply(&rec2020_to_XYZD50, rgb);
326}
327
328sp<DataStruct> IccHelper::write_clut(const uint8_t* grid_points, const uint8_t* grid_16) {
329 uint32_t value_count = kNumChannels;
330 for (uint32_t i = 0; i < kNumChannels; ++i) {
331 value_count *= grid_points[i];
332 }
333
334 int total_length = 20 + 2 * value_count;
335 total_length = (((total_length + 2) >> 2) << 2); // 4 aligned
Ram Mohane69d9d22023-06-02 17:44:45 +0530336 sp<DataStruct> dataStruct = sp<DataStruct>::make(total_length);
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000337
338 for (size_t i = 0; i < 16; ++i) {
339 dataStruct->write8(i < kNumChannels ? grid_points[i] : 0); // Grid size
340 }
341 dataStruct->write8(2); // Grid byte width (always 16-bit)
342 dataStruct->write8(0); // Reserved
343 dataStruct->write8(0); // Reserved
344 dataStruct->write8(0); // Reserved
345
346 for (uint32_t i = 0; i < value_count; ++i) {
347 uint16_t value = reinterpret_cast<const uint16_t*>(grid_16)[i];
348 dataStruct->write16(value);
349 }
350
351 return dataStruct;
352}
353
354sp<DataStruct> IccHelper::write_mAB_or_mBA_tag(uint32_t type,
355 bool has_a_curves,
356 const uint8_t* grid_points,
357 const uint8_t* grid_16) {
358 const size_t b_curves_offset = 32;
359 sp<DataStruct> b_curves_data[kNumChannels];
360 sp<DataStruct> a_curves_data[kNumChannels];
361 size_t clut_offset = 0;
362 sp<DataStruct> clut;
363 size_t a_curves_offset = 0;
364
365 // The "B" curve is required.
366 for (size_t i = 0; i < kNumChannels; ++i) {
Dichen Zhang4d2b74a2023-06-14 22:58:09 +0000367 b_curves_data[i] = write_trc_tag(kLinear_TransFun);
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000368 }
369
370 // The "A" curve and CLUT are optional.
371 if (has_a_curves) {
372 clut_offset = b_curves_offset;
373 for (size_t i = 0; i < kNumChannels; ++i) {
374 clut_offset += b_curves_data[i]->getLength();
375 }
376 clut = write_clut(grid_points, grid_16);
377
378 a_curves_offset = clut_offset + clut->getLength();
379 for (size_t i = 0; i < kNumChannels; ++i) {
Dichen Zhang4d2b74a2023-06-14 22:58:09 +0000380 a_curves_data[i] = write_trc_tag(kLinear_TransFun);
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000381 }
382 }
383
384 int total_length = b_curves_offset;
385 for (size_t i = 0; i < kNumChannels; ++i) {
386 total_length += b_curves_data[i]->getLength();
387 }
388 if (has_a_curves) {
389 total_length += clut->getLength();
390 for (size_t i = 0; i < kNumChannels; ++i) {
391 total_length += a_curves_data[i]->getLength();
392 }
393 }
Ram Mohane69d9d22023-06-02 17:44:45 +0530394 sp<DataStruct> dataStruct = sp<DataStruct>::make(total_length);
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000395 dataStruct->write32(Endian_SwapBE32(type)); // Type signature
396 dataStruct->write32(0); // Reserved
397 dataStruct->write8(kNumChannels); // Input channels
398 dataStruct->write8(kNumChannels); // Output channels
399 dataStruct->write16(0); // Reserved
400 dataStruct->write32(Endian_SwapBE32(b_curves_offset)); // B curve offset
401 dataStruct->write32(Endian_SwapBE32(0)); // Matrix offset (ignored)
402 dataStruct->write32(Endian_SwapBE32(0)); // M curve offset (ignored)
403 dataStruct->write32(Endian_SwapBE32(clut_offset)); // CLUT offset
404 dataStruct->write32(Endian_SwapBE32(a_curves_offset)); // A curve offset
405 for (size_t i = 0; i < kNumChannels; ++i) {
406 if (dataStruct->write(b_curves_data[i]->getData(), b_curves_data[i]->getLength())) {
407 return dataStruct;
408 }
409 }
410 if (has_a_curves) {
411 dataStruct->write(clut->getData(), clut->getLength());
412 for (size_t i = 0; i < kNumChannels; ++i) {
413 dataStruct->write(a_curves_data[i]->getData(), a_curves_data[i]->getLength());
414 }
415 }
416 return dataStruct;
417}
418
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000419sp<DataStruct> IccHelper::writeIccProfile(ultrahdr_transfer_function tf,
420 ultrahdr_color_gamut gamut) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000421 ICCHeader header;
422
423 std::vector<std::pair<uint32_t, sp<DataStruct>>> tags;
424
425 // Compute profile description tag
426 std::string desc = get_desc_string(tf, gamut);
427
428 tags.emplace_back(kTAG_desc, write_text_tag(desc.c_str()));
429
430 Matrix3x3 toXYZD50;
431 switch (gamut) {
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000432 case ULTRAHDR_COLORGAMUT_BT709:
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000433 toXYZD50 = kSRGB;
434 break;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000435 case ULTRAHDR_COLORGAMUT_P3:
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000436 toXYZD50 = kDisplayP3;
437 break;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000438 case ULTRAHDR_COLORGAMUT_BT2100:
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000439 toXYZD50 = kRec2020;
440 break;
441 default:
442 // Should not fall here.
Ram Mohane69d9d22023-06-02 17:44:45 +0530443 return nullptr;
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000444 }
445
446 // Compute primaries.
447 {
448 tags.emplace_back(kTAG_rXYZ,
449 write_xyz_tag(toXYZD50.vals[0][0], toXYZD50.vals[1][0], toXYZD50.vals[2][0]));
450 tags.emplace_back(kTAG_gXYZ,
451 write_xyz_tag(toXYZD50.vals[0][1], toXYZD50.vals[1][1], toXYZD50.vals[2][1]));
452 tags.emplace_back(kTAG_bXYZ,
453 write_xyz_tag(toXYZD50.vals[0][2], toXYZD50.vals[1][2], toXYZD50.vals[2][2]));
454 }
455
456 // Compute white point tag (must be D50)
457 tags.emplace_back(kTAG_wtpt, write_xyz_tag(kD50_x, kD50_y, kD50_z));
458
459 // Compute transfer curves.
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000460 if (tf != ULTRAHDR_TF_PQ) {
461 if (tf == ULTRAHDR_TF_HLG) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000462 std::vector<uint8_t> trc_table;
463 trc_table.resize(kTrcTableSize * 2);
464 for (uint32_t i = 0; i < kTrcTableSize; ++i) {
465 float x = i / (kTrcTableSize - 1.f);
466 float y = hlgOetf(x);
467 y *= compute_tone_map_gain(tf, y);
468 float_to_table16(y, &trc_table[2 * i]);
469 }
470
471 tags.emplace_back(kTAG_rTRC,
472 write_trc_tag(kTrcTableSize, reinterpret_cast<uint8_t*>(trc_table.data())));
473 tags.emplace_back(kTAG_gTRC,
474 write_trc_tag(kTrcTableSize, reinterpret_cast<uint8_t*>(trc_table.data())));
475 tags.emplace_back(kTAG_bTRC,
476 write_trc_tag(kTrcTableSize, reinterpret_cast<uint8_t*>(trc_table.data())));
477 } else {
Dichen Zhang4d2b74a2023-06-14 22:58:09 +0000478 tags.emplace_back(kTAG_rTRC, write_trc_tag(kSRGB_TransFun));
479 tags.emplace_back(kTAG_gTRC, write_trc_tag(kSRGB_TransFun));
480 tags.emplace_back(kTAG_bTRC, write_trc_tag(kSRGB_TransFun));
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000481 }
482 }
483
484 // Compute CICP.
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000485 if (tf == ULTRAHDR_TF_HLG || tf == ULTRAHDR_TF_PQ) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000486 // The CICP tag is present in ICC 4.4, so update the header's version.
487 header.version = Endian_SwapBE32(0x04400000);
488
489 uint32_t color_primaries = 0;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000490 if (gamut == ULTRAHDR_COLORGAMUT_BT709) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000491 color_primaries = kCICPPrimariesSRGB;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000492 } else if (gamut == ULTRAHDR_COLORGAMUT_P3) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000493 color_primaries = kCICPPrimariesP3;
494 }
495
496 uint32_t transfer_characteristics = 0;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000497 if (tf == ULTRAHDR_TF_SRGB) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000498 transfer_characteristics = kCICPTrfnSRGB;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000499 } else if (tf == ULTRAHDR_TF_LINEAR) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000500 transfer_characteristics = kCICPTrfnLinear;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000501 } else if (tf == ULTRAHDR_TF_PQ) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000502 transfer_characteristics = kCICPTrfnPQ;
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000503 } else if (tf == ULTRAHDR_TF_HLG) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000504 transfer_characteristics = kCICPTrfnHLG;
505 }
506 tags.emplace_back(kTAG_cicp, write_cicp_tag(color_primaries, transfer_characteristics));
507 }
508
509 // Compute A2B0.
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000510 if (tf == ULTRAHDR_TF_PQ) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000511 std::vector<uint8_t> a2b_grid;
512 a2b_grid.resize(kGridSize * kGridSize * kGridSize * kNumChannels * 2);
513 size_t a2b_grid_index = 0;
514 for (uint32_t r_index = 0; r_index < kGridSize; ++r_index) {
515 for (uint32_t g_index = 0; g_index < kGridSize; ++g_index) {
516 for (uint32_t b_index = 0; b_index < kGridSize; ++b_index) {
517 float rgb[3] = {
518 r_index / (kGridSize - 1.f),
519 g_index / (kGridSize - 1.f),
520 b_index / (kGridSize - 1.f),
521 };
522 compute_lut_entry(toXYZD50, rgb);
523 float_XYZD50_to_grid16_lab(rgb, &a2b_grid[a2b_grid_index]);
524 a2b_grid_index += 6;
525 }
526 }
527 }
528 const uint8_t* grid_16 = reinterpret_cast<const uint8_t*>(a2b_grid.data());
529
530 uint8_t grid_points[kNumChannels];
531 for (size_t i = 0; i < kNumChannels; ++i) {
532 grid_points[i] = kGridSize;
533 }
534
535 auto a2b_data = write_mAB_or_mBA_tag(kTAG_mABType,
536 /* has_a_curves */ true,
537 grid_points,
538 grid_16);
539 tags.emplace_back(kTAG_A2B0, std::move(a2b_data));
540 }
541
542 // Compute B2A0.
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000543 if (tf == ULTRAHDR_TF_PQ) {
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000544 auto b2a_data = write_mAB_or_mBA_tag(kTAG_mBAType,
545 /* has_a_curves */ false,
546 /* grid_points */ nullptr,
547 /* grid_16 */ nullptr);
548 tags.emplace_back(kTAG_B2A0, std::move(b2a_data));
549 }
550
551 // Compute copyright tag
552 tags.emplace_back(kTAG_cprt, write_text_tag("Google Inc. 2022"));
553
554 // Compute the size of the profile.
555 size_t tag_data_size = 0;
556 for (const auto& tag : tags) {
557 tag_data_size += tag.second->getLength();
558 }
559 size_t tag_table_size = kICCTagTableEntrySize * tags.size();
560 size_t profile_size = kICCHeaderSize + tag_table_size + tag_data_size;
561
Nick Deakin0db53ee2023-05-19 17:14:45 -0400562 sp<DataStruct> dataStruct = sp<DataStruct>::make(profile_size + kICCIdentifierSize);
563
564 // Write identifier, chunk count, and chunk ID
565 if (!dataStruct->write(kICCIdentifier, sizeof(kICCIdentifier)) ||
566 !dataStruct->write8(1) || !dataStruct->write8(1)) {
567 ALOGE("writeIccProfile(): error in identifier");
568 return dataStruct;
569 }
570
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000571 // Write the header.
572 header.data_color_space = Endian_SwapBE32(Signature_RGB);
Dichen Zhangdbceb0e2023-04-14 19:03:18 +0000573 header.pcs = Endian_SwapBE32(tf == ULTRAHDR_TF_PQ ? Signature_Lab : Signature_XYZ);
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000574 header.size = Endian_SwapBE32(profile_size);
575 header.tag_count = Endian_SwapBE32(tags.size());
576
Dichen Zhange46f9bb2023-02-23 19:34:53 +0000577 if (!dataStruct->write(&header, sizeof(header))) {
578 ALOGE("writeIccProfile(): error in header");
579 return dataStruct;
580 }
581
582 // Write the tag table. Track the offset and size of the previous tag to
583 // compute each tag's offset. An empty SkData indicates that the previous
584 // tag is to be reused.
585 uint32_t last_tag_offset = sizeof(header) + tag_table_size;
586 uint32_t last_tag_size = 0;
587 for (const auto& tag : tags) {
588 last_tag_offset = last_tag_offset + last_tag_size;
589 last_tag_size = tag.second->getLength();
590 uint32_t tag_table_entry[3] = {
591 Endian_SwapBE32(tag.first),
592 Endian_SwapBE32(last_tag_offset),
593 Endian_SwapBE32(last_tag_size),
594 };
595 if (!dataStruct->write(tag_table_entry, sizeof(tag_table_entry))) {
596 ALOGE("writeIccProfile(): error in writing tag table");
597 return dataStruct;
598 }
599 }
600
601 // Write the tags.
602 for (const auto& tag : tags) {
603 if (!dataStruct->write(tag.second->getData(), tag.second->getLength())) {
604 ALOGE("writeIccProfile(): error in writing tags");
605 return dataStruct;
606 }
607 }
608
609 return dataStruct;
610}
611
Nick Deakin0db53ee2023-05-19 17:14:45 -0400612bool IccHelper::tagsEqualToMatrix(const Matrix3x3& matrix,
613 const uint8_t* red_tag,
614 const uint8_t* green_tag,
615 const uint8_t* blue_tag) {
616 sp<DataStruct> red_tag_test = write_xyz_tag(matrix.vals[0][0], matrix.vals[1][0],
617 matrix.vals[2][0]);
618 sp<DataStruct> green_tag_test = write_xyz_tag(matrix.vals[0][1], matrix.vals[1][1],
619 matrix.vals[2][1]);
620 sp<DataStruct> blue_tag_test = write_xyz_tag(matrix.vals[0][2], matrix.vals[1][2],
621 matrix.vals[2][2]);
622 return memcmp(red_tag, red_tag_test->getData(), kColorantTagSize) == 0 &&
623 memcmp(green_tag, green_tag_test->getData(), kColorantTagSize) == 0 &&
624 memcmp(blue_tag, blue_tag_test->getData(), kColorantTagSize) == 0;
625}
626
627ultrahdr_color_gamut IccHelper::readIccColorGamut(void* icc_data, size_t icc_size) {
628 // Each tag table entry consists of 3 fields of 4 bytes each.
629 static const size_t kTagTableEntrySize = 12;
630
631 if (icc_data == nullptr || icc_size < sizeof(ICCHeader) + kICCIdentifierSize) {
632 return ULTRAHDR_COLORGAMUT_UNSPECIFIED;
633 }
634
635 if (memcmp(icc_data, kICCIdentifier, sizeof(kICCIdentifier)) != 0) {
636 return ULTRAHDR_COLORGAMUT_UNSPECIFIED;
637 }
638
639 uint8_t* icc_bytes = reinterpret_cast<uint8_t*>(icc_data) + kICCIdentifierSize;
640
641 ICCHeader* header = reinterpret_cast<ICCHeader*>(icc_bytes);
642
643 // Use 0 to indicate not found, since offsets are always relative to start
644 // of ICC data and therefore a tag offset of zero would never be valid.
645 size_t red_primary_offset = 0, green_primary_offset = 0, blue_primary_offset = 0;
646 size_t red_primary_size = 0, green_primary_size = 0, blue_primary_size = 0;
647 for (size_t tag_idx = 0; tag_idx < Endian_SwapBE32(header->tag_count); ++tag_idx) {
648 uint32_t* tag_entry_start = reinterpret_cast<uint32_t*>(
649 icc_bytes + sizeof(ICCHeader) + tag_idx * kTagTableEntrySize);
650 // first 4 bytes are the tag signature, next 4 bytes are the tag offset,
651 // last 4 bytes are the tag length in bytes.
652 if (red_primary_offset == 0 && *tag_entry_start == Endian_SwapBE32(kTAG_rXYZ)) {
653 red_primary_offset = Endian_SwapBE32(*(tag_entry_start+1));
654 red_primary_size = Endian_SwapBE32(*(tag_entry_start+2));
655 } else if (green_primary_offset == 0 && *tag_entry_start == Endian_SwapBE32(kTAG_gXYZ)) {
656 green_primary_offset = Endian_SwapBE32(*(tag_entry_start+1));
657 green_primary_size = Endian_SwapBE32(*(tag_entry_start+2));
658 } else if (blue_primary_offset == 0 && *tag_entry_start == Endian_SwapBE32(kTAG_bXYZ)) {
659 blue_primary_offset = Endian_SwapBE32(*(tag_entry_start+1));
660 blue_primary_size = Endian_SwapBE32(*(tag_entry_start+2));
661 }
662 }
663
664 if (red_primary_offset == 0 || red_primary_size != kColorantTagSize ||
665 kICCIdentifierSize + red_primary_offset + red_primary_size > icc_size ||
666 green_primary_offset == 0 || green_primary_size != kColorantTagSize ||
667 kICCIdentifierSize + green_primary_offset + green_primary_size > icc_size ||
668 blue_primary_offset == 0 || blue_primary_size != kColorantTagSize ||
669 kICCIdentifierSize + blue_primary_offset + blue_primary_size > icc_size) {
670 return ULTRAHDR_COLORGAMUT_UNSPECIFIED;
671 }
672
673 uint8_t* red_tag = icc_bytes + red_primary_offset;
674 uint8_t* green_tag = icc_bytes + green_primary_offset;
675 uint8_t* blue_tag = icc_bytes + blue_primary_offset;
676
677 // Serialize tags as we do on encode and compare what we find to that to
678 // determine the gamut (since we don't have a need yet for full deserialize).
679 if (tagsEqualToMatrix(kSRGB, red_tag, green_tag, blue_tag)) {
680 return ULTRAHDR_COLORGAMUT_BT709;
681 } else if (tagsEqualToMatrix(kDisplayP3, red_tag, green_tag, blue_tag)) {
682 return ULTRAHDR_COLORGAMUT_P3;
683 } else if (tagsEqualToMatrix(kRec2020, red_tag, green_tag, blue_tag)) {
684 return ULTRAHDR_COLORGAMUT_BT2100;
685 }
686
687 // Didn't find a match to one of the profiles we write; indicate the gamut
688 // is unspecified since we don't understand it.
689 return ULTRAHDR_COLORGAMUT_UNSPECIFIED;
690}
691
692} // namespace android::ultrahdr