Fix:
[mplayer/glamo.git] / liba52 / parse.c
blobcabfee8ecdb265d961a064b4f1d82b352706ffbb
1 /*
2 * parse.c
3 * Copyright (C) 2000-2001 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
6 * This file is part of a52dec, a free ATSC A-52 stream decoder.
7 * See http://liba52.sourceforge.net/ for updates.
9 * Modified for use with MPlayer, changes contained in liba52_changes.diff.
10 * detailed CVS changelog at http://www.mplayerhq.hu/cgi-bin/cvsweb.cgi/main/
11 * $Id$
13 * a52dec is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * a52dec is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "config.h"
30 #include <stdlib.h>
31 #include <string.h>
32 #include <inttypes.h>
34 #include "a52.h"
35 #include "a52_internal.h"
36 #include "bitstream.h"
37 #include "tables.h"
38 #include "mm_accel.h"
40 #ifdef HAVE_MEMALIGN
41 /* some systems have memalign() but no declaration for it */
42 void * memalign (size_t align, size_t size);
43 #endif
45 typedef struct {
46 sample_t q1[2];
47 sample_t q2[2];
48 sample_t q4;
49 int q1_ptr;
50 int q2_ptr;
51 int q4_ptr;
52 } quantizer_t;
54 static uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
56 sample_t * a52_init (uint32_t mm_accel)
58 sample_t * samples;
59 int i;
61 samples = memalign (16, 256 * 12 * sizeof (sample_t));
62 #if defined(__MINGW32__) && defined(HAVE_SSE)
63 for(i=0;i<10;i++){
64 if((int)samples%16){
65 sample_t* samplestmp=malloc(256 * 12 * sizeof (sample_t));
66 free(samples);
67 samples = samplestmp;
69 else break;
71 #endif
72 if(((int)samples%16) && (mm_accel&MM_ACCEL_X86_SSE)){
73 mm_accel &=~MM_ACCEL_X86_SSE;
74 printf("liba52: unable to get 16 byte aligned memory disabling usage of SSE instructions\n");
77 if (samples == NULL)
78 return NULL;
80 imdct_init (mm_accel);
81 downmix_accel_init(mm_accel);
83 for (i = 0; i < 256 * 12; i++)
84 samples[i] = 0;
86 return samples;
89 int a52_syncinfo (uint8_t * buf, int * flags,
90 int * sample_rate, int * bit_rate)
92 static int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
93 128, 160, 192, 224, 256, 320, 384, 448,
94 512, 576, 640};
95 static uint8_t lfeon[8] = {0x10, 0x10, 0x04, 0x04, 0x04, 0x01, 0x04, 0x01};
96 int frmsizecod;
97 int bitrate;
98 int half;
99 int acmod;
101 if ((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */
102 return 0;
104 if (buf[5] >= 0x60) /* bsid >= 12 */
105 return 0;
106 half = halfrate[buf[5] >> 3];
108 /* acmod, dsurmod and lfeon */
109 acmod = buf[6] >> 5;
110 *flags = ((((buf[6] & 0xf8) == 0x50) ? A52_DOLBY : acmod) |
111 ((buf[6] & lfeon[acmod]) ? A52_LFE : 0));
113 frmsizecod = buf[4] & 63;
114 if (frmsizecod >= 38)
115 return 0;
116 bitrate = rate [frmsizecod >> 1];
117 *bit_rate = (bitrate * 1000) >> half;
119 switch (buf[4] & 0xc0) {
120 case 0: /* 48 KHz */
121 *sample_rate = 48000 >> half;
122 return 4 * bitrate;
123 case 0x40:
124 *sample_rate = 44100 >> half;
125 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
126 case 0x80:
127 *sample_rate = 32000 >> half;
128 return 6 * bitrate;
129 default:
130 return 0;
134 int a52_frame (a52_state_t * state, uint8_t * buf, int * flags,
135 sample_t * level, sample_t bias)
137 static sample_t clev[4] = {LEVEL_3DB, LEVEL_45DB, LEVEL_6DB, LEVEL_45DB};
138 static sample_t slev[4] = {LEVEL_3DB, LEVEL_6DB, 0, LEVEL_6DB};
139 int chaninfo;
140 int acmod;
142 state->fscod = buf[4] >> 6;
143 state->halfrate = halfrate[buf[5] >> 3];
144 state->acmod = acmod = buf[6] >> 5;
146 bitstream_set_ptr (buf + 6);
147 bitstream_skip (3); /* skip acmod we already parsed */
149 if ((acmod == 2) && (bitstream_get (2) == 2)) /* dsurmod */
150 acmod = A52_DOLBY;
152 if ((acmod & 1) && (acmod != 1))
153 state->clev = clev[bitstream_get (2)]; /* cmixlev */
155 if (acmod & 4)
156 state->slev = slev[bitstream_get (2)]; /* surmixlev */
158 state->lfeon = bitstream_get (1);
160 state->output = downmix_init (acmod, *flags, level,
161 state->clev, state->slev);
162 if (state->output < 0)
163 return 1;
164 if (state->lfeon && (*flags & A52_LFE))
165 state->output |= A52_LFE;
166 *flags = state->output;
167 /* the 2* compensates for differences in imdct */
168 state->dynrng = state->level = 2 * *level;
169 state->bias = bias;
170 state->dynrnge = 1;
171 state->dynrngcall = NULL;
173 chaninfo = !acmod;
174 do {
175 bitstream_skip (5); /* dialnorm */
176 if (bitstream_get (1)) /* compre */
177 bitstream_skip (8); /* compr */
178 if (bitstream_get (1)) /* langcode */
179 bitstream_skip (8); /* langcod */
180 if (bitstream_get (1)) /* audprodie */
181 bitstream_skip (7); /* mixlevel + roomtyp */
182 } while (chaninfo--);
184 bitstream_skip (2); /* copyrightb + origbs */
186 if (bitstream_get (1)) /* timecod1e */
187 bitstream_skip (14); /* timecod1 */
188 if (bitstream_get (1)) /* timecod2e */
189 bitstream_skip (14); /* timecod2 */
191 if (bitstream_get (1)) { /* addbsie */
192 int addbsil;
194 addbsil = bitstream_get (6);
195 do {
196 bitstream_skip (8); /* addbsi */
197 } while (addbsil--);
200 return 0;
203 void a52_dynrng (a52_state_t * state,
204 sample_t (* call) (sample_t, void *), void * data)
206 state->dynrnge = 0;
207 if (call) {
208 state->dynrnge = 1;
209 state->dynrngcall = call;
210 state->dynrngdata = data;
214 static int parse_exponents (int expstr, int ngrps, uint8_t exponent,
215 uint8_t * dest)
217 int exps;
219 while (ngrps--) {
220 exps = bitstream_get (7);
222 exponent += exp_1[exps];
223 if (exponent > 24)
224 return 1;
226 switch (expstr) {
227 case EXP_D45:
228 *(dest++) = exponent;
229 *(dest++) = exponent;
230 case EXP_D25:
231 *(dest++) = exponent;
232 case EXP_D15:
233 *(dest++) = exponent;
236 exponent += exp_2[exps];
237 if (exponent > 24)
238 return 1;
240 switch (expstr) {
241 case EXP_D45:
242 *(dest++) = exponent;
243 *(dest++) = exponent;
244 case EXP_D25:
245 *(dest++) = exponent;
246 case EXP_D15:
247 *(dest++) = exponent;
250 exponent += exp_3[exps];
251 if (exponent > 24)
252 return 1;
254 switch (expstr) {
255 case EXP_D45:
256 *(dest++) = exponent;
257 *(dest++) = exponent;
258 case EXP_D25:
259 *(dest++) = exponent;
260 case EXP_D15:
261 *(dest++) = exponent;
265 return 0;
268 static int parse_deltba (int8_t * deltba)
270 int deltnseg, deltlen, delta, j;
272 memset (deltba, 0, 50);
274 deltnseg = bitstream_get (3);
275 j = 0;
276 do {
277 j += bitstream_get (5);
278 deltlen = bitstream_get (4);
279 delta = bitstream_get (3);
280 delta -= (delta >= 4) ? 3 : 4;
281 if (!deltlen)
282 continue;
283 if (j + deltlen >= 50)
284 return 1;
285 while (deltlen--)
286 deltba[j++] = delta;
287 } while (deltnseg--);
289 return 0;
292 static inline int zero_snr_offsets (int nfchans, a52_state_t * state)
294 int i;
296 if ((state->csnroffst) || (state->cplinu && state->cplba.fsnroffst) ||
297 (state->lfeon && state->lfeba.fsnroffst))
298 return 0;
299 for (i = 0; i < nfchans; i++)
300 if (state->ba[i].fsnroffst)
301 return 0;
302 return 1;
305 static inline int16_t dither_gen (void)
307 static uint16_t lfsr_state = 1;
308 int16_t state;
310 state = dither_lut[lfsr_state >> 8] ^ (lfsr_state << 8);
312 lfsr_state = (uint16_t) state;
314 return state;
317 static void coeff_get (sample_t * coeff, uint8_t * exp, int8_t * bap,
318 quantizer_t * quantizer, sample_t level,
319 int dither, int end)
321 int i;
322 sample_t factor[25];
324 for (i = 0; i <= 24; i++)
325 factor[i] = scale_factor[i] * level;
327 for (i = 0; i < end; i++) {
328 int bapi;
330 bapi = bap[i];
331 switch (bapi) {
332 case 0:
333 if (dither) {
334 coeff[i] = dither_gen() * LEVEL_3DB * factor[exp[i]];
335 continue;
336 } else {
337 coeff[i] = 0;
338 continue;
341 case -1:
342 if (quantizer->q1_ptr >= 0) {
343 coeff[i] = quantizer->q1[quantizer->q1_ptr--] * factor[exp[i]];
344 continue;
345 } else {
346 int code;
348 code = bitstream_get (5);
350 quantizer->q1_ptr = 1;
351 quantizer->q1[0] = q_1_2[code];
352 quantizer->q1[1] = q_1_1[code];
353 coeff[i] = q_1_0[code] * factor[exp[i]];
354 continue;
357 case -2:
358 if (quantizer->q2_ptr >= 0) {
359 coeff[i] = quantizer->q2[quantizer->q2_ptr--] * factor[exp[i]];
360 continue;
361 } else {
362 int code;
364 code = bitstream_get (7);
366 quantizer->q2_ptr = 1;
367 quantizer->q2[0] = q_2_2[code];
368 quantizer->q2[1] = q_2_1[code];
369 coeff[i] = q_2_0[code] * factor[exp[i]];
370 continue;
373 case 3:
374 coeff[i] = q_3[bitstream_get (3)] * factor[exp[i]];
375 continue;
377 case -3:
378 if (quantizer->q4_ptr == 0) {
379 quantizer->q4_ptr = -1;
380 coeff[i] = quantizer->q4 * factor[exp[i]];
381 continue;
382 } else {
383 int code;
385 code = bitstream_get (7);
387 quantizer->q4_ptr = 0;
388 quantizer->q4 = q_4_1[code];
389 coeff[i] = q_4_0[code] * factor[exp[i]];
390 continue;
393 case 4:
394 coeff[i] = q_5[bitstream_get (4)] * factor[exp[i]];
395 continue;
397 default:
398 coeff[i] = ((bitstream_get_2 (bapi) << (16 - bapi)) *
399 factor[exp[i]]);
404 static void coeff_get_coupling (a52_state_t * state, int nfchans,
405 sample_t * coeff, sample_t (* samples)[256],
406 quantizer_t * quantizer, uint8_t dithflag[5])
408 int sub_bnd, bnd, i, i_end, ch;
409 int8_t * bap;
410 uint8_t * exp;
411 sample_t cplco[5];
413 bap = state->cpl_bap;
414 exp = state->cpl_exp;
415 sub_bnd = bnd = 0;
416 i = state->cplstrtmant;
417 while (i < state->cplendmant) {
418 i_end = i + 12;
419 while (state->cplbndstrc[sub_bnd++])
420 i_end += 12;
421 for (ch = 0; ch < nfchans; ch++)
422 cplco[ch] = state->cplco[ch][bnd] * coeff[ch];
423 bnd++;
425 while (i < i_end) {
426 sample_t cplcoeff;
427 int bapi;
429 bapi = bap[i];
430 switch (bapi) {
431 case 0:
432 cplcoeff = LEVEL_3DB * scale_factor[exp[i]];
433 for (ch = 0; ch < nfchans; ch++)
434 if (state->chincpl[ch]) {
435 if (dithflag[ch])
436 samples[ch][i] = (cplcoeff * cplco[ch] *
437 dither_gen ());
438 else
439 samples[ch][i] = 0;
441 i++;
442 continue;
444 case -1:
445 if (quantizer->q1_ptr >= 0) {
446 cplcoeff = quantizer->q1[quantizer->q1_ptr--];
447 break;
448 } else {
449 int code;
451 code = bitstream_get (5);
453 quantizer->q1_ptr = 1;
454 quantizer->q1[0] = q_1_2[code];
455 quantizer->q1[1] = q_1_1[code];
456 cplcoeff = q_1_0[code];
457 break;
460 case -2:
461 if (quantizer->q2_ptr >= 0) {
462 cplcoeff = quantizer->q2[quantizer->q2_ptr--];
463 break;
464 } else {
465 int code;
467 code = bitstream_get (7);
469 quantizer->q2_ptr = 1;
470 quantizer->q2[0] = q_2_2[code];
471 quantizer->q2[1] = q_2_1[code];
472 cplcoeff = q_2_0[code];
473 break;
476 case 3:
477 cplcoeff = q_3[bitstream_get (3)];
478 break;
480 case -3:
481 if (quantizer->q4_ptr == 0) {
482 quantizer->q4_ptr = -1;
483 cplcoeff = quantizer->q4;
484 break;
485 } else {
486 int code;
488 code = bitstream_get (7);
490 quantizer->q4_ptr = 0;
491 quantizer->q4 = q_4_1[code];
492 cplcoeff = q_4_0[code];
493 break;
496 case 4:
497 cplcoeff = q_5[bitstream_get (4)];
498 break;
500 default:
501 cplcoeff = bitstream_get_2 (bapi) << (16 - bapi);
504 cplcoeff *= scale_factor[exp[i]];
505 for (ch = 0; ch < nfchans; ch++)
506 if (state->chincpl[ch])
507 samples[ch][i] = cplcoeff * cplco[ch];
508 i++;
513 int a52_block (a52_state_t * state, sample_t * samples)
515 static const uint8_t nfchans_tbl[] = {2, 1, 2, 3, 3, 4, 4, 5, 1, 1, 2};
516 static int rematrix_band[4] = {25, 37, 61, 253};
517 int i, nfchans, chaninfo;
518 uint8_t cplexpstr, chexpstr[5], lfeexpstr, do_bit_alloc, done_cpl;
519 uint8_t blksw[5], dithflag[5];
520 sample_t coeff[5];
521 int chanbias;
522 quantizer_t quantizer;
524 nfchans = nfchans_tbl[state->acmod];
526 for (i = 0; i < nfchans; i++)
527 blksw[i] = bitstream_get (1);
529 for (i = 0; i < nfchans; i++)
530 dithflag[i] = bitstream_get (1);
532 chaninfo = !(state->acmod);
533 do {
534 if (bitstream_get (1)) { /* dynrnge */
535 int dynrng;
537 dynrng = bitstream_get_2 (8);
538 if (state->dynrnge) {
539 sample_t range;
541 range = ((((dynrng & 0x1f) | 0x20) << 13) *
542 scale_factor[3 - (dynrng >> 5)]);
543 if (state->dynrngcall)
544 range = state->dynrngcall (range, state->dynrngdata);
545 state->dynrng = state->level * range;
548 } while (chaninfo--);
550 if (bitstream_get (1)) { /* cplstre */
551 state->cplinu = bitstream_get (1);
552 if (state->cplinu) {
553 static int bndtab[16] = {31, 35, 37, 39, 41, 42, 43, 44,
554 45, 45, 46, 46, 47, 47, 48, 48};
555 int cplbegf;
556 int cplendf;
557 int ncplsubnd;
559 for (i = 0; i < nfchans; i++)
560 state->chincpl[i] = bitstream_get (1);
561 switch (state->acmod) {
562 case 0: case 1:
563 return 1;
564 case 2:
565 state->phsflginu = bitstream_get (1);
567 cplbegf = bitstream_get (4);
568 cplendf = bitstream_get (4);
570 if (cplendf + 3 - cplbegf < 0)
571 return 1;
572 state->ncplbnd = ncplsubnd = cplendf + 3 - cplbegf;
573 state->cplstrtbnd = bndtab[cplbegf];
574 state->cplstrtmant = cplbegf * 12 + 37;
575 state->cplendmant = cplendf * 12 + 73;
577 for (i = 0; i < ncplsubnd - 1; i++) {
578 state->cplbndstrc[i] = bitstream_get (1);
579 state->ncplbnd -= state->cplbndstrc[i];
581 state->cplbndstrc[i] = 0; /* last value is a sentinel */
585 if (state->cplinu) {
586 int j, cplcoe;
588 cplcoe = 0;
589 for (i = 0; i < nfchans; i++)
590 if (state->chincpl[i])
591 if (bitstream_get (1)) { /* cplcoe */
592 int mstrcplco, cplcoexp, cplcomant;
594 cplcoe = 1;
595 mstrcplco = 3 * bitstream_get (2);
596 for (j = 0; j < state->ncplbnd; j++) {
597 cplcoexp = bitstream_get (4);
598 cplcomant = bitstream_get (4);
599 if (cplcoexp == 15)
600 cplcomant <<= 14;
601 else
602 cplcomant = (cplcomant | 0x10) << 13;
603 state->cplco[i][j] =
604 cplcomant * scale_factor[cplcoexp + mstrcplco];
607 if ((state->acmod == 2) && state->phsflginu && cplcoe)
608 for (j = 0; j < state->ncplbnd; j++)
609 if (bitstream_get (1)) /* phsflg */
610 state->cplco[1][j] = -state->cplco[1][j];
613 if ((state->acmod == 2) && (bitstream_get (1))) { /* rematstr */
614 int end;
616 end = (state->cplinu) ? state->cplstrtmant : 253;
617 i = 0;
619 state->rematflg[i] = bitstream_get (1);
620 while (rematrix_band[i++] < end);
623 cplexpstr = EXP_REUSE;
624 lfeexpstr = EXP_REUSE;
625 if (state->cplinu)
626 cplexpstr = bitstream_get (2);
627 for (i = 0; i < nfchans; i++)
628 chexpstr[i] = bitstream_get (2);
629 if (state->lfeon)
630 lfeexpstr = bitstream_get (1);
632 for (i = 0; i < nfchans; i++)
633 if (chexpstr[i] != EXP_REUSE) {
634 if (state->cplinu && state->chincpl[i])
635 state->endmant[i] = state->cplstrtmant;
636 else {
637 int chbwcod;
639 chbwcod = bitstream_get (6);
640 if (chbwcod > 60)
641 return 1;
642 state->endmant[i] = chbwcod * 3 + 73;
646 do_bit_alloc = 0;
648 if (cplexpstr != EXP_REUSE) {
649 int cplabsexp, ncplgrps;
651 do_bit_alloc = 64;
652 ncplgrps = ((state->cplendmant - state->cplstrtmant) /
653 (3 << (cplexpstr - 1)));
654 cplabsexp = bitstream_get (4) << 1;
655 if (parse_exponents (cplexpstr, ncplgrps, cplabsexp,
656 state->cpl_exp + state->cplstrtmant))
657 return 1;
659 for (i = 0; i < nfchans; i++)
660 if (chexpstr[i] != EXP_REUSE) {
661 int grp_size, nchgrps;
663 do_bit_alloc |= 1 << i;
664 grp_size = 3 << (chexpstr[i] - 1);
665 nchgrps = (state->endmant[i] + grp_size - 4) / grp_size;
666 state->fbw_exp[i][0] = bitstream_get (4);
667 if (parse_exponents (chexpstr[i], nchgrps, state->fbw_exp[i][0],
668 state->fbw_exp[i] + 1))
669 return 1;
670 bitstream_skip (2); /* gainrng */
672 if (lfeexpstr != EXP_REUSE) {
673 do_bit_alloc |= 32;
674 state->lfe_exp[0] = bitstream_get (4);
675 if (parse_exponents (lfeexpstr, 2, state->lfe_exp[0],
676 state->lfe_exp + 1))
677 return 1;
680 if (bitstream_get (1)) { /* baie */
681 do_bit_alloc = -1;
682 state->sdcycod = bitstream_get (2);
683 state->fdcycod = bitstream_get (2);
684 state->sgaincod = bitstream_get (2);
685 state->dbpbcod = bitstream_get (2);
686 state->floorcod = bitstream_get (3);
688 if (bitstream_get (1)) { /* snroffste */
689 do_bit_alloc = -1;
690 state->csnroffst = bitstream_get (6);
691 if (state->cplinu) {
692 state->cplba.fsnroffst = bitstream_get (4);
693 state->cplba.fgaincod = bitstream_get (3);
695 for (i = 0; i < nfchans; i++) {
696 state->ba[i].fsnroffst = bitstream_get (4);
697 state->ba[i].fgaincod = bitstream_get (3);
699 if (state->lfeon) {
700 state->lfeba.fsnroffst = bitstream_get (4);
701 state->lfeba.fgaincod = bitstream_get (3);
704 if ((state->cplinu) && (bitstream_get (1))) { /* cplleake */
705 do_bit_alloc |= 64;
706 state->cplfleak = 2304 - (bitstream_get (3) << 8);
707 state->cplsleak = 2304 - (bitstream_get (3) << 8);
710 if (bitstream_get (1)) { /* deltbaie */
711 do_bit_alloc = -1;
712 if (state->cplinu)
713 state->cplba.deltbae = bitstream_get (2);
714 for (i = 0; i < nfchans; i++)
715 state->ba[i].deltbae = bitstream_get (2);
716 if (state->cplinu && (state->cplba.deltbae == DELTA_BIT_NEW) &&
717 parse_deltba (state->cplba.deltba))
718 return 1;
719 for (i = 0; i < nfchans; i++)
720 if ((state->ba[i].deltbae == DELTA_BIT_NEW) &&
721 parse_deltba (state->ba[i].deltba))
722 return 1;
725 if (do_bit_alloc) {
726 if (zero_snr_offsets (nfchans, state)) {
727 memset (state->cpl_bap, 0, sizeof (state->cpl_bap));
728 memset (state->fbw_bap, 0, sizeof (state->fbw_bap));
729 memset (state->lfe_bap, 0, sizeof (state->lfe_bap));
730 } else {
731 if (state->cplinu && (do_bit_alloc & 64))
732 bit_allocate (state, &state->cplba, state->cplstrtbnd,
733 state->cplstrtmant, state->cplendmant,
734 state->cplfleak, state->cplsleak,
735 state->cpl_exp, state->cpl_bap);
736 for (i = 0; i < nfchans; i++)
737 if (do_bit_alloc & (1 << i))
738 bit_allocate (state, state->ba + i, 0, 0,
739 state->endmant[i], 0, 0, state->fbw_exp[i],
740 state->fbw_bap[i]);
741 if (state->lfeon && (do_bit_alloc & 32)) {
742 state->lfeba.deltbae = DELTA_BIT_NONE;
743 bit_allocate (state, &state->lfeba, 0, 0, 7, 0, 0,
744 state->lfe_exp, state->lfe_bap);
749 if (bitstream_get (1)) { /* skiple */
750 i = bitstream_get (9); /* skipl */
751 while (i--)
752 bitstream_skip (8);
755 if (state->output & A52_LFE)
756 samples += 256; /* shift for LFE channel */
758 chanbias = downmix_coeff (coeff, state->acmod, state->output,
759 state->dynrng, state->clev, state->slev);
761 quantizer.q1_ptr = quantizer.q2_ptr = quantizer.q4_ptr = -1;
762 done_cpl = 0;
764 for (i = 0; i < nfchans; i++) {
765 int j;
767 coeff_get (samples + 256 * i, state->fbw_exp[i], state->fbw_bap[i],
768 &quantizer, coeff[i], dithflag[i], state->endmant[i]);
770 if (state->cplinu && state->chincpl[i]) {
771 if (!done_cpl) {
772 done_cpl = 1;
773 coeff_get_coupling (state, nfchans, coeff,
774 (sample_t (*)[256])samples, &quantizer,
775 dithflag);
777 j = state->cplendmant;
778 } else
779 j = state->endmant[i];
781 (samples + 256 * i)[j] = 0;
782 while (++j < 256);
785 if (state->acmod == 2) {
786 int j, end, band;
788 end = ((state->endmant[0] < state->endmant[1]) ?
789 state->endmant[0] : state->endmant[1]);
791 i = 0;
792 j = 13;
793 do {
794 if (!state->rematflg[i]) {
795 j = rematrix_band[i++];
796 continue;
798 band = rematrix_band[i++];
799 if (band > end)
800 band = end;
801 do {
802 sample_t tmp0, tmp1;
804 tmp0 = samples[j];
805 tmp1 = (samples+256)[j];
806 samples[j] = tmp0 + tmp1;
807 (samples+256)[j] = tmp0 - tmp1;
808 } while (++j < band);
809 } while (j < end);
812 if (state->lfeon) {
813 if (state->output & A52_LFE) {
814 coeff_get (samples - 256, state->lfe_exp, state->lfe_bap,
815 &quantizer, state->dynrng, 0, 7);
816 for (i = 7; i < 256; i++)
817 (samples-256)[i] = 0;
818 imdct_512 (samples - 256, samples + 1536 - 256, state->bias);
819 } else {
820 /* just skip the LFE coefficients */
821 coeff_get (samples + 1280, state->lfe_exp, state->lfe_bap,
822 &quantizer, 0, 0, 7);
826 i = 0;
827 if (nfchans_tbl[state->output & A52_CHANNEL_MASK] < nfchans)
828 for (i = 1; i < nfchans; i++)
829 if (blksw[i] != blksw[0])
830 break;
832 if (i < nfchans) {
833 if (samples[2 * 1536 - 1] == (sample_t)0x776b6e21) {
834 samples[2 * 1536 - 1] = 0;
835 upmix (samples + 1536, state->acmod, state->output);
838 for (i = 0; i < nfchans; i++) {
839 sample_t bias;
841 bias = 0;
842 if (!(chanbias & (1 << i)))
843 bias = state->bias;
845 if (coeff[i]) {
846 if (blksw[i])
847 imdct_256 (samples + 256 * i, samples + 1536 + 256 * i,
848 bias);
849 else
850 imdct_512 (samples + 256 * i, samples + 1536 + 256 * i,
851 bias);
852 } else {
853 int j;
855 for (j = 0; j < 256; j++)
856 (samples + 256 * i)[j] = bias;
860 downmix (samples, state->acmod, state->output, state->bias,
861 state->clev, state->slev);
862 } else {
863 nfchans = nfchans_tbl[state->output & A52_CHANNEL_MASK];
865 downmix (samples, state->acmod, state->output, 0,
866 state->clev, state->slev);
868 if (samples[2 * 1536 - 1] != (sample_t)0x776b6e21) {
869 downmix (samples + 1536, state->acmod, state->output, 0,
870 state->clev, state->slev);
871 samples[2 * 1536 - 1] = (sample_t)0x776b6e21;
874 if (blksw[0])
875 for (i = 0; i < nfchans; i++)
876 imdct_256 (samples + 256 * i, samples + 1536 + 256 * i,
877 state->bias);
878 else
879 for (i = 0; i < nfchans; i++)
880 imdct_512 (samples + 256 * i, samples + 1536 + 256 * i,
881 state->bias);
884 return 0;