comparison ac3dec.c @ 5476:e50f0d583a36 libavcodec

consolidate decoding of lfe and coupling channels with decoding of full-bandwidth channels.
author jbr
date Sat, 04 Aug 2007 19:19:48 +0000
parents aedc8e0ade8c
children ec1f268792cd
comparison
equal deleted inserted replaced
5475:aedc8e0ade8c 5476:e50f0d583a36
83 static const float clevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB, 83 static const float clevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB,
84 LEVEL_MINUS_6DB, LEVEL_MINUS_4POINT5DB }; 84 LEVEL_MINUS_6DB, LEVEL_MINUS_4POINT5DB };
85 85
86 static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB }; 86 static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB };
87 87
88 /* override ac3.h to include coupling channel */
89 #undef AC3_MAX_CHANNELS
90 #define AC3_MAX_CHANNELS 7
91 #define CPL_CH 0
92
88 #define AC3_OUTPUT_LFEON 8 93 #define AC3_OUTPUT_LFEON 8
89 94
90 typedef struct { 95 typedef struct {
91 int acmod; 96 int acmod;
92 int cmixlev; 97 int cmixlev;
101 int phsflginu; 106 int phsflginu;
102 int cplbndstrc[18]; 107 int cplbndstrc[18];
103 int rematstr; 108 int rematstr;
104 int nrematbnd; 109 int nrematbnd;
105 int rematflg[4]; 110 int rematflg[4];
106 int cplexpstr; 111 int expstr[AC3_MAX_CHANNELS];
107 int lfeexpstr; 112 int snroffst[AC3_MAX_CHANNELS];
108 int chexpstr[5]; 113 int fgain[AC3_MAX_CHANNELS];
109 int cplsnroffst; 114 int deltbae[AC3_MAX_CHANNELS];
110 int cplfgain; 115 int deltnseg[AC3_MAX_CHANNELS];
111 int snroffst[5]; 116 uint8_t deltoffst[AC3_MAX_CHANNELS][8];
112 int fgain[5]; 117 uint8_t deltlen[AC3_MAX_CHANNELS][8];
113 int lfesnroffst; 118 uint8_t deltba[AC3_MAX_CHANNELS][8];
114 int lfefgain;
115 int cpldeltbae;
116 int deltbae[5];
117 int cpldeltnseg;
118 uint8_t cpldeltoffst[8];
119 uint8_t cpldeltlen[8];
120 uint8_t cpldeltba[8];
121 int deltnseg[5];
122 uint8_t deltoffst[5][8];
123 uint8_t deltlen[5][8];
124 uint8_t deltba[5][8];
125 119
126 /* Derived Attributes. */ 120 /* Derived Attributes. */
127 int sampling_rate; 121 int sampling_rate;
128 int bit_rate; 122 int bit_rate;
129 int frame_size; 123 int frame_size;
130 124
131 int nchans; //number of total channels 125 int nchans; //number of total channels
132 int nfchans; //number of full-bandwidth channels 126 int nfchans; //number of full-bandwidth channels
133 int lfeon; //lfe channel in use 127 int lfeon; //lfe channel in use
128 int lfe_ch; ///< index of LFE channel
134 int output_mode; ///< output channel configuration 129 int output_mode; ///< output channel configuration
135 int out_channels; ///< number of output channels 130 int out_channels; ///< number of output channels
136 131
137 float dynrng; //dynamic range gain 132 float dynrng; //dynamic range gain
138 float dynrng2; //dynamic range gain for 1+1 mode 133 float dynrng2; //dynamic range gain for 1+1 mode
139 float cplco[5][18]; //coupling coordinates 134 float cplco[AC3_MAX_CHANNELS][18]; //coupling coordinates
140 int ncplbnd; //number of coupling bands 135 int ncplbnd; //number of coupling bands
141 int ncplsubnd; //number of coupling sub bands 136 int ncplsubnd; //number of coupling sub bands
142 int cplstrtmant; //coupling start mantissa 137 int startmant[AC3_MAX_CHANNELS]; ///< start frequency bin
143 int cplendmant; //coupling end mantissa 138 int endmant[AC3_MAX_CHANNELS]; //channel end mantissas
144 int endmant[5]; //channel end mantissas
145 AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters 139 AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
146 140
147 int8_t dcplexps[256]; //decoded coupling exponents 141 int8_t dexps[AC3_MAX_CHANNELS][256]; ///< decoded exponents
148 int8_t dexps[5][256]; //decoded fbw channel exponents 142 uint8_t bap[AC3_MAX_CHANNELS][256]; ///< bit allocation pointers
149 int8_t dlfeexps[256]; //decoded lfe channel exponents 143 int16_t psd[AC3_MAX_CHANNELS][256]; ///< scaled exponents
150 uint8_t cplbap[256]; //coupling bit allocation pointers 144 int16_t bndpsd[AC3_MAX_CHANNELS][50]; ///< interpolated exponents
151 uint8_t bap[5][256]; //fbw channel bit allocation pointers 145 int16_t mask[AC3_MAX_CHANNELS][50]; ///< masking curve values
152 uint8_t lfebap[256]; //lfe channel bit allocation pointers 146
153
154 float transform_coeffs_cpl[256];
155 DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]); //transform coefficients 147 DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]); //transform coefficients
156 148
157 /* For IMDCT. */ 149 /* For IMDCT. */
158 MDCTContext imdct_512; //for 512 sample imdct transform 150 MDCTContext imdct_512; //for 512 sample imdct transform
159 MDCTContext imdct_256; //for 256 sample imdct transform 151 MDCTContext imdct_256; //for 256 sample imdct transform
160 DSPContext dsp; //for optimization 152 DSPContext dsp; //for optimization
161 153
162 DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][256]); //output after imdct transform and windowing 154 DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS-1][256]); //output after imdct transform and windowing
163 DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][256]); //delay - added to the next block 155 DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS-1][256]); //delay - added to the next block
164 DECLARE_ALIGNED_16(float, tmp_imdct[256]); //temporary storage for imdct transform 156 DECLARE_ALIGNED_16(float, tmp_imdct[256]); //temporary storage for imdct transform
165 DECLARE_ALIGNED_16(float, tmp_output[512]); //temporary storage for output before windowing 157 DECLARE_ALIGNED_16(float, tmp_output[512]); //temporary storage for output before windowing
166 DECLARE_ALIGNED_16(float, window[256]); //window coefficients 158 DECLARE_ALIGNED_16(float, window[256]); //window coefficients
167 159
168 /* Miscellaneous. */ 160 /* Miscellaneous. */
299 ctx->bit_alloc_params.halfratecod = hdr.halfratecod; 291 ctx->bit_alloc_params.halfratecod = hdr.halfratecod;
300 ctx->sampling_rate = hdr.sample_rate; 292 ctx->sampling_rate = hdr.sample_rate;
301 ctx->bit_rate = hdr.bit_rate; 293 ctx->bit_rate = hdr.bit_rate;
302 ctx->nchans = hdr.channels; 294 ctx->nchans = hdr.channels;
303 ctx->nfchans = ctx->nchans - ctx->lfeon; 295 ctx->nfchans = ctx->nchans - ctx->lfeon;
296 ctx->lfe_ch = ctx->nfchans + 1;
304 ctx->frame_size = hdr.frame_size; 297 ctx->frame_size = hdr.frame_size;
305 298
306 /* set default output to all source channels */ 299 /* set default output to all source channels */
307 ctx->out_channels = ctx->nchans; 300 ctx->out_channels = ctx->nchans;
308 ctx->output_mode = ctx->acmod; 301 ctx->output_mode = ctx->acmod;
399 static void uncouple_channels(AC3DecodeContext *ctx) 392 static void uncouple_channels(AC3DecodeContext *ctx)
400 { 393 {
401 int i, j, ch, bnd, subbnd; 394 int i, j, ch, bnd, subbnd;
402 395
403 subbnd = -1; 396 subbnd = -1;
404 i = ctx->cplstrtmant; 397 i = ctx->startmant[CPL_CH];
405 for(bnd=0; bnd<ctx->ncplbnd; bnd++) { 398 for(bnd=0; bnd<ctx->ncplbnd; bnd++) {
406 do { 399 do {
407 subbnd++; 400 subbnd++;
408 for(j=0; j<12; j++) { 401 for(j=0; j<12; j++) {
409 for(ch=1; ch<=ctx->nfchans; ch++) { 402 for(ch=1; ch<=ctx->nfchans; ch++) {
410 if(ctx->chincpl[ch-1]) 403 if(ctx->chincpl[ch])
411 ctx->transform_coeffs[ch][i] = ctx->transform_coeffs_cpl[i] * ctx->cplco[ch-1][bnd] * 8.0f; 404 ctx->transform_coeffs[ch][i] = ctx->transform_coeffs[CPL_CH][i] * ctx->cplco[ch][bnd] * 8.0f;
412 } 405 }
413 i++; 406 i++;
414 } 407 }
415 } while(ctx->cplbndstrc[subbnd]); 408 } while(ctx->cplbndstrc[subbnd]);
416 } 409 }
432 int i, gcode, tbap, start, end; 425 int i, gcode, tbap, start, end;
433 uint8_t *exps; 426 uint8_t *exps;
434 uint8_t *bap; 427 uint8_t *bap;
435 float *coeffs; 428 float *coeffs;
436 429
437 if (ch_index >= 0) { /* fbw channels */
438 exps = ctx->dexps[ch_index]; 430 exps = ctx->dexps[ch_index];
439 bap = ctx->bap[ch_index]; 431 bap = ctx->bap[ch_index];
440 coeffs = ctx->transform_coeffs[ch_index + 1]; 432 coeffs = ctx->transform_coeffs[ch_index];
441 start = 0; 433 start = ctx->startmant[ch_index];
442 end = ctx->endmant[ch_index]; 434 end = ctx->endmant[ch_index];
443 } else if (ch_index == -1) {
444 exps = ctx->dlfeexps;
445 bap = ctx->lfebap;
446 coeffs = ctx->transform_coeffs[0];
447 start = 0;
448 end = 7;
449 } else {
450 exps = ctx->dcplexps;
451 bap = ctx->cplbap;
452 coeffs = ctx->transform_coeffs_cpl;
453 start = ctx->cplstrtmant;
454 end = ctx->cplendmant;
455 }
456 435
457 436
458 for (i = start; i < end; i++) { 437 for (i = start; i < end; i++) {
459 tbap = bap[i]; 438 tbap = bap[i];
460 switch (tbap) { 439 switch (tbap) {
521 int end=0; 500 int end=0;
522 float *coeffs; 501 float *coeffs;
523 uint8_t *bap; 502 uint8_t *bap;
524 503
525 for(ch=1; ch<=ctx->nfchans; ch++) { 504 for(ch=1; ch<=ctx->nfchans; ch++) {
526 if(!ctx->dithflag[ch-1]) { 505 if(!ctx->dithflag[ch]) {
527 coeffs = ctx->transform_coeffs[ch]; 506 coeffs = ctx->transform_coeffs[ch];
528 bap = ctx->bap[ch-1]; 507 bap = ctx->bap[ch];
529 if(ctx->chincpl[ch-1]) 508 if(ctx->chincpl[ch])
530 end = ctx->cplstrtmant; 509 end = ctx->startmant[CPL_CH];
531 else 510 else
532 end = ctx->endmant[ch-1]; 511 end = ctx->endmant[ch];
533 for(i=0; i<end; i++) { 512 for(i=0; i<end; i++) {
534 if(bap[i] == 0) 513 if(bap[i] == 0)
535 coeffs[i] = 0.0f; 514 coeffs[i] = 0.0f;
536 } 515 }
537 if(ctx->chincpl[ch-1]) { 516 if(ctx->chincpl[ch]) {
538 bap = ctx->cplbap; 517 bap = ctx->bap[CPL_CH];
539 for(; i<ctx->cplendmant; i++) { 518 for(; i<ctx->endmant[CPL_CH]; i++) {
540 if(bap[i] == 0) 519 if(bap[i] == 0)
541 coeffs[i] = 0.0f; 520 coeffs[i] = 0.0f;
542 } 521 }
543 } 522 }
544 } 523 }
549 * This function extracts the tranform coefficients form the ac3 bitstream. 528 * This function extracts the tranform coefficients form the ac3 bitstream.
550 * This function is called after bit allocation is performed. 529 * This function is called after bit allocation is performed.
551 */ 530 */
552 static int get_transform_coeffs(AC3DecodeContext * ctx) 531 static int get_transform_coeffs(AC3DecodeContext * ctx)
553 { 532 {
554 int ch, i, end; 533 int ch, end;
555 int got_cplchan = 0; 534 int got_cplchan = 0;
556 mant_groups m; 535 mant_groups m;
557 536
558 m.b1ptr = m.b2ptr = m.b4ptr = 3; 537 m.b1ptr = m.b2ptr = m.b4ptr = 3;
559 538
560 for (ch = 0; ch < ctx->nfchans; ch++) { 539 for (ch = 1; ch <= ctx->nchans; ch++) {
561 /* transform coefficients for individual channel */ 540 /* transform coefficients for individual channel */
562 if (get_transform_coeffs_ch(ctx, ch, &m)) 541 if (get_transform_coeffs_ch(ctx, ch, &m))
563 return -1; 542 return -1;
564 /* tranform coefficients for coupling channels */ 543 /* tranform coefficients for coupling channels */
565 if (ctx->chincpl[ch]) { 544 if (ctx->chincpl[ch]) {
566 if (!got_cplchan) { 545 if (!got_cplchan) {
567 if (get_transform_coeffs_ch(ctx, -2, &m)) { 546 if (get_transform_coeffs_ch(ctx, CPL_CH, &m)) {
568 av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n"); 547 av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
569 return -1; 548 return -1;
570 } 549 }
571 uncouple_channels(ctx); 550 uncouple_channels(ctx);
572 got_cplchan = 1; 551 got_cplchan = 1;
573 } 552 }
574 end = ctx->cplendmant; 553 end = ctx->endmant[CPL_CH];
575 } else { 554 } else {
576 end = ctx->endmant[ch]; 555 end = ctx->endmant[ch];
577 } 556 }
578 do 557 do
579 ctx->transform_coeffs[ch + 1][end] = 0; 558 ctx->transform_coeffs[ch][end] = 0;
580 while(++end < 256); 559 while(++end < 256);
581 }
582 if (ctx->lfeon) {
583 if (get_transform_coeffs_ch(ctx, -1, &m))
584 return -1;
585 for (i = 7; i < 256; i++) {
586 ctx->transform_coeffs[0][i] = 0;
587 }
588 } 560 }
589 561
590 /* if any channel doesn't use dithering, zero appropriate coefficients */ 562 /* if any channel doesn't use dithering, zero appropriate coefficients */
591 if(!ctx->dither_all) 563 if(!ctx->dither_all)
592 remove_dithering(ctx); 564 remove_dithering(ctx);
602 { 574 {
603 int bnd, i; 575 int bnd, i;
604 int end, bndend; 576 int end, bndend;
605 float tmp0, tmp1; 577 float tmp0, tmp1;
606 578
607 end = FFMIN(ctx->endmant[0], ctx->endmant[1]); 579 end = FFMIN(ctx->endmant[1], ctx->endmant[2]);
608 580
609 for(bnd=0; bnd<ctx->nrematbnd; bnd++) { 581 for(bnd=0; bnd<ctx->nrematbnd; bnd++) {
610 if(ctx->rematflg[bnd]) { 582 if(ctx->rematflg[bnd]) {
611 bndend = FFMIN(end, rematrix_band_tbl[bnd+1]); 583 bndend = FFMIN(end, rematrix_band_tbl[bnd+1]);
612 for(i=rematrix_band_tbl[bnd]; i<bndend; i++) { 584 for(i=rematrix_band_tbl[bnd]; i<bndend; i++) {
662 634
663 /* IMDCT Transform. */ 635 /* IMDCT Transform. */
664 static inline void do_imdct(AC3DecodeContext *ctx) 636 static inline void do_imdct(AC3DecodeContext *ctx)
665 { 637 {
666 int ch; 638 int ch;
667 639 int nchans;
668 if (ctx->output_mode & AC3_OUTPUT_LFEON) { 640
669 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output, 641 nchans = ctx->nfchans;
670 ctx->transform_coeffs[0], ctx->tmp_imdct); 642 if(ctx->output_mode & AC3_OUTPUT_LFEON)
671 ctx->dsp.vector_fmul_add_add(ctx->output[0], ctx->tmp_output, 643 nchans++;
672 ctx->window, ctx->delay[0], 384, 256, 1); 644
673 ctx->dsp.vector_fmul_reverse(ctx->delay[0], ctx->tmp_output+256, 645 for (ch=1; ch<=nchans; ch++) {
674 ctx->window, 256); 646 if (ctx->blksw[ch]) {
675 }
676 for (ch=1; ch<=ctx->nfchans; ch++) {
677 if (ctx->blksw[ch-1]) {
678 do_imdct_256(ctx, ch); 647 do_imdct_256(ctx, ch);
679 } else { 648 } else {
680 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output, 649 ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
681 ctx->transform_coeffs[ch], 650 ctx->transform_coeffs[ch],
682 ctx->tmp_imdct); 651 ctx->tmp_imdct);
683 } 652 }
684 ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output, 653 ctx->dsp.vector_fmul_add_add(ctx->output[ch-1], ctx->tmp_output,
685 ctx->window, ctx->delay[ch], 384, 256, 1); 654 ctx->window, ctx->delay[ch-1], 384, 256, 1);
686 ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256, 655 ctx->dsp.vector_fmul_reverse(ctx->delay[ch-1], ctx->tmp_output+256,
687 ctx->window, 256); 656 ctx->window, 256);
688 } 657 }
689 } 658 }
690 659
691 /* Parse the audio block from ac3 bitstream. 660 /* Parse the audio block from ac3 bitstream.
697 { 666 {
698 int nfchans = ctx->nfchans; 667 int nfchans = ctx->nfchans;
699 int acmod = ctx->acmod; 668 int acmod = ctx->acmod;
700 int i, bnd, seg, ch; 669 int i, bnd, seg, ch;
701 GetBitContext *gb = &ctx->gb; 670 GetBitContext *gb = &ctx->gb;
702 int bit_alloc_flags = 0; 671 uint8_t bit_alloc_stages[AC3_MAX_CHANNELS];
703 672
704 for (ch = 0; ch < nfchans; ch++) /*block switch flag */ 673 memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS);
674
675 for (ch = 1; ch <= nfchans; ch++) /*block switch flag */
705 ctx->blksw[ch] = get_bits1(gb); 676 ctx->blksw[ch] = get_bits1(gb);
706 677
707 ctx->dither_all = 1; 678 ctx->dither_all = 1;
708 for (ch = 0; ch < nfchans; ch++) { /* dithering flag */ 679 for (ch = 1; ch <= nfchans; ch++) { /* dithering flag */
709 ctx->dithflag[ch] = get_bits1(gb); 680 ctx->dithflag[ch] = get_bits1(gb);
710 if(!ctx->dithflag[ch]) 681 if(!ctx->dithflag[ch])
711 ctx->dither_all = 0; 682 ctx->dither_all = 0;
712 } 683 }
713 684
724 ctx->dynrng2 = 1.0; 695 ctx->dynrng2 = 1.0;
725 } 696 }
726 } 697 }
727 698
728 if (get_bits1(gb)) { /* coupling strategy */ 699 if (get_bits1(gb)) { /* coupling strategy */
700 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
729 ctx->cplinu = get_bits1(gb); 701 ctx->cplinu = get_bits1(gb);
730 if (ctx->cplinu) { /* coupling in use */ 702 if (ctx->cplinu) { /* coupling in use */
731 int cplbegf, cplendf; 703 int cplbegf, cplendf;
732 704
733 for (ch = 0; ch < nfchans; ch++) 705 for (ch = 1; ch <= nfchans; ch++)
734 ctx->chincpl[ch] = get_bits1(gb); 706 ctx->chincpl[ch] = get_bits1(gb);
735 707
736 if (acmod == AC3_ACMOD_STEREO) 708 if (acmod == AC3_ACMOD_STEREO)
737 ctx->phsflginu = get_bits1(gb); //phase flag in use 709 ctx->phsflginu = get_bits1(gb); //phase flag in use
738 710
743 av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", cplendf, cplbegf); 715 av_log(NULL, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", cplendf, cplbegf);
744 return -1; 716 return -1;
745 } 717 }
746 718
747 ctx->ncplbnd = ctx->ncplsubnd = 3 + cplendf - cplbegf; 719 ctx->ncplbnd = ctx->ncplsubnd = 3 + cplendf - cplbegf;
748 ctx->cplstrtmant = cplbegf * 12 + 37; 720 ctx->startmant[CPL_CH] = cplbegf * 12 + 37;
749 ctx->cplendmant = cplendf * 12 + 73; 721 ctx->endmant[CPL_CH] = cplendf * 12 + 73;
750 for (bnd = 0; bnd < ctx->ncplsubnd - 1; bnd++) { /* coupling band structure */ 722 for (bnd = 0; bnd < ctx->ncplsubnd - 1; bnd++) { /* coupling band structure */
751 if (get_bits1(gb)) { 723 if (get_bits1(gb)) {
752 ctx->cplbndstrc[bnd] = 1; 724 ctx->cplbndstrc[bnd] = 1;
753 ctx->ncplbnd--; 725 ctx->ncplbnd--;
754 } 726 }
755 } 727 }
756 } else { 728 } else {
757 for (ch = 0; ch < nfchans; ch++) 729 for (ch = 1; ch <= nfchans; ch++)
758 ctx->chincpl[ch] = 0; 730 ctx->chincpl[ch] = 0;
759 } 731 }
760 } 732 }
761 733
762 if (ctx->cplinu) { 734 if (ctx->cplinu) {
763 int cplcoe = 0; 735 int cplcoe = 0;
764 736
765 for (ch = 0; ch < nfchans; ch++) { 737 for (ch = 1; ch <= nfchans; ch++) {
766 if (ctx->chincpl[ch]) { 738 if (ctx->chincpl[ch]) {
767 if (get_bits1(gb)) { /* coupling co-ordinates */ 739 if (get_bits1(gb)) { /* coupling co-ordinates */
768 int mstrcplco, cplcoexp, cplcomant; 740 int mstrcplco, cplcoexp, cplcomant;
769 cplcoe = 1; 741 cplcoe = 1;
770 mstrcplco = 3 * get_bits(gb, 2); 742 mstrcplco = 3 * get_bits(gb, 2);
782 } 754 }
783 755
784 if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && cplcoe) { 756 if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && cplcoe) {
785 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) { 757 for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
786 if (get_bits1(gb)) 758 if (get_bits1(gb))
787 ctx->cplco[1][bnd] = -ctx->cplco[1][bnd]; 759 ctx->cplco[2][bnd] = -ctx->cplco[2][bnd];
788 } 760 }
789 } 761 }
790 } 762 }
791 763
792 if (acmod == AC3_ACMOD_STEREO) {/* rematrixing */ 764 if (acmod == AC3_ACMOD_STEREO) {/* rematrixing */
793 ctx->rematstr = get_bits1(gb); 765 ctx->rematstr = get_bits1(gb);
794 if (ctx->rematstr) { 766 if (ctx->rematstr) {
795 ctx->nrematbnd = 4; 767 ctx->nrematbnd = 4;
796 if(ctx->cplinu && ctx->cplstrtmant <= 61) 768 if(ctx->cplinu && ctx->startmant[CPL_CH] <= 61)
797 ctx->nrematbnd -= 1 + (ctx->cplstrtmant == 37); 769 ctx->nrematbnd -= 1 + (ctx->startmant[CPL_CH] == 37);
798 for(bnd=0; bnd<ctx->nrematbnd; bnd++) 770 for(bnd=0; bnd<ctx->nrematbnd; bnd++)
799 ctx->rematflg[bnd] = get_bits1(gb); 771 ctx->rematflg[bnd] = get_bits1(gb);
800 } 772 }
801 } 773 }
802 774
803 ctx->cplexpstr = EXP_REUSE; 775 ctx->expstr[CPL_CH] = EXP_REUSE;
804 ctx->lfeexpstr = EXP_REUSE; 776 ctx->expstr[ctx->lfe_ch] = EXP_REUSE;
805 if (ctx->cplinu) /* coupling exponent strategy */ 777 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {
806 ctx->cplexpstr = get_bits(gb, 2); 778 if(ch == ctx->lfe_ch)
807 for (ch = 0; ch < nfchans; ch++) /* channel exponent strategy */ 779 ctx->expstr[ch] = get_bits(gb, 1);
808 ctx->chexpstr[ch] = get_bits(gb, 2); 780 else
809 if (ctx->lfeon) /* lfe exponent strategy */ 781 ctx->expstr[ch] = get_bits(gb, 2);
810 ctx->lfeexpstr = get_bits1(gb); 782 if(ctx->expstr[ch] != EXP_REUSE)
811 783 bit_alloc_stages[ch] = 3;
812 for (ch = 0; ch < nfchans; ch++) { /* channel bandwidth code */ 784 }
813 if (ctx->chexpstr[ch] != EXP_REUSE) { 785
786 for (ch = 1; ch <= nfchans; ch++) { /* channel bandwidth code */
787 ctx->startmant[ch] = 0;
788 if (ctx->expstr[ch] != EXP_REUSE) {
789 int prev = ctx->endmant[ch];
814 if (ctx->chincpl[ch]) 790 if (ctx->chincpl[ch])
815 ctx->endmant[ch] = ctx->cplstrtmant; 791 ctx->endmant[ch] = ctx->startmant[CPL_CH];
816 else { 792 else {
817 int chbwcod = get_bits(gb, 6); 793 int chbwcod = get_bits(gb, 6);
818 if (chbwcod > 60) { 794 if (chbwcod > 60) {
819 av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod); 795 av_log(NULL, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);
820 return -1; 796 return -1;
821 } 797 }
822 ctx->endmant[ch] = chbwcod * 3 + 73; 798 ctx->endmant[ch] = chbwcod * 3 + 73;
823 } 799 }
824 } 800 if(blk > 0 && ctx->endmant[ch] != prev)
825 } 801 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
826 802 }
827 if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */ 803 }
828 int grpsize, ngrps, absexp; 804 ctx->startmant[ctx->lfe_ch] = 0;
829 bit_alloc_flags = 64; 805 ctx->endmant[ctx->lfe_ch] = 7;
830 grpsize = 3 << (ctx->cplexpstr - 1); 806
831 ngrps = (ctx->cplendmant - ctx->cplstrtmant) / grpsize; 807 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {
832 absexp = get_bits(gb, 4) << 1; 808 if (ctx->expstr[ch] != EXP_REUSE) {
833 decode_exponents(gb, ctx->cplexpstr, ngrps, absexp, &ctx->dcplexps[ctx->cplstrtmant]); 809 int grpsize, ngrps;
834 } 810 grpsize = 3 << (ctx->expstr[ch] - 1);
835 811 if(ch == CPL_CH)
836 for (ch = 0; ch < nfchans; ch++) { /* fbw channel exponents */ 812 ngrps = (ctx->endmant[ch] - ctx->startmant[ch]) / grpsize;
837 if (ctx->chexpstr[ch] != EXP_REUSE) { 813 else if(ch == ctx->lfe_ch)
838 int grpsize, ngrps, absexp; 814 ngrps = 2;
839 bit_alloc_flags |= 1 << ch; 815 else
840 grpsize = 3 << (ctx->chexpstr[ch] - 1);
841 ngrps = (ctx->endmant[ch] + grpsize - 4) / grpsize; 816 ngrps = (ctx->endmant[ch] + grpsize - 4) / grpsize;
842 absexp = ctx->dexps[ch][0] = get_bits(gb, 4); 817 ctx->dexps[ch][0] = get_bits(gb, 4) << !ch;
843 decode_exponents(gb, ctx->chexpstr[ch], ngrps, absexp, &ctx->dexps[ch][1]); 818 decode_exponents(gb, ctx->expstr[ch], ngrps, ctx->dexps[ch][0],
819 &ctx->dexps[ch][ctx->startmant[ch]+!!ch]);
820 if(ch != CPL_CH && ch != ctx->lfe_ch)
844 skip_bits(gb, 2); /* skip gainrng */ 821 skip_bits(gb, 2); /* skip gainrng */
845 } 822 }
846 } 823 }
847 824
848 if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
849 int ngrps, absexp;
850 bit_alloc_flags |= 32;
851 ngrps = 2;
852 absexp = ctx->dlfeexps[0] = get_bits(gb, 4);
853 decode_exponents(gb, ctx->lfeexpstr, ngrps, absexp, &ctx->dlfeexps[1]);
854 }
855
856 if (get_bits1(gb)) { /* bit allocation information */ 825 if (get_bits1(gb)) { /* bit allocation information */
857 bit_alloc_flags = 127;
858 ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)]; 826 ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)];
859 ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)]; 827 ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)];
860 ctx->bit_alloc_params.sgain = ff_sgaintab[get_bits(gb, 2)]; 828 ctx->bit_alloc_params.sgain = ff_sgaintab[get_bits(gb, 2)];
861 ctx->bit_alloc_params.dbknee = ff_dbkneetab[get_bits(gb, 2)]; 829 ctx->bit_alloc_params.dbknee = ff_dbkneetab[get_bits(gb, 2)];
862 ctx->bit_alloc_params.floor = ff_floortab[get_bits(gb, 3)]; 830 ctx->bit_alloc_params.floor = ff_floortab[get_bits(gb, 3)];
831 for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) {
832 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
833 }
863 } 834 }
864 835
865 if (get_bits1(gb)) { /* snroffset */ 836 if (get_bits1(gb)) { /* snroffset */
866 int csnr; 837 int csnr;
867 bit_alloc_flags = 127;
868 csnr = (get_bits(gb, 6) - 15) << 4; 838 csnr = (get_bits(gb, 6) - 15) << 4;
869 if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */ 839 for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) { /* snr offset and fast gain */
870 ctx->cplsnroffst = (csnr + get_bits(gb, 4)) << 2;
871 ctx->cplfgain = ff_fgaintab[get_bits(gb, 3)];
872 }
873 for (ch = 0; ch < nfchans; ch++) { /* channel fine snr offset and fast gain code */
874 ctx->snroffst[ch] = (csnr + get_bits(gb, 4)) << 2; 840 ctx->snroffst[ch] = (csnr + get_bits(gb, 4)) << 2;
875 ctx->fgain[ch] = ff_fgaintab[get_bits(gb, 3)]; 841 ctx->fgain[ch] = ff_fgaintab[get_bits(gb, 3)];
876 } 842 }
877 if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */ 843 memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
878 ctx->lfesnroffst = (csnr + get_bits(gb, 4)) << 2;
879 ctx->lfefgain = ff_fgaintab[get_bits(gb, 3)];
880 }
881 } 844 }
882 845
883 if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */ 846 if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
884 bit_alloc_flags |= 64;
885 ctx->bit_alloc_params.cplfleak = get_bits(gb, 3); 847 ctx->bit_alloc_params.cplfleak = get_bits(gb, 3);
886 ctx->bit_alloc_params.cplsleak = get_bits(gb, 3); 848 ctx->bit_alloc_params.cplsleak = get_bits(gb, 3);
849 bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2);
887 } 850 }
888 851
889 if (get_bits1(gb)) { /* delta bit allocation information */ 852 if (get_bits1(gb)) { /* delta bit allocation information */
890 bit_alloc_flags = 127; 853 for (ch = !ctx->cplinu; ch <= nfchans; ch++) {
891
892 if (ctx->cplinu) {
893 ctx->cpldeltbae = get_bits(gb, 2);
894 if (ctx->cpldeltbae == DBA_RESERVED) {
895 av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
896 return -1;
897 }
898 }
899
900 for (ch = 0; ch < nfchans; ch++) {
901 ctx->deltbae[ch] = get_bits(gb, 2); 854 ctx->deltbae[ch] = get_bits(gb, 2);
902 if (ctx->deltbae[ch] == DBA_RESERVED) { 855 if (ctx->deltbae[ch] == DBA_RESERVED) {
903 av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n"); 856 av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
904 return -1; 857 return -1;
905 } 858 }
906 } 859 bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
907 860 }
908 if (ctx->cplinu) { 861
909 if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */ 862 for (ch = !ctx->cplinu; ch <= nfchans; ch++) {
910 ctx->cpldeltnseg = get_bits(gb, 3);
911 for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
912 ctx->cpldeltoffst[seg] = get_bits(gb, 5);
913 ctx->cpldeltlen[seg] = get_bits(gb, 4);
914 ctx->cpldeltba[seg] = get_bits(gb, 3);
915 }
916 }
917 }
918
919 for (ch = 0; ch < nfchans; ch++) {
920 if (ctx->deltbae[ch] == DBA_NEW) {/*channel delta offset, len and bit allocation */ 863 if (ctx->deltbae[ch] == DBA_NEW) {/*channel delta offset, len and bit allocation */
921 ctx->deltnseg[ch] = get_bits(gb, 3); 864 ctx->deltnseg[ch] = get_bits(gb, 3);
922 for (seg = 0; seg <= ctx->deltnseg[ch]; seg++) { 865 for (seg = 0; seg <= ctx->deltnseg[ch]; seg++) {
923 ctx->deltoffst[ch][seg] = get_bits(gb, 5); 866 ctx->deltoffst[ch][seg] = get_bits(gb, 5);
924 ctx->deltlen[ch][seg] = get_bits(gb, 4); 867 ctx->deltlen[ch][seg] = get_bits(gb, 4);
925 ctx->deltba[ch][seg] = get_bits(gb, 3); 868 ctx->deltba[ch][seg] = get_bits(gb, 3);
926 } 869 }
927 } 870 }
928 } 871 }
929 } else if(blk == 0) { 872 } else if(blk == 0) {
930 if(ctx->cplinu) 873 for(ch=0; ch<=ctx->nchans; ch++) {
931 ctx->cpldeltbae = DBA_NONE;
932 for(ch=0; ch<nfchans; ch++) {
933 ctx->deltbae[ch] = DBA_NONE; 874 ctx->deltbae[ch] = DBA_NONE;
934 } 875 }
935 } 876 }
936 877
937 if (bit_alloc_flags) { 878 for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) {
938 if (ctx->cplinu && (bit_alloc_flags & 64)) { 879 if(bit_alloc_stages[ch] > 2) {
939 ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->cplbap, 880 /* Exponent mapping into PSD and PSD integration */
940 ctx->dcplexps, ctx->cplstrtmant, 881 ff_ac3_bit_alloc_calc_psd(ctx->dexps[ch],
941 ctx->cplendmant, ctx->cplsnroffst, 882 ctx->startmant[ch], ctx->endmant[ch],
942 ctx->cplfgain, 0, 883 ctx->psd[ch], ctx->bndpsd[ch]);
943 ctx->cpldeltbae, ctx->cpldeltnseg, 884 }
944 ctx->cpldeltoffst, ctx->cpldeltlen, 885 if(bit_alloc_stages[ch] > 1) {
945 ctx->cpldeltba); 886 /* Compute excitation function, Compute masking curve, and
946 } 887 Apply delta bit allocation */
947 for (ch = 0; ch < nfchans; ch++) { 888 ff_ac3_bit_alloc_calc_mask(&ctx->bit_alloc_params, ctx->bndpsd[ch],
948 if ((bit_alloc_flags >> ch) & 1) { 889 ctx->startmant[ch], ctx->endmant[ch],
949 ac3_parametric_bit_allocation(&ctx->bit_alloc_params, 890 ctx->fgain[ch], (ch == ctx->lfe_ch),
950 ctx->bap[ch], ctx->dexps[ch], 0, 891 ctx->deltbae[ch], ctx->deltnseg[ch],
951 ctx->endmant[ch], ctx->snroffst[ch], 892 ctx->deltoffst[ch], ctx->deltlen[ch],
952 ctx->fgain[ch], 0, ctx->deltbae[ch], 893 ctx->deltba[ch], ctx->mask[ch]);
953 ctx->deltnseg[ch], ctx->deltoffst[ch], 894 }
954 ctx->deltlen[ch], ctx->deltba[ch]); 895 if(bit_alloc_stages[ch] > 0) {
955 } 896 /* Compute bit allocation */
956 } 897 ff_ac3_bit_alloc_calc_bap(ctx->mask[ch], ctx->psd[ch],
957 if (ctx->lfeon && (bit_alloc_flags & 32)) { 898 ctx->startmant[ch], ctx->endmant[ch],
958 ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->lfebap, 899 ctx->snroffst[ch],
959 ctx->dlfeexps, 0, 7, ctx->lfesnroffst, 900 ctx->bit_alloc_params.floor,
960 ctx->lfefgain, 1, 901 ctx->bap[ch]);
961 DBA_NONE, 0, NULL, NULL, NULL);
962 } 902 }
963 } 903 }
964 904
965 if (get_bits1(gb)) { /* unused dummy data */ 905 if (get_bits1(gb)) { /* unused dummy data */
966 int skipl = get_bits(gb, 9); 906 int skipl = get_bits(gb, 9);
978 /* recover coefficients if rematrixing is in use */ 918 /* recover coefficients if rematrixing is in use */
979 if(ctx->acmod == AC3_ACMOD_STEREO) 919 if(ctx->acmod == AC3_ACMOD_STEREO)
980 do_rematrixing(ctx); 920 do_rematrixing(ctx);
981 921
982 /* apply scaling to coefficients (headroom, dynrng) */ 922 /* apply scaling to coefficients (headroom, dynrng) */
983 if(ctx->lfeon) { 923 for(ch=1; ch<=ctx->nchans; ch++) {
984 for(i=0; i<7; i++) {
985 ctx->transform_coeffs[0][i] *= 2.0f * ctx->dynrng;
986 }
987 }
988 for(ch=1; ch<=ctx->nfchans; ch++) {
989 float gain = 2.0f; 924 float gain = 2.0f;
990 if(ctx->acmod == AC3_ACMOD_DUALMONO && ch == 2) { 925 if(ctx->acmod == AC3_ACMOD_DUALMONO && ch == 2) {
991 gain *= ctx->dynrng2; 926 gain *= ctx->dynrng2;
992 } else { 927 } else {
993 gain *= ctx->dynrng; 928 gain *= ctx->dynrng;
994 } 929 }
995 for(i=0; i<ctx->endmant[ch-1]; i++) { 930 for(i=0; i<ctx->endmant[ch]; i++) {
996 ctx->transform_coeffs[ch][i] *= gain; 931 ctx->transform_coeffs[ch][i] *= gain;
997 } 932 }
998 } 933 }
999 934
1000 do_imdct(ctx); 935 do_imdct(ctx);
1022 */ 957 */
1023 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size) 958 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1024 { 959 {
1025 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data; 960 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1026 int16_t *out_samples = (int16_t *)data; 961 int16_t *out_samples = (int16_t *)data;
1027 int i, blk, ch, start; 962 int i, blk, ch;
1028 int32_t *int_ptr[6]; 963 int32_t *int_ptr[6];
1029 964
1030 for (ch = 0; ch < 6; ch++) 965 for (ch = 0; ch < 6; ch++)
1031 int_ptr[ch] = (int32_t *)(&ctx->output[ch]); 966 int_ptr[ch] = (int32_t *)(&ctx->output[ch]);
1032 967
1060 if (ac3_parse_audio_block(ctx, blk)) { 995 if (ac3_parse_audio_block(ctx, blk)) {
1061 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n"); 996 av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");
1062 *data_size = 0; 997 *data_size = 0;
1063 return ctx->frame_size; 998 return ctx->frame_size;
1064 } 999 }
1065 start = (ctx->output_mode & AC3_OUTPUT_LFEON) ? 0 : 1;
1066 for (i = 0; i < 256; i++) 1000 for (i = 0; i < 256; i++)
1067 for (ch = start; ch <= ctx->nfchans; ch++) 1001 for (ch = 0; ch < ctx->out_channels; ch++)
1068 *(out_samples++) = convert(int_ptr[ch][i]); 1002 *(out_samples++) = convert(int_ptr[ch][i]);
1069 } 1003 }
1070 *data_size = NB_BLOCKS * 256 * avctx->channels * sizeof (int16_t); 1004 *data_size = NB_BLOCKS * 256 * avctx->channels * sizeof (int16_t);
1071 return ctx->frame_size; 1005 return ctx->frame_size;
1072 } 1006 }