FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dovi_rpudec.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 213 468 45.5%
Functions: 6 10 60.0%
Branches: 113 299 37.8%

Line Branch Exec Source
1 /*
2 * Dolby Vision RPU decoder
3 *
4 * Copyright (C) 2021 Jan Ekström
5 * Copyright (C) 2021-2024 Niklas Haas
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "libavutil/mem.h"
25 #include "libavutil/crc.h"
26
27 #include "avcodec.h"
28 #include "dovi_rpu.h"
29 #include "golomb.h"
30 #include "get_bits.h"
31 #include "libavutil/refstruct.h"
32
33 10284 int ff_dovi_get_metadata(DOVIContext *s, AVDOVIMetadata **out_metadata)
34 {
35 AVDOVIMetadata *dovi;
36 size_t dovi_size;
37
38
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10282 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
10284 if (!s->mapping || !s->color)
39 10282 return 0; /* incomplete dovi metadata */
40
41 2 dovi = av_dovi_metadata_alloc(&dovi_size);
42
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!dovi)
43 return AVERROR(ENOMEM);
44
45 /* Copy only the parts of these structs known to us at compiler-time. */
46 #define COPY(t, a, b, last) memcpy(a, b, offsetof(t, last) + sizeof((b)->last))
47 2 COPY(AVDOVIRpuDataHeader, av_dovi_get_header(dovi), &s->header, ext_mapping_idc_5_7);
48 2 COPY(AVDOVIDataMapping, av_dovi_get_mapping(dovi), s->mapping, nlq_pivots);
49 2 COPY(AVDOVIColorMetadata, av_dovi_get_color(dovi), s->color, source_diagonal);
50
51
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (s->ext_blocks) {
52 2 const DOVIExt *ext = s->ext_blocks;
53 2 size_t ext_sz = FFMIN(sizeof(AVDOVIDmData), dovi->ext_block_size);
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 for (int i = 0; i < ext->num_static; i++)
55 memcpy(av_dovi_get_ext(dovi, dovi->num_ext_blocks++), &ext->dm_static[i], ext_sz);
56
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (int i = 0; i < ext->num_dynamic; i++)
57 2 memcpy(av_dovi_get_ext(dovi, dovi->num_ext_blocks++), &ext->dm_dynamic[i], ext_sz);
58 }
59
60 2 *out_metadata = dovi;
61 2 return dovi_size;
62 }
63
64 10284 int ff_dovi_attach_side_data(DOVIContext *s, AVFrame *frame)
65 {
66 AVFrameSideData *sd;
67 AVDOVIMetadata *dovi;
68 AVBufferRef *buf;
69 int size;
70
71 10284 size = ff_dovi_get_metadata(s, &dovi);
72
2/2
✓ Branch 0 taken 10282 times.
✓ Branch 1 taken 2 times.
10284 if (size <= 0)
73 10282 return size;
74
75 2 buf = av_buffer_create((uint8_t *) dovi, size, NULL, NULL, 0);
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!buf) {
77 av_free(dovi);
78 return AVERROR(ENOMEM);
79 }
80
81 2 sd = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_DOVI_METADATA, buf);
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!sd) {
83 av_buffer_unref(&buf);
84 return AVERROR(ENOMEM);
85 }
86
87 2 return 0;
88 }
89
90 static inline uint64_t get_ue_coef(GetBitContext *gb, const AVDOVIRpuDataHeader *hdr)
91 {
92 uint64_t ipart;
93 union { uint32_t u32; float f32; } fpart;
94
95 switch (hdr->coef_data_type) {
96 case RPU_COEFF_FIXED:
97 ipart = get_ue_golomb_long(gb);
98 fpart.u32 = get_bits_long(gb, hdr->coef_log2_denom);
99 return (ipart << hdr->coef_log2_denom) | fpart.u32;
100
101 case RPU_COEFF_FLOAT:
102 fpart.u32 = get_bits_long(gb, 32);
103 return fpart.f32 * (1LL << hdr->coef_log2_denom);
104 }
105
106 return 0; /* unreachable */
107 }
108
109 136 static inline int64_t get_se_coef(GetBitContext *gb, const AVDOVIRpuDataHeader *hdr)
110 {
111 int64_t ipart;
112 union { uint32_t u32; float f32; } fpart;
113
114
1/3
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
136 switch (hdr->coef_data_type) {
115 136 case RPU_COEFF_FIXED:
116 136 ipart = get_se_golomb_long(gb);
117 136 fpart.u32 = get_bits_long(gb, hdr->coef_log2_denom);
118 136 return ipart * (1LL << hdr->coef_log2_denom) | fpart.u32;
119
120 case RPU_COEFF_FLOAT:
121 fpart.u32 = get_bits_long(gb, 32);
122 return fpart.f32 * (1LL << hdr->coef_log2_denom);
123 }
124
125 return 0; /* unreachable */
126 }
127
128 static inline unsigned get_variable_bits(GetBitContext *gb, int n)
129 {
130 unsigned int value = get_bits(gb, n);
131 int read_more = get_bits1(gb);
132 while (read_more) {
133 value = (value + 1) << n;
134 value |= get_bits(gb, n);
135 read_more = get_bits1(gb);
136 }
137 return value;
138 }
139
140 #define VALIDATE(VAR, MIN, MAX) \
141 do { \
142 if (VAR < MIN || VAR > MAX) { \
143 av_log(s->logctx, AV_LOG_ERROR, "RPU validation failed: " \
144 #MIN" <= "#VAR" = %d <= "#MAX"\n", (int) VAR); \
145 ff_dovi_ctx_unref(s); \
146 return AVERROR_INVALIDDATA; \
147 } \
148 } while (0)
149
150 2 static int parse_ext_v1(DOVIContext *s, GetBitContext *gb, AVDOVIDmData *dm)
151 {
152
1/7
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
2 switch (dm->level) {
153 2 case 1:
154 2 dm->l1.min_pq = get_bits(gb, 12);
155 2 dm->l1.max_pq = get_bits(gb, 12);
156 2 dm->l1.avg_pq = get_bits(gb, 12);
157 2 break;
158 case 2:
159 dm->l2.target_max_pq = get_bits(gb, 12);
160 dm->l2.trim_slope = get_bits(gb, 12);
161 dm->l2.trim_offset = get_bits(gb, 12);
162 dm->l2.trim_power = get_bits(gb, 12);
163 dm->l2.trim_chroma_weight = get_bits(gb, 12);
164 dm->l2.trim_saturation_gain = get_bits(gb, 12);
165 dm->l2.ms_weight = get_sbits(gb, 13);
166 VALIDATE(dm->l2.ms_weight, -1, 4095);
167 break;
168 case 4:
169 dm->l4.anchor_pq = get_bits(gb, 12);
170 dm->l4.anchor_power = get_bits(gb, 12);
171 break;
172 case 5:
173 dm->l5.left_offset = get_bits(gb, 13);
174 dm->l5.right_offset = get_bits(gb, 13);
175 dm->l5.top_offset = get_bits(gb, 13);
176 dm->l5.bottom_offset = get_bits(gb, 13);
177 break;
178 case 6:
179 dm->l6.max_luminance = get_bits(gb, 16);
180 dm->l6.min_luminance = get_bits(gb, 16);
181 dm->l6.max_cll = get_bits(gb, 16);
182 dm->l6.max_fall = get_bits(gb, 16);
183 break;
184 case 255:
185 dm->l255.dm_run_mode = get_bits(gb, 8);
186 dm->l255.dm_run_version = get_bits(gb, 8);
187 for (int i = 0; i < 4; i++)
188 dm->l255.dm_debug[i] = get_bits(gb, 8);
189 break;
190 default:
191 avpriv_request_sample(s->logctx, "Dolby Vision DM v1 level %u", dm->level);
192 }
193
194 2 return 0;
195 }
196
197 static AVCIExy get_cie_xy(GetBitContext *gb)
198 {
199 AVCIExy xy;
200 const int denom = 32767;
201 xy.x = av_make_q(get_sbits(gb, 16), denom);
202 xy.y = av_make_q(get_sbits(gb, 16), denom);
203 return xy;
204 }
205
206 static int parse_ext_v2(DOVIContext *s, GetBitContext *gb, AVDOVIDmData *dm,
207 int ext_block_length)
208 {
209 switch (dm->level) {
210 case 3:
211 dm->l3.min_pq_offset = get_bits(gb, 12);
212 dm->l3.max_pq_offset = get_bits(gb, 12);
213 dm->l3.avg_pq_offset = get_bits(gb, 12);
214 break;
215 case 8:
216 dm->l8.target_display_index = get_bits(gb, 8);
217 dm->l8.trim_slope = get_bits(gb, 12);
218 dm->l8.trim_offset = get_bits(gb, 12);
219 dm->l8.trim_power = get_bits(gb, 12);
220 dm->l8.trim_chroma_weight = get_bits(gb, 12);
221 dm->l8.trim_saturation_gain = get_bits(gb, 12);
222 dm->l8.ms_weight = get_bits(gb, 12);
223 if (ext_block_length < 12)
224 break;
225 dm->l8.target_mid_contrast = get_bits(gb, 12);
226 if (ext_block_length < 13)
227 break;
228 dm->l8.clip_trim = get_bits(gb, 12);
229 if (ext_block_length < 19)
230 break;
231 for (int i = 0; i < 6; i++)
232 dm->l8.saturation_vector_field[i] = get_bits(gb, 8);
233 if (ext_block_length < 25)
234 break;
235 for (int i = 0; i < 6; i++)
236 dm->l8.hue_vector_field[i] = get_bits(gb, 8);
237 break;
238 case 9:
239 dm->l9.source_primary_index = get_bits(gb, 8);
240 if (ext_block_length < 17)
241 break;
242 dm->l9.source_display_primaries.prim.r = get_cie_xy(gb);
243 dm->l9.source_display_primaries.prim.g = get_cie_xy(gb);
244 dm->l9.source_display_primaries.prim.b = get_cie_xy(gb);
245 dm->l9.source_display_primaries.wp = get_cie_xy(gb);
246 break;
247 case 10:
248 dm->l10.target_display_index = get_bits(gb, 8);
249 dm->l10.target_max_pq = get_bits(gb, 12);
250 dm->l10.target_min_pq = get_bits(gb, 12);
251 dm->l10.target_primary_index = get_bits(gb, 8);
252 if (ext_block_length < 21)
253 break;
254 dm->l10.target_display_primaries.prim.r = get_cie_xy(gb);
255 dm->l10.target_display_primaries.prim.g = get_cie_xy(gb);
256 dm->l10.target_display_primaries.prim.b = get_cie_xy(gb);
257 dm->l10.target_display_primaries.wp = get_cie_xy(gb);
258 break;
259 case 11:
260 dm->l11.content_type = get_bits(gb, 8);
261 dm->l11.whitepoint = get_bits(gb, 4);
262 dm->l11.reference_mode_flag = get_bits1(gb);
263 skip_bits(gb, 3); /* reserved */
264 dm->l11.sharpness = get_bits(gb, 2);
265 dm->l11.noise_reduction = get_bits(gb, 2);
266 dm->l11.mpeg_noise_reduction = get_bits(gb, 2);
267 dm->l11.frame_rate_conversion = get_bits(gb, 2);
268 dm->l11.brightness = get_bits(gb, 2);
269 dm->l11.color = get_bits(gb, 2);
270 break;
271 case 254:
272 dm->l254.dm_mode = get_bits(gb, 8);
273 dm->l254.dm_version_index = get_bits(gb, 8);
274 break;
275 default:
276 avpriv_request_sample(s->logctx, "Dolby Vision DM v2 level %u", dm->level);
277 }
278
279 return 0;
280 }
281
282 2 static int parse_ext_blocks(DOVIContext *s, GetBitContext *gb, int ver,
283 int compression, int err_recognition)
284 {
285 int num_ext_blocks, ext_block_length, start_pos, parsed_bits, ret;
286 2 DOVIExt *ext = s->ext_blocks;
287
288 2 num_ext_blocks = get_ue_golomb_31(gb);
289 2 align_get_bits(gb);
290
291
3/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
2 if (num_ext_blocks && !ext) {
292 1 ext = s->ext_blocks = av_refstruct_allocz(sizeof(*s->ext_blocks));
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!ext)
294 return AVERROR(ENOMEM);
295 }
296
297
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 while (num_ext_blocks--) {
298 AVDOVIDmData dummy;
299 AVDOVIDmData *dm;
300 uint8_t level;
301
302 2 ext_block_length = get_ue_golomb_31(gb);
303 2 level = get_bits(gb, 8);
304 2 start_pos = get_bits_count(gb);
305
306
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (ff_dovi_rpu_extension_is_static(level)) {
307 if (compression) {
308 av_log(s->logctx, AV_LOG_WARNING, "Compressed DM RPU contains "
309 "static extension block level %d\n", level);
310 if (err_recognition & (AV_EF_AGGRESSIVE | AV_EF_EXPLODE))
311 return AVERROR_INVALIDDATA;
312 dm = &dummy;
313 } else {
314 if (ext->num_static >= FF_ARRAY_ELEMS(ext->dm_static))
315 return AVERROR_INVALIDDATA;
316 dm = &ext->dm_static[ext->num_static++];
317 }
318 } else {
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ext->num_dynamic >= FF_ARRAY_ELEMS(ext->dm_dynamic))
320 return AVERROR_INVALIDDATA;
321 2 dm = &ext->dm_dynamic[ext->num_dynamic++];
322 }
323
324 2 memset(dm, 0, sizeof(*dm));
325 2 dm->level = level;
326
1/3
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
2 switch (ver) {
327 2 case 1: ret = parse_ext_v1(s, gb, dm); break;
328 case 2: ret = parse_ext_v2(s, gb, dm, ext_block_length); break;
329 default:
330 avpriv_request_sample(s->logctx, "Dolby Vision DM v%d", ver);
331 goto skip;
332 }
333
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (ret < 0)
335 return ret;
336
337 2 skip:
338 2 parsed_bits = get_bits_count(gb) - start_pos;
339
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (parsed_bits > ext_block_length * 8)
340 return AVERROR_INVALIDDATA;
341 2 skip_bits(gb, ext_block_length * 8 - parsed_bits);
342 }
343
344 2 return 0;
345 }
346
347 2 int ff_dovi_rpu_parse(DOVIContext *s, const uint8_t *rpu, size_t rpu_size,
348 int err_recognition)
349 {
350 2 AVDOVIRpuDataHeader *hdr = &s->header;
351 2 GetBitContext *gb = &(GetBitContext){0};
352 int ret;
353
354 uint8_t rpu_type;
355 uint8_t vdr_seq_info_present;
356 uint8_t vdr_dm_metadata_present;
357 2 uint8_t dm_compression = 0;
358 uint8_t use_prev_vdr_rpu;
359 uint8_t use_nlq;
360 uint8_t profile;
361
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 uint8_t compression = s->cfg.dv_profile ? s->cfg.dv_md_compression : 0;
362
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (rpu_size < 5)
364 return AVERROR_INVALIDDATA;
365
366 /* Container */
367
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (s->cfg.dv_profile == 10 /* dav1.10 */) {
368 /* DV inside AV1 reuses an EMDF container skeleton, but with fixed
369 * values - so we can effectively treat this as a magic byte sequence.
370 *
371 * The exact fields are, as follows:
372 * emdf_version : f(2) = 0
373 * key_id : f(3) = 6
374 * emdf_payload_id : f(5) = 31
375 * emdf_payload_id_ext : var(5) = 225
376 * smploffste : f(1) = 0
377 * duratione : f(1) = 0
378 * groupide : f(1) = 0
379 * codecdatae : f(1) = 0
380 * discard_unknown_payload : f(1) = 1
381 */
382 const unsigned header_magic = 0x01be6841u;
383 unsigned emdf_header, emdf_payload_size, emdf_protection;
384 if ((ret = init_get_bits8(gb, rpu, rpu_size)) < 0)
385 return ret;
386 emdf_header = get_bits_long(gb, 27);
387 VALIDATE(emdf_header, header_magic, header_magic);
388 emdf_payload_size = get_variable_bits(gb, 8);
389 VALIDATE(emdf_payload_size, 6, 512);
390 if (emdf_payload_size * 8 > get_bits_left(gb))
391 return AVERROR_INVALIDDATA;
392
393 /* The payload is not byte-aligned (off by *one* bit, curse Dolby),
394 * so copy into a fresh buffer to preserve byte alignment of the
395 * RPU struct */
396 av_fast_padded_malloc(&s->rpu_buf, &s->rpu_buf_sz, emdf_payload_size);
397 if (!s->rpu_buf)
398 return AVERROR(ENOMEM);
399 for (int i = 0; i < emdf_payload_size; i++)
400 s->rpu_buf[i] = get_bits(gb, 8);
401 rpu = s->rpu_buf;
402 rpu_size = emdf_payload_size;
403
404 /* Validate EMDF footer */
405 emdf_protection = get_bits(gb, 5 + 12);
406 VALIDATE(emdf_protection, 0x400, 0x400);
407 } else {
408 /* NAL unit with prefix and trailing zeroes */
409
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 VALIDATE(rpu[0], 25, 25); /* NAL prefix */
410 2 rpu++;
411 2 rpu_size--;
412 }
413
414
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = init_get_bits8(gb, rpu, rpu_size)) < 0)
415 return ret;
416
417 /* RPU header */
418 2 rpu_type = get_bits(gb, 6);
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (rpu_type != 2) {
420 av_log(s->logctx, AV_LOG_WARNING, "Unrecognized RPU type "
421 "%"PRIu8", ignoring\n", rpu_type);
422 return 0;
423 }
424
425 2 hdr->rpu_type = rpu_type;
426 2 hdr->rpu_format = get_bits(gb, 11);
427
428 /* Values specific to RPU type 2 */
429 2 hdr->vdr_rpu_profile = get_bits(gb, 4);
430 2 hdr->vdr_rpu_level = get_bits(gb, 4);
431
432 2 vdr_seq_info_present = get_bits1(gb);
433
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (vdr_seq_info_present) {
434 2 hdr->chroma_resampling_explicit_filter_flag = get_bits1(gb);
435 2 hdr->coef_data_type = get_bits(gb, 2);
436
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 VALIDATE(hdr->coef_data_type, RPU_COEFF_FIXED, RPU_COEFF_FLOAT);
437
1/3
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
2 switch (hdr->coef_data_type) {
438 2 case RPU_COEFF_FIXED:
439 2 hdr->coef_log2_denom = get_ue_golomb(gb);
440
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 VALIDATE(hdr->coef_log2_denom, 13, 32);
441 2 break;
442 case RPU_COEFF_FLOAT:
443 hdr->coef_log2_denom = 32; /* arbitrary, choose maximum precision */
444 break;
445 }
446
447 2 hdr->vdr_rpu_normalized_idc = get_bits(gb, 2);
448 2 hdr->bl_video_full_range_flag = get_bits1(gb);
449
450
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if ((hdr->rpu_format & 0x700) == 0) {
451 2 int bl_bit_depth_minus8 = get_ue_golomb_31(gb);
452 2 int el_bit_depth_minus8 = get_ue_golomb_long(gb);
453 2 int vdr_bit_depth_minus8 = get_ue_golomb_31(gb);
454 /* ext_mapping_idc is in the upper 8 bits of el_bit_depth_minus8 */
455 2 int ext_mapping_idc = el_bit_depth_minus8 >> 8;
456 2 el_bit_depth_minus8 = el_bit_depth_minus8 & 0xFF;
457
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 VALIDATE(bl_bit_depth_minus8, 0, 8);
458
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 VALIDATE(el_bit_depth_minus8, 0, 8);
459
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 VALIDATE(ext_mapping_idc, 0, 0xFF);
460
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 VALIDATE(vdr_bit_depth_minus8, 0, 8);
461 2 hdr->bl_bit_depth = bl_bit_depth_minus8 + 8;
462 2 hdr->el_bit_depth = el_bit_depth_minus8 + 8;
463 2 hdr->ext_mapping_idc_0_4 = ext_mapping_idc & 0x1f; /* 5 bits */
464 2 hdr->ext_mapping_idc_5_7 = ext_mapping_idc >> 5;
465 2 hdr->vdr_bit_depth = vdr_bit_depth_minus8 + 8;
466 2 hdr->spatial_resampling_filter_flag = get_bits1(gb);
467 2 dm_compression = get_bits(gb, 3);
468 2 hdr->el_spatial_resampling_filter_flag = get_bits1(gb);
469 2 hdr->disable_residual_flag = get_bits1(gb);
470 } else {
471 avpriv_request_sample(s->logctx, "Unsupported RPU format 0x%x\n", hdr->rpu_format);
472 ff_dovi_ctx_unref(s);
473 return AVERROR_PATCHWELCOME;
474 }
475 } else {
476 /* lack of documentation/samples */
477 avpriv_request_sample(s->logctx, "Missing RPU VDR sequence info\n");
478 ff_dovi_ctx_unref(s);
479 return AVERROR_PATCHWELCOME;
480 }
481
482 2 vdr_dm_metadata_present = get_bits1(gb);
483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (dm_compression > 1) {
484 /* It seems no device supports this */
485 av_log(s->logctx, AV_LOG_ERROR, "Dynamic metadata compression is not "
486 "yet implemented");
487 return AVERROR_PATCHWELCOME;
488
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 } else if (dm_compression && !vdr_dm_metadata_present) {
489 av_log(s->logctx, AV_LOG_ERROR, "Nonzero DM metadata compression method "
490 "but no DM metadata present");
491 return AVERROR_INVALIDDATA;
492 }
493
494 2 use_prev_vdr_rpu = get_bits1(gb);
495
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 use_nlq = (hdr->rpu_format & 0x700) == 0 && !hdr->disable_residual_flag;
496
497
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 profile = s->cfg.dv_profile ? s->cfg.dv_profile : ff_dovi_guess_profile_hevc(hdr);
498
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (profile == 5 && use_nlq) {
499 av_log(s->logctx, AV_LOG_ERROR, "Profile 5 RPUs should not use NLQ\n");
500 ff_dovi_ctx_unref(s);
501 return AVERROR_INVALIDDATA;
502 }
503
504
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (err_recognition & (AV_EF_COMPLIANT | AV_EF_CAREFUL)) {
505 if (profile < 8 && compression) {
506 av_log(s->logctx, AV_LOG_ERROR, "Profile %d RPUs should not use "
507 "metadata compression.", profile);
508 return AVERROR_INVALIDDATA;
509 }
510
511 if (use_prev_vdr_rpu && !compression) {
512 av_log(s->logctx, AV_LOG_ERROR, "Uncompressed RPUs should not have "
513 "use_prev_vdr_rpu=1\n");
514 return AVERROR_INVALIDDATA;
515 }
516
517 if (dm_compression && !compression) {
518 av_log(s->logctx, AV_LOG_ERROR, "Uncompressed RPUs should not use "
519 "dm_compression=%d\n", dm_compression);
520 return AVERROR_INVALIDDATA;
521 }
522 }
523
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (use_prev_vdr_rpu) {
525 int prev_vdr_rpu_id = get_ue_golomb_31(gb);
526 VALIDATE(prev_vdr_rpu_id, 0, DOVI_MAX_DM_ID);
527 if (!s->vdr[prev_vdr_rpu_id])
528 prev_vdr_rpu_id = 0;
529 if (!s->vdr[prev_vdr_rpu_id]) {
530 /* FIXME: Technically, the spec says that in this case we should
531 * synthesize "neutral" vdr metadata, but easier to just error
532 * out as this corner case is not hit in practice */
533 av_log(s->logctx, AV_LOG_ERROR, "Unknown previous RPU ID: %u\n",
534 prev_vdr_rpu_id);
535 ff_dovi_ctx_unref(s);
536 return AVERROR_INVALIDDATA;
537 }
538 s->mapping = s->vdr[prev_vdr_rpu_id];
539 } else {
540 AVDOVIDataMapping *mapping;
541 2 int vdr_rpu_id = get_ue_golomb_31(gb);
542
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 VALIDATE(vdr_rpu_id, 0, DOVI_MAX_DM_ID);
543
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!s->vdr[vdr_rpu_id]) {
544 1 s->vdr[vdr_rpu_id] = av_refstruct_allocz(sizeof(AVDOVIDataMapping));
545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->vdr[vdr_rpu_id]) {
546 ff_dovi_ctx_unref(s);
547 return AVERROR(ENOMEM);
548 }
549 }
550
551 2 s->mapping = mapping = s->vdr[vdr_rpu_id];
552 2 mapping->vdr_rpu_id = vdr_rpu_id;
553 2 mapping->mapping_color_space = get_ue_golomb_31(gb);
554 2 mapping->mapping_chroma_format_idc = get_ue_golomb_31(gb);
555
556
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (int c = 0; c < 3; c++) {
557 6 AVDOVIReshapingCurve *curve = &mapping->curves[c];
558 6 int num_pivots_minus_2 = get_ue_golomb_31(gb);
559 6 int pivot = 0;
560
561
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
6 VALIDATE(num_pivots_minus_2, 0, AV_DOVI_MAX_PIECES - 1);
562 6 curve->num_pivots = num_pivots_minus_2 + 2;
563
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 6 times.
32 for (int i = 0; i < curve->num_pivots; i++) {
564 26 pivot += get_bits(gb, hdr->bl_bit_depth);
565 26 curve->pivots[i] = av_clip_uint16(pivot);
566 }
567 }
568
569
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (use_nlq) {
570 int nlq_pivot = 0;
571 mapping->nlq_method_idc = get_bits(gb, 3);
572
573 for (int i = 0; i < 2; i++) {
574 nlq_pivot += get_bits(gb, hdr->bl_bit_depth);
575 mapping->nlq_pivots[i] = av_clip_uint16(nlq_pivot);
576 }
577
578 /**
579 * The patent mentions another legal value, NLQ_MU_LAW, but it's
580 * not documented anywhere how to parse or apply that type of NLQ.
581 */
582 VALIDATE(mapping->nlq_method_idc, 0, AV_DOVI_NLQ_LINEAR_DZ);
583 } else {
584 2 mapping->nlq_method_idc = AV_DOVI_NLQ_NONE;
585 }
586
587 2 mapping->num_x_partitions = get_ue_golomb_long(gb) + 1;
588 2 mapping->num_y_partitions = get_ue_golomb_long(gb) + 1;
589 /* End of rpu_data_header(), start of vdr_rpu_data_payload() */
590
591
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (int c = 0; c < 3; c++) {
592 6 AVDOVIReshapingCurve *curve = &mapping->curves[c];
593
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 6 times.
26 for (int i = 0; i < curve->num_pivots - 1; i++) {
594 20 int mapping_idc = get_ue_golomb_31(gb);
595
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 20 times.
20 VALIDATE(mapping_idc, 0, 1);
596 20 curve->mapping_idc[i] = mapping_idc;
597
2/3
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
20 switch (mapping_idc) {
598 16 case AV_DOVI_MAPPING_POLYNOMIAL: {
599 16 int poly_order_minus1 = get_ue_golomb_31(gb);
600
2/4
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
16 VALIDATE(poly_order_minus1, 0, 1);
601 16 curve->poly_order[i] = poly_order_minus1 + 1;
602
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
16 if (poly_order_minus1 == 0) {
603 int linear_interp_flag = get_bits1(gb);
604 if (linear_interp_flag) {
605 /* lack of documentation/samples */
606 avpriv_request_sample(s->logctx, "Dolby Vision "
607 "linear interpolation");
608 ff_dovi_ctx_unref(s);
609 return AVERROR_PATCHWELCOME;
610 }
611 }
612
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 16 times.
64 for (int k = 0; k <= curve->poly_order[i]; k++)
613 48 curve->poly_coef[i][k] = get_se_coef(gb, hdr);
614 16 break;
615 }
616 4 case AV_DOVI_MAPPING_MMR: {
617 4 int mmr_order_minus1 = get_bits(gb, 2);
618
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
4 VALIDATE(mmr_order_minus1, 0, 2);
619 4 curve->mmr_order[i] = mmr_order_minus1 + 1;
620 4 curve->mmr_constant[i] = get_se_coef(gb, hdr);
621
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
16 for (int j = 0; j < curve->mmr_order[i]; j++) {
622
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 12 times.
96 for (int k = 0; k < 7; k++)
623 84 curve->mmr_coef[i][j][k] = get_se_coef(gb, hdr);
624 }
625 4 break;
626 }
627 }
628 }
629 }
630
631
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (use_nlq) {
632 for (int c = 0; c < 3; c++) {
633 AVDOVINLQParams *nlq = &mapping->nlq[c];
634 nlq->nlq_offset = get_bits(gb, hdr->el_bit_depth);
635 nlq->vdr_in_max = get_ue_coef(gb, hdr);
636 switch (mapping->nlq_method_idc) {
637 case AV_DOVI_NLQ_LINEAR_DZ:
638 nlq->linear_deadzone_slope = get_ue_coef(gb, hdr);
639 nlq->linear_deadzone_threshold = get_ue_coef(gb, hdr);
640 break;
641 }
642 }
643 }
644 }
645
646
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (vdr_dm_metadata_present) {
647 AVDOVIColorMetadata *color;
648 2 int affected_dm_id = get_ue_golomb_31(gb);
649 2 int current_dm_id = get_ue_golomb_31(gb);
650
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 VALIDATE(affected_dm_id, 0, DOVI_MAX_DM_ID);
651
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 VALIDATE(current_dm_id, 0, DOVI_MAX_DM_ID);
652
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (affected_dm_id != current_dm_id) {
653 /* The spec does not explain these fields at all, and there is
654 * a lack of samples to understand how they're supposed to work,
655 * so just assert them being equal for now */
656 avpriv_request_sample(s->logctx, "affected/current_dm_metadata_id "
657 "mismatch? %u != %u\n", affected_dm_id, current_dm_id);
658 ff_dovi_ctx_unref(s);
659 return AVERROR_PATCHWELCOME;
660 }
661
662
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!s->dm) {
663 1 s->dm = av_refstruct_allocz(sizeof(AVDOVIColorMetadata));
664
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->dm) {
665 ff_dovi_ctx_unref(s);
666 return AVERROR(ENOMEM);
667 }
668 }
669
670 2 s->color = color = s->dm;
671 2 color->dm_metadata_id = affected_dm_id;
672 2 color->scene_refresh_flag = get_ue_golomb_31(gb);
673
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (!dm_compression) {
674
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 for (int i = 0; i < 9; i++)
675 18 color->ycc_to_rgb_matrix[i] = av_make_q(get_sbits(gb, 16), 1 << 13);
676
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (int i = 0; i < 3; i++) {
677
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 int denom = profile == 4 ? (1 << 30) : (1 << 28);
678 6 unsigned offset = get_bits_long(gb, 32);
679
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (offset > INT_MAX) {
680 /* Ensure the result fits inside AVRational */
681 offset >>= 1;
682 denom >>= 1;
683 }
684 6 color->ycc_to_rgb_offset[i] = av_make_q(offset, denom);
685 }
686
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 2 times.
20 for (int i = 0; i < 9; i++)
687 18 color->rgb_to_lms_matrix[i] = av_make_q(get_sbits(gb, 16), 1 << 14);
688
689 2 color->signal_eotf = get_bits(gb, 16);
690 2 color->signal_eotf_param0 = get_bits(gb, 16);
691 2 color->signal_eotf_param1 = get_bits(gb, 16);
692 2 color->signal_eotf_param2 = get_bits_long(gb, 32);
693 2 color->signal_bit_depth = get_bits(gb, 5);
694
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 VALIDATE(color->signal_bit_depth, 8, 16);
695 2 color->signal_color_space = get_bits(gb, 2);
696 2 color->signal_chroma_format = get_bits(gb, 2);
697 2 color->signal_full_range_flag = get_bits(gb, 2);
698 2 color->source_min_pq = get_bits(gb, 12);
699 2 color->source_max_pq = get_bits(gb, 12);
700 2 color->source_diagonal = get_bits(gb, 10);
701 }
702
703 /* Parse extension blocks */
704
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (s->ext_blocks) {
705 1 DOVIExt *ext = s->ext_blocks;
706
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (!dm_compression)
707 1 ext->num_static = 0;
708 1 ext->num_dynamic = 0;
709 }
710
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if ((ret = parse_ext_blocks(s, gb, 1, dm_compression, err_recognition)) < 0) {
711 ff_dovi_ctx_unref(s);
712 return ret;
713 }
714
715
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (get_bits_left(gb) > 48 /* padding + CRC32 + terminator */) {
716 if ((ret = parse_ext_blocks(s, gb, 2, dm_compression, err_recognition)) < 0) {
717 ff_dovi_ctx_unref(s);
718 return ret;
719 }
720 }
721 } else {
722 s->color = &ff_dovi_color_default;
723 av_refstruct_unref(&s->ext_blocks);
724 }
725
726 2 align_get_bits(gb);
727 2 skip_bits(gb, 32); /* CRC32 */
728
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (get_bits(gb, 8) != 0x80) {
729 avpriv_request_sample(s->logctx, "Unexpected RPU format");
730 ff_dovi_ctx_unref(s);
731 return AVERROR_PATCHWELCOME;
732 }
733
734
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (err_recognition & AV_EF_CRCCHECK) {
735 rpu_size = get_bits_count(gb) / 8;
736 uint32_t crc = av_bswap32(av_crc(av_crc_get_table(AV_CRC_32_IEEE),
737 -1, rpu, rpu_size - 1)); /* exclude 0x80 */
738 if (crc) {
739 av_log(s->logctx, AV_LOG_ERROR, "RPU CRC mismatch: %X\n", crc);
740 if (err_recognition & AV_EF_EXPLODE) {
741 ff_dovi_ctx_unref(s);
742 return AVERROR_INVALIDDATA;
743 }
744 }
745 }
746
747 2 return 0;
748 }
749