Mercurial > audlegacy
annotate Plugins/Input/aac/libfaad2/ic_predict.c @ 1526:98a9c54459f0 trunk
[svn] - a minor tweak to the save function
author | nenolod |
---|---|
date | Mon, 07 Aug 2006 23:08:59 -0700 |
parents | 705d4c089fce |
children |
rev | line source |
---|---|
61 | 1 /* |
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding | |
199
0a2ad94e8607
[svn] Synced with bmp-mp4. Build system is fragile, but should work now.
chainsaw
parents:
61
diff
changeset
|
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com |
61 | 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 | |
1459 | 17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
61 | 18 ** |
19 ** Any non-GPL usage of this software or parts of this software is strictly | |
20 ** forbidden. | |
21 ** | |
22 ** Commercial non-GPL licensing of this software is possible. | |
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com. | |
24 ** | |
199
0a2ad94e8607
[svn] Synced with bmp-mp4. Build system is fragile, but should work now.
chainsaw
parents:
61
diff
changeset
|
25 ** $Id: ic_predict.c,v 1.23 2004/09/04 14:56:28 menno Exp $ |
61 | 26 **/ |
27 | |
28 #include "common.h" | |
29 #include "structs.h" | |
30 | |
31 #ifdef MAIN_DEC | |
32 | |
33 #include "syntax.h" | |
34 #include "ic_predict.h" | |
35 #include "pns.h" | |
36 | |
37 | |
38 static void flt_round(float32_t *pf) | |
39 { | |
40 int32_t flg; | |
41 uint32_t tmp, tmp1, tmp2; | |
320
d8889f819081
[svn] Dereferencing type-punned pointer will break strict-aliasing rules squashed by Mark Loeser <halcy0n@gentoo.org>.
chainsaw
parents:
199
diff
changeset
|
42 uint32_t *tmp3, *tmp4, *tmp5; |
d8889f819081
[svn] Dereferencing type-punned pointer will break strict-aliasing rules squashed by Mark Loeser <halcy0n@gentoo.org>.
chainsaw
parents:
199
diff
changeset
|
43 tmp3 = &tmp; |
d8889f819081
[svn] Dereferencing type-punned pointer will break strict-aliasing rules squashed by Mark Loeser <halcy0n@gentoo.org>.
chainsaw
parents:
199
diff
changeset
|
44 tmp4 = &tmp1; |
d8889f819081
[svn] Dereferencing type-punned pointer will break strict-aliasing rules squashed by Mark Loeser <halcy0n@gentoo.org>.
chainsaw
parents:
199
diff
changeset
|
45 tmp5 = &tmp2; |
d8889f819081
[svn] Dereferencing type-punned pointer will break strict-aliasing rules squashed by Mark Loeser <halcy0n@gentoo.org>.
chainsaw
parents:
199
diff
changeset
|
46 |
61 | 47 tmp = *(uint32_t*)pf; |
48 flg = tmp & (uint32_t)0x00008000; | |
49 tmp &= (uint32_t)0xffff0000; | |
50 tmp1 = tmp; | |
51 /* round 1/2 lsb toward infinity */ | |
52 if (flg) | |
53 { | |
54 tmp &= (uint32_t)0xff800000; /* extract exponent and sign */ | |
55 tmp |= (uint32_t)0x00010000; /* insert 1 lsb */ | |
56 tmp2 = tmp; /* add 1 lsb and elided one */ | |
57 tmp &= (uint32_t)0xff800000; /* extract exponent and sign */ | |
320
d8889f819081
[svn] Dereferencing type-punned pointer will break strict-aliasing rules squashed by Mark Loeser <halcy0n@gentoo.org>.
chainsaw
parents:
199
diff
changeset
|
58 |
d8889f819081
[svn] Dereferencing type-punned pointer will break strict-aliasing rules squashed by Mark Loeser <halcy0n@gentoo.org>.
chainsaw
parents:
199
diff
changeset
|
59 *pf = *(float32_t*)tmp4 + *(float32_t*)tmp4 - *(float32_t*)tmp5; |
61 | 60 } else { |
320
d8889f819081
[svn] Dereferencing type-punned pointer will break strict-aliasing rules squashed by Mark Loeser <halcy0n@gentoo.org>.
chainsaw
parents:
199
diff
changeset
|
61 *pf = *(float32_t*)tmp3; |
61 | 62 } |
63 } | |
64 | |
65 static int16_t quant_pred(float32_t x) | |
66 { | |
67 int16_t q; | |
320
d8889f819081
[svn] Dereferencing type-punned pointer will break strict-aliasing rules squashed by Mark Loeser <halcy0n@gentoo.org>.
chainsaw
parents:
199
diff
changeset
|
68 float32_t *tmp1 = &x; |
d8889f819081
[svn] Dereferencing type-punned pointer will break strict-aliasing rules squashed by Mark Loeser <halcy0n@gentoo.org>.
chainsaw
parents:
199
diff
changeset
|
69 uint32_t *tmp = (uint32_t*)tmp1; |
61 | 70 |
71 q = (int16_t)(*tmp>>16); | |
72 | |
73 return q; | |
74 } | |
75 | |
76 static float32_t inv_quant_pred(int16_t q) | |
77 { | |
78 float32_t x; | |
320
d8889f819081
[svn] Dereferencing type-punned pointer will break strict-aliasing rules squashed by Mark Loeser <halcy0n@gentoo.org>.
chainsaw
parents:
199
diff
changeset
|
79 float32_t *tmp1 = &x; |
d8889f819081
[svn] Dereferencing type-punned pointer will break strict-aliasing rules squashed by Mark Loeser <halcy0n@gentoo.org>.
chainsaw
parents:
199
diff
changeset
|
80 uint32_t *tmp = (uint32_t*)tmp1; |
61 | 81 *tmp = ((uint32_t)q)<<16; |
82 | |
83 return x; | |
84 } | |
85 | |
86 static void ic_predict(pred_state *state, real_t input, real_t *output, uint8_t pred) | |
87 { | |
199
0a2ad94e8607
[svn] Synced with bmp-mp4. Build system is fragile, but should work now.
chainsaw
parents:
61
diff
changeset
|
88 uint16_t tmp; |
61 | 89 int16_t i, j; |
90 real_t dr1, predictedvalue; | |
91 real_t e0, e1; | |
92 real_t k1, k2; | |
93 | |
94 real_t r[2]; | |
95 real_t COR[2]; | |
96 real_t VAR[2]; | |
97 | |
98 r[0] = inv_quant_pred(state->r[0]); | |
99 r[1] = inv_quant_pred(state->r[1]); | |
100 COR[0] = inv_quant_pred(state->COR[0]); | |
101 COR[1] = inv_quant_pred(state->COR[1]); | |
102 VAR[0] = inv_quant_pred(state->VAR[0]); | |
103 VAR[1] = inv_quant_pred(state->VAR[1]); | |
104 | |
105 | |
106 #if 1 | |
107 tmp = state->VAR[0]; | |
108 j = (tmp >> 7); | |
109 i = tmp & 0x7f; | |
110 if (j >= 128) | |
111 { | |
112 j -= 128; | |
113 k1 = COR[0] * exp_table[j] * mnt_table[i]; | |
114 } else { | |
115 k1 = REAL_CONST(0); | |
116 } | |
117 #else | |
118 | |
119 { | |
120 #define B 0.953125 | |
121 real_t c = COR[0]; | |
122 real_t v = VAR[0]; | |
123 real_t tmp; | |
124 if (c == 0 || v <= 1) | |
125 { | |
126 k1 = 0; | |
127 } else { | |
128 tmp = B / v; | |
129 flt_round(&tmp); | |
130 k1 = c * tmp; | |
131 } | |
132 } | |
133 #endif | |
134 | |
135 if (pred) | |
136 { | |
137 #if 1 | |
138 tmp = state->VAR[1]; | |
139 j = (tmp >> 7); | |
140 i = tmp & 0x7f; | |
141 if (j >= 128) | |
142 { | |
143 j -= 128; | |
144 k2 = COR[1] * exp_table[j] * mnt_table[i]; | |
145 } else { | |
146 k2 = REAL_CONST(0); | |
147 } | |
148 #else | |
149 | |
150 #define B 0.953125 | |
151 real_t c = COR[1]; | |
152 real_t v = VAR[1]; | |
153 real_t tmp; | |
154 if (c == 0 || v <= 1) | |
155 { | |
156 k2 = 0; | |
157 } else { | |
158 tmp = B / v; | |
159 flt_round(&tmp); | |
160 k2 = c * tmp; | |
161 } | |
162 #endif | |
163 | |
164 predictedvalue = k1*r[0] + k2*r[1]; | |
165 flt_round(&predictedvalue); | |
166 *output = input + predictedvalue; | |
167 } | |
168 | |
169 /* calculate new state data */ | |
170 e0 = *output; | |
171 e1 = e0 - k1*r[0]; | |
172 dr1 = k1*e0; | |
173 | |
174 VAR[0] = ALPHA*VAR[0] + 0.5f * (r[0]*r[0] + e0*e0); | |
175 COR[0] = ALPHA*COR[0] + r[0]*e0; | |
176 VAR[1] = ALPHA*VAR[1] + 0.5f * (r[1]*r[1] + e1*e1); | |
177 COR[1] = ALPHA*COR[1] + r[1]*e1; | |
178 | |
179 r[1] = A * (r[0]-dr1); | |
180 r[0] = A * e0; | |
181 | |
182 state->r[0] = quant_pred(r[0]); | |
183 state->r[1] = quant_pred(r[1]); | |
184 state->COR[0] = quant_pred(COR[0]); | |
185 state->COR[1] = quant_pred(COR[1]); | |
186 state->VAR[0] = quant_pred(VAR[0]); | |
187 state->VAR[1] = quant_pred(VAR[1]); | |
188 } | |
189 | |
190 static void reset_pred_state(pred_state *state) | |
191 { | |
192 state->r[0] = 0; | |
193 state->r[1] = 0; | |
194 state->COR[0] = 0; | |
195 state->COR[1] = 0; | |
196 state->VAR[0] = 0x3F80; | |
197 state->VAR[1] = 0x3F80; | |
198 } | |
199 | |
200 void pns_reset_pred_state(ic_stream *ics, pred_state *state) | |
201 { | |
202 uint8_t sfb, g, b; | |
203 uint16_t i, offs, offs2; | |
204 | |
205 /* prediction only for long blocks */ | |
206 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) | |
207 return; | |
208 | |
209 for (g = 0; g < ics->num_window_groups; g++) | |
210 { | |
211 for (b = 0; b < ics->window_group_length[g]; b++) | |
212 { | |
213 for (sfb = 0; sfb < ics->max_sfb; sfb++) | |
214 { | |
215 if (is_noise(ics, g, sfb)) | |
216 { | |
217 offs = ics->swb_offset[sfb]; | |
218 offs2 = ics->swb_offset[sfb+1]; | |
219 | |
220 for (i = offs; i < offs2; i++) | |
221 reset_pred_state(&state[i]); | |
222 } | |
223 } | |
224 } | |
225 } | |
226 } | |
227 | |
228 void reset_all_predictors(pred_state *state, uint16_t frame_len) | |
229 { | |
230 uint16_t i; | |
231 | |
232 for (i = 0; i < frame_len; i++) | |
233 reset_pred_state(&state[i]); | |
234 } | |
235 | |
236 /* intra channel prediction */ | |
237 void ic_prediction(ic_stream *ics, real_t *spec, pred_state *state, | |
238 uint16_t frame_len, uint8_t sf_index) | |
239 { | |
240 uint8_t sfb; | |
241 uint16_t bin; | |
242 | |
243 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) | |
244 { | |
245 reset_all_predictors(state, frame_len); | |
246 } else { | |
247 for (sfb = 0; sfb < max_pred_sfb(sf_index); sfb++) | |
248 { | |
249 uint16_t low = ics->swb_offset[sfb]; | |
250 uint16_t high = ics->swb_offset[sfb+1]; | |
251 | |
252 for (bin = low; bin < high; bin++) | |
253 { | |
254 ic_predict(&state[bin], spec[bin], &spec[bin], | |
255 (ics->predictor_data_present && ics->pred.prediction_used[sfb])); | |
256 } | |
257 } | |
258 | |
259 if (ics->predictor_data_present) | |
260 { | |
261 if (ics->pred.predictor_reset) | |
262 { | |
263 for (bin = ics->pred.predictor_reset_group_number - 1; | |
264 bin < frame_len; bin += 30) | |
265 { | |
266 reset_pred_state(&state[bin]); | |
267 } | |
268 } | |
269 } | |
270 } | |
271 } | |
272 | |
273 #endif |