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