FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/dovi_meta.h
Date: 2024-04-26 14:42:52
Exec Total Coverage
Lines: 9 9 100.0%
Functions: 4 4 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2020 Vacing Fang <vacingfang@tencent.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 /**
22 * @file
23 * DOVI configuration
24 */
25
26
27 #ifndef AVUTIL_DOVI_META_H
28 #define AVUTIL_DOVI_META_H
29
30 #include <stdint.h>
31 #include <stddef.h>
32
33 #include "rational.h"
34 #include "csp.h"
35
36 /*
37 * DOVI configuration
38 * ref: dolby-vision-bitstreams-within-the-iso-base-media-file-format-v2.1.2
39 dolby-vision-bitstreams-in-mpeg-2-transport-stream-multiplex-v1.2
40 * @code
41 * uint8_t dv_version_major, the major version number that the stream complies with
42 * uint8_t dv_version_minor, the minor version number that the stream complies with
43 * uint8_t dv_profile, the Dolby Vision profile
44 * uint8_t dv_level, the Dolby Vision level
45 * uint8_t rpu_present_flag
46 * uint8_t el_present_flag
47 * uint8_t bl_present_flag
48 * uint8_t dv_bl_signal_compatibility_id
49 * @endcode
50 *
51 * @note The struct must be allocated with av_dovi_alloc() and
52 * its size is not a part of the public ABI.
53 */
54 typedef struct AVDOVIDecoderConfigurationRecord {
55 uint8_t dv_version_major;
56 uint8_t dv_version_minor;
57 uint8_t dv_profile;
58 uint8_t dv_level;
59 uint8_t rpu_present_flag;
60 uint8_t el_present_flag;
61 uint8_t bl_present_flag;
62 uint8_t dv_bl_signal_compatibility_id;
63 } AVDOVIDecoderConfigurationRecord;
64
65 /**
66 * Allocate a AVDOVIDecoderConfigurationRecord structure and initialize its
67 * fields to default values.
68 *
69 * @return the newly allocated struct or NULL on failure
70 */
71 AVDOVIDecoderConfigurationRecord *av_dovi_alloc(size_t *size);
72
73 /**
74 * Dolby Vision RPU data header.
75 *
76 * @note sizeof(AVDOVIRpuDataHeader) is not part of the public ABI.
77 */
78 typedef struct AVDOVIRpuDataHeader {
79 uint8_t rpu_type;
80 uint16_t rpu_format;
81 uint8_t vdr_rpu_profile;
82 uint8_t vdr_rpu_level;
83 uint8_t chroma_resampling_explicit_filter_flag;
84 uint8_t coef_data_type; /* informative, lavc always converts to fixed */
85 uint8_t coef_log2_denom;
86 uint8_t vdr_rpu_normalized_idc;
87 uint8_t bl_video_full_range_flag;
88 uint8_t bl_bit_depth; /* [8, 16] */
89 uint8_t el_bit_depth; /* [8, 16] */
90 uint8_t vdr_bit_depth; /* [8, 16] */
91 uint8_t spatial_resampling_filter_flag;
92 uint8_t el_spatial_resampling_filter_flag;
93 uint8_t disable_residual_flag;
94 } AVDOVIRpuDataHeader;
95
96 enum AVDOVIMappingMethod {
97 AV_DOVI_MAPPING_POLYNOMIAL = 0,
98 AV_DOVI_MAPPING_MMR = 1,
99 };
100
101 /**
102 * Coefficients of a piece-wise function. The pieces of the function span the
103 * value ranges between two adjacent pivot values.
104 */
105 #define AV_DOVI_MAX_PIECES 8
106 typedef struct AVDOVIReshapingCurve {
107 uint8_t num_pivots; /* [2, 9] */
108 uint16_t pivots[AV_DOVI_MAX_PIECES + 1]; /* sorted ascending */
109 enum AVDOVIMappingMethod mapping_idc[AV_DOVI_MAX_PIECES];
110 /* AV_DOVI_MAPPING_POLYNOMIAL */
111 uint8_t poly_order[AV_DOVI_MAX_PIECES]; /* [1, 2] */
112 int64_t poly_coef[AV_DOVI_MAX_PIECES][3]; /* x^0, x^1, x^2 */
113 /* AV_DOVI_MAPPING_MMR */
114 uint8_t mmr_order[AV_DOVI_MAX_PIECES]; /* [1, 3] */
115 int64_t mmr_constant[AV_DOVI_MAX_PIECES];
116 int64_t mmr_coef[AV_DOVI_MAX_PIECES][3/* order - 1 */][7];
117 } AVDOVIReshapingCurve;
118
119 enum AVDOVINLQMethod {
120 AV_DOVI_NLQ_NONE = -1,
121 AV_DOVI_NLQ_LINEAR_DZ = 0,
122 };
123
124 /**
125 * Coefficients of the non-linear inverse quantization. For the interpretation
126 * of these, see ETSI GS CCM 001.
127 */
128 typedef struct AVDOVINLQParams {
129 uint16_t nlq_offset;
130 uint64_t vdr_in_max;
131 /* AV_DOVI_NLQ_LINEAR_DZ */
132 uint64_t linear_deadzone_slope;
133 uint64_t linear_deadzone_threshold;
134 } AVDOVINLQParams;
135
136 /**
137 * Dolby Vision RPU data mapping parameters.
138 *
139 * @note sizeof(AVDOVIDataMapping) is not part of the public ABI.
140 */
141 typedef struct AVDOVIDataMapping {
142 uint8_t vdr_rpu_id;
143 uint8_t mapping_color_space;
144 uint8_t mapping_chroma_format_idc;
145 AVDOVIReshapingCurve curves[3]; /* per component */
146
147 /* Non-linear inverse quantization */
148 enum AVDOVINLQMethod nlq_method_idc;
149 uint32_t num_x_partitions;
150 uint32_t num_y_partitions;
151 AVDOVINLQParams nlq[3]; /* per component */
152 uint16_t nlq_pivots[2];
153 } AVDOVIDataMapping;
154
155 /**
156 * Dolby Vision RPU colorspace metadata parameters.
157 *
158 * @note sizeof(AVDOVIColorMetadata) is not part of the public ABI.
159 */
160 typedef struct AVDOVIColorMetadata {
161 uint8_t dm_metadata_id;
162 uint8_t scene_refresh_flag;
163
164 /**
165 * Coefficients of the custom Dolby Vision IPT-PQ matrices. These are to be
166 * used instead of the matrices indicated by the frame's colorspace tags.
167 * The output of rgb_to_lms_matrix is to be fed into a BT.2020 LMS->RGB
168 * matrix based on a Hunt-Pointer-Estevez transform, but without any
169 * crosstalk. (See the definition of the ICtCp colorspace for more
170 * information.)
171 */
172 AVRational ycc_to_rgb_matrix[9]; /* before PQ linearization */
173 AVRational ycc_to_rgb_offset[3]; /* input offset of neutral value */
174 AVRational rgb_to_lms_matrix[9]; /* after PQ linearization */
175
176 /**
177 * Extra signal metadata (see Dolby patents for more info).
178 */
179 uint16_t signal_eotf;
180 uint16_t signal_eotf_param0;
181 uint16_t signal_eotf_param1;
182 uint32_t signal_eotf_param2;
183 uint8_t signal_bit_depth;
184 uint8_t signal_color_space;
185 uint8_t signal_chroma_format;
186 uint8_t signal_full_range_flag; /* [0, 3] */
187 uint16_t source_min_pq;
188 uint16_t source_max_pq;
189 uint16_t source_diagonal;
190 } AVDOVIColorMetadata;
191
192 typedef struct AVDOVIDmLevel1 {
193 /* Per-frame brightness metadata */
194 uint16_t min_pq;
195 uint16_t max_pq;
196 uint16_t avg_pq;
197 } AVDOVIDmLevel1;
198
199 typedef struct AVDOVIDmLevel2 {
200 /* Usually derived from level 8 (at different levels) */
201 uint16_t target_max_pq;
202 uint16_t trim_slope;
203 uint16_t trim_offset;
204 uint16_t trim_power;
205 uint16_t trim_chroma_weight;
206 uint16_t trim_saturation_gain;
207 int16_t ms_weight;
208 } AVDOVIDmLevel2;
209
210 typedef struct AVDOVIDmLevel3 {
211 uint16_t min_pq_offset;
212 uint16_t max_pq_offset;
213 uint16_t avg_pq_offset;
214 } AVDOVIDmLevel3;
215
216 typedef struct AVDOVIDmLevel4 {
217 uint16_t anchor_pq;
218 uint16_t anchor_power;
219 } AVDOVIDmLevel4;
220
221 typedef struct AVDOVIDmLevel5 {
222 /* Active area definition */
223 uint16_t left_offset;
224 uint16_t right_offset;
225 uint16_t top_offset;
226 uint16_t bottom_offset;
227 } AVDOVIDmLevel5;
228
229 typedef struct AVDOVIDmLevel6 {
230 /* Static HDR10 metadata */
231 uint16_t max_luminance;
232 uint16_t min_luminance;
233 uint16_t max_cll;
234 uint16_t max_fall;
235 } AVDOVIDmLevel6;
236
237 typedef struct AVDOVIDmLevel8 {
238 /* Extended version of level 2 */
239 uint8_t target_display_index;
240 uint16_t trim_slope;
241 uint16_t trim_offset;
242 uint16_t trim_power;
243 uint16_t trim_chroma_weight;
244 uint16_t trim_saturation_gain;
245 uint16_t ms_weight;
246 uint16_t target_mid_contrast;
247 uint16_t clip_trim;
248 uint8_t saturation_vector_field[6];
249 uint8_t hue_vector_field[6];
250 } AVDOVIDmLevel8;
251
252 typedef struct AVDOVIDmLevel9 {
253 /* Source display characteristics */
254 uint8_t source_primary_index;
255 AVColorPrimariesDesc source_display_primaries;
256 } AVDOVIDmLevel9;
257
258 typedef struct AVDOVIDmLevel10 {
259 /* Target display characteristics */
260 uint8_t target_display_index;
261 uint16_t target_max_pq;
262 uint16_t target_min_pq;
263 uint8_t target_primary_index;
264 AVColorPrimariesDesc target_display_primaries;
265 } AVDOVIDmLevel10;
266
267 typedef struct AVDOVIDmLevel11 {
268 uint8_t content_type;
269 uint8_t whitepoint;
270 uint8_t reference_mode_flag;
271 uint8_t sharpness;
272 uint8_t noise_reduction;
273 uint8_t mpeg_noise_reduction;
274 uint8_t frame_rate_conversion;
275 uint8_t brightness;
276 uint8_t color;
277 } AVDOVIDmLevel11;
278
279 typedef struct AVDOVIDmLevel254 {
280 /* DMv2 info block, always present in samples with DMv2 metadata */
281 uint8_t dm_mode;
282 uint8_t dm_version_index;
283 } AVDOVIDmLevel254;
284
285 typedef struct AVDOVIDmLevel255 {
286 /* Debug block, not really used in samples */
287 uint8_t dm_run_mode;
288 uint8_t dm_run_version;
289 uint8_t dm_debug[4];
290 } AVDOVIDmLevel255;
291
292 /**
293 * Dolby Vision metadata extension block.
294 *
295 * @note sizeof(AVDOVIDmData) is not part of the public API.
296 */
297 typedef struct AVDOVIDmData {
298 uint8_t level; /* [1, 255] */
299 union {
300 AVDOVIDmLevel1 l1;
301 AVDOVIDmLevel2 l2; /* may appear multiple times */
302 AVDOVIDmLevel3 l3;
303 AVDOVIDmLevel4 l4;
304 AVDOVIDmLevel5 l5;
305 AVDOVIDmLevel6 l6;
306 /* level 7 is currently unused */
307 AVDOVIDmLevel8 l8; /* may appear multiple times */
308 AVDOVIDmLevel9 l9;
309 AVDOVIDmLevel10 l10; /* may appear multiple times */
310 AVDOVIDmLevel11 l11;
311 AVDOVIDmLevel254 l254;
312 AVDOVIDmLevel255 l255;
313 };
314 } AVDOVIDmData;
315
316 /**
317 * Combined struct representing a combination of header, mapping and color
318 * metadata, for attaching to frames as side data.
319 *
320 * @note The struct must be allocated with av_dovi_metadata_alloc() and
321 * its size is not a part of the public ABI.
322 */
323
324 typedef struct AVDOVIMetadata {
325 /**
326 * Offset in bytes from the beginning of this structure at which the
327 * respective structs start.
328 */
329 size_t header_offset; /* AVDOVIRpuDataHeader */
330 size_t mapping_offset; /* AVDOVIDataMapping */
331 size_t color_offset; /* AVDOVIColorMetadata */
332
333 size_t ext_block_offset; /* offset to start of ext blocks array */
334 size_t ext_block_size; /* size per element */
335 int num_ext_blocks; /* number of extension blocks */
336
337 /* static limit on num_ext_blocks, derived from bitstream limitations */
338 #define AV_DOVI_MAX_EXT_BLOCKS 32
339 } AVDOVIMetadata;
340
341 static av_always_inline AVDOVIRpuDataHeader *
342 4 av_dovi_get_header(const AVDOVIMetadata *data)
343 {
344 4 return (AVDOVIRpuDataHeader *)((uint8_t *) data + data->header_offset);
345 }
346
347 static av_always_inline AVDOVIDataMapping *
348 4 av_dovi_get_mapping(const AVDOVIMetadata *data)
349 {
350 4 return (AVDOVIDataMapping *)((uint8_t *) data + data->mapping_offset);
351 }
352
353 static av_always_inline AVDOVIColorMetadata *
354 4 av_dovi_get_color(const AVDOVIMetadata *data)
355 {
356 4 return (AVDOVIColorMetadata *)((uint8_t *) data + data->color_offset);
357 }
358
359 static av_always_inline AVDOVIDmData *
360 2 av_dovi_get_ext(const AVDOVIMetadata *data, int index)
361 {
362 4 return (AVDOVIDmData *)((uint8_t *) data + data->ext_block_offset +
363 2 data->ext_block_size * index);
364 }
365
366 /**
367 * Find an extension block with a given level, or NULL. In the case of
368 * multiple extension blocks, only the first is returned.
369 */
370 AVDOVIDmData *av_dovi_find_level(const AVDOVIMetadata *data, uint8_t level);
371
372 /**
373 * Allocate an AVDOVIMetadata structure and initialize its
374 * fields to default values.
375 *
376 * @param size If this parameter is non-NULL, the size in bytes of the
377 * allocated struct will be written here on success
378 *
379 * @return the newly allocated struct or NULL on failure
380 */
381 AVDOVIMetadata *av_dovi_metadata_alloc(size_t *size);
382
383 #endif /* AVUTIL_DOVI_META_H */
384