summaryrefslogtreecommitdiffstats
path: root/video/decode/vdpau.c
blob: a6b6210804cf9ded7769e8b492072f23a669883f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * 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 <libavcodec/avcodec.h>
#include <libavcodec/vdpau.h>
#include <libavutil/common.h>

#include "lavc.h"
#include "common/common.h"
#include "video/mp_image_pool.h"
#include "video/vdpau.h"
#include "video/hwdec.h"

struct priv {
    struct mp_log              *log;
    struct mp_vdpau_ctx        *mpvdp;
    uint64_t                    preemption_counter;
    // vdpau-copy
    Display                    *display;
    struct mp_image_pool       *sw_pool;
};

static int init_decoder(struct lavc_ctx *ctx, int w, int h)
{
    struct priv *p = ctx->hwdec_priv;

    // During preemption, pretend everything is ok.
    if (mp_vdpau_handle_preemption(p->mpvdp, &p->preemption_counter) < 0)
        return 0;

    return av_vdpau_bind_context(ctx->avctx, p->mpvdp->vdp_device,
                                 p->mpvdp->get_proc_address,
                                 AV_HWACCEL_FLAG_IGNORE_LEVEL |
                                 AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH);
}

static struct mp_image *allocate_image(struct lavc_ctx *ctx, int w, int h)
{
    struct priv *p = ctx->hwdec_priv;

    // In case of preemption, reinit the decoder. Setting hwdec_request_reinit
    // will cause init_decoder() to be called again.
    if (mp_vdpau_handle_preemption(p->mpvdp, &p->preemption_counter) == 0)
        ctx->hwdec_request_reinit = true;

    VdpChromaType chroma = 0;
    uint32_t s_w = w, s_h = h;
    if (av_vdpau_get_surface_parameters(ctx->avctx, &chroma, &s_w, &s_h) < 0)
        return NULL;

    return mp_vdpau_get_video_surface(p->mpvdp, chroma, s_w, s_h);
}

static struct mp_image *update_format(struct lavc_ctx *ctx, struct mp_image *img)
{
    VdpChromaType chroma = 0;
    uint32_t s_w, s_h;
    if (av_vdpau_get_surface_parameters(ctx->avctx, &chroma, &s_w, &s_h) >= 0) {
        if (chroma == VDP_CHROMA_TYPE_420)
            img->params.hw_subfmt = IMGFMT_NV12;
    }
    return img;
}

static void uninit(struct lavc_ctx *ctx)
{
    struct priv *p = ctx->hwdec_priv;

    if (p->display) {
        // for copy path: we own this stuff
        mp_vdpau_destroy(p->mpvdp);
        XCloseDisplay(p->display);
    }

    TA_FREEP(&ctx->hwdec_priv);

    if (ctx->avctx)
        av_freep(&ctx->avctx->hwaccel_context);
}

static int init(struct lavc_ctx *ctx)
{
    struct priv *p = talloc_ptrtype(NULL, p);
    *p = (struct priv) {
        .log = mp_log_new(p, ctx->log, "vdpau"),
        .mpvdp = hwdec_devices_get(ctx->hwdec_devs, HWDEC_VDPAU)->ctx,
    };
    ctx->hwdec_priv = p;

    mp_vdpau_handle_preemption(p->mpvdp, &p->preemption_counter);
    return 0;
}

static int probe(struct lavc_ctx *ctx, struct vd_lavc_hwdec *hwdec,
                 const char *codec)
{
    if (!hwdec_devices_load(ctx->hwdec_devs, HWDEC_VDPAU))
        return HWDEC_ERR_NO_CTX;
    return 0;
}

static int init_copy(struct lavc_ctx *ctx)
{
    struct priv *p = talloc_ptrtype(NULL, p);
    *p = (struct priv) {
        .log = mp_log_new(p, ctx->log, "vdpau"),
    };

    p->display = XOpenDisplay(NULL);
    if (!p->display)
        goto error;

    p->mpvdp = mp_vdpau_create_device_x11(p->log, p->display, true);
    if (!p->mpvdp)
        goto error;

    p->sw_pool = talloc_steal(p, mp_image_pool_new(17));

    ctx->hwdec_priv = p;

    mp_vdpau_handle_preemption(p->mpvdp, &p->preemption_counter);
    return 0;

error:
    if (p->display)
        XCloseDisplay(p->display);
    talloc_free(p);
    return -1;
}

static int probe_copy(struct lavc_ctx *ctx, struct vd_lavc_hwdec *hwdec,
                      const char *codec)
{
    assert(!ctx->hwdec_priv);

    int r = HWDEC_ERR_NO_CTX;
    if (init_copy(ctx) >=0 ) {
        struct priv *p = ctx->hwdec_priv;
        r = mp_vdpau_guess_if_emulated(p->mpvdp) ? HWDEC_ERR_EMULATED : 0;
        uninit(ctx);
    }
    return r;
}

static struct mp_image *copy_image(struct lavc_ctx *ctx, struct mp_image *img)
{
    struct priv *p = ctx->hwdec_priv;
    struct mp_hwdec_ctx *hwctx = &p->mpvdp->hwctx;
    struct mp_image *out = hwctx->download_image(hwctx, img, p->sw_pool);
    talloc_free(img);
    return out;
}

const struct vd_lavc_hwdec mp_vd_lavc_vdpau = {
    .type = HWDEC_VDPAU,
    .image_format = IMGFMT_VDPAU,
    .probe = probe,
    .init = init,
    .uninit = uninit,
    .init_decoder = init_decoder,
    .allocate_image = allocate_image,
    .process_image = update_format,
};

const struct vd_lavc_hwdec mp_vd_lavc_vdpau_copy = {
    .type = HWDEC_VDPAU_COPY,
    .copying = true,
    .image_format = IMGFMT_VDPAU,
    .probe = probe_copy,
    .init = init_copy,
    .uninit = uninit,
    .init_decoder = init_decoder,
    .allocate_image = allocate_image,
    .process_image = copy_image,
};