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 *input;
44 CodedBitstreamContext *output;
45 CodedBitstreamFragment access_unit;
51 AVRational sample_aspect_ratio;
54 int video_full_range_flag;
56 int transfer_characteristics;
57 int matrix_coefficients;
59 int chroma_sample_loc_type;
62 int poc_proportional_to_timing_flag;
63 int num_ticks_poc_diff_one;
73 } H265MetadataContext;
76 static void h265_metadata_guess_level(AVBSFContext *bsf,
77 const CodedBitstreamFragment *au)
79 H265MetadataContext *ctx = bsf->priv_data;
80 const H265LevelDescriptor *desc;
81 const H265RawProfileTierLevel *ptl = NULL;
82 const H265RawHRDParameters *hrd = NULL;
84 int width = 0, height = 0;
85 int tile_cols = 0, tile_rows = 0;
86 int max_dec_pic_buffering = 0;
89 for (i = 0; i < au->nb_units; i++) {
90 const CodedBitstreamUnit *unit = &au->units[i];
92 if (unit->type == HEVC_NAL_VPS) {
93 const H265RawVPS *vps = unit->content;
95 ptl = &vps->profile_tier_level;
96 max_dec_pic_buffering = vps->vps_max_dec_pic_buffering_minus1[0] + 1;
98 if (vps->vps_num_hrd_parameters > 0)
99 hrd = &vps->hrd_parameters[0];
101 } else if (unit->type == HEVC_NAL_SPS) {
102 const H265RawSPS *sps = unit->content;
104 ptl = &sps->profile_tier_level;
105 max_dec_pic_buffering = sps->sps_max_dec_pic_buffering_minus1[0] + 1;
107 width = sps->pic_width_in_luma_samples;
108 height = sps->pic_height_in_luma_samples;
110 if (sps->vui.vui_hrd_parameters_present_flag)
111 hrd = &sps->vui.hrd_parameters;
113 } else if (unit->type == HEVC_NAL_PPS) {
114 const H265RawPPS *pps = unit->content;
116 if (pps->tiles_enabled_flag) {
117 tile_cols = pps->num_tile_columns_minus1 + 1;
118 tile_rows = pps->num_tile_rows_minus1 + 1;
124 if (hrd->nal_hrd_parameters_present_flag) {
125 bit_rate = (hrd->nal_sub_layer_hrd_parameters[0].bit_rate_value_minus1[0] + 1) *
126 (INT64_C(1) << hrd->bit_rate_scale + 6);
127 } else if (hrd->vcl_hrd_parameters_present_flag) {
128 bit_rate = (hrd->vcl_sub_layer_hrd_parameters[0].bit_rate_value_minus1[0] + 1) *
129 (INT64_C(1) << hrd->bit_rate_scale + 6);
130 // Adjust for VCL vs. NAL limits.
131 bit_rate = bit_rate * 11 / 10;
135 desc = ff_h265_guess_level(ptl, bit_rate, width, height,
136 0, tile_rows, tile_cols,
137 max_dec_pic_buffering);
139 av_log(bsf, AV_LOG_DEBUG, "Stream appears to conform to "
140 "level %s.\n", desc->name);
141 ctx->level_guess = desc->level_idc;
145 static void h265_metadata_update_level(AVBSFContext *bsf,
148 H265MetadataContext *ctx = bsf->priv_data;
150 if (ctx->level != LEVEL_UNSET) {
151 if (ctx->level == LEVEL_AUTO) {
152 if (ctx->level_guess) {
153 *level_idc = ctx->level_guess;
155 if (!ctx->level_warned) {
156 av_log(bsf, AV_LOG_WARNING, "Unable to determine level "
157 "of stream: using level 8.5.\n");
158 ctx->level_warned = 1;
163 *level_idc = ctx->level;
168 static int h265_metadata_update_vps(AVBSFContext *bsf,
171 H265MetadataContext *ctx = bsf->priv_data;
173 if (ctx->tick_rate.num && ctx->tick_rate.den) {
176 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
177 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
179 vps->vps_time_scale = num;
180 vps->vps_num_units_in_tick = den;
182 vps->vps_timing_info_present_flag = 1;
184 if (ctx->num_ticks_poc_diff_one > 0) {
185 vps->vps_num_ticks_poc_diff_one_minus1 =
186 ctx->num_ticks_poc_diff_one - 1;
187 vps->vps_poc_proportional_to_timing_flag = 1;
188 } else if (ctx->num_ticks_poc_diff_one == 0) {
189 vps->vps_poc_proportional_to_timing_flag = 0;
193 h265_metadata_update_level(bsf, &vps->profile_tier_level.general_level_idc);
198 static int h265_metadata_update_sps(AVBSFContext *bsf,
201 H265MetadataContext *ctx = bsf->priv_data;
203 int crop_unit_x, crop_unit_y;
205 if (ctx->sample_aspect_ratio.num && ctx->sample_aspect_ratio.den) {
207 static const AVRational sar_idc[] = {
208 { 0, 0 }, // Unspecified (never written here).
209 { 1, 1 }, { 12, 11 }, { 10, 11 }, { 16, 11 },
210 { 40, 33 }, { 24, 11 }, { 20, 11 }, { 32, 11 },
211 { 80, 33 }, { 18, 11 }, { 15, 11 }, { 64, 33 },
212 { 160, 99 }, { 4, 3 }, { 3, 2 }, { 2, 1 },
216 av_reduce(&num, &den, ctx->sample_aspect_ratio.num,
217 ctx->sample_aspect_ratio.den, 65535);
219 for (i = 1; i < FF_ARRAY_ELEMS(sar_idc); i++) {
220 if (num == sar_idc[i].num &&
221 den == sar_idc[i].den)
224 if (i == FF_ARRAY_ELEMS(sar_idc)) {
225 sps->vui.aspect_ratio_idc = 255;
226 sps->vui.sar_width = num;
227 sps->vui.sar_height = den;
229 sps->vui.aspect_ratio_idc = i;
231 sps->vui.aspect_ratio_info_present_flag = 1;
235 #define SET_OR_INFER(field, value, present_flag, infer) do { \
239 } else if (!present_flag) \
243 if (ctx->video_format >= 0 ||
244 ctx->video_full_range_flag >= 0 ||
245 ctx->colour_primaries >= 0 ||
246 ctx->transfer_characteristics >= 0 ||
247 ctx->matrix_coefficients >= 0) {
249 SET_OR_INFER(sps->vui.video_format, ctx->video_format,
250 sps->vui.video_signal_type_present_flag, 5);
252 SET_OR_INFER(sps->vui.video_full_range_flag,
253 ctx->video_full_range_flag,
254 sps->vui.video_signal_type_present_flag, 0);
256 if (ctx->colour_primaries >= 0 ||
257 ctx->transfer_characteristics >= 0 ||
258 ctx->matrix_coefficients >= 0) {
260 SET_OR_INFER(sps->vui.colour_primaries,
261 ctx->colour_primaries,
262 sps->vui.colour_description_present_flag, 2);
264 SET_OR_INFER(sps->vui.transfer_characteristics,
265 ctx->transfer_characteristics,
266 sps->vui.colour_description_present_flag, 2);
268 SET_OR_INFER(sps->vui.matrix_coefficients,
269 ctx->matrix_coefficients,
270 sps->vui.colour_description_present_flag, 2);
272 sps->vui.colour_description_present_flag = 1;
274 sps->vui.video_signal_type_present_flag = 1;
278 if (ctx->chroma_sample_loc_type >= 0) {
279 sps->vui.chroma_sample_loc_type_top_field =
280 ctx->chroma_sample_loc_type;
281 sps->vui.chroma_sample_loc_type_bottom_field =
282 ctx->chroma_sample_loc_type;
283 sps->vui.chroma_loc_info_present_flag = 1;
287 if (ctx->tick_rate.num && ctx->tick_rate.den) {
290 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
291 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
293 sps->vui.vui_time_scale = num;
294 sps->vui.vui_num_units_in_tick = den;
296 sps->vui.vui_timing_info_present_flag = 1;
299 if (ctx->num_ticks_poc_diff_one > 0) {
300 sps->vui.vui_num_ticks_poc_diff_one_minus1 =
301 ctx->num_ticks_poc_diff_one - 1;
302 sps->vui.vui_poc_proportional_to_timing_flag = 1;
303 } else if (ctx->num_ticks_poc_diff_one == 0) {
304 sps->vui.vui_poc_proportional_to_timing_flag = 0;
308 if (sps->separate_colour_plane_flag || sps->chroma_format_idc == 0) {
312 crop_unit_x = 1 + (sps->chroma_format_idc < 3);
313 crop_unit_y = 1 + (sps->chroma_format_idc < 2);
315 #define CROP(border, unit) do { \
316 if (ctx->crop_ ## border >= 0) { \
317 if (ctx->crop_ ## border % unit != 0) { \
318 av_log(bsf, AV_LOG_ERROR, "Invalid value for crop_%s: " \
319 "must be a multiple of %d.\n", #border, unit); \
320 return AVERROR(EINVAL); \
322 sps->conf_win_ ## border ## _offset = \
323 ctx->crop_ ## border / unit; \
324 sps->conformance_window_flag = 1; \
327 CROP(left, crop_unit_x);
328 CROP(right, crop_unit_x);
329 CROP(top, crop_unit_y);
330 CROP(bottom, crop_unit_y);
334 sps->vui_parameters_present_flag = 1;
336 h265_metadata_update_level(bsf, &sps->profile_tier_level.general_level_idc);
341 static int h265_metadata_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
343 H265MetadataContext *ctx = bsf->priv_data;
344 CodedBitstreamFragment *au = &ctx->access_unit;
349 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
354 err = ff_cbs_read(ctx->input, au, side_data, side_data_size);
356 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata from packet side data.\n");
360 if (ctx->level == LEVEL_AUTO && !ctx->level_guess)
361 h265_metadata_guess_level(bsf, au);
363 for (i = 0; i < au->nb_units; i++) {
364 if (au->units[i].type == HEVC_NAL_VPS) {
365 err = h265_metadata_update_vps(bsf, au->units[i].content);
369 if (au->units[i].type == HEVC_NAL_SPS) {
370 err = h265_metadata_update_sps(bsf, au->units[i].content);
376 err = ff_cbs_write_fragment_data(ctx->output, au);
378 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata into packet side data.\n");
382 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, au->data_size);
384 return AVERROR(ENOMEM);
385 memcpy(side_data, au->data, au->data_size);
387 ff_cbs_fragment_reset(au);
392 static int h265_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
394 H265MetadataContext *ctx = bsf->priv_data;
395 CodedBitstreamFragment *au = &ctx->access_unit;
398 err = ff_bsf_get_packet_ref(bsf, pkt);
402 err = h265_metadata_update_side_data(bsf, pkt);
406 err = ff_cbs_read_packet(ctx->input, au, pkt);
408 av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
412 if (au->nb_units == 0) {
413 av_log(bsf, AV_LOG_ERROR, "No NAL units in packet.\n");
414 err = AVERROR_INVALIDDATA;
418 // If an AUD is present, it must be the first NAL unit.
419 if (au->units[0].type == HEVC_NAL_AUD) {
420 if (ctx->aud == REMOVE)
421 ff_cbs_delete_unit(au, 0);
423 if (ctx->aud == INSERT) {
424 H265RawAUD *aud = &ctx->aud_nal;
425 int pic_type = 0, temporal_id = 8, layer_id = 0;
427 for (i = 0; i < au->nb_units; i++) {
428 const H265RawNALUnitHeader *nal = au->units[i].content;
431 if (nal->nuh_temporal_id_plus1 < temporal_id + 1)
432 temporal_id = nal->nuh_temporal_id_plus1 - 1;
434 if (au->units[i].type <= HEVC_NAL_RSV_VCL31) {
435 const H265RawSlice *slice = au->units[i].content;
436 layer_id = nal->nuh_layer_id;
437 if (slice->header.slice_type == HEVC_SLICE_B &&
440 if (slice->header.slice_type == HEVC_SLICE_P &&
446 aud->nal_unit_header = (H265RawNALUnitHeader) {
447 .nal_unit_type = HEVC_NAL_AUD,
448 .nuh_layer_id = layer_id,
449 .nuh_temporal_id_plus1 = temporal_id + 1,
451 aud->pic_type = pic_type;
453 err = ff_cbs_insert_unit_content(au, 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->output, pkt, au);
479 av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
485 ff_cbs_fragment_reset(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->input, AV_CODEC_ID_HEVC, bsf);
502 err = ff_cbs_init(&ctx->output, AV_CODEC_ID_HEVC, bsf);
506 if (bsf->par_in->extradata) {
507 err = ff_cbs_read_extradata(ctx->input, au, bsf->par_in);
509 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
513 if (ctx->level == LEVEL_AUTO)
514 h265_metadata_guess_level(bsf, au);
516 for (i = 0; i < au->nb_units; i++) {
517 if (au->units[i].type == HEVC_NAL_VPS) {
518 err = h265_metadata_update_vps(bsf, au->units[i].content);
522 if (au->units[i].type == HEVC_NAL_SPS) {
523 err = h265_metadata_update_sps(bsf, au->units[i].content);
529 err = ff_cbs_write_extradata(ctx->output, bsf->par_out, au);
531 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
538 ff_cbs_fragment_reset(au);
542 static void h265_metadata_close(AVBSFContext *bsf)
544 H265MetadataContext *ctx = bsf->priv_data;
546 ff_cbs_fragment_free(&ctx->access_unit);
547 ff_cbs_close(&ctx->input);
548 ff_cbs_close(&ctx->output);
551 #define OFFSET(x) offsetof(H265MetadataContext, x)
552 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
553 static const AVOption h265_metadata_options[] = {
554 { "aud", "Access Unit Delimiter NAL units",
555 OFFSET(aud), AV_OPT_TYPE_INT,
556 { .i64 = PASS }, PASS, REMOVE, FLAGS, "aud" },
557 { "pass", NULL, 0, AV_OPT_TYPE_CONST,
558 { .i64 = PASS }, .flags = FLAGS, .unit = "aud" },
559 { "insert", NULL, 0, AV_OPT_TYPE_CONST,
560 { .i64 = INSERT }, .flags = FLAGS, .unit = "aud" },
561 { "remove", NULL, 0, AV_OPT_TYPE_CONST,
562 { .i64 = REMOVE }, .flags = FLAGS, .unit = "aud" },
564 { "sample_aspect_ratio", "Set sample aspect ratio (table E-1)",
565 OFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL,
566 { .dbl = 0.0 }, 0, 65535, FLAGS },
568 { "video_format", "Set video format (table E-2)",
569 OFFSET(video_format), AV_OPT_TYPE_INT,
570 { .i64 = -1 }, -1, 7, FLAGS },
571 { "video_full_range_flag", "Set video full range flag",
572 OFFSET(video_full_range_flag), AV_OPT_TYPE_INT,
573 { .i64 = -1 }, -1, 1, FLAGS },
574 { "colour_primaries", "Set colour primaries (table E-3)",
575 OFFSET(colour_primaries), AV_OPT_TYPE_INT,
576 { .i64 = -1 }, -1, 255, FLAGS },
577 { "transfer_characteristics", "Set transfer characteristics (table E-4)",
578 OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
579 { .i64 = -1 }, -1, 255, FLAGS },
580 { "matrix_coefficients", "Set matrix coefficients (table E-5)",
581 OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
582 { .i64 = -1 }, -1, 255, FLAGS },
584 { "chroma_sample_loc_type", "Set chroma sample location type (figure E-1)",
585 OFFSET(chroma_sample_loc_type), AV_OPT_TYPE_INT,
586 { .i64 = -1 }, -1, 6, FLAGS },
589 "Set VPS and VUI tick rate (num_units_in_tick / time_scale)",
590 OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
591 { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
592 { "num_ticks_poc_diff_one",
593 "Set VPS and VUI number of ticks per POC increment",
594 OFFSET(num_ticks_poc_diff_one), AV_OPT_TYPE_INT,
595 { .i64 = -1 }, -1, INT_MAX, FLAGS },
597 { "crop_left", "Set left border crop offset",
598 OFFSET(crop_left), AV_OPT_TYPE_INT,
599 { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS },
600 { "crop_right", "Set right border crop offset",
601 OFFSET(crop_right), AV_OPT_TYPE_INT,
602 { .i64 = -1 }, -1, HEVC_MAX_WIDTH, FLAGS },
603 { "crop_top", "Set top border crop offset",
604 OFFSET(crop_top), AV_OPT_TYPE_INT,
605 { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS },
606 { "crop_bottom", "Set bottom border crop offset",
607 OFFSET(crop_bottom), AV_OPT_TYPE_INT,
608 { .i64 = -1 }, -1, HEVC_MAX_HEIGHT, FLAGS },
610 { "level", "Set level (tables A.6 and A.7)",
611 OFFSET(level), AV_OPT_TYPE_INT,
612 { .i64 = LEVEL_UNSET }, LEVEL_UNSET, 0xff, FLAGS, "level" },
613 { "auto", "Attempt to guess level from stream properties",
614 0, AV_OPT_TYPE_CONST,
615 { .i64 = LEVEL_AUTO }, .flags = FLAGS, .unit = "level" },
616 #define LEVEL(name, value) name, NULL, 0, AV_OPT_TYPE_CONST, \
617 { .i64 = value }, .flags = FLAGS, .unit = "level"
620 { LEVEL("2.1", 63) },
622 { LEVEL("3.1", 93) },
624 { LEVEL("4.1", 123) },
626 { LEVEL("5.1", 153) },
627 { LEVEL("5.2", 156) },
629 { LEVEL("6.1", 183) },
630 { LEVEL("6.2", 186) },
631 { LEVEL("8.5", 255) },
637 static const AVClass h265_metadata_class = {
638 .class_name = "h265_metadata_bsf",
639 .item_name = av_default_item_name,
640 .option = h265_metadata_options,
641 .version = LIBAVUTIL_VERSION_INT,
644 static const enum AVCodecID h265_metadata_codec_ids[] = {
645 AV_CODEC_ID_HEVC, AV_CODEC_ID_NONE,
648 const AVBitStreamFilter ff_hevc_metadata_bsf = {
649 .name = "hevc_metadata",
650 .priv_data_size = sizeof(H265MetadataContext),
651 .priv_class = &h265_metadata_class,
652 .init = &h265_metadata_init,
653 .close = &h265_metadata_close,
654 .filter = &h265_metadata_filter,
655 .codec_ids = h265_metadata_codec_ids,