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"
33 typedef struct AV1MetadataContext {
36 CodedBitstreamContext *cbc;
37 CodedBitstreamFragment access_unit;
42 int transfer_characteristics;
43 int matrix_coefficients;
46 int chroma_sample_position;
49 int num_ticks_per_picture;
55 static int av1_metadata_update_sequence_header(AVBSFContext *bsf,
56 AV1RawSequenceHeader *seq)
58 AV1MetadataContext *ctx = bsf->priv_data;
59 AV1RawColorConfig *clc = &seq->color_config;
60 AV1RawTimingInfo *tim = &seq->timing_info;
62 if (ctx->color_primaries >= 0 ||
63 ctx->transfer_characteristics >= 0 ||
64 ctx->matrix_coefficients >= 0) {
65 clc->color_description_present_flag = 1;
67 if (ctx->color_primaries >= 0)
68 clc->color_primaries = ctx->color_primaries;
69 if (ctx->transfer_characteristics >= 0)
70 clc->transfer_characteristics = ctx->transfer_characteristics;
71 if (ctx->matrix_coefficients >= 0)
72 clc->matrix_coefficients = ctx->matrix_coefficients;
75 if (ctx->color_range >= 0) {
76 if (clc->color_primaries == AVCOL_PRI_BT709 &&
77 clc->transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
78 clc->matrix_coefficients == AVCOL_SPC_RGB) {
79 av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot be set "
80 "on RGB streams encoded in BT.709 sRGB.\n");
82 clc->color_range = ctx->color_range;
86 if (ctx->chroma_sample_position >= 0) {
87 if (clc->mono_chrome || !clc->subsampling_x || !clc->subsampling_y) {
88 av_log(bsf, AV_LOG_WARNING, "Warning: chroma_sample_position "
89 "can only be set for 4:2:0 streams.\n");
91 clc->chroma_sample_position = ctx->chroma_sample_position;
95 if (ctx->tick_rate.num && ctx->tick_rate.den) {
98 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
99 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
101 tim->time_scale = num;
102 tim->num_units_in_display_tick = den;
103 seq->timing_info_present_flag = 1;
105 if (ctx->num_ticks_per_picture > 0) {
106 tim->equal_picture_interval = 1;
107 tim->num_ticks_per_picture_minus_1 =
108 ctx->num_ticks_per_picture - 1;
115 static int av1_metadata_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
117 AV1MetadataContext *ctx = bsf->priv_data;
118 CodedBitstreamFragment *frag = &ctx->access_unit;
123 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
128 err = ff_cbs_read(ctx->cbc, frag, side_data, side_data_size);
130 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata from packet side data.\n");
134 for (i = 0; i < frag->nb_units; i++) {
135 if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
136 AV1RawOBU *obu = frag->units[i].content;
137 err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
143 err = ff_cbs_write_fragment_data(ctx->cbc, frag);
145 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata into packet side data.\n");
149 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, frag->data_size);
151 return AVERROR(ENOMEM);
152 memcpy(side_data, frag->data, frag->data_size);
154 ff_cbs_fragment_reset(frag);
159 static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
161 AV1MetadataContext *ctx = bsf->priv_data;
162 CodedBitstreamFragment *frag = &ctx->access_unit;
166 err = ff_bsf_get_packet_ref(bsf, pkt);
170 err = av1_metadata_update_side_data(bsf, pkt);
174 err = ff_cbs_read_packet(ctx->cbc, frag, pkt);
176 av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
180 if (frag->nb_units == 0) {
181 av_log(bsf, AV_LOG_ERROR, "No OBU in packet.\n");
182 err = AVERROR_INVALIDDATA;
186 for (i = 0; i < frag->nb_units; i++) {
187 if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
188 obu = frag->units[i].content;
189 err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
195 // If a Temporal Delimiter is present, it must be the first OBU.
196 if (frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
197 if (ctx->td == REMOVE)
198 ff_cbs_delete_unit(frag, 0);
199 } else if (ctx->td == INSERT) {
201 .header.obu_type = AV1_OBU_TEMPORAL_DELIMITER,
204 err = ff_cbs_insert_unit_content(frag, 0, AV1_OBU_TEMPORAL_DELIMITER,
207 av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n");
212 if (ctx->delete_padding) {
213 for (i = frag->nb_units - 1; i >= 0; i--) {
214 if (frag->units[i].type == AV1_OBU_PADDING)
215 ff_cbs_delete_unit(frag, i);
219 err = ff_cbs_write_packet(ctx->cbc, pkt, frag);
221 av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
227 ff_cbs_fragment_reset(frag);
230 av_packet_unref(pkt);
235 static int av1_metadata_init(AVBSFContext *bsf)
237 AV1MetadataContext *ctx = bsf->priv_data;
238 CodedBitstreamFragment *frag = &ctx->access_unit;
242 err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_AV1, bsf);
246 if (bsf->par_in->extradata) {
247 err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in);
249 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
253 for (i = 0; i < frag->nb_units; i++) {
254 if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
255 obu = frag->units[i].content;
256 err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
262 err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, frag);
264 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
271 ff_cbs_fragment_reset(frag);
275 static void av1_metadata_close(AVBSFContext *bsf)
277 AV1MetadataContext *ctx = bsf->priv_data;
279 ff_cbs_fragment_free(&ctx->access_unit);
280 ff_cbs_close(&ctx->cbc);
283 #define OFFSET(x) offsetof(AV1MetadataContext, x)
284 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
285 static const AVOption av1_metadata_options[] = {
286 { "td", "Temporal Delimiter OBU",
287 OFFSET(td), AV_OPT_TYPE_INT,
288 { .i64 = PASS }, PASS, REMOVE, FLAGS, "td" },
289 { "pass", NULL, 0, AV_OPT_TYPE_CONST,
290 { .i64 = PASS }, .flags = FLAGS, .unit = "td" },
291 { "insert", NULL, 0, AV_OPT_TYPE_CONST,
292 { .i64 = INSERT }, .flags = FLAGS, .unit = "td" },
293 { "remove", NULL, 0, AV_OPT_TYPE_CONST,
294 { .i64 = REMOVE }, .flags = FLAGS, .unit = "td" },
296 { "color_primaries", "Set color primaries (section 6.4.2)",
297 OFFSET(color_primaries), AV_OPT_TYPE_INT,
298 { .i64 = -1 }, -1, 255, FLAGS },
299 { "transfer_characteristics", "Set transfer characteristics (section 6.4.2)",
300 OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
301 { .i64 = -1 }, -1, 255, FLAGS },
302 { "matrix_coefficients", "Set matrix coefficients (section 6.4.2)",
303 OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
304 { .i64 = -1 }, -1, 255, FLAGS },
306 { "color_range", "Set color range flag (section 6.4.2)",
307 OFFSET(color_range), AV_OPT_TYPE_INT,
308 { .i64 = -1 }, -1, 1, FLAGS, "cr" },
309 { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
310 { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
311 { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
312 { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
314 { "chroma_sample_position", "Set chroma sample position (section 6.4.2)",
315 OFFSET(chroma_sample_position), AV_OPT_TYPE_INT,
316 { .i64 = -1 }, -1, 3, FLAGS, "csp" },
317 { "unknown", "Unknown chroma sample position", 0, AV_OPT_TYPE_CONST,
318 { .i64 = AV1_CSP_UNKNOWN }, .flags = FLAGS, .unit = "csp" },
319 { "vertical", "Left chroma sample position", 0, AV_OPT_TYPE_CONST,
320 { .i64 = AV1_CSP_VERTICAL }, .flags = FLAGS, .unit = "csp" },
321 { "colocated", "Top-left chroma sample position", 0, AV_OPT_TYPE_CONST,
322 { .i64 = AV1_CSP_COLOCATED }, .flags = FLAGS, .unit = "csp" },
324 { "tick_rate", "Set display tick rate (num_units_in_display_tick / time_scale)",
325 OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
326 { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
327 { "num_ticks_per_picture", "Set display ticks per picture for CFR streams",
328 OFFSET(num_ticks_per_picture), AV_OPT_TYPE_INT,
329 { .i64 = -1 }, -1, INT_MAX, FLAGS },
331 { "delete_padding", "Delete all Padding OBUs",
332 OFFSET(delete_padding), AV_OPT_TYPE_BOOL,
333 { .i64 = 0 }, 0, 1, FLAGS},
338 static const AVClass av1_metadata_class = {
339 .class_name = "av1_metadata_bsf",
340 .item_name = av_default_item_name,
341 .option = av1_metadata_options,
342 .version = LIBAVUTIL_VERSION_INT,
345 static const enum AVCodecID av1_metadata_codec_ids[] = {
346 AV_CODEC_ID_AV1, AV_CODEC_ID_NONE,
349 const AVBitStreamFilter ff_av1_metadata_bsf = {
350 .name = "av1_metadata",
351 .priv_data_size = sizeof(AV1MetadataContext),
352 .priv_class = &av1_metadata_class,
353 .init = &av1_metadata_init,
354 .close = &av1_metadata_close,
355 .filter = &av1_metadata_filter,
356 .codec_ids = av1_metadata_codec_ids,