]> Some of my projects - aniplayer.git/commitdiff
Use new MPV render API
authorAPTX <marek321@gmail.com>
Sat, 19 Feb 2022 09:02:57 +0000 (18:02 +0900)
committerAPTX <marek321@gmail.com>
Mon, 21 Feb 2022 15:10:20 +0000 (00:10 +0900)
backendplugins/backend_mpv/backendmpv.cpp
backendplugins/backend_mpv/backendmpv.h

index fbc55f8b9efb9ff5497efb549c596b622195e783..ef9c3d2b5b78ecbbc1e88cf6ce0126c0686ddf09 100644 (file)
@@ -6,8 +6,9 @@
 #include <QUrl>
 
 #include "qthelper.hpp"
-#include <mpv/client.h>
-#include <mpv/opengl_cb.h>
+
+#include <mpv/render.h>
+#include <mpv/render_gl.h>
 
 #include <QLoggingCategory>
 
@@ -39,8 +40,6 @@ MpvInstance::MpvInstance(PlayerPluginInterface *playerInterface,
       << "Client API version: " << (mpv_client_api_version() >> 16) << '.'
       << (mpv_client_api_version() & ~(~0u << 16));
 
-  mpv_set_option_string(m_handle, "vo", "opengl-cb");
-
   int error = mpv_initialize(m_handle);
   if (error) {
     qCCritical(mpvBackend) << "Error initializing mpv"
@@ -422,40 +421,52 @@ VideoRendererMpv::VideoRendererMpv(mpv_handle *handle,
                                    VideoUpdateInterface *vui)
     : m_handle{handle} {
   qCDebug(mpvBackend, "VideoRendererMpv::VideoRendererMpv");
-  m_oglCtx = static_cast<mpv_opengl_cb_context *>(
-      mpv_get_sub_api(handle, MPV_SUB_API_OPENGL_CB));
-  qCDebug(mpvBackend) << "created ogl ctx" << m_oglCtx;
-  if (!m_oglCtx) {
-    qCDebug(mpvBackend) << "Error obtaining mpv ogl context";
+  mpv_opengl_init_params opengl_init_params;
+  opengl_init_params.get_proc_address = getProcAddress;
+  opengl_init_params.get_proc_address_ctx = nullptr;
+  mpv_render_param render_params[] = {
+      {MPV_RENDER_PARAM_API_TYPE,
+       const_cast<char *>(MPV_RENDER_API_TYPE_OPENGL)},
+      {MPV_RENDER_PARAM_OPENGL_INIT_PARAMS, &opengl_init_params},
+      {MPV_RENDER_PARAM_INVALID, nullptr}};
+
+  int error = mpv_render_context_create(&m_renderCtx, m_handle, render_params);
+  if (error) {
+    qCCritical(mpvBackend())
+        << "Error initializing mpv ogl context:" << mpv_error_string(error);
+    m_renderCtx = nullptr;
+  }
+  if (!m_renderCtx) {
+    qCDebug(mpvBackend) << "Error obtaining mpv render context";
     return;
   }
 
   qCDebug(mpvBackend, "setting callback");
-  mpv_opengl_cb_set_update_callback(m_oglCtx, mpvUpdate, vui);
+  mpv_render_context_set_update_callback(m_renderCtx, mpvUpdate, vui);
   qCDebug(mpvBackend, "initializing gl context");
-  int error = mpv_opengl_cb_init_gl(m_oglCtx, NULL, getProcAddress, this);
-  if (error) {
-    qCCritical(mpvBackend())
-        << "Error initializing mpv ogl context:" << mpv_error_string(error);
-    m_oglCtx = nullptr;
-  }
+
   qCDebug(mpvBackend, "all done");
 }
 
 VideoRendererMpv::~VideoRendererMpv() {
-  if (m_oglCtx) {
-    int error = mpv_opengl_cb_uninit_gl(m_oglCtx);
-    if (error)
-      qCWarning(mpvBackend)
-          << "Error uninitializing mpv ogl context:" << mpv_error_string(error);
+  if (m_renderCtx) {
+    mpv_render_context_free(m_renderCtx);
+    m_renderCtx = nullptr;
   }
 }
 
 void VideoRendererMpv::render(QOpenGLFramebufferObject *fbo) {
-  int error =
-      mpv_opengl_cb_draw(m_oglCtx,
-                         static_cast<int>(fbo->handle()), // GLuint is unsigned
-                         fbo->width(), fbo->height());
+  if (!m_renderCtx) {
+    return;
+  }
+  mpv_opengl_fbo opengl_fbo;
+  opengl_fbo.fbo = static_cast<int>(fbo->handle());
+  opengl_fbo.w = fbo->width();
+  opengl_fbo.h = fbo->height();
+  opengl_fbo.internal_format = fbo->format().internalTextureFormat();
+  mpv_render_param render_param{MPV_RENDER_PARAM_OPENGL_FBO, &opengl_fbo};
+
+  int error = mpv_render_context_render(m_renderCtx, &render_param);
   if (error)
     qCCritical(mpvBackend) << "Error rendering mpv frame:"
                            << mpv_error_string(error);
index ab4f0e925d5ba7b846107d37163e8a37f67fa404..2afb0bac37a52e3a8aa8bab9bd26474917977101 100644 (file)
@@ -8,8 +8,8 @@
 #include "aniplayer/backendpluginbase.h"
 
 struct mpv_handle;
-struct mpv_opengl_cb_context;
 struct mpv_event_property;
+struct mpv_render_context;
 
 class BACKEND_MPVSHARED_EXPORT BackendMpv : public QObject,
                                             public BackendPluginBase {
@@ -71,7 +71,7 @@ public:
 
 private:
   mpv_handle *m_handle = nullptr;
-  mpv_opengl_cb_context *m_oglCtx = nullptr;
+  mpv_render_context *m_renderCtx = nullptr;
 
   static void *getProcAddress(void *, const char *name);
   static void mpvUpdate(void *);