From patchwork Fri Dec 31 10:53:05 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 32958 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp13864536iog; Fri, 31 Dec 2021 02:54:02 -0800 (PST) X-Google-Smtp-Source: ABdhPJyGHWfvfGNwSyhYX0XpaX+AHRML6MPe2iZXgUwH6vTWDy2/P3EWxSu0Dfvk7kaVKAy7kvlM X-Received: by 2002:a05:6402:f0f:: with SMTP id i15mr3672981eda.97.1640948042038; Fri, 31 Dec 2021 02:54:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1640948042; cv=none; d=google.com; s=arc-20160816; b=selkQdVzyweHA+t0AJKqcgBPR+6/NymwFBKTLfOocixtiCU6/UUt8p3fmez5XZ0Hy1 LujxzouZYDzqzQCI5zhS83blOWqoQFamXbCSFsa76S6nl8BPgewfEICKZZ/dKyYMP0Ry N1QjfJpJsQwywabeYDhA82yxOTqswWeWLf5pRnxL/mvzDfEPzbAlpgRahV0NuaHS5S/E U2G6bSllSzMPlJ5YtBjVSS7rxNh7JzF5zbSCgxd5Y4Ym8ZPic4I55GuaPj0eC+bf5IuL 4VzUwHBVFSm0/tc8XM+mbc1au3FCsHJWWZdLblT7OAvu4sDGQ0e8CrObQSzKDWQt43uA Ik0g== 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:delivered-to; bh=5HP3smqSBRlYpPBEtmurvxv+9M5jI/vIuD4IEjPNgUk=; b=Fa+m0/28nB0QcmvBAw42pvyKNOOP7PwxX/WOm/1IQHkDn+FXV9l/Wbj70tL6IJATkF K4eFElnexYokJLRP872dtUnuIh6osD9U68xXehYihZfQEnk95YzzNLNo6Yt8N13aC15t X85e/YIVl0/igIQpsn4VHqt1Q8dthbQaKdbQxAqW+qY8cZYAlX0apOJvV/YkRjvbm9BO CGJUtKQyNrTjQPqPksWIRf2PG+oGvfVWl5JvRWUiRcBPcqtQWS8IL2uihXeFb8n0hNmU 1AFIHTekAYD0U3/pLXgfl3xNiBmDvyLAIAhRNQ3TOcAQm8eqBghcnauMa2GgtQY650Jt xOQg== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ox26si11774477ejb.331.2021.12.31.02.54.01; Fri, 31 Dec 2021 02:54:02 -0800 (PST) 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; 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 Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A932668AFF0; Fri, 31 Dec 2021 12:53:31 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (red.khirnov.net [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C8C42680044 for ; Fri, 31 Dec 2021 12:53:22 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 24B34240179 for ; Fri, 31 Dec 2021 11:53:22 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id a3NcqjqwogKY for ; Fri, 31 Dec 2021 11:53:21 +0100 (CET) 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 mail0.khirnov.net (Postfix) with ESMTPS id 961A124017E for ; Fri, 31 Dec 2021 11:53:19 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id A59E33A0746; Fri, 31 Dec 2021 11:53:19 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Fri, 31 Dec 2021 11:53:05 +0100 Message-Id: <20211231105307.30946-5-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211231105307.30946-1-anton@khirnov.net> References: <20211231105307.30946-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 5/7] lavu/fifo: do not copy the whole fifo when reallocating 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: DT3baWfGh7Ub av_realloc() the buffer and only move the part of the ring buffer that needs it. Also avoids allocating a temporary fifo. --- libavutil/fifo.c | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/libavutil/fifo.c b/libavutil/fifo.c index 5eee18a8c8..171c1aa9cd 100644 --- a/libavutil/fifo.c +++ b/libavutil/fifo.c @@ -27,7 +27,7 @@ AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size) { AVFifoBuffer *f; - void *buffer = av_malloc_array(nmemb, size); + void *buffer = av_realloc_array(NULL, nmemb, size); if (!buffer) return NULL; f = av_mallocz(sizeof(AVFifoBuffer)); @@ -83,17 +83,31 @@ int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size) unsigned int old_size = f->end - f->buffer; if (old_size < new_size) { - int len = av_fifo_size(f); - AVFifoBuffer *f2 = av_fifo_alloc(new_size); + size_t offset_r = f->rptr - f->buffer; + size_t offset_w = f->wptr - f->buffer; + uint8_t *tmp; - if (!f2) + tmp = av_realloc(f->buffer, new_size); + if (!tmp) return AVERROR(ENOMEM); - av_fifo_generic_read(f, f2->buffer, len, NULL); - f2->wptr += len; - f2->wndx += len; - av_free(f->buffer); - *f = *f2; - av_free(f2); + + // move the data from the beginning of the ring buffer + // to the newly allocated space + // the second condition distinguishes full vs empty fifo + if (offset_w <= offset_r && av_fifo_size(f)) { + const size_t copy = FFMIN(new_size - old_size, offset_w); + memcpy(tmp + old_size, tmp, copy); + if (copy < offset_w) { + memmove(tmp, tmp + copy , offset_w - copy); + offset_w -= copy; + } else + offset_w = old_size + copy; + } + + f->buffer = tmp; + f->end = f->buffer + new_size; + f->rptr = f->buffer + offset_r; + f->wptr = f->buffer + offset_w; } return 0; }