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 {
34 int transfer_characteristics;
35 int matrix_coefficients;
38 int chroma_sample_position;
41 int num_ticks_per_picture;
47 static int av1_metadata_update_sequence_header(AVBSFContext *bsf,
48 AV1RawSequenceHeader *seq)
50 AV1MetadataContext *ctx = bsf->priv_data;
51 AV1RawColorConfig *clc = &seq->color_config;
52 AV1RawTimingInfo *tim = &seq->timing_info;
54 if (ctx->color_primaries >= 0 ||
55 ctx->transfer_characteristics >= 0 ||
56 ctx->matrix_coefficients >= 0) {
57 clc->color_description_present_flag = 1;
59 if (ctx->color_primaries >= 0)
60 clc->color_primaries = ctx->color_primaries;
61 if (ctx->transfer_characteristics >= 0)
62 clc->transfer_characteristics = ctx->transfer_characteristics;
63 if (ctx->matrix_coefficients >= 0)
64 clc->matrix_coefficients = ctx->matrix_coefficients;
67 if (ctx->color_range >= 0) {
68 if (clc->color_primaries == AVCOL_PRI_BT709 &&
69 clc->transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
70 clc->matrix_coefficients == AVCOL_SPC_RGB) {
71 av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot be set "
72 "on RGB streams encoded in BT.709 sRGB.\n");
74 clc->color_range = ctx->color_range;
78 if (ctx->chroma_sample_position >= 0) {
79 if (clc->mono_chrome || !clc->subsampling_x || !clc->subsampling_y) {
80 av_log(bsf, AV_LOG_WARNING, "Warning: chroma_sample_position "
81 "can only be set for 4:2:0 streams.\n");
83 clc->chroma_sample_position = ctx->chroma_sample_position;
87 if (ctx->tick_rate.num && ctx->tick_rate.den) {
90 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
91 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
93 tim->time_scale = num;
94 tim->num_units_in_display_tick = den;
95 seq->timing_info_present_flag = 1;
97 if (ctx->num_ticks_per_picture > 0) {
98 tim->equal_picture_interval = 1;
99 tim->num_ticks_per_picture_minus_1 =
100 ctx->num_ticks_per_picture - 1;
107 static int av1_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt,
108 CodedBitstreamFragment *frag)
110 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 AV1RawOBU *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->nb_units && 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) {
127 err = ff_cbs_insert_unit_content(frag, 0, AV1_OBU_TEMPORAL_DELIMITER,
130 av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n");
135 if (ctx->delete_padding) {
136 for (i = frag->nb_units - 1; i >= 0; i--) {
137 if (frag->units[i].type == AV1_OBU_PADDING)
138 ff_cbs_delete_unit(frag, i);
145 static const CBSBSFType av1_metadata_type = {
146 .codec_id = AV_CODEC_ID_AV1,
147 .fragment_name = "temporal unit",
149 .update_fragment = &av1_metadata_update_fragment,
152 static int av1_metadata_init(AVBSFContext *bsf)
154 AV1MetadataContext *ctx = bsf->priv_data;
156 ctx->td_obu = (AV1RawOBU) {
157 .header.obu_type = AV1_OBU_TEMPORAL_DELIMITER,
160 return ff_cbs_bsf_generic_init(bsf, &av1_metadata_type);
163 #define OFFSET(x) offsetof(AV1MetadataContext, x)
164 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
165 static const AVOption av1_metadata_options[] = {
166 BSF_ELEMENT_OPTIONS_PIR("td", "Temporal Delimiter OBU",
169 { "color_primaries", "Set color primaries (section 6.4.2)",
170 OFFSET(color_primaries), AV_OPT_TYPE_INT,
171 { .i64 = -1 }, -1, 255, FLAGS },
172 { "transfer_characteristics", "Set transfer characteristics (section 6.4.2)",
173 OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
174 { .i64 = -1 }, -1, 255, FLAGS },
175 { "matrix_coefficients", "Set matrix coefficients (section 6.4.2)",
176 OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
177 { .i64 = -1 }, -1, 255, FLAGS },
179 { "color_range", "Set color range flag (section 6.4.2)",
180 OFFSET(color_range), AV_OPT_TYPE_INT,
181 { .i64 = -1 }, -1, 1, FLAGS, "cr" },
182 { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
183 { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
184 { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
185 { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
187 { "chroma_sample_position", "Set chroma sample position (section 6.4.2)",
188 OFFSET(chroma_sample_position), AV_OPT_TYPE_INT,
189 { .i64 = -1 }, -1, 3, FLAGS, "csp" },
190 { "unknown", "Unknown chroma sample position", 0, AV_OPT_TYPE_CONST,
191 { .i64 = AV1_CSP_UNKNOWN }, .flags = FLAGS, .unit = "csp" },
192 { "vertical", "Left chroma sample position", 0, AV_OPT_TYPE_CONST,
193 { .i64 = AV1_CSP_VERTICAL }, .flags = FLAGS, .unit = "csp" },
194 { "colocated", "Top-left chroma sample position", 0, AV_OPT_TYPE_CONST,
195 { .i64 = AV1_CSP_COLOCATED }, .flags = FLAGS, .unit = "csp" },
197 { "tick_rate", "Set display tick rate (num_units_in_display_tick / time_scale)",
198 OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
199 { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
200 { "num_ticks_per_picture", "Set display ticks per picture for CFR streams",
201 OFFSET(num_ticks_per_picture), AV_OPT_TYPE_INT,
202 { .i64 = -1 }, -1, INT_MAX, FLAGS },
204 { "delete_padding", "Delete all Padding OBUs",
205 OFFSET(delete_padding), AV_OPT_TYPE_BOOL,
206 { .i64 = 0 }, 0, 1, FLAGS},
211 static const AVClass av1_metadata_class = {
212 .class_name = "av1_metadata_bsf",
213 .item_name = av_default_item_name,
214 .option = av1_metadata_options,
215 .version = LIBAVUTIL_VERSION_INT,
218 static const enum AVCodecID av1_metadata_codec_ids[] = {
219 AV_CODEC_ID_AV1, AV_CODEC_ID_NONE,
222 const AVBitStreamFilter ff_av1_metadata_bsf = {
223 .name = "av1_metadata",
224 .priv_data_size = sizeof(AV1MetadataContext),
225 .priv_class = &av1_metadata_class,
226 .init = &av1_metadata_init,
227 .close = &ff_cbs_bsf_generic_close,
228 .filter = &ff_cbs_bsf_generic_filter,
229 .codec_ids = av1_metadata_codec_ids,