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 7ECE74308C for ; Thu, 13 Jan 2022 17:53:37 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id EF83D68B52C; Thu, 13 Jan 2022 19:53:34 +0200 (EET) Received: from EUR04-VI1-obe.outbound.protection.outlook.com (mail-oln040092075020.outbound.protection.outlook.com [40.92.75.20]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 218CF68B17A for ; Thu, 13 Jan 2022 19:53:28 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=Mh39y4xITziKxB4FM/zSOOp1aoFeX07aY3LGXeqRDGSP7+kgF14qFl/HcZq6lChIPYJSMZqgqN2Cia3CAajmJN0QpU91uAtjxMblSUQBN9LZypu1+Tejr0mn6oug7zH9BprPKSxXDqnP2m8kvTh16MTuSnLVydDD4rcFoHuIV5PiA61Wl8r538qnT142DpUasIQNr7YMBmykZmECLjSjvTDiaHoYc/Nchfm1qMntV66XP0az17+6RgUpBHIltx5U1i6z8wFgOQBm+vJ1pN9oo28Y2RkEPJujUWC8qtCNc8w2CVAim13jpJSqFZd3TIiUQHqJk12k64s7tUDjQ8Jq4Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=BA1xLjZ8lFwE5/cGxBIDuxyXGqWTDi8TbTFjYlYSNEE=; b=ZT2VFAzPhh42JqwKGV69t7jy/33po/gV+giAubvM3pd8nJREGcwQN/VVPFRgc8GwT2QQu2nAAsXnL+WnEosGqVLcOLw9e2GOqxjrtJhzDu6UIUsjtjyfJxVWxdHhHD1KaTdb24rcYvpXoGWbYXfookmkMqHjuVNuhLfFbMiTmJwX4aj+s9/N4S8C4oWUSIOCbgfre6rw+dDP2fpnhdqclp2FRDYAI00Vg+O5JrS7icpaNxs1XMuM5C5HFMG90Q1RNScYUrgRswX1M+1H7oPS2QRtj/rsikeDD6GfGpk5AzHi26j/XflEQTIoMNl13/7AW+0Wt4O6/i5lmVPicr2+Sg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BA1xLjZ8lFwE5/cGxBIDuxyXGqWTDi8TbTFjYlYSNEE=; b=RXIvqOP8whS2S5CU6XDJRe/UM2Hv2AFx77X/CnTvDNV5pt1Uzm2JyEprYnp8B2jIE2o4Wp9AjfY4L47P+UdUgCIUbFnmKeX35R6GGjHbmzDTLdLB1Zm1LYqAoPEjgkxixNKgRrHSntBk7Xa/EBQPXkr45BFQo3kJx65l/4S9BNYxkuqM7eDQCdBS3aUtAivUIct2Ibmo+wTRgZxgVJHaU1BNCwyuudT7ZuEEYN+/Vsrz32RJTx8QMWGjnVLmC31BoKvY+87qyJV/UX8GRO4FZVUpr3JuSPyED9clPbkPZGWpvTPVmTwQZewhgtOWZV1CV0K/Qi2WRNf2Xi/Hp6j0ZQ== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by AM4PR0301MB2227.eurprd03.prod.outlook.com (2603:10a6:200:4e::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4867.9; Thu, 13 Jan 2022 17:53:26 +0000 Received: from AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::19fc:be9f:2c9c:53f5]) by AM7PR03MB6660.eurprd03.prod.outlook.com ([fe80::19fc:be9f:2c9c:53f5%9]) with mapi id 15.20.4888.010; Thu, 13 Jan 2022 17:53:26 +0000 Message-ID: Date: Thu, 13 Jan 2022 18:53:25 +0100 Content-Language: en-US To: ffmpeg-devel@ffmpeg.org References: <20220111204610.14262-1-anton@khirnov.net> <20220111204610.14262-9-anton@khirnov.net> From: Andreas Rheinhardt In-Reply-To: <20220111204610.14262-9-anton@khirnov.net> X-TMN: [oSnVU6ltu6ZPKqaAuTKNlPhSSkB37JEf] X-ClientProxiedBy: AS9P194CA0008.EURP194.PROD.OUTLOOK.COM (2603:10a6:20b:46d::27) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: <003f877e-4296-cdbd-a246-50421d556c3d@outlook.com> MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 8eface14-38b0-4788-9353-08d9d6bd997d X-MS-TrafficTypeDiagnostic: AM4PR0301MB2227:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: G6nYMYnDUloiOYmfbdSBhfEM7343y9dqgZApPuAgZNSrEJ1NoTDFkq0nb1mQlwoKZA7NqdMgJ3mbhHSUJrzquGrW/+XOL6G6+2qNPSxdN1Wlhl/eFrnrtZar3qwZiQ0nYDv3SndoT3Vv/qYzuewTP92aCsI4EBVLBVyne6LJharBpyidmqRt8U55FO3pkjvym8zvuarh8bYF+ryuwf10CH2D26D/XmPpckBoiqpnSYEWgMeK5sdsOqUdDlIxW2CY47h63KJVroZz9KQtLR/9DfXzN4526FdilEIR5uodak+KxXQweFON8QSqBZWAxZm5mHvWPIwdqjG1fc6nCNuEHRQVxLeLAQBibiS1wsQ33r0CStHbi+uxRPh67Rp358RHVhxFx5MmTsGk+ipA9HZU5ep2kxrf9cmIjMqaf97vokbG3WZpYQXQmiiOohP6NV3nCZpOp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YrVmhB?= =?utf-8?B?N1RIOEN4U1ZDdlZJdHBIckVISFVvVzVGTnNmM1BER3N1WTQrbUM1M1hweTJ1?= =?utf-8?B?MGkybjdYdURNSGVYb3lUem9UbzA1U01MVytZZVI2dlR5SGdxNElBSllneXF3?= =?utf-8?B?cnJ1c1pNWnU4ckdlSVJHNE5ONXVoSCtCTGVSTnR2SUNYRkFJR21YalZqVzR2?= =?utf-8?B?Z2pFaGZPMExPMU1MektwWGpBdzVLRk4veDRuUGZXZzBGeXZFRWNyWnhCbjEz?= =?utf-8?B?Y2JPK2dzUWpibUQ3ZElucGhRc2pHOWF1SUZWT0NlZHFmcXdZZ0tJNWFidkJw?= =?utf-8?B?M3ljMFRRMDBwZ3d6RnBDMkZUSHV5ZFVHcW5iWFBEZjN4Um9uKzNuU25wand6?= =?utf-8?B?c1AreU9tcWE0VkRCT09ZSi9tKzd0Mkg5YitLaUJ3ZVRibmVmeVRpdUZRNksx?= =?utf-8?B?QnRTZjhSZGUwNW55aGVUMnYyS3FZVWlmbXJmQUw5UE8vak1IcXdPWk8rTDc5?= =?utf-8?B?by9RaCtsc2hrSWRSS2RrUHBJYVFmbDBnUnVnd0hlaDRTMytXZUErU1I0dTBR?= =?utf-8?B?bjhESzU4aEE0RUltZzFZWG43MEN0WkJMWVFiTnZ5c05BRE5YQT09?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 8eface14-38b0-4788-9353-08d9d6bd997d X-MS-Exchange-CrossTenant-AuthSource: AM7PR03MB6660.eurprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 13 Jan 2022 17:53:26.4129 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM4PR0301MB2227 Subject: Re: [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 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: Anton Khirnov: > 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; This is still linear if one always writes a fixed amount of elements. Using av_fast_realloc_array() would actually fit very well in here. Shall I resend a size_t version? > + 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 > _______________________________________________ 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".