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