2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com
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.
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.
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.
19 ** Any non-GPL usage of this software or parts of this software is strictly
22 ** The "appropriate copyright message" mentioned in section 2c of the GPLv2
23 ** must read: "Code from FAAD2 is copyright (c) Nero AG, www.nero.com"
25 ** Commercial non-GPL licensing of this software is possible.
26 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com.
28 ** $Id: tns.c,v 1.40 2007/11/01 12:33:40 menno Exp $
38 /* static function declarations */
39 static void tns_decode_coef(uint8_t order, uint8_t coef_res_bits, uint8_t coef_compress,
40 uint8_t *coef, real_t *a);
41 static void tns_ar_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
43 static void tns_ma_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
48 #pragma warning(disable:4305)
49 #pragma warning(disable:4244)
51 static real_t tns_coef_0_3[] =
53 COEF_CONST(0.0), COEF_CONST(0.4338837391), COEF_CONST(0.7818314825), COEF_CONST(0.9749279122),
54 COEF_CONST(-0.9848077530), COEF_CONST(-0.8660254038), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433),
55 COEF_CONST(-0.4338837391), COEF_CONST(-0.7818314825), COEF_CONST(-0.9749279122), COEF_CONST(-0.9749279122),
56 COEF_CONST(-0.9848077530), COEF_CONST(-0.8660254038), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433)
58 static real_t tns_coef_0_4[] =
60 COEF_CONST(0.0), COEF_CONST(0.2079116908), COEF_CONST(0.4067366431), COEF_CONST(0.5877852523),
61 COEF_CONST(0.7431448255), COEF_CONST(0.8660254038), COEF_CONST(0.9510565163), COEF_CONST(0.9945218954),
62 COEF_CONST(-0.9957341763), COEF_CONST(-0.9618256432), COEF_CONST(-0.8951632914), COEF_CONST(-0.7980172273),
63 COEF_CONST(-0.6736956436), COEF_CONST(-0.5264321629), COEF_CONST(-0.3612416662), COEF_CONST(-0.1837495178)
65 static real_t tns_coef_1_3[] =
67 COEF_CONST(0.0), COEF_CONST(0.4338837391), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433),
68 COEF_CONST(0.9749279122), COEF_CONST(0.7818314825), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433),
69 COEF_CONST(-0.4338837391), COEF_CONST(-0.7818314825), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433),
70 COEF_CONST(-0.7818314825), COEF_CONST(-0.4338837391), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433)
72 static real_t tns_coef_1_4[] =
74 COEF_CONST(0.0), COEF_CONST(0.2079116908), COEF_CONST(0.4067366431), COEF_CONST(0.5877852523),
75 COEF_CONST(-0.6736956436), COEF_CONST(-0.5264321629), COEF_CONST(-0.3612416662), COEF_CONST(-0.1837495178),
76 COEF_CONST(0.9945218954), COEF_CONST(0.9510565163), COEF_CONST(0.8660254038), COEF_CONST(0.7431448255),
77 COEF_CONST(-0.6736956436), COEF_CONST(-0.5264321629), COEF_CONST(-0.3612416662), COEF_CONST(-0.1837495178)
81 /* TNS decoding for one channel and frame */
82 void tns_decode_frame(ic_stream *ics, tns_info *tns, uint8_t sr_index,
83 uint8_t object_type, real_t *spec, uint16_t frame_len)
85 uint8_t w, f, tns_order;
88 uint16_t bottom, top, start, end;
89 uint16_t nshort = frame_len/8;
90 real_t lpc[TNS_MAX_ORDER+1];
92 if (!ics->tns_data_present)
95 for (w = 0; w < ics->num_windows; w++)
97 bottom = ics->num_swb;
99 for (f = 0; f < tns->n_filt[w]; f++)
102 bottom = max(top - tns->length[w][f], 0);
103 tns_order = min(tns->order[w][f], TNS_MAX_ORDER);
107 tns_decode_coef(tns_order, tns->coef_res[w]+3,
108 tns->coef_compress[w][f], tns->coef[w][f], lpc);
110 start = min(bottom, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE)));
111 start = min(start, ics->max_sfb);
112 start = min(ics->swb_offset[start], ics->swb_offset_max);
114 end = min(top, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE)));
115 end = min(end, ics->max_sfb);
116 end = min(ics->swb_offset[end], ics->swb_offset_max);
122 if (tns->direction[w][f])
130 tns_ar_filter(&spec[(w*nshort)+start], size, inc, lpc, tns_order);
135 /* TNS encoding for one channel and frame */
136 void tns_encode_frame(ic_stream *ics, tns_info *tns, uint8_t sr_index,
137 uint8_t object_type, real_t *spec, uint16_t frame_len)
139 uint8_t w, f, tns_order;
142 uint16_t bottom, top, start, end;
143 uint16_t nshort = frame_len/8;
144 real_t lpc[TNS_MAX_ORDER+1];
146 if (!ics->tns_data_present)
149 for (w = 0; w < ics->num_windows; w++)
151 bottom = ics->num_swb;
153 for (f = 0; f < tns->n_filt[w]; f++)
156 bottom = max(top - tns->length[w][f], 0);
157 tns_order = min(tns->order[w][f], TNS_MAX_ORDER);
161 tns_decode_coef(tns_order, tns->coef_res[w]+3,
162 tns->coef_compress[w][f], tns->coef[w][f], lpc);
164 start = min(bottom, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE)));
165 start = min(start, ics->max_sfb);
166 start = min(ics->swb_offset[start], ics->swb_offset_max);
168 end = min(top, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE)));
169 end = min(end, ics->max_sfb);
170 end = min(ics->swb_offset[end], ics->swb_offset_max);
176 if (tns->direction[w][f])
184 tns_ma_filter(&spec[(w*nshort)+start], size, inc, lpc, tns_order);
189 /* Decoder transmitted coefficients for one TNS filter */
190 static void tns_decode_coef(uint8_t order, uint8_t coef_res_bits, uint8_t coef_compress,
191 uint8_t *coef, real_t *a)
194 real_t tmp2[TNS_MAX_ORDER+1], b[TNS_MAX_ORDER+1];
196 /* Conversion to signed integer */
197 for (i = 0; i < order; i++)
199 if (coef_compress == 0)
201 if (coef_res_bits == 3)
203 tmp2[i] = tns_coef_0_3[coef[i]];
205 tmp2[i] = tns_coef_0_4[coef[i]];
208 if (coef_res_bits == 3)
210 tmp2[i] = tns_coef_1_3[coef[i]];
212 tmp2[i] = tns_coef_1_4[coef[i]];
217 /* Conversion to LPC coefficients */
218 a[0] = COEF_CONST(1.0);
219 for (m = 1; m <= order; m++)
221 for (i = 1; i < m; i++) /* loop only while i<m */
222 b[i] = a[i] + MUL_C(tmp2[m-1], a[m-i]);
224 for (i = 1; i < m; i++) /* loop only while i<m */
227 a[m] = tmp2[m-1]; /* changed */
231 static void tns_ar_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
235 - Simple all-pole filter of order "order" defined by
236 y(n) = x(n) - lpc[1]*y(n-1) - ... - lpc[order]*y(n-order)
237 - The state variables of the filter are initialized to zero every time
238 - The output data is written over the input data ("in-place operation")
239 - An input vector of "size" samples is processed and the index increment
240 to the next data sample is given by "inc"
246 /* state is stored as a double ringbuffer */
247 real_t state[2*TNS_MAX_ORDER] = {0};
248 int8_t state_index = 0;
250 for (i = 0; i < size; i++)
254 for (j = 0; j < order; j++)
255 y -= MUL_C(state[state_index+j], lpc[j+1]);
257 /* double ringbuffer state */
260 state_index = order-1;
261 state[state_index] = state[state_index + order] = y;
269 printf("0x%.8X\n", y);
274 static void tns_ma_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc,
278 - Simple all-zero filter of order "order" defined by
279 y(n) = x(n) + a(2)*x(n-1) + ... + a(order+1)*x(n-order)
280 - The state variables of the filter are initialized to zero every time
281 - The output data is written over the input data ("in-place operation")
282 - An input vector of "size" samples is processed and the index increment
283 to the next data sample is given by "inc"
289 /* state is stored as a double ringbuffer */
290 real_t state[2*TNS_MAX_ORDER] = {0};
291 int8_t state_index = 0;
293 for (i = 0; i < size; i++)
297 for (j = 0; j < order; j++)
298 y += MUL_C(state[state_index+j], lpc[j+1]);
300 /* double ringbuffer state */
303 state_index = order-1;
304 state[state_index] = state[state_index + order] = *spectrum;