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 406484A410 for ; Wed, 27 Mar 2024 19:17:09 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 79E1F68D68E; Wed, 27 Mar 2024 21:17:07 +0200 (EET) Received: from mail-pl1-f174.google.com (mail-pl1-f174.google.com [209.85.214.174]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 00D9D68C6BD for ; Wed, 27 Mar 2024 21:17:00 +0200 (EET) Received: by mail-pl1-f174.google.com with SMTP id d9443c01a7336-1e0f3052145so1903845ad.2 for ; Wed, 27 Mar 2024 12:17:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1711567018; x=1712171818; 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=ASSC9v2x8yiITSbg83+6wR0/CJBOmcuIkZXvd8Urqto=; b=bW8ztyBleJwfm3V3LZ9wRNAOZmMYzGn8yDjKXTTHL8zksSp6vP4Bnn96OkQUSuRhDI 8pwcuvL/4VtnvkmqBGca4I2zvNps0MPk3o3kD4t6bq8wClbk9PB+L7NuxonZJNfJEmAs er7VS+JKa7CCnPY7c+uXWcvmVCT1ptOx5uN2vNHh1vEeCeciL3bH19Wh5JNStLW8MbSt 26p5gwJRYlUVhbPAV5sildzSRMW/PMWh8EVkWztXsQ1X2ru3Kss55d7LGDjdAdo4WOSR Bhs0ncz36xSbIAT7UXpqGQyY+nNuCFK9+r3gX8Q1RkUdCF54Uc3Ug1KmxcvLpnP++tyw CRCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711567018; x=1712171818; 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=ASSC9v2x8yiITSbg83+6wR0/CJBOmcuIkZXvd8Urqto=; b=pzznT9Y3PQwWzz7JHZljPDtbY54zH9RMuUGaaSExEpB7fsQILbXdf47NLZz/DomvkB UnK1xgDH1HBymPY6+MopEa8GUD52Bsv6gP5Hl+Mjv/fuB/Jzc7wgiTgFQi/OVfmiaV4L xBSJw/VIIvMpCiLfhJ6nLz81er3F5vUsJ46pzGIgYSjCvF3vKY8VDXCVgmMM3qJLG14S 8796mN/YuMLZuXfGfij+cKxoP5vn+7O/l7BMb0ZDD6kJjnGm1v69rgXkLeh8rSoR2QWV 3tdzujIQOqwWgR/X5OfJ3CGzgh7+JE/FOsnertaU8uEO47/P9uFmhWKtK8AMB3m2ZKdy +uAw== X-Gm-Message-State: AOJu0YxhjQ6znAtd0+SpclOD0CXBlTPn4f15lCiBKcCMX1oR6cPdrMpM jCVbt5CWZFWPgJLVZ/1AflFQYh7Xi5rUrv0YRODKVVW0sSSwToLntZklyr5F X-Google-Smtp-Source: AGHT+IHa3sCjTJoMBuy5WfCylL1eXjWzITMte24N9JMZrYluwGnD3r9rJASgs7wc84oTHTsBjA0Dfg== X-Received: by 2002:a17:902:c409:b0:1e0:b574:421c with SMTP id k9-20020a170902c40900b001e0b574421cmr471594plk.44.1711567018393; Wed, 27 Mar 2024 12:16:58 -0700 (PDT) Received: from localhost.localdomain ([190.194.167.233]) by smtp.gmail.com with ESMTPSA id c2-20020a170903234200b001e20afa1038sm94806plh.8.2024.03.27.12.16.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Mar 2024 12:16:57 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Wed, 27 Mar 2024 16:16:48 -0300 Message-ID: <20240327191648.5017-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 02/10] 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 --- 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".