FFmpeg  4.4.9
hwcontext_opencl.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #define CL_USE_DEPRECATED_OPENCL_1_2_APIS
20 
21 #include <string.h>
22 
23 #include "config.h"
24 
25 #include "avassert.h"
26 #include "avstring.h"
27 #include "common.h"
28 #include "hwcontext.h"
29 #include "hwcontext_internal.h"
30 #include "hwcontext_opencl.h"
31 #include "mem.h"
32 #include "pixdesc.h"
33 
34 #if HAVE_OPENCL_VAAPI_BEIGNET
35 #include <unistd.h>
36 #include <va/va.h>
37 #include <va/va_drmcommon.h>
38 #include <CL/cl_intel.h>
39 #include "hwcontext_vaapi.h"
40 #endif
41 
42 #if HAVE_OPENCL_DRM_BEIGNET
43 #include <unistd.h>
44 #include <CL/cl_intel.h>
45 #include "hwcontext_drm.h"
46 #endif
47 
48 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
49 #if CONFIG_LIBMFX
50 #include <mfx/mfxstructures.h>
51 #endif
52 #include <va/va.h>
53 #include <CL/cl_va_api_media_sharing_intel.h>
54 #include "hwcontext_vaapi.h"
55 #endif
56 
57 #if HAVE_OPENCL_DXVA2
58 #define COBJMACROS
59 #include <CL/cl_dx9_media_sharing.h>
60 #include <dxva2api.h>
61 #include "hwcontext_dxva2.h"
62 #endif
63 
64 #if HAVE_OPENCL_D3D11
65 #include <CL/cl_d3d11.h>
66 #include "hwcontext_d3d11va.h"
67 #endif
68 
69 #if HAVE_OPENCL_DRM_ARM
70 #include <CL/cl_ext.h>
71 #include <drm_fourcc.h>
72 #include "hwcontext_drm.h"
73 #endif
74 
75 
76 typedef struct OpenCLDeviceContext {
77  // Default command queue to use for transfer/mapping operations on
78  // the device. If the user supplies one, this is a reference to it.
79  // Otherwise, it is newly-created.
80  cl_command_queue command_queue;
81 
82  // The platform the context exists on. This is needed to query and
83  // retrieve extension functions.
84  cl_platform_id platform_id;
85 
86  // Platform/device-specific functions.
87 #if HAVE_OPENCL_DRM_BEIGNET
88  int beignet_drm_mapping_usable;
89  clCreateImageFromFdINTEL_fn clCreateImageFromFdINTEL;
90 #endif
91 
92 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
93  int qsv_mapping_usable;
94  clCreateFromVA_APIMediaSurfaceINTEL_fn
95  clCreateFromVA_APIMediaSurfaceINTEL;
96  clEnqueueAcquireVA_APIMediaSurfacesINTEL_fn
97  clEnqueueAcquireVA_APIMediaSurfacesINTEL;
98  clEnqueueReleaseVA_APIMediaSurfacesINTEL_fn
99  clEnqueueReleaseVA_APIMediaSurfacesINTEL;
100 #endif
101 
102 #if HAVE_OPENCL_DXVA2
103  int dxva2_mapping_usable;
104  cl_dx9_media_adapter_type_khr dx9_media_adapter_type;
105 
106  clCreateFromDX9MediaSurfaceKHR_fn
107  clCreateFromDX9MediaSurfaceKHR;
108  clEnqueueAcquireDX9MediaSurfacesKHR_fn
109  clEnqueueAcquireDX9MediaSurfacesKHR;
110  clEnqueueReleaseDX9MediaSurfacesKHR_fn
111  clEnqueueReleaseDX9MediaSurfacesKHR;
112 #endif
113 
114 #if HAVE_OPENCL_D3D11
115  int d3d11_mapping_usable;
116  clCreateFromD3D11Texture2DKHR_fn
117  clCreateFromD3D11Texture2DKHR;
118  clEnqueueAcquireD3D11ObjectsKHR_fn
119  clEnqueueAcquireD3D11ObjectsKHR;
120  clEnqueueReleaseD3D11ObjectsKHR_fn
121  clEnqueueReleaseD3D11ObjectsKHR;
122 #endif
123 
124 #if HAVE_OPENCL_DRM_ARM
125  int drm_arm_mapping_usable;
126 #endif
128 
129 typedef struct OpenCLFramesContext {
130  // Command queue used for transfer/mapping operations on this frames
131  // context. If the user supplies one, this is a reference to it.
132  // Otherwise, it is a reference to the default command queue for the
133  // device.
134  cl_command_queue command_queue;
135 
136 #if HAVE_OPENCL_DXVA2 || HAVE_OPENCL_D3D11
137  // For mapping APIs which have separate creation and acquire/release
138  // steps, this stores the OpenCL memory objects corresponding to each
139  // frame.
140  int nb_mapped_frames;
141  AVOpenCLFrameDescriptor *mapped_frames;
142 #endif
144 
145 
146 static void CL_CALLBACK opencl_error_callback(const char *errinfo,
147  const void *private_info,
148  size_t cb,
149  void *user_data)
150 {
152  av_log(ctx, AV_LOG_ERROR, "OpenCL error: %s\n", errinfo);
153 }
154 
156 {
157  AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
158  cl_int cle;
159 
160  cle = clReleaseContext(hwctx->context);
161  if (cle != CL_SUCCESS) {
162  av_log(hwdev, AV_LOG_ERROR, "Failed to release OpenCL "
163  "context: %d.\n", cle);
164  }
165 }
166 
167 static struct {
168  const char *key;
169  cl_platform_info name;
171  { "platform_profile", CL_PLATFORM_PROFILE },
172  { "platform_version", CL_PLATFORM_VERSION },
173  { "platform_name", CL_PLATFORM_NAME },
174  { "platform_vendor", CL_PLATFORM_VENDOR },
175  { "platform_extensions", CL_PLATFORM_EXTENSIONS },
176 };
177 
178 static struct {
179  const char *key;
180  cl_device_info name;
181 } opencl_device_params[] = {
182  { "device_name", CL_DEVICE_NAME },
183  { "device_vendor", CL_DEVICE_VENDOR },
184  { "driver_version", CL_DRIVER_VERSION },
185  { "device_version", CL_DEVICE_VERSION },
186  { "device_profile", CL_DEVICE_PROFILE },
187  { "device_extensions", CL_DEVICE_EXTENSIONS },
188 };
189 
190 static struct {
191  const char *key;
192  cl_device_type type;
193 } opencl_device_types[] = {
194  { "cpu", CL_DEVICE_TYPE_CPU },
195  { "gpu", CL_DEVICE_TYPE_GPU },
196  { "accelerator", CL_DEVICE_TYPE_ACCELERATOR },
197  { "custom", CL_DEVICE_TYPE_CUSTOM },
198  { "default", CL_DEVICE_TYPE_DEFAULT },
199  { "all", CL_DEVICE_TYPE_ALL },
200 };
201 
202 static char *opencl_get_platform_string(cl_platform_id platform_id,
203  cl_platform_info key)
204 {
205  char *str;
206  size_t size;
207  cl_int cle;
208  cle = clGetPlatformInfo(platform_id, key, 0, NULL, &size);
209  if (cle != CL_SUCCESS)
210  return NULL;
211  str = av_malloc(size);
212  if (!str)
213  return NULL;
214  cle = clGetPlatformInfo(platform_id, key, size, str, &size);
215  if (cle != CL_SUCCESS) {
216  av_free(str);
217  return NULL;
218  }
219  av_assert0(strlen(str) + 1 == size);
220  return str;
221 }
222 
223 static char *opencl_get_device_string(cl_device_id device_id,
224  cl_device_info key)
225 {
226  char *str;
227  size_t size;
228  cl_int cle;
229  cle = clGetDeviceInfo(device_id, key, 0, NULL, &size);
230  if (cle != CL_SUCCESS)
231  return NULL;
232  str = av_malloc(size);
233  if (!str)
234  return NULL;
235  cle = clGetDeviceInfo(device_id, key, size, str, &size);
236  if (cle != CL_SUCCESS) {
237  av_free(str);
238  return NULL;
239  }
240  av_assert0(strlen(str) + 1== size);
241  return str;
242 }
243 
244 static int opencl_check_platform_extension(cl_platform_id platform_id,
245  const char *name)
246 {
247  char *str;
248  int found = 0;
249  str = opencl_get_platform_string(platform_id,
250  CL_PLATFORM_EXTENSIONS);
251  if (str && strstr(str, name))
252  found = 1;
253  av_free(str);
254  return found;
255 }
256 
257 static int opencl_check_device_extension(cl_device_id device_id,
258  const char *name)
259 {
260  char *str;
261  int found = 0;
262  str = opencl_get_device_string(device_id,
263  CL_DEVICE_EXTENSIONS);
264  if (str && strstr(str, name))
265  found = 1;
266  av_free(str);
267  return found;
268 }
269 
271  const char *name)
272 {
273  AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
274  OpenCLDeviceContext *priv = hwdev->internal->priv;
275 
277  av_log(hwdev, AV_LOG_DEBUG,
278  "%s found as platform extension.\n", name);
279  return 1;
280  }
281 
283  av_log(hwdev, AV_LOG_DEBUG,
284  "%s found as device extension.\n", name);
285  return 1;
286  }
287 
288  return 0;
289 }
290 
292  cl_uint *nb_platforms,
293  cl_platform_id **platforms,
294  void *context)
295 {
296  cl_int cle;
297 
298  cle = clGetPlatformIDs(0, NULL, nb_platforms);
299  if (cle != CL_SUCCESS) {
300  av_log(hwdev, AV_LOG_ERROR, "Failed to get number of "
301  "OpenCL platforms: %d.\n", cle);
302  return AVERROR(ENODEV);
303  }
304  av_log(hwdev, AV_LOG_DEBUG, "%u OpenCL platforms found.\n",
305  *nb_platforms);
306 
307  *platforms = av_malloc_array(*nb_platforms, sizeof(**platforms));
308  if (!*platforms)
309  return AVERROR(ENOMEM);
310 
311  cle = clGetPlatformIDs(*nb_platforms, *platforms, NULL);
312  if (cle != CL_SUCCESS) {
313  av_log(hwdev, AV_LOG_ERROR, "Failed to get list of OpenCL "
314  "platforms: %d.\n", cle);
315  av_freep(platforms);
316  return AVERROR(ENODEV);
317  }
318 
319  return 0;
320 }
321 
323  cl_platform_id platform_id,
324  const char *platform_name,
325  void *context)
326 {
327  AVDictionary *opts = context;
328  const AVDictionaryEntry *param;
329  char *str;
330  int i, ret = 0;
331 
332  for (i = 0; i < FF_ARRAY_ELEMS(opencl_platform_params); i++) {
334  NULL, 0);
335  if (!param)
336  continue;
337 
338  str = opencl_get_platform_string(platform_id,
340  if (!str) {
341  av_log(hwdev, AV_LOG_ERROR, "Failed to query %s "
342  "of platform \"%s\".\n",
343  opencl_platform_params[i].key, platform_name);
344  return AVERROR_UNKNOWN;
345  }
346  if (!av_stristr(str, param->value)) {
347  av_log(hwdev, AV_LOG_DEBUG, "%s does not match (\"%s\").\n",
348  param->key, str);
349  ret = 1;
350  }
351  av_free(str);
352  }
353 
354  return ret;
355 }
356 
358  cl_platform_id platform_id,
359  const char *platform_name,
360  cl_uint *nb_devices,
361  cl_device_id **devices,
362  void *context)
363 {
364  cl_int cle;
365 
366  cle = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL,
367  0, NULL, nb_devices);
368  if (cle == CL_DEVICE_NOT_FOUND) {
369  av_log(hwdev, AV_LOG_DEBUG, "No devices found "
370  "on platform \"%s\".\n", platform_name);
371  *nb_devices = 0;
372  return 0;
373  } else if (cle != CL_SUCCESS) {
374  av_log(hwdev, AV_LOG_ERROR, "Failed to get number of devices "
375  "on platform \"%s\": %d.\n", platform_name, cle);
376  return AVERROR(ENODEV);
377  }
378  av_log(hwdev, AV_LOG_DEBUG, "%u OpenCL devices found on "
379  "platform \"%s\".\n", *nb_devices, platform_name);
380 
381  *devices = av_malloc_array(*nb_devices, sizeof(**devices));
382  if (!*devices)
383  return AVERROR(ENOMEM);
384 
385  cle = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL,
386  *nb_devices, *devices, NULL);
387  if (cle != CL_SUCCESS) {
388  av_log(hwdev, AV_LOG_ERROR, "Failed to get list of devices "
389  "on platform \"%s\": %d.\n", platform_name, cle);
390  av_freep(devices);
391  return AVERROR(ENODEV);
392  }
393 
394  return 0;
395 }
396 
398  cl_device_id device_id,
399  const char *device_name,
400  void *context)
401 {
402  AVDictionary *opts = context;
403  const AVDictionaryEntry *param;
404  char *str;
405  int i, ret = 0;
406 
407  param = av_dict_get(opts, "device_type", NULL, 0);
408  if (param) {
409  cl_device_type match_type = 0, device_type;
410  cl_int cle;
411 
412  for (i = 0; i < FF_ARRAY_ELEMS(opencl_device_types); i++) {
413  if (!strcmp(opencl_device_types[i].key, param->value)) {
414  match_type = opencl_device_types[i].type;
415  break;
416  }
417  }
418  if (!match_type) {
419  av_log(hwdev, AV_LOG_ERROR, "Unknown device type %s.\n",
420  param->value);
421  return AVERROR(EINVAL);
422  }
423 
424  cle = clGetDeviceInfo(device_id, CL_DEVICE_TYPE,
425  sizeof(device_type), &device_type, NULL);
426  if (cle != CL_SUCCESS) {
427  av_log(hwdev, AV_LOG_ERROR, "Failed to query device type "
428  "of device \"%s\".\n", device_name);
429  return AVERROR_UNKNOWN;
430  }
431 
432  if (!(device_type & match_type)) {
433  av_log(hwdev, AV_LOG_DEBUG, "device_type does not match.\n");
434  return 1;
435  }
436  }
437 
438  for (i = 0; i < FF_ARRAY_ELEMS(opencl_device_params); i++) {
440  NULL, 0);
441  if (!param)
442  continue;
443 
444  str = opencl_get_device_string(device_id,
446  if (!str) {
447  av_log(hwdev, AV_LOG_ERROR, "Failed to query %s "
448  "of device \"%s\".\n",
449  opencl_device_params[i].key, device_name);
450  return AVERROR_UNKNOWN;
451  }
452  if (!av_stristr(str, param->value)) {
453  av_log(hwdev, AV_LOG_DEBUG, "%s does not match (\"%s\").\n",
454  param->key, str);
455  ret = 1;
456  }
457  av_free(str);
458  }
459 
460  return ret;
461 }
462 
463 typedef struct OpenCLDeviceSelector {
466  void *context;
468  cl_uint *nb_platforms,
469  cl_platform_id **platforms,
470  void *context);
472  cl_platform_id platform_id,
473  const char *platform_name,
474  void *context);
476  cl_platform_id platform_id,
477  const char *platform_name,
478  cl_uint *nb_devices,
479  cl_device_id **devices,
480  void *context);
482  cl_device_id device_id,
483  const char *device_name,
484  void *context);
486 
488  const OpenCLDeviceSelector *selector,
489  cl_context_properties *props)
490 {
491  cl_uint nb_platforms;
492  cl_platform_id *platforms = NULL;
493  cl_platform_id platform_id;
494  cl_uint nb_devices;
495  cl_device_id *devices = NULL;
496  AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
497  cl_int cle;
498  cl_context_properties default_props[3];
499  char *platform_name_src = NULL,
500  *device_name_src = NULL;
501  int err, found, p, d;
502 
503  av_assert0(selector->enumerate_platforms &&
504  selector->enumerate_devices);
505 
506  err = selector->enumerate_platforms(hwdev, &nb_platforms, &platforms,
507  selector->context);
508  if (err)
509  return err;
510 
511  found = 0;
512  for (p = 0; p < nb_platforms; p++) {
513  const char *platform_name;
514 
515  if (selector->platform_index >= 0 &&
516  selector->platform_index != p)
517  continue;
518 
519  av_freep(&platform_name_src);
520  platform_name_src = opencl_get_platform_string(platforms[p],
521  CL_PLATFORM_NAME);
522  if (platform_name_src)
523  platform_name = platform_name_src;
524  else
525  platform_name = "Unknown Platform";
526 
527  if (selector->filter_platform) {
528  err = selector->filter_platform(hwdev, platforms[p],
529  platform_name,
530  selector->context);
531  if (err < 0)
532  goto fail;
533  if (err > 0)
534  continue;
535  }
536 
537  err = selector->enumerate_devices(hwdev, platforms[p], platform_name,
538  &nb_devices, &devices,
539  selector->context);
540  if (err < 0)
541  continue;
542 
543  for (d = 0; d < nb_devices; d++) {
544  const char *device_name;
545 
546  if (selector->device_index >= 0 &&
547  selector->device_index != d)
548  continue;
549 
550  av_freep(&device_name_src);
551  device_name_src = opencl_get_device_string(devices[d],
552  CL_DEVICE_NAME);
553  if (device_name_src)
554  device_name = device_name_src;
555  else
556  device_name = "Unknown Device";
557 
558  if (selector->filter_device) {
559  err = selector->filter_device(hwdev, devices[d],
560  device_name,
561  selector->context);
562  if (err < 0)
563  goto fail;
564  if (err > 0)
565  continue;
566  }
567 
568  av_log(hwdev, AV_LOG_VERBOSE, "%d.%d: %s / %s\n", p, d,
569  platform_name, device_name);
570 
571  ++found;
572  platform_id = platforms[p];
573  hwctx->device_id = devices[d];
574  }
575 
576  av_freep(&devices);
577  }
578 
579  if (found == 0) {
580  av_log(hwdev, AV_LOG_ERROR, "No matching devices found.\n");
581  err = AVERROR(ENODEV);
582  goto fail;
583  }
584  if (found > 1) {
585  av_log(hwdev, AV_LOG_ERROR, "More than one matching device found.\n");
586  err = AVERROR(ENODEV);
587  goto fail;
588  }
589 
590  if (!props) {
591  props = default_props;
592  default_props[0] = CL_CONTEXT_PLATFORM;
593  default_props[1] = (intptr_t)platform_id;
594  default_props[2] = 0;
595  } else {
596  if (props[0] == CL_CONTEXT_PLATFORM && props[1] == 0)
597  props[1] = (intptr_t)platform_id;
598  }
599 
600  hwctx->context = clCreateContext(props, 1, &hwctx->device_id,
601  &opencl_error_callback, hwdev, &cle);
602  if (!hwctx->context) {
603  av_log(hwdev, AV_LOG_ERROR, "Failed to create OpenCL context: "
604  "%d.\n", cle);
605  err = AVERROR(ENODEV);
606  goto fail;
607  }
608 
609  hwdev->free = &opencl_device_free;
610 
611  err = 0;
612 fail:
613  av_freep(&platform_name_src);
614  av_freep(&device_name_src);
615  av_freep(&platforms);
616  av_freep(&devices);
617  return err;
618 }
619 
620 static int opencl_device_create(AVHWDeviceContext *hwdev, const char *device,
621  AVDictionary *opts, int flags)
622 {
623  OpenCLDeviceSelector selector = {
624  .context = opts,
625  .enumerate_platforms = &opencl_enumerate_platforms,
626  .filter_platform = &opencl_filter_platform,
627  .enumerate_devices = &opencl_enumerate_devices,
628  .filter_device = &opencl_filter_device,
629  };
630 
631  if (device && device[0]) {
632  // Match one or both indices for platform and device.
633  int d = -1, p = -1, ret;
634  if (device[0] == '.')
635  ret = sscanf(device, ".%d", &d);
636  else
637  ret = sscanf(device, "%d.%d", &p, &d);
638  if (ret < 1) {
639  av_log(hwdev, AV_LOG_ERROR, "Invalid OpenCL platform/device "
640  "index specification \"%s\".\n", device);
641  return AVERROR(EINVAL);
642  }
643  selector.platform_index = p;
644  selector.device_index = d;
645  } else {
646  selector.platform_index = -1;
647  selector.device_index = -1;
648  }
649 
650  return opencl_device_create_internal(hwdev, &selector, NULL);
651 }
652 
654 {
655  AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
656  OpenCLDeviceContext *priv = hwdev->internal->priv;
657  cl_int cle;
658 
659  if (hwctx->command_queue) {
660  cle = clRetainCommandQueue(hwctx->command_queue);
661  if (cle != CL_SUCCESS) {
662  av_log(hwdev, AV_LOG_ERROR, "Failed to retain external "
663  "command queue: %d.\n", cle);
664  return AVERROR(EIO);
665  }
666  priv->command_queue = hwctx->command_queue;
667  } else {
668  priv->command_queue = clCreateCommandQueue(hwctx->context,
669  hwctx->device_id,
670  0, &cle);
671  if (!priv->command_queue) {
672  av_log(hwdev, AV_LOG_ERROR, "Failed to create internal "
673  "command queue: %d.\n", cle);
674  return AVERROR(EIO);
675  }
676  }
677 
678  cle = clGetDeviceInfo(hwctx->device_id, CL_DEVICE_PLATFORM,
679  sizeof(priv->platform_id), &priv->platform_id,
680  NULL);
681  if (cle != CL_SUCCESS) {
682  av_log(hwdev, AV_LOG_ERROR, "Failed to determine the OpenCL "
683  "platform containing the device.\n");
684  return AVERROR(EIO);
685  }
686 
687 #define CL_FUNC(name, desc) do { \
688  if (fail) \
689  break; \
690  priv->name = clGetExtensionFunctionAddressForPlatform( \
691  priv->platform_id, #name); \
692  if (!priv->name) { \
693  av_log(hwdev, AV_LOG_VERBOSE, \
694  desc " function not found (%s).\n", #name); \
695  fail = 1; \
696  } else { \
697  av_log(hwdev, AV_LOG_VERBOSE, \
698  desc " function found (%s).\n", #name); \
699  } \
700  } while (0)
701 
702 #if HAVE_OPENCL_DRM_BEIGNET
703  {
704  int fail = 0;
705 
706  CL_FUNC(clCreateImageFromFdINTEL,
707  "Beignet DRM to OpenCL image mapping");
708 
709  if (fail) {
710  av_log(hwdev, AV_LOG_WARNING, "Beignet DRM to OpenCL "
711  "mapping not usable.\n");
712  priv->beignet_drm_mapping_usable = 0;
713  } else {
714  priv->beignet_drm_mapping_usable = 1;
715  }
716  }
717 #endif
718 
719 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
720  {
721  size_t props_size;
722  cl_context_properties *props = NULL;
723  VADisplay va_display;
724  const char *va_ext = "cl_intel_va_api_media_sharing";
725  int i, fail = 0;
726 
727  if (!opencl_check_extension(hwdev, va_ext)) {
728  av_log(hwdev, AV_LOG_VERBOSE, "The %s extension is "
729  "required for QSV to OpenCL mapping.\n", va_ext);
730  goto no_qsv;
731  }
732 
733  cle = clGetContextInfo(hwctx->context, CL_CONTEXT_PROPERTIES,
734  0, NULL, &props_size);
735  if (cle != CL_SUCCESS) {
736  av_log(hwdev, AV_LOG_VERBOSE, "Failed to get context "
737  "properties: %d.\n", cle);
738  goto no_qsv;
739  }
740  if (props_size == 0) {
741  av_log(hwdev, AV_LOG_VERBOSE, "Media sharing must be "
742  "enabled on context creation to use QSV to "
743  "OpenCL mapping.\n");
744  goto no_qsv;
745  }
746 
747  props = av_malloc(props_size);
748  if (!props)
749  return AVERROR(ENOMEM);
750 
751  cle = clGetContextInfo(hwctx->context, CL_CONTEXT_PROPERTIES,
752  props_size, props, NULL);
753  if (cle != CL_SUCCESS) {
754  av_log(hwdev, AV_LOG_VERBOSE, "Failed to get context "
755  "properties: %d.\n", cle);
756  goto no_qsv;
757  }
758 
759  va_display = NULL;
760  for (i = 0; i < (props_size / sizeof(*props) - 1); i++) {
761  if (props[i] == CL_CONTEXT_VA_API_DISPLAY_INTEL) {
762  va_display = (VADisplay)(intptr_t)props[i+1];
763  break;
764  }
765  }
766  if (!va_display) {
767  av_log(hwdev, AV_LOG_VERBOSE, "Media sharing must be "
768  "enabled on context creation to use QSV to "
769  "OpenCL mapping.\n");
770  goto no_qsv;
771  }
772  if (!vaDisplayIsValid(va_display)) {
773  av_log(hwdev, AV_LOG_VERBOSE, "A valid VADisplay is "
774  "required on context creation to use QSV to "
775  "OpenCL mapping.\n");
776  goto no_qsv;
777  }
778 
779  CL_FUNC(clCreateFromVA_APIMediaSurfaceINTEL,
780  "Intel QSV to OpenCL mapping");
781  CL_FUNC(clEnqueueAcquireVA_APIMediaSurfacesINTEL,
782  "Intel QSV in OpenCL acquire");
783  CL_FUNC(clEnqueueReleaseVA_APIMediaSurfacesINTEL,
784  "Intel QSV in OpenCL release");
785 
786  if (fail) {
787  no_qsv:
788  av_log(hwdev, AV_LOG_WARNING, "QSV to OpenCL mapping "
789  "not usable.\n");
790  priv->qsv_mapping_usable = 0;
791  } else {
792  priv->qsv_mapping_usable = 1;
793  }
794  av_free(props);
795  }
796 #endif
797 
798 #if HAVE_OPENCL_DXVA2
799  {
800  int fail = 0;
801 
802  CL_FUNC(clCreateFromDX9MediaSurfaceKHR,
803  "DXVA2 to OpenCL mapping");
804  CL_FUNC(clEnqueueAcquireDX9MediaSurfacesKHR,
805  "DXVA2 in OpenCL acquire");
806  CL_FUNC(clEnqueueReleaseDX9MediaSurfacesKHR,
807  "DXVA2 in OpenCL release");
808 
809  if (fail) {
810  av_log(hwdev, AV_LOG_WARNING, "DXVA2 to OpenCL mapping "
811  "not usable.\n");
812  priv->dxva2_mapping_usable = 0;
813  } else {
814  priv->dx9_media_adapter_type = CL_ADAPTER_D3D9EX_KHR;
815  priv->dxva2_mapping_usable = 1;
816  }
817  }
818 #endif
819 
820 #if HAVE_OPENCL_D3D11
821  {
822  const char *d3d11_ext = "cl_khr_d3d11_sharing";
823  const char *nv12_ext = "cl_intel_d3d11_nv12_media_sharing";
824  int fail = 0;
825 
826  if (!opencl_check_extension(hwdev, d3d11_ext)) {
827  av_log(hwdev, AV_LOG_VERBOSE, "The %s extension is "
828  "required for D3D11 to OpenCL mapping.\n", d3d11_ext);
829  fail = 1;
830  } else if (!opencl_check_extension(hwdev, nv12_ext)) {
831  av_log(hwdev, AV_LOG_VERBOSE, "The %s extension may be "
832  "required for D3D11 to OpenCL mapping.\n", nv12_ext);
833  // Not fatal.
834  }
835 
836  CL_FUNC(clCreateFromD3D11Texture2DKHR,
837  "D3D11 to OpenCL mapping");
838  CL_FUNC(clEnqueueAcquireD3D11ObjectsKHR,
839  "D3D11 in OpenCL acquire");
840  CL_FUNC(clEnqueueReleaseD3D11ObjectsKHR,
841  "D3D11 in OpenCL release");
842 
843  if (fail) {
844  av_log(hwdev, AV_LOG_WARNING, "D3D11 to OpenCL mapping "
845  "not usable.\n");
846  priv->d3d11_mapping_usable = 0;
847  } else {
848  priv->d3d11_mapping_usable = 1;
849  }
850  }
851 #endif
852 
853 #if HAVE_OPENCL_DRM_ARM
854  {
855  const char *drm_arm_ext = "cl_arm_import_memory";
856  const char *image_ext = "cl_khr_image2d_from_buffer";
857  int fail = 0;
858 
859  if (!opencl_check_extension(hwdev, drm_arm_ext)) {
860  av_log(hwdev, AV_LOG_VERBOSE, "The %s extension is "
861  "required for DRM to OpenCL mapping on ARM.\n",
862  drm_arm_ext);
863  fail = 1;
864  }
865  if (!opencl_check_extension(hwdev, image_ext)) {
866  av_log(hwdev, AV_LOG_VERBOSE, "The %s extension is "
867  "required for DRM to OpenCL mapping on ARM.\n",
868  image_ext);
869  fail = 1;
870  }
871 
872  // clImportMemoryARM() is linked statically.
873 
874  if (fail) {
875  av_log(hwdev, AV_LOG_WARNING, "DRM to OpenCL mapping on ARM "
876  "not usable.\n");
877  priv->drm_arm_mapping_usable = 0;
878  } else {
879  priv->drm_arm_mapping_usable = 1;
880  }
881  }
882 #endif
883 
884 #undef CL_FUNC
885 
886  return 0;
887 }
888 
890 {
891  OpenCLDeviceContext *priv = hwdev->internal->priv;
892  cl_int cle;
893 
894  if (priv->command_queue) {
895  cle = clReleaseCommandQueue(priv->command_queue);
896  if (cle != CL_SUCCESS) {
897  av_log(hwdev, AV_LOG_ERROR, "Failed to release internal "
898  "command queue reference: %d.\n", cle);
899  }
900  priv->command_queue = NULL;
901  }
902 }
903 
904 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
905 static int opencl_filter_intel_media_vaapi_platform(AVHWDeviceContext *hwdev,
906  cl_platform_id platform_id,
907  const char *platform_name,
908  void *context)
909 {
910  // This doesn't exist as a platform extension, so just test whether
911  // the function we will use for device enumeration exists.
912 
913  if (!clGetExtensionFunctionAddressForPlatform(platform_id,
914  "clGetDeviceIDsFromVA_APIMediaAdapterINTEL")) {
915  av_log(hwdev, AV_LOG_DEBUG, "Platform %s does not export the "
916  "VAAPI device enumeration function.\n", platform_name);
917  return 1;
918  } else {
919  return 0;
920  }
921 }
922 
923 static int opencl_enumerate_intel_media_vaapi_devices(AVHWDeviceContext *hwdev,
924  cl_platform_id platform_id,
925  const char *platform_name,
926  cl_uint *nb_devices,
927  cl_device_id **devices,
928  void *context)
929 {
930  VADisplay va_display = context;
931  clGetDeviceIDsFromVA_APIMediaAdapterINTEL_fn
932  clGetDeviceIDsFromVA_APIMediaAdapterINTEL;
933  cl_int cle;
934 
935  clGetDeviceIDsFromVA_APIMediaAdapterINTEL =
936  clGetExtensionFunctionAddressForPlatform(platform_id,
937  "clGetDeviceIDsFromVA_APIMediaAdapterINTEL");
938  if (!clGetDeviceIDsFromVA_APIMediaAdapterINTEL) {
939  av_log(hwdev, AV_LOG_ERROR, "Failed to get address of "
940  "clGetDeviceIDsFromVA_APIMediaAdapterINTEL().\n");
941  return AVERROR_UNKNOWN;
942  }
943 
944  cle = clGetDeviceIDsFromVA_APIMediaAdapterINTEL(
945  platform_id, CL_VA_API_DISPLAY_INTEL, va_display,
946  CL_PREFERRED_DEVICES_FOR_VA_API_INTEL, 0, NULL, nb_devices);
947  if (cle == CL_DEVICE_NOT_FOUND) {
948  av_log(hwdev, AV_LOG_DEBUG, "No VAAPI-supporting devices found "
949  "on platform \"%s\".\n", platform_name);
950  *nb_devices = 0;
951  return 0;
952  } else if (cle != CL_SUCCESS) {
953  av_log(hwdev, AV_LOG_ERROR, "Failed to get number of devices "
954  "on platform \"%s\": %d.\n", platform_name, cle);
955  return AVERROR_UNKNOWN;
956  }
957 
958  *devices = av_malloc_array(*nb_devices, sizeof(**devices));
959  if (!*devices)
960  return AVERROR(ENOMEM);
961 
962  cle = clGetDeviceIDsFromVA_APIMediaAdapterINTEL(
963  platform_id, CL_VA_API_DISPLAY_INTEL, va_display,
964  CL_PREFERRED_DEVICES_FOR_VA_API_INTEL, *nb_devices, *devices, NULL);
965  if (cle != CL_SUCCESS) {
966  av_log(hwdev, AV_LOG_ERROR, "Failed to get list of VAAPI-supporting "
967  "devices on platform \"%s\": %d.\n", platform_name, cle);
968  av_freep(devices);
969  return AVERROR_UNKNOWN;
970  }
971 
972  return 0;
973 }
974 
975 static int opencl_filter_intel_media_vaapi_device(AVHWDeviceContext *hwdev,
976  cl_device_id device_id,
977  const char *device_name,
978  void *context)
979 {
980  const char *va_ext = "cl_intel_va_api_media_sharing";
981 
982  if (opencl_check_device_extension(device_id, va_ext)) {
983  return 0;
984  } else {
985  av_log(hwdev, AV_LOG_DEBUG, "Device %s does not support the "
986  "%s extension.\n", device_name, va_ext);
987  return 1;
988  }
989 }
990 #endif
991 
992 #if HAVE_OPENCL_DXVA2
993 static int opencl_filter_dxva2_platform(AVHWDeviceContext *hwdev,
994  cl_platform_id platform_id,
995  const char *platform_name,
996  void *context)
997 {
998  const char *dx9_ext = "cl_khr_dx9_media_sharing";
999 
1000  if (opencl_check_platform_extension(platform_id, dx9_ext)) {
1001  return 0;
1002  } else {
1003  av_log(hwdev, AV_LOG_DEBUG, "Platform %s does not support the "
1004  "%s extension.\n", platform_name, dx9_ext);
1005  return 1;
1006  }
1007 }
1008 
1009 static int opencl_enumerate_dxva2_devices(AVHWDeviceContext *hwdev,
1010  cl_platform_id platform_id,
1011  const char *platform_name,
1012  cl_uint *nb_devices,
1013  cl_device_id **devices,
1014  void *context)
1015 {
1016  IDirect3DDevice9 *device = context;
1017  clGetDeviceIDsFromDX9MediaAdapterKHR_fn
1018  clGetDeviceIDsFromDX9MediaAdapterKHR;
1019  cl_dx9_media_adapter_type_khr media_adapter_type = CL_ADAPTER_D3D9EX_KHR;
1020  cl_int cle;
1021 
1022  clGetDeviceIDsFromDX9MediaAdapterKHR =
1023  clGetExtensionFunctionAddressForPlatform(platform_id,
1024  "clGetDeviceIDsFromDX9MediaAdapterKHR");
1025  if (!clGetDeviceIDsFromDX9MediaAdapterKHR) {
1026  av_log(hwdev, AV_LOG_ERROR, "Failed to get address of "
1027  "clGetDeviceIDsFromDX9MediaAdapterKHR().\n");
1028  return AVERROR_UNKNOWN;
1029  }
1030 
1031  cle = clGetDeviceIDsFromDX9MediaAdapterKHR(
1032  platform_id, 1, &media_adapter_type, (void**)&device,
1033  CL_PREFERRED_DEVICES_FOR_DX9_MEDIA_ADAPTER_KHR,
1034  0, NULL, nb_devices);
1035  if (cle == CL_DEVICE_NOT_FOUND) {
1036  av_log(hwdev, AV_LOG_DEBUG, "No DXVA2-supporting devices found "
1037  "on platform \"%s\".\n", platform_name);
1038  *nb_devices = 0;
1039  return 0;
1040  } else if (cle != CL_SUCCESS) {
1041  av_log(hwdev, AV_LOG_ERROR, "Failed to get number of devices "
1042  "on platform \"%s\": %d.\n", platform_name, cle);
1043  return AVERROR_UNKNOWN;
1044  }
1045 
1046  *devices = av_malloc_array(*nb_devices, sizeof(**devices));
1047  if (!*devices)
1048  return AVERROR(ENOMEM);
1049 
1050  cle = clGetDeviceIDsFromDX9MediaAdapterKHR(
1051  platform_id, 1, &media_adapter_type, (void**)&device,
1052  CL_PREFERRED_DEVICES_FOR_DX9_MEDIA_ADAPTER_KHR,
1053  *nb_devices, *devices, NULL);
1054  if (cle != CL_SUCCESS) {
1055  av_log(hwdev, AV_LOG_ERROR, "Failed to get list of DXVA2-supporting "
1056  "devices on platform \"%s\": %d.\n", platform_name, cle);
1057  av_freep(devices);
1058  return AVERROR_UNKNOWN;
1059  }
1060 
1061  return 0;
1062 }
1063 #endif
1064 
1065 #if HAVE_OPENCL_D3D11
1066 static int opencl_filter_d3d11_platform(AVHWDeviceContext *hwdev,
1067  cl_platform_id platform_id,
1068  const char *platform_name,
1069  void *context)
1070 {
1071  const char *d3d11_ext = "cl_khr_d3d11_sharing";
1072 
1073  if (opencl_check_platform_extension(platform_id, d3d11_ext)) {
1074  return 0;
1075  } else {
1076  av_log(hwdev, AV_LOG_DEBUG, "Platform %s does not support the "
1077  "%s extension.\n", platform_name, d3d11_ext);
1078  return 1;
1079  }
1080 }
1081 
1082 static int opencl_enumerate_d3d11_devices(AVHWDeviceContext *hwdev,
1083  cl_platform_id platform_id,
1084  const char *platform_name,
1085  cl_uint *nb_devices,
1086  cl_device_id **devices,
1087  void *context)
1088 {
1089  ID3D11Device *device = context;
1090  clGetDeviceIDsFromD3D11KHR_fn clGetDeviceIDsFromD3D11KHR;
1091  cl_int cle;
1092 
1093  clGetDeviceIDsFromD3D11KHR =
1094  clGetExtensionFunctionAddressForPlatform(platform_id,
1095  "clGetDeviceIDsFromD3D11KHR");
1096  if (!clGetDeviceIDsFromD3D11KHR) {
1097  av_log(hwdev, AV_LOG_ERROR, "Failed to get address of "
1098  "clGetDeviceIDsFromD3D11KHR().\n");
1099  return AVERROR_UNKNOWN;
1100  }
1101 
1102  cle = clGetDeviceIDsFromD3D11KHR(platform_id,
1103  CL_D3D11_DEVICE_KHR, device,
1104  CL_PREFERRED_DEVICES_FOR_D3D11_KHR,
1105  0, NULL, nb_devices);
1106  if (cle == CL_DEVICE_NOT_FOUND) {
1107  av_log(hwdev, AV_LOG_DEBUG, "No D3D11-supporting devices found "
1108  "on platform \"%s\".\n", platform_name);
1109  *nb_devices = 0;
1110  return 0;
1111  } else if (cle != CL_SUCCESS) {
1112  av_log(hwdev, AV_LOG_ERROR, "Failed to get number of devices "
1113  "on platform \"%s\": %d.\n", platform_name, cle);
1114  return AVERROR_UNKNOWN;
1115  }
1116 
1117  *devices = av_malloc_array(*nb_devices, sizeof(**devices));
1118  if (!*devices)
1119  return AVERROR(ENOMEM);
1120 
1121  cle = clGetDeviceIDsFromD3D11KHR(platform_id,
1122  CL_D3D11_DEVICE_KHR, device,
1123  CL_PREFERRED_DEVICES_FOR_D3D11_KHR,
1124  *nb_devices, *devices, NULL);
1125  if (cle != CL_SUCCESS) {
1126  av_log(hwdev, AV_LOG_ERROR, "Failed to get list of D3D11-supporting "
1127  "devices on platform \"%s\": %d.\n", platform_name, cle);
1128  av_freep(devices);
1129  return AVERROR_UNKNOWN;
1130  }
1131 
1132  return 0;
1133 }
1134 #endif
1135 
1136 #if HAVE_OPENCL_DXVA2 || HAVE_OPENCL_D3D11
1137 static int opencl_filter_gpu_device(AVHWDeviceContext *hwdev,
1138  cl_device_id device_id,
1139  const char *device_name,
1140  void *context)
1141 {
1142  cl_device_type device_type;
1143  cl_int cle;
1144 
1145  cle = clGetDeviceInfo(device_id, CL_DEVICE_TYPE,
1146  sizeof(device_type), &device_type, NULL);
1147  if (cle != CL_SUCCESS) {
1148  av_log(hwdev, AV_LOG_ERROR, "Failed to query device type "
1149  "of device \"%s\".\n", device_name);
1150  return AVERROR_UNKNOWN;
1151  }
1152  if (!(device_type & CL_DEVICE_TYPE_GPU)) {
1153  av_log(hwdev, AV_LOG_DEBUG, "Device %s skipped (not GPU).\n",
1154  device_name);
1155  return 1;
1156  }
1157 
1158  return 0;
1159 }
1160 #endif
1161 
1162 #if HAVE_OPENCL_DRM_ARM
1163 static int opencl_filter_drm_arm_platform(AVHWDeviceContext *hwdev,
1164  cl_platform_id platform_id,
1165  const char *platform_name,
1166  void *context)
1167 {
1168  const char *drm_arm_ext = "cl_arm_import_memory";
1169 
1170  if (opencl_check_platform_extension(platform_id, drm_arm_ext)) {
1171  return 0;
1172  } else {
1173  av_log(hwdev, AV_LOG_DEBUG, "Platform %s does not support the "
1174  "%s extension.\n", platform_name, drm_arm_ext);
1175  return 1;
1176  }
1177 }
1178 
1179 static int opencl_filter_drm_arm_device(AVHWDeviceContext *hwdev,
1180  cl_device_id device_id,
1181  const char *device_name,
1182  void *context)
1183 {
1184  const char *drm_arm_ext = "cl_arm_import_memory";
1185 
1186  if (opencl_check_device_extension(device_id, drm_arm_ext)) {
1187  return 0;
1188  } else {
1189  av_log(hwdev, AV_LOG_DEBUG, "Device %s does not support the "
1190  "%s extension.\n", device_name, drm_arm_ext);
1191  return 1;
1192  }
1193 }
1194 #endif
1195 
1197  AVHWDeviceContext *src_ctx, AVDictionary *opts,
1198  int flags)
1199 {
1200  int err;
1201  switch (src_ctx->type) {
1202 
1203 #if HAVE_OPENCL_DRM_BEIGNET
1204  case AV_HWDEVICE_TYPE_DRM:
1206  {
1207  // Surface mapping works via DRM PRIME fds with no special
1208  // initialisation required in advance. This just finds the
1209  // Beignet ICD by name.
1210  AVDictionary *selector_opts = NULL;
1211 
1212  err = av_dict_set(&selector_opts, "platform_vendor", "Intel", 0);
1213  if (err >= 0)
1214  err = av_dict_set(&selector_opts, "platform_version", "beignet", 0);
1215  if (err >= 0) {
1216  OpenCLDeviceSelector selector = {
1217  .platform_index = -1,
1218  .device_index = 0,
1219  .context = selector_opts,
1220  .enumerate_platforms = &opencl_enumerate_platforms,
1221  .filter_platform = &opencl_filter_platform,
1222  .enumerate_devices = &opencl_enumerate_devices,
1223  .filter_device = NULL,
1224  };
1225  err = opencl_device_create_internal(hwdev, &selector, NULL);
1226  }
1227  av_dict_free(&selector_opts);
1228  }
1229  break;
1230 #endif
1231 
1232 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
1233  // The generic code automatically attempts to derive from all
1234  // ancestors of the given device, so we can ignore QSV devices here
1235  // and just consider the inner VAAPI device it was derived from.
1237  {
1238  AVVAAPIDeviceContext *src_hwctx = src_ctx->hwctx;
1239  cl_context_properties props[7] = {
1240  CL_CONTEXT_PLATFORM,
1241  0,
1242  CL_CONTEXT_VA_API_DISPLAY_INTEL,
1243  (intptr_t)src_hwctx->display,
1244  CL_CONTEXT_INTEROP_USER_SYNC,
1245  CL_FALSE,
1246  0,
1247  };
1248  OpenCLDeviceSelector selector = {
1249  .platform_index = -1,
1250  .device_index = -1,
1251  .context = src_hwctx->display,
1252  .enumerate_platforms = &opencl_enumerate_platforms,
1253  .filter_platform = &opencl_filter_intel_media_vaapi_platform,
1254  .enumerate_devices = &opencl_enumerate_intel_media_vaapi_devices,
1255  .filter_device = &opencl_filter_intel_media_vaapi_device,
1256  };
1257 
1258  err = opencl_device_create_internal(hwdev, &selector, props);
1259  }
1260  break;
1261 #endif
1262 
1263 #if HAVE_OPENCL_DXVA2
1265  {
1266  AVDXVA2DeviceContext *src_hwctx = src_ctx->hwctx;
1267  IDirect3DDevice9 *device;
1268  HANDLE device_handle;
1269  HRESULT hr;
1270 
1271  hr = IDirect3DDeviceManager9_OpenDeviceHandle(src_hwctx->devmgr,
1272  &device_handle);
1273  if (FAILED(hr)) {
1274  av_log(hwdev, AV_LOG_ERROR, "Failed to open device handle "
1275  "for Direct3D9 device: %lx.\n", (unsigned long)hr);
1276  err = AVERROR_UNKNOWN;
1277  break;
1278  }
1279 
1280  hr = IDirect3DDeviceManager9_LockDevice(src_hwctx->devmgr,
1281  device_handle,
1282  &device, FALSE);
1283  if (SUCCEEDED(hr)) {
1284  cl_context_properties props[5] = {
1285  CL_CONTEXT_PLATFORM,
1286  0,
1287  CL_CONTEXT_ADAPTER_D3D9EX_KHR,
1288  (intptr_t)device,
1289  0,
1290  };
1291  OpenCLDeviceSelector selector = {
1292  .platform_index = -1,
1293  .device_index = -1,
1294  .context = device,
1295  .enumerate_platforms = &opencl_enumerate_platforms,
1296  .filter_platform = &opencl_filter_dxva2_platform,
1297  .enumerate_devices = &opencl_enumerate_dxva2_devices,
1298  .filter_device = &opencl_filter_gpu_device,
1299  };
1300 
1301  err = opencl_device_create_internal(hwdev, &selector, props);
1302 
1303  IDirect3DDeviceManager9_UnlockDevice(src_hwctx->devmgr,
1304  device_handle, FALSE);
1305  } else {
1306  av_log(hwdev, AV_LOG_ERROR, "Failed to lock device handle "
1307  "for Direct3D9 device: %lx.\n", (unsigned long)hr);
1308  err = AVERROR_UNKNOWN;
1309  }
1310 
1311  IDirect3DDeviceManager9_CloseDeviceHandle(src_hwctx->devmgr,
1312  device_handle);
1313  }
1314  break;
1315 #endif
1316 
1317 #if HAVE_OPENCL_D3D11
1319  {
1320  AVD3D11VADeviceContext *src_hwctx = src_ctx->hwctx;
1321  cl_context_properties props[5] = {
1322  CL_CONTEXT_PLATFORM,
1323  0,
1324  CL_CONTEXT_D3D11_DEVICE_KHR,
1325  (intptr_t)src_hwctx->device,
1326  0,
1327  };
1328  OpenCLDeviceSelector selector = {
1329  .platform_index = -1,
1330  .device_index = -1,
1331  .context = src_hwctx->device,
1332  .enumerate_platforms = &opencl_enumerate_platforms,
1333  .filter_platform = &opencl_filter_d3d11_platform,
1334  .enumerate_devices = &opencl_enumerate_d3d11_devices,
1335  .filter_device = &opencl_filter_gpu_device,
1336  };
1337 
1338  err = opencl_device_create_internal(hwdev, &selector, props);
1339  }
1340  break;
1341 #endif
1342 
1343 #if HAVE_OPENCL_DRM_ARM
1344  case AV_HWDEVICE_TYPE_DRM:
1345  {
1346  OpenCLDeviceSelector selector = {
1347  .platform_index = -1,
1348  .device_index = -1,
1349  .context = NULL,
1350  .enumerate_platforms = &opencl_enumerate_platforms,
1351  .filter_platform = &opencl_filter_drm_arm_platform,
1352  .enumerate_devices = &opencl_enumerate_devices,
1353  .filter_device = &opencl_filter_drm_arm_device,
1354  };
1355 
1356  err = opencl_device_create_internal(hwdev, &selector, NULL);
1357  }
1358  break;
1359 #endif
1360 
1361  default:
1362  err = AVERROR(ENOSYS);
1363  break;
1364  }
1365 
1366  return err;
1367 }
1368 
1370  int plane, int width, int height,
1371  cl_image_format *image_format,
1372  cl_image_desc *image_desc)
1373 {
1374  const AVPixFmtDescriptor *desc;
1375  const AVComponentDescriptor *comp;
1376  int channels = 0, order = 0, depth = 0, step = 0;
1377  int wsub, hsub, alpha;
1378  int c;
1379 
1380  if (plane >= AV_NUM_DATA_POINTERS)
1381  return AVERROR(ENOENT);
1382 
1384 
1385  // Only normal images are allowed.
1386  if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM |
1389  return AVERROR(EINVAL);
1390 
1391  wsub = 1 << desc->log2_chroma_w;
1392  hsub = 1 << desc->log2_chroma_h;
1393  // Subsampled components must be exact.
1394  if (width & wsub - 1 || height & hsub - 1)
1395  return AVERROR(EINVAL);
1396 
1397  for (c = 0; c < desc->nb_components; c++) {
1398  comp = &desc->comp[c];
1399  if (comp->plane != plane)
1400  continue;
1401  // The step size must be a power of two.
1402  if (comp->step != 1 && comp->step != 2 &&
1403  comp->step != 4 && comp->step != 8)
1404  return AVERROR(EINVAL);
1405  // The bits in each component must be packed in the
1406  // most-significant-bits of the relevant bytes.
1407  if (comp->shift + comp->depth != 8 &&
1408  comp->shift + comp->depth != 16)
1409  return AVERROR(EINVAL);
1410  // The depth must not vary between components.
1411  if (depth && comp->depth != depth)
1412  return AVERROR(EINVAL);
1413  // If a single data element crosses multiple bytes then
1414  // it must match the native endianness.
1415  if (comp->depth > 8 &&
1416  HAVE_BIGENDIAN == !(desc->flags & AV_PIX_FMT_FLAG_BE))
1417  return AVERROR(EINVAL);
1418  // A single data element must not contain multiple samples
1419  // from the same component.
1420  if (step && comp->step != step)
1421  return AVERROR(EINVAL);
1422 
1423  depth = comp->depth;
1424  order = order * 10 + comp->offset / ((depth + 7) / 8) + 1;
1425  step = comp->step;
1426  alpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA &&
1427  c == desc->nb_components - 1);
1428  ++channels;
1429  }
1430  if (channels == 0)
1431  return AVERROR(ENOENT);
1432 
1433  memset(image_format, 0, sizeof(*image_format));
1434  memset(image_desc, 0, sizeof(*image_desc));
1435  image_desc->image_type = CL_MEM_OBJECT_IMAGE2D;
1436 
1437  if (plane == 0 || alpha) {
1438  image_desc->image_width = width;
1439  image_desc->image_height = height;
1440  image_desc->image_row_pitch = step * width;
1441  } else {
1442  image_desc->image_width = width / wsub;
1443  image_desc->image_height = height / hsub;
1444  image_desc->image_row_pitch = step * width / wsub;
1445  }
1446 
1447  if (depth <= 8) {
1448  image_format->image_channel_data_type = CL_UNORM_INT8;
1449  } else {
1450  if (depth <= 16)
1451  image_format->image_channel_data_type = CL_UNORM_INT16;
1452  else
1453  return AVERROR(EINVAL);
1454  }
1455 
1456 #define CHANNEL_ORDER(order, type) \
1457  case order: image_format->image_channel_order = type; break;
1458  switch (order) {
1459  CHANNEL_ORDER(1, CL_R);
1460  CHANNEL_ORDER(12, CL_RG);
1461  CHANNEL_ORDER(1234, CL_RGBA);
1462  CHANNEL_ORDER(2341, CL_ARGB);
1463  CHANNEL_ORDER(3214, CL_BGRA);
1464 #ifdef CL_ABGR
1465  CHANNEL_ORDER(4321, CL_ABGR);
1466 #endif
1467  default:
1468  return AVERROR(EINVAL);
1469  }
1470 #undef CHANNEL_ORDER
1471 
1472  return 0;
1473 }
1474 
1476  const void *hwconfig,
1477  AVHWFramesConstraints *constraints)
1478 {
1479  AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
1480  cl_uint nb_image_formats;
1481  cl_image_format *image_formats = NULL;
1482  cl_int cle;
1483  enum AVPixelFormat pix_fmt;
1484  int err, pix_fmts_found;
1485  size_t max_width, max_height;
1486 
1487  cle = clGetDeviceInfo(hwctx->device_id, CL_DEVICE_IMAGE2D_MAX_WIDTH,
1488  sizeof(max_width), &max_width, NULL);
1489  if (cle != CL_SUCCESS) {
1490  av_log(hwdev, AV_LOG_ERROR, "Failed to query maximum "
1491  "supported image width: %d.\n", cle);
1492  } else {
1493  constraints->max_width = max_width;
1494  }
1495  cle = clGetDeviceInfo(hwctx->device_id, CL_DEVICE_IMAGE2D_MAX_HEIGHT,
1496  sizeof(max_height), &max_height, NULL);
1497  if (cle != CL_SUCCESS) {
1498  av_log(hwdev, AV_LOG_ERROR, "Failed to query maximum "
1499  "supported image height: %d.\n", cle);
1500  } else {
1501  constraints->max_height = max_height;
1502  }
1503  av_log(hwdev, AV_LOG_DEBUG, "Maximum supported image size %dx%d.\n",
1504  constraints->max_width, constraints->max_height);
1505 
1506  cle = clGetSupportedImageFormats(hwctx->context,
1507  CL_MEM_READ_WRITE,
1508  CL_MEM_OBJECT_IMAGE2D,
1509  0, NULL, &nb_image_formats);
1510  if (cle != CL_SUCCESS) {
1511  av_log(hwdev, AV_LOG_ERROR, "Failed to query supported "
1512  "image formats: %d.\n", cle);
1513  err = AVERROR(ENOSYS);
1514  goto fail;
1515  }
1516  if (nb_image_formats == 0) {
1517  av_log(hwdev, AV_LOG_ERROR, "No image support in OpenCL "
1518  "driver (zero supported image formats).\n");
1519  err = AVERROR(ENOSYS);
1520  goto fail;
1521  }
1522 
1523  image_formats =
1524  av_malloc_array(nb_image_formats, sizeof(*image_formats));
1525  if (!image_formats) {
1526  err = AVERROR(ENOMEM);
1527  goto fail;
1528  }
1529 
1530  cle = clGetSupportedImageFormats(hwctx->context,
1531  CL_MEM_READ_WRITE,
1532  CL_MEM_OBJECT_IMAGE2D,
1533  nb_image_formats,
1534  image_formats, NULL);
1535  if (cle != CL_SUCCESS) {
1536  av_log(hwdev, AV_LOG_ERROR, "Failed to query supported "
1537  "image formats: %d.\n", cle);
1538  err = AVERROR(ENOSYS);
1539  goto fail;
1540  }
1541 
1542  pix_fmts_found = 0;
1543  for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++) {
1544  cl_image_format image_format;
1545  cl_image_desc image_desc;
1546  int plane, i;
1547 
1548  for (plane = 0;; plane++) {
1549  err = opencl_get_plane_format(pix_fmt, plane, 0, 0,
1550  &image_format,
1551  &image_desc);
1552  if (err < 0)
1553  break;
1554 
1555  for (i = 0; i < nb_image_formats; i++) {
1556  if (image_formats[i].image_channel_order ==
1557  image_format.image_channel_order &&
1558  image_formats[i].image_channel_data_type ==
1559  image_format.image_channel_data_type)
1560  break;
1561  }
1562  if (i == nb_image_formats) {
1563  err = AVERROR(EINVAL);
1564  break;
1565  }
1566  }
1567  if (err != AVERROR(ENOENT))
1568  continue;
1569 
1570  av_log(hwdev, AV_LOG_DEBUG, "Format %s supported.\n",
1572 
1573  err = av_reallocp_array(&constraints->valid_sw_formats,
1574  pix_fmts_found + 2,
1575  sizeof(*constraints->valid_sw_formats));
1576  if (err < 0)
1577  goto fail;
1578  constraints->valid_sw_formats[pix_fmts_found] = pix_fmt;
1579  constraints->valid_sw_formats[pix_fmts_found + 1] =
1581  ++pix_fmts_found;
1582  }
1583 
1584  av_freep(&image_formats);
1585 
1586  constraints->valid_hw_formats =
1587  av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
1588  if (!constraints->valid_hw_formats) {
1589  err = AVERROR(ENOMEM);
1590  goto fail;
1591  }
1592  constraints->valid_hw_formats[0] = AV_PIX_FMT_OPENCL;
1593  constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
1594 
1595  return 0;
1596 
1597 fail:
1598  av_freep(&image_formats);
1599  return err;
1600 }
1601 
1602 static void opencl_pool_free(void *opaque, uint8_t *data)
1603 {
1604  AVHWFramesContext *hwfc = opaque;
1606  cl_int cle;
1607  int p;
1608 
1609  for (p = 0; p < desc->nb_planes; p++) {
1610  cle = clReleaseMemObject(desc->planes[p]);
1611  if (cle != CL_SUCCESS) {
1612  av_log(hwfc, AV_LOG_ERROR, "Failed to release plane %d: "
1613  "%d.\n", p, cle);
1614  }
1615  }
1616 
1617  av_free(desc);
1618 }
1619 
1621 {
1622  AVHWFramesContext *hwfc = opaque;
1623  AVOpenCLDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1625  cl_int cle;
1626  cl_mem image;
1627  cl_image_format image_format;
1628  cl_image_desc image_desc;
1629  int err, p;
1630  AVBufferRef *ref;
1631 
1632  desc = av_mallocz(sizeof(*desc));
1633  if (!desc)
1634  return NULL;
1635 
1636  for (p = 0;; p++) {
1637  err = opencl_get_plane_format(hwfc->sw_format, p,
1638  hwfc->width, hwfc->height,
1639  &image_format, &image_desc);
1640  if (err == AVERROR(ENOENT))
1641  break;
1642  if (err < 0)
1643  goto fail;
1644 
1645  // For generic image objects, the pitch is determined by the
1646  // implementation.
1647  image_desc.image_row_pitch = 0;
1648 
1649  image = clCreateImage(hwctx->context, CL_MEM_READ_WRITE,
1650  &image_format, &image_desc, NULL, &cle);
1651  if (!image) {
1652  av_log(hwfc, AV_LOG_ERROR, "Failed to create image for "
1653  "plane %d: %d.\n", p, cle);
1654  goto fail;
1655  }
1656 
1657  desc->planes[p] = image;
1658  }
1659 
1660  desc->nb_planes = p;
1661 
1662  ref = av_buffer_create((uint8_t*)desc, sizeof(*desc),
1663  &opencl_pool_free, hwfc, 0);
1664  if (!ref)
1665  goto fail;
1666 
1667  return ref;
1668 
1669 fail:
1670  for (p = 0; desc->planes[p]; p++)
1671  clReleaseMemObject(desc->planes[p]);
1672  av_free(desc);
1673  return NULL;
1674 }
1675 
1677 {
1678  AVOpenCLFramesContext *hwctx = hwfc->hwctx;
1679  OpenCLDeviceContext *devpriv = hwfc->device_ctx->internal->priv;
1680  OpenCLFramesContext *priv = hwfc->internal->priv;
1681  cl_int cle;
1682 
1683  priv->command_queue = hwctx->command_queue ? hwctx->command_queue
1684  : devpriv->command_queue;
1685  cle = clRetainCommandQueue(priv->command_queue);
1686  if (cle != CL_SUCCESS) {
1687  av_log(hwfc, AV_LOG_ERROR, "Failed to retain frame "
1688  "command queue: %d.\n", cle);
1689  return AVERROR(EIO);
1690  }
1691 
1692  return 0;
1693 }
1694 
1696 {
1697  if (!hwfc->pool) {
1698  hwfc->internal->pool_internal =
1699  av_buffer_pool_init2(sizeof(cl_mem), hwfc,
1701  if (!hwfc->internal->pool_internal)
1702  return AVERROR(ENOMEM);
1703  }
1704 
1705  return opencl_frames_init_command_queue(hwfc);
1706 }
1707 
1709 {
1710  OpenCLFramesContext *priv = hwfc->internal->priv;
1711  cl_int cle;
1712 
1713 #if HAVE_OPENCL_DXVA2 || HAVE_OPENCL_D3D11
1714  int i, p;
1715  for (i = 0; i < priv->nb_mapped_frames; i++) {
1716  AVOpenCLFrameDescriptor *desc = &priv->mapped_frames[i];
1717  for (p = 0; p < desc->nb_planes; p++) {
1718  cle = clReleaseMemObject(desc->planes[p]);
1719  if (cle != CL_SUCCESS) {
1720  av_log(hwfc, AV_LOG_ERROR, "Failed to release mapped "
1721  "frame object (frame %d plane %d): %d.\n",
1722  i, p, cle);
1723  }
1724  }
1725  }
1726  av_freep(&priv->mapped_frames);
1727 #endif
1728 
1729  if (priv->command_queue) {
1730  cle = clReleaseCommandQueue(priv->command_queue);
1731  if (cle != CL_SUCCESS) {
1732  av_log(hwfc, AV_LOG_ERROR, "Failed to release frame "
1733  "command queue: %d.\n", cle);
1734  }
1735  priv->command_queue = NULL;
1736  }
1737 }
1738 
1740 {
1742  int p;
1743 
1744  frame->buf[0] = av_buffer_pool_get(hwfc->pool);
1745  if (!frame->buf[0])
1746  return AVERROR(ENOMEM);
1747 
1749 
1750  for (p = 0; p < desc->nb_planes; p++)
1751  frame->data[p] = (uint8_t*)desc->planes[p];
1752 
1754  frame->width = hwfc->width;
1755  frame->height = hwfc->height;
1756 
1757  return 0;
1758 }
1759 
1761  enum AVHWFrameTransferDirection dir,
1762  enum AVPixelFormat **formats)
1763 {
1764  enum AVPixelFormat *fmts;
1765 
1766  fmts = av_malloc_array(2, sizeof(*fmts));
1767  if (!fmts)
1768  return AVERROR(ENOMEM);
1769 
1770  fmts[0] = hwfc->sw_format;
1771  fmts[1] = AV_PIX_FMT_NONE;
1772 
1773  *formats = fmts;
1774  return 0;
1775 }
1776 
1778  cl_event *events, int nb_events)
1779 {
1780  cl_int cle;
1781  int i;
1782 
1783  cle = clWaitForEvents(nb_events, events);
1784  if (cle != CL_SUCCESS) {
1785  av_log(hwfc, AV_LOG_ERROR, "Failed to wait for event "
1786  "completion: %d.\n", cle);
1787  return AVERROR(EIO);
1788  }
1789 
1790  for (i = 0; i < nb_events; i++) {
1791  cle = clReleaseEvent(events[i]);
1792  if (cle != CL_SUCCESS) {
1793  av_log(hwfc, AV_LOG_ERROR, "Failed to release "
1794  "event: %d.\n", cle);
1795  }
1796  }
1797 
1798  return 0;
1799 }
1800 
1802  AVFrame *dst, const AVFrame *src)
1803 {
1804  OpenCLFramesContext *priv = hwfc->internal->priv;
1805  cl_image_format image_format;
1806  cl_image_desc image_desc;
1807  cl_int cle;
1808  size_t origin[3] = { 0, 0, 0 };
1809  size_t region[3];
1810  cl_event events[AV_NUM_DATA_POINTERS];
1811  int err, p;
1812 
1813  if (dst->format != hwfc->sw_format)
1814  return AVERROR(EINVAL);
1815 
1816  for (p = 0;; p++) {
1817  err = opencl_get_plane_format(hwfc->sw_format, p,
1818  src->width, src->height,
1819  &image_format, &image_desc);
1820  if (err < 0) {
1821  if (err == AVERROR(ENOENT))
1822  err = 0;
1823  break;
1824  }
1825 
1826  if (!dst->data[p]) {
1827  av_log(hwfc, AV_LOG_ERROR, "Plane %d missing on "
1828  "destination frame for transfer.\n", p);
1829  err = AVERROR(EINVAL);
1830  break;
1831  }
1832 
1833  region[0] = image_desc.image_width;
1834  region[1] = image_desc.image_height;
1835  region[2] = 1;
1836 
1837  cle = clEnqueueReadImage(priv->command_queue,
1838  (cl_mem)src->data[p],
1839  CL_FALSE, origin, region,
1840  dst->linesize[p], 0,
1841  dst->data[p],
1842  0, NULL, &events[p]);
1843  if (cle != CL_SUCCESS) {
1844  av_log(hwfc, AV_LOG_ERROR, "Failed to enqueue read of "
1845  "OpenCL image plane %d: %d.\n", p, cle);
1846  err = AVERROR(EIO);
1847  break;
1848  }
1849  }
1850 
1851  opencl_wait_events(hwfc, events, p);
1852 
1853  return err;
1854 }
1855 
1857  AVFrame *dst, const AVFrame *src)
1858 {
1859  OpenCLFramesContext *priv = hwfc->internal->priv;
1860  cl_image_format image_format;
1861  cl_image_desc image_desc;
1862  cl_int cle;
1863  size_t origin[3] = { 0, 0, 0 };
1864  size_t region[3];
1865  cl_event events[AV_NUM_DATA_POINTERS];
1866  int err, p;
1867 
1868  if (src->format != hwfc->sw_format)
1869  return AVERROR(EINVAL);
1870 
1871  for (p = 0;; p++) {
1872  err = opencl_get_plane_format(hwfc->sw_format, p,
1873  src->width, src->height,
1874  &image_format, &image_desc);
1875  if (err < 0) {
1876  if (err == AVERROR(ENOENT))
1877  err = 0;
1878  break;
1879  }
1880 
1881  if (!src->data[p]) {
1882  av_log(hwfc, AV_LOG_ERROR, "Plane %d missing on "
1883  "source frame for transfer.\n", p);
1884  err = AVERROR(EINVAL);
1885  break;
1886  }
1887 
1888  region[0] = image_desc.image_width;
1889  region[1] = image_desc.image_height;
1890  region[2] = 1;
1891 
1892  cle = clEnqueueWriteImage(priv->command_queue,
1893  (cl_mem)dst->data[p],
1894  CL_FALSE, origin, region,
1895  src->linesize[p], 0,
1896  src->data[p],
1897  0, NULL, &events[p]);
1898  if (cle != CL_SUCCESS) {
1899  av_log(hwfc, AV_LOG_ERROR, "Failed to enqueue write of "
1900  "OpenCL image plane %d: %d.\n", p, cle);
1901  err = AVERROR(EIO);
1902  break;
1903  }
1904  }
1905 
1906  opencl_wait_events(hwfc, events, p);
1907 
1908  return err;
1909 }
1910 
1911 typedef struct OpenCLMapping {
1912  // The mapped addresses for each plane.
1913  // The destination frame is not available when we unmap, so these
1914  // need to be stored separately.
1916 } OpenCLMapping;
1917 
1919  HWMapDescriptor *hwmap)
1920 {
1921  OpenCLFramesContext *priv = hwfc->internal->priv;
1922  OpenCLMapping *map = hwmap->priv;
1923  cl_event events[AV_NUM_DATA_POINTERS];
1924  int p, e;
1925  cl_int cle;
1926 
1927  for (p = e = 0; p < FF_ARRAY_ELEMS(map->address); p++) {
1928  if (!map->address[p])
1929  break;
1930 
1931  cle = clEnqueueUnmapMemObject(priv->command_queue,
1932  (cl_mem)hwmap->source->data[p],
1933  map->address[p],
1934  0, NULL, &events[e]);
1935  if (cle != CL_SUCCESS) {
1936  av_log(hwfc, AV_LOG_ERROR, "Failed to unmap OpenCL "
1937  "image plane %d: %d.\n", p, cle);
1938  }
1939  ++e;
1940  }
1941 
1942  opencl_wait_events(hwfc, events, e);
1943 
1944  av_free(map);
1945 }
1946 
1948  const AVFrame *src, int flags)
1949 {
1950  OpenCLFramesContext *priv = hwfc->internal->priv;
1951  cl_map_flags map_flags;
1952  cl_image_format image_format;
1953  cl_image_desc image_desc;
1954  cl_int cle;
1955  OpenCLMapping *map;
1956  size_t origin[3] = { 0, 0, 0 };
1957  size_t region[3];
1958  size_t row_pitch;
1959  cl_event events[AV_NUM_DATA_POINTERS];
1960  int err, p;
1961 
1962  av_assert0(hwfc->sw_format == dst->format);
1963 
1965  !(flags & AV_HWFRAME_MAP_READ)) {
1966  // This is mutually exclusive with the read/write flags, so
1967  // there is no way to map with read here.
1968  map_flags = CL_MAP_WRITE_INVALIDATE_REGION;
1969  } else {
1970  map_flags = 0;
1971  if (flags & AV_HWFRAME_MAP_READ)
1972  map_flags |= CL_MAP_READ;
1974  map_flags |= CL_MAP_WRITE;
1975  }
1976 
1977  map = av_mallocz(sizeof(*map));
1978  if (!map)
1979  return AVERROR(ENOMEM);
1980 
1981  for (p = 0;; p++) {
1982  err = opencl_get_plane_format(hwfc->sw_format, p,
1983  src->width, src->height,
1984  &image_format, &image_desc);
1985  if (err == AVERROR(ENOENT))
1986  break;
1987  if (err < 0)
1988  goto fail;
1989 
1990  region[0] = image_desc.image_width;
1991  region[1] = image_desc.image_height;
1992  region[2] = 1;
1993 
1994  map->address[p] =
1995  clEnqueueMapImage(priv->command_queue,
1996  (cl_mem)src->data[p],
1997  CL_FALSE, map_flags, origin, region,
1998  &row_pitch, NULL, 0, NULL,
1999  &events[p], &cle);
2000  if (!map->address[p]) {
2001  av_log(hwfc, AV_LOG_ERROR, "Failed to map OpenCL "
2002  "image plane %d: %d.\n", p, cle);
2003  err = AVERROR(EIO);
2004  goto fail;
2005  }
2006 
2007  dst->data[p] = map->address[p];
2008  dst->linesize[p] = row_pitch;
2009 
2010  av_log(hwfc, AV_LOG_DEBUG, "Map plane %d (%p -> %p).\n",
2011  p, src->data[p], dst->data[p]);
2012  }
2013 
2014  err = opencl_wait_events(hwfc, events, p);
2015  if (err < 0)
2016  goto fail;
2017 
2018  err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
2020  if (err < 0)
2021  goto fail;
2022 
2023  dst->width = src->width;
2024  dst->height = src->height;
2025 
2026  return 0;
2027 
2028 fail:
2029  for (p = 0; p < AV_NUM_DATA_POINTERS; p++) {
2030  if (!map->address[p])
2031  break;
2032  clEnqueueUnmapMemObject(priv->command_queue,
2033  (cl_mem)src->data[p],
2034  map->address[p],
2035  0, NULL, &events[p]);
2036  }
2037  if (p > 0)
2038  opencl_wait_events(hwfc, events, p);
2039  av_freep(&map);
2040  return err;
2041 }
2042 
2043 #if HAVE_OPENCL_DRM_BEIGNET
2044 
2045 typedef struct DRMBeignetToOpenCLMapping {
2046  AVFrame *drm_frame;
2047  AVDRMFrameDescriptor *drm_desc;
2048 
2050 } DRMBeignetToOpenCLMapping;
2051 
2052 static void opencl_unmap_from_drm_beignet(AVHWFramesContext *dst_fc,
2053  HWMapDescriptor *hwmap)
2054 {
2055  DRMBeignetToOpenCLMapping *mapping = hwmap->priv;
2056  cl_int cle;
2057  int i;
2058 
2059  for (i = 0; i < mapping->frame.nb_planes; i++) {
2060  cle = clReleaseMemObject(mapping->frame.planes[i]);
2061  if (cle != CL_SUCCESS) {
2062  av_log(dst_fc, AV_LOG_ERROR, "Failed to release CL image "
2063  "of plane %d of DRM frame: %d.\n", i, cle);
2064  }
2065  }
2066 
2067  av_free(mapping);
2068 }
2069 
2070 static int opencl_map_from_drm_beignet(AVHWFramesContext *dst_fc,
2071  AVFrame *dst, const AVFrame *src,
2072  int flags)
2073 {
2074  AVOpenCLDeviceContext *hwctx = dst_fc->device_ctx->hwctx;
2075  OpenCLDeviceContext *priv = dst_fc->device_ctx->internal->priv;
2076  DRMBeignetToOpenCLMapping *mapping;
2077  const AVDRMFrameDescriptor *desc;
2078  cl_int cle;
2079  int err, i, j, p;
2080 
2081  desc = (const AVDRMFrameDescriptor*)src->data[0];
2082 
2083  mapping = av_mallocz(sizeof(*mapping));
2084  if (!mapping)
2085  return AVERROR(ENOMEM);
2086 
2087  p = 0;
2088  for (i = 0; i < desc->nb_layers; i++) {
2089  const AVDRMLayerDescriptor *layer = &desc->layers[i];
2090  for (j = 0; j < layer->nb_planes; j++) {
2091  const AVDRMPlaneDescriptor *plane = &layer->planes[j];
2092  const AVDRMObjectDescriptor *object =
2093  &desc->objects[plane->object_index];
2094 
2095  cl_import_image_info_intel image_info = {
2096  .fd = object->fd,
2097  .size = object->size,
2098  .type = CL_MEM_OBJECT_IMAGE2D,
2099  .offset = plane->offset,
2100  .row_pitch = plane->pitch,
2101  };
2102  cl_image_desc image_desc;
2103 
2104  err = opencl_get_plane_format(dst_fc->sw_format, p,
2105  src->width, src->height,
2106  &image_info.fmt,
2107  &image_desc);
2108  if (err < 0) {
2109  av_log(dst_fc, AV_LOG_ERROR, "DRM frame layer %d "
2110  "plane %d is not representable in OpenCL: %d.\n",
2111  i, j, err);
2112  goto fail;
2113  }
2114  image_info.width = image_desc.image_width;
2115  image_info.height = image_desc.image_height;
2116 
2117  mapping->frame.planes[p] =
2118  priv->clCreateImageFromFdINTEL(hwctx->context,
2119  &image_info, &cle);
2120  if (!mapping->frame.planes[p]) {
2121  av_log(dst_fc, AV_LOG_ERROR, "Failed to create CL image "
2122  "from layer %d plane %d of DRM frame: %d.\n",
2123  i, j, cle);
2124  err = AVERROR(EIO);
2125  goto fail;
2126  }
2127 
2128  dst->data[p] = (uint8_t*)mapping->frame.planes[p];
2129  mapping->frame.nb_planes = ++p;
2130  }
2131  }
2132 
2133  err = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src,
2134  &opencl_unmap_from_drm_beignet,
2135  mapping);
2136  if (err < 0)
2137  goto fail;
2138 
2139  dst->width = src->width;
2140  dst->height = src->height;
2141 
2142  return 0;
2143 
2144 fail:
2145  for (p = 0; p < mapping->frame.nb_planes; p++) {
2146  if (mapping->frame.planes[p])
2147  clReleaseMemObject(mapping->frame.planes[p]);
2148  }
2149  av_free(mapping);
2150  return err;
2151 }
2152 
2153 #if HAVE_OPENCL_VAAPI_BEIGNET
2154 
2155 static int opencl_map_from_vaapi(AVHWFramesContext *dst_fc,
2156  AVFrame *dst, const AVFrame *src,
2157  int flags)
2158 {
2159  AVFrame *tmp;
2160  int err;
2161 
2162  tmp = av_frame_alloc();
2163  if (!tmp)
2164  return AVERROR(ENOMEM);
2165 
2166  tmp->format = AV_PIX_FMT_DRM_PRIME;
2167 
2168  err = av_hwframe_map(tmp, src, flags);
2169  if (err < 0)
2170  goto fail;
2171 
2172  err = opencl_map_from_drm_beignet(dst_fc, dst, tmp, flags);
2173  if (err < 0)
2174  goto fail;
2175 
2176  err = ff_hwframe_map_replace(dst, src);
2177 
2178 fail:
2179  av_frame_free(&tmp);
2180  return err;
2181 }
2182 
2183 #endif /* HAVE_OPENCL_VAAPI_BEIGNET */
2184 #endif /* HAVE_OPENCL_DRM_BEIGNET */
2185 
2186 static inline cl_mem_flags opencl_mem_flags_for_mapping(int map_flags)
2187 {
2188  if ((map_flags & AV_HWFRAME_MAP_READ) &&
2189  (map_flags & AV_HWFRAME_MAP_WRITE))
2190  return CL_MEM_READ_WRITE;
2191  else if (map_flags & AV_HWFRAME_MAP_READ)
2192  return CL_MEM_READ_ONLY;
2193  else if (map_flags & AV_HWFRAME_MAP_WRITE)
2194  return CL_MEM_WRITE_ONLY;
2195  else
2196  return 0;
2197 }
2198 
2199 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
2200 
2201 static void opencl_unmap_from_qsv(AVHWFramesContext *dst_fc,
2202  HWMapDescriptor *hwmap)
2203 {
2204  AVOpenCLFrameDescriptor *desc = hwmap->priv;
2205  OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
2206  OpenCLFramesContext *frames_priv = dst_fc->internal->priv;
2207  cl_event event;
2208  cl_int cle;
2209  int p;
2210 
2211  av_log(dst_fc, AV_LOG_DEBUG, "Unmap QSV/VAAPI surface from OpenCL.\n");
2212 
2213  cle = device_priv->clEnqueueReleaseVA_APIMediaSurfacesINTEL(
2214  frames_priv->command_queue, desc->nb_planes, desc->planes,
2215  0, NULL, &event);
2216  if (cle != CL_SUCCESS) {
2217  av_log(dst_fc, AV_LOG_ERROR, "Failed to release surface "
2218  "handles: %d.\n", cle);
2219  }
2220 
2221  opencl_wait_events(dst_fc, &event, 1);
2222 
2223  for (p = 0; p < desc->nb_planes; p++) {
2224  cle = clReleaseMemObject(desc->planes[p]);
2225  if (cle != CL_SUCCESS) {
2226  av_log(dst_fc, AV_LOG_ERROR, "Failed to release CL "
2227  "image of plane %d of QSV/VAAPI surface: %d\n",
2228  p, cle);
2229  }
2230  }
2231 
2232  av_free(desc);
2233 }
2234 
2235 static int opencl_map_from_qsv(AVHWFramesContext *dst_fc, AVFrame *dst,
2236  const AVFrame *src, int flags)
2237 {
2238  AVHWFramesContext *src_fc =
2239  (AVHWFramesContext*)src->hw_frames_ctx->data;
2240  AVOpenCLDeviceContext *dst_dev = dst_fc->device_ctx->hwctx;
2241  OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
2242  OpenCLFramesContext *frames_priv = dst_fc->internal->priv;
2244  VASurfaceID va_surface;
2245  cl_mem_flags cl_flags;
2246  cl_event event;
2247  cl_int cle;
2248  int err, p;
2249 
2250 #if CONFIG_LIBMFX
2251  if (src->format == AV_PIX_FMT_QSV) {
2252  mfxFrameSurface1 *mfx_surface = (mfxFrameSurface1*)src->data[3];
2253  va_surface = *(VASurfaceID*)mfx_surface->Data.MemId;
2254  } else
2255 #endif
2256  if (src->format == AV_PIX_FMT_VAAPI) {
2257  va_surface = (VASurfaceID)(uintptr_t)src->data[3];
2258  } else {
2259  return AVERROR(ENOSYS);
2260  }
2261 
2262  cl_flags = opencl_mem_flags_for_mapping(flags);
2263  if (!cl_flags)
2264  return AVERROR(EINVAL);
2265 
2266  av_log(src_fc, AV_LOG_DEBUG, "Map QSV/VAAPI surface %#x to "
2267  "OpenCL.\n", va_surface);
2268 
2269  desc = av_mallocz(sizeof(*desc));
2270  if (!desc)
2271  return AVERROR(ENOMEM);
2272 
2273  // The cl_intel_va_api_media_sharing extension only supports NV12
2274  // surfaces, so for now there are always exactly two planes.
2275  desc->nb_planes = 2;
2276 
2277  for (p = 0; p < desc->nb_planes; p++) {
2278  desc->planes[p] =
2279  device_priv->clCreateFromVA_APIMediaSurfaceINTEL(
2280  dst_dev->context, cl_flags, &va_surface, p, &cle);
2281  if (!desc->planes[p]) {
2282  av_log(dst_fc, AV_LOG_ERROR, "Failed to create CL "
2283  "image from plane %d of QSV/VAAPI surface "
2284  "%#x: %d.\n", p, va_surface, cle);
2285  err = AVERROR(EIO);
2286  goto fail;
2287  }
2288 
2289  dst->data[p] = (uint8_t*)desc->planes[p];
2290  }
2291 
2292  cle = device_priv->clEnqueueAcquireVA_APIMediaSurfacesINTEL(
2293  frames_priv->command_queue, desc->nb_planes, desc->planes,
2294  0, NULL, &event);
2295  if (cle != CL_SUCCESS) {
2296  av_log(dst_fc, AV_LOG_ERROR, "Failed to acquire surface "
2297  "handles: %d.\n", cle);
2298  err = AVERROR(EIO);
2299  goto fail;
2300  }
2301 
2302  err = opencl_wait_events(dst_fc, &event, 1);
2303  if (err < 0)
2304  goto fail;
2305 
2306  err = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src,
2307  &opencl_unmap_from_qsv, desc);
2308  if (err < 0)
2309  goto fail;
2310 
2311  dst->width = src->width;
2312  dst->height = src->height;
2313 
2314  return 0;
2315 
2316 fail:
2317  for (p = 0; p < desc->nb_planes; p++)
2318  if (desc->planes[p])
2319  clReleaseMemObject(desc->planes[p]);
2320  av_freep(&desc);
2321  return err;
2322 }
2323 
2324 #endif
2325 
2326 #if HAVE_OPENCL_DXVA2
2327 
2328 static void opencl_unmap_from_dxva2(AVHWFramesContext *dst_fc,
2329  HWMapDescriptor *hwmap)
2330 {
2331  AVOpenCLFrameDescriptor *desc = hwmap->priv;
2332  OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
2333  OpenCLFramesContext *frames_priv = dst_fc->device_ctx->internal->priv;
2334  cl_event event;
2335  cl_int cle;
2336 
2337  av_log(dst_fc, AV_LOG_DEBUG, "Unmap DXVA2 surface from OpenCL.\n");
2338 
2339  cle = device_priv->clEnqueueReleaseDX9MediaSurfacesKHR(
2340  frames_priv->command_queue, desc->nb_planes, desc->planes,
2341  0, NULL, &event);
2342  if (cle != CL_SUCCESS) {
2343  av_log(dst_fc, AV_LOG_ERROR, "Failed to release surface "
2344  "handle: %d.\n", cle);
2345  return;
2346  }
2347 
2348  opencl_wait_events(dst_fc, &event, 1);
2349 }
2350 
2351 static int opencl_map_from_dxva2(AVHWFramesContext *dst_fc, AVFrame *dst,
2352  const AVFrame *src, int flags)
2353 {
2354  AVHWFramesContext *src_fc =
2355  (AVHWFramesContext*)src->hw_frames_ctx->data;
2356  AVDXVA2FramesContext *src_hwctx = src_fc->hwctx;
2357  OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
2358  OpenCLFramesContext *frames_priv = dst_fc->internal->priv;
2360  cl_event event;
2361  cl_int cle;
2362  int err, i;
2363 
2364  av_log(dst_fc, AV_LOG_DEBUG, "Map DXVA2 surface %p to "
2365  "OpenCL.\n", src->data[3]);
2366 
2367  for (i = 0; i < src_hwctx->nb_surfaces; i++) {
2368  if (src_hwctx->surfaces[i] == (IDirect3DSurface9*)src->data[3])
2369  break;
2370  }
2371  if (i >= src_hwctx->nb_surfaces) {
2372  av_log(dst_fc, AV_LOG_ERROR, "Trying to map from a surface which "
2373  "is not in the mapped frames context.\n");
2374  return AVERROR(EINVAL);
2375  }
2376 
2377  desc = &frames_priv->mapped_frames[i];
2378 
2379  cle = device_priv->clEnqueueAcquireDX9MediaSurfacesKHR(
2380  frames_priv->command_queue, desc->nb_planes, desc->planes,
2381  0, NULL, &event);
2382  if (cle != CL_SUCCESS) {
2383  av_log(dst_fc, AV_LOG_ERROR, "Failed to acquire surface "
2384  "handle: %d.\n", cle);
2385  return AVERROR(EIO);
2386  }
2387 
2388  err = opencl_wait_events(dst_fc, &event, 1);
2389  if (err < 0)
2390  goto fail;
2391 
2392  for (i = 0; i < desc->nb_planes; i++)
2393  dst->data[i] = (uint8_t*)desc->planes[i];
2394 
2395  err = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src,
2396  &opencl_unmap_from_dxva2, desc);
2397  if (err < 0)
2398  goto fail;
2399 
2400  dst->width = src->width;
2401  dst->height = src->height;
2402 
2403  return 0;
2404 
2405 fail:
2406  cle = device_priv->clEnqueueReleaseDX9MediaSurfacesKHR(
2407  frames_priv->command_queue, desc->nb_planes, desc->planes,
2408  0, NULL, &event);
2409  if (cle == CL_SUCCESS)
2410  opencl_wait_events(dst_fc, &event, 1);
2411  return err;
2412 }
2413 
2414 static int opencl_frames_derive_from_dxva2(AVHWFramesContext *dst_fc,
2415  AVHWFramesContext *src_fc, int flags)
2416 {
2417  AVOpenCLDeviceContext *dst_dev = dst_fc->device_ctx->hwctx;
2418  AVDXVA2FramesContext *src_hwctx = src_fc->hwctx;
2419  OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
2420  OpenCLFramesContext *frames_priv = dst_fc->internal->priv;
2421  cl_mem_flags cl_flags;
2422  cl_int cle;
2423  int err, i, p, nb_planes;
2424 
2425  if (src_fc->sw_format != AV_PIX_FMT_NV12) {
2426  av_log(dst_fc, AV_LOG_ERROR, "Only NV12 textures are supported "
2427  "for DXVA2 to OpenCL mapping.\n");
2428  return AVERROR(EINVAL);
2429  }
2430  nb_planes = 2;
2431 
2432  if (src_fc->initial_pool_size == 0) {
2433  av_log(dst_fc, AV_LOG_ERROR, "Only fixed-size pools are supported "
2434  "for DXVA2 to OpenCL mapping.\n");
2435  return AVERROR(EINVAL);
2436  }
2437 
2438  cl_flags = opencl_mem_flags_for_mapping(flags);
2439  if (!cl_flags)
2440  return AVERROR(EINVAL);
2441 
2442  frames_priv->nb_mapped_frames = src_hwctx->nb_surfaces;
2443 
2444  frames_priv->mapped_frames =
2445  av_mallocz_array(frames_priv->nb_mapped_frames,
2446  sizeof(*frames_priv->mapped_frames));
2447  if (!frames_priv->mapped_frames)
2448  return AVERROR(ENOMEM);
2449 
2450  for (i = 0; i < frames_priv->nb_mapped_frames; i++) {
2451  AVOpenCLFrameDescriptor *desc = &frames_priv->mapped_frames[i];
2452  cl_dx9_surface_info_khr surface_info = {
2453  .resource = src_hwctx->surfaces[i],
2454  .shared_handle = NULL,
2455  };
2456  desc->nb_planes = nb_planes;
2457  for (p = 0; p < nb_planes; p++) {
2458  desc->planes[p] =
2459  device_priv->clCreateFromDX9MediaSurfaceKHR(
2460  dst_dev->context, cl_flags,
2461  device_priv->dx9_media_adapter_type,
2462  &surface_info, p, &cle);
2463  if (!desc->planes[p]) {
2464  av_log(dst_fc, AV_LOG_ERROR, "Failed to create CL "
2465  "image from plane %d of DXVA2 surface %d: %d.\n",
2466  p, i, cle);
2467  err = AVERROR(EIO);
2468  goto fail;
2469  }
2470  }
2471  }
2472 
2473  return 0;
2474 
2475 fail:
2476  for (i = 0; i < frames_priv->nb_mapped_frames; i++) {
2477  AVOpenCLFrameDescriptor *desc = &frames_priv->mapped_frames[i];
2478  for (p = 0; p < desc->nb_planes; p++) {
2479  if (desc->planes[p])
2480  clReleaseMemObject(desc->planes[p]);
2481  }
2482  }
2483  av_freep(&frames_priv->mapped_frames);
2484  frames_priv->nb_mapped_frames = 0;
2485  return err;
2486 }
2487 
2488 #endif
2489 
2490 #if HAVE_OPENCL_D3D11
2491 
2492 static void opencl_unmap_from_d3d11(AVHWFramesContext *dst_fc,
2493  HWMapDescriptor *hwmap)
2494 {
2495  AVOpenCLFrameDescriptor *desc = hwmap->priv;
2496  OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
2497  OpenCLFramesContext *frames_priv = dst_fc->device_ctx->internal->priv;
2498  cl_event event;
2499  cl_int cle;
2500 
2501  cle = device_priv->clEnqueueReleaseD3D11ObjectsKHR(
2502  frames_priv->command_queue, desc->nb_planes, desc->planes,
2503  0, NULL, &event);
2504  if (cle != CL_SUCCESS) {
2505  av_log(dst_fc, AV_LOG_ERROR, "Failed to release surface "
2506  "handle: %d.\n", cle);
2507  }
2508 
2509  opencl_wait_events(dst_fc, &event, 1);
2510 }
2511 
2512 static int opencl_map_from_d3d11(AVHWFramesContext *dst_fc, AVFrame *dst,
2513  const AVFrame *src, int flags)
2514 {
2515  OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
2516  OpenCLFramesContext *frames_priv = dst_fc->internal->priv;
2518  cl_event event;
2519  cl_int cle;
2520  int err, index, i;
2521 
2522  index = (intptr_t)src->data[1];
2523  if (index >= frames_priv->nb_mapped_frames) {
2524  av_log(dst_fc, AV_LOG_ERROR, "Texture array index out of range for "
2525  "mapping: %d >= %d.\n", index, frames_priv->nb_mapped_frames);
2526  return AVERROR(EINVAL);
2527  }
2528 
2529  av_log(dst_fc, AV_LOG_DEBUG, "Map D3D11 texture %d to OpenCL.\n",
2530  index);
2531 
2532  desc = &frames_priv->mapped_frames[index];
2533 
2534  cle = device_priv->clEnqueueAcquireD3D11ObjectsKHR(
2535  frames_priv->command_queue, desc->nb_planes, desc->planes,
2536  0, NULL, &event);
2537  if (cle != CL_SUCCESS) {
2538  av_log(dst_fc, AV_LOG_ERROR, "Failed to acquire surface "
2539  "handle: %d.\n", cle);
2540  return AVERROR(EIO);
2541  }
2542 
2543  err = opencl_wait_events(dst_fc, &event, 1);
2544  if (err < 0)
2545  goto fail;
2546 
2547  for (i = 0; i < desc->nb_planes; i++)
2548  dst->data[i] = (uint8_t*)desc->planes[i];
2549 
2550  err = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src,
2551  &opencl_unmap_from_d3d11, desc);
2552  if (err < 0)
2553  goto fail;
2554 
2555  dst->width = src->width;
2556  dst->height = src->height;
2557 
2558  return 0;
2559 
2560 fail:
2561  cle = device_priv->clEnqueueReleaseD3D11ObjectsKHR(
2562  frames_priv->command_queue, desc->nb_planes, desc->planes,
2563  0, NULL, &event);
2564  if (cle == CL_SUCCESS)
2565  opencl_wait_events(dst_fc, &event, 1);
2566  return err;
2567 }
2568 
2569 static int opencl_frames_derive_from_d3d11(AVHWFramesContext *dst_fc,
2570  AVHWFramesContext *src_fc, int flags)
2571 {
2572  AVOpenCLDeviceContext *dst_dev = dst_fc->device_ctx->hwctx;
2573  AVD3D11VAFramesContext *src_hwctx = src_fc->hwctx;
2574  OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
2575  OpenCLFramesContext *frames_priv = dst_fc->internal->priv;
2576  cl_mem_flags cl_flags;
2577  cl_int cle;
2578  int err, i, p, nb_planes;
2579 
2580  if (src_fc->sw_format != AV_PIX_FMT_NV12) {
2581  av_log(dst_fc, AV_LOG_ERROR, "Only NV12 textures are supported "
2582  "for D3D11 to OpenCL mapping.\n");
2583  return AVERROR(EINVAL);
2584  }
2585  nb_planes = 2;
2586 
2587  if (src_fc->initial_pool_size == 0) {
2588  av_log(dst_fc, AV_LOG_ERROR, "Only fixed-size pools are supported "
2589  "for D3D11 to OpenCL mapping.\n");
2590  return AVERROR(EINVAL);
2591  }
2592 
2593  cl_flags = opencl_mem_flags_for_mapping(flags);
2594  if (!cl_flags)
2595  return AVERROR(EINVAL);
2596 
2597  frames_priv->nb_mapped_frames = src_fc->initial_pool_size;
2598 
2599  frames_priv->mapped_frames =
2600  av_mallocz_array(frames_priv->nb_mapped_frames,
2601  sizeof(*frames_priv->mapped_frames));
2602  if (!frames_priv->mapped_frames)
2603  return AVERROR(ENOMEM);
2604 
2605  for (i = 0; i < frames_priv->nb_mapped_frames; i++) {
2606  AVOpenCLFrameDescriptor *desc = &frames_priv->mapped_frames[i];
2607  desc->nb_planes = nb_planes;
2608  for (p = 0; p < nb_planes; p++) {
2609  UINT subresource = 2 * i + p;
2610 
2611  desc->planes[p] =
2612  device_priv->clCreateFromD3D11Texture2DKHR(
2613  dst_dev->context, cl_flags, src_hwctx->texture,
2614  subresource, &cle);
2615  if (!desc->planes[p]) {
2616  av_log(dst_fc, AV_LOG_ERROR, "Failed to create CL "
2617  "image from plane %d of D3D texture "
2618  "index %d (subresource %u): %d.\n",
2619  p, i, (unsigned int)subresource, cle);
2620  err = AVERROR(EIO);
2621  goto fail;
2622  }
2623  }
2624  }
2625 
2626  return 0;
2627 
2628 fail:
2629  for (i = 0; i < frames_priv->nb_mapped_frames; i++) {
2630  AVOpenCLFrameDescriptor *desc = &frames_priv->mapped_frames[i];
2631  for (p = 0; p < desc->nb_planes; p++) {
2632  if (desc->planes[p])
2633  clReleaseMemObject(desc->planes[p]);
2634  }
2635  }
2636  av_freep(&frames_priv->mapped_frames);
2637  frames_priv->nb_mapped_frames = 0;
2638  return err;
2639 }
2640 
2641 #endif
2642 
2643 #if HAVE_OPENCL_DRM_ARM
2644 
2645 typedef struct DRMARMtoOpenCLMapping {
2646  int nb_objects;
2647  cl_mem object_buffers[AV_DRM_MAX_PLANES];
2648  int nb_planes;
2649  cl_mem plane_images[AV_DRM_MAX_PLANES];
2650 } DRMARMtoOpenCLMapping;
2651 
2652 static void opencl_unmap_from_drm_arm(AVHWFramesContext *dst_fc,
2653  HWMapDescriptor *hwmap)
2654 {
2655  DRMARMtoOpenCLMapping *mapping = hwmap->priv;
2656  int i;
2657 
2658  for (i = 0; i < mapping->nb_planes; i++)
2659  clReleaseMemObject(mapping->plane_images[i]);
2660 
2661  for (i = 0; i < mapping->nb_objects; i++)
2662  clReleaseMemObject(mapping->object_buffers[i]);
2663 
2664  av_free(mapping);
2665 }
2666 
2667 static int opencl_map_from_drm_arm(AVHWFramesContext *dst_fc, AVFrame *dst,
2668  const AVFrame *src, int flags)
2669 {
2670  AVHWFramesContext *src_fc =
2671  (AVHWFramesContext*)src->hw_frames_ctx->data;
2672  AVOpenCLDeviceContext *dst_dev = dst_fc->device_ctx->hwctx;
2673  const AVDRMFrameDescriptor *desc;
2674  DRMARMtoOpenCLMapping *mapping = NULL;
2675  cl_mem_flags cl_flags;
2676  const cl_import_properties_arm props[3] = {
2677  CL_IMPORT_TYPE_ARM, CL_IMPORT_TYPE_DMA_BUF_ARM, 0,
2678  };
2679  cl_int cle;
2680  int err, i, j;
2681 
2682  desc = (const AVDRMFrameDescriptor*)src->data[0];
2683 
2684  cl_flags = opencl_mem_flags_for_mapping(flags);
2685  if (!cl_flags)
2686  return AVERROR(EINVAL);
2687 
2688  mapping = av_mallocz(sizeof(*mapping));
2689  if (!mapping)
2690  return AVERROR(ENOMEM);
2691 
2692  mapping->nb_objects = desc->nb_objects;
2693  for (i = 0; i < desc->nb_objects; i++) {
2694  int fd = desc->objects[i].fd;
2695 
2696  av_log(dst_fc, AV_LOG_DEBUG, "Map DRM PRIME fd %d to OpenCL.\n", fd);
2697 
2698  if (desc->objects[i].format_modifier) {
2699  av_log(dst_fc, AV_LOG_DEBUG, "Warning: object %d fd %d has "
2700  "nonzero format modifier %"PRId64", result may not "
2701  "be as expected.\n", i, fd,
2702  desc->objects[i].format_modifier);
2703  }
2704 
2705  mapping->object_buffers[i] =
2706  clImportMemoryARM(dst_dev->context, cl_flags, props,
2707  &fd, desc->objects[i].size, &cle);
2708  if (!mapping->object_buffers[i]) {
2709  av_log(dst_fc, AV_LOG_ERROR, "Failed to create CL buffer "
2710  "from object %d (fd %d, size %"SIZE_SPECIFIER") of DRM frame: %d.\n",
2711  i, fd, desc->objects[i].size, cle);
2712  err = AVERROR(EIO);
2713  goto fail;
2714  }
2715  }
2716 
2717  mapping->nb_planes = 0;
2718  for (i = 0; i < desc->nb_layers; i++) {
2719  const AVDRMLayerDescriptor *layer = &desc->layers[i];
2720 
2721  for (j = 0; j < layer->nb_planes; j++) {
2722  const AVDRMPlaneDescriptor *plane = &layer->planes[j];
2723  cl_mem plane_buffer;
2724  cl_image_format image_format;
2725  cl_image_desc image_desc;
2726  cl_buffer_region region;
2727  int p = mapping->nb_planes;
2728 
2729  err = opencl_get_plane_format(src_fc->sw_format, p,
2730  src_fc->width, src_fc->height,
2731  &image_format, &image_desc);
2732  if (err < 0) {
2733  av_log(dst_fc, AV_LOG_ERROR, "Invalid plane %d (DRM "
2734  "layer %d plane %d): %d.\n", p, i, j, err);
2735  goto fail;
2736  }
2737 
2738  region.origin = plane->offset;
2739  region.size = image_desc.image_row_pitch *
2740  image_desc.image_height;
2741 
2742  plane_buffer =
2743  clCreateSubBuffer(mapping->object_buffers[plane->object_index],
2744  cl_flags,
2745  CL_BUFFER_CREATE_TYPE_REGION,
2746  &region, &cle);
2747  if (!plane_buffer) {
2748  av_log(dst_fc, AV_LOG_ERROR, "Failed to create sub-buffer "
2749  "for plane %d: %d.\n", p, cle);
2750  err = AVERROR(EIO);
2751  goto fail;
2752  }
2753 
2754  image_desc.buffer = plane_buffer;
2755 
2756  mapping->plane_images[p] =
2757  clCreateImage(dst_dev->context, cl_flags,
2758  &image_format, &image_desc, NULL, &cle);
2759 
2760  // Unreference the sub-buffer immediately - we don't need it
2761  // directly and a reference is held by the image.
2762  clReleaseMemObject(plane_buffer);
2763 
2764  if (!mapping->plane_images[p]) {
2765  av_log(dst_fc, AV_LOG_ERROR, "Failed to create image "
2766  "for plane %d: %d.\n", p, cle);
2767  err = AVERROR(EIO);
2768  goto fail;
2769  }
2770 
2771  ++mapping->nb_planes;
2772  }
2773  }
2774 
2775  for (i = 0; i < mapping->nb_planes; i++)
2776  dst->data[i] = (uint8_t*)mapping->plane_images[i];
2777 
2778  err = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src,
2779  &opencl_unmap_from_drm_arm, mapping);
2780  if (err < 0)
2781  goto fail;
2782 
2783  dst->width = src->width;
2784  dst->height = src->height;
2785 
2786  return 0;
2787 
2788 fail:
2789  for (i = 0; i < mapping->nb_planes; i++) {
2790  clReleaseMemObject(mapping->plane_images[i]);
2791  }
2792  for (i = 0; i < mapping->nb_objects; i++) {
2793  if (mapping->object_buffers[i])
2794  clReleaseMemObject(mapping->object_buffers[i]);
2795  }
2796  av_free(mapping);
2797  return err;
2798 }
2799 
2800 #endif
2801 
2803  const AVFrame *src, int flags)
2804 {
2805  av_assert0(src->format == AV_PIX_FMT_OPENCL);
2806  if (hwfc->sw_format != dst->format)
2807  return AVERROR(ENOSYS);
2808  return opencl_map_frame(hwfc, dst, src, flags);
2809 }
2810 
2811 static int opencl_map_to(AVHWFramesContext *hwfc, AVFrame *dst,
2812  const AVFrame *src, int flags)
2813 {
2816  switch (src->format) {
2817 #if HAVE_OPENCL_DRM_BEIGNET
2818  case AV_PIX_FMT_DRM_PRIME:
2819  if (priv->beignet_drm_mapping_usable)
2820  return opencl_map_from_drm_beignet(hwfc, dst, src, flags);
2821  break;
2822 #endif
2823 #if HAVE_OPENCL_VAAPI_BEIGNET
2824  case AV_PIX_FMT_VAAPI:
2825  if (priv->beignet_drm_mapping_usable)
2826  return opencl_map_from_vaapi(hwfc, dst, src, flags);
2827  break;
2828 #endif
2829 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
2830  case AV_PIX_FMT_QSV:
2831  case AV_PIX_FMT_VAAPI:
2832  if (priv->qsv_mapping_usable)
2833  return opencl_map_from_qsv(hwfc, dst, src, flags);
2834  break;
2835 #endif
2836 #if HAVE_OPENCL_DXVA2
2837  case AV_PIX_FMT_DXVA2_VLD:
2838  if (priv->dxva2_mapping_usable)
2839  return opencl_map_from_dxva2(hwfc, dst, src, flags);
2840  break;
2841 #endif
2842 #if HAVE_OPENCL_D3D11
2843  case AV_PIX_FMT_D3D11:
2844  if (priv->d3d11_mapping_usable)
2845  return opencl_map_from_d3d11(hwfc, dst, src, flags);
2846  break;
2847 #endif
2848 #if HAVE_OPENCL_DRM_ARM
2849  case AV_PIX_FMT_DRM_PRIME:
2850  if (priv->drm_arm_mapping_usable)
2851  return opencl_map_from_drm_arm(hwfc, dst, src, flags);
2852  break;
2853 #endif
2854  }
2855  return AVERROR(ENOSYS);
2856 }
2857 
2859  AVHWFramesContext *src_fc, int flags)
2860 {
2862  switch (src_fc->device_ctx->type) {
2863 #if HAVE_OPENCL_DRM_BEIGNET
2864  case AV_HWDEVICE_TYPE_DRM:
2865  if (!priv->beignet_drm_mapping_usable)
2866  return AVERROR(ENOSYS);
2867  break;
2868 #endif
2869 #if HAVE_OPENCL_VAAPI_BEIGNET
2871  if (!priv->beignet_drm_mapping_usable)
2872  return AVERROR(ENOSYS);
2873  break;
2874 #endif
2875 #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
2876  case AV_HWDEVICE_TYPE_QSV:
2878  if (!priv->qsv_mapping_usable)
2879  return AVERROR(ENOSYS);
2880  break;
2881 #endif
2882 #if HAVE_OPENCL_DXVA2
2884  if (!priv->dxva2_mapping_usable)
2885  return AVERROR(ENOSYS);
2886  {
2887  int err;
2888  err = opencl_frames_derive_from_dxva2(dst_fc, src_fc, flags);
2889  if (err < 0)
2890  return err;
2891  }
2892  break;
2893 #endif
2894 #if HAVE_OPENCL_D3D11
2896  if (!priv->d3d11_mapping_usable)
2897  return AVERROR(ENOSYS);
2898  {
2899  int err;
2900  err = opencl_frames_derive_from_d3d11(dst_fc, src_fc, flags);
2901  if (err < 0)
2902  return err;
2903  }
2904  break;
2905 #endif
2906 #if HAVE_OPENCL_DRM_ARM
2907  case AV_HWDEVICE_TYPE_DRM:
2908  if (!priv->drm_arm_mapping_usable)
2909  return AVERROR(ENOSYS);
2910  break;
2911 #endif
2912  default:
2913  return AVERROR(ENOSYS);
2914  }
2915  return opencl_frames_init_command_queue(dst_fc);
2916 }
2917 
2920  .name = "OpenCL",
2921 
2922  .device_hwctx_size = sizeof(AVOpenCLDeviceContext),
2923  .device_priv_size = sizeof(OpenCLDeviceContext),
2924  .frames_hwctx_size = sizeof(AVOpenCLFramesContext),
2925  .frames_priv_size = sizeof(OpenCLFramesContext),
2926 
2927  .device_create = &opencl_device_create,
2928  .device_derive = &opencl_device_derive,
2929  .device_init = &opencl_device_init,
2930  .device_uninit = &opencl_device_uninit,
2931 
2932  .frames_get_constraints = &opencl_frames_get_constraints,
2933  .frames_init = &opencl_frames_init,
2934  .frames_uninit = &opencl_frames_uninit,
2935  .frames_get_buffer = &opencl_get_buffer,
2936 
2937  .transfer_get_formats = &opencl_transfer_get_formats,
2938  .transfer_data_to = &opencl_transfer_data_to,
2939  .transfer_data_from = &opencl_transfer_data_from,
2940 
2941  .map_from = &opencl_map_from,
2942  .map_to = &opencl_map_to,
2943  .frames_derive_to = &opencl_frames_derive_to,
2944 
2945  .pix_fmts = (const enum AVPixelFormat[]) {
2948  },
2949 };
channels
Definition: aptx.h:33
#define av_unused
Definition: attributes.h:131
uint8_t
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define flags(name, subs,...)
Definition: cbs_av1.c:572
static int FUNC() user_data(CodedBitstreamContext *ctx, RWContext *rw, MPEG2RawUserData *current)
#define fail()
Definition: checkasm.h:133
common internal and external API header
#define HAVE_BIGENDIAN
Definition: config.h:202
#define CONFIG_LIBMFX
Definition: config.h:538
#define NULL
Definition: coverity.c:32
static enum AVPixelFormat pix_fmt
static AVFrame * frame
static void comp(unsigned char *dst, ptrdiff_t dst_stride, unsigned char *src, ptrdiff_t src_stride, int add)
Definition: eamad.c:85
int
#define AV_NUM_DATA_POINTERS
Definition: frame.h:319
AVBufferRef * av_buffer_create(uint8_t *data, buffer_size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:29
AVBufferRef * av_buffer_pool_get(AVBufferPool *pool)
Allocate a new AVBuffer, reusing an old buffer from the pool when available.
Definition: buffer.c:379
AVBufferPool * av_buffer_pool_init2(buffer_size_t size, void *opaque, AVBufferRef *(*alloc)(void *opaque, buffer_size_t size), void(*pool_free)(void *opaque))
Allocate and initialize a buffer pool with a more complex allocator.
Definition: buffer.c:245
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:199
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
#define AVERROR_UNKNOWN
Unknown error, typically from an external library.
Definition: error.h:71
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array through a pointer to a pointer.
Definition: mem.c:206
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190
char * av_stristr(const char *s1, const char *s2)
Locate the first case-independent occurrence in the string haystack of the string needle.
Definition: avstring.c:56
int index
Definition: gxfenc.c:89
for(j=16;j >0;--j)
int ff_hwframe_map_create(AVBufferRef *hwframe_ref, AVFrame *dst, const AVFrame *src, void(*unmap)(AVHWFramesContext *ctx, HWMapDescriptor *hwmap), void *priv)
Definition: hwcontext.c:741
int av_hwframe_map(AVFrame *dst, const AVFrame *src, int flags)
Map a hardware frame.
Definition: hwcontext.c:793
int ff_hwframe_map_replace(AVFrame *dst, const AVFrame *src)
Replace the current hwmap of dst with the one from src, used for indirect mappings like VAAPI->(DRM)-...
Definition: hwcontext.c:927
@ AV_HWFRAME_MAP_READ
The mapping must be readable.
Definition: hwcontext.h:524
@ AV_HWFRAME_MAP_WRITE
The mapping must be writeable.
Definition: hwcontext.h:528
@ AV_HWFRAME_MAP_OVERWRITE
The mapped frame will be overwritten completely in subsequent operations, so the current frame data n...
Definition: hwcontext.h:534
AVHWFrameTransferDirection
Definition: hwcontext.h:415
@ AV_HWDEVICE_TYPE_D3D11VA
Definition: hwcontext.h:35
@ AV_HWDEVICE_TYPE_OPENCL
Definition: hwcontext.h:37
@ AV_HWDEVICE_TYPE_QSV
Definition: hwcontext.h:33
@ AV_HWDEVICE_TYPE_DRM
Definition: hwcontext.h:36
@ AV_HWDEVICE_TYPE_DXVA2
Definition: hwcontext.h:32
@ AV_HWDEVICE_TYPE_VAAPI
Definition: hwcontext.h:31
An API-specific header for AV_HWDEVICE_TYPE_D3D11VA.
API-specific header for AV_HWDEVICE_TYPE_DRM.
@ AV_DRM_MAX_PLANES
The maximum number of layers/planes in a DRM frame.
Definition: hwcontext_drm.h:39
An API-specific header for AV_HWDEVICE_TYPE_DXVA2.
static char * opencl_get_device_string(cl_device_id device_id, cl_device_info key)
static AVBufferRef * opencl_pool_alloc(void *opaque, buffer_size_t size)
const HWContextType ff_hwcontext_type_opencl
#define CHANNEL_ORDER(order, type)
static int opencl_frames_get_constraints(AVHWDeviceContext *hwdev, const void *hwconfig, AVHWFramesConstraints *constraints)
static int opencl_wait_events(AVHWFramesContext *hwfc, cl_event *events, int nb_events)
static void opencl_device_free(AVHWDeviceContext *hwdev)
static int opencl_frames_init_command_queue(AVHWFramesContext *hwfc)
static int opencl_transfer_data_from(AVHWFramesContext *hwfc, AVFrame *dst, const AVFrame *src)
static int opencl_check_device_extension(cl_device_id device_id, const char *name)
static struct @302 opencl_platform_params[]
static int opencl_map_to(AVHWFramesContext *hwfc, AVFrame *dst, const AVFrame *src, int flags)
cl_device_type type
static int opencl_frames_init(AVHWFramesContext *hwfc)
static struct @304 opencl_device_types[]
cl_platform_info name
static av_unused int opencl_check_extension(AVHWDeviceContext *hwdev, const char *name)
static void opencl_unmap_frame(AVHWFramesContext *hwfc, HWMapDescriptor *hwmap)
static int opencl_device_create(AVHWDeviceContext *hwdev, const char *device, AVDictionary *opts, int flags)
static int opencl_frames_derive_to(AVHWFramesContext *dst_fc, AVHWFramesContext *src_fc, int flags)
static int opencl_device_init(AVHWDeviceContext *hwdev)
static void opencl_pool_free(void *opaque, uint8_t *data)
static int opencl_enumerate_platforms(AVHWDeviceContext *hwdev, cl_uint *nb_platforms, cl_platform_id **platforms, void *context)
static cl_mem_flags opencl_mem_flags_for_mapping(int map_flags)
static void opencl_device_uninit(AVHWDeviceContext *hwdev)
#define CL_FUNC(name, desc)
const char * key
static int opencl_device_create_internal(AVHWDeviceContext *hwdev, const OpenCLDeviceSelector *selector, cl_context_properties *props)
static int opencl_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
static void opencl_frames_uninit(AVHWFramesContext *hwfc)
static void CL_CALLBACK opencl_error_callback(const char *errinfo, const void *private_info, size_t cb, void *user_data)
static struct @303 opencl_device_params[]
static int opencl_map_from(AVHWFramesContext *hwfc, AVFrame *dst, const AVFrame *src, int flags)
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)
static char * opencl_get_platform_string(cl_platform_id platform_id, cl_platform_info key)
static int opencl_transfer_data_to(AVHWFramesContext *hwfc, AVFrame *dst, const AVFrame *src)
static int opencl_transfer_get_formats(AVHWFramesContext *hwfc, enum AVHWFrameTransferDirection dir, enum AVPixelFormat **formats)
static int opencl_get_plane_format(enum AVPixelFormat pixfmt, int plane, int width, int height, cl_image_format *image_format, cl_image_desc *image_desc)
static int opencl_device_derive(AVHWDeviceContext *hwdev, AVHWDeviceContext *src_ctx, AVDictionary *opts, int flags)
static int opencl_filter_platform(AVHWDeviceContext *hwdev, cl_platform_id platform_id, const char *platform_name, void *context)
static int opencl_map_frame(AVHWFramesContext *hwfc, AVFrame *dst, const AVFrame *src, int flags)
static int opencl_filter_device(AVHWDeviceContext *hwdev, cl_device_id device_id, const char *device_name, void *context)
static int opencl_check_platform_extension(cl_platform_id platform_id, const char *name)
API-specific header for AV_HWDEVICE_TYPE_OPENCL.
enum AVHWDeviceType device_type
Definition: hwcontext_qsv.c:90
API-specific header for AV_HWDEVICE_TYPE_VAAPI.
const VDPAUPixFmtMap * map
static const int16_t alpha[]
Definition: ilbcdata.h:55
int i
Definition: input.c:407
enum AVPixelFormat pixfmt
Definition: kmsgrab.c:365
#define SIZE_SPECIFIER
Definition: internal.h:193
int buffer_size_t
Definition: internal.h:306
const char * desc
Definition: libsvtav1.c:83
Memory handling functions.
const char data[16]
Definition: mxf.c:142
void ID3D11Device
Definition: nvenc.h:28
const char * av_get_pix_fmt_name(enum AVPixelFormat pix_fmt)
Return the short name for a pixel format, NULL in case pix_fmt is unknown.
Definition: pixdesc.c:2489
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
Definition: pixdesc.c:2573
#define AV_PIX_FMT_FLAG_ALPHA
The pixel format has an alpha channel.
Definition: pixdesc.h:179
#define AV_PIX_FMT_FLAG_BITSTREAM
All values of a component are bit-wise packed end to end.
Definition: pixdesc.h:136
#define AV_PIX_FMT_FLAG_HWACCEL
Pixel format is an HW accelerated format.
Definition: pixdesc.h:140
#define AV_PIX_FMT_FLAG_BE
Pixel format is big-endian.
Definition: pixdesc.h:128
#define AV_PIX_FMT_FLAG_PAL
Pixel format has a palette in data[1], values are indexes in this palette.
Definition: pixdesc.h:132
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
@ AV_PIX_FMT_NV12
planar YUV 4:2:0, 12bpp, 1 plane for Y and 1 plane for the UV components, which are interleaved (firs...
Definition: pixfmt.h:89
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
@ AV_PIX_FMT_NB
number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of...
Definition: pixfmt.h:363
@ AV_PIX_FMT_DXVA2_VLD
HW decoding through DXVA2, Picture.data[3] contains a LPDIRECT3DSURFACE9 pointer.
Definition: pixfmt.h:137
@ AV_PIX_FMT_DRM_PRIME
DRM-managed buffers exposed through PRIME buffer sharing.
Definition: pixfmt.h:328
@ AV_PIX_FMT_D3D11
Hardware surfaces for Direct3D11.
Definition: pixfmt.h:313
@ AV_PIX_FMT_OPENCL
Hardware surfaces for OpenCL.
Definition: pixfmt.h:335
@ AV_PIX_FMT_VAAPI
Definition: pixfmt.h:122
@ AV_PIX_FMT_QSV
HW acceleration through QSV, data[3] contains a pointer to the mfxFrameSurface1 structure.
Definition: pixfmt.h:222
formats
Definition: signature.h:48
#define FF_ARRAY_ELEMS(a)
A reference to a data buffer.
Definition: buffer.h:84
uint8_t * data
The data buffer.
Definition: buffer.h:92
This struct is allocated as AVHWDeviceContext.hwctx.
ID3D11Device * device
Device used for texture creation and access.
This struct is allocated as AVHWFramesContext.hwctx.
ID3D11Texture2D * texture
The canonical texture used for pool allocation.
DRM frame descriptor.
DRM layer descriptor.
Definition: hwcontext_drm.h:96
AVDRMPlaneDescriptor planes[AV_DRM_MAX_PLANES]
Array of planes in this layer.
int nb_planes
Number of planes in the layer.
DRM object descriptor.
Definition: hwcontext_drm.h:48
DRM plane descriptor.
Definition: hwcontext_drm.h:74
ptrdiff_t pitch
Pitch (linesize) of this plane.
Definition: hwcontext_drm.h:87
int object_index
Index of the object containing this plane in the objects array of the enclosing frame descriptor.
Definition: hwcontext_drm.h:79
ptrdiff_t offset
Offset within that object of this plane.
Definition: hwcontext_drm.h:83
This struct is allocated as AVHWDeviceContext.hwctx.
IDirect3DDeviceManager9 * devmgr
This struct is allocated as AVHWFramesContext.hwctx.
IDirect3DSurface9 ** surfaces
The surface pool.
char * key
Definition: dict.h:82
char * value
Definition: dict.h:83
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:332
int width
Definition: frame.h:376
AVBufferRef * hw_frames_ctx
For hwaccel-format frames, this should be a reference to the AVHWFramesContext describing the frame.
Definition: frame.h:657
int height
Definition: frame.h:376
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:509
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
Definition: frame.h:349
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames,...
Definition: frame.h:391
This struct aggregates all the (hardware/vendor-specific) "high-level" state, i.e.
Definition: hwcontext.h:61
void * hwctx
The format-specific data, allocated and freed by libavutil along with this context.
Definition: hwcontext.h:92
void(* free)(struct AVHWDeviceContext *ctx)
This field may be set by the caller before calling av_hwdevice_ctx_init().
Definition: hwcontext.h:104
AVHWDeviceInternal * internal
Private data used internally by libavutil.
Definition: hwcontext.h:71
enum AVHWDeviceType type
This field identifies the underlying API used for hardware access.
Definition: hwcontext.h:79
This struct describes the constraints on hardware frames attached to a given device with a hardware-s...
Definition: hwcontext.h:453
int max_width
The maximum size of frames in this hw_frames_ctx.
Definition: hwcontext.h:478
enum AVPixelFormat * valid_hw_formats
A list of possible values for format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition: hwcontext.h:458
enum AVPixelFormat * valid_sw_formats
A list of possible values for sw_format in the hw_frames_ctx, terminated by AV_PIX_FMT_NONE.
Definition: hwcontext.h:465
This struct describes a set or pool of "hardware" frames (i.e.
Definition: hwcontext.h:124
void * hwctx
The format-specific data, allocated and freed automatically along with this context.
Definition: hwcontext.h:162
enum AVPixelFormat sw_format
The pixel format identifying the actual data layout of the hardware frames.
Definition: hwcontext.h:222
AVHWFramesInternal * internal
Private data used internally by libavutil.
Definition: hwcontext.h:134
int initial_pool_size
Initial size of the frame pool.
Definition: hwcontext.h:199
int width
The allocated dimensions of the frames in this pool.
Definition: hwcontext.h:229
AVHWDeviceContext * device_ctx
The parent AVHWDeviceContext.
Definition: hwcontext.h:149
AVBufferPool * pool
A pool from which the frames are allocated by av_hwframe_get_buffer().
Definition: hwcontext.h:190
AVBufferPool * pool_internal
OpenCL device details.
cl_device_id device_id
The primary device ID of the device.
cl_command_queue command_queue
The default command queue for this device, which will be used by all frames contexts which do not hav...
cl_context context
The OpenCL context which will contain all operations and frames on this device.
OpenCL frame descriptor for pool allocation.
OpenCL-specific data associated with a frame pool.
cl_command_queue command_queue
The command queue used for internal asynchronous operations on this device (av_hwframe_transfer_data(...
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
Definition: pixdesc.h:81
VAAPI connection details.
VADisplay display
The VADisplay handle, to be filled by the user.
enum AVHWDeviceType type
void * priv
Hardware-specific private data associated with the mapping.
AVFrame * source
A reference to the original source of the mapping.
cl_platform_id platform_id
cl_command_queue command_queue
int(* filter_platform)(AVHWDeviceContext *hwdev, cl_platform_id platform_id, const char *platform_name, void *context)
int(* enumerate_platforms)(AVHWDeviceContext *hwdev, cl_uint *nb_platforms, cl_platform_id **platforms, 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)
cl_command_queue command_queue
void * address[AV_NUM_DATA_POINTERS]
#define av_free(p)
#define av_malloc_array(a, b)
#define av_freep(p)
#define av_malloc(s)
#define av_log(a,...)
static uint8_t tmp[11]
Definition: aes_ctr.c:27
#define src
Definition: vp8dsp.c:255
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
AVFormatContext * ctx
Definition: movenc.c:48
AVDictionary * opts
Definition: movenc.c:50
#define height
#define width
int size
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:220
if(ret< 0)
Definition: vf_mcdeint.c:282
static void hsub(htype *dst, const htype *src, int bins)
Definition: vf_median.c:76
static double c[64]