comparison liba52/parse.c @ 1072:68d0a38bd802 libavcodec

* sync with main liba52 sources
author kabi
date Tue, 18 Feb 2003 11:48:57 +0000
parents 696ccd81403e
children e101d1cffec6
comparison
equal deleted inserted replaced
1071:0a48dd404167 1072:68d0a38bd802
1 /* 1 /*
2 * parse.c 2 * parse.c
3 * Copyright (C) 2000-2002 Michel Lespinasse <walken@zoy.org> 3 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca> 4 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
5 * 5 *
6 * This file is part of a52dec, a free ATSC A-52 stream decoder. 6 * This file is part of a52dec, a free ATSC A-52 stream decoder.
7 * See http://liba52.sourceforge.net/ for updates. 7 * See http://liba52.sourceforge.net/ for updates.
8 * 8 *
19 * You should have received a copy of the GNU General Public License 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 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 21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */ 22 */
23 23
24 #include "config.h"
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <inttypes.h>
29
24 #include "a52.h" 30 #include "a52.h"
25 #include "a52_internal.h" 31 #include "a52_internal.h"
26 #include "bitstream.h" 32 #include "bitstream.h"
27 #include "tables.h" 33 #include "tables.h"
28 34
29 #ifdef HAVE_MEMALIGN 35 #if defined(HAVE_MEMALIGN) && !defined(__cplusplus)
30 /* some systems have memalign() but no declaration for it */ 36 /* some systems have memalign() but no declaration for it */
31 void * memalign (size_t align, size_t size); 37 void * memalign (size_t align, size_t size);
32 #else 38 #else
33 /* assume malloc alignment is sufficient */ 39 /* assume malloc alignment is sufficient */
34 #define memalign(align,size) malloc (size) 40 #define memalign(align,size) malloc (size)
35 #endif 41 #endif
36 42
37 typedef struct { 43 typedef struct {
38 sample_t q1[2]; 44 quantizer_t q1[2];
39 sample_t q2[2]; 45 quantizer_t q2[2];
40 sample_t q4; 46 quantizer_t q4;
41 int q1_ptr; 47 int q1_ptr;
42 int q2_ptr; 48 int q2_ptr;
43 int q4_ptr; 49 int q4_ptr;
44 } quantizer_t; 50 } quantizer_set_t;
45 51
46 static uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3}; 52 static uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
47 53
48 a52_state_t * a52_init (uint32_t mm_accel) 54 a52_state_t * a52_init (uint32_t mm_accel)
49 { 55 {
50 a52_state_t * state; 56 a52_state_t * state;
51 int i; 57 int i;
52 58
53 state = malloc (sizeof (a52_state_t)); 59 state = (a52_state_t *) malloc (sizeof (a52_state_t));
54 if (state == NULL) 60 if (state == NULL)
55 return NULL; 61 return NULL;
56 62
57 state->samples = memalign (16, 256 * 12 * sizeof (sample_t)); 63 state->samples = (sample_t *) memalign (16, 256 * 12 * sizeof (sample_t));
58 if (state->samples == NULL) { 64 if (state->samples == NULL) {
59 free (state); 65 free (state);
60 return NULL; 66 return NULL;
61 } 67 }
62 68
63 for (i = 0; i < 256 * 12; i++) 69 for (i = 0; i < 256 * 12; i++)
64 state->samples[i] = 0; 70 state->samples[i] = 0;
65 71
66 state->downmixed = 1; 72 state->downmixed = 1;
73
74 state->lfsr_state = 1;
67 75
68 a52_imdct_init (mm_accel); 76 a52_imdct_init (mm_accel);
69 77
70 return state; 78 return state;
71 } 79 }
119 return 0; 127 return 0;
120 } 128 }
121 } 129 }
122 130
123 int a52_frame (a52_state_t * state, uint8_t * buf, int * flags, 131 int a52_frame (a52_state_t * state, uint8_t * buf, int * flags,
124 sample_t * level, sample_t bias) 132 level_t * level, sample_t bias)
125 { 133 {
126 static sample_t clev[4] = {LEVEL_3DB, LEVEL_45DB, LEVEL_6DB, LEVEL_45DB}; 134 static level_t clev[4] = { LEVEL (LEVEL_3DB), LEVEL (LEVEL_45DB),
127 static sample_t slev[4] = {LEVEL_3DB, LEVEL_6DB, 0, LEVEL_6DB}; 135 LEVEL (LEVEL_6DB), LEVEL (LEVEL_45DB) };
136 static level_t slev[4] = { LEVEL (LEVEL_3DB), LEVEL (LEVEL_6DB),
137 0, LEVEL (LEVEL_6DB) };
128 int chaninfo; 138 int chaninfo;
129 int acmod; 139 int acmod;
130 140
131 state->fscod = buf[4] >> 6; 141 state->fscod = buf[4] >> 6;
132 state->halfrate = halfrate[buf[5] >> 3]; 142 state->halfrate = halfrate[buf[5] >> 3];
133 state->acmod = acmod = buf[6] >> 5; 143 state->acmod = acmod = buf[6] >> 5;
134 144
135 a52_bitstream_set_ptr (buf + 6); 145 a52_bitstream_set_ptr (state, buf + 6);
136 bitstream_get (3); /* skip acmod we already parsed */ 146 bitstream_get (state, 3); /* skip acmod we already parsed */
137 147
138 if ((acmod == 2) && (bitstream_get (2) == 2)) /* dsurmod */ 148 if ((acmod == 2) && (bitstream_get (state, 2) == 2)) /* dsurmod */
139 acmod = A52_DOLBY; 149 acmod = A52_DOLBY;
140 150
151 state->clev = state->slev = 0;
152
141 if ((acmod & 1) && (acmod != 1)) 153 if ((acmod & 1) && (acmod != 1))
142 state->clev = clev[bitstream_get (2)]; /* cmixlev */ 154 state->clev = clev[bitstream_get (state, 2)]; /* cmixlev */
143 155
144 if (acmod & 4) 156 if (acmod & 4)
145 state->slev = slev[bitstream_get (2)]; /* surmixlev */ 157 state->slev = slev[bitstream_get (state, 2)]; /* surmixlev */
146 158
147 state->lfeon = bitstream_get (1); 159 state->lfeon = bitstream_get (state, 1);
148 160
149 state->output = a52_downmix_init (acmod, *flags, level, 161 state->output = a52_downmix_init (acmod, *flags, level,
150 state->clev, state->slev); 162 state->clev, state->slev);
151 if (state->output < 0) 163 if (state->output < 0)
152 return 1; 164 return 1;
153 if (state->lfeon && (*flags & A52_LFE)) 165 if (state->lfeon && (*flags & A52_LFE))
154 state->output |= A52_LFE; 166 state->output |= A52_LFE;
155 *flags = state->output; 167 *flags = state->output;
156 /* the 2* compensates for differences in imdct */ 168 /* the 2* compensates for differences in imdct */
157 state->dynrng = state->level = 2 * *level; 169 state->dynrng = state->level = MUL_C (*level, 2);
158 state->bias = bias; 170 state->bias = bias;
159 state->dynrnge = 1; 171 state->dynrnge = 1;
160 state->dynrngcall = NULL; 172 state->dynrngcall = NULL;
161 state->cplba.deltbae = DELTA_BIT_NONE; 173 state->cplba.deltbae = DELTA_BIT_NONE;
162 state->ba[0].deltbae = state->ba[1].deltbae = state->ba[2].deltbae = 174 state->ba[0].deltbae = state->ba[1].deltbae = state->ba[2].deltbae =
163 state->ba[3].deltbae = state->ba[4].deltbae = DELTA_BIT_NONE; 175 state->ba[3].deltbae = state->ba[4].deltbae = DELTA_BIT_NONE;
164 176
165 chaninfo = !acmod; 177 chaninfo = !acmod;
166 do { 178 do {
167 bitstream_get (5); /* dialnorm */ 179 bitstream_get (state, 5); /* dialnorm */
168 if (bitstream_get (1)) /* compre */ 180 if (bitstream_get (state, 1)) /* compre */
169 bitstream_get (8); /* compr */ 181 bitstream_get (state, 8); /* compr */
170 if (bitstream_get (1)) /* langcode */ 182 if (bitstream_get (state, 1)) /* langcode */
171 bitstream_get (8); /* langcod */ 183 bitstream_get (state, 8); /* langcod */
172 if (bitstream_get (1)) /* audprodie */ 184 if (bitstream_get (state, 1)) /* audprodie */
173 bitstream_get (7); /* mixlevel + roomtyp */ 185 bitstream_get (state, 7); /* mixlevel + roomtyp */
174 } while (chaninfo--); 186 } while (chaninfo--);
175 187
176 bitstream_get (2); /* copyrightb + origbs */ 188 bitstream_get (state, 2); /* copyrightb + origbs */
177 189
178 if (bitstream_get (1)) /* timecod1e */ 190 if (bitstream_get (state, 1)) /* timecod1e */
179 bitstream_get (14); /* timecod1 */ 191 bitstream_get (state, 14); /* timecod1 */
180 if (bitstream_get (1)) /* timecod2e */ 192 if (bitstream_get (state, 1)) /* timecod2e */
181 bitstream_get (14); /* timecod2 */ 193 bitstream_get (state, 14); /* timecod2 */
182 194
183 if (bitstream_get (1)) { /* addbsie */ 195 if (bitstream_get (state, 1)) { /* addbsie */
184 int addbsil; 196 int addbsil;
185 197
186 addbsil = bitstream_get (6); 198 addbsil = bitstream_get (state, 6);
187 do { 199 do {
188 bitstream_get (8); /* addbsi */ 200 bitstream_get (state, 8); /* addbsi */
189 } while (addbsil--); 201 } while (addbsil--);
190 } 202 }
191 203
192 return 0; 204 return 0;
193 } 205 }
194 206
195 void a52_dynrng (a52_state_t * state, 207 void a52_dynrng (a52_state_t * state,
196 sample_t (* call) (sample_t, void *), void * data) 208 level_t (* call) (level_t, void *), void * data)
197 { 209 {
198 state->dynrnge = 0; 210 state->dynrnge = 0;
199 if (call) { 211 if (call) {
200 state->dynrnge = 1; 212 state->dynrnge = 1;
201 state->dynrngcall = call; 213 state->dynrngcall = call;
202 state->dynrngdata = data; 214 state->dynrngdata = data;
203 } 215 }
204 } 216 }
205 217
206 static int parse_exponents (int expstr, int ngrps, uint8_t exponent, 218 static int parse_exponents (a52_state_t * state, int expstr, int ngrps,
207 uint8_t * dest) 219 uint8_t exponent, uint8_t * dest)
208 { 220 {
209 int exps; 221 int exps;
210 222
211 while (ngrps--) { 223 while (ngrps--) {
212 exps = bitstream_get (7); 224 exps = bitstream_get (state, 7);
213 225
214 exponent += exp_1[exps]; 226 exponent += exp_1[exps];
215 if (exponent > 24) 227 if (exponent > 24)
216 return 1; 228 return 1;
217 229
255 } 267 }
256 268
257 return 0; 269 return 0;
258 } 270 }
259 271
260 static int parse_deltba (int8_t * deltba) 272 static int parse_deltba (a52_state_t * state, int8_t * deltba)
261 { 273 {
262 int deltnseg, deltlen, delta, j; 274 int deltnseg, deltlen, delta, j;
263 275
264 memset (deltba, 0, 50); 276 memset (deltba, 0, 50);
265 277
266 deltnseg = bitstream_get (3); 278 deltnseg = bitstream_get (state, 3);
267 j = 0; 279 j = 0;
268 do { 280 do {
269 j += bitstream_get (5); 281 j += bitstream_get (state, 5);
270 deltlen = bitstream_get (4); 282 deltlen = bitstream_get (state, 4);
271 delta = bitstream_get (3); 283 delta = bitstream_get (state, 3);
272 delta -= (delta >= 4) ? 3 : 4; 284 delta -= (delta >= 4) ? 3 : 4;
273 if (!deltlen) 285 if (!deltlen)
274 continue; 286 continue;
275 if (j + deltlen >= 50) 287 if (j + deltlen >= 50)
276 return 1; 288 return 1;
293 if (state->ba[i].bai >> 3) /* fsnroffst */ 305 if (state->ba[i].bai >> 3) /* fsnroffst */
294 return 0; 306 return 0;
295 return 1; 307 return 1;
296 } 308 }
297 309
298 static inline int16_t dither_gen (void) 310 static inline int16_t dither_gen (a52_state_t * state)
299 { 311 {
300 static uint16_t lfsr_state = 1; 312 int16_t nstate;
301 int16_t state; 313
302 314 nstate = dither_lut[state->lfsr_state >> 8] ^ (state->lfsr_state << 8);
303 state = dither_lut[lfsr_state >> 8] ^ (lfsr_state << 8);
304 315
305 lfsr_state = (uint16_t) state; 316 state->lfsr_state = (uint16_t) nstate;
306 317
307 return state; 318 return (3 * nstate) >> 2;
308 } 319 }
309 320
310 static void coeff_get (sample_t * coeff, expbap_t * expbap, 321 #ifndef LIBA52_FIXED
311 quantizer_t * quantizer, sample_t level, 322 #define COEFF(c,t,l,s,e) (c) = (t) * (s)[e]
312 int dither, int end) 323 #else
324 #define COEFF(c,_t,_l,s,e) do { \
325 quantizer_t t = (_t); \
326 level_t l = (_l); \
327 int shift = e - 5; \
328 sample_t tmp = t * (l >> 16) + ((t * (l & 0xffff)) >> 16); \
329 if (shift >= 0) \
330 (c) = tmp >> shift; \
331 else \
332 (c) = tmp << -shift; \
333 } while (0)
334 #endif
335
336 static void coeff_get (a52_state_t * state, sample_t * coeff,
337 expbap_t * expbap, quantizer_set_t * quant,
338 level_t level, int dither, int end)
313 { 339 {
314 int i; 340 int i;
315 uint8_t * exp; 341 uint8_t * exp;
316 int8_t * bap; 342 int8_t * bap;
343
344 #ifndef LIBA52_FIXED
317 sample_t factor[25]; 345 sample_t factor[25];
318 346
319 for (i = 0; i <= 24; i++) 347 for (i = 0; i <= 24; i++)
320 factor[i] = scale_factor[i] * level; 348 factor[i] = scale_factor[i] * level;
349 #endif
321 350
322 exp = expbap->exp; 351 exp = expbap->exp;
323 bap = expbap->bap; 352 bap = expbap->bap;
324 353
325 for (i = 0; i < end; i++) { 354 for (i = 0; i < end; i++) {
327 356
328 bapi = bap[i]; 357 bapi = bap[i];
329 switch (bapi) { 358 switch (bapi) {
330 case 0: 359 case 0:
331 if (dither) { 360 if (dither) {
332 coeff[i] = dither_gen() * LEVEL_3DB * factor[exp[i]]; 361 COEFF (coeff[i], dither_gen (state), level, factor, exp[i]);
333 continue; 362 continue;
334 } else { 363 } else {
335 coeff[i] = 0; 364 coeff[i] = 0;
336 continue; 365 continue;
337 } 366 }
338 367
339 case -1: 368 case -1:
340 if (quantizer->q1_ptr >= 0) { 369 if (quant->q1_ptr >= 0) {
341 coeff[i] = quantizer->q1[quantizer->q1_ptr--] * factor[exp[i]]; 370 COEFF (coeff[i], quant->q1[quant->q1_ptr--], level,
371 factor, exp[i]);
342 continue; 372 continue;
343 } else { 373 } else {
344 int code; 374 int code;
345 375
346 code = bitstream_get (5); 376 code = bitstream_get (state, 5);
347 377
348 quantizer->q1_ptr = 1; 378 quant->q1_ptr = 1;
349 quantizer->q1[0] = q_1_2[code]; 379 quant->q1[0] = q_1_2[code];
350 quantizer->q1[1] = q_1_1[code]; 380 quant->q1[1] = q_1_1[code];
351 coeff[i] = q_1_0[code] * factor[exp[i]]; 381 COEFF (coeff[i], q_1_0[code], level, factor, exp[i]);
352 continue; 382 continue;
353 } 383 }
354 384
355 case -2: 385 case -2:
356 if (quantizer->q2_ptr >= 0) { 386 if (quant->q2_ptr >= 0) {
357 coeff[i] = quantizer->q2[quantizer->q2_ptr--] * factor[exp[i]]; 387 COEFF (coeff[i], quant->q2[quant->q2_ptr--], level,
388 factor, exp[i]);
358 continue; 389 continue;
359 } else { 390 } else {
360 int code; 391 int code;
361 392
362 code = bitstream_get (7); 393 code = bitstream_get (state, 7);
363 394
364 quantizer->q2_ptr = 1; 395 quant->q2_ptr = 1;
365 quantizer->q2[0] = q_2_2[code]; 396 quant->q2[0] = q_2_2[code];
366 quantizer->q2[1] = q_2_1[code]; 397 quant->q2[1] = q_2_1[code];
367 coeff[i] = q_2_0[code] * factor[exp[i]]; 398 COEFF (coeff[i], q_2_0[code], level, factor, exp[i]);
368 continue; 399 continue;
369 } 400 }
370 401
371 case 3: 402 case 3:
372 coeff[i] = q_3[bitstream_get (3)] * factor[exp[i]]; 403 COEFF (coeff[i], q_3[bitstream_get (state, 3)], level,
404 factor, exp[i]);
373 continue; 405 continue;
374 406
375 case -3: 407 case -3:
376 if (quantizer->q4_ptr == 0) { 408 if (quant->q4_ptr == 0) {
377 quantizer->q4_ptr = -1; 409 quant->q4_ptr = -1;
378 coeff[i] = quantizer->q4 * factor[exp[i]]; 410 COEFF (coeff[i], quant->q4, level, factor, exp[i]);
379 continue; 411 continue;
380 } else { 412 } else {
381 int code; 413 int code;
382 414
383 code = bitstream_get (7); 415 code = bitstream_get (state, 7);
384 416
385 quantizer->q4_ptr = 0; 417 quant->q4_ptr = 0;
386 quantizer->q4 = q_4_1[code]; 418 quant->q4 = q_4_1[code];
387 coeff[i] = q_4_0[code] * factor[exp[i]]; 419 COEFF (coeff[i], q_4_0[code], level, factor, exp[i]);
388 continue; 420 continue;
389 } 421 }
390 422
391 case 4: 423 case 4:
392 coeff[i] = q_5[bitstream_get (4)] * factor[exp[i]]; 424 COEFF (coeff[i], q_5[bitstream_get (state, 4)], level,
425 factor, exp[i]);
393 continue; 426 continue;
394 427
395 default: 428 default:
396 coeff[i] = ((bitstream_get_2 (bapi) << (16 - bapi)) * 429 COEFF (coeff[i], bitstream_get_2 (state, bapi) << (16 - bapi),
397 factor[exp[i]]); 430 level, factor, exp[i]);
398 } 431 }
399 } 432 }
400 } 433 }
401 434
402 static void coeff_get_coupling (a52_state_t * state, int nfchans, 435 static void coeff_get_coupling (a52_state_t * state, int nfchans,
403 sample_t * coeff, sample_t (* samples)[256], 436 level_t * coeff, sample_t (* samples)[256],
404 quantizer_t * quantizer, uint8_t dithflag[5]) 437 quantizer_set_t * quant, uint8_t dithflag[5])
405 { 438 {
406 int cplbndstrc, bnd, i, i_end, ch; 439 int cplbndstrc, bnd, i, i_end, ch;
407 uint8_t * exp; 440 uint8_t * exp;
408 int8_t * bap; 441 int8_t * bap;
409 sample_t cplco[5]; 442 level_t cplco[5];
410 443
411 exp = state->cpl_expbap.exp; 444 exp = state->cpl_expbap.exp;
412 bap = state->cpl_expbap.bap; 445 bap = state->cpl_expbap.bap;
413 bnd = 0; 446 bnd = 0;
414 cplbndstrc = state->cplbndstrc; 447 cplbndstrc = state->cplbndstrc;
419 cplbndstrc >>= 1; 452 cplbndstrc >>= 1;
420 i_end += 12; 453 i_end += 12;
421 } 454 }
422 cplbndstrc >>= 1; 455 cplbndstrc >>= 1;
423 for (ch = 0; ch < nfchans; ch++) 456 for (ch = 0; ch < nfchans; ch++)
424 cplco[ch] = state->cplco[ch][bnd] * coeff[ch]; 457 cplco[ch] = MUL_L (state->cplco[ch][bnd], coeff[ch]);
425 bnd++; 458 bnd++;
426 459
427 while (i < i_end) { 460 while (i < i_end) {
428 sample_t cplcoeff; 461 quantizer_t cplcoeff;
429 int bapi; 462 int bapi;
430 463
431 bapi = bap[i]; 464 bapi = bap[i];
432 switch (bapi) { 465 switch (bapi) {
433 case 0: 466 case 0:
434 cplcoeff = LEVEL_3DB * scale_factor[exp[i]];
435 for (ch = 0; ch < nfchans; ch++) 467 for (ch = 0; ch < nfchans; ch++)
436 if ((state->chincpl >> ch) & 1) { 468 if ((state->chincpl >> ch) & 1) {
437 if (dithflag[ch]) 469 if (dithflag[ch])
438 samples[ch][i] = (cplcoeff * cplco[ch] * 470 #ifndef LIBA52_FIXED
439 dither_gen ()); 471 samples[ch][i] = (scale_factor[exp[i]] *
472 cplco[ch] * dither_gen (state));
473 #else
474 COEFF (samples[ch][i], dither_gen (state),
475 cplco[ch], scale_factor, exp[i]);
476 #endif
440 else 477 else
441 samples[ch][i] = 0; 478 samples[ch][i] = 0;
442 } 479 }
443 i++; 480 i++;
444 continue; 481 continue;
445 482
446 case -1: 483 case -1:
447 if (quantizer->q1_ptr >= 0) { 484 if (quant->q1_ptr >= 0) {
448 cplcoeff = quantizer->q1[quantizer->q1_ptr--]; 485 cplcoeff = quant->q1[quant->q1_ptr--];
449 break; 486 break;
450 } else { 487 } else {
451 int code; 488 int code;
452 489
453 code = bitstream_get (5); 490 code = bitstream_get (state, 5);
454 491
455 quantizer->q1_ptr = 1; 492 quant->q1_ptr = 1;
456 quantizer->q1[0] = q_1_2[code]; 493 quant->q1[0] = q_1_2[code];
457 quantizer->q1[1] = q_1_1[code]; 494 quant->q1[1] = q_1_1[code];
458 cplcoeff = q_1_0[code]; 495 cplcoeff = q_1_0[code];
459 break; 496 break;
460 } 497 }
461 498
462 case -2: 499 case -2:
463 if (quantizer->q2_ptr >= 0) { 500 if (quant->q2_ptr >= 0) {
464 cplcoeff = quantizer->q2[quantizer->q2_ptr--]; 501 cplcoeff = quant->q2[quant->q2_ptr--];
465 break; 502 break;
466 } else { 503 } else {
467 int code; 504 int code;
468 505
469 code = bitstream_get (7); 506 code = bitstream_get (state, 7);
470 507
471 quantizer->q2_ptr = 1; 508 quant->q2_ptr = 1;
472 quantizer->q2[0] = q_2_2[code]; 509 quant->q2[0] = q_2_2[code];
473 quantizer->q2[1] = q_2_1[code]; 510 quant->q2[1] = q_2_1[code];
474 cplcoeff = q_2_0[code]; 511 cplcoeff = q_2_0[code];
475 break; 512 break;
476 } 513 }
477 514
478 case 3: 515 case 3:
479 cplcoeff = q_3[bitstream_get (3)]; 516 cplcoeff = q_3[bitstream_get (state, 3)];
480 break; 517 break;
481 518
482 case -3: 519 case -3:
483 if (quantizer->q4_ptr == 0) { 520 if (quant->q4_ptr == 0) {
484 quantizer->q4_ptr = -1; 521 quant->q4_ptr = -1;
485 cplcoeff = quantizer->q4; 522 cplcoeff = quant->q4;
486 break; 523 break;
487 } else { 524 } else {
488 int code; 525 int code;
489 526
490 code = bitstream_get (7); 527 code = bitstream_get (state, 7);
491 528
492 quantizer->q4_ptr = 0; 529 quant->q4_ptr = 0;
493 quantizer->q4 = q_4_1[code]; 530 quant->q4 = q_4_1[code];
494 cplcoeff = q_4_0[code]; 531 cplcoeff = q_4_0[code];
495 break; 532 break;
496 } 533 }
497 534
498 case 4: 535 case 4:
499 cplcoeff = q_5[bitstream_get (4)]; 536 cplcoeff = q_5[bitstream_get (state, 4)];
500 break; 537 break;
501 538
502 default: 539 default:
503 cplcoeff = bitstream_get_2 (bapi) << (16 - bapi); 540 cplcoeff = bitstream_get_2 (state, bapi) << (16 - bapi);
504 } 541 }
505 542 #ifndef LIBA52_FIXED
506 cplcoeff *= scale_factor[exp[i]]; 543 cplcoeff *= scale_factor[exp[i]];
544 #endif
507 for (ch = 0; ch < nfchans; ch++) 545 for (ch = 0; ch < nfchans; ch++)
508 if ((state->chincpl >> ch) & 1) 546 if ((state->chincpl >> ch) & 1)
547 #ifndef LIBA52_FIXED
509 samples[ch][i] = cplcoeff * cplco[ch]; 548 samples[ch][i] = cplcoeff * cplco[ch];
549 #else
550 COEFF (samples[ch][i], cplcoeff, cplco[ch],
551 scale_factor, exp[i]);
552 #endif
510 i++; 553 i++;
511 } 554 }
512 } 555 }
513 } 556 }
514 557
517 static const uint8_t nfchans_tbl[] = {2, 1, 2, 3, 3, 4, 4, 5, 1, 1, 2}; 560 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}; 561 static int rematrix_band[4] = {25, 37, 61, 253};
519 int i, nfchans, chaninfo; 562 int i, nfchans, chaninfo;
520 uint8_t cplexpstr, chexpstr[5], lfeexpstr, do_bit_alloc, done_cpl; 563 uint8_t cplexpstr, chexpstr[5], lfeexpstr, do_bit_alloc, done_cpl;
521 uint8_t blksw[5], dithflag[5]; 564 uint8_t blksw[5], dithflag[5];
522 sample_t coeff[5]; 565 level_t coeff[5];
523 int chanbias; 566 int chanbias;
524 quantizer_t quantizer; 567 quantizer_set_t quant;
525 sample_t * samples; 568 sample_t * samples;
526 569
527 nfchans = nfchans_tbl[state->acmod]; 570 nfchans = nfchans_tbl[state->acmod];
528 571
529 for (i = 0; i < nfchans; i++) 572 for (i = 0; i < nfchans; i++)
530 blksw[i] = bitstream_get (1); 573 blksw[i] = bitstream_get (state, 1);
531 574
532 for (i = 0; i < nfchans; i++) 575 for (i = 0; i < nfchans; i++)
533 dithflag[i] = bitstream_get (1); 576 dithflag[i] = bitstream_get (state, 1);
534 577
535 chaninfo = !state->acmod; 578 chaninfo = !state->acmod;
536 do { 579 do {
537 if (bitstream_get (1)) { /* dynrnge */ 580 if (bitstream_get (state, 1)) { /* dynrnge */
538 int dynrng; 581 int dynrng;
539 582
540 dynrng = bitstream_get_2 (8); 583 dynrng = bitstream_get_2 (state, 8);
541 if (state->dynrnge) { 584 if (state->dynrnge) {
542 sample_t range; 585 level_t range;
543 586
587 #if !defined(LIBA52_FIXED)
544 range = ((((dynrng & 0x1f) | 0x20) << 13) * 588 range = ((((dynrng & 0x1f) | 0x20) << 13) *
545 scale_factor[3 - (dynrng >> 5)]); 589 scale_factor[3 - (dynrng >> 5)]);
590 #else
591 range = ((dynrng & 0x1f) | 0x20) << (21 + (dynrng >> 5));
592 #endif
546 if (state->dynrngcall) 593 if (state->dynrngcall)
547 range = state->dynrngcall (range, state->dynrngdata); 594 range = state->dynrngcall (range, state->dynrngdata);
548 state->dynrng = state->level * range; 595 state->dynrng = MUL_L (state->level, range);
549 } 596 }
550 } 597 }
551 } while (chaninfo--); 598 } while (chaninfo--);
552 599
553 if (bitstream_get (1)) { /* cplstre */ 600 if (bitstream_get (state, 1)) { /* cplstre */
554 state->chincpl = 0; 601 state->chincpl = 0;
555 if (bitstream_get (1)) { /* cplinu */ 602 if (bitstream_get (state, 1)) { /* cplinu */
556 static uint8_t bndtab[16] = {31, 35, 37, 39, 41, 42, 43, 44, 603 static uint8_t bndtab[16] = {31, 35, 37, 39, 41, 42, 43, 44,
557 45, 45, 46, 46, 47, 47, 48, 48}; 604 45, 45, 46, 46, 47, 47, 48, 48};
558 int cplbegf; 605 int cplbegf;
559 int cplendf; 606 int cplendf;
560 int ncplsubnd; 607 int ncplsubnd;
561 608
562 for (i = 0; i < nfchans; i++) 609 for (i = 0; i < nfchans; i++)
563 state->chincpl |= bitstream_get (1) << i; 610 state->chincpl |= bitstream_get (state, 1) << i;
564 switch (state->acmod) { 611 switch (state->acmod) {
565 case 0: case 1: 612 case 0: case 1:
566 return 1; 613 return 1;
567 case 2: 614 case 2:
568 state->phsflginu = bitstream_get (1); 615 state->phsflginu = bitstream_get (state, 1);
569 } 616 }
570 cplbegf = bitstream_get (4); 617 cplbegf = bitstream_get (state, 4);
571 cplendf = bitstream_get (4); 618 cplendf = bitstream_get (state, 4);
572 619
573 if (cplendf + 3 - cplbegf < 0) 620 if (cplendf + 3 - cplbegf < 0)
574 return 1; 621 return 1;
575 state->ncplbnd = ncplsubnd = cplendf + 3 - cplbegf; 622 state->ncplbnd = ncplsubnd = cplendf + 3 - cplbegf;
576 state->cplstrtbnd = bndtab[cplbegf]; 623 state->cplstrtbnd = bndtab[cplbegf];
577 state->cplstrtmant = cplbegf * 12 + 37; 624 state->cplstrtmant = cplbegf * 12 + 37;
578 state->cplendmant = cplendf * 12 + 73; 625 state->cplendmant = cplendf * 12 + 73;
579 626
580 state->cplbndstrc = 0; 627 state->cplbndstrc = 0;
581 for (i = 0; i < ncplsubnd - 1; i++) 628 for (i = 0; i < ncplsubnd - 1; i++)
582 if (bitstream_get (1)) { 629 if (bitstream_get (state, 1)) {
583 state->cplbndstrc |= 1 << i; 630 state->cplbndstrc |= 1 << i;
584 state->ncplbnd--; 631 state->ncplbnd--;
585 } 632 }
586 } 633 }
587 } 634 }
590 int j, cplcoe; 637 int j, cplcoe;
591 638
592 cplcoe = 0; 639 cplcoe = 0;
593 for (i = 0; i < nfchans; i++) 640 for (i = 0; i < nfchans; i++)
594 if ((state->chincpl) >> i & 1) 641 if ((state->chincpl) >> i & 1)
595 if (bitstream_get (1)) { /* cplcoe */ 642 if (bitstream_get (state, 1)) { /* cplcoe */
596 int mstrcplco, cplcoexp, cplcomant; 643 int mstrcplco, cplcoexp, cplcomant;
597 644
598 cplcoe = 1; 645 cplcoe = 1;
599 mstrcplco = 3 * bitstream_get (2); 646 mstrcplco = 3 * bitstream_get (state, 2);
600 for (j = 0; j < state->ncplbnd; j++) { 647 for (j = 0; j < state->ncplbnd; j++) {
601 cplcoexp = bitstream_get (4); 648 cplcoexp = bitstream_get (state, 4);
602 cplcomant = bitstream_get (4); 649 cplcomant = bitstream_get (state, 4);
603 if (cplcoexp == 15) 650 if (cplcoexp == 15)
604 cplcomant <<= 14; 651 cplcomant <<= 14;
605 else 652 else
606 cplcomant = (cplcomant | 0x10) << 13; 653 cplcomant = (cplcomant | 0x10) << 13;
654 #ifndef LIBA52_FIXED
607 state->cplco[i][j] = 655 state->cplco[i][j] =
608 cplcomant * scale_factor[cplcoexp + mstrcplco]; 656 cplcomant * scale_factor[cplcoexp + mstrcplco];
657 #else
658 state->cplco[i][j] = (cplcomant << 11) >> (cplcoexp + mstrcplco);
659 #endif
660
609 } 661 }
610 } 662 }
611 if ((state->acmod == 2) && state->phsflginu && cplcoe) 663 if ((state->acmod == 2) && state->phsflginu && cplcoe)
612 for (j = 0; j < state->ncplbnd; j++) 664 for (j = 0; j < state->ncplbnd; j++)
613 if (bitstream_get (1)) /* phsflg */ 665 if (bitstream_get (state, 1)) /* phsflg */
614 state->cplco[1][j] = -state->cplco[1][j]; 666 state->cplco[1][j] = -state->cplco[1][j];
615 } 667 }
616 668
617 if ((state->acmod == 2) && (bitstream_get (1))) { /* rematstr */ 669 if ((state->acmod == 2) && (bitstream_get (state, 1))) { /* rematstr */
618 int end; 670 int end;
619 671
620 state->rematflg = 0; 672 state->rematflg = 0;
621 end = (state->chincpl) ? state->cplstrtmant : 253; /* cplinu */ 673 end = (state->chincpl) ? state->cplstrtmant : 253; /* cplinu */
622 i = 0; 674 i = 0;
623 do 675 do
624 state->rematflg |= bitstream_get (1) << i; 676 state->rematflg |= bitstream_get (state, 1) << i;
625 while (rematrix_band[i++] < end); 677 while (rematrix_band[i++] < end);
626 } 678 }
627 679
628 cplexpstr = EXP_REUSE; 680 cplexpstr = EXP_REUSE;
629 lfeexpstr = EXP_REUSE; 681 lfeexpstr = EXP_REUSE;
630 if (state->chincpl) /* cplinu */ 682 if (state->chincpl) /* cplinu */
631 cplexpstr = bitstream_get (2); 683 cplexpstr = bitstream_get (state, 2);
632 for (i = 0; i < nfchans; i++) 684 for (i = 0; i < nfchans; i++)
633 chexpstr[i] = bitstream_get (2); 685 chexpstr[i] = bitstream_get (state, 2);
634 if (state->lfeon) 686 if (state->lfeon)
635 lfeexpstr = bitstream_get (1); 687 lfeexpstr = bitstream_get (state, 1);
636 688
637 for (i = 0; i < nfchans; i++) 689 for (i = 0; i < nfchans; i++)
638 if (chexpstr[i] != EXP_REUSE) { 690 if (chexpstr[i] != EXP_REUSE) {
639 if ((state->chincpl >> i) & 1) 691 if ((state->chincpl >> i) & 1)
640 state->endmant[i] = state->cplstrtmant; 692 state->endmant[i] = state->cplstrtmant;
641 else { 693 else {
642 int chbwcod; 694 int chbwcod;
643 695
644 chbwcod = bitstream_get (6); 696 chbwcod = bitstream_get (state, 6);
645 if (chbwcod > 60) 697 if (chbwcod > 60)
646 return 1; 698 return 1;
647 state->endmant[i] = chbwcod * 3 + 73; 699 state->endmant[i] = chbwcod * 3 + 73;
648 } 700 }
649 } 701 }
654 int cplabsexp, ncplgrps; 706 int cplabsexp, ncplgrps;
655 707
656 do_bit_alloc = 64; 708 do_bit_alloc = 64;
657 ncplgrps = ((state->cplendmant - state->cplstrtmant) / 709 ncplgrps = ((state->cplendmant - state->cplstrtmant) /
658 (3 << (cplexpstr - 1))); 710 (3 << (cplexpstr - 1)));
659 cplabsexp = bitstream_get (4) << 1; 711 cplabsexp = bitstream_get (state, 4) << 1;
660 if (parse_exponents (cplexpstr, ncplgrps, cplabsexp, 712 if (parse_exponents (state, cplexpstr, ncplgrps, cplabsexp,
661 state->cpl_expbap.exp + state->cplstrtmant)) 713 state->cpl_expbap.exp + state->cplstrtmant))
662 return 1; 714 return 1;
663 } 715 }
664 for (i = 0; i < nfchans; i++) 716 for (i = 0; i < nfchans; i++)
665 if (chexpstr[i] != EXP_REUSE) { 717 if (chexpstr[i] != EXP_REUSE) {
666 int grp_size, nchgrps; 718 int grp_size, nchgrps;
667 719
668 do_bit_alloc |= 1 << i; 720 do_bit_alloc |= 1 << i;
669 grp_size = 3 << (chexpstr[i] - 1); 721 grp_size = 3 << (chexpstr[i] - 1);
670 nchgrps = (state->endmant[i] + grp_size - 4) / grp_size; 722 nchgrps = (state->endmant[i] + grp_size - 4) / grp_size;
671 state->fbw_expbap[i].exp[0] = bitstream_get (4); 723 state->fbw_expbap[i].exp[0] = bitstream_get (state, 4);
672 if (parse_exponents (chexpstr[i], nchgrps, 724 if (parse_exponents (state, chexpstr[i], nchgrps,
673 state->fbw_expbap[i].exp[0], 725 state->fbw_expbap[i].exp[0],
674 state->fbw_expbap[i].exp + 1)) 726 state->fbw_expbap[i].exp + 1))
675 return 1; 727 return 1;
676 bitstream_get (2); /* gainrng */ 728 bitstream_get (state, 2); /* gainrng */
677 } 729 }
678 if (lfeexpstr != EXP_REUSE) { 730 if (lfeexpstr != EXP_REUSE) {
679 do_bit_alloc |= 32; 731 do_bit_alloc |= 32;
680 state->lfe_expbap.exp[0] = bitstream_get (4); 732 state->lfe_expbap.exp[0] = bitstream_get (state, 4);
681 if (parse_exponents (lfeexpstr, 2, state->lfe_expbap.exp[0], 733 if (parse_exponents (state, lfeexpstr, 2, state->lfe_expbap.exp[0],
682 state->lfe_expbap.exp + 1)) 734 state->lfe_expbap.exp + 1))
683 return 1; 735 return 1;
684 } 736 }
685 737
686 if (bitstream_get (1)) { /* baie */ 738 if (bitstream_get (state, 1)) { /* baie */
687 do_bit_alloc = -1; 739 do_bit_alloc = 127;
688 state->bai = bitstream_get (11); 740 state->bai = bitstream_get (state, 11);
689 } 741 }
690 if (bitstream_get (1)) { /* snroffste */ 742 if (bitstream_get (state, 1)) { /* snroffste */
691 do_bit_alloc = -1; 743 do_bit_alloc = 127;
692 state->csnroffst = bitstream_get (6); 744 state->csnroffst = bitstream_get (state, 6);
693 if (state->chincpl) /* cplinu */ 745 if (state->chincpl) /* cplinu */
694 state->cplba.bai = bitstream_get (7); 746 state->cplba.bai = bitstream_get (state, 7);
695 for (i = 0; i < nfchans; i++) 747 for (i = 0; i < nfchans; i++)
696 state->ba[i].bai = bitstream_get (7); 748 state->ba[i].bai = bitstream_get (state, 7);
697 if (state->lfeon) 749 if (state->lfeon)
698 state->lfeba.bai = bitstream_get (7); 750 state->lfeba.bai = bitstream_get (state, 7);
699 } 751 }
700 if ((state->chincpl) && (bitstream_get (1))) { /* cplinu, cplleake */ 752 if ((state->chincpl) && (bitstream_get (state, 1))) { /* cplleake */
701 do_bit_alloc |= 64; 753 do_bit_alloc |= 64;
702 state->cplfleak = 9 - bitstream_get (3); 754 state->cplfleak = 9 - bitstream_get (state, 3);
703 state->cplsleak = 9 - bitstream_get (3); 755 state->cplsleak = 9 - bitstream_get (state, 3);
704 } 756 }
705 757
706 if (bitstream_get (1)) { /* deltbaie */ 758 if (bitstream_get (state, 1)) { /* deltbaie */
707 do_bit_alloc = -1; 759 do_bit_alloc = 127;
708 if (state->chincpl) /* cplinu */ 760 if (state->chincpl) /* cplinu */
709 state->cplba.deltbae = bitstream_get (2); 761 state->cplba.deltbae = bitstream_get (state, 2);
710 for (i = 0; i < nfchans; i++) 762 for (i = 0; i < nfchans; i++)
711 state->ba[i].deltbae = bitstream_get (2); 763 state->ba[i].deltbae = bitstream_get (state, 2);
712 if (state->chincpl && /* cplinu */ 764 if (state->chincpl && /* cplinu */
713 (state->cplba.deltbae == DELTA_BIT_NEW) && 765 (state->cplba.deltbae == DELTA_BIT_NEW) &&
714 parse_deltba (state->cplba.deltba)) 766 parse_deltba (state, state->cplba.deltba))
715 return 1; 767 return 1;
716 for (i = 0; i < nfchans; i++) 768 for (i = 0; i < nfchans; i++)
717 if ((state->ba[i].deltbae == DELTA_BIT_NEW) && 769 if ((state->ba[i].deltbae == DELTA_BIT_NEW) &&
718 parse_deltba (state->ba[i].deltba)) 770 parse_deltba (state, state->ba[i].deltba))
719 return 1; 771 return 1;
720 } 772 }
721 773
722 if (do_bit_alloc) { 774 if (do_bit_alloc) {
723 if (zero_snr_offsets (nfchans, state)) { 775 if (zero_snr_offsets (nfchans, state)) {
743 &state->lfe_expbap); 795 &state->lfe_expbap);
744 } 796 }
745 } 797 }
746 } 798 }
747 799
748 if (bitstream_get (1)) { /* skiple */ 800 if (bitstream_get (state, 1)) { /* skiple */
749 i = bitstream_get (9); /* skipl */ 801 i = bitstream_get (state, 9); /* skipl */
750 while (i--) 802 while (i--)
751 bitstream_get (8); 803 bitstream_get (state, 8);
752 } 804 }
753 805
754 samples = state->samples; 806 samples = state->samples;
755 if (state->output & A52_LFE) 807 if (state->output & A52_LFE)
756 samples += 256; /* shift for LFE channel */ 808 samples += 256; /* shift for LFE channel */
757 809
758 chanbias = a52_downmix_coeff (coeff, state->acmod, state->output, 810 chanbias = a52_downmix_coeff (coeff, state->acmod, state->output,
759 state->dynrng, state->clev, state->slev); 811 state->dynrng, state->clev, state->slev);
760 812
761 quantizer.q1_ptr = quantizer.q2_ptr = quantizer.q4_ptr = -1; 813 quant.q1_ptr = quant.q2_ptr = quant.q4_ptr = -1;
762 done_cpl = 0; 814 done_cpl = 0;
763 815
764 for (i = 0; i < nfchans; i++) { 816 for (i = 0; i < nfchans; i++) {
765 int j; 817 int j;
766 818
767 coeff_get (samples + 256 * i, state->fbw_expbap +i, &quantizer, 819 coeff_get (state, samples + 256 * i, state->fbw_expbap +i, &quant,
768 coeff[i], dithflag[i], state->endmant[i]); 820 coeff[i], dithflag[i], state->endmant[i]);
769 821
770 if ((state->chincpl >> i) & 1) { 822 if ((state->chincpl >> i) & 1) {
771 if (!done_cpl) { 823 if (!done_cpl) {
772 done_cpl = 1; 824 done_cpl = 1;
773 coeff_get_coupling (state, nfchans, coeff, 825 coeff_get_coupling (state, nfchans, coeff,
774 (sample_t (*)[256])samples, &quantizer, 826 (sample_t (*)[256])samples, &quant,
775 dithflag); 827 dithflag);
776 } 828 }
777 j = state->cplendmant; 829 j = state->cplendmant;
778 } else 830 } else
779 j = state->endmant[i]; 831 j = state->endmant[i];
812 } while (j < end); 864 } while (j < end);
813 } 865 }
814 866
815 if (state->lfeon) { 867 if (state->lfeon) {
816 if (state->output & A52_LFE) { 868 if (state->output & A52_LFE) {
817 coeff_get (samples - 256, &state->lfe_expbap, &quantizer, 869 coeff_get (state, samples - 256, &state->lfe_expbap, &quant,
818 state->dynrng, 0, 7); 870 state->dynrng, 0, 7);
819 for (i = 7; i < 256; i++) 871 for (i = 7; i < 256; i++)
820 (samples-256)[i] = 0; 872 (samples-256)[i] = 0;
821 a52_imdct_512 (samples - 256, samples + 1536 - 256, state->bias); 873 a52_imdct_512 (samples - 256, samples + 1536 - 256, state->bias);
822 } else { 874 } else {
823 /* just skip the LFE coefficients */ 875 /* just skip the LFE coefficients */
824 coeff_get (samples + 1280, &state->lfe_expbap, &quantizer, 876 coeff_get (state, samples + 1280, &state->lfe_expbap, &quant,
825 0, 0, 7); 877 0, 0, 7);
826 } 878 }
827 } 879 }
828 880
829 i = 0; 881 i = 0;