comparison ac3dec.c @ 5310:9aa9197034d7 libavcodec

AC-3 decoder, soc revision 40, Aug 9 00:10:14 2006 UTC by cloud9 More code cleanup. Window is now runtime generated. Fixed the bugs in rematrixing routine and in Decoding AC3 Bitstreams when coupling is in use. Still struggling to find out what affects the quality of the produced sound. Can anybody have a look at the imdct routines do_imdct_256 and do_imdct_512 and tell me whether it is the correctly implemented as described in standard.
author jbr
date Sat, 14 Jul 2007 15:57:51 +0000
parents 0662a270aab7
children 7742d5411c9d
comparison
equal deleted inserted replaced
5309:0662a270aab7 5310:9aa9197034d7
217 return ((dither_uint32(state) << 16) >> 16); 217 return ((dither_uint32(state) << 16) >> 16);
218 } 218 }
219 219
220 /* END Mersenne Twister */ 220 /* END Mersenne Twister */
221 221
222 /**
223 * Generate a Kaiser Window.
224 */
225 static void
226 k_window_init(int alpha, double *window, int n, int iter)
227 {
228 int j, k;
229 double a, x;
230 a = alpha * M_PI / n;
231 a = a*a;
232 for(k=0; k<n; k++) {
233 x = k * (n - k) * a;
234 window[k] = 1.0;
235 for(j=iter; j>0; j--) {
236 window[k] = (window[k] * x / (j*j)) + 1.0;
237 }
238 }
239 }
240
241 /**
242 * Generate a Kaiser-Bessel Derived Window.
243 * @param alpha determines window shape
244 * @param window array to fill with window values
245 * @param n length of the window
246 * @param iter number of iterations to use in BesselI0
247 */
248 static void
249 kbd_window_init(int alpha, double *window, int n, int iter)
250 {
251 int k, n2;
252 double *kwindow;
253
254 n2 = n >> 1;
255 kwindow = &window[n2];
256 k_window_init(alpha, kwindow, n2, iter);
257 window[0] = kwindow[0];
258 for(k=1; k<n2; k++) {
259 window[k] = window[k-1] + kwindow[k];
260 }
261 for(k=0; k<n2; k++) {
262 window[k] = sqrt(window[k] / (window[n2-1]+1));
263 window[n-1-k] = window[k];
264 }
265 }
266
222 static void generate_quantizers_table(int16_t quantizers[], int level, int length) 267 static void generate_quantizers_table(int16_t quantizers[], int level, int length)
223 { 268 {
224 int i; 269 int i;
225 270
226 for (i = 0; i < length; i++) 271 for (i = 0; i < length; i++)
340 for (i = 0; i < N / 8; i++) { 385 for (i = 0; i < N / 8; i++) {
341 alpha = 2 * M_PI * (8 * i + 1) / (4 * N); 386 alpha = 2 * M_PI * (8 * i + 1) / (4 * N);
342 x_cos2[i] = -cos(alpha); 387 x_cos2[i] = -cos(alpha);
343 x_sin2[i] = -sin(alpha); 388 x_sin2[i] = -sin(alpha);
344 } 389 }
390
391 /* Kaiser-Bessel derived window. */
392 kbd_window_init(5, window, 256, 100);
345 } 393 }
346 394
347 395
348 static int ac3_decode_init(AVCodecContext *avctx) 396 static int ac3_decode_init(AVCodecContext *avctx)
349 { 397 {
371 //Returns -1 when 'fscod' is not valid; 419 //Returns -1 when 'fscod' is not valid;
372 static int ac3_parse_sync_info(AC3DecodeContext *ctx) 420 static int ac3_parse_sync_info(AC3DecodeContext *ctx)
373 { 421 {
374 GetBitContext *gb = &ctx->gb; 422 GetBitContext *gb = &ctx->gb;
375 int frmsizecod, bsid; 423 int frmsizecod, bsid;
376
377 memset (ctx, sizeof (AC3DecodeContext), 0);
378 424
379 skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16); 425 skip_bits(gb, 16); //skip the sync_word, sync_info->sync_word = get_bits(gb, 16);
380 ctx->crc1 = get_bits(gb, 16); 426 ctx->crc1 = get_bits(gb, 16);
381 ctx->fscod = get_bits(gb, 2); 427 ctx->fscod = get_bits(gb, 2);
382 if (ctx->fscod == 0x03) 428 if (ctx->fscod == 0x03)
469 * Uses liba52 tables. 515 * Uses liba52 tables.
470 */ 516 */
471 static int decode_exponents(GetBitContext *gb, int expstr, int ngrps, uint8_t absexp, uint8_t *dexps) 517 static int decode_exponents(GetBitContext *gb, int expstr, int ngrps, uint8_t absexp, uint8_t *dexps)
472 { 518 {
473 int exps; 519 int exps;
474 av_log(NULL, AV_LOG_INFO, "%d\n", ngrps);
475 520
476 while (ngrps--) { 521 while (ngrps--) {
477 exps = get_bits(gb, 7); 522 exps = get_bits(gb, 7);
478 523
479 absexp += exp_1[exps]; 524 absexp += exp_1[exps];
528 static inline int logadd(int a, int b) 573 static inline int logadd(int a, int b)
529 { 574 {
530 int c = a - b; 575 int c = a - b;
531 int address; 576 int address;
532 577
533 address = FFMIN(ABS(c) >> 1, 255); 578 address = FFMIN((ABS(c) >> 1), 255);
534 579
535 if (c >= 0) 580 if (c >= 0)
536 return (a + latab[address]); 581 return (a + latab[address]);
537 else 582 else
538 return (b + latab[address]); 583 return (b + latab[address]);
542 { 587 {
543 if (bin < 7) { 588 if (bin < 7) {
544 if ((b0 + 256) == b1) 589 if ((b0 + 256) == b1)
545 a = 384; 590 a = 384;
546 else if (b0 > b1) 591 else if (b0 > b1)
547 a = FFMAX(0, a - 64); 592 a = FFMAX(0, (a - 64));
548 } 593 }
549 else if (bin < 20) { 594 else if (bin < 20) {
550 if ((b0 + 256) == b1) 595 if ((b0 + 256) == b1)
551 a = 320; 596 a = 320;
552 else if (b0 > b1) 597 else if (b0 > b1)
553 a = FFMAX(0, a - 64); 598 a = FFMAX(0, (a - 64));
554 } 599 }
555 else 600 else
556 a = FFMAX(0, a - 128); 601 a = FFMAX(0, (a - 128));
557 602
558 return a; 603 return a;
559 } 604 }
560 605
561 /* do the bit allocation for chnl. 606 /* do the bit allocation for chnl.
562 * chnl = 0 to 4 - fbw channel 607 * chnl = 0 to 4 - fbw channel
563 * chnl = 5 coupling channel 608 * chnl = 5 coupling channel
564 * chnl = 6 lfe channel 609 * chnl = 6 lfe channel
565 */ 610 */
566 static void do_bit_allocation1(AC3DecodeContext *ctx, int chnl) 611 static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
567 { 612 {
613 int16_t psd[256], bndpsd[50], excite[50], mask[50], delta;
568 int sdecay, fdecay, sgain, dbknee, floor; 614 int sdecay, fdecay, sgain, dbknee, floor;
569 int lowcomp = 0, fgain = 0, snroffset = 0, fastleak = 0, slowleak = 0; 615 int lowcomp = 0, fgain = 0, snroffset = 0, fastleak = 0, slowleak = 0, do_delta = 0;
570 int psd[256], bndpsd[50], excite[50], mask[50], delta;
571 int start = 0, end = 0, bin = 0, i = 0, j = 0, k = 0, lastbin = 0, bndstrt = 0; 616 int start = 0, end = 0, bin = 0, i = 0, j = 0, k = 0, lastbin = 0, bndstrt = 0;
572 int bndend = 0, begin = 0, deltnseg = 0, band = 0, seg = 0, address = 0; 617 int bndend = 0, begin = 0, deltnseg = 0, band = 0, seg = 0, address = 0;
573 int fscod = ctx->fscod; 618 int fscod = ctx->fscod;
574 uint8_t *exps, *deltoffst = 0, *deltlen = 0, *deltba = 0; 619 uint8_t *deltoffst = 0, *deltlen = 0, *deltba = 0;
575 uint8_t *baps; 620 uint8_t *exps = 0, *bap = 0;
576 int do_delta = 0;
577 621
578 /* initialization */ 622 /* initialization */
579 sdecay = sdecaytab[ctx->sdcycod]; 623 sdecay = sdecaytab[ctx->sdcycod];
580 fdecay = fdecaytab[ctx->fdcycod]; 624 fdecay = fdecaytab[ctx->fdcycod];
581 sgain = sgaintab[ctx->sgaincod]; 625 sgain = sgaintab[ctx->sgaincod];
588 fgain = fgaintab[ctx->cplfgaincod]; 632 fgain = fgaintab[ctx->cplfgaincod];
589 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2; 633 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->cplfsnroffst) << 2;
590 fastleak = (ctx->cplfleak << 8) + 768; 634 fastleak = (ctx->cplfleak << 8) + 768;
591 slowleak = (ctx->cplsleak << 8) + 768; 635 slowleak = (ctx->cplsleak << 8) + 768;
592 exps = ctx->dcplexps; 636 exps = ctx->dcplexps;
593 baps = ctx->cplbap; 637 bap = ctx->cplbap;
594 if (ctx->cpldeltbae == AC3_DBASTR_NEW) { 638 if (ctx->cpldeltbae == AC3_DBASTR_NEW || ctx->deltbae == AC3_DBASTR_REUSE) {
595 do_delta = 1; 639 do_delta = 1;
596 deltnseg = ctx->cpldeltnseg; 640 deltnseg = ctx->cpldeltnseg;
597 deltoffst = ctx->cpldeltoffst; 641 deltoffst = ctx->cpldeltoffst;
598 deltlen = ctx->cpldeltlen; 642 deltlen = ctx->cpldeltlen;
599 deltba = ctx->cpldeltba; 643 deltba = ctx->cpldeltba;
606 fastleak = 0; 650 fastleak = 0;
607 slowleak = 0; 651 slowleak = 0;
608 fgain = fgaintab[ctx->lfefgaincod]; 652 fgain = fgaintab[ctx->lfefgaincod];
609 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2; 653 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->lfefsnroffst) << 2;
610 exps = ctx->dlfeexps; 654 exps = ctx->dlfeexps;
611 baps = ctx->lfebap; 655 bap = ctx->lfebap;
612 } 656 }
613 else { 657 else {
614 start = 0; 658 start = 0;
615 end = ctx->endmant[chnl]; 659 end = ctx->endmant[chnl];
616 lowcomp = 0; 660 lowcomp = 0;
617 fastleak = 0; 661 fastleak = 0;
618 slowleak = 0; 662 slowleak = 0;
619 fgain = fgaintab[ctx->fgaincod[chnl]]; 663 fgain = fgaintab[ctx->fgaincod[chnl]];
620 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2; 664 snroffset = (((ctx->csnroffst - 15) << 4) + ctx->fsnroffst[chnl]) << 2;
621 exps = ctx->dexps[chnl]; 665 exps = ctx->dexps[chnl];
622 baps = ctx->bap[chnl]; 666 bap = ctx->bap[chnl];
623 if (ctx->deltbae[chnl] == AC3_DBASTR_NEW) { 667 if (ctx->deltbae[chnl] == AC3_DBASTR_NEW || ctx->deltbae[chnl] == AC3_DBASTR_REUSE) {
624 do_delta = 1; 668 do_delta = 1;
625 deltnseg = ctx->deltnseg[chnl]; 669 deltnseg = ctx->deltnseg[chnl];
626 deltoffst = ctx->deltoffst[chnl]; 670 deltoffst = ctx->deltoffst[chnl];
627 deltlen = ctx->deltlen[chnl]; 671 deltlen = ctx->deltlen[chnl];
628 deltba = ctx->deltba[chnl]; 672 deltba = ctx->deltba[chnl];
629 } 673 }
630 } 674 }
631 675
632 for (bin = start; bin < end; bin++) /* exponent mapping into psd */ 676 for (bin = start; bin < end; bin++) /* exponent mapping into psd */
633 psd[bin] = (3072 - ((int)(exps[bin]) << 7)); 677 psd[bin] = (3072 - (exps[bin] << 7));
634 678
635 /* psd integration */ 679 /* psd integration */
636 j = start; 680 j = start;
637 k = masktab[start]; 681 k = masktab[start];
638 do { 682 do {
639 lastbin = FFMIN(bndtab[k] + bndsz[k], end); 683 lastbin = FFMIN((bndtab[k] + bndsz[k]), end);
640 bndpsd[k] = psd[j]; 684 bndpsd[k] = psd[j];
641 j++; 685 j++;
642 for (i = j; i < lastbin; i++) { 686 for (i = j; i < lastbin; i++) {
643 bndpsd[k] = logadd(bndpsd[k], psd[j]); 687 bndpsd[k] = logadd(bndpsd[k], psd[j]);
644 j++; 688 j++;
654 excite[0] = bndpsd[0] - fgain - lowcomp; 698 excite[0] = bndpsd[0] - fgain - lowcomp;
655 lowcomp = calc_lowcomp(lowcomp, bndpsd[1], bndpsd[2], 1); 699 lowcomp = calc_lowcomp(lowcomp, bndpsd[1], bndpsd[2], 1);
656 excite[1] = bndpsd[1] - fgain - lowcomp; 700 excite[1] = bndpsd[1] - fgain - lowcomp;
657 begin = 7; 701 begin = 7;
658 for (bin = 2; bin < 7; bin++) { 702 for (bin = 2; bin < 7; bin++) {
659 if (!(chnl == 6 && bin == 6)) 703 if ((bndend != 7) || (bin != 6))
660 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin); 704 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
661 fastleak = bndpsd[bin] - fgain; 705 fastleak = bndpsd[bin] - fgain;
662 slowleak = bndpsd[bin] - sgain; 706 slowleak = bndpsd[bin] - sgain;
663 excite[bin] = fastleak - lowcomp; 707 excite[bin] = fastleak - lowcomp;
664 if (!(chnl == 6 && bin == 6)) 708 if ((bndend != 7) || (bin != 6))
665 if (bndpsd[bin] <= bndpsd[bin + 1]) { 709 if (bndpsd[bin] <= bndpsd[bin + 1]) {
666 begin = bin + 1; 710 begin = bin + 1;
667 break; 711 break;
668 } 712 }
669 } 713 }
670 for (bin = begin; bin < FFMIN(bndend, 22); bin++) { 714 for (bin = begin; bin < FFMIN(bndend, 22); bin++) {
671 if (!(chnl == 6 && bin == 6)) 715 if ((bndend != 7) || (bin != 6))
672 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin); 716 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin + 1], bin);
673 fastleak -= fdecay; 717 fastleak -= fdecay;
674 fastleak = FFMAX(fastleak, bndpsd[bin] - fgain); 718 fastleak = FFMAX(fastleak, (bndpsd[bin] - fgain));
675 slowleak -= sdecay; 719 slowleak -= sdecay;
676 slowleak = FFMAX(slowleak, bndpsd[bin] - sgain); 720 slowleak = FFMAX(slowleak, (bndpsd[bin] - sgain));
677 excite[bin] = FFMAX(fastleak - lowcomp, slowleak); 721 excite[bin] = FFMAX((fastleak - lowcomp), slowleak);
678 } 722 }
679 begin = 22; 723 begin = 22;
680 } 724 }
681 else { 725 else {
682 begin = bndstrt; 726 begin = bndstrt;
683 } 727 }
684 for (bin = begin; bin < bndend; bin++) { 728 for (bin = begin; bin < bndend; bin++) {
685 fastleak -= fdecay; 729 fastleak -= fdecay;
686 fastleak = FFMAX(fastleak, bndpsd[bin] - fgain); 730 fastleak = FFMAX(fastleak, (bndpsd[bin] - fgain));
687 slowleak -= sdecay; 731 slowleak -= sdecay;
688 slowleak = FFMAX(slowleak, bndpsd[bin] - sgain); 732 slowleak = FFMAX(slowleak, (bndpsd[bin] - sgain));
689 excite[bin] = FFMAX(fastleak, slowleak); 733 excite[bin] = FFMAX(fastleak, slowleak);
690 } 734 }
691 735
692 /* compute the masking curve */ 736 /* compute the masking curve */
693 for (bin = bndstrt; bin < bndend; bin++) { 737 for (bin = bndstrt; bin < bndend; bin++) {
698 742
699 /* apply the delta bit allocation */ 743 /* apply the delta bit allocation */
700 if (do_delta) { 744 if (do_delta) {
701 band = 0; 745 band = 0;
702 for (seg = 0; seg < deltnseg + 1; seg++) { 746 for (seg = 0; seg < deltnseg + 1; seg++) {
703 band += (int)(deltoffst[seg]); 747 band += deltoffst[seg];
704 if ((int)(deltba[seg]) >= 4) 748 if (deltba[seg] >= 4)
705 delta = ((int)(deltba[seg]) - 3) << 7; 749 delta = (deltba[seg] - 3) << 7;
706 else 750 else
707 delta = ((int)(deltba[seg]) - 4) << 7; 751 delta = (deltba[seg] - 4) << 7;
708 for (k = 0; k < (int)(deltlen[seg]); k++) { 752 for (k = 0; k < deltlen[seg]; k++) {
709 mask[band] += delta; 753 mask[band] += delta;
710 band++; 754 band++;
711 } 755 }
712 } 756 }
713 } 757 }
714 758
715 /*compute the bit allocation */ 759 /*compute the bit allocation */
716 i = start; 760 i = start;
717 j = masktab[start]; 761 j = masktab[start];
718 do { 762 do {
719 lastbin = FFMIN(bndtab[j] + bndsz[j], end); 763 lastbin = FFMIN((bndtab[j] + bndsz[j]), end);
720 mask[j] -= snroffset; 764 mask[j] -= snroffset;
721 mask[j] -= floor; 765 mask[j] -= floor;
722 if (mask[j] < 0) 766 if (mask[j] < 0)
723 mask[j] = 0; 767 mask[j] = 0;
724 mask[j] &= 0x1fe0; 768 mask[j] &= 0x1fe0;
725 mask[j] += floor; 769 mask[j] += floor;
726 for (k = i; k < lastbin; k++) { 770 for (k = i; k < lastbin; k++) {
727 address = (psd[i] - mask[j]) >> 5; 771 address = (psd[i] - mask[j]) >> 5;
728 address = FFMIN(63, FFMAX(0, address)); 772 address = FFMIN(63, (FFMAX(0, address)));
729 baps[i] = baptab[address]; 773 bap[i] = baptab[address];
730 i++; 774 i++;
731 } 775 }
732 j++; 776 j++;
733 } while (end > lastbin); 777 } while (end > lastbin);
734 } 778 }
735 779
736 static void do_bit_allocation(AC3DecodeContext *ctx, int flags) 780 /* Check if snroffsets are zero. */
737 { 781 static int is_snr_offsets_zero(AC3DecodeContext *ctx)
738 int i, zerosnroffst = 1; 782 {
739 783 int i;
740 if (!flags) /* bit allocation is not required */ 784
741 return; 785 if ((ctx->csnroffst) || (ctx->cplinu && ctx->cplfsnroffst) ||
742
743 /* Check if snroffsets are zero. */
744 if ((ctx->csnroffst) || (ctx->chincpl && ctx->cplfsnroffst) ||
745 (ctx->lfeon && ctx->lfefsnroffst)) 786 (ctx->lfeon && ctx->lfefsnroffst))
746 zerosnroffst = 0; 787 return 0;
747 if (zerosnroffst) 788
748 for (i = 0; i < ctx->nfchans; i++)
749 if (ctx->fsnroffst[i]) {
750 zerosnroffst = 0;
751 break;
752 }
753
754 if (zerosnroffst) {
755 memset(ctx->cplbap, 0, sizeof (ctx->cplbap));
756 for (i = 0; i < ctx->nfchans; i++)
757 memset(ctx->bap[i], 0, sizeof (ctx->bap[i]));
758 memset(ctx->lfebap, 0, sizeof (ctx->lfebap));
759 return;
760 }
761
762 /* perform bit allocation */
763 if (ctx->cplinu && (flags & 64))
764 do_bit_allocation1(ctx, 5);
765 for (i = 0; i < ctx->nfchans; i++) 789 for (i = 0; i < ctx->nfchans; i++)
766 if (flags & (1 << i)) 790 if (ctx->fsnroffst[i])
767 do_bit_allocation1(ctx, i); 791 return 0;
768 if (ctx->lfeon && (flags & 32)) 792
769 do_bit_allocation1(ctx, 6); 793 return 1;
770 } 794 }
771 795
772 typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */ 796 typedef struct { /* grouped mantissas for 3-level 5-leve and 11-level quantization */
773 int16_t l3_quantizers[3]; 797 int16_t l3_quantizers[3];
774 int16_t l5_quantizers[3]; 798 int16_t l5_quantizers[3];
775 int16_t l11_quantizers[2]; 799 int16_t l11_quantizers[2];
776 int l3ptr; 800 int l3ptr;
777 int l5ptr; 801 int l5ptr;
778 int l11ptr; 802 int l11ptr;
779 int bits;
780 } mant_groups; 803 } mant_groups;
781 804
782 #define TRANSFORM_COEFF(tc, m, e, f) (tc) = (m) * (f)[(e)] 805 #define TRANSFORM_COEFF(tc, m, e, f) (tc) = (m) * (f)[(e)]
783 806
784 /* Get the transform coefficients for coupling channel and uncouple channels. 807 /* Get the transform coefficients for coupling channel and uncouple channels.
787 * getting transform coefficients for the channel. 810 * getting transform coefficients for the channel.
788 */ 811 */
789 static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m) 812 static int get_transform_coeffs_cpling(AC3DecodeContext *ctx, mant_groups *m)
790 { 813 {
791 GetBitContext *gb = &ctx->gb; 814 GetBitContext *gb = &ctx->gb;
792 int ch, bin, start, end, cplbndstrc, bnd, gcode, tbap; 815 int ch, start, end, cplbndstrc, bnd, gcode, tbap;
793 float cplcos[5], cplcoeff; 816 float cplcos[5], cplcoeff;
794 uint8_t *exps = ctx->dcplexps; 817 uint8_t *exps = ctx->dcplexps;
795 uint8_t *bap = ctx->cplbap; 818 uint8_t *bap = ctx->cplbap;
796
797 int bits_consumed = m->bits;
798 819
799 cplbndstrc = ctx->cplbndstrc; 820 cplbndstrc = ctx->cplbndstrc;
800 start = ctx->cplstrtmant; 821 start = ctx->cplstrtmant;
801 bnd = 0; 822 bnd = 0;
802 823
813 834
814 while (start < end) { 835 while (start < end) {
815 tbap = bap[start]; 836 tbap = bap[start];
816 switch(tbap) { 837 switch(tbap) {
817 case 0: 838 case 0:
818 for (ch = 0; ch < ctx->nfchans; ctx++) 839 for (ch = 0; ch < ctx->nfchans; ch++)
819 if (((ctx->chincpl) >> ch) & 1) { 840 if (((ctx->chincpl) >> ch) & 1) {
820 if (((ctx->dithflag) >> ch) & 1) { 841 if (!(((ctx->dithflag) >> ch) & 1)) {
821 TRANSFORM_COEFF(cplcoeff, dither_int16(&ctx->dith_state), exps[start], scale_factors); 842 TRANSFORM_COEFF(cplcoeff, dither_int16(&ctx->dith_state), exps[start], scale_factors);
822 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch]; 843 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
823 } else 844 } else
824 ctx->transform_coeffs[ch + 1][start] = 0; 845 ctx->transform_coeffs[ch + 1][start] = 0;
825 } 846 }
826 start++; 847 start++;
827 continue; 848 continue;
828 case 1: 849 case 1:
829 if (m->l3ptr > 2) { 850 if (m->l3ptr > 2) {
830 gcode = get_bits(gb, 5); 851 gcode = get_bits(gb, 5);
831 /*if (gcode > 26)
832 return -1;*/
833 m->l3_quantizers[0] = l3_quantizers_1[gcode]; 852 m->l3_quantizers[0] = l3_quantizers_1[gcode];
834 m->l3_quantizers[1] = l3_quantizers_2[gcode]; 853 m->l3_quantizers[1] = l3_quantizers_2[gcode];
835 m->l3_quantizers[2] = l3_quantizers_3[gcode]; 854 m->l3_quantizers[2] = l3_quantizers_3[gcode];
836 m->l3ptr = 0; 855 m->l3ptr = 0;
837 m->bits += 5;
838 } 856 }
839 TRANSFORM_COEFF(cplcoeff, m->l3_quantizers[m->l3ptr++], exps[start], scale_factors); 857 TRANSFORM_COEFF(cplcoeff, m->l3_quantizers[m->l3ptr++], exps[start], scale_factors);
840 break; 858 break;
841 859
842 case 2: 860 case 2:
843 if (m->l5ptr > 2) { 861 if (m->l5ptr > 2) {
844 gcode = get_bits(gb, 7); 862 gcode = get_bits(gb, 7);
845 /*if (gcode > 124)
846 return -1;*/
847 m->l5_quantizers[0] = l5_quantizers_1[gcode]; 863 m->l5_quantizers[0] = l5_quantizers_1[gcode];
848 m->l5_quantizers[1] = l5_quantizers_2[gcode]; 864 m->l5_quantizers[1] = l5_quantizers_2[gcode];
849 m->l5_quantizers[2] = l5_quantizers_3[gcode]; 865 m->l5_quantizers[2] = l5_quantizers_3[gcode];
850 m->l5ptr = 0; 866 m->l5ptr = 0;
851 m->bits += 7;
852 } 867 }
853 TRANSFORM_COEFF(cplcoeff, m->l5_quantizers[m->l5ptr++], exps[start], scale_factors); 868 TRANSFORM_COEFF(cplcoeff, m->l5_quantizers[m->l5ptr++], exps[start], scale_factors);
854 break; 869 break;
855 870
856 case 3: 871 case 3:
857 gcode = get_bits(gb, 3); 872 TRANSFORM_COEFF(cplcoeff, l7_quantizers[get_bits(gb, 3)], exps[start], scale_factors);
858 /*if (gcode > 6)
859 return -1;*/
860 m->bits += 3;
861 TRANSFORM_COEFF(cplcoeff, l7_quantizers[gcode], exps[start], scale_factors);
862 break; 873 break;
863 874
864 case 4: 875 case 4:
865 if (m->l11ptr > 1) { 876 if (m->l11ptr > 1) {
866 gcode = get_bits(gb, 7); 877 gcode = get_bits(gb, 7);
867 /*if (gcode > 120)
868 return -1;*/
869 m->l11_quantizers[0] = l11_quantizers_1[gcode]; 878 m->l11_quantizers[0] = l11_quantizers_1[gcode];
870 m->l11_quantizers[1] = l11_quantizers_2[gcode]; 879 m->l11_quantizers[1] = l11_quantizers_2[gcode];
871 m->l11ptr = 0; 880 m->l11ptr = 0;
872 m->bits += 7;
873 } 881 }
874 TRANSFORM_COEFF(cplcoeff, m->l11_quantizers[m->l11ptr++], exps[start], scale_factors); 882 TRANSFORM_COEFF(cplcoeff, m->l11_quantizers[m->l11ptr++], exps[start], scale_factors);
875 break; 883 break;
876 884
877 case 5: 885 case 5:
878 gcode = get_bits(gb, 4); 886 TRANSFORM_COEFF(cplcoeff, l15_quantizers[get_bits(gb, 4)], exps[start], scale_factors);
879 /*if (gcode > 14)
880 return -1;*/
881 m->bits += 4;
882 TRANSFORM_COEFF(cplcoeff, l15_quantizers[gcode], exps[start], scale_factors);
883 break; 887 break;
884 888
885 default: 889 default:
886 m->bits += qntztab[bap[bin]];
887 TRANSFORM_COEFF(cplcoeff, get_bits(gb, qntztab[tbap]) << (16 - qntztab[tbap]), 890 TRANSFORM_COEFF(cplcoeff, get_bits(gb, qntztab[tbap]) << (16 - qntztab[tbap]),
888 exps[bin], scale_factors); 891 exps[start], scale_factors);
889 } 892 }
890 for (ch = 0; ch < ctx->nfchans; ch++) 893 for (ch = 0; ch < ctx->nfchans; ch++)
891 if ((ctx->chincpl >> ch) & 1) 894 if ((ctx->chincpl >> ch) & 1)
892 ctx->transform_coeffs[ch][bin] = cplcoeff * cplcos[ch]; 895 ctx->transform_coeffs[ch + 1][start] = cplcoeff * cplcos[ch];
893 start++; 896 start++;
894 } 897 }
895 } 898 }
896 899
897 bits_consumed = m->bits - bits_consumed;
898 av_log(NULL, AV_LOG_INFO, "\tbits consumed by coupling = %d\n", bits_consumed);
899
900 return 0; 900 return 0;
901 } 901 }
902 902
903 /* Get the transform coefficients for particular channel */ 903 /* Get the transform coefficients for particular channel */
904 static int get_transform_coeffs_ch(uint8_t *exps, uint8_t *bap, float chcoeff, 904 static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
905 float *coeffs, int start, int end, int dith_flag, GetBitContext *gb, 905 {
906 dither_state *state, mant_groups *m) 906 GetBitContext *gb = &ctx->gb;
907 { 907 int i, gcode, tbap, dithflag, end;
908 int i; 908 uint8_t *exps;
909 int gcode; 909 uint8_t *bap;
910 int tbap; 910 float *coeffs;
911 float factors[25]; 911 float factors[25];
912 912
913 int bits_consumed = m->bits;
914
915 for (i = 0; i < 25; i++) 913 for (i = 0; i < 25; i++)
916 factors[i] = scale_factors[i] * chcoeff; 914 factors[i] = scale_factors[i] * ctx->chcoeffs[ch_index];
917 915
918 for (i = start; i < end; i++) { 916 if (ch_index != -1) { /* fbw channels */
917 dithflag = (ctx->dithflag >> ch_index) & 1;
918 exps = ctx->dexps[ch_index];
919 bap = ctx->bap[ch_index];
920 coeffs = ctx->transform_coeffs[ch_index + 1];
921 end = ctx->endmant[ch_index];
922 } else if (ch_index == -1) {
923 dithflag = 0;
924 exps = ctx->dlfeexps;
925 bap = ctx->lfebap;
926 coeffs = ctx->transform_coeffs[0];
927 end = 7;
928 }
929
930
931 for (i = 0; i < end; i++) {
919 tbap = bap[i]; 932 tbap = bap[i];
920 switch (tbap) { 933 switch (tbap) {
921 case 0: 934 case 0:
922 if (!dith_flag) { 935 if (dithflag) {
923 coeffs[i] = 0; 936 coeffs[i] = 0;
924 continue; 937 continue;
925 } 938 }
926 else { 939 else {
927 TRANSFORM_COEFF(coeffs[i], dither_int16(state), exps[i], factors); 940 TRANSFORM_COEFF(coeffs[i], dither_int16(&ctx->dith_state), exps[i], factors);
928 coeffs[i] *= LEVEL_PLUS_3DB; 941 coeffs[i] *= LEVEL_PLUS_3DB;
929 continue; 942 continue;
930 } 943 }
931 944
932 case 1: 945 case 1:
933 if (m->l3ptr > 2) { 946 if (m->l3ptr > 2) {
934 gcode = get_bits(gb, 5); 947 gcode = get_bits(gb, 5);
935 /*if (gcode > 26)
936 return -1;*/
937 m->l3_quantizers[0] = l3_quantizers_1[gcode]; 948 m->l3_quantizers[0] = l3_quantizers_1[gcode];
938 m->l3_quantizers[1] = l3_quantizers_2[gcode]; 949 m->l3_quantizers[1] = l3_quantizers_2[gcode];
939 m->l3_quantizers[2] = l3_quantizers_3[gcode]; 950 m->l3_quantizers[2] = l3_quantizers_3[gcode];
940 m->l3ptr = 0; 951 m->l3ptr = 0;
941 m->bits += 5;
942 } 952 }
943 TRANSFORM_COEFF(coeffs[i], m->l3_quantizers[m->l3ptr++], exps[i], factors); 953 TRANSFORM_COEFF(coeffs[i], m->l3_quantizers[m->l3ptr++], exps[i], factors);
944 continue; 954 continue;
945 955
946 case 2: 956 case 2:
947 if (m->l5ptr > 2) { 957 if (m->l5ptr > 2) {
948 gcode = get_bits(gb, 7); 958 gcode = get_bits(gb, 7);
949 /*if (gcode > 124)
950 return -1;*/
951 m->l5_quantizers[0] = l5_quantizers_1[gcode]; 959 m->l5_quantizers[0] = l5_quantizers_1[gcode];
952 m->l5_quantizers[1] = l5_quantizers_2[gcode]; 960 m->l5_quantizers[1] = l5_quantizers_2[gcode];
953 m->l5_quantizers[2] = l5_quantizers_3[gcode]; 961 m->l5_quantizers[2] = l5_quantizers_3[gcode];
954 m->l5ptr = 0; 962 m->l5ptr = 0;
955 m->bits += 7;
956 } 963 }
957 TRANSFORM_COEFF(coeffs[i], m->l5_quantizers[m->l5ptr++], exps[i], factors); 964 TRANSFORM_COEFF(coeffs[i], m->l5_quantizers[m->l5ptr++], exps[i], factors);
958 continue; 965 continue;
959 966
960 case 3: 967 case 3:
961 gcode = get_bits(gb, 3); 968 TRANSFORM_COEFF(coeffs[i], l7_quantizers[get_bits(gb, 3)], exps[i], factors);
962 /*if (gcode > 6)
963 return -1; */
964 m->bits += 3;
965 TRANSFORM_COEFF(coeffs[i], l7_quantizers[gcode], exps[i], factors);
966 continue; 969 continue;
967 970
968 case 4: 971 case 4:
969 if (m->l11ptr > 1) { 972 if (m->l11ptr > 1) {
970 gcode = get_bits(gb, 7); 973 gcode = get_bits(gb, 7);
971 /*if (gcode > 120)
972 return -1;*/
973 m->l11_quantizers[0] = l11_quantizers_1[gcode]; 974 m->l11_quantizers[0] = l11_quantizers_1[gcode];
974 m->l11_quantizers[1] = l11_quantizers_2[gcode]; 975 m->l11_quantizers[1] = l11_quantizers_2[gcode];
975 m->l11ptr = 0; 976 m->l11ptr = 0;
976 m->bits += 7;
977 } 977 }
978 TRANSFORM_COEFF(coeffs[i], m->l11_quantizers[m->l11ptr++], exps[i], factors); 978 TRANSFORM_COEFF(coeffs[i], m->l11_quantizers[m->l11ptr++], exps[i], factors);
979 continue; 979 continue;
980 980
981 case 5: 981 case 5:
982 gcode = get_bits(gb, 4); 982 TRANSFORM_COEFF(coeffs[i], l15_quantizers[get_bits(gb, 4)], exps[i], factors);
983 /*if (gcode > 14)
984 return -1;*/
985 m->bits += 4;
986 TRANSFORM_COEFF(coeffs[i], l15_quantizers[gcode], exps[i], factors);
987 continue; 983 continue;
988 984
989 default: 985 default:
990 m->bits += qntztab[bap[i]];
991 TRANSFORM_COEFF(coeffs[i], get_bits(gb, qntztab[tbap]) << (16 - qntztab[tbap]), exps[i], factors); 986 TRANSFORM_COEFF(coeffs[i], get_bits(gb, qntztab[tbap]) << (16 - qntztab[tbap]), exps[i], factors);
992 continue; 987 continue;
993 } 988 }
994 } 989 }
995 990
996 bits_consumed = m->bits - bits_consumed;
997 av_log(NULL, AV_LOG_INFO, "\tbits consumed by channel = %d\n", bits_consumed);
998
999 return 0; 991 return 0;
1000 } 992 }
1001 993
1002 static int get_transform_coeffs(AC3DecodeContext * ctx) 994 static int get_transform_coeffs(AC3DecodeContext * ctx)
1003 { 995 {
1004 int i, end; 996 int i, end;
1005 int got_cplchan = 0; 997 int got_cplchan = 0;
1006 int dithflag = 0;
1007 mant_groups m; 998 mant_groups m;
1008 999
1009 m.l3ptr = m.l5ptr = m.l11ptr = 3; 1000 m.l3ptr = m.l5ptr = m.l11ptr = 3;
1010 m.bits = 0;
1011 1001
1012 for (i = 0; i < ctx->nfchans; i++) { 1002 for (i = 0; i < ctx->nfchans; i++) {
1013 dithflag = (ctx->dithflag >> i) & 1;
1014 /* transform coefficients for individual channel */ 1003 /* transform coefficients for individual channel */
1015 if (get_transform_coeffs_ch(ctx->dexps[i], ctx->bap[i], ctx->chcoeffs[i], ctx->transform_coeffs[i + 1], 1004 if (get_transform_coeffs_ch(ctx, i, &m))
1016 0, ctx->endmant[i], dithflag, &ctx->gb, &ctx->dith_state, &m))
1017 return -1; 1005 return -1;
1018 /* tranform coefficients for coupling channels */ 1006 /* tranform coefficients for coupling channels */
1019 if ((ctx->chincpl >> i) & 1) { 1007 if ((ctx->chincpl >> i) & 1) {
1020 if (!got_cplchan) { 1008 if (!got_cplchan) {
1021 if (get_transform_coeffs_cpling(ctx, &m)) { 1009 if (get_transform_coeffs_cpling(ctx, &m)) {
1030 do 1018 do
1031 ctx->transform_coeffs[i + 1][end] = 0; 1019 ctx->transform_coeffs[i + 1][end] = 0;
1032 while(++end < 256); 1020 while(++end < 256);
1033 } 1021 }
1034 if (ctx->lfeon) { 1022 if (ctx->lfeon) {
1035 if (get_transform_coeffs_ch(ctx->dlfeexps, ctx->lfebap, 1.0f, ctx->transform_coeffs[0], 0, 7, 0, &ctx->gb, &ctx->dith_state, &m)) 1023 if (get_transform_coeffs_ch(ctx, -1, &m))
1036 return -1; 1024 return -1;
1037 for (i = 7; i < 256; i++) { 1025 for (i = 7; i < 256; i++) {
1038 ctx->transform_coeffs[0][i] = 0; 1026 ctx->transform_coeffs[0][i] = 0;
1039 } 1027 }
1040 } 1028 }
1041
1042 av_log(NULL, AV_LOG_INFO, "bits consumed by get_transform_coeffs = %d\n", m.bits);
1043 1029
1044 return 0; 1030 return 0;
1045 } 1031 }
1046 1032
1047 static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end) 1033 static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
1057 } 1043 }
1058 } 1044 }
1059 1045
1060 static void do_rematrixing(AC3DecodeContext *ctx) 1046 static void do_rematrixing(AC3DecodeContext *ctx)
1061 { 1047 {
1062 uint8_t bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61; 1048 int bnd1 = 13, bnd2 = 25, bnd3 = 37, bnd4 = 61;
1063 uint8_t bndend; 1049 int end, bndend;
1064 1050
1065 bndend = FFMIN(ctx->endmant[0], ctx->endmant[1]); 1051 end = FFMIN(ctx->endmant[0], ctx->endmant[1]);
1052
1066 if (ctx->rematflg & 1) 1053 if (ctx->rematflg & 1)
1067 do_rematrixing1(ctx, bnd1, bnd2); 1054 do_rematrixing1(ctx, bnd1, bnd2);
1055
1068 if (ctx->rematflg & 2) 1056 if (ctx->rematflg & 2)
1069 do_rematrixing1(ctx, bnd2, bnd3); 1057 do_rematrixing1(ctx, bnd2, bnd3);
1070 if (ctx->rematflg & 4) { 1058
1071 if (ctx->cplbegf > 0 && ctx->cplbegf <= 2 && (ctx->chincpl)) 1059 bndend = bnd4;
1060 if (bndend > end) {
1061 bndend = end;
1062 if (ctx->rematflg & 4)
1072 do_rematrixing1(ctx, bnd3, bndend); 1063 do_rematrixing1(ctx, bnd3, bndend);
1073 else { 1064 } else {
1065 if (ctx->rematflg & 4)
1074 do_rematrixing1(ctx, bnd3, bnd4); 1066 do_rematrixing1(ctx, bnd3, bnd4);
1075 if (ctx->rematflg & 8) 1067 if (ctx->rematflg & 8)
1076 do_rematrixing1(ctx, bnd4, bndend); 1068 do_rematrixing1(ctx, bnd4, end);
1077 }
1078 } 1069 }
1079 } 1070 }
1080 1071
1081 static void get_downmix_coeffs(AC3DecodeContext *ctx) 1072 static void get_downmix_coeffs(AC3DecodeContext *ctx)
1082 { 1073 {
1116 case AC3_INPUT_3F: 1107 case AC3_INPUT_3F:
1117 switch (to) { 1108 switch (to) {
1118 case AC3_OUTPUT_MONO: 1109 case AC3_OUTPUT_MONO:
1119 ctx->chcoeffs[0] *= LEVEL_MINUS_3DB; 1110 ctx->chcoeffs[0] *= LEVEL_MINUS_3DB;
1120 ctx->chcoeffs[2] *= LEVEL_MINUS_3DB; 1111 ctx->chcoeffs[2] *= LEVEL_MINUS_3DB;
1121 ctx->chcoeffs[1] *= clev * LEVEL_PLUS_3DB; 1112 ctx->chcoeffs[1] *= (clev * LEVEL_PLUS_3DB);
1122 break; 1113 break;
1123 case AC3_OUTPUT_STEREO: 1114 case AC3_OUTPUT_STEREO:
1124 ctx->chcoeffs[1] *= clev; 1115 ctx->chcoeffs[1] *= clev;
1125 break; 1116 break;
1126 } 1117 }
1559 for (k = 0; k < n8; k++) { 1550 for (k = 0; k < n8; k++) {
1560 CMUL(z1[k].re, z1[k].im, x1[n4 - 2 * k - 1], x1[2 * k], x_cos2[k], x_sin2[k]); 1551 CMUL(z1[k].re, z1[k].im, x1[n4 - 2 * k - 1], x1[2 * k], x_cos2[k], x_sin2[k]);
1561 CMUL(z2[k].re, z2[k].im, x2[n4 - 2 * k - 1], x2[2 * k], x_cos2[k], x_sin2[k]); 1552 CMUL(z2[k].re, z2[k].im, x2[n4 - 2 * k - 1], x2[2 * k], x_cos2[k], x_sin2[k]);
1562 } 1553 }
1563 1554
1564 /* Permutation needed before calling ff_fft_calc. */ 1555 /* N/8 pointe complex IFFT. */
1565 ff_fft_permute(fft_ctx, z1); 1556 ff_fft_permute(fft_ctx, z1);
1557 ff_fft_calc(fft_ctx, z1);
1566 ff_fft_permute(fft_ctx, z2); 1558 ff_fft_permute(fft_ctx, z2);
1567
1568 /* N/8 pointe complex IFFT. */
1569 ff_fft_calc(fft_ctx, z1);
1570 ff_fft_calc(fft_ctx, z2); 1559 ff_fft_calc(fft_ctx, z2);
1560
1571 1561
1572 /* Post IFFT Complex Multiply Step. */ 1562 /* Post IFFT Complex Multiply Step. */
1573 for (k = 0; k < n8; k++) { 1563 for (k = 0; k < n8; k++) {
1574 CMUL(z1[k].re, z1[k].im, z1[k].re, z1[k].im, x_cos2[k], x_sin2[k]); 1564 CMUL(z1[k].re, z1[k].im, z1[k].re, z1[k].im, x_cos2[k], x_sin2[k]);
1575 CMUL(z2[k].re, z2[k].im, z2[k].re, z2[k].im, x_cos2[k], x_sin2[k]); 1565 CMUL(z2[k].re, z2[k].im, z2[k].re, z2[k].im, x_cos2[k], x_sin2[k]);
1644 1634
1645 if (ctx->blkoutput & AC3_OUTPUT_LFEON) { 1635 if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
1646 do_imdct_512(&ctx->fft_128, ctx->transform_coeffs[0], ctx->output[0], 1636 do_imdct_512(&ctx->fft_128, ctx->transform_coeffs[0], ctx->output[0],
1647 ctx->delay[0], ctx->tmp_imdct, ctx->tmp_output); 1637 ctx->delay[0], ctx->tmp_imdct, ctx->tmp_output);
1648 } 1638 }
1649 for (i = 0; i < ctx->nfchans + 1; i++) { 1639 for (i = 0; i < ctx->nfchans; i++) {
1650 if (!(((ctx->blksw) >> i) & 1)) { 1640 if (!((ctx->blksw >> i) & 1)) {
1651 do_imdct_512(&ctx->fft_128, ctx->transform_coeffs[i + 1], ctx->output[i + 1], 1641 do_imdct_512(&ctx->fft_128, ctx->transform_coeffs[i + 1], ctx->output[i + 1],
1652 ctx->delay[i + 1], ctx->tmp_imdct, ctx->tmp_output); 1642 ctx->delay[i + 1], ctx->tmp_imdct, ctx->tmp_output);
1653 } else { 1643 } else {
1644 av_log(NULL, AV_LOG_INFO, "block switching enabled.\n");
1654 do_imdct_256(&ctx->fft_64, ctx->transform_coeffs[i + 1], ctx->output[i + 1], 1645 do_imdct_256(&ctx->fft_64, ctx->transform_coeffs[i + 1], ctx->output[i + 1],
1655 ctx->delay[i + 1], ctx->tmp_imdct, ctx->tmp_output); 1646 ctx->delay[i + 1], ctx->tmp_imdct, ctx->tmp_output);
1656 } 1647 }
1657 } 1648 }
1658 } 1649 }
1662 int nfchans = ctx->nfchans; 1653 int nfchans = ctx->nfchans;
1663 int acmod = ctx->acmod; 1654 int acmod = ctx->acmod;
1664 int i, bnd, rbnd, seg, grpsize; 1655 int i, bnd, rbnd, seg, grpsize;
1665 GetBitContext *gb = &ctx->gb; 1656 GetBitContext *gb = &ctx->gb;
1666 int bit_alloc_flags = 0; 1657 int bit_alloc_flags = 0;
1667 float drange, tmpco; 1658 float drange;
1668 uint8_t *dexps; 1659 uint8_t *dexps;
1669 int mstrcplco, cplcoexp, cplcomant, sbnd, cplbndstrc; 1660 int mstrcplco, cplcoexp, cplcomant;
1670 int dynrng, chbwcod, ngrps, cplabsexp, skipl; 1661 int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1671 1662
1672 for (i = 0; i < 5; i++) 1663 for (i = 0; i < 5; i++)
1673 ctx->chcoeffs[i] = 1.0; 1664 ctx->chcoeffs[i] = 1.0;
1674 1665
1693 1684
1694 get_downmix_coeffs(ctx); 1685 get_downmix_coeffs(ctx);
1695 1686
1696 if (get_bits1(gb)) { /* coupling strategy */ 1687 if (get_bits1(gb)) { /* coupling strategy */
1697 ctx->cplinu = get_bits1(gb); 1688 ctx->cplinu = get_bits1(gb);
1689 ctx->cplbndstrc = 0;
1690 ctx->chincpl = 0;
1698 if (ctx->cplinu) { /* coupling in use */ 1691 if (ctx->cplinu) { /* coupling in use */
1699 ctx->chincpl = 0;
1700 for (i = 0; i < nfchans; i++) 1692 for (i = 0; i < nfchans; i++)
1701 ctx->chincpl |= get_bits1(gb) << i; 1693 ctx->chincpl |= get_bits1(gb) << i;
1702
1703 if (acmod == 0x00 || acmod == 0x01) //atleast two shared channels required
1704 return -1;
1705 1694
1706 if (acmod == 0x02) 1695 if (acmod == 0x02)
1707 ctx->phsflginu = get_bits1(gb); //phase flag in use 1696 ctx->phsflginu = get_bits1(gb); //phase flag in use
1708 1697
1709 ctx->cplbegf = get_bits(gb, 4); 1698 ctx->cplbegf = get_bits(gb, 4);
1715 } 1704 }
1716 1705
1717 ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf; 1706 ctx->ncplbnd = ctx->ncplsubnd = 3 + ctx->cplendf - ctx->cplbegf;
1718 ctx->cplstrtmant = ctx->cplbegf * 12 + 37; 1707 ctx->cplstrtmant = ctx->cplbegf * 12 + 37;
1719 ctx->cplendmant = ctx->cplendf * 12 + 73; 1708 ctx->cplendmant = ctx->cplendf * 12 + 73;
1720 ctx->cplbndstrc = 0;
1721 for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */ 1709 for (i = 0; i < ctx->ncplsubnd - 1; i++) /* coupling band structure */
1722 if (get_bits1(gb)) { 1710 if (get_bits1(gb)) {
1723 ctx->cplbndstrc |= 1 << i; 1711 ctx->cplbndstrc |= 1 << i;
1724 ctx->ncplbnd--; 1712 ctx->ncplbnd--;
1725 } 1713 }
1730 ctx->cplcoe = 0; 1718 ctx->cplcoe = 0;
1731 1719
1732 for (i = 0; i < nfchans; i++) 1720 for (i = 0; i < nfchans; i++)
1733 if ((ctx->chincpl) >> i & 1) 1721 if ((ctx->chincpl) >> i & 1)
1734 if (get_bits1(gb)) { /* coupling co-ordinates */ 1722 if (get_bits1(gb)) { /* coupling co-ordinates */
1735 ctx->cplcoe = 1; 1723 ctx->cplcoe |= 1 << i;
1736 mstrcplco = 3 * get_bits(gb, 2); 1724 mstrcplco = 3 * get_bits(gb, 2);
1737 cplbndstrc = ctx->cplbndstrc;
1738 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) { 1725 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
1739 cplcoexp = get_bits(gb, 4); 1726 cplcoexp = get_bits(gb, 4);
1740 cplcomant = get_bits(gb, 4); 1727 cplcomant = get_bits(gb, 4);
1741 if (cplcoexp == 15) 1728 if (cplcoexp == 15)
1742 cplcomant <<= 14; 1729 cplcomant <<= 14;
1744 cplcomant = (cplcomant | 0x10) << 13; 1731 cplcomant = (cplcomant | 0x10) << 13;
1745 ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco]; 1732 ctx->cplco[i][bnd] = cplcomant * scale_factors[cplcoexp + mstrcplco];
1746 } 1733 }
1747 } 1734 }
1748 1735
1749 if (acmod == 0x02 && ctx->phsflginu && ctx->cplcoe) 1736 if (acmod == 0x02 && ctx->phsflginu && (ctx->cplcoe & 1 || ctx->cplcoe & 2))
1750 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) 1737 for (bnd = 0; bnd < ctx->ncplbnd; bnd++)
1751 if (get_bits1(gb)) 1738 if (get_bits1(gb))
1752 ctx->cplco[1][bnd] = -ctx->cplco[1][bnd]; 1739 ctx->cplco[1][bnd] = -ctx->cplco[1][bnd];
1753 } 1740 }
1754 1741
1787 if (chbwcod > 60) { 1774 if (chbwcod > 60) {
1788 av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod); 1775 av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
1789 return -1; 1776 return -1;
1790 } 1777 }
1791 ctx->endmant[i] = chbwcod * 3 + 73; 1778 ctx->endmant[i] = chbwcod * 3 + 73;
1792 av_log(NULL, AV_LOG_INFO, "i = %d \t chbwcod = %d \t endmant = %d\n", i, chbwcod, ctx->endmant[i]);
1793 } 1779 }
1794 } 1780 }
1795 1781
1796 if (ctx->cplexpstr != AC3_EXPSTR_REUSE) {/* coupling exponents */ 1782 if (ctx->cplexpstr != AC3_EXPSTR_REUSE) {/* coupling exponents */
1797 bit_alloc_flags |= 64; 1783 bit_alloc_flags = 64;
1798 cplabsexp = get_bits(gb, 4) << 1; 1784 cplabsexp = get_bits(gb, 4) << 1;
1799 ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1)); 1785 ngrps = (ctx->cplendmant - ctx->cplstrtmant) / (3 << (ctx->cplexpstr - 1));
1800 if (decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant)) { 1786 if (decode_exponents(gb, ctx->cplexpstr, ngrps, cplabsexp, ctx->dcplexps + ctx->cplstrtmant)) {
1801 av_log(NULL, AV_LOG_ERROR, "error decoding coupling exponents\n"); 1787 av_log(NULL, AV_LOG_ERROR, "error decoding coupling exponents\n");
1802 return -1; 1788 return -1;
1825 return -1; 1811 return -1;
1826 } 1812 }
1827 } 1813 }
1828 1814
1829 if (get_bits1(gb)) { /* bit allocation information */ 1815 if (get_bits1(gb)) { /* bit allocation information */
1830 bit_alloc_flags |= 127; 1816 bit_alloc_flags = 127;
1831 ctx->sdcycod = get_bits(gb, 2); 1817 ctx->sdcycod = get_bits(gb, 2);
1832 ctx->fdcycod = get_bits(gb, 2); 1818 ctx->fdcycod = get_bits(gb, 2);
1833 ctx->sgaincod = get_bits(gb, 2); 1819 ctx->sgaincod = get_bits(gb, 2);
1834 ctx->dbpbcod = get_bits(gb, 2); 1820 ctx->dbpbcod = get_bits(gb, 2);
1835 ctx->floorcod = get_bits(gb, 3); 1821 ctx->floorcod = get_bits(gb, 3);
1836 } 1822 }
1837 1823
1838 if (get_bits1(gb)) { /* snroffset */ 1824 if (get_bits1(gb)) { /* snroffset */
1839 bit_alloc_flags |= 127; 1825 bit_alloc_flags = 127;
1840 ctx->csnroffst = get_bits(gb, 6); 1826 ctx->csnroffst = get_bits(gb, 6);
1841 if (ctx->cplinu) { /* couling fine snr offset and fast gain code */ 1827 if (ctx->cplinu) { /* couling fine snr offset and fast gain code */
1842 ctx->cplfsnroffst = get_bits(gb, 4); 1828 ctx->cplfsnroffst = get_bits(gb, 4);
1843 ctx->cplfgaincod = get_bits(gb, 3); 1829 ctx->cplfgaincod = get_bits(gb, 3);
1844 } 1830 }
1850 ctx->lfefsnroffst = get_bits(gb, 4); 1836 ctx->lfefsnroffst = get_bits(gb, 4);
1851 ctx->lfefgaincod = get_bits(gb, 3); 1837 ctx->lfefgaincod = get_bits(gb, 3);
1852 } 1838 }
1853 } 1839 }
1854 1840
1855 ctx->cplfleak = 0;
1856 ctx->cplsleak = 0;
1857 if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */ 1841 if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
1858 bit_alloc_flags |= 64; 1842 bit_alloc_flags |= 64;
1859 ctx->cplfleak = get_bits(gb, 3); 1843 ctx->cplfleak = get_bits(gb, 3);
1860 ctx->cplsleak = get_bits(gb, 3); 1844 ctx->cplsleak = get_bits(gb, 3);
1861 } 1845 }
1862 1846
1863 if (get_bits1(gb)) { /* delta bit allocation information */ 1847 if (get_bits1(gb)) { /* delta bit allocation information */
1864 bit_alloc_flags |= 127; 1848 bit_alloc_flags = 127;
1865 1849
1866 if (ctx->cplinu) { 1850 if (ctx->cplinu) {
1867 ctx->cpldeltbae = get_bits(gb, 2); 1851 ctx->cpldeltbae = get_bits(gb, 2);
1868 if (ctx->cpldeltbae == AC3_DBASTR_RESERVED) { 1852 if (ctx->cpldeltbae == AC3_DBASTR_RESERVED) {
1869 av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n"); 1853 av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
1898 ctx->deltba[i][seg] = get_bits(gb, 3); 1882 ctx->deltba[i][seg] = get_bits(gb, 3);
1899 } 1883 }
1900 } 1884 }
1901 } 1885 }
1902 1886
1903 do_bit_allocation (ctx, bit_alloc_flags); /* perform the bit allocation */ 1887 if (bit_alloc_flags) {
1888 if (is_snr_offsets_zero(ctx)) {
1889 memset(ctx->cplbap, 0, sizeof (ctx->cplbap));
1890 memset(ctx->lfebap, 0, sizeof (ctx->lfebap));
1891 for (i = 0; i < nfchans; i++)
1892 memset(ctx->bap[i], 0, sizeof(ctx->bap[i]));
1893 } else {
1894 if (ctx->chincpl && (bit_alloc_flags & 64))
1895 do_bit_allocation(ctx, 5);
1896 for (i = 0; i < nfchans; i++)
1897 if ((bit_alloc_flags >> i) & 1)
1898 do_bit_allocation(ctx, i);
1899 if (ctx->lfeon && (bit_alloc_flags & 32))
1900 do_bit_allocation(ctx, 6);
1901 }
1902 }
1904 1903
1905 if (get_bits1(gb)) { /* unused dummy data */ 1904 if (get_bits1(gb)) { /* unused dummy data */
1906 skipl = get_bits(gb, 9); 1905 skipl = get_bits(gb, 9);
1907 while(skipl--) 1906 while(skipl--)
1908 skip_bits(gb, 8); 1907 skip_bits(gb, 8);
1916 } 1915 }
1917 /*for (i = 0; i < nfchans; i++) 1916 /*for (i = 0; i < nfchans; i++)
1918 dump_floats("channel transform coefficients", 10, ctx->transform_coeffs[i + 1], BLOCK_SIZE);*/ 1917 dump_floats("channel transform coefficients", 10, ctx->transform_coeffs[i + 1], BLOCK_SIZE);*/
1919 1918
1920 /* recover coefficients if rematrixing is in use */ 1919 /* recover coefficients if rematrixing is in use */
1921 if (ctx->rematflg) 1920 if (ctx->rematstr)
1922 do_rematrixing(ctx); 1921 do_rematrixing(ctx);
1923 1922
1924 do_imdct(ctx); 1923 do_imdct(ctx);
1925 /*for(i = 0; i < nfchans; i++) 1924 /*for(i = 0; i < nfchans; i++)
1926 dump_floats("channel output", 10, ctx->output[i + 1], BLOCK_SIZE);*/ 1925 dump_floats("channel output", 10, ctx->output[i + 1], BLOCK_SIZE);*/