FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/bsf/h265_metadata.c
Date: 2024-05-03 15:42:48
Exec Total Coverage
Lines: 46 177 26.0%
Functions: 5 6 83.3%
Branches: 35 164 21.3%

Line Branch Exec Source
1 /*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include "libavutil/common.h"
20 #include "libavutil/opt.h"
21
22 #include "bsf.h"
23 #include "bsf_internal.h"
24 #include "cbs.h"
25 #include "cbs_bsf.h"
26 #include "cbs_h265.h"
27 #include "h2645data.h"
28 #include "hevc.h"
29 #include "h265_profile_level.h"
30
31 enum {
32 LEVEL_UNSET = -2,
33 LEVEL_AUTO = -1,
34 };
35
36 typedef struct H265MetadataContext {
37 CBSBSFContext common;
38
39 H265RawAUD aud_nal;
40
41 int aud;
42
43 AVRational sample_aspect_ratio;
44
45 int video_format;
46 int video_full_range_flag;
47 int colour_primaries;
48 int transfer_characteristics;
49 int matrix_coefficients;
50
51 int chroma_sample_loc_type;
52
53 AVRational tick_rate;
54 int poc_proportional_to_timing_flag;
55 int num_ticks_poc_diff_one;
56
57 int crop_left;
58 int crop_right;
59 int crop_top;
60 int crop_bottom;
61
62 int level;
63 int level_guess;
64 int level_warned;
65 } H265MetadataContext;
66
67
68 static void h265_metadata_guess_level(AVBSFContext *bsf,
69 const CodedBitstreamFragment *au)
70 {
71 H265MetadataContext *ctx = bsf->priv_data;
72 const H265LevelDescriptor *desc;
73 const H265RawProfileTierLevel *ptl = NULL;
74 const H265RawHRDParameters *hrd = NULL;
75 int64_t bit_rate = 0;
76 int width = 0, height = 0;
77 int tile_cols = 0, tile_rows = 0;
78 int max_dec_pic_buffering = 0;
79 int i;
80
81 for (i = 0; i < au->nb_units; i++) {
82 const CodedBitstreamUnit *unit = &au->units[i];
83
84 if (unit->type == HEVC_NAL_VPS) {
85 const H265RawVPS *vps = unit->content;
86
87 ptl = &vps->profile_tier_level;
88 max_dec_pic_buffering = vps->vps_max_dec_pic_buffering_minus1[0] + 1;
89
90 if (vps->vps_num_hrd_parameters > 0)
91 hrd = &vps->hrd_parameters[0];
92
93 } else if (unit->type == HEVC_NAL_SPS) {
94 const H265RawSPS *sps = unit->content;
95
96 ptl = &sps->profile_tier_level;
97 max_dec_pic_buffering = sps->sps_max_dec_pic_buffering_minus1[0] + 1;
98
99 width = sps->pic_width_in_luma_samples;
100 height = sps->pic_height_in_luma_samples;
101
102 if (sps->vui.vui_hrd_parameters_present_flag)
103 hrd = &sps->vui.hrd_parameters;
104
105 } else if (unit->type == HEVC_NAL_PPS) {
106 const H265RawPPS *pps = unit->content;
107
108 if (pps->tiles_enabled_flag) {
109 tile_cols = pps->num_tile_columns_minus1 + 1;
110 tile_rows = pps->num_tile_rows_minus1 + 1;
111 }
112 }
113 }
114
115 if (hrd) {
116 if (hrd->nal_hrd_parameters_present_flag) {
117 bit_rate = (hrd->nal_sub_layer_hrd_parameters[0].bit_rate_value_minus1[0] + 1) *
118 (INT64_C(1) << hrd->bit_rate_scale + 6);
119 } else if (hrd->vcl_hrd_parameters_present_flag) {
120 bit_rate = (hrd->vcl_sub_layer_hrd_parameters[0].bit_rate_value_minus1[0] + 1) *
121 (INT64_C(1) << hrd->bit_rate_scale + 6);
122 // Adjust for VCL vs. NAL limits.
123 bit_rate = bit_rate * 11 / 10;
124 }
125 }
126
127 desc = ff_h265_guess_level(ptl, bit_rate, width, height,
128 0, tile_rows, tile_cols,
129 max_dec_pic_buffering);
130 if (desc) {
131 av_log(bsf, AV_LOG_DEBUG, "Stream appears to conform to "
132 "level %s.\n", desc->name);
133 ctx->level_guess = desc->level_idc;
134 }
135 }
136
137 98 static void h265_metadata_update_level(AVBSFContext *bsf,
138 uint8_t *level_idc)
139 {
140 98 H265MetadataContext *ctx = bsf->priv_data;
141
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 98 times.
98 if (ctx->level != LEVEL_UNSET) {
143 if (ctx->level == LEVEL_AUTO) {
144 if (ctx->level_guess) {
145 *level_idc = ctx->level_guess;
146 } else {
147 if (!ctx->level_warned) {
148 av_log(bsf, AV_LOG_WARNING, "Unable to determine level "
149 "of stream: using level 8.5.\n");
150 ctx->level_warned = 1;
151 }
152 *level_idc = 255;
153 }
154 } else {
155 *level_idc = ctx->level;
156 }
157 }
158 98 }
159
160 48 static int h265_metadata_update_vps(AVBSFContext *bsf,
161 H265RawVPS *vps)
162 {
163 48 H265MetadataContext *ctx = bsf->priv_data;
164
165
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
48 if (ctx->tick_rate.num && ctx->tick_rate.den) {
166 int num, den;
167
168 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
169 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
170
171 vps->vps_time_scale = num;
172 vps->vps_num_units_in_tick = den;
173
174 vps->vps_timing_info_present_flag = 1;
175
176 if (ctx->num_ticks_poc_diff_one > 0) {
177 vps->vps_num_ticks_poc_diff_one_minus1 =
178 ctx->num_ticks_poc_diff_one - 1;
179 vps->vps_poc_proportional_to_timing_flag = 1;
180 } else if (ctx->num_ticks_poc_diff_one == 0) {
181 vps->vps_poc_proportional_to_timing_flag = 0;
182 }
183 }
184
185 48 h265_metadata_update_level(bsf, &vps->profile_tier_level.general_level_idc);
186
187 48 return 0;
188 }
189
190 50 static int h265_metadata_update_sps(AVBSFContext *bsf,
191 H265RawSPS *sps)
192 {
193 50 H265MetadataContext *ctx = bsf->priv_data;
194 50 int need_vui = 0;
195 int crop_unit_x, crop_unit_y;
196
197
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
50 if (ctx->sample_aspect_ratio.num && ctx->sample_aspect_ratio.den) {
198 int num, den, i;
199
200 av_reduce(&num, &den, ctx->sample_aspect_ratio.num,
201 ctx->sample_aspect_ratio.den, 65535);
202
203 for (i = 1; i < FF_ARRAY_ELEMS(ff_h2645_pixel_aspect); i++) {
204 if (num == ff_h2645_pixel_aspect[i].num &&
205 den == ff_h2645_pixel_aspect[i].den)
206 break;
207 }
208 if (i == FF_ARRAY_ELEMS(ff_h2645_pixel_aspect)) {
209 sps->vui.aspect_ratio_idc = 255;
210 sps->vui.sar_width = num;
211 sps->vui.sar_height = den;
212 } else {
213 sps->vui.aspect_ratio_idc = i;
214 }
215 sps->vui.aspect_ratio_info_present_flag = 1;
216 need_vui = 1;
217 }
218
219 #define SET_OR_INFER(field, value, present_flag, infer) do { \
220 if (value >= 0) { \
221 field = value; \
222 need_vui = 1; \
223 } else if (!present_flag) \
224 field = infer; \
225 } while (0)
226
227
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (ctx->video_format >= 0 ||
228
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 ctx->video_full_range_flag >= 0 ||
229
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 ctx->colour_primaries >= 0 ||
230
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 ctx->transfer_characteristics >= 0 ||
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 ctx->matrix_coefficients >= 0) {
232
233 SET_OR_INFER(sps->vui.video_format, ctx->video_format,
234 sps->vui.video_signal_type_present_flag, 5);
235
236 SET_OR_INFER(sps->vui.video_full_range_flag,
237 ctx->video_full_range_flag,
238 sps->vui.video_signal_type_present_flag, 0);
239
240 if (ctx->colour_primaries >= 0 ||
241 ctx->transfer_characteristics >= 0 ||
242 ctx->matrix_coefficients >= 0) {
243
244 SET_OR_INFER(sps->vui.colour_primaries,
245 ctx->colour_primaries,
246 sps->vui.colour_description_present_flag, 2);
247
248 SET_OR_INFER(sps->vui.transfer_characteristics,
249 ctx->transfer_characteristics,
250 sps->vui.colour_description_present_flag, 2);
251
252 SET_OR_INFER(sps->vui.matrix_coefficients,
253 ctx->matrix_coefficients,
254 sps->vui.colour_description_present_flag, 2);
255
256 sps->vui.colour_description_present_flag = 1;
257 }
258 sps->vui.video_signal_type_present_flag = 1;
259 need_vui = 1;
260 }
261
262
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (ctx->chroma_sample_loc_type >= 0) {
263 sps->vui.chroma_sample_loc_type_top_field =
264 ctx->chroma_sample_loc_type;
265 sps->vui.chroma_sample_loc_type_bottom_field =
266 ctx->chroma_sample_loc_type;
267 sps->vui.chroma_loc_info_present_flag = 1;
268 need_vui = 1;
269 }
270
271
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
50 if (ctx->tick_rate.num && ctx->tick_rate.den) {
272 int num, den;
273
274 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
275 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
276
277 sps->vui.vui_time_scale = num;
278 sps->vui.vui_num_units_in_tick = den;
279
280 sps->vui.vui_timing_info_present_flag = 1;
281 need_vui = 1;
282
283 if (ctx->num_ticks_poc_diff_one > 0) {
284 sps->vui.vui_num_ticks_poc_diff_one_minus1 =
285 ctx->num_ticks_poc_diff_one - 1;
286 sps->vui.vui_poc_proportional_to_timing_flag = 1;
287 } else if (ctx->num_ticks_poc_diff_one == 0) {
288 sps->vui.vui_poc_proportional_to_timing_flag = 0;
289 }
290 }
291
292
2/4
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 50 times.
50 if (sps->separate_colour_plane_flag || sps->chroma_format_idc == 0) {
293 crop_unit_x = 1;
294 crop_unit_y = 1;
295 } else {
296
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 crop_unit_x = 1 + (sps->chroma_format_idc < 3);
297
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 crop_unit_y = 1 + (sps->chroma_format_idc < 2);
298 }
299 #define CROP(border, unit) do { \
300 if (ctx->crop_ ## border >= 0) { \
301 if (ctx->crop_ ## border % unit != 0) { \
302 av_log(bsf, AV_LOG_ERROR, "Invalid value for crop_%s: " \
303 "must be a multiple of %d.\n", #border, unit); \
304 return AVERROR(EINVAL); \
305 } \
306 sps->conf_win_ ## border ## _offset = \
307 ctx->crop_ ## border / unit; \
308 sps->conformance_window_flag = 1; \
309 } \
310 } while (0)
311
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
50 CROP(left, crop_unit_x);
312
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
50 CROP(right, crop_unit_x);
313
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
50 CROP(top, crop_unit_y);
314
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
50 CROP(bottom, crop_unit_y);
315 #undef CROP
316
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (need_vui)
318 sps->vui_parameters_present_flag = 1;
319
320 50 h265_metadata_update_level(bsf, &sps->profile_tier_level.general_level_idc);
321
322 50 return 0;
323 }
324
325 2152 static int h265_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
326 CodedBitstreamFragment *au)
327 {
328 2152 H265MetadataContext *ctx = bsf->priv_data;
329 int err, i;
330
331 // If an AUD is present, it must be the first NAL unit.
332
3/4
✓ Branch 0 taken 2152 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 60 times.
✓ Branch 3 taken 2092 times.
2152 if (au->nb_units && au->units[0].type == HEVC_NAL_AUD) {
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (ctx->aud == BSF_ELEMENT_REMOVE)
334 ff_cbs_delete_unit(au, 0);
335 } else {
336
3/4
✓ Branch 0 taken 2072 times.
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2072 times.
2092 if (pkt && ctx->aud == BSF_ELEMENT_INSERT) {
337 H265RawAUD *aud = &ctx->aud_nal;
338 int pic_type = 0, temporal_id = 8, layer_id = 0;
339
340 for (i = 0; i < au->nb_units; i++) {
341 const H265RawNALUnitHeader *nal = au->units[i].content;
342 if (!nal)
343 continue;
344 if (nal->nuh_temporal_id_plus1 < temporal_id + 1)
345 temporal_id = nal->nuh_temporal_id_plus1 - 1;
346
347 if (au->units[i].type <= HEVC_NAL_RSV_VCL31) {
348 const H265RawSlice *slice = au->units[i].content;
349 layer_id = nal->nuh_layer_id;
350 if (slice->header.slice_type == HEVC_SLICE_B &&
351 pic_type < 2)
352 pic_type = 2;
353 if (slice->header.slice_type == HEVC_SLICE_P &&
354 pic_type < 1)
355 pic_type = 1;
356 }
357 }
358
359 aud->nal_unit_header = (H265RawNALUnitHeader) {
360 .nal_unit_type = HEVC_NAL_AUD,
361 .nuh_layer_id = layer_id,
362 .nuh_temporal_id_plus1 = temporal_id + 1,
363 };
364 aud->pic_type = pic_type;
365
366 err = ff_cbs_insert_unit_content(au, 0, HEVC_NAL_AUD, aud, NULL);
367 if (err < 0) {
368 av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
369 return err;
370 }
371 }
372 }
373
374
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2152 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2152 if (ctx->level == LEVEL_AUTO && !ctx->level_guess)
375 h265_metadata_guess_level(bsf, au);
376
377
2/2
✓ Branch 0 taken 7832 times.
✓ Branch 1 taken 2152 times.
9984 for (i = 0; i < au->nb_units; i++) {
378
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 7784 times.
7832 if (au->units[i].type == HEVC_NAL_VPS) {
379 48 err = h265_metadata_update_vps(bsf, au->units[i].content);
380
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
48 if (err < 0)
381 return err;
382 }
383
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 7782 times.
7832 if (au->units[i].type == HEVC_NAL_SPS) {
384 50 err = h265_metadata_update_sps(bsf, au->units[i].content);
385
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50 times.
50 if (err < 0)
386 return err;
387 }
388 }
389
390 2152 return 0;
391 }
392
393 static const CBSBSFType h265_metadata_type = {
394 .codec_id = AV_CODEC_ID_HEVC,
395 .fragment_name = "access unit",
396 .unit_name = "NAL unit",
397 .update_fragment = &h265_metadata_update_fragment,
398 };
399
400 20 static int h265_metadata_init(AVBSFContext *bsf)
401 {
402 20 return ff_cbs_bsf_generic_init(bsf, &h265_metadata_type);
403 }
404
405 #define OFFSET(x) offsetof(H265MetadataContext, x)
406 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
407 static const AVOption h265_metadata_options[] = {
408 BSF_ELEMENT_OPTIONS_PIR("aud", "Access Unit Delimiter NAL units",
409 aud, FLAGS),
410
411 { "sample_aspect_ratio", "Set sample aspect ratio (table E-1)",
412 OFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL,
413 { .dbl = 0.0 }, 0, 65535, FLAGS },
414
415 { "video_format", "Set video format (table E-2)",
416 OFFSET(video_format), AV_OPT_TYPE_INT,
417 { .i64 = -1 }, -1, 7, FLAGS },
418 { "video_full_range_flag", "Set video full range flag",
419 OFFSET(video_full_range_flag), AV_OPT_TYPE_INT,
420 { .i64 = -1 }, -1, 1, FLAGS },
421 { "colour_primaries", "Set colour primaries (table E-3)",
422 OFFSET(colour_primaries), AV_OPT_TYPE_INT,
423 { .i64 = -1 }, -1, 255, FLAGS },
424 { "transfer_characteristics", "Set transfer characteristics (table E-4)",
425 OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
426 { .i64 = -1 }, -1, 255, FLAGS },
427 { "matrix_coefficients", "Set matrix coefficients (table E-5)",
428 OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
429 { .i64 = -1 }, -1, 255, FLAGS },
430
431 { "chroma_sample_loc_type", "Set chroma sample location type (figure E-1)",
432 OFFSET(chroma_sample_loc_type), AV_OPT_TYPE_INT,
433 { .i64 = -1 }, -1, 5, FLAGS },
434
435 { "tick_rate",
436 "Set VPS and VUI tick rate (time_scale / num_units_in_tick)",
437 OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
438 { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
439 { "num_ticks_poc_diff_one",
440 "Set VPS and VUI number of ticks per POC increment",
441 OFFSET(num_ticks_poc_diff_one), AV_OPT_TYPE_INT,
442 { .i64 = -1 }, -1, INT_MAX, FLAGS },
443
444 { "crop_left", "Set left border crop offset",
445 OFFSET(crop_left), AV_OPT_TYPE_INT,
446 { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS },
447 { "crop_right", "Set right border crop offset",
448 OFFSET(crop_right), AV_OPT_TYPE_INT,
449 { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS },
450 { "crop_top", "Set top border crop offset",
451 OFFSET(crop_top), AV_OPT_TYPE_INT,
452 { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS },
453 { "crop_bottom", "Set bottom border crop offset",
454 OFFSET(crop_bottom), AV_OPT_TYPE_INT,
455 { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS },
456
457 { "level", "Set level (tables A.6 and A.7)",
458 OFFSET(level), AV_OPT_TYPE_INT,
459 { .i64 = LEVEL_UNSET }, LEVEL_UNSET, 0xff, FLAGS, .unit = "level" },
460 { "auto", "Attempt to guess level from stream properties",
461 0, AV_OPT_TYPE_CONST,
462 { .i64 = LEVEL_AUTO }, .flags = FLAGS, .unit = "level" },
463 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
464 { .i64 = value }, .flags = FLAGS, .unit = "level"
465 { LEVEL("1", 30) },
466 { LEVEL("2", 60) },
467 { LEVEL("2.1", 63) },
468 { LEVEL("3", 90) },
469 { LEVEL("3.1", 93) },
470 { LEVEL("4", 120) },
471 { LEVEL("4.1", 123) },
472 { LEVEL("5", 150) },
473 { LEVEL("5.1", 153) },
474 { LEVEL("5.2", 156) },
475 { LEVEL("6", 180) },
476 { LEVEL("6.1", 183) },
477 { LEVEL("6.2", 186) },
478 { LEVEL("8.5", 255) },
479 #undef LEVEL
480
481 { NULL }
482 };
483
484 static const AVClass h265_metadata_class = {
485 .class_name = "h265_metadata_bsf",
486 .item_name = av_default_item_name,
487 .option = h265_metadata_options,
488 .version = LIBAVUTIL_VERSION_INT,
489 };
490
491 static const enum AVCodecID h265_metadata_codec_ids[] = {
492 AV_CODEC_ID_HEVC, AV_CODEC_ID_NONE,
493 };
494
495 const FFBitStreamFilter ff_hevc_metadata_bsf = {
496 .p.name = "hevc_metadata",
497 .p.codec_ids = h265_metadata_codec_ids,
498 .p.priv_class = &h265_metadata_class,
499 .priv_data_size = sizeof(H265MetadataContext),
500 .init = &h265_metadata_init,
501 .close = &ff_cbs_bsf_generic_close,
502 .filter = &ff_cbs_bsf_generic_filter,
503 };
504