1 ;*****************************************************************************
2 ;* mc-a.asm: h264 encoder library
3 ;*****************************************************************************
4 ;* Copyright (C) 2003-2008 x264 project
6 ;* Authors: Loren Merritt <lorenm@u.washington.edu>
7 ;* Fiona Glaser <fiona@x264.com>
8 ;* Laurent Aimar <fenrir@via.ecp.fr>
9 ;* Min Chen <chenm001.163.com>
11 ;* This program is free software; you can redistribute it and/or modify
12 ;* it under the terms of the GNU General Public License as published by
13 ;* the Free Software Foundation; either version 2 of the License, or
14 ;* (at your option) any later version.
16 ;* This program is distributed in the hope that it will be useful,
17 ;* but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;* GNU General Public License for more details.
21 ;* You should have received a copy of the GNU General Public License
22 ;* along with this program; if not, write to the Free Software
23 ;* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
24 ;*****************************************************************************
38 ;=============================================================================
40 ;=============================================================================
41 ; implicit bipred only:
42 ; assumes log2_denom = 5, offset = 0, weight1 + weight2 = 64
44 DECLARE_REG_TMP 0,1,2,3,4,5,10,11
45 %macro AVG_START 0-1 0
53 DECLARE_REG_TMP 1,2,3,4,5,6,1,2
54 %macro AVG_START 0-1 0
87 %macro BIWEIGHT_START_MMX 0
89 SPLATW m2, m2 ; weight_dst
90 mova m3, [pw_64 GLOBAL]
91 psubw m3, m2 ; weight_src
92 mova m4, [pw_32 GLOBAL] ; rounding
96 %macro BIWEIGHT_SSSE3 2
105 %macro BIWEIGHT_START_SSSE3 0
106 movzx t6d, byte r6m ; FIXME x86_64
112 mova m4, [pw_32 GLOBAL]
113 SPLATW m3, m3 ; weight_dst,src
116 %macro BIWEIGHT_ROW 4
123 BIWEIGHT [%2+mmsize/2], [%3+mmsize/2]
129 ;-----------------------------------------------------------------------------
130 ; int x264_pixel_avg_weight_w16_mmxext( uint8_t *dst, int, uint8_t *src1, int, uint8_t *src2, int, int i_weight )
131 ;-----------------------------------------------------------------------------
132 %macro AVG_WEIGHT 2-3 0
133 cglobal x264_pixel_avg_weight_w%2_%1
136 %if %2==8 && mmsize==16
139 BIWEIGHT [t2+t3], [t4+t5]
146 BIWEIGHT_ROW t0+x, t2+x, t4+x, %2
147 BIWEIGHT_ROW t0+x+t1, t2+x+t3, t4+x+t5, %2
159 %define BIWEIGHT BIWEIGHT_MMX
160 %define BIWEIGHT_START BIWEIGHT_START_MMX
164 AVG_WEIGHT mmxext, 16
166 %define x264_pixel_avg_weight_w4_sse2 x264_pixel_avg_weight_w4_mmxext
167 AVG_WEIGHT sse2, 8, 7
168 AVG_WEIGHT sse2, 16, 7
169 %define BIWEIGHT BIWEIGHT_SSSE3
170 %define BIWEIGHT_START BIWEIGHT_START_SSSE3
174 AVG_WEIGHT ssse3, 8, 7
175 AVG_WEIGHT ssse3, 16, 7
179 ;=============================================================================
181 ;=============================================================================
183 ;-----------------------------------------------------------------------------
184 ; void x264_pixel_avg_4x4_mmxext( uint8_t *dst, int dst_stride,
185 ; uint8_t *src1, int src1_stride, uint8_t *src2, int src2_stride, int weight );
186 ;-----------------------------------------------------------------------------
188 cglobal x264_pixel_avg_%1x%2_%3
191 jne x264_pixel_avg_weight_w%1_%3
192 %if mmsize == 16 && %1 == 16
194 jz x264_pixel_avg_w%1_sse2
196 jmp x264_pixel_avg_w%1_mmxext
199 ;-----------------------------------------------------------------------------
200 ; void x264_pixel_avg_w4_mmxext( uint8_t *dst, int dst_stride,
201 ; uint8_t *src1, int src1_stride, uint8_t *src2, int src2_stride,
202 ; int height, int weight );
203 ;-----------------------------------------------------------------------------
227 AVG_FUNC x264_pixel_avg_w4_mmxext, movd, movd
232 AVG_FUNC x264_pixel_avg_w8_mmxext, movq, movq
237 cglobal x264_pixel_avg_w16_mmxext
257 AVG_FUNC x264_pixel_avg_w16_sse2, movdqu, movdqa
275 ;=============================================================================
277 ;=============================================================================
279 ;-----------------------------------------------------------------------------
280 ; void x264_pixel_avg2_w4_mmxext( uint8_t *dst, int dst_stride,
281 ; uint8_t *src1, int src_stride,
282 ; uint8_t *src2, int height );
283 ;-----------------------------------------------------------------------------
285 cglobal x264_pixel_avg2_w%1_mmxext, 6,7
306 cglobal x264_pixel_avg2_w%1_mmxext, 6,7
332 cglobal x264_pixel_avg2_w20_mmxext, 6,7
344 pavgb mm2, [r2+r4+16]
347 pavgb mm5, [r2+r6+16]
360 cglobal x264_pixel_avg2_w16_sse2, 6,7
379 cglobal x264_pixel_avg2_w20_%1, 6,7
387 %ifidn %1, sse2_misalign
396 pavgb mm4, [r2+r4+16]
397 pavgb mm5, [r2+r6+16]
410 AVG2_W20 sse2_misalign
412 ; Cacheline split code for processors with high latencies for loads
413 ; split over cache lines. See sad-a.asm for a more detailed explanation.
414 ; This particular instance is complicated by the fact that src1 and src2
415 ; can have different alignments. For simplicity and code size, only the
416 ; MMX cacheline workaround is used. As a result, in the case of SSE2
417 ; pixel_avg, the cacheline check functions calls the SSE2 version if there
418 ; is no cacheline split, and the MMX workaround if there is.
423 movd %1, [sw_64 GLOBAL]
428 %macro AVG_CACHELINE_CHECK 3 ; width, cacheline, instruction set
429 cglobal x264_pixel_avg2_w%1_cache%2_%3
431 and eax, 0x1f|(%2>>1)
432 cmp eax, (32-%1)|(%2>>1)
433 jle x264_pixel_avg2_w%1_%3
434 ;w12 isn't needed because w16 is just as fast if there's no cacheline split
436 jmp x264_pixel_avg2_w16_cache_mmxext
438 jmp x264_pixel_avg2_w%1_cache_mmxext
442 %macro AVG_CACHELINE_START 0
443 %assign stack_offset 0
454 %macro AVG_CACHELINE_LOOP 2
457 movq mm2, [r2+r4+8+%1]
469 x264_pixel_avg2_w8_cache_mmxext:
471 AVG_CACHELINE_LOOP 0, movq
478 x264_pixel_avg2_w16_cache_mmxext:
480 AVG_CACHELINE_LOOP 0, movq
481 AVG_CACHELINE_LOOP 8, movq
488 x264_pixel_avg2_w20_cache_mmxext:
490 AVG_CACHELINE_LOOP 0, movq
491 AVG_CACHELINE_LOOP 8, movq
492 AVG_CACHELINE_LOOP 16, movd
500 AVG_CACHELINE_CHECK 8, 32, mmxext
501 AVG_CACHELINE_CHECK 12, 32, mmxext
502 AVG_CACHELINE_CHECK 16, 32, mmxext
503 AVG_CACHELINE_CHECK 20, 32, mmxext
504 AVG_CACHELINE_CHECK 16, 64, mmxext
505 AVG_CACHELINE_CHECK 20, 64, mmxext
508 AVG_CACHELINE_CHECK 8, 64, mmxext
509 AVG_CACHELINE_CHECK 12, 64, mmxext
510 AVG_CACHELINE_CHECK 16, 64, sse2
511 AVG_CACHELINE_CHECK 20, 64, sse2
513 ;=============================================================================
515 ;=============================================================================
529 ;-----------------------------------------------------------------------------
530 ; void x264_mc_copy_w4_mmx( uint8_t *dst, int i_dst_stride,
531 ; uint8_t *src, int i_src_stride, int i_height )
532 ;-----------------------------------------------------------------------------
533 cglobal x264_mc_copy_w4_mmx, 4,6
538 COPY4 movd, movd, r4, r5
542 COPY4 movd, movd, r4, r5
545 cglobal x264_mc_copy_w8_mmx, 5,7
549 COPY4 movq, movq, r5, r6
556 cglobal x264_mc_copy_w16_mmx, 5,7
565 movq mm5, [r2+r3*2+8]
573 movq [r0+r1*2+8], mm5
583 %macro COPY_W16_SSE2 2
588 COPY4 movdqa, %2, r5, r6
596 COPY_W16_SSE2 x264_mc_copy_w16_sse2, movdqu
597 ; cacheline split with mmx has too much overhead; the speed benefit is near-zero.
598 ; but with SSE3 the overhead is zero, so there's no reason not to include it.
599 COPY_W16_SSE2 x264_mc_copy_w16_sse3, lddqu
600 COPY_W16_SSE2 x264_mc_copy_w16_aligned_sse2, movdqa
604 ;=============================================================================
606 ;=============================================================================
607 ; FIXME assumes 64 byte cachelines
609 ;-----------------------------------------------------------------------------
610 ; void x264_prefetch_fenc_mmxext( uint8_t *pix_y, int stride_y,
611 ; uint8_t *pix_uv, int stride_uv, int mb_x )
612 ;-----------------------------------------------------------------------------
614 cglobal x264_prefetch_fenc_mmxext, 5,5
618 lea r0, [r0+rax*4+64]
633 cglobal x264_prefetch_fenc_mmxext
657 ;-----------------------------------------------------------------------------
658 ; void x264_prefetch_ref_mmxext( uint8_t *pix, int stride, int parity )
659 ;-----------------------------------------------------------------------------
660 cglobal x264_prefetch_ref_mmxext, 3,3
678 ;=============================================================================
680 ;=============================================================================
689 %macro MC_CHROMA_START 0
701 add r2, t0 ; src += (dx>>3) + (dy>>3) * src_stride
704 ;-----------------------------------------------------------------------------
705 ; void x264_mc_chroma_mmxext( uint8_t *dst, int dst_stride,
706 ; uint8_t *src, int src_stride,
708 ; int width, int height )
709 ;-----------------------------------------------------------------------------
710 %macro MC_CHROMA 1-2 0
711 cglobal x264_mc_chroma_%1
714 jle x264_mc_chroma_mmxext
726 SPLATW m5, m5 ; m5 = dx
727 SPLATW m6, m6 ; m6 = dy
729 mova m4, [pw_8 GLOBAL]
731 psubw m4, m5 ; m4 = 8-dx
732 psubw m0, m6 ; m0 = 8-dy
735 pmullw m5, m0 ; m5 = dx*(8-dy) = cB
736 pmullw m7, m6 ; m7 = dx*dy = cD
737 pmullw m6, m4 ; m6 = (8-dx)*dy = cC
738 pmullw m4, m0 ; m4 = (8-dx)*(8-dy) = cA
753 punpcklbw m1, m3 ; 00 px1 | 00 px2 | 00 px3 | 00 px4
755 pmullw m1, m6 ; 2nd line * cC
756 pmullw m0, m4 ; 1st line * cA
757 paddw m0, m1 ; m0 <- result
764 paddw m0, [pw_32 GLOBAL]
766 pmullw m2, m5 ; line * cB
767 pmullw m1, m7 ; line * cD
772 packuswb m0, m3 ; 00 00 00 00 px1 px2 px3 px4
776 add r0, r1 ; dst_stride
782 jnz .finish ; width != 8 so assume 4
784 lea r0, [r10+4] ; dst
785 lea r2, [r11+4] ; src
791 mov r4d, r7m ; height
800 mov r5, r3 ; pel_offset = dx ? 1 : src_stride
806 mova m5, [pw_8 GLOBAL]
808 mova m7, [pw_4 GLOBAL]
865 %endmacro ; MC_CHROMA
873 cglobal x264_mc_chroma_ssse3, 0,6,8
883 imul r5d, t0d ; (x*255+8)*y
884 imul r4d, t0d ; (x*255+8)*(8-y)
887 mova m5, [pw_32 GLOBAL]
902 punpcklbw m3, [r2+r3+1]
928 mova m5, [pw_32 GLOBAL]
969 ; mc_chroma 1d ssse3 is negligibly faster, and definitely not worth the extra code size