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 6655848CF2 for ; Fri, 23 Feb 2024 14:34:31 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2264E68CA84; Fri, 23 Feb 2024 16:31:58 +0200 (EET) Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id D731E68C67B for ; Fri, 23 Feb 2024 16:31:35 +0200 (EET) Authentication-Results: mail1.khirnov.net; dkim=pass (2048-bit key; unprotected) header.d=khirnov.net header.i=@khirnov.net header.a=rsa-sha256 header.s=mail header.b=XH+iNO/J; dkim-atps=neutral Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id C16164D48 for ; Fri, 23 Feb 2024 15:31:32 +0100 (CET) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id Xx2vOKgcXkec for ; Fri, 23 Feb 2024 15:31:32 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=khirnov.net; s=mail; t=1708698687; bh=8NSdbSecEDFo1db0XMw0pe5IL1N0PiNL4xz/vylmdXM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=XH+iNO/J8zO/4JTi1FpCHe43FKMu48JxqEuoyjdmbMI3QAfYOehLBX6nNtBGIVTx/ OmdMVbHAQOdStO5BBbU4ADd2UJDJIjkZA/xWCu8tUeKGsWG5hNNUsV+NMVdV2hKEBF nOR9fA1im+RQkE3lABeQw7MdgtStPzjYbxKAPMf35ppGZRR+4Hlnic0hbnLWCrbGmv 7fZewhMK4uLAmEwdxmnMwAhUn/IEf03yX/mOZ6RRvqD1iyJrAGCoVDJNH9+RDNSBS2 9lvtLQHqL4+zkJmu+u/mGOY7ziVv9OL51WRp5b2gR3fHJDuCzeXLv4vLMBRM8SZyuB bjRVQ5CdRXsNw== Received: from libav.khirnov.net (libav.khirnov.net [IPv6:2a00:c500:561:201::7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "libav.khirnov.net", Issuer "smtp.khirnov.net SMTP CA" (verified OK)) by mail1.khirnov.net (Postfix) with ESMTPS id 96F2A4D49 for ; Fri, 23 Feb 2024 15:31:27 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 98D193A1E00 for ; Fri, 23 Feb 2024 15:31:22 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 23 Feb 2024 14:58:39 +0100 Message-ID: <20240223143115.16521-18-anton@khirnov.net> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240223143115.16521-1-anton@khirnov.net> References: <20240223143115.16521-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 17/38] avcodec: add internal side data wrappers 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: From: Niklas Haas The signature of ff_frame_new_side_data got more complicated due to a need to distinguish between "failed allocating side data" and "side data was already present". We could do something similar to ff_frame_new_side_data_from_buf, but most callers ignore the OOM condition on this function, which is merely re-allocating the side data array. So preserve the return signature to make it slightly less of a pain to use. Signed-off-by: Anton Khirnov --- libavcodec/decode.c | 47 +++++++++++++++++++++++++++++++++++++++++++++ libavcodec/decode.h | 20 +++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/libavcodec/decode.c b/libavcodec/decode.c index 5524e229c2..10946f208a 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -1857,6 +1857,53 @@ int ff_decode_preinit(AVCodecContext *avctx) return 0; } +/** + * Check side data preference and clear existing side data from frame + * if needed. + * + * @retval 0 side data of this type can be added to frame + * @retval 1 side data of this type should not be added to frame + */ +static int side_data_pref(const AVCodecContext *avctx, AVFrame *frame, + enum AVFrameSideDataType type) +{ + DecodeContext *dc = decode_ctx(avctx->internal); + + // Note: could be skipped for `type` without corresponding packet sd + if (av_frame_get_side_data(frame, type)) { + if (dc->side_data_pref_mask & (1ULL << type)) + return 1; + av_frame_remove_side_data(frame, type); + } + + return 0; +} + + +int ff_frame_new_side_data(const AVCodecContext *avctx, AVFrame *frame, + enum AVFrameSideDataType type, size_t size, + AVFrameSideData **sd) +{ + if (side_data_pref(avctx, frame, type)) { + *sd = NULL; + return 0; + } + + *sd = av_frame_new_side_data(frame, type, size); + return *sd ? 0 : AVERROR(ENOMEM); +} + +AVFrameSideData *ff_frame_new_side_data_from_buf(const AVCodecContext *avctx, + AVFrame *frame, + enum AVFrameSideDataType type, + AVBufferRef *buf) +{ + if (side_data_pref(avctx, frame, type)) + return NULL; + + return av_frame_new_side_data_from_buf(frame, type, buf); +} + int ff_copy_palette(void *dst, const AVPacket *src, void *logctx) { size_t size; diff --git a/libavcodec/decode.h b/libavcodec/decode.h index daf1a67444..a131b9940a 100644 --- a/libavcodec/decode.h +++ b/libavcodec/decode.h @@ -155,4 +155,24 @@ int ff_hwaccel_frame_priv_alloc(AVCodecContext *avctx, void **hwaccel_picture_pr const AVPacketSideData *ff_get_coded_side_data(const AVCodecContext *avctx, enum AVPacketSideDataType type); +/** + * Wrapper around av_frame_new_side_data, which rejects side data overridden by + * the demuxer. Returns 0 on success, and a negative error code otherwise. + * If successful, *sd may either be a pointer to the new side data, or NULL + * in case the side data was already present. + */ +int ff_frame_new_side_data(const AVCodecContext *avctx, AVFrame *frame, + enum AVFrameSideDataType type, size_t size, + AVFrameSideData **sd); + +/** + * Similar to `ff_frame_new_side_data`, but using an existing buffer ref. + * On success, returns the newly added side data, and passes ownership + * of `buf` to the frame. + */ +AVFrameSideData *ff_frame_new_side_data_from_buf(const AVCodecContext *avctx, + AVFrame *frame, + enum AVFrameSideDataType type, + AVBufferRef *buf); + #endif /* AVCODEC_DECODE_H */ -- 2.42.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".