/src/libheif/libheif/nclx.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * HEIF codec. |
3 | | * Copyright (c) 2020 Dirk Farin <dirk.farin@gmail.com> |
4 | | * |
5 | | * This file is part of libheif. |
6 | | * |
7 | | * libheif is free software: you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as |
9 | | * published by the Free Software Foundation, either version 3 of |
10 | | * the License, or (at your option) any later version. |
11 | | * |
12 | | * libheif is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with libheif. If not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | |
22 | | #include "nclx.h" |
23 | | #include "libheif/heif_experimental.h" |
24 | | |
25 | | #include <cassert> |
26 | | #include <memory> |
27 | | #include <string> |
28 | | #include <vector> |
29 | | |
30 | | |
31 | | primaries::primaries(float gx, float gy, float bx, float by, float rx, float ry, float wx, float wy) |
32 | 6 | { |
33 | 6 | defined = true; |
34 | 6 | redX = rx; |
35 | 6 | redY = ry; |
36 | 6 | greenX = gx; |
37 | 6 | greenY = gy; |
38 | 6 | blueX = bx; |
39 | 6 | blueY = by; |
40 | 6 | whiteX = wx; |
41 | 6 | whiteY = wy; |
42 | 6 | } |
43 | | |
44 | | |
45 | | primaries get_colour_primaries(uint16_t primaries_idx) |
46 | 38 | { |
47 | 38 | switch (primaries_idx) { |
48 | 1 | case 1: |
49 | 1 | return {0.300f, 0.600f, 0.150f, 0.060f, 0.640f, 0.330f, 0.3127f, 0.3290f}; |
50 | 1 | case 4: |
51 | 1 | return {0.21f, 0.71f, 0.14f, 0.08f, 0.67f, 0.33f, 0.310f, 0.316f}; |
52 | 0 | case 5: |
53 | 0 | return {0.29f, 0.60f, 0.15f, 0.06f, 0.64f, 0.33f, 0.3127f, 0.3290f}; |
54 | 0 | case 6: |
55 | 1 | case 7: |
56 | 1 | return {0.310f, 0.595f, 0.155f, 0.070f, 0.630f, 0.340f, 0.3127f, 0.3290f}; |
57 | 0 | case 8: |
58 | 0 | return {0.243f, 0.692f, 0.145f, 0.049f, 0.681f, 0.319f, 0.310f, 0.316f}; |
59 | 0 | case 9: |
60 | 0 | return {0.170f, 0.797f, 0.131f, 0.046f, 0.708f, 0.292f, 0.3127f, 0.3290f}; |
61 | 0 | case 10: |
62 | 0 | return {0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.333333f, 0.33333f}; |
63 | 3 | case 11: |
64 | 3 | return {0.265f, 0.690f, 0.150f, 0.060f, 0.680f, 0.320f, 0.314f, 0.351f}; |
65 | 0 | case 12: |
66 | 0 | return {0.265f, 0.690f, 0.150f, 0.060f, 0.680f, 0.320f, 0.3127f, 0.3290f}; |
67 | 0 | case 22: |
68 | 0 | return {0.295f, 0.605f, 0.155f, 0.077f, 0.630f, 0.340f, 0.3127f, 0.3290f}; |
69 | 32 | default: |
70 | 32 | return {}; |
71 | 38 | } |
72 | 38 | } |
73 | | |
74 | | Kr_Kb Kr_Kb::defaults() |
75 | 0 | { |
76 | 0 | Kr_Kb kr_kb; |
77 | | // Rec 601. |
78 | 0 | kr_kb.Kr = 0.2990f; |
79 | 0 | kr_kb.Kb = 0.1140f; |
80 | 0 | return kr_kb; |
81 | 0 | } |
82 | | |
83 | | |
84 | | Kr_Kb get_Kr_Kb(uint16_t matrix_coefficients_idx, uint16_t primaries_idx) |
85 | 2.53k | { |
86 | 2.53k | Kr_Kb result; |
87 | | |
88 | 2.53k | if (matrix_coefficients_idx == 12 || |
89 | 2.53k | matrix_coefficients_idx == 13) { |
90 | | |
91 | 38 | primaries p = get_colour_primaries(primaries_idx); |
92 | 38 | float zr = 1 - (p.redX + p.redY); |
93 | 38 | float zg = 1 - (p.greenX + p.greenY); |
94 | 38 | float zb = 1 - (p.blueX + p.blueY); |
95 | 38 | float zw = 1 - (p.whiteX + p.whiteY); |
96 | | |
97 | 38 | float denom = p.whiteY * (p.redX * (p.greenY * zb - p.blueY * zg) + p.greenX * (p.blueY * zr - p.redY * zb) + |
98 | 38 | p.blueX * (p.redY * zg - p.greenY * zr)); |
99 | | |
100 | 38 | if (denom == 0.0f) { |
101 | 32 | return result; |
102 | 32 | } |
103 | | |
104 | 6 | result.Kr = (p.redY * (p.whiteX * (p.greenY * zb - p.blueY * zg) + p.whiteY * (p.blueX * zg - p.greenX * zb) + |
105 | 6 | zw * (p.greenX * p.blueY - p.blueX * p.greenY))) / denom; |
106 | 6 | result.Kb = (p.blueY * (p.whiteX * (p.redY * zg - p.greenY * zr) + p.whiteY * (p.greenX * zr - p.redX * zg) + |
107 | 6 | zw * (p.redX * p.greenY - p.greenX * p.redY))) / denom; |
108 | 6 | } |
109 | 2.49k | else |
110 | 2.49k | switch (matrix_coefficients_idx) { |
111 | 19 | case 1: |
112 | 19 | result.Kr = 0.2126f; |
113 | 19 | result.Kb = 0.0722f; |
114 | 19 | break; |
115 | 7 | case 4: |
116 | 7 | result.Kr = 0.30f; |
117 | 7 | result.Kb = 0.11f; |
118 | 7 | break; |
119 | 23 | case 5: |
120 | 1.24k | case 6: |
121 | 1.24k | result.Kr = 0.299f; |
122 | 1.24k | result.Kb = 0.114f; |
123 | 1.24k | break; |
124 | 1 | case 7: |
125 | 1 | result.Kr = 0.212f; |
126 | 1 | result.Kb = 0.087f; |
127 | 1 | break; |
128 | 2 | case 9: |
129 | 7 | case 10: |
130 | 7 | result.Kr = 0.2627f; |
131 | 7 | result.Kb = 0.0593f; |
132 | 7 | break; |
133 | 1.21k | default:; |
134 | 2.49k | } |
135 | | |
136 | 2.49k | return result; |
137 | 2.53k | } |
138 | | |
139 | | |
140 | | YCbCr_to_RGB_coefficients YCbCr_to_RGB_coefficients::defaults() |
141 | 3.77k | { |
142 | 3.77k | YCbCr_to_RGB_coefficients coeffs; |
143 | 3.77k | coeffs.defined = true; |
144 | 3.77k | coeffs.r_cr = 1.402f; |
145 | 3.77k | coeffs.g_cb = -0.344136f; |
146 | 3.77k | coeffs.g_cr = -0.714136f; |
147 | 3.77k | coeffs.b_cb = 1.772f; |
148 | 3.77k | return coeffs; |
149 | 3.77k | } |
150 | | |
151 | | YCbCr_to_RGB_coefficients |
152 | | get_YCbCr_to_RGB_coefficients(uint16_t matrix_coefficients_idx, uint16_t primaries_idx) |
153 | 2.53k | { |
154 | 2.53k | YCbCr_to_RGB_coefficients coeffs; |
155 | | |
156 | 2.53k | Kr_Kb k = get_Kr_Kb(matrix_coefficients_idx, primaries_idx); |
157 | | |
158 | 2.53k | if (k.Kb != 0 || k.Kr != 0) { // both will be != 0 when valid |
159 | 1.28k | coeffs.defined = true; |
160 | 1.28k | coeffs.r_cr = 2 * (-k.Kr + 1); |
161 | 1.28k | coeffs.g_cb = 2 * k.Kb * (-k.Kb + 1) / (k.Kb + k.Kr - 1); |
162 | 1.28k | coeffs.g_cr = 2 * k.Kr * (-k.Kr + 1) / (k.Kb + k.Kr - 1); |
163 | 1.28k | coeffs.b_cb = 2 * (-k.Kb + 1); |
164 | 1.28k | } |
165 | 1.24k | else { |
166 | 1.24k | coeffs = YCbCr_to_RGB_coefficients::defaults(); |
167 | 1.24k | } |
168 | | |
169 | 2.53k | return coeffs; |
170 | 2.53k | } |
171 | | |
172 | | |
173 | | RGB_to_YCbCr_coefficients |
174 | | get_RGB_to_YCbCr_coefficients(uint16_t matrix_coefficients_idx, uint16_t primaries_idx) |
175 | 0 | { |
176 | 0 | RGB_to_YCbCr_coefficients coeffs; |
177 | |
|
178 | 0 | Kr_Kb k = get_Kr_Kb(matrix_coefficients_idx, primaries_idx); |
179 | |
|
180 | 0 | if (k.Kb != 0 || k.Kr != 0) { // both will be != 0 when valid |
181 | 0 | coeffs.defined = true; |
182 | 0 | coeffs.c[0][0] = k.Kr; |
183 | 0 | coeffs.c[0][1] = 1 - k.Kr - k.Kb; |
184 | 0 | coeffs.c[0][2] = k.Kb; |
185 | 0 | coeffs.c[1][0] = -k.Kr / (1 - k.Kb) / 2; |
186 | 0 | coeffs.c[1][1] = -(1 - k.Kr - k.Kb) / (1 - k.Kb) / 2; |
187 | 0 | coeffs.c[1][2] = 0.5f; |
188 | 0 | coeffs.c[2][0] = 0.5f; |
189 | 0 | coeffs.c[2][1] = -(1 - k.Kr - k.Kb) / (1 - k.Kr) / 2; |
190 | 0 | coeffs.c[2][2] = -k.Kb / (1 - k.Kr) / 2; |
191 | 0 | } |
192 | 0 | else { |
193 | 0 | coeffs = RGB_to_YCbCr_coefficients::defaults(); |
194 | 0 | } |
195 | |
|
196 | 0 | return coeffs; |
197 | 0 | } |
198 | | |
199 | | |
200 | | RGB_to_YCbCr_coefficients RGB_to_YCbCr_coefficients::defaults() |
201 | 0 | { |
202 | 0 | RGB_to_YCbCr_coefficients coeffs; |
203 | 0 | coeffs.defined = true; |
204 | | // Rec 601 full. Kr=0.2990f Kb=0.1140f. |
205 | 0 | coeffs.c[0][0] = 0.299f; |
206 | 0 | coeffs.c[0][1] = 0.587f; |
207 | 0 | coeffs.c[0][2] = 0.114f; |
208 | 0 | coeffs.c[1][0] = -0.168735f; |
209 | 0 | coeffs.c[1][1] = -0.331264f; |
210 | 0 | coeffs.c[1][2] = 0.5f; |
211 | 0 | coeffs.c[2][0] = 0.5f; |
212 | 0 | coeffs.c[2][1] = -0.418688f; |
213 | 0 | coeffs.c[2][2] = -0.081312f; |
214 | |
|
215 | 0 | return coeffs; |
216 | 0 | } |
217 | | |
218 | | |
219 | | Error color_profile_nclx::parse(BitstreamRange& range) |
220 | 34 | { |
221 | 34 | StreamReader::grow_status status; |
222 | 34 | status = range.wait_for_available_bytes(7); |
223 | 34 | if (status != StreamReader::grow_status::size_reached) { |
224 | | // TODO: return recoverable error at timeout |
225 | 0 | return Error(heif_error_Invalid_input, |
226 | 0 | heif_suberror_End_of_data); |
227 | 0 | } |
228 | | |
229 | 34 | m_colour_primaries = range.read16(); |
230 | 34 | m_transfer_characteristics = range.read16(); |
231 | 34 | m_matrix_coefficients = range.read16(); |
232 | 34 | m_full_range_flag = (range.read8() & 0x80 ? true : false); |
233 | | |
234 | 34 | return Error::Ok; |
235 | 34 | } |
236 | | |
237 | | Error color_profile_nclx::get_nclx_color_profile(struct heif_color_profile_nclx** out_data) const |
238 | 4 | { |
239 | 4 | *out_data = nullptr; |
240 | | |
241 | 4 | struct heif_color_profile_nclx* nclx = alloc_nclx_color_profile(); |
242 | | |
243 | 4 | if (nclx == nullptr) { |
244 | 0 | return Error(heif_error_Memory_allocation_error, |
245 | 0 | heif_suberror_Unspecified); |
246 | 0 | } |
247 | | |
248 | 4 | struct heif_error err; |
249 | | |
250 | 4 | nclx->version = 1; |
251 | | |
252 | 4 | err = heif_nclx_color_profile_set_color_primaries(nclx, get_colour_primaries()); |
253 | 4 | if (err.code) { |
254 | 2 | free_nclx_color_profile(nclx); |
255 | 2 | return {err.code, err.subcode}; |
256 | 2 | } |
257 | | |
258 | 2 | err = heif_nclx_color_profile_set_transfer_characteristics(nclx, get_transfer_characteristics()); |
259 | 2 | if (err.code) { |
260 | 2 | free_nclx_color_profile(nclx); |
261 | 2 | return {err.code, err.subcode}; |
262 | 2 | } |
263 | | |
264 | 0 | err = heif_nclx_color_profile_set_matrix_coefficients(nclx, get_matrix_coefficients()); |
265 | 0 | if (err.code) { |
266 | 0 | free_nclx_color_profile(nclx); |
267 | 0 | return {err.code, err.subcode}; |
268 | 0 | } |
269 | | |
270 | 0 | nclx->full_range_flag = get_full_range_flag(); |
271 | | |
272 | | // fill color primaries |
273 | |
|
274 | 0 | auto primaries = ::get_colour_primaries(nclx->color_primaries); |
275 | |
|
276 | 0 | nclx->color_primary_red_x = primaries.redX; |
277 | 0 | nclx->color_primary_red_y = primaries.redY; |
278 | 0 | nclx->color_primary_green_x = primaries.greenX; |
279 | 0 | nclx->color_primary_green_y = primaries.greenY; |
280 | 0 | nclx->color_primary_blue_x = primaries.blueX; |
281 | 0 | nclx->color_primary_blue_y = primaries.blueY; |
282 | 0 | nclx->color_primary_white_x = primaries.whiteX; |
283 | 0 | nclx->color_primary_white_y = primaries.whiteY; |
284 | |
|
285 | 0 | *out_data = nclx; |
286 | |
|
287 | 0 | return Error::Ok; |
288 | 0 | } |
289 | | |
290 | | |
291 | | struct heif_color_profile_nclx* color_profile_nclx::alloc_nclx_color_profile() |
292 | 2.58k | { |
293 | 2.58k | auto profile = (heif_color_profile_nclx*) malloc(sizeof(struct heif_color_profile_nclx)); |
294 | | |
295 | 2.58k | if (profile) { |
296 | 2.58k | profile->version = 1; |
297 | | |
298 | | // sRGB defaults |
299 | 2.58k | profile->color_primaries = heif_color_primaries_ITU_R_BT_709_5; // 1 |
300 | 2.58k | profile->transfer_characteristics = heif_transfer_characteristic_IEC_61966_2_1; // 13 |
301 | 2.58k | profile->matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_601_6; // 6 |
302 | 2.58k | profile->full_range_flag = true; |
303 | 2.58k | } |
304 | | |
305 | 2.58k | return profile; |
306 | 2.58k | } |
307 | | |
308 | | |
309 | | void color_profile_nclx::free_nclx_color_profile(struct heif_color_profile_nclx* profile) |
310 | 2.58k | { |
311 | 2.58k | free(profile); |
312 | 2.58k | } |
313 | | |
314 | | |
315 | | void color_profile_nclx::set_sRGB_defaults() |
316 | 77.8k | { |
317 | | // sRGB defaults |
318 | 77.8k | m_colour_primaries = 1; |
319 | 77.8k | m_transfer_characteristics = 13; |
320 | 77.8k | m_matrix_coefficients = 6; |
321 | 77.8k | m_full_range_flag = true; |
322 | 77.8k | } |
323 | | |
324 | | |
325 | | void color_profile_nclx::set_undefined() |
326 | 0 | { |
327 | 0 | m_colour_primaries = 2; |
328 | 0 | m_transfer_characteristics = 2; |
329 | 0 | m_matrix_coefficients = 2; |
330 | 0 | m_full_range_flag = true; |
331 | 0 | } |
332 | | |
333 | | |
334 | | void color_profile_nclx::set_from_heif_color_profile_nclx(const struct heif_color_profile_nclx* nclx) |
335 | 0 | { |
336 | 0 | if (nclx) { |
337 | 0 | m_colour_primaries = nclx->color_primaries; |
338 | 0 | m_transfer_characteristics = nclx->transfer_characteristics; |
339 | 0 | m_matrix_coefficients = nclx->matrix_coefficients; |
340 | 0 | m_full_range_flag = nclx->full_range_flag; |
341 | 0 | } |
342 | 0 | } |
343 | | |
344 | | |
345 | | void color_profile_nclx::replace_undefined_values_with_sRGB_defaults() |
346 | 2.69k | { |
347 | 2.69k | if (m_matrix_coefficients == heif_matrix_coefficients_unspecified) { |
348 | 1.18k | m_matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_601_6; |
349 | 1.18k | } |
350 | | |
351 | 2.69k | if (m_colour_primaries == heif_color_primaries_unspecified) { |
352 | 1.48k | m_colour_primaries = heif_color_primaries_ITU_R_BT_709_5; |
353 | 1.48k | } |
354 | | |
355 | 2.69k | if (m_transfer_characteristics == heif_transfer_characteristic_unspecified) { |
356 | 1.39k | m_transfer_characteristics = heif_transfer_characteristic_IEC_61966_2_1; |
357 | 1.39k | } |
358 | 2.69k | } |
359 | | |
360 | | |
361 | | Error Box_colr::parse(BitstreamRange& range, const heif_security_limits* limits) |
362 | 1.63k | { |
363 | 1.63k | StreamReader::grow_status status; |
364 | 1.63k | uint32_t colour_type = range.read32(); |
365 | | |
366 | 1.63k | if (colour_type == fourcc("nclx")) { |
367 | 34 | auto color_profile = std::make_shared<color_profile_nclx>(); |
368 | 34 | m_color_profile = color_profile; |
369 | 34 | Error err = color_profile->parse(range); |
370 | 34 | if (err) { |
371 | 0 | return err; |
372 | 0 | } |
373 | 34 | } |
374 | 1.60k | else if (colour_type == fourcc("prof") || |
375 | 1.60k | colour_type == fourcc("rICC")) { |
376 | 1.57k | if (!has_fixed_box_size()) { |
377 | 1 | return Error(heif_error_Unsupported_feature, heif_suberror_Unspecified, "colr boxes with undefined box size are not supported"); |
378 | 1 | } |
379 | | |
380 | 1.57k | uint64_t profile_size_64 = get_box_size() - get_header_size() - 4; |
381 | 1.57k | if (limits->max_color_profile_size && profile_size_64 > limits->max_color_profile_size) { |
382 | 0 | return Error(heif_error_Invalid_input, heif_suberror_Security_limit_exceeded, "Color profile exceeds maximum supported size"); |
383 | 0 | } |
384 | | |
385 | 1.57k | size_t profile_size = static_cast<size_t>(profile_size_64); |
386 | | |
387 | 1.57k | status = range.wait_for_available_bytes(profile_size); |
388 | 1.57k | if (status != StreamReader::grow_status::size_reached) { |
389 | | // TODO: return recoverable error at timeout |
390 | 0 | return Error(heif_error_Invalid_input, |
391 | 0 | heif_suberror_End_of_data); |
392 | 0 | } |
393 | | |
394 | 1.57k | std::vector<uint8_t> rawData(profile_size); |
395 | 1.01M | for (size_t i = 0; i < profile_size; i++) { |
396 | 1.00M | rawData[i] = range.read8(); |
397 | 1.00M | } |
398 | | |
399 | 1.57k | m_color_profile = std::make_shared<color_profile_raw>(colour_type, rawData); |
400 | 1.57k | } |
401 | 27 | else { |
402 | 27 | return Error(heif_error_Invalid_input, |
403 | 27 | heif_suberror_Unknown_color_profile_type); |
404 | 27 | } |
405 | | |
406 | 1.60k | return range.get_error(); |
407 | 1.63k | } |
408 | | |
409 | | |
410 | | std::string Box_colr::dump(Indent& indent) const |
411 | 0 | { |
412 | 0 | std::ostringstream sstr; |
413 | 0 | sstr << Box::dump(indent); |
414 | |
|
415 | 0 | if (m_color_profile) { |
416 | 0 | sstr << indent << "colour_type: " << fourcc_to_string(get_color_profile_type()) << "\n"; |
417 | 0 | sstr << m_color_profile->dump(indent); |
418 | 0 | } |
419 | 0 | else { |
420 | 0 | sstr << indent << "colour_type: ---\n"; |
421 | 0 | sstr << "no color profile\n"; |
422 | 0 | } |
423 | |
|
424 | 0 | return sstr.str(); |
425 | 0 | } |
426 | | |
427 | | |
428 | | std::string color_profile_raw::dump(Indent& indent) const |
429 | 0 | { |
430 | 0 | std::ostringstream sstr; |
431 | 0 | sstr << indent << "profile size: " << m_data.size() << "\n"; |
432 | 0 | return sstr.str(); |
433 | 0 | } |
434 | | |
435 | | |
436 | | std::string color_profile_nclx::dump(Indent& indent) const |
437 | 0 | { |
438 | 0 | std::ostringstream sstr; |
439 | 0 | sstr << indent << "colour_primaries: " << m_colour_primaries << "\n" |
440 | 0 | << indent << "transfer_characteristics: " << m_transfer_characteristics << "\n" |
441 | 0 | << indent << "matrix_coefficients: " << m_matrix_coefficients << "\n" |
442 | 0 | << indent << "full_range_flag: " << m_full_range_flag << "\n"; |
443 | 0 | return sstr.str(); |
444 | 0 | } |
445 | | |
446 | | |
447 | | Error color_profile_nclx::write(StreamWriter& writer) const |
448 | 0 | { |
449 | 0 | writer.write16(m_colour_primaries); |
450 | 0 | writer.write16(m_transfer_characteristics); |
451 | 0 | writer.write16(m_matrix_coefficients); |
452 | 0 | writer.write8(m_full_range_flag ? 0x80 : 0x00); |
453 | |
|
454 | 0 | return Error::Ok; |
455 | 0 | } |
456 | | |
457 | | Error color_profile_raw::write(StreamWriter& writer) const |
458 | 0 | { |
459 | 0 | writer.write(m_data); |
460 | |
|
461 | 0 | return Error::Ok; |
462 | 0 | } |
463 | | |
464 | | Error Box_colr::write(StreamWriter& writer) const |
465 | 0 | { |
466 | 0 | size_t box_start = reserve_box_header_space(writer); |
467 | |
|
468 | 0 | assert(m_color_profile); |
469 | | |
470 | 0 | writer.write32(m_color_profile->get_type()); |
471 | |
|
472 | 0 | Error err = m_color_profile->write(writer); |
473 | 0 | if (err) { |
474 | 0 | return err; |
475 | 0 | } |
476 | | |
477 | 0 | prepend_header(writer, box_start); |
478 | |
|
479 | 0 | return Error::Ok; |
480 | 0 | } |
481 | | |
482 | | |