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