ps_dec.c

00001 /*
00002 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
00003 ** Copyright (C) 2003-2005 M. Bakker, Ahead Software AG, http://www.nero.com
00004 **  
00005 ** This program is free software; you can redistribute it and/or modify
00006 ** it under the terms of the GNU General Public License as published by
00007 ** the Free Software Foundation; either version 2 of the License, or
00008 ** (at your option) any later version.
00009 ** 
00010 ** This program is distributed in the hope that it will be useful,
00011 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 ** GNU General Public License for more details.
00014 ** 
00015 ** You should have received a copy of the GNU General Public License
00016 ** along with this program; if not, write to the Free Software 
00017 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00018 **
00019 ** Any non-GPL usage of this software or parts of this software is strictly
00020 ** forbidden.
00021 **
00022 ** Software using this code must display the following message visibly in the
00023 ** software:
00024 ** "FAAD2 AAC/HE-AAC/HE-AACv2/DRM decoder (c) Ahead Software, www.nero.com"
00025 ** in, for example, the about-box or help/startup screen.
00026 **
00027 ** Commercial non-GPL licensing of this software is possible.
00028 ** For more info contact Ahead Software through [email protected].
00029 **
00030 ** $Id: ps_dec.c,v 1.3 2005/11/01 21:41:43 gabest Exp $
00031 **/
00032 
00033 #include "common.h"
00034 
00035 #ifdef PS_DEC
00036 
00037 #include <stdlib.h>
00038 #include "ps_dec.h"
00039 #include "ps_tables.h"
00040 
00041 /* constants */
00042 #define NEGATE_IPD_MASK            (0x1000)
00043 #define DECAY_SLOPE                FRAC_CONST(0.05)
00044 #define COEF_SQRT2                 COEF_CONST(1.4142135623731)
00045 
00046 /* tables */
00047 /* filters are mirrored in coef 6, second half left out */
00048 static const real_t p8_13_20[7] =
00049 {
00050     FRAC_CONST(0.00746082949812),
00051     FRAC_CONST(0.02270420949825),
00052     FRAC_CONST(0.04546865930473),
00053     FRAC_CONST(0.07266113929591),
00054     FRAC_CONST(0.09885108575264),
00055     FRAC_CONST(0.11793710567217),
00056     FRAC_CONST(0.125)
00057 };
00058 
00059 static const real_t p2_13_20[7] =
00060 {
00061     FRAC_CONST(0.0),
00062     FRAC_CONST(0.01899487526049),
00063     FRAC_CONST(0.0),
00064     FRAC_CONST(-0.07293139167538),
00065     FRAC_CONST(0.0),
00066     FRAC_CONST(0.30596630545168),
00067     FRAC_CONST(0.5)
00068 };
00069 
00070 static const real_t p12_13_34[7] =
00071 {
00072     FRAC_CONST(0.04081179924692),
00073     FRAC_CONST(0.03812810994926),
00074     FRAC_CONST(0.05144908135699),
00075     FRAC_CONST(0.06399831151592),
00076     FRAC_CONST(0.07428313801106),
00077     FRAC_CONST(0.08100347892914),
00078     FRAC_CONST(0.08333333333333)
00079 };
00080 
00081 static const real_t p8_13_34[7] =
00082 {
00083     FRAC_CONST(0.01565675600122),
00084     FRAC_CONST(0.03752716391991),
00085     FRAC_CONST(0.05417891378782),
00086     FRAC_CONST(0.08417044116767),
00087     FRAC_CONST(0.10307344158036),
00088     FRAC_CONST(0.12222452249753),
00089     FRAC_CONST(0.125)
00090 };
00091 
00092 static const real_t p4_13_34[7] =
00093 {
00094     FRAC_CONST(-0.05908211155639),
00095     FRAC_CONST(-0.04871498374946),
00096     FRAC_CONST(0.0),
00097     FRAC_CONST(0.07778723915851),
00098     FRAC_CONST(0.16486303567403),
00099     FRAC_CONST(0.23279856662996),
00100     FRAC_CONST(0.25)
00101 };
00102 
00103 #ifdef PARAM_32KHZ
00104 static const uint8_t delay_length_d[2][NO_ALLPASS_LINKS] = {
00105     { 1, 2, 3 } /* d_24kHz */,
00106     { 3, 4, 5 } /* d_48kHz */
00107 };
00108 #else
00109 static const uint8_t delay_length_d[NO_ALLPASS_LINKS] = {
00110     3, 4, 5 /* d_48kHz */
00111 };
00112 #endif
00113 static const real_t filter_a[NO_ALLPASS_LINKS] = { /* a(m) = exp(-d_48kHz(m)/7) */
00114     FRAC_CONST(0.65143905753106),
00115     FRAC_CONST(0.56471812200776),
00116     FRAC_CONST(0.48954165955695)
00117 };
00118 
00119 static const uint8_t group_border20[10+12 + 1] =
00120 {
00121     6, 7, 0, 1, 2, 3, /* 6 subqmf subbands */
00122     9, 8,             /* 2 subqmf subbands */
00123     10, 11,           /* 2 subqmf subbands */
00124     3, 4, 5, 6, 7, 8, 9, 11, 14, 18, 23, 35, 64
00125 };
00126 
00127 static const uint8_t group_border34[32+18 + 1] =
00128 {
00129      0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, /* 12 subqmf subbands */
00130      12, 13, 14, 15, 16, 17, 18, 19,                 /*  8 subqmf subbands */
00131      20, 21, 22, 23,                                 /*  4 subqmf subbands */
00132      24, 25, 26, 27,                                 /*  4 subqmf subbands */
00133      28, 29, 30, 31,                                 /*  4 subqmf subbands */
00134      32-27, 33-27, 34-27, 35-27, 36-27, 37-27, 38-27, 40-27, 42-27, 44-27, 46-27, 48-27, 51-27, 54-27, 57-27, 60-27, 64-27, 68-27, 91-27
00135 };
00136 
00137 static const uint16_t map_group2bk20[10+12] =
00138 {
00139     (NEGATE_IPD_MASK | 1), (NEGATE_IPD_MASK | 0),
00140     0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
00141 };
00142 
00143 static const uint16_t map_group2bk34[32+18] =
00144 {
00145     0,  1,  2,  3,  4,  5,  6,  6,  7, (NEGATE_IPD_MASK | 2), (NEGATE_IPD_MASK | 1), (NEGATE_IPD_MASK | 0),
00146     10, 10, 4,  5,  6,  7,  8,  9,
00147     10, 11, 12, 9,
00148     14, 11, 12, 13,
00149     14, 15, 16, 13,
00150     16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33
00151 };
00152 
00153 /* type definitions */
00154 typedef struct
00155 {
00156     uint8_t frame_len;
00157     uint8_t resolution20[3];
00158     uint8_t resolution34[5];
00159 
00160     qmf_t *work;
00161     qmf_t **buffer;
00162     qmf_t **temp;
00163 } hyb_info;
00164 
00165 /* static function declarations */
00166 static void ps_data_decode(ps_info *ps);
00167 static hyb_info *hybrid_init();
00168 static void channel_filter2(hyb_info *hyb, uint8_t frame_len, const real_t *filter,
00169                             qmf_t *buffer, qmf_t **X_hybrid);
00170 static void INLINE DCT3_4_unscaled(real_t *y, real_t *x);
00171 static void channel_filter8(hyb_info *hyb, uint8_t frame_len, const real_t *filter,
00172                             qmf_t *buffer, qmf_t **X_hybrid);
00173 static void hybrid_analysis(hyb_info *hyb, qmf_t X[32][64], qmf_t X_hybrid[32][32],
00174                             uint8_t use34);
00175 static void hybrid_synthesis(hyb_info *hyb, qmf_t X[32][64], qmf_t X_hybrid[32][32],
00176                              uint8_t use34);
00177 static int8_t delta_clip(int8_t i, int8_t min, int8_t max);
00178 static void delta_decode(uint8_t enable, int8_t *index, int8_t *index_prev,
00179                          uint8_t dt_flag, uint8_t nr_par, uint8_t stride,
00180                          int8_t min_index, int8_t max_index);
00181 static void delta_modulo_decode(uint8_t enable, int8_t *index, int8_t *index_prev,
00182                                 uint8_t dt_flag, uint8_t nr_par, uint8_t stride,
00183                                 int8_t and_modulo);
00184 static void map20indexto34(int8_t *index, uint8_t bins);
00185 #ifdef PS_LOW_POWER
00186 static void map34indexto20(int8_t *index, uint8_t bins);
00187 #endif
00188 static void ps_data_decode(ps_info *ps);
00189 static void ps_decorrelate(ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64],
00190                            qmf_t X_hybrid_left[32][32], qmf_t X_hybrid_right[32][32]);
00191 static void ps_mix_phase(ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64],
00192                          qmf_t X_hybrid_left[32][32], qmf_t X_hybrid_right[32][32]);
00193 
00194 /*  */
00195 
00196 
00197 static hyb_info *hybrid_init()
00198 {
00199     uint8_t i;
00200 
00201     hyb_info *hyb = (hyb_info*)faad_malloc(sizeof(hyb_info));
00202 
00203     hyb->resolution34[0] = 12;
00204     hyb->resolution34[1] = 8;
00205     hyb->resolution34[2] = 4;
00206     hyb->resolution34[3] = 4;
00207     hyb->resolution34[4] = 4;
00208 
00209     hyb->resolution20[0] = 8;
00210     hyb->resolution20[1] = 2;
00211     hyb->resolution20[2] = 2;
00212 
00213     hyb->frame_len = 32;
00214 
00215     hyb->work = (qmf_t*)faad_malloc((hyb->frame_len+12) * sizeof(qmf_t));
00216     memset(hyb->work, 0, (hyb->frame_len+12) * sizeof(qmf_t));
00217 
00218     hyb->buffer = (qmf_t**)faad_malloc(5 * sizeof(qmf_t*));
00219     for (i = 0; i < 5; i++)
00220     {
00221         hyb->buffer[i] = (qmf_t*)faad_malloc(hyb->frame_len * sizeof(qmf_t));
00222         memset(hyb->buffer[i], 0, hyb->frame_len * sizeof(qmf_t));
00223     }
00224 
00225     hyb->temp = (qmf_t**)faad_malloc(hyb->frame_len * sizeof(qmf_t*));
00226     for (i = 0; i < hyb->frame_len; i++)
00227     {
00228         hyb->temp[i] = (qmf_t*)faad_malloc(12 /*max*/ * sizeof(qmf_t));
00229     }
00230 
00231     return hyb;
00232 }
00233 
00234 static void hybrid_free(hyb_info *hyb)
00235 {
00236     uint8_t i;
00237 
00238     if (hyb->work)
00239         faad_free(hyb->work);
00240 
00241     for (i = 0; i < 5; i++)
00242     {
00243         if (hyb->buffer[i])
00244             faad_free(hyb->buffer[i]);
00245     }
00246     if (hyb->buffer)
00247         faad_free(hyb->buffer);
00248 
00249     for (i = 0; i < hyb->frame_len; i++)
00250     {
00251         if (hyb->temp[i])
00252             faad_free(hyb->temp[i]);
00253     }
00254     if (hyb->temp)
00255         faad_free(hyb->temp);
00256 }
00257 
00258 /* real filter, size 2 */
00259 static void channel_filter2(hyb_info *hyb, uint8_t frame_len, const real_t *filter,
00260                             qmf_t *buffer, qmf_t **X_hybrid)
00261 {
00262     uint8_t i;
00263 
00264     for (i = 0; i < frame_len; i++)
00265     {
00266         real_t r0 = MUL_F(filter[0],(QMF_RE(buffer[0+i]) + QMF_RE(buffer[12+i])));
00267         real_t r1 = MUL_F(filter[1],(QMF_RE(buffer[1+i]) + QMF_RE(buffer[11+i])));
00268         real_t r2 = MUL_F(filter[2],(QMF_RE(buffer[2+i]) + QMF_RE(buffer[10+i])));
00269         real_t r3 = MUL_F(filter[3],(QMF_RE(buffer[3+i]) + QMF_RE(buffer[9+i])));
00270         real_t r4 = MUL_F(filter[4],(QMF_RE(buffer[4+i]) + QMF_RE(buffer[8+i])));
00271         real_t r5 = MUL_F(filter[5],(QMF_RE(buffer[5+i]) + QMF_RE(buffer[7+i])));
00272         real_t r6 = MUL_F(filter[6],QMF_RE(buffer[6+i]));
00273         real_t i0 = MUL_F(filter[0],(QMF_IM(buffer[0+i]) + QMF_IM(buffer[12+i])));
00274         real_t i1 = MUL_F(filter[1],(QMF_IM(buffer[1+i]) + QMF_IM(buffer[11+i])));
00275         real_t i2 = MUL_F(filter[2],(QMF_IM(buffer[2+i]) + QMF_IM(buffer[10+i])));
00276         real_t i3 = MUL_F(filter[3],(QMF_IM(buffer[3+i]) + QMF_IM(buffer[9+i])));
00277         real_t i4 = MUL_F(filter[4],(QMF_IM(buffer[4+i]) + QMF_IM(buffer[8+i])));
00278         real_t i5 = MUL_F(filter[5],(QMF_IM(buffer[5+i]) + QMF_IM(buffer[7+i])));
00279         real_t i6 = MUL_F(filter[6],QMF_IM(buffer[6+i]));
00280 
00281         /* q = 0 */
00282         QMF_RE(X_hybrid[i][0]) = r0 + r1 + r2 + r3 + r4 + r5 + r6;
00283         QMF_IM(X_hybrid[i][0]) = i0 + i1 + i2 + i3 + i4 + i5 + i6;
00284 
00285         /* q = 1 */
00286         QMF_RE(X_hybrid[i][1]) = r0 - r1 + r2 - r3 + r4 - r5 + r6;
00287         QMF_IM(X_hybrid[i][1]) = i0 - i1 + i2 - i3 + i4 - i5 + i6;
00288     }
00289 }
00290 
00291 /* complex filter, size 4 */
00292 static void channel_filter4(hyb_info *hyb, uint8_t frame_len, const real_t *filter,
00293                             qmf_t *buffer, qmf_t **X_hybrid)
00294 {
00295     uint8_t i;
00296     real_t input_re1[2], input_re2[2], input_im1[2], input_im2[2];
00297 
00298     for (i = 0; i < frame_len; i++)
00299     {
00300         input_re1[0] = -MUL_F(filter[2], (QMF_RE(buffer[i+2]) + QMF_RE(buffer[i+10]))) +
00301             MUL_F(filter[6], QMF_RE(buffer[i+6]));
00302         input_re1[1] = MUL_F(FRAC_CONST(-0.70710678118655),
00303             (MUL_F(filter[1], (QMF_RE(buffer[i+1]) + QMF_RE(buffer[i+11]))) +
00304             MUL_F(filter[3], (QMF_RE(buffer[i+3]) + QMF_RE(buffer[i+9]))) -
00305             MUL_F(filter[5], (QMF_RE(buffer[i+5]) + QMF_RE(buffer[i+7])))));
00306 
00307         input_im1[0] = MUL_F(filter[0], (QMF_IM(buffer[i+0]) - QMF_IM(buffer[i+12]))) -
00308             MUL_F(filter[4], (QMF_IM(buffer[i+4]) - QMF_IM(buffer[i+8])));
00309         input_im1[1] = MUL_F(FRAC_CONST(0.70710678118655),
00310             (MUL_F(filter[1], (QMF_IM(buffer[i+1]) - QMF_IM(buffer[i+11]))) -
00311             MUL_F(filter[3], (QMF_IM(buffer[i+3]) - QMF_IM(buffer[i+9]))) -
00312             MUL_F(filter[5], (QMF_IM(buffer[i+5]) - QMF_IM(buffer[i+7])))));
00313 
00314         input_re2[0] = MUL_F(filter[0], (QMF_RE(buffer[i+0]) - QMF_RE(buffer[i+12]))) -
00315             MUL_F(filter[4], (QMF_RE(buffer[i+4]) - QMF_RE(buffer[i+8])));
00316         input_re2[1] = MUL_F(FRAC_CONST(0.70710678118655),
00317             (MUL_F(filter[1], (QMF_RE(buffer[i+1]) - QMF_RE(buffer[i+11]))) -
00318             MUL_F(filter[3], (QMF_RE(buffer[i+3]) - QMF_RE(buffer[i+9]))) -
00319             MUL_F(filter[5], (QMF_RE(buffer[i+5]) - QMF_RE(buffer[i+7])))));
00320 
00321         input_im2[0] = -MUL_F(filter[2], (QMF_IM(buffer[i+2]) + QMF_IM(buffer[i+10]))) +
00322             MUL_F(filter[6], QMF_IM(buffer[i+6]));
00323         input_im2[1] = MUL_F(FRAC_CONST(-0.70710678118655),
00324             (MUL_F(filter[1], (QMF_IM(buffer[i+1]) + QMF_IM(buffer[i+11]))) +
00325             MUL_F(filter[3], (QMF_IM(buffer[i+3]) + QMF_IM(buffer[i+9]))) -
00326             MUL_F(filter[5], (QMF_IM(buffer[i+5]) + QMF_IM(buffer[i+7])))));
00327 
00328         /* q == 0 */
00329         QMF_RE(X_hybrid[i][0]) =  input_re1[0] + input_re1[1] + input_im1[0] + input_im1[1];
00330         QMF_IM(X_hybrid[i][0]) = -input_re2[0] - input_re2[1] + input_im2[0] + input_im2[1];
00331 
00332         /* q == 1 */
00333         QMF_RE(X_hybrid[i][1]) =  input_re1[0] - input_re1[1] - input_im1[0] + input_im1[1];
00334         QMF_IM(X_hybrid[i][1]) =  input_re2[0] - input_re2[1] + input_im2[0] - input_im2[1];
00335 
00336         /* q == 2 */
00337         QMF_RE(X_hybrid[i][2]) =  input_re1[0] - input_re1[1] + input_im1[0] - input_im1[1];
00338         QMF_IM(X_hybrid[i][2]) = -input_re2[0] + input_re2[1] + input_im2[0] - input_im2[1];
00339 
00340         /* q == 3 */
00341         QMF_RE(X_hybrid[i][3]) =  input_re1[0] + input_re1[1] - input_im1[0] - input_im1[1];
00342         QMF_IM(X_hybrid[i][3]) =  input_re2[0] + input_re2[1] + input_im2[0] + input_im2[1];
00343     }
00344 }
00345 
00346 static void INLINE DCT3_4_unscaled(real_t *y, real_t *x)
00347 {
00348     real_t f0, f1, f2, f3, f4, f5, f6, f7, f8;
00349 
00350     f0 = MUL_F(x[2], FRAC_CONST(0.7071067811865476));
00351     f1 = x[0] - f0;
00352     f2 = x[0] + f0;
00353     f3 = x[1] + x[3];
00354     f4 = MUL_C(x[1], COEF_CONST(1.3065629648763766));
00355     f5 = MUL_F(f3, FRAC_CONST(-0.9238795325112866));
00356     f6 = MUL_F(x[3], FRAC_CONST(-0.5411961001461967));
00357     f7 = f4 + f5;
00358     f8 = f6 - f5;
00359     y[3] = f2 - f8;
00360     y[0] = f2 + f8;
00361     y[2] = f1 - f7;
00362     y[1] = f1 + f7;
00363 }
00364 
00365 /* complex filter, size 8 */
00366 static void channel_filter8(hyb_info *hyb, uint8_t frame_len, const real_t *filter,
00367                             qmf_t *buffer, qmf_t **X_hybrid)
00368 {
00369     uint8_t i, n;
00370     real_t input_re1[4], input_re2[4], input_im1[4], input_im2[4];
00371     real_t x[4];
00372 
00373     for (i = 0; i < frame_len; i++)
00374     {
00375         input_re1[0] =  MUL_F(filter[6],QMF_RE(buffer[6+i]));
00376         input_re1[1] =  MUL_F(filter[5],(QMF_RE(buffer[5+i]) + QMF_RE(buffer[7+i])));
00377         input_re1[2] = -MUL_F(filter[0],(QMF_RE(buffer[0+i]) + QMF_RE(buffer[12+i]))) + MUL_F(filter[4],(QMF_RE(buffer[4+i]) + QMF_RE(buffer[8+i])));
00378         input_re1[3] = -MUL_F(filter[1],(QMF_RE(buffer[1+i]) + QMF_RE(buffer[11+i]))) + MUL_F(filter[3],(QMF_RE(buffer[3+i]) + QMF_RE(buffer[9+i])));
00379 
00380         input_im1[0] = MUL_F(filter[5],(QMF_IM(buffer[7+i]) - QMF_IM(buffer[5+i])));
00381         input_im1[1] = MUL_F(filter[0],(QMF_IM(buffer[12+i]) - QMF_IM(buffer[0+i]))) + MUL_F(filter[4],(QMF_IM(buffer[8+i]) - QMF_IM(buffer[4+i])));
00382         input_im1[2] = MUL_F(filter[1],(QMF_IM(buffer[11+i]) - QMF_IM(buffer[1+i]))) + MUL_F(filter[3],(QMF_IM(buffer[9+i]) - QMF_IM(buffer[3+i])));
00383         input_im1[3] = MUL_F(filter[2],(QMF_IM(buffer[10+i]) - QMF_IM(buffer[2+i])));
00384 
00385         for (n = 0; n < 4; n++)
00386         {
00387             x[n] = input_re1[n] - input_im1[3-n];
00388         }
00389         DCT3_4_unscaled(x, x);
00390         QMF_RE(X_hybrid[i][7]) = x[0];
00391         QMF_RE(X_hybrid[i][5]) = x[2];
00392         QMF_RE(X_hybrid[i][3]) = x[3];
00393         QMF_RE(X_hybrid[i][1]) = x[1];
00394 
00395         for (n = 0; n < 4; n++)
00396         {
00397             x[n] = input_re1[n] + input_im1[3-n];
00398         }
00399         DCT3_4_unscaled(x, x);
00400         QMF_RE(X_hybrid[i][6]) = x[1];
00401         QMF_RE(X_hybrid[i][4]) = x[3];
00402         QMF_RE(X_hybrid[i][2]) = x[2];
00403         QMF_RE(X_hybrid[i][0]) = x[0];
00404 
00405         input_im2[0] =  MUL_F(filter[6],QMF_IM(buffer[6+i]));
00406         input_im2[1] =  MUL_F(filter[5],(QMF_IM(buffer[5+i]) + QMF_IM(buffer[7+i])));
00407         input_im2[2] = -MUL_F(filter[0],(QMF_IM(buffer[0+i]) + QMF_IM(buffer[12+i]))) + MUL_F(filter[4],(QMF_IM(buffer[4+i]) + QMF_IM(buffer[8+i])));
00408         input_im2[3] = -MUL_F(filter[1],(QMF_IM(buffer[1+i]) + QMF_IM(buffer[11+i]))) + MUL_F(filter[3],(QMF_IM(buffer[3+i]) + QMF_IM(buffer[9+i])));
00409 
00410         input_re2[0] = MUL_F(filter[5],(QMF_RE(buffer[7+i]) - QMF_RE(buffer[5+i])));
00411         input_re2[1] = MUL_F(filter[0],(QMF_RE(buffer[12+i]) - QMF_RE(buffer[0+i]))) + MUL_F(filter[4],(QMF_RE(buffer[8+i]) - QMF_RE(buffer[4+i])));
00412         input_re2[2] = MUL_F(filter[1],(QMF_RE(buffer[11+i]) - QMF_RE(buffer[1+i]))) + MUL_F(filter[3],(QMF_RE(buffer[9+i]) - QMF_RE(buffer[3+i])));
00413         input_re2[3] = MUL_F(filter[2],(QMF_RE(buffer[10+i]) - QMF_RE(buffer[2+i])));
00414 
00415         for (n = 0; n < 4; n++)
00416         {
00417             x[n] = input_im2[n] + input_re2[3-n];
00418         }
00419         DCT3_4_unscaled(x, x);
00420         QMF_IM(X_hybrid[i][7]) = x[0];
00421         QMF_IM(X_hybrid[i][5]) = x[2];
00422         QMF_IM(X_hybrid[i][3]) = x[3];
00423         QMF_IM(X_hybrid[i][1]) = x[1];
00424 
00425         for (n = 0; n < 4; n++)
00426         {
00427             x[n] = input_im2[n] - input_re2[3-n];
00428         }
00429         DCT3_4_unscaled(x, x);
00430         QMF_IM(X_hybrid[i][6]) = x[1];
00431         QMF_IM(X_hybrid[i][4]) = x[3];
00432         QMF_IM(X_hybrid[i][2]) = x[2];
00433         QMF_IM(X_hybrid[i][0]) = x[0];
00434     }
00435 }
00436 
00437 static void INLINE DCT3_6_unscaled(real_t *y, real_t *x)
00438 {
00439     real_t f0, f1, f2, f3, f4, f5, f6, f7;
00440 
00441     f0 = MUL_F(x[3], FRAC_CONST(0.70710678118655));
00442     f1 = x[0] + f0;
00443     f2 = x[0] - f0;
00444     f3 = MUL_F((x[1] - x[5]), FRAC_CONST(0.70710678118655));
00445     f4 = MUL_F(x[2], FRAC_CONST(0.86602540378444)) + MUL_F(x[4], FRAC_CONST(0.5));
00446     f5 = f4 - x[4];
00447     f6 = MUL_F(x[1], FRAC_CONST(0.96592582628907)) + MUL_F(x[5], FRAC_CONST(0.25881904510252));
00448     f7 = f6 - f3;
00449     y[0] = f1 + f6 + f4;
00450     y[1] = f2 + f3 - x[4];
00451     y[2] = f7 + f2 - f5;
00452     y[3] = f1 - f7 - f5;
00453     y[4] = f1 - f3 - x[4];
00454     y[5] = f2 - f6 + f4;
00455 }
00456 
00457 /* complex filter, size 12 */
00458 static void channel_filter12(hyb_info *hyb, uint8_t frame_len, const real_t *filter,
00459                              qmf_t *buffer, qmf_t **X_hybrid)
00460 {
00461     uint8_t i, n;
00462     real_t input_re1[6], input_re2[6], input_im1[6], input_im2[6];
00463     real_t out_re1[6], out_re2[6], out_im1[6], out_im2[6];
00464 
00465     for (i = 0; i < frame_len; i++)
00466     {
00467         for (n = 0; n < 6; n++)
00468         {
00469             if (n == 0)
00470             {
00471                 input_re1[0] = MUL_F(QMF_RE(buffer[6+i]), filter[6]);
00472                 input_re2[0] = MUL_F(QMF_IM(buffer[6+i]), filter[6]);
00473             } else {
00474                 input_re1[6-n] = MUL_F((QMF_RE(buffer[n+i]) + QMF_RE(buffer[12-n+i])), filter[n]);
00475                 input_re2[6-n] = MUL_F((QMF_IM(buffer[n+i]) + QMF_IM(buffer[12-n+i])), filter[n]);
00476             }
00477             input_im2[n] = MUL_F((QMF_RE(buffer[n+i]) - QMF_RE(buffer[12-n+i])), filter[n]);
00478             input_im1[n] = MUL_F((QMF_IM(buffer[n+i]) - QMF_IM(buffer[12-n+i])), filter[n]);
00479         }
00480 
00481         DCT3_6_unscaled(out_re1, input_re1);
00482         DCT3_6_unscaled(out_re2, input_re2);
00483 
00484         DCT3_6_unscaled(out_im1, input_im1);
00485         DCT3_6_unscaled(out_im2, input_im2);
00486 
00487         for (n = 0; n < 6; n += 2)
00488         {
00489             QMF_RE(X_hybrid[i][n]) = out_re1[n] - out_im1[n];
00490             QMF_IM(X_hybrid[i][n]) = out_re2[n] + out_im2[n];
00491             QMF_RE(X_hybrid[i][n+1]) = out_re1[n+1] + out_im1[n+1];
00492             QMF_IM(X_hybrid[i][n+1]) = out_re2[n+1] - out_im2[n+1];
00493 
00494             QMF_RE(X_hybrid[i][10-n]) = out_re1[n+1] - out_im1[n+1];
00495             QMF_IM(X_hybrid[i][10-n]) = out_re2[n+1] + out_im2[n+1];
00496             QMF_RE(X_hybrid[i][11-n]) = out_re1[n] + out_im1[n];
00497             QMF_IM(X_hybrid[i][11-n]) = out_re2[n] - out_im2[n];
00498         }
00499     }
00500 }
00501 
00502 /* Hybrid analysis: further split up QMF subbands
00503  * to improve frequency resolution
00504  */
00505 static void hybrid_analysis(hyb_info *hyb, qmf_t X[32][64], qmf_t X_hybrid[32][32],
00506                             uint8_t use34)
00507 {
00508     uint8_t k, n, band;
00509     uint8_t offset = 0;
00510     uint8_t qmf_bands = (use34) ? 5 : 3;
00511     uint8_t *resolution = (use34) ? hyb->resolution34 : hyb->resolution20;
00512 
00513     for (band = 0; band < qmf_bands; band++)
00514     {
00515         /* build working buffer */
00516         memcpy(hyb->work, hyb->buffer[band], 12 * sizeof(qmf_t));
00517 
00518         /* add new samples */
00519         for (n = 0; n < hyb->frame_len; n++)
00520         {
00521             QMF_RE(hyb->work[12 + n]) = QMF_RE(X[n + 6 /*delay*/][band]);
00522             QMF_IM(hyb->work[12 + n]) = QMF_IM(X[n + 6 /*delay*/][band]);
00523         }
00524 
00525         /* store samples */
00526         memcpy(hyb->buffer[band], hyb->work + hyb->frame_len, 12 * sizeof(qmf_t));
00527 
00528 
00529         switch(resolution[band])
00530         {
00531         case 2:
00532             /* Type B real filter, Q[p] = 2 */
00533             channel_filter2(hyb, hyb->frame_len, p2_13_20, hyb->work, hyb->temp);
00534             break;
00535         case 4:
00536             /* Type A complex filter, Q[p] = 4 */
00537             channel_filter4(hyb, hyb->frame_len, p4_13_34, hyb->work, hyb->temp);
00538             break;
00539         case 8:
00540             /* Type A complex filter, Q[p] = 8 */
00541             channel_filter8(hyb, hyb->frame_len, (use34) ? p8_13_34 : p8_13_20,
00542                 hyb->work, hyb->temp);
00543             break;
00544         case 12:
00545             /* Type A complex filter, Q[p] = 12 */
00546             channel_filter12(hyb, hyb->frame_len, p12_13_34, hyb->work, hyb->temp);
00547             break;
00548         }
00549 
00550         for (n = 0; n < hyb->frame_len; n++)
00551         {
00552             for (k = 0; k < resolution[band]; k++)
00553             {
00554                 QMF_RE(X_hybrid[n][offset + k]) = QMF_RE(hyb->temp[n][k]);
00555                 QMF_IM(X_hybrid[n][offset + k]) = QMF_IM(hyb->temp[n][k]);
00556             }
00557         }
00558         offset += resolution[band];
00559     }
00560 
00561     /* group hybrid channels */
00562     if (!use34)
00563     {
00564         for (n = 0; n < 32 /*30?*/; n++)
00565         {
00566             QMF_RE(X_hybrid[n][3]) += QMF_RE(X_hybrid[n][4]);
00567             QMF_IM(X_hybrid[n][3]) += QMF_IM(X_hybrid[n][4]);
00568             QMF_RE(X_hybrid[n][4]) = 0;
00569             QMF_IM(X_hybrid[n][4]) = 0;
00570 
00571             QMF_RE(X_hybrid[n][2]) += QMF_RE(X_hybrid[n][5]);
00572             QMF_IM(X_hybrid[n][2]) += QMF_IM(X_hybrid[n][5]);
00573             QMF_RE(X_hybrid[n][5]) = 0;
00574             QMF_IM(X_hybrid[n][5]) = 0;
00575         }
00576     }
00577 }
00578 
00579 static void hybrid_synthesis(hyb_info *hyb, qmf_t X[32][64], qmf_t X_hybrid[32][32],
00580                              uint8_t use34)
00581 {
00582     uint8_t k, n, band;
00583     uint8_t offset = 0;
00584     uint8_t qmf_bands = (use34) ? 5 : 3;
00585     uint8_t *resolution = (use34) ? hyb->resolution34 : hyb->resolution20;
00586 
00587     for(band = 0; band < qmf_bands; band++)
00588     {
00589         for (n = 0; n < hyb->frame_len; n++)
00590         {
00591             QMF_RE(X[n][band]) = 0;
00592             QMF_IM(X[n][band]) = 0;
00593 
00594             for (k = 0; k < resolution[band]; k++)
00595             {
00596                 QMF_RE(X[n][band]) += QMF_RE(X_hybrid[n][offset + k]);
00597                 QMF_IM(X[n][band]) += QMF_IM(X_hybrid[n][offset + k]);
00598             }
00599         }
00600         offset += resolution[band];
00601     }
00602 }
00603 
00604 /* limits the value i to the range [min,max] */
00605 static int8_t delta_clip(int8_t i, int8_t min, int8_t max)
00606 {
00607     if (i < min)
00608         return min;
00609     else if (i > max)
00610         return max;
00611     else
00612         return i;
00613 }
00614 
00615 //int iid = 0;
00616 
00617 /* delta decode array */
00618 static void delta_decode(uint8_t enable, int8_t *index, int8_t *index_prev,
00619                          uint8_t dt_flag, uint8_t nr_par, uint8_t stride,
00620                          int8_t min_index, int8_t max_index)
00621 {
00622     int8_t i;
00623 
00624     if (enable == 1)
00625     {
00626         if (dt_flag == 0)
00627         {
00628             /* delta coded in frequency direction */
00629             index[0] = 0 + index[0];
00630             index[0] = delta_clip(index[0], min_index, max_index);
00631 
00632             for (i = 1; i < nr_par; i++)
00633             {
00634                 index[i] = index[i-1] + index[i];
00635                 index[i] = delta_clip(index[i], min_index, max_index);
00636             }
00637         } else {
00638             /* delta coded in time direction */
00639             for (i = 0; i < nr_par; i++)
00640             {
00641                 //int8_t tmp2;
00642                 //int8_t tmp = index[i];
00643 
00644                 //printf("%d %d\n", index_prev[i*stride], index[i]);
00645                 //printf("%d\n", index[i]);
00646 
00647                 index[i] = index_prev[i*stride] + index[i];
00648                 //tmp2 = index[i];
00649                 index[i] = delta_clip(index[i], min_index, max_index);
00650 
00651                 //if (iid)
00652                 //{
00653                 //    if (index[i] == 7)
00654                 //    {
00655                 //        printf("%d %d %d\n", index_prev[i*stride], tmp, tmp2);
00656                 //    }
00657                 //}
00658             }
00659         }
00660     } else {
00661         /* set indices to zero */
00662         for (i = 0; i < nr_par; i++)
00663         {
00664             index[i] = 0;
00665         }
00666     }
00667 
00668     /* coarse */
00669     if (stride == 2)
00670     {
00671         for (i = (nr_par<<1)-1; i > 0; i--)
00672         {
00673             index[i] = index[i>>1];
00674         }
00675     }
00676 }
00677 
00678 /* delta modulo decode array */
00679 /* in: log2 value of the modulo value to allow using AND instead of MOD */
00680 static void delta_modulo_decode(uint8_t enable, int8_t *index, int8_t *index_prev,
00681                                 uint8_t dt_flag, uint8_t nr_par, uint8_t stride,
00682                                 int8_t and_modulo)
00683 {
00684     int8_t i;
00685 
00686     if (enable == 1)
00687     {
00688         if (dt_flag == 0)
00689         {
00690             /* delta coded in frequency direction */
00691             index[0] = 0 + index[0];
00692             index[0] &= and_modulo;
00693 
00694             for (i = 1; i < nr_par; i++)
00695             {
00696                 index[i] = index[i-1] + index[i];
00697                 index[i] &= and_modulo;
00698             }
00699         } else {
00700             /* delta coded in time direction */
00701             for (i = 0; i < nr_par; i++)
00702             {
00703                 index[i] = index_prev[i*stride] + index[i];
00704                 index[i] &= and_modulo;
00705             }
00706         }
00707     } else {
00708         /* set indices to zero */
00709         for (i = 0; i < nr_par; i++)
00710         {
00711             index[i] = 0;
00712         }
00713     }
00714 
00715     /* coarse */
00716     if (stride == 2)
00717     {
00718         index[0] = 0;
00719         for (i = (nr_par<<1)-1; i > 0; i--)
00720         {
00721             index[i] = index[i>>1];
00722         }
00723     }
00724 }
00725 
00726 #ifdef PS_LOW_POWER
00727 static void map34indexto20(int8_t *index, uint8_t bins)
00728 {
00729     index[0] = (2*index[0]+index[1])/3;
00730     index[1] = (index[1]+2*index[2])/3;
00731     index[2] = (2*index[3]+index[4])/3;
00732     index[3] = (index[4]+2*index[5])/3;
00733     index[4] = (index[6]+index[7])/2;
00734     index[5] = (index[8]+index[9])/2;
00735     index[6] = index[10];
00736     index[7] = index[11];
00737     index[8] = (index[12]+index[13])/2;
00738     index[9] = (index[14]+index[15])/2;
00739     index[10] = index[16];
00740 
00741     if (bins == 34)
00742     {
00743         index[11] = index[17];
00744         index[12] = index[18];
00745         index[13] = index[19];
00746         index[14] = (index[20]+index[21])/2;
00747         index[15] = (index[22]+index[23])/2;
00748         index[16] = (index[24]+index[25])/2;
00749         index[17] = (index[26]+index[27])/2;
00750         index[18] = (index[28]+index[29]+index[30]+index[31])/4;
00751         index[19] = (index[32]+index[33])/2;
00752     }
00753 }
00754 #endif
00755 
00756 static void map20indexto34(int8_t *index, uint8_t bins)
00757 {
00758     index[0] = index[0];
00759     index[1] = (index[0] + index[1])/2;
00760     index[2] = index[1];
00761     index[3] = index[2];
00762     index[4] = (index[2] + index[3])/2;
00763     index[5] = index[3];
00764     index[6] = index[4];
00765     index[7] = index[4];
00766     index[8] = index[5];
00767     index[9] = index[5];
00768     index[10] = index[6];
00769     index[11] = index[7];
00770     index[12] = index[8];
00771     index[13] = index[8];
00772     index[14] = index[9];
00773     index[15] = index[9];
00774     index[16] = index[10];
00775 
00776     if (bins == 34)
00777     {
00778         index[17] = index[11];
00779         index[18] = index[12];
00780         index[19] = index[13];
00781         index[20] = index[14];
00782         index[21] = index[14];
00783         index[22] = index[15];
00784         index[23] = index[15];
00785         index[24] = index[16];
00786         index[25] = index[16];
00787         index[26] = index[17];
00788         index[27] = index[17];
00789         index[28] = index[18];
00790         index[29] = index[18];
00791         index[30] = index[18];
00792         index[31] = index[18];
00793         index[32] = index[19];
00794         index[33] = index[19];
00795     }
00796 }
00797 
00798 /* parse the bitstream data decoded in ps_data() */
00799 static void ps_data_decode(ps_info *ps)
00800 {
00801     uint8_t env, bin;
00802 
00803     /* ps data not available, use data from previous frame */
00804     if (ps->ps_data_available == 0)
00805     {
00806         ps->num_env = 0;
00807     }
00808 
00809     for (env = 0; env < ps->num_env; env++)
00810     {
00811         int8_t *iid_index_prev;
00812         int8_t *icc_index_prev;
00813         int8_t *ipd_index_prev;
00814         int8_t *opd_index_prev;
00815 
00816         int8_t num_iid_steps = (ps->iid_mode < 3) ? 7 : 15 /*fine quant*/;
00817 
00818         if (env == 0)
00819         {
00820             /* take last envelope from previous frame */
00821             iid_index_prev = ps->iid_index_prev;
00822             icc_index_prev = ps->icc_index_prev;
00823             ipd_index_prev = ps->ipd_index_prev;
00824             opd_index_prev = ps->opd_index_prev;
00825         } else {
00826             /* take index values from previous envelope */
00827             iid_index_prev = ps->iid_index[env - 1];
00828             icc_index_prev = ps->icc_index[env - 1];
00829             ipd_index_prev = ps->ipd_index[env - 1];
00830             opd_index_prev = ps->opd_index[env - 1];
00831         }
00832 
00833 //        iid = 1;
00834         /* delta decode iid parameters */
00835         delta_decode(ps->enable_iid, ps->iid_index[env], iid_index_prev,
00836             ps->iid_dt[env], ps->nr_iid_par,
00837             (ps->iid_mode == 0 || ps->iid_mode == 3) ? 2 : 1,
00838             -num_iid_steps, num_iid_steps);
00839 //        iid = 0;
00840 
00841         /* delta decode icc parameters */
00842         delta_decode(ps->enable_icc, ps->icc_index[env], icc_index_prev,
00843             ps->icc_dt[env], ps->nr_icc_par,
00844             (ps->icc_mode == 0 || ps->icc_mode == 3) ? 2 : 1,
00845             0, 7);
00846 
00847         /* delta modulo decode ipd parameters */
00848         delta_modulo_decode(ps->enable_ipdopd, ps->ipd_index[env], ipd_index_prev,
00849             ps->ipd_dt[env], ps->nr_ipdopd_par, 1, 7);
00850 
00851         /* delta modulo decode opd parameters */
00852         delta_modulo_decode(ps->enable_ipdopd, ps->opd_index[env], opd_index_prev,
00853             ps->opd_dt[env], ps->nr_ipdopd_par, 1, 7);
00854     }
00855 
00856     /* handle error case */
00857     if (ps->num_env == 0)
00858     {
00859         /* force to 1 */
00860         ps->num_env = 1;
00861 
00862         if (ps->enable_iid)
00863         {
00864             for (bin = 0; bin < 34; bin++)
00865                 ps->iid_index[0][bin] = ps->iid_index_prev[bin];
00866         } else {
00867             for (bin = 0; bin < 34; bin++)
00868                 ps->iid_index[0][bin] = 0;
00869         }
00870 
00871         if (ps->enable_icc)
00872         {
00873             for (bin = 0; bin < 34; bin++)
00874                 ps->icc_index[0][bin] = ps->icc_index_prev[bin];
00875         } else {
00876             for (bin = 0; bin < 34; bin++)
00877                 ps->icc_index[0][bin] = 0;
00878         }
00879 
00880         if (ps->enable_ipdopd)
00881         {
00882             for (bin = 0; bin < 17; bin++)
00883             {
00884                 ps->ipd_index[0][bin] = ps->ipd_index_prev[bin];
00885                 ps->opd_index[0][bin] = ps->opd_index_prev[bin];
00886             }
00887         } else {
00888             for (bin = 0; bin < 17; bin++)
00889             {
00890                 ps->ipd_index[0][bin] = 0;
00891                 ps->opd_index[0][bin] = 0;
00892             }
00893         }
00894     }
00895 
00896     /* update previous indices */
00897     for (bin = 0; bin < 34; bin++)
00898         ps->iid_index_prev[bin] = ps->iid_index[ps->num_env-1][bin];
00899     for (bin = 0; bin < 34; bin++)
00900         ps->icc_index_prev[bin] = ps->icc_index[ps->num_env-1][bin];
00901     for (bin = 0; bin < 17; bin++)
00902     {
00903         ps->ipd_index_prev[bin] = ps->ipd_index[ps->num_env-1][bin];
00904         ps->opd_index_prev[bin] = ps->opd_index[ps->num_env-1][bin];
00905     }
00906 
00907     ps->ps_data_available = 0;
00908 
00909     if (ps->frame_class == 0)
00910     {
00911         ps->border_position[0] = 0;
00912         for (env = 1; env < ps->num_env; env++)
00913         {
00914             ps->border_position[env] = (env * 32 /* 30 for 960? */) / ps->num_env;
00915         }
00916         ps->border_position[ps->num_env] = 32 /* 30 for 960? */;
00917     } else {
00918         ps->border_position[0] = 0;
00919 
00920         if (ps->border_position[ps->num_env] < 32 /* 30 for 960? */)
00921         {
00922             ps->num_env++;
00923             ps->border_position[ps->num_env] = 32 /* 30 for 960? */;
00924             for (bin = 0; bin < 34; bin++)
00925             {
00926                 ps->iid_index[ps->num_env][bin] = ps->iid_index[ps->num_env-1][bin];
00927                 ps->icc_index[ps->num_env][bin] = ps->icc_index[ps->num_env-1][bin];
00928             }
00929             for (bin = 0; bin < 17; bin++)
00930             {
00931                 ps->ipd_index[ps->num_env][bin] = ps->ipd_index[ps->num_env-1][bin];
00932                 ps->opd_index[ps->num_env][bin] = ps->opd_index[ps->num_env-1][bin];
00933             }
00934         }
00935 
00936         for (env = 1; env < ps->num_env; env++)
00937         {
00938             int8_t thr = 32 /* 30 for 960? */ - (ps->num_env - env);
00939 
00940             if (ps->border_position[env] > thr)
00941             {
00942                 ps->border_position[env] = thr;
00943             } else {
00944                 thr = ps->border_position[env-1]+1;
00945                 if (ps->border_position[env] < thr)
00946                 {
00947                     ps->border_position[env] = thr;
00948                 }
00949             }
00950         }
00951     }
00952 
00953     /* make sure that the indices of all parameters can be mapped
00954      * to the same hybrid synthesis filterbank
00955      */
00956 #ifdef PS_LOW_POWER
00957     for (env = 0; env < ps->num_env; env++)
00958     {
00959         if (ps->iid_mode == 2 || ps->iid_mode == 5)
00960             map34indexto20(ps->iid_index[env], 34);
00961         if (ps->icc_mode == 2 || ps->icc_mode == 5)
00962             map34indexto20(ps->icc_index[env], 34);
00963 
00964         /* disable ipd/opd */
00965         for (bin = 0; bin < 17; bin++)
00966         {
00967             ps->aaIpdIndex[env][bin] = 0;
00968             ps->aaOpdIndex[env][bin] = 0;
00969         }
00970     }
00971 #else
00972     if (ps->use34hybrid_bands)
00973     {
00974         for (env = 0; env < ps->num_env; env++)
00975         {
00976             if (ps->iid_mode != 2 && ps->iid_mode != 5)
00977                 map20indexto34(ps->iid_index[env], 34);
00978             if (ps->icc_mode != 2 && ps->icc_mode != 5)
00979                 map20indexto34(ps->icc_index[env], 34);
00980             if (ps->ipd_mode != 2 && ps->ipd_mode != 5)
00981             {
00982                 map20indexto34(ps->ipd_index[env], 17);
00983                 map20indexto34(ps->opd_index[env], 17);
00984             }
00985         }
00986     }
00987 #endif
00988 
00989 #if 0
00990     for (env = 0; env < ps->num_env; env++)
00991     {
00992         printf("iid[env:%d]:", env);
00993         for (bin = 0; bin < 34; bin++)
00994         {
00995             printf(" %d", ps->iid_index[env][bin]);
00996         }
00997         printf("\n");
00998     }
00999     for (env = 0; env < ps->num_env; env++)
01000     {
01001         printf("icc[env:%d]:", env);
01002         for (bin = 0; bin < 34; bin++)
01003         {
01004             printf(" %d", ps->icc_index[env][bin]);
01005         }
01006         printf("\n");
01007     }
01008     for (env = 0; env < ps->num_env; env++)
01009     {
01010         printf("ipd[env:%d]:", env);
01011         for (bin = 0; bin < 17; bin++)
01012         {
01013             printf(" %d", ps->ipd_index[env][bin]);
01014         }
01015         printf("\n");
01016     }
01017     for (env = 0; env < ps->num_env; env++)
01018     {
01019         printf("opd[env:%d]:", env);
01020         for (bin = 0; bin < 17; bin++)
01021         {
01022             printf(" %d", ps->opd_index[env][bin]);
01023         }
01024         printf("\n");
01025     }
01026     printf("\n");
01027 #endif
01028 }
01029 
01030 /* decorrelate the mono signal using an allpass filter */
01031 static void ps_decorrelate(ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64],
01032                            qmf_t X_hybrid_left[32][32], qmf_t X_hybrid_right[32][32])
01033 {
01034     uint8_t gr, n, m, bk;
01035     uint8_t temp_delay;
01036     uint8_t sb, maxsb;
01037     const complex_t *Phi_Fract_SubQmf;
01038     uint8_t temp_delay_ser[NO_ALLPASS_LINKS];
01039     real_t P_SmoothPeakDecayDiffNrg, nrg;
01040     real_t P[32][34];
01041     real_t G_TransientRatio[32][34] = {{0}};
01042     complex_t inputLeft;
01043 
01044 
01045     /* chose hybrid filterbank: 20 or 34 band case */
01046     if (ps->use34hybrid_bands)
01047     {
01048         Phi_Fract_SubQmf = Phi_Fract_SubQmf34;
01049     } else{
01050         Phi_Fract_SubQmf = Phi_Fract_SubQmf20;
01051     }
01052 
01053     /* clear the energy values */
01054     for (n = 0; n < 32; n++)
01055     {
01056         for (bk = 0; bk < 34; bk++)
01057         {
01058             P[n][bk] = 0;
01059         }
01060     }
01061 
01062     /* calculate the energy in each parameter band b(k) */
01063     for (gr = 0; gr < ps->num_groups; gr++)
01064     {
01065         /* select the parameter index b(k) to which this group belongs */
01066         bk = (~NEGATE_IPD_MASK) & ps->map_group2bk[gr];
01067 
01068         /* select the upper subband border for this group */
01069         maxsb = (gr < ps->num_hybrid_groups) ? ps->group_border[gr]+1 : ps->group_border[gr+1];
01070 
01071         for (sb = ps->group_border[gr]; sb < maxsb; sb++)
01072         {
01073             for (n = ps->border_position[0]; n < ps->border_position[ps->num_env]; n++)
01074             {
01075 #ifdef FIXED_POINT
01076                 uint32_t in_re, in_im;
01077 #endif
01078 
01079                 /* input from hybrid subbands or QMF subbands */
01080                 if (gr < ps->num_hybrid_groups)
01081                 {
01082                     RE(inputLeft) = QMF_RE(X_hybrid_left[n][sb]);
01083                     IM(inputLeft) = QMF_IM(X_hybrid_left[n][sb]);
01084                 } else {
01085                     RE(inputLeft) = QMF_RE(X_left[n][sb]);
01086                     IM(inputLeft) = QMF_IM(X_left[n][sb]);
01087                 }
01088 
01089                 /* accumulate energy */
01090 #ifdef FIXED_POINT
01091                 /* NOTE: all input is scaled by 2^(-5) because of fixed point QMF
01092                  * meaning that P will be scaled by 2^(-10) compared to floating point version
01093                  */
01094                 in_re = ((abs(RE(inputLeft))+(1<<(REAL_BITS-1)))>>REAL_BITS);
01095                 in_im = ((abs(IM(inputLeft))+(1<<(REAL_BITS-1)))>>REAL_BITS);
01096                 P[n][bk] += in_re*in_re + in_im*in_im;
01097 #else
01098                 P[n][bk] += MUL_R(RE(inputLeft),RE(inputLeft)) + MUL_R(IM(inputLeft),IM(inputLeft));
01099 #endif
01100             }
01101         }
01102     }
01103 
01104 #if 0
01105     for (n = 0; n < 32; n++)
01106     {
01107         for (bk = 0; bk < 34; bk++)
01108         {
01109 #ifdef FIXED_POINT
01110             printf("%d %d: %d\n", n, bk, P[n][bk] /*/(float)REAL_PRECISION*/);
01111 #else
01112             printf("%d %d: %f\n", n, bk, P[n][bk]/1024.0);
01113 #endif
01114         }
01115     }
01116 #endif
01117 
01118     /* calculate transient reduction ratio for each parameter band b(k) */
01119     for (bk = 0; bk < ps->nr_par_bands; bk++)
01120     {
01121         for (n = ps->border_position[0]; n < ps->border_position[ps->num_env]; n++)
01122         {
01123             const real_t gamma = COEF_CONST(1.5);
01124 
01125             ps->P_PeakDecayNrg[bk] = MUL_F(ps->P_PeakDecayNrg[bk], ps->alpha_decay);
01126             if (ps->P_PeakDecayNrg[bk] < P[n][bk])
01127                 ps->P_PeakDecayNrg[bk] = P[n][bk];
01128 
01129             /* apply smoothing filter to peak decay energy */
01130             P_SmoothPeakDecayDiffNrg = ps->P_SmoothPeakDecayDiffNrg_prev[bk];
01131             P_SmoothPeakDecayDiffNrg += MUL_F((ps->P_PeakDecayNrg[bk] - P[n][bk] - ps->P_SmoothPeakDecayDiffNrg_prev[bk]), ps->alpha_smooth);
01132             ps->P_SmoothPeakDecayDiffNrg_prev[bk] = P_SmoothPeakDecayDiffNrg;
01133 
01134             /* apply smoothing filter to energy */
01135             nrg = ps->P_prev[bk];
01136             nrg += MUL_F((P[n][bk] - ps->P_prev[bk]), ps->alpha_smooth);
01137             ps->P_prev[bk] = nrg;
01138 
01139             /* calculate transient ratio */
01140             if (MUL_C(P_SmoothPeakDecayDiffNrg, gamma) <= nrg)
01141             {
01142                 G_TransientRatio[n][bk] = REAL_CONST(1.0);
01143             } else {
01144                 G_TransientRatio[n][bk] = DIV_R(nrg, (MUL_C(P_SmoothPeakDecayDiffNrg, gamma)));
01145             }
01146         }
01147     }
01148 
01149 #if 0
01150     for (n = 0; n < 32; n++)
01151     {
01152         for (bk = 0; bk < 34; bk++)
01153         {
01154 #ifdef FIXED_POINT
01155             printf("%d %d: %f\n", n, bk, G_TransientRatio[n][bk]/(float)REAL_PRECISION);
01156 #else
01157             printf("%d %d: %f\n", n, bk, G_TransientRatio[n][bk]);
01158 #endif
01159         }
01160     }
01161 #endif
01162 
01163     /* apply stereo decorrelation filter to the signal */
01164     for (gr = 0; gr < ps->num_groups; gr++)
01165     {
01166         if (gr < ps->num_hybrid_groups)
01167             maxsb = ps->group_border[gr] + 1;
01168         else
01169             maxsb = ps->group_border[gr + 1];
01170 
01171         /* QMF channel */
01172         for (sb = ps->group_border[gr]; sb < maxsb; sb++)
01173         {
01174             real_t g_DecaySlope;
01175             real_t g_DecaySlope_filt[NO_ALLPASS_LINKS];
01176 
01177             /* g_DecaySlope: [0..1] */
01178             if (gr < ps->num_hybrid_groups || sb <= ps->decay_cutoff)
01179             {
01180                 g_DecaySlope = FRAC_CONST(1.0);
01181             } else {
01182                 int8_t decay = ps->decay_cutoff - sb;
01183                 if (decay <= -20 /* -1/DECAY_SLOPE */)
01184                 {
01185                     g_DecaySlope = 0;
01186                 } else {
01187                     /* decay(int)*decay_slope(frac) = g_DecaySlope(frac) */
01188                     g_DecaySlope = FRAC_CONST(1.0) + DECAY_SLOPE * decay;
01189                 }
01190             }
01191 
01192             /* calculate g_DecaySlope_filt for every m multiplied by filter_a[m] */
01193             for (m = 0; m < NO_ALLPASS_LINKS; m++)
01194             {
01195                 g_DecaySlope_filt[m] = MUL_F(g_DecaySlope, filter_a[m]);
01196             }
01197 
01198 
01199             /* set delay indices */
01200             temp_delay = ps->saved_delay;
01201             for (n = 0; n < NO_ALLPASS_LINKS; n++)
01202                 temp_delay_ser[n] = ps->delay_buf_index_ser[n];
01203 
01204             for (n = ps->border_position[0]; n < ps->border_position[ps->num_env]; n++)
01205             {
01206                 complex_t tmp, tmp0, R0;
01207 
01208                 if (gr < ps->num_hybrid_groups)
01209                 {
01210                     /* hybrid filterbank input */
01211                     RE(inputLeft) = QMF_RE(X_hybrid_left[n][sb]);
01212                     IM(inputLeft) = QMF_IM(X_hybrid_left[n][sb]);
01213                 } else {
01214                     /* QMF filterbank input */
01215                     RE(inputLeft) = QMF_RE(X_left[n][sb]);
01216                     IM(inputLeft) = QMF_IM(X_left[n][sb]);
01217                 }
01218 
01219                 if (sb > ps->nr_allpass_bands && gr >= ps->num_hybrid_groups)
01220                 {
01221                     /* delay */
01222 
01223                     /* never hybrid subbands here, always QMF subbands */
01224                     RE(tmp) = RE(ps->delay_Qmf[ps->delay_buf_index_delay[sb]][sb]);
01225                     IM(tmp) = IM(ps->delay_Qmf[ps->delay_buf_index_delay[sb]][sb]);
01226                     RE(R0) = RE(tmp);
01227                     IM(R0) = IM(tmp);
01228                     RE(ps->delay_Qmf[ps->delay_buf_index_delay[sb]][sb]) = RE(inputLeft);
01229                     IM(ps->delay_Qmf[ps->delay_buf_index_delay[sb]][sb]) = IM(inputLeft);
01230                 } else {
01231                     /* allpass filter */
01232                     uint8_t m;
01233                     complex_t Phi_Fract;
01234 
01235                     /* fetch parameters */
01236                     if (gr < ps->num_hybrid_groups)
01237                     {
01238                         /* select data from the hybrid subbands */
01239                         RE(tmp0) = RE(ps->delay_SubQmf[temp_delay][sb]);
01240                         IM(tmp0) = IM(ps->delay_SubQmf[temp_delay][sb]);
01241 
01242                         RE(ps->delay_SubQmf[temp_delay][sb]) = RE(inputLeft);
01243                         IM(ps->delay_SubQmf[temp_delay][sb]) = IM(inputLeft);
01244 
01245                         RE(Phi_Fract) = RE(Phi_Fract_SubQmf[sb]);
01246                         IM(Phi_Fract) = IM(Phi_Fract_SubQmf[sb]);
01247                     } else {
01248                         /* select data from the QMF subbands */
01249                         RE(tmp0) = RE(ps->delay_Qmf[temp_delay][sb]);
01250                         IM(tmp0) = IM(ps->delay_Qmf[temp_delay][sb]);
01251 
01252                         RE(ps->delay_Qmf[temp_delay][sb]) = RE(inputLeft);
01253                         IM(ps->delay_Qmf[temp_delay][sb]) = IM(inputLeft);
01254 
01255                         RE(Phi_Fract) = RE(Phi_Fract_Qmf[sb]);
01256                         IM(Phi_Fract) = IM(Phi_Fract_Qmf[sb]);
01257                     }
01258 
01259                     /* z^(-2) * Phi_Fract[k] */
01260                     ComplexMult(&RE(tmp), &IM(tmp), RE(tmp0), IM(tmp0), RE(Phi_Fract), IM(Phi_Fract));
01261 
01262                     RE(R0) = RE(tmp);
01263                     IM(R0) = IM(tmp);
01264                     for (m = 0; m < NO_ALLPASS_LINKS; m++)
01265                     {
01266                         complex_t Q_Fract_allpass, tmp2;
01267 
01268                         /* fetch parameters */
01269                         if (gr < ps->num_hybrid_groups)
01270                         {
01271                             /* select data from the hybrid subbands */
01272                             RE(tmp0) = RE(ps->delay_SubQmf_ser[m][temp_delay_ser[m]][sb]);
01273                             IM(tmp0) = IM(ps->delay_SubQmf_ser[m][temp_delay_ser[m]][sb]);
01274 
01275                             if (ps->use34hybrid_bands)
01276                             {
01277                                 RE(Q_Fract_allpass) = RE(Q_Fract_allpass_SubQmf34[sb][m]);
01278                                 IM(Q_Fract_allpass) = IM(Q_Fract_allpass_SubQmf34[sb][m]);
01279                             } else {
01280                                 RE(Q_Fract_allpass) = RE(Q_Fract_allpass_SubQmf20[sb][m]);
01281                                 IM(Q_Fract_allpass) = IM(Q_Fract_allpass_SubQmf20[sb][m]);
01282                             }
01283                         } else {
01284                             /* select data from the QMF subbands */
01285                             RE(tmp0) = RE(ps->delay_Qmf_ser[m][temp_delay_ser[m]][sb]);
01286                             IM(tmp0) = IM(ps->delay_Qmf_ser[m][temp_delay_ser[m]][sb]);
01287 
01288                             RE(Q_Fract_allpass) = RE(Q_Fract_allpass_Qmf[sb][m]);
01289                             IM(Q_Fract_allpass) = IM(Q_Fract_allpass_Qmf[sb][m]);
01290                         }
01291 
01292                         /* delay by a fraction */
01293                         /* z^(-d(m)) * Q_Fract_allpass[k,m] */
01294                         ComplexMult(&RE(tmp), &IM(tmp), RE(tmp0), IM(tmp0), RE(Q_Fract_allpass), IM(Q_Fract_allpass));
01295 
01296                         /* -a(m) * g_DecaySlope[k] */
01297                         RE(tmp) += -MUL_F(g_DecaySlope_filt[m], RE(R0));
01298                         IM(tmp) += -MUL_F(g_DecaySlope_filt[m], IM(R0));
01299 
01300                         /* -a(m) * g_DecaySlope[k] * Q_Fract_allpass[k,m] * z^(-d(m)) */
01301                         RE(tmp2) = RE(R0) + MUL_F(g_DecaySlope_filt[m], RE(tmp));
01302                         IM(tmp2) = IM(R0) + MUL_F(g_DecaySlope_filt[m], IM(tmp));
01303 
01304                         /* store sample */
01305                         if (gr < ps->num_hybrid_groups)
01306                         {
01307                             RE(ps->delay_SubQmf_ser[m][temp_delay_ser[m]][sb]) = RE(tmp2);
01308                             IM(ps->delay_SubQmf_ser[m][temp_delay_ser[m]][sb]) = IM(tmp2);
01309                         } else {
01310                             RE(ps->delay_Qmf_ser[m][temp_delay_ser[m]][sb]) = RE(tmp2);
01311                             IM(ps->delay_Qmf_ser[m][temp_delay_ser[m]][sb]) = IM(tmp2);
01312                         }
01313 
01314                         /* store for next iteration (or as output value if last iteration) */
01315                         RE(R0) = RE(tmp);
01316                         IM(R0) = IM(tmp);
01317                     }
01318                 }
01319 
01320                 /* select b(k) for reading the transient ratio */
01321                 bk = (~NEGATE_IPD_MASK) & ps->map_group2bk[gr];
01322 
01323                 /* duck if a past transient is found */
01324                 RE(R0) = MUL_R(G_TransientRatio[n][bk], RE(R0));
01325                 IM(R0) = MUL_R(G_TransientRatio[n][bk], IM(R0));
01326 
01327                 if (gr < ps->num_hybrid_groups)
01328                 {
01329                     /* hybrid */
01330                     QMF_RE(X_hybrid_right[n][sb]) = RE(R0);
01331                     QMF_IM(X_hybrid_right[n][sb]) = IM(R0);
01332                 } else {
01333                     /* QMF */
01334                     QMF_RE(X_right[n][sb]) = RE(R0);
01335                     QMF_IM(X_right[n][sb]) = IM(R0);
01336                 }
01337 
01338                 /* Update delay buffer index */
01339                 if (++temp_delay >= 2)
01340                 {
01341                     temp_delay = 0;
01342                 }
01343 
01344                 /* update delay indices */
01345                 if (sb > ps->nr_allpass_bands && gr >= ps->num_hybrid_groups)
01346                 {
01347                     /* delay_D depends on the samplerate, it can hold the values 14 and 1 */
01348                     if (++ps->delay_buf_index_delay[sb] >= ps->delay_D[sb])
01349                     {
01350                         ps->delay_buf_index_delay[sb] = 0;
01351                     }
01352                 }
01353 
01354                 for (m = 0; m < NO_ALLPASS_LINKS; m++)
01355                 {
01356                     if (++temp_delay_ser[m] >= ps->num_sample_delay_ser[m])
01357                     {
01358                         temp_delay_ser[m] = 0;
01359                     }
01360                 }
01361             }
01362         }
01363     }
01364 
01365     /* update delay indices */
01366     ps->saved_delay = temp_delay;
01367     for (m = 0; m < NO_ALLPASS_LINKS; m++)
01368         ps->delay_buf_index_ser[m] = temp_delay_ser[m];
01369 }
01370 
01371 #ifdef FIXED_POINT
01372 #define step(shift) \
01373     if ((0x40000000l >> shift) + root <= value)       \
01374     {                                                 \
01375         value -= (0x40000000l >> shift) + root;       \
01376         root = (root >> 1) | (0x40000000l >> shift);  \
01377     } else {                                          \
01378         root = root >> 1;                             \
01379     }
01380 
01381 /* fixed point square root approximation */
01382 static real_t ps_sqrt(real_t value)
01383 {
01384     real_t root = 0;
01385 
01386     step( 0); step( 2); step( 4); step( 6);
01387     step( 8); step(10); step(12); step(14);
01388     step(16); step(18); step(20); step(22);
01389     step(24); step(26); step(28); step(30);
01390 
01391     if (root < value)
01392         ++root;
01393 
01394     root <<= (REAL_BITS/2);
01395 
01396     return root;
01397 }
01398 #else
01399 #define ps_sqrt(A) sqrt(A)
01400 #endif
01401 
01402 static const real_t ipdopd_cos_tab[] = {
01403     FRAC_CONST(1.000000000000000),
01404     FRAC_CONST(0.707106781186548),
01405     FRAC_CONST(0.000000000000000),
01406     FRAC_CONST(-0.707106781186547),
01407     FRAC_CONST(-1.000000000000000),
01408     FRAC_CONST(-0.707106781186548),
01409     FRAC_CONST(-0.000000000000000),
01410     FRAC_CONST(0.707106781186547),
01411     FRAC_CONST(1.000000000000000)
01412 };
01413 
01414 static const real_t ipdopd_sin_tab[] = {
01415     FRAC_CONST(0.000000000000000),
01416     FRAC_CONST(0.707106781186547),
01417     FRAC_CONST(1.000000000000000),
01418     FRAC_CONST(0.707106781186548),
01419     FRAC_CONST(0.000000000000000),
01420     FRAC_CONST(-0.707106781186547),
01421     FRAC_CONST(-1.000000000000000),
01422     FRAC_CONST(-0.707106781186548),
01423     FRAC_CONST(-0.000000000000000)
01424 };
01425 
01426 static real_t magnitude_c(complex_t c)
01427 {
01428 #ifdef FIXED_POINT
01429 #define ps_abs(A) (((A) > 0) ? (A) : (-(A)))
01430 #define ALPHA FRAC_CONST(0.948059448969)
01431 #define BETA  FRAC_CONST(0.392699081699)
01432 
01433     real_t abs_inphase = ps_abs(RE(c));
01434     real_t abs_quadrature = ps_abs(IM(c));
01435 
01436     if (abs_inphase > abs_quadrature) {
01437         return MUL_F(abs_inphase, ALPHA) + MUL_F(abs_quadrature, BETA);
01438     } else {
01439         return MUL_F(abs_quadrature, ALPHA) + MUL_F(abs_inphase, BETA);
01440     }
01441 #else
01442     return sqrt(RE(c)*RE(c) + IM(c)*IM(c));
01443 #endif
01444 }
01445 
01446 static void ps_mix_phase(ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64],
01447                          qmf_t X_hybrid_left[32][32], qmf_t X_hybrid_right[32][32])
01448 {
01449     uint8_t n;
01450     uint8_t gr;
01451     uint8_t bk = 0;
01452     uint8_t sb, maxsb;
01453     uint8_t env;
01454     uint8_t nr_ipdopd_par;
01455     complex_t h11, h12, h21, h22;
01456     complex_t H11, H12, H21, H22;
01457     complex_t deltaH11, deltaH12, deltaH21, deltaH22;
01458     complex_t tempLeft;
01459     complex_t tempRight;
01460     complex_t phaseLeft;
01461     complex_t phaseRight;
01462     real_t L;
01463     const real_t *sf_iid;
01464     uint8_t no_iid_steps;
01465 
01466     if (ps->iid_mode >= 3)
01467     {
01468         no_iid_steps = 15;
01469         sf_iid = sf_iid_fine;
01470     } else {
01471         no_iid_steps = 7;
01472         sf_iid = sf_iid_normal;
01473     }
01474 
01475     if (ps->ipd_mode == 0 || ps->ipd_mode == 3)
01476     {
01477         nr_ipdopd_par = 11; /* resolution */
01478     } else {
01479         nr_ipdopd_par = ps->nr_ipdopd_par;
01480     }
01481 
01482     for (gr = 0; gr < ps->num_groups; gr++)
01483     {
01484         bk = (~NEGATE_IPD_MASK) & ps->map_group2bk[gr];
01485 
01486         /* use one channel per group in the subqmf domain */
01487         maxsb = (gr < ps->num_hybrid_groups) ? ps->group_border[gr] + 1 : ps->group_border[gr + 1];
01488 
01489         for (env = 0; env < ps->num_env; env++)
01490         {
01491             if (ps->icc_mode < 3)
01492             {
01493                 /* type 'A' mixing as described in 8.6.4.6.2.1 */
01494                 real_t c_1, c_2;
01495                 real_t cosa, sina;
01496                 real_t cosb, sinb;
01497                 real_t ab1, ab2;
01498                 real_t ab3, ab4;
01499 
01500                 /*
01501                 c_1 = sqrt(2.0 / (1.0 + pow(10.0, quant_iid[no_iid_steps + iid_index] / 10.0)));
01502                 c_2 = sqrt(2.0 / (1.0 + pow(10.0, quant_iid[no_iid_steps - iid_index] / 10.0)));
01503                 alpha = 0.5 * acos(quant_rho[icc_index]);
01504                 beta = alpha * ( c_1 - c_2 ) / sqrt(2.0);
01505                 */
01506 
01507                 //printf("%d\n", ps->iid_index[env][bk]);
01508 
01509                 /* calculate the scalefactors c_1 and c_2 from the intensity differences */
01510                 c_1 = sf_iid[no_iid_steps + ps->iid_index[env][bk]];
01511                 c_2 = sf_iid[no_iid_steps - ps->iid_index[env][bk]];
01512 
01513                 /* calculate alpha and beta using the ICC parameters */
01514                 cosa = cos_alphas[ps->icc_index[env][bk]];
01515                 sina = sin_alphas[ps->icc_index[env][bk]];
01516 
01517                 if (ps->iid_mode >= 3)
01518                 {
01519                     if (ps->iid_index[env][bk] < 0)
01520                     {
01521                         cosb =  cos_betas_fine[-ps->iid_index[env][bk]][ps->icc_index[env][bk]];
01522                         sinb = -sin_betas_fine[-ps->iid_index[env][bk]][ps->icc_index[env][bk]];
01523                     } else {
01524                         cosb = cos_betas_fine[ps->iid_index[env][bk]][ps->icc_index[env][bk]];
01525                         sinb = sin_betas_fine[ps->iid_index[env][bk]][ps->icc_index[env][bk]];
01526                     }
01527                 } else {
01528                     if (ps->iid_index[env][bk] < 0)
01529                     {
01530                         cosb =  cos_betas_normal[-ps->iid_index[env][bk]][ps->icc_index[env][bk]];
01531                         sinb = -sin_betas_normal[-ps->iid_index[env][bk]][ps->icc_index[env][bk]];
01532                     } else {
01533                         cosb = cos_betas_normal[ps->iid_index[env][bk]][ps->icc_index[env][bk]];
01534                         sinb = sin_betas_normal[ps->iid_index[env][bk]][ps->icc_index[env][bk]];
01535                     }
01536                 }
01537 
01538                 ab1 = MUL_C(cosb, cosa);
01539                 ab2 = MUL_C(sinb, sina);
01540                 ab3 = MUL_C(sinb, cosa);
01541                 ab4 = MUL_C(cosb, sina);
01542 
01543                 /* h_xy: COEF */
01544                 RE(h11) = MUL_C(c_2, (ab1 - ab2));
01545                 RE(h12) = MUL_C(c_1, (ab1 + ab2));
01546                 RE(h21) = MUL_C(c_2, (ab3 + ab4));
01547                 RE(h22) = MUL_C(c_1, (ab3 - ab4));
01548             } else {
01549                 /* type 'B' mixing as described in 8.6.4.6.2.2 */
01550                 real_t sina, cosa;
01551                 real_t cosg, sing;
01552 
01553                 /*
01554                 real_t c, rho, mu, alpha, gamma;
01555                 uint8_t i;
01556 
01557                 i = ps->iid_index[env][bk];
01558                 c = (real_t)pow(10.0, ((i)?(((i>0)?1:-1)*quant_iid[((i>0)?i:-i)-1]):0.)/20.0);
01559                 rho = quant_rho[ps->icc_index[env][bk]];
01560 
01561                 if (rho == 0.0f && c == 1.)
01562                 {
01563                     alpha = (real_t)M_PI/4.0f;
01564                     rho = 0.05f;
01565                 } else {
01566                     if (rho <= 0.05f)
01567                     {
01568                         rho = 0.05f;
01569                     }
01570                     alpha = 0.5f*(real_t)atan( (2.0f*c*rho) / (c*c-1.0f) );
01571 
01572                     if (alpha < 0.)
01573                     {
01574                         alpha += (real_t)M_PI/2.0f;
01575                     }
01576                     if (rho < 0.)
01577                     {
01578                         alpha += (real_t)M_PI;
01579                     }
01580                 }
01581                 mu = c+1.0f/c;
01582                 mu = 1+(4.0f*rho*rho-4.0f)/(mu*mu);
01583                 gamma = (real_t)atan(sqrt((1.0f-sqrt(mu))/(1.0f+sqrt(mu))));
01584                 */
01585 
01586                 if (ps->iid_mode >= 3)
01587                 {
01588                     uint8_t abs_iid = abs(ps->iid_index[env][bk]);
01589 
01590                     cosa = sincos_alphas_B_fine[no_iid_steps + ps->iid_index[env][bk]][ps->icc_index[env][bk]];
01591                     sina = sincos_alphas_B_fine[30 - (no_iid_steps + ps->iid_index[env][bk])][ps->icc_index[env][bk]];
01592                     cosg = cos_gammas_fine[abs_iid][ps->icc_index[env][bk]];
01593                     sing = sin_gammas_fine[abs_iid][ps->icc_index[env][bk]];
01594                 } else {
01595                     uint8_t abs_iid = abs(ps->iid_index[env][bk]);
01596 
01597                     cosa = sincos_alphas_B_normal[no_iid_steps + ps->iid_index[env][bk]][ps->icc_index[env][bk]];
01598                     sina = sincos_alphas_B_normal[14 - (no_iid_steps + ps->iid_index[env][bk])][ps->icc_index[env][bk]];
01599                     cosg = cos_gammas_normal[abs_iid][ps->icc_index[env][bk]];
01600                     sing = sin_gammas_normal[abs_iid][ps->icc_index[env][bk]];
01601                 }
01602 
01603                 RE(h11) = MUL_C(COEF_SQRT2, MUL_C(cosa, cosg));
01604                 RE(h12) = MUL_C(COEF_SQRT2, MUL_C(sina, cosg));
01605                 RE(h21) = MUL_C(COEF_SQRT2, MUL_C(-cosa, sing));
01606                 RE(h22) = MUL_C(COEF_SQRT2, MUL_C(sina, sing));
01607             }
01608 
01609             /* calculate phase rotation parameters H_xy */
01610             /* note that the imaginary part of these parameters are only calculated when
01611                IPD and OPD are enabled
01612              */
01613             if ((ps->enable_ipdopd) && (bk < nr_ipdopd_par))
01614             {
01615                 int8_t i;
01616                 real_t xy, pq, xypq;
01617 
01618                 /* ringbuffer index */
01619                 i = ps->phase_hist;
01620 
01621                 /* previous value */
01622 #ifdef FIXED_POINT
01623                 /* divide by 4, shift right 2 bits */
01624                 RE(tempLeft)  = RE(ps->ipd_prev[bk][i]) >> 2;
01625                 IM(tempLeft)  = IM(ps->ipd_prev[bk][i]) >> 2;
01626                 RE(tempRight) = RE(ps->opd_prev[bk][i]) >> 2;
01627                 IM(tempRight) = IM(ps->opd_prev[bk][i]) >> 2;
01628 #else
01629                 RE(tempLeft)  = MUL_F(RE(ps->ipd_prev[bk][i]), FRAC_CONST(0.25));
01630                 IM(tempLeft)  = MUL_F(IM(ps->ipd_prev[bk][i]), FRAC_CONST(0.25));
01631                 RE(tempRight) = MUL_F(RE(ps->opd_prev[bk][i]), FRAC_CONST(0.25));
01632                 IM(tempRight) = MUL_F(IM(ps->opd_prev[bk][i]), FRAC_CONST(0.25));
01633 #endif
01634 
01635                 /* save current value */
01636                 RE(ps->ipd_prev[bk][i]) = ipdopd_cos_tab[abs(ps->ipd_index[env][bk])];
01637                 IM(ps->ipd_prev[bk][i]) = ipdopd_sin_tab[abs(ps->ipd_index[env][bk])];
01638                 RE(ps->opd_prev[bk][i]) = ipdopd_cos_tab[abs(ps->opd_index[env][bk])];
01639                 IM(ps->opd_prev[bk][i]) = ipdopd_sin_tab[abs(ps->opd_index[env][bk])];
01640 
01641                 /* add current value */
01642                 RE(tempLeft)  += RE(ps->ipd_prev[bk][i]);
01643                 IM(tempLeft)  += IM(ps->ipd_prev[bk][i]);
01644                 RE(tempRight) += RE(ps->opd_prev[bk][i]);
01645                 IM(tempRight) += IM(ps->opd_prev[bk][i]);
01646 
01647                 /* ringbuffer index */
01648                 if (i == 0)
01649                 {
01650                     i = 2;
01651                 }
01652                 i--;
01653 
01654                 /* get value before previous */
01655 #ifdef FIXED_POINT
01656                 /* dividing by 2, shift right 1 bit */
01657                 RE(tempLeft)  += (RE(ps->ipd_prev[bk][i]) >> 1);
01658                 IM(tempLeft)  += (IM(ps->ipd_prev[bk][i]) >> 1);
01659                 RE(tempRight) += (RE(ps->opd_prev[bk][i]) >> 1);
01660                 IM(tempRight) += (IM(ps->opd_prev[bk][i]) >> 1);
01661 #else
01662                 RE(tempLeft)  += MUL_F(RE(ps->ipd_prev[bk][i]), FRAC_CONST(0.5));
01663                 IM(tempLeft)  += MUL_F(IM(ps->ipd_prev[bk][i]), FRAC_CONST(0.5));
01664                 RE(tempRight) += MUL_F(RE(ps->opd_prev[bk][i]), FRAC_CONST(0.5));
01665                 IM(tempRight) += MUL_F(IM(ps->opd_prev[bk][i]), FRAC_CONST(0.5));
01666 #endif
01667 
01668 #if 0 /* original code */
01669                 ipd = (float)atan2(IM(tempLeft), RE(tempLeft));
01670                 opd = (float)atan2(IM(tempRight), RE(tempRight));
01671 
01672                 /* phase rotation */
01673                 RE(phaseLeft) = (float)cos(opd);
01674                 IM(phaseLeft) = (float)sin(opd);
01675                 opd -= ipd;
01676                 RE(phaseRight) = (float)cos(opd);
01677                 IM(phaseRight) = (float)sin(opd);
01678 #else
01679 
01680                 // x = IM(tempLeft)
01681                 // y = RE(tempLeft)
01682                 // p = IM(tempRight)
01683                 // q = RE(tempRight)
01684                 // cos(atan2(x,y)) = y/sqrt((x*x) + (y*y))
01685                 // sin(atan2(x,y)) = x/sqrt((x*x) + (y*y))
01686                 // cos(atan2(x,y)-atan2(p,q)) = (y*q + x*p) / ( sqrt((x*x) + (y*y)) * sqrt((p*p) + (q*q)) );
01687                 // sin(atan2(x,y)-atan2(p,q)) = (x*q - y*p) / ( sqrt((x*x) + (y*y)) * sqrt((p*p) + (q*q)) );
01688 
01689                 xy = magnitude_c(tempRight);
01690                 pq = magnitude_c(tempLeft);
01691 
01692                 if (xy != 0)
01693                 {
01694                     RE(phaseLeft) = DIV_R(RE(tempRight), xy);
01695                     IM(phaseLeft) = DIV_R(IM(tempRight), xy);
01696                 } else {
01697                     RE(phaseLeft) = 0;
01698                     IM(phaseLeft) = 0;
01699                 }
01700 
01701                 xypq = MUL_R(xy, pq);
01702 
01703                 if (xypq != 0)
01704                 {
01705                     real_t tmp1 = MUL_R(RE(tempRight), RE(tempLeft)) + MUL_R(IM(tempRight), IM(tempLeft));
01706                     real_t tmp2 = MUL_R(IM(tempRight), RE(tempLeft)) - MUL_R(RE(tempRight), IM(tempLeft));
01707 
01708                     RE(phaseRight) = DIV_R(tmp1, xypq);
01709                     IM(phaseRight) = DIV_R(tmp2, xypq);
01710                 } else {
01711                     RE(phaseRight) = 0;
01712                     IM(phaseRight) = 0;
01713                 }
01714 
01715 #endif
01716 
01717                 /* MUL_F(COEF, REAL) = COEF */
01718                 IM(h11) = MUL_R(RE(h11), IM(phaseLeft));
01719                 IM(h12) = MUL_R(RE(h12), IM(phaseRight));
01720                 IM(h21) = MUL_R(RE(h21), IM(phaseLeft));
01721                 IM(h22) = MUL_R(RE(h22), IM(phaseRight));
01722 
01723                 RE(h11) = MUL_R(RE(h11), RE(phaseLeft));
01724                 RE(h12) = MUL_R(RE(h12), RE(phaseRight));
01725                 RE(h21) = MUL_R(RE(h21), RE(phaseLeft));
01726                 RE(h22) = MUL_R(RE(h22), RE(phaseRight));
01727             }
01728 
01729             /* length of the envelope n_e+1 - n_e (in time samples) */
01730             /* 0 < L <= 32: integer */
01731             L = (real_t)(ps->border_position[env + 1] - ps->border_position[env]);
01732 
01733             /* obtain final H_xy by means of linear interpolation */
01734             RE(deltaH11) = (RE(h11) - RE(ps->h11_prev[gr])) / L;
01735             RE(deltaH12) = (RE(h12) - RE(ps->h12_prev[gr])) / L;
01736             RE(deltaH21) = (RE(h21) - RE(ps->h21_prev[gr])) / L;
01737             RE(deltaH22) = (RE(h22) - RE(ps->h22_prev[gr])) / L;
01738 
01739             RE(H11) = RE(ps->h11_prev[gr]);
01740             RE(H12) = RE(ps->h12_prev[gr]);
01741             RE(H21) = RE(ps->h21_prev[gr]);
01742             RE(H22) = RE(ps->h22_prev[gr]);
01743 
01744             RE(ps->h11_prev[gr]) = RE(h11);
01745             RE(ps->h12_prev[gr]) = RE(h12);
01746             RE(ps->h21_prev[gr]) = RE(h21);
01747             RE(ps->h22_prev[gr]) = RE(h22);
01748 
01749             /* only calculate imaginary part when needed */
01750             if ((ps->enable_ipdopd) && (bk < nr_ipdopd_par))
01751             {
01752                 /* obtain final H_xy by means of linear interpolation */
01753                 IM(deltaH11) = (IM(h11) - IM(ps->h11_prev[gr])) / L;
01754                 IM(deltaH12) = (IM(h12) - IM(ps->h12_prev[gr])) / L;
01755                 IM(deltaH21) = (IM(h21) - IM(ps->h21_prev[gr])) / L;
01756                 IM(deltaH22) = (IM(h22) - IM(ps->h22_prev[gr])) / L;
01757 
01758                 IM(H11) = IM(ps->h11_prev[gr]);
01759                 IM(H12) = IM(ps->h12_prev[gr]);
01760                 IM(H21) = IM(ps->h21_prev[gr]);
01761                 IM(H22) = IM(ps->h22_prev[gr]);
01762 
01763                 if ((NEGATE_IPD_MASK & ps->map_group2bk[gr]) != 0)
01764                 {
01765                     IM(deltaH11) = -IM(deltaH11);
01766                     IM(deltaH12) = -IM(deltaH12);
01767                     IM(deltaH21) = -IM(deltaH21);
01768                     IM(deltaH22) = -IM(deltaH22);
01769 
01770                     IM(H11) = -IM(H11);
01771                     IM(H12) = -IM(H12);
01772                     IM(H21) = -IM(H21);
01773                     IM(H22) = -IM(H22);
01774                 }
01775 
01776                 IM(ps->h11_prev[gr]) = IM(h11);
01777                 IM(ps->h12_prev[gr]) = IM(h12);
01778                 IM(ps->h21_prev[gr]) = IM(h21);
01779                 IM(ps->h22_prev[gr]) = IM(h22);
01780             }
01781 
01782             /* apply H_xy to the current envelope band of the decorrelated subband */
01783             for (n = ps->border_position[env]; n < ps->border_position[env + 1]; n++)
01784             {
01785                 /* addition finalises the interpolation over every n */
01786                 RE(H11) += RE(deltaH11);
01787                 RE(H12) += RE(deltaH12);
01788                 RE(H21) += RE(deltaH21);
01789                 RE(H22) += RE(deltaH22);
01790                 if ((ps->enable_ipdopd) && (bk < nr_ipdopd_par))
01791                 {
01792                     IM(H11) += IM(deltaH11);
01793                     IM(H12) += IM(deltaH12);
01794                     IM(H21) += IM(deltaH21);
01795                     IM(H22) += IM(deltaH22);
01796                 }
01797 
01798                 /* channel is an alias to the subband */
01799                 for (sb = ps->group_border[gr]; sb < maxsb; sb++)
01800                 {
01801                     complex_t inLeft, inRight;
01802 
01803                     /* load decorrelated samples */
01804                     if (gr < ps->num_hybrid_groups)
01805                     {
01806                         RE(inLeft) =  RE(X_hybrid_left[n][sb]);
01807                         IM(inLeft) =  IM(X_hybrid_left[n][sb]);
01808                         RE(inRight) = RE(X_hybrid_right[n][sb]);
01809                         IM(inRight) = IM(X_hybrid_right[n][sb]);
01810                     } else {
01811                         RE(inLeft) =  RE(X_left[n][sb]);
01812                         IM(inLeft) =  IM(X_left[n][sb]);
01813                         RE(inRight) = RE(X_right[n][sb]);
01814                         IM(inRight) = IM(X_right[n][sb]);
01815                     }
01816 
01817                     /* apply mixing */
01818                     RE(tempLeft) =  MUL_C(RE(H11), RE(inLeft)) + MUL_C(RE(H21), RE(inRight));
01819                     IM(tempLeft) =  MUL_C(RE(H11), IM(inLeft)) + MUL_C(RE(H21), IM(inRight));
01820                     RE(tempRight) = MUL_C(RE(H12), RE(inLeft)) + MUL_C(RE(H22), RE(inRight));
01821                     IM(tempRight) = MUL_C(RE(H12), IM(inLeft)) + MUL_C(RE(H22), IM(inRight));
01822 
01823                     /* only perform imaginary operations when needed */
01824                     if ((ps->enable_ipdopd) && (bk < nr_ipdopd_par))
01825                     {
01826                         /* apply rotation */
01827                         RE(tempLeft)  -= MUL_C(IM(H11), IM(inLeft)) + MUL_C(IM(H21), IM(inRight));
01828                         IM(tempLeft)  += MUL_C(IM(H11), RE(inLeft)) + MUL_C(IM(H21), RE(inRight));
01829                         RE(tempRight) -= MUL_C(IM(H12), IM(inLeft)) + MUL_C(IM(H22), IM(inRight));
01830                         IM(tempRight) += MUL_C(IM(H12), RE(inLeft)) + MUL_C(IM(H22), RE(inRight));
01831                     }
01832 
01833                     /* store final samples */
01834                     if (gr < ps->num_hybrid_groups)
01835                     {
01836                         RE(X_hybrid_left[n][sb])  = RE(tempLeft);
01837                         IM(X_hybrid_left[n][sb])  = IM(tempLeft);
01838                         RE(X_hybrid_right[n][sb]) = RE(tempRight);
01839                         IM(X_hybrid_right[n][sb]) = IM(tempRight);
01840                     } else {
01841                         RE(X_left[n][sb])  = RE(tempLeft);
01842                         IM(X_left[n][sb])  = IM(tempLeft);
01843                         RE(X_right[n][sb]) = RE(tempRight);
01844                         IM(X_right[n][sb]) = IM(tempRight);
01845                     }
01846                 }
01847             }
01848 
01849             /* shift phase smoother's circular buffer index */
01850             ps->phase_hist++;
01851             if (ps->phase_hist == 2)
01852             {
01853                 ps->phase_hist = 0;
01854             }
01855         }
01856     }
01857 }
01858 
01859 void ps_free(ps_info *ps)
01860 {
01861     /* free hybrid filterbank structures */
01862     hybrid_free(ps->hyb);
01863 
01864     faad_free(ps);
01865 }
01866 
01867 ps_info *ps_init(uint8_t sr_index)
01868 {
01869     uint8_t i;
01870     uint8_t short_delay_band;
01871 
01872     ps_info *ps = (ps_info*)faad_malloc(sizeof(ps_info));
01873     memset(ps, 0, sizeof(ps_info));
01874 
01875     ps->hyb = hybrid_init();
01876 
01877     ps->ps_data_available = 0;
01878 
01879     /* delay stuff*/
01880     ps->saved_delay = 0;
01881 
01882     for (i = 0; i < 64; i++)
01883     {
01884         ps->delay_buf_index_delay[i] = 0;
01885     }
01886 
01887     for (i = 0; i < NO_ALLPASS_LINKS; i++)
01888     {
01889         ps->delay_buf_index_ser[i] = 0;
01890 #ifdef PARAM_32KHZ
01891         if (sr_index <= 5) /* >= 32 kHz*/
01892         {
01893             ps->num_sample_delay_ser[i] = delay_length_d[1][i];
01894         } else {
01895             ps->num_sample_delay_ser[i] = delay_length_d[0][i];
01896         }
01897 #else
01898         /* THESE ARE CONSTANTS NOW */
01899         ps->num_sample_delay_ser[i] = delay_length_d[i];
01900 #endif
01901     }
01902 
01903 #ifdef PARAM_32KHZ
01904     if (sr_index <= 5) /* >= 32 kHz*/
01905     {
01906         short_delay_band = 35;
01907         ps->nr_allpass_bands = 22;
01908         ps->alpha_decay = FRAC_CONST(0.76592833836465);
01909         ps->alpha_smooth = FRAC_CONST(0.25);
01910     } else {
01911         short_delay_band = 64;
01912         ps->nr_allpass_bands = 45;
01913         ps->alpha_decay = FRAC_CONST(0.58664621951003);
01914         ps->alpha_smooth = FRAC_CONST(0.6);
01915     }
01916 #else
01917     /* THESE ARE CONSTANTS NOW */
01918     short_delay_band = 35;
01919     ps->nr_allpass_bands = 22;
01920     ps->alpha_decay = FRAC_CONST(0.76592833836465);
01921     ps->alpha_smooth = FRAC_CONST(0.25);
01922 #endif
01923 
01924     /* THESE ARE CONSTANT NOW IF PS IS INDEPENDANT OF SAMPLERATE */
01925     for (i = 0; i < short_delay_band; i++)
01926     {
01927         ps->delay_D[i] = 14;
01928     }
01929     for (i = short_delay_band; i < 64; i++)
01930     {
01931         ps->delay_D[i] = 1;
01932     }
01933 
01934     /* mixing and phase */
01935     for (i = 0; i < 50; i++)
01936     {
01937         RE(ps->h11_prev[i]) = 1;
01938         IM(ps->h12_prev[i]) = 1;
01939         RE(ps->h11_prev[i]) = 1;
01940         IM(ps->h12_prev[i]) = 1;
01941     }
01942 
01943     ps->phase_hist = 0;
01944 
01945     for (i = 0; i < 20; i++)
01946     {
01947         RE(ps->ipd_prev[i][0]) = 0;
01948         IM(ps->ipd_prev[i][0]) = 0;
01949         RE(ps->ipd_prev[i][1]) = 0;
01950         IM(ps->ipd_prev[i][1]) = 0;
01951         RE(ps->opd_prev[i][0]) = 0;
01952         IM(ps->opd_prev[i][0]) = 0;
01953         RE(ps->opd_prev[i][1]) = 0;
01954         IM(ps->opd_prev[i][1]) = 0;
01955     }
01956 
01957     return ps;
01958 }
01959 
01960 /* main Parametric Stereo decoding function */
01961 uint8_t ps_decode(ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64])
01962 {
01963     qmf_t X_hybrid_left[32][32] = {{0}};
01964     qmf_t X_hybrid_right[32][32] = {{0}};
01965 
01966     /* delta decoding of the bitstream data */
01967     ps_data_decode(ps);
01968 
01969     /* set up some parameters depending on filterbank type */
01970     if (ps->use34hybrid_bands)
01971     {
01972         ps->group_border = (uint8_t*)group_border34;
01973         ps->map_group2bk = (uint16_t*)map_group2bk34;
01974         ps->num_groups = 32+18;
01975         ps->num_hybrid_groups = 32;
01976         ps->nr_par_bands = 34;
01977         ps->decay_cutoff = 5;
01978     } else {
01979         ps->group_border = (uint8_t*)group_border20;
01980         ps->map_group2bk = (uint16_t*)map_group2bk20;
01981         ps->num_groups = 10+12;
01982         ps->num_hybrid_groups = 10;
01983         ps->nr_par_bands = 20;
01984         ps->decay_cutoff = 3;
01985     }
01986 
01987     /* Perform further analysis on the lowest subbands to get a higher
01988      * frequency resolution
01989      */
01990     hybrid_analysis((hyb_info*)ps->hyb, X_left, X_hybrid_left,
01991         ps->use34hybrid_bands);
01992 
01993     /* decorrelate mono signal */
01994     ps_decorrelate(ps, X_left, X_right, X_hybrid_left, X_hybrid_right);
01995 
01996     /* apply mixing and phase parameters */
01997     ps_mix_phase(ps, X_left, X_right, X_hybrid_left, X_hybrid_right);
01998 
01999     /* hybrid synthesis, to rebuild the SBR QMF matrices */
02000     hybrid_synthesis((hyb_info*)ps->hyb, X_left, X_hybrid_left,
02001         ps->use34hybrid_bands);
02002 
02003     hybrid_synthesis((hyb_info*)ps->hyb, X_right, X_hybrid_right,
02004         ps->use34hybrid_bands);
02005 
02006     return 0;
02007 }
02008 
02009 #endif
02010 

Generated on Tue Dec 13 14:47:42 2005 for guliverkli by  doxygen 1.4.5