-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFlashes.cpp
More file actions
344 lines (295 loc) · 13.9 KB
/
Flashes.cpp
File metadata and controls
344 lines (295 loc) · 13.9 KB
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//////////////////////////////////////////
// This file contains a simple flashes
// skeleton you can use to get started.
// With no changes it simply passes
// frames through.
#include "VapourSynth.h"
#include "VSHelper.h"
typedef struct {
VSNodeRef *node;
const VSVideoInfo *vi;
int StartFrame;
int EndFrame;
int xcoord;
int ycoord;
int radmax; // max radius to which the smoke cloud expand
// float Tsmoke; // smoke filling time in seconds
// float Tflash; // flash duration seconds
int nFlashes;
int nSmoke;
int ntFlash;
int* coord;
} FlashesData;
static void VS_CC flashesInit(VSMap *in, VSMap *out, void **instanceData, VSNode *node, VSCore *core, const VSAPI *vsapi) {
FlashesData *d = (FlashesData *) * instanceData;
vsapi->setVideoInfo(d->vi, 1, node);
d-> nFlashes = 200;
d->coord = (int *)vs_aligned_malloc(sizeof(int) * d->nFlashes, 32);
srand(((d->EndFrame - d->StartFrame) * (d->EndFrame - d->StartFrame)
* (d->EndFrame - d->StartFrame)) | 1);
for (int i = 0; i < d->nFlashes; i++)
{
// flash coordinates - rad to + rad wrt cloud center
d->coord[i] = - d->radmax + rand() % (2 * d->radmax);
}
}
static const VSFrameRef *VS_CC flashesGetFrame(int in, int activationReason, void **instanceData, void **frameData, VSFrameContext *frameCtx, VSCore *core, const VSAPI *vsapi) {
FlashesData *d = (FlashesData *) * instanceData;
if (activationReason == arInitial) {
vsapi->requestFrameFilter(in, d->node, frameCtx);
} else if (activationReason == arAllFramesReady) {
const VSFrameRef* src = vsapi->getFrameFilter(in, d->node, frameCtx);
if (in < d->StartFrame || in > d->EndFrame)
{
return src;
}
int n = in - d->StartFrame;
const VSFormat* fi = d->vi->format;
int ht = d->vi->height;
int wd = d->vi->width;
VSFrameRef* dst = vsapi->copyFrame(src, core);
const unsigned char* sp[] = { NULL, NULL, NULL };
unsigned char* dp[] = { NULL, NULL, NULL };
int subH[] = { 0, fi->subSamplingH, fi->subSamplingH };
int subW[] = { 0, fi->subSamplingW, fi->subSamplingW };
int andH = (1 << subH[1]) - 1;
int andW = (1 << subW[1]) - 1;
int pitch[] = { 0,0,0 };
int nbytes = fi->bytesPerSample;
int nbits = fi->bitsPerSample;
int np = fi->numPlanes > 3 ? 3 : fi->numPlanes;
for (int p = 0; p < np; p++)
{
sp[p] = vsapi->getReadPtr(src, p);
dp[p] = vsapi->getWritePtr(dst, p);
pitch[p] = vsapi->getStride(dst, p) / nbytes;
}
// get upto 24 flashes in a frame lasting for 1 second each. Each frame starts new flashes
// first 4 seconds frames smoke to fill area. Flashes to start then
int nSmoke = d->nSmoke; // number of frames to fill smoke
int flash_duration = d->ntFlash;
int smoke_radius = n < nSmoke ? (n * d->radmax) / nSmoke : d->radmax;
int index = (n - nSmoke);
int nflashes = 2 * (n > nSmoke ? (n - nSmoke > flash_duration ? flash_duration : n - nSmoke) : 0);
int fcycle = flash_duration / 4; // 4 times the point flashes before disappearing
int add[] = { 0, 127, 127 }; // required for float data
// fill with smoke
for (int h = d->ycoord - smoke_radius; h < d->ycoord + smoke_radius; h++)
{
if (h > 2 && h < ht - 2)
{
for (int w = d->xcoord - smoke_radius; w < d->xcoord + smoke_radius; w++)
{
if (w > 2 && w < wd - 2)
{
// to create wooly effect
int delta = smoke_radius - rand() % (2 + smoke_radius / 8); // to create hazy boundary
if ((w - d->xcoord) * (w - d->xcoord) + (h - d->ycoord) * (h - d->ycoord) <= delta * delta)
{
uint8_t grey[] = { (uint8_t)((rand() % 60) + 60), (uint8_t)127, (uint8_t)127 };// for changing smoke grey value
if (fi->colorFamily == cmRGB)
{
grey[1] = grey[0], grey[2] = grey[0];
}
for (int p = 0; p < np; p++)
{
if (nbytes == 1)
*(dp[p] + (h >> subH[p]) * pitch[p] + (w >> subW[p])) = grey[p];
else if (nbytes == 2)
*((uint16_t*)(dp[p]) + (h >> subH[p]) * pitch[p] + (w >> subW[p])) = (uint16_t)((int)grey[p] << (nbits - 8));
else if (nbytes == 4)
*((float*)(dp[p]) + (h >> subH[p]) * pitch[p] + (w >> subW[p])) = (grey[p] - add[p]) / 256.0f;
}
}
}
}
}
}
for (int f = 0; f < nflashes; f++)
{
int factor = (abs(flash_duration / 2 - f)) % fcycle; // flashing color multiplier
unsigned char *color;
unsigned char coloryuv[] = { 0,0,0 };
unsigned char colorrgb[] = { (uint8_t)(255 - (factor * 100) / (fcycle)), (uint8_t)20, (uint8_t)20 };
// unable to understand logic. Internally supposed to be in B,G,R order
// but gives wrong colors. so this illogical conversion
if (fi->colorFamily == cmYUV)
{
unsigned char colorbgr[] = { (uint8_t)20, (uint8_t)20, (uint8_t)(255 - (factor * 100) / (fcycle)) };
BGR8YUV(coloryuv, colorbgr);
color = coloryuv;
}
else
{
color = colorrgb;
}
int m = (index - f) % d->nFlashes; // for y
int nf = (m + 1) % d->nFlashes; // for x this way effectively double number of random coordinates
int w = (d->xcoord + d->coord[nf]) & 0xfffffffe;
int h = (d->ycoord + d->coord[m]) & 0xfffffffe;
// ensure flashes occur within 7/8 th of smoke cloud
if (d->coord[m] * d->coord[m] + d->coord[nf] * d->coord[nf] <= (7 * d->radmax * 7 * d->radmax) / 64) // within smoke
{
if (d->ycoord + d->coord[m] > 8 && d->ycoord + d->coord[m] < ht - 8) // within frame height
{
if (d->xcoord + d->coord[nf] > 8 && d->xcoord + d->coord[nf] < wd - 8) // within frame width
{
// body of flash
for (int i = 0; i < 8; i++)
{
for (int p = 0; p < np; p++)
{
if (fi->sampleType == stInteger && nbytes == 1)
{
*(dp[p] + ((h + i) >> subH[p]) * pitch[p] + (w >> subW[p]))
= color[p];
*(dp[p] + (h >> subH[p]) * pitch[p] + ((w + i) >> subW[p]))
= color[p];
*(dp[p] + ((h + i) >> subH[p]) * pitch[p] + ((w + i) >> subW[p]))
= color[p];
*(dp[p] + ((h + 8 - i) >> subH[p]) * pitch[p] + ((w + i) >> subW[p]))
= color[p];
}
else if (fi->sampleType == stInteger && nbytes == 2)
{
*( (uint16_t*)(dp[p]) + ((h + i) >> subH[p]) * pitch[p] + (w >> subW[p]))
= (uint16_t)(((int)color[p]) << (nbits - 8));
*((uint16_t*)(dp[p]) + (h >> subH[p]) * pitch[p] + ((w + i) >> subW[p]))
= (uint16_t)(((int)color[p]) << (nbits - 8));
*((uint16_t*)(dp[p]) + ((h + i) >> subH[p]) * pitch[p] + ((w + i) >> subW[p]))
= (uint16_t)(((int)color[p]) << (nbits - 8));
*((uint16_t*)(dp[p]) + ((h + 8 - i) >> subH[p]) * pitch[p] + ((w + i) >> subW[p]))
= (uint16_t)(((int)color[p]) << (nbits - 8));
}
else if (fi->sampleType == stFloat && nbytes == 4)
{
float col = color[p] / 255.0f;;
if (fi->colorFamily == cmYUV)
if (p == 0)
col = (color[p] - 16) / 235.0f;
else
col = (color[p] - 128)/ 235.0f;
*((float*)(dp[p]) + ((h + i) >> subH[p]) * pitch[p] + (w >> subW[p]))
= (col);
*((float*)(dp[p]) + (h >> subH[p]) * pitch[p] + ((w + i) >> subW[p]))
= (col);
*((float*)(dp[p]) + ((h + i) >> subH[p]) * pitch[p] + ((w + i) >> subW[p]))
= (col);
*((float*)(dp[p]) + ((h + 8 - i) >> subH[p]) * pitch[p] + ((w + i) >> subW[p]))
= (col);
}
}
}
}
}
}
}
vsapi->freeFrame(src);
return dst;
}
return 0;
}
static void VS_CC flashesFree(void *instanceData, VSCore *core, const VSAPI *vsapi) {
FlashesData *d = (FlashesData *)instanceData;
vsapi->freeNode(d->node);
vs_aligned_free(d->coord);
free(d);
}
static void VS_CC flashesCreate(const VSMap *in, VSMap *out, void *userData, VSCore *core, const VSAPI *vsapi) {
FlashesData d;
FlashesData *data;
int err;
d.node = vsapi->propGetNode(in, "clip", 0, 0);
d.vi = vsapi->getVideoInfo(d.node);
const VSFormat* fi = d.vi->format;
if (fi->colorFamily != cmRGB && fi->colorFamily != cmYUV && fi->colorFamily != cmGray
&& fi->sampleType != stInteger && fi->sampleType != stFloat && !isConstantFormat(d.vi))
{
vsapi->setError(out, "Flashes: only RGB, YUV, Gray constant formats, with integer or float samples as Input allowed ");
vsapi->freeNode(d.node);
return;
}
d.StartFrame = int64ToIntS(vsapi->propGetInt(in, "sf", 0, &err));
if (err)
d.StartFrame = 0;
else if (d.StartFrame < 0 || d.StartFrame > d.vi->numFrames - 1)
{
vsapi->setError(out, "Flashes: sf must be within video");
vsapi->freeNode(d.node);
return;
}
d.EndFrame = int64ToIntS(vsapi->propGetInt(in, "ef", 0, &err));
if (err)
d.EndFrame = d.vi->numFrames - 1;
else if (d.EndFrame < 0 || d.EndFrame > d.vi->numFrames - 1 || d.EndFrame < d.StartFrame)
{
vsapi->setError(out, "Flashes: ef must be within video and not less than sf");
vsapi->freeNode(d.node);
return;
}
d.xcoord = int64ToIntS(vsapi->propGetInt(in, "x", 0, &err));
if (err)
d.xcoord = d.vi->width / 2;
else if (d.xcoord < 0 || d.xcoord > d.vi->width - 1 )
{
vsapi->setError(out, "Flashes: x must be within frame");
vsapi->freeNode(d.node);
return;
}
d.ycoord = int64ToIntS(vsapi->propGetInt(in, "y", 0, &err));
if (err)
d.ycoord = d.vi->height / 2;
else if (d.ycoord < 0 || d.ycoord > d.vi->height - 1)
{
vsapi->setError(out, "Flashes: y must be within frame");
vsapi->freeNode(d.node);
return;
}
int rmax = d.vi->width > d.vi->height ? d.vi->height / 2 : d.vi->width / 2;
d.radmax = int64ToIntS(vsapi->propGetInt(in, "rmax", 0, &err));
if (err)
d.radmax = rmax / 2;
else if (d.radmax < 8 || d.radmax > rmax)
{
vsapi->setError(out, "Flashes: rmax value can be 8 to 1/2 of smaller dimension of frame");
vsapi->freeNode(d.node);
return;
}
float tmax = (float)( ((d.EndFrame - d.StartFrame) * d.vi->fpsNum) / d.vi->fpsDen);
float ts = (float)(vsapi->propGetFloat(in, "ts", 0, &err));
if (err)
d.nSmoke = (int)(2.0 * d.vi->fpsNum / d.vi->fpsDen);
else if (ts < 0 || ts > tmax)
{
vsapi->setError(out, "Flashes: ts time in seconds for smoke cloud to expand to max rmax must be within effect duration in seconds");
vsapi->freeNode(d.node);
return;
}
else
d.nSmoke = (int)ts;
ts = (float)(vsapi->propGetFloat(in, "tf", 0, &err));
if (err)
d.ntFlash = (int)(0.25 * d.vi->fpsNum / d.vi->fpsDen);
else if (ts < 0 || ts > tmax)
{
vsapi->setError(out, "Flashes: tf time in seconds each flash lasts must be 0.25 to 1.0 seconds");
vsapi->freeNode(d.node);
return;
}
else d.ntFlash = (int)ts;
if ( d.ntFlash + d.nSmoke > d.EndFrame - d.StartFrame)
{
vsapi->setError(out, "Flashes: tf + ts must be at least cover duration of effect");
vsapi->freeNode(d.node);
return;
}
data = (FlashesData*)malloc(sizeof(d));
*data = d;
vsapi->createFilter(in, out, "Flashes", flashesInit, flashesGetFrame, flashesFree, fmParallel, 0, data, core);
}
//////////////////////////////////////////
/*
VS_EXTERNAL_API(void) VapourSynthPluginInit(VSConfigPlugin configFunc, VSRegisterFunction registerFunc, VSPlugin *plugin) {
configFunc("com.flashes.vfx", "Flashes", "VapourSynth vfx plugin", VAPOURSYNTH_API_VERSION, 1, plugin);
registerFunc("Flashes", "clip:clip;sf:int:opt;ef:int:opt;x:int:opt;y:int:opt;rmax:int:opt;ts:float:opt;tf:int:opt;", flashesCreate, 0, plugin);
}*/