/src/libheif/libheif/nclx.cc
Line | Count | Source |
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 | 23 | { |
33 | 23 | defined = true; |
34 | 23 | redX = rx; |
35 | 23 | redY = ry; |
36 | 23 | greenX = gx; |
37 | 23 | greenY = gy; |
38 | 23 | blueX = bx; |
39 | 23 | blueY = by; |
40 | 23 | whiteX = wx; |
41 | 23 | whiteY = wy; |
42 | 23 | } |
43 | | |
44 | | |
45 | | primaries get_colour_primaries(uint16_t primaries_idx) |
46 | 31 | { |
47 | 31 | switch (primaries_idx) { |
48 | 3 | case 1: |
49 | 3 | 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 | 1 | case 5: |
53 | 1 | return {0.29f, 0.60f, 0.15f, 0.06f, 0.64f, 0.33f, 0.3127f, 0.3290f}; |
54 | 5 | case 6: |
55 | 6 | case 7: |
56 | 6 | return {0.310f, 0.595f, 0.155f, 0.070f, 0.630f, 0.340f, 0.3127f, 0.3290f}; |
57 | 1 | case 8: |
58 | 1 | return {0.243f, 0.692f, 0.145f, 0.049f, 0.681f, 0.319f, 0.310f, 0.316f}; |
59 | 3 | case 9: |
60 | 3 | return {0.170f, 0.797f, 0.131f, 0.046f, 0.708f, 0.292f, 0.3127f, 0.3290f}; |
61 | 3 | case 10: |
62 | 3 | return {0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.333333f, 0.33333f}; |
63 | 2 | case 11: |
64 | 2 | return {0.265f, 0.690f, 0.150f, 0.060f, 0.680f, 0.320f, 0.314f, 0.351f}; |
65 | 2 | case 12: |
66 | 2 | return {0.265f, 0.690f, 0.150f, 0.060f, 0.680f, 0.320f, 0.3127f, 0.3290f}; |
67 | 1 | case 22: |
68 | 1 | return {0.295f, 0.605f, 0.155f, 0.077f, 0.630f, 0.340f, 0.3127f, 0.3290f}; |
69 | 8 | default: |
70 | 8 | return {}; |
71 | 31 | } |
72 | 31 | } |
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.11k | { |
86 | 2.11k | Kr_Kb result; |
87 | | |
88 | 2.11k | if (matrix_coefficients_idx == 12 || |
89 | 2.09k | matrix_coefficients_idx == 13) { |
90 | | |
91 | 31 | primaries p = get_colour_primaries(primaries_idx); |
92 | 31 | float zr = 1 - (p.redX + p.redY); |
93 | 31 | float zg = 1 - (p.greenX + p.greenY); |
94 | 31 | float zb = 1 - (p.blueX + p.blueY); |
95 | 31 | float zw = 1 - (p.whiteX + p.whiteY); |
96 | | |
97 | 31 | float denom = p.whiteY * (p.redX * (p.greenY * zb - p.blueY * zg) + p.greenX * (p.blueY * zr - p.redY * zb) + |
98 | 31 | p.blueX * (p.redY * zg - p.greenY * zr)); |
99 | | |
100 | 31 | if (denom == 0.0f) { |
101 | 8 | return result; |
102 | 8 | } |
103 | | |
104 | 23 | result.Kr = (p.redY * (p.whiteX * (p.greenY * zb - p.blueY * zg) + p.whiteY * (p.blueX * zg - p.greenX * zb) + |
105 | 23 | zw * (p.greenX * p.blueY - p.blueX * p.greenY))) / denom; |
106 | 23 | result.Kb = (p.blueY * (p.whiteX * (p.redY * zg - p.greenY * zr) + p.whiteY * (p.greenX * zr - p.redX * zg) + |
107 | 23 | zw * (p.redX * p.greenY - p.greenX * p.redY))) / denom; |
108 | 23 | } |
109 | 2.08k | else |
110 | 2.08k | switch (matrix_coefficients_idx) { |
111 | 34 | case 1: |
112 | 34 | result.Kr = 0.2126f; |
113 | 34 | result.Kb = 0.0722f; |
114 | 34 | break; |
115 | 4 | case 4: |
116 | 4 | result.Kr = 0.30f; |
117 | 4 | result.Kb = 0.11f; |
118 | 4 | break; |
119 | 9 | case 5: |
120 | 34 | case 6: |
121 | 34 | result.Kr = 0.299f; |
122 | 34 | result.Kb = 0.114f; |
123 | 34 | break; |
124 | 8 | case 7: |
125 | 8 | result.Kr = 0.212f; |
126 | 8 | result.Kb = 0.087f; |
127 | 8 | break; |
128 | 12 | case 9: |
129 | 15 | case 10: |
130 | 15 | result.Kr = 0.2627f; |
131 | 15 | result.Kb = 0.0593f; |
132 | 15 | break; |
133 | 1.98k | default:; |
134 | 2.08k | } |
135 | | |
136 | 2.10k | return result; |
137 | 2.11k | } |
138 | | |
139 | | |
140 | | YCbCr_to_RGB_coefficients YCbCr_to_RGB_coefficients::defaults() |
141 | 4.15k | { |
142 | 4.15k | YCbCr_to_RGB_coefficients coeffs; |
143 | 4.15k | coeffs.defined = true; |
144 | 4.15k | coeffs.r_cr = 1.402f; |
145 | 4.15k | coeffs.g_cb = -0.344136f; |
146 | 4.15k | coeffs.g_cr = -0.714136f; |
147 | 4.15k | coeffs.b_cb = 1.772f; |
148 | 4.15k | return coeffs; |
149 | 4.15k | } |
150 | | |
151 | | YCbCr_to_RGB_coefficients |
152 | | get_YCbCr_to_RGB_coefficients(uint16_t matrix_coefficients_idx, uint16_t primaries_idx) |
153 | 2.11k | { |
154 | 2.11k | YCbCr_to_RGB_coefficients coeffs; |
155 | | |
156 | 2.11k | Kr_Kb k = get_Kr_Kb(matrix_coefficients_idx, primaries_idx); |
157 | | |
158 | 2.11k | if (k.Kb != 0 || k.Kr != 0) { // both will be != 0 when valid |
159 | 115 | coeffs.defined = true; |
160 | 115 | coeffs.r_cr = 2 * (-k.Kr + 1); |
161 | 115 | coeffs.g_cb = 2 * k.Kb * (-k.Kb + 1) / (k.Kb + k.Kr - 1); |
162 | 115 | coeffs.g_cr = 2 * k.Kr * (-k.Kr + 1) / (k.Kb + k.Kr - 1); |
163 | 115 | coeffs.b_cb = 2 * (-k.Kb + 1); |
164 | 115 | } |
165 | 1.99k | else { |
166 | 1.99k | coeffs = YCbCr_to_RGB_coefficients::defaults(); |
167 | 1.99k | } |
168 | | |
169 | 2.11k | return coeffs; |
170 | 2.11k | } |
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 | 6.91k | { |
221 | 6.91k | StreamReader::grow_status status; |
222 | 6.91k | status = range.wait_for_available_bytes(7); |
223 | 6.91k | if (status != StreamReader::grow_status::size_reached) { |
224 | | // TODO: return recoverable error at timeout |
225 | 2 | return Error(heif_error_Invalid_input, |
226 | 2 | heif_suberror_End_of_data); |
227 | 2 | } |
228 | | |
229 | 6.91k | m_profile.m_colour_primaries = range.read16(); |
230 | 6.91k | m_profile.m_transfer_characteristics = range.read16(); |
231 | 6.91k | m_profile.m_matrix_coefficients = range.read16(); |
232 | 6.91k | m_profile.m_full_range_flag = (range.read8() & 0x80 ? true : false); |
233 | | |
234 | 6.91k | return Error::Ok; |
235 | 6.91k | } |
236 | | |
237 | | Error nclx_profile::get_nclx_color_profile(heif_color_profile_nclx** out_data) const |
238 | 0 | { |
239 | 0 | *out_data = nullptr; |
240 | |
|
241 | 0 | heif_color_profile_nclx* nclx = heif_nclx_color_profile_alloc(); |
242 | |
|
243 | 0 | if (nclx == nullptr) { |
244 | 0 | return Error(heif_error_Memory_allocation_error, |
245 | 0 | heif_suberror_Unspecified); |
246 | 0 | } |
247 | | |
248 | 0 | heif_error err; |
249 | |
|
250 | 0 | nclx->version = 1; |
251 | |
|
252 | 0 | err = heif_nclx_color_profile_set_color_primaries(nclx, get_colour_primaries()); |
253 | 0 | if (err.code) { |
254 | 0 | heif_nclx_color_profile_free(nclx); |
255 | 0 | return {err.code, err.subcode}; |
256 | 0 | } |
257 | | |
258 | 0 | err = heif_nclx_color_profile_set_transfer_characteristics(nclx, get_transfer_characteristics()); |
259 | 0 | if (err.code) { |
260 | 0 | heif_nclx_color_profile_free(nclx); |
261 | 0 | return {err.code, err.subcode}; |
262 | 0 | } |
263 | | |
264 | 0 | err = heif_nclx_color_profile_set_matrix_coefficients(nclx, get_matrix_coefficients()); |
265 | 0 | if (err.code) { |
266 | 0 | heif_nclx_color_profile_free(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 | | void nclx_profile::set_sRGB_defaults() |
292 | 58.1k | { |
293 | | // sRGB defaults |
294 | 58.1k | m_colour_primaries = 1; |
295 | 58.1k | m_transfer_characteristics = 13; |
296 | 58.1k | m_matrix_coefficients = 6; |
297 | 58.1k | m_full_range_flag = true; |
298 | 58.1k | } |
299 | | |
300 | | |
301 | | void nclx_profile::set_undefined() |
302 | 0 | { |
303 | 0 | m_colour_primaries = 2; |
304 | 0 | m_transfer_characteristics = 2; |
305 | 0 | m_matrix_coefficients = 2; |
306 | 0 | m_full_range_flag = true; |
307 | 0 | } |
308 | | |
309 | | |
310 | | bool nclx_profile::is_undefined() const |
311 | 2.58k | { |
312 | 2.58k | return *this == nclx_profile::undefined(); |
313 | 2.58k | } |
314 | | |
315 | | |
316 | | void nclx_profile::set_from_heif_color_profile_nclx(const heif_color_profile_nclx* nclx) |
317 | 0 | { |
318 | 0 | if (nclx) { |
319 | 0 | m_colour_primaries = nclx->color_primaries; |
320 | 0 | m_transfer_characteristics = nclx->transfer_characteristics; |
321 | 0 | m_matrix_coefficients = nclx->matrix_coefficients; |
322 | 0 | m_full_range_flag = nclx->full_range_flag; |
323 | 0 | } |
324 | 0 | } |
325 | | |
326 | | |
327 | | void nclx_profile::replace_undefined_values_with_sRGB_defaults() |
328 | 2.51k | { |
329 | 2.51k | if (m_matrix_coefficients == heif_matrix_coefficients_unspecified) { |
330 | 1.99k | m_matrix_coefficients = heif_matrix_coefficients_ITU_R_BT_601_6; |
331 | 1.99k | } |
332 | | |
333 | 2.51k | if (m_colour_primaries == heif_color_primaries_unspecified) { |
334 | 2.14k | m_colour_primaries = heif_color_primaries_ITU_R_BT_709_5; |
335 | 2.14k | } |
336 | | |
337 | 2.51k | if (m_transfer_characteristics == heif_transfer_characteristic_unspecified) { |
338 | 2.14k | m_transfer_characteristics = heif_transfer_characteristic_IEC_61966_2_1; |
339 | 2.14k | } |
340 | 2.51k | } |
341 | | |
342 | | |
343 | | bool nclx_profile::equal_except_transfer_curve(const nclx_profile& b) const |
344 | 1.56k | { |
345 | 1.56k | return (m_matrix_coefficients == b.m_matrix_coefficients && |
346 | 1.51k | m_colour_primaries == b.m_colour_primaries && |
347 | 1.50k | m_full_range_flag == b.m_full_range_flag); |
348 | 1.56k | } |
349 | | |
350 | | |
351 | | Error Box_colr::parse(BitstreamRange& range, const heif_security_limits* limits) |
352 | 8.21k | { |
353 | 8.21k | StreamReader::grow_status status; |
354 | 8.21k | uint32_t colour_type = range.read32(); |
355 | | |
356 | 8.21k | if (colour_type == fourcc("nclx")) { |
357 | 6.91k | auto color_profile = std::make_shared<color_profile_nclx>(); |
358 | 6.91k | m_color_profile = color_profile; |
359 | 6.91k | Error err = color_profile->parse(range); |
360 | 6.91k | if (err) { |
361 | 2 | return err; |
362 | 2 | } |
363 | 6.91k | } |
364 | 1.30k | else if (colour_type == fourcc("prof") || |
365 | 1.18k | colour_type == fourcc("rICC")) { |
366 | 1.18k | if (!has_fixed_box_size()) { |
367 | 4 | return Error(heif_error_Unsupported_feature, heif_suberror_Unspecified, "colr boxes with undefined box size are not supported"); |
368 | 4 | } |
369 | | |
370 | 1.17k | uint64_t profile_size_64 = get_box_size() - get_header_size() - 4; |
371 | 1.17k | if (limits->max_color_profile_size && profile_size_64 > limits->max_color_profile_size) { |
372 | 0 | return Error(heif_error_Invalid_input, heif_suberror_Security_limit_exceeded, "Color profile exceeds maximum supported size"); |
373 | 0 | } |
374 | | |
375 | 1.17k | size_t profile_size = static_cast<size_t>(profile_size_64); |
376 | | |
377 | 1.17k | status = range.wait_for_available_bytes(profile_size); |
378 | 1.17k | if (status != StreamReader::grow_status::size_reached) { |
379 | | // TODO: return recoverable error at timeout |
380 | 0 | return Error(heif_error_Invalid_input, |
381 | 0 | heif_suberror_End_of_data); |
382 | 0 | } |
383 | | |
384 | 1.17k | std::vector<uint8_t> rawData(profile_size); |
385 | 468k | for (size_t i = 0; i < profile_size; i++) { |
386 | 467k | rawData[i] = range.read8(); |
387 | 467k | } |
388 | | |
389 | 1.17k | m_color_profile = std::make_shared<color_profile_raw>(colour_type, rawData); |
390 | 1.17k | } |
391 | 120 | else { |
392 | 120 | return Error(heif_error_Invalid_input, |
393 | 120 | heif_suberror_Unknown_color_profile_type); |
394 | 120 | } |
395 | | |
396 | 8.09k | return range.get_error(); |
397 | 8.21k | } |
398 | | |
399 | | |
400 | | std::string Box_colr::dump(Indent& indent) const |
401 | 0 | { |
402 | 0 | std::ostringstream sstr; |
403 | 0 | sstr << Box::dump(indent); |
404 | |
|
405 | 0 | if (m_color_profile) { |
406 | 0 | sstr << indent << "colour_type: " << fourcc_to_string(get_color_profile_type()) << "\n"; |
407 | 0 | sstr << m_color_profile->dump(indent); |
408 | 0 | } |
409 | 0 | else { |
410 | 0 | sstr << indent << "colour_type: ---\n"; |
411 | 0 | sstr << "no color profile\n"; |
412 | 0 | } |
413 | |
|
414 | 0 | return sstr.str(); |
415 | 0 | } |
416 | | |
417 | | |
418 | | std::string color_profile_raw::dump(Indent& indent) const |
419 | 0 | { |
420 | 0 | std::ostringstream sstr; |
421 | 0 | sstr << indent << "profile size: " << m_data.size() << "\n"; |
422 | 0 | return sstr.str(); |
423 | 0 | } |
424 | | |
425 | | |
426 | | std::string color_profile_nclx::dump(Indent& indent) const |
427 | 0 | { |
428 | 0 | std::ostringstream sstr; |
429 | 0 | sstr << indent << "colour_primaries: " << m_profile.m_colour_primaries << "\n" |
430 | 0 | << indent << "transfer_characteristics: " << m_profile.m_transfer_characteristics << "\n" |
431 | 0 | << indent << "matrix_coefficients: " << m_profile.m_matrix_coefficients << "\n" |
432 | 0 | << indent << "full_range_flag: " << m_profile.m_full_range_flag << "\n"; |
433 | 0 | return sstr.str(); |
434 | 0 | } |
435 | | |
436 | | |
437 | | Error color_profile_nclx::write(StreamWriter& writer) const |
438 | 9.25k | { |
439 | 9.25k | writer.write16(m_profile.m_colour_primaries); |
440 | 9.25k | writer.write16(m_profile.m_transfer_characteristics); |
441 | 9.25k | writer.write16(m_profile.m_matrix_coefficients); |
442 | 9.25k | writer.write8(m_profile.m_full_range_flag ? 0x80 : 0x00); |
443 | | |
444 | 9.25k | return Error::Ok; |
445 | 9.25k | } |
446 | | |
447 | | Error color_profile_raw::write(StreamWriter& writer) const |
448 | 6.95k | { |
449 | 6.95k | writer.write(m_data); |
450 | | |
451 | 6.95k | return Error::Ok; |
452 | 6.95k | } |
453 | | |
454 | | Error Box_colr::write(StreamWriter& writer) const |
455 | 16.2k | { |
456 | 16.2k | size_t box_start = reserve_box_header_space(writer); |
457 | | |
458 | 16.2k | assert(m_color_profile); |
459 | | |
460 | 16.2k | writer.write32(m_color_profile->get_type()); |
461 | | |
462 | 16.2k | Error err = m_color_profile->write(writer); |
463 | 16.2k | if (err) { |
464 | 0 | return err; |
465 | 0 | } |
466 | | |
467 | 16.2k | prepend_header(writer, box_start); |
468 | | |
469 | 16.2k | return Error::Ok; |
470 | 16.2k | } |
471 | | |
472 | | |