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"
23 #include "bsf_internal.h"
27 #include "h265_profile_level.h"
40 typedef struct H265MetadataContext {
43 CodedBitstreamContext *cbc;
44 CodedBitstreamFragment access_unit;
50 AVRational sample_aspect_ratio;
53 int video_full_range_flag;
55 int transfer_characteristics;
56 int matrix_coefficients;
58 int chroma_sample_loc_type;
61 int poc_proportional_to_timing_flag;
62 int num_ticks_poc_diff_one;
72 } H265MetadataContext;
75 static void h265_metadata_guess_level(AVBSFContext *bsf,
76 const CodedBitstreamFragment *au)
78 H265MetadataContext *ctx = bsf->priv_data;
79 const H265LevelDescriptor *desc;
80 const H265RawProfileTierLevel *ptl = NULL;
81 const H265RawHRDParameters *hrd = NULL;
83 int width = 0, height = 0;
84 int tile_cols = 0, tile_rows = 0;
85 int max_dec_pic_buffering = 0;
88 for (i = 0; i < au->nb_units; i++) {
89 const CodedBitstreamUnit *unit = &au->units[i];
91 if (unit->type == HEVC_NAL_VPS) {
92 const H265RawVPS *vps = unit->content;
94 ptl = &vps->profile_tier_level;
95 max_dec_pic_buffering = vps->vps_max_dec_pic_buffering_minus1[0] + 1;
97 if (vps->vps_num_hrd_parameters > 0)
98 hrd = &vps->hrd_parameters[0];
100 } else if (unit->type == HEVC_NAL_SPS) {
101 const H265RawSPS *sps = unit->content;
103 ptl = &sps->profile_tier_level;
104 max_dec_pic_buffering = sps->sps_max_dec_pic_buffering_minus1[0] + 1;
106 width = sps->pic_width_in_luma_samples;
107 height = sps->pic_height_in_luma_samples;
109 if (sps->vui.vui_hrd_parameters_present_flag)
110 hrd = &sps->vui.hrd_parameters;
112 } else if (unit->type == HEVC_NAL_PPS) {
113 const H265RawPPS *pps = unit->content;
115 if (pps->tiles_enabled_flag) {
116 tile_cols = pps->num_tile_columns_minus1 + 1;
117 tile_rows = pps->num_tile_rows_minus1 + 1;
123 if (hrd->nal_hrd_parameters_present_flag) {
124 bit_rate = (hrd->nal_sub_layer_hrd_parameters[0].bit_rate_value_minus1[0] + 1) *
125 (INT64_C(1) << hrd->bit_rate_scale + 6);
126 } else if (hrd->vcl_hrd_parameters_present_flag) {
127 bit_rate = (hrd->vcl_sub_layer_hrd_parameters[0].bit_rate_value_minus1[0] + 1) *
128 (INT64_C(1) << hrd->bit_rate_scale + 6);
129 // Adjust for VCL vs. NAL limits.
130 bit_rate = bit_rate * 11 / 10;
134 desc = ff_h265_guess_level(ptl, bit_rate, width, height,
135 0, tile_rows, tile_cols,
136 max_dec_pic_buffering);
138 av_log(bsf, AV_LOG_DEBUG, "Stream appears to conform to "
139 "level %s.\n", desc->name);
140 ctx->level_guess = desc->level_idc;
144 static void h265_metadata_update_level(AVBSFContext *bsf,
147 H265MetadataContext *ctx = bsf->priv_data;
149 if (ctx->level != LEVEL_UNSET) {
150 if (ctx->level == LEVEL_AUTO) {
151 if (ctx->level_guess) {
152 *level_idc = ctx->level_guess;
154 if (!ctx->level_warned) {
155 av_log(bsf, AV_LOG_WARNING, "Unable to determine level "
156 "of stream: using level 8.5.\n");
157 ctx->level_warned = 1;
162 *level_idc = ctx->level;
167 static int h265_metadata_update_vps(AVBSFContext *bsf,
170 H265MetadataContext *ctx = bsf->priv_data;
172 if (ctx->tick_rate.num && ctx->tick_rate.den) {
175 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
176 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
178 vps->vps_time_scale = num;
179 vps->vps_num_units_in_tick = den;
181 vps->vps_timing_info_present_flag = 1;
183 if (ctx->num_ticks_poc_diff_one > 0) {
184 vps->vps_num_ticks_poc_diff_one_minus1 =
185 ctx->num_ticks_poc_diff_one - 1;
186 vps->vps_poc_proportional_to_timing_flag = 1;
187 } else if (ctx->num_ticks_poc_diff_one == 0) {
188 vps->vps_poc_proportional_to_timing_flag = 0;
192 h265_metadata_update_level(bsf, &vps->profile_tier_level.general_level_idc);
197 static int h265_metadata_update_sps(AVBSFContext *bsf,
200 H265MetadataContext *ctx = bsf->priv_data;
202 int crop_unit_x, crop_unit_y;
204 if (ctx->sample_aspect_ratio.num && ctx->sample_aspect_ratio.den) {
206 static const AVRational sar_idc[] = {
207 { 0, 0 }, // Unspecified (never written here).
208 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
209 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
210 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
211 { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
215 av_reduce(&num, &den, ctx->sample_aspect_ratio.num,
216 ctx->sample_aspect_ratio.den, 65535);
218 for (i = 1; i < FF_ARRAY_ELEMS(sar_idc); i++) {
219 if (num == sar_idc[i].num &&
220 den == sar_idc[i].den)
223 if (i == FF_ARRAY_ELEMS(sar_idc)) {
224 sps->vui.aspect_ratio_idc = 255;
225 sps->vui.sar_width = num;
226 sps->vui.sar_height = den;
228 sps->vui.aspect_ratio_idc = i;
230 sps->vui.aspect_ratio_info_present_flag = 1;
234 #define SET_OR_INFER(field, value, present_flag, infer) do { \
238 } else if (!present_flag) \
242 if (ctx->video_format >= 0 ||
243 ctx->video_full_range_flag >= 0 ||
244 ctx->colour_primaries >= 0 ||
245 ctx->transfer_characteristics >= 0 ||
246 ctx->matrix_coefficients >= 0) {
248 SET_OR_INFER(sps->vui.video_format, ctx->video_format,
249 sps->vui.video_signal_type_present_flag, 5);
251 SET_OR_INFER(sps->vui.video_full_range_flag,
252 ctx->video_full_range_flag,
253 sps->vui.video_signal_type_present_flag, 0);
255 if (ctx->colour_primaries >= 0 ||
256 ctx->transfer_characteristics >= 0 ||
257 ctx->matrix_coefficients >= 0) {
259 SET_OR_INFER(sps->vui.colour_primaries,
260 ctx->colour_primaries,
261 sps->vui.colour_description_present_flag, 2);
263 SET_OR_INFER(sps->vui.transfer_characteristics,
264 ctx->transfer_characteristics,
265 sps->vui.colour_description_present_flag, 2);
267 SET_OR_INFER(sps->vui.matrix_coefficients,
268 ctx->matrix_coefficients,
269 sps->vui.colour_description_present_flag, 2);
271 sps->vui.colour_description_present_flag = 1;
273 sps->vui.video_signal_type_present_flag = 1;
277 if (ctx->chroma_sample_loc_type >= 0) {
278 sps->vui.chroma_sample_loc_type_top_field =
279 ctx->chroma_sample_loc_type;
280 sps->vui.chroma_sample_loc_type_bottom_field =
281 ctx->chroma_sample_loc_type;
282 sps->vui.chroma_loc_info_present_flag = 1;
286 if (ctx->tick_rate.num && ctx->tick_rate.den) {
289 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
290 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
292 sps->vui.vui_time_scale = num;
293 sps->vui.vui_num_units_in_tick = den;
295 sps->vui.vui_timing_info_present_flag = 1;
298 if (ctx->num_ticks_poc_diff_one > 0) {
299 sps->vui.vui_num_ticks_poc_diff_one_minus1 =
300 ctx->num_ticks_poc_diff_one - 1;
301 sps->vui.vui_poc_proportional_to_timing_flag = 1;
302 } else if (ctx->num_ticks_poc_diff_one == 0) {
303 sps->vui.vui_poc_proportional_to_timing_flag = 0;
307 if (sps->separate_colour_plane_flag || sps->chroma_format_idc == 0) {
311 crop_unit_x = 1 + (sps->chroma_format_idc < 3);
312 crop_unit_y = 1 + (sps->chroma_format_idc < 2);
314 #define CROP(border, unit) do { \
315 if (ctx->crop_ ## border >= 0) { \
316 if (ctx->crop_ ## border % unit != 0) { \
317 av_log(bsf, AV_LOG_ERROR, "Invalid value for crop_%s: " \
318 "must be a multiple of %d.\n", #border, unit); \
319 return AVERROR(EINVAL); \
321 sps->conf_win_ ## border ## _offset = \
322 ctx->crop_ ## border / unit; \
323 sps->conformance_window_flag = 1; \
326 CROP(left, crop_unit_x);
327 CROP(right, crop_unit_x);
328 CROP(top, crop_unit_y);
329 CROP(bottom, crop_unit_y);
333 sps->vui_parameters_present_flag = 1;
335 h265_metadata_update_level(bsf, &sps->profile_tier_level.general_level_idc);
340 static int h265_metadata_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
342 H265MetadataContext *ctx = bsf->priv_data;
343 CodedBitstreamFragment *au = &ctx->access_unit;
348 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
353 err = ff_cbs_read(ctx->cbc, au, side_data, side_data_size);
355 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata from packet side data.\n");
359 if (ctx->level == LEVEL_AUTO && !ctx->level_guess)
360 h265_metadata_guess_level(bsf, au);
362 for (i = 0; i < au->nb_units; i++) {
363 if (au->units[i].type == HEVC_NAL_VPS) {
364 err = h265_metadata_update_vps(bsf, au->units[i].content);
368 if (au->units[i].type == HEVC_NAL_SPS) {
369 err = h265_metadata_update_sps(bsf, au->units[i].content);
375 err = ff_cbs_write_fragment_data(ctx->cbc, au);
377 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata into packet side data.\n");
381 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, au->data_size);
383 return AVERROR(ENOMEM);
384 memcpy(side_data, au->data, au->data_size);
386 ff_cbs_fragment_reset(ctx->cbc, au);
391 static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
393 H265MetadataContext *ctx = bsf->priv_data;
394 CodedBitstreamFragment *au = &ctx->access_unit;
397 err = ff_bsf_get_packet_ref(bsf, pkt);
401 err = h265_metadata_update_side_data(bsf, pkt);
405 err = ff_cbs_read_packet(ctx->cbc, au, pkt);
407 av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
411 if (au->nb_units == 0) {
412 av_log(bsf, AV_LOG_ERROR, "No NAL units in packet.\n");
413 err = AVERROR_INVALIDDATA;
417 // If an AUD is present, it must be the first NAL unit.
418 if (au->units[0].type == HEVC_NAL_AUD) {
419 if (ctx->aud == REMOVE)
420 ff_cbs_delete_unit(ctx->cbc, au, 0);
422 if (ctx->aud == INSERT) {
423 H265RawAUD *aud = &ctx->aud_nal;
424 int pic_type = 0, temporal_id = 8, layer_id = 0;
426 for (i = 0; i < au->nb_units; i++) {
427 const H265RawNALUnitHeader *nal = au->units[i].content;
430 if (nal->nuh_temporal_id_plus1 < temporal_id + 1)
431 temporal_id = nal->nuh_temporal_id_plus1 - 1;
433 if (au->units[i].type <= HEVC_NAL_RSV_VCL31) {
434 const H265RawSlice *slice = au->units[i].content;
435 layer_id = nal->nuh_layer_id;
436 if (slice->header.slice_type == HEVC_SLICE_B &&
439 if (slice->header.slice_type == HEVC_SLICE_P &&
445 aud->nal_unit_header = (H265RawNALUnitHeader) {
446 .nal_unit_type = HEVC_NAL_AUD,
447 .nuh_layer_id = layer_id,
448 .nuh_temporal_id_plus1 = temporal_id + 1,
450 aud->pic_type = pic_type;
452 err = ff_cbs_insert_unit_content(ctx->cbc, au,
453 0, HEVC_NAL_AUD, aud, NULL);
455 av_log(bsf, AV_LOG_ERROR, "Failed to insert AUD.\n");
461 if (ctx->level == LEVEL_AUTO && !ctx->level_guess)
462 h265_metadata_guess_level(bsf, au);
464 for (i = 0; i < au->nb_units; i++) {
465 if (au->units[i].type == HEVC_NAL_VPS) {
466 err = h265_metadata_update_vps(bsf, au->units[i].content);
470 if (au->units[i].type == HEVC_NAL_SPS) {
471 err = h265_metadata_update_sps(bsf, au->units[i].content);
477 err = ff_cbs_write_packet(ctx->cbc, pkt, au);
479 av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
485 ff_cbs_fragment_reset(ctx->cbc, au);
488 av_packet_unref(pkt);
493 static int h265_metadata_init(AVBSFContext *bsf)
495 H265MetadataContext *ctx = bsf->priv_data;
496 CodedBitstreamFragment *au = &ctx->access_unit;
499 err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_HEVC, bsf);
503 if (bsf->par_in->extradata) {
504 err = ff_cbs_read_extradata(ctx->cbc, au, bsf->par_in);
506 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
510 if (ctx->level == LEVEL_AUTO)
511 h265_metadata_guess_level(bsf, au);
513 for (i = 0; i < au->nb_units; i++) {
514 if (au->units[i].type == HEVC_NAL_VPS) {
515 err = h265_metadata_update_vps(bsf, au->units[i].content);
519 if (au->units[i].type == HEVC_NAL_SPS) {
520 err = h265_metadata_update_sps(bsf, au->units[i].content);
526 err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, au);
528 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
535 ff_cbs_fragment_reset(ctx->cbc, au);
539 static void h265_metadata_close(AVBSFContext *bsf)
541 H265MetadataContext *ctx = bsf->priv_data;
543 ff_cbs_fragment_free(ctx->cbc, &ctx->access_unit);
544 ff_cbs_close(&ctx->cbc);
547 #define OFFSET(x) offsetof(H265MetadataContext, x)
548 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
549 static const AVOption h265_metadata_options[] = {
550 { "aud", "Access Unit Delimiter NAL units",
551 OFFSET(aud), AV_OPT_TYPE_INT,
552 { .i64 = PASS }, PASS, REMOVE, FLAGS, "aud" },
553 { "pass", NULL, 0, AV_OPT_TYPE_CONST,
554 { .i64 = PASS }, .flags = FLAGS, .unit = "aud" },
555 { "insert", NULL, 0, AV_OPT_TYPE_CONST,
556 { .i64 = INSERT }, .flags = FLAGS, .unit = "aud" },
557 { "remove", NULL, 0, AV_OPT_TYPE_CONST,
558 { .i64 = REMOVE }, .flags = FLAGS, .unit = "aud" },
560 { "sample_aspect_ratio", "Set sample aspect ratio (table E-1)",
561 OFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL,
562 { .dbl = 0.0 }, 0, 65535, FLAGS },
564 { "video_format", "Set video format (table E-2)",
565 OFFSET(video_format), AV_OPT_TYPE_INT,
566 { .i64 = -1 }, -1, 7, FLAGS },
567 { "video_full_range_flag", "Set video full range flag",
568 OFFSET(video_full_range_flag), AV_OPT_TYPE_INT,
569 { .i64 = -1 }, -1, 1, FLAGS },
570 { "colour_primaries", "Set colour primaries (table E-3)",
571 OFFSET(colour_primaries), AV_OPT_TYPE_INT,
572 { .i64 = -1 }, -1, 255, FLAGS },
573 { "transfer_characteristics", "Set transfer characteristics (table E-4)",
574 OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
575 { .i64 = -1 }, -1, 255, FLAGS },
576 { "matrix_coefficients", "Set matrix coefficients (table E-5)",
577 OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
578 { .i64 = -1 }, -1, 255, FLAGS },
580 { "chroma_sample_loc_type", "Set chroma sample location type (figure E-1)",
581 OFFSET(chroma_sample_loc_type), AV_OPT_TYPE_INT,
582 { .i64 = -1 }, -1, 6, FLAGS },
585 "Set VPS and VUI tick rate (num_units_in_tick / time_scale)",
586 OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
587 { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
588 { "num_ticks_poc_diff_one",
589 "Set VPS and VUI number of ticks per POC increment",
590 OFFSET(num_ticks_poc_diff_one), AV_OPT_TYPE_INT,
591 { .i64 = -1 }, -1, INT_MAX, FLAGS },
593 { "crop_left", "Set left border crop offset",
594 OFFSET(crop_left), AV_OPT_TYPE_INT,
595 { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS },
596 { "crop_right", "Set right border crop offset",
597 OFFSET(crop_right), AV_OPT_TYPE_INT,
598 { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS },
599 { "crop_top", "Set top border crop offset",
600 OFFSET(crop_top), AV_OPT_TYPE_INT,
601 { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS },
602 { "crop_bottom", "Set bottom border crop offset",
603 OFFSET(crop_bottom), AV_OPT_TYPE_INT,
604 { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS },
606 { "level", "Set level (tables A.6 and A.7)",
607 OFFSET(level), AV_OPT_TYPE_INT,
608 { .i64 = LEVEL_UNSET }, LEVEL_UNSET, 0xff, FLAGS, "level" },
609 { "auto", "Attempt to guess level from stream properties",
610 0, AV_OPT_TYPE_CONST,
611 { .i64 = LEVEL_AUTO }, .flags = FLAGS, .unit = "level" },
612 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
613 { .i64 = value }, .flags = FLAGS, .unit = "level"
616 { LEVEL("2.1", 63) },
618 { LEVEL("3.1", 93) },
620 { LEVEL("4.1", 123) },
622 { LEVEL("5.1", 153) },
623 { LEVEL("5.2", 156) },
625 { LEVEL("6.1", 183) },
626 { LEVEL("6.2", 186) },
627 { LEVEL("8.5", 255) },
633 static const AVClass h265_metadata_class = {
634 .class_name = "h265_metadata_bsf",
635 .item_name = av_default_item_name,
636 .option = h265_metadata_options,
637 .version = LIBAVUTIL_VERSION_INT,
640 static const enum AVCodecID h265_metadata_codec_ids[] = {
641 AV_CODEC_ID_HEVC, AV_CODEC_ID_NONE,
644 const AVBitStreamFilter ff_hevc_metadata_bsf = {
645 .name = "hevc_metadata",
646 .priv_data_size = sizeof(H265MetadataContext),
647 .priv_class = &h265_metadata_class,
648 .init = &h265_metadata_init,
649 .close = &h265_metadata_close,
650 .filter = &h265_metadata_filter,
651 .codec_ids = h265_metadata_codec_ids,