2
0

hwcontext_opencl.c 95 KB

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