Skip to content

Commit f323a72

Browse files
authored
amd_media - decoder multi-gpu support (#706)
* add multiple device support for vaapi * choose multiple devices only if it is available * check for devices and allow only if it exists for using hwaccel * minor change
1 parent 23a485c commit f323a72

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

amd_openvx_extensions/amd_media/decoder.cpp

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,50 @@ class CLoomIoMediaDecoder {
122122

123123
static enum AVPixelFormat hwPixelFormat;
124124

125-
static int hw_decoder_init(AVCodecContext *ctx, const enum AVHWDeviceType type, AVBufferRef *hw_device_ctx)
125+
static inline bool exists (const char *name) {
126+
if (FILE *file = fopen(name, "r")) {
127+
fclose(file);
128+
return true;
129+
} else {
130+
return false;
131+
}
132+
}
133+
134+
static inline int num_hw_devices() {
135+
char device[128] = "";
136+
int num_hw_devices = 0;
137+
for (int i=0; i<10000; i++){
138+
snprintf(device, sizeof(device), "/dev/dri/renderD%d", 128 + i);
139+
// check if the device file exists in the system: todo:: is there any other way to enumerate the device?
140+
if (exists(device))
141+
num_hw_devices++;
142+
else
143+
break;
144+
}
145+
return num_hw_devices;
146+
}
147+
148+
static int hw_decoder_init(AVCodecContext *ctx, const enum AVHWDeviceType type, AVBufferRef *hw_device_ctx, int hw_device_id)
126149
{
127150
int err = 0;
128-
129-
if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type,
130-
NULL, NULL, 0)) < 0) {
151+
char device[128] = "";
152+
char* pdevice = NULL;
153+
int num_devices = 1; // default;
154+
if (type == AV_HWDEVICE_TYPE_VAAPI)
155+
num_devices = num_hw_devices();
156+
if (hw_device_id >= 0 && hw_device_id < 10000) {
157+
if (type == AV_HWDEVICE_TYPE_VAAPI) {
158+
snprintf(device, sizeof(device), "/dev/dri/renderD%d", (128 + (hw_device_id % num_devices)));
159+
}else {
160+
snprintf(device, sizeof(device), "%d", hw_device_id);
161+
}
162+
pdevice = device;
163+
}
164+
const char* device_name = pdevice? pdevice : NULL;
165+
if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type, device_name, NULL, 0)) < 0) {
131166
return err;
132167
}
168+
printf("VAAPI device created for device %s and stream %d\n", device_name, hw_device_id);
133169
ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
134170

135171
return err;
@@ -377,7 +413,7 @@ vx_status CLoomIoMediaDecoder::Initialize()
377413
vxAddLogEntry((vx_reference)node, status, "ERROR: vaapi is not supported for this device\n");
378414
return status;
379415
}
380-
printf("Found vaapi device for %d\n", mediaIndex);
416+
//printf("Found vaapi device for %d\n", mediaIndex);
381417
}
382418
int err = avformat_open_input(&formatContext, mediaFileName, inputFormat, nullptr);
383419
if (err) {
@@ -451,7 +487,7 @@ vx_status CLoomIoMediaDecoder::Initialize()
451487
return -1;
452488
if (useVaapi[mediaIndex]) {
453489
codecContext->get_format = get_hw_format;
454-
if (hw_decoder_init(codecContext, hw_type, hw_device_ctx) < 0) {
490+
if (hw_decoder_init(codecContext, hw_type, hw_device_ctx, mediaIndex) < 0) {
455491
vxAddLogEntry((vx_reference)node, VX_FAILURE, "ERROR: Failed to create specified HW device.\n");
456492
return VX_FAILURE;
457493
}

0 commit comments

Comments
 (0)