FFmpeg + cpp, How to get list of my GPUs handling h264_nvenc?

1.6k Views Asked by At

I'm writing app to use on PCs with more than one GPU, I'm trying to get a list of GPU indexes that can decode stream in h264 to assign all new video source equally between all available GPU.

I've fount how to do it in command prompt but i need to write line belong it in c++

ffmpeg -vsync 0 -i input.mp4 -c:v h264_nvenc -gpu list -f null –

I need it to dynamically pass it to av_hwdevice_ctx_create(AVBufferRef**,char *int)

Does anyone know how to do this?

1

There are 1 best solutions below

2
On

As far as I know you don't need to pass the device explicitly to

av_hwdevice_ctx_create  (   
        AVBufferRef **  device_ctx,
        enum AVHWDeviceType     type,
        const char *    device,
        AVDictionary *  opts,
        int     flags 
)   

the device_ctx can be NULL, because it gets created here. You only need to know the type you desire. e.g. AV_HWDEVICE_TYPE_CUDA . The remaining parameters can be NULL or 0. At least that's how it's done in the hw_decode example:

static AVBufferRef *hw_device_ctx = NULL;
//...
static int hw_decoder_init(AVCodecContext *ctx, const enum AVHWDeviceType type)
{
    int err = 0;
    if ((err = av_hwdevice_ctx_create(&hw_device_ctx, type,
                                  NULL, NULL, 0)) < 0) {
        fprintf(stderr, "Failed to create specified HW device.\n");
        return err;
    }
    ctx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
    return err;
}

(Note: I haven't used the function myself. I'm just basing my answer on the how it's done in the example.)