hwcontext_vaapi.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689
  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. #include "config.h"
  19. #if HAVE_VAAPI_X11
  20. # include <va/va_x11.h>
  21. #endif
  22. #if HAVE_VAAPI_DRM
  23. # include <va/va_drm.h>
  24. #endif
  25. #if CONFIG_LIBDRM
  26. # include <va/va_drmcommon.h>
  27. # include <xf86drm.h>
  28. # include <drm_fourcc.h>
  29. # ifndef DRM_FORMAT_MOD_INVALID
  30. # define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
  31. # endif
  32. #endif
  33. #include <fcntl.h>
  34. #if HAVE_UNISTD_H
  35. # include <unistd.h>
  36. #endif
  37. #include "avassert.h"
  38. #include "buffer.h"
  39. #include "common.h"
  40. #include "hwcontext.h"
  41. #include "hwcontext_drm.h"
  42. #include "hwcontext_internal.h"
  43. #include "hwcontext_vaapi.h"
  44. #include "mem.h"
  45. #include "pixdesc.h"
  46. #include "pixfmt.h"
  47. typedef struct VAAPIDevicePriv {
  48. #if HAVE_VAAPI_X11
  49. Display *x11_display;
  50. #endif
  51. int drm_fd;
  52. } VAAPIDevicePriv;
  53. typedef struct VAAPISurfaceFormat {
  54. enum AVPixelFormat pix_fmt;
  55. VAImageFormat image_format;
  56. } VAAPISurfaceFormat;
  57. typedef struct VAAPIDeviceContext {
  58. // Surface formats which can be used with this device.
  59. VAAPISurfaceFormat *formats;
  60. int nb_formats;
  61. } VAAPIDeviceContext;
  62. typedef struct VAAPIFramesContext {
  63. // Surface attributes set at create time.
  64. VASurfaceAttrib *attributes;
  65. int nb_attributes;
  66. // RT format of the underlying surface (Intel driver ignores this anyway).
  67. unsigned int rt_format;
  68. // Whether vaDeriveImage works.
  69. int derive_works;
  70. } VAAPIFramesContext;
  71. typedef struct VAAPIMapping {
  72. // Handle to the derived or copied image which is mapped.
  73. VAImage image;
  74. // The mapping flags actually used.
  75. int flags;
  76. } VAAPIMapping;
  77. typedef struct VAAPIFormat {
  78. unsigned int fourcc;
  79. unsigned int rt_format;
  80. enum AVPixelFormat pix_fmt;
  81. int chroma_planes_swapped;
  82. } VAAPIFormatDescriptor;
  83. #define MAP(va, rt, av, swap_uv) { \
  84. VA_FOURCC_ ## va, \
  85. VA_RT_FORMAT_ ## rt, \
  86. AV_PIX_FMT_ ## av, \
  87. swap_uv, \
  88. }
  89. // The map fourcc <-> pix_fmt isn't bijective because of the annoying U/V
  90. // plane swap cases. The frame handling below tries to hide these.
  91. static const VAAPIFormatDescriptor vaapi_format_map[] = {
  92. MAP(NV12, YUV420, NV12, 0),
  93. #ifdef VA_FOURCC_I420
  94. MAP(I420, YUV420, YUV420P, 0),
  95. #endif
  96. MAP(YV12, YUV420, YUV420P, 1),
  97. MAP(IYUV, YUV420, YUV420P, 0),
  98. MAP(422H, YUV422, YUV422P, 0),
  99. #ifdef VA_FOURCC_YV16
  100. MAP(YV16, YUV422, YUV422P, 1),
  101. #endif
  102. MAP(UYVY, YUV422, UYVY422, 0),
  103. MAP(YUY2, YUV422, YUYV422, 0),
  104. MAP(411P, YUV411, YUV411P, 0),
  105. MAP(422V, YUV422, YUV440P, 0),
  106. MAP(444P, YUV444, YUV444P, 0),
  107. MAP(Y800, YUV400, GRAY8, 0),
  108. #ifdef VA_FOURCC_P010
  109. MAP(P010, YUV420_10BPP, P010, 0),
  110. #endif
  111. MAP(BGRA, RGB32, BGRA, 0),
  112. MAP(BGRX, RGB32, BGR0, 0),
  113. MAP(RGBA, RGB32, RGBA, 0),
  114. MAP(RGBX, RGB32, RGB0, 0),
  115. #ifdef VA_FOURCC_ABGR
  116. MAP(ABGR, RGB32, ABGR, 0),
  117. MAP(XBGR, RGB32, 0BGR, 0),
  118. #endif
  119. MAP(ARGB, RGB32, ARGB, 0),
  120. MAP(XRGB, RGB32, 0RGB, 0),
  121. };
  122. #undef MAP
  123. static const VAAPIFormatDescriptor *
  124. vaapi_format_from_fourcc(unsigned int fourcc)
  125. {
  126. int i;
  127. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++)
  128. if (vaapi_format_map[i].fourcc == fourcc)
  129. return &vaapi_format_map[i];
  130. return NULL;
  131. }
  132. static const VAAPIFormatDescriptor *
  133. vaapi_format_from_pix_fmt(enum AVPixelFormat pix_fmt)
  134. {
  135. int i;
  136. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++)
  137. if (vaapi_format_map[i].pix_fmt == pix_fmt)
  138. return &vaapi_format_map[i];
  139. return NULL;
  140. }
  141. static enum AVPixelFormat vaapi_pix_fmt_from_fourcc(unsigned int fourcc)
  142. {
  143. const VAAPIFormatDescriptor *desc;
  144. desc = vaapi_format_from_fourcc(fourcc);
  145. if (desc)
  146. return desc->pix_fmt;
  147. else
  148. return AV_PIX_FMT_NONE;
  149. }
  150. static int vaapi_get_image_format(AVHWDeviceContext *hwdev,
  151. enum AVPixelFormat pix_fmt,
  152. VAImageFormat **image_format)
  153. {
  154. VAAPIDeviceContext *ctx = hwdev->internal->priv;
  155. int i;
  156. for (i = 0; i < ctx->nb_formats; i++) {
  157. if (ctx->formats[i].pix_fmt == pix_fmt) {
  158. if (image_format)
  159. *image_format = &ctx->formats[i].image_format;
  160. return 0;
  161. }
  162. }
  163. return AVERROR(EINVAL);
  164. }
  165. static int vaapi_frames_get_constraints(AVHWDeviceContext *hwdev,
  166. const void *hwconfig,
  167. AVHWFramesConstraints *constraints)
  168. {
  169. AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
  170. const AVVAAPIHWConfig *config = hwconfig;
  171. VAAPIDeviceContext *ctx = hwdev->internal->priv;
  172. VASurfaceAttrib *attr_list = NULL;
  173. VAStatus vas;
  174. enum AVPixelFormat pix_fmt;
  175. unsigned int fourcc;
  176. int err, i, j, attr_count, pix_fmt_count;
  177. if (config &&
  178. !(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_SURFACE_ATTRIBUTES)) {
  179. attr_count = 0;
  180. vas = vaQuerySurfaceAttributes(hwctx->display, config->config_id,
  181. 0, &attr_count);
  182. if (vas != VA_STATUS_SUCCESS) {
  183. av_log(hwdev, AV_LOG_ERROR, "Failed to query surface attributes: "
  184. "%d (%s).\n", vas, vaErrorStr(vas));
  185. err = AVERROR(ENOSYS);
  186. goto fail;
  187. }
  188. attr_list = av_malloc(attr_count * sizeof(*attr_list));
  189. if (!attr_list) {
  190. err = AVERROR(ENOMEM);
  191. goto fail;
  192. }
  193. vas = vaQuerySurfaceAttributes(hwctx->display, config->config_id,
  194. attr_list, &attr_count);
  195. if (vas != VA_STATUS_SUCCESS) {
  196. av_log(hwdev, AV_LOG_ERROR, "Failed to query surface attributes: "
  197. "%d (%s).\n", vas, vaErrorStr(vas));
  198. err = AVERROR(ENOSYS);
  199. goto fail;
  200. }
  201. pix_fmt_count = 0;
  202. for (i = 0; i < attr_count; i++) {
  203. switch (attr_list[i].type) {
  204. case VASurfaceAttribPixelFormat:
  205. fourcc = attr_list[i].value.value.i;
  206. pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
  207. if (pix_fmt != AV_PIX_FMT_NONE) {
  208. ++pix_fmt_count;
  209. } else {
  210. // Something unsupported - ignore.
  211. }
  212. break;
  213. case VASurfaceAttribMinWidth:
  214. constraints->min_width = attr_list[i].value.value.i;
  215. break;
  216. case VASurfaceAttribMinHeight:
  217. constraints->min_height = attr_list[i].value.value.i;
  218. break;
  219. case VASurfaceAttribMaxWidth:
  220. constraints->max_width = attr_list[i].value.value.i;
  221. break;
  222. case VASurfaceAttribMaxHeight:
  223. constraints->max_height = attr_list[i].value.value.i;
  224. break;
  225. }
  226. }
  227. if (pix_fmt_count == 0) {
  228. // Nothing usable found. Presumably there exists something which
  229. // works, so leave the set null to indicate unknown.
  230. constraints->valid_sw_formats = NULL;
  231. } else {
  232. constraints->valid_sw_formats = av_malloc_array(pix_fmt_count + 1,
  233. sizeof(pix_fmt));
  234. if (!constraints->valid_sw_formats) {
  235. err = AVERROR(ENOMEM);
  236. goto fail;
  237. }
  238. for (i = j = 0; i < attr_count; i++) {
  239. if (attr_list[i].type != VASurfaceAttribPixelFormat)
  240. continue;
  241. fourcc = attr_list[i].value.value.i;
  242. pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
  243. if (pix_fmt != AV_PIX_FMT_NONE)
  244. constraints->valid_sw_formats[j++] = pix_fmt;
  245. }
  246. av_assert0(j == pix_fmt_count);
  247. constraints->valid_sw_formats[j] = AV_PIX_FMT_NONE;
  248. }
  249. } else {
  250. // No configuration supplied.
  251. // Return the full set of image formats known by the implementation.
  252. constraints->valid_sw_formats = av_malloc_array(ctx->nb_formats + 1,
  253. sizeof(pix_fmt));
  254. if (!constraints->valid_sw_formats) {
  255. err = AVERROR(ENOMEM);
  256. goto fail;
  257. }
  258. for (i = 0; i < ctx->nb_formats; i++)
  259. constraints->valid_sw_formats[i] = ctx->formats[i].pix_fmt;
  260. constraints->valid_sw_formats[i] = AV_PIX_FMT_NONE;
  261. }
  262. constraints->valid_hw_formats = av_malloc_array(2, sizeof(pix_fmt));
  263. if (!constraints->valid_hw_formats) {
  264. err = AVERROR(ENOMEM);
  265. goto fail;
  266. }
  267. constraints->valid_hw_formats[0] = AV_PIX_FMT_VAAPI;
  268. constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
  269. err = 0;
  270. fail:
  271. av_freep(&attr_list);
  272. return err;
  273. }
  274. static const struct {
  275. const char *friendly_name;
  276. const char *match_string;
  277. unsigned int quirks;
  278. } vaapi_driver_quirks_table[] = {
  279. #if !VA_CHECK_VERSION(1, 0, 0)
  280. // The i965 driver did not conform before version 2.0.
  281. {
  282. "Intel i965 (Quick Sync)",
  283. "i965",
  284. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS,
  285. },
  286. #endif
  287. {
  288. "Intel iHD",
  289. "ubit",
  290. AV_VAAPI_DRIVER_QUIRK_ATTRIB_MEMTYPE,
  291. },
  292. {
  293. "VDPAU wrapper",
  294. "Splitted-Desktop Systems VDPAU backend for VA-API",
  295. AV_VAAPI_DRIVER_QUIRK_SURFACE_ATTRIBUTES,
  296. },
  297. };
  298. static int vaapi_device_init(AVHWDeviceContext *hwdev)
  299. {
  300. VAAPIDeviceContext *ctx = hwdev->internal->priv;
  301. AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
  302. VAImageFormat *image_list = NULL;
  303. VAStatus vas;
  304. const char *vendor_string;
  305. int err, i, image_count;
  306. enum AVPixelFormat pix_fmt;
  307. unsigned int fourcc;
  308. image_count = vaMaxNumImageFormats(hwctx->display);
  309. if (image_count <= 0) {
  310. err = AVERROR(EIO);
  311. goto fail;
  312. }
  313. image_list = av_malloc(image_count * sizeof(*image_list));
  314. if (!image_list) {
  315. err = AVERROR(ENOMEM);
  316. goto fail;
  317. }
  318. vas = vaQueryImageFormats(hwctx->display, image_list, &image_count);
  319. if (vas != VA_STATUS_SUCCESS) {
  320. err = AVERROR(EIO);
  321. goto fail;
  322. }
  323. ctx->formats = av_malloc(image_count * sizeof(*ctx->formats));
  324. if (!ctx->formats) {
  325. err = AVERROR(ENOMEM);
  326. goto fail;
  327. }
  328. ctx->nb_formats = 0;
  329. for (i = 0; i < image_count; i++) {
  330. fourcc = image_list[i].fourcc;
  331. pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
  332. if (pix_fmt == AV_PIX_FMT_NONE) {
  333. av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> unknown.\n",
  334. fourcc);
  335. } else {
  336. av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> %s.\n",
  337. fourcc, av_get_pix_fmt_name(pix_fmt));
  338. ctx->formats[ctx->nb_formats].pix_fmt = pix_fmt;
  339. ctx->formats[ctx->nb_formats].image_format = image_list[i];
  340. ++ctx->nb_formats;
  341. }
  342. }
  343. vendor_string = vaQueryVendorString(hwctx->display);
  344. if (vendor_string)
  345. av_log(hwdev, AV_LOG_VERBOSE, "VAAPI driver: %s.\n", vendor_string);
  346. if (hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_USER_SET) {
  347. av_log(hwdev, AV_LOG_VERBOSE, "Using quirks set by user (%#x).\n",
  348. hwctx->driver_quirks);
  349. } else {
  350. // Detect the driver in use and set quirk flags if necessary.
  351. hwctx->driver_quirks = 0;
  352. if (vendor_string) {
  353. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_driver_quirks_table); i++) {
  354. if (strstr(vendor_string,
  355. vaapi_driver_quirks_table[i].match_string)) {
  356. av_log(hwdev, AV_LOG_VERBOSE, "Matched driver string "
  357. "as known nonstandard driver \"%s\", setting "
  358. "quirks (%#x).\n",
  359. vaapi_driver_quirks_table[i].friendly_name,
  360. vaapi_driver_quirks_table[i].quirks);
  361. hwctx->driver_quirks |=
  362. vaapi_driver_quirks_table[i].quirks;
  363. break;
  364. }
  365. }
  366. if (!(i < FF_ARRAY_ELEMS(vaapi_driver_quirks_table))) {
  367. av_log(hwdev, AV_LOG_VERBOSE, "Driver not found in known "
  368. "nonstandard list, using standard behaviour.\n");
  369. }
  370. } else {
  371. av_log(hwdev, AV_LOG_VERBOSE, "Driver has no vendor string, "
  372. "assuming standard behaviour.\n");
  373. }
  374. }
  375. av_free(image_list);
  376. return 0;
  377. fail:
  378. av_freep(&ctx->formats);
  379. av_free(image_list);
  380. return err;
  381. }
  382. static void vaapi_device_uninit(AVHWDeviceContext *hwdev)
  383. {
  384. VAAPIDeviceContext *ctx = hwdev->internal->priv;
  385. av_freep(&ctx->formats);
  386. }
  387. static void vaapi_buffer_free(void *opaque, uint8_t *data)
  388. {
  389. AVHWFramesContext *hwfc = opaque;
  390. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  391. VASurfaceID surface_id;
  392. VAStatus vas;
  393. surface_id = (VASurfaceID)(uintptr_t)data;
  394. vas = vaDestroySurfaces(hwctx->display, &surface_id, 1);
  395. if (vas != VA_STATUS_SUCCESS) {
  396. av_log(hwfc, AV_LOG_ERROR, "Failed to destroy surface %#x: "
  397. "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
  398. }
  399. }
  400. static AVBufferRef *vaapi_pool_alloc(void *opaque, int size)
  401. {
  402. AVHWFramesContext *hwfc = opaque;
  403. VAAPIFramesContext *ctx = hwfc->internal->priv;
  404. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  405. AVVAAPIFramesContext *avfc = hwfc->hwctx;
  406. VASurfaceID surface_id;
  407. VAStatus vas;
  408. AVBufferRef *ref;
  409. if (hwfc->initial_pool_size > 0 &&
  410. avfc->nb_surfaces >= hwfc->initial_pool_size)
  411. return NULL;
  412. vas = vaCreateSurfaces(hwctx->display, ctx->rt_format,
  413. hwfc->width, hwfc->height,
  414. &surface_id, 1,
  415. ctx->attributes, ctx->nb_attributes);
  416. if (vas != VA_STATUS_SUCCESS) {
  417. av_log(hwfc, AV_LOG_ERROR, "Failed to create surface: "
  418. "%d (%s).\n", vas, vaErrorStr(vas));
  419. return NULL;
  420. }
  421. av_log(hwfc, AV_LOG_DEBUG, "Created surface %#x.\n", surface_id);
  422. ref = av_buffer_create((uint8_t*)(uintptr_t)surface_id,
  423. sizeof(surface_id), &vaapi_buffer_free,
  424. hwfc, AV_BUFFER_FLAG_READONLY);
  425. if (!ref) {
  426. vaDestroySurfaces(hwctx->display, &surface_id, 1);
  427. return NULL;
  428. }
  429. if (hwfc->initial_pool_size > 0) {
  430. // This is a fixed-size pool, so we must still be in the initial
  431. // allocation sequence.
  432. av_assert0(avfc->nb_surfaces < hwfc->initial_pool_size);
  433. avfc->surface_ids[avfc->nb_surfaces] = surface_id;
  434. ++avfc->nb_surfaces;
  435. }
  436. return ref;
  437. }
  438. static int vaapi_frames_init(AVHWFramesContext *hwfc)
  439. {
  440. AVVAAPIFramesContext *avfc = hwfc->hwctx;
  441. VAAPIFramesContext *ctx = hwfc->internal->priv;
  442. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  443. const VAAPIFormatDescriptor *desc;
  444. VAImageFormat *expected_format;
  445. AVBufferRef *test_surface = NULL;
  446. VASurfaceID test_surface_id;
  447. VAImage test_image;
  448. VAStatus vas;
  449. int err, i;
  450. desc = vaapi_format_from_pix_fmt(hwfc->sw_format);
  451. if (!desc) {
  452. av_log(hwfc, AV_LOG_ERROR, "Unsupported format: %s.\n",
  453. av_get_pix_fmt_name(hwfc->sw_format));
  454. return AVERROR(EINVAL);
  455. }
  456. if (!hwfc->pool) {
  457. if (!(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_SURFACE_ATTRIBUTES)) {
  458. int need_memory_type = !(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_ATTRIB_MEMTYPE);
  459. int need_pixel_format = 1;
  460. for (i = 0; i < avfc->nb_attributes; i++) {
  461. if (avfc->attributes[i].type == VASurfaceAttribMemoryType)
  462. need_memory_type = 0;
  463. if (avfc->attributes[i].type == VASurfaceAttribPixelFormat)
  464. need_pixel_format = 0;
  465. }
  466. ctx->nb_attributes =
  467. avfc->nb_attributes + need_memory_type + need_pixel_format;
  468. ctx->attributes = av_malloc(ctx->nb_attributes *
  469. sizeof(*ctx->attributes));
  470. if (!ctx->attributes) {
  471. err = AVERROR(ENOMEM);
  472. goto fail;
  473. }
  474. for (i = 0; i < avfc->nb_attributes; i++)
  475. ctx->attributes[i] = avfc->attributes[i];
  476. if (need_memory_type) {
  477. ctx->attributes[i++] = (VASurfaceAttrib) {
  478. .type = VASurfaceAttribMemoryType,
  479. .flags = VA_SURFACE_ATTRIB_SETTABLE,
  480. .value.type = VAGenericValueTypeInteger,
  481. .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA,
  482. };
  483. }
  484. if (need_pixel_format) {
  485. ctx->attributes[i++] = (VASurfaceAttrib) {
  486. .type = VASurfaceAttribPixelFormat,
  487. .flags = VA_SURFACE_ATTRIB_SETTABLE,
  488. .value.type = VAGenericValueTypeInteger,
  489. .value.value.i = desc->fourcc,
  490. };
  491. }
  492. av_assert0(i == ctx->nb_attributes);
  493. } else {
  494. ctx->attributes = NULL;
  495. ctx->nb_attributes = 0;
  496. }
  497. ctx->rt_format = desc->rt_format;
  498. if (hwfc->initial_pool_size > 0) {
  499. // This pool will be usable as a render target, so we need to store
  500. // all of the surface IDs somewhere that vaCreateContext() calls
  501. // will be able to access them.
  502. avfc->nb_surfaces = 0;
  503. avfc->surface_ids = av_malloc(hwfc->initial_pool_size *
  504. sizeof(*avfc->surface_ids));
  505. if (!avfc->surface_ids) {
  506. err = AVERROR(ENOMEM);
  507. goto fail;
  508. }
  509. } else {
  510. // This pool allows dynamic sizing, and will not be usable as a
  511. // render target.
  512. avfc->nb_surfaces = 0;
  513. avfc->surface_ids = NULL;
  514. }
  515. hwfc->internal->pool_internal =
  516. av_buffer_pool_init2(sizeof(VASurfaceID), hwfc,
  517. &vaapi_pool_alloc, NULL);
  518. if (!hwfc->internal->pool_internal) {
  519. av_log(hwfc, AV_LOG_ERROR, "Failed to create VAAPI surface pool.\n");
  520. err = AVERROR(ENOMEM);
  521. goto fail;
  522. }
  523. }
  524. // Allocate a single surface to test whether vaDeriveImage() is going
  525. // to work for the specific configuration.
  526. if (hwfc->pool) {
  527. test_surface = av_buffer_pool_get(hwfc->pool);
  528. if (!test_surface) {
  529. av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
  530. "user-configured buffer pool.\n");
  531. err = AVERROR(ENOMEM);
  532. goto fail;
  533. }
  534. } else {
  535. test_surface = av_buffer_pool_get(hwfc->internal->pool_internal);
  536. if (!test_surface) {
  537. av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
  538. "internal buffer pool.\n");
  539. err = AVERROR(ENOMEM);
  540. goto fail;
  541. }
  542. }
  543. test_surface_id = (VASurfaceID)(uintptr_t)test_surface->data;
  544. ctx->derive_works = 0;
  545. err = vaapi_get_image_format(hwfc->device_ctx,
  546. hwfc->sw_format, &expected_format);
  547. if (err == 0) {
  548. vas = vaDeriveImage(hwctx->display, test_surface_id, &test_image);
  549. if (vas == VA_STATUS_SUCCESS) {
  550. if (expected_format->fourcc == test_image.format.fourcc) {
  551. av_log(hwfc, AV_LOG_DEBUG, "Direct mapping possible.\n");
  552. ctx->derive_works = 1;
  553. } else {
  554. av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
  555. "derived image format %08x does not match "
  556. "expected format %08x.\n",
  557. expected_format->fourcc, test_image.format.fourcc);
  558. }
  559. vaDestroyImage(hwctx->display, test_image.image_id);
  560. } else {
  561. av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
  562. "deriving image does not work: "
  563. "%d (%s).\n", vas, vaErrorStr(vas));
  564. }
  565. } else {
  566. av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
  567. "image format is not supported.\n");
  568. }
  569. av_buffer_unref(&test_surface);
  570. return 0;
  571. fail:
  572. av_buffer_unref(&test_surface);
  573. av_freep(&avfc->surface_ids);
  574. av_freep(&ctx->attributes);
  575. return err;
  576. }
  577. static void vaapi_frames_uninit(AVHWFramesContext *hwfc)
  578. {
  579. AVVAAPIFramesContext *avfc = hwfc->hwctx;
  580. VAAPIFramesContext *ctx = hwfc->internal->priv;
  581. av_freep(&avfc->surface_ids);
  582. av_freep(&ctx->attributes);
  583. }
  584. static int vaapi_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
  585. {
  586. frame->buf[0] = av_buffer_pool_get(hwfc->pool);
  587. if (!frame->buf[0])
  588. return AVERROR(ENOMEM);
  589. frame->data[3] = frame->buf[0]->data;
  590. frame->format = AV_PIX_FMT_VAAPI;
  591. frame->width = hwfc->width;
  592. frame->height = hwfc->height;
  593. return 0;
  594. }
  595. static int vaapi_transfer_get_formats(AVHWFramesContext *hwfc,
  596. enum AVHWFrameTransferDirection dir,
  597. enum AVPixelFormat **formats)
  598. {
  599. VAAPIDeviceContext *ctx = hwfc->device_ctx->internal->priv;
  600. enum AVPixelFormat *pix_fmts;
  601. int i, k, sw_format_available;
  602. sw_format_available = 0;
  603. for (i = 0; i < ctx->nb_formats; i++) {
  604. if (ctx->formats[i].pix_fmt == hwfc->sw_format)
  605. sw_format_available = 1;
  606. }
  607. pix_fmts = av_malloc((ctx->nb_formats + 1) * sizeof(*pix_fmts));
  608. if (!pix_fmts)
  609. return AVERROR(ENOMEM);
  610. if (sw_format_available) {
  611. pix_fmts[0] = hwfc->sw_format;
  612. k = 1;
  613. } else {
  614. k = 0;
  615. }
  616. for (i = 0; i < ctx->nb_formats; i++) {
  617. if (ctx->formats[i].pix_fmt == hwfc->sw_format)
  618. continue;
  619. av_assert0(k < ctx->nb_formats);
  620. pix_fmts[k++] = ctx->formats[i].pix_fmt;
  621. }
  622. pix_fmts[k] = AV_PIX_FMT_NONE;
  623. *formats = pix_fmts;
  624. return 0;
  625. }
  626. static void vaapi_unmap_frame(AVHWFramesContext *hwfc,
  627. HWMapDescriptor *hwmap)
  628. {
  629. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  630. VAAPIMapping *map = hwmap->priv;
  631. VASurfaceID surface_id;
  632. VAStatus vas;
  633. surface_id = (VASurfaceID)(uintptr_t)hwmap->source->data[3];
  634. av_log(hwfc, AV_LOG_DEBUG, "Unmap surface %#x.\n", surface_id);
  635. vas = vaUnmapBuffer(hwctx->display, map->image.buf);
  636. if (vas != VA_STATUS_SUCCESS) {
  637. av_log(hwfc, AV_LOG_ERROR, "Failed to unmap image from surface "
  638. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  639. }
  640. if ((map->flags & AV_HWFRAME_MAP_WRITE) &&
  641. !(map->flags & AV_HWFRAME_MAP_DIRECT)) {
  642. vas = vaPutImage(hwctx->display, surface_id, map->image.image_id,
  643. 0, 0, hwfc->width, hwfc->height,
  644. 0, 0, hwfc->width, hwfc->height);
  645. if (vas != VA_STATUS_SUCCESS) {
  646. av_log(hwfc, AV_LOG_ERROR, "Failed to write image to surface "
  647. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  648. }
  649. }
  650. vas = vaDestroyImage(hwctx->display, map->image.image_id);
  651. if (vas != VA_STATUS_SUCCESS) {
  652. av_log(hwfc, AV_LOG_ERROR, "Failed to destroy image from surface "
  653. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  654. }
  655. av_free(map);
  656. }
  657. static int vaapi_map_frame(AVHWFramesContext *hwfc,
  658. AVFrame *dst, const AVFrame *src, int flags)
  659. {
  660. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  661. VAAPIFramesContext *ctx = hwfc->internal->priv;
  662. VASurfaceID surface_id;
  663. const VAAPIFormatDescriptor *desc;
  664. VAImageFormat *image_format;
  665. VAAPIMapping *map;
  666. VAStatus vas;
  667. void *address = NULL;
  668. int err, i;
  669. surface_id = (VASurfaceID)(uintptr_t)src->data[3];
  670. av_log(hwfc, AV_LOG_DEBUG, "Map surface %#x.\n", surface_id);
  671. if (!ctx->derive_works && (flags & AV_HWFRAME_MAP_DIRECT)) {
  672. // Requested direct mapping but it is not possible.
  673. return AVERROR(EINVAL);
  674. }
  675. if (dst->format == AV_PIX_FMT_NONE)
  676. dst->format = hwfc->sw_format;
  677. if (dst->format != hwfc->sw_format && (flags & AV_HWFRAME_MAP_DIRECT)) {
  678. // Requested direct mapping but the formats do not match.
  679. return AVERROR(EINVAL);
  680. }
  681. err = vaapi_get_image_format(hwfc->device_ctx, dst->format, &image_format);
  682. if (err < 0) {
  683. // Requested format is not a valid output format.
  684. return AVERROR(EINVAL);
  685. }
  686. map = av_malloc(sizeof(*map));
  687. if (!map)
  688. return AVERROR(ENOMEM);
  689. map->flags = flags;
  690. map->image.image_id = VA_INVALID_ID;
  691. vas = vaSyncSurface(hwctx->display, surface_id);
  692. if (vas != VA_STATUS_SUCCESS) {
  693. av_log(hwfc, AV_LOG_ERROR, "Failed to sync surface "
  694. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  695. err = AVERROR(EIO);
  696. goto fail;
  697. }
  698. // The memory which we map using derive need not be connected to the CPU
  699. // in a way conducive to fast access. On Gen7-Gen9 Intel graphics, the
  700. // memory is mappable but not cached, so normal memcpy()-like access is
  701. // very slow to read it (but writing is ok). It is possible to read much
  702. // faster with a copy routine which is aware of the limitation, but we
  703. // assume for now that the user is not aware of that and would therefore
  704. // prefer not to be given direct-mapped memory if they request read access.
  705. if (ctx->derive_works && dst->format == hwfc->sw_format &&
  706. ((flags & AV_HWFRAME_MAP_DIRECT) || !(flags & AV_HWFRAME_MAP_READ))) {
  707. vas = vaDeriveImage(hwctx->display, surface_id, &map->image);
  708. if (vas != VA_STATUS_SUCCESS) {
  709. av_log(hwfc, AV_LOG_ERROR, "Failed to derive image from "
  710. "surface %#x: %d (%s).\n",
  711. surface_id, vas, vaErrorStr(vas));
  712. err = AVERROR(EIO);
  713. goto fail;
  714. }
  715. if (map->image.format.fourcc != image_format->fourcc) {
  716. av_log(hwfc, AV_LOG_ERROR, "Derive image of surface %#x "
  717. "is in wrong format: expected %#08x, got %#08x.\n",
  718. surface_id, image_format->fourcc, map->image.format.fourcc);
  719. err = AVERROR(EIO);
  720. goto fail;
  721. }
  722. map->flags |= AV_HWFRAME_MAP_DIRECT;
  723. } else {
  724. vas = vaCreateImage(hwctx->display, image_format,
  725. hwfc->width, hwfc->height, &map->image);
  726. if (vas != VA_STATUS_SUCCESS) {
  727. av_log(hwfc, AV_LOG_ERROR, "Failed to create image for "
  728. "surface %#x: %d (%s).\n",
  729. surface_id, vas, vaErrorStr(vas));
  730. err = AVERROR(EIO);
  731. goto fail;
  732. }
  733. if (!(flags & AV_HWFRAME_MAP_OVERWRITE)) {
  734. vas = vaGetImage(hwctx->display, surface_id, 0, 0,
  735. hwfc->width, hwfc->height, map->image.image_id);
  736. if (vas != VA_STATUS_SUCCESS) {
  737. av_log(hwfc, AV_LOG_ERROR, "Failed to read image from "
  738. "surface %#x: %d (%s).\n",
  739. surface_id, vas, vaErrorStr(vas));
  740. err = AVERROR(EIO);
  741. goto fail;
  742. }
  743. }
  744. }
  745. vas = vaMapBuffer(hwctx->display, map->image.buf, &address);
  746. if (vas != VA_STATUS_SUCCESS) {
  747. av_log(hwfc, AV_LOG_ERROR, "Failed to map image from surface "
  748. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  749. err = AVERROR(EIO);
  750. goto fail;
  751. }
  752. err = ff_hwframe_map_create(src->hw_frames_ctx,
  753. dst, src, &vaapi_unmap_frame, map);
  754. if (err < 0)
  755. goto fail;
  756. dst->width = src->width;
  757. dst->height = src->height;
  758. for (i = 0; i < map->image.num_planes; i++) {
  759. dst->data[i] = (uint8_t*)address + map->image.offsets[i];
  760. dst->linesize[i] = map->image.pitches[i];
  761. }
  762. desc = vaapi_format_from_fourcc(map->image.format.fourcc);
  763. if (desc && desc->chroma_planes_swapped) {
  764. // Chroma planes are YVU rather than YUV, so swap them.
  765. FFSWAP(uint8_t*, dst->data[1], dst->data[2]);
  766. }
  767. return 0;
  768. fail:
  769. if (map) {
  770. if (address)
  771. vaUnmapBuffer(hwctx->display, map->image.buf);
  772. if (map->image.image_id != VA_INVALID_ID)
  773. vaDestroyImage(hwctx->display, map->image.image_id);
  774. av_free(map);
  775. }
  776. return err;
  777. }
  778. static int vaapi_transfer_data_from(AVHWFramesContext *hwfc,
  779. AVFrame *dst, const AVFrame *src)
  780. {
  781. AVFrame *map;
  782. int err;
  783. if (dst->width > hwfc->width || dst->height > hwfc->height)
  784. return AVERROR(EINVAL);
  785. map = av_frame_alloc();
  786. if (!map)
  787. return AVERROR(ENOMEM);
  788. map->format = dst->format;
  789. err = vaapi_map_frame(hwfc, map, src, AV_HWFRAME_MAP_READ);
  790. if (err)
  791. goto fail;
  792. map->width = dst->width;
  793. map->height = dst->height;
  794. err = av_frame_copy(dst, map);
  795. if (err)
  796. goto fail;
  797. err = 0;
  798. fail:
  799. av_frame_free(&map);
  800. return err;
  801. }
  802. static int vaapi_transfer_data_to(AVHWFramesContext *hwfc,
  803. AVFrame *dst, const AVFrame *src)
  804. {
  805. AVFrame *map;
  806. int err;
  807. if (src->width > hwfc->width || src->height > hwfc->height)
  808. return AVERROR(EINVAL);
  809. map = av_frame_alloc();
  810. if (!map)
  811. return AVERROR(ENOMEM);
  812. map->format = src->format;
  813. err = vaapi_map_frame(hwfc, map, dst, AV_HWFRAME_MAP_WRITE | AV_HWFRAME_MAP_OVERWRITE);
  814. if (err)
  815. goto fail;
  816. map->width = src->width;
  817. map->height = src->height;
  818. err = av_frame_copy(map, src);
  819. if (err)
  820. goto fail;
  821. err = 0;
  822. fail:
  823. av_frame_free(&map);
  824. return err;
  825. }
  826. static int vaapi_map_to_memory(AVHWFramesContext *hwfc, AVFrame *dst,
  827. const AVFrame *src, int flags)
  828. {
  829. int err;
  830. if (dst->format != AV_PIX_FMT_NONE) {
  831. err = vaapi_get_image_format(hwfc->device_ctx, dst->format, NULL);
  832. if (err < 0)
  833. return AVERROR(ENOSYS);
  834. }
  835. err = vaapi_map_frame(hwfc, dst, src, flags);
  836. if (err)
  837. return err;
  838. err = av_frame_copy_props(dst, src);
  839. if (err)
  840. return err;
  841. return 0;
  842. }
  843. #if CONFIG_LIBDRM
  844. #define DRM_MAP(va, layers, ...) { \
  845. VA_FOURCC_ ## va, \
  846. layers, \
  847. { __VA_ARGS__ } \
  848. }
  849. static const struct {
  850. uint32_t va_fourcc;
  851. int nb_layer_formats;
  852. uint32_t layer_formats[AV_DRM_MAX_PLANES];
  853. } vaapi_drm_format_map[] = {
  854. #ifdef DRM_FORMAT_R8
  855. DRM_MAP(NV12, 2, DRM_FORMAT_R8, DRM_FORMAT_RG88),
  856. #endif
  857. DRM_MAP(NV12, 1, DRM_FORMAT_NV12),
  858. #if defined(VA_FOURCC_P010) && defined(DRM_FORMAT_R16)
  859. DRM_MAP(P010, 2, DRM_FORMAT_R16, DRM_FORMAT_RG1616),
  860. #endif
  861. DRM_MAP(BGRA, 1, DRM_FORMAT_ARGB8888),
  862. DRM_MAP(BGRX, 1, DRM_FORMAT_XRGB8888),
  863. DRM_MAP(RGBA, 1, DRM_FORMAT_ABGR8888),
  864. DRM_MAP(RGBX, 1, DRM_FORMAT_XBGR8888),
  865. #ifdef VA_FOURCC_ABGR
  866. DRM_MAP(ABGR, 1, DRM_FORMAT_RGBA8888),
  867. DRM_MAP(XBGR, 1, DRM_FORMAT_RGBX8888),
  868. #endif
  869. DRM_MAP(ARGB, 1, DRM_FORMAT_BGRA8888),
  870. DRM_MAP(XRGB, 1, DRM_FORMAT_BGRX8888),
  871. };
  872. #undef DRM_MAP
  873. static void vaapi_unmap_from_drm(AVHWFramesContext *dst_fc,
  874. HWMapDescriptor *hwmap)
  875. {
  876. AVVAAPIDeviceContext *dst_dev = dst_fc->device_ctx->hwctx;
  877. VASurfaceID surface_id = (VASurfaceID)(uintptr_t)hwmap->priv;
  878. av_log(dst_fc, AV_LOG_DEBUG, "Destroy surface %#x.\n", surface_id);
  879. vaDestroySurfaces(dst_dev->display, &surface_id, 1);
  880. }
  881. static int vaapi_map_from_drm(AVHWFramesContext *src_fc, AVFrame *dst,
  882. const AVFrame *src, int flags)
  883. {
  884. AVHWFramesContext *dst_fc =
  885. (AVHWFramesContext*)dst->hw_frames_ctx->data;
  886. AVVAAPIDeviceContext *dst_dev = dst_fc->device_ctx->hwctx;
  887. const AVDRMFrameDescriptor *desc;
  888. const VAAPIFormatDescriptor *format_desc;
  889. VASurfaceID surface_id;
  890. VAStatus vas;
  891. uint32_t va_fourcc;
  892. int err, i, j, k;
  893. unsigned long buffer_handle;
  894. VASurfaceAttribExternalBuffers buffer_desc;
  895. VASurfaceAttrib attrs[2] = {
  896. {
  897. .type = VASurfaceAttribMemoryType,
  898. .flags = VA_SURFACE_ATTRIB_SETTABLE,
  899. .value.type = VAGenericValueTypeInteger,
  900. .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME,
  901. },
  902. {
  903. .type = VASurfaceAttribExternalBufferDescriptor,
  904. .flags = VA_SURFACE_ATTRIB_SETTABLE,
  905. .value.type = VAGenericValueTypePointer,
  906. .value.value.p = &buffer_desc,
  907. }
  908. };
  909. desc = (AVDRMFrameDescriptor*)src->data[0];
  910. if (desc->nb_objects != 1) {
  911. av_log(dst_fc, AV_LOG_ERROR, "VAAPI can only map frames "
  912. "made from a single DRM object.\n");
  913. return AVERROR(EINVAL);
  914. }
  915. va_fourcc = 0;
  916. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_drm_format_map); i++) {
  917. if (desc->nb_layers != vaapi_drm_format_map[i].nb_layer_formats)
  918. continue;
  919. for (j = 0; j < desc->nb_layers; j++) {
  920. if (desc->layers[j].format !=
  921. vaapi_drm_format_map[i].layer_formats[j])
  922. break;
  923. }
  924. if (j != desc->nb_layers)
  925. continue;
  926. va_fourcc = vaapi_drm_format_map[i].va_fourcc;
  927. break;
  928. }
  929. if (!va_fourcc) {
  930. av_log(dst_fc, AV_LOG_ERROR, "DRM format not supported "
  931. "by VAAPI.\n");
  932. return AVERROR(EINVAL);
  933. }
  934. av_log(dst_fc, AV_LOG_DEBUG, "Map DRM object %d to VAAPI as "
  935. "%08x.\n", desc->objects[0].fd, va_fourcc);
  936. format_desc = vaapi_format_from_fourcc(va_fourcc);
  937. av_assert0(format_desc);
  938. buffer_handle = desc->objects[0].fd;
  939. buffer_desc.pixel_format = va_fourcc;
  940. buffer_desc.width = src_fc->width;
  941. buffer_desc.height = src_fc->height;
  942. buffer_desc.data_size = desc->objects[0].size;
  943. buffer_desc.buffers = &buffer_handle;
  944. buffer_desc.num_buffers = 1;
  945. buffer_desc.flags = 0;
  946. k = 0;
  947. for (i = 0; i < desc->nb_layers; i++) {
  948. for (j = 0; j < desc->layers[i].nb_planes; j++) {
  949. buffer_desc.pitches[k] = desc->layers[i].planes[j].pitch;
  950. buffer_desc.offsets[k] = desc->layers[i].planes[j].offset;
  951. ++k;
  952. }
  953. }
  954. buffer_desc.num_planes = k;
  955. if (format_desc->chroma_planes_swapped &&
  956. buffer_desc.num_planes == 3) {
  957. FFSWAP(uint32_t, buffer_desc.pitches[1], buffer_desc.pitches[2]);
  958. FFSWAP(uint32_t, buffer_desc.offsets[1], buffer_desc.offsets[2]);
  959. }
  960. vas = vaCreateSurfaces(dst_dev->display, format_desc->rt_format,
  961. src->width, src->height,
  962. &surface_id, 1,
  963. attrs, FF_ARRAY_ELEMS(attrs));
  964. if (vas != VA_STATUS_SUCCESS) {
  965. av_log(dst_fc, AV_LOG_ERROR, "Failed to create surface from DRM "
  966. "object: %d (%s).\n", vas, vaErrorStr(vas));
  967. return AVERROR(EIO);
  968. }
  969. av_log(dst_fc, AV_LOG_DEBUG, "Create surface %#x.\n", surface_id);
  970. err = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src,
  971. &vaapi_unmap_from_drm,
  972. (void*)(uintptr_t)surface_id);
  973. if (err < 0)
  974. return err;
  975. dst->width = src->width;
  976. dst->height = src->height;
  977. dst->data[3] = (uint8_t*)(uintptr_t)surface_id;
  978. av_log(dst_fc, AV_LOG_DEBUG, "Mapped DRM object %d to "
  979. "surface %#x.\n", desc->objects[0].fd, surface_id);
  980. return 0;
  981. }
  982. #if VA_CHECK_VERSION(1, 1, 0)
  983. static void vaapi_unmap_to_drm_esh(AVHWFramesContext *hwfc,
  984. HWMapDescriptor *hwmap)
  985. {
  986. AVDRMFrameDescriptor *drm_desc = hwmap->priv;
  987. int i;
  988. for (i = 0; i < drm_desc->nb_objects; i++)
  989. close(drm_desc->objects[i].fd);
  990. av_freep(&drm_desc);
  991. }
  992. static int vaapi_map_to_drm_esh(AVHWFramesContext *hwfc, AVFrame *dst,
  993. const AVFrame *src, int flags)
  994. {
  995. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  996. VASurfaceID surface_id;
  997. VAStatus vas;
  998. VADRMPRIMESurfaceDescriptor va_desc;
  999. AVDRMFrameDescriptor *drm_desc = NULL;
  1000. uint32_t export_flags;
  1001. int err, i, j;
  1002. surface_id = (VASurfaceID)(uintptr_t)src->data[3];
  1003. export_flags = VA_EXPORT_SURFACE_SEPARATE_LAYERS;
  1004. if (flags & AV_HWFRAME_MAP_READ)
  1005. export_flags |= VA_EXPORT_SURFACE_READ_ONLY;
  1006. if (flags & AV_HWFRAME_MAP_WRITE)
  1007. export_flags |= VA_EXPORT_SURFACE_WRITE_ONLY;
  1008. vas = vaExportSurfaceHandle(hwctx->display, surface_id,
  1009. VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2,
  1010. export_flags, &va_desc);
  1011. if (vas != VA_STATUS_SUCCESS) {
  1012. if (vas == VA_STATUS_ERROR_UNIMPLEMENTED)
  1013. return AVERROR(ENOSYS);
  1014. av_log(hwfc, AV_LOG_ERROR, "Failed to export surface %#x: "
  1015. "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
  1016. return AVERROR(EIO);
  1017. }
  1018. drm_desc = av_mallocz(sizeof(*drm_desc));
  1019. if (!drm_desc) {
  1020. err = AVERROR(ENOMEM);
  1021. goto fail;
  1022. }
  1023. // By some bizarre coincidence, these structures are very similar...
  1024. drm_desc->nb_objects = va_desc.num_objects;
  1025. for (i = 0; i < va_desc.num_objects; i++) {
  1026. drm_desc->objects[i].fd = va_desc.objects[i].fd;
  1027. drm_desc->objects[i].size = va_desc.objects[i].size;
  1028. drm_desc->objects[i].format_modifier =
  1029. va_desc.objects[i].drm_format_modifier;
  1030. }
  1031. drm_desc->nb_layers = va_desc.num_layers;
  1032. for (i = 0; i < va_desc.num_layers; i++) {
  1033. drm_desc->layers[i].format = va_desc.layers[i].drm_format;
  1034. drm_desc->layers[i].nb_planes = va_desc.layers[i].num_planes;
  1035. for (j = 0; j < va_desc.layers[i].num_planes; j++) {
  1036. drm_desc->layers[i].planes[j].object_index =
  1037. va_desc.layers[i].object_index[j];
  1038. drm_desc->layers[i].planes[j].offset =
  1039. va_desc.layers[i].offset[j];
  1040. drm_desc->layers[i].planes[j].pitch =
  1041. va_desc.layers[i].pitch[j];
  1042. }
  1043. }
  1044. err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
  1045. &vaapi_unmap_to_drm_esh, drm_desc);
  1046. if (err < 0)
  1047. goto fail;
  1048. dst->width = src->width;
  1049. dst->height = src->height;
  1050. dst->data[0] = (uint8_t*)drm_desc;
  1051. return 0;
  1052. fail:
  1053. for (i = 0; i < va_desc.num_objects; i++)
  1054. close(va_desc.objects[i].fd);
  1055. av_freep(&drm_desc);
  1056. return err;
  1057. }
  1058. #endif
  1059. #if VA_CHECK_VERSION(0, 36, 0)
  1060. typedef struct VAAPIDRMImageBufferMapping {
  1061. VAImage image;
  1062. VABufferInfo buffer_info;
  1063. AVDRMFrameDescriptor drm_desc;
  1064. } VAAPIDRMImageBufferMapping;
  1065. static void vaapi_unmap_to_drm_abh(AVHWFramesContext *hwfc,
  1066. HWMapDescriptor *hwmap)
  1067. {
  1068. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  1069. VAAPIDRMImageBufferMapping *mapping = hwmap->priv;
  1070. VASurfaceID surface_id;
  1071. VAStatus vas;
  1072. surface_id = (VASurfaceID)(uintptr_t)hwmap->source->data[3];
  1073. av_log(hwfc, AV_LOG_DEBUG, "Unmap VAAPI surface %#x from DRM.\n",
  1074. surface_id);
  1075. // DRM PRIME file descriptors are closed by vaReleaseBufferHandle(),
  1076. // so we shouldn't close them separately.
  1077. vas = vaReleaseBufferHandle(hwctx->display, mapping->image.buf);
  1078. if (vas != VA_STATUS_SUCCESS) {
  1079. av_log(hwfc, AV_LOG_ERROR, "Failed to release buffer "
  1080. "handle of image %#x (derived from surface %#x): "
  1081. "%d (%s).\n", mapping->image.buf, surface_id,
  1082. vas, vaErrorStr(vas));
  1083. }
  1084. vas = vaDestroyImage(hwctx->display, mapping->image.image_id);
  1085. if (vas != VA_STATUS_SUCCESS) {
  1086. av_log(hwfc, AV_LOG_ERROR, "Failed to destroy image "
  1087. "derived from surface %#x: %d (%s).\n",
  1088. surface_id, vas, vaErrorStr(vas));
  1089. }
  1090. av_free(mapping);
  1091. }
  1092. static int vaapi_map_to_drm_abh(AVHWFramesContext *hwfc, AVFrame *dst,
  1093. const AVFrame *src, int flags)
  1094. {
  1095. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  1096. VAAPIDRMImageBufferMapping *mapping = NULL;
  1097. VASurfaceID surface_id;
  1098. VAStatus vas;
  1099. int err, i, p;
  1100. surface_id = (VASurfaceID)(uintptr_t)src->data[3];
  1101. av_log(hwfc, AV_LOG_DEBUG, "Map VAAPI surface %#x to DRM.\n",
  1102. surface_id);
  1103. mapping = av_mallocz(sizeof(*mapping));
  1104. if (!mapping)
  1105. return AVERROR(ENOMEM);
  1106. vas = vaDeriveImage(hwctx->display, surface_id,
  1107. &mapping->image);
  1108. if (vas != VA_STATUS_SUCCESS) {
  1109. av_log(hwfc, AV_LOG_ERROR, "Failed to derive image from "
  1110. "surface %#x: %d (%s).\n",
  1111. surface_id, vas, vaErrorStr(vas));
  1112. err = AVERROR(EIO);
  1113. goto fail;
  1114. }
  1115. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_drm_format_map); i++) {
  1116. if (vaapi_drm_format_map[i].va_fourcc ==
  1117. mapping->image.format.fourcc)
  1118. break;
  1119. }
  1120. if (i >= FF_ARRAY_ELEMS(vaapi_drm_format_map)) {
  1121. av_log(hwfc, AV_LOG_ERROR, "No matching DRM format for "
  1122. "VAAPI format %#x.\n", mapping->image.format.fourcc);
  1123. err = AVERROR(EINVAL);
  1124. goto fail_derived;
  1125. }
  1126. mapping->buffer_info.mem_type =
  1127. VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
  1128. mapping->drm_desc.nb_layers =
  1129. vaapi_drm_format_map[i].nb_layer_formats;
  1130. if (mapping->drm_desc.nb_layers > 1) {
  1131. if (mapping->drm_desc.nb_layers != mapping->image.num_planes) {
  1132. av_log(hwfc, AV_LOG_ERROR, "Image properties do not match "
  1133. "expected format: got %d planes, but expected %d.\n",
  1134. mapping->image.num_planes, mapping->drm_desc.nb_layers);
  1135. err = AVERROR(EINVAL);
  1136. goto fail_derived;
  1137. }
  1138. for(p = 0; p < mapping->drm_desc.nb_layers; p++) {
  1139. mapping->drm_desc.layers[p] = (AVDRMLayerDescriptor) {
  1140. .format = vaapi_drm_format_map[i].layer_formats[p],
  1141. .nb_planes = 1,
  1142. .planes[0] = {
  1143. .object_index = 0,
  1144. .offset = mapping->image.offsets[p],
  1145. .pitch = mapping->image.pitches[p],
  1146. },
  1147. };
  1148. }
  1149. } else {
  1150. mapping->drm_desc.layers[0].format =
  1151. vaapi_drm_format_map[i].layer_formats[0];
  1152. mapping->drm_desc.layers[0].nb_planes = mapping->image.num_planes;
  1153. for (p = 0; p < mapping->image.num_planes; p++) {
  1154. mapping->drm_desc.layers[0].planes[p] = (AVDRMPlaneDescriptor) {
  1155. .object_index = 0,
  1156. .offset = mapping->image.offsets[p],
  1157. .pitch = mapping->image.pitches[p],
  1158. };
  1159. }
  1160. }
  1161. vas = vaAcquireBufferHandle(hwctx->display, mapping->image.buf,
  1162. &mapping->buffer_info);
  1163. if (vas != VA_STATUS_SUCCESS) {
  1164. av_log(hwfc, AV_LOG_ERROR, "Failed to get buffer "
  1165. "handle from image %#x (derived from surface %#x): "
  1166. "%d (%s).\n", mapping->image.buf, surface_id,
  1167. vas, vaErrorStr(vas));
  1168. err = AVERROR(EIO);
  1169. goto fail_derived;
  1170. }
  1171. av_log(hwfc, AV_LOG_DEBUG, "DRM PRIME fd is %ld.\n",
  1172. mapping->buffer_info.handle);
  1173. mapping->drm_desc.nb_objects = 1;
  1174. mapping->drm_desc.objects[0] = (AVDRMObjectDescriptor) {
  1175. .fd = mapping->buffer_info.handle,
  1176. .size = mapping->image.data_size,
  1177. // There is no way to get the format modifier with this API.
  1178. .format_modifier = DRM_FORMAT_MOD_INVALID,
  1179. };
  1180. err = ff_hwframe_map_create(src->hw_frames_ctx,
  1181. dst, src, &vaapi_unmap_to_drm_abh,
  1182. mapping);
  1183. if (err < 0)
  1184. goto fail_mapped;
  1185. dst->data[0] = (uint8_t*)&mapping->drm_desc;
  1186. dst->width = src->width;
  1187. dst->height = src->height;
  1188. return 0;
  1189. fail_mapped:
  1190. vaReleaseBufferHandle(hwctx->display, mapping->image.buf);
  1191. fail_derived:
  1192. vaDestroyImage(hwctx->display, mapping->image.image_id);
  1193. fail:
  1194. av_freep(&mapping);
  1195. return err;
  1196. }
  1197. #endif
  1198. static int vaapi_map_to_drm(AVHWFramesContext *hwfc, AVFrame *dst,
  1199. const AVFrame *src, int flags)
  1200. {
  1201. #if VA_CHECK_VERSION(1, 1, 0)
  1202. int err;
  1203. err = vaapi_map_to_drm_esh(hwfc, dst, src, flags);
  1204. if (err != AVERROR(ENOSYS))
  1205. return err;
  1206. #endif
  1207. #if VA_CHECK_VERSION(0, 36, 0)
  1208. return vaapi_map_to_drm_abh(hwfc, dst, src, flags);
  1209. #endif
  1210. return AVERROR(ENOSYS);
  1211. }
  1212. #endif /* CONFIG_LIBDRM */
  1213. static int vaapi_map_to(AVHWFramesContext *hwfc, AVFrame *dst,
  1214. const AVFrame *src, int flags)
  1215. {
  1216. switch (src->format) {
  1217. #if CONFIG_LIBDRM
  1218. case AV_PIX_FMT_DRM_PRIME:
  1219. return vaapi_map_from_drm(hwfc, dst, src, flags);
  1220. #endif
  1221. default:
  1222. return AVERROR(ENOSYS);
  1223. }
  1224. }
  1225. static int vaapi_map_from(AVHWFramesContext *hwfc, AVFrame *dst,
  1226. const AVFrame *src, int flags)
  1227. {
  1228. switch (dst->format) {
  1229. #if CONFIG_LIBDRM
  1230. case AV_PIX_FMT_DRM_PRIME:
  1231. return vaapi_map_to_drm(hwfc, dst, src, flags);
  1232. #endif
  1233. default:
  1234. return vaapi_map_to_memory(hwfc, dst, src, flags);
  1235. }
  1236. }
  1237. static void vaapi_device_free(AVHWDeviceContext *ctx)
  1238. {
  1239. AVVAAPIDeviceContext *hwctx = ctx->hwctx;
  1240. VAAPIDevicePriv *priv = ctx->user_opaque;
  1241. if (hwctx->display)
  1242. vaTerminate(hwctx->display);
  1243. #if HAVE_VAAPI_X11
  1244. if (priv->x11_display)
  1245. XCloseDisplay(priv->x11_display);
  1246. #endif
  1247. if (priv->drm_fd >= 0)
  1248. close(priv->drm_fd);
  1249. av_freep(&priv);
  1250. }
  1251. #if CONFIG_VAAPI_1
  1252. static void vaapi_device_log_error(void *context, const char *message)
  1253. {
  1254. AVHWDeviceContext *ctx = context;
  1255. av_log(ctx, AV_LOG_ERROR, "libva: %s", message);
  1256. }
  1257. static void vaapi_device_log_info(void *context, const char *message)
  1258. {
  1259. AVHWDeviceContext *ctx = context;
  1260. av_log(ctx, AV_LOG_VERBOSE, "libva: %s", message);
  1261. }
  1262. #endif
  1263. static int vaapi_device_connect(AVHWDeviceContext *ctx,
  1264. VADisplay display)
  1265. {
  1266. AVVAAPIDeviceContext *hwctx = ctx->hwctx;
  1267. int major, minor;
  1268. VAStatus vas;
  1269. #if CONFIG_VAAPI_1
  1270. vaSetErrorCallback(display, &vaapi_device_log_error, ctx);
  1271. vaSetInfoCallback (display, &vaapi_device_log_info, ctx);
  1272. #endif
  1273. hwctx->display = display;
  1274. vas = vaInitialize(display, &major, &minor);
  1275. if (vas != VA_STATUS_SUCCESS) {
  1276. av_log(ctx, AV_LOG_ERROR, "Failed to initialise VAAPI "
  1277. "connection: %d (%s).\n", vas, vaErrorStr(vas));
  1278. return AVERROR(EIO);
  1279. }
  1280. av_log(ctx, AV_LOG_VERBOSE, "Initialised VAAPI connection: "
  1281. "version %d.%d\n", major, minor);
  1282. return 0;
  1283. }
  1284. static int vaapi_device_create(AVHWDeviceContext *ctx, const char *device,
  1285. AVDictionary *opts, int flags)
  1286. {
  1287. VAAPIDevicePriv *priv;
  1288. VADisplay display = NULL;
  1289. const AVDictionaryEntry *ent;
  1290. int try_drm, try_x11, try_all;
  1291. priv = av_mallocz(sizeof(*priv));
  1292. if (!priv)
  1293. return AVERROR(ENOMEM);
  1294. priv->drm_fd = -1;
  1295. ctx->user_opaque = priv;
  1296. ctx->free = vaapi_device_free;
  1297. ent = av_dict_get(opts, "connection_type", NULL, 0);
  1298. if (ent) {
  1299. try_all = try_drm = try_x11 = 0;
  1300. if (!strcmp(ent->value, "drm")) {
  1301. try_drm = 1;
  1302. } else if (!strcmp(ent->value, "x11")) {
  1303. try_x11 = 1;
  1304. } else {
  1305. av_log(ctx, AV_LOG_ERROR, "Invalid connection type %s.\n",
  1306. ent->value);
  1307. return AVERROR(EINVAL);
  1308. }
  1309. } else {
  1310. try_all = 1;
  1311. try_drm = HAVE_VAAPI_DRM;
  1312. try_x11 = HAVE_VAAPI_X11;
  1313. }
  1314. #if HAVE_VAAPI_DRM
  1315. while (!display && try_drm) {
  1316. // If the device is specified, try to open it as a DRM device node.
  1317. // If not, look for a usable render node, possibly restricted to those
  1318. // using a specified kernel driver.
  1319. int loglevel = try_all ? AV_LOG_VERBOSE : AV_LOG_ERROR;
  1320. if (device) {
  1321. priv->drm_fd = open(device, O_RDWR);
  1322. if (priv->drm_fd < 0) {
  1323. av_log(ctx, loglevel, "Failed to open %s as "
  1324. "DRM device node.\n", device);
  1325. break;
  1326. }
  1327. } else {
  1328. char path[64];
  1329. int n, max_devices = 8;
  1330. #if CONFIG_LIBDRM
  1331. const AVDictionaryEntry *kernel_driver;
  1332. kernel_driver = av_dict_get(opts, "kernel_driver", NULL, 0);
  1333. #endif
  1334. for (n = 0; n < max_devices; n++) {
  1335. snprintf(path, sizeof(path),
  1336. "/dev/dri/renderD%d", 128 + n);
  1337. priv->drm_fd = open(path, O_RDWR);
  1338. if (priv->drm_fd < 0) {
  1339. av_log(ctx, AV_LOG_VERBOSE, "Cannot open "
  1340. "DRM render node for device %d.\n", n);
  1341. break;
  1342. }
  1343. #if CONFIG_LIBDRM
  1344. if (kernel_driver) {
  1345. drmVersion *info;
  1346. info = drmGetVersion(priv->drm_fd);
  1347. if (strcmp(kernel_driver->value, info->name)) {
  1348. av_log(ctx, AV_LOG_VERBOSE, "Ignoring device %d "
  1349. "with non-matching kernel driver (%s).\n",
  1350. n, info->name);
  1351. drmFreeVersion(info);
  1352. close(priv->drm_fd);
  1353. priv->drm_fd = -1;
  1354. continue;
  1355. }
  1356. av_log(ctx, AV_LOG_VERBOSE, "Trying to use "
  1357. "DRM render node for device %d, "
  1358. "with matching kernel driver (%s).\n",
  1359. n, info->name);
  1360. drmFreeVersion(info);
  1361. } else
  1362. #endif
  1363. {
  1364. av_log(ctx, AV_LOG_VERBOSE, "Trying to use "
  1365. "DRM render node for device %d.\n", n);
  1366. }
  1367. break;
  1368. }
  1369. if (n >= max_devices)
  1370. break;
  1371. }
  1372. display = vaGetDisplayDRM(priv->drm_fd);
  1373. if (!display) {
  1374. av_log(ctx, AV_LOG_VERBOSE, "Cannot open a VA display "
  1375. "from DRM device %s.\n", device);
  1376. return AVERROR_EXTERNAL;
  1377. }
  1378. break;
  1379. }
  1380. #endif
  1381. #if HAVE_VAAPI_X11
  1382. if (!display && try_x11) {
  1383. // Try to open the device as an X11 display.
  1384. priv->x11_display = XOpenDisplay(device);
  1385. if (!priv->x11_display) {
  1386. av_log(ctx, AV_LOG_VERBOSE, "Cannot open X11 display "
  1387. "%s.\n", XDisplayName(device));
  1388. } else {
  1389. display = vaGetDisplay(priv->x11_display);
  1390. if (!display) {
  1391. av_log(ctx, AV_LOG_ERROR, "Cannot open a VA display "
  1392. "from X11 display %s.\n", XDisplayName(device));
  1393. return AVERROR_UNKNOWN;
  1394. }
  1395. av_log(ctx, AV_LOG_VERBOSE, "Opened VA display via "
  1396. "X11 display %s.\n", XDisplayName(device));
  1397. }
  1398. }
  1399. #endif
  1400. if (!display) {
  1401. if (device)
  1402. av_log(ctx, AV_LOG_ERROR, "No VA display found for "
  1403. "device %s.\n", device);
  1404. else
  1405. av_log(ctx, AV_LOG_ERROR, "No VA display found for "
  1406. "any default device.\n");
  1407. return AVERROR(EINVAL);
  1408. }
  1409. ent = av_dict_get(opts, "driver", NULL, 0);
  1410. if (ent) {
  1411. #if VA_CHECK_VERSION(0, 38, 0)
  1412. VAStatus vas;
  1413. vas = vaSetDriverName(display, ent->value);
  1414. if (vas != VA_STATUS_SUCCESS) {
  1415. av_log(ctx, AV_LOG_ERROR, "Failed to set driver name to "
  1416. "%s: %d (%s).\n", ent->value, vas, vaErrorStr(vas));
  1417. vaTerminate(display);
  1418. return AVERROR_EXTERNAL;
  1419. }
  1420. #else
  1421. av_log(ctx, AV_LOG_WARNING, "Driver name setting is not "
  1422. "supported with this VAAPI version.\n");
  1423. #endif
  1424. }
  1425. return vaapi_device_connect(ctx, display);
  1426. }
  1427. static int vaapi_device_derive(AVHWDeviceContext *ctx,
  1428. AVHWDeviceContext *src_ctx, int flags)
  1429. {
  1430. #if HAVE_VAAPI_DRM
  1431. if (src_ctx->type == AV_HWDEVICE_TYPE_DRM) {
  1432. AVDRMDeviceContext *src_hwctx = src_ctx->hwctx;
  1433. VADisplay *display;
  1434. VAAPIDevicePriv *priv;
  1435. if (src_hwctx->fd < 0) {
  1436. av_log(ctx, AV_LOG_ERROR, "DRM instance requires an associated "
  1437. "device to derive a VA display from.\n");
  1438. return AVERROR(EINVAL);
  1439. }
  1440. priv = av_mallocz(sizeof(*priv));
  1441. if (!priv)
  1442. return AVERROR(ENOMEM);
  1443. // Inherits the fd from the source context, which will close it.
  1444. priv->drm_fd = -1;
  1445. ctx->user_opaque = priv;
  1446. ctx->free = &vaapi_device_free;
  1447. display = vaGetDisplayDRM(src_hwctx->fd);
  1448. if (!display) {
  1449. av_log(ctx, AV_LOG_ERROR, "Failed to open a VA display from "
  1450. "DRM device.\n");
  1451. return AVERROR(EIO);
  1452. }
  1453. return vaapi_device_connect(ctx, display);
  1454. }
  1455. #endif
  1456. return AVERROR(ENOSYS);
  1457. }
  1458. const HWContextType ff_hwcontext_type_vaapi = {
  1459. .type = AV_HWDEVICE_TYPE_VAAPI,
  1460. .name = "VAAPI",
  1461. .device_hwctx_size = sizeof(AVVAAPIDeviceContext),
  1462. .device_priv_size = sizeof(VAAPIDeviceContext),
  1463. .device_hwconfig_size = sizeof(AVVAAPIHWConfig),
  1464. .frames_hwctx_size = sizeof(AVVAAPIFramesContext),
  1465. .frames_priv_size = sizeof(VAAPIFramesContext),
  1466. .device_create = &vaapi_device_create,
  1467. .device_derive = &vaapi_device_derive,
  1468. .device_init = &vaapi_device_init,
  1469. .device_uninit = &vaapi_device_uninit,
  1470. .frames_get_constraints = &vaapi_frames_get_constraints,
  1471. .frames_init = &vaapi_frames_init,
  1472. .frames_uninit = &vaapi_frames_uninit,
  1473. .frames_get_buffer = &vaapi_get_buffer,
  1474. .transfer_get_formats = &vaapi_transfer_get_formats,
  1475. .transfer_data_to = &vaapi_transfer_data_to,
  1476. .transfer_data_from = &vaapi_transfer_data_from,
  1477. .map_to = &vaapi_map_to,
  1478. .map_from = &vaapi_map_from,
  1479. .pix_fmts = (const enum AVPixelFormat[]) {
  1480. AV_PIX_FMT_VAAPI,
  1481. AV_PIX_FMT_NONE
  1482. },
  1483. };