From 899e497122f793c3d97f5aac7bee62567f23fe29 Mon Sep 17 00:00:00 2001 From: Niklas Haas Date: Thu, 11 Sep 2025 01:48:32 +0200 Subject: [PATCH] avfilter/vf_libplacebo: force premultiplied blending for linear texture Blending onto independent alpha framebuffers is not possible under the constraints of the supported blend operators. While we could handle blending premul-onto-premul, this would break if the base layer is YUV, since premultiplied alpha does not survive the (nonlinear) YUV conversion. Fortunately, blending independent-onto-premul is just as easy, and works in all cases. So just force this mode when using a linear intermediate blend texture, which is always RGBA. --- libavfilter/vf_libplacebo.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libavfilter/vf_libplacebo.c b/libavfilter/vf_libplacebo.c index 8f30eb193b..80d3b273bb 100644 --- a/libavfilter/vf_libplacebo.c +++ b/libavfilter/vf_libplacebo.c @@ -1033,6 +1033,7 @@ props_done: .color = orig_target.color, .rotation = orig_target.rotation, }; + target.repr.alpha = PL_ALPHA_PREMULTIPLIED; target.color.transfer = PL_COLOR_TRC_LINEAR; use_linear_compositor = true; } @@ -1052,7 +1053,8 @@ props_done: update_crops(ctx, in, &target, target_pts); pl_render_image_mix(in->renderer, &in->mix, &target, &tmp_params); - /* Force straight output and set correct blend mode */ + /* Force straight output and set correct blend operator. This is + * required to get correct blending onto YUV target buffers. */ target.repr.alpha = PL_ALPHA_INDEPENDENT; tmp_params.blend_params = &pl_alpha_overlay; #if PL_API_VER >= 346 @@ -1065,6 +1067,7 @@ props_done: if (use_linear_compositor) { /* Blit the linear intermediate image to the output frame */ target.crop = orig_target.crop = (struct pl_rect2df) {0}; + target.repr.alpha = PL_ALPHA_PREMULTIPLIED; pl_render_image(s->linear_rr, &target, &orig_target, &opts->params); target = orig_target; } else if (!ref) {