2 * This file is part of FFmpeg.
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.
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.
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
22 # include <va/va_x11.h>
25 # include <va/va_drm.h>
29 # include <va/va_drmcommon.h>
31 # include <drm_fourcc.h>
32 # ifndef DRM_FORMAT_MOD_INVALID
33 # define DRM_FORMAT_MOD_INVALID ((1ULL << 56) - 1)
46 #include "hwcontext.h"
47 #include "hwcontext_drm.h"
48 #include "hwcontext_internal.h"
49 #include "hwcontext_vaapi.h"
55 typedef struct VAAPIDevicePriv {
63 typedef struct VAAPISurfaceFormat {
64 enum AVPixelFormat pix_fmt;
65 VAImageFormat image_format;
68 typedef struct VAAPIDeviceContext {
69 // Surface formats which can be used with this device.
70 VAAPISurfaceFormat *formats;
74 typedef struct VAAPIFramesContext {
75 // Surface attributes set at create time.
76 VASurfaceAttrib *attributes;
78 // RT format of the underlying surface (Intel driver ignores this anyway).
79 unsigned int rt_format;
80 // Whether vaDeriveImage works.
84 typedef struct VAAPIMapping {
85 // Handle to the derived or copied image which is mapped.
87 // The mapping flags actually used.
91 typedef struct VAAPIFormat {
93 unsigned int rt_format;
94 enum AVPixelFormat pix_fmt;
95 int chroma_planes_swapped;
96 } VAAPIFormatDescriptor;
98 #define MAP(va, rt, av, swap_uv) { \
100 VA_RT_FORMAT_ ## rt, \
104 // The map fourcc <-> pix_fmt isn't bijective because of the annoying U/V
105 // plane swap cases. The frame handling below tries to hide these.
106 static const VAAPIFormatDescriptor vaapi_format_map[] = {
107 MAP(NV12, YUV420, NV12, 0),
108 #ifdef VA_FOURCC_I420
109 MAP(I420, YUV420, YUV420P, 0),
111 MAP(YV12, YUV420, YUV420P, 1),
112 MAP(IYUV, YUV420, YUV420P, 0),
113 MAP(422H, YUV422, YUV422P, 0),
114 #ifdef VA_FOURCC_YV16
115 MAP(YV16, YUV422, YUV422P, 1),
117 MAP(UYVY, YUV422, UYVY422, 0),
118 MAP(YUY2, YUV422, YUYV422, 0),
119 #ifdef VA_FOURCC_Y210
120 MAP(Y210, YUV422_10, Y210, 0),
122 MAP(411P, YUV411, YUV411P, 0),
123 MAP(422V, YUV422, YUV440P, 0),
124 MAP(444P, YUV444, YUV444P, 0),
125 MAP(Y800, YUV400, GRAY8, 0),
126 #ifdef VA_FOURCC_P010
127 MAP(P010, YUV420_10BPP, P010, 0),
129 MAP(BGRA, RGB32, BGRA, 0),
130 MAP(BGRX, RGB32, BGR0, 0),
131 MAP(RGBA, RGB32, RGBA, 0),
132 MAP(RGBX, RGB32, RGB0, 0),
133 #ifdef VA_FOURCC_ABGR
134 MAP(ABGR, RGB32, ABGR, 0),
135 MAP(XBGR, RGB32, 0BGR, 0),
137 MAP(ARGB, RGB32, ARGB, 0),
138 MAP(XRGB, RGB32, 0RGB, 0),
139 #ifdef VA_FOURCC_X2R10G10B10
140 MAP(X2R10G10B10, RGB32_10, X2RGB10, 0),
145 static const VAAPIFormatDescriptor *
146 vaapi_format_from_fourcc(unsigned int fourcc)
149 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++)
150 if (vaapi_format_map[i].fourcc == fourcc)
151 return &vaapi_format_map[i];
155 static const VAAPIFormatDescriptor *
156 vaapi_format_from_pix_fmt(enum AVPixelFormat pix_fmt)
159 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++)
160 if (vaapi_format_map[i].pix_fmt == pix_fmt)
161 return &vaapi_format_map[i];
165 static enum AVPixelFormat vaapi_pix_fmt_from_fourcc(unsigned int fourcc)
167 const VAAPIFormatDescriptor *desc;
168 desc = vaapi_format_from_fourcc(fourcc);
170 return desc->pix_fmt;
172 return AV_PIX_FMT_NONE;
175 static int vaapi_get_image_format(AVHWDeviceContext *hwdev,
176 enum AVPixelFormat pix_fmt,
177 VAImageFormat **image_format)
179 VAAPIDeviceContext *ctx = hwdev->internal->priv;
182 for (i = 0; i < ctx->nb_formats; i++) {
183 if (ctx->formats[i].pix_fmt == pix_fmt) {
185 *image_format = &ctx->formats[i].image_format;
189 return AVERROR(EINVAL);
192 static int vaapi_frames_get_constraints(AVHWDeviceContext *hwdev,
193 const void *hwconfig,
194 AVHWFramesConstraints *constraints)
196 AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
197 const AVVAAPIHWConfig *config = hwconfig;
198 VAAPIDeviceContext *ctx = hwdev->internal->priv;
199 VASurfaceAttrib *attr_list = NULL;
201 enum AVPixelFormat pix_fmt;
203 int err, i, j, attr_count, pix_fmt_count;
206 !(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_SURFACE_ATTRIBUTES)) {
208 vas = vaQuerySurfaceAttributes(hwctx->display, config->config_id,
210 if (vas != VA_STATUS_SUCCESS) {
211 av_log(hwdev, AV_LOG_ERROR, "Failed to query surface attributes: "
212 "%d (%s).\n", vas, vaErrorStr(vas));
213 err = AVERROR(ENOSYS);
217 attr_list = av_malloc(attr_count * sizeof(*attr_list));
219 err = AVERROR(ENOMEM);
223 vas = vaQuerySurfaceAttributes(hwctx->display, config->config_id,
224 attr_list, &attr_count);
225 if (vas != VA_STATUS_SUCCESS) {
226 av_log(hwdev, AV_LOG_ERROR, "Failed to query surface attributes: "
227 "%d (%s).\n", vas, vaErrorStr(vas));
228 err = AVERROR(ENOSYS);
233 for (i = 0; i < attr_count; i++) {
234 switch (attr_list[i].type) {
235 case VASurfaceAttribPixelFormat:
236 fourcc = attr_list[i].value.value.i;
237 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
238 if (pix_fmt != AV_PIX_FMT_NONE) {
241 // Something unsupported - ignore.
244 case VASurfaceAttribMinWidth:
245 constraints->min_width = attr_list[i].value.value.i;
247 case VASurfaceAttribMinHeight:
248 constraints->min_height = attr_list[i].value.value.i;
250 case VASurfaceAttribMaxWidth:
251 constraints->max_width = attr_list[i].value.value.i;
253 case VASurfaceAttribMaxHeight:
254 constraints->max_height = attr_list[i].value.value.i;
258 if (pix_fmt_count == 0) {
259 // Nothing usable found. Presumably there exists something which
260 // works, so leave the set null to indicate unknown.
261 constraints->valid_sw_formats = NULL;
263 constraints->valid_sw_formats = av_malloc_array(pix_fmt_count + 1,
265 if (!constraints->valid_sw_formats) {
266 err = AVERROR(ENOMEM);
270 for (i = j = 0; i < attr_count; i++) {
271 if (attr_list[i].type != VASurfaceAttribPixelFormat)
273 fourcc = attr_list[i].value.value.i;
274 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
275 if (pix_fmt != AV_PIX_FMT_NONE)
276 constraints->valid_sw_formats[j++] = pix_fmt;
278 av_assert0(j == pix_fmt_count);
279 constraints->valid_sw_formats[j] = AV_PIX_FMT_NONE;
282 // No configuration supplied.
283 // Return the full set of image formats known by the implementation.
284 constraints->valid_sw_formats = av_malloc_array(ctx->nb_formats + 1,
286 if (!constraints->valid_sw_formats) {
287 err = AVERROR(ENOMEM);
290 for (i = 0; i < ctx->nb_formats; i++)
291 constraints->valid_sw_formats[i] = ctx->formats[i].pix_fmt;
292 constraints->valid_sw_formats[i] = AV_PIX_FMT_NONE;
295 constraints->valid_hw_formats = av_malloc_array(2, sizeof(pix_fmt));
296 if (!constraints->valid_hw_formats) {
297 err = AVERROR(ENOMEM);
300 constraints->valid_hw_formats[0] = AV_PIX_FMT_VAAPI;
301 constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
305 av_freep(&attr_list);
309 static const struct {
310 const char *friendly_name;
311 const char *match_string;
313 } vaapi_driver_quirks_table[] = {
314 #if !VA_CHECK_VERSION(1, 0, 0)
315 // The i965 driver did not conform before version 2.0.
317 "Intel i965 (Quick Sync)",
319 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS,
325 AV_VAAPI_DRIVER_QUIRK_ATTRIB_MEMTYPE,
329 "Splitted-Desktop Systems VDPAU backend for VA-API",
330 AV_VAAPI_DRIVER_QUIRK_SURFACE_ATTRIBUTES,
334 static int vaapi_device_init(AVHWDeviceContext *hwdev)
336 VAAPIDeviceContext *ctx = hwdev->internal->priv;
337 AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
338 VAImageFormat *image_list = NULL;
340 const char *vendor_string;
341 int err, i, image_count;
342 enum AVPixelFormat pix_fmt;
345 image_count = vaMaxNumImageFormats(hwctx->display);
346 if (image_count <= 0) {
350 image_list = av_malloc(image_count * sizeof(*image_list));
352 err = AVERROR(ENOMEM);
355 vas = vaQueryImageFormats(hwctx->display, image_list, &image_count);
356 if (vas != VA_STATUS_SUCCESS) {
361 ctx->formats = av_malloc(image_count * sizeof(*ctx->formats));
363 err = AVERROR(ENOMEM);
367 for (i = 0; i < image_count; i++) {
368 fourcc = image_list[i].fourcc;
369 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
370 if (pix_fmt == AV_PIX_FMT_NONE) {
371 av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> unknown.\n",
374 av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> %s.\n",
375 fourcc, av_get_pix_fmt_name(pix_fmt));
376 ctx->formats[ctx->nb_formats].pix_fmt = pix_fmt;
377 ctx->formats[ctx->nb_formats].image_format = image_list[i];
382 vendor_string = vaQueryVendorString(hwctx->display);
384 av_log(hwdev, AV_LOG_VERBOSE, "VAAPI driver: %s.\n", vendor_string);
386 if (hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_USER_SET) {
387 av_log(hwdev, AV_LOG_VERBOSE, "Using quirks set by user (%#x).\n",
388 hwctx->driver_quirks);
390 // Detect the driver in use and set quirk flags if necessary.
391 hwctx->driver_quirks = 0;
393 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_driver_quirks_table); i++) {
394 if (strstr(vendor_string,
395 vaapi_driver_quirks_table[i].match_string)) {
396 av_log(hwdev, AV_LOG_VERBOSE, "Matched driver string "
397 "as known nonstandard driver \"%s\", setting "
399 vaapi_driver_quirks_table[i].friendly_name,
400 vaapi_driver_quirks_table[i].quirks);
401 hwctx->driver_quirks |=
402 vaapi_driver_quirks_table[i].quirks;
406 if (!(i < FF_ARRAY_ELEMS(vaapi_driver_quirks_table))) {
407 av_log(hwdev, AV_LOG_VERBOSE, "Driver not found in known "
408 "nonstandard list, using standard behaviour.\n");
411 av_log(hwdev, AV_LOG_VERBOSE, "Driver has no vendor string, "
412 "assuming standard behaviour.\n");
419 av_freep(&ctx->formats);
424 static void vaapi_device_uninit(AVHWDeviceContext *hwdev)
426 VAAPIDeviceContext *ctx = hwdev->internal->priv;
428 av_freep(&ctx->formats);
431 static void vaapi_buffer_free(void *opaque, uint8_t *data)
433 AVHWFramesContext *hwfc = opaque;
434 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
435 VASurfaceID surface_id;
438 surface_id = (VASurfaceID)(uintptr_t)data;
440 vas = vaDestroySurfaces(hwctx->display, &surface_id, 1);
441 if (vas != VA_STATUS_SUCCESS) {
442 av_log(hwfc, AV_LOG_ERROR, "Failed to destroy surface %#x: "
443 "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
447 static AVBufferRef *vaapi_pool_alloc(void *opaque, int size)
449 AVHWFramesContext *hwfc = opaque;
450 VAAPIFramesContext *ctx = hwfc->internal->priv;
451 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
452 AVVAAPIFramesContext *avfc = hwfc->hwctx;
453 VASurfaceID surface_id;
457 if (hwfc->initial_pool_size > 0 &&
458 avfc->nb_surfaces >= hwfc->initial_pool_size)
461 vas = vaCreateSurfaces(hwctx->display, ctx->rt_format,
462 hwfc->width, hwfc->height,
464 ctx->attributes, ctx->nb_attributes);
465 if (vas != VA_STATUS_SUCCESS) {
466 av_log(hwfc, AV_LOG_ERROR, "Failed to create surface: "
467 "%d (%s).\n", vas, vaErrorStr(vas));
470 av_log(hwfc, AV_LOG_DEBUG, "Created surface %#x.\n", surface_id);
472 ref = av_buffer_create((uint8_t*)(uintptr_t)surface_id,
473 sizeof(surface_id), &vaapi_buffer_free,
474 hwfc, AV_BUFFER_FLAG_READONLY);
476 vaDestroySurfaces(hwctx->display, &surface_id, 1);
480 if (hwfc->initial_pool_size > 0) {
481 // This is a fixed-size pool, so we must still be in the initial
482 // allocation sequence.
483 av_assert0(avfc->nb_surfaces < hwfc->initial_pool_size);
484 avfc->surface_ids[avfc->nb_surfaces] = surface_id;
491 static int vaapi_frames_init(AVHWFramesContext *hwfc)
493 AVVAAPIFramesContext *avfc = hwfc->hwctx;
494 VAAPIFramesContext *ctx = hwfc->internal->priv;
495 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
496 const VAAPIFormatDescriptor *desc;
497 VAImageFormat *expected_format;
498 AVBufferRef *test_surface = NULL;
499 VASurfaceID test_surface_id;
504 desc = vaapi_format_from_pix_fmt(hwfc->sw_format);
506 av_log(hwfc, AV_LOG_ERROR, "Unsupported format: %s.\n",
507 av_get_pix_fmt_name(hwfc->sw_format));
508 return AVERROR(EINVAL);
512 if (!(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_SURFACE_ATTRIBUTES)) {
513 int need_memory_type = !(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_ATTRIB_MEMTYPE);
514 int need_pixel_format = 1;
515 for (i = 0; i < avfc->nb_attributes; i++) {
516 if (avfc->attributes[i].type == VASurfaceAttribMemoryType)
517 need_memory_type = 0;
518 if (avfc->attributes[i].type == VASurfaceAttribPixelFormat)
519 need_pixel_format = 0;
522 avfc->nb_attributes + need_memory_type + need_pixel_format;
524 ctx->attributes = av_malloc(ctx->nb_attributes *
525 sizeof(*ctx->attributes));
526 if (!ctx->attributes) {
527 err = AVERROR(ENOMEM);
531 for (i = 0; i < avfc->nb_attributes; i++)
532 ctx->attributes[i] = avfc->attributes[i];
533 if (need_memory_type) {
534 ctx->attributes[i++] = (VASurfaceAttrib) {
535 .type = VASurfaceAttribMemoryType,
536 .flags = VA_SURFACE_ATTRIB_SETTABLE,
537 .value.type = VAGenericValueTypeInteger,
538 .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA,
541 if (need_pixel_format) {
542 ctx->attributes[i++] = (VASurfaceAttrib) {
543 .type = VASurfaceAttribPixelFormat,
544 .flags = VA_SURFACE_ATTRIB_SETTABLE,
545 .value.type = VAGenericValueTypeInteger,
546 .value.value.i = desc->fourcc,
549 av_assert0(i == ctx->nb_attributes);
551 ctx->attributes = NULL;
552 ctx->nb_attributes = 0;
555 ctx->rt_format = desc->rt_format;
557 if (hwfc->initial_pool_size > 0) {
558 // This pool will be usable as a render target, so we need to store
559 // all of the surface IDs somewhere that vaCreateContext() calls
560 // will be able to access them.
561 avfc->nb_surfaces = 0;
562 avfc->surface_ids = av_malloc(hwfc->initial_pool_size *
563 sizeof(*avfc->surface_ids));
564 if (!avfc->surface_ids) {
565 err = AVERROR(ENOMEM);
569 // This pool allows dynamic sizing, and will not be usable as a
571 avfc->nb_surfaces = 0;
572 avfc->surface_ids = NULL;
575 hwfc->internal->pool_internal =
576 av_buffer_pool_init2(sizeof(VASurfaceID), hwfc,
577 &vaapi_pool_alloc, NULL);
578 if (!hwfc->internal->pool_internal) {
579 av_log(hwfc, AV_LOG_ERROR, "Failed to create VAAPI surface pool.\n");
580 err = AVERROR(ENOMEM);
585 // Allocate a single surface to test whether vaDeriveImage() is going
586 // to work for the specific configuration.
588 test_surface = av_buffer_pool_get(hwfc->pool);
590 av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
591 "user-configured buffer pool.\n");
592 err = AVERROR(ENOMEM);
596 test_surface = av_buffer_pool_get(hwfc->internal->pool_internal);
598 av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
599 "internal buffer pool.\n");
600 err = AVERROR(ENOMEM);
604 test_surface_id = (VASurfaceID)(uintptr_t)test_surface->data;
606 ctx->derive_works = 0;
608 err = vaapi_get_image_format(hwfc->device_ctx,
609 hwfc->sw_format, &expected_format);
611 vas = vaDeriveImage(hwctx->display, test_surface_id, &test_image);
612 if (vas == VA_STATUS_SUCCESS) {
613 if (expected_format->fourcc == test_image.format.fourcc) {
614 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping possible.\n");
615 ctx->derive_works = 1;
617 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
618 "derived image format %08x does not match "
619 "expected format %08x.\n",
620 expected_format->fourcc, test_image.format.fourcc);
622 vaDestroyImage(hwctx->display, test_image.image_id);
624 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
625 "deriving image does not work: "
626 "%d (%s).\n", vas, vaErrorStr(vas));
629 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
630 "image format is not supported.\n");
633 av_buffer_unref(&test_surface);
637 av_buffer_unref(&test_surface);
638 av_freep(&avfc->surface_ids);
639 av_freep(&ctx->attributes);
643 static void vaapi_frames_uninit(AVHWFramesContext *hwfc)
645 AVVAAPIFramesContext *avfc = hwfc->hwctx;
646 VAAPIFramesContext *ctx = hwfc->internal->priv;
648 av_freep(&avfc->surface_ids);
649 av_freep(&ctx->attributes);
652 static int vaapi_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
654 frame->buf[0] = av_buffer_pool_get(hwfc->pool);
656 return AVERROR(ENOMEM);
658 frame->data[3] = frame->buf[0]->data;
659 frame->format = AV_PIX_FMT_VAAPI;
660 frame->width = hwfc->width;
661 frame->height = hwfc->height;
666 static int vaapi_transfer_get_formats(AVHWFramesContext *hwfc,
667 enum AVHWFrameTransferDirection dir,
668 enum AVPixelFormat **formats)
670 VAAPIDeviceContext *ctx = hwfc->device_ctx->internal->priv;
671 enum AVPixelFormat *pix_fmts;
672 int i, k, sw_format_available;
674 sw_format_available = 0;
675 for (i = 0; i < ctx->nb_formats; i++) {
676 if (ctx->formats[i].pix_fmt == hwfc->sw_format)
677 sw_format_available = 1;
680 pix_fmts = av_malloc((ctx->nb_formats + 1) * sizeof(*pix_fmts));
682 return AVERROR(ENOMEM);
684 if (sw_format_available) {
685 pix_fmts[0] = hwfc->sw_format;
690 for (i = 0; i < ctx->nb_formats; i++) {
691 if (ctx->formats[i].pix_fmt == hwfc->sw_format)
693 av_assert0(k < ctx->nb_formats);
694 pix_fmts[k++] = ctx->formats[i].pix_fmt;
696 pix_fmts[k] = AV_PIX_FMT_NONE;
702 static void vaapi_unmap_frame(AVHWFramesContext *hwfc,
703 HWMapDescriptor *hwmap)
705 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
706 VAAPIMapping *map = hwmap->priv;
707 VASurfaceID surface_id;
710 surface_id = (VASurfaceID)(uintptr_t)hwmap->source->data[3];
711 av_log(hwfc, AV_LOG_DEBUG, "Unmap surface %#x.\n", surface_id);
713 vas = vaUnmapBuffer(hwctx->display, map->image.buf);
714 if (vas != VA_STATUS_SUCCESS) {
715 av_log(hwfc, AV_LOG_ERROR, "Failed to unmap image from surface "
716 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
719 if ((map->flags & AV_HWFRAME_MAP_WRITE) &&
720 !(map->flags & AV_HWFRAME_MAP_DIRECT)) {
721 vas = vaPutImage(hwctx->display, surface_id, map->image.image_id,
722 0, 0, hwfc->width, hwfc->height,
723 0, 0, hwfc->width, hwfc->height);
724 if (vas != VA_STATUS_SUCCESS) {
725 av_log(hwfc, AV_LOG_ERROR, "Failed to write image to surface "
726 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
730 vas = vaDestroyImage(hwctx->display, map->image.image_id);
731 if (vas != VA_STATUS_SUCCESS) {
732 av_log(hwfc, AV_LOG_ERROR, "Failed to destroy image from surface "
733 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
739 static int vaapi_map_frame(AVHWFramesContext *hwfc,
740 AVFrame *dst, const AVFrame *src, int flags)
742 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
743 VAAPIFramesContext *ctx = hwfc->internal->priv;
744 VASurfaceID surface_id;
745 const VAAPIFormatDescriptor *desc;
746 VAImageFormat *image_format;
749 void *address = NULL;
752 surface_id = (VASurfaceID)(uintptr_t)src->data[3];
753 av_log(hwfc, AV_LOG_DEBUG, "Map surface %#x.\n", surface_id);
755 if (!ctx->derive_works && (flags & AV_HWFRAME_MAP_DIRECT)) {
756 // Requested direct mapping but it is not possible.
757 return AVERROR(EINVAL);
759 if (dst->format == AV_PIX_FMT_NONE)
760 dst->format = hwfc->sw_format;
761 if (dst->format != hwfc->sw_format && (flags & AV_HWFRAME_MAP_DIRECT)) {
762 // Requested direct mapping but the formats do not match.
763 return AVERROR(EINVAL);
766 err = vaapi_get_image_format(hwfc->device_ctx, dst->format, &image_format);
768 // Requested format is not a valid output format.
769 return AVERROR(EINVAL);
772 map = av_malloc(sizeof(*map));
774 return AVERROR(ENOMEM);
776 map->image.image_id = VA_INVALID_ID;
778 vas = vaSyncSurface(hwctx->display, surface_id);
779 if (vas != VA_STATUS_SUCCESS) {
780 av_log(hwfc, AV_LOG_ERROR, "Failed to sync surface "
781 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
786 // The memory which we map using derive need not be connected to the CPU
787 // in a way conducive to fast access. On Gen7-Gen9 Intel graphics, the
788 // memory is mappable but not cached, so normal memcpy()-like access is
789 // very slow to read it (but writing is ok). It is possible to read much
790 // faster with a copy routine which is aware of the limitation, but we
791 // assume for now that the user is not aware of that and would therefore
792 // prefer not to be given direct-mapped memory if they request read access.
793 if (ctx->derive_works && dst->format == hwfc->sw_format &&
794 ((flags & AV_HWFRAME_MAP_DIRECT) || !(flags & AV_HWFRAME_MAP_READ))) {
795 vas = vaDeriveImage(hwctx->display, surface_id, &map->image);
796 if (vas != VA_STATUS_SUCCESS) {
797 av_log(hwfc, AV_LOG_ERROR, "Failed to derive image from "
798 "surface %#x: %d (%s).\n",
799 surface_id, vas, vaErrorStr(vas));
803 if (map->image.format.fourcc != image_format->fourcc) {
804 av_log(hwfc, AV_LOG_ERROR, "Derive image of surface %#x "
805 "is in wrong format: expected %#08x, got %#08x.\n",
806 surface_id, image_format->fourcc, map->image.format.fourcc);
810 map->flags |= AV_HWFRAME_MAP_DIRECT;
812 vas = vaCreateImage(hwctx->display, image_format,
813 hwfc->width, hwfc->height, &map->image);
814 if (vas != VA_STATUS_SUCCESS) {
815 av_log(hwfc, AV_LOG_ERROR, "Failed to create image for "
816 "surface %#x: %d (%s).\n",
817 surface_id, vas, vaErrorStr(vas));
821 if (!(flags & AV_HWFRAME_MAP_OVERWRITE)) {
822 vas = vaGetImage(hwctx->display, surface_id, 0, 0,
823 hwfc->width, hwfc->height, map->image.image_id);
824 if (vas != VA_STATUS_SUCCESS) {
825 av_log(hwfc, AV_LOG_ERROR, "Failed to read image from "
826 "surface %#x: %d (%s).\n",
827 surface_id, vas, vaErrorStr(vas));
834 vas = vaMapBuffer(hwctx->display, map->image.buf, &address);
835 if (vas != VA_STATUS_SUCCESS) {
836 av_log(hwfc, AV_LOG_ERROR, "Failed to map image from surface "
837 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
842 err = ff_hwframe_map_create(src->hw_frames_ctx,
843 dst, src, &vaapi_unmap_frame, map);
847 dst->width = src->width;
848 dst->height = src->height;
850 for (i = 0; i < map->image.num_planes; i++) {
851 dst->data[i] = (uint8_t*)address + map->image.offsets[i];
852 dst->linesize[i] = map->image.pitches[i];
855 desc = vaapi_format_from_fourcc(map->image.format.fourcc);
856 if (desc && desc->chroma_planes_swapped) {
857 // Chroma planes are YVU rather than YUV, so swap them.
858 FFSWAP(uint8_t*, dst->data[1], dst->data[2]);
866 vaUnmapBuffer(hwctx->display, map->image.buf);
867 if (map->image.image_id != VA_INVALID_ID)
868 vaDestroyImage(hwctx->display, map->image.image_id);
874 static int vaapi_transfer_data_from(AVHWFramesContext *hwfc,
875 AVFrame *dst, const AVFrame *src)
880 if (dst->width > hwfc->width || dst->height > hwfc->height)
881 return AVERROR(EINVAL);
883 map = av_frame_alloc();
885 return AVERROR(ENOMEM);
886 map->format = dst->format;
888 err = vaapi_map_frame(hwfc, map, src, AV_HWFRAME_MAP_READ);
892 map->width = dst->width;
893 map->height = dst->height;
895 err = av_frame_copy(dst, map);
905 static int vaapi_transfer_data_to(AVHWFramesContext *hwfc,
906 AVFrame *dst, const AVFrame *src)
911 if (src->width > hwfc->width || src->height > hwfc->height)
912 return AVERROR(EINVAL);
914 map = av_frame_alloc();
916 return AVERROR(ENOMEM);
917 map->format = src->format;
919 err = vaapi_map_frame(hwfc, map, dst, AV_HWFRAME_MAP_WRITE | AV_HWFRAME_MAP_OVERWRITE);
923 map->width = src->width;
924 map->height = src->height;
926 err = av_frame_copy(map, src);
936 static int vaapi_map_to_memory(AVHWFramesContext *hwfc, AVFrame *dst,
937 const AVFrame *src, int flags)
941 if (dst->format != AV_PIX_FMT_NONE) {
942 err = vaapi_get_image_format(hwfc->device_ctx, dst->format, NULL);
944 return AVERROR(ENOSYS);
947 err = vaapi_map_frame(hwfc, dst, src, flags);
951 err = av_frame_copy_props(dst, src);
960 #define DRM_MAP(va, layers, ...) { \
965 static const struct {
967 int nb_layer_formats;
968 uint32_t layer_formats[AV_DRM_MAX_PLANES];
969 } vaapi_drm_format_map[] = {
971 DRM_MAP(NV12, 2, DRM_FORMAT_R8, DRM_FORMAT_RG88),
973 DRM_MAP(NV12, 1, DRM_FORMAT_NV12),
974 #if defined(VA_FOURCC_P010) && defined(DRM_FORMAT_R16)
975 DRM_MAP(P010, 2, DRM_FORMAT_R16, DRM_FORMAT_RG1616),
977 DRM_MAP(BGRA, 1, DRM_FORMAT_ARGB8888),
978 DRM_MAP(BGRX, 1, DRM_FORMAT_XRGB8888),
979 DRM_MAP(RGBA, 1, DRM_FORMAT_ABGR8888),
980 DRM_MAP(RGBX, 1, DRM_FORMAT_XBGR8888),
981 #ifdef VA_FOURCC_ABGR
982 DRM_MAP(ABGR, 1, DRM_FORMAT_RGBA8888),
983 DRM_MAP(XBGR, 1, DRM_FORMAT_RGBX8888),
985 DRM_MAP(ARGB, 1, DRM_FORMAT_BGRA8888),
986 DRM_MAP(XRGB, 1, DRM_FORMAT_BGRX8888),
990 static void vaapi_unmap_from_drm(AVHWFramesContext *dst_fc,
991 HWMapDescriptor *hwmap)
993 AVVAAPIDeviceContext *dst_dev = dst_fc->device_ctx->hwctx;
995 VASurfaceID surface_id = (VASurfaceID)(uintptr_t)hwmap->priv;
997 av_log(dst_fc, AV_LOG_DEBUG, "Destroy surface %#x.\n", surface_id);
999 vaDestroySurfaces(dst_dev->display, &surface_id, 1);
1002 static int vaapi_map_from_drm(AVHWFramesContext *src_fc, AVFrame *dst,
1003 const AVFrame *src, int flags)
1005 AVHWFramesContext *dst_fc =
1006 (AVHWFramesContext*)dst->hw_frames_ctx->data;
1007 AVVAAPIDeviceContext *dst_dev = dst_fc->device_ctx->hwctx;
1008 const AVDRMFrameDescriptor *desc;
1009 const VAAPIFormatDescriptor *format_desc;
1010 VASurfaceID surface_id;
1015 unsigned long buffer_handle;
1016 VASurfaceAttribExternalBuffers buffer_desc;
1017 VASurfaceAttrib attrs[2] = {
1019 .type = VASurfaceAttribMemoryType,
1020 .flags = VA_SURFACE_ATTRIB_SETTABLE,
1021 .value.type = VAGenericValueTypeInteger,
1022 .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME,
1025 .type = VASurfaceAttribExternalBufferDescriptor,
1026 .flags = VA_SURFACE_ATTRIB_SETTABLE,
1027 .value.type = VAGenericValueTypePointer,
1028 .value.value.p = &buffer_desc,
1032 desc = (AVDRMFrameDescriptor*)src->data[0];
1034 if (desc->nb_objects != 1) {
1035 av_log(dst_fc, AV_LOG_ERROR, "VAAPI can only map frames "
1036 "made from a single DRM object.\n");
1037 return AVERROR(EINVAL);
1041 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_drm_format_map); i++) {
1042 if (desc->nb_layers != vaapi_drm_format_map[i].nb_layer_formats)
1044 for (j = 0; j < desc->nb_layers; j++) {
1045 if (desc->layers[j].format !=
1046 vaapi_drm_format_map[i].layer_formats[j])
1049 if (j != desc->nb_layers)
1051 va_fourcc = vaapi_drm_format_map[i].va_fourcc;
1055 av_log(dst_fc, AV_LOG_ERROR, "DRM format not supported "
1057 return AVERROR(EINVAL);
1060 av_log(dst_fc, AV_LOG_DEBUG, "Map DRM object %d to VAAPI as "
1061 "%08x.\n", desc->objects[0].fd, va_fourcc);
1063 format_desc = vaapi_format_from_fourcc(va_fourcc);
1064 av_assert0(format_desc);
1066 buffer_handle = desc->objects[0].fd;
1067 buffer_desc.pixel_format = va_fourcc;
1068 buffer_desc.width = src_fc->width;
1069 buffer_desc.height = src_fc->height;
1070 buffer_desc.data_size = desc->objects[0].size;
1071 buffer_desc.buffers = &buffer_handle;
1072 buffer_desc.num_buffers = 1;
1073 buffer_desc.flags = 0;
1076 for (i = 0; i < desc->nb_layers; i++) {
1077 for (j = 0; j < desc->layers[i].nb_planes; j++) {
1078 buffer_desc.pitches[k] = desc->layers[i].planes[j].pitch;
1079 buffer_desc.offsets[k] = desc->layers[i].planes[j].offset;
1083 buffer_desc.num_planes = k;
1085 if (format_desc->chroma_planes_swapped &&
1086 buffer_desc.num_planes == 3) {
1087 FFSWAP(uint32_t, buffer_desc.pitches[1], buffer_desc.pitches[2]);
1088 FFSWAP(uint32_t, buffer_desc.offsets[1], buffer_desc.offsets[2]);
1091 vas = vaCreateSurfaces(dst_dev->display, format_desc->rt_format,
1092 src->width, src->height,
1094 attrs, FF_ARRAY_ELEMS(attrs));
1095 if (vas != VA_STATUS_SUCCESS) {
1096 av_log(dst_fc, AV_LOG_ERROR, "Failed to create surface from DRM "
1097 "object: %d (%s).\n", vas, vaErrorStr(vas));
1098 return AVERROR(EIO);
1100 av_log(dst_fc, AV_LOG_DEBUG, "Create surface %#x.\n", surface_id);
1102 err = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src,
1103 &vaapi_unmap_from_drm,
1104 (void*)(uintptr_t)surface_id);
1108 dst->width = src->width;
1109 dst->height = src->height;
1110 dst->data[3] = (uint8_t*)(uintptr_t)surface_id;
1112 av_log(dst_fc, AV_LOG_DEBUG, "Mapped DRM object %d to "
1113 "surface %#x.\n", desc->objects[0].fd, surface_id);
1118 #if VA_CHECK_VERSION(1, 1, 0)
1119 static void vaapi_unmap_to_drm_esh(AVHWFramesContext *hwfc,
1120 HWMapDescriptor *hwmap)
1122 AVDRMFrameDescriptor *drm_desc = hwmap->priv;
1125 for (i = 0; i < drm_desc->nb_objects; i++)
1126 close(drm_desc->objects[i].fd);
1128 av_freep(&drm_desc);
1131 static int vaapi_map_to_drm_esh(AVHWFramesContext *hwfc, AVFrame *dst,
1132 const AVFrame *src, int flags)
1134 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1135 VASurfaceID surface_id;
1137 VADRMPRIMESurfaceDescriptor va_desc;
1138 AVDRMFrameDescriptor *drm_desc = NULL;
1139 uint32_t export_flags;
1142 surface_id = (VASurfaceID)(uintptr_t)src->data[3];
1144 export_flags = VA_EXPORT_SURFACE_SEPARATE_LAYERS;
1145 if (flags & AV_HWFRAME_MAP_READ)
1146 export_flags |= VA_EXPORT_SURFACE_READ_ONLY;
1147 if (flags & AV_HWFRAME_MAP_WRITE)
1148 export_flags |= VA_EXPORT_SURFACE_WRITE_ONLY;
1150 vas = vaExportSurfaceHandle(hwctx->display, surface_id,
1151 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2,
1152 export_flags, &va_desc);
1153 if (vas != VA_STATUS_SUCCESS) {
1154 if (vas == VA_STATUS_ERROR_UNIMPLEMENTED)
1155 return AVERROR(ENOSYS);
1156 av_log(hwfc, AV_LOG_ERROR, "Failed to export surface %#x: "
1157 "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
1158 return AVERROR(EIO);
1161 drm_desc = av_mallocz(sizeof(*drm_desc));
1163 err = AVERROR(ENOMEM);
1167 // By some bizarre coincidence, these structures are very similar...
1168 drm_desc->nb_objects = va_desc.num_objects;
1169 for (i = 0; i < va_desc.num_objects; i++) {
1170 drm_desc->objects[i].fd = va_desc.objects[i].fd;
1171 drm_desc->objects[i].size = va_desc.objects[i].size;
1172 drm_desc->objects[i].format_modifier =
1173 va_desc.objects[i].drm_format_modifier;
1175 drm_desc->nb_layers = va_desc.num_layers;
1176 for (i = 0; i < va_desc.num_layers; i++) {
1177 drm_desc->layers[i].format = va_desc.layers[i].drm_format;
1178 drm_desc->layers[i].nb_planes = va_desc.layers[i].num_planes;
1179 for (j = 0; j < va_desc.layers[i].num_planes; j++) {
1180 drm_desc->layers[i].planes[j].object_index =
1181 va_desc.layers[i].object_index[j];
1182 drm_desc->layers[i].planes[j].offset =
1183 va_desc.layers[i].offset[j];
1184 drm_desc->layers[i].planes[j].pitch =
1185 va_desc.layers[i].pitch[j];
1189 err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
1190 &vaapi_unmap_to_drm_esh, drm_desc);
1194 dst->width = src->width;
1195 dst->height = src->height;
1196 dst->data[0] = (uint8_t*)drm_desc;
1201 for (i = 0; i < va_desc.num_objects; i++)
1202 close(va_desc.objects[i].fd);
1203 av_freep(&drm_desc);
1208 #if VA_CHECK_VERSION(0, 36, 0)
1209 typedef struct VAAPIDRMImageBufferMapping {
1211 VABufferInfo buffer_info;
1213 AVDRMFrameDescriptor drm_desc;
1214 } VAAPIDRMImageBufferMapping;
1216 static void vaapi_unmap_to_drm_abh(AVHWFramesContext *hwfc,
1217 HWMapDescriptor *hwmap)
1219 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1220 VAAPIDRMImageBufferMapping *mapping = hwmap->priv;
1221 VASurfaceID surface_id;
1224 surface_id = (VASurfaceID)(uintptr_t)hwmap->source->data[3];
1225 av_log(hwfc, AV_LOG_DEBUG, "Unmap VAAPI surface %#x from DRM.\n",
1228 // DRM PRIME file descriptors are closed by vaReleaseBufferHandle(),
1229 // so we shouldn't close them separately.
1231 vas = vaReleaseBufferHandle(hwctx->display, mapping->image.buf);
1232 if (vas != VA_STATUS_SUCCESS) {
1233 av_log(hwfc, AV_LOG_ERROR, "Failed to release buffer "
1234 "handle of image %#x (derived from surface %#x): "
1235 "%d (%s).\n", mapping->image.buf, surface_id,
1236 vas, vaErrorStr(vas));
1239 vas = vaDestroyImage(hwctx->display, mapping->image.image_id);
1240 if (vas != VA_STATUS_SUCCESS) {
1241 av_log(hwfc, AV_LOG_ERROR, "Failed to destroy image "
1242 "derived from surface %#x: %d (%s).\n",
1243 surface_id, vas, vaErrorStr(vas));
1249 static int vaapi_map_to_drm_abh(AVHWFramesContext *hwfc, AVFrame *dst,
1250 const AVFrame *src, int flags)
1252 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1253 VAAPIDRMImageBufferMapping *mapping = NULL;
1254 VASurfaceID surface_id;
1258 surface_id = (VASurfaceID)(uintptr_t)src->data[3];
1259 av_log(hwfc, AV_LOG_DEBUG, "Map VAAPI surface %#x to DRM.\n",
1262 mapping = av_mallocz(sizeof(*mapping));
1264 return AVERROR(ENOMEM);
1266 vas = vaDeriveImage(hwctx->display, surface_id,
1268 if (vas != VA_STATUS_SUCCESS) {
1269 av_log(hwfc, AV_LOG_ERROR, "Failed to derive image from "
1270 "surface %#x: %d (%s).\n",
1271 surface_id, vas, vaErrorStr(vas));
1276 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_drm_format_map); i++) {
1277 if (vaapi_drm_format_map[i].va_fourcc ==
1278 mapping->image.format.fourcc)
1281 if (i >= FF_ARRAY_ELEMS(vaapi_drm_format_map)) {
1282 av_log(hwfc, AV_LOG_ERROR, "No matching DRM format for "
1283 "VAAPI format %#x.\n", mapping->image.format.fourcc);
1284 err = AVERROR(EINVAL);
1288 mapping->buffer_info.mem_type =
1289 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
1291 mapping->drm_desc.nb_layers =
1292 vaapi_drm_format_map[i].nb_layer_formats;
1293 if (mapping->drm_desc.nb_layers > 1) {
1294 if (mapping->drm_desc.nb_layers != mapping->image.num_planes) {
1295 av_log(hwfc, AV_LOG_ERROR, "Image properties do not match "
1296 "expected format: got %d planes, but expected %d.\n",
1297 mapping->image.num_planes, mapping->drm_desc.nb_layers);
1298 err = AVERROR(EINVAL);
1302 for(p = 0; p < mapping->drm_desc.nb_layers; p++) {
1303 mapping->drm_desc.layers[p] = (AVDRMLayerDescriptor) {
1304 .format = vaapi_drm_format_map[i].layer_formats[p],
1308 .offset = mapping->image.offsets[p],
1309 .pitch = mapping->image.pitches[p],
1314 mapping->drm_desc.layers[0].format =
1315 vaapi_drm_format_map[i].layer_formats[0];
1316 mapping->drm_desc.layers[0].nb_planes = mapping->image.num_planes;
1317 for (p = 0; p < mapping->image.num_planes; p++) {
1318 mapping->drm_desc.layers[0].planes[p] = (AVDRMPlaneDescriptor) {
1320 .offset = mapping->image.offsets[p],
1321 .pitch = mapping->image.pitches[p],
1326 vas = vaAcquireBufferHandle(hwctx->display, mapping->image.buf,
1327 &mapping->buffer_info);
1328 if (vas != VA_STATUS_SUCCESS) {
1329 av_log(hwfc, AV_LOG_ERROR, "Failed to get buffer "
1330 "handle from image %#x (derived from surface %#x): "
1331 "%d (%s).\n", mapping->image.buf, surface_id,
1332 vas, vaErrorStr(vas));
1337 av_log(hwfc, AV_LOG_DEBUG, "DRM PRIME fd is %ld.\n",
1338 mapping->buffer_info.handle);
1340 mapping->drm_desc.nb_objects = 1;
1341 mapping->drm_desc.objects[0] = (AVDRMObjectDescriptor) {
1342 .fd = mapping->buffer_info.handle,
1343 .size = mapping->image.data_size,
1344 // There is no way to get the format modifier with this API.
1345 .format_modifier = DRM_FORMAT_MOD_INVALID,
1348 err = ff_hwframe_map_create(src->hw_frames_ctx,
1349 dst, src, &vaapi_unmap_to_drm_abh,
1354 dst->data[0] = (uint8_t*)&mapping->drm_desc;
1355 dst->width = src->width;
1356 dst->height = src->height;
1361 vaReleaseBufferHandle(hwctx->display, mapping->image.buf);
1363 vaDestroyImage(hwctx->display, mapping->image.image_id);
1370 static int vaapi_map_to_drm(AVHWFramesContext *hwfc, AVFrame *dst,
1371 const AVFrame *src, int flags)
1373 #if VA_CHECK_VERSION(1, 1, 0)
1375 err = vaapi_map_to_drm_esh(hwfc, dst, src, flags);
1376 if (err != AVERROR(ENOSYS))
1379 #if VA_CHECK_VERSION(0, 36, 0)
1380 return vaapi_map_to_drm_abh(hwfc, dst, src, flags);
1382 return AVERROR(ENOSYS);
1385 #endif /* CONFIG_LIBDRM */
1387 static int vaapi_map_to(AVHWFramesContext *hwfc, AVFrame *dst,
1388 const AVFrame *src, int flags)
1390 switch (src->format) {
1392 case AV_PIX_FMT_DRM_PRIME:
1393 return vaapi_map_from_drm(hwfc, dst, src, flags);
1396 return AVERROR(ENOSYS);
1400 static int vaapi_map_from(AVHWFramesContext *hwfc, AVFrame *dst,
1401 const AVFrame *src, int flags)
1403 switch (dst->format) {
1405 case AV_PIX_FMT_DRM_PRIME:
1406 return vaapi_map_to_drm(hwfc, dst, src, flags);
1409 return vaapi_map_to_memory(hwfc, dst, src, flags);
1413 static void vaapi_device_free(AVHWDeviceContext *ctx)
1415 AVVAAPIDeviceContext *hwctx = ctx->hwctx;
1416 VAAPIDevicePriv *priv = ctx->user_opaque;
1419 vaTerminate(hwctx->display);
1422 if (priv->x11_display)
1423 XCloseDisplay(priv->x11_display);
1426 if (priv->drm_fd >= 0)
1427 close(priv->drm_fd);
1433 static void vaapi_device_log_error(void *context, const char *message)
1435 AVHWDeviceContext *ctx = context;
1437 av_log(ctx, AV_LOG_ERROR, "libva: %s", message);
1440 static void vaapi_device_log_info(void *context, const char *message)
1442 AVHWDeviceContext *ctx = context;
1444 av_log(ctx, AV_LOG_VERBOSE, "libva: %s", message);
1448 static int vaapi_device_connect(AVHWDeviceContext *ctx,
1451 AVVAAPIDeviceContext *hwctx = ctx->hwctx;
1456 vaSetErrorCallback(display, &vaapi_device_log_error, ctx);
1457 vaSetInfoCallback (display, &vaapi_device_log_info, ctx);
1460 hwctx->display = display;
1462 vas = vaInitialize(display, &major, &minor);
1463 if (vas != VA_STATUS_SUCCESS) {
1464 av_log(ctx, AV_LOG_ERROR, "Failed to initialise VAAPI "
1465 "connection: %d (%s).\n", vas, vaErrorStr(vas));
1466 return AVERROR(EIO);
1468 av_log(ctx, AV_LOG_VERBOSE, "Initialised VAAPI connection: "
1469 "version %d.%d\n", major, minor);
1474 static int vaapi_device_create(AVHWDeviceContext *ctx, const char *device,
1475 AVDictionary *opts, int flags)
1477 VAAPIDevicePriv *priv;
1478 VADisplay display = NULL;
1479 const AVDictionaryEntry *ent;
1480 int try_drm, try_x11, try_all;
1482 priv = av_mallocz(sizeof(*priv));
1484 return AVERROR(ENOMEM);
1488 ctx->user_opaque = priv;
1489 ctx->free = vaapi_device_free;
1491 ent = av_dict_get(opts, "connection_type", NULL, 0);
1493 try_all = try_drm = try_x11 = 0;
1494 if (!strcmp(ent->value, "drm")) {
1496 } else if (!strcmp(ent->value, "x11")) {
1499 av_log(ctx, AV_LOG_ERROR, "Invalid connection type %s.\n",
1501 return AVERROR(EINVAL);
1505 try_drm = HAVE_VAAPI_DRM;
1506 try_x11 = HAVE_VAAPI_X11;
1510 while (!display && try_drm) {
1511 // If the device is specified, try to open it as a DRM device node.
1512 // If not, look for a usable render node, possibly restricted to those
1513 // using a specified kernel driver.
1514 int loglevel = try_all ? AV_LOG_VERBOSE : AV_LOG_ERROR;
1516 priv->drm_fd = open(device, O_RDWR);
1517 if (priv->drm_fd < 0) {
1518 av_log(ctx, loglevel, "Failed to open %s as "
1519 "DRM device node.\n", device);
1524 int n, max_devices = 8;
1526 const AVDictionaryEntry *kernel_driver;
1527 kernel_driver = av_dict_get(opts, "kernel_driver", NULL, 0);
1529 for (n = 0; n < max_devices; n++) {
1530 snprintf(path, sizeof(path),
1531 "/dev/dri/renderD%d", 128 + n);
1532 priv->drm_fd = open(path, O_RDWR);
1533 if (priv->drm_fd < 0) {
1534 av_log(ctx, AV_LOG_VERBOSE, "Cannot open "
1535 "DRM render node for device %d.\n", n);
1539 if (kernel_driver) {
1541 info = drmGetVersion(priv->drm_fd);
1542 if (strcmp(kernel_driver->value, info->name)) {
1543 av_log(ctx, AV_LOG_VERBOSE, "Ignoring device %d "
1544 "with non-matching kernel driver (%s).\n",
1546 drmFreeVersion(info);
1547 close(priv->drm_fd);
1551 av_log(ctx, AV_LOG_VERBOSE, "Trying to use "
1552 "DRM render node for device %d, "
1553 "with matching kernel driver (%s).\n",
1555 drmFreeVersion(info);
1559 av_log(ctx, AV_LOG_VERBOSE, "Trying to use "
1560 "DRM render node for device %d.\n", n);
1564 if (n >= max_devices)
1568 display = vaGetDisplayDRM(priv->drm_fd);
1570 av_log(ctx, AV_LOG_VERBOSE, "Cannot open a VA display "
1571 "from DRM device %s.\n", device);
1572 return AVERROR_EXTERNAL;
1579 if (!display && try_x11) {
1580 // Try to open the device as an X11 display.
1581 priv->x11_display = XOpenDisplay(device);
1582 if (!priv->x11_display) {
1583 av_log(ctx, AV_LOG_VERBOSE, "Cannot open X11 display "
1584 "%s.\n", XDisplayName(device));
1586 display = vaGetDisplay(priv->x11_display);
1588 av_log(ctx, AV_LOG_ERROR, "Cannot open a VA display "
1589 "from X11 display %s.\n", XDisplayName(device));
1590 return AVERROR_UNKNOWN;
1593 av_log(ctx, AV_LOG_VERBOSE, "Opened VA display via "
1594 "X11 display %s.\n", XDisplayName(device));
1601 av_log(ctx, AV_LOG_ERROR, "No VA display found for "
1602 "device %s.\n", device);
1604 av_log(ctx, AV_LOG_ERROR, "No VA display found for "
1605 "any default device.\n");
1606 return AVERROR(EINVAL);
1609 ent = av_dict_get(opts, "driver", NULL, 0);
1611 #if VA_CHECK_VERSION(0, 38, 0)
1613 vas = vaSetDriverName(display, ent->value);
1614 if (vas != VA_STATUS_SUCCESS) {
1615 av_log(ctx, AV_LOG_ERROR, "Failed to set driver name to "
1616 "%s: %d (%s).\n", ent->value, vas, vaErrorStr(vas));
1617 vaTerminate(display);
1618 return AVERROR_EXTERNAL;
1621 av_log(ctx, AV_LOG_WARNING, "Driver name setting is not "
1622 "supported with this VAAPI version.\n");
1626 return vaapi_device_connect(ctx, display);
1629 static int vaapi_device_derive(AVHWDeviceContext *ctx,
1630 AVHWDeviceContext *src_ctx,
1631 AVDictionary *opts, int flags)
1634 if (src_ctx->type == AV_HWDEVICE_TYPE_DRM) {
1635 AVDRMDeviceContext *src_hwctx = src_ctx->hwctx;
1637 VAAPIDevicePriv *priv;
1640 if (src_hwctx->fd < 0) {
1641 av_log(ctx, AV_LOG_ERROR, "DRM instance requires an associated "
1642 "device to derive a VA display from.\n");
1643 return AVERROR(EINVAL);
1648 int node_type = drmGetNodeTypeFromFd(src_hwctx->fd);
1650 if (node_type < 0) {
1651 av_log(ctx, AV_LOG_ERROR, "DRM instance fd does not appear "
1652 "to refer to a DRM device.\n");
1653 return AVERROR(EINVAL);
1655 if (node_type == DRM_NODE_RENDER) {
1658 render_node = drmGetRenderDeviceNameFromFd(src_hwctx->fd);
1660 av_log(ctx, AV_LOG_ERROR, "Failed to find a render node "
1661 "matching the DRM device.\n");
1662 return AVERROR(ENODEV);
1664 fd = open(render_node, O_RDWR);
1666 av_log(ctx, AV_LOG_ERROR, "Failed to open render node %s"
1667 "matching the DRM device.\n", render_node);
1669 return AVERROR(errno);
1671 av_log(ctx, AV_LOG_VERBOSE, "Using render node %s in place "
1672 "of non-render DRM device.\n", render_node);
1680 priv = av_mallocz(sizeof(*priv));
1682 return AVERROR(ENOMEM);
1684 if (fd == src_hwctx->fd) {
1685 // The fd is inherited from the source context and we are holding
1686 // a reference to that, we don't want to close it from here.
1692 ctx->user_opaque = priv;
1693 ctx->free = &vaapi_device_free;
1695 display = vaGetDisplayDRM(fd);
1697 av_log(ctx, AV_LOG_ERROR, "Failed to open a VA display from "
1699 return AVERROR(EIO);
1702 return vaapi_device_connect(ctx, display);
1705 return AVERROR(ENOSYS);
1708 const HWContextType ff_hwcontext_type_vaapi = {
1709 .type = AV_HWDEVICE_TYPE_VAAPI,
1712 .device_hwctx_size = sizeof(AVVAAPIDeviceContext),
1713 .device_priv_size = sizeof(VAAPIDeviceContext),
1714 .device_hwconfig_size = sizeof(AVVAAPIHWConfig),
1715 .frames_hwctx_size = sizeof(AVVAAPIFramesContext),
1716 .frames_priv_size = sizeof(VAAPIFramesContext),
1718 .device_create = &vaapi_device_create,
1719 .device_derive = &vaapi_device_derive,
1720 .device_init = &vaapi_device_init,
1721 .device_uninit = &vaapi_device_uninit,
1722 .frames_get_constraints = &vaapi_frames_get_constraints,
1723 .frames_init = &vaapi_frames_init,
1724 .frames_uninit = &vaapi_frames_uninit,
1725 .frames_get_buffer = &vaapi_get_buffer,
1726 .transfer_get_formats = &vaapi_transfer_get_formats,
1727 .transfer_data_to = &vaapi_transfer_data_to,
1728 .transfer_data_from = &vaapi_transfer_data_from,
1729 .map_to = &vaapi_map_to,
1730 .map_from = &vaapi_map_from,
1732 .pix_fmts = (const enum AVPixelFormat[]) {