From 88e9b1d2e0700f419501589d98692f1acf848d05 Mon Sep 17 00:00:00 2001 From: WyattBlue Date: Fri, 13 Nov 2026 00:53:23 -0500 Subject: [PATCH] Run hwaccel tests with VideoToolbox by default on macOS test_hardware_frame_reformat_matches_downloaded_frame decoded an interlaced sample, which VideoToolbox can't decode. Use a progressive H.264 sample instead. The hwaccel tests now default to videotoolbox on macOS outside CI, so they run locally without setting HWACCEL_DEVICE_TYPE. Setting it to an empty value disables them. Fixes #2515 --- tests/common.py | 37 ++++++++++++++++++++++++++ tests/test_decode.py | 63 ++++++++------------------------------------ tests/test_encode.py | 23 +++------------- 3 files changed, 51 insertions(+), 72 deletions(-) diff --git a/tests/common.py b/tests/common.py index d9785551b..5d83d9da4 100644 --- a/tests/common.py +++ b/tests/common.py @@ -4,12 +4,15 @@ import errno import functools import os +import sys import typing from typing import TYPE_CHECKING from unittest import TestCase as _Base import numpy as np +import pytest +import av.codec.hwaccel from av.datasets import fate as fate_suite try: @@ -33,6 +36,40 @@ skip_tests = frozenset(os.environ.get("PYAV_SKIP_TESTS", "").split(",")) +@functools.cache +def _hwaccel_device_type() -> str | None: + available = av.codec.hwaccel.hwdevices_available() + device_type = os.environ.get("HWACCEL_DEVICE_TYPE") + if device_type is None: + if ( + sys.platform == "darwin" + and "CI" not in os.environ + and "videotoolbox" in available + ): + return "videotoolbox" + return None + if not device_type: + return None + + assert device_type in available, f"{device_type} not available" + return device_type + + +def hwaccel_device_type() -> str: + """Return the hwaccel device type to test, or skip the test. + + Uses HWACCEL_DEVICE_TYPE if set (an empty value disables the tests). Otherwise + defaults to videotoolbox on macOS, except in CI where there is no usable GPU. + """ + device_type = _hwaccel_device_type() + if device_type is None: + pytest.skip( + "Set the HWACCEL_DEVICE_TYPE to run this test. Options are " + + " ".join(av.codec.hwaccel.hwdevices_available()) + ) + return device_type + + def safe_makedirs(path: str) -> None: try: os.makedirs(path) diff --git a/tests/test_decode.py b/tests/test_decode.py index 19581e671..f9b38394c 100644 --- a/tests/test_decode.py +++ b/tests/test_decode.py @@ -1,7 +1,6 @@ import functools import gc import io -import os import pathlib from fractions import Fraction from typing import cast @@ -16,7 +15,7 @@ from av.sidedata.sidedata import SideData, Type from av.subtitles.subtitle import SubtitleSet -from .common import TestCase, fate_suite +from .common import TestCase, fate_suite, hwaccel_device_type @functools.cache @@ -373,23 +372,13 @@ def test_side_data(self) -> None: assert frame.rotation == -90 def test_hardware_decode(self) -> 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)}" - ) - - HWACCEL_DEVICE_TYPE = os.environ["HWACCEL_DEVICE_TYPE"] - assert HWACCEL_DEVICE_TYPE in hwdevices_available, ( - f"{HWACCEL_DEVICE_TYPE} not available" - ) + device_type = hwaccel_device_type() test_video_path = "tests/assets/black.mp4" make_h264_test_video(test_video_path) hwaccel = av.codec.hwaccel.HWAccel( - device_type=HWACCEL_DEVICE_TYPE, allow_software_fallback=False + device_type=device_type, allow_software_fallback=False ) container = av.open(test_video_path, hwaccel=hwaccel) @@ -403,45 +392,25 @@ def test_hardware_decode(self) -> None: assert frame_count == video_stream.frames def test_hardware_create_keeps_flags(self) -> 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)}" - ) - - HWACCEL_DEVICE_TYPE = os.environ["HWACCEL_DEVICE_TYPE"] - assert HWACCEL_DEVICE_TYPE in hwdevices_available, ( - f"{HWACCEL_DEVICE_TYPE} not available" - ) + device_type = hwaccel_device_type() # AV_CUDA_USE_PRIMARY_CONTEXT. It is the only flag FFmpeg defines for # device creation, and every other device type ignores it. - hwaccel = av.codec.hwaccel.HWAccel(device_type=HWACCEL_DEVICE_TYPE, flags=1) + hwaccel = av.codec.hwaccel.HWAccel(device_type=device_type, flags=1) created = hwaccel.create(av.Codec("h264", "r")) assert created.flags == 1 @pytest.mark.parametrize("is_hw_owned", [False, True]) def test_hardware_decode_download_preserves_frame_props(is_hw_owned: bool) -> 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)}" - ) - - hwaccel_device_type = os.environ["HWACCEL_DEVICE_TYPE"] - assert hwaccel_device_type in hwdevices_available, ( - f"{hwaccel_device_type} not available" - ) + device_type = hwaccel_device_type() test_video_path = fate_suite("hevc/hdr10_plus_h265_sample.hevc") cpu_frame = decode_first_video_frame(test_video_path) hw_frame = decode_first_video_frame( test_video_path, av.codec.hwaccel.HWAccel( - device_type=hwaccel_device_type, + device_type=device_type, is_hw_owned=is_hw_owned, allow_software_fallback=False, ), @@ -457,23 +426,13 @@ def test_hardware_decode_download_preserves_frame_props(is_hw_owned: bool) -> No def test_hardware_frame_reformat_matches_downloaded_frame() -> 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)}" - ) - - hwaccel_device_type = os.environ["HWACCEL_DEVICE_TYPE"] - assert hwaccel_device_type in hwdevices_available, ( - f"{hwaccel_device_type} not available" - ) + device_type = hwaccel_device_type() - test_video_path = fate_suite("h264/interlaced_crop.mp4") + test_video_path = fate_suite("h264/extradata-reload-multi-stsd.mov") downloaded_frame = decode_first_video_frame( test_video_path, av.codec.hwaccel.HWAccel( - device_type=hwaccel_device_type, + device_type=device_type, is_hw_owned=False, allow_software_fallback=False, ), @@ -481,7 +440,7 @@ def test_hardware_frame_reformat_matches_downloaded_frame() -> None: hw_frame = decode_first_video_frame( test_video_path, av.codec.hwaccel.HWAccel( - device_type=hwaccel_device_type, + device_type=device_type, is_hw_owned=True, allow_software_fallback=False, ), diff --git a/tests/test_encode.py b/tests/test_encode.py index e8c4ec8d4..a07142566 100644 --- a/tests/test_encode.py +++ b/tests/test_encode.py @@ -2,7 +2,6 @@ import io import math -import os from fractions import Fraction from pathlib import Path @@ -15,7 +14,7 @@ from av.audio.stream import AudioStream from av.video.stream import VideoStream -from .common import TestCase, fate_suite, has_pillow +from .common import TestCase, fate_suite, has_pillow, hwaccel_device_type WIDTH = 320 HEIGHT = 240 @@ -606,15 +605,7 @@ def get_hwaccel_format(encoder: str, device_type: str) -> str: def test_hardware_encode() -> 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" + device_type = hwaccel_device_type() encoder = _HWACCEL_ENCODERS.get(device_type) if encoder is None: @@ -660,15 +651,7 @@ def test_hardware_encode() -> None: 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" + device_type = hwaccel_device_type() encoder = _HWACCEL_ENCODERS.get(device_type) if encoder is None: