2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 ** Any non-GPL usage of this software or parts of this software is strictly
22 ** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23 ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
28 ** $Id: ps_dec.c,v 1.16 2009/01/26 22:32:31 menno Exp $
40 #define NEGATE_IPD_MASK (0x1000)
41 #define DECAY_SLOPE FRAC_CONST(0.05)
42 #define COEF_SQRT2 COEF_CONST(1.4142135623731)
45 /* filters are mirrored in coef 6, second half left out */
46 static const real_t p8_13_20[7] =
48 FRAC_CONST(0.00746082949812),
49 FRAC_CONST(0.02270420949825),
50 FRAC_CONST(0.04546865930473),
51 FRAC_CONST(0.07266113929591),
52 FRAC_CONST(0.09885108575264),
53 FRAC_CONST(0.11793710567217),
57 static const real_t p2_13_20[7] =
60 FRAC_CONST(0.01899487526049),
62 FRAC_CONST(-0.07293139167538),
64 FRAC_CONST(0.30596630545168),
68 static const real_t p12_13_34[7] =
70 FRAC_CONST(0.04081179924692),
71 FRAC_CONST(0.03812810994926),
72 FRAC_CONST(0.05144908135699),
73 FRAC_CONST(0.06399831151592),
74 FRAC_CONST(0.07428313801106),
75 FRAC_CONST(0.08100347892914),
76 FRAC_CONST(0.08333333333333)
79 static const real_t p8_13_34[7] =
81 FRAC_CONST(0.01565675600122),
82 FRAC_CONST(0.03752716391991),
83 FRAC_CONST(0.05417891378782),
84 FRAC_CONST(0.08417044116767),
85 FRAC_CONST(0.10307344158036),
86 FRAC_CONST(0.12222452249753),
90 static const real_t p4_13_34[7] =
92 FRAC_CONST(-0.05908211155639),
93 FRAC_CONST(-0.04871498374946),
95 FRAC_CONST(0.07778723915851),
96 FRAC_CONST(0.16486303567403),
97 FRAC_CONST(0.23279856662996),
102 static const uint8_t delay_length_d[2][NO_ALLPASS_LINKS] = {
103 { 1, 2, 3 } /* d_24kHz */,
104 { 3, 4, 5 } /* d_48kHz */
107 static const uint8_t delay_length_d[NO_ALLPASS_LINKS] = {
108 3, 4, 5 /* d_48kHz */
111 static const real_t filter_a[NO_ALLPASS_LINKS] = { /* a(m) = exp(-d_48kHz(m)/7) */
112 FRAC_CONST(0.65143905753106),
113 FRAC_CONST(0.56471812200776),
114 FRAC_CONST(0.48954165955695)
117 static const uint8_t group_border20[10+12 + 1] =
119 6, 7, 0, 1, 2, 3, /* 6 subqmf subbands */
120 9, 8, /* 2 subqmf subbands */
121 10, 11, /* 2 subqmf subbands */
122 3, 4, 5, 6, 7, 8, 9, 11, 14, 18, 23, 35, 64
125 static const uint8_t group_border34[32+18 + 1] =
127 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, /* 12 subqmf subbands */
128 12, 13, 14, 15, 16, 17, 18, 19, /* 8 subqmf subbands */
129 20, 21, 22, 23, /* 4 subqmf subbands */
130 24, 25, 26, 27, /* 4 subqmf subbands */
131 28, 29, 30, 31, /* 4 subqmf subbands */
132 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
135 static const uint16_t map_group2bk20[10+12] =
137 (NEGATE_IPD_MASK | 1), (NEGATE_IPD_MASK | 0),
138 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
141 static const uint16_t map_group2bk34[32+18] =
143 0, 1, 2, 3, 4, 5, 6, 6, 7, (NEGATE_IPD_MASK | 2), (NEGATE_IPD_MASK | 1), (NEGATE_IPD_MASK | 0),
144 10, 10, 4, 5, 6, 7, 8, 9,
148 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33
151 /* type definitions */
155 uint8_t resolution20[3];
156 uint8_t resolution34[5];
163 /* static function declarations */
164 static void ps_data_decode(ps_info *ps);
165 static hyb_info *hybrid_init(uint8_t numTimeSlotsRate);
166 static void channel_filter2(hyb_info *hyb, uint8_t frame_len, const real_t *filter,
167 qmf_t *buffer, qmf_t **X_hybrid);
168 static void INLINE DCT3_4_unscaled(real_t *y, real_t *x);
169 static void channel_filter8(hyb_info *hyb, uint8_t frame_len, const real_t *filter,
170 qmf_t *buffer, qmf_t **X_hybrid);
171 static void hybrid_analysis(hyb_info *hyb, qmf_t X[32][64], qmf_t X_hybrid[32][32],
172 uint8_t use34, uint8_t numTimeSlotsRate);
173 static void hybrid_synthesis(hyb_info *hyb, qmf_t X[32][64], qmf_t X_hybrid[32][32],
174 uint8_t use34, uint8_t numTimeSlotsRate);
175 static int8_t delta_clip(int8_t i, int8_t min, int8_t max);
176 static void delta_decode(uint8_t enable, int8_t *index, int8_t *index_prev,
177 uint8_t dt_flag, uint8_t nr_par, uint8_t stride,
178 int8_t min_index, int8_t max_index);
179 static void delta_modulo_decode(uint8_t enable, int8_t *index, int8_t *index_prev,
180 uint8_t dt_flag, uint8_t nr_par, uint8_t stride,
182 static void map20indexto34(int8_t *index, uint8_t bins);
184 static void map34indexto20(int8_t *index, uint8_t bins);
186 static void ps_data_decode(ps_info *ps);
187 static void ps_decorrelate(ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64],
188 qmf_t X_hybrid_left[32][32], qmf_t X_hybrid_right[32][32]);
189 static void ps_mix_phase(ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64],
190 qmf_t X_hybrid_left[32][32], qmf_t X_hybrid_right[32][32]);
195 static hyb_info *hybrid_init(uint8_t numTimeSlotsRate)
199 hyb_info *hyb = (hyb_info*)faad_malloc(sizeof(hyb_info));
201 hyb->resolution34[0] = 12;
202 hyb->resolution34[1] = 8;
203 hyb->resolution34[2] = 4;
204 hyb->resolution34[3] = 4;
205 hyb->resolution34[4] = 4;
207 hyb->resolution20[0] = 8;
208 hyb->resolution20[1] = 2;
209 hyb->resolution20[2] = 2;
211 hyb->frame_len = numTimeSlotsRate;
213 hyb->work = (qmf_t*)faad_malloc((hyb->frame_len+12) * sizeof(qmf_t));
214 memset(hyb->work, 0, (hyb->frame_len+12) * sizeof(qmf_t));
216 hyb->buffer = (qmf_t**)faad_malloc(5 * sizeof(qmf_t*));
217 for (i = 0; i < 5; i++)
219 hyb->buffer[i] = (qmf_t*)faad_malloc(hyb->frame_len * sizeof(qmf_t));
220 memset(hyb->buffer[i], 0, hyb->frame_len * sizeof(qmf_t));
223 hyb->temp = (qmf_t**)faad_malloc(hyb->frame_len * sizeof(qmf_t*));
224 for (i = 0; i < hyb->frame_len; i++)
226 hyb->temp[i] = (qmf_t*)faad_malloc(12 /*max*/ * sizeof(qmf_t));
232 static void hybrid_free(hyb_info *hyb)
239 faad_free(hyb->work);
241 for (i = 0; i < 5; i++)
244 faad_free(hyb->buffer[i]);
247 faad_free(hyb->buffer);
249 for (i = 0; i < hyb->frame_len; i++)
252 faad_free(hyb->temp[i]);
255 faad_free(hyb->temp);
260 /* real filter, size 2 */
261 static void channel_filter2(hyb_info *hyb, uint8_t frame_len, const real_t *filter,
262 qmf_t *buffer, qmf_t **X_hybrid)
266 for (i = 0; i < frame_len; i++)
268 real_t r0 = MUL_F(filter[0],(QMF_RE(buffer[0+i]) + QMF_RE(buffer[12+i])));
269 real_t r1 = MUL_F(filter[1],(QMF_RE(buffer[1+i]) + QMF_RE(buffer[11+i])));
270 real_t r2 = MUL_F(filter[2],(QMF_RE(buffer[2+i]) + QMF_RE(buffer[10+i])));
271 real_t r3 = MUL_F(filter[3],(QMF_RE(buffer[3+i]) + QMF_RE(buffer[9+i])));
272 real_t r4 = MUL_F(filter[4],(QMF_RE(buffer[4+i]) + QMF_RE(buffer[8+i])));
273 real_t r5 = MUL_F(filter[5],(QMF_RE(buffer[5+i]) + QMF_RE(buffer[7+i])));
274 real_t r6 = MUL_F(filter[6],QMF_RE(buffer[6+i]));
275 real_t i0 = MUL_F(filter[0],(QMF_IM(buffer[0+i]) + QMF_IM(buffer[12+i])));
276 real_t i1 = MUL_F(filter[1],(QMF_IM(buffer[1+i]) + QMF_IM(buffer[11+i])));
277 real_t i2 = MUL_F(filter[2],(QMF_IM(buffer[2+i]) + QMF_IM(buffer[10+i])));
278 real_t i3 = MUL_F(filter[3],(QMF_IM(buffer[3+i]) + QMF_IM(buffer[9+i])));
279 real_t i4 = MUL_F(filter[4],(QMF_IM(buffer[4+i]) + QMF_IM(buffer[8+i])));
280 real_t i5 = MUL_F(filter[5],(QMF_IM(buffer[5+i]) + QMF_IM(buffer[7+i])));
281 real_t i6 = MUL_F(filter[6],QMF_IM(buffer[6+i]));
284 QMF_RE(X_hybrid[i][0]) = r0 + r1 + r2 + r3 + r4 + r5 + r6;
285 QMF_IM(X_hybrid[i][0]) = i0 + i1 + i2 + i3 + i4 + i5 + i6;
288 QMF_RE(X_hybrid[i][1]) = r0 - r1 + r2 - r3 + r4 - r5 + r6;
289 QMF_IM(X_hybrid[i][1]) = i0 - i1 + i2 - i3 + i4 - i5 + i6;
293 /* complex filter, size 4 */
294 static void channel_filter4(hyb_info *hyb, uint8_t frame_len, const real_t *filter,
295 qmf_t *buffer, qmf_t **X_hybrid)
298 real_t input_re1[2], input_re2[2], input_im1[2], input_im2[2];
300 for (i = 0; i < frame_len; i++)
302 input_re1[0] = -MUL_F(filter[2], (QMF_RE(buffer[i+2]) + QMF_RE(buffer[i+10]))) +
303 MUL_F(filter[6], QMF_RE(buffer[i+6]));
304 input_re1[1] = MUL_F(FRAC_CONST(-0.70710678118655),
305 (MUL_F(filter[1], (QMF_RE(buffer[i+1]) + QMF_RE(buffer[i+11]))) +
306 MUL_F(filter[3], (QMF_RE(buffer[i+3]) + QMF_RE(buffer[i+9]))) -
307 MUL_F(filter[5], (QMF_RE(buffer[i+5]) + QMF_RE(buffer[i+7])))));
309 input_im1[0] = MUL_F(filter[0], (QMF_IM(buffer[i+0]) - QMF_IM(buffer[i+12]))) -
310 MUL_F(filter[4], (QMF_IM(buffer[i+4]) - QMF_IM(buffer[i+8])));
311 input_im1[1] = MUL_F(FRAC_CONST(0.70710678118655),
312 (MUL_F(filter[1], (QMF_IM(buffer[i+1]) - QMF_IM(buffer[i+11]))) -
313 MUL_F(filter[3], (QMF_IM(buffer[i+3]) - QMF_IM(buffer[i+9]))) -
314 MUL_F(filter[5], (QMF_IM(buffer[i+5]) - QMF_IM(buffer[i+7])))));
316 input_re2[0] = MUL_F(filter[0], (QMF_RE(buffer[i+0]) - QMF_RE(buffer[i+12]))) -
317 MUL_F(filter[4], (QMF_RE(buffer[i+4]) - QMF_RE(buffer[i+8])));
318 input_re2[1] = MUL_F(FRAC_CONST(0.70710678118655),
319 (MUL_F(filter[1], (QMF_RE(buffer[i+1]) - QMF_RE(buffer[i+11]))) -
320 MUL_F(filter[3], (QMF_RE(buffer[i+3]) - QMF_RE(buffer[i+9]))) -
321 MUL_F(filter[5], (QMF_RE(buffer[i+5]) - QMF_RE(buffer[i+7])))));
323 input_im2[0] = -MUL_F(filter[2], (QMF_IM(buffer[i+2]) + QMF_IM(buffer[i+10]))) +
324 MUL_F(filter[6], QMF_IM(buffer[i+6]));
325 input_im2[1] = MUL_F(FRAC_CONST(-0.70710678118655),
326 (MUL_F(filter[1], (QMF_IM(buffer[i+1]) + QMF_IM(buffer[i+11]))) +
327 MUL_F(filter[3], (QMF_IM(buffer[i+3]) + QMF_IM(buffer[i+9]))) -
328 MUL_F(filter[5], (QMF_IM(buffer[i+5]) + QMF_IM(buffer[i+7])))));
331 QMF_RE(X_hybrid[i][0]) = input_re1[0] + input_re1[1] + input_im1[0] + input_im1[1];
332 QMF_IM(X_hybrid[i][0]) = -input_re2[0] - input_re2[1] + input_im2[0] + input_im2[1];
335 QMF_RE(X_hybrid[i][1]) = input_re1[0] - input_re1[1] - input_im1[0] + input_im1[1];
336 QMF_IM(X_hybrid[i][1]) = input_re2[0] - input_re2[1] + input_im2[0] - input_im2[1];
339 QMF_RE(X_hybrid[i][2]) = input_re1[0] - input_re1[1] + input_im1[0] - input_im1[1];
340 QMF_IM(X_hybrid[i][2]) = -input_re2[0] + input_re2[1] + input_im2[0] - input_im2[1];
343 QMF_RE(X_hybrid[i][3]) = input_re1[0] + input_re1[1] - input_im1[0] - input_im1[1];
344 QMF_IM(X_hybrid[i][3]) = input_re2[0] + input_re2[1] + input_im2[0] + input_im2[1];
348 static void INLINE DCT3_4_unscaled(real_t *y, real_t *x)
350 real_t f0, f1, f2, f3, f4, f5, f6, f7, f8;
352 f0 = MUL_F(x[2], FRAC_CONST(0.7071067811865476));
356 f4 = MUL_C(x[1], COEF_CONST(1.3065629648763766));
357 f5 = MUL_F(f3, FRAC_CONST(-0.9238795325112866));
358 f6 = MUL_F(x[3], FRAC_CONST(-0.5411961001461967));
367 /* complex filter, size 8 */
368 static void channel_filter8(hyb_info *hyb, uint8_t frame_len, const real_t *filter,
369 qmf_t *buffer, qmf_t **X_hybrid)
372 real_t input_re1[4], input_re2[4], input_im1[4], input_im2[4];
375 for (i = 0; i < frame_len; i++)
377 input_re1[0] = MUL_F(filter[6],QMF_RE(buffer[6+i]));
378 input_re1[1] = MUL_F(filter[5],(QMF_RE(buffer[5+i]) + QMF_RE(buffer[7+i])));
379 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])));
380 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])));
382 input_im1[0] = MUL_F(filter[5],(QMF_IM(buffer[7+i]) - QMF_IM(buffer[5+i])));
383 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])));
384 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])));
385 input_im1[3] = MUL_F(filter[2],(QMF_IM(buffer[10+i]) - QMF_IM(buffer[2+i])));
387 for (n = 0; n < 4; n++)
389 x[n] = input_re1[n] - input_im1[3-n];
391 DCT3_4_unscaled(x, x);
392 QMF_RE(X_hybrid[i][7]) = x[0];
393 QMF_RE(X_hybrid[i][5]) = x[2];
394 QMF_RE(X_hybrid[i][3]) = x[3];
395 QMF_RE(X_hybrid[i][1]) = x[1];
397 for (n = 0; n < 4; n++)
399 x[n] = input_re1[n] + input_im1[3-n];
401 DCT3_4_unscaled(x, x);
402 QMF_RE(X_hybrid[i][6]) = x[1];
403 QMF_RE(X_hybrid[i][4]) = x[3];
404 QMF_RE(X_hybrid[i][2]) = x[2];
405 QMF_RE(X_hybrid[i][0]) = x[0];
407 input_im2[0] = MUL_F(filter[6],QMF_IM(buffer[6+i]));
408 input_im2[1] = MUL_F(filter[5],(QMF_IM(buffer[5+i]) + QMF_IM(buffer[7+i])));
409 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])));
410 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])));
412 input_re2[0] = MUL_F(filter[5],(QMF_RE(buffer[7+i]) - QMF_RE(buffer[5+i])));
413 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])));
414 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])));
415 input_re2[3] = MUL_F(filter[2],(QMF_RE(buffer[10+i]) - QMF_RE(buffer[2+i])));
417 for (n = 0; n < 4; n++)
419 x[n] = input_im2[n] + input_re2[3-n];
421 DCT3_4_unscaled(x, x);
422 QMF_IM(X_hybrid[i][7]) = x[0];
423 QMF_IM(X_hybrid[i][5]) = x[2];
424 QMF_IM(X_hybrid[i][3]) = x[3];
425 QMF_IM(X_hybrid[i][1]) = x[1];
427 for (n = 0; n < 4; n++)
429 x[n] = input_im2[n] - input_re2[3-n];
431 DCT3_4_unscaled(x, x);
432 QMF_IM(X_hybrid[i][6]) = x[1];
433 QMF_IM(X_hybrid[i][4]) = x[3];
434 QMF_IM(X_hybrid[i][2]) = x[2];
435 QMF_IM(X_hybrid[i][0]) = x[0];
439 static void INLINE DCT3_6_unscaled(real_t *y, real_t *x)
441 real_t f0, f1, f2, f3, f4, f5, f6, f7;
443 f0 = MUL_F(x[3], FRAC_CONST(0.70710678118655));
446 f3 = MUL_F((x[1] - x[5]), FRAC_CONST(0.70710678118655));
447 f4 = MUL_F(x[2], FRAC_CONST(0.86602540378444)) + MUL_F(x[4], FRAC_CONST(0.5));
449 f6 = MUL_F(x[1], FRAC_CONST(0.96592582628907)) + MUL_F(x[5], FRAC_CONST(0.25881904510252));
452 y[1] = f2 + f3 - x[4];
455 y[4] = f1 - f3 - x[4];
459 /* complex filter, size 12 */
460 static void channel_filter12(hyb_info *hyb, uint8_t frame_len, const real_t *filter,
461 qmf_t *buffer, qmf_t **X_hybrid)
464 real_t input_re1[6], input_re2[6], input_im1[6], input_im2[6];
465 real_t out_re1[6], out_re2[6], out_im1[6], out_im2[6];
467 for (i = 0; i < frame_len; i++)
469 for (n = 0; n < 6; n++)
473 input_re1[0] = MUL_F(QMF_RE(buffer[6+i]), filter[6]);
474 input_re2[0] = MUL_F(QMF_IM(buffer[6+i]), filter[6]);
476 input_re1[6-n] = MUL_F((QMF_RE(buffer[n+i]) + QMF_RE(buffer[12-n+i])), filter[n]);
477 input_re2[6-n] = MUL_F((QMF_IM(buffer[n+i]) + QMF_IM(buffer[12-n+i])), filter[n]);
479 input_im2[n] = MUL_F((QMF_RE(buffer[n+i]) - QMF_RE(buffer[12-n+i])), filter[n]);
480 input_im1[n] = MUL_F((QMF_IM(buffer[n+i]) - QMF_IM(buffer[12-n+i])), filter[n]);
483 DCT3_6_unscaled(out_re1, input_re1);
484 DCT3_6_unscaled(out_re2, input_re2);
486 DCT3_6_unscaled(out_im1, input_im1);
487 DCT3_6_unscaled(out_im2, input_im2);
489 for (n = 0; n < 6; n += 2)
491 QMF_RE(X_hybrid[i][n]) = out_re1[n] - out_im1[n];
492 QMF_IM(X_hybrid[i][n]) = out_re2[n] + out_im2[n];
493 QMF_RE(X_hybrid[i][n+1]) = out_re1[n+1] + out_im1[n+1];
494 QMF_IM(X_hybrid[i][n+1]) = out_re2[n+1] - out_im2[n+1];
496 QMF_RE(X_hybrid[i][10-n]) = out_re1[n+1] - out_im1[n+1];
497 QMF_IM(X_hybrid[i][10-n]) = out_re2[n+1] + out_im2[n+1];
498 QMF_RE(X_hybrid[i][11-n]) = out_re1[n] + out_im1[n];
499 QMF_IM(X_hybrid[i][11-n]) = out_re2[n] - out_im2[n];
504 /* Hybrid analysis: further split up QMF subbands
505 * to improve frequency resolution
507 static void hybrid_analysis(hyb_info *hyb, qmf_t X[32][64], qmf_t X_hybrid[32][32],
508 uint8_t use34, uint8_t numTimeSlotsRate)
512 uint8_t qmf_bands = (use34) ? 5 : 3;
513 uint8_t *resolution = (use34) ? hyb->resolution34 : hyb->resolution20;
515 for (band = 0; band < qmf_bands; band++)
517 /* build working buffer */
518 memcpy(hyb->work, hyb->buffer[band], 12 * sizeof(qmf_t));
520 /* add new samples */
521 for (n = 0; n < hyb->frame_len; n++)
523 QMF_RE(hyb->work[12 + n]) = QMF_RE(X[n + 6 /*delay*/][band]);
524 QMF_IM(hyb->work[12 + n]) = QMF_IM(X[n + 6 /*delay*/][band]);
528 memcpy(hyb->buffer[band], hyb->work + hyb->frame_len, 12 * sizeof(qmf_t));
531 switch(resolution[band])
534 /* Type B real filter, Q[p] = 2 */
535 channel_filter2(hyb, hyb->frame_len, p2_13_20, hyb->work, hyb->temp);
538 /* Type A complex filter, Q[p] = 4 */
539 channel_filter4(hyb, hyb->frame_len, p4_13_34, hyb->work, hyb->temp);
542 /* Type A complex filter, Q[p] = 8 */
543 channel_filter8(hyb, hyb->frame_len, (use34) ? p8_13_34 : p8_13_20,
544 hyb->work, hyb->temp);
547 /* Type A complex filter, Q[p] = 12 */
548 channel_filter12(hyb, hyb->frame_len, p12_13_34, hyb->work, hyb->temp);
552 for (n = 0; n < hyb->frame_len; n++)
554 for (k = 0; k < resolution[band]; k++)
556 QMF_RE(X_hybrid[n][offset + k]) = QMF_RE(hyb->temp[n][k]);
557 QMF_IM(X_hybrid[n][offset + k]) = QMF_IM(hyb->temp[n][k]);
560 offset += resolution[band];
563 /* group hybrid channels */
566 for (n = 0; n < numTimeSlotsRate; n++)
568 QMF_RE(X_hybrid[n][3]) += QMF_RE(X_hybrid[n][4]);
569 QMF_IM(X_hybrid[n][3]) += QMF_IM(X_hybrid[n][4]);
570 QMF_RE(X_hybrid[n][4]) = 0;
571 QMF_IM(X_hybrid[n][4]) = 0;
573 QMF_RE(X_hybrid[n][2]) += QMF_RE(X_hybrid[n][5]);
574 QMF_IM(X_hybrid[n][2]) += QMF_IM(X_hybrid[n][5]);
575 QMF_RE(X_hybrid[n][5]) = 0;
576 QMF_IM(X_hybrid[n][5]) = 0;
581 static void hybrid_synthesis(hyb_info *hyb, qmf_t X[32][64], qmf_t X_hybrid[32][32],
582 uint8_t use34, uint8_t numTimeSlotsRate)
586 uint8_t qmf_bands = (use34) ? 5 : 3;
587 uint8_t *resolution = (use34) ? hyb->resolution34 : hyb->resolution20;
589 for(band = 0; band < qmf_bands; band++)
591 for (n = 0; n < hyb->frame_len; n++)
593 QMF_RE(X[n][band]) = 0;
594 QMF_IM(X[n][band]) = 0;
596 for (k = 0; k < resolution[band]; k++)
598 QMF_RE(X[n][band]) += QMF_RE(X_hybrid[n][offset + k]);
599 QMF_IM(X[n][band]) += QMF_IM(X_hybrid[n][offset + k]);
602 offset += resolution[band];
606 /* limits the value i to the range [min,max] */
607 static int8_t delta_clip(int8_t i, int8_t min, int8_t max)
619 /* delta decode array */
620 static void delta_decode(uint8_t enable, int8_t *index, int8_t *index_prev,
621 uint8_t dt_flag, uint8_t nr_par, uint8_t stride,
622 int8_t min_index, int8_t max_index)
630 /* delta coded in frequency direction */
631 index[0] = 0 + index[0];
632 index[0] = delta_clip(index[0], min_index, max_index);
634 for (i = 1; i < nr_par; i++)
636 index[i] = index[i-1] + index[i];
637 index[i] = delta_clip(index[i], min_index, max_index);
640 /* delta coded in time direction */
641 for (i = 0; i < nr_par; i++)
644 //int8_t tmp = index[i];
646 //printf("%d %d\n", index_prev[i*stride], index[i]);
647 //printf("%d\n", index[i]);
649 index[i] = index_prev[i*stride] + index[i];
651 index[i] = delta_clip(index[i], min_index, max_index);
655 // if (index[i] == 7)
657 // printf("%d %d %d\n", index_prev[i*stride], tmp, tmp2);
663 /* set indices to zero */
664 for (i = 0; i < nr_par; i++)
673 for (i = (nr_par<<1)-1; i > 0; i--)
675 index[i] = index[i>>1];
680 /* delta modulo decode array */
681 /* in: log2 value of the modulo value to allow using AND instead of MOD */
682 static void delta_modulo_decode(uint8_t enable, int8_t *index, int8_t *index_prev,
683 uint8_t dt_flag, uint8_t nr_par, uint8_t stride,
692 /* delta coded in frequency direction */
693 index[0] = 0 + index[0];
694 index[0] &= and_modulo;
696 for (i = 1; i < nr_par; i++)
698 index[i] = index[i-1] + index[i];
699 index[i] &= and_modulo;
702 /* delta coded in time direction */
703 for (i = 0; i < nr_par; i++)
705 index[i] = index_prev[i*stride] + index[i];
706 index[i] &= and_modulo;
710 /* set indices to zero */
711 for (i = 0; i < nr_par; i++)
721 for (i = (nr_par<<1)-1; i > 0; i--)
723 index[i] = index[i>>1];
729 static void map34indexto20(int8_t *index, uint8_t bins)
731 index[0] = (2*index[0]+index[1])/3;
732 index[1] = (index[1]+2*index[2])/3;
733 index[2] = (2*index[3]+index[4])/3;
734 index[3] = (index[4]+2*index[5])/3;
735 index[4] = (index[6]+index[7])/2;
736 index[5] = (index[8]+index[9])/2;
737 index[6] = index[10];
738 index[7] = index[11];
739 index[8] = (index[12]+index[13])/2;
740 index[9] = (index[14]+index[15])/2;
741 index[10] = index[16];
745 index[11] = index[17];
746 index[12] = index[18];
747 index[13] = index[19];
748 index[14] = (index[20]+index[21])/2;
749 index[15] = (index[22]+index[23])/2;
750 index[16] = (index[24]+index[25])/2;
751 index[17] = (index[26]+index[27])/2;
752 index[18] = (index[28]+index[29]+index[30]+index[31])/4;
753 index[19] = (index[32]+index[33])/2;
758 static void map20indexto34(int8_t *index, uint8_t bins)
761 index[1] = (index[0] + index[1])/2;
764 index[4] = (index[2] + index[3])/2;
770 index[10] = index[6];
771 index[11] = index[7];
772 index[12] = index[8];
773 index[13] = index[8];
774 index[14] = index[9];
775 index[15] = index[9];
776 index[16] = index[10];
780 index[17] = index[11];
781 index[18] = index[12];
782 index[19] = index[13];
783 index[20] = index[14];
784 index[21] = index[14];
785 index[22] = index[15];
786 index[23] = index[15];
787 index[24] = index[16];
788 index[25] = index[16];
789 index[26] = index[17];
790 index[27] = index[17];
791 index[28] = index[18];
792 index[29] = index[18];
793 index[30] = index[18];
794 index[31] = index[18];
795 index[32] = index[19];
796 index[33] = index[19];
800 /* parse the bitstream data decoded in ps_data() */
801 static void ps_data_decode(ps_info *ps)
805 /* ps data not available, use data from previous frame */
806 if (ps->ps_data_available == 0)
811 for (env = 0; env < ps->num_env; env++)
813 int8_t *iid_index_prev;
814 int8_t *icc_index_prev;
815 int8_t *ipd_index_prev;
816 int8_t *opd_index_prev;
818 int8_t num_iid_steps = (ps->iid_mode < 3) ? 7 : 15 /*fine quant*/;
822 /* take last envelope from previous frame */
823 iid_index_prev = ps->iid_index_prev;
824 icc_index_prev = ps->icc_index_prev;
825 ipd_index_prev = ps->ipd_index_prev;
826 opd_index_prev = ps->opd_index_prev;
828 /* take index values from previous envelope */
829 iid_index_prev = ps->iid_index[env - 1];
830 icc_index_prev = ps->icc_index[env - 1];
831 ipd_index_prev = ps->ipd_index[env - 1];
832 opd_index_prev = ps->opd_index[env - 1];
836 /* delta decode iid parameters */
837 delta_decode(ps->enable_iid, ps->iid_index[env], iid_index_prev,
838 ps->iid_dt[env], ps->nr_iid_par,
839 (ps->iid_mode == 0 || ps->iid_mode == 3) ? 2 : 1,
840 -num_iid_steps, num_iid_steps);
843 /* delta decode icc parameters */
844 delta_decode(ps->enable_icc, ps->icc_index[env], icc_index_prev,
845 ps->icc_dt[env], ps->nr_icc_par,
846 (ps->icc_mode == 0 || ps->icc_mode == 3) ? 2 : 1,
849 /* delta modulo decode ipd parameters */
850 delta_modulo_decode(ps->enable_ipdopd, ps->ipd_index[env], ipd_index_prev,
851 ps->ipd_dt[env], ps->nr_ipdopd_par, 1, 7);
853 /* delta modulo decode opd parameters */
854 delta_modulo_decode(ps->enable_ipdopd, ps->opd_index[env], opd_index_prev,
855 ps->opd_dt[env], ps->nr_ipdopd_par, 1, 7);
858 /* handle error case */
859 if (ps->num_env == 0)
866 for (bin = 0; bin < 34; bin++)
867 ps->iid_index[0][bin] = ps->iid_index_prev[bin];
869 for (bin = 0; bin < 34; bin++)
870 ps->iid_index[0][bin] = 0;
875 for (bin = 0; bin < 34; bin++)
876 ps->icc_index[0][bin] = ps->icc_index_prev[bin];
878 for (bin = 0; bin < 34; bin++)
879 ps->icc_index[0][bin] = 0;
882 if (ps->enable_ipdopd)
884 for (bin = 0; bin < 17; bin++)
886 ps->ipd_index[0][bin] = ps->ipd_index_prev[bin];
887 ps->opd_index[0][bin] = ps->opd_index_prev[bin];
890 for (bin = 0; bin < 17; bin++)
892 ps->ipd_index[0][bin] = 0;
893 ps->opd_index[0][bin] = 0;
898 /* update previous indices */
899 for (bin = 0; bin < 34; bin++)
900 ps->iid_index_prev[bin] = ps->iid_index[ps->num_env-1][bin];
901 for (bin = 0; bin < 34; bin++)
902 ps->icc_index_prev[bin] = ps->icc_index[ps->num_env-1][bin];
903 for (bin = 0; bin < 17; bin++)
905 ps->ipd_index_prev[bin] = ps->ipd_index[ps->num_env-1][bin];
906 ps->opd_index_prev[bin] = ps->opd_index[ps->num_env-1][bin];
909 ps->ps_data_available = 0;
911 if (ps->frame_class == 0)
913 ps->border_position[0] = 0;
914 for (env = 1; env < ps->num_env; env++)
916 ps->border_position[env] = (env * ps->numTimeSlotsRate) / ps->num_env;
918 ps->border_position[ps->num_env] = ps->numTimeSlotsRate;
920 ps->border_position[0] = 0;
922 if (ps->border_position[ps->num_env] < ps->numTimeSlotsRate)
924 for (bin = 0; bin < 34; bin++)
926 ps->iid_index[ps->num_env][bin] = ps->iid_index[ps->num_env-1][bin];
927 ps->icc_index[ps->num_env][bin] = ps->icc_index[ps->num_env-1][bin];
929 for (bin = 0; bin < 17; bin++)
931 ps->ipd_index[ps->num_env][bin] = ps->ipd_index[ps->num_env-1][bin];
932 ps->opd_index[ps->num_env][bin] = ps->opd_index[ps->num_env-1][bin];
935 ps->border_position[ps->num_env] = ps->numTimeSlotsRate;
938 for (env = 1; env < ps->num_env; env++)
940 int8_t thr = ps->numTimeSlotsRate - (ps->num_env - env);
942 if (ps->border_position[env] > thr)
944 ps->border_position[env] = thr;
946 thr = ps->border_position[env-1]+1;
947 if (ps->border_position[env] < thr)
949 ps->border_position[env] = thr;
955 /* make sure that the indices of all parameters can be mapped
956 * to the same hybrid synthesis filterbank
959 for (env = 0; env < ps->num_env; env++)
961 if (ps->iid_mode == 2 || ps->iid_mode == 5)
962 map34indexto20(ps->iid_index[env], 34);
963 if (ps->icc_mode == 2 || ps->icc_mode == 5)
964 map34indexto20(ps->icc_index[env], 34);
966 /* disable ipd/opd */
967 for (bin = 0; bin < 17; bin++)
969 ps->aaIpdIndex[env][bin] = 0;
970 ps->aaOpdIndex[env][bin] = 0;
974 if (ps->use34hybrid_bands)
976 for (env = 0; env < ps->num_env; env++)
978 if (ps->iid_mode != 2 && ps->iid_mode != 5)
979 map20indexto34(ps->iid_index[env], 34);
980 if (ps->icc_mode != 2 && ps->icc_mode != 5)
981 map20indexto34(ps->icc_index[env], 34);
982 if (ps->ipd_mode != 2 && ps->ipd_mode != 5)
984 map20indexto34(ps->ipd_index[env], 17);
985 map20indexto34(ps->opd_index[env], 17);
992 for (env = 0; env < ps->num_env; env++)
994 printf("iid[env:%d]:", env);
995 for (bin = 0; bin < 34; bin++)
997 printf(" %d", ps->iid_index[env][bin]);
1001 for (env = 0; env < ps->num_env; env++)
1003 printf("icc[env:%d]:", env);
1004 for (bin = 0; bin < 34; bin++)
1006 printf(" %d", ps->icc_index[env][bin]);
1010 for (env = 0; env < ps->num_env; env++)
1012 printf("ipd[env:%d]:", env);
1013 for (bin = 0; bin < 17; bin++)
1015 printf(" %d", ps->ipd_index[env][bin]);
1019 for (env = 0; env < ps->num_env; env++)
1021 printf("opd[env:%d]:", env);
1022 for (bin = 0; bin < 17; bin++)
1024 printf(" %d", ps->opd_index[env][bin]);
1032 /* decorrelate the mono signal using an allpass filter */
1033 static void ps_decorrelate(ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64],
1034 qmf_t X_hybrid_left[32][32], qmf_t X_hybrid_right[32][32])
1036 uint8_t gr, n, m, bk;
1039 const complex_t *Phi_Fract_SubQmf;
1040 uint8_t temp_delay_ser[NO_ALLPASS_LINKS];
1041 real_t P_SmoothPeakDecayDiffNrg, nrg;
1043 real_t G_TransientRatio[32][34] = {{0}};
1044 complex_t inputLeft;
1047 /* chose hybrid filterbank: 20 or 34 band case */
1048 if (ps->use34hybrid_bands)
1050 Phi_Fract_SubQmf = Phi_Fract_SubQmf34;
1052 Phi_Fract_SubQmf = Phi_Fract_SubQmf20;
1055 /* clear the energy values */
1056 for (n = 0; n < 32; n++)
1058 for (bk = 0; bk < 34; bk++)
1064 /* calculate the energy in each parameter band b(k) */
1065 for (gr = 0; gr < ps->num_groups; gr++)
1067 /* select the parameter index b(k) to which this group belongs */
1068 bk = (~NEGATE_IPD_MASK) & ps->map_group2bk[gr];
1070 /* select the upper subband border for this group */
1071 maxsb = (gr < ps->num_hybrid_groups) ? ps->group_border[gr]+1 : ps->group_border[gr+1];
1073 for (sb = ps->group_border[gr]; sb < maxsb; sb++)
1075 for (n = ps->border_position[0]; n < ps->border_position[ps->num_env]; n++)
1078 uint32_t in_re, in_im;
1081 /* input from hybrid subbands or QMF subbands */
1082 if (gr < ps->num_hybrid_groups)
1084 RE(inputLeft) = QMF_RE(X_hybrid_left[n][sb]);
1085 IM(inputLeft) = QMF_IM(X_hybrid_left[n][sb]);
1087 RE(inputLeft) = QMF_RE(X_left[n][sb]);
1088 IM(inputLeft) = QMF_IM(X_left[n][sb]);
1091 /* accumulate energy */
1093 /* NOTE: all input is scaled by 2^(-5) because of fixed point QMF
1094 * meaning that P will be scaled by 2^(-10) compared to floating point version
1096 in_re = ((abs(RE(inputLeft))+(1<<(REAL_BITS-1)))>>REAL_BITS);
1097 in_im = ((abs(IM(inputLeft))+(1<<(REAL_BITS-1)))>>REAL_BITS);
1098 P[n][bk] += in_re*in_re + in_im*in_im;
1100 P[n][bk] += MUL_R(RE(inputLeft),RE(inputLeft)) + MUL_R(IM(inputLeft),IM(inputLeft));
1107 for (n = 0; n < 32; n++)
1109 for (bk = 0; bk < 34; bk++)
1112 printf("%d %d: %d\n", n, bk, P[n][bk] /*/(float)REAL_PRECISION*/);
1114 printf("%d %d: %f\n", n, bk, P[n][bk]/1024.0);
1120 /* calculate transient reduction ratio for each parameter band b(k) */
1121 for (bk = 0; bk < ps->nr_par_bands; bk++)
1123 for (n = ps->border_position[0]; n < ps->border_position[ps->num_env]; n++)
1125 const real_t gamma = COEF_CONST(1.5);
1127 ps->P_PeakDecayNrg[bk] = MUL_F(ps->P_PeakDecayNrg[bk], ps->alpha_decay);
1128 if (ps->P_PeakDecayNrg[bk] < P[n][bk])
1129 ps->P_PeakDecayNrg[bk] = P[n][bk];
1131 /* apply smoothing filter to peak decay energy */
1132 P_SmoothPeakDecayDiffNrg = ps->P_SmoothPeakDecayDiffNrg_prev[bk];
1133 P_SmoothPeakDecayDiffNrg += MUL_F((ps->P_PeakDecayNrg[bk] - P[n][bk] - ps->P_SmoothPeakDecayDiffNrg_prev[bk]), ps->alpha_smooth);
1134 ps->P_SmoothPeakDecayDiffNrg_prev[bk] = P_SmoothPeakDecayDiffNrg;
1136 /* apply smoothing filter to energy */
1137 nrg = ps->P_prev[bk];
1138 nrg += MUL_F((P[n][bk] - ps->P_prev[bk]), ps->alpha_smooth);
1139 ps->P_prev[bk] = nrg;
1141 /* calculate transient ratio */
1142 if (MUL_C(P_SmoothPeakDecayDiffNrg, gamma) <= nrg)
1144 G_TransientRatio[n][bk] = REAL_CONST(1.0);
1146 G_TransientRatio[n][bk] = DIV_R(nrg, (MUL_C(P_SmoothPeakDecayDiffNrg, gamma)));
1152 for (n = 0; n < 32; n++)
1154 for (bk = 0; bk < 34; bk++)
1157 printf("%d %d: %f\n", n, bk, G_TransientRatio[n][bk]/(float)REAL_PRECISION);
1159 printf("%d %d: %f\n", n, bk, G_TransientRatio[n][bk]);
1165 /* apply stereo decorrelation filter to the signal */
1166 for (gr = 0; gr < ps->num_groups; gr++)
1168 if (gr < ps->num_hybrid_groups)
1169 maxsb = ps->group_border[gr] + 1;
1171 maxsb = ps->group_border[gr + 1];
1174 for (sb = ps->group_border[gr]; sb < maxsb; sb++)
1176 real_t g_DecaySlope;
1177 real_t g_DecaySlope_filt[NO_ALLPASS_LINKS];
1179 /* g_DecaySlope: [0..1] */
1180 if (gr < ps->num_hybrid_groups || sb <= ps->decay_cutoff)
1182 g_DecaySlope = FRAC_CONST(1.0);
1184 int8_t decay = ps->decay_cutoff - sb;
1185 if (decay <= -20 /* -1/DECAY_SLOPE */)
1189 /* decay(int)*decay_slope(frac) = g_DecaySlope(frac) */
1190 g_DecaySlope = FRAC_CONST(1.0) + DECAY_SLOPE * decay;
1194 /* calculate g_DecaySlope_filt for every m multiplied by filter_a[m] */
1195 for (m = 0; m < NO_ALLPASS_LINKS; m++)
1197 g_DecaySlope_filt[m] = MUL_F(g_DecaySlope, filter_a[m]);
1201 /* set delay indices */
1202 temp_delay = ps->saved_delay;
1203 for (n = 0; n < NO_ALLPASS_LINKS; n++)
1204 temp_delay_ser[n] = ps->delay_buf_index_ser[n];
1206 for (n = ps->border_position[0]; n < ps->border_position[ps->num_env]; n++)
1208 complex_t tmp, tmp0, R0;
1210 if (gr < ps->num_hybrid_groups)
1212 /* hybrid filterbank input */
1213 RE(inputLeft) = QMF_RE(X_hybrid_left[n][sb]);
1214 IM(inputLeft) = QMF_IM(X_hybrid_left[n][sb]);
1216 /* QMF filterbank input */
1217 RE(inputLeft) = QMF_RE(X_left[n][sb]);
1218 IM(inputLeft) = QMF_IM(X_left[n][sb]);
1221 if (sb > ps->nr_allpass_bands && gr >= ps->num_hybrid_groups)
1225 /* never hybrid subbands here, always QMF subbands */
1226 RE(tmp) = RE(ps->delay_Qmf[ps->delay_buf_index_delay[sb]][sb]);
1227 IM(tmp) = IM(ps->delay_Qmf[ps->delay_buf_index_delay[sb]][sb]);
1230 RE(ps->delay_Qmf[ps->delay_buf_index_delay[sb]][sb]) = RE(inputLeft);
1231 IM(ps->delay_Qmf[ps->delay_buf_index_delay[sb]][sb]) = IM(inputLeft);
1233 /* allpass filter */
1235 complex_t Phi_Fract;
1237 /* fetch parameters */
1238 if (gr < ps->num_hybrid_groups)
1240 /* select data from the hybrid subbands */
1241 RE(tmp0) = RE(ps->delay_SubQmf[temp_delay][sb]);
1242 IM(tmp0) = IM(ps->delay_SubQmf[temp_delay][sb]);
1244 RE(ps->delay_SubQmf[temp_delay][sb]) = RE(inputLeft);
1245 IM(ps->delay_SubQmf[temp_delay][sb]) = IM(inputLeft);
1247 RE(Phi_Fract) = RE(Phi_Fract_SubQmf[sb]);
1248 IM(Phi_Fract) = IM(Phi_Fract_SubQmf[sb]);
1250 /* select data from the QMF subbands */
1251 RE(tmp0) = RE(ps->delay_Qmf[temp_delay][sb]);
1252 IM(tmp0) = IM(ps->delay_Qmf[temp_delay][sb]);
1254 RE(ps->delay_Qmf[temp_delay][sb]) = RE(inputLeft);
1255 IM(ps->delay_Qmf[temp_delay][sb]) = IM(inputLeft);
1257 RE(Phi_Fract) = RE(Phi_Fract_Qmf[sb]);
1258 IM(Phi_Fract) = IM(Phi_Fract_Qmf[sb]);
1261 /* z^(-2) * Phi_Fract[k] */
1262 ComplexMult(&RE(tmp), &IM(tmp), RE(tmp0), IM(tmp0), RE(Phi_Fract), IM(Phi_Fract));
1266 for (m = 0; m < NO_ALLPASS_LINKS; m++)
1268 complex_t Q_Fract_allpass, tmp2;
1270 /* fetch parameters */
1271 if (gr < ps->num_hybrid_groups)
1273 /* select data from the hybrid subbands */
1274 RE(tmp0) = RE(ps->delay_SubQmf_ser[m][temp_delay_ser[m]][sb]);
1275 IM(tmp0) = IM(ps->delay_SubQmf_ser[m][temp_delay_ser[m]][sb]);
1277 if (ps->use34hybrid_bands)
1279 RE(Q_Fract_allpass) = RE(Q_Fract_allpass_SubQmf34[sb][m]);
1280 IM(Q_Fract_allpass) = IM(Q_Fract_allpass_SubQmf34[sb][m]);
1282 RE(Q_Fract_allpass) = RE(Q_Fract_allpass_SubQmf20[sb][m]);
1283 IM(Q_Fract_allpass) = IM(Q_Fract_allpass_SubQmf20[sb][m]);
1286 /* select data from the QMF subbands */
1287 RE(tmp0) = RE(ps->delay_Qmf_ser[m][temp_delay_ser[m]][sb]);
1288 IM(tmp0) = IM(ps->delay_Qmf_ser[m][temp_delay_ser[m]][sb]);
1290 RE(Q_Fract_allpass) = RE(Q_Fract_allpass_Qmf[sb][m]);
1291 IM(Q_Fract_allpass) = IM(Q_Fract_allpass_Qmf[sb][m]);
1294 /* delay by a fraction */
1295 /* z^(-d(m)) * Q_Fract_allpass[k,m] */
1296 ComplexMult(&RE(tmp), &IM(tmp), RE(tmp0), IM(tmp0), RE(Q_Fract_allpass), IM(Q_Fract_allpass));
1298 /* -a(m) * g_DecaySlope[k] */
1299 RE(tmp) += -MUL_F(g_DecaySlope_filt[m], RE(R0));
1300 IM(tmp) += -MUL_F(g_DecaySlope_filt[m], IM(R0));
1302 /* -a(m) * g_DecaySlope[k] * Q_Fract_allpass[k,m] * z^(-d(m)) */
1303 RE(tmp2) = RE(R0) + MUL_F(g_DecaySlope_filt[m], RE(tmp));
1304 IM(tmp2) = IM(R0) + MUL_F(g_DecaySlope_filt[m], IM(tmp));
1307 if (gr < ps->num_hybrid_groups)
1309 RE(ps->delay_SubQmf_ser[m][temp_delay_ser[m]][sb]) = RE(tmp2);
1310 IM(ps->delay_SubQmf_ser[m][temp_delay_ser[m]][sb]) = IM(tmp2);
1312 RE(ps->delay_Qmf_ser[m][temp_delay_ser[m]][sb]) = RE(tmp2);
1313 IM(ps->delay_Qmf_ser[m][temp_delay_ser[m]][sb]) = IM(tmp2);
1316 /* store for next iteration (or as output value if last iteration) */
1322 /* select b(k) for reading the transient ratio */
1323 bk = (~NEGATE_IPD_MASK) & ps->map_group2bk[gr];
1325 /* duck if a past transient is found */
1326 RE(R0) = MUL_R(G_TransientRatio[n][bk], RE(R0));
1327 IM(R0) = MUL_R(G_TransientRatio[n][bk], IM(R0));
1329 if (gr < ps->num_hybrid_groups)
1332 QMF_RE(X_hybrid_right[n][sb]) = RE(R0);
1333 QMF_IM(X_hybrid_right[n][sb]) = IM(R0);
1336 QMF_RE(X_right[n][sb]) = RE(R0);
1337 QMF_IM(X_right[n][sb]) = IM(R0);
1340 /* Update delay buffer index */
1341 if (++temp_delay >= 2)
1346 /* update delay indices */
1347 if (sb > ps->nr_allpass_bands && gr >= ps->num_hybrid_groups)
1349 /* delay_D depends on the samplerate, it can hold the values 14 and 1 */
1350 if (++ps->delay_buf_index_delay[sb] >= ps->delay_D[sb])
1352 ps->delay_buf_index_delay[sb] = 0;
1356 for (m = 0; m < NO_ALLPASS_LINKS; m++)
1358 if (++temp_delay_ser[m] >= ps->num_sample_delay_ser[m])
1360 temp_delay_ser[m] = 0;
1367 /* update delay indices */
1368 ps->saved_delay = temp_delay;
1369 for (m = 0; m < NO_ALLPASS_LINKS; m++)
1370 ps->delay_buf_index_ser[m] = temp_delay_ser[m];
1374 #define step(shift) \
1375 if ((0x40000000l >> shift) + root <= value) \
1377 value -= (0x40000000l >> shift) + root; \
1378 root = (root >> 1) | (0x40000000l >> shift); \
1383 /* fixed point square root approximation */
1384 static real_t ps_sqrt(real_t value)
1388 step( 0); step( 2); step( 4); step( 6);
1389 step( 8); step(10); step(12); step(14);
1390 step(16); step(18); step(20); step(22);
1391 step(24); step(26); step(28); step(30);
1396 root <<= (REAL_BITS/2);
1401 #define ps_sqrt(A) sqrt(A)
1404 static const real_t ipdopd_cos_tab[] = {
1405 FRAC_CONST(1.000000000000000),
1406 FRAC_CONST(0.707106781186548),
1407 FRAC_CONST(0.000000000000000),
1408 FRAC_CONST(-0.707106781186547),
1409 FRAC_CONST(-1.000000000000000),
1410 FRAC_CONST(-0.707106781186548),
1411 FRAC_CONST(-0.000000000000000),
1412 FRAC_CONST(0.707106781186547),
1413 FRAC_CONST(1.000000000000000)
1416 static const real_t ipdopd_sin_tab[] = {
1417 FRAC_CONST(0.000000000000000),
1418 FRAC_CONST(0.707106781186547),
1419 FRAC_CONST(1.000000000000000),
1420 FRAC_CONST(0.707106781186548),
1421 FRAC_CONST(0.000000000000000),
1422 FRAC_CONST(-0.707106781186547),
1423 FRAC_CONST(-1.000000000000000),
1424 FRAC_CONST(-0.707106781186548),
1425 FRAC_CONST(-0.000000000000000)
1428 static real_t magnitude_c(complex_t c)
1431 #define ps_abs(A) (((A) > 0) ? (A) : (-(A)))
1432 #define ALPHA FRAC_CONST(0.948059448969)
1433 #define BETA FRAC_CONST(0.392699081699)
1435 real_t abs_inphase = ps_abs(RE(c));
1436 real_t abs_quadrature = ps_abs(IM(c));
1438 if (abs_inphase > abs_quadrature) {
1439 return MUL_F(abs_inphase, ALPHA) + MUL_F(abs_quadrature, BETA);
1441 return MUL_F(abs_quadrature, ALPHA) + MUL_F(abs_inphase, BETA);
1444 return sqrt(RE(c)*RE(c) + IM(c)*IM(c));
1448 static void ps_mix_phase(ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64],
1449 qmf_t X_hybrid_left[32][32], qmf_t X_hybrid_right[32][32])
1456 uint8_t nr_ipdopd_par;
1457 complex_t h11, h12, h21, h22;
1458 complex_t H11, H12, H21, H22;
1459 complex_t deltaH11, deltaH12, deltaH21, deltaH22;
1461 complex_t tempRight;
1462 complex_t phaseLeft;
1463 complex_t phaseRight;
1465 const real_t *sf_iid;
1466 uint8_t no_iid_steps;
1468 if (ps->iid_mode >= 3)
1471 sf_iid = sf_iid_fine;
1474 sf_iid = sf_iid_normal;
1477 if (ps->ipd_mode == 0 || ps->ipd_mode == 3)
1479 nr_ipdopd_par = 11; /* resolution */
1481 nr_ipdopd_par = ps->nr_ipdopd_par;
1484 for (gr = 0; gr < ps->num_groups; gr++)
1486 bk = (~NEGATE_IPD_MASK) & ps->map_group2bk[gr];
1488 /* use one channel per group in the subqmf domain */
1489 maxsb = (gr < ps->num_hybrid_groups) ? ps->group_border[gr] + 1 : ps->group_border[gr + 1];
1491 for (env = 0; env < ps->num_env; env++)
1493 if (ps->icc_mode < 3)
1495 /* type 'A' mixing as described in 8.6.4.6.2.1 */
1503 c_1 = sqrt(2.0 / (1.0 + pow(10.0, quant_iid[no_iid_steps + iid_index] / 10.0)));
1504 c_2 = sqrt(2.0 / (1.0 + pow(10.0, quant_iid[no_iid_steps - iid_index] / 10.0)));
1505 alpha = 0.5 * acos(quant_rho[icc_index]);
1506 beta = alpha * ( c_1 - c_2 ) / sqrt(2.0);
1509 //printf("%d\n", ps->iid_index[env][bk]);
1511 /* calculate the scalefactors c_1 and c_2 from the intensity differences */
1512 c_1 = sf_iid[no_iid_steps + ps->iid_index[env][bk]];
1513 c_2 = sf_iid[no_iid_steps - ps->iid_index[env][bk]];
1515 /* calculate alpha and beta using the ICC parameters */
1516 cosa = cos_alphas[ps->icc_index[env][bk]];
1517 sina = sin_alphas[ps->icc_index[env][bk]];
1519 if (ps->iid_mode >= 3)
1521 if (ps->iid_index[env][bk] < 0)
1523 cosb = cos_betas_fine[-ps->iid_index[env][bk]][ps->icc_index[env][bk]];
1524 sinb = -sin_betas_fine[-ps->iid_index[env][bk]][ps->icc_index[env][bk]];
1526 cosb = cos_betas_fine[ps->iid_index[env][bk]][ps->icc_index[env][bk]];
1527 sinb = sin_betas_fine[ps->iid_index[env][bk]][ps->icc_index[env][bk]];
1530 if (ps->iid_index[env][bk] < 0)
1532 cosb = cos_betas_normal[-ps->iid_index[env][bk]][ps->icc_index[env][bk]];
1533 sinb = -sin_betas_normal[-ps->iid_index[env][bk]][ps->icc_index[env][bk]];
1535 cosb = cos_betas_normal[ps->iid_index[env][bk]][ps->icc_index[env][bk]];
1536 sinb = sin_betas_normal[ps->iid_index[env][bk]][ps->icc_index[env][bk]];
1540 ab1 = MUL_C(cosb, cosa);
1541 ab2 = MUL_C(sinb, sina);
1542 ab3 = MUL_C(sinb, cosa);
1543 ab4 = MUL_C(cosb, sina);
1546 RE(h11) = MUL_C(c_2, (ab1 - ab2));
1547 RE(h12) = MUL_C(c_1, (ab1 + ab2));
1548 RE(h21) = MUL_C(c_2, (ab3 + ab4));
1549 RE(h22) = MUL_C(c_1, (ab3 - ab4));
1551 /* type 'B' mixing as described in 8.6.4.6.2.2 */
1556 real_t c, rho, mu, alpha, gamma;
1559 i = ps->iid_index[env][bk];
1560 c = (real_t)pow(10.0, ((i)?(((i>0)?1:-1)*quant_iid[((i>0)?i:-i)-1]):0.)/20.0);
1561 rho = quant_rho[ps->icc_index[env][bk]];
1563 if (rho == 0.0f && c == 1.)
1565 alpha = (real_t)M_PI/4.0f;
1572 alpha = 0.5f*(real_t)atan( (2.0f*c*rho) / (c*c-1.0f) );
1576 alpha += (real_t)M_PI/2.0f;
1580 alpha += (real_t)M_PI;
1584 mu = 1+(4.0f*rho*rho-4.0f)/(mu*mu);
1585 gamma = (real_t)atan(sqrt((1.0f-sqrt(mu))/(1.0f+sqrt(mu))));
1588 if (ps->iid_mode >= 3)
1590 uint8_t abs_iid = abs(ps->iid_index[env][bk]);
1592 cosa = sincos_alphas_B_fine[no_iid_steps + ps->iid_index[env][bk]][ps->icc_index[env][bk]];
1593 sina = sincos_alphas_B_fine[30 - (no_iid_steps + ps->iid_index[env][bk])][ps->icc_index[env][bk]];
1594 cosg = cos_gammas_fine[abs_iid][ps->icc_index[env][bk]];
1595 sing = sin_gammas_fine[abs_iid][ps->icc_index[env][bk]];
1597 uint8_t abs_iid = abs(ps->iid_index[env][bk]);
1599 cosa = sincos_alphas_B_normal[no_iid_steps + ps->iid_index[env][bk]][ps->icc_index[env][bk]];
1600 sina = sincos_alphas_B_normal[14 - (no_iid_steps + ps->iid_index[env][bk])][ps->icc_index[env][bk]];
1601 cosg = cos_gammas_normal[abs_iid][ps->icc_index[env][bk]];
1602 sing = sin_gammas_normal[abs_iid][ps->icc_index[env][bk]];
1605 RE(h11) = MUL_C(COEF_SQRT2, MUL_C(cosa, cosg));
1606 RE(h12) = MUL_C(COEF_SQRT2, MUL_C(sina, cosg));
1607 RE(h21) = MUL_C(COEF_SQRT2, MUL_C(-cosa, sing));
1608 RE(h22) = MUL_C(COEF_SQRT2, MUL_C(sina, sing));
1611 /* calculate phase rotation parameters H_xy */
1612 /* note that the imaginary part of these parameters are only calculated when
1613 IPD and OPD are enabled
1615 if ((ps->enable_ipdopd) && (bk < nr_ipdopd_par))
1618 real_t xy, pq, xypq;
1620 /* ringbuffer index */
1623 /* previous value */
1625 /* divide by 4, shift right 2 bits */
1626 RE(tempLeft) = RE(ps->ipd_prev[bk][i]) >> 2;
1627 IM(tempLeft) = IM(ps->ipd_prev[bk][i]) >> 2;
1628 RE(tempRight) = RE(ps->opd_prev[bk][i]) >> 2;
1629 IM(tempRight) = IM(ps->opd_prev[bk][i]) >> 2;
1631 RE(tempLeft) = MUL_F(RE(ps->ipd_prev[bk][i]), FRAC_CONST(0.25));
1632 IM(tempLeft) = MUL_F(IM(ps->ipd_prev[bk][i]), FRAC_CONST(0.25));
1633 RE(tempRight) = MUL_F(RE(ps->opd_prev[bk][i]), FRAC_CONST(0.25));
1634 IM(tempRight) = MUL_F(IM(ps->opd_prev[bk][i]), FRAC_CONST(0.25));
1637 /* save current value */
1638 RE(ps->ipd_prev[bk][i]) = ipdopd_cos_tab[abs(ps->ipd_index[env][bk])];
1639 IM(ps->ipd_prev[bk][i]) = ipdopd_sin_tab[abs(ps->ipd_index[env][bk])];
1640 RE(ps->opd_prev[bk][i]) = ipdopd_cos_tab[abs(ps->opd_index[env][bk])];
1641 IM(ps->opd_prev[bk][i]) = ipdopd_sin_tab[abs(ps->opd_index[env][bk])];
1643 /* add current value */
1644 RE(tempLeft) += RE(ps->ipd_prev[bk][i]);
1645 IM(tempLeft) += IM(ps->ipd_prev[bk][i]);
1646 RE(tempRight) += RE(ps->opd_prev[bk][i]);
1647 IM(tempRight) += IM(ps->opd_prev[bk][i]);
1649 /* ringbuffer index */
1656 /* get value before previous */
1658 /* dividing by 2, shift right 1 bit */
1659 RE(tempLeft) += (RE(ps->ipd_prev[bk][i]) >> 1);
1660 IM(tempLeft) += (IM(ps->ipd_prev[bk][i]) >> 1);
1661 RE(tempRight) += (RE(ps->opd_prev[bk][i]) >> 1);
1662 IM(tempRight) += (IM(ps->opd_prev[bk][i]) >> 1);
1664 RE(tempLeft) += MUL_F(RE(ps->ipd_prev[bk][i]), FRAC_CONST(0.5));
1665 IM(tempLeft) += MUL_F(IM(ps->ipd_prev[bk][i]), FRAC_CONST(0.5));
1666 RE(tempRight) += MUL_F(RE(ps->opd_prev[bk][i]), FRAC_CONST(0.5));
1667 IM(tempRight) += MUL_F(IM(ps->opd_prev[bk][i]), FRAC_CONST(0.5));
1670 #if 0 /* original code */
1671 ipd = (float)atan2(IM(tempLeft), RE(tempLeft));
1672 opd = (float)atan2(IM(tempRight), RE(tempRight));
1674 /* phase rotation */
1675 RE(phaseLeft) = (float)cos(opd);
1676 IM(phaseLeft) = (float)sin(opd);
1678 RE(phaseRight) = (float)cos(opd);
1679 IM(phaseRight) = (float)sin(opd);
1684 // p = IM(tempRight)
1685 // q = RE(tempRight)
1686 // cos(atan2(x,y)) = y/sqrt((x*x) + (y*y))
1687 // sin(atan2(x,y)) = x/sqrt((x*x) + (y*y))
1688 // cos(atan2(x,y)-atan2(p,q)) = (y*q + x*p) / ( sqrt((x*x) + (y*y)) * sqrt((p*p) + (q*q)) );
1689 // sin(atan2(x,y)-atan2(p,q)) = (x*q - y*p) / ( sqrt((x*x) + (y*y)) * sqrt((p*p) + (q*q)) );
1691 xy = magnitude_c(tempRight);
1692 pq = magnitude_c(tempLeft);
1696 RE(phaseLeft) = DIV_R(RE(tempRight), xy);
1697 IM(phaseLeft) = DIV_R(IM(tempRight), xy);
1703 xypq = MUL_R(xy, pq);
1707 real_t tmp1 = MUL_R(RE(tempRight), RE(tempLeft)) + MUL_R(IM(tempRight), IM(tempLeft));
1708 real_t tmp2 = MUL_R(IM(tempRight), RE(tempLeft)) - MUL_R(RE(tempRight), IM(tempLeft));
1710 RE(phaseRight) = DIV_R(tmp1, xypq);
1711 IM(phaseRight) = DIV_R(tmp2, xypq);
1719 /* MUL_F(COEF, REAL) = COEF */
1720 IM(h11) = MUL_R(RE(h11), IM(phaseLeft));
1721 IM(h12) = MUL_R(RE(h12), IM(phaseRight));
1722 IM(h21) = MUL_R(RE(h21), IM(phaseLeft));
1723 IM(h22) = MUL_R(RE(h22), IM(phaseRight));
1725 RE(h11) = MUL_R(RE(h11), RE(phaseLeft));
1726 RE(h12) = MUL_R(RE(h12), RE(phaseRight));
1727 RE(h21) = MUL_R(RE(h21), RE(phaseLeft));
1728 RE(h22) = MUL_R(RE(h22), RE(phaseRight));
1731 /* length of the envelope n_e+1 - n_e (in time samples) */
1732 /* 0 < L <= 32: integer */
1733 L = (real_t)(ps->border_position[env + 1] - ps->border_position[env]);
1735 /* obtain final H_xy by means of linear interpolation */
1736 RE(deltaH11) = (RE(h11) - RE(ps->h11_prev[gr])) / L;
1737 RE(deltaH12) = (RE(h12) - RE(ps->h12_prev[gr])) / L;
1738 RE(deltaH21) = (RE(h21) - RE(ps->h21_prev[gr])) / L;
1739 RE(deltaH22) = (RE(h22) - RE(ps->h22_prev[gr])) / L;
1741 RE(H11) = RE(ps->h11_prev[gr]);
1742 RE(H12) = RE(ps->h12_prev[gr]);
1743 RE(H21) = RE(ps->h21_prev[gr]);
1744 RE(H22) = RE(ps->h22_prev[gr]);
1746 RE(ps->h11_prev[gr]) = RE(h11);
1747 RE(ps->h12_prev[gr]) = RE(h12);
1748 RE(ps->h21_prev[gr]) = RE(h21);
1749 RE(ps->h22_prev[gr]) = RE(h22);
1751 /* only calculate imaginary part when needed */
1752 if ((ps->enable_ipdopd) && (bk < nr_ipdopd_par))
1754 /* obtain final H_xy by means of linear interpolation */
1755 IM(deltaH11) = (IM(h11) - IM(ps->h11_prev[gr])) / L;
1756 IM(deltaH12) = (IM(h12) - IM(ps->h12_prev[gr])) / L;
1757 IM(deltaH21) = (IM(h21) - IM(ps->h21_prev[gr])) / L;
1758 IM(deltaH22) = (IM(h22) - IM(ps->h22_prev[gr])) / L;
1760 IM(H11) = IM(ps->h11_prev[gr]);
1761 IM(H12) = IM(ps->h12_prev[gr]);
1762 IM(H21) = IM(ps->h21_prev[gr]);
1763 IM(H22) = IM(ps->h22_prev[gr]);
1765 if ((NEGATE_IPD_MASK & ps->map_group2bk[gr]) != 0)
1767 IM(deltaH11) = -IM(deltaH11);
1768 IM(deltaH12) = -IM(deltaH12);
1769 IM(deltaH21) = -IM(deltaH21);
1770 IM(deltaH22) = -IM(deltaH22);
1778 IM(ps->h11_prev[gr]) = IM(h11);
1779 IM(ps->h12_prev[gr]) = IM(h12);
1780 IM(ps->h21_prev[gr]) = IM(h21);
1781 IM(ps->h22_prev[gr]) = IM(h22);
1784 /* apply H_xy to the current envelope band of the decorrelated subband */
1785 for (n = ps->border_position[env]; n < ps->border_position[env + 1]; n++)
1787 /* addition finalises the interpolation over every n */
1788 RE(H11) += RE(deltaH11);
1789 RE(H12) += RE(deltaH12);
1790 RE(H21) += RE(deltaH21);
1791 RE(H22) += RE(deltaH22);
1792 if ((ps->enable_ipdopd) && (bk < nr_ipdopd_par))
1794 IM(H11) += IM(deltaH11);
1795 IM(H12) += IM(deltaH12);
1796 IM(H21) += IM(deltaH21);
1797 IM(H22) += IM(deltaH22);
1800 /* channel is an alias to the subband */
1801 for (sb = ps->group_border[gr]; sb < maxsb; sb++)
1803 complex_t inLeft, inRight;
1805 /* load decorrelated samples */
1806 if (gr < ps->num_hybrid_groups)
1808 RE(inLeft) = RE(X_hybrid_left[n][sb]);
1809 IM(inLeft) = IM(X_hybrid_left[n][sb]);
1810 RE(inRight) = RE(X_hybrid_right[n][sb]);
1811 IM(inRight) = IM(X_hybrid_right[n][sb]);
1813 RE(inLeft) = RE(X_left[n][sb]);
1814 IM(inLeft) = IM(X_left[n][sb]);
1815 RE(inRight) = RE(X_right[n][sb]);
1816 IM(inRight) = IM(X_right[n][sb]);
1820 RE(tempLeft) = MUL_C(RE(H11), RE(inLeft)) + MUL_C(RE(H21), RE(inRight));
1821 IM(tempLeft) = MUL_C(RE(H11), IM(inLeft)) + MUL_C(RE(H21), IM(inRight));
1822 RE(tempRight) = MUL_C(RE(H12), RE(inLeft)) + MUL_C(RE(H22), RE(inRight));
1823 IM(tempRight) = MUL_C(RE(H12), IM(inLeft)) + MUL_C(RE(H22), IM(inRight));
1825 /* only perform imaginary operations when needed */
1826 if ((ps->enable_ipdopd) && (bk < nr_ipdopd_par))
1828 /* apply rotation */
1829 RE(tempLeft) -= MUL_C(IM(H11), IM(inLeft)) + MUL_C(IM(H21), IM(inRight));
1830 IM(tempLeft) += MUL_C(IM(H11), RE(inLeft)) + MUL_C(IM(H21), RE(inRight));
1831 RE(tempRight) -= MUL_C(IM(H12), IM(inLeft)) + MUL_C(IM(H22), IM(inRight));
1832 IM(tempRight) += MUL_C(IM(H12), RE(inLeft)) + MUL_C(IM(H22), RE(inRight));
1835 /* store final samples */
1836 if (gr < ps->num_hybrid_groups)
1838 RE(X_hybrid_left[n][sb]) = RE(tempLeft);
1839 IM(X_hybrid_left[n][sb]) = IM(tempLeft);
1840 RE(X_hybrid_right[n][sb]) = RE(tempRight);
1841 IM(X_hybrid_right[n][sb]) = IM(tempRight);
1843 RE(X_left[n][sb]) = RE(tempLeft);
1844 IM(X_left[n][sb]) = IM(tempLeft);
1845 RE(X_right[n][sb]) = RE(tempRight);
1846 IM(X_right[n][sb]) = IM(tempRight);
1851 /* shift phase smoother's circular buffer index */
1853 if (ps->phase_hist == 2)
1861 void ps_free(ps_info *ps)
1863 /* free hybrid filterbank structures */
1864 hybrid_free(ps->hyb);
1869 ps_info *ps_init(uint8_t sr_index, uint8_t numTimeSlotsRate)
1872 uint8_t short_delay_band;
1874 ps_info *ps = (ps_info*)faad_malloc(sizeof(ps_info));
1875 memset(ps, 0, sizeof(ps_info));
1877 ps->hyb = hybrid_init(numTimeSlotsRate);
1878 ps->numTimeSlotsRate = numTimeSlotsRate;
1880 ps->ps_data_available = 0;
1883 ps->saved_delay = 0;
1885 for (i = 0; i < 64; i++)
1887 ps->delay_buf_index_delay[i] = 0;
1890 for (i = 0; i < NO_ALLPASS_LINKS; i++)
1892 ps->delay_buf_index_ser[i] = 0;
1894 if (sr_index <= 5) /* >= 32 kHz*/
1896 ps->num_sample_delay_ser[i] = delay_length_d[1][i];
1898 ps->num_sample_delay_ser[i] = delay_length_d[0][i];
1901 /* THESE ARE CONSTANTS NOW */
1902 ps->num_sample_delay_ser[i] = delay_length_d[i];
1907 if (sr_index <= 5) /* >= 32 kHz*/
1909 short_delay_band = 35;
1910 ps->nr_allpass_bands = 22;
1911 ps->alpha_decay = FRAC_CONST(0.76592833836465);
1912 ps->alpha_smooth = FRAC_CONST(0.25);
1914 short_delay_band = 64;
1915 ps->nr_allpass_bands = 45;
1916 ps->alpha_decay = FRAC_CONST(0.58664621951003);
1917 ps->alpha_smooth = FRAC_CONST(0.6);
1920 /* THESE ARE CONSTANTS NOW */
1921 short_delay_band = 35;
1922 ps->nr_allpass_bands = 22;
1923 ps->alpha_decay = FRAC_CONST(0.76592833836465);
1924 ps->alpha_smooth = FRAC_CONST(0.25);
1927 /* THESE ARE CONSTANT NOW IF PS IS INDEPENDANT OF SAMPLERATE */
1928 for (i = 0; i < short_delay_band; i++)
1930 ps->delay_D[i] = 14;
1932 for (i = short_delay_band; i < 64; i++)
1937 /* mixing and phase */
1938 for (i = 0; i < 50; i++)
1940 RE(ps->h11_prev[i]) = 1;
1941 IM(ps->h12_prev[i]) = 1;
1942 RE(ps->h11_prev[i]) = 1;
1943 IM(ps->h12_prev[i]) = 1;
1948 for (i = 0; i < 20; i++)
1950 RE(ps->ipd_prev[i][0]) = 0;
1951 IM(ps->ipd_prev[i][0]) = 0;
1952 RE(ps->ipd_prev[i][1]) = 0;
1953 IM(ps->ipd_prev[i][1]) = 0;
1954 RE(ps->opd_prev[i][0]) = 0;
1955 IM(ps->opd_prev[i][0]) = 0;
1956 RE(ps->opd_prev[i][1]) = 0;
1957 IM(ps->opd_prev[i][1]) = 0;
1963 /* main Parametric Stereo decoding function */
1964 uint8_t ps_decode(ps_info *ps, qmf_t X_left[38][64], qmf_t X_right[38][64])
1966 qmf_t X_hybrid_left[32][32] = {{0}};
1967 qmf_t X_hybrid_right[32][32] = {{0}};
1969 /* delta decoding of the bitstream data */
1972 /* set up some parameters depending on filterbank type */
1973 if (ps->use34hybrid_bands)
1975 ps->group_border = (uint8_t*)group_border34;
1976 ps->map_group2bk = (uint16_t*)map_group2bk34;
1977 ps->num_groups = 32+18;
1978 ps->num_hybrid_groups = 32;
1979 ps->nr_par_bands = 34;
1980 ps->decay_cutoff = 5;
1982 ps->group_border = (uint8_t*)group_border20;
1983 ps->map_group2bk = (uint16_t*)map_group2bk20;
1984 ps->num_groups = 10+12;
1985 ps->num_hybrid_groups = 10;
1986 ps->nr_par_bands = 20;
1987 ps->decay_cutoff = 3;
1990 /* Perform further analysis on the lowest subbands to get a higher
1991 * frequency resolution
1993 hybrid_analysis((hyb_info*)ps->hyb, X_left, X_hybrid_left,
1994 ps->use34hybrid_bands, ps->numTimeSlotsRate);
1996 /* decorrelate mono signal */
1997 ps_decorrelate(ps, X_left, X_right, X_hybrid_left, X_hybrid_right);
1999 /* apply mixing and phase parameters */
2000 ps_mix_phase(ps, X_left, X_right, X_hybrid_left, X_hybrid_right);
2002 /* hybrid synthesis, to rebuild the SBR QMF matrices */
2003 hybrid_synthesis((hyb_info*)ps->hyb, X_left, X_hybrid_left,
2004 ps->use34hybrid_bands, ps->numTimeSlotsRate);
2006 hybrid_synthesis((hyb_info*)ps->hyb, X_right, X_hybrid_right,
2007 ps->use34hybrid_bands, ps->numTimeSlotsRate);