1 ;******************************************************************************
2 ;* x86-optimized horizontal line scaling functions
3 ;* Copyright (c) 2011 Ronald S. Bultje <rsbultje@gmail.com>
5 ;* This file is part of FFmpeg.
7 ;* FFmpeg is free software; you can redistribute it and/or
8 ;* modify it under the terms of the GNU Lesser General Public
9 ;* License as published by the Free Software Foundation; either
10 ;* version 2.1 of the License, or (at your option) any later version.
12 ;* FFmpeg is distributed in the hope that it will be useful,
13 ;* but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 ;* Lesser General Public License for more details.
17 ;* You should have received a copy of the GNU Lesser General Public
18 ;* License along with FFmpeg; if not, write to the Free Software
19 ;* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 ;******************************************************************************
22 %include "libavutil/x86/x86util.asm"
26 max_19bit_int: times 4 dd 0x7ffff
27 max_19bit_flt: times 4 dd 524287.0
28 minshort: times 8 dw 0x8000
29 unicoeff: times 4 dd 0x20000000
33 ;-----------------------------------------------------------------------------
34 ; horizontal line scaling
36 ; void hscale<source_width>to<intermediate_nbits>_<filterSize>_<opt>
37 ; (SwsContext *c, int{16,32}_t *dst,
38 ; int dstW, const uint{8,16}_t *src,
39 ; const int16_t *filter,
40 ; const int32_t *filterPos, int filterSize);
42 ; Scale one horizontal line. Input is either 8-bit width or 16-bit width
43 ; ($source_width can be either 8, 9, 10 or 16, difference is whether we have to
44 ; downscale before multiplying). Filter is 14 bits. Output is either 15 bits
45 ; (in int16_t) or 19 bits (in int32_t), as given in $intermediate_nbits. Each
46 ; output pixel is generated from $filterSize input pixels, the position of
47 ; the first pixel is given in filterPos[nOutputPixel].
48 ;-----------------------------------------------------------------------------
50 ; SCALE_FUNC source_width, intermediate_nbits, filtersize, filtersuffix, n_args, n_xmm
53 cglobal hscale%1to%2_%4, %5, 7, %6, pos0, dst, w, src, filter, fltpos, pos1
55 cglobal hscale%1to%2_%4, %5, 10, %6, pos0, dst, w, srcmem, filter, fltpos, fltsize
65 mova m2, [max_19bit_int]
67 mova m2, [max_19bit_int]
69 mova m2, [max_19bit_flt]
70 %endif ; mmx/sse2/ssse3/sse4
93 shl wq, 1 ; this allows *16 (i.e. now *8) in lea instructions for the 8-tap filter
98 lea filterq, [filterq+wq*8]
100 lea dstq, [dstq+wq*(2>>wshr)]
102 lea dstq, [dstq+wq*(4>>wshr)]
104 lea fltposq, [fltposq+wq*(4>>wshr)]
108 %if %3 == 4 ; filterSize == 4 scaling
109 ; load 2x4 or 4x4 source pixels into m0/m1
110 mov32 pos0q, dword [fltposq+wq*4+ 0] ; filterPos[0]
111 mov32 pos1q, dword [fltposq+wq*4+ 4] ; filterPos[1]
112 movlh m0, [srcq+pos0q*srcmul] ; src[filterPos[0] + {0,1,2,3}]
114 movlh m1, [srcq+pos1q*srcmul] ; src[filterPos[1] + {0,1,2,3}]
117 movhps m0, [srcq+pos1q*srcmul] ; src[filterPos[1] + {0,1,2,3}]
119 movd m4, [srcq+pos1q*srcmul] ; src[filterPos[1] + {0,1,2,3}]
121 mov32 pos0q, dword [fltposq+wq*4+ 8] ; filterPos[2]
122 mov32 pos1q, dword [fltposq+wq*4+12] ; filterPos[3]
123 movlh m1, [srcq+pos0q*srcmul] ; src[filterPos[2] + {0,1,2,3}]
125 movhps m1, [srcq+pos1q*srcmul] ; src[filterPos[3] + {0,1,2,3}]
127 movd m5, [srcq+pos1q*srcmul] ; src[filterPos[3] + {0,1,2,3}]
131 %endif ; mmsize == 8/16
133 punpcklbw m0, m3 ; byte -> word
134 punpcklbw m1, m3 ; byte -> word
137 ; multiply with filter coefficients
138 %if %1 == 16 ; pmaddwd needs signed adds, so this moves unsigned -> signed, we'll
139 ; add back 0x8000 * sum(coeffs) after the horizontal add
143 pmaddwd m0, [filterq+wq*8+mmsize*0] ; *= filter[{0,1,..,6,7}]
144 pmaddwd m1, [filterq+wq*8+mmsize*1] ; *= filter[{8,9,..,14,15}]
146 ; add up horizontally (4 srcpix * 4 coefficients -> 1 dstpix)
147 %if mmsize == 8 ; mmx
152 %elif notcpuflag(ssse3) ; sse2
154 shufps m0, m1, 10001000b
155 shufps m4, m1, 11011101b
158 phaddd m0, m1 ; filter[{ 0, 1, 2, 3}]*src[filterPos[0]+{0,1,2,3}],
159 ; filter[{ 4, 5, 6, 7}]*src[filterPos[1]+{0,1,2,3}],
160 ; filter[{ 8, 9,10,11}]*src[filterPos[2]+{0,1,2,3}],
161 ; filter[{12,13,14,15}]*src[filterPos[3]+{0,1,2,3}]
162 %endif ; mmx/sse2/ssse3/sse4
163 %else ; %3 == 8, i.e. filterSize == 8 scaling
164 ; load 2x8 or 4x8 source pixels into m0, m1, m4 and m5
165 mov32 pos0q, dword [fltposq+wq*2+0] ; filterPos[0]
166 mov32 pos1q, dword [fltposq+wq*2+4] ; filterPos[1]
167 movbh m0, [srcq+ pos0q *srcmul] ; src[filterPos[0] + {0,1,2,3,4,5,6,7}]
169 movbh m1, [srcq+(pos0q+4)*srcmul] ; src[filterPos[0] + {4,5,6,7}]
170 movbh m4, [srcq+ pos1q *srcmul] ; src[filterPos[1] + {0,1,2,3}]
171 movbh m5, [srcq+(pos1q+4)*srcmul] ; src[filterPos[1] + {4,5,6,7}]
173 movbh m1, [srcq+ pos1q *srcmul] ; src[filterPos[1] + {0,1,2,3,4,5,6,7}]
174 mov32 pos0q, dword [fltposq+wq*2+8] ; filterPos[2]
175 mov32 pos1q, dword [fltposq+wq*2+12] ; filterPos[3]
176 movbh m4, [srcq+ pos0q *srcmul] ; src[filterPos[2] + {0,1,2,3,4,5,6,7}]
177 movbh m5, [srcq+ pos1q *srcmul] ; src[filterPos[3] + {0,1,2,3,4,5,6,7}]
178 %endif ; mmsize == 8/16
180 punpcklbw m0, m3 ; byte -> word
181 punpcklbw m1, m3 ; byte -> word
182 punpcklbw m4, m3 ; byte -> word
183 punpcklbw m5, m3 ; byte -> word
187 %if %1 == 16 ; pmaddwd needs signed adds, so this moves unsigned -> signed, we'll
188 ; add back 0x8000 * sum(coeffs) after the horizontal add
194 pmaddwd m0, [filterq+wq*8+mmsize*0] ; *= filter[{0,1,..,6,7}]
195 pmaddwd m1, [filterq+wq*8+mmsize*1] ; *= filter[{8,9,..,14,15}]
196 pmaddwd m4, [filterq+wq*8+mmsize*2] ; *= filter[{16,17,..,22,23}]
197 pmaddwd m5, [filterq+wq*8+mmsize*3] ; *= filter[{24,25,..,30,31}]
199 ; add up horizontally (8 srcpix * 8 coefficients -> 1 dstpix)
207 %elif notcpuflag(ssse3) ; sse2
213 ; emulate horizontal add as transpose + vertical add
227 ; FIXME if we rearrange the filter in pairs of 4, we can
228 ; load pixels likewise and use 2 x paddd + phaddd instead
229 ; of 3 x phaddd here, faster on older cpus
232 phaddd m0, m4 ; filter[{ 0, 1,..., 6, 7}]*src[filterPos[0]+{0,1,...,6,7}],
233 ; filter[{ 8, 9,...,14,15}]*src[filterPos[1]+{0,1,...,6,7}],
234 ; filter[{16,17,...,22,23}]*src[filterPos[2]+{0,1,...,6,7}],
235 ; filter[{24,25,...,30,31}]*src[filterPos[3]+{0,1,...,6,7}]
236 %endif ; mmx/sse2/ssse3/sse4
239 %else ; %3 == X, i.e. any filterSize scaling
243 %else ; %4 == X || %4 == X8
250 movsxd fltsizeq, fltsized ; filterSize
251 lea srcendq, [srcmemq+(fltsizeq-dlt)*srcmul] ; &src[filterSize&~4]
256 lea pos0q, [srcmemq+(fltsizeq-dlt)*srcmul] ; &src[filterSize&~4]
259 lea fltposq, [fltposq+wq*4]
261 lea dstq, [dstq+wq*2]
263 lea dstq, [dstq+wq*4]
265 movifnidn dstmp, dstq
269 mov32 pos0q, dword [fltposq+wq*4+0] ; filterPos[0]
270 mov32 pos1q, dword [fltposq+wq*4+4] ; filterPos[1]
271 ; FIXME maybe do 4px/iteration on x86-64 (x86-32 wouldn't have enough regs)?
277 ; load 2x4 (mmx) or 2x8 (sse) source pixels into m0/m1 -> m4/m5
278 movbh m0, [srcq+ pos0q *srcmul] ; src[filterPos[0] + {0,1,2,3(,4,5,6,7)}]
279 movbh m1, [srcq+(pos1q+dlt)*srcmul] ; src[filterPos[1] + {0,1,2,3(,4,5,6,7)}]
286 %if %1 == 16 ; pmaddwd needs signed adds, so this moves unsigned -> signed, we'll
287 ; add back 0x8000 * sum(coeffs) after the horizontal add
291 pmaddwd m0, [filterq] ; filter[{0,1,2,3(,4,5,6,7)}]
292 pmaddwd m1, [filterq+(fltsizeq+dlt)*2]; filter[filtersize+{0,1,2,3(,4,5,6,7)}]
296 add srcq, srcmul*mmsize/2
297 cmp srcq, srcendq ; while (src += 4) < &src[filterSize]
301 mov32 pos1q, dword [fltposq+wq*4+4] ; filterPos[1]
302 movlh m0, [srcq+ pos0q *srcmul] ; split last 4 srcpx of dstpx[0]
303 sub pos1q, fltsizeq ; and first 4 srcpx of dstpx[1]
305 movhps m0, [srcq+(pos1q+dlt)*srcmul]
307 movd m1, [srcq+(pos1q+dlt)*srcmul]
313 %if %1 == 16 ; pmaddwd needs signed adds, so this moves unsigned -> signed, we'll
314 ; add back 0x8000 * sum(coeffs) after the horizontal add
317 pmaddwd m0, [filterq]
320 lea filterq, [filterq+(fltsizeq+dlt)*2]
322 %if mmsize == 8 ; mmx
328 %if notcpuflag(ssse3) ; sse2
335 %endif ; sse2/ssse3/sse4
339 %if notcpuflag(ssse3) ; sse2
340 pshufd m4, m4, 11011000b
346 %endif ; sse2/ssse3/sse4
347 %endif ; mmsize == 8/16
350 %if %1 == 16 ; add 0x8000 * sum(coeffs), i.e. back from signed -> unsigned
355 psrad m0, 14 + %1 - %2
357 movifnidn dstq, dstmp
362 movh [dstq+wq*(2>>wshr)], m0
369 mova [dstq+wq*(4>>wshr)], m0
375 add wq, (mmsize<<wshr)/4 ; both 8tap and 4tap really only do 4 pixels (or for mmx: 2 pixels)
376 ; per iteration. see "shl wq,1" above as for why we do this
384 ; SCALE_FUNCS source_width, intermediate_nbits, n_xmm
386 SCALE_FUNC %1, %2, 4, 4, 6, %3
387 SCALE_FUNC %1, %2, 8, 8, 6, %3
389 SCALE_FUNC %1, %2, X, X, 7, %3
391 SCALE_FUNC %1, %2, X, X4, 7, %3
392 SCALE_FUNC %1, %2, X, X8, 7, %3
396 ; SCALE_FUNCS2 8_xmm_args, 9to10_xmm_args, 16_xmm_args
397 %macro SCALE_FUNCS2 3
399 SCALE_FUNCS 8, 15, %1
400 SCALE_FUNCS 9, 15, %2
401 SCALE_FUNCS 10, 15, %2
402 SCALE_FUNCS 12, 15, %2
403 SCALE_FUNCS 14, 15, %2
404 SCALE_FUNCS 16, 15, %3
406 SCALE_FUNCS 8, 19, %1
407 SCALE_FUNCS 9, 19, %2
408 SCALE_FUNCS 10, 19, %2
409 SCALE_FUNCS 12, 19, %2
410 SCALE_FUNCS 14, 19, %2
411 SCALE_FUNCS 16, 19, %3