-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveGrain.cpp
5244 lines (5035 loc) · 163 KB
/
RemoveGrain.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
#define LOGO "RemoveGrain 0.9\n"
// An Avisynth plugin for removing grain from progressive video
//
// By Rainer Wittmann <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// 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.
//
// To get a copy of the GNU General Public License write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA, or visit
// http://www.gnu.org/copyleft/gpl.html .
//#define MODIFYPLUGIN 1// creat Repair plugin instead of RemoveGrain, 0 = compatible with RemoveGrain
//#define SSE2_TEST // ISSE2 version that can be used side by side with the SSE version
//#define DEBUG_NAME // for debugging
//#define ISSE 2 // P4, Athlon 64, Sempron 3100
//#define ISSE 3 // Prescott P4
//#define CVERSION // for debugging only
#define ALIGNPITCH
#define SMOOTH2
#define DEFAULT_MODE 2
#define DEFAULT_RGLIMIT 0
#define VC_EXTRALEAN
#include "wrap_windows.h"
#include <stdio.h>
#include <stdarg.h>
#include "avxsynth.h"
#include "planar.h"
static IScriptEnvironment *AVSenvironment;
#ifdef SSE2_TEST
#ifndef ISSE
#define ISSE 2
#endif
#ifndef DEBUG_NAME
#define DEBUG_NAME
#endif
#endif
#ifndef ISSE
#define ISSE 1
#endif
#if ISSE > 1
#define CPUFLAGS CPUF_SSE2
#else
#define CPUFLAGS CPUF_INTEGER_SSE
#endif
#ifdef MODIFYPLUGIN
#define MAXMODE 18
#else
#define MAXMODE 18
#endif
#if 1
void debug_printf(const char *format, ...)
{
char buffer[200];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
OutputDebugString(buffer);
}
#endif
#define COMPARE_MASK (~24)
static void CompareVideoInfo(VideoInfo &vi1, const VideoInfo &vi2, const char *progname)
{
if( (vi1.width != vi2.width) || (vi1.height != vi2.height) || ( (vi1.pixel_type & COMPARE_MASK) != (vi2.pixel_type & COMPARE_MASK) ))
{
#if 1
debug_printf("widths = %u, %u, heights = %u, %u, color spaces = %X, %X\n"
, vi1.width, vi2.width, vi1.height, vi2.height, vi1.pixel_type, vi2.pixel_type);
#endif
AVSenvironment->ThrowError("%s: clips must be of equal type", progname);
}
if(vi1.num_frames > vi2.num_frames) vi1.num_frames = vi2.num_frames;
}
#ifdef TESTCOMPARE
unsigned testcompare(const BYTE *dp, int dpitch, const BYTE *pp, int ppitch, int width, int height)
{
int i = height;
--dp; --pp;
unsigned diffsum = 0;
do
{
int j = width;
do
{
int diff = dp[j] - pp[j];
if( diff < 0 ) diff = -diff;
diffsum += diff;
} while( --j );
dp += dpitch;
pp += ppitch;
} while( --i );
return diffsum;
}
#define xpitch 1
void RemoveGrain(BYTE *dp, int dpitch, const BYTE *sp, int spitch, int width, int height, int threshold)
{
int sinc = - (width + 1) * xpitch;
dpitch += sinc;
sinc += spitch;
do
{
dp[0] = sp[0];
dp += xpitch; sp += xpitch;
int i = width;
do
{
unsigned sort1[8];
int leq = 0;
int geq = 0;
unsigned x = sp[0];
if( (sort1[0] = (sp += xpitch)[0]) <= x )
{
if( sort1[0] == x ) ++geq;
++leq;
}
if( (sort1[1] = (sp += spitch)[0]) <= x )
{
if( sort1[1] == x ) ++geq;
++leq;
}
if( (sort1[2] = (sp -= xpitch)[0]) <= x )
{
if( sort1[2] == x ) ++geq;
++leq;
}
if( (sort1[3] = (sp -= xpitch)[0]) <= x )
{
if( sort1[3] >= x ) ++geq;
++leq;
}
if( (sort1[4] = (sp -= spitch)[0]) <= x )
{
if( sort1[4] >= x ) ++geq;
++leq;
}
if( (sort1[5] = (sp -= spitch)[0]) <= x )
{
if( sort1[5] >= x ) ++geq;
++leq;
}
if( (sort1[6] = (sp += xpitch)[0]) <= x )
{
if( sort1[6] >= x ) ++geq;
++leq;
}
if( (sort1[7] = (sp += xpitch)[0]) <= x )
{
if( sort1[7] >= x ) ++geq;
++leq;
}
if( ((geq += 8 - leq) < threshold) || (leq < threshold) )
{ // do a merge sort of sort1[8] as fast as possible
unsigned sort2[8];
if( sort1[1] < sort1[0] )
{
sort2[0] = sort1[1];
sort2[1] = sort1[0];
}
else
{
sort2[0] = sort1[0];
sort2[1] = sort1[1];
}
if( sort1[3] < sort1[2] )
{
sort2[2] = sort1[3];
sort2[3] = sort1[2];
}
else
{
sort2[2] = sort1[2];
sort2[3] = sort1[3];
}
if( sort1[5] < sort1[4] )
{
sort2[4] = sort1[5];
sort2[5] = sort1[4];
}
else
{
sort2[4] = sort1[4];
sort2[5] = sort1[5];
}
if( sort1[7] < sort1[6] )
{
sort2[6] = sort1[7];
sort2[7] = sort1[6];
}
else
{
sort2[6] = sort1[6];
sort2[7] = sort1[7];
}
if( sort2[0] > sort2[2] )
{
sort1[0] = sort2[2];
if( sort2[3] <= sort2[0] )
{
sort1[1] = sort2[3];
sort1[2] = sort2[0];
sort1[3] = sort2[1];
}
else
{
sort1[1] = sort2[0];
if( sort2[1] < sort2[3] )
{
sort1[2] = sort2[1];
sort1[3] = sort2[3];
}
else
{
sort1[2] = sort2[3];
sort1[3] = sort2[1];
}
}
}
else
{
sort1[0] = sort2[0];
if( sort2[1] <= sort2[2] )
{
sort1[1] = sort2[1];
sort1[2] = sort2[2];
sort1[3] = sort2[3];
}
else
{
sort1[1] = sort2[2];
if( sort2[3] < sort2[1] )
{
sort1[2] = sort2[3];
sort1[3] = sort2[1];
}
else
{
sort1[2] = sort2[1];
sort1[3] = sort2[3];
}
}
}
#if 0
if( (sort1[0] > sort1[1]) || (sort1[1] > sort1[2]) || (sort1[2] > sort1[3]) )
debug_printf("merge error: sort = %u, %u, %u, %u\n", sort1[0], sort1[1], sort1[2], sort1[3]);
#endif
if( sort2[4] > sort2[6] )
{
sort1[4] = sort2[6];
if( sort2[7] <= sort2[4] )
{
sort1[5] = sort2[7];
sort1[6] = sort2[4];
sort1[7] = sort2[5];
}
else
{
sort1[5] = sort2[4];
if( sort2[5] < sort2[7] )
{
sort1[6] = sort2[5];
sort1[7] = sort2[7];
}
else
{
sort1[6] = sort2[7];
sort1[7] = sort2[5];
}
}
}
else
{
sort1[4] = sort2[4];
if( sort2[5] <= sort2[6] )
{
sort1[5] = sort2[5];
sort1[6] = sort2[6];
sort1[7] = sort2[7];
}
else
{
sort1[5] = sort2[6];
if( sort2[7] < sort2[5] )
{
sort1[6] = sort2[7];
sort1[7] = sort2[5];
}
else
{
sort1[6] = sort2[5];
sort1[7] = sort2[7];
}
}
}
#if 0
if( (sort1[4] > sort1[5]) || (sort1[5] > sort1[6]) || (sort1[6] > sort1[7]) )
debug_printf("merge error: sort = %u, %u, %u, %u\n", sort1[4], sort1[5], sort1[6], sort1[7]);
#endif
unsigned *s1 = sort1, *s2 = sort1 + 4, *t = sort2;
*t++ = *s1 > *s2 ? *s2++ : *s1++;
*t++ = *s1 > *s2 ? *s2++ : *s1++;
*t++ = *s1 > *s2 ? *s2++ : *s1++;
if( sort1[3] > sort1[7] )
{
do
{
*t++ = *s1 > *s2 ? *s2++ : *s1++;
} while( s2 != sort1 + 8 );
do
{
*t++ = *s1++;
} while( s1 != sort1 + 4 );
}
else
{
do
{
*t++ = *s1 > *s2 ? *s2++ : *s1++;
} while( s1 != sort1 + 4 );
do
{
*t++ = *s2++;
} while( s2 != sort1 + 8 );
}
#if 0
if( (leq > 0) && (sort2[leq - 1] > x) ) debug_printf("leq = %u, x = %u, sort = %u,%u,%u,%u,%u,%u,%u,%u\n", leq, x, sort2[0], sort2[1], sort2[2], sort2[3], sort2[4], sort2[5], sort2[6], sort2[7]);
if( (leq < 8) && (sort2[leq] <= x) ) debug_printf("leq = %u, x = %u, sort = %u,%u,%u,%u,%u,%u,%u,%u\n", leq, x, sort2[0], sort2[1], sort2[2], sort2[3], sort2[4], sort2[5], sort2[6], sort2[7]);
if( (geq > 0) && (sort2[8 - geq] < x) ) debug_printf("geq = %u, x = %u, sort = %u,%u,%u,%u,%u,%u,%u,%u\n", geq, x, sort2[0], sort2[1], sort2[2], sort2[3], sort2[4], sort2[5], sort2[6], sort2[7]);
if( (geq < 8) && (sort2[7 - geq] >= x) ) debug_printf("geq = %u, x = %u, sort = %u,%u,%u,%u,%u,%u,%u,%u\n", geq, x, sort2[0], sort2[1], sort2[2], sort2[3], sort2[4], sort2[5], sort2[6], sort2[7]);
#endif
x = leq < threshold ? sort2[threshold - 1] : sort2[8 - threshold];
}
dp[0] = x;
dp += xpitch;
sp += spitch;
} while( --i );
dp[0] = sp[0];
dp += dpitch; sp += sinc;
} while( --height );
}
#undef xpitch
#endif // TESTCOMPARE
#if ISSE > 1
#define SSE_INCREMENT 16
#define SSE_SHIFT 4
#define SSE_MOVE movdqu
#if ISSE > 2
#define SSE3_MOVE lddqu
#else
#define SSE3_MOVE movdqu
#endif
#define SSE_RMOVE movdqa
#define SSE0 xmm0
#define SSE1 xmm1
#define SSE2 xmm2
#define SSE3 xmm3
#define SSE4 xmm4
#define SSE5 xmm5
#define SSE6 xmm6
#define SSE7 xmm7
#define SSE_EMMS
#define REGEXEC(instruction, dest, src, reg) \
__asm SSE3_MOVE reg, src \
__asm instruction dest, reg
#else
#define SSE_INCREMENT 8
#define SSE_SHIFT 3
#define SSE_MOVE movq
#define SSE3_MOVE movq
#define SSE_RMOVE movq
#define SSE0 mm0
#define SSE1 mm1
#define SSE2 mm2
#define SSE3 mm3
#define SSE4 mm4
#define SSE5 mm5
#define SSE6 mm6
#define SSE7 mm7
#define SSE_EMMS __asm emms
#define REGEXEC(instruction, dest, src, reg) \
__asm instruction dest, src
#endif // ISSE
void do_nothing(BYTE *dp, int dpitch, const BYTE *sp, int spitch, int hblocks, int remainder, int incpitch, int height)
{
}
void copy_yv12(BYTE *dp, int dpitch, const BYTE *sp, int spitch, int hblocks, int remainder, int incpitch, int height)
{
AVSenvironment->BitBlt(dp, dpitch, sp, spitch, hblocks * SSE_INCREMENT + 2 * (SSE_INCREMENT + 1) + remainder, height);
}
#define ins2(first, second, reg) \
__asm pmaxub second, reg \
__asm pminub second, first \
__asm pmaxub first, reg
#define ins3(first, second, third, reg) \
__asm pmaxub third, reg \
__asm pminub third, second \
ins2(first, second, reg)
#define ins4(first, second, third, fourth, reg) \
__asm pmaxub fourth, reg \
__asm pminub fourth, third \
ins3(first, second, third, reg)
#define ins5(first, second, third, fourth, fifth, reg) \
__asm pmaxub fifth, reg \
__asm pminub fifth, fourth \
ins4(first, second, third, fourth, reg)
#define ins6(first, second, third, fourth, fifth, sixth, reg) \
__asm pmaxub sixth, reg \
__asm pminub sixth, fifth \
ins5(first, second, third, fourth, fifth, reg)
#define add2(first, second, reg) \
__asm SSE_RMOVE second, reg \
__asm pminub second, first \
__asm pmaxub first, reg
#define add3(first, second, third, reg) \
__asm SSE_RMOVE third, reg \
__asm pminub third, second \
ins2(first, second, reg)
#define add4(first, second, third, fourth, reg) \
__asm SSE_RMOVE fourth, reg \
__asm pminub fourth, third \
ins3(first, second, third, reg)
#define add5(first, second, third, fourth, fifth, reg) \
__asm SSE_RMOVE fifth, reg \
__asm pminub fifth, fourth \
ins4(first, second, third, fourth, reg)
#define add6(first, second, third, fourth, fifth, sixth, reg) \
__asm SSE_RMOVE sixth, reg \
__asm pminub sixth, fifth \
ins5(first, second, third, fourth, fifth, reg)
#define sub2(first, second, val) \
__asm pmaxub second, val \
__asm pminub second, first
#define sub3(first, second, third, reg) \
__asm pmaxub third, reg \
__asm pminub third, second \
sub2(first, second, reg)
#define sub4(first, second, third, fourth, reg) \
__asm pmaxub fourth, reg \
__asm pminub fourth, third \
sub3(first, second, third, reg)
#define sub5(first, second, third, fourth, fifth, reg) \
__asm pmaxub fifth, reg \
__asm pminub fifth, fourth \
sub4(first, second, third, fourth, reg)
#define sub6(first, second, third, fourth, fifth, sixth, reg) \
__asm pmaxub sixth, reg \
__asm pminub sixth, fifth \
sub5(first, second, third, fourth, fifth, reg)
#define minmax1(min, max, val) \
__asm pminub min, val \
__asm pmaxub max, val
#define minmax2(max1, max2, min2, min1, reg) \
__asm pminub min2, reg \
__asm pmaxub max2, reg \
__asm pmaxub min2, min1 \
__asm pminub max2, max1 \
__asm pminub min1, reg \
__asm pmaxub max1, reg
#define minmax3(max1, max2, max3, min3, min2, min1, reg)\
__asm pminub min3, reg \
__asm pmaxub max3, reg \
__asm pmaxub min3, min2 \
__asm pminub max3, max2 \
minmax2(max1, max2, min2, min1, reg)
#define minmax2sub(max1, max2, min2, min1, val) \
__asm pminub min2, val \
__asm pmaxub max2, val \
__asm pmaxub min2, min1 \
__asm pminub max2, max1
#define minmax3sub(max1, max2, max3, min3, min2, min1, reg)\
__asm pminub min3, reg \
__asm pmaxub max3, reg \
__asm pmaxub min3, min2 \
__asm pminub max3, max2 \
minmax2sub(max1, max2, min2, min1, reg)
void SSE_RemoveGrain4(BYTE *dp, int dpitch, const BYTE *_sp, int spitch, int hblocks, int remainder, int incpitch, int height)
{
__asm mov eax, hblocks
__asm mov ebx, spitch
#ifdef MODIFYPLUGIN
__asm mov ecx, eax
#endif
__asm mov edx, remainder
#if SSE_INCREMENT == 16
__asm add eax, eax
#endif
__asm mov esi, _sp
#ifdef MODIFYPLUGIN
__asm lea eax, [eax * 8 + edx]
#else
__asm lea eax, [eax * 8 + edx + SSE_INCREMENT + 1]
#endif
__asm sub esi, ebx
__asm sub dpitch, eax
__asm neg eax
__asm mov edi, dp
#ifdef MODIFYPLUGIN
__asm inc edi
__asm lea eax, [ebx + eax]
#else
__asm lea eax, [ebx + eax + 1]
__asm align 16
__asm column_loop:
__asm SSE3_MOVE SSE0, [esi]
__asm SSE3_MOVE SSE7, [esi + 1]
add2(SSE0, SSE1, SSE7)
__asm SSE3_MOVE SSE6, [esi + 2]
__asm SSE3_MOVE SSE5, [esi + ebx]
add3(SSE0, SSE1, SSE2, SSE6)
__asm SSE3_MOVE SSE7, [esi + ebx + 2]
add4(SSE0, SSE1, SSE2, SSE3, SSE5)
__asm movd [edi], SSE5
__asm SSE3_MOVE SSE6, [esi + 2*ebx]
add5(SSE0, SSE1, SSE2, SSE3, SSE4, SSE7)
__asm SSE3_MOVE SSE5, [esi + 2*ebx + 1]
sub5(SSE0, SSE1, SSE2, SSE3, SSE4, SSE6)
__asm SSE3_MOVE SSE7, [esi + 2*ebx + 2]
sub4(SSE1, SSE2, SSE3, SSE4, SSE5)
#if ISSE > 1
__asm SSE3_MOVE SSE5, [esi + ebx + 1]
#endif
sub3(SSE2, SSE3, SSE4, SSE7)
#if ISSE > 1
__asm pmaxub SSE4, SSE5
#else
__asm pmaxub SSE4, [esi + ebx + 1]
#endif
__asm pminub SSE3, SSE4
__asm SSE_MOVE [edi + 1], SSE3
// now the pixels in the middle
__asm add esi, SSE_INCREMENT
__asm add edi, SSE_INCREMENT + 1
__asm mov ecx, hblocks
#endif // MODIFYPLUGIN
__asm align 16
__asm middle_loop:
__asm SSE3_MOVE SSE0, [esi]
__asm SSE3_MOVE SSE7, [esi + 1]
add2(SSE0, SSE1, SSE7)
__asm SSE3_MOVE SSE6, [esi + 2]
__asm SSE3_MOVE SSE5, [esi + ebx]
add3(SSE0, SSE1, SSE2, SSE6)
__asm SSE3_MOVE SSE7, [esi + ebx + 2]
add4(SSE0, SSE1, SSE2, SSE3, SSE5)
#if MODIFYPLUGIN == 1
__asm SSE3_MOVE SSE6, [esi + ebx + 1]
add5(SSE0, SSE1, SSE2, SSE3, SSE4, SSE7)
__asm SSE3_MOVE SSE7, [esi + 2*ebx]
add6(SSE0, SSE1, SSE2, SSE3, SSE4, SSE5, SSE6)
__asm SSE3_MOVE SSE6, [esi + 2*ebx + 1]
sub6(SSE0, SSE1, SSE2, SSE3, SSE4, SSE5, SSE7)
__asm SSE3_MOVE SSE7, [esi + 2*ebx + 2]
sub5(SSE1, SSE2, SSE3, SSE4, SSE5, SSE6)
#if ISSE > 1
__asm SSE3_MOVE SSE0, [edi]
#endif
sub4(SSE2, SSE3, SSE4, SSE5, SSE7)
#if ISSE > 1
__asm pmaxub SSE5, SSE0
#else
__asm pmaxub SSE5, [edi]
#endif
__asm add esi, SSE_INCREMENT
__asm pminub SSE3, SSE5
#else // MODIFYPLUGIN == 1
__asm SSE3_MOVE SSE6, [esi + 2*ebx]
add5(SSE0, SSE1, SSE2, SSE3, SSE4, SSE7)
__asm SSE3_MOVE SSE5, [esi + 2*ebx + 1]
sub5(SSE0, SSE1, SSE2, SSE3, SSE4, SSE6)
__asm SSE3_MOVE SSE7, [esi + 2*ebx + 2]
sub4(SSE1, SSE2, SSE3, SSE4, SSE5)
#if ISSE > 1
#ifdef MODIFYPLUGIN
__asm SSE3_MOVE SSE5, [edi]
#else
__asm SSE3_MOVE SSE5, [esi + ebx + 1]
#endif
#endif
sub3(SSE2, SSE3, SSE4, SSE7)
#if ISSE > 1
__asm pmaxub SSE4, SSE5
#else
#ifdef MODIFYPLUGIN
__asm pmaxub SSE4, [edi]
#else // ISSE > 1
__asm pmaxub SSE4, [esi + ebx + 1]
#endif
#endif // ISSE > 1
__asm add esi, SSE_INCREMENT
__asm pminub SSE3, SSE4
#endif //MODIFYPLUGIN == 1
__asm SSE_MOVE [edi], SSE3
__asm add edi, SSE_INCREMENT
__asm dec ecx
__asm jnz middle_loop
// the last pixels
__asm add esi, edx
__asm add edi, edx
__asm SSE3_MOVE SSE0, [esi]
__asm SSE3_MOVE SSE7, [esi + 1]
add2(SSE0, SSE1, SSE7)
__asm SSE3_MOVE SSE6, [esi + 2]
__asm SSE3_MOVE SSE5, [esi + ebx]
add3(SSE0, SSE1, SSE2, SSE6)
__asm SSE3_MOVE SSE7, [esi + ebx + 2]
add4(SSE0, SSE1, SSE2, SSE3, SSE5)
#ifndef MODIFYPLUGIN
__asm SSE_MOVE [edi + 1], SSE7
#endif
#if MODIFYPLUGIN == 1
__asm SSE3_MOVE SSE6, [esi + ebx + 1]
add5(SSE0, SSE1, SSE2, SSE3, SSE4, SSE7)
__asm SSE3_MOVE SSE7, [esi + 2*ebx]
add6(SSE0, SSE1, SSE2, SSE3, SSE4, SSE5, SSE6)
__asm SSE3_MOVE SSE6, [esi + 2*ebx + 1]
sub6(SSE0, SSE1, SSE2, SSE3, SSE4, SSE5, SSE7)
__asm SSE3_MOVE SSE7, [esi + 2*ebx + 2]
sub5(SSE1, SSE2, SSE3, SSE4, SSE5, SSE6)
#if ISSE > 1
__asm SSE3_MOVE SSE0, [edi]
#endif
sub4(SSE2, SSE3, SSE4, SSE5, SSE7)
#if ISSE > 1
__asm pmaxub SSE5, SSE0
#else
__asm pmaxub SSE5, [edi]
#endif
__asm pminub SSE3, SSE5
#else // MODIFYPLUGIN == 1
__asm SSE3_MOVE SSE6, [esi + 2*ebx]
add5(SSE0, SSE1, SSE2, SSE3, SSE4, SSE7)
__asm SSE3_MOVE SSE5, [esi + 2*ebx + 1]
sub5(SSE0, SSE1, SSE2, SSE3, SSE4, SSE6)
__asm SSE3_MOVE SSE7, [esi + 2*ebx + 2]
sub4(SSE1, SSE2, SSE3, SSE4, SSE5)
#if ISSE > 1
#ifdef MODIFYPLUGIN
__asm SSE3_MOVE SSE5, [edi]
#else
__asm SSE3_MOVE SSE5, [esi + ebx + 1]
#endif
#endif
sub3(SSE2, SSE3, SSE4, SSE7)
#if ISSE > 1
__asm pmaxub SSE4, SSE5
#else
#ifdef MODIFYPLUGIN
__asm pmaxub SSE4, [edi]
#else // ISSE > 1
__asm pmaxub SSE4, [esi + ebx + 1]
#endif
#endif // ISSE > 1
__asm pminub SSE3, SSE4
#endif //MODIFYPLUGIN == 1
__asm add esi, eax
__asm SSE_MOVE [edi], SSE3
__asm add edi, dpitch
__asm dec height
#ifdef MODIFYPLUGIN
__asm mov ecx, hblocks
__asm jnz middle_loop
#else
__asm jnz column_loop
#endif
}
void SSE_RemoveGrain1(BYTE *dp, int dpitch, const BYTE *_sp, int spitch, int hblocks, int remainder, int incpitch, int height)
{
__asm mov eax, hblocks
__asm mov ebx, spitch
#ifdef MODIFYPLUGIN
__asm mov ecx, eax
#endif
__asm mov edx, remainder
#if SSE_INCREMENT == 16
__asm add eax, eax
#endif
__asm mov esi, _sp
#ifdef MODIFYPLUGIN
__asm lea eax, [eax * 8 + edx]
#else
__asm lea eax, [eax * 8 + edx + SSE_INCREMENT + 1]
#endif
__asm sub esi, ebx
__asm sub dpitch, eax
__asm neg eax
__asm mov edi, dp
#ifdef MODIFYPLUGIN
__asm inc edi
__asm lea eax, [ebx + eax]
#else
__asm lea eax, [ebx + eax + 1]
__asm align 16
__asm column_loop:
__asm SSE3_MOVE SSE0, [esi]
__asm SSE3_MOVE SSE5, [esi + 1]
__asm SSE_RMOVE SSE1, SSE0
__asm SSE3_MOVE SSE4, [esi + 2]
minmax1(SSE0, SSE1, SSE5)
__asm SSE3_MOVE SSE5, [esi + 2*ebx]
minmax1(SSE0, SSE1, SSE4)
__asm SSE3_MOVE SSE4, [esi + 2*ebx + 1]
minmax1(SSE0, SSE1, SSE5)
__asm SSE3_MOVE SSE5, [esi + 2*ebx + 2]
minmax1(SSE0, SSE1, SSE4)
__asm SSE3_MOVE SSE4, [esi + ebx]
minmax1(SSE0, SSE1, SSE5)
__asm SSE3_MOVE SSE5, [esi + ebx + 2]
minmax1(SSE0, SSE1, SSE4)
__asm movd [edi], SSE4 // only for saving the first byte
minmax1(SSE0, SSE1, SSE5)
REGEXEC(pmaxub, SSE0, [esi + ebx + 1], SSE7)
__asm pminub SSE0, SSE1
__asm SSE_MOVE [edi + 1], SSE0
// now the pixels in the middle
__asm add esi, SSE_INCREMENT
__asm add edi, SSE_INCREMENT + 1
__asm mov ecx, hblocks
#endif // MODIFYPLUGIN
__asm align 16
__asm middle_loop:
__asm SSE3_MOVE SSE0, [esi]
__asm SSE3_MOVE SSE5, [esi + 1]
__asm SSE_RMOVE SSE1, SSE0
__asm SSE3_MOVE SSE4, [esi + 2]
minmax1(SSE0, SSE1, SSE5)
__asm SSE3_MOVE SSE6, [esi + 2*ebx]
minmax1(SSE0, SSE1, SSE4)
__asm SSE3_MOVE SSE5, [esi + 2*ebx + 1]
minmax1(SSE0, SSE1, SSE6)
__asm SSE3_MOVE SSE4, [esi + 2*ebx + 2]
minmax1(SSE0, SSE1, SSE5)
__asm SSE3_MOVE SSE6, [esi + ebx]
minmax1(SSE0, SSE1, SSE4)
#if MODIFYPLUGIN == 1
__asm SSE3_MOVE SSE5, [esi + ebx + 1]
minmax1(SSE0, SSE1, SSE6)
__asm SSE3_MOVE SSE4, [esi + ebx + 2]
minmax1(SSE0, SSE1, SSE5)
minmax1(SSE0, SSE1, SSE4)
#else // MODIFYPLUGIN
__asm SSE3_MOVE SSE5, [esi + ebx + 2]
minmax1(SSE0, SSE1, SSE6)
minmax1(SSE0, SSE1, SSE5)
#endif // MODIFYPLUGIN
#ifdef MODIFYPLUGIN
REGEXEC(pmaxub, SSE0, [edi], SSE7)
#else
REGEXEC(pmaxub, SSE0, [esi + ebx + 1], SSE7)
#endif
__asm pminub SSE0, SSE1
__asm add esi, SSE_INCREMENT
__asm SSE_MOVE [edi], SSE0
__asm add edi, SSE_INCREMENT
#if (MODIFYPLUGIN == 1) && (ISSE > 1)
__asm dec ecx
__asm jnz middle_loop
#else
__asm loop middle_loop
#endif
// the last pixels
__asm add esi, edx
__asm add edi, edx
__asm SSE3_MOVE SSE0, [esi]
__asm SSE3_MOVE SSE5, [esi + 1]
__asm SSE_RMOVE SSE1, SSE0
__asm SSE3_MOVE SSE4, [esi + 2]
minmax1(SSE0, SSE1, SSE5)
__asm SSE3_MOVE SSE6, [esi + 2*ebx]
minmax1(SSE0, SSE1, SSE4)
__asm SSE3_MOVE SSE5, [esi + 2*ebx + 1]
minmax1(SSE0, SSE1, SSE6)
__asm SSE3_MOVE SSE4, [esi + 2*ebx + 2]
minmax1(SSE0, SSE1, SSE5)
__asm SSE3_MOVE SSE6, [esi + ebx]
minmax1(SSE0, SSE1, SSE4)
#if MODIFYPLUGIN == 1
__asm SSE3_MOVE SSE5, [esi + ebx + 1]
minmax1(SSE0, SSE1, SSE6)
__asm SSE3_MOVE SSE4, [esi + ebx + 2]
minmax1(SSE0, SSE1, SSE5)
minmax1(SSE0, SSE1, SSE4)
#else // MODIFYPLUGIN
__asm SSE3_MOVE SSE5, [esi + ebx + 2]
minmax1(SSE0, SSE1, SSE6)
#ifndef MODIFYPLUGIN
__asm SSE_MOVE [edi + 1], SSE5 // only for saving the last byte
#endif
minmax1(SSE0, SSE1, SSE5)
#endif // MODIFYPLUGIN
#ifdef MODIFYPLUGIN
REGEXEC(pmaxub, SSE0, [edi], SSE7)
#else
REGEXEC(pmaxub, SSE0, [esi + ebx + 1], SSE7)
#endif
__asm pminub SSE0, SSE1
__asm SSE_MOVE [edi], SSE0
__asm add esi, eax
__asm add edi, dpitch
__asm dec height
#ifdef MODIFYPLUGIN
__asm mov ecx, hblocks
__asm jnz middle_loop
#else
__asm jnz column_loop
#endif
}
void SSE_RemoveGrain2(BYTE *dp, int dpitch, const BYTE *_sp, int spitch, int hblocks, int remainder, int incpitch, int height)
{
__asm mov eax, hblocks
__asm mov ebx, spitch
#ifdef MODIFYPLUGIN
__asm mov ecx, eax
#endif
__asm mov edx, remainder
#if SSE_INCREMENT == 16
__asm add eax, eax
#endif
__asm mov esi, _sp
#ifdef MODIFYPLUGIN
__asm lea eax, [eax * 8 + edx]
#else
__asm lea eax, [eax * 8 + edx + SSE_INCREMENT + 1]
#endif
__asm sub esi, ebx
__asm sub dpitch, eax
__asm neg eax
__asm mov edi, dp
#ifdef MODIFYPLUGIN
__asm inc edi
__asm lea eax, [ebx + eax]
#else
__asm lea eax, [ebx + eax + 1]
__asm align 16
__asm column_loop:
__asm SSE3_MOVE SSE0, [esi]
__asm SSE3_MOVE SSE7, [esi + 1]
add2(SSE0, SSE1, SSE7)
__asm SSE3_MOVE SSE6, [esi + 2]
__asm SSE3_MOVE SSE7, [esi + ebx]
add3(SSE0, SSE1, SSE2, SSE6)
__asm movd [edi], SSE7
__asm SSE3_MOVE SSE6, [esi + ebx + 2]
add4(SSE0, SSE1, SSE2, SSE3, SSE7)
__asm SSE3_MOVE SSE5, [esi + 2*ebx]
minmax2(SSE0, SSE1, SSE2, SSE3, SSE6)
__asm SSE3_MOVE SSE7, [esi + 2*ebx + 1]
minmax2(SSE0, SSE1, SSE2, SSE3, SSE5)
__asm SSE3_MOVE SSE6, [esi + 2*ebx + 2]
minmax2(SSE0, SSE1, SSE2, SSE3, SSE7)
#if ISSE > 1
__asm SSE3_MOVE SSE5, [esi + ebx + 1]
#endif
minmax2sub(SSE0, SSE1, SSE2, SSE3, SSE6)
#if ISSE > 1
__asm pmaxub SSE2, SSE5
#else
__asm pmaxub SSE2, [esi + ebx + 1]
#endif
__asm pminub SSE1, SSE2
__asm SSE_MOVE [edi + 1], SSE1
// now the pixels in the middle
__asm add esi, SSE_INCREMENT
__asm add edi, SSE_INCREMENT + 1
__asm mov ecx, hblocks
#endif // MODIFYPLUGIN
__asm align 16
__asm middle_loop:
__asm SSE3_MOVE SSE0, [esi]
__asm SSE3_MOVE SSE7, [esi + 1]
add2(SSE0, SSE1, SSE7)
__asm SSE3_MOVE SSE6, [esi + 2]
__asm SSE3_MOVE SSE7, [esi + ebx]
add3(SSE0, SSE1, SSE2, SSE6)
#if MODIFYPLUGIN == 1
__asm SSE3_MOVE SSE4, [esi + ebx + 1]
#else
__asm SSE3_MOVE SSE6, [esi + ebx + 2]
#endif
add4(SSE0, SSE1, SSE2, SSE3, SSE7)
#if MODIFYPLUGIN == 1
__asm SSE3_MOVE SSE6, [esi + ebx + 2]
minmax2(SSE0, SSE1, SSE2, SSE3, SSE4)
#endif
__asm SSE3_MOVE SSE5, [esi + 2*ebx]
minmax2(SSE0, SSE1, SSE2, SSE3, SSE6)
__asm SSE3_MOVE SSE7, [esi + 2*ebx + 1]
minmax2(SSE0, SSE1, SSE2, SSE3, SSE5)
__asm SSE3_MOVE SSE6, [esi + 2*ebx + 2]
minmax2(SSE0, SSE1, SSE2, SSE3, SSE7)
#if ISSE > 1
#ifdef MODIFYPLUGIN
__asm SSE3_MOVE SSE5, [edi]
#else
__asm SSE3_MOVE SSE5, [esi + ebx + 1]
#endif
#endif
minmax2sub(SSE0, SSE1, SSE2, SSE3, SSE6)
#if ISSE > 1
__asm pmaxub SSE2, SSE5
#else
#ifdef MODIFYPLUGIN
__asm pmaxub SSE2, [edi]
#else
__asm pmaxub SSE2, [esi + ebx + 1]
#endif
#endif // ISSE > 1
__asm pminub SSE1, SSE2
__asm add esi, SSE_INCREMENT
__asm SSE_MOVE [edi], SSE1
__asm add edi, SSE_INCREMENT
__asm dec ecx
__asm jnz middle_loop
// the last pixels
__asm add esi, edx
__asm add edi, edx
__asm SSE3_MOVE SSE0, [esi]
__asm SSE3_MOVE SSE7, [esi + 1]
add2(SSE0, SSE1, SSE7)
__asm SSE3_MOVE SSE6, [esi + 2]
__asm SSE3_MOVE SSE7, [esi + ebx]
add3(SSE0, SSE1, SSE2, SSE6)
#if MODIFYPLUGIN == 1
__asm SSE3_MOVE SSE4, [esi + ebx + 1]
#else
__asm SSE3_MOVE SSE6, [esi + ebx + 2]
#endif
add4(SSE0, SSE1, SSE2, SSE3, SSE7)
#if MODIFYPLUGIN == 1
__asm SSE3_MOVE SSE6, [esi + ebx + 2]
minmax2(SSE0, SSE1, SSE2, SSE3, SSE4)
#endif
#ifndef MODIFYPLUGIN
__asm SSE_MOVE [edi + 1], SSE6
#endif
__asm SSE3_MOVE SSE5, [esi + 2*ebx]
minmax2(SSE0, SSE1, SSE2, SSE3, SSE6)
__asm SSE3_MOVE SSE7, [esi + 2*ebx + 1]
minmax2(SSE0, SSE1, SSE2, SSE3, SSE5)
__asm SSE3_MOVE SSE6, [esi + 2*ebx + 2]
minmax2(SSE0, SSE1, SSE2, SSE3, SSE7)
#if ISSE > 1
#ifdef MODIFYPLUGIN
__asm SSE3_MOVE SSE5, [edi]
#else
__asm SSE3_MOVE SSE5, [esi + ebx + 1]
#endif