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: ic_predict.c,v 1.28 2007/11/01 12:33:31 menno Exp $
41 static void flt_round(float32_t *pf)
44 uint32_t tmp, tmp1, tmp2;
47 flg = tmp & (uint32_t)0x00008000;
48 tmp &= (uint32_t)0xffff0000;
50 /* round 1/2 lsb toward infinity */
53 tmp &= (uint32_t)0xff800000; /* extract exponent and sign */
54 tmp |= (uint32_t)0x00010000; /* insert 1 lsb */
55 tmp2 = tmp; /* add 1 lsb and elided one */
56 tmp &= (uint32_t)0xff800000; /* extract exponent and sign */
58 *pf = *(float32_t*)&tmp1 + *(float32_t*)&tmp2 - *(float32_t*)&tmp;
60 *pf = *(float32_t*)&tmp;
64 static int16_t quant_pred(float32_t x)
67 uint32_t *tmp = (uint32_t*)&x;
69 q = (int16_t)(*tmp>>16);
74 static float32_t inv_quant_pred(int16_t q)
77 uint32_t *tmp = (uint32_t*)&x;
78 *tmp = ((uint32_t)q)<<16;
83 static void ic_predict(pred_state *state, real_t input, real_t *output, uint8_t pred)
88 float32_t predictedvalue;
96 r[0] = inv_quant_pred(state->r[0]);
97 r[1] = inv_quant_pred(state->r[1]);
98 COR[0] = inv_quant_pred(state->COR[0]);
99 COR[1] = inv_quant_pred(state->COR[1]);
100 VAR[0] = inv_quant_pred(state->VAR[0]);
101 VAR[1] = inv_quant_pred(state->VAR[1]);
111 k1 = COR[0] * exp_table[j] * mnt_table[i];
122 if (c == 0 || v <= 1)
142 k2 = COR[1] * exp_table[j] * mnt_table[i];
152 if (c == 0 || v <= 1)
162 predictedvalue = k1*r[0] + k2*r[1];
163 flt_round(&predictedvalue);
164 *output = input + predictedvalue;
167 /* calculate new state data */
172 VAR[0] = ALPHA*VAR[0] + 0.5f * (r[0]*r[0] + e0*e0);
173 COR[0] = ALPHA*COR[0] + r[0]*e0;
174 VAR[1] = ALPHA*VAR[1] + 0.5f * (r[1]*r[1] + e1*e1);
175 COR[1] = ALPHA*COR[1] + r[1]*e1;
177 r[1] = A * (r[0]-dr1);
180 state->r[0] = quant_pred(r[0]);
181 state->r[1] = quant_pred(r[1]);
182 state->COR[0] = quant_pred(COR[0]);
183 state->COR[1] = quant_pred(COR[1]);
184 state->VAR[0] = quant_pred(VAR[0]);
185 state->VAR[1] = quant_pred(VAR[1]);
188 static void reset_pred_state(pred_state *state)
194 state->VAR[0] = 0x3F80;
195 state->VAR[1] = 0x3F80;
198 void pns_reset_pred_state(ic_stream *ics, pred_state *state)
201 uint16_t i, offs, offs2;
203 /* prediction only for long blocks */
204 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
207 for (g = 0; g < ics->num_window_groups; g++)
209 for (b = 0; b < ics->window_group_length[g]; b++)
211 for (sfb = 0; sfb < ics->max_sfb; sfb++)
213 if (is_noise(ics, g, sfb))
215 offs = ics->swb_offset[sfb];
216 offs2 = min(ics->swb_offset[sfb+1], ics->swb_offset_max);
218 for (i = offs; i < offs2; i++)
219 reset_pred_state(&state[i]);
226 void reset_all_predictors(pred_state *state, uint16_t frame_len)
230 for (i = 0; i < frame_len; i++)
231 reset_pred_state(&state[i]);
234 /* intra channel prediction */
235 void ic_prediction(ic_stream *ics, real_t *spec, pred_state *state,
236 uint16_t frame_len, uint8_t sf_index)
241 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
243 reset_all_predictors(state, frame_len);
245 for (sfb = 0; sfb < max_pred_sfb(sf_index); sfb++)
247 uint16_t low = ics->swb_offset[sfb];
248 uint16_t high = min(ics->swb_offset[sfb+1], ics->swb_offset_max);
250 for (bin = low; bin < high; bin++)
252 ic_predict(&state[bin], spec[bin], &spec[bin],
253 (ics->predictor_data_present && ics->pred.prediction_used[sfb]));
257 if (ics->predictor_data_present)
259 if (ics->pred.predictor_reset)
261 for (bin = ics->pred.predictor_reset_group_number - 1;
262 bin < frame_len; bin += 30)
264 reset_pred_state(&state[bin]);