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.

1008 lines
33KB

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