1 ;*****************************************************************************
2 ;* MMX/SSE2/AVX-optimized H.264 deblocking code
3 ;*****************************************************************************
4 ;* Copyright (C) 2005-2011 x264 project
6 ;* Authors: Loren Merritt <lorenm@u.washington.edu>
7 ;* Jason Garrett-Glaser <darkshikari@gmail.com>
8 ;* Oskar Arvidsson <oskar@irock.se>
10 ;* This file is part of FFmpeg.
12 ;* FFmpeg is free software; you can redistribute it and/or
13 ;* modify it under the terms of the GNU Lesser General Public
14 ;* License as published by the Free Software Foundation; either
15 ;* version 2.1 of the License, or (at your option) any later version.
17 ;* FFmpeg is distributed in the hope that it will be useful,
18 ;* but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;* Lesser General Public License for more details.
22 ;* You should have received a copy of the GNU Lesser General Public
23 ;* License along with FFmpeg; if not, write to the Free Software
24 ;* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 ;******************************************************************************
27 %include "libavutil/x86/x86util.asm"
31 pb_A1: times 16 db 0xA1
32 pb_3_1: times 4 db 3, 1
40 ; expands to [base],...,[base+7*stride]
41 %define PASS8ROWS(base, base3, stride, stride3) \
42 [base], [base+stride], [base+stride*2], [base3], \
43 [base3+stride], [base3+stride*2], [base3+stride3], [base3+stride*4]
45 %define PASS8ROWS(base, base3, stride, stride3, offset) \
46 PASS8ROWS(base+offset, base3+offset, stride, stride3)
48 ; in: 8 rows of 4 bytes in %4..%11
49 ; out: 4 rows of 8 bytes in m0..m3
50 %macro TRANSPOSE4x8_LOAD 11
77 ; in: 4 rows of 8 bytes in m0..m3
78 ; out: 8 rows of 4 bytes in %1..%8
79 %macro TRANSPOSE8x4B_STORE 8
108 %macro TRANSPOSE4x8B_LOAD 8
109 TRANSPOSE4x8_LOAD bw, wd, dq, %1, %2, %3, %4, %5, %6, %7, %8
117 ; in: 8 rows of 8 (only the middle 6 pels are used) in %1..%8
118 ; out: 6 rows of 8 in [%9+0*16] .. [%9+5*16]
119 %macro TRANSPOSE6x8_MEM 9
128 SBUTTERFLY bw, 0, 1, 7
129 SBUTTERFLY bw, 2, 3, 7
130 SBUTTERFLY bw, 4, 5, 7
132 SBUTTERFLY3 bw, m6, %8, m7
133 SBUTTERFLY wd, 0, 2, 3
134 SBUTTERFLY wd, 4, 6, 3
137 SBUTTERFLY3 wd, m1, [%9+0x10], m3
138 SBUTTERFLY wd, 5, 7, 0
139 SBUTTERFLY dq, 1, 5, 0
140 SBUTTERFLY dq, 2, 6, 0
150 ; in: 8 rows of 8 in %1..%8
151 ; out: 8 rows of 8 in %9..%16
152 %macro TRANSPOSE8x8_MEM 16
161 SBUTTERFLY bw, 0, 1, 7
162 SBUTTERFLY bw, 2, 3, 7
163 SBUTTERFLY bw, 4, 5, 7
164 SBUTTERFLY3 bw, m6, %8, m7
166 SBUTTERFLY wd, 0, 2, 5
167 SBUTTERFLY wd, 4, 6, 5
168 SBUTTERFLY wd, 1, 3, 5
171 SBUTTERFLY wd, 6, 7, 5
172 SBUTTERFLY dq, 0, 4, 5
173 SBUTTERFLY dq, 1, 6, 5
178 SBUTTERFLY3 dq, m2, %11, m0
179 SBUTTERFLY dq, 3, 7, 4
187 ; out: %4 = |%1-%2|>%3
203 ; out: %4 = |%1-%2|>%3
220 ; in: m0=p1 m1=p0 m2=q0 m3=q1 %1=alpha-1 %2=beta-1
221 ; out: m5=beta-1, m7=mask, %3=alpha-1
228 packuswb m4, m4 ; 16x alpha-1
229 packuswb m5, m5 ; 16x beta-1
233 DIFF_GT m1, m2, m4, m7, m6 ; |p0-q0| > alpha-1
234 DIFF_GT m0, m1, m5, m4, m6 ; |p1-p0| > beta-1
236 DIFF_GT m3, m2, m5, m4, m6 ; |q1-q0| > beta-1
242 ; in: m0=p1 m1=p0 m2=q0 m3=q1 m7=(tc&mask)
245 %macro DEBLOCK_P0_Q0 0
247 pxor m5, m1, m2 ; p0^q0
249 pand m5, [pb_1] ; (p0^q0)&1
250 pavgb m3, m0 ; (p1 - q1 + 256)>>1
252 pavgb m3, [pb_3] ; (((p1 - q1 + 256)>>1)+4)>>1 = 64+2+(p1-q1)>>2
253 pavgb m4, m2 ; (q0 - p0 + 256)>>1
256 paddusb m3, m4 ; d+128+33
268 ; %1=p1 %2=q2 %3=[q2] %4=[q1] %5=tc0 %6=tmp
269 ; out: [q1] = clip( (q2+((p0+q0+1)>>1))>>1, q1-tc0, q1+tc0 )
270 ; clobbers: q2, tmp, tc0
273 pavgb %2, %6 ; avg(p2,avg(p0,q0))
275 pand %6, [pb_1] ; (p2^avg(p0,q0))&1
276 psubusb %2, %6 ; (p2+((p0+q0+1)>>1))>>1
285 ;-----------------------------------------------------------------------------
286 ; void deblock_v_luma( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 )
287 ;-----------------------------------------------------------------------------
288 %macro DEBLOCK_LUMA 0
289 cglobal deblock_v_luma_8, 5,5,10
295 add r4, r0 ; pix-3*stride
297 mova m0, [r4+r1] ; p1
298 mova m1, [r4+2*r1] ; p0
300 mova m3, [r0+r1] ; q1
304 punpcklbw m8, m8 ; tc = 4x tc0[3], 4x tc0[2], 4x tc0[1], 4x tc0[0]
311 DIFF_GT2 m1, m3, m5, m6, m7 ; |p2-p0| > beta-1
315 LUMA_Q1 m0, m3, [r4], [r4+r1], m6, m4
317 movdqa m4, [r0+2*r1] ; q2
318 DIFF_GT2 m2, m4, m5, m6, m3 ; |q2-q0| > beta-1
323 LUMA_Q1 m3, m4, [r0+2*r1], [r0+r1], m8, m6
330 ;-----------------------------------------------------------------------------
331 ; void deblock_h_luma( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 )
332 ;-----------------------------------------------------------------------------
334 cglobal deblock_h_luma_8, 5,9
341 %define pix_tmp rsp+0x30
347 ; transpose 6x16 -> tmp space
348 TRANSPOSE6x8_MEM PASS8ROWS(r6, r5, r7, r8), pix_tmp
351 TRANSPOSE6x8_MEM PASS8ROWS(r6, r5, r7, r8), pix_tmp+8
354 ; alpha, beta, tc0 are still in r2d, r3d, r4
355 ; don't backup r6, r5, r7, r8 because deblock_v_luma_sse2 doesn't use them
356 lea r0, [pix_tmp+0x30]
361 call deblock_v_luma_8
363 ; transpose 16x4 -> original space (only the middle 4 rows were changed by the filter)
366 movq m0, [pix_tmp+0x18]
367 movq m1, [pix_tmp+0x28]
368 movq m2, [pix_tmp+0x38]
369 movq m3, [pix_tmp+0x48]
370 TRANSPOSE8x4B_STORE PASS8ROWS(r6, r5, r7, r8)
376 movq m0, [pix_tmp+0x10]
377 movq m1, [pix_tmp+0x20]
378 movq m2, [pix_tmp+0x30]
379 movq m3, [pix_tmp+0x40]
380 TRANSPOSE8x4B_STORE PASS8ROWS(r6, r5, r7, r8)
392 %if HAVE_AVX_EXTERNAL
399 %macro DEBLOCK_LUMA 2
400 ;-----------------------------------------------------------------------------
401 ; void deblock_v8_luma( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 )
402 ;-----------------------------------------------------------------------------
403 cglobal deblock_%1_luma_8, 5,5,8,2*%2
408 add r4, r0 ; pix-3*stride
410 mova m0, [r4+r1] ; p1
411 mova m1, [r4+2*r1] ; p0
413 mova m3, [r0+r1] ; q1
420 punpcklbw m4, m4 ; tc = 4x tc0[3], 4x tc0[2], 4x tc0[1], 4x tc0[0]
421 mova [esp+%2], m4 ; tc
425 mova [esp], m4 ; mask
427 DIFF_GT2 m1, m3, m5, m6, m7 ; |p2-p0| > beta-1
429 pand m4, [esp+%2] ; tc
432 LUMA_Q1 m0, m3, [r4], [r4+r1], m6, m4
434 mova m4, [r0+2*r1] ; q2
435 DIFF_GT2 m2, m4, m5, m6, m3 ; |q2-q0| > beta-1
436 pand m6, [esp] ; mask
437 mova m5, [esp+%2] ; tc
441 LUMA_Q1 m3, m4, [r0+2*r1], [r0+r1], m5, m6
448 ;-----------------------------------------------------------------------------
449 ; void deblock_h_luma( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 )
450 ;-----------------------------------------------------------------------------
452 cglobal deblock_h_luma_8, 0,5,8,0x60+HAVE_ALIGNED_STACK*12
458 %define pix_tmp esp+12*HAVE_ALIGNED_STACK
460 ; transpose 6x16 -> tmp space
461 TRANSPOSE6x8_MEM PASS8ROWS(r0, r1, r3, r4), pix_tmp
464 TRANSPOSE6x8_MEM PASS8ROWS(r0, r1, r3, r4), pix_tmp+8
467 lea r0, [pix_tmp+0x30]
473 call deblock_%1_luma_8
475 add dword [esp ], 8 ; pix_tmp+0x38
476 add dword [esp+16], 2 ; tc0+2
477 call deblock_%1_luma_8
481 ; transpose 16x4 -> original space (only the middle 4 rows were changed by the filter)
485 movq m0, [pix_tmp+0x10]
486 movq m1, [pix_tmp+0x20]
488 movq m2, [pix_tmp+0x30]
489 movq m3, [pix_tmp+0x40]
490 TRANSPOSE8x4B_STORE PASS8ROWS(r0, r1, r3, r4)
494 movq m0, [pix_tmp+0x18]
495 movq m1, [pix_tmp+0x28]
496 movq m2, [pix_tmp+0x38]
497 movq m3, [pix_tmp+0x48]
498 TRANSPOSE8x4B_STORE PASS8ROWS(r0, r1, r3, r4)
501 %endmacro ; DEBLOCK_LUMA
507 %if HAVE_AVX_EXTERNAL
516 %macro LUMA_INTRA_P012 4 ; p0..p3 in memory
526 pavgb t0, t1 ; ((p2+p1+1)/2 + (p0+q0+1)/2 + 1)/2
544 psubb t0, t2 ; p1' = (p2+p1+p0+q0+2)/4;
556 psubb t3, t2 ; p2+2*p1+2*p0+2*q0+q1
560 pavgb t1, t5 ; (((p2+q1)/2 + p1+1)/2 + (p0+q0+1)/2 + 1)/2
565 psubb t1, t3 ; p0'a = (p2+2*p1+2*p0+2*q0+q1+4)/8
571 pavgb t2, p1 ; p0'b = (2*p1+p0+q0+2)/4
579 mova %1, t1 ; store p0
584 pavgb t1, t0 ; (p3+p2+1)/2 + (p2+p1+p0+q0+2)/4
586 paddb t2, t4 ; 2*p3+3*p2+p1+p0+q0
591 psubb t1, t2 ; p2' = (2*p3+3*p2+p1+p0+q0+4)/8
599 mova %2, t0 ; store p1
600 mova %3, t1 ; store p2
603 %macro LUMA_INTRA_SWAP_PQ 0
609 %define mask1p mask1q
612 %macro DEBLOCK_LUMA_INTRA 1
631 %define mask1q [rsp-24]
636 %define spill(x) [esp+16*x]
641 %define mask0 spill(2)
642 %define mask1p spill(3)
643 %define mask1q spill(4)
648 ;-----------------------------------------------------------------------------
649 ; void deblock_v_luma_intra( uint8_t *pix, int stride, int alpha, int beta )
650 ;-----------------------------------------------------------------------------
652 cglobal deblock_%1_luma_intra_8, 4,6,16,0x10
654 cglobal deblock_%1_luma_intra_8, 4,6,16,ARCH_X86_64*0x50-0x50
657 lea r5, [r1*3] ; 3*stride
663 add r4, r0 ; pix-4*stride
671 LOAD_MASK r2d, r3d, t5 ; m5=beta-1, t5=alpha-1, m7=mask0
672 SWAP 7, 12 ; m12=mask0
674 pavgb t5, mpb_1 ; alpha/4+1
677 DIFF_GT2 p0, q0, t5, t0, t3 ; t0 = |p0-q0| > alpha/4+1
678 DIFF_GT2 p0, p2, m5, t2, t5 ; mask1 = |p2-p0| > beta-1
679 DIFF_GT2 q0, q2, m5, t4, t5 ; t4 = |q2-q0| > beta-1
686 LOAD_MASK r2d, r3d, t5 ; m5=beta-1, t5=alpha-1, m7=mask0
690 pavgb m4, [pb_1] ; alpha/4+1
691 DIFF_GT2 p0, q0, m4, m6, m7 ; m6 = |p0-q0| > alpha/4+1
693 DIFF_GT2 p0, p2, m5, m4, m7 ; m4 = |p2-p0| > beta-1
696 DIFF_GT2 q0, q2, m5, m4, m7 ; m4 = |q2-q0| > beta-1
700 LUMA_INTRA_P012 [r4+r5], [r4+2*r1], [r4+r1], [r4]
702 LUMA_INTRA_P012 [r0], [r0+r1], [r0+2*r1], [r0+r5]
708 ;-----------------------------------------------------------------------------
709 ; void deblock_h_luma_intra( uint8_t *pix, int stride, int alpha, int beta )
710 ;-----------------------------------------------------------------------------
711 cglobal deblock_h_luma_intra_8, 4,9
719 ; transpose 8x16 -> tmp space
720 TRANSPOSE8x8_MEM PASS8ROWS(r6, r5, r7, r8), PASS8ROWS(pix_tmp, pix_tmp+0x30, 0x10, 0x30)
723 TRANSPOSE8x8_MEM PASS8ROWS(r6, r5, r7, r8), PASS8ROWS(pix_tmp+8, pix_tmp+0x38, 0x10, 0x30)
725 lea r0, [pix_tmp+0x40]
727 call deblock_v_luma_intra_8
729 ; transpose 16x6 -> original space (but we can't write only 6 pixels, so really 16x8)
731 TRANSPOSE8x8_MEM PASS8ROWS(pix_tmp+8, pix_tmp+0x38, 0x10, 0x30), PASS8ROWS(r6, r5, r7, r8)
736 TRANSPOSE8x8_MEM PASS8ROWS(pix_tmp, pix_tmp+0x30, 0x10, 0x30), PASS8ROWS(r6, r5, r7, r8)
740 cglobal deblock_h_luma_intra_8, 2,4,8,0x80
746 ; transpose 8x16 -> tmp space
747 TRANSPOSE8x8_MEM PASS8ROWS(r0, r2, r1, r3), PASS8ROWS(pix_tmp, pix_tmp+0x30, 0x10, 0x30)
750 TRANSPOSE8x8_MEM PASS8ROWS(r0, r2, r1, r3), PASS8ROWS(pix_tmp+8, pix_tmp+0x38, 0x10, 0x30)
752 lea r0, [pix_tmp+0x40]
757 call deblock_%1_luma_intra_8
759 add dword [rsp], 8 ; pix_tmp+8
760 call deblock_%1_luma_intra_8
769 ; transpose 16x6 -> original space (but we can't write only 6 pixels, so really 16x8)
770 TRANSPOSE8x8_MEM PASS8ROWS(pix_tmp, pix_tmp+0x30, 0x10, 0x30), PASS8ROWS(r0, r2, r1, r3)
773 TRANSPOSE8x8_MEM PASS8ROWS(pix_tmp+8, pix_tmp+0x38, 0x10, 0x30), PASS8ROWS(r0, r2, r1, r3)
776 %endmacro ; DEBLOCK_LUMA_INTRA
780 %if HAVE_AVX_EXTERNAL
786 DEBLOCK_LUMA_INTRA v8
791 %macro CHROMA_V_START 0
799 %macro CHROMA_H_START 0
811 ;-----------------------------------------------------------------------------
812 ; void ff_deblock_v_chroma( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 )
813 ;-----------------------------------------------------------------------------
814 cglobal deblock_v_chroma_8, 5,6
820 call ff_chroma_inter_body_mmxext
825 ;-----------------------------------------------------------------------------
826 ; void ff_deblock_h_chroma( uint8_t *pix, int stride, int alpha, int beta, int8_t *tc0 )
827 ;-----------------------------------------------------------------------------
828 cglobal deblock_h_chroma_8, 5,7
830 %define buf0 [rsp-24]
831 %define buf1 [rsp-16]
841 TRANSPOSE4x8_LOAD bw, wd, dq, PASS8ROWS(t5, r0, r1, t6)
851 TRANSPOSE8x4B_STORE PASS8ROWS(t5, r0, r1, t6)
858 ff_chroma_inter_body_mmxext:
868 ; in: %1=p0 %2=p1 %3=q1
869 ; out: p0 = (p0 + q1 + 2*p1 + 2) >> 2
870 %macro CHROMA_INTRA_P0 3
873 pand m4, [pb_1] ; m4 = (p0^q1)&1
876 pavgb %1, %2 ; dst = avg(p1, avg(p0,q1) - ((p0^q1)&1))
882 ;-----------------------------------------------------------------------------
883 ; void ff_deblock_v_chroma_intra( uint8_t *pix, int stride, int alpha, int beta )
884 ;-----------------------------------------------------------------------------
885 cglobal deblock_v_chroma_intra_8, 4,5
891 call ff_chroma_intra_body_mmxext
896 ;-----------------------------------------------------------------------------
897 ; void ff_deblock_h_chroma_intra( uint8_t *pix, int stride, int alpha, int beta )
898 ;-----------------------------------------------------------------------------
899 cglobal deblock_h_chroma_intra_8, 4,6
901 TRANSPOSE4x8_LOAD bw, wd, dq, PASS8ROWS(t5, r0, r1, t6)
902 call ff_chroma_intra_body_mmxext
903 TRANSPOSE8x4B_STORE PASS8ROWS(t5, r0, r1, t6)
907 ff_chroma_intra_body_mmxext:
911 CHROMA_INTRA_P0 m1, m0, m3
912 CHROMA_INTRA_P0 m2, m3, m0
921 ;-----------------------------------------------------------------------------
922 ; void h264_loop_filter_strength(int16_t bs[2][4][4], uint8_t nnz[40],
923 ; int8_t ref[2][40], int16_t mv[2][40][2],
924 ; int bidir, int edges, int step,
925 ; int mask_mv0, int mask_mv1, int field);
933 ;-----------------------------------------------------------------------------
934 %macro loop_filter_strength_iteration 7 ; edges, step, mask_mv,
935 ; dir, d_idx, mask_dir, bidir
943 xor b_idxd, b_idxd ; for (b_idx = 0; b_idx < edges; b_idx += step)
948 test b_idxd, dword mask_mvd
949 jnz %%.skip_loop_iter ; if (!(b_idx & mask_mv))
951 movd m2, [refq+b_idxq+d_idx+12] ; { ref0[bn] }
952 punpckldq m2, [refq+b_idxq+d_idx+52] ; { ref0[bn], ref1[bn] }
953 pshufw m0, [refq+b_idxq+12], 0x44 ; { ref0[b], ref0[b] }
954 pshufw m1, [refq+b_idxq+52], 0x44 ; { ref1[b], ref1[b] }
955 pshufw m3, m2, 0x4E ; { ref1[bn], ref0[bn] }
956 psubb m0, m2 ; { ref0[b] != ref0[bn],
957 ; ref0[b] != ref1[bn] }
958 psubb m1, m3 ; { ref1[b] != ref1[bn],
959 ; ref1[b] != ref0[bn] }
962 mova m1, [mvq+b_idxq*4+(d_idx+12)*4]
963 mova m2, [mvq+b_idxq*4+(d_idx+12)*4+mmsize]
966 psubw m1, [mvq+b_idxq*4+12*4]
967 psubw m2, [mvq+b_idxq*4+12*4+mmsize]
968 psubw m3, [mvq+b_idxq*4+52*4]
969 psubw m4, [mvq+b_idxq*4+52*4+mmsize]
974 psubusb m1, m5 ; abs(mv[b] - mv[bn]) >= limit
979 mova m1, [mvq+b_idxq*4+(d_idx+52)*4]
980 mova m2, [mvq+b_idxq*4+(d_idx+52)*4+mmsize]
983 psubw m1, [mvq+b_idxq*4+12*4]
984 psubw m2, [mvq+b_idxq*4+12*4+mmsize]
985 psubw m3, [mvq+b_idxq*4+52*4]
986 psubw m4, [mvq+b_idxq*4+52*4+mmsize]
991 psubusb m1, m5 ; abs(mv[b] - mv[bn]) >= limit
1000 movd m0, [refq+b_idxq+12]
1001 psubb m0, [refq+b_idxq+d_idx+12] ; ref[b] != ref[bn]
1003 mova m1, [mvq+b_idxq*4+12*4]
1004 mova m2, [mvq+b_idxq*4+12*4+mmsize]
1005 psubw m1, [mvq+b_idxq*4+(d_idx+12)*4]
1006 psubw m2, [mvq+b_idxq*4+(d_idx+12)*4+mmsize]
1009 psubusb m1, m5 ; abs(mv[b] - mv[bn]) >= limit
1012 %endif ; bidir == 1/0
1015 movd m1, [nnzq+b_idxq+12]
1016 por m1, [nnzq+b_idxq+d_idx+12] ; nnz[b] || nnz[bn]
1024 movq [bsq+b_idxq+32*dir], m1
1026 add b_idxd, dword stepd
1027 cmp b_idxd, dword edgesd
1032 cglobal h264_loop_filter_strength, 9, 9, 0, bs, nnz, ref, mv, bidir, edges, \
1033 step, mask_mv0, mask_mv1, field
1034 %define b_idxq bidirq
1035 %define b_idxd bidird
1048 %define mask_mv0d mask_mv0m
1049 %define mask_mv1d mask_mv1m
1051 shl dword mask_mv1d, 3
1052 shl dword mask_mv0d, 3
1056 loop_filter_strength_iteration edgesd, stepd, mask_mv1d, 1, -8, 0, 0
1057 loop_filter_strength_iteration 32, 8, mask_mv0d, 0, -1, -1, 0
1059 mova m0, [bsq+mmsize*0]
1060 mova m1, [bsq+mmsize*1]
1061 mova m2, [bsq+mmsize*2]
1062 mova m3, [bsq+mmsize*3]
1063 TRANSPOSE4x4W 0, 1, 2, 3, 4
1064 mova [bsq+mmsize*0], m0
1065 mova [bsq+mmsize*1], m1
1066 mova [bsq+mmsize*2], m2
1067 mova [bsq+mmsize*3], m3
1071 loop_filter_strength_iteration edgesd, stepd, mask_mv1d, 1, -8, 0, 1
1072 loop_filter_strength_iteration 32, 8, mask_mv0d, 0, -1, -1, 1
1074 mova m0, [bsq+mmsize*0]
1075 mova m1, [bsq+mmsize*1]
1076 mova m2, [bsq+mmsize*2]
1077 mova m3, [bsq+mmsize*3]
1078 TRANSPOSE4x4W 0, 1, 2, 3, 4
1079 mova [bsq+mmsize*0], m0
1080 mova [bsq+mmsize*1], m1
1081 mova [bsq+mmsize*2], m2
1082 mova [bsq+mmsize*3], m3