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