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 <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_VIDEOTOOLBOX
76 #include <OpenCL/cl_gl_ext.h>
77 #include <VideoToolbox/VideoToolbox.h>
80 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA && CONFIG_LIBMFX
103 #if HAVE_OPENCL_DRM_BEIGNET
104 int beignet_drm_mapping_usable;
105 clCreateImageFromFdINTEL_fn clCreateImageFromFdINTEL;
108 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
109 int qsv_mapping_usable;
110 clCreateFromVA_APIMediaSurfaceINTEL_fn
111 clCreateFromVA_APIMediaSurfaceINTEL;
112 clEnqueueAcquireVA_APIMediaSurfacesINTEL_fn
113 clEnqueueAcquireVA_APIMediaSurfacesINTEL;
114 clEnqueueReleaseVA_APIMediaSurfacesINTEL_fn
115 clEnqueueReleaseVA_APIMediaSurfacesINTEL;
118 #if HAVE_OPENCL_DXVA2
119 int dxva2_mapping_usable;
120 cl_dx9_media_adapter_type_khr dx9_media_adapter_type;
122 clCreateFromDX9MediaSurfaceKHR_fn
123 clCreateFromDX9MediaSurfaceKHR;
124 clEnqueueAcquireDX9MediaSurfacesKHR_fn
125 clEnqueueAcquireDX9MediaSurfacesKHR;
126 clEnqueueReleaseDX9MediaSurfacesKHR_fn
127 clEnqueueReleaseDX9MediaSurfacesKHR;
130 #if HAVE_OPENCL_D3D11
131 int d3d11_mapping_usable;
132 clCreateFromD3D11Texture2DKHR_fn
133 clCreateFromD3D11Texture2DKHR;
134 clEnqueueAcquireD3D11ObjectsKHR_fn
135 clEnqueueAcquireD3D11ObjectsKHR;
136 clEnqueueReleaseD3D11ObjectsKHR_fn
137 clEnqueueReleaseD3D11ObjectsKHR;
140 #if HAVE_OPENCL_DRM_ARM
141 int drm_arm_mapping_usable;
157 #if HAVE_OPENCL_DXVA2 || HAVE_OPENCL_D3D11
161 int nb_mapped_frames;
168 const void *private_info,
181 cle = clReleaseContext(hwctx->
context);
182 if (cle != CL_SUCCESS) {
184 "context: %d.\n", cle);
192 {
"platform_profile", CL_PLATFORM_PROFILE },
193 {
"platform_version", CL_PLATFORM_VERSION },
194 {
"platform_name", CL_PLATFORM_NAME },
195 {
"platform_vendor", CL_PLATFORM_VENDOR },
196 {
"platform_extensions", CL_PLATFORM_EXTENSIONS },
203 {
"device_name", CL_DEVICE_NAME },
204 {
"device_vendor", CL_DEVICE_VENDOR },
205 {
"driver_version", CL_DRIVER_VERSION },
206 {
"device_version", CL_DEVICE_VERSION },
207 {
"device_profile", CL_DEVICE_PROFILE },
208 {
"device_extensions", CL_DEVICE_EXTENSIONS },
215 {
"cpu", CL_DEVICE_TYPE_CPU },
216 {
"gpu", CL_DEVICE_TYPE_GPU },
217 {
"accelerator", CL_DEVICE_TYPE_ACCELERATOR },
218 {
"custom", CL_DEVICE_TYPE_CUSTOM },
219 {
"default", CL_DEVICE_TYPE_DEFAULT },
220 {
"all", CL_DEVICE_TYPE_ALL },
224 cl_platform_info
key)
229 cle = clGetPlatformInfo(platform_id,
key, 0,
NULL, &
size);
230 if (cle != CL_SUCCESS)
235 cle = clGetPlatformInfo(platform_id,
key,
size, str, &
size);
236 if (cle != CL_SUCCESS) {
250 cle = clGetDeviceInfo(device_id,
key, 0,
NULL, &
size);
251 if (cle != CL_SUCCESS)
256 cle = clGetDeviceInfo(device_id,
key,
size, str, &
size);
257 if (cle != CL_SUCCESS) {
271 CL_PLATFORM_EXTENSIONS);
272 if (str && strstr(str,
name))
284 CL_DEVICE_EXTENSIONS);
285 if (str && strstr(str,
name))
299 "%s found as platform extension.\n",
name);
305 "%s found as device extension.\n",
name);
313 cl_uint *nb_platforms,
314 cl_platform_id **platforms,
319 cle = clGetPlatformIDs(0,
NULL, nb_platforms);
320 if (cle != CL_SUCCESS) {
322 "OpenCL platforms: %d.\n", cle);
332 cle = clGetPlatformIDs(*nb_platforms, *platforms,
NULL);
333 if (cle != CL_SUCCESS) {
335 "platforms: %d.\n", cle);
344 cl_platform_id platform_id,
345 const char *platform_name,
363 "of platform \"%s\".\n",
379 cl_platform_id platform_id,
380 const char *platform_name,
382 cl_device_id **devices,
387 cle = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL,
388 0,
NULL, nb_devices);
389 if (cle == CL_DEVICE_NOT_FOUND) {
391 "on platform \"%s\".\n", platform_name);
394 }
else if (cle != CL_SUCCESS) {
396 "on platform \"%s\": %d.\n", platform_name, cle);
400 "platform \"%s\".\n", *nb_devices, platform_name);
406 cle = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL,
407 *nb_devices, *devices,
NULL);
408 if (cle != CL_SUCCESS) {
410 "on platform \"%s\": %d.\n", platform_name, cle);
419 cl_device_id device_id,
420 const char *device_name,
430 cl_device_type match_type = 0, device_type;
445 cle = clGetDeviceInfo(device_id, CL_DEVICE_TYPE,
446 sizeof(device_type), &device_type,
NULL);
447 if (cle != CL_SUCCESS) {
449 "of device \"%s\".\n", device_name);
453 if (!(device_type & match_type)) {
469 "of device \"%s\".\n",
489 cl_uint *nb_platforms,
490 cl_platform_id **platforms,
493 cl_platform_id platform_id,
494 const char *platform_name,
497 cl_platform_id platform_id,
498 const char *platform_name,
500 cl_device_id **devices,
503 cl_device_id device_id,
504 const char *device_name,
510 cl_context_properties *props)
512 cl_uint nb_platforms;
513 cl_platform_id *platforms =
NULL;
514 cl_platform_id platform_id;
516 cl_device_id *devices =
NULL;
519 cl_context_properties default_props[3];
520 char *platform_name_src =
NULL,
521 *device_name_src =
NULL;
522 int err, found, p, d;
533 for (p = 0; p < nb_platforms; p++) {
534 const char *platform_name;
543 if (platform_name_src)
544 platform_name = platform_name_src;
546 platform_name =
"Unknown Platform";
559 &nb_devices, &devices,
564 for (d = 0; d < nb_devices; d++) {
565 const char *device_name;
575 device_name = device_name_src;
577 device_name =
"Unknown Device";
590 platform_name, device_name);
593 platform_id = platforms[p];
612 props = default_props;
613 default_props[0] = CL_CONTEXT_PLATFORM;
614 default_props[1] = (intptr_t)platform_id;
615 default_props[2] = 0;
617 if (props[0] == CL_CONTEXT_PLATFORM && props[1] == 0)
618 props[1] = (intptr_t)platform_id;
652 if (device && device[0]) {
654 int d = -1, p = -1,
ret;
655 if (device[0] ==
'.')
656 ret = sscanf(device,
".%d", &d);
658 ret = sscanf(device,
"%d.%d", &p, &d);
661 "index specification \"%s\".\n", device);
682 if (cle != CL_SUCCESS) {
684 "command queue: %d.\n", cle);
694 "command queue: %d.\n", cle);
699 cle = clGetDeviceInfo(hwctx->
device_id, CL_DEVICE_PLATFORM,
702 if (cle != CL_SUCCESS) {
704 "platform containing the device.\n");
708 #define CL_FUNC(name, desc) do { \
711 priv->name = clGetExtensionFunctionAddressForPlatform( \
712 priv->platform_id, #name); \
714 av_log(hwdev, AV_LOG_VERBOSE, \
715 desc " function not found (%s).\n", #name); \
718 av_log(hwdev, AV_LOG_VERBOSE, \
719 desc " function found (%s).\n", #name); \
723 #if HAVE_OPENCL_DRM_BEIGNET
727 CL_FUNC(clCreateImageFromFdINTEL,
728 "Beignet DRM to OpenCL image mapping");
732 "mapping not usable.\n");
733 priv->beignet_drm_mapping_usable = 0;
735 priv->beignet_drm_mapping_usable = 1;
740 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
743 cl_context_properties *props =
NULL;
744 VADisplay va_display;
745 const char *va_ext =
"cl_intel_va_api_media_sharing";
750 "required for QSV to OpenCL mapping.\n", va_ext);
754 cle = clGetContextInfo(hwctx->
context, CL_CONTEXT_PROPERTIES,
755 0,
NULL, &props_size);
756 if (cle != CL_SUCCESS) {
758 "properties: %d.\n", cle);
761 if (props_size == 0) {
763 "enabled on context creation to use QSV to "
764 "OpenCL mapping.\n");
772 cle = clGetContextInfo(hwctx->
context, CL_CONTEXT_PROPERTIES,
773 props_size, props,
NULL);
774 if (cle != CL_SUCCESS) {
776 "properties: %d.\n", cle);
781 for (
i = 0;
i < (props_size /
sizeof(*props) - 1);
i++) {
782 if (props[
i] == CL_CONTEXT_VA_API_DISPLAY_INTEL) {
783 va_display = (VADisplay)(intptr_t)props[
i+1];
789 "enabled on context creation to use QSV to "
790 "OpenCL mapping.\n");
793 if (!vaDisplayIsValid(va_display)) {
795 "required on context creation to use QSV to "
796 "OpenCL mapping.\n");
800 CL_FUNC(clCreateFromVA_APIMediaSurfaceINTEL,
801 "Intel QSV to OpenCL mapping");
802 CL_FUNC(clEnqueueAcquireVA_APIMediaSurfacesINTEL,
803 "Intel QSV in OpenCL acquire");
804 CL_FUNC(clEnqueueReleaseVA_APIMediaSurfacesINTEL,
805 "Intel QSV in OpenCL release");
811 priv->qsv_mapping_usable = 0;
813 priv->qsv_mapping_usable = 1;
819 #if HAVE_OPENCL_DXVA2
823 CL_FUNC(clCreateFromDX9MediaSurfaceKHR,
824 "DXVA2 to OpenCL mapping");
825 CL_FUNC(clEnqueueAcquireDX9MediaSurfacesKHR,
826 "DXVA2 in OpenCL acquire");
827 CL_FUNC(clEnqueueReleaseDX9MediaSurfacesKHR,
828 "DXVA2 in OpenCL release");
833 priv->dxva2_mapping_usable = 0;
835 priv->dx9_media_adapter_type = CL_ADAPTER_D3D9EX_KHR;
836 priv->dxva2_mapping_usable = 1;
841 #if HAVE_OPENCL_D3D11
843 const char *d3d11_ext =
"cl_khr_d3d11_sharing";
844 const char *nv12_ext =
"cl_intel_d3d11_nv12_media_sharing";
849 "required for D3D11 to OpenCL mapping.\n", d3d11_ext);
853 "required for D3D11 to OpenCL mapping.\n", nv12_ext);
857 CL_FUNC(clCreateFromD3D11Texture2DKHR,
858 "D3D11 to OpenCL mapping");
859 CL_FUNC(clEnqueueAcquireD3D11ObjectsKHR,
860 "D3D11 in OpenCL acquire");
861 CL_FUNC(clEnqueueReleaseD3D11ObjectsKHR,
862 "D3D11 in OpenCL release");
867 priv->d3d11_mapping_usable = 0;
869 priv->d3d11_mapping_usable = 1;
874 #if HAVE_OPENCL_DRM_ARM
876 const char *drm_arm_ext =
"cl_arm_import_memory";
877 const char *image_ext =
"cl_khr_image2d_from_buffer";
882 "required for DRM to OpenCL mapping on ARM.\n",
888 "required for DRM to OpenCL mapping on ARM.\n",
898 priv->drm_arm_mapping_usable = 0;
900 priv->drm_arm_mapping_usable = 1;
917 if (cle != CL_SUCCESS) {
919 "command queue reference: %d.\n", cle);
925 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
927 cl_platform_id platform_id,
928 const char *platform_name,
934 if (!clGetExtensionFunctionAddressForPlatform(platform_id,
935 "clGetDeviceIDsFromVA_APIMediaAdapterINTEL")) {
937 "VAAPI device enumeration function.\n", platform_name);
945 cl_platform_id platform_id,
946 const char *platform_name,
948 cl_device_id **devices,
951 VADisplay va_display =
context;
952 clGetDeviceIDsFromVA_APIMediaAdapterINTEL_fn
953 clGetDeviceIDsFromVA_APIMediaAdapterINTEL;
956 clGetDeviceIDsFromVA_APIMediaAdapterINTEL =
957 clGetExtensionFunctionAddressForPlatform(platform_id,
958 "clGetDeviceIDsFromVA_APIMediaAdapterINTEL");
959 if (!clGetDeviceIDsFromVA_APIMediaAdapterINTEL) {
961 "clGetDeviceIDsFromVA_APIMediaAdapterINTEL().\n");
965 cle = clGetDeviceIDsFromVA_APIMediaAdapterINTEL(
966 platform_id, CL_VA_API_DISPLAY_INTEL, va_display,
967 CL_PREFERRED_DEVICES_FOR_VA_API_INTEL, 0,
NULL, nb_devices);
968 if (cle == CL_DEVICE_NOT_FOUND) {
970 "on platform \"%s\".\n", platform_name);
973 }
else if (cle != CL_SUCCESS) {
975 "on platform \"%s\": %d.\n", platform_name, cle);
983 cle = clGetDeviceIDsFromVA_APIMediaAdapterINTEL(
984 platform_id, CL_VA_API_DISPLAY_INTEL, va_display,
985 CL_PREFERRED_DEVICES_FOR_VA_API_INTEL, *nb_devices, *devices,
NULL);
986 if (cle != CL_SUCCESS) {
988 "devices on platform \"%s\": %d.\n", platform_name, cle);
997 cl_device_id device_id,
998 const char *device_name,
1001 const char *va_ext =
"cl_intel_va_api_media_sharing";
1007 "%s extension.\n", device_name, va_ext);
1013 #if HAVE_OPENCL_DXVA2
1015 cl_platform_id platform_id,
1016 const char *platform_name,
1019 const char *dx9_ext =
"cl_khr_dx9_media_sharing";
1025 "%s extension.\n", platform_name, dx9_ext);
1031 cl_platform_id platform_id,
1032 const char *platform_name,
1033 cl_uint *nb_devices,
1034 cl_device_id **devices,
1037 IDirect3DDevice9 *device =
context;
1038 clGetDeviceIDsFromDX9MediaAdapterKHR_fn
1039 clGetDeviceIDsFromDX9MediaAdapterKHR;
1040 cl_dx9_media_adapter_type_khr media_adapter_type = CL_ADAPTER_D3D9EX_KHR;
1043 clGetDeviceIDsFromDX9MediaAdapterKHR =
1044 clGetExtensionFunctionAddressForPlatform(platform_id,
1045 "clGetDeviceIDsFromDX9MediaAdapterKHR");
1046 if (!clGetDeviceIDsFromDX9MediaAdapterKHR) {
1048 "clGetDeviceIDsFromDX9MediaAdapterKHR().\n");
1052 cle = clGetDeviceIDsFromDX9MediaAdapterKHR(
1053 platform_id, 1, &media_adapter_type, (
void**)&device,
1054 CL_PREFERRED_DEVICES_FOR_DX9_MEDIA_ADAPTER_KHR,
1055 0,
NULL, nb_devices);
1056 if (cle == CL_DEVICE_NOT_FOUND) {
1058 "on platform \"%s\".\n", platform_name);
1061 }
else if (cle != CL_SUCCESS) {
1063 "on platform \"%s\": %d.\n", platform_name, cle);
1071 cle = clGetDeviceIDsFromDX9MediaAdapterKHR(
1072 platform_id, 1, &media_adapter_type, (
void**)&device,
1073 CL_PREFERRED_DEVICES_FOR_DX9_MEDIA_ADAPTER_KHR,
1074 *nb_devices, *devices,
NULL);
1075 if (cle != CL_SUCCESS) {
1077 "devices on platform \"%s\": %d.\n", platform_name, cle);
1086 #if HAVE_OPENCL_D3D11
1088 cl_platform_id platform_id,
1089 const char *platform_name,
1092 const char *d3d11_ext =
"cl_khr_d3d11_sharing";
1098 "%s extension.\n", platform_name, d3d11_ext);
1104 cl_platform_id platform_id,
1105 const char *platform_name,
1106 cl_uint *nb_devices,
1107 cl_device_id **devices,
1111 clGetDeviceIDsFromD3D11KHR_fn clGetDeviceIDsFromD3D11KHR;
1114 clGetDeviceIDsFromD3D11KHR =
1115 clGetExtensionFunctionAddressForPlatform(platform_id,
1116 "clGetDeviceIDsFromD3D11KHR");
1117 if (!clGetDeviceIDsFromD3D11KHR) {
1119 "clGetDeviceIDsFromD3D11KHR().\n");
1123 cle = clGetDeviceIDsFromD3D11KHR(platform_id,
1124 CL_D3D11_DEVICE_KHR, device,
1125 CL_PREFERRED_DEVICES_FOR_D3D11_KHR,
1126 0,
NULL, nb_devices);
1127 if (cle == CL_DEVICE_NOT_FOUND) {
1129 "on platform \"%s\".\n", platform_name);
1132 }
else if (cle != CL_SUCCESS) {
1134 "on platform \"%s\": %d.\n", platform_name, cle);
1142 cle = clGetDeviceIDsFromD3D11KHR(platform_id,
1143 CL_D3D11_DEVICE_KHR, device,
1144 CL_PREFERRED_DEVICES_FOR_D3D11_KHR,
1145 *nb_devices, *devices,
NULL);
1146 if (cle != CL_SUCCESS) {
1148 "devices on platform \"%s\": %d.\n", platform_name, cle);
1157 #if HAVE_OPENCL_DXVA2 || HAVE_OPENCL_D3D11
1159 cl_device_id device_id,
1160 const char *device_name,
1163 cl_device_type device_type;
1166 cle = clGetDeviceInfo(device_id, CL_DEVICE_TYPE,
1167 sizeof(device_type), &device_type,
NULL);
1168 if (cle != CL_SUCCESS) {
1170 "of device \"%s\".\n", device_name);
1173 if (!(device_type & CL_DEVICE_TYPE_GPU)) {
1183 #if HAVE_OPENCL_DRM_ARM
1185 cl_platform_id platform_id,
1186 const char *platform_name,
1189 const char *drm_arm_ext =
"cl_arm_import_memory";
1195 "%s extension.\n", platform_name, drm_arm_ext);
1201 cl_device_id device_id,
1202 const char *device_name,
1205 const char *drm_arm_ext =
"cl_arm_import_memory";
1211 "%s extension.\n", device_name, drm_arm_ext);
1222 switch (src_ctx->
type) {
1224 #if HAVE_OPENCL_DRM_BEIGNET
1233 err =
av_dict_set(&selector_opts,
"platform_vendor",
"Intel", 0);
1235 err =
av_dict_set(&selector_opts,
"platform_version",
"beignet", 0);
1240 .context = selector_opts,
1244 .filter_device =
NULL,
1253 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
1260 cl_context_properties props[7] = {
1261 CL_CONTEXT_PLATFORM,
1263 CL_CONTEXT_VA_API_DISPLAY_INTEL,
1265 CL_CONTEXT_INTEROP_USER_SYNC,
1272 .context = src_hwctx->
display,
1274 .filter_platform = &opencl_filter_intel_media_vaapi_platform,
1275 .enumerate_devices = &opencl_enumerate_intel_media_vaapi_devices,
1276 .filter_device = &opencl_filter_intel_media_vaapi_device,
1284 #if HAVE_OPENCL_DXVA2
1288 IDirect3DDevice9 *device;
1289 HANDLE device_handle;
1292 hr = IDirect3DDeviceManager9_OpenDeviceHandle(src_hwctx->
devmgr,
1296 "for Direct3D9 device: %lx.\n", (
unsigned long)hr);
1301 hr = IDirect3DDeviceManager9_LockDevice(src_hwctx->
devmgr,
1304 if (SUCCEEDED(hr)) {
1305 cl_context_properties props[5] = {
1306 CL_CONTEXT_PLATFORM,
1308 CL_CONTEXT_ADAPTER_D3D9EX_KHR,
1317 .filter_platform = &opencl_filter_dxva2_platform,
1318 .enumerate_devices = &opencl_enumerate_dxva2_devices,
1319 .filter_device = &opencl_filter_gpu_device,
1324 IDirect3DDeviceManager9_UnlockDevice(src_hwctx->
devmgr,
1325 device_handle, FALSE);
1328 "for Direct3D9 device: %lx.\n", (
unsigned long)hr);
1332 IDirect3DDeviceManager9_CloseDeviceHandle(src_hwctx->
devmgr,
1338 #if HAVE_OPENCL_D3D11
1342 cl_context_properties props[5] = {
1343 CL_CONTEXT_PLATFORM,
1345 CL_CONTEXT_D3D11_DEVICE_KHR,
1346 (intptr_t)src_hwctx->
device,
1352 .context = src_hwctx->
device,
1354 .filter_platform = &opencl_filter_d3d11_platform,
1355 .enumerate_devices = &opencl_enumerate_d3d11_devices,
1356 .filter_device = &opencl_filter_gpu_device,
1364 #if HAVE_OPENCL_DRM_ARM
1372 .filter_platform = &opencl_filter_drm_arm_platform,
1374 .filter_device = &opencl_filter_drm_arm_device,
1382 #if HAVE_OPENCL_VIDEOTOOLBOX
1398 cl_image_format *image_format,
1399 cl_image_desc *image_desc)
1418 wsub = 1 <<
desc->log2_chroma_w;
1424 for (
c = 0;
c <
desc->nb_components;
c++) {
1426 if (
comp->plane != plane)
1429 if (
comp->step != 1 &&
comp->step != 2 &&
1430 comp->step != 4 &&
comp->step != 8)
1434 if (
comp->shift +
comp->depth != 8 &&
1439 if (depth &&
comp->depth != depth)
1443 if (
comp->depth > 8 &&
1451 depth =
comp->depth;
1452 order = order * 10 +
comp->offset / ((depth + 7) / 8) + 1;
1455 c ==
desc->nb_components - 1);
1461 memset(image_format, 0,
sizeof(*image_format));
1462 memset(image_desc, 0,
sizeof(*image_desc));
1463 image_desc->image_type = CL_MEM_OBJECT_IMAGE2D;
1465 if (plane == 0 ||
alpha) {
1466 image_desc->image_width =
width;
1467 image_desc->image_height =
height;
1468 image_desc->image_row_pitch =
step *
width;
1470 image_desc->image_width =
width / wsub;
1472 image_desc->image_row_pitch =
step *
width / wsub;
1476 image_format->image_channel_data_type = CL_UNORM_INT8;
1479 image_format->image_channel_data_type = CL_UNORM_INT16;
1480 else if (depth == 32)
1481 image_format->image_channel_data_type = CL_FLOAT;
1486 #define CHANNEL_ORDER(order, type) \
1487 case order: image_format->image_channel_order = type; break;
1500 #undef CHANNEL_ORDER
1506 const void *hwconfig,
1510 cl_uint nb_image_formats;
1511 cl_image_format *image_formats =
NULL;
1514 int err, pix_fmts_found;
1515 size_t max_width, max_height;
1517 cle = clGetDeviceInfo(hwctx->
device_id, CL_DEVICE_IMAGE2D_MAX_WIDTH,
1518 sizeof(max_width), &max_width,
NULL);
1519 if (cle != CL_SUCCESS) {
1521 "supported image width: %d.\n", cle);
1525 cle = clGetDeviceInfo(hwctx->
device_id, CL_DEVICE_IMAGE2D_MAX_HEIGHT,
1526 sizeof(max_height), &max_height,
NULL);
1527 if (cle != CL_SUCCESS) {
1529 "supported image height: %d.\n", cle);
1536 cle = clGetSupportedImageFormats(hwctx->
context,
1538 CL_MEM_OBJECT_IMAGE2D,
1539 0,
NULL, &nb_image_formats);
1540 if (cle != CL_SUCCESS) {
1542 "image formats: %d.\n", cle);
1546 if (nb_image_formats == 0) {
1548 "driver (zero supported image formats).\n");
1555 if (!image_formats) {
1560 cle = clGetSupportedImageFormats(hwctx->
context,
1562 CL_MEM_OBJECT_IMAGE2D,
1564 image_formats,
NULL);
1565 if (cle != CL_SUCCESS) {
1567 "image formats: %d.\n", cle);
1574 cl_image_format image_format;
1575 cl_image_desc image_desc;
1578 for (plane = 0;; plane++) {
1585 for (
i = 0;
i < nb_image_formats;
i++) {
1586 if (image_formats[
i].image_channel_order ==
1587 image_format.image_channel_order &&
1588 image_formats[
i].image_channel_data_type ==
1589 image_format.image_channel_data_type)
1592 if (
i == nb_image_formats) {
1639 for (p = 0; p <
desc->nb_planes; p++) {
1640 cle = clReleaseMemObject(
desc->planes[p]);
1641 if (cle != CL_SUCCESS) {
1657 cl_image_format image_format;
1658 cl_image_desc image_desc;
1669 &image_format, &image_desc);
1677 image_desc.image_row_pitch = 0;
1679 image = clCreateImage(hwctx->
context, CL_MEM_READ_WRITE,
1680 &image_format, &image_desc,
NULL, &cle);
1683 "plane %d: %d.\n", p, cle);
1687 desc->planes[p] = image;
1690 desc->nb_planes = p;
1700 for (p = 0;
desc->planes[p]; p++)
1701 clReleaseMemObject(
desc->planes[p]);
1716 if (cle != CL_SUCCESS) {
1718 "command queue: %d.\n", cle);
1743 #if HAVE_OPENCL_DXVA2 || HAVE_OPENCL_D3D11
1745 for (
i = 0;
i < priv->nb_mapped_frames;
i++) {
1747 for (p = 0; p <
desc->nb_planes; p++) {
1748 cle = clReleaseMemObject(
desc->planes[p]);
1749 if (cle != CL_SUCCESS) {
1751 "frame object (frame %d plane %d): %d.\n",
1761 if (cle != CL_SUCCESS) {
1763 "command queue: %d.\n", cle);
1780 for (p = 0; p <
desc->nb_planes; p++)
1781 frame->data[p] = (uint8_t*)
desc->planes[p];
1808 cl_event *events,
int nb_events)
1813 cle = clWaitForEvents(nb_events, events);
1814 if (cle != CL_SUCCESS) {
1816 "completion: %d.\n", cle);
1820 for (
i = 0;
i < nb_events;
i++) {
1821 cle = clReleaseEvent(events[
i]);
1822 if (cle != CL_SUCCESS) {
1824 "event: %d.\n", cle);
1835 cl_image_format image_format;
1836 cl_image_desc image_desc;
1838 size_t origin[3] = { 0, 0, 0 };
1849 &image_format, &image_desc);
1856 if (!
dst->data[p]) {
1858 "destination frame for transfer.\n", p);
1863 region[0] = image_desc.image_width;
1864 region[1] = image_desc.image_height;
1868 (cl_mem)
src->data[p],
1869 CL_FALSE, origin, region,
1870 dst->linesize[p], 0,
1872 0,
NULL, &events[p]);
1873 if (cle != CL_SUCCESS) {
1875 "OpenCL image plane %d: %d.\n", p, cle);
1890 cl_image_format image_format;
1891 cl_image_desc image_desc;
1893 size_t origin[3] = { 0, 0, 0 };
1904 &image_format, &image_desc);
1911 if (!
src->data[p]) {
1913 "source frame for transfer.\n", p);
1918 region[0] = image_desc.image_width;
1919 region[1] = image_desc.image_height;
1923 (cl_mem)
dst->data[p],
1924 CL_FALSE, origin, region,
1925 src->linesize[p], 0,
1927 0,
NULL, &events[p]);
1928 if (cle != CL_SUCCESS) {
1930 "OpenCL image plane %d: %d.\n", p, cle);
1958 if (!
map->address[p])
1964 0,
NULL, &events[e]);
1965 if (cle != CL_SUCCESS) {
1967 "image plane %d: %d.\n", p, cle);
1981 cl_map_flags map_flags;
1982 cl_image_format image_format;
1983 cl_image_desc image_desc;
1986 size_t origin[3] = { 0, 0, 0 };
1998 map_flags = CL_MAP_WRITE_INVALIDATE_REGION;
2002 map_flags |= CL_MAP_READ;
2004 map_flags |= CL_MAP_WRITE;
2014 &image_format, &image_desc);
2020 region[0] = image_desc.image_width;
2021 region[1] = image_desc.image_height;
2026 (cl_mem)
src->data[p],
2027 CL_FALSE, map_flags, origin, region,
2030 if (!
map->address[p]) {
2032 "image plane %d: %d.\n", p, cle);
2037 dst->data[p] =
map->address[p];
2038 dst->linesize[p] = row_pitch;
2041 p,
src->data[p],
dst->data[p]);
2054 dst->height =
src->height;
2060 if (!
map->address[p])
2063 (cl_mem)
src->data[p],
2065 0,
NULL, &events[p]);
2073 #if HAVE_OPENCL_DRM_BEIGNET
2075 typedef struct DRMBeignetToOpenCLMapping {
2080 } DRMBeignetToOpenCLMapping;
2085 DRMBeignetToOpenCLMapping *mapping = hwmap->
priv;
2089 for (
i = 0;
i < mapping->frame.nb_planes;
i++) {
2090 cle = clReleaseMemObject(mapping->frame.planes[
i]);
2091 if (cle != CL_SUCCESS) {
2093 "of plane %d of DRM frame: %d.\n",
i, cle);
2106 DRMBeignetToOpenCLMapping *mapping;
2118 for (
i = 0;
i <
desc->nb_layers;
i++) {
2120 for (j = 0; j < layer->
nb_planes; j++) {
2125 cl_import_image_info_intel image_info = {
2127 .size =
object->size,
2128 .type = CL_MEM_OBJECT_IMAGE2D,
2130 .row_pitch = plane->
pitch,
2132 cl_image_desc image_desc;
2140 "plane %d is not representable in OpenCL: %d.\n",
2144 image_info.width = image_desc.image_width;
2145 image_info.height = image_desc.image_height;
2147 mapping->frame.planes[p] =
2148 priv->clCreateImageFromFdINTEL(hwctx->
context,
2150 if (!mapping->frame.planes[p]) {
2152 "from layer %d plane %d of DRM frame: %d.\n",
2158 dst->data[p] = (uint8_t*)mapping->frame.planes[p];
2159 mapping->frame.nb_planes = ++p;
2164 &opencl_unmap_from_drm_beignet,
2170 dst->height =
src->height;
2175 for (p = 0; p < mapping->frame.nb_planes; p++) {
2176 if (mapping->frame.planes[p])
2177 clReleaseMemObject(mapping->frame.planes[p]);
2180 memset(
dst->data, 0,
sizeof(
dst->data));
2184 #if HAVE_OPENCL_VAAPI_BEIGNET
2203 err = opencl_map_from_drm_beignet(dst_fc,
dst,
tmp,
flags);
2221 return CL_MEM_READ_WRITE;
2223 return CL_MEM_READ_ONLY;
2225 return CL_MEM_WRITE_ONLY;
2230 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
2244 cle = device_priv->clEnqueueReleaseVA_APIMediaSurfacesINTEL(
2247 if (cle != CL_SUCCESS) {
2249 "handles: %d.\n", cle);
2254 for (p = 0; p <
desc->nb_planes; p++) {
2255 cle = clReleaseMemObject(
desc->planes[p]);
2256 if (cle != CL_SUCCESS) {
2258 "image of plane %d of QSV/VAAPI surface: %d\n",
2275 VASurfaceID va_surface;
2276 cl_mem_flags cl_flags;
2284 mfxFrameSurface1 *mfx_surface = (mfxFrameSurface1*)
src->data[3];
2290 va_surface = *(VASurfaceID *)base_handle;
2294 va_surface = (VASurfaceID)(uintptr_t)
src->data[3];
2304 "OpenCL.\n", va_surface);
2312 desc->nb_planes = 2;
2314 for (p = 0; p <
desc->nb_planes; p++) {
2316 device_priv->clCreateFromVA_APIMediaSurfaceINTEL(
2317 dst_dev->context, cl_flags, &va_surface, p, &cle);
2318 if (!
desc->planes[p]) {
2320 "image from plane %d of QSV/VAAPI surface "
2321 "%#x: %d.\n", p, va_surface, cle);
2326 dst->data[p] = (uint8_t*)
desc->planes[p];
2329 cle = device_priv->clEnqueueAcquireVA_APIMediaSurfacesINTEL(
2332 if (cle != CL_SUCCESS) {
2334 "handles: %d.\n", cle);
2344 &opencl_unmap_from_qsv,
desc);
2349 dst->height =
src->height;
2354 for (p = 0; p <
desc->nb_planes; p++)
2355 if (
desc->planes[p])
2356 clReleaseMemObject(
desc->planes[p]);
2358 memset(
dst->data, 0,
sizeof(
dst->data));
2364 #if HAVE_OPENCL_DXVA2
2377 cle = device_priv->clEnqueueReleaseDX9MediaSurfacesKHR(
2380 if (cle != CL_SUCCESS) {
2382 "handle: %d.\n", cle);
2403 "OpenCL.\n",
src->data[3]);
2405 for (
i = 0;
i < src_hwctx->nb_surfaces;
i++) {
2406 if (src_hwctx->surfaces[
i] == (IDirect3DSurface9*)
src->data[3])
2409 if (
i >= src_hwctx->nb_surfaces) {
2411 "is not in the mapped frames context.\n");
2415 desc = &frames_priv->mapped_frames[
i];
2417 cle = device_priv->clEnqueueAcquireDX9MediaSurfacesKHR(
2420 if (cle != CL_SUCCESS) {
2422 "handle: %d.\n", cle);
2430 for (
i = 0;
i <
desc->nb_planes;
i++)
2431 dst->data[
i] = (uint8_t*)
desc->planes[
i];
2434 &opencl_unmap_from_dxva2,
desc);
2439 dst->height =
src->height;
2444 cle = device_priv->clEnqueueReleaseDX9MediaSurfacesKHR(
2447 if (cle == CL_SUCCESS)
2449 memset(
dst->data, 0,
sizeof(
dst->data));
2460 cl_mem_flags cl_flags;
2462 int err,
i, p, nb_planes;
2466 "for DXVA2 to OpenCL mapping.\n");
2473 "for DXVA2 to OpenCL mapping.\n");
2481 frames_priv->nb_mapped_frames = src_hwctx->
nb_surfaces;
2483 frames_priv->mapped_frames =
2484 av_calloc(frames_priv->nb_mapped_frames,
2485 sizeof(*frames_priv->mapped_frames));
2486 if (!frames_priv->mapped_frames)
2489 for (
i = 0;
i < frames_priv->nb_mapped_frames;
i++) {
2491 cl_dx9_surface_info_khr surface_info = {
2493 .shared_handle =
NULL,
2495 desc->nb_planes = nb_planes;
2496 for (p = 0; p < nb_planes; p++) {
2498 device_priv->clCreateFromDX9MediaSurfaceKHR(
2500 device_priv->dx9_media_adapter_type,
2501 &surface_info, p, &cle);
2502 if (!
desc->planes[p]) {
2504 "image from plane %d of DXVA2 surface %d: %d.\n",
2515 for (
i = 0;
i < frames_priv->nb_mapped_frames;
i++) {
2517 for (p = 0; p <
desc->nb_planes; p++) {
2518 if (
desc->planes[p])
2519 clReleaseMemObject(
desc->planes[p]);
2522 av_freep(&frames_priv->mapped_frames);
2523 frames_priv->nb_mapped_frames = 0;
2529 #if HAVE_OPENCL_D3D11
2540 cle = device_priv->clEnqueueReleaseD3D11ObjectsKHR(
2543 if (cle != CL_SUCCESS) {
2545 "handle: %d.\n", cle);
2562 if (
index >= frames_priv->nb_mapped_frames) {
2564 "mapping: %d >= %d.\n",
index, frames_priv->nb_mapped_frames);
2571 desc = &frames_priv->mapped_frames[
index];
2573 cle = device_priv->clEnqueueAcquireD3D11ObjectsKHR(
2576 if (cle != CL_SUCCESS) {
2578 "handle: %d.\n", cle);
2586 for (
i = 0;
i <
desc->nb_planes;
i++)
2587 dst->data[
i] = (uint8_t*)
desc->planes[
i];
2590 &opencl_unmap_from_d3d11,
desc);
2595 dst->height =
src->height;
2600 cle = device_priv->clEnqueueReleaseD3D11ObjectsKHR(
2603 if (cle == CL_SUCCESS)
2605 memset(
dst->data, 0,
sizeof(
dst->data));
2616 cl_mem_flags cl_flags;
2618 int err,
i, p, nb_planes;
2622 "for D3D11 to OpenCL mapping.\n");
2629 "for D3D11 to OpenCL mapping.\n");
2639 frames_priv->mapped_frames =
2640 av_calloc(frames_priv->nb_mapped_frames,
2641 sizeof(*frames_priv->mapped_frames));
2642 if (!frames_priv->mapped_frames)
2645 for (
i = 0;
i < frames_priv->nb_mapped_frames;
i++) {
2647 desc->nb_planes = nb_planes;
2648 for (p = 0; p < nb_planes; p++) {
2649 UINT subresource = 2 *
i + p;
2652 device_priv->clCreateFromD3D11Texture2DKHR(
2655 if (!
desc->planes[p]) {
2657 "image from plane %d of D3D texture "
2658 "index %d (subresource %u): %d.\n",
2659 p,
i, (
unsigned int)subresource, cle);
2669 for (
i = 0;
i < frames_priv->nb_mapped_frames;
i++) {
2671 for (p = 0; p <
desc->nb_planes; p++) {
2672 if (
desc->planes[p])
2673 clReleaseMemObject(
desc->planes[p]);
2676 av_freep(&frames_priv->mapped_frames);
2677 frames_priv->nb_mapped_frames = 0;
2683 #if HAVE_OPENCL_DRM_ARM
2685 typedef struct DRMARMtoOpenCLMapping {
2690 } DRMARMtoOpenCLMapping;
2695 DRMARMtoOpenCLMapping *mapping = hwmap->
priv;
2698 for (
i = 0;
i < mapping->nb_planes;
i++)
2699 clReleaseMemObject(mapping->plane_images[
i]);
2701 for (
i = 0;
i < mapping->nb_objects;
i++)
2702 clReleaseMemObject(mapping->object_buffers[
i]);
2714 DRMARMtoOpenCLMapping *mapping =
NULL;
2715 cl_mem_flags cl_flags;
2716 const cl_import_properties_arm props[3] = {
2717 CL_IMPORT_TYPE_ARM, CL_IMPORT_TYPE_DMA_BUF_ARM, 0,
2732 mapping->nb_objects =
desc->nb_objects;
2733 for (
i = 0;
i <
desc->nb_objects;
i++) {
2734 int fd =
desc->objects[
i].fd;
2738 if (
desc->objects[
i].format_modifier) {
2740 "nonzero format modifier %"PRId64
", result may not "
2741 "be as expected.\n",
i, fd,
2742 desc->objects[
i].format_modifier);
2745 mapping->object_buffers[
i] =
2746 clImportMemoryARM(dst_dev->
context, cl_flags, props,
2747 &fd,
desc->objects[
i].size, &cle);
2748 if (!mapping->object_buffers[
i]) {
2750 "from object %d (fd %d, size %"SIZE_SPECIFIER") of DRM frame: %d.\n",
2751 i, fd,
desc->objects[
i].size, cle);
2757 mapping->nb_planes = 0;
2758 for (
i = 0;
i <
desc->nb_layers;
i++) {
2761 for (j = 0; j < layer->
nb_planes; j++) {
2763 cl_mem plane_buffer;
2764 cl_image_format image_format;
2765 cl_image_desc image_desc;
2766 cl_buffer_region region;
2767 int p = mapping->nb_planes;
2771 &image_format, &image_desc);
2774 "layer %d plane %d): %d.\n", p,
i, j, err);
2778 region.origin = plane->
offset;
2779 region.size = image_desc.image_row_pitch *
2780 image_desc.image_height;
2783 clCreateSubBuffer(mapping->object_buffers[plane->
object_index],
2785 CL_BUFFER_CREATE_TYPE_REGION,
2787 if (!plane_buffer) {
2789 "for plane %d: %d.\n", p, cle);
2794 image_desc.buffer = plane_buffer;
2796 mapping->plane_images[p] =
2797 clCreateImage(dst_dev->
context, cl_flags,
2798 &image_format, &image_desc,
NULL, &cle);
2802 clReleaseMemObject(plane_buffer);
2804 if (!mapping->plane_images[p]) {
2806 "for plane %d: %d.\n", p, cle);
2811 ++mapping->nb_planes;
2815 for (
i = 0;
i < mapping->nb_planes;
i++)
2816 dst->data[
i] = (uint8_t*)mapping->plane_images[
i];
2819 &opencl_unmap_from_drm_arm, mapping);
2824 dst->height =
src->height;
2829 for (
i = 0;
i < mapping->nb_planes;
i++) {
2830 clReleaseMemObject(mapping->plane_images[
i]);
2832 for (
i = 0;
i < mapping->nb_objects;
i++) {
2833 if (mapping->object_buffers[
i])
2834 clReleaseMemObject(mapping->object_buffers[
i]);
2837 memset(
dst->data, 0,
sizeof(
dst->data));
2843 #if HAVE_OPENCL_VIDEOTOOLBOX
2855 CVPixelBufferRef pixbuf = (CVPixelBufferRef)
src->data[3];
2856 IOSurfaceRef io_surface_ref = CVPixelBufferGetIOSurface(pixbuf);
2861 if (!io_surface_ref) {
2870 for (
int p = 0;; p++) {
2871 cl_image_format image_format;
2872 cl_image_desc image_desc;
2873 cl_iosurface_properties_APPLE props[] = {
2874 CL_IOSURFACE_REF_APPLE, (cl_iosurface_properties_APPLE) io_surface_ref,
2875 CL_IOSURFACE_PLANE_APPLE, p,
2881 &image_format, &image_desc);
2887 desc->planes[p] = clCreateImageFromIOSurfaceWithPropertiesAPPLE(dst_dev->
context,
2889 &image_format, &image_desc,
2891 if (!
desc->planes[p]) {
2899 for (
int i = 0;
i <
desc->nb_planes;
i++)
2900 dst->data[
i] = (uint8_t *)
desc->planes[
i];
2903 opencl_unmap_from_vt,
desc);
2908 dst->height =
src->height;
2913 for (
int i = 0;
i <
desc->nb_planes;
i++)
2914 clReleaseMemObject(
desc->planes[
i]);
2935 switch (
src->format) {
2936 #if HAVE_OPENCL_DRM_BEIGNET
2938 if (priv->beignet_drm_mapping_usable)
2939 return opencl_map_from_drm_beignet(hwfc,
dst,
src,
flags);
2941 #if HAVE_OPENCL_VAAPI_BEIGNET
2943 if (priv->beignet_drm_mapping_usable)
2944 return opencl_map_from_vaapi(hwfc,
dst,
src,
flags);
2946 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
2949 if (priv->qsv_mapping_usable)
2950 return opencl_map_from_qsv(hwfc,
dst,
src,
flags);
2952 #if HAVE_OPENCL_DXVA2
2954 if (priv->dxva2_mapping_usable)
2955 return opencl_map_from_dxva2(hwfc,
dst,
src,
flags);
2957 #if HAVE_OPENCL_D3D11
2959 if (priv->d3d11_mapping_usable)
2960 return opencl_map_from_d3d11(hwfc,
dst,
src,
flags);
2962 #if HAVE_OPENCL_DRM_ARM
2964 if (priv->drm_arm_mapping_usable)
2965 return opencl_map_from_drm_arm(hwfc,
dst,
src,
flags);
2967 #if HAVE_OPENCL_VIDEOTOOLBOX
2980 #if HAVE_OPENCL_DRM_BEIGNET
2982 if (!priv->beignet_drm_mapping_usable)
2986 #if HAVE_OPENCL_VAAPI_BEIGNET
2988 if (!priv->beignet_drm_mapping_usable)
2992 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
2995 if (!priv->qsv_mapping_usable)
2999 #if HAVE_OPENCL_DXVA2
3001 if (!priv->dxva2_mapping_usable)
3005 err = opencl_frames_derive_from_dxva2(dst_fc, src_fc,
flags);
3011 #if HAVE_OPENCL_D3D11
3013 if (!priv->d3d11_mapping_usable)
3017 err = opencl_frames_derive_from_d3d11(dst_fc, src_fc,
flags);
3023 #if HAVE_OPENCL_DRM_ARM
3025 if (!priv->drm_arm_mapping_usable)
3029 #if HAVE_OPENCL_VIDEOTOOLBOX