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