/proc/self/cwd/libfaad/ic_predict.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
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 | | |
41 | 335M | static uint32_t float_to_bits(float32_t f32) { |
42 | 335M | uint32_t u32; |
43 | 335M | memcpy(&u32, &f32, 4); |
44 | 335M | return u32; |
45 | 335M | } |
46 | | |
47 | 335M | static float32_t bits_to_float(uint32_t u32) { |
48 | 335M | float32_t f32; |
49 | 335M | memcpy(&f32, &u32, 4); |
50 | 335M | return f32; |
51 | 335M | } |
52 | | |
53 | | static float32_t flt_round(float32_t pf) |
54 | 15.0k | { |
55 | 15.0k | int32_t flg; |
56 | 15.0k | uint32_t tmp, tmp1, tmp2; |
57 | | |
58 | 15.0k | tmp = float_to_bits(pf); |
59 | 15.0k | flg = tmp & (uint32_t)0x00008000; |
60 | 15.0k | tmp &= (uint32_t)0xffff0000; |
61 | 15.0k | tmp1 = tmp; |
62 | | /* round 1/2 lsb toward infinity */ |
63 | 15.0k | if (flg) |
64 | 0 | { |
65 | 0 | tmp &= (uint32_t)0xff800000; /* extract exponent and sign */ |
66 | 0 | tmp |= (uint32_t)0x00010000; /* insert 1 lsb */ |
67 | 0 | tmp2 = tmp; /* add 1 lsb and elided one */ |
68 | 0 | tmp &= (uint32_t)0xff800000; /* extract exponent and sign */ |
69 | |
|
70 | 0 | return bits_to_float(tmp1) + bits_to_float(tmp2) - bits_to_float(tmp); |
71 | 15.0k | } else { |
72 | 15.0k | return bits_to_float(tmp); |
73 | 15.0k | } |
74 | 15.0k | } |
75 | | |
76 | | static int16_t quant_pred(float32_t x) |
77 | 335M | { |
78 | 335M | return (int16_t)(float_to_bits(x) >> 16); |
79 | 335M | } |
80 | | |
81 | | static float32_t inv_quant_pred(int16_t q) |
82 | 335M | { |
83 | 335M | uint16_t u16 = (uint16_t)q; |
84 | 335M | return bits_to_float((uint32_t)u16 << 16); |
85 | 335M | } |
86 | | |
87 | | static void ic_predict(pred_state *state, real_t input, real_t *output, uint8_t pred) |
88 | 55.9M | { |
89 | | #ifdef FIXED_POINT |
90 | | // main codepath is simply not ready for FIXED_POINT, better not to run it at all. |
91 | | if (pred) |
92 | | *output = input; |
93 | | #else |
94 | 55.9M | uint16_t tmp; |
95 | 55.9M | int16_t i, j; |
96 | 55.9M | real_t dr1; |
97 | 55.9M | float32_t predictedvalue; |
98 | 55.9M | real_t e0, e1; |
99 | 55.9M | real_t k1, k2; |
100 | | |
101 | 55.9M | real_t r[2]; |
102 | 55.9M | real_t COR[2]; |
103 | 55.9M | real_t VAR[2]; |
104 | | |
105 | 55.9M | r[0] = inv_quant_pred(state->r[0]); |
106 | 55.9M | r[1] = inv_quant_pred(state->r[1]); |
107 | 55.9M | COR[0] = inv_quant_pred(state->COR[0]); |
108 | 55.9M | COR[1] = inv_quant_pred(state->COR[1]); |
109 | 55.9M | VAR[0] = inv_quant_pred(state->VAR[0]); |
110 | 55.9M | VAR[1] = inv_quant_pred(state->VAR[1]); |
111 | | |
112 | | |
113 | 55.9M | #if 1 |
114 | 55.9M | tmp = state->VAR[0]; |
115 | 55.9M | j = (tmp >> 7); |
116 | 55.9M | i = tmp & 0x7f; |
117 | 55.9M | if (j >= 128) |
118 | 4.44k | { |
119 | 4.44k | j -= 128; |
120 | 4.44k | k1 = COR[0] * exp_table[j] * mnt_table[i]; |
121 | 55.9M | } else { |
122 | 55.9M | k1 = REAL_CONST(0); |
123 | 55.9M | } |
124 | | #else |
125 | | |
126 | | { |
127 | | #define B 0.953125 |
128 | | real_t c = COR[0]; |
129 | | real_t v = VAR[0]; |
130 | | float32_t tmp; |
131 | | if (c == 0 || v <= 1) |
132 | | { |
133 | | k1 = 0; |
134 | | } else { |
135 | | tmp = B / v; |
136 | | flt_round(&tmp); |
137 | | k1 = c * tmp; |
138 | | } |
139 | | } |
140 | | #endif |
141 | | |
142 | 55.9M | if (pred) |
143 | 15.0k | { |
144 | 15.0k | #if 1 |
145 | 15.0k | tmp = state->VAR[1]; |
146 | 15.0k | j = (tmp >> 7); |
147 | 15.0k | i = tmp & 0x7f; |
148 | 15.0k | if (j >= 128) |
149 | 434 | { |
150 | 434 | j -= 128; |
151 | 434 | k2 = COR[1] * exp_table[j] * mnt_table[i]; |
152 | 14.6k | } else { |
153 | 14.6k | k2 = REAL_CONST(0); |
154 | 14.6k | } |
155 | | #else |
156 | | |
157 | | #define B 0.953125 |
158 | | real_t c = COR[1]; |
159 | | real_t v = VAR[1]; |
160 | | float32_t tmp; |
161 | | if (c == 0 || v <= 1) |
162 | | { |
163 | | k2 = 0; |
164 | | } else { |
165 | | tmp = B / v; |
166 | | flt_round(&tmp); |
167 | | k2 = c * tmp; |
168 | | } |
169 | | #endif |
170 | | |
171 | 15.0k | predictedvalue = k1*r[0] + k2*r[1]; |
172 | 15.0k | predictedvalue = flt_round(predictedvalue); |
173 | 15.0k | *output = input + predictedvalue; |
174 | 15.0k | } |
175 | | |
176 | | /* calculate new state data */ |
177 | 55.9M | e0 = *output; |
178 | 55.9M | e1 = e0 - k1*r[0]; |
179 | 55.9M | dr1 = k1*e0; |
180 | | |
181 | 55.9M | VAR[0] = ALPHA*VAR[0] + 0.5f * (r[0]*r[0] + e0*e0); |
182 | 55.9M | COR[0] = ALPHA*COR[0] + r[0]*e0; |
183 | 55.9M | VAR[1] = ALPHA*VAR[1] + 0.5f * (r[1]*r[1] + e1*e1); |
184 | 55.9M | COR[1] = ALPHA*COR[1] + r[1]*e1; |
185 | | |
186 | 55.9M | r[1] = A * (r[0]-dr1); |
187 | 55.9M | r[0] = A * e0; |
188 | | |
189 | 55.9M | state->r[0] = quant_pred(r[0]); |
190 | 55.9M | state->r[1] = quant_pred(r[1]); |
191 | 55.9M | state->COR[0] = quant_pred(COR[0]); |
192 | 55.9M | state->COR[1] = quant_pred(COR[1]); |
193 | 55.9M | state->VAR[0] = quant_pred(VAR[0]); |
194 | 55.9M | state->VAR[1] = quant_pred(VAR[1]); |
195 | 55.9M | #endif |
196 | 55.9M | } |
197 | | |
198 | | static void reset_pred_state(pred_state *state) |
199 | 103M | { |
200 | 103M | state->r[0] = 0; |
201 | 103M | state->r[1] = 0; |
202 | 103M | state->COR[0] = 0; |
203 | 103M | state->COR[1] = 0; |
204 | 103M | state->VAR[0] = 0x3F80; |
205 | 103M | state->VAR[1] = 0x3F80; |
206 | 103M | } |
207 | | |
208 | | void pns_reset_pred_state(ic_stream *ics, pred_state *state) |
209 | 102k | { |
210 | 102k | uint8_t sfb, g, b; |
211 | 102k | uint16_t i, offs, offs2; |
212 | | |
213 | | /* prediction only for long blocks */ |
214 | 102k | if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) |
215 | 9.54k | return; |
216 | | |
217 | 186k | for (g = 0; g < ics->num_window_groups; g++) |
218 | 93.0k | { |
219 | 186k | for (b = 0; b < ics->window_group_length[g]; b++) |
220 | 93.0k | { |
221 | 114k | for (sfb = 0; sfb < ics->max_sfb; sfb++) |
222 | 21.5k | { |
223 | 21.5k | if (is_noise(ics, g, sfb)) |
224 | 9.63k | { |
225 | 9.63k | offs = ics->swb_offset[sfb]; |
226 | 9.63k | offs2 = min(ics->swb_offset[sfb+1], ics->swb_offset_max); |
227 | | |
228 | 313k | for (i = offs; i < offs2; i++) |
229 | 303k | reset_pred_state(&state[i]); |
230 | 9.63k | } |
231 | 21.5k | } |
232 | 93.0k | } |
233 | 93.0k | } |
234 | 93.0k | } |
235 | | |
236 | | void reset_all_predictors(pred_state *state, uint16_t frame_len) |
237 | 100k | { |
238 | 100k | uint16_t i; |
239 | | |
240 | 102M | for (i = 0; i < frame_len; i++) |
241 | 102M | reset_pred_state(&state[i]); |
242 | 100k | } |
243 | | |
244 | | /* intra channel prediction */ |
245 | | void ic_prediction(ic_stream *ics, real_t *spec, pred_state *state, |
246 | | uint16_t frame_len, uint8_t sf_index) |
247 | 102k | { |
248 | 102k | uint8_t sfb; |
249 | 102k | uint16_t bin; |
250 | | |
251 | 102k | if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) |
252 | 9.54k | { |
253 | 9.54k | reset_all_predictors(state, frame_len); |
254 | 93.0k | } else { |
255 | 3.39M | for (sfb = 0; sfb < max_pred_sfb(sf_index); sfb++) |
256 | 3.29M | { |
257 | 3.29M | uint16_t low = ics->swb_offset[sfb]; |
258 | 3.29M | uint16_t high = min(ics->swb_offset[sfb+1], ics->swb_offset_max); |
259 | | |
260 | 59.2M | for (bin = low; bin < high; bin++) |
261 | 55.9M | { |
262 | 55.9M | ic_predict(&state[bin], spec[bin], &spec[bin], |
263 | 55.9M | (ics->predictor_data_present && ics->pred.prediction_used[sfb])); |
264 | 55.9M | } |
265 | 3.29M | } |
266 | | |
267 | 93.0k | if (ics->predictor_data_present) |
268 | 14.8k | { |
269 | 14.8k | if (ics->pred.predictor_reset) |
270 | 13.2k | { |
271 | 13.2k | for (bin = ics->pred.predictor_reset_group_number - 1; |
272 | 426k | bin < frame_len; bin += 30) |
273 | 412k | { |
274 | 412k | reset_pred_state(&state[bin]); |
275 | 412k | } |
276 | 13.2k | } |
277 | 14.8k | } |
278 | 93.0k | } |
279 | 102k | } |
280 | | |
281 | | #endif |