 libavcodec/qsv.c | 67 +++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 39 insertions(+), 28 deletions(-)

diff --git sources/libavcodec/qsv.c sources/libavcodec/qsv.c
index 7563625627..6bd37f98b6 100644
--- sources/libavcodec/qsv.c
+++ sources/libavcodec/qsv.c
@@ -487,6 +487,42 @@ static int ff_qsv_set_display_handle(AVCodecContext *avctx, QSVSession *qs)
 }
 #endif //AVCODEC_QSV_LINUX_SESSION_HANDLE
 
+static int qsv_create_mfx_session_legacy(AVCodecContext *avctx,
+                                         mfxIMPL implementation,
+                                         mfxVersion *pver,
+                                         int gpu_copy,
+                                         mfxSession *psession)
+{
+    mfxInitParam init_par = {MFX_IMPL_AUTO_ANY};
+    mfxSession session = NULL;
+    mfxStatus sts;
+
+    av_log(avctx, AV_LOG_VERBOSE,
+           "Use Intel(R) Media SDK to create MFX session, the required "
+           "implementation version is %d.%d\n",
+           pver->Major, pver->Minor);
+
+    *psession = NULL;
+
+    init_par.GPUCopy = gpu_copy;
+    init_par.Implementation = implementation;
+    init_par.Version = *pver;
+    sts = MFXInitEx(init_par, &session);
+    if (sts < 0)
+        return ff_qsv_print_error(avctx, sts,
+                                  "Error initializing a MFX session");
+    else if (sts > 0)
+    {
+        ff_qsv_print_warning(avctx, sts,
+                             "Warning in MFX initialization");
+        return AVERROR_UNKNOWN;
+    }
+
+    *psession = session;
+
+    return 0;
+}
+
 #if QSV_ONEVPL
 static int qsv_new_mfx_loader(AVCodecContext *avctx,
                               mfxIMPL implementation,
@@ -618,7 +654,8 @@ static int qsv_create_mfx_session(AVCodecContext *avctx,
         loader = *ploader;
     }
 
-    if (qsv_create_mfx_session_from_loader(avctx, loader, psession))
+    if (qsv_create_mfx_session_from_loader(avctx, loader, psession) &&
+        qsv_create_mfx_session_legacy(avctx, implementation, pver, gpu_copy, psession))
         goto fail;
 
     if (!*ploader)
@@ -642,34 +679,8 @@ static int qsv_create_mfx_session(AVCodecContext *avctx,
                                   mfxSession *psession,
                                   void **ploader)
 {
-    mfxInitParam init_par = { MFX_IMPL_AUTO_ANY };
-    mfxSession session = NULL;
-    mfxStatus sts;
-
-    av_log(avctx, AV_LOG_VERBOSE,
-           "Use Intel(R) Media SDK to create MFX session, the required "
-           "implementation version is %d.%d\n",
-           pver->Major, pver->Minor);
-
-    *psession = NULL;
     *ploader = NULL;
-
-    init_par.GPUCopy = gpu_copy;
-    init_par.Implementation = implementation;
-    init_par.Version = *pver;
-    sts = MFXInitEx(init_par, &session);
-    if (sts < 0)
-        return ff_qsv_print_error(avctx, sts,
-                                  "Error initializing a MFX session");
-    else if (sts > 0) {
-        ff_qsv_print_warning(avctx, sts,
-                             "Warning in MFX initialization");
-        return AVERROR_UNKNOWN;
-    }
-
-    *psession = session;
-
-    return 0;
+    return qsv_create_mfx_session_legacy(avctx, implementation, pver, gpu_copy, psession);
 }
 
 #endif
 libavfilter/qsvvpp.c | 55 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 34 insertions(+), 21 deletions(-)

diff --git sources/libavfilter/qsvvpp.c sources/libavfilter/qsvvpp.c
index 5cdba7d54a..8fef2b8414 100644
--- sources/libavfilter/qsvvpp.c
+++ sources/libavfilter/qsvvpp.c
@@ -1053,6 +1053,36 @@ int ff_qsvvpp_filter_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picr
     return 0;
 }
 
+static int ff_qsvvpp_create_mfx_session_legacy(void *ctx,
+                                               void *loader,
+                                               mfxIMPL implementation,
+                                               mfxVersion *pver,
+                                               mfxSession *psession)
+{
+    mfxSession session = NULL;
+    mfxStatus sts;
+
+    av_log(ctx, AV_LOG_VERBOSE,
+           "Use Intel(R) Media SDK to create MFX session, API version is "
+           "%d.%d, the required implementation version is %d.%d\n",
+           MFX_VERSION_MAJOR, MFX_VERSION_MINOR, pver->Major, pver->Minor);
+
+    *psession = NULL;
+    sts = MFXInit(implementation, pver, &session);
+    if (sts < 0)
+        return ff_qsvvpp_print_error(ctx, sts,
+                                     "Error initializing an MFX session");
+    else if (sts > 0)
+    {
+        ff_qsvvpp_print_warning(ctx, sts, "Warning in MFX session initialization");
+        return AVERROR_UNKNOWN;
+    }
+
+    *psession = session;
+
+    return 0;
+}
+
 #if QSV_ONEVPL
 
 int ff_qsvvpp_create_mfx_session(void *ctx,
@@ -1096,6 +1126,9 @@ int ff_qsvvpp_create_mfx_session(void *ctx,
         impl_idx++;
     }
 
+    if (sts && !ff_qsvvpp_create_mfx_session_legacy(ctx, loader, implementation, pver, psession))
+        return 0;
+
     if (sts < 0)
         return ff_qsvvpp_print_error(ctx, sts,
                                      "Error creating a MFX session");
@@ -1118,27 +1151,7 @@ int ff_qsvvpp_create_mfx_session(void *ctx,
                                  mfxVersion *pver,
                                  mfxSession *psession)
 {
-    mfxSession session = NULL;
-    mfxStatus sts;
-
-    av_log(ctx, AV_LOG_VERBOSE,
-           "Use Intel(R) Media SDK to create MFX session, API version is "
-           "%d.%d, the required implementation version is %d.%d\n",
-           MFX_VERSION_MAJOR, MFX_VERSION_MINOR, pver->Major, pver->Minor);
-
-    *psession = NULL;
-    sts = MFXInit(implementation, pver, &session);
-    if (sts < 0)
-        return ff_qsvvpp_print_error(ctx, sts,
-                                     "Error initializing an MFX session");
-    else if (sts > 0) {
-        ff_qsvvpp_print_warning(ctx, sts, "Warning in MFX session initialization");
-        return AVERROR_UNKNOWN;
-    }
-
-    *psession = session;
-
-    return 0;
+    return ff_qsvvpp_create_mfx_session_legacy(ctx, loader, implementation, pver, psession);
 }
 
 #endif
 libavutil/hwcontext_qsv.c | 112 +++++++++++++++++++++++++++-------------------
 1 file changed, 65 insertions(+), 47 deletions(-)

diff --git sources/libavutil/hwcontext_qsv.c sources/libavutil/hwcontext_qsv.c
index 1bfda9e69b..ff71690d9d 100644
--- sources/libavutil/hwcontext_qsv.c
+++ sources/libavutil/hwcontext_qsv.c
@@ -661,6 +661,68 @@ static mfxStatus frame_get_hdl(mfxHDL pthis, mfxMemId mid, mfxHDL *hdl)
     return MFX_ERR_NONE;
 }
 
+static int qsv_create_mfx_session_legacy(void *ctx,
+                                         mfxHDL handle,
+                                         mfxHandleType handle_type,
+                                         mfxIMPL implementation,
+                                         mfxVersion *pver,
+                                         mfxSession *psession)
+{
+    mfxVersion ver;
+    mfxStatus sts;
+    mfxSession session = NULL;
+
+    av_log(ctx, AV_LOG_VERBOSE,
+           "Use Intel(R) Media SDK to create MFX session, API version is "
+           "%d.%d, the required implementation version is %d.%d\n",
+           MFX_VERSION_MAJOR, MFX_VERSION_MINOR, pver->Major, pver->Minor);
+
+    *psession = NULL;
+    ver = *pver;
+    sts = MFXInit(implementation, &ver, &session);
+    if (sts != MFX_ERR_NONE)
+    {
+        av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session: "
+                                  "%d.\n",
+               sts);
+        goto fail;
+    }
+
+    sts = MFXQueryVersion(session, &ver);
+    if (sts != MFX_ERR_NONE)
+    {
+        av_log(ctx, AV_LOG_ERROR, "Error querying an MFX session: "
+                                  "%d.\n",
+               sts);
+        goto fail;
+    }
+
+    av_log(ctx, AV_LOG_VERBOSE, "Initialize MFX session: implementation "
+                                "version is %d.%d\n",
+           ver.Major, ver.Minor);
+
+    MFXClose(session);
+
+    sts = MFXInit(implementation, &ver, &session);
+    if (sts != MFX_ERR_NONE)
+    {
+        av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session: "
+                                  "%d.\n",
+               sts);
+        goto fail;
+    }
+
+    *psession = session;
+
+    return 0;
+
+fail:
+    if (session)
+        MFXClose(session);
+
+    return AVERROR_UNKNOWN;
+}
+
 #if QSV_ONEVPL
 
 static int qsv_d3d11_update_config(void *ctx, mfxHDL handle, mfxConfig cfg)
@@ -1028,7 +1090,8 @@ static int qsv_create_mfx_session(void *ctx,
     } else
         loader = *ploader;      // Use the input mfxLoader to create mfx session
 
-    if (qsv_create_mfx_session_from_loader(ctx, loader, psession))
+    if (qsv_create_mfx_session_from_loader(ctx, loader, psession) &&
+        qsv_create_mfx_session_legacy(ctx, handle, handle_type, implementation, pver, psession))
         goto fail;
 
     if (!*ploader)
@@ -1053,53 +1116,8 @@ static int qsv_create_mfx_session(void *ctx,
                                   mfxSession *psession,
                                   void **ploader)
 {
-    mfxVersion ver;
-    mfxStatus sts;
-    mfxSession session = NULL;
-
-    av_log(ctx, AV_LOG_VERBOSE,
-           "Use Intel(R) Media SDK to create MFX session, API version is "
-           "%d.%d, the required implementation version is %d.%d\n",
-           MFX_VERSION_MAJOR, MFX_VERSION_MINOR, pver->Major, pver->Minor);
-
     *ploader = NULL;
-    *psession = NULL;
-    ver = *pver;
-    sts = MFXInit(implementation, &ver, &session);
-    if (sts != MFX_ERR_NONE) {
-        av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session: "
-               "%d.\n", sts);
-        goto fail;
-    }
-
-    sts = MFXQueryVersion(session, &ver);
-    if (sts != MFX_ERR_NONE) {
-        av_log(ctx, AV_LOG_ERROR, "Error querying an MFX session: "
-               "%d.\n", sts);
-        goto fail;
-    }
-
-    av_log(ctx, AV_LOG_VERBOSE, "Initialize MFX session: implementation "
-           "version is %d.%d\n", ver.Major, ver.Minor);
-
-    MFXClose(session);
-
-    sts = MFXInit(implementation, &ver, &session);
-    if (sts != MFX_ERR_NONE) {
-        av_log(ctx, AV_LOG_ERROR, "Error initializing an MFX session: "
-               "%d.\n", sts);
-        goto fail;
-    }
-
-    *psession = session;
-
-    return 0;
-
-fail:
-    if (session)
-        MFXClose(session);
-
-    return AVERROR_UNKNOWN;
+    return qsv_create_mfx_session_legacy(ctx, handle, handle_type, implementation, pver, psession);
 }
 
 #endif