Mercurial > mplayer.hg
comparison libfaad2/ic_predict.c @ 12527:4a370c80fe5c
update to the 2.0 release of faad, patch by adland
author | diego |
---|---|
date | Wed, 02 Jun 2004 22:59:04 +0000 |
parents | e989150f8216 |
children | d81145997036 |
comparison
equal
deleted
inserted
replaced
12526:e183ad37d24c | 12527:4a370c80fe5c |
---|---|
1 /* | 1 /* |
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding | 2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding |
3 ** Copyright (C) 2003 M. Bakker, Ahead Software AG, http://www.nero.com | 3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com |
4 ** | 4 ** |
5 ** This program is free software; you can redistribute it and/or modify | 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 | 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 | 7 ** the Free Software Foundation; either version 2 of the License, or |
8 ** (at your option) any later version. | 8 ** (at your option) any later version. |
20 ** forbidden. | 20 ** forbidden. |
21 ** | 21 ** |
22 ** Commercial non-GPL licensing of this software is possible. | 22 ** Commercial non-GPL licensing of this software is possible. |
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com. | 23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com. |
24 ** | 24 ** |
25 ** $Id: ic_predict.c,v 1.12 2003/07/29 08:20:12 menno Exp $ | 25 ** $Id: ic_predict.c,v 1.1 2003/08/30 22:30:21 arpi Exp $ |
26 **/ | 26 **/ |
27 | 27 |
28 #include "common.h" | 28 #include "common.h" |
29 #include "structs.h" | 29 #include "structs.h" |
30 | 30 |
32 | 32 |
33 #include "syntax.h" | 33 #include "syntax.h" |
34 #include "ic_predict.h" | 34 #include "ic_predict.h" |
35 #include "pns.h" | 35 #include "pns.h" |
36 | 36 |
37 static void flt_round(real_t *pf) | 37 |
38 { | 38 static void flt_round(float32_t *pf) |
39 /* more stable version for clever compilers like gcc 3.x */ | 39 { |
40 int32_t flg; | 40 int32_t flg; |
41 uint32_t tmp, tmp1, tmp2; | 41 uint32_t tmp, tmp1, tmp2; |
42 | 42 |
43 tmp = *(uint32_t*)pf; | 43 tmp = *(uint32_t*)pf; |
44 flg = tmp & (uint32_t)0x00008000; | 44 flg = tmp & (uint32_t)0x00008000; |
45 tmp &= (uint32_t)0xffff0000; | 45 tmp &= (uint32_t)0xffff0000; |
46 tmp1 = tmp; | 46 tmp1 = tmp; |
47 | |
48 /* round 1/2 lsb toward infinity */ | 47 /* round 1/2 lsb toward infinity */ |
49 if (flg) | 48 if (flg) |
50 { | 49 { |
51 tmp &= (uint32_t)0xff800000; /* extract exponent and sign */ | 50 tmp &= (uint32_t)0xff800000; /* extract exponent and sign */ |
52 tmp |= (uint32_t)0x00010000; /* insert 1 lsb */ | 51 tmp |= (uint32_t)0x00010000; /* insert 1 lsb */ |
53 tmp2 = tmp; /* add 1 lsb and elided one */ | 52 tmp2 = tmp; /* add 1 lsb and elided one */ |
54 tmp &= (uint32_t)0xff800000; /* extract exponent and sign */ | 53 tmp &= (uint32_t)0xff800000; /* extract exponent and sign */ |
55 | 54 |
56 *pf = *(real_t*)&tmp1+*(real_t*)&tmp2-*(real_t*)&tmp;/* subtract elided one */ | 55 *pf = *(float32_t*)&tmp1 + *(float32_t*)&tmp2 - *(float32_t*)&tmp; |
57 } else { | 56 } else { |
58 *pf = *(real_t*)&tmp; | 57 *pf = *(float32_t*)&tmp; |
59 } | 58 } |
59 } | |
60 | |
61 static int16_t quant_pred(float32_t x) | |
62 { | |
63 int16_t q; | |
64 uint32_t *tmp = (uint32_t*)&x; | |
65 | |
66 q = (int16_t)(*tmp>>16); | |
67 | |
68 return q; | |
69 } | |
70 | |
71 static float32_t inv_quant_pred(int16_t q) | |
72 { | |
73 float32_t x; | |
74 uint32_t *tmp = (uint32_t*)&x; | |
75 *tmp = ((uint32_t)q)<<16; | |
76 | |
77 return x; | |
60 } | 78 } |
61 | 79 |
62 static void ic_predict(pred_state *state, real_t input, real_t *output, uint8_t pred) | 80 static void ic_predict(pred_state *state, real_t input, real_t *output, uint8_t pred) |
63 { | 81 { |
82 uint16_t tmp; | |
83 int16_t i, j; | |
64 real_t dr1, predictedvalue; | 84 real_t dr1, predictedvalue; |
65 real_t e0, e1; | 85 real_t e0, e1; |
66 real_t k1, k2; | 86 real_t k1, k2; |
67 | 87 |
68 real_t *r; | 88 real_t r[2]; |
69 real_t *KOR; | 89 real_t COR[2]; |
70 real_t *VAR; | 90 real_t VAR[2]; |
71 | 91 |
72 r = state->r; /* delay elements */ | 92 r[0] = inv_quant_pred(state->r[0]); |
73 KOR = state->KOR; /* correlations */ | 93 r[1] = inv_quant_pred(state->r[1]); |
74 VAR = state->VAR; /* variances */ | 94 COR[0] = inv_quant_pred(state->COR[0]); |
75 | 95 COR[1] = inv_quant_pred(state->COR[1]); |
76 if (VAR[0] <= 1) | 96 VAR[0] = inv_quant_pred(state->VAR[0]); |
77 k1 = 0; | 97 VAR[1] = inv_quant_pred(state->VAR[1]); |
78 else | 98 |
79 k1 = KOR[0]/VAR[0]*B; | 99 |
100 #if 1 | |
101 tmp = state->VAR[0]; | |
102 j = (tmp >> 7); | |
103 i = tmp & 0x7f; | |
104 if (j >= 128) | |
105 { | |
106 j -= 128; | |
107 k1 = COR[0] * exp_table[j] * mnt_table[i]; | |
108 } else { | |
109 k1 = REAL_CONST(0); | |
110 } | |
111 #else | |
112 | |
113 { | |
114 #define B 0.953125 | |
115 real_t c = COR[0]; | |
116 real_t v = VAR[0]; | |
117 real_t tmp; | |
118 if (c == 0 || v <= 1) | |
119 { | |
120 k1 = 0; | |
121 } else { | |
122 tmp = B / v; | |
123 flt_round(&tmp); | |
124 k1 = c * tmp; | |
125 } | |
126 } | |
127 #endif | |
80 | 128 |
81 if (pred) | 129 if (pred) |
82 { | 130 { |
83 /* only needed for the actual predicted value, k1 is always needed */ | 131 #if 1 |
84 if (VAR[1] <= 1) | 132 tmp = state->VAR[1]; |
133 j = (tmp >> 7); | |
134 i = tmp & 0x7f; | |
135 if (j >= 128) | |
136 { | |
137 j -= 128; | |
138 k2 = COR[1] * exp_table[j] * mnt_table[i]; | |
139 } else { | |
140 k2 = REAL_CONST(0); | |
141 } | |
142 #else | |
143 | |
144 #define B 0.953125 | |
145 real_t c = COR[1]; | |
146 real_t v = VAR[1]; | |
147 real_t tmp; | |
148 if (c == 0 || v <= 1) | |
149 { | |
85 k2 = 0; | 150 k2 = 0; |
86 else | 151 } else { |
87 k2 = KOR[1]/VAR[1]*B; | 152 tmp = B / v; |
88 | 153 flt_round(&tmp); |
89 predictedvalue = MUL(k1, r[0]) + MUL(k2, r[1]); | 154 k2 = c * tmp; |
155 } | |
156 #endif | |
157 | |
158 predictedvalue = k1*r[0] + k2*r[1]; | |
90 flt_round(&predictedvalue); | 159 flt_round(&predictedvalue); |
91 | |
92 *output = input + predictedvalue; | 160 *output = input + predictedvalue; |
93 } else { | |
94 *output = input; | |
95 } | 161 } |
96 | 162 |
97 /* calculate new state data */ | 163 /* calculate new state data */ |
98 e0 = *output; | 164 e0 = *output; |
99 e1 = e0 - MUL(k1, r[0]); | 165 e1 = e0 - k1*r[0]; |
100 | 166 dr1 = k1*e0; |
101 dr1 = MUL(k1, e0); | 167 |
102 | 168 VAR[0] = ALPHA*VAR[0] + 0.5f * (r[0]*r[0] + e0*e0); |
103 VAR[0] = MUL(ALPHA, VAR[0]) + MUL(REAL_CONST(0.5), (MUL(r[0], r[0]) + MUL(e0, e0))); | 169 COR[0] = ALPHA*COR[0] + r[0]*e0; |
104 KOR[0] = MUL(ALPHA, KOR[0]) + MUL(r[0], e0); | 170 VAR[1] = ALPHA*VAR[1] + 0.5f * (r[1]*r[1] + e1*e1); |
105 VAR[1] = MUL(ALPHA, VAR[1]) + MUL(REAL_CONST(0.5), (MUL(r[1], r[1]) + MUL(e1, e1))); | 171 COR[1] = ALPHA*COR[1] + r[1]*e1; |
106 KOR[1] = MUL(ALPHA, KOR[1]) + MUL(r[1], e1); | 172 |
107 | 173 r[1] = A * (r[0]-dr1); |
108 r[1] = MUL(A, (r[0]-dr1)); | 174 r[0] = A * e0; |
109 r[0] = MUL(A, e0); | 175 |
176 state->r[0] = quant_pred(r[0]); | |
177 state->r[1] = quant_pred(r[1]); | |
178 state->COR[0] = quant_pred(COR[0]); | |
179 state->COR[1] = quant_pred(COR[1]); | |
180 state->VAR[0] = quant_pred(VAR[0]); | |
181 state->VAR[1] = quant_pred(VAR[1]); | |
110 } | 182 } |
111 | 183 |
112 static void reset_pred_state(pred_state *state) | 184 static void reset_pred_state(pred_state *state) |
113 { | 185 { |
114 state->r[0] = 0; | 186 state->r[0] = 0; |
115 state->r[1] = 0; | 187 state->r[1] = 0; |
116 state->KOR[0] = 0; | 188 state->COR[0] = 0; |
117 state->KOR[1] = 0; | 189 state->COR[1] = 0; |
118 state->VAR[0] = REAL_CONST(1.0); | 190 state->VAR[0] = 0x3F80; |
119 state->VAR[1] = REAL_CONST(1.0); | 191 state->VAR[1] = 0x3F80; |
120 } | 192 } |
121 | 193 |
122 void pns_reset_pred_state(ic_stream *ics, pred_state *state) | 194 void pns_reset_pred_state(ic_stream *ics, pred_state *state) |
123 { | 195 { |
124 uint8_t sfb, g, b; | 196 uint8_t sfb, g, b; |
155 reset_pred_state(&state[i]); | 227 reset_pred_state(&state[i]); |
156 } | 228 } |
157 | 229 |
158 /* intra channel prediction */ | 230 /* intra channel prediction */ |
159 void ic_prediction(ic_stream *ics, real_t *spec, pred_state *state, | 231 void ic_prediction(ic_stream *ics, real_t *spec, pred_state *state, |
160 uint16_t frame_len) | 232 uint16_t frame_len, uint8_t sf_index) |
161 { | 233 { |
162 uint8_t sfb; | 234 uint8_t sfb; |
163 uint16_t bin; | 235 uint16_t bin; |
164 | 236 |
165 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) | 237 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE) |
166 { | 238 { |
167 reset_all_predictors(state, frame_len); | 239 reset_all_predictors(state, frame_len); |
168 } else { | 240 } else { |
169 for (sfb = 0; sfb < ics->pred.limit; sfb++) | 241 for (sfb = 0; sfb < max_pred_sfb(sf_index); sfb++) |
170 { | 242 { |
171 uint16_t low = ics->swb_offset[sfb]; | 243 uint16_t low = ics->swb_offset[sfb]; |
172 uint16_t high = ics->swb_offset[sfb+1]; | 244 uint16_t high = ics->swb_offset[sfb+1]; |
173 | 245 |
174 for (bin = low; bin < high; bin++) | 246 for (bin = low; bin < high; bin++) |
175 { | 247 { |
176 ic_predict(&state[bin], spec[bin], &spec[bin], | 248 ic_predict(&state[bin], spec[bin], &spec[bin], |
177 (ics->predictor_data_present && | 249 (ics->predictor_data_present && ics->pred.prediction_used[sfb])); |
178 ics->pred.prediction_used[sfb])); | |
179 } | 250 } |
180 } | 251 } |
181 | 252 |
182 if (ics->predictor_data_present) | 253 if (ics->predictor_data_present) |
183 { | 254 { |