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