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