aacdec_mips.c 18.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
/*
 * Copyright (c) 2012
 *      MIPS Technologies, Inc., California.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the MIPS Technologies, Inc., nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE MIPS TECHNOLOGIES, INC. ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE MIPS TECHNOLOGIES, INC. BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Authors:  Darko Laus      (darko@mips.com)
 *           Djordje Pesut   (djordje@mips.com)
 *           Mirjana Vulin   (mvulin@mips.com)
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * @file
 * Reference: libavcodec/aacdec.c
 */

#include "libavcodec/aac.h"
#include "aacdec_mips.h"
#include "libavcodec/aactab.h"
#include "libavcodec/sinewin.h"
59
#include "libavutil/mips/asmdefs.h"
60 61

#if HAVE_INLINE_ASM
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
static av_always_inline void float_copy(float *dst, const float *src, int count)
{
    // Copy 'count' floats from src to dst
    const float *loop_end = src + count;
    int temp[8];

    // count must be a multiple of 8
    av_assert2(count % 8 == 0);

    // loop unrolled 8 times
    __asm__ volatile (
        ".set push                               \n\t"
        ".set noreorder                          \n\t"
    "1:                                          \n\t"
        "lw      %[temp0],    0(%[src])          \n\t"
        "lw      %[temp1],    4(%[src])          \n\t"
        "lw      %[temp2],    8(%[src])          \n\t"
        "lw      %[temp3],    12(%[src])         \n\t"
        "lw      %[temp4],    16(%[src])         \n\t"
        "lw      %[temp5],    20(%[src])         \n\t"
        "lw      %[temp6],    24(%[src])         \n\t"
        "lw      %[temp7],    28(%[src])         \n\t"
84
        PTR_ADDIU "%[src],    %[src],      32    \n\t"
85 86 87 88 89 90 91 92 93
        "sw      %[temp0],    0(%[dst])          \n\t"
        "sw      %[temp1],    4(%[dst])          \n\t"
        "sw      %[temp2],    8(%[dst])          \n\t"
        "sw      %[temp3],    12(%[dst])         \n\t"
        "sw      %[temp4],    16(%[dst])         \n\t"
        "sw      %[temp5],    20(%[dst])         \n\t"
        "sw      %[temp6],    24(%[dst])         \n\t"
        "sw      %[temp7],    28(%[dst])         \n\t"
        "bne     %[src],      %[loop_end], 1b    \n\t"
94
        PTR_ADDIU "%[dst],    %[dst],      32    \n\t"
95 96 97 98 99 100 101 102 103 104 105 106
        ".set pop                                \n\t"

        : [temp0]"=&r"(temp[0]), [temp1]"=&r"(temp[1]),
          [temp2]"=&r"(temp[2]), [temp3]"=&r"(temp[3]),
          [temp4]"=&r"(temp[4]), [temp5]"=&r"(temp[5]),
          [temp6]"=&r"(temp[6]), [temp7]"=&r"(temp[7]),
          [src]"+r"(src), [dst]"+r"(dst)
        : [loop_end]"r"(loop_end)
        : "memory"
    );
}

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
static av_always_inline int lcg_random(unsigned previous_val)
{
    union { unsigned u; int s; } v = { previous_val * 1664525u + 1013904223 };
    return v.s;
}

static void imdct_and_windowing_mips(AACContext *ac, SingleChannelElement *sce)
{
    IndividualChannelStream *ics = &sce->ics;
    float *in    = sce->coeffs;
    float *out   = sce->ret;
    float *saved = sce->saved;
    const float *swindow      = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
    const float *lwindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_long_1024 : ff_sine_1024;
    const float *swindow_prev = ics->use_kb_window[1] ? ff_aac_kbd_short_128 : ff_sine_128;
    float *buf  = ac->buf_mdct;
    int i;

    if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
        for (i = 0; i < 1024; i += 128)
            ac->mdct_small.imdct_half(&ac->mdct_small, buf + i, in + i);
    } else
        ac->mdct.imdct_half(&ac->mdct, buf, in);

    /* window overlapping
     * NOTE: To simplify the overlapping code, all 'meaningless' short to long
     * and long to short transitions are considered to be short to short
     * transitions. This leaves just two cases (long to long and short to short)
     * with a little special sauce for EIGHT_SHORT_SEQUENCE.
     */
    if ((ics->window_sequence[1] == ONLY_LONG_SEQUENCE || ics->window_sequence[1] == LONG_STOP_SEQUENCE) &&
            (ics->window_sequence[0] == ONLY_LONG_SEQUENCE || ics->window_sequence[0] == LONG_START_SEQUENCE)) {
139
        ac->fdsp->vector_fmul_window(    out,               saved,            buf,         lwindow_prev, 512);
140
    } else {
141
        float_copy(out, saved, 448);
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

        if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
            {
                float wi;
                float wj;
                int i;
                float temp0, temp1, temp2, temp3;
                float *dst0 = out + 448 + 0*128;
                float *dst1 = dst0 + 64 + 63;
                float *dst2 = saved + 63;
                float *win0 = (float*)swindow;
                float *win1 = win0 + 64 + 63;
                float *win0_prev = (float*)swindow_prev;
                float *win1_prev = win0_prev + 64 + 63;
                float *src0_prev = saved + 448;
                float *src1_prev = buf + 0*128 + 63;
                float *src0 = buf + 0*128 + 64;
                float *src1 = buf + 1*128 + 63;

                for(i = 0; i < 64; i++)
                {
                    temp0 = src0_prev[0];
                    temp1 = src1_prev[0];
                    wi = *win0_prev;
                    wj = *win1_prev;
                    temp2 = src0[0];
                    temp3 = src1[0];
                    dst0[0] = temp0 * wj - temp1 * wi;
                    dst1[0] = temp0 * wi + temp1 * wj;

                    wi = *win0;
                    wj = *win1;

                    temp0 = src0[128];
                    temp1 = src1[128];
                    dst0[128] = temp2 * wj - temp3 * wi;
                    dst1[128] = temp2 * wi + temp3 * wj;

                    temp2 = src0[256];
                    temp3 = src1[256];
                    dst0[256] = temp0 * wj - temp1 * wi;
                    dst1[256] = temp0 * wi + temp1 * wj;
                    dst0[384] = temp2 * wj - temp3 * wi;
                    dst1[384] = temp2 * wi + temp3 * wj;

                    temp0 = src0[384];
                    temp1 = src1[384];
                    dst0[512] = temp0 * wj - temp1 * wi;
                    dst2[0] = temp0 * wi + temp1 * wj;

                    src0++;
                    src1--;
                    src0_prev++;
                    src1_prev--;
                    win0++;
                    win1--;
                    win0_prev++;
                    win1_prev--;
                    dst0++;
                    dst1--;
                    dst2--;
                }
            }
        } else {
206
            ac->fdsp->vector_fmul_window(out + 448,         saved + 448,      buf,         swindow_prev, 64);
207
            float_copy(out + 576, buf + 64, 448);
208 209 210 211 212
        }
    }

    // buffer update
    if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
213 214 215
        ac->fdsp->vector_fmul_window(saved + 64,  buf + 4*128 + 64, buf + 5*128, swindow, 64);
        ac->fdsp->vector_fmul_window(saved + 192, buf + 5*128 + 64, buf + 6*128, swindow, 64);
        ac->fdsp->vector_fmul_window(saved + 320, buf + 6*128 + 64, buf + 7*128, swindow, 64);
216
        float_copy(saved + 448, buf + 7*128 + 64, 64);
217
    } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
218 219
        float_copy(saved, buf + 512, 448);
        float_copy(saved + 448, buf + 7*128 + 64, 64);
220
    } else { // LONG_STOP or ONLY_LONG
221
        float_copy(saved, buf + 512, 512);
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
    }
}

static void apply_ltp_mips(AACContext *ac, SingleChannelElement *sce)
{
    const LongTermPrediction *ltp = &sce->ics.ltp;
    const uint16_t *offsets = sce->ics.swb_offset;
    int i, sfb;
    int j, k;

    if (sce->ics.window_sequence[0] != EIGHT_SHORT_SEQUENCE) {
        float *predTime = sce->ret;
        float *predFreq = ac->buf_mdct;
        float *p_predTime;
        int16_t num_samples = 2048;

        if (ltp->lag < 1024)
            num_samples = ltp->lag + 1024;
            j = (2048 - num_samples) >> 2;
            k = (2048 - num_samples) & 3;
            p_predTime = &predTime[num_samples];

        for (i = 0; i < num_samples; i++)
            predTime[i] = sce->ltp_state[i + 2048 - ltp->lag] * ltp->coef;
        for (i = 0; i < j; i++) {

            /* loop unrolled 4 times */
            __asm__ volatile (
                "sw      $0,              0(%[p_predTime])        \n\t"
                "sw      $0,              4(%[p_predTime])        \n\t"
                "sw      $0,              8(%[p_predTime])        \n\t"
                "sw      $0,              12(%[p_predTime])       \n\t"
254
                PTR_ADDIU "%[p_predTime], %[p_predTime],     16   \n\t"
255 256 257 258 259 260 261 262 263 264

                : [p_predTime]"+r"(p_predTime)
                :
                : "memory"
            );
        }
        for (i = 0; i < k; i++) {

            __asm__ volatile (
                "sw      $0,              0(%[p_predTime])        \n\t"
265
                PTR_ADDIU "%[p_predTime], %[p_predTime],     4    \n\t"
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

                : [p_predTime]"+r"(p_predTime)
                :
                : "memory"
            );
        }

        ac->windowing_and_mdct_ltp(ac, predFreq, predTime, &sce->ics);

        if (sce->tns.present)
            ac->apply_tns(predFreq, &sce->tns, &sce->ics, 0);

        for (sfb = 0; sfb < FFMIN(sce->ics.max_sfb, MAX_LTP_LONG_SFB); sfb++)
            if (ltp->used[sfb])
                for (i = offsets[sfb]; i < offsets[sfb + 1]; i++)
                    sce->coeffs[i] += predFreq[i];
    }
}

#if HAVE_MIPSFPU
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
static av_always_inline void fmul_and_reverse(float *dst, const float *src0, const float *src1, int count)
{
    /* Multiply 'count' floats in src0 by src1 and store the results in dst in reverse */
    /* This should be equivalent to a normal fmul, followed by reversing dst */

    // count must be a multiple of 4
    av_assert2(count % 4 == 0);

    // move src0 and src1 to the last element of their arrays
    src0 += count - 1;
    src1 += count - 1;

    for (; count > 0; count -= 4){
        float temp[12];

        /* loop unrolled 4 times */
        __asm__ volatile (
            "lwc1    %[temp0],    0(%[ptr2])                \n\t"
            "lwc1    %[temp1],    -4(%[ptr2])               \n\t"
            "lwc1    %[temp2],    -8(%[ptr2])               \n\t"
            "lwc1    %[temp3],    -12(%[ptr2])              \n\t"
            "lwc1    %[temp4],    0(%[ptr3])                \n\t"
            "lwc1    %[temp5],    -4(%[ptr3])               \n\t"
            "lwc1    %[temp6],    -8(%[ptr3])               \n\t"
            "lwc1    %[temp7],    -12(%[ptr3])              \n\t"
            "mul.s   %[temp8],    %[temp0],     %[temp4]    \n\t"
            "mul.s   %[temp9],    %[temp1],     %[temp5]    \n\t"
            "mul.s   %[temp10],   %[temp2],     %[temp6]    \n\t"
            "mul.s   %[temp11],   %[temp3],     %[temp7]    \n\t"
            "swc1    %[temp8],    0(%[ptr1])                \n\t"
            "swc1    %[temp9],    4(%[ptr1])                \n\t"
            "swc1    %[temp10],   8(%[ptr1])                \n\t"
            "swc1    %[temp11],   12(%[ptr1])               \n\t"
319 320 321
            PTR_ADDIU "%[ptr1],   %[ptr1],      16          \n\t"
            PTR_ADDIU "%[ptr2],   %[ptr2],      -16         \n\t"
            PTR_ADDIU "%[ptr3],   %[ptr3],      -16         \n\t"
322 323 324 325 326 327 328 329 330 331 332 333 334 335

            : [temp0]"=&f"(temp[0]), [temp1]"=&f"(temp[1]),
              [temp2]"=&f"(temp[2]), [temp3]"=&f"(temp[3]),
              [temp4]"=&f"(temp[4]), [temp5]"=&f"(temp[5]),
              [temp6]"=&f"(temp[6]), [temp7]"=&f"(temp[7]),
              [temp8]"=&f"(temp[8]), [temp9]"=&f"(temp[9]),
              [temp10]"=&f"(temp[10]), [temp11]"=&f"(temp[11]),
              [ptr1]"+r"(dst), [ptr2]"+r"(src0), [ptr3]"+r"(src1)
            :
            : "memory"
        );
    }
}

336 337 338 339 340 341 342
static void update_ltp_mips(AACContext *ac, SingleChannelElement *sce)
{
    IndividualChannelStream *ics = &sce->ics;
    float *saved     = sce->saved;
    float *saved_ltp = sce->coeffs;
    const float *lwindow = ics->use_kb_window[0] ? ff_aac_kbd_long_1024 : ff_sine_1024;
    const float *swindow = ics->use_kb_window[0] ? ff_aac_kbd_short_128 : ff_sine_128;
343
    float temp0, temp1, temp2, temp3, temp4, temp5, temp6, temp7;
344 345 346

    if (ics->window_sequence[0] == EIGHT_SHORT_SEQUENCE) {
        float *p_saved_ltp = saved_ltp + 576;
347
        float *loop_end1 = p_saved_ltp + 448;
348

349
        float_copy(saved_ltp, saved, 512);
350 351 352 353 354 355 356 357 358 359 360 361

        /* loop unrolled 8 times */
        __asm__ volatile (
        "1:                                                   \n\t"
            "sw     $0,              0(%[p_saved_ltp])        \n\t"
            "sw     $0,              4(%[p_saved_ltp])        \n\t"
            "sw     $0,              8(%[p_saved_ltp])        \n\t"
            "sw     $0,              12(%[p_saved_ltp])       \n\t"
            "sw     $0,              16(%[p_saved_ltp])       \n\t"
            "sw     $0,              20(%[p_saved_ltp])       \n\t"
            "sw     $0,              24(%[p_saved_ltp])       \n\t"
            "sw     $0,              28(%[p_saved_ltp])       \n\t"
362
            PTR_ADDIU "%[p_saved_ltp],%[p_saved_ltp],    32   \n\t"
363 364 365 366 367 368 369
            "bne    %[p_saved_ltp],  %[loop_end1],       1b   \n\t"

            : [p_saved_ltp]"+r"(p_saved_ltp)
            : [loop_end1]"r"(loop_end1)
            : "memory"
        );

370
        ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960,     &swindow[64],      64);
371
        fmul_and_reverse(saved_ltp + 512, ac->buf_mdct + 960, swindow, 64);
372 373 374
    } else if (ics->window_sequence[0] == LONG_START_SEQUENCE) {
        float *buff0 = saved;
        float *buff1 = saved_ltp;
375
        float *loop_end = saved + 448;
376 377 378 379 380 381 382 383 384 385 386 387 388 389

        /* loop unrolled 8 times */
        __asm__ volatile (
            ".set push                                  \n\t"
            ".set noreorder                             \n\t"
        "1:                                             \n\t"
            "lw      %[temp0],    0(%[src])             \n\t"
            "lw      %[temp1],    4(%[src])             \n\t"
            "lw      %[temp2],    8(%[src])             \n\t"
            "lw      %[temp3],    12(%[src])            \n\t"
            "lw      %[temp4],    16(%[src])            \n\t"
            "lw      %[temp5],    20(%[src])            \n\t"
            "lw      %[temp6],    24(%[src])            \n\t"
            "lw      %[temp7],    28(%[src])            \n\t"
390
            PTR_ADDIU "%[src],    %[src],         32    \n\t"
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
            "sw      %[temp0],    0(%[dst])             \n\t"
            "sw      %[temp1],    4(%[dst])             \n\t"
            "sw      %[temp2],    8(%[dst])             \n\t"
            "sw      %[temp3],    12(%[dst])            \n\t"
            "sw      %[temp4],    16(%[dst])            \n\t"
            "sw      %[temp5],    20(%[dst])            \n\t"
            "sw      %[temp6],    24(%[dst])            \n\t"
            "sw      %[temp7],    28(%[dst])            \n\t"
            "sw      $0,          2304(%[dst])          \n\t"
            "sw      $0,          2308(%[dst])          \n\t"
            "sw      $0,          2312(%[dst])          \n\t"
            "sw      $0,          2316(%[dst])          \n\t"
            "sw      $0,          2320(%[dst])          \n\t"
            "sw      $0,          2324(%[dst])          \n\t"
            "sw      $0,          2328(%[dst])          \n\t"
            "sw      $0,          2332(%[dst])          \n\t"
            "bne     %[src],      %[loop_end],    1b    \n\t"
408
            PTR_ADDIU "%[dst],    %[dst],         32    \n\t"
409 410 411 412 413 414 415 416 417 418
            ".set pop                                   \n\t"

            : [temp0]"=&r"(temp0), [temp1]"=&r"(temp1),
              [temp2]"=&r"(temp2), [temp3]"=&r"(temp3),
              [temp4]"=&r"(temp4), [temp5]"=&r"(temp5),
              [temp6]"=&r"(temp6), [temp7]"=&r"(temp7),
              [src]"+r"(buff0), [dst]"+r"(buff1)
            : [loop_end]"r"(loop_end)
            : "memory"
        );
419
        ac->fdsp->vector_fmul_reverse(saved_ltp + 448, ac->buf_mdct + 960,     &swindow[64],      64);
420
        fmul_and_reverse(saved_ltp + 512, ac->buf_mdct + 960, swindow, 64);
421
    } else { // LONG_STOP or ONLY_LONG
422
        ac->fdsp->vector_fmul_reverse(saved_ltp,       ac->buf_mdct + 512,     &lwindow[512],     512);
423
        fmul_and_reverse(saved_ltp + 512, ac->buf_mdct + 512, lwindow, 512);
424 425
    }

426 427 428
    float_copy(sce->ltp_state, sce->ltp_state + 1024, 1024);
    float_copy(sce->ltp_state + 1024, sce->ret, 1024);
    float_copy(sce->ltp_state + 2048, saved_ltp, 1024);
429 430 431 432 433 434 435 436 437 438 439 440 441 442
}
#endif /* HAVE_MIPSFPU */
#endif /* HAVE_INLINE_ASM */

void ff_aacdec_init_mips(AACContext *c)
{
#if HAVE_INLINE_ASM
    c->imdct_and_windowing         = imdct_and_windowing_mips;
    c->apply_ltp                   = apply_ltp_mips;
#if HAVE_MIPSFPU
    c->update_ltp                  = update_ltp_mips;
#endif /* HAVE_MIPSFPU */
#endif /* HAVE_INLINE_ASM */
}