Repository navigation
Pick a decoder that supports the requested hwaccel device - #2523
Conversation
<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>
| 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) |
There was a problem hiding this comment.
Providing hwaccel here is what allows pyav to find the hardware accelerated encoder you want to find
|
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 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. |
|
i added the software fallback. but hmm... the new test feels weak without the assertion and the silent skip. |
|
A device without AV1 support fails on the first send_packet, so only that needs a skip. To make the assertion stronger, open with |
|
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). |
5b30bc8 to
db12391
Compare
|
Thank you! |
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:
With
allow_software_fallback=Truethe stream is silently decoded in software. H.264 and HEVC are fine.There are two causes.
InputContainerpicks the decoder withavcodec_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 nativeav1decoder.VideoCodecContext._initsetspix_fmtto 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()inlibavcodec/av1dec.creturns early), so it never callsget_format(), never initialises the hwaccel, and the first packet fails withget_buffer() failed/Get current frame error.Change
hwaccelis 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 nohwaccelis passed.pix_fmtup front. Theget_formatcallback that is already installed does the negotiation.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.Behaviour change to be aware of
CodecContext.pix_fmton a hardware-accelerated decoder used to report the hardware format (for examplecuda) 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:is_hw_owned=True(frames staycuda) andFalse(frames come back asnv12), 600 frames each.tests/test_decode.py: 31 passed, including the new test.test_hardware_decodefails here only because this FFmpeg build has no libx264 to generate its test file.masterin this environment (4 need libx264, 1 is a local pytest plugin problem).ruff format --checkis clean.Not verified:
cuda. VAAPI could not be opened on this machine, and I have no macOS or Windows machine for VideoToolbox, D3D11VA or D3D12VA.mypyandisort, which are not installed here.cudathat is the nativeav1decoder.