0
|
1 /*
|
|
2 * bit_allocate.c
|
|
3 *
|
|
4 * Copyright (C) Aaron Holtzman - May 1999
|
|
5 *
|
|
6 * This file is part of ac3dec, a free Dolby AC-3 stream decoder.
|
|
7 *
|
|
8 * ac3dec is free software; you can redistribute it and/or modify
|
|
9 * it under the terms of the GNU General Public License as published by
|
|
10 * the Free Software Foundation; either version 2, or (at your option)
|
|
11 * any later version.
|
|
12 *
|
|
13 * ac3dec is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with GNU Make; see the file COPYING. If not, write to
|
|
20 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
21 *
|
|
22 */
|
|
23
|
|
24 #include <inttypes.h>
|
|
25 #include <stdlib.h>
|
|
26 #include <string.h>
|
|
27 #include "ac3.h"
|
|
28 #include "ac3_internal.h"
|
|
29
|
|
30 static int hthtab[3][50] = {
|
|
31 {0x730, 0x730, 0x7c0, 0x800, 0x820, 0x840, 0x850, 0x850, 0x860, 0x860,
|
|
32 0x860, 0x860, 0x860, 0x870, 0x870, 0x870, 0x880, 0x880, 0x890, 0x890,
|
|
33 0x8a0, 0x8a0, 0x8b0, 0x8b0, 0x8c0, 0x8c0, 0x8d0, 0x8e0, 0x8f0, 0x900,
|
|
34 0x910, 0x910, 0x910, 0x910, 0x900, 0x8f0, 0x8c0, 0x870, 0x820, 0x7e0,
|
|
35 0x7a0, 0x770, 0x760, 0x7a0, 0x7c0, 0x7c0, 0x6e0, 0x400, 0x3c0, 0x3c0},
|
|
36 {0x710, 0x710, 0x7a0, 0x7f0, 0x820, 0x830, 0x840, 0x850, 0x850, 0x860,
|
|
37 0x860, 0x860, 0x860, 0x860, 0x870, 0x870, 0x870, 0x880, 0x880, 0x880,
|
|
38 0x890, 0x890, 0x8a0, 0x8a0, 0x8b0, 0x8b0, 0x8c0, 0x8c0, 0x8e0, 0x8f0,
|
|
39 0x900, 0x910, 0x910, 0x910, 0x910, 0x900, 0x8e0, 0x8b0, 0x870, 0x820,
|
|
40 0x7e0, 0x7b0, 0x760, 0x770, 0x7a0, 0x7c0, 0x780, 0x5d0, 0x3c0, 0x3c0},
|
|
41 {0x680, 0x680, 0x750, 0x7b0, 0x7e0, 0x810, 0x820, 0x830, 0x840, 0x850,
|
|
42 0x850, 0x850, 0x860, 0x860, 0x860, 0x860, 0x860, 0x860, 0x860, 0x860,
|
|
43 0x870, 0x870, 0x870, 0x870, 0x880, 0x880, 0x880, 0x890, 0x8a0, 0x8b0,
|
|
44 0x8c0, 0x8d0, 0x8e0, 0x8f0, 0x900, 0x910, 0x910, 0x910, 0x900, 0x8f0,
|
|
45 0x8d0, 0x8b0, 0x840, 0x7f0, 0x790, 0x760, 0x7a0, 0x7c0, 0x7b0, 0x720}
|
|
46 };
|
|
47
|
|
48 static int8_t baptab[305] = {
|
|
49 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
|
|
50 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
|
|
51 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,
|
|
54 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, // 93 padding entries
|
|
55
|
|
56 16, 16, 16, 16, 16, 16, 16, 16, 16, 14, 14, 14, 14, 14, 14, 14,
|
|
57 14, 12, 12, 12, 12, 11, 11, 11, 11, 10, 10, 10, 10, 9, 9, 9,
|
|
58 9, 8, 8, 8, 8, 7, 7, 7, 7, 6, 6, 6, 6, 5, 5, 5,
|
|
59 5, 4, 4, -3, -3, 3, 3, 3, -2, -2, -1, -1, -1, -1, -1, 0,
|
|
60
|
|
61 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
62 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
63 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
64 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
65 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
66 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
67 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,
|
|
70 0, 0, 0, 0 // 148 padding entries
|
|
71 };
|
|
72
|
|
73 static int bndtab[30] = {21, 22, 23, 24, 25, 26, 27, 28, 31, 34,
|
|
74 37, 40, 43, 46, 49, 55, 61, 67, 73, 79,
|
|
75 85, 97, 109, 121, 133, 157, 181, 205, 229, 253};
|
|
76
|
|
77 static int8_t latab[256] = {
|
|
78 -64, -63, -62, -61, -60, -59, -58, -57, -56, -55, -54, -53,
|
|
79 -52, -52, -51, -50, -49, -48, -47, -47, -46, -45, -44, -44,
|
|
80 -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -36, -35,
|
|
81 -35, -34, -33, -33, -32, -32, -31, -30, -30, -29, -29, -28,
|
|
82 -28, -27, -27, -26, -26, -25, -25, -24, -24, -23, -23, -22,
|
|
83 -22, -21, -21, -21, -20, -20, -19, -19, -19, -18, -18, -18,
|
|
84 -17, -17, -17, -16, -16, -16, -15, -15, -15, -14, -14, -14,
|
|
85 -13, -13, -13, -13, -12, -12, -12, -12, -11, -11, -11, -11,
|
|
86 -10, -10, -10, -10, -10, -9, -9, -9, -9, -9, -8, -8,
|
|
87 -8, -8, -8, -8, -7, -7, -7, -7, -7, -7, -6, -6,
|
|
88 -6, -6, -6, -6, -6, -6, -5, -5, -5, -5, -5, -5,
|
|
89 -5, -5, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4,
|
|
90 -4, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3,
|
|
91 -3, -3, -3, -2, -2, -2, -2, -2, -2, -2, -2, -2,
|
|
92 -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1,
|
|
93 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
94 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
|
95 -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0,
|
|
96 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
97 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
98 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
99 0, 0, 0, 0
|
|
100 };
|
|
101
|
|
102 #define UPDATE_LEAK() \
|
|
103 do { \
|
|
104 fastleak += fdecay; \
|
|
105 if (fastleak > psd + fgain) \
|
|
106 fastleak = psd + fgain; \
|
|
107 slowleak += sdecay; \
|
|
108 if (slowleak > psd + sgain) \
|
|
109 slowleak = psd + sgain; \
|
|
110 } while (0)
|
|
111
|
|
112 #define COMPUTE_MASK() \
|
|
113 do { \
|
|
114 if (psd > dbknee) \
|
|
115 mask -= (psd - dbknee) >> 2; \
|
|
116 if (mask > hth [i >> halfrate]) \
|
|
117 mask = hth [i >> halfrate]; \
|
|
118 mask -= snroffset + 128 * deltba[i]; \
|
|
119 mask = (mask > 0) ? 0 : ((-mask) >> 5); \
|
|
120 mask -= floor; \
|
|
121 } while (0)
|
|
122
|
|
123 void bit_allocate (ac3_state_t * state, ac3_ba_t * ba, int bndstart,
|
|
124 int start, int end, int fastleak, int slowleak,
|
|
125 uint8_t * exp, int8_t * bap)
|
|
126 {
|
|
127 static int slowgain[4] = {0x540, 0x4d8, 0x478, 0x410};
|
|
128 static int dbpbtab[4] = {0xc00, 0x500, 0x300, 0x100};
|
|
129 static int floortab[8] = {0x910, 0x950, 0x990, 0x9d0,
|
|
130 0xa10, 0xa90, 0xb10, 0x1400};
|
|
131
|
|
132 int i, j;
|
|
133 int fdecay, fgain, sdecay, sgain, dbknee, floor, snroffset;
|
|
134 int psd, mask;
|
|
135 int8_t * deltba;
|
|
136 int * hth;
|
|
137 int halfrate;
|
|
138
|
|
139 halfrate = state->halfrate;
|
|
140 fdecay = (63 + 20 * state->fdcycod) >> halfrate;
|
|
141 fgain = 128 + 128 * ba->fgaincod;
|
|
142 sdecay = (15 + 2 * state->sdcycod) >> halfrate;
|
|
143 sgain = slowgain[state->sgaincod];
|
|
144 dbknee = dbpbtab[state->dbpbcod];
|
|
145 hth = hthtab[state->fscod];
|
|
146 /*
|
|
147 * if there is no delta bit allocation, make deltba point to an area
|
|
148 * known to contain zeroes. baptab+156 here.
|
|
149 */
|
|
150 deltba = (ba->deltbae == DELTA_BIT_NONE) ? baptab + 156 : ba->deltba;
|
|
151 floor = floortab[state->floorcod];
|
|
152 snroffset = 960 - 64 * state->csnroffst - 4 * ba->fsnroffst + floor;
|
|
153 floor >>= 5;
|
|
154
|
|
155 i = bndstart;
|
|
156 j = start;
|
|
157 if (start == 0) { // not the coupling channel
|
|
158 int lowcomp;
|
|
159
|
|
160 lowcomp = 0;
|
|
161 j = end - 1;
|
|
162 do {
|
|
163 if (i < j) {
|
|
164 if (exp[i+1] == exp[i] - 2)
|
|
165 lowcomp = 384;
|
|
166 else if (lowcomp && (exp[i+1] > exp[i]))
|
|
167 lowcomp -= 64;
|
|
168 }
|
|
169 psd = 128 * exp[i];
|
|
170 mask = psd + fgain + lowcomp;
|
|
171 COMPUTE_MASK ();
|
|
172 bap[i++] = (baptab+156)[mask + 4 * exp[i]];
|
|
173 } while ((i < 3) || ((i < 7) && (exp[i] > exp[i-1])));
|
|
174 fastleak = psd + fgain;
|
|
175 slowleak = psd + sgain;
|
|
176
|
|
177 while (i < 7) {
|
|
178 if (i < j) {
|
|
179 if (exp[i+1] == exp[i] - 2)
|
|
180 lowcomp = 384;
|
|
181 else if (lowcomp && (exp[i+1] > exp[i]))
|
|
182 lowcomp -= 64;
|
|
183 }
|
|
184 psd = 128 * exp[i];
|
|
185 UPDATE_LEAK ();
|
|
186 mask = ((fastleak + lowcomp < slowleak) ?
|
|
187 fastleak + lowcomp : slowleak);
|
|
188 COMPUTE_MASK ();
|
|
189 bap[i++] = (baptab+156)[mask + 4 * exp[i]];
|
|
190 }
|
|
191
|
|
192 if (end == 7) // lfe channel
|
|
193 return;
|
|
194
|
|
195 do {
|
|
196 if (exp[i+1] == exp[i] - 2)
|
|
197 lowcomp = 320;
|
|
198 else if (lowcomp && (exp[i+1] > exp[i]))
|
|
199 lowcomp -= 64;
|
|
200 psd = 128 * exp[i];
|
|
201 UPDATE_LEAK ();
|
|
202 mask = ((fastleak + lowcomp < slowleak) ?
|
|
203 fastleak + lowcomp : slowleak);
|
|
204 COMPUTE_MASK ();
|
|
205 bap[i++] = (baptab+156)[mask + 4 * exp[i]];
|
|
206 } while (i < 20);
|
|
207
|
|
208 while (lowcomp > 128) { // two iterations maximum
|
|
209 lowcomp -= 128;
|
|
210 psd = 128 * exp[i];
|
|
211 UPDATE_LEAK ();
|
|
212 mask = ((fastleak + lowcomp < slowleak) ?
|
|
213 fastleak + lowcomp : slowleak);
|
|
214 COMPUTE_MASK ();
|
|
215 bap[i++] = (baptab+156)[mask + 4 * exp[i]];
|
|
216 }
|
|
217 j = i;
|
|
218 }
|
|
219
|
|
220 do {
|
|
221 int startband, endband;
|
|
222
|
|
223 startband = j;
|
|
224 endband = ((bndtab-20)[i] < end) ? (bndtab-20)[i] : end;
|
|
225 psd = 128 * exp[j++];
|
|
226 while (j < endband) {
|
|
227 int next, delta;
|
|
228
|
|
229 next = 128 * exp[j++];
|
|
230 delta = next - psd;
|
|
231 switch (delta >> 9) {
|
|
232 case -6: case -5: case -4: case -3: case -2:
|
|
233 psd = next;
|
|
234 break;
|
|
235 case -1:
|
|
236 psd = next + latab[(-delta) >> 1];
|
|
237 break;
|
|
238 case 0:
|
|
239 psd += latab[delta >> 1];
|
|
240 break;
|
|
241 }
|
|
242 }
|
|
243 // minpsd = -289
|
|
244 UPDATE_LEAK ();
|
|
245 mask = (fastleak < slowleak) ? fastleak : slowleak;
|
|
246 COMPUTE_MASK ();
|
|
247 i++;
|
|
248 j = startband;
|
|
249 do {
|
|
250 // max(mask+4*exp)=147=-(minpsd+fgain-deltba-snroffset)>>5+4*exp
|
|
251 // min(mask+4*exp)=-156=-(sgain-deltba-snroffset)>>5
|
|
252 bap[j++] = (baptab+156)[mask + 4 * exp[j]];
|
|
253 } while (j < endband);
|
|
254 } while (j < end);
|
|
255 }
|