comparison ac3dec.c @ 5335:8ecef197b08e libavcodec

use shared ac3 bit allocation function
author jbr
date Sun, 15 Jul 2007 13:53:42 +0000
parents d406baea3275
children cc3f5a28aa2a
comparison
equal deleted inserted replaced
5334:d406baea3275 5335:8ecef197b08e
159 int ncplbnd; //number of coupling bands 159 int ncplbnd; //number of coupling bands
160 int ncplsubnd; //number of coupling sub bands 160 int ncplsubnd; //number of coupling sub bands
161 int cplstrtmant; //coupling start mantissa 161 int cplstrtmant; //coupling start mantissa
162 int cplendmant; //coupling end mantissa 162 int cplendmant; //coupling end mantissa
163 int endmant[5]; //channel end mantissas 163 int endmant[5]; //channel end mantissas
164 AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
164 165
165 uint8_t dcplexps[256]; //decoded coupling exponents 166 uint8_t dcplexps[256]; //decoded coupling exponents
166 uint8_t dexps[5][256]; //decoded fbw channel exponents 167 uint8_t dexps[5][256]; //decoded fbw channel exponents
167 uint8_t dlfeexps[256]; //decoded lfe channel exponents 168 uint8_t dlfeexps[256]; //decoded lfe channel exponents
168 uint8_t cplbap[256]; //coupling bit allocation pointers 169 uint8_t cplbap[256]; //coupling bit allocation pointers
547 } 548 }
548 549
549 return 0; 550 return 0;
550 } 551 }
551 552
552 /*********** HELPER FUNCTIONS FOR BIT ALLOCATION ***********/
553 static inline int logadd(int a, int b)
554 {
555 int c = a - b;
556 int address;
557
558 address = FFMIN((FFABS(c) >> 1), 255);
559
560 if (c >= 0)
561 return (a + ff_ac3_latab[address]);
562 else
563 return (b + ff_ac3_latab[address]);
564 }
565
566 static inline int calc_lowcomp(int a, int b0, int b1, int bin)
567 {
568 if (bin < 7) {
569 if ((b0 + 256) == b1)
570 a = 384;
571 else if (b0 > b1)
572 a = FFMAX(0, (a - 64));
573 }
574 else if (bin < 20) {
575 if ((b0 + 256) == b1)
576 a = 320;
577 else if (b0 > b1)
578 a = FFMAX(0, (a - 64));
579 }
580 else
581 a = FFMAX(0, (a - 128));
582
583 return a;
584 }
585 /*********** END HELPER FUNCTIONS FOR BIT ALLOCATION ***********/
586
587 /* Performs bit allocation. 553 /* Performs bit allocation.
588 * This function performs bit allocation for the requested chanenl. 554 * This function performs bit allocation for the requested chanenl.
589 */ 555 */
590 static void do_bit_allocation(AC3DecodeContext *ctx, int chnl) 556 static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
591 { 557 {
592 int16_t psd[256], bndpsd[50], excite[50], mask[50], delta; 558 int fgain, snroffset;
593 int sdecay, fdecay, sgain, dbknee, floor;
594 int lowcomp = 0, fgain = 0, snroffset = 0, fastleak = 0, slowleak = 0, do_delta = 0;
595 int start = 0, end = 0, bin = 0, i = 0, j = 0, k = 0, lastbin = 0, bndstrt = 0;
596 int bndend = 0, begin = 0, deltnseg = 0, band = 0, seg = 0, address = 0;
597 int fscod = ctx->fscod;
598 uint8_t *deltoffst = 0, *deltlen = 0, *deltba = 0;
599 uint8_t *exps = 0, *bap = 0;
600
601 /* initialization */
602 sdecay = ff_sdecaytab[ctx->sdcycod];
603 fdecay = ff_fdecaytab[ctx->fdcycod];
604 sgain = ff_sgaintab[ctx->sgaincod];
605 dbknee = ff_dbkneetab[ctx->dbpbcod];
606 floor = ff_floortab[ctx->floorcod];
607 559
608 if (chnl == 5) { 560 if (chnl == 5) {
609 start = ctx->cplstrtmant;
610 end = ctx->cplendmant;
611 fgain = ff_fgaintab[ctx->cplfgaincod]; 561 fgain = ff_fgaintab[ctx->cplfgaincod];
612 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2; 562 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2;
613 fastleak = (ctx->cplfleak << 8) + 768; 563 ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->cplbap,
614 slowleak = (ctx->cplsleak << 8) + 768; 564 ctx->dcplexps, ctx->cplstrtmant,
615 exps = ctx->dcplexps; 565 ctx->cplendmant, snroffset, fgain, 0,
616 bap = ctx->cplbap; 566 ctx->cpldeltbae, ctx->cpldeltnseg,
617 if (ctx->cpldeltbae == DBA_NEW || ctx->deltbae == DBA_REUSE) { 567 ctx->cpldeltoffst, ctx->cpldeltlen,
618 do_delta = 1; 568 ctx->cpldeltba);
619 deltnseg = ctx->cpldeltnseg;
620 deltoffst = ctx->cpldeltoffst;
621 deltlen = ctx->cpldeltlen;
622 deltba = ctx->cpldeltba;
623 }
624 } 569 }
625 else if (chnl == 6) { 570 else if (chnl == 6) {
626 start = 0;
627 end = 7;
628 lowcomp = 0;
629 fastleak = 0;
630 slowleak = 0;
631 fgain = ff_fgaintab[ctx->lfefgaincod]; 571 fgain = ff_fgaintab[ctx->lfefgaincod];
632 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2; 572 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2;
633 exps = ctx->dlfeexps; 573 ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->lfebap,
634 bap = ctx->lfebap; 574 ctx->dlfeexps, 0, 7, snroffset, fgain, 1,
575 DBA_NONE, 0, NULL, NULL, NULL);
635 } 576 }
636 else { 577 else {
637 start = 0;
638 end = ctx->endmant[chnl];
639 lowcomp = 0;
640 fastleak = 0;
641 slowleak = 0;
642 fgain = ff_fgaintab[ctx->fgaincod[chnl]]; 578 fgain = ff_fgaintab[ctx->fgaincod[chnl]];
643 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2; 579 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2;
644 exps = ctx->dexps[chnl]; 580 ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->bap[chnl],
645 bap = ctx->bap[chnl]; 581 ctx->dexps[chnl], 0, ctx->endmant[chnl],
646 if (ctx->deltbae[chnl] == DBA_NEW || ctx->deltbae[chnl] == DBA_REUSE) { 582 snroffset, fgain, 0, ctx->deltbae[chnl],
647 do_delta = 1; 583 ctx->deltnseg[chnl], ctx->deltoffst[chnl],
648 deltnseg = ctx->deltnseg[chnl]; 584 ctx->deltlen[chnl], ctx->deltba[chnl]);
649 deltoffst = ctx->deltoffst[chnl]; 585 }
650 deltlen = ctx->deltlen[chnl];
651 deltba = ctx->deltba[chnl];
652 }
653 }
654
655 for (bin = start; bin < end; bin++) /* exponent mapping into psd */
656 psd[bin] = psdtab[exps[bin]];
657
658 /* psd integration */
659 j = start;
660 k = masktab[start];
661 do {
662 lastbin = FFMIN((bndtab[k] + ff_ac3_bndsz[k]), end);
663 bndpsd[k] = psd[j];
664 j++;
665 for (i = j; i < lastbin; i++) {
666 bndpsd[k] = logadd(bndpsd[k], psd[j]);
667 j++;
668 }
669 k++;
670 } while (end > lastbin);
671
672 /* compute the excite function */
673 bndstrt = masktab[start];
674 bndend = masktab[end - 1] + 1;
675 if (bndstrt == 0) {
676 lowcomp = calc_lowcomp(lowcomp, bndpsd[0], bndpsd[1], 0);
677 excite[0] = bndpsd[0] - fgain - lowcomp;
678 lowcomp = calc_lowcomp(lowcomp, bndpsd[1], bndpsd[2], 1);
679 excite[1] = bndpsd[1] - fgain - lowcomp;
680 begin = 7;
681 for (bin = 2; bin < 7; bin++) {
682 if ((bndend != 7) || (bin != 6))
683 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
684 fastleak = bndpsd[bin] - fgain;
685 slowleak = bndpsd[bin] - sgain;
686 excite[bin] = fastleak - lowcomp;
687 if ((bndend != 7) || (bin != 6))
688 if (bndpsd[bin] <= bndpsd[bin + 1]) {
689 begin = bin + 1;
690 break;
691 }
692 }
693 for (bin = begin; bin < FFMIN(bndend, 22); bin++) {
694 if ((bndend != 7) || (bin != 6))
695 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
696 fastleak -= fdecay;
697 fastleak = FFMAX(fastleak, (bndpsd[bin] - fgain));
698 slowleak -= sdecay;
699 slowleak = FFMAX(slowleak, (bndpsd[bin] - sgain));
700 excite[bin] = FFMAX((fastleak - lowcomp), slowleak);
701 }
702 begin = 22;
703 }
704 else {
705 begin = bndstrt;
706 }
707 for (bin = begin; bin < bndend; bin++) {
708 fastleak -= fdecay;
709 fastleak = FFMAX(fastleak, (bndpsd[bin] - fgain));
710 slowleak -= sdecay;
711 slowleak = FFMAX(slowleak, (bndpsd[bin] - sgain));
712 excite[bin] = FFMAX(fastleak, slowleak);
713 }
714
715 /* compute the masking curve */
716 for (bin = bndstrt; bin < bndend; bin++) {
717 if (bndpsd[bin] < dbknee)
718 excite[bin] += ((dbknee - bndpsd[bin]) >> 2);
719 mask[bin] = FFMAX(excite[bin], ff_ac3_hth[bin][fscod]);
720 }
721
722 /* apply the delta bit allocation */
723 if (do_delta) {
724 band = 0;
725 for (seg = 0; seg < deltnseg + 1; seg++) {
726 band += deltoffst[seg];
727 if (deltba[seg] >= 4)
728 delta = (deltba[seg] - 3) << 7;
729 else
730 delta = (deltba[seg] - 4) << 7;
731 for (k = 0; k < deltlen[seg]; k++) {
732 mask[band] += delta;
733 band++;
734 }
735 }
736 }
737
738 /*compute the bit allocation */
739 i = start;
740 j = masktab[start];
741 do {
742 lastbin = FFMIN((bndtab[j] + ff_ac3_bndsz[j]), end);
743 mask[j] -= snroffset;
744 mask[j] -= floor;
745 if (mask[j] < 0)
746 mask[j] = 0;
747 mask[j] &= 0x1fe0;
748 mask[j] += floor;
749 for (k = i; k < lastbin; k++) {
750 address = (psd[i] - mask[j]) >> 5;
751 address = FFMIN(63, (FFMAX(0, address)));
752 bap[i] = ff_ac3_baptab[address];
753 i++;
754 }
755 j++;
756 } while (end > lastbin);
757 } 586 }
758 587
759 /* Check if snroffsets are zero. */ 588 /* Check if snroffsets are zero. */
760 static int is_snr_offsets_zero(AC3DecodeContext *ctx) 589 static int is_snr_offsets_zero(AC3DecodeContext *ctx)
761 { 590 {
1854 memset(ctx->cplbap, 0, sizeof (ctx->cplbap)); 1683 memset(ctx->cplbap, 0, sizeof (ctx->cplbap));
1855 memset(ctx->lfebap, 0, sizeof (ctx->lfebap)); 1684 memset(ctx->lfebap, 0, sizeof (ctx->lfebap));
1856 for (i = 0; i < nfchans; i++) 1685 for (i = 0; i < nfchans; i++)
1857 memset(ctx->bap[i], 0, sizeof(ctx->bap[i])); 1686 memset(ctx->bap[i], 0, sizeof(ctx->bap[i]));
1858 } else { 1687 } else {
1688 /* set bit allocation parameters */
1689 ctx->bit_alloc_params.fscod = ctx->fscod;
1690 ctx->bit_alloc_params.halfratecod = 0;
1691 ctx->bit_alloc_params.sdecay = ff_sdecaytab[ctx->sdcycod];
1692 ctx->bit_alloc_params.fdecay = ff_fdecaytab[ctx->fdcycod];
1693 ctx->bit_alloc_params.sgain = ff_sgaintab[ctx->sgaincod];
1694 ctx->bit_alloc_params.dbknee = ff_dbkneetab[ctx->dbpbcod];
1695 ctx->bit_alloc_params.floor = ff_floortab[ctx->floorcod];
1696 ctx->bit_alloc_params.cplfleak = ctx->cplfleak;
1697 ctx->bit_alloc_params.cplsleak = ctx->cplsleak;
1698
1859 if (ctx->chincpl && (bit_alloc_flags & 64)) 1699 if (ctx->chincpl && (bit_alloc_flags & 64))
1860 do_bit_allocation(ctx, 5); 1700 do_bit_allocation(ctx, 5);
1861 for (i = 0; i < nfchans; i++) 1701 for (i = 0; i < nfchans; i++)
1862 if ((bit_alloc_flags >> i) & 1) 1702 if ((bit_alloc_flags >> i) & 1)
1863 do_bit_allocation(ctx, i); 1703 do_bit_allocation(ctx, i);