FFmpeg
Loading...
Searching...
No Matches
hdr_dynamic_metadata.c
Go to the documentation of this file.
1/**
2 * Copyright (c) 2018 Mohammad Izadi <moh.izadi at gmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "avassert.h"
23#include "mem.h"
24#include "libavcodec/defs.h"
25#include "libavcodec/get_bits.h"
26#include "libavcodec/put_bits.h"
27
28static const int64_t luminance_den = 1;
29static const int32_t peak_luminance_den = 15;
30static const int64_t rgb_den = 100000;
31static const int32_t fraction_pixel_den = 1000;
32static const int32_t knee_point_den = 4095;
33static const int32_t bezier_anchor_den = 1023;
35
37{
38 AVDynamicHDRPlus *hdr_plus = av_mallocz(sizeof(AVDynamicHDRPlus));
39
40 if (size)
41 *size = hdr_plus ? sizeof(*hdr_plus) : 0;
42
43 return hdr_plus;
44}
45
47{
50 sizeof(AVDynamicHDRPlus));
51 if (!side_data)
52 return NULL;
53
54 memset(side_data->data, 0, sizeof(AVDynamicHDRPlus));
55
56 return (AVDynamicHDRPlus *)side_data->data;
57}
58
60 size_t size)
61{
63 GetBitContext gbc, *gb = &gbc;
64 int ret;
65
66 if (!s)
67 return AVERROR(EINVAL);
68
70 return AVERROR(EINVAL);
71
72 memcpy(padded_buf, data, size);
73 // Zero-initialize the buffer padding to avoid overreads into uninitialized data.
74 memset(padded_buf + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
75
76 ret = init_get_bits8(gb, padded_buf, size);
77 if (ret < 0)
78 return ret;
79
80 if (get_bits_left(gb) < 10)
82
83 s->application_version = get_bits(gb, 8);
84 s->num_windows = get_bits(gb, 2);
85
86 if (s->num_windows < 1 || s->num_windows > 3) {
88 }
89
90 if (get_bits_left(gb) < ((19 * 8 + 1) * (s->num_windows - 1)))
92
93 for (int w = 1; w < s->num_windows; w++) {
94 // The corners are set to absolute coordinates here. They should be
95 // converted to the relative coordinates (in [0, 1]) in the decoder.
97 params->window_upper_left_corner_x =
98 (AVRational){get_bits(gb, 16), 1};
99 params->window_upper_left_corner_y =
100 (AVRational){get_bits(gb, 16), 1};
101 params->window_lower_right_corner_x =
102 (AVRational){get_bits(gb, 16), 1};
103 params->window_lower_right_corner_y =
104 (AVRational){get_bits(gb, 16), 1};
105
106 params->center_of_ellipse_x = get_bits(gb, 16);
107 params->center_of_ellipse_y = get_bits(gb, 16);
108 params->rotation_angle = get_bits(gb, 8);
109 params->semimajor_axis_internal_ellipse = get_bits(gb, 16);
110 params->semimajor_axis_external_ellipse = get_bits(gb, 16);
111 params->semiminor_axis_external_ellipse = get_bits(gb, 16);
112 params->overlap_process_option = get_bits1(gb);
113 }
114
115 if (get_bits_left(gb) < 28)
116 return AVERROR_INVALIDDATA;
117
118 s->targeted_system_display_maximum_luminance =
120 s->targeted_system_display_actual_peak_luminance_flag = get_bits1(gb);
121
122 if (s->targeted_system_display_actual_peak_luminance_flag) {
123 int rows, cols;
124 if (get_bits_left(gb) < 10)
125 return AVERROR_INVALIDDATA;
126 rows = get_bits(gb, 5);
127 cols = get_bits(gb, 5);
128 if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) {
129 return AVERROR_INVALIDDATA;
130 }
131 s->num_rows_targeted_system_display_actual_peak_luminance = rows;
132 s->num_cols_targeted_system_display_actual_peak_luminance = cols;
133
134 if (get_bits_left(gb) < (rows * cols * 4))
135 return AVERROR_INVALIDDATA;
136
137 for (int i = 0; i < rows; i++) {
138 for (int j = 0; j < cols; j++) {
139 s->targeted_system_display_actual_peak_luminance[i][j] =
141 }
142 }
143 }
144 for (int w = 0; w < s->num_windows; w++) {
146 if (get_bits_left(gb) < (3 * 17 + 17 + 4))
147 return AVERROR_INVALIDDATA;
148
149 for (int i = 0; i < 3; i++) {
150 params->maxscl[i] =
151 (AVRational){get_bits(gb, 17), rgb_den};
152 }
153 params->average_maxrgb =
154 (AVRational){get_bits(gb, 17), rgb_den};
155 params->num_distribution_maxrgb_percentiles = get_bits(gb, 4);
156
157 if (get_bits_left(gb) <
158 (params->num_distribution_maxrgb_percentiles * 24))
159 return AVERROR_INVALIDDATA;
160
161 for (int i = 0; i < params->num_distribution_maxrgb_percentiles; i++) {
162 params->distribution_maxrgb[i].percentage = get_bits(gb, 7);
163 params->distribution_maxrgb[i].percentile =
164 (AVRational){get_bits(gb, 17), rgb_den};
165 }
166
167 if (get_bits_left(gb) < 10)
168 return AVERROR_INVALIDDATA;
169
170 params->fraction_bright_pixels = (AVRational){get_bits(gb, 10), fraction_pixel_den};
171 }
172 if (get_bits_left(gb) < 1)
173 return AVERROR_INVALIDDATA;
174 s->mastering_display_actual_peak_luminance_flag = get_bits1(gb);
175 if (s->mastering_display_actual_peak_luminance_flag) {
176 int rows, cols;
177 if (get_bits_left(gb) < 10)
178 return AVERROR_INVALIDDATA;
179 rows = get_bits(gb, 5);
180 cols = get_bits(gb, 5);
181 if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) {
182 return AVERROR_INVALIDDATA;
183 }
184 s->num_rows_mastering_display_actual_peak_luminance = rows;
185 s->num_cols_mastering_display_actual_peak_luminance = cols;
186
187 if (get_bits_left(gb) < (rows * cols * 4))
188 return AVERROR_INVALIDDATA;
189
190 for (int i = 0; i < rows; i++) {
191 for (int j = 0; j < cols; j++) {
192 s->mastering_display_actual_peak_luminance[i][j] =
194 }
195 }
196 }
197
198 for (int w = 0; w < s->num_windows; w++) {
200 if (get_bits_left(gb) < 1)
201 return AVERROR_INVALIDDATA;
202
203 params->tone_mapping_flag = get_bits1(gb);
204 if (params->tone_mapping_flag) {
205 if (get_bits_left(gb) < 28)
206 return AVERROR_INVALIDDATA;
207
208 params->knee_point_x =
210 params->knee_point_y =
212 params->num_bezier_curve_anchors = get_bits(gb, 4);
213
214 if (get_bits_left(gb) < (params->num_bezier_curve_anchors * 10))
215 return AVERROR_INVALIDDATA;
216
217 for (int i = 0; i < params->num_bezier_curve_anchors; i++) {
218 params->bezier_curve_anchors[i] =
220 }
221 }
222
223 if (get_bits_left(gb) < 1)
224 return AVERROR_INVALIDDATA;
225 params->color_saturation_mapping_flag = get_bits1(gb);
226 if (params->color_saturation_mapping_flag) {
227 if (get_bits_left(gb) < 6)
228 return AVERROR_INVALIDDATA;
229 params->color_saturation_weight =
231 }
232 }
233
234 return 0;
235}
236
238{
239 uint8_t *buf;
240 size_t size_bits, size_bytes;
241 PutBitContext pbc, *pb = &pbc;
242
243 if (!s)
244 return AVERROR(EINVAL);
245 if ((!data || *data) && !size)
246 return AVERROR(EINVAL);
247 /* The caps put the payload at exactly 907 bytes; the assert cannot fire. */
248 if (s->num_windows > FF_ARRAY_ELEMS(s->params))
249 return AVERROR(EINVAL);
250
251 for (int w = 0; w < s->num_windows; w++) {
252 const AVHDRPlusColorTransformParams *p = &s->params[w];
253
254 if (p->num_distribution_maxrgb_percentiles >
255 FF_ARRAY_ELEMS(p->distribution_maxrgb))
256 return AVERROR(EINVAL);
257
258 /* The anchors are only read while tone mapping is enabled. */
259 if (p->tone_mapping_flag &&
260 p->num_bezier_curve_anchors >
261 FF_ARRAY_ELEMS(p->bezier_curve_anchors))
262 return AVERROR(EINVAL);
263 }
264
265 /* The peak-luminance pairs use a row count and a column count, and are
266 * only read when their flag is set, so check them the same way. */
267 if (s->targeted_system_display_actual_peak_luminance_flag) {
268 if (s->num_rows_targeted_system_display_actual_peak_luminance >
269 FF_ARRAY_ELEMS(s->targeted_system_display_actual_peak_luminance) ||
270 s->num_cols_targeted_system_display_actual_peak_luminance >
271 FF_ARRAY_ELEMS(s->targeted_system_display_actual_peak_luminance[0]))
272 return AVERROR(EINVAL);
273 }
274
275 if (s->mastering_display_actual_peak_luminance_flag) {
276 if (s->num_rows_mastering_display_actual_peak_luminance >
277 FF_ARRAY_ELEMS(s->mastering_display_actual_peak_luminance) ||
278 s->num_cols_mastering_display_actual_peak_luminance >
279 FF_ARRAY_ELEMS(s->mastering_display_actual_peak_luminance[0]))
280 return AVERROR(EINVAL);
281 }
282
283 /**
284 * Buffer size per CTA-861-H p.253-254:
285 * 48 header bits (excluded from the serialized payload)
286 * 8 bits for application_mode
287 * 2 bits for num_windows
288 * 153 bits for window geometry, for each window above 1
289 * 27 bits for targeted_system_display_maximum_luminance
290 * 1-2511 bits for targeted system display peak luminance information
291 * 82-442 bits per window for pixel distribution information
292 * 1-2511 bits for mastering display peak luminance information
293 * 1-179 bits per window for tonemapping information
294 * 1-7 bits per window for color saturation mapping information
295 * Total: 123-7249 bits, excluding trimmed header bits
296 */
297 size_bits = 8;
298
299 size_bits += 2;
300
301 for (int w = 1; w < s->num_windows; w++)
302 size_bits += 153;
303
304 size_bits += 27;
305
306 size_bits += 1;
307 if (s->targeted_system_display_actual_peak_luminance_flag)
308 size_bits += 10 +
309 s->num_rows_targeted_system_display_actual_peak_luminance *
310 s->num_cols_targeted_system_display_actual_peak_luminance * 4;
311
312 for (int w = 0; w < s->num_windows; w++)
313 size_bits += 72 + s->params[w].num_distribution_maxrgb_percentiles * 24 + 10;
314
315 size_bits += 1;
316 if (s->mastering_display_actual_peak_luminance_flag)
317 size_bits += 10 +
318 s->num_rows_mastering_display_actual_peak_luminance *
319 s->num_cols_mastering_display_actual_peak_luminance * 4;
320
321 for (int w = 0; w < s->num_windows; w++) {
322 size_bits += 1;
323 if (s->params[w].tone_mapping_flag)
324 size_bits += 28 + s->params[w].num_bezier_curve_anchors * 10;
325
326 size_bits += 1;
327 if (s->params[w].color_saturation_mapping_flag)
328 size_bits += 6;
329 }
330
331 size_bytes = (size_bits + 7) / 8;
332
334
335 if (!data) {
336 *size = size_bytes;
337 return 0;
338 } else if (*data) {
339 if (*size < size_bytes)
341 buf = *data;
342 } else {
343 buf = av_malloc(size_bytes);
344 if (!buf)
345 return AVERROR(ENOMEM);
346 }
347
348 init_put_bits(pb, buf, size_bytes);
349
350 // application_mode is set to Application Version 1
351 put_bits(pb, 8, 1);
352
353 // Payload as per CTA-861-H p.253-254
354 put_bits(pb, 2, s->num_windows);
355
356 for (int w = 1; w < s->num_windows; w++) {
357 put_bits(pb, 16, s->params[w].window_upper_left_corner_x.num / s->params[w].window_upper_left_corner_x.den);
358 put_bits(pb, 16, s->params[w].window_upper_left_corner_y.num / s->params[w].window_upper_left_corner_y.den);
359 put_bits(pb, 16, s->params[w].window_lower_right_corner_x.num / s->params[w].window_lower_right_corner_x.den);
360 put_bits(pb, 16, s->params[w].window_lower_right_corner_y.num / s->params[w].window_lower_right_corner_y.den);
361 put_bits(pb, 16, s->params[w].center_of_ellipse_x);
362 put_bits(pb, 16, s->params[w].center_of_ellipse_y);
363 put_bits(pb, 8, s->params[w].rotation_angle);
364 put_bits(pb, 16, s->params[w].semimajor_axis_internal_ellipse);
365 put_bits(pb, 16, s->params[w].semimajor_axis_external_ellipse);
366 put_bits(pb, 16, s->params[w].semiminor_axis_external_ellipse);
367 put_bits(pb, 1, s->params[w].overlap_process_option);
368 }
369
370 put_bits(pb, 27, s->targeted_system_display_maximum_luminance.num * luminance_den /
371 s->targeted_system_display_maximum_luminance.den);
372 put_bits(pb, 1, s->targeted_system_display_actual_peak_luminance_flag);
373 if (s->targeted_system_display_actual_peak_luminance_flag) {
374 put_bits(pb, 5, s->num_rows_targeted_system_display_actual_peak_luminance);
375 put_bits(pb, 5, s->num_cols_targeted_system_display_actual_peak_luminance);
376 for (int i = 0; i < s->num_rows_targeted_system_display_actual_peak_luminance; i++) {
377 for (int j = 0; j < s->num_cols_targeted_system_display_actual_peak_luminance; j++)
378 put_bits(pb, 4, s->targeted_system_display_actual_peak_luminance[i][j].num * peak_luminance_den /
379 s->targeted_system_display_actual_peak_luminance[i][j].den);
380 }
381 }
382
383 for (int w = 0; w < s->num_windows; w++) {
384 for (int i = 0; i < 3; i++)
385 put_bits(pb, 17, s->params[w].maxscl[i].num * rgb_den / s->params[w].maxscl[i].den);
386 put_bits(pb, 17, s->params[w].average_maxrgb.num * rgb_den / s->params[w].average_maxrgb.den);
387 put_bits(pb, 4, s->params[w].num_distribution_maxrgb_percentiles);
388 for (int i = 0; i < s->params[w].num_distribution_maxrgb_percentiles; i++) {
389 put_bits(pb, 7, s->params[w].distribution_maxrgb[i].percentage);
390 put_bits(pb, 17, s->params[w].distribution_maxrgb[i].percentile.num * rgb_den /
391 s->params[w].distribution_maxrgb[i].percentile.den);
392 }
393 put_bits(pb, 10, s->params[w].fraction_bright_pixels.num * fraction_pixel_den /
394 s->params[w].fraction_bright_pixels.den);
395 }
396
397 put_bits(pb, 1, s->mastering_display_actual_peak_luminance_flag);
398 if (s->mastering_display_actual_peak_luminance_flag) {
399 put_bits(pb, 5, s->num_rows_mastering_display_actual_peak_luminance);
400 put_bits(pb, 5, s->num_cols_mastering_display_actual_peak_luminance);
401 for (int i = 0; i < s->num_rows_mastering_display_actual_peak_luminance; i++) {
402 for (int j = 0; j < s->num_cols_mastering_display_actual_peak_luminance; j++)
403 put_bits(pb, 4, s->mastering_display_actual_peak_luminance[i][j].num * peak_luminance_den /
404 s->mastering_display_actual_peak_luminance[i][j].den);
405 }
406 }
407
408 for (int w = 0; w < s->num_windows; w++) {
409 put_bits(pb, 1, s->params[w].tone_mapping_flag);
410 if (s->params[w].tone_mapping_flag) {
411 put_bits(pb, 12, s->params[w].knee_point_x.num * knee_point_den / s->params[w].knee_point_x.den);
412 put_bits(pb, 12, s->params[w].knee_point_y.num * knee_point_den / s->params[w].knee_point_y.den);
413 put_bits(pb, 4, s->params[w].num_bezier_curve_anchors);
414 for (int i = 0; i < s->params[w].num_bezier_curve_anchors; i++)
415 put_bits(pb, 10, s->params[w].bezier_curve_anchors[i].num * bezier_anchor_den /
416 s->params[w].bezier_curve_anchors[i].den);
417 }
418 put_bits(pb, 1, s->params[w].color_saturation_mapping_flag);
419 if (s->params[w].color_saturation_mapping_flag)
420 put_bits(pb, 6, s->params[w].color_saturation_weight.num * saturation_weight_den /
421 s->params[w].color_saturation_weight.den);
422 }
423
424 flush_put_bits(pb);
425
426 *data = buf;
427 if (size)
428 *size = size_bytes;
429 return 0;
430}
431
433{
435
436 if (size)
437 *size = smpte2094_app5 ? sizeof(*smpte2094_app5) : 0;
438
439 return smpte2094_app5;
440}
441
454
455#define GET_BITS_OR_FAIL(var, n) \
456 do { \
457 if (get_bits_left(gb) < n) { \
458 ret = AVERROR_INVALIDDATA; \
459 goto end; \
460 } \
461 var = get_bits(gb, n); \
462 } while (0)
463
465 size_t size)
466{
467 GetBitContext gbc, *gb = &gbc;
468 int ret, reserved_zero;
469 size_t padded_size = size + AV_INPUT_BUFFER_PADDING_SIZE;
470
471 if (!s)
472 return AVERROR(EINVAL);
473
474 uint8_t *padded_data = av_mallocz(padded_size);
475 if (!padded_data)
476 return AVERROR(ENOMEM);
477
478 memcpy(padded_data, data, size);
479 ret = init_get_bits8(gb, padded_data, size);
480 if (ret < 0)
481 goto end;
482
483 // Table C.1
484 GET_BITS_OR_FAIL(s->application_version, 3);
485 GET_BITS_OR_FAIL(s->minimum_application_version, 3);
486 GET_BITS_OR_FAIL(reserved_zero, 2);
487 if (reserved_zero) {
489 goto end;
490 }
491
492 // Table C.2
493 GET_BITS_OR_FAIL(s->has_custom_hdr_reference_white_flag, 1);
494 GET_BITS_OR_FAIL(s->has_adaptive_tone_map_flag, 1);
495 GET_BITS_OR_FAIL(reserved_zero, 6);
496 if (reserved_zero) {
498 goto end;
499 }
500 if (s->has_custom_hdr_reference_white_flag)
501 GET_BITS_OR_FAIL(s->hdr_reference_white, 16);
502 if (!s->has_adaptive_tone_map_flag) {
503 ret = 0;
504 goto end;
505 }
506
507 // Table C.3
508 GET_BITS_OR_FAIL(s->baseline_hdr_headroom, 16);
509 GET_BITS_OR_FAIL(s->use_reference_white_tone_mapping_flag, 1);
510 if (s->use_reference_white_tone_mapping_flag) {
511 GET_BITS_OR_FAIL(reserved_zero, 7);
512 ret = reserved_zero ? AVERROR_INVALIDDATA : 0;
513 goto end;
514 }
515 GET_BITS_OR_FAIL(s->num_alternate_images, 3);
516 if (s->num_alternate_images > 4) {
518 goto end;
519 }
520 GET_BITS_OR_FAIL(s->gain_application_space_chromaticities_flag, 2);
521 GET_BITS_OR_FAIL(s->has_common_component_mix_params_flag, 1);
522 GET_BITS_OR_FAIL(s->has_common_curve_params_flag, 1);
523 if (s->gain_application_space_chromaticities_flag == 3)
524 for (int r = 0; r < 8; r++)
525 GET_BITS_OR_FAIL(s->gain_application_space_chromaticities[r], 16);
526
527 for (int a = 0; a < s->num_alternate_images; a++) {
528 GET_BITS_OR_FAIL(s->alternate_hdr_headrooms[a], 16);
529
530 // Table C.4
531 if (!a || !s->has_common_component_mix_params_flag) {
532 GET_BITS_OR_FAIL(s->component_mixing_type[a], 2);
533 if (s->component_mixing_type[a] != 3) {
534 GET_BITS_OR_FAIL(reserved_zero, 6);
535 if (reserved_zero) {
537 goto end;
538 }
539 } else {
540 for (int k = 0; k < 6; k++)
541 GET_BITS_OR_FAIL(s->has_component_mixing_coefficient_flag[a][k], 1);
542 for (int k = 0; k < 6; k++) {
543 if (s->has_component_mixing_coefficient_flag[a][k]) {
544 GET_BITS_OR_FAIL(s->component_mixing_coefficient[a][k], 16);
545 } else {
546 s->component_mixing_coefficient[a][k] = 0;
547 }
548 }
549 }
550 } else {
551 s->component_mixing_type[a] = s->component_mixing_type[0];
552 if (s->component_mixing_type[a] == 3) {
553 for (int k = 0; k < 6; k++) {
554 s->has_component_mixing_coefficient_flag[a][k] =
555 s->has_component_mixing_coefficient_flag[0][k];
556 s->component_mixing_coefficient[a][k] = s->component_mixing_coefficient[0][k];
557 }
558 }
559 }
560
561 // Table C.5
562 if (!a || !s->has_common_curve_params_flag) {
563 GET_BITS_OR_FAIL(s->gain_curve_num_control_points_minus_1[a], 5);
564 if (s->gain_curve_num_control_points_minus_1[a] > 31) {
566 goto end;
567 }
568 GET_BITS_OR_FAIL(s->gain_curve_use_pchip_slope_flag[a], 1);
569 GET_BITS_OR_FAIL(reserved_zero, 2);
570 if (reserved_zero) {
572 goto end;
573 }
574 for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++) {
575 GET_BITS_OR_FAIL(s->gain_curve_control_points_x[a][c], 16);
576 }
577 } else {
578 s->gain_curve_num_control_points_minus_1[a] =
579 s->gain_curve_num_control_points_minus_1[0];
580 s->gain_curve_use_pchip_slope_flag[a] = s->gain_curve_use_pchip_slope_flag[0];
581 for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++) {
582 s->gain_curve_control_points_x[a][c] = s->gain_curve_control_points_x[0][c];
583 }
584 }
585 for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
586 GET_BITS_OR_FAIL(s->gain_curve_control_points_y[a][c], 16);
587 if (!s->gain_curve_use_pchip_slope_flag[a]) {
588 for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
589 GET_BITS_OR_FAIL(s->gain_curve_control_points_theta[a][c], 16);
590 }
591 }
592 ret = 0;
593end:
594 av_free(padded_data);
595 return ret;
596}
597
599 size_t *size)
600{
601 uint8_t *buf;
602 size_t size_bytes, size_bits;
603 PutBitContext pbc, *pb = &pbc;
604
605 if (!s)
606 return AVERROR(EINVAL);
607 if ((!data || *data) && !size)
608 return AVERROR(EINVAL);
609
610 if (s->application_version >= 8 || s->minimum_application_version >= 3)
611 return AVERROR_INVALIDDATA;
612 size_bits = 0;
613 size_bits += 3 + 3 + 2;
614 size_bits += 1 + 1 + 6;
615 if (s->has_custom_hdr_reference_white_flag)
616 size_bits += 16;
617 if (s->has_adaptive_tone_map_flag) {
618 size_bits += 16 + 1;
619 if (s->use_reference_white_tone_mapping_flag) {
620 size_bits += 7;
621 } else {
622 size_bits += 3 + 2 + 1 + 1;
623 if (s->gain_application_space_chromaticities_flag == 3)
624 size_bits += 16 * 8;
625 if (s->num_alternate_images > 4)
626 return AVERROR_INVALIDDATA;
627 for (int a = 0; a < s->num_alternate_images; a++) {
628 size_bits += 16;
629 if (!a || !s->has_common_component_mix_params_flag) {
630 size_bits += 2;
631 if (s->component_mixing_type[a] != 3) {
632 size_bits += 6;
633 } else {
634 size_bits += 6;
635 for (int k = 0; k < 6; k++)
636 if (s->has_component_mixing_coefficient_flag[a][k])
637 size_bits += 16;
638 }
639 }
640 if (!a || !s->has_common_curve_params_flag) {
641 size_bits += 5 + 1 + 2;
642 if (s->gain_curve_num_control_points_minus_1[a] > 31)
643 return AVERROR_INVALIDDATA;
644 size_bits += 16 * (s->gain_curve_num_control_points_minus_1[a] + 1);
645 }
646 size_bits += 16 * (s->gain_curve_num_control_points_minus_1[a] + 1);
647 if (!s->gain_curve_use_pchip_slope_flag[a])
648 size_bits += 16 * (s->gain_curve_num_control_points_minus_1[a] + 1);
649 }
650 }
651 }
652 if (size_bits % 8)
653 return AVERROR_INVALIDDATA;
654 size_bytes = size_bits >> 3;
655
656 if (!data) {
657 *size = size_bytes;
658 return 0;
659 } else if (*data) {
660 if (*size < size_bytes)
662 buf = *data;
663 } else {
664 buf = av_malloc(size_bytes);
665 if (!buf)
666 return AVERROR(ENOMEM);
667 }
668
669 init_put_bits(pb, buf, size_bytes);
670
671 // Table C.1
672 put_bits(pb, 3, s->application_version);
673 put_bits(pb, 3, s->minimum_application_version);
674 put_bits(pb, 2, 0); // reserved_zero
675
676 // Table C.2
677 put_bits(pb, 1, s->has_custom_hdr_reference_white_flag);
678 put_bits(pb, 1, s->has_adaptive_tone_map_flag);
679 put_bits(pb, 6, 0); // reserved_zero
680
681 if (s->has_custom_hdr_reference_white_flag)
682 put_bits(pb, 16, s->hdr_reference_white);
683
684 if (s->has_adaptive_tone_map_flag) {
685 // Table C.3
686 put_bits(pb, 16, s->baseline_hdr_headroom);
687 put_bits(pb, 1, s->use_reference_white_tone_mapping_flag);
688 if (s->use_reference_white_tone_mapping_flag) {
689 put_bits(pb, 7, 0); // reserved_zero
690 } else {
691 put_bits(pb, 3, s->num_alternate_images);
692 put_bits(pb, 2, s->gain_application_space_chromaticities_flag);
693 put_bits(pb, 1, s->has_common_component_mix_params_flag);
694 put_bits(pb, 1, s->has_common_curve_params_flag);
695
696 if (s->gain_application_space_chromaticities_flag == 3)
697 for (int r = 0; r < 8; r++)
698 put_bits(pb, 16, s->gain_application_space_chromaticities[r]);
699
700 for (int a = 0; a < s->num_alternate_images; a++) {
701 put_bits(pb, 16, s->alternate_hdr_headrooms[a]);
702
703 // Table C.4
704 if (!a || !s->has_common_component_mix_params_flag) {
705 put_bits(pb, 2, s->component_mixing_type[a]);
706 if (s->component_mixing_type[a] != 3) {
707 put_bits(pb, 6, 0); // reserved_zero
708 } else {
709 for (int k = 0; k < 6; k++)
710 put_bits(pb, 1, s->has_component_mixing_coefficient_flag[a][k]);
711 for (int k = 0; k < 6; k++)
712 if (s->has_component_mixing_coefficient_flag[a][k])
713 put_bits(pb, 16, s->component_mixing_coefficient[a][k]);
714 }
715 }
716
717 // Table C.5
718 if (!a || !s->has_common_curve_params_flag) {
719 put_bits(pb, 5, s->gain_curve_num_control_points_minus_1[a]);
720 put_bits(pb, 1, s->gain_curve_use_pchip_slope_flag[a]);
721 put_bits(pb, 2, 0); // reserved_zero
722 for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
723 put_bits(pb, 16, s->gain_curve_control_points_x[a][c]);
724 }
725 for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
726 put_bits(pb, 16, s->gain_curve_control_points_y[a][c]);
727 if (!s->gain_curve_use_pchip_slope_flag[a]) {
728 for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
729 put_bits(pb, 16, s->gain_curve_control_points_theta[a][c]);
730 }
731 }
732 }
733 }
734
735 flush_put_bits(pb);
736
737 *data = buf;
738 if (size)
739 *size = size_bytes;
740 return 0;
741}
SwsAArch64OpImplParams params
Definition ops.c:52
int32_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
Misc types and constants that do not belong anywhere else.
static AVFrame * frame
int ret
Definition ffplay.c:391
bitstream reader API header.
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition get_bits.h:424
static int get_bits_left(GetBitContext *gb)
Definition get_bits.h:688
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition get_bits.h:337
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition defs.h:40
#define AVERROR_BUFFER_TOO_SMALL
Buffer too small.
Definition error.h:53
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
AVFrameSideData * av_frame_new_side_data(AVFrame *frame, enum AVFrameSideDataType type, size_t size)
Add a new side data to a frame.
Definition frame.c:650
@ AV_FRAME_DATA_DYNAMIC_HDR_PLUS
HDR dynamic metadata associated with a video frame.
Definition frame.h:159
@ AV_FRAME_DATA_DYNAMIC_HDR_SMPTE_2094_APP5
HDR dynamic metadata associated with a video frame.
Definition frame.h:270
int a
AVDynamicHDRPlus * av_dynamic_hdr_plus_alloc(size_t *size)
Allocate an AVDynamicHDRPlus structure and set its fields to default values.
AVDynamicHDRPlus * av_dynamic_hdr_plus_create_side_data(AVFrame *frame)
Allocate a complete AVDynamicHDRPlus and add it to the frame.
static const int64_t rgb_den
AVDynamicHDRSmpte2094App5 * av_dynamic_hdr_smpte2094_app5_alloc(size_t *size)
Allocate an AVDynamicHDRSmpte2094App5 structure and set its fields to default values.
static const int32_t peak_luminance_den
static const int32_t bezier_anchor_den
static const int32_t fraction_pixel_den
#define GET_BITS_OR_FAIL(var, n)
int av_dynamic_hdr_smpte2094_app5_to_t35(const AVDynamicHDRSmpte2094App5 *s, uint8_t **data, size_t *size)
Serialize dynamic SMPTE-2094-50 metadata to a ITU-T T.35 message.
int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data, size_t size)
Parse the user data registered ITU-T T.35 to AVbuffer (AVDynamicHDRPlus).
static const int32_t knee_point_den
int av_dynamic_hdr_plus_to_t35(const AVDynamicHDRPlus *s, uint8_t **data, size_t *size)
Serialize dynamic HDR10+ metadata to a user data registered ITU-T T.35 buffer, excluding the first 48...
int av_dynamic_hdr_smpte2094_app5_from_t35(AVDynamicHDRSmpte2094App5 *s, const uint8_t *data, size_t size)
Parse the user data formatted as ITU-T T.35 message to AVDynamicHDRSmpte2094App5.
AVDynamicHDRSmpte2094App5 * av_dynamic_hdr_smpte2094_app5_create_side_data(AVFrame *frame)
Allocate a complete AVDynamicHDRSmpte2094App5 and add it to the frame.
static const int32_t saturation_weight_den
static const int64_t luminance_den
Copyright (c) 2018 Mohammad Izadi <moh.izadi at gmail.com>
#define AV_HDR_PLUS_MAX_PAYLOAD_SIZE
#define r
Definition input.c:42
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition j2kenc.c:154
uint8_t w
Definition llvidencdsp.c:39
Memory handling functions.
const char data[16]
Definition mxf.c:149
#define av_malloc(s)
Definition ops_static.c:52
bitstream writer API
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition put_bits.h:62
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition put_bits.h:153
#define FF_ARRAY_ELEMS(a)
This struct represents dynamic metadata for color volume transform - application 4 of SMPTE 2094-40:2...
This struct represents dynamic metadata for color volume transform as specified in the SMPTE 2094-50 ...
Structure to hold side data for an AVFrame.
Definition frame.h:334
uint8_t * data
Definition frame.h:336
This structure describes decoded (raw) audio or video data.
Definition frame.h:479
Color transform parameters at a processing window in a dynamic metadata for SMPTE 2094-40.
Rational number (pair of numerator and denominator).
Definition rational.h:58
#define av_free(p)
#define av_mallocz(s)
int size
static double c[64]