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