-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMDegrainN.cpp
More file actions
1994 lines (1775 loc) · 62.9 KB
/
Copy pathMDegrainN.cpp
File metadata and controls
1994 lines (1775 loc) · 62.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
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
#include "ClipFnc.h"
#include "CopyCode.h"
#include "def.h"
#include "MDegrainN.h"
#include "MVDegrain3.h"
#include "MVFrame.h"
#include "MVPlane.h"
#include "MVFilter.h"
#include "profile.h"
#include "SuperParams64Bits.h"
#include <emmintrin.h>
#include <mmintrin.h>
#include <cassert>
#include <cmath>
#include <map>
#include <tuple>
#include <stdint.h>
#include "commonfunctions.h"
// out16_type:
// 0: native 8 or 16
// 1: 8bit in, lsb
// 2: 8bit in, native16 out
template <typename pixel_t, int blockWidth, int blockHeight, int out16_type>
void DegrainN_C(
BYTE* pDst, BYTE* pDstLsb, int nDstPitch,
const BYTE* pSrc, int nSrcPitch,
const BYTE* pRef[], int Pitch[],
int Wall[], int trad
)
{
// for less template, see solution in Degrain1to6_C
constexpr bool lsb_flag = (out16_type == 1);
constexpr bool out16 = (out16_type == 2);
if constexpr (lsb_flag || out16)
{
// 8 bit base only
for (int h = 0; h < blockHeight; ++h)
{
for (int x = 0; x < blockWidth; ++x)
{
int val = pSrc[x] * Wall[0];
for (int k = 0; k < trad; ++k)
{
val += pRef[k * 2][x] * Wall[k * 2 + 1]
+ pRef[k * 2 + 1][x] * Wall[k * 2 + 2];
}
if constexpr (lsb_flag) {
pDst[x] = (uint8_t)(val >> 8);
pDstLsb[x] = (uint8_t)(val & 255);
}
else { // out16
reinterpret_cast<uint16_t*>(pDst)[x] = (uint16_t)val;
}
}
pDst += nDstPitch;
if constexpr (lsb_flag)
pDstLsb += nDstPitch;
pSrc += nSrcPitch;
for (int k = 0; k < trad; ++k)
{
pRef[k * 2] += Pitch[k * 2];
pRef[k * 2 + 1] += Pitch[k * 2 + 1];
}
}
}
else
{
typedef typename std::conditional < sizeof(pixel_t) <= 2, int, float>::type target_t;
constexpr target_t rounder = (sizeof(pixel_t) <= 2) ? 128 : 0;
constexpr float scaleback = 1.0f / (1 << DEGRAIN_WEIGHT_BITS);
// Wall: 8 bit. rounding: 128
for (int h = 0; h < blockHeight; ++h)
{
for (int x = 0; x < blockWidth; ++x)
{
target_t val = reinterpret_cast<const pixel_t*>(pSrc)[x] * (target_t)Wall[0] + rounder;
for (int k = 0; k < trad; ++k)
{
val += reinterpret_cast<const pixel_t*>(pRef[k * 2])[x] * (target_t)Wall[k * 2 + 1]
+ reinterpret_cast<const pixel_t*>(pRef[k * 2 + 1])[x] * (target_t)Wall[k * 2 + 2];
}
if constexpr (sizeof(pixel_t) <= 2)
reinterpret_cast<pixel_t*>(pDst)[x] = (pixel_t)(val >> 8); // 8-16bit
else
reinterpret_cast<pixel_t*>(pDst)[x] = val * scaleback; // 32bit float
}
pDst += nDstPitch;
pSrc += nSrcPitch;
for (int k = 0; k < trad; ++k)
{
pRef[k * 2] += Pitch[k * 2];
pRef[k * 2 + 1] += Pitch[k * 2 + 1];
}
}
}
}
// Debug note: DegrainN filter is calling Degrain1-6 instead if ThSAD(C) == ThSAD(C)2.
// To reach DegrainN_ functions, set the above parameters to different values
// out16_type:
// 0: native 8 or 16
// 1: 8bit in, lsb
// 2: 8bit in, native16 out
template <int blockWidth, int blockHeight, int out16_type>
void DegrainN_sse2(
BYTE *pDst, BYTE *pDstLsb, int nDstPitch,
const BYTE *pSrc, int nSrcPitch,
const BYTE *pRef[], int Pitch[],
int Wall[], int trad
)
{
assert(blockWidth % 4 == 0);
// only mod4 supported
constexpr bool lsb_flag = (out16_type == 1);
constexpr bool out16 = (out16_type == 2);
const __m128i z = _mm_setzero_si128();
constexpr bool is_mod8 = blockWidth % 8 == 0;
constexpr int pixels_at_a_time = is_mod8 ? 8 : 4; // 4 for 4 and 12; 8 for all others 8, 16, 24, 32...
if constexpr(lsb_flag || out16)
{
// no rounding
const __m128i m = _mm_set1_epi16(255);
for (int h = 0; h < blockHeight; ++h)
{
for (int x = 0; x < blockWidth; x += pixels_at_a_time)
{
__m128i src;
if (is_mod8) // load 8 pixels
src = _mm_loadl_epi64((__m128i*) (pSrc + x));
else // load 4 pixels
src = _mm_cvtsi32_si128(*(uint32_t*)(pSrc + x));
__m128i val = _mm_mullo_epi16(_mm_unpacklo_epi8(src, z), _mm_set1_epi16(Wall[0]));
for (int k = 0; k < trad; ++k)
{
__m128i src1, src2;
if constexpr(is_mod8) // load 8-8 pixels
{
src1 = _mm_loadl_epi64((__m128i*) (pRef[k * 2] + x));
src2 = _mm_loadl_epi64((__m128i*) (pRef[k * 2 + 1] + x));
}
else { // 4-4 pixels
src1 = _mm_cvtsi32_si128(*(uint32_t*)(pRef[k * 2] + x));
src2 = _mm_cvtsi32_si128(*(uint32_t*)(pRef[k * 2 + 1] + x));
}
const __m128i s1 = _mm_mullo_epi16(_mm_unpacklo_epi8(src1, z), _mm_set1_epi16(Wall[k * 2 + 1]));
const __m128i s2 = _mm_mullo_epi16(_mm_unpacklo_epi8(src2, z), _mm_set1_epi16(Wall[k * 2 + 2]));
val = _mm_add_epi16(val, s1);
val = _mm_add_epi16(val, s2);
}
if constexpr(is_mod8) {
if constexpr(lsb_flag) {
_mm_storel_epi64((__m128i*)(pDst + x), _mm_packus_epi16(_mm_srli_epi16(val, 8), z));
_mm_storel_epi64((__m128i*)(pDstLsb + x), _mm_packus_epi16(_mm_and_si128(val, m), z));
}
else {
_mm_storeu_si128((__m128i*)(pDst + x * 2), val);
}
}
else {
if constexpr(lsb_flag) {
*(uint32_t*)(pDst + x) = _mm_cvtsi128_si32(_mm_packus_epi16(_mm_srli_epi16(val, 8), z));
*(uint32_t*)(pDstLsb + x) = _mm_cvtsi128_si32(_mm_packus_epi16(_mm_and_si128(val, m), z));
}
else {
_mm_storel_epi64((__m128i*)(pDst + x * 2), val);
}
}
}
pDst += nDstPitch;
if constexpr(lsb_flag)
pDstLsb += nDstPitch;
pSrc += nSrcPitch;
for (int k = 0; k < trad; ++k)
{
pRef[k * 2] += Pitch[k * 2];
pRef[k * 2 + 1] += Pitch[k * 2 + 1];
}
}
}
else
{
// base 8 bit -> 8 bit
const __m128i o = _mm_set1_epi16(128); // rounding
for (int h = 0; h < blockHeight; ++h)
{
for (int x = 0; x < blockWidth; x += pixels_at_a_time)
{
__m128i src;
if constexpr(is_mod8) // load 8 pixels
src = _mm_loadl_epi64((__m128i*) (pSrc + x));
else // load 4 pixels
src = _mm_cvtsi32_si128(*(uint32_t *)(pSrc + x));
__m128i val = _mm_add_epi16(_mm_mullo_epi16(_mm_unpacklo_epi8(src, z), _mm_set1_epi16(Wall[0])), o);
for (int k = 0; k < trad; ++k)
{
__m128i src1, src2;
if constexpr(is_mod8) // load 8-8 pixels
{
src1 = _mm_loadl_epi64((__m128i*) (pRef[k * 2] + x));
src2 = _mm_loadl_epi64((__m128i*) (pRef[k * 2 + 1] + x));
}
else { // 4-4 pixels
src1 = _mm_cvtsi32_si128(*(uint32_t*)(pRef[k * 2] + x));
src2 = _mm_cvtsi32_si128(*(uint32_t*)(pRef[k * 2 + 1] + x));
}
const __m128i s1 = _mm_mullo_epi16(_mm_unpacklo_epi8(src1, z), _mm_set1_epi16(Wall[k * 2 + 1]));
const __m128i s2 = _mm_mullo_epi16(_mm_unpacklo_epi8(src2, z), _mm_set1_epi16(Wall[k * 2 + 2]));
val = _mm_add_epi16(val, s1);
val = _mm_add_epi16(val, s2);
}
auto res = _mm_packus_epi16(_mm_srli_epi16(val, 8), z);
if constexpr(is_mod8) {
_mm_storel_epi64((__m128i*)(pDst + x), res);
}
else {
*(uint32_t*)(pDst + x) = _mm_cvtsi128_si32(res);
}
}
pDst += nDstPitch;
pSrc += nSrcPitch;
for (int k = 0; k < trad; ++k)
{
pRef[k * 2] += Pitch[k * 2];
pRef[k * 2 + 1] += Pitch[k * 2 + 1];
}
}
}
}
template<int blockWidth, int blockHeight, bool lessThan16bits>
void DegrainN_16_sse41(
BYTE* pDst, BYTE* pDstLsb, int nDstPitch,
const BYTE* pSrc, int nSrcPitch,
const BYTE* pRef[], int Pitch[],
int Wall[], int trad
)
{
assert(blockWidth % 4 == 0);
// only mod4 supported
// able to do madd for real 16 bit uint16_t data
const auto signed16_shifter = _mm_set1_epi16(-32768);
const auto signed16_shifter_si32 = _mm_set1_epi32(32768 << DEGRAIN_WEIGHT_BITS);
const __m128i z = _mm_setzero_si128();
constexpr int SHIFTBACK = DEGRAIN_WEIGHT_BITS;
constexpr int rounder_i = (1 << SHIFTBACK) / 2;
// note: DEGRAIN_WEIGHT_BITS is fixed 8 bits, so no rounding occurs on 8 bit in 16 bit out
__m128i rounder = _mm_set1_epi32(rounder_i); // rounding: 128 (mul by 8 bit wref scale back)
for (int h = 0; h < blockHeight; ++h)
{
for (int x = 0; x < blockWidth; x += 8 / sizeof(uint16_t)) // up to 4 pixels per cycle
{
// load 4 pixels
auto src = _mm_loadl_epi64((__m128i*)(pSrc + x * sizeof(uint16_t)));
// weights array structure: center, forward1, backward1, forward2, backward2, etc
// Wall[0] Wall[1] Wall[2] Wall[3] Wall[4] ...
// inputs structure: pSrc pRef[0] pRef[1] pRef[2] pRef[3] ...
__m128i res;
// make signed when unsigned 16 bit mode
if constexpr (!lessThan16bits)
src = _mm_add_epi16(src, signed16_shifter);
// Interleave Src 0 Src 0 ...
src = _mm_cvtepu16_epi32(src); // sse4 unpacklo_epi16 w/ zero
// interleave 0 and center weight
auto ws = _mm_set1_epi32((0 << 16) + Wall[0]);
// pSrc[x] * WSrc + 0 * 0
res = _mm_madd_epi16(src, ws);
// pRefF[n][x] * WRefF[n] + pRefB[n][x] * WRefB[n]
for (int k = 0; k < trad; ++k)
{
// Interleave SrcF SrcB
src = _mm_unpacklo_epi16(
_mm_loadl_epi64((__m128i*)(pRef[k * 2] + x * sizeof(uint16_t))), // from forward
_mm_loadl_epi64((__m128i*)(pRef[k * 2 + 1] + x * sizeof(uint16_t)))); // from backward
if constexpr (!lessThan16bits)
src = _mm_add_epi16(src, signed16_shifter);
// Interleave Forward and Backward 16 bit weights for madd
// backward << 16 | forward in a 32 bit
auto weightBF = _mm_set1_epi32((Wall[k * 2 + 2] << 16) + Wall[k * 2 + 1]);
res = _mm_add_epi32(res, _mm_madd_epi16(src, weightBF));
}
res = _mm_add_epi32(res, rounder); // round
res = _mm_packs_epi32(_mm_srai_epi32(res, SHIFTBACK), z);
// make unsigned when unsigned 16 bit mode
if constexpr (!lessThan16bits)
res = _mm_add_epi16(res, signed16_shifter);
// we are supporting only mod4
// 4, 8, 12, ...
_mm_storel_epi64((__m128i*)(pDst + x * sizeof(uint16_t)), res);
#if 0
// sample from MDegrainX, not only mod4
if constexpr (blockWidth == 6) {
// special, 4+2
if (x == 0)
_mm_storel_epi64((__m128i*)(pDst + x * sizeof(uint16_t)), res);
else
*(uint32_t*)(pDst + x * sizeof(uint16_t)) = _mm_cvtsi128_si32(res);
}
else if constexpr (blockWidth >= 8 / sizeof(uint16_t)) { // block 4 is already 8 bytes
// 4, 8, 12, ...
_mm_storel_epi64((__m128i*)(pDst + x * sizeof(uint16_t)), res);
}
else if constexpr (blockWidth == 3) { // blockwidth 3 is 6 bytes
// x == 0 always
*(uint32_t*)(pDst) = _mm_cvtsi128_si32(res); // 1-4 bytes
uint32_t res32 = _mm_cvtsi128_si32(_mm_srli_si128(res, 4)); // 5-8 byte
*(uint16_t*)(pDst + sizeof(uint32_t)) = (uint16_t)res32; // 2 bytes needed
}
else { // blockwidth 2 is 4 bytes
*(uint32_t*)(pDst + x * sizeof(uint16_t)) = _mm_cvtsi128_si32(res);
}
#endif
}
pDst += nDstPitch;
pSrc += nSrcPitch;
for (int k = 0; k < trad; ++k)
{
pRef[k * 2] += Pitch[k * 2];
pRef[k * 2 + 1] += Pitch[k * 2 + 1];
}
}
}
MDegrainN::DenoiseNFunction* MDegrainN::get_denoiseN_function(int BlockX, int BlockY, int _bits_per_pixel, bool _lsb_flag, bool _out16_flag, arch_t arch)
{
//---------- DENOISE/DEGRAIN
const int DEGRAIN_TYPE_8BIT = 1;
const int DEGRAIN_TYPE_8BIT_STACKED = 2;
const int DEGRAIN_TYPE_8BIT_OUT16 = 4;
const int DEGRAIN_TYPE_10to14BIT = 8;
const int DEGRAIN_TYPE_16BIT = 16;
const int DEGRAIN_TYPE_32BIT = 32;
// BlkSizeX, BlkSizeY, degrain_type, arch_t
std::map<std::tuple<int, int, int, arch_t>, DenoiseNFunction*> func_degrain;
using std::make_tuple;
int type_to_search;
if (_bits_per_pixel == 8) {
if (_out16_flag)
type_to_search = DEGRAIN_TYPE_8BIT_OUT16;
else if (_lsb_flag)
type_to_search = DEGRAIN_TYPE_8BIT_STACKED;
else
type_to_search = DEGRAIN_TYPE_8BIT;
}
else if (_bits_per_pixel <= 14)
type_to_search = DEGRAIN_TYPE_10to14BIT;
else if (_bits_per_pixel == 16)
type_to_search = DEGRAIN_TYPE_16BIT;
else if (_bits_per_pixel == 32)
type_to_search = DEGRAIN_TYPE_32BIT;
else
return nullptr;
// 8bit C, 8bit lsb C, 8bit out16 C, 10-16 bit C, float C (same for all, no blocksize templates)
#define MAKE_FN(x, y) \
func_degrain[make_tuple(x, y, DEGRAIN_TYPE_8BIT, NO_SIMD)] = DegrainN_C<uint8_t, x, y, 0>; \
func_degrain[make_tuple(x, y, DEGRAIN_TYPE_8BIT_STACKED, NO_SIMD)] = DegrainN_C<uint8_t, x, y, 1>; \
func_degrain[make_tuple(x, y, DEGRAIN_TYPE_8BIT_OUT16, NO_SIMD)] = DegrainN_C<uint8_t, x, y, 2>; \
func_degrain[make_tuple(x, y, DEGRAIN_TYPE_10to14BIT, NO_SIMD)] = DegrainN_C<uint16_t, x, y, 0>; \
func_degrain[make_tuple(x, y, DEGRAIN_TYPE_16BIT, NO_SIMD)] = DegrainN_C<uint16_t, x, y, 0>; \
func_degrain[make_tuple(x, y, DEGRAIN_TYPE_32BIT, NO_SIMD)] = DegrainN_C<float, x, y, 0>;
MAKE_FN(64, 64)
MAKE_FN(64, 48)
MAKE_FN(64, 32)
MAKE_FN(64, 16)
MAKE_FN(48, 64)
MAKE_FN(48, 48)
MAKE_FN(48, 24)
MAKE_FN(48, 12)
MAKE_FN(32, 64)
MAKE_FN(32, 32)
MAKE_FN(32, 24)
MAKE_FN(32, 16)
MAKE_FN(32, 8)
MAKE_FN(24, 48)
MAKE_FN(24, 32)
MAKE_FN(24, 24)
MAKE_FN(24, 12)
MAKE_FN(24, 6)
MAKE_FN(16, 64)
MAKE_FN(16, 32)
MAKE_FN(16, 16)
MAKE_FN(16, 12)
MAKE_FN(16, 8)
MAKE_FN(16, 4)
MAKE_FN(16, 2)
MAKE_FN(16, 1)
MAKE_FN(12, 48)
MAKE_FN(12, 24)
MAKE_FN(12, 16)
MAKE_FN(12, 12)
MAKE_FN(12, 6)
MAKE_FN(12, 3)
MAKE_FN(8, 32)
MAKE_FN(8, 16)
MAKE_FN(8, 8)
MAKE_FN(8, 4)
MAKE_FN(8, 2)
MAKE_FN(8, 1)
MAKE_FN(6, 24)
MAKE_FN(6, 12)
MAKE_FN(6, 6)
MAKE_FN(6, 3)
MAKE_FN(4, 8)
MAKE_FN(4, 4)
MAKE_FN(4, 2)
MAKE_FN(4, 1)
MAKE_FN(3, 6)
MAKE_FN(3, 3)
MAKE_FN(2, 4)
MAKE_FN(2, 2)
MAKE_FN(2, 1)
#undef MAKE_FN
#undef MAKE_FN_LEVEL
// and the SSE2 versions for 8 bit
#define MAKE_FN(x, y) \
func_degrain[make_tuple(x, y, DEGRAIN_TYPE_8BIT, USE_SSE2)] = DegrainN_sse2<x, y, 0>; \
func_degrain[make_tuple(x, y, DEGRAIN_TYPE_8BIT_STACKED, USE_SSE2)] = DegrainN_sse2<x, y, 1>; \
func_degrain[make_tuple(x, y, DEGRAIN_TYPE_8BIT_OUT16, USE_SSE2)] = DegrainN_sse2<x, y, 2>; \
func_degrain[make_tuple(x, y, DEGRAIN_TYPE_10to14BIT, USE_SSE41)] = DegrainN_16_sse41<x, y, true>; \
func_degrain[make_tuple(x, y, DEGRAIN_TYPE_16BIT, USE_SSE41)] = DegrainN_16_sse41<x, y, false>;
MAKE_FN(64, 64)
MAKE_FN(64, 48)
MAKE_FN(64, 32)
MAKE_FN(64, 16)
MAKE_FN(48, 64)
MAKE_FN(48, 48)
MAKE_FN(48, 24)
MAKE_FN(48, 12)
MAKE_FN(32, 64)
MAKE_FN(32, 32)
MAKE_FN(32, 24)
MAKE_FN(32, 16)
MAKE_FN(32, 8)
MAKE_FN(24, 48)
MAKE_FN(24, 32)
MAKE_FN(24, 24)
MAKE_FN(24, 12)
MAKE_FN(24, 6)
MAKE_FN(16, 64)
MAKE_FN(16, 32)
MAKE_FN(16, 16)
MAKE_FN(16, 12)
MAKE_FN(16, 8)
MAKE_FN(16, 4)
MAKE_FN(16, 2)
MAKE_FN(16, 1)
MAKE_FN(12, 48)
MAKE_FN(12, 24)
MAKE_FN(12, 16)
MAKE_FN(12, 12)
MAKE_FN(12, 6)
MAKE_FN(12, 3)
MAKE_FN(8, 32)
MAKE_FN(8, 16)
MAKE_FN(8, 8)
MAKE_FN(8, 4)
MAKE_FN(8, 2)
MAKE_FN(8, 1)
//MAKE_FN(6, 24) // w is mod4 only supported
//MAKE_FN(6, 12)
//MAKE_FN(6, 6)
//MAKE_FN(6, 3)
MAKE_FN(4, 8)
MAKE_FN(4, 4)
MAKE_FN(4, 2)
MAKE_FN(4, 1)
//MAKE_FN(3, 6) // w is mod4 only supported
//MAKE_FN(3, 3)
//MAKE_FN(2, 4) // no 2 byte width, only C
//MAKE_FN(2, 2) // no 2 byte width, only C
//MAKE_FN(2, 1) // no 2 byte width, only C
#undef MAKE_FN
#undef MAKE_FN_LEVEL
DenoiseNFunction* result = nullptr;
arch_t archlist[] = { USE_AVX2, USE_AVX, USE_SSE41, USE_SSE2, NO_SIMD };
int index = 0;
while (result == nullptr) {
arch_t current_arch_try = archlist[index++];
if (current_arch_try > arch) continue;
result = func_degrain[make_tuple(BlockX, BlockY, type_to_search, current_arch_try)];
if (result == nullptr && current_arch_try == NO_SIMD)
break;
}
return result;
}
MDegrainN::MDegrainN(
PClip child, PClip super, PClip mvmulti, int trad,
sad_t thsad, sad_t thsadc, int yuvplanes, float nlimit, float nlimitc,
sad_t nscd1, int nscd2, bool isse_flag, bool planar_flag, bool lsb_flag,
sad_t thsad2, sad_t thsadc2, bool mt_flag, bool out16_flag, IScriptEnvironment* env_ptr
)
: GenericVideoFilter(child)
, MVFilter(mvmulti, "MDegrainN", env_ptr, 1, 0)
, _mv_clip_arr()
, _trad(trad)
, _yuvplanes(yuvplanes)
, _nlimit(nlimit)
, _nlimitc(nlimitc)
, _super(super)
, _planar_flag(planar_flag)
, _lsb_flag(lsb_flag)
, _mt_flag(mt_flag)
, _out16_flag(out16_flag)
, _height_lsb_or_out16_mul((lsb_flag || out16_flag) ? 2 : 1)
, _nsupermodeyuv(-1)
, _dst_planes(nullptr)
, _src_planes(nullptr)
, _overwins()
, _overwins_uv()
, _oversluma_ptr(0)
, _overschroma_ptr(0)
, _oversluma16_ptr(0)
, _overschroma16_ptr(0)
, _oversluma32_ptr(0)
, _overschroma32_ptr(0)
, _oversluma_lsb_ptr(0)
, _overschroma_lsb_ptr(0)
, _degrainluma_ptr(0)
, _degrainchroma_ptr(0)
, _dst_short()
, _dst_short_pitch()
, _dst_int()
, _dst_int_pitch()
//, _usable_flag_arr ()
//, _planes_ptr ()
//, _dst_ptr_arr ()
//, _src_ptr_arr ()
//, _dst_pitch_arr ()
//, _src_pitch_arr ()
//, _lsb_offset_arr ()
, _covered_width(0)
, _covered_height(0)
, _boundary_cnt_arr()
{
has_at_least_v8 = true;
try { env_ptr->CheckVersion(8); }
catch (const AvisynthError&) { has_at_least_v8 = false; }
if (trad > MAX_TEMP_RAD)
{
env_ptr->ThrowError(
"MDegrainN: temporal radius too large (max %d)",
MAX_TEMP_RAD
);
}
else if (trad < 1)
{
env_ptr->ThrowError("MDegrainN: temporal radius must be at least 1.");
}
_mv_clip_arr.resize(_trad * 2);
for (int k = 0; k < _trad * 2; ++k)
{
_mv_clip_arr[k]._clip_sptr = SharedPtr <MVClip>(
new MVClip(mvmulti, nscd1, nscd2, env_ptr, _trad * 2, k)
);
static const char *name_0[2] = { "mvbw", "mvfw" };
char txt_0[127 + 1];
sprintf(txt_0, "%s%d", name_0[k & 1], 1 + k / 2);
CheckSimilarity(*(_mv_clip_arr[k]._clip_sptr), txt_0, env_ptr);
}
const sad_t mv_thscd1 = _mv_clip_arr[0]._clip_sptr->GetThSCD1();
thsad = (uint64_t)thsad * mv_thscd1 / nscd1; // normalize to block SAD
thsadc = (uint64_t)thsadc * mv_thscd1 / nscd1; // chroma
thsad2 = (uint64_t)thsad2 * mv_thscd1 / nscd1;
thsadc2 = (uint64_t)thsadc2 * mv_thscd1 / nscd1;
const ::VideoInfo &vi_super = _super->GetVideoInfo();
if (!vi.IsSameColorspace(_super->GetVideoInfo()))
env_ptr->ThrowError("MDegrainN: source and super clip video format is different!");
// v2.7.39- make subsampling independent from motion vector's origin:
// because xRatioUV and yRatioUV: in MVFilter, property of motion vectors
xRatioUV_super = 1;
yRatioUV_super = 1;
if (!vi.IsY() && !vi.IsRGB()) {
xRatioUV_super = vi.IsYUY2() ? 2 : (1 << vi.GetPlaneWidthSubsampling(PLANAR_U));
yRatioUV_super = vi.IsYUY2() ? 1 : (1 << vi.GetPlaneHeightSubsampling(PLANAR_U));
}
nLogxRatioUV_super = ilog2(xRatioUV_super);
nLogyRatioUV_super = ilog2(yRatioUV_super);
pixelsize_super = vi_super.ComponentSize(); // of MVFilter
bits_per_pixel_super = vi_super.BitsPerComponent();
_cpuFlags = isse_flag ? env_ptr->GetCPUFlags() : 0;
// get parameters of prepared super clip - v2.0
SuperParams64Bits params;
memcpy(¶ms, &vi_super.num_audio_samples, 8);
const int nHeightS = params.nHeight;
const int nSuperHPad = params.nHPad;
const int nSuperVPad = params.nVPad;
const int nSuperPel = params.nPel;
const int nSuperLevels = params.nLevels;
_nsupermodeyuv = params.nModeYUV;
// no need for SAD scaling, it is coming from the mv clip analysis. nSCD1 is already scaled in MVClip constructor
/* must be good from 2.7.13.22
thsad = sad_t(thsad / 255.0 * ((1 << bits_per_pixel) - 1));
thsadc = sad_t(thsadc / 255.0 * ((1 << bits_per_pixel) - 1));
thsad2 = sad_t(thsad2 / 255.0 * ((1 << bits_per_pixel) - 1));
thsadc2 = sad_t(thsadc2 / 255.0 * ((1 << bits_per_pixel) - 1));
*/
for (int k = 0; k < _trad * 2; ++k)
{
MvClipInfo &c_info = _mv_clip_arr[k];
c_info._gof_sptr = SharedPtr <MVGroupOfFrames>(new MVGroupOfFrames(
nSuperLevels,
nWidth,
nHeight,
nSuperPel,
nSuperHPad,
nSuperVPad,
_nsupermodeyuv,
_cpuFlags,
xRatioUV_super,
yRatioUV_super,
pixelsize_super,
bits_per_pixel_super,
mt_flag
));
// Computes the SAD thresholds for this source frame, a cosine-shaped
// smooth transition between thsad(c) and thsad(c)2.
const int d = k / 2 + 1;
c_info._thsad = ClipFnc::interpolate_thsad(thsad, thsad2, d, _trad);
c_info._thsadc = ClipFnc::interpolate_thsad(thsadc, thsadc2, d, _trad);
c_info._thsad_sq = double(c_info._thsad) * double(c_info._thsad);
c_info._thsadc_sq = double(c_info._thsadc) * double(c_info._thsadc);
}
const int nSuperWidth = vi_super.width;
pixelsize_super_shift = ilog2(pixelsize_super);
if (nHeight != nHeightS
|| nHeight != vi.height
|| nWidth != nSuperWidth - nSuperHPad * 2
|| nWidth != vi.width
|| nPel != nSuperPel)
{
env_ptr->ThrowError("MDegrainN : wrong source or super frame size");
}
if(lsb_flag && (pixelsize != 1 || pixelsize_super != 1))
env_ptr->ThrowError("MDegrainN : lsb_flag only for 8 bit sources");
if (out16_flag) {
if (pixelsize != 1 || pixelsize_super != 1)
env_ptr->ThrowError("MDegrainN : out16 flag only for 8 bit sources");
if (!vi.IsY8() && !vi.IsYV12() && !vi.IsYV16() && !vi.IsYV24())
env_ptr->ThrowError("MDegrainN : only YV8, YV12, YV16 or YV24 allowed for out16");
}
if (lsb_flag && out16_flag)
env_ptr->ThrowError("MDegrainN : cannot specify both lsb and out16 flag");
// output can be different bit depth from input
pixelsize_output = pixelsize_super;
bits_per_pixel_output = bits_per_pixel_super;
pixelsize_output_shift = pixelsize_super_shift;
if (out16_flag) {
pixelsize_output = sizeof(uint16_t);
bits_per_pixel_output = 16;
pixelsize_output_shift = ilog2(pixelsize_output);
}
if ((pixelType & VideoInfo::CS_YUY2) == VideoInfo::CS_YUY2 && !_planar_flag)
{
_dst_planes = std::unique_ptr <YUY2Planes>(
new YUY2Planes(nWidth, nHeight * _height_lsb_or_out16_mul)
);
_src_planes = std::unique_ptr <YUY2Planes>(
new YUY2Planes(nWidth, nHeight)
);
}
_dst_short_pitch = ((nWidth + 15) / 16) * 16;
_dst_int_pitch = _dst_short_pitch;
if (nOverlapX > 0 || nOverlapY > 0)
{
_overwins = std::unique_ptr <OverlapWindows>(
new OverlapWindows(nBlkSizeX, nBlkSizeY, nOverlapX, nOverlapY)
);
_overwins_uv = std::unique_ptr <OverlapWindows>(new OverlapWindows(
nBlkSizeX >> nLogxRatioUV_super, nBlkSizeY >> nLogyRatioUV_super,
nOverlapX >> nLogxRatioUV_super, nOverlapY >> nLogyRatioUV_super
));
if (_lsb_flag || pixelsize_output > 1)
{
_dst_int.resize(_dst_int_pitch * nHeight);
}
else
{
_dst_short.resize(_dst_short_pitch * nHeight);
}
}
if (nOverlapY > 0)
{
_boundary_cnt_arr.resize(nBlkY);
}
// in overlaps.h
// OverlapsLsbFunction
// OverlapsFunction
// in M(V)DegrainX: DenoiseXFunction
arch_t arch;
if ((_cpuFlags & CPUF_AVX2) != 0)
arch = USE_AVX2;
else if ((_cpuFlags & CPUF_AVX) != 0)
arch = USE_AVX;
else if ((_cpuFlags & CPUF_SSE4_1) != 0)
arch = USE_SSE41;
else if ((_cpuFlags & CPUF_SSE2) != 0)
arch = USE_SSE2;
else
arch = NO_SIMD;
// C only -> NO_SIMD
_oversluma_lsb_ptr = get_overlaps_lsb_function(nBlkSizeX, nBlkSizeY, sizeof(uint8_t), NO_SIMD);
_overschroma_lsb_ptr = get_overlaps_lsb_function(nBlkSizeX / xRatioUV_super, nBlkSizeY / yRatioUV_super, sizeof(uint8_t), NO_SIMD);
_oversluma_ptr = get_overlaps_function(nBlkSizeX, nBlkSizeY, sizeof(uint8_t), false, arch);
_overschroma_ptr = get_overlaps_function(nBlkSizeX / xRatioUV_super, nBlkSizeY / yRatioUV_super, sizeof(uint8_t), false, arch);
_oversluma16_ptr = get_overlaps_function(nBlkSizeX, nBlkSizeY, sizeof(uint16_t), false, arch);
_overschroma16_ptr = get_overlaps_function(nBlkSizeX >> nLogxRatioUV_super, nBlkSizeY >> nLogyRatioUV_super, sizeof(uint16_t), false, arch);
_oversluma32_ptr = get_overlaps_function(nBlkSizeX, nBlkSizeY, sizeof(float), false, arch);
_overschroma32_ptr = get_overlaps_function(nBlkSizeX >> nLogxRatioUV_super, nBlkSizeY >> nLogyRatioUV_super, sizeof(float), false, arch);
_degrainluma_ptr = get_denoiseN_function(nBlkSizeX, nBlkSizeY, bits_per_pixel_super, lsb_flag, out16_flag, arch);
_degrainchroma_ptr = get_denoiseN_function(nBlkSizeX / xRatioUV_super, nBlkSizeY / yRatioUV_super, bits_per_pixel_super, lsb_flag, out16_flag, arch);
if (!_oversluma_lsb_ptr)
env_ptr->ThrowError("MDegrainN : no valid _oversluma_lsb_ptr function for %dx%d, pixelsize=%d, lsb_flag=%d", nBlkSizeX, nBlkSizeY, pixelsize_super, (int)lsb_flag);
if (!_overschroma_lsb_ptr)
env_ptr->ThrowError("MDegrainN : no valid _overschroma_lsb_ptr function for %dx%d, pixelsize=%d, lsb_flag=%d", nBlkSizeX, nBlkSizeY, pixelsize_super, (int)lsb_flag);
if (!_oversluma_ptr)
env_ptr->ThrowError("MDegrainN : no valid _oversluma_ptr function for %dx%d, pixelsize=%d, lsb_flag=%d", nBlkSizeX, nBlkSizeY, pixelsize_super, (int)lsb_flag);
if (!_overschroma_ptr)
env_ptr->ThrowError("MDegrainN : no valid _overschroma_ptr function for %dx%d, pixelsize=%d, lsb_flag=%d", nBlkSizeX, nBlkSizeY, pixelsize_super, (int)lsb_flag);
if (!_degrainluma_ptr)
env_ptr->ThrowError("MDegrainN : no valid _degrainluma_ptr function for %dx%d, pixelsize=%d, lsb_flag=%d", nBlkSizeX, nBlkSizeY, pixelsize_super, (int)lsb_flag);
if (!_degrainchroma_ptr)
env_ptr->ThrowError("MDegrainN : no valid _degrainchroma_ptr function for %dx%d, pixelsize=%d, lsb_flag=%d", nBlkSizeX, nBlkSizeY, pixelsize_super, (int)lsb_flag);
if ((_cpuFlags & CPUF_SSE2) != 0)
{
if(out16_flag)
LimitFunction = LimitChanges_src8_target16_c; // todo SSE2
else if (pixelsize_super == 1)
LimitFunction = LimitChanges_sse2_new<uint8_t, 0>;
else if (pixelsize_super == 2) { // pixelsize_super == 2
if ((_cpuFlags & CPUF_SSE4_1) != 0)
LimitFunction = LimitChanges_sse2_new<uint16_t, 1>;
else
LimitFunction = LimitChanges_sse2_new<uint16_t, 0>;
}
else {
LimitFunction = LimitChanges_float_c; // no SSE2
}
}
else
{
if (out16_flag)
LimitFunction = LimitChanges_src8_target16_c; // todo SSE2
else if (pixelsize_super == 1)
LimitFunction = LimitChanges_c<uint8_t>;
else if (pixelsize_super == 2)
LimitFunction = LimitChanges_c<uint16_t>;
else
LimitFunction = LimitChanges_float_c;
}
//---------- end of functions
// 16 bit output hack
if (_lsb_flag)
{
vi.height <<= 1;
}
if (out16_flag) {
if (vi.IsY8())
vi.pixel_type = VideoInfo::CS_Y16;
else if (vi.IsYV12())
vi.pixel_type = VideoInfo::CS_YUV420P16;
else if (vi.IsYV16())
vi.pixel_type = VideoInfo::CS_YUV422P16;
else if (vi.IsYV24())
vi.pixel_type = VideoInfo::CS_YUV444P16;
}
}
MDegrainN::~MDegrainN()
{
// Nothing
}
static void plane_copy_8_to_16_c(uint8_t *dstp, int dstpitch, const uint8_t *srcp, int srcpitch, int width, int height)
{
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
reinterpret_cast<uint16_t *>(dstp)[x] = srcp[x] << 8;
}
dstp += dstpitch;
srcp += srcpitch;
}
}
::PVideoFrame __stdcall MDegrainN::GetFrame(int n, ::IScriptEnvironment* env_ptr)
{
_covered_width = nBlkX * (nBlkSizeX - nOverlapX) + nOverlapX;
_covered_height = nBlkY * (nBlkSizeY - nOverlapY) + nOverlapY;
const BYTE * pRef[MAX_TEMP_RAD * 2][3];
int nRefPitches[MAX_TEMP_RAD * 2][3];
unsigned char *pDstYUY2;
const unsigned char *pSrcYUY2;
int nDstPitchYUY2;
int nSrcPitchYUY2;
for (int k2 = 0; k2 < _trad * 2; ++k2)
{
// reorder ror regular frames order in v2.0.9.2
const int k = reorder_ref(k2);
// v2.0.9.2 - it seems we do not need in vectors clip anymore when we
// finished copying them to fakeblockdatas
MVClip &mv_clip = *(_mv_clip_arr[k]._clip_sptr);
::PVideoFrame mv = mv_clip.GetFrame(n, env_ptr);
mv_clip.Update(mv, env_ptr);
_usable_flag_arr[k] = mv_clip.IsUsable();
}
PVideoFrame src = child->GetFrame(n, env_ptr);
PVideoFrame dst = has_at_least_v8 ? env_ptr->NewVideoFrameP(vi, &src) : env_ptr->NewVideoFrame(vi); // frame property support
if ((pixelType & VideoInfo::CS_YUY2) == VideoInfo::CS_YUY2)
{
if (!_planar_flag)
{
pDstYUY2 = dst->GetWritePtr();
nDstPitchYUY2 = dst->GetPitch();
_dst_ptr_arr[0] = _dst_planes->GetPtr();
_dst_ptr_arr[1] = _dst_planes->GetPtrU();
_dst_ptr_arr[2] = _dst_planes->GetPtrV();
_dst_pitch_arr[0] = _dst_planes->GetPitch();
_dst_pitch_arr[1] = _dst_planes->GetPitchUV();
_dst_pitch_arr[2] = _dst_planes->GetPitchUV();
pSrcYUY2 = src->GetReadPtr();
nSrcPitchYUY2 = src->GetPitch();
_src_ptr_arr[0] = _src_planes->GetPtr();
_src_ptr_arr[1] = _src_planes->GetPtrU();
_src_ptr_arr[2] = _src_planes->GetPtrV();
_src_pitch_arr[0] = _src_planes->GetPitch();
_src_pitch_arr[1] = _src_planes->GetPitchUV();
_src_pitch_arr[2] = _src_planes->GetPitchUV();
YUY2ToPlanes(
pSrcYUY2, nSrcPitchYUY2, nWidth, nHeight,
_src_ptr_arr[0], _src_pitch_arr[0],
_src_ptr_arr[1], _src_ptr_arr[2], _src_pitch_arr[1],
_cpuFlags
);
}
else
{
_dst_ptr_arr[0] = dst->GetWritePtr();
_dst_ptr_arr[1] = _dst_ptr_arr[0] + nWidth;
_dst_ptr_arr[2] = _dst_ptr_arr[1] + nWidth / 2; //yuy2 xratio
_dst_pitch_arr[0] = dst->GetPitch();
_dst_pitch_arr[1] = _dst_pitch_arr[0];
_dst_pitch_arr[2] = _dst_pitch_arr[0];
_src_ptr_arr[0] = src->GetReadPtr();
_src_ptr_arr[1] = _src_ptr_arr[0] + nWidth;
_src_ptr_arr[2] = _src_ptr_arr[1] + nWidth / 2;
_src_pitch_arr[0] = src->GetPitch();
_src_pitch_arr[1] = _src_pitch_arr[0];
_src_pitch_arr[2] = _src_pitch_arr[0];
}
}
else
{
_dst_ptr_arr[0] = YWPLAN(dst);
_dst_ptr_arr[1] = UWPLAN(dst);
_dst_ptr_arr[2] = VWPLAN(dst);
_dst_pitch_arr[0] = YPITCH(dst);
_dst_pitch_arr[1] = UPITCH(dst);
_dst_pitch_arr[2] = VPITCH(dst);
_src_ptr_arr[0] = YRPLAN(src);
_src_ptr_arr[1] = URPLAN(src);
_src_ptr_arr[2] = VRPLAN(src);
_src_pitch_arr[0] = YPITCH(src);
_src_pitch_arr[1] = UPITCH(src);
_src_pitch_arr[2] = VPITCH(src);
}
_lsb_offset_arr[0] = _dst_pitch_arr[0] * nHeight;
_lsb_offset_arr[1] = _dst_pitch_arr[1] * (nHeight >> nLogyRatioUV_super);
_lsb_offset_arr[2] = _dst_pitch_arr[2] * (nHeight >> nLogyRatioUV_super);
if (_lsb_flag)
{
memset(_dst_ptr_arr[0] + _lsb_offset_arr[0], 0, _lsb_offset_arr[0]);
if (!_planar_flag)
{
memset(_dst_ptr_arr[1] + _lsb_offset_arr[1], 0, _lsb_offset_arr[1]);
memset(_dst_ptr_arr[2] + _lsb_offset_arr[2], 0, _lsb_offset_arr[2]);
}
}
::PVideoFrame ref[MAX_TEMP_RAD * 2];
for (int k2 = 0; k2 < _trad * 2; ++k2)
{
// reorder ror regular frames order in v2.0.9.2
const int k = reorder_ref(k2);
MVClip &mv_clip = *(_mv_clip_arr[k]._clip_sptr);
mv_clip.use_ref_frame(ref[k], _usable_flag_arr[k], _super, n, env_ptr);
}
if ((pixelType & VideoInfo::CS_YUY2) == VideoInfo::CS_YUY2)
{
for (int k2 = 0; k2 < _trad * 2; ++k2)
{
const int k = reorder_ref(k2);
if (_usable_flag_arr[k])
{
pRef[k][0] = ref[k]->GetReadPtr();
pRef[k][1] = pRef[k][0] + ref[k]->GetRowSize() / 2;
pRef[k][2] = pRef[k][1] + ref[k]->GetRowSize() / 4;
nRefPitches[k][0] = ref[k]->GetPitch();
nRefPitches[k][1] = nRefPitches[k][0];
nRefPitches[k][2] = nRefPitches[k][0];
}
}
}
else
{
for (int k2 = 0; k2 < _trad * 2; ++k2)
{
const int k = reorder_ref(k2);
if (_usable_flag_arr[k])
{