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