Mercurial > mplayer.hg
annotate libfaad2/tns.c @ 13941:00d6903e0c56
My e-mail address has changed
author | gpoirier |
---|---|
date | Sun, 14 Nov 2004 14:55:07 +0000 |
parents | 6d50ef45a058 |
children | 2ae5ab4331ca |
rev | line source |
---|---|
10725 | 1 /* |
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding | |
12527 | 3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com |
10725 | 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 ** | |
12625
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
25 ** Initially modified for use with MPlayer by Arpad Gereöffy on 2003/08/30 |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
26 ** $Id: tns.c,v 1.4 2004/06/23 13:50:53 diego Exp $ |
12625
d81145997036
More information about modifications to comply more closely with GPL 2a.
diego
parents:
12527
diff
changeset
|
27 ** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ |
10725 | 28 **/ |
29 | |
30 #include "common.h" | |
31 #include "structs.h" | |
32 | |
33 #include "syntax.h" | |
34 #include "tns.h" | |
35 | |
12527 | 36 |
37 /* static function declarations */ | |
38 static void tns_decode_coef(uint8_t order, uint8_t coef_res_bits, uint8_t coef_compress, | |
39 uint8_t *coef, real_t *a); | |
40 static void tns_ar_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc, | |
41 uint8_t order); | |
42 static void tns_ma_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc, | |
43 uint8_t order); | |
44 | |
45 | |
10725 | 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 { | |
10989 | 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) | |
10725 | 56 }; |
57 static real_t tns_coef_0_4[] = | |
58 { | |
10989 | 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) | |
10725 | 63 }; |
64 static real_t tns_coef_1_3[] = | |
65 { | |
10989 | 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) | |
10725 | 70 }; |
71 static real_t tns_coef_1_4[] = | |
72 { | |
10989 | 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) | |
10725 | 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; | |
12527 | 86 int16_t size; |
87 uint16_t bottom, top, start, end; | |
10725 | 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 | |
12527 | 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]; | |
10725 | 112 |
12527 | 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) | |
10725 | 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 /* TNS encoding for one channel and frame */ | |
135 void tns_encode_frame(ic_stream *ics, tns_info *tns, uint8_t sr_index, | |
136 uint8_t object_type, real_t *spec, uint16_t frame_len) | |
137 { | |
138 uint8_t w, f, tns_order; | |
139 int8_t inc; | |
12527 | 140 int16_t size; |
141 uint16_t bottom, top, start, end; | |
10725 | 142 uint16_t nshort = frame_len/8; |
143 real_t lpc[TNS_MAX_ORDER+1]; | |
144 | |
145 if (!ics->tns_data_present) | |
146 return; | |
147 | |
148 for (w = 0; w < ics->num_windows; w++) | |
149 { | |
150 bottom = ics->num_swb; | |
151 | |
152 for (f = 0; f < tns->n_filt[w]; f++) | |
153 { | |
154 top = bottom; | |
155 bottom = max(top - tns->length[w][f], 0); | |
156 tns_order = min(tns->order[w][f], TNS_MAX_ORDER); | |
157 if (!tns_order) | |
158 continue; | |
159 | |
160 tns_decode_coef(tns_order, tns->coef_res[w]+3, | |
161 tns->coef_compress[w][f], tns->coef[w][f], lpc); | |
162 | |
12527 | 163 start = min(bottom, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE))); |
164 start = min(start, ics->max_sfb); | |
165 start = ics->swb_offset[start]; | |
10725 | 166 |
12527 | 167 end = min(top, max_tns_sfb(sr_index, object_type, (ics->window_sequence == EIGHT_SHORT_SEQUENCE))); |
168 end = min(end, ics->max_sfb); | |
169 end = ics->swb_offset[end]; | |
170 | |
171 size = end - start; | |
172 if (size <= 0) | |
10725 | 173 continue; |
174 | |
175 if (tns->direction[w][f]) | |
176 { | |
177 inc = -1; | |
178 start = end - 1; | |
179 } else { | |
180 inc = 1; | |
181 } | |
182 | |
183 tns_ma_filter(&spec[(w*nshort)+start], size, inc, lpc, tns_order); | |
184 } | |
185 } | |
186 } | |
187 | |
188 /* Decoder transmitted coefficients for one TNS filter */ | |
189 static void tns_decode_coef(uint8_t order, uint8_t coef_res_bits, uint8_t coef_compress, | |
190 uint8_t *coef, real_t *a) | |
191 { | |
192 uint8_t i, m; | |
193 real_t tmp2[TNS_MAX_ORDER+1], b[TNS_MAX_ORDER+1]; | |
194 | |
195 /* Conversion to signed integer */ | |
196 for (i = 0; i < order; i++) | |
197 { | |
198 if (coef_compress == 0) | |
199 { | |
200 if (coef_res_bits == 3) | |
201 { | |
202 tmp2[i] = tns_coef_0_3[coef[i]]; | |
203 } else { | |
204 tmp2[i] = tns_coef_0_4[coef[i]]; | |
205 } | |
206 } else { | |
207 if (coef_res_bits == 3) | |
208 { | |
209 tmp2[i] = tns_coef_1_3[coef[i]]; | |
210 } else { | |
211 tmp2[i] = tns_coef_1_4[coef[i]]; | |
212 } | |
213 } | |
214 } | |
215 | |
216 /* Conversion to LPC coefficients */ | |
217 a[0] = COEF_CONST(1.0); | |
218 for (m = 1; m <= order; m++) | |
219 { | |
220 for (i = 1; i < m; i++) /* loop only while i<m */ | |
12527 | 221 b[i] = a[i] + MUL_C(tmp2[m-1], a[m-i]); |
10725 | 222 |
223 for (i = 1; i < m; i++) /* loop only while i<m */ | |
224 a[i] = b[i]; | |
225 | |
226 a[m] = tmp2[m-1]; /* changed */ | |
227 } | |
228 } | |
229 | |
230 static void tns_ar_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc, | |
231 uint8_t order) | |
232 { | |
233 /* | |
234 - Simple all-pole filter of order "order" defined by | |
235 y(n) = x(n) - lpc[1]*y(n-1) - ... - lpc[order]*y(n-order) | |
236 - The state variables of the filter are initialized to zero every time | |
237 - The output data is written over the input data ("in-place operation") | |
238 - An input vector of "size" samples is processed and the index increment | |
239 to the next data sample is given by "inc" | |
240 */ | |
241 | |
242 uint8_t j; | |
243 uint16_t i; | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
244 real_t y; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
245 /* state is stored as a double ringbuffer */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
246 real_t state[2*TNS_MAX_ORDER] = {0}; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
247 int8_t state_index = 0; |
10725 | 248 |
249 for (i = 0; i < size; i++) | |
250 { | |
251 y = *spectrum; | |
252 | |
253 for (j = 0; j < order; j++) | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
254 y -= MUL_C(state[state_index+j], lpc[j+1]); |
10725 | 255 |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
256 /* double ringbuffer state */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
257 state_index--; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
258 if (state_index < 0) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
259 state_index = order-1; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
260 state[state_index] = state[state_index + order] = y; |
10725 | 261 |
262 *spectrum = y; | |
263 spectrum += inc; | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
264 |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
265 //#define TNS_PRINT |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
266 #ifdef TNS_PRINT |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
267 //printf("%d\n", y); |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
268 printf("0x%.8X\n", y); |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
269 #endif |
10725 | 270 } |
271 } | |
272 | |
273 static void tns_ma_filter(real_t *spectrum, uint16_t size, int8_t inc, real_t *lpc, | |
274 uint8_t order) | |
275 { | |
276 /* | |
277 - Simple all-zero filter of order "order" defined by | |
278 y(n) = x(n) + a(2)*x(n-1) + ... + a(order+1)*x(n-order) | |
279 - The state variables of the filter are initialized to zero every time | |
280 - The output data is written over the input data ("in-place operation") | |
281 - An input vector of "size" samples is processed and the index increment | |
282 to the next data sample is given by "inc" | |
283 */ | |
284 | |
285 uint8_t j; | |
286 uint16_t i; | |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
287 real_t y; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
288 /* state is stored as a double ringbuffer */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
289 real_t state[2*TNS_MAX_ORDER] = {0}; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
290 int8_t state_index = 0; |
10725 | 291 |
292 for (i = 0; i < size; i++) | |
293 { | |
294 y = *spectrum; | |
295 | |
296 for (j = 0; j < order; j++) | |
12527 | 297 y += MUL_C(state[j], lpc[j+1]); |
10725 | 298 |
13453
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
299 /* double ringbuffer state */ |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
300 state_index--; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
301 if (state_index < 0) |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
302 state_index = order-1; |
6d50ef45a058
Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12.
diego
parents:
12625
diff
changeset
|
303 state[state_index] = state[state_index + order] = *spectrum; |
10725 | 304 |
305 *spectrum = y; | |
306 spectrum += inc; | |
307 } | |
308 } |