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>
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 #ifdef VA_FOURCC_ABGR
111 MAP(ABGR, RGB32, ABGR),
112 MAP(XBGR, RGB32, 0BGR),
114 MAP(ARGB, RGB32, ARGB),
115 MAP(XRGB, RGB32, 0RGB),
119 static enum AVPixelFormat vaapi_pix_fmt_from_fourcc(unsigned int fourcc)
122 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++)
123 if (vaapi_format_map[i].fourcc == fourcc)
124 return vaapi_format_map[i].pix_fmt;
125 return AV_PIX_FMT_NONE;
128 static int vaapi_get_image_format(AVHWDeviceContext *hwdev,
129 enum AVPixelFormat pix_fmt,
130 VAImageFormat **image_format)
132 VAAPIDeviceContext *ctx = hwdev->internal->priv;
135 for (i = 0; i < ctx->nb_formats; i++) {
136 if (ctx->formats[i].pix_fmt == pix_fmt) {
138 *image_format = &ctx->formats[i].image_format;
142 return AVERROR(EINVAL);
145 static int vaapi_frames_get_constraints(AVHWDeviceContext *hwdev,
146 const void *hwconfig,
147 AVHWFramesConstraints *constraints)
149 AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
150 const AVVAAPIHWConfig *config = hwconfig;
151 VAAPIDeviceContext *ctx = hwdev->internal->priv;
152 VASurfaceAttrib *attr_list = NULL;
154 enum AVPixelFormat pix_fmt;
156 int err, i, j, attr_count, pix_fmt_count;
160 vas = vaQuerySurfaceAttributes(hwctx->display, config->config_id,
162 if (vas != VA_STATUS_SUCCESS) {
163 av_log(hwdev, AV_LOG_ERROR, "Failed to query surface attributes: "
164 "%d (%s).\n", vas, vaErrorStr(vas));
165 err = AVERROR(ENOSYS);
169 attr_list = av_malloc(attr_count * sizeof(*attr_list));
171 err = AVERROR(ENOMEM);
175 vas = vaQuerySurfaceAttributes(hwctx->display, config->config_id,
176 attr_list, &attr_count);
177 if (vas != VA_STATUS_SUCCESS) {
178 av_log(hwdev, AV_LOG_ERROR, "Failed to query surface attributes: "
179 "%d (%s).\n", vas, vaErrorStr(vas));
180 err = AVERROR(ENOSYS);
185 for (i = 0; i < attr_count; i++) {
186 switch (attr_list[i].type) {
187 case VASurfaceAttribPixelFormat:
188 fourcc = attr_list[i].value.value.i;
189 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
190 if (pix_fmt != AV_PIX_FMT_NONE) {
193 // Something unsupported - ignore.
196 case VASurfaceAttribMinWidth:
197 constraints->min_width = attr_list[i].value.value.i;
199 case VASurfaceAttribMinHeight:
200 constraints->min_height = attr_list[i].value.value.i;
202 case VASurfaceAttribMaxWidth:
203 constraints->max_width = attr_list[i].value.value.i;
205 case VASurfaceAttribMaxHeight:
206 constraints->max_height = attr_list[i].value.value.i;
210 if (pix_fmt_count == 0) {
211 // Nothing usable found. Presumably there exists something which
212 // works, so leave the set null to indicate unknown.
213 constraints->valid_sw_formats = NULL;
215 constraints->valid_sw_formats = av_malloc_array(pix_fmt_count + 1,
217 if (!constraints->valid_sw_formats) {
218 err = AVERROR(ENOMEM);
222 for (i = j = 0; i < attr_count; i++) {
223 if (attr_list[i].type != VASurfaceAttribPixelFormat)
225 fourcc = attr_list[i].value.value.i;
226 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
227 if (pix_fmt != AV_PIX_FMT_NONE)
228 constraints->valid_sw_formats[j++] = pix_fmt;
230 av_assert0(j == pix_fmt_count);
231 constraints->valid_sw_formats[j] = AV_PIX_FMT_NONE;
234 // No configuration supplied.
235 // Return the full set of image formats known by the implementation.
236 constraints->valid_sw_formats = av_malloc_array(ctx->nb_formats + 1,
238 if (!constraints->valid_sw_formats) {
239 err = AVERROR(ENOMEM);
242 for (i = 0; i < ctx->nb_formats; i++)
243 constraints->valid_sw_formats[i] = ctx->formats[i].pix_fmt;
244 constraints->valid_sw_formats[i] = AV_PIX_FMT_NONE;
247 constraints->valid_hw_formats = av_malloc_array(2, sizeof(pix_fmt));
248 if (!constraints->valid_hw_formats) {
249 err = AVERROR(ENOMEM);
252 constraints->valid_hw_formats[0] = AV_PIX_FMT_VAAPI;
253 constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
257 av_freep(&attr_list);
261 static const struct {
262 const char *friendly_name;
263 const char *match_string;
265 } vaapi_driver_quirks_table[] = {
267 "Intel i965 (Quick Sync)",
269 AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS,
274 AV_VAAPI_DRIVER_QUIRK_ATTRIB_MEMTYPE,
278 static int vaapi_device_init(AVHWDeviceContext *hwdev)
280 VAAPIDeviceContext *ctx = hwdev->internal->priv;
281 AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
282 VAImageFormat *image_list = NULL;
284 const char *vendor_string;
285 int err, i, image_count;
286 enum AVPixelFormat pix_fmt;
289 image_count = vaMaxNumImageFormats(hwctx->display);
290 if (image_count <= 0) {
294 image_list = av_malloc(image_count * sizeof(*image_list));
296 err = AVERROR(ENOMEM);
299 vas = vaQueryImageFormats(hwctx->display, image_list, &image_count);
300 if (vas != VA_STATUS_SUCCESS) {
305 ctx->formats = av_malloc(image_count * sizeof(*ctx->formats));
307 err = AVERROR(ENOMEM);
311 for (i = 0; i < image_count; i++) {
312 fourcc = image_list[i].fourcc;
313 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
314 if (pix_fmt == AV_PIX_FMT_NONE) {
315 av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> unknown.\n",
318 av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> %s.\n",
319 fourcc, av_get_pix_fmt_name(pix_fmt));
320 ctx->formats[ctx->nb_formats].pix_fmt = pix_fmt;
321 ctx->formats[ctx->nb_formats].image_format = image_list[i];
326 if (hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_USER_SET) {
327 av_log(hwdev, AV_LOG_VERBOSE, "Not detecting driver: "
328 "quirks set by user.\n");
330 // Detect the driver in use and set quirk flags if necessary.
331 vendor_string = vaQueryVendorString(hwctx->display);
332 hwctx->driver_quirks = 0;
334 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_driver_quirks_table); i++) {
335 if (strstr(vendor_string,
336 vaapi_driver_quirks_table[i].match_string)) {
337 av_log(hwdev, AV_LOG_VERBOSE, "Matched \"%s\" as known "
338 "driver \"%s\".\n", vendor_string,
339 vaapi_driver_quirks_table[i].friendly_name);
340 hwctx->driver_quirks |=
341 vaapi_driver_quirks_table[i].quirks;
345 if (!(i < FF_ARRAY_ELEMS(vaapi_driver_quirks_table))) {
346 av_log(hwdev, AV_LOG_VERBOSE, "Unknown driver \"%s\", "
347 "assuming standard behaviour.\n", vendor_string);
355 av_freep(&ctx->formats);
360 static void vaapi_device_uninit(AVHWDeviceContext *hwdev)
362 VAAPIDeviceContext *ctx = hwdev->internal->priv;
364 av_freep(&ctx->formats);
367 static void vaapi_buffer_free(void *opaque, uint8_t *data)
369 AVHWFramesContext *hwfc = opaque;
370 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
371 VASurfaceID surface_id;
374 surface_id = (VASurfaceID)(uintptr_t)data;
376 vas = vaDestroySurfaces(hwctx->display, &surface_id, 1);
377 if (vas != VA_STATUS_SUCCESS) {
378 av_log(hwfc, AV_LOG_ERROR, "Failed to destroy surface %#x: "
379 "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
383 static AVBufferRef *vaapi_pool_alloc(void *opaque, int size)
385 AVHWFramesContext *hwfc = opaque;
386 VAAPIFramesContext *ctx = hwfc->internal->priv;
387 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
388 AVVAAPIFramesContext *avfc = hwfc->hwctx;
389 VASurfaceID surface_id;
393 if (hwfc->initial_pool_size > 0 &&
394 avfc->nb_surfaces >= hwfc->initial_pool_size)
397 vas = vaCreateSurfaces(hwctx->display, ctx->rt_format,
398 hwfc->width, hwfc->height,
400 ctx->attributes, ctx->nb_attributes);
401 if (vas != VA_STATUS_SUCCESS) {
402 av_log(hwfc, AV_LOG_ERROR, "Failed to create surface: "
403 "%d (%s).\n", vas, vaErrorStr(vas));
406 av_log(hwfc, AV_LOG_DEBUG, "Created surface %#x.\n", surface_id);
408 ref = av_buffer_create((uint8_t*)(uintptr_t)surface_id,
409 sizeof(surface_id), &vaapi_buffer_free,
410 hwfc, AV_BUFFER_FLAG_READONLY);
412 vaDestroySurfaces(hwctx->display, &surface_id, 1);
416 if (hwfc->initial_pool_size > 0) {
417 // This is a fixed-size pool, so we must still be in the initial
418 // allocation sequence.
419 av_assert0(avfc->nb_surfaces < hwfc->initial_pool_size);
420 avfc->surface_ids[avfc->nb_surfaces] = surface_id;
427 static int vaapi_frames_init(AVHWFramesContext *hwfc)
429 AVVAAPIFramesContext *avfc = hwfc->hwctx;
430 VAAPIFramesContext *ctx = hwfc->internal->priv;
431 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
432 VAImageFormat *expected_format;
433 AVBufferRef *test_surface = NULL;
434 VASurfaceID test_surface_id;
438 unsigned int fourcc, rt_format;
440 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++) {
441 if (vaapi_format_map[i].pix_fmt == hwfc->sw_format) {
442 fourcc = vaapi_format_map[i].fourcc;
443 rt_format = vaapi_format_map[i].rt_format;
447 if (i >= FF_ARRAY_ELEMS(vaapi_format_map)) {
448 av_log(hwfc, AV_LOG_ERROR, "Unsupported format: %s.\n",
449 av_get_pix_fmt_name(hwfc->sw_format));
450 return AVERROR(EINVAL);
454 int need_memory_type = !(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_ATTRIB_MEMTYPE);
455 int need_pixel_format = 1;
456 for (i = 0; i < avfc->nb_attributes; i++) {
457 if (ctx->attributes[i].type == VASurfaceAttribMemoryType)
458 need_memory_type = 0;
459 if (ctx->attributes[i].type == VASurfaceAttribPixelFormat)
460 need_pixel_format = 0;
463 avfc->nb_attributes + need_memory_type + need_pixel_format;
465 ctx->attributes = av_malloc(ctx->nb_attributes *
466 sizeof(*ctx->attributes));
467 if (!ctx->attributes) {
468 err = AVERROR(ENOMEM);
472 for (i = 0; i < avfc->nb_attributes; i++)
473 ctx->attributes[i] = avfc->attributes[i];
474 if (need_memory_type) {
475 ctx->attributes[i++] = (VASurfaceAttrib) {
476 .type = VASurfaceAttribMemoryType,
477 .flags = VA_SURFACE_ATTRIB_SETTABLE,
478 .value.type = VAGenericValueTypeInteger,
479 .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA,
482 if (need_pixel_format) {
483 ctx->attributes[i++] = (VASurfaceAttrib) {
484 .type = VASurfaceAttribPixelFormat,
485 .flags = VA_SURFACE_ATTRIB_SETTABLE,
486 .value.type = VAGenericValueTypeInteger,
487 .value.value.i = fourcc,
490 av_assert0(i == ctx->nb_attributes);
492 ctx->rt_format = rt_format;
494 if (hwfc->initial_pool_size > 0) {
495 // This pool will be usable as a render target, so we need to store
496 // all of the surface IDs somewhere that vaCreateContext() calls
497 // will be able to access them.
498 avfc->nb_surfaces = 0;
499 avfc->surface_ids = av_malloc(hwfc->initial_pool_size *
500 sizeof(*avfc->surface_ids));
501 if (!avfc->surface_ids) {
502 err = AVERROR(ENOMEM);
506 // This pool allows dynamic sizing, and will not be usable as a
508 avfc->nb_surfaces = 0;
509 avfc->surface_ids = NULL;
512 hwfc->internal->pool_internal =
513 av_buffer_pool_init2(sizeof(VASurfaceID), hwfc,
514 &vaapi_pool_alloc, NULL);
515 if (!hwfc->internal->pool_internal) {
516 av_log(hwfc, AV_LOG_ERROR, "Failed to create VAAPI surface pool.\n");
517 err = AVERROR(ENOMEM);
522 // Allocate a single surface to test whether vaDeriveImage() is going
523 // to work for the specific configuration.
525 test_surface = av_buffer_pool_get(hwfc->pool);
527 av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
528 "user-configured buffer pool.\n");
529 err = AVERROR(ENOMEM);
533 test_surface = av_buffer_pool_get(hwfc->internal->pool_internal);
535 av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
536 "internal buffer pool.\n");
537 err = AVERROR(ENOMEM);
541 test_surface_id = (VASurfaceID)(uintptr_t)test_surface->data;
543 ctx->derive_works = 0;
545 err = vaapi_get_image_format(hwfc->device_ctx,
546 hwfc->sw_format, &expected_format);
548 vas = vaDeriveImage(hwctx->display, test_surface_id, &test_image);
549 if (vas == VA_STATUS_SUCCESS) {
550 if (expected_format->fourcc == test_image.format.fourcc) {
551 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping possible.\n");
552 ctx->derive_works = 1;
554 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
555 "derived image format %08x does not match "
556 "expected format %08x.\n",
557 expected_format->fourcc, test_image.format.fourcc);
559 vaDestroyImage(hwctx->display, test_image.image_id);
561 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
562 "deriving image does not work: "
563 "%d (%s).\n", vas, vaErrorStr(vas));
566 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
567 "image format is not supported.\n");
570 av_buffer_unref(&test_surface);
574 av_buffer_unref(&test_surface);
575 av_freep(&avfc->surface_ids);
576 av_freep(&ctx->attributes);
580 static void vaapi_frames_uninit(AVHWFramesContext *hwfc)
582 AVVAAPIFramesContext *avfc = hwfc->hwctx;
583 VAAPIFramesContext *ctx = hwfc->internal->priv;
585 av_freep(&avfc->surface_ids);
586 av_freep(&ctx->attributes);
589 static int vaapi_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
591 frame->buf[0] = av_buffer_pool_get(hwfc->pool);
593 return AVERROR(ENOMEM);
595 frame->data[3] = frame->buf[0]->data;
596 frame->format = AV_PIX_FMT_VAAPI;
597 frame->width = hwfc->width;
598 frame->height = hwfc->height;
603 static int vaapi_transfer_get_formats(AVHWFramesContext *hwfc,
604 enum AVHWFrameTransferDirection dir,
605 enum AVPixelFormat **formats)
607 VAAPIDeviceContext *ctx = hwfc->device_ctx->internal->priv;
608 enum AVPixelFormat *pix_fmts, preferred_format;
611 preferred_format = hwfc->sw_format;
613 pix_fmts = av_malloc((ctx->nb_formats + 1) * sizeof(*pix_fmts));
615 return AVERROR(ENOMEM);
617 pix_fmts[0] = preferred_format;
619 for (i = 0; i < ctx->nb_formats; i++) {
620 if (ctx->formats[i].pix_fmt == preferred_format)
622 av_assert0(k < ctx->nb_formats);
623 pix_fmts[k++] = ctx->formats[i].pix_fmt;
625 av_assert0(k == ctx->nb_formats);
626 pix_fmts[k] = AV_PIX_FMT_NONE;
632 static void vaapi_unmap_frame(AVHWFramesContext *hwfc,
633 HWMapDescriptor *hwmap)
635 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
636 VAAPIMapping *map = hwmap->priv;
637 VASurfaceID surface_id;
640 surface_id = (VASurfaceID)(uintptr_t)hwmap->source->data[3];
641 av_log(hwfc, AV_LOG_DEBUG, "Unmap surface %#x.\n", surface_id);
643 vas = vaUnmapBuffer(hwctx->display, map->image.buf);
644 if (vas != VA_STATUS_SUCCESS) {
645 av_log(hwfc, AV_LOG_ERROR, "Failed to unmap image from surface "
646 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
649 if ((map->flags & AV_HWFRAME_MAP_WRITE) &&
650 !(map->flags & AV_HWFRAME_MAP_DIRECT)) {
651 vas = vaPutImage(hwctx->display, surface_id, map->image.image_id,
652 0, 0, hwfc->width, hwfc->height,
653 0, 0, hwfc->width, hwfc->height);
654 if (vas != VA_STATUS_SUCCESS) {
655 av_log(hwfc, AV_LOG_ERROR, "Failed to write image to surface "
656 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
660 vas = vaDestroyImage(hwctx->display, map->image.image_id);
661 if (vas != VA_STATUS_SUCCESS) {
662 av_log(hwfc, AV_LOG_ERROR, "Failed to destroy image from surface "
663 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
669 static int vaapi_map_frame(AVHWFramesContext *hwfc,
670 AVFrame *dst, const AVFrame *src, int flags)
672 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
673 VAAPIFramesContext *ctx = hwfc->internal->priv;
674 VASurfaceID surface_id;
675 VAImageFormat *image_format;
678 void *address = NULL;
681 surface_id = (VASurfaceID)(uintptr_t)src->data[3];
682 av_log(hwfc, AV_LOG_DEBUG, "Map surface %#x.\n", surface_id);
684 if (!ctx->derive_works && (flags & AV_HWFRAME_MAP_DIRECT)) {
685 // Requested direct mapping but it is not possible.
686 return AVERROR(EINVAL);
688 if (dst->format == AV_PIX_FMT_NONE)
689 dst->format = hwfc->sw_format;
690 if (dst->format != hwfc->sw_format && (flags & AV_HWFRAME_MAP_DIRECT)) {
691 // Requested direct mapping but the formats do not match.
692 return AVERROR(EINVAL);
695 err = vaapi_get_image_format(hwfc->device_ctx, dst->format, &image_format);
697 // Requested format is not a valid output format.
698 return AVERROR(EINVAL);
701 map = av_malloc(sizeof(*map));
703 return AVERROR(ENOMEM);
705 map->image.image_id = VA_INVALID_ID;
707 vas = vaSyncSurface(hwctx->display, surface_id);
708 if (vas != VA_STATUS_SUCCESS) {
709 av_log(hwfc, AV_LOG_ERROR, "Failed to sync surface "
710 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
715 // The memory which we map using derive need not be connected to the CPU
716 // in a way conducive to fast access. On Gen7-Gen9 Intel graphics, the
717 // memory is mappable but not cached, so normal memcpy()-like access is
718 // very slow to read it (but writing is ok). It is possible to read much
719 // faster with a copy routine which is aware of the limitation, but we
720 // assume for now that the user is not aware of that and would therefore
721 // prefer not to be given direct-mapped memory if they request read access.
722 if (ctx->derive_works && dst->format == hwfc->sw_format &&
723 ((flags & AV_HWFRAME_MAP_DIRECT) || !(flags & AV_HWFRAME_MAP_READ))) {
724 vas = vaDeriveImage(hwctx->display, surface_id, &map->image);
725 if (vas != VA_STATUS_SUCCESS) {
726 av_log(hwfc, AV_LOG_ERROR, "Failed to derive image from "
727 "surface %#x: %d (%s).\n",
728 surface_id, vas, vaErrorStr(vas));
732 if (map->image.format.fourcc != image_format->fourcc) {
733 av_log(hwfc, AV_LOG_ERROR, "Derive image of surface %#x "
734 "is in wrong format: expected %#08x, got %#08x.\n",
735 surface_id, image_format->fourcc, map->image.format.fourcc);
739 map->flags |= AV_HWFRAME_MAP_DIRECT;
741 vas = vaCreateImage(hwctx->display, image_format,
742 hwfc->width, hwfc->height, &map->image);
743 if (vas != VA_STATUS_SUCCESS) {
744 av_log(hwfc, AV_LOG_ERROR, "Failed to create image for "
745 "surface %#x: %d (%s).\n",
746 surface_id, vas, vaErrorStr(vas));
750 if (!(flags & AV_HWFRAME_MAP_OVERWRITE)) {
751 vas = vaGetImage(hwctx->display, surface_id, 0, 0,
752 hwfc->width, hwfc->height, map->image.image_id);
753 if (vas != VA_STATUS_SUCCESS) {
754 av_log(hwfc, AV_LOG_ERROR, "Failed to read image from "
755 "surface %#x: %d (%s).\n",
756 surface_id, vas, vaErrorStr(vas));
763 vas = vaMapBuffer(hwctx->display, map->image.buf, &address);
764 if (vas != VA_STATUS_SUCCESS) {
765 av_log(hwfc, AV_LOG_ERROR, "Failed to map image from surface "
766 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
771 err = ff_hwframe_map_create(src->hw_frames_ctx,
772 dst, src, &vaapi_unmap_frame, map);
776 dst->width = src->width;
777 dst->height = src->height;
779 for (i = 0; i < map->image.num_planes; i++) {
780 dst->data[i] = (uint8_t*)address + map->image.offsets[i];
781 dst->linesize[i] = map->image.pitches[i];
784 #ifdef VA_FOURCC_YV16
785 map->image.format.fourcc == VA_FOURCC_YV16 ||
787 map->image.format.fourcc == VA_FOURCC_YV12) {
788 // Chroma planes are YVU rather than YUV, so swap them.
789 FFSWAP(uint8_t*, dst->data[1], dst->data[2]);
797 vaUnmapBuffer(hwctx->display, map->image.buf);
798 if (map->image.image_id != VA_INVALID_ID)
799 vaDestroyImage(hwctx->display, map->image.image_id);
805 static int vaapi_transfer_data_from(AVHWFramesContext *hwfc,
806 AVFrame *dst, const AVFrame *src)
811 if (dst->width > hwfc->width || dst->height > hwfc->height)
812 return AVERROR(EINVAL);
814 map = av_frame_alloc();
816 return AVERROR(ENOMEM);
817 map->format = dst->format;
819 err = vaapi_map_frame(hwfc, map, src, AV_HWFRAME_MAP_READ);
823 map->width = dst->width;
824 map->height = dst->height;
826 err = av_frame_copy(dst, map);
836 static int vaapi_transfer_data_to(AVHWFramesContext *hwfc,
837 AVFrame *dst, const AVFrame *src)
842 if (src->width > hwfc->width || src->height > hwfc->height)
843 return AVERROR(EINVAL);
845 map = av_frame_alloc();
847 return AVERROR(ENOMEM);
848 map->format = src->format;
850 err = vaapi_map_frame(hwfc, map, dst, AV_HWFRAME_MAP_WRITE | AV_HWFRAME_MAP_OVERWRITE);
854 map->width = src->width;
855 map->height = src->height;
857 err = av_frame_copy(map, src);
867 static int vaapi_map_from(AVHWFramesContext *hwfc, AVFrame *dst,
868 const AVFrame *src, int flags)
872 if (dst->format != AV_PIX_FMT_NONE) {
873 err = vaapi_get_image_format(hwfc->device_ctx, dst->format, NULL);
875 return AVERROR(ENOSYS);
878 err = vaapi_map_frame(hwfc, dst, src, flags);
882 err = av_frame_copy_props(dst, src);
889 static void vaapi_device_free(AVHWDeviceContext *ctx)
891 AVVAAPIDeviceContext *hwctx = ctx->hwctx;
892 VAAPIDevicePriv *priv = ctx->user_opaque;
895 vaTerminate(hwctx->display);
898 if (priv->x11_display)
899 XCloseDisplay(priv->x11_display);
902 if (priv->drm_fd >= 0)
908 static int vaapi_device_create(AVHWDeviceContext *ctx, const char *device,
909 AVDictionary *opts, int flags)
911 AVVAAPIDeviceContext *hwctx = ctx->hwctx;
912 VAAPIDevicePriv *priv;
913 VADisplay display = 0;
917 priv = av_mallocz(sizeof(*priv));
919 return AVERROR(ENOMEM);
923 ctx->user_opaque = priv;
924 ctx->free = vaapi_device_free;
927 if (!display && !(device && device[0] == '/')) {
928 // Try to open the device as an X11 display.
929 priv->x11_display = XOpenDisplay(device);
930 if (!priv->x11_display) {
931 av_log(ctx, AV_LOG_VERBOSE, "Cannot open X11 display "
932 "%s.\n", XDisplayName(device));
934 display = vaGetDisplay(priv->x11_display);
936 av_log(ctx, AV_LOG_ERROR, "Cannot open a VA display "
937 "from X11 display %s.\n", XDisplayName(device));
938 return AVERROR_UNKNOWN;
941 av_log(ctx, AV_LOG_VERBOSE, "Opened VA display via "
942 "X11 display %s.\n", XDisplayName(device));
949 // Try to open the device as a DRM path.
950 // Default to using the first render node if the user did not
952 const char *path = device ? device : "/dev/dri/renderD128";
953 priv->drm_fd = open(path, O_RDWR);
954 if (priv->drm_fd < 0) {
955 av_log(ctx, AV_LOG_VERBOSE, "Cannot open DRM device %s.\n",
958 display = vaGetDisplayDRM(priv->drm_fd);
960 av_log(ctx, AV_LOG_ERROR, "Cannot open a VA display "
961 "from DRM device %s.\n", path);
962 return AVERROR_UNKNOWN;
965 av_log(ctx, AV_LOG_VERBOSE, "Opened VA display via "
966 "DRM device %s.\n", path);
972 av_log(ctx, AV_LOG_ERROR, "No VA display found for "
973 "device: %s.\n", device ? device : "");
974 return AVERROR(EINVAL);
977 hwctx->display = display;
979 vas = vaInitialize(display, &major, &minor);
980 if (vas != VA_STATUS_SUCCESS) {
981 av_log(ctx, AV_LOG_ERROR, "Failed to initialise VAAPI "
982 "connection: %d (%s).\n", vas, vaErrorStr(vas));
985 av_log(ctx, AV_LOG_VERBOSE, "Initialised VAAPI connection: "
986 "version %d.%d\n", major, minor);
991 const HWContextType ff_hwcontext_type_vaapi = {
992 .type = AV_HWDEVICE_TYPE_VAAPI,
995 .device_hwctx_size = sizeof(AVVAAPIDeviceContext),
996 .device_priv_size = sizeof(VAAPIDeviceContext),
997 .device_hwconfig_size = sizeof(AVVAAPIHWConfig),
998 .frames_hwctx_size = sizeof(AVVAAPIFramesContext),
999 .frames_priv_size = sizeof(VAAPIFramesContext),
1001 .device_create = &vaapi_device_create,
1002 .device_init = &vaapi_device_init,
1003 .device_uninit = &vaapi_device_uninit,
1004 .frames_get_constraints = &vaapi_frames_get_constraints,
1005 .frames_init = &vaapi_frames_init,
1006 .frames_uninit = &vaapi_frames_uninit,
1007 .frames_get_buffer = &vaapi_get_buffer,
1008 .transfer_get_formats = &vaapi_transfer_get_formats,
1009 .transfer_data_to = &vaapi_transfer_data_to,
1010 .transfer_data_from = &vaapi_transfer_data_from,
1012 .map_from = &vaapi_map_from,
1014 .pix_fmts = (const enum AVPixelFormat[]) {