Skip to content

Pick a decoder that supports the requested hwaccel device - #2523

Merged
WyattBlue merged 3 commits into
PyAV-Org:masterfrom
hmaarrfk:hwaccel-decoder-selection
Nov 24, 2026
Merged

WyattBlue merged 3 commits into
PyAV-Org:masterfrom
hmaarrfk:hwaccel-decoder-selection

Conversation

@hmaarrfk

@hmaarrfk hmaarrfk commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

I'm trying to get access to the hardware accelerated encoders. i think this makes sense and seems to allow access to the nvidia encoder.

Feedback welcome.

Thx. Happy to open these discussions as issues / discussions in the future, however there are many discussions about this already:

Claude

Problem

Hardware decoding of AV1 does not work when FFmpeg is built with libdav1d:

hwaccel = av.codec.hwaccel.HWAccel("cuda", allow_software_fallback=False)
av.open("video_av1.mp4", hwaccel=hwaccel)
# RuntimeError: Hardware accelerated decode requested but no stream is compatible

With allow_software_fallback=True the stream is silently decoded in software. H.264 and HEVC are fine.

There are two causes.

  1. InputContainer picks the decoder with avcodec_find_decoder(), which returns the first decoder registered for the codec. For AV1 that is libdav1d, which has no hardware configs. The hwaccels belong to FFmpeg's native av1 decoder.
  2. Selecting the native decoder is not enough. VideoCodecContext._init sets pix_fmt to the hardware format before the decoder is opened. FFmpeg's AV1 decoder treats a context that already holds a hardware pixel format as one whose hwaccel is set up (get_pixel_format() in libavcodec/av1dec.c returns early), so it never calls get_format(), never initialises the hwaccel, and the first packet fails with get_buffer() failed / Get current frame error.

Change

  • When a hwaccel is given and the default decoder has no device-context hardware config for its device type, look through the other decoders of the same codec for one that has, and use it. The default decoder is still preferred, so nothing changes for codecs whose default decoder already supports the device, or when no hwaccel is passed.
  • Stop setting pix_fmt up front. The get_format callback that is already installed does the negotiation.
  • A test that decodes a generated AV1 file with the device from HWACCEL_DEVICE_TYPE. It skips when no AV1 encoder is available to make the file, or when FFmpeg has no AV1 hwaccel for the device.
  • A changelog entry.

Behaviour change to be aware of

CodecContext.pix_fmt on a hardware-accelerated decoder used to report the hardware format (for example cuda) as soon as the container was opened. It now reports the stream's software format until the first frame has been decoded, after which the decoder sets the hardware format itself. I did not find anything in PyAV that depends on the earlier value, but user code could.

Verification

On Linux with one NVIDIA GPU, FFmpeg 9.0.2 with libdav1d, HWACCEL_DEVICE_TYPE=cuda:

  • AV1 now decodes on the device, both with is_hw_owned=True (frames stay cuda) and False (frames come back as nv12), 600 frames each.
  • H.264 and HEVC hardware decode still work in both modes, at the same speed as before.
  • tests/test_decode.py: 31 passed, including the new test. test_hardware_decode fails here only because this FFmpeg build has no libx264 to generate its test file.
  • Full suite without a device: 612 passed, 37 skipped, 5 failed. The same 5 fail on unmodified master in this environment (4 need libx264, 1 is a local pytest plugin problem).
  • ruff format --check is clean.

Not verified:

  • Any device type other than cuda. VAAPI could not be opened on this machine, and I have no macOS or Windows machine for VideoToolbox, D3D11VA or D3D12VA.
  • mypy and isort, which are not installed here.
  • Which decoder is picked when several non-default decoders support the device. The first match in FFmpeg's registration order wins; for AV1 with cuda that is the native av1 decoder.

<details><summary>Claude's draft</summary>

av.open(path, hwaccel=...) always used avcodec_find_decoder(), which
returns the first decoder registered for a codec. For AV1 that is
libdav1d when FFmpeg is built with it, and libdav1d has no hardware
configs, so the stream was decoded in software, or the open failed with
"Hardware accelerated decode requested but no stream is compatible"
when allow_software_fallback is False.

When a hwaccel is given and the default decoder cannot use its device
type, look for another decoder of the same codec that can. The default
is still preferred, so nothing changes for codecs whose default decoder
already supports the device.

That alone is not enough for AV1. The decoder context was given the
hardware pixel format before it was opened, and FFmpeg's AV1 decoder
treats a context that already holds a hardware format as one whose
hwaccel is set up: it skips get_format(), never initialises the
hwaccel, and fails on the first packet with "get_buffer() failed". Stop
setting pix_fmt up front and let the decoder negotiate it through the
get_format callback that is already installed. CodecContext.pix_fmt now
reports the stream's software format until the first frame is decoded.
</details>
Comment thread av/container/input.py
for i in range(self.ptr.nb_streams):
stream = self.ptr.streams[i]
codec = lib.avcodec_find_decoder(stream.codecpar.codec_id)
codec = _find_decoder(stream.codecpar.codec_id, self.hwaccel)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Providing hwaccel here is what allows pyav to find the hardware accelerated encoder you want to find

@hmaarrfk
hmaarrfk marked this pull request as ready for review November 22, 2026 15:10
@WyattBlue

Copy link
Copy Markdown
Member

Thanks, the pix_fmt change looks right.

The decoder switch breaks software fallback, though. FFmpeg's native av1 decoder can only decode through a hwaccel, and having an AV1 hwaccel compiled in doesn't mean the GPU can actually decode AV1. On my M1 Pro with HWAccel("videotoolbox"), an AV1 file that master decodes in software via libdav1d now fails with NotImplementedError: Function not implemented: 'avcodec_send_packet()', even with allow_software_fallback=True. I think the same applies to pre-Ampere NVIDIA GPUs with cuda.

Could you limit the switch to allow_software_fallback=False, where the default decoder can't use the device anyway? The new test should also skip when the device can't decode AV1.

@hmaarrfk

Copy link
Copy Markdown
Contributor Author

i added the software fallback.

but hmm... the new test feels weak without the assertion and the silent skip.

@WyattBlue

Copy link
Copy Markdown
Member

A device without AV1 support fails on the first send_packet, so only that needs a skip.

To make the assertion stronger, open with is_hw_owned=True and assert that each frame's format is the hardware format (codec_context.hwaccel_ctx.config.format.name I think)

@hmaarrfk

Copy link
Copy Markdown
Contributor Author

i'm kinda ok without a test. what do you think? i don't have a mac today (and i likely won't be getting an M1).

You could also push the test you want.

Its just hard to "test" this stuff (which I guess is why it takes so long to develop).

@hmaarrfk
hmaarrfk force-pushed the hwaccel-decoder-selection branch from 5b30bc8 to db12391 Compare November 22, 2026 22:00
@WyattBlue
WyattBlue merged commit 6bb4a90 into PyAV-Org:master Nov 24, 2026
8 checks passed
@hmaarrfk

Copy link
Copy Markdown
Contributor Author

Thank you!

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants