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.

1002 lines
32KB

  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 const struct {
  242. const char *friendly_name;
  243. const char *match_string;
  244. unsigned int quirks;
  245. } vaapi_driver_quirks_table[] = {
  246. {
  247. "Intel i965 (Quick Sync)",
  248. "i965",
  249. AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS,
  250. },
  251. {
  252. "Intel iHD",
  253. "ubit",
  254. AV_VAAPI_DRIVER_QUIRK_ATTRIB_MEMTYPE,
  255. },
  256. };
  257. static int vaapi_device_init(AVHWDeviceContext *hwdev)
  258. {
  259. VAAPIDeviceContext *ctx = hwdev->internal->priv;
  260. AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
  261. VAImageFormat *image_list = NULL;
  262. VAStatus vas;
  263. const char *vendor_string;
  264. int err, i, image_count;
  265. enum AVPixelFormat pix_fmt;
  266. unsigned int fourcc;
  267. image_count = vaMaxNumImageFormats(hwctx->display);
  268. if (image_count <= 0) {
  269. err = AVERROR(EIO);
  270. goto fail;
  271. }
  272. image_list = av_malloc(image_count * sizeof(*image_list));
  273. if (!image_list) {
  274. err = AVERROR(ENOMEM);
  275. goto fail;
  276. }
  277. vas = vaQueryImageFormats(hwctx->display, image_list, &image_count);
  278. if (vas != VA_STATUS_SUCCESS) {
  279. err = AVERROR(EIO);
  280. goto fail;
  281. }
  282. ctx->formats = av_malloc(image_count * sizeof(*ctx->formats));
  283. if (!ctx->formats) {
  284. err = AVERROR(ENOMEM);
  285. goto fail;
  286. }
  287. ctx->nb_formats = 0;
  288. for (i = 0; i < image_count; i++) {
  289. fourcc = image_list[i].fourcc;
  290. pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
  291. if (pix_fmt == AV_PIX_FMT_NONE) {
  292. av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> unknown.\n",
  293. fourcc);
  294. } else {
  295. av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> %s.\n",
  296. fourcc, av_get_pix_fmt_name(pix_fmt));
  297. ctx->formats[ctx->nb_formats].pix_fmt = pix_fmt;
  298. ctx->formats[ctx->nb_formats].image_format = image_list[i];
  299. ++ctx->nb_formats;
  300. }
  301. }
  302. if (hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_USER_SET) {
  303. av_log(hwdev, AV_LOG_VERBOSE, "Not detecting driver: "
  304. "quirks set by user.\n");
  305. } else {
  306. // Detect the driver in use and set quirk flags if necessary.
  307. vendor_string = vaQueryVendorString(hwctx->display);
  308. hwctx->driver_quirks = 0;
  309. if (vendor_string) {
  310. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_driver_quirks_table); i++) {
  311. if (strstr(vendor_string,
  312. vaapi_driver_quirks_table[i].match_string)) {
  313. av_log(hwdev, AV_LOG_VERBOSE, "Matched \"%s\" as known "
  314. "driver \"%s\".\n", vendor_string,
  315. vaapi_driver_quirks_table[i].friendly_name);
  316. hwctx->driver_quirks |=
  317. vaapi_driver_quirks_table[i].quirks;
  318. break;
  319. }
  320. }
  321. if (!(i < FF_ARRAY_ELEMS(vaapi_driver_quirks_table))) {
  322. av_log(hwdev, AV_LOG_VERBOSE, "Unknown driver \"%s\", "
  323. "assuming standard behaviour.\n", vendor_string);
  324. }
  325. }
  326. }
  327. av_free(image_list);
  328. return 0;
  329. fail:
  330. av_freep(&ctx->formats);
  331. av_free(image_list);
  332. return err;
  333. }
  334. static void vaapi_device_uninit(AVHWDeviceContext *hwdev)
  335. {
  336. VAAPIDeviceContext *ctx = hwdev->internal->priv;
  337. av_freep(&ctx->formats);
  338. }
  339. static void vaapi_buffer_free(void *opaque, uint8_t *data)
  340. {
  341. AVHWFramesContext *hwfc = opaque;
  342. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  343. VASurfaceID surface_id;
  344. VAStatus vas;
  345. surface_id = (VASurfaceID)(uintptr_t)data;
  346. vas = vaDestroySurfaces(hwctx->display, &surface_id, 1);
  347. if (vas != VA_STATUS_SUCCESS) {
  348. av_log(hwfc, AV_LOG_ERROR, "Failed to destroy surface %#x: "
  349. "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
  350. }
  351. }
  352. static AVBufferRef *vaapi_pool_alloc(void *opaque, int size)
  353. {
  354. AVHWFramesContext *hwfc = opaque;
  355. VAAPIFramesContext *ctx = hwfc->internal->priv;
  356. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  357. AVVAAPIFramesContext *avfc = hwfc->hwctx;
  358. VASurfaceID surface_id;
  359. VAStatus vas;
  360. AVBufferRef *ref;
  361. vas = vaCreateSurfaces(hwctx->display, ctx->rt_format,
  362. hwfc->width, hwfc->height,
  363. &surface_id, 1,
  364. ctx->attributes, ctx->nb_attributes);
  365. if (vas != VA_STATUS_SUCCESS) {
  366. av_log(hwfc, AV_LOG_ERROR, "Failed to create surface: "
  367. "%d (%s).\n", vas, vaErrorStr(vas));
  368. return NULL;
  369. }
  370. av_log(hwfc, AV_LOG_DEBUG, "Created surface %#x.\n", surface_id);
  371. ref = av_buffer_create((uint8_t*)(uintptr_t)surface_id,
  372. sizeof(surface_id), &vaapi_buffer_free,
  373. hwfc, AV_BUFFER_FLAG_READONLY);
  374. if (!ref) {
  375. vaDestroySurfaces(hwctx->display, &surface_id, 1);
  376. return NULL;
  377. }
  378. if (hwfc->initial_pool_size > 0) {
  379. // This is a fixed-size pool, so we must still be in the initial
  380. // allocation sequence.
  381. av_assert0(avfc->nb_surfaces < hwfc->initial_pool_size);
  382. avfc->surface_ids[avfc->nb_surfaces] = surface_id;
  383. ++avfc->nb_surfaces;
  384. }
  385. return ref;
  386. }
  387. static int vaapi_frames_init(AVHWFramesContext *hwfc)
  388. {
  389. AVVAAPIFramesContext *avfc = hwfc->hwctx;
  390. VAAPIFramesContext *ctx = hwfc->internal->priv;
  391. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  392. VAImageFormat *expected_format;
  393. AVBufferRef *test_surface = NULL;
  394. VASurfaceID test_surface_id;
  395. VAImage test_image;
  396. VAStatus vas;
  397. int err, i;
  398. unsigned int fourcc, rt_format;
  399. for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++) {
  400. if (vaapi_format_map[i].pix_fmt == hwfc->sw_format) {
  401. fourcc = vaapi_format_map[i].fourcc;
  402. rt_format = vaapi_format_map[i].rt_format;
  403. break;
  404. }
  405. }
  406. if (i >= FF_ARRAY_ELEMS(vaapi_format_map)) {
  407. av_log(hwfc, AV_LOG_ERROR, "Unsupported format: %s.\n",
  408. av_get_pix_fmt_name(hwfc->sw_format));
  409. return AVERROR(EINVAL);
  410. }
  411. if (!hwfc->pool) {
  412. int need_memory_type = !(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_ATTRIB_MEMTYPE);
  413. int need_pixel_format = 1;
  414. for (i = 0; i < avfc->nb_attributes; i++) {
  415. if (ctx->attributes[i].type == VASurfaceAttribMemoryType)
  416. need_memory_type = 0;
  417. if (ctx->attributes[i].type == VASurfaceAttribPixelFormat)
  418. need_pixel_format = 0;
  419. }
  420. ctx->nb_attributes =
  421. avfc->nb_attributes + need_memory_type + need_pixel_format;
  422. ctx->attributes = av_malloc(ctx->nb_attributes *
  423. sizeof(*ctx->attributes));
  424. if (!ctx->attributes) {
  425. err = AVERROR(ENOMEM);
  426. goto fail;
  427. }
  428. for (i = 0; i < avfc->nb_attributes; i++)
  429. ctx->attributes[i] = avfc->attributes[i];
  430. if (need_memory_type) {
  431. ctx->attributes[i++] = (VASurfaceAttrib) {
  432. .type = VASurfaceAttribMemoryType,
  433. .flags = VA_SURFACE_ATTRIB_SETTABLE,
  434. .value.type = VAGenericValueTypeInteger,
  435. .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA,
  436. };
  437. }
  438. if (need_pixel_format) {
  439. ctx->attributes[i++] = (VASurfaceAttrib) {
  440. .type = VASurfaceAttribPixelFormat,
  441. .flags = VA_SURFACE_ATTRIB_SETTABLE,
  442. .value.type = VAGenericValueTypeInteger,
  443. .value.value.i = fourcc,
  444. };
  445. }
  446. av_assert0(i == ctx->nb_attributes);
  447. ctx->rt_format = rt_format;
  448. if (hwfc->initial_pool_size > 0) {
  449. // This pool will be usable as a render target, so we need to store
  450. // all of the surface IDs somewhere that vaCreateContext() calls
  451. // will be able to access them.
  452. avfc->nb_surfaces = 0;
  453. avfc->surface_ids = av_malloc(hwfc->initial_pool_size *
  454. sizeof(*avfc->surface_ids));
  455. if (!avfc->surface_ids) {
  456. err = AVERROR(ENOMEM);
  457. goto fail;
  458. }
  459. } else {
  460. // This pool allows dynamic sizing, and will not be usable as a
  461. // render target.
  462. avfc->nb_surfaces = 0;
  463. avfc->surface_ids = NULL;
  464. }
  465. hwfc->internal->pool_internal =
  466. av_buffer_pool_init2(sizeof(VASurfaceID), hwfc,
  467. &vaapi_pool_alloc, NULL);
  468. if (!hwfc->internal->pool_internal) {
  469. av_log(hwfc, AV_LOG_ERROR, "Failed to create VAAPI surface pool.\n");
  470. err = AVERROR(ENOMEM);
  471. goto fail;
  472. }
  473. }
  474. // Allocate a single surface to test whether vaDeriveImage() is going
  475. // to work for the specific configuration.
  476. if (hwfc->pool) {
  477. test_surface = av_buffer_pool_get(hwfc->pool);
  478. if (!test_surface) {
  479. av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
  480. "user-configured buffer pool.\n");
  481. err = AVERROR(ENOMEM);
  482. goto fail;
  483. }
  484. } else {
  485. test_surface = av_buffer_pool_get(hwfc->internal->pool_internal);
  486. if (!test_surface) {
  487. av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
  488. "internal buffer pool.\n");
  489. err = AVERROR(ENOMEM);
  490. goto fail;
  491. }
  492. }
  493. test_surface_id = (VASurfaceID)(uintptr_t)test_surface->data;
  494. ctx->derive_works = 0;
  495. err = vaapi_get_image_format(hwfc->device_ctx,
  496. hwfc->sw_format, &expected_format);
  497. if (err == 0) {
  498. vas = vaDeriveImage(hwctx->display, test_surface_id, &test_image);
  499. if (vas == VA_STATUS_SUCCESS) {
  500. if (expected_format->fourcc == test_image.format.fourcc) {
  501. av_log(hwfc, AV_LOG_DEBUG, "Direct mapping possible.\n");
  502. ctx->derive_works = 1;
  503. } else {
  504. av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
  505. "derived image format %08x does not match "
  506. "expected format %08x.\n",
  507. expected_format->fourcc, test_image.format.fourcc);
  508. }
  509. vaDestroyImage(hwctx->display, test_image.image_id);
  510. } else {
  511. av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
  512. "deriving image does not work: "
  513. "%d (%s).\n", vas, vaErrorStr(vas));
  514. }
  515. } else {
  516. av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
  517. "image format is not supported.\n");
  518. }
  519. av_buffer_unref(&test_surface);
  520. return 0;
  521. fail:
  522. av_buffer_unref(&test_surface);
  523. av_freep(&avfc->surface_ids);
  524. av_freep(&ctx->attributes);
  525. return err;
  526. }
  527. static void vaapi_frames_uninit(AVHWFramesContext *hwfc)
  528. {
  529. AVVAAPIFramesContext *avfc = hwfc->hwctx;
  530. VAAPIFramesContext *ctx = hwfc->internal->priv;
  531. av_freep(&avfc->surface_ids);
  532. av_freep(&ctx->attributes);
  533. }
  534. static int vaapi_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
  535. {
  536. frame->buf[0] = av_buffer_pool_get(hwfc->pool);
  537. if (!frame->buf[0])
  538. return AVERROR(ENOMEM);
  539. frame->data[3] = frame->buf[0]->data;
  540. frame->format = AV_PIX_FMT_VAAPI;
  541. frame->width = hwfc->width;
  542. frame->height = hwfc->height;
  543. return 0;
  544. }
  545. static int vaapi_transfer_get_formats(AVHWFramesContext *hwfc,
  546. enum AVHWFrameTransferDirection dir,
  547. enum AVPixelFormat **formats)
  548. {
  549. VAAPIDeviceContext *ctx = hwfc->device_ctx->internal->priv;
  550. enum AVPixelFormat *pix_fmts, preferred_format;
  551. int i, k;
  552. preferred_format = hwfc->sw_format;
  553. pix_fmts = av_malloc((ctx->nb_formats + 1) * sizeof(*pix_fmts));
  554. if (!pix_fmts)
  555. return AVERROR(ENOMEM);
  556. pix_fmts[0] = preferred_format;
  557. k = 1;
  558. for (i = 0; i < ctx->nb_formats; i++) {
  559. if (ctx->formats[i].pix_fmt == preferred_format)
  560. continue;
  561. av_assert0(k < ctx->nb_formats);
  562. pix_fmts[k++] = ctx->formats[i].pix_fmt;
  563. }
  564. av_assert0(k == ctx->nb_formats);
  565. pix_fmts[k] = AV_PIX_FMT_NONE;
  566. *formats = pix_fmts;
  567. return 0;
  568. }
  569. static void vaapi_unmap_frame(void *opaque, uint8_t *data)
  570. {
  571. AVHWFramesContext *hwfc = opaque;
  572. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  573. VAAPISurfaceMap *map = (VAAPISurfaceMap*)data;
  574. const AVFrame *src;
  575. VASurfaceID surface_id;
  576. VAStatus vas;
  577. src = map->source;
  578. surface_id = (VASurfaceID)(uintptr_t)src->data[3];
  579. av_log(hwfc, AV_LOG_DEBUG, "Unmap surface %#x.\n", surface_id);
  580. vas = vaUnmapBuffer(hwctx->display, map->image.buf);
  581. if (vas != VA_STATUS_SUCCESS) {
  582. av_log(hwfc, AV_LOG_ERROR, "Failed to unmap image from surface "
  583. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  584. }
  585. if ((map->flags & VAAPI_MAP_WRITE) &&
  586. !(map->flags & VAAPI_MAP_DIRECT)) {
  587. vas = vaPutImage(hwctx->display, surface_id, map->image.image_id,
  588. 0, 0, hwfc->width, hwfc->height,
  589. 0, 0, hwfc->width, hwfc->height);
  590. if (vas != VA_STATUS_SUCCESS) {
  591. av_log(hwfc, AV_LOG_ERROR, "Failed to write image to surface "
  592. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  593. }
  594. }
  595. vas = vaDestroyImage(hwctx->display, map->image.image_id);
  596. if (vas != VA_STATUS_SUCCESS) {
  597. av_log(hwfc, AV_LOG_ERROR, "Failed to destroy image from surface "
  598. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  599. }
  600. av_free(map);
  601. }
  602. static int vaapi_map_frame(AVHWFramesContext *hwfc,
  603. AVFrame *dst, const AVFrame *src, int flags)
  604. {
  605. AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
  606. VAAPIFramesContext *ctx = hwfc->internal->priv;
  607. VASurfaceID surface_id;
  608. VAImageFormat *image_format;
  609. VAAPISurfaceMap *map;
  610. VAStatus vas;
  611. void *address = NULL;
  612. int err, i;
  613. surface_id = (VASurfaceID)(uintptr_t)src->data[3];
  614. av_log(hwfc, AV_LOG_DEBUG, "Map surface %#x.\n", surface_id);
  615. if (!ctx->derive_works && (flags & VAAPI_MAP_DIRECT)) {
  616. // Requested direct mapping but it is not possible.
  617. return AVERROR(EINVAL);
  618. }
  619. if (dst->format == AV_PIX_FMT_NONE)
  620. dst->format = hwfc->sw_format;
  621. if (dst->format != hwfc->sw_format && (flags & VAAPI_MAP_DIRECT)) {
  622. // Requested direct mapping but the formats do not match.
  623. return AVERROR(EINVAL);
  624. }
  625. err = vaapi_get_image_format(hwfc->device_ctx, dst->format, &image_format);
  626. if (err < 0) {
  627. // Requested format is not a valid output format.
  628. return AVERROR(EINVAL);
  629. }
  630. map = av_malloc(sizeof(VAAPISurfaceMap));
  631. if (!map)
  632. return AVERROR(ENOMEM);
  633. map->source = src;
  634. map->flags = flags;
  635. map->image.image_id = VA_INVALID_ID;
  636. vas = vaSyncSurface(hwctx->display, surface_id);
  637. if (vas != VA_STATUS_SUCCESS) {
  638. av_log(hwfc, AV_LOG_ERROR, "Failed to sync surface "
  639. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  640. err = AVERROR(EIO);
  641. goto fail;
  642. }
  643. // The memory which we map using derive need not be connected to the CPU
  644. // in a way conducive to fast access. On Gen7-Gen9 Intel graphics, the
  645. // memory is mappable but not cached, so normal memcpy()-like access is
  646. // very slow to read it (but writing is ok). It is possible to read much
  647. // faster with a copy routine which is aware of the limitation, but we
  648. // assume for now that the user is not aware of that and would therefore
  649. // prefer not to be given direct-mapped memory if they request read access.
  650. if (ctx->derive_works &&
  651. ((flags & VAAPI_MAP_DIRECT) || !(flags & VAAPI_MAP_READ))) {
  652. vas = vaDeriveImage(hwctx->display, surface_id, &map->image);
  653. if (vas != VA_STATUS_SUCCESS) {
  654. av_log(hwfc, AV_LOG_ERROR, "Failed to derive image from "
  655. "surface %#x: %d (%s).\n",
  656. surface_id, vas, vaErrorStr(vas));
  657. err = AVERROR(EIO);
  658. goto fail;
  659. }
  660. if (map->image.format.fourcc != image_format->fourcc) {
  661. av_log(hwfc, AV_LOG_ERROR, "Derive image of surface %#x "
  662. "is in wrong format: expected %#08x, got %#08x.\n",
  663. surface_id, image_format->fourcc, map->image.format.fourcc);
  664. err = AVERROR(EIO);
  665. goto fail;
  666. }
  667. map->flags |= VAAPI_MAP_DIRECT;
  668. } else {
  669. vas = vaCreateImage(hwctx->display, image_format,
  670. hwfc->width, hwfc->height, &map->image);
  671. if (vas != VA_STATUS_SUCCESS) {
  672. av_log(hwfc, AV_LOG_ERROR, "Failed to create image for "
  673. "surface %#x: %d (%s).\n",
  674. surface_id, vas, vaErrorStr(vas));
  675. err = AVERROR(EIO);
  676. goto fail;
  677. }
  678. if (flags & VAAPI_MAP_READ) {
  679. vas = vaGetImage(hwctx->display, surface_id, 0, 0,
  680. hwfc->width, hwfc->height, map->image.image_id);
  681. if (vas != VA_STATUS_SUCCESS) {
  682. av_log(hwfc, AV_LOG_ERROR, "Failed to read image from "
  683. "surface %#x: %d (%s).\n",
  684. surface_id, vas, vaErrorStr(vas));
  685. err = AVERROR(EIO);
  686. goto fail;
  687. }
  688. }
  689. }
  690. vas = vaMapBuffer(hwctx->display, map->image.buf, &address);
  691. if (vas != VA_STATUS_SUCCESS) {
  692. av_log(hwfc, AV_LOG_ERROR, "Failed to map image from surface "
  693. "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
  694. err = AVERROR(EIO);
  695. goto fail;
  696. }
  697. dst->width = src->width;
  698. dst->height = src->height;
  699. for (i = 0; i < map->image.num_planes; i++) {
  700. dst->data[i] = (uint8_t*)address + map->image.offsets[i];
  701. dst->linesize[i] = map->image.pitches[i];
  702. }
  703. if (
  704. #ifdef VA_FOURCC_YV16
  705. map->image.format.fourcc == VA_FOURCC_YV16 ||
  706. #endif
  707. map->image.format.fourcc == VA_FOURCC_YV12) {
  708. // Chroma planes are YVU rather than YUV, so swap them.
  709. FFSWAP(uint8_t*, dst->data[1], dst->data[2]);
  710. }
  711. dst->buf[0] = av_buffer_create((uint8_t*)map, sizeof(*map),
  712. &vaapi_unmap_frame, hwfc, 0);
  713. if (!dst->buf[0]) {
  714. err = AVERROR(ENOMEM);
  715. goto fail;
  716. }
  717. return 0;
  718. fail:
  719. if (map) {
  720. if (address)
  721. vaUnmapBuffer(hwctx->display, map->image.buf);
  722. if (map->image.image_id != VA_INVALID_ID)
  723. vaDestroyImage(hwctx->display, map->image.image_id);
  724. av_free(map);
  725. }
  726. return err;
  727. }
  728. static int vaapi_transfer_data_from(AVHWFramesContext *hwfc,
  729. AVFrame *dst, const AVFrame *src)
  730. {
  731. AVFrame *map;
  732. int err;
  733. if (dst->width > hwfc->width || dst->height > hwfc->height)
  734. return AVERROR(EINVAL);
  735. map = av_frame_alloc();
  736. if (!map)
  737. return AVERROR(ENOMEM);
  738. map->format = dst->format;
  739. err = vaapi_map_frame(hwfc, map, src, VAAPI_MAP_READ);
  740. if (err)
  741. goto fail;
  742. map->width = dst->width;
  743. map->height = dst->height;
  744. err = av_frame_copy(dst, map);
  745. if (err)
  746. goto fail;
  747. err = 0;
  748. fail:
  749. av_frame_free(&map);
  750. return err;
  751. }
  752. static int vaapi_transfer_data_to(AVHWFramesContext *hwfc,
  753. AVFrame *dst, const AVFrame *src)
  754. {
  755. AVFrame *map;
  756. int err;
  757. if (src->width > hwfc->width || src->height > hwfc->height)
  758. return AVERROR(EINVAL);
  759. map = av_frame_alloc();
  760. if (!map)
  761. return AVERROR(ENOMEM);
  762. map->format = src->format;
  763. err = vaapi_map_frame(hwfc, map, dst, VAAPI_MAP_WRITE);
  764. if (err)
  765. goto fail;
  766. map->width = src->width;
  767. map->height = src->height;
  768. err = av_frame_copy(map, src);
  769. if (err)
  770. goto fail;
  771. err = 0;
  772. fail:
  773. av_frame_free(&map);
  774. return err;
  775. }
  776. static void vaapi_device_free(AVHWDeviceContext *ctx)
  777. {
  778. AVVAAPIDeviceContext *hwctx = ctx->hwctx;
  779. VAAPIDevicePriv *priv = ctx->user_opaque;
  780. if (hwctx->display)
  781. vaTerminate(hwctx->display);
  782. #if HAVE_VAAPI_X11
  783. if (priv->x11_display)
  784. XCloseDisplay(priv->x11_display);
  785. #endif
  786. if (priv->drm_fd >= 0)
  787. close(priv->drm_fd);
  788. av_freep(&priv);
  789. }
  790. static int vaapi_device_create(AVHWDeviceContext *ctx, const char *device,
  791. AVDictionary *opts, int flags)
  792. {
  793. AVVAAPIDeviceContext *hwctx = ctx->hwctx;
  794. VAAPIDevicePriv *priv;
  795. VADisplay display = 0;
  796. VAStatus vas;
  797. int major, minor;
  798. priv = av_mallocz(sizeof(*priv));
  799. if (!priv)
  800. return AVERROR(ENOMEM);
  801. priv->drm_fd = -1;
  802. ctx->user_opaque = priv;
  803. ctx->free = vaapi_device_free;
  804. #if HAVE_VAAPI_X11
  805. if (!display && !(device && device[0] == '/')) {
  806. // Try to open the device as an X11 display.
  807. priv->x11_display = XOpenDisplay(device);
  808. if (!priv->x11_display) {
  809. av_log(ctx, AV_LOG_VERBOSE, "Cannot open X11 display "
  810. "%s.\n", XDisplayName(device));
  811. } else {
  812. display = vaGetDisplay(priv->x11_display);
  813. if (!display) {
  814. av_log(ctx, AV_LOG_ERROR, "Cannot open a VA display "
  815. "from X11 display %s.\n", XDisplayName(device));
  816. return AVERROR_UNKNOWN;
  817. }
  818. av_log(ctx, AV_LOG_VERBOSE, "Opened VA display via "
  819. "X11 display %s.\n", XDisplayName(device));
  820. }
  821. }
  822. #endif
  823. #if HAVE_VAAPI_DRM
  824. if (!display) {
  825. // Try to open the device as a DRM path.
  826. // Default to using the first render node if the user did not
  827. // supply a path.
  828. const char *path = device ? device : "/dev/dri/renderD128";
  829. priv->drm_fd = open(path, O_RDWR);
  830. if (priv->drm_fd < 0) {
  831. av_log(ctx, AV_LOG_VERBOSE, "Cannot open DRM device %s.\n",
  832. path);
  833. } else {
  834. display = vaGetDisplayDRM(priv->drm_fd);
  835. if (!display) {
  836. av_log(ctx, AV_LOG_ERROR, "Cannot open a VA display "
  837. "from DRM device %s.\n", path);
  838. return AVERROR_UNKNOWN;
  839. }
  840. av_log(ctx, AV_LOG_VERBOSE, "Opened VA display via "
  841. "DRM device %s.\n", path);
  842. }
  843. }
  844. #endif
  845. if (!display) {
  846. av_log(ctx, AV_LOG_ERROR, "No VA display found for "
  847. "device: %s.\n", device ? device : "");
  848. return AVERROR(EINVAL);
  849. }
  850. hwctx->display = display;
  851. vas = vaInitialize(display, &major, &minor);
  852. if (vas != VA_STATUS_SUCCESS) {
  853. av_log(ctx, AV_LOG_ERROR, "Failed to initialise VAAPI "
  854. "connection: %d (%s).\n", vas, vaErrorStr(vas));
  855. return AVERROR(EIO);
  856. }
  857. av_log(ctx, AV_LOG_VERBOSE, "Initialised VAAPI connection: "
  858. "version %d.%d\n", major, minor);
  859. return 0;
  860. }
  861. const HWContextType ff_hwcontext_type_vaapi = {
  862. .type = AV_HWDEVICE_TYPE_VAAPI,
  863. .name = "VAAPI",
  864. .device_hwctx_size = sizeof(AVVAAPIDeviceContext),
  865. .device_priv_size = sizeof(VAAPIDeviceContext),
  866. .device_hwconfig_size = sizeof(AVVAAPIHWConfig),
  867. .frames_hwctx_size = sizeof(AVVAAPIFramesContext),
  868. .frames_priv_size = sizeof(VAAPIFramesContext),
  869. .device_create = &vaapi_device_create,
  870. .device_init = &vaapi_device_init,
  871. .device_uninit = &vaapi_device_uninit,
  872. .frames_get_constraints = &vaapi_frames_get_constraints,
  873. .frames_init = &vaapi_frames_init,
  874. .frames_uninit = &vaapi_frames_uninit,
  875. .frames_get_buffer = &vaapi_get_buffer,
  876. .transfer_get_formats = &vaapi_transfer_get_formats,
  877. .transfer_data_to = &vaapi_transfer_data_to,
  878. .transfer_data_from = &vaapi_transfer_data_from,
  879. .pix_fmts = (const enum AVPixelFormat[]) {
  880. AV_PIX_FMT_VAAPI,
  881. AV_PIX_FMT_NONE
  882. },
  883. };