Fixes
[mplayer/glamo.git] / liba52 / parse.c
blob974e12ae0e401c5a2f4aaf8eb070b8af87fe644f
1 /*
2 * parse.c
3 * Copyright (C) 2000-2002 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 changelog at http://svn.mplayerhq.hu/mplayer/trunk/
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 <stdio.h>
32 #include <string.h>
33 #include <inttypes.h>
35 #include "a52.h"
36 #include "a52_internal.h"
37 #include "bitstream.h"
38 #include "tables.h"
39 #include "mm_accel.h"
41 #ifdef HAVE_MEMALIGN
42 /* some systems have memalign() but no declaration for it */
43 void * memalign (size_t align, size_t size);
44 #endif
46 typedef struct {
47 sample_t q1[2];
48 sample_t q2[2];
49 sample_t q4;
50 int q1_ptr;
51 int q2_ptr;
52 int q4_ptr;
53 } quantizer_t;
55 static uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
57 a52_state_t * a52_init (uint32_t mm_accel)
59 a52_state_t * state;
60 int i;
62 state = malloc (sizeof (a52_state_t));
63 if (state == NULL)
64 return NULL;
66 state->samples = memalign (16, 256 * 12 * sizeof (sample_t));
67 #if defined(__MINGW32__) && defined(HAVE_SSE)
68 for(i=0;i<10;i++){
69 if((int)state->samples%16){
70 sample_t* samplestmp=malloc(256 * 12 * sizeof (sample_t));
71 free(state->samples);
72 state->samples = samplestmp;
74 else break;
76 #endif
77 if(((int)state->samples%16) && (mm_accel&MM_ACCEL_X86_SSE)){
78 mm_accel &=~MM_ACCEL_X86_SSE;
79 fprintf(stderr, "liba52: unable to get 16 byte aligned memory disabling usage of SSE instructions\n");
82 if (state->samples == NULL) {
83 free (state);
84 return NULL;
87 for (i = 0; i < 256 * 12; i++)
88 state->samples[i] = 0;
90 state->downmixed = 1;
92 state->lfsr_state = 1;
94 a52_imdct_init (mm_accel);
95 downmix_accel_init(mm_accel);
97 return state;
100 sample_t * a52_samples (a52_state_t * state)
102 return state->samples;
105 int a52_syncinfo (uint8_t * buf, int * flags,
106 int * sample_rate, int * bit_rate)
108 static int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
109 128, 160, 192, 224, 256, 320, 384, 448,
110 512, 576, 640};
111 static uint8_t lfeon[8] = {0x10, 0x10, 0x04, 0x04, 0x04, 0x01, 0x04, 0x01};
112 int frmsizecod;
113 int bitrate;
114 int half;
115 int acmod;
117 if ((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */
118 return 0;
120 if (buf[5] >= 0x60) /* bsid >= 12 */
121 return 0;
122 half = halfrate[buf[5] >> 3];
124 /* acmod, dsurmod and lfeon */
125 acmod = buf[6] >> 5;
126 *flags = ((((buf[6] & 0xf8) == 0x50) ? A52_DOLBY : acmod) |
127 ((buf[6] & lfeon[acmod]) ? A52_LFE : 0));
129 frmsizecod = buf[4] & 63;
130 if (frmsizecod >= 38)
131 return 0;
132 bitrate = rate [frmsizecod >> 1];
133 *bit_rate = (bitrate * 1000) >> half;
135 switch (buf[4] & 0xc0) {
136 case 0:
137 *sample_rate = 48000 >> half;
138 return 4 * bitrate;
139 case 0x40:
140 *sample_rate = 44100 >> half;
141 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
142 case 0x80:
143 *sample_rate = 32000 >> half;
144 return 6 * bitrate;
145 default:
146 return 0;
150 int a52_frame (a52_state_t * state, uint8_t * buf, int * flags,
151 sample_t * level, sample_t bias)
153 static sample_t clev[4] = {LEVEL_3DB, LEVEL_45DB, LEVEL_6DB, LEVEL_45DB};
154 static sample_t slev[4] = {LEVEL_3DB, LEVEL_6DB, 0, LEVEL_6DB};
155 int chaninfo;
156 int acmod;
158 state->fscod = buf[4] >> 6;
159 state->halfrate = halfrate[buf[5] >> 3];
160 state->acmod = acmod = buf[6] >> 5;
162 a52_bitstream_set_ptr (state, buf + 6);
163 bitstream_skip (state, 3); /* skip acmod we already parsed */
165 if ((acmod == 2) && (bitstream_get (state, 2) == 2)) /* dsurmod */
166 acmod = A52_DOLBY;
168 if ((acmod & 1) && (acmod != 1))
169 state->clev = clev[bitstream_get (state, 2)]; /* cmixlev */
171 if (acmod & 4)
172 state->slev = slev[bitstream_get (state, 2)]; /* surmixlev */
174 state->lfeon = bitstream_get (state, 1);
176 state->output = a52_downmix_init (acmod, *flags, level,
177 state->clev, state->slev);
178 if (state->output < 0)
179 return 1;
180 if (state->lfeon && (*flags & A52_LFE))
181 state->output |= A52_LFE;
182 *flags = state->output;
183 /* the 2* compensates for differences in imdct */
184 state->dynrng = state->level = 2 * *level;
185 state->bias = bias;
186 state->dynrnge = 1;
187 state->dynrngcall = NULL;
188 state->cplba.deltbae = DELTA_BIT_NONE;
189 state->ba[0].deltbae = state->ba[1].deltbae = state->ba[2].deltbae =
190 state->ba[3].deltbae = state->ba[4].deltbae = DELTA_BIT_NONE;
192 chaninfo = !acmod;
193 do {
194 bitstream_skip (state, 5); /* dialnorm */
195 if (bitstream_get (state, 1)) /* compre */
196 bitstream_skip (state, 8); /* compr */
197 if (bitstream_get (state, 1)) /* langcode */
198 bitstream_skip (state, 8); /* langcod */
199 if (bitstream_get (state, 1)) /* audprodie */
200 bitstream_skip (state, 7); /* mixlevel + roomtyp */
201 } while (chaninfo--);
203 bitstream_skip (state, 2); /* copyrightb + origbs */
205 if (bitstream_get (state, 1)) /* timecod1e */
206 bitstream_skip (state, 14); /* timecod1 */
207 if (bitstream_get (state, 1)) /* timecod2e */
208 bitstream_skip (state, 14); /* timecod2 */
210 if (bitstream_get (state, 1)) { /* addbsie */
211 int addbsil;
213 addbsil = bitstream_get (state, 6);
214 do {
215 bitstream_skip (state, 8); /* addbsi */
216 } while (addbsil--);
219 return 0;
222 void a52_dynrng (a52_state_t * state,
223 sample_t (* call) (sample_t, void *), void * data)
225 state->dynrnge = 0;
226 if (call) {
227 state->dynrnge = 1;
228 state->dynrngcall = call;
229 state->dynrngdata = data;
233 static int parse_exponents (a52_state_t * state, int expstr, int ngrps,
234 uint8_t exponent, uint8_t * dest)
236 int exps;
238 while (ngrps--) {
239 exps = bitstream_get (state, 7);
241 exponent += exp_1[exps];
242 if (exponent > 24)
243 return 1;
245 switch (expstr) {
246 case EXP_D45:
247 *(dest++) = exponent;
248 *(dest++) = exponent;
249 case EXP_D25:
250 *(dest++) = exponent;
251 case EXP_D15:
252 *(dest++) = exponent;
255 exponent += exp_2[exps];
256 if (exponent > 24)
257 return 1;
259 switch (expstr) {
260 case EXP_D45:
261 *(dest++) = exponent;
262 *(dest++) = exponent;
263 case EXP_D25:
264 *(dest++) = exponent;
265 case EXP_D15:
266 *(dest++) = exponent;
269 exponent += exp_3[exps];
270 if (exponent > 24)
271 return 1;
273 switch (expstr) {
274 case EXP_D45:
275 *(dest++) = exponent;
276 *(dest++) = exponent;
277 case EXP_D25:
278 *(dest++) = exponent;
279 case EXP_D15:
280 *(dest++) = exponent;
284 return 0;
287 static int parse_deltba (a52_state_t * state, int8_t * deltba)
289 int deltnseg, deltlen, delta, j;
291 memset (deltba, 0, 50);
293 deltnseg = bitstream_get (state, 3);
294 j = 0;
295 do {
296 j += bitstream_get (state, 5);
297 deltlen = bitstream_get (state, 4);
298 delta = bitstream_get (state, 3);
299 delta -= (delta >= 4) ? 3 : 4;
300 if (!deltlen)
301 continue;
302 if (j + deltlen >= 50)
303 return 1;
304 while (deltlen--)
305 deltba[j++] = delta;
306 } while (deltnseg--);
308 return 0;
311 static inline int zero_snr_offsets (int nfchans, a52_state_t * state)
313 int i;
315 if ((state->csnroffst) ||
316 (state->chincpl && state->cplba.bai >> 3) || /* cplinu, fsnroffst */
317 (state->lfeon && state->lfeba.bai >> 3)) /* fsnroffst */
318 return 0;
319 for (i = 0; i < nfchans; i++)
320 if (state->ba[i].bai >> 3) /* fsnroffst */
321 return 0;
322 return 1;
325 static inline int16_t dither_gen (a52_state_t * state)
327 int16_t nstate;
329 nstate = dither_lut[state->lfsr_state >> 8] ^ (state->lfsr_state << 8);
331 state->lfsr_state = (uint16_t) nstate;
333 return nstate;
336 static void coeff_get (a52_state_t * state, sample_t * coeff,
337 expbap_t * expbap, quantizer_t * quantizer,
338 sample_t level, int dither, int end)
340 int i;
341 uint8_t * exp;
342 int8_t * bap;
343 sample_t factor[25];
345 for (i = 0; i <= 24; i++)
346 factor[i] = scale_factor[i] * level;
348 exp = expbap->exp;
349 bap = expbap->bap;
351 for (i = 0; i < end; i++) {
352 int bapi;
354 bapi = bap[i];
355 switch (bapi) {
356 case 0:
357 if (dither) {
358 coeff[i] = dither_gen (state) * LEVEL_3DB * factor[exp[i]];
359 continue;
360 } else {
361 coeff[i] = 0;
362 continue;
365 case -1:
366 if (quantizer->q1_ptr >= 0) {
367 coeff[i] = quantizer->q1[quantizer->q1_ptr--] * factor[exp[i]];
368 continue;
369 } else {
370 int code;
372 code = bitstream_get (state, 5);
374 quantizer->q1_ptr = 1;
375 quantizer->q1[0] = q_1_2[code];
376 quantizer->q1[1] = q_1_1[code];
377 coeff[i] = q_1_0[code] * factor[exp[i]];
378 continue;
381 case -2:
382 if (quantizer->q2_ptr >= 0) {
383 coeff[i] = quantizer->q2[quantizer->q2_ptr--] * factor[exp[i]];
384 continue;
385 } else {
386 int code;
388 code = bitstream_get (state, 7);
390 quantizer->q2_ptr = 1;
391 quantizer->q2[0] = q_2_2[code];
392 quantizer->q2[1] = q_2_1[code];
393 coeff[i] = q_2_0[code] * factor[exp[i]];
394 continue;
397 case 3:
398 coeff[i] = q_3[bitstream_get (state, 3)] * factor[exp[i]];
399 continue;
401 case -3:
402 if (quantizer->q4_ptr == 0) {
403 quantizer->q4_ptr = -1;
404 coeff[i] = quantizer->q4 * factor[exp[i]];
405 continue;
406 } else {
407 int code;
409 code = bitstream_get (state, 7);
411 quantizer->q4_ptr = 0;
412 quantizer->q4 = q_4_1[code];
413 coeff[i] = q_4_0[code] * factor[exp[i]];
414 continue;
417 case 4:
418 coeff[i] = q_5[bitstream_get (state, 4)] * factor[exp[i]];
419 continue;
421 default:
422 coeff[i] = ((bitstream_get_2 (state, bapi) << (16 - bapi)) *
423 factor[exp[i]]);
428 static void coeff_get_coupling (a52_state_t * state, int nfchans,
429 sample_t * coeff, sample_t (* samples)[256],
430 quantizer_t * quantizer, uint8_t dithflag[5])
432 int cplbndstrc, bnd, i, i_end, ch;
433 uint8_t * exp;
434 int8_t * bap;
435 sample_t cplco[5];
437 exp = state->cpl_expbap.exp;
438 bap = state->cpl_expbap.bap;
439 bnd = 0;
440 cplbndstrc = state->cplbndstrc;
441 i = state->cplstrtmant;
442 while (i < state->cplendmant) {
443 i_end = i + 12;
444 while (cplbndstrc & 1) {
445 cplbndstrc >>= 1;
446 i_end += 12;
448 cplbndstrc >>= 1;
449 for (ch = 0; ch < nfchans; ch++)
450 cplco[ch] = state->cplco[ch][bnd] * coeff[ch];
451 bnd++;
453 while (i < i_end) {
454 sample_t cplcoeff;
455 int bapi;
457 bapi = bap[i];
458 switch (bapi) {
459 case 0:
460 cplcoeff = LEVEL_3DB * scale_factor[exp[i]];
461 for (ch = 0; ch < nfchans; ch++)
462 if ((state->chincpl >> ch) & 1) {
463 if (dithflag[ch])
464 samples[ch][i] = (cplcoeff * cplco[ch] *
465 dither_gen (state));
466 else
467 samples[ch][i] = 0;
469 i++;
470 continue;
472 case -1:
473 if (quantizer->q1_ptr >= 0) {
474 cplcoeff = quantizer->q1[quantizer->q1_ptr--];
475 break;
476 } else {
477 int code;
479 code = bitstream_get (state, 5);
481 quantizer->q1_ptr = 1;
482 quantizer->q1[0] = q_1_2[code];
483 quantizer->q1[1] = q_1_1[code];
484 cplcoeff = q_1_0[code];
485 break;
488 case -2:
489 if (quantizer->q2_ptr >= 0) {
490 cplcoeff = quantizer->q2[quantizer->q2_ptr--];
491 break;
492 } else {
493 int code;
495 code = bitstream_get (state, 7);
497 quantizer->q2_ptr = 1;
498 quantizer->q2[0] = q_2_2[code];
499 quantizer->q2[1] = q_2_1[code];
500 cplcoeff = q_2_0[code];
501 break;
504 case 3:
505 cplcoeff = q_3[bitstream_get (state, 3)];
506 break;
508 case -3:
509 if (quantizer->q4_ptr == 0) {
510 quantizer->q4_ptr = -1;
511 cplcoeff = quantizer->q4;
512 break;
513 } else {
514 int code;
516 code = bitstream_get (state, 7);
518 quantizer->q4_ptr = 0;
519 quantizer->q4 = q_4_1[code];
520 cplcoeff = q_4_0[code];
521 break;
524 case 4:
525 cplcoeff = q_5[bitstream_get (state, 4)];
526 break;
528 default:
529 cplcoeff = bitstream_get_2 (state, bapi) << (16 - bapi);
532 cplcoeff *= scale_factor[exp[i]];
533 for (ch = 0; ch < nfchans; ch++)
534 if ((state->chincpl >> ch) & 1)
535 samples[ch][i] = cplcoeff * cplco[ch];
536 i++;
541 int a52_block (a52_state_t * state)
543 static const uint8_t nfchans_tbl[] = {2, 1, 2, 3, 3, 4, 4, 5, 1, 1, 2};
544 static int rematrix_band[4] = {25, 37, 61, 253};
545 int i, nfchans, chaninfo;
546 uint8_t cplexpstr, chexpstr[5], lfeexpstr, do_bit_alloc, done_cpl;
547 uint8_t blksw[5], dithflag[5];
548 sample_t coeff[5];
549 int chanbias;
550 quantizer_t quantizer;
551 sample_t * samples;
553 nfchans = nfchans_tbl[state->acmod];
555 for (i = 0; i < nfchans; i++)
556 blksw[i] = bitstream_get (state, 1);
558 for (i = 0; i < nfchans; i++)
559 dithflag[i] = bitstream_get (state, 1);
561 chaninfo = !state->acmod;
562 do {
563 if (bitstream_get (state, 1)) { /* dynrnge */
564 int dynrng;
566 dynrng = bitstream_get_2 (state, 8);
567 if (state->dynrnge) {
568 sample_t range;
570 range = ((((dynrng & 0x1f) | 0x20) << 13) *
571 scale_factor[3 - (dynrng >> 5)]);
572 if (state->dynrngcall)
573 range = state->dynrngcall (range, state->dynrngdata);
574 state->dynrng = state->level * range;
577 } while (chaninfo--);
579 if (bitstream_get (state, 1)) { /* cplstre */
580 state->chincpl = 0;
581 if (bitstream_get (state, 1)) { /* cplinu */
582 static uint8_t bndtab[16] = {31, 35, 37, 39, 41, 42, 43, 44,
583 45, 45, 46, 46, 47, 47, 48, 48};
584 int cplbegf;
585 int cplendf;
586 int ncplsubnd;
588 for (i = 0; i < nfchans; i++)
589 state->chincpl |= bitstream_get (state, 1) << i;
590 switch (state->acmod) {
591 case 0: case 1:
592 return 1;
593 case 2:
594 state->phsflginu = bitstream_get (state, 1);
596 cplbegf = bitstream_get (state, 4);
597 cplendf = bitstream_get (state, 4);
599 if (cplendf + 3 - cplbegf < 0)
600 return 1;
601 state->ncplbnd = ncplsubnd = cplendf + 3 - cplbegf;
602 state->cplstrtbnd = bndtab[cplbegf];
603 state->cplstrtmant = cplbegf * 12 + 37;
604 state->cplendmant = cplendf * 12 + 73;
606 state->cplbndstrc = 0;
607 for (i = 0; i < ncplsubnd - 1; i++)
608 if (bitstream_get (state, 1)) {
609 state->cplbndstrc |= 1 << i;
610 state->ncplbnd--;
615 if (state->chincpl) { /* cplinu */
616 int j, cplcoe;
618 cplcoe = 0;
619 for (i = 0; i < nfchans; i++)
620 if ((state->chincpl) >> i & 1)
621 if (bitstream_get (state, 1)) { /* cplcoe */
622 int mstrcplco, cplcoexp, cplcomant;
624 cplcoe = 1;
625 mstrcplco = 3 * bitstream_get (state, 2);
626 for (j = 0; j < state->ncplbnd; j++) {
627 cplcoexp = bitstream_get (state, 4);
628 cplcomant = bitstream_get (state, 4);
629 if (cplcoexp == 15)
630 cplcomant <<= 14;
631 else
632 cplcomant = (cplcomant | 0x10) << 13;
633 state->cplco[i][j] =
634 cplcomant * scale_factor[cplcoexp + mstrcplco];
637 if ((state->acmod == 2) && state->phsflginu && cplcoe)
638 for (j = 0; j < state->ncplbnd; j++)
639 if (bitstream_get (state, 1)) /* phsflg */
640 state->cplco[1][j] = -state->cplco[1][j];
643 if ((state->acmod == 2) && (bitstream_get (state, 1))) { /* rematstr */
644 int end;
646 state->rematflg = 0;
647 end = (state->chincpl) ? state->cplstrtmant : 253; /* cplinu */
648 i = 0;
650 state->rematflg |= bitstream_get (state, 1) << i;
651 while (rematrix_band[i++] < end);
654 cplexpstr = EXP_REUSE;
655 lfeexpstr = EXP_REUSE;
656 if (state->chincpl) /* cplinu */
657 cplexpstr = bitstream_get (state, 2);
658 for (i = 0; i < nfchans; i++)
659 chexpstr[i] = bitstream_get (state, 2);
660 if (state->lfeon)
661 lfeexpstr = bitstream_get (state, 1);
663 for (i = 0; i < nfchans; i++)
664 if (chexpstr[i] != EXP_REUSE) {
665 if ((state->chincpl >> i) & 1)
666 state->endmant[i] = state->cplstrtmant;
667 else {
668 int chbwcod;
670 chbwcod = bitstream_get (state, 6);
671 if (chbwcod > 60)
672 return 1;
673 state->endmant[i] = chbwcod * 3 + 73;
677 do_bit_alloc = 0;
679 if (cplexpstr != EXP_REUSE) {
680 int cplabsexp, ncplgrps;
682 do_bit_alloc = 64;
683 ncplgrps = ((state->cplendmant - state->cplstrtmant) /
684 (3 << (cplexpstr - 1)));
685 cplabsexp = bitstream_get (state, 4) << 1;
686 if (parse_exponents (state, cplexpstr, ncplgrps, cplabsexp,
687 state->cpl_expbap.exp + state->cplstrtmant))
688 return 1;
690 for (i = 0; i < nfchans; i++)
691 if (chexpstr[i] != EXP_REUSE) {
692 int grp_size, nchgrps;
694 do_bit_alloc |= 1 << i;
695 grp_size = 3 << (chexpstr[i] - 1);
696 nchgrps = (state->endmant[i] + grp_size - 4) / grp_size;
697 state->fbw_expbap[i].exp[0] = bitstream_get (state, 4);
698 if (parse_exponents (state, chexpstr[i], nchgrps,
699 state->fbw_expbap[i].exp[0],
700 state->fbw_expbap[i].exp + 1))
701 return 1;
702 bitstream_skip (state, 2); /* gainrng */
704 if (lfeexpstr != EXP_REUSE) {
705 do_bit_alloc |= 32;
706 state->lfe_expbap.exp[0] = bitstream_get (state, 4);
707 if (parse_exponents (state, lfeexpstr, 2, state->lfe_expbap.exp[0],
708 state->lfe_expbap.exp + 1))
709 return 1;
712 if (bitstream_get (state, 1)) { /* baie */
713 do_bit_alloc = -1;
714 state->bai = bitstream_get (state, 11);
716 if (bitstream_get (state, 1)) { /* snroffste */
717 do_bit_alloc = -1;
718 state->csnroffst = bitstream_get (state, 6);
719 if (state->chincpl) /* cplinu */
720 state->cplba.bai = bitstream_get (state, 7);
721 for (i = 0; i < nfchans; i++)
722 state->ba[i].bai = bitstream_get (state, 7);
723 if (state->lfeon)
724 state->lfeba.bai = bitstream_get (state, 7);
726 if ((state->chincpl) && (bitstream_get (state, 1))) { /* cplleake */
727 do_bit_alloc |= 64;
728 state->cplfleak = 9 - bitstream_get (state, 3);
729 state->cplsleak = 9 - bitstream_get (state, 3);
732 if (bitstream_get (state, 1)) { /* deltbaie */
733 do_bit_alloc = -1;
734 if (state->chincpl) /* cplinu */
735 state->cplba.deltbae = bitstream_get (state, 2);
736 for (i = 0; i < nfchans; i++)
737 state->ba[i].deltbae = bitstream_get (state, 2);
738 if (state->chincpl && /* cplinu */
739 (state->cplba.deltbae == DELTA_BIT_NEW) &&
740 parse_deltba (state, state->cplba.deltba))
741 return 1;
742 for (i = 0; i < nfchans; i++)
743 if ((state->ba[i].deltbae == DELTA_BIT_NEW) &&
744 parse_deltba (state, state->ba[i].deltba))
745 return 1;
748 if (do_bit_alloc) {
749 if (zero_snr_offsets (nfchans, state)) {
750 memset (state->cpl_expbap.bap, 0, sizeof (state->cpl_expbap.bap));
751 for (i = 0; i < nfchans; i++)
752 memset (state->fbw_expbap[i].bap, 0,
753 sizeof (state->fbw_expbap[i].bap));
754 memset (state->lfe_expbap.bap, 0, sizeof (state->lfe_expbap.bap));
755 } else {
756 if (state->chincpl && (do_bit_alloc & 64)) /* cplinu */
757 a52_bit_allocate (state, &state->cplba, state->cplstrtbnd,
758 state->cplstrtmant, state->cplendmant,
759 state->cplfleak << 8, state->cplsleak << 8,
760 &state->cpl_expbap);
761 for (i = 0; i < nfchans; i++)
762 if (do_bit_alloc & (1 << i))
763 a52_bit_allocate (state, state->ba + i, 0, 0,
764 state->endmant[i], 0, 0,
765 state->fbw_expbap +i);
766 if (state->lfeon && (do_bit_alloc & 32)) {
767 state->lfeba.deltbae = DELTA_BIT_NONE;
768 a52_bit_allocate (state, &state->lfeba, 0, 0, 7, 0, 0,
769 &state->lfe_expbap);
774 if (bitstream_get (state, 1)) { /* skiple */
775 i = bitstream_get (state, 9); /* skipl */
776 while (i--)
777 bitstream_skip (state, 8);
780 samples = state->samples;
781 if (state->output & A52_LFE)
782 samples += 256; /* shift for LFE channel */
784 chanbias = a52_downmix_coeff (coeff, state->acmod, state->output,
785 state->dynrng, state->clev, state->slev);
787 quantizer.q1_ptr = quantizer.q2_ptr = quantizer.q4_ptr = -1;
788 done_cpl = 0;
790 for (i = 0; i < nfchans; i++) {
791 int j;
793 coeff_get (state, samples + 256 * i, state->fbw_expbap +i, &quantizer,
794 coeff[i], dithflag[i], state->endmant[i]);
796 if ((state->chincpl >> i) & 1) {
797 if (!done_cpl) {
798 done_cpl = 1;
799 coeff_get_coupling (state, nfchans, coeff,
800 (sample_t (*)[256])samples, &quantizer,
801 dithflag);
803 j = state->cplendmant;
804 } else
805 j = state->endmant[i];
807 (samples + 256 * i)[j] = 0;
808 while (++j < 256);
811 if (state->acmod == 2) {
812 int j, end, band, rematflg;
814 end = ((state->endmant[0] < state->endmant[1]) ?
815 state->endmant[0] : state->endmant[1]);
817 i = 0;
818 j = 13;
819 rematflg = state->rematflg;
820 do {
821 if (! (rematflg & 1)) {
822 rematflg >>= 1;
823 j = rematrix_band[i++];
824 continue;
826 rematflg >>= 1;
827 band = rematrix_band[i++];
828 if (band > end)
829 band = end;
830 do {
831 sample_t tmp0, tmp1;
833 tmp0 = samples[j];
834 tmp1 = (samples+256)[j];
835 samples[j] = tmp0 + tmp1;
836 (samples+256)[j] = tmp0 - tmp1;
837 } while (++j < band);
838 } while (j < end);
841 if (state->lfeon) {
842 if (state->output & A52_LFE) {
843 coeff_get (state, samples - 256, &state->lfe_expbap, &quantizer,
844 state->dynrng, 0, 7);
845 for (i = 7; i < 256; i++)
846 (samples-256)[i] = 0;
847 a52_imdct_512 (samples - 256, samples + 1536 - 256, state->bias);
848 } else {
849 /* just skip the LFE coefficients */
850 coeff_get (state, samples + 1280, &state->lfe_expbap, &quantizer,
851 0, 0, 7);
855 i = 0;
856 if (nfchans_tbl[state->output & A52_CHANNEL_MASK] < nfchans)
857 for (i = 1; i < nfchans; i++)
858 if (blksw[i] != blksw[0])
859 break;
861 if (i < nfchans) {
862 if (state->downmixed) {
863 state->downmixed = 0;
864 a52_upmix (samples + 1536, state->acmod, state->output);
867 for (i = 0; i < nfchans; i++) {
868 sample_t bias;
870 bias = 0;
871 if (!(chanbias & (1 << i)))
872 bias = state->bias;
874 if (coeff[i]) {
875 if (blksw[i])
876 a52_imdct_256 (samples + 256 * i, samples + 1536 + 256 * i,
877 bias);
878 else
879 a52_imdct_512 (samples + 256 * i, samples + 1536 + 256 * i,
880 bias);
881 } else {
882 int j;
884 for (j = 0; j < 256; j++)
885 (samples + 256 * i)[j] = bias;
889 a52_downmix (samples, state->acmod, state->output, state->bias,
890 state->clev, state->slev);
891 } else {
892 nfchans = nfchans_tbl[state->output & A52_CHANNEL_MASK];
894 a52_downmix (samples, state->acmod, state->output, 0,
895 state->clev, state->slev);
897 if (!state->downmixed) {
898 state->downmixed = 1;
899 a52_downmix (samples + 1536, state->acmod, state->output, 0,
900 state->clev, state->slev);
903 if (blksw[0])
904 for (i = 0; i < nfchans; i++)
905 a52_imdct_256 (samples + 256 * i, samples + 1536 + 256 * i,
906 state->bias);
907 else
908 for (i = 0; i < nfchans; i++)
909 a52_imdct_512 (samples + 256 * i, samples + 1536 + 256 * i,
910 state->bias);
913 return 0;
916 void a52_free (a52_state_t * state)
918 free (state->samples);
919 free (state);