Skip to content
Snippets Groups Projects
hwcontext_opencl.c 95 KiB
Newer Older
  • Learn to ignore specific revisions
  • /*
     * This file is part of FFmpeg.
     *
     * FFmpeg is free software; you can redistribute it and/or
     * modify it under the terms of the GNU Lesser General Public
     * License as published by the Free Software Foundation; either
     * version 2.1 of the License, or (at your option) any later version.
     *
     * FFmpeg is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     * Lesser General Public License for more details.
     *
     * You should have received a copy of the GNU Lesser General Public
     * License along with FFmpeg; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     */
    
    
    #include <string.h>
    
    #include "config.h"
    
    #include "avassert.h"
    #include "avstring.h"
    #include "common.h"
    #include "hwcontext.h"
    #include "hwcontext_internal.h"
    #include "hwcontext_opencl.h"
    #include "mem.h"
    #include "pixdesc.h"
    
    
    #if HAVE_OPENCL_VAAPI_BEIGNET
    #include <unistd.h>
    #include <va/va.h>
    #include <va/va_drmcommon.h>
    #include <CL/cl_intel.h>
    #include "hwcontext_vaapi.h"
    #endif
    
    
    #if HAVE_OPENCL_DRM_BEIGNET
    #include <unistd.h>
    #include <CL/cl_intel.h>
    #include "hwcontext_drm.h"
    #endif
    
    
    #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
    
    #if CONFIG_LIBMFX
    
    #include <mfx/mfxstructures.h>
    
    #include <va/va.h>
    #include <CL/va_ext.h>
    #include "hwcontext_vaapi.h"
    #endif
    
    
    #if HAVE_OPENCL_DXVA2
    #define COBJMACROS
    #include <CL/cl_dx9_media_sharing.h>
    #include <dxva2api.h>
    #include "hwcontext_dxva2.h"
    #endif
    
    
    #if HAVE_OPENCL_D3D11
    #include <CL/cl_d3d11.h>
    #include "hwcontext_d3d11va.h"
    #endif
    
    
    #if HAVE_OPENCL_DRM_ARM
    #include <CL/cl_ext.h>
    #include <drm_fourcc.h>
    #include "hwcontext_drm.h"
    #endif
    
    
    
    typedef struct OpenCLDeviceContext {
        // Default command queue to use for transfer/mapping operations on
        // the device.  If the user supplies one, this is a reference to it.
        // Otherwise, it is newly-created.
        cl_command_queue command_queue;
    
        // The platform the context exists on.  This is needed to query and
        // retrieve extension functions.
        cl_platform_id platform_id;
    
        // Platform/device-specific functions.
    
    #if HAVE_OPENCL_DRM_BEIGNET
        int beignet_drm_mapping_usable;
        clCreateImageFromFdINTEL_fn clCreateImageFromFdINTEL;
    
    
    #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
        int qsv_mapping_usable;
        clCreateFromVA_APIMediaSurfaceINTEL_fn
            clCreateFromVA_APIMediaSurfaceINTEL;
        clEnqueueAcquireVA_APIMediaSurfacesINTEL_fn
            clEnqueueAcquireVA_APIMediaSurfacesINTEL;
        clEnqueueReleaseVA_APIMediaSurfacesINTEL_fn
            clEnqueueReleaseVA_APIMediaSurfacesINTEL;
    #endif
    
    
    #if HAVE_OPENCL_DXVA2
        int dxva2_mapping_usable;
        cl_dx9_media_adapter_type_khr dx9_media_adapter_type;
    
        clCreateFromDX9MediaSurfaceKHR_fn
            clCreateFromDX9MediaSurfaceKHR;
        clEnqueueAcquireDX9MediaSurfacesKHR_fn
            clEnqueueAcquireDX9MediaSurfacesKHR;
        clEnqueueReleaseDX9MediaSurfacesKHR_fn
            clEnqueueReleaseDX9MediaSurfacesKHR;
    #endif
    
    
    #if HAVE_OPENCL_D3D11
        int d3d11_mapping_usable;
        clCreateFromD3D11Texture2DKHR_fn
            clCreateFromD3D11Texture2DKHR;
        clEnqueueAcquireD3D11ObjectsKHR_fn
            clEnqueueAcquireD3D11ObjectsKHR;
        clEnqueueReleaseD3D11ObjectsKHR_fn
            clEnqueueReleaseD3D11ObjectsKHR;
    #endif
    
    
    #if HAVE_OPENCL_DRM_ARM
        int drm_arm_mapping_usable;
    #endif
    
    } OpenCLDeviceContext;
    
    typedef struct OpenCLFramesContext {
        // Command queue used for transfer/mapping operations on this frames
        // context.  If the user supplies one, this is a reference to it.
        // Otherwise, it is a reference to the default command queue for the
        // device.
        cl_command_queue command_queue;
    
    #if HAVE_OPENCL_DXVA2 || HAVE_OPENCL_D3D11
    
        // For mapping APIs which have separate creation and acquire/release
        // steps, this stores the OpenCL memory objects corresponding to each
        // frame.
        int                   nb_mapped_frames;
        AVOpenCLFrameDescriptor *mapped_frames;
    #endif
    
    } OpenCLFramesContext;
    
    
    
    static void CL_CALLBACK opencl_error_callback(const char *errinfo,
                                                  const void *private_info,
                                                  size_t cb,
                                                  void *user_data)
    
    150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
    {
        AVHWDeviceContext *ctx = user_data;
        av_log(ctx, AV_LOG_ERROR, "OpenCL error: %s\n", errinfo);
    }
    
    static void opencl_device_free(AVHWDeviceContext *hwdev)
    {
        AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
        cl_int cle;
    
        cle = clReleaseContext(hwctx->context);
        if (cle != CL_SUCCESS) {
            av_log(hwdev, AV_LOG_ERROR, "Failed to release OpenCL "
                   "context: %d.\n", cle);
        }
    }
    
    static struct {
        const char *key;
        cl_platform_info name;
    } opencl_platform_params[] = {
        { "platform_profile",    CL_PLATFORM_PROFILE    },
        { "platform_version",    CL_PLATFORM_VERSION    },
        { "platform_name",       CL_PLATFORM_NAME       },
        { "platform_vendor",     CL_PLATFORM_VENDOR     },
        { "platform_extensions", CL_PLATFORM_EXTENSIONS },
    };
    
    static struct {
        const char *key;
        cl_device_info name;
    } opencl_device_params[] = {
        { "device_name",         CL_DEVICE_NAME         },
        { "device_vendor",       CL_DEVICE_VENDOR       },
        { "driver_version",      CL_DRIVER_VERSION      },
        { "device_version",      CL_DEVICE_VERSION      },
        { "device_profile",      CL_DEVICE_PROFILE      },
        { "device_extensions",   CL_DEVICE_EXTENSIONS   },
    };
    
    static struct {
        const char *key;
        cl_device_type type;
    } opencl_device_types[] = {
        { "cpu",         CL_DEVICE_TYPE_CPU         },
        { "gpu",         CL_DEVICE_TYPE_GPU         },
        { "accelerator", CL_DEVICE_TYPE_ACCELERATOR },
        { "custom",      CL_DEVICE_TYPE_CUSTOM      },
        { "default",     CL_DEVICE_TYPE_DEFAULT     },
        { "all",         CL_DEVICE_TYPE_ALL         },
    };
    
    static char *opencl_get_platform_string(cl_platform_id platform_id,
                                            cl_platform_info key)
    {
        char *str;
        size_t size;
        cl_int cle;
        cle = clGetPlatformInfo(platform_id, key, 0, NULL, &size);
        if (cle != CL_SUCCESS)
            return NULL;
        str = av_malloc(size);
        if (!str)
            return NULL;
        cle = clGetPlatformInfo(platform_id, key, size, str, &size);
        if (cle != CL_SUCCESS) {
            av_free(str);
            return NULL;
        }
        av_assert0(strlen(str) + 1 == size);
        return str;
    }
    
    static char *opencl_get_device_string(cl_device_id device_id,
                                          cl_device_info key)
    {
        char *str;
        size_t size;
        cl_int cle;
        cle = clGetDeviceInfo(device_id, key, 0, NULL, &size);
        if (cle != CL_SUCCESS)
            return NULL;
        str = av_malloc(size);
        if (!str)
            return NULL;
        cle = clGetDeviceInfo(device_id, key, size, str, &size);
        if (cle != CL_SUCCESS) {
            av_free(str);
            return NULL;
        }
        av_assert0(strlen(str) + 1== size);
        return str;
    }
    
    static int opencl_check_platform_extension(cl_platform_id platform_id,
                                               const char *name)
    {
        char *str;
        int found = 0;
        str = opencl_get_platform_string(platform_id,
                                         CL_PLATFORM_EXTENSIONS);
        if (str && strstr(str, name))
            found = 1;
        av_free(str);
        return found;
    }
    
    static int opencl_check_device_extension(cl_device_id device_id,
                                             const char *name)
    {
        char *str;
        int found = 0;
        str = opencl_get_device_string(device_id,
                                       CL_DEVICE_EXTENSIONS);
        if (str && strstr(str, name))
            found = 1;
        av_free(str);
        return found;
    }
    
    static av_unused int opencl_check_extension(AVHWDeviceContext *hwdev,
                                                const char *name)
    {
        AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
        OpenCLDeviceContext    *priv = hwdev->internal->priv;
    
        if (opencl_check_platform_extension(priv->platform_id, name)) {
            av_log(hwdev, AV_LOG_DEBUG,
                   "%s found as platform extension.\n", name);
            return 1;
        }
    
        if (opencl_check_device_extension(hwctx->device_id, name)) {
            av_log(hwdev, AV_LOG_DEBUG,
                   "%s found as device extension.\n", name);
            return 1;
        }
    
        return 0;
    }
    
    static int opencl_enumerate_platforms(AVHWDeviceContext *hwdev,
                                          cl_uint *nb_platforms,
                                          cl_platform_id **platforms,
                                          void *context)
    {
        cl_int cle;
    
        cle = clGetPlatformIDs(0, NULL, nb_platforms);
        if (cle != CL_SUCCESS) {
            av_log(hwdev, AV_LOG_ERROR, "Failed to get number of "
                   "OpenCL platforms: %d.\n", cle);
            return AVERROR(ENODEV);
        }
        av_log(hwdev, AV_LOG_DEBUG, "%u OpenCL platforms found.\n",
               *nb_platforms);
    
        *platforms = av_malloc_array(*nb_platforms, sizeof(**platforms));
        if (!*platforms)
            return AVERROR(ENOMEM);
    
        cle = clGetPlatformIDs(*nb_platforms, *platforms, NULL);
        if (cle != CL_SUCCESS) {
            av_log(hwdev, AV_LOG_ERROR, "Failed to get list of OpenCL "
                   "platforms: %d.\n", cle);
            av_freep(platforms);
            return AVERROR(ENODEV);
        }
    
        return 0;
    }
    
    static int opencl_filter_platform(AVHWDeviceContext *hwdev,
                                      cl_platform_id platform_id,
                                      const char *platform_name,
                                      void *context)
    {
        AVDictionary *opts = context;
        const AVDictionaryEntry *param;
        char *str;
        int i, ret = 0;
    
        for (i = 0; i < FF_ARRAY_ELEMS(opencl_platform_params); i++) {
            param = av_dict_get(opts, opencl_platform_params[i].key,
                                NULL, 0);
            if (!param)
                continue;
    
            str = opencl_get_platform_string(platform_id,
                                             opencl_platform_params[i].name);
            if (!str) {
                av_log(hwdev, AV_LOG_ERROR, "Failed to query %s "
                       "of platform \"%s\".\n",
                       opencl_platform_params[i].key, platform_name);
                return AVERROR_UNKNOWN;
            }
            if (!av_stristr(str, param->value)) {
                av_log(hwdev, AV_LOG_DEBUG, "%s does not match (\"%s\").\n",
                       param->key, str);
                ret = 1;
            }
            av_free(str);
        }
    
        return ret;
    }
    
    static int opencl_enumerate_devices(AVHWDeviceContext *hwdev,
                                        cl_platform_id platform_id,
                                        const char *platform_name,
                                        cl_uint *nb_devices,
                                        cl_device_id **devices,
                                        void *context)
    {
        cl_int cle;
    
        cle = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL,
                             0, NULL, nb_devices);
        if (cle == CL_DEVICE_NOT_FOUND) {
            av_log(hwdev, AV_LOG_DEBUG, "No devices found "
                   "on platform \"%s\".\n", platform_name);
            *nb_devices = 0;
            return 0;
        } else if (cle != CL_SUCCESS) {
            av_log(hwdev, AV_LOG_ERROR, "Failed to get number of devices "
                   "on platform \"%s\": %d.\n", platform_name, cle);
            return AVERROR(ENODEV);
        }
        av_log(hwdev, AV_LOG_DEBUG, "%u OpenCL devices found on "
               "platform \"%s\".\n", *nb_devices, platform_name);
    
        *devices = av_malloc_array(*nb_devices, sizeof(**devices));
        if (!*devices)
            return AVERROR(ENOMEM);
    
        cle = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL,
                             *nb_devices, *devices, NULL);
        if (cle != CL_SUCCESS) {
            av_log(hwdev, AV_LOG_ERROR, "Failed to get list of devices "
                   "on platform \"%s\": %d.\n", platform_name, cle);
            av_freep(devices);
            return AVERROR(ENODEV);
        }
    
        return 0;
    }
    
    static int opencl_filter_device(AVHWDeviceContext *hwdev,
                                    cl_device_id device_id,
                                    const char *device_name,
                                    void *context)
    {
        AVDictionary *opts = context;
        const AVDictionaryEntry *param;
        char *str;
        int i, ret = 0;
    
        param = av_dict_get(opts, "device_type", NULL, 0);
        if (param) {
            cl_device_type match_type = 0, device_type;
            cl_int cle;
    
            for (i = 0; i < FF_ARRAY_ELEMS(opencl_device_types); i++) {
                if (!strcmp(opencl_device_types[i].key, param->value)) {
                    match_type = opencl_device_types[i].type;
                    break;
                }
            }
            if (!match_type) {
                av_log(hwdev, AV_LOG_ERROR, "Unknown device type %s.\n",
                       param->value);
                return AVERROR(EINVAL);
            }
    
            cle = clGetDeviceInfo(device_id, CL_DEVICE_TYPE,
                                  sizeof(device_type), &device_type, NULL);
            if (cle != CL_SUCCESS) {
                av_log(hwdev, AV_LOG_ERROR, "Failed to query device type "
                       "of device \"%s\".\n", device_name);
                return AVERROR_UNKNOWN;
            }
    
            if (!(device_type & match_type)) {
                av_log(hwdev, AV_LOG_DEBUG, "device_type does not match.\n");
                return 1;
            }
        }
    
        for (i = 0; i < FF_ARRAY_ELEMS(opencl_device_params); i++) {
            param = av_dict_get(opts, opencl_device_params[i].key,
                                NULL, 0);
            if (!param)
                continue;
    
            str = opencl_get_device_string(device_id,
                                           opencl_device_params[i].name);
            if (!str) {
                av_log(hwdev, AV_LOG_ERROR, "Failed to query %s "
                       "of device \"%s\".\n",
                       opencl_device_params[i].key, device_name);
                return AVERROR_UNKNOWN;
            }
            if (!av_stristr(str, param->value)) {
                av_log(hwdev, AV_LOG_DEBUG, "%s does not match (\"%s\").\n",
                       param->key, str);
                ret = 1;
            }
            av_free(str);
        }
    
        return ret;
    }
    
    typedef struct OpenCLDeviceSelector {
        int platform_index;
        int device_index;
        void *context;
        int (*enumerate_platforms)(AVHWDeviceContext *hwdev,
                                   cl_uint *nb_platforms,
                                   cl_platform_id **platforms,
                                   void *context);
        int (*filter_platform)    (AVHWDeviceContext *hwdev,
                                   cl_platform_id platform_id,
                                   const char *platform_name,
                                   void *context);
        int (*enumerate_devices)  (AVHWDeviceContext *hwdev,
                                   cl_platform_id platform_id,
                                   const char *platform_name,
                                   cl_uint *nb_devices,
                                   cl_device_id **devices,
                                   void *context);
        int (*filter_device)      (AVHWDeviceContext *hwdev,
                                   cl_device_id device_id,
                                   const char *device_name,
                                   void *context);
    } OpenCLDeviceSelector;
    
    static int opencl_device_create_internal(AVHWDeviceContext *hwdev,
                                             const OpenCLDeviceSelector *selector,
                                             cl_context_properties *props)
    {
        cl_uint      nb_platforms;
        cl_platform_id *platforms = NULL;
        cl_platform_id  platform_id;
        cl_uint      nb_devices;
        cl_device_id   *devices = NULL;
        AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
        cl_int cle;
        cl_context_properties default_props[3];
        char *platform_name_src = NULL,
             *device_name_src   = NULL;
        int err, found, p, d;
    
        err = selector->enumerate_platforms(hwdev, &nb_platforms, &platforms,
                                            selector->context);
        if (err)
            return err;
    
        found = 0;
        for (p = 0; p < nb_platforms; p++) {
            const char *platform_name;
    
            if (selector->platform_index >= 0 &&
                selector->platform_index != p)
                continue;
    
            av_freep(&platform_name_src);
            platform_name_src = opencl_get_platform_string(platforms[p],
                                                               CL_PLATFORM_NAME);
            if (platform_name_src)
                platform_name = platform_name_src;
            else
                platform_name = "Unknown Platform";
    
            if (selector->filter_platform) {
                err = selector->filter_platform(hwdev, platforms[p],
                                                platform_name,
                                                selector->context);
                if (err < 0)
                    goto fail;
                if (err > 0)
                    continue;
            }
    
            err = opencl_enumerate_devices(hwdev, platforms[p], platform_name,
                                           &nb_devices, &devices,
                                           selector->context);
            if (err < 0)
                continue;
    
            for (d = 0; d < nb_devices; d++) {
                const char *device_name;
    
                if (selector->device_index >= 0 &&
                    selector->device_index != d)
                    continue;
    
                av_freep(&device_name_src);
                device_name_src = opencl_get_device_string(devices[d],
                                                               CL_DEVICE_NAME);
                if (device_name_src)
                    device_name = device_name_src;
                else
                    device_name = "Unknown Device";
    
                if (selector->filter_device) {
                    err = selector->filter_device(hwdev, devices[d],
                                                  device_name,
                                                  selector->context);
                    if (err < 0)
                        goto fail;
                    if (err > 0)
                        continue;
                }
    
                av_log(hwdev, AV_LOG_VERBOSE, "%d.%d: %s / %s\n", p, d,
                       platform_name, device_name);
    
                ++found;
                platform_id      = platforms[p];
                hwctx->device_id = devices[d];
            }
    
            av_freep(&devices);
        }
    
        if (found == 0) {
            av_log(hwdev, AV_LOG_ERROR, "No matching devices found.\n");
            err = AVERROR(ENODEV);
            goto fail;
        }
        if (found > 1) {
            av_log(hwdev, AV_LOG_ERROR, "More than one matching device found.\n");
            err = AVERROR(ENODEV);
            goto fail;
        }
    
        if (!props) {
            props = default_props;
            default_props[0] = CL_CONTEXT_PLATFORM;
            default_props[1] = (intptr_t)platform_id;
            default_props[2] = 0;
        } else {
            if (props[0] == CL_CONTEXT_PLATFORM && props[1] == 0)
                props[1] = (intptr_t)platform_id;
        }
    
        hwctx->context = clCreateContext(props, 1, &hwctx->device_id,
                                         &opencl_error_callback, hwdev, &cle);
        if (!hwctx->context) {
            av_log(hwdev, AV_LOG_ERROR, "Failed to create OpenCL context: "
                   "%d.\n", cle);
            err = AVERROR(ENODEV);
            goto fail;
        }
    
        hwdev->free = &opencl_device_free;
    
        err = 0;
    fail:
        av_freep(&platform_name_src);
        av_freep(&device_name_src);
        av_freep(&platforms);
        av_freep(&devices);
        return err;
    }
    
    static int opencl_device_create(AVHWDeviceContext *hwdev, const char *device,
                                    AVDictionary *opts, int flags)
    {
        OpenCLDeviceSelector selector = {
            .context = opts,
            .enumerate_platforms = &opencl_enumerate_platforms,
            .filter_platform     = &opencl_filter_platform,
            .enumerate_devices   = &opencl_enumerate_devices,
            .filter_device       = &opencl_filter_device,
        };
    
        if (device && device[0]) {
            // Match one or both indices for platform and device.
            int d = -1, p = -1, ret;
            if (device[0] == '.')
                ret = sscanf(device, ".%d", &d);
            else
                ret = sscanf(device, "%d.%d", &p, &d);
            if (ret < 1) {
                av_log(hwdev, AV_LOG_ERROR, "Invalid OpenCL platform/device "
                       "index specification \"%s\".\n", device);
                return AVERROR(EINVAL);
            }
            selector.platform_index = p;
            selector.device_index   = d;
        } else {
            selector.platform_index = -1;
            selector.device_index   = -1;
        }
    
        return opencl_device_create_internal(hwdev, &selector, NULL);
    }
    
    static int opencl_device_init(AVHWDeviceContext *hwdev)
    {
        AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
        OpenCLDeviceContext    *priv = hwdev->internal->priv;
        cl_int cle;
    
        if (hwctx->command_queue) {
            cle = clRetainCommandQueue(hwctx->command_queue);
            if (cle != CL_SUCCESS) {
                av_log(hwdev, AV_LOG_ERROR, "Failed to retain external "
                       "command queue: %d.\n", cle);
                return AVERROR(EIO);
            }
            priv->command_queue = hwctx->command_queue;
        } else {
            priv->command_queue = clCreateCommandQueue(hwctx->context,
                                                       hwctx->device_id,
                                                       0, &cle);
            if (!priv->command_queue) {
                av_log(hwdev, AV_LOG_ERROR, "Failed to create internal "
                       "command queue: %d.\n", cle);
                return AVERROR(EIO);
            }
        }
    
        cle = clGetDeviceInfo(hwctx->device_id, CL_DEVICE_PLATFORM,
                              sizeof(priv->platform_id), &priv->platform_id,
                              NULL);
        if (cle != CL_SUCCESS) {
            av_log(hwdev, AV_LOG_ERROR, "Failed to determine the OpenCL "
                   "platform containing the device.\n");
            return AVERROR(EIO);
        }
    
    
    #define CL_FUNC(name, desc) do {                                \
            if (fail)                                               \
                break;                                              \
            priv->name = clGetExtensionFunctionAddressForPlatform(  \
                priv->platform_id, #name);                          \
            if (!priv->name) {                                      \
                av_log(hwdev, AV_LOG_VERBOSE,                       \
                       desc " function not found (%s).\n", #name);  \
                fail = 1;                                           \
            } else {                                                \
                av_log(hwdev, AV_LOG_VERBOSE,                       \
                       desc " function found (%s).\n", #name);      \
            }                                                       \
        } while (0)
    
    
        {
            int fail = 0;
    
            CL_FUNC(clCreateImageFromFdINTEL,
    
                    "Beignet DRM to OpenCL image mapping");
    
                av_log(hwdev, AV_LOG_WARNING, "Beignet DRM to OpenCL "
                       "mapping not usable.\n");
                priv->beignet_drm_mapping_usable = 0;
    
                priv->beignet_drm_mapping_usable = 1;
    
    #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
        {
            size_t props_size;
            cl_context_properties *props = NULL;
            VADisplay va_display;
            const char *va_ext = "cl_intel_va_api_media_sharing";
            int i, fail = 0;
    
            if (!opencl_check_extension(hwdev, va_ext)) {
                av_log(hwdev, AV_LOG_VERBOSE, "The %s extension is "
                       "required for QSV to OpenCL mapping.\n", va_ext);
                goto no_qsv;
            }
    
            cle = clGetContextInfo(hwctx->context, CL_CONTEXT_PROPERTIES,
                                   0, NULL, &props_size);
            if (cle != CL_SUCCESS) {
                av_log(hwdev, AV_LOG_VERBOSE, "Failed to get context "
                       "properties: %d.\n", cle);
                goto no_qsv;
            }
            if (props_size == 0) {
                av_log(hwdev, AV_LOG_VERBOSE, "Media sharing must be "
                       "enabled on context creation to use QSV to "
                       "OpenCL mapping.\n");
                goto no_qsv;
            }
    
            props = av_malloc(props_size);
            if (!props)
                return AVERROR(ENOMEM);
    
            cle = clGetContextInfo(hwctx->context, CL_CONTEXT_PROPERTIES,
                                   props_size, props, NULL);
            if (cle != CL_SUCCESS) {
                av_log(hwdev, AV_LOG_VERBOSE, "Failed to get context "
                       "properties: %d.\n", cle);
                goto no_qsv;
            }
    
            va_display = NULL;
            for (i = 0; i < (props_size / sizeof(*props) - 1); i++) {
                if (props[i] == CL_CONTEXT_VA_API_DISPLAY_INTEL) {
                    va_display = (VADisplay)(intptr_t)props[i+1];
                    break;
                }
            }
            if (!va_display) {
                av_log(hwdev, AV_LOG_VERBOSE, "Media sharing must be "
                       "enabled on context creation to use QSV to "
                       "OpenCL mapping.\n");
                goto no_qsv;
            }
            if (!vaDisplayIsValid(va_display)) {
                av_log(hwdev, AV_LOG_VERBOSE, "A valid VADisplay is "
                       "required on context creation to use QSV to "
                       "OpenCL mapping.\n");
                goto no_qsv;
            }
    
            CL_FUNC(clCreateFromVA_APIMediaSurfaceINTEL,
                    "Intel QSV to OpenCL mapping");
            CL_FUNC(clEnqueueAcquireVA_APIMediaSurfacesINTEL,
                    "Intel QSV in OpenCL acquire");
            CL_FUNC(clEnqueueReleaseVA_APIMediaSurfacesINTEL,
                    "Intel QSV in OpenCL release");
    
            if (fail) {
            no_qsv:
                av_log(hwdev, AV_LOG_WARNING, "QSV to OpenCL mapping "
                       "not usable.\n");
                priv->qsv_mapping_usable = 0;
            } else {
                priv->qsv_mapping_usable = 1;
            }
            av_free(props);
        }
    #endif
    
    
    #if HAVE_OPENCL_DXVA2
        {
            int fail = 0;
    
            CL_FUNC(clCreateFromDX9MediaSurfaceKHR,
                    "DXVA2 to OpenCL mapping");
            CL_FUNC(clEnqueueAcquireDX9MediaSurfacesKHR,
                    "DXVA2 in OpenCL acquire");
            CL_FUNC(clEnqueueReleaseDX9MediaSurfacesKHR,
                    "DXVA2 in OpenCL release");
    
            if (fail) {
                av_log(hwdev, AV_LOG_WARNING, "DXVA2 to OpenCL mapping "
                       "not usable.\n");
                priv->dxva2_mapping_usable = 0;
            } else {
                priv->dx9_media_adapter_type = CL_ADAPTER_D3D9EX_KHR;
                priv->dxva2_mapping_usable = 1;
            }
        }
    #endif
    
    
    #if HAVE_OPENCL_D3D11
        {
            const char *d3d11_ext = "cl_khr_d3d11_sharing";
            const char *nv12_ext  = "cl_intel_d3d11_nv12_media_sharing";
            int fail = 0;
    
            if (!opencl_check_extension(hwdev, d3d11_ext)) {
                av_log(hwdev, AV_LOG_VERBOSE, "The %s extension is "
                       "required for D3D11 to OpenCL mapping.\n", d3d11_ext);
                fail = 1;
            } else if (!opencl_check_extension(hwdev, nv12_ext)) {
                av_log(hwdev, AV_LOG_VERBOSE, "The %s extension may be "
                       "required for D3D11 to OpenCL mapping.\n", nv12_ext);
                // Not fatal.
            }
    
            CL_FUNC(clCreateFromD3D11Texture2DKHR,
                    "D3D11 to OpenCL mapping");
            CL_FUNC(clEnqueueAcquireD3D11ObjectsKHR,
                    "D3D11 in OpenCL acquire");
            CL_FUNC(clEnqueueReleaseD3D11ObjectsKHR,
                    "D3D11 in OpenCL release");
    
            if (fail) {
                av_log(hwdev, AV_LOG_WARNING, "D3D11 to OpenCL mapping "
                       "not usable.\n");
                priv->d3d11_mapping_usable = 0;
            } else {
                priv->d3d11_mapping_usable = 1;
            }
        }
    #endif
    
    
    #if HAVE_OPENCL_DRM_ARM
        {
            const char *drm_arm_ext = "cl_arm_import_memory";
            const char *image_ext   = "cl_khr_image2d_from_buffer";
            int fail = 0;
    
            if (!opencl_check_extension(hwdev, drm_arm_ext)) {
                av_log(hwdev, AV_LOG_VERBOSE, "The %s extension is "
                       "required for DRM to OpenCL mapping on ARM.\n",
                       drm_arm_ext);
                fail = 1;
            }
            if (!opencl_check_extension(hwdev, image_ext)) {
                av_log(hwdev, AV_LOG_VERBOSE, "The %s extension is "
                       "required for DRM to OpenCL mapping on ARM.\n",
                       image_ext);
                fail = 1;
            }
    
            // clImportMemoryARM() is linked statically.
    
            if (fail) {
                av_log(hwdev, AV_LOG_WARNING, "DRM to OpenCL mapping on ARM "
                       "not usable.\n");
                priv->drm_arm_mapping_usable = 0;
            } else {
                priv->drm_arm_mapping_usable = 1;
            }
        }
    #endif
    
    
        return 0;
    }
    
    static void opencl_device_uninit(AVHWDeviceContext *hwdev)
    {
        OpenCLDeviceContext *priv = hwdev->internal->priv;
        cl_int cle;
    
        if (priv->command_queue) {
            cle = clReleaseCommandQueue(priv->command_queue);
            if (cle != CL_SUCCESS) {
                av_log(hwdev, AV_LOG_ERROR, "Failed to release internal "
                       "command queue reference: %d.\n", cle);
            }
    
    #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
    static int opencl_filter_intel_media_vaapi_platform(AVHWDeviceContext *hwdev,
                                                        cl_platform_id platform_id,
                                                        const char *platform_name,
                                                        void *context)
    {
        // This doesn't exist as a platform extension, so just test whether
        // the function we will use for device enumeration exists.
    
        if (!clGetExtensionFunctionAddressForPlatform(platform_id,
                "clGetDeviceIDsFromVA_APIMediaAdapterINTEL")) {
            av_log(hwdev, AV_LOG_DEBUG, "Platform %s does not export the "
                   "VAAPI device enumeration function.\n", platform_name);
            return 1;
        } else {
            return 0;
        }
    }
    
    static int opencl_enumerate_intel_media_vaapi_devices(AVHWDeviceContext *hwdev,
                                                          cl_platform_id platform_id,
                                                          const char *platform_name,
                                                          cl_uint *nb_devices,
                                                          cl_device_id **devices,
                                                          void *context)
    {
        VADisplay va_display = context;
        clGetDeviceIDsFromVA_APIMediaAdapterINTEL_fn
            clGetDeviceIDsFromVA_APIMediaAdapterINTEL;
        cl_int cle;
        int err;
    
        clGetDeviceIDsFromVA_APIMediaAdapterINTEL =
            clGetExtensionFunctionAddressForPlatform(platform_id,
                "clGetDeviceIDsFromVA_APIMediaAdapterINTEL");
        if (!clGetDeviceIDsFromVA_APIMediaAdapterINTEL) {
            av_log(hwdev, AV_LOG_ERROR, "Failed to get address of "
                   "clGetDeviceIDsFromVA_APIMediaAdapterINTEL().\n");
            return AVERROR_UNKNOWN;
        }
    
        cle = clGetDeviceIDsFromVA_APIMediaAdapterINTEL(
            platform_id, CL_VA_API_DISPLAY_INTEL, va_display,
            CL_PREFERRED_DEVICES_FOR_VA_API_INTEL, 0, NULL, nb_devices);
        if (cle == CL_DEVICE_NOT_FOUND) {
            av_log(hwdev, AV_LOG_DEBUG, "No VAAPI-supporting devices found "
                   "on platform \"%s\".\n", platform_name);
            *nb_devices = 0;
            return 0;
        } else if (cle != CL_SUCCESS) {
            av_log(hwdev, AV_LOG_ERROR, "Failed to get number of devices "
                   "on platform \"%s\": %d.\n", platform_name, cle);
            return AVERROR_UNKNOWN;
        }
    
        *devices = av_malloc_array(*nb_devices, sizeof(**devices));
        if (!*devices)
            return AVERROR(ENOMEM);
    
        cle = clGetDeviceIDsFromVA_APIMediaAdapterINTEL(
            platform_id, CL_VA_API_DISPLAY_INTEL, va_display,
            CL_PREFERRED_DEVICES_FOR_VA_API_INTEL, *nb_devices, *devices, NULL);
        if (cle != CL_SUCCESS) {
            av_log(hwdev, AV_LOG_ERROR, "Failed to get list of VAAPI-supporting "
                   "devices on platform \"%s\": %d.\n", platform_name, cle);
            av_freep(devices);
            return AVERROR_UNKNOWN;
        }
    
        return 0;
    }
    
    static int opencl_filter_intel_media_vaapi_device(AVHWDeviceContext *hwdev,
                                                      cl_device_id device_id,
                                                      const char *device_name,
                                                      void *context)
    {
        const char *va_ext = "cl_intel_va_api_media_sharing";
    
        if (opencl_check_device_extension(device_id, va_ext)) {
            return 0;
        } else {
            av_log(hwdev, AV_LOG_DEBUG, "Device %s does not support the "
                   "%s extension.\n", device_name, va_ext);
            return 1;
        }
    }
    #endif
    
    
    #if HAVE_OPENCL_DXVA2
    static int opencl_filter_dxva2_platform(AVHWDeviceContext *hwdev,
                                            cl_platform_id platform_id,
                                            const char *platform_name,
                                            void *context)
    {
        const char *dx9_ext = "cl_khr_dx9_media_sharing";
    
        if (opencl_check_platform_extension(platform_id, dx9_ext)) {
            return 0;
        } else {