FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h2645_sei.c
Date: 2026-06-06 18:10:07
Exec Total Coverage
Lines: 212 406 52.2%
Functions: 12 16 75.0%
Branches: 96 240 40.0%

Line Branch Exec Source
1 /*
2 * Common H.264 and HEVC Supplementary Enhancement Information messages
3 *
4 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
5 * Copyright (C) 2012 - 2013 Guillaume Martres
6 * Copyright (C) 2012 - 2013 Gildas Cocherel
7 * Copyright (C) 2013 Vittorio Giovara
8 *
9 * This file is part of FFmpeg.
10 *
11 * FFmpeg is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * FFmpeg is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 */
25
26 #include "config_components.h"
27
28 #include "libavutil/ambient_viewing_environment.h"
29 #include "libavutil/buffer.h"
30 #include "libavutil/display.h"
31 #include "libavutil/film_grain_params.h"
32 #include "libavutil/mastering_display_metadata.h"
33 #include "libavutil/mem.h"
34 #include "libavutil/refstruct.h"
35 #include "libavutil/stereo3d.h"
36
37 #include "atsc_a53.h"
38 #include "avcodec.h"
39 #include "decode.h"
40 #include "get_bits.h"
41 #include "golomb.h"
42 #include "h2645_sei.h"
43 #include "itut35.h"
44
45 #define IS_H264(codec_id) (CONFIG_H264_SEI && (CONFIG_HEVC_SEI || CONFIG_VVC_SEI ) ? codec_id == AV_CODEC_ID_H264 : CONFIG_H264_SEI)
46 #define IS_HEVC(codec_id) (CONFIG_HEVC_SEI && (CONFIG_H264_SEI || CONFIG_VVC_SEI ) ? codec_id == AV_CODEC_ID_HEVC : CONFIG_HEVC_SEI)
47 #define IS_VVC(codec_id) (CONFIG_VVC_SEI && (CONFIG_H264_SEI || CONFIG_HEVC_SEI) ? codec_id == AV_CODEC_ID_VVC : CONFIG_VVC_SEI )
48
49 1525 static int decode_registered_user_data(H2645SEI *h, GetByteContext *gb,
50 enum AVCodecID codec_id, void *logctx)
51 {
52 1525 FFITUTT35 itut_t35 = { 0 };
53 1525 FFITUTT35Aux aux = { NULL };
54 int ret;
55
56 1525 ret = ff_itut_t35_parse_buffer(&itut_t35, gb->buffer, bytestream2_get_bytes_left(gb), 0);
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1525 times.
1525 if (!ret)
58 av_log(logctx, AV_LOG_VERBOSE,
59 "Unsupported User Data Registered ITU-T T35 SEI message (country_code = %d, provider_code = %d)\n",
60 itut_t35.country_code, itut_t35.provider_code);
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1525 times.
1525 if (ret <= 0)
62 return ret;
63
64 1525 return ff_itut_t35_parse_payload_to_struct(&itut_t35, &aux, &h->itut_t35, 0);
65 }
66
67 650 static int decode_unregistered_user_data(H2645SEIUnregistered *h,
68 GetByteContext *gb,
69 enum AVCodecID codec_id)
70 {
71 uint8_t *user_data;
72 650 int size = bytestream2_get_bytes_left(gb);
73 AVBufferRef *buf_ref, **tmp;
74
75
2/4
✓ Branch 0 taken 650 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 650 times.
650 if (size < 16 || size >= INT_MAX - 1)
76 return AVERROR_INVALIDDATA;
77
78 650 tmp = av_realloc_array(h->buf_ref, h->nb_buf_ref + 1, sizeof(*h->buf_ref));
79
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 650 times.
650 if (!tmp)
80 return AVERROR(ENOMEM);
81 650 h->buf_ref = tmp;
82
83 650 buf_ref = av_buffer_alloc(size + 1);
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 650 times.
650 if (!buf_ref)
85 return AVERROR(ENOMEM);
86 650 user_data = buf_ref->data;
87
88 650 bytestream2_get_bufferu(gb, user_data, size);
89 650 user_data[size] = 0;
90 650 buf_ref->size = size;
91 650 h->buf_ref[h->nb_buf_ref++] = buf_ref;
92
93
2/2
✓ Branch 0 taken 603 times.
✓ Branch 1 taken 47 times.
650 if (IS_H264(codec_id)) {
94 int e, build;
95 603 e = sscanf(user_data + 16, "x264 - core %d", &build);
96
4/4
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 427 times.
✓ Branch 2 taken 173 times.
✓ Branch 3 taken 3 times.
603 if (e == 1 && build > 0)
97 173 h->x264_build = build;
98
3/6
✓ Branch 0 taken 176 times.
✓ Branch 1 taken 427 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 176 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
603 if (e == 1 && build == 1 && !strncmp(user_data+16, "x264 - core 0000", 16))
99 h->x264_build = 67;
100 }
101
102 650 return 0;
103 }
104
105 2 static int decode_display_orientation(H2645SEIDisplayOrientation *h,
106 GetBitContext *gb)
107 {
108 2 h->present = !get_bits1(gb); // display_orientation_cancel_flag
109
110
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (h->present) {
111 2 h->hflip = get_bits1(gb); // hor_flip
112 2 h->vflip = get_bits1(gb); // ver_flip
113
114 2 h->anticlockwise_rotation = get_bits(gb, 16);
115 // This is followed by display_orientation_repetition_period
116 // and display_orientation_extension_flag for H.264
117 // and by display_orientation_persistence_flag for HEVC.
118 }
119
120 2 return 0;
121 }
122
123 static int decode_frame_packing_arrangement(H2645SEIFramePacking *h,
124 GetBitContext *gb,
125 enum AVCodecID codec_id)
126 {
127 h->arrangement_id = get_ue_golomb_long(gb);
128 h->arrangement_cancel_flag = get_bits1(gb);
129 h->present = !h->arrangement_cancel_flag;
130
131 if (h->present) {
132 h->arrangement_type = get_bits(gb, 7);
133 h->quincunx_sampling_flag = get_bits1(gb);
134 h->content_interpretation_type = get_bits(gb, 6);
135
136 // spatial_flipping_flag, frame0_flipped_flag, field_views_flag
137 skip_bits(gb, 3);
138 h->current_frame_is_frame0_flag = get_bits1(gb);
139 // frame0_self_contained_flag, frame1_self_contained_flag
140 skip_bits(gb, 2);
141
142 if (!h->quincunx_sampling_flag && h->arrangement_type != 5)
143 skip_bits(gb, 16); // frame[01]_grid_position_[xy]
144 skip_bits(gb, 8); // frame_packing_arrangement_reserved_byte
145 if (IS_H264(codec_id))
146 h->arrangement_repetition_period = get_ue_golomb_long(gb);
147 else
148 skip_bits1(gb); // frame_packing_arrangement_persistence_flag
149 }
150 // H.264: frame_packing_arrangement_extension_flag,
151 // HEVC: upsampled_aspect_ratio_flag
152 skip_bits1(gb);
153
154 return 0;
155 }
156
157 static int decode_alternative_transfer(H2645SEIAlternativeTransfer *s,
158 GetByteContext *gb)
159 {
160 if (bytestream2_get_bytes_left(gb) < 1)
161 return AVERROR_INVALIDDATA;
162
163 s->present = 1;
164 s->preferred_transfer_characteristics = bytestream2_get_byteu(gb);
165
166 return 0;
167 }
168
169 9 static int decode_ambient_viewing_environment(H2645SEIAmbientViewingEnvironment *s,
170 GetByteContext *gb)
171 {
172 static const uint16_t max_ambient_light_value = 50000;
173
174
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
9 if (bytestream2_get_bytes_left(gb) < 8)
175 return AVERROR_INVALIDDATA;
176
177 9 s->ambient_illuminance = bytestream2_get_be32u(gb);
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!s->ambient_illuminance)
179 return AVERROR_INVALIDDATA;
180
181 9 s->ambient_light_x = bytestream2_get_be16u(gb);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (s->ambient_light_x > max_ambient_light_value)
183 return AVERROR_INVALIDDATA;
184
185 9 s->ambient_light_y = bytestream2_get_be16u(gb);
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (s->ambient_light_y > max_ambient_light_value)
187 return AVERROR_INVALIDDATA;
188
189 9 s->present = 1;
190
191 9 return 0;
192 }
193
194 static int decode_film_grain_characteristics(H2645SEIFilmGrainCharacteristics *h,
195 enum AVCodecID codec_id, GetBitContext *gb)
196 {
197 h->present = !get_bits1(gb); // film_grain_characteristics_cancel_flag
198
199 if (h->present) {
200 memset(h, 0, sizeof(*h));
201 h->model_id = get_bits(gb, 2);
202 h->separate_colour_description_present_flag = get_bits1(gb);
203 if (h->separate_colour_description_present_flag) {
204 h->bit_depth_luma = get_bits(gb, 3) + 8;
205 h->bit_depth_chroma = get_bits(gb, 3) + 8;
206 h->full_range = get_bits1(gb);
207 h->color_primaries = get_bits(gb, 8);
208 h->transfer_characteristics = get_bits(gb, 8);
209 h->matrix_coeffs = get_bits(gb, 8);
210 }
211 h->blending_mode_id = get_bits(gb, 2);
212 h->log2_scale_factor = get_bits(gb, 4);
213 for (int c = 0; c < 3; c++)
214 h->comp_model_present_flag[c] = get_bits1(gb);
215 for (int c = 0; c < 3; c++) {
216 if (h->comp_model_present_flag[c]) {
217 h->num_intensity_intervals[c] = get_bits(gb, 8) + 1;
218 h->num_model_values[c] = get_bits(gb, 3) + 1;
219 if (h->num_model_values[c] > 6)
220 return AVERROR_INVALIDDATA;
221 for (int i = 0; i < h->num_intensity_intervals[c]; i++) {
222 h->intensity_interval_lower_bound[c][i] = get_bits(gb, 8);
223 h->intensity_interval_upper_bound[c][i] = get_bits(gb, 8);
224 for (int j = 0; j < h->num_model_values[c]; j++)
225 h->comp_model_value[c][i][j] = get_se_golomb_long(gb);
226 }
227 }
228 }
229 if (!IS_H264(codec_id))
230 h->persistence_flag = get_bits1(gb);
231 else
232 h->repetition_period = get_ue_golomb_long(gb);
233
234 h->present = 1;
235 }
236
237 return 0;
238 }
239
240 47 static int decode_nal_sei_mastering_display_info(H2645SEIMasteringDisplay *s,
241 GetByteContext *gb)
242 {
243 int i;
244
245
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
47 if (bytestream2_get_bytes_left(gb) < 24)
246 return AVERROR_INVALIDDATA;
247
248 // Mastering primaries
249
2/2
✓ Branch 0 taken 141 times.
✓ Branch 1 taken 47 times.
188 for (i = 0; i < 3; i++) {
250 141 s->display_primaries[i][0] = bytestream2_get_be16u(gb);
251 141 s->display_primaries[i][1] = bytestream2_get_be16u(gb);
252 }
253 // White point (x, y)
254 47 s->white_point[0] = bytestream2_get_be16u(gb);
255 47 s->white_point[1] = bytestream2_get_be16u(gb);
256
257 // Max and min luminance of mastering display
258 47 s->max_luminance = bytestream2_get_be32u(gb);
259 47 s->min_luminance = bytestream2_get_be32u(gb);
260
261 // As this SEI message comes before the first frame that references it,
262 // initialize the flag to 2 and decrement on IRAP access unit so it
263 // persists for the coded video sequence (e.g., between two IRAPs)
264 47 s->present = 2;
265
266 47 return 0;
267 }
268
269 38 static int decode_nal_sei_content_light_info(H2645SEIContentLight *s,
270 GetByteContext *gb)
271 {
272
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
38 if (bytestream2_get_bytes_left(gb) < 4)
273 return AVERROR_INVALIDDATA;
274
275 // Max and average light levels
276 38 s->max_content_light_level = bytestream2_get_be16u(gb);
277 38 s->max_pic_average_light_level = bytestream2_get_be16u(gb);
278 // As this SEI message comes before the first frame that references it,
279 // initialize the flag to 2 and decrement on IRAP access unit so it
280 // persists for the coded video sequence (e.g., between two IRAPs)
281 38 s->present = 2;
282
283 38 return 0;
284 }
285
286 2655 int ff_h2645_sei_message_decode(H2645SEI *h, enum SEIType type,
287 enum AVCodecID codec_id, GetBitContext *gb,
288 GetByteContext *gbyte, void *logctx)
289 {
290
7/10
✓ Branch 0 taken 1525 times.
✓ Branch 1 taken 650 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
✓ Branch 7 taken 47 times.
✓ Branch 8 taken 38 times.
✓ Branch 9 taken 384 times.
2655 switch (type) {
291 1525 case SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
292 1525 return decode_registered_user_data(h, gbyte, codec_id, logctx);
293 650 case SEI_TYPE_USER_DATA_UNREGISTERED:
294 650 return decode_unregistered_user_data(&h->unregistered, gbyte, codec_id);
295 2 case SEI_TYPE_DISPLAY_ORIENTATION:
296 2 return decode_display_orientation(&h->display_orientation, gb);
297 case SEI_TYPE_FILM_GRAIN_CHARACTERISTICS:
298 av_refstruct_unref(&h->film_grain_characteristics);
299 h->film_grain_characteristics = av_refstruct_allocz(sizeof(*h->film_grain_characteristics));
300 if (!h->film_grain_characteristics)
301 return AVERROR(ENOMEM);
302 return decode_film_grain_characteristics(h->film_grain_characteristics, codec_id, gb);
303 case SEI_TYPE_FRAME_PACKING_ARRANGEMENT:
304 return decode_frame_packing_arrangement(&h->frame_packing, gb, codec_id);
305 case SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
306 return decode_alternative_transfer(&h->alternative_transfer, gbyte);
307 9 case SEI_TYPE_AMBIENT_VIEWING_ENVIRONMENT:
308 9 return decode_ambient_viewing_environment(&h->ambient_viewing_environment,
309 gbyte);
310 47 case SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
311 47 return decode_nal_sei_mastering_display_info(&h->mastering_display,
312 gbyte);
313 38 case SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO:
314 38 return decode_nal_sei_content_light_info(&h->content_light, gbyte);
315 384 default:
316 384 return FF_H2645_SEI_MESSAGE_UNHANDLED;
317 }
318 }
319
320 1091 int ff_h2645_sei_ctx_replace(H2645SEI *dst, const H2645SEI *src)
321 {
322 1091 int ret = av_buffer_replace(&dst->itut_t35.a53_cc,
323 1091 src->itut_t35.a53_cc);
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1091 times.
1091 if (ret < 0)
325 return ret;
326
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1091 times.
1091 for (unsigned i = 0; i < dst->unregistered.nb_buf_ref; i++)
328 av_buffer_unref(&dst->unregistered.buf_ref[i]);
329 1091 dst->unregistered.nb_buf_ref = 0;
330
331 1091 ret = av_buffer_replace(&dst->itut_t35.lcevc, src->itut_t35.lcevc);
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1091 times.
1091 if (ret < 0)
333 return ret;
334
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1091 times.
1091 if (src->unregistered.nb_buf_ref) {
336 ret = av_reallocp_array(&dst->unregistered.buf_ref,
337 src->unregistered.nb_buf_ref,
338 sizeof(*dst->unregistered.buf_ref));
339 if (ret < 0)
340 return ret;
341
342 for (unsigned i = 0; i < src->unregistered.nb_buf_ref; i++) {
343 dst->unregistered.buf_ref[i] = av_buffer_ref(src->unregistered.buf_ref[i]);
344 if (!dst->unregistered.buf_ref[i])
345 return AVERROR(ENOMEM);
346 dst->unregistered.nb_buf_ref++;
347 }
348 }
349
350
2/2
✓ Branch 0 taken 8728 times.
✓ Branch 1 taken 1091 times.
9819 for (unsigned i = 0; i < FF_ARRAY_ELEMS(dst->itut_t35.aom_film_grain.sets); i++) {
351 8728 ret = av_buffer_replace(&dst->itut_t35.aom_film_grain.sets[i],
352 8728 src->itut_t35.aom_film_grain.sets[i]);
353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8728 times.
8728 if (ret < 0)
354 return ret;
355 }
356 1091 dst->itut_t35.aom_film_grain.enable = src->itut_t35.aom_film_grain.enable;
357
358 1091 dst->ambient_viewing_environment = src->ambient_viewing_environment;
359 1091 dst->mastering_display = src->mastering_display;
360 1091 dst->content_light = src->content_light;
361
362 1091 av_refstruct_replace(&dst->film_grain_characteristics,
363 1091 src->film_grain_characteristics);
364
365 1091 return 0;
366 }
367
368 static int is_frame_packing_type_valid(SEIFpaType type, enum AVCodecID codec_id)
369 {
370 if (IS_H264(codec_id))
371 return type <= SEI_FPA_H264_TYPE_2D &&
372 type >= SEI_FPA_H264_TYPE_CHECKERBOARD;
373 else
374 return type <= SEI_FPA_TYPE_INTERLEAVE_TEMPORAL &&
375 type >= SEI_FPA_TYPE_SIDE_BY_SIDE;
376 }
377
378 37564 static int h2645_sei_to_side_data(AVCodecContext *avctx, H2645SEI *sei,
379 AVFrameSideData ***sd, int *nb_sd)
380 {
381 int ret;
382
383
2/2
✓ Branch 0 taken 326 times.
✓ Branch 1 taken 37564 times.
37890 for (unsigned i = 0; i < sei->unregistered.nb_buf_ref; i++) {
384 326 H2645SEIUnregistered *unreg = &sei->unregistered;
385
386
1/2
✓ Branch 0 taken 326 times.
✗ Branch 1 not taken.
326 if (unreg->buf_ref[i]) {
387 AVFrameSideData *entry =
388 326 av_frame_side_data_add(sd, nb_sd, AV_FRAME_DATA_SEI_UNREGISTERED,
389 326 &unreg->buf_ref[i], 0);
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 326 times.
326 if (!entry)
391 av_buffer_unref(&unreg->buf_ref[i]);
392 }
393 }
394 37564 sei->unregistered.nb_buf_ref = 0;
395
396
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 37554 times.
37564 if (sei->ambient_viewing_environment.present) {
397 10 H2645SEIAmbientViewingEnvironment *env = &sei->ambient_viewing_environment;
398 AVBufferRef *buf;
399 size_t size;
400
401 AVAmbientViewingEnvironment *dst_env =
402 10 av_ambient_viewing_environment_alloc(&size);
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!dst_env)
404 return AVERROR(ENOMEM);
405
406 10 buf = av_buffer_create((uint8_t *)dst_env, size, NULL, NULL, 0);
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!buf) {
408 av_free(dst_env);
409 return AVERROR(ENOMEM);
410 }
411
412 10 dst_env->ambient_illuminance = av_make_q(env->ambient_illuminance, 10000);
413 10 dst_env->ambient_light_x = av_make_q(env->ambient_light_x, 50000);
414 10 dst_env->ambient_light_y = av_make_q(env->ambient_light_y, 50000);
415
416 10 ret = ff_frame_new_side_data_from_buf_ext(avctx, sd, nb_sd,
417 AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT, &buf);
418
419
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
420 return ret;
421 }
422
423
2/2
✓ Branch 0 taken 59 times.
✓ Branch 1 taken 37505 times.
37564 if (sei->mastering_display.present) {
424 // HEVC uses a g,b,r ordering, which we convert to a more natural r,g,b
425 59 const int mapping[3] = {2, 0, 1};
426 59 const int chroma_den = 50000;
427 59 const int luma_den = 10000;
428 int i;
429 AVMasteringDisplayMetadata *metadata;
430
431 59 ret = ff_decode_mastering_display_new_ext(avctx, sd, nb_sd, &metadata);
432
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 59 times.
59 if (ret < 0)
433 return ret;
434
435
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 10 times.
59 if (metadata) {
436 49 metadata->has_luminance = 1;
437 49 metadata->has_primaries = 1;
438
439
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 49 times.
196 for (i = 0; i < 3; i++) {
440 147 const int j = mapping[i];
441 147 metadata->display_primaries[i][0].num = sei->mastering_display.display_primaries[j][0];
442 147 metadata->display_primaries[i][0].den = chroma_den;
443
1/2
✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
294 metadata->has_primaries &= sei->mastering_display.display_primaries[j][0] >= 5 &&
444
1/2
✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
147 sei->mastering_display.display_primaries[j][0] <= 37000;
445
446 147 metadata->display_primaries[i][1].num = sei->mastering_display.display_primaries[j][1];
447 147 metadata->display_primaries[i][1].den = chroma_den;
448
1/2
✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
294 metadata->has_primaries &= sei->mastering_display.display_primaries[j][1] >= 5 &&
449
1/2
✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
147 sei->mastering_display.display_primaries[j][1] <= 42000;
450 }
451 49 metadata->white_point[0].num = sei->mastering_display.white_point[0];
452 49 metadata->white_point[0].den = chroma_den;
453
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
98 metadata->has_primaries &= sei->mastering_display.white_point[0] >= 5 &&
454
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 sei->mastering_display.white_point[0] <= 37000;
455
456 49 metadata->white_point[1].num = sei->mastering_display.white_point[1];
457 49 metadata->white_point[1].den = chroma_den;
458
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
98 metadata->has_primaries &= sei->mastering_display.white_point[1] >= 5 &&
459
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 sei->mastering_display.white_point[1] <= 42000;
460
461 49 metadata->max_luminance.num = sei->mastering_display.max_luminance;
462 49 metadata->max_luminance.den = luma_den;
463
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
98 metadata->has_luminance &= sei->mastering_display.max_luminance >= 50000 &&
464
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 1 times.
49 sei->mastering_display.max_luminance <= 100000000;
465
466 49 metadata->min_luminance.num = sei->mastering_display.min_luminance;
467 49 metadata->min_luminance.den = luma_den;
468
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
98 metadata->has_luminance &= sei->mastering_display.min_luminance <= 50000 &&
469 49 sei->mastering_display.min_luminance <
470
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 sei->mastering_display.max_luminance;
471
472 /* Real (blu-ray) releases in the wild come with minimum luminance
473 * values of 0.000 cd/m2, so permit this edge case */
474
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 49 times.
49 if (avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT)
475 metadata->has_luminance &= sei->mastering_display.min_luminance >= 1;
476
477
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
49 if (metadata->has_luminance || metadata->has_primaries)
478 49 av_log(avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
479
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 if (metadata->has_primaries) {
480 49 av_log(avctx, AV_LOG_DEBUG,
481 "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
482 49 av_q2d(metadata->display_primaries[0][0]),
483 49 av_q2d(metadata->display_primaries[0][1]),
484 49 av_q2d(metadata->display_primaries[1][0]),
485 49 av_q2d(metadata->display_primaries[1][1]),
486 49 av_q2d(metadata->display_primaries[2][0]),
487 49 av_q2d(metadata->display_primaries[2][1]),
488 49 av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
489 }
490
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 1 times.
49 if (metadata->has_luminance) {
491 48 av_log(avctx, AV_LOG_DEBUG,
492 "min_luminance=%f, max_luminance=%f\n",
493 48 av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
494 }
495 }
496 }
497
498
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 37532 times.
37564 if (sei->content_light.present) {
499 AVContentLightMetadata *metadata;
500
501 32 ret = ff_decode_content_light_new_ext(avctx, sd, nb_sd, &metadata);
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (ret < 0)
503 return ret;
504
505
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (metadata) {
506 32 metadata->MaxCLL = sei->content_light.max_content_light_level;
507 32 metadata->MaxFALL = sei->content_light.max_pic_average_light_level;
508
509 32 av_log(avctx, AV_LOG_DEBUG, "Content Light Level Metadata:\n");
510 32 av_log(avctx, AV_LOG_DEBUG, "MaxCLL=%d, MaxFALL=%d\n",
511 32 metadata->MaxCLL, metadata->MaxFALL);
512 }
513 }
514
515 37564 return 0;
516 }
517
518 37237 int ff_h2645_sei_to_frame(AVFrame *frame, H2645SEI *sei,
519 enum AVCodecID codec_id,
520 AVCodecContext *avctx, const H2645VUI *vui,
521 unsigned bit_depth_luma, unsigned bit_depth_chroma,
522 int seed)
523 {
524 37237 H2645SEIFramePacking *fp = &sei->frame_packing;
525 37237 FFITUTT35Meta *itut_t35 = &sei->itut_t35;
526 int ret;
527
528
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 37237 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
37237 if (fp->present &&
529 is_frame_packing_type_valid(fp->arrangement_type, codec_id) &&
530 fp->content_interpretation_type > 0 &&
531 fp->content_interpretation_type < 3) {
532 AVStereo3D *stereo = av_stereo3d_create_side_data(frame);
533
534 if (!stereo)
535 return AVERROR(ENOMEM);
536
537 switch (fp->arrangement_type) {
538 #if CONFIG_H264_SEI
539 case SEI_FPA_H264_TYPE_CHECKERBOARD:
540 stereo->type = AV_STEREO3D_CHECKERBOARD;
541 break;
542 case SEI_FPA_H264_TYPE_INTERLEAVE_COLUMN:
543 stereo->type = AV_STEREO3D_COLUMNS;
544 break;
545 case SEI_FPA_H264_TYPE_INTERLEAVE_ROW:
546 stereo->type = AV_STEREO3D_LINES;
547 break;
548 #endif
549 case SEI_FPA_TYPE_SIDE_BY_SIDE:
550 if (fp->quincunx_sampling_flag)
551 stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
552 else
553 stereo->type = AV_STEREO3D_SIDEBYSIDE;
554 break;
555 case SEI_FPA_TYPE_TOP_BOTTOM:
556 stereo->type = AV_STEREO3D_TOPBOTTOM;
557 break;
558 case SEI_FPA_TYPE_INTERLEAVE_TEMPORAL:
559 stereo->type = AV_STEREO3D_FRAMESEQUENCE;
560 break;
561 #if CONFIG_H264_SEI
562 case SEI_FPA_H264_TYPE_2D:
563 stereo->type = AV_STEREO3D_2D;
564 break;
565 #endif
566 }
567
568 if (fp->content_interpretation_type == 2)
569 stereo->flags = AV_STEREO3D_FLAG_INVERT;
570
571 if (fp->arrangement_type == SEI_FPA_TYPE_INTERLEAVE_TEMPORAL) {
572 if (fp->current_frame_is_frame0_flag)
573 stereo->view = AV_STEREO3D_VIEW_LEFT;
574 else
575 stereo->view = AV_STEREO3D_VIEW_RIGHT;
576 }
577 }
578
579
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37236 times.
37237 if (sei->display_orientation.present &&
580
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (sei->display_orientation.anticlockwise_rotation ||
581
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 sei->display_orientation.hflip ||
582
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 sei->display_orientation.vflip)) {
583 H2645SEIDisplayOrientation *o = &sei->display_orientation;
584 double angle = o->anticlockwise_rotation * 360 / (double) (1 << 16);
585 AVFrameSideData *rotation = av_frame_new_side_data(frame,
586 AV_FRAME_DATA_DISPLAYMATRIX,
587 sizeof(int32_t) * 9);
588 if (!rotation)
589 return AVERROR(ENOMEM);
590
591 /* av_display_rotation_set() expects the angle in the clockwise
592 * direction, hence the first minus.
593 * The below code applies the flips after the rotation, yet
594 * the H.2645 specs require flipping to be applied first.
595 * Because of R O(phi) = O(-phi) R (where R is flipping around
596 * an arbitatry axis and O(phi) is the proper rotation by phi)
597 * we can create display matrices as desired by negating
598 * the degree once for every flip applied. */
599 angle = -angle * (1 - 2 * !!o->hflip) * (1 - 2 * !!o->vflip);
600 av_display_rotation_set((int32_t *)rotation->data, angle);
601 av_display_matrix_flip((int32_t *)rotation->data,
602 o->hflip, o->vflip);
603 }
604
605
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37236 times.
37237 if (itut_t35->a53_cc) {
606 1 AVFrameSideData *sd = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_A53_CC, itut_t35->a53_cc);
607
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!sd)
608 av_buffer_unref(&itut_t35->a53_cc);
609 1 itut_t35->a53_cc = NULL;
610 #if FF_API_CODEC_PROPS
611 FF_DISABLE_DEPRECATION_WARNINGS
612 1 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
613 FF_ENABLE_DEPRECATION_WARNINGS
614 #endif
615 }
616
617 37237 ret = h2645_sei_to_side_data(avctx, sei, &frame->side_data, &frame->nb_side_data);
618
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37237 times.
37237 if (ret < 0)
619 return ret;
620
621
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 36913 times.
37237 if (itut_t35->afd) {
622
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 324 times.
324 if (!av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_AFD, itut_t35->afd))
623 av_buffer_unref(&itut_t35->afd);
624 324 itut_t35->afd = NULL;
625 }
626
627
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37237 times.
37237 if (itut_t35->lcevc) {
628 ret = ff_frame_new_side_data_from_buf(avctx, frame, AV_FRAME_DATA_LCEVC,
629 &itut_t35->lcevc);
630 if (ret < 0)
631 return ret;
632 }
633
634
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 37237 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
37237 if (sei->film_grain_characteristics && sei->film_grain_characteristics->present) {
635 H2645SEIFilmGrainCharacteristics *fgc = sei->film_grain_characteristics;
636 AVFilmGrainParams *fgp = av_film_grain_params_create_side_data(frame);
637 AVFilmGrainH274Params *h274;
638
639 if (!fgp)
640 return AVERROR(ENOMEM);
641
642 fgp->type = AV_FILM_GRAIN_PARAMS_H274;
643 h274 = &fgp->codec.h274;
644
645 fgp->seed = seed;
646 fgp->width = frame->width;
647 fgp->height = frame->height;
648
649 /* H.274 mandates film grain be applied to 4:4:4 frames */
650 fgp->subsampling_x = fgp->subsampling_y = 0;
651
652 h274->model_id = fgc->model_id;
653 if (IS_VVC(codec_id) || fgc->separate_colour_description_present_flag) {
654 fgp->bit_depth_luma = fgc->bit_depth_luma;
655 fgp->bit_depth_chroma = fgc->bit_depth_chroma;
656 fgp->color_range = fgc->full_range + 1;
657 fgp->color_primaries = fgc->color_primaries;
658 fgp->color_trc = fgc->transfer_characteristics;
659 fgp->color_space = fgc->matrix_coeffs;
660 } else {
661 fgp->bit_depth_luma = bit_depth_luma;
662 fgp->bit_depth_chroma = bit_depth_chroma;
663 if (vui->video_signal_type_present_flag)
664 fgp->color_range = vui->video_full_range_flag + 1;
665 if (vui->colour_description_present_flag) {
666 fgp->color_primaries = vui->colour_primaries;
667 fgp->color_trc = vui->transfer_characteristics;
668 fgp->color_space = vui->matrix_coeffs;
669 }
670 }
671 h274->blending_mode_id = fgc->blending_mode_id;
672 h274->log2_scale_factor = fgc->log2_scale_factor;
673
674 memcpy(&h274->component_model_present, &fgc->comp_model_present_flag,
675 sizeof(h274->component_model_present));
676 memcpy(&h274->num_intensity_intervals, &fgc->num_intensity_intervals,
677 sizeof(h274->num_intensity_intervals));
678 memcpy(&h274->num_model_values, &fgc->num_model_values,
679 sizeof(h274->num_model_values));
680 memcpy(&h274->intensity_interval_lower_bound, &fgc->intensity_interval_lower_bound,
681 sizeof(h274->intensity_interval_lower_bound));
682 memcpy(&h274->intensity_interval_upper_bound, &fgc->intensity_interval_upper_bound,
683 sizeof(h274->intensity_interval_upper_bound));
684 memcpy(&h274->comp_model_value, &fgc->comp_model_value,
685 sizeof(h274->comp_model_value));
686
687 if (IS_H264(codec_id))
688 fgc->present = !!fgc->repetition_period;
689 else
690 fgc->present = fgc->persistence_flag;
691
692 #if FF_API_CODEC_PROPS
693 FF_DISABLE_DEPRECATION_WARNINGS
694 avctx->properties |= FF_CODEC_PROPERTY_FILM_GRAIN;
695 FF_ENABLE_DEPRECATION_WARNINGS
696 #endif
697 }
698
699 #if CONFIG_HEVC_SEI
700 37237 ret = ff_aom_attach_film_grain_sets(&itut_t35->aom_film_grain, frame);
701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37237 times.
37237 if (ret < 0)
702 return ret;
703 #endif
704
705 37237 return 0;
706 }
707
708 327 int ff_h2645_sei_to_context(AVCodecContext *avctx, H2645SEI *sei)
709 {
710 327 return h2645_sei_to_side_data(avctx, sei, &avctx->decoded_side_data,
711 &avctx->nb_decoded_side_data);
712 }
713
714 77818 void ff_h2645_sei_reset(H2645SEI *s)
715 {
716
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 77818 times.
78142 for (unsigned i = 0; i < s->unregistered.nb_buf_ref; i++)
717 324 av_buffer_unref(&s->unregistered.buf_ref[i]);
718 77818 s->unregistered.nb_buf_ref = 0;
719 77818 av_freep(&s->unregistered.buf_ref);
720 77818 ff_itut_t35_unref(&s->itut_t35);
721 77818 s->ambient_viewing_environment.present = 0;
722 77818 s->mastering_display.present = 0;
723 77818 s->content_light.present = 0;
724
725 77818 av_refstruct_unref(&s->film_grain_characteristics);
726 77818 }
727