From patchwork Tue Sep 19 19:57:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andreas Rheinhardt X-Patchwork-Id: 43822 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:a886:b0:149:dfde:5c0a with SMTP id ca6csp185920pzb; Tue, 19 Sep 2023 12:58:23 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEIM0cYG2uLnn3dfEjdGv9dQZb1w3ZL1JdiCVBNKQPkrfsBHnnIuwmORJyOVarFmLs7B1db X-Received: by 2002:a17:906:2099:b0:9a1:c812:cbb9 with SMTP id 25-20020a170906209900b009a1c812cbb9mr371903ejq.31.1695153502738; Tue, 19 Sep 2023 12:58:22 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id lr6-20020a170906fb8600b0099d74066201si11262209ejb.415.2023.09.19.12.58.22; Tue, 19 Sep 2023 12:58:22 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@outlook.com header.s=selector1 header.b=PvPJmdw1; arc=fail (body hash mismatch); spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=outlook.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A9B7868C966; Tue, 19 Sep 2023 22:57:18 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-vi1eur04olkn2070.outbound.protection.outlook.com [40.92.75.70]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E30BC68C960 for ; Tue, 19 Sep 2023 22:57:16 +0300 (EEST) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XYLmZLheb3BjHe8EbMCeSBb0SeqWwF2rQgtgc2XFCICvXc3W2tlOMl3hTbghg6RQ1avzQUTMYPI/8Tl/deHxTNt3f3GmZcJhnfsCXj/JlUVnhK+6ncLCN7ron+snGWjJlykPb/RfrWlg6TQC0RdCF7DWG2bgLNWc/LvplVKB7EU1BKLRSO0N7yIwrJrOOfZEW7Bw2mCmn1g1oBL48YdUbm74jfzGURUJ+3kc4Y2868lICA+DL7BYiI0pC7diIGnFQbuCNfucjfGzc6mgh6CydLQOYENvbojnSQOV9tPjvdkWd/P+fWhfWrCkjgPwY1U5ILUYD04NJ9C+epB0ob3P8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=PTDcofH44rhpf7wNKcBVTDqan0UmN5zTRFmJzR4Ba4k=; b=mQr8fH3nk3SoL9EOmEDys5zkvCau88aaovjPmPksXOApqynN51HVW6strJCejnbfCJ5uvVG3FDUkHF5w4P8HkzuNM8S7vY3ibM7+Ve5M02YhCy3UQ71n7Jt5g9sSZT0PtQgsCZ7aUvOGMT9lWgmrWPfhL1WYlpUf7eVRStaHf8qrcgQsKfJ74VXgyM3rI+LMvOhOBnOyMHKxSbafPAGkZqrycF3KOipm4S1bC1K4Fh2vnK3wDHUMIDVJO3eaC5+J8U9wT1fwQx+W52Wv0vIUa6G9my/264HotsT1ZsmOR3UNmaSuxa5PuW8UKxm2046FwnccK9IVMj02e/hbIdDOfg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=PTDcofH44rhpf7wNKcBVTDqan0UmN5zTRFmJzR4Ba4k=; b=PvPJmdw1dHUVFIfjAjamUVi8+wgLz7aUu3iJoxJloC01xD2rRgAHrG++y8delxyt++7+9PoqVPmNbI4dl1oKcF3EcfLoxBSZpP9d0tOnfVIM3DnfybxUnFtg81OiW5LAGquIc0zJIxkLS5469NE8StRA94fuPPRpUQJfJP39+LofkBhPKjAmJldQu3RLQdSpytBI/ImbkE8tWOMWOCCuJSE49A54fPNKwwxyYnK/NU05wmwGI/YGD8iaSfoff9MCIv+iKiVbciN2Rl9Yz8nc4nwk8frbJR4DbzwhP/bT48/pCr4dzkruoHpRrR4Rz9uWHbAsQiimBvef+vOwKDyL5g== Received: from AS8P250MB0744.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:541::14) by AM8P250MB0215.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:326::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.26; Tue, 19 Sep 2023 19:57:15 +0000 Received: from AS8P250MB0744.EURP250.PROD.OUTLOOK.COM ([fe80::5e01:aea5:d3a8:cafa]) by AS8P250MB0744.EURP250.PROD.OUTLOOK.COM ([fe80::5e01:aea5:d3a8:cafa%3]) with mapi id 15.20.6792.026; Tue, 19 Sep 2023 19:57:15 +0000 From: Andreas Rheinhardt To: ffmpeg-devel@ffmpeg.org Date: Tue, 19 Sep 2023 21:57:03 +0200 Message-ID: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-TMN: [hpnGYDiy6P+8drpzW+JiX8Wl+LOjw4Um] X-ClientProxiedBy: ZR0P278CA0086.CHEP278.PROD.OUTLOOK.COM (2603:10a6:910:22::19) To AS8P250MB0744.EURP250.PROD.OUTLOOK.COM (2603:10a6:20b:541::14) X-Microsoft-Original-Message-ID: <20230919195734.1005750-11-andreas.rheinhardt@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: AS8P250MB0744:EE_|AM8P250MB0215:EE_ X-MS-Office365-Filtering-Correlation-Id: f5deb960-2e0d-4e0b-99a9-08dbb94a9f3c X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 8DNfu/WklQCuqZpcZUic4GVLRymzQwuct50ydky3WU+uvqxUW20RfOrGMD1VP7/WteUlgpZ+lk5RmCRGfwSe2m50IQKeYWhEkefiSowMyrSGJZdWruwkrAsmKYhVbtDetE/KbQdwEMrfJECYNMsMq6w+6RHlrZDjDIWCT63TPe1lmf/m5z9ZSmlJzARcLUupN/vBL58urq4crMkCunXAs2XqNpNTZUVsiiGeQvyTiV7vrKE+ilhqcunCxmULT/RXZbkCNWoY7Z+JS6YWZef6fn3E9hCrA+4zO18aB5pgiYD4QNIWXuOTfvqB2r4gEtPVAvBf2vHFLMpo7PVo74kaXSaY9iuagqI6RO/V9ydejb+ofx56iksbBuTBzL+IL7nr2daNBjmdOb8HaXJaEiK++OmuoGDGLH2eX/mDWGWhbJDJBR+gZa0fF/SHJy/Cra4iOmDQ6jAVCP6J0KLjIMsZnhaeQrD3n6hfigLJ0h2TsJ66pe/Usutw6IF9Sn8sPLFV77pH2TlAVGzsgZ5cQfZHpEMd3JNvJJBpobeAPBVh6ER74xpplCOUysbWKN2AVJ3PjFlDzylDE6HM2DZ145r9Y+WpuvSEWIJncVLp9jRFi2jtDSkTvwU1jBrrXatvrpAn X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: wnPVzeOKQ/adjkG9L11qsbQ8SVkWMnHmw7sBp8uAXFEVEqt1RnLCBGiipfCixnxS6k7VMTg/AP3t5YYB2Qm2hA16RAmp7OsTE+N8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HRPtzWXVvPnCPobHC0G4OOu15232NWl9A27vinRGw3MlPyTw3BGhu2cUBqIbL3YnmFqoMHt/H0mDyTJ0d7Zoe3z6YkH+Q90Bk7OvtG8v2 X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: f5deb960-2e0d-4e0b-99a9-08dbb94a9f3c X-MS-Exchange-CrossTenant-AuthSource: AS8P250MB0744.EURP250.PROD.OUTLOOK.COM X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Sep 2023 19:57:15.5456 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM8P250MB0215 Subject: [FFmpeg-devel] [PATCH 11/42] avcodec/cbs_sei: Use RefStruct API for SEI messages X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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 Cc: Andreas Rheinhardt Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: TUWFV1TzWWKf The SEI message code uses the AVBuffer API for its SEI messages and contained buffers (like the extension buffer for HEVC or the user data (un)registered payload buffers). Contrary to the ordinary CBS code (where some of these contained buffer references are actually references to the provided AVPacket's data so that one can't replace them with the RefStruct API), the CBS SEI code never uses outside buffers at all and can therefore be switched entirely to the RefStruct API. This avoids the overhead inherent in the AVBuffer API (namely the separate allocations etc.). Notice that the refcounting here is actually currently unused; the refcounts are always one (or zero in case of no refcounting); its only advantage is the flexibility provided by custom free functions. Signed-off-by: Andreas Rheinhardt --- libavcodec/cbs_h2645.c | 19 +++++++-- libavcodec/cbs_sei.c | 61 ++++++++++++---------------- libavcodec/cbs_sei.h | 20 ++++----- libavcodec/cbs_sei_syntax_template.c | 5 +++ 4 files changed, 54 insertions(+), 51 deletions(-) diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c index e071442c31..c1f67fefb9 100644 --- a/libavcodec/cbs_h2645.c +++ b/libavcodec/cbs_h2645.c @@ -357,18 +357,31 @@ static int cbs_h2645_read_more_rbsp_data(GetBitContext *gbc) #define bit_position(rw) (get_bits_count(rw)) #define byte_alignment(rw) (get_bits_count(rw) % 8) +/* The CBS SEI code uses the refstruct API for the allocation + * of its child buffers. */ #define allocate(name, size) do { \ - name ## _ref = av_buffer_allocz(size + \ + name = ff_refstruct_allocz(size + \ AV_INPUT_BUFFER_PADDING_SIZE); \ - if (!name ## _ref) \ + if (!name) \ return AVERROR(ENOMEM); \ - name = name ## _ref->data; \ } while (0) #define FUNC(name) FUNC_SEI(name) #include "cbs_sei_syntax_template.c" #undef FUNC +#undef allocate + +/* The other code uses the refstruct API for the allocation + * of its child buffers. */ +#define allocate(name, size) do { \ + name ## _ref = av_buffer_allocz(size + \ + AV_INPUT_BUFFER_PADDING_SIZE); \ + if (!name ## _ref) \ + return AVERROR(ENOMEM); \ + name = name ## _ref->data; \ + } while (0) + #define FUNC(name) FUNC_H264(name) #include "cbs_h264_syntax_template.c" #undef FUNC diff --git a/libavcodec/cbs_sei.c b/libavcodec/cbs_sei.c index bd7f6f4938..e28c2f9093 100644 --- a/libavcodec/cbs_sei.c +++ b/libavcodec/cbs_sei.c @@ -22,25 +22,25 @@ #include "cbs_h265.h" #include "cbs_h266.h" #include "cbs_sei.h" +#include "refstruct.h" -static void cbs_free_user_data_registered(void *opaque, uint8_t *data) +static void cbs_free_user_data_registered(FFRefStructOpaque unused, void *obj) { - SEIRawUserDataRegistered *udr = (SEIRawUserDataRegistered*)data; - av_buffer_unref(&udr->data_ref); - av_free(udr); + SEIRawUserDataRegistered *udr = obj; + ff_refstruct_unref(&udr->data); } -static void cbs_free_user_data_unregistered(void *opaque, uint8_t *data) +static void cbs_free_user_data_unregistered(FFRefStructOpaque unused, void *obj) { - SEIRawUserDataUnregistered *udu = (SEIRawUserDataUnregistered*)data; - av_buffer_unref(&udu->data_ref); - av_free(udu); + SEIRawUserDataUnregistered *udu = obj; + ff_refstruct_unref(&udu->data); } int ff_cbs_sei_alloc_message_payload(SEIRawMessage *message, const SEIMessageTypeDescriptor *desc) { - void (*free_func)(void*, uint8_t*); + void (*free_func)(FFRefStructOpaque, void*); + unsigned flags = 0; av_assert0(message->payload == NULL && message->payload_ref == NULL); @@ -50,24 +50,16 @@ int ff_cbs_sei_alloc_message_payload(SEIRawMessage *message, free_func = &cbs_free_user_data_registered; else if (desc->type == SEI_TYPE_USER_DATA_UNREGISTERED) free_func = &cbs_free_user_data_unregistered; - else + else { free_func = NULL; - - if (free_func) { - message->payload = av_mallocz(desc->size); - if (!message->payload) - return AVERROR(ENOMEM); - message->payload_ref = - av_buffer_create(message->payload, desc->size, - free_func, NULL, 0); - } else { - message->payload_ref = av_buffer_alloc(desc->size); + flags = FF_REFSTRUCT_FLAG_NO_ZEROING; } - if (!message->payload_ref) { - av_freep(&message->payload); + + message->payload_ref = ff_refstruct_alloc_ext(desc->size, flags, + NULL, free_func); + if (!message->payload_ref) return AVERROR(ENOMEM); - } - message->payload = message->payload_ref->data; + message->payload = message->payload_ref; return 0; } @@ -101,8 +93,8 @@ void ff_cbs_sei_free_message_list(SEIRawMessageList *list) { for (int i = 0; i < list->nb_messages; i++) { SEIRawMessage *message = &list->messages[i]; - av_buffer_unref(&message->payload_ref); - av_buffer_unref(&message->extension_data_ref); + ff_refstruct_unref(&message->payload_ref); + ff_refstruct_unref(&message->extension_data); } av_free(list->messages); } @@ -278,13 +270,12 @@ int ff_cbs_sei_add_message(CodedBitstreamContext *ctx, int prefix, uint32_t payload_type, void *payload_data, - AVBufferRef *payload_buf) + void *payload_ref) { const SEIMessageTypeDescriptor *desc; CodedBitstreamUnit *unit; SEIRawMessageList *list; SEIRawMessage *message; - AVBufferRef *payload_ref; int err; desc = ff_cbs_sei_find_type(ctx, payload_type); @@ -306,12 +297,10 @@ int ff_cbs_sei_add_message(CodedBitstreamContext *ctx, if (err < 0) return err; - if (payload_buf) { - payload_ref = av_buffer_ref(payload_buf); - if (!payload_ref) - return AVERROR(ENOMEM); - } else { - payload_ref = NULL; + if (payload_ref) { + /* The following just increments payload_ref's refcount, + * so that payload_ref is now owned by us. */ + payload_ref = ff_refstruct_ref(payload_ref); } message = &list->messages[list->nb_messages - 1]; @@ -364,8 +353,8 @@ static void cbs_sei_delete_message(SEIRawMessageList *list, av_assert0(0 <= position && position < list->nb_messages); message = &list->messages[position]; - av_buffer_unref(&message->payload_ref); - av_buffer_unref(&message->extension_data_ref); + ff_refstruct_unref(&message->payload_ref); + ff_refstruct_unref(&message->extension_data); --list->nb_messages; diff --git a/libavcodec/cbs_sei.h b/libavcodec/cbs_sei.h index 1c327a4689..4511c506cc 100644 --- a/libavcodec/cbs_sei.h +++ b/libavcodec/cbs_sei.h @@ -22,8 +22,6 @@ #include #include -#include "libavutil/buffer.h" - #include "cbs.h" #include "sei.h" @@ -35,15 +33,13 @@ typedef struct SEIRawFillerPayload { typedef struct SEIRawUserDataRegistered { uint8_t itu_t_t35_country_code; uint8_t itu_t_t35_country_code_extension_byte; - uint8_t *data; - AVBufferRef *data_ref; + uint8_t *data; ///< RefStruct reference size_t data_length; } SEIRawUserDataRegistered; typedef struct SEIRawUserDataUnregistered { uint8_t uuid_iso_iec_11578[16]; - uint8_t *data; - AVBufferRef *data_ref; + uint8_t *data; ///< RefStruct reference size_t data_length; } SEIRawUserDataUnregistered; @@ -75,9 +71,8 @@ typedef struct SEIRawMessage { uint32_t payload_type; uint32_t payload_size; void *payload; - AVBufferRef *payload_ref; - uint8_t *extension_data; - AVBufferRef *extension_data_ref; + void *payload_ref; ///< RefStruct reference + uint8_t *extension_data; ///< RefStruct reference size_t extension_bit_length; } SEIRawMessage; @@ -174,15 +169,16 @@ void ff_cbs_sei_free_message_list(SEIRawMessageList *list); * Will add to an existing SEI NAL unit, or create a new one for the * message if there is no suitable existing one. * - * Takes a new reference to payload_buf, if set. If payload_buf is - * NULL then the new message will not be reference counted. + * If set, payload_ref must be a RefStruct reference backing payload_data. + * This function creates a new reference to payload_ref in this case. + * If payload_ref is NULL, the new message will not be reference counted. */ int ff_cbs_sei_add_message(CodedBitstreamContext *ctx, CodedBitstreamFragment *au, int prefix, uint32_t payload_type, void *payload_data, - AVBufferRef *payload_buf); + void *payload_ref); /** * Iterate over messages with the given payload type in an access unit. diff --git a/libavcodec/cbs_sei_syntax_template.c b/libavcodec/cbs_sei_syntax_template.c index 6a7cc36dda..62dd1dabaa 100644 --- a/libavcodec/cbs_sei_syntax_template.c +++ b/libavcodec/cbs_sei_syntax_template.c @@ -234,7 +234,12 @@ static int FUNC(message)(CodedBitstreamContext *ctx, RWContext *rw, } else { uint8_t *data; +#ifdef READ + allocate(current->payload_ref, current->payload_size); + current->payload = current->payload_ref; +#else allocate(current->payload, current->payload_size); +#endif data = current->payload; for (i = 0; i < current->payload_size; i++)