Skip to content

Commit

Permalink
hwdec: support videotoolbox with libplacebo
Browse files Browse the repository at this point in the history
  • Loading branch information
rcombs authored and Dudemanguy committed Sep 26, 2023
1 parent d955cca commit ee41202
Show file tree
Hide file tree
Showing 7 changed files with 520 additions and 85 deletions.
8 changes: 6 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -1388,7 +1388,7 @@ endif
ios_gl = cc.has_header_symbol('OpenGLES/ES3/glext.h', 'GL_RGB32F', required: get_option('ios-gl'))
features += {'ios-gl': ios_gl}
if features['ios-gl']
sources += files('video/out/opengl/hwdec_ios.m')
sources += files('video/out/hwdec/hwdec_ios_gl.m')
endif

rpi_mmal_opt = get_option('rpi-mmal').require(
Expand Down Expand Up @@ -1480,8 +1480,12 @@ videotoolbox_gl = get_option('videotoolbox-gl').require(
error_message: 'gl-cocoa nor ios-gl could be found!',
)
features += {'videotoolbox-gl': videotoolbox_gl.allowed()}
features += {'videotoolbox-pl': videotoolbox_gl.allowed() and libplacebo.found()}
if features['videotoolbox-gl']
sources += files('video/out/opengl/hwdec_osx.c')
sources += files('video/out/hwdec/hwdec_mac_gl.c', 'video/out/hwdec/hwdec_vt.c')
endif
if features['videotoolbox-pl']
sources += files('video/out/hwdec/hwdec_vt_pl.m')
endif


Expand Down
2 changes: 1 addition & 1 deletion video/out/gpu/hwdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const struct ra_hwdec_driver *const ra_hwdec_drivers[] = {
#if HAVE_VAAPI_EGL || HAVE_VAAPI_LIBPLACEBO
&ra_hwdec_vaapi,
#endif
#if HAVE_VIDEOTOOLBOX_GL || HAVE_IOS_GL
#if HAVE_VIDEOTOOLBOX_GL || HAVE_IOS_GL || (HAVE_COCOA && HAVE_VULKAN)
&ra_hwdec_videotoolbox,
#endif
#if HAVE_D3D_HWACCEL
Expand Down
58 changes: 17 additions & 41 deletions video/out/opengl/hwdec_ios.m → video/out/hwdec/hwdec_ios_gl.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,10 @@

#include "video/out/gpu/hwdec.h"
#include "video/mp_image_pool.h"
#include "ra_gl.h"
#include "video/out/opengl/ra_gl.h"
#include "hwdec_vt.h"

struct priv_owner {
struct mp_hwdec_ctx hwctx;
};

struct priv {
CVPixelBufferRef pbuf;
CVOpenGLESTextureCacheRef gl_texture_cache;
CVOpenGLESTextureRef gl_planes[MP_MAX_PLANES];
struct ra_imgfmt_desc desc;
};

static bool check_hwdec(struct ra_hwdec *hw)
static bool check_hwdec(const struct ra_hwdec *hw)
{
if (!ra_is_gl(hw->ra_ctx->ra))
return false;
Expand Down Expand Up @@ -130,20 +120,6 @@ static int mapper_init(struct ra_hwdec_mapper *mapper)
{
struct priv *p = mapper->priv;

mapper->dst_params = mapper->src_params;
mapper->dst_params.imgfmt = mapper->src_params.hw_subfmt;
mapper->dst_params.hw_subfmt = 0;

if (!mapper->dst_params.imgfmt) {
MP_ERR(mapper, "Unsupported CVPixelBuffer format.\n");
return -1;
}

if (!ra_get_imgfmt_desc(mapper->ra, mapper->dst_params.imgfmt, &p->desc)) {
MP_ERR(mapper, "Unsupported texture format.\n");
return -1;
}

for (int n = 0; n < p->desc.num_planes; n++) {
p->desc.planes[n] = find_la_variant(mapper->ra, p->desc.planes[n]);
if (!p->desc.planes[n] || p->desc.planes[n]->ctype != RA_CTYPE_UNORM) {
Expand Down Expand Up @@ -262,17 +238,17 @@ static void mapper_uninit(struct ra_hwdec_mapper *mapper)
}
}

const struct ra_hwdec_driver ra_hwdec_videotoolbox = {
.name = "videotoolbox",
.priv_size = sizeof(struct priv_owner),
.imgfmts = {IMGFMT_VIDEOTOOLBOX, 0},
.init = init,
.uninit = uninit,
.mapper = &(const struct ra_hwdec_mapper_driver){
.priv_size = sizeof(struct priv),
.init = mapper_init,
.uninit = mapper_uninit,
.map = mapper_map,
.unmap = mapper_unmap,
},
};
bool vt_gl_init(const struct ra_hwdec *hw)
{
struct priv_owner *p = hw->priv;

if (!check_hwdec(hw))
return false;

p->interop_init = mapper_init;
p->interop_uninit = mapper_uninit;
p->interop_map = mapper_map;
p->interop_unmap = mapper_unmap;

return true;
}
59 changes: 18 additions & 41 deletions video/out/opengl/hwdec_osx.c → video/out/hwdec/hwdec_mac_gl.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,10 @@

#include "video/mp_image_pool.h"
#include "video/out/gpu/hwdec.h"
#include "ra_gl.h"
#include "video/out/opengl/ra_gl.h"
#include "hwdec_vt.h"

struct priv_owner {
struct mp_hwdec_ctx hwctx;
};

struct priv {
CVPixelBufferRef pbuf;
GLuint gl_planes[MP_MAX_PLANES];

struct ra_imgfmt_desc desc;
};

static bool check_hwdec(struct ra_hwdec *hw)
static bool check_hwdec(const struct ra_hwdec *hw)
{
if (!ra_is_gl(hw->ra_ctx->ra))
return false;
Expand Down Expand Up @@ -99,26 +89,13 @@ static int mapper_init(struct ra_hwdec_mapper *mapper)

gl->GenTextures(MP_MAX_PLANES, p->gl_planes);

mapper->dst_params = mapper->src_params;
mapper->dst_params.imgfmt = mapper->src_params.hw_subfmt;
mapper->dst_params.hw_subfmt = 0;

if (!mapper->dst_params.imgfmt) {
MP_ERR(mapper, "Unsupported CVPixelBuffer format.\n");
return -1;
}

if (!ra_get_imgfmt_desc(mapper->ra, mapper->dst_params.imgfmt, &p->desc)) {
MP_ERR(mapper, "Unsupported texture format.\n");
return -1;
}

for (int n = 0; n < p->desc.num_planes; n++) {
if (p->desc.planes[n]->ctype != RA_CTYPE_UNORM) {
MP_ERR(mapper, "Format unsupported.\n");
return -1;
}
}

return 0;
}

Expand Down Expand Up @@ -208,17 +185,17 @@ static void mapper_uninit(struct ra_hwdec_mapper *mapper)
gl->DeleteTextures(MP_MAX_PLANES, p->gl_planes);
}

const struct ra_hwdec_driver ra_hwdec_videotoolbox = {
.name = "videotoolbox",
.priv_size = sizeof(struct priv_owner),
.imgfmts = {IMGFMT_VIDEOTOOLBOX, 0},
.init = init,
.uninit = uninit,
.mapper = &(const struct ra_hwdec_mapper_driver){
.priv_size = sizeof(struct priv),
.init = mapper_init,
.uninit = mapper_uninit,
.map = mapper_map,
.unmap = mapper_unmap,
},
};
bool vt_gl_init(const struct ra_hwdec *hw)
{
struct priv_owner *p = hw->priv;

if (!check_hwdec(hw))
return false;

p->interop_init = mapper_init;
p->interop_uninit = mapper_uninit;
p->interop_map = mapper_map;
p->interop_unmap = mapper_unmap;

return true;
}
139 changes: 139 additions & 0 deletions video/out/hwdec/hwdec_vt.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* This file is part of mpv.
*
* mpv 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.
*
* mpv 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 mpv. If not, see <http://www.gnu.org/licenses/>.
*/

#include <stddef.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>

#include <libavutil/hwcontext.h>
#include <libavutil/hwcontext_videotoolbox.h>

#include "config.h"

#include "video/out/gpu/hwdec.h"
#include "video/out/hwdec/hwdec_vt.h"

static void determine_working_formats(struct ra_hwdec *hw);

static void uninit(struct ra_hwdec *hw)
{
struct priv_owner *p = hw->priv;

hwdec_devices_remove(hw->devs, &p->hwctx);
av_buffer_unref(&p->hwctx.av_device_ref);
}

const static vt_interop_init interop_inits[] = {
#if HAVE_VIDEOTOOLBOX_GL || HAVE_IOS_GL
vt_gl_init,
#endif
#if HAVE_LIBPLACEBO && COREVIDEO_SUPPORTS_METAL
vt_pl_init,
#endif
NULL
};

static int init(struct ra_hwdec *hw)
{
struct priv_owner *p = hw->priv;

for (int i = 0; interop_inits[i]; i++) {
if (interop_inits[i](hw)) {
break;
}
}

if (!p->interop_map || !p->interop_unmap) {
MP_VERBOSE(hw, "VT hwdec only works with OpenGL or Vulkan backends.\n");
return -1;
}

p->hwctx = (struct mp_hwdec_ctx){
.driver_name = hw->driver->name,
.hw_imgfmt = IMGFMT_VIDEOTOOLBOX,
};

av_hwdevice_ctx_create(&p->hwctx.av_device_ref, AV_HWDEVICE_TYPE_VIDEOTOOLBOX,
NULL, NULL, 0);

hwdec_devices_add(hw->devs, &p->hwctx);

return 0;
}

static void mapper_unmap(struct ra_hwdec_mapper *mapper)
{
struct priv_owner *p_owner = mapper->owner->priv;

p_owner->interop_unmap(mapper);
}

static void mapper_uninit(struct ra_hwdec_mapper *mapper)
{
struct priv_owner *p_owner = mapper->owner->priv;
if (p_owner->interop_uninit) {
p_owner->interop_uninit(mapper);
}
}

static int mapper_init(struct ra_hwdec_mapper *mapper)
{
struct priv_owner *p_owner = mapper->owner->priv;
struct priv *p = mapper->priv;

mapper->dst_params = mapper->src_params;
mapper->dst_params.imgfmt = mapper->src_params.hw_subfmt;
mapper->dst_params.hw_subfmt = 0;

if (!mapper->dst_params.imgfmt) {
MP_ERR(mapper, "Unsupported CVPixelBuffer format.\n");
return -1;
}

if (!ra_get_imgfmt_desc(mapper->ra, mapper->dst_params.imgfmt, &p->desc)) {
MP_ERR(mapper, "Unsupported texture format.\n");
return -1;
}

if (p_owner->interop_init)
return p_owner->interop_init(mapper);

return 0;
}

static int mapper_map(struct ra_hwdec_mapper *mapper)
{
struct priv_owner *p_owner = mapper->owner->priv;

return p_owner->interop_map(mapper);
}

const struct ra_hwdec_driver ra_hwdec_videotoolbox = {
.name = "videotoolbox",
.priv_size = sizeof(struct priv_owner),
.imgfmts = {IMGFMT_VIDEOTOOLBOX, 0},
.init = init,
.uninit = uninit,
.mapper = &(const struct ra_hwdec_mapper_driver){
.priv_size = sizeof(struct priv),
.init = mapper_init,
.uninit = mapper_uninit,
.map = mapper_map,
.unmap = mapper_unmap,
},
};
63 changes: 63 additions & 0 deletions video/out/hwdec/hwdec_vt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* This file is part of mpv.
*
* mpv 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.
*
* mpv 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 mpv. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <CoreVideo/CoreVideo.h>

#include "config.h"
#include "video/out/gpu/hwdec.h"

struct priv_owner {
struct mp_hwdec_ctx hwctx;

int (*interop_init)(struct ra_hwdec_mapper *mapper);
void (*interop_uninit)(struct ra_hwdec_mapper *mapper);

int (*interop_map)(struct ra_hwdec_mapper *mapper);
void (*interop_unmap)(struct ra_hwdec_mapper *mapper);
};

struct priv {
void *interop_mapper_priv;

CVPixelBufferRef pbuf;

#if HAVE_VIDEOTOOLBOX_GL
GLuint gl_planes[MP_MAX_PLANES];
#elif HAVE_IOS_GL
CVOpenGLESTextureCacheRef gl_texture_cache;
CVOpenGLESTextureRef gl_planes[MP_MAX_PLANES];
#endif

#if HAVE_VULKAN && COREVIDEO_SUPPORTS_METAL
#if defined(__OBJC__)
CVMetalTextureCacheRef mtl_texture_cache;
CVMetalTextureRef mtl_planes[MP_MAX_PLANES];
#else
void* mtl_texture_cache;
void* mtl_planes[MP_MAX_PLANES];
#endif
#endif

struct ra_imgfmt_desc desc;
};

typedef bool (*vt_interop_init)(const struct ra_hwdec *hw);

bool vt_gl_init(const struct ra_hwdec *hw);
bool vt_pl_init(const struct ra_hwdec *hw);
Loading

0 comments on commit ee41202

Please sign in to comment.