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 B149B49BAA for ; Mon, 4 Mar 2024 13:07:49 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id AEF9868D474; Mon, 4 Mar 2024 15:07:17 +0200 (EET) Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C37DB68D3A9 for ; Mon, 4 Mar 2024 15:07:08 +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=a3Pq810z; dkim-atps=neutral Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id C8A034D4C for ; Mon, 4 Mar 2024 14:07:07 +0100 (CET) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id VprrrcVSGfWo for ; Mon, 4 Mar 2024 14:07:07 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=khirnov.net; s=mail; t=1709557626; bh=eLMoH5353mquqMr+oUxgL3bROM94gBVzMBkggM/e8z8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=a3Pq810zH5zPwXLg5re7cmhsnVTVYk33BzoQ0A4rkZ5QCd7TmKYs/s/KecBMZAFpH giVgrSV936M+VcJMfuEGXRYk2ecJsa6G1qXvrpxXVLb1FHfQFrzsWbQF7KRl6XNyCh 1apdfDOtzXe91EWc21XxmMLfQu1sXUhJ5CruFXmdeXl6X7R/gnXiLHoWIDegzPIfYy efImVQ8pwfIRbOQN6b3vnXNGciIuiani9MJMO+Jm7SY2TBZtljJ/t6CcEZ20pZ0mdz Qa+BzU0i16FsMtlExgQ4ftPNV5pt/CpnPNqBs0vSrpEaKWu0CaICWigudMwgZrBZgc 6dB3Q7KXVwqaA== 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 3B6A24D42 for ; Mon, 4 Mar 2024 14:07:06 +0100 (CET) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id 393CD3A0DA6 for ; Mon, 4 Mar 2024 14:07:00 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Mon, 4 Mar 2024 14:06:23 +0100 Message-ID: <20240304130657.30631-8-anton@khirnov.net> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240304130657.30631-1-anton@khirnov.net> References: <20240304130657.30631-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 08/29] 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 these wrappers is more complicated due to a need to distinguish between "failed allocating side data" and "side data was already present". Signed-off-by: Anton Khirnov --- libavcodec/decode.c | 66 +++++++++++++++++++++++++++++++++++++++++++++ libavcodec/decode.h | 20 ++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/libavcodec/decode.c b/libavcodec/decode.c index 5c80ef9cd0..f3e8b72be7 100644 --- a/libavcodec/decode.c +++ b/libavcodec/decode.c @@ -1857,6 +1857,72 @@ 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 **psd) +{ + AVFrameSideData *sd; + + if (side_data_pref(avctx, frame, type)) { + if (psd) + *psd = NULL; + return 0; + } + + sd = av_frame_new_side_data(frame, type, size); + if (psd) + *psd = sd; + + return sd ? 0 : AVERROR(ENOMEM); +} + +int ff_frame_new_side_data_from_buf(const AVCodecContext *avctx, + AVFrame *frame, enum AVFrameSideDataType type, + AVBufferRef **buf, AVFrameSideData **psd) +{ + AVFrameSideData *sd = NULL; + int ret = 0; + + if (side_data_pref(avctx, frame, type)) + goto finish; + + sd = av_frame_new_side_data_from_buf(frame, type, *buf); + if (sd) + *buf = NULL; + else + ret = AVERROR(ENOMEM); + +finish: + av_buffer_unref(buf); + if (psd) + *psd = sd; + + return ret; +} + 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..b269b5a43b 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 and sd is not NULL, *sd may either contain 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. + * + * *buf is ALWAYS consumed by this function and NULL written in its place, even + * on failure. + */ +int ff_frame_new_side_data_from_buf(const AVCodecContext *avctx, + AVFrame *frame, enum AVFrameSideDataType type, + AVBufferRef **buf, AVFrameSideData **sd); + #endif /* AVCODEC_DECODE_H */ -- 2.43.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".