From patchwork Tue Jan 11 20:45:40 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 33238 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp4047890iog; Tue, 11 Jan 2022 12:48:12 -0800 (PST) X-Google-Smtp-Source: ABdhPJxDNu65/bEBnfxcupIeB9zjsMEo8rEaiKSlVP2cyMucpS5Urtc3Z+TWfNcQtoCx1aMdSZk1 X-Received: by 2002:a17:907:1b29:: with SMTP id mp41mr5359575ejc.591.1641934092323; Tue, 11 Jan 2022 12:48:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1641934092; cv=none; d=google.com; s=arc-20160816; b=i1DfM+1gABWPab6Z3JxHL+XRKVmSKM/BlryoXh3EohrOKnroiWsc5hgTbQxkxeYQp5 0cGzeYdNDdDIVLwv1F42/OWGi8wjziKHKavomeB1MmYuXzAdL2/zxNsKZ8sjYSx27zpf myy5NioZRowbZ6UIxM217KRjY0xGttsy2uqin398f5FSwTBsw5UWQnEY8lp7R5w9a1rx kQEkhsAqXOD1rFSxWxLcnjzUQAegUBrp15KQNt4SAvVN/MyjF1rWuM6VXTopeDSUsb6m Nqi3cmxTqYqu4I2FYvCz7vBySy+yX5FnIY4JXTuCJjz/Mme6sXrAOJsUKFkpleVICrhJ nCvg== 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=oq4qra+1a/3aI9rryqNhluZDwCbHndLYgIS9v8oeMmc=; b=s+sTYEH9vFJPDc215OFmHgkv1Eqh5zmaY2IVrytvoS/53aBWNw8WFN4gxwQMAARXg4 N/1MdEdPyU+QsXmpuwsi0EykNhjbdfciwxMfdBHlsxw6sqGguXU0uEdPZVMXssl80ViU iv8m5qmUVKSD9UdYlMyt0IlBIdCKJS6Iub/34ZIU0JWyLemZSpimfw2qg7oOYq/5YE/6 8U78LekpvEsn6/DlftYNxmLN/xoCW0nqTxS07vNUZK19HpxPDS5+nk5qawFOKWW/KGrR YAIoTnddSySLU5wJKb1xZbWiFgxx/LhBqM9YF3b8BQH9BB1olXjNzA+8tkr0WGUVPyGL OXPw== 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 ak17si938656ejc.651.2022.01.11.12.48.11; Tue, 11 Jan 2022 12:48:12 -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 5E33F68AF0D; Tue, 11 Jan 2022 22:47:37 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail0.khirnov.net (unknown [176.97.15.12]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 89EF168A6E0 for ; Tue, 11 Jan 2022 22:47:27 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 12FDF24017C for ; Tue, 11 Jan 2022 21:47:27 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id ZFqcHSWO9KHy for ; Tue, 11 Jan 2022 21:47:26 +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 27CDC2404FE for ; Tue, 11 Jan 2022 21:47:25 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id F0DE73A0746; Tue, 11 Jan 2022 21:47:24 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 11 Jan 2022 21:45:40 +0100 Message-Id: <20220111204610.14262-5-anton@khirnov.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20220111204610.14262-1-anton@khirnov.net> References: <20220111204610.14262-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 05/35] lavu/fifo: add a new FIFO grow function 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: MrRjre8Tago/ Consistently use size_t for sizes. Unlike av_fifo_grow(), which addds to the currently used size, this function adds to the allocated size. No new function is provided for a generic realloc, since the current code only supports increasing the FIFO size. --- doc/APIchanges | 3 ++- libavutil/fifo.c | 61 +++++++++++++++++++++++++++--------------------- libavutil/fifo.h | 14 +++++++++++ 3 files changed, 50 insertions(+), 28 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index 9400c5147a..98eae55719 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -18,7 +18,8 @@ API changes, most recent first: Add av_fifo_alloc2(), which allows setting a FIFO element size. Operations on FIFOs created with this function on these elements rather than bytes. - Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(). + Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(), + av_fifo_grow2(). 2022-01-xx - xxxxxxxxxx - lavu fifo.h Access to all AVFifoBuffer members is deprecated. The struct will diff --git a/libavutil/fifo.c b/libavutil/fifo.c index 8cde2c20e1..28d6d1b2e6 100644 --- a/libavutil/fifo.c +++ b/libavutil/fifo.c @@ -150,41 +150,48 @@ int av_fifo_space(const AVFifoBuffer *f) return av_fifo_can_write(f); } -int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size) +int av_fifo_grow2(AVFifoBuffer *f, size_t inc) { FifoBuffer *fb = (FifoBuffer*)f; + uint8_t *tmp; - if (new_size > FIFO_SIZE_MAX) + if (inc > FIFO_SIZE_MAX - fb->nb_elems) return AVERROR(EINVAL); - if (fb->nb_elems < new_size) { - uint8_t *tmp; - - tmp = av_realloc_array(f->buffer, new_size, fb->elem_size); - if (!tmp) - return AVERROR(ENOMEM); - - // move the data from the beginning of the ring buffer - // to the newly allocated space - if (fb->offset_w <= fb->offset_r && !fb->is_empty) { - const size_t copy = FFMIN(new_size - fb->nb_elems, fb->offset_w); - memcpy(tmp + fb->nb_elems * fb->elem_size, tmp, copy * fb->elem_size); - if (copy < fb->offset_w) { - memmove(tmp, tmp + copy * fb->elem_size, - (fb->offset_w - copy) * fb->elem_size); - fb->offset_w -= copy; - } else - fb->offset_w = fb->nb_elems + copy; - } + tmp = av_realloc_array(f->buffer, fb->nb_elems + inc, fb->elem_size); + if (!tmp) + return AVERROR(ENOMEM); + + // move the data from the beginning of the ring buffer + // to the newly allocated space + if (fb->offset_w <= fb->offset_r && !fb->is_empty) { + const size_t copy = FFMIN(inc, fb->offset_w); + memcpy(tmp + fb->nb_elems * fb->elem_size, tmp, copy * fb->elem_size); + if (copy < fb->offset_w) { + memmove(tmp, tmp + copy * fb->elem_size, + (fb->offset_w - copy) * fb->elem_size); + fb->offset_w -= copy; + } else + fb->offset_w = fb->nb_elems + copy; + } + + f->buffer = tmp; + fb->nb_elems += inc; - f->buffer = tmp; #if FF_API_FIFO_PUBLIC - f->end = f->buffer + new_size; - f->rptr = f->buffer + fb->offset_r * fb->elem_size; - f->wptr = f->buffer + fb->offset_w * fb->elem_size; + f->end = f->buffer + fb->nb_elems * fb->elem_size; + f->rptr = f->buffer + fb->offset_r * fb->elem_size; + f->wptr = f->buffer + fb->offset_w * fb->elem_size; #endif - fb->nb_elems = new_size; - } + + return 0; +} + +int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size) +{ + FifoBuffer *fb = (FifoBuffer*)f; + if (fb->nb_elems < new_size) + return av_fifo_grow2(f, new_size - fb->nb_elems); return 0; } diff --git a/libavutil/fifo.h b/libavutil/fifo.h index 9e78082b3b..375d0d133b 100644 --- a/libavutil/fifo.h +++ b/libavutil/fifo.h @@ -129,6 +129,20 @@ size_t av_fifo_can_read(const AVFifoBuffer *f); */ size_t av_fifo_can_write(const AVFifoBuffer *f); +/** + * Enlarge an AVFifoBuffer. + * + * On success, the FIFO will be large enough to hold exactly + * inc + av_fifo_can_read() + av_fifo_can_write() + * elements. In case of failure, the old FIFO is kept unchanged. + * + * @param f AVFifoBuffer to resize + * @param inc number of elements to allocate for, in addition to the current + * allocated size + * @return a non-negative number on success, a negative error code on failure + */ +int av_fifo_grow2(AVFifoBuffer *f, size_t inc); + /** * Feed data at specific position from an AVFifoBuffer to a user-supplied callback. * Similar as av_fifo_gereric_read but without discarding data.