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

📄 ac3dec.c

📁 ffmpeg的完整源代码和作者自己写的文档。不但有在Linux的工程哦
💻 C
📖 第 1 页 / 共 3 页
字号:
    /* block switch flags */    for (ch = 1; ch <= nfchans; ch++)        ctx->blksw[ch] = get_bits1(gb);    /* dithering flags */    ctx->dither_all = 1;    for (ch = 1; ch <= nfchans; ch++) {        ctx->dithflag[ch] = get_bits1(gb);        if(!ctx->dithflag[ch])            ctx->dither_all = 0;    }    /* dynamic range */    i = !(ctx->acmod);    do {        if(get_bits1(gb)) {            ctx->dynrng[i] = dynrng_tab[get_bits(gb, 8)];        } else if(blk == 0) {            ctx->dynrng[i] = 1.0f;        }    } while(i--);    /* coupling strategy */    if (get_bits1(gb)) {        memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);        ctx->cplinu = get_bits1(gb);        if (ctx->cplinu) {            /* coupling in use */            int cplbegf, cplendf;            /* determine which channels are coupled */            for (ch = 1; ch <= nfchans; ch++)                ctx->chincpl[ch] = get_bits1(gb);            /* phase flags in use */            if (acmod == AC3_ACMOD_STEREO)                ctx->phsflginu = get_bits1(gb);            /* coupling frequency range and band structure */            cplbegf = get_bits(gb, 4);            cplendf = get_bits(gb, 4);            if (3 + cplendf - cplbegf < 0) {                av_log(ctx->avctx, AV_LOG_ERROR, "cplendf = %d < cplbegf = %d\n", cplendf, cplbegf);                return -1;            }            ctx->ncplbnd = ctx->ncplsubnd = 3 + cplendf - cplbegf;            ctx->startmant[CPL_CH] = cplbegf * 12 + 37;            ctx->endmant[CPL_CH] = cplendf * 12 + 73;            for (bnd = 0; bnd < ctx->ncplsubnd - 1; bnd++) {                if (get_bits1(gb)) {                    ctx->cplbndstrc[bnd] = 1;                    ctx->ncplbnd--;                }            }        } else {            /* coupling not in use */            for (ch = 1; ch <= nfchans; ch++)                ctx->chincpl[ch] = 0;        }    }    /* coupling coordinates */    if (ctx->cplinu) {        int cplcoe = 0;        for (ch = 1; ch <= nfchans; ch++) {            if (ctx->chincpl[ch]) {                if (get_bits1(gb)) {                    int mstrcplco, cplcoexp, cplcomant;                    cplcoe = 1;                    mstrcplco = 3 * get_bits(gb, 2);                    for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {                        cplcoexp = get_bits(gb, 4);                        cplcomant = get_bits(gb, 4);                        if (cplcoexp == 15)                            ctx->cplco[ch][bnd] = cplcomant / 16.0f;                        else                            ctx->cplco[ch][bnd] = (cplcomant + 16.0f) / 32.0f;                        ctx->cplco[ch][bnd] *= scale_factors[cplcoexp + mstrcplco];                    }                }            }        }        /* phase flags */        if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && cplcoe) {            for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {                if (get_bits1(gb))                    ctx->cplco[2][bnd] = -ctx->cplco[2][bnd];            }        }    }    /* stereo rematrixing strategy and band structure */    if (acmod == AC3_ACMOD_STEREO) {        ctx->rematstr = get_bits1(gb);        if (ctx->rematstr) {            ctx->nrematbnd = 4;            if(ctx->cplinu && ctx->startmant[CPL_CH] <= 61)                ctx->nrematbnd -= 1 + (ctx->startmant[CPL_CH] == 37);            for(bnd=0; bnd<ctx->nrematbnd; bnd++)                ctx->rematflg[bnd] = get_bits1(gb);        }    }    /* exponent strategies for each channel */    ctx->expstr[CPL_CH] = EXP_REUSE;    ctx->expstr[ctx->lfe_ch] = EXP_REUSE;    for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {        if(ch == ctx->lfe_ch)            ctx->expstr[ch] = get_bits(gb, 1);        else            ctx->expstr[ch] = get_bits(gb, 2);        if(ctx->expstr[ch] != EXP_REUSE)            bit_alloc_stages[ch] = 3;    }    /* channel bandwidth */    for (ch = 1; ch <= nfchans; ch++) {        ctx->startmant[ch] = 0;        if (ctx->expstr[ch] != EXP_REUSE) {            int prev = ctx->endmant[ch];            if (ctx->chincpl[ch])                ctx->endmant[ch] = ctx->startmant[CPL_CH];            else {                int chbwcod = get_bits(gb, 6);                if (chbwcod > 60) {                    av_log(ctx->avctx, AV_LOG_ERROR, "chbwcod = %d > 60", chbwcod);                    return -1;                }                ctx->endmant[ch] = chbwcod * 3 + 73;            }            if(blk > 0 && ctx->endmant[ch] != prev)                memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);        }    }    ctx->startmant[ctx->lfe_ch] = 0;    ctx->endmant[ctx->lfe_ch] = 7;    /* decode exponents for each channel */    for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {        if (ctx->expstr[ch] != EXP_REUSE) {            int grpsize, ngrps;            grpsize = 3 << (ctx->expstr[ch] - 1);            if(ch == CPL_CH)                ngrps = (ctx->endmant[ch] - ctx->startmant[ch]) / grpsize;            else if(ch == ctx->lfe_ch)                ngrps = 2;            else                ngrps = (ctx->endmant[ch] + grpsize - 4) / grpsize;            ctx->dexps[ch][0] = get_bits(gb, 4) << !ch;            decode_exponents(gb, ctx->expstr[ch], ngrps, ctx->dexps[ch][0],                             &ctx->dexps[ch][ctx->startmant[ch]+!!ch]);            if(ch != CPL_CH && ch != ctx->lfe_ch)                skip_bits(gb, 2); /* skip gainrng */        }    }    /* bit allocation information */    if (get_bits1(gb)) {        ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)] >> ctx->bit_alloc_params.halfratecod;        ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)] >> ctx->bit_alloc_params.halfratecod;        ctx->bit_alloc_params.sgain  = ff_sgaintab[get_bits(gb, 2)];        ctx->bit_alloc_params.dbknee = ff_dbkneetab[get_bits(gb, 2)];        ctx->bit_alloc_params.floor  = ff_floortab[get_bits(gb, 3)];        for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) {            bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);        }    }    /* signal-to-noise ratio offsets and fast gains (signal-to-mask ratios) */    if (get_bits1(gb)) {        int csnr;        csnr = (get_bits(gb, 6) - 15) << 4;        for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) { /* snr offset and fast gain */            ctx->snroffst[ch] = (csnr + get_bits(gb, 4)) << 2;            ctx->fgain[ch] = ff_fgaintab[get_bits(gb, 3)];        }        memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);    }    /* coupling leak information */    if (ctx->cplinu && get_bits1(gb)) {        ctx->bit_alloc_params.cplfleak = get_bits(gb, 3);        ctx->bit_alloc_params.cplsleak = get_bits(gb, 3);        bit_alloc_stages[CPL_CH] = FFMAX(bit_alloc_stages[CPL_CH], 2);    }    /* delta bit allocation information */    if (get_bits1(gb)) {        /* delta bit allocation exists (strategy) */        for (ch = !ctx->cplinu; ch <= nfchans; ch++) {            ctx->deltbae[ch] = get_bits(gb, 2);            if (ctx->deltbae[ch] == DBA_RESERVED) {                av_log(ctx->avctx, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");                return -1;            }            bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);        }        /* channel delta offset, len and bit allocation */        for (ch = !ctx->cplinu; ch <= nfchans; ch++) {            if (ctx->deltbae[ch] == DBA_NEW) {                ctx->deltnseg[ch] = get_bits(gb, 3);                for (seg = 0; seg <= ctx->deltnseg[ch]; seg++) {                    ctx->deltoffst[ch][seg] = get_bits(gb, 5);                    ctx->deltlen[ch][seg] = get_bits(gb, 4);                    ctx->deltba[ch][seg] = get_bits(gb, 3);                }            }        }    } else if(blk == 0) {        for(ch=0; ch<=ctx->nchans; ch++) {            ctx->deltbae[ch] = DBA_NONE;        }    }    /* Bit allocation */    for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) {        if(bit_alloc_stages[ch] > 2) {            /* Exponent mapping into PSD and PSD integration */            ff_ac3_bit_alloc_calc_psd(ctx->dexps[ch],                                      ctx->startmant[ch], ctx->endmant[ch],                                      ctx->psd[ch], ctx->bndpsd[ch]);        }        if(bit_alloc_stages[ch] > 1) {            /* Compute excitation function, Compute masking curve, and               Apply delta bit allocation */            ff_ac3_bit_alloc_calc_mask(&ctx->bit_alloc_params, ctx->bndpsd[ch],                                       ctx->startmant[ch], ctx->endmant[ch],                                       ctx->fgain[ch], (ch == ctx->lfe_ch),                                       ctx->deltbae[ch], ctx->deltnseg[ch],                                       ctx->deltoffst[ch], ctx->deltlen[ch],                                       ctx->deltba[ch], ctx->mask[ch]);        }        if(bit_alloc_stages[ch] > 0) {            /* Compute bit allocation */            ff_ac3_bit_alloc_calc_bap(ctx->mask[ch], ctx->psd[ch],                                      ctx->startmant[ch], ctx->endmant[ch],                                      ctx->snroffst[ch],                                      ctx->bit_alloc_params.floor,                                      ctx->bap[ch]);        }    }    /* unused dummy data */    if (get_bits1(gb)) {        int skipl = get_bits(gb, 9);        while(skipl--)            skip_bits(gb, 8);    }    /* unpack the transform coefficients       this also uncouples channels if coupling is in use. */    if (get_transform_coeffs(ctx)) {        av_log(ctx->avctx, AV_LOG_ERROR, "Error in routine get_transform_coeffs\n");        return -1;    }    /* recover coefficients if rematrixing is in use */    if(ctx->acmod == AC3_ACMOD_STEREO)        do_rematrixing(ctx);    /* apply scaling to coefficients (headroom, dialnorm, dynrng) */    for(ch=1; ch<=ctx->nchans; ch++) {        float gain = 2.0f * ctx->mul_bias;        if(ctx->acmod == AC3_ACMOD_DUALMONO) {            gain *= ctx->dialnorm[ch-1] * ctx->dynrng[ch-1];        } else {            gain *= ctx->dialnorm[0] * ctx->dynrng[0];        }        for(i=0; i<ctx->endmant[ch]; i++) {            ctx->transform_coeffs[ch][i] *= gain;        }    }    do_imdct(ctx);    /* downmix output if needed */    if(ctx->nchans != ctx->out_channels && !((ctx->output_mode & AC3_OUTPUT_LFEON) &&            ctx->nfchans == ctx->out_channels)) {        ac3_downmix(ctx->output, ctx->nfchans, ctx->output_mode,                    ctx->downmix_coeffs);    }    /* convert float to 16-bit integer */    for(ch=0; ch<ctx->out_channels; ch++) {        for(i=0; i<256; i++) {            ctx->output[ch][i] += ctx->add_bias;        }        ctx->dsp.float_to_int16(ctx->int_output[ch], ctx->output[ch], 256);    }    return 0;}/** * Decode a single AC-3 frame. */static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size){    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;    int16_t *out_samples = (int16_t *)data;    int i, blk, ch, err;    /* initialize the GetBitContext with the start of valid AC-3 Frame */    init_get_bits(&ctx->gb, buf, buf_size * 8);    /* parse the syncinfo */    err = ac3_parse_header(ctx);    if(err) {        switch(err) {            case AC3_PARSE_ERROR_SYNC:                av_log(avctx, AV_LOG_ERROR, "frame sync error\n");                break;            case AC3_PARSE_ERROR_BSID:                av_log(avctx, AV_LOG_ERROR, "invalid bitstream id\n");                break;            case AC3_PARSE_ERROR_SAMPLE_RATE:                av_log(avctx, AV_LOG_ERROR, "invalid sample rate\n");                break;            case AC3_PARSE_ERROR_FRAME_SIZE:                av_log(avctx, AV_LOG_ERROR, "invalid frame size\n");                break;            default:                av_log(avctx, AV_LOG_ERROR, "invalid header\n");                break;        }        return -1;    }    avctx->sample_rate = ctx->sampling_rate;    avctx->bit_rate = ctx->bit_rate;    /* check that reported frame size fits in input buffer */    if(ctx->frame_size > buf_size) {        av_log(avctx, AV_LOG_ERROR, "incomplete frame\n");        return -1;    }    /* channel config */    ctx->out_channels = ctx->nchans;    if (avctx->channels == 0) {        avctx->channels = ctx->out_channels;    } else if(ctx->out_channels < avctx->channels) {        av_log(avctx, AV_LOG_ERROR, "Cannot upmix AC3 from %d to %d channels.\n",               ctx->out_channels, avctx->channels);        return -1;    }    if(avctx->channels == 2) {        ctx->output_mode = AC3_ACMOD_STEREO;    } else if(avctx->channels == 1) {        ctx->output_mode = AC3_ACMOD_MONO;    } else if(avctx->channels != ctx->out_channels) {        av_log(avctx, AV_LOG_ERROR, "Cannot downmix AC3 from %d to %d channels.\n",               ctx->out_channels, avctx->channels);        return -1;    }    ctx->out_channels = avctx->channels;    /* parse the audio blocks */    for (blk = 0; blk < NB_BLOCKS; blk++) {        if (ac3_parse_audio_block(ctx, blk)) {            av_log(avctx, AV_LOG_ERROR, "error parsing the audio block\n");            *data_size = 0;            return ctx->frame_size;        }        for (i = 0; i < 256; i++)            for (ch = 0; ch < ctx->out_channels; ch++)                *(out_samples++) = ctx->int_output[ch][i];    }    *data_size = NB_BLOCKS * 256 * avctx->channels * sizeof (int16_t);    return ctx->frame_size;}/** * Uninitialize the AC-3 decoder. */static int ac3_decode_end(AVCodecContext *avctx){    AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;    ff_mdct_end(&ctx->imdct_512);    ff_mdct_end(&ctx->imdct_256);    return 0;}AVCodec ac3_decoder = {    .name = "ac3",    .type = CODEC_TYPE_AUDIO,    .id = CODEC_ID_AC3,    .priv_data_size = sizeof (AC3DecodeContext),    .init = ac3_decode_init,    .close = ac3_decode_end,    .decode = ac3_decode_frame,};

⌨️ 快捷键说明

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