Ffmpeg artifacts when encoding with h264_vaapi

As the title suggests, I have a problem with ffmpeg and I can’t figure out why it happens. When I try to transcode a video (any video I’ve tried) with hardware acceleration (h264_vaapi), ffmpeg produces artifacts in the final video. To illustrate what type of artifacts I’m talking about, here is a before and after:

Before (original file)

After

The command used is:

ffmpeg -vaapi_device /dev/dri/renderD128 -i input.mp4 -vf 'format=nv12,hwupload' -c:v h264_vaapi output.mp4 -y

Which simply encodes the file using h264_vaapi (decoding doesn’t seem to be the problem).
I also tried these commands:

# This essentially downscales the video:
ffmpeg -vaapi_device /dev/dri/renderD128 -i input.mp4 -vf 'format=nv12,hwupload,scale_vaapi=w=1280:h=720'  -c:v h264_vaapi output.mp4 -y
# If I understand this correctly, this both decodes and encodes using hardware acceleration
# Works only on certain files, doesn't work on included file (top of the post)
# (This was the original command I was trying to use)
ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128 -hwaccel_output_format vaapi -i input.mp4 -vf scale_vaapi=w=1280:h=720 -c:v h264_vaapi -b:v 2M -maxrate 2M -qp 20 output.mp4

They all have the same effect: random flashes of solid-colored frames. I’ve also noticed that with hevc_vaapi there are no problems, so it must be a problem relating with the h264_encoder.

I tested the same command with the same file on a Fedora live environment (I had a usb with the installer on it lying around) just to make sure the problem is not my hardware or the commands, and for some reason there were no artifacts. The version on fedora was 4.4.1, so other than potential hardware problems or mistakes in the commands, it doesn’t make sense to compare the two. Speaking of versions, on Manjaro I have the standard package from the extra repo (5.0-5). Complete output of ffmpeg -version:

ffmpeg version n5.0 Copyright (c) 2000-2022 the FFmpeg developers
built with gcc 11.2.0 (GCC)
configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-amf --enable-avisynth --enable-cuda-llvm --enable-lto --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libdrm --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libjack --enable-libmfx --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librav1e --enable-librsvg --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-libzimg --enable-nvdec --enable-nvenc --enable-shared --enable-version3
libavutil      57. 17.100 / 57. 17.100
libavcodec     59. 18.100 / 59. 18.100
libavformat    59. 16.100 / 59. 16.100
libavdevice    59.  4.100 / 59.  4.100
libavfilter     8. 24.100 /  8. 24.100
libswscale      6.  4.100 /  6.  4.100
libswresample   4.  3.100 /  4.  3.100
libpostproc    56.  3.100 / 56.  3.100

As for the graphics card, I have an AMD RX 5600 XT. Output of vainfo:

vainfo: VA-API version: 1.13 (libva 2.13.0)
vainfo: Driver version: Mesa Gallium driver 21.3.7 for AMD Radeon RX 5600 XT (NAVI10, DRM 3.44.0, 5.16.14-1-MANJARO, LLVM 13.0.1)
vainfo: Supported profile and entrypoints
      VAProfileMPEG2Simple            :	VAEntrypointVLD
      VAProfileMPEG2Main              :	VAEntrypointVLD
      VAProfileVC1Simple              :	VAEntrypointVLD
      VAProfileVC1Main                :	VAEntrypointVLD
      VAProfileVC1Advanced            :	VAEntrypointVLD
      VAProfileH264ConstrainedBaseline:	VAEntrypointVLD
      VAProfileH264ConstrainedBaseline:	VAEntrypointEncSlice
      VAProfileH264Main               :	VAEntrypointVLD
      VAProfileH264Main               :	VAEntrypointEncSlice
      VAProfileH264High               :	VAEntrypointVLD
      VAProfileH264High               :	VAEntrypointEncSlice
      VAProfileHEVCMain               :	VAEntrypointVLD
      VAProfileHEVCMain               :	VAEntrypointEncSlice
      VAProfileHEVCMain10             :	VAEntrypointVLD
      VAProfileHEVCMain10             :	VAEntrypointEncSlice
      VAProfileJPEGBaseline           :	VAEntrypointVLD
      VAProfileVP9Profile0            :	VAEntrypointVLD
      VAProfileVP9Profile2            :	VAEntrypointVLD
      VAProfileNone                   :	VAEntrypointVideoProc