PR #26272 opened by igouss URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26272 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26272.patch
# Summary of changes Fixes #26271. An overlay placed partly past the right or bottom edge of the video, as for a logo sliding in or a picture-in-picture near a corner, makes `overlay_vaapi` fail on Intel with `Failed to render parameter buffer: 18 (invalid parameter)`. The filter logs that it "will crop the overlay image" and then passes the rectangle on unclipped; intel-media-driver rejects it. This is stacked on #26118 because it changes the same lines: the first two commits here are that PR, only the last one is new. I will rebase once #26118 is in. The new commit clips the destination rectangle to the output and shortens the source rectangle to match. An overlay that is entirely outside, or has no size, is not drawn. With the script from #26271 (overlay at `x=288:y=208` on a 320x240 video), Intel UHD 630, intel-media-driver 26.3.5: Before: `overlay_vaapi exit=251`. After: `overlay_vaapi exit=0 Y at 300,220: 200`, the same as the software `overlay` filter. <details><summary>Details and limits</summary> - Right and bottom: clipped in the filter. An unscaled overlay is cut exactly (byte-identical to the same overlay drawn fully visible on a larger canvas). A scaled one is cut to the nearest overlay pixel, so the cut edge can be up to half an overlay pixel off, and at least one pixel is always read. - Left and top: intel-media-driver clips there itself, so the rectangle is only clipped in the filter where that gives the same output (the cut falls on an overlay pixel, as it always does unscaled, and the visible region starts on a chroma sample) or where the position does not fit a `VARectangle`. Otherwise it is passed on as before. - Nothing that rendered before changes: in 69,862 placements on the UHD 630 (inside, touching, crossing and beyond every edge; unscaled and scaled; NV12 and BGRA; cropped with even and odd origins), every one that rendered without this commit gives byte-identical output with it. The exception is positions or sizes that do not fit a `VARectangle` (x/y outside 16 bits, w/h above 65535): those were truncated before and drawn in the wrong place or at the wrong size, and are now drawn as requested. - Mesa radeonsi already clips a rectangle past the right or bottom edge itself. For a scaled overlay the filter's cut can differ from Mesa's own by about one pixel at that edge. I did not run off-screen placements on Mesa with this patch: with the unpatched filter, a negative position or an overlay fully outside aborted inside `radeonsi_drv_video.so` on my R9 280X, and I did not want to repeat that. - With intel-media-driver 26.3.5, a layer read from a number of rows that is 1 modulo 4 gets its first row shown in place of its last. That is a driver matter: an overlay of such a height (for example 40x37) placed inside the frame shows it on master, and with #26118 a cropped one does too. An overlay clipped at the bottom can now land on such a height; in my runs about a quarter of bottom-clipped placements did. - The 16 cases I use for #26118 give byte-identical output with and without this commit on both drivers. </details> 🤖 Generated with [Claude Code](https://claude.com/claude-code) From db49e46321a04bb9fc03b47bcee63ff32605a609 Mon Sep 17 00:00:00 2001 From: Iouri Goussev <[email protected]> Date: Mon, 16 Nov 2026 19:04:13 -0500 Subject: [PATCH 1/3] avfilter/vf_overlay_vaapi: use the visible region of the overlay frame The overlay layer was submitted with surface_region set to NULL, which makes the driver read the whole surface. A decoded frame's surface is usually taller than the picture: 1920x1080 H.264 is decoded into a 1920x1088 surface. The padding rows were therefore scaled into the overlay rectangle and the picture was squeezed by 1080/1088, so a line at row 540 of the overlay was drawn at row 536. Cropping set on the overlay frame was ignored as well. Pass the frame's visible region instead, as is already done for the main input. Co-Authored-By: Claude Fable 5.2 <[email protected]> Signed-off-by: Iouri Goussev <[email protected]> --- libavfilter/vf_overlay_vaapi.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/libavfilter/vf_overlay_vaapi.c b/libavfilter/vf_overlay_vaapi.c index a771b60ff36..3107d92c7a2 100644 --- a/libavfilter/vf_overlay_vaapi.c +++ b/libavfilter/vf_overlay_vaapi.c @@ -167,7 +167,7 @@ static int overlay_vaapi_blend(FFFrameSync *fs) AVFrame *output; VAProcPipelineParameterBuffer params[2]; VABlendState blend_state = { 0 }; /**< Blend State */ - VARectangle overlay_region, output_region; + VARectangle overlay_region, overlay_input_region, output_region; int err; err = ff_framesync_get_frame(fs, 0, &input_main, 0); @@ -234,8 +234,17 @@ static int overlay_vaapi_blend(FFFrameSync *fs) blend_state.global_alpha = ctx->blend_alpha; params[1].blend_state = &blend_state; + overlay_input_region = (VARectangle) { + .x = input_overlay->crop_left, + .y = input_overlay->crop_top, + .width = input_overlay->width - + (input_overlay->crop_left + input_overlay->crop_right), + .height = input_overlay->height - + (input_overlay->crop_top + input_overlay->crop_bottom), + }; + params[1].surface = (VASurfaceID)(uintptr_t)input_overlay->data[3]; - params[1].surface_region = NULL; + params[1].surface_region = &overlay_input_region; params[1].output_region = &overlay_region; } -- 2.54.1 From e8763aa7d951c49cbb7eba6c01505b97bd3f913a Mon Sep 17 00:00:00 2001 From: Iouri Goussev <[email protected]> Date: Mon, 16 Nov 2026 20:01:48 -0500 Subject: [PATCH 2/3] avfilter/vf_overlay_vaapi: describe the overlay with its own colour properties The parameters of the overlay layer were a copy of those of the main input, so the driver was told that the overlay has the colour properties of the main frame. An RGB overlay on a limited-range BT.709 video was therefore blended as limited-range RGB: white was written as Y=255 instead of 235 and black as Y=0 instead of 16. A YUV overlay whose frame properties differ from those of the main input was not converted either. Initialise the parameters of the overlay from its own frame, as vf_stack_vaapi does for each input. Properties that the overlay frame does not specify are still taken from the main input, which is what the driver was told before, so only properties that the overlay does specify change what is sent. Matrix and range are taken over only when both frames are RGB or both are YUV. Fixes: #26119 Co-Authored-By: Claude Fable 5.2 <[email protected]> Signed-off-by: Iouri Goussev <[email protected]> --- libavfilter/vf_overlay_vaapi.c | 47 +++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/libavfilter/vf_overlay_vaapi.c b/libavfilter/vf_overlay_vaapi.c index 3107d92c7a2..c5390977c09 100644 --- a/libavfilter/vf_overlay_vaapi.c +++ b/libavfilter/vf_overlay_vaapi.c @@ -157,6 +157,17 @@ static int overlay_vaapi_build_filter_params(AVFilterContext *avctx) return 0; } +static int overlay_vaapi_frame_is_rgb(const AVFrame *frame) +{ + const AVHWFramesContext *hwfc; + const AVPixFmtDescriptor *desc; + + hwfc = (const AVHWFramesContext *)frame->hw_frames_ctx->data; + desc = av_pix_fmt_desc_get(hwfc->sw_format); + + return desc && (desc->flags & AV_PIX_FMT_FLAG_RGB); +} + static int overlay_vaapi_blend(FFFrameSync *fs) { AVFilterContext *avctx = fs->parent; @@ -164,10 +175,11 @@ static int overlay_vaapi_blend(FFFrameSync *fs) OverlayVAAPIContext *ctx = avctx->priv; VAAPIVPPContext *vpp_ctx = avctx->priv; AVFrame *input_main, *input_overlay; - AVFrame *output; + AVFrame *output, *overlay = NULL; VAProcPipelineParameterBuffer params[2]; VABlendState blend_state = { 0 }; /**< Blend State */ - VARectangle overlay_region, overlay_input_region, output_region; + VARectangle overlay_region, overlay_input_region; + VARectangle main_input_region, output_region; int err; err = ff_framesync_get_frame(fs, 0, &input_main, 0); @@ -199,6 +211,11 @@ static int overlay_vaapi_blend(FFFrameSync *fs) if (err < 0) goto fail; + /* The region set above lives in the context and is overwritten when + * the parameters of the overlay are initialised below. */ + main_input_region = vpp_ctx->input_region; + params[0].surface_region = &main_input_region; + output_region = (VARectangle) { .x = 0, .y = 0, @@ -228,7 +245,29 @@ static int overlay_vaapi_blend(FFFrameSync *fs) "will crop the overlay image according based on the main image.\n"); } - memcpy(¶ms[1], ¶ms[0], sizeof(params[0])); + /* The overlay is described by its own colour properties. Those it + * does not specify are assumed to be those of the main input; matrix + * and range only if both are RGB or both are YUV. */ + overlay = av_frame_clone(input_overlay); + if (!overlay) { + err = AVERROR(ENOMEM); + goto fail; + } + if (overlay->color_primaries == AVCOL_PRI_UNSPECIFIED) + overlay->color_primaries = input_main->color_primaries; + if (overlay->color_trc == AVCOL_TRC_UNSPECIFIED) + overlay->color_trc = input_main->color_trc; + if (overlay_vaapi_frame_is_rgb(overlay) == + overlay_vaapi_frame_is_rgb(input_main)) { + if (overlay->colorspace == AVCOL_SPC_UNSPECIFIED) + overlay->colorspace = input_main->colorspace; + if (overlay->color_range == AVCOL_RANGE_UNSPECIFIED) + overlay->color_range = input_main->color_range; + } + + err = ff_vaapi_vpp_init_params(avctx, ¶ms[1], overlay, output); + if (err < 0) + goto fail; blend_state.flags = ctx->blend_flags; blend_state.global_alpha = ctx->blend_alpha; @@ -249,6 +288,7 @@ static int overlay_vaapi_blend(FFFrameSync *fs) } err = ff_vaapi_vpp_render_pictures(avctx, params, input_overlay ? 2 : 1, output); + av_frame_free(&overlay); if (err < 0) goto fail; @@ -259,6 +299,7 @@ static int overlay_vaapi_blend(FFFrameSync *fs) return ff_filter_frame(outlink, output); fail: + av_frame_free(&overlay); av_frame_free(&output); return err; } -- 2.54.1 From 85ffe6448ed61976b0d8e1d59187f1960e311f17 Mon Sep 17 00:00:00 2001 From: Iouri Goussev <[email protected]> Date: Mon, 16 Nov 2026 21:32:04 -0500 Subject: [PATCH 3/3] avfilter/vf_overlay_vaapi: clip the overlay to the output The rectangle the overlay is drawn to was passed to the driver as given. VA-API describes it as a region within the output surface, and intel-media-driver rejects one that extends beyond the right or bottom edge, so an overlay placed partly beyond those edges failed with "Failed to render parameter buffer: 18 (invalid parameter)". The filter only logged that the overlay would be cropped. Clip the rectangle at the right and bottom edge of the output and shorten the rectangle the overlay is read from to match. The rectangles are integers, so an unscaled overlay is clipped exactly and a scaled one to the nearest of its pixels, and at least one pixel is read. At the left and top edge the rectangle is only clipped where intel-media-driver, which clips there itself, gives the same output: when the cut falls on a pixel of the overlay, as it always does for an unscaled one, and the visible region of the overlay starts on a chroma sample. It is also clipped when the position does not fit a VARectangle. Otherwise it is passed on as before. An overlay that lies outside the output, or whose size is not positive, is not drawn. With intel-media-driver 26.3.5, a layer read from a number of rows that is 1 modulo 4 can get its first row copied into its last output row. That is not new, a cropped overlay of such a height shows it as well, but an overlay clipped at the bottom can now have such a height. Co-Authored-By: Claude Fable 5.2 <[email protected]> Signed-off-by: Iouri Goussev <[email protected]> --- libavfilter/vf_overlay_vaapi.c | 110 ++++++++++++++++++++++++++++----- 1 file changed, 93 insertions(+), 17 deletions(-) diff --git a/libavfilter/vf_overlay_vaapi.c b/libavfilter/vf_overlay_vaapi.c index c5390977c09..efb56456bf4 100644 --- a/libavfilter/vf_overlay_vaapi.c +++ b/libavfilter/vf_overlay_vaapi.c @@ -168,6 +168,67 @@ static int overlay_vaapi_frame_is_rgb(const AVFrame *frame) return desc && (desc->flags & AV_PIX_FMT_FLAG_RGB); } +/** + * Fit the rectangle the overlay is drawn to into the output, and shorten + * the rectangle it is read from to match. + * + * The right and bottom edge are always clipped, to the nearest pixel of the + * overlay. The left and top edge are only clipped where the driver is + * known to give the same result, or where the position does not fit a + * VARectangle; otherwise the position is passed on as it is. + * + * @return 0 if no part of the overlay is visible, 1 otherwise + */ +static int overlay_vaapi_clip(VARectangle *src, VARectangle *dst, + int64_t x, int64_t y, int64_t w, int64_t h, + int out_w, int out_h, int sub_w, int sub_h) +{ + int64_t x1 = FFMIN(x + w, out_w), y1 = FFMIN(y + h, out_h); + int64_t sx0 = 0, sx1 = src->width, sy0 = 0, sy1 = src->height; + + if (w <= 0 || h <= 0 || x >= out_w || y >= out_h || + x + w <= 0 || y + h <= 0) + return 0; + + /* Clip at the left and top edge when the position does not fit a + * VARectangle, or when the cut falls on a pixel of the overlay and the + * visible region of the overlay starts on a chroma sample. */ + if (x < INT16_MIN || + (x < 0 && !(-x * src->width % w) && !(src->x & ((1 << sub_w) - 1)))) { + sx0 = FFMIN(-x * src->width / w, src->width - 1); + x = 0; + } + if (y < INT16_MIN || + (y < 0 && !(-y * src->height % h) && !(src->y & ((1 << sub_h) - 1)))) { + sy0 = FFMIN(-y * src->height / h, src->height - 1); + y = 0; + } + + /* What is left of the overlay does not reach its right or bottom edge + * if it was clipped there. */ + if (x1 != x + w) + sx1 = sx0 + av_clip64(((x1 - x) * src->width + w / 2) / w, + 1, src->width - sx0); + if (y1 != y + h) + sy1 = sy0 + av_clip64(((y1 - y) * src->height + h / 2) / h, + 1, src->height - sy0); + + *src = (VARectangle) { + .x = src->x + sx0, + .y = src->y + sy0, + .width = sx1 - sx0, + .height = sy1 - sy0, + }; + *dst = (VARectangle) { + .x = x, + .y = y, + .width = x1 - x, + .height = y1 - y, + }; + + return 1; +} + static int overlay_vaapi_blend(FFFrameSync *fs) { AVFilterContext *avctx = fs->parent; @@ -180,6 +241,7 @@ static int overlay_vaapi_blend(FFFrameSync *fs) VABlendState blend_state = { 0 }; /**< Blend State */ VARectangle overlay_region, overlay_input_region; VARectangle main_input_region, output_region; + int draw_overlay = 0; int err; err = ff_framesync_get_frame(fs, 0, &input_main, 0); @@ -227,24 +289,46 @@ static int overlay_vaapi_blend(FFFrameSync *fs) params[0].output_background_color = VAAPI_VPP_BACKGROUND_BLACK; if (input_overlay) { + const AVHWFramesContext *hwfc = + (const AVHWFramesContext *)input_overlay->hw_frames_ctx->data; + const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(hwfc->sw_format); + int64_t ow = ctx->ow ? ctx->ow : input_overlay->width; + int64_t oh = ctx->oh ? ctx->oh : input_overlay->height; + av_log(avctx, AV_LOG_DEBUG, "Filter overlay: %s, %ux%u (%"PRId64").\n", av_get_pix_fmt_name(input_overlay->format), input_overlay->width, input_overlay->height, input_overlay->pts); - overlay_region = (VARectangle) { - .x = ctx->ox, - .y = ctx->oy, - .width = ctx->ow ? ctx->ow : input_overlay->width, - .height = ctx->oh ? ctx->oh : input_overlay->height, + overlay_input_region = (VARectangle) { + .x = input_overlay->crop_left, + .y = input_overlay->crop_top, + .width = input_overlay->width - + (input_overlay->crop_left + input_overlay->crop_right), + .height = input_overlay->height - + (input_overlay->crop_top + input_overlay->crop_bottom), }; - if (overlay_region.x + overlay_region.width > input_main->width || - overlay_region.y + overlay_region.height > input_main->height) { + if (ctx->ox + ow > output->width || ctx->oy + oh > output->height) { av_log(ctx, AV_LOG_WARNING, "The overlay image exceeds the scope of the main image, " "will crop the overlay image according based on the main image.\n"); } + if (ow <= 0 || oh <= 0) + av_log(ctx, AV_LOG_WARNING, + "The overlay has no size and is not drawn.\n"); + + /* Drivers are not required to clip. One rejects a rectangle that + * extends beyond the right or bottom edge of the output. */ + draw_overlay = overlay_vaapi_clip(&overlay_input_region, + &overlay_region, + ctx->ox, ctx->oy, ow, oh, + output->width, output->height, + desc ? desc->log2_chroma_w : 0, + desc ? desc->log2_chroma_h : 0); + } + + if (draw_overlay) { /* The overlay is described by its own colour properties. Those it * does not specify are assumed to be those of the main input; matrix * and range only if both are RGB or both are YUV. */ @@ -273,21 +357,13 @@ static int overlay_vaapi_blend(FFFrameSync *fs) blend_state.global_alpha = ctx->blend_alpha; params[1].blend_state = &blend_state; - overlay_input_region = (VARectangle) { - .x = input_overlay->crop_left, - .y = input_overlay->crop_top, - .width = input_overlay->width - - (input_overlay->crop_left + input_overlay->crop_right), - .height = input_overlay->height - - (input_overlay->crop_top + input_overlay->crop_bottom), - }; - params[1].surface = (VASurfaceID)(uintptr_t)input_overlay->data[3]; params[1].surface_region = &overlay_input_region; params[1].output_region = &overlay_region; } - err = ff_vaapi_vpp_render_pictures(avctx, params, input_overlay ? 2 : 1, output); + err = ff_vaapi_vpp_render_pictures(avctx, params, draw_overlay ? 2 : 1, + output); av_frame_free(&overlay); if (err < 0) goto fail; -- 2.54.1 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
