]> git.sesse.net Git - ffmpeg/blob - libavcodec/mips/celp_filters_mips.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / mips / celp_filters_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:  Nedeljko Babic (nbabic@mips.com)
30  *
31  * various filters for CELP-based codecs 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/celp_filters.c
53  */
54 #include "libavutil/attributes.h"
55 #include "libavutil/common.h"
56 #include "libavcodec/celp_filters.h"
57
58 static void ff_celp_lp_synthesis_filterf_mips(float *out,
59                                   const float *filter_coeffs,
60                                   const float* in, int buffer_length,
61                                   int filter_length)
62 {
63     int i,n;
64
65     float out0, out1, out2, out3;
66     float old_out0, old_out1, old_out2, old_out3;
67     float a,b,c;
68     const float *p_filter_coeffs;
69     float *p_out;
70
71     a = filter_coeffs[0];
72     b = filter_coeffs[1];
73     c = filter_coeffs[2];
74     b -= filter_coeffs[0] * filter_coeffs[0];
75     c -= filter_coeffs[1] * filter_coeffs[0];
76     c -= filter_coeffs[0] * b;
77
78     old_out0 = out[-4];
79     old_out1 = out[-3];
80     old_out2 = out[-2];
81     old_out3 = out[-1];
82     for (n = 0; n <= buffer_length - 4; n+=4) {
83         p_filter_coeffs = filter_coeffs;
84         p_out = out;
85
86         out0 = in[0];
87         out1 = in[1];
88         out2 = in[2];
89         out3 = in[3];
90
91         __asm__ volatile(
92             "lwc1       $f2,     8(%[filter_coeffs])                        \n\t"
93             "lwc1       $f1,     4(%[filter_coeffs])                        \n\t"
94             "lwc1       $f0,     0(%[filter_coeffs])                        \n\t"
95             "nmsub.s    %[out0], %[out0],             $f2, %[old_out1]      \n\t"
96             "nmsub.s    %[out1], %[out1],             $f2, %[old_out2]      \n\t"
97             "nmsub.s    %[out2], %[out2],             $f2, %[old_out3]      \n\t"
98             "lwc1       $f3,     12(%[filter_coeffs])                       \n\t"
99             "nmsub.s    %[out0], %[out0],             $f1, %[old_out2]      \n\t"
100             "nmsub.s    %[out1], %[out1],             $f1, %[old_out3]      \n\t"
101             "nmsub.s    %[out2], %[out2],             $f3, %[old_out2]      \n\t"
102             "nmsub.s    %[out0], %[out0],             $f0, %[old_out3]      \n\t"
103             "nmsub.s    %[out3], %[out3],             $f3, %[old_out3]      \n\t"
104             "nmsub.s    %[out1], %[out1],             $f3, %[old_out1]      \n\t"
105             "nmsub.s    %[out0], %[out0],             $f3, %[old_out0]      \n\t"
106
107             : [out0]"+f"(out0), [out1]"+f"(out1),
108               [out2]"+f"(out2), [out3]"+f"(out3)
109             : [old_out0]"f"(old_out0), [old_out1]"f"(old_out1),
110               [old_out2]"f"(old_out2), [old_out3]"f"(old_out3),
111               [filter_coeffs]"r"(filter_coeffs)
112             : "$f0", "$f1", "$f2", "$f3", "$f4"
113         );
114
115         for (i = 5; i <= filter_length; i += 2) {
116             __asm__ volatile(
117                 "lwc1    %[old_out3], -20(%[p_out])                         \n\t"
118                 "lwc1    $f5,         16(%[p_filter_coeffs])                \n\t"
119                 "addiu   %[p_out],    -8                                    \n\t"
120                 "addiu   %[p_filter_coeffs], 8                              \n\t"
121                 "nmsub.s %[out1],     %[out1],      $f5, %[old_out0]        \n\t"
122                 "nmsub.s %[out3],     %[out3],      $f5, %[old_out2]        \n\t"
123                 "lwc1    $f4,         12(%[p_filter_coeffs])                \n\t"
124                 "lwc1    %[old_out2], -16(%[p_out])                         \n\t"
125                 "nmsub.s %[out0],     %[out0],      $f5, %[old_out3]        \n\t"
126                 "nmsub.s %[out2],     %[out2],      $f5, %[old_out1]        \n\t"
127                 "nmsub.s %[out1],     %[out1],      $f4, %[old_out3]        \n\t"
128                 "nmsub.s %[out3],     %[out3],      $f4, %[old_out1]        \n\t"
129                 "mov.s   %[old_out1], %[old_out3]                           \n\t"
130                 "nmsub.s %[out0],     %[out0],      $f4, %[old_out2]        \n\t"
131                 "nmsub.s %[out2],     %[out2],      $f4, %[old_out0]        \n\t"
132
133                 : [out0]"+f"(out0), [out1]"+f"(out1),
134                   [out2]"+f"(out2), [out3]"+f"(out3), [old_out0]"+f"(old_out0),
135                   [old_out1]"+f"(old_out1), [old_out2]"+f"(old_out2),
136                   [old_out3]"+f"(old_out3),[p_filter_coeffs]"+r"(p_filter_coeffs),
137                   [p_out]"+r"(p_out)
138                 :
139                 : "$f4", "$f5"
140             );
141             FFSWAP(float, old_out0, old_out2);
142         }
143
144         __asm__ volatile(
145             "nmsub.s    %[out3], %[out3], %[a], %[out2]                     \n\t"
146             "nmsub.s    %[out2], %[out2], %[a], %[out1]                     \n\t"
147             "nmsub.s    %[out3], %[out3], %[b], %[out1]                     \n\t"
148             "nmsub.s    %[out1], %[out1], %[a], %[out0]                     \n\t"
149             "nmsub.s    %[out2], %[out2], %[b], %[out0]                     \n\t"
150             "nmsub.s    %[out3], %[out3], %[c], %[out0]                     \n\t"
151
152             : [out0]"+f"(out0), [out1]"+f"(out1),
153               [out2]"+f"(out2), [out3]"+f"(out3)
154             : [a]"f"(a), [b]"f"(b), [c]"f"(c)
155         );
156
157         out[0] = out0;
158         out[1] = out1;
159         out[2] = out2;
160         out[3] = out3;
161
162         old_out0 = out0;
163         old_out1 = out1;
164         old_out2 = out2;
165         old_out3 = out3;
166
167         out += 4;
168         in  += 4;
169     }
170
171     out -= n;
172     in -= n;
173     for (; n < buffer_length; n++) {
174         float out_val, out_val_i, fc_val;
175         p_filter_coeffs = filter_coeffs;
176         p_out = &out[n];
177         out_val = in[n];
178         for (i = 1; i <= filter_length; i++) {
179             __asm__ volatile(
180                 "lwc1    %[fc_val],          0(%[p_filter_coeffs])                        \n\t"
181                 "lwc1    %[out_val_i],       -4(%[p_out])                                 \n\t"
182                 "addiu   %[p_filter_coeffs], 4                                            \n\t"
183                 "addiu   %[p_out],           -4                                           \n\t"
184                 "nmsub.s %[out_val],         %[out_val],          %[fc_val], %[out_val_i] \n\t"
185
186                 : [fc_val]"=&f"(fc_val), [out_val]"+f"(out_val),
187                   [out_val_i]"=&f"(out_val_i), [p_out]"+r"(p_out),
188                   [p_filter_coeffs]"+r"(p_filter_coeffs)
189             );
190         }
191         out[n] = out_val;
192     }
193 }
194
195 static void ff_celp_lp_zero_synthesis_filterf_mips(float *out,
196                                        const float *filter_coeffs,
197                                        const float *in, int buffer_length,
198                                        int filter_length)
199 {
200     int i,n;
201     float sum_out8, sum_out7, sum_out6, sum_out5, sum_out4, fc_val;
202     float sum_out3, sum_out2, sum_out1;
203     const float *p_filter_coeffs, *p_in;
204
205     for (n = 0; n < buffer_length; n+=8) {
206         p_in = &in[n];
207         p_filter_coeffs = filter_coeffs;
208         sum_out8 = in[n+7];
209         sum_out7 = in[n+6];
210         sum_out6 = in[n+5];
211         sum_out5 = in[n+4];
212         sum_out4 = in[n+3];
213         sum_out3 = in[n+2];
214         sum_out2 = in[n+1];
215         sum_out1 = in[n];
216         i = filter_length;
217
218         /* i is always greater than 0
219         * outer loop is unrolled eight times so there is less memory access
220         * inner loop is unrolled two times
221         */
222         __asm__ volatile(
223             "filt_lp_inner%=:                                               \n\t"
224             "lwc1   %[fc_val],   0(%[p_filter_coeffs])                      \n\t"
225             "lwc1   $f7,         6*4(%[p_in])                               \n\t"
226             "lwc1   $f6,         5*4(%[p_in])                               \n\t"
227             "lwc1   $f5,         4*4(%[p_in])                               \n\t"
228             "lwc1   $f4,         3*4(%[p_in])                               \n\t"
229             "lwc1   $f3,         2*4(%[p_in])                               \n\t"
230             "lwc1   $f2,         4(%[p_in])                                 \n\t"
231             "lwc1   $f1,         0(%[p_in])                                 \n\t"
232             "lwc1   $f0,         -4(%[p_in])                                \n\t"
233             "addiu  %[i],        -2                                         \n\t"
234             "madd.s %[sum_out8], %[sum_out8],          %[fc_val], $f7       \n\t"
235             "madd.s %[sum_out7], %[sum_out7],          %[fc_val], $f6       \n\t"
236             "madd.s %[sum_out6], %[sum_out6],          %[fc_val], $f5       \n\t"
237             "madd.s %[sum_out5], %[sum_out5],          %[fc_val], $f4       \n\t"
238             "madd.s %[sum_out4], %[sum_out4],          %[fc_val], $f3       \n\t"
239             "madd.s %[sum_out3], %[sum_out3],          %[fc_val], $f2       \n\t"
240             "madd.s %[sum_out2], %[sum_out2],          %[fc_val], $f1       \n\t"
241             "madd.s %[sum_out1], %[sum_out1],          %[fc_val], $f0       \n\t"
242             "lwc1   %[fc_val],   4(%[p_filter_coeffs])                      \n\t"
243             "lwc1   $f7,         -8(%[p_in])                                \n\t"
244             "addiu  %[p_filter_coeffs], 8                                   \n\t"
245             "addiu  %[p_in],     -8                                         \n\t"
246             "madd.s %[sum_out8], %[sum_out8],          %[fc_val], $f6       \n\t"
247             "madd.s %[sum_out7], %[sum_out7],          %[fc_val], $f5       \n\t"
248             "madd.s %[sum_out6], %[sum_out6],          %[fc_val], $f4       \n\t"
249             "madd.s %[sum_out5], %[sum_out5],          %[fc_val], $f3       \n\t"
250             "madd.s %[sum_out4], %[sum_out4],          %[fc_val], $f2       \n\t"
251             "madd.s %[sum_out3], %[sum_out3],          %[fc_val], $f1       \n\t"
252             "madd.s %[sum_out2], %[sum_out2],          %[fc_val], $f0       \n\t"
253             "madd.s %[sum_out1], %[sum_out1],          %[fc_val], $f7       \n\t"
254             "bgtz   %[i],        filt_lp_inner%=                            \n\t"
255
256             : [sum_out8]"+f"(sum_out8), [sum_out7]"+f"(sum_out7),
257               [sum_out6]"+f"(sum_out6), [sum_out5]"+f"(sum_out5),
258               [sum_out4]"+f"(sum_out4), [sum_out3]"+f"(sum_out3),
259               [sum_out2]"+f"(sum_out2), [sum_out1]"+f"(sum_out1),
260               [fc_val]"=&f"(fc_val), [p_filter_coeffs]"+r"(p_filter_coeffs),
261               [p_in]"+r"(p_in), [i]"+r"(i)
262             :
263             : "$f0", "$f1", "$f2", "$f3", "$f4", "$f5", "$f6", "$f7"
264         );
265
266         out[n+7] = sum_out8;
267         out[n+6] = sum_out7;
268         out[n+5] = sum_out6;
269         out[n+4] = sum_out5;
270         out[n+3] = sum_out4;
271         out[n+2] = sum_out3;
272         out[n+1] = sum_out2;
273         out[n] = sum_out1;
274     }
275 }
276
277 void ff_celp_filter_init_mips(CELPFContext *c)
278 {
279     c->celp_lp_synthesis_filterf        = ff_celp_lp_synthesis_filterf_mips;
280     c->celp_lp_zero_synthesis_filterf   = ff_celp_lp_zero_synthesis_filterf_mips;
281 }