diff options
Diffstat (limited to 'lib/rbcodec/codecs/libfaad/tns.c')
-rw-r--r-- | lib/rbcodec/codecs/libfaad/tns.c | 312 |
1 files changed, 312 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libfaad/tns.c b/lib/rbcodec/codecs/libfaad/tns.c new file mode 100644 index 0000000000..a2bec86519 --- /dev/null +++ b/lib/rbcodec/codecs/libfaad/tns.c | |||
@@ -0,0 +1,312 @@ | |||
1 | /* | ||
2 | ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding | ||
3 | ** Copyright (C) 2003-2004 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$ | ||
26 | **/ | ||
27 | |||
28 | #include "common.h" | ||
29 | #include "structs.h" | ||
30 | |||
31 | #include "syntax.h" | ||
32 | #include "tns.h" | ||
33 | |||
34 | |||
35 | /* static function declarations */ | ||
36 | static void tns_decode_coef(uint8_t order, uint8_t coef_res_bits, uint8_t coef_compress, | ||
37 | uint8_t *coef, real_t *a); | ||
38 | static void tns_ar_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc, | ||
39 | int8_t order); | ||
40 | #ifdef LTP_DEC | ||
41 | static void tns_ma_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc, | ||
42 | int8_t order); | ||
43 | #endif | ||
44 | |||
45 | |||
46 | #ifdef _MSC_VER | ||
47 | #pragma warning(disable:4305) | ||
48 | #pragma warning(disable:4244) | ||
49 | #endif | ||
50 | static real_t tns_coef_0_3[] = | ||
51 | { | ||
52 | COEF_CONST(0.0), COEF_CONST(0.4338837391), COEF_CONST(0.7818314825), COEF_CONST(0.9749279122), | ||
53 | COEF_CONST(-0.9848077530), COEF_CONST(-0.8660254038), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433), | ||
54 | COEF_CONST(-0.4338837391), COEF_CONST(-0.7818314825), COEF_CONST(-0.9749279122), COEF_CONST(-0.9749279122), | ||
55 | COEF_CONST(-0.9848077530), COEF_CONST(-0.8660254038), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433) | ||
56 | }; | ||
57 | static real_t tns_coef_0_4[] = | ||
58 | { | ||
59 | COEF_CONST(0.0), COEF_CONST(0.2079116908), COEF_CONST(0.4067366431), COEF_CONST(0.5877852523), | ||
60 | COEF_CONST(0.7431448255), COEF_CONST(0.8660254038), COEF_CONST(0.9510565163), COEF_CONST(0.9945218954), | ||
61 | COEF_CONST(-0.9957341763), COEF_CONST(-0.9618256432), COEF_CONST(-0.8951632914), COEF_CONST(-0.7980172273), | ||
62 | COEF_CONST(-0.6736956436), COEF_CONST(-0.5264321629), COEF_CONST(-0.3612416662), COEF_CONST(-0.1837495178) | ||
63 | }; | ||
64 | static real_t tns_coef_1_3[] = | ||
65 | { | ||
66 | COEF_CONST(0.0), COEF_CONST(0.4338837391), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433), | ||
67 | COEF_CONST(0.9749279122), COEF_CONST(0.7818314825), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433), | ||
68 | COEF_CONST(-0.4338837391), COEF_CONST(-0.7818314825), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433), | ||
69 | COEF_CONST(-0.7818314825), COEF_CONST(-0.4338837391), COEF_CONST(-0.6427876097), COEF_CONST(-0.3420201433) | ||
70 | }; | ||
71 | static real_t tns_coef_1_4[] = | ||
72 | { | ||
73 | COEF_CONST(0.0), COEF_CONST(0.2079116908), COEF_CONST(0.4067366431), COEF_CONST(0.5877852523), | ||
74 | COEF_CONST(-0.6736956436), COEF_CONST(-0.5264321629), COEF_CONST(-0.3612416662), COEF_CONST(-0.1837495178), | ||
75 | COEF_CONST(0.9945218954), COEF_CONST(0.9510565163), COEF_CONST(0.8660254038), COEF_CONST(0.7431448255), | ||
76 | COEF_CONST(-0.6736956436), COEF_CONST(-0.5264321629), COEF_CONST(-0.3612416662), COEF_CONST(-0.1837495178) | ||
77 | }; | ||
78 | |||
79 | |||
80 | /* TNS decoding for one channel and frame */ | ||
81 | void tns_decode_frame(ic_stream *ics, tns_info *tns, uint8_t sr_index, | ||
82 | uint8_t object_type, real_t *spec, uint16_t frame_len) | ||
83 | { | ||
84 | uint8_t w, f, tns_order; | ||
85 | int8_t inc; | ||
86 | int16_t size; | ||
87 | uint16_t bottom, top, start, end; | ||
88 | uint16_t nshort = frame_len/8; | ||
89 | real_t lpc[TNS_MAX_ORDER+1]; | ||
90 | |||
91 | if (!ics->tns_data_present) | ||
92 | return; | ||
93 | |||
94 | for (w = 0; w < ics->num_windows; w++) | ||
95 | { | ||
96 | bottom = ics->num_swb; | ||
97 | |||
98 | for (f = 0; f < tns->n_filt[w]; f++) | ||
99 | { | ||
100 | top = bottom; | ||
101 | bottom = max(top - tns->length[w][f], 0); | ||
102 | tns_order = min(tns->order[w][f], TNS_MAX_ORDER); | ||
103 | if (!tns_order) | ||
104 | continue; | ||
105 | |||
106 | tns_decode_coef(tns_order, tns->coef_res[w]+3, | ||
107 | tns->coef_compress[w][f], tns->coef[w][f], lpc); | ||
108 | |||
109 | start = min(bottom, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE))); | ||
110 | start = min(start, ics->max_sfb); | ||
111 | start = ics->swb_offset[start]; | ||
112 | |||
113 | end = min(top, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE))); | ||
114 | end = min(end, ics->max_sfb); | ||
115 | end = ics->swb_offset[end]; | ||
116 | |||
117 | size = end - start; | ||
118 | if (size <= 0) | ||
119 | continue; | ||
120 | |||
121 | if (tns->direction[w][f]) | ||
122 | { | ||
123 | inc = -1; | ||
124 | start = end - 1; | ||
125 | } else { | ||
126 | inc = 1; | ||
127 | } | ||
128 | |||
129 | tns_ar_filter(&spec[(w*nshort)+start], size, inc, lpc, tns_order); | ||
130 | } | ||
131 | } | ||
132 | } | ||
133 | |||
134 | #ifdef LTP_DEC | ||
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) | ||
138 | { | ||
139 | uint8_t w, f, tns_order; | ||
140 | int8_t inc; | ||
141 | int16_t size; | ||
142 | uint16_t bottom, top, start, end; | ||
143 | uint16_t nshort = frame_len/8; | ||
144 | real_t lpc[TNS_MAX_ORDER+1]; | ||
145 | |||
146 | if (!ics->tns_data_present) | ||
147 | return; | ||
148 | |||
149 | for (w = 0; w < ics->num_windows; w++) | ||
150 | { | ||
151 | bottom = ics->num_swb; | ||
152 | |||
153 | for (f = 0; f < tns->n_filt[w]; f++) | ||
154 | { | ||
155 | top = bottom; | ||
156 | bottom = max(top - tns->length[w][f], 0); | ||
157 | tns_order = min(tns->order[w][f], TNS_MAX_ORDER); | ||
158 | if (!tns_order) | ||
159 | continue; | ||
160 | |||
161 | tns_decode_coef(tns_order, tns->coef_res[w]+3, | ||
162 | tns->coef_compress[w][f], tns->coef[w][f], lpc); | ||
163 | |||
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 = ics->swb_offset[start]; | ||
167 | |||
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 = ics->swb_offset[end]; | ||
171 | |||
172 | size = end - start; | ||
173 | if (size <= 0) | ||
174 | continue; | ||
175 | |||
176 | if (tns->direction[w][f]) | ||
177 | { | ||
178 | inc = -1; | ||
179 | start = end - 1; | ||
180 | } else { | ||
181 | inc = 1; | ||
182 | } | ||
183 | |||
184 | tns_ma_filter(&spec[(w*nshort)+start], size, inc, lpc, tns_order); | ||
185 | } | ||
186 | } | ||
187 | } | ||
188 | #endif | ||
189 | |||
190 | /* Decoder transmitted coefficients for one TNS filter */ | ||
191 | static void tns_decode_coef(uint8_t order, uint8_t coef_res_bits, uint8_t coef_compress, | ||
192 | uint8_t *coef, real_t *a) | ||
193 | { | ||
194 | uint8_t i, m; | ||
195 | real_t tmp2[TNS_MAX_ORDER+1], b[TNS_MAX_ORDER+1]; | ||
196 | |||
197 | /* Conversion to signed integer */ | ||
198 | for (i = 0; i < order; i++) | ||
199 | { | ||
200 | if (coef_compress == 0) | ||
201 | { | ||
202 | if (coef_res_bits == 3) | ||
203 | { | ||
204 | tmp2[i] = tns_coef_0_3[coef[i]]; | ||
205 | } else { | ||
206 | tmp2[i] = tns_coef_0_4[coef[i]]; | ||
207 | } | ||
208 | } else { | ||
209 | if (coef_res_bits == 3) | ||
210 | { | ||
211 | tmp2[i] = tns_coef_1_3[coef[i]]; | ||
212 | } else { | ||
213 | tmp2[i] = tns_coef_1_4[coef[i]]; | ||
214 | } | ||
215 | } | ||
216 | } | ||
217 | |||
218 | /* Conversion to LPC coefficients */ | ||
219 | a[0] = COEF_CONST(1.0); | ||
220 | for (m = 1; m <= order; m++) | ||
221 | { | ||
222 | for (i = 1; i < m; i++) /* loop only while i<m */ | ||
223 | b[i] = a[i] + MUL_C(tmp2[m-1], a[m-i]); | ||
224 | |||
225 | for (i = 1; i < m; i++) /* loop only while i<m */ | ||
226 | a[i] = b[i]; | ||
227 | |||
228 | a[m] = tmp2[m-1]; /* changed */ | ||
229 | } | ||
230 | } | ||
231 | |||
232 | static void tns_ar_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc, | ||
233 | int8_t order) | ||
234 | { | ||
235 | /* | ||
236 | - Simple all-pole filter of order "order" defined by | ||
237 | y(n) = x(n) - lpc[1]*y(n-1) - ... - lpc[order]*y(n-order) | ||
238 | - The state variables of the filter are initialized to zero every time | ||
239 | - The output data is written over the input data ("in-place operation") | ||
240 | - An input vector of "size" samples is processed and the index increment | ||
241 | to the next data sample is given by "inc" | ||
242 | */ | ||
243 | |||
244 | uint8_t j; | ||
245 | uint16_t i; | ||
246 | real_t y; | ||
247 | /* state is stored as a double ringbuffer */ | ||
248 | real_t state[2*TNS_MAX_ORDER] = {0}; | ||
249 | int8_t state_index = 0; | ||
250 | |||
251 | for (i = 0; i < size; i++) | ||
252 | { | ||
253 | y = *spectrum; | ||
254 | |||
255 | for (j = 0; j < order; j++) | ||
256 | y -= MUL_C(state[state_index+j], lpc[j+1]); | ||
257 | |||
258 | /* double ringbuffer state */ | ||
259 | state_index--; | ||
260 | if (state_index < 0) | ||
261 | state_index = order-1; | ||
262 | state[state_index] = state[state_index + order] = y; | ||
263 | |||
264 | *spectrum = y; | ||
265 | spectrum += inc; | ||
266 | |||
267 | //#define TNS_PRINT | ||
268 | #ifdef TNS_PRINT | ||
269 | //printf("%d\n", y); | ||
270 | printf("0x%.8X\n", y); | ||
271 | #endif | ||
272 | } | ||
273 | } | ||
274 | |||
275 | #ifdef LTP_DEC | ||
276 | static void tns_ma_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc, | ||
277 | int8_t order) | ||
278 | { | ||
279 | /* | ||
280 | - Simple all-zero filter of order "order" defined by | ||
281 | y(n) = x(n) + a(2)*x(n-1) + ... + a(order+1)*x(n-order) | ||
282 | - The state variables of the filter are initialized to zero every time | ||
283 | - The output data is written over the input data ("in-place operation") | ||
284 | - An input vector of "size" samples is processed and the index increment | ||
285 | to the next data sample is given by "inc" | ||
286 | */ | ||
287 | |||
288 | uint8_t j; | ||
289 | uint16_t i; | ||
290 | real_t y; | ||
291 | /* state is stored as a double ringbuffer */ | ||
292 | real_t state[2*TNS_MAX_ORDER] = {0}; | ||
293 | int8_t state_index = 0; | ||
294 | |||
295 | for (i = 0; i < size; i++) | ||
296 | { | ||
297 | y = *spectrum; | ||
298 | |||
299 | for (j = 0; j < order; j++) | ||
300 | y += MUL_C(state[j], lpc[j+1]); | ||
301 | |||
302 | /* double ringbuffer state */ | ||
303 | state_index--; | ||
304 | if (state_index < 0) | ||
305 | state_index = order-1; | ||
306 | state[state_index] = state[state_index + order] = *spectrum; | ||
307 | |||
308 | *spectrum = y; | ||
309 | spectrum += inc; | ||
310 | } | ||
311 | } | ||
312 | #endif | ||