10725
|
1 /*
|
|
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
|
|
3 ** Copyright (C) 2003 M. Bakker, Ahead Software 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 ** Commercial non-GPL licensing of this software is possible.
|
|
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
|
|
24 **
|
|
25 ** $Id: ic_predict.c,v 1.12 2003/07/29 08:20:12 menno Exp $
|
|
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 static void flt_round(real_t *pf)
|
|
38 {
|
|
39 /* more stable version for clever compilers like gcc 3.x */
|
|
40 int32_t flg;
|
|
41 uint32_t tmp, tmp1, tmp2;
|
|
42
|
|
43 tmp = *(uint32_t*)pf;
|
|
44 flg = tmp & (uint32_t)0x00008000;
|
|
45 tmp &= (uint32_t)0xffff0000;
|
|
46 tmp1 = tmp;
|
|
47
|
|
48 /* round 1/2 lsb toward infinity */
|
|
49 if (flg)
|
|
50 {
|
|
51 tmp &= (uint32_t)0xff800000; /* extract exponent and sign */
|
|
52 tmp |= (uint32_t)0x00010000; /* insert 1 lsb */
|
|
53 tmp2 = tmp; /* add 1 lsb and elided one */
|
|
54 tmp &= (uint32_t)0xff800000; /* extract exponent and sign */
|
|
55
|
|
56 *pf = *(real_t*)&tmp1+*(real_t*)&tmp2-*(real_t*)&tmp;/* subtract elided one */
|
|
57 } else {
|
|
58 *pf = *(real_t*)&tmp;
|
|
59 }
|
|
60 }
|
|
61
|
|
62 static void ic_predict(pred_state *state, real_t input, real_t *output, uint8_t pred)
|
|
63 {
|
|
64 real_t dr1, predictedvalue;
|
|
65 real_t e0, e1;
|
|
66 real_t k1, k2;
|
|
67
|
|
68 real_t *r;
|
|
69 real_t *KOR;
|
|
70 real_t *VAR;
|
|
71
|
|
72 r = state->r; /* delay elements */
|
|
73 KOR = state->KOR; /* correlations */
|
|
74 VAR = state->VAR; /* variances */
|
|
75
|
|
76 if (VAR[0] <= 1)
|
|
77 k1 = 0;
|
|
78 else
|
|
79 k1 = KOR[0]/VAR[0]*B;
|
|
80
|
|
81 if (pred)
|
|
82 {
|
|
83 /* only needed for the actual predicted value, k1 is always needed */
|
|
84 if (VAR[1] <= 1)
|
|
85 k2 = 0;
|
|
86 else
|
|
87 k2 = KOR[1]/VAR[1]*B;
|
|
88
|
|
89 predictedvalue = MUL(k1, r[0]) + MUL(k2, r[1]);
|
|
90 flt_round(&predictedvalue);
|
|
91
|
|
92 *output = input + predictedvalue;
|
|
93 } else {
|
|
94 *output = input;
|
|
95 }
|
|
96
|
|
97 /* calculate new state data */
|
|
98 e0 = *output;
|
|
99 e1 = e0 - MUL(k1, r[0]);
|
|
100
|
|
101 dr1 = MUL(k1, e0);
|
|
102
|
|
103 VAR[0] = MUL(ALPHA, VAR[0]) + MUL(REAL_CONST(0.5), (MUL(r[0], r[0]) + MUL(e0, e0)));
|
|
104 KOR[0] = MUL(ALPHA, KOR[0]) + MUL(r[0], e0);
|
|
105 VAR[1] = MUL(ALPHA, VAR[1]) + MUL(REAL_CONST(0.5), (MUL(r[1], r[1]) + MUL(e1, e1)));
|
|
106 KOR[1] = MUL(ALPHA, KOR[1]) + MUL(r[1], e1);
|
|
107
|
|
108 r[1] = MUL(A, (r[0]-dr1));
|
|
109 r[0] = MUL(A, e0);
|
|
110 }
|
|
111
|
|
112 static void reset_pred_state(pred_state *state)
|
|
113 {
|
|
114 state->r[0] = 0;
|
|
115 state->r[1] = 0;
|
|
116 state->KOR[0] = 0;
|
|
117 state->KOR[1] = 0;
|
|
118 state->VAR[0] = REAL_CONST(1.0);
|
|
119 state->VAR[1] = REAL_CONST(1.0);
|
|
120 }
|
|
121
|
|
122 void pns_reset_pred_state(ic_stream *ics, pred_state *state)
|
|
123 {
|
|
124 uint8_t sfb, g, b;
|
|
125 uint16_t i, offs, offs2;
|
|
126
|
|
127 /* prediction only for long blocks */
|
|
128 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
|
|
129 return;
|
|
130
|
|
131 for (g = 0; g < ics->num_window_groups; g++)
|
|
132 {
|
|
133 for (b = 0; b < ics->window_group_length[g]; b++)
|
|
134 {
|
|
135 for (sfb = 0; sfb < ics->max_sfb; sfb++)
|
|
136 {
|
|
137 if (is_noise(ics, g, sfb))
|
|
138 {
|
|
139 offs = ics->swb_offset[sfb];
|
|
140 offs2 = ics->swb_offset[sfb+1];
|
|
141
|
|
142 for (i = offs; i < offs2; i++)
|
|
143 reset_pred_state(&state[i]);
|
|
144 }
|
|
145 }
|
|
146 }
|
|
147 }
|
|
148 }
|
|
149
|
|
150 void reset_all_predictors(pred_state *state, uint16_t frame_len)
|
|
151 {
|
|
152 uint16_t i;
|
|
153
|
|
154 for (i = 0; i < frame_len; i++)
|
|
155 reset_pred_state(&state[i]);
|
|
156 }
|
|
157
|
|
158 /* intra channel prediction */
|
|
159 void ic_prediction(ic_stream *ics, real_t *spec, pred_state *state,
|
|
160 uint16_t frame_len)
|
|
161 {
|
|
162 uint8_t sfb;
|
|
163 uint16_t bin;
|
|
164
|
|
165 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
|
|
166 {
|
|
167 reset_all_predictors(state, frame_len);
|
|
168 } else {
|
|
169 for (sfb = 0; sfb < ics->pred.limit; sfb++)
|
|
170 {
|
|
171 uint16_t low = ics->swb_offset[sfb];
|
|
172 uint16_t high = ics->swb_offset[sfb+1];
|
|
173
|
|
174 for (bin = low; bin < high; bin++)
|
|
175 {
|
|
176 ic_predict(&state[bin], spec[bin], &spec[bin],
|
|
177 (ics->predictor_data_present &&
|
|
178 ics->pred.prediction_used[sfb]));
|
|
179 }
|
|
180 }
|
|
181
|
|
182 if (ics->predictor_data_present)
|
|
183 {
|
|
184 if (ics->pred.predictor_reset)
|
|
185 {
|
|
186 for (bin = ics->pred.predictor_reset_group_number - 1;
|
|
187 bin < frame_len; bin += 30)
|
|
188 {
|
|
189 reset_pred_state(&state[bin]);
|
|
190 }
|
|
191 }
|
|
192 }
|
|
193 }
|
|
194 }
|
|
195
|
|
196 #endif
|