-
Notifications
You must be signed in to change notification settings - Fork 1
/
FQSharphelper.cpp
428 lines (290 loc) · 9.65 KB
/
FQSharphelper.cpp
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/******************************************************************************
FQSharp filter plugin for vapoursynth by V.C.Mohan
This filter operates in freq domain (2d) and blurs or unblurs
linear (motion) or circular (focus) styles
Author V.C.Mohan.
June 2015
Copyright (C) < 2014> <V.C.Mohan>
This program 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, version 3 of the License.
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.
A copy of the GNU General Public License is at
see <http://www.gnu.org/licenses/>.
For details of how to contact author see <http://www.avisynth.org/vcmohan>
********************************************************************************/
void ApplyFilter(fftwf_complex * fout, float * Filter, int wd, int ht);
template <typename finc>
void getRealInput(float *data,const finc * fptr, int pitch,
int wd, int ht, int wpad, int hpad, bool centered);
template <typename finc>
void getRealOutput(float *data, finc * fptr, int pitch,
int wd, int ht, int wpad, bool cent, finc min, finc max);
// int getSign ( int h, int w);
int getSign(int i);
int DrawPSF(float *psf, bool linear, int xval, int yval, int bestx, int besty, float spike = 0.0);
void DesignInverse(fftwf_complex * fout, float * Filter, float wn,
int bestx, int besty, float scale);
//-------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
//Draws the PSF linear or circular at center of the psf buffer
// draws psf in float data
int DrawPSF(float *psf, bool linear, int xval, int yval, int bestx, int besty, float spike)
{
int count;
// zero psf area
for(int h = 0; h < bestx * besty; h ++)
psf[h] = 0.0;
if( linear)
{
int length = abs( yval) > xval? abs(yval) : xval;
count = 2 * length + 1;
// draw the blur line at the center of frames best sizes
if( abs(yval) > xval)
{
if(yval < 0)
{
yval = -yval;
xval = - xval;
}
for(int h = - abs( yval); h <= abs( yval) ; h ++)
{
int w = (h * xval ) / yval; // get nearest integer
int fraction = abs(h * xval ) % abs(yval); // get fractional part
// distribute amp in ratio of fraction
psf[ (besty/2 + h ) * bestx + ( bestx/2 + w)]
= (1.0f * (abs(yval) - fraction)) / (count * abs(yval));
if( h * xval > 0)
psf[ (besty/2 + h ) * bestx + ( bestx/2 + w + 1)]
= (1.0f * fraction) / (count * abs(yval));
else // if( h < 0)
psf[ (besty/2 + h ) * bestx + ( bestx/2 + w - 1)] = (1.0f * fraction) / (count * abs(yval));
}
}
else // xval is greater than yval
{
//xval is always a positive
for(int w = - xval; w <= xval; w ++)
{
int h = (w * yval) / xval;
int fraction = abs((w * yval )) % xval; // get fractional part
psf[ (besty/2 + h ) * bestx + ( bestx/2 + w)]
= (1.0f * ( xval - fraction)) / (count * xval);
if( w * yval > 0)
psf[ (besty/2 + h + 1 ) * bestx + ( bestx/2 + w )]
= (1.0f * fraction) / (count * xval);
else // if( w * yval < 0)
psf[ (besty/2 + h - 1 ) * bestx + ( bestx/2 + w )]
= (1.0f * fraction) / (count * xval);
}
}
}
else // circular
{
// draw the blur circle at the center of frame
count = 0;
for(int h = - xval; h <= xval; h ++)
for( int w = - xval; w <= xval; w ++)
if( h * h + w * w <= xval * xval)
count ++;
// incase of deblur there is a spike added to center value
// actually all values except center are reduced.
for(int h = - xval; h <= xval; h ++)
for( int w = - xval; w <= xval; w ++)
if( h * h + w * w <= xval * xval)
psf[ (besty/2 + h ) * bestx + ( bestx/2 + w)] = 1.0f / count;
// center value will have spike to provide white noise and reduce instability
if(spike > 0.001)
psf[ (besty/2 ) * bestx + ( bestx/2 )] = (1.0f + spike)/ count;
}
return count;
}
//------------------------------------------------------------------------------------------------------------
void DesignInverse(fftwf_complex * fout, float * Filter,
float wn,int bestx, int besty,
float scale)
{
// the forward transform of PSF is in fout. Only real positive values
// get max value
float mval = fout[0][0];
for(int h = 0; h < bestx * besty; h ++)
// get max value. Possibly the zeroth val is max but lets find
if( (fout[ h ][0]) > mval)
mval = fout[ h ][0];
mval *= wn; // this is the min value we will accept for inversion
// apply freq mask, and wn of inversion
// PSF during forward transform is scaled up by sqrt(bestx * besty)
// while inverting it it is 1/ this value. so no sscaling for transform is needed.
float scaler = scale * (1.0 + wn)/ (bestx * besty ) /(1.0f - wn); // This trial/error derived approximation for scaler
// as the higher wn is frequencies are inverted less
// and we lose amplitude
// we add white noise and scaler .
for(int h = 0; h < bestx * besty; h ++)
Filter[ h ] = scaler / (fout[ h][0] + mval);
}
//------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------
// use this when data type is complex
int getSign(int i)
{
return (i & 1) == 0 ? 1 : -1;
}
/*// use this for real data
int getSign(int h, int w)
{
return ((h + (w >> 1)) & 1) == 0 ? 1 : -1;
}
*/
//---------------------------------------------------------------------------------------------
template <typename finc>
// convert input unsigned char data to float type
void getRealInput(float *data,const finc * fptr,
int pitch, int wd, int ht,int bytes,
int wpad, int hpad, bool centered)
{
// parameter bytes is for avisynth. not needed here
// convert frame y values to float and keep in data buffer
float *data1 = data;
const finc * fptr1 = fptr;
if(centered)
{
// values multiplied by -1^(x+y) i.e sign to get the spectogram centered in frame
for(int h = 0; h < ht; h ++)
{
for(int w = 0; w < wd; w ++)
{
data1[ w] = getSign( h, w) * fptr1[w];
}
data1 += wpad;
fptr1 += pitch;
}
}
else // not centered. So as it is
{
for(int h = 0; h < ht; h ++)
{
for(int w = 0;w < wd; w ++)
{
data1[ w] = fptr1[w];
}
data1 += wpad;
fptr1 += pitch;
}
}
data1 = data + ht * wpad;
// fill with zeroes rest of data buffer
for(int h = ht; h < hpad; h ++)
{
for(int w = 0; w < wpad;w ++)
{
data1[ w] = 0.0;
}
data1 += wpad;
}
data1 = data;
for(int h = 0; h < hpad; h ++)
{
for(int w = wd;w < wpad; w ++)
{
data1[ w] = 0.0;
}
data1 += wpad;
}
}
//---------------------------------------------------------------------------------------------
template <typename finc>
// convert float type output to unsigned char data
void getRealOutput(float *data, finc* fptr, int pitch,int wd, int ht,int wpad,int bytes, bool cent, finc min, finc max)
{
// vapoursynth does not require bytes parameter
// as values after fft may become -ve or go beyond 255 check and clip
float *data1= data;
finc * fptr1 = fptr;
if(cent )
{
for(size_t h = 0; h < ht; h ++)
{
for(size_t w = 0; w < wd; w ++)
{
int val = data1[ w] * getSign(h, w);
if(val < min)
fptr1[w] = min;
else if(val > max)
fptr1[ w] = max;
else
fptr1[ w] = val;
}
data1 += wpad;
fptr1 += pitch;
}
}
else
{
for(size_t h = 0; h < ht; h ++)
{
for(size_t w = 0; w < wd; w ++)
{
if(data1[ w] < min)
fptr1[ w] = min;
else if(data1[ w] > max)
fptr1[ w] = max;
else
fptr1[ w] = data1[ w];
}
data1 += wpad;
fptr1 += pitch;
}
}
}
//---------------------------------------------------------------------------------------------
void hammingWindowing(float * cosBell, int pitch, int width, int height, int rfilt)
{
// design a cosine bell hamming windowing function
int rfiltsq = rfilt * rfilt;
// float rfilt = sqrt(rfiltsq );
for(int h = 0; h < height/2; h ++)
{
for (int w = 0; w < width/2; w ++)
{
if( h * h + w * w <= rfiltsq)
{
float radial = sqrt((float) h * h + w * w );
float bell = 0.46 + 0.54 * cos( (3.1416 * radial) / rfilt);
for ( int hh = -1; hh <= 1; hh += 2)
{
for ( int ww = - 1; ww <= 1; ww += 2)
{
cosBell[ (height/2 + h * hh) * pitch + width/2 + w * ww ] *= bell;
}
}
}
else
{
for ( int hh = -1; hh <= 1; hh += 2)
{
for ( int ww = - 1; ww <= 1; ww += 2)
{
cosBell[ (height/2 + h * hh) * pitch + width/2 + w * ww ] *= 0.0;
}
}
}
}
}
}
//-------------------------------------------------------------------------------------------------------
void ApplyFilter(fftwf_complex* fout, float * Filter, int wd, int ht )
{
// applies the designed filter.in freq domain. Scalar multiply
// of freq response of designed filter with freq transform of input
//float scale = 1.0 / (hbest * wbest );
int nval = ht * wd;
for(int h = 0; h < nval; h++)
{
fout[h ][0] *= Filter[h ];
fout[h ][1] *= Filter[h ];
}
}
//-------------------------------------------------------------------------------------------------------------------------