comparison Plugins/Input/aac/libfaad2/specrec.c @ 1021:1e6c0a3f2d15 trunk

[svn] - 2.1 beta
author nenolod
date Wed, 10 May 2006 14:44:20 -0700
parents 29feaace84d0
children f12d7e208b43
comparison
equal deleted inserted replaced
1020:d70514b3b436 1021:1e6c0a3f2d15
1 /* 1 /*
2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding 2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding
3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com 3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com
4 ** 4 **
5 ** This program is free software; you can redistribute it and/or modify 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 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 7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version. 8 ** (at your option) any later version.
9 ** 9 **
10 ** This program is distributed in the hope that it will be useful, 10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of 11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details. 13 ** GNU General Public License for more details.
14 ** 14 **
15 ** You should have received a copy of the GNU General Public License 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 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. 17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 ** 18 **
19 ** Any non-GPL usage of this software or parts of this software is strictly 19 ** Any non-GPL usage of this software or parts of this software is strictly
20 ** forbidden. 20 ** forbidden.
21 ** 21 **
22 ** Commercial non-GPL licensing of this software is possible. 22 ** Commercial non-GPL licensing of this software is possible.
23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com. 23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com.
24 ** 24 **
25 ** $Id: specrec.c,v 1.43 2004/01/29 11:31:11 menno Exp $ 25 ** $Id: specrec.c,v 1.56 2004/09/08 09:43:11 gcp Exp $
26 **/ 26 **/
27 27
28 /* 28 /*
29 Spectral reconstruction: 29 Spectral reconstruction:
30 - grouping/sectioning 30 - grouping/sectioning
36 #include "structs.h" 36 #include "structs.h"
37 37
38 #include <string.h> 38 #include <string.h>
39 #include <stdlib.h> 39 #include <stdlib.h>
40 #include "specrec.h" 40 #include "specrec.h"
41 #include "filtbank.h"
41 #include "syntax.h" 42 #include "syntax.h"
42 #include "iq_table.h" 43 #include "iq_table.h"
43 #include "ms.h" 44 #include "ms.h"
44 #include "is.h" 45 #include "is.h"
45 #include "pns.h" 46 #include "pns.h"
52 #include "ssr_fb.h" 53 #include "ssr_fb.h"
53 #endif 54 #endif
54 55
55 56
56 /* static function declarations */ 57 /* static function declarations */
57 static void quant_to_spec(ic_stream *ics, real_t *spec_data, uint16_t frame_len); 58 static uint8_t quant_to_spec(NeAACDecHandle hDecoder,
58 static uint8_t inverse_quantization(real_t *x_invquant, const int16_t *x_quant, const uint16_t frame_len); 59 ic_stream *ics, int16_t *quant_data,
60 real_t *spec_data, uint16_t frame_len);
59 61
60 62
61 #ifdef LD_DEC 63 #ifdef LD_DEC
62 ALIGN static const uint8_t num_swb_512_window[] = 64 ALIGN static const uint8_t num_swb_512_window[] =
63 { 65 {
293 scalefactor window band named swb of the window actually used 295 scalefactor window band named swb of the window actually used
294 - determine sect_sfb_offset[g][section],the offset of the first coefficient 296 - determine sect_sfb_offset[g][section],the offset of the first coefficient
295 in section named section. This offset depends on window_sequence and 297 in section named section. This offset depends on window_sequence and
296 scale_factor_grouping and is needed to decode the spectral_data(). 298 scale_factor_grouping and is needed to decode the spectral_data().
297 */ 299 */
298 uint8_t window_grouping_info(faacDecHandle hDecoder, ic_stream *ics) 300 uint8_t window_grouping_info(NeAACDecHandle hDecoder, ic_stream *ics)
299 { 301 {
300 uint8_t i, g; 302 uint8_t i, g;
301 303
302 uint8_t sf_index = hDecoder->sf_index; 304 uint8_t sf_index = hDecoder->sf_index;
303 305
405 default: 407 default:
406 return 1; 408 return 1;
407 } 409 }
408 } 410 }
409 411
410 /* 412 /* iquant() *
411 For ONLY_LONG_SEQUENCE windows (num_window_groups = 1, 413 * output = sign(input)*abs(input)^(4/3)
412 window_group_length[0] = 1) the spectral data is in ascending spectral 414 */
413 order.
414 For the EIGHT_SHORT_SEQUENCE window, the spectral order depends on the
415 grouping in the following manner:
416 - Groups are ordered sequentially
417 - Within a group, a scalefactor band consists of the spectral data of all
418 grouped SHORT_WINDOWs for the associated scalefactor window band. To
419 clarify via example, the length of a group is in the range of one to eight
420 SHORT_WINDOWs.
421 - If there are eight groups each with length one (num_window_groups = 8,
422 window_group_length[0..7] = 1), the result is a sequence of eight spectra,
423 each in ascending spectral order.
424 - If there is only one group with length eight (num_window_groups = 1,
425 window_group_length[0] = 8), the result is that spectral data of all eight
426 SHORT_WINDOWs is interleaved by scalefactor window bands.
427 - Within a scalefactor window band, the coefficients are in ascending
428 spectral order.
429 */
430 static void quant_to_spec(ic_stream *ics, real_t *spec_data, uint16_t frame_len)
431 {
432 uint8_t g, sfb, win;
433 uint16_t width, bin, k, gindex;
434
435 ALIGN real_t tmp_spec[1024] = {0};
436
437 k = 0;
438 gindex = 0;
439
440 for (g = 0; g < ics->num_window_groups; g++)
441 {
442 uint16_t j = 0;
443 uint16_t gincrease = 0;
444 uint16_t win_inc = ics->swb_offset[ics->num_swb];
445
446 for (sfb = 0; sfb < ics->num_swb; sfb++)
447 {
448 width = ics->swb_offset[sfb+1] - ics->swb_offset[sfb];
449
450 for (win = 0; win < ics->window_group_length[g]; win++)
451 {
452 for (bin = 0; bin < width; bin += 4)
453 {
454 tmp_spec[gindex+(win*win_inc)+j+bin+0] = spec_data[k+0];
455 tmp_spec[gindex+(win*win_inc)+j+bin+1] = spec_data[k+1];
456 tmp_spec[gindex+(win*win_inc)+j+bin+2] = spec_data[k+2];
457 tmp_spec[gindex+(win*win_inc)+j+bin+3] = spec_data[k+3];
458 gincrease += 4;
459 k += 4;
460 }
461 }
462 j += width;
463 }
464 gindex += gincrease;
465 }
466
467 memcpy(spec_data, tmp_spec, frame_len*sizeof(real_t));
468 }
469
470 static INLINE real_t iquant(int16_t q, const real_t *tab, uint8_t *error) 415 static INLINE real_t iquant(int16_t q, const real_t *tab, uint8_t *error)
471 { 416 {
472 #ifdef FIXED_POINT 417 #ifdef FIXED_POINT
418 /* For FIXED_POINT the iq_table is prescaled by 3 bits (iq_table[]/8) */
419 /* BIG_IQ_TABLE allows you to use the full 8192 value table, if this is not
420 * defined a 1026 value table and interpolation will be used
421 */
422 #ifndef BIG_IQ_TABLE
473 static const real_t errcorr[] = { 423 static const real_t errcorr[] = {
474 REAL_CONST(0), REAL_CONST(1.0/8.0), REAL_CONST(2.0/8.0), REAL_CONST(3.0/8.0), 424 REAL_CONST(0), REAL_CONST(1.0/8.0), REAL_CONST(2.0/8.0), REAL_CONST(3.0/8.0),
475 REAL_CONST(4.0/8.0), REAL_CONST(5.0/8.0), REAL_CONST(6.0/8.0), REAL_CONST(7.0/8.0), 425 REAL_CONST(4.0/8.0), REAL_CONST(5.0/8.0), REAL_CONST(6.0/8.0), REAL_CONST(7.0/8.0),
476 REAL_CONST(0) 426 REAL_CONST(0)
477 }; 427 };
478 real_t x1, x2; 428 real_t x1, x2;
429 #endif
479 int16_t sgn = 1; 430 int16_t sgn = 1;
480 431
481 if (q < 0) 432 if (q < 0)
482 { 433 {
483 q = -q; 434 q = -q;
484 sgn = -1; 435 sgn = -1;
485 } 436 }
486 437
487 if (q < IQ_TABLE_SIZE) 438 if (q < IQ_TABLE_SIZE)
439 {
440 //#define IQUANT_PRINT
441 #ifdef IQUANT_PRINT
442 //printf("0x%.8X\n", sgn * tab[q]);
443 printf("%d\n", sgn * tab[q]);
444 #endif
488 return sgn * tab[q]; 445 return sgn * tab[q];
446 }
447
448 #ifndef BIG_IQ_TABLE
449 if (q >= 8192)
450 {
451 *error = 17;
452 return 0;
453 }
489 454
490 /* linear interpolation */ 455 /* linear interpolation */
491 x1 = tab[q>>3]; 456 x1 = tab[q>>3];
492 x2 = tab[(q>>3) + 1]; 457 x2 = tab[(q>>3) + 1];
493 return sgn * 16 * (MUL_R(errcorr[q&7],(x2-x1)) + x1); 458 return sgn * 16 * (MUL_R(errcorr[q&7],(x2-x1)) + x1);
459 #else
460 *error = 17;
461 return 0;
462 #endif
463
494 #else 464 #else
495 if (q < 0) 465 if (q < 0)
496 { 466 {
497 /* tab contains a value for all possible q [0,8192] */ 467 /* tab contains a value for all possible q [0,8192] */
498 if (-q < IQ_TABLE_SIZE) 468 if (-q < IQ_TABLE_SIZE)
507 477
508 *error = 17; 478 *error = 17;
509 return 0; 479 return 0;
510 } 480 }
511 #endif 481 #endif
512 }
513
514 static uint8_t inverse_quantization(real_t *x_invquant, const int16_t *x_quant, const uint16_t frame_len)
515 {
516 int16_t i;
517 uint8_t error = 0; /* Init error flag */
518 const real_t *tab = iq_table;
519
520 for (i = 0; i < frame_len; i+=4)
521 {
522 x_invquant[i] = iquant(x_quant[i], tab, &error);
523 x_invquant[i+1] = iquant(x_quant[i+1], tab, &error);
524 x_invquant[i+2] = iquant(x_quant[i+2], tab, &error);
525 x_invquant[i+3] = iquant(x_quant[i+3], tab, &error);
526 }
527
528 return error;
529 } 482 }
530 483
531 #ifndef FIXED_POINT 484 #ifndef FIXED_POINT
532 ALIGN static const real_t pow2sf_tab[] = { 485 ALIGN static const real_t pow2sf_tab[] = {
533 2.9802322387695313E-008, 5.9604644775390625E-008, 1.1920928955078125E-007, 486 2.9802322387695313E-008, 5.9604644775390625E-008, 1.1920928955078125E-007,
552 8589934592.0, 17179869184.0, 34359738368.0, 505 8589934592.0, 17179869184.0, 34359738368.0,
553 68719476736.0, 137438953472.0, 274877906944.0 506 68719476736.0, 137438953472.0, 274877906944.0
554 }; 507 };
555 #endif 508 #endif
556 509
557 ALIGN static real_t pow2_table[] = 510 /* quant_to_spec: perform dequantisation and scaling
558 { 511 * and in case of short block it also does the deinterleaving
559 #if 0 512 */
560 COEF_CONST(0.59460355750136053335874998528024), /* 2^-0.75 */ 513 /*
561 COEF_CONST(0.70710678118654752440084436210485), /* 2^-0.5 */ 514 For ONLY_LONG_SEQUENCE windows (num_window_groups = 1,
562 COEF_CONST(0.84089641525371454303112547623321), /* 2^-0.25 */ 515 window_group_length[0] = 1) the spectral data is in ascending spectral
563 #endif 516 order.
564 COEF_CONST(1.0), 517 For the EIGHT_SHORT_SEQUENCE window, the spectral order depends on the
565 COEF_CONST(1.1892071150027210667174999705605), /* 2^0.25 */ 518 grouping in the following manner:
566 COEF_CONST(1.4142135623730950488016887242097), /* 2^0.5 */ 519 - Groups are ordered sequentially
567 COEF_CONST(1.6817928305074290860622509524664) /* 2^0.75 */ 520 - Within a group, a scalefactor band consists of the spectral data of all
568 }; 521 grouped SHORT_WINDOWs for the associated scalefactor window band. To
569 522 clarify via example, the length of a group is in the range of one to eight
570 void apply_scalefactors(faacDecHandle hDecoder, ic_stream *ics, 523 SHORT_WINDOWs.
571 real_t *x_invquant, uint16_t frame_len) 524 - If there are eight groups each with length one (num_window_groups = 8,
572 { 525 window_group_length[0..7] = 1), the result is a sequence of eight spectra,
573 uint8_t g, sfb; 526 each in ascending spectral order.
574 uint16_t top; 527 - If there is only one group with length eight (num_window_groups = 1,
575 int32_t exp, frac; 528 window_group_length[0] = 8), the result is that spectral data of all eight
576 uint8_t groups = 0; 529 SHORT_WINDOWs is interleaved by scalefactor window bands.
577 uint16_t nshort = frame_len/8; 530 - Within a scalefactor window band, the coefficients are in ascending
531 spectral order.
532 */
533 static uint8_t quant_to_spec(NeAACDecHandle hDecoder,
534 ic_stream *ics, int16_t *quant_data,
535 real_t *spec_data, uint16_t frame_len)
536 {
537 ALIGN static const real_t pow2_table[] =
538 {
539 COEF_CONST(1.0),
540 COEF_CONST(1.1892071150027210667174999705605), /* 2^0.25 */
541 COEF_CONST(1.4142135623730950488016887242097), /* 2^0.5 */
542 COEF_CONST(1.6817928305074290860622509524664) /* 2^0.75 */
543 };
544 const real_t *tab = iq_table;
545
546 uint8_t g, sfb, win;
547 uint16_t width, bin, k, gindex, wa, wb;
548 uint8_t error = 0; /* Init error flag */
549 #ifndef FIXED_POINT
550 real_t scf;
551 #endif
552
553 k = 0;
554 gindex = 0;
578 555
579 for (g = 0; g < ics->num_window_groups; g++) 556 for (g = 0; g < ics->num_window_groups; g++)
580 { 557 {
581 uint16_t k = 0; 558 uint16_t j = 0;
582 559 uint16_t gincrease = 0;
583 /* using this nshort*groups doesn't hurt long blocks, because 560 uint16_t win_inc = ics->swb_offset[ics->num_swb];
584 long blocks only have 1 group, so that means 'groups' is 561
585 always 0 for long blocks 562 for (sfb = 0; sfb < ics->num_swb; sfb++)
586 */ 563 {
587 for (sfb = 0; sfb < ics->max_sfb; sfb++) 564 int32_t exp, frac;
588 { 565
589 top = ics->sect_sfb_offset[g][sfb+1]; 566 width = ics->swb_offset[sfb+1] - ics->swb_offset[sfb];
590 567
591 /* this could be scalefactor for IS or PNS, those can be negative or bigger then 255 */ 568 /* this could be scalefactor for IS or PNS, those can be negative or bigger then 255 */
592 /* just ignore them */ 569 /* just ignore them */
593 if (ics->scale_factors[g][sfb] < 0 || ics->scale_factors[g][sfb] > 255) 570 if (ics->scale_factors[g][sfb] < 0 || ics->scale_factors[g][sfb] > 255)
594 { 571 {
595 exp = 0; 572 exp = 0;
596 frac = 0; 573 frac = 0;
597 } else { 574 } else {
598 /* ics->scale_factors[g][sfb] must be between 0 and 255 */ 575 /* ics->scale_factors[g][sfb] must be between 0 and 255 */
599 exp = (ics->scale_factors[g][sfb] /* - 100 */) >> 2; 576 exp = (ics->scale_factors[g][sfb] /* - 100 */) >> 2;
577 /* frac must always be > 0 */
600 frac = (ics->scale_factors[g][sfb] /* - 100 */) & 3; 578 frac = (ics->scale_factors[g][sfb] /* - 100 */) & 3;
601 } 579 }
602 580
603 #ifdef FIXED_POINT 581 #ifdef FIXED_POINT
604 exp -= 25; 582 exp -= 25;
612 else 590 else
613 exp -= 7 /*10*/; 591 exp -= 7 /*10*/;
614 } 592 }
615 #endif 593 #endif
616 594
617 /* minimum size of a sf band is 4 and always a multiple of 4 */ 595 wa = gindex + j;
618 for ( ; k < top; k += 4) 596
597 #ifndef FIXED_POINT
598 scf = pow2sf_tab[exp/*+25*/] * pow2_table[frac];
599 #endif
600
601 for (win = 0; win < ics->window_group_length[g]; win++)
619 { 602 {
620 #ifdef FIXED_POINT 603 for (bin = 0; bin < width; bin += 4)
621 if (exp < 0)
622 { 604 {
623 x_invquant[k+(groups*nshort)] >>= -exp; 605 #ifndef FIXED_POINT
624 x_invquant[k+(groups*nshort)+1] >>= -exp; 606 wb = wa + bin;
625 x_invquant[k+(groups*nshort)+2] >>= -exp; 607
626 x_invquant[k+(groups*nshort)+3] >>= -exp; 608 spec_data[wb+0] = iquant(quant_data[k+0], tab, &error) * scf;
627 } else { 609 spec_data[wb+1] = iquant(quant_data[k+1], tab, &error) * scf;
628 x_invquant[k+(groups*nshort)] <<= exp; 610 spec_data[wb+2] = iquant(quant_data[k+2], tab, &error) * scf;
629 x_invquant[k+(groups*nshort)+1] <<= exp; 611 spec_data[wb+3] = iquant(quant_data[k+3], tab, &error) * scf;
630 x_invquant[k+(groups*nshort)+2] <<= exp; 612
631 x_invquant[k+(groups*nshort)+3] <<= exp; 613 #else
614 real_t iq0 = iquant(quant_data[k+0], tab, &error);
615 real_t iq1 = iquant(quant_data[k+1], tab, &error);
616 real_t iq2 = iquant(quant_data[k+2], tab, &error);
617 real_t iq3 = iquant(quant_data[k+3], tab, &error);
618
619 wb = wa + bin;
620
621 if (exp < 0)
622 {
623 spec_data[wb+0] = iq0 >>= -exp;
624 spec_data[wb+1] = iq1 >>= -exp;
625 spec_data[wb+2] = iq2 >>= -exp;
626 spec_data[wb+3] = iq3 >>= -exp;
627 } else {
628 spec_data[wb+0] = iq0 <<= exp;
629 spec_data[wb+1] = iq1 <<= exp;
630 spec_data[wb+2] = iq2 <<= exp;
631 spec_data[wb+3] = iq3 <<= exp;
632 }
633 if (frac != 0)
634 {
635 spec_data[wb+0] = MUL_C(spec_data[wb+0],pow2_table[frac]);
636 spec_data[wb+1] = MUL_C(spec_data[wb+1],pow2_table[frac]);
637 spec_data[wb+2] = MUL_C(spec_data[wb+2],pow2_table[frac]);
638 spec_data[wb+3] = MUL_C(spec_data[wb+3],pow2_table[frac]);
639 }
640
641 //#define SCFS_PRINT
642 #ifdef SCFS_PRINT
643 printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
644 printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
645 printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
646 printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
647 //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
648 //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
649 //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
650 //printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
651 #endif
652 #endif
653
654 gincrease += 4;
655 k += 4;
632 } 656 }
633 #else 657 wa += win_inc;
634 x_invquant[k+(groups*nshort)] = x_invquant[k+(groups*nshort)] * pow2sf_tab[exp/*+25*/];
635 x_invquant[k+(groups*nshort)+1] = x_invquant[k+(groups*nshort)+1] * pow2sf_tab[exp/*+25*/];
636 x_invquant[k+(groups*nshort)+2] = x_invquant[k+(groups*nshort)+2] * pow2sf_tab[exp/*+25*/];
637 x_invquant[k+(groups*nshort)+3] = x_invquant[k+(groups*nshort)+3] * pow2sf_tab[exp/*+25*/];
638 #endif
639
640 x_invquant[k+(groups*nshort)] = MUL_C(x_invquant[k+(groups*nshort)],pow2_table[frac /* + 3*/]);
641 x_invquant[k+(groups*nshort)+1] = MUL_C(x_invquant[k+(groups*nshort)+1],pow2_table[frac /* + 3*/]);
642 x_invquant[k+(groups*nshort)+2] = MUL_C(x_invquant[k+(groups*nshort)+2],pow2_table[frac /* + 3*/]);
643 x_invquant[k+(groups*nshort)+3] = MUL_C(x_invquant[k+(groups*nshort)+3],pow2_table[frac /* + 3*/]);
644 } 658 }
645 } 659 j += width;
646 groups += ics->window_group_length[g]; 660 }
647 } 661 gindex += gincrease;
662 }
663
664 return error;
648 } 665 }
649 666
650 #ifdef USE_SSE 667 static uint8_t allocate_single_channel(NeAACDecHandle hDecoder, uint8_t channel,
651 void apply_scalefactors_sse(faacDecHandle hDecoder, ic_stream *ics,
652 real_t *x_invquant, uint16_t frame_len)
653 {
654 uint8_t g, sfb;
655 uint16_t top;
656 int32_t exp, frac;
657 uint8_t groups = 0;
658 uint16_t nshort = frame_len/8;
659
660 for (g = 0; g < ics->num_window_groups; g++)
661 {
662 uint16_t k = 0;
663
664 /* using this nshort*groups doesn't hurt long blocks, because
665 long blocks only have 1 group, so that means 'groups' is
666 always 0 for long blocks
667 */
668 for (sfb = 0; sfb < ics->max_sfb; sfb++)
669 {
670 top = ics->sect_sfb_offset[g][sfb+1];
671
672 exp = (ics->scale_factors[g][sfb] /* - 100 */) >> 2;
673 frac = (ics->scale_factors[g][sfb] /* - 100 */) & 3;
674
675 /* minimum size of a sf band is 4 and always a multiple of 4 */
676 for ( ; k < top; k += 4)
677 {
678 __m128 m1 = _mm_load_ps(&x_invquant[k+(groups*nshort)]);
679 __m128 m2 = _mm_load_ps1(&pow2sf_tab[exp /*+25*/]);
680 __m128 m3 = _mm_load_ps1(&pow2_table[frac /* + 3*/]);
681 __m128 m4 = _mm_mul_ps(m1, m2);
682 __m128 m5 = _mm_mul_ps(m3, m4);
683 _mm_store_ps(&x_invquant[k+(groups*nshort)], m5);
684 }
685 }
686 groups += ics->window_group_length[g];
687 }
688 }
689 #endif
690
691 static uint8_t allocate_single_channel(faacDecHandle hDecoder, uint8_t channel,
692 uint8_t output_channels) 668 uint8_t output_channels)
693 { 669 {
694 uint8_t mul = 1; 670 uint8_t mul = 1;
695 671
696 #ifdef MAIN_DEC 672 #ifdef MAIN_DEC
769 #endif 745 #endif
770 746
771 return 0; 747 return 0;
772 } 748 }
773 749
774 static uint8_t allocate_channel_pair(faacDecHandle hDecoder, 750 static uint8_t allocate_channel_pair(NeAACDecHandle hDecoder,
775 uint8_t channel, uint8_t paired_channel) 751 uint8_t channel, uint8_t paired_channel)
776 { 752 {
777 uint8_t mul = 1; 753 uint8_t mul = 1;
778 754
779 #ifdef MAIN_DEC 755 #ifdef MAIN_DEC
874 #endif 850 #endif
875 851
876 return 0; 852 return 0;
877 } 853 }
878 854
879 uint8_t reconstruct_single_channel(faacDecHandle hDecoder, ic_stream *ics, 855 uint8_t reconstruct_single_channel(NeAACDecHandle hDecoder, ic_stream *ics,
880 element *sce, int16_t *spec_data) 856 element *sce, int16_t *spec_data)
881 { 857 {
882 uint8_t retval, output_channels; 858 uint8_t retval, output_channels;
883 ALIGN real_t spec_coef[1024]; 859 ALIGN real_t spec_coef[1024];
884 860
885 #ifdef PROFILE 861 #ifdef PROFILE
886 int64_t count = faad_get_ts(); 862 int64_t count = faad_get_ts();
887 #endif 863 #endif
888 864
889 865
890 /* determine whether some mono->stereo tool is used */ 866 /* always allocate 2 channels, PS can always "suddenly" turn up */
891 #if (defined(PS_DEC) || defined(DRM_PS)) 867 #if (defined(PS_DEC) || defined(DRM_PS))
892 output_channels = hDecoder->ps_used[hDecoder->fr_ch_ele] ? 2 : 1; 868 output_channels = 2;
893 #else 869 #else
894 output_channels = 1; 870 output_channels = 1;
895 #endif 871 #endif
872
896 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0) 873 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
897 { 874 {
898 /* element_output_channels not set yet */ 875 /* element_output_channels not set yet */
899 hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels; 876 hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels;
900 } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != output_channels) { 877 } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != output_channels) {
901 /* element inconsistency */ 878 /* element inconsistency */
902 return 21; 879 return 21;
903 } 880 }
904 881
905
906 if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0) 882 if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
907 { 883 {
908 retval = allocate_single_channel(hDecoder, sce->channel, output_channels); 884 retval = allocate_single_channel(hDecoder, sce->channel, output_channels);
909 if (retval > 0) 885 if (retval > 0)
910 return retval; 886 return retval;
911 887
912 hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1; 888 hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
913 } 889 }
914 890
915 891
916 /* inverse quantization */ 892 /* dequantisation and scaling */
917 retval = inverse_quantization(spec_coef, spec_data, hDecoder->frameLength); 893 retval = quant_to_spec(hDecoder, ics, spec_data, spec_coef, hDecoder->frameLength);
918 if (retval > 0) 894 if (retval > 0)
919 return retval; 895 return retval;
920
921 /* apply scalefactors */
922 #ifndef USE_SSE
923 apply_scalefactors(hDecoder, ics, spec_coef, hDecoder->frameLength);
924 #else
925 hDecoder->apply_sf_func(hDecoder, ics, spec_coef, hDecoder->frameLength);
926 #endif
927
928 /* deinterleave short block grouping */
929 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
930 quant_to_spec(ics, spec_coef, hDecoder->frameLength);
931 896
932 #ifdef PROFILE 897 #ifdef PROFILE
933 count = faad_get_ts() - count; 898 count = faad_get_ts() - count;
934 hDecoder->requant_cycles += count; 899 hDecoder->requant_cycles += count;
935 #endif 900 #endif
985 { 950 {
986 if (!hDecoder->drc->exclude_mask[sce->channel] || !hDecoder->drc->excluded_chns_present) 951 if (!hDecoder->drc->exclude_mask[sce->channel] || !hDecoder->drc->excluded_chns_present)
987 drc_decode(hDecoder->drc, spec_coef); 952 drc_decode(hDecoder->drc, spec_coef);
988 } 953 }
989 954
990
991 /* filter bank */ 955 /* filter bank */
992 #ifdef SSR_DEC 956 #ifdef SSR_DEC
993 if (hDecoder->object_type != SSR) 957 if (hDecoder->object_type != SSR)
994 { 958 {
995 #endif 959 #endif
996 #ifdef USE_SSE
997 hDecoder->fb->if_func(hDecoder->fb, ics->window_sequence, ics->window_shape,
998 hDecoder->window_shape_prev[sce->channel], spec_coef,
999 hDecoder->time_out[sce->channel], hDecoder->object_type, hDecoder->frameLength);
1000 #else
1001 ifilter_bank(hDecoder->fb, ics->window_sequence, ics->window_shape, 960 ifilter_bank(hDecoder->fb, ics->window_sequence, ics->window_shape,
1002 hDecoder->window_shape_prev[sce->channel], spec_coef, 961 hDecoder->window_shape_prev[sce->channel], spec_coef,
1003 hDecoder->time_out[sce->channel], hDecoder->fb_intermed[sce->channel], 962 hDecoder->time_out[sce->channel], hDecoder->fb_intermed[sce->channel],
1004 hDecoder->object_type, hDecoder->frameLength); 963 hDecoder->object_type, hDecoder->frameLength);
1005 #endif
1006 #ifdef SSR_DEC 964 #ifdef SSR_DEC
1007 } else { 965 } else {
1008 ssr_decode(&(ics->ssr), hDecoder->fb, ics->window_sequence, ics->window_shape, 966 ssr_decode(&(ics->ssr), hDecoder->fb, ics->window_sequence, ics->window_shape,
1009 hDecoder->window_shape_prev[sce->channel], spec_coef, hDecoder->time_out[sce->channel], 967 hDecoder->window_shape_prev[sce->channel], spec_coef, hDecoder->time_out[sce->channel],
1010 hDecoder->ssr_overlap[sce->channel], hDecoder->ipqf_buffer[sce->channel], hDecoder->prev_fmd[sce->channel], 968 hDecoder->ssr_overlap[sce->channel], hDecoder->ipqf_buffer[sce->channel], hDecoder->prev_fmd[sce->channel],
1032 990
1033 /* following case can happen when forceUpSampling == 1 */ 991 /* following case can happen when forceUpSampling == 1 */
1034 if (hDecoder->sbr[ele] == NULL) 992 if (hDecoder->sbr[ele] == NULL)
1035 { 993 {
1036 hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength, 994 hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
1037 sce->ele_id, 2*get_sample_rate(hDecoder->sf_index) 995 hDecoder->element_id[ele], 2*get_sample_rate(hDecoder->sf_index),
996 hDecoder->downSampledSBR
1038 #ifdef DRM 997 #ifdef DRM
1039 , 0 998 , 0
1040 #endif 999 #endif
1041 ); 1000 );
1042 } 1001 }
1043 1002
1003 if (sce->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
1004 hDecoder->sbr[ele]->maxAACLine = 8*sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)];
1005 else
1006 hDecoder->sbr[ele]->maxAACLine = sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)];
1007
1044 /* check if any of the PS tools is used */ 1008 /* check if any of the PS tools is used */
1045 #if (defined(PS_DEC) || defined(DRM_PS)) 1009 #if (defined(PS_DEC) || defined(DRM_PS))
1046 if (output_channels == 1) 1010 if (hDecoder->ps_used[ele] == 0)
1047 { 1011 {
1048 #endif 1012 #endif
1049 retval = sbrDecodeSingleFrame(hDecoder->sbr[ele], hDecoder->time_out[ch], 1013 retval = sbrDecodeSingleFrame(hDecoder->sbr[ele], hDecoder->time_out[ch],
1050 hDecoder->postSeekResetFlag, hDecoder->forceUpSampling); 1014 hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
1051 #if (defined(PS_DEC) || defined(DRM_PS)) 1015 #if (defined(PS_DEC) || defined(DRM_PS))
1052 } else { 1016 } else {
1053 retval = sbrDecodeSingleFramePS(hDecoder->sbr[ele], hDecoder->time_out[ch], 1017 retval = sbrDecodeSingleFramePS(hDecoder->sbr[ele], hDecoder->time_out[ch],
1054 hDecoder->time_out[ch+1], hDecoder->postSeekResetFlag, 1018 hDecoder->time_out[ch+1], hDecoder->postSeekResetFlag,
1055 hDecoder->forceUpSampling); 1019 hDecoder->downSampledSBR);
1056 } 1020 }
1057 #endif 1021 #endif
1058 if (retval > 0) 1022 if (retval > 0)
1059 return retval; 1023 return retval;
1060 } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) 1024 } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1062 { 1026 {
1063 return 23; 1027 return 23;
1064 } 1028 }
1065 #endif 1029 #endif
1066 1030
1031 /* copy L to R when no PS is used */
1032 #if (defined(PS_DEC) || defined(DRM_PS))
1033 if ((hDecoder->ps_used[hDecoder->fr_ch_ele] == 0))
1034 {
1035 uint8_t ele = hDecoder->fr_ch_ele;
1036 uint8_t ch = sce->channel;
1037 uint16_t frame_size = (hDecoder->sbr_alloced[ele]) ? 2 : 1;
1038 frame_size *= hDecoder->frameLength*sizeof(real_t);
1039
1040 memcpy(hDecoder->time_out[ch+1], hDecoder->time_out[ch], frame_size);
1041 }
1042 #endif
1043
1067 return 0; 1044 return 0;
1068 } 1045 }
1069 1046
1070 uint8_t reconstruct_channel_pair(faacDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2, 1047 uint8_t reconstruct_channel_pair(NeAACDecHandle hDecoder, ic_stream *ics1, ic_stream *ics2,
1071 element *cpe, int16_t *spec_data1, int16_t *spec_data2) 1048 element *cpe, int16_t *spec_data1, int16_t *spec_data2)
1072 { 1049 {
1073 uint8_t retval; 1050 uint8_t retval;
1074 ALIGN real_t spec_coef1[1024]; 1051 ALIGN real_t spec_coef1[1024];
1075 ALIGN real_t spec_coef2[1024]; 1052 ALIGN real_t spec_coef2[1024];
1077 #ifdef PROFILE 1054 #ifdef PROFILE
1078 int64_t count = faad_get_ts(); 1055 int64_t count = faad_get_ts();
1079 #endif 1056 #endif
1080 if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0) 1057 if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
1081 { 1058 {
1082 retval = allocate_channel_pair(hDecoder, cpe->channel, cpe->paired_channel); 1059 retval = allocate_channel_pair(hDecoder, cpe->channel, (uint8_t)cpe->paired_channel);
1083 if (retval > 0) 1060 if (retval > 0)
1084 return retval; 1061 return retval;
1085 1062
1086 hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1; 1063 hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
1087 } 1064 }
1088 1065
1089 /* inverse quantization */ 1066 /* dequantisation and scaling */
1090 retval = inverse_quantization(spec_coef1, spec_data1, hDecoder->frameLength); 1067 retval = quant_to_spec(hDecoder, ics1, spec_data1, spec_coef1, hDecoder->frameLength);
1091 if (retval > 0) 1068 if (retval > 0)
1092 return retval; 1069 return retval;
1093 1070 retval = quant_to_spec(hDecoder, ics2, spec_data2, spec_coef2, hDecoder->frameLength);
1094 retval = inverse_quantization(spec_coef2, spec_data2, hDecoder->frameLength);
1095 if (retval > 0) 1071 if (retval > 0)
1096 return retval; 1072 return retval;
1097
1098 /* apply scalefactors */
1099 #ifndef USE_SSE
1100 apply_scalefactors(hDecoder, ics1, spec_coef1, hDecoder->frameLength);
1101 apply_scalefactors(hDecoder, ics2, spec_coef2, hDecoder->frameLength);
1102 #else
1103 hDecoder->apply_sf_func(hDecoder, ics1, spec_coef1, hDecoder->frameLength);
1104 hDecoder->apply_sf_func(hDecoder, ics2, spec_coef2, hDecoder->frameLength);
1105 #endif
1106
1107 /* deinterleave short block grouping */
1108 if (ics1->window_sequence == EIGHT_SHORT_SEQUENCE)
1109 quant_to_spec(ics1, spec_coef1, hDecoder->frameLength);
1110 if (ics2->window_sequence == EIGHT_SHORT_SEQUENCE)
1111 quant_to_spec(ics2, spec_coef2, hDecoder->frameLength);
1112 1073
1113 #ifdef PROFILE 1074 #ifdef PROFILE
1114 count = faad_get_ts() - count; 1075 count = faad_get_ts() - count;
1115 hDecoder->requant_cycles += count; 1076 hDecoder->requant_cycles += count;
1116 #endif 1077 #endif
1126 } 1087 }
1127 1088
1128 /* mid/side decoding */ 1089 /* mid/side decoding */
1129 ms_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength); 1090 ms_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
1130 1091
1092 #if 0
1093 {
1094 int i;
1095 for (i = 0; i < 1024; i++)
1096 {
1097 //printf("%d\n", spec_coef1[i]);
1098 printf("0x%.8X\n", spec_coef1[i]);
1099 }
1100 for (i = 0; i < 1024; i++)
1101 {
1102 //printf("%d\n", spec_coef2[i]);
1103 printf("0x%.8X\n", spec_coef2[i]);
1104 }
1105 }
1106 #endif
1107
1131 /* intensity stereo decoding */ 1108 /* intensity stereo decoding */
1132 is_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength); 1109 is_decode(ics1, ics2, spec_coef1, spec_coef2, hDecoder->frameLength);
1110
1111 #if 0
1112 {
1113 int i;
1114 for (i = 0; i < 1024; i++)
1115 {
1116 printf("%d\n", spec_coef1[i]);
1117 //printf("0x%.8X\n", spec_coef1[i]);
1118 }
1119 for (i = 0; i < 1024; i++)
1120 {
1121 printf("%d\n", spec_coef2[i]);
1122 //printf("0x%.8X\n", spec_coef2[i]);
1123 }
1124 }
1125 #endif
1133 1126
1134 #ifdef MAIN_DEC 1127 #ifdef MAIN_DEC
1135 /* MAIN object type prediction */ 1128 /* MAIN object type prediction */
1136 if (hDecoder->object_type == MAIN) 1129 if (hDecoder->object_type == MAIN)
1137 { 1130 {
1201 /* filter bank */ 1194 /* filter bank */
1202 #ifdef SSR_DEC 1195 #ifdef SSR_DEC
1203 if (hDecoder->object_type != SSR) 1196 if (hDecoder->object_type != SSR)
1204 { 1197 {
1205 #endif 1198 #endif
1206 #ifdef USE_SSE
1207 hDecoder->fb->if_func(hDecoder->fb, ics1->window_sequence, ics1->window_shape,
1208 hDecoder->window_shape_prev[cpe->channel], spec_coef1,
1209 hDecoder->time_out[cpe->channel], hDecoder->object_type, hDecoder->frameLength);
1210 hDecoder->fb->if_func(hDecoder->fb, ics2->window_sequence, ics2->window_shape,
1211 hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2,
1212 hDecoder->time_out[cpe->paired_channel], hDecoder->object_type, hDecoder->frameLength);
1213 #else
1214 ifilter_bank(hDecoder->fb, ics1->window_sequence, ics1->window_shape, 1199 ifilter_bank(hDecoder->fb, ics1->window_sequence, ics1->window_shape,
1215 hDecoder->window_shape_prev[cpe->channel], spec_coef1, 1200 hDecoder->window_shape_prev[cpe->channel], spec_coef1,
1216 hDecoder->time_out[cpe->channel], hDecoder->fb_intermed[cpe->channel], 1201 hDecoder->time_out[cpe->channel], hDecoder->fb_intermed[cpe->channel],
1217 hDecoder->object_type, hDecoder->frameLength); 1202 hDecoder->object_type, hDecoder->frameLength);
1218 ifilter_bank(hDecoder->fb, ics2->window_sequence, ics2->window_shape, 1203 ifilter_bank(hDecoder->fb, ics2->window_sequence, ics2->window_shape,
1219 hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2, 1204 hDecoder->window_shape_prev[cpe->paired_channel], spec_coef2,
1220 hDecoder->time_out[cpe->paired_channel], hDecoder->fb_intermed[cpe->paired_channel], 1205 hDecoder->time_out[cpe->paired_channel], hDecoder->fb_intermed[cpe->paired_channel],
1221 hDecoder->object_type, hDecoder->frameLength); 1206 hDecoder->object_type, hDecoder->frameLength);
1222 #endif
1223 #ifdef SSR_DEC 1207 #ifdef SSR_DEC
1224 } else { 1208 } else {
1225 ssr_decode(&(ics1->ssr), hDecoder->fb, ics1->window_sequence, ics1->window_shape, 1209 ssr_decode(&(ics1->ssr), hDecoder->fb, ics1->window_sequence, ics1->window_shape,
1226 hDecoder->window_shape_prev[cpe->channel], spec_coef1, hDecoder->time_out[cpe->channel], 1210 hDecoder->window_shape_prev[cpe->channel], spec_coef1, hDecoder->time_out[cpe->channel],
1227 hDecoder->ssr_overlap[cpe->channel], hDecoder->ipqf_buffer[cpe->channel], 1211 hDecoder->ssr_overlap[cpe->channel], hDecoder->ipqf_buffer[cpe->channel],
1257 1241
1258 /* following case can happen when forceUpSampling == 1 */ 1242 /* following case can happen when forceUpSampling == 1 */
1259 if (hDecoder->sbr[ele] == NULL) 1243 if (hDecoder->sbr[ele] == NULL)
1260 { 1244 {
1261 hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength, 1245 hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
1262 cpe->ele_id, 2*get_sample_rate(hDecoder->sf_index) 1246 hDecoder->element_id[ele], 2*get_sample_rate(hDecoder->sf_index),
1247 hDecoder->downSampledSBR
1263 #ifdef DRM 1248 #ifdef DRM
1264 , 0 1249 , 0
1265 #endif 1250 #endif
1266 ); 1251 );
1267 } 1252 }
1268 1253
1254 if (cpe->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
1255 hDecoder->sbr[ele]->maxAACLine = 8*cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)];
1256 else
1257 hDecoder->sbr[ele]->maxAACLine = cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)];
1258
1269 retval = sbrDecodeCoupleFrame(hDecoder->sbr[ele], 1259 retval = sbrDecodeCoupleFrame(hDecoder->sbr[ele],
1270 hDecoder->time_out[ch0], hDecoder->time_out[ch1], 1260 hDecoder->time_out[ch0], hDecoder->time_out[ch1],
1271 hDecoder->postSeekResetFlag, hDecoder->forceUpSampling); 1261 hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
1272 if (retval > 0) 1262 if (retval > 0)
1273 return retval; 1263 return retval;
1274 } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) 1264 } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1275 && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele]) 1265 && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1276 { 1266 {