summaryrefslogtreecommitdiffstats
path: root/video/out/gl_hwdec_vda.c
blob: 65615bac6b115c961100e68c4e159d4d48c58cf8 (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
/*
 * Copyright (c) 2013 Stefano Pigozzi <stefano.pigozzi@gmail.com>
 *
 * This file is part of mpv.
 *
 * mpv is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with mpv.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <IOSurface/IOSurface.h>
#include <CoreVideo/CoreVideo.h>
#include <OpenGL/OpenGL.h>

#include "video/decode/dec_video.h"
#include "cocoa_common.h"
#include "gl_common.h"

struct priv {
    CVPixelBufferRef pbuf;
    GLuint gl_texture;
};

static bool check_hwdec(struct gl_hwdec *hw)
{
    struct vo *vo = hw->mpgl->vo;

    if (hw->gl_texture_target != GL_TEXTURE_RECTANGLE) {
        MP_ERR(hw, "must use rectangle video textures with VDA\n");
        return false;
    }

    if (!vo->cocoa) {
        MP_ERR(hw, "need cocoa opengl backend to be active");
        return false;
    }

    return true;
}

static int create(struct gl_hwdec *hw)
{
    struct priv *p = talloc_zero(hw, struct priv);
    hw->priv = p;
    hw->converted_imgfmt = IMGFMT_UYVY;
    hw->gl_texture_target = GL_TEXTURE_RECTANGLE;

    if (!check_hwdec(hw))
        return -1;

    GL *gl = hw->mpgl->gl;
    gl->GenTextures(1, &p->gl_texture);

    return 0;
}

static int reinit(struct gl_hwdec *hw, const struct mp_image_params *params)
{
    return 0;
}

static int map_image(struct gl_hwdec *hw, struct mp_image *hw_image,
                     GLuint *out_textures)
{
    if (!check_hwdec(hw))
        return -1;

    struct priv *p = hw->priv;
    struct vo *vo = hw->mpgl->vo;
    GL *gl = hw->mpgl->gl;

    CVPixelBufferRelease(p->pbuf);
    p->pbuf = (CVPixelBufferRef)hw_image->planes[3];
    CVPixelBufferRetain(p->pbuf);
    IOSurfaceRef surface = CVPixelBufferGetIOSurface(p->pbuf);

    gl->BindTexture(hw->gl_texture_target, p->gl_texture);

    CGLError err = CGLTexImageIOSurface2D(
        vo_cocoa_cgl_context(vo), hw->gl_texture_target, GL_RGB,
        CVPixelBufferGetWidth(p->pbuf), CVPixelBufferGetHeight(p->pbuf),
        GL_RGB_422_APPLE, GL_UNSIGNED_SHORT_8_8_APPLE, surface, 0);

    if (err != kCGLNoError)
        MP_ERR(vo, "error creating IOSurface texture: %s (%x)\n",
               CGLErrorString(err), gl->GetError());

    gl->BindTexture(hw->gl_texture_target, 0);

    out_textures[0] = p->gl_texture;
    return 0;
}

static void unmap_image(struct gl_hwdec *hw) { }

static struct mp_image *download_image(struct gl_hwdec *hw)
{
    struct priv *p = hw->priv;
    CVPixelBufferLockBaseAddress(p->pbuf, 0);
    void *base = CVPixelBufferGetBaseAddress(p->pbuf);
    size_t width  = CVPixelBufferGetWidth(p->pbuf);
    size_t height = CVPixelBufferGetHeight(p->pbuf);
    size_t stride = CVPixelBufferGetBytesPerRow(p->pbuf);

    struct mp_image img = {0};
    mp_image_setfmt(&img, IMGFMT_UYVY);
    mp_image_set_size(&img, width, height);
    img.planes[0] = base;
    img.stride[0] = stride;

    struct mp_image *image = mp_image_new_copy(&img);
    CVPixelBufferUnlockBaseAddress(p->pbuf, 0);

    return image;
}

static void destroy(struct gl_hwdec *hw)
{
    struct priv *p = hw->priv;
    GL *gl = hw->mpgl->gl;

    CVPixelBufferRelease(p->pbuf);
    gl->DeleteTextures(1, &p->gl_texture);
    p->gl_texture = 0;
}


const struct gl_hwdec_driver gl_hwdec_vda = {
    .api_name = "vda",
    .imgfmt = IMGFMT_VDA,
    .create = create,
    .reinit = reinit,
    .map_image = map_image,
    .unmap_image = unmap_image,
    .download_image = download_image,
    .destroy = destroy,
};