]> git.sesse.net Git - ffmpeg/blob - libavutil/hwcontext_vaapi.c
Merge commit '83fef16b6a8dbbcbd80d159ba3ebe818dbbb2776'
[ffmpeg] / libavutil / hwcontext_vaapi.c
1 /*
2  * This file is part of FFmpeg.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include "config.h"
20
21 #if HAVE_VAAPI_X11
22 #   include <va/va_x11.h>
23 #endif
24 #if HAVE_VAAPI_DRM
25 #   include <va/va_drm.h>
26 #endif
27
28 #if CONFIG_LIBDRM
29 #   include <va/va_drmcommon.h>
30 #   include <drm_fourcc.h>
31 #endif
32
33 #include <fcntl.h>
34 #if HAVE_UNISTD_H
35 #   include <unistd.h>
36 #endif
37
38
39 #include "avassert.h"
40 #include "buffer.h"
41 #include "common.h"
42 #include "hwcontext.h"
43 #include "hwcontext_internal.h"
44 #include "hwcontext_vaapi.h"
45 #include "mem.h"
46 #include "pixdesc.h"
47 #include "pixfmt.h"
48
49 #if CONFIG_LIBDRM
50 #   include "hwcontext_drm.h"
51 #endif
52
53 typedef struct VAAPIDevicePriv {
54 #if HAVE_VAAPI_X11
55     Display *x11_display;
56 #endif
57
58     int drm_fd;
59 } VAAPIDevicePriv;
60
61 typedef struct VAAPISurfaceFormat {
62     enum AVPixelFormat pix_fmt;
63     VAImageFormat image_format;
64 } VAAPISurfaceFormat;
65
66 typedef struct VAAPIDeviceContext {
67     // Surface formats which can be used with this device.
68     VAAPISurfaceFormat *formats;
69     int              nb_formats;
70 } VAAPIDeviceContext;
71
72 typedef struct VAAPIFramesContext {
73     // Surface attributes set at create time.
74     VASurfaceAttrib *attributes;
75     int           nb_attributes;
76     // RT format of the underlying surface (Intel driver ignores this anyway).
77     unsigned int rt_format;
78     // Whether vaDeriveImage works.
79     int derive_works;
80 } VAAPIFramesContext;
81
82 typedef struct VAAPIMapping {
83     // Handle to the derived or copied image which is mapped.
84     VAImage image;
85     // The mapping flags actually used.
86     int flags;
87 } VAAPIMapping;
88
89 #define MAP(va, rt, av) { \
90         VA_FOURCC_ ## va, \
91         VA_RT_FORMAT_ ## rt, \
92         AV_PIX_FMT_ ## av \
93     }
94 // The map fourcc <-> pix_fmt isn't bijective because of the annoying U/V
95 // plane swap cases.  The frame handling below tries to hide these.
96 static const struct {
97     unsigned int fourcc;
98     unsigned int rt_format;
99     enum AVPixelFormat pix_fmt;
100 } vaapi_format_map[] = {
101     MAP(NV12, YUV420,  NV12),
102     MAP(YV12, YUV420,  YUV420P), // With U/V planes swapped.
103     MAP(IYUV, YUV420,  YUV420P),
104 #ifdef VA_FOURCC_I420
105     MAP(I420, YUV420,  YUV420P),
106 #endif
107 #ifdef VA_FOURCC_YV16
108     MAP(YV16, YUV422,  YUV422P), // With U/V planes swapped.
109 #endif
110     MAP(422H, YUV422,  YUV422P),
111     MAP(UYVY, YUV422,  UYVY422),
112     MAP(YUY2, YUV422,  YUYV422),
113     MAP(411P, YUV411,  YUV411P),
114     MAP(422V, YUV422,  YUV440P),
115     MAP(444P, YUV444,  YUV444P),
116     MAP(Y800, YUV400,  GRAY8),
117 #ifdef VA_FOURCC_P010
118     MAP(P010, YUV420_10BPP, P010),
119 #endif
120     MAP(BGRA, RGB32,   BGRA),
121     MAP(BGRX, RGB32,   BGR0),
122     MAP(RGBA, RGB32,   RGBA),
123     MAP(RGBX, RGB32,   RGB0),
124 #ifdef VA_FOURCC_ABGR
125     MAP(ABGR, RGB32,   ABGR),
126     MAP(XBGR, RGB32,   0BGR),
127 #endif
128     MAP(ARGB, RGB32,   ARGB),
129     MAP(XRGB, RGB32,   0RGB),
130 };
131 #undef MAP
132
133 static enum AVPixelFormat vaapi_pix_fmt_from_fourcc(unsigned int fourcc)
134 {
135     int i;
136     for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++)
137         if (vaapi_format_map[i].fourcc == fourcc)
138             return vaapi_format_map[i].pix_fmt;
139     return AV_PIX_FMT_NONE;
140 }
141
142 static int vaapi_get_image_format(AVHWDeviceContext *hwdev,
143                                   enum AVPixelFormat pix_fmt,
144                                   VAImageFormat **image_format)
145 {
146     VAAPIDeviceContext *ctx = hwdev->internal->priv;
147     int i;
148
149     for (i = 0; i < ctx->nb_formats; i++) {
150         if (ctx->formats[i].pix_fmt == pix_fmt) {
151             if (image_format)
152                 *image_format = &ctx->formats[i].image_format;
153             return 0;
154         }
155     }
156     return AVERROR(EINVAL);
157 }
158
159 static int vaapi_frames_get_constraints(AVHWDeviceContext *hwdev,
160                                         const void *hwconfig,
161                                         AVHWFramesConstraints *constraints)
162 {
163     AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
164     const AVVAAPIHWConfig *config = hwconfig;
165     VAAPIDeviceContext *ctx = hwdev->internal->priv;
166     VASurfaceAttrib *attr_list = NULL;
167     VAStatus vas;
168     enum AVPixelFormat pix_fmt;
169     unsigned int fourcc;
170     int err, i, j, attr_count, pix_fmt_count;
171
172     if (config &&
173         !(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_SURFACE_ATTRIBUTES)) {
174         attr_count = 0;
175         vas = vaQuerySurfaceAttributes(hwctx->display, config->config_id,
176                                        0, &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);
181             goto fail;
182         }
183
184         attr_list = av_malloc(attr_count * sizeof(*attr_list));
185         if (!attr_list) {
186             err = AVERROR(ENOMEM);
187             goto fail;
188         }
189
190         vas = vaQuerySurfaceAttributes(hwctx->display, config->config_id,
191                                        attr_list, &attr_count);
192         if (vas != VA_STATUS_SUCCESS) {
193             av_log(hwdev, AV_LOG_ERROR, "Failed to query surface attributes: "
194                    "%d (%s).\n", vas, vaErrorStr(vas));
195             err = AVERROR(ENOSYS);
196             goto fail;
197         }
198
199         pix_fmt_count = 0;
200         for (i = 0; i < attr_count; i++) {
201             switch (attr_list[i].type) {
202             case VASurfaceAttribPixelFormat:
203                 fourcc = attr_list[i].value.value.i;
204                 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
205                 if (pix_fmt != AV_PIX_FMT_NONE) {
206                     ++pix_fmt_count;
207                 } else {
208                     // Something unsupported - ignore.
209                 }
210                 break;
211             case VASurfaceAttribMinWidth:
212                 constraints->min_width  = attr_list[i].value.value.i;
213                 break;
214             case VASurfaceAttribMinHeight:
215                 constraints->min_height = attr_list[i].value.value.i;
216                 break;
217             case VASurfaceAttribMaxWidth:
218                 constraints->max_width  = attr_list[i].value.value.i;
219                 break;
220             case VASurfaceAttribMaxHeight:
221                 constraints->max_height = attr_list[i].value.value.i;
222                 break;
223             }
224         }
225         if (pix_fmt_count == 0) {
226             // Nothing usable found.  Presumably there exists something which
227             // works, so leave the set null to indicate unknown.
228             constraints->valid_sw_formats = NULL;
229         } else {
230             constraints->valid_sw_formats = av_malloc_array(pix_fmt_count + 1,
231                                                             sizeof(pix_fmt));
232             if (!constraints->valid_sw_formats) {
233                 err = AVERROR(ENOMEM);
234                 goto fail;
235             }
236
237             for (i = j = 0; i < attr_count; i++) {
238                 if (attr_list[i].type != VASurfaceAttribPixelFormat)
239                     continue;
240                 fourcc = attr_list[i].value.value.i;
241                 pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
242                 if (pix_fmt != AV_PIX_FMT_NONE)
243                     constraints->valid_sw_formats[j++] = pix_fmt;
244             }
245             av_assert0(j == pix_fmt_count);
246             constraints->valid_sw_formats[j] = AV_PIX_FMT_NONE;
247         }
248     } else {
249         // No configuration supplied.
250         // Return the full set of image formats known by the implementation.
251         constraints->valid_sw_formats = av_malloc_array(ctx->nb_formats + 1,
252                                                         sizeof(pix_fmt));
253         if (!constraints->valid_sw_formats) {
254             err = AVERROR(ENOMEM);
255             goto fail;
256         }
257         for (i = 0; i < ctx->nb_formats; i++)
258             constraints->valid_sw_formats[i] = ctx->formats[i].pix_fmt;
259         constraints->valid_sw_formats[i] = AV_PIX_FMT_NONE;
260     }
261
262     constraints->valid_hw_formats = av_malloc_array(2, sizeof(pix_fmt));
263     if (!constraints->valid_hw_formats) {
264         err = AVERROR(ENOMEM);
265         goto fail;
266     }
267     constraints->valid_hw_formats[0] = AV_PIX_FMT_VAAPI;
268     constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
269
270     err = 0;
271 fail:
272     av_freep(&attr_list);
273     return err;
274 }
275
276 static const struct {
277     const char *friendly_name;
278     const char *match_string;
279     unsigned int quirks;
280 } vaapi_driver_quirks_table[] = {
281     {
282         "Intel i965 (Quick Sync)",
283         "i965",
284         AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS,
285     },
286     {
287         "Intel iHD",
288         "ubit",
289         AV_VAAPI_DRIVER_QUIRK_ATTRIB_MEMTYPE,
290     },
291     {
292         "VDPAU wrapper",
293         "Splitted-Desktop Systems VDPAU backend for VA-API",
294         AV_VAAPI_DRIVER_QUIRK_SURFACE_ATTRIBUTES,
295     },
296 };
297
298 static int vaapi_device_init(AVHWDeviceContext *hwdev)
299 {
300     VAAPIDeviceContext *ctx = hwdev->internal->priv;
301     AVVAAPIDeviceContext *hwctx = hwdev->hwctx;
302     VAImageFormat *image_list = NULL;
303     VAStatus vas;
304     const char *vendor_string;
305     int err, i, image_count;
306     enum AVPixelFormat pix_fmt;
307     unsigned int fourcc;
308
309     image_count = vaMaxNumImageFormats(hwctx->display);
310     if (image_count <= 0) {
311         err = AVERROR(EIO);
312         goto fail;
313     }
314     image_list = av_malloc(image_count * sizeof(*image_list));
315     if (!image_list) {
316         err = AVERROR(ENOMEM);
317         goto fail;
318     }
319     vas = vaQueryImageFormats(hwctx->display, image_list, &image_count);
320     if (vas != VA_STATUS_SUCCESS) {
321         err = AVERROR(EIO);
322         goto fail;
323     }
324
325     ctx->formats  = av_malloc(image_count * sizeof(*ctx->formats));
326     if (!ctx->formats) {
327         err = AVERROR(ENOMEM);
328         goto fail;
329     }
330     ctx->nb_formats = 0;
331     for (i = 0; i < image_count; i++) {
332         fourcc  = image_list[i].fourcc;
333         pix_fmt = vaapi_pix_fmt_from_fourcc(fourcc);
334         if (pix_fmt == AV_PIX_FMT_NONE) {
335             av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> unknown.\n",
336                    fourcc);
337         } else {
338             av_log(hwdev, AV_LOG_DEBUG, "Format %#x -> %s.\n",
339                    fourcc, av_get_pix_fmt_name(pix_fmt));
340             ctx->formats[ctx->nb_formats].pix_fmt      = pix_fmt;
341             ctx->formats[ctx->nb_formats].image_format = image_list[i];
342             ++ctx->nb_formats;
343         }
344     }
345
346     if (hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_USER_SET) {
347         av_log(hwdev, AV_LOG_VERBOSE, "Not detecting driver: "
348                "quirks set by user.\n");
349     } else {
350         // Detect the driver in use and set quirk flags if necessary.
351         vendor_string = vaQueryVendorString(hwctx->display);
352         hwctx->driver_quirks = 0;
353         if (vendor_string) {
354             for (i = 0; i < FF_ARRAY_ELEMS(vaapi_driver_quirks_table); i++) {
355                 if (strstr(vendor_string,
356                            vaapi_driver_quirks_table[i].match_string)) {
357                     av_log(hwdev, AV_LOG_VERBOSE, "Matched \"%s\" as known "
358                            "driver \"%s\".\n", vendor_string,
359                            vaapi_driver_quirks_table[i].friendly_name);
360                     hwctx->driver_quirks |=
361                         vaapi_driver_quirks_table[i].quirks;
362                     break;
363                 }
364             }
365             if (!(i < FF_ARRAY_ELEMS(vaapi_driver_quirks_table))) {
366                 av_log(hwdev, AV_LOG_VERBOSE, "Unknown driver \"%s\", "
367                        "assuming standard behaviour.\n", vendor_string);
368             }
369         }
370     }
371
372     av_free(image_list);
373     return 0;
374 fail:
375     av_freep(&ctx->formats);
376     av_free(image_list);
377     return err;
378 }
379
380 static void vaapi_device_uninit(AVHWDeviceContext *hwdev)
381 {
382     VAAPIDeviceContext *ctx = hwdev->internal->priv;
383
384     av_freep(&ctx->formats);
385 }
386
387 static void vaapi_buffer_free(void *opaque, uint8_t *data)
388 {
389     AVHWFramesContext     *hwfc = opaque;
390     AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
391     VASurfaceID surface_id;
392     VAStatus vas;
393
394     surface_id = (VASurfaceID)(uintptr_t)data;
395
396     vas = vaDestroySurfaces(hwctx->display, &surface_id, 1);
397     if (vas != VA_STATUS_SUCCESS) {
398         av_log(hwfc, AV_LOG_ERROR, "Failed to destroy surface %#x: "
399                "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
400     }
401 }
402
403 static AVBufferRef *vaapi_pool_alloc(void *opaque, int size)
404 {
405     AVHWFramesContext     *hwfc = opaque;
406     VAAPIFramesContext     *ctx = hwfc->internal->priv;
407     AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
408     AVVAAPIFramesContext  *avfc = hwfc->hwctx;
409     VASurfaceID surface_id;
410     VAStatus vas;
411     AVBufferRef *ref;
412
413     if (hwfc->initial_pool_size > 0 &&
414         avfc->nb_surfaces >= hwfc->initial_pool_size)
415         return NULL;
416
417     vas = vaCreateSurfaces(hwctx->display, ctx->rt_format,
418                            hwfc->width, hwfc->height,
419                            &surface_id, 1,
420                            ctx->attributes, ctx->nb_attributes);
421     if (vas != VA_STATUS_SUCCESS) {
422         av_log(hwfc, AV_LOG_ERROR, "Failed to create surface: "
423                "%d (%s).\n", vas, vaErrorStr(vas));
424         return NULL;
425     }
426     av_log(hwfc, AV_LOG_DEBUG, "Created surface %#x.\n", surface_id);
427
428     ref = av_buffer_create((uint8_t*)(uintptr_t)surface_id,
429                            sizeof(surface_id), &vaapi_buffer_free,
430                            hwfc, AV_BUFFER_FLAG_READONLY);
431     if (!ref) {
432         vaDestroySurfaces(hwctx->display, &surface_id, 1);
433         return NULL;
434     }
435
436     if (hwfc->initial_pool_size > 0) {
437         // This is a fixed-size pool, so we must still be in the initial
438         // allocation sequence.
439         av_assert0(avfc->nb_surfaces < hwfc->initial_pool_size);
440         avfc->surface_ids[avfc->nb_surfaces] = surface_id;
441         ++avfc->nb_surfaces;
442     }
443
444     return ref;
445 }
446
447 static int vaapi_frames_init(AVHWFramesContext *hwfc)
448 {
449     AVVAAPIFramesContext  *avfc = hwfc->hwctx;
450     VAAPIFramesContext     *ctx = hwfc->internal->priv;
451     AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
452     VAImageFormat *expected_format;
453     AVBufferRef *test_surface = NULL;
454     VASurfaceID test_surface_id;
455     VAImage test_image;
456     VAStatus vas;
457     int err, i;
458     unsigned int fourcc, rt_format;
459
460     for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++) {
461         if (vaapi_format_map[i].pix_fmt == hwfc->sw_format) {
462             fourcc    = vaapi_format_map[i].fourcc;
463             rt_format = vaapi_format_map[i].rt_format;
464             break;
465         }
466     }
467     if (i >= FF_ARRAY_ELEMS(vaapi_format_map)) {
468         av_log(hwfc, AV_LOG_ERROR, "Unsupported format: %s.\n",
469                av_get_pix_fmt_name(hwfc->sw_format));
470         return AVERROR(EINVAL);
471     }
472
473     if (!hwfc->pool) {
474         if (!(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_SURFACE_ATTRIBUTES)) {
475             int need_memory_type = !(hwctx->driver_quirks & AV_VAAPI_DRIVER_QUIRK_ATTRIB_MEMTYPE);
476             int need_pixel_format = 1;
477             for (i = 0; i < avfc->nb_attributes; i++) {
478                 if (avfc->attributes[i].type == VASurfaceAttribMemoryType)
479                     need_memory_type  = 0;
480                 if (avfc->attributes[i].type == VASurfaceAttribPixelFormat)
481                     need_pixel_format = 0;
482             }
483             ctx->nb_attributes =
484                 avfc->nb_attributes + need_memory_type + need_pixel_format;
485
486             ctx->attributes = av_malloc(ctx->nb_attributes *
487                                         sizeof(*ctx->attributes));
488             if (!ctx->attributes) {
489                 err = AVERROR(ENOMEM);
490                 goto fail;
491             }
492
493             for (i = 0; i < avfc->nb_attributes; i++)
494                 ctx->attributes[i] = avfc->attributes[i];
495             if (need_memory_type) {
496                 ctx->attributes[i++] = (VASurfaceAttrib) {
497                     .type          = VASurfaceAttribMemoryType,
498                     .flags         = VA_SURFACE_ATTRIB_SETTABLE,
499                     .value.type    = VAGenericValueTypeInteger,
500                     .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_VA,
501                 };
502             }
503             if (need_pixel_format) {
504                 ctx->attributes[i++] = (VASurfaceAttrib) {
505                     .type          = VASurfaceAttribPixelFormat,
506                     .flags         = VA_SURFACE_ATTRIB_SETTABLE,
507                     .value.type    = VAGenericValueTypeInteger,
508                     .value.value.i = fourcc,
509                 };
510             }
511             av_assert0(i == ctx->nb_attributes);
512         } else {
513             ctx->attributes = NULL;
514             ctx->nb_attributes = 0;
515         }
516
517         ctx->rt_format = rt_format;
518
519         if (hwfc->initial_pool_size > 0) {
520             // This pool will be usable as a render target, so we need to store
521             // all of the surface IDs somewhere that vaCreateContext() calls
522             // will be able to access them.
523             avfc->nb_surfaces = 0;
524             avfc->surface_ids = av_malloc(hwfc->initial_pool_size *
525                                           sizeof(*avfc->surface_ids));
526             if (!avfc->surface_ids) {
527                 err = AVERROR(ENOMEM);
528                 goto fail;
529             }
530         } else {
531             // This pool allows dynamic sizing, and will not be usable as a
532             // render target.
533             avfc->nb_surfaces = 0;
534             avfc->surface_ids = NULL;
535         }
536
537         hwfc->internal->pool_internal =
538             av_buffer_pool_init2(sizeof(VASurfaceID), hwfc,
539                                  &vaapi_pool_alloc, NULL);
540         if (!hwfc->internal->pool_internal) {
541             av_log(hwfc, AV_LOG_ERROR, "Failed to create VAAPI surface pool.\n");
542             err = AVERROR(ENOMEM);
543             goto fail;
544         }
545     }
546
547     // Allocate a single surface to test whether vaDeriveImage() is going
548     // to work for the specific configuration.
549     if (hwfc->pool) {
550         test_surface = av_buffer_pool_get(hwfc->pool);
551         if (!test_surface) {
552             av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
553                    "user-configured buffer pool.\n");
554             err = AVERROR(ENOMEM);
555             goto fail;
556         }
557     } else {
558         test_surface = av_buffer_pool_get(hwfc->internal->pool_internal);
559         if (!test_surface) {
560             av_log(hwfc, AV_LOG_ERROR, "Unable to allocate a surface from "
561                    "internal buffer pool.\n");
562             err = AVERROR(ENOMEM);
563             goto fail;
564         }
565     }
566     test_surface_id = (VASurfaceID)(uintptr_t)test_surface->data;
567
568     ctx->derive_works = 0;
569
570     err = vaapi_get_image_format(hwfc->device_ctx,
571                                  hwfc->sw_format, &expected_format);
572     if (err == 0) {
573         vas = vaDeriveImage(hwctx->display, test_surface_id, &test_image);
574         if (vas == VA_STATUS_SUCCESS) {
575             if (expected_format->fourcc == test_image.format.fourcc) {
576                 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping possible.\n");
577                 ctx->derive_works = 1;
578             } else {
579                 av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
580                        "derived image format %08x does not match "
581                        "expected format %08x.\n",
582                        expected_format->fourcc, test_image.format.fourcc);
583             }
584             vaDestroyImage(hwctx->display, test_image.image_id);
585         } else {
586             av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
587                    "deriving image does not work: "
588                    "%d (%s).\n", vas, vaErrorStr(vas));
589         }
590     } else {
591         av_log(hwfc, AV_LOG_DEBUG, "Direct mapping disabled: "
592                "image format is not supported.\n");
593     }
594
595     av_buffer_unref(&test_surface);
596     return 0;
597
598 fail:
599     av_buffer_unref(&test_surface);
600     av_freep(&avfc->surface_ids);
601     av_freep(&ctx->attributes);
602     return err;
603 }
604
605 static void vaapi_frames_uninit(AVHWFramesContext *hwfc)
606 {
607     AVVAAPIFramesContext *avfc = hwfc->hwctx;
608     VAAPIFramesContext    *ctx = hwfc->internal->priv;
609
610     av_freep(&avfc->surface_ids);
611     av_freep(&ctx->attributes);
612 }
613
614 static int vaapi_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
615 {
616     frame->buf[0] = av_buffer_pool_get(hwfc->pool);
617     if (!frame->buf[0])
618         return AVERROR(ENOMEM);
619
620     frame->data[3] = frame->buf[0]->data;
621     frame->format  = AV_PIX_FMT_VAAPI;
622     frame->width   = hwfc->width;
623     frame->height  = hwfc->height;
624
625     return 0;
626 }
627
628 static int vaapi_transfer_get_formats(AVHWFramesContext *hwfc,
629                                       enum AVHWFrameTransferDirection dir,
630                                       enum AVPixelFormat **formats)
631 {
632     VAAPIDeviceContext *ctx = hwfc->device_ctx->internal->priv;
633     enum AVPixelFormat *pix_fmts;
634     int i, k, sw_format_available;
635
636     sw_format_available = 0;
637     for (i = 0; i < ctx->nb_formats; i++) {
638         if (ctx->formats[i].pix_fmt == hwfc->sw_format)
639             sw_format_available = 1;
640     }
641
642     pix_fmts = av_malloc((ctx->nb_formats + 1) * sizeof(*pix_fmts));
643     if (!pix_fmts)
644         return AVERROR(ENOMEM);
645
646     if (sw_format_available) {
647         pix_fmts[0] = hwfc->sw_format;
648         k = 1;
649     } else {
650         k = 0;
651     }
652     for (i = 0; i < ctx->nb_formats; i++) {
653         if (ctx->formats[i].pix_fmt == hwfc->sw_format)
654             continue;
655         av_assert0(k < ctx->nb_formats);
656         pix_fmts[k++] = ctx->formats[i].pix_fmt;
657     }
658     pix_fmts[k] = AV_PIX_FMT_NONE;
659
660     *formats = pix_fmts;
661     return 0;
662 }
663
664 static void vaapi_unmap_frame(AVHWFramesContext *hwfc,
665                               HWMapDescriptor *hwmap)
666 {
667     AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
668     VAAPIMapping           *map = hwmap->priv;
669     VASurfaceID surface_id;
670     VAStatus vas;
671
672     surface_id = (VASurfaceID)(uintptr_t)hwmap->source->data[3];
673     av_log(hwfc, AV_LOG_DEBUG, "Unmap surface %#x.\n", surface_id);
674
675     vas = vaUnmapBuffer(hwctx->display, map->image.buf);
676     if (vas != VA_STATUS_SUCCESS) {
677         av_log(hwfc, AV_LOG_ERROR, "Failed to unmap image from surface "
678                "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
679     }
680
681     if ((map->flags & AV_HWFRAME_MAP_WRITE) &&
682         !(map->flags & AV_HWFRAME_MAP_DIRECT)) {
683         vas = vaPutImage(hwctx->display, surface_id, map->image.image_id,
684                          0, 0, hwfc->width, hwfc->height,
685                          0, 0, hwfc->width, hwfc->height);
686         if (vas != VA_STATUS_SUCCESS) {
687             av_log(hwfc, AV_LOG_ERROR, "Failed to write image to surface "
688                    "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
689         }
690     }
691
692     vas = vaDestroyImage(hwctx->display, map->image.image_id);
693     if (vas != VA_STATUS_SUCCESS) {
694         av_log(hwfc, AV_LOG_ERROR, "Failed to destroy image from surface "
695                "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
696     }
697
698     av_free(map);
699 }
700
701 static int vaapi_map_frame(AVHWFramesContext *hwfc,
702                            AVFrame *dst, const AVFrame *src, int flags)
703 {
704     AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
705     VAAPIFramesContext *ctx = hwfc->internal->priv;
706     VASurfaceID surface_id;
707     VAImageFormat *image_format;
708     VAAPIMapping *map;
709     VAStatus vas;
710     void *address = NULL;
711     int err, i;
712
713     surface_id = (VASurfaceID)(uintptr_t)src->data[3];
714     av_log(hwfc, AV_LOG_DEBUG, "Map surface %#x.\n", surface_id);
715
716     if (!ctx->derive_works && (flags & AV_HWFRAME_MAP_DIRECT)) {
717         // Requested direct mapping but it is not possible.
718         return AVERROR(EINVAL);
719     }
720     if (dst->format == AV_PIX_FMT_NONE)
721         dst->format = hwfc->sw_format;
722     if (dst->format != hwfc->sw_format && (flags & AV_HWFRAME_MAP_DIRECT)) {
723         // Requested direct mapping but the formats do not match.
724         return AVERROR(EINVAL);
725     }
726
727     err = vaapi_get_image_format(hwfc->device_ctx, dst->format, &image_format);
728     if (err < 0) {
729         // Requested format is not a valid output format.
730         return AVERROR(EINVAL);
731     }
732
733     map = av_malloc(sizeof(*map));
734     if (!map)
735         return AVERROR(ENOMEM);
736     map->flags = flags;
737     map->image.image_id = VA_INVALID_ID;
738
739     vas = vaSyncSurface(hwctx->display, surface_id);
740     if (vas != VA_STATUS_SUCCESS) {
741         av_log(hwfc, AV_LOG_ERROR, "Failed to sync surface "
742                "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
743         err = AVERROR(EIO);
744         goto fail;
745     }
746
747     // The memory which we map using derive need not be connected to the CPU
748     // in a way conducive to fast access.  On Gen7-Gen9 Intel graphics, the
749     // memory is mappable but not cached, so normal memcpy()-like access is
750     // very slow to read it (but writing is ok).  It is possible to read much
751     // faster with a copy routine which is aware of the limitation, but we
752     // assume for now that the user is not aware of that and would therefore
753     // prefer not to be given direct-mapped memory if they request read access.
754     if (ctx->derive_works && dst->format == hwfc->sw_format &&
755         ((flags & AV_HWFRAME_MAP_DIRECT) || !(flags & AV_HWFRAME_MAP_READ))) {
756         vas = vaDeriveImage(hwctx->display, surface_id, &map->image);
757         if (vas != VA_STATUS_SUCCESS) {
758             av_log(hwfc, AV_LOG_ERROR, "Failed to derive image from "
759                    "surface %#x: %d (%s).\n",
760                    surface_id, vas, vaErrorStr(vas));
761             err = AVERROR(EIO);
762             goto fail;
763         }
764         if (map->image.format.fourcc != image_format->fourcc) {
765             av_log(hwfc, AV_LOG_ERROR, "Derive image of surface %#x "
766                    "is in wrong format: expected %#08x, got %#08x.\n",
767                    surface_id, image_format->fourcc, map->image.format.fourcc);
768             err = AVERROR(EIO);
769             goto fail;
770         }
771         map->flags |= AV_HWFRAME_MAP_DIRECT;
772     } else {
773         vas = vaCreateImage(hwctx->display, image_format,
774                             hwfc->width, hwfc->height, &map->image);
775         if (vas != VA_STATUS_SUCCESS) {
776             av_log(hwfc, AV_LOG_ERROR, "Failed to create image for "
777                    "surface %#x: %d (%s).\n",
778                    surface_id, vas, vaErrorStr(vas));
779             err = AVERROR(EIO);
780             goto fail;
781         }
782         if (!(flags & AV_HWFRAME_MAP_OVERWRITE)) {
783             vas = vaGetImage(hwctx->display, surface_id, 0, 0,
784                              hwfc->width, hwfc->height, map->image.image_id);
785             if (vas != VA_STATUS_SUCCESS) {
786                 av_log(hwfc, AV_LOG_ERROR, "Failed to read image from "
787                        "surface %#x: %d (%s).\n",
788                        surface_id, vas, vaErrorStr(vas));
789                 err = AVERROR(EIO);
790                 goto fail;
791             }
792         }
793     }
794
795     vas = vaMapBuffer(hwctx->display, map->image.buf, &address);
796     if (vas != VA_STATUS_SUCCESS) {
797         av_log(hwfc, AV_LOG_ERROR, "Failed to map image from surface "
798                "%#x: %d (%s).\n", surface_id, vas, vaErrorStr(vas));
799         err = AVERROR(EIO);
800         goto fail;
801     }
802
803     err = ff_hwframe_map_create(src->hw_frames_ctx,
804                                 dst, src, &vaapi_unmap_frame, map);
805     if (err < 0)
806         goto fail;
807
808     dst->width  = src->width;
809     dst->height = src->height;
810
811     for (i = 0; i < map->image.num_planes; i++) {
812         dst->data[i] = (uint8_t*)address + map->image.offsets[i];
813         dst->linesize[i] = map->image.pitches[i];
814     }
815     if (
816 #ifdef VA_FOURCC_YV16
817         map->image.format.fourcc == VA_FOURCC_YV16 ||
818 #endif
819         map->image.format.fourcc == VA_FOURCC_YV12) {
820         // Chroma planes are YVU rather than YUV, so swap them.
821         FFSWAP(uint8_t*, dst->data[1], dst->data[2]);
822     }
823
824     return 0;
825
826 fail:
827     if (map) {
828         if (address)
829             vaUnmapBuffer(hwctx->display, map->image.buf);
830         if (map->image.image_id != VA_INVALID_ID)
831             vaDestroyImage(hwctx->display, map->image.image_id);
832         av_free(map);
833     }
834     return err;
835 }
836
837 static int vaapi_transfer_data_from(AVHWFramesContext *hwfc,
838                                     AVFrame *dst, const AVFrame *src)
839 {
840     AVFrame *map;
841     int err;
842
843     if (dst->width > hwfc->width || dst->height > hwfc->height)
844         return AVERROR(EINVAL);
845
846     map = av_frame_alloc();
847     if (!map)
848         return AVERROR(ENOMEM);
849     map->format = dst->format;
850
851     err = vaapi_map_frame(hwfc, map, src, AV_HWFRAME_MAP_READ);
852     if (err)
853         goto fail;
854
855     map->width  = dst->width;
856     map->height = dst->height;
857
858     err = av_frame_copy(dst, map);
859     if (err)
860         goto fail;
861
862     err = 0;
863 fail:
864     av_frame_free(&map);
865     return err;
866 }
867
868 static int vaapi_transfer_data_to(AVHWFramesContext *hwfc,
869                                   AVFrame *dst, const AVFrame *src)
870 {
871     AVFrame *map;
872     int err;
873
874     if (src->width > hwfc->width || src->height > hwfc->height)
875         return AVERROR(EINVAL);
876
877     map = av_frame_alloc();
878     if (!map)
879         return AVERROR(ENOMEM);
880     map->format = src->format;
881
882     err = vaapi_map_frame(hwfc, map, dst, AV_HWFRAME_MAP_WRITE | AV_HWFRAME_MAP_OVERWRITE);
883     if (err)
884         goto fail;
885
886     map->width  = src->width;
887     map->height = src->height;
888
889     err = av_frame_copy(map, src);
890     if (err)
891         goto fail;
892
893     err = 0;
894 fail:
895     av_frame_free(&map);
896     return err;
897 }
898
899 static int vaapi_map_to_memory(AVHWFramesContext *hwfc, AVFrame *dst,
900                                const AVFrame *src, int flags)
901 {
902     int err;
903
904     if (dst->format != AV_PIX_FMT_NONE) {
905         err = vaapi_get_image_format(hwfc->device_ctx, dst->format, NULL);
906         if (err < 0)
907             return AVERROR(ENOSYS);
908     }
909
910     err = vaapi_map_frame(hwfc, dst, src, flags);
911     if (err)
912         return err;
913
914     err = av_frame_copy_props(dst, src);
915     if (err)
916         return err;
917
918     return 0;
919 }
920
921 #if CONFIG_LIBDRM
922
923 #define DRM_MAP(va, layers, ...) { \
924         VA_FOURCC_ ## va, \
925         layers, \
926         { __VA_ARGS__ } \
927     }
928 static const struct {
929     uint32_t va_fourcc;
930     int   nb_layer_formats;
931     uint32_t layer_formats[AV_DRM_MAX_PLANES];
932 } vaapi_drm_format_map[] = {
933 #ifdef DRM_FORMAT_R8
934     DRM_MAP(NV12, 2, DRM_FORMAT_R8,  DRM_FORMAT_RG88),
935 #endif
936     DRM_MAP(NV12, 1, DRM_FORMAT_NV12),
937 #if defined(VA_FOURCC_P010) && defined(DRM_FORMAT_R16)
938     DRM_MAP(P010, 2, DRM_FORMAT_R16, DRM_FORMAT_RG1616),
939 #endif
940     DRM_MAP(BGRA, 1, DRM_FORMAT_ARGB8888),
941     DRM_MAP(BGRX, 1, DRM_FORMAT_XRGB8888),
942     DRM_MAP(RGBA, 1, DRM_FORMAT_ABGR8888),
943     DRM_MAP(RGBX, 1, DRM_FORMAT_XBGR8888),
944 #ifdef VA_FOURCC_ABGR
945     DRM_MAP(ABGR, 1, DRM_FORMAT_RGBA8888),
946     DRM_MAP(XBGR, 1, DRM_FORMAT_RGBX8888),
947 #endif
948     DRM_MAP(ARGB, 1, DRM_FORMAT_BGRA8888),
949     DRM_MAP(XRGB, 1, DRM_FORMAT_BGRX8888),
950 };
951 #undef DRM_MAP
952
953 static void vaapi_unmap_from_drm(AVHWFramesContext *dst_fc,
954                                  HWMapDescriptor *hwmap)
955 {
956     AVVAAPIDeviceContext *dst_dev = dst_fc->device_ctx->hwctx;
957
958     VASurfaceID surface_id = (VASurfaceID)(uintptr_t)hwmap->priv;
959
960     av_log(dst_fc, AV_LOG_DEBUG, "Destroy surface %#x.\n", surface_id);
961
962     vaDestroySurfaces(dst_dev->display, &surface_id, 1);
963 }
964
965 static int vaapi_map_from_drm(AVHWFramesContext *src_fc, AVFrame *dst,
966                               const AVFrame *src, int flags)
967 {
968     AVHWFramesContext      *dst_fc =
969         (AVHWFramesContext*)dst->hw_frames_ctx->data;
970     AVVAAPIDeviceContext  *dst_dev = dst_fc->device_ctx->hwctx;
971     const AVDRMFrameDescriptor *desc;
972     VASurfaceID surface_id;
973     VAStatus vas;
974     uint32_t va_fourcc, va_rt_format;
975     int err, i, j, k;
976
977     unsigned long buffer_handle;
978     VASurfaceAttribExternalBuffers buffer_desc;
979     VASurfaceAttrib attrs[2] = {
980         {
981             .type  = VASurfaceAttribMemoryType,
982             .flags = VA_SURFACE_ATTRIB_SETTABLE,
983             .value.type    = VAGenericValueTypeInteger,
984             .value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME,
985         },
986         {
987             .type  = VASurfaceAttribExternalBufferDescriptor,
988             .flags = VA_SURFACE_ATTRIB_SETTABLE,
989             .value.type    = VAGenericValueTypePointer,
990             .value.value.p = &buffer_desc,
991         }
992     };
993
994     desc = (AVDRMFrameDescriptor*)src->data[0];
995
996     if (desc->nb_objects != 1) {
997         av_log(dst_fc, AV_LOG_ERROR, "VAAPI can only map frames "
998                "made from a single DRM object.\n");
999         return AVERROR(EINVAL);
1000     }
1001
1002     va_fourcc = 0;
1003     for (i = 0; i < FF_ARRAY_ELEMS(vaapi_drm_format_map); i++) {
1004         if (desc->nb_layers != vaapi_drm_format_map[i].nb_layer_formats)
1005             continue;
1006         for (j = 0; j < desc->nb_layers; j++) {
1007             if (desc->layers[j].format !=
1008                 vaapi_drm_format_map[i].layer_formats[j])
1009                 break;
1010         }
1011         if (j != desc->nb_layers)
1012             continue;
1013         va_fourcc = vaapi_drm_format_map[i].va_fourcc;
1014         break;
1015     }
1016     if (!va_fourcc) {
1017         av_log(dst_fc, AV_LOG_ERROR, "DRM format not supported "
1018                "by VAAPI.\n");
1019         return AVERROR(EINVAL);
1020     }
1021
1022     av_log(dst_fc, AV_LOG_DEBUG, "Map DRM object %d to VAAPI as "
1023            "%08x.\n", desc->objects[0].fd, va_fourcc);
1024
1025     for (i = 0; i < FF_ARRAY_ELEMS(vaapi_format_map); i++) {
1026         if (vaapi_format_map[i].fourcc == va_fourcc)
1027             va_rt_format = vaapi_format_map[i].rt_format;
1028     }
1029
1030     buffer_handle = desc->objects[0].fd;
1031     buffer_desc.pixel_format = va_fourcc;
1032     buffer_desc.width        = src_fc->width;
1033     buffer_desc.height       = src_fc->height;
1034     buffer_desc.data_size    = desc->objects[0].size;
1035     buffer_desc.buffers      = &buffer_handle;
1036     buffer_desc.num_buffers  = 1;
1037     buffer_desc.flags        = 0;
1038
1039     k = 0;
1040     for (i = 0; i < desc->nb_layers; i++) {
1041         for (j = 0; j < desc->layers[i].nb_planes; j++) {
1042             buffer_desc.pitches[k] = desc->layers[i].planes[j].pitch;
1043             buffer_desc.offsets[k] = desc->layers[i].planes[j].offset;
1044             ++k;
1045         }
1046     }
1047     buffer_desc.num_planes = k;
1048
1049     vas = vaCreateSurfaces(dst_dev->display, va_rt_format,
1050                            src->width, src->height,
1051                            &surface_id, 1,
1052                            attrs, FF_ARRAY_ELEMS(attrs));
1053     if (vas != VA_STATUS_SUCCESS) {
1054         av_log(dst_fc, AV_LOG_ERROR, "Failed to create surface from DRM "
1055                "object: %d (%s).\n", vas, vaErrorStr(vas));
1056         return AVERROR(EIO);
1057     }
1058     av_log(dst_fc, AV_LOG_DEBUG, "Create surface %#x.\n", surface_id);
1059
1060     err = ff_hwframe_map_create(dst->hw_frames_ctx, dst, src,
1061                                 &vaapi_unmap_from_drm,
1062                                 (void*)(uintptr_t)surface_id);
1063     if (err < 0)
1064         return err;
1065
1066     dst->width   = src->width;
1067     dst->height  = src->height;
1068     dst->data[3] = (uint8_t*)(uintptr_t)surface_id;
1069
1070     av_log(dst_fc, AV_LOG_DEBUG, "Mapped DRM object %d to "
1071            "surface %#x.\n", desc->objects[0].fd, surface_id);
1072
1073     return 0;
1074 }
1075
1076 static void vaapi_unmap_to_drm(AVHWFramesContext *dst_fc,
1077                                HWMapDescriptor *hwmap)
1078 {
1079     AVDRMFrameDescriptor *drm_desc = hwmap->priv;
1080     int i;
1081
1082     for (i = 0; i < drm_desc->nb_objects; i++)
1083         close(drm_desc->objects[i].fd);
1084
1085     av_freep(&drm_desc);
1086 }
1087
1088 static int vaapi_map_to_drm(AVHWFramesContext *hwfc, AVFrame *dst,
1089                             const AVFrame *src, int flags)
1090 {
1091 #if VA_CHECK_VERSION(1, 1, 0)
1092     AVVAAPIDeviceContext *hwctx = hwfc->device_ctx->hwctx;
1093     VASurfaceID surface_id;
1094     VAStatus vas;
1095     VADRMPRIMESurfaceDescriptor va_desc;
1096     AVDRMFrameDescriptor *drm_desc = NULL;
1097     int err, i, j;
1098
1099     surface_id = (VASurfaceID)(uintptr_t)src->data[3];
1100
1101     vas = vaExportSurfaceHandle(hwctx->display, surface_id,
1102                                 VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2,
1103                                 VA_EXPORT_SURFACE_READ_ONLY |
1104                                 VA_EXPORT_SURFACE_SEPARATE_LAYERS,
1105                                 &va_desc);
1106     if (vas != VA_STATUS_SUCCESS) {
1107         if (vas == VA_STATUS_ERROR_UNIMPLEMENTED)
1108             return AVERROR(ENOSYS);
1109         av_log(hwfc, AV_LOG_ERROR, "Failed to export surface %#x: "
1110                "%d (%s).\n", surface_id, vas, vaErrorStr(vas));
1111         return AVERROR(EIO);
1112     }
1113
1114     drm_desc = av_mallocz(sizeof(*drm_desc));
1115     if (!drm_desc) {
1116         err = AVERROR(ENOMEM);
1117         goto fail;
1118     }
1119
1120     // By some bizarre coincidence, these structures are very similar...
1121     drm_desc->nb_objects = va_desc.num_objects;
1122     for (i = 0; i < va_desc.num_objects; i++) {
1123         drm_desc->objects[i].fd   = va_desc.objects[i].fd;
1124         drm_desc->objects[i].size = va_desc.objects[i].size;
1125         drm_desc->objects[i].format_modifier =
1126             va_desc.objects[i].drm_format_modifier;
1127     }
1128     drm_desc->nb_layers = va_desc.num_layers;
1129     for (i = 0; i < va_desc.num_layers; i++) {
1130         drm_desc->layers[i].format    = va_desc.layers[i].drm_format;
1131         drm_desc->layers[i].nb_planes = va_desc.layers[i].num_planes;
1132         for (j = 0; j < va_desc.layers[i].num_planes; j++) {
1133             drm_desc->layers[i].planes[j].object_index =
1134                 va_desc.layers[i].object_index[j];
1135             drm_desc->layers[i].planes[j].offset =
1136                 va_desc.layers[i].offset[j];
1137             drm_desc->layers[i].planes[j].pitch =
1138                 va_desc.layers[i].pitch[j];
1139         }
1140     }
1141
1142     err = ff_hwframe_map_create(src->hw_frames_ctx, dst, src,
1143                                 &vaapi_unmap_to_drm, drm_desc);
1144     if (err < 0)
1145         goto fail;
1146
1147     dst->width   = src->width;
1148     dst->height  = src->height;
1149     dst->data[0] = (uint8_t*)drm_desc;
1150
1151     return 0;
1152
1153 fail:
1154     for (i = 0; i < va_desc.num_objects; i++)
1155         close(va_desc.objects[i].fd);
1156     av_freep(&drm_desc);
1157     return err;
1158 #else
1159     // Older versions without vaExportSurfaceHandle() are not supported -
1160     // in theory this is possible with a combination of vaDeriveImage()
1161     // and vaAcquireBufferHandle(), but it doesn't carry enough metadata
1162     // to actually use the result in a generic way.
1163     return AVERROR(ENOSYS);
1164 #endif
1165 }
1166 #endif
1167
1168 static int vaapi_map_to(AVHWFramesContext *hwfc, AVFrame *dst,
1169                         const AVFrame *src, int flags)
1170 {
1171     switch (src->format) {
1172 #if CONFIG_LIBDRM
1173     case AV_PIX_FMT_DRM_PRIME:
1174         return vaapi_map_from_drm(hwfc, dst, src, flags);
1175 #endif
1176     default:
1177         return AVERROR(ENOSYS);
1178     }
1179 }
1180
1181 static int vaapi_map_from(AVHWFramesContext *hwfc, AVFrame *dst,
1182                           const AVFrame *src, int flags)
1183 {
1184     switch (dst->format) {
1185 #if CONFIG_LIBDRM
1186     case AV_PIX_FMT_DRM_PRIME:
1187         return vaapi_map_to_drm(hwfc, dst, src, flags);
1188 #endif
1189     default:
1190         return vaapi_map_to_memory(hwfc, dst, src, flags);
1191     }
1192 }
1193
1194 static void vaapi_device_free(AVHWDeviceContext *ctx)
1195 {
1196     AVVAAPIDeviceContext *hwctx = ctx->hwctx;
1197     VAAPIDevicePriv      *priv  = ctx->user_opaque;
1198
1199     if (hwctx->display)
1200         vaTerminate(hwctx->display);
1201
1202 #if HAVE_VAAPI_X11
1203     if (priv->x11_display)
1204         XCloseDisplay(priv->x11_display);
1205 #endif
1206
1207     if (priv->drm_fd >= 0)
1208         close(priv->drm_fd);
1209
1210     av_freep(&priv);
1211 }
1212
1213 #if CONFIG_VAAPI_1
1214 static void vaapi_device_log_error(void *context, const char *message)
1215 {
1216     AVHWDeviceContext *ctx = context;
1217
1218     av_log(ctx, AV_LOG_ERROR, "libva: %s", message);
1219 }
1220
1221 static void vaapi_device_log_info(void *context, const char *message)
1222 {
1223     AVHWDeviceContext *ctx = context;
1224
1225     av_log(ctx, AV_LOG_VERBOSE, "libva: %s", message);
1226 }
1227 #endif
1228
1229 static int vaapi_device_connect(AVHWDeviceContext *ctx,
1230                                 VADisplay display)
1231 {
1232     AVVAAPIDeviceContext *hwctx = ctx->hwctx;
1233     int major, minor;
1234     VAStatus vas;
1235
1236 #if CONFIG_VAAPI_1
1237     vaSetErrorCallback(display, &vaapi_device_log_error, ctx);
1238     vaSetInfoCallback (display, &vaapi_device_log_info,  ctx);
1239 #endif
1240
1241     hwctx->display = display;
1242
1243     vas = vaInitialize(display, &major, &minor);
1244     if (vas != VA_STATUS_SUCCESS) {
1245         av_log(ctx, AV_LOG_ERROR, "Failed to initialise VAAPI "
1246                "connection: %d (%s).\n", vas, vaErrorStr(vas));
1247         return AVERROR(EIO);
1248     }
1249     av_log(ctx, AV_LOG_VERBOSE, "Initialised VAAPI connection: "
1250            "version %d.%d\n", major, minor);
1251
1252     return 0;
1253 }
1254
1255 static int vaapi_device_create(AVHWDeviceContext *ctx, const char *device,
1256                                AVDictionary *opts, int flags)
1257 {
1258     VAAPIDevicePriv *priv;
1259     VADisplay display = NULL;
1260
1261     priv = av_mallocz(sizeof(*priv));
1262     if (!priv)
1263         return AVERROR(ENOMEM);
1264
1265     priv->drm_fd = -1;
1266
1267     ctx->user_opaque = priv;
1268     ctx->free        = vaapi_device_free;
1269
1270 #if HAVE_VAAPI_X11
1271     if (!display && !(device && device[0] == '/')) {
1272         // Try to open the device as an X11 display.
1273         priv->x11_display = XOpenDisplay(device);
1274         if (!priv->x11_display) {
1275             av_log(ctx, AV_LOG_VERBOSE, "Cannot open X11 display "
1276                    "%s.\n", XDisplayName(device));
1277         } else {
1278             display = vaGetDisplay(priv->x11_display);
1279             if (!display) {
1280                 av_log(ctx, AV_LOG_ERROR, "Cannot open a VA display "
1281                        "from X11 display %s.\n", XDisplayName(device));
1282                 return AVERROR_UNKNOWN;
1283             }
1284
1285             av_log(ctx, AV_LOG_VERBOSE, "Opened VA display via "
1286                    "X11 display %s.\n", XDisplayName(device));
1287         }
1288     }
1289 #endif
1290
1291 #if HAVE_VAAPI_DRM
1292     if (!display) {
1293         // Try to open the device as a DRM path.
1294         // Default to using the first render node if the user did not
1295         // supply a path.
1296         const char *path = device ? device : "/dev/dri/renderD128";
1297         priv->drm_fd = open(path, O_RDWR);
1298         if (priv->drm_fd < 0) {
1299             av_log(ctx, AV_LOG_VERBOSE, "Cannot open DRM device %s.\n",
1300                    path);
1301         } else {
1302             display = vaGetDisplayDRM(priv->drm_fd);
1303             if (!display) {
1304                 av_log(ctx, AV_LOG_ERROR, "Cannot open a VA display "
1305                        "from DRM device %s.\n", path);
1306                 return AVERROR_UNKNOWN;
1307             }
1308
1309             av_log(ctx, AV_LOG_VERBOSE, "Opened VA display via "
1310                    "DRM device %s.\n", path);
1311         }
1312     }
1313 #endif
1314
1315     if (!display) {
1316         av_log(ctx, AV_LOG_ERROR, "No VA display found for "
1317                "device: %s.\n", device ? device : "");
1318         return AVERROR(EINVAL);
1319     }
1320
1321     return vaapi_device_connect(ctx, display);
1322 }
1323
1324 static int vaapi_device_derive(AVHWDeviceContext *ctx,
1325                                AVHWDeviceContext *src_ctx, int flags)
1326 {
1327 #if CONFIG_LIBDRM
1328     if (src_ctx->type == AV_HWDEVICE_TYPE_DRM) {
1329         AVDRMDeviceContext *src_hwctx = src_ctx->hwctx;
1330         VADisplay *display;
1331         VAAPIDevicePriv *priv;
1332
1333         if (src_hwctx->fd < 0) {
1334             av_log(ctx, AV_LOG_ERROR, "DRM instance requires an associated "
1335                    "device to derive a VA display from.\n");
1336             return AVERROR(EINVAL);
1337         }
1338
1339         priv = av_mallocz(sizeof(*priv));
1340         if (!priv)
1341             return AVERROR(ENOMEM);
1342
1343         // Inherits the fd from the source context, which will close it.
1344         priv->drm_fd = -1;
1345
1346         ctx->user_opaque = priv;
1347         ctx->free        = &vaapi_device_free;
1348
1349         display = vaGetDisplayDRM(src_hwctx->fd);
1350         if (!display) {
1351             av_log(ctx, AV_LOG_ERROR, "Failed to open a VA display from "
1352                    "DRM device.\n");
1353             return AVERROR(EIO);
1354         }
1355
1356         return vaapi_device_connect(ctx, display);
1357     }
1358 #endif
1359     return AVERROR(ENOSYS);
1360 }
1361
1362 const HWContextType ff_hwcontext_type_vaapi = {
1363     .type                   = AV_HWDEVICE_TYPE_VAAPI,
1364     .name                   = "VAAPI",
1365
1366     .device_hwctx_size      = sizeof(AVVAAPIDeviceContext),
1367     .device_priv_size       = sizeof(VAAPIDeviceContext),
1368     .device_hwconfig_size   = sizeof(AVVAAPIHWConfig),
1369     .frames_hwctx_size      = sizeof(AVVAAPIFramesContext),
1370     .frames_priv_size       = sizeof(VAAPIFramesContext),
1371
1372     .device_create          = &vaapi_device_create,
1373     .device_derive          = &vaapi_device_derive,
1374     .device_init            = &vaapi_device_init,
1375     .device_uninit          = &vaapi_device_uninit,
1376     .frames_get_constraints = &vaapi_frames_get_constraints,
1377     .frames_init            = &vaapi_frames_init,
1378     .frames_uninit          = &vaapi_frames_uninit,
1379     .frames_get_buffer      = &vaapi_get_buffer,
1380     .transfer_get_formats   = &vaapi_transfer_get_formats,
1381     .transfer_data_to       = &vaapi_transfer_data_to,
1382     .transfer_data_from     = &vaapi_transfer_data_from,
1383     .map_to                 = &vaapi_map_to,
1384     .map_from               = &vaapi_map_from,
1385
1386     .pix_fmts = (const enum AVPixelFormat[]) {
1387         AV_PIX_FMT_VAAPI,
1388         AV_PIX_FMT_NONE
1389     },
1390 };