aboutsummaryrefslogtreecommitdiffstats
path: root/libavcodec/x86/h264_intrapred_10bit.asm
blob: 24a7bfa875925d96270945e60b80bad5b6b9226f (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
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
;*****************************************************************************
;* MMX/SSE2/AVX-optimized 10-bit H.264 intra prediction code
;*****************************************************************************
;* Copyright (C) 2005-2011 x264 project
;*
;* Authors: Daniel Kang <daniel.d.kang@gmail.com>
;*
;* This file is part of Libav.
;*
;* Libav is free software; you can redistribute it and/or
;* modify it under the terms of the GNU Lesser General Public
;* License as published by the Free Software Foundation; either
;* version 2.1 of the License, or (at your option) any later version.
;*
;* Libav 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
;* Lesser General Public License for more details.
;*
;* You should have received a copy of the GNU Lesser General Public
;* License along with Libav; if not, write to the Free Software
;* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
;******************************************************************************

%include "x86inc.asm"
%include "x86util.asm"

SECTION_RODATA

SECTION .text

cextern pw_16
cextern pw_8
cextern pw_4
cextern pw_2
cextern pw_1

pw_m32101234: dw -3, -2, -1, 0, 1, 2, 3, 4
pw_m3:        times 8 dw -3
pw_pixel_max: times 8 dw ((1 << 10)-1)
pw_512:       times 8 dw 512
pd_17:        times 4 dd 17
pd_16:        times 4 dd 16

; dest, left, right, src
; output: %1 = (t[n-1] + t[n]*2 + t[n+1] + 2) >> 2
%macro PRED4x4_LOWPASS 4
    paddw       %2, %3
    psrlw       %2, 1
    pavgw       %1, %4, %2
%endmacro

;-----------------------------------------------------------------------------
; void pred4x4_down_right(pixel *src, const pixel *topright, int stride)
;-----------------------------------------------------------------------------
%macro PRED4x4_DR 1
cglobal pred4x4_down_right_10_%1, 3,3
    sub       r0, r2
    lea       r1, [r0+r2*2]
    movhps    m1, [r1-8]
    movhps    m2, [r0+r2*1-8]
    movhps    m4, [r0-8]
    punpckhwd m2, m4
    movq      m3, [r0]
    punpckhdq m1, m2
    PALIGNR   m3, m1, 10, m1
    mova      m1, m3
    movhps    m4, [r1+r2*1-8]
    PALIGNR   m3, m4, 14, m4
    mova      m2, m3
    movhps    m4, [r1+r2*2-8]
    PALIGNR   m3, m4, 14, m4
    PRED4x4_LOWPASS m0, m3, m1, m2
    movq      [r1+r2*2], m0
    psrldq    m0, 2
    movq      [r1+r2*1], m0
    psrldq    m0, 2
    movq      [r0+r2*2], m0
    psrldq    m0, 2
    movq      [r0+r2*1], m0
    RET
%endmacro

INIT_XMM
%define PALIGNR PALIGNR_MMX
PRED4x4_DR sse2
%define PALIGNR PALIGNR_SSSE3
PRED4x4_DR ssse3
%ifdef HAVE_AVX
INIT_AVX
PRED4x4_DR avx
%endif

;-----------------------------------------------------------------------------
; void pred4x4_vertical_right(pixel *src, const pixel *topright, int stride)
;-----------------------------------------------------------------------------
%macro PRED4x4_VR 1
cglobal pred4x4_vertical_right_10_%1, 3,3,6
    sub     r0, r2
    lea     r1, [r0+r2*2]
    movq    m5, [r0]            ; ........t3t2t1t0
    movhps  m1, [r0-8]
    PALIGNR m0, m5, m1, 14, m1  ; ......t3t2t1t0lt
    pavgw   m5, m0
    movhps  m1, [r0+r2*1-8]
    PALIGNR m0, m1, 14, m1      ; ....t3t2t1t0ltl0
    mova    m1, m0
    movhps  m2, [r0+r2*2-8]
    PALIGNR m0, m2, 14, m2      ; ..t3t2t1t0ltl0l1
    mova    m2, m0
    movhps  m3, [r1+r2*1-8]
    PALIGNR m0, m3, 14, m3      ; t3t2t1t0ltl0l1l2
    PRED4x4_LOWPASS m3, m1, m0, m2
    pslldq  m1, m3, 12
    psrldq  m3, 4
    movq    [r0+r2*1], m5
    movq    [r0+r2*2], m3
    PALIGNR m5, m1, 14, m2
    pslldq  m1, 2
    movq    [r1+r2*1], m5
    PALIGNR m3, m1, 14, m1
    movq    [r1+r2*2], m3
    RET
%endmacro

INIT_XMM
%define PALIGNR PALIGNR_MMX
PRED4x4_VR sse2
%define PALIGNR PALIGNR_SSSE3
PRED4x4_VR ssse3
%ifdef HAVE_AVX
INIT_AVX
PRED4x4_VR avx
%endif

;-----------------------------------------------------------------------------
; void pred4x4_horizontal_down(pixel *src, const pixel *topright, int stride)
;-----------------------------------------------------------------------------
%macro PRED4x4_HD 1
cglobal pred4x4_horizontal_down_10_%1, 3,3
    sub        r0, r2
    lea        r1, [r0+r2*2]
    movq       m0, [r0-8]      ; lt ..
    movhps     m0, [r0]
    pslldq     m0, 2           ; t2 t1 t0 lt .. .. .. ..
    movq       m1, [r1+r2*2-8] ; l3
    movq       m3, [r1+r2*1-8]
    punpcklwd  m1, m3          ; l2 l3
    movq       m2, [r0+r2*2-8] ; l1
    movq       m3, [r0+r2*1-8]
    punpcklwd  m2, m3          ; l0 l1
    punpckhdq  m1, m2          ; l0 l1 l2 l3
    punpckhqdq m1, m0          ; t2 t1 t0 lt l0 l1 l2 l3
    psrldq     m0, m1, 4       ; .. .. t2 t1 t0 lt l0 l1
    psrldq     m2, m1, 2       ; .. t2 t1 t0 lt l0 l1 l2
    pavgw      m5, m1, m2
    PRED4x4_LOWPASS m3, m1, m0, m2
    punpcklwd  m5, m3
    psrldq     m3, 8
    PALIGNR    m3, m5, 12, m4
    movq       [r1+r2*2], m5
    movhps     [r0+r2*2], m5
    psrldq     m5, 4
    movq       [r1+r2*1], m5
    movq       [r0+r2*1], m3
    RET
%endmacro

INIT_XMM
%define PALIGNR PALIGNR_MMX
PRED4x4_HD sse2
%define PALIGNR PALIGNR_SSSE3
PRED4x4_HD ssse3
%ifdef HAVE_AVX
INIT_AVX
PRED4x4_HD avx
%endif

;-----------------------------------------------------------------------------
; void pred4x4_dc(pixel *src, const pixel *topright, int stride)
;-----------------------------------------------------------------------------
%macro HADDD 2 ; sum junk
%if mmsize == 16
    movhlps %2, %1
    paddd   %1, %2
    pshuflw %2, %1, 0xE
    paddd   %1, %2
%else
    pshufw  %2, %1, 0xE
    paddd   %1, %2
%endif
%endmacro

%macro HADDW 2
    pmaddwd %1, [pw_1]
    HADDD   %1, %2
%endmacro

INIT_MMX
cglobal pred4x4_dc_10_mmxext, 3,3
    sub    r0, r2
    lea    r1, [r0+r2*2]
    movq   m2, [r0+r2*1-8]
    paddw  m2, [r0+r2*2-8]
    paddw  m2, [r1+r2*1-8]
    paddw  m2, [r1+r2*2-8]
    psrlq  m2, 48
    movq   m0, [r0]
    HADDW  m0, m1
    paddw  m0, [pw_4]
    paddw  m0, m2
    psrlw  m0, 3
    SPLATW m0, m0, 0
    movq   [r0+r2*1], m0
    movq   [r0+r2*2], m0
    movq   [r1+r2*1], m0
    movq   [r1+r2*2], m0
    RET

;-----------------------------------------------------------------------------
; void pred4x4_down_left(pixel *src, const pixel *topright, int stride)
;-----------------------------------------------------------------------------
;TODO: more AVX here
%macro PRED4x4_DL 1
cglobal pred4x4_down_left_10_%1, 3,3
    sub        r0, r2
    movq       m1, [r0]
    movhps     m1, [r1]
    pslldq     m5, m1, 2
    pxor       m2, m5, m1
    psrldq     m2, 2
    pxor       m3, m1, m2
    PRED4x4_LOWPASS m0, m5, m3, m1
    lea        r1, [r0+r2*2]
    movhps     [r1+r2*2], m0
    psrldq     m0, 2
    movq       [r0+r2*1], m0
    psrldq     m0, 2
    movq       [r0+r2*2], m0
    psrldq     m0, 2
    movq       [r1+r2*1], m0
    RET
%endmacro

INIT_XMM
PRED4x4_DL sse2
%ifdef HAVE_AVX
INIT_AVX
PRED4x4_DL avx
%endif

;-----------------------------------------------------------------------------
; void pred4x4_vertical_left(pixel *src, const pixel *topright, int stride)
;-----------------------------------------------------------------------------
%macro PRED4x4_VL 1
cglobal pred4x4_vertical_left_10_%1, 3,3
    sub        r0, r2
    movu       m1, [r0]
    movhps     m1, [r1]
    psrldq     m3, m1, 2
    psrldq     m2, m1, 4
    pavgw      m4, m3, m1
    PRED4x4_LOWPASS m0, m1, m2, m3
    lea        r1, [r0+r2*2]
    movq       [r0+r2*1], m4
    movq       [r0+r2*2], m0
    psrldq     m4, 2
    psrldq     m0, 2
    movq       [r1+r2*1], m4
    movq       [r1+r2*2], m0
    RET
%endmacro

INIT_XMM
PRED4x4_VL sse2
%ifdef HAVE_AVX
INIT_AVX
PRED4x4_VL avx
%endif

;-----------------------------------------------------------------------------
; void pred4x4_horizontal_up(pixel *src, const pixel *topright, int stride)
;-----------------------------------------------------------------------------
INIT_MMX
cglobal pred4x4_horizontal_up_10_mmxext, 3,3
    sub       r0, r2
    lea       r1, [r0+r2*2]
    movq      m0, [r0+r2*1-8]
    punpckhwd m0, [r0+r2*2-8]
    movq      m1, [r1+r2*1-8]
    punpckhwd m1, [r1+r2*2-8]
    punpckhdq m0, m1
    pshufw    m1, m1, 0xFF
    movq      [r1+r2*2], m1
    movd      [r1+r2*1+4], m1
    pshufw    m2, m0, 11111001b
    movq      m1, m2
    pavgw     m2, m0

    pshufw    m5, m0, 11111110b
    PRED4x4_LOWPASS m3, m0, m5, m1
    movq      m6, m2
    punpcklwd m6, m3
    movq      [r0+r2*1], m6
    psrlq     m2, 16
    psrlq     m3, 16
    punpcklwd m2, m3
    movq      [r0+r2*2], m2
    psrlq     m2, 32
    movd      [r1+r2*1], m2
    RET



;-----------------------------------------------------------------------------
; void pred8x8_vertical(pixel *src, int stride)
;-----------------------------------------------------------------------------
INIT_XMM
cglobal pred8x8_vertical_10_sse2, 2,2
    sub  r0, r1
    mova m0, [r0]
%rep 3
    mova [r0+r1*1], m0
    mova [r0+r1*2], m0
    lea  r0, [r0+r1*2]
%endrep
    mova [r0+r1*1], m0
    mova [r0+r1*2], m0
    RET

;-----------------------------------------------------------------------------
; void pred8x8_horizontal(pixel *src, int stride)
;-----------------------------------------------------------------------------
INIT_XMM
cglobal pred8x8_horizontal_10_sse2, 2,3
    mov          r2, 4
.loop:
    movq         m0, [r0+r1*0-8]
    movq         m1, [r0+r1*1-8]
    pshuflw      m0, m0, 0xff
    pshuflw      m1, m1, 0xff
    punpcklqdq   m0, m0
    punpcklqdq   m1, m1
    mova  [r0+r1*0], m0
    mova  [r0+r1*1], m1
    lea          r0, [r0+r1*2]
    dec          r2
    jg .loop
    REP_RET

;-----------------------------------------------------------------------------
; void predict_8x8_dc(pixel *src, int stride)
;-----------------------------------------------------------------------------
%macro MOV8 2-3
; sort of a hack, but it works
%if mmsize==8
    movq    [%1+0], %2
    movq    [%1+8], %3
%else
    movdqa    [%1], %2
%endif
%endmacro

%macro PRED8x8_DC 2
cglobal pred8x8_dc_10_%1, 2,4
%ifdef ARCH_X86_64
%define t0 r10
%else
%define t0 r0m
%endif
    sub         r0, r1
    pxor        m4, m4
    movq        m0, [r0+0]
    movq        m1, [r0+8]
    HADDW       m0, m2
    mov         t0, r0
    HADDW       m1, m2

    movzx      r2d, word [r0+r1*1-2]
    movzx      r3d, word [r0+r1*2-2]
    lea         r0, [r0+r1*2]
    add        r2d, r3d
    movzx      r3d, word [r0+r1*1-2]
    add        r2d, r3d
    movzx      r3d, word [r0+r1*2-2]
    add        r2d, r3d
    lea         r0, [r0+r1*2]
    movd        m2, r2d            ; s2

    movzx      r2d, word [r0+r1*1-2]
    movzx      r3d, word [r0+r1*2-2]
    lea         r0, [r0+r1*2]
    add        r2d, r3d
    movzx      r3d, word [r0+r1*1-2]
    add        r2d, r3d
    movzx      r3d, word [r0+r1*2-2]
    add        r2d, r3d
    movd        m3, r2d            ; s3

    punpcklwd   m0, m1
    mov         r0, t0
    punpcklwd   m2, m3
    punpckldq   m0, m2            ; s0, s1, s2, s3
    %2          m3, m0, 11110110b ; s2, s1, s3, s3
    lea         r2, [r1+r1*2]
    %2          m0, m0, 01110100b ; s0, s1, s3, s1
    paddw       m0, m3
    lea         r3, [r0+r1*4]
    psrlw       m0, 2
    pavgw       m0, m4            ; s0+s2, s1, s3, s1+s3
%ifidn %1, sse2
    punpcklwd   m0, m0
    pshufd      m3, m0, 11111010b
    punpckldq   m0, m0
    SWAP         0,1
%else
    pshufw      m1, m0, 0x00
    pshufw      m2, m0, 0x55
    pshufw      m3, m0, 0xaa
    pshufw      m4, m0, 0xff
%endif
    MOV8   r0+r1*1, m1, m2
    MOV8   r0+r1*2, m1, m2
    MOV8   r0+r2*1, m1, m2
    MOV8   r0+r1*4, m1, m2
    MOV8   r3+r1*1, m3, m4
    MOV8   r3+r1*2, m3, m4
    MOV8   r3+r2*1, m3, m4
    MOV8   r3+r1*4, m3, m4
    RET
%endmacro

INIT_MMX
PRED8x8_DC mmxext, pshufw
INIT_XMM
PRED8x8_DC sse2  , pshuflw

;-----------------------------------------------------------------------------
; void pred8x8_top_dc(pixel *src, int stride)
;-----------------------------------------------------------------------------
%macro PRED8x8_TOP_DC 2
cglobal pred8x8_top_dc_10_%1, 2,4
    sub         r0, r1
    movq        m0, [r0+0]
    movq        m1, [r0+8]
    HADDW       m0, m2
    HADDW       m1, m3
    lea         r2, [r1+r1*2]
    paddw       m0, [pw_2]
    paddw       m1, [pw_2]
    lea         r3, [r0+r1*4]
    psrlw       m0, 2
    psrlw       m1, 2
    %2          m0, m0, 0
    %2          m1, m1, 0
%ifidn %1, sse2
    punpcklqdq  m0, m1
%endif
    MOV8   r0+r1*1, m0, m1
    MOV8   r0+r1*2, m0, m1
    MOV8   r0+r2*1, m0, m1
    MOV8   r0+r1*4, m0, m1
    MOV8   r3+r1*1, m0, m1
    MOV8   r3+r1*2, m0, m1
    MOV8   r3+r2*1, m0, m1
    MOV8   r3+r1*4, m0, m1
    RET
%endmacro

INIT_MMX
PRED8x8_TOP_DC mmxext, pshufw
INIT_XMM
PRED8x8_TOP_DC sse2  , pshuflw

;-----------------------------------------------------------------------------
; void pred8x8_plane(pixel *src, int stride)
;-----------------------------------------------------------------------------
INIT_XMM
cglobal pred8x8_plane_10_sse2, 2,7,7
    sub       r0, r1
    lea       r2, [r1+r1*2]
    lea       r3, [r0+r1*4]
    mova      m2, [r0]
    pmaddwd   m2, [pw_m32101234]
    HADDD     m2, m1
    movd      m0, [r0-4]
    psrld     m0, 14
    psubw     m2, m0               ; H
    movd      m0, [r3+r1*4-4]
    movd      m1, [r0+12]
    paddw     m0, m1
    psllw     m0, 4                ; 16*(src[7*stride-1] + src[-stride+7])
    movzx    r4d, word [r3+r1*1-2] ; src[4*stride-1]
    movzx    r5d, word [r0+r2*1-2] ; src[2*stride-1]
    sub      r4d, r5d
    movzx    r6d, word [r3+r1*2-2] ; src[5*stride-1]
    movzx    r5d, word [r0+r1*2-2] ; src[1*stride-1]
    sub      r6d, r5d
    lea      r4d, [r4+r6*2]
    movzx    r5d, word [r3+r2*1-2] ; src[6*stride-1]
    movzx    r6d, word [r0+r1*1-2] ; src[0*stride-1]
    sub      r5d, r6d
    lea      r5d, [r5+r5*2]
    add      r4d, r5d
    movzx    r6d, word [r3+r1*4-2] ; src[7*stride-1]
    movzx    r5d, word [r0+r1*0-2] ; src[ -stride-1]
    sub      r6d, r5d
    lea      r4d, [r4+r6*4]
    movd      m3, r4d              ; V
    punpckldq m2, m3
    pmaddwd   m2, [pd_17]
    paddd     m2, [pd_16]
    psrad     m2, 5                ; b, c

    mova      m3, [pw_pixel_max]
    pxor      m1, m1
    SPLATW    m0, m0, 1
    SPLATW    m4, m2, 2
    SPLATW    m2, m2, 0
    pmullw    m2, [pw_m32101234]   ; b
    pmullw    m5, m4, [pw_m3]      ; c
    paddw     m5, [pw_16]
    mov      r2d, 8
    add       r0, r1
.loop:
    paddsw    m6, m2, m5
    paddsw    m6, m0
    psraw     m6, 5
    CLIPW     m6, m1, m3
    mova    [r0], m6
    paddw     m5, m4
    add       r0, r1
    dec r2d
    jg .loop
    REP_RET


;-----------------------------------------------------------------------------
; void pred8x8l_128_dc(pixel *src, int has_topleft, int has_topright, int stride)
;-----------------------------------------------------------------------------
%macro PRED8x8L_128_DC 1
cglobal pred8x8l_128_dc_10_%1, 4,4
    mova      m0, [pw_512]
    lea       r1, [r3+r3*2]
    lea       r2, [r0+r3*4]
    MOV8 r0+r3*0, m0, m0
    MOV8 r0+r3*1, m0, m0
    MOV8 r0+r3*2, m0, m0
    MOV8 r0+r1*1, m0, m0
    MOV8 r2+r3*0, m0, m0
    MOV8 r2+r3*1, m0, m0
    MOV8 r2+r3*2, m0, m0
    MOV8 r2+r1*1, m0, m0
    RET
%endmacro

INIT_MMX
PRED8x8L_128_DC mmxext
INIT_XMM
PRED8x8L_128_DC sse2

;-----------------------------------------------------------------------------
; void pred8x8l_top_dc(pixel *src, int has_topleft, int has_topright, int stride)
;-----------------------------------------------------------------------------
%macro PRED8x8L_TOP_DC 1
cglobal pred8x8l_top_dc_10_%1, 4,4,6
    sub         r0, r3
    pxor        m7, m7
    mova        m0, [r0-16]
    mova        m3, [r0]
    mova        m1, [r0+16]
    mova        m2, m3
    mova        m4, m3
    PALIGNR     m2, m0, 14, m0
    PALIGNR     m1, m4,  2, m4
    test        r1, r1 ; top_left
    jz .fix_lt_2
    test        r2, r2 ; top_right
    jz .fix_tr_1
    jmp .body
.fix_lt_2:
    mova        m5, m3
    pxor        m5, m2
    pslldq      m5, 14
    psrldq      m5, 14
    pxor        m2, m5
    test        r2, r2 ; top_right
    jnz .body
.fix_tr_1:
    mova        m5, m3
    pxor        m5, m1
    psrldq      m5, 14
    pslldq      m5, 14
    pxor        m1, m5
.body
    lea         r1, [r3+r3*2]
    lea         r2, [r0+r3*4]
    PRED4x4_LOWPASS m0, m2, m1, m3
    HADDW       m0, m1
    paddw       m0, [pw_4]
    psrlw       m0, 3
    SPLATW      m0, m0, 0
    mova [r0+r3*1], m0
    mova [r0+r3*2], m0
    mova [r0+r1*1], m0
    mova [r0+r3*4], m0
    mova [r2+r3*1], m0
    mova [r2+r3*2], m0
    mova [r2+r1*1], m0
    mova [r2+r3*4], m0
    RET
%endmacro

INIT_XMM
%define PALIGNR PALIGNR_MMX
PRED8x8L_TOP_DC sse2
%define PALIGNR PALIGNR_SSSE3
PRED8x8L_TOP_DC ssse3

;-----------------------------------------------------------------------------
;void pred8x8l_dc(pixel *src, int has_topleft, int has_topright, int stride)
;-----------------------------------------------------------------------------
;TODO: see if scalar is faster
%macro PRED8x8L_DC 1
cglobal pred8x8l_dc_10_%1, 4,5,8
    sub         r0, r3
    lea         r4, [r0+r3*2]
    mova        m0, [r0+r3*1-16]
    punpckhwd   m0, [r0+r3*0-16]
    mova        m1, [r4+r3*1-16]
    punpckhwd   m1, [r0+r3*2-16]
    mov         r4, r0
    punpckhdq   m1, m0
    lea         r0, [r0+r3*4]
    mova        m2, [r0+r3*1-16]
    punpckhwd   m2, [r0+r3*0-16]
    lea         r0, [r0+r3*2]
    mova        m3, [r0+r3*1-16]
    punpckhwd   m3, [r0+r3*0-16]
    punpckhdq   m3, m2
    punpckhqdq  m3, m1
    lea         r0, [r0+r3*2]
    mova        m0, [r0+r3*0-16]
    mova        m1, [r4]
    mov         r0, r4
    mova        m4, m3
    mova        m2, m3
    PALIGNR     m4, m0, 14, m0
    PALIGNR     m1, m2,  2, m2
    test        r1, r1
    jnz .do_left
.fix_lt_1:
    mova        m5, m3
    pxor        m5, m4
    psrldq      m5, 14
    pslldq      m5, 12
    pxor        m1, m5
    jmp .do_left
.fix_lt_2:
    mova        m5, m3
    pxor        m5, m2
    pslldq      m5, 14
    psrldq      m5, 14
    pxor        m2, m5
    test        r2, r2
    jnz .body
.fix_tr_1:
    mova        m5, m3
    pxor        m5, m1
    psrldq      m5, 14
    pslldq      m5, 14
    pxor        m1, m5
    jmp .body
.do_left:
    mova        m0, m4
    PRED4x4_LOWPASS m2, m1, m4, m3
    mova        m4, m0
    mova        m7, m2
    PRED4x4_LOWPASS m1, m3, m0, m4
    pslldq      m1, 14
    PALIGNR     m7, m1, 14, m3
    mova        m0, [r0-16]
    mova        m3, [r0]
    mova        m1, [r0+16]
    mova        m2, m3
    mova        m4, m3
    PALIGNR     m2, m0, 14, m0
    PALIGNR     m1, m4,  2, m4
    test        r1, r1
    jz .fix_lt_2
    test        r2, r2
    jz .fix_tr_1
.body
    lea         r1, [r3+r3*2]
    PRED4x4_LOWPASS m6, m2, m1, m3
    HADDW       m7, m0
    HADDW       m6, m0
    lea         r2, [r0+r3*4]
    paddw       m7, [pw_8]
    paddw       m7, m6
    psrlw       m7, 4
    SPLATW      m7, m7
    mova [r0+r3*1], m7
    mova [r0+r3*2], m7
    mova [r0+r1*1], m7
    mova [r0+r3*4], m7
    mova [r2+r3*1], m7
    mova [r2+r3*2], m7
    mova [r2+r1*1], m7
    mova [r2+r3*4], m7
    RET
%endmacro

INIT_XMM
%define PALIGNR PALIGNR_MMX
PRED8x8L_DC sse2
%define PALIGNR PALIGNR_SSSE3
PRED8x8L_DC ssse3

;-----------------------------------------------------------------------------
; void pred8x8l_vertical(pixel *src, int has_topleft, int has_topright, int stride)
;-----------------------------------------------------------------------------
%macro PRED8x8L_VERTICAL 1
cglobal pred8x8l_vertical_10_%1, 4,4,6
    sub         r0, r3
    mova        m0, [r0-16]
    mova        m3, [r0]
    mova        m1, [r0+16]
    mova        m2, m3
    mova        m4, m3
    PALIGNR     m2, m0, 14, m0
    PALIGNR     m1, m4,  2, m4
    test        r1, r1 ; top_left
    jz .fix_lt_2
    test        r2, r2 ; top_right
    jz .fix_tr_1
    jmp .body
.fix_lt_2:
    mova        m5, m3
    pxor        m5, m2
    pslldq      m5, 14
    psrldq      m5, 14
    pxor        m2, m5
    test        r2, r2 ; top_right
    jnz .body
.fix_tr_1:
    mova        m5, m3
    pxor        m5, m1
    psrldq      m5, 14
    pslldq      m5, 14
    pxor        m1, m5
.body
    lea         r1, [r3+r3*2]
    lea         r2, [r0+r3*4]
    PRED4x4_LOWPASS m0, m2, m1, m3
    mova [r0+r3*1], m0
    mova [r0+r3*2], m0
    mova [r0+r1*1], m0
    mova [r0+r3*4], m0
    mova [r2+r3*1], m0
    mova [r2+r3*2], m0
    mova [r2+r1*1], m0
    mova [r2+r3*4], m0
    RET
%endmacro

INIT_XMM
%define PALIGNR PALIGNR_MMX
PRED8x8L_VERTICAL sse2
%define PALIGNR PALIGNR_SSSE3
PRED8x8L_VERTICAL ssse3

;-----------------------------------------------------------------------------
; void pred8x8l_horizontal(uint8_t *src, int has_topleft, int has_topright, int stride)
;-----------------------------------------------------------------------------
%macro PRED8x8L_HORIZONTAL 1
cglobal pred8x8l_horizontal_10_%1, 4,4,8
    sub         r0, r3
    lea         r2, [r0+r3*2]
    mova        m0, [r0+r3*1-16]
    test        r1, r1
    lea         r1, [r0+r3]
    cmovnz      r1, r0
    punpckhwd   m0, [r1+r3*0-16]
    mova        m1, [r2+r3*1-16]
    punpckhwd   m1, [r0+r3*2-16]
    mov         r2, r0
    punpckhdq   m1, m0
    lea         r0, [r0+r3*4]
    mova        m2, [r0+r3*1-16]
    punpckhwd   m2, [r0+r3*0-16]
    lea         r0, [r0+r3*2]
    mova        m3, [r0+r3*1-16]
    punpckhwd   m3, [r0+r3*0-16]
    punpckhdq   m3, m2
    punpckhqdq  m3, m1
    lea         r0, [r0+r3*2]
    mova        m0, [r0+r3*0-16]
    mova        m1, [r1+r3*0-16]
    mov         r0, r2
    mova        m4, m3
    mova        m2, m3
    PALIGNR     m4, m0, 14, m0
    PALIGNR     m1, m2,  2, m2
    mova        m0, m4
    PRED4x4_LOWPASS m2, m1, m4, m3
    mova        m4, m0
    mova        m7, m2
    PRED4x4_LOWPASS m1, m3, m0, m4
    pslldq      m1, 14
    PALIGNR     m7, m1, 14, m3
    lea         r1, [r3+r3*2]
    punpckhwd   m3, m7, m7
    punpcklwd   m7, m7
    pshufd      m0, m3, 0xff
    pshufd      m1, m3, 0xaa
    lea         r2, [r0+r3*4]
    pshufd      m2, m3, 0x55
    pshufd      m3, m3, 0x00
    pshufd      m4, m7, 0xff
    pshufd      m5, m7, 0xaa
    pshufd      m6, m7, 0x55
    pshufd      m7, m7, 0x00
    mova [r0+r3*1], m0
    mova [r0+r3*2], m1
    mova [r0+r1*1], m2
    mova [r0+r3*4], m3
    mova [r2+r3*1], m4
    mova [r2+r3*2], m5
    mova [r2+r1*1], m6
    mova [r2+r3*4], m7
    RET
%endmacro

INIT_XMM
%define PALIGNR PALIGNR_MMX
PRED8x8L_HORIZONTAL sse2
%define PALIGNR PALIGNR_SSSE3
PRED8x8L_HORIZONTAL ssse3

;-----------------------------------------------------------------------------
;void pred8x8l_down_left(pixel *src, int has_topleft, int has_topright, int stride)
;-----------------------------------------------------------------------------
%macro PRED8x8L_DOWN_LEFT 1
cglobal pred8x8l_down_left_10_%1, 4,4,8
    sub         r0, r3
    mova        m0, [r0-16]
    mova        m3, [r0]
    mova        m1, [r0+16]
    mova        m2, m3
    mova        m4, m3
    PALIGNR     m2, m0, 14, m0
    PALIGNR     m1, m4,  2, m4
    test        r1, r1
    jz .fix_lt_2
    test        r2, r2
    jz .fix_tr_1
    jmp .do_top
.fix_lt_2:
    mova        m5, m3
    pxor        m5, m2
    pslldq      m5, 14
    psrldq      m5, 14
    pxor        m2, m5
    test        r2, r2
    jnz .do_top
.fix_tr_1:
    mova        m5, m3
    pxor        m5, m1
    psrldq      m5, 14
    pslldq      m5, 14
    pxor        m1, m5
    jmp .do_top
.fix_tr_2:
    punpckhwd   m3, m3
    pshufd      m1, m3, 0xFF
    jmp .do_topright
.do_top:
    PRED4x4_LOWPASS m4, m2, m1, m3
    mova        m7, m4
    test        r2, r2
    jz .fix_tr_2
    mova        m0, [r0+16]
    mova        m5, m0
    mova        m2, m0
    mova        m4, m0
    psrldq      m5, 14
    PALIGNR     m2, m3, 14, m3
    PALIGNR     m5, m4,  2, m4
    PRED4x4_LOWPASS m1, m2, m5, m0
.do_topright:
    lea         r1, [r3+r3*2]
    mova        m6, m1
    psrldq      m1, 14
    mova        m4, m1
    lea         r2, [r0+r3*4]
    mova        m2, m6
    PALIGNR     m2, m7,  2, m0
    mova        m3, m6
    PALIGNR     m3, m7, 14, m0
    PALIGNR     m4, m6,  2, m0
    mova        m5, m7
    mova        m1, m7
    mova        m7, m6
    pslldq      m1, 2
    PRED4x4_LOWPASS m0, m1, m2, m5
    PRED4x4_LOWPASS m1, m3, m4, m7
    mova [r2+r3*4], m1
    mova        m2, m0
    pslldq      m1, 2
    psrldq      m2, 14
    pslldq      m0, 2
    por         m1, m2
    mova [r2+r1*1], m1
    mova        m2, m0
    pslldq      m1, 2
    psrldq      m2, 14
    pslldq      m0, 2
    por         m1, m2
    mova [r2+r3*2], m1
    mova        m2, m0
    pslldq      m1, 2
    psrldq      m2, 14
    pslldq      m0, 2
    por         m1, m2
    mova [r2+r3*1], m1
    mova        m2, m0
    pslldq      m1, 2
    psrldq      m2, 14
    pslldq      m0, 2
    por         m1, m2
    mova [r0+r3*4], m1
    mova        m2, m0
    pslldq      m1, 2
    psrldq      m2, 14
    pslldq      m0, 2
    por         m1, m2
    mova [r0+r1*1], m1
    mova        m2, m0
    pslldq      m1, 2
    psrldq      m2, 14
    pslldq      m0, 2
    por         m1, m2
    mova [r0+r3*2], m1
    pslldq      m1, 2
    psrldq      m0, 14
    por         m1, m0
    mova [r0+r3*1], m1
    RET
%endmacro

INIT_XMM
%define PALIGNR PALIGNR_MMX
PRED8x8L_DOWN_LEFT sse2
%define PALIGNR PALIGNR_SSSE3
PRED8x8L_DOWN_LEFT ssse3

;-----------------------------------------------------------------------------
;void pred8x8l_down_right_mxext(pixel *src, int has_topleft, int has_topright, int stride)
;-----------------------------------------------------------------------------
%macro PRED8x8L_DOWN_RIGHT 1
cglobal pred8x8l_down_right_10_%1, 4,5,8
    sub         r0, r3
    lea         r4, [r0+r3*2]
    mova        m0, [r0+r3*1-16]
    punpckhwd   m0, [r0+r3*0-16]
    mova        m1, [r4+r3*1-16]
    punpckhwd   m1, [r0+r3*2-16]
    mov         r4, r0
    punpckhdq   m1, m0
    lea         r0, [r0+r3*4]
    mova        m2, [r0+r3*1-16]
    punpckhwd   m2, [r0+r3*0-16]
    lea         r0, [r0+r3*2]
    mova        m3, [r0+r3*1-16]
    punpckhwd   m3, [r0+r3*0-16]
    punpckhdq   m3, m2
    punpckhqdq  m3, m1
    lea         r0, [r0+r3*2]
    mova        m0, [r0+r3*0-16]
    mova        m1, [r4]
    mov         r0, r4
    mova        m4, m3
    mova        m2, m3
    PALIGNR     m4, m0, 14, m0
    PALIGNR     m1, m2,  2, m2
    test        r1, r1 ; top_left
    jz .fix_lt_1
.do_left:
    mova        m0, m4
    PRED4x4_LOWPASS m2, m1, m4, m3
    mova        m4, m0
    mova        m7, m2
    mova        m6, m2
    PRED4x4_LOWPASS m1, m3, m0, m4
    pslldq      m1, 14
    PALIGNR     m7, m1, 14, m3
    mova        m0, [r0-16]
    mova        m3, [r0]
    mova        m1, [r0+16]
    mova        m2, m3
    mova        m4, m3
    PALIGNR     m2, m0, 14, m0
    PALIGNR     m1, m4,  2, m4
    test        r1, r1 ; top_left
    jz .fix_lt_2
    test        r2, r2 ; top_right
    jz .fix_tr_1
.do_top:
    PRED4x4_LOWPASS m4, m2, m1, m3
    mova        m5, m4
    jmp .body
.fix_lt_1:
    mova        m5, m3
    pxor        m5, m4
    psrldq      m5, 14
    pslldq      m5, 12
    pxor        m1, m5
    jmp .do_left
.fix_lt_2:
    mova        m5, m3
    pxor        m5, m2
    pslldq      m5, 14
    psrldq      m5, 14
    pxor        m2, m5
    test        r2, r2 ; top_right
    jnz .do_top
.fix_tr_1:
    mova        m5, m3
    pxor        m5, m1
    psrldq      m5, 14
    pslldq      m5, 14
    pxor        m1, m5
    jmp .do_top
.body
    lea         r1, [r3+r3*2]
    mova        m1, m7
    mova        m7, m5
    mova        m5, m6
    mova        m2, m7
    lea         r2, [r0+r3*4]
    PALIGNR     m2, m6,  2, m0
    mova        m3, m7
    PALIGNR     m3, m6, 14, m0
    mova        m4, m7
    psrldq      m4, 2
    PRED4x4_LOWPASS m0, m1, m2, m5
    PRED4x4_LOWPASS m1, m3, m4, m7
    mova [r2+r3*4], m0
    mova        m2, m1
    psrldq      m0, 2
    pslldq      m2, 14
    psrldq      m1, 2
    por         m0, m2
    mova [r2+r1*1], m0
    mova        m2, m1
    psrldq      m0, 2
    pslldq      m2, 14
    psrldq      m1, 2
    por         m0, m2
    mova [r2+r3*2], m0
    mova        m2, m1
    psrldq      m0, 2
    pslldq      m2, 14
    psrldq      m1, 2
    por         m0, m2
    mova [r2+r3*1], m0
    mova        m2, m1
    psrldq      m0, 2
    pslldq      m2, 14
    psrldq      m1, 2
    por         m0, m2
    mova [r0+r3*4], m0
    mova        m2, m1
    psrldq      m0, 2
    pslldq      m2, 14
    psrldq      m1, 2
    por         m0, m2
    mova [r0+r1*1], m0
    mova        m2, m1
    psrldq      m0, 2
    pslldq      m2, 14
    psrldq      m1, 2
    por         m0, m2
    mova [r0+r3*2], m0
    psrldq      m0, 2
    pslldq      m1, 14
    por         m0, m1
    mova [r0+r3*1], m0
    RET
%endmacro

INIT_XMM
%define PALIGNR PALIGNR_MMX
PRED8x8L_DOWN_RIGHT sse2
%define PALIGNR PALIGNR_SSSE3
PRED8x8L_DOWN_RIGHT ssse3

;-----------------------------------------------------------------------------
; void pred8x8l_vertical_right(pixel *src, int has_topleft, int has_topright, int stride)
;-----------------------------------------------------------------------------
%macro PRED8x8L_VERTICAL_RIGHT 1
cglobal pred8x8l_vertical_right_10_%1, 4,5,8
    sub         r0, r3
    lea         r4, [r0+r3*2]
    mova        m0, [r0+r3*1-16]
    punpckhwd   m0, [r0+r3*0-16]
    mova        m1, [r4+r3*1-16]
    punpckhwd   m1, [r0+r3*2-16]
    mov         r4, r0
    punpckhdq   m1, m0
    lea         r0, [r0+r3*4]
    mova        m2, [r0+r3*1-16]
    punpckhwd   m2, [r0+r3*0-16]
    lea         r0, [r0+r3*2]
    mova        m3, [r0+r3*1-16]
    punpckhwd   m3, [r0+r3*0-16]
    punpckhdq   m3, m2
    punpckhqdq  m3, m1
    lea         r0, [r0+r3*2]
    mova        m0, [r0+r3*0-16]
    mova        m1, [r4]
    mov         r0, r4
    mova        m4, m3
    mova        m2, m3
    PALIGNR     m4, m0, 14, m0
    PALIGNR     m1, m2,  2, m2
    test        r1, r1
    jz .fix_lt_1
    jmp .do_left
.fix_lt_1:
    mova        m5, m3
    pxor        m5, m4
    psrldq      m5, 14
    pslldq      m5, 12
    pxor        m1, m5
    jmp .do_left
.fix_lt_2:
    mova        m5, m3
    pxor        m5, m2
    pslldq      m5, 14
    psrldq      m5, 14
    pxor        m2, m5
    test        r2, r2
    jnz .do_top
.fix_tr_1:
    mova        m5, m3
    pxor        m5, m1
    psrldq      m5, 14
    pslldq      m5, 14
    pxor        m1, m5
    jmp .do_top
.do_left:
    mova        m0, m4
    PRED4x4_LOWPASS m2, m1, m4, m3
    mova        m7, m2
    mova        m0, [r0-16]
    mova        m3, [r0]
    mova        m1, [r0+16]
    mova        m2, m3
    mova        m4, m3
    PALIGNR     m2, m0, 14, m0
    PALIGNR     m1, m4,  2, m4
    test        r1, r1
    jz .fix_lt_2
    test        r2, r2
    jz .fix_tr_1
.do_top
    PRED4x4_LOWPASS m6, m2, m1, m3
    lea         r1, [r3+r3*2]
    mova        m2, m6
    mova        m3, m6
    PALIGNR     m3, m7, 14, m0
    PALIGNR     m6, m7, 12, m1
    mova        m4, m3
    pavgw       m3, m2
    lea         r2, [r0+r3*4]
    PRED4x4_LOWPASS m0, m6, m2, m4
    mova [r0+r3*1], m3
    mova [r0+r3*2], m0
    mova        m5, m0
    mova        m6, m3
    mova        m1, m7
    mova        m2, m1
    pslldq      m2, 2
    mova        m3, m1
    pslldq      m3, 4
    PRED4x4_LOWPASS m0, m1, m3, m2
    PALIGNR     m6, m0, 14, m2
    mova [r0+r1*1], m6
    pslldq      m0, 2
    PALIGNR     m5, m0, 14, m1
    mova [r0+r3*4], m5
    pslldq      m0, 2
    PALIGNR     m6, m0, 14, m2
    mova [r2+r3*1], m6
    pslldq      m0, 2
    PALIGNR     m5, m0, 14, m1
    mova [r2+r3*2], m5
    pslldq      m0, 2
    PALIGNR     m6, m0, 14, m2
    mova [r2+r1*1], m6
    pslldq      m0, 2
    PALIGNR     m5, m0, 14, m1
    mova [r2+r3*4], m5
    RET
%endmacro

INIT_XMM
%define PALIGNR PALIGNR_MMX
PRED8x8L_VERTICAL_RIGHT sse2
%define PALIGNR PALIGNR_SSSE3
PRED8x8L_VERTICAL_RIGHT ssse3

;-----------------------------------------------------------------------------
; void pred8x8l_horizontal_up(pixel *src, int has_topleft, int has_topright, int stride)
;-----------------------------------------------------------------------------
%macro PRED8x8L_HORIZONTAL_UP 1
cglobal pred8x8l_horizontal_up_10_%1, 4,4,8
    sub         r0, r3
    lea         r2, [r0+r3*2]
    mova        m0, [r0+r3*1-16]
    test        r1, r1
    lea         r1, [r0+r3]
    cmovnz      r1, r0
    punpckhwd   m0, [r1+r3*0-16]
    mova        m1, [r2+r3*1-16]
    punpckhwd   m1, [r0+r3*2-16]
    mov         r2, r0
    punpckhdq   m1, m0
    lea         r0, [r0+r3*4]
    mova        m2, [r0+r3*1-16]
    punpckhwd   m2, [r0+r3*0-16]
    lea         r0, [r0+r3*2]
    mova        m3, [r0+r3*1-16]
    punpckhwd   m3, [r0+r3*0-16]
    punpckhdq   m3, m2
    punpckhqdq  m3, m1
    lea         r0, [r0+r3*2]
    mova        m0, [r0+r3*0-16]
    mova        m1, [r1+r3*0-16]
    mov         r0, r2
    mova        m4, m3
    mova        m2, m3
    PALIGNR     m4, m0, 14, m0
    PALIGNR     m1, m2,  2, m2
    mova        m0, m4
    PRED4x4_LOWPASS m2, m1, m4, m3
    mova        m4, m0
    mova        m7, m2
    PRED4x4_LOWPASS m1, m3, m0, m4
    pslldq      m1, 14
    PALIGNR     m7, m1, 14, m3
    lea         r1, [r3+r3*2]
    pshufd      m0, m7, 00011011b ; l6 l7 l4 l5 l2 l3 l0 l1
    pslldq      m7, 14             ; l7 .. .. .. .. .. .. ..
    mova        m2, m0
    pslld       m0, 16
    psrld       m2, 16
    por         m2, m0            ; l7 l6 l5 l4 l3 l2 l1 l0
    mova        m3, m2
    mova        m4, m2
    mova        m5, m2
    psrldq      m2, 2
    psrldq      m3, 4
    lea         r2, [r0+r3*4]
    por         m2, m7            ; l7 l7 l6 l5 l4 l3 l2 l1
    punpckhwd   m7, m7
    por         m3, m7            ; l7 l7 l7 l6 l5 l4 l3 l2
    pavgw       m4, m2
    PRED4x4_LOWPASS m1, m3, m5, m2
    mova        m5, m4
    punpcklwd   m4, m1            ; p4 p3 p2 p1
    punpckhwd   m5, m1            ; p8 p7 p6 p5
    mova        m6, m5
    mova        m7, m5
    mova        m0, m5
    PALIGNR     m5, m4, 4, m1
    pshufd      m1, m6, 11111001b
    PALIGNR     m6, m4, 8, m2
    pshufd      m2, m7, 11111110b
    PALIGNR     m7, m4, 12, m3
    pshufd      m3, m0, 11111111b
    mova [r0+r3*1], m4
    mova [r0+r3*2], m5
    mova [r0+r1*1], m6
    mova [r0+r3*4], m7
    mova [r2+r3*1], m0
    mova [r2+r3*2], m1
    mova [r2+r1*1], m2
    mova [r2+r3*4], m3
    RET
%endmacro

INIT_XMM
%define PALIGNR PALIGNR_MMX
PRED8x8L_HORIZONTAL_UP sse2
%define PALIGNR PALIGNR_SSSE3
PRED8x8L_HORIZONTAL_UP ssse3



;-----------------------------------------------------------------------------
; void pred16x16_vertical(pixel *src, int stride)
;-----------------------------------------------------------------------------
%macro MOV16 3-5
    mova [%1+     0], %2
    mova [%1+mmsize], %3
%if mmsize==8
    mova [%1+    16], %4
    mova [%1+    24], %5
%endif
%endmacro

%macro PRED16x16_VERTICAL 1
cglobal pred16x16_vertical_10_%1, 2,3
    sub   r0, r1
    mov   r2, 8
    mova  m0, [r0+ 0]
    mova  m1, [r0+mmsize]
%if mmsize==8
    mova  m2, [r0+16]
    mova  m3, [r0+24]
%endif
.loop:
    MOV16 r0+r1*1, m0, m1, m2, m3
    MOV16 r0+r1*2, m0, m1, m2, m3
    lea   r0, [r0+r1*2]
    dec   r2
    jg .loop
    REP_RET
%endmacro

INIT_MMX
PRED16x16_VERTICAL mmxext
INIT_XMM
PRED16x16_VERTICAL sse2

;-----------------------------------------------------------------------------
; void pred16x16_horizontal(pixel *src, int stride)
;-----------------------------------------------------------------------------
%macro PRED16x16_HORIZONTAL 1
cglobal pred16x16_horizontal_10_%1, 2,3
    mov    r2, 8
.vloop:
    movd   m0, [r0+r1*0-4]
    movd   m1, [r0+r1*1-4]
    SPLATW m0, m0, 1
    SPLATW m1, m1, 1
    MOV16  r0+r1*0, m0, m0, m0, m0
    MOV16  r0+r1*1, m1, m1, m1, m1
    lea    r0, [r0+r1*2]
    dec    r2
    jg .vloop
    REP_RET
%endmacro

INIT_MMX
PRED16x16_HORIZONTAL mmxext
INIT_XMM
PRED16x16_HORIZONTAL sse2

;-----------------------------------------------------------------------------
; void pred16x16_dc(pixel *src, int stride)
;-----------------------------------------------------------------------------
%macro PRED16x16_DC 1
cglobal pred16x16_dc_10_%1, 2,7
    mov        r4, r0
    sub        r0, r1
    mova       m0, [r0+0]
    paddw      m0, [r0+mmsize]
%if mmsize==8
    paddw      m0, [r0+16]
    paddw      m0, [r0+24]
%endif
    HADDW      m0, m2

    sub        r0, 2
    movzx     r3d, word [r0+r1*1]
    movzx     r5d, word [r0+r1*2]
%rep 7
    lea        r0, [r0+r1*2]
    movzx     r2d, word [r0+r1*1]
    add       r3d, r2d
    movzx     r2d, word [r0+r1*2]
    add       r5d, r2d
%endrep
    lea       r3d, [r3+r5+16]

    movd       m1, r3d
    paddw      m0, m1
    psrlw      m0, 5
    SPLATW     m0, m0
    mov       r3d, 8
.loop:
    MOV16 r4+r1*0, m0, m0, m0, m0
    MOV16 r4+r1*1, m0, m0, m0, m0
    lea        r4, [r4+r1*2]
    dec       r3d
    jg .loop
    REP_RET
%endmacro

INIT_MMX
PRED16x16_DC mmxext
INIT_XMM
PRED16x16_DC sse2

;-----------------------------------------------------------------------------
; void pred16x16_top_dc(pixel *src, int stride)
;-----------------------------------------------------------------------------
%macro PRED16x16_TOP_DC 1
cglobal pred16x16_top_dc_10_%1, 2,3
    sub        r0, r1
    mova       m0, [r0+0]
    paddw      m0, [r0+mmsize]
%if mmsize==8
    paddw      m0, [r0+16]
    paddw      m0, [r0+24]
%endif
    HADDW      m0, m2

    SPLATW     m0, m0
    paddw      m0, [pw_8]
    psrlw      m0, 4
    mov       r2d, 8
.loop:
    MOV16 r0+r1*1, m0, m0, m0, m0
    MOV16 r0+r1*2, m0, m0, m0, m0
    lea        r0, [r0+r1*2]
    dec       r2d
    jg .loop
    REP_RET
%endmacro

INIT_MMX
PRED16x16_TOP_DC mmxext
INIT_XMM
PRED16x16_TOP_DC sse2

;-----------------------------------------------------------------------------
; void pred16x16_left_dc(pixel *src, int stride)
;-----------------------------------------------------------------------------
%macro PRED16x16_LEFT_DC 1
cglobal pred16x16_left_dc_10_%1, 2,7
    mov        r4, r0

    sub        r0, 2
    movzx     r5d, word [r0+r1*0]
    movzx     r6d, word [r0+r1*1]
%rep 7
    lea        r0, [r0+r1*2]
    movzx     r2d, word [r0+r1*0]
    movzx     r3d, word [r0+r1*1]
    add       r5d, r2d
    add       r6d, r3d
%endrep
    lea       r2d, [r5+r6+8]
    shr       r2d, 4

    movd       m0, r2d
    SPLATW     m0, m0
    mov       r3d, 8
.loop:
    MOV16 r4+r1*0, m0, m0, m0, m0
    MOV16 r4+r1*1, m0, m0, m0, m0
    lea        r4, [r4+r1*2]
    dec       r3d
    jg .loop
    REP_RET
%endmacro

INIT_MMX
PRED16x16_LEFT_DC mmxext
INIT_XMM
PRED16x16_LEFT_DC sse2

;-----------------------------------------------------------------------------
; void pred16x16_128_dc(pixel *src, int stride)
;-----------------------------------------------------------------------------
%macro PRED16x16_128_DC 1
cglobal pred16x16_128_dc_10_%1, 2,3
    mova       m0, [pw_512]
    mov       r2d, 8
.loop:
    MOV16 r0+r1*0, m0, m0, m0, m0
    MOV16 r0+r1*1, m0, m0, m0, m0
    lea        r0, [r0+r1*2]
    dec       r2d
    jg .loop
    REP_RET
%endmacro

INIT_MMX
PRED16x16_128_DC mmxext
INIT_XMM
PRED16x16_128_DC sse2