annotate libac3/parse.c @ 44:92d51f683931 libavcodec

added forgotten emms() - fix various segmentation faults when using mjpeg
author glantau
date Wed, 08 Aug 2001 19:08:50 +0000
parents 986e461dc072
children 5aa6292a1660
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
986e461dc072 Initial revision
glantau
parents:
diff changeset
1 /*
986e461dc072 Initial revision
glantau
parents:
diff changeset
2 * parse.c
986e461dc072 Initial revision
glantau
parents:
diff changeset
3 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
4 * Copyright (C) Aaron Holtzman - May 1999
986e461dc072 Initial revision
glantau
parents:
diff changeset
5 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
6 * This file is part of ac3dec, a free Dolby AC-3 stream decoder.
986e461dc072 Initial revision
glantau
parents:
diff changeset
7 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
8 * ac3dec is free software; you can redistribute it and/or modify
986e461dc072 Initial revision
glantau
parents:
diff changeset
9 * it under the terms of the GNU General Public License as published by
986e461dc072 Initial revision
glantau
parents:
diff changeset
10 * the Free Software Foundation; either version 2, or (at your option)
986e461dc072 Initial revision
glantau
parents:
diff changeset
11 * any later version.
986e461dc072 Initial revision
glantau
parents:
diff changeset
12 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
13 * ac3dec is distributed in the hope that it will be useful,
986e461dc072 Initial revision
glantau
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
986e461dc072 Initial revision
glantau
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
986e461dc072 Initial revision
glantau
parents:
diff changeset
16 * GNU General Public License for more details.
986e461dc072 Initial revision
glantau
parents:
diff changeset
17 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
986e461dc072 Initial revision
glantau
parents:
diff changeset
19 * along with GNU Make; see the file COPYING. If not, write to
986e461dc072 Initial revision
glantau
parents:
diff changeset
20 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
986e461dc072 Initial revision
glantau
parents:
diff changeset
21 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
22 *
986e461dc072 Initial revision
glantau
parents:
diff changeset
23 */
986e461dc072 Initial revision
glantau
parents:
diff changeset
24
986e461dc072 Initial revision
glantau
parents:
diff changeset
25 #include <inttypes.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
26 #include <string.h>
986e461dc072 Initial revision
glantau
parents:
diff changeset
27
986e461dc072 Initial revision
glantau
parents:
diff changeset
28 #include "ac3.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
29 #include "ac3_internal.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
30
986e461dc072 Initial revision
glantau
parents:
diff changeset
31 #include "bitstream.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
32 #include "tables.h"
986e461dc072 Initial revision
glantau
parents:
diff changeset
33
986e461dc072 Initial revision
glantau
parents:
diff changeset
34 extern stream_samples_t samples; // FIXME
986e461dc072 Initial revision
glantau
parents:
diff changeset
35 static float delay[6][256];
986e461dc072 Initial revision
glantau
parents:
diff changeset
36
986e461dc072 Initial revision
glantau
parents:
diff changeset
37 void ac3_init (void)
986e461dc072 Initial revision
glantau
parents:
diff changeset
38 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
39 imdct_init ();
986e461dc072 Initial revision
glantau
parents:
diff changeset
40 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
41
986e461dc072 Initial revision
glantau
parents:
diff changeset
42 static uint8_t halfrate[12] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3};
986e461dc072 Initial revision
glantau
parents:
diff changeset
43
986e461dc072 Initial revision
glantau
parents:
diff changeset
44 int ac3_syncinfo (uint8_t * buf, int * flags,
986e461dc072 Initial revision
glantau
parents:
diff changeset
45 int * sample_rate, int * bit_rate)
986e461dc072 Initial revision
glantau
parents:
diff changeset
46 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
47 static int rate[] = { 32, 40, 48, 56, 64, 80, 96, 112,
986e461dc072 Initial revision
glantau
parents:
diff changeset
48 128, 160, 192, 224, 256, 320, 384, 448,
986e461dc072 Initial revision
glantau
parents:
diff changeset
49 512, 576, 640};
986e461dc072 Initial revision
glantau
parents:
diff changeset
50 static uint8_t lfeon[8] = {0x10, 0x10, 0x04, 0x04, 0x04, 0x01, 0x04, 0x01};
986e461dc072 Initial revision
glantau
parents:
diff changeset
51 int frmsizecod;
986e461dc072 Initial revision
glantau
parents:
diff changeset
52 int bitrate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
53 int half;
986e461dc072 Initial revision
glantau
parents:
diff changeset
54 int acmod;
986e461dc072 Initial revision
glantau
parents:
diff changeset
55
986e461dc072 Initial revision
glantau
parents:
diff changeset
56 if ((buf[0] != 0x0b) || (buf[1] != 0x77)) // syncword
986e461dc072 Initial revision
glantau
parents:
diff changeset
57 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
58
986e461dc072 Initial revision
glantau
parents:
diff changeset
59 if (buf[5] >= 0x60) // bsid >= 12
986e461dc072 Initial revision
glantau
parents:
diff changeset
60 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
61 half = halfrate[buf[5] >> 3];
986e461dc072 Initial revision
glantau
parents:
diff changeset
62
986e461dc072 Initial revision
glantau
parents:
diff changeset
63 // acmod, dsurmod and lfeon
986e461dc072 Initial revision
glantau
parents:
diff changeset
64 acmod = buf[6] >> 5;
986e461dc072 Initial revision
glantau
parents:
diff changeset
65 *flags = (((buf[6] & 0xf8) == 0x50) ? AC3_DOLBY : acmod) |
986e461dc072 Initial revision
glantau
parents:
diff changeset
66 ((buf[6] & lfeon[acmod]) ? AC3_LFE : 0);
986e461dc072 Initial revision
glantau
parents:
diff changeset
67
986e461dc072 Initial revision
glantau
parents:
diff changeset
68 frmsizecod = buf[4] & 63;
986e461dc072 Initial revision
glantau
parents:
diff changeset
69 if (frmsizecod >= 38)
986e461dc072 Initial revision
glantau
parents:
diff changeset
70 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
71 bitrate = rate [frmsizecod >> 1];
986e461dc072 Initial revision
glantau
parents:
diff changeset
72 *bit_rate = (bitrate * 1000) >> half;
986e461dc072 Initial revision
glantau
parents:
diff changeset
73
986e461dc072 Initial revision
glantau
parents:
diff changeset
74 switch (buf[4] & 0xc0) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
75 case 0: // 48 KHz
986e461dc072 Initial revision
glantau
parents:
diff changeset
76 *sample_rate = 48000 >> half;
986e461dc072 Initial revision
glantau
parents:
diff changeset
77 return 4 * bitrate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
78 case 0x40:
986e461dc072 Initial revision
glantau
parents:
diff changeset
79 *sample_rate = 44100 >> half;
986e461dc072 Initial revision
glantau
parents:
diff changeset
80 return 2 * (320 * bitrate / 147 + (frmsizecod & 1));
986e461dc072 Initial revision
glantau
parents:
diff changeset
81 case 0x80:
986e461dc072 Initial revision
glantau
parents:
diff changeset
82 *sample_rate = 32000 >> half;
986e461dc072 Initial revision
glantau
parents:
diff changeset
83 return 6 * bitrate;
986e461dc072 Initial revision
glantau
parents:
diff changeset
84 default:
986e461dc072 Initial revision
glantau
parents:
diff changeset
85 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
86 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
87 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
88
986e461dc072 Initial revision
glantau
parents:
diff changeset
89 int ac3_frame (ac3_state_t * state, uint8_t * buf, int * flags, float * level,
986e461dc072 Initial revision
glantau
parents:
diff changeset
90 float bias)
986e461dc072 Initial revision
glantau
parents:
diff changeset
91 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
92 static float clev[4] = {LEVEL_3DB, LEVEL_45DB, LEVEL_6DB, LEVEL_45DB};
986e461dc072 Initial revision
glantau
parents:
diff changeset
93 static float slev[4] = {LEVEL_3DB, LEVEL_6DB, 0, LEVEL_6DB};
986e461dc072 Initial revision
glantau
parents:
diff changeset
94 int chaninfo;
986e461dc072 Initial revision
glantau
parents:
diff changeset
95 int acmod;
986e461dc072 Initial revision
glantau
parents:
diff changeset
96
986e461dc072 Initial revision
glantau
parents:
diff changeset
97 state->fscod = buf[4] >> 6;
986e461dc072 Initial revision
glantau
parents:
diff changeset
98 state->halfrate = halfrate[buf[5] >> 3];
986e461dc072 Initial revision
glantau
parents:
diff changeset
99 state->acmod = acmod = buf[6] >> 5;
986e461dc072 Initial revision
glantau
parents:
diff changeset
100
986e461dc072 Initial revision
glantau
parents:
diff changeset
101 bitstream_set_ptr (buf + 6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
102 bitstream_get (3); // skip acmod we already parsed
986e461dc072 Initial revision
glantau
parents:
diff changeset
103
986e461dc072 Initial revision
glantau
parents:
diff changeset
104 if ((acmod == 2) && (bitstream_get (2) == 2)) // dsurmod
986e461dc072 Initial revision
glantau
parents:
diff changeset
105 acmod = AC3_DOLBY;
986e461dc072 Initial revision
glantau
parents:
diff changeset
106
986e461dc072 Initial revision
glantau
parents:
diff changeset
107 if ((acmod & 1) && (acmod != 1))
986e461dc072 Initial revision
glantau
parents:
diff changeset
108 state->clev = clev[bitstream_get (2)]; // cmixlev
986e461dc072 Initial revision
glantau
parents:
diff changeset
109
986e461dc072 Initial revision
glantau
parents:
diff changeset
110 if (acmod & 4)
986e461dc072 Initial revision
glantau
parents:
diff changeset
111 state->slev = slev[bitstream_get (2)]; // surmixlev
986e461dc072 Initial revision
glantau
parents:
diff changeset
112
986e461dc072 Initial revision
glantau
parents:
diff changeset
113 state->lfeon = bitstream_get (1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
114
986e461dc072 Initial revision
glantau
parents:
diff changeset
115 state->output = downmix_init (acmod, *flags, level,
986e461dc072 Initial revision
glantau
parents:
diff changeset
116 state->clev, state->slev);
986e461dc072 Initial revision
glantau
parents:
diff changeset
117 if (state->output < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
118 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
119 *flags = state->output;
986e461dc072 Initial revision
glantau
parents:
diff changeset
120 state->level = *level;
986e461dc072 Initial revision
glantau
parents:
diff changeset
121 state->bias = bias;
986e461dc072 Initial revision
glantau
parents:
diff changeset
122
986e461dc072 Initial revision
glantau
parents:
diff changeset
123 chaninfo = !acmod;
986e461dc072 Initial revision
glantau
parents:
diff changeset
124 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
125 bitstream_get (5); // dialnorm
986e461dc072 Initial revision
glantau
parents:
diff changeset
126 if (bitstream_get (1)) // compre
986e461dc072 Initial revision
glantau
parents:
diff changeset
127 bitstream_get (8); // compr
986e461dc072 Initial revision
glantau
parents:
diff changeset
128 if (bitstream_get (1)) // langcode
986e461dc072 Initial revision
glantau
parents:
diff changeset
129 bitstream_get (8); // langcod
986e461dc072 Initial revision
glantau
parents:
diff changeset
130 if (bitstream_get (1)) // audprodie
986e461dc072 Initial revision
glantau
parents:
diff changeset
131 bitstream_get (7); // mixlevel + roomtyp
986e461dc072 Initial revision
glantau
parents:
diff changeset
132 } while (chaninfo--);
986e461dc072 Initial revision
glantau
parents:
diff changeset
133
986e461dc072 Initial revision
glantau
parents:
diff changeset
134 bitstream_get (2); // copyrightb + origbs
986e461dc072 Initial revision
glantau
parents:
diff changeset
135
986e461dc072 Initial revision
glantau
parents:
diff changeset
136 if (bitstream_get (1)) // timecod1e
986e461dc072 Initial revision
glantau
parents:
diff changeset
137 bitstream_get (14); // timecod1
986e461dc072 Initial revision
glantau
parents:
diff changeset
138 if (bitstream_get (1)) // timecod2e
986e461dc072 Initial revision
glantau
parents:
diff changeset
139 bitstream_get (14); // timecod2
986e461dc072 Initial revision
glantau
parents:
diff changeset
140
986e461dc072 Initial revision
glantau
parents:
diff changeset
141 if (bitstream_get (1)) { // addbsie
986e461dc072 Initial revision
glantau
parents:
diff changeset
142 int addbsil;
986e461dc072 Initial revision
glantau
parents:
diff changeset
143
986e461dc072 Initial revision
glantau
parents:
diff changeset
144 addbsil = bitstream_get (6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
145 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
146 bitstream_get (8); // addbsi
986e461dc072 Initial revision
glantau
parents:
diff changeset
147 } while (addbsil--);
986e461dc072 Initial revision
glantau
parents:
diff changeset
148 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
149
986e461dc072 Initial revision
glantau
parents:
diff changeset
150 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
151 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
152
986e461dc072 Initial revision
glantau
parents:
diff changeset
153 static int parse_exponents (int expstr, int ngrps, uint8_t exponent,
986e461dc072 Initial revision
glantau
parents:
diff changeset
154 uint8_t * dest)
986e461dc072 Initial revision
glantau
parents:
diff changeset
155 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
156 int exps;
986e461dc072 Initial revision
glantau
parents:
diff changeset
157
986e461dc072 Initial revision
glantau
parents:
diff changeset
158 while (ngrps--) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
159 exps = bitstream_get (7);
986e461dc072 Initial revision
glantau
parents:
diff changeset
160
986e461dc072 Initial revision
glantau
parents:
diff changeset
161 exponent += exp_1[exps];
986e461dc072 Initial revision
glantau
parents:
diff changeset
162 if (exponent > 24)
986e461dc072 Initial revision
glantau
parents:
diff changeset
163 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
164
986e461dc072 Initial revision
glantau
parents:
diff changeset
165 switch (expstr) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
166 case EXP_D45:
986e461dc072 Initial revision
glantau
parents:
diff changeset
167 *(dest++) = exponent;
986e461dc072 Initial revision
glantau
parents:
diff changeset
168 *(dest++) = exponent;
986e461dc072 Initial revision
glantau
parents:
diff changeset
169 case EXP_D25:
986e461dc072 Initial revision
glantau
parents:
diff changeset
170 *(dest++) = exponent;
986e461dc072 Initial revision
glantau
parents:
diff changeset
171 case EXP_D15:
986e461dc072 Initial revision
glantau
parents:
diff changeset
172 *(dest++) = exponent;
986e461dc072 Initial revision
glantau
parents:
diff changeset
173 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
174
986e461dc072 Initial revision
glantau
parents:
diff changeset
175 exponent += exp_2[exps];
986e461dc072 Initial revision
glantau
parents:
diff changeset
176 if (exponent > 24)
986e461dc072 Initial revision
glantau
parents:
diff changeset
177 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
178
986e461dc072 Initial revision
glantau
parents:
diff changeset
179 switch (expstr) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
180 case EXP_D45:
986e461dc072 Initial revision
glantau
parents:
diff changeset
181 *(dest++) = exponent;
986e461dc072 Initial revision
glantau
parents:
diff changeset
182 *(dest++) = exponent;
986e461dc072 Initial revision
glantau
parents:
diff changeset
183 case EXP_D25:
986e461dc072 Initial revision
glantau
parents:
diff changeset
184 *(dest++) = exponent;
986e461dc072 Initial revision
glantau
parents:
diff changeset
185 case EXP_D15:
986e461dc072 Initial revision
glantau
parents:
diff changeset
186 *(dest++) = exponent;
986e461dc072 Initial revision
glantau
parents:
diff changeset
187 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
188
986e461dc072 Initial revision
glantau
parents:
diff changeset
189 exponent += exp_3[exps];
986e461dc072 Initial revision
glantau
parents:
diff changeset
190 if (exponent > 24)
986e461dc072 Initial revision
glantau
parents:
diff changeset
191 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
192
986e461dc072 Initial revision
glantau
parents:
diff changeset
193 switch (expstr) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
194 case EXP_D45:
986e461dc072 Initial revision
glantau
parents:
diff changeset
195 *(dest++) = exponent;
986e461dc072 Initial revision
glantau
parents:
diff changeset
196 *(dest++) = exponent;
986e461dc072 Initial revision
glantau
parents:
diff changeset
197 case EXP_D25:
986e461dc072 Initial revision
glantau
parents:
diff changeset
198 *(dest++) = exponent;
986e461dc072 Initial revision
glantau
parents:
diff changeset
199 case EXP_D15:
986e461dc072 Initial revision
glantau
parents:
diff changeset
200 *(dest++) = exponent;
986e461dc072 Initial revision
glantau
parents:
diff changeset
201 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
202 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
203
986e461dc072 Initial revision
glantau
parents:
diff changeset
204 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
205 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
206
986e461dc072 Initial revision
glantau
parents:
diff changeset
207 static int parse_deltba (int8_t * deltba)
986e461dc072 Initial revision
glantau
parents:
diff changeset
208 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
209 int deltnseg, deltlen, delta, j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
210
986e461dc072 Initial revision
glantau
parents:
diff changeset
211 memset (deltba, 0, 50);
986e461dc072 Initial revision
glantau
parents:
diff changeset
212
986e461dc072 Initial revision
glantau
parents:
diff changeset
213 deltnseg = bitstream_get (3);
986e461dc072 Initial revision
glantau
parents:
diff changeset
214 j = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
215 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
216 j += bitstream_get (5);
986e461dc072 Initial revision
glantau
parents:
diff changeset
217 deltlen = bitstream_get (4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
218 delta = bitstream_get (3);
986e461dc072 Initial revision
glantau
parents:
diff changeset
219 delta -= (delta >= 4) ? 3 : 4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
220 if (!deltlen)
986e461dc072 Initial revision
glantau
parents:
diff changeset
221 continue;
986e461dc072 Initial revision
glantau
parents:
diff changeset
222 if (j + deltlen >= 50)
986e461dc072 Initial revision
glantau
parents:
diff changeset
223 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
224 while (deltlen--)
986e461dc072 Initial revision
glantau
parents:
diff changeset
225 deltba[j++] = delta;
986e461dc072 Initial revision
glantau
parents:
diff changeset
226 } while (deltnseg--);
986e461dc072 Initial revision
glantau
parents:
diff changeset
227
986e461dc072 Initial revision
glantau
parents:
diff changeset
228 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
229 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
230
986e461dc072 Initial revision
glantau
parents:
diff changeset
231 static inline int zero_snr_offsets (int nfchans, ac3_state_t * state)
986e461dc072 Initial revision
glantau
parents:
diff changeset
232 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
233 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
234
986e461dc072 Initial revision
glantau
parents:
diff changeset
235 if ((state->csnroffst) || (state->cplinu && state->cplba.fsnroffst) ||
986e461dc072 Initial revision
glantau
parents:
diff changeset
236 (state->lfeon && state->lfeba.fsnroffst))
986e461dc072 Initial revision
glantau
parents:
diff changeset
237 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
238 for (i = 0; i < nfchans; i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
239 if (state->ba[i].fsnroffst)
986e461dc072 Initial revision
glantau
parents:
diff changeset
240 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
241 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
242 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
243
986e461dc072 Initial revision
glantau
parents:
diff changeset
244 static float q_1[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
245 static float q_2[2];
986e461dc072 Initial revision
glantau
parents:
diff changeset
246 static float q_4;
986e461dc072 Initial revision
glantau
parents:
diff changeset
247 static int q_1_pointer;
986e461dc072 Initial revision
glantau
parents:
diff changeset
248 static int q_2_pointer;
986e461dc072 Initial revision
glantau
parents:
diff changeset
249 static int q_4_pointer;
986e461dc072 Initial revision
glantau
parents:
diff changeset
250
986e461dc072 Initial revision
glantau
parents:
diff changeset
251 #define GET_COEFF(COEFF,DITHER) \
986e461dc072 Initial revision
glantau
parents:
diff changeset
252 switch (bap[i]) { \
986e461dc072 Initial revision
glantau
parents:
diff changeset
253 case 0: \
986e461dc072 Initial revision
glantau
parents:
diff changeset
254 DITHER (scale_factor[exp[i]]); \
986e461dc072 Initial revision
glantau
parents:
diff changeset
255 \
986e461dc072 Initial revision
glantau
parents:
diff changeset
256 case -1: \
986e461dc072 Initial revision
glantau
parents:
diff changeset
257 if (q_1_pointer >= 0) { \
986e461dc072 Initial revision
glantau
parents:
diff changeset
258 COEFF (q_1[q_1_pointer--] * scale_factor[exp[i]]); \
986e461dc072 Initial revision
glantau
parents:
diff changeset
259 } else { \
986e461dc072 Initial revision
glantau
parents:
diff changeset
260 int code; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
261 \
986e461dc072 Initial revision
glantau
parents:
diff changeset
262 code = bitstream_get (5); \
986e461dc072 Initial revision
glantau
parents:
diff changeset
263 \
986e461dc072 Initial revision
glantau
parents:
diff changeset
264 q_1_pointer = 1; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
265 q_1[0] = q_1_2[code]; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
266 q_1[1] = q_1_1[code]; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
267 COEFF (q_1_0[code] * scale_factor[exp[i]]); \
986e461dc072 Initial revision
glantau
parents:
diff changeset
268 } \
986e461dc072 Initial revision
glantau
parents:
diff changeset
269 \
986e461dc072 Initial revision
glantau
parents:
diff changeset
270 case -2: \
986e461dc072 Initial revision
glantau
parents:
diff changeset
271 if (q_2_pointer >= 0) { \
986e461dc072 Initial revision
glantau
parents:
diff changeset
272 COEFF (q_2[q_2_pointer--] * scale_factor[exp[i]]); \
986e461dc072 Initial revision
glantau
parents:
diff changeset
273 } else { \
986e461dc072 Initial revision
glantau
parents:
diff changeset
274 int code; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
275 \
986e461dc072 Initial revision
glantau
parents:
diff changeset
276 code = bitstream_get (7); \
986e461dc072 Initial revision
glantau
parents:
diff changeset
277 \
986e461dc072 Initial revision
glantau
parents:
diff changeset
278 q_2_pointer = 1; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
279 q_2[0] = q_2_2[code]; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
280 q_2[1] = q_2_1[code]; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
281 COEFF (q_2_0[code] * scale_factor[exp[i]]); \
986e461dc072 Initial revision
glantau
parents:
diff changeset
282 } \
986e461dc072 Initial revision
glantau
parents:
diff changeset
283 \
986e461dc072 Initial revision
glantau
parents:
diff changeset
284 case 3: \
986e461dc072 Initial revision
glantau
parents:
diff changeset
285 COEFF (q_3[bitstream_get (3)] * scale_factor[exp[i]]); \
986e461dc072 Initial revision
glantau
parents:
diff changeset
286 \
986e461dc072 Initial revision
glantau
parents:
diff changeset
287 case -3: \
986e461dc072 Initial revision
glantau
parents:
diff changeset
288 if (q_4_pointer == 0) { \
986e461dc072 Initial revision
glantau
parents:
diff changeset
289 q_4_pointer = -1; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
290 COEFF (q_4 * scale_factor[exp[i]]); \
986e461dc072 Initial revision
glantau
parents:
diff changeset
291 } else { \
986e461dc072 Initial revision
glantau
parents:
diff changeset
292 int code; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
293 \
986e461dc072 Initial revision
glantau
parents:
diff changeset
294 code = bitstream_get (7); \
986e461dc072 Initial revision
glantau
parents:
diff changeset
295 \
986e461dc072 Initial revision
glantau
parents:
diff changeset
296 q_4_pointer = 0; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
297 q_4 = q_4_1[code]; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
298 COEFF (q_4_0[code] * scale_factor[exp[i]]); \
986e461dc072 Initial revision
glantau
parents:
diff changeset
299 } \
986e461dc072 Initial revision
glantau
parents:
diff changeset
300 \
986e461dc072 Initial revision
glantau
parents:
diff changeset
301 case 4: \
986e461dc072 Initial revision
glantau
parents:
diff changeset
302 COEFF (q_5[bitstream_get (4)] * scale_factor[exp[i]]); \
986e461dc072 Initial revision
glantau
parents:
diff changeset
303 \
986e461dc072 Initial revision
glantau
parents:
diff changeset
304 default: \
986e461dc072 Initial revision
glantau
parents:
diff changeset
305 COEFF (((int16_t)(bitstream_get(bap[i]) << (16 - bap[i]))) * \
986e461dc072 Initial revision
glantau
parents:
diff changeset
306 scale_factor[exp[i]]); \
986e461dc072 Initial revision
glantau
parents:
diff changeset
307 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
308
986e461dc072 Initial revision
glantau
parents:
diff changeset
309 #define CHANNEL_COEFF(val) \
986e461dc072 Initial revision
glantau
parents:
diff changeset
310 coeff[i++] = val; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
311 continue;
986e461dc072 Initial revision
glantau
parents:
diff changeset
312
986e461dc072 Initial revision
glantau
parents:
diff changeset
313 #define CHANNEL_DITHER(val) \
986e461dc072 Initial revision
glantau
parents:
diff changeset
314 if (dither) { \
986e461dc072 Initial revision
glantau
parents:
diff changeset
315 coeff[i++] = dither_gen () * val; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
316 continue; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
317 } else { \
986e461dc072 Initial revision
glantau
parents:
diff changeset
318 coeff[i++] = 0; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
319 continue; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
320 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
321
986e461dc072 Initial revision
glantau
parents:
diff changeset
322 static uint16_t lfsr_state = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
323
986e461dc072 Initial revision
glantau
parents:
diff changeset
324 static inline int16_t dither_gen(void)
986e461dc072 Initial revision
glantau
parents:
diff changeset
325 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
326 int16_t state;
986e461dc072 Initial revision
glantau
parents:
diff changeset
327
986e461dc072 Initial revision
glantau
parents:
diff changeset
328 state = dither_lut[lfsr_state >> 8] ^ (lfsr_state << 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
329
986e461dc072 Initial revision
glantau
parents:
diff changeset
330 lfsr_state = (uint16_t) state;
986e461dc072 Initial revision
glantau
parents:
diff changeset
331
986e461dc072 Initial revision
glantau
parents:
diff changeset
332 return ((state * (int) (LEVEL_3DB * 256)) >> 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
333 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
334
986e461dc072 Initial revision
glantau
parents:
diff changeset
335 static void coeff_get (float * coeff, uint8_t * exp, int8_t * bap,
986e461dc072 Initial revision
glantau
parents:
diff changeset
336 int dither, int end)
986e461dc072 Initial revision
glantau
parents:
diff changeset
337 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
338 int i;
986e461dc072 Initial revision
glantau
parents:
diff changeset
339
986e461dc072 Initial revision
glantau
parents:
diff changeset
340 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
341 while (i < end)
986e461dc072 Initial revision
glantau
parents:
diff changeset
342 GET_COEFF (CHANNEL_COEFF, CHANNEL_DITHER);
986e461dc072 Initial revision
glantau
parents:
diff changeset
343 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
344
986e461dc072 Initial revision
glantau
parents:
diff changeset
345 #define COUPLING_COEFF(val) \
986e461dc072 Initial revision
glantau
parents:
diff changeset
346 cplcoeff = val; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
347 break;
986e461dc072 Initial revision
glantau
parents:
diff changeset
348
986e461dc072 Initial revision
glantau
parents:
diff changeset
349 #define COUPLING_DITHER(val) \
986e461dc072 Initial revision
glantau
parents:
diff changeset
350 cplcoeff = val; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
351 for (ch = 0; ch < nfchans; ch++) \
986e461dc072 Initial revision
glantau
parents:
diff changeset
352 if (state->chincpl[ch]) { \
986e461dc072 Initial revision
glantau
parents:
diff changeset
353 if (dithflag[ch]) \
986e461dc072 Initial revision
glantau
parents:
diff changeset
354 samples[ch][i] = \
986e461dc072 Initial revision
glantau
parents:
diff changeset
355 state->cplco[ch][bnd] * dither_gen () * cplcoeff; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
356 else \
986e461dc072 Initial revision
glantau
parents:
diff changeset
357 samples[ch][i] = 0; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
358 } \
986e461dc072 Initial revision
glantau
parents:
diff changeset
359 i++; \
986e461dc072 Initial revision
glantau
parents:
diff changeset
360 continue;
986e461dc072 Initial revision
glantau
parents:
diff changeset
361
986e461dc072 Initial revision
glantau
parents:
diff changeset
362 int ac3_block (ac3_state_t * state)
986e461dc072 Initial revision
glantau
parents:
diff changeset
363 {
986e461dc072 Initial revision
glantau
parents:
diff changeset
364 static const uint8_t nfchans_tbl[8] = {2, 1, 2, 3, 3, 4, 4, 5};
986e461dc072 Initial revision
glantau
parents:
diff changeset
365 static int rematrix_band[4] = {25, 37, 61, 253};
986e461dc072 Initial revision
glantau
parents:
diff changeset
366 int i, nfchans, chaninfo;
986e461dc072 Initial revision
glantau
parents:
diff changeset
367 uint8_t cplexpstr, chexpstr[5], lfeexpstr, do_bit_alloc, done_cpl;
986e461dc072 Initial revision
glantau
parents:
diff changeset
368 uint8_t blksw[5], dithflag[5];
986e461dc072 Initial revision
glantau
parents:
diff changeset
369
986e461dc072 Initial revision
glantau
parents:
diff changeset
370 nfchans = nfchans_tbl[state->acmod];
986e461dc072 Initial revision
glantau
parents:
diff changeset
371
986e461dc072 Initial revision
glantau
parents:
diff changeset
372 for (i = 0; i < nfchans; i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
373 blksw[i] = bitstream_get (1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
374
986e461dc072 Initial revision
glantau
parents:
diff changeset
375 for (i = 0; i < nfchans; i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
376 dithflag[i] = bitstream_get (1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
377
986e461dc072 Initial revision
glantau
parents:
diff changeset
378 chaninfo = !(state->acmod);
986e461dc072 Initial revision
glantau
parents:
diff changeset
379 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
380 if (bitstream_get (1)) // dynrnge
986e461dc072 Initial revision
glantau
parents:
diff changeset
381 bitstream_get (8); // dynrng
986e461dc072 Initial revision
glantau
parents:
diff changeset
382 } while (chaninfo--);
986e461dc072 Initial revision
glantau
parents:
diff changeset
383
986e461dc072 Initial revision
glantau
parents:
diff changeset
384 if (bitstream_get (1)) { // cplstre
986e461dc072 Initial revision
glantau
parents:
diff changeset
385 state->cplinu = bitstream_get (1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
386 if (state->cplinu) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
387 static int bndtab[16] = {31, 35, 37, 39, 41, 42, 43, 44,
986e461dc072 Initial revision
glantau
parents:
diff changeset
388 45, 45, 46, 46, 47, 47, 48, 48};
986e461dc072 Initial revision
glantau
parents:
diff changeset
389 int cplbegf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
390 int cplendf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
391 int ncplsubnd;
986e461dc072 Initial revision
glantau
parents:
diff changeset
392
986e461dc072 Initial revision
glantau
parents:
diff changeset
393 for (i = 0; i < nfchans; i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
394 state->chincpl[i] = bitstream_get (1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
395 switch (state->acmod) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
396 case 0: case 1:
986e461dc072 Initial revision
glantau
parents:
diff changeset
397 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
398 case 2:
986e461dc072 Initial revision
glantau
parents:
diff changeset
399 state->phsflginu = bitstream_get (1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
400 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
401 cplbegf = bitstream_get (4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
402 cplendf = bitstream_get (4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
403
986e461dc072 Initial revision
glantau
parents:
diff changeset
404 if (cplendf + 3 - cplbegf < 0)
986e461dc072 Initial revision
glantau
parents:
diff changeset
405 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
406 state->ncplbnd = ncplsubnd = cplendf + 3 - cplbegf;
986e461dc072 Initial revision
glantau
parents:
diff changeset
407 state->cplstrtbnd = bndtab[cplbegf];
986e461dc072 Initial revision
glantau
parents:
diff changeset
408 state->cplstrtmant = cplbegf * 12 + 37;
986e461dc072 Initial revision
glantau
parents:
diff changeset
409 state->cplendmant = cplendf * 12 + 73;
986e461dc072 Initial revision
glantau
parents:
diff changeset
410
986e461dc072 Initial revision
glantau
parents:
diff changeset
411 for (i = 0; i < ncplsubnd - 1; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
412 state->cplbndstrc[i] = bitstream_get (1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
413 state->ncplbnd -= state->cplbndstrc[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
414 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
415 state->cplbndstrc[i] = 0; // last value is a sentinel
986e461dc072 Initial revision
glantau
parents:
diff changeset
416 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
417 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
418
986e461dc072 Initial revision
glantau
parents:
diff changeset
419 if (state->cplinu) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
420 int j, cplcoe;
986e461dc072 Initial revision
glantau
parents:
diff changeset
421
986e461dc072 Initial revision
glantau
parents:
diff changeset
422 cplcoe = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
423 for (i = 0; i < nfchans; i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
424 if (state->chincpl[i])
986e461dc072 Initial revision
glantau
parents:
diff changeset
425 if (bitstream_get (1)) { // cplcoe
986e461dc072 Initial revision
glantau
parents:
diff changeset
426 int mstrcplco, cplcoexp, cplcomant;
986e461dc072 Initial revision
glantau
parents:
diff changeset
427
986e461dc072 Initial revision
glantau
parents:
diff changeset
428 cplcoe = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
429 mstrcplco = 3 * bitstream_get (2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
430 for (j = 0; j < state->ncplbnd; j++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
431 cplcoexp = bitstream_get (4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
432 cplcomant = bitstream_get (4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
433 if (cplcoexp == 15)
986e461dc072 Initial revision
glantau
parents:
diff changeset
434 cplcomant <<= 14;
986e461dc072 Initial revision
glantau
parents:
diff changeset
435 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
436 cplcomant = (cplcomant | 0x10) << 13;
986e461dc072 Initial revision
glantau
parents:
diff changeset
437 state->cplco[i][j] =
986e461dc072 Initial revision
glantau
parents:
diff changeset
438 cplcomant * scale_factor[cplcoexp + mstrcplco];
986e461dc072 Initial revision
glantau
parents:
diff changeset
439 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
440 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
441 if ((state->acmod == 2) && state->phsflginu && cplcoe)
986e461dc072 Initial revision
glantau
parents:
diff changeset
442 for (j = 0; j < state->ncplbnd; j++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
443 if (bitstream_get (1)) // phsflg
986e461dc072 Initial revision
glantau
parents:
diff changeset
444 state->cplco[1][j] = -state->cplco[1][j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
445 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
446
986e461dc072 Initial revision
glantau
parents:
diff changeset
447 if ((state->acmod == 2) && (bitstream_get (1))) { // rematstr
986e461dc072 Initial revision
glantau
parents:
diff changeset
448 int end;
986e461dc072 Initial revision
glantau
parents:
diff changeset
449
986e461dc072 Initial revision
glantau
parents:
diff changeset
450 end = (state->cplinu) ? state->cplstrtmant : 253;
986e461dc072 Initial revision
glantau
parents:
diff changeset
451 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
452 do
986e461dc072 Initial revision
glantau
parents:
diff changeset
453 state->rematflg[i] = bitstream_get (1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
454 while (rematrix_band[i++] < end);
986e461dc072 Initial revision
glantau
parents:
diff changeset
455 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
456
986e461dc072 Initial revision
glantau
parents:
diff changeset
457 cplexpstr = EXP_REUSE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
458 lfeexpstr = EXP_REUSE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
459 if (state->cplinu)
986e461dc072 Initial revision
glantau
parents:
diff changeset
460 cplexpstr = bitstream_get (2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
461 for (i = 0; i < nfchans; i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
462 chexpstr[i] = bitstream_get (2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
463 if (state->lfeon)
986e461dc072 Initial revision
glantau
parents:
diff changeset
464 lfeexpstr = bitstream_get (1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
465
986e461dc072 Initial revision
glantau
parents:
diff changeset
466 for (i = 0; i < nfchans; i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
467 if (chexpstr[i] != EXP_REUSE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
468 if (state->cplinu && state->chincpl[i])
986e461dc072 Initial revision
glantau
parents:
diff changeset
469 state->endmant[i] = state->cplstrtmant;
986e461dc072 Initial revision
glantau
parents:
diff changeset
470 else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
471 int chbwcod;
986e461dc072 Initial revision
glantau
parents:
diff changeset
472
986e461dc072 Initial revision
glantau
parents:
diff changeset
473 chbwcod = bitstream_get (6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
474 if (chbwcod > 60)
986e461dc072 Initial revision
glantau
parents:
diff changeset
475 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
476 state->endmant[i] = chbwcod * 3 + 73;
986e461dc072 Initial revision
glantau
parents:
diff changeset
477 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
478 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
479
986e461dc072 Initial revision
glantau
parents:
diff changeset
480 do_bit_alloc = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
481
986e461dc072 Initial revision
glantau
parents:
diff changeset
482 if (cplexpstr != EXP_REUSE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
483 int cplabsexp, ncplgrps;
986e461dc072 Initial revision
glantau
parents:
diff changeset
484
986e461dc072 Initial revision
glantau
parents:
diff changeset
485 do_bit_alloc = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
486 ncplgrps = ((state->cplendmant - state->cplstrtmant) /
986e461dc072 Initial revision
glantau
parents:
diff changeset
487 (3 << (cplexpstr - 1)));
986e461dc072 Initial revision
glantau
parents:
diff changeset
488 cplabsexp = bitstream_get (4) << 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
489 if (parse_exponents (cplexpstr, ncplgrps, cplabsexp,
986e461dc072 Initial revision
glantau
parents:
diff changeset
490 state->cpl_exp + state->cplstrtmant))
986e461dc072 Initial revision
glantau
parents:
diff changeset
491 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
492 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
493 for (i = 0; i < nfchans; i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
494 if (chexpstr[i] != EXP_REUSE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
495 int grp_size, nchgrps;
986e461dc072 Initial revision
glantau
parents:
diff changeset
496
986e461dc072 Initial revision
glantau
parents:
diff changeset
497 do_bit_alloc = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
498 grp_size = 3 << (chexpstr[i] - 1);
986e461dc072 Initial revision
glantau
parents:
diff changeset
499 nchgrps = (state->endmant[i] + grp_size - 4) / grp_size;
986e461dc072 Initial revision
glantau
parents:
diff changeset
500 state->fbw_exp[i][0] = bitstream_get (4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
501 if (parse_exponents (chexpstr[i], nchgrps, state->fbw_exp[i][0],
986e461dc072 Initial revision
glantau
parents:
diff changeset
502 state->fbw_exp[i] + 1))
986e461dc072 Initial revision
glantau
parents:
diff changeset
503 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
504 bitstream_get (2); // gainrng
986e461dc072 Initial revision
glantau
parents:
diff changeset
505 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
506 if (lfeexpstr != EXP_REUSE) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
507 do_bit_alloc = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
508 state->lfe_exp[0] = bitstream_get (4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
509 if (parse_exponents (lfeexpstr, 2, state->lfe_exp[0],
986e461dc072 Initial revision
glantau
parents:
diff changeset
510 state->lfe_exp + 1))
986e461dc072 Initial revision
glantau
parents:
diff changeset
511 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
512 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
513
986e461dc072 Initial revision
glantau
parents:
diff changeset
514 if (bitstream_get (1)) { // baie
986e461dc072 Initial revision
glantau
parents:
diff changeset
515 do_bit_alloc = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
516 state->sdcycod = bitstream_get (2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
517 state->fdcycod = bitstream_get (2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
518 state->sgaincod = bitstream_get (2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
519 state->dbpbcod = bitstream_get (2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
520 state->floorcod = bitstream_get (3);
986e461dc072 Initial revision
glantau
parents:
diff changeset
521 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
522 if (bitstream_get (1)) { //snroffste
986e461dc072 Initial revision
glantau
parents:
diff changeset
523 do_bit_alloc = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
524 state->csnroffst = bitstream_get (6);
986e461dc072 Initial revision
glantau
parents:
diff changeset
525 if (state->cplinu) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
526 state->cplba.fsnroffst = bitstream_get (4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
527 state->cplba.fgaincod = bitstream_get (3);
986e461dc072 Initial revision
glantau
parents:
diff changeset
528 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
529 for (i = 0; i < nfchans; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
530 state->ba[i].fsnroffst = bitstream_get (4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
531 state->ba[i].fgaincod = bitstream_get (3);
986e461dc072 Initial revision
glantau
parents:
diff changeset
532 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
533 if (state->lfeon) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
534 state->lfeba.fsnroffst = bitstream_get (4);
986e461dc072 Initial revision
glantau
parents:
diff changeset
535 state->lfeba.fgaincod = bitstream_get (3);
986e461dc072 Initial revision
glantau
parents:
diff changeset
536 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
537 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
538 if ((state->cplinu) && (bitstream_get (1))) { // cplleake
986e461dc072 Initial revision
glantau
parents:
diff changeset
539 do_bit_alloc = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
540 state->cplfleak = 2304 - (bitstream_get (3) << 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
541 state->cplsleak = 2304 - (bitstream_get (3) << 8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
542 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
543
986e461dc072 Initial revision
glantau
parents:
diff changeset
544 if (bitstream_get (1)) { // deltbaie
986e461dc072 Initial revision
glantau
parents:
diff changeset
545 do_bit_alloc = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
546 if (state->cplinu)
986e461dc072 Initial revision
glantau
parents:
diff changeset
547 state->cplba.deltbae = bitstream_get (2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
548 for (i = 0; i < nfchans; i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
549 state->ba[i].deltbae = bitstream_get (2);
986e461dc072 Initial revision
glantau
parents:
diff changeset
550 if (state->cplinu && (state->cplba.deltbae == DELTA_BIT_NEW) &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
551 parse_deltba (state->cplba.deltba))
986e461dc072 Initial revision
glantau
parents:
diff changeset
552 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
553 for (i = 0; i < nfchans; i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
554 if ((state->ba[i].deltbae == DELTA_BIT_NEW) &&
986e461dc072 Initial revision
glantau
parents:
diff changeset
555 parse_deltba (state->ba[i].deltba))
986e461dc072 Initial revision
glantau
parents:
diff changeset
556 return 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
557 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
558
986e461dc072 Initial revision
glantau
parents:
diff changeset
559 if (do_bit_alloc) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
560 if (zero_snr_offsets (nfchans, state)) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
561 memset (state->cpl_bap, 0, sizeof (state->cpl_bap));
986e461dc072 Initial revision
glantau
parents:
diff changeset
562 memset (state->fbw_bap, 0, sizeof (state->fbw_bap));
986e461dc072 Initial revision
glantau
parents:
diff changeset
563 memset (state->lfe_bap, 0, sizeof (state->lfe_bap));
986e461dc072 Initial revision
glantau
parents:
diff changeset
564 } else {
986e461dc072 Initial revision
glantau
parents:
diff changeset
565 if (state->cplinu)
986e461dc072 Initial revision
glantau
parents:
diff changeset
566 bit_allocate (state, &state->cplba, state->cplstrtbnd,
986e461dc072 Initial revision
glantau
parents:
diff changeset
567 state->cplstrtmant, state->cplendmant,
986e461dc072 Initial revision
glantau
parents:
diff changeset
568 state->cplfleak, state->cplsleak,
986e461dc072 Initial revision
glantau
parents:
diff changeset
569 state->cpl_exp, state->cpl_bap);
986e461dc072 Initial revision
glantau
parents:
diff changeset
570 for (i = 0; i < nfchans; i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
571 bit_allocate (state, state->ba + i, 0, 0, state->endmant[i],
986e461dc072 Initial revision
glantau
parents:
diff changeset
572 0, 0, state->fbw_exp[i], state->fbw_bap[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
573 if (state->lfeon) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
574 state->lfeba.deltbae = DELTA_BIT_NONE;
986e461dc072 Initial revision
glantau
parents:
diff changeset
575 bit_allocate (state, &state->lfeba, 0, 0, 7, 0, 0,
986e461dc072 Initial revision
glantau
parents:
diff changeset
576 state->lfe_exp, state->lfe_bap);
986e461dc072 Initial revision
glantau
parents:
diff changeset
577 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
578 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
579 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
580
986e461dc072 Initial revision
glantau
parents:
diff changeset
581 if (bitstream_get (1)) { // skiple
986e461dc072 Initial revision
glantau
parents:
diff changeset
582 i = bitstream_get (9); // skipl
986e461dc072 Initial revision
glantau
parents:
diff changeset
583 while (i--)
986e461dc072 Initial revision
glantau
parents:
diff changeset
584 bitstream_get (8);
986e461dc072 Initial revision
glantau
parents:
diff changeset
585 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
586
986e461dc072 Initial revision
glantau
parents:
diff changeset
587 q_1_pointer = q_2_pointer = q_4_pointer = -1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
588 done_cpl = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
589
986e461dc072 Initial revision
glantau
parents:
diff changeset
590 for (i = 0; i < nfchans; i++) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
591 int j;
986e461dc072 Initial revision
glantau
parents:
diff changeset
592
986e461dc072 Initial revision
glantau
parents:
diff changeset
593 coeff_get (samples[i], state->fbw_exp[i], state->fbw_bap[i],
986e461dc072 Initial revision
glantau
parents:
diff changeset
594 dithflag[i], state->endmant[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
595
986e461dc072 Initial revision
glantau
parents:
diff changeset
596 if (state->cplinu && state->chincpl[i]) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
597 if (!done_cpl) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
598 int i, i_end, bnd, sub_bnd, ch;
986e461dc072 Initial revision
glantau
parents:
diff changeset
599 float cplcoeff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
600
986e461dc072 Initial revision
glantau
parents:
diff changeset
601 done_cpl = 1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
602
986e461dc072 Initial revision
glantau
parents:
diff changeset
603 #define bap state->cpl_bap
986e461dc072 Initial revision
glantau
parents:
diff changeset
604 #define exp state->cpl_exp
986e461dc072 Initial revision
glantau
parents:
diff changeset
605
986e461dc072 Initial revision
glantau
parents:
diff changeset
606 sub_bnd = bnd = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
607 i = state->cplstrtmant;
986e461dc072 Initial revision
glantau
parents:
diff changeset
608 while (i < state->cplendmant) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
609 i_end = i + 12;
986e461dc072 Initial revision
glantau
parents:
diff changeset
610 while (state->cplbndstrc[sub_bnd++])
986e461dc072 Initial revision
glantau
parents:
diff changeset
611 i_end += 12;
986e461dc072 Initial revision
glantau
parents:
diff changeset
612
986e461dc072 Initial revision
glantau
parents:
diff changeset
613 while (i < i_end) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
614 GET_COEFF (COUPLING_COEFF, COUPLING_DITHER);
986e461dc072 Initial revision
glantau
parents:
diff changeset
615 for (ch = 0; ch < nfchans; ch++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
616 if (state->chincpl[ch])
986e461dc072 Initial revision
glantau
parents:
diff changeset
617 samples[ch][i] =
986e461dc072 Initial revision
glantau
parents:
diff changeset
618 state->cplco[ch][bnd] * cplcoeff;
986e461dc072 Initial revision
glantau
parents:
diff changeset
619 i++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
620 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
621 bnd++;
986e461dc072 Initial revision
glantau
parents:
diff changeset
622 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
623
986e461dc072 Initial revision
glantau
parents:
diff changeset
624 #undef bap
986e461dc072 Initial revision
glantau
parents:
diff changeset
625 #undef exp
986e461dc072 Initial revision
glantau
parents:
diff changeset
626 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
627 j = state->cplendmant;
986e461dc072 Initial revision
glantau
parents:
diff changeset
628 } else
986e461dc072 Initial revision
glantau
parents:
diff changeset
629 j = state->endmant[i];
986e461dc072 Initial revision
glantau
parents:
diff changeset
630 for (; j < 256; j++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
631 samples[i][j] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
632 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
633
986e461dc072 Initial revision
glantau
parents:
diff changeset
634 if (state->acmod == 2) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
635 int j, end, band;
986e461dc072 Initial revision
glantau
parents:
diff changeset
636
986e461dc072 Initial revision
glantau
parents:
diff changeset
637 end = ((state->endmant[0] < state->endmant[1]) ?
986e461dc072 Initial revision
glantau
parents:
diff changeset
638 state->endmant[0] : state->endmant[1]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
639
986e461dc072 Initial revision
glantau
parents:
diff changeset
640 i = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
641 j = 13;
986e461dc072 Initial revision
glantau
parents:
diff changeset
642 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
643 if (!state->rematflg[i]) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
644 j = rematrix_band[i++];
986e461dc072 Initial revision
glantau
parents:
diff changeset
645 continue;
986e461dc072 Initial revision
glantau
parents:
diff changeset
646 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
647 band = rematrix_band[i++];
986e461dc072 Initial revision
glantau
parents:
diff changeset
648 if (band > end)
986e461dc072 Initial revision
glantau
parents:
diff changeset
649 band = end;
986e461dc072 Initial revision
glantau
parents:
diff changeset
650 do {
986e461dc072 Initial revision
glantau
parents:
diff changeset
651 float tmp0, tmp1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
652
986e461dc072 Initial revision
glantau
parents:
diff changeset
653 tmp0 = samples[0][j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
654 tmp1 = samples[1][j];
986e461dc072 Initial revision
glantau
parents:
diff changeset
655 samples[0][j] = tmp0 + tmp1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
656 samples[1][j] = tmp0 - tmp1;
986e461dc072 Initial revision
glantau
parents:
diff changeset
657 } while (++j < band);
986e461dc072 Initial revision
glantau
parents:
diff changeset
658 } while (j < end);
986e461dc072 Initial revision
glantau
parents:
diff changeset
659 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
660
986e461dc072 Initial revision
glantau
parents:
diff changeset
661 if (state->lfeon) {
986e461dc072 Initial revision
glantau
parents:
diff changeset
662 coeff_get (samples[5], state->lfe_exp, state->lfe_bap, 0, 7);
986e461dc072 Initial revision
glantau
parents:
diff changeset
663 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
664 for (i = 7; i < 256; i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
665 samples[5][i] = 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
666 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
667 }
986e461dc072 Initial revision
glantau
parents:
diff changeset
668
986e461dc072 Initial revision
glantau
parents:
diff changeset
669 for (i = 0; i < nfchans; i++)
986e461dc072 Initial revision
glantau
parents:
diff changeset
670 if (blksw[i])
986e461dc072 Initial revision
glantau
parents:
diff changeset
671 imdct_256 (samples[i], delay[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
672 else
986e461dc072 Initial revision
glantau
parents:
diff changeset
673 imdct_512 (samples[i], delay[i]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
674
986e461dc072 Initial revision
glantau
parents:
diff changeset
675 #if 0
986e461dc072 Initial revision
glantau
parents:
diff changeset
676 if (state->lfeon)
986e461dc072 Initial revision
glantau
parents:
diff changeset
677 imdct_512 (samples[5], delay[5]);
986e461dc072 Initial revision
glantau
parents:
diff changeset
678 #endif
986e461dc072 Initial revision
glantau
parents:
diff changeset
679
986e461dc072 Initial revision
glantau
parents:
diff changeset
680 downmix (*samples, state->acmod, state->output, state->level, state->bias,
986e461dc072 Initial revision
glantau
parents:
diff changeset
681 state->clev, state->slev);
986e461dc072 Initial revision
glantau
parents:
diff changeset
682
986e461dc072 Initial revision
glantau
parents:
diff changeset
683 return 0;
986e461dc072 Initial revision
glantau
parents:
diff changeset
684 }