Skip to content

Commit 6b18d4d

Browse files
author
wm4
committed
video: add --hwdec=vdpau-copy mode
At this point, all other hwaccels provide -copy modes, and vdpau is the exception with not having one. Although there is vf_vdpaurb, it's less convenient in certain situations, and exposes some issues with the filter chain code as well.
1 parent dd02369 commit 6b18d4d

File tree

5 files changed

+82
-4
lines changed

5 files changed

+82
-4
lines changed

DOCS/man/options.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ Video
610610
:yes: exactly the same as ``auto``
611611
:auto-copy: enable best hw decoder with copy-back (see below)
612612
:vdpau: requires ``--vo=vdpau`` or ``--vo=opengl`` (Linux only)
613+
:vdpau-copy: copies video back into system RAM (Linux with some GPUs only)
613614
:vaapi: requires ``--vo=opengl`` or ``--vo=vaapi`` (Linux only)
614615
:vaapi-copy: copies video back into system RAM (Linux with Intel GPUs only)
615616
:videotoolbox: requires ``--vo=opengl`` (OS X 10.8 and up only)
@@ -651,10 +652,10 @@ Video
651652
primarily implemented on the CPU. Some exceptions are ``vdpaupp``,
652653
``vdpaurb`` and ``vavpp``. See `VIDEO FILTERS`_ for more details.
653654

654-
The ``vaapi-copy`` and ``dxva2-copy`` modes allow you to use hardware
655+
The ``...-copy`` modes (e.g. ``dxva2-copy``) allow you to use hardware
655656
decoding with any VO, backend or filter. Because these copy the decoded
656-
video back to system RAM, they're likely less efficient than the ``vaapi``
657-
or ``dxva2`` modes respectively.
657+
video back to system RAM, they're likely less efficient than the direct
658+
modes (like e.g. ``dxva2``).
658659

659660
.. note::
660661

options/options.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const struct m_opt_choice_alternatives mp_hwdec_names[] = {
9393
{"yes" , HWDEC_AUTO},
9494
{"auto-copy", HWDEC_AUTO_COPY},
9595
{"vdpau", HWDEC_VDPAU},
96+
{"vdpau-copy", HWDEC_VDPAU_COPY},
9697
{"videotoolbox",HWDEC_VIDEOTOOLBOX},
9798
{"videotoolbox-copy",HWDEC_VIDEOTOOLBOX_COPY},
9899
{"vaapi", HWDEC_VAAPI},

video/decode/vd_lavc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ const struct m_sub_options vd_lavc_conf = {
125125
};
126126

127127
extern const struct vd_lavc_hwdec mp_vd_lavc_vdpau;
128+
extern const struct vd_lavc_hwdec mp_vd_lavc_vdpau_copy;
128129
extern const struct vd_lavc_hwdec mp_vd_lavc_videotoolbox;
129130
extern const struct vd_lavc_hwdec mp_vd_lavc_videotoolbox_copy;
130131
extern const struct vd_lavc_hwdec mp_vd_lavc_vaapi;
@@ -177,6 +178,7 @@ static const struct vd_lavc_hwdec *const hwdec_list[] = {
177178
#endif
178179
#if HAVE_VDPAU_HWACCEL
179180
&mp_vd_lavc_vdpau,
181+
&mp_vd_lavc_vdpau_copy,
180182
#endif
181183
#if HAVE_VIDEOTOOLBOX_HWACCEL
182184
&mp_vd_lavc_videotoolbox,

video/decode/vdpau.c

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@
2121

2222
#include "lavc.h"
2323
#include "common/common.h"
24+
#include "video/mp_image_pool.h"
2425
#include "video/vdpau.h"
2526
#include "video/hwdec.h"
2627

2728
struct priv {
2829
struct mp_log *log;
2930
struct mp_vdpau_ctx *mpvdp;
3031
uint64_t preemption_counter;
32+
// vdpau-copy
33+
Display *display;
34+
struct mp_image_pool *sw_pool;
3135
};
3236

3337
static int init_decoder(struct lavc_ctx *ctx, int w, int h)
@@ -76,9 +80,16 @@ static void uninit(struct lavc_ctx *ctx)
7680
{
7781
struct priv *p = ctx->hwdec_priv;
7882

83+
if (p->display) {
84+
// for copy path: we own this stuff
85+
mp_vdpau_destroy(p->mpvdp);
86+
XCloseDisplay(p->display);
87+
}
88+
7989
talloc_free(p);
8090

81-
av_freep(&ctx->avctx->hwaccel_context);
91+
if (ctx->avctx)
92+
av_freep(&ctx->avctx->hwaccel_context);
8293
}
8394

8495
static int init(struct lavc_ctx *ctx)
@@ -102,6 +113,56 @@ static int probe(struct lavc_ctx *ctx, struct vd_lavc_hwdec *hwdec,
102113
return 0;
103114
}
104115

116+
static int init_copy(struct lavc_ctx *ctx)
117+
{
118+
struct priv *p = talloc_ptrtype(NULL, p);
119+
*p = (struct priv) {
120+
.log = mp_log_new(p, ctx->log, "vdpau"),
121+
};
122+
123+
p->display = XOpenDisplay(NULL);
124+
if (!p->display)
125+
goto error;
126+
127+
p->mpvdp = mp_vdpau_create_device_x11(p->log, p->display, true);
128+
if (!p->mpvdp)
129+
goto error;
130+
131+
p->sw_pool = talloc_steal(p, mp_image_pool_new(17));
132+
133+
ctx->hwdec_priv = p;
134+
135+
mp_vdpau_handle_preemption(p->mpvdp, &p->preemption_counter);
136+
return 0;
137+
138+
error:
139+
if (p->display)
140+
XCloseDisplay(p->display);
141+
talloc_free(p);
142+
return -1;
143+
}
144+
145+
static int probe_copy(struct lavc_ctx *ctx, struct vd_lavc_hwdec *hwdec,
146+
const char *codec)
147+
{
148+
assert(!ctx->hwdec_priv);
149+
int r = init_copy(ctx);
150+
if (ctx->hwdec_priv)
151+
uninit(ctx);
152+
ctx->hwdec_priv = NULL;
153+
154+
return r < 0 ? HWDEC_ERR_NO_CTX : 0;
155+
}
156+
157+
static struct mp_image *copy_image(struct lavc_ctx *ctx, struct mp_image *img)
158+
{
159+
struct priv *p = ctx->hwdec_priv;
160+
struct mp_hwdec_ctx *hwctx = &p->mpvdp->hwctx;
161+
struct mp_image *out = hwctx->download_image(hwctx, img, p->sw_pool);
162+
talloc_free(img);
163+
return out;
164+
}
165+
105166
const struct vd_lavc_hwdec mp_vd_lavc_vdpau = {
106167
.type = HWDEC_VDPAU,
107168
.image_format = IMGFMT_VDPAU,
@@ -112,3 +173,15 @@ const struct vd_lavc_hwdec mp_vd_lavc_vdpau = {
112173
.allocate_image = allocate_image,
113174
.process_image = update_format,
114175
};
176+
177+
const struct vd_lavc_hwdec mp_vd_lavc_vdpau_copy = {
178+
.type = HWDEC_VDPAU_COPY,
179+
.copying = true,
180+
.image_format = IMGFMT_VDPAU,
181+
.probe = probe_copy,
182+
.init = init_copy,
183+
.uninit = uninit,
184+
.init_decoder = init_decoder,
185+
.allocate_image = allocate_image,
186+
.process_image = copy_image,
187+
};

video/hwdec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ enum hwdec_type {
1111
HWDEC_AUTO,
1212
HWDEC_AUTO_COPY,
1313
HWDEC_VDPAU,
14+
HWDEC_VDPAU_COPY,
1415
HWDEC_VIDEOTOOLBOX,
1516
HWDEC_VIDEOTOOLBOX_COPY,
1617
HWDEC_VAAPI,

0 commit comments

Comments
 (0)