From patchwork Fri Jan 1 21:35:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 24751 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 7269644B4DF for ; Fri, 1 Jan 2021 23:44:10 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 5475968AA35; Fri, 1 Jan 2021 23:44:10 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 4210B68A9D9 for ; Fri, 1 Jan 2021 23:44:03 +0200 (EET) Received: by mail-wm1-f50.google.com with SMTP id e25so10026302wme.0 for ; Fri, 01 Jan 2021 13:44:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=85Lfrna4j+4RFIRAVil03RcyjgPHcBgqvw1cpk6QIjM=; b=FHHbzzhj7m6lDzQPiZQknaHHh8JMqZRqV1qvpgqex2yubBid6riq5Fys8z9tTDmfB+ /Ms3tV7uNBjIzzVgVQt2KQqhsPQ3Z+au6bn+em2uPUHGSmR0foYC4S4eMNRVe5W1qJ2i ltQbVO4cnozAXAp1H+A7Y63vvm/aqKSoTa000piJ2cWluBztqZUTitpD1afQbSpWDh90 pNlI7Rw3oZNzK3xMuozhS6GP3GHjwztYowxyboAihAUm6L5pxyetvorW87Cj7pQ1rtNj 1U8N4udOTceJnhnBE3og3J+ZSBXCn0qv8azqZ5CaXkZb4WX/xTVnynpTKuqJOzXsTxLi 6yig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=85Lfrna4j+4RFIRAVil03RcyjgPHcBgqvw1cpk6QIjM=; b=k1eS4bsIua4AxWPLVCiXVyZihkzNHQNs/GsKW1zbKWSv3M3wHElghSJc6f610iWu8Q tM+gwu38JU1cMJPL34cMyNQiVPmyL8M7+MrjhgIOaYE0pKOorkLUverxJH5E4T2bjArg lp12MBQXdutVSf9pgAnykCSajAzKuAxHGIJUgG0I3YtnCepJiD06dvM9q2hdp+L05Gi9 fIH0pK5qbxepMoHBTmkHkJXNBOH7gTVmAqToTh/3/WhwmZPWtx4tHQB1JG6a2FMkHC2a IgWK90G/yR+a9gLJFv84m9h3sIS5NP3PS/wcpM9Zb8noIWlPCRWvZUB3XQwmslA30yPx 2NFA== X-Gm-Message-State: AOAM533f5427Q/jIQwgcAb0Z8KrfIUjWSaWsdweLKZaeJgIzVlam1ql4 E9ah5NgOIijQ3kmniqTPrv6iKzrMniBwgw== X-Google-Smtp-Source: ABdhPJx5/5I7brfi8wdDwd4Z8djdtXKFh+QQcpmy49vu5v0BmazZwxrSBNC3UZo3l58LUlA3+o0z9Q== X-Received: by 2002:a1c:2605:: with SMTP id m5mr17351612wmm.111.1609537078007; Fri, 01 Jan 2021 13:37:58 -0800 (PST) Received: from localhost.localdomain (cpc91226-cmbg18-2-0-cust7.5-4.cable.virginm.net. [82.0.29.8]) by smtp.gmail.com with ESMTPSA id v20sm18784595wml.34.2021.01.01.13.37.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Jan 2021 13:37:57 -0800 (PST) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Fri, 1 Jan 2021 21:35:18 +0000 Message-Id: <20210101213537.169546-9-sw@jkqxz.net> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210101213537.169546-1-sw@jkqxz.net> References: <20210101213537.169546-1-sw@jkqxz.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 08/27] h264_metadata_bsf: Use common cbs bsf implementation X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" --- libavcodec/h264_metadata_bsf.c | 232 +++++++-------------------------- 1 file changed, 49 insertions(+), 183 deletions(-) diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c index 71b49696cd..f5f439018d 100644 --- a/libavcodec/h264_metadata_bsf.c +++ b/libavcodec/h264_metadata_bsf.c @@ -22,20 +22,13 @@ #include "libavutil/opt.h" #include "bsf.h" -#include "bsf_internal.h" #include "cbs.h" +#include "cbs_bsf.h" #include "cbs_h264.h" #include "h264.h" #include "h264_levels.h" #include "h264_sei.h" -enum { - PASS, - INSERT, - REMOVE, - EXTRACT, -}; - enum { FLIP_HORIZONTAL = 1, FLIP_VERTICAL = 2, @@ -47,11 +40,7 @@ enum { }; typedef struct H264MetadataContext { - const AVClass *class; - - CodedBitstreamContext *input; - CodedBitstreamContext *output; - CodedBitstreamFragment access_unit; + CBSBSFContext common; int done_first_au; @@ -333,49 +322,6 @@ static int h264_metadata_update_sps(AVBSFContext *bsf, return 0; } -static int h264_metadata_update_side_data(AVBSFContext *bsf, AVPacket *pkt) -{ - H264MetadataContext *ctx = bsf->priv_data; - CodedBitstreamFragment *au = &ctx->access_unit; - uint8_t *side_data; - int side_data_size; - int err, i; - - side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, - &side_data_size); - if (!side_data_size) - return 0; - - err = ff_cbs_read(ctx->input, au, side_data, side_data_size); - if (err < 0) { - av_log(bsf, AV_LOG_ERROR, "Failed to read extradata from packet side data.\n"); - return err; - } - - for (i = 0; i < au->nb_units; i++) { - if (au->units[i].type == H264_NAL_SPS) { - err = h264_metadata_update_sps(bsf, au->units[i].content); - if (err < 0) - return err; - } - } - - err = ff_cbs_write_fragment_data(ctx->output, au); - if (err < 0) { - av_log(bsf, AV_LOG_ERROR, "Failed to write extradata into packet side data.\n"); - return err; - } - - side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_NEW_EXTRADATA, au->data_size); - if (!side_data) - return AVERROR(ENOMEM); - memcpy(side_data, au->data, au->data_size); - - ff_cbs_fragment_reset(au); - - return 0; -} - static int h264_metadata_handle_display_orientation(AVBSFContext *bsf, AVPacket *pkt, CodedBitstreamFragment *au, @@ -386,7 +332,7 @@ static int h264_metadata_handle_display_orientation(AVBSFContext *bsf, int err; message = NULL; - while (ff_cbs_sei_find_message(ctx->output, au, + while (ff_cbs_sei_find_message(ctx->common.output, au, H264_SEI_TYPE_DISPLAY_ORIENTATION, &message) == 0) { H264RawSEIDisplayOrientation *disp = message->payload; @@ -415,13 +361,13 @@ static int h264_metadata_handle_display_orientation(AVBSFContext *bsf, } } - if (ctx->display_orientation == REMOVE || - ctx->display_orientation == INSERT) { - ff_cbs_sei_delete_message_type(ctx->output, au, + if (ctx->display_orientation == BSF_ELEMENT_REMOVE || + ctx->display_orientation == BSF_ELEMENT_INSERT) { + ff_cbs_sei_delete_message_type(ctx->common.output, au, H264_SEI_TYPE_DISPLAY_ORIENTATION); } - if (ctx->display_orientation == INSERT) { + if (ctx->display_orientation == BSF_ELEMENT_INSERT) { H264RawSEIDisplayOrientation *disp = &ctx->display_orientation_payload; uint8_t *data; @@ -495,7 +441,7 @@ static int h264_metadata_handle_display_orientation(AVBSFContext *bsf, if (write) { disp->display_orientation_repetition_period = 1; - err = ff_cbs_sei_add_message(ctx->output, au, 1, + err = ff_cbs_sei_add_message(ctx->common.output, au, 1, H264_SEI_TYPE_DISPLAY_ORIENTATION, disp, NULL); if (err < 0) { @@ -509,41 +455,21 @@ static int h264_metadata_handle_display_orientation(AVBSFContext *bsf, return 0; } -static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *pkt) +static int h264_metadata_update_fragment(AVBSFContext *bsf, AVPacket *pkt, + CodedBitstreamFragment *au) { H264MetadataContext *ctx = bsf->priv_data; - CodedBitstreamFragment *au = &ctx->access_unit; int err, i, has_sps, seek_point; - err = ff_bsf_get_packet_ref(bsf, pkt); - if (err < 0) - return err; - - err = h264_metadata_update_side_data(bsf, pkt); - if (err < 0) - goto fail; - - err = ff_cbs_read_packet(ctx->input, au, pkt); - if (err < 0) { - av_log(bsf, AV_LOG_ERROR, "Failed to read packet.\n"); - goto fail; - } - - if (au->nb_units == 0) { - av_log(bsf, AV_LOG_ERROR, "No NAL units in packet.\n"); - err = AVERROR_INVALIDDATA; - goto fail; - } - // If an AUD is present, it must be the first NAL unit. if (au->units[0].type == H264_NAL_AUD) { - if (ctx->aud == REMOVE) + if (ctx->aud == BSF_ELEMENT_REMOVE) ff_cbs_delete_unit(au, 0); } else { - if (ctx->aud == INSERT) { + if (ctx->aud == BSF_ELEMENT_INSERT) { err = h264_metadata_insert_aud(bsf, au); if (err < 0) - goto fail; + return err; } } @@ -552,27 +478,31 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *pkt) if (au->units[i].type == H264_NAL_SPS) { err = h264_metadata_update_sps(bsf, au->units[i].content); if (err < 0) - goto fail; + return err; has_sps = 1; } } - // The current packet should be treated as a seek point for metadata - // insertion if any of: - // - It is the first packet in the stream. - // - It contains an SPS, indicating that a sequence might start here. - // - It is marked as containing a key frame. - seek_point = !ctx->done_first_au || has_sps || - (pkt->flags & AV_PKT_FLAG_KEY); + if (pkt) { + // The current packet should be treated as a seek point for metadata + // insertion if any of: + // - It is the first packet in the stream. + // - It contains an SPS, indicating that a sequence might start here. + // - It is marked as containing a key frame. + seek_point = !ctx->done_first_au || has_sps || + (pkt->flags & AV_PKT_FLAG_KEY); + } else { + seek_point = 0; + } if (ctx->sei_user_data && seek_point) { - err = ff_cbs_sei_add_message(ctx->output, au, 1, + err = ff_cbs_sei_add_message(ctx->common.output, au, 1, H264_SEI_TYPE_USER_DATA_UNREGISTERED, &ctx->sei_user_data_payload, NULL); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to add user data SEI " "message to access unit.\n"); - goto fail; + return err; } } @@ -584,44 +514,37 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *pkt) } } - ff_cbs_sei_delete_message_type(ctx->output, au, + ff_cbs_sei_delete_message_type(ctx->common.output, au, H264_SEI_TYPE_FILLER_PAYLOAD); } - if (ctx->display_orientation != PASS) { + if (pkt && ctx->display_orientation != BSF_ELEMENT_PASS) { err = h264_metadata_handle_display_orientation(bsf, pkt, au, seek_point); if (err < 0) - goto fail; + return err; } - err = ff_cbs_write_packet(ctx->output, pkt, au); - if (err < 0) { - av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n"); - goto fail; - } - - ctx->done_first_au = 1; - - err = 0; -fail: - ff_cbs_fragment_reset(au); - - if (err < 0) - av_packet_unref(pkt); + if (pkt) + ctx->done_first_au = 1; return err; } +static const CBSBSFType h264_metadata_type = { + .codec_id = AV_CODEC_ID_H264, + .fragment_name = "access unit", + .unit_name = "NAL unit", + .update_fragment = &h264_metadata_update_fragment, +}; + static int h264_metadata_init(AVBSFContext *bsf) { H264MetadataContext *ctx = bsf->priv_data; - CodedBitstreamFragment *au = &ctx->access_unit; - int err, i; if (ctx->sei_user_data) { SEIRawUserDataUnregistered *udu = &ctx->sei_user_data_payload; - int j; + int i, j; // Parse UUID. It must be a hex string of length 32, possibly // containing '-'s between hex digits (which we ignore). @@ -648,67 +571,18 @@ static int h264_metadata_init(AVBSFContext *bsf) } else { av_log(bsf, AV_LOG_ERROR, "Invalid user data: " "must be \"UUID+string\".\n"); - err = AVERROR(EINVAL); - goto fail; + return AVERROR(EINVAL); } } - err = ff_cbs_init(&ctx->input, AV_CODEC_ID_H264, bsf); - if (err < 0) - return err; - err = ff_cbs_init(&ctx->output, AV_CODEC_ID_H264, bsf); - if (err < 0) - return err; - - if (bsf->par_in->extradata) { - err = ff_cbs_read_extradata(ctx->input, au, bsf->par_in); - if (err < 0) { - av_log(bsf, AV_LOG_ERROR, "Failed to read extradata.\n"); - goto fail; - } - - for (i = 0; i < au->nb_units; i++) { - if (au->units[i].type == H264_NAL_SPS) { - err = h264_metadata_update_sps(bsf, au->units[i].content); - if (err < 0) - goto fail; - } - } - - err = ff_cbs_write_extradata(ctx->output, bsf->par_out, au); - if (err < 0) { - av_log(bsf, AV_LOG_ERROR, "Failed to write extradata.\n"); - goto fail; - } - } - - err = 0; -fail: - ff_cbs_fragment_reset(au); - return err; -} - -static void h264_metadata_close(AVBSFContext *bsf) -{ - H264MetadataContext *ctx = bsf->priv_data; - - ff_cbs_fragment_free(&ctx->access_unit); - ff_cbs_close(&ctx->input); - ff_cbs_close(&ctx->output); + return ff_cbs_bsf_init(bsf, &h264_metadata_type); } #define OFFSET(x) offsetof(H264MetadataContext, x) #define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_BSF_PARAM) static const AVOption h264_metadata_options[] = { - { "aud", "Access Unit Delimiter NAL units", - OFFSET(aud), AV_OPT_TYPE_INT, - { .i64 = PASS }, PASS, REMOVE, FLAGS, "aud" }, - { "pass", NULL, 0, AV_OPT_TYPE_CONST, - { .i64 = PASS }, .flags = FLAGS, .unit = "aud" }, - { "insert", NULL, 0, AV_OPT_TYPE_CONST, - { .i64 = INSERT }, .flags = FLAGS, .unit = "aud" }, - { "remove", NULL, 0, AV_OPT_TYPE_CONST, - { .i64 = REMOVE }, .flags = FLAGS, .unit = "aud" }, + BSF_ELEMENT_OPTIONS_PIR("aud", "Access Unit Delimiter NAL units", + aud, "aud"), { "sample_aspect_ratio", "Set sample aspect ratio (table E-1)", OFFSET(sample_aspect_ratio), AV_OPT_TYPE_RATIONAL, @@ -764,17 +638,9 @@ static const AVOption h264_metadata_options[] = { { "delete_filler", "Delete all filler (both NAL and SEI)", OFFSET(delete_filler), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS}, - { "display_orientation", "Display orientation SEI", - OFFSET(display_orientation), AV_OPT_TYPE_INT, - { .i64 = PASS }, PASS, EXTRACT, FLAGS, "disp_or" }, - { "pass", NULL, 0, AV_OPT_TYPE_CONST, - { .i64 = PASS }, .flags = FLAGS, .unit = "disp_or" }, - { "insert", NULL, 0, AV_OPT_TYPE_CONST, - { .i64 = INSERT }, .flags = FLAGS, .unit = "disp_or" }, - { "remove", NULL, 0, AV_OPT_TYPE_CONST, - { .i64 = REMOVE }, .flags = FLAGS, .unit = "disp_or" }, - { "extract", NULL, 0, AV_OPT_TYPE_CONST, - { .i64 = EXTRACT }, .flags = FLAGS, .unit = "disp_or" }, + BSF_ELEMENT_OPTIONS_PIRE("display_orientation", + "Display orientation SEI", + display_orientation, "disp_or"), { "rotate", "Set rotation in display orientation SEI (anticlockwise angle in degrees)", OFFSET(rotate), AV_OPT_TYPE_DOUBLE, @@ -838,7 +704,7 @@ const AVBitStreamFilter ff_h264_metadata_bsf = { .priv_data_size = sizeof(H264MetadataContext), .priv_class = &h264_metadata_class, .init = &h264_metadata_init, - .close = &h264_metadata_close, - .filter = &h264_metadata_filter, + .close = &ff_cbs_bsf_close, + .filter = &ff_cbs_bsf_filter, .codec_ids = h264_metadata_codec_ids, };