From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 8D365454C5 for ; Sat, 1 Apr 2023 15:22:29 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9AA3868BFC4; Sat, 1 Apr 2023 18:22:28 +0300 (EEST) Received: from mail-oa1-f42.google.com (mail-oa1-f42.google.com [209.85.160.42]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id A9A2468BF30 for ; Sat, 1 Apr 2023 18:22:22 +0300 (EEST) Received: by mail-oa1-f42.google.com with SMTP id 586e51a60fabf-17aaa51a911so26271466fac.5 for ; Sat, 01 Apr 2023 08:22:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1680362541; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=LfcwzP4UkVHPEQH4EDcVnWjAf9h17+Dp5dS8+SVRGjY=; b=XCPUbTqmU9C2+Y0WEzTCdEAZp2hxf5QtAtvAKW3vFI3jDkbtVU9Uv8hLuTLB3c/v2w 6CQg7yV4LMePhrSZtHtsZ65+4SCm4QWBYsjXgSBou6KzARZ0DodCFvsOFAvC4+GgpDcd QSrL/m1rXFSOPuIMv/qQroea3cLPPt5o5o/WHFUOdXOr0KGH7xljc5Da81zm7rARtgkM DmyOSO6N1U+p3E9S8wEZmJzXDj95ym+OAimZpZd6ngnv8O64OBA6r6W2jeSkMxeldq/3 CkH4ndCR94EKyukrlnBuM8Ff7z5BpyNpbI1yLyPZKaMZX66QFfFW7unZG5oDKGWgWsFB Wpyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680362541; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=LfcwzP4UkVHPEQH4EDcVnWjAf9h17+Dp5dS8+SVRGjY=; b=1nN8mOk5EPYgWpt2N4gmfOsgdxxZ9yjE9Er9SjYVeFSD1UB8xdzUMS/8XwYu5umjW1 6QOKAPZUGDyZb+N0JPbHgiRHheHUz0JV5p8O2jF/IcVi2tyop1xAU6nIxmcx/R3O6Mxw dnAphZ7u4DqihQCnW8H0MxVe5Fr6tVM/v2d/x1vTyQT383FJNUMIUGwF6CmfGzfgAn0w HeN1Z5JxEHjfMnVu9vD6yOPNWLhD6ArkKOq47C4JtdbHDEwCP/cUcUFvAmRLgs1iptnJ bU/zPSKb/Uke9O8vX0a2aTEhtcCDyFBfrx7v6Qspt+lBhZ1jNBDeNnLBaBGHSzWkOmoa KPJg== X-Gm-Message-State: AO0yUKVPlFU3A51Li5z2qhyHnKXzn82ycXb7PPiz4yYSky9+8LMORi0R Dx4WGOov1R3CUaJpeeRENgq/IekcPL8= X-Google-Smtp-Source: AKy350aobliygOY+Z+SmLQu4aCozZm3Czizv4jEDAo4vsH8QUDOai5vQTxch6vyUnip7U6jsY0gjtA== X-Received: by 2002:a05:6870:8321:b0:17b:1b08:d56c with SMTP id p33-20020a056870832100b0017b1b08d56cmr19725630oae.14.1680362540645; Sat, 01 Apr 2023 08:22:20 -0700 (PDT) Received: from localhost.localdomain (host197.190-225-105.telecom.net.ar. [190.225.105.197]) by smtp.gmail.com with ESMTPSA id b22-20020a9d7556000000b006a305c68617sm995033otl.53.2023.04.01.08.22.19 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 01 Apr 2023 08:22:20 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Sat, 1 Apr 2023 12:22:05 -0300 Message-Id: <20230401152205.296-1-jamrial@gmail.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avutil/hdr_dynamic_metadata: allow av_dynamic_hdr_plus_to_t35() to accept an existing buffer 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 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: The function now accepts an existing buffer to avoid unnecessary allocations, as well as only reporting the needed amount of bytes if you pass a NULL pointer as input for data. For this, both parameters become input and output, as well as making data optional, and the max payload size define is made public so library users can just use that f they want to use their own buffers, knowing it will be enough for any input AVDynamicHDRPlus. Signed-off-by: James Almer --- Missing version bump (Or maybe i can just not bump since this function is recent enough?) and APIChanges entry. libavutil/hdr_dynamic_metadata.c | 25 +++++++++++++++++-------- libavutil/hdr_dynamic_metadata.h | 14 +++++++++++--- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/libavutil/hdr_dynamic_metadata.c b/libavutil/hdr_dynamic_metadata.c index d458788c32..5b56448f0d 100644 --- a/libavutil/hdr_dynamic_metadata.c +++ b/libavutil/hdr_dynamic_metadata.c @@ -24,8 +24,6 @@ #include "libavcodec/get_bits.h" #include "libavcodec/put_bits.h" -#define T35_PAYLOAD_MAX_SIZE 907 - static const int64_t luminance_den = 1; static const int32_t peak_luminance_den = 15; static const int64_t rgb_den = 100000; @@ -62,14 +60,14 @@ AVDynamicHDRPlus *av_dynamic_hdr_plus_create_side_data(AVFrame *frame) int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data, size_t size) { - uint8_t padded_buf[T35_PAYLOAD_MAX_SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; + uint8_t padded_buf[AV_HDR_PLUS_MAX_PAYLOAD_SIZE + AV_INPUT_BUFFER_PADDING_SIZE]; GetBitContext gbc, *gb = &gbc; int ret; if (!s) return AVERROR(ENOMEM); - if (size > T35_PAYLOAD_MAX_SIZE) + if (size > AV_HDR_PLUS_MAX_PAYLOAD_SIZE) return AVERROR(EINVAL); memcpy(padded_buf, data, size); @@ -243,8 +241,10 @@ int av_dynamic_hdr_plus_to_t35(const AVDynamicHDRPlus *s, uint8_t **data, size_t size_t size_bits, size_bytes; PutBitContext pbc, *pb = &pbc; - if (!s || !data) + if (!s) return AVERROR(EINVAL); + if ((!data || *data) && !size) + return AVERROR(EINVAL); /** * Buffer size per CTA-861-H p.253-254: @@ -296,9 +296,18 @@ int av_dynamic_hdr_plus_to_t35(const AVDynamicHDRPlus *s, uint8_t **data, size_t size_bytes = (size_bits + 7) / 8; - buf = av_mallocz(size_bytes); - if (!buf) - return AVERROR(ENOMEM); + if (!data) { + *size = size_bytes; + return 0; + } else if (*data) { + if (*size < size_bytes) + return AVERROR_BUFFER_TOO_SMALL; + buf = *data; + } else { + buf = av_malloc(size_bytes); + if (!buf) + return AVERROR(ENOMEM); + } init_put_bits(pb, buf, size_bytes); diff --git a/libavutil/hdr_dynamic_metadata.h b/libavutil/hdr_dynamic_metadata.h index 771bb8f468..f3513807f6 100644 --- a/libavutil/hdr_dynamic_metadata.h +++ b/libavutil/hdr_dynamic_metadata.h @@ -353,13 +353,21 @@ AVDynamicHDRPlus *av_dynamic_hdr_plus_create_side_data(AVFrame *frame); int av_dynamic_hdr_plus_from_t35(AVDynamicHDRPlus *s, const uint8_t *data, size_t size); +#define AV_HDR_PLUS_MAX_PAYLOAD_SIZE 907 + /** * Serialize dynamic HDR10+ metadata to a user data registered ITU-T T.35 buffer, * excluding the first 48 bytes of the header, and beginning with the application mode. * @param s A pointer containing the decoded AVDynamicHDRPlus structure. - * @param data A pointer to a byte buffer to be allocated and filled - * with the serialized metadata. - * @param size A pointer to a size to be set to the returned buffer's size (optional). + * @param data[in,out] A pointer to pointer to a byte buffer to be filled with the + * serialized metadata. + * If *data is NULL, a buffer be will be allocated and a pointer to + * it stored in its place. The caller takes ownership of the buffer. + * May be NULL, in which case the function will only store the + * required buffer size in *size. + * @param size[in,out] A pointer to a size to be set to the returned buffer's size. + * If *data is not NULL, *size must contain the size of the input + * buffer. May be NULL only if *data is NULL. * * @return >= 0 on success. Otherwise, returns the appropriate AVERROR. */ -- 2.40.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".