comparison libfaad2/specrec.c @ 10725:e989150f8216

libfaad2 v2.0rc1 imported
author arpi
date Sat, 30 Aug 2003 22:30:28 +0000
parents
children 3185f64f6350
comparison
equal deleted inserted replaced
10724:adf5697b9d83 10725:e989150f8216
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 **
25 ** $Id: specrec.c,v 1.23 2003/07/29 08:20:13 menno Exp $
26 **/
27
28 /*
29 Spectral reconstruction:
30 - grouping/sectioning
31 - inverse quantization
32 - applying scalefactors
33 */
34
35 #include "common.h"
36 #include "structs.h"
37
38 #include <string.h>
39 #include "specrec.h"
40 #include "syntax.h"
41 #include "iq_table.h"
42
43 #ifdef LD_DEC
44 static uint8_t num_swb_512_window[] =
45 {
46 0, 0, 0, 36, 36, 37, 31, 31, 0, 0, 0, 0
47 };
48 static uint8_t num_swb_480_window[] =
49 {
50 0, 0, 0, 35, 35, 37, 30, 30, 0, 0, 0, 0
51 };
52 #endif
53
54 static uint8_t num_swb_960_window[] =
55 {
56 40, 40, 45, 49, 49, 49, 46, 46, 42, 42, 42, 40
57 };
58
59 static uint8_t num_swb_1024_window[] =
60 {
61 41, 41, 47, 49, 49, 51, 47, 47, 43, 43, 43, 40
62 };
63
64 static uint8_t num_swb_128_window[] =
65 {
66 12, 12, 12, 14, 14, 14, 15, 15, 15, 15, 15, 15
67 };
68
69 static uint16_t swb_offset_1024_96[] =
70 {
71 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56,
72 64, 72, 80, 88, 96, 108, 120, 132, 144, 156, 172, 188, 212, 240,
73 276, 320, 384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024
74 };
75
76 static uint16_t swb_offset_128_96[] =
77 {
78 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
79 };
80
81 static uint16_t swb_offset_1024_64[] =
82 {
83 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56,
84 64, 72, 80, 88, 100, 112, 124, 140, 156, 172, 192, 216, 240, 268,
85 304, 344, 384, 424, 464, 504, 544, 584, 624, 664, 704, 744, 784, 824,
86 864, 904, 944, 984, 1024
87 };
88
89 static uint16_t swb_offset_128_64[] =
90 {
91 0, 4, 8, 12, 16, 20, 24, 32, 40, 48, 64, 92, 128
92 };
93
94
95 static uint16_t swb_offset_1024_48[] =
96 {
97 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72,
98 80, 88, 96, 108, 120, 132, 144, 160, 176, 196, 216, 240, 264, 292,
99 320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736,
100 768, 800, 832, 864, 896, 928, 1024
101 };
102
103 #ifdef LD_DEC
104 static uint16_t swb_offset_512_48[] =
105 {
106 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 68, 76, 84,
107 92, 100, 112, 124, 136, 148, 164, 184, 208, 236, 268, 300, 332, 364, 396,
108 428, 460, 512
109 };
110
111 static uint16_t swb_offset_480_48[] =
112 {
113 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72 ,80 ,88,
114 96, 108, 120, 132, 144, 156, 172, 188, 212, 240, 272, 304, 336, 368, 400,
115 432, 480
116 };
117 #endif
118
119 static uint16_t swb_offset_128_48[] =
120 {
121 0, 4, 8, 12, 16, 20, 28, 36, 44, 56, 68, 80, 96, 112, 128
122 };
123
124 static uint16_t swb_offset_1024_32[] =
125 {
126 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 48, 56, 64, 72,
127 80, 88, 96, 108, 120, 132, 144, 160, 176, 196, 216, 240, 264, 292,
128 320, 352, 384, 416, 448, 480, 512, 544, 576, 608, 640, 672, 704, 736,
129 768, 800, 832, 864, 896, 928, 960, 992, 1024
130 };
131
132 #ifdef LD_DEC
133 static uint16_t swb_offset_512_32[] =
134 {
135 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 64, 72, 80,
136 88, 96, 108, 120, 132, 144, 160, 176, 192, 212, 236, 260, 288, 320, 352,
137 384, 416, 448, 480, 512
138 };
139
140 static uint16_t swb_offset_480_32[] =
141 {
142 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 64, 72, 80,
143 88, 96, 104, 112, 124, 136, 148, 164, 180, 200, 224, 256, 288, 320, 352,
144 384, 416, 448, 480
145 };
146 #endif
147
148 static uint16_t swb_offset_1024_24[] =
149 {
150 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68,
151 76, 84, 92, 100, 108, 116, 124, 136, 148, 160, 172, 188, 204, 220,
152 240, 260, 284, 308, 336, 364, 396, 432, 468, 508, 552, 600, 652, 704,
153 768, 832, 896, 960, 1024
154 };
155
156 #ifdef LD_DEC
157 static uint16_t swb_offset_512_24[] =
158 {
159 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68,
160 80, 92, 104, 120, 140, 164, 192, 224, 256, 288, 320, 352, 384, 416,
161 448, 480, 512
162 };
163
164 static uint16_t swb_offset_480_24[] =
165 {
166 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 52, 60, 68, 80, 92, 104, 120,
167 140, 164, 192, 224, 256, 288, 320, 352, 384, 416, 448, 480
168 };
169 #endif
170
171 static uint16_t swb_offset_128_24[] =
172 {
173 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 64, 76, 92, 108, 128
174 };
175
176 static uint16_t swb_offset_1024_16[] =
177 {
178 0, 8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 100, 112, 124,
179 136, 148, 160, 172, 184, 196, 212, 228, 244, 260, 280, 300, 320, 344,
180 368, 396, 424, 456, 492, 532, 572, 616, 664, 716, 772, 832, 896, 960, 1024
181 };
182
183 static uint16_t swb_offset_128_16[] =
184 {
185 0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 60, 72, 88, 108, 128
186 };
187
188 static uint16_t swb_offset_1024_8[] =
189 {
190 0, 12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144, 156, 172,
191 188, 204, 220, 236, 252, 268, 288, 308, 328, 348, 372, 396, 420, 448,
192 476, 508, 544, 580, 620, 664, 712, 764, 820, 880, 944, 1024
193 };
194
195 static uint16_t swb_offset_128_8[] =
196 {
197 0, 4, 8, 12, 16, 20, 24, 28, 36, 44, 52, 60, 72, 88, 108, 128
198 };
199
200 static uint16_t *swb_offset_1024_window[] =
201 {
202 swb_offset_1024_96, /* 96000 */
203 swb_offset_1024_96, /* 88200 */
204 swb_offset_1024_64, /* 64000 */
205 swb_offset_1024_48, /* 48000 */
206 swb_offset_1024_48, /* 44100 */
207 swb_offset_1024_32, /* 32000 */
208 swb_offset_1024_24, /* 24000 */
209 swb_offset_1024_24, /* 22050 */
210 swb_offset_1024_16, /* 16000 */
211 swb_offset_1024_16, /* 12000 */
212 swb_offset_1024_16, /* 11025 */
213 swb_offset_1024_8 /* 8000 */
214 };
215
216 #ifdef LD_DEC
217 static uint16_t *swb_offset_512_window[] =
218 {
219 0, /* 96000 */
220 0, /* 88200 */
221 0, /* 64000 */
222 swb_offset_512_48, /* 48000 */
223 swb_offset_512_48, /* 44100 */
224 swb_offset_512_32, /* 32000 */
225 swb_offset_512_24, /* 24000 */
226 swb_offset_512_24, /* 22050 */
227 0, /* 16000 */
228 0, /* 12000 */
229 0, /* 11025 */
230 0 /* 8000 */
231 };
232
233 static uint16_t *swb_offset_480_window[] =
234 {
235 0, /* 96000 */
236 0, /* 88200 */
237 0, /* 64000 */
238 swb_offset_480_48, /* 48000 */
239 swb_offset_480_48, /* 44100 */
240 swb_offset_480_32, /* 32000 */
241 swb_offset_480_24, /* 24000 */
242 swb_offset_480_24, /* 22050 */
243 0, /* 16000 */
244 0, /* 12000 */
245 0, /* 11025 */
246 0 /* 8000 */
247 };
248 #endif
249
250 static uint16_t *swb_offset_128_window[] =
251 {
252 swb_offset_128_96, /* 96000 */
253 swb_offset_128_96, /* 88200 */
254 swb_offset_128_64, /* 64000 */
255 swb_offset_128_48, /* 48000 */
256 swb_offset_128_48, /* 44100 */
257 swb_offset_128_48, /* 32000 */
258 swb_offset_128_24, /* 24000 */
259 swb_offset_128_24, /* 22050 */
260 swb_offset_128_16, /* 16000 */
261 swb_offset_128_16, /* 12000 */
262 swb_offset_128_16, /* 11025 */
263 swb_offset_128_8 /* 8000 */
264 };
265
266 #define bit_set(A, B) ((A) & (1<<(B)))
267
268 /* 4.5.2.3.4 */
269 /*
270 - determine the number of windows in a window_sequence named num_windows
271 - determine the number of window_groups named num_window_groups
272 - determine the number of windows in each group named window_group_length[g]
273 - determine the total number of scalefactor window bands named num_swb for
274 the actual window type
275 - determine swb_offset[swb], the offset of the first coefficient in
276 scalefactor window band named swb of the window actually used
277 - determine sect_sfb_offset[g][section],the offset of the first coefficient
278 in section named section. This offset depends on window_sequence and
279 scale_factor_grouping and is needed to decode the spectral_data().
280 */
281 uint8_t window_grouping_info(faacDecHandle hDecoder, ic_stream *ics)
282 {
283 uint8_t i, g;
284
285 uint8_t sf_index = hDecoder->sf_index;
286
287 switch (ics->window_sequence) {
288 case ONLY_LONG_SEQUENCE:
289 case LONG_START_SEQUENCE:
290 case LONG_STOP_SEQUENCE:
291 ics->num_windows = 1;
292 ics->num_window_groups = 1;
293 ics->window_group_length[ics->num_window_groups-1] = 1;
294 #ifdef LD_DEC
295 if (hDecoder->object_type == LD)
296 {
297 if (hDecoder->frameLength == 512)
298 ics->num_swb = num_swb_512_window[sf_index];
299 else /* if (hDecoder->frameLength == 480) */
300 ics->num_swb = num_swb_480_window[sf_index];
301 } else {
302 #endif
303 if (hDecoder->frameLength == 1024)
304 ics->num_swb = num_swb_1024_window[sf_index];
305 else /* if (hDecoder->frameLength == 960) */
306 ics->num_swb = num_swb_960_window[sf_index];
307 #ifdef LD_DEC
308 }
309 #endif
310
311 /* preparation of sect_sfb_offset for long blocks */
312 /* also copy the last value! */
313 #ifdef LD_DEC
314 if (hDecoder->object_type == LD)
315 {
316 if (hDecoder->frameLength == 512)
317 {
318 for (i = 0; i < ics->num_swb; i++)
319 {
320 ics->sect_sfb_offset[0][i] = swb_offset_512_window[sf_index][i];
321 ics->swb_offset[i] = swb_offset_512_window[sf_index][i];
322 }
323 } else /* if (hDecoder->frameLength == 480) */ {
324 for (i = 0; i < ics->num_swb; i++)
325 {
326 ics->sect_sfb_offset[0][i] = swb_offset_480_window[sf_index][i];
327 ics->swb_offset[i] = swb_offset_480_window[sf_index][i];
328 }
329 }
330 ics->sect_sfb_offset[0][ics->num_swb] = hDecoder->frameLength;
331 ics->swb_offset[ics->num_swb] = hDecoder->frameLength;
332 } else {
333 #endif
334 for (i = 0; i < ics->num_swb; i++)
335 {
336 ics->sect_sfb_offset[0][i] = swb_offset_1024_window[sf_index][i];
337 ics->swb_offset[i] = swb_offset_1024_window[sf_index][i];
338 }
339 ics->sect_sfb_offset[0][ics->num_swb] = hDecoder->frameLength;
340 ics->swb_offset[ics->num_swb] = hDecoder->frameLength;
341 #ifdef LD_DEC
342 }
343 #endif
344 return 0;
345 case EIGHT_SHORT_SEQUENCE:
346 ics->num_windows = 8;
347 ics->num_window_groups = 1;
348 ics->window_group_length[ics->num_window_groups-1] = 1;
349 ics->num_swb = num_swb_128_window[sf_index];
350
351 for (i = 0; i < ics->num_swb; i++)
352 ics->swb_offset[i] = swb_offset_128_window[sf_index][i];
353 ics->swb_offset[ics->num_swb] = hDecoder->frameLength/8;
354
355 for (i = 0; i < ics->num_windows-1; i++) {
356 if (bit_set(ics->scale_factor_grouping, 6-i) == 0)
357 {
358 ics->num_window_groups += 1;
359 ics->window_group_length[ics->num_window_groups-1] = 1;
360 } else {
361 ics->window_group_length[ics->num_window_groups-1] += 1;
362 }
363 }
364
365 /* preparation of sect_sfb_offset for short blocks */
366 for (g = 0; g < ics->num_window_groups; g++)
367 {
368 uint16_t width;
369 uint8_t sect_sfb = 0;
370 uint16_t offset = 0;
371
372 for (i = 0; i < ics->num_swb; i++)
373 {
374 if (i+1 == ics->num_swb)
375 {
376 width = (hDecoder->frameLength/8) - swb_offset_128_window[sf_index][i];
377 } else {
378 width = swb_offset_128_window[sf_index][i+1] -
379 swb_offset_128_window[sf_index][i];
380 }
381 width *= ics->window_group_length[g];
382 ics->sect_sfb_offset[g][sect_sfb++] = offset;
383 offset += width;
384 }
385 ics->sect_sfb_offset[g][sect_sfb] = offset;
386 }
387 return 0;
388 default:
389 return 1;
390 }
391 }
392
393 /*
394 For ONLY_LONG_SEQUENCE windows (num_window_groups = 1,
395 window_group_length[0] = 1) the spectral data is in ascending spectral
396 order.
397 For the EIGHT_SHORT_SEQUENCE window, the spectral order depends on the
398 grouping in the following manner:
399 - Groups are ordered sequentially
400 - Within a group, a scalefactor band consists of the spectral data of all
401 grouped SHORT_WINDOWs for the associated scalefactor window band. To
402 clarify via example, the length of a group is in the range of one to eight
403 SHORT_WINDOWs.
404 - If there are eight groups each with length one (num_window_groups = 8,
405 window_group_length[0..7] = 1), the result is a sequence of eight spectra,
406 each in ascending spectral order.
407 - If there is only one group with length eight (num_window_groups = 1,
408 window_group_length[0] = 8), the result is that spectral data of all eight
409 SHORT_WINDOWs is interleaved by scalefactor window bands.
410 - Within a scalefactor window band, the coefficients are in ascending
411 spectral order.
412 */
413 void quant_to_spec(ic_stream *ics, real_t *spec_data, uint16_t frame_len)
414 {
415 uint8_t g, sfb, win;
416 uint16_t width, bin;
417 real_t *start_inptr, *start_win_ptr, *win_ptr;
418
419 real_t tmp_spec[1024];
420 real_t *tmp_spec_ptr, *spec_ptr;
421
422 tmp_spec_ptr = tmp_spec;
423 memset(tmp_spec_ptr, 0, frame_len*sizeof(real_t));
424
425 spec_ptr = spec_data;
426 tmp_spec_ptr = tmp_spec;
427 start_win_ptr = tmp_spec_ptr;
428
429 for (g = 0; g < ics->num_window_groups; g++)
430 {
431 uint16_t j = 0;
432 uint16_t win_inc = 0;
433
434 start_inptr = spec_ptr;
435
436 win_inc = ics->swb_offset[ics->num_swb];
437
438 for (sfb = 0; sfb < ics->num_swb; sfb++)
439 {
440 width = ics->swb_offset[sfb+1] - ics->swb_offset[sfb];
441
442 win_ptr = start_win_ptr;
443
444 for (win = 0; win < ics->window_group_length[g]; win++)
445 {
446 tmp_spec_ptr = win_ptr + j;
447
448 for (bin = 0; bin < width; bin += 4)
449 {
450 tmp_spec_ptr[0] = spec_ptr[0];
451 tmp_spec_ptr[1] = spec_ptr[1];
452 tmp_spec_ptr[2] = spec_ptr[2];
453 tmp_spec_ptr[3] = spec_ptr[3];
454 tmp_spec_ptr += 4;
455 spec_ptr += 4;
456 }
457
458 win_ptr += win_inc;
459 }
460 j += width;
461 }
462 start_win_ptr += (spec_ptr - start_inptr);
463 }
464
465 spec_ptr = spec_data;
466 tmp_spec_ptr = tmp_spec;
467
468 memcpy(spec_ptr, tmp_spec_ptr, frame_len*sizeof(real_t));
469 }
470
471 #ifndef FIXED_POINT
472 void build_tables(real_t *pow2_table)
473 {
474 uint16_t i;
475
476 /* build pow(2, 0.25*x) table for scalefactors */
477 for(i = 0; i < POW_TABLE_SIZE; i++)
478 {
479 pow2_table[i] = REAL_CONST(pow(2.0, 0.25 * (i-100)));
480 }
481 }
482 #endif
483
484 static INLINE real_t iquant(int16_t q, real_t *tab)
485 {
486 int16_t sgn = 1;
487
488 if (q == 0) return 0;
489
490 if (q < 0)
491 {
492 q = -q;
493 sgn = -1;
494 }
495
496 if (q >= IQ_TABLE_SIZE)
497 return sgn * tab[q>>3] * 16;
498
499 return sgn * tab[q];
500 }
501
502 void inverse_quantization(real_t *x_invquant, int16_t *x_quant, uint16_t frame_len)
503 {
504 int16_t i;
505 int16_t *in_ptr = x_quant;
506 real_t *out_ptr = x_invquant;
507 real_t *tab = iq_table;
508
509 for(i = frame_len/4-1; i >= 0; --i)
510 {
511 out_ptr[0] = iquant(in_ptr[0], tab);
512 out_ptr[1] = iquant(in_ptr[1], tab);
513 out_ptr[2] = iquant(in_ptr[2], tab);
514 out_ptr[3] = iquant(in_ptr[3], tab);
515 out_ptr += 4;
516 in_ptr += 4;
517 }
518 }
519
520 #ifndef FIXED_POINT
521 static INLINE real_t get_scale_factor_gain(uint16_t scale_factor, real_t *pow2_table)
522 {
523 if (scale_factor < POW_TABLE_SIZE)
524 return pow2_table[scale_factor];
525 else
526 return REAL_CONST(pow(2.0, 0.25 * (scale_factor - 100)));
527 }
528 #else
529 static real_t pow2_table[] =
530 {
531 COEF_CONST(0.59460355750136),
532 COEF_CONST(0.70710678118655),
533 COEF_CONST(0.84089641525371),
534 COEF_CONST(1.0),
535 COEF_CONST(1.18920711500272),
536 COEF_CONST(1.41421356237310),
537 COEF_CONST(1.68179283050743)
538 };
539 #endif
540
541 #ifdef FIXED_POINT
542 void apply_scalefactors(faacDecHandle hDecoder, ic_stream *ics, real_t *x_invquant,
543 uint16_t frame_len)
544 #else
545 void apply_scalefactors(ic_stream *ics, real_t *x_invquant, real_t *pow2_table,
546 uint16_t frame_len)
547 #endif
548 {
549 uint8_t g, sfb;
550 uint16_t top;
551 real_t *fp;
552 #ifndef FIXED_POINT
553 real_t scale;
554 #else
555 int32_t exp, frac;
556 #endif
557 uint8_t groups = 0;
558 uint16_t nshort = frame_len/8;
559
560 for (g = 0; g < ics->num_window_groups; g++)
561 {
562 uint16_t k = 0;
563
564 /* using this 128*groups doesn't hurt long blocks, because
565 long blocks only have 1 group, so that means 'groups' is
566 always 0 for long blocks
567 */
568 fp = x_invquant + (groups*nshort);
569
570 for (sfb = 0; sfb < ics->max_sfb; sfb++)
571 {
572 top = ics->sect_sfb_offset[g][sfb+1];
573
574 #ifndef FIXED_POINT
575 scale = get_scale_factor_gain(ics->scale_factors[g][sfb], pow2_table);
576 #else
577 exp = (ics->scale_factors[g][sfb] - 100) / 4;
578 frac = (ics->scale_factors[g][sfb] - 100) % 4;
579
580 if (hDecoder->object_type == LD)
581 {
582 exp -= 6 /*9*/;
583 } else {
584 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
585 exp -= 4 /*7*/;
586 else
587 exp -= 7 /*10*/;
588 }
589 #endif
590
591 /* minimum size of a sf band is 4 and always a multiple of 4 */
592 for ( ; k < top; k += 4)
593 {
594 #ifndef FIXED_POINT
595 fp[0] = fp[0] * scale;
596 fp[1] = fp[1] * scale;
597 fp[2] = fp[2] * scale;
598 fp[3] = fp[3] * scale;
599 #else
600 if (exp < 0)
601 {
602 fp[0] >>= -exp;
603 fp[1] >>= -exp;
604 fp[2] >>= -exp;
605 fp[3] >>= -exp;
606 } else {
607 fp[0] <<= exp;
608 fp[1] <<= exp;
609 fp[2] <<= exp;
610 fp[3] <<= exp;
611 }
612
613 if (frac)
614 {
615 fp[0] = MUL_R_C(fp[0],pow2_table[frac + 3]);
616 fp[1] = MUL_R_C(fp[1],pow2_table[frac + 3]);
617 fp[2] = MUL_R_C(fp[2],pow2_table[frac + 3]);
618 fp[3] = MUL_R_C(fp[3],pow2_table[frac + 3]);
619 }
620 #endif
621 fp += 4;
622 }
623 }
624 groups += ics->window_group_length[g];
625 }
626 }