comparison ac3dec.c @ 5319:40af705cef7e libavcodec

AC-3 decoder, soc revision 69, Aug 31 07:12:56 2006 UTC by cloud9 Fix the bugs: 1. The quality of output because of incorrect windowing coefficients. New code for window generation. 2. Dynrng values were reset where dynrng value is present in the first block, but not in the subsequent block.
author jbr
date Sat, 14 Jul 2007 16:03:14 +0000
parents ef4ef249ca72
children 3348a76efb67
comparison
equal deleted inserted replaced
5318:ef4ef249ca72 5319:40af705cef7e
149 int frame_size; 149 int frame_size;
150 150
151 int nfchans; 151 int nfchans;
152 int lfeon; 152 int lfeon;
153 153
154 float dynrng;
155 float dynrng2;
154 float chcoeffs[6]; 156 float chcoeffs[6];
155 float cplco[5][18]; 157 float cplco[5][18];
156 int ncplbnd; 158 int ncplbnd;
157 int ncplsubnd; 159 int ncplsubnd;
158 int cplstrtmant; 160 int cplstrtmant;
174 DSPContext dsp; //for optimization 176 DSPContext dsp; //for optimization
175 DECLARE_ALIGNED_16(float, output[MAX_CHANNELS][BLOCK_SIZE]); 177 DECLARE_ALIGNED_16(float, output[MAX_CHANNELS][BLOCK_SIZE]);
176 DECLARE_ALIGNED_16(float, delay[MAX_CHANNELS][BLOCK_SIZE]); 178 DECLARE_ALIGNED_16(float, delay[MAX_CHANNELS][BLOCK_SIZE]);
177 DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]); 179 DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]);
178 DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]); 180 DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]);
181 DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]);
179 182
180 /* Miscellaneous. */ 183 /* Miscellaneous. */
181 GetBitContext gb; 184 GetBitContext gb;
182 dither_state dith_state; 185 dither_state dith_state;
183 } AC3DecodeContext; 186 } AC3DecodeContext;
230 } 233 }
231 234
232 /* END Mersenne Twister */ 235 /* END Mersenne Twister */
233 236
234 /** 237 /**
235 * Generate a Kaiser Window. 238 * Generate a Kaiser-Bessel Derived Window.
236 */ 239 */
237 static void 240 static void
238 k_window_init(int alpha, float *window, int n, int iter) 241 ac3_window_init(float *window)
239 { 242 {
240 int j, k; 243 int i, j;
241 float a, x; 244 double sum = 0.0, bessel, tmp;
242 a = alpha * M_PI / n; 245 double local_window[256];
243 a = a*a; 246 double alpha2 = (5.0 * M_PI / 256.0) * (5.0 * M_PI / 256.0);
244 for(k=0; k<n; k++) { 247
245 x = k * (n - k) * a; 248 for (i = 0; i < 256; i++) {
246 window[k] = 1.0; 249 tmp = i * (256 - i) * alpha2;
247 for(j=iter; j>0; j--) { 250 bessel = 1.0;
248 window[k] = (window[k] * x / (j*j)) + 1.0; 251 for (j = 100; j > 0; j--) /* defaul to 100 iterations */
249 } 252 bessel = bessel * tmp / (j * j) + 1;
250 } 253 sum += bessel;
251 } 254 local_window[i] = sum;
252 255 }
253 /** 256
254 * Generate a Kaiser-Bessel Derived Window. 257 sum++;
255 * @param alpha determines window shape 258 for (i = 0; i < 256; i++)
256 * @param window array to fill with window values 259 window[i] = sqrt(local_window[i] / sum);
257 * @param n length of the window
258 * @param iter number of iterations to use in BesselI0
259 */
260 static void
261 kbd_window_init(int alpha, float *window, int n, int iter)
262 {
263 int k, n2;
264 float *kwindow;
265
266 n2 = n >> 1;
267 kwindow = &window[n2];
268 k_window_init(alpha, kwindow, n2, iter);
269 window[0] = kwindow[0];
270 for(k=1; k<n2; k++) {
271 window[k] = window[k-1] + kwindow[k];
272 }
273 for(k=0; k<n2; k++) {
274 window[k] = sqrt(window[k] / (window[n2-1]+1));
275 window[n-1-k] = window[k];
276 }
277 } 260 }
278 261
279 static void generate_quantizers_table(int16_t quantizers[], int level, int length) 262 static void generate_quantizers_table(int16_t quantizers[], int level, int length)
280 { 263 {
281 int i; 264 int i;
383 generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128); 366 generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
384 generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128); 367 generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
385 368
386 //for level-15 quantizers 369 //for level-15 quantizers
387 generate_quantizers_table(l15_quantizers, 15, 15); 370 generate_quantizers_table(l15_quantizers, 15, 15);
388
389 /* Kaiser-Bessel derived window. */
390 kbd_window_init(5, window, 256, 100);
391 } 371 }
392 372
393 373
394 static int ac3_decode_init(AVCodecContext *avctx) 374 static int ac3_decode_init(AVCodecContext *avctx)
395 { 375 {
396 AC3DecodeContext *ctx = avctx->priv_data; 376 AC3DecodeContext *ctx = avctx->priv_data;
397 377
398 ac3_tables_init(); 378 ac3_tables_init();
399 ff_mdct_init(&ctx->imdct_256, 8, 1); 379 ff_mdct_init(&ctx->imdct_256, 8, 1);
400 ff_mdct_init(&ctx->imdct_512, 9, 1); 380 ff_mdct_init(&ctx->imdct_512, 9, 1);
381 /* Kaiser-Bessel derived window. */
382 ac3_window_init(ctx->window);
401 dsputil_init(&ctx->dsp, avctx); 383 dsputil_init(&ctx->dsp, avctx);
402 dither_seed(&ctx->dith_state, 0); 384 dither_seed(&ctx->dith_state, 0);
403 385
404 return 0; 386 return 0;
405 } 387 }
467 ctx->cpldeltnseg = 0; 449 ctx->cpldeltnseg = 0;
468 for (i = 0; i < 5; i++) { 450 for (i = 0; i < 5; i++) {
469 ctx->deltbae[i] = AC3_DBASTR_NONE; 451 ctx->deltbae[i] = AC3_DBASTR_NONE;
470 ctx->deltnseg[i] = 0; 452 ctx->deltnseg[i] = 0;
471 } 453 }
454 ctx->dynrng = 1.0;
455 ctx->dynrng2 = 1.0;
472 456
473 ctx->acmod = get_bits(gb, 3); 457 ctx->acmod = get_bits(gb, 3);
474 ctx->nfchans = nfchans_tbl[ctx->acmod]; 458 ctx->nfchans = nfchans_tbl[ctx->acmod];
475 459
476 if (ctx->acmod & 0x01 && ctx->acmod != 0x01) 460 if (ctx->acmod & 0x01 && ctx->acmod != 0x01)
1073 int from = ctx->acmod; 1057 int from = ctx->acmod;
1074 int to = ctx->blkoutput; 1058 int to = ctx->blkoutput;
1075 float clev = clevs[ctx->cmixlev]; 1059 float clev = clevs[ctx->cmixlev];
1076 float slev = slevs[ctx->surmixlev]; 1060 float slev = slevs[ctx->surmixlev];
1077 float nf = 1.0; //normalization factor for downmix coeffs 1061 float nf = 1.0; //normalization factor for downmix coeffs
1062 int i;
1063
1064 if (!ctx->acmod) {
1065 ctx->chcoeffs[0] = 2 * ctx->dynrng;
1066 ctx->chcoeffs[1] = 2 * ctx->dynrng2;
1067 } else {
1068 for (i = 0; i < ctx->nfchans; i++)
1069 ctx->chcoeffs[i] = 2 * ctx->dynrng;
1070 }
1078 1071
1079 if (to == AC3_OUTPUT_UNMODIFIED) 1072 if (to == AC3_OUTPUT_UNMODIFIED)
1080 return; 1073 return;
1081 1074
1082 switch (from) { 1075 switch (from) {
1577 1570
1578 ff_imdct_calc(&ctx->imdct_256, ctx->tmp_output, x1, ctx->tmp_imdct); 1571 ff_imdct_calc(&ctx->imdct_256, ctx->tmp_output, x1, ctx->tmp_imdct);
1579 ff_imdct_calc(&ctx->imdct_256, ctx->tmp_output + 256, x2, ctx->tmp_imdct); 1572 ff_imdct_calc(&ctx->imdct_256, ctx->tmp_output + 256, x2, ctx->tmp_imdct);
1580 1573
1581 ptr = ctx->output[chindex]; 1574 ptr = ctx->output[chindex];
1582 ctx->dsp.vector_fmul_add_add(ptr, ctx->tmp_output, window, ctx->delay[chindex], 384, BLOCK_SIZE, 1); 1575 ctx->dsp.vector_fmul_add_add(ptr, ctx->tmp_output, ctx->window, ctx->delay[chindex], 384, BLOCK_SIZE, 1);
1583 ptr = ctx->delay[chindex]; 1576 ptr = ctx->delay[chindex];
1584 ctx->dsp.vector_fmul_reverse(ptr, ctx->tmp_output + 256, window, BLOCK_SIZE); 1577 ctx->dsp.vector_fmul_reverse(ptr, ctx->tmp_output + 256, ctx->window, BLOCK_SIZE);
1585 } 1578 }
1586 1579
1587 static void do_imdct_512(AC3DecodeContext *ctx, int chindex) 1580 static void do_imdct_512(AC3DecodeContext *ctx, int chindex)
1588 { 1581 {
1589 float *ptr; 1582 float *ptr;
1590 1583
1591 ff_imdct_calc(&ctx->imdct_512, ctx->tmp_output, 1584 ff_imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1592 ctx->transform_coeffs[chindex], ctx->tmp_imdct); 1585 ctx->transform_coeffs[chindex], ctx->tmp_imdct);
1593 ptr = ctx->output[chindex]; 1586 ptr = ctx->output[chindex];
1594 ctx->dsp.vector_fmul_add_add(ptr, ctx->tmp_output, window, ctx->delay[chindex], 384, BLOCK_SIZE, 1); 1587 ctx->dsp.vector_fmul_add_add(ptr, ctx->tmp_output, ctx->window, ctx->delay[chindex], 384, BLOCK_SIZE, 1);
1595 ptr = ctx->delay[chindex]; 1588 ptr = ctx->delay[chindex];
1596 ctx->dsp.vector_fmul_reverse(ptr, ctx->tmp_output + 256, window, BLOCK_SIZE); 1589 ctx->dsp.vector_fmul_reverse(ptr, ctx->tmp_output + 256, ctx->window, BLOCK_SIZE);
1597 } 1590 }
1598 1591
1599 static inline void do_imdct(AC3DecodeContext *ctx) 1592 static inline void do_imdct(AC3DecodeContext *ctx)
1600 { 1593 {
1601 int i; 1594 int i;
1621 float drange; 1614 float drange;
1622 uint8_t *dexps; 1615 uint8_t *dexps;
1623 int mstrcplco, cplcoexp, cplcomant; 1616 int mstrcplco, cplcoexp, cplcomant;
1624 int dynrng, chbwcod, ngrps, cplabsexp, skipl; 1617 int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1625 1618
1626 for (i = 0; i < 5; i++)
1627 ctx->chcoeffs[i] = 2.0;
1628
1629 ctx->blksw = 0; 1619 ctx->blksw = 0;
1630 for (i = 0; i < nfchans; i++) /*block switch flag */ 1620 for (i = 0; i < nfchans; i++) /*block switch flag */
1631 ctx->blksw |= get_bits1(gb) << i; 1621 ctx->blksw |= get_bits1(gb) << i;
1632 1622
1633 ctx->dithflag = 0; 1623 ctx->dithflag = 0;
1634 for (i = 0; i < nfchans; i++) /* dithering flag */ 1624 for (i = 0; i < nfchans; i++) /* dithering flag */
1635 ctx->dithflag |= get_bits1(gb) << i; 1625 ctx->dithflag |= get_bits1(gb) << i;
1636 1626
1637 if (get_bits1(gb)) { /* dynamic range */ 1627 if (get_bits1(gb)) { /* dynamic range */
1638 dynrng = get_sbits(gb, 8); 1628 dynrng = get_sbits(gb, 8);
1639 drange = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]); 1629 ctx->dynrng = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1640 for (i = 0; i < nfchans; i++)
1641 ctx->chcoeffs[i] *= drange;
1642 } 1630 }
1643 1631
1644 if (acmod == 0x00 && get_bits1(gb)) { /* dynamic range 1+1 mode */ 1632 if (acmod == 0x00 && get_bits1(gb)) { /* dynamic range 1+1 mode */
1645 dynrng = get_sbits(gb, 8); 1633 dynrng = get_sbits(gb, 8);
1646 drange = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]); 1634 ctx->dynrng2 = ((((dynrng & 0x1f) | 0x20) << 13) * scale_factors[3 - (dynrng >> 5)]);
1647 ctx->chcoeffs[1] *= drange;
1648 } 1635 }
1649 1636
1650 get_downmix_coeffs(ctx); 1637 get_downmix_coeffs(ctx);
1651 1638
1652 if (get_bits1(gb)) { /* coupling strategy */ 1639 if (get_bits1(gb)) { /* coupling strategy */