comparison libfaad2/tns.c @ 10725:e989150f8216

libfaad2 v2.0rc1 imported
author arpi
date Sat, 30 Aug 2003 22:30:28 +0000
parents
children 3185f64f6350
comparison
equal deleted inserted replaced
10724:adf5697b9d83 10725:e989150f8216
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: tns.c,v 1.21 2003/07/29 08:20:14 menno Exp $
26 **/
27
28 #include "common.h"
29 #include "structs.h"
30
31 #include "syntax.h"
32 #include "tns.h"
33
34 #ifdef FIXED_POINT
35 static real_t tns_coef_0_3[] =
36 {
37 0x0, 0x6F13013, 0xC8261BA, 0xF994E02,
38 0xF03E3A3A, 0xF224C28C, 0xF5B72457, 0xFA8715E3,
39 0xF90ECFED, 0xF37D9E46, 0xF066B1FE, 0xF066B1FE,
40 0xF03E3A3A, 0xF224C28C, 0xF5B72457, 0xFA8715E3
41 };
42 static real_t tns_coef_0_4[] =
43 {
44 0x0, 0x3539B35, 0x681FE48, 0x9679182,
45 0xBE3EBD4, 0xDDB3D74, 0xF378709, 0xFE98FCA,
46 0xF011790B, 0xF09C5CB7, 0xF1AD6942, 0xF33B524A,
47 0xF5388AEB, 0xF793BBDF, 0xFA385AA9, 0xFD0F5CAB
48 };
49 static real_t tns_coef_1_3[] =
50 {
51 0x0, 0x6F13013, 0xF5B72457, 0xFA8715E3,
52 0xF994E02, 0xC8261BA, 0xF5B72457, 0xFA8715E3,
53 0xF90ECFED, 0xF37D9E46, 0xF5B72457, 0xFA8715E3,
54 0xF37D9E46, 0xF90ECFED, 0xF5B72457, 0xFA8715E3
55 };
56 static real_t tns_coef_1_4[] =
57 {
58 0x0, 0x3539B35, 0x681FE48, 0x9679182,
59 0xF5388AEB, 0xF793BBDF, 0xFA385AA9, 0xFD0F5CAB,
60 0xFE98FCA, 0xF378709, 0xDDB3D74, 0xBE3EBD4,
61 0xF5388AEB, 0xF793BBDF, 0xFA385AA9, 0xFD0F5CAB
62 };
63 #else
64 #ifdef _MSC_VER
65 #pragma warning(disable:4305)
66 #pragma warning(disable:4244)
67 #endif
68 static real_t tns_coef_0_3[] =
69 {
70 0.0, 0.4338837391, 0.7818314825, 0.9749279122,
71 -0.9848077530, -0.8660254038, -0.6427876097, -0.3420201433,
72 -0.4338837391, -0.7818314825, -0.9749279122, -0.9749279122,
73 -0.9848077530, -0.8660254038, -0.6427876097, -0.3420201433
74 };
75 static real_t tns_coef_0_4[] =
76 {
77 0.0, 0.2079116908, 0.4067366431, 0.5877852523,
78 0.7431448255, 0.8660254038, 0.9510565163, 0.9945218954,
79 -0.9957341763, -0.9618256432, -0.8951632914, -0.7980172273,
80 -0.6736956436, -0.5264321629, -0.3612416662, -0.1837495178
81 };
82 static real_t tns_coef_1_3[] =
83 {
84 0.0, 0.4338837391, -0.6427876097, -0.3420201433,
85 0.9749279122, 0.7818314825, -0.6427876097, -0.3420201433,
86 -0.4338837391, -0.7818314825, -0.6427876097, -0.3420201433,
87 -0.7818314825, -0.4338837391, -0.6427876097, -0.3420201433
88 };
89 static real_t tns_coef_1_4[] =
90 {
91 0.0, 0.2079116908, 0.4067366431, 0.5877852523,
92 -0.6736956436, -0.5264321629, -0.3612416662, -0.1837495178,
93 0.9945218954, 0.9510565163, 0.8660254038, 0.7431448255,
94 -0.6736956436, -0.5264321629, -0.3612416662, -0.1837495178
95 };
96 #endif
97
98
99 /* TNS decoding for one channel and frame */
100 void tns_decode_frame(ic_stream *ics, tns_info *tns, uint8_t sr_index,
101 uint8_t object_type, real_t *spec, uint16_t frame_len)
102 {
103 uint8_t w, f, tns_order;
104 int8_t inc;
105 uint16_t bottom, top, start, end, size;
106 uint16_t nshort = frame_len/8;
107 real_t lpc[TNS_MAX_ORDER+1];
108
109 if (!ics->tns_data_present)
110 return;
111
112 for (w = 0; w < ics->num_windows; w++)
113 {
114 bottom = ics->num_swb;
115
116 for (f = 0; f < tns->n_filt[w]; f++)
117 {
118 top = bottom;
119 bottom = max(top - tns->length[w][f], 0);
120 tns_order = min(tns->order[w][f], TNS_MAX_ORDER);
121 if (!tns_order)
122 continue;
123
124 tns_decode_coef(tns_order, tns->coef_res[w]+3,
125 tns->coef_compress[w][f], tns->coef[w][f], lpc);
126
127 start = ics->swb_offset[min(bottom, ics->max_sfb)];
128 end = ics->swb_offset[min(top, ics->max_sfb)];
129
130 if ((size = end - start) <= 0)
131 continue;
132
133 if (tns->direction[w][f])
134 {
135 inc = -1;
136 start = end - 1;
137 } else {
138 inc = 1;
139 }
140
141 tns_ar_filter(&spec[(w*nshort)+start], size, inc, lpc, tns_order);
142 }
143 }
144 }
145
146 /* TNS encoding for one channel and frame */
147 void tns_encode_frame(ic_stream *ics, tns_info *tns, uint8_t sr_index,
148 uint8_t object_type, real_t *spec, uint16_t frame_len)
149 {
150 uint8_t w, f, tns_order;
151 int8_t inc;
152 uint16_t bottom, top, start, end, size;
153 uint16_t nshort = frame_len/8;
154 real_t lpc[TNS_MAX_ORDER+1];
155
156 if (!ics->tns_data_present)
157 return;
158
159 for (w = 0; w < ics->num_windows; w++)
160 {
161 bottom = ics->num_swb;
162
163 for (f = 0; f < tns->n_filt[w]; f++)
164 {
165 top = bottom;
166 bottom = max(top - tns->length[w][f], 0);
167 tns_order = min(tns->order[w][f], TNS_MAX_ORDER);
168 if (!tns_order)
169 continue;
170
171 tns_decode_coef(tns_order, tns->coef_res[w]+3,
172 tns->coef_compress[w][f], tns->coef[w][f], lpc);
173
174 start = ics->swb_offset[min(bottom, ics->max_sfb)];
175 end = ics->swb_offset[min(top, ics->max_sfb)];
176
177 if ((size = end - start) <= 0)
178 continue;
179
180 if (tns->direction[w][f])
181 {
182 inc = -1;
183 start = end - 1;
184 } else {
185 inc = 1;
186 }
187
188 tns_ma_filter(&spec[(w*nshort)+start], size, inc, lpc, tns_order);
189 }
190 }
191 }
192
193 /* Decoder transmitted coefficients for one TNS filter */
194 static void tns_decode_coef(uint8_t order, uint8_t coef_res_bits, uint8_t coef_compress,
195 uint8_t *coef, real_t *a)
196 {
197 uint8_t i, m;
198 real_t tmp2[TNS_MAX_ORDER+1], b[TNS_MAX_ORDER+1];
199
200 /* Conversion to signed integer */
201 for (i = 0; i < order; i++)
202 {
203 if (coef_compress == 0)
204 {
205 if (coef_res_bits == 3)
206 {
207 tmp2[i] = tns_coef_0_3[coef[i]];
208 } else {
209 tmp2[i] = tns_coef_0_4[coef[i]];
210 }
211 } else {
212 if (coef_res_bits == 3)
213 {
214 tmp2[i] = tns_coef_1_3[coef[i]];
215 } else {
216 tmp2[i] = tns_coef_1_4[coef[i]];
217 }
218 }
219 }
220
221 /* Conversion to LPC coefficients */
222 a[0] = COEF_CONST(1.0);
223 for (m = 1; m <= order; m++)
224 {
225 for (i = 1; i < m; i++) /* loop only while i<m */
226 b[i] = a[i] + MUL_C_C(tmp2[m-1], a[m-i]);
227
228 for (i = 1; i < m; i++) /* loop only while i<m */
229 a[i] = b[i];
230
231 a[m] = tmp2[m-1]; /* changed */
232 }
233 }
234
235 static void tns_ar_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
236 uint8_t order)
237 {
238 /*
239 - Simple all-pole filter of order "order" defined by
240 y(n) = x(n) - lpc[1]*y(n-1) - ... - lpc[order]*y(n-order)
241 - The state variables of the filter are initialized to zero every time
242 - The output data is written over the input data ("in-place operation")
243 - An input vector of "size" samples is processed and the index increment
244 to the next data sample is given by "inc"
245 */
246
247 uint8_t j;
248 uint16_t i;
249 real_t y, state[TNS_MAX_ORDER];
250
251 for (i = 0; i < order; i++)
252 state[i] = 0;
253
254 for (i = 0; i < size; i++)
255 {
256 y = *spectrum;
257
258 for (j = 0; j < order; j++)
259 y -= MUL_R_C(state[j], lpc[j+1]);
260
261 for (j = order-1; j > 0; j--)
262 state[j] = state[j-1];
263
264 state[0] = y;
265 *spectrum = y;
266 spectrum += inc;
267 }
268 }
269
270 static void tns_ma_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
271 uint8_t order)
272 {
273 /*
274 - Simple all-zero filter of order "order" defined by
275 y(n) = x(n) + a(2)*x(n-1) + ... + a(order+1)*x(n-order)
276 - The state variables of the filter are initialized to zero every time
277 - The output data is written over the input data ("in-place operation")
278 - An input vector of "size" samples is processed and the index increment
279 to the next data sample is given by "inc"
280 */
281
282 uint8_t j;
283 uint16_t i;
284 real_t y, state[TNS_MAX_ORDER];
285
286 for (i = 0; i < order; i++)
287 state[i] = REAL_CONST(0.0);
288
289 for (i = 0; i < size; i++)
290 {
291 y = *spectrum;
292
293 for (j = 0; j < order; j++)
294 y += MUL_R_C(state[j], lpc[j+1]);
295
296 for (j = order-1; j > 0; j--)
297 state[j] = state[j-1];
298
299 state[0] = *spectrum;
300 *spectrum = y;
301 spectrum += inc;
302 }
303 }