FFmpeg coverage


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