Skip to content

A self-hosted media server and file management system

fix: storyboard generation does not derive qsv device correctly between platforms

aminnausin1a1f540c8d60
diff --git a/app/Services/Ffmpeg/FFmpegCommandBuilder.php b/app/Services/Ffmpeg/FFmpegCommandBuilder.php
index 48272bde..99f53db2 100644
--- a/app/Services/Ffmpeg/FFmpegCommandBuilder.php
+++ b/app/Services/Ffmpeg/FFmpegCommandBuilder.php
@@ -12,35 +12,56 @@ class FFmpegCommandBuilder {
) {}
public function storyboard(string $filePath, string $outputPattern, StoryboardOptions $options): array {
- $hw = $this->hardware->detect()->best();
+ $profile = $this->hardware->detect();
+ $hw = $profile->best();
- $defaultEncode = [
- 'mjpeg',
- ];
+ $defaultEncode = ['mjpeg'];
+ $w = $options->width;
+ $h = $options->height;
- $hardwareOptions = match ($hw) {
- HardwareType::CUDA => [
+ $hardwareOptions = match (true) {
+ $hw === HardwareType::CUDA => [
+ 'init' => [],
'decode' => ['-hwaccel', 'cuda'],
'decode_flags' => ['-noautoscale'],
'encode' => $defaultEncode,
],
- HardwareType::QSV => [
- 'decode' => [],
- 'decode_flags' => ['-noautoscale'],
+ $hw === HardwareType::QSV && $profile->qsv === 'derive_vaapi' && $profile->vaapiDevice => [
+ 'init' => [
+ '-init_hw_device',
+ "vaapi=va:{$profile->vaapiDevice}",
+ '-init_hw_device',
+ 'qsv=qs@va',
+ '-filter_hw_device',
+ 'qs',
+ ],
+ 'decode' => ['-hwaccel', 'vaapi', '-hwaccel_output_format', 'vaapi'],
+ 'decode_flags' => [],
+ 'encode' => ['mjpeg_qsv'],
+ ],
+ $hw === HardwareType::QSV && $profile->qsv === 'derive_d3d11va' => [
+ 'init' => [
+ '-init_hw_device',
+ 'd3d11va=dx11:,vendor=' . HardwareDetectionService::INTEL_VENDOR_ID,
+ '-init_hw_device',
+ 'qsv=qs@dx11',
+ '-filter_hw_device',
+ 'qs',
+ ],
+ 'decode' => ['-hwaccel', 'd3d11va', '-hwaccel_output_format', 'd3d11'],
+ 'decode_flags' => [],
'encode' => ['mjpeg_qsv'],
],
default => [
+ 'init' => [],
'decode' => [],
'decode_flags' => [],
'encode' => $defaultEncode,
],
};
- $w = $options->width;
- $h = $options->height;
-
$scale = match ($hw) {
- HardwareType::QSV => "vpp_qsv=w={$w}:h={$h}:format=nv12:out_range=pc:scale_mode=hq",
+ HardwareType::QSV => "hwmap=derive_device=qsv,vpp_qsv=w={$w}:h={$h}:format=nv12:passthrough=0:out_range=pc:scale_mode=hq",
default => "scale='min({$w},iw)':-2:flags=lanczos",
};
@@ -59,6 +80,7 @@ class FFmpegCommandBuilder {
return [
'ffmpeg',
+ ...$hardwareOptions['init'],
...$hardwareOptions['decode'],
...$skipFrame,
'-threads',
@@ -68,7 +90,7 @@ class FFmpegCommandBuilder {
'-an',
'-sn',
'-vf',
- "fps={$options->fps},{$scale},setparams=color_primaries=bt709:color_trc=bt709:colorspace=bt709,tile={$options->cols}x{$options->rows}",
+ "fps={$options->fps},setparams=color_primaries=bt709:color_trc=bt709:colorspace=bt709,{$scale},tile={$options->cols}x{$options->rows}",
'-threads',
'1',
'-c:v',
diff --git a/app/Services/Hardware/HardwareDetectionService.php b/app/Services/Hardware/HardwareDetectionService.php
index 740df710..82c6390f 100644
--- a/app/Services/Hardware/HardwareDetectionService.php
+++ b/app/Services/Hardware/HardwareDetectionService.php
@@ -8,6 +8,12 @@ use Symfony\Component\Process\Process;
class HardwareDetectionService {
private ?HardwareProfile $cached = null;
+ const PROFILE_VERSION = 2;
+
+ const CACHE_KEY = 'ffmpeg_hardware_profile_v' . self::PROFILE_VERSION;
+
+ const INTEL_VENDOR_ID = '0x8086';
+
const DEFAULT_ARGUMENTS = [
'-hide_banner',
'-loglevel',
@@ -19,25 +25,28 @@ class HardwareDetectionService {
return $this->cached;
}
- $cached = Cache::remember('ffmpeg_hardware_profile', 86400, function () {
+ $profile = Cache::remember(self::CACHE_KEY, 86400, function () {
$hwaccels = $this->getHwaccels();
$encoders = $this->getEncoders();
+ $vaapiDevice = $this->findIntelRenderDevice();
return [
'cuda' => in_array('cuda', $hwaccels) && $this->validateCuda(),
- 'qsv' => in_array('qsv', $hwaccels) && in_array('mjpeg_qsv', $encoders) && $this->validateQsv(),
+ 'qsv' => in_array('qsv', $hwaccels) && in_array('mjpeg_qsv', $encoders) ? $this->validateQsv($vaapiDevice) : null,
'vaapi' => in_array('vaapi', $hwaccels) && $this->validateVaapi(),
+ 'vaapi_device' => $vaapiDevice,
];
});
return $this->cached = new HardwareProfile(
- cuda: $cached['cuda'],
- qsv: $cached['qsv'],
- vaapi: $cached['vaapi'],
+ cuda: $profile['cuda'],
+ qsv: $profile['qsv'],
+ vaapi: $profile['vaapi'],
+ vaapiDevice: $profile['vaapi_device']
);
}
- public function getHwaccels(): array {
+ private function getHwaccels(): array {
$process = new Process(['ffmpeg', '-hwaccels', ...self::DEFAULT_ARGUMENTS]);
$process->run();
$output = $process->getOutput();
@@ -47,7 +56,7 @@ class HardwareDetectionService {
return array_values(array_filter(array_map('trim', $lines)));
}
- public function getEncoders(): array {
+ private function getEncoders(): array {
$process = new Process(['ffmpeg', '-encoders', '-hide_banner']);
$process->run();
preg_match_all('/\s(\w+)\s/', $process->getOutput(), $matches);
@@ -55,7 +64,7 @@ class HardwareDetectionService {
return $matches[1] ?? [];
}
- public function validateCuda(): bool {
+ private function validateCuda(): bool {
$process = new Process([
'ffmpeg',
...self::DEFAULT_ARGUMENTS,
@@ -78,7 +87,7 @@ class HardwareDetectionService {
return $process->isSuccessful();
}
- public function validateVaapi(): bool {
+ private function validateVaapi(): bool {
$process = new Process([
'ffmpeg',
...self::DEFAULT_ARGUMENTS,
@@ -101,7 +110,96 @@ class HardwareDetectionService {
return $process->isSuccessful();
}
- private function validateQsv(): bool {
+ private function validateQsv(?string $vaapiDevice): ?string {
+ return match (PHP_OS_FAMILY) {
+ 'Windows' => $this->validateQsvD3d11va() ? 'derive_d3d11va' : null,
+ 'Linux' => $this->validateQsvVaapi($vaapiDevice) ? 'derive_vaapi' : null,
+ default => null
+ };
+ }
+
+ /**
+ * Linux QSV detection using vaapi and a specific device path
+ */
+ private function validateQsvVaapi(string $devicePath): bool {
+ if (! $devicePath) {
+ return false;
+ }
+ $process = new Process([
+ 'ffmpeg',
+ ...self::DEFAULT_ARGUMENTS,
+ '-init_hw_device',
+ "vaapi=va:{$devicePath}",
+ '-init_hw_device',
+ 'qsv=qs@va',
+ '-filter_hw_device',
+ 'qs',
+ '-f',
+ 'lavfi',
+ '-i',
+ 'nullsrc=s=16x16',
+ '-frames:v',
+ '1',
+ '-f',
+ 'null',
+ '-',
+ ]);
+ $process->run();
+
+ return $process->isSuccessful();
+ }
+
+ /**
+ * Windows QSV detection using D3D11VA and the Intel vendor ID
+ */
+ private function validateQsvD3d11va(): bool {
+ $process = new Process([
+ 'ffmpeg',
+ ...self::DEFAULT_ARGUMENTS,
+ '-init_hw_device',
+ 'd3d11va=dx11:,vendor=' . self::INTEL_VENDOR_ID,
+ '-init_hw_device',
+ 'qsv=qs@dx11',
+ '-filter_hw_device',
+ 'qs',
+ '-f',
+ 'lavfi',
+ '-i',
+ 'nullsrc=s=16x16',
+ '-frames:v',
+ '1',
+ '-f',
+ 'null',
+ '-',
+ ]);
+ $process->run();
+
+ return $process->isSuccessful();
+ }
+
+ /**
+ * Searches for a render node with the Intel vendor ID
+ */
+ private function findIntelRenderDevice(): ?string {
+ $renderNodes = glob('/dev/dri/renderD*') ?: [];
+
+ foreach ($renderNodes as $node) {
+ $name = basename($node);
+ $vendorPath = "/sys/class/drm/{$name}/device/vendor";
+
+ if (is_readable($vendorPath) && trim(file_get_contents($vendorPath)) === self::INTEL_VENDOR_ID) {
+ return $node;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Old way of deriving basic qsv without vaapi or D3d11va
+ * This method was often slower than just using the CPU
+ */
+ private function validateQsvPlain(): bool {
$process = new Process([
'ffmpeg',
...self::DEFAULT_ARGUMENTS,
diff --git a/app/Services/Hardware/HardwareProfile.php b/app/Services/Hardware/HardwareProfile.php
index 3b101401..956c08e0 100644
--- a/app/Services/Hardware/HardwareProfile.php
+++ b/app/Services/Hardware/HardwareProfile.php
@@ -7,15 +7,15 @@ use App\Enums\HardwareType;
class HardwareProfile {
public function __construct(
public bool $cuda,
- public bool $qsv,
+ public ?string $qsv, // null | 'derive_vaapi' | 'derive_d3d11va'
public bool $vaapi,
+ public ?string $vaapiDevice,
) {}
public function best(): HardwareType {
return match (true) {
$this->cuda => HardwareType::CUDA,
- $this->qsv => HardwareType::QSV,
- $this->vaapi => HardwareType::VAAPI,
+ $this->qsv !== null => HardwareType::QSV,
default => HardwareType::CPU,
};
}