comparison ac3dec.c @ 5321:fcb2a52c88fe libavcodec

AC-3 decoder, soc revision 71, Sep 4 09:47:12 2006 UTC by cloud9 Redundant code cleanup. Optimized dither generation. Minor optimization in bit allocation. Comments.
author jbr
date Sat, 14 Jul 2007 16:04:17 +0000
parents 3348a76efb67
children b55b8c792066
comparison
equal deleted inserted replaced
5320:3348a76efb67 5321:fcb2a52c88fe
10 * For exponent decoding the code is reused from liba52 by Michel Lespinasse 10 * For exponent decoding the code is reused from liba52 by Michel Lespinasse
11 * and Aaron Holtzman. 11 * and Aaron Holtzman.
12 * http://liba52.sourceforge.net 12 * http://liba52.sourceforge.net
13 * 13 *
14 * Thanks Makoto Matsumoto and Takuji Nishimura for the Mersenne Twister. 14 * Thanks Makoto Matsumoto and Takuji Nishimura for the Mersenne Twister.
15 *
16 * Kaiser-Bessel derived window by Justin Ruggles.
17 * 15 *
18 * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com). 16 * Copyright (c) 2006 Kartikey Mahendra BHATT (bhattkm at gmail dot com).
19 * Something is wrong up on cloud # 9! 17 * Something is wrong up on cloud # 9!
20 * 18 *
21 * This library is free software; you can redistribute it and/or 19 * This library is free software; you can redistribute it and/or
93 int mti; 91 int mti;
94 } dither_state; 92 } dither_state;
95 /* Mersenne Twister */ 93 /* Mersenne Twister */
96 94
97 typedef struct { 95 typedef struct {
98 uint32_t flags;
99 uint16_t crc1; 96 uint16_t crc1;
100 uint8_t fscod; 97 uint8_t fscod;
101 98
102 uint8_t acmod; 99 uint8_t acmod;
103 uint8_t cmixlev; 100 uint8_t cmixlev;
146 /* Derived Attributes. */ 143 /* Derived Attributes. */
147 int sampling_rate; 144 int sampling_rate;
148 int bit_rate; 145 int bit_rate;
149 int frame_size; 146 int frame_size;
150 147
151 int nfchans; 148 int nfchans; //number of channels
152 int lfeon; 149 int lfeon; //lfe channel in use
153 150
154 float dynrng; 151 float dynrng; //dynamic range gain
155 float dynrng2; 152 float dynrng2; //dynamic range gain for 1+1 mode
156 float chcoeffs[6]; 153 float chcoeffs[6]; //normalized channel coefficients
157 float cplco[5][18]; 154 float cplco[5][18]; //coupling coordinates
158 int ncplbnd; 155 int ncplbnd; //number of coupling bands
159 int ncplsubnd; 156 int ncplsubnd; //number of coupling sub bands
160 int cplstrtmant; 157 int cplstrtmant; //coupling start mantissa
161 int cplendmant; 158 int cplendmant; //coupling end mantissa
162 int endmant[5]; 159 int endmant[5]; //channel end mantissas
163 uint8_t dcplexps[256]; 160
164 uint8_t dexps[5][256]; 161 uint8_t dcplexps[256]; //decoded coupling exponents
165 uint8_t dlfeexps[256]; 162 uint8_t dexps[5][256]; //decoded fbw channel exponents
166 uint8_t cplbap[256]; 163 uint8_t dlfeexps[256]; //decoded lfe channel exponents
167 uint8_t bap[5][256]; 164 uint8_t cplbap[256]; //coupling bit allocation pointers
168 uint8_t lfebap[256]; 165 uint8_t bap[5][256]; //fbw channel bit allocation pointers
169 int blkoutput; 166 uint8_t lfebap[256]; //lfe channel bit allocation pointers
170 167
171 DECLARE_ALIGNED_16(float, transform_coeffs[MAX_CHANNELS][BLOCK_SIZE]); 168 int blkoutput; //output configuration for block
169
170 DECLARE_ALIGNED_16(float, transform_coeffs[MAX_CHANNELS][BLOCK_SIZE]); //transform coefficients
172 171
173 /* For IMDCT. */ 172 /* For IMDCT. */
174 MDCTContext imdct_512; //N/8 point IFFT context 173 MDCTContext imdct_512; //for 512 sample imdct transform
175 MDCTContext imdct_256; //N/4 point IFFT context 174 MDCTContext imdct_256; //for 256 sample imdct transform
176 DSPContext dsp; //for optimization 175 DSPContext dsp; //for optimization
177 DECLARE_ALIGNED_16(float, output[MAX_CHANNELS][BLOCK_SIZE]); 176
178 DECLARE_ALIGNED_16(float, delay[MAX_CHANNELS][BLOCK_SIZE]); 177 DECLARE_ALIGNED_16(float, output[MAX_CHANNELS][BLOCK_SIZE]); //output after imdct transform and windowing
179 DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]); 178 DECLARE_ALIGNED_16(float, delay[MAX_CHANNELS][BLOCK_SIZE]); //delay - added to the next block
180 DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]); 179 DECLARE_ALIGNED_16(float, tmp_imdct[BLOCK_SIZE]); //temporary storage for imdct transform
181 DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]); 180 DECLARE_ALIGNED_16(float, tmp_output[BLOCK_SIZE * 2]); //temporary storage for output before windowing
181 DECLARE_ALIGNED_16(float, window[BLOCK_SIZE]); //window coefficients
182 182
183 /* Miscellaneous. */ 183 /* Miscellaneous. */
184 GetBitContext gb; 184 GetBitContext gb;
185 dither_state dith_state; 185 dither_state dith_state; //for dither generation
186 } AC3DecodeContext; 186 } AC3DecodeContext;
187 187
188 188
189 /* BEGIN Mersenne Twister Code. */ 189 /* BEGIN Mersenne Twister Code. */
190 static void dither_seed(dither_state *state, uint32_t seed) 190 static void dither_seed(dither_state *state, uint32_t seed)
191 { 191 {
192 if (seed == 0) seed = 0x1f2e3d4c; 192 static const uint32_t mag01[2] = { 0x00, MATRIX_A };
193 uint32_t y;
194 int kk;
195
196 if (seed == 0)
197 seed = 0x7ba05e; //default seed to my birthday!
193 198
194 state->mt[0] = seed; 199 state->mt[0] = seed;
195 for (state->mti = 1; state->mti < NMT; state->mti++) 200 for (state->mti = 1; state->mti < NMT; state->mti++)
196 state->mt[state->mti] = ((69069 * state->mt[state->mti - 1]) + 1); 201 state->mt[state->mti] = ((69069 * state->mt[state->mti - 1]) + 1);
197 } 202
198 203 for (kk = 0; kk < NMT - MMT; kk++) {
199 static uint32_t dither_uint32(dither_state *state) 204 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
205 state->mt[kk] = state->mt[kk + MMT] ^ (y >> 1) ^ mag01[y & 0x01];
206 }
207 for (;kk < NMT - 1; kk++) {
208 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
209 state->mt[kk] = state->mt[kk + (MMT - NMT)] ^ (y >> 1) ^ mag01[y & 0x01];
210 }
211 y = (state->mt[NMT - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK);
212 state->mt[NMT - 1] = state->mt[MMT - 1] ^ (y >> 1) ^ mag01[y & 0x01];
213
214 state->mti = 0;
215 }
216
217 static int16_t dither_int16(dither_state *state)
200 { 218 {
201 uint32_t y; 219 uint32_t y;
202 static const uint32_t mag01[2] = { 0x00, MATRIX_A }; 220
203 int kk; 221 if (state->mti >= NMT)
204
205 if (state->mti >= NMT) {
206 for (kk = 0; kk < NMT - MMT; kk++) {
207 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
208 state->mt[kk] = state->mt[kk + MMT] ^ (y >> 1) ^ mag01[y & 0x01];
209 }
210 for (;kk < NMT - 1; kk++) {
211 y = (state->mt[kk] & UPPER_MASK) | (state->mt[kk + 1] & LOWER_MASK);
212 state->mt[kk] = state->mt[kk + (MMT - NMT)] ^ (y >> 1) ^ mag01[y & 0x01];
213 }
214 y = (state->mt[NMT - 1] & UPPER_MASK) | (state->mt[0] & LOWER_MASK);
215 state->mt[NMT - 1] = state->mt[MMT - 1] ^ (y >> 1) ^ mag01[y & 0x01];
216
217 state->mti = 0; 222 state->mti = 0;
218 }
219 223
220 y = state->mt[state->mti++]; 224 y = state->mt[state->mti++];
221 y ^= (y >> 11); 225 y ^= (y >> 11);
222 y ^= ((y << 7) & 0x9d2c5680); 226 y ^= ((y << 7) & 0x9d2c5680);
223 y ^= ((y << 15) & 0xefc60000); 227 y ^= ((y << 15) & 0xefc60000);
224 y ^= (y >> 18); 228 y ^= (y >> 18);
225 229
226 return y; 230 return ((y << 16) >> 16);
227 } 231 }
228
229 static inline int16_t dither_int16(dither_state *state)
230 {
231 return ((dither_uint32(state) << 16) >> 16);
232 }
233
234 /* END Mersenne Twister */ 232 /* END Mersenne Twister */
235 233
234 /*********** BEGIN INIT HELPER FUNCTIONS ***********/
236 /** 235 /**
237 * Generate a Kaiser-Bessel Derived Window. 236 * Generate a Kaiser-Bessel Derived Window.
238 */ 237 */
239 static void ac3_window_init(float *window) 238 static void ac3_window_init(float *window)
240 { 239 {
255 sum++; 254 sum++;
256 for (i = 0; i < 256; i++) 255 for (i = 0; i < 256; i++)
257 window[i] = sqrt(local_window[i] / sum); 256 window[i] = sqrt(local_window[i] / sum);
258 } 257 }
259 258
259 /*
260 * Generate quantizer tables.
261 */
260 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)
261 { 263 {
262 int i; 264 int i;
263 265
264 for (i = 0; i < length; i++) 266 for (i = 0; i < length; i++)
306 308
307 for (i = length1 * length2; i < size; i++) 309 for (i = length1 * length2; i < size; i++)
308 quantizers[i] = 0; 310 quantizers[i] = 0;
309 } 311 }
310 312
313 /*
314 * Initialize tables at runtime.
315 */
311 static void ac3_tables_init(void) 316 static void ac3_tables_init(void)
312 { 317 {
313 int i, j, k, l, v; 318 int i, j, k, l, v;
314 /* compute bndtab and masktab from bandsz */ 319 /* compute bndtab and masktab from bandsz */
315 k = 0; 320 k = 0;
321 l += v; 326 l += v;
322 } 327 }
323 masktab[253] = masktab[254] = masktab[255] = 0; 328 masktab[253] = masktab[254] = masktab[255] = 0;
324 bndtab[50] = 0; 329 bndtab[50] = 0;
325 330
331 /* PSD Table For Mapping Exponents To PSD. */
332 for (i = 0; i < 25; i++)
333 psdtab[i] = 3072 - (i << 7);
334
326 /* Exponent Decoding Tables */ 335 /* Exponent Decoding Tables */
327 for (i = 0; i < 5; i++) { 336 for (i = 0; i < 5; i++) {
328 v = i - 2; 337 v = i - 2;
329 for (j = 0; j < 25; j++) 338 for (j = 0; j < 25; j++)
330 exp_1[i * 25 + j] = v; 339 exp_1[i * 25 + j] = v;
364 generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128); 373 generate_quantizers_table_2(l11_quantizers_1, 11, 11, 11, 128);
365 generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128); 374 generate_quantizers_table_3(l11_quantizers_2, 11, 11, 11, 128);
366 375
367 //for level-15 quantizers 376 //for level-15 quantizers
368 generate_quantizers_table(l15_quantizers, 15, 15); 377 generate_quantizers_table(l15_quantizers, 15, 15);
378 /* End Quantizer ungrouping tables. */
379
380 //generate scale factors
381 for (i = 0; i < 25; i++)
382 scale_factors[i] = pow(2.0, -(i + 15));
369 } 383 }
370 384
371 385
372 static int ac3_decode_init(AVCodecContext *avctx) 386 static int ac3_decode_init(AVCodecContext *avctx)
373 { 387 {
374 AC3DecodeContext *ctx = avctx->priv_data; 388 AC3DecodeContext *ctx = avctx->priv_data;
375 389
376 ac3_tables_init(); 390 ac3_tables_init();
377 ff_mdct_init(&ctx->imdct_256, 8, 1); 391 ff_mdct_init(&ctx->imdct_256, 8, 1);
378 ff_mdct_init(&ctx->imdct_512, 9, 1); 392 ff_mdct_init(&ctx->imdct_512, 9, 1);
379 /* Kaiser-Bessel derived window. */
380 ac3_window_init(ctx->window); 393 ac3_window_init(ctx->window);
381 dsputil_init(&ctx->dsp, avctx); 394 dsputil_init(&ctx->dsp, avctx);
382 dither_seed(&ctx->dith_state, 0); 395 dither_seed(&ctx->dith_state, 0);
383 396
384 return 0; 397 return 0;
385 } 398 }
386 399 /*********** END INIT FUNCTIONS ***********/
400
401 /* Synchronize to ac3 bitstream.
402 * This function searches for the syncword '0xb77'.
403 *
404 * @param buf Pointer to "probable" ac3 bitstream buffer
405 * @param buf_size Size of buffer
406 * @return Returns the position where syncword is found, -1 if no syncword is found
407 */
387 static int ac3_synchronize(uint8_t *buf, int buf_size) 408 static int ac3_synchronize(uint8_t *buf, int buf_size)
388 { 409 {
389 int i; 410 int i;
390 411
391 for (i = 0; i < buf_size - 1; i++) 412 for (i = 0; i < buf_size - 1; i++)
393 return i; 414 return i;
394 415
395 return -1; 416 return -1;
396 } 417 }
397 418
398 //Returns -1 when 'fscod' is not valid; 419 /* Parse the 'sync_info' from the ac3 bitstream.
420 * This function extracts the sync_info from ac3 bitstream.
421 * GetBitContext within AC3DecodeContext must point to
422 * start of the synchronized ac3 bitstream.
423 *
424 * @param ctx AC3DecodeContext
425 * @return Returns framesize, returns 0 if fscod, frmsizecod or bsid is not valid
426 */
399 static int ac3_parse_sync_info(AC3DecodeContext *ctx) 427 static int ac3_parse_sync_info(AC3DecodeContext *ctx)
400 { 428 {
401 GetBitContext *gb = &ctx->gb; 429 GetBitContext *gb = &ctx->gb;
402 int frmsizecod, bsid; 430 int frmsizecod, bsid;
403 431
432 460
433 /* never reached */ 461 /* never reached */
434 return 0; 462 return 0;
435 } 463 }
436 464
465 /* Parse bsi from ac3 bitstream.
466 * This function extracts the bitstream information (bsi) from ac3 bitstream.
467 *
468 * @param ctx AC3DecodeContext after processed by ac3_parse_sync_info
469 */
437 static void ac3_parse_bsi(AC3DecodeContext *ctx) 470 static void ac3_parse_bsi(AC3DecodeContext *ctx)
438 { 471 {
439 GetBitContext *gb = &ctx->gb; 472 GetBitContext *gb = &ctx->gb;
440 int i; 473 int i;
441 474
488 skip_bits(gb, 8); 521 skip_bits(gb, 8);
489 } while(i--); 522 } while(i--);
490 } 523 }
491 } 524 }
492 525
493 /* Decodes the grouped exponents and stores them 526 /* Decodes the grouped exponents.
494 * in decoded exponents (dexps). 527 * This function decodes the coded exponents according to exponent strategy
495 * The code is derived from liba52. 528 * and stores them in the decoded exponents buffer.
496 * Uses liba52 tables. 529 *
530 * @param gb GetBitContext which points to start of coded exponents
531 * @param expstr Exponent coding strategy
532 * @param ngrps Number of grouped exponetns
533 * @param absexp Absolute exponent
534 * @param dexps Decoded exponents are stored in dexps
535 * @return Returns 0 if exponents are decoded successfully, -1 if error occurs
497 */ 536 */
498 static int decode_exponents(GetBitContext *gb, int expstr, int ngrps, uint8_t absexp, uint8_t *dexps) 537 static int decode_exponents(GetBitContext *gb, int expstr, int ngrps, uint8_t absexp, uint8_t *dexps)
499 { 538 {
500 int exps; 539 int exps;
501 540
549 } 588 }
550 589
551 return 0; 590 return 0;
552 } 591 }
553 592
593 /*********** HELPER FUNCTIONS FOR BIT ALLOCATION ***********/
554 static inline int logadd(int a, int b) 594 static inline int logadd(int a, int b)
555 { 595 {
556 int c = a - b; 596 int c = a - b;
557 int address; 597 int address;
558 598
581 else 621 else
582 a = FFMAX(0, (a - 128)); 622 a = FFMAX(0, (a - 128));
583 623
584 return a; 624 return a;
585 } 625 }
586 626 /*********** END HELPER FUNCTIONS FOR BIT ALLOCATION ***********/
587 /* do the bit allocation for chnl. 627
588 * chnl = 0 to 4 - fbw channel 628 /* Performs bit allocation.
589 * chnl = 5 coupling channel 629 * This function performs bit allocation for the requested chanenl.
590 * chnl = 6 lfe channel
591 */ 630 */
592 static void do_bit_allocation(AC3DecodeContext *ctx, int chnl) 631 static void do_bit_allocation(AC3DecodeContext *ctx, int chnl)
593 { 632 {
594 int16_t psd[256], bndpsd[50], excite[50], mask[50], delta; 633 int16_t psd[256], bndpsd[50], excite[50], mask[50], delta;
595 int sdecay, fdecay, sgain, dbknee, floor; 634 int sdecay, fdecay, sgain, dbknee, floor;
653 deltba = ctx->deltba[chnl]; 692 deltba = ctx->deltba[chnl];
654 } 693 }
655 } 694 }
656 695
657 for (bin = start; bin < end; bin++) /* exponent mapping into psd */ 696 for (bin = start; bin < end; bin++) /* exponent mapping into psd */
658 psd[bin] = (3072 - (exps[bin] << 7)); 697 psd[bin] = psdtab[exps[bin]];
659 698
660 /* psd integration */ 699 /* psd integration */
661 j = start; 700 j = start;
662 k = masktab[start]; 701 k = masktab[start];
663 do { 702 do {
970 } 1009 }
971 1010
972 return 0; 1011 return 0;
973 } 1012 }
974 1013
1014 /* Get the transform coefficients.
1015 * This function extracts the tranform coefficients form the ac3 bitstream.
1016 * This function is called after bit allocation is performed.
1017 */
975 static int get_transform_coeffs(AC3DecodeContext * ctx) 1018 static int get_transform_coeffs(AC3DecodeContext * ctx)
976 { 1019 {
977 int i, end; 1020 int i, end;
978 int got_cplchan = 0; 1021 int got_cplchan = 0;
979 mant_groups m; 1022 mant_groups m;
1009 } 1052 }
1010 1053
1011 return 0; 1054 return 0;
1012 } 1055 }
1013 1056
1057 /* Rematrixing routines. */
1014 static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end) 1058 static void do_rematrixing1(AC3DecodeContext *ctx, int start, int end)
1015 { 1059 {
1016 float tmp0, tmp1; 1060 float tmp0, tmp1;
1017 1061
1018 while (start < end) { 1062 while (start < end) {
1048 if (ctx->rematflg & 8) 1092 if (ctx->rematflg & 8)
1049 do_rematrixing1(ctx, bnd4, end); 1093 do_rematrixing1(ctx, bnd4, end);
1050 } 1094 }
1051 } 1095 }
1052 1096
1097 /* This function sets the normalized channel coefficients.
1098 * Transform coefficients are multipllied by the channel
1099 * coefficients to get normalized transform coefficients.
1100 */
1053 static void get_downmix_coeffs(AC3DecodeContext *ctx) 1101 static void get_downmix_coeffs(AC3DecodeContext *ctx)
1054 { 1102 {
1055 int from = ctx->acmod; 1103 int from = ctx->acmod;
1056 int to = ctx->blkoutput; 1104 int to = ctx->blkoutput;
1057 float clev = clevs[ctx->cmixlev]; 1105 float clev = clevs[ctx->cmixlev];
1215 } 1263 }
1216 break; 1264 break;
1217 } 1265 }
1218 } 1266 }
1219 1267
1268 /*********** BEGIN DOWNMIX FUNCTIONS ***********/
1220 static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx) 1269 static inline void mix_dualmono_to_mono(AC3DecodeContext *ctx)
1221 { 1270 {
1222 int i; 1271 int i;
1223 float (*output)[BLOCK_SIZE] = ctx->output; 1272 float (*output)[BLOCK_SIZE] = ctx->output;
1224 1273
1431 } 1480 }
1432 memset(output[3], 0, sizeof(output[3])); 1481 memset(output[3], 0, sizeof(output[3]));
1433 memset(output[4], 0, sizeof(output[4])); 1482 memset(output[4], 0, sizeof(output[4]));
1434 memset(output[5], 0, sizeof(output[5])); 1483 memset(output[5], 0, sizeof(output[5]));
1435 } 1484 }
1436 1485 /*********** END DOWNMIX FUNCTIONS ***********/
1486
1487 /* Downmix the output.
1488 * This function downmixes the output when the number of input
1489 * channels is not equal to the number of output channels requested.
1490 */
1437 static void do_downmix(AC3DecodeContext *ctx) 1491 static void do_downmix(AC3DecodeContext *ctx)
1438 { 1492 {
1439 int from = ctx->acmod; 1493 int from = ctx->acmod;
1440 int to = ctx->blkoutput; 1494 int to = ctx->blkoutput;
1495
1496 if (to == AC3_OUTPUT_UNMODIFIED)
1497 return;
1441 1498
1442 switch (from) { 1499 switch (from) {
1443 case AC3_INPUT_DUALMONO: 1500 case AC3_INPUT_DUALMONO:
1444 switch (to) { 1501 switch (to) {
1445 case AC3_OUTPUT_MONO: 1502 case AC3_OUTPUT_MONO:
1543 } 1600 }
1544 if ((i & 7) != 0) 1601 if ((i & 7) != 0)
1545 av_log(NULL, AV_LOG_INFO, "\n"); 1602 av_log(NULL, AV_LOG_INFO, "\n");
1546 } 1603 }
1547 1604
1548 #define CMUL(pre, pim, are, aim, bre, bim) \ 1605 /* This function performs the imdct on 256 sample transform
1549 {\ 1606 * coefficients.
1550 float _are = (are);\ 1607 */
1551 float _aim = (aim);\
1552 float _bre = (bre);\
1553 float _bim = (bim);\
1554 (pre) = _are * _bre - _aim * _bim;\
1555 (pim) = _are * _bim + _aim * _bre;\
1556 }
1557
1558 static void do_imdct_256(AC3DecodeContext *ctx, int chindex) 1608 static void do_imdct_256(AC3DecodeContext *ctx, int chindex)
1559 { 1609 {
1560 int k; 1610 int k;
1561 float x1[128], x2[128]; 1611 float x1[128], x2[128];
1562 float *ptr; 1612 float *ptr;
1573 ctx->dsp.vector_fmul_add_add(ptr, ctx->tmp_output, ctx->window, ctx->delay[chindex], 384, BLOCK_SIZE, 1); 1623 ctx->dsp.vector_fmul_add_add(ptr, ctx->tmp_output, ctx->window, ctx->delay[chindex], 384, BLOCK_SIZE, 1);
1574 ptr = ctx->delay[chindex]; 1624 ptr = ctx->delay[chindex];
1575 ctx->dsp.vector_fmul_reverse(ptr, ctx->tmp_output + 256, ctx->window, BLOCK_SIZE); 1625 ctx->dsp.vector_fmul_reverse(ptr, ctx->tmp_output + 256, ctx->window, BLOCK_SIZE);
1576 } 1626 }
1577 1627
1628 /* This function performs the imdct on 512 sample transform
1629 * coefficients.
1630 */
1578 static void do_imdct_512(AC3DecodeContext *ctx, int chindex) 1631 static void do_imdct_512(AC3DecodeContext *ctx, int chindex)
1579 { 1632 {
1580 float *ptr; 1633 float *ptr;
1581 1634
1582 ff_imdct_calc(&ctx->imdct_512, ctx->tmp_output, 1635 ff_imdct_calc(&ctx->imdct_512, ctx->tmp_output,
1585 ctx->dsp.vector_fmul_add_add(ptr, ctx->tmp_output, ctx->window, ctx->delay[chindex], 384, BLOCK_SIZE, 1); 1638 ctx->dsp.vector_fmul_add_add(ptr, ctx->tmp_output, ctx->window, ctx->delay[chindex], 384, BLOCK_SIZE, 1);
1586 ptr = ctx->delay[chindex]; 1639 ptr = ctx->delay[chindex];
1587 ctx->dsp.vector_fmul_reverse(ptr, ctx->tmp_output + 256, ctx->window, BLOCK_SIZE); 1640 ctx->dsp.vector_fmul_reverse(ptr, ctx->tmp_output + 256, ctx->window, BLOCK_SIZE);
1588 } 1641 }
1589 1642
1643 /* IMDCT Transform. */
1590 static inline void do_imdct(AC3DecodeContext *ctx) 1644 static inline void do_imdct(AC3DecodeContext *ctx)
1591 { 1645 {
1592 int i; 1646 int i;
1593 1647
1594 if (ctx->blkoutput & AC3_OUTPUT_LFEON) { 1648 if (ctx->blkoutput & AC3_OUTPUT_LFEON) {
1600 else 1654 else
1601 do_imdct_512(ctx, i + 1); 1655 do_imdct_512(ctx, i + 1);
1602 } 1656 }
1603 } 1657 }
1604 1658
1659 /* Parse the audio block from ac3 bitstream.
1660 * This function extract the audio block from the ac3 bitstream
1661 * and produces the output for the block. This function must
1662 * be called for each of the six audio block in the ac3 bitstream.
1663 */
1605 static int ac3_parse_audio_block(AC3DecodeContext * ctx) 1664 static int ac3_parse_audio_block(AC3DecodeContext * ctx)
1606 { 1665 {
1607 int nfchans = ctx->nfchans; 1666 int nfchans = ctx->nfchans;
1608 int acmod = ctx->acmod; 1667 int acmod = ctx->acmod;
1609 int i, bnd, rbnd, seg, grpsize; 1668 int i, bnd, rbnd, seg, grpsize;
1610 GetBitContext *gb = &ctx->gb; 1669 GetBitContext *gb = &ctx->gb;
1611 int bit_alloc_flags = 0; 1670 int bit_alloc_flags = 0;
1612 float drange;
1613 uint8_t *dexps; 1671 uint8_t *dexps;
1614 int mstrcplco, cplcoexp, cplcomant; 1672 int mstrcplco, cplcoexp, cplcomant;
1615 int dynrng, chbwcod, ngrps, cplabsexp, skipl; 1673 int dynrng, chbwcod, ngrps, cplabsexp, skipl;
1616 1674
1617 ctx->blksw = 0; 1675 ctx->blksw = 0;
1877 dump_floats("channel output", 10, ctx->output[i + 1], BLOCK_SIZE);*/ 1935 dump_floats("channel output", 10, ctx->output[i + 1], BLOCK_SIZE);*/
1878 1936
1879 return 0; 1937 return 0;
1880 } 1938 }
1881 1939
1882 /*static inline int16_t convert(float f)
1883 {
1884 if (f >= 1.0)
1885 return 32767;
1886 else if (f <= -1.0)
1887 return -32768;
1888 else
1889 return (lrintf(f * 32767.0));
1890 }*/
1891
1892 static inline int16_t convert(int32_t i) 1940 static inline int16_t convert(int32_t i)
1893 { 1941 {
1894 if (i > 0x43c07fff) 1942 if (i > 0x43c07fff)
1895 return 32767; 1943 return 32767;
1896 else if (i <= 0x43bf8000) 1944 else if (i <= 0x43bf8000)
1899 return (i - 0x43c00000); 1947 return (i - 0x43c00000);
1900 } 1948 }
1901 1949
1902 static int frame_count = 0; 1950 static int frame_count = 0;
1903 1951
1952 /* Decode ac3 frame.
1953 *
1954 * @param avctx Pointer to AVCodecContext
1955 * @param data Pointer to pcm smaples
1956 * @param data_size Set to number of pcm samples produced by decoding
1957 * @param buf Data to be decoded
1958 * @param buf_size Size of the buffer
1959 */
1904 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size) 1960 static int ac3_decode_frame(AVCodecContext * avctx, void *data, int *data_size, uint8_t *buf, int buf_size)
1905 { 1961 {
1906 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data; 1962 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
1907 int frame_start; 1963 int frame_start;
1908 int16_t *out_samples = (int16_t *)data; 1964 int16_t *out_samples = (int16_t *)data;
1979 } 2035 }
1980 *data_size = AUDIO_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t); 2036 *data_size = AUDIO_BLOCKS * BLOCK_SIZE * avctx->channels * sizeof (int16_t);
1981 return ctx->frame_size; 2037 return ctx->frame_size;
1982 } 2038 }
1983 2039
1984 static int ac3_decode_end(AVCodecContext *ctx) 2040 /* Uninitialize ac3 decoder.
1985 { 2041 */
2042 static int ac3_decode_end(AVCodecContext *avctx)
2043 {
2044 AC3DecodeContext *ctx = (AC3DecodeContext *)avctx->priv_data;
2045 ff_mdct_end(&ctx->imdct_512);
2046 ff_mdct_end(&ctx->imdct_256);
2047
1986 return 0; 2048 return 0;
1987 } 2049 }
1988 2050
1989 AVCodec lgpl_ac3_decoder = { 2051 AVCodec lgpl_ac3_decoder = {
1990 .name = "ac3", 2052 .name = "ac3",