1
0
Fork 0
mirror of https://github.com/immich-app/immich.git synced 2025-01-16 16:56:46 +01:00

fix(server): always set transcoding device, prefer renderD* (#14455)

always set device, prefer renderD*
This commit is contained in:
Mert 2024-12-02 20:28:50 -05:00 committed by GitHub
parent ba71fd42da
commit 52247c3650
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 73 additions and 105 deletions

View file

@ -1637,7 +1637,10 @@ describe(MediaService.name, () => {
'/original/path.ext', '/original/path.ext',
'upload/encoded-video/user-id/as/se/asset-id.mp4', 'upload/encoded-video/user-id/as/se/asset-id.mp4',
expect.objectContaining({ expect.objectContaining({
inputOptions: expect.arrayContaining(['-init_hw_device qsv=hw', '-filter_hw_device hw']), inputOptions: expect.arrayContaining([
'-init_hw_device qsv=hw,child_device=/dev/dri/renderD128',
'-filter_hw_device hw',
]),
outputOptions: expect.arrayContaining([ outputOptions: expect.arrayContaining([
`-c:v h264_qsv`, `-c:v h264_qsv`,
'-c:a copy', '-c:a copy',
@ -1696,7 +1699,10 @@ describe(MediaService.name, () => {
'/original/path.ext', '/original/path.ext',
'upload/encoded-video/user-id/as/se/asset-id.mp4', 'upload/encoded-video/user-id/as/se/asset-id.mp4',
expect.objectContaining({ expect.objectContaining({
inputOptions: expect.arrayContaining(['-init_hw_device qsv=hw', '-filter_hw_device hw']), inputOptions: expect.arrayContaining([
'-init_hw_device qsv=hw,child_device=/dev/dri/renderD128',
'-filter_hw_device hw',
]),
outputOptions: expect.not.arrayContaining([expect.stringContaining('-preset')]), outputOptions: expect.not.arrayContaining([expect.stringContaining('-preset')]),
twoPass: false, twoPass: false,
}), }),
@ -1713,7 +1719,10 @@ describe(MediaService.name, () => {
'/original/path.ext', '/original/path.ext',
'upload/encoded-video/user-id/as/se/asset-id.mp4', 'upload/encoded-video/user-id/as/se/asset-id.mp4',
expect.objectContaining({ expect.objectContaining({
inputOptions: expect.arrayContaining(['-init_hw_device qsv=hw', '-filter_hw_device hw']), inputOptions: expect.arrayContaining([
'-init_hw_device qsv=hw,child_device=/dev/dri/renderD128',
'-filter_hw_device hw',
]),
outputOptions: expect.arrayContaining(['-low_power 1']), outputOptions: expect.arrayContaining(['-low_power 1']),
twoPass: false, twoPass: false,
}), }),
@ -1730,6 +1739,26 @@ describe(MediaService.name, () => {
expect(loggerMock.debug).toHaveBeenCalledWith('No devices found in /dev/dri.'); expect(loggerMock.debug).toHaveBeenCalledWith('No devices found in /dev/dri.');
}); });
it('should prefer higher index renderD* device for qsv', async () => {
storageMock.readdir.mockResolvedValue(['card1', 'renderD129', 'card0', 'renderD128']);
mediaMock.probe.mockResolvedValue(probeStub.matroskaContainer);
systemMock.get.mockResolvedValue({ ffmpeg: { accel: TranscodeHWAccel.QSV } });
assetMock.getByIds.mockResolvedValue([assetStub.video]);
await sut.handleVideoConversion({ id: assetStub.video.id });
expect(mediaMock.transcode).toHaveBeenCalledWith(
'/original/path.ext',
'upload/encoded-video/user-id/as/se/asset-id.mp4',
expect.objectContaining({
inputOptions: expect.arrayContaining([
'-init_hw_device qsv=hw,child_device=/dev/dri/renderD129',
'-filter_hw_device hw',
]),
outputOptions: expect.arrayContaining([`-c:v h264_qsv`]),
twoPass: false,
}),
);
});
it('should use hardware decoding for qsv if enabled', async () => { it('should use hardware decoding for qsv if enabled', async () => {
storageMock.readdir.mockResolvedValue(['renderD128']); storageMock.readdir.mockResolvedValue(['renderD128']);
mediaMock.probe.mockResolvedValue(probeStub.matroskaContainer); mediaMock.probe.mockResolvedValue(probeStub.matroskaContainer);
@ -1750,6 +1779,7 @@ describe(MediaService.name, () => {
'-async_depth 4', '-async_depth 4',
'-noautorotate', '-noautorotate',
'-threads 1', '-threads 1',
'-qsv_device /dev/dri/renderD128',
]), ]),
outputOptions: expect.arrayContaining([ outputOptions: expect.arrayContaining([
expect.stringContaining('scale_qsv=-1:720:async_depth=4:mode=hq:format=nv12'), expect.stringContaining('scale_qsv=-1:720:async_depth=4:mode=hq:format=nv12'),
@ -1939,8 +1969,8 @@ describe(MediaService.name, () => {
); );
}); });
it('should prefer gpu for vaapi if available', async () => { it('should prefer higher index renderD* device for vaapi', async () => {
storageMock.readdir.mockResolvedValue(['renderD129', 'card1', 'card0', 'renderD128']); storageMock.readdir.mockResolvedValue(['card1', 'renderD129', 'card0', 'renderD128']);
mediaMock.probe.mockResolvedValue(probeStub.matroskaContainer); mediaMock.probe.mockResolvedValue(probeStub.matroskaContainer);
systemMock.get.mockResolvedValue({ ffmpeg: { accel: TranscodeHWAccel.VAAPI } }); systemMock.get.mockResolvedValue({ ffmpeg: { accel: TranscodeHWAccel.VAAPI } });
assetMock.getByIds.mockResolvedValue([assetStub.video]); assetMock.getByIds.mockResolvedValue([assetStub.video]);
@ -1950,27 +1980,7 @@ describe(MediaService.name, () => {
'upload/encoded-video/user-id/as/se/asset-id.mp4', 'upload/encoded-video/user-id/as/se/asset-id.mp4',
expect.objectContaining({ expect.objectContaining({
inputOptions: expect.arrayContaining([ inputOptions: expect.arrayContaining([
'-init_hw_device vaapi=accel:/dev/dri/card1', '-init_hw_device vaapi=accel:/dev/dri/renderD129',
'-filter_hw_device accel',
]),
outputOptions: expect.arrayContaining([`-c:v h264_vaapi`]),
twoPass: false,
}),
);
});
it('should prefer higher index gpu node', async () => {
storageMock.readdir.mockResolvedValue(['renderD129', 'renderD130', 'renderD128']);
mediaMock.probe.mockResolvedValue(probeStub.matroskaContainer);
systemMock.get.mockResolvedValue({ ffmpeg: { accel: TranscodeHWAccel.VAAPI } });
assetMock.getByIds.mockResolvedValue([assetStub.video]);
await sut.handleVideoConversion({ id: assetStub.video.id });
expect(mediaMock.transcode).toHaveBeenCalledWith(
'/original/path.ext',
'upload/encoded-video/user-id/as/se/asset-id.mp4',
expect.objectContaining({
inputOptions: expect.arrayContaining([
'-init_hw_device vaapi=accel:/dev/dri/renderD130',
'-filter_hw_device accel', '-filter_hw_device accel',
]), ]),
outputOptions: expect.arrayContaining([`-c:v h264_vaapi`]), outputOptions: expect.arrayContaining([`-c:v h264_vaapi`]),
@ -2020,6 +2030,7 @@ describe(MediaService.name, () => {
'-hwaccel_output_format vaapi', '-hwaccel_output_format vaapi',
'-noautorotate', '-noautorotate',
'-threads 1', '-threads 1',
'-hwaccel_device /dev/dri/renderD128',
]), ]),
outputOptions: expect.arrayContaining([ outputOptions: expect.arrayContaining([
expect.stringContaining('scale_vaapi=-2:720:mode=hq:out_range=pc:format=nv12'), expect.stringContaining('scale_vaapi=-2:720:mode=hq:out_range=pc:format=nv12'),

View file

@ -322,14 +322,14 @@ export class BaseConfig implements VideoCodecSWConfig {
} }
export class BaseHWConfig extends BaseConfig implements VideoCodecHWConfig { export class BaseHWConfig extends BaseConfig implements VideoCodecHWConfig {
protected devices: string[]; protected device: string;
constructor( constructor(
protected config: SystemConfigFFmpegDto, protected config: SystemConfigFFmpegDto,
devices: string[] = [], devices: string[] = [],
) { ) {
super(config); super(config);
this.devices = this.validateDevices(devices); this.device = this.getDevice(devices);
} }
getSupportedCodecs() { getSupportedCodecs() {
@ -337,20 +337,31 @@ export class BaseHWConfig extends BaseConfig implements VideoCodecHWConfig {
} }
validateDevices(devices: string[]) { validateDevices(devices: string[]) {
return devices if (devices.length === 0) {
.filter((device) => device.startsWith('renderD') || device.startsWith('card')) throw new Error('No /dev/dri devices found. If using Docker, make sure at least one /dev/dri device is mounted');
.sort((a, b) => {
// order GPU devices first
if (a.startsWith('card') && b.startsWith('renderD')) {
return -1;
} }
if (a.startsWith('renderD') && b.startsWith('card')) {
return 1; return devices.filter(function (device) {
} return device.startsWith('renderD') || device.startsWith('card');
return -a.localeCompare(b);
}); });
} }
getDevice(devices: string[]) {
if (this.config.preferredHwDevice === 'auto') {
// eslint-disable-next-line unicorn/no-array-reduce
return `/dev/dri/${this.validateDevices(devices).reduce(function (a, b) {
return a.localeCompare(b) < 0 ? b : a;
})}`;
}
const deviceName = this.config.preferredHwDevice.replace('/dev/dri/', '');
if (!devices.includes(deviceName)) {
throw new Error(`Device '${deviceName}' does not exist. If using Docker, make sure this device is mounted`);
}
return `/dev/dri/${deviceName}`;
}
getVideoCodec(): string { getVideoCodec(): string {
return `${this.config.targetVideoCodec}_${this.config.accel}`; return `${this.config.targetVideoCodec}_${this.config.accel}`;
} }
@ -361,20 +372,6 @@ export class BaseHWConfig extends BaseConfig implements VideoCodecHWConfig {
} }
return this.config.gopSize; return this.config.gopSize;
} }
getPreferredHardwareDevice(): string | undefined {
const device = this.config.preferredHwDevice;
if (device === 'auto') {
return;
}
const deviceName = device.replace('/dev/dri/', '');
if (!this.devices.includes(deviceName)) {
throw new Error(`Device '${device}' does not exist`);
}
return `/dev/dri/${deviceName}`;
}
} }
export class ThumbnailConfig extends BaseConfig { export class ThumbnailConfig extends BaseConfig {
@ -513,12 +510,16 @@ export class AV1Config extends BaseConfig {
} }
export class NvencSwDecodeConfig extends BaseHWConfig { export class NvencSwDecodeConfig extends BaseHWConfig {
getDevice() {
return '0';
}
getSupportedCodecs() { getSupportedCodecs() {
return [VideoCodec.H264, VideoCodec.HEVC, VideoCodec.AV1]; return [VideoCodec.H264, VideoCodec.HEVC, VideoCodec.AV1];
} }
getBaseInputOptions() { getBaseInputOptions() {
return ['-init_hw_device cuda=cuda:0', '-filter_hw_device cuda']; return [`-init_hw_device cuda=cuda:${this.device}`, '-filter_hw_device cuda'];
} }
getBaseOutputOptions(target: TranscodeTarget, videoStream: VideoStreamInfo, audioStream?: AudioStreamInfo) { getBaseOutputOptions(target: TranscodeTarget, videoStream: VideoStreamInfo, audioStream?: AudioStreamInfo) {
@ -641,17 +642,7 @@ export class NvencHwDecodeConfig extends NvencSwDecodeConfig {
export class QsvSwDecodeConfig extends BaseHWConfig { export class QsvSwDecodeConfig extends BaseHWConfig {
getBaseInputOptions() { getBaseInputOptions() {
if (this.devices.length === 0) { return [`-init_hw_device qsv=hw,child_device=${this.device}`, '-filter_hw_device hw'];
throw new Error('No QSV device found');
}
let qsvString = '';
const hwDevice = this.getPreferredHardwareDevice();
if (hwDevice) {
qsvString = `,child_device=${hwDevice}`;
}
return [`-init_hw_device qsv=hw${qsvString}`, '-filter_hw_device hw'];
} }
getBaseOutputOptions(target: TranscodeTarget, videoStream: VideoStreamInfo, audioStream?: AudioStreamInfo) { getBaseOutputOptions(target: TranscodeTarget, videoStream: VideoStreamInfo, audioStream?: AudioStreamInfo) {
@ -721,23 +712,14 @@ export class QsvSwDecodeConfig extends BaseHWConfig {
export class QsvHwDecodeConfig extends QsvSwDecodeConfig { export class QsvHwDecodeConfig extends QsvSwDecodeConfig {
getBaseInputOptions() { getBaseInputOptions() {
if (this.devices.length === 0) { return [
throw new Error('No QSV device found');
}
const options = [
'-hwaccel qsv', '-hwaccel qsv',
'-hwaccel_output_format qsv', '-hwaccel_output_format qsv',
'-async_depth 4', '-async_depth 4',
'-noautorotate', '-noautorotate',
`-qsv_device ${this.device}`,
...this.getInputThreadOptions(), ...this.getInputThreadOptions(),
]; ];
const hwDevice = this.getPreferredHardwareDevice();
if (hwDevice) {
options.push(`-qsv_device ${hwDevice}`);
}
return options;
} }
getFilterOptions(videoStream: VideoStreamInfo) { getFilterOptions(videoStream: VideoStreamInfo) {
@ -789,16 +771,7 @@ export class QsvHwDecodeConfig extends QsvSwDecodeConfig {
export class VaapiSwDecodeConfig extends BaseHWConfig { export class VaapiSwDecodeConfig extends BaseHWConfig {
getBaseInputOptions() { getBaseInputOptions() {
if (this.devices.length === 0) { return [`-init_hw_device vaapi=accel:${this.device}`, '-filter_hw_device accel'];
throw new Error('No VAAPI device found');
}
let hwDevice = this.getPreferredHardwareDevice();
if (!hwDevice) {
hwDevice = `/dev/dri/${this.devices[0]}`;
}
return [`-init_hw_device vaapi=accel:${hwDevice}`, '-filter_hw_device accel'];
} }
getFilterOptions(videoStream: VideoStreamInfo) { getFilterOptions(videoStream: VideoStreamInfo) {
@ -856,22 +829,13 @@ export class VaapiSwDecodeConfig extends BaseHWConfig {
export class VaapiHwDecodeConfig extends VaapiSwDecodeConfig { export class VaapiHwDecodeConfig extends VaapiSwDecodeConfig {
getBaseInputOptions() { getBaseInputOptions() {
if (this.devices.length === 0) { return [
throw new Error('No VAAPI device found');
}
const options = [
'-hwaccel vaapi', '-hwaccel vaapi',
'-hwaccel_output_format vaapi', '-hwaccel_output_format vaapi',
'-noautorotate', '-noautorotate',
`-hwaccel_device ${this.device}`,
...this.getInputThreadOptions(), ...this.getInputThreadOptions(),
]; ];
const hwDevice = this.getPreferredHardwareDevice();
if (hwDevice) {
options.push(`-hwaccel_device ${hwDevice}`);
}
return options;
} }
getFilterOptions(videoStream: VideoStreamInfo) { getFilterOptions(videoStream: VideoStreamInfo) {
@ -934,9 +898,6 @@ export class RkmppSwDecodeConfig extends BaseHWConfig {
} }
getBaseInputOptions(): string[] { getBaseInputOptions(): string[] {
if (this.devices.length === 0) {
throw new Error('No RKMPP device found');
}
return []; return [];
} }
@ -987,10 +948,6 @@ export class RkmppHwDecodeConfig extends RkmppSwDecodeConfig {
} }
getBaseInputOptions() { getBaseInputOptions() {
if (this.devices.length === 0) {
throw new Error('No RKMPP device found');
}
return ['-hwaccel rkmpp', '-hwaccel_output_format drm_prime', '-afbc rga', '-noautorotate']; return ['-hwaccel rkmpp', '-hwaccel_output_format drm_prime', '-afbc rga', '-noautorotate'];
} }