-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathFFT3DFilter.cpp
1851 lines (1730 loc) · 85.5 KB
/
FFT3DFilter.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 file from this package to some directory in path
* (for example, C:\WINNT\System32).
*
* 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
*
* For AviSynth 2.5
* Version 0.1, 23 November 2004 - initial
* Version 0.2, 3 December 2004 - add beta parameter of noise margin
* Version 0.3, 21 December 2004 - add bt parameter of temporal size
* Version 0.4, 16 January 2005 - algorithm optimized for speed for bt=2 (now default),
* mode bt=3 is temporary disabled, changed default bw=bh=32, filtered region now centered.
* Version 0.5, 28 January 2005 - added YUY2 support
* Version 0.6, 29 January 2005 - added Kalman filter mode for bt=0, ratio parameter
* Version 0.7, 30 January 2005 - re-enabled Wiener filter mode with 3 frames (bt=3)
* Version 0.8, 05 February2005 - added option to sharpen, and bt=-1
* Version 0.8.1, 6 February2005 - skip sharpening of the lowest frequencies to prevent parasitic lines near border
* Version 0.8.2, February 15, 2005 - added internal buffer to process whole frame (borders included) for any bw, bh (a little slower)
* Version 0.8.3, March 16, 2005 - fixed sharpen mode (bt=-1) for YUY2
* Version 0.8.4, April 3, 2005 - delayed FFTW3.DLL loading
* Version 0.9 - April 3,2005 - variable overlapping size
* Version 0.9.1 - April 7,2005 - some assembler 3DNow! optimization for mode bt=3
* Version 0.9.2 - April 10,2005 - some assembler 3DNow! optimization for mode bt=2,
* option measure=true is now default as more fast
* Version 0.9.3 - April 24,2005 - bug fixed for bt=2 with 3DNow; * bt=3 now default;
* modifyed sharpen to horizontal only (still experimental)
* Version 1.0 - June 22, 2005 - improved edges processing (by padding);
* added svr parameter to control vertical sharpening
* Version 1.0.1 - July 05, 2005 - fixed bug for YUY2 chroma planes
* Version 1.1 - July 8,2005 - improved sharpen mode to prevent grid artifactes and to limit sharpening,
* added parameters smin, smax; renamed parameter ratio to kratio.
* Version 1.2 - July 12, 2005 - changed parameters defaults (bw=bh=48, ow=bw/3, oh=bh/3) to prevent grid artifactes
* Version 1.3 - July 20, 2005 - added interlaced mode
* Version 1.3.1 - July 21, 2005 - fixed bug for YUY2 interlaced
* Version 1.4 - July 23, 2005 - corrected neutral level for chroma processing, added wintype to decrease grid artefactes
* Version 1.5 - July 26, 2005 - added noise pattern method and its parameters pframe, px, py, pshow, pcutoff, pfactor
* Version 1.5.1 - July 29, 2005 - fixed bug with pshow
* Version 1.5.2 - July 31, 2005 - fixed bug with Kalman mode (bt=0) for Athlon (introduced in v1.5)
* Version 1.6 - August 01, 2005 - added mode bt=4; optimized SSE version for bt=2,3
* Version 1.7 - August 29, 2005 - added SSE version for for sharpen and pattern modes bt=2,3 ; restuctured code, GPL v2
* Version 1.8 - September 6, 2005 - improved internal fft cache; added degrid=0; changed wintype=0
* Version 1.8.1 - October 26, 2005 - fixed bug with sharpen>0 AND degrid>0 for bt not equal 1.
* Version 1.8.2 - November 04, 2005 - really set default degrid=1.0 (was = 0)
* Version 1.8.3 - November 28, 2005 - fixed bug with first frame for Kalman YV12 (thanks to Tsp)
* Version 1.8.4 - November 29, 2005 - added multiplane modes plane=3,4
* Version 1.8.5 - 4 December 2005 - fixed bug with memory leakage (thanks to tsp).
* Version 1.9 - April 25, 2006 - added dehalo options; corrected sharpen mode a little;
* re-enabled 3DNow and SSE optimization for degrid=0; added SSE optimization for bt=3,-1 with degrid>0 (faster by 15%)
* Version 1.9.1 - May 10, 2006 - added SSE optimization for bt=4 with degrid>0 (faster by 30%)
* Version 1.9.2 - September 6, 2006 - added new mode bt=5
* Version 2.0.0 - november 6, 2006 - added motion compensation mc parameter, window reorganized, multi-cpu
* Version 2.1.0 - January 17, 2007 - removed motion compensation mc parameter
* Version 2.1.1 - February 19, 2007 - fixed bug with bw not mod 4 (restored v1.9.2 window method)
*
* For VapourSynth
* February 2, 2015 - imported for VapourSynth without SIMD optimizations.
* February 4, 2015 - stopped using explicit linking to FFTW3 library.
*****************************************************************************/
#include <cstring>
#include <algorithm>
#include "FFT3DFilter.h"
#include "info.h"
/** declarations of filtering functions: **/
/* C */
void ApplyWiener2D_C( fftwf_complex *out, int outwidth, int outpitch, 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 *outcur, int outwidth, int outpitch, int bh, int howmanyblocks, float pfactor, const float *pattern2d0, float beta );
void ApplyWiener3D2_C( const fftwf_complex *outcur, fftwf_complex *outprev, int outwidth, int outpitch, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta );
void ApplyPattern3D2_C( const fftwf_complex *outcur, fftwf_complex *outprev, int outwidth, int outpitch, int bh, int howmanyblocks, const float *pattern3d, float beta );
void ApplyWiener3D3_C( const fftwf_complex *out, fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitch, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta );
void ApplyPattern3D3_C( const fftwf_complex *out, fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitch, int bh, int howmanyblocks, const float *pattern3d, float beta );
void ApplyWiener3D4_C( const fftwf_complex *out, fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitch, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta );
void ApplyPattern3D4_C( const fftwf_complex *out, fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitch, int bh, int howmanyblocks, const float *pattern3d, float beta );
void ApplyWiener3D5_C( const fftwf_complex *out, fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, const fftwf_complex *outnext2, int outwidth, int outpitch, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta );
void ApplyPattern3D5_C( const fftwf_complex *out, fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, const fftwf_complex *outnext2, int outwidth, int outpitch, int bh, int howmanyblocks, const float *pattern3d, float beta );
void ApplyKalmanPattern_C( const fftwf_complex *outcur, fftwf_complex *outLast, fftwf_complex *covar, fftwf_complex *covarProcess, int outwidth, int outpitch, int bh, int howmanyblocks, const float *covarNoiseNormed, float kratio2 );
void ApplyKalman_C( const fftwf_complex *outcur, fftwf_complex *outLast, fftwf_complex *covar, fftwf_complex *covarProcess, int outwidth, int outpitch, int bh, int howmanyblocks, float covarNoiseNormed, float kratio2 );
void Sharpen_C( fftwf_complex *outcur, int outwidth, int outpitch, 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 *out, int outwidth, int outpitch, 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( const fftwf_complex *outcur, fftwf_complex *outprev, int outwidth, int outpitch, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta, float degrid, const fftwf_complex *gridsample );
void ApplyWiener3D3_degrid_C( const fftwf_complex *outcur, fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitch, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta, float degrid, const fftwf_complex *gridsample );
void ApplyWiener3D4_degrid_C( const fftwf_complex *outcur, fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitch, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta, float degrid, const fftwf_complex *gridsample );
void ApplyWiener3D5_degrid_C( const fftwf_complex *outcur, fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, const fftwf_complex *outnext2, int outwidth, int outpitch, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta, float degrid, const fftwf_complex *gridsample );
void Sharpen_degrid_C( fftwf_complex *outcur, int outwidth, int outpitch, 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 *outcur, int outwidth, int outpitch, int bh, int howmanyblocks, float pfactor, const float *pattern2d0, float beta, float degrid, const fftwf_complex *gridsample );
void ApplyPattern3D2_degrid_C( const fftwf_complex *outcur, fftwf_complex *outprev, int outwidth, int outpitch, int bh, int howmanyblocks, const float *pattern3d, float beta, float degrid, const fftwf_complex *gridsample );
void ApplyPattern3D3_degrid_C( const fftwf_complex *out, fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitch, int bh, int howmanyblocks, const float *pattern3d, float beta, float degrid, const fftwf_complex *gridsample );
void ApplyPattern3D4_degrid_C( const fftwf_complex *out, fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, int outwidth, int outpitch, int bh, int howmanyblocks, const float *pattern3d, float beta, float degrid, const fftwf_complex *gridsample );
void ApplyPattern3D5_degrid_C( const fftwf_complex *out, fftwf_complex *outprev2, const fftwf_complex *outprev, const fftwf_complex *outnext, const fftwf_complex *outnext2, int outwidth, int outpitch, int bh, int howmanyblocks, const float *pattern3d, float beta, float degrid, const fftwf_complex *gridsample );
//-------------------------------------------------------------------------------------------
static void ApplyWiener2D( fftwf_complex *out, int outwidth, int outpitch, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed,
float beta, float sharpen, float sigmaSquaredSharpenMin, float sigmaSquaredSharpenMax, const float *wsharpen, float dehalo, const float *wdehalo, float ht2n )
{
ApplyWiener2D_C( out, outwidth, outpitch, bh, howmanyblocks, sigmaSquaredNoiseNormed, beta, sharpen, sigmaSquaredSharpenMin, sigmaSquaredSharpenMax, wsharpen, dehalo, wdehalo, ht2n );
}
//-------------------------------------------------------------------------------------------
static void ApplyPattern2D( fftwf_complex *outcur, int outwidth, int outpitch, int bh, int howmanyblocks, float pfactor, const float *pattern2d0, float beta )
{
ApplyPattern2D_C( outcur, outwidth, outpitch, bh, howmanyblocks, pfactor, pattern2d0, beta );
}
//-------------------------------------------------------------------------------------------
template < int btcur >
static void ApplyWiener3D_degrid( const fftwf_complex *out, fftwf_complex *outprev2, fftwf_complex *outprev, const fftwf_complex *outnext, const fftwf_complex *outnext2, int outwidth, int outpitch, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta, float degrid, fftwf_complex *gridsample )
{
if( btcur == 5 ) ApplyWiener3D5_degrid_C( out, outprev2, outprev, outnext, outnext2, outwidth, outpitch, bh, howmanyblocks, sigmaSquaredNoiseNormed, beta, degrid, gridsample );
if( btcur == 4 ) ApplyWiener3D4_degrid_C( out, outprev2, outprev, outnext, outwidth, outpitch, bh, howmanyblocks, sigmaSquaredNoiseNormed, beta, degrid, gridsample );
if( btcur == 3 ) ApplyWiener3D3_degrid_C( out, outprev, outnext, outwidth, outpitch, bh, howmanyblocks, sigmaSquaredNoiseNormed, beta, degrid, gridsample );
if( btcur == 2 ) ApplyWiener3D2_degrid_C( out, outprev, outwidth, outpitch, bh, howmanyblocks, sigmaSquaredNoiseNormed, beta, degrid, gridsample );
}
//-------------------------------------------------------------------------------------------
template < int btcur >
static void ApplyPattern3D_degrid( const fftwf_complex *out, fftwf_complex *outprev2, fftwf_complex *outprev, const fftwf_complex *outnext, const fftwf_complex *outnext2, int outwidth, int outpitch, int bh, int howmanyblocks, float *pattern3d, float beta, float degrid, fftwf_complex *gridsample )
{
if( btcur == 5 ) ApplyPattern3D5_degrid_C( out, outprev2, outprev, outnext, outnext2, outwidth, outpitch, bh, howmanyblocks, pattern3d, beta, degrid, gridsample );
if( btcur == 4 ) ApplyPattern3D4_degrid_C( out, outprev2, outprev, outnext, outwidth, outpitch, bh, howmanyblocks, pattern3d, beta, degrid, gridsample );
if( btcur == 3 ) ApplyPattern3D3_degrid_C( out, outprev, outnext, outwidth, outpitch, bh, howmanyblocks, pattern3d, beta, degrid, gridsample );
if( btcur == 2 ) ApplyPattern3D2_degrid_C( out, outprev, outwidth, outpitch, bh, howmanyblocks, pattern3d, beta, degrid, gridsample );
}
//-------------------------------------------------------------------------------------------
template < int btcur >
static void ApplyWiener3D( const fftwf_complex *out, fftwf_complex *outprev2, fftwf_complex *outprev, const fftwf_complex *outnext, const fftwf_complex *outnext2, int outwidth, int outpitch, int bh, int howmanyblocks, float sigmaSquaredNoiseNormed, float beta )
{
if( btcur == 5 ) ApplyWiener3D5_C( out, outprev2, outprev, outnext, outnext2, outwidth, outpitch, bh, howmanyblocks, sigmaSquaredNoiseNormed, beta );
if( btcur == 4 ) ApplyWiener3D4_C( out, outprev2, outprev, outnext, outwidth, outpitch, bh, howmanyblocks, sigmaSquaredNoiseNormed, beta );
if( btcur == 3 ) ApplyWiener3D3_C( out, outprev, outnext, outwidth, outpitch, bh, howmanyblocks, sigmaSquaredNoiseNormed, beta );
if( btcur == 2 ) ApplyWiener3D2_C( out, outprev, outwidth, outpitch, bh, howmanyblocks, sigmaSquaredNoiseNormed, beta );
}
//-------------------------------------------------------------------------------------------
template < int btcur >
static void ApplyPattern3D( const fftwf_complex *out, fftwf_complex *outprev2, fftwf_complex *outprev, const fftwf_complex *outnext, const fftwf_complex *outnext2, int outwidth, int outpitch, int bh, int howmanyblocks, const float *pattern3d, float beta )
{
if( btcur == 5 ) ApplyPattern3D5_C( out, outprev2, outprev, outnext, outnext2, outwidth, outpitch, bh, howmanyblocks, pattern3d, beta );
if( btcur == 4 ) ApplyPattern3D4_C( out, outprev2, outprev, outnext, outwidth, outpitch, bh, howmanyblocks, pattern3d, beta );
if( btcur == 3 ) ApplyPattern3D3_C( out, outprev, outnext, outwidth, outpitch, bh, howmanyblocks, pattern3d, beta );
if( btcur == 2 ) ApplyPattern3D2_C( out, outprev, outwidth, outpitch, bh, howmanyblocks, pattern3d, beta );
}
//-------------------------------------------------------------------------------------------
static void ApplyKalmanPattern( const fftwf_complex *outcur, fftwf_complex *outLast, fftwf_complex *covar, fftwf_complex *covarProcess, int outwidth, int outpitch, int bh, int howmanyblocks, const float *covarNoiseNormed, float kratio2 )
{
ApplyKalmanPattern_C( outcur, outLast, covar, covarProcess, outwidth, outpitch, bh, howmanyblocks, covarNoiseNormed, kratio2 );
}
//-------------------------------------------------------------------------------------------
static void ApplyKalman( const fftwf_complex *outcur, fftwf_complex *outLast, fftwf_complex *covar, fftwf_complex *covarProcess, int outwidth, int outpitch, int bh, int howmanyblocks, float covarNoiseNormed, float kratio2 )
{
ApplyKalman_C( outcur, outLast, covar, covarProcess, outwidth, outpitch, bh, howmanyblocks, covarNoiseNormed, kratio2 );
}
//-------------------------------------------------------------------------------------------
static void Sharpen( fftwf_complex *outcur, int outwidth, int outpitch, int bh, int howmanyblocks, float sharpen, float sigmaSquaredSharpenMin, float sigmaSquaredSharpenMax, const float *wsharpen, float dehalo, const float *wdehalo, float ht2n )
{
Sharpen_C( outcur, outwidth, outpitch, bh, howmanyblocks, sharpen, sigmaSquaredSharpenMin, sigmaSquaredSharpenMax, wsharpen, dehalo, wdehalo, ht2n );
}
//-------------------------------------------------------------------------------------------
static void Sharpen_degrid( fftwf_complex *outcur, int outwidth, int outpitch, 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 )
{
Sharpen_degrid_C( outcur, outwidth, outpitch, bh, howmanyblocks, sharpen, sigmaSquaredSharpenMin, sigmaSquaredSharpenMax, wsharpen, degrid, gridsample, dehalo, wdehalo, ht2n );
}
//-------------------------------------------------------------------------------------------
//-------------------------------------------------------------------
static void fill_complex( fftwf_complex *plane, int outsize, float realvalue, float imgvalue)
{
/* it is not fast, but called only in constructor */
for( int w = 0; w < outsize; w++ )
{
plane[w][0] = realvalue;
plane[w][1] = imgvalue;
}
}
//-------------------------------------------------------------------
static void SigmasToPattern( float sigma, float sigma2, float sigma3, float sigma4, int bh, int outwidth, int outpitch, float norm, float *pattern2d )
{
/* it is not fast, but called only in constructor */
float sigmacur;
constexpr float ft2 = sqrt( 0.5f ) / 2; /* frequency for sigma2 */
constexpr float ft3 = sqrt( 0.5f ) / 4; /* frequency for sigma3 */
for( int h = 0; h < bh; h++ )
{
for( int w = 0; w < outwidth; w++ )
{
float fy = (bh - 2.0f * abs( h - bh / 2)) / bh; /* normalized to 1 */
float fx = (w * 1.0f) / outwidth; /* normalized to 1 */
float f = sqrt( (fx * fx + fy * fy) * 0.5f ); /* normalized to 1 */
if( f < ft3 )
{ /* low frequencies */
sigmacur = sigma4 + (sigma3 - sigma4) * f / ft3;
}
else if( f < ft2 )
{ /* middle frequencies */
sigmacur = sigma3 + (sigma2 - sigma3) * (f - ft3) / (ft2 - ft3);
}
else
{ /* high frequencies */
sigmacur = sigma + (sigma2 - sigma) * (1 - f) / (1 - ft2);
}
pattern2d[w] = sigmacur * sigmacur / norm;
}
pattern2d += outpitch;
}
}
//-------------------------------------------------------------------------------------------
static void BitBlt
(
void *dstp,
int dst_stride,
const void *srcp,
int src_stride,
int row_size,
int height
)
{
if( height <= 0 )
return;
if( src_stride == dst_stride
&& src_stride == row_size )
std::memcpy( dstp, srcp, row_size * height );
else
{
unsigned char *srcp8 = static_cast<unsigned char *>(const_cast<void *>(srcp));
unsigned char *dstp8 = static_cast<unsigned char *> (dstp);
for( int i = 0; i < height; ++i )
{
std::memcpy( dstp8, srcp8, row_size );
srcp8 += src_stride;
dstp8 += dst_stride;
}
}
}
//-------------------------------------------------------------------
FFT3DFilter::FFT3DFilter
(
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,
bool _measure, bool _interlaced, int _wintype,
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, int _multiplane,
VSVideoInfo _vi, VSNodeRef *_node
) : sigma( _sigma ), beta( _beta ), plane( _plane ), bw( _bw ), bh( _bh ), bt( _bt ), ow( _ow ), oh( _oh ),
kratio( _kratio ), sharpen( _sharpen ), scutoff( _scutoff ), svr( _svr ), smin( _smin ), smax( _smax ),
measure( _measure ), interlaced( _interlaced ), wintype( _wintype ),
pframe( _pframe ), px( _px ), py( _py ), pshow( _pshow ), pcutoff( _pcutoff ), pfactor( _pfactor ),
sigma2( _sigma2 ), sigma3( _sigma3 ), sigma4( _sigma4 ), degrid( _degrid ),
dehalo( _dehalo ), hr( _hr ), ht( _ht ), ncpu( _ncpu ), multiplane( _multiplane ),
vi( _vi ), node( _node )
{
int i, j;
//_asm emms;
if( ow * 2 > bw ) throw bad_param{ "Must not be 2*ow > bw" };
if( oh * 2 > bh ) throw bad_param{ "Must not be 2*oh > bh" };
if( ow < 0 ) ow = bw / 3; /* changed from bw/4 to bw/3 in v.1.2 */
if( oh < 0 ) oh = bh / 3; /* changed from bh/4 to bh/3 in v.1.2 */
if( bt < -1 || bt > 5 ) throw bad_param{ "bt must be -1(Sharpen), 0(Kalman), 1,2,3,4,5(Wiener)" };
if( vi.format->colorFamily != cmGray && vi.format->colorFamily != cmYUV )
throw bad_param{ "only planar YUV formats are supported" };
if( vi.format->bitsPerSample != 8 )
throw bad_param{ "only 8-bit formats are supported" };
if( plane < 3 )
{
nox = ((vi.width >> (plane ? vi.format->subSamplingW : 0)) - ow + (bw - ow - 1)) / (bw - ow);
noy = ((vi.height >> (plane ? vi.format->subSamplingW : 0)) - oh + (bh - oh - 1)) / (bh - oh);
}
else
throw bad_param{ "internal plane must be 0, 1 or 2" };
/* padding by 1 block per side */
nox += 2;
noy += 2;
mirw = bw - ow; /* set mirror size as block interval */
mirh = bh - oh;
if( beta < 1 )
throw bad_param{ "beta must be not less 1.0" };
int istat = fftwf_init_threads();
if( istat == 0 )
throw bad_open{ "fftwf_init_threads() failed!" };
coverwidth = nox * ( bw - ow ) + ow;
coverheight = noy * ( bh - oh ) + oh;
coverpitch = ((coverwidth + 7) / 8 ) * 8;
coverbuf = (uint8_t *)malloc( coverheight * coverpitch );
int insize = bw * bh * nox * noy;
in = (float *)fftwf_malloc( sizeof(float) * insize );
outwidth = bw / 2 + 1; /* width (pitch) of complex fft block */
outpitch = ((outwidth + 1) / 2) * 2; /* must be even for SSE - v1.7 */
outsize = outpitch * bh * nox * noy; /* replace outwidth to outpitch here and below in v1.7 */
if( bt == 0 ) /* Kalman */
{
outLast = (fftwf_complex *)fftwf_malloc( sizeof(fftwf_complex) * outsize );
covar = (fftwf_complex *)fftwf_malloc( sizeof(fftwf_complex) * outsize );
covarProcess = (fftwf_complex *)fftwf_malloc( sizeof(fftwf_complex) * outsize );
}
outrez = (fftwf_complex *)fftwf_malloc( sizeof(fftwf_complex) * outsize ); /* v1.8 */
gridsample = (fftwf_complex *)fftwf_malloc( sizeof(fftwf_complex) * outsize ); /* v1.8 */
/* fft cache - added in v1.8 */
cachesize = bt + 2;
cachewhat = (int *)malloc( sizeof(int) * cachesize );
cachefft = (fftwf_complex **)fftwf_malloc( sizeof(fftwf_complex *) * cachesize );
for( i = 0; i < cachesize; i++ )
{
cachefft [i] = (fftwf_complex *)fftwf_malloc( sizeof(fftwf_complex) * outsize );
cachewhat[i] = -1; /* init as notexistant */
}
int planFlags;
/* use FFTW_ESTIMATE or FFTW_MEASURE (more optimal plan, but with time calculation at load stage) */
if( measure )
planFlags = FFTW_MEASURE;
else
planFlags = FFTW_ESTIMATE;
int rank = 2; /* 2d */
ndim[0] = bh; /* size of block along height */
ndim[1] = bw; /* size of block along width */
int istride = 1;
int ostride = 1;
int idist = bw * bh;
int odist = outpitch * bh;/* v1.7 (was outwidth) */
inembed[0] = bh;
inembed[1] = bw;
onembed[0] = bh;
onembed[1] = outpitch; /* v1.7 (was outwidth) */
howmanyblocks = nox * noy;
fftwf_plan_with_nthreads( ncpu );
plan = fftwf_plan_many_dft_r2c( rank, ndim, howmanyblocks,
in, inembed, istride, idist, outrez, onembed, ostride, odist, planFlags );
if( plan == nullptr )
throw bad_plan{ "fftwf_plan_many_dft_r2c" };
planinv = fftwf_plan_many_dft_c2r( rank, ndim, howmanyblocks,
outrez, onembed, ostride, odist, in, inembed, istride, idist, planFlags );
if( planinv == nullptr )
throw bad_plan{ "fftwf_plan_many_dft_c2r" };
fftwf_plan_with_nthreads( 1 );
wanxl = (float *)malloc( ow * sizeof(float) );
wanxr = (float *)malloc( ow * sizeof(float) );
wanyl = (float *)malloc( oh * sizeof(float) );
wanyr = (float *)malloc( oh * sizeof(float) );
wsynxl = (float *)malloc( ow * sizeof(float) );
wsynxr = (float *)malloc( ow * sizeof(float) );
wsynyl = (float *)malloc( oh * sizeof(float) );
wsynyr = (float *)malloc( oh * sizeof(float) );
wsharpen = (float *)fftwf_malloc( bh * outpitch * sizeof(float) );
wdehalo = (float *)fftwf_malloc( bh * outpitch * sizeof(float) );
/* define analysis and synthesis windows
* combining window (analize mult by synthesis) is raised cosine (Hanning) */
constexpr float pi = 3.1415926535897932384626433832795f;
if( wintype == 0 ) /* window type */
{ /* , used in all version up to 1.3
* half-cosine, the same for analysis and synthesis
* define analysis windows */
for( 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( i = 0; i < oh; i++ )
{
wanyl[i] = cosf( pi * (i - oh + 0.5f) / (oh * 2) );
wanyr[i] = cosf( pi * (i + 0.5f) / (oh * 2) );
}
/* use the same windows for synthesis too. */
for( i = 0; i < ow; i++ )
{
wsynxl[i] = wanxl[i]; /* left window (half-cosine) */
wsynxr[i] = wanxr[i]; /* right window (half-cosine) */
}
for( i = 0; i < oh; i++ )
{
wsynyl[i] = wanyl[i];
wsynyr[i] = wanyr[i];
}
}
else if( wintype == 1 ) /* added in v.1.4 */
{
/* define analysis windows as more flat (to decrease grid) */
for( 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( 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) ) );
}
/* define synthesis as supplenent to rised cosine (Hanning) */
for( i = 0; i < ow; i++ )
{
wsynxl[i] = wanxl[i] * wanxl[i] * wanxl[i]; /* left window */
wsynxr[i] = wanxr[i] * wanxr[i] * wanxr[i]; /* right window */
}
for( i = 0; i < oh; i++ )
{
wsynyl[i] = wanyl[i] * wanyl[i] * wanyl[i];
wsynyr[i] = wanyr[i] * wanyr[i] * wanyr[i];
}
}
else /* (wintype==2) - added in v.1.4 */
{
/* define analysis windows as flat (to prevent grid) */
for( i = 0; i < ow; i++ )
{
wanxl[i] = 1;
wanxr[i] = 1;
}
for( i = 0; i < oh; i++ )
{
wanyl[i] = 1;
wanyr[i] = 1;
}
/* define synthesis as rised cosine (Hanning) */
for( 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( 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];
}
}
/* window for sharpen */
for( j = 0; j < bh; j++ )
{
int dj = j;
if( j >= bh / 2)
dj = bh - j;
float d2v = float(dj * dj) * (svr * svr) / ((bh / 2) * (bh / 2)); /* v1.7 */
for( i = 0; i < outwidth; i++ )
{
float d2 = d2v + float(i * i) / ((bw / 2) * (bw / 2)); /* distance_2 - v1.7 */
wsharpen[i] = 1 - exp( -d2 / (2 * scutoff * scutoff) );
}
wsharpen += outpitch;
}
wsharpen -= outpitch * bh; /* restore pointer */
/* window for dehalo - added in v1.9 */
float wmax = 0;
for( j = 0; j < bh; j++ )
{
int dj = j;
if( j >= bh / 2 )
dj = bh - j;
float d2v = float(dj * dj) * (svr * svr) / ((bh / 2) * (bh / 2));
for( i = 0; i < outwidth; i++ )
{
float d2 = d2v + float(i * i) / ((bw / 2) * (bw / 2)); /* squared distance in frequency domain */
//float d1 = sqrt( d2 );
wdehalo[i] = exp( -0.7f * d2 * hr * hr ) - exp( -d2 * hr * hr ); /* some window with max around 1/hr, small at low and high frequencies */
if( wdehalo[i] > wmax )
wmax = wdehalo[i]; /* for normalization */
}
wdehalo += outpitch;
}
wdehalo -= outpitch * bh; /* restore pointer */
for( j = 0; j < bh; j++ )
{
for( i = 0; i < outwidth; i++ )
{
wdehalo[i] /= wmax; /* normalize */
}
wdehalo += outpitch;
}
wdehalo -= outpitch * bh; /* restore pointer */
/* init nlast */
nlast = -999; /* init as nonexistant */
btcurlast = -999; /* init as nonexistant */
norm = 1.0f / (bw * bh); /* do not forget set FFT normalization factor */
sigmaSquaredNoiseNormed2D = sigma * sigma / norm;
sigmaNoiseNormed2D = sigma / sqrtf( norm );
sigmaMotionNormed = sigma * kratio / sqrtf( norm );
sigmaSquaredSharpenMinNormed = smin * smin / norm;
sigmaSquaredSharpenMaxNormed = smax * smax / norm;
ht2n = ht * ht / norm; /* halo threshold squared and normed - v1.9 */
/* init Kalman */
if( bt == 0 ) /* Kalman */
{
fill_complex( outLast, outsize, 0, 0 );
fill_complex( covar, outsize, sigmaSquaredNoiseNormed2D, sigmaSquaredNoiseNormed2D );
fill_complex( covarProcess, outsize, sigmaSquaredNoiseNormed2D, sigmaSquaredNoiseNormed2D );
}
mean = (float *)malloc( nox * noy * sizeof(float) );
pwin = (float *)malloc( bh * outpitch * sizeof(float) ); /* pattern window array */
for( 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( i = 0; i < outwidth; i++ )
{
float fw2 = (i * 2.0f / bw) * (j * 2.0f / bw);
pwin[i] = (fh2 + fw2) / (fh2 + fw2 + pcutoff * pcutoff);
}
pwin += outpitch;
}
pwin -= outpitch * bh; /* restore pointer */
pattern2d = (float *)fftwf_malloc( bh * outpitch * sizeof(float) ); /* noise pattern window array */
pattern3d = (float *)fftwf_malloc( bh * outpitch * sizeof(float) ); /* noise pattern window array */
if( (sigma2 != sigma || sigma3 != sigma || sigma4 != sigma) && pfactor == 0 )
{ /* we have different sigmas, so create pattern from sigmas */
SigmasToPattern( sigma, sigma2, sigma3, sigma4, bh, outwidth, outpitch, norm, pattern2d );
isPatternSet = true;
pfactor = 1;
}
else
{
isPatternSet = false; /* pattern must be estimated */
}
/* prepare window compensation array gridsample
* allocate large array for simplicity :)
* but use one block only for speed
* Attention: other block could be the same, but we do not calculate them! */
plan1 = fftwf_plan_many_dft_r2c( rank, ndim, 1,
in, inembed, istride, idist, outrez, onembed, ostride, odist, planFlags ); /* 1 block */
memset( coverbuf, 255, coverheight * coverpitch );
FFT3DFilter::InitOverlapPlane( in, coverbuf, coverpitch, 0 );
/* make FFT 2D */
fftwf_execute_dft_r2c( plan1, in, gridsample );
messagebuf = (char *)malloc( 80 ); /* 1.8.5 */
}
//-------------------------------------------------------------------------------------------
FFT3DFilter::~FFT3DFilter()
{
fftwf_destroy_plan( plan );
fftwf_destroy_plan( plan1 );
fftwf_destroy_plan( planinv );
fftwf_free( in );
free( wanxl );
free( wanxr );
free( wanyl );
free( wanyr );
free( wsynxl );
free( wsynxr );
free( wsynyl );
free( wsynyr );
fftwf_free( wsharpen );
fftwf_free( wdehalo );
free( mean );
free( pwin );
fftwf_free( pattern2d );
fftwf_free( pattern3d );
fftwf_free( outrez );
if( bt == 0 ) /* Kalman */
{
fftwf_free( outLast );
fftwf_free( covar );
fftwf_free( covarProcess );
}
free( coverbuf );
free( cachewhat );
for( int i = 0; i < cachesize; i++ )
{
fftwf_free( cachefft[i] );
}
fftwf_free( cachefft );
fftwf_free( gridsample ); /* fixed memory leakage in v1.8.5 */
free( messagebuf ); /* v1.8.5 */
}
//-----------------------------------------------------------------------
//
static void PlanarPlaneToCovebuf( const uint8_t *srcp, int src_width, int src_height, int src_pitch, uint8_t *coverbuf, int coverwidth, int coverheight, int coverpitch, int mirw, int mirh, bool interlaced )
{
int h, w;
int width2 = src_width + src_width + mirw + mirw - 2;
uint8_t *coverbuf1 = coverbuf + coverpitch * mirh;
if( !interlaced ) /* progressive */
{
for( h = mirh; h < src_height + mirh; h++ )
{
BitBlt( coverbuf1 + mirw, coverpitch, srcp, src_pitch, src_width, 1 ); /* copy line */
for( w = 0; w < mirw; w++ )
{
coverbuf1[w] = coverbuf1[mirw + mirw - w]; /* mirror left border */
}
for( w = src_width + mirw; w < coverwidth; w++ )
{
coverbuf1[w] = coverbuf1[width2 - w]; /* mirror right border */
}
coverbuf1 += coverpitch;
srcp += src_pitch;
}
}
else /* interlaced */
{
for( h = mirh; h < src_height / 2 + mirh; h++ ) /* first field */
{
BitBlt( coverbuf1 + mirw, coverpitch, srcp, src_pitch, src_width, 1 ); /* copy line */
for( w = 0; w < mirw; w++ )
{
coverbuf1[w] = coverbuf1[mirw + mirw - w]; /* mirror left border */
}
for( 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( h = src_height / 2 + mirh; h < src_height + mirh; h++ ) /* flip second field */
{
BitBlt( coverbuf1 + mirw, coverpitch, srcp, src_pitch, src_width, 1 ); /* copy line */
for( w = 0; w < mirw; w++ )
{
coverbuf1[w] = coverbuf1[mirw + mirw - w]; /* mirror left border */
}
for( w = src_width + mirw; w < coverwidth; w++ )
{
coverbuf1[w] = coverbuf1[width2 - w]; /* mirror right border */
}
coverbuf1 += coverpitch;
srcp -= src_pitch * 2;
}
}
uint8_t *pmirror = coverbuf1 - coverpitch * 2; /* pointer to vertical mirror */
for( h = src_height + mirh; h < coverheight; h++ )
{
BitBlt( coverbuf1, coverpitch, pmirror, coverpitch, coverwidth, 1 ); /* mirror bottom line by line */
coverbuf1 += coverpitch;
pmirror -= coverpitch;
}
coverbuf1 = coverbuf;
pmirror = coverbuf1 + coverpitch * mirh * 2; /* pointer to vertical mirror */
for( h = 0; h < mirh; h++ )
{
BitBlt( coverbuf1, coverpitch, pmirror, coverpitch, coverwidth, 1 ); /* mirror bottom line by line */
coverbuf1 += coverpitch;
pmirror -= coverpitch;
}
}
//-----------------------------------------------------------------------
//
static void CoverbufToPlanarPlane( const uint8_t *coverbuf, int coverwidth, int coverheight, int coverpitch, uint8_t *dstp, int dst_width, int dst_height, int dst_pitch, int mirw, int mirh, bool interlaced )
{
int h;
const uint8_t *coverbuf1 = coverbuf + coverpitch * mirh + mirw;
if( !interlaced ) /* progressive */
{
for( h = 0; h < dst_height; h++ )
{
BitBlt( dstp, dst_pitch, coverbuf1, coverpitch, dst_width, 1 ); /* copy pure frame size only */
dstp += dst_pitch;
coverbuf1 += coverpitch;
}
}
else /* interlaced */
{
for( h = 0; h < dst_height; h += 2 )
{
BitBlt( dstp, dst_pitch, coverbuf1, coverpitch, dst_width, 1 ); /* copy pure frame size only */
dstp += dst_pitch * 2;
coverbuf1 += coverpitch;
}
/* second field is flipped */
dstp -= dst_pitch;
for( h = 0; h < dst_height; h += 2 )
{
BitBlt( dstp, dst_pitch, coverbuf1, coverpitch, dst_width, 1 ); /* copy pure frame size only */
dstp -= dst_pitch * 2;
coverbuf1 += coverpitch;
}
}
}
//-----------------------------------------------------------------------
//
static void FramePlaneToCoverbuf
(
int plane,
const VSFrameRef *src,
uint8_t *coverbuf,
int coverwidth,
int coverheight,
int coverpitch,
int mirw,
int mirh,
bool interlaced,
const VSAPI *vsapi
)
{
const uint8_t *srcp = vsapi->getReadPtr ( src, plane );
int src_height = vsapi->getFrameHeight( src, plane );
int src_width = vsapi->getFrameWidth ( src, plane );
int src_pitch = vsapi->getStride ( src, plane );
PlanarPlaneToCovebuf( srcp, src_width, src_height, src_pitch, coverbuf, coverwidth, coverheight, coverpitch, mirw, mirh, interlaced );
}
//-----------------------------------------------------------------------
//
static void CoverbufToFramePlane
(
int plane,
const uint8_t *coverbuf,
int coverwidth,
int coverheight,
int coverpitch,
VSFrameRef *dst,
int mirw,
int mirh,
bool interlaced,
const VSAPI *vsapi
)
{
uint8_t *dstp = vsapi->getWritePtr ( dst, plane );
int dst_height = vsapi->getFrameHeight( dst, plane );
int dst_width = vsapi->getFrameWidth ( dst, plane );
int dst_pitch = vsapi->getStride ( dst, plane );
CoverbufToPlanarPlane( coverbuf, coverwidth, coverheight, coverpitch, dstp, dst_width, dst_height, dst_pitch, mirw, mirh, interlaced );
}
//-----------------------------------------------------------------------
/* put source bytes to float array of overlapped blocks
* use analysis windows */
void FFT3DFilter::InitOverlapPlane( float *inp0, const uint8_t *srcp0, int src_pitch, int planeBase )
{
int w, h;
int ihx, ihy;
const uint8_t *srcp = srcp0;// + (hrest/2)*src_pitch + wrest/2; /* centered */
float ftmp;
int xoffset = bh * bw - (bw - ow); /* skip frames */
int yoffset = bw * nox * bh - bw * (bh - oh); /* vertical offset of same block (overlap) */
float *inp = inp0;
ihy = 0; /* first top (big non-overlapped) part */
{
for( h = 0; h < oh; h++ )
{
inp = inp0 + h * bw;
for( 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( 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( ihx =1; ihx < nox; ihx += 1 ) /* middle horizontal blocks */
{
for( w = 0; w < ow; w++ ) /* first part (overlapped) row of block */
{
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( 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( 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( h = oh; h < bh - oh; h++ )
{
inp = inp0 + h * bw;
for( 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( 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( ihx = 1; ihx < nox; ihx += 1 ) /* middle horizontal blocks */
{
for( w = 0; w < ow; w++ ) /* first part (overlapped) row of block */
{
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( 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( 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( h = 0; h < oh; h++ ) /* top overlapped part */
{
inp = inp0 + (ihy - 1) * (yoffset + (bh - oh) * bw) + (bh - oh) * bw + h * bw;
for( w = 0; w < ow; w++ ) /* first half line of first block */
{
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( w = ow; w < bw - ow; w++ ) /* first half line of first block */
{
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( ihx = 1; ihx < nox; ihx++ ) /* middle blocks */
{
for( w = 0; w < ow; w++ ) /* half overlapped line of block */
{
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( w = 0; w < bw - ow - ow; w++ ) /* half non-overlapped line of block */
{
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( w = 0; w < ow; w++ ) /* last half line of last block */
{
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( h = 0; h < bh - oh - oh; h++ )
{
inp = inp0 + (ihy - 1) * (yoffset + (bh - oh) * bw) + (bh) * bw + h * bw + yoffset;
for( w = 0; w < ow; w++ ) /* first half line of first block */
{
ftmp = float(wanxl[w] * (srcp[w] - planeBase));
inp[w] = ftmp; /* Copy each byte from source to float array */
}
for( w = ow; w < bw - ow; w++ ) /* first half line of first block */
{
ftmp = float((srcp[w] - planeBase));
inp[w] = ftmp; /* Copy each byte from source to float array */
}
inp += bw - ow;
srcp += bw - ow;
for( ihx = 1; ihx < nox; ihx++ ) /* middle blocks */
{
for( w = 0; w < ow; w++ ) /* half overlapped line of block */
{
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( w = 0; w < bw - ow - ow; w++ ) /* half non-overlapped line of block */
{
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( w = 0; w < ow; w++ ) /* last half line of last block */
{
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( h = 0; h < oh; h++ )
{
inp = inp0 + (ihy - 1) * (yoffset + (bh - oh) * bw) + (bh - oh) * bw + h * bw;
for( w = 0; w < ow; w++ ) /* first half line of first block */
{
ftmp = float(wanxl[w] * wanyr[h] * (srcp[w] - planeBase));
inp[w] = ftmp; /* Copy each byte from source to float array */
}
for( w = ow; w < bw - ow; w++ ) /* first half line of first block */
{
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( ihx = 1; ihx < nox; ihx++ ) /* middle blocks */
{
for( 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];