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"
32 typedef struct AV1MetadataContext {
35 CodedBitstreamContext *cbc;
36 CodedBitstreamFragment access_unit;
41 int transfer_characteristics;
42 int matrix_coefficients;
45 int chroma_sample_position;
48 int num_ticks_per_picture;
52 static int av1_metadata_update_sequence_header(AVBSFContext *bsf,
53 AV1RawSequenceHeader *seq)
55 AV1MetadataContext *ctx = bsf->priv_data;
56 AV1RawColorConfig *clc = &seq->color_config;
57 AV1RawTimingInfo *tim = &seq->timing_info;
59 if (ctx->color_primaries >= 0 ||
60 ctx->transfer_characteristics >= 0 ||
61 ctx->matrix_coefficients >= 0) {
62 if (!clc->color_description_present_flag) {
63 clc->color_description_present_flag = 1;
64 clc->color_primaries = AVCOL_PRI_UNSPECIFIED;
65 clc->transfer_characteristics = AVCOL_TRC_UNSPECIFIED;
66 clc->matrix_coefficients = AVCOL_SPC_UNSPECIFIED;
69 if (ctx->color_primaries >= 0)
70 clc->color_primaries = ctx->color_primaries;
71 if (ctx->transfer_characteristics >= 0)
72 clc->transfer_characteristics = ctx->transfer_characteristics;
73 if (ctx->matrix_coefficients >= 0)
74 clc->matrix_coefficients = ctx->matrix_coefficients;
77 if (ctx->color_range >= 0) {
78 if (clc->color_primaries == AVCOL_PRI_BT709 &&
79 clc->transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
80 clc->matrix_coefficients == AVCOL_SPC_RGB) {
81 av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot be set "
82 "on RGB streams encoded in BT.709 sRGB.\n");
84 clc->color_range = ctx->color_range;
88 if (ctx->chroma_sample_position >= 0) {
89 if (clc->mono_chrome) {
90 av_log(bsf, AV_LOG_WARNING, "Warning: chroma_sample_position "
91 "is not meaningful for monochrome streams.\n");
92 } else if (clc->subsampling_x == 0 &&
93 clc->subsampling_y == 0) {
94 av_log(bsf, AV_LOG_WARNING, "Warning: chroma_sample_position "
95 "is not meaningful for non-chroma-subsampled streams.\n");
97 clc->chroma_sample_position = ctx->chroma_sample_position;
101 if (ctx->tick_rate.num && ctx->tick_rate.den) {
104 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
105 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
107 tim->time_scale = num;
108 tim->num_units_in_display_tick = den;
109 seq->timing_info_present_flag = 1;
111 if (ctx->num_ticks_per_picture > 0) {
112 tim->equal_picture_interval = 1;
113 tim->num_ticks_per_picture_minus_1 =
114 ctx->num_ticks_per_picture - 1;
121 static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *out)
123 AV1MetadataContext *ctx = bsf->priv_data;
125 CodedBitstreamFragment *frag = &ctx->access_unit;
129 err = ff_bsf_get_packet(bsf, &in);
133 err = ff_cbs_read_packet(ctx->cbc, frag, in);
135 av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
139 for (i = 0; i < frag->nb_units; i++) {
140 if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
141 obu = frag->units[i].content;
142 err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
148 // If a Temporal Delimiter is present, it must be the first OBU.
149 if (frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
150 if (ctx->td == REMOVE)
151 ff_cbs_delete_unit(ctx->cbc, frag, 0);
152 } else if (ctx->td == INSERT) {
154 .header.obu_type = AV1_OBU_TEMPORAL_DELIMITER,
157 err = ff_cbs_insert_unit_content(ctx->cbc, frag, 0, AV1_OBU_TEMPORAL_DELIMITER,
160 av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n");
165 err = ff_cbs_write_packet(ctx->cbc, out, frag);
167 av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
171 err = av_packet_copy_props(out, in);
177 ff_cbs_fragment_uninit(ctx->cbc, frag);
180 av_packet_unref(out);
186 static int av1_metadata_init(AVBSFContext *bsf)
188 AV1MetadataContext *ctx = bsf->priv_data;
189 CodedBitstreamFragment *frag = &ctx->access_unit;
193 err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_AV1, bsf);
197 if (bsf->par_in->extradata) {
198 err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in);
200 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
204 for (i = 0; i < frag->nb_units; i++) {
205 if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
206 obu = frag->units[i].content;
207 err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
213 err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, frag);
215 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
222 ff_cbs_fragment_uninit(ctx->cbc, frag);
226 static void av1_metadata_close(AVBSFContext *bsf)
228 AV1MetadataContext *ctx = bsf->priv_data;
229 ff_cbs_close(&ctx->cbc);
232 #define OFFSET(x) offsetof(AV1MetadataContext, x)
233 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
234 static const AVOption av1_metadata_options[] = {
235 { "td", "Temporal Delimiter OBU",
236 OFFSET(td), AV_OPT_TYPE_INT,
237 { .i64 = PASS }, PASS, REMOVE, FLAGS, "td" },
238 { "pass", NULL, 0, AV_OPT_TYPE_CONST,
239 { .i64 = PASS }, .flags = FLAGS, .unit = "td" },
240 { "insert", NULL, 0, AV_OPT_TYPE_CONST,
241 { .i64 = INSERT }, .flags = FLAGS, .unit = "td" },
242 { "remove", NULL, 0, AV_OPT_TYPE_CONST,
243 { .i64 = REMOVE }, .flags = FLAGS, .unit = "td" },
245 { "color_primaries", "Set color primaries (section 6.4.2)",
246 OFFSET(color_primaries), AV_OPT_TYPE_INT,
247 { .i64 = -1 }, -1, 255, FLAGS },
248 { "transfer_characteristics", "Set transfer characteristics (section 6.4.2)",
249 OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
250 { .i64 = -1 }, -1, 255, FLAGS },
251 { "matrix_coefficients", "Set matrix coefficients (section 6.4.2)",
252 OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
253 { .i64 = -1 }, -1, 255, FLAGS },
255 { "color_range", "Set color range flag (section 6.4.2)",
256 OFFSET(color_range), AV_OPT_TYPE_INT,
257 { .i64 = -1 }, -1, 1, FLAGS, "cr" },
258 { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
259 { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
260 { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
261 { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
263 { "chroma_sample_position", "Set chroma sample position (section 6.4.2)",
264 OFFSET(chroma_sample_position), AV_OPT_TYPE_INT,
265 { .i64 = -1 }, -1, 3, FLAGS, "csp" },
266 { "unknown", "Unknown chroma sample position", 0, AV_OPT_TYPE_CONST,
267 { .i64 = AV1_CSP_UNKNOWN }, .flags = FLAGS, .unit = "csp" },
268 { "vertical", "Left chroma sample position", 0, AV_OPT_TYPE_CONST,
269 { .i64 = AV1_CSP_VERTICAL }, .flags = FLAGS, .unit = "csp" },
270 { "colocated", "Top-left chroma sample position", 0, AV_OPT_TYPE_CONST,
271 { .i64 = AV1_CSP_COLOCATED }, .flags = FLAGS, .unit = "csp" },
273 { "tick_rate", "Set display tick rate (num_units_in_display_tick / time_scale)",
274 OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
275 { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
276 { "num_ticks_per_picture", "Set display ticks per picture for CFR streams",
277 OFFSET(num_ticks_per_picture), AV_OPT_TYPE_INT,
278 { .i64 = -1 }, -1, INT_MAX, FLAGS },
283 static const AVClass av1_metadata_class = {
284 .class_name = "av1_metadata_bsf",
285 .item_name = av_default_item_name,
286 .option = av1_metadata_options,
287 .version = LIBAVUTIL_VERSION_INT,
290 static const enum AVCodecID av1_metadata_codec_ids[] = {
291 AV_CODEC_ID_AV1, AV_CODEC_ID_NONE,
294 const AVBitStreamFilter ff_av1_metadata_bsf = {
295 .name = "av1_metadata",
296 .priv_data_size = sizeof(AV1MetadataContext),
297 .priv_class = &av1_metadata_class,
298 .init = &av1_metadata_init,
299 .close = &av1_metadata_close,
300 .filter = &av1_metadata_filter,
301 .codec_ids = av1_metadata_codec_ids,