]> git.sesse.net Git - ffmpeg/blob - libavcodec/mips/aacdec_mips.c
ffmpeg: notify when the thread message queue blocks.
[ffmpeg] / libavcodec / mips / aacdec_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  * Authors:  Darko Laus      (darko@mips.com)
30  *           Djordje Pesut   (djordje@mips.com)
31  *           Mirjana Vulin   (mvulin@mips.com)
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/aacdec.c
53  */
54
55 #include "libavcodec/aac.h"
56 #include "aacdec_mips.h"
57 #include "libavcodec/aactab.h"
58 #include "libavcodec/sinewin.h"
59
60 #if HAVE_INLINE_ASM
61 static av_always_inline void float_copy(float *dst, const float *src, int count)
62 {
63     // Copy 'count' floats from src to dst
64     const float *loop_end = src + count;
65     int temp[8];
66
67     // count must be a multiple of 8
68     av_assert2(count % 8 == 0);
69
70     // loop unrolled 8 times
71     __asm__ volatile (
72         ".set push                               \n\t"
73         ".set noreorder                          \n\t"
74     "1:                                          \n\t"
75         "lw      %[temp0],    0(%[src])          \n\t"
76         "lw      %[temp1],    4(%[src])          \n\t"
77         "lw      %[temp2],    8(%[src])          \n\t"
78         "lw      %[temp3],    12(%[src])         \n\t"
79         "lw      %[temp4],    16(%[src])         \n\t"
80         "lw      %[temp5],    20(%[src])         \n\t"
81         "lw      %[temp6],    24(%[src])         \n\t"
82         "lw      %[temp7],    28(%[src])         \n\t"
83         "addiu   %[src],      %[src],      32    \n\t"
84         "sw      %[temp0],    0(%[dst])          \n\t"
85         "sw      %[temp1],    4(%[dst])          \n\t"
86         "sw      %[temp2],    8(%[dst])          \n\t"
87         "sw      %[temp3],    12(%[dst])         \n\t"
88         "sw      %[temp4],    16(%[dst])         \n\t"
89         "sw      %[temp5],    20(%[dst])         \n\t"
90         "sw      %[temp6],    24(%[dst])         \n\t"
91         "sw      %[temp7],    28(%[dst])         \n\t"
92         "bne     %[src],      %[loop_end], 1b    \n\t"
93         "addiu   %[dst],      %[dst],      32    \n\t"
94         ".set pop                                \n\t"
95
96         : [temp0]"=&r"(temp[0]), [temp1]"=&r"(temp[1]),
97           [temp2]"=&r"(temp[2]), [temp3]"=&r"(temp[3]),
98           [temp4]"=&r"(temp[4]), [temp5]"=&r"(temp[5]),
99           [temp6]"=&r"(temp[6]), [temp7]"=&r"(temp[7]),
100           [src]"+r"(src), [dst]"+r"(dst)
101         : [loop_end]"r"(loop_end)
102         : "memory"
103     );
104 }
105
106 static av_always_inline int lcg_random(unsigned previous_val)
107 {
108     union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
109     return v.s;
110 }
111
112 static void imdct_and_windowing_mips(AACContext *ac, SingleChannelElement *sce)
113 {
114     IndividualChannelStream *ics = &sce->ics;
115     float *in    = sce->coeffs;
116     float *out   = sce->ret;
117     float *saved = sce->saved;
118     const float *swindow      = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
119     const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
120     const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
121     float *buf  = ac->buf_mdct;
122     int i;
123
124     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
125         for (i = 0; i < 1024; i += 128)
126             ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
127     } else
128         ac->mdct.imdct_half(&ac->mdct, buf, in);
129
130     /* window overlapping
131      * NOTE: To simplify the overlapping code, all 'meaningless' short to long
132      * and long to short transitions are considered to be short to short
133      * transitions. This leaves just two cases (long to long and short to short)
134      * with a little special sauce for EIGHT_SHORT_SEQUENCE.
135      */
136     if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
137             (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
138         ac->fdsp->vector_fmul_window(    out,               saved,            buf,         lwindow_prev, 512);
139     } else {
140         float_copy(out, saved, 448);
141
142         if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
143             {
144                 float wi;
145                 float wj;
146                 int i;
147                 float temp0, temp1, temp2, temp3;
148                 float *dst0 = out + 448 + 0*128;
149                 float *dst1 = dst0 + 64 + 63;
150                 float *dst2 = saved + 63;
151                 float *win0 = (float*)swindow;
152                 float *win1 = win0 + 64 + 63;
153                 float *win0_prev = (float*)swindow_prev;
154                 float *win1_prev = win0_prev + 64 + 63;
155                 float *src0_prev = saved + 448;
156                 float *src1_prev = buf + 0*128 + 63;
157                 float *src0 = buf + 0*128 + 64;
158                 float *src1 = buf + 1*128 + 63;
159
160                 for(i = 0; i < 64; i++)
161                 {
162                     temp0 = src0_prev[0];
163                     temp1 = src1_prev[0];
164                     wi = *win0_prev;
165                     wj = *win1_prev;
166                     temp2 = src0[0];
167                     temp3 = src1[0];
168                     dst0[0] = temp0 * wj - temp1 * wi;
169                     dst1[0] = temp0 * wi + temp1 * wj;
170
171                     wi = *win0;
172                     wj = *win1;
173
174                     temp0 = src0[128];
175                     temp1 = src1[128];
176                     dst0[128] = temp2 * wj - temp3 * wi;
177                     dst1[128] = temp2 * wi + temp3 * wj;
178
179                     temp2 = src0[256];
180                     temp3 = src1[256];
181                     dst0[256] = temp0 * wj - temp1 * wi;
182                     dst1[256] = temp0 * wi + temp1 * wj;
183                     dst0[384] = temp2 * wj - temp3 * wi;
184                     dst1[384] = temp2 * wi + temp3 * wj;
185
186                     temp0 = src0[384];
187                     temp1 = src1[384];
188                     dst0[512] = temp0 * wj - temp1 * wi;
189                     dst2[0] = temp0 * wi + temp1 * wj;
190
191                     src0++;
192                     src1--;
193                     src0_prev++;
194                     src1_prev--;
195                     win0++;
196                     win1--;
197                     win0_prev++;
198                     win1_prev--;
199                     dst0++;
200                     dst1--;
201                     dst2--;
202                 }
203             }
204         } else {
205             ac->fdsp->vector_fmul_window(out + 448,         saved + 448,      buf,         swindow_prev, 64);
206             float_copy(out + 576, buf + 64, 448);
207         }
208     }
209
210     // buffer update
211     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
212         ac->fdsp->vector_fmul_window(saved + 64,  buf + 4*128 + 64, buf + 5*128, swindow, 64);
213         ac->fdsp->vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
214         ac->fdsp->vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
215         float_copy(saved + 448, buf + 7*128 + 64, 64);
216     } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
217         float_copy(saved, buf + 512, 448);
218         float_copy(saved + 448, buf + 7*128 + 64, 64);
219     } else { // LONG_STOP or ONLY_LONG
220         float_copy(saved, buf + 512, 512);
221     }
222 }
223
224 static void apply_ltp_mips(AACContext *ac, SingleChannelElement *sce)
225 {
226     const LongTermPrediction *ltp = &sce->ics.ltp;
227     const uint16_t *offsets = sce->ics.swb_offset;
228     int i, sfb;
229     int j, k;
230
231     if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
232         float *predTime = sce->ret;
233         float *predFreq = ac->buf_mdct;
234         float *p_predTime;
235         int16_t num_samples = 2048;
236
237         if (ltp->lag < 1024)
238             num_samples = ltp->lag + 1024;
239             j = (2048 - num_samples) >> 2;
240             k = (2048 - num_samples) & 3;
241             p_predTime = &predTime[num_samples];
242
243         for (i = 0; i < num_samples; i++)
244             predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
245         for (i = 0; i < j; i++) {
246
247             /* loop unrolled 4 times */
248             __asm__ volatile (
249                 "sw      $0,              0(%[p_predTime])        \n\t"
250                 "sw      $0,              4(%[p_predTime])        \n\t"
251                 "sw      $0,              8(%[p_predTime])        \n\t"
252                 "sw      $0,              12(%[p_predTime])       \n\t"
253                 "addiu   %[p_predTime],   %[p_predTime],     16   \n\t"
254
255                 : [p_predTime]"+r"(p_predTime)
256                 :
257                 : "memory"
258             );
259         }
260         for (i = 0; i < k; i++) {
261
262             __asm__ volatile (
263                 "sw      $0,              0(%[p_predTime])        \n\t"
264                 "addiu   %[p_predTime],   %[p_predTime],     4    \n\t"
265
266                 : [p_predTime]"+r"(p_predTime)
267                 :
268                 : "memory"
269             );
270         }
271
272         ac->windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);
273
274         if (sce->tns.present)
275             ac->apply_tns(predFreq, &sce->tns, &sce->ics, 0);
276
277         for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
278             if (ltp->used[sfb])
279                 for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
280                     sce->coeffs[i] += predFreq[i];
281     }
282 }
283
284 #if HAVE_MIPSFPU
285 static av_always_inline void fmul_and_reverse(float *dst, const float *src0, const float *src1, int count)
286 {
287     /* Multiply 'count' floats in src0 by src1 and store the results in dst in reverse */
288     /* This should be equivalent to a normal fmul, followed by reversing dst */
289
290     // count must be a multiple of 4
291     av_assert2(count % 4 == 0);
292
293     // move src0 and src1 to the last element of their arrays
294     src0 += count - 1;
295     src1 += count - 1;
296
297     for (; count > 0; count -= 4){
298         float temp[12];
299
300         /* loop unrolled 4 times */
301         __asm__ volatile (
302             "lwc1    %[temp0],    0(%[ptr2])                \n\t"
303             "lwc1    %[temp1],    -4(%[ptr2])               \n\t"
304             "lwc1    %[temp2],    -8(%[ptr2])               \n\t"
305             "lwc1    %[temp3],    -12(%[ptr2])              \n\t"
306             "lwc1    %[temp4],    0(%[ptr3])                \n\t"
307             "lwc1    %[temp5],    -4(%[ptr3])               \n\t"
308             "lwc1    %[temp6],    -8(%[ptr3])               \n\t"
309             "lwc1    %[temp7],    -12(%[ptr3])              \n\t"
310             "mul.s   %[temp8],    %[temp0],     %[temp4]    \n\t"
311             "mul.s   %[temp9],    %[temp1],     %[temp5]    \n\t"
312             "mul.s   %[temp10],   %[temp2],     %[temp6]    \n\t"
313             "mul.s   %[temp11],   %[temp3],     %[temp7]    \n\t"
314             "swc1    %[temp8],    0(%[ptr1])                \n\t"
315             "swc1    %[temp9],    4(%[ptr1])                \n\t"
316             "swc1    %[temp10],   8(%[ptr1])                \n\t"
317             "swc1    %[temp11],   12(%[ptr1])               \n\t"
318             "addiu   %[ptr1],     %[ptr1],      16          \n\t"
319             "addiu   %[ptr2],     %[ptr2],      -16         \n\t"
320             "addiu   %[ptr3],     %[ptr3],      -16         \n\t"
321
322             : [temp0]"=&f"(temp[0]), [temp1]"=&f"(temp[1]),
323               [temp2]"=&f"(temp[2]), [temp3]"=&f"(temp[3]),
324               [temp4]"=&f"(temp[4]), [temp5]"=&f"(temp[5]),
325               [temp6]"=&f"(temp[6]), [temp7]"=&f"(temp[7]),
326               [temp8]"=&f"(temp[8]), [temp9]"=&f"(temp[9]),
327               [temp10]"=&f"(temp[10]), [temp11]"=&f"(temp[11]),
328               [ptr1]"+r"(dst), [ptr2]"+r"(src0), [ptr3]"+r"(src1)
329             :
330             : "memory"
331         );
332     }
333 }
334
335 static void update_ltp_mips(AACContext *ac, SingleChannelElement *sce)
336 {
337     IndividualChannelStream *ics = &sce->ics;
338     float *saved     = sce->saved;
339     float *saved_ltp = sce->coeffs;
340     const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
341     const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
342     float temp0, temp1, temp2, temp3, temp4, temp5, temp6, temp7;
343
344     if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
345         float *p_saved_ltp = saved_ltp + 576;
346         int loop_end1 = (int)(p_saved_ltp + 448);
347
348         float_copy(saved_ltp, saved, 512);
349
350         /* loop unrolled 8 times */
351         __asm__ volatile (
352         "1:                                                   \n\t"
353             "sw     $0,              0(%[p_saved_ltp])        \n\t"
354             "sw     $0,              4(%[p_saved_ltp])        \n\t"
355             "sw     $0,              8(%[p_saved_ltp])        \n\t"
356             "sw     $0,              12(%[p_saved_ltp])       \n\t"
357             "sw     $0,              16(%[p_saved_ltp])       \n\t"
358             "sw     $0,              20(%[p_saved_ltp])       \n\t"
359             "sw     $0,              24(%[p_saved_ltp])       \n\t"
360             "sw     $0,              28(%[p_saved_ltp])       \n\t"
361             "addiu  %[p_saved_ltp],  %[p_saved_ltp],     32   \n\t"
362             "bne    %[p_saved_ltp],  %[loop_end1],       1b   \n\t"
363
364             : [p_saved_ltp]"+r"(p_saved_ltp)
365             : [loop_end1]"r"(loop_end1)
366             : "memory"
367         );
368
369         ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960,     &swindow[64],      64);
370         fmul_and_reverse(saved_ltp + 512, ac->buf_mdct + 960, swindow, 64);
371     } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
372         float *buff0 = saved;
373         float *buff1 = saved_ltp;
374         float *loop_end = saved + 448;
375
376         /* loop unrolled 8 times */
377         __asm__ volatile (
378             ".set push                                  \n\t"
379             ".set noreorder                             \n\t"
380         "1:                                             \n\t"
381             "lw      %[temp0],    0(%[src])             \n\t"
382             "lw      %[temp1],    4(%[src])             \n\t"
383             "lw      %[temp2],    8(%[src])             \n\t"
384             "lw      %[temp3],    12(%[src])            \n\t"
385             "lw      %[temp4],    16(%[src])            \n\t"
386             "lw      %[temp5],    20(%[src])            \n\t"
387             "lw      %[temp6],    24(%[src])            \n\t"
388             "lw      %[temp7],    28(%[src])            \n\t"
389             "addiu   %[src],      %[src],         32    \n\t"
390             "sw      %[temp0],    0(%[dst])             \n\t"
391             "sw      %[temp1],    4(%[dst])             \n\t"
392             "sw      %[temp2],    8(%[dst])             \n\t"
393             "sw      %[temp3],    12(%[dst])            \n\t"
394             "sw      %[temp4],    16(%[dst])            \n\t"
395             "sw      %[temp5],    20(%[dst])            \n\t"
396             "sw      %[temp6],    24(%[dst])            \n\t"
397             "sw      %[temp7],    28(%[dst])            \n\t"
398             "sw      $0,          2304(%[dst])          \n\t"
399             "sw      $0,          2308(%[dst])          \n\t"
400             "sw      $0,          2312(%[dst])          \n\t"
401             "sw      $0,          2316(%[dst])          \n\t"
402             "sw      $0,          2320(%[dst])          \n\t"
403             "sw      $0,          2324(%[dst])          \n\t"
404             "sw      $0,          2328(%[dst])          \n\t"
405             "sw      $0,          2332(%[dst])          \n\t"
406             "bne     %[src],      %[loop_end],    1b    \n\t"
407             " addiu  %[dst],      %[dst],         32    \n\t"
408             ".set pop                                   \n\t"
409
410             : [temp0]"=&r"(temp0), [temp1]"=&r"(temp1),
411               [temp2]"=&r"(temp2), [temp3]"=&r"(temp3),
412               [temp4]"=&r"(temp4), [temp5]"=&r"(temp5),
413               [temp6]"=&r"(temp6), [temp7]"=&r"(temp7),
414               [src]"+r"(buff0), [dst]"+r"(buff1)
415             : [loop_end]"r"(loop_end)
416             : "memory"
417         );
418         ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960,     &swindow[64],      64);
419         fmul_and_reverse(saved_ltp + 512, ac->buf_mdct + 960, swindow, 64);
420     } else { // LONG_STOP or ONLY_LONG
421         ac->fdsp->vector_fmul_reverse(saved_ltp,       ac->buf_mdct + 512,     &lwindow[512],     512);
422         fmul_and_reverse(saved_ltp + 512, ac->buf_mdct + 512, lwindow, 512);
423     }
424
425     float_copy(sce->ltp_state, sce->ltp_state + 1024, 1024);
426     float_copy(sce->ltp_state + 1024, sce->ret, 1024);
427     float_copy(sce->ltp_state + 2048, saved_ltp, 1024);
428 }
429 #endif /* HAVE_MIPSFPU */
430 #endif /* HAVE_INLINE_ASM */
431
432 void ff_aacdec_init_mips(AACContext *c)
433 {
434 #if HAVE_INLINE_ASM
435     c->imdct_and_windowing         = imdct_and_windowing_mips;
436     c->apply_ltp                   = apply_ltp_mips;
437 #if HAVE_MIPSFPU
438     c->update_ltp                  = update_ltp_mips;
439 #endif /* HAVE_MIPSFPU */
440 #endif /* HAVE_INLINE_ASM */
441 }