PR #26118 opened by igouss
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26118
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26118.patch

# Summary of changes

`overlay_vaapi` squeezes a hardware-decoded overlay vertically whenever its 
height is not a multiple of 16. I hit this while testing the filter's colour 
handling: a 1080p picture-in-picture came out 4 pixels short at mid-height.

The overlay layer is submitted with `surface_region = NULL`, so the driver 
reads the whole surface. A 1920x1080 H.264 stream is decoded into a 1920x1088 
surface, and the 8 padding rows are scaled into the overlay rectangle together 
with the picture. Cropping set on the overlay frame is ignored for the same 
reason.

This patch passes the frame's visible region instead, the way the main input 
already does.

Before/after frames: https://claude.ai/artifact/KdqpEnN3KaUS1CUQxoBS4B

## Reproduce

```sh
FF=${FF:-ffmpeg}; DEV=${DEV:-/dev/dri/renderD128}; ENC=${ENC:-ffmpeg}
python3 -c "import 
sys;sys.stdout.buffer.write(b'\x10'*(1920*540)+b'\xeb'*(1920*540))" > edge.gray
$ENC -hide_banner -loglevel error -nostdin -y -f rawvideo -pix_fmt gray -s 
1920x1080 -i edge.gray -pix_fmt yuv420p -c:v libx264 -qp 8 edge.mp4
python3 -c "import sys;sys.stdout.buffer.write(b'\x80'*(1920*1080*3//2))" > 
main.nv12
VA="-init_hw_device vaapi=va:$DEV -filter_hw_device va"; HW="-hwaccel vaapi 
-hwaccel_device va -hwaccel_output_format vaapi"
$FF -hide_banner -loglevel error -nostdin -y $VA -f rawvideo -pix_fmt nv12 -s 
1920x1080 -i main.nv12 $HW -i edge.mp4 -filter_complex 
"[0]hwupload[m];[m][1]overlay_vaapi,hwdownload,format=nv12" -frames:v 1 -f 
rawvideo ov.nv12
python3 -c "d=open('ov.nv12','rb').read();print('first bright row:',next(r for 
r in range(1080) if d[r*1920+100]>125))"
```

Expected: `first bright row: 540`.

Before this patch: `first bright row: 536` (1080/1088 of 540).

With this patch: `first bright row: 540`.

Tested on master 0e164cf74a with Intel UHD 630 (intel-media-driver 26.3.5) and 
AMD R9 280X (Mesa radeonsi 26.2.5), libva 2.24. The unpatched result is the 
same on 9.0.2.

<details><summary>What else was checked</summary>

- A 320x200 overlay (208-row surface): edge at row 100 moves from 96 to 100.
- Heights that are multiples of 16, overlays uploaded with `hwupload`, and the 
main input were not affected before and are unchanged.
- Output is byte-identical to the unpatched build for same-tag YUV overlays, 
RGB overlays, `alpha`, and `eof_action=pass` after the overlay ends, on both 
drivers.
- `hwupload,crop=32:64:32:0` on the overlay now draws the cropped region. It is 
stretched to the overlay's uncropped size unless `w`/`h` are set, which is how 
a cropped main input already behaves. Anyone who crops the overlay today sees 
the uncropped image, so this is a visible change for them.
- `surface_region = NULL` dates from 2aad9765ef, which stopped the overlay from 
sharing the main's input region.

</details>



>From 128a593c0bfcae04da43bbb5a3e486be5cd8f876 Mon Sep 17 00:00:00 2001
From: Iouri Goussev <[email protected]>
Date: Mon, 16 Nov 2026 19:04:13 -0500
Subject: [PATCH] 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.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to