@@ -122,14 +122,50 @@ class CLoomIoMediaDecoder {
122
122
123
123
static enum AVPixelFormat hwPixelFormat;
124
124
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)
126
149
{
127
150
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 ) {
131
166
return err;
132
167
}
168
+ printf (" VAAPI device created for device %s and stream %d\n " , device_name, hw_device_id);
133
169
ctx->hw_device_ctx = av_buffer_ref (hw_device_ctx);
134
170
135
171
return err;
@@ -377,7 +413,7 @@ vx_status CLoomIoMediaDecoder::Initialize()
377
413
vxAddLogEntry ((vx_reference)node, status, " ERROR: vaapi is not supported for this device\n " );
378
414
return status;
379
415
}
380
- printf (" Found vaapi device for %d\n " , mediaIndex);
416
+ // printf("Found vaapi device for %d\n", mediaIndex);
381
417
}
382
418
int err = avformat_open_input (&formatContext, mediaFileName, inputFormat, nullptr );
383
419
if (err) {
@@ -451,7 +487,7 @@ vx_status CLoomIoMediaDecoder::Initialize()
451
487
return -1 ;
452
488
if (useVaapi[mediaIndex]) {
453
489
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 ) {
455
491
vxAddLogEntry ((vx_reference)node, VX_FAILURE, " ERROR: Failed to create specified HW device.\n " );
456
492
return VX_FAILURE;
457
493
}
0 commit comments