FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/h2645_sei.c
Date: 2026-10-08 05:59:33
Exec Total Coverage
Lines: 219 405 54.1%
Functions: 13 16 81.2%
Branches: 98 240 40.8%

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 1589 static int decode_registered_user_data(H2645SEI *h, GetByteContext *gb,
50 enum AVCodecID codec_id, void *logctx)
51 {
52 1589 FFITUTT35 itut_t35 = { 0 };
53 1589 FFITUTT35Aux aux = { NULL };
54 int ret;
55
56 1589 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 1589 times.
1589 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 1589 times.
1589 if (ret <= 0)
62 ✗ return ret;
63
64 1589 return ff_itut_t35_parse_payload_to_struct(&itut_t35, &aux, &h->itut_t35, 0);
65 }
66
67 675 static int decode_unregistered_user_data(H2645SEIUnregistered *h,
68 GetByteContext *gb,
69 enum AVCodecID codec_id)
70 {
71 uint8_t *user_data;
72 675 int size = bytestream2_get_bytes_left(gb);
73 AVBufferRef *buf_ref, **tmp;
74
75
2/4
✓ Branch 0 taken 675 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 675 times.
675 if (size < 16 || size >= INT_MAX - 1)
76 ✗ return AVERROR_INVALIDDATA;
77
78 675 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 675 times.
675 if (!tmp)
80 ✗ return AVERROR(ENOMEM);
81 675 h->buf_ref = tmp;
82
83 675 buf_ref = av_buffer_alloc(size + 1);
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 675 times.
675 if (!buf_ref)
85 ✗ return AVERROR(ENOMEM);
86 675 user_data = buf_ref->data;
87
88 675 bytestream2_get_bufferu(gb, user_data, size);
89 675 user_data[size] = 0;
90 675 buf_ref->size = size;
91 675 h->buf_ref[h->nb_buf_ref++] = buf_ref;
92
93
2/2
✓ Branch 0 taken 609 times.
✓ Branch 1 taken 66 times.
675 if (IS_H264(codec_id)) {
94 int e, build;
95 609 e = sscanf(user_data + 16, "x264 - core %d", &build);
96
4/4
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 427 times.
✓ Branch 2 taken 179 times.
✓ Branch 3 taken 3 times.
609 if (e == 1 && build > 0)
97 179 h->x264_build = build;
98
3/6
✓ Branch 0 taken 182 times.
✓ Branch 1 taken 427 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 182 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
609 if (e == 1 && build == 1 && !strncmp(user_data+16, "x264 - core 0000", 16))
99 ✗ h->x264_build = 67;
100 }
101
102 675 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 2 static int decode_alternative_transfer(H2645SEIAlternativeTransfer *s,
158 GetByteContext *gb)
159 {
160
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (bytestream2_get_bytes_left(gb) < 1)
161 ✗ return AVERROR_INVALIDDATA;
162
163 2 s->present = 1;
164 2 s->preferred_transfer_characteristics = bytestream2_get_byteu(gb);
165
166 2 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 67 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 67 times.
67 if (bytestream2_get_bytes_left(gb) < 24)
246 ✗ return AVERROR_INVALIDDATA;
247
248 // Mastering primaries
249
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 67 times.
268 for (i = 0; i < 3; i++) {
250 201 s->display_primaries[i][0] = bytestream2_get_be16u(gb);
251 201 s->display_primaries[i][1] = bytestream2_get_be16u(gb);
252 }
253 // White point (x, y)
254 67 s->white_point[0] = bytestream2_get_be16u(gb);
255 67 s->white_point[1] = bytestream2_get_be16u(gb);
256
257 // Max and min luminance of mastering display
258 67 s->max_luminance = bytestream2_get_be32u(gb);
259 67 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 67 s->present = 2;
265
266 67 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 2773 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
8/10
✓ Branch 0 taken 1589 times.
✓ Branch 1 taken 675 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 9 times.
✓ Branch 7 taken 67 times.
✓ Branch 8 taken 38 times.
✓ Branch 9 taken 391 times.
2773 switch (type) {
291 1589 case SEI_TYPE_USER_DATA_REGISTERED_ITU_T_T35:
292 1589 return decode_registered_user_data(h, gbyte, codec_id, logctx);
293 675 case SEI_TYPE_USER_DATA_UNREGISTERED:
294 675 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 2 case SEI_TYPE_ALTERNATIVE_TRANSFER_CHARACTERISTICS:
306 2 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 67 case SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME:
311 67 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 391 default:
316 391 return FF_H2645_SEI_MESSAGE_UNHANDLED;
317 }
318 }
319
320 1121 int ff_h2645_sei_ctx_replace(H2645SEI *dst, const H2645SEI *src)
321 {
322 1121 int ret = av_buffer_replace(&dst->itut_t35.a53_cc,
323 1121 src->itut_t35.a53_cc);
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1121 times.
1121 if (ret < 0)
325 ✗ return ret;
326
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1121 times.
1121 for (unsigned i = 0; i < dst->unregistered.nb_buf_ref; i++)
328 ✗ av_buffer_unref(&dst->unregistered.buf_ref[i]);
329 1121 dst->unregistered.nb_buf_ref = 0;
330
331 1121 ret = av_buffer_replace(&dst->itut_t35.lcevc, src->itut_t35.lcevc);
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1121 times.
1121 if (ret < 0)
333 ✗ return ret;
334
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1121 times.
1121 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 8968 times.
✓ Branch 1 taken 1121 times.
10089 for (unsigned i = 0; i < FF_ARRAY_ELEMS(dst->itut_t35.aom_film_grain.sets); i++) {
351 8968 ret = av_buffer_replace(&dst->itut_t35.aom_film_grain.sets[i],
352 8968 src->itut_t35.aom_film_grain.sets[i]);
353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8968 times.
8968 if (ret < 0)
354 ✗ return ret;
355 }
356 1121 dst->itut_t35.aom_film_grain.enable = src->itut_t35.aom_film_grain.enable;
357
358 1121 dst->alternative_transfer = src->alternative_transfer;
359 1121 dst->ambient_viewing_environment = src->ambient_viewing_environment;
360 1121 dst->mastering_display = src->mastering_display;
361 1121 dst->content_light = src->content_light;
362
363 1121 av_refstruct_replace(&dst->film_grain_characteristics,
364 1121 src->film_grain_characteristics);
365
366 1121 return 0;
367 }
368
369 ✗ static int is_frame_packing_type_valid(SEIFpaType type, enum AVCodecID codec_id)
370 {
371 ✗ if (IS_H264(codec_id))
372 ✗ return type <= SEI_FPA_H264_TYPE_2D &&
373 type >= SEI_FPA_H264_TYPE_CHECKERBOARD;
374 else
375 ✗ return type <= SEI_FPA_TYPE_INTERLEAVE_TEMPORAL &&
376 type >= SEI_FPA_TYPE_SIDE_BY_SIDE;
377 }
378
379 38009 static int h2645_sei_to_side_data(AVCodecContext *avctx, H2645SEI *sei,
380 AVFrameSideData ***sd, int *nb_sd)
381 {
382 int ret;
383
384
2/2
✓ Branch 0 taken 338 times.
✓ Branch 1 taken 38009 times.
38347 for (unsigned i = 0; i < sei->unregistered.nb_buf_ref; i++) {
385 338 H2645SEIUnregistered *unreg = &sei->unregistered;
386
387
1/2
✓ Branch 0 taken 338 times.
✗ Branch 1 not taken.
338 if (unreg->buf_ref[i]) {
388 AVFrameSideData *entry =
389 338 av_frame_side_data_add(sd, nb_sd, AV_FRAME_DATA_SEI_UNREGISTERED,
390 338 &unreg->buf_ref[i], 0);
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 338 times.
338 if (!entry)
392 ✗ av_buffer_unref(&unreg->buf_ref[i]);
393 }
394 }
395 38009 sei->unregistered.nb_buf_ref = 0;
396
397
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 37999 times.
38009 if (sei->ambient_viewing_environment.present) {
398 10 H2645SEIAmbientViewingEnvironment *env = &sei->ambient_viewing_environment;
399 AVBufferRef *buf;
400 size_t size;
401
402 AVAmbientViewingEnvironment *dst_env =
403 10 av_ambient_viewing_environment_alloc(&size);
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!dst_env)
405 ✗ return AVERROR(ENOMEM);
406
407 10 buf = av_buffer_create((uint8_t *)dst_env, size, NULL, NULL, 0);
408
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!buf) {
409 ✗ av_free(dst_env);
410 ✗ return AVERROR(ENOMEM);
411 }
412
413 10 dst_env->ambient_illuminance = av_make_q(env->ambient_illuminance, 10000);
414 10 dst_env->ambient_light_x = av_make_q(env->ambient_light_x, 50000);
415 10 dst_env->ambient_light_y = av_make_q(env->ambient_light_y, 50000);
416
417 10 ret = ff_frame_new_side_data_from_buf_ext(avctx, sd, nb_sd,
418 AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT, &buf);
419
420
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (ret < 0)
421 ✗ return ret;
422 }
423
424
2/2
✓ Branch 0 taken 175 times.
✓ Branch 1 taken 37834 times.
38009 if (sei->mastering_display.present) {
425 // HEVC uses a g,b,r ordering, which we convert to a more natural r,g,b
426 175 const int mapping[3] = {2, 0, 1};
427 175 const int chroma_den = 50000;
428 175 const int luma_den = 10000;
429 int i;
430 AVMasteringDisplayMetadata *metadata;
431
432 175 ret = ff_decode_mastering_display_new_ext(avctx, sd, nb_sd, &metadata);
433
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 175 times.
175 if (ret < 0)
434 ✗ return ret;
435
436
2/2
✓ Branch 0 taken 165 times.
✓ Branch 1 taken 10 times.
175 if (metadata) {
437 165 metadata->has_luminance = 1;
438 165 metadata->has_primaries = 1;
439
440
2/2
✓ Branch 0 taken 495 times.
✓ Branch 1 taken 165 times.
660 for (i = 0; i < 3; i++) {
441 495 const int j = mapping[i];
442 495 metadata->display_primaries[i][0].num = sei->mastering_display.display_primaries[j][0];
443 495 metadata->display_primaries[i][0].den = chroma_den;
444
1/2
✓ Branch 0 taken 495 times.
✗ Branch 1 not taken.
990 metadata->has_primaries &= sei->mastering_display.display_primaries[j][0] >= 5 &&
445
1/2
✓ Branch 0 taken 495 times.
✗ Branch 1 not taken.
495 sei->mastering_display.display_primaries[j][0] <= 37000;
446
447 495 metadata->display_primaries[i][1].num = sei->mastering_display.display_primaries[j][1];
448 495 metadata->display_primaries[i][1].den = chroma_den;
449
1/2
✓ Branch 0 taken 495 times.
✗ Branch 1 not taken.
990 metadata->has_primaries &= sei->mastering_display.display_primaries[j][1] >= 5 &&
450
1/2
✓ Branch 0 taken 495 times.
✗ Branch 1 not taken.
495 sei->mastering_display.display_primaries[j][1] <= 42000;
451 }
452 165 metadata->white_point[0].num = sei->mastering_display.white_point[0];
453 165 metadata->white_point[0].den = chroma_den;
454
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
330 metadata->has_primaries &= sei->mastering_display.white_point[0] >= 5 &&
455
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 sei->mastering_display.white_point[0] <= 37000;
456
457 165 metadata->white_point[1].num = sei->mastering_display.white_point[1];
458 165 metadata->white_point[1].den = chroma_den;
459
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
330 metadata->has_primaries &= sei->mastering_display.white_point[1] >= 5 &&
460
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 sei->mastering_display.white_point[1] <= 42000;
461
462 165 metadata->max_luminance.num = sei->mastering_display.max_luminance;
463 165 metadata->max_luminance.den = luma_den;
464
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
330 metadata->has_luminance &= sei->mastering_display.max_luminance >= 50000 &&
465
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 1 times.
165 sei->mastering_display.max_luminance <= 100000000;
466
467 165 metadata->min_luminance.num = sei->mastering_display.min_luminance;
468 165 metadata->min_luminance.den = luma_den;
469
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
330 metadata->has_luminance &= sei->mastering_display.min_luminance <= 50000 &&
470 165 sei->mastering_display.min_luminance <
471
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 sei->mastering_display.max_luminance;
472
473 /* Real (blu-ray) releases in the wild come with minimum luminance
474 * values of 0.000 cd/m2, so permit this edge case */
475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 165 times.
165 if (avctx->strict_std_compliance >= FF_COMPLIANCE_STRICT)
476 ✗ metadata->has_luminance &= sei->mastering_display.min_luminance >= 1;
477
478
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 164 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
165 if (metadata->has_luminance || metadata->has_primaries)
479 165 av_log(avctx, AV_LOG_DEBUG, "Mastering Display Metadata:\n");
480
1/2
✓ Branch 0 taken 165 times.
✗ Branch 1 not taken.
165 if (metadata->has_primaries) {
481 165 av_log(avctx, AV_LOG_DEBUG,
482 "r(%5.4f,%5.4f) g(%5.4f,%5.4f) b(%5.4f %5.4f) wp(%5.4f, %5.4f)\n",
483 165 av_q2d(metadata->display_primaries[0][0]),
484 165 av_q2d(metadata->display_primaries[0][1]),
485 165 av_q2d(metadata->display_primaries[1][0]),
486 165 av_q2d(metadata->display_primaries[1][1]),
487 165 av_q2d(metadata->display_primaries[2][0]),
488 165 av_q2d(metadata->display_primaries[2][1]),
489 165 av_q2d(metadata->white_point[0]), av_q2d(metadata->white_point[1]));
490 }
491
2/2
✓ Branch 0 taken 164 times.
✓ Branch 1 taken 1 times.
165 if (metadata->has_luminance) {
492 164 av_log(avctx, AV_LOG_DEBUG,
493 "min_luminance=%f, max_luminance=%f\n",
494 164 av_q2d(metadata->min_luminance), av_q2d(metadata->max_luminance));
495 }
496 }
497 }
498
499
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 37981 times.
38009 if (sei->content_light.present) {
500 AVContentLightMetadata *metadata;
501
502 28 ret = ff_decode_content_light_new_ext(avctx, sd, nb_sd, &metadata);
503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (ret < 0)
504 ✗ return ret;
505
506
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (metadata) {
507 28 metadata->MaxCLL = sei->content_light.max_content_light_level;
508 28 metadata->MaxFALL = sei->content_light.max_pic_average_light_level;
509
510 28 av_log(avctx, AV_LOG_DEBUG, "Content Light Level Metadata:\n");
511 28 av_log(avctx, AV_LOG_DEBUG, "MaxCLL=%d, MaxFALL=%d\n",
512 28 metadata->MaxCLL, metadata->MaxFALL);
513 }
514 }
515
516 38009 return 0;
517 }
518
519 37647 int ff_h2645_sei_to_frame(AVFrame *frame, H2645SEI *sei,
520 enum AVCodecID codec_id,
521 AVCodecContext *avctx, const H2645VUI *vui,
522 unsigned bit_depth_luma, unsigned bit_depth_chroma,
523 int seed)
524 {
525 37647 H2645SEIFramePacking *fp = &sei->frame_packing;
526 37647 FFITUTT35Meta *itut_t35 = &sei->itut_t35;
527 int ret;
528
529
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 37647 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
37647 if (fp->present &&
530 ✗ is_frame_packing_type_valid(fp->arrangement_type, codec_id) &&
531 ✗ fp->content_interpretation_type > 0 &&
532 ✗ fp->content_interpretation_type < 3) {
533 ✗ AVStereo3D *stereo = av_stereo3d_create_side_data(frame);
534
535 ✗ if (!stereo)
536 ✗ return AVERROR(ENOMEM);
537
538 ✗ switch (fp->arrangement_type) {
539 #if CONFIG_H264_SEI
540 ✗ case SEI_FPA_H264_TYPE_CHECKERBOARD:
541 ✗ stereo->type = AV_STEREO3D_CHECKERBOARD;
542 ✗ break;
543 ✗ case SEI_FPA_H264_TYPE_INTERLEAVE_COLUMN:
544 ✗ stereo->type = AV_STEREO3D_COLUMNS;
545 ✗ break;
546 ✗ case SEI_FPA_H264_TYPE_INTERLEAVE_ROW:
547 ✗ stereo->type = AV_STEREO3D_LINES;
548 ✗ break;
549 #endif
550 ✗ case SEI_FPA_TYPE_SIDE_BY_SIDE:
551 ✗ if (fp->quincunx_sampling_flag)
552 ✗ stereo->type = AV_STEREO3D_SIDEBYSIDE_QUINCUNX;
553 else
554 ✗ stereo->type = AV_STEREO3D_SIDEBYSIDE;
555 ✗ break;
556 ✗ case SEI_FPA_TYPE_TOP_BOTTOM:
557 ✗ stereo->type = AV_STEREO3D_TOPBOTTOM;
558 ✗ break;
559 ✗ case SEI_FPA_TYPE_INTERLEAVE_TEMPORAL:
560 ✗ stereo->type = AV_STEREO3D_FRAMESEQUENCE;
561 ✗ break;
562 #if CONFIG_H264_SEI
563 ✗ case SEI_FPA_H264_TYPE_2D:
564 ✗ stereo->type = AV_STEREO3D_2D;
565 ✗ break;
566 #endif
567 }
568
569 ✗ if (fp->content_interpretation_type == 2)
570 ✗ stereo->flags = AV_STEREO3D_FLAG_INVERT;
571
572 ✗ if (fp->arrangement_type == SEI_FPA_TYPE_INTERLEAVE_TEMPORAL) {
573 ✗ if (fp->current_frame_is_frame0_flag)
574 ✗ stereo->view = AV_STEREO3D_VIEW_LEFT;
575 else
576 ✗ stereo->view = AV_STEREO3D_VIEW_RIGHT;
577 }
578 }
579
580
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37646 times.
37647 if (sei->display_orientation.present &&
581
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 (sei->display_orientation.anticlockwise_rotation ||
582
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 sei->display_orientation.hflip ||
583
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 sei->display_orientation.vflip)) {
584 ✗ H2645SEIDisplayOrientation *o = &sei->display_orientation;
585 ✗ double angle = o->anticlockwise_rotation * 360 / (double) (1 << 16);
586 ✗ AVFrameSideData *rotation = av_frame_new_side_data(frame,
587 AV_FRAME_DATA_DISPLAYMATRIX,
588 sizeof(int32_t) * 9);
589 ✗ if (!rotation)
590 ✗ return AVERROR(ENOMEM);
591
592 /* av_display_rotation_set() expects the angle in the clockwise
593 * direction, hence the first minus.
594 * The below code applies the flips after the rotation, yet
595 * the H.2645 specs require flipping to be applied first.
596 * Because of R O(phi) = O(-phi) R (where R is flipping around
597 * an arbitatry axis and O(phi) is the proper rotation by phi)
598 * we can create display matrices as desired by negating
599 * the degree once for every flip applied. */
600 ✗ angle = -angle * (1 - 2 * !!o->hflip) * (1 - 2 * !!o->vflip);
601 ✗ av_display_rotation_set((int32_t *)rotation->data, angle);
602 ✗ av_display_matrix_flip((int32_t *)rotation->data,
603 o->hflip, o->vflip);
604 }
605
606
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37646 times.
37647 if (itut_t35->a53_cc) {
607 1 AVFrameSideData *sd = av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_A53_CC, itut_t35->a53_cc);
608
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!sd)
609 ✗ av_buffer_unref(&itut_t35->a53_cc);
610 1 itut_t35->a53_cc = NULL;
611 }
612
613 37647 ret = h2645_sei_to_side_data(avctx, sei, &frame->side_data, &frame->nb_side_data);
614
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37647 times.
37647 if (ret < 0)
615 ✗ return ret;
616
617
2/2
✓ Branch 0 taken 321 times.
✓ Branch 1 taken 37326 times.
37647 if (itut_t35->afd) {
618
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 321 times.
321 if (!av_frame_new_side_data_from_buf(frame, AV_FRAME_DATA_AFD, itut_t35->afd))
619 ✗ av_buffer_unref(&itut_t35->afd);
620 321 itut_t35->afd = NULL;
621 }
622
623
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37647 times.
37647 if (itut_t35->lcevc) {
624 ✗ ret = ff_frame_new_side_data_from_buf(avctx, frame, AV_FRAME_DATA_LCEVC,
625 &itut_t35->lcevc);
626 ✗ if (ret < 0)
627 ✗ return ret;
628 }
629
630
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 37647 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
37647 if (sei->film_grain_characteristics && sei->film_grain_characteristics->present) {
631 ✗ H2645SEIFilmGrainCharacteristics *fgc = sei->film_grain_characteristics;
632 ✗ AVFilmGrainParams *fgp = av_film_grain_params_create_side_data(frame);
633 AVFilmGrainH274Params *h274;
634
635 ✗ if (!fgp)
636 ✗ return AVERROR(ENOMEM);
637
638 ✗ fgp->type = AV_FILM_GRAIN_PARAMS_H274;
639 ✗ h274 = &fgp->codec.h274;
640
641 ✗ fgp->seed = seed;
642 ✗ fgp->width = frame->width;
643 ✗ fgp->height = frame->height;
644
645 /* H.274 mandates film grain be applied to 4:4:4 frames */
646 ✗ fgp->subsampling_x = fgp->subsampling_y = 0;
647
648 ✗ h274->model_id = fgc->model_id;
649 ✗ if (IS_VVC(codec_id) || fgc->separate_colour_description_present_flag) {
650 ✗ fgp->bit_depth_luma = fgc->bit_depth_luma;
651 ✗ fgp->bit_depth_chroma = fgc->bit_depth_chroma;
652 ✗ fgp->color_range = fgc->full_range + 1;
653 ✗ fgp->color_primaries = fgc->color_primaries;
654 ✗ fgp->color_trc = fgc->transfer_characteristics;
655 ✗ fgp->color_space = fgc->matrix_coeffs;
656 } else {
657 ✗ fgp->bit_depth_luma = bit_depth_luma;
658 ✗ fgp->bit_depth_chroma = bit_depth_chroma;
659 ✗ if (vui->video_signal_type_present_flag)
660 ✗ fgp->color_range = vui->video_full_range_flag + 1;
661 ✗ if (vui->colour_description_present_flag) {
662 ✗ fgp->color_primaries = vui->colour_primaries;
663 ✗ fgp->color_trc = vui->transfer_characteristics;
664 ✗ fgp->color_space = vui->matrix_coeffs;
665 }
666 }
667 ✗ h274->blending_mode_id = fgc->blending_mode_id;
668 ✗ h274->log2_scale_factor = fgc->log2_scale_factor;
669
670 ✗ memcpy(&h274->component_model_present, &fgc->comp_model_present_flag,
671 sizeof(h274->component_model_present));
672 ✗ memcpy(&h274->num_intensity_intervals, &fgc->num_intensity_intervals,
673 sizeof(h274->num_intensity_intervals));
674 ✗ memcpy(&h274->num_model_values, &fgc->num_model_values,
675 sizeof(h274->num_model_values));
676 ✗ memcpy(&h274->intensity_interval_lower_bound, &fgc->intensity_interval_lower_bound,
677 sizeof(h274->intensity_interval_lower_bound));
678 ✗ memcpy(&h274->intensity_interval_upper_bound, &fgc->intensity_interval_upper_bound,
679 sizeof(h274->intensity_interval_upper_bound));
680 ✗ memcpy(&h274->comp_model_value, &fgc->comp_model_value,
681 sizeof(h274->comp_model_value));
682
683 ✗ if (IS_H264(codec_id))
684 ✗ fgc->present = !!fgc->repetition_period;
685 else
686 ✗ fgc->present = fgc->persistence_flag;
687 }
688
689 #if CONFIG_HEVC_SEI
690 37647 ret = ff_aom_attach_film_grain_sets(&itut_t35->aom_film_grain, frame);
691
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37647 times.
37647 if (ret < 0)
692 ✗ return ret;
693 #endif
694
695 37647 return 0;
696 }
697
698 362 int ff_h2645_sei_to_context(AVCodecContext *avctx, H2645SEI *sei)
699 {
700 362 return h2645_sei_to_side_data(avctx, sei, &avctx->decoded_side_data,
701 &avctx->nb_decoded_side_data);
702 }
703
704 78919 void ff_h2645_sei_reset(H2645SEI *s)
705 {
706
2/2
✓ Branch 0 taken 337 times.
✓ Branch 1 taken 78919 times.
79256 for (unsigned i = 0; i < s->unregistered.nb_buf_ref; i++)
707 337 av_buffer_unref(&s->unregistered.buf_ref[i]);
708 78919 s->unregistered.nb_buf_ref = 0;
709 78919 av_freep(&s->unregistered.buf_ref);
710 78919 ff_itut_t35_unref(&s->itut_t35);
711 78919 s->ambient_viewing_environment.present = 0;
712 78919 s->mastering_display.present = 0;
713 78919 s->content_light.present = 0;
714
715 78919 av_refstruct_unref(&s->film_grain_characteristics);
716 78919 }
717