You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

942 lines
30KB

  1. /*
  2. * This file is part of Libav.
  3. *
  4. * Libav 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. * Libav 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 Libav; 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. #include <fcntl.h>
  26. #if HAVE_UNISTD_H
  27. # include <unistd.h>
  28. #endif
  29. #include "avassert.h"
  30. #include "buffer.h"
  31. #include "common.h"
  32. #include "hwcontext.h"
  33. #include "hwcontext_internal.h"
  34. #include "hwcontext_vaapi.h"
  35. #include "mem.h"
  36. #include "pixdesc.h"
  37. #include "pixfmt.h"
  38. typedef struct VAAPIDevicePriv {
  39. #if HAVE_VAAPI_X11
  40. Display *x11_display;
  41. #endif
  42. int drm_fd;
  43. } VAAPIDevicePriv;
  44. typedef struct VAAPISurfaceFormat {
  45. enum AVPixelFormat pix_fmt;
  46. VAImageFormat image_format;
  47. } VAAPISurfaceFormat;
  48. typedef struct VAAPIDeviceContext {
  49. // Surface formats which can be used with this device.
  50. VAAPISurfaceFormat *formats;
  51. int nb_formats;
  52. } VAAPIDeviceContext;
  53. typedef struct VAAPIFramesContext {
  54. // Surface attributes set at create time.
  55. VASurfaceAttrib *attributes;
  56. int nb_attributes;
  57. // RT format of the underlying surface (Intel driver ignores this anyway).
  58. unsigned int rt_format;
  59. // Whether vaDeriveImage works.
  60. int derive_works;
  61. } VAAPIFramesContext;
  62. enum {
  63. VAAPI_MAP_READ = 0x01,
  64. VAAPI_MAP_WRITE = 0x02,
  65. VAAPI_MAP_DIRECT = 0x04,
  66. };
  67. typedef struct VAAPISurfaceMap {
  68. // The source hardware frame of this mapping (with hw_frames_ctx set).
  69. const AVFrame *source;
  70. // VAAPI_MAP_* flags which apply to this mapping.
  71. int flags;
  72. // Handle to the derived or copied image which is mapped.
  73. VAImage image;
  74. } VAAPISurfaceMap;
  75. #define MAP(va, rt, av) { \
  76. VA_FOURCC_ ## va, \
  77. VA_RT_FORMAT_ ## rt, \
  78. AV_PIX_FMT_ ## av \
  79. }
  80. // The map fourcc <-> pix_fmt isn't bijective because of the annoying U/V
  81. // plane swap cases. The frame handling below tries to hide these.
  82. static struct {
  83. unsigned int fourcc;
  84. unsigned int rt_format;
  85. enum AVPixelFormat pix_fmt;
  86. } vaapi_format_map[] = {
  87. MAP(NV12, YUV420, NV12),
  88. MAP(YV12, YUV420, YUV420P), // With U/V planes swapped.
  89. MAP(IYUV, YUV420, YUV420P),
  90. //MAP(I420, YUV420, YUV420P), // Not in libva but used by Intel driver.
  91. #ifdef VA_FOURCC_YV16
  92. MAP(YV16, YUV422, YUV422P), // With U/V planes swapped.
  93. #endif
  94. MAP(422H, YUV422, YUV422P),
  95. MAP(UYVY, YUV422, UYVY422),
  96. MAP(YUY2, YUV422, YUYV422),
  97. MAP(Y800, YUV400, GRAY8),
  98. #ifdef VA_FOURCC_P010
  99. //MAP(P010, YUV420_10BPP, P010),
  100. #endif
  101. MAP(BGRA, RGB32, BGRA),
  102. //MAP(BGRX, RGB32, BGR0),
  103. MAP(RGBA, RGB32, RGBA),
  104. //MAP(RGBX, RGB32, RGB0),
  105. MAP(ABGR, RGB32, ABGR),
  106. //MAP(XBGR, RGB32, 0BGR),
  107. MAP(ARGB, RGB32, ARGB),
  108. //MAP(XRGB, RGB32, 0RGB),
  109. };
  110. #undef MAP
  111. static enum AVPixelFormat vaapi_pix_fmt_from_fourcc(unsigned int fourcc)
  112. {
  113. int i;
  114. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++)
  115. if (vaapi_format_map[i].fourcc == fourcc)
  116. return vaapi_format_map[i].pix_fmt;
  117. return AV_PIX_FMT_NONE;
  118. }
  119. static int vaapi_get_image_format(AVHWDeviceContext *hwdev,
  120. enum AVPixelFormat pix_fmt,
  121. VAImageFormat **image_format)
  122. {
  123. VAAPIDeviceContext *ctx = hwdev->internal->priv;
  124. int i;
  125. for (i = 0; i < ctx->nb_formats; i++) {
  126. if (ctx->formats[i].pix_fmt == pix_fmt) {
  127. *image_format = &ctx->formats[i].image_format;
  128. return 0;
  129. }
  130. }
  131. return AVERROR(EINVAL);
  132. }
  133. static int vaapi_frames_get_constraints(AVHWDeviceContext *hwdev,
  134. const void *hwconfig,
  135. AVHWFramesConstraints *constraints)
  136. {
  137. AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
  138. const AVVAAPIHWConfig *config = hwconfig;
  139. VAAPIDeviceContext *ctx = hwdev->internal->priv;
  140. VASurfaceAttrib *attr_list = NULL;
  141. VAStatus vas;
  142. enum AVPixelFormat pix_fmt;
  143. unsigned int fourcc;
  144. int err, i, j, attr_count, pix_fmt_count;
  145. if (config) {
  146. attr_count = 0;
  147. vas = vaQuerySurfaceAttributes(hwctx->display, config->config_id,
  148. 0, &attr_count);
  149. if (vas != VA_STATUS_SUCCESS) {
  150. av_log(hwdev, AV_LOG_ERROR, "Failed to query surface attributes: "
  151. "%d (%s).\n", vas, vaErrorStr(vas));
  152. err = AVERROR(ENOSYS);
  153. goto fail;
  154. }
  155. attr_list = av_malloc(attr_count * sizeof(*attr_list));
  156. if (!attr_list) {
  157. err = AVERROR(ENOMEM);
  158. goto fail;
  159. }
  160. vas = vaQuerySurfaceAttributes(hwctx->display, config->config_id,
  161. attr_list, &attr_count);
  162. if (vas != VA_STATUS_SUCCESS) {
  163. av_log(hwdev, AV_LOG_ERROR, "Failed to query surface attributes: "
  164. "%d (%s).\n", vas, vaErrorStr(vas));
  165. err = AVERROR(ENOSYS);
  166. goto fail;
  167. }
  168. pix_fmt_count = 0;
  169. for (i = 0; i < attr_count; i++) {
  170. switch (attr_list[i].type) {
  171. case VASurfaceAttribPixelFormat:
  172. fourcc = attr_list[i].value.value.i;
  173. pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
  174. if (pix_fmt != AV_PIX_FMT_NONE) {
  175. ++pix_fmt_count;
  176. } else {
  177. // Something unsupported - ignore.
  178. }
  179. break;
  180. case VASurfaceAttribMinWidth:
  181. constraints->min_width = attr_list[i].value.value.i;
  182. break;
  183. case VASurfaceAttribMinHeight:
  184. constraints->min_height = attr_list[i].value.value.i;
  185. break;
  186. case VASurfaceAttribMaxWidth:
  187. constraints->max_width = attr_list[i].value.value.i;
  188. break;
  189. case VASurfaceAttribMaxHeight:
  190. constraints->max_height = attr_list[i].value.value.i;
  191. break;
  192. }
  193. }
  194. if (pix_fmt_count == 0) {
  195. // Nothing usable found. Presumably there exists something which
  196. // works, so leave the set null to indicate unknown.
  197. constraints->valid_sw_formats = NULL;
  198. } else {
  199. constraints->valid_sw_formats = av_malloc_array(pix_fmt_count + 1,
  200. sizeof(pix_fmt));
  201. if (!constraints->valid_sw_formats) {
  202. err = AVERROR(ENOMEM);
  203. goto fail;
  204. }
  205. for (i = j = 0; i < attr_count; i++) {
  206. if (attr_list[i].type != VASurfaceAttribPixelFormat)
  207. continue;
  208. fourcc = attr_list[i].value.value.i;
  209. pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
  210. if (pix_fmt != AV_PIX_FMT_NONE)
  211. constraints->valid_sw_formats[j++] = pix_fmt;
  212. }
  213. av_assert0(j == pix_fmt_count);
  214. constraints->valid_sw_formats[j] = AV_PIX_FMT_NONE;
  215. }
  216. } else {
  217. // No configuration supplied.
  218. // Return the full set of image formats known by the implementation.
  219. constraints->valid_sw_formats = av_malloc_array(ctx->nb_formats + 1,
  220. sizeof(pix_fmt));
  221. if (!constraints->valid_sw_formats) {
  222. err = AVERROR(ENOMEM);
  223. goto fail;
  224. }
  225. for (i = 0; i < ctx->nb_formats; i++)
  226. constraints->valid_sw_formats[i] = ctx->formats[i].pix_fmt;
  227. constraints->valid_sw_formats[i] = AV_PIX_FMT_NONE;
  228. }
  229. constraints->valid_hw_formats = av_malloc_array(2, sizeof(pix_fmt));
  230. if (!constraints->valid_hw_formats) {
  231. err = AVERROR(ENOMEM);
  232. goto fail;
  233. }
  234. constraints->valid_hw_formats[0] = AV_PIX_FMT_VAAPI;
  235. constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
  236. err = 0;
  237. fail:
  238. av_freep(&attr_list);
  239. return err;
  240. }
  241. static int vaapi_device_init(AVHWDeviceContext *hwdev)
  242. {
  243. VAAPIDeviceContext *ctx = hwdev->internal->priv;
  244. AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
  245. VAImageFormat *image_list = NULL;
  246. VAStatus vas;
  247. int err, i, image_count;
  248. enum AVPixelFormat pix_fmt;
  249. unsigned int fourcc;
  250. image_count = vaMaxNumImageFormats(hwctx->display);
  251. if (image_count <= 0) {
  252. err = AVERROR(EIO);
  253. goto fail;
  254. }
  255. image_list = av_malloc(image_count * sizeof(*image_list));
  256. if (!image_list) {
  257. err = AVERROR(ENOMEM);
  258. goto fail;
  259. }
  260. vas = vaQueryImageFormats(hwctx->display, image_list, &image_count);
  261. if (vas != VA_STATUS_SUCCESS) {
  262. err = AVERROR(EIO);
  263. goto fail;
  264. }
  265. ctx->formats = av_malloc(image_count * sizeof(*ctx->formats));
  266. if (!ctx->formats) {
  267. err = AVERROR(ENOMEM);
  268. goto fail;
  269. }
  270. ctx->nb_formats = 0;
  271. for (i = 0; i < image_count; i++) {
  272. fourcc = image_list[i].fourcc;
  273. pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
  274. if (pix_fmt == AV_PIX_FMT_NONE) {
  275. av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> unknown.\n",
  276. fourcc);
  277. } else {
  278. av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> %s.\n",
  279. fourcc, av_get_pix_fmt_name(pix_fmt));
  280. ctx->formats[ctx->nb_formats].pix_fmt = pix_fmt;
  281. ctx->formats[ctx->nb_formats].image_format = image_list[i];
  282. ++ctx->nb_formats;
  283. }
  284. }
  285. av_free(image_list);
  286. return 0;
  287. fail:
  288. av_freep(&ctx->formats);
  289. av_free(image_list);
  290. return err;
  291. }
  292. static void vaapi_device_uninit(AVHWDeviceContext *hwdev)
  293. {
  294. VAAPIDeviceContext *ctx = hwdev->internal->priv;
  295. av_freep(&ctx->formats);
  296. }
  297. static void vaapi_buffer_free(void *opaque, uint8_t *data)
  298. {
  299. AVHWFramesContext *hwfc = opaque;
  300. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  301. VASurfaceID surface_id;
  302. VAStatus vas;
  303. surface_id = (VASurfaceID)(uintptr_t)data;
  304. vas = vaDestroySurfaces(hwctx->display, &surface_id, 1);
  305. if (vas != VA_STATUS_SUCCESS) {
  306. av_log(hwfc, AV_LOG_ERROR, "Failed to destroy surface %#x: "
  307. "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
  308. }
  309. }
  310. static AVBufferRef *vaapi_pool_alloc(void *opaque, int size)
  311. {
  312. AVHWFramesContext *hwfc = opaque;
  313. VAAPIFramesContext *ctx = hwfc->internal->priv;
  314. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  315. AVVAAPIFramesContext *avfc = hwfc->hwctx;
  316. VASurfaceID surface_id;
  317. VAStatus vas;
  318. AVBufferRef *ref;
  319. vas = vaCreateSurfaces(hwctx->display, ctx->rt_format,
  320. hwfc->width, hwfc->height,
  321. &surface_id, 1,
  322. ctx->attributes, ctx->nb_attributes);
  323. if (vas != VA_STATUS_SUCCESS) {
  324. av_log(hwfc, AV_LOG_ERROR, "Failed to create surface: "
  325. "%d (%s).\n", vas, vaErrorStr(vas));
  326. return NULL;
  327. }
  328. av_log(hwfc, AV_LOG_DEBUG, "Created surface %#x.\n", surface_id);
  329. ref = av_buffer_create((uint8_t*)(uintptr_t)surface_id,
  330. sizeof(surface_id), &vaapi_buffer_free,
  331. hwfc, AV_BUFFER_FLAG_READONLY);
  332. if (!ref) {
  333. vaDestroySurfaces(hwctx->display, &surface_id, 1);
  334. return NULL;
  335. }
  336. if (hwfc->initial_pool_size > 0) {
  337. // This is a fixed-size pool, so we must still be in the initial
  338. // allocation sequence.
  339. av_assert0(avfc->nb_surfaces < hwfc->initial_pool_size);
  340. avfc->surface_ids[avfc->nb_surfaces] = surface_id;
  341. ++avfc->nb_surfaces;
  342. }
  343. return ref;
  344. }
  345. static int vaapi_frames_init(AVHWFramesContext *hwfc)
  346. {
  347. AVVAAPIFramesContext *avfc = hwfc->hwctx;
  348. VAAPIFramesContext *ctx = hwfc->internal->priv;
  349. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  350. VAImageFormat *expected_format;
  351. AVBufferRef *test_surface = NULL;
  352. VASurfaceID test_surface_id;
  353. VAImage test_image;
  354. VAStatus vas;
  355. int err, i;
  356. unsigned int fourcc, rt_format;
  357. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++) {
  358. if (vaapi_format_map[i].pix_fmt == hwfc->sw_format) {
  359. fourcc = vaapi_format_map[i].fourcc;
  360. rt_format = vaapi_format_map[i].rt_format;
  361. break;
  362. }
  363. }
  364. if (i >= FF_ARRAY_ELEMS(vaapi_format_map)) {
  365. av_log(hwfc, AV_LOG_ERROR, "Unsupported format: %s.\n",
  366. av_get_pix_fmt_name(hwfc->sw_format));
  367. return AVERROR(EINVAL);
  368. }
  369. if (!hwfc->pool) {
  370. int need_memory_type = 1, need_pixel_format = 1;
  371. for (i = 0; i < avfc->nb_attributes; i++) {
  372. if (ctx->attributes[i].type == VASurfaceAttribMemoryType)
  373. need_memory_type = 0;
  374. if (ctx->attributes[i].type == VASurfaceAttribPixelFormat)
  375. need_pixel_format = 0;
  376. }
  377. ctx->nb_attributes =
  378. avfc->nb_attributes + need_memory_type + need_pixel_format;
  379. ctx->attributes = av_malloc(ctx->nb_attributes *
  380. sizeof(*ctx->attributes));
  381. if (!ctx->attributes) {
  382. err = AVERROR(ENOMEM);
  383. goto fail;
  384. }
  385. for (i = 0; i < avfc->nb_attributes; i++)
  386. ctx->attributes[i] = avfc->attributes[i];
  387. if (need_memory_type) {
  388. ctx->attributes[i++] = (VASurfaceAttrib) {
  389. .type = VASurfaceAttribMemoryType,
  390. .flags = VA_SURFACE_ATTRIB_SETTABLE,
  391. .value.type = VAGenericValueTypeInteger,
  392. .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA,
  393. };
  394. }
  395. if (need_pixel_format) {
  396. ctx->attributes[i++] = (VASurfaceAttrib) {
  397. .type = VASurfaceAttribPixelFormat,
  398. .flags = VA_SURFACE_ATTRIB_SETTABLE,
  399. .value.type = VAGenericValueTypeInteger,
  400. .value.value.i = fourcc,
  401. };
  402. }
  403. av_assert0(i == ctx->nb_attributes);
  404. ctx->rt_format = rt_format;
  405. if (hwfc->initial_pool_size > 0) {
  406. // This pool will be usable as a render target, so we need to store
  407. // all of the surface IDs somewhere that vaCreateContext() calls
  408. // will be able to access them.
  409. avfc->nb_surfaces = 0;
  410. avfc->surface_ids = av_malloc(hwfc->initial_pool_size *
  411. sizeof(*avfc->surface_ids));
  412. if (!avfc->surface_ids) {
  413. err = AVERROR(ENOMEM);
  414. goto fail;
  415. }
  416. } else {
  417. // This pool allows dynamic sizing, and will not be usable as a
  418. // render target.
  419. avfc->nb_surfaces = 0;
  420. avfc->surface_ids = NULL;
  421. }
  422. hwfc->internal->pool_internal =
  423. av_buffer_pool_init2(sizeof(VASurfaceID), hwfc,
  424. &vaapi_pool_alloc, NULL);
  425. if (!hwfc->internal->pool_internal) {
  426. av_log(hwfc, AV_LOG_ERROR, "Failed to create VAAPI surface pool.\n");
  427. err = AVERROR(ENOMEM);
  428. goto fail;
  429. }
  430. }
  431. // Allocate a single surface to test whether vaDeriveImage() is going
  432. // to work for the specific configuration.
  433. if (hwfc->pool) {
  434. test_surface = av_buffer_pool_get(hwfc->pool);
  435. if (!test_surface) {
  436. av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
  437. "user-configured buffer pool.\n");
  438. err = AVERROR(ENOMEM);
  439. goto fail;
  440. }
  441. } else {
  442. test_surface = av_buffer_pool_get(hwfc->internal->pool_internal);
  443. if (!test_surface) {
  444. av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
  445. "internal buffer pool.\n");
  446. err = AVERROR(ENOMEM);
  447. goto fail;
  448. }
  449. }
  450. test_surface_id = (VASurfaceID)(uintptr_t)test_surface->data;
  451. ctx->derive_works = 0;
  452. err = vaapi_get_image_format(hwfc->device_ctx,
  453. hwfc->sw_format, &expected_format);
  454. if (err == 0) {
  455. vas = vaDeriveImage(hwctx->display, test_surface_id, &test_image);
  456. if (vas == VA_STATUS_SUCCESS) {
  457. if (expected_format->fourcc == test_image.format.fourcc) {
  458. av_log(hwfc, AV_LOG_DEBUG, "Direct mapping possible.\n");
  459. ctx->derive_works = 1;
  460. } else {
  461. av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
  462. "derived image format %08x does not match "
  463. "expected format %08x.\n",
  464. expected_format->fourcc, test_image.format.fourcc);
  465. }
  466. vaDestroyImage(hwctx->display, test_image.image_id);
  467. } else {
  468. av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
  469. "deriving image does not work: "
  470. "%d (%s).\n", vas, vaErrorStr(vas));
  471. }
  472. } else {
  473. av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
  474. "image format is not supported.\n");
  475. }
  476. av_buffer_unref(&test_surface);
  477. return 0;
  478. fail:
  479. av_buffer_unref(&test_surface);
  480. av_freep(&avfc->surface_ids);
  481. av_freep(&ctx->attributes);
  482. return err;
  483. }
  484. static void vaapi_frames_uninit(AVHWFramesContext *hwfc)
  485. {
  486. AVVAAPIFramesContext *avfc = hwfc->hwctx;
  487. VAAPIFramesContext *ctx = hwfc->internal->priv;
  488. av_freep(&avfc->surface_ids);
  489. av_freep(&ctx->attributes);
  490. }
  491. static int vaapi_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
  492. {
  493. frame->buf[0] = av_buffer_pool_get(hwfc->pool);
  494. if (!frame->buf[0])
  495. return AVERROR(ENOMEM);
  496. frame->data[3] = frame->buf[0]->data;
  497. frame->format = AV_PIX_FMT_VAAPI;
  498. frame->width = hwfc->width;
  499. frame->height = hwfc->height;
  500. return 0;
  501. }
  502. static int vaapi_transfer_get_formats(AVHWFramesContext *hwfc,
  503. enum AVHWFrameTransferDirection dir,
  504. enum AVPixelFormat **formats)
  505. {
  506. VAAPIDeviceContext *ctx = hwfc->device_ctx->internal->priv;
  507. enum AVPixelFormat *pix_fmts, preferred_format;
  508. int i, k;
  509. preferred_format = hwfc->sw_format;
  510. pix_fmts = av_malloc((ctx->nb_formats + 1) * sizeof(*pix_fmts));
  511. if (!pix_fmts)
  512. return AVERROR(ENOMEM);
  513. pix_fmts[0] = preferred_format;
  514. k = 1;
  515. for (i = 0; i < ctx->nb_formats; i++) {
  516. if (ctx->formats[i].pix_fmt == preferred_format)
  517. continue;
  518. av_assert0(k < ctx->nb_formats);
  519. pix_fmts[k++] = ctx->formats[i].pix_fmt;
  520. }
  521. av_assert0(k == ctx->nb_formats);
  522. pix_fmts[k] = AV_PIX_FMT_NONE;
  523. *formats = pix_fmts;
  524. return 0;
  525. }
  526. static void vaapi_unmap_frame(void *opaque, uint8_t *data)
  527. {
  528. AVHWFramesContext *hwfc = opaque;
  529. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  530. VAAPISurfaceMap *map = (VAAPISurfaceMap*)data;
  531. const AVFrame *src;
  532. VASurfaceID surface_id;
  533. VAStatus vas;
  534. src = map->source;
  535. surface_id = (VASurfaceID)(uintptr_t)src->data[3];
  536. av_log(hwfc, AV_LOG_DEBUG, "Unmap surface %#x.\n", surface_id);
  537. vas = vaUnmapBuffer(hwctx->display, map->image.buf);
  538. if (vas != VA_STATUS_SUCCESS) {
  539. av_log(hwfc, AV_LOG_ERROR, "Failed to unmap image from surface "
  540. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  541. }
  542. if ((map->flags & VAAPI_MAP_WRITE) &&
  543. !(map->flags & VAAPI_MAP_DIRECT)) {
  544. vas = vaPutImage(hwctx->display, surface_id, map->image.image_id,
  545. 0, 0, hwfc->width, hwfc->height,
  546. 0, 0, hwfc->width, hwfc->height);
  547. if (vas != VA_STATUS_SUCCESS) {
  548. av_log(hwfc, AV_LOG_ERROR, "Failed to write image to surface "
  549. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  550. }
  551. }
  552. vas = vaDestroyImage(hwctx->display, map->image.image_id);
  553. if (vas != VA_STATUS_SUCCESS) {
  554. av_log(hwfc, AV_LOG_ERROR, "Failed to destroy image from surface "
  555. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  556. }
  557. av_free(map);
  558. }
  559. static int vaapi_map_frame(AVHWFramesContext *hwfc,
  560. AVFrame *dst, const AVFrame *src, int flags)
  561. {
  562. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  563. VAAPIFramesContext *ctx = hwfc->internal->priv;
  564. VASurfaceID surface_id;
  565. VAImageFormat *image_format;
  566. VAAPISurfaceMap *map;
  567. VAStatus vas;
  568. void *address = NULL;
  569. int err, i;
  570. surface_id = (VASurfaceID)(uintptr_t)src->data[3];
  571. av_log(hwfc, AV_LOG_DEBUG, "Map surface %#x.\n", surface_id);
  572. if (!ctx->derive_works && (flags & VAAPI_MAP_DIRECT)) {
  573. // Requested direct mapping but it is not possible.
  574. return AVERROR(EINVAL);
  575. }
  576. if (dst->format == AV_PIX_FMT_NONE)
  577. dst->format = hwfc->sw_format;
  578. if (dst->format != hwfc->sw_format && (flags & VAAPI_MAP_DIRECT)) {
  579. // Requested direct mapping but the formats do not match.
  580. return AVERROR(EINVAL);
  581. }
  582. err = vaapi_get_image_format(hwfc->device_ctx, dst->format, &image_format);
  583. if (err < 0) {
  584. // Requested format is not a valid output format.
  585. return AVERROR(EINVAL);
  586. }
  587. map = av_malloc(sizeof(VAAPISurfaceMap));
  588. if (!map)
  589. return AVERROR(ENOMEM);
  590. map->source = src;
  591. map->flags = flags;
  592. map->image.image_id = VA_INVALID_ID;
  593. vas = vaSyncSurface(hwctx->display, surface_id);
  594. if (vas != VA_STATUS_SUCCESS) {
  595. av_log(hwfc, AV_LOG_ERROR, "Failed to sync surface "
  596. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  597. err = AVERROR(EIO);
  598. goto fail;
  599. }
  600. // The memory which we map using derive need not be connected to the CPU
  601. // in a way conducive to fast access. On Gen7-Gen9 Intel graphics, the
  602. // memory is mappable but not cached, so normal memcpy()-like access is
  603. // very slow to read it (but writing is ok). It is possible to read much
  604. // faster with a copy routine which is aware of the limitation, but we
  605. // assume for now that the user is not aware of that and would therefore
  606. // prefer not to be given direct-mapped memory if they request read access.
  607. if (ctx->derive_works &&
  608. ((flags & VAAPI_MAP_DIRECT) || !(flags & VAAPI_MAP_READ))) {
  609. vas = vaDeriveImage(hwctx->display, surface_id, &map->image);
  610. if (vas != VA_STATUS_SUCCESS) {
  611. av_log(hwfc, AV_LOG_ERROR, "Failed to derive image from "
  612. "surface %#x: %d (%s).\n",
  613. surface_id, vas, vaErrorStr(vas));
  614. err = AVERROR(EIO);
  615. goto fail;
  616. }
  617. if (map->image.format.fourcc != image_format->fourcc) {
  618. av_log(hwfc, AV_LOG_ERROR, "Derive image of surface %#x "
  619. "is in wrong format: expected %#08x, got %#08x.\n",
  620. surface_id, image_format->fourcc, map->image.format.fourcc);
  621. err = AVERROR(EIO);
  622. goto fail;
  623. }
  624. map->flags |= VAAPI_MAP_DIRECT;
  625. } else {
  626. vas = vaCreateImage(hwctx->display, image_format,
  627. hwfc->width, hwfc->height, &map->image);
  628. if (vas != VA_STATUS_SUCCESS) {
  629. av_log(hwfc, AV_LOG_ERROR, "Failed to create image for "
  630. "surface %#x: %d (%s).\n",
  631. surface_id, vas, vaErrorStr(vas));
  632. err = AVERROR(EIO);
  633. goto fail;
  634. }
  635. if (flags & VAAPI_MAP_READ) {
  636. vas = vaGetImage(hwctx->display, surface_id, 0, 0,
  637. hwfc->width, hwfc->height, map->image.image_id);
  638. if (vas != VA_STATUS_SUCCESS) {
  639. av_log(hwfc, AV_LOG_ERROR, "Failed to read image from "
  640. "surface %#x: %d (%s).\n",
  641. surface_id, vas, vaErrorStr(vas));
  642. err = AVERROR(EIO);
  643. goto fail;
  644. }
  645. }
  646. }
  647. vas = vaMapBuffer(hwctx->display, map->image.buf, &address);
  648. if (vas != VA_STATUS_SUCCESS) {
  649. av_log(hwfc, AV_LOG_ERROR, "Failed to map image from surface "
  650. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  651. err = AVERROR(EIO);
  652. goto fail;
  653. }
  654. dst->width = src->width;
  655. dst->height = src->height;
  656. for (i = 0; i < map->image.num_planes; i++) {
  657. dst->data[i] = (uint8_t*)address + map->image.offsets[i];
  658. dst->linesize[i] = map->image.pitches[i];
  659. }
  660. if (
  661. #ifdef VA_FOURCC_YV16
  662. map->image.format.fourcc == VA_FOURCC_YV16 ||
  663. #endif
  664. map->image.format.fourcc == VA_FOURCC_YV12) {
  665. // Chroma planes are YVU rather than YUV, so swap them.
  666. FFSWAP(uint8_t*, dst->data[1], dst->data[2]);
  667. }
  668. dst->buf[0] = av_buffer_create((uint8_t*)map, sizeof(*map),
  669. &vaapi_unmap_frame, hwfc, 0);
  670. if (!dst->buf[0]) {
  671. err = AVERROR(ENOMEM);
  672. goto fail;
  673. }
  674. return 0;
  675. fail:
  676. if (map) {
  677. if (address)
  678. vaUnmapBuffer(hwctx->display, map->image.buf);
  679. if (map->image.image_id != VA_INVALID_ID)
  680. vaDestroyImage(hwctx->display, map->image.image_id);
  681. av_free(map);
  682. }
  683. return err;
  684. }
  685. static int vaapi_transfer_data_from(AVHWFramesContext *hwfc,
  686. AVFrame *dst, const AVFrame *src)
  687. {
  688. AVFrame *map;
  689. int err;
  690. map = av_frame_alloc();
  691. if (!map)
  692. return AVERROR(ENOMEM);
  693. map->format = dst->format;
  694. err = vaapi_map_frame(hwfc, map, src, VAAPI_MAP_READ);
  695. if (err)
  696. goto fail;
  697. err = av_frame_copy(dst, map);
  698. if (err)
  699. goto fail;
  700. err = 0;
  701. fail:
  702. av_frame_free(&map);
  703. return err;
  704. }
  705. static int vaapi_transfer_data_to(AVHWFramesContext *hwfc,
  706. AVFrame *dst, const AVFrame *src)
  707. {
  708. AVFrame *map;
  709. int err;
  710. map = av_frame_alloc();
  711. if (!map)
  712. return AVERROR(ENOMEM);
  713. map->format = src->format;
  714. err = vaapi_map_frame(hwfc, map, dst, VAAPI_MAP_WRITE);
  715. if (err)
  716. goto fail;
  717. err = av_frame_copy(map, src);
  718. if (err)
  719. goto fail;
  720. err = 0;
  721. fail:
  722. av_frame_free(&map);
  723. return err;
  724. }
  725. static void vaapi_device_free(AVHWDeviceContext *ctx)
  726. {
  727. AVVAAPIDeviceContext *hwctx = ctx->hwctx;
  728. VAAPIDevicePriv *priv = ctx->user_opaque;
  729. if (hwctx->display)
  730. vaTerminate(hwctx->display);
  731. #if HAVE_VAAPI_X11
  732. if (priv->x11_display)
  733. XCloseDisplay(priv->x11_display);
  734. #endif
  735. if (priv->drm_fd >= 0)
  736. close(priv->drm_fd);
  737. av_freep(&priv);
  738. }
  739. static int vaapi_device_create(AVHWDeviceContext *ctx, const char *device,
  740. AVDictionary *opts, int flags)
  741. {
  742. AVVAAPIDeviceContext *hwctx = ctx->hwctx;
  743. VAAPIDevicePriv *priv;
  744. VADisplay display = 0;
  745. VAStatus vas;
  746. int major, minor;
  747. priv = av_mallocz(sizeof(*priv));
  748. if (!priv)
  749. return AVERROR(ENOMEM);
  750. priv->drm_fd = -1;
  751. ctx->user_opaque = priv;
  752. ctx->free = vaapi_device_free;
  753. #if HAVE_VAAPI_X11
  754. if (!display && !(device && device[0] == '/')) {
  755. // Try to open the device as an X11 display.
  756. priv->x11_display = XOpenDisplay(device);
  757. if (!priv->x11_display) {
  758. av_log(ctx, AV_LOG_VERBOSE, "Cannot open X11 display "
  759. "%s.\n", XDisplayName(device));
  760. } else {
  761. display = vaGetDisplay(priv->x11_display);
  762. if (!display) {
  763. av_log(ctx, AV_LOG_ERROR, "Cannot open a VA display "
  764. "from X11 display %s.\n", XDisplayName(device));
  765. return AVERROR_UNKNOWN;
  766. }
  767. av_log(ctx, AV_LOG_VERBOSE, "Opened VA display via "
  768. "X11 display %s.\n", XDisplayName(device));
  769. }
  770. }
  771. #endif
  772. #if HAVE_VAAPI_DRM
  773. if (!display && device) {
  774. // Try to open the device as a DRM path.
  775. priv->drm_fd = open(device, O_RDWR);
  776. if (priv->drm_fd < 0) {
  777. av_log(ctx, AV_LOG_VERBOSE, "Cannot open DRM device %s.\n",
  778. device);
  779. } else {
  780. display = vaGetDisplayDRM(priv->drm_fd);
  781. if (!display) {
  782. av_log(ctx, AV_LOG_ERROR, "Cannot open a VA display "
  783. "from DRM device %s.\n", device);
  784. return AVERROR_UNKNOWN;
  785. }
  786. av_log(ctx, AV_LOG_VERBOSE, "Opened VA display via "
  787. "DRM device %s.\n", device);
  788. }
  789. }
  790. #endif
  791. if (!display) {
  792. av_log(ctx, AV_LOG_ERROR, "No VA display found for "
  793. "device: %s.\n", device ? device : "");
  794. return AVERROR(EINVAL);
  795. }
  796. hwctx->display = display;
  797. vas = vaInitialize(display, &major, &minor);
  798. if (vas != VA_STATUS_SUCCESS) {
  799. av_log(ctx, AV_LOG_ERROR, "Failed to initialise VAAPI "
  800. "connection: %d (%s).\n", vas, vaErrorStr(vas));
  801. return AVERROR(EIO);
  802. }
  803. av_log(ctx, AV_LOG_VERBOSE, "Initialised VAAPI connection: "
  804. "version %d.%d\n", major, minor);
  805. return 0;
  806. }
  807. const HWContextType ff_hwcontext_type_vaapi = {
  808. .type = AV_HWDEVICE_TYPE_VAAPI,
  809. .name = "VAAPI",
  810. .device_hwctx_size = sizeof(AVVAAPIDeviceContext),
  811. .device_priv_size = sizeof(VAAPIDeviceContext),
  812. .device_hwconfig_size = sizeof(AVVAAPIHWConfig),
  813. .frames_hwctx_size = sizeof(AVVAAPIFramesContext),
  814. .frames_priv_size = sizeof(VAAPIFramesContext),
  815. .device_create = &vaapi_device_create,
  816. .device_init = &vaapi_device_init,
  817. .device_uninit = &vaapi_device_uninit,
  818. .frames_get_constraints = &vaapi_frames_get_constraints,
  819. .frames_init = &vaapi_frames_init,
  820. .frames_uninit = &vaapi_frames_uninit,
  821. .frames_get_buffer = &vaapi_get_buffer,
  822. .transfer_get_formats = &vaapi_transfer_get_formats,
  823. .transfer_data_to = &vaapi_transfer_data_to,
  824. .transfer_data_from = &vaapi_transfer_data_from,
  825. .pix_fmts = (const enum AVPixelFormat[]) {
  826. AV_PIX_FMT_VAAPI,
  827. AV_PIX_FMT_NONE
  828. },
  829. };