Revision 60f07fad

View differences:

libavcodec/ac3dec.c
92 92

  
93 93
    int blksw[AC3_MAX_CHANNELS];
94 94
    int dithflag[AC3_MAX_CHANNELS];
95
    int dither_all;
95 96
    int cplinu;
96 97
    int chincpl[AC3_MAX_CHANNELS];
97 98
    int phsflginu;
......
460 461
static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m)
461 462
{
462 463
    GetBitContext *gb = &ctx->gb;
463
    int i, gcode, tbap, dithflag, start, end;
464
    int i, gcode, tbap, start, end;
464 465
    uint8_t *exps;
465 466
    uint8_t *bap;
466 467
    float *coeffs;
467 468

  
468 469
    if (ch_index >= 0) { /* fbw channels */
469
        dithflag = ctx->dithflag[ch_index];
470 470
        exps = ctx->dexps[ch_index];
471 471
        bap = ctx->bap[ch_index];
472 472
        coeffs = ctx->transform_coeffs[ch_index + 1];
473 473
        start = 0;
474 474
        end = ctx->endmant[ch_index];
475 475
    } else if (ch_index == -1) {
476
        dithflag = 0;
477 476
        exps = ctx->dlfeexps;
478 477
        bap = ctx->lfebap;
479 478
        coeffs = ctx->transform_coeffs[0];
480 479
        start = 0;
481 480
        end = 7;
482 481
    } else {
483
        dithflag = 0;
484 482
        exps = ctx->dcplexps;
485 483
        bap = ctx->cplbap;
486 484
        coeffs = ctx->transform_coeffs_cpl;
......
493 491
        tbap = bap[i];
494 492
        switch (tbap) {
495 493
            case 0:
496
                if (!dithflag) {
497
                    coeffs[i] = 0;
498
                }
499
                else {
500 494
                    coeffs[i] = (av_random(&ctx->dith_state) & 0xFFFF) * LEVEL_MINUS_3DB;
501
                }
502 495
                break;
503 496

  
504 497
            case 1:
......
551 544
    return 0;
552 545
}
553 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
    }
578
}
579

  
554 580
/* Get the transform coefficients.
555 581
 * This function extracts the tranform coefficients form the ac3 bitstream.
556 582
 * This function is called after bit allocation is performed.
......
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 625
    return 0;
596 626
}
597 627

  
......
708 738
    for (i = 0; i < nfchans; i++) /*block switch flag */
709 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 743
        ctx->dithflag[i] = get_bits1(gb);
744
        if(!ctx->dithflag[i])
745
            ctx->dither_all = 0;
746
    }
713 747

  
714 748
    if (get_bits1(gb)) { /* dynamic range */
715 749
        dynrng = get_sbits(gb, 8);

Also available in: Unified diff