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
50 DECLARE_REG_TMP 1,2,3,4,5,6,1,2
84 %macro BIWEIGHT_START_MMX 0
86 SPLATW m4, m4 ; weight_dst
87 mova m5, [pw_64 GLOBAL]
88 psubw m5, m4 ; weight_src
89 mova m6, [pw_32 GLOBAL] ; rounding
93 %macro BIWEIGHT_SSSE3 2
102 %macro BIWEIGHT_START_SSSE3 0
103 movzx t6d, byte r6m ; FIXME x86_64
109 mova m6, [pw_32 GLOBAL]
110 SPLATW m5, m5 ; weight_dst,src
113 %macro BIWEIGHT_ROW 4
120 BIWEIGHT [%2+mmsize/2], [%3+mmsize/2]
126 ;-----------------------------------------------------------------------------
127 ; int x264_pixel_avg_weight_w16_mmxext( uint8_t *dst, int, uint8_t *src1, int, uint8_t *src2, int, int i_weight )
128 ;-----------------------------------------------------------------------------
130 cglobal x264_pixel_avg_weight_w%2_%1, 0,0
133 %if %2==8 && mmsize==16
136 BIWEIGHT [t2+t3], [t4+t5]
143 BIWEIGHT_ROW t0+x, t2+x, t4+x, %2
144 BIWEIGHT_ROW t0+x+t1, t2+x+t3, t4+x+t5, %2
156 %define BIWEIGHT BIWEIGHT_MMX
157 %define BIWEIGHT_START BIWEIGHT_START_MMX
161 AVG_WEIGHT mmxext, 16
163 %define x264_pixel_avg_weight_w4_sse2 x264_pixel_avg_weight_w4_mmxext
166 %define BIWEIGHT BIWEIGHT_SSSE3
167 %define BIWEIGHT_START BIWEIGHT_START_SSSE3
176 ;=============================================================================
178 ;=============================================================================
180 ;-----------------------------------------------------------------------------
181 ; void x264_pixel_avg_4x4_mmxext( uint8_t *dst, int dst_stride,
182 ; uint8_t *src1, int src1_stride, uint8_t *src2, int src2_stride, int weight );
183 ;-----------------------------------------------------------------------------
185 cglobal x264_pixel_avg_%1x%2_%3,0,0
188 jne x264_pixel_avg_weight_w%1_%3
189 %if mmsize == 16 && %1 == 16
191 jz x264_pixel_avg_w%1_sse2
193 jmp x264_pixel_avg_w%1_mmxext
196 ;-----------------------------------------------------------------------------
197 ; void x264_pixel_avg_w4_mmxext( uint8_t *dst, int dst_stride,
198 ; uint8_t *src1, int src1_stride, uint8_t *src2, int src2_stride,
199 ; int height, int weight );
200 ;-----------------------------------------------------------------------------
224 AVG_FUNC x264_pixel_avg_w4_mmxext, movd, movd
229 AVG_FUNC x264_pixel_avg_w8_mmxext, movq, movq
234 cglobal x264_pixel_avg_w16_mmxext
254 AVG_FUNC x264_pixel_avg_w16_sse2, movdqu, movdqa
272 ;=============================================================================
274 ;=============================================================================
276 ;-----------------------------------------------------------------------------
277 ; void x264_pixel_avg2_w4_mmxext( uint8_t *dst, int dst_stride,
278 ; uint8_t *src1, int src_stride,
279 ; uint8_t *src2, int height );
280 ;-----------------------------------------------------------------------------
282 cglobal x264_pixel_avg2_w%1_mmxext, 6,7
303 cglobal x264_pixel_avg2_w%1_mmxext, 6,7
329 cglobal x264_pixel_avg2_w20_mmxext, 6,7
341 pavgb mm2, [r2+r4+16]
344 pavgb mm5, [r2+r6+16]
357 cglobal x264_pixel_avg2_w16_sse2, 6,7
376 cglobal x264_pixel_avg2_w20_%1, 6,7
384 %ifidn %1, sse2_misalign
393 pavgb mm4, [r2+r4+16]
394 pavgb mm5, [r2+r6+16]
407 AVG2_W20 sse2_misalign
409 ; Cacheline split code for processors with high latencies for loads
410 ; split over cache lines. See sad-a.asm for a more detailed explanation.
411 ; This particular instance is complicated by the fact that src1 and src2
412 ; can have different alignments. For simplicity and code size, only the
413 ; MMX cacheline workaround is used. As a result, in the case of SSE2
414 ; pixel_avg, the cacheline check functions calls the SSE2 version if there
415 ; is no cacheline split, and the MMX workaround if there is.
420 movd %1, [sw_64 GLOBAL]
425 %macro AVG_CACHELINE_CHECK 3 ; width, cacheline, instruction set
426 cglobal x264_pixel_avg2_w%1_cache%2_%3, 0,0
428 and eax, 0x1f|(%2>>1)
429 cmp eax, (32-%1)|(%2>>1)
430 jle x264_pixel_avg2_w%1_%3
431 ;w12 isn't needed because w16 is just as fast if there's no cacheline split
433 jmp x264_pixel_avg2_w16_cache_mmxext
435 jmp x264_pixel_avg2_w%1_cache_mmxext
439 %macro AVG_CACHELINE_START 0
440 %assign stack_offset 0
451 %macro AVG_CACHELINE_LOOP 2
454 movq mm2, [r2+r4+8+%1]
466 x264_pixel_avg2_w8_cache_mmxext:
468 AVG_CACHELINE_LOOP 0, movq
475 x264_pixel_avg2_w16_cache_mmxext:
477 AVG_CACHELINE_LOOP 0, movq
478 AVG_CACHELINE_LOOP 8, movq
485 x264_pixel_avg2_w20_cache_mmxext:
487 AVG_CACHELINE_LOOP 0, movq
488 AVG_CACHELINE_LOOP 8, movq
489 AVG_CACHELINE_LOOP 16, movd
497 AVG_CACHELINE_CHECK 8, 32, mmxext
498 AVG_CACHELINE_CHECK 12, 32, mmxext
499 AVG_CACHELINE_CHECK 16, 32, mmxext
500 AVG_CACHELINE_CHECK 20, 32, mmxext
501 AVG_CACHELINE_CHECK 16, 64, mmxext
502 AVG_CACHELINE_CHECK 20, 64, mmxext
505 AVG_CACHELINE_CHECK 8, 64, mmxext
506 AVG_CACHELINE_CHECK 12, 64, mmxext
507 AVG_CACHELINE_CHECK 16, 64, sse2
508 AVG_CACHELINE_CHECK 20, 64, sse2
510 ;=============================================================================
512 ;=============================================================================
526 ;-----------------------------------------------------------------------------
527 ; void x264_mc_copy_w4_mmx( uint8_t *dst, int i_dst_stride,
528 ; uint8_t *src, int i_src_stride, int i_height )
529 ;-----------------------------------------------------------------------------
530 cglobal x264_mc_copy_w4_mmx, 4,6
535 COPY4 movd, movd, r4, r5
539 COPY4 movd, movd, r4, r5
542 cglobal x264_mc_copy_w8_mmx, 5,7
546 COPY4 movq, movq, r5, r6
553 cglobal x264_mc_copy_w16_mmx, 5,7
562 movq mm5, [r2+r3*2+8]
570 movq [r0+r1*2+8], mm5
580 %macro COPY_W16_SSE2 2
585 COPY4 movdqa, %2, r5, r6
593 COPY_W16_SSE2 x264_mc_copy_w16_sse2, movdqu
594 ; cacheline split with mmx has too much overhead; the speed benefit is near-zero.
595 ; but with SSE3 the overhead is zero, so there's no reason not to include it.
596 COPY_W16_SSE2 x264_mc_copy_w16_sse3, lddqu
597 COPY_W16_SSE2 x264_mc_copy_w16_aligned_sse2, movdqa
601 ;=============================================================================
603 ;=============================================================================
604 ; FIXME assumes 64 byte cachelines
606 ;-----------------------------------------------------------------------------
607 ; void x264_prefetch_fenc_mmxext( uint8_t *pix_y, int stride_y,
608 ; uint8_t *pix_uv, int stride_uv, int mb_x )
609 ;-----------------------------------------------------------------------------
611 cglobal x264_prefetch_fenc_mmxext, 5,5
615 lea r0, [r0+rax*4+64]
630 cglobal x264_prefetch_fenc_mmxext
654 ;-----------------------------------------------------------------------------
655 ; void x264_prefetch_ref_mmxext( uint8_t *pix, int stride, int parity )
656 ;-----------------------------------------------------------------------------
657 cglobal x264_prefetch_ref_mmxext, 3,3
675 ;=============================================================================
677 ;=============================================================================
686 %macro MC_CHROMA_START 0
698 add r2, t0 ; src += (dx>>3) + (dy>>3) * src_stride
701 ;-----------------------------------------------------------------------------
702 ; void x264_mc_chroma_mmxext( uint8_t *dst, int dst_stride,
703 ; uint8_t *src, int src_stride,
705 ; int width, int height )
706 ;-----------------------------------------------------------------------------
708 cglobal x264_mc_chroma_%1, 0,6
711 jle x264_mc_chroma_mmxext %+ .skip_prologue
723 SPLATW m5, m5 ; m5 = dx
724 SPLATW m6, m6 ; m6 = dy
726 mova m4, [pw_8 GLOBAL]
728 psubw m4, m5 ; m4 = 8-dx
729 psubw m0, m6 ; m0 = 8-dy
732 pmullw m5, m0 ; m5 = dx*(8-dy) = cB
733 pmullw m7, m6 ; m7 = dx*dy = cD
734 pmullw m6, m4 ; m6 = (8-dx)*dy = cC
735 pmullw m4, m0 ; m4 = (8-dx)*(8-dy) = cA
750 punpcklbw m1, m3 ; 00 px1 | 00 px2 | 00 px3 | 00 px4
752 pmullw m1, m6 ; 2nd line * cC
753 pmullw m0, m4 ; 1st line * cA
754 paddw m0, m1 ; m0 <- result
761 paddw m0, [pw_32 GLOBAL]
763 pmullw m2, m5 ; line * cB
764 pmullw m1, m7 ; line * cD
769 packuswb m0, m3 ; 00 00 00 00 px1 px2 px3 px4
773 add r0, r1 ; dst_stride
779 jnz .finish ; width != 8 so assume 4
781 lea r0, [r10+4] ; dst
782 lea r2, [r11+4] ; src
788 mov r4d, r7m ; height
797 mov r5, r3 ; pel_offset = dx ? 1 : src_stride
803 mova m5, [pw_8 GLOBAL]
805 mova m7, [pw_4 GLOBAL]
862 %endmacro ; MC_CHROMA
870 cglobal x264_mc_chroma_ssse3, 0,6
880 imul r5d, t0d ; (x*255+8)*y
881 imul r4d, t0d ; (x*255+8)*(8-y)
884 mova m5, [pw_32 GLOBAL]
899 punpcklbw m3, [r2+r3+1]
925 mova m5, [pw_32 GLOBAL]
966 ; mc_chroma 1d ssse3 is negligibly faster, and definitely not worth the extra code size