blob: 4541f9b0e1e588882e92b3ee6b6f18b23fc9d912 [file] [log] [blame]
Nick Deakinf6bca5a2022-11-04 10:43:43 -04001/*
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
17#include <cmath>
18
19#include <jpegrecoverymap/recoverymapmath.h>
20
21namespace android::recoverymap {
22
Nick Deakin594a4ca2022-11-16 20:57:42 -050023////////////////////////////////////////////////////////////////////////////////
24// sRGB transformations
Nick Deakinf6bca5a2022-11-04 10:43:43 -040025
Nick Deakin594a4ca2022-11-16 20:57:42 -050026static const float kSrgbR = 0.299f, kSrgbG = 0.587f, kSrgbB = 0.114f;
27
28float srgbLuminance(Color e) {
29 return kSrgbR * e.r + kSrgbG * e.g + kSrgbB * e.b;
Nick Deakinf6bca5a2022-11-04 10:43:43 -040030}
31
32static const float kSrgbRCr = 1.402f, kSrgbGCb = 0.34414f, kSrgbGCr = 0.71414f, kSrgbBCb = 1.772f;
33
Nick Deakin594a4ca2022-11-16 20:57:42 -050034Color srgbYuvToRgb(Color e_gamma) {
35 return {{{ e_gamma.y + kSrgbRCr * e_gamma.v,
36 e_gamma.y - kSrgbGCb * e_gamma.u - kSrgbGCr * e_gamma.v,
37 e_gamma.y + kSrgbBCb * e_gamma.u }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -040038}
39
Nick Deakin594a4ca2022-11-16 20:57:42 -050040static const float kSrgbUR = -0.1687f, kSrgbUG = -0.3313f, kSrgbUB = 0.5f;
41static const float kSrgbVR = 0.5f, kSrgbVG = -0.4187f, kSrgbVB = -0.0813f;
42
43Color srgbRgbToYuv(Color e_gamma) {
44 return {{{ kSrgbR * e_gamma.r + kSrgbG * e_gamma.g + kSrgbB * e_gamma.b,
45 kSrgbUR * e_gamma.r + kSrgbUG * e_gamma.g + kSrgbUB * e_gamma.b,
46 kSrgbVR * e_gamma.r + kSrgbVG * e_gamma.g + kSrgbVB * e_gamma.b }}};
47}
48
49float srgbInvOetf(float e_gamma) {
50 if (e_gamma <= 0.04045f) {
51 return e_gamma / 12.92f;
Nick Deakinf6bca5a2022-11-04 10:43:43 -040052 } else {
Nick Deakin594a4ca2022-11-16 20:57:42 -050053 return pow((e_gamma + 0.055f) / 1.055f, 2.4);
Nick Deakinf6bca5a2022-11-04 10:43:43 -040054 }
55}
56
Nick Deakin594a4ca2022-11-16 20:57:42 -050057Color srgbInvOetf(Color e_gamma) {
58 return {{{ srgbInvOetf(e_gamma.r),
59 srgbInvOetf(e_gamma.g),
60 srgbInvOetf(e_gamma.b) }}};
61}
62
63
64////////////////////////////////////////////////////////////////////////////////
65// Display-P3 transformations
66
Nick Deakin6bd90432022-11-20 16:26:37 -050067static const float kP3R = 0.22897f, kP3G = 0.69174f, kP3B = 0.07929f;
68
69float p3Luminance(Color e) {
70 return kP3R * e.r + kP3G * e.g + kP3B * e.b;
71}
Nick Deakin594a4ca2022-11-16 20:57:42 -050072
73
74////////////////////////////////////////////////////////////////////////////////
75// BT.2100 transformations - according to ITU-R BT.2100-2
76
77static const float kBt2100R = 0.2627f, kBt2100G = 0.6780f, kBt2100B = 0.0593f;
78
79float bt2100Luminance(Color e) {
80 return kBt2100R * e.r + kBt2100G * e.g + kBt2100B * e.b;
81}
82
83static const float kBt2100Cb = 1.8814f, kBt2100Cr = 1.4746f;
84
85Color bt2100RgbToYuv(Color e_gamma) {
86 float y_gamma = bt2100Luminance(e_gamma);
87 return {{{ y_gamma,
88 (e_gamma.b - y_gamma) / kBt2100Cb,
89 (e_gamma.r - y_gamma) / kBt2100Cr }}};
90}
91
92// Derived from the reverse of bt2100RgbToYuv. The derivation for R and B are
93// pretty straight forward; we just reverse the formulas for U and V above. But
94// deriving the formula for G is a bit more complicated:
95//
96// Start with equation for luminance:
97// Y = kBt2100R * R + kBt2100G * G + kBt2100B * B
98// Solve for G:
99// G = (Y - kBt2100R * R - kBt2100B * B) / kBt2100B
100// Substitute equations for R and B in terms YUV:
101// G = (Y - kBt2100R * (Y + kBt2100Cr * V) - kBt2100B * (Y + kBt2100Cb * U)) / kBt2100B
102// Simplify:
103// G = Y * ((1 - kBt2100R - kBt2100B) / kBt2100G)
104// + U * (kBt2100B * kBt2100Cb / kBt2100G)
105// + V * (kBt2100R * kBt2100Cr / kBt2100G)
106//
107// We then get the following coeficients for calculating G from YUV:
108//
109// Coef for Y = (1 - kBt2100R - kBt2100B) / kBt2100G = 1
110// Coef for U = kBt2100B * kBt2100Cb / kBt2100G = kBt2100GCb = ~0.1645
111// Coef for V = kBt2100R * kBt2100Cr / kBt2100G = kBt2100GCr = ~0.5713
112
113static const float kBt2100GCb = kBt2100B * kBt2100Cb / kBt2100G;
114static const float kBt2100GCr = kBt2100R * kBt2100Cr / kBt2100G;
115
116Color bt2100YuvToRgb(Color e_gamma) {
117 return {{{ e_gamma.y + kBt2100Cr * e_gamma.v,
118 e_gamma.y - kBt2100GCb * e_gamma.u - kBt2100GCr * e_gamma.v,
119 e_gamma.y + kBt2100Cb * e_gamma.u }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400120}
121
122static const float kHlgA = 0.17883277f, kHlgB = 0.28466892f, kHlgC = 0.55991073;
123
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500124static float hlgOetf(float e) {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400125 if (e <= 1.0f/12.0f) {
126 return sqrt(3.0f * e);
127 } else {
128 return kHlgA * log(12.0f * e - kHlgB) + kHlgC;
129 }
130}
131
132Color hlgOetf(Color e) {
133 return {{{ hlgOetf(e.r), hlgOetf(e.g), hlgOetf(e.b) }}};
134}
135
Nick Deakin6bd90432022-11-20 16:26:37 -0500136static float hlgInvOetf(float e_gamma) {
Nick Deakin594a4ca2022-11-16 20:57:42 -0500137 if (e_gamma <= 0.5f) {
138 return pow(e_gamma, 2.0f) / 3.0f;
139 } else {
140 return (exp((e_gamma - kHlgC) / kHlgA) + kHlgB) / 12.0f;
141 }
142}
143
144Color hlgInvOetf(Color e_gamma) {
145 return {{{ hlgInvOetf(e_gamma.r),
146 hlgInvOetf(e_gamma.g),
147 hlgInvOetf(e_gamma.b) }}};
148}
149
Nick Deakin6bd90432022-11-20 16:26:37 -0500150static const float kPqM1 = 2610.0f / 16384.0f, kPqM2 = 2523.0f / 4096.0f * 128.0f;
151static const float kPqC1 = 3424.0f / 4096.0f, kPqC2 = 2413.0f / 4096.0f * 32.0f,
152 kPqC3 = 2392.0f / 4096.0f * 32.0f;
153
154static float pqOetf(float e) {
155 if (e < 0.0f) e = 0.0f;
156 return pow((kPqC1 + kPqC2 * pow(e / 10000.0f, kPqM1)) / (1 + kPqC3 * pow(e / 10000.0f, kPqM1)),
157 kPqM2);
158}
159
160Color pqOetf(Color e) {
161 return {{{ pqOetf(e.r), pqOetf(e.g), pqOetf(e.b) }}};
162}
163
164static float pqInvOetf(float e_gamma) {
165 static const float kPqInvOetfCoef = log2(-(pow(kPqM1, 1.0f / kPqM2) - kPqC1)
166 / (kPqC3 * pow(kPqM1, 1.0f / kPqM2) - kPqC2));
167 return kPqInvOetfCoef / log2(e_gamma * 10000.0f);
168}
169
170Color pqInvOetf(Color e_gamma) {
171 return {{{ pqInvOetf(e_gamma.r),
172 pqInvOetf(e_gamma.g),
173 pqInvOetf(e_gamma.b) }}};
174}
175
Nick Deakin594a4ca2022-11-16 20:57:42 -0500176
177////////////////////////////////////////////////////////////////////////////////
178// Color conversions
179
Nick Deakin6bd90432022-11-20 16:26:37 -0500180Color bt709ToP3(Color e) {
181 return {{{ 0.82254f * e.r + 0.17755f * e.g + 0.00006f * e.b,
182 0.03312f * e.r + 0.96684f * e.g + -0.00001f * e.b,
183 0.01706f * e.r + 0.07240f * e.g + 0.91049f * e.b }}};
184}
185
186Color bt709ToBt2100(Color e) {
187 return {{{ 0.62740f * e.r + 0.32930f * e.g + 0.04332f * e.b,
188 0.06904f * e.r + 0.91958f * e.g + 0.01138f * e.b,
189 0.01636f * e.r + 0.08799f * e.g + 0.89555f * e.b }}};
190}
191
192Color p3ToBt709(Color e) {
193 return {{{ 1.22482f * e.r + -0.22490f * e.g + -0.00007f * e.b,
194 -0.04196f * e.r + 1.04199f * e.g + 0.00001f * e.b,
195 -0.01961f * e.r + -0.07865f * e.g + 1.09831f * e.b }}};
196}
197
198Color p3ToBt2100(Color e) {
199 return {{{ 0.75378f * e.r + 0.19862f * e.g + 0.04754f * e.b,
200 0.04576f * e.r + 0.94177f * e.g + 0.01250f * e.b,
201 -0.00121f * e.r + 0.01757f * e.g + 0.98359f * e.b }}};
202}
203
204Color bt2100ToBt709(Color e) {
205 return {{{ 1.66045f * e.r + -0.58764f * e.g + -0.07286f * e.b,
206 -0.12445f * e.r + 1.13282f * e.g + -0.00837f * e.b,
207 -0.01811f * e.r + -0.10057f * e.g + 1.11878f * e.b }}};
208}
209
210Color bt2100ToP3(Color e) {
211 return {{{ 1.34369f * e.r + -0.28223f * e.g + -0.06135f * e.b,
212 -0.06533f * e.r + 1.07580f * e.g + -0.01051f * e.b,
213 0.00283f * e.r + -0.01957f * e.g + 1.01679f * e.b
214 }}};
215}
216
217// TODO: confirm we always want to convert like this before calculating
218// luminance.
219ColorTransformFn getHdrConversionFn(jpegr_color_gamut sdr_gamut, jpegr_color_gamut hdr_gamut) {
220 switch (sdr_gamut) {
221 case JPEGR_COLORGAMUT_BT709:
222 switch (hdr_gamut) {
223 case JPEGR_COLORGAMUT_BT709:
224 return identityConversion;
225 case JPEGR_COLORGAMUT_P3:
226 return p3ToBt709;
227 case JPEGR_COLORGAMUT_BT2100:
228 return bt2100ToBt709;
229 case JPEGR_COLORGAMUT_UNSPECIFIED:
230 return nullptr;
231 }
232 break;
233 case JPEGR_COLORGAMUT_P3:
234 switch (hdr_gamut) {
235 case JPEGR_COLORGAMUT_BT709:
236 return bt709ToP3;
237 case JPEGR_COLORGAMUT_P3:
238 return identityConversion;
239 case JPEGR_COLORGAMUT_BT2100:
240 return bt2100ToP3;
241 case JPEGR_COLORGAMUT_UNSPECIFIED:
242 return nullptr;
243 }
244 break;
245 case JPEGR_COLORGAMUT_BT2100:
246 switch (hdr_gamut) {
247 case JPEGR_COLORGAMUT_BT709:
248 return bt709ToBt2100;
249 case JPEGR_COLORGAMUT_P3:
250 return p3ToBt2100;
251 case JPEGR_COLORGAMUT_BT2100:
252 return identityConversion;
253 case JPEGR_COLORGAMUT_UNSPECIFIED:
254 return nullptr;
255 }
256 break;
257 case JPEGR_COLORGAMUT_UNSPECIFIED:
258 return nullptr;
259 }
260}
261
Nick Deakin594a4ca2022-11-16 20:57:42 -0500262
263////////////////////////////////////////////////////////////////////////////////
264// Recovery map calculations
265
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500266uint8_t encodeRecovery(float y_sdr, float y_hdr, float hdr_ratio) {
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400267 float gain = 1.0f;
268 if (y_sdr > 0.0f) {
269 gain = y_hdr / y_sdr;
270 }
271
272 if (gain < -hdr_ratio) gain = -hdr_ratio;
273 if (gain > hdr_ratio) gain = hdr_ratio;
274
275 return static_cast<uint8_t>(log2(gain) / log2(hdr_ratio) * 127.5f + 127.5f);
276}
277
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500278static float applyRecovery(float e, float recovery, float hdr_ratio) {
279 return exp2(log2(e) + recovery * log2(hdr_ratio));
280}
281
282Color applyRecovery(Color e, float recovery, float hdr_ratio) {
283 return {{{ applyRecovery(e.r, recovery, hdr_ratio),
284 applyRecovery(e.g, recovery, hdr_ratio),
285 applyRecovery(e.b, recovery, hdr_ratio) }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400286}
287
288// TODO: do we need something more clever for filtering either the map or images
289// to generate the map?
290
291static float mapUintToFloat(uint8_t map_uint) {
292 return (static_cast<float>(map_uint) - 127.5f) / 127.5f;
293}
294
295float sampleMap(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y) {
296 float x_map = static_cast<float>(x) / static_cast<float>(map_scale_factor);
297 float y_map = static_cast<float>(y) / static_cast<float>(map_scale_factor);
298
299 size_t x_lower = static_cast<size_t>(floor(x_map));
300 size_t x_upper = x_lower + 1;
301 size_t y_lower = static_cast<size_t>(floor(y_map));
302 size_t y_upper = y_lower + 1;
303
304 float x_influence = x_map - static_cast<float>(x_lower);
305 float y_influence = y_map - static_cast<float>(y_lower);
306
307 float e1 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_lower * map->width]);
308 float e2 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_upper * map->width]);
309 float e3 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_lower * map->width]);
310 float e4 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_upper * map->width]);
311
312 return e1 * (x_influence + y_influence) / 2.0f
313 + e2 * (x_influence + 1.0f - y_influence) / 2.0f
314 + e3 * (1.0f - x_influence + y_influence) / 2.0f
315 + e4 * (1.0f - x_influence + 1.0f - y_influence) / 2.0f;
316}
317
318Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
319 size_t pixel_count = image->width * image->height;
320
321 size_t pixel_y_idx = x + y * image->width;
322 size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2);
323
324 uint8_t y_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y_idx];
325 uint8_t u_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count + pixel_uv_idx];
326 uint8_t v_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count * 5 / 4 + pixel_uv_idx];
327
328 // 128 bias for UV given we are using jpeglib; see:
329 // https://github.com/kornelski/libjpeg/blob/master/structure.doc
330 return {{{ static_cast<float>(y_uint) / 255.0f,
331 (static_cast<float>(u_uint) - 128.0f) / 255.0f,
332 (static_cast<float>(v_uint) - 128.0f) / 255.0f }}};
333}
334
Nick Deakin594a4ca2022-11-16 20:57:42 -0500335Color getP010Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
336 size_t pixel_count = image->width * image->height;
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400337
Nick Deakin594a4ca2022-11-16 20:57:42 -0500338 size_t pixel_y_idx = x + y * image->width;
339 size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2);
340
341 uint16_t y_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_y_idx];
342 uint16_t u_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_count + pixel_uv_idx * 2];
343 uint16_t v_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_count + pixel_uv_idx * 2 + 1];
344
345 // Conversions include taking narrow-range into account.
346 return {{{ static_cast<float>(y_uint) / 940.0f,
347 (static_cast<float>(u_uint) - 64.0f) / 940.0f - 0.5f,
348 (static_cast<float>(v_uint) - 64.0f) / 940.0f - 0.5f }}};
349}
350
351typedef Color (*getPixelFn)(jr_uncompressed_ptr, size_t, size_t);
352
353static Color samplePixels(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y,
354 getPixelFn get_pixel_fn) {
355 Color e = {{{ 0.0f, 0.0f, 0.0f }}};
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400356 for (size_t dy = 0; dy < map_scale_factor; ++dy) {
357 for (size_t dx = 0; dx < map_scale_factor; ++dx) {
Nick Deakin594a4ca2022-11-16 20:57:42 -0500358 e += get_pixel_fn(image, x * map_scale_factor + dx, y * map_scale_factor + dy);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400359 }
360 }
361
362 return e / static_cast<float>(map_scale_factor * map_scale_factor);
363}
364
Nick Deakin594a4ca2022-11-16 20:57:42 -0500365Color sampleYuv420(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
366 return samplePixels(image, map_scale_factor, x, y, getYuv420Pixel);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400367}
368
Nick Deakin594a4ca2022-11-16 20:57:42 -0500369Color sampleP010(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
370 return samplePixels(image, map_scale_factor, x, y, getP010Pixel);
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400371}
Nick Deakin5c20b9e2022-11-15 17:39:24 -0500372
Nick Deakin6bd90432022-11-20 16:26:37 -0500373uint32_t colorToRgba1010102(Color e_gamma) {
374 return (0x3ff & static_cast<uint32_t>(e_gamma.r * 1023.0f))
375 | ((0x3ff & static_cast<uint32_t>(e_gamma.g * 1023.0f)) << 10)
376 | ((0x3ff & static_cast<uint32_t>(e_gamma.b * 1023.0f)) << 20)
377 | (0x3 << 30); // Set alpha to 1.0
378}
379
Nick Deakinf6bca5a2022-11-04 10:43:43 -0400380} // namespace android::recoverymap