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
27
35
37 {
39 if (!hdr_plus)
41
43 *
size =
sizeof(*hdr_plus);
44
45 return hdr_plus;
46 }
47
49 {
53 if (!side_data)
55
57
59 }
60
63 {
67
70
73
75 // Zero-initialize the buffer padding to avoid overreads into uninitialized data.
77
81
84
87
88 if (
s->num_windows < 1 ||
s->num_windows > 3) {
90 }
91
94
95 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.
107
115 }
116
119
120 s->targeted_system_display_maximum_luminance =
122 s->targeted_system_display_actual_peak_luminance_flag =
get_bits1(gb);
123
124 if (
s->targeted_system_display_actual_peak_luminance_flag) {
125 int rows, cols;
130 if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) {
132 }
133 s->num_rows_targeted_system_display_actual_peak_luminance = rows;
134 s->num_cols_targeted_system_display_actual_peak_luminance = cols;
135
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] =
143 }
144 }
145 }
146 for (
int w = 0;
w <
s->num_windows;
w++) {
150
151 for (
int i = 0;
i < 3;
i++) {
154 }
158
162
167 }
168
171
173 }
176 s->mastering_display_actual_peak_luminance_flag =
get_bits1(gb);
177 if (
s->mastering_display_actual_peak_luminance_flag) {
178 int rows, cols;
183 if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) {
185 }
186 s->num_rows_mastering_display_actual_peak_luminance = rows;
187 s->num_cols_mastering_display_actual_peak_luminance = cols;
188
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] =
196 }
197 }
198 }
199
200 for (
int w = 0;
w <
s->num_windows;
w++) {
204
209
215
218
222 }
223 }
224
233 }
234 }
235
236 return 0;
237 }
238
240 {
241 uint8_t *buf;
242 size_t size_bits, size_bytes;
244
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 size_bits = 8;
265
266 size_bits += 2;
267
268 for (
int w = 1;
w <
s->num_windows;
w++)
269 size_bits += 153;
270
271 size_bits += 27;
272
273 size_bits += 1;
274 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 for (
int w = 0;
w <
s->num_windows;
w++)
280 size_bits += 72 +
s->params[
w].num_distribution_maxrgb_percentiles * 24 + 10;
281
282 size_bits += 1;
283 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 for (
int w = 0;
w <
s->num_windows;
w++) {
289 size_bits += 1;
290 if (
s->params[
w].tone_mapping_flag)
291 size_bits += 28 +
s->params[
w].num_bezier_curve_anchors * 10;
292
293 size_bits += 1;
294 if (
s->params[
w].color_saturation_mapping_flag)
295 size_bits += 6;
296 }
297
298 size_bytes = (size_bits + 7) / 8;
299
301
304 return 0;
306 if (*
size < size_bytes)
309 } else {
311 if (!buf)
313 }
314
316
317 // application_mode is set to Application Version 1
319
320 // Payload as per CTA-861-H p.253-254
322
323 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);
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
338 s->targeted_system_display_maximum_luminance.den);
339 put_bits(pb, 1,
s->targeted_system_display_actual_peak_luminance_flag);
340 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++)
346 s->targeted_system_display_actual_peak_luminance[
i][j].den);
347 }
348 }
349
350 for (
int w = 0;
w <
s->num_windows;
w++) {
351 for (
int i = 0;
i < 3;
i++)
353 put_bits(pb, 17,
s->params[
w].average_maxrgb.num *
rgb_den /
s->params[
w].average_maxrgb.den);
354 put_bits(pb, 4,
s->params[
w].num_distribution_maxrgb_percentiles);
355 for (
int i = 0;
i <
s->params[
w].num_distribution_maxrgb_percentiles;
i++) {
356 put_bits(pb, 7,
s->params[
w].distribution_maxrgb[
i].percentage);
358 s->params[
w].distribution_maxrgb[
i].percentile.den);
359 }
361 s->params[
w].fraction_bright_pixels.den);
362 }
363
364 put_bits(pb, 1,
s->mastering_display_actual_peak_luminance_flag);
365 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++)
371 s->mastering_display_actual_peak_luminance[
i][j].den);
372 }
373 }
374
375 for (
int w = 0;
w <
s->num_windows;
w++) {
376 put_bits(pb, 1,
s->params[
w].tone_mapping_flag);
377 if (
s->params[
w].tone_mapping_flag) {
380 put_bits(pb, 4,
s->params[
w].num_bezier_curve_anchors);
381 for (
int i = 0;
i <
s->params[
w].num_bezier_curve_anchors;
i++)
383 s->params[
w].bezier_curve_anchors[
i].den);
384 put_bits(pb, 1,
s->params[
w].color_saturation_mapping_flag);
385 if (
s->params[
w].color_saturation_mapping_flag)
387 s->params[
w].color_saturation_weight.den);
388 }
389 }
390
392
396 return 0;
397 }