comparison ac3dec.c @ 5454:5b497d971546 libavcodec

dither zero-bit mantissas by default. remove dithering only if it's explicitly turned off.
author jbr
date Sat, 04 Aug 2007 01:13:08 +0000
parents d3a265cd395c
children 5afadd8cad2a
comparison
equal deleted inserted replaced
5453:d3a265cd395c 5454:5b497d971546
90 int surmixlev; 90 int surmixlev;
91 int dsurmod; 91 int dsurmod;
92 92
93 int blksw[AC3_MAX_CHANNELS]; 93 int blksw[AC3_MAX_CHANNELS];
94 int dithflag[AC3_MAX_CHANNELS]; 94 int dithflag[AC3_MAX_CHANNELS];
95 int dither_all;
95 int cplinu; 96 int cplinu;
96 int chincpl[AC3_MAX_CHANNELS]; 97 int chincpl[AC3_MAX_CHANNELS];
97 int phsflginu; 98 int phsflginu;
98 int cplcoe; 99 int cplcoe;
99 uint32_t cplbndstrc; 100 uint32_t cplbndstrc;
458 459
459 /* Get the transform coefficients for particular channel */ 460 /* Get the transform coefficients for particular channel */
460 static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m) 461 static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
461 { 462 {
462 GetBitContext *gb = &ctx->gb; 463 GetBitContext *gb = &ctx->gb;
463 int i, gcode, tbap, dithflag, start, end; 464 int i, gcode, tbap, start, end;
464 uint8_t *exps; 465 uint8_t *exps;
465 uint8_t *bap; 466 uint8_t *bap;
466 float *coeffs; 467 float *coeffs;
467 468
468 if (ch_index >= 0) { /* fbw channels */ 469 if (ch_index >= 0) { /* fbw channels */
469 dithflag = ctx->dithflag[ch_index];
470 exps = ctx->dexps[ch_index]; 470 exps = ctx->dexps[ch_index];
471 bap = ctx->bap[ch_index]; 471 bap = ctx->bap[ch_index];
472 coeffs = ctx->transform_coeffs[ch_index + 1]; 472 coeffs = ctx->transform_coeffs[ch_index + 1];
473 start = 0; 473 start = 0;
474 end = ctx->endmant[ch_index]; 474 end = ctx->endmant[ch_index];
475 } else if (ch_index == -1) { 475 } else if (ch_index == -1) {
476 dithflag = 0;
477 exps = ctx->dlfeexps; 476 exps = ctx->dlfeexps;
478 bap = ctx->lfebap; 477 bap = ctx->lfebap;
479 coeffs = ctx->transform_coeffs[0]; 478 coeffs = ctx->transform_coeffs[0];
480 start = 0; 479 start = 0;
481 end = 7; 480 end = 7;
482 } else { 481 } else {
483 dithflag = 0;
484 exps = ctx->dcplexps; 482 exps = ctx->dcplexps;
485 bap = ctx->cplbap; 483 bap = ctx->cplbap;
486 coeffs = ctx->transform_coeffs_cpl; 484 coeffs = ctx->transform_coeffs_cpl;
487 start = ctx->cplstrtmant; 485 start = ctx->cplstrtmant;
488 end = ctx->cplendmant; 486 end = ctx->cplendmant;
491 489
492 for (i = start; i < end; i++) { 490 for (i = start; i < end; i++) {
493 tbap = bap[i]; 491 tbap = bap[i];
494 switch (tbap) { 492 switch (tbap) {
495 case 0: 493 case 0:
496 if (!dithflag) {
497 coeffs[i] = 0;
498 }
499 else {
500 coeffs[i] = (av_random(&ctx->dith_state) & 0xFFFF) * LEVEL_MINUS_3DB; 494 coeffs[i] = (av_random(&ctx->dith_state) & 0xFFFF) * LEVEL_MINUS_3DB;
501 }
502 break; 495 break;
503 496
504 case 1: 497 case 1:
505 if (m->l3ptr > 2) { 498 if (m->l3ptr > 2) {
506 gcode = get_bits(gb, 5); 499 gcode = get_bits(gb, 5);
547 } 540 }
548 coeffs[i] *= scale_factors[exps[i]]; 541 coeffs[i] *= scale_factors[exps[i]];
549 } 542 }
550 543
551 return 0; 544 return 0;
545 }
546
547 /**
548 * Removes random dithering from coefficients with zero-bit mantissas
549 * reference: Section 7.3.4 Dither for Zero Bit Mantissas (bap=0)
550 */
551 static void remove_dithering(AC3DecodeContext *ctx) {
552 int ch, i;
553 int end=0;
554 float *coeffs;
555 uint8_t *bap;
556
557 for(ch=1; ch<=ctx->nfchans; ch++) {
558 if(!ctx->dithflag[ch-1]) {
559 coeffs = ctx->transform_coeffs[ch];
560 bap = ctx->bap[ch-1];
561 if(ctx->chincpl[ch-1])
562 end = ctx->cplstrtmant;
563 else
564 end = ctx->endmant[ch-1];
565 for(i=0; i<end; i++) {
566 if(bap[i] == 0)
567 coeffs[i] = 0.0f;
568 }
569 if(ctx->chincpl[ch-1]) {
570 bap = ctx->cplbap;
571 for(; i<ctx->cplendmant; i++) {
572 if(bap[i] == 0)
573 coeffs[i] = 0.0f;
574 }
575 }
576 }
577 }
552 } 578 }
553 579
554 /* Get the transform coefficients. 580 /* Get the transform coefficients.
555 * This function extracts the tranform coefficients form the ac3 bitstream. 581 * This function extracts the tranform coefficients form the ac3 bitstream.
556 * This function is called after bit allocation is performed. 582 * This function is called after bit allocation is performed.
590 for (i = 7; i < 256; i++) { 616 for (i = 7; i < 256; i++) {
591 ctx->transform_coeffs[0][i] = 0; 617 ctx->transform_coeffs[0][i] = 0;
592 } 618 }
593 } 619 }
594 620
621 /* if any channel doesn't use dithering, zero appropriate coefficients */
622 if(!ctx->dither_all)
623 remove_dithering(ctx);
624
595 return 0; 625 return 0;
596 } 626 }
597 627
598 /** 628 /**
599 * Performs stereo rematrixing. 629 * Performs stereo rematrixing.
706 int dynrng, chbwcod, ngrps, cplabsexp, skipl; 736 int dynrng, chbwcod, ngrps, cplabsexp, skipl;
707 737
708 for (i = 0; i < nfchans; i++) /*block switch flag */ 738 for (i = 0; i < nfchans; i++) /*block switch flag */
709 ctx->blksw[i] = get_bits1(gb); 739 ctx->blksw[i] = get_bits1(gb);
710 740
711 for (i = 0; i < nfchans; i++) /* dithering flag */ 741 ctx->dither_all = 1;
742 for (i = 0; i < nfchans; i++) { /* dithering flag */
712 ctx->dithflag[i] = get_bits1(gb); 743 ctx->dithflag[i] = get_bits1(gb);
744 if(!ctx->dithflag[i])
745 ctx->dither_all = 0;
746 }
713 747
714 if (get_bits1(gb)) { /* dynamic range */ 748 if (get_bits1(gb)) { /* dynamic range */
715 dynrng = get_sbits(gb, 8); 749 dynrng = get_sbits(gb, 8);
716 ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]); 750 ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
717 } else if(blk == 0) { 751 } else if(blk == 0) {