1 ;******************************************************************************
2 ;* Copyright (c) 2012 Michael Niedermayer
3 ;* Copyright (c) 2014 James Almer <jamrial <at> gmail.com>
4 ;* Copyright (c) 2014 Ronald S. Bultje <rsbultje@gmail.com>
6 ;* This file is part of FFmpeg.
8 ;* FFmpeg is free software; you can redistribute it and/or
9 ;* modify it under the terms of the GNU Lesser General Public
10 ;* License as published by the Free Software Foundation; either
11 ;* version 2.1 of the License, or (at your option) any later version.
13 ;* FFmpeg is distributed in the hope that it will be useful,
14 ;* but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 ;* Lesser General Public License for more details.
18 ;* You should have received a copy of the GNU Lesser General Public
19 ;* License along with FFmpeg; if not, write to the Free Software
20 ;* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 ;******************************************************************************
23 %include "libavutil/x86/x86util.asm"
33 .filter_bank: pointer 1
34 .filter_length: resd 1
36 .ideal_dst_incr: resd 1
43 .compensation_distance: resd 1
47 ; there's a few more here but we only care about the first few
58 %macro RESAMPLE_FNS 3-5 ; format [float or int16], bps, log2_bps, float op suffix [s or d], 1.0 constant
59 ; int resample_common_$format(ResampleContext *ctx, $format *dst,
60 ; const $format *src, int size, int update_ctx)
61 %if ARCH_X86_64 ; unix64 and win64
62 cglobal resample_common_%1, 0, 15, 2, ctx, dst, src, phase_shift, index, frac, \
63 dst_incr_mod, size, min_filter_count_x4, \
64 min_filter_len_x4, dst_incr_div, src_incr, \
65 phase_mask, dst_end, filter_bank
67 ; use red-zone for variable storage
68 %define ctx_stackq [rsp-0x8]
69 %define src_stackq [rsp-0x10]
71 %define update_context_stackd r4m
73 %define update_context_stackd [rsp-0x14]
76 ; load as many variables in registers as possible; for the rest, store
77 ; on stack so that we have 'ctx' available as one extra register
79 mov phase_maskd, [ctxq+ResampleContext.phase_mask]
81 mov update_context_stackd, r4d
83 mov indexd, [ctxq+ResampleContext.index]
84 mov fracd, [ctxq+ResampleContext.frac]
85 mov dst_incr_modd, [ctxq+ResampleContext.dst_incr_mod]
86 mov filter_bankq, [ctxq+ResampleContext.filter_bank]
87 mov src_incrd, [ctxq+ResampleContext.src_incr]
89 mov min_filter_len_x4d, [ctxq+ResampleContext.filter_length]
90 mov dst_incr_divd, [ctxq+ResampleContext.dst_incr_div]
91 shl min_filter_len_x4d, %3
92 lea dst_endq, [dstq+sizeq*%2]
95 mov ecx, [ctxq+ResampleContext.phase_shift]
96 mov edi, [ctxq+ResampleContext.filter_alloc]
98 DEFINE_ARGS filter_alloc, dst, src, phase_shift, index, frac, dst_incr_mod, \
99 filter, min_filter_count_x4, min_filter_len_x4, dst_incr_div, \
100 src_incr, phase_mask, dst_end, filter_bank
102 mov R9d, [ctxq+ResampleContext.filter_alloc]
103 mov ecx, [ctxq+ResampleContext.phase_shift]
105 DEFINE_ARGS phase_shift, dst, src, filter_alloc, index, frac, dst_incr_mod, \
106 filter, min_filter_count_x4, min_filter_len_x4, dst_incr_div, \
107 src_incr, phase_mask, dst_end, filter_bank
110 neg min_filter_len_x4q
111 sub filter_bankq, min_filter_len_x4q
112 sub srcq, min_filter_len_x4q
115 cglobal resample_common_%1, 1, 7, 2, ctx, phase_shift, dst, frac, \
116 index, min_filter_length_x4, filter_bank
118 ; push temp variables to stack
119 %define ctx_stackq r0mp
120 %define src_stackq r2mp
121 %define update_context_stackd r4m
126 PUSH dword [ctxq+ResampleContext.dst_incr_div]
127 PUSH dword [ctxq+ResampleContext.dst_incr_mod]
128 PUSH dword [ctxq+ResampleContext.filter_alloc]
130 PUSH dword [ctxq+ResampleContext.phase_mask]
131 PUSH dword [ctxq+ResampleContext.src_incr]
132 mov min_filter_length_x4d, [ctxq+ResampleContext.filter_length]
133 mov indexd, [ctxq+ResampleContext.index]
134 shl min_filter_length_x4d, %3
135 mov fracd, [ctxq+ResampleContext.frac]
136 neg min_filter_length_x4q
137 mov filter_bankq, [ctxq+ResampleContext.filter_bank]
138 sub r2mp, min_filter_length_x4q
139 sub filter_bankq, min_filter_length_x4q
140 PUSH min_filter_length_x4q
142 mov phase_shiftd, [ctxq+ResampleContext.phase_shift]
144 DEFINE_ARGS src, phase_shift, dst, frac, index, min_filter_count_x4, filter
146 %define filter_bankq dword [rsp+0x0]
147 %define min_filter_length_x4q dword [rsp+0x4]
148 %define src_incrd dword [rsp+0x8]
149 %define phase_maskd dword [rsp+0xc]
150 %define dst_endq dword [rsp+0x10]
151 %define filter_allocd dword [rsp+0x14]
152 %define dst_incr_modd dword [rsp+0x18]
153 %define dst_incr_divd dword [rsp+0x1c]
159 mov filterd, filter_allocd
162 mov min_filter_count_x4q, min_filter_len_x4q
163 lea filterq, [filter_bankq+filterq*%2]
165 mov min_filter_count_x4q, filter_bankq
166 lea filterq, [min_filter_count_x4q+filterq*%2]
167 mov min_filter_count_x4q, min_filter_length_x4q
177 movu m1, [srcq+min_filter_count_x4q*1]
179 PMADCSWD m0, m1, [filterq+min_filter_count_x4q*1], m0, m1
181 %if cpuflag(fma4) || cpuflag(fma3)
182 fmaddp%4 m0, m1, [filterq+min_filter_count_x4q*1], m0
184 mulp%4 m1, m1, [filterq+min_filter_count_x4q*1]
188 add min_filter_count_x4q, mmsize
194 add fracd, dst_incr_modd
196 add indexd, dst_incr_divd
199 ; horizontal sum & store
201 vextractf128 xm1, m0, 0x1
207 shufps xm1, xm0, xm0, q0001
209 add fracd, dst_incr_modd
211 add indexd, dst_incr_divd
220 DEFINE_ARGS filter_alloc, dst, src, phase_shift, index, frac, dst_incr_mod, \
221 index_incr, min_filter_count_x4, min_filter_len_x4, dst_incr_div, \
222 src_incr, phase_mask, dst_end, filter_bank
224 DEFINE_ARGS phase_shift, dst, src, filter_alloc, index, frac, dst_incr_mod, \
225 index_incr, min_filter_count_x4, min_filter_len_x4, dst_incr_div, \
226 src_incr, phase_mask, dst_end, filter_bank
228 DEFINE_ARGS src, phase_shift, dst, frac, index, index_incr
232 mov index_incrd, indexd
234 and indexd, phase_maskd
235 sar index_incrd, phase_shiftb
236 lea srcq, [srcq+index_incrq*%2]
241 DEFINE_ARGS ctx, dst, src, phase_shift, index, frac
243 DEFINE_ARGS src, ctx, update_context, frac, index
246 cmp dword update_context_stackd, 0
248 ; strictly speaking, the function should always return the consumed
249 ; number of bytes; however, we only use the value if update_context
250 ; is true, so let's just leave it uninitialized otherwise
253 mov [ctxq+ResampleContext.frac ], fracd
255 mov [ctxq+ResampleContext.index], indexd
264 ; int resample_linear_$format(ResampleContext *ctx, float *dst,
265 ; const float *src, int size, int update_ctx)
266 %if ARCH_X86_64 ; unix64 and win64
268 cglobal resample_linear_%1, 0, 15, 5, ctx, dst, phase_mask, phase_shift, index, frac, \
269 size, dst_incr_mod, min_filter_count_x4, \
270 min_filter_len_x4, dst_incr_div, src_incr, \
271 src, dst_end, filter_bank
275 cglobal resample_linear_%1, 0, 15, 5, ctx, phase_mask, src, phase_shift, index, frac, \
276 size, dst_incr_mod, min_filter_count_x4, \
277 min_filter_len_x4, dst_incr_div, src_incr, \
278 dst, dst_end, filter_bank
283 ; use red-zone for variable storage
284 %define ctx_stackq [rsp-0x8]
285 %define src_stackq [rsp-0x10]
286 %define phase_mask_stackd [rsp-0x14]
288 %define update_context_stackd r4m
290 %define update_context_stackd [rsp-0x18]
293 ; load as many variables in registers as possible; for the rest, store
294 ; on stack so that we have 'ctx' available as one extra register
296 mov phase_maskd, [ctxq+ResampleContext.phase_mask]
298 mov update_context_stackd, r4d
300 mov indexd, [ctxq+ResampleContext.index]
301 mov fracd, [ctxq+ResampleContext.frac]
302 mov dst_incr_modd, [ctxq+ResampleContext.dst_incr_mod]
303 mov filter_bankq, [ctxq+ResampleContext.filter_bank]
304 mov src_incrd, [ctxq+ResampleContext.src_incr]
306 mov phase_mask_stackd, phase_maskd
307 mov min_filter_len_x4d, [ctxq+ResampleContext.filter_length]
311 cvtsi2s%4 xm0, src_incrd
315 mov dst_incr_divd, [ctxq+ResampleContext.dst_incr_div]
316 shl min_filter_len_x4d, %3
317 lea dst_endq, [dstq+sizeq*%2]
320 mov ecx, [ctxq+ResampleContext.phase_shift]
321 mov edi, [ctxq+ResampleContext.filter_alloc]
323 DEFINE_ARGS filter_alloc, dst, filter2, phase_shift, index, frac, filter1, \
324 dst_incr_mod, min_filter_count_x4, min_filter_len_x4, \
325 dst_incr_div, src_incr, src, dst_end, filter_bank
327 mov R9d, [ctxq+ResampleContext.filter_alloc]
328 mov ecx, [ctxq+ResampleContext.phase_shift]
330 DEFINE_ARGS phase_shift, filter2, src, filter_alloc, index, frac, filter1, \
331 dst_incr_mod, min_filter_count_x4, min_filter_len_x4, \
332 dst_incr_div, src_incr, dst, dst_end, filter_bank
335 neg min_filter_len_x4q
336 sub filter_bankq, min_filter_len_x4q
337 sub srcq, min_filter_len_x4q
340 cglobal resample_linear_%1, 1, 7, 5, ctx, min_filter_length_x4, filter2, \
341 frac, index, dst, filter_bank
343 ; push temp variables to stack
344 %define ctx_stackq r0mp
345 %define src_stackq r2mp
346 %define update_context_stackd r4m
351 PUSH dword [ctxq+ResampleContext.dst_incr_div]
353 mov r3, dword [ctxq+ResampleContext.filter_alloc]
354 PUSH dword [ctxq+ResampleContext.dst_incr_mod]
358 mov r3, dword [ctxq+ResampleContext.src_incr]
359 PUSH dword [ctxq+ResampleContext.phase_mask]
368 mov min_filter_length_x4d, [ctxq+ResampleContext.filter_length]
369 mov indexd, [ctxq+ResampleContext.index]
370 shl min_filter_length_x4d, %3
371 mov fracd, [ctxq+ResampleContext.frac]
372 neg min_filter_length_x4q
373 mov filter_bankq, [ctxq+ResampleContext.filter_bank]
374 sub r2mp, min_filter_length_x4q
375 sub filter_bankq, min_filter_length_x4q
376 PUSH min_filter_length_x4q
378 PUSH dword [ctxq+ResampleContext.phase_shift]
380 DEFINE_ARGS filter1, min_filter_count_x4, filter2, frac, index, dst, src
382 %define phase_shift_stackd dword [rsp+0x0]
383 %define filter_bankq dword [rsp+0x4]
384 %define min_filter_length_x4q dword [rsp+0x8]
385 %define src_incrd dword [rsp+0xc]
386 %define phase_mask_stackd dword [rsp+0x10]
387 %define filter_alloc_x4q dword [rsp+0x14]
388 %define filter_allocd dword [rsp+0x18]
389 %define dst_incr_modd dword [rsp+0x1c]
390 %define dst_endq dword [rsp+0x20]
391 %define dst_incr_divd dword [rsp+0x24]
397 mov filter1d, filter_allocd
398 imul filter1d, indexd
400 mov min_filter_count_x4q, min_filter_len_x4q
401 lea filter1q, [filter_bankq+filter1q*%2]
402 lea filter2q, [filter1q+filter_allocq*%2]
404 mov min_filter_count_x4q, filter_bankq
405 lea filter1q, [min_filter_count_x4q+filter1q*%2]
406 mov min_filter_count_x4q, min_filter_length_x4q
407 mov filter2q, filter1q
408 add filter2q, filter_alloc_x4q
420 movu m1, [srcq+min_filter_count_x4q*1]
423 vpmadcswd m2, m1, [filter2q+min_filter_count_x4q*1], m2
424 vpmadcswd m0, m1, [filter1q+min_filter_count_x4q*1], m0
426 pmaddwd m3, m1, [filter2q+min_filter_count_x4q*1]
427 pmaddwd m1, [filter1q+min_filter_count_x4q*1]
432 %if cpuflag(fma4) || cpuflag(fma3)
433 fmaddp%4 m2, m1, [filter2q+min_filter_count_x4q*1], m2
434 fmaddp%4 m0, m1, [filter1q+min_filter_count_x4q*1], m0
436 mulp%4 m3, m1, [filter2q+min_filter_count_x4q*1]
437 mulp%4 m1, m1, [filter1q+min_filter_count_x4q*1]
442 add min_filter_count_x4q, mmsize
457 PSHUFLW m3, m2, q0032
458 PSHUFLW m1, m0, q0032
463 ; This is probably a really bad idea on atom and other machines with a
464 ; long transfer latency between GPRs and XMMs (atom). However, it does
465 ; make the clip a lot simpler...
467 add indexd, dst_incr_divd
471 add fracd, dst_incr_modd
477 ; note that for imul/idiv, I need to move filter to edx/eax for each:
478 ; - 32bit: eax=r0[filter1], edx=r2[filter2]
479 ; - win64: eax=r6[filter1], edx=r1[todo]
480 ; - unix64: eax=r6[filter1], edx=r2[todo]
482 ; val += (v2 - val) * (FELEML) frac / c->src_incr;
484 vextractf128 xm1, m0, 0x1
485 vextractf128 xm3, m2, 0x1
492 shufp%4 xm1, xm1, q0000
493 %if cpuflag(fma4) || cpuflag(fma3)
494 fmaddp%4 xm0, xm2, xm1, xm0
500 ; horizontal sum & store
504 shufps xm1, xm0, xm0, q0001
506 add fracd, dst_incr_modd
508 add indexd, dst_incr_divd
517 DEFINE_ARGS filter_alloc, dst, filter2, phase_shift, index, frac, index_incr, \
518 dst_incr_mod, min_filter_count_x4, min_filter_len_x4, \
519 dst_incr_div, src_incr, src, dst_end, filter_bank
521 DEFINE_ARGS phase_shift, filter2, src, filter_alloc, index, frac, index_incr, \
522 dst_incr_mod, min_filter_count_x4, min_filter_len_x4, \
523 dst_incr_div, src_incr, dst, dst_end, filter_bank
525 DEFINE_ARGS filter1, phase_shift, index_incr, frac, index, dst, src
530 mov phase_shiftd, phase_shift_stackd
532 mov index_incrd, indexd
534 and indexd, phase_mask_stackd
535 sar index_incrd, phase_shiftb
536 lea srcq, [srcq+index_incrq*%2]
541 DEFINE_ARGS ctx, dst, filter2, phase_shift, index, frac, index_incr, \
542 dst_incr_mod, min_filter_count_x4, min_filter_len_x4, \
543 dst_incr_div, src_incr, src, dst_end, filter_bank
545 DEFINE_ARGS ctx, filter2, src, phase_shift, index, frac, index_incr, \
546 dst_incr_mod, min_filter_count_x4, min_filter_len_x4, \
547 dst_incr_div, src_incr, dst, dst_end, filter_bank
549 DEFINE_ARGS filter1, ctx, update_context, frac, index, dst, src
552 cmp dword update_context_stackd, 0
554 ; strictly speaking, the function should always return the consumed
555 ; number of bytes; however, we only use the value if update_context
556 ; is true, so let's just leave it uninitialized otherwise
559 mov [ctxq+ResampleContext.frac ], fracd
561 mov [ctxq+ResampleContext.index], indexd
572 RESAMPLE_FNS float, 4, 2, s, pf_1
574 %if HAVE_AVX_EXTERNAL
576 RESAMPLE_FNS float, 4, 2, s, pf_1
578 %if HAVE_FMA3_EXTERNAL
580 RESAMPLE_FNS float, 4, 2, s, pf_1
582 %if HAVE_FMA4_EXTERNAL
584 RESAMPLE_FNS float, 4, 2, s, pf_1
589 RESAMPLE_FNS int16, 2, 1
593 RESAMPLE_FNS int16, 2, 1
594 %if HAVE_XOP_EXTERNAL
596 RESAMPLE_FNS int16, 2, 1
600 RESAMPLE_FNS double, 8, 3, d, pdbl_1