How can I specify a GPU using ffmpeg API?

Viewed 228

As I know, it can use -hwaccel_device or -gpu to specify a GPU device in command line, but how can I specify a GPU while using API?

I have tryed to find out it in ffmpeg.c while that is too complicated for a new guy to c++ and ffmpeg.

Can I use AVDictionary? What is Keyword?

it is much appreciate if someone can give some hints!

1 Answers

I finally solved this problem by reading source code of OpenCV. The key API is

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

Source code in cap_ffmpeg_hw.hpp of OpenCV :

char device[128] = "";
char* pdevice = NULL;
if (hw_device >= 0 && hw_device < 100000) {
    if (child_type == AV_HWDEVICE_TYPE_VAAPI) {
       snprintf(device, sizeof(device), "/dev/dri/renderD%d", 128 + hw_device);
    }else {
       snprintf(device, sizeof(device), "%d", hw_device);
    }
     pdevice = device;
}
const char* hw_child_name = av_hwdevice_get_type_name(child_type);const char* device_name = pdevice ? pdevice : "'default'";
int err = av_hwdevice_ctx_create(&hw_device_ctx, child_type, pdevice, NULL, 0);
Related