1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/*
** FAAD - Freeware Advanced Audio Decoder
** Copyright (C) 2002 M. Bakker
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
**
** $Id: tns.c,v 1.2 2002/12/16 19:02:10 miguelfreitas Exp $
**/
#include "common.h"
#include "structs.h"
#include "syntax.h"
#include "tns.h"
#ifdef FIXED_POINT
static real_t tns_coef_0_3[] =
{
0x0, 0x6F13013, 0xC8261BA, 0xF994E02,
0xF03E3A3A, 0xF224C28C, 0xF5B72457, 0xFA8715E3,
0xF90ECFED, 0xF37D9E46, 0xF066B1FE, 0xF066B1FE,
0xF03E3A3A, 0xF224C28C, 0xF5B72457, 0xFA8715E3
};
static real_t tns_coef_0_4[] =
{
0x0, 0x3539B35, 0x681FE48, 0x9679182,
0xBE3EBD4, 0xDDB3D74, 0xF378709, 0xFE98FCA,
0xF011790B, 0xF09C5CB7, 0xF1AD6942, 0xF33B524A,
0xF5388AEB, 0xF793BBDF, 0xFA385AA9, 0xFD0F5CAB
};
static real_t tns_coef_1_3[] =
{
0x0, 0x6F13013, 0xF5B72457, 0xFA8715E3,
0xF994E02, 0xC8261BA, 0xF5B72457, 0xFA8715E3,
0xF90ECFED, 0xF37D9E46, 0xF5B72457, 0xFA8715E3,
0xF37D9E46, 0xF90ECFED, 0xF5B72457, 0xFA8715E3
};
static real_t tns_coef_1_4[] =
{
0x0, 0x3539B35, 0x681FE48, 0x9679182,
0xF5388AEB, 0xF793BBDF, 0xFA385AA9, 0xFD0F5CAB,
0xFE98FCA, 0xF378709, 0xDDB3D74, 0xBE3EBD4,
0xF5388AEB, 0xF793BBDF, 0xFA385AA9, 0xFD0F5CAB
};
#else
#ifdef _MSC_VER
#pragma warning(disable:4305)
#pragma warning(disable:4244)
#endif
static real_t tns_coef_0_3[] =
{
0.0, 0.4338837391, 0.7818314825, 0.9749279122,
-0.9848077530, -0.8660254038, -0.6427876097, -0.3420201433,
-0.4338837391, -0.7818314825, -0.9749279122, -0.9749279122,
-0.9848077530, -0.8660254038, -0.6427876097, -0.3420201433
};
static real_t tns_coef_0_4[] =
{
0.0, 0.2079116908, 0.4067366431, 0.5877852523,
0.7431448255, 0.8660254038, 0.9510565163, 0.9945218954,
-0.9957341763, -0.9618256432, -0.8951632914, -0.7980172273,
-0.6736956436, -0.5264321629, -0.3612416662, -0.1837495178
};
static real_t tns_coef_1_3[] =
{
0.0, 0.4338837391, -0.6427876097, -0.3420201433,
0.9749279122, 0.7818314825, -0.6427876097, -0.3420201433,
-0.4338837391, -0.7818314825, -0.6427876097, -0.3420201433,
-0.7818314825, -0.4338837391, -0.6427876097, -0.3420201433
};
static real_t tns_coef_1_4[] =
{
0.0, 0.2079116908, 0.4067366431, 0.5877852523,
-0.6736956436, -0.5264321629, -0.3612416662, -0.1837495178,
0.9945218954, 0.9510565163, 0.8660254038, 0.7431448255,
-0.6736956436, -0.5264321629, -0.3612416662, -0.1837495178
};
#endif
/* TNS decoding for one channel and frame */
void tns_decode_frame(ic_stream *ics, tns_info *tns, uint8_t sr_index,
uint8_t object_type, real_t *spec, uint16_t frame_len)
{
uint8_t w, f, tns_order;
int8_t inc;
uint16_t bottom, top, start, end, size;
uint16_t nshort = frame_len/8;
real_t lpc[TNS_MAX_ORDER+1];
if (!ics->tns_data_present)
return;
for (w = 0; w < ics->num_windows; w++)
{
bottom = ics->num_swb;
for (f = 0; f < tns->n_filt[w]; f++)
{
top = bottom;
bottom = max(top - tns->length[w][f], 0);
tns_order = min(tns->order[w][f], tns_max_order(ics, sr_index,
object_type));
if (!tns_order)
continue;
tns_decode_coef(tns_order, tns->coef_res[w]+3,
tns->coef_compress[w][f], tns->coef[w][f], lpc);
start = ics->swb_offset[min(bottom,
min(tns_max_bands(ics, sr_index, object_type, frame_len),
ics->max_sfb))];
end = ics->swb_offset[min(top,
min(tns_max_bands(ics, sr_index, object_type, frame_len),
ics->max_sfb))];
if ((size = end - start) <= 0)
continue;
if (tns->direction[w][f])
{
inc = -1;
start = end - 1;
} else {
inc = 1;
}
tns_ar_filter(&spec[(w*nshort)+start], size, inc, lpc, tns_order);
}
}
}
/* TNS encoding for one channel and frame */
void tns_encode_frame(ic_stream *ics, tns_info *tns, uint8_t sr_index,
uint8_t object_type, real_t *spec, uint16_t frame_len)
{
uint8_t w, f, tns_order;
int8_t inc;
uint16_t bottom, top, start, end, size;
uint16_t nshort = frame_len/8;
real_t lpc[TNS_MAX_ORDER+1];
if (!ics->tns_data_present)
return;
for (w = 0; w < ics->num_windows; w++)
{
bottom = ics->num_swb;
for (f = 0; f < tns->n_filt[w]; f++)
{
top = bottom;
bottom = max(top - tns->length[w][f], 0);
tns_order = min(tns->order[w][f], tns_max_order(ics, sr_index,
object_type));
if (!tns_order)
continue;
tns_decode_coef(tns_order, tns->coef_res[w]+3,
tns->coef_compress[w][f], tns->coef[w][f], lpc);
start = ics->swb_offset[min(bottom,
min(tns_max_bands(ics, sr_index, object_type, frame_len),
ics->max_sfb))];
end = ics->swb_offset[min(top,
min(tns_max_bands(ics, sr_index, object_type, frame_len),
ics->max_sfb))];
if ((size = end - start) <= 0)
continue;
if (tns->direction[w][f])
{
inc = -1;
start = end - 1;
} else {
inc = 1;
}
tns_ma_filter(&spec[(w*nshort)+start], size, inc, lpc, tns_order);
}
}
}
/* Decoder transmitted coefficients for one TNS filter */
static void tns_decode_coef(uint8_t order, uint8_t coef_res_bits, uint8_t coef_compress,
uint8_t *coef, real_t *a)
{
uint8_t i, m;
real_t tmp2[TNS_MAX_ORDER+1], b[TNS_MAX_ORDER+1];
/* Conversion to signed integer */
for (i = 0; i < order; i++)
{
if (coef_compress == 0)
{
if (coef_res_bits == 3)
{
tmp2[i] = tns_coef_0_3[coef[i]];
} else {
tmp2[i] = tns_coef_0_4[coef[i]];
}
} else {
if (coef_res_bits == 3)
{
tmp2[i] = tns_coef_1_3[coef[i]];
} else {
tmp2[i] = tns_coef_1_4[coef[i]];
}
}
}
/* Conversion to LPC coefficients */
a[0] = COEF_CONST(1.0);
for (m = 1; m <= order; m++)
{
for (i = 1; i < m; i++) /* loop only while i<m */
b[i] = a[i] + MUL_C_C(tmp2[m-1], a[m-i]);
for (i = 1; i < m; i++) /* loop only while i<m */
a[i] = b[i];
a[m] = tmp2[m-1]; /* changed */
}
}
static void tns_ar_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
uint8_t order)
{
/*
- Simple all-pole filter of order "order" defined by
y(n) = x(n) - lpc[1]*y(n-1) - ... - lpc[order]*y(n-order)
- The state variables of the filter are initialized to zero every time
- The output data is written over the input data ("in-place operation")
- An input vector of "size" samples is processed and the index increment
to the next data sample is given by "inc"
*/
uint8_t j;
uint16_t i;
real_t y, state[TNS_MAX_ORDER];
for (i = 0; i < order; i++)
state[i] = 0;
for (i = 0; i < size; i++)
{
y = *spectrum;
for (j = 0; j < order; j++)
y -= MUL_R_C(state[j], lpc[j+1]);
for (j = order-1; j > 0; j--)
state[j] = state[j-1];
state[0] = y;
*spectrum = y;
spectrum += inc;
}
}
static void tns_ma_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
uint8_t order)
{
/*
- Simple all-zero filter of order "order" defined by
y(n) = x(n) + a(2)*x(n-1) + ... + a(order+1)*x(n-order)
- The state variables of the filter are initialized to zero every time
- The output data is written over the input data ("in-place operation")
- An input vector of "size" samples is processed and the index increment
to the next data sample is given by "inc"
*/
uint8_t j;
uint16_t i;
real_t y, state[TNS_MAX_ORDER];
for (i = 0; i < order; i++)
state[i] = REAL_CONST(0.0);
for (i = 0; i < size; i++)
{
y = *spectrum;
for (j = 0; j < order; j++)
y += MUL_R_C(state[j], lpc[j+1]);
for (j = order-1; j > 0; j--)
state[j] = state[j-1];
state[0] = *spectrum;
*spectrum = y;
spectrum += inc;
}
}
static uint8_t tns_max_bands_table[12][6] =
{
/* entry for each sampling rate
* 1 Main/LC long window
* 2 Main/LC short window
* 3 SSR long window
* 4 SSR short window
* 5 LD 512 window
* 6 LD 480 window
*/
{ 31, 9, 28, 7, 0, 0 }, /* 96000 */
{ 31, 9, 28, 7, 0, 0 }, /* 88200 */
{ 34, 10, 27, 7, 0, 0 }, /* 64000 */
{ 40, 14, 26, 6, 31, 31 }, /* 48000 */
{ 42, 14, 26, 6, 32, 32 }, /* 44100 */
{ 51, 14, 26, 6, 37, 37 }, /* 32000 */
{ 46, 14, 29, 7, 31, 30 }, /* 24000 */
{ 46, 14, 29, 7, 31, 30 }, /* 22050 */
{ 42, 14, 23, 8, 0, 0 }, /* 16000 */
{ 42, 14, 23, 8, 0, 0 }, /* 12000 */
{ 42, 14, 23, 8, 0, 0 }, /* 11025 */
{ 39, 14, 19, 7, 0, 0 }, /* 8000 */
};
static uint8_t tns_max_bands(ic_stream *ics, uint8_t sr_index,
uint8_t object_type, uint16_t frame_len)
{
uint8_t i;
i = (ics->window_sequence == EIGHT_SHORT_SEQUENCE) ? 1 : 0;
#ifdef LD_DEC
if (object_type == LD)
{
if (frame_len == 512)
i = 4;
else
i = 5;
}
#endif
return tns_max_bands_table[sr_index][i];
}
static uint8_t tns_max_order(ic_stream *ics, uint8_t sr_index,
uint8_t object_type)
{
/* Correction in 14496-3 Cor. 1
Works like MPEG2-AAC (13818-7) now
For other object types (scalable) the following goes for tns max order
for long windows:
if (sr_index <= 5)
return 12;
else
return 20;
*/
if (ics->window_sequence != EIGHT_SHORT_SEQUENCE)
{
switch (object_type)
{
case MAIN:
case LTP:
case ER_LTP:
#ifdef LD_DEC
case LD:
#endif
return 20;
case LC:
case ER_LC:
case DRM_ER_LC:
case SSR:
return 12;
}
} else {
return 7;
}
return 0;
}
|