aboutsummaryrefslogtreecommitdiff
path: root/src/capture/capture.c
blob: f775d017f29d5c9581f4a9e92d184687cf70d1fb (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "../../include/capture/capture.h"
#include "../../include/egl.h"
#include "../../include/cuda.h"
#include <stdio.h>
#include <stdint.h>
#include <va/va.h>
#include <va/va_drmcommon.h>
#include <libavutil/frame.h>
#include <libavcodec/avcodec.h>

#define FOURCC_NV12 842094158
#define FOURCC_P010 808530000

int gsr_capture_start(gsr_capture *cap, AVCodecContext *video_codec_context, AVFrame *frame) {
    if(cap->started)
        return -1;

    int res = cap->start(cap, video_codec_context, frame);
    if(res == 0)
        cap->started = true;

    return res;
}

void gsr_capture_tick(gsr_capture *cap, AVCodecContext *video_codec_context) {
    if(!cap->started) {
        fprintf(stderr, "gsr error: gsp_capture_tick failed: the gsr capture has not been started\n");
        return;
    }

    if(cap->tick)
        cap->tick(cap, video_codec_context);
}

bool gsr_capture_should_stop(gsr_capture *cap, bool *err) {
    if(!cap->started) {
        fprintf(stderr, "gsr error: gsr_capture_should_stop failed: the gsr capture has not been started\n");
        return false;
    }

    if(!cap->should_stop)
        return false;

    return cap->should_stop(cap, err);
}

int gsr_capture_capture(gsr_capture *cap, AVFrame *frame) {
    if(!cap->started) {
        fprintf(stderr, "gsr error: gsr_capture_capture failed: the gsr capture has not been started\n");
        return -1;
    }
    return cap->capture(cap, frame);
}

void gsr_capture_end(gsr_capture *cap, AVFrame *frame) {
    if(!cap->started) {
        fprintf(stderr, "gsr error: gsr_capture_end failed: the gsr capture has not been started\n");
        return;
    }

    if(!cap->capture_end)
        return;

    cap->capture_end(cap, frame);
}

void gsr_capture_destroy(gsr_capture *cap, AVCodecContext *video_codec_context) {
    cap->destroy(cap, video_codec_context);
}

static uint32_t fourcc(uint32_t a, uint32_t b, uint32_t c, uint32_t d) {
    return (d << 24) | (c << 16) | (b << 8) | a;
}

bool gsr_capture_base_setup_vaapi_textures(gsr_capture_base *self, AVFrame *frame, gsr_egl *egl, VADisplay va_dpy, VADRMPRIMESurfaceDescriptor *prime, gsr_color_range color_range) {
    const int res = av_hwframe_get_buffer(self->video_codec_context->hw_frames_ctx, frame, 0);
    if(res < 0) {
        fprintf(stderr, "gsr error: gsr_capture_kms_setup_vaapi_textures: av_hwframe_get_buffer failed: %d\n", res);
        return false;
    }

    VASurfaceID target_surface_id = (uintptr_t)frame->data[3];

    VAStatus va_status = vaExportSurfaceHandle(va_dpy, target_surface_id, VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2, VA_EXPORT_SURFACE_WRITE_ONLY | VA_EXPORT_SURFACE_SEPARATE_LAYERS, prime);
    if(va_status != VA_STATUS_SUCCESS) {
        fprintf(stderr, "gsr error: gsr_capture_kms_setup_vaapi_textures: vaExportSurfaceHandle failed, error: %d\n", va_status);
        return false;
    }
    vaSyncSurface(va_dpy, target_surface_id);

    egl->glGenTextures(1, &self->input_texture);
    egl->glBindTexture(GL_TEXTURE_2D, self->input_texture);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    egl->glBindTexture(GL_TEXTURE_2D, 0);

    egl->glGenTextures(1, &self->cursor_texture);
    egl->glBindTexture(GL_TEXTURE_2D, self->cursor_texture);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    egl->glBindTexture(GL_TEXTURE_2D, 0);

    const uint32_t formats_nv12[2] = { fourcc('R', '8', ' ', ' '), fourcc('G', 'R', '8', '8') };
    const uint32_t formats_p010[2] = { fourcc('R', '1', '6', ' '), fourcc('G', 'R', '3', '2') };

    if(prime->fourcc == FOURCC_NV12 || prime->fourcc == FOURCC_P010) {
        const uint32_t *formats = prime->fourcc == FOURCC_NV12 ? formats_nv12 : formats_p010;
        const int div[2] = {1, 2}; // divide UV texture size by 2 because chroma is half size

        egl->glGenTextures(2, self->target_textures);
        for(int i = 0; i < 2; ++i) {
            const int layer = i;
            const int plane = 0;

            //const uint64_t modifier = prime->objects[prime->layers[layer].object_index[plane]].drm_format_modifier;

            const intptr_t img_attr[] = {
                EGL_LINUX_DRM_FOURCC_EXT,       formats[i],
                EGL_WIDTH,                      prime->width / div[i],
                EGL_HEIGHT,                     prime->height / div[i],
                EGL_DMA_BUF_PLANE0_FD_EXT,      prime->objects[prime->layers[layer].object_index[plane]].fd,
                EGL_DMA_BUF_PLANE0_OFFSET_EXT,  prime->layers[layer].offset[plane],
                EGL_DMA_BUF_PLANE0_PITCH_EXT,   prime->layers[layer].pitch[plane],
                // TODO:
                //EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT, modifier & 0xFFFFFFFFULL,
                //EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT, modifier >> 32ULL,
                EGL_NONE
            };

            while(egl->eglGetError() != EGL_SUCCESS){}
            EGLImage image = egl->eglCreateImage(egl->egl_display, 0, EGL_LINUX_DMA_BUF_EXT, NULL, img_attr);
            if(!image) {
                fprintf(stderr, "gsr error: gsr_capture_kms_setup_vaapi_textures: failed to create egl image from drm fd for output drm fd, error: %d\n", egl->eglGetError());
                return false;
            }

            egl->glBindTexture(GL_TEXTURE_2D, self->target_textures[i]);
            egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
            egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
            egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

            while(egl->glGetError()) {}
            while(egl->eglGetError() != EGL_SUCCESS){}
            egl->glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, image);
            if(egl->glGetError() != 0 || egl->eglGetError() != EGL_SUCCESS) {
                // TODO: Get the error properly
                fprintf(stderr, "gsr error: gsr_capture_kms_setup_vaapi_textures: failed to bind egl image to gl texture, error: %d\n", egl->eglGetError());
                egl->eglDestroyImage(egl->egl_display, image);
                egl->glBindTexture(GL_TEXTURE_2D, 0);
                return false;
            }

            egl->eglDestroyImage(egl->egl_display, image);
            egl->glBindTexture(GL_TEXTURE_2D, 0);
        }

        gsr_color_conversion_params color_conversion_params = {0};
        color_conversion_params.color_range = color_range;
        color_conversion_params.egl = egl;
        color_conversion_params.source_color = GSR_SOURCE_COLOR_RGB;
        if(prime->fourcc == FOURCC_NV12)
            color_conversion_params.destination_color = GSR_DESTINATION_COLOR_NV12;
        else
            color_conversion_params.destination_color = GSR_DESTINATION_COLOR_P010;

        color_conversion_params.destination_textures[0] = self->target_textures[0];
        color_conversion_params.destination_textures[1] = self->target_textures[1];
        color_conversion_params.num_destination_textures = 2;

        if(gsr_color_conversion_init(&self->color_conversion, &color_conversion_params) != 0) {
            fprintf(stderr, "gsr error: gsr_capture_kms_setup_vaapi_textures: failed to create color conversion\n");
            return false;
        }

        return true;
    } else {
        fprintf(stderr, "gsr error: gsr_capture_kms_setup_vaapi_textures: unexpected fourcc %u for output drm fd, expected nv12 or p010\n", prime->fourcc);
        return false;
    }
}

static unsigned int gl_create_texture(gsr_egl *egl, int width, int height, int internal_format, unsigned int format) {
    unsigned int texture_id = 0;
    egl->glGenTextures(1, &texture_id);
    egl->glBindTexture(GL_TEXTURE_2D, texture_id);
    egl->glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, NULL);

    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    egl->glBindTexture(GL_TEXTURE_2D, 0);
    return texture_id;
}

static bool cuda_register_opengl_texture(gsr_cuda *cuda, CUgraphicsResource *cuda_graphics_resource, CUarray *mapped_array, unsigned int texture_id) {
    CUresult res;
    CUcontext old_ctx;
    res = cuda->cuCtxPushCurrent_v2(cuda->cu_ctx);
    res = cuda->cuGraphicsGLRegisterImage(cuda_graphics_resource, texture_id, GL_TEXTURE_2D, CU_GRAPHICS_REGISTER_FLAGS_NONE);
    if (res != CUDA_SUCCESS) {
        const char *err_str = "unknown";
        cuda->cuGetErrorString(res, &err_str);
        fprintf(stderr, "gsr error: cuda_register_opengl_texture: cuGraphicsGLRegisterImage failed, error: %s, texture " "id: %u\n", err_str, texture_id);
        res = cuda->cuCtxPopCurrent_v2(&old_ctx);
        return false;
    }

    res = cuda->cuGraphicsResourceSetMapFlags(*cuda_graphics_resource, CU_GRAPHICS_MAP_RESOURCE_FLAGS_NONE);
    res = cuda->cuGraphicsMapResources(1, cuda_graphics_resource, 0);

    res = cuda->cuGraphicsSubResourceGetMappedArray(mapped_array, *cuda_graphics_resource, 0, 0);
    res = cuda->cuCtxPopCurrent_v2(&old_ctx);
    return true;
}

bool gsr_capture_base_setup_cuda_textures(gsr_capture_base *base, AVFrame *frame, gsr_cuda_context *cuda_context, gsr_egl *egl, gsr_color_range color_range, bool hdr) {
    // TODO:
    const int res = av_hwframe_get_buffer(base->video_codec_context->hw_frames_ctx, frame, 0);
    if(res < 0) {
        fprintf(stderr, "gsr error: gsr_capture_kms_setup_cuda_textures: av_hwframe_get_buffer failed: %d\n", res);
        return false;
    }

    egl->glGenTextures(1, &base->input_texture);
    egl->glBindTexture(GL_TEXTURE_2D, base->input_texture);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    egl->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    egl->glBindTexture(GL_TEXTURE_2D, 0);

    egl->glGenTextures(1, &base->cursor_texture);
    egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, base->cursor_texture);
    egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    egl->glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    egl->glBindTexture(GL_TEXTURE_EXTERNAL_OES, 0);

    const unsigned int internal_formats_nv12[2] = { GL_R8, GL_RG8 };
    const unsigned int internal_formats_p010[2] = { GL_R16, GL_RG16 };
    const unsigned int formats[2] = { GL_RED, GL_RG };
    const int div[2] = {1, 2}; // divide UV texture size by 2 because chroma is half size

    for(int i = 0; i < 2; ++i) {
        base->target_textures[i] = gl_create_texture(egl, base->video_codec_context->width / div[i], base->video_codec_context->height / div[i], !hdr ? internal_formats_nv12[i] : internal_formats_p010[i], formats[i]);
        if(base->target_textures[i] == 0) {
            fprintf(stderr, "gsr error: gsr_capture_kms_setup_cuda_textures: failed to create opengl texture\n");
            return false;
        }

        if(!cuda_register_opengl_texture(cuda_context->cuda, &cuda_context->cuda_graphics_resources[i], &cuda_context->mapped_arrays[i], base->target_textures[i])) {
            return false;
        }
    }

    gsr_color_conversion_params color_conversion_params = {0};
    color_conversion_params.color_range = color_range;
    color_conversion_params.egl = egl;
    color_conversion_params.source_color = GSR_SOURCE_COLOR_RGB;
    if(!hdr)
        color_conversion_params.destination_color = GSR_DESTINATION_COLOR_NV12;
    else
        color_conversion_params.destination_color = GSR_DESTINATION_COLOR_P010;

    color_conversion_params.destination_textures[0] = base->target_textures[0];
    color_conversion_params.destination_textures[1] = base->target_textures[1];
    color_conversion_params.num_destination_textures = 2;

    if(gsr_color_conversion_init(&base->color_conversion, &color_conversion_params) != 0) {
        fprintf(stderr, "gsr error: gsr_capture_kms_setup_cuda_textures: failed to create color conversion\n");
        return false;
    }

    return true;
}

void gsr_capture_base_stop(gsr_capture_base *self, gsr_egl *egl) {
    gsr_color_conversion_deinit(&self->color_conversion);

    if(egl->egl_context) {
        if(self->input_texture) {
            egl->glDeleteTextures(1, &self->input_texture);
            self->input_texture = 0;
        }

        if(self->cursor_texture) {
            egl->glDeleteTextures(1, &self->cursor_texture);
            self->cursor_texture = 0;
        }

        egl->glDeleteTextures(2, self->target_textures);
        self->target_textures[0] = 0;
        self->target_textures[1] = 0;
    }
}