diff mbox series

[FFmpeg-devel,v2,3/4] libavcodec/qsv: enabling d3d11va support, added mfxhdlpair

Message ID 20200415130741.27263-3-artem.galin@gmail.com
State Superseded
Headers show
Series [FFmpeg-devel,v2,1/4] fftools/qsv: enabling d3d11va/dxva2 device selection | expand

Checks

Context Check Description
andriy/default pending
andriy/make success Make finished
andriy/make_fate success Make fate finished

Commit Message

galinart April 15, 2020, 1:07 p.m. UTC
From: Artem Galin <artem.galin@intel.com>

Adding DX11 relevant device type checks and adjusting callbacks with
proper MediaSDK pair type support.

Extending structure for proper MediaSDK pair type support.

Signed-off-by: Artem Galin <artem.galin@intel.com>
---
 libavcodec/qsv.c          | 52 ++++++++++++++++++++++++++++++++++++---
 libavcodec/qsv_internal.h |  1 +
 2 files changed, 49 insertions(+), 4 deletions(-)

Comments

Steve Lhomme April 21, 2020, 10:25 a.m. UTC | #1
Mostly the same remarks as for 2/4 regarding the uneeded loop and 
uninitialized handle_type, plus comments below.

On 2020-04-15 15:07, artem.galin@gmail.com wrote:
> From: Artem Galin <artem.galin@intel.com>
> 
> Adding DX11 relevant device type checks and adjusting callbacks with
> proper MediaSDK pair type support.
> 
> Extending structure for proper MediaSDK pair type support.
> 
> Signed-off-by: Artem Galin <artem.galin@intel.com>
> ---
>   libavcodec/qsv.c          | 52 ++++++++++++++++++++++++++++++++++++---
>   libavcodec/qsv_internal.h |  1 +
>   2 files changed, 49 insertions(+), 4 deletions(-)
> 
> diff --git a/libavcodec/qsv.c b/libavcodec/qsv.c
> index db98c75073..ceef1b7fd9 100644
> --- a/libavcodec/qsv.c
> +++ b/libavcodec/qsv.c
> @@ -36,6 +36,8 @@
>   #include "avcodec.h"
>   #include "qsv_internal.h"
>   
> +#define MFX_IMPL_VIA_MASK(impl) (0x0f00 & (impl))
> +
>   #if QSV_VERSION_ATLEAST(1, 12)
>   #include "mfx/mfxvp8.h"
>   #endif
> @@ -221,8 +223,15 @@ int ff_qsv_find_surface_idx(QSVFramesContext *ctx, QSVFrame *frame)
>       int i;
>       for (i = 0; i < ctx->nb_mids; i++) {
>           QSVMid *mid = &ctx->mids[i];
> +#if CONFIG_VAAPI
>           if (mid->handle == frame->surface.Data.MemId)
>               return i;
> +#else
> +        mfxHDLPair *pair = (mfxHDLPair*)frame->surface.Data.MemId;
> +        if ((mid->handle_pair.first == pair->first) &&
> +            (mid->handle_pair.second == pair->second))
> +            return i;
> +#endif
>       }
>       return AVERROR_BUG;
>   }
> @@ -362,7 +371,11 @@ static int ff_qsv_set_display_handle(AVCodecContext *avctx, QSVSession *qs)
>   int ff_qsv_init_internal_session(AVCodecContext *avctx, QSVSession *qs,
>                                    const char *load_plugins, int gpu_copy)
>   {
> +#ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
>       mfxIMPL          impl = MFX_IMPL_AUTO_ANY;
> +#else
> +    mfxIMPL          impl = MFX_IMPL_AUTO_ANY | MFX_IMPL_VIA_D3D11;

Why is this needed ? If D3D11 is available it should pick it, no ? Does 
it mean you can favor D3D11 over D3D9 ?

> +#endif
>       mfxVersion        ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
>       mfxInitParam init_par = { MFX_IMPL_AUTO_ANY };
>   
> @@ -449,11 +462,19 @@ static AVBufferRef *qsv_create_mids(AVBufferRef *hw_frames_ref)
>           return NULL;
>       }
>   
> +#if CONFIG_VAAPI
>       for (i = 0; i < nb_surfaces; i++) {
>           QSVMid *mid = &mids[i];
>           mid->handle        = frames_hwctx->surfaces[i].Data.MemId;
>           mid->hw_frames_ref = hw_frames_ref1;
>       }
> +#else
> +    for (i = 0; i < nb_surfaces; i++) {
> +        QSVMid *mid = &mids[i];
> +        mid->handle_pair   = *((mfxHDLPair*)frames_hwctx->surfaces[i].Data.MemId);
> +        mid->hw_frames_ref = hw_frames_ref1;
> +    }
> +#endif
>   
>       return mids_buf;
>   }
> @@ -628,7 +649,11 @@ static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
>           goto fail;
>   
>       qsv_mid->surf.Info = hw_frames_hwctx->surfaces[0].Info;
> +#if CONFIG_VAAPI
>       qsv_mid->surf.Data.MemId = qsv_mid->handle;
> +#else
> +    qsv_mid->surf.Data.MemId = &qsv_mid->handle_pair;
> +#endif
>   
>       /* map the data to the system memory */
>       ret = av_hwframe_map(qsv_mid->locked_frame, qsv_mid->hw_frame,
> @@ -661,7 +686,17 @@ static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
>   static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
>   {
>       QSVMid *qsv_mid = (QSVMid*)mid;
> +#if CONFIG_VAAPI
>       *hdl = qsv_mid->handle;
> +#else
> +    mfxHDLPair *pair_dst = (mfxHDLPair*)hdl;
> +    mfxHDLPair *pair_src = (mfxHDLPair*)&qsv_mid->handle_pair;
> +
> +    pair_dst->first = pair_src->first;
> +
> +    if (pair_src->second != (mfxMemId)MFX_INFINITE)
> +        pair_dst->second = pair_src->second;
> +#endif
>       return MFX_ERR_NONE;
>   }
>   
> @@ -695,11 +730,20 @@ int ff_qsv_init_session_device(AVCodecContext *avctx, mfxSession *psession,
>           return ff_qsv_print_error(avctx, err,
>                                     "Error querying the session attributes");
>   
> +    if (MFX_IMPL_VIA_D3D11 == MFX_IMPL_VIA_MASK(impl)) {
> +        handle_type = MFX_HANDLE_D3D11_DEVICE;
> +    } else if (MFX_IMPL_VIA_D3D9 == MFX_IMPL_VIA_MASK(impl)) {
> +        handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
> +    } else if (MFX_IMPL_VIA_VAAPI == MFX_IMPL_VIA_MASK(impl)) {
> +        handle_type = MFX_HANDLE_VA_DISPLAY;
> +    }
> +
>       for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
> -        err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
> -        if (err == MFX_ERR_NONE) {
> -            handle_type = handle_types[i];
> -            break;
> +        if (handle_types[i] == handle_type) {
> +            err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
> +            if (err == MFX_ERR_NONE) {
> +                break;
> +            }
>           }
>           handle = NULL;
>       }
> diff --git a/libavcodec/qsv_internal.h b/libavcodec/qsv_internal.h
> index 6489836a67..7a4a66e9d6 100644
> --- a/libavcodec/qsv_internal.h
> +++ b/libavcodec/qsv_internal.h
> @@ -61,6 +61,7 @@
>   typedef struct QSVMid {
>       AVBufferRef *hw_frames_ref;
>       mfxHDL handle;
> +    mfxHDLPair handle_pair;
>   
>       AVFrame *locked_frame;
>       AVFrame *hw_frame;
> -- 
> 2.26.0
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
>
galinart April 22, 2020, 6:29 p.m. UTC | #2
On Tue, 21 Apr 2020 at 11:26, Steve Lhomme <robux4@ycbcr.xyz> wrote:

> Mostly the same remarks as for 2/4 regarding the uneeded loop and
> uninitialized handle_type, plus comments below.
>
> Answers on the same remarks in 2/4

> On 2020-04-15 15:07, artem.galin@gmail.com wrote:
> > From: Artem Galin <artem.galin@intel.com>
> >
> > Adding DX11 relevant device type checks and adjusting callbacks with
> > proper MediaSDK pair type support.
> >
> > Extending structure for proper MediaSDK pair type support.
> >
> > Signed-off-by: Artem Galin <artem.galin@intel.com>
> > ---
> >   libavcodec/qsv.c          | 52 ++++++++++++++++++++++++++++++++++++---
> >   libavcodec/qsv_internal.h |  1 +
> >   2 files changed, 49 insertions(+), 4 deletions(-)
> >
> > diff --git a/libavcodec/qsv.c b/libavcodec/qsv.c
> > index db98c75073..ceef1b7fd9 100644
> > --- a/libavcodec/qsv.c
> > +++ b/libavcodec/qsv.c
> > @@ -36,6 +36,8 @@
> >   #include "avcodec.h"
> >   #include "qsv_internal.h"
> >
> > +#define MFX_IMPL_VIA_MASK(impl) (0x0f00 & (impl))
> > +
> >   #if QSV_VERSION_ATLEAST(1, 12)
> >   #include "mfx/mfxvp8.h"
> >   #endif
> > @@ -221,8 +223,15 @@ int ff_qsv_find_surface_idx(QSVFramesContext *ctx,
> QSVFrame *frame)
> >       int i;
> >       for (i = 0; i < ctx->nb_mids; i++) {
> >           QSVMid *mid = &ctx->mids[i];
> > +#if CONFIG_VAAPI
> >           if (mid->handle == frame->surface.Data.MemId)
> >               return i;
> > +#else
> > +        mfxHDLPair *pair = (mfxHDLPair*)frame->surface.Data.MemId;
> > +        if ((mid->handle_pair.first == pair->first) &&
> > +            (mid->handle_pair.second == pair->second))
> > +            return i;
> > +#endif
> >       }
> >       return AVERROR_BUG;
> >   }
> > @@ -362,7 +371,11 @@ static int ff_qsv_set_display_handle(AVCodecContext
> *avctx, QSVSession *qs)
> >   int ff_qsv_init_internal_session(AVCodecContext *avctx, QSVSession *qs,
> >                                    const char *load_plugins, int
> gpu_copy)
> >   {
> > +#ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
> >       mfxIMPL          impl = MFX_IMPL_AUTO_ANY;
> > +#else
> > +    mfxIMPL          impl = MFX_IMPL_AUTO_ANY | MFX_IMPL_VIA_D3D11;
>
> Why is this needed ? If D3D11 is available it should pick it, no ?

I can make it only if CONFIG_D3D11VA  is available.

> Does
> it mean you can favor D3D11 over D3D9 ?
>
Yes, we do favor D3D11 over D3D9
For DX11 it is essential to add this flag.
https://github.com/Intel-Media-SDK/MediaSDK/blob/master/samples/sample_multi_transcode/src/transcode_utils.cpp#L1586


> > +#endif
> >       mfxVersion        ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR }
> };
> >       mfxInitParam init_par = { MFX_IMPL_AUTO_ANY };
> >
> > @@ -449,11 +462,19 @@ static AVBufferRef *qsv_create_mids(AVBufferRef
> *hw_frames_ref)
> >           return NULL;
> >       }
> >
> > +#if CONFIG_VAAPI
> >       for (i = 0; i < nb_surfaces; i++) {
> >           QSVMid *mid = &mids[i];
> >           mid->handle        = frames_hwctx->surfaces[i].Data.MemId;
> >           mid->hw_frames_ref = hw_frames_ref1;
> >       }
> > +#else
> > +    for (i = 0; i < nb_surfaces; i++) {
> > +        QSVMid *mid = &mids[i];
> > +        mid->handle_pair   =
> *((mfxHDLPair*)frames_hwctx->surfaces[i].Data.MemId);
> > +        mid->hw_frames_ref = hw_frames_ref1;
> > +    }
> > +#endif
> >
> >       return mids_buf;
> >   }
> > @@ -628,7 +649,11 @@ static mfxStatus qsv_frame_lock(mfxHDL pthis,
> mfxMemId mid, mfxFrameData *ptr)
> >           goto fail;
> >
> >       qsv_mid->surf.Info = hw_frames_hwctx->surfaces[0].Info;
> > +#if CONFIG_VAAPI
> >       qsv_mid->surf.Data.MemId = qsv_mid->handle;
> > +#else
> > +    qsv_mid->surf.Data.MemId = &qsv_mid->handle_pair;
> > +#endif
> >
> >       /* map the data to the system memory */
> >       ret = av_hwframe_map(qsv_mid->locked_frame, qsv_mid->hw_frame,
> > @@ -661,7 +686,17 @@ static mfxStatus qsv_frame_unlock(mfxHDL pthis,
> mfxMemId mid, mfxFrameData *ptr)
> >   static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL
> *hdl)
> >   {
> >       QSVMid *qsv_mid = (QSVMid*)mid;
> > +#if CONFIG_VAAPI
> >       *hdl = qsv_mid->handle;
> > +#else
> > +    mfxHDLPair *pair_dst = (mfxHDLPair*)hdl;
> > +    mfxHDLPair *pair_src = (mfxHDLPair*)&qsv_mid->handle_pair;
> > +
> > +    pair_dst->first = pair_src->first;
> > +
> > +    if (pair_src->second != (mfxMemId)MFX_INFINITE)
> > +        pair_dst->second = pair_src->second;
> > +#endif
> >       return MFX_ERR_NONE;
> >   }
> >
> > @@ -695,11 +730,20 @@ int ff_qsv_init_session_device(AVCodecContext
> *avctx, mfxSession *psession,
> >           return ff_qsv_print_error(avctx, err,
> >                                     "Error querying the session
> attributes");
> >
> > +    if (MFX_IMPL_VIA_D3D11 == MFX_IMPL_VIA_MASK(impl)) {
> > +        handle_type = MFX_HANDLE_D3D11_DEVICE;
> > +    } else if (MFX_IMPL_VIA_D3D9 == MFX_IMPL_VIA_MASK(impl)) {
> > +        handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
> > +    } else if (MFX_IMPL_VIA_VAAPI == MFX_IMPL_VIA_MASK(impl)) {
> > +        handle_type = MFX_HANDLE_VA_DISPLAY;
> > +    }
> > +
> >       for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
> > -        err = MFXVideoCORE_GetHandle(parent_session, handle_types[i],
> &handle);
> > -        if (err == MFX_ERR_NONE) {
> > -            handle_type = handle_types[i];
> > -            break;
> > +        if (handle_types[i] == handle_type) {
> > +            err = MFXVideoCORE_GetHandle(parent_session,
> handle_types[i], &handle);
> > +            if (err == MFX_ERR_NONE) {
> > +                break;
> > +            }
> >           }
> >           handle = NULL;
> >       }
> > diff --git a/libavcodec/qsv_internal.h b/libavcodec/qsv_internal.h
> > index 6489836a67..7a4a66e9d6 100644
> > --- a/libavcodec/qsv_internal.h
> > +++ b/libavcodec/qsv_internal.h
> > @@ -61,6 +61,7 @@
> >   typedef struct QSVMid {
> >       AVBufferRef *hw_frames_ref;
> >       mfxHDL handle;
> > +    mfxHDLPair handle_pair;
> >
> >       AVFrame *locked_frame;
> >       AVFrame *hw_frame;
> > --
> > 2.26.0
> >
> > _______________________________________________
> > ffmpeg-devel mailing list
> > ffmpeg-devel@ffmpeg.org
> > https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> >
> > To unsubscribe, visit link above, or email
> > ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
> >
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
diff mbox series

Patch

diff --git a/libavcodec/qsv.c b/libavcodec/qsv.c
index db98c75073..ceef1b7fd9 100644
--- a/libavcodec/qsv.c
+++ b/libavcodec/qsv.c
@@ -36,6 +36,8 @@ 
 #include "avcodec.h"
 #include "qsv_internal.h"
 
+#define MFX_IMPL_VIA_MASK(impl) (0x0f00 & (impl))
+
 #if QSV_VERSION_ATLEAST(1, 12)
 #include "mfx/mfxvp8.h"
 #endif
@@ -221,8 +223,15 @@  int ff_qsv_find_surface_idx(QSVFramesContext *ctx, QSVFrame *frame)
     int i;
     for (i = 0; i < ctx->nb_mids; i++) {
         QSVMid *mid = &ctx->mids[i];
+#if CONFIG_VAAPI
         if (mid->handle == frame->surface.Data.MemId)
             return i;
+#else
+        mfxHDLPair *pair = (mfxHDLPair*)frame->surface.Data.MemId;
+        if ((mid->handle_pair.first == pair->first) &&
+            (mid->handle_pair.second == pair->second))
+            return i;
+#endif
     }
     return AVERROR_BUG;
 }
@@ -362,7 +371,11 @@  static int ff_qsv_set_display_handle(AVCodecContext *avctx, QSVSession *qs)
 int ff_qsv_init_internal_session(AVCodecContext *avctx, QSVSession *qs,
                                  const char *load_plugins, int gpu_copy)
 {
+#ifdef AVCODEC_QSV_LINUX_SESSION_HANDLE
     mfxIMPL          impl = MFX_IMPL_AUTO_ANY;
+#else
+    mfxIMPL          impl = MFX_IMPL_AUTO_ANY | MFX_IMPL_VIA_D3D11;
+#endif
     mfxVersion        ver = { { QSV_VERSION_MINOR, QSV_VERSION_MAJOR } };
     mfxInitParam init_par = { MFX_IMPL_AUTO_ANY };
 
@@ -449,11 +462,19 @@  static AVBufferRef *qsv_create_mids(AVBufferRef *hw_frames_ref)
         return NULL;
     }
 
+#if CONFIG_VAAPI
     for (i = 0; i < nb_surfaces; i++) {
         QSVMid *mid = &mids[i];
         mid->handle        = frames_hwctx->surfaces[i].Data.MemId;
         mid->hw_frames_ref = hw_frames_ref1;
     }
+#else
+    for (i = 0; i < nb_surfaces; i++) {
+        QSVMid *mid = &mids[i];
+        mid->handle_pair   = *((mfxHDLPair*)frames_hwctx->surfaces[i].Data.MemId);
+        mid->hw_frames_ref = hw_frames_ref1;
+    }
+#endif
 
     return mids_buf;
 }
@@ -628,7 +649,11 @@  static mfxStatus qsv_frame_lock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
         goto fail;
 
     qsv_mid->surf.Info = hw_frames_hwctx->surfaces[0].Info;
+#if CONFIG_VAAPI
     qsv_mid->surf.Data.MemId = qsv_mid->handle;
+#else
+    qsv_mid->surf.Data.MemId = &qsv_mid->handle_pair;
+#endif
 
     /* map the data to the system memory */
     ret = av_hwframe_map(qsv_mid->locked_frame, qsv_mid->hw_frame,
@@ -661,7 +686,17 @@  static mfxStatus qsv_frame_unlock(mfxHDL pthis, mfxMemId mid, mfxFrameData *ptr)
 static mfxStatus qsv_frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
 {
     QSVMid *qsv_mid = (QSVMid*)mid;
+#if CONFIG_VAAPI
     *hdl = qsv_mid->handle;
+#else
+    mfxHDLPair *pair_dst = (mfxHDLPair*)hdl;
+    mfxHDLPair *pair_src = (mfxHDLPair*)&qsv_mid->handle_pair;
+
+    pair_dst->first = pair_src->first;
+
+    if (pair_src->second != (mfxMemId)MFX_INFINITE)
+        pair_dst->second = pair_src->second;
+#endif
     return MFX_ERR_NONE;
 }
 
@@ -695,11 +730,20 @@  int ff_qsv_init_session_device(AVCodecContext *avctx, mfxSession *psession,
         return ff_qsv_print_error(avctx, err,
                                   "Error querying the session attributes");
 
+    if (MFX_IMPL_VIA_D3D11 == MFX_IMPL_VIA_MASK(impl)) {
+        handle_type = MFX_HANDLE_D3D11_DEVICE;
+    } else if (MFX_IMPL_VIA_D3D9 == MFX_IMPL_VIA_MASK(impl)) {
+        handle_type = MFX_HANDLE_D3D9_DEVICE_MANAGER;
+    } else if (MFX_IMPL_VIA_VAAPI == MFX_IMPL_VIA_MASK(impl)) {
+        handle_type = MFX_HANDLE_VA_DISPLAY;
+    }
+
     for (i = 0; i < FF_ARRAY_ELEMS(handle_types); i++) {
-        err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
-        if (err == MFX_ERR_NONE) {
-            handle_type = handle_types[i];
-            break;
+        if (handle_types[i] == handle_type) {
+            err = MFXVideoCORE_GetHandle(parent_session, handle_types[i], &handle);
+            if (err == MFX_ERR_NONE) {
+                break;
+            }
         }
         handle = NULL;
     }
diff --git a/libavcodec/qsv_internal.h b/libavcodec/qsv_internal.h
index 6489836a67..7a4a66e9d6 100644
--- a/libavcodec/qsv_internal.h
+++ b/libavcodec/qsv_internal.h
@@ -61,6 +61,7 @@ 
 typedef struct QSVMid {
     AVBufferRef *hw_frames_ref;
     mfxHDL handle;
+    mfxHDLPair handle_pair;
 
     AVFrame *locked_frame;
     AVFrame *hw_frame;