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 *input;
37 CodedBitstreamContext *output;
38 CodedBitstreamFragment access_unit;
43 int transfer_characteristics;
44 int matrix_coefficients;
47 int chroma_sample_position;
50 int num_ticks_per_picture;
56 static int av1_metadata_update_sequence_header(AVBSFContext *bsf,
57 AV1RawSequenceHeader *seq)
59 AV1MetadataContext *ctx = bsf->priv_data;
60 AV1RawColorConfig *clc = &seq->color_config;
61 AV1RawTimingInfo *tim = &seq->timing_info;
63 if (ctx->color_primaries >= 0 ||
64 ctx->transfer_characteristics >= 0 ||
65 ctx->matrix_coefficients >= 0) {
66 clc->color_description_present_flag = 1;
68 if (ctx->color_primaries >= 0)
69 clc->color_primaries = ctx->color_primaries;
70 if (ctx->transfer_characteristics >= 0)
71 clc->transfer_characteristics = ctx->transfer_characteristics;
72 if (ctx->matrix_coefficients >= 0)
73 clc->matrix_coefficients = ctx->matrix_coefficients;
76 if (ctx->color_range >= 0) {
77 if (clc->color_primaries == AVCOL_PRI_BT709 &&
78 clc->transfer_characteristics == AVCOL_TRC_IEC61966_2_1 &&
79 clc->matrix_coefficients == AVCOL_SPC_RGB) {
80 av_log(bsf, AV_LOG_WARNING, "Warning: color_range cannot be set "
81 "on RGB streams encoded in BT.709 sRGB.\n");
83 clc->color_range = ctx->color_range;
87 if (ctx->chroma_sample_position >= 0) {
88 if (clc->mono_chrome || !clc->subsampling_x || !clc->subsampling_y) {
89 av_log(bsf, AV_LOG_WARNING, "Warning: chroma_sample_position "
90 "can only be set for 4:2:0 streams.\n");
92 clc->chroma_sample_position = ctx->chroma_sample_position;
96 if (ctx->tick_rate.num && ctx->tick_rate.den) {
99 av_reduce(&num, &den, ctx->tick_rate.num, ctx->tick_rate.den,
100 UINT32_MAX > INT_MAX ? UINT32_MAX : INT_MAX);
102 tim->time_scale = num;
103 tim->num_units_in_display_tick = den;
104 seq->timing_info_present_flag = 1;
106 if (ctx->num_ticks_per_picture > 0) {
107 tim->equal_picture_interval = 1;
108 tim->num_ticks_per_picture_minus_1 =
109 ctx->num_ticks_per_picture - 1;
116 static int av1_metadata_update_side_data(AVBSFContext *bsf, AVPacket *pkt)
118 AV1MetadataContext *ctx = bsf->priv_data;
119 CodedBitstreamFragment *frag = &ctx->access_unit;
124 side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA,
129 err = ff_cbs_read(ctx->input, frag, side_data, side_data_size);
131 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata from packet side data.\n");
135 for (i = 0; i < frag->nb_units; i++) {
136 if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
137 AV1RawOBU *obu = frag->units[i].content;
138 err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
144 err = ff_cbs_write_fragment_data(ctx->output, frag);
146 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata into packet side data.\n");
150 side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, frag->data_size);
152 return AVERROR(ENOMEM);
153 memcpy(side_data, frag->data, frag->data_size);
155 ff_cbs_fragment_reset(frag);
160 static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
162 AV1MetadataContext *ctx = bsf->priv_data;
163 CodedBitstreamFragment *frag = &ctx->access_unit;
167 err = ff_bsf_get_packet_ref(bsf, pkt);
171 err = av1_metadata_update_side_data(bsf, pkt);
175 err = ff_cbs_read_packet(ctx->input, frag, pkt);
177 av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n");
181 if (frag->nb_units == 0) {
182 av_log(bsf, AV_LOG_ERROR, "No OBU in packet.\n");
183 err = AVERROR_INVALIDDATA;
187 for (i = 0; i < frag->nb_units; i++) {
188 if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
189 obu = frag->units[i].content;
190 err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
196 // If a Temporal Delimiter is present, it must be the first OBU.
197 if (frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) {
198 if (ctx->td == REMOVE)
199 ff_cbs_delete_unit(frag, 0);
200 } else if (ctx->td == INSERT) {
202 .header.obu_type = AV1_OBU_TEMPORAL_DELIMITER,
205 err = ff_cbs_insert_unit_content(frag, 0, AV1_OBU_TEMPORAL_DELIMITER,
208 av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n");
213 if (ctx->delete_padding) {
214 for (i = frag->nb_units - 1; i >= 0; i--) {
215 if (frag->units[i].type == AV1_OBU_PADDING)
216 ff_cbs_delete_unit(frag, i);
220 err = ff_cbs_write_packet(ctx->output, pkt, frag);
222 av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
228 ff_cbs_fragment_reset(frag);
231 av_packet_unref(pkt);
236 static int av1_metadata_init(AVBSFContext *bsf)
238 AV1MetadataContext *ctx = bsf->priv_data;
239 CodedBitstreamFragment *frag = &ctx->access_unit;
243 err = ff_cbs_init(&ctx->input, AV_CODEC_ID_AV1, bsf);
246 err = ff_cbs_init(&ctx->output, AV_CODEC_ID_AV1, bsf);
250 if (bsf->par_in->extradata) {
251 err = ff_cbs_read_extradata(ctx->input, frag, bsf->par_in);
253 av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n");
257 for (i = 0; i < frag->nb_units; i++) {
258 if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
259 obu = frag->units[i].content;
260 err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
266 err = ff_cbs_write_extradata(ctx->output, bsf->par_out, frag);
268 av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n");
275 ff_cbs_fragment_reset(frag);
279 static void av1_metadata_close(AVBSFContext *bsf)
281 AV1MetadataContext *ctx = bsf->priv_data;
283 ff_cbs_fragment_free(&ctx->access_unit);
284 ff_cbs_close(&ctx->input);
285 ff_cbs_close(&ctx->output);
288 #define OFFSET(x) offsetof(AV1MetadataContext, x)
289 #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM)
290 static const AVOption av1_metadata_options[] = {
291 { "td", "Temporal Delimiter OBU",
292 OFFSET(td), AV_OPT_TYPE_INT,
293 { .i64 = PASS }, PASS, REMOVE, FLAGS, "td" },
294 { "pass", NULL, 0, AV_OPT_TYPE_CONST,
295 { .i64 = PASS }, .flags = FLAGS, .unit = "td" },
296 { "insert", NULL, 0, AV_OPT_TYPE_CONST,
297 { .i64 = INSERT }, .flags = FLAGS, .unit = "td" },
298 { "remove", NULL, 0, AV_OPT_TYPE_CONST,
299 { .i64 = REMOVE }, .flags = FLAGS, .unit = "td" },
301 { "color_primaries", "Set color primaries (section 6.4.2)",
302 OFFSET(color_primaries), AV_OPT_TYPE_INT,
303 { .i64 = -1 }, -1, 255, FLAGS },
304 { "transfer_characteristics", "Set transfer characteristics (section 6.4.2)",
305 OFFSET(transfer_characteristics), AV_OPT_TYPE_INT,
306 { .i64 = -1 }, -1, 255, FLAGS },
307 { "matrix_coefficients", "Set matrix coefficients (section 6.4.2)",
308 OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
309 { .i64 = -1 }, -1, 255, FLAGS },
311 { "color_range", "Set color range flag (section 6.4.2)",
312 OFFSET(color_range), AV_OPT_TYPE_INT,
313 { .i64 = -1 }, -1, 1, FLAGS, "cr" },
314 { "tv", "TV (limited) range", 0, AV_OPT_TYPE_CONST,
315 { .i64 = 0 }, .flags = FLAGS, .unit = "cr" },
316 { "pc", "PC (full) range", 0, AV_OPT_TYPE_CONST,
317 { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
319 { "chroma_sample_position", "Set chroma sample position (section 6.4.2)",
320 OFFSET(chroma_sample_position), AV_OPT_TYPE_INT,
321 { .i64 = -1 }, -1, 3, FLAGS, "csp" },
322 { "unknown", "Unknown chroma sample position", 0, AV_OPT_TYPE_CONST,
323 { .i64 = AV1_CSP_UNKNOWN }, .flags = FLAGS, .unit = "csp" },
324 { "vertical", "Left chroma sample position", 0, AV_OPT_TYPE_CONST,
325 { .i64 = AV1_CSP_VERTICAL }, .flags = FLAGS, .unit = "csp" },
326 { "colocated", "Top-left chroma sample position", 0, AV_OPT_TYPE_CONST,
327 { .i64 = AV1_CSP_COLOCATED }, .flags = FLAGS, .unit = "csp" },
329 { "tick_rate", "Set display tick rate (num_units_in_display_tick / time_scale)",
330 OFFSET(tick_rate), AV_OPT_TYPE_RATIONAL,
331 { .dbl = 0.0 }, 0, UINT_MAX, FLAGS },
332 { "num_ticks_per_picture", "Set display ticks per picture for CFR streams",
333 OFFSET(num_ticks_per_picture), AV_OPT_TYPE_INT,
334 { .i64 = -1 }, -1, INT_MAX, FLAGS },
336 { "delete_padding", "Delete all Padding OBUs",
337 OFFSET(delete_padding), AV_OPT_TYPE_BOOL,
338 { .i64 = 0 }, 0, 1, FLAGS},
343 static const AVClass av1_metadata_class = {
344 .class_name = "av1_metadata_bsf",
345 .item_name = av_default_item_name,
346 .option = av1_metadata_options,
347 .version = LIBAVUTIL_VERSION_INT,
350 static const enum AVCodecID av1_metadata_codec_ids[] = {
351 AV_CODEC_ID_AV1, AV_CODEC_ID_NONE,
354 const AVBitStreamFilter ff_av1_metadata_bsf = {
355 .name = "av1_metadata",
356 .priv_data_size = sizeof(AV1MetadataContext),
357 .priv_class = &av1_metadata_class,
358 .init = &av1_metadata_init,
359 .close = &av1_metadata_close,
360 .filter = &av1_metadata_filter,
361 .codec_ids = av1_metadata_codec_ids,