f849e950875fd246271c013a238e12a50575d4d9
[ffmpeg.git] / libavcodec / mips / ac3dsp_mips.c
1 /*
2  * Copyright (c) 2012
3  *      MIPS Technologies, Inc., California.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the MIPS Technologies, Inc., nor the names of its
14  *    contributors may be used to endorse or promote products derived from
15  *    this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * Author:  Branimir Vasic (bvasic@mips.com)
30  *
31  * Various AC-3 DSP Utils optimized for MIPS
32  *
33  * This file is part of FFmpeg.
34  *
35  * FFmpeg is free software; you can redistribute it and/or
36  * modify it under the terms of the GNU Lesser General Public
37  * License as published by the Free Software Foundation; either
38  * version 2.1 of the License, or (at your option) any later version.
39  *
40  * FFmpeg is distributed in the hope that it will be useful,
41  * but WITHOUT ANY WARRANTY; without even the implied warranty of
42  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43  * Lesser General Public License for more details.
44  *
45  * You should have received a copy of the GNU Lesser General Public
46  * License along with FFmpeg; if not, write to the Free Software
47  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
48  */
49
50 /**
51  * @file
52  * Reference: libavcodec/ac3dsp.c
53  */
54
55 #include "config.h"
56 #include "libavcodec/ac3dsp.h"
57 #include "libavcodec/ac3.h"
58
59
60 #if HAVE_INLINE_ASM
61 #if HAVE_MIPSDSPR1
62 static void ac3_bit_alloc_calc_bap_mips(int16_t *mask, int16_t *psd,
63                                         int start, int end,
64                                         int snr_offset, int floor,
65                                         const uint8_t *bap_tab, uint8_t *bap)
66 {
67     int band, band_end, cond;
68     int m, address1, address2;
69     int16_t *psd1, *psd_end;
70     uint8_t *bap1;
71
72     if (snr_offset == -960) {
73         memset(bap, 0, AC3_MAX_COEFS);
74         return;
75     }
76
77     psd1 = &psd[start];
78     bap1 = &bap[start];
79     band = ff_ac3_bin_to_band_tab[start];
80
81     do {
82         m = (FFMAX(mask[band] - snr_offset - floor, 0) & 0x1FE0) + floor;
83         band_end = ff_ac3_band_start_tab[++band];
84         band_end = FFMIN(band_end, end);
85         psd_end = psd + band_end - 1;
86
87         __asm__ volatile (
88             "slt        %[cond],        %[psd1],        %[psd_end]  \n\t"
89             "beqz       %[cond],        1f                          \n\t"
90             "2:                                                     \n\t"
91             "lh         %[address1],    0(%[psd1])                  \n\t"
92             "lh         %[address2],    2(%[psd1])                  \n\t"
93             "addiu      %[psd1],        %[psd1],        4           \n\t"
94             "subu       %[address1],    %[address1],    %[m]        \n\t"
95             "sra        %[address1],    %[address1],    5           \n\t"
96             "addiu      %[address1],    %[address1],    -32         \n\t"
97             "shll_s.w   %[address1],    %[address1],    26          \n\t"
98             "subu       %[address2],    %[address2],    %[m]        \n\t"
99             "sra        %[address2],    %[address2],    5           \n\t"
100             "sra        %[address1],    %[address1],    26          \n\t"
101             "addiu      %[address1],    %[address1],    32          \n\t"
102             "lbux       %[address1],    %[address1](%[bap_tab])     \n\t"
103             "addiu      %[address2],    %[address2],    -32         \n\t"
104             "shll_s.w   %[address2],    %[address2],    26          \n\t"
105             "sb         %[address1],    0(%[bap1])                  \n\t"
106             "slt        %[cond],        %[psd1],        %[psd_end]  \n\t"
107             "sra        %[address2],    %[address2],    26          \n\t"
108             "addiu      %[address2],    %[address2],    32          \n\t"
109             "lbux       %[address2],    %[address2](%[bap_tab])     \n\t"
110             "sb         %[address2],    1(%[bap1])                  \n\t"
111             "addiu      %[bap1],        %[bap1],        2           \n\t"
112             "bnez       %[cond],        2b                          \n\t"
113             "addiu      %[psd_end],     %[psd_end],     2           \n\t"
114             "slt        %[cond],        %[psd1],        %[psd_end]  \n\t"
115             "beqz       %[cond],        3f                          \n\t"
116             "1:                                                     \n\t"
117             "lh         %[address1],    0(%[psd1])                  \n\t"
118             "addiu      %[psd1],        %[psd1],        2           \n\t"
119             "subu       %[address1],    %[address1],    %[m]        \n\t"
120             "sra        %[address1],    %[address1],    5           \n\t"
121             "addiu      %[address1],    %[address1],    -32         \n\t"
122             "shll_s.w   %[address1],    %[address1],    26          \n\t"
123             "sra        %[address1],    %[address1],    26          \n\t"
124             "addiu      %[address1],    %[address1],    32          \n\t"
125             "lbux       %[address1],    %[address1](%[bap_tab])     \n\t"
126             "sb         %[address1],    0(%[bap1])                  \n\t"
127             "addiu      %[bap1],        %[bap1],        1           \n\t"
128             "3:                                                     \n\t"
129
130             : [address1]"=&r"(address1), [address2]"=&r"(address2),
131               [cond]"=&r"(cond), [bap1]"+r"(bap1),
132               [psd1]"+r"(psd1), [psd_end]"+r"(psd_end)
133             : [m]"r"(m), [bap_tab]"r"(bap_tab)
134             : "memory"
135         );
136     } while (end > band_end);
137 }
138
139 static void ac3_update_bap_counts_mips(uint16_t mant_cnt[16], uint8_t *bap,
140                                        int len)
141 {
142     int temp0, temp1, temp2, temp3, temp4, temp5, temp6, temp7;
143
144     __asm__ volatile (
145         "andi   %[temp3],   %[len],         3               \n\t"
146         "addu   %[temp2],   %[bap],         %[len]          \n\t"
147         "addu   %[temp4],   %[bap],         %[temp3]        \n\t"
148         "beq    %[temp2],   %[temp4],       4f              \n\t"
149         "1:                                                 \n\t"
150         "lbu    %[temp0],   -1(%[temp2])                    \n\t"
151         "lbu    %[temp5],   -2(%[temp2])                    \n\t"
152         "lbu    %[temp6],   -3(%[temp2])                    \n\t"
153         "sll    %[temp0],   %[temp0],       1               \n\t"
154         "addu   %[temp0],   %[mant_cnt],    %[temp0]        \n\t"
155         "sll    %[temp5],   %[temp5],       1               \n\t"
156         "addu   %[temp5],   %[mant_cnt],    %[temp5]        \n\t"
157         "lhu    %[temp1],   0(%[temp0])                     \n\t"
158         "sll    %[temp6],   %[temp6],       1               \n\t"
159         "addu   %[temp6],   %[mant_cnt],    %[temp6]        \n\t"
160         "addiu  %[temp1],   %[temp1],       1               \n\t"
161         "sh     %[temp1],   0(%[temp0])                     \n\t"
162         "lhu    %[temp1],   0(%[temp5])                     \n\t"
163         "lbu    %[temp7],   -4(%[temp2])                    \n\t"
164         "addiu  %[temp2],   %[temp2],       -4              \n\t"
165         "addiu  %[temp1],   %[temp1],       1               \n\t"
166         "sh     %[temp1],   0(%[temp5])                     \n\t"
167         "lhu    %[temp1],   0(%[temp6])                     \n\t"
168         "sll    %[temp7],   %[temp7],       1               \n\t"
169         "addu   %[temp7],   %[mant_cnt],    %[temp7]        \n\t"
170         "addiu  %[temp1],   %[temp1],1                      \n\t"
171         "sh     %[temp1],   0(%[temp6])                     \n\t"
172         "lhu    %[temp1],   0(%[temp7])                     \n\t"
173         "addiu  %[temp1],   %[temp1],       1               \n\t"
174         "sh     %[temp1],   0(%[temp7])                     \n\t"
175         "bne    %[temp2],   %[temp4],       1b              \n\t"
176         "4:                                                 \n\t"
177         "beqz   %[temp3],   2f                              \n\t"
178         "3:                                                 \n\t"
179         "addiu  %[temp3],   %[temp3],       -1              \n\t"
180         "lbu    %[temp0],   -1(%[temp2])                    \n\t"
181         "addiu  %[temp2],   %[temp2],       -1              \n\t"
182         "sll    %[temp0],   %[temp0],       1               \n\t"
183         "addu   %[temp0],   %[mant_cnt],    %[temp0]        \n\t"
184         "lhu    %[temp1],   0(%[temp0])                     \n\t"
185         "addiu  %[temp1],   %[temp1],       1               \n\t"
186         "sh     %[temp1],   0(%[temp0])                     \n\t"
187         "bgtz   %[temp3],   3b                              \n\t"
188         "2:                                                 \n\t"
189
190         : [temp0] "=&r" (temp0), [temp1] "=&r" (temp1),
191           [temp2] "=&r" (temp2), [temp3] "=&r" (temp3),
192           [temp4] "=&r" (temp4), [temp5] "=&r" (temp5),
193           [temp6] "=&r" (temp6), [temp7] "=&r" (temp7)
194         : [len] "r" (len), [bap] "r" (bap),
195           [mant_cnt] "r" (mant_cnt)
196         : "memory"
197     );
198 }
199 #endif
200
201 #if HAVE_MIPSFPU
202 static void float_to_fixed24_mips(int32_t *dst, const float *src, unsigned int len)
203 {
204     const float scale = 1 << 24;
205     float src0, src1, src2, src3, src4, src5, src6, src7;
206     int temp0, temp1, temp2, temp3, temp4, temp5, temp6, temp7;
207
208     do {
209         __asm__ volatile (
210             "lwc1       %[src0],    0(%[src])               \n\t"
211             "lwc1       %[src1],    4(%[src])               \n\t"
212             "lwc1       %[src2],    8(%[src])               \n\t"
213             "lwc1       %[src3],    12(%[src])              \n\t"
214             "lwc1       %[src4],    16(%[src])              \n\t"
215             "lwc1       %[src5],    20(%[src])              \n\t"
216             "lwc1       %[src6],    24(%[src])              \n\t"
217             "lwc1       %[src7],    28(%[src])              \n\t"
218             "mul.s      %[src0],    %[src0],    %[scale]    \n\t"
219             "mul.s      %[src1],    %[src1],    %[scale]    \n\t"
220             "mul.s      %[src2],    %[src2],    %[scale]    \n\t"
221             "mul.s      %[src3],    %[src3],    %[scale]    \n\t"
222             "mul.s      %[src4],    %[src4],    %[scale]    \n\t"
223             "mul.s      %[src5],    %[src5],    %[scale]    \n\t"
224             "mul.s      %[src6],    %[src6],    %[scale]    \n\t"
225             "mul.s      %[src7],    %[src7],    %[scale]    \n\t"
226             "cvt.w.s    %[src0],    %[src0]                 \n\t"
227             "cvt.w.s    %[src1],    %[src1]                 \n\t"
228             "cvt.w.s    %[src2],    %[src2]                 \n\t"
229             "cvt.w.s    %[src3],    %[src3]                 \n\t"
230             "cvt.w.s    %[src4],    %[src4]                 \n\t"
231             "cvt.w.s    %[src5],    %[src5]                 \n\t"
232             "cvt.w.s    %[src6],    %[src6]                 \n\t"
233             "cvt.w.s    %[src7],    %[src7]                 \n\t"
234             "mfc1       %[temp0],   %[src0]                 \n\t"
235             "mfc1       %[temp1],   %[src1]                 \n\t"
236             "mfc1       %[temp2],   %[src2]                 \n\t"
237             "mfc1       %[temp3],   %[src3]                 \n\t"
238             "mfc1       %[temp4],   %[src4]                 \n\t"
239             "mfc1       %[temp5],   %[src5]                 \n\t"
240             "mfc1       %[temp6],   %[src6]                 \n\t"
241             "mfc1       %[temp7],   %[src7]                 \n\t"
242             "sw         %[temp0],   0(%[dst])               \n\t"
243             "sw         %[temp1],   4(%[dst])               \n\t"
244             "sw         %[temp2],   8(%[dst])               \n\t"
245             "sw         %[temp3],   12(%[dst])              \n\t"
246             "sw         %[temp4],   16(%[dst])              \n\t"
247             "sw         %[temp5],   20(%[dst])              \n\t"
248             "sw         %[temp6],   24(%[dst])              \n\t"
249             "sw         %[temp7],   28(%[dst])              \n\t"
250
251             : [dst] "+r" (dst), [src] "+r" (src),
252               [src0] "=&f" (src0), [src1] "=&f" (src1),
253               [src2] "=&f" (src2), [src3] "=&f" (src3),
254               [src4] "=&f" (src4), [src5] "=&f" (src5),
255               [src6] "=&f" (src6), [src7] "=&f" (src7),
256               [temp0] "=r" (temp0), [temp1] "=r" (temp1),
257               [temp2] "=r" (temp2), [temp3] "=r" (temp3),
258               [temp4] "=r" (temp4), [temp5] "=r" (temp5),
259               [temp6] "=r" (temp6), [temp7] "=r" (temp7)
260             : [scale] "f" (scale)
261             : "memory"
262         );
263         src = src + 8;
264         dst = dst + 8;
265         len -= 8;
266     } while (len > 0);
267 }
268
269 static void ac3_downmix_mips(float (*samples)[256], float (*matrix)[2], int out_ch, int in_ch, int len)
270 {
271     int i, j;
272     float v0, v1, v2, v3;
273     float v4, v5, v6, v7;
274     float samples0, samples1, samples2, samples3, matrix_j, matrix_j2;
275     float *samples_p,*matrix_p;
276     if (out_ch == 2) {
277         for (i = 0; i < len; i += 4) {
278             v0 = v1 = v2 = v3 = 0.0f;
279             v4 = v5 = v6 = v7 = 0.0f;
280             samples_p = &samples[0][i];
281             matrix_p = &matrix[0][0];
282             __asm__ volatile (
283                 "move   %[j],           $zero                               \n\t"
284                 "1:                                                         \n\t"
285                 "lwc1   %[matrix_j],    0(%[matrix_p])                      \n\t"
286                 "lwc1   %[matrix_j2],   4(%[matrix_p])                      \n\t"
287                 "lwc1   %[samples0],    0(%[samples_p])                     \n\t"
288                 "lwc1   %[samples1],    4(%[samples_p])                     \n\t"
289                 "lwc1   %[samples2],    8(%[samples_p])                     \n\t"
290                 "lwc1   %[samples3],    12(%[samples_p])                    \n\t"
291                 "addiu  %[matrix_p],    8                                   \n\t"
292                 "madd.s %[v0],          %[v0],  %[samples0],    %[matrix_j] \n\t"
293                 "madd.s %[v1],          %[v1],  %[samples1],    %[matrix_j] \n\t"
294                 "madd.s %[v2],          %[v2],  %[samples2],    %[matrix_j] \n\t"
295                 "madd.s %[v3],          %[v3],  %[samples3],    %[matrix_j] \n\t"
296                 "madd.s %[v4],          %[v4],  %[samples0],    %[matrix_j2]\n\t"
297                 "madd.s %[v5],          %[v5],  %[samples1],    %[matrix_j2]\n\t"
298                 "madd.s %[v6],          %[v6],  %[samples2],    %[matrix_j2]\n\t"
299                 "madd.s %[v7],          %[v7],  %[samples3],    %[matrix_j2]\n\t"
300                 "addiu  %[j],           1                                   \n\t"
301                 "addiu  %[samples_p],   1024                                \n\t"
302                 "bne    %[j],           %[in_ch],   1b                      \n\t"
303                 :[samples0]"=&f"(samples0), [samples1]"=&f"(samples1),
304                  [samples2]"=&f"(samples2), [samples3]"=&f"(samples3),
305                  [samples_p]"+r"(samples_p), [matrix_j]"=&f"(matrix_j),
306                  [matrix_p]"+r"(matrix_p), [v0]"+f"(v0), [v1]"+f"(v1),
307                  [v2]"+f"(v2), [v3]"+f"(v3), [v4]"+f"(v4), [v5]"+f"(v5),
308                  [v6]"+f"(v6), [v7]"+f"(v7),[j]"=&r"(j), [matrix_j2]"=&f"(matrix_j2)
309                 :[in_ch]"r"(in_ch)
310                 :"memory"
311             );
312             samples[0][i  ] = v0;
313             samples[0][i+1] = v1;
314             samples[0][i+2] = v2;
315             samples[0][i+3] = v3;
316             samples[1][i  ] = v4;
317             samples[1][i+1] = v5;
318             samples[1][i+2] = v6;
319             samples[1][i+3] = v7;
320         }
321     } else if (out_ch == 1) {
322         for (i = 0; i < len; i += 4) {
323             v0 = v1 = v2 = v3 = 0.0f;
324             samples_p = &samples[0][i];
325             matrix_p = &matrix[0][0];
326             __asm__ volatile (
327                 "move   %[j],           $zero                               \n\t"
328                 "1:                                                         \n\t"
329                 "lwc1   %[matrix_j],    0(%[matrix_p])                      \n\t"
330                 "lwc1   %[samples0],    0(%[samples_p])                     \n\t"
331                 "lwc1   %[samples1],    4(%[samples_p])                     \n\t"
332                 "lwc1   %[samples2],    8(%[samples_p])                     \n\t"
333                 "lwc1   %[samples3],    12(%[samples_p])                    \n\t"
334                 "addiu  %[matrix_p],    8                                   \n\t"
335                 "madd.s %[v0],          %[v0],  %[samples0],    %[matrix_j] \n\t"
336                 "madd.s %[v1],          %[v1],  %[samples1],    %[matrix_j] \n\t"
337                 "madd.s %[v2],          %[v2],  %[samples2],    %[matrix_j] \n\t"
338                 "madd.s %[v3],          %[v3],  %[samples3],    %[matrix_j] \n\t"
339                 "addiu  %[j],           1                                   \n\t"
340                 "addiu  %[samples_p],   1024                                \n\t"
341                 "bne    %[j],           %[in_ch],   1b                      \n\t"
342                 :[samples0]"=&f"(samples0), [samples1]"=&f"(samples1),
343                  [samples2]"=&f"(samples2), [samples3]"=&f"(samples3),
344                  [samples_p]"+r"(samples_p), [matrix_j]"=&f"(matrix_j),
345                  [matrix_p]"+r"(matrix_p), [v0]"+f"(v0), [v1]"+f"(v1),
346                  [v2]"+f"(v2), [v3]"+f"(v3), [j]"=&r"(j)
347                 :[in_ch]"r"(in_ch)
348                 :"memory"
349             );
350             samples[0][i  ] = v0;
351             samples[0][i+1] = v1;
352             samples[0][i+2] = v2;
353             samples[0][i+3] = v3;
354         }
355     }
356 }
357 #endif
358 #endif /* HAVE_INLINE_ASM */
359
360 void ff_ac3dsp_init_mips(AC3DSPContext *c, int bit_exact) {
361 #if HAVE_INLINE_ASM
362 #if HAVE_MIPSDSPR1
363     c->bit_alloc_calc_bap = ac3_bit_alloc_calc_bap_mips;
364     c->update_bap_counts  = ac3_update_bap_counts_mips;
365 #endif
366 #if HAVE_MIPSFPU
367     c->float_to_fixed24 = float_to_fixed24_mips;
368 //     c->downmix          = ac3_downmix_mips;
369 #endif
370 #endif
371
372 }