From patchwork Tue Jul 25 11:14:17 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Cox X-Patchwork-Id: 42974 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:2987:b0:130:ccc6:6c4b with SMTP id f7csp145274pzh; Tue, 25 Jul 2023 04:15:01 -0700 (PDT) X-Google-Smtp-Source: APBJJlErHIy9HzfXTH7CsSDAni3/ui+QCAuET4luQrJcQtZJmwJMSTFDbZGhEiVRRsovx5mI0olk X-Received: by 2002:a05:6512:1cd:b0:4fd:d4b4:faba with SMTP id f13-20020a05651201cd00b004fdd4b4fabamr7927427lfp.51.1690283700849; Tue, 25 Jul 2023 04:15:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690283700; cv=none; d=google.com; s=arc-20160816; b=CN1uT80dTffsrvTMCI4QQHSldgiHZKBAxcLfeBDRCbfC5Jv9KZt6wiiPEPvxPP3dMb u7zNHpbKoavfpxn9GFrLCkX0nBlf1flayVa5ndXSaRmNxlpypwW8BAbqE3iUJpTeCOU7 KR9Tj2Ooxo3MBp8L8mUzXmkoG7dfEFcPRWkngYHtJhxDKjWCcEwJRxNgH0WcAoETrozn mmEfx5aN6Iy7ZUebmIrNP7XnVpSYFvHYCS4re6trt5snUn5LJbrLfseifZDNQWKBLBy4 dqtMCvftnmS/A6HzggWQzMX7L8RLhJsJWktmN6W1/fLFFCXEd8JCIphtdvgvOdcyvH6r 3y6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc: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:dkim-signature:delivered-to; bh=wjP9F33bki8SGIhB7YV2mYNcR9nwlCUYekSk30PtoLo=; fh=RtkXQwrzbGVs06JUrNEEeGv1QjUQNDEuJeaoUdU5VnM=; b=zh3tOfjribYGnM5ARuF20+7TogplZ+W5ibiRIpIJg45Wz+uO4XZU7DyUm+dAJbJstc VvwNSAPyFo4gkAOOjlh4W2buy9G0wSRayZj7vXG9UPDcL19NokL230Vo7DyVW7EIV3TJ ElLr/a619AoBStoBKfRLGzu8b3EALfd25A/octba9yw2eR7YxseXzHYKOLfT7SgKUJRF Gg9TcazENBgCOUWNmV828CNcQOWPMSxXdHN+ctZZmmJF5guySAskzzTdDldw1nsubLz0 i1w0Dgi4W+pNlHaunsu6CM989PpaFiNUJY0hOGTDM1lnZjz59UWuDz6AI+8mmgqfCOJK H7+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@kynesim-co-uk.20221208.gappssmtp.com header.s=20221208 header.b=SH3LsbsS; 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 u9-20020aa7d989000000b0052257789cbfsi130350eds.235.2023.07.25.04.14.49; Tue, 25 Jul 2023 04:15:00 -0700 (PDT) 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; dkim=neutral (body hash did not verify) header.i=@kynesim-co-uk.20221208.gappssmtp.com header.s=20221208 header.b=SH3LsbsS; 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 7604A68C8E3; Tue, 25 Jul 2023 14:14:36 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f52.google.com (mail-wr1-f52.google.com [209.85.221.52]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5D56268C8D8 for ; Tue, 25 Jul 2023 14:14:30 +0300 (EEST) Received: by mail-wr1-f52.google.com with SMTP id ffacd0b85a97d-3143ccb0f75so5010548f8f.0 for ; Tue, 25 Jul 2023 04:14:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kynesim-co-uk.20221208.gappssmtp.com; s=20221208; t=1690283669; x=1690888469; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=TJQoQqjG5/airSEFUozDsTSHRptoqgOpdn/fcqIlnfg=; b=SH3LsbsSCtAyj9wIE023v7SddV5kHIbAHQwLOO32O3GyOw/9mrzSKG/fIyqz3Y2f/w IpIrt0QY0Akv/VNtHhA2EqKmF4UaKFYjePEwkzgtePSCYZibN4Mx2ovNxbTRvZBMVBOQ gnZndUvuYe13YvMXFALtbJzPHoUZy4D84fJQeJMao9ziRMM4HE0VvlfmAFHyChoP2ffV R7udhpkjbMrSgCkq4RO98Yx2mmQ7v9XK3xxmJh0EFcBENu6ITDebI64XTgLdeBDl0LYi VocxEFonbbN8YRQWoFLmzUXOhnVz5Uz5wXvK1s2BnNMaLtysIK1Ff4/rd4g6r1DxYPYK hS2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690283669; x=1690888469; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=TJQoQqjG5/airSEFUozDsTSHRptoqgOpdn/fcqIlnfg=; b=Oa4ZhTgzbi1GAIjIqcKVKB3tguzmSvIy4IcGQ69d4r+C5uqaJYf55QIDL1V5ze68l2 LEO6bybpYQ0K/Yt1I7LDBwxV4z79a5zN92B04dqjAV/Oedrn/SoWG5JtAdq6UoFj5ckx 1Oo0wxgiDJgEEZ1yo3YVDTE0SEbXPYlzaYiQN332ANECKxBsijruvuA44ZIrlTpxL3Y7 0dKCrUQFliUGsJNXtbfcHz2kJMT21K9giZBCKX97Jy+ZHpoN9gjzT7mqQFEbAxctl2Mh r6oEX+Oo0xxrurLnp3xl1QZkQ0eU7hyxv4o826Akowk4cwHu/Szc0u/w1GoW7TXxk2Sy W2dg== X-Gm-Message-State: ABy/qLaTMVajPxizz2CcgXC7PlbLjllwpdTpIVL0K25yp61+InsQ4w+J 7U4PAmJLv7feywTeouxRuVwSUcsrExY90L9BUyk= X-Received: by 2002:a5d:452c:0:b0:317:5182:7b55 with SMTP id j12-20020a5d452c000000b0031751827b55mr6556591wra.42.1690283669782; Tue, 25 Jul 2023 04:14:29 -0700 (PDT) Received: from sucnaath.outer.uphall.net (cpc1-cmbg20-2-0-cust759.5-4.cable.virginm.net. [86.21.218.248]) by smtp.gmail.com with ESMTPSA id a17-20020adffad1000000b003143cdc5949sm16120140wrs.9.2023.07.25.04.14.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 25 Jul 2023 04:14:29 -0700 (PDT) From: John Cox To: ffmpeg-devel@ffmpeg.org Date: Tue, 25 Jul 2023 11:14:17 +0000 Message-Id: <20230725111417.1171953-2-jc@kynesim.co.uk> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230725111417.1171953-1-jc@kynesim.co.uk> References: <20230725111417.1171953-1-jc@kynesim.co.uk> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4 1/1] avfilter/buffersink: Add video frame allocation callback 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 Cc: stefasab@gmail.com, John Cox Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: n9/653cZoofQ Add a callback to enable user allocation of video frames on the final stage of a filter chain. Signed-off-by: John Cox --- libavfilter/buffersink.c | 38 ++++++++++++++++++++++++++++++ libavfilter/buffersink.h | 51 ++++++++++++++++++++++++++++++++++++++++ libavfilter/version.h | 2 +- 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/libavfilter/buffersink.c b/libavfilter/buffersink.c index 306c283f77..93f78f6db8 100644 --- a/libavfilter/buffersink.c +++ b/libavfilter/buffersink.c @@ -62,6 +62,12 @@ typedef struct BufferSinkContext { int sample_rates_size; AVFrame *peeked_frame; + + union { + AVBuffersinkAllocVideoFrameFunc video; + AVBuffersinkAllocAudioFrameFunc audio; + } alloc_cb; + void *alloc_v; } BufferSinkContext; #define NB_ITEMS(list) (list ## _size / sizeof(*list)) @@ -154,6 +160,36 @@ int attribute_align_arg av_buffersink_get_samples(AVFilterContext *ctx, return get_frame_internal(ctx, frame, 0, nb_samples); } +static AVFrame *alloc_audio_buffer(AVFilterLink *link, int nb_samples) +{ + AVFilterContext *const ctx = link->dst; + BufferSinkContext *const buf = ctx->priv; + return buf->alloc_cb.audio ? buf->alloc_cb.audio(ctx, buf->alloc_v, nb_samples) : + ff_default_get_audio_buffer(link, nb_samples); +} + +void av_buffersink_set_alloc_audio_frame(AVFilterContext *ctx, AVBuffersinkAllocAudioFrameFunc cb, void *v) +{ + BufferSinkContext *const buf = ctx->priv; + buf->alloc_cb.audio = cb; + buf->alloc_v = v; +} + +static AVFrame *alloc_video_buffer(AVFilterLink *link, int w, int h) +{ + AVFilterContext *const ctx = link->dst; + BufferSinkContext *const buf = ctx->priv; + return buf->alloc_cb.video ? buf->alloc_cb.video(ctx, buf->alloc_v, w, h) : + ff_default_get_video_buffer(link, w, h); +} + +void av_buffersink_set_alloc_video_frame(AVFilterContext *ctx, AVBuffersinkAllocVideoFrameFunc cb, void *v) +{ + BufferSinkContext *const buf = ctx->priv; + buf->alloc_cb.video = cb; + buf->alloc_v = v; +} + static av_cold int common_init(AVFilterContext *ctx) { BufferSinkContext *buf = ctx->priv; @@ -381,6 +417,7 @@ static const AVFilterPad avfilter_vsink_buffer_inputs[] = { { .name = "default", .type = AVMEDIA_TYPE_VIDEO, + .get_buffer = {.video = alloc_video_buffer}, }, }; @@ -400,6 +437,7 @@ static const AVFilterPad avfilter_asink_abuffer_inputs[] = { { .name = "default", .type = AVMEDIA_TYPE_AUDIO, + .get_buffer = {.audio = alloc_audio_buffer}, }, }; diff --git a/libavfilter/buffersink.h b/libavfilter/buffersink.h index 64e08de53e..72ee755688 100644 --- a/libavfilter/buffersink.h +++ b/libavfilter/buffersink.h @@ -166,6 +166,57 @@ int av_buffersink_get_frame(AVFilterContext *ctx, AVFrame *frame); */ int av_buffersink_get_samples(AVFilterContext *ctx, AVFrame *frame, int nb_samples); +/** + * Callback from ::av_buffersink_set_alloc_audio_frame to + * allocate a frame + * + * @param ctx pointer to a context of the abuffersink AVFilter. + * @param v opaque pointer passed to + * ::av_buffersink_set_alloc_audio_frame + * @param nb_samples number of samples to allocate in the frame + * + * @retval AVFrame* The newly allocated frame + * @retval NULL Error allocating the frame + */ +typedef AVFrame *(*AVBuffersinkAllocAudioFrameFunc)(AVFilterContext *ctx, void *v, int nb_samples); + +/** + * Set an audio frame allocation method for abuffersink + * + * @param ctx pointer to a context of the abuffersink AVFilter. + * @param cb Callback to the allocation function. If set to NULL + * then the default avfilter allocation function will + * be used. + * @param v Opaque to pass to the allocation function + */ +void av_buffersink_set_alloc_audio_frame(AVFilterContext *ctx, AVBuffersinkAllocAudioFrameFunc cb, void *v); + +/** + * Callback from ::av_buffersink_set_alloc_video_frame to + * allocate a frame + * + * @param ctx pointer to a context of the buffersink AVFilter. + * @param v opaque pointer passed to + * ::av_buffersink_set_alloc_video_frame + * @param w width of frame to allocate + * @param h height of frame to allocate + * + * @retval AVFrame* The newly allocated frame + * @retval NULL Error allocating the frame + */ +typedef AVFrame *(*AVBuffersinkAllocVideoFrameFunc)(AVFilterContext *ctx, void *v, int w, int h); + +/** + * Set a video frame allocation method for buffersink + * + * @param ctx pointer to a context of the buffersink AVFilter. + * @param cb Callback to the allocation function. If set to NULL + * then the default avfilter allocation function will + * be used. + * @param v Opaque to pass to the allocation function + */ +void av_buffersink_set_alloc_video_frame(AVFilterContext *ctx, AVBuffersinkAllocVideoFrameFunc cb, void *v); + /** * @} */ diff --git a/libavfilter/version.h b/libavfilter/version.h index 4a69d6be98..8f4a7a9cd3 100644 --- a/libavfilter/version.h +++ b/libavfilter/version.h @@ -31,7 +31,7 @@ #include "version_major.h" -#define LIBAVFILTER_VERSION_MINOR 10 +#define LIBAVFILTER_VERSION_MINOR 11 #define LIBAVFILTER_VERSION_MICRO 100