forked from VFR-maniac/VapourSynth-FFT3DFilter
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFFT3DFilterTransform.cpp
1123 lines (1001 loc) · 53 KB
/
FFT3DFilterTransform.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************
* FFT3DFilter.cpp
*****************************************************************************
* 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.
*****************************************************************************
*
* Plugin uses external FFTW library version 3 (http://www.fftw.org)
* You must put libfftw3f-3.dll in the same directory as the plugin dll
*
* The algorithm is based on the 3D IIR/3D Frequency Domain Filter from:
* MOTION PICTURE RESTORATION. by Anil Christopher Kokaram. Ph.D. Thesis. May 1993.
* http://www.mee.tcd.ie/~ack/papers/a4ackphd.ps.gz
*
*****************************************************************************/
#include "FFT3DFilter.h"
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
template<typename T>
static void fft3d_memset(T *dst, T val, size_t count) {
for (size_t i = 0; i < count; i++)
dst[i] = val;
}
static void GetAnalysisWindow(int wintype, int ow, int oh, float *wanxl, float *wanxr, float *wanyl, float *wanyr) {
constexpr float pi = 3.1415926535897932384626433832795f;
if (wintype == 0) {
/*
* half-cosine, the same for analysis and synthesis
* define analysis windows */
for (int i = 0; i < ow; i++) {
wanxl[i] = cosf(pi * (i - ow + 0.5f) / (ow * 2)); /* left analize window (half-cosine) */
wanxr[i] = cosf(pi * (i + 0.5f) / (ow * 2)); /* right analize window (half-cosine) */
}
for (int i = 0; i < oh; i++) {
wanyl[i] = cosf(pi * (i - oh + 0.5f) / (oh * 2));
wanyr[i] = cosf(pi * (i + 0.5f) / (oh * 2));
}
} else if (wintype == 1) {
/* define analysis windows as more flat (to decrease grid) */
for (int i = 0; i < ow; i++) {
wanxl[i] = sqrt(cosf(pi * (i - ow + 0.5f) / (ow * 2)));
wanxr[i] = sqrt(cosf(pi * (i + 0.5f) / (oh * 2)));
}
for (int i = 0; i < oh; i++) {
wanyl[i] = sqrt(cosf(pi * (i - oh + 0.5f) / (oh * 2)));
wanyr[i] = sqrt(cosf(pi * (i + 0.5f) / (oh * 2)));
}
} else /* (wintype==2) */ {
/* define analysis windows as flat (to prevent grid) */
for (int i = 0; i < ow; i++) {
wanxl[i] = 1;
wanxr[i] = 1;
}
for (int i = 0; i < oh; i++) {
wanyl[i] = 1;
wanyr[i] = 1;
}
}
}
static void GetSynthesisWindow(int wintype, int ow, int oh, float *wsynxl, float *wsynxr, float *wsynyl, float *wsynyr) {
constexpr float pi = 3.1415926535897932384626433832795f;
if (wintype == 0) {
for (int i = 0; i < ow; i++) {
wsynxl[i] = cosf(pi * (i - ow + 0.5f) / (ow * 2)); /* left analize window (half-cosine) */
wsynxr[i] = cosf(pi * (i + 0.5f) / (ow * 2)); /* right analize window (half-cosine) */
}
for (int i = 0; i < oh; i++) {
wsynyl[i] = cosf(pi * (i - oh + 0.5f) / (oh * 2));
wsynyr[i] = cosf(pi * (i + 0.5f) / (oh * 2));
}
} else if (wintype == 1) {
/* define synthesis as supplenent to rised cosine (Hanning) */
for (int i = 0; i < ow; i++) {
float wanxl = sqrt(cosf(pi * (i - ow + 0.5f) / (ow * 2)));
float wanxr = sqrt(cosf(pi * (i + 0.5f) / (oh * 2)));
wsynxl[i] = wanxl * wanxl * wanxl; /* left window */
wsynxr[i] = wanxr * wanxr * wanxr; /* right window */
}
for (int i = 0; i < oh; i++) {
float wanyl = sqrt(cosf(pi * (i - oh + 0.5f) / (oh * 2)));
float wanyr = sqrt(cosf(pi * (i + 0.5f) / (oh * 2)));
wsynyl[i] = wanyl * wanyl * wanyl;
wsynyr[i] = wanyr * wanyr * wanyr;
}
} else {
/* define synthesis as rised cosine (Hanning) */
for (int i = 0; i < ow; i++) {
wsynxl[i] = cosf(pi * (i - ow + 0.5f) / (ow * 2));
wsynxl[i] = wsynxl[i] * wsynxl[i]; /* left window (rised cosine) */
wsynxr[i] = cosf(pi * (i + 0.5f) / (ow * 2));
wsynxr[i] = wsynxr[i] * wsynxr[i]; /* right window (falled cosine) */
}
for (int i = 0; i < oh; i++) {
wsynyl[i] = cosf(pi * (i - oh + 0.5f) / (oh * 2));
wsynyl[i] = wsynyl[i] * wsynyl[i];
wsynyr[i] = cosf(pi * (i + 0.5f) / (oh * 2));
wsynyr[i] = wsynyr[i] * wsynyr[i];
}
}
}
static void GetPatternWindow(int bw, int bh, int outwidth, int outpitchelems, float pcutoff, float *pwin) {
for (int j = 0; j < bh; j++) {
float fh2;
if (j < bh / 2)
fh2 = (j * 2.0f / bh) * (j * 2.0f / bh);
else
fh2 = ((bh - 1 - j) * 2.0f / bh) * ((bh - 1 - j) * 2.0f / bh);
for (int i = 0; i < outwidth; i++) {
float fw2 = (i * 2.0f / bw) * (j * 2.0f / bw);
pwin[i + j * outpitchelems] = (fh2 + fw2) / (fh2 + fw2 + pcutoff * pcutoff);
}
}
}
//
template<typename T>
static void FramePlaneToCoverbuf(int plane, const VSFrame *src, T *__restrict coverbuf, int coverwidth, int coverheight, ptrdiff_t coverpitch, int mirw, int mirh, bool interlaced, const VSAPI *vsapi) {
const T *__restrict srcp = reinterpret_cast<const T *>(vsapi->getReadPtr(src, plane));
int src_height = vsapi->getFrameHeight(src, plane);
int src_width = vsapi->getFrameWidth(src, plane);
ptrdiff_t src_pitch = vsapi->getStride(src, plane) / sizeof(T);
coverpitch /= sizeof(T);
int width2 = src_width + src_width + mirw + mirw - 2;
T *__restrict coverbuf1 = coverbuf + coverpitch * mirh;
if (!interlaced) /* progressive */
{
for (int h = mirh; h < src_height + mirh; h++) {
for (int w = 0; w < mirw; w++) {
coverbuf1[w] = coverbuf1[mirw + mirw - w]; /* mirror left border */
}
memcpy(coverbuf1 + mirw, srcp, src_width * sizeof(T)); /* copy line */
for (int w = src_width + mirw; w < coverwidth; w++) {
coverbuf1[w] = coverbuf1[width2 - w]; /* mirror right border */
}
coverbuf1 += coverpitch;
srcp += src_pitch;
}
} else /* interlaced */
{
for (int h = mirh; h < src_height / 2 + mirh; h++) /* first field */
{
for (int w = 0; w < mirw; w++) {
coverbuf1[w] = coverbuf1[mirw + mirw - w]; /* mirror left border */
}
memcpy(coverbuf1 + mirw, srcp, src_width * sizeof(T)); /* copy line */
for (int w = src_width + mirw; w < coverwidth; w++) {
coverbuf1[w] = coverbuf1[width2 - w]; /* mirror right border */
}
coverbuf1 += coverpitch;
srcp += src_pitch * 2;
}
srcp -= src_pitch;
for (int h = src_height / 2 + mirh; h < src_height + mirh; h++) /* flip second field */
{
for (int w = 0; w < mirw; w++) {
coverbuf1[w] = coverbuf1[mirw + mirw - w]; /* mirror left border */
}
memcpy(coverbuf1 + mirw, srcp, src_width * sizeof(T)); /* copy line */
for (int w = src_width + mirw; w < coverwidth; w++) {
coverbuf1[w] = coverbuf1[width2 - w]; /* mirror right border */
}
coverbuf1 += coverpitch;
srcp -= src_pitch * 2;
}
}
T *pmirror = coverbuf1 - coverpitch * 2; /* pointer to vertical mirror */
for (int h = src_height + mirh; h < coverheight; h++) {
memcpy(coverbuf1, pmirror, coverwidth * sizeof(T)); /* mirror bottom line by line */
coverbuf1 += coverpitch;
pmirror -= coverpitch;
}
coverbuf1 = coverbuf;
pmirror = coverbuf1 + coverpitch * mirh * 2; /* pointer to vertical mirror */
for (int h = 0; h < mirh; h++) {
memcpy(coverbuf1, pmirror, coverwidth * sizeof(T)); /* mirror bottom line by line */
coverbuf1 += coverpitch;
pmirror -= coverpitch;
}
}
//-----------------------------------------------------------------------
//
template<typename T>
static void CoverbufToFramePlane(const T *__restrict coverbuf, int coverwidth, int coverheight, ptrdiff_t coverpitch, VSFrame *dst, int mirw, int mirh, bool interlaced, const VSAPI *vsapi) {
T *__restrict dstp = reinterpret_cast<T *>(vsapi->getWritePtr(dst, 0));
int dst_height = vsapi->getFrameHeight(dst, 0);
int dst_width = vsapi->getFrameWidth(dst, 0);
ptrdiff_t dst_pitch = vsapi->getStride(dst, 0) / sizeof(T);
coverpitch /= sizeof(T);
const T *__restrict coverbuf1 = coverbuf + coverpitch * mirh + mirw;
if (!interlaced) /* progressive */
{
for (int h = 0; h < dst_height; h++) {
memcpy(dstp, coverbuf1, dst_width * sizeof(T)); /* copy pure frame size only */
dstp += dst_pitch;
coverbuf1 += coverpitch;
}
} else /* interlaced */
{
for (int h = 0; h < dst_height; h += 2) {
memcpy(dstp, coverbuf1, dst_width * sizeof(T)); /* copy pure frame size only */
dstp += dst_pitch * 2;
coverbuf1 += coverpitch;
}
/* second field is flipped */
dstp -= dst_pitch;
for (int h = 0; h < dst_height; h += 2) {
memcpy(dstp, coverbuf1, dst_width * sizeof(T)); /* copy pure frame size only */
dstp -= dst_pitch * 2;
coverbuf1 += coverpitch;
}
}
}
//-----------------------------------------------------------------------
//-----------------------------------------------------------------------
/* put source bytes to float array of overlapped blocks
* use analysis windows */
template<typename T>
static void InitOverlapPlane(float *__restrict inp0, const T *__restrict srcp0, ptrdiff_t src_pitch, float *__restrict wanxl, float *__restrict wanxr, float *__restrict wanyl, float *__restrict wanyr, int bw, int bh, int ow, int oh, int nox, int noy, int coverwidth, int planeBase) {
int ihy;
const T *__restrict srcp = srcp0;
int xoffset = bh * bw - (bw - ow); /* skip frames */
int yoffset = bw * nox * bh - bw * (bh - oh); /* vertical offset of same block (overlap) */
src_pitch /= sizeof(T);
float *__restrict inp = inp0;
if constexpr (std::is_floating_point_v<T>)
planeBase = 0;
ihy = 0; /* first top (big non-overlapped) part */
{
for (int h = 0; h < oh; h++) {
inp = inp0 + h * bw;
for (int w = 0; w < ow; w++) /* left part (non-overlapped) row of first block */
{
inp[w] = float(wanxl[w] * wanyl[h] * (srcp[w] - planeBase)); /* Copy each byte from source to float array */
}
for (int w = ow; w < bw - ow; w++) /* left part (non-overlapped) row of first block */
{
inp[w] = float(wanyl[h] * (srcp[w] - planeBase)); /* Copy each byte from source to float array */
}
inp += bw - ow;
srcp += bw - ow;
for (int ihx = 1; ihx < nox; ihx += 1) /* middle horizontal blocks */
{
for (int w = 0; w < ow; w++) /* first part (overlapped) row of block */
{
float ftmp = float(wanyl[h] * (srcp[w] - planeBase)); /* Copy each byte from source to float array */
inp[w] = ftmp * wanxr[w]; /* cur block */
inp[w + xoffset] = ftmp * wanxl[w]; /* overlapped Copy - next block */
}
inp += ow;
inp += xoffset;
srcp += ow;
for (int w = 0; w < bw - ow - ow; w++) /* center part (non-overlapped) row of first block */
{
inp[w] = float(wanyl[h] * (srcp[w] - planeBase)); /* Copy each byte from source to float array */
}
inp += bw - ow - ow;
srcp += bw - ow - ow;
}
for (int w = 0; w < ow; w++) /* last part (non-overlapped) of line of last block */
{
inp[w] = float(wanxr[w] * wanyl[h] * (srcp[w] - planeBase)); /* Copy each byte from source to float array */
}
inp += ow;
srcp += ow;
srcp += (src_pitch - coverwidth); /* Add the pitch of one line (in bytes) to the source image. */
}
for (int h = oh; h < bh - oh; h++) {
inp = inp0 + h * bw;
for (int w = 0; w < ow; w++) /* left part (non-overlapped) row of first block */
{
inp[w] = float(wanxl[w] * (srcp[w] - planeBase)); /* Copy each byte from source to float array */
}
for (int w = ow; w < bw - ow; w++) /* left part (non-overlapped) row of first block */
{
inp[w] = float((srcp[w] - planeBase)); /* Copy each byte from source to float array */
}
inp += bw - ow;
srcp += bw - ow;
for (int ihx = 1; ihx < nox; ihx += 1) /* middle horizontal blocks */
{
for (int w = 0; w < ow; w++) /* first part (overlapped) row of block */
{
float ftmp = float((srcp[w] - planeBase)); /* Copy each byte from source to float array */
inp[w] = ftmp * wanxr[w]; /* cur block */
inp[w + xoffset] = ftmp * wanxl[w]; /* overlapped Copy - next block */
}
inp += ow;
inp += xoffset;
srcp += ow;
for (int w = 0; w < bw - ow - ow; w++) /* center part (non-overlapped) row of first block */
{
inp[w] = float((srcp[w] - planeBase)); /* Copy each byte from source to float array */
}
inp += bw - ow - ow;
srcp += bw - ow - ow;
}
for (int w = 0; w < ow; w++) /* last part (non-overlapped) line of last block */
{
inp[w] = float(wanxr[w] * (srcp[w] - planeBase)); /* Copy each byte from source to float array */
}
inp += ow;
srcp += ow;
srcp += (src_pitch - coverwidth); /* Add the pitch of one line (in bytes) to the source image. */
}
}
for (ihy = 1; ihy < noy; ihy += 1) /* middle vertical */
{
for (int h = 0; h < oh; h++) /* top overlapped part */
{
inp = inp0 + (ihy - 1) * (yoffset + (bh - oh) * bw) + (bh - oh) * bw + h * bw;
for (int w = 0; w < ow; w++) /* first half line of first block */
{
float ftmp = float(wanxl[w] * (srcp[w] - planeBase));
inp[w] = ftmp * wanyr[h]; /* Copy each byte from source to float array */
inp[w + yoffset] = ftmp * wanyl[h]; /* y overlapped */
}
for (int w = ow; w < bw - ow; w++) /* first half line of first block */
{
float ftmp = float((srcp[w] - planeBase));
inp[w] = ftmp * wanyr[h]; /* Copy each byte from source to float array */
inp[w + yoffset] = ftmp * wanyl[h]; /* y overlapped */
}
inp += bw - ow;
srcp += bw - ow;
for (int ihx = 1; ihx < nox; ihx++) /* middle blocks */
{
for (int w = 0; w < ow; w++) /* half overlapped line of block */
{
float ftmp = float((srcp[w] - planeBase)); /* Copy each byte from source to float array */
inp[w] = ftmp * wanxr[w] * wanyr[h];
inp[w + xoffset] = ftmp * wanxl[w] * wanyr[h]; /* x overlapped */
inp[w + yoffset] = ftmp * wanxr[w] * wanyl[h];
inp[w + xoffset + yoffset] = ftmp * wanxl[w] * wanyl[h]; /* x overlapped */
}
inp += ow;
inp += xoffset;
srcp += ow;
for (int w = 0; w < bw - ow - ow; w++) /* half non-overlapped line of block */
{
float ftmp = float((srcp[w] - planeBase)); /* Copy each byte from source to float array */
inp[w] = ftmp * wanyr[h];
inp[w + yoffset] = ftmp * wanyl[h];
}
inp += bw - ow - ow;
srcp += bw - ow - ow;
}
for (int w = 0; w < ow; w++) /* last half line of last block */
{
float ftmp = float(wanxr[w] * (srcp[w] - planeBase)); /* Copy each byte from source to float array */
inp[w] = ftmp * wanyr[h];
inp[w + yoffset] = ftmp * wanyl[h];
}
inp += ow;
srcp += ow;
srcp += (src_pitch - coverwidth); /* Add the pitch of one line (in bytes) to the source image. */
}
/* middle vertical nonovelapped part */
for (int h = 0; h < bh - oh - oh; h++) {
inp = inp0 + (ihy - 1) * (yoffset + (bh - oh) * bw) + (bh)*bw + h * bw + yoffset;
for (int w = 0; w < ow; w++) /* first half line of first block */
{
float ftmp = float(wanxl[w] * (srcp[w] - planeBase));
inp[w] = ftmp; /* Copy each byte from source to float array */
}
for (int w = ow; w < bw - ow; w++) /* first half line of first block */
{
float ftmp = float((srcp[w] - planeBase));
inp[w] = ftmp; /* Copy each byte from source to float array */
}
inp += bw - ow;
srcp += bw - ow;
for (int ihx = 1; ihx < nox; ihx++) /* middle blocks */
{
for (int w = 0; w < ow; w++) /* half overlapped line of block */
{
float ftmp = float((srcp[w] - planeBase)); /* Copy each byte from source to float array */
inp[w] = ftmp * wanxr[w];
inp[w + xoffset] = ftmp * wanxl[w]; /* x overlapped */
}
inp += ow;
inp += xoffset;
srcp += ow;
for (int w = 0; w < bw - ow - ow; w++) /* half non-overlapped line of block */
{
float ftmp = float((srcp[w] - planeBase)); /* Copy each byte from source to float array */
inp[w] = ftmp;
}
inp += bw - ow - ow;
srcp += bw - ow - ow;
}
for (int w = 0; w < ow; w++) /* last half line of last block */
{
float ftmp = float(wanxr[w] * (srcp[w] - planeBase)); /* Copy each byte from source to float array */
inp[w] = ftmp;
}
inp += ow;
srcp += ow;
srcp += (src_pitch - coverwidth); /* Add the pitch of one line (in bytes) to the source image. */
}
}
ihy = noy; /* last bottom part */
{
for (int h = 0; h < oh; h++) {
inp = inp0 + (ihy - 1) * (yoffset + (bh - oh) * bw) + (bh - oh) * bw + h * bw;
for (int w = 0; w < ow; w++) /* first half line of first block */
{
float ftmp = float(wanxl[w] * wanyr[h] * (srcp[w] - planeBase));
inp[w] = ftmp; /* Copy each byte from source to float array */
}
for (int w = ow; w < bw - ow; w++) /* first half line of first block */
{
float ftmp = float(wanyr[h] * (srcp[w] - planeBase));
inp[w] = ftmp; /* Copy each byte from source to float array */
}
inp += bw - ow;
srcp += bw - ow;
for (int ihx = 1; ihx < nox; ihx++) /* middle blocks */
{
for (int w = 0; w < ow; w++) /* half line of block */
{
float ftmp = float(wanyr[h] * (srcp[w] - planeBase)); /* Copy each byte from source to float array */
inp[w] = ftmp * wanxr[w];
inp[w + xoffset] = ftmp * wanxl[w]; /* overlapped Copy */
}
inp += ow;
inp += xoffset;
srcp += ow;
for (int w = 0; w < bw - ow - ow; w++) /* center part (non-overlapped) row of first block */
{
inp[w] = float(wanyr[h] * (srcp[w] - planeBase)); /* Copy each byte from source to float array */
}
inp += bw - ow - ow;
srcp += bw - ow - ow;
}
for (int w = 0; w < ow; w++) /* last half line of last block */
{
float ftmp = float(wanxr[w] * wanyr[h] * (srcp[w] - planeBase));
inp[w] = ftmp; /* Copy each byte from source to float array */
}
inp += ow;
srcp += ow;
srcp += (src_pitch - coverwidth); /* Add the pitch of one line (in bytes) to the source image. */
}
}
}
template<typename T>
static void DecodeOverlapPlane(const float *__restrict inp0, float norm, T *__restrict dstp0, ptrdiff_t dst_pitch, float *__restrict wsynxl, float *__restrict wsynxr, float *__restrict wsynyr, float *__restrict wsynyl, int bw, int bh, int ow, int oh, int nox, int noy, int coverwidth, int planeBase, int maxval) {
int ihy;
T *__restrict dstp = dstp0;
const float *__restrict inp = inp0;
int xoffset = bh * bw - (bw - ow);
int yoffset = bw * nox * bh - bw * (bh - oh); /* vertical offset of same block (overlap) */
dst_pitch /= sizeof(T);
ihy = 0; /* first top big non-overlapped) part */
{
for (int h = 0; h < bh - oh; h++) {
inp = inp0 + h * bw;
for (int w = 0; w < bw - ow; w++) /* first half line of first block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)(inp[w] * norm) + planeBase)); /* Copy each byte from float array to dest with windows */
else
dstp[w] = inp[w] * norm;
}
inp += bw - ow;
dstp += bw - ow;
for (int ihx = 1; ihx < nox; ihx++) /* middle horizontal half-blocks */
{
for (int w = 0; w < ow; w++) /* half line of block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)((inp[w] * wsynxr[w] + inp[w + xoffset] * wsynxl[w]) * norm) + planeBase)); /* overlapped Copy */
else
dstp[w] = (inp[w] * wsynxr[w] + inp[w + xoffset] * wsynxl[w]) * norm;
}
inp += xoffset + ow;
dstp += ow;
for (int w = 0; w < bw - ow - ow; w++) /* first half line of first block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)(inp[w] * norm) + planeBase)); /* Copy each byte from float array to dest with windows */
else
dstp[w] = inp[w] * norm;
}
inp += bw - ow - ow;
dstp += bw - ow - ow;
}
for (int w = 0; w < ow; w++) /* last half line of last block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)(inp[w] * norm) + planeBase));
else
dstp[w] = inp[w] * norm;
}
inp += ow;
dstp += ow;
dstp += (dst_pitch - coverwidth); /* Add the pitch of one line (in bytes) to the dest image. */
}
}
for (ihy = 1; ihy < noy; ihy += 1) /* middle vertical */
{
for (int h = 0; h < oh; h++) /* top overlapped part */
{
inp = inp0 + (ihy - 1) * (yoffset + (bh - oh) * bw) + (bh - oh) * bw + h * bw;
float wsynyrh = wsynyr[h] * norm; /* remove from cycle for speed */
float wsynylh = wsynyl[h] * norm;
for (int w = 0; w < bw - ow; w++) /* first half line of first block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)((inp[w] * wsynyrh + inp[w + yoffset] * wsynylh)) + planeBase)); /* y overlapped */
else
dstp[w] = inp[w] * wsynyrh + inp[w + yoffset] * wsynylh;
}
inp += bw - ow;
dstp += bw - ow;
for (int ihx = 1; ihx < nox; ihx++) /* middle blocks */
{
for (int w = 0; w < ow; w++) /* half overlapped line of block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)(((inp[w] * wsynxr[w] + inp[w + xoffset] * wsynxl[w]) * wsynyrh
+ (inp[w + yoffset] * wsynxr[w] + inp[w + xoffset + yoffset] * wsynxl[w]) * wsynylh)) + planeBase)); /* x overlapped */
else
dstp[w] = (inp[w] * wsynxr[w] + inp[w + xoffset] * wsynxl[w]) * wsynyrh
+ (inp[w + yoffset] * wsynxr[w] + inp[w + xoffset + yoffset] * wsynxl[w]) * wsynylh;
}
inp += xoffset + ow;
dstp += ow;
for (int w = 0; w < bw - ow - ow; w++) /* double minus - half non-overlapped line of block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)((inp[w] * wsynyrh + inp[w + yoffset] * wsynylh)) + planeBase));
else
dstp[w] = inp[w] * wsynyrh + inp[w + yoffset] * wsynylh;
}
inp += bw - ow - ow;
dstp += bw - ow - ow;
}
for (int w = 0; w < ow; w++) /* last half line of last block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)((inp[w] * wsynyrh + inp[w + yoffset] * wsynylh)) + planeBase));
else
dstp[w] = inp[w] * wsynyrh + inp[w + yoffset] * wsynylh;
}
inp += ow;
dstp += ow;
dstp += (dst_pitch - coverwidth); /* Add the pitch of one line (in bytes) to the source image. */
}
/* middle vertical non-ovelapped part */
for (int h = 0; h < (bh - oh - oh); h++) {
inp = inp0 + (ihy - 1) * (yoffset + (bh - oh) * bw) + (bh)*bw + h * bw + yoffset;
for (int w = 0; w < bw - ow; w++) /* first half line of first block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)(inp[w] * norm) + planeBase));
else
dstp[w] = inp[w] * norm;
}
inp += bw - ow;
dstp += bw - ow;
for (int ihx = 1; ihx < nox; ihx++) /* middle blocks */
{
for (int w = 0; w < ow; w++) /* half overlapped line of block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)((inp[w] * wsynxr[w] + inp[w + xoffset] * wsynxl[w]) * norm) + planeBase)); /* x overlapped */
else
dstp[w] = (inp[w] * wsynxr[w] + inp[w + xoffset] * wsynxl[w]) * norm;
}
inp += xoffset + ow;
dstp += ow;
for (int w = 0; w < bw - ow - ow; w++) /* half non-overlapped line of block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)(inp[w] * norm) + planeBase));
else
dstp[w] = inp[w] * norm;
}
inp += bw - ow - ow;
dstp += bw - ow - ow;
}
for (int w = 0; w < ow; w++) /* last half line of last block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)(inp[w] * norm) + planeBase));
else
dstp[w] = inp[w] * norm;
}
inp += ow;
dstp += ow;
dstp += (dst_pitch - coverwidth); /* Add the pitch of one line (in bytes) to the source image. */
}
}
ihy = noy; /* last bottom part */
{
for (int h = 0; h < oh; h++) {
inp = inp0 + (ihy - 1) * (yoffset + (bh - oh) * bw) + (bh - oh) * bw + h * bw;
for (int w = 0; w < bw - ow; w++) /* first half line of first block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)(inp[w] * norm) + planeBase));
else
dstp[w] = inp[w] * norm;
}
inp += bw - ow;
dstp += bw - ow;
for (int ihx = 1; ihx < nox; ihx++) /* middle blocks */
{
for (int w = 0; w < ow; w++) /* half line of block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)((inp[w] * wsynxr[w] + inp[w + xoffset] * wsynxl[w]) * norm) + planeBase)); /* overlapped Copy */
else
dstp[w] = (inp[w] * wsynxr[w] + inp[w + xoffset] * wsynxl[w]) * norm;
}
inp += xoffset + ow;
dstp += ow;
for (int w = 0; w < bw - ow - ow; w++) /* half line of block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)(inp[w] * norm) + planeBase));
else
dstp[w] = inp[w] * norm;
}
inp += bw - ow - ow;
dstp += bw - ow - ow;
}
for (int w = 0; w < ow; w++) /* last half line of last block */
{
if constexpr (std::is_integral_v<T>)
dstp[w] = std::min(maxval, std::max(0, (int)(inp[w] * norm) + planeBase));
else
dstp[w] = inp[w] * norm;
}
inp += ow;
dstp += ow;
dstp += (dst_pitch - coverwidth); /* Add the pitch of one line (in bytes) to the source image. */
}
}
}
FFT3DFilterTransform::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) : plane(plane_), node(node_), bw(bw_), bh(bh_), ow(ow_), oh(oh_), px(px_), py(py_), pcutoff(pcutoff_), degrid(degrid_), interlaced(interlaced_), in(nullptr, nullptr), plan(nullptr, nullptr) {
if (ow < 0)
ow = bw / 3;
if (oh < 0)
oh = bh / 3;
const VSVideoInfo *srcvi = vsapi->getVideoInfo(node);
planeBase = (plane > 0 && srcvi->format.sampleType == stInteger && srcvi->format.colorFamily == cfYUV) ? (1 << (srcvi->format.bitsPerSample - 1)) : 0;
nox = ((srcvi->width >> (plane ? srcvi->format.subSamplingW : 0)) - ow + (bw - ow - 1)) / (bw - ow);
noy = ((srcvi->height >> (plane ? srcvi->format.subSamplingH : 0)) - oh + (bh - oh - 1)) / (bh - oh);
wanxl = std::unique_ptr<float[]>(new float[ow]);
wanxr = std::unique_ptr<float[]>(new float[ow]);
wanyl = std::unique_ptr<float[]>(new float[oh]);
wanyr = std::unique_ptr<float[]>(new float[oh]);
GetAnalysisWindow(wintype, ow, oh, wanxl.get(), wanxr.get(), wanyl.get(), wanyr.get());
/* padding by 1 block per side */
nox += 2;
noy += 2;
mirw = bw - ow; /* set mirror size as block interval */
mirh = bh - oh;
coverwidth = nox * (bw - ow) + ow;
coverheight = noy * (bh - oh) + oh;
coverpitch = ((coverwidth + 7) / 8) * 8 * srcvi->format.bytesPerSample;
coverbuf = std::unique_ptr<uint8_t[]>(new uint8_t[coverheight * coverpitch]);
int insize = bw * bh * nox * noy;
in = std::unique_ptr<float[], decltype(&fftw_free)>(fftwf_alloc_real(insize), fftwf_free);
outwidth = bw / 2 + 1; /* width (pitch) of complex fft block */
outpitchelems = ((outwidth + 1) / 2) * 2;
int outsize = outpitchelems * bh * nox * noy; /* replace outwidth to outpitchelems here and below in v1.7 */
int planFlags = (measure ? FFTW_MEASURE : FFTW_ESTIMATE) | FFTW_DESTROY_INPUT;
int ndim[2] = { bh, bw };
int idist = bw * bh;
int odist = outpitchelems * bh;
int inembed[2] = { bh, bw };
int onembed[2] = { bh, outpitchelems };
int howmanyblocks = nox * noy;
dstvi = *srcvi;
vsapi->getVideoFormatByID(&dstvi.format, pfGrayS, core);
dstvi.width = outsize * 2; // 2 floats per complex number
dstvi.height = 1;
VSFrame *out = vsapi->newVideoFrame(&dstvi.format, dstvi.width, dstvi.height, nullptr, core);
fftwf_plan_with_nthreads(ncpu);
plan = std::unique_ptr<fftwf_plan_s, decltype(&fftwf_destroy_plan)>(fftwf_plan_many_dft_r2c(2, ndim, howmanyblocks,
in.get(), inembed, 1, idist, reinterpret_cast<fftwf_complex *>(vsapi->getWritePtr(out, 0)), onembed, 1, odist, planFlags), fftwf_destroy_plan);
fftwf_plan_with_nthreads(1);
vsapi->freeFrame(out);
// reset output format since it's only passed through
if (pshow)
outvi = *srcvi;
else
outvi = dstvi;
}
VSFrame *FFT3DFilterTransform::GetFrame(const VSFrame *src, VSCore *core, const VSAPI *vsapi) {
const VSVideoFormat *fi = vsapi->getVideoFrameFormat(src);
if (fi->bytesPerSample == 1) {
FramePlaneToCoverbuf<uint8_t>(plane, src, reinterpret_cast<uint8_t *>(coverbuf.get()), coverwidth, coverheight, coverpitch, mirw, mirh, interlaced, vsapi);
InitOverlapPlane<uint8_t>(in.get(), reinterpret_cast<uint8_t *>(coverbuf.get()), coverpitch, wanxl.get(), wanxr.get(), wanyl.get(), wanyr.get(), bw, bh, ow, oh, nox, noy, coverwidth, planeBase);
} else if (fi->bytesPerSample == 2) {
FramePlaneToCoverbuf<uint16_t>(plane, src, reinterpret_cast<uint16_t *>(coverbuf.get()), coverwidth, coverheight, coverpitch, mirw, mirh, interlaced, vsapi);
InitOverlapPlane<uint16_t>(in.get(), reinterpret_cast<uint16_t *>(coverbuf.get()), coverpitch, wanxl.get(), wanxr.get(), wanyl.get(), wanyr.get(), bw, bh, ow, oh, nox, noy, coverwidth, planeBase);
} else if (fi->bytesPerSample == 4) {
FramePlaneToCoverbuf<float>(plane, src, reinterpret_cast<float *>(coverbuf.get()), coverwidth, coverheight, coverpitch, mirw, mirh, interlaced, vsapi);
InitOverlapPlane<float>(in.get(), reinterpret_cast<float *>(coverbuf.get()), coverpitch, wanxl.get(), wanxr.get(), wanyl.get(), wanyr.get(), bw, bh, ow, oh, nox, noy, coverwidth, planeBase);
}
VSFrame *dst = vsapi->newVideoFrame(&dstvi.format, dstvi.width, dstvi.height, src, core);
fftwf_execute_dft_r2c(plan.get(), in.get(), reinterpret_cast<fftwf_complex *>(vsapi->getWritePtr(dst, 0)));
return dst;
}
const VSFrame *VS_CC FFT3DFilterTransform::GetFrame(int n, int activation_reason, void *instance_data, void **frame_data, VSFrameContext *frame_ctx, VSCore *core, const VSAPI *vsapi) {
FFT3DFilterTransform *data = reinterpret_cast<FFT3DFilterTransform *>(instance_data);
if (activation_reason == arInitial) {
vsapi->requestFrameFilter(n, data->node, frame_ctx);
} else if (activation_reason == arAllFramesReady) {
const VSFrame *src = vsapi->getFrameFilter(n, data->node, frame_ctx);
VSFrame *dst = data->GetFrame(src, core, vsapi);
vsapi->freeFrame(src);
return dst;
}
return nullptr;
}
const VSFrame *VS_CC FFT3DFilterTransform::GetPShowFrame(int n, int activation_reason, void *instance_data, void **frame_data, VSFrameContext *frame_ctx, VSCore *core, const VSAPI *vsapi) {
FFT3DFilterTransform *data = reinterpret_cast<FFT3DFilterTransform *>(instance_data);
if (activation_reason == arInitial) {
vsapi->requestFrameFilter(n, data->node, frame_ctx);
} else if (activation_reason == arAllFramesReady) {
const VSFrame *src = vsapi->getFrameFilter(n, data->node, frame_ctx);
VSFrame *dst = data->GetPShowInfo(src, core, vsapi);
vsapi->freeFrame(src);
return dst;
}
return nullptr;
}
const VSFrame *FFT3DFilterTransform::GetGridSample(VSCore *core, const VSAPI *vsapi) {
const VSVideoInfo *vi = vsapi->getVideoInfo(node);
int bytesPerSample = vi->format.bytesPerSample;
if (bytesPerSample == 1) {
memset(coverbuf.get(), 255, coverheight * coverpitch);
InitOverlapPlane(in.get(), reinterpret_cast<uint8_t *>(coverbuf.get()), coverpitch, wanxl.get(), wanxr.get(), wanyl.get(), wanyr.get(), bw, bh, ow, oh, nox, noy, coverwidth, 0);
} else if (bytesPerSample == 2) {
int maxval = (1 << vi->format.bitsPerSample) - 1;
fft3d_memset(reinterpret_cast<uint16_t *>(coverbuf.get()), static_cast<uint16_t>(maxval), coverheight * coverpitch / 2);
InitOverlapPlane(in.get(), reinterpret_cast<uint16_t *>(coverbuf.get()), coverpitch, wanxl.get(), wanxr.get(), wanyl.get(), wanyr.get(), bw, bh, ow, oh, nox, noy, coverwidth, 0);
} else if (bytesPerSample == 4) {
fft3d_memset(reinterpret_cast<float *>(coverbuf.get()), 1.f, coverheight * coverpitch / 4);
InitOverlapPlane(in.get(), reinterpret_cast<float *>(coverbuf.get()), coverpitch, wanxl.get(), wanxr.get(), wanyl.get(), wanyr.get(), bw, bh, ow, oh, nox, noy, coverwidth, 0);
}
VSFrame *dst = vsapi->newVideoFrame(&dstvi.format, dstvi.width, dstvi.height, nullptr, core);
fftwf_execute_dft_r2c(plan.get(), in.get(), reinterpret_cast<fftwf_complex *>(vsapi->getWritePtr(dst, 0)));
return dst;
}
//-------------------------------------------------------------------------------------------
static void FindPatternBlock(const fftwf_complex *outcur0, int outwidth, int outpitchelems, int bh, int nox, int noy, int &px, int &py, const float *pwin, float degrid, const fftwf_complex *gridsample) {
/* since v1.7 outwidth must be really an outpitchelems */
float sigmaSquared = 1e15f;
for (int by = 2; by < noy - 2; by++) {
for (int bx = 2; bx < nox - 2; bx++) {
const fftwf_complex *outcur = outcur0 + nox * by * bh * outpitchelems + bx * bh * outpitchelems;
float sigmaSquaredcur = 0;
float gcur = degrid * outcur[0][0] / gridsample[0][0]; /* grid (windowing) correction factor */
for (int h = 0; h < bh; h++) {
for (int w = 0; w < outwidth; w++) {
float grid0 = gcur * gridsample[w][0];
float grid1 = gcur * gridsample[w][1];
float corrected0 = outcur[w][0] - grid0;
float corrected1 = outcur[w][1] - grid1;
float psd = corrected0 * corrected0 + corrected1 * corrected1;
sigmaSquaredcur += psd * pwin[w]; /* windowing */
}
outcur += outpitchelems;
pwin += outpitchelems;
gridsample += outpitchelems;
}
pwin -= outpitchelems * bh; /* restore */
if (sigmaSquaredcur < sigmaSquared) {
px = bx;
py = by;
sigmaSquared = sigmaSquaredcur;
}
}
}
}
//-------------------------------------------------------------------------------------------
static void SetPattern(const fftwf_complex *outcur, int outwidth, int outpitchelems, int bh, int nox, int noy, int px, int py, const float *pwin, float *pattern2d, float &psigma, float degrid, const fftwf_complex *gridsample) {
outcur += nox * py * bh * outpitchelems + px * bh * outpitchelems;
float sigmaSquared = 0;
float weight = 0;
for (int h = 0; h < bh; h++) {
for (int w = 0; w < outwidth; w++) {
weight += pwin[w];
}
pwin += outpitchelems;
}
pwin -= outpitchelems * bh; /* restore */
float gcur = degrid * outcur[0][0] / gridsample[0][0]; /* grid (windowing) correction factor */
for (int h = 0; h < bh; h++) {
for (int w = 0; w < outwidth; w++) {
float grid0 = gcur * gridsample[w][0];
float grid1 = gcur * gridsample[w][1];
float corrected0 = outcur[w][0] - grid0;
float corrected1 = outcur[w][1] - grid1;
float psd = corrected0 * corrected0 + corrected1 * corrected1;
pattern2d[w] = psd * pwin[w]; /* windowing */
sigmaSquared += pattern2d[w]; /* sum */
}
outcur += outpitchelems;
pattern2d += outpitchelems;
pwin += outpitchelems;
gridsample += outpitchelems;
}
psigma = sqrt(sigmaSquared / (weight * bh * outwidth)); /* mean std deviation (sigma) */
}
void FFT3DFilterTransform::GetNoisePattern(int n, int &px, int &py, float *pattern2d, float &psigma, const fftwf_complex *gridsample, VSCore *core, const VSAPI *vsapi) {
const VSFrame *src = vsapi->getFrame(n, node, nullptr, 0);
VSFrame *dst = GetFrame(src, core, vsapi);
vsapi->freeFrame(src);
std::unique_ptr<float[]> pwin = std::unique_ptr<float[]>(new float[bh * outpitchelems]); /* pattern window array */
GetPatternWindow(bw, bh, outwidth, outpitchelems, pcutoff, pwin.get());
if (px == 0 && py == 0) /* try find pattern block with minimal noise sigma */
FindPatternBlock(reinterpret_cast<const fftwf_complex *>(vsapi->getReadPtr(dst, 0)), outwidth, outpitchelems, bh, nox, noy, px, py, pwin.get(), degrid, gridsample);
SetPattern(reinterpret_cast<const fftwf_complex *>(vsapi->getReadPtr(dst, 0)), outwidth, outpitchelems, bh, nox, noy, px, py, pwin.get(), pattern2d, psigma, degrid, gridsample);
}
VSFrame *FFT3DFilterTransform::GetPShowInfo(const VSFrame *src, VSCore *core, const VSAPI *vsapi) {
// accept a lot of extra recalculation and allocation when visualizing
// should be fast enough since it's all spatial anyway
// requires px, py, pcutoff, degrid
VSFrame *transformed = GetFrame(src, core, vsapi);
const VSFrame *gridsample = GetGridSample(core, vsapi);
std::unique_ptr<float[]> pwin = std::unique_ptr<float[]>(new float[bh * outpitchelems]); /* pattern window array */
GetPatternWindow(bw, bh, outwidth, outpitchelems, pcutoff, pwin.get());
int pxf, pyf;
if (px == 0 && py == 0) { /* try find pattern block with minimal noise sigma */
FindPatternBlock(reinterpret_cast<const fftwf_complex *>(vsapi->getReadPtr(transformed, 0)), outwidth, outpitchelems, bh, nox, noy, pxf, pyf, pwin.get(), degrid, reinterpret_cast<const fftwf_complex *>(vsapi->getReadPtr(gridsample, 0)));
} else {
pxf = px;
pyf = py;
}
float psigma;
std::unique_ptr<float[], decltype(&fftw_free)> pattern2d = std::unique_ptr<float[], decltype(&fftw_free)>(fftwf_alloc_real(bh * outpitchelems), fftwf_free);
SetPattern(reinterpret_cast<const fftwf_complex *>(vsapi->getReadPtr(transformed, 0)), outwidth, outpitchelems, bh, nox, noy, pxf, pyf, pwin.get(), pattern2d.get(), psigma, degrid, reinterpret_cast<const fftwf_complex *>(vsapi->getReadPtr(gridsample, 0)));
vsapi->freeFrame(transformed);
vsapi->freeFrame(gridsample);
VSFrame *dst = vsapi->copyFrame(src, core);
VSMap *props = vsapi->getFramePropertiesRW(dst);
vsapi->mapSetInt(props, "px", pxf, maReplace);
vsapi->mapSetInt(props, "py", pyf, maReplace);
vsapi->mapSetFloat(props, "sigma", psigma, maReplace);
return dst;
}
void VS_CC FFT3DFilterTransform::Free(void *instance_data, VSCore *core, const VSAPI *vsapi) {
FFT3DFilterTransform *data = reinterpret_cast<FFT3DFilterTransform *>(instance_data);
vsapi->freeNode(data->node);
delete data;
}
//-----------------------------------------------------------------------------------------
FFT3DFilterInvTransform::FFT3DFilterInvTransform(VSNode *node_, const VSVideoInfo *srcvi, int plane, int wintype, int bw_, int bh_, int ow_, int oh_, bool interlaced_, bool measure, int ncpu, VSCore *core, const VSAPI *vsapi) : node(node_), bw(bw_), bh(bh_), ow(ow_), oh(oh_), interlaced(interlaced_), in(nullptr, nullptr), planinv(nullptr, nullptr) {
if (ow < 0)
ow = bw / 3;
if (oh < 0)
oh = bh / 3;
planeBase = (plane > 0 && srcvi->format.sampleType == stInteger && srcvi->format.colorFamily == cfYUV) ? (1 << (srcvi->format.bitsPerSample - 1)) : 0;
nox = ((srcvi->width >> (plane ? srcvi->format.subSamplingW : 0)) - ow + (bw - ow - 1)) / (bw - ow);
noy = ((srcvi->height >> (plane ? srcvi->format.subSamplingH : 0)) - oh + (bh - oh - 1)) / (bh - oh);
wsynxl = std::unique_ptr<float[]>(new float[ow]);
wsynxr = std::unique_ptr<float[]>(new float[ow]);
wsynyl = std::unique_ptr<float[]>(new float[oh]);
wsynyr = std::unique_ptr<float[]>(new float[oh]);
GetSynthesisWindow(wintype, ow, oh, wsynxl.get(), wsynxr.get(), wsynyl.get(), wsynyr.get());
/* padding by 1 block per side */
nox += 2;
noy += 2;
mirw = bw - ow; /* set mirror size as block interval */
mirh = bh - oh;
coverwidth = nox * (bw - ow) + ow;
coverheight = noy * (bh - oh) + oh;
coverpitch = ((coverwidth + 7) / 8) * 8 * srcvi->format.bytesPerSample;
coverbuf = std::unique_ptr<uint8_t[]>(new uint8_t[coverheight * coverpitch]);
int insize = bw * bh * nox * noy;
in = std::unique_ptr<float[], decltype(&fftw_free)>(fftwf_alloc_real(insize), fftwf_free);
outwidth = bw / 2 + 1; /* width (pitch) of complex fft block */
outpitchelems = ((outwidth + 1) / 2) * 2;
norm = 1.0f / (bw * bh); /* do not forget set FFT normalization factor */
// FFTW_PRESERVE_INPUT would be preferred but it's not implemented due to the infinite greatness of FFTW
int planFlags = (measure ? FFTW_MEASURE : FFTW_ESTIMATE) | FFTW_DESTROY_INPUT; // needed since a read only frame is the source
int ndim[2] = { bh, bw };
int idist = bw * bh;
int odist = outpitchelems * bh;
int inembed[2] = { bh, bw };
int onembed[2] = { bh, outpitchelems };
int howmanyblocks = nox * noy;
dstvi = *srcvi;
dstvi.width = (srcvi->width >> (plane ? srcvi->format.subSamplingW : 0));