Merge remote-tracking branch 'qatar/master'
[ffmpeg.git] / libavcodec / x86 / ac3dsp_init.c
1 /*
2  * x86-optimized AC-3 DSP utils
3  * Copyright (c) 2011 Justin Ruggles
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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  */
21
22 #include "libavutil/mem.h"
23 #include "libavutil/x86/asm.h"
24 #include "libavutil/x86/cpu.h"
25 #include "dsputil_mmx.h"
26 #include "libavcodec/ac3.h"
27 #include "libavcodec/ac3dsp.h"
28
29 extern void ff_ac3_exponent_min_mmx   (uint8_t *exp, int num_reuse_blocks, int nb_coefs);
30 extern void ff_ac3_exponent_min_mmxext(uint8_t *exp, int num_reuse_blocks, int nb_coefs);
31 extern void ff_ac3_exponent_min_sse2  (uint8_t *exp, int num_reuse_blocks, int nb_coefs);
32
33 extern int ff_ac3_max_msb_abs_int16_mmx  (const int16_t *src, int len);
34 extern int ff_ac3_max_msb_abs_int16_mmxext(const int16_t *src, int len);
35 extern int ff_ac3_max_msb_abs_int16_sse2 (const int16_t *src, int len);
36 extern int ff_ac3_max_msb_abs_int16_ssse3(const int16_t *src, int len);
37
38 extern void ff_ac3_lshift_int16_mmx (int16_t *src, unsigned int len, unsigned int shift);
39 extern void ff_ac3_lshift_int16_sse2(int16_t *src, unsigned int len, unsigned int shift);
40
41 extern void ff_ac3_rshift_int32_mmx (int32_t *src, unsigned int len, unsigned int shift);
42 extern void ff_ac3_rshift_int32_sse2(int32_t *src, unsigned int len, unsigned int shift);
43
44 extern void ff_float_to_fixed24_3dnow(int32_t *dst, const float *src, unsigned int len);
45 extern void ff_float_to_fixed24_sse  (int32_t *dst, const float *src, unsigned int len);
46 extern void ff_float_to_fixed24_sse2 (int32_t *dst, const float *src, unsigned int len);
47
48 extern int ff_ac3_compute_mantissa_size_sse2(uint16_t mant_cnt[6][16]);
49
50 extern void ff_ac3_extract_exponents_3dnow(uint8_t *exp, int32_t *coef, int nb_coefs);
51 extern void ff_ac3_extract_exponents_sse2 (uint8_t *exp, int32_t *coef, int nb_coefs);
52 extern void ff_ac3_extract_exponents_ssse3(uint8_t *exp, int32_t *coef, int nb_coefs);
53
54 #if HAVE_SSE_INLINE
55
56 #define IF1(x) x
57 #define IF0(x)
58
59 #define MIX5(mono, stereo)                                      \
60     __asm__ volatile (                                          \
61         "movss           0(%2), %%xmm5          \n"             \
62         "movss           8(%2), %%xmm6          \n"             \
63         "movss          24(%2), %%xmm7          \n"             \
64         "shufps     $0, %%xmm5, %%xmm5          \n"             \
65         "shufps     $0, %%xmm6, %%xmm6          \n"             \
66         "shufps     $0, %%xmm7, %%xmm7          \n"             \
67         "1:                                     \n"             \
68         "movaps       (%0, %1), %%xmm0          \n"             \
69         "movaps  0x400(%0, %1), %%xmm1          \n"             \
70         "movaps  0x800(%0, %1), %%xmm2          \n"             \
71         "movaps  0xc00(%0, %1), %%xmm3          \n"             \
72         "movaps 0x1000(%0, %1), %%xmm4          \n"             \
73         "mulps          %%xmm5, %%xmm0          \n"             \
74         "mulps          %%xmm6, %%xmm1          \n"             \
75         "mulps          %%xmm5, %%xmm2          \n"             \
76         "mulps          %%xmm7, %%xmm3          \n"             \
77         "mulps          %%xmm7, %%xmm4          \n"             \
78  stereo("addps          %%xmm1, %%xmm0          \n")            \
79         "addps          %%xmm1, %%xmm2          \n"             \
80         "addps          %%xmm3, %%xmm0          \n"             \
81         "addps          %%xmm4, %%xmm2          \n"             \
82    mono("addps          %%xmm2, %%xmm0          \n")            \
83         "movaps         %%xmm0, (%0, %1)        \n"             \
84  stereo("movaps         %%xmm2, 0x400(%0, %1)   \n")            \
85         "add               $16, %0              \n"             \
86         "jl                 1b                  \n"             \
87         : "+&r"(i)                                              \
88         : "r"(samples[0] + len), "r"(matrix)                    \
89         : XMM_CLOBBERS("%xmm0", "%xmm1", "%xmm2", "%xmm3",      \
90                       "%xmm4", "%xmm5", "%xmm6", "%xmm7",)      \
91          "memory"                                               \
92     );
93
94 #define MIX_MISC(stereo)                                        \
95     __asm__ volatile (                                          \
96         "1:                                 \n"                 \
97         "movaps     (%3, %0), %%xmm0        \n"                 \
98  stereo("movaps       %%xmm0, %%xmm1        \n")                \
99         "mulps        %%xmm4, %%xmm0        \n"                 \
100  stereo("mulps        %%xmm5, %%xmm1        \n")                \
101         "lea    1024(%3, %0), %1            \n"                 \
102         "mov              %5, %2            \n"                 \
103         "2:                                 \n"                 \
104         "movaps         (%1), %%xmm2        \n"                 \
105  stereo("movaps       %%xmm2, %%xmm3        \n")                \
106         "mulps      (%4, %2), %%xmm2        \n"                 \
107  stereo("mulps    16(%4, %2), %%xmm3        \n")                \
108         "addps        %%xmm2, %%xmm0        \n"                 \
109  stereo("addps        %%xmm3, %%xmm1        \n")                \
110         "add           $1024, %1            \n"                 \
111         "add             $32, %2            \n"                 \
112         "jl               2b                \n"                 \
113         "movaps       %%xmm0,     (%3, %0)  \n"                 \
114  stereo("movaps       %%xmm1, 1024(%3, %0)  \n")                \
115         "add             $16, %0            \n"                 \
116         "jl               1b                \n"                 \
117         : "+&r"(i), "=&r"(j), "=&r"(k)                          \
118         : "r"(samples[0] + len), "r"(matrix_simd + in_ch),      \
119           "g"((intptr_t) - 32 * (in_ch - 1))                    \
120         : "memory"                                              \
121     );
122
123 static void ac3_downmix_sse(float (*samples)[256], float (*matrix)[2],
124                             int out_ch, int in_ch, int len)
125 {
126     int (*matrix_cmp)[2] = (int(*)[2])matrix;
127     intptr_t i, j, k;
128
129     i = -len * sizeof(float);
130     if (in_ch == 5 && out_ch == 2 &&
131         !(matrix_cmp[0][1] | matrix_cmp[2][0]   |
132           matrix_cmp[3][1] | matrix_cmp[4][0]   |
133           (matrix_cmp[1][0] ^ matrix_cmp[1][1]) |
134           (matrix_cmp[0][0] ^ matrix_cmp[2][1]))) {
135         MIX5(IF0, IF1);
136     } else if (in_ch == 5 && out_ch == 1 &&
137                matrix_cmp[0][0] == matrix_cmp[2][0] &&
138                matrix_cmp[3][0] == matrix_cmp[4][0]) {
139         MIX5(IF1, IF0);
140     } else {
141         DECLARE_ALIGNED(16, float, matrix_simd)[AC3_MAX_CHANNELS][2][4];
142         j = 2 * in_ch * sizeof(float);
143         __asm__ volatile (
144             "1:                                 \n"
145             "sub             $8, %0             \n"
146             "movss     (%2, %0), %%xmm4         \n"
147             "movss    4(%2, %0), %%xmm5         \n"
148             "shufps          $0, %%xmm4, %%xmm4 \n"
149             "shufps          $0, %%xmm5, %%xmm5 \n"
150             "movaps      %%xmm4,   (%1, %0, 4)  \n"
151             "movaps      %%xmm5, 16(%1, %0, 4)  \n"
152             "jg              1b                 \n"
153             : "+&r"(j)
154             : "r"(matrix_simd), "r"(matrix)
155             : "memory"
156         );
157         if (out_ch == 2) {
158             MIX_MISC(IF1);
159         } else {
160             MIX_MISC(IF0);
161         }
162     }
163 }
164
165 #endif /* HAVE_SSE_INLINE */
166
167 av_cold void ff_ac3dsp_init_x86(AC3DSPContext *c, int bit_exact)
168 {
169     int mm_flags = av_get_cpu_flags();
170
171     if (EXTERNAL_MMX(mm_flags)) {
172         c->ac3_exponent_min = ff_ac3_exponent_min_mmx;
173         c->ac3_max_msb_abs_int16 = ff_ac3_max_msb_abs_int16_mmx;
174         c->ac3_lshift_int16 = ff_ac3_lshift_int16_mmx;
175         c->ac3_rshift_int32 = ff_ac3_rshift_int32_mmx;
176     }
177     if (EXTERNAL_AMD3DNOW(mm_flags)) {
178         c->extract_exponents = ff_ac3_extract_exponents_3dnow;
179         if (!bit_exact) {
180             c->float_to_fixed24 = ff_float_to_fixed24_3dnow;
181         }
182     }
183     if (EXTERNAL_MMXEXT(mm_flags)) {
184         c->ac3_exponent_min = ff_ac3_exponent_min_mmxext;
185         c->ac3_max_msb_abs_int16 = ff_ac3_max_msb_abs_int16_mmxext;
186     }
187     if (EXTERNAL_SSE(mm_flags)) {
188         c->float_to_fixed24 = ff_float_to_fixed24_sse;
189     }
190     if (EXTERNAL_SSE2(mm_flags)) {
191         c->ac3_exponent_min = ff_ac3_exponent_min_sse2;
192         c->ac3_max_msb_abs_int16 = ff_ac3_max_msb_abs_int16_sse2;
193         c->float_to_fixed24 = ff_float_to_fixed24_sse2;
194         c->compute_mantissa_size = ff_ac3_compute_mantissa_size_sse2;
195         c->extract_exponents = ff_ac3_extract_exponents_sse2;
196         if (!(mm_flags & AV_CPU_FLAG_SSE2SLOW)) {
197             c->ac3_lshift_int16 = ff_ac3_lshift_int16_sse2;
198             c->ac3_rshift_int32 = ff_ac3_rshift_int32_sse2;
199         }
200     }
201     if (EXTERNAL_SSSE3(mm_flags)) {
202         c->ac3_max_msb_abs_int16 = ff_ac3_max_msb_abs_int16_ssse3;
203         if (!(mm_flags & AV_CPU_FLAG_ATOM)) {
204             c->extract_exponents = ff_ac3_extract_exponents_ssse3;
205         }
206     }
207
208 #if HAVE_SSE_INLINE
209     if (INLINE_SSE(mm_flags)) {
210         c->downmix = ac3_downmix_sse;
211     }
212 #endif
213 }