summaryrefslogtreecommitdiffstats
path: root/video/d3d11va.c
blob: a9be571e9ce440bef2c85e0ad5155c9ca1215cd3 (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
/*
 * 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 "mp_image.h"
#include "d3d11va.h"

struct d3d11va_surface {
    HMODULE d3d11_dll;
    ID3D11Texture2D              *texture;
    ID3D11VideoDecoderOutputView *surface;
};

ID3D11VideoDecoderOutputView *d3d11_surface_in_mp_image(struct mp_image *mpi)
{
    return mpi && mpi->imgfmt == IMGFMT_D3D11VA ?
        (ID3D11VideoDecoderOutputView *)mpi->planes[3] : NULL;
}

ID3D11Texture2D *d3d11_texture_in_mp_image(struct mp_image *mpi)
{
    if (!mpi || mpi->imgfmt != IMGFMT_D3D11VA)
        return NULL;
    struct d3d11va_surface *surface = (void *)mpi->planes[0];
    return surface->texture;
}

static void d3d11va_release_img(void *arg)
{
    struct d3d11va_surface *surface = arg;
    if (surface->surface)
        ID3D11VideoDecoderOutputView_Release(surface->surface);

    if (surface->texture)
        ID3D11Texture2D_Release(surface->texture);

    if (surface->d3d11_dll)
        FreeLibrary(surface->d3d11_dll);

    talloc_free(surface);
}

struct mp_image *d3d11va_new_ref(ID3D11VideoDecoderOutputView *view,
                                 int w, int h)
{
    if (!view)
        return NULL;
    struct d3d11va_surface *surface = talloc_zero(NULL, struct d3d11va_surface);

    surface->d3d11_dll = LoadLibrary(L"d3d11.dll");
    if (!surface->d3d11_dll)
        goto fail;

    surface->surface = view;
    ID3D11VideoDecoderOutputView_AddRef(surface->surface);
    ID3D11VideoDecoderOutputView_GetResource(
        surface->surface, (ID3D11Resource **)&surface->texture);

    struct mp_image *mpi = mp_image_new_custom_ref(
        &(struct mp_image){0}, surface, d3d11va_release_img);
    if (!mpi)
        abort();

    mp_image_setfmt(mpi, IMGFMT_D3D11VA);
    mp_image_set_size(mpi, w, h);
    mpi->planes[0] = (void *)surface;
    mpi->planes[3] = (void *)surface->surface;

    return mpi;
fail:
    d3d11va_release_img(surface);
    return NULL;
}