From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.ffmpeg.org (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTPS id 1C42E4B8FB for ; Tue, 22 Jul 2025 11:04:14 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id ECDC268CD94; Tue, 22 Jul 2025 14:04:11 +0300 (EEST) Received: from haasn.dev (haasn.dev [78.46.187.166]) by ffbox0-bg.ffmpeg.org (Postfix) with ESMTP id D582368CE02 for ; Tue, 22 Jul 2025 14:04:10 +0300 (EEST) Received: from haasn.dev (unknown [10.30.1.1]) by haasn.dev (Postfix) with UTF8SMTP id 7E079403F0; Tue, 22 Jul 2025 13:04:10 +0200 (CEST) From: Niklas Haas To: ffmpeg-devel@ffmpeg.org Date: Tue, 22 Jul 2025 13:04:08 +0200 Message-ID: <20250722110408.88468-1-ffmpeg@haasn.xyz> X-Mailer: git-send-email 2.50.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH] avfilter/vf_libplacebo: composite multiple inputs in linear light 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: Niklas Haas 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: From: Niklas Haas This gives vastly improved blending results than when blending directly in the desired output colorspace. Overridable by the existing "disable_linear" option. This is functionally similar to combining multiple "libplacebo" filters, but does not rely on the existence of a Vulkan filter link, so it can be used without performance penalty in all circumstances. It's also enabled by default, without requiring special action from the user. --- libavfilter/vf_libplacebo.c | 51 +++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/libavfilter/vf_libplacebo.c b/libavfilter/vf_libplacebo.c index 475030c80d..347151acd3 100644 --- a/libavfilter/vf_libplacebo.c +++ b/libavfilter/vf_libplacebo.c @@ -160,6 +160,10 @@ typedef struct LibplaceboContext { pl_gpu gpu; pl_tex tex[4]; + /* dedicated renderer for linear output composition */ + pl_renderer linear_rr; + pl_tex linear_tex; + /* input state */ LibplaceboInput *inputs; int nb_inputs; @@ -735,6 +739,7 @@ static int init_vulkan(AVFilterContext *avctx, const AVVulkanDeviceContext *hwct return AVERROR(ENOMEM); for (int i = 0; i < s->nb_inputs; i++) RET(input_init(avctx, &s->inputs[i], i)); + s->linear_rr = pl_renderer_create(s->log, s->gpu); /* fall through */ fail: @@ -760,6 +765,8 @@ static void libplacebo_uninit(AVFilterContext *avctx) #if PL_API_VER >= 351 pl_cache_destroy(&s->cache); #endif + pl_renderer_destroy(&s->linear_rr); + pl_tex_destroy(s->gpu, &s->linear_tex); pl_options_free(&s->opts); pl_vulkan_destroy(&s->vulkan); pl_log_destroy(&s->log); @@ -953,6 +960,20 @@ static int output_frame(AVFilterContext *ctx, int64_t pts) goto fail; } + struct pl_frame orig_target = target; + bool use_linear_compositor = false; + if (s->linear_tex && target.color.transfer != PL_COLOR_TRC_LINEAR && !s->disable_linear) { + use_linear_compositor = true; + target.color.transfer = PL_COLOR_TRC_LINEAR; + target.repr = pl_color_repr_rgb; + target.num_planes = 1; + target.planes[0] = (struct pl_plane) { + .components = 4, + .component_mapping = {0, 1, 2, 3}, + .texture = s->linear_tex, + }; + } + /* Draw first frame opaque, others with blending */ opts->params.blend_params = NULL; #if PL_API_VER >= 346 @@ -981,6 +1002,13 @@ static int output_frame(AVFilterContext *ctx, int64_t pts) #endif } + if (use_linear_compositor) { + /* Blit the linear intermediate image to the output frame */ + target.crop = orig_target.crop = (struct pl_rect2df) {0}; + pl_render_image(s->linear_rr, &target, &orig_target, &opts->params); + target = orig_target; + } + if (outdesc->flags & AV_PIX_FMT_FLAG_HWACCEL) { pl_unmap_avframe(s->gpu, &target); } else if (!pl_download_avframe(s->gpu, &target, out)) { @@ -1307,6 +1335,29 @@ static int libplacebo_config_output(AVFilterLink *outlink) s->force_divisible_by, s->reset_sar ? sar_in : 1.0); + + if (s->nb_inputs > 1 && !s->disable_fbos) { + /* Create a separate renderer and composition texture */ + pl_fmt fmt = pl_find_fmt(s->gpu, PL_FMT_FLOAT, 4, 16, 0, PL_FMT_CAP_BLENDABLE); + bool ok = !!fmt; + if (ok) { + ok = pl_tex_recreate(s->gpu, &s->linear_tex, pl_tex_params( + .format = fmt, + .w = outlink->w, + .h = outlink->h, + .renderable = true, + .sampleable = true, + .storable = fmt->caps & PL_FMT_CAP_STORABLE, + )); + } + + if (!ok) { + av_log(s, AV_LOG_WARNING, "Failed to create a linear texture for " + "compositing multiple inputs, falling back to non-linear " + "blending.\n"); + } + } + if (s->reset_sar) { /* SAR is normalized, or we have multiple inputs, set out to 1:1 */ outlink->sample_aspect_ratio = (AVRational){ 1, 1 }; -- 2.50.1 _______________________________________________ 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".