aboutsummaryrefslogtreecommitdiffstats
path: root/libavfilter/x86/vf_removegrain.asm
blob: d049bf257d3583431ce8ce3e18fd145414f60e56 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
;*****************************************************************************
;* x86-optimized functions for removegrain filter
;*
;* Copyright (C) 2015 James Darnley
;*
;* This file is part of FFmpeg.
;*
;* FFmpeg 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.
;*
;* FFmpeg 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 FFmpeg; if not, write to the Free Software Foundation, Inc.,
;* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
;*****************************************************************************

; column: -1  0 +1
; row -1: a1 a2 a3
; row  0: a4  c a5
; row +1: a6 a7 a8

%include "libavutil/x86/x86util.asm"

SECTION_RODATA 32

pw_4:    times 16 dw 4
pw_8:    times 16 dw 8
pw_div9: times 16 dw ((1<<16)+4)/9

SECTION .text

;*** Preprocessor helpers

%define a1 srcq+stride_n-1
%define a2 srcq+stride_n
%define a3 srcq+stride_n+1
%define a4 srcq-1
%define c  srcq
%define a5 srcq+1
%define a6 srcq+stride_p-1
%define a7 srcq+stride_p
%define a8 srcq+stride_p+1

; %1 dest simd register
; %2 source memory location
; %3 zero location (simd register/memory)
%macro LOAD 3
    movh %1, %2
    punpcklbw %1, %3
%endmacro

%macro LOAD_SQUARE 0
    movu m1, [a1]
    movu m2, [a2]
    movu m3, [a3]
    movu m4, [a4]
    movu m0, [c]
    movu m5, [a5]
    movu m6, [a6]
    movu m7, [a7]
    movu m8, [a8]
%endmacro

; %1 zero location (simd register/memory)
%macro LOAD_SQUARE_16 1
    LOAD m1, [a1], %1
    LOAD m2, [a2], %1
    LOAD m3, [a3], %1
    LOAD m4, [a4], %1
    LOAD m0, [c], %1
    LOAD m5, [a5], %1
    LOAD m6, [a6], %1
    LOAD m7, [a7], %1
    LOAD m8, [a8], %1
%endmacro

; %1 data type
; %2 simd register to hold maximums
; %3 simd register to hold minimums
; %4 temp location (simd register/memory)
%macro SORT_PAIR 4
    mova   %4, %2
    pmin%1 %2, %3
    pmax%1 %3, %4
%endmacro

%macro SORT_AXIS 0
    SORT_PAIR ub, m1, m8, m9
    SORT_PAIR ub, m2, m7, m10
    SORT_PAIR ub, m3, m6, m11
    SORT_PAIR ub, m4, m5, m12
%endmacro


%macro SORT_AXIS_16 0
    SORT_PAIR sw, m1, m8, m9
    SORT_PAIR sw, m2, m7, m10
    SORT_PAIR sw, m3, m6, m11
    SORT_PAIR sw, m4, m5, m12
%endmacro

; The loop doesn't need to do all the iterations.  It could stop when the right
; pixels are in the right registers.
%macro SORT_SQUARE 0
    %assign k 7
    %rep 7
        %assign i 1
        %assign j 2
        %rep k
            SORT_PAIR ub, m %+ i , m %+ j , m9
            %assign i i+1
            %assign j j+1
        %endrep
        %assign k k-1
    %endrep
%endmacro

; %1 dest simd register
; %2 source (simd register/memory)
; %3 temp simd register
%macro ABS_DIFF 3
    mova %3, %2
    psubusb %3, %1
    psubusb %1, %2
    por %1, %3
%endmacro

; %1 dest simd register
; %2 source (simd register/memory)
; %3 temp simd register
%macro ABS_DIFF_W 3
    mova %3, %2
    psubusw %3, %1
    psubusw %1, %2
    por %1, %3
%endmacro

; %1 simd register that holds the "false" values and will hold the result
; %2 simd register that holds the "true" values
; %3 location (simd register/memory) that hold the mask
%macro BLEND 3
%if cpuflag(avx2)
    vpblendvb %1, %1, %2, %3
%else
    pand      %2, %3
    pandn     %3, %1
    por       %3, %2
    SWAP      %1, %3
%endif
%endmacro

; Functions

INIT_XMM sse2
cglobal rg_fl_mode_1, 4, 5, 3, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    .loop:
        movu m0, [a1]
        mova m1, m0

        movu m2, [a2]
        pmaxub m0, m2
        pminub m1, m2

        movu m2, [a3]
        pmaxub m0, m2
        pminub m1, m2

        movu m2, [a4]
        pmaxub m0, m2
        pminub m1, m2

        movu m2, [a5]
        pmaxub m0, m2
        pminub m1, m2

        movu m2, [a6]
        pmaxub m0, m2
        pminub m1, m2

        movu m2, [a7]
        pmaxub m0, m2
        pminub m1, m2

        movu m2, [a8]
        pmaxub m0, m2
        pminub m1, m2

        movu m2, [c]
        pminub m2, m0
        pmaxub m2, m1

        movu [dstq], m2
        add srcq, mmsize
        add dstq, mmsize
        sub pixelsd, mmsize
    jg .loop
RET

%if ARCH_X86_64
cglobal rg_fl_mode_2, 4, 5, 10, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    .loop:
        LOAD_SQUARE
        SORT_SQUARE

        CLIPUB m0, m2, m7

        movu [dstq], m0
        add srcq, mmsize
        add dstq, mmsize
        sub pixelsd, mmsize
    jg .loop
RET

cglobal rg_fl_mode_3, 4, 5, 10, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    .loop:
        LOAD_SQUARE
        SORT_SQUARE

        CLIPUB m0, m3, m6

        movu [dstq], m0
        add srcq, mmsize
        add dstq, mmsize
        sub pixelsd, mmsize
    jg .loop
RET

cglobal rg_fl_mode_4, 4, 5, 10, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    .loop:
        LOAD_SQUARE
        SORT_SQUARE

        CLIPUB m0, m4, m5

        movu [dstq], m0
        add srcq, mmsize
        add dstq, mmsize
        sub pixelsd, mmsize
    jg .loop
RET

cglobal rg_fl_mode_5, 4, 5, 13, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    .loop:
        LOAD_SQUARE
        SORT_AXIS

        mova m9, m0
        mova m10, m0
        mova m11, m0
        mova m12, m0

        CLIPUB m9, m1, m8
        CLIPUB m10, m2, m7
        CLIPUB m11, m3, m6
        CLIPUB m12, m4, m5

        mova m8, m9  ; clip1
        mova m7, m10 ; clip2
        mova m6, m11 ; clip3
        mova m5, m12 ; clip4

        ABS_DIFF m9, m0, m1  ; c1
        ABS_DIFF m10, m0, m2 ; c2
        ABS_DIFF m11, m0, m3 ; c3
        ABS_DIFF m12, m0, m4 ; c4

        pminub m9, m10
        pminub m9, m11
        pminub m9, m12 ; mindiff

        pcmpeqb m10, m9
        pcmpeqb m11, m9
        pcmpeqb m12, m9

        ; Notice the order here: c1, c3, c2, c4
        BLEND m8, m6, m11
        BLEND m8, m7, m10
        BLEND m8, m5, m12

        movu [dstq], m8
        add srcq, mmsize
        add dstq, mmsize
        sub pixelsd, mmsize
    jg .loop
RET

cglobal rg_fl_mode_6, 4, 5, 16, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    ; Some register saving suggestions: the zero can be somewhere other than a
    ; register, the center pixels could be on the stack.

    pxor m15, m15
    .loop:
        LOAD_SQUARE_16 m15
        SORT_AXIS_16

        mova m9, m0
        mova m10, m0
        mova m11, m0
        mova m12, m0
        CLIPW m9, m1, m8  ; clip1
        CLIPW m10, m2, m7 ; clip2
        CLIPW m11, m3, m6 ; clip3
        CLIPW m12, m4, m5 ; clip4

        psubw m8, m1 ; d1
        psubw m7, m2 ; d2
        psubw m6, m3 ; d3
        psubw m5, m4 ; d4

        mova m1, m9
        mova m2, m10
        mova m3, m11
        mova m4, m12
        ABS_DIFF_W m1, m0, m13
        ABS_DIFF_W m2, m0, m14
        ABS_DIFF_W m3, m0, m13
        ABS_DIFF_W m4, m0, m14
        psllw m1, 1
        psllw m2, 1
        psllw m3, 1
        psllw m4, 1
        paddw m1, m8 ; c1
        paddw m2, m7 ; c2
        paddw m3, m6 ; c3
        paddw m4, m5 ; c4
        ; As the differences (d1..d4) can only be positive, there is no need to
        ; clip to zero.  Also, the maximum positive value is less than 768.

        pminsw m1, m2
        pminsw m1, m3
        pminsw m1, m4

        pcmpeqw m2, m1
        pcmpeqw m3, m1
        pcmpeqw m4, m1

        BLEND m9, m11, m3
        BLEND m9, m10, m2
        BLEND m9, m12, m4
        packuswb m9, m9

        movh [dstq], m9
        add srcq, mmsize/2
        add dstq, mmsize/2
        sub pixelsd, mmsize/2
    jg .loop
RET

; This is just copy-pasted straight from mode 6 with the left shifts removed.
cglobal rg_fl_mode_7, 4, 5, 16, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    ; Can this be done without unpacking?

    pxor m15, m15
    .loop:
        LOAD_SQUARE_16 m15
        SORT_AXIS_16

        mova m9, m0
        mova m10, m0
        mova m11, m0
        mova m12, m0
        CLIPW m9, m1, m8  ; clip1
        CLIPW m10, m2, m7 ; clip2
        CLIPW m11, m3, m6 ; clip3
        CLIPW m12, m4, m5 ; clip4

        psubw m8, m1 ; d1
        psubw m7, m2 ; d2
        psubw m6, m3 ; d3
        psubw m5, m4 ; d4

        mova m1, m9
        mova m2, m10
        mova m3, m11
        mova m4, m12
        ABS_DIFF_W m1, m0, m13
        ABS_DIFF_W m2, m0, m14
        ABS_DIFF_W m3, m0, m13
        ABS_DIFF_W m4, m0, m14
        paddw m1, m8 ; c1
        paddw m2, m7 ; c2
        paddw m3, m6 ; c3
        paddw m4, m5 ; c4

        pminsw m1, m2
        pminsw m1, m3
        pminsw m1, m4

        pcmpeqw m2, m1
        pcmpeqw m3, m1
        pcmpeqw m4, m1

        BLEND m9, m11, m3
        BLEND m9, m10, m2
        BLEND m9, m12, m4
        packuswb m9, m9

        movh [dstq], m9
        add srcq, mmsize/2
        add dstq, mmsize/2
        sub pixelsd, mmsize/2
    jg .loop
RET

; This is just copy-pasted straight from mode 6 with a few changes.
cglobal rg_fl_mode_8, 4, 5, 16, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    pxor m15, m15
    .loop:
        LOAD_SQUARE_16 m15
        SORT_AXIS_16

        mova m9, m0
        mova m10, m0
        mova m11, m0
        mova m12, m0
        CLIPW m9, m1, m8  ; clip1
        CLIPW m10, m2, m7 ; clip2
        CLIPW m11, m3, m6 ; clip3
        CLIPW m12, m4, m5 ; clip4

        psubw m8, m1 ; d1
        psubw m7, m2 ; d2
        psubw m6, m3 ; d3
        psubw m5, m4 ; d4
        psllw m8, 1
        psllw m7, 1
        psllw m6, 1
        psllw m5, 1

        mova m1, m9
        mova m2, m10
        mova m3, m11
        mova m4, m12
        ABS_DIFF_W m1, m0, m13
        ABS_DIFF_W m2, m0, m14
        ABS_DIFF_W m3, m0, m13
        ABS_DIFF_W m4, m0, m14
        paddw m1, m8 ; c1
        paddw m2, m7 ; c1
        paddw m3, m6 ; c1
        paddw m4, m5 ; c1
        ; As the differences (d1..d4) can only be positive, there is no need to
        ; clip to zero.  Also, the maximum positive value is less than 768.

        pminsw m1, m2
        pminsw m1, m3
        pminsw m1, m4

        pcmpeqw m2, m1
        pcmpeqw m3, m1
        pcmpeqw m4, m1

        BLEND m9, m11, m3
        BLEND m9, m10, m2
        BLEND m9, m12, m4
        packuswb m9, m9

        movh [dstq], m9
        add srcq, mmsize/2
        add dstq, mmsize/2
        sub pixelsd, mmsize/2
    jg .loop
RET

cglobal rg_fl_mode_9, 4, 5, 13, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    .loop:
        LOAD_SQUARE
        SORT_AXIS

        mova m9, m0
        mova m10, m0
        mova m11, m0
        mova m12, m0
        CLIPUB m9, m1, m8  ; clip1
        CLIPUB m10, m2, m7 ; clip2
        CLIPUB m11, m3, m6 ; clip3
        CLIPUB m12, m4, m5 ; clip4

        psubb m8, m1 ; d1
        psubb m7, m2 ; d2
        psubb m6, m3 ; d3
        psubb m5, m4 ; d4

        pminub m8, m7
        pminub m8, m6
        pminub m8, m5

        pcmpeqb m7, m8
        pcmpeqb m6, m8
        pcmpeqb m5, m8

        BLEND m9, m11, m6
        BLEND m9, m10, m7
        BLEND m9, m12, m5

        movu [dstq], m9
        add srcq, mmsize
        add dstq, mmsize
        sub pixelsd, mmsize
    jg .loop
RET
%endif

cglobal rg_fl_mode_10, 4, 5, 8, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    .loop:
        movu m0, [c]

        movu m1, [a4]
        mova m2, m1
        ABS_DIFF m1, m0, m7

        movu m3, [a5]       ; load pixel
        mova m4, m3
        ABS_DIFF m4, m0, m7 ; absolute difference from center
        pminub m1, m4       ; mindiff
        pcmpeqb m4, m1      ; if (difference == mindiff)
        BLEND m2, m3, m4    ;     return pixel

        movu m5, [a1]
        mova m6, m5
        ABS_DIFF m6, m0, m7
        pminub m1, m6
        pcmpeqb m6, m1
        BLEND m2, m5, m6

        movu m3, [a3]
        mova m4, m3
        ABS_DIFF m4, m0, m7
        pminub m1, m4
        pcmpeqb m4, m1
        BLEND m2, m3, m4

        movu m5, [a2]
        mova m6, m5
        ABS_DIFF m6, m0, m7
        pminub m1, m6
        pcmpeqb m6, m1
        BLEND m2, m5, m6

        movu m3, [a6]
        mova m4, m3
        ABS_DIFF m4, m0, m7
        pminub m1, m4
        pcmpeqb m4, m1
        BLEND m2, m3, m4

        movu m5, [a8]
        mova m6, m5
        ABS_DIFF m6, m0, m7
        pminub m1, m6
        pcmpeqb m6, m1
        BLEND m2, m5, m6

        movu m3, [a7]
        mova m4, m3
        ABS_DIFF m4, m0, m7
        pminub m1, m4
        pcmpeqb m4, m1
        BLEND m2, m3, m4

        movu [dstq], m2
        add srcq, mmsize
        add dstq, mmsize
        sub pixelsd, mmsize
    jg .loop
RET

cglobal rg_fl_mode_11_12, 4, 5, 7, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    pxor m0, m0
    .loop:
        LOAD m1, [c], m0
        LOAD m2, [a2], m0
        LOAD m3, [a4], m0
        LOAD m4, [a5], m0
        LOAD m5, [a7], m0

        psllw m1, 2
        paddw m2, m3
        paddw m4, m5
        paddw m2, m4
        psllw m2, 1

        LOAD m3, [a1], m0
        LOAD m4, [a3], m0
        LOAD m5, [a6], m0
        LOAD m6, [a8], m0
        paddw m1, m2
        paddw m3, m4
        paddw m5, m6
        paddw m1, m3
        paddw m1, m5

        paddw m1, [pw_8]
        psraw m1, 4

        packuswb m1, m1

        movh [dstq], m1
        add srcq, mmsize/2
        add dstq, mmsize/2
        sub pixelsd, mmsize/2
    jg .loop
RET

cglobal rg_fl_mode_13_14, 4, 5, 8, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    .loop:
        movu m1, [a1]
        movu m2, [a8]
        mova m0, m1
        pavgb m1, m2
        ABS_DIFF m0, m2, m6

        movu m3, [a3]
        movu m4, [a6]
        mova m5, m3
        pavgb m3, m4
        ABS_DIFF m5, m4, m7
        pminub m0, m5
        pcmpeqb m5, m0
        BLEND m1, m3, m5

        movu m2, [a2]
        movu m3, [a7]
        mova m4, m2
        pavgb m2, m3
        ABS_DIFF m4, m3, m6
        pminub m0, m4
        pcmpeqb m4, m0
        BLEND m1, m2, m4

        movu [dstq], m1
        add srcq, mmsize
        add dstq, mmsize
        sub pixelsd, mmsize
    jg .loop
RET

%if ARCH_X86_64
cglobal rg_fl_mode_15_16, 4, 5, 16, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    pxor m15, m15
    .loop:
        LOAD_SQUARE_16 m15

        mova m9, m1
        mova m10, m2
        mova m11, m3
        ABS_DIFF_W m9, m8, m12
        ABS_DIFF_W m10, m7, m13
        ABS_DIFF_W m11, m6, m14
        pminsw m9, m10
        pminsw m9, m11
        pcmpeqw m10, m9
        pcmpeqw m11, m9

        mova m12, m2
        mova m13, m1
        mova m14, m6
        paddw m12, m7
        psllw m12, 1
        paddw m13, m3
        paddw m14, m8
        paddw m12, [pw_4]
        paddw m13, m14
        paddw m12, m13
        psrlw m12, 3

        SORT_PAIR ub, m1, m8, m0
        SORT_PAIR ub, m2, m7, m9
        SORT_PAIR ub, m3, m6, m14
        mova m4, m12
        mova m5, m12
        CLIPW m4, m1, m8
        CLIPW m5, m2, m7
        CLIPW m12, m3, m6

        BLEND m4, m12, m11
        BLEND m4,  m5, m10
        packuswb m4, m4

        movh [dstq], m4
        add srcq, mmsize/2
        add dstq, mmsize/2
        sub pixelsd, mmsize/2
    jg .loop
RET

cglobal rg_fl_mode_17, 4, 5, 9, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    .loop:
        LOAD_SQUARE
        SORT_AXIS

        pmaxub m1, m2
        pmaxub m3, m4

        pminub m8, m7
        pminub m5, m6

        pmaxub m1, m3
        pminub m8, m5

        mova m2, m1
        pminub m1, m8
        pmaxub m8, m2

        CLIPUB m0, m1, m8

        movu [dstq], m0
        add srcq, mmsize
        add dstq, mmsize
        sub pixelsd, mmsize
    jg .loop
RET

cglobal rg_fl_mode_18, 4, 5, 16, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    .loop:
        LOAD_SQUARE

        mova m9, m1
        mova m10, m8
        ABS_DIFF m9, m0, m11
        ABS_DIFF m10, m0, m12
        pmaxub m9, m10 ; m9 = d1

        mova m10, m2
        mova m11, m7
        ABS_DIFF m10, m0, m12
        ABS_DIFF m11, m0, m13
        pmaxub m10, m11 ; m10 = d2

        mova m11, m3
        mova m12, m6
        ABS_DIFF m11, m0, m13
        ABS_DIFF m12, m0, m14
        pmaxub m11, m12 ; m11 = d3

        mova m12, m4
        mova m13, m5
        ABS_DIFF m12, m0, m14
        ABS_DIFF m13, m0, m15
        pmaxub m12, m13 ; m12 = d4

        mova m13, m9
        pminub m13, m10
        pminub m13, m11
        pminub m13, m12 ; m13 = mindiff

        pcmpeqb m10, m13
        pcmpeqb m11, m13
        pcmpeqb m12, m13

        mova m14, m1
        pminub m1, m8
        pmaxub m8, m14

        mova m13, m0
        mova m14, m1
        pminub m1, m8
        pmaxub m8, m14
        CLIPUB m13, m1, m8 ; m13 = ret...d1

        mova m14, m0
        mova m15, m3
        pminub m3, m6
        pmaxub m6, m15
        CLIPUB m14, m3, m6
        pand m14, m11
        pandn m11, m13
        por m14, m11 ; m14 = ret...d3

        mova m15, m0
        mova m1, m2
        pminub m2, m7
        pmaxub m7, m1
        CLIPUB m15, m2, m7
        pand m15, m10
        pandn m10, m14
        por m15, m10 ; m15 = ret...d2

        mova m1, m0
        mova m2, m4
        pminub m4, m5
        pmaxub m5, m2
        CLIPUB m1, m4, m5
        pand m1, m12
        pandn m12, m15
        por m1, m12 ; m15 = ret...d4

        movu [dstq], m1
        add srcq, mmsize
        add dstq, mmsize
        sub pixelsd, mmsize
    jg .loop
RET
%endif

cglobal rg_fl_mode_19, 4, 5, 7, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    pxor m0, m0
    .loop:
        LOAD m1, [a1], m0
        LOAD m2, [a2], m0
        paddw m1, m2

        LOAD m3, [a3], m0
        LOAD m4, [a4], m0
        paddw m3, m4

        LOAD m5, [a5], m0
        LOAD m6, [a6], m0
        paddw m5, m6

        LOAD m2, [a7], m0
        LOAD m4, [a8], m0
        paddw m2, m4

        paddw m1, m3
        paddw m2, m5
        paddw m1, m2

        paddw m1, [pw_4]
        psraw m1, 3

        packuswb m1, m1

        movh [dstq], m1
        add srcq, mmsize/2
        add dstq, mmsize/2
        sub pixelsd, mmsize/2
    jg .loop
RET

cglobal rg_fl_mode_20, 4, 5, 7, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    pxor m0, m0
    .loop:
        LOAD m1, [a1], m0
        LOAD m2, [a2], m0
        paddw m1, m2

        LOAD m3, [a3], m0
        LOAD m4, [a4], m0
        paddw m3, m4

        LOAD m5, [a5], m0
        LOAD m6, [a6], m0
        paddw m5, m6

        LOAD m2, [a7], m0
        LOAD m4, [a8], m0
        paddw m2, m4

        LOAD m6, [c], m0
        paddw m1, m3
        paddw m2, m5
        paddw m6, [pw_4]

        paddw m1, m2
        paddw m1, m6

        pmulhuw m1, [pw_div9]

        packuswb m1, m1

        movh [dstq], m1
        add srcq, mmsize/2
        add dstq, mmsize/2
        sub pixelsd, mmsize/2
    jg .loop
RET

cglobal rg_fl_mode_21, 4, 5, 8, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    pxor m0, m0
    .loop:
        movu m1, [a1]
        movu m2, [a8]
        pavgb m7, m1, m2
        punpckhbw m3, m1, m0
        punpcklbw m1, m0
        punpckhbw m4, m2, m0
        punpcklbw m2, m0
        paddw m3, m4
        paddw m1, m2
        psrlw m3, 1
        psrlw m1, 1
        packuswb m1, m3

        movu m2, [a2]
        movu m3, [a7]
        pavgb m6, m2, m3
        punpckhbw m4, m2, m0
        punpcklbw m2, m0
        punpckhbw m5, m3, m0
        punpcklbw m3, m0
        paddw m4, m5
        paddw m2, m3
        psrlw m4, 1
        psrlw m2, 1
        packuswb m2, m4

        pminub m1, m2
        pmaxub m7, m6

        movu m2, [a3]
        movu m3, [a6]
        pavgb m6, m2, m3
        punpckhbw m4, m2, m0
        punpcklbw m2, m0
        punpckhbw m5, m3, m0
        punpcklbw m3, m0
        paddw m4, m5
        paddw m2, m3
        psrlw m4, 1
        psrlw m2, 1
        packuswb m2, m4

        pminub m1, m2
        pmaxub m7, m6

        movu m2, [a4]
        movu m3, [a5]
        pavgb m6, m2, m3
        punpckhbw m4, m2, m0
        punpcklbw m2, m0
        punpckhbw m5, m3, m0
        punpcklbw m3, m0
        paddw m4, m5
        paddw m2, m3
        psrlw m4, 1
        psrlw m2, 1
        packuswb m2, m4

        pminub m1, m2
        pmaxub m7, m6

        movu m3, [c]
        CLIPUB m3, m1, m7

        movu [dstq], m3
        add srcq, mmsize
        add dstq, mmsize
        sub pixelsd, mmsize
    jg .loop
RET

cglobal rg_fl_mode_22, 4, 5, 8, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    .loop:
        movu m0, [a1]
        movu m1, [a8]
        pavgb m0, m1
        movu m2, [a2]
        movu m3, [a7]
        pavgb m2, m3
        movu m4, [a3]
        movu m5, [a6]
        pavgb m4, m5
        movu m6, [a4]
        movu m7, [a5]
        pavgb m6, m7

        mova m1, m0
        mova m3, m2
        mova m5, m4
        mova m7, m6
        pminub m0, m2
        pminub m4, m6
        pmaxub m1, m3
        pmaxub m5, m7
        pminub m0, m4
        pmaxub m1, m5

        movu m2, [c]
        CLIPUB m2, m0, m1

        movu [dstq], m2
        add srcq, mmsize
        add dstq, mmsize
        sub pixelsd, mmsize
    jg .loop
RET

%if ARCH_X86_64
cglobal rg_fl_mode_23, 4, 5, 16, 0, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    pxor m15, m15
    .loop:
        LOAD_SQUARE_16 m15
        SORT_AXIS_16

        mova m9, m8
        mova m10, m7
        mova m11, m6
        mova m12, m5
        psubw m9, m1  ; linediff1
        psubw m10, m2 ; linediff2
        psubw m11, m3 ; linediff3
        psubw m12, m4 ; linediff4

        psubw m1, m0
        psubw m2, m0
        psubw m3, m0
        psubw m4, m0
        pminsw m1, m9  ; d1
        pminsw m2, m10 ; d2
        pminsw m3, m11 ; d3
        pminsw m4, m12 ; d4
        pmaxsw m1, m2
        pmaxsw m3, m4
        pmaxsw m1, m3
        pmaxsw m1, m15 ; d

        mova m13, m0
        mova m14, m0
        mova m2, m0
        mova m4, m0
        psubw m13, m8
        psubw m14, m7
        psubw m2, m6
        psubw m4, m5
        pminsw m9, m13  ; u1
        pminsw m10, m14 ; u2
        pminsw m11, m2  ; u3
        pminsw m12, m4  ; u4
        pmaxsw m9, m10
        pmaxsw m11, m12
        pmaxsw m9, m11
        pmaxsw m9, m15  ; u

        paddw m0, m1
        psubw m0, m9
        packuswb m0, m0

        movh [dstq], m0
        add srcq, mmsize/2
        add dstq, mmsize/2
        sub pixelsd, mmsize/2
    jg .loop
RET

cglobal rg_fl_mode_24, 4, 5, 16, mmsize, dst, src, stride, pixels
    mov r4q, strideq
    neg r4q
    %define stride_p strideq
    %define stride_n r4q

    pxor m15, m15
    .loop:
        LOAD_SQUARE_16 m15
        mova [rsp], m0
        SORT_AXIS_16

        mova m9, m8
        mova m10, m7
        mova m11, m6
        mova m12, m5
        psubw m9, m1  ; linediff1
        psubw m10, m2 ; linediff2
        psubw m11, m3 ; linediff3
        psubw m12, m4 ; linediff4

        psubw m1, [rsp] ; td1
        psubw m2, [rsp] ; td2
        psubw m3, [rsp] ; td3
        psubw m4, [rsp] ; td4
        mova m0, m9
        mova m13, m10
        mova m14, m11
        mova m15, m12
        psubw m0, m1
        psubw m13, m2
        psubw m14, m3
        psubw m15, m4
        pminsw m1, m0  ; d1
        pminsw m2, m13 ; d2
        pminsw m3, m14 ; d3
        pminsw m4, m15 ; d4
        pmaxsw m1, m2
        pmaxsw m3, m4

        mova m0, [rsp]
        mova m13, [rsp]
        mova m14, [rsp]
        mova m15, [rsp]
        psubw m0, m8  ; tu1
        psubw m13, m7 ; tu2
        psubw m14, m6 ; tu3
        psubw m15, m5 ; tu4
        psubw m9, m0
        psubw m10, m13
        psubw m11, m14
        psubw m12, m15
        pminsw m9, m0   ; u1
        pminsw m10, m13 ; u2
        pminsw m11, m14 ; u3
        pminsw m12, m15 ; u4
        pmaxsw m9, m10
        pmaxsw m11, m12

        pmaxsw m1, m3  ; d without max(d,0)
        pmaxsw m9, m11  ; u without max(u,0)
        pxor m15, m15
        pmaxsw m1, m15
        pmaxsw m9, m15

        mova m0, [rsp]
        paddw m0, m1
        psubw m0, m9
        packuswb m0, m0

        movh [dstq], m0
        add srcq, mmsize/2
        add dstq, mmsize/2
        sub pixelsd, mmsize/2
    jg .loop
RET
%endif