Skip to content

Allow configuring hardware encoder sw_format#2332

Merged
WyattBlue merged 1 commit into
PyAV-Org:mainfrom
CarlosRDomin:feat/configurable-hw-encode-sw-format
Jun 29, 2026
Merged

Allow configuring hardware encoder sw_format#2332
WyattBlue merged 1 commit into
PyAV-Org:mainfrom
CarlosRDomin:feat/configurable-hw-encode-sw-format

Conversation

@CarlosRDomin

Copy link
Copy Markdown
Contributor

Currently hardware encode setup derives AVHWFramesContext.sw_format from AVCodecContext.pix_fmt. If the user sets pix_fmt="cuda", PyAV treats that as the hardware format and falls back to nv12 for software format, with no public way to request a different software format such as yuv420p.

This PR adds a setter for VideoCodecContext.sw_format and makes _setup_encode_hwframes() prefer AVCodecContext.sw_pix_fmt when it is set, while preserving the existing stream.pix_fmt = "nv12" path and the current
nv12 default.

The new test_hardware_encode_honors_sw_format test verifies that pix_fmt="cuda" plus sw_format="yuv420p" opens the encoder with a CUDA hardware frames context whose software format remains yuv420p instead of nv12 (before this PR). Also tested on Mac with videotoolbox hwaccel with similar results.

Comment thread tests/test_encode.py
stream.pix_fmt = hw_format
stream.codec_context.sw_format = "yuv420p"

assert stream.codec_context.sw_format.name == "yuv420p"

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.

Note that this used to return None (hw_frames_ctx was NULL), now we allow getting (and setting) the value

Comment thread tests/test_encode.py
container.mux(packet)

assert stream.codec_context.pix_fmt == hw_format
assert stream.codec_context.sw_format.name == "yuv420p"

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.

Note that this used to return "nv12" (and would make the encoder use nv12 as the pixel format, with no way to control the value), now the sw_format property is respected

@CarlosRDomin CarlosRDomin force-pushed the feat/configurable-hw-encode-sw-format branch from bbb01aa to 72650ff Compare June 29, 2026 20:30
@WyattBlue WyattBlue merged commit e5104e4 into PyAV-Org:main Jun 29, 2026
8 checks passed
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