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 "hwcontext.h"
23 #include "hwcontext_internal.h"
24 #include "hwcontext_vaapi.h"
29 typedef struct VAAPISurfaceFormat {
30 enum AVPixelFormat pix_fmt;
31 VAImageFormat image_format;
34 typedef struct VAAPIDeviceContext {
35 // Surface formats which can be used with this device.
36 VAAPISurfaceFormat *formats;
40 typedef struct VAAPIFramesContext {
41 // Surface attributes set at create time.
42 VASurfaceAttrib *attributes;
44 // RT format of the underlying surface (Intel driver ignores this anyway).
45 unsigned int rt_format;
46 // Whether vaDeriveImage works.
51 VAAPI_MAP_READ = 0x01,
52 VAAPI_MAP_WRITE = 0x02,
53 VAAPI_MAP_DIRECT = 0x04,
56 typedef struct VAAPISurfaceMap {
57 // The source hardware frame of this mapping (with hw_frames_ctx set).
58 const AVFrame *source;
59 // VAAPI_MAP_* flags which apply to this mapping.
61 // Handle to the derived or copied image which is mapped.
65 #define MAP(va, rt, av) { \
67 VA_RT_FORMAT_ ## rt, \
70 // The map fourcc <-> pix_fmt isn't bijective because of the annoying U/V
71 // plane swap cases. The frame handling below tries to hide these.
74 unsigned int rt_format;
75 enum AVPixelFormat pix_fmt;
76 } vaapi_format_map[] = {
77 MAP(NV12, YUV420, NV12),
78 MAP(YV12, YUV420, YUV420P), // With U/V planes swapped.
79 MAP(IYUV, YUV420, YUV420P),
80 //MAP(I420, YUV420, YUV420P), // Not in libva but used by Intel driver.
82 MAP(YV16, YUV422, YUV422P), // With U/V planes swapped.
84 MAP(422H, YUV422, YUV422P),
85 MAP(UYVY, YUV422, UYVY422),
86 MAP(YUY2, YUV422, YUYV422),
87 MAP(Y800, YUV400, GRAY8),
89 MAP(P010, YUV420_10BPP, P010),
91 MAP(BGRA, RGB32, BGRA),
92 MAP(BGRX, RGB32, BGR0),
93 MAP(RGBA, RGB32, RGBA),
94 MAP(RGBX, RGB32, RGB0),
95 MAP(ABGR, RGB32, ABGR),
96 MAP(XBGR, RGB32, 0BGR),
97 MAP(ARGB, RGB32, ARGB),
98 MAP(XRGB, RGB32, 0RGB),
102 static enum AVPixelFormat vaapi_pix_fmt_from_fourcc(unsigned int fourcc)
105 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++)
106 if (vaapi_format_map[i].fourcc == fourcc)
107 return vaapi_format_map[i].pix_fmt;
108 return AV_PIX_FMT_NONE;
111 static int vaapi_get_image_format(AVHWDeviceContext *hwdev,
112 enum AVPixelFormat pix_fmt,
113 VAImageFormat **image_format)
115 VAAPIDeviceContext *ctx = hwdev->internal->priv;
118 for (i = 0; i < ctx->nb_formats; i++) {
119 if (ctx->formats[i].pix_fmt == pix_fmt) {
120 *image_format = &ctx->formats[i].image_format;
124 return AVERROR(EINVAL);
127 static int vaapi_frames_get_constraints(AVHWDeviceContext *hwdev,
128 const void *hwconfig,
129 AVHWFramesConstraints *constraints)
131 AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
132 const AVVAAPIHWConfig *config = hwconfig;
133 AVVAAPIHWConfig *tmp_config;
134 VASurfaceAttrib *attr_list = NULL;
136 enum AVPixelFormat pix_fmt;
138 int err, i, j, attr_count, pix_fmt_count;
141 // No configuration was provided, so we create a temporary pipeline
142 // configuration in order to query all supported image formats.
144 tmp_config = av_mallocz(sizeof(*config));
146 return AVERROR(ENOMEM);
148 vas = vaCreateConfig(hwctx->display,
149 VAProfileNone, VAEntrypointVideoProc,
150 NULL, 0, &tmp_config->config_id);
151 if (vas != VA_STATUS_SUCCESS) {
152 // No vpp. We might still be able to do something useful if
153 // codecs are supported, so try to make the most-commonly
154 // supported decoder configuration we can to query instead.
155 vas = vaCreateConfig(hwctx->display,
156 VAProfileH264ConstrainedBaseline,
157 VAEntrypointVLD, NULL, 0,
158 &tmp_config->config_id);
159 if (vas != VA_STATUS_SUCCESS) {
160 av_freep(&tmp_config);
161 return AVERROR(ENOSYS);
169 vas = vaQuerySurfaceAttributes(hwctx->display, config->config_id,
171 if (vas != VA_STATUS_SUCCESS) {
172 av_log(hwdev, AV_LOG_ERROR, "Failed to query surface attributes: "
173 "%d (%s).\n", vas, vaErrorStr(vas));
174 err = AVERROR(ENOSYS);
178 attr_list = av_malloc(attr_count * sizeof(*attr_list));
180 err = AVERROR(ENOMEM);
184 vas = vaQuerySurfaceAttributes(hwctx->display, config->config_id,
185 attr_list, &attr_count);
186 if (vas != VA_STATUS_SUCCESS) {
187 av_log(hwdev, AV_LOG_ERROR, "Failed to query surface attributes: "
188 "%d (%s).\n", vas, vaErrorStr(vas));
189 err = AVERROR(ENOSYS);
194 for (i = 0; i < attr_count; i++) {
195 switch (attr_list[i].type) {
196 case VASurfaceAttribPixelFormat:
197 fourcc = attr_list[i].value.value.i;
198 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
199 if (pix_fmt != AV_PIX_FMT_NONE) {
202 // Something unsupported - ignore.
205 case VASurfaceAttribMinWidth:
206 constraints->min_width = attr_list[i].value.value.i;
208 case VASurfaceAttribMinHeight:
209 constraints->min_height = attr_list[i].value.value.i;
211 case VASurfaceAttribMaxWidth:
212 constraints->max_width = attr_list[i].value.value.i;
214 case VASurfaceAttribMaxHeight:
215 constraints->max_height = attr_list[i].value.value.i;
219 if (pix_fmt_count == 0) {
220 // Nothing usable found. Presumably there exists something which
221 // works, so leave the set null to indicate unknown.
222 constraints->valid_sw_formats = NULL;
224 constraints->valid_sw_formats = av_malloc_array(pix_fmt_count + 1,
226 if (!constraints->valid_sw_formats) {
227 err = AVERROR(ENOMEM);
231 for (i = j = 0; i < attr_count; i++) {
232 if (attr_list[i].type != VASurfaceAttribPixelFormat)
234 fourcc = attr_list[i].value.value.i;
235 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
236 if (pix_fmt != AV_PIX_FMT_NONE)
237 constraints->valid_sw_formats[j++] = pix_fmt;
239 av_assert0(j == pix_fmt_count);
240 constraints->valid_sw_formats[j] = AV_PIX_FMT_NONE;
243 constraints->valid_hw_formats = av_malloc_array(2, sizeof(pix_fmt));
244 if (!constraints->valid_hw_formats) {
245 err = AVERROR(ENOMEM);
248 constraints->valid_hw_formats[0] = AV_PIX_FMT_VAAPI;
249 constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
253 av_freep(&attr_list);
255 vaDestroyConfig(hwctx->display, tmp_config->config_id);
256 av_freep(&tmp_config);
261 static int vaapi_device_init(AVHWDeviceContext *hwdev)
263 VAAPIDeviceContext *ctx = hwdev->internal->priv;
264 AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
265 AVHWFramesConstraints *constraints = NULL;
266 VAImageFormat *image_list = NULL;
268 int err, i, j, image_count;
269 enum AVPixelFormat pix_fmt;
272 constraints = av_mallocz(sizeof(*constraints));
276 err = vaapi_frames_get_constraints(hwdev, NULL, constraints);
280 image_count = vaMaxNumImageFormats(hwctx->display);
281 if (image_count <= 0) {
285 image_list = av_malloc(image_count * sizeof(*image_list));
287 err = AVERROR(ENOMEM);
290 vas = vaQueryImageFormats(hwctx->display, image_list, &image_count);
291 if (vas != VA_STATUS_SUCCESS) {
296 ctx->formats = av_malloc(image_count * sizeof(*ctx->formats));
298 err = AVERROR(ENOMEM);
302 for (i = 0; i < image_count; i++) {
303 fourcc = image_list[i].fourcc;
304 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
305 for (j = 0; constraints->valid_sw_formats[j] != AV_PIX_FMT_NONE; j++) {
306 if (pix_fmt == constraints->valid_sw_formats[j])
309 if (constraints->valid_sw_formats[j] != AV_PIX_FMT_NONE) {
310 av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> %s.\n",
311 fourcc, av_get_pix_fmt_name(pix_fmt));
312 ctx->formats[ctx->nb_formats].pix_fmt = pix_fmt;
313 ctx->formats[ctx->nb_formats].image_format = image_list[i];
316 av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> unknown.\n", fourcc);
321 av_hwframe_constraints_free(&constraints);
324 av_freep(&ctx->formats);
326 av_hwframe_constraints_free(&constraints);
330 static void vaapi_device_uninit(AVHWDeviceContext *hwdev)
332 VAAPIDeviceContext *ctx = hwdev->internal->priv;
334 av_freep(&ctx->formats);
337 static void vaapi_buffer_free(void *opaque, uint8_t *data)
339 AVHWFramesContext *hwfc = opaque;
340 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
341 VASurfaceID surface_id;
344 surface_id = (VASurfaceID)(uintptr_t)data;
346 vas = vaDestroySurfaces(hwctx->display, &surface_id, 1);
347 if (vas != VA_STATUS_SUCCESS) {
348 av_log(hwfc, AV_LOG_ERROR, "Failed to destroy surface %#x: "
349 "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
353 static AVBufferRef *vaapi_pool_alloc(void *opaque, int size)
355 AVHWFramesContext *hwfc = opaque;
356 VAAPIFramesContext *ctx = hwfc->internal->priv;
357 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
358 AVVAAPIFramesContext *avfc = hwfc->hwctx;
359 VASurfaceID surface_id;
363 vas = vaCreateSurfaces(hwctx->display, ctx->rt_format,
364 hwfc->width, hwfc->height,
366 ctx->attributes, ctx->nb_attributes);
367 if (vas != VA_STATUS_SUCCESS) {
368 av_log(hwfc, AV_LOG_ERROR, "Failed to create surface: "
369 "%d (%s).\n", vas, vaErrorStr(vas));
372 av_log(hwfc, AV_LOG_DEBUG, "Created surface %#x.\n", surface_id);
374 ref = av_buffer_create((uint8_t*)(uintptr_t)surface_id,
375 sizeof(surface_id), &vaapi_buffer_free,
376 hwfc, AV_BUFFER_FLAG_READONLY);
378 vaDestroySurfaces(hwctx->display, &surface_id, 1);
382 if (hwfc->initial_pool_size > 0) {
383 // This is a fixed-size pool, so we must still be in the initial
384 // allocation sequence.
385 av_assert0(avfc->nb_surfaces < hwfc->initial_pool_size);
386 avfc->surface_ids[avfc->nb_surfaces] = surface_id;
393 static int vaapi_frames_init(AVHWFramesContext *hwfc)
395 AVVAAPIFramesContext *avfc = hwfc->hwctx;
396 VAAPIFramesContext *ctx = hwfc->internal->priv;
397 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
398 VAImageFormat *expected_format;
399 AVBufferRef *test_surface = NULL;
400 VASurfaceID test_surface_id;
404 unsigned int fourcc, rt_format;
406 for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++) {
407 if (vaapi_format_map[i].pix_fmt == hwfc->sw_format) {
408 fourcc = vaapi_format_map[i].fourcc;
409 rt_format = vaapi_format_map[i].rt_format;
413 if (i >= FF_ARRAY_ELEMS(vaapi_format_map)) {
414 av_log(hwfc, AV_LOG_ERROR, "Unsupported format: %s.\n",
415 av_get_pix_fmt_name(hwfc->sw_format));
416 return AVERROR(EINVAL);
420 int need_memory_type = 1, need_pixel_format = 1;
421 for (i = 0; i < avfc->nb_attributes; i++) {
422 if (ctx->attributes[i].type == VASurfaceAttribMemoryType)
423 need_memory_type = 0;
424 if (ctx->attributes[i].type == VASurfaceAttribPixelFormat)
425 need_pixel_format = 0;
428 avfc->nb_attributes + need_memory_type + need_pixel_format;
430 ctx->attributes = av_malloc(ctx->nb_attributes *
431 sizeof(*ctx->attributes));
432 if (!ctx->attributes) {
433 err = AVERROR(ENOMEM);
437 for (i = 0; i < avfc->nb_attributes; i++)
438 ctx->attributes[i] = avfc->attributes[i];
439 if (need_memory_type) {
440 ctx->attributes[i++] = (VASurfaceAttrib) {
441 .type = VASurfaceAttribMemoryType,
442 .flags = VA_SURFACE_ATTRIB_SETTABLE,
443 .value.type = VAGenericValueTypeInteger,
444 .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA,
447 if (need_pixel_format) {
448 ctx->attributes[i++] = (VASurfaceAttrib) {
449 .type = VASurfaceAttribPixelFormat,
450 .flags = VA_SURFACE_ATTRIB_SETTABLE,
451 .value.type = VAGenericValueTypeInteger,
452 .value.value.i = fourcc,
455 av_assert0(i == ctx->nb_attributes);
457 ctx->rt_format = rt_format;
459 if (hwfc->initial_pool_size > 0) {
460 // This pool will be usable as a render target, so we need to store
461 // all of the surface IDs somewhere that vaCreateContext() calls
462 // will be able to access them.
463 avfc->nb_surfaces = 0;
464 avfc->surface_ids = av_malloc(hwfc->initial_pool_size *
465 sizeof(*avfc->surface_ids));
466 if (!avfc->surface_ids) {
467 err = AVERROR(ENOMEM);
471 // This pool allows dynamic sizing, and will not be usable as a
473 avfc->nb_surfaces = 0;
474 avfc->surface_ids = NULL;
477 hwfc->internal->pool_internal =
478 av_buffer_pool_init2(sizeof(VASurfaceID), hwfc,
479 &vaapi_pool_alloc, NULL);
480 if (!hwfc->internal->pool_internal) {
481 av_log(hwfc, AV_LOG_ERROR, "Failed to create VAAPI surface pool.\n");
482 err = AVERROR(ENOMEM);
487 // Allocate a single surface to test whether vaDeriveImage() is going
488 // to work for the specific configuration.
490 test_surface = av_buffer_pool_get(hwfc->pool);
492 av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
493 "user-configured buffer pool.\n");
494 err = AVERROR(ENOMEM);
498 test_surface = av_buffer_pool_get(hwfc->internal->pool_internal);
500 av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
501 "internal buffer pool.\n");
502 err = AVERROR(ENOMEM);
506 test_surface_id = (VASurfaceID)(uintptr_t)test_surface->data;
508 ctx->derive_works = 0;
510 err = vaapi_get_image_format(hwfc->device_ctx,
511 hwfc->sw_format, &expected_format);
513 vas = vaDeriveImage(hwctx->display, test_surface_id, &test_image);
514 if (vas == VA_STATUS_SUCCESS) {
515 if (expected_format->fourcc == test_image.format.fourcc) {
516 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping possible.\n");
517 ctx->derive_works = 1;
519 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
520 "derived image format %08x does not match "
521 "expected format %08x.\n",
522 expected_format->fourcc, test_image.format.fourcc);
524 vaDestroyImage(hwctx->display, test_image.image_id);
526 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
527 "deriving image does not work: "
528 "%d (%s).\n", vas, vaErrorStr(vas));
531 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
532 "image format is not supported.\n");
535 av_buffer_unref(&test_surface);
539 av_buffer_unref(&test_surface);
540 av_freep(&avfc->surface_ids);
541 av_freep(&ctx->attributes);
545 static void vaapi_frames_uninit(AVHWFramesContext *hwfc)
547 AVVAAPIFramesContext *avfc = hwfc->hwctx;
548 VAAPIFramesContext *ctx = hwfc->internal->priv;
550 av_freep(&avfc->surface_ids);
551 av_freep(&ctx->attributes);
554 static int vaapi_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
556 frame->buf[0] = av_buffer_pool_get(hwfc->pool);
558 return AVERROR(ENOMEM);
560 frame->data[3] = frame->buf[0]->data;
561 frame->format = AV_PIX_FMT_VAAPI;
562 frame->width = hwfc->width;
563 frame->height = hwfc->height;
568 static int vaapi_transfer_get_formats(AVHWFramesContext *hwfc,
569 enum AVHWFrameTransferDirection dir,
570 enum AVPixelFormat **formats)
572 VAAPIDeviceContext *ctx = hwfc->device_ctx->internal->priv;
573 enum AVPixelFormat *pix_fmts, preferred_format;
576 preferred_format = hwfc->sw_format;
578 pix_fmts = av_malloc((ctx->nb_formats + 1) * sizeof(*pix_fmts));
580 return AVERROR(ENOMEM);
582 pix_fmts[0] = preferred_format;
584 for (i = 0; i < ctx->nb_formats; i++) {
585 if (ctx->formats[i].pix_fmt == preferred_format)
587 av_assert0(k < ctx->nb_formats);
588 pix_fmts[k++] = ctx->formats[i].pix_fmt;
590 av_assert0(k == ctx->nb_formats);
591 pix_fmts[k] = AV_PIX_FMT_NONE;
597 static void vaapi_unmap_frame(void *opaque, uint8_t *data)
599 AVHWFramesContext *hwfc = opaque;
600 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
601 VAAPISurfaceMap *map = (VAAPISurfaceMap*)data;
603 VASurfaceID surface_id;
607 surface_id = (VASurfaceID)(uintptr_t)src->data[3];
608 av_log(hwfc, AV_LOG_DEBUG, "Unmap surface %#x.\n", surface_id);
610 vas = vaUnmapBuffer(hwctx->display, map->image.buf);
611 if (vas != VA_STATUS_SUCCESS) {
612 av_log(hwfc, AV_LOG_ERROR, "Failed to unmap image from surface "
613 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
616 if ((map->flags & VAAPI_MAP_WRITE) &&
617 !(map->flags & VAAPI_MAP_DIRECT)) {
618 vas = vaPutImage(hwctx->display, surface_id, map->image.image_id,
619 0, 0, hwfc->width, hwfc->height,
620 0, 0, hwfc->width, hwfc->height);
621 if (vas != VA_STATUS_SUCCESS) {
622 av_log(hwfc, AV_LOG_ERROR, "Failed to write image to surface "
623 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
627 vas = vaDestroyImage(hwctx->display, map->image.image_id);
628 if (vas != VA_STATUS_SUCCESS) {
629 av_log(hwfc, AV_LOG_ERROR, "Failed to destroy image from surface "
630 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
636 static int vaapi_map_frame(AVHWFramesContext *hwfc,
637 AVFrame *dst, const AVFrame *src, int flags)
639 AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
640 VAAPIFramesContext *ctx = hwfc->internal->priv;
641 VASurfaceID surface_id;
642 VAImageFormat *image_format;
643 VAAPISurfaceMap *map;
645 void *address = NULL;
648 surface_id = (VASurfaceID)(uintptr_t)src->data[3];
649 av_log(hwfc, AV_LOG_DEBUG, "Map surface %#x.\n", surface_id);
651 if (!ctx->derive_works && (flags & VAAPI_MAP_DIRECT)) {
652 // Requested direct mapping but it is not possible.
653 return AVERROR(EINVAL);
655 if (dst->format == AV_PIX_FMT_NONE)
656 dst->format = hwfc->sw_format;
657 if (dst->format != hwfc->sw_format && (flags & VAAPI_MAP_DIRECT)) {
658 // Requested direct mapping but the formats do not match.
659 return AVERROR(EINVAL);
662 err = vaapi_get_image_format(hwfc->device_ctx, dst->format, &image_format);
664 // Requested format is not a valid output format.
665 return AVERROR(EINVAL);
668 map = av_malloc(sizeof(VAAPISurfaceMap));
670 return AVERROR(ENOMEM);
674 map->image.image_id = VA_INVALID_ID;
676 vas = vaSyncSurface(hwctx->display, surface_id);
677 if (vas != VA_STATUS_SUCCESS) {
678 av_log(hwfc, AV_LOG_ERROR, "Failed to sync surface "
679 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
684 // The memory which we map using derive need not be connected to the CPU
685 // in a way conducive to fast access. On Gen7-Gen9 Intel graphics, the
686 // memory is mappable but not cached, so normal memcpy()-like access is
687 // very slow to read it (but writing is ok). It is possible to read much
688 // faster with a copy routine which is aware of the limitation, but we
689 // assume for now that the user is not aware of that and would therefore
690 // prefer not to be given direct-mapped memory if they request read access.
691 if (ctx->derive_works &&
692 ((flags & VAAPI_MAP_DIRECT) || !(flags & VAAPI_MAP_READ))) {
693 vas = vaDeriveImage(hwctx->display, surface_id, &map->image);
694 if (vas != VA_STATUS_SUCCESS) {
695 av_log(hwfc, AV_LOG_ERROR, "Failed to derive image from "
696 "surface %#x: %d (%s).\n",
697 surface_id, vas, vaErrorStr(vas));
701 if (map->image.format.fourcc != image_format->fourcc) {
702 av_log(hwfc, AV_LOG_ERROR, "Derive image of surface %#x "
703 "is in wrong format: expected %#08x, got %#08x.\n",
704 surface_id, image_format->fourcc, map->image.format.fourcc);
708 map->flags |= VAAPI_MAP_DIRECT;
710 vas = vaCreateImage(hwctx->display, image_format,
711 hwfc->width, hwfc->height, &map->image);
712 if (vas != VA_STATUS_SUCCESS) {
713 av_log(hwfc, AV_LOG_ERROR, "Failed to create image for "
714 "surface %#x: %d (%s).\n",
715 surface_id, vas, vaErrorStr(vas));
719 if (flags & VAAPI_MAP_READ) {
720 vas = vaGetImage(hwctx->display, surface_id, 0, 0,
721 hwfc->width, hwfc->height, map->image.image_id);
722 if (vas != VA_STATUS_SUCCESS) {
723 av_log(hwfc, AV_LOG_ERROR, "Failed to read image from "
724 "surface %#x: %d (%s).\n",
725 surface_id, vas, vaErrorStr(vas));
732 vas = vaMapBuffer(hwctx->display, map->image.buf, &address);
733 if (vas != VA_STATUS_SUCCESS) {
734 av_log(hwfc, AV_LOG_ERROR, "Failed to map image from surface "
735 "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
740 dst->width = src->width;
741 dst->height = src->height;
743 for (i = 0; i < map->image.num_planes; i++) {
744 dst->data[i] = (uint8_t*)address + map->image.offsets[i];
745 dst->linesize[i] = map->image.pitches[i];
748 #ifdef VA_FOURCC_YV16
749 map->image.format.fourcc == VA_FOURCC_YV16 ||
751 map->image.format.fourcc == VA_FOURCC_YV12) {
752 // Chroma planes are YVU rather than YUV, so swap them.
753 FFSWAP(uint8_t*, dst->data[1], dst->data[2]);
756 dst->buf[0] = av_buffer_create((uint8_t*)map, sizeof(*map),
757 &vaapi_unmap_frame, hwfc, 0);
759 err = AVERROR(ENOMEM);
768 vaUnmapBuffer(hwctx->display, map->image.buf);
769 if (map->image.image_id != VA_INVALID_ID)
770 vaDestroyImage(hwctx->display, map->image.image_id);
776 static int vaapi_transfer_data_from(AVHWFramesContext *hwfc,
777 AVFrame *dst, const AVFrame *src)
782 map = av_frame_alloc();
784 return AVERROR(ENOMEM);
785 map->format = dst->format;
787 err = vaapi_map_frame(hwfc, map, src, VAAPI_MAP_READ);
791 err = av_frame_copy(dst, map);
801 static int vaapi_transfer_data_to(AVHWFramesContext *hwfc,
802 AVFrame *dst, const AVFrame *src)
807 map = av_frame_alloc();
809 return AVERROR(ENOMEM);
810 map->format = src->format;
812 err = vaapi_map_frame(hwfc, map, dst, VAAPI_MAP_WRITE);
816 err = av_frame_copy(map, src);
826 const HWContextType ff_hwcontext_type_vaapi = {
827 .type = AV_HWDEVICE_TYPE_VAAPI,
830 .device_hwctx_size = sizeof(AVVAAPIDeviceContext),
831 .device_priv_size = sizeof(VAAPIDeviceContext),
832 .device_hwconfig_size = sizeof(AVVAAPIHWConfig),
833 .frames_hwctx_size = sizeof(AVVAAPIFramesContext),
834 .frames_priv_size = sizeof(VAAPIFramesContext),
836 .device_init = &vaapi_device_init,
837 .device_uninit = &vaapi_device_uninit,
838 .frames_get_constraints = &vaapi_frames_get_constraints,
839 .frames_init = &vaapi_frames_init,
840 .frames_uninit = &vaapi_frames_uninit,
841 .frames_get_buffer = &vaapi_get_buffer,
842 .transfer_get_formats = &vaapi_transfer_get_formats,
843 .transfer_data_to = &vaapi_transfer_data_to,
844 .transfer_data_from = &vaapi_transfer_data_from,
846 .pix_fmts = (const enum AVPixelFormat[]) {