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

📄 ac3dec.c

📁 mediastreamer2是开源的网络传输媒体流的库
💻 C
📖 第 1 页 / 共 3 页
字号:
 */static void set_downmix_coeffs(AC3DecodeContext *s){    int i;    float cmix = gain_levels[s->center_mix_level];    float smix = gain_levels[s->surround_mix_level];    for(i=0; i<s->fbw_channels; i++) {        s->downmix_coeffs[i][0] = gain_levels[ac3_default_coeffs[s->channel_mode][i][0]];        s->downmix_coeffs[i][1] = gain_levels[ac3_default_coeffs[s->channel_mode][i][1]];    }    if(s->channel_mode > 1 && s->channel_mode & 1) {        s->downmix_coeffs[1][0] = s->downmix_coeffs[1][1] = cmix;    }    if(s->channel_mode == AC3_CHMODE_2F1R || s->channel_mode == AC3_CHMODE_3F1R) {        int nf = s->channel_mode - 2;        s->downmix_coeffs[nf][0] = s->downmix_coeffs[nf][1] = smix * LEVEL_MINUS_3DB;    }    if(s->channel_mode == AC3_CHMODE_2F2R || s->channel_mode == AC3_CHMODE_3F2R) {        int nf = s->channel_mode - 4;        s->downmix_coeffs[nf][0] = s->downmix_coeffs[nf+1][1] = smix;    }}/** * Decode the grouped exponents according to exponent strategy. * reference: Section 7.1.3 Exponent Decoding */static void decode_exponents(GetBitContext *gbc, int exp_strategy, int ngrps,                             uint8_t absexp, int8_t *dexps){    int i, j, grp, group_size;    int dexp[256];    int expacc, prevexp;    /* unpack groups */    group_size = exp_strategy + (exp_strategy == EXP_D45);    for(grp=0,i=0; grp<ngrps; grp++) {        expacc = get_bits(gbc, 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<group_size; j++) {            dexps[(i*group_size)+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 *s){    int i, j, ch, bnd, subbnd;    subbnd = -1;    i = s->start_freq[CPL_CH];    for(bnd=0; bnd<s->num_cpl_bands; bnd++) {        do {            subbnd++;            for(j=0; j<12; j++) {                for(ch=1; ch<=s->fbw_channels; ch++) {                    if(s->channel_in_cpl[ch]) {                        s->transform_coeffs[ch][i] = s->transform_coeffs[CPL_CH][i] * s->cpl_coords[ch][bnd] * 8.0f;                        if (ch == 2 && s->phase_flags[bnd])                            s->transform_coeffs[ch][i] = -s->transform_coeffs[ch][i];                    }                }                i++;            }        } while(s->cpl_band_struct[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 *s, int ch_index, mant_groups *m){    GetBitContext *gbc = &s->gbc;    int i, gcode, tbap, start, end;    uint8_t *exps;    uint8_t *bap;    float *coeffs;    exps = s->dexps[ch_index];    bap = s->bap[ch_index];    coeffs = s->transform_coeffs[ch_index];    start = s->start_freq[ch_index];    end = s->end_freq[ch_index];    for (i = start; i < end; i++) {        tbap = bap[i];        switch (tbap) {            case 0:                coeffs[i] = ((av_random(&s->dith_state) & 0xFFFF) / 65535.0f) - 0.5f;                break;            case 1:                if(m->b1ptr > 2) {                    gcode = get_bits(gbc, 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(gbc, 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(gbc, 3)];                break;            case 4:                if(m->b4ptr > 1) {                    gcode = get_bits(gbc, 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(gbc, 4)];                break;            default:                /* asymmetric dequantization */                coeffs[i] = get_sbits(gbc, quantization_tab[tbap]) * scale_factors[quantization_tab[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 *s) {    int ch, i;    int end=0;    float *coeffs;    uint8_t *bap;    for(ch=1; ch<=s->fbw_channels; ch++) {        if(!s->dither_flag[ch]) {            coeffs = s->transform_coeffs[ch];            bap = s->bap[ch];            if(s->channel_in_cpl[ch])                end = s->start_freq[CPL_CH];            else                end = s->end_freq[ch];            for(i=0; i<end; i++) {                if(!bap[i])                    coeffs[i] = 0.0f;            }            if(s->channel_in_cpl[ch]) {                bap = s->bap[CPL_CH];                for(; i<s->end_freq[CPL_CH]; i++) {                    if(!bap[i])                        coeffs[i] = 0.0f;                }            }        }    }}/** * Get the transform coefficients. */static int get_transform_coeffs(AC3DecodeContext *s){    int ch, end;    int got_cplchan = 0;    mant_groups m;    m.b1ptr = m.b2ptr = m.b4ptr = 3;    for (ch = 1; ch <= s->channels; ch++) {        /* transform coefficients for full-bandwidth channel */        if (get_transform_coeffs_ch(s, ch, &m))            return -1;        /* tranform coefficients for coupling channel come right after the           coefficients for the first coupled channel*/        if (s->channel_in_cpl[ch])  {            if (!got_cplchan) {                if (get_transform_coeffs_ch(s, CPL_CH, &m)) {                    av_log(s->avctx, AV_LOG_ERROR, "error in decoupling channels\n");                    return -1;                }                uncouple_channels(s);                got_cplchan = 1;            }            end = s->end_freq[CPL_CH];        } else {            end = s->end_freq[ch];        }        do            s->transform_coeffs[ch][end] = 0;        while(++end < 256);    }    /* if any channel doesn't use dithering, zero appropriate coefficients */    if(!s->dither_all)        remove_dithering(s);    return 0;}/** * Stereo rematrixing. * reference: Section 7.5.4 Rematrixing : Decoding Technique */static void do_rematrixing(AC3DecodeContext *s){    int bnd, i;    int end, bndend;    float tmp0, tmp1;    end = FFMIN(s->end_freq[1], s->end_freq[2]);    for(bnd=0; bnd<s->num_rematrixing_bands; bnd++) {        if(s->rematrixing_flags[bnd]) {            bndend = FFMIN(end, rematrix_band_tab[bnd+1]);            for(i=rematrix_band_tab[bnd]; i<bndend; i++) {                tmp0 = s->transform_coeffs[1][i];                tmp1 = s->transform_coeffs[2][i];                s->transform_coeffs[1][i] = tmp0 + tmp1;                s->transform_coeffs[2][i] = tmp0 - tmp1;            }        }    }}/** * Perform the 256-point IMDCT */static void do_imdct_256(AC3DecodeContext *s, int chindex){    int i, k;    DECLARE_ALIGNED_16(float, x[128]);    FFTComplex z[2][64];    float *o_ptr = s->tmp_output;    for(i=0; i<2; i++) {        /* de-interleave coefficients */        for(k=0; k<128; k++) {            x[k] = s->transform_coeffs[chindex][2*k+i];        }        /* run standard IMDCT */        s->imdct_256.fft.imdct_calc(&s->imdct_256, o_ptr, x, s->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 *s){    int ch;    int channels;    /* Don't perform the IMDCT on the LFE channel unless it's used in the output */    channels = s->fbw_channels;    if(s->output_mode & AC3_OUTPUT_LFEON)        channels++;    for (ch=1; ch<=channels; ch++) {        if (s->block_switch[ch]) {            do_imdct_256(s, ch);        } else {            s->imdct_512.fft.imdct_calc(&s->imdct_512, s->tmp_output,                                        s->transform_coeffs[ch], s->tmp_imdct);        }        /* For the first half of the block, apply the window, add the delay           from the previous block, and send to output */        s->dsp.vector_fmul_add_add(s->output[ch-1], s->tmp_output,                                     s->window, s->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 */        s->dsp.vector_fmul_reverse(s->delay[ch-1], s->tmp_output+256,                                   s->window, 256);    }}/** * Downmix the output to mono or stereo. */static void ac3_downmix(AC3DecodeContext *s){    int i, j;    float v0, v1, s0, s1;    for(i=0; i<256; i++) {        v0 = v1 = s0 = s1 = 0.0f;        for(j=0; j<s->fbw_channels; j++) {            v0 += s->output[j][i] * s->downmix_coeffs[j][0];            v1 += s->output[j][i] * s->downmix_coeffs[j][1];            s0 += s->downmix_coeffs[j][0];            s1 += s->downmix_coeffs[j][1];        }        v0 /= s0;        v1 /= s1;        if(s->output_mode == AC3_CHMODE_MONO) {            s->output[0][i] = (v0 + v1) * LEVEL_MINUS_3DB;        } else if(s->output_mode == AC3_CHMODE_STEREO) {            s->output[0][i] = v0;            s->output[1][i] = v1;        }    }}/** * Parse an audio block from AC-3 bitstream. */static int ac3_parse_audio_block(AC3DecodeContext *s, int blk){    int fbw_channels = s->fbw_channels;    int channel_mode = s->channel_mode;    int i, bnd, seg, ch;    GetBitContext *gbc = &s->gbc;    uint8_t bit_alloc_stages[AC3_MAX_CHANNELS];    memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS);    /* block switch flags */    for (ch = 1; ch <= fbw_channels; ch++)        s->block_switch[ch] = get_bits1(gbc);    /* dithering flags */    s->dither_all = 1;    for (ch = 1; ch <= fbw_channels; ch++) {        s->dither_flag[ch] = get_bits1(gbc);

⌨️ 快捷键说明

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