-
Notifications
You must be signed in to change notification settings - Fork 439
Allow configuring hardware encoder sw_format #2332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
WyattBlue
merged 1 commit into
PyAV-Org:main
from
CarlosRDomin:feat/configurable-hw-encode-sw-format
Jun 29, 2026
+75
−4
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -517,6 +517,13 @@ def test_profiles(self) -> None: | |
| } | ||
|
|
||
|
|
||
| def get_hwaccel_format(encoder: str, device_type: str) -> str: | ||
| for config in av.Codec(encoder, "w").hardware_configs: | ||
| if config.device_type.name == device_type and config.format is not None: | ||
| return config.format.name | ||
| pytest.skip(f"No hardware format for {device_type} on {encoder}") | ||
|
|
||
|
|
||
| def test_hardware_encode() -> None: | ||
| hwdevices_available = av.codec.hwaccel.hwdevices_available() | ||
| if "HWACCEL_DEVICE_TYPE" not in os.environ: | ||
|
|
@@ -569,3 +576,45 @@ def test_hardware_encode() -> None: | |
| with av.open(file, "r") as in_container: | ||
| decoded = sum(1 for _ in in_container.decode(video=0)) | ||
| assert decoded == n_frames | ||
|
|
||
|
|
||
| def test_hardware_encode_honors_sw_format() -> None: | ||
| hwdevices_available = av.codec.hwaccel.hwdevices_available() | ||
| if "HWACCEL_DEVICE_TYPE" not in os.environ: | ||
| pytest.skip( | ||
| "Set the HWACCEL_DEVICE_TYPE to run this test. " | ||
| f"Options are {' '.join(hwdevices_available)}" | ||
| ) | ||
|
|
||
| device_type = os.environ["HWACCEL_DEVICE_TYPE"] | ||
| assert device_type in hwdevices_available, f"{device_type} not available" | ||
|
|
||
| encoder = _HWACCEL_ENCODERS.get(device_type) | ||
| if encoder is None: | ||
| pytest.skip(f"No hardware encoder mapped for {device_type}") | ||
| hw_format = get_hwaccel_format(encoder, device_type) | ||
|
|
||
| hwaccel = av.codec.hwaccel.HWAccel( | ||
| device_type=device_type, allow_software_fallback=False | ||
| ) | ||
| container = av.open(io.BytesIO(), mode="w", format="mp4") | ||
| stream = container.add_stream(encoder, rate=30, hwaccel=hwaccel) | ||
| assert isinstance(stream, VideoStream) | ||
| stream.width = 320 | ||
| stream.height = 240 | ||
| stream.pix_fmt = hw_format | ||
| stream.codec_context.sw_format = "yuv420p" | ||
|
|
||
| assert stream.codec_context.sw_format is not None | ||
| assert stream.codec_context.sw_format.name == "yuv420p" | ||
|
|
||
| frame = VideoFrame(320, 240, "rgb24") | ||
| for packet in stream.encode(frame): | ||
| container.mux(packet) | ||
|
|
||
| assert stream.codec_context.pix_fmt == hw_format | ||
| assert stream.codec_context.sw_format is not None | ||
| assert stream.codec_context.sw_format.name == "yuv420p" | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| for packet in stream.encode(): | ||
| container.mux(packet) | ||
| container.close() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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_ctxwas NULL), now we allow getting (and setting) the value