summaryrefslogtreecommitdiff
path: root/apps/codecs/liba52/parse.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/liba52/parse.c')
-rw-r--r--apps/codecs/liba52/parse.c946
1 files changed, 946 insertions, 0 deletions
diff --git a/apps/codecs/liba52/parse.c b/apps/codecs/liba52/parse.c
new file mode 100644
index 0000000000..505a335f09
--- /dev/null
+++ b/apps/codecs/liba52/parse.c
@@ -0,0 +1,946 @@
1/*
2 * parse.c
3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5 *
6 * This file is part of a52dec, a free ATSC A-52 stream decoder.
7 * See http://liba52.sourceforge.net/ for updates.
8 *
9 * a52dec is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * a52dec is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#include "config.h"
25
26#include <stdlib.h>
27#include <string.h>
28#include <inttypes.h>
29
30#include "a52.h"
31#include "a52_internal.h"
32#include "bitstream.h"
33#include "tables.h"
34
35#if defined(HAVE_MEMALIGN) && !defined(__cplusplus)
36/* some systems have memalign() but no declaration for it */
37void * memalign (size_t align, size_t size);
38#else
39/* assume malloc alignment is sufficient */
40#define memalign(align,size) malloc (size)
41#endif
42
43typedef struct {
44 quantizer_t q1[2];
45 quantizer_t q2[2];
46 quantizer_t q4;
47 int q1_ptr;
48 int q2_ptr;
49 int q4_ptr;
50} quantizer_set_t;
51
52static uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
53
54a52_state_t * a52_init (uint32_t mm_accel)
55{
56 a52_state_t * state;
57 int i;
58
59 state = (a52_state_t *) malloc (sizeof (a52_state_t));
60 if (state == NULL)
61 return NULL;
62
63 state->samples = (sample_t *) memalign (16, 256 * 12 * sizeof (sample_t));
64 if (state->samples == NULL) {
65 free (state);
66 return NULL;
67 }
68
69 for (i = 0; i < 256 * 12; i++)
70 state->samples[i] = 0;
71
72 state->downmixed = 1;
73
74 state->lfsr_state = 1;
75
76 a52_imdct_init (mm_accel);
77
78 return state;
79}
80
81sample_t * a52_samples (a52_state_t * state)
82{
83 return state->samples;
84}
85
86int a52_syncinfo (uint8_t * buf, int * flags,
87 int * sample_rate, int * bit_rate)
88{
89 static int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
90 128, 160, 192, 224, 256, 320, 384, 448,
91 512, 576, 640};
92 static uint8_t lfeon[8] = {0x10, 0x10, 0x04, 0x04, 0x04, 0x01, 0x04, 0x01};
93 int frmsizecod;
94 int bitrate;
95 int half;
96 int acmod;
97
98 if ((buf[0] != 0x0b) || (buf[1] != 0x77)) /* syncword */
99 return 0;
100
101 if (buf[5] >= 0x60) /* bsid >= 12 */
102 return 0;
103 half = halfrate[buf[5] >> 3];
104
105 /* acmod, dsurmod and lfeon */
106 acmod = buf[6] >> 5;
107 *flags = ((((buf[6] & 0xf8) == 0x50) ? A52_DOLBY : acmod) |
108 ((buf[6] & lfeon[acmod]) ? A52_LFE : 0));
109
110 frmsizecod = buf[4] & 63;
111 if (frmsizecod >= 38)
112 return 0;
113 bitrate = rate [frmsizecod >> 1];
114 *bit_rate = (bitrate * 1000) >> half;
115
116 switch (buf[4] & 0xc0) {
117 case 0:
118 *sample_rate = 48000 >> half;
119 return 4 * bitrate;
120 case 0x40:
121 *sample_rate = 44100 >> half;
122 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
123 case 0x80:
124 *sample_rate = 32000 >> half;
125 return 6 * bitrate;
126 default:
127 return 0;
128 }
129}
130
131int a52_frame (a52_state_t * state, uint8_t * buf, int * flags,
132 level_t * level, sample_t bias)
133{
134 static level_t clev[4] = { LEVEL (LEVEL_3DB), LEVEL (LEVEL_45DB),
135 LEVEL (LEVEL_6DB), LEVEL (LEVEL_45DB) };
136 static level_t slev[4] = { LEVEL (LEVEL_3DB), LEVEL (LEVEL_6DB),
137 0, LEVEL (LEVEL_6DB) };
138 int chaninfo;
139 int acmod;
140
141 state->fscod = buf[4] >> 6;
142 state->halfrate = halfrate[buf[5] >> 3];
143 state->acmod = acmod = buf[6] >> 5;
144
145 a52_bitstream_set_ptr (state, buf + 6);
146 bitstream_get (state, 3); /* skip acmod we already parsed */
147
148 if ((acmod == 2) && (bitstream_get (state, 2) == 2)) /* dsurmod */
149 acmod = A52_DOLBY;
150
151 state->clev = state->slev = 0;
152
153 if ((acmod & 1) && (acmod != 1))
154 state->clev = clev[bitstream_get (state, 2)]; /* cmixlev */
155
156 if (acmod & 4)
157 state->slev = slev[bitstream_get (state, 2)]; /* surmixlev */
158
159 state->lfeon = bitstream_get (state, 1);
160
161 state->output = a52_downmix_init (acmod, *flags, level,
162 state->clev, state->slev);
163 if (state->output < 0)
164 return 1;
165 if (state->lfeon && (*flags & A52_LFE))
166 state->output |= A52_LFE;
167 *flags = state->output;
168 /* the 2* compensates for differences in imdct */
169 state->dynrng = state->level = MUL_C (*level, 2);
170 state->bias = bias;
171 state->dynrnge = 1;
172 state->dynrngcall = NULL;
173 state->cplba.deltbae = DELTA_BIT_NONE;
174 state->ba[0].deltbae = state->ba[1].deltbae = state->ba[2].deltbae =
175 state->ba[3].deltbae = state->ba[4].deltbae = DELTA_BIT_NONE;
176
177 chaninfo = !acmod;
178 do {
179 bitstream_get (state, 5); /* dialnorm */
180 if (bitstream_get (state, 1)) /* compre */
181 bitstream_get (state, 8); /* compr */
182 if (bitstream_get (state, 1)) /* langcode */
183 bitstream_get (state, 8); /* langcod */
184 if (bitstream_get (state, 1)) /* audprodie */
185 bitstream_get (state, 7); /* mixlevel + roomtyp */
186 } while (chaninfo--);
187
188 bitstream_get (state, 2); /* copyrightb + origbs */
189
190 if (bitstream_get (state, 1)) /* timecod1e */
191 bitstream_get (state, 14); /* timecod1 */
192 if (bitstream_get (state, 1)) /* timecod2e */
193 bitstream_get (state, 14); /* timecod2 */
194
195 if (bitstream_get (state, 1)) { /* addbsie */
196 int addbsil;
197
198 addbsil = bitstream_get (state, 6);
199 do {
200 bitstream_get (state, 8); /* addbsi */
201 } while (addbsil--);
202 }
203
204 return 0;
205}
206
207void a52_dynrng (a52_state_t * state,
208 level_t (* call) (level_t, void *), void * data)
209{
210 state->dynrnge = 0;
211 if (call) {
212 state->dynrnge = 1;
213 state->dynrngcall = call;
214 state->dynrngdata = data;
215 }
216}
217
218static int parse_exponents (a52_state_t * state, int expstr, int ngrps,
219 uint8_t exponent, uint8_t * dest)
220{
221 int exps;
222
223 while (ngrps--) {
224 exps = bitstream_get (state, 7);
225
226 exponent += exp_1[exps];
227 if (exponent > 24)
228 return 1;
229
230 switch (expstr) {
231 case EXP_D45:
232 *(dest++) = exponent;
233 *(dest++) = exponent;
234 case EXP_D25:
235 *(dest++) = exponent;
236 case EXP_D15:
237 *(dest++) = exponent;
238 }
239
240 exponent += exp_2[exps];
241 if (exponent > 24)
242 return 1;
243
244 switch (expstr) {
245 case EXP_D45:
246 *(dest++) = exponent;
247 *(dest++) = exponent;
248 case EXP_D25:
249 *(dest++) = exponent;
250 case EXP_D15:
251 *(dest++) = exponent;
252 }
253
254 exponent += exp_3[exps];
255 if (exponent > 24)
256 return 1;
257
258 switch (expstr) {
259 case EXP_D45:
260 *(dest++) = exponent;
261 *(dest++) = exponent;
262 case EXP_D25:
263 *(dest++) = exponent;
264 case EXP_D15:
265 *(dest++) = exponent;
266 }
267 }
268
269 return 0;
270}
271
272static int parse_deltba (a52_state_t * state, int8_t * deltba)
273{
274 int deltnseg, deltlen, delta, j;
275
276 memset (deltba, 0, 50);
277
278 deltnseg = bitstream_get (state, 3);
279 j = 0;
280 do {
281 j += bitstream_get (state, 5);
282 deltlen = bitstream_get (state, 4);
283 delta = bitstream_get (state, 3);
284 delta -= (delta >= 4) ? 3 : 4;
285 if (!deltlen)
286 continue;
287 if (j + deltlen >= 50)
288 return 1;
289 while (deltlen--)
290 deltba[j++] = delta;
291 } while (deltnseg--);
292
293 return 0;
294}
295
296static inline int zero_snr_offsets (int nfchans, a52_state_t * state)
297{
298 int i;
299
300 if ((state->csnroffst) ||
301 (state->chincpl && state->cplba.bai >> 3) || /* cplinu, fsnroffst */
302 (state->lfeon && state->lfeba.bai >> 3)) /* fsnroffst */
303 return 0;
304 for (i = 0; i < nfchans; i++)
305 if (state->ba[i].bai >> 3) /* fsnroffst */
306 return 0;
307 return 1;
308}
309
310static inline int16_t dither_gen (a52_state_t * state)
311{
312 int16_t nstate;
313
314 nstate = dither_lut[state->lfsr_state >> 8] ^ (state->lfsr_state << 8);
315
316 state->lfsr_state = (uint16_t) nstate;
317
318 return (3 * nstate) >> 2;
319}
320
321#ifndef LIBA52_FIXED
322#define COEFF(c,t,l,s,e) (c) = (t) * (s)[e]
323#else
324#define COEFF(c,_t,_l,s,e) do { \
325 quantizer_t t = (_t); \
326 level_t l = (_l); \
327 int shift = e - 5; \
328 sample_t tmp = t * (l >> 16) + ((t * (l & 0xffff)) >> 16); \
329 if (shift >= 0) \
330 (c) = tmp >> shift; \
331 else \
332 (c) = tmp << -shift; \
333} while (0)
334#endif
335
336static void coeff_get (a52_state_t * state, sample_t * coeff,
337 expbap_t * expbap, quantizer_set_t * quant,
338 level_t level, int dither, int end)
339{
340 int i;
341 uint8_t * exp;
342 int8_t * bap;
343
344#ifndef LIBA52_FIXED
345 sample_t factor[25];
346
347 for (i = 0; i <= 24; i++)
348 factor[i] = scale_factor[i] * level;
349#endif
350
351 exp = expbap->exp;
352 bap = expbap->bap;
353
354 for (i = 0; i < end; i++) {
355 int bapi;
356
357 bapi = bap[i];
358 switch (bapi) {
359 case 0:
360 if (dither) {
361 COEFF (coeff[i], dither_gen (state), level, factor, exp[i]);
362 continue;
363 } else {
364 coeff[i] = 0;
365 continue;
366 }
367
368 case -1:
369 if (quant->q1_ptr >= 0) {
370 COEFF (coeff[i], quant->q1[quant->q1_ptr--], level,
371 factor, exp[i]);
372 continue;
373 } else {
374 int code;
375
376 code = bitstream_get (state, 5);
377
378 quant->q1_ptr = 1;
379 quant->q1[0] = q_1_2[code];
380 quant->q1[1] = q_1_1[code];
381 COEFF (coeff[i], q_1_0[code], level, factor, exp[i]);
382 continue;
383 }
384
385 case -2:
386 if (quant->q2_ptr >= 0) {
387 COEFF (coeff[i], quant->q2[quant->q2_ptr--], level,
388 factor, exp[i]);
389 continue;
390 } else {
391 int code;
392
393 code = bitstream_get (state, 7);
394
395 quant->q2_ptr = 1;
396 quant->q2[0] = q_2_2[code];
397 quant->q2[1] = q_2_1[code];
398 COEFF (coeff[i], q_2_0[code], level, factor, exp[i]);
399 continue;
400 }
401
402 case 3:
403 COEFF (coeff[i], q_3[bitstream_get (state, 3)], level,
404 factor, exp[i]);
405 continue;
406
407 case -3:
408 if (quant->q4_ptr == 0) {
409 quant->q4_ptr = -1;
410 COEFF (coeff[i], quant->q4, level, factor, exp[i]);
411 continue;
412 } else {
413 int code;
414
415 code = bitstream_get (state, 7);
416
417 quant->q4_ptr = 0;
418 quant->q4 = q_4_1[code];
419 COEFF (coeff[i], q_4_0[code], level, factor, exp[i]);
420 continue;
421 }
422
423 case 4:
424 COEFF (coeff[i], q_5[bitstream_get (state, 4)], level,
425 factor, exp[i]);
426 continue;
427
428 default:
429 COEFF (coeff[i], bitstream_get_2 (state, bapi) << (16 - bapi),
430 level, factor, exp[i]);
431 }
432 }
433}
434
435static void coeff_get_coupling (a52_state_t * state, int nfchans,
436 level_t * coeff, sample_t (* samples)[256],
437 quantizer_set_t * quant, uint8_t dithflag[5])
438{
439 int cplbndstrc, bnd, i, i_end, ch;
440 uint8_t * exp;
441 int8_t * bap;
442 level_t cplco[5];
443
444 exp = state->cpl_expbap.exp;
445 bap = state->cpl_expbap.bap;
446 bnd = 0;
447 cplbndstrc = state->cplbndstrc;
448 i = state->cplstrtmant;
449 while (i < state->cplendmant) {
450 i_end = i + 12;
451 while (cplbndstrc & 1) {
452 cplbndstrc >>= 1;
453 i_end += 12;
454 }
455 cplbndstrc >>= 1;
456 for (ch = 0; ch < nfchans; ch++)
457 cplco[ch] = MUL_L (state->cplco[ch][bnd], coeff[ch]);
458 bnd++;
459
460 while (i < i_end) {
461 quantizer_t cplcoeff;
462 int bapi;
463
464 bapi = bap[i];
465 switch (bapi) {
466 case 0:
467 for (ch = 0; ch < nfchans; ch++)
468 if ((state->chincpl >> ch) & 1) {
469 if (dithflag[ch])
470#ifndef LIBA52_FIXED
471 samples[ch][i] = (scale_factor[exp[i]] *
472 cplco[ch] * dither_gen (state));
473#else
474 COEFF (samples[ch][i], dither_gen (state),
475 cplco[ch], scale_factor, exp[i]);
476#endif
477 else
478 samples[ch][i] = 0;
479 }
480 i++;
481 continue;
482
483 case -1:
484 if (quant->q1_ptr >= 0) {
485 cplcoeff = quant->q1[quant->q1_ptr--];
486 break;
487 } else {
488 int code;
489
490 code = bitstream_get (state, 5);
491
492 quant->q1_ptr = 1;
493 quant->q1[0] = q_1_2[code];
494 quant->q1[1] = q_1_1[code];
495 cplcoeff = q_1_0[code];
496 break;
497 }
498
499 case -2:
500 if (quant->q2_ptr >= 0) {
501 cplcoeff = quant->q2[quant->q2_ptr--];
502 break;
503 } else {
504 int code;
505
506 code = bitstream_get (state, 7);
507
508 quant->q2_ptr = 1;
509 quant->q2[0] = q_2_2[code];
510 quant->q2[1] = q_2_1[code];
511 cplcoeff = q_2_0[code];
512 break;
513 }
514
515 case 3:
516 cplcoeff = q_3[bitstream_get (state, 3)];
517 break;
518
519 case -3:
520 if (quant->q4_ptr == 0) {
521 quant->q4_ptr = -1;
522 cplcoeff = quant->q4;
523 break;
524 } else {
525 int code;
526
527 code = bitstream_get (state, 7);
528
529 quant->q4_ptr = 0;
530 quant->q4 = q_4_1[code];
531 cplcoeff = q_4_0[code];
532 break;
533 }
534
535 case 4:
536 cplcoeff = q_5[bitstream_get (state, 4)];
537 break;
538
539 default:
540 cplcoeff = bitstream_get_2 (state, bapi) << (16 - bapi);
541 }
542#ifndef LIBA52_FIXED
543 cplcoeff *= scale_factor[exp[i]];
544#endif
545 for (ch = 0; ch < nfchans; ch++)
546 if ((state->chincpl >> ch) & 1)
547#ifndef LIBA52_FIXED
548 samples[ch][i] = cplcoeff * cplco[ch];
549#else
550 COEFF (samples[ch][i], cplcoeff, cplco[ch],
551 scale_factor, exp[i]);
552#endif
553 i++;
554 }
555 }
556}
557
558int a52_block (a52_state_t * state)
559{
560 static const uint8_t nfchans_tbl[] = {2, 1, 2, 3, 3, 4, 4, 5, 1, 1, 2};
561 static int rematrix_band[4] = {25, 37, 61, 253};
562 int i, nfchans, chaninfo;
563 uint8_t cplexpstr, chexpstr[5], lfeexpstr, do_bit_alloc, done_cpl;
564 uint8_t blksw[5], dithflag[5];
565 level_t coeff[5];
566 int chanbias;
567 quantizer_set_t quant;
568 sample_t * samples;
569
570 nfchans = nfchans_tbl[state->acmod];
571
572 for (i = 0; i < nfchans; i++)
573 blksw[i] = bitstream_get (state, 1);
574
575 for (i = 0; i < nfchans; i++)
576 dithflag[i] = bitstream_get (state, 1);
577
578 chaninfo = !state->acmod;
579 do {
580 if (bitstream_get (state, 1)) { /* dynrnge */
581 int dynrng;
582
583 dynrng = bitstream_get_2 (state, 8);
584 if (state->dynrnge) {
585 level_t range;
586
587#if !defined(LIBA52_FIXED)
588 range = ((((dynrng & 0x1f) | 0x20) << 13) *
589 scale_factor[3 - (dynrng >> 5)]);
590#else
591 range = ((dynrng & 0x1f) | 0x20) << (21 + (dynrng >> 5));
592#endif
593 if (state->dynrngcall)
594 range = state->dynrngcall (range, state->dynrngdata);
595 state->dynrng = MUL_L (state->level, range);
596 }
597 }
598 } while (chaninfo--);
599
600 if (bitstream_get (state, 1)) { /* cplstre */
601 state->chincpl = 0;
602 if (bitstream_get (state, 1)) { /* cplinu */
603 static uint8_t bndtab[16] = {31, 35, 37, 39, 41, 42, 43, 44,
604 45, 45, 46, 46, 47, 47, 48, 48};
605 int cplbegf;
606 int cplendf;
607 int ncplsubnd;
608
609 for (i = 0; i < nfchans; i++)
610 state->chincpl |= bitstream_get (state, 1) << i;
611 switch (state->acmod) {
612 case 0: case 1:
613 return 1;
614 case 2:
615 state->phsflginu = bitstream_get (state, 1);
616 }
617 cplbegf = bitstream_get (state, 4);
618 cplendf = bitstream_get (state, 4);
619
620 if (cplendf + 3 - cplbegf < 0)
621 return 1;
622 state->ncplbnd = ncplsubnd = cplendf + 3 - cplbegf;
623 state->cplstrtbnd = bndtab[cplbegf];
624 state->cplstrtmant = cplbegf * 12 + 37;
625 state->cplendmant = cplendf * 12 + 73;
626
627 state->cplbndstrc = 0;
628 for (i = 0; i < ncplsubnd - 1; i++)
629 if (bitstream_get (state, 1)) {
630 state->cplbndstrc |= 1 << i;
631 state->ncplbnd--;
632 }
633 }
634 }
635
636 if (state->chincpl) { /* cplinu */
637 int j, cplcoe;
638
639 cplcoe = 0;
640 for (i = 0; i < nfchans; i++)
641 if ((state->chincpl) >> i & 1)
642 if (bitstream_get (state, 1)) { /* cplcoe */
643 int mstrcplco, cplcoexp, cplcomant;
644
645 cplcoe = 1;
646 mstrcplco = 3 * bitstream_get (state, 2);
647 for (j = 0; j < state->ncplbnd; j++) {
648 cplcoexp = bitstream_get (state, 4);
649 cplcomant = bitstream_get (state, 4);
650 if (cplcoexp == 15)
651 cplcomant <<= 14;
652 else
653 cplcomant = (cplcomant | 0x10) << 13;
654#ifndef LIBA52_FIXED
655 state->cplco[i][j] =
656 cplcomant * scale_factor[cplcoexp + mstrcplco];
657#else
658 state->cplco[i][j] = (cplcomant << 11) >> (cplcoexp + mstrcplco);
659#endif
660
661 }
662 }
663 if ((state->acmod == 2) && state->phsflginu && cplcoe)
664 for (j = 0; j < state->ncplbnd; j++)
665 if (bitstream_get (state, 1)) /* phsflg */
666 state->cplco[1][j] = -state->cplco[1][j];
667 }
668
669 if ((state->acmod == 2) && (bitstream_get (state, 1))) { /* rematstr */
670 int end;
671
672 state->rematflg = 0;
673 end = (state->chincpl) ? state->cplstrtmant : 253; /* cplinu */
674 i = 0;
675 do
676 state->rematflg |= bitstream_get (state, 1) << i;
677 while (rematrix_band[i++] < end);
678 }
679
680 cplexpstr = EXP_REUSE;
681 lfeexpstr = EXP_REUSE;
682 if (state->chincpl) /* cplinu */
683 cplexpstr = bitstream_get (state, 2);
684 for (i = 0; i < nfchans; i++)
685 chexpstr[i] = bitstream_get (state, 2);
686 if (state->lfeon)
687 lfeexpstr = bitstream_get (state, 1);
688
689 for (i = 0; i < nfchans; i++)
690 if (chexpstr[i] != EXP_REUSE) {
691 if ((state->chincpl >> i) & 1)
692 state->endmant[i] = state->cplstrtmant;
693 else {
694 int chbwcod;
695
696 chbwcod = bitstream_get (state, 6);
697 if (chbwcod > 60)
698 return 1;
699 state->endmant[i] = chbwcod * 3 + 73;
700 }
701 }
702
703 do_bit_alloc = 0;
704
705 if (cplexpstr != EXP_REUSE) {
706 int cplabsexp, ncplgrps;
707
708 do_bit_alloc = 64;
709 ncplgrps = ((state->cplendmant - state->cplstrtmant) /
710 (3 << (cplexpstr - 1)));
711 cplabsexp = bitstream_get (state, 4) << 1;
712 if (parse_exponents (state, cplexpstr, ncplgrps, cplabsexp,
713 state->cpl_expbap.exp + state->cplstrtmant))
714 return 1;
715 }
716 for (i = 0; i < nfchans; i++)
717 if (chexpstr[i] != EXP_REUSE) {
718 int grp_size, nchgrps;
719
720 do_bit_alloc |= 1 << i;
721 grp_size = 3 << (chexpstr[i] - 1);
722 nchgrps = (state->endmant[i] + grp_size - 4) / grp_size;
723 state->fbw_expbap[i].exp[0] = bitstream_get (state, 4);
724 if (parse_exponents (state, chexpstr[i], nchgrps,
725 state->fbw_expbap[i].exp[0],
726 state->fbw_expbap[i].exp + 1))
727 return 1;
728 bitstream_get (state, 2); /* gainrng */
729 }
730 if (lfeexpstr != EXP_REUSE) {
731 do_bit_alloc |= 32;
732 state->lfe_expbap.exp[0] = bitstream_get (state, 4);
733 if (parse_exponents (state, lfeexpstr, 2, state->lfe_expbap.exp[0],
734 state->lfe_expbap.exp + 1))
735 return 1;
736 }
737
738 if (bitstream_get (state, 1)) { /* baie */
739 do_bit_alloc = 127;
740 state->bai = bitstream_get (state, 11);
741 }
742 if (bitstream_get (state, 1)) { /* snroffste */
743 do_bit_alloc = 127;
744 state->csnroffst = bitstream_get (state, 6);
745 if (state->chincpl) /* cplinu */
746 state->cplba.bai = bitstream_get (state, 7);
747 for (i = 0; i < nfchans; i++)
748 state->ba[i].bai = bitstream_get (state, 7);
749 if (state->lfeon)
750 state->lfeba.bai = bitstream_get (state, 7);
751 }
752 if ((state->chincpl) && (bitstream_get (state, 1))) { /* cplleake */
753 do_bit_alloc |= 64;
754 state->cplfleak = 9 - bitstream_get (state, 3);
755 state->cplsleak = 9 - bitstream_get (state, 3);
756 }
757
758 if (bitstream_get (state, 1)) { /* deltbaie */
759 do_bit_alloc = 127;
760 if (state->chincpl) /* cplinu */
761 state->cplba.deltbae = bitstream_get (state, 2);
762 for (i = 0; i < nfchans; i++)
763 state->ba[i].deltbae = bitstream_get (state, 2);
764 if (state->chincpl && /* cplinu */
765 (state->cplba.deltbae == DELTA_BIT_NEW) &&
766 parse_deltba (state, state->cplba.deltba))
767 return 1;
768 for (i = 0; i < nfchans; i++)
769 if ((state->ba[i].deltbae == DELTA_BIT_NEW) &&
770 parse_deltba (state, state->ba[i].deltba))
771 return 1;
772 }
773
774 if (do_bit_alloc) {
775 if (zero_snr_offsets (nfchans, state)) {
776 memset (state->cpl_expbap.bap, 0, sizeof (state->cpl_expbap.bap));
777 for (i = 0; i < nfchans; i++)
778 memset (state->fbw_expbap[i].bap, 0,
779 sizeof (state->fbw_expbap[i].bap));
780 memset (state->lfe_expbap.bap, 0, sizeof (state->lfe_expbap.bap));
781 } else {
782 if (state->chincpl && (do_bit_alloc & 64)) /* cplinu */
783 a52_bit_allocate (state, &state->cplba, state->cplstrtbnd,
784 state->cplstrtmant, state->cplendmant,
785 state->cplfleak << 8, state->cplsleak << 8,
786 &state->cpl_expbap);
787 for (i = 0; i < nfchans; i++)
788 if (do_bit_alloc & (1 << i))
789 a52_bit_allocate (state, state->ba + i, 0, 0,
790 state->endmant[i], 0, 0,
791 state->fbw_expbap +i);
792 if (state->lfeon && (do_bit_alloc & 32)) {
793 state->lfeba.deltbae = DELTA_BIT_NONE;
794 a52_bit_allocate (state, &state->lfeba, 0, 0, 7, 0, 0,
795 &state->lfe_expbap);
796 }
797 }
798 }
799
800 if (bitstream_get (state, 1)) { /* skiple */
801 i = bitstream_get (state, 9); /* skipl */
802 while (i--)
803 bitstream_get (state, 8);
804 }
805
806 samples = state->samples;
807 if (state->output & A52_LFE)
808 samples += 256; /* shift for LFE channel */
809
810 chanbias = a52_downmix_coeff (coeff, state->acmod, state->output,
811 state->dynrng, state->clev, state->slev);
812
813 quant.q1_ptr = quant.q2_ptr = quant.q4_ptr = -1;
814 done_cpl = 0;
815
816 for (i = 0; i < nfchans; i++) {
817 int j;
818
819 coeff_get (state, samples + 256 * i, state->fbw_expbap +i, &quant,
820 coeff[i], dithflag[i], state->endmant[i]);
821
822 if ((state->chincpl >> i) & 1) {
823 if (!done_cpl) {
824 done_cpl = 1;
825 coeff_get_coupling (state, nfchans, coeff,
826 (sample_t (*)[256])samples, &quant,
827 dithflag);
828 }
829 j = state->cplendmant;
830 } else
831 j = state->endmant[i];
832 do
833 (samples + 256 * i)[j] = 0;
834 while (++j < 256);
835 }
836
837 if (state->acmod == 2) {
838 int j, end, band, rematflg;
839
840 end = ((state->endmant[0] < state->endmant[1]) ?
841 state->endmant[0] : state->endmant[1]);
842
843 i = 0;
844 j = 13;
845 rematflg = state->rematflg;
846 do {
847 if (! (rematflg & 1)) {
848 rematflg >>= 1;
849 j = rematrix_band[i++];
850 continue;
851 }
852 rematflg >>= 1;
853 band = rematrix_band[i++];
854 if (band > end)
855 band = end;
856 do {
857 sample_t tmp0, tmp1;
858
859 tmp0 = samples[j];
860 tmp1 = (samples+256)[j];
861 samples[j] = tmp0 + tmp1;
862 (samples+256)[j] = tmp0 - tmp1;
863 } while (++j < band);
864 } while (j < end);
865 }
866
867 if (state->lfeon) {
868 if (state->output & A52_LFE) {
869 coeff_get (state, samples - 256, &state->lfe_expbap, &quant,
870 state->dynrng, 0, 7);
871 for (i = 7; i < 256; i++)
872 (samples-256)[i] = 0;
873 a52_imdct_512 (samples - 256, samples + 1536 - 256, state->bias);
874 } else {
875 /* just skip the LFE coefficients */
876 coeff_get (state, samples + 1280, &state->lfe_expbap, &quant,
877 0, 0, 7);
878 }
879 }
880
881 i = 0;
882 if (nfchans_tbl[state->output & A52_CHANNEL_MASK] < nfchans)
883 for (i = 1; i < nfchans; i++)
884 if (blksw[i] != blksw[0])
885 break;
886
887 if (i < nfchans) {
888 if (state->downmixed) {
889 state->downmixed = 0;
890 a52_upmix (samples + 1536, state->acmod, state->output);
891 }
892
893 for (i = 0; i < nfchans; i++) {
894 sample_t bias;
895
896 bias = 0;
897 if (!(chanbias & (1 << i)))
898 bias = state->bias;
899
900 if (coeff[i]) {
901 if (blksw[i])
902 a52_imdct_256 (samples + 256 * i, samples + 1536 + 256 * i,
903 bias);
904 else
905 a52_imdct_512 (samples + 256 * i, samples + 1536 + 256 * i,
906 bias);
907 } else {
908 int j;
909
910 for (j = 0; j < 256; j++)
911 (samples + 256 * i)[j] = bias;
912 }
913 }
914
915 a52_downmix (samples, state->acmod, state->output, state->bias,
916 state->clev, state->slev);
917 } else {
918 nfchans = nfchans_tbl[state->output & A52_CHANNEL_MASK];
919
920 a52_downmix (samples, state->acmod, state->output, 0,
921 state->clev, state->slev);
922
923 if (!state->downmixed) {
924 state->downmixed = 1;
925 a52_downmix (samples + 1536, state->acmod, state->output, 0,
926 state->clev, state->slev);
927 }
928
929 if (blksw[0])
930 for (i = 0; i < nfchans; i++)
931 a52_imdct_256 (samples + 256 * i, samples + 1536 + 256 * i,
932 state->bias);
933 else
934 for (i = 0; i < nfchans; i++)
935 a52_imdct_512 (samples + 256 * i, samples + 1536 + 256 * i,
936 state->bias);
937 }
938
939 return 0;
940}
941
942void a52_free (a52_state_t * state)
943{
944 free (state->samples);
945 free (state);
946}