hwcontext_opencl.c 99 KB

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