• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 "ultrahdr/gainmapmath.h"
20 
21 namespace ultrahdr {
22 
23 ////////////////////////////////////////////////////////////////////////////////
24 // Framework
25 
getReferenceDisplayPeakLuminanceInNits(uhdr_color_transfer_t transfer)26 float getReferenceDisplayPeakLuminanceInNits(uhdr_color_transfer_t transfer) {
27   switch (transfer) {
28     case UHDR_CT_LINEAR:
29       return kPqMaxNits;
30     case UHDR_CT_HLG:
31       return kHlgMaxNits;
32     case UHDR_CT_PQ:
33       return kPqMaxNits;
34     case UHDR_CT_SRGB:
35       return kSdrWhiteNits;
36     case UHDR_CT_UNSPECIFIED:
37       return -1.0f;
38   }
39   return -1.0f;
40 }
41 
42 ////////////////////////////////////////////////////////////////////////////////
43 // Use Shepard's method for inverse distance weighting.
44 
euclideanDistance(float x1,float x2,float y1,float y2)45 float ShepardsIDW::euclideanDistance(float x1, float x2, float y1, float y2) {
46   return sqrt(((y2 - y1) * (y2 - y1)) + (x2 - x1) * (x2 - x1));
47 }
48 
fillShepardsIDW(float * weights,int incR,int incB)49 void ShepardsIDW::fillShepardsIDW(float* weights, int incR, int incB) {
50   for (int y = 0; y < mMapScaleFactor; y++) {
51     for (int x = 0; x < mMapScaleFactor; x++) {
52       float pos_x = ((float)x) / mMapScaleFactor;
53       float pos_y = ((float)y) / mMapScaleFactor;
54       int curr_x = floor(pos_x);
55       int curr_y = floor(pos_y);
56       int next_x = curr_x + incR;
57       int next_y = curr_y + incB;
58       float e1_distance = euclideanDistance(pos_x, curr_x, pos_y, curr_y);
59       int index = y * mMapScaleFactor * 4 + x * 4;
60       if (e1_distance == 0) {
61         weights[index++] = 1.f;
62         weights[index++] = 0.f;
63         weights[index++] = 0.f;
64         weights[index++] = 0.f;
65       } else {
66         float e1_weight = 1.f / e1_distance;
67 
68         float e2_distance = euclideanDistance(pos_x, curr_x, pos_y, next_y);
69         float e2_weight = 1.f / e2_distance;
70 
71         float e3_distance = euclideanDistance(pos_x, next_x, pos_y, curr_y);
72         float e3_weight = 1.f / e3_distance;
73 
74         float e4_distance = euclideanDistance(pos_x, next_x, pos_y, next_y);
75         float e4_weight = 1.f / e4_distance;
76 
77         float total_weight = e1_weight + e2_weight + e3_weight + e4_weight;
78 
79         weights[index++] = e1_weight / total_weight;
80         weights[index++] = e2_weight / total_weight;
81         weights[index++] = e3_weight / total_weight;
82         weights[index++] = e4_weight / total_weight;
83       }
84     }
85   }
86 }
87 
88 ////////////////////////////////////////////////////////////////////////////////
89 // sRGB transformations
90 
91 // See IEC 61966-2-1/Amd 1:2003, Equation F.7.
92 static const float kSrgbR = 0.212639f, kSrgbG = 0.715169f, kSrgbB = 0.072192f;
93 
srgbLuminance(Color e)94 float srgbLuminance(Color e) { return kSrgbR * e.r + kSrgbG * e.g + kSrgbB * e.b; }
95 
96 // See ITU-R BT.709-6, Section 3.
97 // Uses the same coefficients for deriving luma signal as
98 // IEC 61966-2-1/Amd 1:2003 states for luminance, so we reuse the luminance
99 // function above.
100 static const float kSrgbCb = (2 * (1 - kSrgbB)), kSrgbCr = (2 * (1 - kSrgbR));
101 
srgbRgbToYuv(Color e_gamma)102 Color srgbRgbToYuv(Color e_gamma) {
103   float y_gamma = srgbLuminance(e_gamma);
104   return {{{y_gamma, (e_gamma.b - y_gamma) / kSrgbCb, (e_gamma.r - y_gamma) / kSrgbCr}}};
105 }
106 
107 // See ITU-R BT.709-6, Section 3.
108 // Same derivation to BT.2100's YUV->RGB, below. Similar to srgbRgbToYuv, we
109 // can reuse the luminance coefficients since they are the same.
110 static const float kSrgbGCb = kSrgbB * kSrgbCb / kSrgbG;
111 static const float kSrgbGCr = kSrgbR * kSrgbCr / kSrgbG;
112 
srgbYuvToRgb(Color e_gamma)113 Color srgbYuvToRgb(Color e_gamma) {
114   return {{{clampPixelFloat(e_gamma.y + kSrgbCr * e_gamma.v),
115             clampPixelFloat(e_gamma.y - kSrgbGCb * e_gamma.u - kSrgbGCr * e_gamma.v),
116             clampPixelFloat(e_gamma.y + kSrgbCb * e_gamma.u)}}};
117 }
118 
119 // See IEC 61966-2-1/Amd 1:2003, Equations F.5 and F.6.
srgbInvOetf(float e_gamma)120 float srgbInvOetf(float e_gamma) {
121   if (e_gamma <= 0.04045f) {
122     return e_gamma / 12.92f;
123   } else {
124     return pow((e_gamma + 0.055f) / 1.055f, 2.4f);
125   }
126 }
127 
srgbInvOetf(Color e_gamma)128 Color srgbInvOetf(Color e_gamma) {
129   return {{{srgbInvOetf(e_gamma.r), srgbInvOetf(e_gamma.g), srgbInvOetf(e_gamma.b)}}};
130 }
131 
srgbInvOetfLUT(float e_gamma)132 float srgbInvOetfLUT(float e_gamma) {
133   int32_t value = static_cast<int32_t>(e_gamma * (kSrgbInvOETFNumEntries - 1) + 0.5);
134   // TODO() : Remove once conversion modules have appropriate clamping in place
135   value = CLIP3(value, 0, kSrgbInvOETFNumEntries - 1);
136   static LookUpTable kSrgbLut(kSrgbInvOETFNumEntries, static_cast<float (*)(float)>(srgbInvOetf));
137   return kSrgbLut.getTable()[value];
138 }
139 
srgbInvOetfLUT(Color e_gamma)140 Color srgbInvOetfLUT(Color e_gamma) {
141   return {{{srgbInvOetfLUT(e_gamma.r), srgbInvOetfLUT(e_gamma.g), srgbInvOetfLUT(e_gamma.b)}}};
142 }
143 
144 // See IEC 61966-2-1/Amd 1:2003, Equations F.10 and F.11.
srgbOetf(float e)145 float srgbOetf(float e) {
146   constexpr float kThreshold = 0.0031308f;
147   constexpr float kLowSlope = 12.92f;
148   constexpr float kHighOffset = 0.055f;
149   constexpr float kPowerExponent = 1.0f / 2.4f;
150   if (e <= kThreshold) {
151     return kLowSlope * e;
152   }
153   return (1.0f + kHighOffset) * std::pow(e, kPowerExponent) - kHighOffset;
154 }
155 
srgbOetf(Color e)156 Color srgbOetf(Color e) { return {{{srgbOetf(e.r), srgbOetf(e.g), srgbOetf(e.b)}}}; }
157 
158 ////////////////////////////////////////////////////////////////////////////////
159 // Display-P3 transformations
160 
161 // See SMPTE EG 432-1, Equation G-7.
162 static const float kP3R = 0.2289746f, kP3G = 0.6917385f, kP3B = 0.0792869f;
163 
p3Luminance(Color e)164 float p3Luminance(Color e) { return kP3R * e.r + kP3G * e.g + kP3B * e.b; }
165 
166 // See ITU-R BT.601-7, Sections 2.5.1 and 2.5.2.
167 // Unfortunately, calculation of luma signal differs from calculation of
168 // luminance for Display-P3, so we can't reuse p3Luminance here.
169 static const float kP3YR = 0.299f, kP3YG = 0.587f, kP3YB = 0.114f;
170 static const float kP3Cb = 1.772f, kP3Cr = 1.402f;
171 
p3RgbToYuv(Color e_gamma)172 Color p3RgbToYuv(Color e_gamma) {
173   float y_gamma = kP3YR * e_gamma.r + kP3YG * e_gamma.g + kP3YB * e_gamma.b;
174   return {{{y_gamma, (e_gamma.b - y_gamma) / kP3Cb, (e_gamma.r - y_gamma) / kP3Cr}}};
175 }
176 
177 // See ITU-R BT.601-7, Sections 2.5.1 and 2.5.2.
178 // Same derivation to BT.2100's YUV->RGB, below. Similar to p3RgbToYuv, we must
179 // use luma signal coefficients rather than the luminance coefficients.
180 static const float kP3GCb = kP3YB * kP3Cb / kP3YG;
181 static const float kP3GCr = kP3YR * kP3Cr / kP3YG;
182 
p3YuvToRgb(Color e_gamma)183 Color p3YuvToRgb(Color e_gamma) {
184   return {{{clampPixelFloat(e_gamma.y + kP3Cr * e_gamma.v),
185             clampPixelFloat(e_gamma.y - kP3GCb * e_gamma.u - kP3GCr * e_gamma.v),
186             clampPixelFloat(e_gamma.y + kP3Cb * e_gamma.u)}}};
187 }
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 // BT.2100 transformations - according to ITU-R BT.2100-2
191 
192 // See ITU-R BT.2100-2, Table 5, HLG Reference OOTF
193 static const float kBt2100R = 0.2627f, kBt2100G = 0.677998f, kBt2100B = 0.059302f;
194 
bt2100Luminance(Color e)195 float bt2100Luminance(Color e) { return kBt2100R * e.r + kBt2100G * e.g + kBt2100B * e.b; }
196 
197 // See ITU-R BT.2100-2, Table 6, Derivation of colour difference signals.
198 // BT.2100 uses the same coefficients for calculating luma signal and luminance,
199 // so we reuse the luminance function here.
200 static const float kBt2100Cb = (2 * (1 - kBt2100B)), kBt2100Cr = (2 * (1 - kBt2100R));
201 
bt2100RgbToYuv(Color e_gamma)202 Color bt2100RgbToYuv(Color e_gamma) {
203   float y_gamma = bt2100Luminance(e_gamma);
204   return {{{y_gamma, (e_gamma.b - y_gamma) / kBt2100Cb, (e_gamma.r - y_gamma) / kBt2100Cr}}};
205 }
206 
207 // See ITU-R BT.2100-2, Table 6, Derivation of colour difference signals.
208 //
209 // Similar to bt2100RgbToYuv above, we can reuse the luminance coefficients.
210 //
211 // Derived by inversing bt2100RgbToYuv. The derivation for R and B are  pretty
212 // straight forward; we just invert the formulas for U and V above. But deriving
213 // the formula for G is a bit more complicated:
214 //
215 // Start with equation for luminance:
216 //   Y = kBt2100R * R + kBt2100G * G + kBt2100B * B
217 // Solve for G:
218 //   G = (Y - kBt2100R * R - kBt2100B * B) / kBt2100B
219 // Substitute equations for R and B in terms YUV:
220 //   G = (Y - kBt2100R * (Y + kBt2100Cr * V) - kBt2100B * (Y + kBt2100Cb * U)) / kBt2100B
221 // Simplify:
222 //   G = Y * ((1 - kBt2100R - kBt2100B) / kBt2100G)
223 //     + U * (kBt2100B * kBt2100Cb / kBt2100G)
224 //     + V * (kBt2100R * kBt2100Cr / kBt2100G)
225 //
226 // We then get the following coeficients for calculating G from YUV:
227 //
228 // Coef for Y = (1 - kBt2100R - kBt2100B) / kBt2100G = 1
229 // Coef for U = kBt2100B * kBt2100Cb / kBt2100G = kBt2100GCb = ~0.1645
230 // Coef for V = kBt2100R * kBt2100Cr / kBt2100G = kBt2100GCr = ~0.5713
231 
232 static const float kBt2100GCb = kBt2100B * kBt2100Cb / kBt2100G;
233 static const float kBt2100GCr = kBt2100R * kBt2100Cr / kBt2100G;
234 
bt2100YuvToRgb(Color e_gamma)235 Color bt2100YuvToRgb(Color e_gamma) {
236   return {{{clampPixelFloat(e_gamma.y + kBt2100Cr * e_gamma.v),
237             clampPixelFloat(e_gamma.y - kBt2100GCb * e_gamma.u - kBt2100GCr * e_gamma.v),
238             clampPixelFloat(e_gamma.y + kBt2100Cb * e_gamma.u)}}};
239 }
240 
241 // See ITU-R BT.2100-2, Table 5, HLG Reference OETF.
242 static const float kHlgA = 0.17883277f, kHlgB = 0.28466892f, kHlgC = 0.55991073f;
243 
hlgOetf(float e)244 float hlgOetf(float e) {
245   if (e <= 1.0f / 12.0f) {
246     return sqrt(3.0f * e);
247   } else {
248     return kHlgA * log(12.0f * e - kHlgB) + kHlgC;
249   }
250 }
251 
hlgOetf(Color e)252 Color hlgOetf(Color e) { return {{{hlgOetf(e.r), hlgOetf(e.g), hlgOetf(e.b)}}}; }
253 
hlgOetfLUT(float e)254 float hlgOetfLUT(float e) {
255   int32_t value = static_cast<int32_t>(e * (kHlgOETFNumEntries - 1) + 0.5);
256   // TODO() : Remove once conversion modules have appropriate clamping in place
257   value = CLIP3(value, 0, kHlgOETFNumEntries - 1);
258   static LookUpTable kHlgLut(kHlgOETFNumEntries, static_cast<float (*)(float)>(hlgOetf));
259   return kHlgLut.getTable()[value];
260 }
261 
hlgOetfLUT(Color e)262 Color hlgOetfLUT(Color e) { return {{{hlgOetfLUT(e.r), hlgOetfLUT(e.g), hlgOetfLUT(e.b)}}}; }
263 
264 // See ITU-R BT.2100-2, Table 5, HLG Reference EOTF.
hlgInvOetf(float e_gamma)265 float hlgInvOetf(float e_gamma) {
266   if (e_gamma <= 0.5f) {
267     return pow(e_gamma, 2.0f) / 3.0f;
268   } else {
269     return (exp((e_gamma - kHlgC) / kHlgA) + kHlgB) / 12.0f;
270   }
271 }
272 
hlgInvOetf(Color e_gamma)273 Color hlgInvOetf(Color e_gamma) {
274   return {{{hlgInvOetf(e_gamma.r), hlgInvOetf(e_gamma.g), hlgInvOetf(e_gamma.b)}}};
275 }
276 
hlgInvOetfLUT(float e_gamma)277 float hlgInvOetfLUT(float e_gamma) {
278   int32_t value = static_cast<int32_t>(e_gamma * (kHlgInvOETFNumEntries - 1) + 0.5);
279   // TODO() : Remove once conversion modules have appropriate clamping in place
280   value = CLIP3(value, 0, kHlgInvOETFNumEntries - 1);
281   static LookUpTable kHlgInvLut(kHlgInvOETFNumEntries, static_cast<float (*)(float)>(hlgInvOetf));
282   return kHlgInvLut.getTable()[value];
283 }
284 
hlgInvOetfLUT(Color e_gamma)285 Color hlgInvOetfLUT(Color e_gamma) {
286   return {{{hlgInvOetfLUT(e_gamma.r), hlgInvOetfLUT(e_gamma.g), hlgInvOetfLUT(e_gamma.b)}}};
287 }
288 
289 // See ITU-R BT.2100-2, Table 5, Note 5f
290 // Gamma = 1.2 + 0.42 * log(kHlgMaxNits / 1000)
291 static const float kOotfGamma = 1.2f;
292 
293 // See ITU-R BT.2100-2, Table 5, HLG Reference OOTF
hlgOotf(Color e,LuminanceFn luminance)294 Color hlgOotf(Color e, LuminanceFn luminance) {
295   float y = luminance(e);
296   return e * std::pow(y, kOotfGamma - 1.0f);
297 }
298 
hlgOotfApprox(Color e,LuminanceFn luminance)299 Color hlgOotfApprox(Color e, [[maybe_unused]] LuminanceFn luminance) {
300   return {{{std::pow(e.r, kOotfGamma), std::pow(e.g, kOotfGamma), std::pow(e.b, kOotfGamma)}}};
301 }
302 
303 // See ITU-R BT.2100-2, Table 5, Note 5i
hlgInverseOotf(Color e,LuminanceFn luminance)304 Color hlgInverseOotf(Color e, LuminanceFn luminance) {
305   float y = luminance(e);
306   return e * std::pow(y, (1.0f / kOotfGamma) - 1.0f);
307 }
308 
hlgInverseOotfApprox(Color e)309 Color hlgInverseOotfApprox(Color e) {
310   return {{{std::pow(e.r, 1.0f / kOotfGamma), std::pow(e.g, 1.0f / kOotfGamma),
311             std::pow(e.b, 1.0f / kOotfGamma)}}};
312 }
313 
314 // See ITU-R BT.2100-2, Table 4, Reference PQ OETF.
315 static const float kPqM1 = 2610.0f / 16384.0f, kPqM2 = 2523.0f / 4096.0f * 128.0f;
316 static const float kPqC1 = 3424.0f / 4096.0f, kPqC2 = 2413.0f / 4096.0f * 32.0f,
317                    kPqC3 = 2392.0f / 4096.0f * 32.0f;
318 
pqOetf(float e)319 float pqOetf(float e) {
320   if (e <= 0.0f) return 0.0f;
321   return pow((kPqC1 + kPqC2 * pow(e, kPqM1)) / (1 + kPqC3 * pow(e, kPqM1)), kPqM2);
322 }
323 
pqOetf(Color e)324 Color pqOetf(Color e) { return {{{pqOetf(e.r), pqOetf(e.g), pqOetf(e.b)}}}; }
325 
pqOetfLUT(float e)326 float pqOetfLUT(float e) {
327   int32_t value = static_cast<int32_t>(e * (kPqOETFNumEntries - 1) + 0.5);
328   // TODO() : Remove once conversion modules have appropriate clamping in place
329   value = CLIP3(value, 0, kPqOETFNumEntries - 1);
330   static LookUpTable kPqLut(kPqOETFNumEntries, static_cast<float (*)(float)>(pqOetf));
331   return kPqLut.getTable()[value];
332 }
333 
pqOetfLUT(Color e)334 Color pqOetfLUT(Color e) { return {{{pqOetfLUT(e.r), pqOetfLUT(e.g), pqOetfLUT(e.b)}}}; }
335 
pqInvOetf(float e_gamma)336 float pqInvOetf(float e_gamma) {
337   float val = pow(e_gamma, (1 / kPqM2));
338   return pow((((std::max)(val - kPqC1, 0.0f)) / (kPqC2 - kPqC3 * val)), 1 / kPqM1);
339 }
340 
pqInvOetf(Color e_gamma)341 Color pqInvOetf(Color e_gamma) {
342   return {{{pqInvOetf(e_gamma.r), pqInvOetf(e_gamma.g), pqInvOetf(e_gamma.b)}}};
343 }
344 
pqInvOetfLUT(float e_gamma)345 float pqInvOetfLUT(float e_gamma) {
346   int32_t value = static_cast<int32_t>(e_gamma * (kPqInvOETFNumEntries - 1) + 0.5);
347   // TODO() : Remove once conversion modules have appropriate clamping in place
348   value = CLIP3(value, 0, kPqInvOETFNumEntries - 1);
349   static LookUpTable kPqInvLut(kPqInvOETFNumEntries, static_cast<float (*)(float)>(pqInvOetf));
350   return kPqInvLut.getTable()[value];
351 }
352 
pqInvOetfLUT(Color e_gamma)353 Color pqInvOetfLUT(Color e_gamma) {
354   return {{{pqInvOetfLUT(e_gamma.r), pqInvOetfLUT(e_gamma.g), pqInvOetfLUT(e_gamma.b)}}};
355 }
356 
357 ////////////////////////////////////////////////////////////////////////////////
358 // Color access functions
359 
getYuv4abPixel(uhdr_raw_image_t * image,size_t x,size_t y,int h_factor,int v_factor)360 Color getYuv4abPixel(uhdr_raw_image_t* image, size_t x, size_t y, int h_factor, int v_factor) {
361   uint8_t* luma_data = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_Y]);
362   size_t luma_stride = image->stride[UHDR_PLANE_Y];
363   uint8_t* cb_data = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_U]);
364   size_t cb_stride = image->stride[UHDR_PLANE_U];
365   uint8_t* cr_data = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_V]);
366   size_t cr_stride = image->stride[UHDR_PLANE_V];
367 
368   size_t pixel_y_idx = x + y * luma_stride;
369   size_t pixel_cb_idx = x / h_factor + (y / v_factor) * cb_stride;
370   size_t pixel_cr_idx = x / h_factor + (y / v_factor) * cr_stride;
371 
372   uint8_t y_uint = luma_data[pixel_y_idx];
373   uint8_t u_uint = cb_data[pixel_cb_idx];
374   uint8_t v_uint = cr_data[pixel_cr_idx];
375 
376   // 128 bias for UV given we are using jpeglib; see:
377   // https://github.com/kornelski/libjpeg/blob/master/structure.doc
378   return {
379       {{static_cast<float>(y_uint) * (1 / 255.0f), static_cast<float>(u_uint - 128) * (1 / 255.0f),
380         static_cast<float>(v_uint - 128) * (1 / 255.0f)}}};
381 }
382 
getYuv444Pixel(uhdr_raw_image_t * image,size_t x,size_t y)383 Color getYuv444Pixel(uhdr_raw_image_t* image, size_t x, size_t y) {
384   return getYuv4abPixel(image, x, y, 1, 1);
385 }
386 
getYuv422Pixel(uhdr_raw_image_t * image,size_t x,size_t y)387 Color getYuv422Pixel(uhdr_raw_image_t* image, size_t x, size_t y) {
388   return getYuv4abPixel(image, x, y, 2, 1);
389 }
390 
getYuv420Pixel(uhdr_raw_image_t * image,size_t x,size_t y)391 Color getYuv420Pixel(uhdr_raw_image_t* image, size_t x, size_t y) {
392   return getYuv4abPixel(image, x, y, 2, 2);
393 }
394 
getYuv400Pixel(uhdr_raw_image_t * image,size_t x,size_t y)395 Color getYuv400Pixel(uhdr_raw_image_t* image, size_t x, size_t y) {
396   uint8_t* luma_data = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_Y]);
397   size_t luma_stride = image->stride[UHDR_PLANE_Y];
398   size_t pixel_y_idx = x + y * luma_stride;
399   uint8_t y_uint = luma_data[pixel_y_idx];
400 
401   return {{{static_cast<float>(y_uint) * (1 / 255.0f), 0.f, 0.f}}};
402 }
403 
getYuv444Pixel10bit(uhdr_raw_image_t * image,size_t x,size_t y)404 Color getYuv444Pixel10bit(uhdr_raw_image_t* image, size_t x, size_t y) {
405   uint16_t* luma_data = reinterpret_cast<uint16_t*>(image->planes[UHDR_PLANE_Y]);
406   size_t luma_stride = image->stride[UHDR_PLANE_Y];
407   uint16_t* cb_data = reinterpret_cast<uint16_t*>(image->planes[UHDR_PLANE_U]);
408   size_t cb_stride = image->stride[UHDR_PLANE_U];
409   uint16_t* cr_data = reinterpret_cast<uint16_t*>(image->planes[UHDR_PLANE_V]);
410   size_t cr_stride = image->stride[UHDR_PLANE_V];
411 
412   size_t pixel_y_idx = y * luma_stride + x;
413   size_t pixel_u_idx = y * cb_stride + x;
414   size_t pixel_v_idx = y * cr_stride + x;
415 
416   uint16_t y_uint = luma_data[pixel_y_idx];
417   uint16_t u_uint = cb_data[pixel_u_idx];
418   uint16_t v_uint = cr_data[pixel_v_idx];
419 
420   if (image->range == UHDR_CR_FULL_RANGE) {
421     return {{{static_cast<float>(y_uint) / 1023.0f, static_cast<float>(u_uint) / 1023.0f - 0.5f,
422               static_cast<float>(v_uint) / 1023.0f - 0.5f}}};
423   }
424 
425   // Conversions include taking narrow-range into account.
426   return {{{static_cast<float>(y_uint - 64) * (1 / 876.0f),
427             static_cast<float>(u_uint - 64) * (1 / 896.0f) - 0.5f,
428             static_cast<float>(v_uint - 64) * (1 / 896.0f) - 0.5f}}};
429 }
430 
getP010Pixel(uhdr_raw_image_t * image,size_t x,size_t y)431 Color getP010Pixel(uhdr_raw_image_t* image, size_t x, size_t y) {
432   uint16_t* luma_data = reinterpret_cast<uint16_t*>(image->planes[UHDR_PLANE_Y]);
433   size_t luma_stride = image->stride[UHDR_PLANE_Y];
434   uint16_t* chroma_data = reinterpret_cast<uint16_t*>(image->planes[UHDR_PLANE_UV]);
435   size_t chroma_stride = image->stride[UHDR_PLANE_UV];
436 
437   size_t pixel_y_idx = y * luma_stride + x;
438   size_t pixel_u_idx = (y >> 1) * chroma_stride + (x & ~0x1);
439   size_t pixel_v_idx = pixel_u_idx + 1;
440 
441   uint16_t y_uint = luma_data[pixel_y_idx] >> 6;
442   uint16_t u_uint = chroma_data[pixel_u_idx] >> 6;
443   uint16_t v_uint = chroma_data[pixel_v_idx] >> 6;
444 
445   if (image->range == UHDR_CR_FULL_RANGE) {
446     return {{{static_cast<float>(y_uint) / 1023.0f, static_cast<float>(u_uint) / 1023.0f - 0.5f,
447               static_cast<float>(v_uint) / 1023.0f - 0.5f}}};
448   }
449 
450   // Conversions include taking narrow-range into account.
451   return {{{static_cast<float>(y_uint - 64) * (1 / 876.0f),
452             static_cast<float>(u_uint - 64) * (1 / 896.0f) - 0.5f,
453             static_cast<float>(v_uint - 64) * (1 / 896.0f) - 0.5f}}};
454 }
455 
getRgb888Pixel(uhdr_raw_image_t * image,size_t x,size_t y)456 Color getRgb888Pixel(uhdr_raw_image_t* image, size_t x, size_t y) {
457   uint8_t* rgbData = static_cast<uint8_t*>(image->planes[UHDR_PLANE_PACKED]);
458   unsigned int srcStride = image->stride[UHDR_PLANE_PACKED];
459   size_t offset = x * 3 + y * srcStride * 3;
460   Color pixel;
461   pixel.r = float(rgbData[offset]);
462   pixel.g = float(rgbData[offset + 1]);
463   pixel.b = float(rgbData[offset + 2]);
464   return pixel / 255.0f;
465 }
466 
getRgba8888Pixel(uhdr_raw_image_t * image,size_t x,size_t y)467 Color getRgba8888Pixel(uhdr_raw_image_t* image, size_t x, size_t y) {
468   uint32_t* rgbData = static_cast<uint32_t*>(image->planes[UHDR_PLANE_PACKED]);
469   unsigned int srcStride = image->stride[UHDR_PLANE_PACKED];
470 
471   Color pixel;
472   pixel.r = float(rgbData[x + y * srcStride] & 0xff);
473   pixel.g = float((rgbData[x + y * srcStride] >> 8) & 0xff);
474   pixel.b = float((rgbData[x + y * srcStride] >> 16) & 0xff);
475   return pixel / 255.0f;
476 }
477 
getRgba1010102Pixel(uhdr_raw_image_t * image,size_t x,size_t y)478 Color getRgba1010102Pixel(uhdr_raw_image_t* image, size_t x, size_t y) {
479   uint32_t* rgbData = static_cast<uint32_t*>(image->planes[UHDR_PLANE_PACKED]);
480   unsigned int srcStride = image->stride[UHDR_PLANE_PACKED];
481 
482   Color pixel;
483   pixel.r = float(rgbData[x + y * srcStride] & 0x3ff);
484   pixel.g = float((rgbData[x + y * srcStride] >> 10) & 0x3ff);
485   pixel.b = float((rgbData[x + y * srcStride] >> 20) & 0x3ff);
486   return pixel / 1023.0f;
487 }
488 
getRgbaF16Pixel(uhdr_raw_image_t * image,size_t x,size_t y)489 Color getRgbaF16Pixel(uhdr_raw_image_t* image, size_t x, size_t y) {
490   uint64_t* rgbData = static_cast<uint64_t*>(image->planes[UHDR_PLANE_PACKED]);
491   unsigned int srcStride = image->stride[UHDR_PLANE_PACKED];
492 
493   Color pixel;
494   pixel.r = halfToFloat(rgbData[x + y * srcStride] & 0xffff);
495   pixel.g = halfToFloat((rgbData[x + y * srcStride] >> 16) & 0xffff);
496   pixel.b = halfToFloat((rgbData[x + y * srcStride] >> 32) & 0xffff);
497   return sanitizePixel(pixel);
498 }
499 
samplePixels(uhdr_raw_image_t * image,size_t map_scale_factor,size_t x,size_t y,GetPixelFn get_pixel_fn)500 static Color samplePixels(uhdr_raw_image_t* image, size_t map_scale_factor, size_t x, size_t y,
501                           GetPixelFn get_pixel_fn) {
502   Color e = {{{0.0f, 0.0f, 0.0f}}};
503   for (size_t dy = 0; dy < map_scale_factor; ++dy) {
504     for (size_t dx = 0; dx < map_scale_factor; ++dx) {
505       e += get_pixel_fn(image, x * map_scale_factor + dx, y * map_scale_factor + dy);
506     }
507   }
508 
509   return e / static_cast<float>(map_scale_factor * map_scale_factor);
510 }
511 
sampleYuv444(uhdr_raw_image_t * image,size_t map_scale_factor,size_t x,size_t y)512 Color sampleYuv444(uhdr_raw_image_t* image, size_t map_scale_factor, size_t x, size_t y) {
513   return samplePixels(image, map_scale_factor, x, y, getYuv444Pixel);
514 }
515 
sampleYuv422(uhdr_raw_image_t * image,size_t map_scale_factor,size_t x,size_t y)516 Color sampleYuv422(uhdr_raw_image_t* image, size_t map_scale_factor, size_t x, size_t y) {
517   return samplePixels(image, map_scale_factor, x, y, getYuv422Pixel);
518 }
519 
sampleYuv420(uhdr_raw_image_t * image,size_t map_scale_factor,size_t x,size_t y)520 Color sampleYuv420(uhdr_raw_image_t* image, size_t map_scale_factor, size_t x, size_t y) {
521   return samplePixels(image, map_scale_factor, x, y, getYuv420Pixel);
522 }
523 
sampleP010(uhdr_raw_image_t * image,size_t map_scale_factor,size_t x,size_t y)524 Color sampleP010(uhdr_raw_image_t* image, size_t map_scale_factor, size_t x, size_t y) {
525   return samplePixels(image, map_scale_factor, x, y, getP010Pixel);
526 }
527 
sampleYuv44410bit(uhdr_raw_image_t * image,size_t map_scale_factor,size_t x,size_t y)528 Color sampleYuv44410bit(uhdr_raw_image_t* image, size_t map_scale_factor, size_t x, size_t y) {
529   return samplePixels(image, map_scale_factor, x, y, getYuv444Pixel10bit);
530 }
531 
sampleRgba8888(uhdr_raw_image_t * image,size_t map_scale_factor,size_t x,size_t y)532 Color sampleRgba8888(uhdr_raw_image_t* image, size_t map_scale_factor, size_t x, size_t y) {
533   return samplePixels(image, map_scale_factor, x, y, getRgba8888Pixel);
534 }
535 
sampleRgba1010102(uhdr_raw_image_t * image,size_t map_scale_factor,size_t x,size_t y)536 Color sampleRgba1010102(uhdr_raw_image_t* image, size_t map_scale_factor, size_t x, size_t y) {
537   return samplePixels(image, map_scale_factor, x, y, getRgba1010102Pixel);
538 }
539 
sampleRgbaF16(uhdr_raw_image_t * image,size_t map_scale_factor,size_t x,size_t y)540 Color sampleRgbaF16(uhdr_raw_image_t* image, size_t map_scale_factor, size_t x, size_t y) {
541   return samplePixels(image, map_scale_factor, x, y, getRgbaF16Pixel);
542 }
543 
putRgba8888Pixel(uhdr_raw_image_t * image,size_t x,size_t y,Color & pixel)544 void putRgba8888Pixel(uhdr_raw_image_t* image, size_t x, size_t y, Color& pixel) {
545   uint32_t* rgbData = static_cast<uint32_t*>(image->planes[UHDR_PLANE_PACKED]);
546   unsigned int srcStride = image->stride[UHDR_PLANE_PACKED];
547 
548   pixel *= 255.0f;
549   pixel += 0.5f;
550   pixel.r = CLIP3(pixel.r, 0.0f, 255.0f);
551   pixel.g = CLIP3(pixel.g, 0.0f, 255.0f);
552   pixel.b = CLIP3(pixel.b, 0.0f, 255.0f);
553 
554   int32_t r0 = int32_t(pixel.r);
555   int32_t g0 = int32_t(pixel.g);
556   int32_t b0 = int32_t(pixel.b);
557   rgbData[x + y * srcStride] = r0 | (g0 << 8) | (b0 << 16) | (255 << 24);  // Set alpha to 1.0
558 }
559 
putRgb888Pixel(uhdr_raw_image_t * image,size_t x,size_t y,Color & pixel)560 void putRgb888Pixel(uhdr_raw_image_t* image, size_t x, size_t y, Color& pixel) {
561   uint8_t* rgbData = static_cast<uint8_t*>(image->planes[UHDR_PLANE_PACKED]);
562   unsigned int srcStride = image->stride[UHDR_PLANE_PACKED];
563   size_t offset = x * 3 + y * srcStride * 3;
564   pixel *= 255.0f;
565   pixel += 0.5f;
566   pixel.r = CLIP3(pixel.r, 0.0f, 255.0f);
567   pixel.g = CLIP3(pixel.g, 0.0f, 255.0f);
568   pixel.b = CLIP3(pixel.b, 0.0f, 255.0f);
569   rgbData[offset] = uint8_t(pixel.r);
570   rgbData[offset + 1] = uint8_t(pixel.r);
571   rgbData[offset + 2] = uint8_t(pixel.b);
572 }
573 
putYuv400Pixel(uhdr_raw_image_t * image,size_t x,size_t y,Color & pixel)574 void putYuv400Pixel(uhdr_raw_image_t* image, size_t x, size_t y, Color& pixel) {
575   uint8_t* luma_data = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_Y]);
576   size_t luma_stride = image->stride[UHDR_PLANE_Y];
577 
578   pixel *= 255.0f;
579   pixel += 0.5f;
580   pixel.y = CLIP3(pixel.y, 0.0f, 255.0f);
581 
582   luma_data[x + y * luma_stride] = uint8_t(pixel.y);
583 }
584 
putYuv444Pixel(uhdr_raw_image_t * image,size_t x,size_t y,Color & pixel)585 void putYuv444Pixel(uhdr_raw_image_t* image, size_t x, size_t y, Color& pixel) {
586   uint8_t* luma_data = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_Y]);
587   uint8_t* cb_data = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_U]);
588   uint8_t* cr_data = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_V]);
589   size_t luma_stride = image->stride[UHDR_PLANE_Y];
590   size_t cb_stride = image->stride[UHDR_PLANE_U];
591   size_t cr_stride = image->stride[UHDR_PLANE_V];
592 
593   pixel *= 255.0f;
594   pixel += 0.5f;
595   pixel.y = CLIP3(pixel.y, 0.0f, 255.0f);
596   pixel.u = CLIP3(pixel.u, 0.0f, 255.0f);
597   pixel.v = CLIP3(pixel.v, 0.0f, 255.0f);
598 
599   luma_data[x + y * luma_stride] = uint8_t(pixel.y);
600   cb_data[x + y * cb_stride] = uint8_t(pixel.u);
601   cr_data[x + y * cr_stride] = uint8_t(pixel.v);
602 }
603 
604 ////////////////////////////////////////////////////////////////////////////////
605 // Color space conversions
606 // Sample, See,
607 // https://registry.khronos.org/DataFormat/specs/1.3/dataformat.1.3.html#_bt_709_bt_2020_primary_conversion_example
608 
609 const std::array<float, 9> kBt709ToP3 = {0.822462f,  0.177537f, 0.000001f, 0.033194f, 0.966807f,
610                                          -0.000001f, 0.017083f, 0.072398f, 0.91052f};
611 const std::array<float, 9> kBt709ToBt2100 = {0.627404f, 0.329282f, 0.043314f, 0.069097f, 0.919541f,
612                                              0.011362f, 0.016392f, 0.088013f, 0.895595f};
613 const std::array<float, 9> kP3ToBt709 = {1.22494f, -0.22494f,  0.0f,       -0.042057f, 1.042057f,
614                                          0.0f,     -0.019638f, -0.078636f, 1.098274f};
615 const std::array<float, 9> kP3ToBt2100 = {0.753833f, 0.198597f, 0.04757f,  0.045744f, 0.941777f,
616                                           0.012479f, -0.00121f, 0.017601f, 0.983608f};
617 const std::array<float, 9> kBt2100ToBt709 = {1.660491f,  -0.587641f, -0.07285f,
618                                              -0.124551f, 1.1329f,    -0.008349f,
619                                              -0.018151f, -0.100579f, 1.11873f};
620 const std::array<float, 9> kBt2100ToP3 = {1.343578f, -0.282179f, -0.061399f, -0.065298f, 1.075788f,
621                                           -0.01049f, 0.002822f,  -0.019598f, 1.016777f};
622 
ConvertGamut(Color e,const std::array<float,9> & coeffs)623 Color ConvertGamut(Color e, const std::array<float, 9>& coeffs) {
624   return {{{coeffs[0] * e.r + coeffs[1] * e.g + coeffs[2] * e.b,
625             coeffs[3] * e.r + coeffs[4] * e.g + coeffs[5] * e.b,
626             coeffs[6] * e.r + coeffs[7] * e.g + coeffs[8] * e.b}}};
627 }
bt709ToP3(Color e)628 Color bt709ToP3(Color e) { return ConvertGamut(e, kBt709ToP3); }
bt709ToBt2100(Color e)629 Color bt709ToBt2100(Color e) { return ConvertGamut(e, kBt709ToBt2100); }
p3ToBt709(Color e)630 Color p3ToBt709(Color e) { return ConvertGamut(e, kP3ToBt709); }
p3ToBt2100(Color e)631 Color p3ToBt2100(Color e) { return ConvertGamut(e, kP3ToBt2100); }
bt2100ToBt709(Color e)632 Color bt2100ToBt709(Color e) { return ConvertGamut(e, kBt2100ToBt709); }
bt2100ToP3(Color e)633 Color bt2100ToP3(Color e) { return ConvertGamut(e, kBt2100ToP3); }
634 
635 // All of these conversions are derived from the respective input YUV->RGB conversion followed by
636 // the RGB->YUV for the receiving encoding. They are consistent with the RGB<->YUV functions in
637 // gainmapmath.cpp, given that we use BT.709 encoding for sRGB and BT.601 encoding for Display-P3,
638 // to match DataSpace.
639 
640 // Yuv Bt709 -> Yuv Bt601
641 // Y' = (1.0 * Y) + ( 0.101579 * U) + ( 0.196076 * V)
642 // U' = (0.0 * Y) + ( 0.989854 * U) + (-0.110653 * V)
643 // V' = (0.0 * Y) + (-0.072453 * U) + ( 0.983398 * V)
644 const std::array<float, 9> kYuvBt709ToBt601 = {
645     1.0f, 0.101579f, 0.196076f, 0.0f, 0.989854f, -0.110653f, 0.0f, -0.072453f, 0.983398f};
646 
647 // Yuv Bt709 -> Yuv Bt2100
648 // Y' = (1.0 * Y) + (-0.016969 * U) + ( 0.096312 * V)
649 // U' = (0.0 * Y) + ( 0.995306 * U) + (-0.051192 * V)
650 // V' = (0.0 * Y) + ( 0.011507 * U) + ( 1.002637 * V)
651 const std::array<float, 9> kYuvBt709ToBt2100 = {
652     1.0f, -0.016969f, 0.096312f, 0.0f, 0.995306f, -0.051192f, 0.0f, 0.011507f, 1.002637f};
653 
654 // Yuv Bt601 -> Yuv Bt709
655 // Y' = (1.0 * Y) + (-0.118188 * U) + (-0.212685 * V)
656 // U' = (0.0 * Y) + ( 1.018640 * U) + ( 0.114618 * V)
657 // V' = (0.0 * Y) + ( 0.075049 * U) + ( 1.025327 * V)
658 const std::array<float, 9> kYuvBt601ToBt709 = {
659     1.0f, -0.118188f, -0.212685f, 0.0f, 1.018640f, 0.114618f, 0.0f, 0.075049f, 1.025327f};
660 
661 // Yuv Bt601 -> Yuv Bt2100
662 // Y' = (1.0 * Y) + (-0.128245 * U) + (-0.115879 * V)
663 // U' = (0.0 * Y) + ( 1.010016 * U) + ( 0.061592 * V)
664 // V' = (0.0 * Y) + ( 0.086969 * U) + ( 1.029350 * V)
665 const std::array<float, 9> kYuvBt601ToBt2100 = {
666     1.0f, -0.128245f, -0.115879, 0.0f, 1.010016f, 0.061592f, 0.0f, 0.086969f, 1.029350f};
667 
668 // Yuv Bt2100 -> Yuv Bt709
669 // Y' = (1.0 * Y) + ( 0.018149 * U) + (-0.095132 * V)
670 // U' = (0.0 * Y) + ( 1.004123 * U) + ( 0.051267 * V)
671 // V' = (0.0 * Y) + (-0.011524 * U) + ( 0.996782 * V)
672 const std::array<float, 9> kYuvBt2100ToBt709 = {
673     1.0f, 0.018149f, -0.095132f, 0.0f, 1.004123f, 0.051267f, 0.0f, -0.011524f, 0.996782f};
674 
675 // Yuv Bt2100 -> Yuv Bt601
676 // Y' = (1.0 * Y) + ( 0.117887 * U) + ( 0.105521 * V)
677 // U' = (0.0 * Y) + ( 0.995211 * U) + (-0.059549 * V)
678 // V' = (0.0 * Y) + (-0.084085 * U) + ( 0.976518 * V)
679 const std::array<float, 9> kYuvBt2100ToBt601 = {
680     1.0f, 0.117887f, 0.105521f, 0.0f, 0.995211f, -0.059549f, 0.0f, -0.084085f, 0.976518f};
681 
yuvColorGamutConversion(Color e_gamma,const std::array<float,9> & coeffs)682 Color yuvColorGamutConversion(Color e_gamma, const std::array<float, 9>& coeffs) {
683   const float y = e_gamma.y * std::get<0>(coeffs) + e_gamma.u * std::get<1>(coeffs) +
684                   e_gamma.v * std::get<2>(coeffs);
685   const float u = e_gamma.y * std::get<3>(coeffs) + e_gamma.u * std::get<4>(coeffs) +
686                   e_gamma.v * std::get<5>(coeffs);
687   const float v = e_gamma.y * std::get<6>(coeffs) + e_gamma.u * std::get<7>(coeffs) +
688                   e_gamma.v * std::get<8>(coeffs);
689   return {{{y, u, v}}};
690 }
691 
transformYuv420(uhdr_raw_image_t * image,const std::array<float,9> & coeffs)692 void transformYuv420(uhdr_raw_image_t* image, const std::array<float, 9>& coeffs) {
693   for (size_t y = 0; y < image->h / 2; ++y) {
694     for (size_t x = 0; x < image->w / 2; ++x) {
695       Color yuv1 = getYuv420Pixel(image, x * 2, y * 2);
696       Color yuv2 = getYuv420Pixel(image, x * 2 + 1, y * 2);
697       Color yuv3 = getYuv420Pixel(image, x * 2, y * 2 + 1);
698       Color yuv4 = getYuv420Pixel(image, x * 2 + 1, y * 2 + 1);
699 
700       yuv1 = yuvColorGamutConversion(yuv1, coeffs);
701       yuv2 = yuvColorGamutConversion(yuv2, coeffs);
702       yuv3 = yuvColorGamutConversion(yuv3, coeffs);
703       yuv4 = yuvColorGamutConversion(yuv4, coeffs);
704 
705       Color new_uv = (yuv1 + yuv2 + yuv3 + yuv4) / 4.0f;
706 
707       size_t pixel_y1_idx = x * 2 + y * 2 * image->stride[UHDR_PLANE_Y];
708       size_t pixel_y2_idx = (x * 2 + 1) + y * 2 * image->stride[UHDR_PLANE_Y];
709       size_t pixel_y3_idx = x * 2 + (y * 2 + 1) * image->stride[UHDR_PLANE_Y];
710       size_t pixel_y4_idx = (x * 2 + 1) + (y * 2 + 1) * image->stride[UHDR_PLANE_Y];
711 
712       uint8_t& y1_uint = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_Y])[pixel_y1_idx];
713       uint8_t& y2_uint = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_Y])[pixel_y2_idx];
714       uint8_t& y3_uint = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_Y])[pixel_y3_idx];
715       uint8_t& y4_uint = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_Y])[pixel_y4_idx];
716 
717       size_t pixel_u_idx = x + y * image->stride[UHDR_PLANE_U];
718       uint8_t& u_uint = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_U])[pixel_u_idx];
719 
720       size_t pixel_v_idx = x + y * image->stride[UHDR_PLANE_V];
721       uint8_t& v_uint = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_V])[pixel_v_idx];
722 
723       y1_uint = static_cast<uint8_t>(CLIP3((yuv1.y * 255.0f + 0.5f), 0, 255));
724       y2_uint = static_cast<uint8_t>(CLIP3((yuv2.y * 255.0f + 0.5f), 0, 255));
725       y3_uint = static_cast<uint8_t>(CLIP3((yuv3.y * 255.0f + 0.5f), 0, 255));
726       y4_uint = static_cast<uint8_t>(CLIP3((yuv4.y * 255.0f + 0.5f), 0, 255));
727 
728       u_uint = static_cast<uint8_t>(CLIP3((new_uv.u * 255.0f + 128.0f + 0.5f), 0, 255));
729       v_uint = static_cast<uint8_t>(CLIP3((new_uv.v * 255.0f + 128.0f + 0.5f), 0, 255));
730     }
731   }
732 }
733 
transformYuv444(uhdr_raw_image_t * image,const std::array<float,9> & coeffs)734 void transformYuv444(uhdr_raw_image_t* image, const std::array<float, 9>& coeffs) {
735   for (size_t y = 0; y < image->h; ++y) {
736     for (size_t x = 0; x < image->w; ++x) {
737       Color yuv = getYuv444Pixel(image, x, y);
738       yuv = yuvColorGamutConversion(yuv, coeffs);
739 
740       size_t pixel_y_idx = x + y * image->stride[UHDR_PLANE_Y];
741       uint8_t& y1_uint = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_Y])[pixel_y_idx];
742 
743       size_t pixel_u_idx = x + y * image->stride[UHDR_PLANE_U];
744       uint8_t& u_uint = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_U])[pixel_u_idx];
745 
746       size_t pixel_v_idx = x + y * image->stride[UHDR_PLANE_V];
747       uint8_t& v_uint = reinterpret_cast<uint8_t*>(image->planes[UHDR_PLANE_V])[pixel_v_idx];
748 
749       y1_uint = static_cast<uint8_t>(CLIP3((yuv.y * 255.0f + 0.5f), 0, 255));
750       u_uint = static_cast<uint8_t>(CLIP3((yuv.u * 255.0f + 128.0f + 0.5f), 0, 255));
751       v_uint = static_cast<uint8_t>(CLIP3((yuv.v * 255.0f + 128.0f + 0.5f), 0, 255));
752     }
753   }
754 }
755 
756 ////////////////////////////////////////////////////////////////////////////////
757 // Gain map calculations
758 
encodeGain(float y_sdr,float y_hdr,uhdr_gainmap_metadata_ext_t * metadata,int index)759 uint8_t encodeGain(float y_sdr, float y_hdr, uhdr_gainmap_metadata_ext_t* metadata, int index) {
760   return encodeGain(y_sdr, y_hdr, metadata, log2(metadata->min_content_boost[index]),
761                     log2(metadata->max_content_boost[index]), index);
762 }
763 
encodeGain(float y_sdr,float y_hdr,uhdr_gainmap_metadata_ext_t * metadata,float log2MinContentBoost,float log2MaxContentBoost,int index)764 uint8_t encodeGain(float y_sdr, float y_hdr, uhdr_gainmap_metadata_ext_t* metadata,
765                    float log2MinContentBoost, float log2MaxContentBoost, int index) {
766   float gain = 1.0f;
767   if (y_sdr > 0.0f) {
768     gain = y_hdr / y_sdr;
769   }
770 
771   if (gain < metadata->min_content_boost[index]) gain = metadata->min_content_boost[index];
772   if (gain > metadata->max_content_boost[index]) gain = metadata->max_content_boost[index];
773   float gain_normalized =
774       (log2(gain) - log2MinContentBoost) / (log2MaxContentBoost - log2MinContentBoost);
775   float gain_normalized_gamma = powf(gain_normalized, metadata->gamma[index]);
776   return static_cast<uint8_t>(gain_normalized_gamma * 255.0f);
777 }
778 
computeGain(float sdr,float hdr)779 float computeGain(float sdr, float hdr) {
780   float gain = log2((hdr + kHdrOffset) / (sdr + kSdrOffset));
781   if (sdr < 2.f / 255.0f) {
782     // If sdr is zero and hdr is non zero, it can result in very large gain values. In compression -
783     // decompression process, if the same sdr pixel increases to 1, the hdr recovered pixel will
784     // blow out. Dont allow dark pixels to signal large gains.
785     gain = (std::min)(gain, 2.3f);
786   }
787   return gain;
788 }
789 
affineMapGain(float gainlog2,float mingainlog2,float maxgainlog2,float gamma)790 uint8_t affineMapGain(float gainlog2, float mingainlog2, float maxgainlog2, float gamma) {
791   float mappedVal = (gainlog2 - mingainlog2) / (maxgainlog2 - mingainlog2);
792   if (gamma != 1.0f) mappedVal = pow(mappedVal, gamma);
793   mappedVal *= 255;
794   return CLIP3(mappedVal + 0.5f, 0, 255);
795 }
796 
applyGain(Color e,float gain,uhdr_gainmap_metadata_ext_t * metadata)797 Color applyGain(Color e, float gain, uhdr_gainmap_metadata_ext_t* metadata) {
798   if (metadata->gamma[0] != 1.0f) gain = pow(gain, 1.0f / metadata->gamma[0]);
799   float logBoost = log2(metadata->min_content_boost[0]) * (1.0f - gain) +
800                    log2(metadata->max_content_boost[0]) * gain;
801   float gainFactor = exp2(logBoost);
802   return ((e + metadata->offset_sdr[0]) * gainFactor) - metadata->offset_hdr[0];
803 }
804 
applyGain(Color e,float gain,uhdr_gainmap_metadata_ext_t * metadata,float gainmapWeight)805 Color applyGain(Color e, float gain, uhdr_gainmap_metadata_ext_t* metadata, float gainmapWeight) {
806   if (metadata->gamma[0] != 1.0f) gain = pow(gain, 1.0f / metadata->gamma[0]);
807   float logBoost = log2(metadata->min_content_boost[0]) * (1.0f - gain) +
808                    log2(metadata->max_content_boost[0]) * gain;
809   float gainFactor = exp2(logBoost * gainmapWeight);
810   return ((e + metadata->offset_sdr[0]) * gainFactor) - metadata->offset_hdr[0];
811 }
812 
applyGainLUT(Color e,float gain,GainLUT & gainLUT,uhdr_gainmap_metadata_ext_t * metadata)813 Color applyGainLUT(Color e, float gain, GainLUT& gainLUT, uhdr_gainmap_metadata_ext_t* metadata) {
814   float gainFactor = gainLUT.getGainFactor(gain, 0);
815   return ((e + metadata->offset_sdr[0]) * gainFactor) - metadata->offset_hdr[0];
816 }
817 
applyGain(Color e,Color gain,uhdr_gainmap_metadata_ext_t * metadata)818 Color applyGain(Color e, Color gain, uhdr_gainmap_metadata_ext_t* metadata) {
819   if (metadata->gamma[0] != 1.0f) gain.r = pow(gain.r, 1.0f / metadata->gamma[0]);
820   if (metadata->gamma[1] != 1.0f) gain.g = pow(gain.g, 1.0f / metadata->gamma[1]);
821   if (metadata->gamma[2] != 1.0f) gain.b = pow(gain.b, 1.0f / metadata->gamma[2]);
822   float logBoostR = log2(metadata->min_content_boost[0]) * (1.0f - gain.r) +
823                     log2(metadata->max_content_boost[0]) * gain.r;
824   float logBoostG = log2(metadata->min_content_boost[1]) * (1.0f - gain.g) +
825                     log2(metadata->max_content_boost[1]) * gain.g;
826   float logBoostB = log2(metadata->min_content_boost[2]) * (1.0f - gain.b) +
827                     log2(metadata->max_content_boost[2]) * gain.b;
828   float gainFactorR = exp2(logBoostR);
829   float gainFactorG = exp2(logBoostG);
830   float gainFactorB = exp2(logBoostB);
831   return {{{((e.r + metadata->offset_sdr[0]) * gainFactorR) - metadata->offset_hdr[0],
832             ((e.g + metadata->offset_sdr[1]) * gainFactorG) - metadata->offset_hdr[1],
833             ((e.b + metadata->offset_sdr[2]) * gainFactorB) - metadata->offset_hdr[2]}}};
834 }
835 
applyGain(Color e,Color gain,uhdr_gainmap_metadata_ext_t * metadata,float gainmapWeight)836 Color applyGain(Color e, Color gain, uhdr_gainmap_metadata_ext_t* metadata, float gainmapWeight) {
837   if (metadata->gamma[0] != 1.0f) gain.r = pow(gain.r, 1.0f / metadata->gamma[0]);
838   if (metadata->gamma[1] != 1.0f) gain.g = pow(gain.g, 1.0f / metadata->gamma[1]);
839   if (metadata->gamma[2] != 1.0f) gain.b = pow(gain.b, 1.0f / metadata->gamma[2]);
840   float logBoostR = log2(metadata->min_content_boost[0]) * (1.0f - gain.r) +
841                     log2(metadata->max_content_boost[0]) * gain.r;
842   float logBoostG = log2(metadata->min_content_boost[1]) * (1.0f - gain.g) +
843                     log2(metadata->max_content_boost[1]) * gain.g;
844   float logBoostB = log2(metadata->min_content_boost[2]) * (1.0f - gain.b) +
845                     log2(metadata->max_content_boost[2]) * gain.b;
846   float gainFactorR = exp2(logBoostR * gainmapWeight);
847   float gainFactorG = exp2(logBoostG * gainmapWeight);
848   float gainFactorB = exp2(logBoostB * gainmapWeight);
849   return {{{((e.r + metadata->offset_sdr[0]) * gainFactorR) - metadata->offset_hdr[0],
850             ((e.g + metadata->offset_sdr[1]) * gainFactorG) - metadata->offset_hdr[1],
851             ((e.b + metadata->offset_sdr[2]) * gainFactorB) - metadata->offset_hdr[2]}}};
852 }
853 
applyGainLUT(Color e,Color gain,GainLUT & gainLUT,uhdr_gainmap_metadata_ext_t * metadata)854 Color applyGainLUT(Color e, Color gain, GainLUT& gainLUT, uhdr_gainmap_metadata_ext_t* metadata) {
855   float gainFactorR = gainLUT.getGainFactor(gain.r, 0);
856   float gainFactorG = gainLUT.getGainFactor(gain.g, 1);
857   float gainFactorB = gainLUT.getGainFactor(gain.b, 2);
858   return {{{((e.r + metadata->offset_sdr[0]) * gainFactorR) - metadata->offset_hdr[0],
859             ((e.g + metadata->offset_sdr[1]) * gainFactorG) - metadata->offset_hdr[1],
860             ((e.b + metadata->offset_sdr[2]) * gainFactorB) - metadata->offset_hdr[2]}}};
861 }
862 
863 // TODO: do we need something more clever for filtering either the map or images
864 // to generate the map?
865 
clamp(const size_t & val,const size_t & low,const size_t & high)866 static size_t clamp(const size_t& val, const size_t& low, const size_t& high) {
867   return val < low ? low : (high < val ? high : val);
868 }
869 
mapUintToFloat(uint8_t map_uint)870 static float mapUintToFloat(uint8_t map_uint) { return static_cast<float>(map_uint) / 255.0f; }
871 
pythDistance(float x_diff,float y_diff)872 static float pythDistance(float x_diff, float y_diff) {
873   return sqrt(pow(x_diff, 2.0f) + pow(y_diff, 2.0f));
874 }
875 
876 // TODO: If map_scale_factor is guaranteed to be an integer, then remove the following.
sampleMap(uhdr_raw_image_t * map,float map_scale_factor,size_t x,size_t y)877 float sampleMap(uhdr_raw_image_t* map, float map_scale_factor, size_t x, size_t y) {
878   float x_map = static_cast<float>(x) / map_scale_factor;
879   float y_map = static_cast<float>(y) / map_scale_factor;
880 
881   size_t x_lower = static_cast<size_t>(floor(x_map));
882   size_t x_upper = x_lower + 1;
883   size_t y_lower = static_cast<size_t>(floor(y_map));
884   size_t y_upper = y_lower + 1;
885 
886   x_lower = clamp(x_lower, 0, map->w - 1);
887   x_upper = clamp(x_upper, 0, map->w - 1);
888   y_lower = clamp(y_lower, 0, map->h - 1);
889   y_upper = clamp(y_upper, 0, map->h - 1);
890 
891   // Use Shepard's method for inverse distance weighting. For more information:
892   // en.wikipedia.org/wiki/Inverse_distance_weighting#Shepard's_method
893   uint8_t* data = reinterpret_cast<uint8_t*>(map->planes[UHDR_PLANE_Y]);
894   size_t stride = map->stride[UHDR_PLANE_Y];
895 
896   float e1 = mapUintToFloat(data[x_lower + y_lower * stride]);
897   float e1_dist =
898       pythDistance(x_map - static_cast<float>(x_lower), y_map - static_cast<float>(y_lower));
899   if (e1_dist == 0.0f) return e1;
900 
901   float e2 = mapUintToFloat(data[x_lower + y_upper * stride]);
902   float e2_dist =
903       pythDistance(x_map - static_cast<float>(x_lower), y_map - static_cast<float>(y_upper));
904   if (e2_dist == 0.0f) return e2;
905 
906   float e3 = mapUintToFloat(data[x_upper + y_lower * stride]);
907   float e3_dist =
908       pythDistance(x_map - static_cast<float>(x_upper), y_map - static_cast<float>(y_lower));
909   if (e3_dist == 0.0f) return e3;
910 
911   float e4 = mapUintToFloat(data[x_upper + y_upper * stride]);
912   float e4_dist =
913       pythDistance(x_map - static_cast<float>(x_upper), y_map - static_cast<float>(y_upper));
914   if (e4_dist == 0.0f) return e2;
915 
916   float e1_weight = 1.0f / e1_dist;
917   float e2_weight = 1.0f / e2_dist;
918   float e3_weight = 1.0f / e3_dist;
919   float e4_weight = 1.0f / e4_dist;
920   float total_weight = e1_weight + e2_weight + e3_weight + e4_weight;
921 
922   return e1 * (e1_weight / total_weight) + e2 * (e2_weight / total_weight) +
923          e3 * (e3_weight / total_weight) + e4 * (e4_weight / total_weight);
924 }
925 
sampleMap(uhdr_raw_image_t * map,size_t map_scale_factor,size_t x,size_t y,ShepardsIDW & weightTables)926 float sampleMap(uhdr_raw_image_t* map, size_t map_scale_factor, size_t x, size_t y,
927                 ShepardsIDW& weightTables) {
928   // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the
929   // following by computing log2(map_scale_factor) once and then using >> log2(map_scale_factor)
930   size_t x_lower = x / map_scale_factor;
931   size_t x_upper = x_lower + 1;
932   size_t y_lower = y / map_scale_factor;
933   size_t y_upper = y_lower + 1;
934 
935   x_lower = std::min(x_lower, (size_t)map->w - 1);
936   x_upper = std::min(x_upper, (size_t)map->w - 1);
937   y_lower = std::min(y_lower, (size_t)map->h - 1);
938   y_upper = std::min(y_upper, (size_t)map->h - 1);
939 
940   uint8_t* data = reinterpret_cast<uint8_t*>(map->planes[UHDR_PLANE_Y]);
941   size_t stride = map->stride[UHDR_PLANE_Y];
942   float e1 = mapUintToFloat(data[x_lower + y_lower * stride]);
943   float e2 = mapUintToFloat(data[x_lower + y_upper * stride]);
944   float e3 = mapUintToFloat(data[x_upper + y_lower * stride]);
945   float e4 = mapUintToFloat(data[x_upper + y_upper * stride]);
946 
947   // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the
948   // following by using & (map_scale_factor - 1)
949   size_t offset_x = x % map_scale_factor;
950   size_t offset_y = y % map_scale_factor;
951 
952   float* weights = weightTables.mWeights;
953   if (x_lower == x_upper && y_lower == y_upper)
954     weights = weightTables.mWeightsC;
955   else if (x_lower == x_upper)
956     weights = weightTables.mWeightsNR;
957   else if (y_lower == y_upper)
958     weights = weightTables.mWeightsNB;
959   weights += offset_y * map_scale_factor * 4 + offset_x * 4;
960 
961   return e1 * weights[0] + e2 * weights[1] + e3 * weights[2] + e4 * weights[3];
962 }
963 
sampleMap3Channel(uhdr_raw_image_t * map,float map_scale_factor,size_t x,size_t y,bool has_alpha)964 Color sampleMap3Channel(uhdr_raw_image_t* map, float map_scale_factor, size_t x, size_t y,
965                         bool has_alpha) {
966   float x_map = static_cast<float>(x) / map_scale_factor;
967   float y_map = static_cast<float>(y) / map_scale_factor;
968 
969   size_t x_lower = static_cast<size_t>(floor(x_map));
970   size_t x_upper = x_lower + 1;
971   size_t y_lower = static_cast<size_t>(floor(y_map));
972   size_t y_upper = y_lower + 1;
973 
974   x_lower = std::min(x_lower, (size_t)map->w - 1);
975   x_upper = std::min(x_upper, (size_t)map->w - 1);
976   y_lower = std::min(y_lower, (size_t)map->h - 1);
977   y_upper = std::min(y_upper, (size_t)map->h - 1);
978 
979   int factor = has_alpha ? 4 : 3;
980 
981   uint8_t* data = reinterpret_cast<uint8_t*>(map->planes[UHDR_PLANE_PACKED]);
982   size_t stride = map->stride[UHDR_PLANE_PACKED];
983 
984   float r1 = mapUintToFloat(data[(x_lower + y_lower * stride) * factor]);
985   float r2 = mapUintToFloat(data[(x_lower + y_upper * stride) * factor]);
986   float r3 = mapUintToFloat(data[(x_upper + y_lower * stride) * factor]);
987   float r4 = mapUintToFloat(data[(x_upper + y_upper * stride) * factor]);
988 
989   float g1 = mapUintToFloat(data[(x_lower + y_lower * stride) * factor + 1]);
990   float g2 = mapUintToFloat(data[(x_lower + y_upper * stride) * factor + 1]);
991   float g3 = mapUintToFloat(data[(x_upper + y_lower * stride) * factor + 1]);
992   float g4 = mapUintToFloat(data[(x_upper + y_upper * stride) * factor + 1]);
993 
994   float b1 = mapUintToFloat(data[(x_lower + y_lower * stride) * factor + 2]);
995   float b2 = mapUintToFloat(data[(x_lower + y_upper * stride) * factor + 2]);
996   float b3 = mapUintToFloat(data[(x_upper + y_lower * stride) * factor + 2]);
997   float b4 = mapUintToFloat(data[(x_upper + y_upper * stride) * factor + 2]);
998 
999   Color rgb1 = {{{r1, g1, b1}}};
1000   Color rgb2 = {{{r2, g2, b2}}};
1001   Color rgb3 = {{{r3, g3, b3}}};
1002   Color rgb4 = {{{r4, g4, b4}}};
1003 
1004   // Use Shepard's method for inverse distance weighting. For more information:
1005   // en.wikipedia.org/wiki/Inverse_distance_weighting#Shepard's_method
1006   float e1_dist =
1007       pythDistance(x_map - static_cast<float>(x_lower), y_map - static_cast<float>(y_lower));
1008   if (e1_dist == 0.0f) return rgb1;
1009 
1010   float e2_dist =
1011       pythDistance(x_map - static_cast<float>(x_lower), y_map - static_cast<float>(y_upper));
1012   if (e2_dist == 0.0f) return rgb2;
1013 
1014   float e3_dist =
1015       pythDistance(x_map - static_cast<float>(x_upper), y_map - static_cast<float>(y_lower));
1016   if (e3_dist == 0.0f) return rgb3;
1017 
1018   float e4_dist =
1019       pythDistance(x_map - static_cast<float>(x_upper), y_map - static_cast<float>(y_upper));
1020   if (e4_dist == 0.0f) return rgb4;
1021 
1022   float e1_weight = 1.0f / e1_dist;
1023   float e2_weight = 1.0f / e2_dist;
1024   float e3_weight = 1.0f / e3_dist;
1025   float e4_weight = 1.0f / e4_dist;
1026   float total_weight = e1_weight + e2_weight + e3_weight + e4_weight;
1027 
1028   return rgb1 * (e1_weight / total_weight) + rgb2 * (e2_weight / total_weight) +
1029          rgb3 * (e3_weight / total_weight) + rgb4 * (e4_weight / total_weight);
1030 }
1031 
sampleMap3Channel(uhdr_raw_image_t * map,size_t map_scale_factor,size_t x,size_t y,ShepardsIDW & weightTables,bool has_alpha)1032 Color sampleMap3Channel(uhdr_raw_image_t* map, size_t map_scale_factor, size_t x, size_t y,
1033                         ShepardsIDW& weightTables, bool has_alpha) {
1034   // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the
1035   // following by computing log2(map_scale_factor) once and then using >> log2(map_scale_factor)
1036   size_t x_lower = x / map_scale_factor;
1037   size_t x_upper = x_lower + 1;
1038   size_t y_lower = y / map_scale_factor;
1039   size_t y_upper = y_lower + 1;
1040 
1041   x_lower = std::min(x_lower, (size_t)map->w - 1);
1042   x_upper = std::min(x_upper, (size_t)map->w - 1);
1043   y_lower = std::min(y_lower, (size_t)map->h - 1);
1044   y_upper = std::min(y_upper, (size_t)map->h - 1);
1045 
1046   int factor = has_alpha ? 4 : 3;
1047 
1048   uint8_t* data = reinterpret_cast<uint8_t*>(map->planes[UHDR_PLANE_PACKED]);
1049   size_t stride = map->stride[UHDR_PLANE_PACKED];
1050 
1051   float r1 = mapUintToFloat(data[(x_lower + y_lower * stride) * factor]);
1052   float r2 = mapUintToFloat(data[(x_lower + y_upper * stride) * factor]);
1053   float r3 = mapUintToFloat(data[(x_upper + y_lower * stride) * factor]);
1054   float r4 = mapUintToFloat(data[(x_upper + y_upper * stride) * factor]);
1055 
1056   float g1 = mapUintToFloat(data[(x_lower + y_lower * stride) * factor + 1]);
1057   float g2 = mapUintToFloat(data[(x_lower + y_upper * stride) * factor + 1]);
1058   float g3 = mapUintToFloat(data[(x_upper + y_lower * stride) * factor + 1]);
1059   float g4 = mapUintToFloat(data[(x_upper + y_upper * stride) * factor + 1]);
1060 
1061   float b1 = mapUintToFloat(data[(x_lower + y_lower * stride) * factor + 2]);
1062   float b2 = mapUintToFloat(data[(x_lower + y_upper * stride) * factor + 2]);
1063   float b3 = mapUintToFloat(data[(x_upper + y_lower * stride) * factor + 2]);
1064   float b4 = mapUintToFloat(data[(x_upper + y_upper * stride) * factor + 2]);
1065 
1066   Color rgb1 = {{{r1, g1, b1}}};
1067   Color rgb2 = {{{r2, g2, b2}}};
1068   Color rgb3 = {{{r3, g3, b3}}};
1069   Color rgb4 = {{{r4, g4, b4}}};
1070 
1071   // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the
1072   // following by using & (map_scale_factor - 1)
1073   size_t offset_x = x % map_scale_factor;
1074   size_t offset_y = y % map_scale_factor;
1075 
1076   float* weights = weightTables.mWeights;
1077   if (x_lower == x_upper && y_lower == y_upper)
1078     weights = weightTables.mWeightsC;
1079   else if (x_lower == x_upper)
1080     weights = weightTables.mWeightsNR;
1081   else if (y_lower == y_upper)
1082     weights = weightTables.mWeightsNB;
1083   weights += offset_y * map_scale_factor * 4 + offset_x * 4;
1084 
1085   return rgb1 * weights[0] + rgb2 * weights[1] + rgb3 * weights[2] + rgb4 * weights[3];
1086 }
1087 
1088 ////////////////////////////////////////////////////////////////////////////////
1089 // function selectors
1090 
1091 // TODO: confirm we always want to convert like this before calculating
1092 // luminance.
getGamutConversionFn(uhdr_color_gamut_t dst_gamut,uhdr_color_gamut_t src_gamut)1093 ColorTransformFn getGamutConversionFn(uhdr_color_gamut_t dst_gamut, uhdr_color_gamut_t src_gamut) {
1094   switch (dst_gamut) {
1095     case UHDR_CG_BT_709:
1096       switch (src_gamut) {
1097         case UHDR_CG_BT_709:
1098           return identityConversion;
1099         case UHDR_CG_DISPLAY_P3:
1100           return p3ToBt709;
1101         case UHDR_CG_BT_2100:
1102           return bt2100ToBt709;
1103         case UHDR_CG_UNSPECIFIED:
1104           return nullptr;
1105       }
1106       break;
1107     case UHDR_CG_DISPLAY_P3:
1108       switch (src_gamut) {
1109         case UHDR_CG_BT_709:
1110           return bt709ToP3;
1111         case UHDR_CG_DISPLAY_P3:
1112           return identityConversion;
1113         case UHDR_CG_BT_2100:
1114           return bt2100ToP3;
1115         case UHDR_CG_UNSPECIFIED:
1116           return nullptr;
1117       }
1118       break;
1119     case UHDR_CG_BT_2100:
1120       switch (src_gamut) {
1121         case UHDR_CG_BT_709:
1122           return bt709ToBt2100;
1123         case UHDR_CG_DISPLAY_P3:
1124           return p3ToBt2100;
1125         case UHDR_CG_BT_2100:
1126           return identityConversion;
1127         case UHDR_CG_UNSPECIFIED:
1128           return nullptr;
1129       }
1130       break;
1131     case UHDR_CG_UNSPECIFIED:
1132       return nullptr;
1133   }
1134   return nullptr;
1135 }
1136 
getYuvToRgbFn(uhdr_color_gamut_t gamut)1137 ColorTransformFn getYuvToRgbFn(uhdr_color_gamut_t gamut) {
1138   switch (gamut) {
1139     case UHDR_CG_BT_709:
1140       return srgbYuvToRgb;
1141     case UHDR_CG_DISPLAY_P3:
1142       return p3YuvToRgb;
1143     case UHDR_CG_BT_2100:
1144       return bt2100YuvToRgb;
1145     case UHDR_CG_UNSPECIFIED:
1146       return nullptr;
1147   }
1148   return nullptr;
1149 }
1150 
getLuminanceFn(uhdr_color_gamut_t gamut)1151 LuminanceFn getLuminanceFn(uhdr_color_gamut_t gamut) {
1152   switch (gamut) {
1153     case UHDR_CG_BT_709:
1154       return srgbLuminance;
1155     case UHDR_CG_DISPLAY_P3:
1156       return p3Luminance;
1157     case UHDR_CG_BT_2100:
1158       return bt2100Luminance;
1159     case UHDR_CG_UNSPECIFIED:
1160       return nullptr;
1161   }
1162   return nullptr;
1163 }
1164 
getInverseOetfFn(uhdr_color_transfer_t transfer)1165 ColorTransformFn getInverseOetfFn(uhdr_color_transfer_t transfer) {
1166   switch (transfer) {
1167     case UHDR_CT_LINEAR:
1168       return identityConversion;
1169     case UHDR_CT_HLG:
1170 #if USE_HLG_INVOETF_LUT
1171       return hlgInvOetfLUT;
1172 #else
1173       return hlgInvOetf;
1174 #endif
1175     case UHDR_CT_PQ:
1176 #if USE_PQ_INVOETF_LUT
1177       return pqInvOetfLUT;
1178 #else
1179       return pqInvOetf;
1180 #endif
1181     case UHDR_CT_SRGB:
1182 #if USE_SRGB_INVOETF_LUT
1183       return srgbInvOetfLUT;
1184 #else
1185       return srgbInvOetf;
1186 #endif
1187     case UHDR_CT_UNSPECIFIED:
1188       return nullptr;
1189   }
1190   return nullptr;
1191 }
1192 
getOotfFn(uhdr_color_transfer_t transfer)1193 SceneToDisplayLuminanceFn getOotfFn(uhdr_color_transfer_t transfer) {
1194   switch (transfer) {
1195     case UHDR_CT_LINEAR:
1196       return identityOotf;
1197     case UHDR_CT_HLG:
1198       return hlgOotfApprox;
1199     case UHDR_CT_PQ:
1200       return identityOotf;
1201     case UHDR_CT_SRGB:
1202       return identityOotf;
1203     case UHDR_CT_UNSPECIFIED:
1204       return nullptr;
1205   }
1206   return nullptr;
1207 }
1208 
getPixelFn(uhdr_img_fmt_t format)1209 GetPixelFn getPixelFn(uhdr_img_fmt_t format) {
1210   switch (format) {
1211     case UHDR_IMG_FMT_24bppYCbCr444:
1212       return getYuv444Pixel;
1213     case UHDR_IMG_FMT_16bppYCbCr422:
1214       return getYuv422Pixel;
1215     case UHDR_IMG_FMT_12bppYCbCr420:
1216       return getYuv420Pixel;
1217     case UHDR_IMG_FMT_24bppYCbCrP010:
1218       return getP010Pixel;
1219     case UHDR_IMG_FMT_30bppYCbCr444:
1220       return getYuv444Pixel10bit;
1221     case UHDR_IMG_FMT_32bppRGBA8888:
1222       return getRgba8888Pixel;
1223     case UHDR_IMG_FMT_32bppRGBA1010102:
1224       return getRgba1010102Pixel;
1225     case UHDR_IMG_FMT_64bppRGBAHalfFloat:
1226       return getRgbaF16Pixel;
1227     case UHDR_IMG_FMT_8bppYCbCr400:
1228       return getYuv400Pixel;
1229     case UHDR_IMG_FMT_24bppRGB888:
1230       return getRgb888Pixel;
1231     default:
1232       return nullptr;
1233   }
1234   return nullptr;
1235 }
1236 
putPixelFn(uhdr_img_fmt_t format)1237 PutPixelFn putPixelFn(uhdr_img_fmt_t format) {
1238   switch (format) {
1239     case UHDR_IMG_FMT_24bppYCbCr444:
1240       return putYuv444Pixel;
1241     case UHDR_IMG_FMT_32bppRGBA8888:
1242       return putRgba8888Pixel;
1243     case UHDR_IMG_FMT_8bppYCbCr400:
1244       return putYuv400Pixel;
1245     case UHDR_IMG_FMT_24bppRGB888:
1246       return putRgb888Pixel;
1247     default:
1248       return nullptr;
1249   }
1250   return nullptr;
1251 }
1252 
getSamplePixelFn(uhdr_img_fmt_t format)1253 SamplePixelFn getSamplePixelFn(uhdr_img_fmt_t format) {
1254   switch (format) {
1255     case UHDR_IMG_FMT_24bppYCbCr444:
1256       return sampleYuv444;
1257     case UHDR_IMG_FMT_16bppYCbCr422:
1258       return sampleYuv422;
1259     case UHDR_IMG_FMT_12bppYCbCr420:
1260       return sampleYuv420;
1261     case UHDR_IMG_FMT_24bppYCbCrP010:
1262       return sampleP010;
1263     case UHDR_IMG_FMT_30bppYCbCr444:
1264       return sampleYuv44410bit;
1265     case UHDR_IMG_FMT_32bppRGBA8888:
1266       return sampleRgba8888;
1267     case UHDR_IMG_FMT_32bppRGBA1010102:
1268       return sampleRgba1010102;
1269     case UHDR_IMG_FMT_64bppRGBAHalfFloat:
1270       return sampleRgbaF16;
1271     default:
1272       return nullptr;
1273   }
1274   return nullptr;
1275 }
1276 
1277 ////////////////////////////////////////////////////////////////////////////////
1278 // common utils
1279 
isPixelFormatRgb(uhdr_img_fmt_t format)1280 bool isPixelFormatRgb(uhdr_img_fmt_t format) {
1281   return format == UHDR_IMG_FMT_64bppRGBAHalfFloat || format == UHDR_IMG_FMT_32bppRGBA8888 ||
1282          format == UHDR_IMG_FMT_32bppRGBA1010102;
1283 }
1284 
colorToRgba1010102(Color e_gamma)1285 uint32_t colorToRgba1010102(Color e_gamma) {
1286   uint32_t r = CLIP3((e_gamma.r * 1023 + 0.5f), 0.0f, 1023.0f);
1287   uint32_t g = CLIP3((e_gamma.g * 1023 + 0.5f), 0.0f, 1023.0f);
1288   uint32_t b = CLIP3((e_gamma.b * 1023 + 0.5f), 0.0f, 1023.0f);
1289   return (r | (g << 10) | (b << 20) | (0x3 << 30));  // Set alpha to 1.0
1290 }
1291 
colorToRgbaF16(Color e_gamma)1292 uint64_t colorToRgbaF16(Color e_gamma) {
1293   return (uint64_t)floatToHalf(e_gamma.r) | (((uint64_t)floatToHalf(e_gamma.g)) << 16) |
1294          (((uint64_t)floatToHalf(e_gamma.b)) << 32) | (((uint64_t)floatToHalf(1.0f)) << 48);
1295 }
1296 
convert_raw_input_to_ycbcr(uhdr_raw_image_t * src,bool chroma_sampling_enabled)1297 std::unique_ptr<uhdr_raw_image_ext_t> convert_raw_input_to_ycbcr(uhdr_raw_image_t* src,
1298                                                                  bool chroma_sampling_enabled) {
1299   std::unique_ptr<uhdr_raw_image_ext_t> dst = nullptr;
1300   Color (*rgbToyuv)(Color) = nullptr;
1301 
1302   if (src->fmt == UHDR_IMG_FMT_32bppRGBA1010102 || src->fmt == UHDR_IMG_FMT_32bppRGBA8888) {
1303     if (src->cg == UHDR_CG_BT_709) {
1304       rgbToyuv = srgbRgbToYuv;
1305     } else if (src->cg == UHDR_CG_BT_2100) {
1306       rgbToyuv = bt2100RgbToYuv;
1307     } else if (src->cg == UHDR_CG_DISPLAY_P3) {
1308       rgbToyuv = p3RgbToYuv;
1309     } else {
1310       return dst;
1311     }
1312   }
1313 
1314   if (src->fmt == UHDR_IMG_FMT_32bppRGBA1010102 && chroma_sampling_enabled) {
1315     dst = std::make_unique<uhdr_raw_image_ext_t>(UHDR_IMG_FMT_24bppYCbCrP010, src->cg, src->ct,
1316                                                  UHDR_CR_FULL_RANGE, src->w, src->h, 64);
1317 
1318     uint32_t* rgbData = static_cast<uint32_t*>(src->planes[UHDR_PLANE_PACKED]);
1319     unsigned int srcStride = src->stride[UHDR_PLANE_PACKED];
1320 
1321     uint16_t* yData = static_cast<uint16_t*>(dst->planes[UHDR_PLANE_Y]);
1322     uint16_t* uData = static_cast<uint16_t*>(dst->planes[UHDR_PLANE_UV]);
1323     uint16_t* vData = uData + 1;
1324 
1325     for (size_t i = 0; i < dst->h; i += 2) {
1326       for (size_t j = 0; j < dst->w; j += 2) {
1327         Color pixel[4];
1328 
1329         pixel[0].r = float(rgbData[srcStride * i + j] & 0x3ff);
1330         pixel[0].g = float((rgbData[srcStride * i + j] >> 10) & 0x3ff);
1331         pixel[0].b = float((rgbData[srcStride * i + j] >> 20) & 0x3ff);
1332 
1333         pixel[1].r = float(rgbData[srcStride * i + j + 1] & 0x3ff);
1334         pixel[1].g = float((rgbData[srcStride * i + j + 1] >> 10) & 0x3ff);
1335         pixel[1].b = float((rgbData[srcStride * i + j + 1] >> 20) & 0x3ff);
1336 
1337         pixel[2].r = float(rgbData[srcStride * (i + 1) + j] & 0x3ff);
1338         pixel[2].g = float((rgbData[srcStride * (i + 1) + j] >> 10) & 0x3ff);
1339         pixel[2].b = float((rgbData[srcStride * (i + 1) + j] >> 20) & 0x3ff);
1340 
1341         pixel[3].r = float(rgbData[srcStride * (i + 1) + j + 1] & 0x3ff);
1342         pixel[3].g = float((rgbData[srcStride * (i + 1) + j + 1] >> 10) & 0x3ff);
1343         pixel[3].b = float((rgbData[srcStride * (i + 1) + j + 1] >> 20) & 0x3ff);
1344 
1345         for (int k = 0; k < 4; k++) {
1346           // Now we only support the RGB input being full range
1347           pixel[k] /= 1023.0f;
1348           pixel[k] = (*rgbToyuv)(pixel[k]);
1349 
1350           pixel[k].y = (pixel[k].y * 1023.0f) + 0.5f;
1351           pixel[k].y = CLIP3(pixel[k].y, 0.0f, 1023.0f);
1352         }
1353 
1354         yData[dst->stride[UHDR_PLANE_Y] * i + j] = uint16_t(pixel[0].y) << 6;
1355         yData[dst->stride[UHDR_PLANE_Y] * i + j + 1] = uint16_t(pixel[1].y) << 6;
1356         yData[dst->stride[UHDR_PLANE_Y] * (i + 1) + j] = uint16_t(pixel[2].y) << 6;
1357         yData[dst->stride[UHDR_PLANE_Y] * (i + 1) + j + 1] = uint16_t(pixel[3].y) << 6;
1358 
1359         pixel[0].u = (pixel[0].u + pixel[1].u + pixel[2].u + pixel[3].u) / 4;
1360         pixel[0].v = (pixel[0].v + pixel[1].v + pixel[2].v + pixel[3].v) / 4;
1361 
1362         pixel[0].u = (pixel[0].u * 1023.0f) + 512.0f + 0.5f;
1363         pixel[0].v = (pixel[0].v * 1023.0f) + 512.0f + 0.5f;
1364 
1365         pixel[0].u = CLIP3(pixel[0].u, 0.0f, 1023.0f);
1366         pixel[0].v = CLIP3(pixel[0].v, 0.0f, 1023.0f);
1367 
1368         uData[dst->stride[UHDR_PLANE_UV] * (i / 2) + j] = uint16_t(pixel[0].u) << 6;
1369         vData[dst->stride[UHDR_PLANE_UV] * (i / 2) + j] = uint16_t(pixel[0].v) << 6;
1370       }
1371     }
1372   } else if (src->fmt == UHDR_IMG_FMT_32bppRGBA1010102) {
1373     dst = std::make_unique<uhdr_raw_image_ext_t>(UHDR_IMG_FMT_30bppYCbCr444, src->cg, src->ct,
1374                                                  UHDR_CR_FULL_RANGE, src->w, src->h, 64);
1375 
1376     uint32_t* rgbData = static_cast<uint32_t*>(src->planes[UHDR_PLANE_PACKED]);
1377     unsigned int srcStride = src->stride[UHDR_PLANE_PACKED];
1378 
1379     uint16_t* yData = static_cast<uint16_t*>(dst->planes[UHDR_PLANE_Y]);
1380     uint16_t* uData = static_cast<uint16_t*>(dst->planes[UHDR_PLANE_U]);
1381     uint16_t* vData = static_cast<uint16_t*>(dst->planes[UHDR_PLANE_V]);
1382 
1383     for (size_t i = 0; i < dst->h; i++) {
1384       for (size_t j = 0; j < dst->w; j++) {
1385         Color pixel;
1386 
1387         pixel.r = float(rgbData[srcStride * i + j] & 0x3ff);
1388         pixel.g = float((rgbData[srcStride * i + j] >> 10) & 0x3ff);
1389         pixel.b = float((rgbData[srcStride * i + j] >> 20) & 0x3ff);
1390 
1391         // Now we only support the RGB input being full range
1392         pixel /= 1023.0f;
1393         pixel = (*rgbToyuv)(pixel);
1394 
1395         pixel.y = (pixel.y * 1023.0f) + 0.5f;
1396         pixel.y = CLIP3(pixel.y, 0.0f, 1023.0f);
1397 
1398         yData[dst->stride[UHDR_PLANE_Y] * i + j] = uint16_t(pixel.y);
1399 
1400         pixel.u = (pixel.u * 1023.0f) + 512.0f + 0.5f;
1401         pixel.v = (pixel.v * 1023.0f) + 512.0f + 0.5f;
1402 
1403         pixel.u = CLIP3(pixel.u, 0.0f, 1023.0f);
1404         pixel.v = CLIP3(pixel.v, 0.0f, 1023.0f);
1405 
1406         uData[dst->stride[UHDR_PLANE_U] * i + j] = uint16_t(pixel.u);
1407         vData[dst->stride[UHDR_PLANE_V] * i + j] = uint16_t(pixel.v);
1408       }
1409     }
1410   } else if (src->fmt == UHDR_IMG_FMT_32bppRGBA8888 && chroma_sampling_enabled) {
1411     dst = std::make_unique<uhdr_raw_image_ext_t>(UHDR_IMG_FMT_12bppYCbCr420, src->cg, src->ct,
1412                                                  UHDR_CR_FULL_RANGE, src->w, src->h, 64);
1413     uint32_t* rgbData = static_cast<uint32_t*>(src->planes[UHDR_PLANE_PACKED]);
1414     unsigned int srcStride = src->stride[UHDR_PLANE_PACKED];
1415 
1416     uint8_t* yData = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_Y]);
1417     uint8_t* uData = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_U]);
1418     uint8_t* vData = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_V]);
1419     for (size_t i = 0; i < dst->h; i += 2) {
1420       for (size_t j = 0; j < dst->w; j += 2) {
1421         Color pixel[4];
1422 
1423         pixel[0].r = float(rgbData[srcStride * i + j] & 0xff);
1424         pixel[0].g = float((rgbData[srcStride * i + j] >> 8) & 0xff);
1425         pixel[0].b = float((rgbData[srcStride * i + j] >> 16) & 0xff);
1426 
1427         pixel[1].r = float(rgbData[srcStride * i + (j + 1)] & 0xff);
1428         pixel[1].g = float((rgbData[srcStride * i + (j + 1)] >> 8) & 0xff);
1429         pixel[1].b = float((rgbData[srcStride * i + (j + 1)] >> 16) & 0xff);
1430 
1431         pixel[2].r = float(rgbData[srcStride * (i + 1) + j] & 0xff);
1432         pixel[2].g = float((rgbData[srcStride * (i + 1) + j] >> 8) & 0xff);
1433         pixel[2].b = float((rgbData[srcStride * (i + 1) + j] >> 16) & 0xff);
1434 
1435         pixel[3].r = float(rgbData[srcStride * (i + 1) + (j + 1)] & 0xff);
1436         pixel[3].g = float((rgbData[srcStride * (i + 1) + (j + 1)] >> 8) & 0xff);
1437         pixel[3].b = float((rgbData[srcStride * (i + 1) + (j + 1)] >> 16) & 0xff);
1438 
1439         for (int k = 0; k < 4; k++) {
1440           // Now we only support the RGB input being full range
1441           pixel[k] /= 255.0f;
1442           pixel[k] = (*rgbToyuv)(pixel[k]);
1443 
1444           pixel[k].y = pixel[k].y * 255.0f + 0.5f;
1445           pixel[k].y = CLIP3(pixel[k].y, 0.0f, 255.0f);
1446         }
1447         yData[dst->stride[UHDR_PLANE_Y] * i + j] = uint8_t(pixel[0].y);
1448         yData[dst->stride[UHDR_PLANE_Y] * i + j + 1] = uint8_t(pixel[1].y);
1449         yData[dst->stride[UHDR_PLANE_Y] * (i + 1) + j] = uint8_t(pixel[2].y);
1450         yData[dst->stride[UHDR_PLANE_Y] * (i + 1) + j + 1] = uint8_t(pixel[3].y);
1451 
1452         pixel[0].u = (pixel[0].u + pixel[1].u + pixel[2].u + pixel[3].u) / 4;
1453         pixel[0].v = (pixel[0].v + pixel[1].v + pixel[2].v + pixel[3].v) / 4;
1454 
1455         pixel[0].u = pixel[0].u * 255.0f + 0.5f + 128.0f;
1456         pixel[0].v = pixel[0].v * 255.0f + 0.5f + 128.0f;
1457 
1458         pixel[0].u = CLIP3(pixel[0].u, 0.0f, 255.0f);
1459         pixel[0].v = CLIP3(pixel[0].v, 0.0f, 255.0f);
1460 
1461         uData[dst->stride[UHDR_PLANE_U] * (i / 2) + (j / 2)] = uint8_t(pixel[0].u);
1462         vData[dst->stride[UHDR_PLANE_V] * (i / 2) + (j / 2)] = uint8_t(pixel[0].v);
1463       }
1464     }
1465   } else if (src->fmt == UHDR_IMG_FMT_32bppRGBA8888) {
1466     dst = std::make_unique<uhdr_raw_image_ext_t>(UHDR_IMG_FMT_24bppYCbCr444, src->cg, src->ct,
1467                                                  UHDR_CR_FULL_RANGE, src->w, src->h, 64);
1468     uint32_t* rgbData = static_cast<uint32_t*>(src->planes[UHDR_PLANE_PACKED]);
1469     unsigned int srcStride = src->stride[UHDR_PLANE_PACKED];
1470 
1471     uint8_t* yData = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_Y]);
1472     uint8_t* uData = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_U]);
1473     uint8_t* vData = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_V]);
1474     for (size_t i = 0; i < dst->h; i++) {
1475       for (size_t j = 0; j < dst->w; j++) {
1476         Color pixel;
1477 
1478         pixel.r = float(rgbData[srcStride * i + j] & 0xff);
1479         pixel.g = float((rgbData[srcStride * i + j] >> 8) & 0xff);
1480         pixel.b = float((rgbData[srcStride * i + j] >> 16) & 0xff);
1481 
1482         // Now we only support the RGB input being full range
1483         pixel /= 255.0f;
1484         pixel = (*rgbToyuv)(pixel);
1485 
1486         pixel.y = pixel.y * 255.0f + 0.5f;
1487         pixel.y = CLIP3(pixel.y, 0.0f, 255.0f);
1488         yData[dst->stride[UHDR_PLANE_Y] * i + j] = uint8_t(pixel.y);
1489 
1490         pixel.u = pixel.u * 255.0f + 0.5f + 128.0f;
1491         pixel.v = pixel.v * 255.0f + 0.5f + 128.0f;
1492 
1493         pixel.u = CLIP3(pixel.u, 0.0f, 255.0f);
1494         pixel.v = CLIP3(pixel.v, 0.0f, 255.0f);
1495 
1496         uData[dst->stride[UHDR_PLANE_U] * i + j] = uint8_t(pixel.u);
1497         vData[dst->stride[UHDR_PLANE_V] * i + j] = uint8_t(pixel.v);
1498       }
1499     }
1500   } else if (src->fmt == UHDR_IMG_FMT_12bppYCbCr420 || src->fmt == UHDR_IMG_FMT_24bppYCbCrP010) {
1501     dst = std::make_unique<ultrahdr::uhdr_raw_image_ext_t>(src->fmt, src->cg, src->ct, src->range,
1502                                                            src->w, src->h, 64);
1503     auto status = copy_raw_image(src, dst.get());
1504     if (status.error_code != UHDR_CODEC_OK) return nullptr;
1505   }
1506   return dst;
1507 }
1508 
copy_raw_image(uhdr_raw_image_t * src)1509 std::unique_ptr<uhdr_raw_image_ext_t> copy_raw_image(uhdr_raw_image_t* src) {
1510   std::unique_ptr<uhdr_raw_image_ext_t> dst = std::make_unique<ultrahdr::uhdr_raw_image_ext_t>(
1511       src->fmt, src->cg, src->ct, src->range, src->w, src->h, 64);
1512   auto status = copy_raw_image(src, dst.get());
1513   if (status.error_code != UHDR_CODEC_OK) return nullptr;
1514   return dst;
1515 }
1516 
copy_raw_image(uhdr_raw_image_t * src,uhdr_raw_image_t * dst)1517 uhdr_error_info_t copy_raw_image(uhdr_raw_image_t* src, uhdr_raw_image_t* dst) {
1518   if (dst->w != src->w || dst->h != src->h) {
1519     uhdr_error_info_t status;
1520     status.error_code = UHDR_CODEC_MEM_ERROR;
1521     status.has_detail = 1;
1522     snprintf(status.detail, sizeof status.detail,
1523              "destination image dimensions %dx%d and source image dimensions %dx%d are not "
1524              "identical for copy_raw_image",
1525              dst->w, dst->h, src->w, src->h);
1526     return status;
1527   }
1528 
1529   dst->cg = src->cg;
1530   dst->ct = src->ct;
1531   dst->range = src->range;
1532   if (dst->fmt == src->fmt) {
1533     if (src->fmt == UHDR_IMG_FMT_24bppYCbCrP010) {
1534       size_t bpp = 2;
1535       uint8_t* y_dst = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_Y]);
1536       uint8_t* y_src = static_cast<uint8_t*>(src->planes[UHDR_PLANE_Y]);
1537       uint8_t* uv_dst = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_UV]);
1538       uint8_t* uv_src = static_cast<uint8_t*>(src->planes[UHDR_PLANE_UV]);
1539 
1540       // copy y
1541       for (size_t i = 0; i < src->h; i++) {
1542         memcpy(y_dst, y_src, src->w * bpp);
1543         y_dst += (dst->stride[UHDR_PLANE_Y] * bpp);
1544         y_src += (src->stride[UHDR_PLANE_Y] * bpp);
1545       }
1546       // copy cbcr
1547       for (size_t i = 0; i < src->h / 2; i++) {
1548         memcpy(uv_dst, uv_src, src->w * bpp);
1549         uv_dst += (dst->stride[UHDR_PLANE_UV] * bpp);
1550         uv_src += (src->stride[UHDR_PLANE_UV] * bpp);
1551       }
1552       return g_no_error;
1553     } else if (src->fmt == UHDR_IMG_FMT_12bppYCbCr420) {
1554       uint8_t* y_dst = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_Y]);
1555       uint8_t* y_src = static_cast<uint8_t*>(src->planes[UHDR_PLANE_Y]);
1556       uint8_t* u_dst = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_U]);
1557       uint8_t* u_src = static_cast<uint8_t*>(src->planes[UHDR_PLANE_U]);
1558       uint8_t* v_dst = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_V]);
1559       uint8_t* v_src = static_cast<uint8_t*>(src->planes[UHDR_PLANE_V]);
1560 
1561       // copy y
1562       for (size_t i = 0; i < src->h; i++) {
1563         memcpy(y_dst, y_src, src->w);
1564         y_dst += dst->stride[UHDR_PLANE_Y];
1565         y_src += src->stride[UHDR_PLANE_Y];
1566       }
1567       // copy cb & cr
1568       for (size_t i = 0; i < src->h / 2; i++) {
1569         memcpy(u_dst, u_src, src->w / 2);
1570         memcpy(v_dst, v_src, src->w / 2);
1571         u_dst += dst->stride[UHDR_PLANE_U];
1572         v_dst += dst->stride[UHDR_PLANE_V];
1573         u_src += src->stride[UHDR_PLANE_U];
1574         v_src += src->stride[UHDR_PLANE_V];
1575       }
1576       return g_no_error;
1577     } else if (src->fmt == UHDR_IMG_FMT_8bppYCbCr400 || src->fmt == UHDR_IMG_FMT_32bppRGBA8888 ||
1578                src->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat ||
1579                src->fmt == UHDR_IMG_FMT_32bppRGBA1010102 || src->fmt == UHDR_IMG_FMT_24bppRGB888) {
1580       uint8_t* plane_dst = static_cast<uint8_t*>(dst->planes[UHDR_PLANE_PACKED]);
1581       uint8_t* plane_src = static_cast<uint8_t*>(src->planes[UHDR_PLANE_PACKED]);
1582       size_t bpp = 1;
1583 
1584       if (src->fmt == UHDR_IMG_FMT_32bppRGBA1010102 || src->fmt == UHDR_IMG_FMT_32bppRGBA8888)
1585         bpp = 4;
1586       else if (src->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat)
1587         bpp = 8;
1588       else if (src->fmt == UHDR_IMG_FMT_24bppRGB888)
1589         bpp = 3;
1590       for (size_t i = 0; i < src->h; i++) {
1591         memcpy(plane_dst, plane_src, src->w * bpp);
1592         plane_dst += (bpp * dst->stride[UHDR_PLANE_PACKED]);
1593         plane_src += (bpp * src->stride[UHDR_PLANE_PACKED]);
1594       }
1595       return g_no_error;
1596     }
1597   } else {
1598     if (src->fmt == UHDR_IMG_FMT_24bppRGB888 && dst->fmt == UHDR_IMG_FMT_32bppRGBA8888) {
1599       uint32_t* plane_dst = static_cast<uint32_t*>(dst->planes[UHDR_PLANE_PACKED]);
1600       uint8_t* plane_src = static_cast<uint8_t*>(src->planes[UHDR_PLANE_PACKED]);
1601       for (size_t i = 0; i < src->h; i++) {
1602         uint32_t* pixel_dst = plane_dst;
1603         uint8_t* pixel_src = plane_src;
1604         for (size_t j = 0; j < src->w; j++) {
1605           *pixel_dst = pixel_src[0] | (pixel_src[1] << 8) | (pixel_src[2] << 16) | (0xff << 24);
1606           pixel_src += 3;
1607           pixel_dst += 1;
1608         }
1609         plane_dst += dst->stride[UHDR_PLANE_PACKED];
1610         plane_src += (size_t)3 * src->stride[UHDR_PLANE_PACKED];
1611       }
1612       return g_no_error;
1613     }
1614   }
1615   uhdr_error_info_t status;
1616   status.error_code = UHDR_CODEC_UNSUPPORTED_FEATURE;
1617   status.has_detail = 1;
1618   snprintf(
1619       status.detail, sizeof status.detail,
1620       "unsupported source / destinations color formats in copy_raw_image, src fmt %d, dst fmt %d",
1621       src->fmt, dst->fmt);
1622   return status;
1623 }
1624 
1625 // Use double type for intermediate results for better precision.
floatToUnsignedFractionImpl(float v,uint32_t maxNumerator,uint32_t * numerator,uint32_t * denominator)1626 static bool floatToUnsignedFractionImpl(float v, uint32_t maxNumerator, uint32_t* numerator,
1627                                         uint32_t* denominator) {
1628   if (std::isnan(v) || v < 0 || v > maxNumerator) {
1629     return false;
1630   }
1631 
1632   // Maximum denominator: makes sure that the numerator is <= maxNumerator and the denominator
1633   // is <= UINT32_MAX.
1634   const uint64_t maxD = (v <= 1) ? UINT32_MAX : (uint64_t)floor(maxNumerator / v);
1635 
1636   // Find the best approximation of v as a fraction using continued fractions, see
1637   // https://en.wikipedia.org/wiki/Continued_fraction
1638   *denominator = 1;
1639   uint32_t previousD = 0;
1640   double currentV = (double)v - floor(v);
1641   int iter = 0;
1642   // Set a maximum number of iterations to be safe. Most numbers should
1643   // converge in less than ~20 iterations.
1644   // The golden ratio is the worst case and takes 39 iterations.
1645   const int maxIter = 39;
1646   while (iter < maxIter) {
1647     const double numeratorDouble = (double)(*denominator) * v;
1648     if (numeratorDouble > maxNumerator) {
1649       return false;
1650     }
1651     *numerator = (uint32_t)round(numeratorDouble);
1652     if (fabs(numeratorDouble - (*numerator)) == 0.0) {
1653       return true;
1654     }
1655     currentV = 1.0 / currentV;
1656     const double newD = previousD + floor(currentV) * (*denominator);
1657     if (newD > maxD) {
1658       // This is the best we can do with a denominator <= max_d.
1659       return true;
1660     }
1661     previousD = *denominator;
1662     if (newD > (double)UINT32_MAX) {
1663       return false;
1664     }
1665     *denominator = (uint32_t)newD;
1666     currentV -= floor(currentV);
1667     ++iter;
1668   }
1669   // Maximum number of iterations reached, return what we've found.
1670   // For max_iter >= 39 we shouldn't get here. max_iter can be set
1671   // to a lower value to speed up the algorithm if needed.
1672   *numerator = (uint32_t)round((double)(*denominator) * v);
1673   return true;
1674 }
1675 
floatToSignedFraction(float v,int32_t * numerator,uint32_t * denominator)1676 bool floatToSignedFraction(float v, int32_t* numerator, uint32_t* denominator) {
1677   uint32_t positive_numerator;
1678   if (!floatToUnsignedFractionImpl(fabs(v), INT32_MAX, &positive_numerator, denominator)) {
1679     return false;
1680   }
1681   *numerator = (int32_t)positive_numerator;
1682   if (v < 0) {
1683     *numerator *= -1;
1684   }
1685   return true;
1686 }
1687 
floatToUnsignedFraction(float v,uint32_t * numerator,uint32_t * denominator)1688 bool floatToUnsignedFraction(float v, uint32_t* numerator, uint32_t* denominator) {
1689   return floatToUnsignedFractionImpl(v, UINT32_MAX, numerator, denominator);
1690 }
1691 
1692 }  // namespace ultrahdr
1693