summaryrefslogtreecommitdiff
path: root/apps/codecs/liba52/bit_allocate.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/codecs/liba52/bit_allocate.c')
-rw-r--r--apps/codecs/liba52/bit_allocate.c242
1 files changed, 121 insertions, 121 deletions
diff --git a/apps/codecs/liba52/bit_allocate.c b/apps/codecs/liba52/bit_allocate.c
index f72cd75f0a..3f68c92c0c 100644
--- a/apps/codecs/liba52/bit_allocate.c
+++ b/apps/codecs/liba52/bit_allocate.c
@@ -52,7 +52,7 @@ static int8_t baptab[305] IDATA_ATTR = {
52 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 52 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
53 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 53 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
54 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 54 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
55 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 93 padding elems */ 55 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, /* 93 padding elems */
56 56
57 16, 16, 16, 16, 16, 16, 16, 16, 16, 14, 14, 14, 14, 14, 14, 14, 57 16, 16, 16, 16, 16, 16, 16, 16, 16, 14, 14, 14, 14, 14, 14, 14,
58 14, 12, 12, 12, 12, 11, 11, 11, 11, 10, 10, 10, 10, 9, 9, 9, 58 14, 12, 12, 12, 12, 11, 11, 11, 11, 10, 10, 10, 10, 9, 9, 9,
@@ -68,12 +68,12 @@ static int8_t baptab[305] IDATA_ATTR = {
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 69 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 70 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
71 0, 0, 0, 0 /* 148 padding elems */ 71 0, 0, 0, 0 /* 148 padding elems */
72}; 72};
73 73
74static int bndtab[30] IDATA_ATTR = {21, 22, 23, 24, 25, 26, 27, 28, 31, 34, 74static int bndtab[30] IDATA_ATTR = {21, 22, 23, 24, 25, 26, 27, 28, 31, 34,
75 37, 40, 43, 46, 49, 55, 61, 67, 73, 79, 75 37, 40, 43, 46, 49, 55, 61, 67, 73, 79,
76 85, 97, 109, 121, 133, 157, 181, 205, 229, 253}; 76 85, 97, 109, 121, 133, 157, 181, 205, 229, 253};
77 77
78static int8_t latab[256] IDATA_ATTR = { 78static int8_t latab[256] IDATA_ATTR = {
79 -64, -63, -62, -61, -60, -59, -58, -57, -56, -55, -54, -53, 79 -64, -63, -62, -61, -60, -59, -58, -57, -56, -55, -54, -53,
@@ -100,35 +100,35 @@ static int8_t latab[256] IDATA_ATTR = {
100 0, 0, 0, 0 100 0, 0, 0, 0
101}; 101};
102 102
103#define UPDATE_LEAK() \ 103#define UPDATE_LEAK() \
104do { \ 104do { \
105 fastleak += fdecay; \ 105 fastleak += fdecay; \
106 if (fastleak > psd + fgain) \ 106 if (fastleak > psd + fgain) \
107 fastleak = psd + fgain; \ 107 fastleak = psd + fgain; \
108 slowleak += sdecay; \ 108 slowleak += sdecay; \
109 if (slowleak > psd + sgain) \ 109 if (slowleak > psd + sgain) \
110 slowleak = psd + sgain; \ 110 slowleak = psd + sgain; \
111} while (0) 111} while (0)
112 112
113#define COMPUTE_MASK() \ 113#define COMPUTE_MASK() \
114do { \ 114do { \
115 if (psd > dbknee) \ 115 if (psd > dbknee) \
116 mask -= (psd - dbknee) >> 2; \ 116 mask -= (psd - dbknee) >> 2; \
117 if (mask > hth [i >> halfrate]) \ 117 if (mask > hth [i >> halfrate]) \
118 mask = hth [i >> halfrate]; \ 118 mask = hth [i >> halfrate]; \
119 mask -= snroffset + 128 * deltba[i]; \ 119 mask -= snroffset + 128 * deltba[i]; \
120 mask = (mask > 0) ? 0 : ((-mask) >> 5); \ 120 mask = (mask > 0) ? 0 : ((-mask) >> 5); \
121 mask -= floor; \ 121 mask -= floor; \
122} while (0) 122} while (0)
123 123
124void a52_bit_allocate (a52_state_t * state, ba_t * ba, int bndstart, 124void a52_bit_allocate (a52_state_t * state, ba_t * ba, int bndstart,
125 int start, int end, int fastleak, int slowleak, 125 int start, int end, int fastleak, int slowleak,
126 expbap_t * expbap) 126 expbap_t * expbap)
127{ 127{
128 static int slowgain[4] = {0x540, 0x4d8, 0x478, 0x410}; 128 static int slowgain[4] = {0x540, 0x4d8, 0x478, 0x410};
129 static int dbpbtab[4] = {0xc00, 0x500, 0x300, 0x100}; 129 static int dbpbtab[4] = {0xc00, 0x500, 0x300, 0x100};
130 static int floortab[8] = {0x910, 0x950, 0x990, 0x9d0, 130 static int floortab[8] = {0x910, 0x950, 0x990, 0x9d0,
131 0xa10, 0xa90, 0xb10, 0x1400}; 131 0xa10, 0xa90, 0xb10, 0x1400};
132 132
133 int i, j; 133 int i, j;
134 uint8_t * exp; 134 uint8_t * exp;
@@ -140,18 +140,18 @@ void a52_bit_allocate (a52_state_t * state, ba_t * ba, int bndstart,
140 int halfrate; 140 int halfrate;
141 141
142 halfrate = state->halfrate; 142 halfrate = state->halfrate;
143 fdecay = (63 + 20 * ((state->bai >> 7) & 3)) >> halfrate; /* fdcycod */ 143 fdecay = (63 + 20 * ((state->bai >> 7) & 3)) >> halfrate; /* fdcycod */
144 fgain = 128 + 128 * (ba->bai & 7); /* fgaincod */ 144 fgain = 128 + 128 * (ba->bai & 7); /* fgaincod */
145 sdecay = (15 + 2 * (state->bai >> 9)) >> halfrate; /* sdcycod */ 145 sdecay = (15 + 2 * (state->bai >> 9)) >> halfrate; /* sdcycod */
146 sgain = slowgain[(state->bai >> 5) & 3]; /* sgaincod */ 146 sgain = slowgain[(state->bai >> 5) & 3]; /* sgaincod */
147 dbknee = dbpbtab[(state->bai >> 3) & 3]; /* dbpbcod */ 147 dbknee = dbpbtab[(state->bai >> 3) & 3]; /* dbpbcod */
148 hth = hthtab[state->fscod]; 148 hth = hthtab[state->fscod];
149 /* 149 /*
150 * if there is no delta bit allocation, make deltba point to an area 150 * if there is no delta bit allocation, make deltba point to an area
151 * known to contain zeroes. baptab+156 here. 151 * known to contain zeroes. baptab+156 here.
152 */ 152 */
153 deltba = (ba->deltbae == DELTA_BIT_NONE) ? baptab + 156 : ba->deltba; 153 deltba = (ba->deltbae == DELTA_BIT_NONE) ? baptab + 156 : ba->deltba;
154 floor = floortab[state->bai & 7]; /* floorcod */ 154 floor = floortab[state->bai & 7]; /* floorcod */
155 snroffset = 960 - 64 * state->csnroffst - 4 * (ba->bai >> 3) + floor; 155 snroffset = 960 - 64 * state->csnroffst - 4 * (ba->bai >> 3) + floor;
156 floor >>= 5; 156 floor >>= 5;
157 157
@@ -160,106 +160,106 @@ void a52_bit_allocate (a52_state_t * state, ba_t * ba, int bndstart,
160 160
161 i = bndstart; 161 i = bndstart;
162 j = start; 162 j = start;
163 if (start == 0) { /* not the coupling channel */ 163 if (start == 0) { /* not the coupling channel */
164 int lowcomp; 164 int lowcomp;
165 165
166 lowcomp = 0; 166 lowcomp = 0;
167 j = end - 1; 167 j = end - 1;
168 do { 168 do {
169 if (i < j) { 169 if (i < j) {
170 if (exp[i+1] == exp[i] - 2) 170 if (exp[i+1] == exp[i] - 2)
171 lowcomp = 384; 171 lowcomp = 384;
172 else if (lowcomp && (exp[i+1] > exp[i])) 172 else if (lowcomp && (exp[i+1] > exp[i]))
173 lowcomp -= 64; 173 lowcomp -= 64;
174 } 174 }
175 psd = 128 * exp[i]; 175 psd = 128 * exp[i];
176 mask = psd + fgain + lowcomp; 176 mask = psd + fgain + lowcomp;
177 COMPUTE_MASK (); 177 COMPUTE_MASK ();
178 bap[i] = (baptab+156)[mask + 4 * exp[i]]; 178 bap[i] = (baptab+156)[mask + 4 * exp[i]];
179 i++; 179 i++;
180 } while ((i < 3) || ((i < 7) && (exp[i] > exp[i-1]))); 180 } while ((i < 3) || ((i < 7) && (exp[i] > exp[i-1])));
181 fastleak = psd + fgain; 181 fastleak = psd + fgain;
182 slowleak = psd + sgain; 182 slowleak = psd + sgain;
183 183
184 while (i < 7) { 184 while (i < 7) {
185 if (i < j) { 185 if (i < j) {
186 if (exp[i+1] == exp[i] - 2) 186 if (exp[i+1] == exp[i] - 2)
187 lowcomp = 384; 187 lowcomp = 384;
188 else if (lowcomp && (exp[i+1] > exp[i])) 188 else if (lowcomp && (exp[i+1] > exp[i]))
189 lowcomp -= 64; 189 lowcomp -= 64;
190 } 190 }
191 psd = 128 * exp[i]; 191 psd = 128 * exp[i];
192 UPDATE_LEAK (); 192 UPDATE_LEAK ();
193 mask = ((fastleak + lowcomp < slowleak) ? 193 mask = ((fastleak + lowcomp < slowleak) ?
194 fastleak + lowcomp : slowleak); 194 fastleak + lowcomp : slowleak);
195 COMPUTE_MASK (); 195 COMPUTE_MASK ();
196 bap[i] = (baptab+156)[mask + 4 * exp[i]]; 196 bap[i] = (baptab+156)[mask + 4 * exp[i]];
197 i++; 197 i++;
198 } 198 }
199 199
200 if (end == 7) /* lfe channel */ 200 if (end == 7) /* lfe channel */
201 return; 201 return;
202 202
203 do { 203 do {
204 if (exp[i+1] == exp[i] - 2) 204 if (exp[i+1] == exp[i] - 2)
205 lowcomp = 320; 205 lowcomp = 320;
206 else if (lowcomp && (exp[i+1] > exp[i])) 206 else if (lowcomp && (exp[i+1] > exp[i]))
207 lowcomp -= 64; 207 lowcomp -= 64;
208 psd = 128 * exp[i]; 208 psd = 128 * exp[i];
209 UPDATE_LEAK (); 209 UPDATE_LEAK ();
210 mask = ((fastleak + lowcomp < slowleak) ? 210 mask = ((fastleak + lowcomp < slowleak) ?
211 fastleak + lowcomp : slowleak); 211 fastleak + lowcomp : slowleak);
212 COMPUTE_MASK (); 212 COMPUTE_MASK ();
213 bap[i] = (baptab+156)[mask + 4 * exp[i]]; 213 bap[i] = (baptab+156)[mask + 4 * exp[i]];
214 i++; 214 i++;
215 } while (i < 20); 215 } while (i < 20);
216 216
217 while (lowcomp > 128) { /* two iterations maximum */ 217 while (lowcomp > 128) { /* two iterations maximum */
218 lowcomp -= 128; 218 lowcomp -= 128;
219 psd = 128 * exp[i]; 219 psd = 128 * exp[i];
220 UPDATE_LEAK (); 220 UPDATE_LEAK ();
221 mask = ((fastleak + lowcomp < slowleak) ? 221 mask = ((fastleak + lowcomp < slowleak) ?
222 fastleak + lowcomp : slowleak); 222 fastleak + lowcomp : slowleak);
223 COMPUTE_MASK (); 223 COMPUTE_MASK ();
224 bap[i] = (baptab+156)[mask + 4 * exp[i]]; 224 bap[i] = (baptab+156)[mask + 4 * exp[i]];
225 i++; 225 i++;
226 } 226 }
227 j = i; 227 j = i;
228 } 228 }
229 229
230 do { 230 do {
231 int startband, endband; 231 int startband, endband;
232 232
233 startband = j; 233 startband = j;
234 endband = (bndtab[i-20] < end) ? bndtab[i-20] : end; 234 endband = (bndtab[i-20] < end) ? bndtab[i-20] : end;
235 psd = 128 * exp[j++]; 235 psd = 128 * exp[j++];
236 while (j < endband) { 236 while (j < endband) {
237 int next, delta; 237 int next, delta;
238 238
239 next = 128 * exp[j++]; 239 next = 128 * exp[j++];
240 delta = next - psd; 240 delta = next - psd;
241 switch (delta >> 9) { 241 switch (delta >> 9) {
242 case -6: case -5: case -4: case -3: case -2: 242 case -6: case -5: case -4: case -3: case -2:
243 psd = next; 243 psd = next;
244 break; 244 break;
245 case -1: 245 case -1:
246 psd = next + latab[(-delta) >> 1]; 246 psd = next + latab[(-delta) >> 1];
247 break; 247 break;
248 case 0: 248 case 0:
249 psd += latab[delta >> 1]; 249 psd += latab[delta >> 1];
250 break; 250 break;
251 } 251 }
252 } 252 }
253 /* minpsd = -289 */ 253 /* minpsd = -289 */
254 UPDATE_LEAK (); 254 UPDATE_LEAK ();
255 mask = (fastleak < slowleak) ? fastleak : slowleak; 255 mask = (fastleak < slowleak) ? fastleak : slowleak;
256 COMPUTE_MASK (); 256 COMPUTE_MASK ();
257 i++; 257 i++;
258 j = startband; 258 j = startband;
259 do { 259 do {
260 /* max(mask+4*exp)=147=-(minpsd+fgain-deltba-snroffset)>>5+4*exp */ 260 /* max(mask+4*exp)=147=-(minpsd+fgain-deltba-snroffset)>>5+4*exp */
261 /* min(mask+4*exp)=-156=-(sgain-deltba-snroffset)>>5 */ 261 /* min(mask+4*exp)=-156=-(sgain-deltba-snroffset)>>5 */
262 bap[j] = (baptab+156)[mask + 4 * exp[j]]; 262 bap[j] = (baptab+156)[mask + 4 * exp[j]];
263 } while (++j < endband); 263 } while (++j < endband);
264 } while (j < end); 264 } while (j < end);
265} 265}