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 9780F43088 for ; Thu, 13 Jan 2022 17:41:25 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id DCFCD68B44B; Thu, 13 Jan 2022 19:41:22 +0200 (EET) Received: from EUR03-AM5-obe.outbound.protection.outlook.com (mail-oln040092070017.outbound.protection.outlook.com [40.92.70.17]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 7523B68B345 for ; Thu, 13 Jan 2022 19:41:16 +0200 (EET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=K8uWDiBiXOiXafjUry5FAAvv1OHkYm/JXex2BwNGnusYxjE9Tk8qTObYt+CEv6/x6CEtzGCH+1PrArjiX2mBVZQbbf+E0Gsj0J0JabRv5eLcrzOlgi2cCIuXW531Ts5+PL9z+J+LhiRYlZOemhRrtURRk9p74NEkPAmf1Yp39Pkrma9An3j38ulvsLGHxV6BXmbSVHR++7nSUTIgNVLy/lbmfb+HEUgqWxaKX8nGiUzT+9XhjY+61RIoD1GS0C+XSOD4ZbFkybIxKYhAAfvsGAFibyQt6e65pNvZkwvT9maxEXDJqynayTA/t8TKZT+Nvzx4SWHvDUvBYZzFCbaACg== 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=ECYJgOsmmeeo07iX5YvGfZmQhWeCNQxbHgxYcWnave4=; b=YJ6AqrQzhuKNkZP8ewOs1186qwhr/A2JsomJx9Qt3+Xav85Li3Re753vT6axtkpWbRkurouMvQ6KXjxQj7Mfq7vCPYIkvSlfEXp/hwSJVSWCMQU4n3NTGy/ZGfUfGIUyaNCepApnix/lSLb2ciwB/t4SXJMPq13zrH7Wrdt1Dhwhs4ShG1BZMnzIXhTxQl86+pdM62nlA8vc6aWKWcHzHkyj8JxQRtcFNjCJhOv4A5OhKaUPGj1GAW3kUnzi1MrZmPapWeGDn29W/EToe+Ui52YAdyfNpUF/hzba+S3Ap+W6tnXgAIDaZyT+V4GSWGxp/tJoc8v8eKCjBaYo8PzT4g== 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=ECYJgOsmmeeo07iX5YvGfZmQhWeCNQxbHgxYcWnave4=; b=qqyFIE3Nqp4pkgthOAy5B8VvWm5s84ixpwEcCCcgGFL/emnFEHSosNjxS2+ZQtIsYgxY58Z4DNCGr1VyzDI7hoHXWcdzgttKY5CUWgvuoHbgJ5vMf/FN2m4EmkVwaInb4tP0J0NbZFz67+7ZrMEd49hby/hEDBriQU23feobfz1qDREA+GwqFm/8BEmJ3S5LyWbVDflcJW4DO5OfnBULDskVBCVSSpgXVkxT/+3IY+SmGe6VrxOWXntoDImvF9qpdv91IPDjtu9g0/hRNPEKnO2jOfZI6IY/AmWmfi1yIIZOuiduOSrgzpf+kzRDJPehqSSRiWskKl4lkEwiuJ65VQ== Received: from AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) by AM9PR03MB7631.eurprd03.prod.outlook.com (2603:10a6:20b:412::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4888.11; Thu, 13 Jan 2022 17:41:15 +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:41:15 +0000 Message-ID: Date: Thu, 13 Jan 2022 18:41:12 +0100 Content-Language: en-US To: ffmpeg-devel@ffmpeg.org References: <20220111204610.14262-1-anton@khirnov.net> <20220111204610.14262-8-anton@khirnov.net> From: Andreas Rheinhardt In-Reply-To: <20220111204610.14262-8-anton@khirnov.net> X-TMN: [Aul1O8Y0qYabK0pmO6PNyMnZbhbe5AxN] X-ClientProxiedBy: AM0PR02CA0097.eurprd02.prod.outlook.com (2603:10a6:208:154::38) To AM7PR03MB6660.eurprd03.prod.outlook.com (2603:10a6:20b:1c1::22) X-Microsoft-Original-Message-ID: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 6349f5f1-0b59-4262-da67-08d9d6bbe575 X-MS-TrafficTypeDiagnostic: AM9PR03MB7631:EE_ X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: M6uDns0nX2rt387AFAstW1I8KnGIXA28RnZ23tjRjrT4rcMGtGvQaOCn6a3qREzKUqGMUqcTV5kQAGA+gCPzr1hsGMt5oHbXVqdvuJixH/4sZvF6fe9CCuwyVyEP5jheVygnOySQDVFL2ha7C3siz0yiSAdPEli9hbfEDfDe/DtYPH9zlljYjTewqTe3iP2xYBcnfQk7CKFshFeBk8baQ0iKbpxuFTDdBrG4Y/iyK8SaEUxAho5rtwuCVUquupoeyHoFtJEzAsg+6Jli2NZg0fxQWJLmQVJx94g+M1pa3dDQOkbUIe8eLfbtTNRU43UoO0TIAvif6fk7R18elMr4K7E2R/hmdKsgrogSziXTds6cnTCFhUFLvgz5MMibWI6Ifp7ODSVcgV2jXPvDNUrleqZhi/E09gtJpOmWvdNr6dQRUFAcbcFWr5GCGGOdsn8MCsYuX0aehODMat11cZ49nNDSFXjpbkw8x8cLOMBPCbvCc3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o0NDF5WVI0Ym5pZVpralExVkE3cm1Od01Vb21FeHVp?= =?utf-8?B?Q2RnNnFhMFY2aXA0dVA4S1BmeC8rZGM2dW1wSjNBR1ZKaURSUjFFZ1Z4K1g3?= =?utf-8?B?eHM1MXp1ZlFGeEl4bFJodStMR2pBV0tkVWlnWFlNV3NIL1FwWS9wMkEzbWZO?= =?utf-8?B?RVJGUEcrQjJtaytKa1c0OEhZMVFYYTc0U0owT3RvV04wL0haVldOYkhaTWFV?= =?utf-8?B?Zmd3Q0IzY2ppMUdURXJnVVpYSHV4RjRkSHNSU25yTXNPaXNFaDY3Y295YzhI?= =?utf-8?B?b1ZUY1hhMkJKZFpIMFNjaVIyakhlbGZtTitNc09DOG5xMzFxbk9EQUdKRzJB?= =?utf-8?B?a3RDOVZNaUFjRy9UY3BWVzVQTVc4bnp6QkNTWHRUQXlQOGk3akpGVUNGcXo3?= =?utf-8?B?Y25SNHlHUFNwVWxLQVlKZFphZVl2S0N2cVZVb3BlbFpsaE9pY2pRNE1RcFRa?= =?utf-8?B?c0d1QUdnNm5YWEc0SWZsd1JaTE1UdDdHNWRuYjlqT0tyZDcveFo5b1U4dnN1?= =?utf-8?B?TldIbW1tZzhlYjB0a3k3NnFJU1RSYWl5TFNmZGtWSkEvUGV4dz09?= X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 6349f5f1-0b59-4262-da67-08d9d6bbe575 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:41:14.9841 (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: AM9PR03MB7631 Subject: Re: [FFmpeg-devel] [PATCH 08/35] lavu/fifo: add new FIFO read/peek functions 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: > As for writing, use separate functions for reading to a buffer and a > callback. Allow the callbacks to limit the amount of data read, > similarly to what is done for writing. > > Consistently use size_t for sizes. > --- > doc/APIchanges | 3 ++- > libavutil/fifo.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++ > libavutil/fifo.h | 60 ++++++++++++++++++++++++++++++++++++++++++ > 3 files changed, 130 insertions(+), 1 deletion(-) > > diff --git a/doc/APIchanges b/doc/APIchanges > index 0b179c30e5..f64759d69d 100644 > --- a/doc/APIchanges > +++ b/doc/APIchanges > @@ -19,7 +19,8 @@ API changes, most recent first: > 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(), > - av_fifo_grow2(), av_fifo_drain2(), av_fifo_write(), av_fifo_write_from_cb(). > + 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(). > > 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 1d94fff457..ea944bc936 100644 > --- a/libavutil/fifo.c > +++ b/libavutil/fifo.c > @@ -265,6 +265,74 @@ int av_fifo_write_from_cb(AVFifoBuffer *f, AVFifoCB read_cb, > return fifo_write_common(f, NULL, nb_elems, read_cb, opaque); > } > > +static int fifo_peek_common(const AVFifoBuffer *f, uint8_t *buf, size_t *nb_elems, > + size_t offset, AVFifoCB write_cb, void *opaque) > +{ > + const FifoBuffer *fb = (FifoBuffer*)f; > + size_t to_read = *nb_elems; > + size_t offset_r = fb->offset_r; > + int ret = 0; > + > + if (offset > av_fifo_can_read(f) || > + to_read > av_fifo_can_read(f) - offset) { You are calling av_fifo_can_read() multiple times. > + *nb_elems = 0; > + return AVERROR(EINVAL); > + } > + > + if (offset_r >= fb->nb_elems - offset) > + offset_r -= fb->nb_elems - offset; > + else > + offset_r += offset; > + > + do { Shouldn't this be a while loop? > + size_t len = FFMIN(fb->nb_elems - offset_r, to_read); > + uint8_t *rptr = f->buffer + offset_r * fb->elem_size; > + > + if (write_cb) { > + ret = write_cb(opaque, rptr, &len); > + if (ret < 0 || len == 0) > + break; > + } else { > + memcpy(buf, rptr, len * fb->elem_size); > + buf += len * fb->elem_size; > + } > + offset_r += len; > + if (offset_r >= fb->nb_elems) > + offset_r = 0; > + to_read -= len; > + } while (to_read > 0); > + > + *nb_elems -= to_read; > + > + return ret; > +} > + > +int av_fifo_read(AVFifoBuffer *f, void *buf, size_t nb_elems) > +{ > + int ret = fifo_peek_common(f, buf, &nb_elems, 0, NULL, NULL); > + av_fifo_drain2(f, nb_elems); In contrast to the current av_fifo_generic_read() the callback will still see the non-drained FIFO if the callback is called multiple times. I don't know whether this slight behaviour change can cause problems when updating. > + return ret; > +} > + > +int av_fifo_read_to_cb(AVFifoBuffer *f, AVFifoCB write_cb, > + void *opaque, size_t *nb_elems) > +{ > + int ret = fifo_peek_common(f, NULL, nb_elems, 0, write_cb, opaque); > + av_fifo_drain2(f, *nb_elems); > + return ret; > +} > + > +int av_fifo_peek(AVFifoBuffer *f, void *buf, size_t nb_elems, size_t offset) > +{ > + return fifo_peek_common(f, buf, &nb_elems, offset, NULL, NULL); > +} > + > +int av_fifo_peek_to_cb(AVFifoBuffer *f, AVFifoCB write_cb, void *opaque, > + size_t *nb_elems, size_t offset) > +{ > + return fifo_peek_common(f, NULL, nb_elems, offset, write_cb, opaque); > +} > + > int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size) > { > FifoBuffer *fb = (FifoBuffer*)f; > diff --git a/libavutil/fifo.h b/libavutil/fifo.h > index ac1245ff39..c7be5e8f7d 100644 > --- a/libavutil/fifo.h > +++ b/libavutil/fifo.h > @@ -189,6 +189,66 @@ int av_fifo_write(AVFifoBuffer *f, const void *buf, size_t nb_elems); > int av_fifo_write_from_cb(AVFifoBuffer *f, AVFifoCB read_cb, > void *opaque, size_t *nb_elems); > > +/** > + * Read data from a FIFO. > + * > + * @param f the FIFO buffer > + * @param buf Buffer to store the data. nb_elems * av_fifo_elem_size(f) bytes > + * will be written into buf. > + * @param nb_elems number of elements to read from FIFO > + * > + * @return a non-negative number on success, a negative error code on failure > + */ > +int av_fifo_read(AVFifoBuffer *f, void *buf, size_t nb_elems); > + > +/** > + * Feed data from a FIFO into a user-provided callback. > + * > + * @param f the FIFO buffer > + * @param write_cb Callback the data will be supplied to. May be called > + * multiple times. > + * @param opaque opaque user data to be provided to write_cb > + * @param nb_elems Should point to the maximum number of elements that can be > + * read. Will be updated to contain the total number of elements > + * actually sent to the callback. > + * > + * @return non-negative number on success, a negative error code on failure > + */ > +int av_fifo_read_to_cb(AVFifoBuffer *f, AVFifoCB write_cb, > + void *opaque, size_t *nb_elems); > + > +/** > + * Read data from a FIFO without modifying FIFO state. > + * > + * @param f the FIFO buffer > + * @param buf Buffer to store the data. nb_elems * av_fifo_elem_size(f) bytes > + * will be written into buf. > + * @param nb_elems number of elements to read from FIFO > + * @param offset number of initial elements to skip; offset + nb_elems must not > + * be larger than av_fifo_can_read(f). > + * > + * @return a non-negative number on success, a negative error code on failure > + */ > +int av_fifo_peek(AVFifoBuffer *f, void *buf, size_t nb_elems, size_t offset); > + > +/** > + * Feed data from a FIFO into a user-provided callback. > + * > + * @param f the FIFO buffer > + * @param write_cb Callback the data will be supplied to. May be called > + * multiple times. > + * @param opaque opaque user data to be provided to write_cb > + * @param nb_elems Should point to the maximum number of elements that can be > + * read. Will be updated to contain the total number of elements > + * actually sent to the callback. > + * @param offset number of initial elements to skip; offset + *nb_elems must not > + * be larger than av_fifo_can_read(f). > + * > + * @return a non-negative number on success, a negative error code on failure > + */ > +int av_fifo_peek_to_cb(AVFifoBuffer *f, AVFifoCB write_cb, void *opaque, Ok, seems like we differ on our naming: I'd call this a read_cb, because it reads from the fifo, but it of course also writes, so it seems fine given that you are consistent. > + size_t *nb_elems, size_t offset); > + > /** > * Discard the specified amount of data from an AVFifoBuffer. > * @param size number of elements to discard, MUST NOT be larger than > _______________________________________________ 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".