From patchwork Tue Feb 8 18:26:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 34188 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp1044898iov; Tue, 8 Feb 2022 10:26:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJzuK3LAk5ba/Q0JwGORarYhc+jhI7JQRmizThZLAAqiqtuLXLCgFkIbbLSBYQS2UCJqbAAx X-Received: by 2002:a05:6402:27d0:: with SMTP id c16mr1699257ede.276.1644344789648; Tue, 08 Feb 2022 10:26:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644344789; cv=none; d=google.com; s=arc-20160816; b=XYBOT56U3T6pOndEUZ+EnqcxmCRm52hDcyZNzKpZMAQFt52ylb5mh/EAZEkiFNke56 BoWTqsQkGvQGkyFxspwDflAzmctZKRz15oPSzv0FclLklmwJQJd+2qnWpmhGuHrW0ata 8m/6/Z3DQy6rYU4QV1zu2xpxj1xZZZaK5UApVGZic5XCxlq5YgOSJsJ5TKI7vq/ZX4ew MN4lrHy/hGzhmcOLwmxVvv0Wv83pNi1K+zhASbZuwJwqdLWXeViUVxKmOqSN86wA6+zj nU5euTl4+voRH1o0IeLzbvlY9zxsdbIbWHG89dC8d8XFCZx1Qe/KBrd7OlqJ5ual0Svw odtA== 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:message-id:date:to:from :delivered-to; bh=GhaQX+zkiCS5PedLz89eblJgcj6q0sb/5qX7zSboFRs=; b=X8PuKpPRswAHzAbmd2nDsgWB1am8OJ3vvdurezX7xHdqPgZvu5lfIADZ825LrMLq0I 46wFO0lt5E2kv9P9xKYG09wlm6AC9omBoH2NsgwSHRIHj4uHLlFz59TTOfdli4SFrYNc oan55NnY7lMi7R5GCsebDzzzm82476sjp2euJ0mYPRBGy6a/Vt61LxCPlT2TotS9vY2i V6aScJPQrmf3KEKNChfrWvNwBTo1N9ezmtq/S3iuKeOp3p+Ffm+Z8JiyPdbkbTq4JCgd MNI/llK847wcmsdTaAVNGeceBjUscZLnI1eZU/i7dO1t3e2aIa3TPjTXaBJPyOWmHDKg JxOg== 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 hp3si10871350ejc.847.2022.02.08.10.26.28; Tue, 08 Feb 2022 10:26:29 -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 9573768B120; Tue, 8 Feb 2022 20:26:25 +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 CB92868AEAA for ; Tue, 8 Feb 2022 20:26:18 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 424C624017C for ; Tue, 8 Feb 2022 19:26:18 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id xlyGBwEE72i9 for ; Tue, 8 Feb 2022 19:26:16 +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 E780E240175 for ; Tue, 8 Feb 2022 19:26:16 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id DCDFA3A06CD; Tue, 8 Feb 2022 19:26:16 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Feb 2022 19:26:13 +0100 Message-Id: <20220208182614.25504-1-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] lavu/fifo: fix a corner case in av_fifo_grow2() 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: 1ojvZe7OsaBG When the fifo is grown by exactly the current write offset, it would end up with offset_w = nb_elems. If av_fifo_write_from_cb() is called in such a state, the user callback would get callled with *nb_elems=0, which will then cause the write to return without writing anything. --- libavutil/fifo.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libavutil/fifo.c b/libavutil/fifo.c index 0af0154945..2af8842cc5 100644 --- a/libavutil/fifo.c +++ b/libavutil/fifo.c @@ -115,7 +115,7 @@ int av_fifo_grow2(AVFifo *f, size_t inc) (f->offset_w - copy) * f->elem_size); f->offset_w -= copy; } else - f->offset_w = f->nb_elems + copy; + f->offset_w = copy == inc ? 0 : f->nb_elems + copy; } f->nb_elems += inc; From patchwork Tue Feb 8 18:26:14 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Anton Khirnov X-Patchwork-Id: 34189 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6602:2c4e:0:0:0:0 with SMTP id x14csp1044978iov; Tue, 8 Feb 2022 10:26:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJztWcLjwDYwXeS+MQLCzerOhPWnwlqToDB4RLAINYKeiALqXQkCEVfefapcKpIcqdV79doJ X-Received: by 2002:a05:6402:2792:: with SMTP id b18mr5955889ede.344.1644344797394; Tue, 08 Feb 2022 10:26:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644344797; cv=none; d=google.com; s=arc-20160816; b=bjbdXnZk65/fez/XnCO4uI32xrdO1PCBq1fS1PO0F9+y7wuq+PwXAEMHLiCwFgFlSI /9xP24eO/c45ok8RDILML+GKNiYZBp83nHThYOP7xiWWUmfdtsw6uIrljp7/H/Byyigc Zkkl+vuE1PiVdvcl4wMWBZ6emfTcXBAYg05S+AXLtvb8OV76Wkmh7JyRH9Na/8w4pC2w wDyvtd1jG7ybBS57VKNaxseMnqhTYrtQHTFHCGkBFFLeTlU8519nUFjgg7fY4RMR8iCQ WSS5FP+Fjx46f9Ik9f4aN93fwcMuUfJN1p37JnJZmAaEbgVQ0LnWnV19Lw/Ttx/98bv+ yEWw== 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=p+6GMKCyD41q1gIuX0z8kPO+8fBu+A+GA7aX3CPRZI8=; b=Le6LTgRAyHI/MudgFmhuAMnEqsiPHit2XPJnWaB2n/pVhX1r1kW0evR/jGTae+1gna eKwMWu72FRo4FtB2WhDMTrDz/YSX7c9TnH0FNfVExuxhdfyatxROQMxlFFJu+wdJ/GYZ Mmcut3zlbsnOsf+KHkzdaNEv0lH1edwX25ecXqYKzqYCFywtdW6IVwLYp09E15lI+POZ m0PZoxiYRy4iirDIwAmkLXcRD2xxdC5UCTH79Nhg6gMBypHbbC2R+KuEp4ANpWF+7SIV BNY9qy6J9oKIFPDGVnmjLbG1JCmMqJuwPLszZtggZhAE9bPRH6ioRjargbplEvcNFL8u V1ow== 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 t7si10533162edd.599.2022.02.08.10.26.37; Tue, 08 Feb 2022 10:26:37 -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 A4E5168B161; Tue, 8 Feb 2022 20:26:26 +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 E713E68B056 for ; Tue, 8 Feb 2022 20:26:18 +0200 (EET) Received: from localhost (localhost [IPv6:::1]) by mail0.khirnov.net (Postfix) with ESMTP id 8018D240175 for ; Tue, 8 Feb 2022 19:26:18 +0100 (CET) Received: from mail0.khirnov.net ([IPv6:::1]) by localhost (mail0.khirnov.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id z6nmDRyAMM3p for ; Tue, 8 Feb 2022 19:26:17 +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 E7E8B240179 for ; Tue, 8 Feb 2022 19:26:17 +0100 (CET) Received: by libav.khirnov.net (Postfix, from userid 1000) id DE5213A017B; Tue, 8 Feb 2022 19:26:16 +0100 (CET) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 8 Feb 2022 19:26:14 +0100 Message-Id: <20220208182614.25504-2-anton@khirnov.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220208182614.25504-1-anton@khirnov.net> References: <20220208182614.25504-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] lavu/fifo: add a test for _cb 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: uTqDDaAIuad1 Makes an auto-growing FIFO and performs a sequence of randomly-sized writes/peeks/reads. --- libavutil/tests/fifo.c | 101 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 101 insertions(+) diff --git a/libavutil/tests/fifo.c b/libavutil/tests/fifo.c index 579602ccf3..bfcdfeebfb 100644 --- a/libavutil/tests/fifo.c +++ b/libavutil/tests/fifo.c @@ -18,7 +18,53 @@ #include #include +#include "libavutil/common.h" #include "libavutil/fifo.h" +#include "libavutil/lfg.h" +#include "libavutil/random_seed.h" + +typedef struct CBState { + unsigned int read_idx; + unsigned int write_idx; + unsigned int to_process; + unsigned int offset; +} CBState; + +static int read_cb(void *opaque, void *buf, size_t *nb_elems) +{ + CBState *s = opaque; + unsigned *b = buf; + + *nb_elems = FFMIN(*nb_elems, s->to_process); + + for (unsigned i = 0; i < *nb_elems; i++) + if (b[i] != s->read_idx + s->offset + i) { + printf("Mismatch at idx %u offset %u i %u\n", + s->read_idx, s->offset, i); + return AVERROR_BUG; + } + + s->offset += *nb_elems; + s->to_process -= *nb_elems; + + return 0; +} + +static int write_cb(void *opaque, void *buf, size_t *nb_elems) +{ + CBState *s = opaque; + unsigned *b = buf; + + *nb_elems = FFMIN(*nb_elems, s->to_process); + + for (unsigned i = 0; i < *nb_elems; i++) + b[i] = s->write_idx + i; + + s->write_idx += *nb_elems; + s->to_process -= *nb_elems; + + return 0; +} int main(void) { @@ -89,6 +135,61 @@ int main(void) printf("%d: %d\n", i, j); } + av_fifo_freep2(&fifo); + + /* test randomly-sized write/read/peek with a callback */ + { + CBState s = { 0 }; + uint32_t seed = av_get_random_seed(); + + AVLFG lfg; + int ret; + + av_lfg_init(&lfg, seed); + + fifo = av_fifo_alloc2(1, sizeof(unsigned), AV_FIFO_FLAG_AUTO_GROW); + + for (i = 0; i < 32; i++) { + size_t nb_elems = 16; + unsigned to_process = av_lfg_get(&lfg) % nb_elems; + + s.to_process = to_process; + + ret = av_fifo_write_from_cb(fifo, write_cb, &s, &nb_elems); + if (ret < 0 || s.to_process || nb_elems != to_process) { + printf("FIFO write fail; seed %"PRIu32"\n", seed); + return 1; + } + + nb_elems = av_fifo_can_read(fifo); + if (nb_elems > 1) { + s.offset = av_lfg_get(&lfg) % (nb_elems - 1); + nb_elems -= s.offset; + + s.to_process = av_lfg_get(&lfg) % nb_elems; + to_process = s.to_process; + + ret = av_fifo_peek_to_cb(fifo, read_cb, &s, &nb_elems, s.offset); + if (ret < 0 || s.to_process || nb_elems != to_process) { + printf("FIFO peek fail; seed %"PRIu32"\n", seed); + return 1; + } + } + + nb_elems = av_fifo_can_read(fifo); + to_process = nb_elems ? av_lfg_get(&lfg) % nb_elems : 0; + s.to_process = to_process; + s.offset = 0; + + ret = av_fifo_read_to_cb(fifo, read_cb, &s, &nb_elems); + if (ret < 0 || s.to_process || to_process != nb_elems) { + printf("FIFO peek fail; seed %"PRIu32"\n", seed); + return 1; + } + s.read_idx += s.offset; + } + } + av_fifo_freep2(&fifo); free(p);