[vlc-devel] [PATCH 6/6] hw: vaapi: use the "decoder context" module

Thomas Guillem thomas at gllm.fr
Sun Feb 24 12:27:08 CET 2019


vlc_vaapi_instance is not refcounted anymore, but vlc_decoder_context is.
---
 modules/codec/avcodec/vaapi.c                 |  10 +-
 modules/hw/vaapi/Makefile.am                  |  26 +++
 modules/hw/vaapi/chroma.c                     |  20 +--
 modules/hw/vaapi/decoder_context.c            | 153 ++++++++++++++++++
 modules/hw/vaapi/filters.c                    |  37 ++---
 modules/hw/vaapi/filters.h                    |   4 +-
 modules/hw/vaapi/vlc_vaapi.c                  |  39 ++---
 modules/hw/vaapi/vlc_vaapi.h                  |  14 +-
 modules/video_output/Makefile.am              |  31 +---
 modules/video_output/opengl/converter_vaapi.c | 114 ++-----------
 10 files changed, 246 insertions(+), 202 deletions(-)
 create mode 100644 modules/hw/vaapi/decoder_context.c

diff --git a/modules/codec/avcodec/vaapi.c b/modules/codec/avcodec/vaapi.c
index 8bc9609439..1a796b8ed1 100644
--- a/modules/codec/avcodec/vaapi.c
+++ b/modules/codec/avcodec/vaapi.c
@@ -50,7 +50,7 @@
 
 struct vlc_va_sys_t
 {
-    struct vlc_vaapi_instance *va_inst;
+    vlc_decoder_context *dec_context;
     struct vaapi_context hw_ctx;
 };
 
@@ -141,7 +141,7 @@ static void Delete(vlc_va_t *va, void *hwctx)
 
     vlc_vaapi_DestroyContext(o, sys->hw_ctx.display, sys->hw_ctx.context_id);
     vlc_vaapi_DestroyConfig(o, sys->hw_ctx.display, sys->hw_ctx.config_id);
-    vlc_vaapi_ReleaseInstance(sys->va_inst);
+    vlc_decoder_context_Release(sys->dec_context);
     free(sys);
 }
 
@@ -159,7 +159,7 @@ static int Create(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
 
     /* The picture must be allocated by the vout */
     VADisplay va_dpy;
-    struct vlc_vaapi_instance *va_inst =
+    vlc_decoder_context *dec_context =
         vlc_vaapi_PicSysHoldInstance(p_sys, &va_dpy);
 
     VASurfaceID *render_targets;
@@ -183,7 +183,7 @@ static int Create(vlc_va_t *va, AVCodecContext *ctx, enum PixelFormat pix_fmt,
     memset(sys, 0, sizeof (*sys));
 
     /* */
-    sys->va_inst = va_inst;
+    sys->dec_context = dec_context;
     sys->hw_ctx.display = va_dpy;
     sys->hw_ctx.config_id = VA_INVALID_ID;
     sys->hw_ctx.context_id = VA_INVALID_ID;
@@ -218,7 +218,7 @@ error:
             vlc_vaapi_DestroyConfig(o, sys->hw_ctx.display, sys->hw_ctx.config_id);
         free(sys);
     }
-    vlc_vaapi_ReleaseInstance(va_inst);
+    vlc_decoder_context_Release(dec_context);
     return ret;
 }
 
diff --git a/modules/hw/vaapi/Makefile.am b/modules/hw/vaapi/Makefile.am
index 194bff14f8..ad99445214 100644
--- a/modules/hw/vaapi/Makefile.am
+++ b/modules/hw/vaapi/Makefile.am
@@ -1,5 +1,18 @@
 vaapidir = $(pluginsdir)/vaapi
 
+libdecctx_vaapi_wl_plugin_la_SOURCES = hw/vaapi/decoder_context.c \
+	hw/vaapi/vlc_vaapi.c hw/vaapi/vlc_vaapi.h
+libdecctx_vaapi_wl_plugin_la_CFLAGS = $(AM_CFLAGS) $(GL_CFLAGS) -DHAVE_VA_WL $(LIBVA_WL_CFLAGS)
+libdecctx_vaapi_wl_plugin_la_LIBADD = $(LIBVA_LIBS) $(LIBVA_WL_LIBS)
+
+libdecctx_vaapi_x11_plugin_la_SOURCES = $(libdecctx_vaapi_wl_plugin_la_SOURCES)
+libdecctx_vaapi_x11_plugin_la_CFLAGS = $(AM_CFLAGS) -DHAVE_VA_X11
+libdecctx_vaapi_x11_plugin_la_LIBADD = $(LIBVA_LIBS) $(LIBVA_X11_LIBS) $(X_LIBS) $(X_PRE_LIBS) -lX11
+
+libdecctx_vaapi_drm_plugin_la_SOURCES = $(libdecctx_vaapi_wl_plugin_la_SOURCES)
+libdecctx_vaapi_drm_plugin_la_CFLAGS = $(AM_CFLAGS) -DHAVE_VA_DRM
+libdecctx_vaapi_drm_plugin_la_LIBADD = $(LIBVA_LIBS) $(LIBVA_DRM_LIBS)
+
 libvaapi_filters_plugin_la_SOURCES = hw/vaapi/filters.c hw/vaapi/filters.h \
 	hw/vaapi/vlc_vaapi.c hw/vaapi/vlc_vaapi.h \
 	hw/vaapi/chroma.c
@@ -10,4 +23,17 @@ libvaapi_filters_plugin_la_LIBADD = libchroma_copy.la $(AM_LIBADD) $(LIBVA_LIBS)
 
 if HAVE_VAAPI
 vaapi_LTLIBRARIES = libvaapi_filters_plugin.la
+if HAVE_WAYLAND
+if HAVE_VAAPI_WL
+vaapi_LTLIBRARIES += libdecctx_vaapi_wl_plugin.la
+endif
+endif
+if HAVE_XCB
+if HAVE_VAAPI_X11
+vaapi_LTLIBRARIES += libdecctx_vaapi_x11_plugin.la
+endif
+endif
+if HAVE_VAAPI_DRM
+vaapi_LTLIBRARIES += libdecctx_vaapi_drm_plugin.la
+endif
 endif
diff --git a/modules/hw/vaapi/chroma.c b/modules/hw/vaapi/chroma.c
index d8bb7d7ae2..244f83c2fe 100644
--- a/modules/hw/vaapi/chroma.c
+++ b/modules/hw/vaapi/chroma.c
@@ -39,7 +39,7 @@
 
 typedef struct
 {
-    struct vlc_vaapi_instance *va_inst;
+    vlc_decoder_context *dec_context;
     VADisplay           dpy;
     picture_pool_t *    dest_pics;
     VASurfaceID *       va_surface_ids;
@@ -345,22 +345,22 @@ vlc_vaapi_OpenChroma(vlc_object_t *obj)
     filter_sys->image_fallback_failed = false;
     if (is_upload)
     {
-        filter_sys->va_inst = vlc_vaapi_FilterHoldInstance(filter,
-                                                           &filter_sys->dpy);
+        filter_sys->dec_context = vlc_vaapi_FilterHoldInstance(filter,
+                                                               &filter_sys->dpy);
 
-        if (filter_sys->va_inst == NULL)
+        if (filter_sys->dec_context == NULL)
         {
             free(filter_sys);
             return VLC_EGENERIC;
         }
 
         filter_sys->dest_pics =
-            vlc_vaapi_PoolNew(obj, filter_sys->va_inst, filter_sys->dpy,
+            vlc_vaapi_PoolNew(obj, filter_sys->dec_context, filter_sys->dpy,
                               DEST_PICS_POOL_SZ, &filter_sys->va_surface_ids,
                               &filter->fmt_out.video, true);
         if (!filter_sys->dest_pics)
         {
-            vlc_vaapi_FilterReleaseInstance(filter, filter_sys->va_inst);
+            vlc_vaapi_FilterReleaseInstance(filter, filter_sys->dec_context);
             free(filter_sys);
             return VLC_EGENERIC;
         }
@@ -369,7 +369,7 @@ vlc_vaapi_OpenChroma(vlc_object_t *obj)
     {
         /* Don't fetch the vaapi instance since it may be not created yet at
          * this point (in case of cpu rendering) */
-        filter_sys->va_inst = NULL;
+        filter_sys->dec_context = NULL;
         filter_sys->dpy = NULL;
         filter_sys->dest_pics = NULL;
     }
@@ -380,7 +380,7 @@ vlc_vaapi_OpenChroma(vlc_object_t *obj)
         if (is_upload)
         {
             picture_pool_Release(filter_sys->dest_pics);
-            vlc_vaapi_FilterReleaseInstance(filter, filter_sys->va_inst);
+            vlc_vaapi_FilterReleaseInstance(filter, filter_sys->dec_context);
         }
         free(filter_sys);
         return VLC_EGENERIC;
@@ -404,8 +404,8 @@ vlc_vaapi_CloseChroma(vlc_object_t *obj)
 
     if (filter_sys->dest_pics)
         picture_pool_Release(filter_sys->dest_pics);
-    if (filter_sys->va_inst != NULL)
-        vlc_vaapi_FilterReleaseInstance(filter, filter_sys->va_inst);
+    if (filter_sys->dec_context != NULL)
+        vlc_vaapi_FilterReleaseInstance(filter, filter_sys->dec_context);
     CopyCleanCache(&filter_sys->cache);
 
     free(filter_sys);
diff --git a/modules/hw/vaapi/decoder_context.c b/modules/hw/vaapi/decoder_context.c
new file mode 100644
index 0000000000..06171a3915
--- /dev/null
+++ b/modules/hw/vaapi/decoder_context.c
@@ -0,0 +1,153 @@
+/*****************************************************************************
+ * decoder_context.c: VAAPI decoder_context
+ *****************************************************************************
+ * Copyright (C) 2017-2019 VLC authors and VideoLAN
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_vout_window.h>
+#include <vlc_codec.h>
+
+#include "hw/vaapi/vlc_vaapi.h"
+#include <va/va_drmcommon.h>
+
+#ifdef HAVE_VA_WL
+# include <va/va_wayland.h>
+#endif
+
+#ifdef HAVE_VA_X11
+# include <va/va_x11.h>
+# include <vlc_xlib.h>
+#endif
+
+#ifdef HAVE_VA_DRM
+# include <va/va_drm.h>
+# include <vlc_fs.h>
+# include <fcntl.h>
+#endif
+
+#ifdef HAVE_VA_X11
+static void
+x11_native_destroy_cb(VANativeDisplay native)
+{
+    XCloseDisplay(native);
+}
+
+static struct vlc_vaapi_instance *
+x11_init_vaapi_instance(vout_window_t *window, VADisplay *vadpyp)
+{
+    if (!vlc_xlib_init(VLC_OBJECT(window)))
+        return NULL;
+
+    Display *x11dpy = XOpenDisplay(window->display.x11);
+    if (x11dpy == NULL)
+        return NULL;
+
+    VADisplay vadpy = *vadpyp = vaGetDisplay(x11dpy);
+    if (vadpy == NULL)
+    {
+        x11_native_destroy_cb(x11dpy);
+        return NULL;
+    }
+
+    return vlc_vaapi_InitializeInstance(VLC_OBJECT(window), vadpy,
+                                        x11dpy, x11_native_destroy_cb);
+}
+#endif
+
+#ifdef HAVE_VA_DRM
+static struct vlc_vaapi_instance *
+drm_init_vaapi_instance(vout_window_t *window,  VADisplay *vadpyp)
+{
+    return vlc_vaapi_InitializeInstanceDRM(VLC_OBJECT(window), vaGetDisplayDRM,
+                                           vadpyp, NULL);
+}
+#endif
+
+#ifdef HAVE_VA_WL
+static struct vlc_vaapi_instance *
+wl_init_vaapi_instance(vout_window_t *window, VADisplay *vadpyp)
+{
+    VADisplay vadpy = *vadpyp = vaGetDisplayWl(window->display.wl);
+    if (vadpy == NULL)
+        return NULL;
+
+    return vlc_vaapi_InitializeInstance(VLC_OBJECT(window), vadpy, NULL, NULL);
+}
+#endif
+
+static void
+Close(vlc_decoder_context *context)
+{
+    vlc_vaapi_DestroyInstance(context->sys);
+}
+
+static int
+Open(vlc_decoder_context *context, vout_window_t *window)
+{
+    VADisplay vadpy = NULL;
+    struct vlc_vaapi_instance *vainst = NULL;
+#if defined (HAVE_VA_X11)
+    if (window->type == VOUT_WINDOW_TYPE_XID)
+        vainst = x11_init_vaapi_instance(window, &vadpy);
+#elif defined(HAVE_VA_WL)
+    if (window->type == VOUT_WINDOW_TYPE_WAYLAND)
+        vainst = wl_init_vaapi_instance(window, &vadpy);
+#elif defined (HAVE_VA_DRM)
+    fprintf(stderr, "woof?\n");
+    vainst = drm_init_vaapi_instance(window, &vadpy);
+#else
+# error need X11/WL/DRM support
+#endif
+    if (!vainst)
+        return VLC_EGENERIC;
+    assert(vadpy != NULL);
+
+    context->sys = vainst;
+    context->type = VLC_DECODER_CONTEXT_VAAPI;
+    context->opaque = vadpy;
+    return VLC_SUCCESS;
+}
+
+#if defined (HAVE_VA_X11)
+# define PRIORITY 2
+# define SHORTCUT "vaapi_x11"
+# define DESCRIPTION_SUFFIX "X11"
+#elif defined(HAVE_VA_WL)
+# define PRIORITY 2
+# define SHORTCUT "vaapi_wl"
+# define DESCRIPTION_SUFFIX "Wayland"
+#elif defined (HAVE_VA_DRM)
+# define PRIORITY 1
+# define SHORTCUT "vaapi_drm"
+# define DESCRIPTION_SUFFIX "DRM"
+#endif
+
+vlc_module_begin ()
+    set_description("VA-API decoder context for " DESCRIPTION_SUFFIX)
+    set_capability("decoder context", PRIORITY)
+    set_callbacks(Open, Close)
+    set_category(CAT_VIDEO)
+    set_subcategory(SUBCAT_VIDEO_VOUT)
+    add_shortcut("vaapi", SHORTCUT)
+    add_submodule()
+vlc_module_end ()
diff --git a/modules/hw/vaapi/filters.c b/modules/hw/vaapi/filters.c
index 482321d969..302d5d2b09 100644
--- a/modules/hw/vaapi/filters.c
+++ b/modules/hw/vaapi/filters.c
@@ -44,11 +44,11 @@
  * filter is destroyed before the other ones. */
 static struct {
     vlc_mutex_t lock;
-    struct vlc_vaapi_instance *inst;
+    vlc_decoder_context *dec_context;
     filter_t *owner;
 } holder = { VLC_STATIC_MUTEX, NULL, NULL };
 
-struct vlc_vaapi_instance *
+vlc_decoder_context *
 vlc_vaapi_FilterHoldInstance(filter_t *filter, VADisplay *dpy)
 {
 
@@ -62,35 +62,36 @@ vlc_vaapi_FilterHoldInstance(filter_t *filter, VADisplay *dpy)
         return NULL;
     }
 
-    struct vlc_vaapi_instance *va_inst = NULL;
+    vlc_decoder_context *dec_context = NULL;
 
     vlc_mutex_lock(&holder.lock);
-    if (holder.inst != NULL)
+    if (holder.dec_context != NULL)
     {
-        va_inst = holder.inst;
-        *dpy = vlc_vaapi_HoldInstance(holder.inst);
+        dec_context = vlc_decoder_context_Hold(holder.dec_context);
+        *dpy = dec_context->opaque;
     }
     else
     {
         holder.owner = filter;
-        holder.inst = va_inst = pic->p_sys ?
+        holder.dec_context = dec_context = pic->p_sys ?
             vlc_vaapi_PicSysHoldInstance(pic->p_sys, dpy) : NULL;
+        assert(holder.dec_context->type == VLC_DECODER_CONTEXT_VAAPI);
     }
     vlc_mutex_unlock(&holder.lock);
     picture_Release(pic);
 
-    return va_inst;
+    return dec_context;
 }
 
 void
 vlc_vaapi_FilterReleaseInstance(filter_t *filter,
-                                struct vlc_vaapi_instance *va_inst)
+                                vlc_decoder_context *dec_context)
 {
-    vlc_vaapi_ReleaseInstance(va_inst);
+    vlc_decoder_context_Release(dec_context);
     vlc_mutex_lock(&holder.lock);
     if (filter == holder.owner)
     {
-        holder.inst = NULL;
+        holder.dec_context = NULL;
         holder.owner = NULL;
     }
     vlc_mutex_unlock(&holder.lock);
@@ -101,7 +102,7 @@ vlc_vaapi_FilterReleaseInstance(filter_t *filter,
 
 struct  va_filter_desc
 {
-    struct vlc_vaapi_instance *inst;
+    vlc_decoder_context *dec_context;
     VADisplay           dpy;
     VAConfigID          conf;
     VAContextID         ctx;
@@ -374,13 +375,13 @@ Open(filter_t * filter,
     filter_sys->va.conf = VA_INVALID_ID;
     filter_sys->va.ctx = VA_INVALID_ID;
     filter_sys->va.buf = VA_INVALID_ID;
-    filter_sys->va.inst =
+    filter_sys->va.dec_context =
         vlc_vaapi_FilterHoldInstance(filter, &filter_sys->va.dpy);
-    if (!filter_sys->va.inst)
+    if (!filter_sys->va.dec_context)
         goto error;
 
     filter_sys->dest_pics =
-        vlc_vaapi_PoolNew(VLC_OBJECT(filter), filter_sys->va.inst,
+        vlc_vaapi_PoolNew(VLC_OBJECT(filter), filter_sys->va.dec_context,
                           filter_sys->va.dpy, DEST_PICS_POOL_SZ,
                           &filter_sys->va.surface_ids, &filter->fmt_out.video,
                           true);
@@ -455,8 +456,8 @@ error:
                                 filter_sys->va.dpy, filter_sys->va.conf);
     if (filter_sys->dest_pics)
         picture_pool_Release(filter_sys->dest_pics);
-    if (filter_sys->va.inst)
-        vlc_vaapi_FilterReleaseInstance(filter, filter_sys->va.inst);
+    if (filter_sys->va.dec_context)
+        vlc_vaapi_FilterReleaseInstance(filter, filter_sys->va.dec_context);
     free(filter_sys);
     return VLC_EGENERIC;
 }
@@ -469,7 +470,7 @@ Close(filter_t *filter, filter_sys_t * filter_sys)
     vlc_vaapi_DestroyBuffer(obj, filter_sys->va.dpy, filter_sys->va.buf);
     vlc_vaapi_DestroyContext(obj, filter_sys->va.dpy, filter_sys->va.ctx);
     vlc_vaapi_DestroyConfig(obj, filter_sys->va.dpy, filter_sys->va.conf);
-    vlc_vaapi_FilterReleaseInstance(filter, filter_sys->va.inst);
+    vlc_vaapi_FilterReleaseInstance(filter, filter_sys->va.dec_context);
     free(filter_sys);
 }
 
diff --git a/modules/hw/vaapi/filters.h b/modules/hw/vaapi/filters.h
index 2e739feb37..1da8358e38 100644
--- a/modules/hw/vaapi/filters.h
+++ b/modules/hw/vaapi/filters.h
@@ -32,11 +32,11 @@ int  vlc_vaapi_OpenChroma(vlc_object_t *obj);
 void vlc_vaapi_CloseChroma(vlc_object_t *obj);
 
 /* Get and hold the VADisplay instance from a filter */
-struct vlc_vaapi_instance *
+vlc_decoder_context *
 vlc_vaapi_FilterHoldInstance(filter_t *filter, VADisplay *dpy);
 
 void
 vlc_vaapi_FilterReleaseInstance(filter_t *filter,
-                                struct vlc_vaapi_instance *va_inst);
+                                vlc_decoder_context *dec_context);
 
 #endif /* VLC_VAAPI_FILTERS_H */
diff --git a/modules/hw/vaapi/vlc_vaapi.c b/modules/hw/vaapi/vlc_vaapi.c
index b6d6d4bd14..103711beb4 100644
--- a/modules/hw/vaapi/vlc_vaapi.c
+++ b/modules/hw/vaapi/vlc_vaapi.c
@@ -83,7 +83,6 @@ struct vlc_vaapi_instance {
     VADisplay dpy;
     VANativeDisplay native;
     vlc_vaapi_native_destroy_cb native_destroy_cb;
-    atomic_uint pic_refcount;
 };
 
 struct vlc_vaapi_instance *
@@ -100,7 +99,6 @@ vlc_vaapi_InitializeInstance(vlc_object_t *o, VADisplay dpy,
     inst->dpy = dpy;
     inst->native = native;
     inst->native_destroy_cb = native_destroy_cb;
-    atomic_init(&inst->pic_refcount, 1);
 
     return inst;
 error:
@@ -167,24 +165,13 @@ vlc_vaapi_InitializeInstanceDRM(vlc_object_t *o,
     return NULL;
 }
 
-
-VADisplay
-vlc_vaapi_HoldInstance(struct vlc_vaapi_instance *inst)
-{
-    atomic_fetch_add(&inst->pic_refcount, 1);
-    return inst->dpy;
-}
-
 void
-vlc_vaapi_ReleaseInstance(struct vlc_vaapi_instance *inst)
+vlc_vaapi_DestroyInstance(struct vlc_vaapi_instance *inst)
 {
-    if (atomic_fetch_sub(&inst->pic_refcount, 1) == 1)
-    {
-        vaTerminate(inst->dpy);
-        if (inst->native != NULL && inst->native_destroy_cb != NULL)
-            inst->native_destroy_cb(inst->native);
-        free(inst);
-    }
+    vaTerminate(inst->dpy);
+    if (inst->native != NULL && inst->native_destroy_cb != NULL)
+        inst->native_destroy_cb(inst->native);
+    free(inst);
 }
 
 /**************************
@@ -539,7 +526,7 @@ struct pic_sys_vaapi_instance
 {
     atomic_int pic_refcount;
     VADisplay va_dpy;
-    struct vlc_vaapi_instance *va_inst;
+    vlc_decoder_context *dec_context;
     unsigned num_render_targets;
     VASurfaceID render_targets[];
 };
@@ -560,7 +547,7 @@ pool_pic_destroy_cb(picture_t *pic)
     {
         vaDestroySurfaces(instance->va_dpy, instance->render_targets,
                           instance->num_render_targets);
-        vlc_vaapi_ReleaseInstance(instance->va_inst);
+        vlc_decoder_context_Release(instance->dec_context);
         free(instance);
     }
     free(pic->p_sys);
@@ -596,7 +583,7 @@ pic_sys_ctx_destroy_cb(struct picture_context_t *opaque)
 }
 
 picture_pool_t *
-vlc_vaapi_PoolNew(vlc_object_t *o, struct vlc_vaapi_instance *va_inst,
+vlc_vaapi_PoolNew(vlc_object_t *o, vlc_decoder_context *dec_context,
                   VADisplay dpy, unsigned count, VASurfaceID **render_targets,
                   const video_format_t *restrict fmt, bool b_force_fourcc)
 {
@@ -665,8 +652,8 @@ vlc_vaapi_PoolNew(vlc_object_t *o, struct vlc_vaapi_instance *va_inst,
         goto error_pic;
 
     atomic_store(&instance->pic_refcount, count);
-    instance->va_dpy = vlc_vaapi_HoldInstance(va_inst);
-    instance->va_inst = va_inst;
+    instance->va_dpy = dpy;
+    instance->dec_context = vlc_decoder_context_Hold(dec_context);
 
     *render_targets = instance->render_targets;
     return pool;
@@ -692,13 +679,13 @@ vlc_vaapi_PicSysGetRenderTargets(void *_sys, VASurfaceID **render_targets)
     return sys->instance->num_render_targets;
 }
 
-struct vlc_vaapi_instance *
+vlc_decoder_context *
 vlc_vaapi_PicSysHoldInstance(void *_sys, VADisplay *dpy)
 {
     picture_sys_t *sys = (picture_sys_t *)_sys;
     assert(sys->instance != NULL);
-    *dpy = vlc_vaapi_HoldInstance(sys->instance->va_inst);
-    return sys->instance->va_inst;
+    *dpy = sys->instance->va_dpy;
+    return vlc_decoder_context_Hold(sys->instance->dec_context);
 }
 
 #define ASSERT_VAAPI_CHROMA(pic) do { \
diff --git a/modules/hw/vaapi/vlc_vaapi.h b/modules/hw/vaapi/vlc_vaapi.h
index 8f7ca3455e..1d917726e7 100644
--- a/modules/hw/vaapi/vlc_vaapi.h
+++ b/modules/hw/vaapi/vlc_vaapi.h
@@ -36,6 +36,7 @@
 #endif
 
 #include <vlc_common.h>
+#include <vlc_codec.h>
 #include <vlc_fourcc.h>
 #include <vlc_picture_pool.h>
 
@@ -63,14 +64,9 @@ vlc_vaapi_InitializeInstanceDRM(vlc_object_t *o,
                                 VADisplay *pdpy, const char *device);
 
 
-/* Increments the VAAPI instance refcount */
-VADisplay
-vlc_vaapi_HoldInstance(struct vlc_vaapi_instance *inst);
-
-/* Decrements the VAAPI instance refcount, and call vaTerminate if that counter
- * reaches 0 */
+/* Destroy the VAAPI instance refcount, and call vaTerminate */
 void
-vlc_vaapi_ReleaseInstance(struct vlc_vaapi_instance *inst);
+vlc_vaapi_DestroyInstance(struct vlc_vaapi_instance *inst);
 
 /**************************
  * VAAPI create & destroy *
@@ -194,7 +190,7 @@ vlc_vaapi_CreateConfigChecked(vlc_object_t *o, VADisplay dpy,
 /* Create a pool backed by VASurfaceID. render_targets will destroyed once
  * the pool and every pictures are released. */
 picture_pool_t *
-vlc_vaapi_PoolNew(vlc_object_t *o, struct vlc_vaapi_instance *vainst,
+vlc_vaapi_PoolNew(vlc_object_t *o, vlc_decoder_context *dec_context,
                   VADisplay dpy, unsigned count, VASurfaceID **render_targets,
                   const video_format_t *restrict fmt, bool b_force_fourcc);
 
@@ -204,7 +200,7 @@ unsigned
 vlc_vaapi_PicSysGetRenderTargets(void *sys, VASurfaceID **render_targets);
 
 /* Get and hold the VADisplay instance attached to the picture sys */
-struct vlc_vaapi_instance *
+vlc_decoder_context *
 vlc_vaapi_PicSysHoldInstance(void *sys, VADisplay *dpy);
 
 /* Attachs the VASurface to the picture context, the picture must be allocated
diff --git a/modules/video_output/Makefile.am b/modules/video_output/Makefile.am
index 6c95e621ca..2871c0b72f 100644
--- a/modules/video_output/Makefile.am
+++ b/modules/video_output/Makefile.am
@@ -77,22 +77,11 @@ libgl_plugin_la_CFLAGS += -DHAVE_GL_CORE_SYMBOLS
 libgl_plugin_la_LIBADD += $(GL_LIBS)
 endif
 
-libglconv_vaapi_wl_plugin_la_SOURCES = video_output/opengl/converter_vaapi.c \
+libglconv_vaapi_plugin_la_SOURCES = video_output/opengl/converter_vaapi.c \
 	video_output/opengl/converter.h \
 	hw/vaapi/vlc_vaapi.c hw/vaapi/vlc_vaapi.h
-libglconv_vaapi_wl_plugin_la_CFLAGS = $(AM_CFLAGS) $(GL_CFLAGS) -DHAVE_VA_WL $(LIBVA_WL_CFLAGS)
-libglconv_vaapi_wl_plugin_la_LIBADD = $(LIBVA_LIBS) $(LIBVA_EGL_LIBS) \
-	$(LIBVA_WL_LIBS)
-
-libglconv_vaapi_x11_plugin_la_SOURCES = $(libglconv_vaapi_wl_plugin_la_SOURCES)
-libglconv_vaapi_x11_plugin_la_CFLAGS = $(AM_CFLAGS) -DHAVE_VA_X11
-libglconv_vaapi_x11_plugin_la_LIBADD = $(LIBVA_LIBS) $(LIBVA_EGL_LIBS) \
-	$(LIBVA_X11_LIBS) $(X_LIBS) $(X_PRE_LIBS) -lX11
-
-libglconv_vaapi_drm_plugin_la_SOURCES = $(libglconv_vaapi_wl_plugin_la_SOURCES)
-libglconv_vaapi_drm_plugin_la_CFLAGS = $(AM_CFLAGS) -DHAVE_VA_DRM
-libglconv_vaapi_drm_plugin_la_LIBADD = $(LIBVA_LIBS) $(LIBVA_EGL_LIBS) \
-	$(LIBVA_DRM_LIBS)
+libglconv_vaapi_plugin_la_CFLAGS = $(AM_CFLAGS) $(GL_CFLAGS)
+libglconv_vaapi_plugin_la_LIBADD = $(LIBVA_LIBS) $(LIBVA_EGL_LIBS)
 
 libglconv_vdpau_plugin_la_SOURCES = video_output/opengl/converter_vdpau.c \
 	video_output/opengl/converter.h hw/vdpau/picture.c hw/vdpau/vlc_vdpau.h
@@ -103,19 +92,7 @@ if HAVE_GL
 vout_LTLIBRARIES += libgl_plugin.la
 if HAVE_EGL
 if HAVE_VAAPI
-if HAVE_WAYLAND
-if HAVE_VAAPI_WL
-vout_LTLIBRARIES += libglconv_vaapi_wl_plugin.la
-endif
-endif
-if HAVE_XCB
-if HAVE_VAAPI_X11
-vout_LTLIBRARIES += libglconv_vaapi_x11_plugin.la
-endif
-endif
-if HAVE_VAAPI_DRM
-vout_LTLIBRARIES += libglconv_vaapi_drm_plugin.la
-endif
+vout_LTLIBRARIES += libglconv_vaapi_plugin.la
 endif
 endif # HAVE_EGL
 
diff --git a/modules/video_output/opengl/converter_vaapi.c b/modules/video_output/opengl/converter_vaapi.c
index f4de838b86..885d4450ae 100644
--- a/modules/video_output/opengl/converter_vaapi.c
+++ b/modules/video_output/opengl/converter_vaapi.c
@@ -25,6 +25,7 @@
 #include "converter.h"
 #include "../../hw/vaapi/vlc_vaapi.h"
 #include <vlc_vout_window.h>
+#include <vlc_codec.h>
 
 #include <assert.h>
 
@@ -32,24 +33,8 @@
 #include <EGL/eglext.h>
 #include <va/va_drmcommon.h>
 
-#ifdef HAVE_VA_WL
-# include <va/va_wayland.h>
-#endif
-
-#ifdef HAVE_VA_X11
-# include <va/va_x11.h>
-# include <vlc_xlib.h>
-#endif
-
-#ifdef HAVE_VA_DRM
-# include <va/va_drm.h>
-# include <vlc_fs.h>
-# include <fcntl.h>
-#endif
-
 struct priv
 {
-    struct vlc_vaapi_instance *vainst;
     VADisplay vadpy;
     VASurfaceID *va_surface_ids;
     PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES;
@@ -238,7 +223,7 @@ tc_vaegl_get_pool(const opengl_tex_converter_t *tc, unsigned requested_count)
     struct priv *priv = tc->priv;
 
     picture_pool_t *pool =
-        vlc_vaapi_PoolNew(VLC_OBJECT(tc->gl), priv->vainst, priv->vadpy,
+        vlc_vaapi_PoolNew(VLC_OBJECT(tc->gl), tc->dec_context, priv->vadpy,
                           requested_count, &priv->va_surface_ids, &tc->fmt,
                           true);
     if (!pool)
@@ -301,8 +286,6 @@ Close(vlc_object_t *obj)
     if (priv->last.pic != NULL)
         vaegl_release_last_pic(tc, priv);
 
-    vlc_vaapi_ReleaseInstance(priv->vainst);
-
     free(tc->priv);
 }
 
@@ -340,63 +323,14 @@ tc_va_check_interop_blacklist(opengl_tex_converter_t *tc, VADisplay *vadpy)
     return VLC_SUCCESS;
 }
 
-#ifdef HAVE_VA_X11
-static void
-x11_native_destroy_cb(VANativeDisplay native)
-{
-    XCloseDisplay(native);
-}
-
-static struct vlc_vaapi_instance *
-x11_init_vaapi_instance(vout_window_t *window, VADisplay *vadpyp)
-{
-    if (!vlc_xlib_init(VLC_OBJECT(window)))
-        return NULL;
-
-    Display *x11dpy = XOpenDisplay(window->display.x11);
-    if (x11dpy == NULL)
-        return NULL;
-
-    VADisplay vadpy = *vadpyp = vaGetDisplay(x11dpy);
-    if (vadpy == NULL)
-    {
-        x11_native_destroy_cb(x11dpy);
-        return NULL;
-    }
-
-    return vlc_vaapi_InitializeInstance(VLC_OBJECT(window), vadpy,
-                                        x11dpy, x11_native_destroy_cb);
-}
-#endif
-
-#ifdef HAVE_VA_DRM
-static struct vlc_vaapi_instance *
-drm_init_vaapi_instance(vout_window_t *window,  VADisplay *vadpyp)
-{
-    return vlc_vaapi_InitializeInstanceDRM(VLC_OBJECT(window), vaGetDisplayDRM,
-                                           vadpyp, NULL);
-}
-#endif
-
-#ifdef HAVE_VA_WL
-static struct vlc_vaapi_instance *
-wl_init_vaapi_instance(vout_window_t *window, VADisplay *vadpyp)
-{
-    VADisplay vadpy = *vadpyp = vaGetDisplayWl(window->display.wl);
-    if (vadpy == NULL)
-        return NULL;
-
-    return vlc_vaapi_InitializeInstance(VLC_OBJECT(window), vadpy, NULL, NULL);
-}
-#endif
-
-
 static int
 Open(vlc_object_t *obj)
 {
     opengl_tex_converter_t *tc = (void *) obj;
 
-    if (!vlc_vaapi_IsChromaOpaque(tc->fmt.i_chroma)
+    if (tc->dec_context == NULL
+     || tc->dec_context->type != VLC_DECODER_CONTEXT_VAAPI
+     || !vlc_vaapi_IsChromaOpaque(tc->fmt.i_chroma)
      || tc->gl->ext != VLC_GL_EXT_EGL
      || tc->gl->egl.createImageKHR == NULL
      || tc->gl->egl.destroyImageKHR == NULL)
@@ -413,7 +347,6 @@ Open(vlc_object_t *obj)
     if (unlikely(tc->priv == NULL))
         goto error;
     priv->fourcc = 0;
-    priv->vainst = NULL;
 
     int va_fourcc;
     int vlc_sw_chroma;
@@ -439,20 +372,7 @@ Open(vlc_object_t *obj)
     if (priv->glEGLImageTargetTexture2DOES == NULL)
         goto error;
 
-#if defined (HAVE_VA_X11)
-    if (tc->gl->surface->type == VOUT_WINDOW_TYPE_XID)
-        priv->vainst = x11_init_vaapi_instance(tc->gl->surface, &priv->vadpy);
-#elif defined(HAVE_VA_WL)
-    if (tc->gl->surface->type == VOUT_WINDOW_TYPE_WAYLAND)
-        priv->vainst = wl_init_vaapi_instance(tc->gl->surface, &priv->vadpy);
-#elif defined (HAVE_VA_DRM)
-    priv->vainst = drm_init_vaapi_instance(tc->gl->surface, &priv->vadpy);
-#else
-# error need X11/WL/DRM support
-#endif
-
-    if (!priv->vainst)
-        goto error;
+    priv->vadpy = tc->dec_context->opaque;
     assert(priv->vadpy != NULL);
 
     if (tc_va_check_interop_blacklist(tc, priv->vadpy))
@@ -468,31 +388,15 @@ Open(vlc_object_t *obj)
 
     return VLC_SUCCESS;
 error:
-    if (priv && priv->vainst)
-        vlc_vaapi_ReleaseInstance(priv->vainst);
     free(priv);
     return VLC_EGENERIC;
 }
 
-#if defined (HAVE_VA_X11)
-# define PRIORITY 2
-# define SHORTCUT "vaapi_x11"
-# define DESCRIPTION_SUFFIX "X11"
-#elif defined(HAVE_VA_WL)
-# define PRIORITY 2
-# define SHORTCUT "vaapi_wl"
-# define DESCRIPTION_SUFFIX "Wayland"
-#elif defined (HAVE_VA_DRM)
-# define PRIORITY 1
-# define SHORTCUT "vaapi_drm"
-# define DESCRIPTION_SUFFIX "DRM"
-#endif
-
 vlc_module_begin ()
-    set_description("VA-API OpenGL surface converter for " DESCRIPTION_SUFFIX)
-    set_capability("glconv", PRIORITY)
+    set_description("VA-API OpenGL surface converte")
+    set_capability("glconv", 1)
     set_callbacks(Open, Close)
     set_category(CAT_VIDEO)
     set_subcategory(SUBCAT_VIDEO_VOUT)
-    add_shortcut("vaapi", SHORTCUT)
+    add_shortcut("vaapi")
 vlc_module_end ()
-- 
2.20.1



More information about the vlc-devel mailing list