From patchwork Tue Jan 11 20:45:44 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 33256 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a6b:cd86:0:0:0:0:0 with SMTP id d128csp4049677iog; Tue, 11 Jan 2022 12:51:08 -0800 (PST) X-Google-Smtp-Source: ABdhPJwKfdREFnlWjCU3crSIJryuflhzhMxSfhctiwGeuRzG594EPJaF+PcItO+j6Q/hajlnI0Ju X-Received: by 2002:a17:907:961d:: with SMTP id gb29mr4927044ejc.123.1641934268534; Tue, 11 Jan 2022 12:51:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1641934268; cv=none; d=google.com; s=arc-20160816; b=zJHC5Y8zNGs/jYE9pJccoVFE64Z4D3nNFySTxa2ewyf9kVKd1MBsypDmjZJUbCetva 4b+BY7GrGLlCVaK0QxDibH5UmI9kDuCkn7nMA8xRxMQwiX8KjC/jA50WrUpHAwozl3Td V36YLerDRdQAouJmGQUbil+Hz7bolykycV4ed/k7x4zUZU0XQxBcmyDYCXjZRqUETcV+ dxAslnuFJhIAZGnCVkdQgaTNVBP0fiorozV8/co9v003+gQ9COT0eCdryjRH2k3ZJiqp mG3JS1qoOsyUYRoNnool4mQVdGvnVt/7ADDmFbv2meKTI4bgVYViBW1fN3zgC/Sw975a 18fg== 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=prd9VjHlsPIMcMDYp8aWJeUOY88pW0TCJspRFHg/Mk8=; b=Bhtf2PI0FJXoFHWISPD9FWvUCVILXiHZJtDDrTHt3hoMhqdkii9bv+ar1plhJZ5lwA axXJAcMQPJfPXuXr+wpEJT5XH6WEubHyhTAfTqzyeN9thlorMU1vjE0kVK+TixrqdqJ8 4VG/RzJaJGaS68SIrQe4qHHKRYjESglDMwG+Ot9tTd/sdWPicboZpVJg+I8s1mCwzKaj x2YMtOa/9Wt/IB7NlYLSr9MdimSnabRHh4ZMrWwuoBJntBsVDyiu99D5XsNGUb5yUnGz eRjSkWLlEt1itoYfwI4dm0eCzUxZpTTLGuC2tAFKPqrp+0359N8peutKeXokYHK+cnP6 mTdQ== 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 m9si3996771edc.554.2022.01.11.12.51.08; Tue, 11 Jan 2022 12:51:08 -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 7624768AEE0; Tue, 11 Jan 2022 22:47:54 +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 9E99068AF2E for ; Tue, 11 Jan 2022 22:47:32 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 9CAA4240506 for ; Tue, 11 Jan 2022 21:47:29 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id ojY09lZnUMso for ; Tue, 11 Jan 2022 21:47:28 +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 86A5B240512 for ; Tue, 11 Jan 2022 21:47:25 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id 0E2063A09CE; Tue, 11 Jan 2022 21:47:25 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 11 Jan 2022 21:45:44 +0100 Message-Id: <20220111204610.14262-9-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 09/35] lavu/fifo: add a flag for automatically growing the FIFO as needed 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: 48+q5uxKtEhs This will not increase the FIFO beyond 1MB, unless the caller explicitly specifies otherwise. --- doc/APIchanges | 3 ++- libavutil/fifo.c | 41 +++++++++++++++++++++++++++++++++++++++-- libavutil/fifo.h | 15 ++++++++++++++- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/doc/APIchanges b/doc/APIchanges index f64759d69d..52b42762ea 100644 --- a/doc/APIchanges +++ b/doc/APIchanges @@ -20,7 +20,8 @@ API changes, most recent first: rather than bytes. Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(), av_fifo_grow2(), av_fifo_drain2(), av_fifo_write(), av_fifo_write_from_cb(), - av_fifo_read(), av_fifo_read_to_cb(), av_fifo_peek(), av_fifo_peek_to_cb(). + av_fifo_read(), av_fifo_read_to_cb(), av_fifo_peek(), av_fifo_peek_to_cb(), + av_fifo_auto_grow_limit(). 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 ea944bc936..2c15df5d5c 100644 --- a/libavutil/fifo.c +++ b/libavutil/fifo.c @@ -34,6 +34,9 @@ # define CTX_STRUCT_NAME AVFifoBuffer #endif +// by default the FIFO can be auto-grown to 1MB +#define AUTO_GROW_DEFAULT_BYTES (1024 * 1024) + typedef struct CTX_STRUCT_NAME { // These fields must match then contents of AVFifoBuffer in fifo.h // until FF_API_FIFO_PUBLIC is removed @@ -48,6 +51,9 @@ typedef struct CTX_STRUCT_NAME { size_t offset_r, offset_w; // distinguishes the ambigous situation offset_r == offset_w int is_empty; + + unsigned int flags; + size_t auto_grow_limit; } FifoBuffer; AVFifoBuffer *av_fifo_alloc2(size_t nb_elems, size_t elem_size, @@ -75,10 +81,19 @@ AVFifoBuffer *av_fifo_alloc2(size_t nb_elems, size_t elem_size, f->nb_elems = nb_elems; f->elem_size = elem_size; + f->flags = flags; + f->auto_grow_limit = FFMAX(AUTO_GROW_DEFAULT_BYTES / elem_size, 1); + av_fifo_reset((AVFifoBuffer*)f); return (AVFifoBuffer*)f; } +void av_fifo_auto_grow_limit(AVFifoBuffer *f, size_t max_elems) +{ + FifoBuffer *fb = (FifoBuffer*)f; + fb->auto_grow_limit = max_elems; +} + AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size) { if (nmemb > SIZE_MAX / size) @@ -207,6 +222,27 @@ void av_fifo_drain2(AVFifoBuffer *f, size_t size) #endif } +static int fifo_check_space(AVFifoBuffer *f, size_t to_write) +{ + FifoBuffer *fb = (FifoBuffer*)f; + const size_t can_write = av_fifo_can_write(f); + const size_t need_grow = to_write > can_write ? to_write - can_write : 0; + size_t can_grow; + + if (!need_grow) + return 0; + + can_grow = fb->auto_grow_limit > fb->nb_elems ? + fb->auto_grow_limit - fb->nb_elems : 0; + if ((fb->flags & AV_FIFO_FLAG_AUTO_GROW) && need_grow <= can_grow) { + // allocate a bit more than necessary, if we can + const size_t inc = (need_grow < can_grow / 2 ) ? need_grow * 2 : can_grow; + return av_fifo_grow2(f, inc); + } + + return AVERROR(ENOSPC); +} + static int fifo_write_common(AVFifoBuffer *f, const uint8_t *buf, size_t *nb_elems, AVFifoCB read_cb, void *opaque) { @@ -218,8 +254,9 @@ static int fifo_write_common(AVFifoBuffer *f, const uint8_t *buf, size_t *nb_ele uint32_t wndx= f->wndx; #endif - if (to_write > av_fifo_can_write(f)) - return AVERROR(ENOSPC); + ret = fifo_check_space(f, to_write); + if (ret < 0) + return ret; do { size_t len = FFMIN(fb->nb_elems - offset_w, to_write); diff --git a/libavutil/fifo.h b/libavutil/fifo.h index c7be5e8f7d..11eb36944a 100644 --- a/libavutil/fifo.h +++ b/libavutil/fifo.h @@ -83,6 +83,13 @@ AVFifoBuffer *av_fifo_alloc(unsigned int size); */ AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size); +/** + * Automatically resize the FIFO on writes, so that the data fits. This + * automatic resizing happens up to a limit that can be modified with + * av_fifo_auto_grow_limit(). + */ +#define AV_FIFO_FLAG_AUTO_GROW (1 << 0) + /** * Allocate and initialize an AVFifoBuffer with a given element size. * @@ -90,7 +97,7 @@ AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size); * @param nb_elems initial number of elements that can be stored in the FIFO * @param elem_size Size in bytes of a single element. Further operations on * the returned FIFO will implicitly use this element size. - * @param flags currently unused, must be 0 + * @param flags a combination of AV_FIFO_FLAG_* * * @return newly-allocated AVFifoBuffer on success, a negative error code on failure */ @@ -115,6 +122,12 @@ void av_fifo_freep(AVFifoBuffer **f); */ size_t av_fifo_elem_size(const AVFifoBuffer *f); +/** + * Set the maximum size (in elements) to which the FIFO can be resized + * automatically. Has no effect unless AV_FIFO_FLAG_AUTO_GROW is used. + */ +void av_fifo_auto_grow_limit(AVFifoBuffer *f, size_t max_elems); + /** * Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied. * @param f AVFifoBuffer to reset