-
Notifications
You must be signed in to change notification settings - Fork 1
/
screen_flash_filter.c
310 lines (221 loc) · 8.54 KB
/
screen_flash_filter.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
/*****************************************************************************
+Copyright (C) 2017 by Eric Rasmuson <[email protected]>
+
+This file is part of OBS.
+
+OBS 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.
+
+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 General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*****************************************************************************/
#include <obs-module.h>
#include <obs-source.h>
#include <obs.h>
#include <util/platform.h>
struct screen_flash_filter_data {
obs_source_t *context;
gs_effect_t *effect;
uint32_t cx;
uint32_t cy;
gs_texrender_t *currentRender;
gs_texrender_t *oldRender1;
gs_texrender_t *oldRender2;
gs_texrender_t *oldRender3;
gs_texrender_t *oldRender4;
gs_texrender_t *oldRender5;
gs_texture_t *currentTex1;
gs_texture_t *oldTex1;
gs_texture_t *oldTex2;
gs_texture_t *oldTex3;
gs_texture_t *oldTex4;
gs_texture_t *oldTex5;
float threshold1;
struct vec4 colorTarget;
bool processed_frame;
bool firstRun;
};
static const char *screen_flash_filter_getname(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_module_text("ScreenFlashFilter");
}
static void screen_flash_filter_update(void *data, obs_data_t *settings)
{
struct screen_flash_filter_data *filter = data;
obs_source_t *target = obs_filter_get_target(filter->context);
uint32_t cx;
uint32_t cy;
cx = obs_source_get_base_width(target);
cy = obs_source_get_base_height(target);
filter->cx = cx;
filter->cy = cy;
double threshold1 = obs_data_get_double(settings, "threshold1");
filter->threshold1 = (float)threshold1;
uint32_t CT = (uint32_t)obs_data_get_int(settings,"colorTarget");
vec4_from_rgba(&filter->colorTarget, CT);
}
static void free_textures(struct screen_flash_filter_data *f)
{
obs_enter_graphics();
obs_leave_graphics();
}
static void screen_flash_filter_destroy(void *data)
{
struct screen_flash_filter_data *filter = data;
if (filter->effect) {
obs_enter_graphics();
gs_effect_destroy(filter->effect);
obs_leave_graphics();
}
free_textures(filter);
bfree(data);
}
static void *screen_flash_filter_create(obs_data_t *settings, obs_source_t *context)
{
struct screen_flash_filter_data *filter =
bzalloc(sizeof(struct screen_flash_filter_data));
char *effect_path = obs_module_file("screen_flash_filter.effect");
filter->firstRun = true;
filter->processed_frame = false;
filter->context = context;
obs_enter_graphics();
filter->effect = gs_effect_create_from_file(effect_path, NULL);
if (filter->effect)
{
filter->currentRender = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
filter->currentTex1 = gs_texrender_get_texture(gs_texrender_create(GS_RGBA, GS_ZS_NONE));
filter->oldTex1 = gs_texrender_get_texture(gs_texrender_create(GS_RGBA, GS_ZS_NONE));
filter->oldTex2 = gs_texrender_get_texture(gs_texrender_create(GS_RGBA, GS_ZS_NONE));
filter->oldTex3 = gs_texrender_get_texture(gs_texrender_create(GS_RGBA, GS_ZS_NONE));
filter->oldTex4 = gs_texrender_get_texture(gs_texrender_create(GS_RGBA, GS_ZS_NONE));
filter->oldTex5 = gs_texrender_get_texture(gs_texrender_create(GS_RGBA, GS_ZS_NONE));
}
obs_leave_graphics();
bfree(effect_path);
if (!filter->effect) {
screen_flash_filter_destroy(filter);
return NULL;
}
screen_flash_filter_update(filter, settings);
return filter;
}
static void screen_flash_filter_tick(void *data, float t)
{
UNUSED_PARAMETER(t);
struct screen_flash_filter_data *filter = data;
}
static void screen_flash_filter_render(void *data, gs_effect_t *effect)
{
struct screen_flash_filter_data *filter = data;
obs_source_t *target = obs_filter_get_target(filter->context);
obs_source_t *parent = obs_filter_get_parent(filter->context);
if(filter->currentRender != NULL) filter->currentTex1 = gs_texrender_get_texture(filter->currentRender);
if (filter->oldRender1 != NULL) filter->oldTex1 = gs_texrender_get_texture(filter->oldRender1);
if (filter->oldRender2 != NULL) filter->oldTex2 = gs_texrender_get_texture(filter->oldRender2);
if (filter->oldRender3 != NULL) filter->oldTex3 = gs_texrender_get_texture(filter->oldRender3);
if (filter->oldRender4 != NULL) filter->oldTex4 = gs_texrender_get_texture(filter->oldRender4);
if (filter->oldRender5 != NULL) filter->oldTex5 = gs_texrender_get_texture(filter->oldRender5);
gs_eparam_t *param;
if (!parent || !target)
{
obs_source_skip_video_filter(filter->context);
return;
}
//effect filter
if (!obs_source_process_filter_begin(filter->context, GS_RGBA, OBS_ALLOW_DIRECT_RENDERING))
return;
obs_source_process_filter_begin(filter->context, GS_RGBA, OBS_ALLOW_DIRECT_RENDERING);
param = gs_effect_get_param_by_name(filter->effect, "currentTex1");
gs_effect_set_texture(param, filter->currentTex1);
param = gs_effect_get_param_by_name(filter->effect, "oldTex1");
gs_effect_set_texture(param, filter->oldTex1);
param = gs_effect_get_param_by_name(filter->effect, "oldTex2");
gs_effect_set_texture(param, filter->oldTex2);
param = gs_effect_get_param_by_name(filter->effect, "oldTex3");
gs_effect_set_texture(param, filter->oldTex3);
param = gs_effect_get_param_by_name(filter->effect, "oldTex4");
gs_effect_set_texture(param, filter->oldTex4);
param = gs_effect_get_param_by_name(filter->effect, "oldTex5");
gs_effect_set_texture(param, filter->oldTex5);
param = gs_effect_get_param_by_name(filter->effect, "threshold1");
gs_effect_set_float(param, filter->threshold1);
param = gs_effect_get_param_by_name(filter->effect, "colorTarget");
gs_effect_set_vec4(param, &filter->colorTarget);
obs_source_process_filter_end(filter->context, filter->effect, 0, 0);
uint32_t cx;
uint32_t cy;
cx = obs_source_get_base_width(target);
cy = obs_source_get_base_height(target);
filter->cx = cx;
filter->cy = cy;
filter->currentRender = NULL;
gs_texrender_t *texRender1 = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
filter->currentRender = texRender1;
gs_texrender_reset(filter->currentRender);
gs_blend_state_push();
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
if (gs_texrender_begin(filter->currentRender, filter->cx, filter->cy)) {
uint32_t parent_flags = obs_source_get_output_flags(target);
struct vec4 clear_color;
vec4_zero(&clear_color);
gs_clear(GS_CLEAR_COLOR, &clear_color, 0.0f, 0);
gs_ortho(0.0f, (float)filter->cx, 0.0f, (float)filter->cy,-100.0f, 100.0f);
obs_source_video_render(target);
gs_texrender_end(filter->currentRender);
}
gs_blend_state_pop();
gs_texrender_destroy(filter->oldRender5);
filter->oldRender5 = filter->oldRender4;
filter->oldRender4 = filter->oldRender3;
filter->oldRender3 = filter->oldRender2;
filter->oldRender2 = filter->oldRender1;
filter->oldRender1 = filter->currentRender;
filter->processed_frame = true;
UNUSED_PARAMETER(effect);
}
static obs_properties_t *screen_flash_filter_properties(void *data)
{
obs_properties_t *props = obs_properties_create();
obs_properties_add_color(props, "colorTarget", "Flash color to filter");
obs_properties_add_float_slider(props, "threshold1", "Threshold (Usually between .10 and .30)", 0.0f, 0.3f, 0.01f);
UNUSED_PARAMETER(data);
return props;
}
static void screen_flash_filter_defaults(obs_data_t *settings)
{
/*
<Title> <Platform>
<Color> <Hex> <R> <G> <B>
Time Crisis (PSX)
White 0XFFF8F8F8 248 248 248
Project Horned Owl (PSX)
Blue 0xFF000CC7 0 12 199
Time Crisis 2 (PS2)
Grey 0xFF838383 131 131 131
Point Blank (PSX)
White/Grey 0xFFDEDEDE 222 222 222
*/
obs_data_set_default_double(settings, "threshold1", 0.1);
obs_data_set_default_int(settings, "colorTarget", 0xFFFFFFFF);
}
struct obs_source_info screen_flash_filter = {
.id = "screen_flash_filter",
.type = OBS_SOURCE_TYPE_FILTER,
.output_flags = OBS_SOURCE_VIDEO,
.get_name = screen_flash_filter_getname,
.create = screen_flash_filter_create,
.destroy = screen_flash_filter_destroy,
.update = screen_flash_filter_update,
.video_render = screen_flash_filter_render,
.video_tick = screen_flash_filter_tick,
.get_properties = screen_flash_filter_properties,
.get_defaults = screen_flash_filter_defaults
};