19 #define CL_USE_DEPRECATED_OPENCL_1_2_APIS
34 #if HAVE_OPENCL_VAAPI_BEIGNET
37 #include <va/va_drmcommon.h>
38 #include <CL/cl_intel.h>
42 #if HAVE_OPENCL_DRM_BEIGNET
44 #include <CL/cl_intel.h>
48 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
50 #include <mfx/mfxstructures.h>
53 #include <CL/cl_va_api_media_sharing_intel.h>
59 #include <CL/cl_dx9_media_sharing.h>
65 #include <CL/cl_d3d11.h>
69 #if HAVE_OPENCL_DRM_ARM
70 #include <CL/cl_ext.h>
71 #include <drm_fourcc.h>
75 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA && CONFIG_LIBMFX
93 #if HAVE_OPENCL_DRM_BEIGNET
94 int beignet_drm_mapping_usable;
95 clCreateImageFromFdINTEL_fn clCreateImageFromFdINTEL;
98 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
99 int qsv_mapping_usable;
100 clCreateFromVA_APIMediaSurfaceINTEL_fn
101 clCreateFromVA_APIMediaSurfaceINTEL;
102 clEnqueueAcquireVA_APIMediaSurfacesINTEL_fn
103 clEnqueueAcquireVA_APIMediaSurfacesINTEL;
104 clEnqueueReleaseVA_APIMediaSurfacesINTEL_fn
105 clEnqueueReleaseVA_APIMediaSurfacesINTEL;
108 #if HAVE_OPENCL_DXVA2
109 int dxva2_mapping_usable;
110 cl_dx9_media_adapter_type_khr dx9_media_adapter_type;
112 clCreateFromDX9MediaSurfaceKHR_fn
113 clCreateFromDX9MediaSurfaceKHR;
114 clEnqueueAcquireDX9MediaSurfacesKHR_fn
115 clEnqueueAcquireDX9MediaSurfacesKHR;
116 clEnqueueReleaseDX9MediaSurfacesKHR_fn
117 clEnqueueReleaseDX9MediaSurfacesKHR;
120 #if HAVE_OPENCL_D3D11
121 int d3d11_mapping_usable;
122 clCreateFromD3D11Texture2DKHR_fn
123 clCreateFromD3D11Texture2DKHR;
124 clEnqueueAcquireD3D11ObjectsKHR_fn
125 clEnqueueAcquireD3D11ObjectsKHR;
126 clEnqueueReleaseD3D11ObjectsKHR_fn
127 clEnqueueReleaseD3D11ObjectsKHR;
130 #if HAVE_OPENCL_DRM_ARM
131 int drm_arm_mapping_usable;
142 #if HAVE_OPENCL_DXVA2 || HAVE_OPENCL_D3D11
146 int nb_mapped_frames;
153 const void *private_info,
166 cle = clReleaseContext(hwctx->
context);
167 if (cle != CL_SUCCESS) {
169 "context: %d.\n", cle);
177 {
"platform_profile", CL_PLATFORM_PROFILE },
178 {
"platform_version", CL_PLATFORM_VERSION },
179 {
"platform_name", CL_PLATFORM_NAME },
180 {
"platform_vendor", CL_PLATFORM_VENDOR },
181 {
"platform_extensions", CL_PLATFORM_EXTENSIONS },
188 {
"device_name", CL_DEVICE_NAME },
189 {
"device_vendor", CL_DEVICE_VENDOR },
190 {
"driver_version", CL_DRIVER_VERSION },
191 {
"device_version", CL_DEVICE_VERSION },
192 {
"device_profile", CL_DEVICE_PROFILE },
193 {
"device_extensions", CL_DEVICE_EXTENSIONS },
200 {
"cpu", CL_DEVICE_TYPE_CPU },
201 {
"gpu", CL_DEVICE_TYPE_GPU },
202 {
"accelerator", CL_DEVICE_TYPE_ACCELERATOR },
203 {
"custom", CL_DEVICE_TYPE_CUSTOM },
204 {
"default", CL_DEVICE_TYPE_DEFAULT },
205 {
"all", CL_DEVICE_TYPE_ALL },
209 cl_platform_info
key)
214 cle = clGetPlatformInfo(platform_id,
key, 0,
NULL, &
size);
215 if (cle != CL_SUCCESS)
221 if (cle != CL_SUCCESS) {
235 cle = clGetDeviceInfo(device_id,
key, 0,
NULL, &
size);
236 if (cle != CL_SUCCESS)
242 if (cle != CL_SUCCESS) {
256 CL_PLATFORM_EXTENSIONS);
269 CL_DEVICE_EXTENSIONS);
284 "%s found as platform extension.\n",
name);
290 "%s found as device extension.\n",
name);
298 cl_uint *nb_platforms,
299 cl_platform_id **platforms,
304 cle = clGetPlatformIDs(0,
NULL, nb_platforms);
305 if (cle != CL_SUCCESS) {
307 "OpenCL platforms: %d.\n", cle);
317 cle = clGetPlatformIDs(*nb_platforms, *platforms,
NULL);
318 if (cle != CL_SUCCESS) {
320 "platforms: %d.\n", cle);
329 cl_platform_id platform_id,
330 const char *platform_name,
348 "of platform \"%s\".\n",
364 cl_platform_id platform_id,
365 const char *platform_name,
367 cl_device_id **devices,
372 cle = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL,
373 0,
NULL, nb_devices);
374 if (cle == CL_DEVICE_NOT_FOUND) {
376 "on platform \"%s\".\n", platform_name);
379 }
else if (cle != CL_SUCCESS) {
381 "on platform \"%s\": %d.\n", platform_name, cle);
385 "platform \"%s\".\n", *nb_devices, platform_name);
391 cle = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL,
392 *nb_devices, *devices,
NULL);
393 if (cle != CL_SUCCESS) {
395 "on platform \"%s\": %d.\n", platform_name, cle);
404 cl_device_id device_id,
405 const char *device_name,
415 cl_device_type match_type = 0, device_type;
430 cle = clGetDeviceInfo(device_id, CL_DEVICE_TYPE,
431 sizeof(device_type), &device_type,
NULL);
432 if (cle != CL_SUCCESS) {
434 "of device \"%s\".\n", device_name);
438 if (!(device_type & match_type)) {
454 "of device \"%s\".\n",
474 cl_uint *nb_platforms,
475 cl_platform_id **platforms,
478 cl_platform_id platform_id,
479 const char *platform_name,
482 cl_platform_id platform_id,
483 const char *platform_name,
485 cl_device_id **devices,
488 cl_device_id device_id,
489 const char *device_name,
495 cl_context_properties *props)
497 cl_uint nb_platforms;
498 cl_platform_id *platforms =
NULL;
499 cl_platform_id platform_id;
501 cl_device_id *devices =
NULL;
504 cl_context_properties default_props[3];
505 char *platform_name_src =
NULL,
506 *device_name_src =
NULL;
507 int err, found, p,
d;
518 for (p = 0; p < nb_platforms; p++) {
519 const char *platform_name;
528 if (platform_name_src)
529 platform_name = platform_name_src;
531 platform_name =
"Unknown Platform";
544 &nb_devices, &devices,
549 for (
d = 0;
d < nb_devices;
d++) {
550 const char *device_name;
560 device_name = device_name_src;
562 device_name =
"Unknown Device";
575 platform_name, device_name);
578 platform_id = platforms[p];
597 props = default_props;
598 default_props[0] = CL_CONTEXT_PLATFORM;
599 default_props[1] = (intptr_t)platform_id;
600 default_props[2] = 0;
602 if (props[0] == CL_CONTEXT_PLATFORM && props[1] == 0)
603 props[1] = (intptr_t)platform_id;
637 if (device && device[0]) {
639 int d = -1, p = -1,
ret;
640 if (device[0] ==
'.')
641 ret = sscanf(device,
".%d", &
d);
643 ret = sscanf(device,
"%d.%d", &p, &
d);
646 "index specification \"%s\".\n", device);
667 if (cle != CL_SUCCESS) {
669 "command queue: %d.\n", cle);
679 "command queue: %d.\n", cle);
684 cle = clGetDeviceInfo(hwctx->
device_id, CL_DEVICE_PLATFORM,
687 if (cle != CL_SUCCESS) {
689 "platform containing the device.\n");
693 #define CL_FUNC(name, desc) do { \
696 priv->name = clGetExtensionFunctionAddressForPlatform( \
697 priv->platform_id, #name); \
699 av_log(hwdev, AV_LOG_VERBOSE, \
700 desc " function not found (%s).\n", #name); \
703 av_log(hwdev, AV_LOG_VERBOSE, \
704 desc " function found (%s).\n", #name); \
708 #if HAVE_OPENCL_DRM_BEIGNET
712 CL_FUNC(clCreateImageFromFdINTEL,
713 "Beignet DRM to OpenCL image mapping");
717 "mapping not usable.\n");
718 priv->beignet_drm_mapping_usable = 0;
720 priv->beignet_drm_mapping_usable = 1;
725 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
728 cl_context_properties *props =
NULL;
729 VADisplay va_display;
730 const char *va_ext =
"cl_intel_va_api_media_sharing";
735 "required for QSV to OpenCL mapping.\n", va_ext);
739 cle = clGetContextInfo(hwctx->
context, CL_CONTEXT_PROPERTIES,
740 0,
NULL, &props_size);
741 if (cle != CL_SUCCESS) {
743 "properties: %d.\n", cle);
746 if (props_size == 0) {
748 "enabled on context creation to use QSV to "
749 "OpenCL mapping.\n");
757 cle = clGetContextInfo(hwctx->
context, CL_CONTEXT_PROPERTIES,
758 props_size, props,
NULL);
759 if (cle != CL_SUCCESS) {
761 "properties: %d.\n", cle);
766 for (
i = 0;
i < (props_size /
sizeof(*props) - 1);
i++) {
767 if (props[
i] == CL_CONTEXT_VA_API_DISPLAY_INTEL) {
768 va_display = (VADisplay)(intptr_t)props[
i+1];
774 "enabled on context creation to use QSV to "
775 "OpenCL mapping.\n");
778 if (!vaDisplayIsValid(va_display)) {
780 "required on context creation to use QSV to "
781 "OpenCL mapping.\n");
785 CL_FUNC(clCreateFromVA_APIMediaSurfaceINTEL,
786 "Intel QSV to OpenCL mapping");
787 CL_FUNC(clEnqueueAcquireVA_APIMediaSurfacesINTEL,
788 "Intel QSV in OpenCL acquire");
789 CL_FUNC(clEnqueueReleaseVA_APIMediaSurfacesINTEL,
790 "Intel QSV in OpenCL release");
796 priv->qsv_mapping_usable = 0;
798 priv->qsv_mapping_usable = 1;
804 #if HAVE_OPENCL_DXVA2
808 CL_FUNC(clCreateFromDX9MediaSurfaceKHR,
809 "DXVA2 to OpenCL mapping");
810 CL_FUNC(clEnqueueAcquireDX9MediaSurfacesKHR,
811 "DXVA2 in OpenCL acquire");
812 CL_FUNC(clEnqueueReleaseDX9MediaSurfacesKHR,
813 "DXVA2 in OpenCL release");
818 priv->dxva2_mapping_usable = 0;
820 priv->dx9_media_adapter_type = CL_ADAPTER_D3D9EX_KHR;
821 priv->dxva2_mapping_usable = 1;
826 #if HAVE_OPENCL_D3D11
828 const char *d3d11_ext =
"cl_khr_d3d11_sharing";
829 const char *nv12_ext =
"cl_intel_d3d11_nv12_media_sharing";
834 "required for D3D11 to OpenCL mapping.\n", d3d11_ext);
838 "required for D3D11 to OpenCL mapping.\n", nv12_ext);
842 CL_FUNC(clCreateFromD3D11Texture2DKHR,
843 "D3D11 to OpenCL mapping");
844 CL_FUNC(clEnqueueAcquireD3D11ObjectsKHR,
845 "D3D11 in OpenCL acquire");
846 CL_FUNC(clEnqueueReleaseD3D11ObjectsKHR,
847 "D3D11 in OpenCL release");
852 priv->d3d11_mapping_usable = 0;
854 priv->d3d11_mapping_usable = 1;
859 #if HAVE_OPENCL_DRM_ARM
861 const char *drm_arm_ext =
"cl_arm_import_memory";
862 const char *image_ext =
"cl_khr_image2d_from_buffer";
867 "required for DRM to OpenCL mapping on ARM.\n",
873 "required for DRM to OpenCL mapping on ARM.\n",
883 priv->drm_arm_mapping_usable = 0;
885 priv->drm_arm_mapping_usable = 1;
902 if (cle != CL_SUCCESS) {
904 "command queue reference: %d.\n", cle);
910 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
912 cl_platform_id platform_id,
913 const char *platform_name,
919 if (!clGetExtensionFunctionAddressForPlatform(platform_id,
920 "clGetDeviceIDsFromVA_APIMediaAdapterINTEL")) {
922 "VAAPI device enumeration function.\n", platform_name);
930 cl_platform_id platform_id,
931 const char *platform_name,
933 cl_device_id **devices,
936 VADisplay va_display =
context;
937 clGetDeviceIDsFromVA_APIMediaAdapterINTEL_fn
938 clGetDeviceIDsFromVA_APIMediaAdapterINTEL;
941 clGetDeviceIDsFromVA_APIMediaAdapterINTEL =
942 clGetExtensionFunctionAddressForPlatform(platform_id,
943 "clGetDeviceIDsFromVA_APIMediaAdapterINTEL");
944 if (!clGetDeviceIDsFromVA_APIMediaAdapterINTEL) {
946 "clGetDeviceIDsFromVA_APIMediaAdapterINTEL().\n");
950 cle = clGetDeviceIDsFromVA_APIMediaAdapterINTEL(
951 platform_id, CL_VA_API_DISPLAY_INTEL, va_display,
952 CL_PREFERRED_DEVICES_FOR_VA_API_INTEL, 0,
NULL, nb_devices);
953 if (cle == CL_DEVICE_NOT_FOUND) {
955 "on platform \"%s\".\n", platform_name);
958 }
else if (cle != CL_SUCCESS) {
960 "on platform \"%s\": %d.\n", platform_name, cle);
968 cle = clGetDeviceIDsFromVA_APIMediaAdapterINTEL(
969 platform_id, CL_VA_API_DISPLAY_INTEL, va_display,
970 CL_PREFERRED_DEVICES_FOR_VA_API_INTEL, *nb_devices, *devices,
NULL);
971 if (cle != CL_SUCCESS) {
973 "devices on platform \"%s\": %d.\n", platform_name, cle);
982 cl_device_id device_id,
983 const char *device_name,
986 const char *va_ext =
"cl_intel_va_api_media_sharing";
992 "%s extension.\n", device_name, va_ext);
998 #if HAVE_OPENCL_DXVA2
1000 cl_platform_id platform_id,
1001 const char *platform_name,
1004 const char *dx9_ext =
"cl_khr_dx9_media_sharing";
1010 "%s extension.\n", platform_name, dx9_ext);
1016 cl_platform_id platform_id,
1017 const char *platform_name,
1018 cl_uint *nb_devices,
1019 cl_device_id **devices,
1022 IDirect3DDevice9 *device =
context;
1023 clGetDeviceIDsFromDX9MediaAdapterKHR_fn
1024 clGetDeviceIDsFromDX9MediaAdapterKHR;
1025 cl_dx9_media_adapter_type_khr media_adapter_type = CL_ADAPTER_D3D9EX_KHR;
1028 clGetDeviceIDsFromDX9MediaAdapterKHR =
1029 clGetExtensionFunctionAddressForPlatform(platform_id,
1030 "clGetDeviceIDsFromDX9MediaAdapterKHR");
1031 if (!clGetDeviceIDsFromDX9MediaAdapterKHR) {
1033 "clGetDeviceIDsFromDX9MediaAdapterKHR().\n");
1037 cle = clGetDeviceIDsFromDX9MediaAdapterKHR(
1038 platform_id, 1, &media_adapter_type, (
void**)&device,
1039 CL_PREFERRED_DEVICES_FOR_DX9_MEDIA_ADAPTER_KHR,
1040 0,
NULL, nb_devices);
1041 if (cle == CL_DEVICE_NOT_FOUND) {
1043 "on platform \"%s\".\n", platform_name);
1046 }
else if (cle != CL_SUCCESS) {
1048 "on platform \"%s\": %d.\n", platform_name, cle);
1056 cle = clGetDeviceIDsFromDX9MediaAdapterKHR(
1057 platform_id, 1, &media_adapter_type, (
void**)&device,
1058 CL_PREFERRED_DEVICES_FOR_DX9_MEDIA_ADAPTER_KHR,
1059 *nb_devices, *devices,
NULL);
1060 if (cle != CL_SUCCESS) {
1062 "devices on platform \"%s\": %d.\n", platform_name, cle);
1071 #if HAVE_OPENCL_D3D11
1073 cl_platform_id platform_id,
1074 const char *platform_name,
1077 const char *d3d11_ext =
"cl_khr_d3d11_sharing";
1083 "%s extension.\n", platform_name, d3d11_ext);
1089 cl_platform_id platform_id,
1090 const char *platform_name,
1091 cl_uint *nb_devices,
1092 cl_device_id **devices,
1096 clGetDeviceIDsFromD3D11KHR_fn clGetDeviceIDsFromD3D11KHR;
1099 clGetDeviceIDsFromD3D11KHR =
1100 clGetExtensionFunctionAddressForPlatform(platform_id,
1101 "clGetDeviceIDsFromD3D11KHR");
1102 if (!clGetDeviceIDsFromD3D11KHR) {
1104 "clGetDeviceIDsFromD3D11KHR().\n");
1108 cle = clGetDeviceIDsFromD3D11KHR(platform_id,
1109 CL_D3D11_DEVICE_KHR, device,
1110 CL_PREFERRED_DEVICES_FOR_D3D11_KHR,
1111 0,
NULL, nb_devices);
1112 if (cle == CL_DEVICE_NOT_FOUND) {
1114 "on platform \"%s\".\n", platform_name);
1117 }
else if (cle != CL_SUCCESS) {
1119 "on platform \"%s\": %d.\n", platform_name, cle);
1127 cle = clGetDeviceIDsFromD3D11KHR(platform_id,
1128 CL_D3D11_DEVICE_KHR, device,
1129 CL_PREFERRED_DEVICES_FOR_D3D11_KHR,
1130 *nb_devices, *devices,
NULL);
1131 if (cle != CL_SUCCESS) {
1133 "devices on platform \"%s\": %d.\n", platform_name, cle);
1142 #if HAVE_OPENCL_DXVA2 || HAVE_OPENCL_D3D11
1144 cl_device_id device_id,
1145 const char *device_name,
1148 cl_device_type device_type;
1151 cle = clGetDeviceInfo(device_id, CL_DEVICE_TYPE,
1152 sizeof(device_type), &device_type,
NULL);
1153 if (cle != CL_SUCCESS) {
1155 "of device \"%s\".\n", device_name);
1158 if (!(device_type & CL_DEVICE_TYPE_GPU)) {
1168 #if HAVE_OPENCL_DRM_ARM
1170 cl_platform_id platform_id,
1171 const char *platform_name,
1174 const char *drm_arm_ext =
"cl_arm_import_memory";
1180 "%s extension.\n", platform_name, drm_arm_ext);
1186 cl_device_id device_id,
1187 const char *device_name,
1190 const char *drm_arm_ext =
"cl_arm_import_memory";
1196 "%s extension.\n", device_name, drm_arm_ext);
1207 switch (src_ctx->
type) {
1209 #if HAVE_OPENCL_DRM_BEIGNET
1218 err =
av_dict_set(&selector_opts,
"platform_vendor",
"Intel", 0);
1220 err =
av_dict_set(&selector_opts,
"platform_version",
"beignet", 0);
1225 .context = selector_opts,
1229 .filter_device =
NULL,
1238 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
1245 cl_context_properties props[7] = {
1246 CL_CONTEXT_PLATFORM,
1248 CL_CONTEXT_VA_API_DISPLAY_INTEL,
1250 CL_CONTEXT_INTEROP_USER_SYNC,
1257 .context = src_hwctx->
display,
1259 .filter_platform = &opencl_filter_intel_media_vaapi_platform,
1260 .enumerate_devices = &opencl_enumerate_intel_media_vaapi_devices,
1261 .filter_device = &opencl_filter_intel_media_vaapi_device,
1269 #if HAVE_OPENCL_DXVA2
1273 IDirect3DDevice9 *device;
1274 HANDLE device_handle;
1277 hr = IDirect3DDeviceManager9_OpenDeviceHandle(src_hwctx->
devmgr,
1281 "for Direct3D9 device: %lx.\n", (
unsigned long)hr);
1286 hr = IDirect3DDeviceManager9_LockDevice(src_hwctx->
devmgr,
1289 if (SUCCEEDED(hr)) {
1290 cl_context_properties props[5] = {
1291 CL_CONTEXT_PLATFORM,
1293 CL_CONTEXT_ADAPTER_D3D9EX_KHR,
1302 .filter_platform = &opencl_filter_dxva2_platform,
1303 .enumerate_devices = &opencl_enumerate_dxva2_devices,
1304 .filter_device = &opencl_filter_gpu_device,
1309 IDirect3DDeviceManager9_UnlockDevice(src_hwctx->
devmgr,
1310 device_handle, FALSE);
1313 "for Direct3D9 device: %lx.\n", (
unsigned long)hr);
1317 IDirect3DDeviceManager9_CloseDeviceHandle(src_hwctx->
devmgr,
1323 #if HAVE_OPENCL_D3D11
1327 cl_context_properties props[5] = {
1328 CL_CONTEXT_PLATFORM,
1330 CL_CONTEXT_D3D11_DEVICE_KHR,
1331 (intptr_t)src_hwctx->
device,
1337 .context = src_hwctx->
device,
1339 .filter_platform = &opencl_filter_d3d11_platform,
1340 .enumerate_devices = &opencl_enumerate_d3d11_devices,
1341 .filter_device = &opencl_filter_gpu_device,
1349 #if HAVE_OPENCL_DRM_ARM
1357 .filter_platform = &opencl_filter_drm_arm_platform,
1359 .filter_device = &opencl_filter_drm_arm_device,
1377 cl_image_format *image_format,
1378 cl_image_desc *image_desc)
1397 wsub = 1 <<
desc->log2_chroma_w;
1403 for (
c = 0;
c <
desc->nb_components;
c++) {
1405 if (
comp->plane != plane)
1408 if (
comp->step != 1 &&
comp->step != 2 &&
1409 comp->step != 4 &&
comp->step != 8)
1413 if (
comp->shift +
comp->depth != 8 &&
1418 if (depth &&
comp->depth != depth)
1422 if (
comp->depth > 8 &&
1430 depth =
comp->depth;
1431 order = order * 10 +
comp->offset / ((depth + 7) / 8) + 1;
1434 c ==
desc->nb_components - 1);
1440 memset(image_format, 0,
sizeof(*image_format));
1441 memset(image_desc, 0,
sizeof(*image_desc));
1442 image_desc->image_type = CL_MEM_OBJECT_IMAGE2D;
1444 if (plane == 0 ||
alpha) {
1445 image_desc->image_width =
width;
1446 image_desc->image_height =
height;
1447 image_desc->image_row_pitch =
step *
width;
1449 image_desc->image_width =
width / wsub;
1451 image_desc->image_row_pitch =
step *
width / wsub;
1455 image_format->image_channel_data_type = CL_UNORM_INT8;
1458 image_format->image_channel_data_type = CL_UNORM_INT16;
1459 else if (depth == 32)
1460 image_format->image_channel_data_type = CL_FLOAT;
1465 #define CHANNEL_ORDER(order, type) \
1466 case order: image_format->image_channel_order = type; break;
1479 #undef CHANNEL_ORDER
1485 const void *hwconfig,
1489 cl_uint nb_image_formats;
1490 cl_image_format *image_formats =
NULL;
1493 int err, pix_fmts_found;
1494 size_t max_width, max_height;
1496 cle = clGetDeviceInfo(hwctx->
device_id, CL_DEVICE_IMAGE2D_MAX_WIDTH,
1497 sizeof(max_width), &max_width,
NULL);
1498 if (cle != CL_SUCCESS) {
1500 "supported image width: %d.\n", cle);
1504 cle = clGetDeviceInfo(hwctx->
device_id, CL_DEVICE_IMAGE2D_MAX_HEIGHT,
1505 sizeof(max_height), &max_height,
NULL);
1506 if (cle != CL_SUCCESS) {
1508 "supported image height: %d.\n", cle);
1515 cle = clGetSupportedImageFormats(hwctx->
context,
1517 CL_MEM_OBJECT_IMAGE2D,
1518 0,
NULL, &nb_image_formats);
1519 if (cle != CL_SUCCESS) {
1521 "image formats: %d.\n", cle);
1525 if (nb_image_formats == 0) {
1527 "driver (zero supported image formats).\n");
1534 if (!image_formats) {
1539 cle = clGetSupportedImageFormats(hwctx->
context,
1541 CL_MEM_OBJECT_IMAGE2D,
1543 image_formats,
NULL);
1544 if (cle != CL_SUCCESS) {
1546 "image formats: %d.\n", cle);
1553 cl_image_format image_format;
1554 cl_image_desc image_desc;
1557 for (plane = 0;; plane++) {
1564 for (
i = 0;
i < nb_image_formats;
i++) {
1565 if (image_formats[
i].image_channel_order ==
1566 image_format.image_channel_order &&
1567 image_formats[
i].image_channel_data_type ==
1568 image_format.image_channel_data_type)
1571 if (
i == nb_image_formats) {
1618 for (p = 0; p <
desc->nb_planes; p++) {
1619 cle = clReleaseMemObject(
desc->planes[p]);
1620 if (cle != CL_SUCCESS) {
1636 cl_image_format image_format;
1637 cl_image_desc image_desc;
1648 &image_format, &image_desc);
1656 image_desc.image_row_pitch = 0;
1658 image = clCreateImage(hwctx->
context, CL_MEM_READ_WRITE,
1659 &image_format, &image_desc,
NULL, &cle);
1662 "plane %d: %d.\n", p, cle);
1666 desc->planes[p] = image;
1669 desc->nb_planes = p;
1679 for (p = 0;
desc->planes[p]; p++)
1680 clReleaseMemObject(
desc->planes[p]);
1695 if (cle != CL_SUCCESS) {
1697 "command queue: %d.\n", cle);
1722 #if HAVE_OPENCL_DXVA2 || HAVE_OPENCL_D3D11
1724 for (
i = 0;
i < priv->nb_mapped_frames;
i++) {
1726 for (p = 0; p <
desc->nb_planes; p++) {
1727 cle = clReleaseMemObject(
desc->planes[p]);
1728 if (cle != CL_SUCCESS) {
1730 "frame object (frame %d plane %d): %d.\n",
1740 if (cle != CL_SUCCESS) {
1742 "command queue: %d.\n", cle);
1759 for (p = 0; p <
desc->nb_planes; p++)
1760 frame->data[p] = (uint8_t*)
desc->planes[p];
1787 cl_event *events,
int nb_events)
1792 cle = clWaitForEvents(nb_events, events);
1793 if (cle != CL_SUCCESS) {
1795 "completion: %d.\n", cle);
1799 for (
i = 0;
i < nb_events;
i++) {
1800 cle = clReleaseEvent(events[
i]);
1801 if (cle != CL_SUCCESS) {
1803 "event: %d.\n", cle);
1814 cl_image_format image_format;
1815 cl_image_desc image_desc;
1817 size_t origin[3] = { 0, 0, 0 };
1828 &image_format, &image_desc);
1835 if (!dst->
data[p]) {
1837 "destination frame for transfer.\n", p);
1842 region[0] = image_desc.image_width;
1843 region[1] = image_desc.image_height;
1847 (cl_mem)
src->data[p],
1848 CL_FALSE, origin, region,
1851 0,
NULL, &events[p]);
1852 if (cle != CL_SUCCESS) {
1854 "OpenCL image plane %d: %d.\n", p, cle);
1869 cl_image_format image_format;
1870 cl_image_desc image_desc;
1872 size_t origin[3] = { 0, 0, 0 };
1883 &image_format, &image_desc);
1890 if (!
src->data[p]) {
1892 "source frame for transfer.\n", p);
1897 region[0] = image_desc.image_width;
1898 region[1] = image_desc.image_height;
1902 (cl_mem)dst->
data[p],
1903 CL_FALSE, origin, region,
1904 src->linesize[p], 0,
1906 0,
NULL, &events[p]);
1907 if (cle != CL_SUCCESS) {
1909 "OpenCL image plane %d: %d.\n", p, cle);
1937 if (!
map->address[p])
1943 0,
NULL, &events[e]);
1944 if (cle != CL_SUCCESS) {
1946 "image plane %d: %d.\n", p, cle);
1960 cl_map_flags map_flags;
1961 cl_image_format image_format;
1962 cl_image_desc image_desc;
1965 size_t origin[3] = { 0, 0, 0 };
1977 map_flags = CL_MAP_WRITE_INVALIDATE_REGION;
1981 map_flags |= CL_MAP_READ;
1983 map_flags |= CL_MAP_WRITE;
1993 &image_format, &image_desc);
1999 region[0] = image_desc.image_width;
2000 region[1] = image_desc.image_height;
2005 (cl_mem)
src->data[p],
2006 CL_FALSE, map_flags, origin, region,
2009 if (!
map->address[p]) {
2011 "image plane %d: %d.\n", p, cle);
2016 dst->
data[p] =
map->address[p];
2019 p,
src->data[p], dst->
data[p]);
2038 if (!
map->address[p])
2041 (cl_mem)
src->data[p],
2043 0,
NULL, &events[p]);
2051 #if HAVE_OPENCL_DRM_BEIGNET
2053 typedef struct DRMBeignetToOpenCLMapping {
2058 } DRMBeignetToOpenCLMapping;
2063 DRMBeignetToOpenCLMapping *mapping = hwmap->
priv;
2067 for (
i = 0;
i < mapping->frame.nb_planes;
i++) {
2068 cle = clReleaseMemObject(mapping->frame.planes[
i]);
2069 if (cle != CL_SUCCESS) {
2071 "of plane %d of DRM frame: %d.\n",
i, cle);
2084 DRMBeignetToOpenCLMapping *mapping;
2096 for (
i = 0;
i <
desc->nb_layers;
i++) {
2098 for (j = 0; j < layer->
nb_planes; j++) {
2103 cl_import_image_info_intel image_info = {
2105 .size =
object->size,
2106 .type = CL_MEM_OBJECT_IMAGE2D,
2108 .row_pitch = plane->
pitch,
2110 cl_image_desc image_desc;
2118 "plane %d is not representable in OpenCL: %d.\n",
2122 image_info.width = image_desc.image_width;
2123 image_info.height = image_desc.image_height;
2125 mapping->frame.planes[p] =
2126 priv->clCreateImageFromFdINTEL(hwctx->
context,
2128 if (!mapping->frame.planes[p]) {
2130 "from layer %d plane %d of DRM frame: %d.\n",
2136 dst->
data[p] = (uint8_t*)mapping->frame.planes[p];
2137 mapping->frame.nb_planes = ++p;
2142 &opencl_unmap_from_drm_beignet,
2153 for (p = 0; p < mapping->frame.nb_planes; p++) {
2154 if (mapping->frame.planes[p])
2155 clReleaseMemObject(mapping->frame.planes[p]);
2158 memset(dst->
data, 0,
sizeof(dst->
data));
2162 #if HAVE_OPENCL_VAAPI_BEIGNET
2181 err = opencl_map_from_drm_beignet(dst_fc, dst,
tmp,
flags);
2199 return CL_MEM_READ_WRITE;
2201 return CL_MEM_READ_ONLY;
2203 return CL_MEM_WRITE_ONLY;
2208 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
2222 cle = device_priv->clEnqueueReleaseVA_APIMediaSurfacesINTEL(
2225 if (cle != CL_SUCCESS) {
2227 "handles: %d.\n", cle);
2232 for (p = 0; p <
desc->nb_planes; p++) {
2233 cle = clReleaseMemObject(
desc->planes[p]);
2234 if (cle != CL_SUCCESS) {
2236 "image of plane %d of QSV/VAAPI surface: %d\n",
2253 VASurfaceID va_surface;
2254 cl_mem_flags cl_flags;
2262 mfxFrameSurface1 *mfx_surface = (mfxFrameSurface1*)
src->data[3];
2268 va_surface = *(VASurfaceID *)base_handle;
2272 va_surface = (VASurfaceID)(uintptr_t)
src->data[3];
2282 "OpenCL.\n", va_surface);
2290 desc->nb_planes = 2;
2292 for (p = 0; p <
desc->nb_planes; p++) {
2294 device_priv->clCreateFromVA_APIMediaSurfaceINTEL(
2295 dst_dev->context, cl_flags, &va_surface, p, &cle);
2296 if (!
desc->planes[p]) {
2298 "image from plane %d of QSV/VAAPI surface "
2299 "%#x: %d.\n", p, va_surface, cle);
2304 dst->
data[p] = (uint8_t*)
desc->planes[p];
2307 cle = device_priv->clEnqueueAcquireVA_APIMediaSurfacesINTEL(
2310 if (cle != CL_SUCCESS) {
2312 "handles: %d.\n", cle);
2322 &opencl_unmap_from_qsv,
desc);
2332 for (p = 0; p <
desc->nb_planes; p++)
2333 if (
desc->planes[p])
2334 clReleaseMemObject(
desc->planes[p]);
2336 memset(dst->
data, 0,
sizeof(dst->
data));
2342 #if HAVE_OPENCL_DXVA2
2355 cle = device_priv->clEnqueueReleaseDX9MediaSurfacesKHR(
2358 if (cle != CL_SUCCESS) {
2360 "handle: %d.\n", cle);
2381 "OpenCL.\n",
src->data[3]);
2383 for (
i = 0;
i < src_hwctx->nb_surfaces;
i++) {
2384 if (src_hwctx->surfaces[
i] == (IDirect3DSurface9*)
src->data[3])
2387 if (
i >= src_hwctx->nb_surfaces) {
2389 "is not in the mapped frames context.\n");
2393 desc = &frames_priv->mapped_frames[
i];
2395 cle = device_priv->clEnqueueAcquireDX9MediaSurfacesKHR(
2398 if (cle != CL_SUCCESS) {
2400 "handle: %d.\n", cle);
2408 for (
i = 0;
i <
desc->nb_planes;
i++)
2412 &opencl_unmap_from_dxva2,
desc);
2422 cle = device_priv->clEnqueueReleaseDX9MediaSurfacesKHR(
2425 if (cle == CL_SUCCESS)
2427 memset(dst->
data, 0,
sizeof(dst->
data));
2438 cl_mem_flags cl_flags;
2440 int err,
i, p, nb_planes;
2444 "for DXVA2 to OpenCL mapping.\n");
2451 "for DXVA2 to OpenCL mapping.\n");
2459 frames_priv->nb_mapped_frames = src_hwctx->
nb_surfaces;
2461 frames_priv->mapped_frames =
2462 av_calloc(frames_priv->nb_mapped_frames,
2463 sizeof(*frames_priv->mapped_frames));
2464 if (!frames_priv->mapped_frames)
2467 for (
i = 0;
i < frames_priv->nb_mapped_frames;
i++) {
2469 cl_dx9_surface_info_khr surface_info = {
2471 .shared_handle =
NULL,
2473 desc->nb_planes = nb_planes;
2474 for (p = 0; p < nb_planes; p++) {
2476 device_priv->clCreateFromDX9MediaSurfaceKHR(
2478 device_priv->dx9_media_adapter_type,
2479 &surface_info, p, &cle);
2480 if (!
desc->planes[p]) {
2482 "image from plane %d of DXVA2 surface %d: %d.\n",
2493 for (
i = 0;
i < frames_priv->nb_mapped_frames;
i++) {
2495 for (p = 0; p <
desc->nb_planes; p++) {
2496 if (
desc->planes[p])
2497 clReleaseMemObject(
desc->planes[p]);
2500 av_freep(&frames_priv->mapped_frames);
2501 frames_priv->nb_mapped_frames = 0;
2507 #if HAVE_OPENCL_D3D11
2518 cle = device_priv->clEnqueueReleaseD3D11ObjectsKHR(
2521 if (cle != CL_SUCCESS) {
2523 "handle: %d.\n", cle);
2540 if (
index >= frames_priv->nb_mapped_frames) {
2542 "mapping: %d >= %d.\n",
index, frames_priv->nb_mapped_frames);
2549 desc = &frames_priv->mapped_frames[
index];
2551 cle = device_priv->clEnqueueAcquireD3D11ObjectsKHR(
2554 if (cle != CL_SUCCESS) {
2556 "handle: %d.\n", cle);
2564 for (
i = 0;
i <
desc->nb_planes;
i++)
2568 &opencl_unmap_from_d3d11,
desc);
2578 cle = device_priv->clEnqueueReleaseD3D11ObjectsKHR(
2581 if (cle == CL_SUCCESS)
2583 memset(dst->
data, 0,
sizeof(dst->
data));
2594 cl_mem_flags cl_flags;
2596 int err,
i, p, nb_planes;
2600 "for D3D11 to OpenCL mapping.\n");
2607 "for D3D11 to OpenCL mapping.\n");
2617 frames_priv->mapped_frames =
2618 av_calloc(frames_priv->nb_mapped_frames,
2619 sizeof(*frames_priv->mapped_frames));
2620 if (!frames_priv->mapped_frames)
2623 for (
i = 0;
i < frames_priv->nb_mapped_frames;
i++) {
2625 desc->nb_planes = nb_planes;
2626 for (p = 0; p < nb_planes; p++) {
2627 UINT subresource = 2 *
i + p;
2630 device_priv->clCreateFromD3D11Texture2DKHR(
2633 if (!
desc->planes[p]) {
2635 "image from plane %d of D3D texture "
2636 "index %d (subresource %u): %d.\n",
2637 p,
i, (
unsigned int)subresource, cle);
2647 for (
i = 0;
i < frames_priv->nb_mapped_frames;
i++) {
2649 for (p = 0; p <
desc->nb_planes; p++) {
2650 if (
desc->planes[p])
2651 clReleaseMemObject(
desc->planes[p]);
2654 av_freep(&frames_priv->mapped_frames);
2655 frames_priv->nb_mapped_frames = 0;
2661 #if HAVE_OPENCL_DRM_ARM
2663 typedef struct DRMARMtoOpenCLMapping {
2668 } DRMARMtoOpenCLMapping;
2673 DRMARMtoOpenCLMapping *mapping = hwmap->
priv;
2676 for (
i = 0;
i < mapping->nb_planes;
i++)
2677 clReleaseMemObject(mapping->plane_images[
i]);
2679 for (
i = 0;
i < mapping->nb_objects;
i++)
2680 clReleaseMemObject(mapping->object_buffers[
i]);
2692 DRMARMtoOpenCLMapping *mapping =
NULL;
2693 cl_mem_flags cl_flags;
2694 const cl_import_properties_arm props[3] = {
2695 CL_IMPORT_TYPE_ARM, CL_IMPORT_TYPE_DMA_BUF_ARM, 0,
2710 mapping->nb_objects =
desc->nb_objects;
2711 for (
i = 0;
i <
desc->nb_objects;
i++) {
2712 int fd =
desc->objects[
i].fd;
2716 if (
desc->objects[
i].format_modifier) {
2718 "nonzero format modifier %"PRId64
", result may not "
2719 "be as expected.\n",
i, fd,
2720 desc->objects[
i].format_modifier);
2723 mapping->object_buffers[
i] =
2724 clImportMemoryARM(dst_dev->
context, cl_flags, props,
2725 &fd,
desc->objects[
i].size, &cle);
2726 if (!mapping->object_buffers[
i]) {
2728 "from object %d (fd %d, size %"SIZE_SPECIFIER") of DRM frame: %d.\n",
2729 i, fd,
desc->objects[
i].size, cle);
2735 mapping->nb_planes = 0;
2736 for (
i = 0;
i <
desc->nb_layers;
i++) {
2739 for (j = 0; j < layer->
nb_planes; j++) {
2741 cl_mem plane_buffer;
2742 cl_image_format image_format;
2743 cl_image_desc image_desc;
2744 cl_buffer_region region;
2745 int p = mapping->nb_planes;
2749 &image_format, &image_desc);
2752 "layer %d plane %d): %d.\n", p,
i, j, err);
2756 region.origin = plane->
offset;
2757 region.size = image_desc.image_row_pitch *
2758 image_desc.image_height;
2761 clCreateSubBuffer(mapping->object_buffers[plane->
object_index],
2763 CL_BUFFER_CREATE_TYPE_REGION,
2765 if (!plane_buffer) {
2767 "for plane %d: %d.\n", p, cle);
2772 image_desc.buffer = plane_buffer;
2774 mapping->plane_images[p] =
2775 clCreateImage(dst_dev->
context, cl_flags,
2776 &image_format, &image_desc,
NULL, &cle);
2780 clReleaseMemObject(plane_buffer);
2782 if (!mapping->plane_images[p]) {
2784 "for plane %d: %d.\n", p, cle);
2789 ++mapping->nb_planes;
2793 for (
i = 0;
i < mapping->nb_planes;
i++)
2794 dst->
data[
i] = (uint8_t*)mapping->plane_images[
i];
2797 &opencl_unmap_from_drm_arm, mapping);
2807 for (
i = 0;
i < mapping->nb_planes;
i++) {
2808 clReleaseMemObject(mapping->plane_images[
i]);
2810 for (
i = 0;
i < mapping->nb_objects;
i++) {
2811 if (mapping->object_buffers[
i])
2812 clReleaseMemObject(mapping->object_buffers[
i]);
2815 memset(dst->
data, 0,
sizeof(dst->
data));
2835 switch (
src->format) {
2836 #if HAVE_OPENCL_DRM_BEIGNET
2838 if (priv->beignet_drm_mapping_usable)
2839 return opencl_map_from_drm_beignet(hwfc, dst,
src,
flags);
2841 #if HAVE_OPENCL_VAAPI_BEIGNET
2843 if (priv->beignet_drm_mapping_usable)
2844 return opencl_map_from_vaapi(hwfc, dst,
src,
flags);
2846 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
2849 if (priv->qsv_mapping_usable)
2850 return opencl_map_from_qsv(hwfc, dst,
src,
flags);
2852 #if HAVE_OPENCL_DXVA2
2854 if (priv->dxva2_mapping_usable)
2855 return opencl_map_from_dxva2(hwfc, dst,
src,
flags);
2857 #if HAVE_OPENCL_D3D11
2859 if (priv->d3d11_mapping_usable)
2860 return opencl_map_from_d3d11(hwfc, dst,
src,
flags);
2862 #if HAVE_OPENCL_DRM_ARM
2864 if (priv->drm_arm_mapping_usable)
2865 return opencl_map_from_drm_arm(hwfc, dst,
src,
flags);
2876 #if HAVE_OPENCL_DRM_BEIGNET
2878 if (!priv->beignet_drm_mapping_usable)
2882 #if HAVE_OPENCL_VAAPI_BEIGNET
2884 if (!priv->beignet_drm_mapping_usable)
2888 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
2891 if (!priv->qsv_mapping_usable)
2895 #if HAVE_OPENCL_DXVA2
2897 if (!priv->dxva2_mapping_usable)
2901 err = opencl_frames_derive_from_dxva2(dst_fc, src_fc,
flags);
2907 #if HAVE_OPENCL_D3D11
2909 if (!priv->d3d11_mapping_usable)
2913 err = opencl_frames_derive_from_d3d11(dst_fc, src_fc,
flags);
2919 #if HAVE_OPENCL_DRM_ARM
2921 if (!priv->drm_arm_mapping_usable)