From patchwork Wed Aug 3 16:02:46 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 37125 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:1649:b0:8b:613a:194d with SMTP id no9csp531318pzb; Wed, 3 Aug 2022 09:04:05 -0700 (PDT) X-Google-Smtp-Source: AA6agR61UwbjD6C/Bhm/tanCyo/LD7+xnErLx6jPUEZApJfLwRIisi3cP2Dk0R44eJlquyJP0re2 X-Received: by 2002:a17:906:8a4e:b0:730:9fcd:d988 with SMTP id gx14-20020a1709068a4e00b007309fcdd988mr6808106ejc.636.1659542644905; Wed, 03 Aug 2022 09:04:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1659542644; cv=none; d=google.com; s=arc-20160816; b=yxEB4tS56zq+MxApfqA/smJ6HbGALmfpqTNpxp6vPrpTN39SPjPsAoV9PDkS3KtKFI iUpMOw1G+VOHU9pEZY+6ixHKd1shUDk0U7Cbtow/VkHzjqkXe/sIGAaKHwJzW081uzLn 4obe0IEpItSrKwvdMaKlx/g2+seOiOS6M83gwYU0DqblSLs541IDVuVp2MJ9g5RLrHKM xKZcC3xFZhthXpNlKprT1bEUB/vB+MVtzBLGPbWsI/7O7U+oqTEakf/mQxxKKVf8wZa3 xvtm3nfhuVA5XDTVDuQZSBgyp7RG6ReobZYLTG/Nbo+5QD0r5RpRc5j0YUcOTPi6qwcf strA== 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:message-id:date:to:from :dkim-signature:delivered-to; bh=P17mEzw/ImlZWkCHgCPLLkIEFFbik/ntLSZ/BsJ6A6w=; b=i2HowoI/B85pP7nZpzdUJntQqqltRj0Hbb/evcX0+y7PHRzsSkXBzM8aaZedLr78v4 0EDjfe5nD67N9wbwk/yLSycF5AMj0IPXTGulSC7dbxaDWeutizqfkZm3qdZahWKJMvlB 4bfhJFyo/nlbYR38MwTAet8Q2ZtcXq1XJp3fyCylpYOl5xx/lcV7JtXG0cxymI4IUQpb 4JgqpUzM8Z2fDYbunXvmHWdpVEkOeC7yUK+BHX2ypSG3NPXqO4jIvKa1bOCjl2cQKlQ1 Y8aS4aig2qGqV1mwpDMjWAXCd1uNQsHqwxLJSnXhq3+GrKAOFzq53k8EnMp3Sf/UpMhg iJUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20210112 header.b=h8LuucmP; 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 s7-20020aa7d787000000b0043be773a90dsi12390773edq.45.2022.08.03.09.04.04; Wed, 03 Aug 2022 09:04:04 -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=20210112 header.b=h8LuucmP; 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 48AFE68B7A0; Wed, 3 Aug 2022 19:03:49 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-qv1-f45.google.com (mail-qv1-f45.google.com [209.85.219.45]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7B94D68B935 for ; Wed, 3 Aug 2022 19:03:42 +0300 (EEST) Received: by mail-qv1-f45.google.com with SMTP id q3so13231015qvp.5 for ; Wed, 03 Aug 2022 09:03:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc; bh=dNz9U3y7uWA31Ap2gtj1mPR6wTDw0t+YvvtBe+681EY=; b=h8LuucmPmCoejB8D2bguFwi6ZGK+9TsZmt9Rlbb4SczfjuGfJEMsUhpfCPcy9wUuiv fvqXPqe2LMSGLka83HQaQnLR2oU+mNqCsxsIEQ6d9F3qnSLqitw+xA4lK2j64IZOLV86 XzY+6SXi83hgnIZSXSXVQ0DMTHEGi8EyLA84BwwOriCgJr4LTqkdAjsKpwKoCAlYN8tg Ku7G0r8tQEXMBMY+2YBgd2Oy54k5BtvT/ACSPEt9SosjMQH4nFidrAWN1xijSkvjZyik nNRQ5qwnMScQxXYbPHSQVBtPYU/FmndkRs6eGj5Jmd+KXaj9Uc+tgSWXDDT4CqPRM2G3 q5iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc; bh=dNz9U3y7uWA31Ap2gtj1mPR6wTDw0t+YvvtBe+681EY=; b=sCUB/j2fMFNuC0t5R62al+d/X3qZ+K7lMCoBW/eKJVz/WUTQ2mb1+pUJ/AENSzcY1e FlBSoYS7gFg8I4pCUOME70kkmtrr1rVtOcLhiM7oIdNSnv9IWYvJIDJHtzNtB48TX+UG HmeOTKP497IXDsS6o7CnsgliMgfrFBrdq1bAacQ2pN/wRlZdNyDwQUGi9Bb4sSfGA7fy R1LXrgzU8qIx0iXl9Y8fcA2W1mNaEQ4I5jJf0UsrIM4wDassnNTJN87aCyOHKUbyMdQI PNahpZT340HAh2JdqJRmTYiemAQ1M2GCxsMWhZI5bXqVI9nHfGXFIzOKOE47pZO9Et4T DHiQ== X-Gm-Message-State: ACgBeo0W/ppIuOCXDQnqykKVgo18eM+yo/vcqIu13x70RmKMrq7+SSLC A7OEJ2ZQgh9tYoCPjIrIh5qmZIjm108= X-Received: by 2002:a67:ff91:0:b0:388:5133:660f with SMTP id v17-20020a67ff91000000b003885133660fmr1555770vsq.52.1659542610114; Wed, 03 Aug 2022 09:03:30 -0700 (PDT) Received: from localhost.localdomain ([186.136.131.204]) by smtp.gmail.com with ESMTPSA id p4-20020ab06244000000b00384641c2bd1sm10548418uao.26.2022.08.03.09.03.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Aug 2022 09:03:29 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Wed, 3 Aug 2022 13:02:46 -0300 Message-Id: <20220803160248.4190-1-jamrial@gmail.com> X-Mailer: git-send-email 2.37.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/3 v4] avutil/frame: add av_frame_replace 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: Tt6M3jxWyGkc Signed-off-by: James Almer --- libavutil/frame.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++ libavutil/frame.h | 13 +++++ 2 files changed, 137 insertions(+) diff --git a/libavutil/frame.c b/libavutil/frame.c index de4ad1f94d..864c66371f 100644 --- a/libavutil/frame.c +++ b/libavutil/frame.c @@ -471,6 +471,130 @@ fail: return ret; } +int av_frame_replace(AVFrame *dst, const AVFrame *src) +{ + int ret = 0; + + if (dst == src) + return AVERROR(EINVAL); + + if (!src->buf[0]) { + av_frame_unref(dst); + + /* duplicate the frame data if it's not refcounted */ + if ( src->data[0] || src->data[1] + || src->data[2] || src->data[3]) + return av_frame_ref(dst, src); + return 0; + } + + dst->format = src->format; + dst->width = src->width; + dst->height = src->height; + dst->nb_samples = src->nb_samples; +#if FF_API_OLD_CHANNEL_LAYOUT +FF_DISABLE_DEPRECATION_WARNINGS + dst->channels = src->channels; + dst->channel_layout = src->channel_layout; + if (!av_channel_layout_check(&src->ch_layout)) { + av_channel_layout_uninit(&dst->ch_layout); + if (src->channel_layout) + av_channel_layout_from_mask(&dst->ch_layout, src->channel_layout); + else { + dst->ch_layout.nb_channels = src->channels; + dst->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; + } + } else { +#endif + ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout); + if (ret < 0) + goto fail; +#if FF_API_OLD_CHANNEL_LAYOUT + } +FF_ENABLE_DEPRECATION_WARNINGS +#endif + + wipe_side_data(dst); + av_dict_free(&dst->metadata); + ret = frame_copy_props(dst, src, 0); + if (ret < 0) + goto fail; + + /* replace the buffers */ + for (int i = 0; i < FF_ARRAY_ELEMS(src->buf); i++) { + ret = av_buffer_replace(&dst->buf[i], src->buf[i]); + if (ret < 0) + goto fail; + } + + if (src->extended_buf) { + if (dst->nb_extended_buf != src->nb_extended_buf) { + int nb_extended_buf = FFMIN(dst->nb_extended_buf, src->nb_extended_buf); + void *tmp; + + for (int i = nb_extended_buf; i < dst->nb_extended_buf; i++) + av_buffer_unref(&dst->extended_buf[i]); + + tmp = av_realloc_array(dst->extended_buf, sizeof(*dst->extended_buf), + src->nb_extended_buf); + if (!tmp) { + ret = AVERROR(ENOMEM); + goto fail; + } + dst->extended_buf = tmp; + dst->nb_extended_buf = src->nb_extended_buf; + + memset(&dst->extended_buf[nb_extended_buf], 0, + (src->nb_extended_buf - nb_extended_buf) * sizeof(*dst->extended_buf)); + } + + for (int i = 0; i < src->nb_extended_buf; i++) { + ret = av_buffer_replace(&dst->extended_buf[i], src->extended_buf[i]); + if (ret < 0) + goto fail; + } + } else if (dst->extended_buf) { + for (int i = 0; i < dst->nb_extended_buf; i++) + av_buffer_unref(&dst->extended_buf[i]); + av_freep(&dst->extended_buf); + } + + ret = av_buffer_replace(&dst->hw_frames_ctx, src->hw_frames_ctx); + if (ret < 0) + goto fail; + + if (dst->extended_data != dst->data) + av_freep(&dst->extended_data); + + if (src->extended_data != src->data) { + int ch = dst->ch_layout.nb_channels; + + if (!ch) { + ret = AVERROR(EINVAL); + goto fail; + } + + if (ch > SIZE_MAX / sizeof(*dst->extended_data)) + goto fail; + + dst->extended_data = av_memdup(src->extended_data, sizeof(*dst->extended_data) * ch); + if (!dst->extended_data) { + ret = AVERROR(ENOMEM); + goto fail; + } + } else + dst->extended_data = dst->data; + + memcpy(dst->data, src->data, sizeof(src->data)); + memcpy(dst->linesize, src->linesize, sizeof(src->linesize)); + + return 0; + +fail: + av_frame_unref(dst); + return ret; +} + AVFrame *av_frame_clone(const AVFrame *src) { AVFrame *ret = av_frame_alloc(); diff --git a/libavutil/frame.h b/libavutil/frame.h index 6d9563bc5d..859ccf6c6c 100644 --- a/libavutil/frame.h +++ b/libavutil/frame.h @@ -762,6 +762,19 @@ void av_frame_free(AVFrame **frame); */ int av_frame_ref(AVFrame *dst, const AVFrame *src); +/** + * Ensure the destination frame refers to the same data described by the source + * frame by creating a new reference for each AVBufferRef from src if they + * differ from those in dst, or if src is not reference counted, by allocating + * new buffers and copying data. + * + * Frame properties on dst will be replaced by those from src. + * + * @return 0 on success, a negative AVERROR on error. On error, dst is + * unreferenced. + */ +int av_frame_replace(AVFrame *dst, const AVFrame *src); + /** * Create a new frame that references the same data as src. *