blob: 3e110bcd268b24f6c0e286e801a5498118068281 [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
23static const float kBt2100R = 0.2627f, kBt2100G = 0.6780f, kBt2100B = 0.0593f;
24static const float kBt2100Cb = 1.8814f, kBt2100Cr = 1.4746f;
25
26Color bt2100RgbToYuv(Color e) {
27 float yp = kBt2100R * e.r + kBt2100G * e.g + kBt2100B * e.b;
28 return {{{yp, (e.b - yp) / kBt2100Cb, (e.r - yp) / kBt2100Cr }}};
29}
30
31static const float kSrgbRCr = 1.402f, kSrgbGCb = 0.34414f, kSrgbGCr = 0.71414f, kSrgbBCb = 1.772f;
32
33Color srgbYuvToRgb(Color e) {
34 return {{{ e.y + kSrgbRCr * e.v, e.y - kSrgbGCb * e.u - kSrgbGCr * e.v, e.y + kSrgbBCb * e.u }}};
35}
36
37float srgbInvOetf(float e) {
38 if (e <= 0.04045f) {
39 return e / 12.92f;
40 } else {
41 return pow((e + 0.055f) / 1.055f, 2.4);
42 }
43}
44
45Color srgbInvOetf(Color e) {
46 return {{{ srgbInvOetf(e.r), srgbInvOetf(e.g), srgbInvOetf(e.b) }}};
47}
48
49static const float kHlgA = 0.17883277f, kHlgB = 0.28466892f, kHlgC = 0.55991073;
50
51float hlgInvOetf(float e) {
52 if (e <= 0.5f) {
53 return pow(e, 2.0f) / 3.0f;
54 } else {
55 return (exp((e - kHlgC) / kHlgA) + kHlgB) / 12.0f;
56 }
57}
58
59float hlgOetf(float e) {
60 if (e <= 1.0f/12.0f) {
61 return sqrt(3.0f * e);
62 } else {
63 return kHlgA * log(12.0f * e - kHlgB) + kHlgC;
64 }
65}
66
67Color hlgOetf(Color e) {
68 return {{{ hlgOetf(e.r), hlgOetf(e.g), hlgOetf(e.b) }}};
69}
70
71uint8_t EncodeRecovery(float y_sdr, float y_hdr, float hdr_ratio) {
72 float gain = 1.0f;
73 if (y_sdr > 0.0f) {
74 gain = y_hdr / y_sdr;
75 }
76
77 if (gain < -hdr_ratio) gain = -hdr_ratio;
78 if (gain > hdr_ratio) gain = hdr_ratio;
79
80 return static_cast<uint8_t>(log2(gain) / log2(hdr_ratio) * 127.5f + 127.5f);
81}
82
83float applyRecovery(float y_sdr, float recovery, float hdr_ratio) {
84 return exp2(log2(y_sdr) + recovery * log2(hdr_ratio));
85}
86
87// TODO: do we need something more clever for filtering either the map or images
88// to generate the map?
89
90static float mapUintToFloat(uint8_t map_uint) {
91 return (static_cast<float>(map_uint) - 127.5f) / 127.5f;
92}
93
94float sampleMap(jr_uncompressed_ptr map, size_t map_scale_factor, size_t x, size_t y) {
95 float x_map = static_cast<float>(x) / static_cast<float>(map_scale_factor);
96 float y_map = static_cast<float>(y) / static_cast<float>(map_scale_factor);
97
98 size_t x_lower = static_cast<size_t>(floor(x_map));
99 size_t x_upper = x_lower + 1;
100 size_t y_lower = static_cast<size_t>(floor(y_map));
101 size_t y_upper = y_lower + 1;
102
103 float x_influence = x_map - static_cast<float>(x_lower);
104 float y_influence = y_map - static_cast<float>(y_lower);
105
106 float e1 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_lower * map->width]);
107 float e2 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_lower + y_upper * map->width]);
108 float e3 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_lower * map->width]);
109 float e4 = mapUintToFloat(reinterpret_cast<uint8_t*>(map->data)[x_upper + y_upper * map->width]);
110
111 return e1 * (x_influence + y_influence) / 2.0f
112 + e2 * (x_influence + 1.0f - y_influence) / 2.0f
113 + e3 * (1.0f - x_influence + y_influence) / 2.0f
114 + e4 * (1.0f - x_influence + 1.0f - y_influence) / 2.0f;
115}
116
117Color getYuv420Pixel(jr_uncompressed_ptr image, size_t x, size_t y) {
118 size_t pixel_count = image->width * image->height;
119
120 size_t pixel_y_idx = x + y * image->width;
121 size_t pixel_uv_idx = x / 2 + (y / 2) * (image->width / 2);
122
123 uint8_t y_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_y_idx];
124 uint8_t u_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count + pixel_uv_idx];
125 uint8_t v_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_count * 5 / 4 + pixel_uv_idx];
126
127 // 128 bias for UV given we are using jpeglib; see:
128 // https://github.com/kornelski/libjpeg/blob/master/structure.doc
129 return {{{ static_cast<float>(y_uint) / 255.0f,
130 (static_cast<float>(u_uint) - 128.0f) / 255.0f,
131 (static_cast<float>(v_uint) - 128.0f) / 255.0f }}};
132}
133
134typedef float (*sampleComponentFn)(jr_uncompressed_ptr, size_t, size_t);
135
136static float sampleComponent(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y,
137 sampleComponentFn sample_fn) {
138 float e = 0.0f;
139 for (size_t dy = 0; dy < map_scale_factor; ++dy) {
140 for (size_t dx = 0; dx < map_scale_factor; ++dx) {
141 e += sample_fn(image, x * map_scale_factor + dx, y * map_scale_factor + dy);
142 }
143 }
144
145 return e / static_cast<float>(map_scale_factor * map_scale_factor);
146}
147
148static float getYuv420Y(jr_uncompressed_ptr image, size_t x, size_t y) {
149 size_t pixel_idx = x + y * image->width;
150 uint8_t y_uint = reinterpret_cast<uint8_t*>(image->data)[pixel_idx];
151 return static_cast<float>(y_uint) / 255.0f;
152}
153
154
155float sampleYuv420Y(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
156 return sampleComponent(image, map_scale_factor, x, y, getYuv420Y);
157}
158
159static float getP010Y(jr_uncompressed_ptr image, size_t x, size_t y) {
160 size_t pixel_idx = x + y * image->width;
161 uint8_t y_uint = reinterpret_cast<uint16_t*>(image->data)[pixel_idx];
162 // Expecting narrow range input
163 return (static_cast<float>(y_uint) - 64.0f) / 960.0f;
164}
165
166float sampleP010Y(jr_uncompressed_ptr image, size_t map_scale_factor, size_t x, size_t y) {
167 return sampleComponent(image, map_scale_factor, x, y, getP010Y);
168}
169} // namespace android::recoverymap