2 * This file is part of FFmpeg.
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.
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.
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
19 #include "libavutil/common.h"
20 #include "libavutil/opt.h"
27 #include "h265_profile_level.h"
34 typedef struct H265MetadataContext {
41 AVRational sample_aspect_ratio;
44 int video_full_range_flag;
46 int transfer_characteristics;
47 int matrix_coefficients;
49 int chroma_sample_loc_type;
52 int poc_proportional_to_timing_flag;
53 int num_ticks_poc_diff_one;
63 } H265MetadataContext;
66 static void h265_metadata_guess_level(AVBSFContext *bsf,
67 const CodedBitstreamFragment *au)
69 H265MetadataContext *ctx = bsf->priv_data;
70 const H265LevelDescriptor *desc;
71 const H265RawProfileTierLevel *ptl = NULL;
72 const H265RawHRDParameters *hrd = NULL;
74 int width = 0, height = 0;
75 int tile_cols = 0, tile_rows = 0;
76 int max_dec_pic_buffering = 0;
79 for (i = 0; i < au->nb_units; i++) {
80 const CodedBitstreamUnit *unit = &au->units[i];
82 if (unit->type == HEVC_NAL_VPS) {
83 const H265RawVPS *vps = unit->content;
85 ptl = &vps->profile_tier_level;
86 max_dec_pic_buffering = vps->vps_max_dec_pic_buffering_minus1[0] + 1;
88 if (vps->vps_num_hrd_parameters > 0)
89 hrd = &vps->hrd_parameters[0];
91 } else if (unit->type == HEVC_NAL_SPS) {
92 const H265RawSPS *sps = unit->content;
94 ptl = &sps->profile_tier_level;
95 max_dec_pic_buffering = sps->sps_max_dec_pic_buffering_minus1[0] + 1;
97 width = sps->pic_width_in_luma_samples;
98 height = sps->pic_height_in_luma_samples;
100 if (sps->vui.vui_hrd_parameters_present_flag)
101 hrd = &sps->vui.hrd_parameters;
103 } else if (unit->type == HEVC_NAL_PPS) {
104 const H265RawPPS *pps = unit->content;
106 if (pps->tiles_enabled_flag) {
107 tile_cols = pps->num_tile_columns_minus1 + 1;
108 tile_rows = pps->num_tile_rows_minus1 + 1;
114 if (hrd->nal_hrd_parameters_present_flag) {
115 bit_rate = (hrd->nal_sub_layer_hrd_parameters[0].bit_rate_value_minus1[0] + 1) *
116 (INT64_C(1) << hrd->bit_rate_scale + 6);
117 } else if (hrd->vcl_hrd_parameters_present_flag) {
118 bit_rate = (hrd->vcl_sub_layer_hrd_parameters[0].bit_rate_value_minus1[0] + 1) *
119 (INT64_C(1) << hrd->bit_rate_scale + 6);
120 // Adjust for VCL vs. NAL limits.
121 bit_rate = bit_rate * 11 / 10;
125 desc = ff_h265_guess_level(ptl, bit_rate, width, height,
126 0, tile_rows, tile_cols,
127 max_dec_pic_buffering);
129 av_log(bsf, AV_LOG_DEBUG, "Stream appears to conform to "
130 "level %s.\n", desc->name);
131 ctx->level_guess = desc->level_idc;
135 static void h265_metadata_update_level(AVBSFContext *bsf,
138 H265MetadataContext *ctx = bsf->priv_data;
140 if (ctx->level != LEVEL_UNSET) {
141 if (ctx->level == LEVEL_AUTO) {
142 if (ctx->level_guess) {
143 *level_idc = ctx->level_guess;
145 if (!ctx->level_warned) {
146 av_log(bsf, AV_LOG_WARNING, "Unable to determine level "
147 "of stream: using level 8.5.\n");
148 ctx->level_warned = 1;
153 *level_idc = ctx->level;
158 static int h265_metadata_update_vps(AVBSFContext *bsf,
161 H265MetadataContext *ctx = bsf->priv_data;
163 if (ctx->tick_rate.num && ctx->tick_rate.den) {
166 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
167 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
169 vps->vps_time_scale = num;
170 vps->vps_num_units_in_tick = den;
172 vps->vps_timing_info_present_flag = 1;
174 if (ctx->num_ticks_poc_diff_one > 0) {
175 vps->vps_num_ticks_poc_diff_one_minus1 =
176 ctx->num_ticks_poc_diff_one - 1;
177 vps->vps_poc_proportional_to_timing_flag = 1;
178 } else if (ctx->num_ticks_poc_diff_one == 0) {
179 vps->vps_poc_proportional_to_timing_flag = 0;
183 h265_metadata_update_level(bsf, &vps->profile_tier_level.general_level_idc);
188 static int h265_metadata_update_sps(AVBSFContext *bsf,
191 H265MetadataContext *ctx = bsf->priv_data;
193 int crop_unit_x, crop_unit_y;
195 if (ctx->sample_aspect_ratio.num && ctx->sample_aspect_ratio.den) {
197 static const AVRational sar_idc[] = {
198 { 0, 0 }, // Unspecified (never written here).
199 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
200 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
201 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
202 { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
206 av_reduce(&num, &den, ctx->sample_aspect_ratio.num,
207 ctx->sample_aspect_ratio.den, 65535);
209 for (i = 1; i < FF_ARRAY_ELEMS(sar_idc); i++) {
210 if (num == sar_idc[i].num &&
211 den == sar_idc[i].den)
214 if (i == FF_ARRAY_ELEMS(sar_idc)) {
215 sps->vui.aspect_ratio_idc = 255;
216 sps->vui.sar_width = num;
217 sps->vui.sar_height = den;
219 sps->vui.aspect_ratio_idc = i;
221 sps->vui.aspect_ratio_info_present_flag = 1;
225 #define SET_OR_INFER(field, value, present_flag, infer) do { \
229 } else if (!present_flag) \
233 if (ctx->video_format >= 0 ||
234 ctx->video_full_range_flag >= 0 ||
235 ctx->colour_primaries >= 0 ||
236 ctx->transfer_characteristics >= 0 ||
237 ctx->matrix_coefficients >= 0) {
239 SET_OR_INFER(sps->vui.video_format, ctx->video_format,
240 sps->vui.video_signal_type_present_flag, 5);
242 SET_OR_INFER(sps->vui.video_full_range_flag,
243 ctx->video_full_range_flag,
244 sps->vui.video_signal_type_present_flag, 0);
246 if (ctx->colour_primaries >= 0 ||
247 ctx->transfer_characteristics >= 0 ||
248 ctx->matrix_coefficients >= 0) {
250 SET_OR_INFER(sps->vui.colour_primaries,
251 ctx->colour_primaries,
252 sps->vui.colour_description_present_flag, 2);
254 SET_OR_INFER(sps->vui.transfer_characteristics,
255 ctx->transfer_characteristics,
256 sps->vui.colour_description_present_flag, 2);
258 SET_OR_INFER(sps->vui.matrix_coefficients,
259 ctx->matrix_coefficients,
260 sps->vui.colour_description_present_flag, 2);
262 sps->vui.colour_description_present_flag = 1;
264 sps->vui.video_signal_type_present_flag = 1;
268 if (ctx->chroma_sample_loc_type >= 0) {
269 sps->vui.chroma_sample_loc_type_top_field =
270 ctx->chroma_sample_loc_type;
271 sps->vui.chroma_sample_loc_type_bottom_field =
272 ctx->chroma_sample_loc_type;
273 sps->vui.chroma_loc_info_present_flag = 1;
277 if (ctx->tick_rate.num && ctx->tick_rate.den) {
280 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
281 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
283 sps->vui.vui_time_scale = num;
284 sps->vui.vui_num_units_in_tick = den;
286 sps->vui.vui_timing_info_present_flag = 1;
289 if (ctx->num_ticks_poc_diff_one > 0) {
290 sps->vui.vui_num_ticks_poc_diff_one_minus1 =
291 ctx->num_ticks_poc_diff_one - 1;
292 sps->vui.vui_poc_proportional_to_timing_flag = 1;
293 } else if (ctx->num_ticks_poc_diff_one == 0) {
294 sps->vui.vui_poc_proportional_to_timing_flag = 0;
298 if (sps->separate_colour_plane_flag || sps->chroma_format_idc == 0) {
302 crop_unit_x = 1 + (sps->chroma_format_idc < 3);
303 crop_unit_y = 1 + (sps->chroma_format_idc < 2);
305 #define CROP(border, unit) do { \
306 if (ctx->crop_ ## border >= 0) { \
307 if (ctx->crop_ ## border % unit != 0) { \
308 av_log(bsf, AV_LOG_ERROR, "Invalid value for crop_%s: " \
309 "must be a multiple of %d.\n", #border, unit); \
310 return AVERROR(EINVAL); \
312 sps->conf_win_ ## border ## _offset = \
313 ctx->crop_ ## border / unit; \
314 sps->conformance_window_flag = 1; \
317 CROP(left, crop_unit_x);
318 CROP(right, crop_unit_x);
319 CROP(top, crop_unit_y);
320 CROP(bottom, crop_unit_y);
324 sps->vui_parameters_present_flag = 1;
326 h265_metadata_update_level(bsf, &sps->profile_tier_level.general_level_idc);
331 static int h265_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
332 CodedBitstreamFragment *au)
334 H265MetadataContext *ctx = bsf->priv_data;
337 // If an AUD is present, it must be the first NAL unit.
338 if (au->units[0].type == HEVC_NAL_AUD) {
339 if (ctx->aud == BSF_ELEMENT_REMOVE)
340 ff_cbs_delete_unit(au, 0);
342 if (pkt && ctx->aud == BSF_ELEMENT_INSERT) {
343 H265RawAUD *aud = &ctx->aud_nal;
344 int pic_type = 0, temporal_id = 8, layer_id = 0;
346 for (i = 0; i < au->nb_units; i++) {
347 const H265RawNALUnitHeader *nal = au->units[i].content;
350 if (nal->nuh_temporal_id_plus1 < temporal_id + 1)
351 temporal_id = nal->nuh_temporal_id_plus1 - 1;
353 if (au->units[i].type <= HEVC_NAL_RSV_VCL31) {
354 const H265RawSlice *slice = au->units[i].content;
355 layer_id = nal->nuh_layer_id;
356 if (slice->header.slice_type == HEVC_SLICE_B &&
359 if (slice->header.slice_type == HEVC_SLICE_P &&
365 aud->nal_unit_header = (H265RawNALUnitHeader) {
366 .nal_unit_type = HEVC_NAL_AUD,
367 .nuh_layer_id = layer_id,
368 .nuh_temporal_id_plus1 = temporal_id + 1,
370 aud->pic_type = pic_type;
372 err = ff_cbs_insert_unit_content(au, 0, HEVC_NAL_AUD, aud, NULL);
374 av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
380 if (ctx->level == LEVEL_AUTO && !ctx->level_guess)
381 h265_metadata_guess_level(bsf, au);
383 for (i = 0; i < au->nb_units; i++) {
384 if (au->units[i].type == HEVC_NAL_VPS) {
385 err = h265_metadata_update_vps(bsf, au->units[i].content);
389 if (au->units[i].type == HEVC_NAL_SPS) {
390 err = h265_metadata_update_sps(bsf, au->units[i].content);
399 static const CBSBSFType h265_metadata_type = {
400 .codec_id = AV_CODEC_ID_HEVC,
401 .fragment_name = "access unit",
402 .unit_name = "NAL unit",
403 .update_fragment = &h265_metadata_update_fragment,
406 static int h265_metadata_init(AVBSFContext *bsf)
408 return ff_cbs_bsf_generic_init(bsf, &h265_metadata_type);
411 #define OFFSET(x) offsetof(H265MetadataContext, x)
412 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
413 static const AVOption h265_metadata_options[] = {
414 BSF_ELEMENT_OPTIONS_PIR("aud", "Access Unit Delimiter NAL units",
417 { "sample_aspect_ratio", "Set sample aspect ratio (table E-1)",
418 OFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL,
419 { .dbl = 0.0 }, 0, 65535, FLAGS },
421 { "video_format", "Set video format (table E-2)",
422 OFFSET(video_format), AV_OPT_TYPE_INT,
423 { .i64 = -1 }, -1, 7, FLAGS },
424 { "video_full_range_flag", "Set video full range flag",
425 OFFSET(video_full_range_flag), AV_OPT_TYPE_INT,
426 { .i64 = -1 }, -1, 1, FLAGS },
427 { "colour_primaries", "Set colour primaries (table E-3)",
428 OFFSET(colour_primaries), AV_OPT_TYPE_INT,
429 { .i64 = -1 }, -1, 255, FLAGS },
430 { "transfer_characteristics", "Set transfer characteristics (table E-4)",
431 OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
432 { .i64 = -1 }, -1, 255, FLAGS },
433 { "matrix_coefficients", "Set matrix coefficients (table E-5)",
434 OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
435 { .i64 = -1 }, -1, 255, FLAGS },
437 { "chroma_sample_loc_type", "Set chroma sample location type (figure E-1)",
438 OFFSET(chroma_sample_loc_type), AV_OPT_TYPE_INT,
439 { .i64 = -1 }, -1, 6, FLAGS },
442 "Set VPS and VUI tick rate (num_units_in_tick / time_scale)",
443 OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
444 { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
445 { "num_ticks_poc_diff_one",
446 "Set VPS and VUI number of ticks per POC increment",
447 OFFSET(num_ticks_poc_diff_one), AV_OPT_TYPE_INT,
448 { .i64 = -1 }, -1, INT_MAX, FLAGS },
450 { "crop_left", "Set left border crop offset",
451 OFFSET(crop_left), AV_OPT_TYPE_INT,
452 { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS },
453 { "crop_right", "Set right border crop offset",
454 OFFSET(crop_right), AV_OPT_TYPE_INT,
455 { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS },
456 { "crop_top", "Set top border crop offset",
457 OFFSET(crop_top), AV_OPT_TYPE_INT,
458 { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS },
459 { "crop_bottom", "Set bottom border crop offset",
460 OFFSET(crop_bottom), AV_OPT_TYPE_INT,
461 { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS },
463 { "level", "Set level (tables A.6 and A.7)",
464 OFFSET(level), AV_OPT_TYPE_INT,
465 { .i64 = LEVEL_UNSET }, LEVEL_UNSET, 0xff, FLAGS, "level" },
466 { "auto", "Attempt to guess level from stream properties",
467 0, AV_OPT_TYPE_CONST,
468 { .i64 = LEVEL_AUTO }, .flags = FLAGS, .unit = "level" },
469 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
470 { .i64 = value }, .flags = FLAGS, .unit = "level"
473 { LEVEL("2.1", 63) },
475 { LEVEL("3.1", 93) },
477 { LEVEL("4.1", 123) },
479 { LEVEL("5.1", 153) },
480 { LEVEL("5.2", 156) },
482 { LEVEL("6.1", 183) },
483 { LEVEL("6.2", 186) },
484 { LEVEL("8.5", 255) },
490 static const AVClass h265_metadata_class = {
491 .class_name = "h265_metadata_bsf",
492 .item_name = av_default_item_name,
493 .option = h265_metadata_options,
494 .version = LIBAVUTIL_VERSION_INT,
497 static const enum AVCodecID h265_metadata_codec_ids[] = {
498 AV_CODEC_ID_HEVC, AV_CODEC_ID_NONE,
501 const AVBitStreamFilter ff_hevc_metadata_bsf = {
502 .name = "hevc_metadata",
503 .priv_data_size = sizeof(H265MetadataContext),
504 .priv_class = &h265_metadata_class,
505 .init = &h265_metadata_init,
506 .close = &ff_cbs_bsf_generic_close,
507 .filter = &ff_cbs_bsf_generic_filter,
508 .codec_ids = h265_metadata_codec_ids,