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 E29A54A412 for ; Wed, 27 Mar 2024 19:22:56 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E817668D6AE; Wed, 27 Mar 2024 21:22:53 +0200 (EET) Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 0EE1968D654 for ; Wed, 27 Mar 2024 21:22:48 +0200 (EET) Received: by mail-pg1-f174.google.com with SMTP id 41be03b00d2f7-5d4d15ec7c5so97170a12.1 for ; Wed, 27 Mar 2024 12:22:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711567365; x=1712172165; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=kPqHDqt8OBlNE3OYf16ziKF5r+3QWfPjhSJwLQwNENc=; b=Whv1YZQJ6mWrEe3pWqURiiBEgJwZB4D/dQlg9Eop/St/9avzqRLU+RaafQ32D1Q9LD aZFMK6ZxxoRqfzJ59fDUHs4M4VMtmZaNApA2rlVi/EIhfoljJ0AAdOVTtovUBqhH5nss x8EKXEBrbZcG1wN/XgU+SV+nDaVvchvc+YfUR1Er9fT8RLr1/WJFIsyni0XDj/maIpet 7hiNf+YggpinWbVXFkAS6JOak9hWWM7Ylt5glijAwpeUmZOcdxraYBly1mOrZTjf4yl2 SoZgEozsse12lf+gYRKv53NkENahTN/+3ubflsHFsGug7b1AKBnv5uYILETroRXjoRvR 9G3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711567365; x=1712172165; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kPqHDqt8OBlNE3OYf16ziKF5r+3QWfPjhSJwLQwNENc=; b=EwHOj1YgX3P7xnm6uesE/7RwVGHK9kFnNSZ554NAPDVtLRg81S0/cf4/Ua9lAx9AK7 ZEWa28IHwAfz2UMXwFXvNb4nqu8MtLuXoY4b9kbEIU+JUYaCEMvvuFIKkPJsXOlMP4ib ct+DlorPxqbiLMkq6cZBHsgRHP/lLSmemU6LUEePUGktHESeAgDmg0URqbqVaRAKRU2F WxLFFe4gJgr5oxmxa+8FZ7+zd2t9ygYvt3xuS2VBaNRh8AZj0oED7Pguw8Qe9zc36uyx nZCRHfae3Lnju5KHg4jVduaJRkPaxI8lwdGxqggwWdk4/qkBEi/70oL9xgk4ZGFkpFWh SiCw== X-Gm-Message-State: AOJu0YzlEGoZnjhHXbo5V/+gN+V1roH6Yldr5Dbo3ahIvmA9To9MODVq ii2M6/4FRo9lTsQD5/GMYHwszjTM8guOYwl+HtbZ8x/SxFK/nj2VRkl+qLqe X-Google-Smtp-Source: AGHT+IEz0RP9xP098QYASjYCwC8jcJckjn1cPlFUAdOFcWzQ/o7Ux+7raGc6rYrnXGdf81h1mDvJWg== X-Received: by 2002:a17:902:f647:b0:1e0:36c7:44a3 with SMTP id m7-20020a170902f64700b001e036c744a3mr880881plg.44.1711567365026; Wed, 27 Mar 2024 12:22:45 -0700 (PDT) Received: from localhost.localdomain ([190.194.167.233]) by smtp.gmail.com with ESMTPSA id p18-20020a170903249200b001dc6f7e794dsm9393821plw.16.2024.03.27.12.22.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 12:22:44 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Wed, 27 Mar 2024 16:22:29 -0300 Message-ID: <20240327192229.5028-1-jamrial@gmail.com> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240325200602.63020-2-jamrial@gmail.com> References: <20240325200602.63020-2-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/6 v3] avutil/frame: add helper for adding side data w/ AVBufferRef to array 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: Signed-off-by: James Almer --- Same as the wrongly named 02/10 i sent earlier. libavutil/frame.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++ libavutil/frame.h | 34 ++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/libavutil/frame.c b/libavutil/frame.c index 2e40018b83..08b6475cc2 100644 --- a/libavutil/frame.c +++ b/libavutil/frame.c @@ -834,6 +834,59 @@ AVFrameSideData *av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd, return ret; } +AVFrameSideData *av_frame_side_data_add(AVFrameSideData ***sd, int *nb_sd, + enum AVFrameSideDataType type, + AVBufferRef **pbuf, unsigned int flags) +{ + const AVSideDataDescriptor *desc = av_frame_side_data_desc(type); + AVFrameSideData *sd_dst = NULL; + AVBufferRef *buf; + + if (!sd || !pbuf || *pbuf || !nb_sd || (*nb_sd && !*sd)) + return NULL; + + if (flags & AV_FRAME_SIDE_DATA_FLAG_UNIQUE) + remove_side_data(sd, nb_sd, type); + if (!desc || !(desc->props & AV_SIDE_DATA_PROP_MULTI)) { + for (int i = 0; i < *nb_sd; i++) { + AVFrameSideData *entry = ((*sd)[i]); + + if (entry->type != type) + continue; + if (!(flags & AV_FRAME_SIDE_DATA_FLAG_REPLACE)) + return NULL; + + buf = *pbuf; + if (flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF) { + int ret = av_buffer_replace(&entry->buf, buf); + if (ret < 0) + return NULL; + } else + *pbuf = NULL; + + av_dict_free(&entry->metadata); + entry->data = buf->data; + entry->size = buf->size; + return 0; + } + } + + buf = (flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF) ? + av_buffer_ref(*pbuf) : *pbuf; + + sd_dst = add_side_data_from_buf(sd, nb_sd, type, buf); + if (!sd_dst) { + if (flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF) + av_buffer_unref(&buf); + return NULL; + } + + if (!(flags & AV_FRAME_SIDE_DATA_FLAG_NEW_REF)) + *pbuf = NULL; + + return sd_dst; +} + int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd, const AVFrameSideData *src, unsigned int flags) { diff --git a/libavutil/frame.h b/libavutil/frame.h index 2ea129888e..3e5d170a5b 100644 --- a/libavutil/frame.h +++ b/libavutil/frame.h @@ -1048,6 +1048,10 @@ void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd); * Don't add a new entry if another of the same type exists. */ #define AV_FRAME_SIDE_DATA_FLAG_REPLACE (1 << 1) +/** + * Create a new reference instead of taking ownership of the passed in one. + */ +#define AV_FRAME_SIDE_DATA_FLAG_NEW_REF (1 << 2) /** * Add new side data entry to an array. @@ -1066,11 +1070,40 @@ void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd); * is attempted. * @note In case of AV_FRAME_SIDE_DATA_FLAG_REPLACE being set, if an * entry of the same type already exists, it will be replaced instead. + * @note AV_FRAME_SIDE_DATA_FLAG_NEW_REF has no effect in this function. */ AVFrameSideData *av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd, enum AVFrameSideDataType type, size_t size, unsigned int flags); +/** + * Add a new side data entry to an array from an existing AVBufferRef. + * + * @param sd pointer to array of side data to which to add another entry, + * or to NULL in order to start a new array. + * @param nb_sd pointer to an integer containing the number of entries in + * the array. + * @param type type of the added side data + * @param buf Pointer to AVBufferRef to add to the array. On success, + * the function takes ownership of the AVBufferRef and *buf is + * set to NULL, unless AV_FRAME_SIDE_DATA_FLAG_NEW_REF is set + * in which case the ownership will remain with the caller. + * @param flags Some combination of AV_FRAME_SIDE_DATA_FLAG_* flags, or 0. + * + * @return newly added side data on success, NULL on error. + * @note In case of AV_FRAME_SIDE_DATA_FLAG_UNIQUE being set, entries of + * matching AVFrameSideDataType will be removed before the addition + * is attempted. + * @note In case of AV_FRAME_SIDE_DATA_FLAG_REPLACE being set, if an + * entry of the same type already exists, it will be replaced instead. + * @note In case of AV_FRAME_SIDE_DATA_FLAG_NEW_REF being set, the ownership + * of *buf will remain with the caller. + * + */ +AVFrameSideData *av_frame_side_data_add(AVFrameSideData ***sd, int *nb_sd, + enum AVFrameSideDataType type, + AVBufferRef **buf, unsigned int flags); + /** * Add a new side data entry to an array based on existing side data, taking * a reference towards the contained AVBufferRef. @@ -1089,6 +1122,7 @@ AVFrameSideData *av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd, * is attempted. * @note In case of AV_FRAME_SIDE_DATA_FLAG_REPLACE being set, if an * entry of the same type already exists, it will be replaced instead. + * @note AV_FRAME_SIDE_DATA_FLAG_NEW_REF has no effect in this function. */ int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd, const AVFrameSideData *src, unsigned int flags); -- 2.44.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".