consolidate decoding of lfe and coupling channels with decoding of full-bandwidth channels.

Originally committed as revision 9910 to svn://svn.ffmpeg.org/ffmpeg/trunk
This commit is contained in:
Justin Ruggles 2007-08-04 19:19:48 +00:00
parent 365515ac7f
commit 7b4076a777
1 changed files with 136 additions and 202 deletions

View File

@ -85,6 +85,11 @@ static const float clevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_4POINT5DB,
static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB }; static const float slevs[4] = { LEVEL_MINUS_3DB, LEVEL_MINUS_6DB, LEVEL_ZERO, LEVEL_MINUS_6DB };
/* override ac3.h to include coupling channel */
#undef AC3_MAX_CHANNELS
#define AC3_MAX_CHANNELS 7
#define CPL_CH 0
#define AC3_OUTPUT_LFEON 8 #define AC3_OUTPUT_LFEON 8
typedef struct { typedef struct {
@ -103,25 +108,14 @@ typedef struct {
int rematstr; int rematstr;
int nrematbnd; int nrematbnd;
int rematflg[4]; int rematflg[4];
int cplexpstr; int expstr[AC3_MAX_CHANNELS];
int lfeexpstr; int snroffst[AC3_MAX_CHANNELS];
int chexpstr[5]; int fgain[AC3_MAX_CHANNELS];
int cplsnroffst; int deltbae[AC3_MAX_CHANNELS];
int cplfgain; int deltnseg[AC3_MAX_CHANNELS];
int snroffst[5]; uint8_t deltoffst[AC3_MAX_CHANNELS][8];
int fgain[5]; uint8_t deltlen[AC3_MAX_CHANNELS][8];
int lfesnroffst; uint8_t deltba[AC3_MAX_CHANNELS][8];
int lfefgain;
int cpldeltbae;
int deltbae[5];
int cpldeltnseg;
uint8_t cpldeltoffst[8];
uint8_t cpldeltlen[8];
uint8_t cpldeltba[8];
int deltnseg[5];
uint8_t deltoffst[5][8];
uint8_t deltlen[5][8];
uint8_t deltba[5][8];
/* Derived Attributes. */ /* Derived Attributes. */
int sampling_rate; int sampling_rate;
@ -131,27 +125,25 @@ typedef struct {
int nchans; //number of total channels int nchans; //number of total channels
int nfchans; //number of full-bandwidth channels int nfchans; //number of full-bandwidth channels
int lfeon; //lfe channel in use int lfeon; //lfe channel in use
int lfe_ch; ///< index of LFE channel
int output_mode; ///< output channel configuration int output_mode; ///< output channel configuration
int out_channels; ///< number of output channels int out_channels; ///< number of output channels
float dynrng; //dynamic range gain float dynrng; //dynamic range gain
float dynrng2; //dynamic range gain for 1+1 mode float dynrng2; //dynamic range gain for 1+1 mode
float cplco[5][18]; //coupling coordinates float cplco[AC3_MAX_CHANNELS][18]; //coupling coordinates
int ncplbnd; //number of coupling bands int ncplbnd; //number of coupling bands
int ncplsubnd; //number of coupling sub bands int ncplsubnd; //number of coupling sub bands
int cplstrtmant; //coupling start mantissa int startmant[AC3_MAX_CHANNELS]; ///< start frequency bin
int cplendmant; //coupling end mantissa int endmant[AC3_MAX_CHANNELS]; //channel end mantissas
int endmant[5]; //channel end mantissas
AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters AC3BitAllocParameters bit_alloc_params; ///< bit allocation parameters
int8_t dcplexps[256]; //decoded coupling exponents int8_t dexps[AC3_MAX_CHANNELS][256]; ///< decoded exponents
int8_t dexps[5][256]; //decoded fbw channel exponents uint8_t bap[AC3_MAX_CHANNELS][256]; ///< bit allocation pointers
int8_t dlfeexps[256]; //decoded lfe channel exponents int16_t psd[AC3_MAX_CHANNELS][256]; ///< scaled exponents
uint8_t cplbap[256]; //coupling bit allocation pointers int16_t bndpsd[AC3_MAX_CHANNELS][50]; ///< interpolated exponents
uint8_t bap[5][256]; //fbw channel bit allocation pointers int16_t mask[AC3_MAX_CHANNELS][50]; ///< masking curve values
uint8_t lfebap[256]; //lfe channel bit allocation pointers
float transform_coeffs_cpl[256];
DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]); //transform coefficients DECLARE_ALIGNED_16(float, transform_coeffs[AC3_MAX_CHANNELS][256]); //transform coefficients
/* For IMDCT. */ /* For IMDCT. */
@ -159,8 +151,8 @@ typedef struct {
MDCTContext imdct_256; //for 256 sample imdct transform MDCTContext imdct_256; //for 256 sample imdct transform
DSPContext dsp; //for optimization DSPContext dsp; //for optimization
DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS][256]); //output after imdct transform and windowing DECLARE_ALIGNED_16(float, output[AC3_MAX_CHANNELS-1][256]); //output after imdct transform and windowing
DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS][256]); //delay - added to the next block DECLARE_ALIGNED_16(float, delay[AC3_MAX_CHANNELS-1][256]); //delay - added to the next block
DECLARE_ALIGNED_16(float, tmp_imdct[256]); //temporary storage for imdct transform DECLARE_ALIGNED_16(float, tmp_imdct[256]); //temporary storage for imdct transform
DECLARE_ALIGNED_16(float, tmp_output[512]); //temporary storage for output before windowing DECLARE_ALIGNED_16(float, tmp_output[512]); //temporary storage for output before windowing
DECLARE_ALIGNED_16(float, window[256]); //window coefficients DECLARE_ALIGNED_16(float, window[256]); //window coefficients
@ -301,6 +293,7 @@ static int ac3_parse_header(AC3DecodeContext *ctx)
ctx->bit_rate = hdr.bit_rate; ctx->bit_rate = hdr.bit_rate;
ctx->nchans = hdr.channels; ctx->nchans = hdr.channels;
ctx->nfchans = ctx->nchans - ctx->lfeon; ctx->nfchans = ctx->nchans - ctx->lfeon;
ctx->lfe_ch = ctx->nfchans + 1;
ctx->frame_size = hdr.frame_size; ctx->frame_size = hdr.frame_size;
/* set default output to all source channels */ /* set default output to all source channels */
@ -401,14 +394,14 @@ static void uncouple_channels(AC3DecodeContext *ctx)
int i, j, ch, bnd, subbnd; int i, j, ch, bnd, subbnd;
subbnd = -1; subbnd = -1;
i = ctx->cplstrtmant; i = ctx->startmant[CPL_CH];
for(bnd=0; bnd<ctx->ncplbnd; bnd++) { for(bnd=0; bnd<ctx->ncplbnd; bnd++) {
do { do {
subbnd++; subbnd++;
for(j=0; j<12; j++) { for(j=0; j<12; j++) {
for(ch=1; ch<=ctx->nfchans; ch++) { for(ch=1; ch<=ctx->nfchans; ch++) {
if(ctx->chincpl[ch-1]) if(ctx->chincpl[ch])
ctx->transform_coeffs[ch][i] = ctx->transform_coeffs_cpl[i] * ctx->cplco[ch-1][bnd] * 8.0f; ctx->transform_coeffs[ch][i] = ctx->transform_coeffs[CPL_CH][i] * ctx->cplco[ch][bnd] * 8.0f;
} }
i++; i++;
} }
@ -434,25 +427,11 @@ static int get_transform_coeffs_ch(AC3DecodeContext *ctx, int ch_index, mant_gro
uint8_t *bap; uint8_t *bap;
float *coeffs; float *coeffs;
if (ch_index >= 0) { /* fbw channels */
exps = ctx->dexps[ch_index]; exps = ctx->dexps[ch_index];
bap = ctx->bap[ch_index]; bap = ctx->bap[ch_index];
coeffs = ctx->transform_coeffs[ch_index + 1]; coeffs = ctx->transform_coeffs[ch_index];
start = 0; start = ctx->startmant[ch_index];
end = ctx->endmant[ch_index]; end = ctx->endmant[ch_index];
} else if (ch_index == -1) {
exps = ctx->dlfeexps;
bap = ctx->lfebap;
coeffs = ctx->transform_coeffs[0];
start = 0;
end = 7;
} else {
exps = ctx->dcplexps;
bap = ctx->cplbap;
coeffs = ctx->transform_coeffs_cpl;
start = ctx->cplstrtmant;
end = ctx->cplendmant;
}
for (i = start; i < end; i++) { for (i = start; i < end; i++) {
@ -523,20 +502,20 @@ static void remove_dithering(AC3DecodeContext *ctx) {
uint8_t *bap; uint8_t *bap;
for(ch=1; ch<=ctx->nfchans; ch++) { for(ch=1; ch<=ctx->nfchans; ch++) {
if(!ctx->dithflag[ch-1]) { if(!ctx->dithflag[ch]) {
coeffs = ctx->transform_coeffs[ch]; coeffs = ctx->transform_coeffs[ch];
bap = ctx->bap[ch-1]; bap = ctx->bap[ch];
if(ctx->chincpl[ch-1]) if(ctx->chincpl[ch])
end = ctx->cplstrtmant; end = ctx->startmant[CPL_CH];
else else
end = ctx->endmant[ch-1]; end = ctx->endmant[ch];
for(i=0; i<end; i++) { for(i=0; i<end; i++) {
if(bap[i] == 0) if(bap[i] == 0)
coeffs[i] = 0.0f; coeffs[i] = 0.0f;
} }
if(ctx->chincpl[ch-1]) { if(ctx->chincpl[ch]) {
bap = ctx->cplbap; bap = ctx->bap[CPL_CH];
for(; i<ctx->cplendmant; i++) { for(; i<ctx->endmant[CPL_CH]; i++) {
if(bap[i] == 0) if(bap[i] == 0)
coeffs[i] = 0.0f; coeffs[i] = 0.0f;
} }
@ -551,41 +530,34 @@ static void remove_dithering(AC3DecodeContext *ctx) {
*/ */
static int get_transform_coeffs(AC3DecodeContext * ctx) static int get_transform_coeffs(AC3DecodeContext * ctx)
{ {
int ch, i, end; int ch, end;
int got_cplchan = 0; int got_cplchan = 0;
mant_groups m; mant_groups m;
m.b1ptr = m.b2ptr = m.b4ptr = 3; m.b1ptr = m.b2ptr = m.b4ptr = 3;
for (ch = 0; ch < ctx->nfchans; ch++) { for (ch = 1; ch <= ctx->nchans; ch++) {
/* transform coefficients for individual channel */ /* transform coefficients for individual channel */
if (get_transform_coeffs_ch(ctx, ch, &m)) if (get_transform_coeffs_ch(ctx, ch, &m))
return -1; return -1;
/* tranform coefficients for coupling channels */ /* tranform coefficients for coupling channels */
if (ctx->chincpl[ch]) { if (ctx->chincpl[ch]) {
if (!got_cplchan) { if (!got_cplchan) {
if (get_transform_coeffs_ch(ctx, -2, &m)) { if (get_transform_coeffs_ch(ctx, CPL_CH, &m)) {
av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n"); av_log(NULL, AV_LOG_ERROR, "error in decoupling channels\n");
return -1; return -1;
} }
uncouple_channels(ctx); uncouple_channels(ctx);
got_cplchan = 1; got_cplchan = 1;
} }
end = ctx->cplendmant; end = ctx->endmant[CPL_CH];
} else { } else {
end = ctx->endmant[ch]; end = ctx->endmant[ch];
} }
do do
ctx->transform_coeffs[ch + 1][end] = 0; ctx->transform_coeffs[ch][end] = 0;
while(++end < 256); while(++end < 256);
} }
if (ctx->lfeon) {
if (get_transform_coeffs_ch(ctx, -1, &m))
return -1;
for (i = 7; i < 256; i++) {
ctx->transform_coeffs[0][i] = 0;
}
}
/* if any channel doesn't use dithering, zero appropriate coefficients */ /* if any channel doesn't use dithering, zero appropriate coefficients */
if(!ctx->dither_all) if(!ctx->dither_all)
@ -604,7 +576,7 @@ static void do_rematrixing(AC3DecodeContext *ctx)
int end, bndend; int end, bndend;
float tmp0, tmp1; float tmp0, tmp1;
end = FFMIN(ctx->endmant[0], ctx->endmant[1]); end = FFMIN(ctx->endmant[1], ctx->endmant[2]);
for(bnd=0; bnd<ctx->nrematbnd; bnd++) { for(bnd=0; bnd<ctx->nrematbnd; bnd++) {
if(ctx->rematflg[bnd]) { if(ctx->rematflg[bnd]) {
@ -664,26 +636,23 @@ static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
static inline void do_imdct(AC3DecodeContext *ctx) static inline void do_imdct(AC3DecodeContext *ctx)
{ {
int ch; int ch;
int nchans;
if (ctx->output_mode & AC3_OUTPUT_LFEON) { nchans = ctx->nfchans;
ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output, if(ctx->output_mode & AC3_OUTPUT_LFEON)
ctx->transform_coeffs[0], ctx->tmp_imdct); nchans++;
ctx->dsp.vector_fmul_add_add(ctx->output[0], ctx->tmp_output,
ctx->window, ctx->delay[0], 384, 256, 1); for (ch=1; ch<=nchans; ch++) {
ctx->dsp.vector_fmul_reverse(ctx->delay[0], ctx->tmp_output+256, if (ctx->blksw[ch]) {
ctx->window, 256);
}
for (ch=1; ch<=ctx->nfchans; ch++) {
if (ctx->blksw[ch-1]) {
do_imdct_256(ctx, ch); do_imdct_256(ctx, ch);
} else { } else {
ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output, ctx->imdct_512.fft.imdct_calc(&ctx->imdct_512, ctx->tmp_output,
ctx->transform_coeffs[ch], ctx->transform_coeffs[ch],
ctx->tmp_imdct); ctx->tmp_imdct);
} }
ctx->dsp.vector_fmul_add_add(ctx->output[ch], ctx->tmp_output, ctx->dsp.vector_fmul_add_add(ctx->output[ch-1], ctx->tmp_output,
ctx->window, ctx->delay[ch], 384, 256, 1); ctx->window, ctx->delay[ch-1], 384, 256, 1);
ctx->dsp.vector_fmul_reverse(ctx->delay[ch], ctx->tmp_output+256, ctx->dsp.vector_fmul_reverse(ctx->delay[ch-1], ctx->tmp_output+256,
ctx->window, 256); ctx->window, 256);
} }
} }
@ -699,13 +668,15 @@ static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
int acmod = ctx->acmod; int acmod = ctx->acmod;
int i, bnd, seg, ch; int i, bnd, seg, ch;
GetBitContext *gb = &ctx->gb; GetBitContext *gb = &ctx->gb;
int bit_alloc_flags = 0; uint8_t bit_alloc_stages[AC3_MAX_CHANNELS];
for (ch = 0; ch < nfchans; ch++) /*block switch flag */ memset(bit_alloc_stages, 0, AC3_MAX_CHANNELS);
for (ch = 1; ch <= nfchans; ch++) /*block switch flag */
ctx->blksw[ch] = get_bits1(gb); ctx->blksw[ch] = get_bits1(gb);
ctx->dither_all = 1; ctx->dither_all = 1;
for (ch = 0; ch < nfchans; ch++) { /* dithering flag */ for (ch = 1; ch <= nfchans; ch++) { /* dithering flag */
ctx->dithflag[ch] = get_bits1(gb); ctx->dithflag[ch] = get_bits1(gb);
if(!ctx->dithflag[ch]) if(!ctx->dithflag[ch])
ctx->dither_all = 0; ctx->dither_all = 0;
@ -726,11 +697,12 @@ static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
} }
if (get_bits1(gb)) { /* coupling strategy */ if (get_bits1(gb)) { /* coupling strategy */
memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
ctx->cplinu = get_bits1(gb); ctx->cplinu = get_bits1(gb);
if (ctx->cplinu) { /* coupling in use */ if (ctx->cplinu) { /* coupling in use */
int cplbegf, cplendf; int cplbegf, cplendf;
for (ch = 0; ch < nfchans; ch++) for (ch = 1; ch <= nfchans; ch++)
ctx->chincpl[ch] = get_bits1(gb); ctx->chincpl[ch] = get_bits1(gb);
if (acmod == AC3_ACMOD_STEREO) if (acmod == AC3_ACMOD_STEREO)
@ -745,8 +717,8 @@ static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
} }
ctx->ncplbnd = ctx->ncplsubnd = 3 + cplendf - cplbegf; ctx->ncplbnd = ctx->ncplsubnd = 3 + cplendf - cplbegf;
ctx->cplstrtmant = cplbegf * 12 + 37; ctx->startmant[CPL_CH] = cplbegf * 12 + 37;
ctx->cplendmant = cplendf * 12 + 73; ctx->endmant[CPL_CH] = cplendf * 12 + 73;
for (bnd = 0; bnd < ctx->ncplsubnd - 1; bnd++) { /* coupling band structure */ for (bnd = 0; bnd < ctx->ncplsubnd - 1; bnd++) { /* coupling band structure */
if (get_bits1(gb)) { if (get_bits1(gb)) {
ctx->cplbndstrc[bnd] = 1; ctx->cplbndstrc[bnd] = 1;
@ -754,7 +726,7 @@ static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
} }
} }
} else { } else {
for (ch = 0; ch < nfchans; ch++) for (ch = 1; ch <= nfchans; ch++)
ctx->chincpl[ch] = 0; ctx->chincpl[ch] = 0;
} }
} }
@ -762,7 +734,7 @@ static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
if (ctx->cplinu) { if (ctx->cplinu) {
int cplcoe = 0; int cplcoe = 0;
for (ch = 0; ch < nfchans; ch++) { for (ch = 1; ch <= nfchans; ch++) {
if (ctx->chincpl[ch]) { if (ctx->chincpl[ch]) {
if (get_bits1(gb)) { /* coupling co-ordinates */ if (get_bits1(gb)) { /* coupling co-ordinates */
int mstrcplco, cplcoexp, cplcomant; int mstrcplco, cplcoexp, cplcomant;
@ -784,7 +756,7 @@ static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && cplcoe) { if (acmod == AC3_ACMOD_STEREO && ctx->phsflginu && cplcoe) {
for (bnd = 0; bnd < ctx->ncplbnd; bnd++) { for (bnd = 0; bnd < ctx->ncplbnd; bnd++) {
if (get_bits1(gb)) if (get_bits1(gb))
ctx->cplco[1][bnd] = -ctx->cplco[1][bnd]; ctx->cplco[2][bnd] = -ctx->cplco[2][bnd];
} }
} }
} }
@ -793,26 +765,30 @@ static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
ctx->rematstr = get_bits1(gb); ctx->rematstr = get_bits1(gb);
if (ctx->rematstr) { if (ctx->rematstr) {
ctx->nrematbnd = 4; ctx->nrematbnd = 4;
if(ctx->cplinu && ctx->cplstrtmant <= 61) if(ctx->cplinu && ctx->startmant[CPL_CH] <= 61)
ctx->nrematbnd -= 1 + (ctx->cplstrtmant == 37); ctx->nrematbnd -= 1 + (ctx->startmant[CPL_CH] == 37);
for(bnd=0; bnd<ctx->nrematbnd; bnd++) for(bnd=0; bnd<ctx->nrematbnd; bnd++)
ctx->rematflg[bnd] = get_bits1(gb); ctx->rematflg[bnd] = get_bits1(gb);
} }
} }
ctx->cplexpstr = EXP_REUSE; ctx->expstr[CPL_CH] = EXP_REUSE;
ctx->lfeexpstr = EXP_REUSE; ctx->expstr[ctx->lfe_ch] = EXP_REUSE;
if (ctx->cplinu) /* coupling exponent strategy */ for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {
ctx->cplexpstr = get_bits(gb, 2); if(ch == ctx->lfe_ch)
for (ch = 0; ch < nfchans; ch++) /* channel exponent strategy */ ctx->expstr[ch] = get_bits(gb, 1);
ctx->chexpstr[ch] = get_bits(gb, 2); else
if (ctx->lfeon) /* lfe exponent strategy */ ctx->expstr[ch] = get_bits(gb, 2);
ctx->lfeexpstr = get_bits1(gb); if(ctx->expstr[ch] != EXP_REUSE)
bit_alloc_stages[ch] = 3;
}
for (ch = 0; ch < nfchans; ch++) { /* channel bandwidth code */ for (ch = 1; ch <= nfchans; ch++) { /* channel bandwidth code */
if (ctx->chexpstr[ch] != EXP_REUSE) { ctx->startmant[ch] = 0;
if (ctx->expstr[ch] != EXP_REUSE) {
int prev = ctx->endmant[ch];
if (ctx->chincpl[ch]) if (ctx->chincpl[ch])
ctx->endmant[ch] = ctx->cplstrtmant; ctx->endmant[ch] = ctx->startmant[CPL_CH];
else { else {
int chbwcod = get_bits(gb, 6); int chbwcod = get_bits(gb, 6);
if (chbwcod > 60) { if (chbwcod > 60) {
@ -821,102 +797,69 @@ static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
} }
ctx->endmant[ch] = chbwcod * 3 + 73; 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;
if (ctx->cplexpstr != EXP_REUSE) {/* coupling exponents */ for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) {
int grpsize, ngrps, absexp; if (ctx->expstr[ch] != EXP_REUSE) {
bit_alloc_flags = 64; int grpsize, ngrps;
grpsize = 3 << (ctx->cplexpstr - 1); grpsize = 3 << (ctx->expstr[ch] - 1);
ngrps = (ctx->cplendmant - ctx->cplstrtmant) / grpsize; if(ch == CPL_CH)
absexp = get_bits(gb, 4) << 1; ngrps = (ctx->endmant[ch] - ctx->startmant[ch]) / grpsize;
decode_exponents(gb, ctx->cplexpstr, ngrps, absexp, &ctx->dcplexps[ctx->cplstrtmant]); else if(ch == ctx->lfe_ch)
} ngrps = 2;
else
for (ch = 0; ch < nfchans; ch++) { /* fbw channel exponents */
if (ctx->chexpstr[ch] != EXP_REUSE) {
int grpsize, ngrps, absexp;
bit_alloc_flags |= 1 << ch;
grpsize = 3 << (ctx->chexpstr[ch] - 1);
ngrps = (ctx->endmant[ch] + grpsize - 4) / grpsize; ngrps = (ctx->endmant[ch] + grpsize - 4) / grpsize;
absexp = ctx->dexps[ch][0] = get_bits(gb, 4); ctx->dexps[ch][0] = get_bits(gb, 4) << !ch;
decode_exponents(gb, ctx->chexpstr[ch], ngrps, absexp, &ctx->dexps[ch][1]); 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 */ skip_bits(gb, 2); /* skip gainrng */
} }
} }
if (ctx->lfeexpstr != EXP_REUSE) { /* lfe exponents */
int ngrps, absexp;
bit_alloc_flags |= 32;
ngrps = 2;
absexp = ctx->dlfeexps[0] = get_bits(gb, 4);
decode_exponents(gb, ctx->lfeexpstr, ngrps, absexp, &ctx->dlfeexps[1]);
}
if (get_bits1(gb)) { /* bit allocation information */ if (get_bits1(gb)) { /* bit allocation information */
bit_alloc_flags = 127;
ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)]; ctx->bit_alloc_params.sdecay = ff_sdecaytab[get_bits(gb, 2)];
ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)]; ctx->bit_alloc_params.fdecay = ff_fdecaytab[get_bits(gb, 2)];
ctx->bit_alloc_params.sgain = ff_sgaintab[get_bits(gb, 2)]; 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.dbknee = ff_dbkneetab[get_bits(gb, 2)];
ctx->bit_alloc_params.floor = ff_floortab[get_bits(gb, 3)]; 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);
}
} }
if (get_bits1(gb)) { /* snroffset */ if (get_bits1(gb)) { /* snroffset */
int csnr; int csnr;
bit_alloc_flags = 127;
csnr = (get_bits(gb, 6) - 15) << 4; csnr = (get_bits(gb, 6) - 15) << 4;
if (ctx->cplinu) { /* coupling fine snr offset and fast gain code */ for (ch = !ctx->cplinu; ch <= ctx->nchans; ch++) { /* snr offset and fast gain */
ctx->cplsnroffst = (csnr + get_bits(gb, 4)) << 2;
ctx->cplfgain = ff_fgaintab[get_bits(gb, 3)];
}
for (ch = 0; ch < nfchans; ch++) { /* channel fine snr offset and fast gain code */
ctx->snroffst[ch] = (csnr + get_bits(gb, 4)) << 2; ctx->snroffst[ch] = (csnr + get_bits(gb, 4)) << 2;
ctx->fgain[ch] = ff_fgaintab[get_bits(gb, 3)]; ctx->fgain[ch] = ff_fgaintab[get_bits(gb, 3)];
} }
if (ctx->lfeon) { /* lfe fine snr offset and fast gain code */ memset(bit_alloc_stages, 3, AC3_MAX_CHANNELS);
ctx->lfesnroffst = (csnr + get_bits(gb, 4)) << 2;
ctx->lfefgain = ff_fgaintab[get_bits(gb, 3)];
}
} }
if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */ if (ctx->cplinu && get_bits1(gb)) { /* coupling leak information */
bit_alloc_flags |= 64;
ctx->bit_alloc_params.cplfleak = get_bits(gb, 3); ctx->bit_alloc_params.cplfleak = get_bits(gb, 3);
ctx->bit_alloc_params.cplsleak = 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);
} }
if (get_bits1(gb)) { /* delta bit allocation information */ if (get_bits1(gb)) { /* delta bit allocation information */
bit_alloc_flags = 127; for (ch = !ctx->cplinu; ch <= nfchans; ch++) {
if (ctx->cplinu) {
ctx->cpldeltbae = get_bits(gb, 2);
if (ctx->cpldeltbae == DBA_RESERVED) {
av_log(NULL, AV_LOG_ERROR, "coupling delta bit allocation strategy reserved\n");
return -1;
}
}
for (ch = 0; ch < nfchans; ch++) {
ctx->deltbae[ch] = get_bits(gb, 2); ctx->deltbae[ch] = get_bits(gb, 2);
if (ctx->deltbae[ch] == DBA_RESERVED) { if (ctx->deltbae[ch] == DBA_RESERVED) {
av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n"); av_log(NULL, AV_LOG_ERROR, "delta bit allocation strategy reserved\n");
return -1; return -1;
} }
bit_alloc_stages[ch] = FFMAX(bit_alloc_stages[ch], 2);
} }
if (ctx->cplinu) { for (ch = !ctx->cplinu; ch <= nfchans; ch++) {
if (ctx->cpldeltbae == DBA_NEW) { /*coupling delta offset, len and bit allocation */
ctx->cpldeltnseg = get_bits(gb, 3);
for (seg = 0; seg <= ctx->cpldeltnseg; seg++) {
ctx->cpldeltoffst[seg] = get_bits(gb, 5);
ctx->cpldeltlen[seg] = get_bits(gb, 4);
ctx->cpldeltba[seg] = get_bits(gb, 3);
}
}
}
for (ch = 0; ch < nfchans; ch++) {
if (ctx->deltbae[ch] == DBA_NEW) {/*channel delta offset, len and bit allocation */ if (ctx->deltbae[ch] == DBA_NEW) {/*channel delta offset, len and bit allocation */
ctx->deltnseg[ch] = get_bits(gb, 3); ctx->deltnseg[ch] = get_bits(gb, 3);
for (seg = 0; seg <= ctx->deltnseg[ch]; seg++) { for (seg = 0; seg <= ctx->deltnseg[ch]; seg++) {
@ -927,38 +870,35 @@ static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
} }
} }
} else if(blk == 0) { } else if(blk == 0) {
if(ctx->cplinu) for(ch=0; ch<=ctx->nchans; ch++) {
ctx->cpldeltbae = DBA_NONE;
for(ch=0; ch<nfchans; ch++) {
ctx->deltbae[ch] = DBA_NONE; ctx->deltbae[ch] = DBA_NONE;
} }
} }
if (bit_alloc_flags) { for(ch=!ctx->cplinu; ch<=ctx->nchans; ch++) {
if (ctx->cplinu && (bit_alloc_flags & 64)) { if(bit_alloc_stages[ch] > 2) {
ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->cplbap, /* Exponent mapping into PSD and PSD integration */
ctx->dcplexps, ctx->cplstrtmant, ff_ac3_bit_alloc_calc_psd(ctx->dexps[ch],
ctx->cplendmant, ctx->cplsnroffst, ctx->startmant[ch], ctx->endmant[ch],
ctx->cplfgain, 0, ctx->psd[ch], ctx->bndpsd[ch]);
ctx->cpldeltbae, ctx->cpldeltnseg,
ctx->cpldeltoffst, ctx->cpldeltlen,
ctx->cpldeltba);
} }
for (ch = 0; ch < nfchans; ch++) { if(bit_alloc_stages[ch] > 1) {
if ((bit_alloc_flags >> ch) & 1) { /* Compute excitation function, Compute masking curve, and
ac3_parametric_bit_allocation(&ctx->bit_alloc_params, Apply delta bit allocation */
ctx->bap[ch], ctx->dexps[ch], 0, ff_ac3_bit_alloc_calc_mask(&ctx->bit_alloc_params, ctx->bndpsd[ch],
ctx->endmant[ch], ctx->snroffst[ch], ctx->startmant[ch], ctx->endmant[ch],
ctx->fgain[ch], 0, ctx->deltbae[ch], ctx->fgain[ch], (ch == ctx->lfe_ch),
ctx->deltnseg[ch], ctx->deltoffst[ch], ctx->deltbae[ch], ctx->deltnseg[ch],
ctx->deltlen[ch], ctx->deltba[ch]); ctx->deltoffst[ch], ctx->deltlen[ch],
} ctx->deltba[ch], ctx->mask[ch]);
} }
if (ctx->lfeon && (bit_alloc_flags & 32)) { if(bit_alloc_stages[ch] > 0) {
ac3_parametric_bit_allocation(&ctx->bit_alloc_params, ctx->lfebap, /* Compute bit allocation */
ctx->dlfeexps, 0, 7, ctx->lfesnroffst, ff_ac3_bit_alloc_calc_bap(ctx->mask[ch], ctx->psd[ch],
ctx->lfefgain, 1, ctx->startmant[ch], ctx->endmant[ch],
DBA_NONE, 0, NULL, NULL, NULL); ctx->snroffst[ch],
ctx->bit_alloc_params.floor,
ctx->bap[ch]);
} }
} }
@ -980,19 +920,14 @@ static int ac3_parse_audio_block(AC3DecodeContext *ctx, int blk)
do_rematrixing(ctx); do_rematrixing(ctx);
/* apply scaling to coefficients (headroom, dynrng) */ /* apply scaling to coefficients (headroom, dynrng) */
if(ctx->lfeon) { for(ch=1; ch<=ctx->nchans; ch++) {
for(i=0; i<7; i++) {
ctx->transform_coeffs[0][i] *= 2.0f * ctx->dynrng;
}
}
for(ch=1; ch<=ctx->nfchans; ch++) {
float gain = 2.0f; float gain = 2.0f;
if(ctx->acmod == AC3_ACMOD_DUALMONO && ch == 2) { if(ctx->acmod == AC3_ACMOD_DUALMONO && ch == 2) {
gain *= ctx->dynrng2; gain *= ctx->dynrng2;
} else { } else {
gain *= ctx->dynrng; gain *= ctx->dynrng;
} }
for(i=0; i<ctx->endmant[ch-1]; i++) { for(i=0; i<ctx->endmant[ch]; i++) {
ctx->transform_coeffs[ch][i] *= gain; ctx->transform_coeffs[ch][i] *= gain;
} }
} }
@ -1024,7 +959,7 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
{ {
AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data; AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
int16_t *out_samples = (int16_t *)data; int16_t *out_samples = (int16_t *)data;
int i, blk, ch, start; int i, blk, ch;
int32_t *int_ptr[6]; int32_t *int_ptr[6];
for (ch = 0; ch < 6; ch++) for (ch = 0; ch < 6; ch++)
@ -1062,9 +997,8 @@ static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size,
*data_size = 0; *data_size = 0;
return ctx->frame_size; return ctx->frame_size;
} }
start = (ctx->output_mode & AC3_OUTPUT_LFEON) ? 0 : 1;
for (i = 0; i < 256; i++) for (i = 0; i < 256; i++)
for (ch = start; ch <= ctx->nfchans; ch++) for (ch = 0; ch < ctx->out_channels; ch++)
*(out_samples++) = convert(int_ptr[ch][i]); *(out_samples++) = convert(int_ptr[ch][i]);
} }
*data_size = NB_BLOCKS * 256 * avctx->channels * sizeof (int16_t); *data_size = NB_BLOCKS * 256 * avctx->channels * sizeof (int16_t);