X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fav1_metadata_bsf.c;h=1fb85d88b7e881b6e27ce987af04300fef3ea28d;hb=dc8e15ef15d060a9ca118905c770710ce43a0723;hp=2b74b697e41ad3f7f9496d7b95e8e2d2b0616fa2;hpb=79025da3f2e7ab047c8f3c0c817952a98480b26b;p=ffmpeg diff --git a/libavcodec/av1_metadata_bsf.c b/libavcodec/av1_metadata_bsf.c index 2b74b697e41..1fb85d88b7e 100644 --- a/libavcodec/av1_metadata_bsf.c +++ b/libavcodec/av1_metadata_bsf.c @@ -21,21 +21,14 @@ #include "bsf.h" #include "cbs.h" +#include "cbs_bsf.h" #include "cbs_av1.h" -enum { - PASS, - INSERT, - REMOVE, -}; - typedef struct AV1MetadataContext { - const AVClass *class; - - CodedBitstreamContext *cbc; - CodedBitstreamFragment access_unit; + CBSBSFContext common; int td; + AV1RawOBU td_obu; int color_primaries; int transfer_characteristics; @@ -46,6 +39,8 @@ typedef struct AV1MetadataContext { AVRational tick_rate; int num_ticks_per_picture; + + int delete_padding; } AV1MetadataContext; @@ -59,12 +54,7 @@ static int av1_metadata_update_sequence_header(AVBSFContext *bsf, if (ctx->color_primaries >= 0 || ctx->transfer_characteristics >= 0 || ctx->matrix_coefficients >= 0) { - if (!clc->color_description_present_flag) { - clc->color_description_present_flag = 1; - clc->color_primaries = AVCOL_PRI_UNSPECIFIED; - clc->transfer_characteristics = AVCOL_TRC_UNSPECIFIED; - clc->matrix_coefficients = AVCOL_SPC_UNSPECIFIED; - } + clc->color_description_present_flag = 1; if (ctx->color_primaries >= 0) clc->color_primaries = ctx->color_primaries; @@ -114,131 +104,67 @@ static int av1_metadata_update_sequence_header(AVBSFContext *bsf, return 0; } -static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *out) +static int av1_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt, + CodedBitstreamFragment *frag) { AV1MetadataContext *ctx = bsf->priv_data; - AVPacket *in = NULL; - CodedBitstreamFragment *frag = &ctx->access_unit; - AV1RawOBU td, *obu; int err, i; - err = ff_bsf_get_packet(bsf, &in); - if (err < 0) - return err; - - err = ff_cbs_read_packet(ctx->cbc, frag, in); - if (err < 0) { - av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n"); - goto fail; - } - for (i = 0; i < frag->nb_units; i++) { if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) { - obu = frag->units[i].content; + AV1RawOBU *obu = frag->units[i].content; err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header); if (err < 0) - goto fail; + return err; } } // If a Temporal Delimiter is present, it must be the first OBU. - if (frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) { - if (ctx->td == REMOVE) - ff_cbs_delete_unit(ctx->cbc, frag, 0); - } else if (ctx->td == INSERT) { - td = (AV1RawOBU) { - .header.obu_type = AV1_OBU_TEMPORAL_DELIMITER, - }; - - err = ff_cbs_insert_unit_content(ctx->cbc, frag, 0, AV1_OBU_TEMPORAL_DELIMITER, - &td, NULL); + if (frag->nb_units && frag->units[0].type == AV1_OBU_TEMPORAL_DELIMITER) { + if (ctx->td == BSF_ELEMENT_REMOVE) + ff_cbs_delete_unit(frag, 0); + } else if (pkt && ctx->td == BSF_ELEMENT_INSERT) { + err = ff_cbs_insert_unit_content(frag, 0, AV1_OBU_TEMPORAL_DELIMITER, + &ctx->td_obu, NULL); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to insert Temporal Delimiter.\n"); - goto fail; + return err; } } - err = ff_cbs_write_packet(ctx->cbc, out, frag); - if (err < 0) { - av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n"); - goto fail; + if (ctx->delete_padding) { + for (i = frag->nb_units - 1; i >= 0; i--) { + if (frag->units[i].type == AV1_OBU_PADDING) + ff_cbs_delete_unit(frag, i); + } } - err = av_packet_copy_props(out, in); - if (err < 0) - goto fail; - - err = 0; -fail: - ff_cbs_fragment_reset(ctx->cbc, frag); - - if (err < 0) - av_packet_unref(out); - av_packet_free(&in); - - return err; + return 0; } +static const CBSBSFType av1_metadata_type = { + .codec_id = AV_CODEC_ID_AV1, + .fragment_name = "temporal unit", + .unit_name = "OBU", + .update_fragment = &av1_metadata_update_fragment, +}; + static int av1_metadata_init(AVBSFContext *bsf) { AV1MetadataContext *ctx = bsf->priv_data; - CodedBitstreamFragment *frag = &ctx->access_unit; - AV1RawOBU *obu; - int err, i; - err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_AV1, bsf); - if (err < 0) - return err; + ctx->td_obu = (AV1RawOBU) { + .header.obu_type = AV1_OBU_TEMPORAL_DELIMITER, + }; - if (bsf->par_in->extradata) { - err = ff_cbs_read_extradata(ctx->cbc, frag, bsf->par_in); - if (err < 0) { - av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n"); - goto fail; - } - - for (i = 0; i < frag->nb_units; i++) { - if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) { - obu = frag->units[i].content; - err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header); - if (err < 0) - goto fail; - } - } - - err = ff_cbs_write_extradata(ctx->cbc, bsf->par_out, frag); - if (err < 0) { - av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n"); - goto fail; - } - } - - err = 0; -fail: - ff_cbs_fragment_reset(ctx->cbc, frag); - return err; -} - -static void av1_metadata_close(AVBSFContext *bsf) -{ - AV1MetadataContext *ctx = bsf->priv_data; - - ff_cbs_fragment_free(ctx->cbc, &ctx->access_unit); - ff_cbs_close(&ctx->cbc); + return ff_cbs_bsf_generic_init(bsf, &av1_metadata_type); } #define OFFSET(x) offsetof(AV1MetadataContext, x) #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM) static const AVOption av1_metadata_options[] = { - { "td", "Temporal Delimiter OBU", - OFFSET(td), AV_OPT_TYPE_INT, - { .i64 = PASS }, PASS, REMOVE, FLAGS, "td" }, - { "pass", NULL, 0, AV_OPT_TYPE_CONST, - { .i64 = PASS }, .flags = FLAGS, .unit = "td" }, - { "insert", NULL, 0, AV_OPT_TYPE_CONST, - { .i64 = INSERT }, .flags = FLAGS, .unit = "td" }, - { "remove", NULL, 0, AV_OPT_TYPE_CONST, - { .i64 = REMOVE }, .flags = FLAGS, .unit = "td" }, + BSF_ELEMENT_OPTIONS_PIR("td", "Temporal Delimiter OBU", + td, FLAGS), { "color_primaries", "Set color primaries (section 6.4.2)", OFFSET(color_primaries), AV_OPT_TYPE_INT, @@ -275,6 +201,10 @@ static const AVOption av1_metadata_options[] = { OFFSET(num_ticks_per_picture), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, FLAGS }, + { "delete_padding", "Delete all Padding OBUs", + OFFSET(delete_padding), AV_OPT_TYPE_BOOL, + { .i64 = 0 }, 0, 1, FLAGS}, + { NULL } }; @@ -294,7 +224,7 @@ const AVBitStreamFilter ff_av1_metadata_bsf = { .priv_data_size = sizeof(AV1MetadataContext), .priv_class = &av1_metadata_class, .init = &av1_metadata_init, - .close = &av1_metadata_close, - .filter = &av1_metadata_filter, + .close = &ff_cbs_bsf_generic_close, + .filter = &ff_cbs_bsf_generic_filter, .codec_ids = av1_metadata_codec_ids, };