From: Niklas Haas <ffmpeg@haasn.xyz> To: ffmpeg-devel@ffmpeg.org Cc: Niklas Haas <git@haasn.dev> Subject: [FFmpeg-devel] [PATCH] avfilter/vf_libplacebo: composite multiple inputs in linear light Date: Tue, 22 Jul 2025 13:04:08 +0200 Message-ID: <20250722110408.88468-1-ffmpeg@haasn.xyz> (raw) From: Niklas Haas <git@haasn.dev> 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".
reply other threads:[~2025-07-22 11:04 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20250722110408.88468-1-ffmpeg@haasn.xyz \ --to=ffmpeg@haasn.xyz \ --cc=ffmpeg-devel@ffmpeg.org \ --cc=git@haasn.dev \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git