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.

1004 lines
32KB

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