hwcontext_opencl.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942
  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. #if CONFIG_LIBMFX
  43. #include <mfx/mfxstructures.h>
  44. #endif
  45. #include <va/va.h>
  46. #include <CL/cl_va_api_media_sharing_intel.h>
  47. #include "hwcontext_vaapi.h"
  48. #endif
  49. #if HAVE_OPENCL_DXVA2
  50. #define COBJMACROS
  51. #include <CL/cl_dx9_media_sharing.h>
  52. #include <dxva2api.h>
  53. #include "hwcontext_dxva2.h"
  54. #endif
  55. #if HAVE_OPENCL_D3D11
  56. #include <CL/cl_d3d11.h>
  57. #include "hwcontext_d3d11va.h"
  58. #endif
  59. #if HAVE_OPENCL_DRM_ARM
  60. #include <CL/cl_ext.h>
  61. #include <drm_fourcc.h>
  62. #include "hwcontext_drm.h"
  63. #endif
  64. typedef struct OpenCLDeviceContext {
  65. // Default command queue to use for transfer/mapping operations on
  66. // the device. If the user supplies one, this is a reference to it.
  67. // Otherwise, it is newly-created.
  68. cl_command_queue command_queue;
  69. // The platform the context exists on. This is needed to query and
  70. // retrieve extension functions.
  71. cl_platform_id platform_id;
  72. // Platform/device-specific functions.
  73. #if HAVE_OPENCL_DRM_BEIGNET
  74. int beignet_drm_mapping_usable;
  75. clCreateImageFromFdINTEL_fn clCreateImageFromFdINTEL;
  76. #endif
  77. #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
  78. int qsv_mapping_usable;
  79. clCreateFromVA_APIMediaSurfaceINTEL_fn
  80. clCreateFromVA_APIMediaSurfaceINTEL;
  81. clEnqueueAcquireVA_APIMediaSurfacesINTEL_fn
  82. clEnqueueAcquireVA_APIMediaSurfacesINTEL;
  83. clEnqueueReleaseVA_APIMediaSurfacesINTEL_fn
  84. clEnqueueReleaseVA_APIMediaSurfacesINTEL;
  85. #endif
  86. #if HAVE_OPENCL_DXVA2
  87. int dxva2_mapping_usable;
  88. cl_dx9_media_adapter_type_khr dx9_media_adapter_type;
  89. clCreateFromDX9MediaSurfaceKHR_fn
  90. clCreateFromDX9MediaSurfaceKHR;
  91. clEnqueueAcquireDX9MediaSurfacesKHR_fn
  92. clEnqueueAcquireDX9MediaSurfacesKHR;
  93. clEnqueueReleaseDX9MediaSurfacesKHR_fn
  94. clEnqueueReleaseDX9MediaSurfacesKHR;
  95. #endif
  96. #if HAVE_OPENCL_D3D11
  97. int d3d11_mapping_usable;
  98. clCreateFromD3D11Texture2DKHR_fn
  99. clCreateFromD3D11Texture2DKHR;
  100. clEnqueueAcquireD3D11ObjectsKHR_fn
  101. clEnqueueAcquireD3D11ObjectsKHR;
  102. clEnqueueReleaseD3D11ObjectsKHR_fn
  103. clEnqueueReleaseD3D11ObjectsKHR;
  104. #endif
  105. #if HAVE_OPENCL_DRM_ARM
  106. int drm_arm_mapping_usable;
  107. #endif
  108. } OpenCLDeviceContext;
  109. typedef struct OpenCLFramesContext {
  110. // Command queue used for transfer/mapping operations on this frames
  111. // context. If the user supplies one, this is a reference to it.
  112. // Otherwise, it is a reference to the default command queue for the
  113. // device.
  114. cl_command_queue command_queue;
  115. #if HAVE_OPENCL_DXVA2 || HAVE_OPENCL_D3D11
  116. // For mapping APIs which have separate creation and acquire/release
  117. // steps, this stores the OpenCL memory objects corresponding to each
  118. // frame.
  119. int nb_mapped_frames;
  120. AVOpenCLFrameDescriptor *mapped_frames;
  121. #endif
  122. } OpenCLFramesContext;
  123. static void CL_CALLBACK opencl_error_callback(const char *errinfo,
  124. const void *private_info,
  125. size_t cb,
  126. void *user_data)
  127. {
  128. AVHWDeviceContext *ctx = user_data;
  129. av_log(ctx, AV_LOG_ERROR, "OpenCL error: %s\n", errinfo);
  130. }
  131. static void opencl_device_free(AVHWDeviceContext *hwdev)
  132. {
  133. AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
  134. cl_int cle;
  135. cle = clReleaseContext(hwctx->context);
  136. if (cle != CL_SUCCESS) {
  137. av_log(hwdev, AV_LOG_ERROR, "Failed to release OpenCL "
  138. "context: %d.\n", cle);
  139. }
  140. }
  141. static struct {
  142. const char *key;
  143. cl_platform_info name;
  144. } opencl_platform_params[] = {
  145. { "platform_profile", CL_PLATFORM_PROFILE },
  146. { "platform_version", CL_PLATFORM_VERSION },
  147. { "platform_name", CL_PLATFORM_NAME },
  148. { "platform_vendor", CL_PLATFORM_VENDOR },
  149. { "platform_extensions", CL_PLATFORM_EXTENSIONS },
  150. };
  151. static struct {
  152. const char *key;
  153. cl_device_info name;
  154. } opencl_device_params[] = {
  155. { "device_name", CL_DEVICE_NAME },
  156. { "device_vendor", CL_DEVICE_VENDOR },
  157. { "driver_version", CL_DRIVER_VERSION },
  158. { "device_version", CL_DEVICE_VERSION },
  159. { "device_profile", CL_DEVICE_PROFILE },
  160. { "device_extensions", CL_DEVICE_EXTENSIONS },
  161. };
  162. static struct {
  163. const char *key;
  164. cl_device_type type;
  165. } opencl_device_types[] = {
  166. { "cpu", CL_DEVICE_TYPE_CPU },
  167. { "gpu", CL_DEVICE_TYPE_GPU },
  168. { "accelerator", CL_DEVICE_TYPE_ACCELERATOR },
  169. { "custom", CL_DEVICE_TYPE_CUSTOM },
  170. { "default", CL_DEVICE_TYPE_DEFAULT },
  171. { "all", CL_DEVICE_TYPE_ALL },
  172. };
  173. static char *opencl_get_platform_string(cl_platform_id platform_id,
  174. cl_platform_info key)
  175. {
  176. char *str;
  177. size_t size;
  178. cl_int cle;
  179. cle = clGetPlatformInfo(platform_id, key, 0, NULL, &size);
  180. if (cle != CL_SUCCESS)
  181. return NULL;
  182. str = av_malloc(size);
  183. if (!str)
  184. return NULL;
  185. cle = clGetPlatformInfo(platform_id, key, size, str, &size);
  186. if (cle != CL_SUCCESS) {
  187. av_free(str);
  188. return NULL;
  189. }
  190. av_assert0(strlen(str) + 1 == size);
  191. return str;
  192. }
  193. static char *opencl_get_device_string(cl_device_id device_id,
  194. cl_device_info key)
  195. {
  196. char *str;
  197. size_t size;
  198. cl_int cle;
  199. cle = clGetDeviceInfo(device_id, key, 0, NULL, &size);
  200. if (cle != CL_SUCCESS)
  201. return NULL;
  202. str = av_malloc(size);
  203. if (!str)
  204. return NULL;
  205. cle = clGetDeviceInfo(device_id, key, size, str, &size);
  206. if (cle != CL_SUCCESS) {
  207. av_free(str);
  208. return NULL;
  209. }
  210. av_assert0(strlen(str) + 1== size);
  211. return str;
  212. }
  213. static int opencl_check_platform_extension(cl_platform_id platform_id,
  214. const char *name)
  215. {
  216. char *str;
  217. int found = 0;
  218. str = opencl_get_platform_string(platform_id,
  219. CL_PLATFORM_EXTENSIONS);
  220. if (str && strstr(str, name))
  221. found = 1;
  222. av_free(str);
  223. return found;
  224. }
  225. static int opencl_check_device_extension(cl_device_id device_id,
  226. const char *name)
  227. {
  228. char *str;
  229. int found = 0;
  230. str = opencl_get_device_string(device_id,
  231. CL_DEVICE_EXTENSIONS);
  232. if (str && strstr(str, name))
  233. found = 1;
  234. av_free(str);
  235. return found;
  236. }
  237. static av_unused int opencl_check_extension(AVHWDeviceContext *hwdev,
  238. const char *name)
  239. {
  240. AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
  241. OpenCLDeviceContext *priv = hwdev->internal->priv;
  242. if (opencl_check_platform_extension(priv->platform_id, name)) {
  243. av_log(hwdev, AV_LOG_DEBUG,
  244. "%s found as platform extension.\n", name);
  245. return 1;
  246. }
  247. if (opencl_check_device_extension(hwctx->device_id, name)) {
  248. av_log(hwdev, AV_LOG_DEBUG,
  249. "%s found as device extension.\n", name);
  250. return 1;
  251. }
  252. return 0;
  253. }
  254. static int opencl_enumerate_platforms(AVHWDeviceContext *hwdev,
  255. cl_uint *nb_platforms,
  256. cl_platform_id **platforms,
  257. void *context)
  258. {
  259. cl_int cle;
  260. cle = clGetPlatformIDs(0, NULL, nb_platforms);
  261. if (cle != CL_SUCCESS) {
  262. av_log(hwdev, AV_LOG_ERROR, "Failed to get number of "
  263. "OpenCL platforms: %d.\n", cle);
  264. return AVERROR(ENODEV);
  265. }
  266. av_log(hwdev, AV_LOG_DEBUG, "%u OpenCL platforms found.\n",
  267. *nb_platforms);
  268. *platforms = av_malloc_array(*nb_platforms, sizeof(**platforms));
  269. if (!*platforms)
  270. return AVERROR(ENOMEM);
  271. cle = clGetPlatformIDs(*nb_platforms, *platforms, NULL);
  272. if (cle != CL_SUCCESS) {
  273. av_log(hwdev, AV_LOG_ERROR, "Failed to get list of OpenCL "
  274. "platforms: %d.\n", cle);
  275. av_freep(platforms);
  276. return AVERROR(ENODEV);
  277. }
  278. return 0;
  279. }
  280. static int opencl_filter_platform(AVHWDeviceContext *hwdev,
  281. cl_platform_id platform_id,
  282. const char *platform_name,
  283. void *context)
  284. {
  285. AVDictionary *opts = context;
  286. const AVDictionaryEntry *param;
  287. char *str;
  288. int i, ret = 0;
  289. for (i = 0; i < FF_ARRAY_ELEMS(opencl_platform_params); i++) {
  290. param = av_dict_get(opts, opencl_platform_params[i].key,
  291. NULL, 0);
  292. if (!param)
  293. continue;
  294. str = opencl_get_platform_string(platform_id,
  295. opencl_platform_params[i].name);
  296. if (!str) {
  297. av_log(hwdev, AV_LOG_ERROR, "Failed to query %s "
  298. "of platform \"%s\".\n",
  299. opencl_platform_params[i].key, platform_name);
  300. return AVERROR_UNKNOWN;
  301. }
  302. if (!av_stristr(str, param->value)) {
  303. av_log(hwdev, AV_LOG_DEBUG, "%s does not match (\"%s\").\n",
  304. param->key, str);
  305. ret = 1;
  306. }
  307. av_free(str);
  308. }
  309. return ret;
  310. }
  311. static int opencl_enumerate_devices(AVHWDeviceContext *hwdev,
  312. cl_platform_id platform_id,
  313. const char *platform_name,
  314. cl_uint *nb_devices,
  315. cl_device_id **devices,
  316. void *context)
  317. {
  318. cl_int cle;
  319. cle = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL,
  320. 0, NULL, nb_devices);
  321. if (cle == CL_DEVICE_NOT_FOUND) {
  322. av_log(hwdev, AV_LOG_DEBUG, "No devices found "
  323. "on platform \"%s\".\n", platform_name);
  324. *nb_devices = 0;
  325. return 0;
  326. } else if (cle != CL_SUCCESS) {
  327. av_log(hwdev, AV_LOG_ERROR, "Failed to get number of devices "
  328. "on platform \"%s\": %d.\n", platform_name, cle);
  329. return AVERROR(ENODEV);
  330. }
  331. av_log(hwdev, AV_LOG_DEBUG, "%u OpenCL devices found on "
  332. "platform \"%s\".\n", *nb_devices, platform_name);
  333. *devices = av_malloc_array(*nb_devices, sizeof(**devices));
  334. if (!*devices)
  335. return AVERROR(ENOMEM);
  336. cle = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_ALL,
  337. *nb_devices, *devices, NULL);
  338. if (cle != CL_SUCCESS) {
  339. av_log(hwdev, AV_LOG_ERROR, "Failed to get list of devices "
  340. "on platform \"%s\": %d.\n", platform_name, cle);
  341. av_freep(devices);
  342. return AVERROR(ENODEV);
  343. }
  344. return 0;
  345. }
  346. static int opencl_filter_device(AVHWDeviceContext *hwdev,
  347. cl_device_id device_id,
  348. const char *device_name,
  349. void *context)
  350. {
  351. AVDictionary *opts = context;
  352. const AVDictionaryEntry *param;
  353. char *str;
  354. int i, ret = 0;
  355. param = av_dict_get(opts, "device_type", NULL, 0);
  356. if (param) {
  357. cl_device_type match_type = 0, device_type;
  358. cl_int cle;
  359. for (i = 0; i < FF_ARRAY_ELEMS(opencl_device_types); i++) {
  360. if (!strcmp(opencl_device_types[i].key, param->value)) {
  361. match_type = opencl_device_types[i].type;
  362. break;
  363. }
  364. }
  365. if (!match_type) {
  366. av_log(hwdev, AV_LOG_ERROR, "Unknown device type %s.\n",
  367. param->value);
  368. return AVERROR(EINVAL);
  369. }
  370. cle = clGetDeviceInfo(device_id, CL_DEVICE_TYPE,
  371. sizeof(device_type), &device_type, NULL);
  372. if (cle != CL_SUCCESS) {
  373. av_log(hwdev, AV_LOG_ERROR, "Failed to query device type "
  374. "of device \"%s\".\n", device_name);
  375. return AVERROR_UNKNOWN;
  376. }
  377. if (!(device_type & match_type)) {
  378. av_log(hwdev, AV_LOG_DEBUG, "device_type does not match.\n");
  379. return 1;
  380. }
  381. }
  382. for (i = 0; i < FF_ARRAY_ELEMS(opencl_device_params); i++) {
  383. param = av_dict_get(opts, opencl_device_params[i].key,
  384. NULL, 0);
  385. if (!param)
  386. continue;
  387. str = opencl_get_device_string(device_id,
  388. opencl_device_params[i].name);
  389. if (!str) {
  390. av_log(hwdev, AV_LOG_ERROR, "Failed to query %s "
  391. "of device \"%s\".\n",
  392. opencl_device_params[i].key, device_name);
  393. return AVERROR_UNKNOWN;
  394. }
  395. if (!av_stristr(str, param->value)) {
  396. av_log(hwdev, AV_LOG_DEBUG, "%s does not match (\"%s\").\n",
  397. param->key, str);
  398. ret = 1;
  399. }
  400. av_free(str);
  401. }
  402. return ret;
  403. }
  404. typedef struct OpenCLDeviceSelector {
  405. int platform_index;
  406. int device_index;
  407. void *context;
  408. int (*enumerate_platforms)(AVHWDeviceContext *hwdev,
  409. cl_uint *nb_platforms,
  410. cl_platform_id **platforms,
  411. void *context);
  412. int (*filter_platform) (AVHWDeviceContext *hwdev,
  413. cl_platform_id platform_id,
  414. const char *platform_name,
  415. void *context);
  416. int (*enumerate_devices) (AVHWDeviceContext *hwdev,
  417. cl_platform_id platform_id,
  418. const char *platform_name,
  419. cl_uint *nb_devices,
  420. cl_device_id **devices,
  421. void *context);
  422. int (*filter_device) (AVHWDeviceContext *hwdev,
  423. cl_device_id device_id,
  424. const char *device_name,
  425. void *context);
  426. } OpenCLDeviceSelector;
  427. static int opencl_device_create_internal(AVHWDeviceContext *hwdev,
  428. const OpenCLDeviceSelector *selector,
  429. cl_context_properties *props)
  430. {
  431. cl_uint nb_platforms;
  432. cl_platform_id *platforms = NULL;
  433. cl_platform_id platform_id;
  434. cl_uint nb_devices;
  435. cl_device_id *devices = NULL;
  436. AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
  437. cl_int cle;
  438. cl_context_properties default_props[3];
  439. char *platform_name_src = NULL,
  440. *device_name_src = NULL;
  441. int err, found, p, d;
  442. av_assert0(selector->enumerate_platforms &&
  443. selector->enumerate_devices);
  444. err = selector->enumerate_platforms(hwdev, &nb_platforms, &platforms,
  445. selector->context);
  446. if (err)
  447. return err;
  448. found = 0;
  449. for (p = 0; p < nb_platforms; p++) {
  450. const char *platform_name;
  451. if (selector->platform_index >= 0 &&
  452. selector->platform_index != p)
  453. continue;
  454. av_freep(&platform_name_src);
  455. platform_name_src = opencl_get_platform_string(platforms[p],
  456. CL_PLATFORM_NAME);
  457. if (platform_name_src)
  458. platform_name = platform_name_src;
  459. else
  460. platform_name = "Unknown Platform";
  461. if (selector->filter_platform) {
  462. err = selector->filter_platform(hwdev, platforms[p],
  463. platform_name,
  464. selector->context);
  465. if (err < 0)
  466. goto fail;
  467. if (err > 0)
  468. continue;
  469. }
  470. err = selector->enumerate_devices(hwdev, platforms[p], platform_name,
  471. &nb_devices, &devices,
  472. selector->context);
  473. if (err < 0)
  474. continue;
  475. for (d = 0; d < nb_devices; d++) {
  476. const char *device_name;
  477. if (selector->device_index >= 0 &&
  478. selector->device_index != d)
  479. continue;
  480. av_freep(&device_name_src);
  481. device_name_src = opencl_get_device_string(devices[d],
  482. CL_DEVICE_NAME);
  483. if (device_name_src)
  484. device_name = device_name_src;
  485. else
  486. device_name = "Unknown Device";
  487. if (selector->filter_device) {
  488. err = selector->filter_device(hwdev, devices[d],
  489. device_name,
  490. selector->context);
  491. if (err < 0)
  492. goto fail;
  493. if (err > 0)
  494. continue;
  495. }
  496. av_log(hwdev, AV_LOG_VERBOSE, "%d.%d: %s / %s\n", p, d,
  497. platform_name, device_name);
  498. ++found;
  499. platform_id = platforms[p];
  500. hwctx->device_id = devices[d];
  501. }
  502. av_freep(&devices);
  503. }
  504. if (found == 0) {
  505. av_log(hwdev, AV_LOG_ERROR, "No matching devices found.\n");
  506. err = AVERROR(ENODEV);
  507. goto fail;
  508. }
  509. if (found > 1) {
  510. av_log(hwdev, AV_LOG_ERROR, "More than one matching device found.\n");
  511. err = AVERROR(ENODEV);
  512. goto fail;
  513. }
  514. if (!props) {
  515. props = default_props;
  516. default_props[0] = CL_CONTEXT_PLATFORM;
  517. default_props[1] = (intptr_t)platform_id;
  518. default_props[2] = 0;
  519. } else {
  520. if (props[0] == CL_CONTEXT_PLATFORM && props[1] == 0)
  521. props[1] = (intptr_t)platform_id;
  522. }
  523. hwctx->context = clCreateContext(props, 1, &hwctx->device_id,
  524. &opencl_error_callback, hwdev, &cle);
  525. if (!hwctx->context) {
  526. av_log(hwdev, AV_LOG_ERROR, "Failed to create OpenCL context: "
  527. "%d.\n", cle);
  528. err = AVERROR(ENODEV);
  529. goto fail;
  530. }
  531. hwdev->free = &opencl_device_free;
  532. err = 0;
  533. fail:
  534. av_freep(&platform_name_src);
  535. av_freep(&device_name_src);
  536. av_freep(&platforms);
  537. av_freep(&devices);
  538. return err;
  539. }
  540. static int opencl_device_create(AVHWDeviceContext *hwdev, const char *device,
  541. AVDictionary *opts, int flags)
  542. {
  543. OpenCLDeviceSelector selector = {
  544. .context = opts,
  545. .enumerate_platforms = &opencl_enumerate_platforms,
  546. .filter_platform = &opencl_filter_platform,
  547. .enumerate_devices = &opencl_enumerate_devices,
  548. .filter_device = &opencl_filter_device,
  549. };
  550. if (device && device[0]) {
  551. // Match one or both indices for platform and device.
  552. int d = -1, p = -1, ret;
  553. if (device[0] == '.')
  554. ret = sscanf(device, ".%d", &d);
  555. else
  556. ret = sscanf(device, "%d.%d", &p, &d);
  557. if (ret < 1) {
  558. av_log(hwdev, AV_LOG_ERROR, "Invalid OpenCL platform/device "
  559. "index specification \"%s\".\n", device);
  560. return AVERROR(EINVAL);
  561. }
  562. selector.platform_index = p;
  563. selector.device_index = d;
  564. } else {
  565. selector.platform_index = -1;
  566. selector.device_index = -1;
  567. }
  568. return opencl_device_create_internal(hwdev, &selector, NULL);
  569. }
  570. static int opencl_device_init(AVHWDeviceContext *hwdev)
  571. {
  572. AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
  573. OpenCLDeviceContext *priv = hwdev->internal->priv;
  574. cl_int cle;
  575. if (hwctx->command_queue) {
  576. cle = clRetainCommandQueue(hwctx->command_queue);
  577. if (cle != CL_SUCCESS) {
  578. av_log(hwdev, AV_LOG_ERROR, "Failed to retain external "
  579. "command queue: %d.\n", cle);
  580. return AVERROR(EIO);
  581. }
  582. priv->command_queue = hwctx->command_queue;
  583. } else {
  584. priv->command_queue = clCreateCommandQueue(hwctx->context,
  585. hwctx->device_id,
  586. 0, &cle);
  587. if (!priv->command_queue) {
  588. av_log(hwdev, AV_LOG_ERROR, "Failed to create internal "
  589. "command queue: %d.\n", cle);
  590. return AVERROR(EIO);
  591. }
  592. }
  593. cle = clGetDeviceInfo(hwctx->device_id, CL_DEVICE_PLATFORM,
  594. sizeof(priv->platform_id), &priv->platform_id,
  595. NULL);
  596. if (cle != CL_SUCCESS) {
  597. av_log(hwdev, AV_LOG_ERROR, "Failed to determine the OpenCL "
  598. "platform containing the device.\n");
  599. return AVERROR(EIO);
  600. }
  601. #define CL_FUNC(name, desc) do { \
  602. if (fail) \
  603. break; \
  604. priv->name = clGetExtensionFunctionAddressForPlatform( \
  605. priv->platform_id, #name); \
  606. if (!priv->name) { \
  607. av_log(hwdev, AV_LOG_VERBOSE, \
  608. desc " function not found (%s).\n", #name); \
  609. fail = 1; \
  610. } else { \
  611. av_log(hwdev, AV_LOG_VERBOSE, \
  612. desc " function found (%s).\n", #name); \
  613. } \
  614. } while (0)
  615. #if HAVE_OPENCL_DRM_BEIGNET
  616. {
  617. int fail = 0;
  618. CL_FUNC(clCreateImageFromFdINTEL,
  619. "Beignet DRM to OpenCL image mapping");
  620. if (fail) {
  621. av_log(hwdev, AV_LOG_WARNING, "Beignet DRM to OpenCL "
  622. "mapping not usable.\n");
  623. priv->beignet_drm_mapping_usable = 0;
  624. } else {
  625. priv->beignet_drm_mapping_usable = 1;
  626. }
  627. }
  628. #endif
  629. #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
  630. {
  631. size_t props_size;
  632. cl_context_properties *props = NULL;
  633. VADisplay va_display;
  634. const char *va_ext = "cl_intel_va_api_media_sharing";
  635. int i, fail = 0;
  636. if (!opencl_check_extension(hwdev, va_ext)) {
  637. av_log(hwdev, AV_LOG_VERBOSE, "The %s extension is "
  638. "required for QSV to OpenCL mapping.\n", va_ext);
  639. goto no_qsv;
  640. }
  641. cle = clGetContextInfo(hwctx->context, CL_CONTEXT_PROPERTIES,
  642. 0, NULL, &props_size);
  643. if (cle != CL_SUCCESS) {
  644. av_log(hwdev, AV_LOG_VERBOSE, "Failed to get context "
  645. "properties: %d.\n", cle);
  646. goto no_qsv;
  647. }
  648. if (props_size == 0) {
  649. av_log(hwdev, AV_LOG_VERBOSE, "Media sharing must be "
  650. "enabled on context creation to use QSV to "
  651. "OpenCL mapping.\n");
  652. goto no_qsv;
  653. }
  654. props = av_malloc(props_size);
  655. if (!props)
  656. return AVERROR(ENOMEM);
  657. cle = clGetContextInfo(hwctx->context, CL_CONTEXT_PROPERTIES,
  658. props_size, props, NULL);
  659. if (cle != CL_SUCCESS) {
  660. av_log(hwdev, AV_LOG_VERBOSE, "Failed to get context "
  661. "properties: %d.\n", cle);
  662. goto no_qsv;
  663. }
  664. va_display = NULL;
  665. for (i = 0; i < (props_size / sizeof(*props) - 1); i++) {
  666. if (props[i] == CL_CONTEXT_VA_API_DISPLAY_INTEL) {
  667. va_display = (VADisplay)(intptr_t)props[i+1];
  668. break;
  669. }
  670. }
  671. if (!va_display) {
  672. av_log(hwdev, AV_LOG_VERBOSE, "Media sharing must be "
  673. "enabled on context creation to use QSV to "
  674. "OpenCL mapping.\n");
  675. goto no_qsv;
  676. }
  677. if (!vaDisplayIsValid(va_display)) {
  678. av_log(hwdev, AV_LOG_VERBOSE, "A valid VADisplay is "
  679. "required on context creation to use QSV to "
  680. "OpenCL mapping.\n");
  681. goto no_qsv;
  682. }
  683. CL_FUNC(clCreateFromVA_APIMediaSurfaceINTEL,
  684. "Intel QSV to OpenCL mapping");
  685. CL_FUNC(clEnqueueAcquireVA_APIMediaSurfacesINTEL,
  686. "Intel QSV in OpenCL acquire");
  687. CL_FUNC(clEnqueueReleaseVA_APIMediaSurfacesINTEL,
  688. "Intel QSV in OpenCL release");
  689. if (fail) {
  690. no_qsv:
  691. av_log(hwdev, AV_LOG_WARNING, "QSV to OpenCL mapping "
  692. "not usable.\n");
  693. priv->qsv_mapping_usable = 0;
  694. } else {
  695. priv->qsv_mapping_usable = 1;
  696. }
  697. av_free(props);
  698. }
  699. #endif
  700. #if HAVE_OPENCL_DXVA2
  701. {
  702. int fail = 0;
  703. CL_FUNC(clCreateFromDX9MediaSurfaceKHR,
  704. "DXVA2 to OpenCL mapping");
  705. CL_FUNC(clEnqueueAcquireDX9MediaSurfacesKHR,
  706. "DXVA2 in OpenCL acquire");
  707. CL_FUNC(clEnqueueReleaseDX9MediaSurfacesKHR,
  708. "DXVA2 in OpenCL release");
  709. if (fail) {
  710. av_log(hwdev, AV_LOG_WARNING, "DXVA2 to OpenCL mapping "
  711. "not usable.\n");
  712. priv->dxva2_mapping_usable = 0;
  713. } else {
  714. priv->dx9_media_adapter_type = CL_ADAPTER_D3D9EX_KHR;
  715. priv->dxva2_mapping_usable = 1;
  716. }
  717. }
  718. #endif
  719. #if HAVE_OPENCL_D3D11
  720. {
  721. const char *d3d11_ext = "cl_khr_d3d11_sharing";
  722. const char *nv12_ext = "cl_intel_d3d11_nv12_media_sharing";
  723. int fail = 0;
  724. if (!opencl_check_extension(hwdev, d3d11_ext)) {
  725. av_log(hwdev, AV_LOG_VERBOSE, "The %s extension is "
  726. "required for D3D11 to OpenCL mapping.\n", d3d11_ext);
  727. fail = 1;
  728. } else if (!opencl_check_extension(hwdev, nv12_ext)) {
  729. av_log(hwdev, AV_LOG_VERBOSE, "The %s extension may be "
  730. "required for D3D11 to OpenCL mapping.\n", nv12_ext);
  731. // Not fatal.
  732. }
  733. CL_FUNC(clCreateFromD3D11Texture2DKHR,
  734. "D3D11 to OpenCL mapping");
  735. CL_FUNC(clEnqueueAcquireD3D11ObjectsKHR,
  736. "D3D11 in OpenCL acquire");
  737. CL_FUNC(clEnqueueReleaseD3D11ObjectsKHR,
  738. "D3D11 in OpenCL release");
  739. if (fail) {
  740. av_log(hwdev, AV_LOG_WARNING, "D3D11 to OpenCL mapping "
  741. "not usable.\n");
  742. priv->d3d11_mapping_usable = 0;
  743. } else {
  744. priv->d3d11_mapping_usable = 1;
  745. }
  746. }
  747. #endif
  748. #if HAVE_OPENCL_DRM_ARM
  749. {
  750. const char *drm_arm_ext = "cl_arm_import_memory";
  751. const char *image_ext = "cl_khr_image2d_from_buffer";
  752. int fail = 0;
  753. if (!opencl_check_extension(hwdev, drm_arm_ext)) {
  754. av_log(hwdev, AV_LOG_VERBOSE, "The %s extension is "
  755. "required for DRM to OpenCL mapping on ARM.\n",
  756. drm_arm_ext);
  757. fail = 1;
  758. }
  759. if (!opencl_check_extension(hwdev, image_ext)) {
  760. av_log(hwdev, AV_LOG_VERBOSE, "The %s extension is "
  761. "required for DRM to OpenCL mapping on ARM.\n",
  762. image_ext);
  763. fail = 1;
  764. }
  765. // clImportMemoryARM() is linked statically.
  766. if (fail) {
  767. av_log(hwdev, AV_LOG_WARNING, "DRM to OpenCL mapping on ARM "
  768. "not usable.\n");
  769. priv->drm_arm_mapping_usable = 0;
  770. } else {
  771. priv->drm_arm_mapping_usable = 1;
  772. }
  773. }
  774. #endif
  775. #undef CL_FUNC
  776. return 0;
  777. }
  778. static void opencl_device_uninit(AVHWDeviceContext *hwdev)
  779. {
  780. OpenCLDeviceContext *priv = hwdev->internal->priv;
  781. cl_int cle;
  782. if (priv->command_queue) {
  783. cle = clReleaseCommandQueue(priv->command_queue);
  784. if (cle != CL_SUCCESS) {
  785. av_log(hwdev, AV_LOG_ERROR, "Failed to release internal "
  786. "command queue reference: %d.\n", cle);
  787. }
  788. priv->command_queue = NULL;
  789. }
  790. }
  791. #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
  792. static int opencl_filter_intel_media_vaapi_platform(AVHWDeviceContext *hwdev,
  793. cl_platform_id platform_id,
  794. const char *platform_name,
  795. void *context)
  796. {
  797. // This doesn't exist as a platform extension, so just test whether
  798. // the function we will use for device enumeration exists.
  799. if (!clGetExtensionFunctionAddressForPlatform(platform_id,
  800. "clGetDeviceIDsFromVA_APIMediaAdapterINTEL")) {
  801. av_log(hwdev, AV_LOG_DEBUG, "Platform %s does not export the "
  802. "VAAPI device enumeration function.\n", platform_name);
  803. return 1;
  804. } else {
  805. return 0;
  806. }
  807. }
  808. static int opencl_enumerate_intel_media_vaapi_devices(AVHWDeviceContext *hwdev,
  809. cl_platform_id platform_id,
  810. const char *platform_name,
  811. cl_uint *nb_devices,
  812. cl_device_id **devices,
  813. void *context)
  814. {
  815. VADisplay va_display = context;
  816. clGetDeviceIDsFromVA_APIMediaAdapterINTEL_fn
  817. clGetDeviceIDsFromVA_APIMediaAdapterINTEL;
  818. cl_int cle;
  819. clGetDeviceIDsFromVA_APIMediaAdapterINTEL =
  820. clGetExtensionFunctionAddressForPlatform(platform_id,
  821. "clGetDeviceIDsFromVA_APIMediaAdapterINTEL");
  822. if (!clGetDeviceIDsFromVA_APIMediaAdapterINTEL) {
  823. av_log(hwdev, AV_LOG_ERROR, "Failed to get address of "
  824. "clGetDeviceIDsFromVA_APIMediaAdapterINTEL().\n");
  825. return AVERROR_UNKNOWN;
  826. }
  827. cle = clGetDeviceIDsFromVA_APIMediaAdapterINTEL(
  828. platform_id, CL_VA_API_DISPLAY_INTEL, va_display,
  829. CL_PREFERRED_DEVICES_FOR_VA_API_INTEL, 0, NULL, nb_devices);
  830. if (cle == CL_DEVICE_NOT_FOUND) {
  831. av_log(hwdev, AV_LOG_DEBUG, "No VAAPI-supporting devices found "
  832. "on platform \"%s\".\n", platform_name);
  833. *nb_devices = 0;
  834. return 0;
  835. } else if (cle != CL_SUCCESS) {
  836. av_log(hwdev, AV_LOG_ERROR, "Failed to get number of devices "
  837. "on platform \"%s\": %d.\n", platform_name, cle);
  838. return AVERROR_UNKNOWN;
  839. }
  840. *devices = av_malloc_array(*nb_devices, sizeof(**devices));
  841. if (!*devices)
  842. return AVERROR(ENOMEM);
  843. cle = clGetDeviceIDsFromVA_APIMediaAdapterINTEL(
  844. platform_id, CL_VA_API_DISPLAY_INTEL, va_display,
  845. CL_PREFERRED_DEVICES_FOR_VA_API_INTEL, *nb_devices, *devices, NULL);
  846. if (cle != CL_SUCCESS) {
  847. av_log(hwdev, AV_LOG_ERROR, "Failed to get list of VAAPI-supporting "
  848. "devices on platform \"%s\": %d.\n", platform_name, cle);
  849. av_freep(devices);
  850. return AVERROR_UNKNOWN;
  851. }
  852. return 0;
  853. }
  854. static int opencl_filter_intel_media_vaapi_device(AVHWDeviceContext *hwdev,
  855. cl_device_id device_id,
  856. const char *device_name,
  857. void *context)
  858. {
  859. const char *va_ext = "cl_intel_va_api_media_sharing";
  860. if (opencl_check_device_extension(device_id, va_ext)) {
  861. return 0;
  862. } else {
  863. av_log(hwdev, AV_LOG_DEBUG, "Device %s does not support the "
  864. "%s extension.\n", device_name, va_ext);
  865. return 1;
  866. }
  867. }
  868. #endif
  869. #if HAVE_OPENCL_DXVA2
  870. static int opencl_filter_dxva2_platform(AVHWDeviceContext *hwdev,
  871. cl_platform_id platform_id,
  872. const char *platform_name,
  873. void *context)
  874. {
  875. const char *dx9_ext = "cl_khr_dx9_media_sharing";
  876. if (opencl_check_platform_extension(platform_id, dx9_ext)) {
  877. return 0;
  878. } else {
  879. av_log(hwdev, AV_LOG_DEBUG, "Platform %s does not support the "
  880. "%s extension.\n", platform_name, dx9_ext);
  881. return 1;
  882. }
  883. }
  884. static int opencl_enumerate_dxva2_devices(AVHWDeviceContext *hwdev,
  885. cl_platform_id platform_id,
  886. const char *platform_name,
  887. cl_uint *nb_devices,
  888. cl_device_id **devices,
  889. void *context)
  890. {
  891. IDirect3DDevice9 *device = context;
  892. clGetDeviceIDsFromDX9MediaAdapterKHR_fn
  893. clGetDeviceIDsFromDX9MediaAdapterKHR;
  894. cl_dx9_media_adapter_type_khr media_adapter_type = CL_ADAPTER_D3D9EX_KHR;
  895. cl_int cle;
  896. clGetDeviceIDsFromDX9MediaAdapterKHR =
  897. clGetExtensionFunctionAddressForPlatform(platform_id,
  898. "clGetDeviceIDsFromDX9MediaAdapterKHR");
  899. if (!clGetDeviceIDsFromDX9MediaAdapterKHR) {
  900. av_log(hwdev, AV_LOG_ERROR, "Failed to get address of "
  901. "clGetDeviceIDsFromDX9MediaAdapterKHR().\n");
  902. return AVERROR_UNKNOWN;
  903. }
  904. cle = clGetDeviceIDsFromDX9MediaAdapterKHR(
  905. platform_id, 1, &media_adapter_type, (void**)&device,
  906. CL_PREFERRED_DEVICES_FOR_DX9_MEDIA_ADAPTER_KHR,
  907. 0, NULL, nb_devices);
  908. if (cle == CL_DEVICE_NOT_FOUND) {
  909. av_log(hwdev, AV_LOG_DEBUG, "No DXVA2-supporting devices found "
  910. "on platform \"%s\".\n", platform_name);
  911. *nb_devices = 0;
  912. return 0;
  913. } else if (cle != CL_SUCCESS) {
  914. av_log(hwdev, AV_LOG_ERROR, "Failed to get number of devices "
  915. "on platform \"%s\": %d.\n", platform_name, cle);
  916. return AVERROR_UNKNOWN;
  917. }
  918. *devices = av_malloc_array(*nb_devices, sizeof(**devices));
  919. if (!*devices)
  920. return AVERROR(ENOMEM);
  921. cle = clGetDeviceIDsFromDX9MediaAdapterKHR(
  922. platform_id, 1, &media_adapter_type, (void**)&device,
  923. CL_PREFERRED_DEVICES_FOR_DX9_MEDIA_ADAPTER_KHR,
  924. *nb_devices, *devices, NULL);
  925. if (cle != CL_SUCCESS) {
  926. av_log(hwdev, AV_LOG_ERROR, "Failed to get list of DXVA2-supporting "
  927. "devices on platform \"%s\": %d.\n", platform_name, cle);
  928. av_freep(devices);
  929. return AVERROR_UNKNOWN;
  930. }
  931. return 0;
  932. }
  933. #endif
  934. #if HAVE_OPENCL_D3D11
  935. static int opencl_filter_d3d11_platform(AVHWDeviceContext *hwdev,
  936. cl_platform_id platform_id,
  937. const char *platform_name,
  938. void *context)
  939. {
  940. const char *d3d11_ext = "cl_khr_d3d11_sharing";
  941. if (opencl_check_platform_extension(platform_id, d3d11_ext)) {
  942. return 0;
  943. } else {
  944. av_log(hwdev, AV_LOG_DEBUG, "Platform %s does not support the "
  945. "%s extension.\n", platform_name, d3d11_ext);
  946. return 1;
  947. }
  948. }
  949. static int opencl_enumerate_d3d11_devices(AVHWDeviceContext *hwdev,
  950. cl_platform_id platform_id,
  951. const char *platform_name,
  952. cl_uint *nb_devices,
  953. cl_device_id **devices,
  954. void *context)
  955. {
  956. ID3D11Device *device = context;
  957. clGetDeviceIDsFromD3D11KHR_fn clGetDeviceIDsFromD3D11KHR;
  958. cl_int cle;
  959. clGetDeviceIDsFromD3D11KHR =
  960. clGetExtensionFunctionAddressForPlatform(platform_id,
  961. "clGetDeviceIDsFromD3D11KHR");
  962. if (!clGetDeviceIDsFromD3D11KHR) {
  963. av_log(hwdev, AV_LOG_ERROR, "Failed to get address of "
  964. "clGetDeviceIDsFromD3D11KHR().\n");
  965. return AVERROR_UNKNOWN;
  966. }
  967. cle = clGetDeviceIDsFromD3D11KHR(platform_id,
  968. CL_D3D11_DEVICE_KHR, device,
  969. CL_PREFERRED_DEVICES_FOR_D3D11_KHR,
  970. 0, NULL, nb_devices);
  971. if (cle == CL_DEVICE_NOT_FOUND) {
  972. av_log(hwdev, AV_LOG_DEBUG, "No D3D11-supporting devices found "
  973. "on platform \"%s\".\n", platform_name);
  974. *nb_devices = 0;
  975. return 0;
  976. } else if (cle != CL_SUCCESS) {
  977. av_log(hwdev, AV_LOG_ERROR, "Failed to get number of devices "
  978. "on platform \"%s\": %d.\n", platform_name, cle);
  979. return AVERROR_UNKNOWN;
  980. }
  981. *devices = av_malloc_array(*nb_devices, sizeof(**devices));
  982. if (!*devices)
  983. return AVERROR(ENOMEM);
  984. cle = clGetDeviceIDsFromD3D11KHR(platform_id,
  985. CL_D3D11_DEVICE_KHR, device,
  986. CL_PREFERRED_DEVICES_FOR_D3D11_KHR,
  987. *nb_devices, *devices, NULL);
  988. if (cle != CL_SUCCESS) {
  989. av_log(hwdev, AV_LOG_ERROR, "Failed to get list of D3D11-supporting "
  990. "devices on platform \"%s\": %d.\n", platform_name, cle);
  991. av_freep(devices);
  992. return AVERROR_UNKNOWN;
  993. }
  994. return 0;
  995. }
  996. #endif
  997. #if HAVE_OPENCL_DXVA2 || HAVE_OPENCL_D3D11
  998. static int opencl_filter_gpu_device(AVHWDeviceContext *hwdev,
  999. cl_device_id device_id,
  1000. const char *device_name,
  1001. void *context)
  1002. {
  1003. cl_device_type device_type;
  1004. cl_int cle;
  1005. cle = clGetDeviceInfo(device_id, CL_DEVICE_TYPE,
  1006. sizeof(device_type), &device_type, NULL);
  1007. if (cle != CL_SUCCESS) {
  1008. av_log(hwdev, AV_LOG_ERROR, "Failed to query device type "
  1009. "of device \"%s\".\n", device_name);
  1010. return AVERROR_UNKNOWN;
  1011. }
  1012. if (!(device_type & CL_DEVICE_TYPE_GPU)) {
  1013. av_log(hwdev, AV_LOG_DEBUG, "Device %s skipped (not GPU).\n",
  1014. device_name);
  1015. return 1;
  1016. }
  1017. return 0;
  1018. }
  1019. #endif
  1020. #if HAVE_OPENCL_DRM_ARM
  1021. static int opencl_filter_drm_arm_platform(AVHWDeviceContext *hwdev,
  1022. cl_platform_id platform_id,
  1023. const char *platform_name,
  1024. void *context)
  1025. {
  1026. const char *drm_arm_ext = "cl_arm_import_memory";
  1027. if (opencl_check_platform_extension(platform_id, drm_arm_ext)) {
  1028. return 0;
  1029. } else {
  1030. av_log(hwdev, AV_LOG_DEBUG, "Platform %s does not support the "
  1031. "%s extension.\n", platform_name, drm_arm_ext);
  1032. return 1;
  1033. }
  1034. }
  1035. static int opencl_filter_drm_arm_device(AVHWDeviceContext *hwdev,
  1036. cl_device_id device_id,
  1037. const char *device_name,
  1038. void *context)
  1039. {
  1040. const char *drm_arm_ext = "cl_arm_import_memory";
  1041. if (opencl_check_device_extension(device_id, drm_arm_ext)) {
  1042. return 0;
  1043. } else {
  1044. av_log(hwdev, AV_LOG_DEBUG, "Device %s does not support the "
  1045. "%s extension.\n", device_name, drm_arm_ext);
  1046. return 1;
  1047. }
  1048. }
  1049. #endif
  1050. static int opencl_device_derive(AVHWDeviceContext *hwdev,
  1051. AVHWDeviceContext *src_ctx,
  1052. int flags)
  1053. {
  1054. int err;
  1055. switch (src_ctx->type) {
  1056. #if HAVE_OPENCL_DRM_BEIGNET
  1057. case AV_HWDEVICE_TYPE_DRM:
  1058. case AV_HWDEVICE_TYPE_VAAPI:
  1059. {
  1060. // Surface mapping works via DRM PRIME fds with no special
  1061. // initialisation required in advance. This just finds the
  1062. // Beignet ICD by name.
  1063. AVDictionary *opts = NULL;
  1064. err = av_dict_set(&opts, "platform_vendor", "Intel", 0);
  1065. if (err >= 0)
  1066. err = av_dict_set(&opts, "platform_version", "beignet", 0);
  1067. if (err >= 0) {
  1068. OpenCLDeviceSelector selector = {
  1069. .platform_index = -1,
  1070. .device_index = 0,
  1071. .context = opts,
  1072. .enumerate_platforms = &opencl_enumerate_platforms,
  1073. .filter_platform = &opencl_filter_platform,
  1074. .enumerate_devices = &opencl_enumerate_devices,
  1075. .filter_device = NULL,
  1076. };
  1077. err = opencl_device_create_internal(hwdev, &selector, NULL);
  1078. }
  1079. av_dict_free(&opts);
  1080. }
  1081. break;
  1082. #endif
  1083. #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
  1084. // The generic code automatically attempts to derive from all
  1085. // ancestors of the given device, so we can ignore QSV devices here
  1086. // and just consider the inner VAAPI device it was derived from.
  1087. case AV_HWDEVICE_TYPE_VAAPI:
  1088. {
  1089. AVVAAPIDeviceContext *src_hwctx = src_ctx->hwctx;
  1090. cl_context_properties props[7] = {
  1091. CL_CONTEXT_PLATFORM,
  1092. 0,
  1093. CL_CONTEXT_VA_API_DISPLAY_INTEL,
  1094. (intptr_t)src_hwctx->display,
  1095. CL_CONTEXT_INTEROP_USER_SYNC,
  1096. CL_FALSE,
  1097. 0,
  1098. };
  1099. OpenCLDeviceSelector selector = {
  1100. .platform_index = -1,
  1101. .device_index = -1,
  1102. .context = src_hwctx->display,
  1103. .enumerate_platforms = &opencl_enumerate_platforms,
  1104. .filter_platform = &opencl_filter_intel_media_vaapi_platform,
  1105. .enumerate_devices = &opencl_enumerate_intel_media_vaapi_devices,
  1106. .filter_device = &opencl_filter_intel_media_vaapi_device,
  1107. };
  1108. err = opencl_device_create_internal(hwdev, &selector, props);
  1109. }
  1110. break;
  1111. #endif
  1112. #if HAVE_OPENCL_DXVA2
  1113. case AV_HWDEVICE_TYPE_DXVA2:
  1114. {
  1115. AVDXVA2DeviceContext *src_hwctx = src_ctx->hwctx;
  1116. IDirect3DDevice9 *device;
  1117. HANDLE device_handle;
  1118. HRESULT hr;
  1119. hr = IDirect3DDeviceManager9_OpenDeviceHandle(src_hwctx->devmgr,
  1120. &device_handle);
  1121. if (FAILED(hr)) {
  1122. av_log(hwdev, AV_LOG_ERROR, "Failed to open device handle "
  1123. "for Direct3D9 device: %lx.\n", (unsigned long)hr);
  1124. err = AVERROR_UNKNOWN;
  1125. break;
  1126. }
  1127. hr = IDirect3DDeviceManager9_LockDevice(src_hwctx->devmgr,
  1128. device_handle,
  1129. &device, FALSE);
  1130. if (SUCCEEDED(hr)) {
  1131. cl_context_properties props[5] = {
  1132. CL_CONTEXT_PLATFORM,
  1133. 0,
  1134. CL_CONTEXT_ADAPTER_D3D9EX_KHR,
  1135. (intptr_t)device,
  1136. 0,
  1137. };
  1138. OpenCLDeviceSelector selector = {
  1139. .platform_index = -1,
  1140. .device_index = -1,
  1141. .context = device,
  1142. .enumerate_platforms = &opencl_enumerate_platforms,
  1143. .filter_platform = &opencl_filter_dxva2_platform,
  1144. .enumerate_devices = &opencl_enumerate_dxva2_devices,
  1145. .filter_device = &opencl_filter_gpu_device,
  1146. };
  1147. err = opencl_device_create_internal(hwdev, &selector, props);
  1148. IDirect3DDeviceManager9_UnlockDevice(src_hwctx->devmgr,
  1149. device_handle, FALSE);
  1150. } else {
  1151. av_log(hwdev, AV_LOG_ERROR, "Failed to lock device handle "
  1152. "for Direct3D9 device: %lx.\n", (unsigned long)hr);
  1153. err = AVERROR_UNKNOWN;
  1154. }
  1155. IDirect3DDeviceManager9_CloseDeviceHandle(src_hwctx->devmgr,
  1156. device_handle);
  1157. }
  1158. break;
  1159. #endif
  1160. #if HAVE_OPENCL_D3D11
  1161. case AV_HWDEVICE_TYPE_D3D11VA:
  1162. {
  1163. AVD3D11VADeviceContext *src_hwctx = src_ctx->hwctx;
  1164. cl_context_properties props[5] = {
  1165. CL_CONTEXT_PLATFORM,
  1166. 0,
  1167. CL_CONTEXT_D3D11_DEVICE_KHR,
  1168. (intptr_t)src_hwctx->device,
  1169. 0,
  1170. };
  1171. OpenCLDeviceSelector selector = {
  1172. .platform_index = -1,
  1173. .device_index = -1,
  1174. .context = src_hwctx->device,
  1175. .enumerate_platforms = &opencl_enumerate_platforms,
  1176. .filter_platform = &opencl_filter_d3d11_platform,
  1177. .enumerate_devices = &opencl_enumerate_d3d11_devices,
  1178. .filter_device = &opencl_filter_gpu_device,
  1179. };
  1180. err = opencl_device_create_internal(hwdev, &selector, props);
  1181. }
  1182. break;
  1183. #endif
  1184. #if HAVE_OPENCL_DRM_ARM
  1185. case AV_HWDEVICE_TYPE_DRM:
  1186. {
  1187. OpenCLDeviceSelector selector = {
  1188. .platform_index = -1,
  1189. .device_index = -1,
  1190. .context = NULL,
  1191. .enumerate_platforms = &opencl_enumerate_platforms,
  1192. .filter_platform = &opencl_filter_drm_arm_platform,
  1193. .enumerate_devices = &opencl_enumerate_devices,
  1194. .filter_device = &opencl_filter_drm_arm_device,
  1195. };
  1196. err = opencl_device_create_internal(hwdev, &selector, NULL);
  1197. }
  1198. break;
  1199. #endif
  1200. default:
  1201. err = AVERROR(ENOSYS);
  1202. break;
  1203. }
  1204. return err;
  1205. }
  1206. static int opencl_get_plane_format(enum AVPixelFormat pixfmt,
  1207. int plane, int width, int height,
  1208. cl_image_format *image_format,
  1209. cl_image_desc *image_desc)
  1210. {
  1211. const AVPixFmtDescriptor *desc;
  1212. const AVComponentDescriptor *comp;
  1213. int channels = 0, order = 0, depth = 0, step = 0;
  1214. int wsub, hsub, alpha;
  1215. int c;
  1216. if (plane >= AV_NUM_DATA_POINTERS)
  1217. return AVERROR(ENOENT);
  1218. desc = av_pix_fmt_desc_get(pixfmt);
  1219. // Only normal images are allowed.
  1220. if (desc->flags & (AV_PIX_FMT_FLAG_BITSTREAM |
  1221. AV_PIX_FMT_FLAG_HWACCEL |
  1222. AV_PIX_FMT_FLAG_PAL))
  1223. return AVERROR(EINVAL);
  1224. wsub = 1 << desc->log2_chroma_w;
  1225. hsub = 1 << desc->log2_chroma_h;
  1226. // Subsampled components must be exact.
  1227. if (width & wsub - 1 || height & hsub - 1)
  1228. return AVERROR(EINVAL);
  1229. for (c = 0; c < desc->nb_components; c++) {
  1230. comp = &desc->comp[c];
  1231. if (comp->plane != plane)
  1232. continue;
  1233. // The step size must be a power of two.
  1234. if (comp->step != 1 && comp->step != 2 &&
  1235. comp->step != 4 && comp->step != 8)
  1236. return AVERROR(EINVAL);
  1237. // The bits in each component must be packed in the
  1238. // most-significant-bits of the relevant bytes.
  1239. if (comp->shift + comp->depth != 8 &&
  1240. comp->shift + comp->depth != 16)
  1241. return AVERROR(EINVAL);
  1242. // The depth must not vary between components.
  1243. if (depth && comp->depth != depth)
  1244. return AVERROR(EINVAL);
  1245. // If a single data element crosses multiple bytes then
  1246. // it must match the native endianness.
  1247. if (comp->depth > 8 &&
  1248. HAVE_BIGENDIAN == !(desc->flags & AV_PIX_FMT_FLAG_BE))
  1249. return AVERROR(EINVAL);
  1250. // A single data element must not contain multiple samples
  1251. // from the same component.
  1252. if (step && comp->step != step)
  1253. return AVERROR(EINVAL);
  1254. depth = comp->depth;
  1255. order = order * 10 + comp->offset / ((depth + 7) / 8) + 1;
  1256. step = comp->step;
  1257. alpha = (desc->flags & AV_PIX_FMT_FLAG_ALPHA &&
  1258. c == desc->nb_components - 1);
  1259. ++channels;
  1260. }
  1261. if (channels == 0)
  1262. return AVERROR(ENOENT);
  1263. memset(image_format, 0, sizeof(*image_format));
  1264. memset(image_desc, 0, sizeof(*image_desc));
  1265. image_desc->image_type = CL_MEM_OBJECT_IMAGE2D;
  1266. if (plane == 0 || alpha) {
  1267. image_desc->image_width = width;
  1268. image_desc->image_height = height;
  1269. image_desc->image_row_pitch = step * width;
  1270. } else {
  1271. image_desc->image_width = width / wsub;
  1272. image_desc->image_height = height / hsub;
  1273. image_desc->image_row_pitch = step * width / wsub;
  1274. }
  1275. if (depth <= 8) {
  1276. image_format->image_channel_data_type = CL_UNORM_INT8;
  1277. } else {
  1278. if (depth <= 16)
  1279. image_format->image_channel_data_type = CL_UNORM_INT16;
  1280. else
  1281. return AVERROR(EINVAL);
  1282. }
  1283. #define CHANNEL_ORDER(order, type) \
  1284. case order: image_format->image_channel_order = type; break;
  1285. switch (order) {
  1286. CHANNEL_ORDER(1, CL_R);
  1287. CHANNEL_ORDER(12, CL_RG);
  1288. CHANNEL_ORDER(1234, CL_RGBA);
  1289. CHANNEL_ORDER(2341, CL_ARGB);
  1290. CHANNEL_ORDER(3214, CL_BGRA);
  1291. #ifdef CL_ABGR
  1292. CHANNEL_ORDER(4321, CL_ABGR);
  1293. #endif
  1294. default:
  1295. return AVERROR(EINVAL);
  1296. }
  1297. #undef CHANNEL_ORDER
  1298. return 0;
  1299. }
  1300. static int opencl_frames_get_constraints(AVHWDeviceContext *hwdev,
  1301. const void *hwconfig,
  1302. AVHWFramesConstraints *constraints)
  1303. {
  1304. AVOpenCLDeviceContext *hwctx = hwdev->hwctx;
  1305. cl_uint nb_image_formats;
  1306. cl_image_format *image_formats = NULL;
  1307. cl_int cle;
  1308. enum AVPixelFormat pix_fmt;
  1309. int err, pix_fmts_found;
  1310. size_t max_width, max_height;
  1311. cle = clGetDeviceInfo(hwctx->device_id, CL_DEVICE_IMAGE2D_MAX_WIDTH,
  1312. sizeof(max_width), &max_width, NULL);
  1313. if (cle != CL_SUCCESS) {
  1314. av_log(hwdev, AV_LOG_ERROR, "Failed to query maximum "
  1315. "supported image width: %d.\n", cle);
  1316. } else {
  1317. constraints->max_width = max_width;
  1318. }
  1319. cle = clGetDeviceInfo(hwctx->device_id, CL_DEVICE_IMAGE2D_MAX_HEIGHT,
  1320. sizeof(max_height), &max_height, NULL);
  1321. if (cle != CL_SUCCESS) {
  1322. av_log(hwdev, AV_LOG_ERROR, "Failed to query maximum "
  1323. "supported image height: %d.\n", cle);
  1324. } else {
  1325. constraints->max_height = max_height;
  1326. }
  1327. av_log(hwdev, AV_LOG_DEBUG, "Maximum supported image size %dx%d.\n",
  1328. constraints->max_width, constraints->max_height);
  1329. cle = clGetSupportedImageFormats(hwctx->context,
  1330. CL_MEM_READ_WRITE,
  1331. CL_MEM_OBJECT_IMAGE2D,
  1332. 0, NULL, &nb_image_formats);
  1333. if (cle != CL_SUCCESS) {
  1334. av_log(hwdev, AV_LOG_ERROR, "Failed to query supported "
  1335. "image formats: %d.\n", cle);
  1336. err = AVERROR(ENOSYS);
  1337. goto fail;
  1338. }
  1339. if (nb_image_formats == 0) {
  1340. av_log(hwdev, AV_LOG_ERROR, "No image support in OpenCL "
  1341. "driver (zero supported image formats).\n");
  1342. err = AVERROR(ENOSYS);
  1343. goto fail;
  1344. }
  1345. image_formats =
  1346. av_malloc_array(nb_image_formats, sizeof(*image_formats));
  1347. if (!image_formats) {
  1348. err = AVERROR(ENOMEM);
  1349. goto fail;
  1350. }
  1351. cle = clGetSupportedImageFormats(hwctx->context,
  1352. CL_MEM_READ_WRITE,
  1353. CL_MEM_OBJECT_IMAGE2D,
  1354. nb_image_formats,
  1355. image_formats, NULL);
  1356. if (cle != CL_SUCCESS) {
  1357. av_log(hwdev, AV_LOG_ERROR, "Failed to query supported "
  1358. "image formats: %d.\n", cle);
  1359. err = AVERROR(ENOSYS);
  1360. goto fail;
  1361. }
  1362. pix_fmts_found = 0;
  1363. for (pix_fmt = 0; pix_fmt < AV_PIX_FMT_NB; pix_fmt++) {
  1364. cl_image_format image_format;
  1365. cl_image_desc image_desc;
  1366. int plane, i;
  1367. for (plane = 0;; plane++) {
  1368. err = opencl_get_plane_format(pix_fmt, plane, 0, 0,
  1369. &image_format,
  1370. &image_desc);
  1371. if (err < 0)
  1372. break;
  1373. for (i = 0; i < nb_image_formats; i++) {
  1374. if (image_formats[i].image_channel_order ==
  1375. image_format.image_channel_order &&
  1376. image_formats[i].image_channel_data_type ==
  1377. image_format.image_channel_data_type)
  1378. break;
  1379. }
  1380. if (i == nb_image_formats) {
  1381. err = AVERROR(EINVAL);
  1382. break;
  1383. }
  1384. }
  1385. if (err != AVERROR(ENOENT))
  1386. continue;
  1387. av_log(hwdev, AV_LOG_DEBUG, "Format %s supported.\n",
  1388. av_get_pix_fmt_name(pix_fmt));
  1389. err = av_reallocp_array(&constraints->valid_sw_formats,
  1390. pix_fmts_found + 2,
  1391. sizeof(*constraints->valid_sw_formats));
  1392. if (err < 0)
  1393. goto fail;
  1394. constraints->valid_sw_formats[pix_fmts_found] = pix_fmt;
  1395. constraints->valid_sw_formats[pix_fmts_found + 1] =
  1396. AV_PIX_FMT_NONE;
  1397. ++pix_fmts_found;
  1398. }
  1399. av_freep(&image_formats);
  1400. constraints->valid_hw_formats =
  1401. av_malloc_array(2, sizeof(*constraints->valid_hw_formats));
  1402. if (!constraints->valid_hw_formats) {
  1403. err = AVERROR(ENOMEM);
  1404. goto fail;
  1405. }
  1406. constraints->valid_hw_formats[0] = AV_PIX_FMT_OPENCL;
  1407. constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
  1408. return 0;
  1409. fail:
  1410. av_freep(&image_formats);
  1411. return err;
  1412. }
  1413. static void opencl_pool_free(void *opaque, uint8_t *data)
  1414. {
  1415. AVHWFramesContext *hwfc = opaque;
  1416. AVOpenCLFrameDescriptor *desc = (AVOpenCLFrameDescriptor*)data;
  1417. cl_int cle;
  1418. int p;
  1419. for (p = 0; p < desc->nb_planes; p++) {
  1420. cle = clReleaseMemObject(desc->planes[p]);
  1421. if (cle != CL_SUCCESS) {
  1422. av_log(hwfc, AV_LOG_ERROR, "Failed to release plane %d: "
  1423. "%d.\n", p, cle);
  1424. }
  1425. }
  1426. av_free(desc);
  1427. }
  1428. static AVBufferRef *opencl_pool_alloc(void *opaque, int size)
  1429. {
  1430. AVHWFramesContext *hwfc = opaque;
  1431. AVOpenCLDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  1432. AVOpenCLFrameDescriptor *desc;
  1433. cl_int cle;
  1434. cl_mem image;
  1435. cl_image_format image_format;
  1436. cl_image_desc image_desc;
  1437. int err, p;
  1438. AVBufferRef *ref;
  1439. desc = av_mallocz(sizeof(*desc));
  1440. if (!desc)
  1441. return NULL;
  1442. for (p = 0;; p++) {
  1443. err = opencl_get_plane_format(hwfc->sw_format, p,
  1444. hwfc->width, hwfc->height,
  1445. &image_format, &image_desc);
  1446. if (err == AVERROR(ENOENT))
  1447. break;
  1448. if (err < 0)
  1449. goto fail;
  1450. // For generic image objects, the pitch is determined by the
  1451. // implementation.
  1452. image_desc.image_row_pitch = 0;
  1453. image = clCreateImage(hwctx->context, CL_MEM_READ_WRITE,
  1454. &image_format, &image_desc, NULL, &cle);
  1455. if (!image) {
  1456. av_log(hwfc, AV_LOG_ERROR, "Failed to create image for "
  1457. "plane %d: %d.\n", p, cle);
  1458. goto fail;
  1459. }
  1460. desc->planes[p] = image;
  1461. }
  1462. desc->nb_planes = p;
  1463. ref = av_buffer_create((uint8_t*)desc, sizeof(*desc),
  1464. &opencl_pool_free, hwfc, 0);
  1465. if (!ref)
  1466. goto fail;
  1467. return ref;
  1468. fail:
  1469. for (p = 0; desc->planes[p]; p++)
  1470. clReleaseMemObject(desc->planes[p]);
  1471. av_free(desc);
  1472. return NULL;
  1473. }
  1474. static int opencl_frames_init_command_queue(AVHWFramesContext *hwfc)
  1475. {
  1476. AVOpenCLFramesContext *hwctx = hwfc->hwctx;
  1477. OpenCLDeviceContext *devpriv = hwfc->device_ctx->internal->priv;
  1478. OpenCLFramesContext *priv = hwfc->internal->priv;
  1479. cl_int cle;
  1480. priv->command_queue = hwctx->command_queue ? hwctx->command_queue
  1481. : devpriv->command_queue;
  1482. cle = clRetainCommandQueue(priv->command_queue);
  1483. if (cle != CL_SUCCESS) {
  1484. av_log(hwfc, AV_LOG_ERROR, "Failed to retain frame "
  1485. "command queue: %d.\n", cle);
  1486. return AVERROR(EIO);
  1487. }
  1488. return 0;
  1489. }
  1490. static int opencl_frames_init(AVHWFramesContext *hwfc)
  1491. {
  1492. if (!hwfc->pool) {
  1493. hwfc->internal->pool_internal =
  1494. av_buffer_pool_init2(sizeof(cl_mem), hwfc,
  1495. &opencl_pool_alloc, NULL);
  1496. if (!hwfc->internal->pool_internal)
  1497. return AVERROR(ENOMEM);
  1498. }
  1499. return opencl_frames_init_command_queue(hwfc);
  1500. }
  1501. static void opencl_frames_uninit(AVHWFramesContext *hwfc)
  1502. {
  1503. OpenCLFramesContext *priv = hwfc->internal->priv;
  1504. cl_int cle;
  1505. #if HAVE_OPENCL_DXVA2 || HAVE_OPENCL_D3D11
  1506. int i, p;
  1507. for (i = 0; i < priv->nb_mapped_frames; i++) {
  1508. AVOpenCLFrameDescriptor *desc = &priv->mapped_frames[i];
  1509. for (p = 0; p < desc->nb_planes; p++) {
  1510. cle = clReleaseMemObject(desc->planes[p]);
  1511. if (cle != CL_SUCCESS) {
  1512. av_log(hwfc, AV_LOG_ERROR, "Failed to release mapped "
  1513. "frame object (frame %d plane %d): %d.\n",
  1514. i, p, cle);
  1515. }
  1516. }
  1517. }
  1518. av_freep(&priv->mapped_frames);
  1519. #endif
  1520. if (priv->command_queue) {
  1521. cle = clReleaseCommandQueue(priv->command_queue);
  1522. if (cle != CL_SUCCESS) {
  1523. av_log(hwfc, AV_LOG_ERROR, "Failed to release frame "
  1524. "command queue: %d.\n", cle);
  1525. }
  1526. priv->command_queue = NULL;
  1527. }
  1528. }
  1529. static int opencl_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
  1530. {
  1531. AVOpenCLFrameDescriptor *desc;
  1532. int p;
  1533. frame->buf[0] = av_buffer_pool_get(hwfc->pool);
  1534. if (!frame->buf[0])
  1535. return AVERROR(ENOMEM);
  1536. desc = (AVOpenCLFrameDescriptor*)frame->buf[0]->data;
  1537. for (p = 0; p < desc->nb_planes; p++)
  1538. frame->data[p] = (uint8_t*)desc->planes[p];
  1539. frame->format = AV_PIX_FMT_OPENCL;
  1540. frame->width = hwfc->width;
  1541. frame->height = hwfc->height;
  1542. return 0;
  1543. }
  1544. static int opencl_transfer_get_formats(AVHWFramesContext *hwfc,
  1545. enum AVHWFrameTransferDirection dir,
  1546. enum AVPixelFormat **formats)
  1547. {
  1548. enum AVPixelFormat *fmts;
  1549. fmts = av_malloc_array(2, sizeof(*fmts));
  1550. if (!fmts)
  1551. return AVERROR(ENOMEM);
  1552. fmts[0] = hwfc->sw_format;
  1553. fmts[1] = AV_PIX_FMT_NONE;
  1554. *formats = fmts;
  1555. return 0;
  1556. }
  1557. static int opencl_wait_events(AVHWFramesContext *hwfc,
  1558. cl_event *events, int nb_events)
  1559. {
  1560. cl_int cle;
  1561. int i;
  1562. cle = clWaitForEvents(nb_events, events);
  1563. if (cle != CL_SUCCESS) {
  1564. av_log(hwfc, AV_LOG_ERROR, "Failed to wait for event "
  1565. "completion: %d.\n", cle);
  1566. return AVERROR(EIO);
  1567. }
  1568. for (i = 0; i < nb_events; i++) {
  1569. cle = clReleaseEvent(events[i]);
  1570. if (cle != CL_SUCCESS) {
  1571. av_log(hwfc, AV_LOG_ERROR, "Failed to release "
  1572. "event: %d.\n", cle);
  1573. }
  1574. }
  1575. return 0;
  1576. }
  1577. static int opencl_transfer_data_from(AVHWFramesContext *hwfc,
  1578. AVFrame *dst, const AVFrame *src)
  1579. {
  1580. OpenCLFramesContext *priv = hwfc->internal->priv;
  1581. cl_image_format image_format;
  1582. cl_image_desc image_desc;
  1583. cl_int cle;
  1584. size_t origin[3] = { 0, 0, 0 };
  1585. size_t region[3];
  1586. cl_event events[AV_NUM_DATA_POINTERS];
  1587. int err, p;
  1588. if (dst->format != hwfc->sw_format)
  1589. return AVERROR(EINVAL);
  1590. for (p = 0;; p++) {
  1591. err = opencl_get_plane_format(hwfc->sw_format, p,
  1592. src->width, src->height,
  1593. &image_format, &image_desc);
  1594. if (err < 0) {
  1595. if (err == AVERROR(ENOENT))
  1596. err = 0;
  1597. break;
  1598. }
  1599. if (!dst->data[p]) {
  1600. av_log(hwfc, AV_LOG_ERROR, "Plane %d missing on "
  1601. "destination frame for transfer.\n", p);
  1602. err = AVERROR(EINVAL);
  1603. break;
  1604. }
  1605. region[0] = image_desc.image_width;
  1606. region[1] = image_desc.image_height;
  1607. region[2] = 1;
  1608. cle = clEnqueueReadImage(priv->command_queue,
  1609. (cl_mem)src->data[p],
  1610. CL_FALSE, origin, region,
  1611. dst->linesize[p], 0,
  1612. dst->data[p],
  1613. 0, NULL, &events[p]);
  1614. if (cle != CL_SUCCESS) {
  1615. av_log(hwfc, AV_LOG_ERROR, "Failed to enqueue read of "
  1616. "OpenCL image plane %d: %d.\n", p, cle);
  1617. err = AVERROR(EIO);
  1618. break;
  1619. }
  1620. }
  1621. opencl_wait_events(hwfc, events, p);
  1622. return err;
  1623. }
  1624. static int opencl_transfer_data_to(AVHWFramesContext *hwfc,
  1625. AVFrame *dst, const AVFrame *src)
  1626. {
  1627. OpenCLFramesContext *priv = hwfc->internal->priv;
  1628. cl_image_format image_format;
  1629. cl_image_desc image_desc;
  1630. cl_int cle;
  1631. size_t origin[3] = { 0, 0, 0 };
  1632. size_t region[3];
  1633. cl_event events[AV_NUM_DATA_POINTERS];
  1634. int err, p;
  1635. if (src->format != hwfc->sw_format)
  1636. return AVERROR(EINVAL);
  1637. for (p = 0;; p++) {
  1638. err = opencl_get_plane_format(hwfc->sw_format, p,
  1639. src->width, src->height,
  1640. &image_format, &image_desc);
  1641. if (err < 0) {
  1642. if (err == AVERROR(ENOENT))
  1643. err = 0;
  1644. break;
  1645. }
  1646. if (!src->data[p]) {
  1647. av_log(hwfc, AV_LOG_ERROR, "Plane %d missing on "
  1648. "source frame for transfer.\n", p);
  1649. err = AVERROR(EINVAL);
  1650. break;
  1651. }
  1652. region[0] = image_desc.image_width;
  1653. region[1] = image_desc.image_height;
  1654. region[2] = 1;
  1655. cle = clEnqueueWriteImage(priv->command_queue,
  1656. (cl_mem)dst->data[p],
  1657. CL_FALSE, origin, region,
  1658. src->linesize[p], 0,
  1659. src->data[p],
  1660. 0, NULL, &events[p]);
  1661. if (cle != CL_SUCCESS) {
  1662. av_log(hwfc, AV_LOG_ERROR, "Failed to enqueue write of "
  1663. "OpenCL image plane %d: %d.\n", p, cle);
  1664. err = AVERROR(EIO);
  1665. break;
  1666. }
  1667. }
  1668. opencl_wait_events(hwfc, events, p);
  1669. return err;
  1670. }
  1671. typedef struct OpenCLMapping {
  1672. // The mapped addresses for each plane.
  1673. // The destination frame is not available when we unmap, so these
  1674. // need to be stored separately.
  1675. void *address[AV_NUM_DATA_POINTERS];
  1676. } OpenCLMapping;
  1677. static void opencl_unmap_frame(AVHWFramesContext *hwfc,
  1678. HWMapDescriptor *hwmap)
  1679. {
  1680. OpenCLFramesContext *priv = hwfc->internal->priv;
  1681. OpenCLMapping *map = hwmap->priv;
  1682. cl_event events[AV_NUM_DATA_POINTERS];
  1683. int p, e;
  1684. cl_int cle;
  1685. for (p = e = 0; p < FF_ARRAY_ELEMS(map->address); p++) {
  1686. if (!map->address[p])
  1687. break;
  1688. cle = clEnqueueUnmapMemObject(priv->command_queue,
  1689. (cl_mem)hwmap->source->data[p],
  1690. map->address[p],
  1691. 0, NULL, &events[e]);
  1692. if (cle != CL_SUCCESS) {
  1693. av_log(hwfc, AV_LOG_ERROR, "Failed to unmap OpenCL "
  1694. "image plane %d: %d.\n", p, cle);
  1695. }
  1696. ++e;
  1697. }
  1698. opencl_wait_events(hwfc, events, e);
  1699. av_free(map);
  1700. }
  1701. static int opencl_map_frame(AVHWFramesContext *hwfc, AVFrame *dst,
  1702. const AVFrame *src, int flags)
  1703. {
  1704. OpenCLFramesContext *priv = hwfc->internal->priv;
  1705. cl_map_flags map_flags;
  1706. cl_image_format image_format;
  1707. cl_image_desc image_desc;
  1708. cl_int cle;
  1709. OpenCLMapping *map;
  1710. size_t origin[3] = { 0, 0, 0 };
  1711. size_t region[3];
  1712. size_t row_pitch;
  1713. cl_event events[AV_NUM_DATA_POINTERS];
  1714. int err, p;
  1715. av_assert0(hwfc->sw_format == dst->format);
  1716. if (flags & AV_HWFRAME_MAP_OVERWRITE &&
  1717. !(flags & AV_HWFRAME_MAP_READ)) {
  1718. // This is mutually exclusive with the read/write flags, so
  1719. // there is no way to map with read here.
  1720. map_flags = CL_MAP_WRITE_INVALIDATE_REGION;
  1721. } else {
  1722. map_flags = 0;
  1723. if (flags & AV_HWFRAME_MAP_READ)
  1724. map_flags |= CL_MAP_READ;
  1725. if (flags & AV_HWFRAME_MAP_WRITE)
  1726. map_flags |= CL_MAP_WRITE;
  1727. }
  1728. map = av_mallocz(sizeof(*map));
  1729. if (!map)
  1730. return AVERROR(ENOMEM);
  1731. for (p = 0;; p++) {
  1732. err = opencl_get_plane_format(hwfc->sw_format, p,
  1733. src->width, src->height,
  1734. &image_format, &image_desc);
  1735. if (err == AVERROR(ENOENT))
  1736. break;
  1737. if (err < 0)
  1738. goto fail;
  1739. region[0] = image_desc.image_width;
  1740. region[1] = image_desc.image_height;
  1741. region[2] = 1;
  1742. map->address[p] =
  1743. clEnqueueMapImage(priv->command_queue,
  1744. (cl_mem)src->data[p],
  1745. CL_FALSE, map_flags, origin, region,
  1746. &row_pitch, NULL, 0, NULL,
  1747. &events[p], &cle);
  1748. if (!map->address[p]) {
  1749. av_log(hwfc, AV_LOG_ERROR, "Failed to map OpenCL "
  1750. "image plane %d: %d.\n", p, cle);
  1751. err = AVERROR(EIO);
  1752. goto fail;
  1753. }
  1754. dst->data[p] = map->address[p];
  1755. av_log(hwfc, AV_LOG_DEBUG, "Map plane %d (%p -> %p).\n",
  1756. p, src->data[p], dst->data[p]);
  1757. }
  1758. err = opencl_wait_events(hwfc, events, p);
  1759. if (err < 0)
  1760. goto fail;
  1761. err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
  1762. &opencl_unmap_frame, map);
  1763. if (err < 0)
  1764. goto fail;
  1765. dst->width = src->width;
  1766. dst->height = src->height;
  1767. return 0;
  1768. fail:
  1769. for (p = 0; p < AV_NUM_DATA_POINTERS; p++) {
  1770. if (!map->address[p])
  1771. break;
  1772. clEnqueueUnmapMemObject(priv->command_queue,
  1773. (cl_mem)src->data[p],
  1774. map->address[p],
  1775. 0, NULL, &events[p]);
  1776. }
  1777. if (p > 0)
  1778. opencl_wait_events(hwfc, events, p);
  1779. av_freep(&map);
  1780. return err;
  1781. }
  1782. #if HAVE_OPENCL_DRM_BEIGNET
  1783. typedef struct DRMBeignetToOpenCLMapping {
  1784. AVFrame *drm_frame;
  1785. AVDRMFrameDescriptor *drm_desc;
  1786. AVOpenCLFrameDescriptor frame;
  1787. } DRMBeignetToOpenCLMapping;
  1788. static void opencl_unmap_from_drm_beignet(AVHWFramesContext *dst_fc,
  1789. HWMapDescriptor *hwmap)
  1790. {
  1791. DRMBeignetToOpenCLMapping *mapping = hwmap->priv;
  1792. cl_int cle;
  1793. int i;
  1794. for (i = 0; i < mapping->frame.nb_planes; i++) {
  1795. cle = clReleaseMemObject(mapping->frame.planes[i]);
  1796. if (cle != CL_SUCCESS) {
  1797. av_log(dst_fc, AV_LOG_ERROR, "Failed to release CL image "
  1798. "of plane %d of DRM frame: %d.\n", i, cle);
  1799. }
  1800. }
  1801. av_free(mapping);
  1802. }
  1803. static int opencl_map_from_drm_beignet(AVHWFramesContext *dst_fc,
  1804. AVFrame *dst, const AVFrame *src,
  1805. int flags)
  1806. {
  1807. AVOpenCLDeviceContext *hwctx = dst_fc->device_ctx->hwctx;
  1808. OpenCLDeviceContext *priv = dst_fc->device_ctx->internal->priv;
  1809. DRMBeignetToOpenCLMapping *mapping;
  1810. const AVDRMFrameDescriptor *desc;
  1811. cl_int cle;
  1812. int err, i, j, p;
  1813. desc = (const AVDRMFrameDescriptor*)src->data[0];
  1814. mapping = av_mallocz(sizeof(*mapping));
  1815. if (!mapping)
  1816. return AVERROR(ENOMEM);
  1817. p = 0;
  1818. for (i = 0; i < desc->nb_layers; i++) {
  1819. const AVDRMLayerDescriptor *layer = &desc->layers[i];
  1820. for (j = 0; j < layer->nb_planes; j++) {
  1821. const AVDRMPlaneDescriptor *plane = &layer->planes[j];
  1822. const AVDRMObjectDescriptor *object =
  1823. &desc->objects[plane->object_index];
  1824. cl_import_image_info_intel image_info = {
  1825. .fd = object->fd,
  1826. .size = object->size,
  1827. .type = CL_MEM_OBJECT_IMAGE2D,
  1828. .offset = plane->offset,
  1829. .row_pitch = plane->pitch,
  1830. };
  1831. cl_image_desc image_desc;
  1832. err = opencl_get_plane_format(dst_fc->sw_format, p,
  1833. src->width, src->height,
  1834. &image_info.fmt,
  1835. &image_desc);
  1836. if (err < 0) {
  1837. av_log(dst_fc, AV_LOG_ERROR, "DRM frame layer %d "
  1838. "plane %d is not representable in OpenCL: %d.\n",
  1839. i, j, err);
  1840. goto fail;
  1841. }
  1842. image_info.width = image_desc.image_width;
  1843. image_info.height = image_desc.image_height;
  1844. mapping->frame.planes[p] =
  1845. priv->clCreateImageFromFdINTEL(hwctx->context,
  1846. &image_info, &cle);
  1847. if (!mapping->frame.planes[p]) {
  1848. av_log(dst_fc, AV_LOG_ERROR, "Failed to create CL image "
  1849. "from layer %d plane %d of DRM frame: %d.\n",
  1850. i, j, cle);
  1851. err = AVERROR(EIO);
  1852. goto fail;
  1853. }
  1854. dst->data[p] = (uint8_t*)mapping->frame.planes[p];
  1855. mapping->frame.nb_planes = ++p;
  1856. }
  1857. }
  1858. err = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src,
  1859. &opencl_unmap_from_drm_beignet,
  1860. mapping);
  1861. if (err < 0)
  1862. goto fail;
  1863. dst->width = src->width;
  1864. dst->height = src->height;
  1865. return 0;
  1866. fail:
  1867. for (p = 0; p < mapping->frame.nb_planes; p++) {
  1868. if (mapping->frame.planes[p])
  1869. clReleaseMemObject(mapping->frame.planes[p]);
  1870. }
  1871. av_free(mapping);
  1872. return err;
  1873. }
  1874. #if HAVE_OPENCL_VAAPI_BEIGNET
  1875. static int opencl_map_from_vaapi(AVHWFramesContext *dst_fc,
  1876. AVFrame *dst, const AVFrame *src,
  1877. int flags)
  1878. {
  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. err = ff_hwframe_map_replace(dst, src);
  1892. fail:
  1893. av_frame_free(&tmp);
  1894. return err;
  1895. }
  1896. #endif /* HAVE_OPENCL_VAAPI_BEIGNET */
  1897. #endif /* HAVE_OPENCL_DRM_BEIGNET */
  1898. static inline cl_mem_flags opencl_mem_flags_for_mapping(int map_flags)
  1899. {
  1900. if ((map_flags & AV_HWFRAME_MAP_READ) &&
  1901. (map_flags & AV_HWFRAME_MAP_WRITE))
  1902. return CL_MEM_READ_WRITE;
  1903. else if (map_flags & AV_HWFRAME_MAP_READ)
  1904. return CL_MEM_READ_ONLY;
  1905. else if (map_flags & AV_HWFRAME_MAP_WRITE)
  1906. return CL_MEM_WRITE_ONLY;
  1907. else
  1908. return 0;
  1909. }
  1910. #if HAVE_OPENCL_VAAPI_INTEL_MEDIA
  1911. static void opencl_unmap_from_qsv(AVHWFramesContext *dst_fc,
  1912. HWMapDescriptor *hwmap)
  1913. {
  1914. AVOpenCLFrameDescriptor *desc = hwmap->priv;
  1915. OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
  1916. OpenCLFramesContext *frames_priv = dst_fc->internal->priv;
  1917. cl_event event;
  1918. cl_int cle;
  1919. int p;
  1920. av_log(dst_fc, AV_LOG_DEBUG, "Unmap QSV/VAAPI surface from OpenCL.\n");
  1921. cle = device_priv->clEnqueueReleaseVA_APIMediaSurfacesINTEL(
  1922. frames_priv->command_queue, desc->nb_planes, desc->planes,
  1923. 0, NULL, &event);
  1924. if (cle != CL_SUCCESS) {
  1925. av_log(dst_fc, AV_LOG_ERROR, "Failed to release surface "
  1926. "handles: %d.\n", cle);
  1927. }
  1928. opencl_wait_events(dst_fc, &event, 1);
  1929. for (p = 0; p < desc->nb_planes; p++) {
  1930. cle = clReleaseMemObject(desc->planes[p]);
  1931. if (cle != CL_SUCCESS) {
  1932. av_log(dst_fc, AV_LOG_ERROR, "Failed to release CL "
  1933. "image of plane %d of QSV/VAAPI surface: %d\n",
  1934. p, cle);
  1935. }
  1936. }
  1937. av_free(desc);
  1938. }
  1939. static int opencl_map_from_qsv(AVHWFramesContext *dst_fc, AVFrame *dst,
  1940. const AVFrame *src, int flags)
  1941. {
  1942. AVHWFramesContext *src_fc =
  1943. (AVHWFramesContext*)src->hw_frames_ctx->data;
  1944. AVOpenCLDeviceContext *dst_dev = dst_fc->device_ctx->hwctx;
  1945. OpenCLDeviceContext *device_priv = dst_fc->device_ctx->internal->priv;
  1946. OpenCLFramesContext *frames_priv = dst_fc->internal->priv;
  1947. AVOpenCLFrameDescriptor *desc;
  1948. VASurfaceID va_surface;
  1949. cl_mem_flags cl_flags;
  1950. cl_event event;
  1951. cl_int cle;
  1952. int err, p;
  1953. #if CONFIG_LIBMFX
  1954. if (src->format == AV_PIX_FMT_QSV) {
  1955. mfxFrameSurface1 *mfx_surface = (mfxFrameSurface1*)src->data[3];
  1956. va_surface = *(VASurfaceID*)mfx_surface->Data.MemId;
  1957. } else
  1958. #endif
  1959. 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. av_unused 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. av_unused 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. };