hwcontext_opencl.c 95 KB

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