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++) {
273 if (attr_list[i].type != VASurfaceAttribPixelFormat)
275 fourcc = attr_list[i].value.value.i;
276 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
278 if (pix_fmt == AV_PIX_FMT_NONE)
281 for (k = 0; k < j; k++) {
282 if (constraints->valid_sw_formats[k] == pix_fmt)
287 constraints->valid_sw_formats[j++] = pix_fmt;
289 constraints->valid_sw_formats[j] = AV_PIX_FMT_NONE;
292 // No configuration supplied.
293 // Return the full set of image formats known by the implementation.
294 constraints->valid_sw_formats = av_malloc_array(ctx->nb_formats + 1,
296 if (!constraints->valid_sw_formats) {
297 err = AVERROR(ENOMEM);
300 for (i = j = 0; i < ctx->nb_formats; i++) {
303 for (k = 0; k < j; k++) {
304 if (constraints->valid_sw_formats[k] == ctx->formats[i].pix_fmt)
309 constraints->valid_sw_formats[j++] = ctx->formats[i].pix_fmt;
312 constraints->valid_sw_formats[j] = AV_PIX_FMT_NONE;
315 constraints->valid_hw_formats = av_malloc_array(2, sizeof(pix_fmt));
316 if (!constraints->valid_hw_formats) {
317 err = AVERROR(ENOMEM);
320 constraints->valid_hw_formats[0] = AV_PIX_FMT_VAAPI;
321 constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
325 av_freep(&attr_list);
329 static const struct {
330 const char *friendly_name;
331 const char *match_string;
333 } vaapi_driver_quirks_table[] = {
334 #if !VA_CHECK_VERSION(1, 0, 0)
335 // The i965 driver did not conform before version 2.0.
337 "Intel i965 (Quick Sync)",
339 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS,
345 AV_VAAPI_DRIVER_QUIRK_ATTRIB_MEMTYPE,
349 "Splitted-Desktop Systems VDPAU backend for VA-API",
350 AV_VAAPI_DRIVER_QUIRK_SURFACE_ATTRIBUTES,
354 static int vaapi_device_init(AVHWDeviceContext *hwdev)
356 VAAPIDeviceContext *ctx = hwdev->internal->priv;
357 AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
358 VAImageFormat *image_list = NULL;
360 const char *vendor_string;
361 int err, i, image_count;
362 enum AVPixelFormat pix_fmt;
365 image_count = vaMaxNumImageFormats(hwctx->display);
366 if (image_count <= 0) {
370 image_list = av_malloc(image_count * sizeof(*image_list));
372 err = AVERROR(ENOMEM);
375 vas = vaQueryImageFormats(hwctx->display, image_list, &image_count);
376 if (vas != VA_STATUS_SUCCESS) {
381 ctx->formats = av_malloc(image_count * sizeof(*ctx->formats));
383 err = AVERROR(ENOMEM);
387 for (i = 0; i < image_count; i++) {
388 fourcc = image_list[i].fourcc;
389 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
390 if (pix_fmt == AV_PIX_FMT_NONE) {
391 av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> unknown.\n",
394 av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> %s.\n",
395 fourcc, av_get_pix_fmt_name(pix_fmt));
396 ctx->formats[ctx->nb_formats].pix_fmt = pix_fmt;
397 ctx->formats[ctx->nb_formats].image_format = image_list[i];
402 vendor_string = vaQueryVendorString(hwctx->display);
404 av_log(hwdev, AV_LOG_VERBOSE, "VAAPI driver: %s.\n", vendor_string);
406 if (hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_USER_SET) {
407 av_log(hwdev, AV_LOG_VERBOSE, "Using quirks set by user (%#x).\n",
408 hwctx->driver_quirks);
410 // Detect the driver in use and set quirk flags if necessary.
411 hwctx->driver_quirks = 0;
413 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_driver_quirks_table); i++) {
414 if (strstr(vendor_string,
415 vaapi_driver_quirks_table[i].match_string)) {
416 av_log(hwdev, AV_LOG_VERBOSE, "Matched driver string "
417 "as known nonstandard driver \"%s\", setting "
419 vaapi_driver_quirks_table[i].friendly_name,
420 vaapi_driver_quirks_table[i].quirks);
421 hwctx->driver_quirks |=
422 vaapi_driver_quirks_table[i].quirks;
426 if (!(i < FF_ARRAY_ELEMS(vaapi_driver_quirks_table))) {
427 av_log(hwdev, AV_LOG_VERBOSE, "Driver not found in known "
428 "nonstandard list, using standard behaviour.\n");
431 av_log(hwdev, AV_LOG_VERBOSE, "Driver has no vendor string, "
432 "assuming standard behaviour.\n");
439 av_freep(&ctx->formats);
444 static void vaapi_device_uninit(AVHWDeviceContext *hwdev)
446 VAAPIDeviceContext *ctx = hwdev->internal->priv;
448 av_freep(&ctx->formats);
451 static void vaapi_buffer_free(void *opaque, uint8_t *data)
453 AVHWFramesContext *hwfc = opaque;
454 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
455 VASurfaceID surface_id;
458 surface_id = (VASurfaceID)(uintptr_t)data;
460 vas = vaDestroySurfaces(hwctx->display, &surface_id, 1);
461 if (vas != VA_STATUS_SUCCESS) {
462 av_log(hwfc, AV_LOG_ERROR, "Failed to destroy surface %#x: "
463 "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
467 static AVBufferRef *vaapi_pool_alloc(void *opaque, int size)
469 AVHWFramesContext *hwfc = opaque;
470 VAAPIFramesContext *ctx = hwfc->internal->priv;
471 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
472 AVVAAPIFramesContext *avfc = hwfc->hwctx;
473 VASurfaceID surface_id;
477 if (hwfc->initial_pool_size > 0 &&
478 avfc->nb_surfaces >= hwfc->initial_pool_size)
481 vas = vaCreateSurfaces(hwctx->display, ctx->rt_format,
482 hwfc->width, hwfc->height,
484 ctx->attributes, ctx->nb_attributes);
485 if (vas != VA_STATUS_SUCCESS) {
486 av_log(hwfc, AV_LOG_ERROR, "Failed to create surface: "
487 "%d (%s).\n", vas, vaErrorStr(vas));
490 av_log(hwfc, AV_LOG_DEBUG, "Created surface %#x.\n", surface_id);
492 ref = av_buffer_create((uint8_t*)(uintptr_t)surface_id,
493 sizeof(surface_id), &vaapi_buffer_free,
494 hwfc, AV_BUFFER_FLAG_READONLY);
496 vaDestroySurfaces(hwctx->display, &surface_id, 1);
500 if (hwfc->initial_pool_size > 0) {
501 // This is a fixed-size pool, so we must still be in the initial
502 // allocation sequence.
503 av_assert0(avfc->nb_surfaces < hwfc->initial_pool_size);
504 avfc->surface_ids[avfc->nb_surfaces] = surface_id;
511 static int vaapi_frames_init(AVHWFramesContext *hwfc)
513 AVVAAPIFramesContext *avfc = hwfc->hwctx;
514 VAAPIFramesContext *ctx = hwfc->internal->priv;
515 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
516 const VAAPIFormatDescriptor *desc;
517 VAImageFormat *expected_format;
518 AVBufferRef *test_surface = NULL;
519 VASurfaceID test_surface_id;
524 desc = vaapi_format_from_pix_fmt(hwfc->sw_format);
526 av_log(hwfc, AV_LOG_ERROR, "Unsupported format: %s.\n",
527 av_get_pix_fmt_name(hwfc->sw_format));
528 return AVERROR(EINVAL);
532 if (!(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_SURFACE_ATTRIBUTES)) {
533 int need_memory_type = !(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_ATTRIB_MEMTYPE);
534 int need_pixel_format = 1;
535 for (i = 0; i < avfc->nb_attributes; i++) {
536 if (avfc->attributes[i].type == VASurfaceAttribMemoryType)
537 need_memory_type = 0;
538 if (avfc->attributes[i].type == VASurfaceAttribPixelFormat)
539 need_pixel_format = 0;
542 avfc->nb_attributes + need_memory_type + need_pixel_format;
544 ctx->attributes = av_malloc(ctx->nb_attributes *
545 sizeof(*ctx->attributes));
546 if (!ctx->attributes) {
547 err = AVERROR(ENOMEM);
551 for (i = 0; i < avfc->nb_attributes; i++)
552 ctx->attributes[i] = avfc->attributes[i];
553 if (need_memory_type) {
554 ctx->attributes[i++] = (VASurfaceAttrib) {
555 .type = VASurfaceAttribMemoryType,
556 .flags = VA_SURFACE_ATTRIB_SETTABLE,
557 .value.type = VAGenericValueTypeInteger,
558 .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA,
561 if (need_pixel_format) {
562 ctx->attributes[i++] = (VASurfaceAttrib) {
563 .type = VASurfaceAttribPixelFormat,
564 .flags = VA_SURFACE_ATTRIB_SETTABLE,
565 .value.type = VAGenericValueTypeInteger,
566 .value.value.i = desc->fourcc,
569 av_assert0(i == ctx->nb_attributes);
571 ctx->attributes = NULL;
572 ctx->nb_attributes = 0;
575 ctx->rt_format = desc->rt_format;
577 if (hwfc->initial_pool_size > 0) {
578 // This pool will be usable as a render target, so we need to store
579 // all of the surface IDs somewhere that vaCreateContext() calls
580 // will be able to access them.
581 avfc->nb_surfaces = 0;
582 avfc->surface_ids = av_malloc(hwfc->initial_pool_size *
583 sizeof(*avfc->surface_ids));
584 if (!avfc->surface_ids) {
585 err = AVERROR(ENOMEM);
589 // This pool allows dynamic sizing, and will not be usable as a
591 avfc->nb_surfaces = 0;
592 avfc->surface_ids = NULL;
595 hwfc->internal->pool_internal =
596 av_buffer_pool_init2(sizeof(VASurfaceID), hwfc,
597 &vaapi_pool_alloc, NULL);
598 if (!hwfc->internal->pool_internal) {
599 av_log(hwfc, AV_LOG_ERROR, "Failed to create VAAPI surface pool.\n");
600 err = AVERROR(ENOMEM);
605 // Allocate a single surface to test whether vaDeriveImage() is going
606 // to work for the specific configuration.
608 test_surface = av_buffer_pool_get(hwfc->pool);
610 av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
611 "user-configured buffer pool.\n");
612 err = AVERROR(ENOMEM);
616 test_surface = av_buffer_pool_get(hwfc->internal->pool_internal);
618 av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
619 "internal buffer pool.\n");
620 err = AVERROR(ENOMEM);
624 test_surface_id = (VASurfaceID)(uintptr_t)test_surface->data;
626 ctx->derive_works = 0;
628 err = vaapi_get_image_format(hwfc->device_ctx,
629 hwfc->sw_format, &expected_format);
631 vas = vaDeriveImage(hwctx->display, test_surface_id, &test_image);
632 if (vas == VA_STATUS_SUCCESS) {
633 if (expected_format->fourcc == test_image.format.fourcc) {
634 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping possible.\n");
635 ctx->derive_works = 1;
637 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
638 "derived image format %08x does not match "
639 "expected format %08x.\n",
640 expected_format->fourcc, test_image.format.fourcc);
642 vaDestroyImage(hwctx->display, test_image.image_id);
644 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
645 "deriving image does not work: "
646 "%d (%s).\n", vas, vaErrorStr(vas));
649 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
650 "image format is not supported.\n");
653 av_buffer_unref(&test_surface);
657 av_buffer_unref(&test_surface);
658 av_freep(&avfc->surface_ids);
659 av_freep(&ctx->attributes);
663 static void vaapi_frames_uninit(AVHWFramesContext *hwfc)
665 AVVAAPIFramesContext *avfc = hwfc->hwctx;
666 VAAPIFramesContext *ctx = hwfc->internal->priv;
668 av_freep(&avfc->surface_ids);
669 av_freep(&ctx->attributes);
672 static int vaapi_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
674 frame->buf[0] = av_buffer_pool_get(hwfc->pool);
676 return AVERROR(ENOMEM);
678 frame->data[3] = frame->buf[0]->data;
679 frame->format = AV_PIX_FMT_VAAPI;
680 frame->width = hwfc->width;
681 frame->height = hwfc->height;
686 static int vaapi_transfer_get_formats(AVHWFramesContext *hwfc,
687 enum AVHWFrameTransferDirection dir,
688 enum AVPixelFormat **formats)
690 VAAPIDeviceContext *ctx = hwfc->device_ctx->internal->priv;
691 enum AVPixelFormat *pix_fmts;
692 int i, k, sw_format_available;
694 sw_format_available = 0;
695 for (i = 0; i < ctx->nb_formats; i++) {
696 if (ctx->formats[i].pix_fmt == hwfc->sw_format)
697 sw_format_available = 1;
700 pix_fmts = av_malloc((ctx->nb_formats + 1) * sizeof(*pix_fmts));
702 return AVERROR(ENOMEM);
704 if (sw_format_available) {
705 pix_fmts[0] = hwfc->sw_format;
710 for (i = 0; i < ctx->nb_formats; i++) {
711 if (ctx->formats[i].pix_fmt == hwfc->sw_format)
713 av_assert0(k < ctx->nb_formats);
714 pix_fmts[k++] = ctx->formats[i].pix_fmt;
716 pix_fmts[k] = AV_PIX_FMT_NONE;
722 static void vaapi_unmap_frame(AVHWFramesContext *hwfc,
723 HWMapDescriptor *hwmap)
725 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
726 VAAPIMapping *map = hwmap->priv;
727 VASurfaceID surface_id;
730 surface_id = (VASurfaceID)(uintptr_t)hwmap->source->data[3];
731 av_log(hwfc, AV_LOG_DEBUG, "Unmap surface %#x.\n", surface_id);
733 vas = vaUnmapBuffer(hwctx->display, map->image.buf);
734 if (vas != VA_STATUS_SUCCESS) {
735 av_log(hwfc, AV_LOG_ERROR, "Failed to unmap image from surface "
736 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
739 if ((map->flags & AV_HWFRAME_MAP_WRITE) &&
740 !(map->flags & AV_HWFRAME_MAP_DIRECT)) {
741 vas = vaPutImage(hwctx->display, surface_id, map->image.image_id,
742 0, 0, hwfc->width, hwfc->height,
743 0, 0, hwfc->width, hwfc->height);
744 if (vas != VA_STATUS_SUCCESS) {
745 av_log(hwfc, AV_LOG_ERROR, "Failed to write image to surface "
746 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
750 vas = vaDestroyImage(hwctx->display, map->image.image_id);
751 if (vas != VA_STATUS_SUCCESS) {
752 av_log(hwfc, AV_LOG_ERROR, "Failed to destroy image from surface "
753 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
759 static int vaapi_map_frame(AVHWFramesContext *hwfc,
760 AVFrame *dst, const AVFrame *src, int flags)
762 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
763 VAAPIFramesContext *ctx = hwfc->internal->priv;
764 VASurfaceID surface_id;
765 const VAAPIFormatDescriptor *desc;
766 VAImageFormat *image_format;
769 void *address = NULL;
772 surface_id = (VASurfaceID)(uintptr_t)src->data[3];
773 av_log(hwfc, AV_LOG_DEBUG, "Map surface %#x.\n", surface_id);
775 if (!ctx->derive_works && (flags & AV_HWFRAME_MAP_DIRECT)) {
776 // Requested direct mapping but it is not possible.
777 return AVERROR(EINVAL);
779 if (dst->format == AV_PIX_FMT_NONE)
780 dst->format = hwfc->sw_format;
781 if (dst->format != hwfc->sw_format && (flags & AV_HWFRAME_MAP_DIRECT)) {
782 // Requested direct mapping but the formats do not match.
783 return AVERROR(EINVAL);
786 err = vaapi_get_image_format(hwfc->device_ctx, dst->format, &image_format);
788 // Requested format is not a valid output format.
789 return AVERROR(EINVAL);
792 map = av_malloc(sizeof(*map));
794 return AVERROR(ENOMEM);
796 map->image.image_id = VA_INVALID_ID;
798 vas = vaSyncSurface(hwctx->display, surface_id);
799 if (vas != VA_STATUS_SUCCESS) {
800 av_log(hwfc, AV_LOG_ERROR, "Failed to sync surface "
801 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
806 // The memory which we map using derive need not be connected to the CPU
807 // in a way conducive to fast access. On Gen7-Gen9 Intel graphics, the
808 // memory is mappable but not cached, so normal memcpy()-like access is
809 // very slow to read it (but writing is ok). It is possible to read much
810 // faster with a copy routine which is aware of the limitation, but we
811 // assume for now that the user is not aware of that and would therefore
812 // prefer not to be given direct-mapped memory if they request read access.
813 if (ctx->derive_works && dst->format == hwfc->sw_format &&
814 ((flags & AV_HWFRAME_MAP_DIRECT) || !(flags & AV_HWFRAME_MAP_READ))) {
815 vas = vaDeriveImage(hwctx->display, surface_id, &map->image);
816 if (vas != VA_STATUS_SUCCESS) {
817 av_log(hwfc, AV_LOG_ERROR, "Failed to derive image from "
818 "surface %#x: %d (%s).\n",
819 surface_id, vas, vaErrorStr(vas));
823 if (map->image.format.fourcc != image_format->fourcc) {
824 av_log(hwfc, AV_LOG_ERROR, "Derive image of surface %#x "
825 "is in wrong format: expected %#08x, got %#08x.\n",
826 surface_id, image_format->fourcc, map->image.format.fourcc);
830 map->flags |= AV_HWFRAME_MAP_DIRECT;
832 vas = vaCreateImage(hwctx->display, image_format,
833 hwfc->width, hwfc->height, &map->image);
834 if (vas != VA_STATUS_SUCCESS) {
835 av_log(hwfc, AV_LOG_ERROR, "Failed to create image for "
836 "surface %#x: %d (%s).\n",
837 surface_id, vas, vaErrorStr(vas));
841 if (!(flags & AV_HWFRAME_MAP_OVERWRITE)) {
842 vas = vaGetImage(hwctx->display, surface_id, 0, 0,
843 hwfc->width, hwfc->height, map->image.image_id);
844 if (vas != VA_STATUS_SUCCESS) {
845 av_log(hwfc, AV_LOG_ERROR, "Failed to read image from "
846 "surface %#x: %d (%s).\n",
847 surface_id, vas, vaErrorStr(vas));
854 vas = vaMapBuffer(hwctx->display, map->image.buf, &address);
855 if (vas != VA_STATUS_SUCCESS) {
856 av_log(hwfc, AV_LOG_ERROR, "Failed to map image from surface "
857 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
862 err = ff_hwframe_map_create(src->hw_frames_ctx,
863 dst, src, &vaapi_unmap_frame, map);
867 dst->width = src->width;
868 dst->height = src->height;
870 for (i = 0; i < map->image.num_planes; i++) {
871 dst->data[i] = (uint8_t*)address + map->image.offsets[i];
872 dst->linesize[i] = map->image.pitches[i];
875 desc = vaapi_format_from_fourcc(map->image.format.fourcc);
876 if (desc && desc->chroma_planes_swapped) {
877 // Chroma planes are YVU rather than YUV, so swap them.
878 FFSWAP(uint8_t*, dst->data[1], dst->data[2]);
886 vaUnmapBuffer(hwctx->display, map->image.buf);
887 if (map->image.image_id != VA_INVALID_ID)
888 vaDestroyImage(hwctx->display, map->image.image_id);
894 static int vaapi_transfer_data_from(AVHWFramesContext *hwfc,
895 AVFrame *dst, const AVFrame *src)
900 if (dst->width > hwfc->width || dst->height > hwfc->height)
901 return AVERROR(EINVAL);
903 map = av_frame_alloc();
905 return AVERROR(ENOMEM);
906 map->format = dst->format;
908 err = vaapi_map_frame(hwfc, map, src, AV_HWFRAME_MAP_READ);
912 map->width = dst->width;
913 map->height = dst->height;
915 err = av_frame_copy(dst, map);
925 static int vaapi_transfer_data_to(AVHWFramesContext *hwfc,
926 AVFrame *dst, const AVFrame *src)
931 if (src->width > hwfc->width || src->height > hwfc->height)
932 return AVERROR(EINVAL);
934 map = av_frame_alloc();
936 return AVERROR(ENOMEM);
937 map->format = src->format;
939 err = vaapi_map_frame(hwfc, map, dst, AV_HWFRAME_MAP_WRITE | AV_HWFRAME_MAP_OVERWRITE);
943 map->width = src->width;
944 map->height = src->height;
946 err = av_frame_copy(map, src);
956 static int vaapi_map_to_memory(AVHWFramesContext *hwfc, AVFrame *dst,
957 const AVFrame *src, int flags)
961 if (dst->format != AV_PIX_FMT_NONE) {
962 err = vaapi_get_image_format(hwfc->device_ctx, dst->format, NULL);
964 return AVERROR(ENOSYS);
967 err = vaapi_map_frame(hwfc, dst, src, flags);
971 err = av_frame_copy_props(dst, src);
980 #define DRM_MAP(va, layers, ...) { \
985 static const struct {
987 int nb_layer_formats;
988 uint32_t layer_formats[AV_DRM_MAX_PLANES];
989 } vaapi_drm_format_map[] = {
991 DRM_MAP(NV12, 2, DRM_FORMAT_R8, DRM_FORMAT_RG88),
993 DRM_MAP(NV12, 1, DRM_FORMAT_NV12),
994 #if defined(VA_FOURCC_P010) && defined(DRM_FORMAT_R16)
995 DRM_MAP(P010, 2, DRM_FORMAT_R16, DRM_FORMAT_RG1616),
997 DRM_MAP(BGRA, 1, DRM_FORMAT_ARGB8888),
998 DRM_MAP(BGRX, 1, DRM_FORMAT_XRGB8888),
999 DRM_MAP(RGBA, 1, DRM_FORMAT_ABGR8888),
1000 DRM_MAP(RGBX, 1, DRM_FORMAT_XBGR8888),
1001 #ifdef VA_FOURCC_ABGR
1002 DRM_MAP(ABGR, 1, DRM_FORMAT_RGBA8888),
1003 DRM_MAP(XBGR, 1, DRM_FORMAT_RGBX8888),
1005 DRM_MAP(ARGB, 1, DRM_FORMAT_BGRA8888),
1006 DRM_MAP(XRGB, 1, DRM_FORMAT_BGRX8888),
1010 static void vaapi_unmap_from_drm(AVHWFramesContext *dst_fc,
1011 HWMapDescriptor *hwmap)
1013 AVVAAPIDeviceContext *dst_dev = dst_fc->device_ctx->hwctx;
1015 VASurfaceID surface_id = (VASurfaceID)(uintptr_t)hwmap->priv;
1017 av_log(dst_fc, AV_LOG_DEBUG, "Destroy surface %#x.\n", surface_id);
1019 vaDestroySurfaces(dst_dev->display, &surface_id, 1);
1022 static int vaapi_map_from_drm(AVHWFramesContext *src_fc, AVFrame *dst,
1023 const AVFrame *src, int flags)
1025 AVHWFramesContext *dst_fc =
1026 (AVHWFramesContext*)dst->hw_frames_ctx->data;
1027 AVVAAPIDeviceContext *dst_dev = dst_fc->device_ctx->hwctx;
1028 const AVDRMFrameDescriptor *desc;
1029 const VAAPIFormatDescriptor *format_desc;
1030 VASurfaceID surface_id;
1035 unsigned long buffer_handle;
1036 VASurfaceAttribExternalBuffers buffer_desc;
1037 VASurfaceAttrib attrs[2] = {
1039 .type = VASurfaceAttribMemoryType,
1040 .flags = VA_SURFACE_ATTRIB_SETTABLE,
1041 .value.type = VAGenericValueTypeInteger,
1042 .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME,
1045 .type = VASurfaceAttribExternalBufferDescriptor,
1046 .flags = VA_SURFACE_ATTRIB_SETTABLE,
1047 .value.type = VAGenericValueTypePointer,
1048 .value.value.p = &buffer_desc,
1052 desc = (AVDRMFrameDescriptor*)src->data[0];
1054 if (desc->nb_objects != 1) {
1055 av_log(dst_fc, AV_LOG_ERROR, "VAAPI can only map frames "
1056 "made from a single DRM object.\n");
1057 return AVERROR(EINVAL);
1061 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_drm_format_map); i++) {
1062 if (desc->nb_layers != vaapi_drm_format_map[i].nb_layer_formats)
1064 for (j = 0; j < desc->nb_layers; j++) {
1065 if (desc->layers[j].format !=
1066 vaapi_drm_format_map[i].layer_formats[j])
1069 if (j != desc->nb_layers)
1071 va_fourcc = vaapi_drm_format_map[i].va_fourcc;
1075 av_log(dst_fc, AV_LOG_ERROR, "DRM format not supported "
1077 return AVERROR(EINVAL);
1080 av_log(dst_fc, AV_LOG_DEBUG, "Map DRM object %d to VAAPI as "
1081 "%08x.\n", desc->objects[0].fd, va_fourcc);
1083 format_desc = vaapi_format_from_fourcc(va_fourcc);
1084 av_assert0(format_desc);
1086 buffer_handle = desc->objects[0].fd;
1087 buffer_desc.pixel_format = va_fourcc;
1088 buffer_desc.width = src_fc->width;
1089 buffer_desc.height = src_fc->height;
1090 buffer_desc.data_size = desc->objects[0].size;
1091 buffer_desc.buffers = &buffer_handle;
1092 buffer_desc.num_buffers = 1;
1093 buffer_desc.flags = 0;
1096 for (i = 0; i < desc->nb_layers; i++) {
1097 for (j = 0; j < desc->layers[i].nb_planes; j++) {
1098 buffer_desc.pitches[k] = desc->layers[i].planes[j].pitch;
1099 buffer_desc.offsets[k] = desc->layers[i].planes[j].offset;
1103 buffer_desc.num_planes = k;
1105 if (format_desc->chroma_planes_swapped &&
1106 buffer_desc.num_planes == 3) {
1107 FFSWAP(uint32_t, buffer_desc.pitches[1], buffer_desc.pitches[2]);
1108 FFSWAP(uint32_t, buffer_desc.offsets[1], buffer_desc.offsets[2]);
1111 vas = vaCreateSurfaces(dst_dev->display, format_desc->rt_format,
1112 src->width, src->height,
1114 attrs, FF_ARRAY_ELEMS(attrs));
1115 if (vas != VA_STATUS_SUCCESS) {
1116 av_log(dst_fc, AV_LOG_ERROR, "Failed to create surface from DRM "
1117 "object: %d (%s).\n", vas, vaErrorStr(vas));
1118 return AVERROR(EIO);
1120 av_log(dst_fc, AV_LOG_DEBUG, "Create surface %#x.\n", surface_id);
1122 err = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src,
1123 &vaapi_unmap_from_drm,
1124 (void*)(uintptr_t)surface_id);
1128 dst->width = src->width;
1129 dst->height = src->height;
1130 dst->data[3] = (uint8_t*)(uintptr_t)surface_id;
1132 av_log(dst_fc, AV_LOG_DEBUG, "Mapped DRM object %d to "
1133 "surface %#x.\n", desc->objects[0].fd, surface_id);
1138 #if VA_CHECK_VERSION(1, 1, 0)
1139 static void vaapi_unmap_to_drm_esh(AVHWFramesContext *hwfc,
1140 HWMapDescriptor *hwmap)
1142 AVDRMFrameDescriptor *drm_desc = hwmap->priv;
1145 for (i = 0; i < drm_desc->nb_objects; i++)
1146 close(drm_desc->objects[i].fd);
1148 av_freep(&drm_desc);
1151 static int vaapi_map_to_drm_esh(AVHWFramesContext *hwfc, AVFrame *dst,
1152 const AVFrame *src, int flags)
1154 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1155 VASurfaceID surface_id;
1157 VADRMPRIMESurfaceDescriptor va_desc;
1158 AVDRMFrameDescriptor *drm_desc = NULL;
1159 uint32_t export_flags;
1162 surface_id = (VASurfaceID)(uintptr_t)src->data[3];
1164 export_flags = VA_EXPORT_SURFACE_SEPARATE_LAYERS;
1165 if (flags & AV_HWFRAME_MAP_READ)
1166 export_flags |= VA_EXPORT_SURFACE_READ_ONLY;
1167 if (flags & AV_HWFRAME_MAP_WRITE)
1168 export_flags |= VA_EXPORT_SURFACE_WRITE_ONLY;
1170 vas = vaExportSurfaceHandle(hwctx->display, surface_id,
1171 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2,
1172 export_flags, &va_desc);
1173 if (vas != VA_STATUS_SUCCESS) {
1174 if (vas == VA_STATUS_ERROR_UNIMPLEMENTED)
1175 return AVERROR(ENOSYS);
1176 av_log(hwfc, AV_LOG_ERROR, "Failed to export surface %#x: "
1177 "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
1178 return AVERROR(EIO);
1181 drm_desc = av_mallocz(sizeof(*drm_desc));
1183 err = AVERROR(ENOMEM);
1187 // By some bizarre coincidence, these structures are very similar...
1188 drm_desc->nb_objects = va_desc.num_objects;
1189 for (i = 0; i < va_desc.num_objects; i++) {
1190 drm_desc->objects[i].fd = va_desc.objects[i].fd;
1191 drm_desc->objects[i].size = va_desc.objects[i].size;
1192 drm_desc->objects[i].format_modifier =
1193 va_desc.objects[i].drm_format_modifier;
1195 drm_desc->nb_layers = va_desc.num_layers;
1196 for (i = 0; i < va_desc.num_layers; i++) {
1197 drm_desc->layers[i].format = va_desc.layers[i].drm_format;
1198 drm_desc->layers[i].nb_planes = va_desc.layers[i].num_planes;
1199 for (j = 0; j < va_desc.layers[i].num_planes; j++) {
1200 drm_desc->layers[i].planes[j].object_index =
1201 va_desc.layers[i].object_index[j];
1202 drm_desc->layers[i].planes[j].offset =
1203 va_desc.layers[i].offset[j];
1204 drm_desc->layers[i].planes[j].pitch =
1205 va_desc.layers[i].pitch[j];
1209 err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
1210 &vaapi_unmap_to_drm_esh, drm_desc);
1214 dst->width = src->width;
1215 dst->height = src->height;
1216 dst->data[0] = (uint8_t*)drm_desc;
1221 for (i = 0; i < va_desc.num_objects; i++)
1222 close(va_desc.objects[i].fd);
1223 av_freep(&drm_desc);
1228 #if VA_CHECK_VERSION(0, 36, 0)
1229 typedef struct VAAPIDRMImageBufferMapping {
1231 VABufferInfo buffer_info;
1233 AVDRMFrameDescriptor drm_desc;
1234 } VAAPIDRMImageBufferMapping;
1236 static void vaapi_unmap_to_drm_abh(AVHWFramesContext *hwfc,
1237 HWMapDescriptor *hwmap)
1239 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1240 VAAPIDRMImageBufferMapping *mapping = hwmap->priv;
1241 VASurfaceID surface_id;
1244 surface_id = (VASurfaceID)(uintptr_t)hwmap->source->data[3];
1245 av_log(hwfc, AV_LOG_DEBUG, "Unmap VAAPI surface %#x from DRM.\n",
1248 // DRM PRIME file descriptors are closed by vaReleaseBufferHandle(),
1249 // so we shouldn't close them separately.
1251 vas = vaReleaseBufferHandle(hwctx->display, mapping->image.buf);
1252 if (vas != VA_STATUS_SUCCESS) {
1253 av_log(hwfc, AV_LOG_ERROR, "Failed to release buffer "
1254 "handle of image %#x (derived from surface %#x): "
1255 "%d (%s).\n", mapping->image.buf, surface_id,
1256 vas, vaErrorStr(vas));
1259 vas = vaDestroyImage(hwctx->display, mapping->image.image_id);
1260 if (vas != VA_STATUS_SUCCESS) {
1261 av_log(hwfc, AV_LOG_ERROR, "Failed to destroy image "
1262 "derived from surface %#x: %d (%s).\n",
1263 surface_id, vas, vaErrorStr(vas));
1269 static int vaapi_map_to_drm_abh(AVHWFramesContext *hwfc, AVFrame *dst,
1270 const AVFrame *src, int flags)
1272 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1273 VAAPIDRMImageBufferMapping *mapping = NULL;
1274 VASurfaceID surface_id;
1278 surface_id = (VASurfaceID)(uintptr_t)src->data[3];
1279 av_log(hwfc, AV_LOG_DEBUG, "Map VAAPI surface %#x to DRM.\n",
1282 mapping = av_mallocz(sizeof(*mapping));
1284 return AVERROR(ENOMEM);
1286 vas = vaDeriveImage(hwctx->display, surface_id,
1288 if (vas != VA_STATUS_SUCCESS) {
1289 av_log(hwfc, AV_LOG_ERROR, "Failed to derive image from "
1290 "surface %#x: %d (%s).\n",
1291 surface_id, vas, vaErrorStr(vas));
1296 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_drm_format_map); i++) {
1297 if (vaapi_drm_format_map[i].va_fourcc ==
1298 mapping->image.format.fourcc)
1301 if (i >= FF_ARRAY_ELEMS(vaapi_drm_format_map)) {
1302 av_log(hwfc, AV_LOG_ERROR, "No matching DRM format for "
1303 "VAAPI format %#x.\n", mapping->image.format.fourcc);
1304 err = AVERROR(EINVAL);
1308 mapping->buffer_info.mem_type =
1309 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME;
1311 mapping->drm_desc.nb_layers =
1312 vaapi_drm_format_map[i].nb_layer_formats;
1313 if (mapping->drm_desc.nb_layers > 1) {
1314 if (mapping->drm_desc.nb_layers != mapping->image.num_planes) {
1315 av_log(hwfc, AV_LOG_ERROR, "Image properties do not match "
1316 "expected format: got %d planes, but expected %d.\n",
1317 mapping->image.num_planes, mapping->drm_desc.nb_layers);
1318 err = AVERROR(EINVAL);
1322 for(p = 0; p < mapping->drm_desc.nb_layers; p++) {
1323 mapping->drm_desc.layers[p] = (AVDRMLayerDescriptor) {
1324 .format = vaapi_drm_format_map[i].layer_formats[p],
1328 .offset = mapping->image.offsets[p],
1329 .pitch = mapping->image.pitches[p],
1334 mapping->drm_desc.layers[0].format =
1335 vaapi_drm_format_map[i].layer_formats[0];
1336 mapping->drm_desc.layers[0].nb_planes = mapping->image.num_planes;
1337 for (p = 0; p < mapping->image.num_planes; p++) {
1338 mapping->drm_desc.layers[0].planes[p] = (AVDRMPlaneDescriptor) {
1340 .offset = mapping->image.offsets[p],
1341 .pitch = mapping->image.pitches[p],
1346 vas = vaAcquireBufferHandle(hwctx->display, mapping->image.buf,
1347 &mapping->buffer_info);
1348 if (vas != VA_STATUS_SUCCESS) {
1349 av_log(hwfc, AV_LOG_ERROR, "Failed to get buffer "
1350 "handle from image %#x (derived from surface %#x): "
1351 "%d (%s).\n", mapping->image.buf, surface_id,
1352 vas, vaErrorStr(vas));
1357 av_log(hwfc, AV_LOG_DEBUG, "DRM PRIME fd is %ld.\n",
1358 mapping->buffer_info.handle);
1360 mapping->drm_desc.nb_objects = 1;
1361 mapping->drm_desc.objects[0] = (AVDRMObjectDescriptor) {
1362 .fd = mapping->buffer_info.handle,
1363 .size = mapping->image.data_size,
1364 // There is no way to get the format modifier with this API.
1365 .format_modifier = DRM_FORMAT_MOD_INVALID,
1368 err = ff_hwframe_map_create(src->hw_frames_ctx,
1369 dst, src, &vaapi_unmap_to_drm_abh,
1374 dst->data[0] = (uint8_t*)&mapping->drm_desc;
1375 dst->width = src->width;
1376 dst->height = src->height;
1381 vaReleaseBufferHandle(hwctx->display, mapping->image.buf);
1383 vaDestroyImage(hwctx->display, mapping->image.image_id);
1390 static int vaapi_map_to_drm(AVHWFramesContext *hwfc, AVFrame *dst,
1391 const AVFrame *src, int flags)
1393 #if VA_CHECK_VERSION(1, 1, 0)
1395 err = vaapi_map_to_drm_esh(hwfc, dst, src, flags);
1396 if (err != AVERROR(ENOSYS))
1399 #if VA_CHECK_VERSION(0, 36, 0)
1400 return vaapi_map_to_drm_abh(hwfc, dst, src, flags);
1402 return AVERROR(ENOSYS);
1405 #endif /* CONFIG_LIBDRM */
1407 static int vaapi_map_to(AVHWFramesContext *hwfc, AVFrame *dst,
1408 const AVFrame *src, int flags)
1410 switch (src->format) {
1412 case AV_PIX_FMT_DRM_PRIME:
1413 return vaapi_map_from_drm(hwfc, dst, src, flags);
1416 return AVERROR(ENOSYS);
1420 static int vaapi_map_from(AVHWFramesContext *hwfc, AVFrame *dst,
1421 const AVFrame *src, int flags)
1423 switch (dst->format) {
1425 case AV_PIX_FMT_DRM_PRIME:
1426 return vaapi_map_to_drm(hwfc, dst, src, flags);
1429 return vaapi_map_to_memory(hwfc, dst, src, flags);
1433 static void vaapi_device_free(AVHWDeviceContext *ctx)
1435 AVVAAPIDeviceContext *hwctx = ctx->hwctx;
1436 VAAPIDevicePriv *priv = ctx->user_opaque;
1439 vaTerminate(hwctx->display);
1442 if (priv->x11_display)
1443 XCloseDisplay(priv->x11_display);
1446 if (priv->drm_fd >= 0)
1447 close(priv->drm_fd);
1453 static void vaapi_device_log_error(void *context, const char *message)
1455 AVHWDeviceContext *ctx = context;
1457 av_log(ctx, AV_LOG_ERROR, "libva: %s", message);
1460 static void vaapi_device_log_info(void *context, const char *message)
1462 AVHWDeviceContext *ctx = context;
1464 av_log(ctx, AV_LOG_VERBOSE, "libva: %s", message);
1468 static int vaapi_device_connect(AVHWDeviceContext *ctx,
1471 AVVAAPIDeviceContext *hwctx = ctx->hwctx;
1476 vaSetErrorCallback(display, &vaapi_device_log_error, ctx);
1477 vaSetInfoCallback (display, &vaapi_device_log_info, ctx);
1480 hwctx->display = display;
1482 vas = vaInitialize(display, &major, &minor);
1483 if (vas != VA_STATUS_SUCCESS) {
1484 av_log(ctx, AV_LOG_ERROR, "Failed to initialise VAAPI "
1485 "connection: %d (%s).\n", vas, vaErrorStr(vas));
1486 return AVERROR(EIO);
1488 av_log(ctx, AV_LOG_VERBOSE, "Initialised VAAPI connection: "
1489 "version %d.%d\n", major, minor);
1494 static int vaapi_device_create(AVHWDeviceContext *ctx, const char *device,
1495 AVDictionary *opts, int flags)
1497 VAAPIDevicePriv *priv;
1498 VADisplay display = NULL;
1499 const AVDictionaryEntry *ent;
1500 int try_drm, try_x11, try_all;
1502 priv = av_mallocz(sizeof(*priv));
1504 return AVERROR(ENOMEM);
1508 ctx->user_opaque = priv;
1509 ctx->free = vaapi_device_free;
1511 ent = av_dict_get(opts, "connection_type", NULL, 0);
1513 try_all = try_drm = try_x11 = 0;
1514 if (!strcmp(ent->value, "drm")) {
1516 } else if (!strcmp(ent->value, "x11")) {
1519 av_log(ctx, AV_LOG_ERROR, "Invalid connection type %s.\n",
1521 return AVERROR(EINVAL);
1525 try_drm = HAVE_VAAPI_DRM;
1526 try_x11 = HAVE_VAAPI_X11;
1530 while (!display && try_drm) {
1531 // If the device is specified, try to open it as a DRM device node.
1532 // If not, look for a usable render node, possibly restricted to those
1533 // using a specified kernel driver.
1534 int loglevel = try_all ? AV_LOG_VERBOSE : AV_LOG_ERROR;
1536 priv->drm_fd = open(device, O_RDWR);
1537 if (priv->drm_fd < 0) {
1538 av_log(ctx, loglevel, "Failed to open %s as "
1539 "DRM device node.\n", device);
1544 int n, max_devices = 8;
1546 const AVDictionaryEntry *kernel_driver;
1547 kernel_driver = av_dict_get(opts, "kernel_driver", NULL, 0);
1549 for (n = 0; n < max_devices; n++) {
1550 snprintf(path, sizeof(path),
1551 "/dev/dri/renderD%d", 128 + n);
1552 priv->drm_fd = open(path, O_RDWR);
1553 if (priv->drm_fd < 0) {
1554 av_log(ctx, AV_LOG_VERBOSE, "Cannot open "
1555 "DRM render node for device %d.\n", n);
1559 if (kernel_driver) {
1561 info = drmGetVersion(priv->drm_fd);
1562 if (strcmp(kernel_driver->value, info->name)) {
1563 av_log(ctx, AV_LOG_VERBOSE, "Ignoring device %d "
1564 "with non-matching kernel driver (%s).\n",
1566 drmFreeVersion(info);
1567 close(priv->drm_fd);
1571 av_log(ctx, AV_LOG_VERBOSE, "Trying to use "
1572 "DRM render node for device %d, "
1573 "with matching kernel driver (%s).\n",
1575 drmFreeVersion(info);
1579 av_log(ctx, AV_LOG_VERBOSE, "Trying to use "
1580 "DRM render node for device %d.\n", n);
1584 if (n >= max_devices)
1588 display = vaGetDisplayDRM(priv->drm_fd);
1590 av_log(ctx, AV_LOG_VERBOSE, "Cannot open a VA display "
1591 "from DRM device %s.\n", device);
1592 return AVERROR_EXTERNAL;
1599 if (!display && try_x11) {
1600 // Try to open the device as an X11 display.
1601 priv->x11_display = XOpenDisplay(device);
1602 if (!priv->x11_display) {
1603 av_log(ctx, AV_LOG_VERBOSE, "Cannot open X11 display "
1604 "%s.\n", XDisplayName(device));
1606 display = vaGetDisplay(priv->x11_display);
1608 av_log(ctx, AV_LOG_ERROR, "Cannot open a VA display "
1609 "from X11 display %s.\n", XDisplayName(device));
1610 return AVERROR_UNKNOWN;
1613 av_log(ctx, AV_LOG_VERBOSE, "Opened VA display via "
1614 "X11 display %s.\n", XDisplayName(device));
1621 av_log(ctx, AV_LOG_ERROR, "No VA display found for "
1622 "device %s.\n", device);
1624 av_log(ctx, AV_LOG_ERROR, "No VA display found for "
1625 "any default device.\n");
1626 return AVERROR(EINVAL);
1629 ent = av_dict_get(opts, "driver", NULL, 0);
1631 #if VA_CHECK_VERSION(0, 38, 0)
1633 vas = vaSetDriverName(display, ent->value);
1634 if (vas != VA_STATUS_SUCCESS) {
1635 av_log(ctx, AV_LOG_ERROR, "Failed to set driver name to "
1636 "%s: %d (%s).\n", ent->value, vas, vaErrorStr(vas));
1637 vaTerminate(display);
1638 return AVERROR_EXTERNAL;
1641 av_log(ctx, AV_LOG_WARNING, "Driver name setting is not "
1642 "supported with this VAAPI version.\n");
1646 return vaapi_device_connect(ctx, display);
1649 static int vaapi_device_derive(AVHWDeviceContext *ctx,
1650 AVHWDeviceContext *src_ctx,
1651 AVDictionary *opts, int flags)
1654 if (src_ctx->type == AV_HWDEVICE_TYPE_DRM) {
1655 AVDRMDeviceContext *src_hwctx = src_ctx->hwctx;
1657 VAAPIDevicePriv *priv;
1660 if (src_hwctx->fd < 0) {
1661 av_log(ctx, AV_LOG_ERROR, "DRM instance requires an associated "
1662 "device to derive a VA display from.\n");
1663 return AVERROR(EINVAL);
1668 int node_type = drmGetNodeTypeFromFd(src_hwctx->fd);
1670 if (node_type < 0) {
1671 av_log(ctx, AV_LOG_ERROR, "DRM instance fd does not appear "
1672 "to refer to a DRM device.\n");
1673 return AVERROR(EINVAL);
1675 if (node_type == DRM_NODE_RENDER) {
1678 render_node = drmGetRenderDeviceNameFromFd(src_hwctx->fd);
1680 av_log(ctx, AV_LOG_VERBOSE, "Using non-render node "
1681 "because the device does not have an "
1682 "associated render node.\n");
1685 fd = open(render_node, O_RDWR);
1687 av_log(ctx, AV_LOG_VERBOSE, "Using non-render node "
1688 "because the associated render node "
1689 "could not be opened.\n");
1692 av_log(ctx, AV_LOG_VERBOSE, "Using render node %s "
1693 "in place of non-render DRM device.\n",
1704 priv = av_mallocz(sizeof(*priv));
1706 if (fd != src_hwctx->fd) {
1707 // The fd was opened in this function.
1710 return AVERROR(ENOMEM);
1713 if (fd == src_hwctx->fd) {
1714 // The fd is inherited from the source context and we are holding
1715 // a reference to that, we don't want to close it from here.
1721 ctx->user_opaque = priv;
1722 ctx->free = &vaapi_device_free;
1724 display = vaGetDisplayDRM(fd);
1726 av_log(ctx, AV_LOG_ERROR, "Failed to open a VA display from "
1728 return AVERROR(EIO);
1731 return vaapi_device_connect(ctx, display);
1734 return AVERROR(ENOSYS);
1737 const HWContextType ff_hwcontext_type_vaapi = {
1738 .type = AV_HWDEVICE_TYPE_VAAPI,
1741 .device_hwctx_size = sizeof(AVVAAPIDeviceContext),
1742 .device_priv_size = sizeof(VAAPIDeviceContext),
1743 .device_hwconfig_size = sizeof(AVVAAPIHWConfig),
1744 .frames_hwctx_size = sizeof(AVVAAPIFramesContext),
1745 .frames_priv_size = sizeof(VAAPIFramesContext),
1747 .device_create = &vaapi_device_create,
1748 .device_derive = &vaapi_device_derive,
1749 .device_init = &vaapi_device_init,
1750 .device_uninit = &vaapi_device_uninit,
1751 .frames_get_constraints = &vaapi_frames_get_constraints,
1752 .frames_init = &vaapi_frames_init,
1753 .frames_uninit = &vaapi_frames_uninit,
1754 .frames_get_buffer = &vaapi_get_buffer,
1755 .transfer_get_formats = &vaapi_transfer_get_formats,
1756 .transfer_data_to = &vaapi_transfer_data_to,
1757 .transfer_data_from = &vaapi_transfer_data_from,
1758 .map_to = &vaapi_map_to,
1759 .map_from = &vaapi_map_from,
1761 .pix_fmts = (const enum AVPixelFormat[]) {