LCOV - code coverage report
Current view: top level - src/libavcodec - aacps.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 468 628 74.5 %
Date: 2017-01-24 04:42:20 Functions: 23 28 82.1 %

          Line data    Source code
       1             : /*
       2             :  * MPEG-4 Parametric Stereo decoding functions
       3             :  * Copyright (c) 2010 Alex Converse <alex.converse@gmail.com>
       4             :  *
       5             :  * This file is part of FFmpeg.
       6             :  *
       7             :  * FFmpeg is free software; you can redistribute it and/or
       8             :  * modify it under the terms of the GNU Lesser General Public
       9             :  * License as published by the Free Software Foundation; either
      10             :  * version 2.1 of the License, or (at your option) any later version.
      11             :  *
      12             :  * FFmpeg is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15             :  * Lesser General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU Lesser General Public
      18             :  * License along with FFmpeg; if not, write to the Free Software
      19             :  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
      20             :  *
      21             :  * Note: Rounding-to-nearest used unless otherwise stated
      22             :  *
      23             :  */
      24             : 
      25             : #include <stdint.h>
      26             : #include "libavutil/common.h"
      27             : #include "libavutil/mathematics.h"
      28             : #include "avcodec.h"
      29             : #include "get_bits.h"
      30             : #include "aacps.h"
      31             : #if USE_FIXED
      32             : #include "aacps_fixed_tablegen.h"
      33             : #else
      34             : #include "libavutil/internal.h"
      35             : #include "aacps_tablegen.h"
      36             : #endif /* USE_FIXED */
      37             : #include "aacpsdata.c"
      38             : 
      39             : #define PS_BASELINE 0  ///< Operate in Baseline PS mode
      40             :                        ///< Baseline implies 10 or 20 stereo bands,
      41             :                        ///< mixing mode A, and no ipd/opd
      42             : 
      43             : #define numQMFSlots 32 //numTimeSlots * RATE
      44             : 
      45             : static const int8_t num_env_tab[2][4] = {
      46             :     { 0, 1, 2, 4, },
      47             :     { 1, 2, 3, 4, },
      48             : };
      49             : 
      50             : static const int8_t nr_iidicc_par_tab[] = {
      51             :     10, 20, 34, 10, 20, 34,
      52             : };
      53             : 
      54             : static const int8_t nr_iidopd_par_tab[] = {
      55             :      5, 11, 17,  5, 11, 17,
      56             : };
      57             : 
      58             : enum {
      59             :     huff_iid_df1,
      60             :     huff_iid_dt1,
      61             :     huff_iid_df0,
      62             :     huff_iid_dt0,
      63             :     huff_icc_df,
      64             :     huff_icc_dt,
      65             :     huff_ipd_df,
      66             :     huff_ipd_dt,
      67             :     huff_opd_df,
      68             :     huff_opd_dt,
      69             : };
      70             : 
      71             : static const int huff_iid[] = {
      72             :     huff_iid_df0,
      73             :     huff_iid_df1,
      74             :     huff_iid_dt0,
      75             :     huff_iid_dt1,
      76             : };
      77             : 
      78             : static VLC vlc_ps[10];
      79             : 
      80             : #define READ_PAR_DATA(PAR, OFFSET, MASK, ERR_CONDITION) \
      81             : /** \
      82             :  * Read Inter-channel Intensity Difference/Inter-Channel Coherence/ \
      83             :  * Inter-channel Phase Difference/Overall Phase Difference parameters from the \
      84             :  * bitstream. \
      85             :  * \
      86             :  * @param avctx contains the current codec context \
      87             :  * @param gb    pointer to the input bitstream \
      88             :  * @param ps    pointer to the Parametric Stereo context \
      89             :  * @param PAR   pointer to the parameter to be read \
      90             :  * @param e     envelope to decode \
      91             :  * @param dt    1: time delta-coded, 0: frequency delta-coded \
      92             :  */ \
      93             : static int read_ ## PAR ## _data(AVCodecContext *avctx, GetBitContext *gb, PSContext *ps, \
      94             :                         int8_t (*PAR)[PS_MAX_NR_IIDICC], int table_idx, int e, int dt) \
      95             : { \
      96             :     int b, num = ps->nr_ ## PAR ## _par; \
      97             :     VLC_TYPE (*vlc_table)[2] = vlc_ps[table_idx].table; \
      98             :     if (dt) { \
      99             :         int e_prev = e ? e - 1 : ps->num_env_old - 1; \
     100             :         e_prev = FFMAX(e_prev, 0); \
     101             :         for (b = 0; b < num; b++) { \
     102             :             int val = PAR[e_prev][b] + get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
     103             :             if (MASK) val &= MASK; \
     104             :             PAR[e][b] = val; \
     105             :             if (ERR_CONDITION) \
     106             :                 goto err; \
     107             :         } \
     108             :     } else { \
     109             :         int val = 0; \
     110             :         for (b = 0; b < num; b++) { \
     111             :             val += get_vlc2(gb, vlc_table, 9, 3) - OFFSET; \
     112             :             if (MASK) val &= MASK; \
     113             :             PAR[e][b] = val; \
     114             :             if (ERR_CONDITION) \
     115             :                 goto err; \
     116             :         } \
     117             :     } \
     118             :     return 0; \
     119             : err: \
     120             :     av_log(avctx, AV_LOG_ERROR, "illegal "#PAR"\n"); \
     121             :     return -1; \
     122             : }
     123             : 
     124        1702 : READ_PAR_DATA(iid,    huff_offset[table_idx],    0, FFABS(ps->iid_par[e][b]) > 7 + 8 * ps->iid_quant)
     125        1806 : READ_PAR_DATA(icc,    huff_offset[table_idx],    0, ps->icc_par[e][b] > 7U)
     126           0 : READ_PAR_DATA(ipdopd,                      0, 0x07, 0)
     127             : 
     128           0 : static int ps_read_extension_data(GetBitContext *gb, PSContext *ps, int ps_extension_id)
     129             : {
     130             :     int e;
     131           0 :     int count = get_bits_count(gb);
     132             : 
     133           0 :     if (ps_extension_id)
     134           0 :         return 0;
     135             : 
     136           0 :     ps->enable_ipdopd = get_bits1(gb);
     137           0 :     if (ps->enable_ipdopd) {
     138           0 :         for (e = 0; e < ps->num_env; e++) {
     139           0 :             int dt = get_bits1(gb);
     140           0 :             read_ipdopd_data(NULL, gb, ps, ps->ipd_par, dt ? huff_ipd_dt : huff_ipd_df, e, dt);
     141           0 :             dt = get_bits1(gb);
     142           0 :             read_ipdopd_data(NULL, gb, ps, ps->opd_par, dt ? huff_opd_dt : huff_opd_df, e, dt);
     143             :         }
     144             :     }
     145           0 :     skip_bits1(gb);      //reserved_ps
     146           0 :     return get_bits_count(gb) - count;
     147             : }
     148             : 
     149           3 : static void ipdopd_reset(int8_t *ipd_hist, int8_t *opd_hist)
     150             : {
     151             :     int i;
     152          54 :     for (i = 0; i < PS_MAX_NR_IPDOPD; i++) {
     153          51 :         opd_hist[i] = 0;
     154          51 :         ipd_hist[i] = 0;
     155             :     }
     156           3 : }
     157             : 
     158        1563 : int AAC_RENAME(ff_ps_read_data)(AVCodecContext *avctx, GetBitContext *gb_host, PSContext *ps, int bits_left)
     159             : {
     160             :     int e;
     161        1563 :     int bit_count_start = get_bits_count(gb_host);
     162             :     int header;
     163             :     int bits_consumed;
     164        1563 :     GetBitContext gbc = *gb_host, *gb = &gbc;
     165             : 
     166        1563 :     header = get_bits1(gb);
     167        1563 :     if (header) {     //enable_ps_header
     168         303 :         ps->enable_iid = get_bits1(gb);
     169         303 :         if (ps->enable_iid) {
     170         171 :             int iid_mode = get_bits(gb, 3);
     171         171 :             if (iid_mode > 5) {
     172           0 :                 av_log(avctx, AV_LOG_ERROR, "iid_mode %d is reserved.\n",
     173             :                        iid_mode);
     174           0 :                 goto err;
     175             :             }
     176         171 :             ps->nr_iid_par    = nr_iidicc_par_tab[iid_mode];
     177         171 :             ps->iid_quant     = iid_mode > 2;
     178         171 :             ps->nr_ipdopd_par = nr_iidopd_par_tab[iid_mode];
     179             :         }
     180         303 :         ps->enable_icc = get_bits1(gb);
     181         303 :         if (ps->enable_icc) {
     182         262 :             ps->icc_mode = get_bits(gb, 3);
     183         262 :             if (ps->icc_mode > 5) {
     184           0 :                 av_log(avctx, AV_LOG_ERROR, "icc_mode %d is reserved.\n",
     185             :                        ps->icc_mode);
     186           0 :                 goto err;
     187             :             }
     188         262 :             ps->nr_icc_par = nr_iidicc_par_tab[ps->icc_mode];
     189             :         }
     190         303 :         ps->enable_ext = get_bits1(gb);
     191             :     }
     192             : 
     193        1563 :     ps->frame_class = get_bits1(gb);
     194        1563 :     ps->num_env_old = ps->num_env;
     195        1563 :     ps->num_env     = num_env_tab[ps->frame_class][get_bits(gb, 2)];
     196             : 
     197        1563 :     ps->border_position[0] = -1;
     198        1563 :     if (ps->frame_class) {
     199         787 :         for (e = 1; e <= ps->num_env; e++)
     200         569 :             ps->border_position[e] = get_bits(gb, 5);
     201             :     } else
     202        2625 :         for (e = 1; e <= ps->num_env; e++)
     203        1280 :             ps->border_position[e] = (e * numQMFSlots >> ff_log2_tab[ps->num_env]) - 1;
     204             : 
     205        1563 :     if (ps->enable_iid) {
     206        3085 :         for (e = 0; e < ps->num_env; e++) {
     207        1702 :             int dt = get_bits1(gb);
     208        1702 :             if (read_iid_data(avctx, gb, ps, ps->iid_par, huff_iid[2*dt+ps->iid_quant], e, dt))
     209           0 :                 goto err;
     210             :         }
     211             :     } else
     212         180 :         memset(ps->iid_par, 0, sizeof(ps->iid_par));
     213             : 
     214        1563 :     if (ps->enable_icc)
     215        3300 :         for (e = 0; e < ps->num_env; e++) {
     216        1806 :             int dt = get_bits1(gb);
     217        1806 :             if (read_icc_data(avctx, gb, ps, ps->icc_par, dt ? huff_icc_dt : huff_icc_df, e, dt))
     218           0 :                 goto err;
     219             :         }
     220             :     else
     221          69 :         memset(ps->icc_par, 0, sizeof(ps->icc_par));
     222             : 
     223        1563 :     if (ps->enable_ext) {
     224           0 :         int cnt = get_bits(gb, 4);
     225           0 :         if (cnt == 15) {
     226           0 :             cnt += get_bits(gb, 8);
     227             :         }
     228           0 :         cnt *= 8;
     229           0 :         while (cnt > 7) {
     230           0 :             int ps_extension_id = get_bits(gb, 2);
     231           0 :             cnt -= 2 + ps_read_extension_data(gb, ps, ps_extension_id);
     232             :         }
     233           0 :         if (cnt < 0) {
     234           0 :             av_log(avctx, AV_LOG_ERROR, "ps extension overflow %d\n", cnt);
     235           0 :             goto err;
     236             :         }
     237           0 :         skip_bits(gb, cnt);
     238             :     }
     239             : 
     240        1563 :     ps->enable_ipdopd &= !PS_BASELINE;
     241             : 
     242             :     //Fix up envelopes
     243        1563 :     if (!ps->num_env || ps->border_position[ps->num_env] < numQMFSlots - 1) {
     244             :         //Create a fake envelope
     245          80 :         int source = ps->num_env ? ps->num_env - 1 : ps->num_env_old - 1;
     246             :         int b;
     247          80 :         if (source >= 0 && source != ps->num_env) {
     248          29 :             if (ps->enable_iid) {
     249          21 :                 memcpy(ps->iid_par+ps->num_env, ps->iid_par+source, sizeof(ps->iid_par[0]));
     250             :             }
     251          29 :             if (ps->enable_icc) {
     252          21 :                 memcpy(ps->icc_par+ps->num_env, ps->icc_par+source, sizeof(ps->icc_par[0]));
     253             :             }
     254          29 :             if (ps->enable_ipdopd) {
     255           0 :                 memcpy(ps->ipd_par+ps->num_env, ps->ipd_par+source, sizeof(ps->ipd_par[0]));
     256           0 :                 memcpy(ps->opd_par+ps->num_env, ps->opd_par+source, sizeof(ps->opd_par[0]));
     257             :             }
     258             :         }
     259          80 :         if (ps->enable_iid){
     260         735 :             for (b = 0; b < ps->nr_iid_par; b++) {
     261         700 :                 if (FFABS(ps->iid_par[ps->num_env][b]) > 7 + 8 * ps->iid_quant) {
     262           0 :                     av_log(avctx, AV_LOG_ERROR, "iid_par invalid\n");
     263           0 :                     goto err;
     264             :                 }
     265             :             }
     266             :         }
     267          80 :         if (ps->enable_icc){
     268         882 :             for (b = 0; b < ps->nr_iid_par; b++) {
     269         840 :                 if (ps->icc_par[ps->num_env][b] > 7U) {
     270           0 :                     av_log(avctx, AV_LOG_ERROR, "icc_par invalid\n");
     271           0 :                     goto err;
     272             :                 }
     273             :             }
     274             :         }
     275          80 :         ps->num_env++;
     276          80 :         ps->border_position[ps->num_env] = numQMFSlots - 1;
     277             :     }
     278             : 
     279             : 
     280        1563 :     ps->is34bands_old = ps->is34bands;
     281        1563 :     if (!PS_BASELINE && (ps->enable_iid || ps->enable_icc))
     282        3038 :         ps->is34bands = (ps->enable_iid && ps->nr_iid_par == 34) ||
     283        2890 :                         (ps->enable_icc && ps->nr_icc_par == 34);
     284             : 
     285             :     //Baseline
     286        1563 :     if (!ps->enable_ipdopd) {
     287        1563 :         memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
     288        1563 :         memset(ps->opd_par, 0, sizeof(ps->opd_par));
     289             :     }
     290             : 
     291        1563 :     if (header)
     292         303 :         ps->start = 1;
     293             : 
     294        1563 :     bits_consumed = get_bits_count(gb) - bit_count_start;
     295        1563 :     if (bits_consumed <= bits_left) {
     296        1563 :         skip_bits_long(gb_host, bits_consumed);
     297        1563 :         return bits_consumed;
     298             :     }
     299           0 :     av_log(avctx, AV_LOG_ERROR, "Expected to read %d PS bits actually read %d.\n", bits_left, bits_consumed);
     300             : err:
     301           0 :     ps->start = 0;
     302           0 :     skip_bits_long(gb_host, bits_left);
     303           0 :     memset(ps->iid_par, 0, sizeof(ps->iid_par));
     304           0 :     memset(ps->icc_par, 0, sizeof(ps->icc_par));
     305           0 :     memset(ps->ipd_par, 0, sizeof(ps->ipd_par));
     306           0 :     memset(ps->opd_par, 0, sizeof(ps->opd_par));
     307           0 :     return bits_left;
     308             : }
     309             : 
     310             : /** Split one subband into 2 subsubbands with a symmetric real filter.
     311             :  * The filter must have its non-center even coefficients equal to zero. */
     312        2922 : static void hybrid2_re(INTFLOAT (*in)[2], INTFLOAT (*out)[32][2], const INTFLOAT filter[8], int len, int reverse)
     313             : {
     314             :     int i, j;
     315       96426 :     for (i = 0; i < len; i++, in++) {
     316       93504 :         INT64FLOAT re_in = AAC_MUL31(filter[6], in[6][0]); //real inphase
     317       93504 :         INT64FLOAT re_op = 0.0f;                          //real out of phase
     318       93504 :         INT64FLOAT im_in = AAC_MUL31(filter[6], in[6][1]); //imag inphase
     319       93504 :         INT64FLOAT im_op = 0.0f;                          //imag out of phase
     320      374016 :         for (j = 0; j < 6; j += 2) {
     321      280512 :             re_op += (INT64FLOAT)filter[j+1] * (in[j+1][0] + in[12-j-1][0]);
     322      280512 :             im_op += (INT64FLOAT)filter[j+1] * (in[j+1][1] + in[12-j-1][1]);
     323             :         }
     324             : 
     325             : #if USE_FIXED
     326           0 :         re_op = (re_op + 0x40000000) >> 31;
     327           0 :         im_op = (im_op + 0x40000000) >> 31;
     328             : #endif /* USE_FIXED */
     329             : 
     330       93504 :         out[ reverse][i][0] = (INTFLOAT)(re_in + re_op);
     331       93504 :         out[ reverse][i][1] = (INTFLOAT)(im_in + im_op);
     332       93504 :         out[!reverse][i][0] = (INTFLOAT)(re_in - re_op);
     333       93504 :         out[!reverse][i][1] = (INTFLOAT)(im_in - im_op);
     334             :     }
     335        2922 : }
     336             : 
     337             : /** Split one subband into 6 subsubbands with a complex filter */
     338        1461 : static void hybrid6_cx(PSDSPContext *dsp, INTFLOAT (*in)[2], INTFLOAT (*out)[32][2],
     339             :                        TABLE_CONST INTFLOAT (*filter)[8][2], int len)
     340             : {
     341             :     int i;
     342        1461 :     int N = 8;
     343        1461 :     LOCAL_ALIGNED_16(INTFLOAT, temp, [8], [2]);
     344             : 
     345       48213 :     for (i = 0; i < len; i++, in++) {
     346       46752 :         dsp->hybrid_analysis(temp, in, (const INTFLOAT (*)[8][2]) filter, 1, N);
     347       46752 :         out[0][i][0] = temp[6][0];
     348       46752 :         out[0][i][1] = temp[6][1];
     349       46752 :         out[1][i][0] = temp[7][0];
     350       46752 :         out[1][i][1] = temp[7][1];
     351       46752 :         out[2][i][0] = temp[0][0];
     352       46752 :         out[2][i][1] = temp[0][1];
     353       46752 :         out[3][i][0] = temp[1][0];
     354       46752 :         out[3][i][1] = temp[1][1];
     355       46752 :         out[4][i][0] = temp[2][0] + temp[5][0];
     356       46752 :         out[4][i][1] = temp[2][1] + temp[5][1];
     357       46752 :         out[5][i][0] = temp[3][0] + temp[4][0];
     358       46752 :         out[5][i][1] = temp[3][1] + temp[4][1];
     359             :     }
     360        1461 : }
     361             : 
     362         510 : static void hybrid4_8_12_cx(PSDSPContext *dsp,
     363             :                             INTFLOAT (*in)[2], INTFLOAT (*out)[32][2],
     364             :                             TABLE_CONST INTFLOAT (*filter)[8][2], int N, int len)
     365             : {
     366             :     int i;
     367             : 
     368       16830 :     for (i = 0; i < len; i++, in++) {
     369       16320 :         dsp->hybrid_analysis(out[0] + i, in, (const INTFLOAT (*)[8][2]) filter, 32, N);
     370             :     }
     371         510 : }
     372             : 
     373        1563 : static void hybrid_analysis(PSDSPContext *dsp, INTFLOAT out[91][32][2],
     374             :                             INTFLOAT in[5][44][2], INTFLOAT L[2][38][64],
     375             :                             int is34, int len)
     376             : {
     377             :     int i, j;
     378        9378 :     for (i = 0; i < 5; i++) {
     379      304785 :         for (j = 0; j < 38; j++) {
     380      296970 :             in[i][j+6][0] = L[0][j][i];
     381      296970 :             in[i][j+6][1] = L[1][j][i];
     382             :         }
     383             :     }
     384        1563 :     if (is34) {
     385         102 :         hybrid4_8_12_cx(dsp, in[0], out,    f34_0_12, 12, len);
     386         102 :         hybrid4_8_12_cx(dsp, in[1], out+12, f34_1_8,   8, len);
     387         102 :         hybrid4_8_12_cx(dsp, in[2], out+20, f34_2_4,   4, len);
     388         102 :         hybrid4_8_12_cx(dsp, in[3], out+24, f34_2_4,   4, len);
     389         102 :         hybrid4_8_12_cx(dsp, in[4], out+28, f34_2_4,   4, len);
     390         102 :         dsp->hybrid_analysis_ileave(out + 27, L, 5, len);
     391             :     } else {
     392        1461 :         hybrid6_cx(dsp, in[0], out, f20_0_8, len);
     393        1461 :         hybrid2_re(in[1], out+6, g1_Q2, len, 1);
     394        1461 :         hybrid2_re(in[2], out+8, g1_Q2, len, 0);
     395        1461 :         dsp->hybrid_analysis_ileave(out + 7, L, 3, len);
     396             :     }
     397             :     //update in_buf
     398        9378 :     for (i = 0; i < 5; i++) {
     399        7815 :         memcpy(in[i], in[i]+32, 6 * sizeof(in[i][0]));
     400             :     }
     401        1563 : }
     402             : 
     403        3126 : static void hybrid_synthesis(PSDSPContext *dsp, INTFLOAT out[2][38][64],
     404             :                              INTFLOAT in[91][32][2], int is34, int len)
     405             : {
     406             :     int i, n;
     407        3126 :     if (is34) {
     408        6732 :         for (n = 0; n < len; n++) {
     409        6528 :             memset(out[0][n], 0, 5*sizeof(out[0][n][0]));
     410        6528 :             memset(out[1][n], 0, 5*sizeof(out[1][n][0]));
     411       84864 :             for (i = 0; i < 12; i++) {
     412       78336 :                 out[0][n][0] += in[   i][n][0];
     413       78336 :                 out[1][n][0] += in[   i][n][1];
     414             :             }
     415       58752 :             for (i = 0; i < 8; i++) {
     416       52224 :                 out[0][n][1] += in[12+i][n][0];
     417       52224 :                 out[1][n][1] += in[12+i][n][1];
     418             :             }
     419       32640 :             for (i = 0; i < 4; i++) {
     420       26112 :                 out[0][n][2] += in[20+i][n][0];
     421       26112 :                 out[1][n][2] += in[20+i][n][1];
     422       26112 :                 out[0][n][3] += in[24+i][n][0];
     423       26112 :                 out[1][n][3] += in[24+i][n][1];
     424       26112 :                 out[0][n][4] += in[28+i][n][0];
     425       26112 :                 out[1][n][4] += in[28+i][n][1];
     426             :             }
     427             :         }
     428         204 :         dsp->hybrid_synthesis_deint(out, in + 27, 5, len);
     429             :     } else {
     430       96426 :         for (n = 0; n < len; n++) {
     431      280512 :             out[0][n][0] = in[0][n][0] + in[1][n][0] + in[2][n][0] +
     432      187008 :                            in[3][n][0] + in[4][n][0] + in[5][n][0];
     433      280512 :             out[1][n][0] = in[0][n][1] + in[1][n][1] + in[2][n][1] +
     434      187008 :                            in[3][n][1] + in[4][n][1] + in[5][n][1];
     435       93504 :             out[0][n][1] = in[6][n][0] + in[7][n][0];
     436       93504 :             out[1][n][1] = in[6][n][1] + in[7][n][1];
     437       93504 :             out[0][n][2] = in[8][n][0] + in[9][n][0];
     438       93504 :             out[1][n][2] = in[8][n][1] + in[9][n][1];
     439             :         }
     440        2922 :         dsp->hybrid_synthesis_deint(out, in + 7, 3, len);
     441             :     }
     442        3126 : }
     443             : 
     444             : /// All-pass filter decay slope
     445             : #define DECAY_SLOPE      Q30(0.05f)
     446             : /// Number of frequency bands that can be addressed by the parameter index, b(k)
     447             : static const int   NR_PAR_BANDS[]      = { 20, 34 };
     448             : static const int   NR_IPDOPD_BANDS[]   = { 11, 17 };
     449             : /// Number of frequency bands that can be addressed by the sub subband index, k
     450             : static const int   NR_BANDS[]          = { 71, 91 };
     451             : /// Start frequency band for the all-pass filter decay slope
     452             : static const int   DECAY_CUTOFF[]      = { 10, 32 };
     453             : /// Number of all-pass filer bands
     454             : static const int   NR_ALLPASS_BANDS[]  = { 30, 50 };
     455             : /// First stereo band using the short one sample delay
     456             : static const int   SHORT_DELAY_BAND[]  = { 42, 62 };
     457             : 
     458             : /** Table 8.46 */
     459           1 : static void map_idx_10_to_20(int8_t *par_mapped, const int8_t *par, int full)
     460             : {
     461             :     int b;
     462           1 :     if (full)
     463           1 :         b = 9;
     464             :     else {
     465           0 :         b = 4;
     466           0 :         par_mapped[10] = 0;
     467             :     }
     468          11 :     for (; b >= 0; b--) {
     469          10 :         par_mapped[2*b+1] = par_mapped[2*b] = par[b];
     470             :     }
     471           1 : }
     472             : 
     473           1 : static void map_idx_34_to_20(int8_t *par_mapped, const int8_t *par, int full)
     474             : {
     475           1 :     par_mapped[ 0] = (2*par[ 0] +   par[ 1]) / 3;
     476           1 :     par_mapped[ 1] = (  par[ 1] + 2*par[ 2]) / 3;
     477           1 :     par_mapped[ 2] = (2*par[ 3] +   par[ 4]) / 3;
     478           1 :     par_mapped[ 3] = (  par[ 4] + 2*par[ 5]) / 3;
     479           1 :     par_mapped[ 4] = (  par[ 6] +   par[ 7]) / 2;
     480           1 :     par_mapped[ 5] = (  par[ 8] +   par[ 9]) / 2;
     481           1 :     par_mapped[ 6] =    par[10];
     482           1 :     par_mapped[ 7] =    par[11];
     483           1 :     par_mapped[ 8] = (  par[12] +   par[13]) / 2;
     484           1 :     par_mapped[ 9] = (  par[14] +   par[15]) / 2;
     485           1 :     par_mapped[10] =    par[16];
     486           1 :     if (full) {
     487           1 :         par_mapped[11] =    par[17];
     488           1 :         par_mapped[12] =    par[18];
     489           1 :         par_mapped[13] =    par[19];
     490           1 :         par_mapped[14] = (  par[20] +   par[21]) / 2;
     491           1 :         par_mapped[15] = (  par[22] +   par[23]) / 2;
     492           1 :         par_mapped[16] = (  par[24] +   par[25]) / 2;
     493           1 :         par_mapped[17] = (  par[26] +   par[27]) / 2;
     494           1 :         par_mapped[18] = (  par[28] +   par[29] +   par[30] +   par[31]) / 4;
     495           1 :         par_mapped[19] = (  par[32] +   par[33]) / 2;
     496             :     }
     497           1 : }
     498             : 
     499           8 : static void map_val_34_to_20(INTFLOAT par[PS_MAX_NR_IIDICC])
     500             : {
     501             : #if USE_FIXED
     502           0 :     par[ 0] = (int)(((int64_t)(par[ 0] + (par[ 1]>>1)) * 1431655765 + \
     503           0 :                       0x40000000) >> 31);
     504           0 :     par[ 1] = (int)(((int64_t)((par[ 1]>>1) + par[ 2]) * 1431655765 + \
     505           0 :                       0x40000000) >> 31);
     506           0 :     par[ 2] = (int)(((int64_t)(par[ 3] + (par[ 4]>>1)) * 1431655765 + \
     507           0 :                       0x40000000) >> 31);
     508           0 :     par[ 3] = (int)(((int64_t)((par[ 4]>>1) + par[ 5]) * 1431655765 + \
     509           0 :                       0x40000000) >> 31);
     510             : #else
     511           8 :     par[ 0] = (2*par[ 0] +   par[ 1]) * 0.33333333f;
     512           8 :     par[ 1] = (  par[ 1] + 2*par[ 2]) * 0.33333333f;
     513           8 :     par[ 2] = (2*par[ 3] +   par[ 4]) * 0.33333333f;
     514           8 :     par[ 3] = (  par[ 4] + 2*par[ 5]) * 0.33333333f;
     515             : #endif /* USE_FIXED */
     516           8 :     par[ 4] = AAC_HALF_SUM(par[ 6], par[ 7]);
     517           8 :     par[ 5] = AAC_HALF_SUM(par[ 8], par[ 9]);
     518           8 :     par[ 6] =    par[10];
     519           8 :     par[ 7] =    par[11];
     520           8 :     par[ 8] = AAC_HALF_SUM(par[12], par[13]);
     521           8 :     par[ 9] = AAC_HALF_SUM(par[14], par[15]);
     522           8 :     par[10] =    par[16];
     523           8 :     par[11] =    par[17];
     524           8 :     par[12] =    par[18];
     525           8 :     par[13] =    par[19];
     526           8 :     par[14] = AAC_HALF_SUM(par[20], par[21]);
     527           8 :     par[15] = AAC_HALF_SUM(par[22], par[23]);
     528           8 :     par[16] = AAC_HALF_SUM(par[24], par[25]);
     529           8 :     par[17] = AAC_HALF_SUM(par[26], par[27]);
     530             : #if USE_FIXED
     531           0 :     par[18] = (((par[28]+2)>>2) + ((par[29]+2)>>2) + ((par[30]+2)>>2) + ((par[31]+2)>>2));
     532             : #else
     533           8 :     par[18] = (  par[28] +   par[29] +   par[30] +   par[31]) * 0.25f;
     534             : #endif /* USE_FIXED */
     535           8 :     par[19] = AAC_HALF_SUM(par[32], par[33]);
     536           8 : }
     537             : 
     538         204 : static void map_idx_10_to_34(int8_t *par_mapped, const int8_t *par, int full)
     539             : {
     540         204 :     if (full) {
     541         204 :         par_mapped[33] = par[9];
     542         204 :         par_mapped[32] = par[9];
     543         204 :         par_mapped[31] = par[9];
     544         204 :         par_mapped[30] = par[9];
     545         204 :         par_mapped[29] = par[9];
     546         204 :         par_mapped[28] = par[9];
     547         204 :         par_mapped[27] = par[8];
     548         204 :         par_mapped[26] = par[8];
     549         204 :         par_mapped[25] = par[8];
     550         204 :         par_mapped[24] = par[8];
     551         204 :         par_mapped[23] = par[7];
     552         204 :         par_mapped[22] = par[7];
     553         204 :         par_mapped[21] = par[7];
     554         204 :         par_mapped[20] = par[7];
     555         204 :         par_mapped[19] = par[6];
     556         204 :         par_mapped[18] = par[6];
     557         204 :         par_mapped[17] = par[5];
     558         204 :         par_mapped[16] = par[5];
     559             :     } else {
     560           0 :         par_mapped[16] =      0;
     561             :     }
     562         204 :     par_mapped[15] = par[4];
     563         204 :     par_mapped[14] = par[4];
     564         204 :     par_mapped[13] = par[4];
     565         204 :     par_mapped[12] = par[4];
     566         204 :     par_mapped[11] = par[3];
     567         204 :     par_mapped[10] = par[3];
     568         204 :     par_mapped[ 9] = par[2];
     569         204 :     par_mapped[ 8] = par[2];
     570         204 :     par_mapped[ 7] = par[2];
     571         204 :     par_mapped[ 6] = par[2];
     572         204 :     par_mapped[ 5] = par[1];
     573         204 :     par_mapped[ 4] = par[1];
     574         204 :     par_mapped[ 3] = par[1];
     575         204 :     par_mapped[ 2] = par[0];
     576         204 :     par_mapped[ 1] = par[0];
     577         204 :     par_mapped[ 0] = par[0];
     578         204 : }
     579             : 
     580           0 : static void map_idx_20_to_34(int8_t *par_mapped, const int8_t *par, int full)
     581             : {
     582           0 :     if (full) {
     583           0 :         par_mapped[33] =  par[19];
     584           0 :         par_mapped[32] =  par[19];
     585           0 :         par_mapped[31] =  par[18];
     586           0 :         par_mapped[30] =  par[18];
     587           0 :         par_mapped[29] =  par[18];
     588           0 :         par_mapped[28] =  par[18];
     589           0 :         par_mapped[27] =  par[17];
     590           0 :         par_mapped[26] =  par[17];
     591           0 :         par_mapped[25] =  par[16];
     592           0 :         par_mapped[24] =  par[16];
     593           0 :         par_mapped[23] =  par[15];
     594           0 :         par_mapped[22] =  par[15];
     595           0 :         par_mapped[21] =  par[14];
     596           0 :         par_mapped[20] =  par[14];
     597           0 :         par_mapped[19] =  par[13];
     598           0 :         par_mapped[18] =  par[12];
     599           0 :         par_mapped[17] =  par[11];
     600             :     }
     601           0 :     par_mapped[16] =  par[10];
     602           0 :     par_mapped[15] =  par[ 9];
     603           0 :     par_mapped[14] =  par[ 9];
     604           0 :     par_mapped[13] =  par[ 8];
     605           0 :     par_mapped[12] =  par[ 8];
     606           0 :     par_mapped[11] =  par[ 7];
     607           0 :     par_mapped[10] =  par[ 6];
     608           0 :     par_mapped[ 9] =  par[ 5];
     609           0 :     par_mapped[ 8] =  par[ 5];
     610           0 :     par_mapped[ 7] =  par[ 4];
     611           0 :     par_mapped[ 6] =  par[ 4];
     612           0 :     par_mapped[ 5] =  par[ 3];
     613           0 :     par_mapped[ 4] = (par[ 2] + par[ 3]) / 2;
     614           0 :     par_mapped[ 3] =  par[ 2];
     615           0 :     par_mapped[ 2] =  par[ 1];
     616           0 :     par_mapped[ 1] = (par[ 0] + par[ 1]) / 2;
     617           0 :     par_mapped[ 0] =  par[ 0];
     618           0 : }
     619             : 
     620          16 : static void map_val_20_to_34(INTFLOAT par[PS_MAX_NR_IIDICC])
     621             : {
     622          16 :     par[33] =  par[19];
     623          16 :     par[32] =  par[19];
     624          16 :     par[31] =  par[18];
     625          16 :     par[30] =  par[18];
     626          16 :     par[29] =  par[18];
     627          16 :     par[28] =  par[18];
     628          16 :     par[27] =  par[17];
     629          16 :     par[26] =  par[17];
     630          16 :     par[25] =  par[16];
     631          16 :     par[24] =  par[16];
     632          16 :     par[23] =  par[15];
     633          16 :     par[22] =  par[15];
     634          16 :     par[21] =  par[14];
     635          16 :     par[20] =  par[14];
     636          16 :     par[19] =  par[13];
     637          16 :     par[18] =  par[12];
     638          16 :     par[17] =  par[11];
     639          16 :     par[16] =  par[10];
     640          16 :     par[15] =  par[ 9];
     641          16 :     par[14] =  par[ 9];
     642          16 :     par[13] =  par[ 8];
     643          16 :     par[12] =  par[ 8];
     644          16 :     par[11] =  par[ 7];
     645          16 :     par[10] =  par[ 6];
     646          16 :     par[ 9] =  par[ 5];
     647          16 :     par[ 8] =  par[ 5];
     648          16 :     par[ 7] =  par[ 4];
     649          16 :     par[ 6] =  par[ 4];
     650          16 :     par[ 5] =  par[ 3];
     651          16 :     par[ 4] = AAC_HALF_SUM(par[ 2], par[ 3]);
     652          16 :     par[ 3] =  par[ 2];
     653          16 :     par[ 2] =  par[ 1];
     654          16 :     par[ 1] = AAC_HALF_SUM(par[ 0], par[ 1]);
     655          16 : }
     656             : 
     657        1563 : static void decorrelation(PSContext *ps, INTFLOAT (*out)[32][2], const INTFLOAT (*s)[32][2], int is34)
     658             : {
     659        1563 :     LOCAL_ALIGNED_16(INTFLOAT, power, [34], [PS_QMF_TIME_SLOTS]);
     660        1563 :     LOCAL_ALIGNED_16(INTFLOAT, transient_gain, [34], [PS_QMF_TIME_SLOTS]);
     661        1563 :     INTFLOAT *peak_decay_nrg = ps->peak_decay_nrg;
     662        1563 :     INTFLOAT *power_smooth = ps->power_smooth;
     663        1563 :     INTFLOAT *peak_decay_diff_smooth = ps->peak_decay_diff_smooth;
     664        1563 :     INTFLOAT (*delay)[PS_QMF_TIME_SLOTS + PS_MAX_DELAY][2] = ps->delay;
     665        1563 :     INTFLOAT (*ap_delay)[PS_AP_LINKS][PS_QMF_TIME_SLOTS + PS_MAX_AP_DELAY][2] = ps->ap_delay;
     666             : #if !USE_FIXED
     667        1563 :     const float transient_impact  = 1.5f;
     668        1563 :     const float a_smooth          = 0.25f; ///< Smoothing coefficient
     669             : #endif /* USE_FIXED */
     670        1563 :     const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
     671             :     int i, k, m, n;
     672        1563 :     int n0 = 0, nL = 32;
     673        1563 :     const INTFLOAT peak_decay_factor = Q31(0.76592833836465f);
     674             : 
     675        1563 :     memset(power, 0, 34 * sizeof(*power));
     676             : 
     677        1563 :     if (is34 != ps->is34bands_old) {
     678           3 :         memset(ps->peak_decay_nrg,         0, sizeof(ps->peak_decay_nrg));
     679           3 :         memset(ps->power_smooth,           0, sizeof(ps->power_smooth));
     680           3 :         memset(ps->peak_decay_diff_smooth, 0, sizeof(ps->peak_decay_diff_smooth));
     681           3 :         memset(ps->delay,                  0, sizeof(ps->delay));
     682           3 :         memset(ps->ap_delay,               0, sizeof(ps->ap_delay));
     683             :     }
     684             : 
     685      114576 :     for (k = 0; k < NR_BANDS[is34]; k++) {
     686      113013 :         int i = k_to_i[k];
     687      113013 :         ps->dsp.add_squares(power[i], s[k], nL - n0);
     688             :     }
     689             : 
     690             :     //Transient detection
     691             : #if USE_FIXED
     692           0 :     for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
     693           0 :         for (n = n0; n < nL; n++) {
     694             :             int decayed_peak;
     695             :             int denom;
     696             : 
     697           0 :             decayed_peak = (int)(((int64_t)peak_decay_factor * \
     698           0 :                                            peak_decay_nrg[i] + 0x40000000) >> 31);
     699           0 :             peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
     700           0 :             power_smooth[i] += (power[i][n] - power_smooth[i] + 2) >> 2;
     701           0 :             peak_decay_diff_smooth[i] += (peak_decay_nrg[i] - power[i][n] - \
     702           0 :                                           peak_decay_diff_smooth[i] + 2) >> 2;
     703           0 :             denom = peak_decay_diff_smooth[i] + (peak_decay_diff_smooth[i] >> 1);
     704           0 :             if (denom > power_smooth[i]) {
     705           0 :               int p = power_smooth[i];
     706           0 :               while (denom < 0x40000000) {
     707           0 :                 denom <<= 1;
     708           0 :                 p <<= 1;
     709             :               }
     710           0 :               transient_gain[i][n] = p / (denom >> 16);
     711             :             }
     712             :             else {
     713           0 :               transient_gain[i][n] = 1 << 16;
     714             :             }
     715             :         }
     716             :     }
     717             : #else
     718       34251 :     for (i = 0; i < NR_PAR_BANDS[is34]; i++) {
     719     1078704 :         for (n = n0; n < nL; n++) {
     720     1046016 :             float decayed_peak = peak_decay_factor * peak_decay_nrg[i];
     721             :             float denom;
     722     1046016 :             peak_decay_nrg[i] = FFMAX(decayed_peak, power[i][n]);
     723     1046016 :             power_smooth[i] += a_smooth * (power[i][n] - power_smooth[i]);
     724     1046016 :             peak_decay_diff_smooth[i] += a_smooth * (peak_decay_nrg[i] - power[i][n] - peak_decay_diff_smooth[i]);
     725     1046016 :             denom = transient_impact * peak_decay_diff_smooth[i];
     726     2092032 :             transient_gain[i][n]   = (denom > power_smooth[i]) ?
     727     1046016 :                                          power_smooth[i] / denom : 1.0f;
     728             :         }
     729             :     }
     730             : 
     731             : #endif /* USE_FIXED */
     732             :     //Decorrelation and transient reduction
     733             :     //                         PS_AP_LINKS - 1
     734             :     //                               -----
     735             :     //                                | |  Q_fract_allpass[k][m]*z^-link_delay[m] - a[m]*g_decay_slope[k]
     736             :     //H[k][z] = z^-2 * phi_fract[k] * | | ----------------------------------------------------------------
     737             :     //                                | | 1 - a[m]*g_decay_slope[k]*Q_fract_allpass[k][m]*z^-link_delay[m]
     738             :     //                               m = 0
     739             :     //d[k][z] (out) = transient_gain_mapped[k][z] * H[k][z] * s[k][z]
     740       50493 :     for (k = 0; k < NR_ALLPASS_BANDS[is34]; k++) {
     741       48930 :         int b = k_to_i[k];
     742             : #if USE_FIXED
     743             :         int g_decay_slope;
     744             : 
     745           0 :         if (k - DECAY_CUTOFF[is34] <= 0) {
     746           0 :           g_decay_slope = 1 << 30;
     747             :         }
     748           0 :         else if (k - DECAY_CUTOFF[is34] >= 20) {
     749           0 :           g_decay_slope = 0;
     750             :         }
     751             :         else {
     752           0 :           g_decay_slope = (1 << 30) - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
     753             :         }
     754             : #else
     755       48930 :         float g_decay_slope = 1.f - DECAY_SLOPE * (k - DECAY_CUTOFF[is34]);
     756       48930 :         g_decay_slope = av_clipf(g_decay_slope, 0.f, 1.f);
     757             : #endif /* USE_FIXED */
     758       48930 :         memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
     759       48930 :         memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
     760      195720 :         for (m = 0; m < PS_AP_LINKS; m++) {
     761      146790 :             memcpy(ap_delay[k][m],   ap_delay[k][m]+numQMFSlots,           5*sizeof(ap_delay[k][m][0]));
     762             :         }
     763       97860 :         ps->dsp.decorrelate(out[k], delay[k] + PS_MAX_DELAY - 2, ap_delay[k],
     764       48930 :                             phi_fract[is34][k],
     765       48930 :                             (const INTFLOAT (*)[2]) Q_fract_allpass[is34][k],
     766       48930 :                             transient_gain[b], g_decay_slope, nL - n0);
     767             :     }
     768       20319 :     for (; k < SHORT_DELAY_BAND[is34]; k++) {
     769       18756 :         int i = k_to_i[k];
     770       18756 :         memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
     771       18756 :         memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
     772             :         //H = delay 14
     773       37512 :         ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 14,
     774       18756 :                                 transient_gain[i], nL - n0);
     775             :     }
     776       46890 :     for (; k < NR_BANDS[is34]; k++) {
     777       45327 :         int i = k_to_i[k];
     778       45327 :         memcpy(delay[k], delay[k]+nL, PS_MAX_DELAY*sizeof(delay[k][0]));
     779       45327 :         memcpy(delay[k]+PS_MAX_DELAY, s[k], numQMFSlots*sizeof(delay[k][0]));
     780             :         //H = delay 1
     781       90654 :         ps->dsp.mul_pair_single(out[k], delay[k] + PS_MAX_DELAY - 1,
     782       45327 :                                 transient_gain[i], nL - n0);
     783             :     }
     784        1563 : }
     785             : 
     786         204 : static void remap34(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
     787             :                     int8_t           (*par)[PS_MAX_NR_IIDICC],
     788             :                     int num_par, int num_env, int full)
     789             : {
     790         204 :     int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
     791             :     int e;
     792         204 :     if (num_par == 20 || num_par == 11) {
     793           0 :         for (e = 0; e < num_env; e++) {
     794           0 :             map_idx_20_to_34(par_mapped[e], par[e], full);
     795             :         }
     796         306 :     } else if (num_par == 10 || num_par == 5) {
     797         306 :         for (e = 0; e < num_env; e++) {
     798         204 :             map_idx_10_to_34(par_mapped[e], par[e], full);
     799             :         }
     800             :     } else {
     801         102 :         *p_par_mapped = par;
     802             :     }
     803         204 : }
     804             : 
     805        2922 : static void remap20(int8_t (**p_par_mapped)[PS_MAX_NR_IIDICC],
     806             :                     int8_t           (*par)[PS_MAX_NR_IIDICC],
     807             :                     int num_par, int num_env, int full)
     808             : {
     809        2922 :     int8_t (*par_mapped)[PS_MAX_NR_IIDICC] = *p_par_mapped;
     810             :     int e;
     811        2923 :     if (num_par == 34 || num_par == 17) {
     812           2 :         for (e = 0; e < num_env; e++) {
     813           1 :             map_idx_34_to_20(par_mapped[e], par[e], full);
     814             :         }
     815        2922 :     } else if (num_par == 10 || num_par == 5) {
     816           2 :         for (e = 0; e < num_env; e++) {
     817           1 :             map_idx_10_to_20(par_mapped[e], par[e], full);
     818             :         }
     819             :     } else {
     820        2920 :         *p_par_mapped = par;
     821             :     }
     822        2922 : }
     823             : 
     824        1563 : static void stereo_processing(PSContext *ps, INTFLOAT (*l)[32][2], INTFLOAT (*r)[32][2], int is34)
     825             : {
     826             :     int e, b, k;
     827             : 
     828        1563 :     INTFLOAT (*H11)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H11;
     829        1563 :     INTFLOAT (*H12)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H12;
     830        1563 :     INTFLOAT (*H21)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H21;
     831        1563 :     INTFLOAT (*H22)[PS_MAX_NUM_ENV+1][PS_MAX_NR_IIDICC] = ps->H22;
     832        1563 :     int8_t *opd_hist = ps->opd_hist;
     833        1563 :     int8_t *ipd_hist = ps->ipd_hist;
     834             :     int8_t iid_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
     835             :     int8_t icc_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
     836             :     int8_t ipd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
     837             :     int8_t opd_mapped_buf[PS_MAX_NUM_ENV][PS_MAX_NR_IIDICC];
     838        1563 :     int8_t (*iid_mapped)[PS_MAX_NR_IIDICC] = iid_mapped_buf;
     839        1563 :     int8_t (*icc_mapped)[PS_MAX_NR_IIDICC] = icc_mapped_buf;
     840        1563 :     int8_t (*ipd_mapped)[PS_MAX_NR_IIDICC] = ipd_mapped_buf;
     841        1563 :     int8_t (*opd_mapped)[PS_MAX_NR_IIDICC] = opd_mapped_buf;
     842        1563 :     const int8_t *k_to_i = is34 ? k_to_i_34 : k_to_i_20;
     843        1563 :     TABLE_CONST INTFLOAT (*H_LUT)[8][4] = (PS_BASELINE || ps->icc_mode < 3) ? HA : HB;
     844             : 
     845             :     //Remapping
     846        1563 :     if (ps->num_env_old) {
     847        1538 :         memcpy(H11[0][0], H11[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[0][0][0]));
     848        1538 :         memcpy(H11[1][0], H11[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H11[1][0][0]));
     849        1538 :         memcpy(H12[0][0], H12[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[0][0][0]));
     850        1538 :         memcpy(H12[1][0], H12[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H12[1][0][0]));
     851        1538 :         memcpy(H21[0][0], H21[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[0][0][0]));
     852        1538 :         memcpy(H21[1][0], H21[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H21[1][0][0]));
     853        1538 :         memcpy(H22[0][0], H22[0][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[0][0][0]));
     854        1538 :         memcpy(H22[1][0], H22[1][ps->num_env_old], PS_MAX_NR_IIDICC*sizeof(H22[1][0][0]));
     855             :     }
     856             : 
     857        1563 :     if (is34) {
     858         102 :         remap34(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
     859         102 :         remap34(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
     860         102 :         if (ps->enable_ipdopd) {
     861           0 :             remap34(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
     862           0 :             remap34(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
     863             :         }
     864         102 :         if (!ps->is34bands_old) {
     865           2 :             map_val_20_to_34(H11[0][0]);
     866           2 :             map_val_20_to_34(H11[1][0]);
     867           2 :             map_val_20_to_34(H12[0][0]);
     868           2 :             map_val_20_to_34(H12[1][0]);
     869           2 :             map_val_20_to_34(H21[0][0]);
     870           2 :             map_val_20_to_34(H21[1][0]);
     871           2 :             map_val_20_to_34(H22[0][0]);
     872           2 :             map_val_20_to_34(H22[1][0]);
     873           2 :             ipdopd_reset(ipd_hist, opd_hist);
     874             :         }
     875             :     } else {
     876        1461 :         remap20(&iid_mapped, ps->iid_par, ps->nr_iid_par, ps->num_env, 1);
     877        1461 :         remap20(&icc_mapped, ps->icc_par, ps->nr_icc_par, ps->num_env, 1);
     878        1461 :         if (ps->enable_ipdopd) {
     879           0 :             remap20(&ipd_mapped, ps->ipd_par, ps->nr_ipdopd_par, ps->num_env, 0);
     880           0 :             remap20(&opd_mapped, ps->opd_par, ps->nr_ipdopd_par, ps->num_env, 0);
     881             :         }
     882        1461 :         if (ps->is34bands_old) {
     883           1 :             map_val_34_to_20(H11[0][0]);
     884           1 :             map_val_34_to_20(H11[1][0]);
     885           1 :             map_val_34_to_20(H12[0][0]);
     886           1 :             map_val_34_to_20(H12[1][0]);
     887           1 :             map_val_34_to_20(H21[0][0]);
     888           1 :             map_val_34_to_20(H21[1][0]);
     889           1 :             map_val_34_to_20(H22[0][0]);
     890           1 :             map_val_34_to_20(H22[1][0]);
     891           1 :             ipdopd_reset(ipd_hist, opd_hist);
     892             :         }
     893             :     }
     894             : 
     895             :     //Mixing
     896        3492 :     for (e = 0; e < ps->num_env; e++) {
     897       43365 :         for (b = 0; b < NR_PAR_BANDS[is34]; b++) {
     898             :             INTFLOAT h11, h12, h21, h22;
     899       41436 :             h11 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][0];
     900       41436 :             h12 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][1];
     901       41436 :             h21 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][2];
     902       41436 :             h22 = H_LUT[iid_mapped[e][b] + 7 + 23 * ps->iid_quant][icc_mapped[e][b]][3];
     903             : 
     904       41436 :             if (!PS_BASELINE && ps->enable_ipdopd && b < NR_IPDOPD_BANDS[is34]) {
     905             :                 //The spec say says to only run this smoother when enable_ipdopd
     906             :                 //is set but the reference decoder appears to run it constantly
     907             :                 INTFLOAT h11i, h12i, h21i, h22i;
     908             :                 INTFLOAT ipd_adj_re, ipd_adj_im;
     909           0 :                 int opd_idx = opd_hist[b] * 8 + opd_mapped[e][b];
     910           0 :                 int ipd_idx = ipd_hist[b] * 8 + ipd_mapped[e][b];
     911           0 :                 INTFLOAT opd_re = pd_re_smooth[opd_idx];
     912           0 :                 INTFLOAT opd_im = pd_im_smooth[opd_idx];
     913           0 :                 INTFLOAT ipd_re = pd_re_smooth[ipd_idx];
     914           0 :                 INTFLOAT ipd_im = pd_im_smooth[ipd_idx];
     915           0 :                 opd_hist[b] = opd_idx & 0x3F;
     916           0 :                 ipd_hist[b] = ipd_idx & 0x3F;
     917             : 
     918           0 :                 ipd_adj_re = AAC_MADD30(opd_re, ipd_re, opd_im, ipd_im);
     919           0 :                 ipd_adj_im = AAC_MSUB30(opd_im, ipd_re, opd_re, ipd_im);
     920           0 :                 h11i = AAC_MUL30(h11,  opd_im);
     921           0 :                 h11  = AAC_MUL30(h11,  opd_re);
     922           0 :                 h12i = AAC_MUL30(h12,  ipd_adj_im);
     923           0 :                 h12  = AAC_MUL30(h12,  ipd_adj_re);
     924           0 :                 h21i = AAC_MUL30(h21,  opd_im);
     925           0 :                 h21  = AAC_MUL30(h21,  opd_re);
     926           0 :                 h22i = AAC_MUL30(h22,  ipd_adj_im);
     927           0 :                 h22  = AAC_MUL30(h22,  ipd_adj_re);
     928           0 :                 H11[1][e+1][b] = h11i;
     929           0 :                 H12[1][e+1][b] = h12i;
     930           0 :                 H21[1][e+1][b] = h21i;
     931           0 :                 H22[1][e+1][b] = h22i;
     932             :             }
     933       41436 :             H11[0][e+1][b] = h11;
     934       41436 :             H12[0][e+1][b] = h12;
     935       41436 :             H21[0][e+1][b] = h21;
     936       41436 :             H22[0][e+1][b] = h22;
     937             :         }
     938      142968 :         for (k = 0; k < NR_BANDS[is34]; k++) {
     939      141039 :             LOCAL_ALIGNED_16(INTFLOAT, h, [2], [4]);
     940      141039 :             LOCAL_ALIGNED_16(INTFLOAT, h_step, [2], [4]);
     941      141039 :             int start = ps->border_position[e];
     942      141039 :             int stop  = ps->border_position[e+1];
     943      141039 :             INTFLOAT width = Q30(1.f) / ((stop - start) ? (stop - start) : 1);
     944             : #if USE_FIXED
     945           0 :             width <<= 1;
     946             : #endif
     947      141039 :             b = k_to_i[k];
     948      141039 :             h[0][0] = H11[0][e][b];
     949      141039 :             h[0][1] = H12[0][e][b];
     950      141039 :             h[0][2] = H21[0][e][b];
     951      141039 :             h[0][3] = H22[0][e][b];
     952      141039 :             if (!PS_BASELINE && ps->enable_ipdopd) {
     953             :             //Is this necessary? ps_04_new seems unchanged
     954           0 :             if ((is34 && k <= 13 && k >= 9) || (!is34 && k <= 1)) {
     955           0 :                 h[1][0] = -H11[1][e][b];
     956           0 :                 h[1][1] = -H12[1][e][b];
     957           0 :                 h[1][2] = -H21[1][e][b];
     958           0 :                 h[1][3] = -H22[1][e][b];
     959             :             } else {
     960           0 :                 h[1][0] = H11[1][e][b];
     961           0 :                 h[1][1] = H12[1][e][b];
     962           0 :                 h[1][2] = H21[1][e][b];
     963           0 :                 h[1][3] = H22[1][e][b];
     964             :             }
     965             :             }
     966             :             //Interpolation
     967      141039 :             h_step[0][0] = AAC_MSUB31_V3(H11[0][e+1][b], h[0][0], width);
     968      141039 :             h_step[0][1] = AAC_MSUB31_V3(H12[0][e+1][b], h[0][1], width);
     969      141039 :             h_step[0][2] = AAC_MSUB31_V3(H21[0][e+1][b], h[0][2], width);
     970      141039 :             h_step[0][3] = AAC_MSUB31_V3(H22[0][e+1][b], h[0][3], width);
     971      141039 :             if (!PS_BASELINE && ps->enable_ipdopd) {
     972           0 :                 h_step[1][0] = AAC_MSUB31_V3(H11[1][e+1][b], h[1][0], width);
     973           0 :                 h_step[1][1] = AAC_MSUB31_V3(H12[1][e+1][b], h[1][1], width);
     974           0 :                 h_step[1][2] = AAC_MSUB31_V3(H21[1][e+1][b], h[1][2], width);
     975           0 :                 h_step[1][3] = AAC_MSUB31_V3(H22[1][e+1][b], h[1][3], width);
     976             :             }
     977      423117 :             ps->dsp.stereo_interpolate[!PS_BASELINE && ps->enable_ipdopd](
     978      282078 :                 l[k] + start + 1, r[k] + start + 1,
     979             :                 h, h_step, stop - start);
     980             :         }
     981             :     }
     982        1563 : }
     983             : 
     984        1563 : int AAC_RENAME(ff_ps_apply)(AVCodecContext *avctx, PSContext *ps, INTFLOAT L[2][38][64], INTFLOAT R[2][38][64], int top)
     985             : {
     986        1563 :     INTFLOAT (*Lbuf)[32][2] = ps->Lbuf;
     987        1563 :     INTFLOAT (*Rbuf)[32][2] = ps->Rbuf;
     988        1563 :     const int len = 32;
     989        1563 :     int is34 = ps->is34bands;
     990             : 
     991        1563 :     top += NR_BANDS[is34] - 64;
     992        1563 :     memset(ps->delay+top, 0, (NR_BANDS[is34] - top)*sizeof(ps->delay[0]));
     993        1563 :     if (top < NR_ALLPASS_BANDS[is34])
     994           0 :         memset(ps->ap_delay + top, 0, (NR_ALLPASS_BANDS[is34] - top)*sizeof(ps->ap_delay[0]));
     995             : 
     996        1563 :     hybrid_analysis(&ps->dsp, Lbuf, ps->in_buf, L, is34, len);
     997        1563 :     decorrelation(ps, Rbuf, (const INTFLOAT (*)[32][2]) Lbuf, is34);
     998        1563 :     stereo_processing(ps, Lbuf, Rbuf, is34);
     999        1563 :     hybrid_synthesis(&ps->dsp, L, Lbuf, is34, len);
    1000        1563 :     hybrid_synthesis(&ps->dsp, R, Rbuf, is34, len);
    1001             : 
    1002        1563 :     return 0;
    1003             : }
    1004             : 
    1005             : #define PS_INIT_VLC_STATIC(num, size) \
    1006             :     INIT_VLC_STATIC(&vlc_ps[num], 9, ps_tmp[num].table_size / ps_tmp[num].elem_size,    \
    1007             :                     ps_tmp[num].ps_bits, 1, 1,                                          \
    1008             :                     ps_tmp[num].ps_codes, ps_tmp[num].elem_size, ps_tmp[num].elem_size, \
    1009             :                     size);
    1010             : 
    1011             : #define PS_VLC_ROW(name) \
    1012             :     { name ## _codes, name ## _bits, sizeof(name ## _codes), sizeof(name ## _codes[0]) }
    1013             : 
    1014         151 : av_cold void AAC_RENAME(ff_ps_init)(void) {
    1015             :     // Syntax initialization
    1016             :     static const struct {
    1017             :         const void *ps_codes, *ps_bits;
    1018             :         const unsigned int table_size, elem_size;
    1019             :     } ps_tmp[] = {
    1020             :         PS_VLC_ROW(huff_iid_df1),
    1021             :         PS_VLC_ROW(huff_iid_dt1),
    1022             :         PS_VLC_ROW(huff_iid_df0),
    1023             :         PS_VLC_ROW(huff_iid_dt0),
    1024             :         PS_VLC_ROW(huff_icc_df),
    1025             :         PS_VLC_ROW(huff_icc_dt),
    1026             :         PS_VLC_ROW(huff_ipd_df),
    1027             :         PS_VLC_ROW(huff_ipd_dt),
    1028             :         PS_VLC_ROW(huff_opd_df),
    1029             :         PS_VLC_ROW(huff_opd_dt),
    1030             :     };
    1031             : 
    1032         151 :     PS_INIT_VLC_STATIC(0, 1544);
    1033         151 :     PS_INIT_VLC_STATIC(1,  832);
    1034         151 :     PS_INIT_VLC_STATIC(2, 1024);
    1035         151 :     PS_INIT_VLC_STATIC(3, 1036);
    1036         151 :     PS_INIT_VLC_STATIC(4,  544);
    1037         151 :     PS_INIT_VLC_STATIC(5,  544);
    1038         151 :     PS_INIT_VLC_STATIC(6,  512);
    1039         151 :     PS_INIT_VLC_STATIC(7,  512);
    1040         151 :     PS_INIT_VLC_STATIC(8,  512);
    1041         151 :     PS_INIT_VLC_STATIC(9,  512);
    1042             : 
    1043         151 :     ps_tableinit();
    1044         151 : }
    1045             : 
    1046         329 : av_cold void AAC_RENAME(ff_ps_ctx_init)(PSContext *ps)
    1047             : {
    1048         329 :     AAC_RENAME(ff_psdsp_init)(&ps->dsp);
    1049         329 : }

Generated by: LCOV version 1.12