/
opengl.c
324 lines (266 loc) · 8.69 KB
/
opengl.c
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*****************************************************************************
* opengl.c: OpenGL filter
*****************************************************************************
* Copyright (C) 2020 Videolabs
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_modules.h>
#include <vlc_picture.h>
#include <vlc_filter.h>
#include <vlc_opengl.h>
#include <vlc_vout_window.h>
#include <vlc_vout_display.h>
#include <vlc_atomic.h>
#include "../video_output/opengl/vout_helper.h"
#include "../video_output/opengl/filters.h"
#include "../video_output/opengl/gl_api.h"
#include "../video_output/opengl/gl_common.h"
#include "../video_output/opengl/interop.h"
#define OPENGL_CFG_PREFIX "opengl-"
static const char *const opengl_options[] = { "filter", NULL };
typedef struct
{
vlc_gl_t *gl;
struct vlc_gl_filters *filters;
struct vlc_gl_interop *interop;
struct vlc_gl_api api;
} filter_sys_t;
static picture_t *Filter(filter_t *filter, picture_t *input)
{
filter_sys_t *sys = filter->p_sys;
if (vlc_gl_MakeCurrent(sys->gl) != VLC_SUCCESS)
return NULL;
int ret = vlc_gl_filters_UpdatePicture(sys->filters, input);
if (ret != VLC_SUCCESS)
{
vlc_gl_ReleaseCurrent(sys->gl);
return NULL;
}
ret = vlc_gl_filters_Draw(sys->filters);
if (ret != VLC_SUCCESS)
{
vlc_gl_ReleaseCurrent(sys->gl);
return NULL;
}
picture_t *output = vlc_gl_SwapOffscreen(sys->gl);
vlc_gl_ReleaseCurrent(sys->gl);
if (output == NULL)
goto end;
output->date = input->date;
output->b_force = input->b_force;
output->b_still = input->b_still;
output->format.i_frame_rate =
filter->fmt_out.video.i_frame_rate;
output->format.i_frame_rate_base =
filter->fmt_out.video.i_frame_rate_base;
end:
picture_Release(input);
return output;
}
static int
AppendVFlip(struct vlc_gl_filters *filters)
{
config_chain_t *cfg = NULL;
char *name;
char *leftover = config_ChainCreate(&name, &cfg, "draw{vflip}");
free(leftover);
free(name);
/* The OpenGL filter will do the real job, this file is just a filter_t
* wrapper */
struct vlc_gl_filter *glfilter =
vlc_gl_filters_Append(filters, "draw", cfg);
config_ChainDestroy(cfg);
if (!glfilter)
return VLC_EGENERIC;
return VLC_SUCCESS;
}
static int
LoadFilters(filter_sys_t *sys, const char *glfilters_config)
{
struct vlc_gl_filters *filters = sys->filters;
assert(glfilters_config);
const char *string = glfilters_config;
char *next_module = NULL;
do
{
char *name;
config_chain_t *config = NULL;
char *leftover = config_ChainCreate(&name, &config, string);
free(next_module);
next_module = leftover;
string = next_module; /* const view of next_module */
if (name)
{
struct vlc_gl_filter *filter =
vlc_gl_filters_Append(filters, name, config);
config_ChainDestroy(config);
if (!filter)
{
msg_Err(sys->gl, "Could not load GL filter: %s", name);
free(name);
return VLC_EGENERIC;
}
free(name);
}
} while (string);
return VLC_SUCCESS;
}
static void Close( filter_t *filter )
{
filter_sys_t *sys = filter->p_sys;
if (sys != NULL)
{
vlc_gl_MakeCurrent(sys->gl);
vlc_gl_filters_Delete(sys->filters);
vlc_gl_interop_Delete(sys->interop);
vlc_gl_ReleaseCurrent(sys->gl);
vlc_gl_Release(sys->gl);
free(sys);
}
}
static int Open( vlc_object_t *obj )
{
filter_t *filter = (filter_t *)obj;
filter_sys_t *sys
= filter->p_sys
= malloc(sizeof *sys);
if (sys == NULL)
return VLC_ENOMEM;
unsigned width = filter->fmt_out.video.i_visible_width;
unsigned height = filter->fmt_out.video.i_visible_height;
// TODO: other than BGRA format ?
#ifdef USE_OPENGL_ES2
# define VLCGLAPI VLC_OPENGL_ES2
#else
# define VLCGLAPI VLC_OPENGL
#endif
struct vlc_decoder_device *device = filter_HoldDecoderDevice(filter);
sys->gl = vlc_gl_CreateOffscreen(obj, device, width, height, VLCGLAPI,
NULL);
/* The vlc_gl_t instance must have hold the device if it needs it. */
if (device)
vlc_decoder_device_Release(device);
if (sys->gl == NULL)
{
msg_Err(obj, "Failed to create opengl context\n");
goto gl_create_failure;
}
if (vlc_gl_MakeCurrent (sys->gl) != VLC_SUCCESS)
{
msg_Err(obj, "Failed to gl make current");
assert(false);
goto make_current_failure;
}
struct vlc_gl_api *api = &sys->api;
if (vlc_gl_api_Init(api, sys->gl) != VLC_SUCCESS)
{
msg_Err(obj, "Failed to init vlc_gl_api");
goto gl_api_failure;
}
sys->interop = vlc_gl_interop_New(sys->gl, api, filter->vctx_in,
&filter->fmt_in.video);
if (!sys->interop)
{
msg_Err(obj, "Could not create interop");
goto gl_interop_failure;
}
config_ChainParse(filter, OPENGL_CFG_PREFIX, opengl_options, filter->p_cfg);
char *glfilters_config =
var_InheritString(filter, OPENGL_CFG_PREFIX "filter");
if (!glfilters_config)
{
msg_Err(obj, "No filters requested");
goto filter_config_failure;
}
sys->filters = vlc_gl_filters_New(sys->gl, api, sys->interop);
if (!sys->filters)
{
msg_Err(obj, "Could not create filters");
free(glfilters_config);
goto filters_new_failure;
}
int ret;
ret = LoadFilters(sys, glfilters_config);
if (ret != VLC_SUCCESS)
{
msg_Err(obj, "Could not load filters: %s", glfilters_config);
free(glfilters_config);
goto filters_load_failure;
}
free(glfilters_config);
if (sys->gl->offscreen_vflip)
{
/* OpenGL renders upside-down, add a filter to get the pixels in the
* normal orientation */
ret = AppendVFlip(sys->filters);
if (ret != VLC_SUCCESS)
return VLC_EGENERIC;
}
ret = vlc_gl_filters_InitFramebuffers(sys->filters);
if (ret != VLC_SUCCESS)
{
msg_Err(obj, "Could not init filters framebuffers");
goto init_framebuffer_failure;
}
vlc_gl_filters_SetViewport(sys->filters, 0, 0, filter->fmt_out.video.i_visible_width, filter->fmt_out.video.i_visible_height);
vlc_gl_ReleaseCurrent(sys->gl);
static const struct vlc_filter_operations ops = {
.filter_video = Filter,
.close = Close,
};
filter->ops = &ops;
filter->fmt_out.video.orientation = ORIENT_NORMAL;
filter->fmt_out.video.i_chroma
= filter->fmt_out.i_codec
= sys->gl->offscreen_chroma_out;
filter->fmt_out.video.i_frame_rate =
filter->fmt_in.video.i_frame_rate;
filter->fmt_out.video.i_frame_rate_base =
filter->fmt_in.video.i_frame_rate_base;
assert(filter->fmt_out.video.i_frame_rate_base != 0);
filter->vctx_out = sys->gl->offscreen_vctx_out;
return VLC_SUCCESS;
init_framebuffer_failure:
filters_load_failure:
vlc_gl_filters_Delete(sys->filters);
filters_new_failure:
filter_config_failure:
vlc_gl_interop_Delete(sys->interop);
gl_interop_failure:
gl_api_failure:
vlc_gl_ReleaseCurrent(sys->gl);
make_current_failure:
vlc_gl_Release(sys->gl);
gl_create_failure:
free(sys);
return VLC_EGENERIC;
}
vlc_module_begin()
set_shortname( N_("opengl") )
set_description( N_("Opengl filter executor") )
set_category( CAT_VIDEO )
set_subcategory( SUBCAT_VIDEO_VFILTER )
set_capability( "video filter", 0 )
add_shortcut( "opengl" )
set_callback( Open )
add_module_list( "opengl-filter", "opengl filter", NULL,
"opengl filter", "List of OpenGL filters to execute" )
vlc_module_end()