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