hwcontext_vaapi.c 51 KB

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