1 ;*****************************************************************************
2 ;* x86-optimized AC-3 DSP functions
3 ;* Copyright (c) 2011 Justin Ruggles
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 ; 16777216.0f - used in ff_float_to_fixed24()
27 pf_1_24: times 4 dd 0x4B800000
29 ; used in ff_ac3_compute_mantissa_size()
31 pw_bap_mul1: dw 21846, 21846, 0, 32768, 21846, 21846, 0, 32768
32 pw_bap_mul2: dw 5, 7, 0, 7, 5, 7, 0, 7
34 ; used in ff_ac3_extract_exponents()
36 pd_151: times 4 dd 151
38 ; used in ff_apply_window_int16()
39 pb_revwords: SHUFFLE_MASK_W 7, 6, 5, 4, 3, 2, 1, 0
40 pd_16384: times 4 dd 16384
44 ;-----------------------------------------------------------------------------
45 ; void ff_ac3_exponent_min(uint8_t *exp, int num_reuse_blocks, int nb_coefs)
46 ;-----------------------------------------------------------------------------
48 %macro AC3_EXPONENT_MIN 0
49 cglobal ac3_exponent_min, 3, 4, 2, exp, reuse_blks, expn, offset
54 mov offsetq, reuse_blksq
55 mova m0, [expq+offsetq]
59 PMINUB m0, [expq+offsetq], m1
73 %if HAVE_MMXEXT_EXTERNAL
74 %define LOOP_ALIGN ALIGN 16
78 %if HAVE_SSE2_EXTERNAL
84 ;-----------------------------------------------------------------------------
85 ; int ff_ac3_max_msb_abs_int16(const int16_t *src, int len)
87 ; This function uses 2 different methods to calculate a valid result.
88 ; 1) logical 'or' of abs of each element
89 ; This is used for ssse3 because of the pabsw instruction.
90 ; It is also used for mmx because of the lack of min/max instructions.
91 ; 2) calculate min/max for the array, then or(abs(min),abs(max))
92 ; This is used for mmxext and sse2 because they have pminsw/pmaxsw.
93 ;-----------------------------------------------------------------------------
95 ; logical 'or' of 4 or 8 words in an mmx or xmm register into the low word
96 %macro OR_WORDS_HORIZ 2 ; src, tmp
100 pshuflw %2, %1, q0032
102 pshuflw %2, %1, q0001
104 %elif cpuflag(mmxext)
119 %macro AC3_MAX_MSB_ABS_INT16 1
120 cglobal ac3_max_msb_abs_int16, 2,2,5, src, len
126 mova m1, [srcq+mmsize]
132 %if notcpuflag(ssse3)
134 mova m1, [srcq+mmsize]
137 ; using memory args is faster for ssse3
139 pabsw m1, [srcq+mmsize]
151 OR_WORDS_HORIZ m2, m0
158 AC3_MAX_MSB_ABS_INT16 or_abs
160 AC3_MAX_MSB_ABS_INT16 min_max
162 AC3_MAX_MSB_ABS_INT16 min_max
164 AC3_MAX_MSB_ABS_INT16 or_abs
166 ;-----------------------------------------------------------------------------
167 ; macro used for ff_ac3_lshift_int16() and ff_ac3_rshift_int32()
168 ;-----------------------------------------------------------------------------
170 %macro AC3_SHIFT 3 ; l/r, 16/32, shift instruction, instruction set
171 cglobal ac3_%1shift_int%2, 3, 3, 5, src, len, shift
175 mova m2, [srcq+mmsize ]
176 mova m3, [srcq+mmsize*2]
177 mova m4, [srcq+mmsize*3]
183 mova [srcq+mmsize ], m2
184 mova [srcq+mmsize*2], m3
185 mova [srcq+mmsize*3], m4
187 sub lend, mmsize*32/%2
193 ;-----------------------------------------------------------------------------
194 ; void ff_ac3_lshift_int16(int16_t *src, unsigned int len, unsigned int shift)
195 ;-----------------------------------------------------------------------------
198 AC3_SHIFT l, 16, psllw
200 AC3_SHIFT l, 16, psllw
202 ;-----------------------------------------------------------------------------
203 ; void ff_ac3_rshift_int32(int32_t *src, unsigned int len, unsigned int shift)
204 ;-----------------------------------------------------------------------------
207 AC3_SHIFT r, 32, psrad
209 AC3_SHIFT r, 32, psrad
211 ;-----------------------------------------------------------------------------
212 ; void ff_float_to_fixed24(int32_t *dst, const float *src, unsigned int len)
213 ;-----------------------------------------------------------------------------
215 ; The 3DNow! version is not bit-identical because pf2id uses truncation rather
216 ; than round-to-nearest.
218 cglobal float_to_fixed24, 3, 3, 0, dst, src, len
245 cglobal float_to_fixed24, 3, 3, 3, dst, src, len
270 cglobal float_to_fixed24, 3, 3, 9, dst, src, len
274 movaps m2, [srcq+16 ]
275 movaps m3, [srcq+32 ]
276 movaps m4, [srcq+48 ]
278 movaps m5, [srcq+64 ]
279 movaps m6, [srcq+80 ]
280 movaps m7, [srcq+96 ]
281 movaps m8, [srcq+112]
304 movdqa [dstq+16 ], m2
305 movdqa [dstq+32 ], m3
306 movdqa [dstq+48 ], m4
308 movdqa [dstq+64 ], m5
309 movdqa [dstq+80 ], m6
310 movdqa [dstq+96 ], m7
311 movdqa [dstq+112], m8
323 ;------------------------------------------------------------------------------
324 ; int ff_ac3_compute_mantissa_size(uint16_t mant_cnt[6][16])
325 ;------------------------------------------------------------------------------
327 %macro PHADDD4 2 ; xmm src, xmm tmp
335 cglobal ac3_compute_mantissa_size, 1, 2, 4, mant_cnt, sum
336 movdqa m0, [mant_cntq ]
337 movdqa m1, [mant_cntq+ 1*16]
338 paddw m0, [mant_cntq+ 2*16]
339 paddw m1, [mant_cntq+ 3*16]
340 paddw m0, [mant_cntq+ 4*16]
341 paddw m1, [mant_cntq+ 5*16]
342 paddw m0, [mant_cntq+ 6*16]
343 paddw m1, [mant_cntq+ 7*16]
344 paddw m0, [mant_cntq+ 8*16]
345 paddw m1, [mant_cntq+ 9*16]
346 paddw m0, [mant_cntq+10*16]
347 paddw m1, [mant_cntq+11*16]
348 pmaddwd m0, [ac3_bap_bits ]
349 pmaddwd m1, [ac3_bap_bits+16]
353 movdqa m3, [pw_bap_mul1]
354 movhpd m0, [mant_cntq +2]
355 movlpd m0, [mant_cntq+1*32+2]
356 movhpd m1, [mant_cntq+2*32+2]
357 movlpd m1, [mant_cntq+3*32+2]
358 movhpd m2, [mant_cntq+4*32+2]
359 movlpd m2, [mant_cntq+5*32+2]
365 pmaddwd m0, [pw_bap_mul2]
371 ;------------------------------------------------------------------------------
372 ; void ff_ac3_extract_exponents(uint8_t *exp, int32_t *coef, int nb_coefs)
373 ;------------------------------------------------------------------------------
375 %macro PABSD 1-2 ; src/dst, unused
386 %macro AC3_EXTRACT_EXPONENTS 0
387 cglobal ac3_extract_exponents, 3, 3, 4, exp, coef, len
389 lea coefq, [coefq+4*lenq]
394 ; move 4 32-bit coefs to xmm0
395 mova m0, [coefq+4*lenq]
398 ; convert to float and extract exponents
405 ; move the lowest byte in each of 4 dwords to the low dword
406 ; NOTE: We cannot just extract the low bytes with pshufb because the dword
407 ; result for 16777215 is -1 due to float inaccuracy. Using packuswb
408 ; clips this to 0, which is the correct exponent.
418 %if HAVE_SSE2_EXTERNAL
420 AC3_EXTRACT_EXPONENTS
422 %if HAVE_SSSE3_EXTERNAL
424 AC3_EXTRACT_EXPONENTS
427 ;-----------------------------------------------------------------------------
428 ; void ff_apply_window_int16(int16_t *output, const int16_t *input,
429 ; const int16_t *window, unsigned int len)
430 ;-----------------------------------------------------------------------------
432 %macro REVERSE_WORDS 1-2
433 %if cpuflag(ssse3) && notcpuflag(atom)
439 %elif cpuflag(mmxext)
445 %if cpuflag(ssse3) ; dst, src, unused
446 ; dst = ((dst * src) + (1<<14)) >> 15
448 %elif cpuflag(mmxext) ; dst, src, temp
449 ; dst = (dst * src) >> 15
450 ; pmulhw cuts off the bottom bit, so we have to lshift by 1 and add it back
451 ; in from the pmullw result.
461 %macro APPLY_WINDOW_INT16 1 ; %1 bitexact version
463 cglobal apply_window_int16, 4,5,6, output, input, window, offset, offset2
465 cglobal apply_window_int16_round, 4,5,6, output, input, window, offset, offset2
467 lea offset2q, [offsetq-mmsize]
468 %if cpuflag(ssse3) && notcpuflag(atom)
469 mova m5, [pb_revwords]
476 ; This version does the 16x16->16 multiplication in-place without expanding
477 ; to 32-bit. The ssse3 version is bit-identical.
478 mova m0, [windowq+offset2q]
479 mova m1, [ inputq+offset2q]
482 pmulhrsw m0, [ inputq+offsetq ]
483 mova [outputq+offset2q], m1
484 mova [outputq+offsetq ], m0
486 ; This version expands 16-bit to 32-bit, multiplies by the window,
487 ; adds 16384 for rounding, right shifts 15, then repacks back to words to
488 ; save to the output. The window is reversed for the second half.
489 mova m3, [windowq+offset2q]
490 mova m4, [ inputq+offset2q]
504 mova [outputq+offset2q], m0
506 mova m4, [ inputq+offsetq]
520 mova [outputq+offsetq], m0
522 ; This version does the 16x16->16 multiplication in-place without expanding
523 ; to 32-bit. The mmxext and sse2 versions do not use rounding, and
524 ; therefore are not bit-identical to the C version.
525 mova m0, [windowq+offset2q]
526 mova m1, [ inputq+offset2q]
527 mova m2, [ inputq+offsetq ]
528 MUL16FIXED m1, m0, m3
530 MUL16FIXED m2, m0, m3
531 mova [outputq+offset2q], m1
532 mova [outputq+offsetq ], m2