Add ability to GPU device to be automatically detected when multiple exist

This commit is contained in:
Nick Mowen 2023-02-06 14:24:27 -07:00
parent 748815b6ce
commit 2c1a6673f3
2 changed files with 32 additions and 6 deletions

View File

@ -6,8 +6,34 @@ from typing import Any
from frigate.version import VERSION from frigate.version import VERSION
from frigate.const import BTBN_PATH from frigate.const import BTBN_PATH
from frigate.util import vainfo_hwaccel
TIMEOUT_PARAM = "-timeout" if os.path.exists(BTBN_PATH) else "-stimeout" TIMEOUT_PARAM = "-timeout" if os.path.exists(BTBN_PATH) else "-stimeout"
GPU_DEVICE_PARAM = None
def get_gpu_device() -> str:
"""Gets the appropriate Intel/AMD GPU device."""
if GPU_DEVICE_PARAM:
return GPU_DEVICE_PARAM
devices = filter(lambda d: d.startswith("render"), os.listdir("/dev/dri"))
if len(devices) < 2:
GPU_DEVICE_PARAM = "renderD128"
return GPU_DEVICE_PARAM
else:
for device in devices:
check = vainfo_hwaccel(device_name=device)
if check.returncode == 0:
GPU_DEVICE_PARAM = device
return GPU_DEVICE_PARAM
raise ValueError(
"Hardware acceleration was requested but no suitable GPU was found."
)
_user_agent_args = [ _user_agent_args = [
"-user_agent", "-user_agent",
@ -23,7 +49,7 @@ PRESETS_HW_ACCEL_DECODE = {
"-hwaccel", "-hwaccel",
"vaapi", "vaapi",
"-hwaccel_device", "-hwaccel_device",
"/dev/dri/renderD128", get_gpu_device(),
"-hwaccel_output_format", "-hwaccel_output_format",
"vaapi", "vaapi",
], ],
@ -31,7 +57,7 @@ PRESETS_HW_ACCEL_DECODE = {
"-hwaccel", "-hwaccel",
"qsv", "qsv",
"-qsv_device", "-qsv_device",
"/dev/dri/renderD128", get_gpu_device(),
"-hwaccel_output_format", "-hwaccel_output_format",
"qsv", "qsv",
"-c:v", "-c:v",
@ -43,7 +69,7 @@ PRESETS_HW_ACCEL_DECODE = {
"-hwaccel", "-hwaccel",
"qsv", "qsv",
"-qsv_device", "-qsv_device",
"/dev/dri/renderD128", get_gpu_device(),
"-hwaccel_output_format", "-hwaccel_output_format",
"qsv", "qsv",
"-c:v", "-c:v",

View File

@ -14,7 +14,7 @@ from abc import ABC, abstractmethod
from collections import Counter from collections import Counter
from collections.abc import Mapping from collections.abc import Mapping
from multiprocessing import shared_memory from multiprocessing import shared_memory
from typing import Any, AnyStr, Tuple from typing import Any, AnyStr, Optional, Tuple
import cv2 import cv2
import numpy as np import numpy as np
@ -976,9 +976,9 @@ def ffprobe_stream(path: str) -> sp.CompletedProcess:
return sp.run(ffprobe_cmd, capture_output=True) return sp.run(ffprobe_cmd, capture_output=True)
def vainfo_hwaccel() -> sp.CompletedProcess: def vainfo_hwaccel(device_name: Optional[str]) -> sp.CompletedProcess:
"""Run vainfo.""" """Run vainfo."""
ffprobe_cmd = ["vainfo"] ffprobe_cmd = ["vainfo"] if not device_name else ["vainfo", "--display", "drm", "--device", f"/dev/dri/{device_name}"]
return sp.run(ffprobe_cmd, capture_output=True) return sp.run(ffprobe_cmd, capture_output=True)