Skip to content

VAAPI Encoder (2nd branch/attempt) - #7467

Merged
sr55 merged 3 commits into
HandBrake:masterfrom
sgothel:hw-encoder-vaapi2
Jun 22, 2026
Merged

sr55 merged 3 commits into
HandBrake:masterfrom
sgothel:hw-encoder-vaapi2

Conversation

@sgothel

@sgothel sgothel commented Nov 29, 2025 •

Copy link
Copy Markdown
Contributor

This is the 2nd clean VAAPI branch tackling issue 1083 #1083
and superseding pull-request #3039

supported: h264, hevc, av1, untested: vp8 and vp9

  • h264, hevc works on AMD >= NAVI10

  • av1 works on AMD > NAVI10(?)

  • Bitrate either VBR (variable) or CQP (constant)

  • Using Mesa 25 / libva 2.22 (Debian 13),
    vaapi supports writing required sequence- and global-packet.
    Therefor, no more patch is required to write into a MKV container.

On Debian 12, Mesa 22, these packets are not written
and hence no MKV container can be used.

+++

Tested on:
Test environment:
- GNU/Linux, Debian 13
- vainfo: VA-API version: 1.22 (libva 2.22.0)
- vainfo: Driver version: Mesa Gallium driver 25.0.7-2 for AMD Radeon RX 5700 XT (radeonsi, navi10, LLVM 19.1.7, DRM 3.61, 6.12.57+deb13-amd64)

@sr55
sr55 requested a review from galad87 November 29, 2025 19:06
@sr55 sr55 added this to the 1.11.0 milestone Nov 29, 2025
Comment thread libhb/handbrake/common.h Resolved
Comment thread libhb/vaapi_common.c Resolved
@bradleysepos
bradleysepos self-requested a review November 29, 2025 19:34
@bradleysepos

Copy link
Copy Markdown
Contributor

Thank you for this, looks pretty clean. Looking forward to again setting up Linux on my AMD machine to test (not a blocker).

Comment thread libhb/encavcodecaudio.c Resolved
Comment thread libhb/encavcodec.c Resolved
Comment thread libhb/encavcodec.c Resolved
Comment thread libhb/handbrake/common.h Resolved
Comment thread libhb/hb.c Resolved
Comment thread libhb/hb.c Resolved
@sgothel

sgothel commented Nov 30, 2025

Copy link
Copy Markdown
Contributor Author

I have force pushed this branch, addressed some comments.

@rathann

rathann commented Nov 30, 2025

Copy link
Copy Markdown

This is the 2nd clean VAAPI branch tackling issue 1083 #1083 and superseding pull-request #3039

Thanks for keeping at it!

h264 works on AMD >= NAVI10, unsupported/untested: h265, vp8 and vp9

I've got a NAVI22, which supports h264 and h265 encoding. I'll try to test this soon.

* Bitrate either VBR (variable) or CQP (constant)

This approach works w/o patched Mesa gallium va frontend, instead it uses a ffmpeg/avformat/matrokaenc patch allowing h264 streams w/o sequence nor global header. See https://trac.ffmpeg.org/ticket/10667.

FWIW, the current code in mesa looks like this:

         case VAConfigAttribEncPackedHeaders:
            value = VA_ENC_PACKED_HEADER_NONE;
            if ((u_reduce_video_profile(ProfileToPipe(profile)) == PIPE_VIDEO_FORMAT_MPEG4_AVC))
               value |= ENC_PACKED_HEADERS_H264;
            else if ((u_reduce_video_profile(ProfileToPipe(profile)) == PIPE_VIDEO_FORMAT_HEVC))
               value |= ENC_PACKED_HEADERS_HEVC;
            else if (u_reduce_video_profile(ProfileToPipe(profile)) == PIPE_VIDEO_FORMAT_AV1)
               value |= ENC_PACKED_HEADERS_AV1;

and the ENC_PACKED_HEADERS_* constants referenced above do include VA_ENC_PACKED_HEADER_SEQUENCE | VA_ENC_PACKED_HEADER_SLICE | VA_ENC_PACKED_HEADER_MISC.

To me, this means mesa doesn't need to be patched anymore, right?

@sgothel
sgothel force-pushed the hw-encoder-vaapi2 branch 2 times, most recently from 15ca91a to 646822a Compare December 1, 2025 06:02
@sgothel

sgothel commented Dec 1, 2025

Copy link
Copy Markdown
Contributor Author

Thanks for keeping at it!
.. and thank you for keep looking at it too.

I've got a NAVI22, which supports h264 and h265 encoding. I'll try to test this soon.
Great. Thx.

FWIW, the current code in mesa looks like this:

         case VAConfigAttribEncPackedHeaders:
            value = VA_ENC_PACKED_HEADER_NONE;
            if ((u_reduce_video_profile(ProfileToPipe(profile)) == PIPE_VIDEO_FORMAT_MPEG4_AVC))
               value |= ENC_PACKED_HEADERS_H264;
            else if ((u_reduce_video_profile(ProfileToPipe(profile)) == PIPE_VIDEO_FORMAT_HEVC))
               value |= ENC_PACKED_HEADERS_HEVC;
            else if (u_reduce_video_profile(ProfileToPipe(profile)) == PIPE_VIDEO_FORMAT_AV1)
               value |= ENC_PACKED_HEADERS_AV1;

and the ENC_PACKED_HEADERS_* constants referenced above do include VA_ENC_PACKED_HEADER_SEQUENCE | VA_ENC_PACKED_HEADER_SLICE | VA_ENC_PACKED_HEADER_MISC.

To me, this means mesa doesn't need to be patched anymore, right?

Yes, looks like!

Mesa commit https://gitlab.freedesktop.org/mesa/mesa/-/commit/fc4abbe27d063337a420d147cf8c9fa492789f71,
not sure which release that is (from July 2024).

So we may need to unblind vaapi in ffmpeg public API, allowing to query these values?
At least w/ my (old) Debian 12.12 version of my test machine this change didn't sooth ffmpeg yet,
still claiming no packet headers produced. Will have a check here.

@sgothel
sgothel force-pushed the hw-encoder-vaapi2 branch 2 times, most recently from ddab085 to 6f6728d Compare December 4, 2025 09:51
@sgothel

sgothel commented Dec 4, 2025

Copy link
Copy Markdown
Contributor Author

Force pushed w/ added av1_vaapi support (untested due to lack of testing hardware, like hevc_vaapi)

@Mastergatto

Mastergatto commented Dec 6, 2025 •

Copy link
Copy Markdown

Hi, I own a Radeon RX 9070 XT so I'm able to test this PR with H.264, HEVC and AV1.

First of all, here is what I get with vainfo on Linux:

Trying display: wayland
vainfo: VA-API version: 1.22 (libva 2.22.0)
vainfo: Driver version: Mesa Gallium driver 25.2.7-arch1.1 for AMD Radeon RX 9070 XT (radeonsi, gfx1201, LLVM 21.1.5, DRM 3.64, 6.17.9-arch1-1)
vainfo: Supported profile and entrypoints
      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
      VAProfileAV1Profile0            :	VAEntrypointVLD
      VAProfileAV1Profile0            :	VAEntrypointEncSlice
      VAProfileNone                   :	VAEntrypointVideoProc

As you can see, VAAPI works on my system and my GPU can encode with H.264, HEVC and AV1, but not with VP9.

The command line used for this test is: HandBrakeCLI -i 'video.ogg' -o '<codec>.mkv' -e <encoder parameter> where video.ogg is a sample video with a length of 8 seconds, is codified with Ogg/Theora and has a framerate of 25 fps. File size is 2,9 MB. Here's what I have found by trying to transcode it to H.264, HEVC and AV1.

H.264:
Despite it saying the encode has failed, it has converted the video although the result is so-so: the new video is effectively a slideshow going at 8 fps, and its size is reduced to 773 kB which doesn't make much sense.
https://pastebin.com/3KV5W2aY

HEVC:
The conversion is successful, the result is really good. The video size is reduced to 1,3 MB.
https://pastebin.com/BrgeStK1

AV1:
The conversion has been a failure. And to nitpick regarding the parameter name for flag -e (for Handbrake's CLI), for transcoding with AV1 through VAAPI should be 'vaapi_av1' not 'vaapi_avi1'.
https://pastebin.com/1y3St0gV

As a control test, I have also tried to transcode with ffmpeg, using VAAPI, and it has worked flawlessly for each codec.

@sgothel

sgothel commented Dec 6, 2025

Copy link
Copy Markdown
Contributor Author

Thank you!

for transcoding with AV1 through VAAPI should be 'vaapi_av1' not 'vaapi_avi1'
fixed typo

Hi, I own a Radeon RX 9070 XT so I'm able to test this PR with H.264, HEVC and AV1.
Great! I just figured I also have at least HEVC encoding available, just not AV1.

First of all, here is what I get with vainfo on Linux:

Trying display: wayland
vainfo: VA-API version: 1.22 (libva 2.22.0)
vainfo: Driver version: Mesa Gallium driver 25.2.7-arch1.1 for AMD Radeon RX 9070 XT (radeonsi, gfx1201, LLVM 21.1.5, DRM 3.64, 6.17.9-arch1-1)
vainfo: Supported profile and entrypoints

As you can see, VAAPI works on my system and my GPU can encode with H.264, HEVC and AV1, but not with VP9.

Yup!

The command line used for this test is: HandBrakeCLI -i 'video.ogg' -o '<codec>.mkv' -e <encoder parameter> where video.ogg is a sample video with a length of 8 seconds, is codified with Ogg/Theora and has a framerate of 25 fps. File size is 2,9 MB. Here's what I have found by trying to transcode it to H.264, HEVC and AV1.

H.264: Despite it saying the encode has failed, it has converted the video although the result is so-so: the new video is effectively a slideshow going at 8 fps, and its size is reduced to 773 kB which doesn't make much sense. https://pastebin.com/3KV5W2aY

I could reproduce this here also with ffmpeg transcoding, stuttering/jumping frames from theora,
but not from like AV1 or H264.

My test script:

LOGL="-loglevel 40"

ffmpeg -y ${LOGL} -ss 05:00 -vaapi_device /dev/dri/renderD128 -i m0.mkv -t 00:20 -vf format=nv12,hwupload -c:v h264_vaapi -c:a copy m0_h264_vaapi.mp4 2>&1 | tee m0_h264_vaapi.log
ffmpeg -y ${LOGL} -ss 05:00 -vaapi_device /dev/dri/renderD128 -i m0.mkv -t 00:20 -vf format=nv12,hwupload -c:v hevc_vaapi -c:a copy m0_hevc_vaapi.mp4 2>&1 | tee m0_hevc_vaapi.log
ffmpeg -y ${LOGL} -ss 05:00 -vaapi_device /dev/dri/renderD128 -i m0.mkv -t 00:20 -vf format=nv12,hwupload -c:v av1_vaapi -c:a copy m0_av1_vaapi.mp4 2>&1 | tee m0_av1_vaapi.log
ffmpeg -y ${LOGL} -ss 05:00 -i m0.mkv -t 00:20 -c:v libtheora -c:a opus -strict -2 m0_theora.ogg 2>&1 | tee m0_theora.log
ffmpeg -y ${LOGL} -ss 05:00 -i m0.mkv -t 00:20 -an -c:v libtheora m0_theora.aonly.ogg 2>&1 | tee m0_theora.aonly.log

IN_AV=m0_theora.ogg
IN_V=m0_theora.aonly.ogg

ffmpeg -y ${LOGL} -vaapi_device /dev/dri/renderD128 -i ${IN_AV} -vf format=nv12,hwupload -c:v h264_vaapi -c:a copy m0_theora_h264_vaapi.av.mp4 2>&1 | tee m0_theora_h264_vaapi.av.log
ffmpeg -y ${LOGL} -vaapi_device /dev/dri/renderD128 -i ${IN_V} -vf format=nv12,hwupload -c:v h264_vaapi m0_theora_h264_vaapi.v.mp4 2>&1 | tee m0_theora_h264_vaapi.v.log

The last two m0_theora_h264_vaapi.av.mp4 and m0_theora_h264_vaapi.v.mp4 show the bug.
Same w/ hevc/h265 vaapi from theora.

Notable, I use the same ffmpeg 8.0.1 version (manual build) as our current Handbrake build
commit 894da5ca7d742e4429ffb2af534fcda0103ef593 (HEAD -> b_8_0_1, tag: n8.0.1)

ffmpeg version n8.0.1 Copyright (c) 2000-2025 the FFmpeg developers
  built with gcc 12 (Debian 12.2.0-14+deb12u1)
  configuration: --enable-gpl --enable-shared --enable-runtime-cpudetect --prefix=/opt-linux-x86_64/ffmpeg-8.0.1 --enable-libmp3lame --enable-libtwolame --enable-libx264 --enable-libx265 --enable-libsvtav1 --enable-libvpx --enable-libtheora --enable-encoder=h264_nvenc --enable-encoder=hevc_nvenc --enable-encoder=h264_vaapi --enable-encoder=hevc_vaapi --enable-encoder=av1_vaapi --enable-encoder=vp8_vaapi --enable-encoder=vp9_vaapi
  libavutil      60.  8.100 / 60.  8.100
  libavcodec     62. 11.100 / 62. 11.100
  libavformat    62.  3.100 / 62.  3.100
  libavdevice    62.  1.100 / 62.  1.100
  libavfilter    11.  4.100 / 11.  4.100
  libswscale      9.  1.100 /  9.  1.100
  libswresample   6.  1.100 /  6.  1.100

This is potentially an ffmpeg issue ...

HEVC: The conversion is successful, the result is really good. The video size is reduced to 1,3 MB. https://pastebin.com/BrgeStK1

AV1: The conversion has been a failure. And to nitpick regarding the parameter name for flag -e (for Handbrake's CLI), for transcoding with AV1 through VAAPI should be 'vaapi_av1' not 'vaapi_avi1'. https://pastebin.com/1y3St0gV

As a control test, I have also tried to transcode with ffmpeg, using VAAPI, and it has worked flawlessly for each codec.

The HEVC + AV1 detection is also fixed, I was using h264 only parameter in hb_avcodec_test_encoder.
Thank you for pointing this out.

Will force push soon after some additional tests.

@Mastergatto

Copy link
Copy Markdown

With the proposed changes above, encode with av1 through VAAPI now works and with good results, though it still complains about missing profile:

[av1_vaapi @ 0x5621d0bfed80] No usable encoding profile found.
[20:40:58] hb_avcodec_test_encoder_available encoder=0x40018082: err -3

And a warning:
[20:40:58] encavcodecInit: Unknown avcodec option q

Also, while with CLI every codec works, but in the GUI only H.264 (vaapi) is available.

@Mastergatto

Mastergatto commented Dec 6, 2025 •

Copy link
Copy Markdown

This is potentially an ffmpeg issue ...

Not sure about the bug being in ffmpeg. I use the same version, 8.0.1.

In my case transcoding directly with ffmpeg was flawless with each codec, it was done with a command line like this, e.g. for H.264: ffmpeg -vaapi_device /dev/dri/renderD128 -i video.ogg -vf 'format=nv12,hwupload' -c:v h264_vaapi h264.mkv

@sgothel

sgothel commented Dec 6, 2025

Copy link
Copy Markdown
Contributor Author

@Mastergatto OK pushed revision w/ our noted changes

Below my test scripts.

Tested w/ below scripts and a proper theora test file (my bad),
no stuttering etc.

Couldn't test AV1, due to hardware - but h264 + hevc/h265 works OK w/ vaapi here.

If you test w/ HB, please ensure the --verbose=4 commandline,
which will enable ffmpeg verbose logging of the vaapi core.

+++

cat ffmpeg_test.sh

#!/bin/sh

LOGL="-loglevel 40"
IN=m1_h264.mp4

ffmpeg -y ${LOGL} -vaapi_device /dev/dri/renderD128 -i ${IN} -vf format=nv12,hwupload -c:v h264_vaapi -c:a copy m1_h264_vaapi.mp4 2>&1 | tee m1_h264_vaapi.log
ffmpeg -y ${LOGL} -vaapi_device /dev/dri/renderD128 -i ${IN} -vf format=nv12,hwupload -c:v hevc_vaapi -c:a copy m1_hevc_vaapi.mp4 2>&1 | tee m1_hevc_vaapi.log
ffmpeg -y ${LOGL} -vaapi_device /dev/dri/renderD128 -i ${IN} -vf format=nv12,hwupload -c:v av1_vaapi -c:a copy m1_av1_vaapi.mp4 2>&1 | tee m1_av1_vaapi.log

ffmpeg -y -i ${IN} -c:v libtheora -r 25 -qscale:v 10 -c:a opus -strict -2 m1_theora.ogg 2>&1 | tee m1_theora.log
ffmpeg -y -i ${IN} -an -c:v libtheora -r 25 -qscale:v 10 -strict -2 m1_theora.v.ogg 2>&1 | tee m1_theora.v.log

IN_TAV=m1_theora.ogg
IN_TV=m1_theora.v.ogg

ffmpeg -y ${LOGL} -vaapi_device /dev/dri/renderD128 -i ${IN_TAV} -vf format=nv12,hwupload -c:v h264_vaapi -c:a copy m1_theora_h264_vaapi.av.mp4 2>&1 | tee m1_theora_h264_vaapi.av.log
ffmpeg -y ${LOGL} -vaapi_device /dev/dri/renderD128 -i ${IN_TV} -vf format=nv12,hwupload -c:v h264_vaapi m1_theora_h264_vaapi.v.mp4 2>&1 | tee m1_theora_h264_vaapi.v.log

ffmpeg -y ${LOGL} -vaapi_device /dev/dri/renderD128 -i ${IN_TAV} -vf format=nv12,hwupload -c:v hevc_vaapi -c:a copy m1_theora_h265_vaapi.av.mp4 2>&1 | tee m1_theora_h265_vaapi.av.log
ffmpeg -y ${LOGL} -vaapi_device /dev/dri/renderD128 -i ${IN_TV} -vf format=nv12,hwupload -c:v hevc_vaapi m1_theora_h265_vaapi.v.mp4 2>&1 | tee m1_theora_h265_vaapi.v.log

cat hb_test.sh

#!/bin/sh

IN=m1_h264.mp4
IN_TAV=m1_theora.ogg
IN_TV=m1_theora.v.ogg

HB=/opt-linux-x86_64/HandBreak/bin/HandBrakeCLI
HB_OPTS="--verbose=4 --no-dvdnav"

${HB} ${HB_OPTS} -i ${IN} -o 'hb_x264_to_h264.mkv' -e vaapi_h264 2>&1 | tee hb_x264_to_h264.log
${HB} ${HB_OPTS} -i ${IN} -o 'hb_x264_to_h265.mkv' -e vaapi_hevc 2>&1 | tee hb_x264_to_h265.log

${HB} ${HB_OPTS} -i ${IN_TAV} -o 'hb_theora_to_h264.av.mkv' -e vaapi_h264 2>&1 | tee hb_theora_to_h264.av.log
${HB} ${HB_OPTS} -i ${IN_TAV} -o 'hb_theora_to_h265.av.mkv' -e vaapi_hevc 2>&1 | tee hb_theora_to_h265.av.log

${HB} ${HB_OPTS} -i ${IN_TV} -o 'hb_theora_to_h264.v.mkv' -e vaapi_h264 2>&1 | tee hb_theora_to_h264.v.log
${HB} ${HB_OPTS} -i ${IN_TV} -o 'hb_theora_to_h265.v.mkv' -e vaapi_hevc 2>&1 | tee hb_theora_to_h265.v.log

@sgothel

sgothel commented Dec 6, 2025

Copy link
Copy Markdown
Contributor Author

With the proposed changes above, encode with av1 through VAAPI now works and with good results, though it still complains about missing profile:

[av1_vaapi @ 0x5621d0bfed80] No usable encoding profile found.
[20:40:58] hb_avcodec_test_encoder_available encoder=0x40018082: err -3

And a warning: [20:40:58] encavcodecInit: Unknown avcodec option q

Also, while with CLI every codec works, but in the GUI only H.264 (vaapi) is available.

I revised the hb_avcodec_test_encoder and added av1_vaapi encoder in the ffmpeg module w/ last forced push,
should be visible in UI and working.

@Mastergatto

Mastergatto commented Dec 6, 2025 •

Copy link
Copy Markdown

H.264, still not good:
https://pastebin.com/M9PbtSH5
I have tried to convert other videos (Theora, AV1 and HEVC) to H.264, still the same result. I have also tried with a much longer video, Handbrake only converted the first 30 seconds and skipped the rest.

For what's worth, I have also tested AV1 - OK:
https://pastebin.com/npZ1gvjS

And HEVC - OK:
https://pastebin.com/akN9UGYY

For GUI, now all three VAAPI encoders are available in the menu, and they produce same result as the CLI.

Thank you for your hard work!

@sgothel

sgothel commented Dec 6, 2025

Copy link
Copy Markdown
Contributor Author

available

H.264, still not good: https://pastebin.com/M9PbtSH5 I have tried to convert other videos (Theora, AV1 and HEVC) to H.264, still the same result. I have also tried with a much longer video, Handbrake only converted the first 30 seconds and skipped the rest.

Hmm, weird .. as it works for me :|

What I see is this in your log

[matroska @ 0x7f6445e1ed00] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 120 >= 40
ERROR: avformatMux: track 0, av_interleaved_write_frame failed with error 'Invalid argument'
[matroska @ 0x7f6445e1ed00] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 120 >= 80

So this may need some more elaboration, could be due to
our different Mesa/vaapi implementation or hardware.

+++

Notable, your new Mesa/vaapi produces the packed header (our dire debate above), good.
(see last line - shows the packet header extradata size by vaapi)

[23:35:11] encavcodec: avcodec_open options: rc_mode=CQP,qp=22,b_depth=2
[h264_vaapi @ 0x7f6445cf82c0] Input surface format is nv12.
[h264_vaapi @ 0x7f6445cf82c0] Using VAAPI profile VAProfileH264High (7).
[h264_vaapi @ 0x7f6445cf82c0] Using VAAPI entrypoint VAEntrypointEncSlice (6).
[h264_vaapi @ 0x7f6445cf82c0] Using VAAPI render target format YUV420 (0x1).
[h264_vaapi @ 0x7f6445cf82c0] RC mode: CQP.
[h264_vaapi @ 0x7f6445cf82c0] Block Level bitrate control: OFF.
[h264_vaapi @ 0x7f6445cf82c0] RC quality: 22.
[h264_vaapi @ 0x7f6445cf82c0] RC framerate: 25/1 (25.00 fps).
[h264_vaapi @ 0x7f6445cf82c0] Driver does not report any additional prediction constraints.
[h264_vaapi @ 0x7f6445cf82c0] Using intra, P- and B-frames (supported references: 1 / 1).
[h264_vaapi @ 0x7f6445cf82c0] All wanted packed headers available (wanted 0xd, found 0x1f).
[23:35:11] Writing Metadata to output file...
[matroska @ 0x7f6445e1ed00] mkv_assemble: h264: extradata_size 46

For what's worth, I have also tested AV1 - OK: https://pastebin.com/npZ1gvjS

And HEVC - OK: https://pastebin.com/akN9UGYY

For GUI, now all three VAAPI encoders are available in the menu, and they produce same result as the CLI.

Great.

Thank you for your hard work!

Thank you!

@sgothel

sgothel commented Dec 8, 2025

Copy link
Copy Markdown
Contributor Author

force pushed changing inline function hb_vcodec_is_vaapi to regular function hb_video_encoder_is_vaapi, as required to build w/ debugging options --debug=std --optimize=none.

@sgothel

sgothel commented Jun 7, 2026 •

Copy link
Copy Markdown
Contributor Author

With latest rebase, I have successfully tested CBQ and VBR w/ VAAPI h264, hevc and av1 on a Navi48.
Notable on Navi48: Same CBQ produces higher bitrate on hevc and av1 than h264 - which is sort of questionable?

  • CBQ 21: h264 4.26Mbit/s, h265 5.58 Mbit/s, av1 12.5 Mbit/s
  • This is perhaps a Navi48 firmware bug?

Earlier, Navi10 worked for h264 and hevc.

  • CBQ 21: h264 6.7 Mbit/s, h265 5.3 Mbit/s

@sgothel

sgothel commented Jun 7, 2026 •

Copy link
Copy Markdown
Contributor Author

and yes, on Navi48 h264 vaapi is (visually) broken w/ HB - but working with ffmpeg, so I guess I have to look at it again :|

[h264_vaapi @ 0x7f7b80464a00] Ignoring attempt to flush encoder that doesn't support it
[matroska @ 0x7f7b80588bc0] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 501 >= 417

@sgothel

sgothel commented Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

linux/windows build failures: ERROR: download failure; HTTP Error 504: Gateway Time-out; continuing.
MacOS: Interrupted .. ???

@sgothel

sgothel commented Jun 8, 2026 •

Copy link
Copy Markdown
Contributor Author

re #7467 (comment)

the timestamp management in HB Encode, i.e. frame.{pts, duration},
is not compatible w/ all vaapi encode (AMD Navi48) (???), which leads to vaapi_h264
to corrupt/stutter the result and encoding to fail.

Skipping this HB encoding pts workaround in Encode (and hence not passing over the pts from frame to hw_frame)
leads to OK vaapi_h264 encoding.
Notable: vaapi example leaves the timestamp as-is, which lead me to this approach.

Need to check HB's history for this,
i.e. encavcodec.c's Bizarro ffmpeg requires timestamp time_base to be == framerate remark.

@sgothel
sgothel force-pushed the hw-encoder-vaapi2 branch from f8a765d to 78277c1 Compare June 20, 2026 10:21
@sgothel

sgothel commented Jun 20, 2026

Copy link
Copy Markdown
Contributor Author

force push delta:

  • encavcodec.c
    - in case of H264 w/ VAAPI, the PTS hack is disabled
    to work on AMD >= NAVI48
    - using vanilla PTS in frame and not the frame-count
    - hw_frame leaves PTS untouched

@sgothel

sgothel commented Jun 20, 2026

Copy link
Copy Markdown
Contributor Author

will do a rebase to latest and test the VAAPI H264 AMD NAVI48 "solution" on an older NAVI10 later this weekend.

sgothel added 2 commits June 20, 2026 13:02
supported: h264, hevc, av1, untested: vp8 and vp9
- h264 and hevc works on AMD >= NAVI10
- av1 works on AMD >= NAVI48

- Bitrate either VBR (variable) or CQP (constant)

- Using Mesa 25 / libva 2.22 (Debian 13),
  vaapi supports writing required sequence- and global-packet.
  Therefor, no more patch is required to write into a MKV container.

  On Debian 12, Mesa 22, these packets are not written
  and hence no MKV container can be used.

+++

Test environment:
- GNU/Linux, Debian 13
- vainfo: VA-API version: 1.22 (libva 2.22.0)
- vainfo: Driver version: Mesa Gallium driver 25.0.7-2 for AMD Radeon RX 5700 XT (radeonsi, navi10, LLVM 19.1.7, DRM 3.61, 6.12.57+deb13-amd64)

Codev h264_vaapi, avcodec_open options: rc_mode=CQP,qp=24,b_depth=2,profile=100,level=40 (profile high, level 4.0)
HD1080 source ~45min with 212-330fps
PSNR and SSIM difference to x264 below 1%,
having x264 using profile high, level 4.0, preset fast, rc 23.
Both using constant-quality w/ variable bitrate.

++++

If FEATURE vaapi is enable (autodetected),
the following libraries are linked: X11 va va-drm va-x11.

- hb.c:
  - Adding global static 'vaapi_device_ctx0' default device
    initialized via hb_avcodec_init() and released via hb_avcodec_free().
    The 'vaapi_device_ctx0' is used in vaa_common hb_avcodec_vaapi_set_hwframe_ctx(..).

  - hb_avcodec_vaapi_set_hwframe_ctx() attaches the new AVHWFramesContext
    to the AVCodecContext at initializing the encoder.
    This is the final sink for the vaapi hw-encoder
    and Encode(..) performs the frame conversion into it.

  - hb_avcodec_test_encoder(..) adds VAAPI branch,
    needs refinement

- encavcodec.c
  - adding VAAPI branches, enabling available, needs refinement.
    Currently profile and level are set to the user preferences.

  - added hb_avcodec_test_encoder*(..) functions, currently only
    used to check vaapi-*codec*-availability.
    We used this codefragment regularly in an earlier HB version.

  - in case of H264 w/ VAAPI, the PTS hack is disabled
    to work on AMD >= NAVI48
    - using vanilla PTS in frame and not the frame-count
    - hw_frame leaves PTS untouched

+++

Discussion:

VAAPI Workflow:
  - It uses hardware frames on the target device, which are being transported
    from the software device. The frame transport implicitly converts the pix_fmt.

  - AVCodecContext's pix_fmt uses AV_PIX_FMT_VAAPI

  - AVHWFramesContext: format uses AV_PIX_FMT_VAAPI and sw_format AV_PIX_FMT_NV12,
    the latter hinting on the actual hw-frame's target format.

  - AV_PIX_FMT_NV12 uses interleaved UV data, where AV_PIX_FMT_YUV420P uses
    seperated planes. Both use a separated Y plane upfront.
    Therefor both formats are not picture compatible, memory requirements are same.

  - Encode(..) allocates a hw_frame and the source frame is being transported
    to the hw-frame target using the pic_fmt conversion to NV12.

    Finally the hw_frame is being sent 'avcodec_send_frame' and the code-path re-aligns
    with non VAAPI.

Further fixes to do:
    add custom extra-video-encoder field
@sgothel
sgothel force-pushed the hw-encoder-vaapi2 branch from 78277c1 to a8d3a1b Compare June 20, 2026 12:23
@sgothel

sgothel commented Jun 20, 2026

Copy link
Copy Markdown
Contributor Author

OK, rebased to master tip and added missing vaapi_av1 in GTK menu.

Tested on AMD NAVI10 (h264, h265) + NAVI48 (av1, h264, h265), all passing my test scripts.
Hence the hack as described in #7467 (comment) did its job.

@sr55 now up to you and team to merge, I am done

@sr55

sr55 commented Jun 20, 2026

Copy link
Copy Markdown
Contributor

Thanks @sgothel

Testing Notes:

  • Intel iGPU -> H.264 = Seems to work ok. H.265 has DTS issues

I'm going to setup another rig and try validate the AMD side of things.

IMO, we should just get this in and start gaining feedback as no doubt we'll have who knows what come out the woodwork.

@sgothel

sgothel commented Jun 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @sgothel

Testing Notes:

* Intel iGPU -> H.264 =  Seems to work ok.   H.265 has DTS issues

Possible that for Intel the PTS/dts hack also needs to be disabled (#7467 (comment) ).
However, when I disabled the hack for AV1, it didn't work anymore for Navi48.
Note: The ffmpeg example vaapi encoder doesn't use it.

(I didn't test Intel, so great reading something is working there too)

Hence vaapi encoding is a sensitive setup (even w/ same brand, Navi10/48)
and same CQP param may lead to different bitrates with different codecs and same encoder
or same codec and different encoder. Hence each user must tune for themselves.
(VBR produced a reliable target bitrate though)

I'm going to setup another rig and try validate the AMD side of things.

Excellent, I tested Navi10 + 48 as described under Debian 13.

IMO, we should just get this in and start gaining feedback as no doubt we'll have who knows what come out the woodwork.

Agreed, so far usable within known environment / constraints.

Thank you!

@sr55
sr55 dismissed galad87’s stale review June 22, 2026 17:54

Closing for now.

@sr55
sr55 merged commit 2aa59a5 into HandBrake:master Jun 22, 2026
7 of 8 checks passed
@sr55

sr55 commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Ok, I've pushed this. Thanks for persisting @sgothel

Lets get some feedback in and see how things go.

@sgothel

sgothel commented Jun 23, 2026

Copy link
Copy Markdown
Contributor Author

@sr55 thx for merging. I noticed that you dropped my (long) commit message and guess its intentional. Reason was to give a starting point for more insight in case of a later analysis etc. Up to you.

@sr55

sr55 commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

I kind of figured since it links pack to the Pull Request, it was probably ok to point here but fair point.

@Headcrabed

Copy link
Copy Markdown

@galad87

galad87 commented Jun 27, 2026

Copy link
Copy Markdown
Contributor

I tested on the latest Ubuntu on a Lunar Lake laptop, and the timestamps are completely broken when the encoder uses B-frames.

vaapi mp4 log.txt

@sgothel

sgothel commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor Author

I tested on the latest Ubuntu on a Lunar Lake laptop, and the timestamps are completely broken when the encoder uses B-frames.

vaapi mp4 log.txt

Thx. Reproduced w/ mp4 container. Works w/ mkv container.

We need to file (an issue and) a subsequent PR to fix this for mp4 container.

Edit: Notable: Encoding fails w/ result = 4.

[mp4 @ 0x7f89d8579900] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 26610333 >= 26610333
ERROR: avformatMux: track 0, av_interleaved_write_frame failed with error 'Invalid argument'
[mp4 @ 0x7f89d8579900] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 26610333 >= 26610333
[18:38:09] worker Muxer waiting to die
...
[18:38:09] libhb: work result = 4

Encode failed (error 4).

Is this ffmpeg issue related?

@sgothel

sgothel commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

OK, I filed #7976 so we can work on it and document issues.
Perhaps change title and subject to accumulate more issues?

@smdstudios

Copy link
Copy Markdown

Is this in any currently shipping build of Handbrake?

@Toorero

This comment was marked as low quality.

@bradleysepos

Copy link
Copy Markdown
Contributor

@smdstudios It will be part of HandBrake 1.12.0, not yet released. Alternatively, you can try a development snapshot build, which you can run alongside releases (installs separately): https://github.com/HandBrake/HandBrake-snapshots

@smdstudios

Copy link
Copy Markdown

@smdstudios It will be part of HandBrake 1.12.0, not yet released. Alternatively, you can try a development snapshot build, which you can run alongside releases (installs separately): https://github.com/HandBrake/HandBrake-snapshots

I installed the latest development snapshot (20260813163945-29a705219-master) and there's no option for hardware acceleration on Radeon.
image

@bradleysepos

Copy link
Copy Markdown
Contributor

@smdstudios Please open a new issue and include the Activity Log generated on startup so we can see what's going on under the hood. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.