Bug 470440 - ffmpeg screen capture with kmsgrab and vaapi either fails or gives corrupt video on kwin wayland
Summary: ffmpeg screen capture with kmsgrab and vaapi either fails or gives corrupt vi...
Status: RESOLVED UPSTREAM
Alias: None
Product: kwin
Classification: Plasma
Component: egl (show other bugs)
Version: 5.27.5
Platform: Arch Linux Linux
: NOR normal
Target Milestone: ---
Assignee: KWin default assignee
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2023-05-30 07:38 UTC by Vasishath Kaushal
Modified: 2023-06-10 21:18 UTC (History)
2 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments
Garbled video when recorded with KWIN_DRM_PREFER_COLOR_DEPTH=24 (3.77 MB, video/x-matroska)
2023-05-30 07:40 UTC, Vasishath Kaushal
Details
drm_info on KDE wayland with KWIN_DRM_PREFER_COLOR_DEPTH=24 (52.97 KB, application/octet-stream)
2023-05-30 07:43 UTC, Vasishath Kaushal
Details
Correct video with yuzu running in full screen mode (DRM format switches to XRGB8888) (921.99 KB, video/x-matroska)
2023-05-30 14:53 UTC, Vasishath Kaushal
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Vasishath Kaushal 2023-05-30 07:38:48 UTC
SUMMARY
On Intel UHD 620 (8th gen KBL-R), when using KDE with wayland backend, the default DRM color format is ARGB2101010 (30-bit). When trying to use kmsgrab with ffmpeg in this mode, it results in the error "drm format not supported by vaapi" since there is no match in vaapi for this drm format. On specifying KWIN_DRM_PREFER_COLOR_DEPTH=24 in /etc/environment, the DRM format changes to ARGB8888 (24-bit), this makes the ffmpeg kmsgrab successful but the output video is garbled. 

This works completely fine on Kwin X11 and GNOME wayland, the only difference I could find was that in both these cases, the color format was XRGB8888.

Here is the ffmpeg command I am running:

ffmpeg -device /dev/dri/card0 -f kmsgrab -framerate 60 -i - -vf 'hwmap=derive_device=vaapi,scale_vaapi=w=1920:h=1080:format=nv12' -c:v hevc_vaapi output.mkv

I have tried using h264_vaapi, vp8_vaapi codecs also, the result is same.

STEPS TO REPRODUCE
1. Login to Plasma wayland with KWIN_DRM_PREFER_COLOR_DEPTH=24
2. Record screen with ffmpeg kmsgrab using vaapi.

OBSERVED RESULT

Video output is grabled

EXPECTED RESULT

Normal screen capture

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: 6.3.4-zen1-1-zen (64-bit)/5.27.5
(available in About System)
KDE Plasma Version: 5.27.5
KDE Frameworks Version: 5.106.0
Qt Version: 5.15.9

ADDITIONAL INFORMATION
Mesa version: 23.2.0-devel (git-613eb64011)
ffmpeg version: 6.0
Comment 1 Vasishath Kaushal 2023-05-30 07:40:32 UTC
Created attachment 159342 [details]
Garbled video when recorded with KWIN_DRM_PREFER_COLOR_DEPTH=24
Comment 2 Vasishath Kaushal 2023-05-30 07:43:20 UTC
Created attachment 159343 [details]
drm_info on KDE wayland with KWIN_DRM_PREFER_COLOR_DEPTH=24
Comment 3 Vasishath Kaushal 2023-05-30 14:47:45 UTC
Update
I found that when I run the emulator yuzu in full screen, the DRM format changes to XRGB8888 and the screen recording also works fine in that case. Attaching the captured video.
Comment 4 Vasishath Kaushal 2023-05-30 14:53:50 UTC
Created attachment 159351 [details]
Correct video with yuzu running in full screen mode (DRM format switches to XRGB8888)
Comment 5 Zamundaaa 2023-06-10 21:18:26 UTC
Most likely this is caused by either kmsgrab or vaapi not handling the buffer format + modifier combination properly. If you get a correct image on the screen, KWin isn't doing anything wrong.

In general, please use the xdg portal system to do screen casting, which handles these sort of things automatically. kmsgrab is not a good way to do screen recording and will only get more problematic over time, as we start to use more hardware features it can't deal with.