[FFmpeg-cvslog] Merge commit 'b46a77f19ddc4b2b5fa3187835ceb602a5244e24'

James Almer git at videolan.org
Sun Nov 12 01:34:12 EET 2017


ffmpeg | branch: master | James Almer <jamrial at gmail.com> | Sat Nov 11 18:58:12 2017 -0300| [48e4eda11d537c6ed52d1000aaa6ce5cbb641e25] | committer: James Almer

Merge commit 'b46a77f19ddc4b2b5fa3187835ceb602a5244e24'

* commit 'b46a77f19ddc4b2b5fa3187835ceb602a5244e24':
  lavc: external hardware frame pool initialization

Includes the fix from e724bdfffbd3c27aac53d1f32f20f105f37caef0

Merged-by: James Almer <jamrial at gmail.com>

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=48e4eda11d537c6ed52d1000aaa6ce5cbb641e25
---

 doc/APIchanges              |   3 +
 libavcodec/avcodec.h        | 113 +++++++++++++++++++++++
 libavcodec/decode.c         |  80 ++++++++++++++++
 libavcodec/decode.h         |  12 +++
 libavcodec/dxva2.c          |  55 +++++------
 libavcodec/dxva2_h264.c     |   3 +
 libavcodec/dxva2_hevc.c     |   3 +
 libavcodec/dxva2_internal.h |   3 +
 libavcodec/dxva2_mpeg2.c    |   3 +
 libavcodec/dxva2_vc1.c      |   5 +
 libavcodec/dxva2_vp9.c      |   3 +
 libavcodec/vaapi_decode.c   | 216 +++++++++++++++++++++-----------------------
 libavcodec/vaapi_decode.h   |   5 +-
 libavcodec/vaapi_h264.c     |   1 +
 libavcodec/vaapi_hevc.c     |   1 +
 libavcodec/vaapi_mpeg2.c    |   1 +
 libavcodec/vaapi_mpeg4.c    |   2 +
 libavcodec/vaapi_vc1.c      |   2 +
 libavcodec/vaapi_vp9.c      |   1 +
 libavcodec/vdpau.c          |  58 ++++++------
 libavcodec/vdpau_h264.c     |   1 +
 libavcodec/vdpau_hevc.c     |   1 +
 libavcodec/vdpau_internal.h |   2 +
 libavcodec/vdpau_mpeg12.c   |   1 +
 libavcodec/vdpau_mpeg4.c    |   1 +
 libavcodec/vdpau_vc1.c      |   2 +
 libavcodec/version.h        |   2 +-
 27 files changed, 399 insertions(+), 181 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 1490d67f27..d336f6ce22 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,6 +15,9 @@ libavutil:     2017-10-21
 
 API changes, most recent first:
 
+2017-xx-xx - xxxxxxx - lavc 58.3.100 - avcodec.h
+  Add avcodec_get_hw_frames_parameters().
+
 -------- 8< --------- FFmpeg 3.4 was cut here -------- 8< ---------
 
 2017-09-28 - b6cf66ae1c - lavc 57.106.104 - avcodec.h
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 15ca871b59..8c76c9f908 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -3534,6 +3534,16 @@ typedef struct AVHWAccel {
     int caps_internal;
 
     /**
+     * Fill the given hw_frames context with current codec parameters. Called
+     * from get_format. Refer to avcodec_get_hw_frames_parameters() for
+     * details.
+     *
+     * This CAN be called before AVHWAccel.init is called, and you must assume
+     * that avctx->hwaccel_priv_data is invalid.
+     */
+    int (*frame_params)(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx);
+
+    /**
      * Some hwaccels are ambiguous if only the id and pix_fmt fields are used.
      * If non-NULL, the associated AVCodec must have
      * FF_CODEC_CAP_HWACCEL_REQUIRE_CLASS set.
@@ -4674,6 +4684,109 @@ int avcodec_send_frame(AVCodecContext *avctx, const AVFrame *frame);
  */
 int avcodec_receive_packet(AVCodecContext *avctx, AVPacket *avpkt);
 
+/**
+ * Create and return a AVHWFramesContext with values adequate for hardware
+ * decoding. This is meant to get called from the get_format callback, and is
+ * a helper for preparing a AVHWFramesContext for AVCodecContext.hw_frames_ctx.
+ * This API is for decoding with certain hardware acceleration modes/APIs only.
+ *
+ * The returned AVHWFramesContext is not initialized. The caller must do this
+ * with av_hwframe_ctx_init().
+ *
+ * Calling this function is not a requirement, but makes it simpler to avoid
+ * codec or hardware API specific details when manually allocating frames.
+ *
+ * Alternatively to this, an API user can set AVCodecContext.hw_device_ctx,
+ * which sets up AVCodecContext.hw_frames_ctx fully automatically, and makes
+ * it unnecessary to call this function or having to care about
+ * AVHWFramesContext initialization at all.
+ *
+ * There are a number of requirements for calling this function:
+ *
+ * - It must be called from get_format with the same avctx parameter that was
+ *   passed to get_format. Calling it outside of get_format is not allowed, and
+ *   can trigger undefined behavior.
+ * - The function is not always supported (see description of return values).
+ *   Even if this function returns successfully, hwaccel initialization could
+ *   fail later. (The degree to which implementations check whether the stream
+ *   is actually supported varies. Some do this check only after the user's
+ *   get_format callback returns.)
+ * - The hw_pix_fmt must be one of the choices suggested by get_format. If the
+ *   user decides to use a AVHWFramesContext prepared with this API function,
+ *   the user must return the same hw_pix_fmt from get_format.
+ * - The device_ref passed to this function must support the given hw_pix_fmt.
+ * - After calling this API function, it is the user's responsibility to
+ *   initialize the AVHWFramesContext (returned by the out_frames_ref parameter),
+ *   and to set AVCodecContext.hw_frames_ctx to it. If done, this must be done
+ *   before returning from get_format (this is implied by the normal
+ *   AVCodecContext.hw_frames_ctx API rules).
+ * - The AVHWFramesContext parameters may change every time time get_format is
+ *   called. Also, AVCodecContext.hw_frames_ctx is reset before get_format. So
+ *   you are inherently required to go through this process again on every
+ *   get_format call.
+ * - It is perfectly possible to call this function without actually using
+ *   the resulting AVHWFramesContext. One use-case might be trying to reuse a
+ *   previously initialized AVHWFramesContext, and calling this API function
+ *   only to test whether the required frame parameters have changed.
+ * - Fields that use dynamically allocated values of any kind must not be set
+ *   by the user unless setting them is explicitly allowed by the documentation.
+ *   If the user sets AVHWFramesContext.free and AVHWFramesContext.user_opaque,
+ *   the new free callback must call the potentially set previous free callback.
+ *   This API call may set any dynamically allocated fields, including the free
+ *   callback.
+ *
+ * The function will set at least the following fields on AVHWFramesContext
+ * (potentially more, depending on hwaccel API):
+ *
+ * - All fields set by av_hwframe_ctx_alloc().
+ * - Set the format field to hw_pix_fmt.
+ * - Set the sw_format field to the most suited and most versatile format. (An
+ *   implication is that this will prefer generic formats over opaque formats
+ *   with arbitrary restrictions, if possible.)
+ * - Set the width/height fields to the coded frame size, rounded up to the
+ *   API-specific minimum alignment.
+ * - Only _if_ the hwaccel requires a pre-allocated pool: set the initial_pool_size
+ *   field to the number of maximum reference surfaces possible with the codec,
+ *   plus 1 surface for the user to work (meaning the user can safely reference
+ *   at most 1 decoded surface at a time), plus additional buffering introduced
+ *   by frame threading. If the hwaccel does not require pre-allocation, the
+ *   field is left to 0, and the decoder will allocate new surfaces on demand
+ *   during decoding.
+ * - Possibly AVHWFramesContext.hwctx fields, depending on the underlying
+ *   hardware API.
+ *
+ * Essentially, out_frames_ref returns the same as av_hwframe_ctx_alloc(), but
+ * with basic frame parameters set.
+ *
+ * The function is stateless, and does not change the AVCodecContext or the
+ * device_ref AVHWDeviceContext.
+ *
+ * @param avctx The context which is currently calling get_format, and which
+ *              implicitly contains all state needed for filling the returned
+ *              AVHWFramesContext properly.
+ * @param device_ref A reference to the AVHWDeviceContext describing the device
+ *                   which will be used by the hardware decoder.
+ * @param hw_pix_fmt The hwaccel format you are going to return from get_format.
+ * @param out_frames_ref On success, set to a reference to an _uninitialized_
+ *                       AVHWFramesContext, created from the given device_ref.
+ *                       Fields will be set to values required for decoding.
+ *                       Not changed if an error is returned.
+ * @return zero on success, a negative value on error. The following error codes
+ *         have special semantics:
+ *      AVERROR(ENOENT): the decoder does not support this functionality. Setup
+ *                       is always manual, or it is a decoder which does not
+ *                       support setting AVCodecContext.hw_frames_ctx at all,
+ *                       or it is a software format.
+ *      AVERROR(EINVAL): it is known that hardware decoding is not supported for
+ *                       this configuration, or the device_ref is not supported
+ *                       for the hwaccel referenced by hw_pix_fmt.
+ */
+int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
+                                     AVBufferRef *device_ref,
+                                     enum AVPixelFormat hw_pix_fmt,
+                                     AVBufferRef **out_frames_ref);
+
+
 
 /**
  * @defgroup lavc_parsing Frame parsing
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 7601d16082..a7f1e23fc2 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -1106,6 +1106,86 @@ static AVHWAccel *find_hwaccel(AVCodecContext *avctx,
     return NULL;
 }
 
+int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
+                                enum AVHWDeviceType dev_type)
+{
+    AVHWDeviceContext *device_ctx;
+    AVHWFramesContext *frames_ctx;
+    int ret;
+
+    if (!avctx->hwaccel)
+        return AVERROR(ENOSYS);
+
+    if (avctx->hw_frames_ctx)
+        return 0;
+    if (!avctx->hw_device_ctx) {
+        av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is "
+                "required for hardware accelerated decoding.\n");
+        return AVERROR(EINVAL);
+    }
+
+    device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
+    if (device_ctx->type != dev_type) {
+        av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware "
+               "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
+               av_hwdevice_get_type_name(device_ctx->type));
+        return AVERROR(EINVAL);
+    }
+
+    ret = avcodec_get_hw_frames_parameters(avctx,
+                                           avctx->hw_device_ctx,
+                                           avctx->hwaccel->pix_fmt,
+                                           &avctx->hw_frames_ctx);
+    if (ret < 0)
+        return ret;
+
+    frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
+
+
+    if (frames_ctx->initial_pool_size) {
+        // We guarantee 4 base work surfaces. The function above guarantees 1
+        // (the absolute minimum), so add the missing count.
+        frames_ctx->initial_pool_size += 3;
+
+        // Add an additional surface per thread is frame threading is enabled.
+        if (avctx->active_thread_type & FF_THREAD_FRAME)
+            frames_ctx->initial_pool_size += avctx->thread_count;
+    }
+
+    ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
+    if (ret < 0) {
+        av_buffer_unref(&avctx->hw_frames_ctx);
+        return ret;
+    }
+
+    return 0;
+}
+
+int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
+                                     AVBufferRef *device_ref,
+                                     enum AVPixelFormat hw_pix_fmt,
+                                     AVBufferRef **out_frames_ref)
+{
+    AVBufferRef *frames_ref = NULL;
+    AVHWAccel *hwa = find_hwaccel(avctx, hw_pix_fmt);
+    int ret;
+
+    if (!hwa || !hwa->frame_params)
+        return AVERROR(ENOENT);
+
+    frames_ref = av_hwframe_ctx_alloc(device_ref);
+    if (!frames_ref)
+        return AVERROR(ENOMEM);
+
+    ret = hwa->frame_params(avctx, frames_ref);
+    if (ret >= 0) {
+        *out_frames_ref = frames_ref;
+    } else {
+        av_buffer_unref(&frames_ref);
+    }
+    return ret;
+}
+
 static int setup_hwaccel(AVCodecContext *avctx,
                          const enum AVPixelFormat fmt,
                          const char *name)
diff --git a/libavcodec/decode.h b/libavcodec/decode.h
index 03fc783bba..424e85e914 100644
--- a/libavcodec/decode.h
+++ b/libavcodec/decode.h
@@ -24,6 +24,10 @@
 #include "libavutil/buffer.h"
 #include "libavutil/frame.h"
 
+#include "libavutil/buffer.h"
+#include "libavutil/frame.h"
+#include "libavutil/hwcontext.h"
+
 #include "avcodec.h"
 
 /**
@@ -65,6 +69,14 @@ int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt);
 
 void ff_decode_bsfs_uninit(AVCodecContext *avctx);
 
+/**
+ * Make sure avctx.hw_frames_ctx is set. If it's not set, the function will
+ * try to allocate it from hw_device_ctx. If that is not possible, an error
+ * message is printed, and an error code is returned.
+ */
+int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
+                                enum AVHWDeviceType dev_type);
+
 int ff_attach_decode_data(AVFrame *frame);
 
 #endif /* AVCODEC_DECODE_H */
diff --git a/libavcodec/dxva2.c b/libavcodec/dxva2.c
index afcd361ef6..6f294703fb 100644
--- a/libavcodec/dxva2.c
+++ b/libavcodec/dxva2.c
@@ -29,6 +29,7 @@
 #include "libavutil/time.h"
 
 #include "avcodec.h"
+#include "decode.h"
 #include "dxva2_internal.h"
 
 /* define all the GUIDs used directly here,
@@ -576,14 +577,20 @@ static void ff_dxva2_unlock(AVCodecContext *avctx)
 #endif
 }
 
-// This must work before the decoder is created.
-// This somehow needs to be exported to the user.
-static void dxva_adjust_hwframes(AVCodecContext *avctx, AVHWFramesContext *frames_ctx)
+int ff_dxva2_common_frame_params(AVCodecContext *avctx,
+                                 AVBufferRef *hw_frames_ctx)
 {
-    FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
+    AVHWFramesContext *frames_ctx = (AVHWFramesContext *)hw_frames_ctx->data;
+    AVHWDeviceContext *device_ctx = frames_ctx->device_ctx;
     int surface_alignment, num_surfaces;
 
-    frames_ctx->format = sctx->pix_fmt;
+    if (device_ctx->type == AV_HWDEVICE_TYPE_DXVA2) {
+        frames_ctx->format = AV_PIX_FMT_DXVA2_VLD;
+    } else if (device_ctx->type == AV_HWDEVICE_TYPE_D3D11VA) {
+        frames_ctx->format = AV_PIX_FMT_D3D11;
+    } else {
+        return AVERROR(EINVAL);
+    }
 
     /* decoding MPEG-2 requires additional alignment on some Intel GPUs,
     but it causes issues for H.264 on certain AMD GPUs..... */
@@ -596,8 +603,8 @@ static void dxva_adjust_hwframes(AVCodecContext *avctx, AVHWFramesContext *frame
     else
         surface_alignment = 16;
 
-    /* 4 base work surfaces */
-    num_surfaces = 4;
+    /* 1 base work surface */
+    num_surfaces = 1;
 
     /* add surfaces based on number of possible refs */
     if (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == AV_CODEC_ID_HEVC)
@@ -633,12 +640,16 @@ static void dxva_adjust_hwframes(AVCodecContext *avctx, AVHWFramesContext *frame
         frames_hwctx->BindFlags |= D3D11_BIND_DECODER;
     }
 #endif
+
+    return 0;
 }
 
 int ff_dxva2_decode_init(AVCodecContext *avctx)
 {
     FFDXVASharedContext *sctx = DXVA_SHARED_CONTEXT(avctx);
-    AVHWFramesContext *frames_ctx = NULL;
+    AVHWFramesContext *frames_ctx;
+    enum AVHWDeviceType dev_type = avctx->hwaccel->pix_fmt == AV_PIX_FMT_DXVA2_VLD
+                            ? AV_HWDEVICE_TYPE_DXVA2 : AV_HWDEVICE_TYPE_D3D11VA;
     int ret = 0;
 
     // Old API.
@@ -648,32 +659,14 @@ int ff_dxva2_decode_init(AVCodecContext *avctx)
     // (avctx->pix_fmt is not updated yet at this point)
     sctx->pix_fmt = avctx->hwaccel->pix_fmt;
 
-    if (!avctx->hw_frames_ctx && !avctx->hw_device_ctx) {
-        av_log(avctx, AV_LOG_ERROR, "Either a hw_frames_ctx or a hw_device_ctx needs to be set for hardware decoding.\n");
-        return AVERROR(EINVAL);
-    }
-
-    if (avctx->hw_frames_ctx) {
-        frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
-    } else {
-        avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
-        if (!avctx->hw_frames_ctx)
-            return AVERROR(ENOMEM);
-
-        frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
-
-        dxva_adjust_hwframes(avctx, frames_ctx);
-
-        ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
-        if (ret < 0)
-            goto fail;
-    }
+    ret = ff_decode_get_hw_frames_ctx(avctx, dev_type);
+    if (ret < 0)
+        return ret;
 
+    frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
     sctx->device_ctx = frames_ctx->device_ctx;
 
-    if (frames_ctx->format != sctx->pix_fmt ||
-        !((sctx->pix_fmt == AV_PIX_FMT_D3D11 && CONFIG_D3D11VA) ||
-          (sctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD && CONFIG_DXVA2))) {
+    if (frames_ctx->format != sctx->pix_fmt) {
         av_log(avctx, AV_LOG_ERROR, "Invalid pixfmt for hwaccel!\n");
         ret = AVERROR(EINVAL);
         goto fail;
diff --git a/libavcodec/dxva2_h264.c b/libavcodec/dxva2_h264.c
index e3a3f7866e..ee35b20e82 100644
--- a/libavcodec/dxva2_h264.c
+++ b/libavcodec/dxva2_h264.c
@@ -528,6 +528,7 @@ AVHWAccel ff_h264_dxva2_hwaccel = {
     .start_frame    = dxva2_h264_start_frame,
     .decode_slice   = dxva2_h264_decode_slice,
     .end_frame      = dxva2_h264_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
@@ -544,6 +545,7 @@ AVHWAccel ff_h264_d3d11va_hwaccel = {
     .start_frame    = dxva2_h264_start_frame,
     .decode_slice   = dxva2_h264_decode_slice,
     .end_frame      = dxva2_h264_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
@@ -560,6 +562,7 @@ AVHWAccel ff_h264_d3d11va2_hwaccel = {
     .start_frame    = dxva2_h264_start_frame,
     .decode_slice   = dxva2_h264_decode_slice,
     .end_frame      = dxva2_h264_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
diff --git a/libavcodec/dxva2_hevc.c b/libavcodec/dxva2_hevc.c
index 88f887a1b5..542afc383a 100644
--- a/libavcodec/dxva2_hevc.c
+++ b/libavcodec/dxva2_hevc.c
@@ -432,6 +432,7 @@ AVHWAccel ff_hevc_dxva2_hwaccel = {
     .start_frame    = dxva2_hevc_start_frame,
     .decode_slice   = dxva2_hevc_decode_slice,
     .end_frame      = dxva2_hevc_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct hevc_dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
@@ -448,6 +449,7 @@ AVHWAccel ff_hevc_d3d11va_hwaccel = {
     .start_frame    = dxva2_hevc_start_frame,
     .decode_slice   = dxva2_hevc_decode_slice,
     .end_frame      = dxva2_hevc_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct hevc_dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
@@ -464,6 +466,7 @@ AVHWAccel ff_hevc_d3d11va2_hwaccel = {
     .start_frame    = dxva2_hevc_start_frame,
     .decode_slice   = dxva2_hevc_decode_slice,
     .end_frame      = dxva2_hevc_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct hevc_dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
diff --git a/libavcodec/dxva2_internal.h b/libavcodec/dxva2_internal.h
index 352a9db1ab..8bb3344090 100644
--- a/libavcodec/dxva2_internal.h
+++ b/libavcodec/dxva2_internal.h
@@ -156,6 +156,9 @@ int ff_dxva2_decode_init(AVCodecContext *avctx);
 
 int ff_dxva2_decode_uninit(AVCodecContext *avctx);
 
+int ff_dxva2_common_frame_params(AVCodecContext *avctx,
+                                 AVBufferRef *hw_frames_ctx);
+
 int ff_dxva2_is_d3d11(const AVCodecContext *avctx);
 
 #endif /* AVCODEC_DXVA2_INTERNAL_H */
diff --git a/libavcodec/dxva2_mpeg2.c b/libavcodec/dxva2_mpeg2.c
index b7c69378f0..df5fe59a7d 100644
--- a/libavcodec/dxva2_mpeg2.c
+++ b/libavcodec/dxva2_mpeg2.c
@@ -327,6 +327,7 @@ AVHWAccel ff_mpeg2_dxva2_hwaccel = {
     .start_frame    = dxva2_mpeg2_start_frame,
     .decode_slice   = dxva2_mpeg2_decode_slice,
     .end_frame      = dxva2_mpeg2_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
@@ -343,6 +344,7 @@ AVHWAccel ff_mpeg2_d3d11va_hwaccel = {
     .start_frame    = dxva2_mpeg2_start_frame,
     .decode_slice   = dxva2_mpeg2_decode_slice,
     .end_frame      = dxva2_mpeg2_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
@@ -359,6 +361,7 @@ AVHWAccel ff_mpeg2_d3d11va2_hwaccel = {
     .start_frame    = dxva2_mpeg2_start_frame,
     .decode_slice   = dxva2_mpeg2_decode_slice,
     .end_frame      = dxva2_mpeg2_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
diff --git a/libavcodec/dxva2_vc1.c b/libavcodec/dxva2_vc1.c
index e5353cdddc..f21c5d5544 100644
--- a/libavcodec/dxva2_vc1.c
+++ b/libavcodec/dxva2_vc1.c
@@ -388,6 +388,7 @@ AVHWAccel ff_wmv3_dxva2_hwaccel = {
     .start_frame    = dxva2_vc1_start_frame,
     .decode_slice   = dxva2_vc1_decode_slice,
     .end_frame      = dxva2_vc1_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
@@ -404,6 +405,7 @@ AVHWAccel ff_vc1_dxva2_hwaccel = {
     .start_frame    = dxva2_vc1_start_frame,
     .decode_slice   = dxva2_vc1_decode_slice,
     .end_frame      = dxva2_vc1_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
@@ -420,6 +422,7 @@ AVHWAccel ff_wmv3_d3d11va_hwaccel = {
     .start_frame    = dxva2_vc1_start_frame,
     .decode_slice   = dxva2_vc1_decode_slice,
     .end_frame      = dxva2_vc1_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
@@ -436,6 +439,7 @@ AVHWAccel ff_wmv3_d3d11va2_hwaccel = {
     .start_frame    = dxva2_vc1_start_frame,
     .decode_slice   = dxva2_vc1_decode_slice,
     .end_frame      = dxva2_vc1_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
@@ -452,6 +456,7 @@ AVHWAccel ff_vc1_d3d11va_hwaccel = {
     .start_frame    = dxva2_vc1_start_frame,
     .decode_slice   = dxva2_vc1_decode_slice,
     .end_frame      = dxva2_vc1_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
diff --git a/libavcodec/dxva2_vp9.c b/libavcodec/dxva2_vp9.c
index 6d87fdd9f2..a2e55f5736 100644
--- a/libavcodec/dxva2_vp9.c
+++ b/libavcodec/dxva2_vp9.c
@@ -319,6 +319,7 @@ AVHWAccel ff_vp9_dxva2_hwaccel = {
     .start_frame    = dxva2_vp9_start_frame,
     .decode_slice   = dxva2_vp9_decode_slice,
     .end_frame      = dxva2_vp9_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct vp9_dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
@@ -335,6 +336,7 @@ AVHWAccel ff_vp9_d3d11va_hwaccel = {
     .start_frame    = dxva2_vp9_start_frame,
     .decode_slice   = dxva2_vp9_decode_slice,
     .end_frame      = dxva2_vp9_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct vp9_dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
@@ -351,6 +353,7 @@ AVHWAccel ff_vp9_d3d11va2_hwaccel = {
     .start_frame    = dxva2_vp9_start_frame,
     .decode_slice   = dxva2_vp9_decode_slice,
     .end_frame      = dxva2_vp9_end_frame,
+    .frame_params   = ff_dxva2_common_frame_params,
     .frame_priv_data_size = sizeof(struct vp9_dxva2_picture_context),
     .priv_data_size = sizeof(FFDXVASharedContext),
 };
diff --git a/libavcodec/vaapi_decode.c b/libavcodec/vaapi_decode.c
index 27ef33837c..d467bed874 100644
--- a/libavcodec/vaapi_decode.c
+++ b/libavcodec/vaapi_decode.c
@@ -21,6 +21,7 @@
 #include "libavutil/pixdesc.h"
 
 #include "avcodec.h"
+#include "decode.h"
 #include "internal.h"
 #include "vaapi_decode.h"
 
@@ -272,10 +273,15 @@ static const struct {
 #undef MAP
 };
 
-static int vaapi_decode_make_config(AVCodecContext *avctx)
+/*
+ * Set *va_config and the frames_ref fields from the current codec parameters
+ * in avctx.
+ */
+static int vaapi_decode_make_config(AVCodecContext *avctx,
+                                    AVBufferRef *device_ref,
+                                    VAConfigID *va_config,
+                                    AVBufferRef *frames_ref)
 {
-    VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
-
     AVVAAPIHWConfig       *hwconfig    = NULL;
     AVHWFramesConstraints *constraints = NULL;
     VAStatus vas;
@@ -285,13 +291,16 @@ static int vaapi_decode_make_config(AVCodecContext *avctx)
     int profile_count, exact_match, alt_profile;
     const AVPixFmtDescriptor *sw_desc, *desc;
 
+    AVHWDeviceContext    *device = (AVHWDeviceContext*)device_ref->data;
+    AVVAAPIDeviceContext *hwctx = device->hwctx;
+
     codec_desc = avcodec_descriptor_get(avctx->codec_id);
     if (!codec_desc) {
         err = AVERROR(EINVAL);
         goto fail;
     }
 
-    profile_count = vaMaxNumProfiles(ctx->hwctx->display);
+    profile_count = vaMaxNumProfiles(hwctx->display);
     profile_list  = av_malloc_array(profile_count,
                                     sizeof(VAProfile));
     if (!profile_list) {
@@ -299,7 +308,7 @@ static int vaapi_decode_make_config(AVCodecContext *avctx)
         goto fail;
     }
 
-    vas = vaQueryConfigProfiles(ctx->hwctx->display,
+    vas = vaQueryConfigProfiles(hwctx->display,
                                 profile_list, &profile_count);
     if (vas != VA_STATUS_SUCCESS) {
         av_log(avctx, AV_LOG_ERROR, "Failed to query profiles: "
@@ -358,12 +367,9 @@ static int vaapi_decode_make_config(AVCodecContext *avctx)
         }
     }
 
-    ctx->va_profile    = profile;
-    ctx->va_entrypoint = VAEntrypointVLD;
-
-    vas = vaCreateConfig(ctx->hwctx->display, ctx->va_profile,
-                         ctx->va_entrypoint, NULL, 0,
-                         &ctx->va_config);
+    vas = vaCreateConfig(hwctx->display, profile,
+                         VAEntrypointVLD, NULL, 0,
+                         va_config);
     if (vas != VA_STATUS_SUCCESS) {
         av_log(avctx, AV_LOG_ERROR, "Failed to create decode "
                "configuration: %d (%s).\n", vas, vaErrorStr(vas));
@@ -371,20 +377,15 @@ static int vaapi_decode_make_config(AVCodecContext *avctx)
         goto fail;
     }
 
-    hwconfig = av_hwdevice_hwconfig_alloc(avctx->hw_device_ctx ?
-                                          avctx->hw_device_ctx :
-                                          ctx->frames->device_ref);
+    hwconfig = av_hwdevice_hwconfig_alloc(device_ref);
     if (!hwconfig) {
         err = AVERROR(ENOMEM);
         goto fail;
     }
-    hwconfig->config_id = ctx->va_config;
+    hwconfig->config_id = *va_config;
 
     constraints =
-        av_hwdevice_get_hwframe_constraints(avctx->hw_device_ctx ?
-                                            avctx->hw_device_ctx :
-                                            ctx->frames->device_ref,
-                                            hwconfig);
+        av_hwdevice_get_hwframe_constraints(device_ref, hwconfig);
     if (!constraints) {
         err = AVERROR(ENOMEM);
         goto fail;
@@ -410,48 +411,52 @@ static int vaapi_decode_make_config(AVCodecContext *avctx)
         goto fail;
     }
 
-    // Find the first format in the list which matches the expected
-    // bit depth and subsampling.  If none are found (this can happen
-    // when 10-bit streams are decoded to 8-bit surfaces, for example)
-    // then just take the first format on the list.
-    ctx->surface_format = constraints->valid_sw_formats[0];
-    sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
-    for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
-        desc = av_pix_fmt_desc_get(constraints->valid_sw_formats[i]);
-        if (desc->nb_components != sw_desc->nb_components ||
-            desc->log2_chroma_w != sw_desc->log2_chroma_w ||
-            desc->log2_chroma_h != sw_desc->log2_chroma_h)
-            continue;
-        for (j = 0; j < desc->nb_components; j++) {
-            if (desc->comp[j].depth != sw_desc->comp[j].depth)
-                break;
+    if (frames_ref) {
+        AVHWFramesContext *frames = (AVHWFramesContext *)frames_ref->data;
+
+        frames->format = AV_PIX_FMT_VAAPI;
+        frames->width = avctx->coded_width;
+        frames->height = avctx->coded_height;
+
+        // Find the first format in the list which matches the expected
+        // bit depth and subsampling.  If none are found (this can happen
+        // when 10-bit streams are decoded to 8-bit surfaces, for example)
+        // then just take the first format on the list.
+        frames->sw_format = constraints->valid_sw_formats[0];
+        sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
+        for (i = 0; constraints->valid_sw_formats[i] != AV_PIX_FMT_NONE; i++) {
+            desc = av_pix_fmt_desc_get(constraints->valid_sw_formats[i]);
+            if (desc->nb_components != sw_desc->nb_components ||
+                desc->log2_chroma_w != sw_desc->log2_chroma_w ||
+                desc->log2_chroma_h != sw_desc->log2_chroma_h)
+                continue;
+            for (j = 0; j < desc->nb_components; j++) {
+                if (desc->comp[j].depth != sw_desc->comp[j].depth)
+                    break;
+            }
+            if (j < desc->nb_components)
+                continue;
+            frames->sw_format = constraints->valid_sw_formats[i];
+            break;
         }
-        if (j < desc->nb_components)
-            continue;
-        ctx->surface_format = constraints->valid_sw_formats[i];
-        break;
-    }
 
-    // Start with at least four surfaces.
-    ctx->surface_count = 4;
-    // Add per-codec number of surfaces used for storing reference frames.
-    switch (avctx->codec_id) {
-    case AV_CODEC_ID_H264:
-    case AV_CODEC_ID_HEVC:
-        ctx->surface_count += 16;
-        break;
-    case AV_CODEC_ID_VP9:
-        ctx->surface_count += 8;
-        break;
-    case AV_CODEC_ID_VP8:
-        ctx->surface_count += 3;
-        break;
-    default:
-        ctx->surface_count += 2;
+        frames->initial_pool_size = 1;
+        // Add per-codec number of surfaces used for storing reference frames.
+        switch (avctx->codec_id) {
+        case AV_CODEC_ID_H264:
+        case AV_CODEC_ID_HEVC:
+            frames->initial_pool_size += 16;
+            break;
+        case AV_CODEC_ID_VP9:
+            frames->initial_pool_size += 8;
+            break;
+        case AV_CODEC_ID_VP8:
+            frames->initial_pool_size += 3;
+            break;
+        default:
+            frames->initial_pool_size += 2;
+        }
     }
-    // Add an additional surface per thread is frame threading is enabled.
-    if (avctx->active_thread_type & FF_THREAD_FRAME)
-        ctx->surface_count += avctx->thread_count;
 
     av_hwframe_constraints_free(&constraints);
     av_freep(&hwconfig);
@@ -461,14 +466,38 @@ static int vaapi_decode_make_config(AVCodecContext *avctx)
 fail:
     av_hwframe_constraints_free(&constraints);
     av_freep(&hwconfig);
-    if (ctx->va_config != VA_INVALID_ID) {
-        vaDestroyConfig(ctx->hwctx->display, ctx->va_config);
-        ctx->va_config = VA_INVALID_ID;
+    if (*va_config != VA_INVALID_ID) {
+        vaDestroyConfig(hwctx->display, *va_config);
+        *va_config = VA_INVALID_ID;
     }
     av_freep(&profile_list);
     return err;
 }
 
+int ff_vaapi_common_frame_params(AVCodecContext *avctx,
+                                 AVBufferRef *hw_frames_ctx)
+{
+    AVHWFramesContext *hw_frames = (AVHWFramesContext *)hw_frames_ctx->data;
+    AVHWDeviceContext *device_ctx = hw_frames->device_ctx;
+    AVVAAPIDeviceContext *hwctx;
+    VAConfigID va_config = VA_INVALID_ID;
+    int err;
+
+    if (device_ctx->type != AV_HWDEVICE_TYPE_VAAPI)
+        return AVERROR(EINVAL);
+    hwctx = device_ctx->hwctx;
+
+    err = vaapi_decode_make_config(avctx, hw_frames->device_ref, &va_config,
+                                   hw_frames_ctx);
+    if (err)
+        return err;
+
+    if (va_config != VA_INVALID_ID)
+        vaDestroyConfig(hwctx->display, va_config);
+
+    return 0;
+}
+
 int ff_vaapi_decode_init(AVCodecContext *avctx)
 {
     VAAPIDecodeContext *ctx = avctx->internal->hwaccel_priv_data;
@@ -505,36 +534,8 @@ int ff_vaapi_decode_init(AVCodecContext *avctx)
         ctx->hwctx->driver_quirks =
             AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
 
-    } else
-#endif
-    if (avctx->hw_frames_ctx) {
-        // This structure has a shorter lifetime than the enclosing
-        // AVCodecContext, so we inherit the references from there
-        // and do not need to make separate ones.
-
-        ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
-        ctx->hwfc   = ctx->frames->hwctx;
-        ctx->device = ctx->frames->device_ctx;
-        ctx->hwctx  = ctx->device->hwctx;
-
-    } else if (avctx->hw_device_ctx) {
-        ctx->device = (AVHWDeviceContext*)avctx->hw_device_ctx->data;
-        ctx->hwctx  = ctx->device->hwctx;
-
-        if (ctx->device->type != AV_HWDEVICE_TYPE_VAAPI) {
-            av_log(avctx, AV_LOG_ERROR, "Device supplied for VAAPI "
-                   "decoding must be a VAAPI device (not %d).\n",
-                   ctx->device->type);
-            err = AVERROR(EINVAL);
-            goto fail;
-        }
-
-    } else {
-        av_log(avctx, AV_LOG_ERROR, "A hardware device or frames context "
-               "is required for VAAPI decoding.\n");
-        err = AVERROR(EINVAL);
-        goto fail;
     }
+#endif
 
 #if FF_API_STRUCT_VAAPI_CONTEXT
     if (ctx->have_old_context) {
@@ -546,34 +547,19 @@ int ff_vaapi_decode_init(AVCodecContext *avctx)
     } else {
 #endif
 
-    err = vaapi_decode_make_config(avctx);
-    if (err)
+    err = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VAAPI);
+    if (err < 0)
         goto fail;
 
-    if (!avctx->hw_frames_ctx) {
-        avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
-        if (!avctx->hw_frames_ctx) {
-            err = AVERROR(ENOMEM);
-            goto fail;
-        }
-        ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
-
-        ctx->frames->format = AV_PIX_FMT_VAAPI;
-        ctx->frames->width  = avctx->coded_width;
-        ctx->frames->height = avctx->coded_height;
-
-        ctx->frames->sw_format         = ctx->surface_format;
-        ctx->frames->initial_pool_size = ctx->surface_count;
+    ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
+    ctx->hwfc   = ctx->frames->hwctx;
+    ctx->device = ctx->frames->device_ctx;
+    ctx->hwctx  = ctx->device->hwctx;
 
-        err = av_hwframe_ctx_init(avctx->hw_frames_ctx);
-        if (err < 0) {
-            av_log(avctx, AV_LOG_ERROR, "Failed to initialise internal "
-                   "frames context: %d.\n", err);
-            goto fail;
-        }
-
-        ctx->hwfc = ctx->frames->hwctx;
-    }
+    err = vaapi_decode_make_config(avctx, ctx->frames->device_ref,
+                                   &ctx->va_config, avctx->hw_frames_ctx);
+    if (err)
+        goto fail;
 
     vas = vaCreateContext(ctx->hwctx->display, ctx->va_config,
                           avctx->coded_width, avctx->coded_height,
diff --git a/libavcodec/vaapi_decode.h b/libavcodec/vaapi_decode.h
index 550ee05432..1fcecac468 100644
--- a/libavcodec/vaapi_decode.h
+++ b/libavcodec/vaapi_decode.h
@@ -53,8 +53,6 @@ typedef struct VAAPIDecodePicture {
 } VAAPIDecodePicture;
 
 typedef struct VAAPIDecodeContext {
-    VAProfile             va_profile;
-    VAEntrypoint          va_entrypoint;
     VAConfigID            va_config;
     VAContextID           va_context;
 
@@ -98,4 +96,7 @@ int ff_vaapi_decode_cancel(AVCodecContext *avctx,
 int ff_vaapi_decode_init(AVCodecContext *avctx);
 int ff_vaapi_decode_uninit(AVCodecContext *avctx);
 
+int ff_vaapi_common_frame_params(AVCodecContext *avctx,
+                                 AVBufferRef *hw_frames_ctx);
+
 #endif /* AVCODEC_VAAPI_DECODE_H */
diff --git a/libavcodec/vaapi_h264.c b/libavcodec/vaapi_h264.c
index 30e7026ccf..c7f9d7bcb4 100644
--- a/libavcodec/vaapi_h264.c
+++ b/libavcodec/vaapi_h264.c
@@ -399,6 +399,7 @@ AVHWAccel ff_h264_vaapi_hwaccel = {
     .frame_priv_data_size = sizeof(VAAPIDecodePicture),
     .init                 = &ff_vaapi_decode_init,
     .uninit               = &ff_vaapi_decode_uninit,
+    .frame_params         = &ff_vaapi_common_frame_params,
     .priv_data_size       = sizeof(VAAPIDecodeContext),
     .caps_internal        = HWACCEL_CAP_ASYNC_SAFE,
 };
diff --git a/libavcodec/vaapi_hevc.c b/libavcodec/vaapi_hevc.c
index 69b8e478c6..718ccc92a2 100644
--- a/libavcodec/vaapi_hevc.c
+++ b/libavcodec/vaapi_hevc.c
@@ -434,6 +434,7 @@ AVHWAccel ff_hevc_vaapi_hwaccel = {
     .frame_priv_data_size = sizeof(VAAPIDecodePictureHEVC),
     .init                 = ff_vaapi_decode_init,
     .uninit               = ff_vaapi_decode_uninit,
+    .frame_params         = ff_vaapi_common_frame_params,
     .priv_data_size       = sizeof(VAAPIDecodeContext),
     .caps_internal        = HWACCEL_CAP_ASYNC_SAFE,
 };
diff --git a/libavcodec/vaapi_mpeg2.c b/libavcodec/vaapi_mpeg2.c
index 0d197c9692..fd5be8d8ae 100644
--- a/libavcodec/vaapi_mpeg2.c
+++ b/libavcodec/vaapi_mpeg2.c
@@ -183,6 +183,7 @@ AVHWAccel ff_mpeg2_vaapi_hwaccel = {
     .frame_priv_data_size = sizeof(VAAPIDecodePicture),
     .init                 = &ff_vaapi_decode_init,
     .uninit               = &ff_vaapi_decode_uninit,
+    .frame_params         = &ff_vaapi_common_frame_params,
     .priv_data_size       = sizeof(VAAPIDecodeContext),
     .caps_internal        = HWACCEL_CAP_ASYNC_SAFE,
 };
diff --git a/libavcodec/vaapi_mpeg4.c b/libavcodec/vaapi_mpeg4.c
index f8c5ddf209..3fe029186e 100644
--- a/libavcodec/vaapi_mpeg4.c
+++ b/libavcodec/vaapi_mpeg4.c
@@ -189,6 +189,7 @@ AVHWAccel ff_mpeg4_vaapi_hwaccel = {
     .frame_priv_data_size = sizeof(VAAPIDecodePicture),
     .init                 = &ff_vaapi_decode_init,
     .uninit               = &ff_vaapi_decode_uninit,
+    .frame_params         = &ff_vaapi_common_frame_params,
     .priv_data_size       = sizeof(VAAPIDecodeContext),
     .caps_internal        = HWACCEL_CAP_ASYNC_SAFE,
 };
@@ -206,6 +207,7 @@ AVHWAccel ff_h263_vaapi_hwaccel = {
     .frame_priv_data_size = sizeof(VAAPIDecodePicture),
     .init                 = &ff_vaapi_decode_init,
     .uninit               = &ff_vaapi_decode_uninit,
+    .frame_params         = &ff_vaapi_common_frame_params,
     .priv_data_size       = sizeof(VAAPIDecodeContext),
     .caps_internal        = HWACCEL_CAP_ASYNC_SAFE,
 };
diff --git a/libavcodec/vaapi_vc1.c b/libavcodec/vaapi_vc1.c
index 30c9ed3c8b..49675744cd 100644
--- a/libavcodec/vaapi_vc1.c
+++ b/libavcodec/vaapi_vc1.c
@@ -399,6 +399,7 @@ AVHWAccel ff_wmv3_vaapi_hwaccel = {
     .frame_priv_data_size = sizeof(VAAPIDecodePicture),
     .init                 = &ff_vaapi_decode_init,
     .uninit               = &ff_vaapi_decode_uninit,
+    .frame_params         = &ff_vaapi_common_frame_params,
     .priv_data_size       = sizeof(VAAPIDecodeContext),
     .caps_internal        = HWACCEL_CAP_ASYNC_SAFE,
 };
@@ -415,6 +416,7 @@ AVHWAccel ff_vc1_vaapi_hwaccel = {
     .frame_priv_data_size = sizeof(VAAPIDecodePicture),
     .init                 = &ff_vaapi_decode_init,
     .uninit               = &ff_vaapi_decode_uninit,
+    .frame_params         = &ff_vaapi_common_frame_params,
     .priv_data_size       = sizeof(VAAPIDecodeContext),
     .caps_internal        = HWACCEL_CAP_ASYNC_SAFE,
 };
diff --git a/libavcodec/vaapi_vp9.c b/libavcodec/vaapi_vp9.c
index d8ece75df4..f295dc9ebb 100644
--- a/libavcodec/vaapi_vp9.c
+++ b/libavcodec/vaapi_vp9.c
@@ -179,6 +179,7 @@ AVHWAccel ff_vp9_vaapi_hwaccel = {
     .frame_priv_data_size = sizeof(VAAPIDecodePicture),
     .init                 = ff_vaapi_decode_init,
     .uninit               = ff_vaapi_decode_uninit,
+    .frame_params         = ff_vaapi_common_frame_params,
     .priv_data_size       = sizeof(VAAPIDecodeContext),
     .caps_internal        = HWACCEL_CAP_ASYNC_SAFE,
 };
diff --git a/libavcodec/vdpau.c b/libavcodec/vdpau.c
index ee3610428f..1b2ec989cd 100644
--- a/libavcodec/vdpau.c
+++ b/libavcodec/vdpau.c
@@ -24,6 +24,7 @@
 #include <limits.h>
 
 #include "avcodec.h"
+#include "decode.h"
 #include "internal.h"
 #include "h264dec.h"
 #include "vc1.h"
@@ -110,6 +111,25 @@ int av_vdpau_get_surface_parameters(AVCodecContext *avctx,
     return 0;
 }
 
+int ff_vdpau_common_frame_params(AVCodecContext *avctx,
+                                 AVBufferRef *hw_frames_ctx)
+{
+    AVHWFramesContext *hw_frames = (AVHWFramesContext*)hw_frames_ctx->data;
+    VdpChromaType type;
+    uint32_t width;
+    uint32_t height;
+
+    if (av_vdpau_get_surface_parameters(avctx, &type, &width, &height))
+        return AVERROR(EINVAL);
+
+    hw_frames->format    = AV_PIX_FMT_VDPAU;
+    hw_frames->sw_format = avctx->sw_pix_fmt;
+    hw_frames->width     = width;
+    hw_frames->height    = height;
+
+    return 0;
+}
+
 int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
                          int level)
 {
@@ -127,6 +147,7 @@ int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
     VdpChromaType type;
     uint32_t width;
     uint32_t height;
+    int ret;
 
     vdctx->width            = UINT32_MAX;
     vdctx->height           = UINT32_MAX;
@@ -154,41 +175,14 @@ int ff_vdpau_common_init(AVCodecContext *avctx, VdpDecoderProfile profile,
             type != VDP_CHROMA_TYPE_420)
             return AVERROR(ENOSYS);
     } else {
-        AVHWFramesContext *frames_ctx = NULL;
+        AVHWFramesContext *frames_ctx;
         AVVDPAUDeviceContext *dev_ctx;
 
-        // We assume the hw_frames_ctx always survives until ff_vdpau_common_uninit
-        // is called. This holds true as the user is not allowed to touch
-        // hw_device_ctx, or hw_frames_ctx after get_format (and ff_get_format
-        // itself also uninits before unreffing hw_frames_ctx).
-        if (avctx->hw_frames_ctx) {
-            frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
-        } else if (avctx->hw_device_ctx) {
-            int ret;
-
-            avctx->hw_frames_ctx = av_hwframe_ctx_alloc(avctx->hw_device_ctx);
-            if (!avctx->hw_frames_ctx)
-                return AVERROR(ENOMEM);
-
-            frames_ctx            = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
-            frames_ctx->format    = AV_PIX_FMT_VDPAU;
-            frames_ctx->sw_format = avctx->sw_pix_fmt;
-            frames_ctx->width     = avctx->coded_width;
-            frames_ctx->height    = avctx->coded_height;
-
-            ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
-            if (ret < 0) {
-                av_buffer_unref(&avctx->hw_frames_ctx);
-                return ret;
-            }
-        }
-
-        if (!frames_ctx) {
-            av_log(avctx, AV_LOG_ERROR, "A hardware frames context is "
-                   "required for VDPAU decoding.\n");
-            return AVERROR(EINVAL);
-        }
+        ret = ff_decode_get_hw_frames_ctx(avctx, AV_HWDEVICE_TYPE_VDPAU);
+        if (ret < 0)
+            return ret;
 
+        frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
         dev_ctx = frames_ctx->device_ctx->hwctx;
 
         vdctx->device           = dev_ctx->device;
diff --git a/libavcodec/vdpau_h264.c b/libavcodec/vdpau_h264.c
index be6ba71433..70f83d76f5 100644
--- a/libavcodec/vdpau_h264.c
+++ b/libavcodec/vdpau_h264.c
@@ -273,6 +273,7 @@ AVHWAccel ff_h264_vdpau_hwaccel = {
     .frame_priv_data_size = sizeof(struct vdpau_picture_context),
     .init           = vdpau_h264_init,
     .uninit         = ff_vdpau_common_uninit,
+    .frame_params   = ff_vdpau_common_frame_params,
     .priv_data_size = sizeof(VDPAUContext),
     .caps_internal  = HWACCEL_CAP_ASYNC_SAFE,
 };
diff --git a/libavcodec/vdpau_hevc.c b/libavcodec/vdpau_hevc.c
index fcdf4b2747..70e76cfcaa 100644
--- a/libavcodec/vdpau_hevc.c
+++ b/libavcodec/vdpau_hevc.c
@@ -424,6 +424,7 @@ AVHWAccel ff_hevc_vdpau_hwaccel = {
     .frame_priv_data_size = sizeof(struct vdpau_picture_context),
     .init           = vdpau_hevc_init,
     .uninit         = ff_vdpau_common_uninit,
+    .frame_params   = ff_vdpau_common_frame_params,
     .priv_data_size = sizeof(VDPAUContext),
     .caps_internal  = HWACCEL_CAP_ASYNC_SAFE,
 };
diff --git a/libavcodec/vdpau_internal.h b/libavcodec/vdpau_internal.h
index 30d01af65d..4d63e50b16 100644
--- a/libavcodec/vdpau_internal.h
+++ b/libavcodec/vdpau_internal.h
@@ -119,5 +119,7 @@ int ff_vdpau_common_end_frame(AVCodecContext *avctx, AVFrame *frame,
 int ff_vdpau_mpeg_end_frame(AVCodecContext *avctx);
 int ff_vdpau_add_buffer(struct vdpau_picture_context *pic, const uint8_t *buf,
                         uint32_t buf_size);
+int ff_vdpau_common_frame_params(AVCodecContext *avctx,
+                                 AVBufferRef *hw_frames_ctx);
 
 #endif /* AVCODEC_VDPAU_INTERNAL_H */
diff --git a/libavcodec/vdpau_mpeg12.c b/libavcodec/vdpau_mpeg12.c
index b657007ee7..71fe889abf 100644
--- a/libavcodec/vdpau_mpeg12.c
+++ b/libavcodec/vdpau_mpeg12.c
@@ -149,6 +149,7 @@ AVHWAccel ff_mpeg2_vdpau_hwaccel = {
     .frame_priv_data_size = sizeof(struct vdpau_picture_context),
     .init           = vdpau_mpeg2_init,
     .uninit         = ff_vdpau_common_uninit,
+    .frame_params   = ff_vdpau_common_frame_params,
     .priv_data_size = sizeof(VDPAUContext),
     .caps_internal  = HWACCEL_CAP_ASYNC_SAFE,
 };
diff --git a/libavcodec/vdpau_mpeg4.c b/libavcodec/vdpau_mpeg4.c
index bbdd843a44..653ef7e7e7 100644
--- a/libavcodec/vdpau_mpeg4.c
+++ b/libavcodec/vdpau_mpeg4.c
@@ -121,6 +121,7 @@ AVHWAccel ff_mpeg4_vdpau_hwaccel = {
     .frame_priv_data_size = sizeof(struct vdpau_picture_context),
     .init           = vdpau_mpeg4_init,
     .uninit         = ff_vdpau_common_uninit,
+    .frame_params   = ff_vdpau_common_frame_params,
     .priv_data_size = sizeof(VDPAUContext),
     .caps_internal  = HWACCEL_CAP_ASYNC_SAFE,
 };
diff --git a/libavcodec/vdpau_vc1.c b/libavcodec/vdpau_vc1.c
index 665a2333f4..73d83f6577 100644
--- a/libavcodec/vdpau_vc1.c
+++ b/libavcodec/vdpau_vc1.c
@@ -147,6 +147,7 @@ AVHWAccel ff_wmv3_vdpau_hwaccel = {
     .frame_priv_data_size = sizeof(struct vdpau_picture_context),
     .init           = vdpau_vc1_init,
     .uninit         = ff_vdpau_common_uninit,
+    .frame_params   = ff_vdpau_common_frame_params,
     .priv_data_size = sizeof(VDPAUContext),
     .caps_internal  = HWACCEL_CAP_ASYNC_SAFE,
 };
@@ -163,6 +164,7 @@ AVHWAccel ff_vc1_vdpau_hwaccel = {
     .frame_priv_data_size = sizeof(struct vdpau_picture_context),
     .init           = vdpau_vc1_init,
     .uninit         = ff_vdpau_common_uninit,
+    .frame_params   = ff_vdpau_common_frame_params,
     .priv_data_size = sizeof(VDPAUContext),
     .caps_internal  = HWACCEL_CAP_ASYNC_SAFE,
 };
diff --git a/libavcodec/version.h b/libavcodec/version.h
index e7323764ec..9c17e5716d 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -28,7 +28,7 @@
 #include "libavutil/version.h"
 
 #define LIBAVCODEC_VERSION_MAJOR  58
-#define LIBAVCODEC_VERSION_MINOR   2
+#define LIBAVCODEC_VERSION_MINOR   3
 #define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \


======================================================================

diff --cc doc/APIchanges
index 1490d67f27,9f3a1f2465..d336f6ce22
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@@ -15,25 -13,10 +15,28 @@@ libavutil:     2017-10-2
  
  API changes, most recent first:
  
 -2017-xx-xx - xxxxxxx - lavc 58.5.0 - avcodec.h
++2017-xx-xx - xxxxxxx - lavc 58.3.100 - avcodec.h
+   Add avcodec_get_hw_frames_parameters().
+ 
 -2017-xx-xx - xxxxxxx - lavu 56.6.0 - pixdesc.h
 +-------- 8< --------- FFmpeg 3.4 was cut here -------- 8< ---------
 +
 +2017-09-28 - b6cf66ae1c - lavc 57.106.104 - avcodec.h
 +  Add AV_PKT_DATA_A53_CC packet side data, to export closed captions
 +
 +2017-09-27 - 7aa6b8a68f - lavu 55.77.101 / lavu 55.31.1 - frame.h
 +  Allow passing the value of 0 (meaning "automatic") as the required alignment
 +  to av_frame_get_buffer().
 +
 +2017-09-27 - 522f877086 - lavu 55.77.100 / lavu 55.31.0 - cpu.h
 +  Add av_cpu_max_align() for querying maximum required data alignment.
 +
 +2017-09-26 - b1cf151c4d - lavc 57.106.102 - avcodec.h
 +  Deprecate AVCodecContext.refcounted_frames. This was useful for deprecated
 +  API only (avcodec_decode_video2/avcodec_decode_audio4). The new decode APIs
 +  (avcodec_send_packet/avcodec_receive_frame) always work with reference
 +  counted frames.
 +
 +2017-09-21 - 6f15f1cdc8 - lavu 55.76.100 / 56.6.0 - pixdesc.h
    Add av_color_range_from_name(), av_color_primaries_from_name(),
    av_color_transfer_from_name(), av_color_space_from_name(), and
    av_chroma_location_from_name().
diff --cc libavcodec/avcodec.h
index 15ca871b59,5624835023..8c76c9f908
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@@ -3534,11 -2992,14 +3534,21 @@@ typedef struct AVHWAccel 
      int caps_internal;
  
      /**
+      * Fill the given hw_frames context with current codec parameters. Called
+      * from get_format. Refer to avcodec_get_hw_frames_parameters() for
+      * details.
+      *
+      * This CAN be called before AVHWAccel.init is called, and you must assume
+      * that avctx->hwaccel_priv_data is invalid.
+      */
+     int (*frame_params)(AVCodecContext *avctx, AVBufferRef *hw_frames_ctx);
++
++    /**
 +     * Some hwaccels are ambiguous if only the id and pix_fmt fields are used.
 +     * If non-NULL, the associated AVCodec must have
 +     * FF_CODEC_CAP_HWACCEL_REQUIRE_CLASS set.
 +     */
 +    const AVClass *decoder_class;
  } AVHWAccel;
  
  /**
diff --cc libavcodec/decode.c
index 7601d16082,54cda530bb..a7f1e23fc2
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@@ -1106,6 -669,88 +1106,86 @@@ static AVHWAccel *find_hwaccel(AVCodecC
      return NULL;
  }
  
+ int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
+                                 enum AVHWDeviceType dev_type)
+ {
+     AVHWDeviceContext *device_ctx;
+     AVHWFramesContext *frames_ctx;
+     int ret;
+ 
+     if (!avctx->hwaccel)
+         return AVERROR(ENOSYS);
+ 
+     if (avctx->hw_frames_ctx)
+         return 0;
+     if (!avctx->hw_device_ctx) {
+         av_log(avctx, AV_LOG_ERROR, "A hardware frames or device context is "
+                 "required for hardware accelerated decoding.\n");
+         return AVERROR(EINVAL);
+     }
+ 
+     device_ctx = (AVHWDeviceContext *)avctx->hw_device_ctx->data;
+     if (device_ctx->type != dev_type) {
+         av_log(avctx, AV_LOG_ERROR, "Device type %s expected for hardware "
+                "decoding, but got %s.\n", av_hwdevice_get_type_name(dev_type),
+                av_hwdevice_get_type_name(device_ctx->type));
+         return AVERROR(EINVAL);
+     }
+ 
+     ret = avcodec_get_hw_frames_parameters(avctx,
+                                            avctx->hw_device_ctx,
+                                            avctx->hwaccel->pix_fmt,
 -                                           avctx->hw_frames_ctx);
 -    if (ret < 0) {
 -        av_buffer_unref(&avctx->hw_frames_ctx);
++                                           &avctx->hw_frames_ctx);
++    if (ret < 0)
+         return ret;
 -    }
+ 
+     frames_ctx = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
+ 
+ 
+     if (frames_ctx->initial_pool_size) {
+         // We guarantee 4 base work surfaces. The function above guarantees 1
+         // (the absolute minimum), so add the missing count.
+         frames_ctx->initial_pool_size += 3;
+ 
+         // Add an additional surface per thread is frame threading is enabled.
+         if (avctx->active_thread_type & FF_THREAD_FRAME)
+             frames_ctx->initial_pool_size += avctx->thread_count;
+     }
+ 
+     ret = av_hwframe_ctx_init(avctx->hw_frames_ctx);
+     if (ret < 0) {
+         av_buffer_unref(&avctx->hw_frames_ctx);
+         return ret;
+     }
+ 
+     return 0;
+ }
+ 
+ int avcodec_get_hw_frames_parameters(AVCodecContext *avctx,
+                                      AVBufferRef *device_ref,
+                                      enum AVPixelFormat hw_pix_fmt,
+                                      AVBufferRef **out_frames_ref)
+ {
+     AVBufferRef *frames_ref = NULL;
 -    AVHWAccel *hwa = find_hwaccel(avctx->codec_id, hw_pix_fmt);
++    AVHWAccel *hwa = find_hwaccel(avctx, hw_pix_fmt);
+     int ret;
+ 
+     if (!hwa || !hwa->frame_params)
+         return AVERROR(ENOENT);
+ 
+     frames_ref = av_hwframe_ctx_alloc(device_ref);
+     if (!frames_ref)
+         return AVERROR(ENOMEM);
+ 
+     ret = hwa->frame_params(avctx, frames_ref);
+     if (ret >= 0) {
+         *out_frames_ref = frames_ref;
+     } else {
+         av_buffer_unref(&frames_ref);
+     }
+     return ret;
+ }
+ 
  static int setup_hwaccel(AVCodecContext *avctx,
                           const enum AVPixelFormat fmt,
                           const char *name)
diff --cc libavcodec/decode.h
index 03fc783bba,37b2e45c63..424e85e914
--- a/libavcodec/decode.h
+++ b/libavcodec/decode.h
@@@ -23,7 -23,8 +23,11 @@@
  
  #include "libavutil/buffer.h"
  #include "libavutil/frame.h"
 +
++#include "libavutil/buffer.h"
++#include "libavutil/frame.h"
+ #include "libavutil/hwcontext.h"
+ 
  #include "avcodec.h"
  
  /**
@@@ -65,6 -71,12 +69,14 @@@ int ff_decode_get_packet(AVCodecContex
  
  void ff_decode_bsfs_uninit(AVCodecContext *avctx);
  
+ /**
+  * Make sure avctx.hw_frames_ctx is set. If it's not set, the function will
+  * try to allocate it from hw_device_ctx. If that is not possible, an error
+  * message is printed, and an error code is returned.
+  */
+ int ff_decode_get_hw_frames_ctx(AVCodecContext *avctx,
+                                 enum AVHWDeviceType dev_type);
+ 
 +int ff_attach_decode_data(AVFrame *frame);
 +
  #endif /* AVCODEC_DECODE_H */
diff --cc libavcodec/dxva2_vp9.c
index 6d87fdd9f2,0000000000..a2e55f5736
mode 100644,000000..100644
--- a/libavcodec/dxva2_vp9.c
+++ b/libavcodec/dxva2_vp9.c
@@@ -1,357 -1,0 +1,360 @@@
 +/*
 + * DXVA2 VP9 HW acceleration.
 + *
 + * copyright (c) 2015 Hendrik Leppkes
 + *
 + * This file is part of FFmpeg.
 + *
 + * FFmpeg 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.
 + *
 + * FFmpeg 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 FFmpeg; if not, write to the Free Software
 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 + */
 +
 +#include "libavutil/avassert.h"
 +#include "libavutil/pixdesc.h"
 +
 +#include "vp9shared.h"
 +
 +// The headers above may include w32threads.h, which uses the original
 +// _WIN32_WINNT define, while dxva2_internal.h redefines it to target a
 +// potentially newer version.
 +#include "dxva2_internal.h"
 +
 +struct vp9_dxva2_picture_context {
 +    DXVA_PicParams_VP9    pp;
 +    DXVA_Slice_VPx_Short  slice;
 +    const uint8_t         *bitstream;
 +    unsigned              bitstream_size;
 +};
 +
 +static void fill_picture_entry(DXVA_PicEntry_VPx *pic,
 +                               unsigned index, unsigned flag)
 +{
 +    av_assert0((index & 0x7f) == index && (flag & 0x01) == flag);
 +    pic->bPicEntry = index | (flag << 7);
 +}
 +
 +static int fill_picture_parameters(const AVCodecContext *avctx, AVDXVAContext *ctx, const VP9SharedContext *h,
 +                                    DXVA_PicParams_VP9 *pp)
 +{
 +    int i;
 +    const AVPixFmtDescriptor * pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
 +
 +    if (!pixdesc)
 +        return -1;
 +
 +    memset(pp, 0, sizeof(*pp));
 +
 +    fill_picture_entry(&pp->CurrPic, ff_dxva2_get_surface_index(avctx, ctx, h->frames[CUR_FRAME].tf.f), 0);
 +
 +    pp->profile = h->h.profile;
 +    pp->wFormatAndPictureInfoFlags = ((h->h.keyframe == 0)   <<  0) |
 +                                     ((h->h.invisible == 0)  <<  1) |
 +                                     (h->h.errorres          <<  2) |
 +                                     (pixdesc->log2_chroma_w <<  3) | /* subsampling_x */
 +                                     (pixdesc->log2_chroma_h <<  4) | /* subsampling_y */
 +                                     (0                      <<  5) | /* extra_plane */
 +                                     (h->h.refreshctx        <<  6) |
 +                                     (h->h.parallelmode      <<  7) |
 +                                     (h->h.intraonly         <<  8) |
 +                                     (h->h.framectxid        <<  9) |
 +                                     (h->h.resetctx          << 11) |
 +                                     ((h->h.keyframe ? 0 : h->h.highprecisionmvs) << 13) |
 +                                     (0                      << 14);  /* ReservedFormatInfo2Bits */
 +
 +    pp->width  = avctx->width;
 +    pp->height = avctx->height;
 +    pp->BitDepthMinus8Luma   = pixdesc->comp[0].depth - 8;
 +    pp->BitDepthMinus8Chroma = pixdesc->comp[1].depth - 8;
 +    /* swap 0/1 to match the reference */
 +    pp->interp_filter = h->h.filtermode ^ (h->h.filtermode <= 1);
 +    pp->Reserved8Bits = 0;
 +
 +    for (i = 0; i < 8; i++) {
 +        if (h->refs[i].f->buf[0]) {
 +            fill_picture_entry(&pp->ref_frame_map[i], ff_dxva2_get_surface_index(avctx, ctx, h->refs[i].f), 0);
 +            pp->ref_frame_coded_width[i]  = h->refs[i].f->width;
 +            pp->ref_frame_coded_height[i] = h->refs[i].f->height;
 +        } else
 +            pp->ref_frame_map[i].bPicEntry = 0xFF;
 +    }
 +
 +    for (i = 0; i < 3; i++) {
 +        uint8_t refidx = h->h.refidx[i];
 +        if (h->refs[refidx].f->buf[0])
 +            fill_picture_entry(&pp->frame_refs[i], ff_dxva2_get_surface_index(avctx, ctx, h->refs[refidx].f), 0);
 +        else
 +            pp->frame_refs[i].bPicEntry = 0xFF;
 +
 +        pp->ref_frame_sign_bias[i + 1] = h->h.signbias[i];
 +    }
 +
 +    pp->filter_level    = h->h.filter.level;
 +    pp->sharpness_level = h->h.filter.sharpness;
 +
 +    pp->wControlInfoFlags = (h->h.lf_delta.enabled   << 0) |
 +                            (h->h.lf_delta.updated   << 1) |
 +                            (h->h.use_last_frame_mvs << 2) |
 +                            (0                       << 3);  /* ReservedControlInfo5Bits */
 +
 +    for (i = 0; i < 4; i++)
 +        pp->ref_deltas[i]  = h->h.lf_delta.ref[i];
 +
 +    for (i = 0; i < 2; i++)
 +        pp->mode_deltas[i]  = h->h.lf_delta.mode[i];
 +
 +    pp->base_qindex   = h->h.yac_qi;
 +    pp->y_dc_delta_q  = h->h.ydc_qdelta;
 +    pp->uv_dc_delta_q = h->h.uvdc_qdelta;
 +    pp->uv_ac_delta_q = h->h.uvac_qdelta;
 +
 +    /* segmentation data */
 +    pp->stVP9Segments.wSegmentInfoFlags = (h->h.segmentation.enabled       << 0) |
 +                                          (h->h.segmentation.update_map    << 1) |
 +                                          (h->h.segmentation.temporal      << 2) |
 +                                          (h->h.segmentation.absolute_vals << 3) |
 +                                          (0                               << 4);  /* ReservedSegmentFlags4Bits */
 +
 +    for (i = 0; i < 7; i++)
 +        pp->stVP9Segments.tree_probs[i] = h->h.segmentation.prob[i];
 +
 +    if (h->h.segmentation.temporal)
 +        for (i = 0; i < 3; i++)
 +            pp->stVP9Segments.pred_probs[i] = h->h.segmentation.pred_prob[i];
 +    else
 +        memset(pp->stVP9Segments.pred_probs, 255, sizeof(pp->stVP9Segments.pred_probs));
 +
 +    for (i = 0; i < 8; i++) {
 +        pp->stVP9Segments.feature_mask[i] = (h->h.segmentation.feat[i].q_enabled    << 0) |
 +                                            (h->h.segmentation.feat[i].lf_enabled   << 1) |
 +                                            (h->h.segmentation.feat[i].ref_enabled  << 2) |
 +                                            (h->h.segmentation.feat[i].skip_enabled << 3);
 +
 +        pp->stVP9Segments.feature_data[i][0] = h->h.segmentation.feat[i].q_val;
 +        pp->stVP9Segments.feature_data[i][1] = h->h.segmentation.feat[i].lf_val;
 +        pp->stVP9Segments.feature_data[i][2] = h->h.segmentation.feat[i].ref_val;
 +        pp->stVP9Segments.feature_data[i][3] = 0; /* no data for skip */
 +    }
 +
 +    pp->log2_tile_cols = h->h.tiling.log2_tile_cols;
 +    pp->log2_tile_rows = h->h.tiling.log2_tile_rows;
 +
 +    pp->uncompressed_header_size_byte_aligned = h->h.uncompressed_header_size;
 +    pp->first_partition_size = h->h.compressed_header_size;
 +
 +    pp->StatusReportFeedbackNumber = 1 + DXVA_CONTEXT_REPORT_ID(avctx, ctx)++;
 +    return 0;
 +}
 +
 +static void fill_slice_short(DXVA_Slice_VPx_Short *slice,
 +                             unsigned position, unsigned size)
 +{
 +    memset(slice, 0, sizeof(*slice));
 +    slice->BSNALunitDataLocation = position;
 +    slice->SliceBytesInBuffer    = size;
 +    slice->wBadSliceChopping     = 0;
 +}
 +
 +static int commit_bitstream_and_slice_buffer(AVCodecContext *avctx,
 +                                             DECODER_BUFFER_DESC *bs,
 +                                             DECODER_BUFFER_DESC *sc)
 +{
 +    const VP9SharedContext *h = avctx->priv_data;
 +    AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
 +    struct vp9_dxva2_picture_context *ctx_pic = h->frames[CUR_FRAME].hwaccel_picture_private;
 +    void     *dxva_data_ptr;
 +    uint8_t  *dxva_data;
 +    unsigned dxva_size;
 +    unsigned padding;
 +    unsigned type;
 +
 +#if CONFIG_D3D11VA
 +    if (ff_dxva2_is_d3d11(avctx)) {
 +        type = D3D11_VIDEO_DECODER_BUFFER_BITSTREAM;
 +        if (FAILED(ID3D11VideoContext_GetDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context,
 +                                                       D3D11VA_CONTEXT(ctx)->decoder,
 +                                                       type,
 +                                                       &dxva_size, &dxva_data_ptr)))
 +            return -1;
 +    }
 +#endif
 +#if CONFIG_DXVA2
 +    if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
 +        type = DXVA2_BitStreamDateBufferType;
 +        if (FAILED(IDirectXVideoDecoder_GetBuffer(DXVA2_CONTEXT(ctx)->decoder,
 +                                                  type,
 +                                                  &dxva_data_ptr, &dxva_size)))
 +            return -1;
 +    }
 +#endif
 +
 +    dxva_data = dxva_data_ptr;
 +
 +    if (ctx_pic->slice.SliceBytesInBuffer > dxva_size) {
 +        av_log(avctx, AV_LOG_ERROR, "Failed to build bitstream");
 +        return -1;
 +    }
 +
 +    memcpy(dxva_data, ctx_pic->bitstream, ctx_pic->slice.SliceBytesInBuffer);
 +
 +    padding = FFMIN(128 - ((ctx_pic->slice.SliceBytesInBuffer) & 127), dxva_size - ctx_pic->slice.SliceBytesInBuffer);
 +    if (padding > 0) {
 +        memset(dxva_data + ctx_pic->slice.SliceBytesInBuffer, 0, padding);
 +        ctx_pic->slice.SliceBytesInBuffer += padding;
 +    }
 +
 +#if CONFIG_D3D11VA
 +    if (ff_dxva2_is_d3d11(avctx))
 +        if (FAILED(ID3D11VideoContext_ReleaseDecoderBuffer(D3D11VA_CONTEXT(ctx)->video_context, D3D11VA_CONTEXT(ctx)->decoder, type)))
 +            return -1;
 +#endif
 +#if CONFIG_DXVA2
 +    if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD)
 +        if (FAILED(IDirectXVideoDecoder_ReleaseBuffer(DXVA2_CONTEXT(ctx)->decoder, type)))
 +            return -1;
 +#endif
 +
 +#if CONFIG_D3D11VA
 +    if (ff_dxva2_is_d3d11(avctx)) {
 +        D3D11_VIDEO_DECODER_BUFFER_DESC *dsc11 = bs;
 +        memset(dsc11, 0, sizeof(*dsc11));
 +        dsc11->BufferType           = type;
 +        dsc11->DataSize             = ctx_pic->slice.SliceBytesInBuffer;
 +        dsc11->NumMBsInBuffer       = 0;
 +
 +        type = D3D11_VIDEO_DECODER_BUFFER_SLICE_CONTROL;
 +    }
 +#endif
 +#if CONFIG_DXVA2
 +    if (avctx->pix_fmt == AV_PIX_FMT_DXVA2_VLD) {
 +        DXVA2_DecodeBufferDesc *dsc2 = bs;
 +        memset(dsc2, 0, sizeof(*dsc2));
 +        dsc2->CompressedBufferType = type;
 +        dsc2->DataSize             = ctx_pic->slice.SliceBytesInBuffer;
 +        dsc2->NumMBsInBuffer       = 0;
 +
 +        type = DXVA2_SliceControlBufferType;
 +    }
 +#endif
 +
 +    return ff_dxva2_commit_buffer(avctx, ctx, sc,
 +                                  type,
 +                                  &ctx_pic->slice, sizeof(ctx_pic->slice), 0);
 +}
 +
 +
 +static int dxva2_vp9_start_frame(AVCodecContext *avctx,
 +                                 av_unused const uint8_t *buffer,
 +                                 av_unused uint32_t size)
 +{
 +    const VP9SharedContext *h = avctx->priv_data;
 +    AVDXVAContext *ctx = DXVA_CONTEXT(avctx);
 +    struct vp9_dxva2_picture_context *ctx_pic = h->frames[CUR_FRAME].hwaccel_picture_private;
 +
 +    if (!DXVA_CONTEXT_VALID(avctx, ctx))
 +        return -1;
 +    av_assert0(ctx_pic);
 +
 +    /* Fill up DXVA_PicParams_VP9 */
 +    if (fill_picture_parameters(avctx, ctx, h, &ctx_pic->pp) < 0)
 +        return -1;
 +
 +    ctx_pic->bitstream_size = 0;
 +    ctx_pic->bitstream      = NULL;
 +    return 0;
 +}
 +
 +static int dxva2_vp9_decode_slice(AVCodecContext *avctx,
 +                                  const uint8_t *buffer,
 +                                  uint32_t size)
 +{
 +    const VP9SharedContext *h = avctx->priv_data;
 +    struct vp9_dxva2_picture_context *ctx_pic = h->frames[CUR_FRAME].hwaccel_picture_private;
 +    unsigned position;
 +
 +    if (!ctx_pic->bitstream)
 +        ctx_pic->bitstream = buffer;
 +    ctx_pic->bitstream_size += size;
 +
 +    position = buffer - ctx_pic->bitstream;
 +    fill_slice_short(&ctx_pic->slice, position, size);
 +
 +    return 0;
 +}
 +
 +static int dxva2_vp9_end_frame(AVCodecContext *avctx)
 +{
 +    VP9SharedContext *h = avctx->priv_data;
 +    struct vp9_dxva2_picture_context *ctx_pic = h->frames[CUR_FRAME].hwaccel_picture_private;
 +    int ret;
 +
 +    if (ctx_pic->bitstream_size <= 0)
 +        return -1;
 +
 +    ret = ff_dxva2_common_end_frame(avctx, h->frames[CUR_FRAME].tf.f,
 +                                    &ctx_pic->pp, sizeof(ctx_pic->pp),
 +                                    NULL, 0,
 +                                    commit_bitstream_and_slice_buffer);
 +    return ret;
 +}
 +
 +#if CONFIG_VP9_DXVA2_HWACCEL
 +AVHWAccel ff_vp9_dxva2_hwaccel = {
 +    .name           = "vp9_dxva2",
 +    .type           = AVMEDIA_TYPE_VIDEO,
 +    .id             = AV_CODEC_ID_VP9,
 +    .pix_fmt        = AV_PIX_FMT_DXVA2_VLD,
 +    .init           = ff_dxva2_decode_init,
 +    .uninit         = ff_dxva2_decode_uninit,
 +    .start_frame    = dxva2_vp9_start_frame,
 +    .decode_slice   = dxva2_vp9_decode_slice,
 +    .end_frame      = dxva2_vp9_end_frame,
++    .frame_params   = ff_dxva2_common_frame_params,
 +    .frame_priv_data_size = sizeof(struct vp9_dxva2_picture_context),
 +    .priv_data_size = sizeof(FFDXVASharedContext),
 +};
 +#endif
 +
 +#if CONFIG_VP9_D3D11VA_HWACCEL
 +AVHWAccel ff_vp9_d3d11va_hwaccel = {
 +    .name           = "vp9_d3d11va",
 +    .type           = AVMEDIA_TYPE_VIDEO,
 +    .id             = AV_CODEC_ID_VP9,
 +    .pix_fmt        = AV_PIX_FMT_D3D11VA_VLD,
 +    .init           = ff_dxva2_decode_init,
 +    .uninit         = ff_dxva2_decode_uninit,
 +    .start_frame    = dxva2_vp9_start_frame,
 +    .decode_slice   = dxva2_vp9_decode_slice,
 +    .end_frame      = dxva2_vp9_end_frame,
++    .frame_params   = ff_dxva2_common_frame_params,
 +    .frame_priv_data_size = sizeof(struct vp9_dxva2_picture_context),
 +    .priv_data_size = sizeof(FFDXVASharedContext),
 +};
 +#endif
 +
 +#if CONFIG_VP9_D3D11VA2_HWACCEL
 +AVHWAccel ff_vp9_d3d11va2_hwaccel = {
 +    .name           = "vp9_d3d11va2",
 +    .type           = AVMEDIA_TYPE_VIDEO,
 +    .id             = AV_CODEC_ID_VP9,
 +    .pix_fmt        = AV_PIX_FMT_D3D11,
 +    .init           = ff_dxva2_decode_init,
 +    .uninit         = ff_dxva2_decode_uninit,
 +    .start_frame    = dxva2_vp9_start_frame,
 +    .decode_slice   = dxva2_vp9_decode_slice,
 +    .end_frame      = dxva2_vp9_end_frame,
++    .frame_params   = ff_dxva2_common_frame_params,
 +    .frame_priv_data_size = sizeof(struct vp9_dxva2_picture_context),
 +    .priv_data_size = sizeof(FFDXVASharedContext),
 +};
 +#endif
diff --cc libavcodec/vaapi_decode.c
index 27ef33837c,5ae98d8fd7..d467bed874
--- a/libavcodec/vaapi_decode.c
+++ b/libavcodec/vaapi_decode.c
@@@ -505,38 -531,10 +534,10 @@@ int ff_vaapi_decode_init(AVCodecContex
          ctx->hwctx->driver_quirks =
              AV_VAAPI_DRIVER_QUIRK_RENDER_PARAM_BUFFERS;
  
-     } else
- #endif
-     if (avctx->hw_frames_ctx) {
-         // This structure has a shorter lifetime than the enclosing
-         // AVCodecContext, so we inherit the references from there
-         // and do not need to make separate ones.
- 
-         ctx->frames = (AVHWFramesContext*)avctx->hw_frames_ctx->data;
-         ctx->hwfc   = ctx->frames->hwctx;
-         ctx->device = ctx->frames->device_ctx;
-         ctx->hwctx  = ctx->device->hwctx;
- 
-     } else if (avctx->hw_device_ctx) {
-         ctx->device = (AVHWDeviceContext*)avctx->hw_device_ctx->data;
-         ctx->hwctx  = ctx->device->hwctx;
- 
-         if (ctx->device->type != AV_HWDEVICE_TYPE_VAAPI) {
-             av_log(avctx, AV_LOG_ERROR, "Device supplied for VAAPI "
-                    "decoding must be a VAAPI device (not %d).\n",
-                    ctx->device->type);
-             err = AVERROR(EINVAL);
-             goto fail;
-         }
- 
-     } else {
-         av_log(avctx, AV_LOG_ERROR, "A hardware device or frames context "
-                "is required for VAAPI decoding.\n");
-         err = AVERROR(EINVAL);
-         goto fail;
      }
+ #endif
  
 -#if FF_API_VAAPI_CONTEXT
 +#if FF_API_STRUCT_VAAPI_CONTEXT
      if (ctx->have_old_context) {
          ctx->va_config  = ctx->old_context->config_id;
          ctx->va_context = ctx->old_context->context_id;
diff --cc libavcodec/vaapi_vp9.c
index d8ece75df4,0000000000..f295dc9ebb
mode 100644,000000..100644
--- a/libavcodec/vaapi_vp9.c
+++ b/libavcodec/vaapi_vp9.c
@@@ -1,184 -1,0 +1,185 @@@
 +/*
 + * VP9 HW decode acceleration through VA API
 + *
 + * Copyright (C) 2015 Timo Rothenpieler <timo at rothenpieler.org>
 + *
 + * This file is part of FFmpeg.
 + *
 + * FFmpeg 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.
 + *
 + * FFmpeg 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 FFmpeg; if not, write to the Free Software
 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 + */
 +
 +#include "libavutil/pixdesc.h"
 +
 +#include "hwaccel.h"
 +#include "vaapi_decode.h"
 +#include "vp9shared.h"
 +
 +static VASurfaceID vaapi_vp9_surface_id(const VP9Frame *vf)
 +{
 +    if (vf)
 +        return ff_vaapi_get_surface_id(vf->tf.f);
 +    else
 +        return VA_INVALID_SURFACE;
 +}
 +
 +static int vaapi_vp9_start_frame(AVCodecContext          *avctx,
 +                                 av_unused const uint8_t *buffer,
 +                                 av_unused uint32_t       size)
 +{
 +    const VP9SharedContext *h = avctx->priv_data;
 +    VAAPIDecodePicture *pic = h->frames[CUR_FRAME].hwaccel_picture_private;
 +    VADecPictureParameterBufferVP9 pic_param;
 +    const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
 +    int err, i;
 +
 +    pic->output_surface = vaapi_vp9_surface_id(&h->frames[CUR_FRAME]);
 +
 +    pic_param = (VADecPictureParameterBufferVP9) {
 +        .frame_width                      = avctx->width,
 +        .frame_height                     = avctx->height,
 +
 +        .pic_fields.bits = {
 +            .subsampling_x                = pixdesc->log2_chroma_w,
 +            .subsampling_y                = pixdesc->log2_chroma_h,
 +            .frame_type                   = !h->h.keyframe,
 +            .show_frame                   = !h->h.invisible,
 +            .error_resilient_mode         = h->h.errorres,
 +            .intra_only                   = h->h.intraonly,
 +            .allow_high_precision_mv      = h->h.keyframe ? 0 : h->h.highprecisionmvs,
 +            .mcomp_filter_type            = h->h.filtermode ^ (h->h.filtermode <= 1),
 +            .frame_parallel_decoding_mode = h->h.parallelmode,
 +            .reset_frame_context          = h->h.resetctx,
 +            .refresh_frame_context        = h->h.refreshctx,
 +            .frame_context_idx            = h->h.framectxid,
 +
 +            .segmentation_enabled          = h->h.segmentation.enabled,
 +            .segmentation_temporal_update  = h->h.segmentation.temporal,
 +            .segmentation_update_map       = h->h.segmentation.update_map,
 +
 +            .last_ref_frame                = h->h.refidx[0],
 +            .last_ref_frame_sign_bias      = h->h.signbias[0],
 +            .golden_ref_frame              = h->h.refidx[1],
 +            .golden_ref_frame_sign_bias    = h->h.signbias[1],
 +            .alt_ref_frame                 = h->h.refidx[2],
 +            .alt_ref_frame_sign_bias       = h->h.signbias[2],
 +            .lossless_flag                 = h->h.lossless,
 +        },
 +
 +        .filter_level                      = h->h.filter.level,
 +        .sharpness_level                   = h->h.filter.sharpness,
 +        .log2_tile_rows                    = h->h.tiling.log2_tile_rows,
 +        .log2_tile_columns                 = h->h.tiling.log2_tile_cols,
 +
 +        .frame_header_length_in_bytes      = h->h.uncompressed_header_size,
 +        .first_partition_size              = h->h.compressed_header_size,
 +
 +        .profile                           = h->h.profile,
 +        .bit_depth                         = h->h.bpp,
 +    };
 +
 +    for (i = 0; i < 7; i++)
 +        pic_param.mb_segment_tree_probs[i] = h->h.segmentation.prob[i];
 +
 +    if (h->h.segmentation.temporal) {
 +        for (i = 0; i < 3; i++)
 +            pic_param.segment_pred_probs[i] = h->h.segmentation.pred_prob[i];
 +    } else {
 +        memset(pic_param.segment_pred_probs, 255, sizeof(pic_param.segment_pred_probs));
 +    }
 +
 +    for (i = 0; i < 8; i++) {
 +        if (h->refs[i].f->buf[0])
 +            pic_param.reference_frames[i] = ff_vaapi_get_surface_id(h->refs[i].f);
 +        else
 +            pic_param.reference_frames[i] = VA_INVALID_ID;
 +    }
 +
 +    err = ff_vaapi_decode_make_param_buffer(avctx, pic,
 +                                            VAPictureParameterBufferType,
 +                                            &pic_param, sizeof(pic_param));
 +    if (err < 0) {
 +        ff_vaapi_decode_cancel(avctx, pic);
 +        return err;
 +    }
 +
 +    return 0;
 +}
 +
 +static int vaapi_vp9_end_frame(AVCodecContext *avctx)
 +{
 +    const VP9SharedContext *h = avctx->priv_data;
 +    VAAPIDecodePicture *pic = h->frames[CUR_FRAME].hwaccel_picture_private;
 +
 +    return ff_vaapi_decode_issue(avctx, pic);
 +}
 +
 +static int vaapi_vp9_decode_slice(AVCodecContext *avctx,
 +                                  const uint8_t  *buffer,
 +                                  uint32_t        size)
 +{
 +    const VP9SharedContext *h = avctx->priv_data;
 +    VAAPIDecodePicture *pic = h->frames[CUR_FRAME].hwaccel_picture_private;
 +    VASliceParameterBufferVP9 slice_param;
 +    int err, i;
 +
 +    slice_param = (VASliceParameterBufferVP9) {
 +        .slice_data_size   = size,
 +        .slice_data_offset = 0,
 +        .slice_data_flag   = VA_SLICE_DATA_FLAG_ALL,
 +    };
 +
 +    for (i = 0; i < 8; i++) {
 +        slice_param.seg_param[i] = (VASegmentParameterVP9) {
 +            .segment_flags.fields = {
 +                .segment_reference_enabled = h->h.segmentation.feat[i].ref_enabled,
 +                .segment_reference         = h->h.segmentation.feat[i].ref_val,
 +                .segment_reference_skipped = h->h.segmentation.feat[i].skip_enabled,
 +            },
 +
 +            .luma_dc_quant_scale           = h->h.segmentation.feat[i].qmul[0][0],
 +            .luma_ac_quant_scale           = h->h.segmentation.feat[i].qmul[0][1],
 +            .chroma_dc_quant_scale         = h->h.segmentation.feat[i].qmul[1][0],
 +            .chroma_ac_quant_scale         = h->h.segmentation.feat[i].qmul[1][1],
 +        };
 +
 +        memcpy(slice_param.seg_param[i].filter_level, h->h.segmentation.feat[i].lflvl, sizeof(slice_param.seg_param[i].filter_level));
 +    }
 +
 +    err = ff_vaapi_decode_make_slice_buffer(avctx, pic,
 +                                            &slice_param, sizeof(slice_param),
 +                                            buffer, size);
 +    if (err) {
 +        ff_vaapi_decode_cancel(avctx, pic);
 +        return err;
 +    }
 +
 +    return 0;
 +}
 +
 +AVHWAccel ff_vp9_vaapi_hwaccel = {
 +    .name                 = "vp9_vaapi",
 +    .type                 = AVMEDIA_TYPE_VIDEO,
 +    .id                   = AV_CODEC_ID_VP9,
 +    .pix_fmt              = AV_PIX_FMT_VAAPI,
 +    .start_frame          = vaapi_vp9_start_frame,
 +    .end_frame            = vaapi_vp9_end_frame,
 +    .decode_slice         = vaapi_vp9_decode_slice,
 +    .frame_priv_data_size = sizeof(VAAPIDecodePicture),
 +    .init                 = ff_vaapi_decode_init,
 +    .uninit               = ff_vaapi_decode_uninit,
++    .frame_params         = ff_vaapi_common_frame_params,
 +    .priv_data_size       = sizeof(VAAPIDecodeContext),
 +    .caps_internal        = HWACCEL_CAP_ASYNC_SAFE,
 +};
diff --cc libavcodec/version.h
index e7323764ec,aa6cdcd6bc..9c17e5716d
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@@ -27,9 -27,9 +27,9 @@@
  
  #include "libavutil/version.h"
  
 -#define LIBAVCODEC_VERSION_MAJOR 58
 -#define LIBAVCODEC_VERSION_MINOR  5
 -#define LIBAVCODEC_VERSION_MICRO  0
 +#define LIBAVCODEC_VERSION_MAJOR  58
- #define LIBAVCODEC_VERSION_MINOR   2
++#define LIBAVCODEC_VERSION_MINOR   3
 +#define LIBAVCODEC_VERSION_MICRO 100
  
  #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                 LIBAVCODEC_VERSION_MINOR, \



More information about the ffmpeg-cvslog mailing list