2 * This file is part of Libav.
4 * Libav is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * Libav is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with Libav; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # include <va/va_x11.h>
25 # include <va/va_drm.h>
37 #include "hwcontext.h"
38 #include "hwcontext_internal.h"
39 #include "hwcontext_vaapi.h"
44 typedef struct VAAPIDevicePriv {
52 typedef struct VAAPISurfaceFormat {
53 enum AVPixelFormat pix_fmt;
54 VAImageFormat image_format;
57 typedef struct VAAPIDeviceContext {
58 // Surface formats which can be used with this device.
59 VAAPISurfaceFormat *formats;
63 typedef struct VAAPIFramesContext {
64 // Surface attributes set at create time.
65 VASurfaceAttrib *attributes;
67 // RT format of the underlying surface (Intel driver ignores this anyway).
68 unsigned int rt_format;
69 // Whether vaDeriveImage works.
73 typedef struct VAAPIMapping {
74 // Handle to the derived or copied image which is mapped.
76 // The mapping flags actually used.
80 #define MAP(va, rt, av) { \
82 VA_RT_FORMAT_ ## rt, \
85 // The map fourcc <-> pix_fmt isn't bijective because of the annoying U/V
86 // plane swap cases. The frame handling below tries to hide these.
89 unsigned int rt_format;
90 enum AVPixelFormat pix_fmt;
91 } vaapi_format_map[] = {
92 MAP(NV12, YUV420, NV12),
93 MAP(YV12, YUV420, YUV420P), // With U/V planes swapped.
94 MAP(IYUV, YUV420, YUV420P),
95 //MAP(I420, YUV420, YUV420P), // Not in libva but used by Intel driver.
97 MAP(YV16, YUV422, YUV422P), // With U/V planes swapped.
99 MAP(422H, YUV422, YUV422P),
100 MAP(UYVY, YUV422, UYVY422),
101 MAP(YUY2, YUV422, YUYV422),
102 MAP(Y800, YUV400, GRAY8),
103 #ifdef VA_FOURCC_P010
104 MAP(P010, YUV420_10BPP, P010),
106 MAP(BGRA, RGB32, BGRA),
107 //MAP(BGRX, RGB32, BGR0),
108 MAP(RGBA, RGB32, RGBA),
109 //MAP(RGBX, RGB32, RGB0),
110 MAP(ABGR, RGB32, ABGR),
111 //MAP(XBGR, RGB32, 0BGR),
112 MAP(ARGB, RGB32, ARGB),
113 //MAP(XRGB, RGB32, 0RGB),
117 static enum AVPixelFormat vaapi_pix_fmt_from_fourcc(unsigned int fourcc)
120 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++)
121 if (vaapi_format_map[i].fourcc == fourcc)
122 return vaapi_format_map[i].pix_fmt;
123 return AV_PIX_FMT_NONE;
126 static int vaapi_get_image_format(AVHWDeviceContext *hwdev,
127 enum AVPixelFormat pix_fmt,
128 VAImageFormat **image_format)
130 VAAPIDeviceContext *ctx = hwdev->internal->priv;
133 for (i = 0; i < ctx->nb_formats; i++) {
134 if (ctx->formats[i].pix_fmt == pix_fmt) {
136 *image_format = &ctx->formats[i].image_format;
140 return AVERROR(EINVAL);
143 static int vaapi_frames_get_constraints(AVHWDeviceContext *hwdev,
144 const void *hwconfig,
145 AVHWFramesConstraints *constraints)
147 AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
148 const AVVAAPIHWConfig *config = hwconfig;
149 VAAPIDeviceContext *ctx = hwdev->internal->priv;
150 VASurfaceAttrib *attr_list = NULL;
152 enum AVPixelFormat pix_fmt;
154 int err, i, j, attr_count, pix_fmt_count;
157 !(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_SURFACE_ATTRIBUTES)) {
159 vas = vaQuerySurfaceAttributes(hwctx->display, config->config_id,
161 if (vas != VA_STATUS_SUCCESS) {
162 av_log(hwdev, AV_LOG_ERROR, "Failed to query surface attributes: "
163 "%d (%s).\n", vas, vaErrorStr(vas));
164 err = AVERROR(ENOSYS);
168 attr_list = av_malloc(attr_count * sizeof(*attr_list));
170 err = AVERROR(ENOMEM);
174 vas = vaQuerySurfaceAttributes(hwctx->display, config->config_id,
175 attr_list, &attr_count);
176 if (vas != VA_STATUS_SUCCESS) {
177 av_log(hwdev, AV_LOG_ERROR, "Failed to query surface attributes: "
178 "%d (%s).\n", vas, vaErrorStr(vas));
179 err = AVERROR(ENOSYS);
184 for (i = 0; i < attr_count; i++) {
185 switch (attr_list[i].type) {
186 case VASurfaceAttribPixelFormat:
187 fourcc = attr_list[i].value.value.i;
188 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
189 if (pix_fmt != AV_PIX_FMT_NONE) {
192 // Something unsupported - ignore.
195 case VASurfaceAttribMinWidth:
196 constraints->min_width = attr_list[i].value.value.i;
198 case VASurfaceAttribMinHeight:
199 constraints->min_height = attr_list[i].value.value.i;
201 case VASurfaceAttribMaxWidth:
202 constraints->max_width = attr_list[i].value.value.i;
204 case VASurfaceAttribMaxHeight:
205 constraints->max_height = attr_list[i].value.value.i;
209 if (pix_fmt_count == 0) {
210 // Nothing usable found. Presumably there exists something which
211 // works, so leave the set null to indicate unknown.
212 constraints->valid_sw_formats = NULL;
214 constraints->valid_sw_formats = av_malloc_array(pix_fmt_count + 1,
216 if (!constraints->valid_sw_formats) {
217 err = AVERROR(ENOMEM);
221 for (i = j = 0; i < attr_count; i++) {
222 if (attr_list[i].type != VASurfaceAttribPixelFormat)
224 fourcc = attr_list[i].value.value.i;
225 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
226 if (pix_fmt != AV_PIX_FMT_NONE)
227 constraints->valid_sw_formats[j++] = pix_fmt;
229 av_assert0(j == pix_fmt_count);
230 constraints->valid_sw_formats[j] = AV_PIX_FMT_NONE;
233 // No configuration supplied.
234 // Return the full set of image formats known by the implementation.
235 constraints->valid_sw_formats = av_malloc_array(ctx->nb_formats + 1,
237 if (!constraints->valid_sw_formats) {
238 err = AVERROR(ENOMEM);
241 for (i = 0; i < ctx->nb_formats; i++)
242 constraints->valid_sw_formats[i] = ctx->formats[i].pix_fmt;
243 constraints->valid_sw_formats[i] = AV_PIX_FMT_NONE;
246 constraints->valid_hw_formats = av_malloc_array(2, sizeof(pix_fmt));
247 if (!constraints->valid_hw_formats) {
248 err = AVERROR(ENOMEM);
251 constraints->valid_hw_formats[0] = AV_PIX_FMT_VAAPI;
252 constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
256 av_freep(&attr_list);
260 static const struct {
261 const char *friendly_name;
262 const char *match_string;
264 } vaapi_driver_quirks_table[] = {
266 "Intel i965 (Quick Sync)",
268 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS,
273 AV_VAAPI_DRIVER_QUIRK_ATTRIB_MEMTYPE,
277 "Splitted-Desktop Systems VDPAU backend for VA-API",
278 AV_VAAPI_DRIVER_QUIRK_SURFACE_ATTRIBUTES,
282 static int vaapi_device_init(AVHWDeviceContext *hwdev)
284 VAAPIDeviceContext *ctx = hwdev->internal->priv;
285 AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
286 VAImageFormat *image_list = NULL;
288 const char *vendor_string;
289 int err, i, image_count;
290 enum AVPixelFormat pix_fmt;
293 image_count = vaMaxNumImageFormats(hwctx->display);
294 if (image_count <= 0) {
298 image_list = av_malloc(image_count * sizeof(*image_list));
300 err = AVERROR(ENOMEM);
303 vas = vaQueryImageFormats(hwctx->display, image_list, &image_count);
304 if (vas != VA_STATUS_SUCCESS) {
309 ctx->formats = av_malloc(image_count * sizeof(*ctx->formats));
311 err = AVERROR(ENOMEM);
315 for (i = 0; i < image_count; i++) {
316 fourcc = image_list[i].fourcc;
317 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
318 if (pix_fmt == AV_PIX_FMT_NONE) {
319 av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> unknown.\n",
322 av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> %s.\n",
323 fourcc, av_get_pix_fmt_name(pix_fmt));
324 ctx->formats[ctx->nb_formats].pix_fmt = pix_fmt;
325 ctx->formats[ctx->nb_formats].image_format = image_list[i];
330 if (hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_USER_SET) {
331 av_log(hwdev, AV_LOG_VERBOSE, "Not detecting driver: "
332 "quirks set by user.\n");
334 // Detect the driver in use and set quirk flags if necessary.
335 vendor_string = vaQueryVendorString(hwctx->display);
336 hwctx->driver_quirks = 0;
338 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_driver_quirks_table); i++) {
339 if (strstr(vendor_string,
340 vaapi_driver_quirks_table[i].match_string)) {
341 av_log(hwdev, AV_LOG_VERBOSE, "Matched \"%s\" as known "
342 "driver \"%s\".\n", vendor_string,
343 vaapi_driver_quirks_table[i].friendly_name);
344 hwctx->driver_quirks |=
345 vaapi_driver_quirks_table[i].quirks;
349 if (!(i < FF_ARRAY_ELEMS(vaapi_driver_quirks_table))) {
350 av_log(hwdev, AV_LOG_VERBOSE, "Unknown driver \"%s\", "
351 "assuming standard behaviour.\n", vendor_string);
359 av_freep(&ctx->formats);
364 static void vaapi_device_uninit(AVHWDeviceContext *hwdev)
366 VAAPIDeviceContext *ctx = hwdev->internal->priv;
368 av_freep(&ctx->formats);
371 static void vaapi_buffer_free(void *opaque, uint8_t *data)
373 AVHWFramesContext *hwfc = opaque;
374 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
375 VASurfaceID surface_id;
378 surface_id = (VASurfaceID)(uintptr_t)data;
380 vas = vaDestroySurfaces(hwctx->display, &surface_id, 1);
381 if (vas != VA_STATUS_SUCCESS) {
382 av_log(hwfc, AV_LOG_ERROR, "Failed to destroy surface %#x: "
383 "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
387 static AVBufferRef *vaapi_pool_alloc(void *opaque, int size)
389 AVHWFramesContext *hwfc = opaque;
390 VAAPIFramesContext *ctx = hwfc->internal->priv;
391 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
392 AVVAAPIFramesContext *avfc = hwfc->hwctx;
393 VASurfaceID surface_id;
397 if (hwfc->initial_pool_size > 0 &&
398 avfc->nb_surfaces >= hwfc->initial_pool_size)
401 vas = vaCreateSurfaces(hwctx->display, ctx->rt_format,
402 hwfc->width, hwfc->height,
404 ctx->attributes, ctx->nb_attributes);
405 if (vas != VA_STATUS_SUCCESS) {
406 av_log(hwfc, AV_LOG_ERROR, "Failed to create surface: "
407 "%d (%s).\n", vas, vaErrorStr(vas));
410 av_log(hwfc, AV_LOG_DEBUG, "Created surface %#x.\n", surface_id);
412 ref = av_buffer_create((uint8_t*)(uintptr_t)surface_id,
413 sizeof(surface_id), &vaapi_buffer_free,
414 hwfc, AV_BUFFER_FLAG_READONLY);
416 vaDestroySurfaces(hwctx->display, &surface_id, 1);
420 if (hwfc->initial_pool_size > 0) {
421 // This is a fixed-size pool, so we must still be in the initial
422 // allocation sequence.
423 av_assert0(avfc->nb_surfaces < hwfc->initial_pool_size);
424 avfc->surface_ids[avfc->nb_surfaces] = surface_id;
431 static int vaapi_frames_init(AVHWFramesContext *hwfc)
433 AVVAAPIFramesContext *avfc = hwfc->hwctx;
434 VAAPIFramesContext *ctx = hwfc->internal->priv;
435 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
436 VAImageFormat *expected_format;
437 AVBufferRef *test_surface = NULL;
438 VASurfaceID test_surface_id;
442 unsigned int fourcc, rt_format;
444 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++) {
445 if (vaapi_format_map[i].pix_fmt == hwfc->sw_format) {
446 fourcc = vaapi_format_map[i].fourcc;
447 rt_format = vaapi_format_map[i].rt_format;
451 if (i >= FF_ARRAY_ELEMS(vaapi_format_map)) {
452 av_log(hwfc, AV_LOG_ERROR, "Unsupported format: %s.\n",
453 av_get_pix_fmt_name(hwfc->sw_format));
454 return AVERROR(EINVAL);
458 if (!(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_SURFACE_ATTRIBUTES)) {
459 int need_memory_type = !(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_ATTRIB_MEMTYPE);
460 int need_pixel_format = 1;
461 for (i = 0; i < avfc->nb_attributes; i++) {
462 if (ctx->attributes[i].type == VASurfaceAttribMemoryType)
463 need_memory_type = 0;
464 if (ctx->attributes[i].type == VASurfaceAttribPixelFormat)
465 need_pixel_format = 0;
468 avfc->nb_attributes + need_memory_type + need_pixel_format;
470 ctx->attributes = av_malloc(ctx->nb_attributes *
471 sizeof(*ctx->attributes));
472 if (!ctx->attributes) {
473 err = AVERROR(ENOMEM);
477 for (i = 0; i < avfc->nb_attributes; i++)
478 ctx->attributes[i] = avfc->attributes[i];
479 if (need_memory_type) {
480 ctx->attributes[i++] = (VASurfaceAttrib) {
481 .type = VASurfaceAttribMemoryType,
482 .flags = VA_SURFACE_ATTRIB_SETTABLE,
483 .value.type = VAGenericValueTypeInteger,
484 .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA,
487 if (need_pixel_format) {
488 ctx->attributes[i++] = (VASurfaceAttrib) {
489 .type = VASurfaceAttribPixelFormat,
490 .flags = VA_SURFACE_ATTRIB_SETTABLE,
491 .value.type = VAGenericValueTypeInteger,
492 .value.value.i = fourcc,
495 av_assert0(i == ctx->nb_attributes);
497 ctx->attributes = NULL;
498 ctx->nb_attributes = 0;
501 ctx->rt_format = rt_format;
503 if (hwfc->initial_pool_size > 0) {
504 // This pool will be usable as a render target, so we need to store
505 // all of the surface IDs somewhere that vaCreateContext() calls
506 // will be able to access them.
507 avfc->nb_surfaces = 0;
508 avfc->surface_ids = av_malloc(hwfc->initial_pool_size *
509 sizeof(*avfc->surface_ids));
510 if (!avfc->surface_ids) {
511 err = AVERROR(ENOMEM);
515 // This pool allows dynamic sizing, and will not be usable as a
517 avfc->nb_surfaces = 0;
518 avfc->surface_ids = NULL;
521 hwfc->internal->pool_internal =
522 av_buffer_pool_init2(sizeof(VASurfaceID), hwfc,
523 &vaapi_pool_alloc, NULL);
524 if (!hwfc->internal->pool_internal) {
525 av_log(hwfc, AV_LOG_ERROR, "Failed to create VAAPI surface pool.\n");
526 err = AVERROR(ENOMEM);
531 // Allocate a single surface to test whether vaDeriveImage() is going
532 // to work for the specific configuration.
534 test_surface = av_buffer_pool_get(hwfc->pool);
536 av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
537 "user-configured buffer pool.\n");
538 err = AVERROR(ENOMEM);
542 test_surface = av_buffer_pool_get(hwfc->internal->pool_internal);
544 av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
545 "internal buffer pool.\n");
546 err = AVERROR(ENOMEM);
550 test_surface_id = (VASurfaceID)(uintptr_t)test_surface->data;
552 ctx->derive_works = 0;
554 err = vaapi_get_image_format(hwfc->device_ctx,
555 hwfc->sw_format, &expected_format);
557 vas = vaDeriveImage(hwctx->display, test_surface_id, &test_image);
558 if (vas == VA_STATUS_SUCCESS) {
559 if (expected_format->fourcc == test_image.format.fourcc) {
560 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping possible.\n");
561 ctx->derive_works = 1;
563 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
564 "derived image format %08x does not match "
565 "expected format %08x.\n",
566 expected_format->fourcc, test_image.format.fourcc);
568 vaDestroyImage(hwctx->display, test_image.image_id);
570 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
571 "deriving image does not work: "
572 "%d (%s).\n", vas, vaErrorStr(vas));
575 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
576 "image format is not supported.\n");
579 av_buffer_unref(&test_surface);
583 av_buffer_unref(&test_surface);
584 av_freep(&avfc->surface_ids);
585 av_freep(&ctx->attributes);
589 static void vaapi_frames_uninit(AVHWFramesContext *hwfc)
591 AVVAAPIFramesContext *avfc = hwfc->hwctx;
592 VAAPIFramesContext *ctx = hwfc->internal->priv;
594 av_freep(&avfc->surface_ids);
595 av_freep(&ctx->attributes);
598 static int vaapi_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
600 frame->buf[0] = av_buffer_pool_get(hwfc->pool);
602 return AVERROR(ENOMEM);
604 frame->data[3] = frame->buf[0]->data;
605 frame->format = AV_PIX_FMT_VAAPI;
606 frame->width = hwfc->width;
607 frame->height = hwfc->height;
612 static int vaapi_transfer_get_formats(AVHWFramesContext *hwfc,
613 enum AVHWFrameTransferDirection dir,
614 enum AVPixelFormat **formats)
616 VAAPIDeviceContext *ctx = hwfc->device_ctx->internal->priv;
617 enum AVPixelFormat *pix_fmts, preferred_format;
620 preferred_format = hwfc->sw_format;
622 pix_fmts = av_malloc((ctx->nb_formats + 1) * sizeof(*pix_fmts));
624 return AVERROR(ENOMEM);
626 pix_fmts[0] = preferred_format;
628 for (i = 0; i < ctx->nb_formats; i++) {
629 if (ctx->formats[i].pix_fmt == preferred_format)
631 av_assert0(k < ctx->nb_formats);
632 pix_fmts[k++] = ctx->formats[i].pix_fmt;
634 av_assert0(k == ctx->nb_formats);
635 pix_fmts[k] = AV_PIX_FMT_NONE;
641 static void vaapi_unmap_frame(AVHWFramesContext *hwfc,
642 HWMapDescriptor *hwmap)
644 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
645 VAAPIMapping *map = hwmap->priv;
646 VASurfaceID surface_id;
649 surface_id = (VASurfaceID)(uintptr_t)hwmap->source->data[3];
650 av_log(hwfc, AV_LOG_DEBUG, "Unmap surface %#x.\n", surface_id);
652 vas = vaUnmapBuffer(hwctx->display, map->image.buf);
653 if (vas != VA_STATUS_SUCCESS) {
654 av_log(hwfc, AV_LOG_ERROR, "Failed to unmap image from surface "
655 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
658 if ((map->flags & AV_HWFRAME_MAP_WRITE) &&
659 !(map->flags & AV_HWFRAME_MAP_DIRECT)) {
660 vas = vaPutImage(hwctx->display, surface_id, map->image.image_id,
661 0, 0, hwfc->width, hwfc->height,
662 0, 0, hwfc->width, hwfc->height);
663 if (vas != VA_STATUS_SUCCESS) {
664 av_log(hwfc, AV_LOG_ERROR, "Failed to write image to surface "
665 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
669 vas = vaDestroyImage(hwctx->display, map->image.image_id);
670 if (vas != VA_STATUS_SUCCESS) {
671 av_log(hwfc, AV_LOG_ERROR, "Failed to destroy image from surface "
672 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
678 static int vaapi_map_frame(AVHWFramesContext *hwfc,
679 AVFrame *dst, const AVFrame *src, int flags)
681 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
682 VAAPIFramesContext *ctx = hwfc->internal->priv;
683 VASurfaceID surface_id;
684 VAImageFormat *image_format;
687 void *address = NULL;
690 surface_id = (VASurfaceID)(uintptr_t)src->data[3];
691 av_log(hwfc, AV_LOG_DEBUG, "Map surface %#x.\n", surface_id);
693 if (!ctx->derive_works && (flags & AV_HWFRAME_MAP_DIRECT)) {
694 // Requested direct mapping but it is not possible.
695 return AVERROR(EINVAL);
697 if (dst->format == AV_PIX_FMT_NONE)
698 dst->format = hwfc->sw_format;
699 if (dst->format != hwfc->sw_format && (flags & AV_HWFRAME_MAP_DIRECT)) {
700 // Requested direct mapping but the formats do not match.
701 return AVERROR(EINVAL);
704 err = vaapi_get_image_format(hwfc->device_ctx, dst->format, &image_format);
706 // Requested format is not a valid output format.
707 return AVERROR(EINVAL);
710 map = av_malloc(sizeof(*map));
712 return AVERROR(ENOMEM);
714 map->image.image_id = VA_INVALID_ID;
716 vas = vaSyncSurface(hwctx->display, surface_id);
717 if (vas != VA_STATUS_SUCCESS) {
718 av_log(hwfc, AV_LOG_ERROR, "Failed to sync surface "
719 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
724 // The memory which we map using derive need not be connected to the CPU
725 // in a way conducive to fast access. On Gen7-Gen9 Intel graphics, the
726 // memory is mappable but not cached, so normal memcpy()-like access is
727 // very slow to read it (but writing is ok). It is possible to read much
728 // faster with a copy routine which is aware of the limitation, but we
729 // assume for now that the user is not aware of that and would therefore
730 // prefer not to be given direct-mapped memory if they request read access.
731 if (ctx->derive_works && dst->format == hwfc->sw_format &&
732 ((flags & AV_HWFRAME_MAP_DIRECT) || !(flags & AV_HWFRAME_MAP_READ))) {
733 vas = vaDeriveImage(hwctx->display, surface_id, &map->image);
734 if (vas != VA_STATUS_SUCCESS) {
735 av_log(hwfc, AV_LOG_ERROR, "Failed to derive image from "
736 "surface %#x: %d (%s).\n",
737 surface_id, vas, vaErrorStr(vas));
741 if (map->image.format.fourcc != image_format->fourcc) {
742 av_log(hwfc, AV_LOG_ERROR, "Derive image of surface %#x "
743 "is in wrong format: expected %#08x, got %#08x.\n",
744 surface_id, image_format->fourcc, map->image.format.fourcc);
748 map->flags |= AV_HWFRAME_MAP_DIRECT;
750 vas = vaCreateImage(hwctx->display, image_format,
751 hwfc->width, hwfc->height, &map->image);
752 if (vas != VA_STATUS_SUCCESS) {
753 av_log(hwfc, AV_LOG_ERROR, "Failed to create image for "
754 "surface %#x: %d (%s).\n",
755 surface_id, vas, vaErrorStr(vas));
759 if (!(flags & AV_HWFRAME_MAP_OVERWRITE)) {
760 vas = vaGetImage(hwctx->display, surface_id, 0, 0,
761 hwfc->width, hwfc->height, map->image.image_id);
762 if (vas != VA_STATUS_SUCCESS) {
763 av_log(hwfc, AV_LOG_ERROR, "Failed to read image from "
764 "surface %#x: %d (%s).\n",
765 surface_id, vas, vaErrorStr(vas));
772 vas = vaMapBuffer(hwctx->display, map->image.buf, &address);
773 if (vas != VA_STATUS_SUCCESS) {
774 av_log(hwfc, AV_LOG_ERROR, "Failed to map image from surface "
775 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
780 err = ff_hwframe_map_create(src->hw_frames_ctx,
781 dst, src, &vaapi_unmap_frame, map);
785 dst->width = src->width;
786 dst->height = src->height;
788 for (i = 0; i < map->image.num_planes; i++) {
789 dst->data[i] = (uint8_t*)address + map->image.offsets[i];
790 dst->linesize[i] = map->image.pitches[i];
793 #ifdef VA_FOURCC_YV16
794 map->image.format.fourcc == VA_FOURCC_YV16 ||
796 map->image.format.fourcc == VA_FOURCC_YV12) {
797 // Chroma planes are YVU rather than YUV, so swap them.
798 FFSWAP(uint8_t*, dst->data[1], dst->data[2]);
806 vaUnmapBuffer(hwctx->display, map->image.buf);
807 if (map->image.image_id != VA_INVALID_ID)
808 vaDestroyImage(hwctx->display, map->image.image_id);
814 static int vaapi_transfer_data_from(AVHWFramesContext *hwfc,
815 AVFrame *dst, const AVFrame *src)
820 if (dst->width > hwfc->width || dst->height > hwfc->height)
821 return AVERROR(EINVAL);
823 map = av_frame_alloc();
825 return AVERROR(ENOMEM);
826 map->format = dst->format;
828 err = vaapi_map_frame(hwfc, map, src, AV_HWFRAME_MAP_READ);
832 map->width = dst->width;
833 map->height = dst->height;
835 err = av_frame_copy(dst, map);
845 static int vaapi_transfer_data_to(AVHWFramesContext *hwfc,
846 AVFrame *dst, const AVFrame *src)
851 if (src->width > hwfc->width || src->height > hwfc->height)
852 return AVERROR(EINVAL);
854 map = av_frame_alloc();
856 return AVERROR(ENOMEM);
857 map->format = src->format;
859 err = vaapi_map_frame(hwfc, map, dst, AV_HWFRAME_MAP_WRITE | AV_HWFRAME_MAP_OVERWRITE);
863 map->width = src->width;
864 map->height = src->height;
866 err = av_frame_copy(map, src);
876 static int vaapi_map_from(AVHWFramesContext *hwfc, AVFrame *dst,
877 const AVFrame *src, int flags)
881 if (dst->format != AV_PIX_FMT_NONE) {
882 err = vaapi_get_image_format(hwfc->device_ctx, dst->format, NULL);
884 return AVERROR(ENOSYS);
887 err = vaapi_map_frame(hwfc, dst, src, flags);
891 err = av_frame_copy_props(dst, src);
898 static void vaapi_device_free(AVHWDeviceContext *ctx)
900 AVVAAPIDeviceContext *hwctx = ctx->hwctx;
901 VAAPIDevicePriv *priv = ctx->user_opaque;
904 vaTerminate(hwctx->display);
907 if (priv->x11_display)
908 XCloseDisplay(priv->x11_display);
911 if (priv->drm_fd >= 0)
917 static int vaapi_device_create(AVHWDeviceContext *ctx, const char *device,
918 AVDictionary *opts, int flags)
920 AVVAAPIDeviceContext *hwctx = ctx->hwctx;
921 VAAPIDevicePriv *priv;
922 VADisplay display = 0;
926 priv = av_mallocz(sizeof(*priv));
928 return AVERROR(ENOMEM);
932 ctx->user_opaque = priv;
933 ctx->free = vaapi_device_free;
936 if (!display && !(device && device[0] == '/')) {
937 // Try to open the device as an X11 display.
938 priv->x11_display = XOpenDisplay(device);
939 if (!priv->x11_display) {
940 av_log(ctx, AV_LOG_VERBOSE, "Cannot open X11 display "
941 "%s.\n", XDisplayName(device));
943 display = vaGetDisplay(priv->x11_display);
945 av_log(ctx, AV_LOG_ERROR, "Cannot open a VA display "
946 "from X11 display %s.\n", XDisplayName(device));
947 return AVERROR_UNKNOWN;
950 av_log(ctx, AV_LOG_VERBOSE, "Opened VA display via "
951 "X11 display %s.\n", XDisplayName(device));
958 // Try to open the device as a DRM path.
959 // Default to using the first render node if the user did not
961 const char *path = device ? device : "/dev/dri/renderD128";
962 priv->drm_fd = open(path, O_RDWR);
963 if (priv->drm_fd < 0) {
964 av_log(ctx, AV_LOG_VERBOSE, "Cannot open DRM device %s.\n",
967 display = vaGetDisplayDRM(priv->drm_fd);
969 av_log(ctx, AV_LOG_ERROR, "Cannot open a VA display "
970 "from DRM device %s.\n", path);
971 return AVERROR_UNKNOWN;
974 av_log(ctx, AV_LOG_VERBOSE, "Opened VA display via "
975 "DRM device %s.\n", path);
981 av_log(ctx, AV_LOG_ERROR, "No VA display found for "
982 "device: %s.\n", device ? device : "");
983 return AVERROR(EINVAL);
986 hwctx->display = display;
988 vas = vaInitialize(display, &major, &minor);
989 if (vas != VA_STATUS_SUCCESS) {
990 av_log(ctx, AV_LOG_ERROR, "Failed to initialise VAAPI "
991 "connection: %d (%s).\n", vas, vaErrorStr(vas));
994 av_log(ctx, AV_LOG_VERBOSE, "Initialised VAAPI connection: "
995 "version %d.%d\n", major, minor);
1000 const HWContextType ff_hwcontext_type_vaapi = {
1001 .type = AV_HWDEVICE_TYPE_VAAPI,
1004 .device_hwctx_size = sizeof(AVVAAPIDeviceContext),
1005 .device_priv_size = sizeof(VAAPIDeviceContext),
1006 .device_hwconfig_size = sizeof(AVVAAPIHWConfig),
1007 .frames_hwctx_size = sizeof(AVVAAPIFramesContext),
1008 .frames_priv_size = sizeof(VAAPIFramesContext),
1010 .device_create = &vaapi_device_create,
1011 .device_init = &vaapi_device_init,
1012 .device_uninit = &vaapi_device_uninit,
1013 .frames_get_constraints = &vaapi_frames_get_constraints,
1014 .frames_init = &vaapi_frames_init,
1015 .frames_uninit = &vaapi_frames_uninit,
1016 .frames_get_buffer = &vaapi_get_buffer,
1017 .transfer_get_formats = &vaapi_transfer_get_formats,
1018 .transfer_data_to = &vaapi_transfer_data_to,
1019 .transfer_data_from = &vaapi_transfer_data_from,
1021 .map_from = &vaapi_map_from,
1023 .pix_fmts = (const enum AVPixelFormat[]) {