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 typedef struct AV1MetadataContext {
33 int transfer_characteristics;
34 int matrix_coefficients;
37 int chroma_sample_position;
40 int num_ticks_per_picture;
46 static int av1_metadata_update_sequence_header(AVBSFContext *bsf,
47 AV1RawSequenceHeader *seq)
49 AV1MetadataContext *ctx = bsf->priv_data;
50 AV1RawColorConfig *clc = &seq->color_config;
51 AV1RawTimingInfo *tim = &seq->timing_info;
53 if (ctx->color_primaries >= 0 ||
54 ctx->transfer_characteristics >= 0 ||
55 ctx->matrix_coefficients >= 0) {
56 clc->color_description_present_flag = 1;
58 if (ctx->color_primaries >= 0)
59 clc->color_primaries = ctx->color_primaries;
60 if (ctx->transfer_characteristics >= 0)
61 clc->transfer_characteristics = ctx->transfer_characteristics;
62 if (ctx->matrix_coefficients >= 0)
63 clc->matrix_coefficients = ctx->matrix_coefficients;
66 if (ctx->color_range >= 0) {
67 if (clc->color_primaries == AVCOL_PRI_BT709 &&
68 clc->transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
69 clc->matrix_coefficients == AVCOL_SPC_RGB) {
70 av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot be set "
71 "on RGB streams encoded in BT.709 sRGB.\n");
73 clc->color_range = ctx->color_range;
77 if (ctx->chroma_sample_position >= 0) {
78 if (clc->mono_chrome || !clc->subsampling_x || !clc->subsampling_y) {
79 av_log(bsf, AV_LOG_WARNING, "Warning: chroma_sample_position "
80 "can only be set for 4:2:0 streams.\n");
82 clc->chroma_sample_position = ctx->chroma_sample_position;
86 if (ctx->tick_rate.num && ctx->tick_rate.den) {
89 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
90 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
92 tim->time_scale = num;
93 tim->num_units_in_display_tick = den;
94 seq->timing_info_present_flag = 1;
96 if (ctx->num_ticks_per_picture > 0) {
97 tim->equal_picture_interval = 1;
98 tim->num_ticks_per_picture_minus_1 =
99 ctx->num_ticks_per_picture - 1;
106 static int av1_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
107 CodedBitstreamFragment *frag)
109 AV1MetadataContext *ctx = bsf->priv_data;
113 for (i = 0; i < frag->nb_units; i++) {
114 if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
115 obu = frag->units[i].content;
116 err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
122 // If a Temporal Delimiter is present, it must be the first OBU.
123 if (frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
124 if (ctx->td == BSF_ELEMENT_REMOVE)
125 ff_cbs_delete_unit(frag, 0);
126 } else if (pkt && ctx->td == BSF_ELEMENT_INSERT) {
128 .header.obu_type = AV1_OBU_TEMPORAL_DELIMITER,
131 err = ff_cbs_insert_unit_content(frag, 0, AV1_OBU_TEMPORAL_DELIMITER,
134 av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n");
139 if (ctx->delete_padding) {
140 for (i = frag->nb_units - 1; i >= 0; i--) {
141 if (frag->units[i].type == AV1_OBU_PADDING)
142 ff_cbs_delete_unit(frag, i);
149 static const CBSBSFType av1_metadata_type = {
150 .codec_id = AV_CODEC_ID_AV1,
151 .fragment_name = "temporal unit",
153 .update_fragment = &av1_metadata_update_fragment,
156 static int av1_metadata_init(AVBSFContext *bsf)
158 return ff_cbs_bsf_generic_init(bsf, &av1_metadata_type);
161 #define OFFSET(x) offsetof(AV1MetadataContext, x)
162 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
163 static const AVOption av1_metadata_options[] = {
164 BSF_ELEMENT_OPTIONS_PIR("td", "Temporal Delimiter OBU",
167 { "color_primaries", "Set color primaries (section 6.4.2)",
168 OFFSET(color_primaries), AV_OPT_TYPE_INT,
169 { .i64 = -1 }, -1, 255, FLAGS },
170 { "transfer_characteristics", "Set transfer characteristics (section 6.4.2)",
171 OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
172 { .i64 = -1 }, -1, 255, FLAGS },
173 { "matrix_coefficients", "Set matrix coefficients (section 6.4.2)",
174 OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
175 { .i64 = -1 }, -1, 255, FLAGS },
177 { "color_range", "Set color range flag (section 6.4.2)",
178 OFFSET(color_range), AV_OPT_TYPE_INT,
179 { .i64 = -1 }, -1, 1, FLAGS, "cr" },
180 { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
181 { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
182 { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
183 { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
185 { "chroma_sample_position", "Set chroma sample position (section 6.4.2)",
186 OFFSET(chroma_sample_position), AV_OPT_TYPE_INT,
187 { .i64 = -1 }, -1, 3, FLAGS, "csp" },
188 { "unknown", "Unknown chroma sample position", 0, AV_OPT_TYPE_CONST,
189 { .i64 = AV1_CSP_UNKNOWN }, .flags = FLAGS, .unit = "csp" },
190 { "vertical", "Left chroma sample position", 0, AV_OPT_TYPE_CONST,
191 { .i64 = AV1_CSP_VERTICAL }, .flags = FLAGS, .unit = "csp" },
192 { "colocated", "Top-left chroma sample position", 0, AV_OPT_TYPE_CONST,
193 { .i64 = AV1_CSP_COLOCATED }, .flags = FLAGS, .unit = "csp" },
195 { "tick_rate", "Set display tick rate (num_units_in_display_tick / time_scale)",
196 OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
197 { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
198 { "num_ticks_per_picture", "Set display ticks per picture for CFR streams",
199 OFFSET(num_ticks_per_picture), AV_OPT_TYPE_INT,
200 { .i64 = -1 }, -1, INT_MAX, FLAGS },
202 { "delete_padding", "Delete all Padding OBUs",
203 OFFSET(delete_padding), AV_OPT_TYPE_BOOL,
204 { .i64 = 0 }, 0, 1, FLAGS},
209 static const AVClass av1_metadata_class = {
210 .class_name = "av1_metadata_bsf",
211 .item_name = av_default_item_name,
212 .option = av1_metadata_options,
213 .version = LIBAVUTIL_VERSION_INT,
216 static const enum AVCodecID av1_metadata_codec_ids[] = {
217 AV_CODEC_ID_AV1, AV_CODEC_ID_NONE,
220 const AVBitStreamFilter ff_av1_metadata_bsf = {
221 .name = "av1_metadata",
222 .priv_data_size = sizeof(AV1MetadataContext),
223 .priv_class = &av1_metadata_class,
224 .init = &av1_metadata_init,
225 .close = &ff_cbs_bsf_generic_close,
226 .filter = &ff_cbs_bsf_generic_filter,
227 .codec_ids = av1_metadata_codec_ids,