FFmpeg
dynamic_hdr10_plus.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #include "dynamic_hdr10_plus.h"
20 #include "get_bits.h"
21 
22 static const int64_t luminance_den = 1;
23 static const int32_t peak_luminance_den = 15;
24 static const int64_t rgb_den = 100000;
25 static const int32_t fraction_pixel_den = 1000;
26 static const int32_t knee_point_den = 4095;
27 static const int32_t bezier_anchor_den = 1023;
28 static const int32_t saturation_weight_den = 8;
29 
31  int size)
32 {
33  GetBitContext gbc, *gb = &gbc;
34  int ret;
35 
36  if (!s)
37  return AVERROR(ENOMEM);
38 
39  ret = init_get_bits8(gb, data, size);
40  if (ret < 0)
41  return ret;
42 
43  if (get_bits_left(gb) < 10)
44  return AVERROR_INVALIDDATA;
45 
46  s->application_version = get_bits(gb, 8);
47  s->num_windows = get_bits(gb, 2);
48 
49  if (s->num_windows < 1 || s->num_windows > 3) {
50  return AVERROR_INVALIDDATA;
51  }
52 
53  if (get_bits_left(gb) < ((19 * 8 + 1) * (s->num_windows - 1)))
54  return AVERROR_INVALIDDATA;
55 
56  for (int w = 1; w < s->num_windows; w++) {
57  // The corners are set to absolute coordinates here. They should be
58  // converted to the relative coordinates (in [0, 1]) in the decoder.
59  AVHDRPlusColorTransformParams *params = &s->params[w];
61  (AVRational){get_bits(gb, 16), 1};
63  (AVRational){get_bits(gb, 16), 1};
65  (AVRational){get_bits(gb, 16), 1};
67  (AVRational){get_bits(gb, 16), 1};
68 
69  params->center_of_ellipse_x = get_bits(gb, 16);
70  params->center_of_ellipse_y = get_bits(gb, 16);
71  params->rotation_angle = get_bits(gb, 8);
72  params->semimajor_axis_internal_ellipse = get_bits(gb, 16);
73  params->semimajor_axis_external_ellipse = get_bits(gb, 16);
74  params->semiminor_axis_external_ellipse = get_bits(gb, 16);
75  params->overlap_process_option = get_bits1(gb);
76  }
77 
78  if (get_bits_left(gb) < 28)
79  return AVERROR_INVALIDDATA;
80 
81  s->targeted_system_display_maximum_luminance =
83  s->targeted_system_display_actual_peak_luminance_flag = get_bits1(gb);
84 
85  if (s->targeted_system_display_actual_peak_luminance_flag) {
86  int rows, cols;
87  if (get_bits_left(gb) < 10)
88  return AVERROR_INVALIDDATA;
89  rows = get_bits(gb, 5);
90  cols = get_bits(gb, 5);
91  if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) {
92  return AVERROR_INVALIDDATA;
93  }
94  s->num_rows_targeted_system_display_actual_peak_luminance = rows;
95  s->num_cols_targeted_system_display_actual_peak_luminance = cols;
96 
97  if (get_bits_left(gb) < (rows * cols * 4))
98  return AVERROR_INVALIDDATA;
99 
100  for (int i = 0; i < rows; i++) {
101  for (int j = 0; j < cols; j++) {
102  s->targeted_system_display_actual_peak_luminance[i][j] =
104  }
105  }
106  }
107  for (int w = 0; w < s->num_windows; w++) {
108  AVHDRPlusColorTransformParams *params = &s->params[w];
109  if (get_bits_left(gb) < (3 * 17 + 17 + 4))
110  return AVERROR_INVALIDDATA;
111 
112  for (int i = 0; i < 3; i++) {
113  params->maxscl[i] =
114  (AVRational){get_bits(gb, 17), rgb_den};
115  }
116  params->average_maxrgb =
117  (AVRational){get_bits(gb, 17), rgb_den};
119 
120  if (get_bits_left(gb) <
122  return AVERROR_INVALIDDATA;
123 
124  for (int i = 0; i < params->num_distribution_maxrgb_percentiles; i++) {
125  params->distribution_maxrgb[i].percentage = get_bits(gb, 7);
126  params->distribution_maxrgb[i].percentile =
127  (AVRational){get_bits(gb, 17), rgb_den};
128  }
129 
130  if (get_bits_left(gb) < 10)
131  return AVERROR_INVALIDDATA;
132 
134  }
135  if (get_bits_left(gb) < 1)
136  return AVERROR_INVALIDDATA;
137  s->mastering_display_actual_peak_luminance_flag = get_bits1(gb);
138  if (s->mastering_display_actual_peak_luminance_flag) {
139  int rows, cols;
140  if (get_bits_left(gb) < 10)
141  return AVERROR_INVALIDDATA;
142  rows = get_bits(gb, 5);
143  cols = get_bits(gb, 5);
144  if (((rows < 2) || (rows > 25)) || ((cols < 2) || (cols > 25))) {
145  return AVERROR_INVALIDDATA;
146  }
147  s->num_rows_mastering_display_actual_peak_luminance = rows;
148  s->num_cols_mastering_display_actual_peak_luminance = cols;
149 
150  if (get_bits_left(gb) < (rows * cols * 4))
151  return AVERROR_INVALIDDATA;
152 
153  for (int i = 0; i < rows; i++) {
154  for (int j = 0; j < cols; j++) {
155  s->mastering_display_actual_peak_luminance[i][j] =
157  }
158  }
159  }
160 
161  for (int w = 0; w < s->num_windows; w++) {
162  AVHDRPlusColorTransformParams *params = &s->params[w];
163  if (get_bits_left(gb) < 1)
164  return AVERROR_INVALIDDATA;
165 
166  params->tone_mapping_flag = get_bits1(gb);
167  if (params->tone_mapping_flag) {
168  if (get_bits_left(gb) < 28)
169  return AVERROR_INVALIDDATA;
170 
171  params->knee_point_x =
172  (AVRational){get_bits(gb, 12), knee_point_den};
173  params->knee_point_y =
174  (AVRational){get_bits(gb, 12), knee_point_den};
175  params->num_bezier_curve_anchors = get_bits(gb, 4);
176 
177  if (get_bits_left(gb) < (params->num_bezier_curve_anchors * 10))
178  return AVERROR_INVALIDDATA;
179 
180  for (int i = 0; i < params->num_bezier_curve_anchors; i++) {
181  params->bezier_curve_anchors[i] =
183  }
184  }
185 
186  if (get_bits_left(gb) < 1)
187  return AVERROR_INVALIDDATA;
189  if (params->color_saturation_mapping_flag) {
190  if (get_bits_left(gb) < 6)
191  return AVERROR_INVALIDDATA;
192  params->color_saturation_weight =
194  }
195  }
196 
197  return 0;
198 }
AVHDRPlusColorTransformParams::average_maxrgb
AVRational average_maxrgb
The average of linearized maxRGB values in the processing window in the scene.
Definition: hdr_dynamic_metadata.h:164
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:850
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVHDRPlusColorTransformParams::rotation_angle
uint8_t rotation_angle
The clockwise rotation angle in degree of arc with respect to the positive direction of the x-axis of...
Definition: hdr_dynamic_metadata.h:118
AVHDRPlusPercentile::percentile
AVRational percentile
The linearized maxRGB value at a specific percentile in the processing window in the scene.
Definition: hdr_dynamic_metadata.h:52
get_bits_long
static unsigned int get_bits_long(GetBitContext *s, int n)
Read 0-32 bits.
Definition: get_bits.h:547
AVHDRPlusColorTransformParams::semimajor_axis_external_ellipse
uint16_t semimajor_axis_external_ellipse
The semi-major axis value of the external ellipse of the elliptical pixel selector in amount of pixel...
Definition: hdr_dynamic_metadata.h:134
AVHDRPlusColorTransformParams
Color transform parameters at a processing window in a dynamic metadata for SMPTE 2094-40.
Definition: hdr_dynamic_metadata.h:59
w
uint8_t w
Definition: llviddspenc.c:38
data
const char data[16]
Definition: mxf.c:143
AVHDRPlusColorTransformParams::tone_mapping_flag
uint8_t tone_mapping_flag
This flag indicates that the metadata for the tone mapping function in the processing window is prese...
Definition: hdr_dynamic_metadata.h:189
AVHDRPlusColorTransformParams::distribution_maxrgb
AVHDRPlusPercentile distribution_maxrgb[15]
The linearized maxRGB values at given percentiles in the processing window in the scene.
Definition: hdr_dynamic_metadata.h:176
AVHDRPlusColorTransformParams::knee_point_x
AVRational knee_point_x
The x coordinate of the separation point between the linear part and the curved part of the tone mapp...
Definition: hdr_dynamic_metadata.h:196
rgb_den
static const int64_t rgb_den
Definition: dynamic_hdr10_plus.c:24
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:380
AVHDRPlusColorTransformParams::color_saturation_mapping_flag
uint8_t color_saturation_mapping_flag
This flag shall be equal to 0 in bitstreams conforming to this version of this Specification.
Definition: hdr_dynamic_metadata.h:222
AVHDRPlusColorTransformParams::center_of_ellipse_x
uint16_t center_of_ellipse_x
The x coordinate of the center position of the concentric internal and external ellipses of the ellip...
Definition: hdr_dynamic_metadata.h:102
GetBitContext
Definition: get_bits.h:62
fraction_pixel_den
static const int32_t fraction_pixel_den
Definition: dynamic_hdr10_plus.c:25
AVHDRPlusColorTransformParams::knee_point_y
AVRational knee_point_y
The y coordinate of the separation point between the linear part and the curved part of the tone mapp...
Definition: hdr_dynamic_metadata.h:203
AVHDRPlusColorTransformParams::num_bezier_curve_anchors
uint8_t num_bezier_curve_anchors
The number of the intermediate anchor parameters of the tone mapping function in the processing windo...
Definition: hdr_dynamic_metadata.h:209
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:678
s
#define s(width, name)
Definition: cbs_vp9.c:257
AVHDRPlusColorTransformParams::semiminor_axis_external_ellipse
uint16_t semiminor_axis_external_ellipse
The semi-minor axis value of the external ellipse of the elliptical pixel selector in amount of pixel...
Definition: hdr_dynamic_metadata.h:141
AVHDRPlusColorTransformParams::window_upper_left_corner_y
AVRational window_upper_left_corner_y
The relative y coordinate of the top left pixel of the processing window.
Definition: hdr_dynamic_metadata.h:76
AVHDRPlusColorTransformParams::window_lower_right_corner_x
AVRational window_lower_right_corner_x
The relative x coordinate of the bottom right pixel of the processing window.
Definition: hdr_dynamic_metadata.h:85
get_bits.h
AVHDRPlusPercentile::percentage
uint8_t percentage
The percentage value corresponding to a specific percentile linearized RGB value in the processing wi...
Definition: hdr_dynamic_metadata.h:45
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:499
dynamic_hdr10_plus.h
AVHDRPlusColorTransformParams::fraction_bright_pixels
AVRational fraction_bright_pixels
The fraction of selected pixels in the image that contains the brightest pixel in the scene.
Definition: hdr_dynamic_metadata.h:183
AVHDRPlusColorTransformParams::color_saturation_weight
AVRational color_saturation_weight
The color saturation gain in the processing window in the scene.
Definition: hdr_dynamic_metadata.h:229
knee_point_den
static const int32_t knee_point_den
Definition: dynamic_hdr10_plus.c:26
peak_luminance_den
static const int32_t peak_luminance_den
Definition: dynamic_hdr10_plus.c:23
size
int size
Definition: twinvq_data.h:10344
AVHDRPlusColorTransformParams::window_lower_right_corner_y
AVRational window_lower_right_corner_y
The relative y coordinate of the bottom right pixel of the processing window.
Definition: hdr_dynamic_metadata.h:94
luminance_den
static const int64_t luminance_den
Definition: dynamic_hdr10_plus.c:22
AVHDRPlusColorTransformParams::window_upper_left_corner_x
AVRational window_upper_left_corner_x
The relative x coordinate of the top left pixel of the processing window.
Definition: hdr_dynamic_metadata.h:67
AVHDRPlusColorTransformParams::semimajor_axis_internal_ellipse
uint16_t semimajor_axis_internal_ellipse
The semi-major axis value of the internal ellipse of the elliptical pixel selector in amount of pixel...
Definition: hdr_dynamic_metadata.h:125
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVHDRPlusColorTransformParams::overlap_process_option
enum AVHDRPlusOverlapProcessOption overlap_process_option
Overlap process option indicates one of the two methods of combining rendered pixels in the processin...
Definition: hdr_dynamic_metadata.h:149
AVDynamicHDRPlus
This struct represents dynamic metadata for color volume transform - application 4 of SMPTE 2094-40:2...
Definition: hdr_dynamic_metadata.h:243
ret
ret
Definition: filter_design.txt:187
AVHDRPlusColorTransformParams::num_distribution_maxrgb_percentiles
uint8_t num_distribution_maxrgb_percentiles
The number of linearized maxRGB values at given percentiles in the processing window in the scene.
Definition: hdr_dynamic_metadata.h:170
AVHDRPlusColorTransformParams::maxscl
AVRational maxscl[3]
The maximum of the color components of linearized RGB values in the processing window in the scene.
Definition: hdr_dynamic_metadata.h:157
ff_parse_itu_t_t35_to_dynamic_hdr10_plus
int ff_parse_itu_t_t35_to_dynamic_hdr10_plus(AVDynamicHDRPlus *s, const uint8_t *data, int size)
Parse the user data registered ITU-T T.35 to AVbuffer (AVDynamicHDRPlus).
Definition: dynamic_hdr10_plus.c:30
AVHDRPlusColorTransformParams::center_of_ellipse_y
uint16_t center_of_ellipse_y
The y coordinate of the center position of the concentric internal and external ellipses of the ellip...
Definition: hdr_dynamic_metadata.h:110
int32_t
int32_t
Definition: audioconvert.c:56
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
saturation_weight_den
static const int32_t saturation_weight_den
Definition: dynamic_hdr10_plus.c:28
bezier_anchor_den
static const int32_t bezier_anchor_den
Definition: dynamic_hdr10_plus.c:27
AVHDRPlusColorTransformParams::bezier_curve_anchors
AVRational bezier_curve_anchors[15]
The intermediate anchor parameters of the tone mapping function in the processing window in the scene...
Definition: hdr_dynamic_metadata.h:216