FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/hdr_dynamic_metadata.c
Date: 2026-04-26 13:12:44
Exec Total Coverage
Lines: 127 442 28.7%
Functions: 3 8 37.5%
Branches: 66 362 18.2%

Line Branch Exec Source
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"
22 #include "hdr_dynamic_metadata.h"
23 #include "mem.h"
24 #include "libavcodec/defs.h"
25 #include "libavcodec/get_bits.h"
26 #include "libavcodec/put_bits.h"
27
28 static const int64_t luminance_den = 1;
29 static const int32_t peak_luminance_den = 15;
30 static const int64_t rgb_den = 100000;
31 static const int32_t fraction_pixel_den = 1000;
32 static const int32_t knee_point_den = 4095;
33 static const int32_t bezier_anchor_den = 1023;
34 static const int32_t saturation_weight_den = 8;
35
36 17 AVDynamicHDRPlus *av_dynamic_hdr_plus_alloc(size_t *size)
37 {
38 17 AVDynamicHDRPlus *hdr_plus = av_mallocz(sizeof(AVDynamicHDRPlus));
39
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!hdr_plus)
40 return NULL;
41
42
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (size)
43 17 *size = sizeof(*hdr_plus);
44
45 17 return hdr_plus;
46 }
47
48 AVDynamicHDRPlus *av_dynamic_hdr_plus_create_side_data(AVFrame *frame)
49 {
50 AVFrameSideData *side_data = av_frame_new_side_data(frame,
51 AV_FRAME_DATA_DYNAMIC_HDR_PLUS,
52 sizeof(AVDynamicHDRPlus));
53 if (!side_data)
54 return NULL;
55
56 memset(side_data->data, 0, sizeof(AVDynamicHDRPlus));
57
58 return (AVDynamicHDRPlus *)side_data->data;
59 }
60
61 17 int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data,
62 size_t size)
63 {
64 uint8_t padded_buf[AV_HDR_PLUS_MAX_PAYLOAD_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
65 17 GetBitContext gbc, *gb = &gbc;
66 int ret;
67
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!s)
69 return AVERROR(ENOMEM);
70
71
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (size > AV_HDR_PLUS_MAX_PAYLOAD_SIZE)
72 return AVERROR(EINVAL);
73
74 17 memcpy(padded_buf, data, size);
75 // Zero-initialize the buffer padding to avoid overreads into uninitialized data.
76 17 memset(padded_buf + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
77
78 17 ret = init_get_bits8(gb, padded_buf, size);
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (ret < 0)
80 return ret;
81
82
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 if (get_bits_left(gb) < 10)
83 return AVERROR_INVALIDDATA;
84
85 17 s->application_version = get_bits(gb, 8);
86 17 s->num_windows = get_bits(gb, 2);
87
88
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 17 times.
17 if (s->num_windows < 1 || s->num_windows > 3) {
89 return AVERROR_INVALIDDATA;
90 }
91
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 if (get_bits_left(gb) < ((19 * 8 + 1) * (s->num_windows - 1)))
93 return AVERROR_INVALIDDATA;
94
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 for (int w = 1; w < s->num_windows; w++) {
96 // The corners are set to absolute coordinates here. They should be
97 // converted to the relative coordinates (in [0, 1]) in the decoder.
98 AVHDRPlusColorTransformParams *params = &s->params[w];
99 params->window_upper_left_corner_x =
100 (AVRational){get_bits(gb, 16), 1};
101 params->window_upper_left_corner_y =
102 (AVRational){get_bits(gb, 16), 1};
103 params->window_lower_right_corner_x =
104 (AVRational){get_bits(gb, 16), 1};
105 params->window_lower_right_corner_y =
106 (AVRational){get_bits(gb, 16), 1};
107
108 params->center_of_ellipse_x = get_bits(gb, 16);
109 params->center_of_ellipse_y = get_bits(gb, 16);
110 params->rotation_angle = get_bits(gb, 8);
111 params->semimajor_axis_internal_ellipse = get_bits(gb, 16);
112 params->semimajor_axis_external_ellipse = get_bits(gb, 16);
113 params->semiminor_axis_external_ellipse = get_bits(gb, 16);
114 params->overlap_process_option = get_bits1(gb);
115 }
116
117
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 if (get_bits_left(gb) < 28)
118 return AVERROR_INVALIDDATA;
119
120 17 s->targeted_system_display_maximum_luminance =
121 17 (AVRational){get_bits_long(gb, 27), luminance_den};
122 17 s->targeted_system_display_actual_peak_luminance_flag = get_bits1(gb);
123
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (s->targeted_system_display_actual_peak_luminance_flag) {
125 int rows, cols;
126 if (get_bits_left(gb) < 10)
127 return AVERROR_INVALIDDATA;
128 rows = get_bits(gb, 5);
129 cols = get_bits(gb, 5);
130 if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) {
131 return AVERROR_INVALIDDATA;
132 }
133 s->num_rows_targeted_system_display_actual_peak_luminance = rows;
134 s->num_cols_targeted_system_display_actual_peak_luminance = cols;
135
136 if (get_bits_left(gb) < (rows * cols * 4))
137 return AVERROR_INVALIDDATA;
138
139 for (int i = 0; i < rows; i++) {
140 for (int j = 0; j < cols; j++) {
141 s->targeted_system_display_actual_peak_luminance[i][j] =
142 (AVRational){get_bits(gb, 4), peak_luminance_den};
143 }
144 }
145 }
146
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 17 times.
34 for (int w = 0; w < s->num_windows; w++) {
147 17 AVHDRPlusColorTransformParams *params = &s->params[w];
148
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 if (get_bits_left(gb) < (3 * 17 + 17 + 4))
149 return AVERROR_INVALIDDATA;
150
151
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 17 times.
68 for (int i = 0; i < 3; i++) {
152 51 params->maxscl[i] =
153 51 (AVRational){get_bits(gb, 17), rgb_den};
154 }
155 17 params->average_maxrgb =
156 17 (AVRational){get_bits(gb, 17), rgb_den};
157 17 params->num_distribution_maxrgb_percentiles = get_bits(gb, 4);
158
159 17 if (get_bits_left(gb) <
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 (params->num_distribution_maxrgb_percentiles * 24))
161 return AVERROR_INVALIDDATA;
162
163
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 17 times.
170 for (int i = 0; i < params->num_distribution_maxrgb_percentiles; i++) {
164 153 params->distribution_maxrgb[i].percentage = get_bits(gb, 7);
165 153 params->distribution_maxrgb[i].percentile =
166 153 (AVRational){get_bits(gb, 17), rgb_den};
167 }
168
169
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 if (get_bits_left(gb) < 10)
170 return AVERROR_INVALIDDATA;
171
172 17 params->fraction_bright_pixels = (AVRational){get_bits(gb, 10), fraction_pixel_den};
173 }
174
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 if (get_bits_left(gb) < 1)
175 return AVERROR_INVALIDDATA;
176 17 s->mastering_display_actual_peak_luminance_flag = get_bits1(gb);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (s->mastering_display_actual_peak_luminance_flag) {
178 int rows, cols;
179 if (get_bits_left(gb) < 10)
180 return AVERROR_INVALIDDATA;
181 rows = get_bits(gb, 5);
182 cols = get_bits(gb, 5);
183 if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) {
184 return AVERROR_INVALIDDATA;
185 }
186 s->num_rows_mastering_display_actual_peak_luminance = rows;
187 s->num_cols_mastering_display_actual_peak_luminance = cols;
188
189 if (get_bits_left(gb) < (rows * cols * 4))
190 return AVERROR_INVALIDDATA;
191
192 for (int i = 0; i < rows; i++) {
193 for (int j = 0; j < cols; j++) {
194 s->mastering_display_actual_peak_luminance[i][j] =
195 (AVRational){get_bits(gb, 4), peak_luminance_den};
196 }
197 }
198 }
199
200
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 17 times.
34 for (int w = 0; w < s->num_windows; w++) {
201 17 AVHDRPlusColorTransformParams *params = &s->params[w];
202
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 if (get_bits_left(gb) < 1)
203 return AVERROR_INVALIDDATA;
204
205 17 params->tone_mapping_flag = get_bits1(gb);
206
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (params->tone_mapping_flag) {
207
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 if (get_bits_left(gb) < 28)
208 return AVERROR_INVALIDDATA;
209
210 17 params->knee_point_x =
211 17 (AVRational){get_bits(gb, 12), knee_point_den};
212 17 params->knee_point_y =
213 17 (AVRational){get_bits(gb, 12), knee_point_den};
214 17 params->num_bezier_curve_anchors = get_bits(gb, 4);
215
216
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 if (get_bits_left(gb) < (params->num_bezier_curve_anchors * 10))
217 return AVERROR_INVALIDDATA;
218
219
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 17 times.
170 for (int i = 0; i < params->num_bezier_curve_anchors; i++) {
220 153 params->bezier_curve_anchors[i] =
221 153 (AVRational){get_bits(gb, 10), bezier_anchor_den};
222 }
223 }
224
225
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
17 if (get_bits_left(gb) < 1)
226 return AVERROR_INVALIDDATA;
227 17 params->color_saturation_mapping_flag = get_bits1(gb);
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (params->color_saturation_mapping_flag) {
229 if (get_bits_left(gb) < 6)
230 return AVERROR_INVALIDDATA;
231 params->color_saturation_weight =
232 (AVRational){get_bits(gb, 6), saturation_weight_den};
233 }
234 }
235
236 17 return 0;
237 }
238
239 2 int av_dynamic_hdr_plus_to_t35(const AVDynamicHDRPlus *s, uint8_t **data, size_t *size)
240 {
241 uint8_t *buf;
242 size_t size_bits, size_bytes;
243 2 PutBitContext pbc, *pb = &pbc;
244
245
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!s)
246 return AVERROR(EINVAL);
247
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
2 if ((!data || *data) && !size)
248 return AVERROR(EINVAL);
249
250 /**
251 * Buffer size per CTA-861-H p.253-254:
252 * 48 header bits (excluded from the serialized payload)
253 * 8 bits for application_mode
254 * 2 bits for num_windows
255 * 153 bits for window geometry, for each window above 1
256 * 27 bits for targeted_system_display_maximum_luminance
257 * 1-2511 bits for targeted system display peak luminance information
258 * 82-442 bits per window for pixel distribution information
259 * 1-2511 bits for mastering display peak luminance information
260 * 1-179 bits per window for tonemapping information
261 * 1-7 bits per window for color saturation mapping information
262 * Total: 123-7249 bits, excluding trimmed header bits
263 */
264 2 size_bits = 8;
265
266 2 size_bits += 2;
267
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 for (int w = 1; w < s->num_windows; w++)
269 size_bits += 153;
270
271 2 size_bits += 27;
272
273 2 size_bits += 1;
274
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->targeted_system_display_actual_peak_luminance_flag)
275 size_bits += 10 +
276 s->num_rows_targeted_system_display_actual_peak_luminance *
277 s->num_cols_targeted_system_display_actual_peak_luminance * 4;
278
279
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int w = 0; w < s->num_windows; w++)
280 2 size_bits += 72 + s->params[w].num_distribution_maxrgb_percentiles * 24 + 10;
281
282 2 size_bits += 1;
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->mastering_display_actual_peak_luminance_flag)
284 size_bits += 10 +
285 s->num_rows_mastering_display_actual_peak_luminance *
286 s->num_cols_mastering_display_actual_peak_luminance * 4;
287
288
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int w = 0; w < s->num_windows; w++) {
289 2 size_bits += 1;
290
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (s->params[w].tone_mapping_flag)
291 2 size_bits += 28 + s->params[w].num_bezier_curve_anchors * 10;
292
293 2 size_bits += 1;
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->params[w].color_saturation_mapping_flag)
295 size_bits += 6;
296 }
297
298 2 size_bytes = (size_bits + 7) / 8;
299
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(size_bytes <= AV_HDR_PLUS_MAX_PAYLOAD_SIZE);
301
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!data) {
303 *size = size_bytes;
304 return 0;
305
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (*data) {
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (*size < size_bytes)
307 return AVERROR_BUFFER_TOO_SMALL;
308 2 buf = *data;
309 } else {
310 buf = av_malloc(size_bytes);
311 if (!buf)
312 return AVERROR(ENOMEM);
313 }
314
315 2 init_put_bits(pb, buf, size_bytes);
316
317 // application_mode is set to Application Version 1
318 2 put_bits(pb, 8, 1);
319
320 // Payload as per CTA-861-H p.253-254
321 2 put_bits(pb, 2, s->num_windows);
322
323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 for (int w = 1; w < s->num_windows; w++) {
324 put_bits(pb, 16, s->params[w].window_upper_left_corner_x.num / s->params[w].window_upper_left_corner_x.den);
325 put_bits(pb, 16, s->params[w].window_upper_left_corner_y.num / s->params[w].window_upper_left_corner_y.den);
326 put_bits(pb, 16, s->params[w].window_lower_right_corner_x.num / s->params[w].window_lower_right_corner_x.den);
327 put_bits(pb, 16, s->params[w].window_lower_right_corner_y.num / s->params[w].window_lower_right_corner_y.den);
328 put_bits(pb, 16, s->params[w].center_of_ellipse_x);
329 put_bits(pb, 16, s->params[w].center_of_ellipse_y);
330 put_bits(pb, 8, s->params[w].rotation_angle);
331 put_bits(pb, 16, s->params[w].semimajor_axis_internal_ellipse);
332 put_bits(pb, 16, s->params[w].semimajor_axis_external_ellipse);
333 put_bits(pb, 16, s->params[w].semiminor_axis_external_ellipse);
334 put_bits(pb, 1, s->params[w].overlap_process_option);
335 }
336
337 2 put_bits(pb, 27, s->targeted_system_display_maximum_luminance.num * luminance_den /
338 2 s->targeted_system_display_maximum_luminance.den);
339 2 put_bits(pb, 1, s->targeted_system_display_actual_peak_luminance_flag);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->targeted_system_display_actual_peak_luminance_flag) {
341 put_bits(pb, 5, s->num_rows_targeted_system_display_actual_peak_luminance);
342 put_bits(pb, 5, s->num_cols_targeted_system_display_actual_peak_luminance);
343 for (int i = 0; i < s->num_rows_targeted_system_display_actual_peak_luminance; i++) {
344 for (int j = 0; j < s->num_cols_targeted_system_display_actual_peak_luminance; j++)
345 put_bits(pb, 4, s->targeted_system_display_actual_peak_luminance[i][j].num * peak_luminance_den /
346 s->targeted_system_display_actual_peak_luminance[i][j].den);
347 }
348 }
349
350
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int w = 0; w < s->num_windows; w++) {
351
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (int i = 0; i < 3; i++)
352 6 put_bits(pb, 17, s->params[w].maxscl[i].num * rgb_den / s->params[w].maxscl[i].den);
353 2 put_bits(pb, 17, s->params[w].average_maxrgb.num * rgb_den / s->params[w].average_maxrgb.den);
354 2 put_bits(pb, 4, s->params[w].num_distribution_maxrgb_percentiles);
355
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 for (int i = 0; i < s->params[w].num_distribution_maxrgb_percentiles; i++) {
356 18 put_bits(pb, 7, s->params[w].distribution_maxrgb[i].percentage);
357 18 put_bits(pb, 17, s->params[w].distribution_maxrgb[i].percentile.num * rgb_den /
358 18 s->params[w].distribution_maxrgb[i].percentile.den);
359 }
360 2 put_bits(pb, 10, s->params[w].fraction_bright_pixels.num * fraction_pixel_den /
361 2 s->params[w].fraction_bright_pixels.den);
362 }
363
364 2 put_bits(pb, 1, s->mastering_display_actual_peak_luminance_flag);
365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->mastering_display_actual_peak_luminance_flag) {
366 put_bits(pb, 5, s->num_rows_mastering_display_actual_peak_luminance);
367 put_bits(pb, 5, s->num_cols_mastering_display_actual_peak_luminance);
368 for (int i = 0; i < s->num_rows_mastering_display_actual_peak_luminance; i++) {
369 for (int j = 0; j < s->num_cols_mastering_display_actual_peak_luminance; j++)
370 put_bits(pb, 4, s->mastering_display_actual_peak_luminance[i][j].num * peak_luminance_den /
371 s->mastering_display_actual_peak_luminance[i][j].den);
372 }
373 }
374
375
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int w = 0; w < s->num_windows; w++) {
376 2 put_bits(pb, 1, s->params[w].tone_mapping_flag);
377
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (s->params[w].tone_mapping_flag) {
378 2 put_bits(pb, 12, s->params[w].knee_point_x.num * knee_point_den / s->params[w].knee_point_x.den);
379 2 put_bits(pb, 12, s->params[w].knee_point_y.num * knee_point_den / s->params[w].knee_point_y.den);
380 2 put_bits(pb, 4, s->params[w].num_bezier_curve_anchors);
381
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 for (int i = 0; i < s->params[w].num_bezier_curve_anchors; i++)
382 18 put_bits(pb, 10, s->params[w].bezier_curve_anchors[i].num * bezier_anchor_den /
383 18 s->params[w].bezier_curve_anchors[i].den);
384 2 put_bits(pb, 1, s->params[w].color_saturation_mapping_flag);
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->params[w].color_saturation_mapping_flag)
386 put_bits(pb, 6, s->params[w].color_saturation_weight.num * saturation_weight_den /
387 s->params[w].color_saturation_weight.den);
388 }
389 }
390
391 2 flush_put_bits(pb);
392
393 2 *data = buf;
394
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (size)
395 2 *size = size_bytes;
396 2 return 0;
397 }
398
399 AVDynamicHDRSmpte2094App5 *av_dynamic_hdr_smpte2094_app5_alloc(size_t *size)
400 {
401 AVDynamicHDRSmpte2094App5 *smpte2094_app5 = av_mallocz(sizeof(AVDynamicHDRSmpte2094App5));
402 if (!smpte2094_app5)
403 return NULL;
404
405 if (size)
406 *size = sizeof(*smpte2094_app5);
407
408 return smpte2094_app5;
409 }
410
411 AVDynamicHDRSmpte2094App5 *av_dynamic_hdr_smpte2094_app5_create_side_data(AVFrame *frame)
412 {
413 AVFrameSideData *side_data = av_frame_new_side_data(frame,
414 AV_FRAME_DATA_DYNAMIC_HDR_SMPTE_2094_APP5,
415 sizeof(AVDynamicHDRSmpte2094App5));
416 if (!side_data)
417 return NULL;
418
419 memset(side_data->data, 0, sizeof(AVDynamicHDRSmpte2094App5));
420
421 return (AVDynamicHDRSmpte2094App5 *)side_data->data;
422 }
423
424 #define GET_BITS_OR_FAIL(var, n) \
425 do { \
426 if (get_bits_left(gb) < n) { \
427 ret = AVERROR_INVALIDDATA; \
428 goto end; \
429 } \
430 var = get_bits(gb, n); \
431 } while (0)
432
433 int av_dynamic_hdr_smpte2094_app5_from_t35(AVDynamicHDRSmpte2094App5 *s, const uint8_t *data,
434 size_t size)
435 {
436 GetBitContext gbc, *gb = &gbc;
437 int ret, reserved_zero;
438 size_t padded_size = size + AV_INPUT_BUFFER_PADDING_SIZE;
439
440 if (!s)
441 return AVERROR(ENOMEM);
442
443 uint8_t *padded_data = av_mallocz(padded_size);
444 memcpy(padded_data, data, size);
445 ret = init_get_bits8(gb, padded_data, size);
446 if (ret < 0)
447 goto end;
448
449 // Table C.1
450 GET_BITS_OR_FAIL(s->application_version, 3);
451 GET_BITS_OR_FAIL(s->minimum_application_version, 3);
452 GET_BITS_OR_FAIL(reserved_zero, 2);
453 if (reserved_zero) {
454 ret = AVERROR_INVALIDDATA;
455 goto end;
456 }
457
458 // Table C.2
459 GET_BITS_OR_FAIL(s->has_custom_hdr_reference_white_flag, 1);
460 GET_BITS_OR_FAIL(s->has_adaptive_tone_map_flag, 1);
461 GET_BITS_OR_FAIL(reserved_zero, 6);
462 if (reserved_zero) {
463 ret = AVERROR_INVALIDDATA;
464 goto end;
465 }
466 if (s->has_custom_hdr_reference_white_flag)
467 GET_BITS_OR_FAIL(s->hdr_reference_white, 16);
468 if (!s->has_adaptive_tone_map_flag) {
469 ret = 0;
470 goto end;
471 }
472
473 // Table C.3
474 GET_BITS_OR_FAIL(s->baseline_hdr_headroom, 16);
475 GET_BITS_OR_FAIL(s->use_reference_white_tone_mapping_flag, 1);
476 if (s->use_reference_white_tone_mapping_flag) {
477 GET_BITS_OR_FAIL(reserved_zero, 7);
478 ret = reserved_zero ? AVERROR_INVALIDDATA : 0;
479 goto end;
480 }
481 GET_BITS_OR_FAIL(s->num_alternate_images, 3);
482 if (s->num_alternate_images > 4) {
483 ret = AVERROR_INVALIDDATA;
484 goto end;
485 }
486 GET_BITS_OR_FAIL(s->gain_application_space_chromaticities_flag, 2);
487 GET_BITS_OR_FAIL(s->has_common_component_mix_params_flag, 1);
488 GET_BITS_OR_FAIL(s->has_common_curve_params_flag, 1);
489 if (s->gain_application_space_chromaticities_flag == 3)
490 for (int r = 0; r < 8; r++)
491 GET_BITS_OR_FAIL(s->gain_application_space_chromaticities[r], 16);
492
493 for (int a = 0; a < s->num_alternate_images; a++) {
494 GET_BITS_OR_FAIL(s->alternate_hdr_headrooms[a], 16);
495
496 // Table C.4
497 if (!a || !s->has_common_component_mix_params_flag) {
498 GET_BITS_OR_FAIL(s->component_mixing_type[a], 2);
499 if (s->component_mixing_type[a] != 3) {
500 GET_BITS_OR_FAIL(reserved_zero, 6);
501 if (reserved_zero) {
502 ret = AVERROR_INVALIDDATA;
503 goto end;
504 }
505 } else {
506 for (int k = 0; k < 6; k++)
507 GET_BITS_OR_FAIL(s->has_component_mixing_coefficient_flag[a][k], 1);
508 for (int k = 0; k < 6; k++) {
509 if (s->has_component_mixing_coefficient_flag[a][k]) {
510 GET_BITS_OR_FAIL(s->component_mixing_coefficient[a][k], 16);
511 } else {
512 s->component_mixing_coefficient[a][k] = 0;
513 }
514 }
515 }
516 } else {
517 s->component_mixing_type[a] = s->component_mixing_type[0];
518 if (s->component_mixing_type[a] == 3) {
519 for (int k = 0; k < 6; k++) {
520 s->has_component_mixing_coefficient_flag[a][k] =
521 s->has_component_mixing_coefficient_flag[0][k];
522 s->component_mixing_coefficient[a][k] = s->component_mixing_coefficient[0][k];
523 }
524 }
525 }
526
527 // Table C.5
528 if (!a || !s->has_common_curve_params_flag) {
529 GET_BITS_OR_FAIL(s->gain_curve_num_control_points_minus_1[a], 5);
530 if (s->gain_curve_num_control_points_minus_1[a] > 31) {
531 ret = AVERROR_INVALIDDATA;
532 goto end;
533 }
534 GET_BITS_OR_FAIL(s->gain_curve_use_pchip_slope_flag[a], 1);
535 GET_BITS_OR_FAIL(reserved_zero, 2);
536 if (reserved_zero) {
537 ret = AVERROR_INVALIDDATA;
538 goto end;
539 }
540 for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++) {
541 GET_BITS_OR_FAIL(s->gain_curve_control_points_x[a][c], 16);
542 }
543 } else {
544 s->gain_curve_num_control_points_minus_1[a] =
545 s->gain_curve_num_control_points_minus_1[0];
546 s->gain_curve_use_pchip_slope_flag[a] = s->gain_curve_use_pchip_slope_flag[0];
547 for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++) {
548 s->gain_curve_control_points_x[a][c] = s->gain_curve_control_points_x[0][c];
549 }
550 }
551 for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
552 GET_BITS_OR_FAIL(s->gain_curve_control_points_y[a][c], 16);
553 if (!s->gain_curve_use_pchip_slope_flag[a]) {
554 for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
555 GET_BITS_OR_FAIL(s->gain_curve_control_points_theta[a][c], 16);
556 }
557 }
558 ret = 0;
559 end:
560 av_free(padded_data);
561 return ret;
562 }
563
564 int av_dynamic_hdr_smpte2094_app5_to_t35(const AVDynamicHDRSmpte2094App5 *s, uint8_t **data,
565 size_t *size)
566 {
567 uint8_t *buf;
568 size_t size_bytes, size_bits;
569 PutBitContext pbc, *pb = &pbc;
570
571 if (!s)
572 return AVERROR(EINVAL);
573 if ((!data || *data) && !size)
574 return AVERROR(EINVAL);
575
576 if (s->application_version >= 8 || s->minimum_application_version >= 3)
577 return AVERROR_INVALIDDATA;
578 size_bits = 0;
579 size_bits += 3 + 3 + 2;
580 size_bits += 1 + 1 + 6;
581 if (s->has_custom_hdr_reference_white_flag)
582 size_bits += 16;
583 if (s->has_adaptive_tone_map_flag) {
584 size_bits += 16 + 1;
585 if (s->use_reference_white_tone_mapping_flag) {
586 size_bits += 7;
587 } else {
588 size_bits += 3 + 2 + 1 + 1;
589 if (s->gain_application_space_chromaticities_flag == 3)
590 size_bits += 16 * 8;
591 if (s->num_alternate_images > 4)
592 return AVERROR_INVALIDDATA;
593 for (int a = 0; a < s->num_alternate_images; a++) {
594 size_bits += 16;
595 if (!a || !s->has_common_component_mix_params_flag) {
596 size_bits += 2;
597 if (s->component_mixing_type[a] != 3) {
598 size_bits += 6;
599 } else {
600 size_bits += 6;
601 for (int k = 0; k < 6; k++)
602 if (s->has_component_mixing_coefficient_flag[a][k])
603 size_bits += 16;
604 }
605 }
606 if (!a || !s->has_common_curve_params_flag) {
607 size_bits += 5 + 1 + 2;
608 if (s->gain_curve_num_control_points_minus_1[a] > 31)
609 return AVERROR_INVALIDDATA;
610 size_bits += 16 * (s->gain_curve_num_control_points_minus_1[a] + 1);
611 }
612 size_bits += 16 * (s->gain_curve_num_control_points_minus_1[a] + 1);
613 if (!s->gain_curve_use_pchip_slope_flag[a])
614 size_bits += 16 * (s->gain_curve_num_control_points_minus_1[a] + 1);
615 }
616 }
617 }
618 if (size_bits % 8)
619 return AVERROR_INVALIDDATA;
620 size_bytes = size_bits >> 3;
621
622 if (!data) {
623 *size = size_bytes;
624 return 0;
625 } else if (*data) {
626 if (*size < size_bytes)
627 return AVERROR_BUFFER_TOO_SMALL;
628 buf = *data;
629 } else {
630 buf = av_malloc(size_bytes);
631 if (!buf)
632 return AVERROR(ENOMEM);
633 }
634
635 init_put_bits(pb, buf, size_bytes);
636
637 // Table C.1
638 put_bits(pb, 3, s->application_version);
639 put_bits(pb, 3, s->minimum_application_version);
640 put_bits(pb, 2, 0); // reserved_zero
641
642 // Table C.2
643 put_bits(pb, 1, s->has_custom_hdr_reference_white_flag);
644 put_bits(pb, 1, s->has_adaptive_tone_map_flag);
645 put_bits(pb, 6, 0); // reserved_zero
646
647 if (s->has_custom_hdr_reference_white_flag)
648 put_bits(pb, 16, s->hdr_reference_white);
649
650 if (s->has_adaptive_tone_map_flag) {
651 // Table C.3
652 put_bits(pb, 16, s->baseline_hdr_headroom);
653 put_bits(pb, 1, s->use_reference_white_tone_mapping_flag);
654 if (s->use_reference_white_tone_mapping_flag) {
655 put_bits(pb, 7, 0); // reserved_zero
656 } else {
657 put_bits(pb, 3, s->num_alternate_images);
658 put_bits(pb, 2, s->gain_application_space_chromaticities_flag);
659 put_bits(pb, 1, s->has_common_component_mix_params_flag);
660 put_bits(pb, 1, s->has_common_curve_params_flag);
661
662 if (s->gain_application_space_chromaticities_flag == 3)
663 for (int r = 0; r < 8; r++)
664 put_bits(pb, 16, s->gain_application_space_chromaticities[r]);
665
666 for (int a = 0; a < s->num_alternate_images; a++) {
667 put_bits(pb, 16, s->alternate_hdr_headrooms[a]);
668
669 // Table C.4
670 if (!a || !s->has_common_component_mix_params_flag) {
671 put_bits(pb, 2, s->component_mixing_type[a]);
672 if (s->component_mixing_type[a] != 3) {
673 put_bits(pb, 6, 0); // reserved_zero
674 } else {
675 for (int k = 0; k < 6; k++)
676 put_bits(pb, 1, s->has_component_mixing_coefficient_flag[a][k]);
677 for (int k = 0; k < 6; k++)
678 if (s->has_component_mixing_coefficient_flag[a][k])
679 put_bits(pb, 16, s->component_mixing_coefficient[a][k]);
680 }
681 }
682
683 // Table C.5
684 if (!a || !s->has_common_curve_params_flag) {
685 put_bits(pb, 5, s->gain_curve_num_control_points_minus_1[a]);
686 put_bits(pb, 1, s->gain_curve_use_pchip_slope_flag[a]);
687 put_bits(pb, 2, 0); // reserved_zero
688 for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
689 put_bits(pb, 16, s->gain_curve_control_points_x[a][c]);
690 }
691 for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
692 put_bits(pb, 16, s->gain_curve_control_points_y[a][c]);
693 if (!s->gain_curve_use_pchip_slope_flag[a]) {
694 for (int c = 0; c <= s->gain_curve_num_control_points_minus_1[a]; c++)
695 put_bits(pb, 16, s->gain_curve_control_points_theta[a][c]);
696 }
697 }
698 }
699 }
700
701 flush_put_bits(pb);
702
703 *data = buf;
704 if (size)
705 *size = size_bytes;
706 return 0;
707 }
708