From patchwork Wed Mar 27 19:22:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 47548 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:9f96:b0:1a3:b6bb:3029 with SMTP id mm22csp849832pzb; Wed, 27 Mar 2024 12:22:57 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUsJ//G5Y/LFQxPy4+iiBn7kuykcVBzR+OBGKon2vJMkr8+Qw2sjZ1F94stFYtkm8JPsJF0fgSv44p3d4VOgG5aetnGDBf2ikf+lA== X-Google-Smtp-Source: AGHT+IEuiINKDXuqD0CAerOIHILb0L5dNKwylU1xOgXWRLFV+d9HsczpChN/LNu+HoIu4Rr6n8xb X-Received: by 2002:a17:906:b1ce:b0:a4d:f681:c964 with SMTP id bv14-20020a170906b1ce00b00a4df681c964mr190885ejb.28.1711567377740; Wed, 27 Mar 2024 12:22:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1711567377; cv=none; d=google.com; s=arc-20160816; b=dh+Itp7l65XXx694qjIgXQrrtp1aXla68rbPQY0LKO4NhDlAUObtTUlZY+PsQ4mgz+ PJhjDDS7jiEPkKrWJWJDz6V/a8IaC5AHSFkynmdOFfrM0ftM4medEoUe6b1fPICwayQ1 VWYR/oe7shXYIg8bODVfAPFZ7uelBRzWX4DbahQTkdFmxas+vph+JQfHMdlTxu7v+Y0N 7ZF0vmBVa3hN+yixbQldg+S1RKqa/VhSSrvK0wSqMVdh+6mCNKRuOdV86V22c/YQA96t wCDDHTDdUANepDgsVwWR2yh2vZXO0DU1chbzpOGOzh33Zsgpc7sBk0ioKoHLewwQ2Hsr gUww== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=h5M6aQSbBH/xdcGOAr91Cg6pwwc5oM9DUWEmfPfBaoY=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=zvRzICONvpU85ROt0zOdAON9dGgfd4XTkcK0L7whqzMsgYkvLdhatuC4z5GG/Nso8F A7HOkw+TsBQAshX7W3reizfQBejbg0Cix6FgWFZEVq/D3wOIGhhCVLfA+hTRPYkWFr7C mrWBPJJgyU+H6dOxtzuFn6eZAODHiCzgDdH4elhUVrKJTdEq+PuQaE4QqCXRGGK2IWLk VmrA/+/AuUxZVLqafztHyaCub6HdX1AjmnFf0MRRfqi2KHzNMVpHmcU5hU3ZizCQpUHk 85IkYVbpmvwdQa41dsXGufv1o4Y1+qdAWBftDb5AH9tRb/rionf2nxNnJ07qiXzManWT gr3g==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=Whv1YZQJ; 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=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id s9-20020a056402520900b0056bf5b99ae7si4938384edd.14.2024.03.27.12.22.57; Wed, 27 Mar 2024 12:22:57 -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=@gmail.com header.s=20230601 header.b=Whv1YZQJ; 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=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 260F668D6B9; Wed, 27 Mar 2024 21:22:54 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org 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-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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: AqTq+oOdjEz+ 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);