---
 Makefile       |   1 +
 avconv.h       |   7 +
 avconv_opt.c   |  38 ++++
 avconv_vaapi.c | 607 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 configure      |   5 +
 5 files changed, 658 insertions(+)
 create mode 100644 avconv_vaapi.c

diff --git a/Makefile b/Makefile
index 7b1e550..a59dea7 100644
--- a/Makefile
+++ b/Makefile
@@ -86,6 +86,7 @@ OBJS-avconv-$(HAVE_VDPAU_X11) += avconv_vdpau.o
 OBJS-avconv-$(HAVE_DXVA2_LIB) += avconv_dxva2.o
 OBJS-avconv-$(CONFIG_VDA)     += avconv_vda.o
 OBJS-avconv-$(CONFIG_LIBMFX)  += avconv_qsv.o
+OBJS-avconv-$(CONFIG_VAAPI_RECENT) += avconv_vaapi.o

 TESTTOOLS   = audiogen videogen rotozoom tiny_psnr base64
 HOSTPROGS  := $(TESTTOOLS:%=tests/%) doc/print_options
diff --git a/avconv.h b/avconv.h
index f0a948f..4a6f151 100644
--- a/avconv.h
+++ b/avconv.h
@@ -55,6 +55,7 @@ enum HWAccelID {
     HWACCEL_DXVA2,
     HWACCEL_VDA,
     HWACCEL_QSV,
+    HWACCEL_VAAPI,
 };

 typedef struct HWAccel {
@@ -115,6 +116,8 @@ typedef struct OptionsContext {
     int        nb_hwaccels;
     SpecifierOpt *hwaccel_devices;
     int        nb_hwaccel_devices;
+    SpecifierOpt *hwaccel_output_formats;
+    int        nb_hwaccel_output_formats;
     SpecifierOpt *autorotate;
     int        nb_autorotate;

@@ -262,6 +265,7 @@ typedef struct InputStream {
     /* hwaccel options */
     enum HWAccelID hwaccel_id;
     char  *hwaccel_device;
+    enum AVPixelFormat hwaccel_output_format;

     /* hwaccel context */
     enum HWAccelID active_hwaccel_id;
@@ -425,6 +429,7 @@ extern const AVIOInterruptCB int_cb;
 extern const OptionDef options[];

 extern const HWAccel hwaccels[];
+extern int hwaccel_lax_profile_check;

 void reset_options(OptionsContext *o);
 void show_usage(void);
@@ -448,5 +453,7 @@ int dxva2_init(AVCodecContext *s);
 int vda_init(AVCodecContext *s);
 int qsv_init(AVCodecContext *s);
 int qsv_transcode_init(OutputStream *ost);
+int vaapi_decode_init(AVCodecContext *avctx);
+int vaapi_device_init(const char *device);

 #endif /* AVCONV_H */
diff --git a/avconv_opt.c b/avconv_opt.c
index 8fe53e6..a617a3d 100644
--- a/avconv_opt.c
+++ b/avconv_opt.c
@@ -68,8 +68,12 @@ const HWAccel hwaccels[] = {
 #if CONFIG_LIBMFX
     { "qsv",   qsv_init,   HWACCEL_QSV,   AV_PIX_FMT_QSV },
 #endif
+#if CONFIG_VAAPI_RECENT
+    { "vaapi", vaapi_decode_init, HWACCEL_VAAPI, AV_PIX_FMT_VAAPI_VLD },
+#endif
     { 0 },
 };
+int hwaccel_lax_profile_check = 0;

 char *vstats_filename;

@@ -321,6 +325,15 @@ static int opt_attach(void *optctx, const char *opt, const 
char *arg)
     return 0;
 }

+#if CONFIG_VAAPI_RECENT
+static int opt_vaapi_device(void *optctx, const char *opt, const char *arg)
+{
+    if (vaapi_device_init(arg))
+        exit_program(1);
+    return 0;
+}
+#endif
+
 /**
  * Parse a metadata specifier passed as 'arg' parameter.
  * @param arg  metadata string to parse
@@ -488,6 +501,7 @@ static void add_input_streams(OptionsContext *o, 
AVFormatContext *ic)
         AVCodecContext *dec = st->codec;
         InputStream *ist = av_mallocz(sizeof(*ist));
         char *framerate = NULL, *hwaccel = NULL, *hwaccel_device = NULL;
+        char *hwaccel_output_format = NULL;
         char *codec_tag = NULL;
         char *next;

@@ -581,6 +595,19 @@ static void add_input_streams(OptionsContext *o, 
AVFormatContext *ic)
                 if (!ist->hwaccel_device)
                     exit_program(1);
             }
+
+            MATCH_PER_STREAM_OPT(hwaccel_output_formats, str,
+                                 hwaccel_output_format, ic, st);
+            if (hwaccel_output_format) {
+                ist->hwaccel_output_format = 
av_get_pix_fmt(hwaccel_output_format);
+                if (ist->hwaccel_output_format == AV_PIX_FMT_NONE) {
+                    av_log(NULL, AV_LOG_FATAL, "Unrecognised hwaccel output "
+                           "format: %s", hwaccel_output_format);
+                }
+            } else {
+                ist->hwaccel_output_format = AV_PIX_FMT_NONE;
+            }
+
             ist->hwaccel_pix_fmt = AV_PIX_FMT_NONE;

             break;
@@ -2470,11 +2497,17 @@ const OptionDef options[] = {
     { "hwaccel_device",   OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
                           OPT_SPEC | OPT_INPUT,                                
  { .off = OFFSET(hwaccel_devices) },
         "select a device for HW acceleration", "devicename" },
+    { "hwaccel_output_format", OPT_VIDEO | OPT_STRING | HAS_ARG | OPT_EXPERT |
+                          OPT_SPEC | OPT_INPUT,                                
  { .off = OFFSET(hwaccel_output_formats) },
+        "select output format used with HW accelerated decoding", "format" },
+
     { "hwaccels",         OPT_EXIT,                                            
  { .func_arg = show_hwaccels },
         "show available HW acceleration methods" },
     { "autorotate",       HAS_ARG | OPT_BOOL | OPT_SPEC |
                           OPT_EXPERT | OPT_INPUT,                              
  { .off = OFFSET(autorotate) },
         "automatically insert correct rotate filters" },
+    { "hwaccel_lax_profile_check", OPT_BOOL | OPT_EXPERT,                      
  { &hwaccel_lax_profile_check},
+        "attempt to decode anyway if HW accelerated decoder's supported 
profiles do not exactly match the stream" },

     /* audio options */
     { "aframes",        OPT_AUDIO | HAS_ARG  | OPT_PERFILE | OPT_OUTPUT,       
    { .func_arg = opt_audio_frames },
@@ -2530,5 +2563,10 @@ const OptionDef options[] = {
     { "dcodec", HAS_ARG | OPT_DATA | OPT_PERFILE | OPT_EXPERT | OPT_INPUT | 
OPT_OUTPUT, { .func_arg = opt_data_codec },
         "force data codec ('copy' to copy stream)", "codec" },

+#if CONFIG_VAAPI_RECENT
+    { "vaapi_device", HAS_ARG | OPT_EXPERT, { .func_arg = opt_vaapi_device },
+        "set VAAPI hardware device (DRM path or X11 display name)", "device" },
+#endif
+
     { NULL, },
 };
diff --git a/avconv_vaapi.c b/avconv_vaapi.c
new file mode 100644
index 0000000..ca49396
--- /dev/null
+++ b/avconv_vaapi.c
@@ -0,0 +1,607 @@
+/*
+ * This file is part of Libav.
+ *
+ * Libav is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * Libav is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with Libav; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "avconv.h"
+
+#include "libavutil/avassert.h"
+#include "libavutil/avconfig.h"
+#include "libavutil/buffer.h"
+#include "libavutil/frame.h"
+#include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
+#include "libavutil/pixfmt.h"
+
+#include "libavutil/hwcontext.h"
+#include "libavutil/hwcontext_vaapi.h"
+
+#include "libavcodec/vaapi.h"
+
+#if HAVE_VAAPI_X11
+#include <va/va_x11.h>
+#endif
+#if HAVE_VAAPI_DRM
+#include <va/va_drm.h>
+#endif
+
+// This is the one global vaapi hardware context, which we want to pass
+// to all users.
+static AVBufferRef *vaapi_device_ref;
+static AVHWDeviceContext *vaapi_device_context;
+
+
+static AVClass vaapi_class = {
+    .class_name = "vaapi",
+    .item_name  = av_default_item_name,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+#define DEFAULT_SURFACES 20
+
+typedef struct VAAPIDecoderContext {
+    const AVClass *class;
+
+    AVBufferRef       *device_ref;
+    AVHWDeviceContext *device;
+    AVBufferRef       *frames_ref;
+    AVHWFramesContext *frames;
+
+    VAProfile    va_profile;
+    VAEntrypoint va_entrypoint;
+    VAConfigID   va_config;
+    VAContextID  va_context;
+
+    enum AVPixelFormat decode_format;
+    int decode_width;
+    int decode_height;
+    int decode_surfaces;
+
+    enum AVPixelFormat output_format;
+    // The output need not have the same format, width and height as the
+    // decoded frames - the copy for non-direct-mapped access is actually
+    // a whole vpp instance which can do arbitrary scaling and format
+    //conversion
+
+    struct vaapi_context decoder_vaapi_context;
+} VAAPIDecoderContext;
+
+
+static int vaapi_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
+{
+    InputStream *ist = avctx->opaque;
+    VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
+    int err;
+
+    err = av_hwframe_get_buffer(ctx->frames_ref, frame, 0);
+    if (err < 0) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to allocate decoder surface.\n");
+    } else {
+        av_log(ctx, AV_LOG_DEBUG, "Decoder given surface %#x.\n",
+               (unsigned int)(uintptr_t)frame->data[3]);
+    }
+    return err;
+}
+
+static int vaapi_retrieve_data(AVCodecContext *avctx, AVFrame *input)
+{
+    InputStream *ist = avctx->opaque;
+    VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
+    AVFrame *output = 0;
+    int err;
+
+    av_log(ctx, AV_LOG_DEBUG, "Retrieve data from surface %#x.\n",
+           (unsigned int)(uintptr_t)input->data[3]);
+
+    output = av_frame_alloc();
+    if (!output)
+        return AVERROR(ENOMEM);
+
+    output->format = ctx->output_format;
+
+    err = av_hwframe_transfer_data(output, input, 0);
+    if (err < 0)
+        goto fail;
+
+    err = av_frame_copy_props(output, input);
+    if (err < 0) {
+        av_frame_unref(output);
+        goto fail;
+    }
+
+    av_frame_unref(input);
+    av_frame_move_ref(input, output);
+    av_frame_free(&output);
+
+    return 0;
+
+  fail:
+    if (output)
+        av_frame_free(&output);
+    return err;
+}
+
+
+static const struct {
+    enum AVCodecID codec_id;
+    int codec_profile;
+    VAProfile va_profile;
+} vaapi_profile_map[] = {
+#define MAP(c, p, v) { AV_CODEC_ID_ ## c, FF_PROFILE_ ## p, VAProfile ## v }
+    MAP(MPEG2VIDEO,  MPEG2_SIMPLE,    MPEG2Simple ),
+    MAP(MPEG2VIDEO,  MPEG2_MAIN,      MPEG2Main   ),
+    MAP(H263,        UNKNOWN,         H263Baseline),
+    MAP(MPEG4,       MPEG4_SIMPLE,    MPEG4Simple ),
+    MAP(MPEG4,       MPEG4_ADVANCED_SIMPLE,
+                               MPEG4AdvancedSimple),
+    MAP(MPEG4,       MPEG4_MAIN,      MPEG4Main   ),
+    MAP(H264,        H264_CONSTRAINED_BASELINE,
+                           H264ConstrainedBaseline),
+    MAP(H264,        H264_BASELINE,   H264Baseline),
+    MAP(H264,        H264_MAIN,       H264Main    ),
+    MAP(H264,        H264_HIGH,       H264High    ),
+#if 0 // CONFIG_HEVC_VAAPI_HWACCEL
+    MAP(HEVC,        HEVC_MAIN,       HEVCMain    ),
+#endif
+    MAP(WMV3,        VC1_SIMPLE,      VC1Simple   ),
+    MAP(WMV3,        VC1_MAIN,        VC1Main     ),
+    MAP(WMV3,        VC1_COMPLEX,     VC1Advanced ),
+    MAP(WMV3,        VC1_ADVANCED,    VC1Advanced ),
+    MAP(VC1,         VC1_SIMPLE,      VC1Simple   ),
+    MAP(VC1,         VC1_MAIN,        VC1Main     ),
+    MAP(VC1,         VC1_COMPLEX,     VC1Advanced ),
+    MAP(VC1,         VC1_ADVANCED,    VC1Advanced ),
+#if 0 // CONFIG_VP9_VAAPI_HWACCEL
+    MAP(VP9,         VP9_0,           VP9Profile0 ),
+#endif
+#undef MAP
+};
+
+static int vaapi_build_decoder_config(VAAPIDecoderContext *ctx,
+                                      AVCodecContext *avctx,
+                                      int fallback_allowed)
+{
+    AVVAAPIDeviceContext *hwctx = ctx->device->hwctx;
+    VAStatus vas;
+    int err, i, j;
+    int loglevel = fallback_allowed ? AV_LOG_VERBOSE : AV_LOG_ERROR;
+
+    // Pick codec profile to use.
+    {
+        const AVCodecDescriptor *codec_desc;
+        VAProfile profile, *profile_list;
+        int profile_count, exact_match, alt_profile;
+
+        codec_desc = avcodec_descriptor_get(avctx->codec_id);
+        if (!codec_desc) {
+            err = AVERROR(EINVAL);
+            goto fail;
+        }
+
+        profile_count = vaMaxNumProfiles(hwctx->display);
+        profile_list = av_malloc(profile_count * sizeof(VAProfile));
+        if (!profile_list)
+            return AVERROR(ENOMEM);
+
+        vas = vaQueryConfigProfiles(hwctx->display,
+                                    profile_list, &profile_count);
+        if (vas != VA_STATUS_SUCCESS) {
+            av_log(ctx, loglevel, "Failed to query profiles: %d (%s).\n",
+                   vas, vaErrorStr(vas));
+            av_free(profile_list);
+            return AVERROR(EINVAL);
+        }
+
+        profile = VAProfileNone;
+        exact_match = 0;
+
+        for (i = 0; i < FF_ARRAY_ELEMS(vaapi_profile_map); i++) {
+            int profile_match = 0;
+            if (avctx->codec_id != vaapi_profile_map[i].codec_id)
+                continue;
+            if (avctx->profile == vaapi_profile_map[i].codec_profile)
+                profile_match = 1;
+            profile = vaapi_profile_map[i].va_profile;
+            for (j = 0; j < profile_count; j++) {
+                if (profile == profile_list[j]) {
+                    exact_match = profile_match;
+                    break;
+                }
+            }
+            if (j < profile_count) {
+                if (exact_match)
+                    break;
+                alt_profile = vaapi_profile_map[i].codec_profile;
+            }
+        }
+        av_free(profile_list);
+
+        if (profile == VAProfileNone) {
+            av_log(ctx, loglevel, "No VAAPI support for codec %s.\n",
+                   codec_desc->name);
+            return AVERROR(EINVAL);
+        }
+        if (!exact_match) {
+            if (fallback_allowed || !hwaccel_lax_profile_check) {
+                av_log(ctx, loglevel, "No VAAPI support for codec %s "
+                       "profile %d.\n", codec_desc->name, avctx->profile);
+                if (!fallback_allowed) {
+                    av_log(ctx, AV_LOG_WARNING, "If you want attempt decoding "
+                           "anyway with a possibly-incompatible profile, add "
+                           "the option -hwaccel_lax_profile_check.\n");
+                }
+                return AVERROR(EINVAL);
+            } else {
+                av_log(ctx, AV_LOG_WARNING, "No VAAPI support for codec %s "
+                       "profile %d: trying instead with profile %d.\n",
+                       codec_desc->name, avctx->profile, alt_profile);
+                av_log(ctx, AV_LOG_WARNING, "This may fail or give "
+                       "incorrect results, depending on your hardware.\n");
+            }
+        }
+
+        ctx->va_profile = profile;
+        ctx->va_entrypoint = VAEntrypointVLD;
+    }
+
+    vas = vaCreateConfig(hwctx->display, ctx->va_profile,
+                         ctx->va_entrypoint, 0, 0, &ctx->va_config);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to create decode pipeline "
+               "configuration: %d (%s).\n", vas, vaErrorStr(vas));
+        return AVERROR(EINVAL);
+    }
+
+    // Decide on the decode format.  Currently fixed - we only support
+    // RT format 4:2:0, which becomes NV12.
+    ctx->decode_format = AV_PIX_FMT_NV12;
+
+    // Decide on the output format.
+    if (ctx->output_format == AV_PIX_FMT_NONE) {
+        // Default to NV12 in the absence of anything user-specified
+        // (via -hwaccel_output_format).
+        ctx->output_format = AV_PIX_FMT_NV12;
+    }
+
+    // Decide how many reference frames we need.
+    {
+        // We should be able to do this in a more sensible way by looking
+        // at how many reference frames the input stream requires.
+        ctx->decode_surfaces = DEFAULT_SURFACES;
+
+        // For frame-threaded decoding, one additional surfaces is needed
+        // for each thread.
+        if (avctx->active_thread_type & FF_THREAD_FRAME)
+            ctx->decode_surfaces += avctx->thread_count;
+    }
+
+    // Test whether the width and height are within allowable limits.
+    {
+        VASurfaceAttrib *attr_list;
+        int attr_count = 0;
+        int min_width, max_width, min_height, max_height;
+
+        vas = vaQuerySurfaceAttributes(hwctx->display, ctx->va_config,
+                                       0, &attr_count);
+        if (vas != VA_STATUS_SUCCESS) {
+            av_log(ctx, AV_LOG_ERROR, "Failed to get surface attribute count: "
+                   "%d (%s).\n", vas, vaErrorStr(vas));
+            err = AVERROR(EINVAL);
+            goto fail;
+        }
+
+        attr_list = av_malloc(attr_count * sizeof(VASurfaceAttrib));
+        if (!attr_list) {
+            err = AVERROR(ENOMEM);
+            goto fail;
+        }
+        min_width = min_height = 0; // Min sizes need not be returned.
+        max_width = max_height = INT_MIN; // Max sizes should be.
+
+        vas = vaQuerySurfaceAttributes(hwctx->display, ctx->va_config,
+                                       attr_list, &attr_count);
+        if (vas != VA_STATUS_SUCCESS)
+            attr_count = 0;
+        for (i = 0; i < attr_count; i++) {
+            switch(attr_list[i].type) {
+            case VASurfaceAttribMinWidth:
+                min_width = attr_list[i].value.value.i;
+                break;
+            case VASurfaceAttribMaxWidth:
+                max_width = attr_list[i].value.value.i;
+                break;
+            case VASurfaceAttribMinHeight:
+                min_height = attr_list[i].value.value.i;
+                break;
+            case VASurfaceAttribMaxHeight:
+                max_height = attr_list[i].value.value.i;
+                break;
+            }
+        }
+        av_free(attr_list);
+        if (attr_count == 0) {
+            av_log(ctx, AV_LOG_ERROR, "Failed to get surface attributes: "
+                   "%d (%s).\n", vas, vaErrorStr(vas));
+            err = AVERROR(EINVAL);
+            goto fail;
+        }
+        if (min_width  > ctx->decode_width  ||
+           max_width  < ctx->decode_width  ||
+           min_height > ctx->decode_height ||
+           max_height < ctx->decode_height) {
+            av_log(ctx, AV_LOG_ERROR, "Decode pipeline does not support "
+                   "image size %dx%d (width %d-%d height %d-%d).\n",
+                   ctx->decode_width, ctx->decode_height,
+                   min_width, max_width, min_height, max_height);
+            err = AVERROR(EINVAL);
+            goto fail;
+        }
+
+        ctx->decode_width  = avctx->coded_width;
+        ctx->decode_height = avctx->coded_height;
+    }
+
+    return 0;
+
+  fail:
+    return err;
+}
+
+static void vaapi_decode_uninit(AVCodecContext *avctx)
+{
+    InputStream  *ist = avctx->opaque;
+    VAAPIDecoderContext *ctx = ist->hwaccel_ctx;
+
+    if (ctx) {
+        AVVAAPIDeviceContext *hwctx = ctx->device->hwctx;
+
+        if (ctx->va_context != VA_INVALID_ID) {
+            vaDestroyContext(hwctx->display, ctx->va_context);
+            ctx->va_context = VA_INVALID_ID;
+        }
+        if (ctx->va_config != VA_INVALID_ID) {
+            vaDestroyConfig(hwctx->display, ctx->va_config);
+            ctx->va_config = VA_INVALID_ID;
+        }
+
+        if (ctx->frames_ref)
+            av_buffer_unref(&ctx->frames_ref);
+        if (ctx->device_ref)
+            av_buffer_unref(&ctx->device_ref);
+        av_free(ctx);
+    }
+
+    ist->hwaccel_ctx           = 0;
+    ist->hwaccel_uninit        = 0;
+    ist->hwaccel_get_buffer    = 0;
+    ist->hwaccel_retrieve_data = 0;
+}
+
+int vaapi_decode_init(AVCodecContext *avctx)
+{
+    InputStream *ist = avctx->opaque;
+    AVVAAPIDeviceContext *hwctx;
+    AVVAAPIFramesContext *avfc;
+    VAAPIDecoderContext *ctx;
+    VAStatus vas;
+    int err;
+    int loglevel = (ist->hwaccel_id != HWACCEL_VAAPI ? AV_LOG_VERBOSE
+                                                     : AV_LOG_ERROR);
+
+    // We have -hwaccel without -vaapi_device, so just initialise here with
+    // the device passed as -hwaccel_device (if -vaapi_device was passed, it
+    // will always have been called before now).
+    if (!vaapi_device_context) {
+        err = vaapi_device_init(ist->hwaccel_device);
+        if (err < 0)
+            return err;
+    }
+
+    if (ist->hwaccel_ctx) {
+        ctx = ist->hwaccel_ctx;
+
+        if (ctx->va_context != VA_INVALID_ID) {
+            vaDestroyContext(hwctx->display, ctx->va_context);
+            ctx->va_context = VA_INVALID_ID;
+        }
+        if (ctx->va_config != VA_INVALID_ID) {
+            vaDestroyConfig(hwctx->display, ctx->va_config);
+            ctx->va_config = VA_INVALID_ID;
+        }
+        if (ctx->frames) {
+            av_buffer_unref(&ctx->frames_ref);
+            ctx->frames = 0;
+        }
+
+    } else {
+        ctx = av_mallocz(sizeof(*ctx));
+        if (!ctx)
+            return AVERROR(ENOMEM);
+        ctx->class = &vaapi_class;
+
+        ctx->device_ref = av_buffer_ref(vaapi_device_ref);
+        ctx->device = vaapi_device_context;
+
+        ctx->va_config  = VA_INVALID_ID;
+        ctx->va_context = VA_INVALID_ID;
+    }
+    hwctx = ctx->device->hwctx;
+
+    ctx->output_format = ist->hwaccel_output_format;
+
+    err = vaapi_build_decoder_config(ctx, avctx,
+                                     ist->hwaccel_id != HWACCEL_VAAPI);
+    if (err < 0) {
+        av_log(ctx, loglevel, "No supported configuration for this codec.");
+        goto fail;
+    }
+
+    avctx->pix_fmt = ctx->output_format;
+
+    ctx->frames_ref = av_hwframe_ctx_alloc(ctx->device_ref);
+    if (!ctx->frames_ref) {
+        av_log(ctx, loglevel, "Failed to create VAAPI frame context.\n");
+        err = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    ctx->frames = (AVHWFramesContext*)ctx->frames_ref->data;
+
+    ctx->frames->format    = AV_PIX_FMT_VAAPI_VLD;
+    ctx->frames->sw_format = ctx->decode_format;
+    ctx->frames->width     = ctx->decode_width;
+    ctx->frames->height    = ctx->decode_height;
+    ctx->frames->initial_pool_size = ctx->decode_surfaces;
+
+    err = av_hwframe_ctx_init(ctx->frames_ref);
+    if (err < 0) {
+        av_log(ctx, loglevel, "Failed to initialise VAAPI frame "
+               "context: %d\n", err);
+        goto fail;
+    }
+
+    avfc = ctx->frames->hwctx;
+
+    vas = vaCreateContext(hwctx->display, ctx->va_config,
+                          ctx->decode_width, ctx->decode_height,
+                          VA_PROGRESSIVE,
+                          avfc->surface_ids, avfc->surface_count,
+                          &ctx->va_context);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to create decode pipeline "
+               "context: %d (%s).\n", vas, vaErrorStr(vas));
+        err = AVERROR(EINVAL);
+        goto fail;
+    }
+
+    av_log(ctx, AV_LOG_DEBUG, "VAAPI decoder (re)init complete.\n");
+
+    avctx->hw_frames_ctx = ctx->frames_ref;
+
+    ist->hwaccel_ctx           = ctx;
+    ist->hwaccel_uninit        = vaapi_decode_uninit;
+    ist->hwaccel_get_buffer    = vaapi_get_buffer;
+    ist->hwaccel_retrieve_data = vaapi_retrieve_data;
+
+    ctx->decoder_vaapi_context.display    = hwctx->display;
+    ctx->decoder_vaapi_context.config_id  = ctx->va_config;
+    ctx->decoder_vaapi_context.context_id = ctx->va_context;
+    avctx->hwaccel_context = &ctx->decoder_vaapi_context;
+
+    return 0;
+
+  fail:
+    vaapi_decode_uninit(avctx);
+    return err;
+}
+
+static AVClass *vaapi_log = &vaapi_class;
+
+int vaapi_device_init(const char *device)
+{
+    AVVAAPIDeviceContext *hwctx;
+    VADisplay display;
+    VAStatus vas;
+    int major, minor, err;
+
+    display = 0;
+
+#if HAVE_VAAPI_X11
+    if (!display) {
+        Display *x11_display;
+
+        // Try to open the device as an X11 display.
+        x11_display = XOpenDisplay(device);
+        if (!x11_display) {
+            av_log(&vaapi_log, AV_LOG_WARNING, "Cannot open X11 display "
+                   "%s.\n", XDisplayName(device));
+        } else {
+            display = vaGetDisplay(x11_display);
+            if (!display) {
+                av_log(&vaapi_log, AV_LOG_WARNING, "Cannot open a VA display "
+                       "from X11 display %s.\n", XDisplayName(device));
+                XCloseDisplay(x11_display);
+            } else {
+                av_log(&vaapi_log, AV_LOG_VERBOSE, "Opened VA display via "
+                       "X11 display %s.\n", XDisplayName(device));
+            }
+        }
+    }
+#endif
+
+#if HAVE_VAAPI_DRM
+    if (!display && device) {
+        int drm_fd;
+
+        // Try to open the device as a DRM path.
+        drm_fd = open(device, O_RDWR);
+        if (drm_fd < 0) {
+            err = errno;
+            av_log(&vaapi_log, AV_LOG_WARNING, "Cannot open DRM device %s.\n",
+                   device);
+        } else {
+            display = vaGetDisplayDRM(drm_fd);
+            if (!display) {
+                av_log(&vaapi_log, AV_LOG_WARNING, "Cannot open a VA display "
+                       "from DRM device %s.\n", device);
+                close(drm_fd);
+            } else {
+                av_log(&vaapi_log, AV_LOG_VERBOSE, "Opened VA display via "
+                       "DRM device %s.\n", device);
+            }
+        }
+    }
+#endif
+
+    if (!display) {
+        av_log(&vaapi_log, AV_LOG_ERROR, "No VA display found for "
+               "device %s.\n", device);
+        return AVERROR(EINVAL);
+    }
+
+    vas = vaInitialize(display, &major, &minor);
+    if (vas != VA_STATUS_SUCCESS) {
+        av_log(&vaapi_log, AV_LOG_ERROR, "Failed to initialise VAAPI "
+               "connection: %d (%s).\n", vas, vaErrorStr(vas));
+        return AVERROR(EINVAL);
+    }
+    av_log(&vaapi_log, AV_LOG_VERBOSE, "Initialised VAAPI connection: "
+           "version %d.%d\n", major, minor);
+
+    vaapi_device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_VAAPI);
+    if (!vaapi_device_ref) {
+        av_log(&vaapi_log, AV_LOG_ERROR, "Failed to create VAAPI "
+               "hardware context.\n");
+        vaTerminate(display);
+        return AVERROR(ENOMEM);
+    }
+    vaapi_device_context = (AVHWDeviceContext*)vaapi_device_ref->data;
+
+    hwctx = vaapi_device_context->hwctx;
+    hwctx->display = display;
+
+    err = av_hwdevice_ctx_init(vaapi_device_ref);
+    if (err < 0) {
+        av_log(&vaapi_log, AV_LOG_ERROR, "Failed to initialise VAAPI "
+               "hardware context: %d\n", err);
+        return err;
+    }
+
+    return 0;
+}
diff --git a/configure b/configure
index c0d8d5e..534d099 100755
--- a/configure
+++ b/configure
@@ -1667,6 +1667,7 @@ HAVE_LIST="
     sdl
     section_data_rel_ro
     threads
+    vaapi_drm
     vaapi_x11
     vdpau_x11
     xlib
@@ -4714,6 +4715,10 @@ enabled vaapi && enabled xlib &&
     enable vaapi_x11

 enabled vaapi &&
+    check_lib2 "va/va.h va/va_drm.h" vaGetDisplayDRM -lva -lva-drm &&
+    enable vaapi_drm
+
+enabled vaapi &&
     check_code cc "va/va.h" "vaCreateSurfaces(0, 0, 0, 0, 0, 0, 0, 0)" &&
     enable vaapi_recent

-- 
2.7.0


_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to