⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ac3dec.c

📁 ffmpeg的完整源代码和作者自己写的文档。不但有在Linux的工程哦
💻 C
📖 第 1 页 / 共 3 页
字号:
    /* skip the timecodes (or extra bitstream information for Alternate Syntax)       TODO: read & use the xbsi1 downmix levels */    if (get_bits1(gb))        skip_bits(gb, 14); //skip timecode1 / xbsi1    if (get_bits1(gb))        skip_bits(gb, 14); //skip timecode2 / xbsi2    /* skip additional bitstream info */    if (get_bits1(gb)) {        i = get_bits(gb, 6);        do {            skip_bits(gb, 8);        } while(i--);    }    /* set stereo downmixing coefficients       reference: Section 7.8.2 Downmixing Into Two Channels */    for(i=0; i<ctx->nfchans; i++) {        ctx->downmix_coeffs[i][0] = gain_levels[ac3_default_coeffs[ctx->acmod][i][0]];        ctx->downmix_coeffs[i][1] = gain_levels[ac3_default_coeffs[ctx->acmod][i][1]];    }    if(ctx->acmod > 1 && ctx->acmod & 1) {        ctx->downmix_coeffs[1][0] = ctx->downmix_coeffs[1][1] = cmixlev;    }    if(ctx->acmod == AC3_ACMOD_2F1R || ctx->acmod == AC3_ACMOD_3F1R) {        int nf = ctx->acmod - 2;        ctx->downmix_coeffs[nf][0] = ctx->downmix_coeffs[nf][1] = surmixlev * LEVEL_MINUS_3DB;    }    if(ctx->acmod == AC3_ACMOD_2F2R || ctx->acmod == AC3_ACMOD_3F2R) {        int nf = ctx->acmod - 4;        ctx->downmix_coeffs[nf][0] = ctx->downmix_coeffs[nf+1][1] = surmixlev;    }    return 0;}/** * Decode the grouped exponents according to exponent strategy. * reference: Section 7.1.3 Exponent Decoding */static void decode_exponents(GetBitContext *gb, int expstr, int ngrps,                             uint8_t absexp, int8_t *dexps){    int i, j, grp, grpsize;    int dexp[256];    int expacc, prevexp;    /* unpack groups */    grpsize = expstr + (expstr == EXP_D45);    for(grp=0,i=0; grp<ngrps; grp++) {        expacc = get_bits(gb, 7);        dexp[i++] = exp_ungroup_tab[expacc][0];        dexp[i++] = exp_ungroup_tab[expacc][1];        dexp[i++] = exp_ungroup_tab[expacc][2];    }    /* convert to absolute exps and expand groups */    prevexp = absexp;    for(i=0; i<ngrps*3; i++) {        prevexp = av_clip(prevexp + dexp[i]-2, 0, 24);        for(j=0; j<grpsize; j++) {            dexps[(i*grpsize)+j] = prevexp;        }    }}/** * Generate transform coefficients for each coupled channel in the coupling * range using the coupling coefficients and coupling coordinates. * reference: Section 7.4.3 Coupling Coordinate Format */static void uncouple_channels(AC3DecodeContext *ctx){    int i, j, ch, bnd, subbnd;    subbnd = -1;    i = ctx->startmant[CPL_CH];    for(bnd=0; bnd<ctx->ncplbnd; bnd++) {        do {            subbnd++;            for(j=0; j<12; j++) {                for(ch=1; ch<=ctx->nfchans; ch++) {                    if(ctx->chincpl[ch])                        ctx->transform_coeffs[ch][i] = ctx->transform_coeffs[CPL_CH][i] * ctx->cplco[ch][bnd] * 8.0f;                }                i++;            }        } while(ctx->cplbndstrc[subbnd]);    }}/** * Grouped mantissas for 3-level 5-level and 11-level quantization */typedef struct {    float b1_mant[3];    float b2_mant[3];    float b4_mant[2];    int b1ptr;    int b2ptr;    int b4ptr;} mant_groups;/** * Get the transform coefficients for a particular channel * reference: Section 7.3 Quantization and Decoding of Mantissas */static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_groups *m){    GetBitContext *gb = &ctx->gb;    int i, gcode, tbap, start, end;    uint8_t *exps;    uint8_t *bap;    float *coeffs;    exps = ctx->dexps[ch_index];    bap = ctx->bap[ch_index];    coeffs = ctx->transform_coeffs[ch_index];    start = ctx->startmant[ch_index];    end = ctx->endmant[ch_index];    for (i = start; i < end; i++) {        tbap = bap[i];        switch (tbap) {            case 0:                coeffs[i] = ((av_random(&ctx->dith_state) & 0xFFFF) / 65535.0f) - 0.5f;                break;            case 1:                if(m->b1ptr > 2) {                    gcode = get_bits(gb, 5);                    m->b1_mant[0] = b1_mantissas[gcode][0];                    m->b1_mant[1] = b1_mantissas[gcode][1];                    m->b1_mant[2] = b1_mantissas[gcode][2];                    m->b1ptr = 0;                }                coeffs[i] = m->b1_mant[m->b1ptr++];                break;            case 2:                if(m->b2ptr > 2) {                    gcode = get_bits(gb, 7);                    m->b2_mant[0] = b2_mantissas[gcode][0];                    m->b2_mant[1] = b2_mantissas[gcode][1];                    m->b2_mant[2] = b2_mantissas[gcode][2];                    m->b2ptr = 0;                }                coeffs[i] = m->b2_mant[m->b2ptr++];                break;            case 3:                coeffs[i] = b3_mantissas[get_bits(gb, 3)];                break;            case 4:                if(m->b4ptr > 1) {                    gcode = get_bits(gb, 7);                    m->b4_mant[0] = b4_mantissas[gcode][0];                    m->b4_mant[1] = b4_mantissas[gcode][1];                    m->b4ptr = 0;                }                coeffs[i] = m->b4_mant[m->b4ptr++];                break;            case 5:                coeffs[i] = b5_mantissas[get_bits(gb, 4)];                break;            default:                /* asymmetric dequantization */                coeffs[i] = get_sbits(gb, qntztab[tbap]) * scale_factors[qntztab[tbap]-1];                break;        }        coeffs[i] *= scale_factors[exps[i]];    }    return 0;}/** * Remove random dithering from coefficients with zero-bit mantissas * reference: Section 7.3.4 Dither for Zero Bit Mantissas (bap=0) */static void remove_dithering(AC3DecodeContext *ctx) {    int ch, i;    int end=0;    float *coeffs;    uint8_t *bap;    for(ch=1; ch<=ctx->nfchans; ch++) {        if(!ctx->dithflag[ch]) {            coeffs = ctx->transform_coeffs[ch];            bap = ctx->bap[ch];            if(ctx->chincpl[ch])                end = ctx->startmant[CPL_CH];            else                end = ctx->endmant[ch];            for(i=0; i<end; i++) {                if(bap[i] == 0)                    coeffs[i] = 0.0f;            }            if(ctx->chincpl[ch]) {                bap = ctx->bap[CPL_CH];                for(; i<ctx->endmant[CPL_CH]; i++) {                    if(bap[i] == 0)                        coeffs[i] = 0.0f;                }            }        }    }}/** * Get the transform coefficients. */static int get_transform_coeffs(AC3DecodeContext * ctx){    int ch, end;    int got_cplchan = 0;    mant_groups m;    m.b1ptr = m.b2ptr = m.b4ptr = 3;    for (ch = 1; ch <= ctx->nchans; ch++) {        /* transform coefficients for full-bandwidth channel */        if (get_transform_coeffs_ch(ctx, ch, &m))            return -1;        /* tranform coefficients for coupling channel come right after the           coefficients for the first coupled channel*/        if (ctx->chincpl[ch])  {            if (!got_cplchan) {                if (get_transform_coeffs_ch(ctx, CPL_CH, &m)) {                    av_log(ctx->avctx, AV_LOG_ERROR, "error in decoupling channels\n");                    return -1;                }                uncouple_channels(ctx);                got_cplchan = 1;            }            end = ctx->endmant[CPL_CH];        } else {            end = ctx->endmant[ch];        }        do            ctx->transform_coeffs[ch][end] = 0;        while(++end < 256);    }    /* if any channel doesn't use dithering, zero appropriate coefficients */    if(!ctx->dither_all)        remove_dithering(ctx);    return 0;}/** * Stereo rematrixing. * reference: Section 7.5.4 Rematrixing : Decoding Technique */static void do_rematrixing(AC3DecodeContext *ctx){    int bnd, i;    int end, bndend;    float tmp0, tmp1;    end = FFMIN(ctx->endmant[1], ctx->endmant[2]);    for(bnd=0; bnd<ctx->nrematbnd; bnd++) {        if(ctx->rematflg[bnd]) {            bndend = FFMIN(end, rematrix_band_tab[bnd+1]);            for(i=rematrix_band_tab[bnd]; i<bndend; i++) {                tmp0 = ctx->transform_coeffs[1][i];                tmp1 = ctx->transform_coeffs[2][i];                ctx->transform_coeffs[1][i] = tmp0 + tmp1;                ctx->transform_coeffs[2][i] = tmp0 - tmp1;            }        }    }}/** * Perform the 256-point IMDCT */static void do_imdct_256(AC3DecodeContext *ctx, int chindex){    int i, k;    DECLARE_ALIGNED_16(float, x[128]);    FFTComplex z[2][64];    float *o_ptr = ctx->tmp_output;    for(i=0; i<2; i++) {        /* de-interleave coefficients */        for(k=0; k<128; k++) {            x[k] = ctx->transform_coeffs[chindex][2*k+i];        }        /* run standard IMDCT */        ctx->imdct_256.fft.imdct_calc(&ctx->imdct_256, o_ptr, x, ctx->tmp_imdct);        /* reverse the post-rotation & reordering from standard IMDCT */        for(k=0; k<32; k++) {            z[i][32+k].re = -o_ptr[128+2*k];            z[i][32+k].im = -o_ptr[2*k];            z[i][31-k].re =  o_ptr[2*k+1];            z[i][31-k].im =  o_ptr[128+2*k+1];        }    }    /* apply AC-3 post-rotation & reordering */    for(k=0; k<64; k++) {        o_ptr[    2*k  ] = -z[0][   k].im;        o_ptr[    2*k+1] =  z[0][63-k].re;        o_ptr[128+2*k  ] = -z[0][   k].re;        o_ptr[128+2*k+1] =  z[0][63-k].im;        o_ptr[256+2*k  ] = -z[1][   k].re;        o_ptr[256+2*k+1] =  z[1][63-k].im;        o_ptr[384+2*k  ] =  z[1][   k].im;        o_ptr[384+2*k+1] = -z[1][63-k].re;    }}/** * Inverse MDCT Transform. * Convert frequency domain coefficients to time-domain audio samples. * reference: Section 7.9.4 Transformation Equations */static inline void do_imdct(AC3DecodeContext *ctx){    int ch;    int nchans;    /* Don't perform the IMDCT on the LFE channel unless it's used in the output */    nchans = ctx->nfchans;    if(ctx->output_mode & AC3_OUTPUT_LFEON)        nchans++;    for (ch=1; ch<=nchans; ch++) {        if (ctx->blksw[ch]) {            do_imdct_256(ctx, ch);        } else {            ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,                                          ctx->transform_coeffs[ch],                                          ctx->tmp_imdct);        }        /* For the first half of the block, apply the window, add the delay           from the previous block, and send to output */        ctx->dsp.vector_fmul_add_add(ctx->output[ch-1], ctx->tmp_output,                                     ctx->window, ctx->delay[ch-1], 0, 256, 1);        /* For the second half of the block, apply the window and store the           samples to delay, to be combined with the next block */        ctx->dsp.vector_fmul_reverse(ctx->delay[ch-1], ctx->tmp_output+256,                                     ctx->window, 256);    }}/** * Downmix the output to mono or stereo. */static void ac3_downmix(float samples[AC3_MAX_CHANNELS][256], int nfchans,                        int output_mode, float coef[AC3_MAX_CHANNELS][2]){    int i, j;    float v0, v1, s0, s1;    for(i=0; i<256; i++) {        v0 = v1 = s0 = s1 = 0.0f;        for(j=0; j<nfchans; j++) {            v0 += samples[j][i] * coef[j][0];            v1 += samples[j][i] * coef[j][1];            s0 += coef[j][0];            s1 += coef[j][1];        }        v0 /= s0;        v1 /= s1;        if(output_mode == AC3_ACMOD_MONO) {            samples[0][i] = (v0 + v1) * LEVEL_MINUS_3DB;        } else if(output_mode == AC3_ACMOD_STEREO) {            samples[0][i] = v0;            samples[1][i] = v1;        }    }}/** * Parse an audio block from AC-3 bitstream. */static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk){    int nfchans = ctx->nfchans;    int acmod = ctx->acmod;    int i, bnd, seg, ch;    GetBitContext *gb = &ctx->gb;    uint8_t bit_alloc_stages[AC3_MAX_CHANNELS];    memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -