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