VirtualBox

source: vbox/trunk/src/libs/ffmpeg-20060710/libavcodec/liba52/bit_allocate.c@ 5776

Last change on this file since 5776 was 5776, checked in by vboxsync, 17 years ago

ffmpeg: exported to OSE

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette