aaccoder_mips.c 103 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 59 60 61 62 63 64
/*
 * 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.
 *
 * Author:  Stanislav Ocovaj (socovaj@mips.com)
 *          Szabolcs Pal     (sabolc@mips.com)
 *
 * AAC coefficients encoder optimized for MIPS floating-point architecture
 *
 * 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/aaccoder.c
 */

#include "libavutil/libm.h"

#include <float.h>
#include "libavutil/mathematics.h"
#include "libavcodec/avcodec.h"
#include "libavcodec/put_bits.h"
#include "libavcodec/aac.h"
#include "libavcodec/aacenc.h"
#include "libavcodec/aactab.h"
65
#include "libavcodec/aacenctab.h"
66
#include "libavcodec/aacenc_utils.h"
67 68

#if HAVE_INLINE_ASM
69
#if !HAVE_MIPS32R6 && !HAVE_MIPS64R6
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 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
typedef struct BandCodingPath {
    int prev_idx;
    float cost;
    int run;
} BandCodingPath;

static const uint8_t uquad_sign_bits[81] = {
    0, 1, 1, 1, 2, 2, 1, 2, 2,
    1, 2, 2, 2, 3, 3, 2, 3, 3,
    1, 2, 2, 2, 3, 3, 2, 3, 3,
    1, 2, 2, 2, 3, 3, 2, 3, 3,
    2, 3, 3, 3, 4, 4, 3, 4, 4,
    2, 3, 3, 3, 4, 4, 3, 4, 4,
    1, 2, 2, 2, 3, 3, 2, 3, 3,
    2, 3, 3, 3, 4, 4, 3, 4, 4,
    2, 3, 3, 3, 4, 4, 3, 4, 4
};

static const uint8_t upair7_sign_bits[64] = {
    0, 1, 1, 1, 1, 1, 1, 1,
    1, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2,
};

static const uint8_t upair12_sign_bits[169] = {
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
};

static const uint8_t esc_sign_bits[289] = {
    0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
    1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
};

/**
 * Functions developed from template function and optimized for quantizing and encoding band
 */
static void quantize_and_encode_band_cost_SQUAD_mips(struct AACEncContext *s,
139
                                                     PutBitContext *pb, const float *in, float *out,
140 141
                                                     const float *scaled, int size, int scale_idx,
                                                     int cb, const float lambda, const float uplim,
142
                                                     int *bits, float *energy, const float ROUNDING)
143 144
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
145
    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
146 147
    int i;
    int qc1, qc2, qc3, qc4;
148
    float qenergy = 0.0f;
149 150 151

    uint8_t  *p_bits  = (uint8_t  *)ff_aac_spectral_bits[cb-1];
    uint16_t *p_codes = (uint16_t *)ff_aac_spectral_codes[cb-1];
152
    float    *p_vec   = (float    *)ff_aac_codebook_vectors[cb-1];
153 154 155 156 157 158

    abs_pow34_v(s->scoefs, in, size);
    scaled = s->scoefs;
    for (i = 0; i < size; i += 4) {
        int curidx;
        int *in_int = (int *)&in[i];
159
        int t0, t1, t2, t3, t4, t5, t6, t7;
160
        const float *vec;
161

162 163 164 165
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
166 167 168 169 170 171 172 173 174

        __asm__ volatile (
            ".set push                      \n\t"
            ".set noreorder                 \n\t"

            "slt    %[qc1], $zero,  %[qc1]  \n\t"
            "slt    %[qc2], $zero,  %[qc2]  \n\t"
            "slt    %[qc3], $zero,  %[qc3]  \n\t"
            "slt    %[qc4], $zero,  %[qc4]  \n\t"
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
            "lw     %[t0],  0(%[in_int])    \n\t"
            "lw     %[t1],  4(%[in_int])    \n\t"
            "lw     %[t2],  8(%[in_int])    \n\t"
            "lw     %[t3],  12(%[in_int])   \n\t"
            "srl    %[t0],  %[t0],  31      \n\t"
            "srl    %[t1],  %[t1],  31      \n\t"
            "srl    %[t2],  %[t2],  31      \n\t"
            "srl    %[t3],  %[t3],  31      \n\t"
            "subu   %[t4],  $zero,  %[qc1]  \n\t"
            "subu   %[t5],  $zero,  %[qc2]  \n\t"
            "subu   %[t6],  $zero,  %[qc3]  \n\t"
            "subu   %[t7],  $zero,  %[qc4]  \n\t"
            "movn   %[qc1], %[t4],  %[t0]   \n\t"
            "movn   %[qc2], %[t5],  %[t1]   \n\t"
            "movn   %[qc3], %[t6],  %[t2]   \n\t"
            "movn   %[qc4], %[t7],  %[t3]   \n\t"
191 192 193 194

            ".set pop                       \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
195 196 197
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4), [t5]"=&r"(t5), [t6]"=&r"(t6), [t7]"=&r"(t7)
198
            : [in_int]"r"(in_int)
199
            : "memory"
200 201 202 203 204 205 206 207 208 209 210 211
        );

        curidx = qc1;
        curidx *= 3;
        curidx += qc2;
        curidx *= 3;
        curidx += qc3;
        curidx *= 3;
        curidx += qc4;
        curidx += 40;

        put_bits(pb, p_bits[curidx], p_codes[curidx]);
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
        if (out || energy) {
            float e1,e2,e3,e4;
            vec = &p_vec[curidx*4];
            e1 = vec[0] * IQ;
            e2 = vec[1] * IQ;
            e3 = vec[2] * IQ;
            e4 = vec[3] * IQ;
            if (out) {
                out[i+0] = e1;
                out[i+1] = e2;
                out[i+2] = e3;
                out[i+3] = e4;
            }
            if (energy)
                qenergy += (e1*e1 + e2*e2) + (e3*e3 + e4*e4);
228
        }
229
    }
230 231
    if (energy)
        *energy = qenergy;
232 233 234
}

static void quantize_and_encode_band_cost_UQUAD_mips(struct AACEncContext *s,
235
                                                     PutBitContext *pb, const float *in, float *out,
236 237
                                                     const float *scaled, int size, int scale_idx,
                                                     int cb, const float lambda, const float uplim,
238
                                                     int *bits, float *energy, const float ROUNDING)
239 240
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
241
    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
242 243
    int i;
    int qc1, qc2, qc3, qc4;
244
    float qenergy = 0.0f;
245 246 247

    uint8_t  *p_bits  = (uint8_t  *)ff_aac_spectral_bits[cb-1];
    uint16_t *p_codes = (uint16_t *)ff_aac_spectral_codes[cb-1];
248
    float    *p_vec   = (float    *)ff_aac_codebook_vectors[cb-1];
249 250 251 252 253 254 255 256

    abs_pow34_v(s->scoefs, in, size);
    scaled = s->scoefs;
    for (i = 0; i < size; i += 4) {
        int curidx, sign, count;
        int *in_int = (int *)&in[i];
        uint8_t v_bits;
        unsigned int v_codes;
257
        int t0, t1, t2, t3, t4;
258
        const float *vec;
259

260 261 262 263
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
264 265 266 267 268

        __asm__ volatile (
            ".set push                              \n\t"
            ".set noreorder                         \n\t"

269
            "ori    %[t4],      $zero,      2       \n\t"
270
            "ori    %[sign],    $zero,      0       \n\t"
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
            "slt    %[t0],      %[t4],      %[qc1]  \n\t"
            "slt    %[t1],      %[t4],      %[qc2]  \n\t"
            "slt    %[t2],      %[t4],      %[qc3]  \n\t"
            "slt    %[t3],      %[t4],      %[qc4]  \n\t"
            "movn   %[qc1],     %[t4],      %[t0]   \n\t"
            "movn   %[qc2],     %[t4],      %[t1]   \n\t"
            "movn   %[qc3],     %[t4],      %[t2]   \n\t"
            "movn   %[qc4],     %[t4],      %[t3]   \n\t"
            "lw     %[t0],      0(%[in_int])        \n\t"
            "lw     %[t1],      4(%[in_int])        \n\t"
            "lw     %[t2],      8(%[in_int])        \n\t"
            "lw     %[t3],      12(%[in_int])       \n\t"
            "slt    %[t0],      %[t0],      $zero   \n\t"
            "movn   %[sign],    %[t0],      %[qc1]  \n\t"
            "slt    %[t1],      %[t1],      $zero   \n\t"
            "slt    %[t2],      %[t2],      $zero   \n\t"
            "slt    %[t3],      %[t3],      $zero   \n\t"
            "sll    %[t0],      %[sign],    1       \n\t"
            "or     %[t0],      %[t0],      %[t1]   \n\t"
            "movn   %[sign],    %[t0],      %[qc2]  \n\t"
            "slt    %[t4],      $zero,      %[qc1]  \n\t"
            "slt    %[t1],      $zero,      %[qc2]  \n\t"
293
            "slt    %[count],   $zero,      %[qc3]  \n\t"
294 295 296 297 298 299 300 301 302 303
            "sll    %[t0],      %[sign],    1       \n\t"
            "or     %[t0],      %[t0],      %[t2]   \n\t"
            "movn   %[sign],    %[t0],      %[qc3]  \n\t"
            "slt    %[t2],      $zero,      %[qc4]  \n\t"
            "addu   %[count],   %[count],   %[t4]   \n\t"
            "addu   %[count],   %[count],   %[t1]   \n\t"
            "sll    %[t0],      %[sign],    1       \n\t"
            "or     %[t0],      %[t0],      %[t3]   \n\t"
            "movn   %[sign],    %[t0],      %[qc4]  \n\t"
            "addu   %[count],   %[count],   %[t2]   \n\t"
304 305 306 307 308

            ".set pop                               \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
309 310 311
              [sign]"=&r"(sign), [count]"=&r"(count),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4)
312
            : [in_int]"r"(in_int)
313
            : "memory"
314 315 316 317 318 319 320 321 322 323 324 325 326
        );

        curidx = qc1;
        curidx *= 3;
        curidx += qc2;
        curidx *= 3;
        curidx += qc3;
        curidx *= 3;
        curidx += qc4;

        v_codes = (p_codes[curidx] << count) | (sign & ((1 << count) - 1));
        v_bits  = p_bits[curidx] + count;
        put_bits(pb, v_bits, v_codes);
327

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
        if (out || energy) {
            float e1,e2,e3,e4;
            vec = &p_vec[curidx*4];
            e1 = copysignf(vec[0] * IQ, in[i+0]);
            e2 = copysignf(vec[1] * IQ, in[i+1]);
            e3 = copysignf(vec[2] * IQ, in[i+2]);
            e4 = copysignf(vec[3] * IQ, in[i+3]);
            if (out) {
                out[i+0] = e1;
                out[i+1] = e2;
                out[i+2] = e3;
                out[i+3] = e4;
            }
            if (energy)
                qenergy += (e1*e1 + e2*e2) + (e3*e3 + e4*e4);
343
        }
344
    }
345 346
    if (energy)
        *energy = qenergy;
347 348 349
}

static void quantize_and_encode_band_cost_SPAIR_mips(struct AACEncContext *s,
350
                                                     PutBitContext *pb, const float *in, float *out,
351 352
                                                     const float *scaled, int size, int scale_idx,
                                                     int cb, const float lambda, const float uplim,
353
                                                     int *bits, float *energy, const float ROUNDING)
354 355
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
356
    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
357 358
    int i;
    int qc1, qc2, qc3, qc4;
359
    float qenergy = 0.0f;
360 361 362

    uint8_t  *p_bits  = (uint8_t  *)ff_aac_spectral_bits[cb-1];
    uint16_t *p_codes = (uint16_t *)ff_aac_spectral_codes[cb-1];
363
    float    *p_vec   = (float    *)ff_aac_codebook_vectors[cb-1];
364 365 366 367 368 369 370 371

    abs_pow34_v(s->scoefs, in, size);
    scaled = s->scoefs;
    for (i = 0; i < size; i += 4) {
        int curidx, curidx2;
        int *in_int = (int *)&in[i];
        uint8_t v_bits;
        unsigned int v_codes;
372
        int t0, t1, t2, t3, t4, t5, t6, t7;
373
        const float *vec1, *vec2;
374

375 376 377 378
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
379 380 381 382 383

        __asm__ volatile (
            ".set push                      \n\t"
            ".set noreorder                 \n\t"

384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
            "ori    %[t4],  $zero,  4       \n\t"
            "slt    %[t0],  %[t4],  %[qc1]  \n\t"
            "slt    %[t1],  %[t4],  %[qc2]  \n\t"
            "slt    %[t2],  %[t4],  %[qc3]  \n\t"
            "slt    %[t3],  %[t4],  %[qc4]  \n\t"
            "movn   %[qc1], %[t4],  %[t0]   \n\t"
            "movn   %[qc2], %[t4],  %[t1]   \n\t"
            "movn   %[qc3], %[t4],  %[t2]   \n\t"
            "movn   %[qc4], %[t4],  %[t3]   \n\t"
            "lw     %[t0],  0(%[in_int])    \n\t"
            "lw     %[t1],  4(%[in_int])    \n\t"
            "lw     %[t2],  8(%[in_int])    \n\t"
            "lw     %[t3],  12(%[in_int])   \n\t"
            "srl    %[t0],  %[t0],  31      \n\t"
            "srl    %[t1],  %[t1],  31      \n\t"
            "srl    %[t2],  %[t2],  31      \n\t"
            "srl    %[t3],  %[t3],  31      \n\t"
            "subu   %[t4],  $zero,  %[qc1]  \n\t"
            "subu   %[t5],  $zero,  %[qc2]  \n\t"
            "subu   %[t6],  $zero,  %[qc3]  \n\t"
            "subu   %[t7],  $zero,  %[qc4]  \n\t"
            "movn   %[qc1], %[t4],  %[t0]   \n\t"
            "movn   %[qc2], %[t5],  %[t1]   \n\t"
            "movn   %[qc3], %[t6],  %[t2]   \n\t"
            "movn   %[qc4], %[t7],  %[t3]   \n\t"
409 410 411 412

            ".set pop                       \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
413 414 415
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4), [t5]"=&r"(t5), [t6]"=&r"(t6), [t7]"=&r"(t7)
416
            : [in_int]"r"(in_int)
417
            : "memory"
418 419 420 421 422 423 424 425 426 427 428
        );

        curidx = 9 * qc1;
        curidx += qc2 + 40;

        curidx2 = 9 * qc3;
        curidx2 += qc4 + 40;

        v_codes = (p_codes[curidx] << p_bits[curidx2]) | (p_codes[curidx2]);
        v_bits  = p_bits[curidx] + p_bits[curidx2];
        put_bits(pb, v_bits, v_codes);
429

430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
        if (out || energy) {
            float e1,e2,e3,e4;
            vec1 = &p_vec[curidx*2 ];
            vec2 = &p_vec[curidx2*2];
            e1 = vec1[0] * IQ;
            e2 = vec1[1] * IQ;
            e3 = vec2[0] * IQ;
            e4 = vec2[1] * IQ;
            if (out) {
                out[i+0] = e1;
                out[i+1] = e2;
                out[i+2] = e3;
                out[i+3] = e4;
            }
            if (energy)
                qenergy += (e1*e1 + e2*e2) + (e3*e3 + e4*e4);
446
        }
447
    }
448 449
    if (energy)
        *energy = qenergy;
450 451 452
}

static void quantize_and_encode_band_cost_UPAIR7_mips(struct AACEncContext *s,
453
                                                      PutBitContext *pb, const float *in, float *out,
454 455
                                                      const float *scaled, int size, int scale_idx,
                                                      int cb, const float lambda, const float uplim,
456
                                                      int *bits, float *energy, const float ROUNDING)
457 458
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
459
    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
460 461
    int i;
    int qc1, qc2, qc3, qc4;
462
    float qenergy = 0.0f;
463 464 465

    uint8_t  *p_bits  = (uint8_t*) ff_aac_spectral_bits[cb-1];
    uint16_t *p_codes = (uint16_t*)ff_aac_spectral_codes[cb-1];
466
    float    *p_vec   = (float    *)ff_aac_codebook_vectors[cb-1];
467 468 469 470

    abs_pow34_v(s->scoefs, in, size);
    scaled = s->scoefs;
    for (i = 0; i < size; i += 4) {
471
        int curidx1, curidx2, sign1, count1, sign2, count2;
472 473 474
        int *in_int = (int *)&in[i];
        uint8_t v_bits;
        unsigned int v_codes;
475
        int t0, t1, t2, t3, t4;
476
        const float *vec1, *vec2;
477

478 479 480 481
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
482 483 484 485 486

        __asm__ volatile (
            ".set push                              \n\t"
            ".set noreorder                         \n\t"

487
            "ori    %[t4],      $zero,      7       \n\t"
488 489
            "ori    %[sign1],   $zero,      0       \n\t"
            "ori    %[sign2],   $zero,      0       \n\t"
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
            "slt    %[t0],      %[t4],      %[qc1]  \n\t"
            "slt    %[t1],      %[t4],      %[qc2]  \n\t"
            "slt    %[t2],      %[t4],      %[qc3]  \n\t"
            "slt    %[t3],      %[t4],      %[qc4]  \n\t"
            "movn   %[qc1],     %[t4],      %[t0]   \n\t"
            "movn   %[qc2],     %[t4],      %[t1]   \n\t"
            "movn   %[qc3],     %[t4],      %[t2]   \n\t"
            "movn   %[qc4],     %[t4],      %[t3]   \n\t"
            "lw     %[t0],      0(%[in_int])        \n\t"
            "lw     %[t1],      4(%[in_int])        \n\t"
            "lw     %[t2],      8(%[in_int])        \n\t"
            "lw     %[t3],      12(%[in_int])       \n\t"
            "slt    %[t0],      %[t0],      $zero   \n\t"
            "movn   %[sign1],   %[t0],      %[qc1]  \n\t"
            "slt    %[t2],      %[t2],      $zero   \n\t"
            "movn   %[sign2],   %[t2],      %[qc3]  \n\t"
            "slt    %[t1],      %[t1],      $zero   \n\t"
            "sll    %[t0],      %[sign1],   1       \n\t"
            "or     %[t0],      %[t0],      %[t1]   \n\t"
            "movn   %[sign1],   %[t0],      %[qc2]  \n\t"
            "slt    %[t3],      %[t3],      $zero   \n\t"
            "sll    %[t0],      %[sign2],   1       \n\t"
            "or     %[t0],      %[t0],      %[t3]   \n\t"
            "movn   %[sign2],   %[t0],      %[qc4]  \n\t"
514
            "slt    %[count1],  $zero,      %[qc1]  \n\t"
515
            "slt    %[t1],      $zero,      %[qc2]  \n\t"
516
            "slt    %[count2],  $zero,      %[qc3]  \n\t"
517 518 519
            "slt    %[t2],      $zero,      %[qc4]  \n\t"
            "addu   %[count1],  %[count1],  %[t1]   \n\t"
            "addu   %[count2],  %[count2],  %[t2]   \n\t"
520 521 522 523 524 525

            ".set pop                               \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [sign1]"=&r"(sign1), [count1]"=&r"(count1),
526 527 528
              [sign2]"=&r"(sign2), [count2]"=&r"(count2),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4)
529 530 531 532 533
            : [in_int]"r"(in_int)
            : "t0", "t1", "t2", "t3", "t4",
              "memory"
        );

534 535
        curidx1  = 8 * qc1;
        curidx1 += qc2;
536

537 538
        v_codes = (p_codes[curidx1] << count1) | sign1;
        v_bits  = p_bits[curidx1] + count1;
539 540
        put_bits(pb, v_bits, v_codes);

541 542
        curidx2  = 8 * qc3;
        curidx2 += qc4;
543

544 545
        v_codes = (p_codes[curidx2] << count2) | sign2;
        v_bits  = p_bits[curidx2] + count2;
546
        put_bits(pb, v_bits, v_codes);
547

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
        if (out || energy) {
            float e1,e2,e3,e4;
            vec1 = &p_vec[curidx1*2];
            vec2 = &p_vec[curidx2*2];
            e1 = copysignf(vec1[0] * IQ, in[i+0]);
            e2 = copysignf(vec1[1] * IQ, in[i+1]);
            e3 = copysignf(vec2[0] * IQ, in[i+2]);
            e4 = copysignf(vec2[1] * IQ, in[i+3]);
            if (out) {
                out[i+0] = e1;
                out[i+1] = e2;
                out[i+2] = e3;
                out[i+3] = e4;
            }
            if (energy)
                qenergy += (e1*e1 + e2*e2) + (e3*e3 + e4*e4);
564
        }
565
    }
566 567
    if (energy)
        *energy = qenergy;
568 569 570
}

static void quantize_and_encode_band_cost_UPAIR12_mips(struct AACEncContext *s,
571
                                                       PutBitContext *pb, const float *in, float *out,
572 573
                                                       const float *scaled, int size, int scale_idx,
                                                       int cb, const float lambda, const float uplim,
574
                                                       int *bits, float *energy, const float ROUNDING)
575 576
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
577
    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
578 579
    int i;
    int qc1, qc2, qc3, qc4;
580
    float qenergy = 0.0f;
581 582 583

    uint8_t  *p_bits  = (uint8_t*) ff_aac_spectral_bits[cb-1];
    uint16_t *p_codes = (uint16_t*)ff_aac_spectral_codes[cb-1];
584
    float    *p_vec   = (float   *)ff_aac_codebook_vectors[cb-1];
585 586 587 588

    abs_pow34_v(s->scoefs, in, size);
    scaled = s->scoefs;
    for (i = 0; i < size; i += 4) {
589
        int curidx1, curidx2, sign1, count1, sign2, count2;
590 591 592
        int *in_int = (int *)&in[i];
        uint8_t v_bits;
        unsigned int v_codes;
593
        int t0, t1, t2, t3, t4;
594
        const float *vec1, *vec2;
595

596 597 598 599
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
600 601 602 603 604

        __asm__ volatile (
            ".set push                              \n\t"
            ".set noreorder                         \n\t"

605
            "ori    %[t4],      $zero,      12      \n\t"
606 607
            "ori    %[sign1],   $zero,      0       \n\t"
            "ori    %[sign2],   $zero,      0       \n\t"
608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
            "slt    %[t0],      %[t4],      %[qc1]  \n\t"
            "slt    %[t1],      %[t4],      %[qc2]  \n\t"
            "slt    %[t2],      %[t4],      %[qc3]  \n\t"
            "slt    %[t3],      %[t4],      %[qc4]  \n\t"
            "movn   %[qc1],     %[t4],      %[t0]   \n\t"
            "movn   %[qc2],     %[t4],      %[t1]   \n\t"
            "movn   %[qc3],     %[t4],      %[t2]   \n\t"
            "movn   %[qc4],     %[t4],      %[t3]   \n\t"
            "lw     %[t0],      0(%[in_int])        \n\t"
            "lw     %[t1],      4(%[in_int])        \n\t"
            "lw     %[t2],      8(%[in_int])        \n\t"
            "lw     %[t3],      12(%[in_int])       \n\t"
            "slt    %[t0],      %[t0],      $zero   \n\t"
            "movn   %[sign1],   %[t0],      %[qc1]  \n\t"
            "slt    %[t2],      %[t2],      $zero   \n\t"
            "movn   %[sign2],   %[t2],      %[qc3]  \n\t"
            "slt    %[t1],      %[t1],      $zero   \n\t"
            "sll    %[t0],      %[sign1],   1       \n\t"
            "or     %[t0],      %[t0],      %[t1]   \n\t"
            "movn   %[sign1],   %[t0],      %[qc2]  \n\t"
            "slt    %[t3],      %[t3],      $zero   \n\t"
            "sll    %[t0],      %[sign2],   1       \n\t"
            "or     %[t0],      %[t0],      %[t3]   \n\t"
            "movn   %[sign2],   %[t0],      %[qc4]  \n\t"
632
            "slt    %[count1],  $zero,      %[qc1]  \n\t"
633
            "slt    %[t1],      $zero,      %[qc2]  \n\t"
634
            "slt    %[count2],  $zero,      %[qc3]  \n\t"
635 636 637
            "slt    %[t2],      $zero,      %[qc4]  \n\t"
            "addu   %[count1],  %[count1],  %[t1]   \n\t"
            "addu   %[count2],  %[count2],  %[t2]   \n\t"
638 639 640 641 642 643

            ".set pop                               \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [sign1]"=&r"(sign1), [count1]"=&r"(count1),
644 645 646
              [sign2]"=&r"(sign2), [count2]"=&r"(count2),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4)
647
            : [in_int]"r"(in_int)
648
            : "memory"
649 650
        );

651 652
        curidx1  = 13 * qc1;
        curidx1 += qc2;
653

654 655
        v_codes = (p_codes[curidx1] << count1) | sign1;
        v_bits  = p_bits[curidx1] + count1;
656 657
        put_bits(pb, v_bits, v_codes);

658 659
        curidx2  = 13 * qc3;
        curidx2 += qc4;
660

661 662
        v_codes = (p_codes[curidx2] << count2) | sign2;
        v_bits  = p_bits[curidx2] + count2;
663
        put_bits(pb, v_bits, v_codes);
664

665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
        if (out || energy) {
            float e1,e2,e3,e4;
            vec1 = &p_vec[curidx1*2];
            vec2 = &p_vec[curidx2*2];
            e1 = copysignf(vec1[0] * IQ, in[i+0]);
            e2 = copysignf(vec1[1] * IQ, in[i+1]);
            e3 = copysignf(vec2[0] * IQ, in[i+2]);
            e4 = copysignf(vec2[1] * IQ, in[i+3]);
            if (out) {
                out[i+0] = e1;
                out[i+1] = e2;
                out[i+2] = e3;
                out[i+3] = e4;
            }
            if (energy)
                qenergy += (e1*e1 + e2*e2) + (e3*e3 + e4*e4);
681
        }
682
    }
683 684
    if (energy)
        *energy = qenergy;
685 686 687
}

static void quantize_and_encode_band_cost_ESC_mips(struct AACEncContext *s,
688
                                                   PutBitContext *pb, const float *in, float *out,
689 690
                                                   const float *scaled, int size, int scale_idx,
                                                   int cb, const float lambda, const float uplim,
691
                                                   int *bits, float *energy, const float ROUNDING)
692 693
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
694
    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
695 696
    int i;
    int qc1, qc2, qc3, qc4;
697
    float qenergy = 0.0f;
698 699 700 701 702 703 704 705 706 707 708 709 710 711

    uint8_t  *p_bits    = (uint8_t* )ff_aac_spectral_bits[cb-1];
    uint16_t *p_codes   = (uint16_t*)ff_aac_spectral_codes[cb-1];
    float    *p_vectors = (float*   )ff_aac_codebook_vectors[cb-1];

    abs_pow34_v(s->scoefs, in, size);
    scaled = s->scoefs;

    if (cb < 11) {
        for (i = 0; i < size; i += 4) {
            int curidx, curidx2, sign1, count1, sign2, count2;
            int *in_int = (int *)&in[i];
            uint8_t v_bits;
            unsigned int v_codes;
712
            int t0, t1, t2, t3, t4;
713
            const float *vec1, *vec2;
714

715 716 717 718
            qc1 = scaled[i  ] * Q34 + ROUNDING;
            qc2 = scaled[i+1] * Q34 + ROUNDING;
            qc3 = scaled[i+2] * Q34 + ROUNDING;
            qc4 = scaled[i+3] * Q34 + ROUNDING;
719 720 721 722 723

            __asm__ volatile (
                ".set push                                  \n\t"
                ".set noreorder                             \n\t"

724
                "ori        %[t4],      $zero,      16      \n\t"
725 726
                "ori        %[sign1],   $zero,      0       \n\t"
                "ori        %[sign2],   $zero,      0       \n\t"
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
                "slt        %[t0],      %[t4],      %[qc1]  \n\t"
                "slt        %[t1],      %[t4],      %[qc2]  \n\t"
                "slt        %[t2],      %[t4],      %[qc3]  \n\t"
                "slt        %[t3],      %[t4],      %[qc4]  \n\t"
                "movn       %[qc1],     %[t4],      %[t0]   \n\t"
                "movn       %[qc2],     %[t4],      %[t1]   \n\t"
                "movn       %[qc3],     %[t4],      %[t2]   \n\t"
                "movn       %[qc4],     %[t4],      %[t3]   \n\t"
                "lw         %[t0],      0(%[in_int])        \n\t"
                "lw         %[t1],      4(%[in_int])        \n\t"
                "lw         %[t2],      8(%[in_int])        \n\t"
                "lw         %[t3],      12(%[in_int])       \n\t"
                "slt        %[t0],      %[t0],      $zero   \n\t"
                "movn       %[sign1],   %[t0],      %[qc1]  \n\t"
                "slt        %[t2],      %[t2],      $zero   \n\t"
                "movn       %[sign2],   %[t2],      %[qc3]  \n\t"
                "slt        %[t1],      %[t1],      $zero   \n\t"
                "sll        %[t0],      %[sign1],   1       \n\t"
                "or         %[t0],      %[t0],      %[t1]   \n\t"
                "movn       %[sign1],   %[t0],      %[qc2]  \n\t"
                "slt        %[t3],      %[t3],      $zero   \n\t"
                "sll        %[t0],      %[sign2],   1       \n\t"
                "or         %[t0],      %[t0],      %[t3]   \n\t"
                "movn       %[sign2],   %[t0],      %[qc4]  \n\t"
751
                "slt        %[count1],  $zero,      %[qc1]  \n\t"
752
                "slt        %[t1],      $zero,      %[qc2]  \n\t"
753
                "slt        %[count2],  $zero,      %[qc3]  \n\t"
754 755 756
                "slt        %[t2],      $zero,      %[qc4]  \n\t"
                "addu       %[count1],  %[count1],  %[t1]   \n\t"
                "addu       %[count2],  %[count2],  %[t2]   \n\t"
757 758 759 760 761 762

                ".set pop                                   \n\t"

                : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
                  [qc3]"+r"(qc3), [qc4]"+r"(qc4),
                  [sign1]"=&r"(sign1), [count1]"=&r"(count1),
763 764 765
                  [sign2]"=&r"(sign2), [count2]"=&r"(count2),
                  [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
                  [t4]"=&r"(t4)
766
                : [in_int]"r"(in_int)
767
                : "memory"
768 769 770 771 772 773 774 775 776 777 778 779 780 781
            );

            curidx = 17 * qc1;
            curidx += qc2;
            curidx2 = 17 * qc3;
            curidx2 += qc4;

            v_codes = (p_codes[curidx] << count1) | sign1;
            v_bits  = p_bits[curidx] + count1;
            put_bits(pb, v_bits, v_codes);

            v_codes = (p_codes[curidx2] << count2) | sign2;
            v_bits  = p_bits[curidx2] + count2;
            put_bits(pb, v_bits, v_codes);
782

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
            if (out || energy) {
                float e1,e2,e3,e4;
                vec1 = &p_vectors[curidx*2 ];
                vec2 = &p_vectors[curidx2*2];
                e1 = copysignf(vec1[0] * IQ, in[i+0]);
                e2 = copysignf(vec1[1] * IQ, in[i+1]);
                e3 = copysignf(vec2[0] * IQ, in[i+2]);
                e4 = copysignf(vec2[1] * IQ, in[i+3]);
                if (out) {
                    out[i+0] = e1;
                    out[i+1] = e2;
                    out[i+2] = e3;
                    out[i+3] = e4;
                }
                if (energy)
                    qenergy += (e1*e1 + e2*e2) + (e3*e3 + e4*e4);
799
            }
800 801 802 803 804 805 806 807
        }
    } else {
        for (i = 0; i < size; i += 4) {
            int curidx, curidx2, sign1, count1, sign2, count2;
            int *in_int = (int *)&in[i];
            uint8_t v_bits;
            unsigned int v_codes;
            int c1, c2, c3, c4;
808
            int t0, t1, t2, t3, t4;
809

810 811 812 813
            qc1 = scaled[i  ] * Q34 + ROUNDING;
            qc2 = scaled[i+1] * Q34 + ROUNDING;
            qc3 = scaled[i+2] * Q34 + ROUNDING;
            qc4 = scaled[i+3] * Q34 + ROUNDING;
814 815 816 817 818

            __asm__ volatile (
                ".set push                                  \n\t"
                ".set noreorder                             \n\t"

819
                "ori        %[t4],      $zero,      16      \n\t"
820 821 822 823 824 825 826 827 828 829
                "ori        %[sign1],   $zero,      0       \n\t"
                "ori        %[sign2],   $zero,      0       \n\t"
                "shll_s.w   %[c1],      %[qc1],     18      \n\t"
                "shll_s.w   %[c2],      %[qc2],     18      \n\t"
                "shll_s.w   %[c3],      %[qc3],     18      \n\t"
                "shll_s.w   %[c4],      %[qc4],     18      \n\t"
                "srl        %[c1],      %[c1],      18      \n\t"
                "srl        %[c2],      %[c2],      18      \n\t"
                "srl        %[c3],      %[c3],      18      \n\t"
                "srl        %[c4],      %[c4],      18      \n\t"
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853
                "slt        %[t0],      %[t4],      %[qc1]  \n\t"
                "slt        %[t1],      %[t4],      %[qc2]  \n\t"
                "slt        %[t2],      %[t4],      %[qc3]  \n\t"
                "slt        %[t3],      %[t4],      %[qc4]  \n\t"
                "movn       %[qc1],     %[t4],      %[t0]   \n\t"
                "movn       %[qc2],     %[t4],      %[t1]   \n\t"
                "movn       %[qc3],     %[t4],      %[t2]   \n\t"
                "movn       %[qc4],     %[t4],      %[t3]   \n\t"
                "lw         %[t0],      0(%[in_int])        \n\t"
                "lw         %[t1],      4(%[in_int])        \n\t"
                "lw         %[t2],      8(%[in_int])        \n\t"
                "lw         %[t3],      12(%[in_int])       \n\t"
                "slt        %[t0],      %[t0],      $zero   \n\t"
                "movn       %[sign1],   %[t0],      %[qc1]  \n\t"
                "slt        %[t2],      %[t2],      $zero   \n\t"
                "movn       %[sign2],   %[t2],      %[qc3]  \n\t"
                "slt        %[t1],      %[t1],      $zero   \n\t"
                "sll        %[t0],      %[sign1],   1       \n\t"
                "or         %[t0],      %[t0],      %[t1]   \n\t"
                "movn       %[sign1],   %[t0],      %[qc2]  \n\t"
                "slt        %[t3],      %[t3],      $zero   \n\t"
                "sll        %[t0],      %[sign2],   1       \n\t"
                "or         %[t0],      %[t0],      %[t3]   \n\t"
                "movn       %[sign2],   %[t0],      %[qc4]  \n\t"
854
                "slt        %[count1],  $zero,      %[qc1]  \n\t"
855
                "slt        %[t1],      $zero,      %[qc2]  \n\t"
856
                "slt        %[count2],  $zero,      %[qc3]  \n\t"
857 858 859
                "slt        %[t2],      $zero,      %[qc4]  \n\t"
                "addu       %[count1],  %[count1],  %[t1]   \n\t"
                "addu       %[count2],  %[count2],  %[t2]   \n\t"
860 861 862 863 864 865 866 867

                ".set pop                                   \n\t"

                : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
                  [qc3]"+r"(qc3), [qc4]"+r"(qc4),
                  [sign1]"=&r"(sign1), [count1]"=&r"(count1),
                  [sign2]"=&r"(sign2), [count2]"=&r"(count2),
                  [c1]"=&r"(c1), [c2]"=&r"(c2),
868 869 870
                  [c3]"=&r"(c3), [c4]"=&r"(c4),
                  [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
                  [t4]"=&r"(t4)
871
                : [in_int]"r"(in_int)
872
                : "memory"
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
            );

            curidx = 17 * qc1;
            curidx += qc2;

            curidx2 = 17 * qc3;
            curidx2 += qc4;

            v_codes = (p_codes[curidx] << count1) | sign1;
            v_bits  = p_bits[curidx] + count1;
            put_bits(pb, v_bits, v_codes);

            if (p_vectors[curidx*2  ] == 64.0f) {
                int len = av_log2(c1);
                v_codes = (((1 << (len - 3)) - 2) << len) | (c1 & ((1 << len) - 1));
                put_bits(pb, len * 2 - 3, v_codes);
            }
            if (p_vectors[curidx*2+1] == 64.0f) {
                int len = av_log2(c2);
                v_codes = (((1 << (len - 3)) - 2) << len) | (c2 & ((1 << len) - 1));
                put_bits(pb, len*2-3, v_codes);
            }

            v_codes = (p_codes[curidx2] << count2) | sign2;
            v_bits  = p_bits[curidx2] + count2;
            put_bits(pb, v_bits, v_codes);

            if (p_vectors[curidx2*2  ] == 64.0f) {
                int len = av_log2(c3);
                v_codes = (((1 << (len - 3)) - 2) << len) | (c3 & ((1 << len) - 1));
                put_bits(pb, len* 2 - 3, v_codes);
            }
            if (p_vectors[curidx2*2+1] == 64.0f) {
                int len = av_log2(c4);
                v_codes = (((1 << (len - 3)) - 2) << len) | (c4 & ((1 << len) - 1));
                put_bits(pb, len * 2 - 3, v_codes);
            }
910

911 912 913 914 915 916 917 918 919 920 921 922 923 924
            if (out || energy) {
                float e1, e2, e3, e4;
                e1 = copysignf(c1 * cbrtf(c1) * IQ, in[i+0]);
                e2 = copysignf(c2 * cbrtf(c2) * IQ, in[i+1]);
                e3 = copysignf(c3 * cbrtf(c3) * IQ, in[i+2]);
                e4 = copysignf(c4 * cbrtf(c4) * IQ, in[i+3]);
                if (out) {
                    out[i+0] = e1;
                    out[i+1] = e2;
                    out[i+2] = e3;
                    out[i+3] = e4;
                }
                if (energy)
                    qenergy += (e1*e1 + e2*e2) + (e3*e3 + e4*e4);
925
            }
926 927
        }
    }
928 929
    if (energy)
        *energy = qenergy;
930 931
}

932 933 934 935
static void quantize_and_encode_band_cost_NONE_mips(struct AACEncContext *s,
                                                         PutBitContext *pb, const float *in, float *out,
                                                         const float *scaled, int size, int scale_idx,
                                                         int cb, const float lambda, const float uplim,
936
                                                         int *bits, float *energy, const float ROUNDING) {
937 938 939 940 941 942 943
    av_assert0(0);
}

static void quantize_and_encode_band_cost_ZERO_mips(struct AACEncContext *s,
                                                         PutBitContext *pb, const float *in, float *out,
                                                         const float *scaled, int size, int scale_idx,
                                                         int cb, const float lambda, const float uplim,
944
                                                         int *bits, float *energy, const float ROUNDING) {
945 946 947 948 949 950 951 952 953 954 955
    int i;
    if (bits)
        *bits = 0;
    if (out) {
        for (i = 0; i < size; i += 4) {
           out[i  ] = 0.0f;
           out[i+1] = 0.0f;
           out[i+2] = 0.0f;
           out[i+3] = 0.0f;
        }
    }
956 957
    if (energy)
        *energy = 0.0f;
958 959
}

960
static void (*const quantize_and_encode_band_cost_arr[])(struct AACEncContext *s,
961
                                                         PutBitContext *pb, const float *in, float *out,
962 963
                                                         const float *scaled, int size, int scale_idx,
                                                         int cb, const float lambda, const float uplim,
964
                                                         int *bits, float *energy, const float ROUNDING) = {
965
    quantize_and_encode_band_cost_ZERO_mips,
966 967 968 969 970 971 972 973 974 975 976
    quantize_and_encode_band_cost_SQUAD_mips,
    quantize_and_encode_band_cost_SQUAD_mips,
    quantize_and_encode_band_cost_UQUAD_mips,
    quantize_and_encode_band_cost_UQUAD_mips,
    quantize_and_encode_band_cost_SPAIR_mips,
    quantize_and_encode_band_cost_SPAIR_mips,
    quantize_and_encode_band_cost_UPAIR7_mips,
    quantize_and_encode_band_cost_UPAIR7_mips,
    quantize_and_encode_band_cost_UPAIR12_mips,
    quantize_and_encode_band_cost_UPAIR12_mips,
    quantize_and_encode_band_cost_ESC_mips,
977 978 979 980
    quantize_and_encode_band_cost_NONE_mips, /* cb 12 doesn't exist */
    quantize_and_encode_band_cost_ZERO_mips,
    quantize_and_encode_band_cost_ZERO_mips,
    quantize_and_encode_band_cost_ZERO_mips,
981 982
};

983 984
#define quantize_and_encode_band_cost(                                       \
                                s, pb, in, out, scaled, size, scale_idx, cb, \
985
                                lambda, uplim, bits, energy, ROUNDING)       \
986 987
    quantize_and_encode_band_cost_arr[cb](                                   \
                                s, pb, in, out, scaled, size, scale_idx, cb, \
988
                                lambda, uplim, bits, energy, ROUNDING)
989 990

static void quantize_and_encode_band_mips(struct AACEncContext *s, PutBitContext *pb,
991 992
                                          const float *in, float *out, int size, int scale_idx,
                                          int cb, const float lambda, int rtz)
993
{
994
    quantize_and_encode_band_cost(s, pb, in, out, NULL, size, scale_idx, cb, lambda,
995
                                  INFINITY, NULL, NULL, (rtz) ? ROUND_TO_ZERO : ROUND_STANDARD);
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
}

/**
 * Functions developed from template function and optimized for getting the number of bits
 */
static float get_band_numbits_ZERO_mips(struct AACEncContext *s,
                                        PutBitContext *pb, const float *in,
                                        const float *scaled, int size, int scale_idx,
                                        int cb, const float lambda, const float uplim,
                                        int *bits)
{
    return 0;
}

1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
static float get_band_numbits_NONE_mips(struct AACEncContext *s,
                                        PutBitContext *pb, const float *in,
                                        const float *scaled, int size, int scale_idx,
                                        int cb, const float lambda, const float uplim,
                                        int *bits)
{
    av_assert0(0);
    return 0;
}

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
static float get_band_numbits_SQUAD_mips(struct AACEncContext *s,
                                         PutBitContext *pb, const float *in,
                                         const float *scaled, int size, int scale_idx,
                                         int cb, const float lambda, const float uplim,
                                         int *bits)
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    int i;
    int qc1, qc2, qc3, qc4;
    int curbits = 0;

    uint8_t *p_bits = (uint8_t *)ff_aac_spectral_bits[cb-1];

    for (i = 0; i < size; i += 4) {
        int curidx;
        int *in_int = (int *)&in[i];
1036
        int t0, t1, t2, t3, t4, t5, t6, t7;
1037

1038 1039 1040 1041
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
1042 1043 1044 1045 1046 1047 1048 1049 1050

        __asm__ volatile (
            ".set push                      \n\t"
            ".set noreorder                 \n\t"

            "slt    %[qc1], $zero,  %[qc1]  \n\t"
            "slt    %[qc2], $zero,  %[qc2]  \n\t"
            "slt    %[qc3], $zero,  %[qc3]  \n\t"
            "slt    %[qc4], $zero,  %[qc4]  \n\t"
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
            "lw     %[t0],  0(%[in_int])    \n\t"
            "lw     %[t1],  4(%[in_int])    \n\t"
            "lw     %[t2],  8(%[in_int])    \n\t"
            "lw     %[t3],  12(%[in_int])   \n\t"
            "srl    %[t0],  %[t0],  31      \n\t"
            "srl    %[t1],  %[t1],  31      \n\t"
            "srl    %[t2],  %[t2],  31      \n\t"
            "srl    %[t3],  %[t3],  31      \n\t"
            "subu   %[t4],  $zero,  %[qc1]  \n\t"
            "subu   %[t5],  $zero,  %[qc2]  \n\t"
            "subu   %[t6],  $zero,  %[qc3]  \n\t"
            "subu   %[t7],  $zero,  %[qc4]  \n\t"
            "movn   %[qc1], %[t4],  %[t0]   \n\t"
            "movn   %[qc2], %[t5],  %[t1]   \n\t"
            "movn   %[qc3], %[t6],  %[t2]   \n\t"
            "movn   %[qc4], %[t7],  %[t3]   \n\t"
1067 1068 1069 1070

            ".set pop                       \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
1071 1072 1073
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4), [t5]"=&r"(t5), [t6]"=&r"(t6), [t7]"=&r"(t7)
1074
            : [in_int]"r"(in_int)
1075
            : "memory"
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
        );

        curidx = qc1;
        curidx *= 3;
        curidx += qc2;
        curidx *= 3;
        curidx += qc3;
        curidx *= 3;
        curidx += qc4;
        curidx += 40;

        curbits += p_bits[curidx];
    }
    return curbits;
}

static float get_band_numbits_UQUAD_mips(struct AACEncContext *s,
                                         PutBitContext *pb, const float *in,
                                         const float *scaled, int size, int scale_idx,
                                         int cb, const float lambda, const float uplim,
                                         int *bits)
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    int i;
    int curbits = 0;
    int qc1, qc2, qc3, qc4;

    uint8_t *p_bits = (uint8_t *)ff_aac_spectral_bits[cb-1];

    for (i = 0; i < size; i += 4) {
        int curidx;
1107
        int t0, t1, t2, t3, t4;
1108

1109 1110 1111 1112
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
1113 1114 1115 1116 1117

        __asm__ volatile (
            ".set push                      \n\t"
            ".set noreorder                 \n\t"

1118 1119 1120 1121 1122 1123 1124 1125 1126
            "ori    %[t4],  $zero,  2       \n\t"
            "slt    %[t0],  %[t4],  %[qc1]  \n\t"
            "slt    %[t1],  %[t4],  %[qc2]  \n\t"
            "slt    %[t2],  %[t4],  %[qc3]  \n\t"
            "slt    %[t3],  %[t4],  %[qc4]  \n\t"
            "movn   %[qc1], %[t4],  %[t0]   \n\t"
            "movn   %[qc2], %[t4],  %[t1]   \n\t"
            "movn   %[qc3], %[t4],  %[t2]   \n\t"
            "movn   %[qc4], %[t4],  %[t3]   \n\t"
1127 1128 1129 1130

            ".set pop                       \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
1131 1132 1133
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4)
1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
        );

        curidx = qc1;
        curidx *= 3;
        curidx += qc2;
        curidx *= 3;
        curidx += qc3;
        curidx *= 3;
        curidx += qc4;

        curbits += p_bits[curidx];
        curbits += uquad_sign_bits[curidx];
    }
    return curbits;
}

static float get_band_numbits_SPAIR_mips(struct AACEncContext *s,
                                         PutBitContext *pb, const float *in,
                                         const float *scaled, int size, int scale_idx,
                                         int cb, const float lambda, const float uplim,
                                         int *bits)
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    int i;
    int qc1, qc2, qc3, qc4;
    int curbits = 0;

    uint8_t *p_bits = (uint8_t*)ff_aac_spectral_bits[cb-1];

    for (i = 0; i < size; i += 4) {
        int curidx, curidx2;
        int *in_int = (int *)&in[i];
1166
        int t0, t1, t2, t3, t4, t5, t6, t7;
1167

1168 1169 1170 1171
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
1172 1173 1174 1175 1176

        __asm__ volatile (
            ".set push                      \n\t"
            ".set noreorder                 \n\t"

1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
            "ori    %[t4],  $zero,  4       \n\t"
            "slt    %[t0],  %[t4],  %[qc1]  \n\t"
            "slt    %[t1],  %[t4],  %[qc2]  \n\t"
            "slt    %[t2],  %[t4],  %[qc3]  \n\t"
            "slt    %[t3],  %[t4],  %[qc4]  \n\t"
            "movn   %[qc1], %[t4],  %[t0]   \n\t"
            "movn   %[qc2], %[t4],  %[t1]   \n\t"
            "movn   %[qc3], %[t4],  %[t2]   \n\t"
            "movn   %[qc4], %[t4],  %[t3]   \n\t"
            "lw     %[t0],  0(%[in_int])    \n\t"
            "lw     %[t1],  4(%[in_int])    \n\t"
            "lw     %[t2],  8(%[in_int])    \n\t"
            "lw     %[t3],  12(%[in_int])   \n\t"
            "srl    %[t0],  %[t0],  31      \n\t"
            "srl    %[t1],  %[t1],  31      \n\t"
            "srl    %[t2],  %[t2],  31      \n\t"
            "srl    %[t3],  %[t3],  31      \n\t"
            "subu   %[t4],  $zero,  %[qc1]  \n\t"
            "subu   %[t5],  $zero,  %[qc2]  \n\t"
            "subu   %[t6],  $zero,  %[qc3]  \n\t"
            "subu   %[t7],  $zero,  %[qc4]  \n\t"
            "movn   %[qc1], %[t4],  %[t0]   \n\t"
            "movn   %[qc2], %[t5],  %[t1]   \n\t"
            "movn   %[qc3], %[t6],  %[t2]   \n\t"
            "movn   %[qc4], %[t7],  %[t3]   \n\t"
1202 1203 1204 1205

            ".set pop                       \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
1206 1207 1208
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4), [t5]"=&r"(t5), [t6]"=&r"(t6), [t7]"=&r"(t7)
1209
            : [in_int]"r"(in_int)
1210
            : "memory"
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238
        );

        curidx  = 9 * qc1;
        curidx += qc2 + 40;

        curidx2  = 9 * qc3;
        curidx2 += qc4 + 40;

        curbits += p_bits[curidx] + p_bits[curidx2];
    }
    return curbits;
}

static float get_band_numbits_UPAIR7_mips(struct AACEncContext *s,
                                          PutBitContext *pb, const float *in,
                                          const float *scaled, int size, int scale_idx,
                                          int cb, const float lambda, const float uplim,
                                          int *bits)
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    int i;
    int qc1, qc2, qc3, qc4;
    int curbits = 0;

    uint8_t *p_bits = (uint8_t *)ff_aac_spectral_bits[cb-1];

    for (i = 0; i < size; i += 4) {
        int curidx, curidx2;
1239
        int t0, t1, t2, t3, t4;
1240

1241 1242 1243 1244
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
1245 1246 1247 1248 1249

        __asm__ volatile (
            ".set push                      \n\t"
            ".set noreorder                 \n\t"

1250 1251 1252 1253 1254 1255 1256 1257 1258
            "ori    %[t4],  $zero,  7       \n\t"
            "slt    %[t0],  %[t4],  %[qc1]  \n\t"
            "slt    %[t1],  %[t4],  %[qc2]  \n\t"
            "slt    %[t2],  %[t4],  %[qc3]  \n\t"
            "slt    %[t3],  %[t4],  %[qc4]  \n\t"
            "movn   %[qc1], %[t4],  %[t0]   \n\t"
            "movn   %[qc2], %[t4],  %[t1]   \n\t"
            "movn   %[qc3], %[t4],  %[t2]   \n\t"
            "movn   %[qc4], %[t4],  %[t3]   \n\t"
1259 1260 1261 1262

            ".set pop                       \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
1263 1264 1265
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4)
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
        );

        curidx  = 8 * qc1;
        curidx += qc2;

        curidx2  = 8 * qc3;
        curidx2 += qc4;

        curbits += p_bits[curidx] +
                   upair7_sign_bits[curidx] +
                   p_bits[curidx2] +
                   upair7_sign_bits[curidx2];
    }
    return curbits;
}

static float get_band_numbits_UPAIR12_mips(struct AACEncContext *s,
                                           PutBitContext *pb, const float *in,
                                           const float *scaled, int size, int scale_idx,
                                           int cb, const float lambda, const float uplim,
                                           int *bits)
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    int i;
    int qc1, qc2, qc3, qc4;
    int curbits = 0;

    uint8_t *p_bits = (uint8_t *)ff_aac_spectral_bits[cb-1];

    for (i = 0; i < size; i += 4) {
        int curidx, curidx2;
1297
        int t0, t1, t2, t3, t4;
1298

1299 1300 1301 1302
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
1303 1304 1305 1306 1307

        __asm__ volatile (
            ".set push                      \n\t"
            ".set noreorder                 \n\t"

1308 1309 1310 1311 1312 1313 1314 1315 1316
            "ori    %[t4],  $zero,  12      \n\t"
            "slt    %[t0],  %[t4],  %[qc1]  \n\t"
            "slt    %[t1],  %[t4],  %[qc2]  \n\t"
            "slt    %[t2],  %[t4],  %[qc3]  \n\t"
            "slt    %[t3],  %[t4],  %[qc4]  \n\t"
            "movn   %[qc1], %[t4],  %[t0]   \n\t"
            "movn   %[qc2], %[t4],  %[t1]   \n\t"
            "movn   %[qc3], %[t4],  %[t2]   \n\t"
            "movn   %[qc4], %[t4],  %[t3]   \n\t"
1317 1318 1319 1320

            ".set pop                       \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
1321 1322 1323
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4)
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356
        );

        curidx  = 13 * qc1;
        curidx += qc2;

        curidx2  = 13 * qc3;
        curidx2 += qc4;

        curbits += p_bits[curidx] +
                   p_bits[curidx2] +
                   upair12_sign_bits[curidx] +
                   upair12_sign_bits[curidx2];
    }
    return curbits;
}

static float get_band_numbits_ESC_mips(struct AACEncContext *s,
                                       PutBitContext *pb, const float *in,
                                       const float *scaled, int size, int scale_idx,
                                       int cb, const float lambda, const float uplim,
                                       int *bits)
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    int i;
    int qc1, qc2, qc3, qc4;
    int curbits = 0;

    uint8_t *p_bits = (uint8_t*)ff_aac_spectral_bits[cb-1];

    for (i = 0; i < size; i += 4) {
        int curidx, curidx2;
        int cond0, cond1, cond2, cond3;
        int c1, c2, c3, c4;
1357
        int t4, t5;
1358

1359 1360 1361 1362
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
1363 1364 1365 1366 1367

        __asm__ volatile (
            ".set push                                  \n\t"
            ".set noreorder                             \n\t"

1368 1369
            "ori        %[t4],      $zero,  15          \n\t"
            "ori        %[t5],      $zero,  16          \n\t"
1370 1371 1372 1373 1374 1375 1376 1377
            "shll_s.w   %[c1],      %[qc1], 18          \n\t"
            "shll_s.w   %[c2],      %[qc2], 18          \n\t"
            "shll_s.w   %[c3],      %[qc3], 18          \n\t"
            "shll_s.w   %[c4],      %[qc4], 18          \n\t"
            "srl        %[c1],      %[c1],  18          \n\t"
            "srl        %[c2],      %[c2],  18          \n\t"
            "srl        %[c3],      %[c3],  18          \n\t"
            "srl        %[c4],      %[c4],  18          \n\t"
1378 1379 1380 1381 1382 1383 1384 1385 1386
            "slt        %[cond0],   %[t4],  %[qc1]      \n\t"
            "slt        %[cond1],   %[t4],  %[qc2]      \n\t"
            "slt        %[cond2],   %[t4],  %[qc3]      \n\t"
            "slt        %[cond3],   %[t4],  %[qc4]      \n\t"
            "movn       %[qc1],     %[t5],  %[cond0]    \n\t"
            "movn       %[qc2],     %[t5],  %[cond1]    \n\t"
            "movn       %[qc3],     %[t5],  %[cond2]    \n\t"
            "movn       %[qc4],     %[t5],  %[cond3]    \n\t"
            "ori        %[t5],      $zero,  31          \n\t"
1387 1388 1389 1390
            "clz        %[c1],      %[c1]               \n\t"
            "clz        %[c2],      %[c2]               \n\t"
            "clz        %[c3],      %[c3]               \n\t"
            "clz        %[c4],      %[c4]               \n\t"
1391 1392 1393 1394
            "subu       %[c1],      %[t5],  %[c1]       \n\t"
            "subu       %[c2],      %[t5],  %[c2]       \n\t"
            "subu       %[c3],      %[t5],  %[c3]       \n\t"
            "subu       %[c4],      %[t5],  %[c4]       \n\t"
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
            "sll        %[c1],      %[c1],  1           \n\t"
            "sll        %[c2],      %[c2],  1           \n\t"
            "sll        %[c3],      %[c3],  1           \n\t"
            "sll        %[c4],      %[c4],  1           \n\t"
            "addiu      %[c1],      %[c1],  -3          \n\t"
            "addiu      %[c2],      %[c2],  -3          \n\t"
            "addiu      %[c3],      %[c3],  -3          \n\t"
            "addiu      %[c4],      %[c4],  -3          \n\t"
            "subu       %[cond0],   $zero,  %[cond0]    \n\t"
            "subu       %[cond1],   $zero,  %[cond1]    \n\t"
            "subu       %[cond2],   $zero,  %[cond2]    \n\t"
            "subu       %[cond3],   $zero,  %[cond3]    \n\t"
            "and        %[c1],      %[c1],  %[cond0]    \n\t"
            "and        %[c2],      %[c2],  %[cond1]    \n\t"
            "and        %[c3],      %[c3],  %[cond2]    \n\t"
            "and        %[c4],      %[c4],  %[cond3]    \n\t"

            ".set pop                                   \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [cond0]"=&r"(cond0), [cond1]"=&r"(cond1),
              [cond2]"=&r"(cond2), [cond3]"=&r"(cond3),
              [c1]"=&r"(c1), [c2]"=&r"(c2),
1419 1420
              [c3]"=&r"(c3), [c4]"=&r"(c4),
              [t4]"=&r"(t4), [t5]"=&r"(t5)
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458
        );

        curidx = 17 * qc1;
        curidx += qc2;

        curidx2 = 17 * qc3;
        curidx2 += qc4;

        curbits += p_bits[curidx];
        curbits += esc_sign_bits[curidx];
        curbits += p_bits[curidx2];
        curbits += esc_sign_bits[curidx2];

        curbits += c1;
        curbits += c2;
        curbits += c3;
        curbits += c4;
    }
    return curbits;
}

static float (*const get_band_numbits_arr[])(struct AACEncContext *s,
                                             PutBitContext *pb, const float *in,
                                             const float *scaled, int size, int scale_idx,
                                             int cb, const float lambda, const float uplim,
                                             int *bits) = {
    get_band_numbits_ZERO_mips,
    get_band_numbits_SQUAD_mips,
    get_band_numbits_SQUAD_mips,
    get_band_numbits_UQUAD_mips,
    get_band_numbits_UQUAD_mips,
    get_band_numbits_SPAIR_mips,
    get_band_numbits_SPAIR_mips,
    get_band_numbits_UPAIR7_mips,
    get_band_numbits_UPAIR7_mips,
    get_band_numbits_UPAIR12_mips,
    get_band_numbits_UPAIR12_mips,
    get_band_numbits_ESC_mips,
1459 1460 1461 1462
    get_band_numbits_NONE_mips, /* cb 12 doesn't exist */
    get_band_numbits_ZERO_mips,
    get_band_numbits_ZERO_mips,
    get_band_numbits_ZERO_mips,
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
};

#define get_band_numbits(                                  \
                                s, pb, in, scaled, size, scale_idx, cb, \
                                lambda, uplim, bits)                    \
    get_band_numbits_arr[cb](                              \
                                s, pb, in, scaled, size, scale_idx, cb, \
                                lambda, uplim, bits)

static float quantize_band_cost_bits(struct AACEncContext *s, const float *in,
                                     const float *scaled, int size, int scale_idx,
                                     int cb, const float lambda, const float uplim,
1475
                                     int *bits, float *energy, int rtz)
1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
{
    return get_band_numbits(s, NULL, in, scaled, size, scale_idx, cb, lambda, uplim, bits);
}

/**
 * Functions developed from template function and optimized for getting the band cost
 */
#if HAVE_MIPSFPU
static float get_band_cost_ZERO_mips(struct AACEncContext *s,
                                     PutBitContext *pb, const float *in,
                                     const float *scaled, int size, int scale_idx,
                                     int cb, const float lambda, const float uplim,
1488
                                     int *bits, float *energy)
1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500
{
    int i;
    float cost = 0;

    for (i = 0; i < size; i += 4) {
        cost += in[i  ] * in[i  ];
        cost += in[i+1] * in[i+1];
        cost += in[i+2] * in[i+2];
        cost += in[i+3] * in[i+3];
    }
    if (bits)
        *bits = 0;
1501 1502
    if (energy)
        *energy = 0.0f;
1503 1504 1505
    return cost * lambda;
}

1506 1507 1508 1509
static float get_band_cost_NONE_mips(struct AACEncContext *s,
                                     PutBitContext *pb, const float *in,
                                     const float *scaled, int size, int scale_idx,
                                     int cb, const float lambda, const float uplim,
1510
                                     int *bits, float *energy)
1511 1512 1513 1514 1515
{
    av_assert0(0);
    return 0;
}

1516 1517 1518 1519
static float get_band_cost_SQUAD_mips(struct AACEncContext *s,
                                      PutBitContext *pb, const float *in,
                                      const float *scaled, int size, int scale_idx,
                                      int cb, const float lambda, const float uplim,
1520
                                      int *bits, float *energy)
1521 1522 1523 1524 1525
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
    int i;
    float cost = 0;
1526
    float qenergy = 0.0f;
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538
    int qc1, qc2, qc3, qc4;
    int curbits = 0;

    uint8_t *p_bits  = (uint8_t *)ff_aac_spectral_bits[cb-1];
    float   *p_codes = (float   *)ff_aac_codebook_vectors[cb-1];

    for (i = 0; i < size; i += 4) {
        const float *vec;
        int curidx;
        int   *in_int = (int   *)&in[i];
        float *in_pos = (float *)&in[i];
        float di0, di1, di2, di3;
1539
        int t0, t1, t2, t3, t4, t5, t6, t7;
1540

1541 1542 1543 1544
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
1545 1546 1547 1548 1549 1550 1551 1552 1553

        __asm__ volatile (
            ".set push                                  \n\t"
            ".set noreorder                             \n\t"

            "slt        %[qc1], $zero,  %[qc1]          \n\t"
            "slt        %[qc2], $zero,  %[qc2]          \n\t"
            "slt        %[qc3], $zero,  %[qc3]          \n\t"
            "slt        %[qc4], $zero,  %[qc4]          \n\t"
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569
            "lw         %[t0],  0(%[in_int])            \n\t"
            "lw         %[t1],  4(%[in_int])            \n\t"
            "lw         %[t2],  8(%[in_int])            \n\t"
            "lw         %[t3],  12(%[in_int])           \n\t"
            "srl        %[t0],  %[t0],  31              \n\t"
            "srl        %[t1],  %[t1],  31              \n\t"
            "srl        %[t2],  %[t2],  31              \n\t"
            "srl        %[t3],  %[t3],  31              \n\t"
            "subu       %[t4],  $zero,  %[qc1]          \n\t"
            "subu       %[t5],  $zero,  %[qc2]          \n\t"
            "subu       %[t6],  $zero,  %[qc3]          \n\t"
            "subu       %[t7],  $zero,  %[qc4]          \n\t"
            "movn       %[qc1], %[t4],  %[t0]           \n\t"
            "movn       %[qc2], %[t5],  %[t1]           \n\t"
            "movn       %[qc3], %[t6],  %[t2]           \n\t"
            "movn       %[qc4], %[t7],  %[t3]           \n\t"
1570 1571 1572 1573

            ".set pop                                   \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
1574 1575 1576
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4), [t5]"=&r"(t5), [t6]"=&r"(t6), [t7]"=&r"(t7)
1577
            : [in_int]"r"(in_int)
1578
            : "memory"
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592
        );

        curidx = qc1;
        curidx *= 3;
        curidx += qc2;
        curidx *= 3;
        curidx += qc3;
        curidx *= 3;
        curidx += qc4;
        curidx += 40;

        curbits += p_bits[curidx];
        vec     = &p_codes[curidx*4];

1593 1594 1595
        qenergy += vec[0]*vec[0] + vec[1]*vec[1]
                +  vec[2]*vec[2] + vec[3]*vec[3];

1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629
        __asm__ volatile (
            ".set push                                  \n\t"
            ".set noreorder                             \n\t"

            "lwc1       $f0,    0(%[in_pos])            \n\t"
            "lwc1       $f1,    0(%[vec])               \n\t"
            "lwc1       $f2,    4(%[in_pos])            \n\t"
            "lwc1       $f3,    4(%[vec])               \n\t"
            "lwc1       $f4,    8(%[in_pos])            \n\t"
            "lwc1       $f5,    8(%[vec])               \n\t"
            "lwc1       $f6,    12(%[in_pos])           \n\t"
            "lwc1       $f7,    12(%[vec])              \n\t"
            "nmsub.s    %[di0], $f0,    $f1,    %[IQ]   \n\t"
            "nmsub.s    %[di1], $f2,    $f3,    %[IQ]   \n\t"
            "nmsub.s    %[di2], $f4,    $f5,    %[IQ]   \n\t"
            "nmsub.s    %[di3], $f6,    $f7,    %[IQ]   \n\t"

            ".set pop                                   \n\t"

            : [di0]"=&f"(di0), [di1]"=&f"(di1),
              [di2]"=&f"(di2), [di3]"=&f"(di3)
            : [in_pos]"r"(in_pos), [vec]"r"(vec),
              [IQ]"f"(IQ)
            : "$f0", "$f1", "$f2", "$f3",
              "$f4", "$f5", "$f6", "$f7",
              "memory"
        );

        cost += di0 * di0 + di1 * di1
                + di2 * di2 + di3 * di3;
    }

    if (bits)
        *bits = curbits;
1630 1631
    if (energy)
        *energy = qenergy * (IQ*IQ);
1632 1633 1634 1635 1636 1637 1638
    return cost * lambda + curbits;
}

static float get_band_cost_UQUAD_mips(struct AACEncContext *s,
                                      PutBitContext *pb, const float *in,
                                      const float *scaled, int size, int scale_idx,
                                      int cb, const float lambda, const float uplim,
1639
                                      int *bits, float *energy)
1640 1641 1642 1643 1644
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
    int i;
    float cost = 0;
1645
    float qenergy = 0.0f;
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
    int curbits = 0;
    int qc1, qc2, qc3, qc4;

    uint8_t *p_bits  = (uint8_t*)ff_aac_spectral_bits[cb-1];
    float   *p_codes = (float  *)ff_aac_codebook_vectors[cb-1];

    for (i = 0; i < size; i += 4) {
        const float *vec;
        int curidx;
        float *in_pos = (float *)&in[i];
        float di0, di1, di2, di3;
1657
        int t0, t1, t2, t3, t4;
1658

1659 1660 1661 1662
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
1663 1664 1665 1666 1667

        __asm__ volatile (
            ".set push                                  \n\t"
            ".set noreorder                             \n\t"

1668 1669 1670 1671 1672 1673 1674 1675 1676
            "ori        %[t4],  $zero,  2               \n\t"
            "slt        %[t0],  %[t4],  %[qc1]          \n\t"
            "slt        %[t1],  %[t4],  %[qc2]          \n\t"
            "slt        %[t2],  %[t4],  %[qc3]          \n\t"
            "slt        %[t3],  %[t4],  %[qc4]          \n\t"
            "movn       %[qc1], %[t4],  %[t0]           \n\t"
            "movn       %[qc2], %[t4],  %[t1]           \n\t"
            "movn       %[qc3], %[t4],  %[t2]           \n\t"
            "movn       %[qc4], %[t4],  %[t3]           \n\t"
1677 1678 1679 1680

            ".set pop                                   \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
1681 1682 1683
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4)
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697
        );

        curidx = qc1;
        curidx *= 3;
        curidx += qc2;
        curidx *= 3;
        curidx += qc3;
        curidx *= 3;
        curidx += qc4;

        curbits += p_bits[curidx];
        curbits += uquad_sign_bits[curidx];
        vec     = &p_codes[curidx*4];

1698 1699 1700
        qenergy += vec[0]*vec[0] + vec[1]*vec[1]
                +  vec[2]*vec[2] + vec[3]*vec[3];

1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737
        __asm__ volatile (
            ".set push                                  \n\t"
            ".set noreorder                             \n\t"

            "lwc1       %[di0], 0(%[in_pos])            \n\t"
            "lwc1       %[di1], 4(%[in_pos])            \n\t"
            "lwc1       %[di2], 8(%[in_pos])            \n\t"
            "lwc1       %[di3], 12(%[in_pos])           \n\t"
            "abs.s      %[di0], %[di0]                  \n\t"
            "abs.s      %[di1], %[di1]                  \n\t"
            "abs.s      %[di2], %[di2]                  \n\t"
            "abs.s      %[di3], %[di3]                  \n\t"
            "lwc1       $f0,    0(%[vec])               \n\t"
            "lwc1       $f1,    4(%[vec])               \n\t"
            "lwc1       $f2,    8(%[vec])               \n\t"
            "lwc1       $f3,    12(%[vec])              \n\t"
            "nmsub.s    %[di0], %[di0], $f0,    %[IQ]   \n\t"
            "nmsub.s    %[di1], %[di1], $f1,    %[IQ]   \n\t"
            "nmsub.s    %[di2], %[di2], $f2,    %[IQ]   \n\t"
            "nmsub.s    %[di3], %[di3], $f3,    %[IQ]   \n\t"

            ".set pop                                   \n\t"

            : [di0]"=&f"(di0), [di1]"=&f"(di1),
              [di2]"=&f"(di2), [di3]"=&f"(di3)
            : [in_pos]"r"(in_pos), [vec]"r"(vec),
              [IQ]"f"(IQ)
            : "$f0", "$f1", "$f2", "$f3",
              "memory"
        );

        cost += di0 * di0 + di1 * di1
                + di2 * di2 + di3 * di3;
    }

    if (bits)
        *bits = curbits;
1738 1739
    if (energy)
        *energy = qenergy * (IQ*IQ);
1740 1741 1742 1743 1744 1745 1746
    return cost * lambda + curbits;
}

static float get_band_cost_SPAIR_mips(struct AACEncContext *s,
                                      PutBitContext *pb, const float *in,
                                      const float *scaled, int size, int scale_idx,
                                      int cb, const float lambda, const float uplim,
1747
                                      int *bits, float *energy)
1748 1749 1750 1751 1752
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
    int i;
    float cost = 0;
1753
    float qenergy = 0.0f;
1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
    int qc1, qc2, qc3, qc4;
    int curbits = 0;

    uint8_t *p_bits  = (uint8_t *)ff_aac_spectral_bits[cb-1];
    float   *p_codes = (float   *)ff_aac_codebook_vectors[cb-1];

    for (i = 0; i < size; i += 4) {
        const float *vec, *vec2;
        int curidx, curidx2;
        int   *in_int = (int   *)&in[i];
        float *in_pos = (float *)&in[i];
        float di0, di1, di2, di3;
1766
        int t0, t1, t2, t3, t4, t5, t6, t7;
1767

1768 1769 1770 1771
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
1772 1773 1774 1775 1776

        __asm__ volatile (
            ".set push                                  \n\t"
            ".set noreorder                             \n\t"

1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
            "ori        %[t4],  $zero,  4               \n\t"
            "slt        %[t0],  %[t4],  %[qc1]          \n\t"
            "slt        %[t1],  %[t4],  %[qc2]          \n\t"
            "slt        %[t2],  %[t4],  %[qc3]          \n\t"
            "slt        %[t3],  %[t4],  %[qc4]          \n\t"
            "movn       %[qc1], %[t4],  %[t0]           \n\t"
            "movn       %[qc2], %[t4],  %[t1]           \n\t"
            "movn       %[qc3], %[t4],  %[t2]           \n\t"
            "movn       %[qc4], %[t4],  %[t3]           \n\t"
            "lw         %[t0],  0(%[in_int])            \n\t"
            "lw         %[t1],  4(%[in_int])            \n\t"
            "lw         %[t2],  8(%[in_int])            \n\t"
            "lw         %[t3],  12(%[in_int])           \n\t"
            "srl        %[t0],  %[t0],  31              \n\t"
            "srl        %[t1],  %[t1],  31              \n\t"
            "srl        %[t2],  %[t2],  31              \n\t"
            "srl        %[t3],  %[t3],  31              \n\t"
            "subu       %[t4],  $zero,  %[qc1]          \n\t"
            "subu       %[t5],  $zero,  %[qc2]          \n\t"
            "subu       %[t6],  $zero,  %[qc3]          \n\t"
            "subu       %[t7],  $zero,  %[qc4]          \n\t"
            "movn       %[qc1], %[t4],  %[t0]           \n\t"
            "movn       %[qc2], %[t5],  %[t1]           \n\t"
            "movn       %[qc3], %[t6],  %[t2]           \n\t"
            "movn       %[qc4], %[t7],  %[t3]           \n\t"
1802 1803 1804 1805

            ".set pop                                   \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
1806 1807 1808
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4), [t5]"=&r"(t5), [t6]"=&r"(t6), [t7]"=&r"(t7)
1809
            : [in_int]"r"(in_int)
1810
            : "memory"
1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824
        );

        curidx = 9 * qc1;
        curidx += qc2 + 40;

        curidx2 = 9 * qc3;
        curidx2 += qc4 + 40;

        curbits += p_bits[curidx];
        curbits += p_bits[curidx2];

        vec     = &p_codes[curidx*2];
        vec2    = &p_codes[curidx2*2];

1825 1826 1827
        qenergy += vec[0]*vec[0] + vec[1]*vec[1]
                +  vec2[0]*vec2[0] + vec2[1]*vec2[1];

1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861
        __asm__ volatile (
            ".set push                                  \n\t"
            ".set noreorder                             \n\t"

            "lwc1       $f0,    0(%[in_pos])            \n\t"
            "lwc1       $f1,    0(%[vec])               \n\t"
            "lwc1       $f2,    4(%[in_pos])            \n\t"
            "lwc1       $f3,    4(%[vec])               \n\t"
            "lwc1       $f4,    8(%[in_pos])            \n\t"
            "lwc1       $f5,    0(%[vec2])              \n\t"
            "lwc1       $f6,    12(%[in_pos])           \n\t"
            "lwc1       $f7,    4(%[vec2])              \n\t"
            "nmsub.s    %[di0], $f0,    $f1,    %[IQ]   \n\t"
            "nmsub.s    %[di1], $f2,    $f3,    %[IQ]   \n\t"
            "nmsub.s    %[di2], $f4,    $f5,    %[IQ]   \n\t"
            "nmsub.s    %[di3], $f6,    $f7,    %[IQ]   \n\t"

            ".set pop                                   \n\t"

            : [di0]"=&f"(di0), [di1]"=&f"(di1),
              [di2]"=&f"(di2), [di3]"=&f"(di3)
            : [in_pos]"r"(in_pos), [vec]"r"(vec),
              [vec2]"r"(vec2), [IQ]"f"(IQ)
            : "$f0", "$f1", "$f2", "$f3",
              "$f4", "$f5", "$f6", "$f7",
              "memory"
        );

        cost += di0 * di0 + di1 * di1
                + di2 * di2 + di3 * di3;
    }

    if (bits)
        *bits = curbits;
1862 1863
    if (energy)
        *energy = qenergy * (IQ*IQ);
1864 1865 1866 1867 1868 1869 1870
    return cost * lambda + curbits;
}

static float get_band_cost_UPAIR7_mips(struct AACEncContext *s,
                                       PutBitContext *pb, const float *in,
                                       const float *scaled, int size, int scale_idx,
                                       int cb, const float lambda, const float uplim,
1871
                                       int *bits, float *energy)
1872 1873 1874 1875 1876
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
    int i;
    float cost = 0;
1877
    float qenergy = 0.0f;
1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889
    int qc1, qc2, qc3, qc4;
    int curbits = 0;

    uint8_t *p_bits  = (uint8_t *)ff_aac_spectral_bits[cb-1];
    float   *p_codes = (float   *)ff_aac_codebook_vectors[cb-1];

    for (i = 0; i < size; i += 4) {
        const float *vec, *vec2;
        int curidx, curidx2, sign1, count1, sign2, count2;
        int   *in_int = (int   *)&in[i];
        float *in_pos = (float *)&in[i];
        float di0, di1, di2, di3;
1890
        int t0, t1, t2, t3, t4;
1891

1892 1893 1894 1895
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
1896 1897 1898 1899 1900

        __asm__ volatile (
            ".set push                                          \n\t"
            ".set noreorder                                     \n\t"

1901
            "ori        %[t4],      $zero,      7               \n\t"
1902 1903
            "ori        %[sign1],   $zero,      0               \n\t"
            "ori        %[sign2],   $zero,      0               \n\t"
1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927
            "slt        %[t0],      %[t4],      %[qc1]          \n\t"
            "slt        %[t1],      %[t4],      %[qc2]          \n\t"
            "slt        %[t2],      %[t4],      %[qc3]          \n\t"
            "slt        %[t3],      %[t4],      %[qc4]          \n\t"
            "movn       %[qc1],     %[t4],      %[t0]           \n\t"
            "movn       %[qc2],     %[t4],      %[t1]           \n\t"
            "movn       %[qc3],     %[t4],      %[t2]           \n\t"
            "movn       %[qc4],     %[t4],      %[t3]           \n\t"
            "lw         %[t0],      0(%[in_int])                \n\t"
            "lw         %[t1],      4(%[in_int])                \n\t"
            "lw         %[t2],      8(%[in_int])                \n\t"
            "lw         %[t3],      12(%[in_int])               \n\t"
            "slt        %[t0],      %[t0],      $zero           \n\t"
            "movn       %[sign1],   %[t0],      %[qc1]          \n\t"
            "slt        %[t2],      %[t2],      $zero           \n\t"
            "movn       %[sign2],   %[t2],      %[qc3]          \n\t"
            "slt        %[t1],      %[t1],      $zero           \n\t"
            "sll        %[t0],      %[sign1],   1               \n\t"
            "or         %[t0],      %[t0],      %[t1]           \n\t"
            "movn       %[sign1],   %[t0],      %[qc2]          \n\t"
            "slt        %[t3],      %[t3],      $zero           \n\t"
            "sll        %[t0],      %[sign2],   1               \n\t"
            "or         %[t0],      %[t0],      %[t3]           \n\t"
            "movn       %[sign2],   %[t0],      %[qc4]          \n\t"
1928
            "slt        %[count1],  $zero,      %[qc1]          \n\t"
1929
            "slt        %[t1],      $zero,      %[qc2]          \n\t"
1930
            "slt        %[count2],  $zero,      %[qc3]          \n\t"
1931 1932 1933
            "slt        %[t2],      $zero,      %[qc4]          \n\t"
            "addu       %[count1],  %[count1],  %[t1]           \n\t"
            "addu       %[count2],  %[count2],  %[t2]           \n\t"
1934 1935 1936 1937 1938 1939

            ".set pop                                           \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [sign1]"=&r"(sign1), [count1]"=&r"(count1),
1940 1941 1942
              [sign2]"=&r"(sign2), [count2]"=&r"(count2),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4)
1943
            : [in_int]"r"(in_int)
1944
            : "memory"
1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
        );

        curidx = 8 * qc1;
        curidx += qc2;

        curidx2 = 8 * qc3;
        curidx2 += qc4;

        curbits += p_bits[curidx];
        curbits += upair7_sign_bits[curidx];
        vec     = &p_codes[curidx*2];

        curbits += p_bits[curidx2];
        curbits += upair7_sign_bits[curidx2];
        vec2    = &p_codes[curidx2*2];

1961 1962 1963
        qenergy += vec[0]*vec[0] + vec[1]*vec[1]
                +  vec2[0]*vec2[0] + vec2[1]*vec2[1];

1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
        __asm__ volatile (
            ".set push                                          \n\t"
            ".set noreorder                                     \n\t"

            "lwc1       %[di0],     0(%[in_pos])                \n\t"
            "lwc1       %[di1],     4(%[in_pos])                \n\t"
            "lwc1       %[di2],     8(%[in_pos])                \n\t"
            "lwc1       %[di3],     12(%[in_pos])               \n\t"
            "abs.s      %[di0],     %[di0]                      \n\t"
            "abs.s      %[di1],     %[di1]                      \n\t"
            "abs.s      %[di2],     %[di2]                      \n\t"
            "abs.s      %[di3],     %[di3]                      \n\t"
            "lwc1       $f0,        0(%[vec])                   \n\t"
            "lwc1       $f1,        4(%[vec])                   \n\t"
            "lwc1       $f2,        0(%[vec2])                  \n\t"
            "lwc1       $f3,        4(%[vec2])                  \n\t"
            "nmsub.s    %[di0],     %[di0],     $f0,    %[IQ]   \n\t"
            "nmsub.s    %[di1],     %[di1],     $f1,    %[IQ]   \n\t"
            "nmsub.s    %[di2],     %[di2],     $f2,    %[IQ]   \n\t"
            "nmsub.s    %[di3],     %[di3],     $f3,    %[IQ]   \n\t"

            ".set pop                                           \n\t"

            : [di0]"=&f"(di0), [di1]"=&f"(di1),
              [di2]"=&f"(di2), [di3]"=&f"(di3)
            : [in_pos]"r"(in_pos), [vec]"r"(vec),
              [vec2]"r"(vec2), [IQ]"f"(IQ)
            : "$f0", "$f1", "$f2", "$f3",
              "memory"
        );

        cost += di0 * di0 + di1 * di1
                + di2 * di2 + di3 * di3;
    }

    if (bits)
        *bits = curbits;
2001 2002
    if (energy)
        *energy = qenergy * (IQ*IQ);
2003 2004 2005 2006 2007 2008 2009
    return cost * lambda + curbits;
}

static float get_band_cost_UPAIR12_mips(struct AACEncContext *s,
                                        PutBitContext *pb, const float *in,
                                        const float *scaled, int size, int scale_idx,
                                        int cb, const float lambda, const float uplim,
2010
                                        int *bits, float *energy)
2011 2012 2013 2014 2015
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
    int i;
    float cost = 0;
2016
    float qenergy = 0.0f;
2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029
    int qc1, qc2, qc3, qc4;
    int curbits = 0;

    uint8_t *p_bits  = (uint8_t *)ff_aac_spectral_bits[cb-1];
    float   *p_codes = (float   *)ff_aac_codebook_vectors[cb-1];

    for (i = 0; i < size; i += 4) {
        const float *vec, *vec2;
        int curidx, curidx2;
        int sign1, count1, sign2, count2;
        int   *in_int = (int   *)&in[i];
        float *in_pos = (float *)&in[i];
        float di0, di1, di2, di3;
2030
        int t0, t1, t2, t3, t4;
2031

2032 2033 2034 2035
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
2036 2037 2038 2039 2040

        __asm__ volatile (
            ".set push                                          \n\t"
            ".set noreorder                                     \n\t"

2041
            "ori        %[t4],      $zero,      12              \n\t"
2042 2043
            "ori        %[sign1],   $zero,      0               \n\t"
            "ori        %[sign2],   $zero,      0               \n\t"
2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
            "slt        %[t0],      %[t4],      %[qc1]          \n\t"
            "slt        %[t1],      %[t4],      %[qc2]          \n\t"
            "slt        %[t2],      %[t4],      %[qc3]          \n\t"
            "slt        %[t3],      %[t4],      %[qc4]          \n\t"
            "movn       %[qc1],     %[t4],      %[t0]           \n\t"
            "movn       %[qc2],     %[t4],      %[t1]           \n\t"
            "movn       %[qc3],     %[t4],      %[t2]           \n\t"
            "movn       %[qc4],     %[t4],      %[t3]           \n\t"
            "lw         %[t0],      0(%[in_int])                \n\t"
            "lw         %[t1],      4(%[in_int])                \n\t"
            "lw         %[t2],      8(%[in_int])                \n\t"
            "lw         %[t3],      12(%[in_int])               \n\t"
            "slt        %[t0],      %[t0],      $zero           \n\t"
            "movn       %[sign1],   %[t0],      %[qc1]          \n\t"
            "slt        %[t2],      %[t2],      $zero           \n\t"
            "movn       %[sign2],   %[t2],      %[qc3]          \n\t"
            "slt        %[t1],      %[t1],      $zero           \n\t"
            "sll        %[t0],      %[sign1],   1               \n\t"
            "or         %[t0],      %[t0],      %[t1]           \n\t"
            "movn       %[sign1],   %[t0],      %[qc2]          \n\t"
            "slt        %[t3],      %[t3],      $zero           \n\t"
            "sll        %[t0],      %[sign2],   1               \n\t"
            "or         %[t0],      %[t0],      %[t3]           \n\t"
            "movn       %[sign2],   %[t0],      %[qc4]          \n\t"
2068
            "slt        %[count1],  $zero,      %[qc1]          \n\t"
2069
            "slt        %[t1],      $zero,      %[qc2]          \n\t"
2070
            "slt        %[count2],  $zero,      %[qc3]          \n\t"
2071 2072 2073
            "slt        %[t2],      $zero,      %[qc4]          \n\t"
            "addu       %[count1],  %[count1],  %[t1]           \n\t"
            "addu       %[count2],  %[count2],  %[t2]           \n\t"
2074 2075 2076 2077 2078 2079

            ".set pop                                           \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [sign1]"=&r"(sign1), [count1]"=&r"(count1),
2080 2081 2082
              [sign2]"=&r"(sign2), [count2]"=&r"(count2),
              [t0]"=&r"(t0), [t1]"=&r"(t1), [t2]"=&r"(t2), [t3]"=&r"(t3),
              [t4]"=&r"(t4)
2083
            : [in_int]"r"(in_int)
2084
            : "memory"
2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099
        );

        curidx = 13 * qc1;
        curidx += qc2;

        curidx2 = 13 * qc3;
        curidx2 += qc4;

        curbits += p_bits[curidx];
        curbits += p_bits[curidx2];
        curbits += upair12_sign_bits[curidx];
        curbits += upair12_sign_bits[curidx2];
        vec     = &p_codes[curidx*2];
        vec2    = &p_codes[curidx2*2];

2100 2101 2102
        qenergy += vec[0]*vec[0] + vec[1]*vec[1]
                +  vec2[0]*vec2[0] + vec2[1]*vec2[1];

2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139
        __asm__ volatile (
            ".set push                                          \n\t"
            ".set noreorder                                     \n\t"

            "lwc1       %[di0],     0(%[in_pos])                \n\t"
            "lwc1       %[di1],     4(%[in_pos])                \n\t"
            "lwc1       %[di2],     8(%[in_pos])                \n\t"
            "lwc1       %[di3],     12(%[in_pos])               \n\t"
            "abs.s      %[di0],     %[di0]                      \n\t"
            "abs.s      %[di1],     %[di1]                      \n\t"
            "abs.s      %[di2],     %[di2]                      \n\t"
            "abs.s      %[di3],     %[di3]                      \n\t"
            "lwc1       $f0,        0(%[vec])                   \n\t"
            "lwc1       $f1,        4(%[vec])                   \n\t"
            "lwc1       $f2,        0(%[vec2])                  \n\t"
            "lwc1       $f3,        4(%[vec2])                  \n\t"
            "nmsub.s    %[di0],     %[di0],     $f0,    %[IQ]   \n\t"
            "nmsub.s    %[di1],     %[di1],     $f1,    %[IQ]   \n\t"
            "nmsub.s    %[di2],     %[di2],     $f2,    %[IQ]   \n\t"
            "nmsub.s    %[di3],     %[di3],     $f3,    %[IQ]   \n\t"

            ".set pop                                           \n\t"

            : [di0]"=&f"(di0), [di1]"=&f"(di1),
              [di2]"=&f"(di2), [di3]"=&f"(di3)
            : [in_pos]"r"(in_pos), [vec]"r"(vec),
              [vec2]"r"(vec2), [IQ]"f"(IQ)
            : "$f0", "$f1", "$f2", "$f3",
              "memory"
        );

        cost += di0 * di0 + di1 * di1
                + di2 * di2 + di3 * di3;
    }

    if (bits)
        *bits = curbits;
2140 2141
    if (energy)
        *energy = qenergy * (IQ*IQ);
2142 2143 2144 2145 2146 2147 2148
    return cost * lambda + curbits;
}

static float get_band_cost_ESC_mips(struct AACEncContext *s,
                                    PutBitContext *pb, const float *in,
                                    const float *scaled, int size, int scale_idx,
                                    int cb, const float lambda, const float uplim,
2149
                                    int *bits, float *energy)
2150 2151 2152 2153 2154 2155
{
    const float Q34 = ff_aac_pow34sf_tab[POW_SF2_ZERO - scale_idx + SCALE_ONE_POS - SCALE_DIV_512];
    const float IQ  = ff_aac_pow2sf_tab [POW_SF2_ZERO + scale_idx - SCALE_ONE_POS + SCALE_DIV_512];
    const float CLIPPED_ESCAPE = 165140.0f * IQ;
    int i;
    float cost = 0;
2156
    float qenergy = 0.0f;
2157 2158 2159 2160 2161 2162 2163 2164 2165
    int qc1, qc2, qc3, qc4;
    int curbits = 0;

    uint8_t *p_bits  = (uint8_t*)ff_aac_spectral_bits[cb-1];
    float   *p_codes = (float*  )ff_aac_codebook_vectors[cb-1];

    for (i = 0; i < size; i += 4) {
        const float *vec, *vec2;
        int curidx, curidx2;
2166
        float t1, t2, t3, t4, V;
2167 2168 2169
        float di1, di2, di3, di4;
        int cond0, cond1, cond2, cond3;
        int c1, c2, c3, c4;
2170
        int t6, t7;
2171

2172 2173 2174 2175
        qc1 = scaled[i  ] * Q34 + ROUND_STANDARD;
        qc2 = scaled[i+1] * Q34 + ROUND_STANDARD;
        qc3 = scaled[i+2] * Q34 + ROUND_STANDARD;
        qc4 = scaled[i+3] * Q34 + ROUND_STANDARD;
2176 2177 2178 2179 2180

        __asm__ volatile (
            ".set push                                  \n\t"
            ".set noreorder                             \n\t"

2181 2182
            "ori        %[t6],      $zero,  15          \n\t"
            "ori        %[t7],      $zero,  16          \n\t"
2183 2184 2185 2186 2187 2188 2189 2190
            "shll_s.w   %[c1],      %[qc1], 18          \n\t"
            "shll_s.w   %[c2],      %[qc2], 18          \n\t"
            "shll_s.w   %[c3],      %[qc3], 18          \n\t"
            "shll_s.w   %[c4],      %[qc4], 18          \n\t"
            "srl        %[c1],      %[c1],  18          \n\t"
            "srl        %[c2],      %[c2],  18          \n\t"
            "srl        %[c3],      %[c3],  18          \n\t"
            "srl        %[c4],      %[c4],  18          \n\t"
2191 2192 2193 2194 2195 2196 2197 2198
            "slt        %[cond0],   %[t6],  %[qc1]      \n\t"
            "slt        %[cond1],   %[t6],  %[qc2]      \n\t"
            "slt        %[cond2],   %[t6],  %[qc3]      \n\t"
            "slt        %[cond3],   %[t6],  %[qc4]      \n\t"
            "movn       %[qc1],     %[t7],  %[cond0]    \n\t"
            "movn       %[qc2],     %[t7],  %[cond1]    \n\t"
            "movn       %[qc3],     %[t7],  %[cond2]    \n\t"
            "movn       %[qc4],     %[t7],  %[cond3]    \n\t"
2199 2200 2201 2202 2203 2204 2205 2206

            ".set pop                                   \n\t"

            : [qc1]"+r"(qc1), [qc2]"+r"(qc2),
              [qc3]"+r"(qc3), [qc4]"+r"(qc4),
              [cond0]"=&r"(cond0), [cond1]"=&r"(cond1),
              [cond2]"=&r"(cond2), [cond3]"=&r"(cond3),
              [c1]"=&r"(c1), [c2]"=&r"(c2),
2207 2208
              [c3]"=&r"(c3), [c4]"=&r"(c4),
              [t6]"=&r"(t6), [t7]"=&r"(t7)
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237
        );

        curidx = 17 * qc1;
        curidx += qc2;

        curidx2 = 17 * qc3;
        curidx2 += qc4;

        curbits += p_bits[curidx];
        curbits += esc_sign_bits[curidx];
        vec     = &p_codes[curidx*2];

        curbits += p_bits[curidx2];
        curbits += esc_sign_bits[curidx2];
        vec2     = &p_codes[curidx2*2];

        curbits += (av_log2(c1) * 2 - 3) & (-cond0);
        curbits += (av_log2(c2) * 2 - 3) & (-cond1);
        curbits += (av_log2(c3) * 2 - 3) & (-cond2);
        curbits += (av_log2(c4) * 2 - 3) & (-cond3);

        t1 = fabsf(in[i  ]);
        t2 = fabsf(in[i+1]);
        t3 = fabsf(in[i+2]);
        t4 = fabsf(in[i+3]);

        if (cond0) {
            if (t1 >= CLIPPED_ESCAPE) {
                di1 = t1 - CLIPPED_ESCAPE;
2238
                qenergy += CLIPPED_ESCAPE*CLIPPED_ESCAPE;
2239
            } else {
2240 2241
                di1 = t1 - (V = c1 * cbrtf(c1) * IQ);
                qenergy += V*V;
2242
            }
2243 2244 2245 2246
        } else {
            di1 = t1 - (V = vec[0] * IQ);
            qenergy += V*V;
        }
2247 2248 2249 2250

        if (cond1) {
            if (t2 >= CLIPPED_ESCAPE) {
                di2 = t2 - CLIPPED_ESCAPE;
2251
                qenergy += CLIPPED_ESCAPE*CLIPPED_ESCAPE;
2252
            } else {
2253 2254
                di2 = t2 - (V = c2 * cbrtf(c2) * IQ);
                qenergy += V*V;
2255
            }
2256 2257 2258 2259
        } else {
            di2 = t2 - (V = vec[1] * IQ);
            qenergy += V*V;
        }
2260 2261 2262 2263

        if (cond2) {
            if (t3 >= CLIPPED_ESCAPE) {
                di3 = t3 - CLIPPED_ESCAPE;
2264
                qenergy += CLIPPED_ESCAPE*CLIPPED_ESCAPE;
2265
            } else {
2266 2267
                di3 = t3 - (V = c3 * cbrtf(c3) * IQ);
                qenergy += V*V;
2268
            }
2269 2270 2271 2272
        } else {
            di3 = t3 - (V = vec2[0] * IQ);
            qenergy += V*V;
        }
2273 2274 2275 2276

        if (cond3) {
            if (t4 >= CLIPPED_ESCAPE) {
                di4 = t4 - CLIPPED_ESCAPE;
2277
                qenergy += CLIPPED_ESCAPE*CLIPPED_ESCAPE;
2278
            } else {
2279 2280
                di4 = t4 - (V = c4 * cbrtf(c4) * IQ);
                qenergy += V*V;
2281
            }
2282 2283 2284 2285
        } else {
            di4 = t4 - (V = vec2[1]*IQ);
            qenergy += V*V;
        }
2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299

        cost += di1 * di1 + di2 * di2
                + di3 * di3 + di4 * di4;
    }

    if (bits)
        *bits = curbits;
    return cost * lambda + curbits;
}

static float (*const get_band_cost_arr[])(struct AACEncContext *s,
                                          PutBitContext *pb, const float *in,
                                          const float *scaled, int size, int scale_idx,
                                          int cb, const float lambda, const float uplim,
2300
                                          int *bits, float *energy) = {
2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
    get_band_cost_ZERO_mips,
    get_band_cost_SQUAD_mips,
    get_band_cost_SQUAD_mips,
    get_band_cost_UQUAD_mips,
    get_band_cost_UQUAD_mips,
    get_band_cost_SPAIR_mips,
    get_band_cost_SPAIR_mips,
    get_band_cost_UPAIR7_mips,
    get_band_cost_UPAIR7_mips,
    get_band_cost_UPAIR12_mips,
    get_band_cost_UPAIR12_mips,
    get_band_cost_ESC_mips,
2313 2314 2315 2316
    get_band_cost_NONE_mips, /* cb 12 doesn't exist */
    get_band_cost_ZERO_mips,
    get_band_cost_ZERO_mips,
    get_band_cost_ZERO_mips,
2317 2318 2319 2320
};

#define get_band_cost(                                  \
                                s, pb, in, scaled, size, scale_idx, cb, \
2321
                                lambda, uplim, bits, energy)            \
2322 2323
    get_band_cost_arr[cb](                              \
                                s, pb, in, scaled, size, scale_idx, cb, \
2324
                                lambda, uplim, bits, energy)
2325 2326 2327 2328

static float quantize_band_cost(struct AACEncContext *s, const float *in,
                                const float *scaled, int size, int scale_idx,
                                int cb, const float lambda, const float uplim,
2329
                                int *bits, float *energy, int rtz)
2330
{
2331 2332 2333
    return get_band_cost(s, NULL, in, scaled, size, scale_idx, cb, lambda, uplim, bits, energy);
}

2334 2335
#include "libavcodec/aacenc_quantization_misc.h"

2336
#include "libavcodec/aaccoder_twoloop.h"
2337

2338
static void search_for_ms_mips(AACEncContext *s, ChannelElement *cpe)
2339
{
2340 2341
    int start = 0, i, w, w2, g, sid_sf_boost, prev_mid, prev_side;
    uint8_t nextband0[128], nextband1[128];
2342 2343
    float M[128], S[128];
    float *L34 = s->scoefs, *R34 = s->scoefs + 128, *M34 = s->scoefs + 128*2, *S34 = s->scoefs + 128*3;
2344
    const float lambda = s->lambda;
2345
    const float mslambda = FFMIN(1.0f, lambda / 120.f);
2346 2347 2348 2349
    SingleChannelElement *sce0 = &cpe->ch[0];
    SingleChannelElement *sce1 = &cpe->ch[1];
    if (!cpe->common_window)
        return;
2350 2351 2352 2353 2354 2355 2356

    /** Scout out next nonzero bands */
    ff_init_nextband_map(sce0, nextband0);
    ff_init_nextband_map(sce1, nextband1);

    prev_mid = sce0->sf_idx[0];
    prev_side = sce1->sf_idx[0];
2357
    for (w = 0; w < sce0->ics.num_windows; w += sce0->ics.group_len[w]) {
2358
        start = 0;
2359
        for (g = 0;  g < sce0->ics.num_swb; g++) {
2360
            float bmax = bval2bmax(g * 17.0f / sce0->ics.num_swb) / 0.0045f;
2361 2362 2363
            if (!cpe->is_mask[w*16+g])
                cpe->ms_mask[w*16+g] = 0;
            if (!sce0->zeroes[w*16+g] && !sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g]) {
2364 2365 2366
                float Mmax = 0.0f, Smax = 0.0f;

                /* Must compute mid/side SF and book for the whole window group */
2367
                for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391
                    for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
                        M[i] = (sce0->coeffs[start+(w+w2)*128+i]
                              + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
                        S[i] =  M[i]
                              - sce1->coeffs[start+(w+w2)*128+i];
                    }
                    abs_pow34_v(M34, M, sce0->ics.swb_sizes[g]);
                    abs_pow34_v(S34, S, sce0->ics.swb_sizes[g]);
                    for (i = 0; i < sce0->ics.swb_sizes[g]; i++ ) {
                        Mmax = FFMAX(Mmax, M34[i]);
                        Smax = FFMAX(Smax, S34[i]);
                    }
                }

                for (sid_sf_boost = 0; sid_sf_boost < 4; sid_sf_boost++) {
                    float dist1 = 0.0f, dist2 = 0.0f;
                    int B0 = 0, B1 = 0;
                    int minidx;
                    int mididx, sididx;
                    int midcb, sidcb;

                    minidx = FFMIN(sce0->sf_idx[w*16+g], sce1->sf_idx[w*16+g]);
                    mididx = av_clip(minidx, 0, SCALE_MAX_POS - SCALE_DIV_512);
                    sididx = av_clip(minidx - sid_sf_boost * 3, 0, SCALE_MAX_POS - SCALE_DIV_512);
2392
                    if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT
2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424
                        && (   !ff_sfdelta_can_replace(sce0, nextband0, prev_mid, mididx, w*16+g)
                            || !ff_sfdelta_can_replace(sce1, nextband1, prev_side, sididx, w*16+g))) {
                        /* scalefactor range violation, bad stuff, will decrease quality unacceptably */
                        continue;
                    }

                    midcb = find_min_book(Mmax, mididx);
                    sidcb = find_min_book(Smax, sididx);

                    /* No CB can be zero */
                    midcb = FFMAX(1,midcb);
                    sidcb = FFMAX(1,sidcb);

                    for (w2 = 0; w2 < sce0->ics.group_len[w]; w2++) {
                        FFPsyBand *band0 = &s->psy.ch[s->cur_channel+0].psy_bands[(w+w2)*16+g];
                        FFPsyBand *band1 = &s->psy.ch[s->cur_channel+1].psy_bands[(w+w2)*16+g];
                        float minthr = FFMIN(band0->threshold, band1->threshold);
                        int b1,b2,b3,b4;
                        for (i = 0; i < sce0->ics.swb_sizes[g]; i++) {
                            M[i] = (sce0->coeffs[start+(w+w2)*128+i]
                                  + sce1->coeffs[start+(w+w2)*128+i]) * 0.5;
                            S[i] =  M[i]
                                  - sce1->coeffs[start+(w+w2)*128+i];
                        }

                        abs_pow34_v(L34, sce0->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
                        abs_pow34_v(R34, sce1->coeffs+start+(w+w2)*128, sce0->ics.swb_sizes[g]);
                        abs_pow34_v(M34, M,                         sce0->ics.swb_sizes[g]);
                        abs_pow34_v(S34, S,                         sce0->ics.swb_sizes[g]);
                        dist1 += quantize_band_cost(s, &sce0->coeffs[start + (w+w2)*128],
                                                    L34,
                                                    sce0->ics.swb_sizes[g],
2425 2426
                                                    sce0->sf_idx[w*16+g],
                                                    sce0->band_type[w*16+g],
2427 2428 2429 2430
                                                    lambda / band0->threshold, INFINITY, &b1, NULL, 0);
                        dist1 += quantize_band_cost(s, &sce1->coeffs[start + (w+w2)*128],
                                                    R34,
                                                    sce1->ics.swb_sizes[g],
2431 2432
                                                    sce1->sf_idx[w*16+g],
                                                    sce1->band_type[w*16+g],
2433 2434 2435 2436
                                                    lambda / band1->threshold, INFINITY, &b2, NULL, 0);
                        dist2 += quantize_band_cost(s, M,
                                                    M34,
                                                    sce0->ics.swb_sizes[g],
2437 2438
                                                    mididx,
                                                    midcb,
2439 2440 2441 2442
                                                    lambda / minthr, INFINITY, &b3, NULL, 0);
                        dist2 += quantize_band_cost(s, S,
                                                    S34,
                                                    sce1->ics.swb_sizes[g],
2443 2444
                                                    sididx,
                                                    sidcb,
2445 2446 2447
                                                    mslambda / (minthr * bmax), INFINITY, &b4, NULL, 0);
                        B0 += b1+b2;
                        B1 += b3+b4;
2448 2449
                        dist1 -= b1+b2;
                        dist2 -= b3+b4;
2450 2451 2452
                    }
                    cpe->ms_mask[w*16+g] = dist2 <= dist1 && B1 < B0;
                    if (cpe->ms_mask[w*16+g]) {
2453
                        if (sce0->band_type[w*16+g] != NOISE_BT && sce1->band_type[w*16+g] != NOISE_BT) {
2454 2455 2456 2457
                            sce0->sf_idx[w*16+g] = mididx;
                            sce1->sf_idx[w*16+g] = sididx;
                            sce0->band_type[w*16+g] = midcb;
                            sce1->band_type[w*16+g] = sidcb;
2458 2459 2460
                        } else if ((sce0->band_type[w*16+g] != NOISE_BT) ^ (sce1->band_type[w*16+g] != NOISE_BT)) {
                            /* ms_mask unneeded, and it confuses some decoders */
                            cpe->ms_mask[w*16+g] = 0;
2461 2462 2463 2464 2465 2466
                        }
                        break;
                    } else if (B1 > B0) {
                        /* More boost won't fix this */
                        break;
                    }
2467 2468
                }
            }
2469 2470 2471 2472
            if (!sce0->zeroes[w*16+g] && sce0->band_type[w*16+g] < RESERVED_BT)
                prev_mid = sce0->sf_idx[w*16+g];
            if (!sce1->zeroes[w*16+g] && !cpe->is_mask[w*16+g] && sce1->band_type[w*16+g] < RESERVED_BT)
                prev_side = sce1->sf_idx[w*16+g];
2473 2474 2475 2476 2477 2478
            start += sce0->ics.swb_sizes[g];
        }
    }
}
#endif /*HAVE_MIPSFPU */

2479
#include "libavcodec/aaccoder_trellis.h"
2480

2481
#endif /* !HAVE_MIPS32R6 && !HAVE_MIPS64R6 */
2482 2483 2484
#endif /* HAVE_INLINE_ASM */

void ff_aac_coder_init_mips(AACEncContext *c) {
2485
#if HAVE_INLINE_ASM
2486
#if !HAVE_MIPS32R6 && !HAVE_MIPS64R6
2487
    AACCoefficientsEncoder *e = c->coder;
2488
    int option = c->options.coder;
2489 2490

    if (option == 2) {
2491
        e->quantize_and_encode_band = quantize_and_encode_band_mips;
2492
        e->encode_window_bands_info = codebook_trellis_rate;
2493
#if HAVE_MIPSFPU
2494
        e->search_for_quantizers    = search_for_quantizers_twoloop;
2495 2496
#endif /* HAVE_MIPSFPU */
    }
2497 2498 2499
#if HAVE_MIPSFPU
    e->search_for_ms            = search_for_ms_mips;
#endif /* HAVE_MIPSFPU */
2500
#endif /* !HAVE_MIPS32R6 && !HAVE_MIPS64R6 */
2501 2502
#endif /* HAVE_INLINE_ASM */
}