Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)
Expand Down
63 changes: 11 additions & 52 deletions tests/test_decode.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import functools
import gc
import io
import os
import pathlib
from fractions import Fraction
from typing import cast
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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,
),
Expand All @@ -457,31 +426,21 @@ 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,
),
)
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,
),
Expand Down
23 changes: 3 additions & 20 deletions tests/test_encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io
import math
import os
from fractions import Fraction
from pathlib import Path

Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
Loading