forked from myrsloik/VapourSynth-FFT3DFilter
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FFT3DFilter.h
278 lines (233 loc) · 15.5 KB
/
FFT3DFilter.h
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
/*****************************************************************************
* FFT3DFilter.h
*****************************************************************************
* FFT3DFilter plugin for VapourSynth - 3D Frequency Domain filter
*
* Copyright (C) 2004-2006 A.G.Balakhnin aka Fizick <[email protected]> http://avisynth.org.ru
* Copyright (C) 2015 Yusuke Nakamura, <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as published by
* the Free Software Foundation.
*
* 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, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*****************************************************************************/
#include <cstdlib>
#include <memory>
#include <thread>
#include <unordered_map>
#include <fftw3.h>
#include <VapourSynth4.h>
/** declarations of filtering functions: **/
/* C */
void ApplyWiener2D_C(fftwf_complex *__restrict out, int outwidth, int outpitchelems, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta, float sharpen, float sigmaSquaredSharpenMin, float sigmaSquaredSharpenMax, const float *wsharpen, float dehalo, const float *wdehalo, float ht2n);
void ApplyPattern2D_C(fftwf_complex *__restrict outcur, int outwidth, int outpitchelems, int bh, int howmanyblocks, float pfactor, const float *pattern2d0, float beta);
void ApplyWiener3D2_C(fftwf_complex *__restrict outcur, const fftwf_complex *outprev, int outwidth, int outpitchelems, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta);
void ApplyPattern3D2_C(fftwf_complex *__restrict outcur, const fftwf_complex *outprev, int outwidth, int outpitchelems, int bh, int howmanyblocks, const float *pattern3d, float beta);
void ApplyWiener3D3_C(fftwf_complex *__restrict out, const fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitchelems, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta);
void ApplyPattern3D3_C(fftwf_complex *__restrict out, const fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitchelems, int bh, int howmanyblocks, const float *pattern3d, float beta);
void ApplyWiener3D4_C(fftwf_complex *__restrict out, const fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitchelems, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta);
void ApplyPattern3D4_C(fftwf_complex *__restrict out, const fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitchelems, int bh, int howmanyblocks, const float *pattern3d, float beta);
void ApplyWiener3D5_C(fftwf_complex *__restrict out, const fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, const fftwf_complex *outnext2, int outwidth, int outpitchelems, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta);
void ApplyPattern3D5_C(fftwf_complex *__restrict out, const fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, const fftwf_complex *outnext2, int outwidth, int outpitchelems, int bh, int howmanyblocks, const float *pattern3d, float beta);
void ApplyKalmanPattern_C(const fftwf_complex *outcur, fftwf_complex *__restrict outLast, fftwf_complex *__restrict covar, fftwf_complex *__restrict covarProcess, int outwidth, int outpitchelems, int bh, int howmanyblocks, const float *covarNoiseNormed, float kratio2);
void ApplyKalman_C(const fftwf_complex *outcur, fftwf_complex *__restrict outLast, fftwf_complex *__restrict covar, fftwf_complex *__restrict covarProcess, int outwidth, int outpitchelems, int bh, int howmanyblocks, float covarNoiseNormed, float kratio2);
void Sharpen_C(fftwf_complex *__restrict outcur, int outwidth, int outpitchelems, int bh, int howmanyblocks, float sharpen, float sigmaSquaredSharpenMin, float sigmaSquaredSharpenMax, const float *wsharpen, float dehalo, const float *wdehalo, float ht2n);
/* degrid_C */
void ApplyWiener2D_degrid_C(fftwf_complex *__restrict out, int outwidth, int outpitchelems, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta, float sharpen, float sigmaSquaredSharpenMin, float sigmaSquaredSharpenMax, const float *wsharpen, float degrid, const fftwf_complex *gridsample, float dehalo, const float *wdehalo, float ht2n);
void ApplyWiener3D2_degrid_C(fftwf_complex *__restrict outcur, const fftwf_complex *outprev, int outwidth, int outpitchelems, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta, float degrid, const fftwf_complex *gridsample);
void ApplyWiener3D3_degrid_C(fftwf_complex *__restrict outcur, const fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitchelems, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta, float degrid, const fftwf_complex *gridsample);
void ApplyWiener3D4_degrid_C(fftwf_complex *__restrict outcur, const fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitchelems, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta, float degrid, const fftwf_complex *gridsample);
void ApplyWiener3D5_degrid_C(fftwf_complex *__restrict outcur, const fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, const fftwf_complex *outnext2, int outwidth, int outpitchelems, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta, float degrid, const fftwf_complex *gridsample);
void Sharpen_degrid_C(fftwf_complex *__restrict outcur, int outwidth, int outpitchelems, int bh, int howmanyblocks, float sharpen, float sigmaSquaredSharpenMin, float sigmaSquaredSharpenMax, const float *wsharpen, float degrid, const fftwf_complex *gridsample, float dehalo, const float *wdehalo, float ht2n);
void ApplyPattern2D_degrid_C(fftwf_complex *__restrict outcur, int outwidth, int outpitchelems, int bh, int howmanyblocks, float pfactor, const float *pattern2d0, float beta, float degrid, const fftwf_complex *gridsample);
void ApplyPattern3D2_degrid_C(fftwf_complex *__restrict outcur, const fftwf_complex *outprev, int outwidth, int outpitchelems, int bh, int howmanyblocks, const float *pattern3d, float beta, float degrid, const fftwf_complex *gridsample);
void ApplyPattern3D3_degrid_C(fftwf_complex *__restrict out, const fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitchelems, int bh, int howmanyblocks, const float *pattern3d, float beta, float degrid, const fftwf_complex *gridsample);
void ApplyPattern3D4_degrid_C(fftwf_complex *__restrict out, const fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitchelems, int bh, int howmanyblocks, const float *pattern3d, float beta, float degrid, const fftwf_complex *gridsample);
void ApplyPattern3D5_degrid_C(fftwf_complex *__restrict out, const fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, const fftwf_complex *outnext2, int outwidth, int outpitchelems, int bh, int howmanyblocks, const float *pattern3d, float beta, float degrid, const fftwf_complex *gridsample);
template <class T, auto Allocator=malloc, auto Deleter=free>
class ThreadLocalPtrPool {
private:
std::unordered_map<std::thread::id, T *> pool;
size_t size;
public:
ThreadLocalPtrPool() = default;
void init(size_t size, int num_threads) noexcept;
T* get() noexcept;
~ThreadLocalPtrPool() noexcept {
for (const auto &[_, ptr] : pool) {
Deleter(ptr);
}
}
};
class FFT3DFilterTransform {
private:
/* parameters */
int plane;
int bw;
int bh;
int ow;
int oh;
int px;
int py;
float pcutoff;
float degrid;
bool interlaced;
VSNode *node;
ThreadLocalPtrPool<uint8_t> coverbuf; /* block buffer covering the frame without remainders (with sufficient width and heigth) */
int coverwidth;
int coverheight;
ptrdiff_t coverpitch;
int mirw; /* mirror width for padding */
int mirh; /* mirror height for padding */
VSVideoInfo dstvi;
VSVideoInfo outvi;
int planeBase;
int nox, noy;
int outwidth;
int outpitchelems; /* v.1.7 */
std::unique_ptr<float[]> wanxl; /* analysis */
std::unique_ptr<float[]> wanxr;
std::unique_ptr<float[]> wanyl;
std::unique_ptr<float[]> wanyr;
ThreadLocalPtrPool<float, fftwf_alloc_real, fftwf_free> in;
std::unique_ptr<fftwf_plan_s, decltype(&fftwf_destroy_plan)> plan;
public:
const VSVideoInfo *GetOutputVI() const { return &outvi; };
FFT3DFilterTransform(bool pshow, VSNode *node, int plane, int wintype, int bw, int bh, int ow, int oh, int px, int py, float pcutoff, float degrid, bool interlaced, bool measure, int ncpu, VSCore *core, const VSAPI *vsapi);
const VSFrame *GetGridSample(VSCore *core, const VSAPI *vsapi);
VSFrame *GetFrame(const VSFrame *src, VSCore *core, const VSAPI *vsapi);
void GetNoisePattern(int n, int &px, int &py, float *pattern2d, float &psigma, const fftwf_complex *gridsample, VSCore *core, const VSAPI *vsapi);
VSFrame *GetPShowInfo(const VSFrame *src, VSCore *core, const VSAPI *vsapi);
static const VSFrame *VS_CC GetFrame(int n, int activation_reason, void *instance_data, void **frame_data, VSFrameContext *frame_ctx, VSCore *core, const VSAPI *vsapi);
static const VSFrame *VS_CC GetPShowFrame(int n, int activation_reason, void *instance_data, void **frame_data, VSFrameContext *frame_ctx, VSCore *core, const VSAPI *vsapi);
static void VS_CC Free(void *instance_data, VSCore *core, const VSAPI *vsapi);
};
class FFT3DFilterInvTransform {
private:
/* parameters */
int bw;
int bh;
int ow;
int oh;
bool interlaced;
VSNode *node;
ThreadLocalPtrPool<uint8_t> coverbuf; /* block buffer covering the frame without remainders (with sufficient width and heigth) */
int coverwidth;
int coverheight;
ptrdiff_t coverpitch;
int mirw; /* mirror width for padding */
int mirh; /* mirror height for padding */
int planeBase;
int nox, noy;
int outwidth;
int outpitchelems;
float norm; /* normalization factor */
VSVideoInfo dstvi;
std::unique_ptr<float[]> wsynxl;
std::unique_ptr<float[]> wsynxr;
std::unique_ptr<float[]> wsynyl;
std::unique_ptr<float[]> wsynyr;
ThreadLocalPtrPool<float, fftwf_alloc_real, fftwf_free> in;
std::unique_ptr<fftwf_plan_s, decltype(&fftwf_destroy_plan)> planinv;
VSFrame *GetFrame(const VSFrame *src, VSCore *core, const VSAPI *vsapi);
public:
const VSVideoInfo *GetOutputVI() const { return &dstvi; };
FFT3DFilterInvTransform(VSNode *node, const VSVideoInfo *vi, int plane, int wintype, int bw, int bh, int ow, int oh, bool interlaced, bool measure, int ncpu, VSCore *core, const VSAPI *vsapi);
static const VSFrame *VS_CC GetFrame(int n, int activation_reason, void *instance_data, void **frame_data, VSFrameContext *frame_ctx, VSCore *core, const VSAPI *vsapi);
static void VS_CC Free(void *instance_data, VSCore *core, const VSAPI *vsapi);
};
class FFT3DFilterPShow {
private:
/* parameters */
int plane;
int bw;
int bh;
int ow;
int oh;
VSNode *node;
const VSVideoInfo *vi;
public:
FFT3DFilterPShow(VSNode *node, int plane, int bw, int bh, int ow, int oh, bool interlaced, VSCore *core, const VSAPI *vsapi);
VSFrame *GetFrame(const VSFrame *src, VSCore *core, const VSAPI *vsapi);
static const VSFrame *VS_CC GetFrame(int n, int activation_reason, void *instance_data, void **frame_data, VSFrameContext *frame_ctx, VSCore *core, const VSAPI *vsapi);
static void VS_CC Free(void *instance_data, VSCore *core, const VSAPI *vsapi);
};
class FFT3DFilter {
private:
/* parameters */
float sigma; /* noise level (std deviation) for high frequncies */
float beta; /* relative noise margin for Wiener filter */
int plane; /* color plane */
int bw; /* block width */
int bh; /* block height */
int bt; /* block size along time (mumber of frames), =0 for Kalman, >0 for Wiener */
int ow; /* overlap width - v.0.9 */
int oh; /* overlap height - v.0.9 */
float kratio; /* threshold to sigma ratio for Kalman filter */
float sharpen; /* sharpen factor (0 to 1 and above) */
float scutoff; /* sharpen cufoff frequency (relative to max) - v1.7 */
float svr; /* sharpen vertical ratio (0 to 1 and above) - v.1.0 */
float smin; /* minimum limit for sharpen (prevent noise amplifying) - v.1.1 */
float smax; /* maximum limit for sharpen (prevent oversharping) - v.1.1 */
int pframe; /* noise pattern frame number */
int px; /* noise pattern window x-position */
int py; /* noise pattern window y-position */
float pfactor; /* noise pattern denoise strength */
float sigma2; /* noise level for middle frequencies */
float sigma3; /* noise level for low frequencies */
float sigma4; /* noise level for lowest (zero) frequencies */
float degrid; /* decrease grid */
float dehalo; /* remove halo strength - v.1.9 */
float hr; /* halo radius - v1.9 */
float ht; /* halo threshold - v1.9 */
/* additional parameterss */
const VSFrame *gridsample;
int nox, noy;
int outwidth;
ptrdiff_t outpitch;
int outpitchelems;
int outsize;
int howmanyblocks;
std::unique_ptr<float[], decltype(&fftw_free)> wsharpen;
std::unique_ptr<float[], decltype(&fftw_free)> wdehalo;
// Shared buffers only used for bt=0 (kalman) mode
std::unique_ptr<fftwf_complex[], decltype(&fftw_free)> outLast;
std::unique_ptr<fftwf_complex[], decltype(&fftw_free)> covar;
std::unique_ptr<fftwf_complex[], decltype(&fftw_free)> covarProcess;
//
float sigmaSquaredNoiseNormed;
float sigmaSquaredNoiseNormed2D;
float sigmaSquaredSharpenMinNormed;
float sigmaSquaredSharpenMaxNormed;
float ht2n; /* halo threshold squared normed */
float norm; /* normalization factor */
std::unique_ptr<float[], decltype(&fftw_free)> pattern2d;
std::unique_ptr<float[], decltype(&fftw_free)> pattern3d;
const VSVideoInfo *vi;
VSNode *node;
template < int btcur >
void Wiener3D(int n, VSNode *node, VSFrame *dst, VSFrameContext *frame_ctx, const VSAPI *vsapi);
public:
const VSFrame *ApplyFilter(int n, VSFrameContext *frame_ctx, VSCore *core, const VSAPI *vsapi);
/* Constructor */
FFT3DFilter
(
FFT3DFilterTransform *transform, const VSVideoInfo *vi,
float _sigma, float _beta, int _plane, int _bw, int _bh, int _bt, int _ow, int _oh,
float _kratio, float _sharpen, float _scutoff, float _svr, float _smin, float _smax,
int _pframe, int _px, int _py, bool _pshow, float _pcutoff, float _pfactor,
float _sigma2, float _sigma3, float _sigma4, float _degrid,
float _dehalo, float _hr, float _ht, int _ncpu,
VSNode *node, VSCore *core, const VSAPI *vsapi
);
static const VSFrame *VS_CC GetFrame(int n, int activation_reason, void *instance_data, void **frame_data, VSFrameContext *frame_ctx, VSCore *core, const VSAPI *vsapi);
static void VS_CC Free(void *instance_data, VSCore *core, const VSAPI *vsapi);
};