summaryrefslogtreecommitdiff
path: root/audio_codec/libfaad/ic_predict.c (plain)
blob: a85247a3032060e01a1ceebc4f76b21f09ae6098
1/*
2** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
4**
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.
9**
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.
14**
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.
18**
19** Any non-GPL usage of this software or parts of this software is strictly
20** forbidden.
21**
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"
24**
25** Commercial non-GPL licensing of this software is possible.
26** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
27**
28** $Id: ic_predict.c,v 1.28 2007/11/01 12:33:31 menno Exp $
29**/
30#include <stdlib.h>
31#include "common.h"
32#include "structs.h"
33
34#ifdef MAIN_DEC
35
36#include "syntax.h"
37#include "ic_predict.h"
38#include "pns.h"
39
40
41static void flt_round(float32_t *pf)
42{
43 int32_t flg;
44 uint32_t tmp, tmp1, tmp2;
45
46 tmp = *(uint32_t*)pf;
47 flg = tmp & (uint32_t)0x00008000;
48 tmp &= (uint32_t)0xffff0000;
49 tmp1 = tmp;
50 /* round 1/2 lsb toward infinity */
51 if (flg) {
52 tmp &= (uint32_t)0xff800000; /* extract exponent and sign */
53 tmp |= (uint32_t)0x00010000; /* insert 1 lsb */
54 tmp2 = tmp; /* add 1 lsb and elided one */
55 tmp &= (uint32_t)0xff800000; /* extract exponent and sign */
56
57 *pf = *(float32_t*)&tmp1 + *(float32_t*)&tmp2 - *(float32_t*)&tmp;
58 } else {
59 *pf = *(float32_t*)&tmp;
60 }
61}
62
63static int16_t quant_pred(float32_t x)
64{
65 int16_t q;
66 uint32_t *tmp = (uint32_t*)&x;
67
68 q = (int16_t)(*tmp >> 16);
69
70 return q;
71}
72
73static float32_t inv_quant_pred(int16_t q)
74{
75 float32_t x;
76 uint32_t *tmp = (uint32_t*)&x;
77 *tmp = ((uint32_t)q) << 16;
78
79 return x;
80}
81
82static void ic_predict(pred_state *state, real_t input, real_t *output, uint8_t pred)
83{
84 uint16_t tmp;
85 int16_t i, j;
86 real_t dr1;
87 float32_t predictedvalue;
88 real_t e0, e1;
89 real_t k1, k2;
90
91 real_t r[2];
92 real_t COR[2];
93 real_t VAR[2];
94
95 r[0] = inv_quant_pred(state->r[0]);
96 r[1] = inv_quant_pred(state->r[1]);
97 COR[0] = inv_quant_pred(state->COR[0]);
98 COR[1] = inv_quant_pred(state->COR[1]);
99 VAR[0] = inv_quant_pred(state->VAR[0]);
100 VAR[1] = inv_quant_pred(state->VAR[1]);
101
102
103#if 1
104 tmp = state->VAR[0];
105 j = (tmp >> 7);
106 i = tmp & 0x7f;
107 if (j >= 128) {
108 j -= 128;
109 k1 = COR[0] * exp_table[j] * mnt_table[i];
110 } else {
111 k1 = REAL_CONST(0);
112 }
113#else
114
115 {
116#define B 0.953125
117 real_t c = COR[0];
118 real_t v = VAR[0];
119 float32_t tmp;
120 if (c == 0 || v <= 1) {
121 k1 = 0;
122 } else {
123 tmp = B / v;
124 flt_round(&tmp);
125 k1 = c * tmp;
126 }
127 }
128#endif
129
130 if (pred) {
131#if 1
132 tmp = state->VAR[1];
133 j = (tmp >> 7);
134 i = tmp & 0x7f;
135 if (j >= 128) {
136 j -= 128;
137 k2 = COR[1] * exp_table[j] * mnt_table[i];
138 } else {
139 k2 = REAL_CONST(0);
140 }
141#else
142
143#define B 0.953125
144 real_t c = COR[1];
145 real_t v = VAR[1];
146 float32_t tmp;
147 if (c == 0 || v <= 1) {
148 k2 = 0;
149 } else {
150 tmp = B / v;
151 flt_round(&tmp);
152 k2 = c * tmp;
153 }
154#endif
155
156 predictedvalue = k1 * r[0] + k2 * r[1];
157 flt_round(&predictedvalue);
158 *output = input + predictedvalue;
159 }
160
161 /* calculate new state data */
162 e0 = *output;
163 e1 = e0 - k1 * r[0];
164 dr1 = k1 * e0;
165
166 VAR[0] = ALPHA * VAR[0] + 0.5f * (r[0] * r[0] + e0 * e0);
167 COR[0] = ALPHA * COR[0] + r[0] * e0;
168 VAR[1] = ALPHA * VAR[1] + 0.5f * (r[1] * r[1] + e1 * e1);
169 COR[1] = ALPHA * COR[1] + r[1] * e1;
170
171 r[1] = A * (r[0] - dr1);
172 r[0] = A * e0;
173
174 state->r[0] = quant_pred(r[0]);
175 state->r[1] = quant_pred(r[1]);
176 state->COR[0] = quant_pred(COR[0]);
177 state->COR[1] = quant_pred(COR[1]);
178 state->VAR[0] = quant_pred(VAR[0]);
179 state->VAR[1] = quant_pred(VAR[1]);
180}
181
182static void reset_pred_state(pred_state *state)
183{
184 state->r[0] = 0;
185 state->r[1] = 0;
186 state->COR[0] = 0;
187 state->COR[1] = 0;
188 state->VAR[0] = 0x3F80;
189 state->VAR[1] = 0x3F80;
190}
191
192void pns_reset_pred_state(ic_stream *ics, pred_state *state)
193{
194 uint8_t sfb, g, b;
195 uint16_t i, offs, offs2;
196
197 /* prediction only for long blocks */
198 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
199 return;
200 }
201
202 for (g = 0; g < ics->num_window_groups; g++) {
203 for (b = 0; b < ics->window_group_length[g]; b++) {
204 for (sfb = 0; sfb < ics->max_sfb; sfb++) {
205 if (is_noise(ics, g, sfb)) {
206 offs = ics->swb_offset[sfb];
207 offs2 = min(ics->swb_offset[sfb + 1], ics->swb_offset_max);
208
209 for (i = offs; i < offs2; i++) {
210 reset_pred_state(&state[i]);
211 }
212 }
213 }
214 }
215 }
216}
217
218void reset_all_predictors(pred_state *state, uint16_t frame_len)
219{
220 uint16_t i;
221
222 for (i = 0; i < frame_len; i++) {
223 reset_pred_state(&state[i]);
224 }
225}
226
227/* intra channel prediction */
228void ic_prediction(ic_stream *ics, real_t *spec, pred_state *state,
229 uint16_t frame_len, uint8_t sf_index)
230{
231 uint8_t sfb;
232 uint16_t bin;
233
234 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) {
235 reset_all_predictors(state, frame_len);
236 } else {
237 for (sfb = 0; sfb < max_pred_sfb(sf_index); sfb++) {
238 uint16_t low = ics->swb_offset[sfb];
239 uint16_t high = min(ics->swb_offset[sfb + 1], ics->swb_offset_max);
240
241 for (bin = low; bin < high; bin++) {
242 ic_predict(&state[bin], spec[bin], &spec[bin],
243 (ics->predictor_data_present && ics->pred.prediction_used[sfb]));
244 }
245 }
246
247 if (ics->predictor_data_present) {
248 if (ics->pred.predictor_reset) {
249 for (bin = ics->pred.predictor_reset_group_number - 1;
250 bin < frame_len; bin += 30) {
251 reset_pred_state(&state[bin]);
252 }
253 }
254 }
255 }
256}
257
258#endif
259