comparison libfaad2/specrec.c @ 13453:6d50ef45a058

Update FAAD to a 2.1 beta CVS snapshot from 2004.07.12. patch by adland <adland123 at yahoo dot com>
author diego
date Fri, 24 Sep 2004 17:31:36 +0000
parents d81145997036
children 2ae5ab4331ca
comparison
equal deleted inserted replaced
13452:c364b7c13dd8 13453:6d50ef45a058
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 ** Initially modified for use with MPlayer by Arpad Gereöffy on 2003/08/30 25 ** Initially modified for use with MPlayer by Arpad Gereöffy on 2003/08/30
26 ** $Id: specrec.c,v 1.3 2004/06/02 22:59:03 diego Exp $ 26 ** $Id: specrec.c,v 1.4 2004/06/23 13:50:52 diego Exp $
27 ** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/ 27 ** detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
28 **/ 28 **/
29 29
30 /* 30 /*
31 Spectral reconstruction: 31 Spectral reconstruction:
38 #include "structs.h" 38 #include "structs.h"
39 39
40 #include <string.h> 40 #include <string.h>
41 #include <stdlib.h> 41 #include <stdlib.h>
42 #include "specrec.h" 42 #include "specrec.h"
43 #include "filtbank.h"
43 #include "syntax.h" 44 #include "syntax.h"
44 #include "iq_table.h" 45 #include "iq_table.h"
45 #include "ms.h" 46 #include "ms.h"
46 #include "is.h" 47 #include "is.h"
47 #include "pns.h" 48 #include "pns.h"
54 #include "ssr_fb.h" 55 #include "ssr_fb.h"
55 #endif 56 #endif
56 57
57 58
58 /* static function declarations */ 59 /* static function declarations */
59 static void quant_to_spec(ic_stream *ics, real_t *spec_data, uint16_t frame_len); 60 static uint8_t quant_to_spec(NeAACDecHandle hDecoder,
60 static uint8_t inverse_quantization(real_t *x_invquant, const int16_t *x_quant, const uint16_t frame_len); 61 ic_stream *ics, int16_t *quant_data,
62 real_t *spec_data, uint16_t frame_len);
61 63
62 64
63 #ifdef LD_DEC 65 #ifdef LD_DEC
64 ALIGN static const uint8_t num_swb_512_window[] = 66 ALIGN static const uint8_t num_swb_512_window[] =
65 { 67 {
295 scalefactor window band named swb of the window actually used 297 scalefactor window band named swb of the window actually used
296 - determine sect_sfb_offset[g][section],the offset of the first coefficient 298 - determine sect_sfb_offset[g][section],the offset of the first coefficient
297 in section named section. This offset depends on window_sequence and 299 in section named section. This offset depends on window_sequence and
298 scale_factor_grouping and is needed to decode the spectral_data(). 300 scale_factor_grouping and is needed to decode the spectral_data().
299 */ 301 */
300 uint8_t window_grouping_info(faacDecHandle hDecoder, ic_stream *ics) 302 uint8_t window_grouping_info(NeAACDecHandle hDecoder, ic_stream *ics)
301 { 303 {
302 uint8_t i, g; 304 uint8_t i, g;
303 305
304 uint8_t sf_index = hDecoder->sf_index; 306 uint8_t sf_index = hDecoder->sf_index;
305 307
407 default: 409 default:
408 return 1; 410 return 1;
409 } 411 }
410 } 412 }
411 413
412 /* 414 /* iquant() *
413 For ONLY_LONG_SEQUENCE windows (num_window_groups = 1, 415 /* output = sign(input)*abs(input)^(4/3) */
414 window_group_length[0] = 1) the spectral data is in ascending spectral 416 /**/
415 order.
416 For the EIGHT_SHORT_SEQUENCE window, the spectral order depends on the
417 grouping in the following manner:
418 - Groups are ordered sequentially
419 - Within a group, a scalefactor band consists of the spectral data of all
420 grouped SHORT_WINDOWs for the associated scalefactor window band. To
421 clarify via example, the length of a group is in the range of one to eight
422 SHORT_WINDOWs.
423 - If there are eight groups each with length one (num_window_groups = 8,
424 window_group_length[0..7] = 1), the result is a sequence of eight spectra,
425 each in ascending spectral order.
426 - If there is only one group with length eight (num_window_groups = 1,
427 window_group_length[0] = 8), the result is that spectral data of all eight
428 SHORT_WINDOWs is interleaved by scalefactor window bands.
429 - Within a scalefactor window band, the coefficients are in ascending
430 spectral order.
431 */
432 static void quant_to_spec(ic_stream *ics, real_t *spec_data, uint16_t frame_len)
433 {
434 uint8_t g, sfb, win;
435 uint16_t width, bin, k, gindex;
436
437 ALIGN real_t tmp_spec[1024] = {0};
438
439 k = 0;
440 gindex = 0;
441
442 for (g = 0; g < ics->num_window_groups; g++)
443 {
444 uint16_t j = 0;
445 uint16_t gincrease = 0;
446 uint16_t win_inc = ics->swb_offset[ics->num_swb];
447
448 for (sfb = 0; sfb < ics->num_swb; sfb++)
449 {
450 width = ics->swb_offset[sfb+1] - ics->swb_offset[sfb];
451
452 for (win = 0; win < ics->window_group_length[g]; win++)
453 {
454 for (bin = 0; bin < width; bin += 4)
455 {
456 tmp_spec[gindex+(win*win_inc)+j+bin+0] = spec_data[k+0];
457 tmp_spec[gindex+(win*win_inc)+j+bin+1] = spec_data[k+1];
458 tmp_spec[gindex+(win*win_inc)+j+bin+2] = spec_data[k+2];
459 tmp_spec[gindex+(win*win_inc)+j+bin+3] = spec_data[k+3];
460 gincrease += 4;
461 k += 4;
462 }
463 }
464 j += width;
465 }
466 gindex += gincrease;
467 }
468
469 memcpy(spec_data, tmp_spec, frame_len*sizeof(real_t));
470 }
471
472 static INLINE real_t iquant(int16_t q, const real_t *tab, uint8_t *error) 417 static INLINE real_t iquant(int16_t q, const real_t *tab, uint8_t *error)
473 { 418 {
474 #ifdef FIXED_POINT 419 #ifdef FIXED_POINT
420 /* For FIXED_POINT the iq_table is prescaled by 3 bits (iq_table[]/8) */
421 /* BIG_IQ_TABLE allows you to use the full 8192 value table, if this is not
422 * defined a 1026 value table and interpolation will be used
423 */
424 #ifndef BIG_IQ_TABLE
475 static const real_t errcorr[] = { 425 static const real_t errcorr[] = {
476 REAL_CONST(0), REAL_CONST(1.0/8.0), REAL_CONST(2.0/8.0), REAL_CONST(3.0/8.0), 426 REAL_CONST(0), REAL_CONST(1.0/8.0), REAL_CONST(2.0/8.0), REAL_CONST(3.0/8.0),
477 REAL_CONST(4.0/8.0), REAL_CONST(5.0/8.0), REAL_CONST(6.0/8.0), REAL_CONST(7.0/8.0), 427 REAL_CONST(4.0/8.0), REAL_CONST(5.0/8.0), REAL_CONST(6.0/8.0), REAL_CONST(7.0/8.0),
478 REAL_CONST(0) 428 REAL_CONST(0)
479 }; 429 };
480 real_t x1, x2; 430 real_t x1, x2;
431 #endif
481 int16_t sgn = 1; 432 int16_t sgn = 1;
482 433
483 if (q < 0) 434 if (q < 0)
484 { 435 {
485 q = -q; 436 q = -q;
486 sgn = -1; 437 sgn = -1;
487 } 438 }
488 439
489 if (q < IQ_TABLE_SIZE) 440 if (q < IQ_TABLE_SIZE)
441 {
442 //#define IQUANT_PRINT
443 #ifdef IQUANT_PRINT
444 //printf("0x%.8X\n", sgn * tab[q]);
445 printf("%d\n", sgn * tab[q]);
446 #endif
490 return sgn * tab[q]; 447 return sgn * tab[q];
448 }
449
450 #ifndef BIG_IQ_TABLE
451 if (q >= 8192)
452 {
453 *error = 17;
454 return 0;
455 }
491 456
492 /* linear interpolation */ 457 /* linear interpolation */
493 x1 = tab[q>>3]; 458 x1 = tab[q>>3];
494 x2 = tab[(q>>3) + 1]; 459 x2 = tab[(q>>3) + 1];
495 return sgn * 16 * (MUL_R(errcorr[q&7],(x2-x1)) + x1); 460 return sgn * 16 * (MUL_R(errcorr[q&7],(x2-x1)) + x1);
461 #else
462 *error = 17;
463 return 0;
464 #endif
465
496 #else 466 #else
497 if (q < 0) 467 if (q < 0)
498 { 468 {
499 /* tab contains a value for all possible q [0,8192] */ 469 /* tab contains a value for all possible q [0,8192] */
500 if (-q < IQ_TABLE_SIZE) 470 if (-q < IQ_TABLE_SIZE)
509 479
510 *error = 17; 480 *error = 17;
511 return 0; 481 return 0;
512 } 482 }
513 #endif 483 #endif
514 }
515
516 static uint8_t inverse_quantization(real_t *x_invquant, const int16_t *x_quant, const uint16_t frame_len)
517 {
518 int16_t i;
519 uint8_t error = 0; /* Init error flag */
520 const real_t *tab = iq_table;
521
522 for (i = 0; i < frame_len; i+=4)
523 {
524 x_invquant[i] = iquant(x_quant[i], tab, &error);
525 x_invquant[i+1] = iquant(x_quant[i+1], tab, &error);
526 x_invquant[i+2] = iquant(x_quant[i+2], tab, &error);
527 x_invquant[i+3] = iquant(x_quant[i+3], tab, &error);
528 }
529
530 return error;
531 } 484 }
532 485
533 #ifndef FIXED_POINT 486 #ifndef FIXED_POINT
534 ALIGN static const real_t pow2sf_tab[] = { 487 ALIGN static const real_t pow2sf_tab[] = {
535 2.9802322387695313E-008, 5.9604644775390625E-008, 1.1920928955078125E-007, 488 2.9802322387695313E-008, 5.9604644775390625E-008, 1.1920928955078125E-007,
554 8589934592.0, 17179869184.0, 34359738368.0, 507 8589934592.0, 17179869184.0, 34359738368.0,
555 68719476736.0, 137438953472.0, 274877906944.0 508 68719476736.0, 137438953472.0, 274877906944.0
556 }; 509 };
557 #endif 510 #endif
558 511
559 ALIGN static real_t pow2_table[] = 512 /* quant_to_spec: perform dequantisation and scaling
560 { 513 * and in case of short block it also does the deinterleaving
561 #if 0 514 */
562 COEF_CONST(0.59460355750136053335874998528024), /* 2^-0.75 */ 515 /*
563 COEF_CONST(0.70710678118654752440084436210485), /* 2^-0.5 */ 516 For ONLY_LONG_SEQUENCE windows (num_window_groups = 1,
564 COEF_CONST(0.84089641525371454303112547623321), /* 2^-0.25 */ 517 window_group_length[0] = 1) the spectral data is in ascending spectral
565 #endif 518 order.
566 COEF_CONST(1.0), 519 For the EIGHT_SHORT_SEQUENCE window, the spectral order depends on the
567 COEF_CONST(1.1892071150027210667174999705605), /* 2^0.25 */ 520 grouping in the following manner:
568 COEF_CONST(1.4142135623730950488016887242097), /* 2^0.5 */ 521 - Groups are ordered sequentially
569 COEF_CONST(1.6817928305074290860622509524664) /* 2^0.75 */ 522 - Within a group, a scalefactor band consists of the spectral data of all
570 }; 523 grouped SHORT_WINDOWs for the associated scalefactor window band. To
571 524 clarify via example, the length of a group is in the range of one to eight
572 void apply_scalefactors(faacDecHandle hDecoder, ic_stream *ics, 525 SHORT_WINDOWs.
573 real_t *x_invquant, uint16_t frame_len) 526 - If there are eight groups each with length one (num_window_groups = 8,
574 { 527 window_group_length[0..7] = 1), the result is a sequence of eight spectra,
575 uint8_t g, sfb; 528 each in ascending spectral order.
576 uint16_t top; 529 - If there is only one group with length eight (num_window_groups = 1,
577 int32_t exp, frac; 530 window_group_length[0] = 8), the result is that spectral data of all eight
578 uint8_t groups = 0; 531 SHORT_WINDOWs is interleaved by scalefactor window bands.
579 uint16_t nshort = frame_len/8; 532 - Within a scalefactor window band, the coefficients are in ascending
533 spectral order.
534 */
535 static uint8_t quant_to_spec(NeAACDecHandle hDecoder,
536 ic_stream *ics, int16_t *quant_data,
537 real_t *spec_data, uint16_t frame_len)
538 {
539 ALIGN static const real_t pow2_table[] =
540 {
541 COEF_CONST(1.0),
542 COEF_CONST(1.1892071150027210667174999705605), /* 2^0.25 */
543 COEF_CONST(1.4142135623730950488016887242097), /* 2^0.5 */
544 COEF_CONST(1.6817928305074290860622509524664) /* 2^0.75 */
545 };
546 const real_t *tab = iq_table;
547
548 uint8_t g, sfb, win;
549 uint16_t width, bin, k, gindex;
550 uint8_t error = 0; /* Init error flag */
551
552
553 k = 0;
554 gindex = 0;
580 555
581 for (g = 0; g < ics->num_window_groups; g++) 556 for (g = 0; g < ics->num_window_groups; g++)
582 { 557 {
583 uint16_t k = 0; 558 uint16_t j = 0;
584 559 uint16_t gincrease = 0;
585 /* using this nshort*groups doesn't hurt long blocks, because 560 uint16_t win_inc = ics->swb_offset[ics->num_swb];
586 long blocks only have 1 group, so that means 'groups' is 561
587 always 0 for long blocks 562 for (sfb = 0; sfb < ics->num_swb; sfb++)
588 */ 563 {
589 for (sfb = 0; sfb < ics->max_sfb; sfb++) 564 int32_t exp, frac;
590 { 565
591 top = ics->sect_sfb_offset[g][sfb+1]; 566 width = ics->swb_offset[sfb+1] - ics->swb_offset[sfb];
592 567
593 /* 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 */
594 /* just ignore them */ 569 /* just ignore them */
595 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)
596 { 571 {
597 exp = 0; 572 exp = 0;
598 frac = 0; 573 frac = 0;
599 } else { 574 } else {
600 /* ics->scale_factors[g][sfb] must be between 0 and 255 */ 575 /* ics->scale_factors[g][sfb] must be between 0 and 255 */
601 exp = (ics->scale_factors[g][sfb] /* - 100 */) >> 2; 576 exp = (ics->scale_factors[g][sfb] /* - 100 */) >> 2;
577 /* frac must always be > 0 */
602 frac = (ics->scale_factors[g][sfb] /* - 100 */) & 3; 578 frac = (ics->scale_factors[g][sfb] /* - 100 */) & 3;
603 } 579 }
604 580
605 #ifdef FIXED_POINT 581 #ifdef FIXED_POINT
606 exp -= 25; 582 exp -= 25;
614 else 590 else
615 exp -= 7 /*10*/; 591 exp -= 7 /*10*/;
616 } 592 }
617 #endif 593 #endif
618 594
619 /* minimum size of a sf band is 4 and always a multiple of 4 */ 595 for (win = 0; win < ics->window_group_length[g]; win++)
620 for ( ; k < top; k += 4)
621 { 596 {
622 #ifdef FIXED_POINT 597 for (bin = 0; bin < width; bin += 4)
623 if (exp < 0)
624 { 598 {
625 x_invquant[k+(groups*nshort)] >>= -exp; 599 #ifndef FIXED_POINT
626 x_invquant[k+(groups*nshort)+1] >>= -exp; 600 spec_data[gindex+(win*win_inc)+j+bin+0] = iquant(quant_data[k+0], tab, &error) *
627 x_invquant[k+(groups*nshort)+2] >>= -exp; 601 pow2sf_tab[exp/*+25*/] * pow2_table[frac];
628 x_invquant[k+(groups*nshort)+3] >>= -exp; 602 spec_data[gindex+(win*win_inc)+j+bin+1] = iquant(quant_data[k+1], tab, &error) *
629 } else { 603 pow2sf_tab[exp/*+25*/] * pow2_table[frac];
630 x_invquant[k+(groups*nshort)] <<= exp; 604 spec_data[gindex+(win*win_inc)+j+bin+2] = iquant(quant_data[k+2], tab, &error) *
631 x_invquant[k+(groups*nshort)+1] <<= exp; 605 pow2sf_tab[exp/*+25*/] * pow2_table[frac];
632 x_invquant[k+(groups*nshort)+2] <<= exp; 606 spec_data[gindex+(win*win_inc)+j+bin+3] = iquant(quant_data[k+3], tab, &error) *
633 x_invquant[k+(groups*nshort)+3] <<= exp; 607 pow2sf_tab[exp/*+25*/] * pow2_table[frac];
608 #else
609 real_t iq0 = iquant(quant_data[k+0], tab, &error);
610 real_t iq1 = iquant(quant_data[k+1], tab, &error);
611 real_t iq2 = iquant(quant_data[k+2], tab, &error);
612 real_t iq3 = iquant(quant_data[k+3], tab, &error);
613 if (exp < 0)
614 {
615 spec_data[gindex+(win*win_inc)+j+bin+0] = iq0 >>= -exp;
616 spec_data[gindex+(win*win_inc)+j+bin+1] = iq1 >>= -exp;
617 spec_data[gindex+(win*win_inc)+j+bin+2] = iq2 >>= -exp;
618 spec_data[gindex+(win*win_inc)+j+bin+3] = iq3 >>= -exp;
619 } else {
620 spec_data[gindex+(win*win_inc)+j+bin+0] = iq0 <<= exp;
621 spec_data[gindex+(win*win_inc)+j+bin+1] = iq1 <<= exp;
622 spec_data[gindex+(win*win_inc)+j+bin+2] = iq2 <<= exp;
623 spec_data[gindex+(win*win_inc)+j+bin+3] = iq3 <<= exp;
624 }
625 spec_data[gindex+(win*win_inc)+j+bin+0] = MUL_C(spec_data[gindex+(win*win_inc)+j+bin+0],pow2_table[frac]);
626 spec_data[gindex+(win*win_inc)+j+bin+1] = MUL_C(spec_data[gindex+(win*win_inc)+j+bin+1],pow2_table[frac]);
627 spec_data[gindex+(win*win_inc)+j+bin+2] = MUL_C(spec_data[gindex+(win*win_inc)+j+bin+2],pow2_table[frac]);
628 spec_data[gindex+(win*win_inc)+j+bin+3] = MUL_C(spec_data[gindex+(win*win_inc)+j+bin+3],pow2_table[frac]);
629
630 //#define SCFS_PRINT
631 #ifdef SCFS_PRINT
632 //printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
633 //printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
634 //printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
635 //printf("%d\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
636 printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+0]);
637 printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+1]);
638 printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+2]);
639 printf("0x%.8X\n", spec_data[gindex+(win*win_inc)+j+bin+3]);
640 #endif
641 #endif
642
643 gincrease += 4;
644 k += 4;
634 } 645 }
635 #else
636 x_invquant[k+(groups*nshort)] = x_invquant[k+(groups*nshort)] * pow2sf_tab[exp/*+25*/];
637 x_invquant[k+(groups*nshort)+1] = x_invquant[k+(groups*nshort)+1] * pow2sf_tab[exp/*+25*/];
638 x_invquant[k+(groups*nshort)+2] = x_invquant[k+(groups*nshort)+2] * pow2sf_tab[exp/*+25*/];
639 x_invquant[k+(groups*nshort)+3] = x_invquant[k+(groups*nshort)+3] * pow2sf_tab[exp/*+25*/];
640 #endif
641
642 x_invquant[k+(groups*nshort)] = MUL_C(x_invquant[k+(groups*nshort)],pow2_table[frac /* + 3*/]);
643 x_invquant[k+(groups*nshort)+1] = MUL_C(x_invquant[k+(groups*nshort)+1],pow2_table[frac /* + 3*/]);
644 x_invquant[k+(groups*nshort)+2] = MUL_C(x_invquant[k+(groups*nshort)+2],pow2_table[frac /* + 3*/]);
645 x_invquant[k+(groups*nshort)+3] = MUL_C(x_invquant[k+(groups*nshort)+3],pow2_table[frac /* + 3*/]);
646 } 646 }
647 } 647 j += width;
648 groups += ics->window_group_length[g]; 648 }
649 } 649 gindex += gincrease;
650 }
651
652 return error;
650 } 653 }
651 654
652 #ifdef USE_SSE 655 static uint8_t allocate_single_channel(NeAACDecHandle hDecoder, uint8_t channel,
653 void apply_scalefactors_sse(faacDecHandle hDecoder, ic_stream *ics,
654 real_t *x_invquant, uint16_t frame_len)
655 {
656 uint8_t g, sfb;
657 uint16_t top;
658 int32_t exp, frac;
659 uint8_t groups = 0;
660 uint16_t nshort = frame_len/8;
661
662 for (g = 0; g < ics->num_window_groups; g++)
663 {
664 uint16_t k = 0;
665
666 /* using this nshort*groups doesn't hurt long blocks, because
667 long blocks only have 1 group, so that means 'groups' is
668 always 0 for long blocks
669 */
670 for (sfb = 0; sfb < ics->max_sfb; sfb++)
671 {
672 top = ics->sect_sfb_offset[g][sfb+1];
673
674 exp = (ics->scale_factors[g][sfb] /* - 100 */) >> 2;
675 frac = (ics->scale_factors[g][sfb] /* - 100 */) & 3;
676
677 /* minimum size of a sf band is 4 and always a multiple of 4 */
678 for ( ; k < top; k += 4)
679 {
680 __m128 m1 = _mm_load_ps(&x_invquant[k+(groups*nshort)]);
681 __m128 m2 = _mm_load_ps1(&pow2sf_tab[exp /*+25*/]);
682 __m128 m3 = _mm_load_ps1(&pow2_table[frac /* + 3*/]);
683 __m128 m4 = _mm_mul_ps(m1, m2);
684 __m128 m5 = _mm_mul_ps(m3, m4);
685 _mm_store_ps(&x_invquant[k+(groups*nshort)], m5);
686 }
687 }
688 groups += ics->window_group_length[g];
689 }
690 }
691 #endif
692
693 static uint8_t allocate_single_channel(faacDecHandle hDecoder, uint8_t channel,
694 uint8_t output_channels) 656 uint8_t output_channels)
695 { 657 {
696 uint8_t mul = 1; 658 uint8_t mul = 1;
697 659
698 #ifdef MAIN_DEC 660 #ifdef MAIN_DEC
771 #endif 733 #endif
772 734
773 return 0; 735 return 0;
774 } 736 }
775 737
776 static uint8_t allocate_channel_pair(faacDecHandle hDecoder, 738 static uint8_t allocate_channel_pair(NeAACDecHandle hDecoder,
777 uint8_t channel, uint8_t paired_channel) 739 uint8_t channel, uint8_t paired_channel)
778 { 740 {
779 uint8_t mul = 1; 741 uint8_t mul = 1;
780 742
781 #ifdef MAIN_DEC 743 #ifdef MAIN_DEC
876 #endif 838 #endif
877 839
878 return 0; 840 return 0;
879 } 841 }
880 842
881 uint8_t reconstruct_single_channel(faacDecHandle hDecoder, ic_stream *ics, 843 uint8_t reconstruct_single_channel(NeAACDecHandle hDecoder, ic_stream *ics,
882 element *sce, int16_t *spec_data) 844 element *sce, int16_t *spec_data)
883 { 845 {
884 uint8_t retval, output_channels; 846 uint8_t retval, output_channels;
885 ALIGN real_t spec_coef[1024]; 847 ALIGN real_t spec_coef[1024];
886 848
893 #if (defined(PS_DEC) || defined(DRM_PS)) 855 #if (defined(PS_DEC) || defined(DRM_PS))
894 output_channels = hDecoder->ps_used[hDecoder->fr_ch_ele] ? 2 : 1; 856 output_channels = hDecoder->ps_used[hDecoder->fr_ch_ele] ? 2 : 1;
895 #else 857 #else
896 output_channels = 1; 858 output_channels = 1;
897 #endif 859 #endif
860 #ifdef DRM_PS
861 /* for DRM error recovery is crucial */
862 /* simply always allocate 2 channels, you never know when PS will pop up */
863 if (hDecoder->object_type == DRM_ER_LC)
864 output_channels = 2;
865 #endif
898 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0) 866 if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] == 0)
899 { 867 {
900 /* element_output_channels not set yet */ 868 /* element_output_channels not set yet */
901 hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels; 869 hDecoder->element_output_channels[hDecoder->fr_ch_ele] = output_channels;
902 } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != output_channels) { 870 } else if (hDecoder->element_output_channels[hDecoder->fr_ch_ele] != output_channels) {
903 /* element inconsistency */ 871 /* element inconsistency */
904 return 21; 872 return 21;
905 } 873 }
906 874
907
908 if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0) 875 if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
909 { 876 {
910 retval = allocate_single_channel(hDecoder, sce->channel, output_channels); 877 retval = allocate_single_channel(hDecoder, sce->channel, output_channels);
911 if (retval > 0) 878 if (retval > 0)
912 return retval; 879 return retval;
913 880
914 hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1; 881 hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
915 } 882 }
916 883
917 884
918 /* inverse quantization */ 885 /* dequantisation and scaling */
919 retval = inverse_quantization(spec_coef, spec_data, hDecoder->frameLength); 886 retval = quant_to_spec(hDecoder, ics, spec_data, spec_coef, hDecoder->frameLength);
920 if (retval > 0) 887 if (retval > 0)
921 return retval; 888 return retval;
922
923 /* apply scalefactors */
924 #ifndef USE_SSE
925 apply_scalefactors(hDecoder, ics, spec_coef, hDecoder->frameLength);
926 #else
927 hDecoder->apply_sf_func(hDecoder, ics, spec_coef, hDecoder->frameLength);
928 #endif
929
930 /* deinterleave short block grouping */
931 if (ics->window_sequence == EIGHT_SHORT_SEQUENCE)
932 quant_to_spec(ics, spec_coef, hDecoder->frameLength);
933 889
934 #ifdef PROFILE 890 #ifdef PROFILE
935 count = faad_get_ts() - count; 891 count = faad_get_ts() - count;
936 hDecoder->requant_cycles += count; 892 hDecoder->requant_cycles += count;
937 #endif 893 #endif
986 if (hDecoder->drc->present) 942 if (hDecoder->drc->present)
987 { 943 {
988 if (!hDecoder->drc->exclude_mask[sce->channel] || !hDecoder->drc->excluded_chns_present) 944 if (!hDecoder->drc->exclude_mask[sce->channel] || !hDecoder->drc->excluded_chns_present)
989 drc_decode(hDecoder->drc, spec_coef); 945 drc_decode(hDecoder->drc, spec_coef);
990 } 946 }
991
992 947
993 /* filter bank */ 948 /* filter bank */
994 #ifdef SSR_DEC 949 #ifdef SSR_DEC
995 if (hDecoder->object_type != SSR) 950 if (hDecoder->object_type != SSR)
996 { 951 {
1034 989
1035 /* following case can happen when forceUpSampling == 1 */ 990 /* following case can happen when forceUpSampling == 1 */
1036 if (hDecoder->sbr[ele] == NULL) 991 if (hDecoder->sbr[ele] == NULL)
1037 { 992 {
1038 hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength, 993 hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
1039 sce->ele_id, 2*get_sample_rate(hDecoder->sf_index) 994 hDecoder->element_id[ele], 2*get_sample_rate(hDecoder->sf_index),
995 hDecoder->downSampledSBR
1040 #ifdef DRM 996 #ifdef DRM
1041 , 0 997 , 0
1042 #endif 998 #endif
1043 ); 999 );
1044 } 1000 }
1045 1001
1002 if (sce->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
1003 hDecoder->sbr[ele]->maxAACLine = 8*sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)];
1004 else
1005 hDecoder->sbr[ele]->maxAACLine = sce->ics1.swb_offset[max(sce->ics1.max_sfb-1, 0)];
1006
1046 /* check if any of the PS tools is used */ 1007 /* check if any of the PS tools is used */
1047 #if (defined(PS_DEC) || defined(DRM_PS)) 1008 #if (defined(PS_DEC) || defined(DRM_PS))
1048 if (output_channels == 1) 1009 if (hDecoder->ps_used[ele] == 0)
1049 { 1010 {
1050 #endif 1011 #endif
1051 retval = sbrDecodeSingleFrame(hDecoder->sbr[ele], hDecoder->time_out[ch], 1012 retval = sbrDecodeSingleFrame(hDecoder->sbr[ele], hDecoder->time_out[ch],
1052 hDecoder->postSeekResetFlag, hDecoder->forceUpSampling); 1013 hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
1053 #if (defined(PS_DEC) || defined(DRM_PS)) 1014 #if (defined(PS_DEC) || defined(DRM_PS))
1054 } else { 1015 } else {
1055 retval = sbrDecodeSingleFramePS(hDecoder->sbr[ele], hDecoder->time_out[ch], 1016 retval = sbrDecodeSingleFramePS(hDecoder->sbr[ele], hDecoder->time_out[ch],
1056 hDecoder->time_out[ch+1], hDecoder->postSeekResetFlag, 1017 hDecoder->time_out[ch+1], hDecoder->postSeekResetFlag,
1057 hDecoder->forceUpSampling); 1018 hDecoder->downSampledSBR);
1058 } 1019 }
1059 #endif 1020 #endif
1060 if (retval > 0) 1021 if (retval > 0)
1061 return retval; 1022 return retval;
1062 } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) 1023 } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1064 { 1025 {
1065 return 23; 1026 return 23;
1066 } 1027 }
1067 #endif 1028 #endif
1068 1029
1030 #ifdef DRM_PS
1031 /* copy L to R for DRM when no PS is used */
1032 if ((hDecoder->object_type == DRM_ER_LC) &&
1033 (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
1069 return 0; 1044 return 0;
1070 } 1045 }
1071 1046
1072 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,
1073 element *cpe, int16_t *spec_data1, int16_t *spec_data2) 1048 element *cpe, int16_t *spec_data1, int16_t *spec_data2)
1074 { 1049 {
1075 uint8_t retval; 1050 uint8_t retval;
1076 ALIGN real_t spec_coef1[1024]; 1051 ALIGN real_t spec_coef1[1024];
1077 ALIGN real_t spec_coef2[1024]; 1052 ALIGN real_t spec_coef2[1024];
1079 #ifdef PROFILE 1054 #ifdef PROFILE
1080 int64_t count = faad_get_ts(); 1055 int64_t count = faad_get_ts();
1081 #endif 1056 #endif
1082 if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0) 1057 if (hDecoder->element_alloced[hDecoder->fr_ch_ele] == 0)
1083 { 1058 {
1084 retval = allocate_channel_pair(hDecoder, cpe->channel, cpe->paired_channel); 1059 retval = allocate_channel_pair(hDecoder, cpe->channel, (uint8_t)cpe->paired_channel);
1085 if (retval > 0) 1060 if (retval > 0)
1086 return retval; 1061 return retval;
1087 1062
1088 hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1; 1063 hDecoder->element_alloced[hDecoder->fr_ch_ele] = 1;
1089 } 1064 }
1090 1065
1091 /* inverse quantization */ 1066 /* dequantisation and scaling */
1092 retval = inverse_quantization(spec_coef1, spec_data1, hDecoder->frameLength); 1067 retval = quant_to_spec(hDecoder, ics1, spec_data1, spec_coef1, hDecoder->frameLength);
1093 if (retval > 0) 1068 if (retval > 0)
1094 return retval; 1069 return retval;
1095 1070 retval = quant_to_spec(hDecoder, ics2, spec_data2, spec_coef2, hDecoder->frameLength);
1096 retval = inverse_quantization(spec_coef2, spec_data2, hDecoder->frameLength);
1097 if (retval > 0) 1071 if (retval > 0)
1098 return retval; 1072 return retval;
1099
1100 /* apply scalefactors */
1101 #ifndef USE_SSE
1102 apply_scalefactors(hDecoder, ics1, spec_coef1, hDecoder->frameLength);
1103 apply_scalefactors(hDecoder, ics2, spec_coef2, hDecoder->frameLength);
1104 #else
1105 hDecoder->apply_sf_func(hDecoder, ics1, spec_coef1, hDecoder->frameLength);
1106 hDecoder->apply_sf_func(hDecoder, ics2, spec_coef2, hDecoder->frameLength);
1107 #endif
1108
1109 /* deinterleave short block grouping */
1110 if (ics1->window_sequence == EIGHT_SHORT_SEQUENCE)
1111 quant_to_spec(ics1, spec_coef1, hDecoder->frameLength);
1112 if (ics2->window_sequence == EIGHT_SHORT_SEQUENCE)
1113 quant_to_spec(ics2, spec_coef2, hDecoder->frameLength);
1114 1073
1115 #ifdef PROFILE 1074 #ifdef PROFILE
1116 count = faad_get_ts() - count; 1075 count = faad_get_ts() - count;
1117 hDecoder->requant_cycles += count; 1076 hDecoder->requant_cycles += count;
1118 #endif 1077 #endif
1259 1218
1260 /* following case can happen when forceUpSampling == 1 */ 1219 /* following case can happen when forceUpSampling == 1 */
1261 if (hDecoder->sbr[ele] == NULL) 1220 if (hDecoder->sbr[ele] == NULL)
1262 { 1221 {
1263 hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength, 1222 hDecoder->sbr[ele] = sbrDecodeInit(hDecoder->frameLength,
1264 cpe->ele_id, 2*get_sample_rate(hDecoder->sf_index) 1223 hDecoder->element_id[ele], 2*get_sample_rate(hDecoder->sf_index),
1224 hDecoder->downSampledSBR
1265 #ifdef DRM 1225 #ifdef DRM
1266 , 0 1226 , 0
1267 #endif 1227 #endif
1268 ); 1228 );
1269 } 1229 }
1270 1230
1231 if (cpe->ics1.window_sequence == EIGHT_SHORT_SEQUENCE)
1232 hDecoder->sbr[ele]->maxAACLine = 8*cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)];
1233 else
1234 hDecoder->sbr[ele]->maxAACLine = cpe->ics1.swb_offset[max(cpe->ics1.max_sfb-1, 0)];
1235
1271 retval = sbrDecodeCoupleFrame(hDecoder->sbr[ele], 1236 retval = sbrDecodeCoupleFrame(hDecoder->sbr[ele],
1272 hDecoder->time_out[ch0], hDecoder->time_out[ch1], 1237 hDecoder->time_out[ch0], hDecoder->time_out[ch1],
1273 hDecoder->postSeekResetFlag, hDecoder->forceUpSampling); 1238 hDecoder->postSeekResetFlag, hDecoder->downSampledSBR);
1274 if (retval > 0) 1239 if (retval > 0)
1275 return retval; 1240 return retval;
1276 } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) 1241 } else if (((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1))
1277 && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele]) 1242 && !hDecoder->sbr_alloced[hDecoder->fr_ch_ele])
1278 { 1243 {