/[pcsx2_0.9.7]/trunk/3rdparty/liba52/parse.c
ViewVC logotype

Contents of /trunk/3rdparty/liba52/parse.c

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22