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 331CE4BD58 for ; Tue, 16 Jul 2024 18:14:30 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8B65568DAED; Tue, 16 Jul 2024 21:14:09 +0300 (EEST) Received: from mail1.khirnov.net (quelana.khirnov.net [94.230.150.81]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 14AC968D8F6 for ; Tue, 16 Jul 2024 21:13:59 +0300 (EEST) Authentication-Results: mail1.khirnov.net; dkim=pass (2048-bit key; unprotected) header.d=khirnov.net header.i=@khirnov.net header.a=rsa-sha256 header.s=mail header.b=OK1xmX62; dkim-atps=neutral Received: from localhost (mail1.khirnov.net [IPv6:::1]) by mail1.khirnov.net (Postfix) with ESMTP id ACF784DEC for ; Tue, 16 Jul 2024 19:16:35 +0200 (CEST) Received: from mail1.khirnov.net ([IPv6:::1]) by localhost (mail1.khirnov.net [IPv6:::1]) (amavis, port 10024) with ESMTP id A2hRHqdFsV0F for ; Tue, 16 Jul 2024 19:16:35 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=khirnov.net; s=mail; t=1721150189; bh=bOrP+TnsUk1rlKp+l63UsMyC4gUCi36NyDOQiZ9p7ro=; h=From:To:Subject:Date:In-Reply-To:References:From; b=OK1xmX62OUofuSHCVvoxi3xkt/t+gzPJwWU+pxFqfMpzLb7X077IRJKm0ePVp2aLC tONHhKhCLcBzgT0abn3V0r2DcjLSCEaYaxT5TTo0HV+BOUT8G2Ck7/8jto58lGCKfL AG+JZTdWiNP+8EM6fO78B9RuwnJG1XlArd9eu6KMnzFTG57ZFLxkJCV1oqr5gw5Zoc gnl6je+F77JsUzp2oqhZmSwv2Cqo8xPHtST4pd2Nr5tWanV0V9oWES3LE5kb1hCZI4 jlQSqJotIgs/4+B2KxD9seYbHBFZ5S/IsbuAMxJnOvDlTIyijBtZqnWSluT1mkZpEp GrUnpPnuehNsA== 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 mail1.khirnov.net (Postfix) with ESMTPS id C7EB74E0C for ; Tue, 16 Jul 2024 19:16:28 +0200 (CEST) Received: from libav.khirnov.net (libav.khirnov.net [IPv6:::1]) by libav.khirnov.net (Postfix) with ESMTP id D69193A33D8 for ; Tue, 16 Jul 2024 19:16:21 +0200 (CEST) From: Anton Khirnov To: ffmpeg-devel@ffmpeg.org Date: Tue, 16 Jul 2024 19:11:51 +0200 Message-ID: <20240716171155.31838-36-anton@khirnov.net> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240716171155.31838-1-anton@khirnov.net> References: <20240716171155.31838-1-anton@khirnov.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 36/39] lavc: add private container FIFO API 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: It provides a FIFO for "container" objects like AVFrame/AVPacket and features an integrated FFRefStructPool-based pool to avoid allocating an freeing them repeatedly. --- libavcodec/container_fifo.c | 195 ++++++++++++++++++++++++++++++++++++ libavcodec/container_fifo.h | 87 ++++++++++++++++ 2 files changed, 282 insertions(+) create mode 100644 libavcodec/container_fifo.c create mode 100644 libavcodec/container_fifo.h diff --git a/libavcodec/container_fifo.c b/libavcodec/container_fifo.c new file mode 100644 index 0000000000..86ed15627b --- /dev/null +++ b/libavcodec/container_fifo.c @@ -0,0 +1,195 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include "libavutil/error.h" +#include "libavutil/fifo.h" +#include "libavutil/frame.h" +#include "libavutil/mem.h" + +#include "container_fifo.h" +#include "refstruct.h" + +struct ContainerFifo { + AVFifo *fifo; + FFRefStructPool *pool; + + void* (*container_alloc)(void); + void (*container_reset)(void *obj); + void (*container_free) (void *obj); + int (*fifo_write) (void *dst, void *src); + int (*fifo_read) (void *dst, void *src); + +}; + +static int container_fifo_init_entry(FFRefStructOpaque opaque, void *obj) +{ + ContainerFifo *cf = opaque.nc; + void **pobj = obj; + + *pobj = cf->container_alloc(); + if (!*pobj) + return AVERROR(ENOMEM); + + return 0; +} + +static void container_fifo_reset_entry(FFRefStructOpaque opaque, void *obj) +{ + ContainerFifo *cf = opaque.nc; + cf->container_reset(*(void**)obj); +} + +static void container_fifo_free_entry(FFRefStructOpaque opaque, void *obj) +{ + ContainerFifo *cf = opaque.nc; + cf->container_free(*(void**)obj); +} + +ContainerFifo* +ff_container_fifo_alloc(void* (*container_alloc)(void), + void (*container_reset)(void *obj), + void (*container_free) (void *obj), + int (*fifo_write) (void *dst, void *src), + int (*fifo_read) (void *dst, void *src)) +{ + ContainerFifo *cf; + + cf = av_mallocz(sizeof(*cf)); + if (!cf) + return NULL; + + cf->container_alloc = container_alloc; + cf->container_reset = container_reset; + cf->container_free = container_free; + cf->fifo_write = fifo_write; + cf->fifo_read = fifo_read; + + cf->fifo = av_fifo_alloc2(1, sizeof(void*), AV_FIFO_FLAG_AUTO_GROW); + if (!cf->fifo) + goto fail; + + cf->pool = ff_refstruct_pool_alloc_ext(sizeof(void*), 0, cf, + container_fifo_init_entry, + container_fifo_reset_entry, + container_fifo_free_entry, + NULL); + if (!cf->pool) + goto fail; + + return cf; +fail: + ff_container_fifo_free(&cf); + return NULL; +} + +void ff_container_fifo_free(ContainerFifo **pcf) +{ + ContainerFifo *cf; + + if (!*pcf) + return; + + cf = *pcf; + + if (cf->fifo) { + void *obj; + while (av_fifo_read(cf->fifo, &obj, 1) >= 0) + ff_refstruct_unref(&obj); + av_fifo_freep2(&cf->fifo); + } + + ff_refstruct_pool_uninit(&cf->pool); + + av_freep(pcf); +} + +int ff_container_fifo_read(ContainerFifo *cf, void *obj) +{ + void **psrc; + int ret; + + ret = av_fifo_read(cf->fifo, &psrc, 1); + if (ret < 0) + return ret; + + ret = cf->fifo_read(obj, *psrc); + ff_refstruct_unref(&psrc); + + return ret; +} + +int ff_container_fifo_write(ContainerFifo *cf, void *obj) +{ + void **pdst; + int ret; + + pdst = ff_refstruct_pool_get(cf->pool); + if (!pdst) + return AVERROR(ENOMEM); + + ret = cf->fifo_write(*pdst, obj); + if (ret < 0) + goto fail; + + ret = av_fifo_write(cf->fifo, &pdst, 1); + if (ret < 0) + goto fail; + + return 0; +fail: + ff_refstruct_unref(&pdst); + return ret; +} + +size_t ff_container_fifo_can_read(ContainerFifo *cf) +{ + return av_fifo_can_read(cf->fifo); +} + +static void* frame_alloc(void) +{ + return av_frame_alloc(); +} + +static void frame_reset(void *obj) +{ + av_frame_unref(obj); +} + +static void frame_free(void *obj) +{ + AVFrame *frame = obj; + av_frame_free(&frame); +} + +static int frame_ref(void *dst, void *src) +{ + return av_frame_ref(dst, src); +} + +static int frame_move_ref(void *dst, void *src) +{ + av_frame_move_ref(dst, src); + return 0; +} + +ContainerFifo *ff_container_fifo_alloc_avframe(unsigned flags) +{ + return ff_container_fifo_alloc(frame_alloc, frame_reset, frame_free, + frame_ref, frame_move_ref); +} diff --git a/libavcodec/container_fifo.h b/libavcodec/container_fifo.h new file mode 100644 index 0000000000..75a8f9e86b --- /dev/null +++ b/libavcodec/container_fifo.h @@ -0,0 +1,87 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVCODEC_CONTAINER_FIFO_H +#define AVCODEC_CONTAINER_FIFO_H + +/** + * ContainerFifo is a FIFO for "containers" - dynamically allocated reusable + * structs (e.g. AVFrame or AVPacket). ContainerFifo uses an internal pool of + * such containers to avoid allocating and freeing them repeatedly. + */ +typedef struct ContainerFifo ContainerFifo; + +/** + * Allocate a new ContainerFifo for the container type defined by provided + * callbacks. + * + * @param container_alloc allocate a new container instance and return a pointer + * to it, or NULL on failure + * @param container_reset reset the provided container instance to a clean state + * @param container_free free the provided container instance + * @param fifo_write transfer the contents of src to dst, where src is a + * container instance provided to ff_container_fifo_write() + * @param fifo_read transfer the contents of src to dst in other cases + * + * @note fifo_read() and fifo_write() are different parameters in order to allow + * fifo_write() implementations that make a new reference in dst, leaving + * src untouched (see e.g. ff_container_fifo_alloc_avframe()) + */ +ContainerFifo* +ff_container_fifo_alloc(void* (*container_alloc)(void), + void (*container_reset)(void *obj), + void (*container_free) (void *obj), + int (*fifo_write) (void *dst, void *src), + int (*fifo_read) (void *dst, void *src)); + +/** + * Allocate a ContainerFifo instance for AVFrames. + * Note that ff_container_fifo_write() will call av_frame_ref() on src, making a + * new reference in dst and leaving src untouched. + * + * @param flags unused currently + */ +ContainerFifo *ff_container_fifo_alloc_avframe(unsigned flags); + +/** + * Free a ContainerFifo and everything in it. + */ +void ff_container_fifo_free(ContainerFifo **pf); + +/** + * Write the contents of obj to the FIFO. + * + * The fifo_write() callback previously provided to ff_container_fifo_alloc() + * will be called with obj as src in order to perform the actual transfer. + */ +int ff_container_fifo_write(ContainerFifo *pf, void *obj); + +/** + * Read the next available object from the FIFO into obj. + * + * The fifo_read() callback previously provided to ff_container_fifo_alloc() + * will be called with obj as dst in order to perform the actual transfer. + */ +int ff_container_fifo_read(ContainerFifo *pf, void *obj); + +/** + * @return number of objects available for reading + */ +size_t ff_container_fifo_can_read(ContainerFifo *pf); + +#endif // AVCODEC_CONTAINER_FIFO_H -- 2.43.0 _______________________________________________ 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".