1 /*
2  * bit_allocate.c
3  * Copyright (C) 2000-2002 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  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23 
24 #include "config.h"
25 
26 #include <inttypes.h>
27 
28 #include "a52.h"
29 #include "a52_internal.h"
30 
31 static const uint16_t hthtab[3][50] = {
32     {0x730, 0x730, 0x7c0, 0x800, 0x820, 0x840, 0x850, 0x850, 0x860, 0x860,
33      0x860, 0x860, 0x860, 0x870, 0x870, 0x870, 0x880, 0x880, 0x890, 0x890,
34      0x8a0, 0x8a0, 0x8b0, 0x8b0, 0x8c0, 0x8c0, 0x8d0, 0x8e0, 0x8f0, 0x900,
35      0x910, 0x910, 0x910, 0x910, 0x900, 0x8f0, 0x8c0, 0x870, 0x820, 0x7e0,
36      0x7a0, 0x770, 0x760, 0x7a0, 0x7c0, 0x7c0, 0x6e0, 0x400, 0x3c0, 0x3c0},
37     {0x710, 0x710, 0x7a0, 0x7f0, 0x820, 0x830, 0x840, 0x850, 0x850, 0x860,
38      0x860, 0x860, 0x860, 0x860, 0x870, 0x870, 0x870, 0x880, 0x880, 0x880,
39      0x890, 0x890, 0x8a0, 0x8a0, 0x8b0, 0x8b0, 0x8c0, 0x8c0, 0x8e0, 0x8f0,
40      0x900, 0x910, 0x910, 0x910, 0x910, 0x900, 0x8e0, 0x8b0, 0x870, 0x820,
41      0x7e0, 0x7b0, 0x760, 0x770, 0x7a0, 0x7c0, 0x780, 0x5d0, 0x3c0, 0x3c0},
42     {0x680, 0x680, 0x750, 0x7b0, 0x7e0, 0x810, 0x820, 0x830, 0x840, 0x850,
43      0x850, 0x850, 0x860, 0x860, 0x860, 0x860, 0x860, 0x860, 0x860, 0x860,
44      0x870, 0x870, 0x870, 0x870, 0x880, 0x880, 0x880, 0x890, 0x8a0, 0x8b0,
45      0x8c0, 0x8d0, 0x8e0, 0x8f0, 0x900, 0x910, 0x910, 0x910, 0x900, 0x8f0,
46      0x8d0, 0x8b0, 0x840, 0x7f0, 0x790, 0x760, 0x7a0, 0x7c0, 0x7b0, 0x720}
47 };
48 
49 static const int8_t baptab[305] = {
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, 16, 16, 16,
55     16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,	/* 93 padding elems */
56 
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,
59      9,  8,  8,  8,  8,  7,  7,  7,  7,  6,  6,  6,  6,  5,  5,  5,
60      5,  4,  4, -3, -3,  3,  3,  3, -2, -2, -1, -1, -1, -1, -1,  0,
61 
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,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
71      0,  0,  0,  0					/* 148 padding elems */
72 };
73 
74 static const uint8_t bndtab[30] = {
75     21, 22,  23,  24,  25,  26,  27,  28,  31,  34,
76     37, 40,  43,  46,  49,  55,  61,  67,  73,  79,
77     85, 97, 109, 121, 133, 157, 181, 205, 229, 253
78 };
79 
80 static const int8_t latab[256] = {
81     -64, -63, -62, -61, -60, -59, -58, -57, -56, -55, -54, -53,
82     -52, -52, -51, -50, -49, -48, -47, -47, -46, -45, -44, -44,
83     -43, -42, -41, -41, -40, -39, -38, -38, -37, -36, -36, -35,
84     -35, -34, -33, -33, -32, -32, -31, -30, -30, -29, -29, -28,
85     -28, -27, -27, -26, -26, -25, -25, -24, -24, -23, -23, -22,
86     -22, -21, -21, -21, -20, -20, -19, -19, -19, -18, -18, -18,
87     -17, -17, -17, -16, -16, -16, -15, -15, -15, -14, -14, -14,
88     -13, -13, -13, -13, -12, -12, -12, -12, -11, -11, -11, -11,
89     -10, -10, -10, -10, -10,  -9,  -9,  -9,  -9,  -9,  -8,  -8,
90      -8,  -8,  -8,  -8,  -7,  -7,  -7,  -7,  -7,  -7,  -6,  -6,
91      -6,  -6,  -6,  -6,  -6,  -6,  -5,  -5,  -5,  -5,  -5,  -5,
92      -5,  -5,  -4,  -4,  -4,  -4,  -4,  -4,  -4,  -4,  -4,  -4,
93      -4,  -3,  -3,  -3,  -3,  -3,  -3,  -3,  -3,  -3,  -3,  -3,
94      -3,  -3,  -3,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,
95      -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -2,  -1,  -1,
96      -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
97      -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,
98      -1,  -1,  -1,  -1,  -1,  -1,   0,   0,   0,   0,   0,   0,
99       0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
100       0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
101       0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,
102       0,   0,   0,   0
103 };
104 
105 #define UPDATE_LEAK() 		\
106 do {				\
107     fastleak += fdecay;		\
108     if (fastleak > psd + fgain)	\
109 	fastleak = psd + fgain;	\
110     slowleak += sdecay;		\
111     if (slowleak > psd + sgain)	\
112 	slowleak = psd + sgain;	\
113 } while (0)
114 
115 #define COMPUTE_MASK()				\
116 do {						\
117     if (psd > dbknee)				\
118 	mask -= (psd - dbknee) >> 2;		\
119     if (mask > hth [i >> halfrate])		\
120 	mask = hth [i >> halfrate];		\
121     mask -= snroffset + 128 * deltba[i];	\
122     mask = (mask > 0) ? 0 : ((-mask) >> 5);	\
123     mask -= floor;				\
124 } while (0)
125 
a52_bit_allocate(a52_state_t * state,ba_t * ba,int bndstart,int start,int end,int fastleak,int slowleak,expbap_t * expbap)126 void a52_bit_allocate (a52_state_t * state, ba_t * ba, int bndstart,
127 		       int start, int end, int fastleak, int slowleak,
128 		       expbap_t * expbap)
129 {
130     static const uint16_t slowgain[4] = {0x540, 0x4d8, 0x478, 0x410};
131     static const uint16_t dbpbtab[4]  = {0xc00, 0x500, 0x300, 0x100};
132     static const uint16_t floortab[8] = {0x910, 0x950, 0x990, 0x9d0,
133 			      0xa10, 0xa90, 0xb10, 0x1400};
134 
135     int i, j;
136     uint8_t * exp;
137     int8_t * bap;
138     int fdecay, fgain, sdecay, sgain, dbknee, floor, snroffset;
139     int psd, mask;
140     const int8_t * deltba;
141     const uint16_t * hth;
142     int halfrate;
143 
144     halfrate = state->halfrate;
145     fdecay = (63 + 20 * ((state->bai >> 7) & 3)) >> halfrate;	/* fdcycod */
146     fgain = 128 + 128 * (ba->bai & 7);				/* fgaincod */
147     sdecay = (15 + 2 * (state->bai >> 9)) >> halfrate;		/* sdcycod */
148     sgain = slowgain[(state->bai >> 5) & 3];			/* sgaincod */
149     dbknee = dbpbtab[(state->bai >> 3) & 3];			/* dbpbcod */
150     hth = hthtab[state->fscod];
151     /*
152      * if there is no delta bit allocation, make deltba point to an area
153      * known to contain zeroes. baptab+156 here.
154      */
155     deltba = (ba->deltbae == DELTA_BIT_NONE) ? baptab + 156 : ba->deltba;
156     floor = floortab[state->bai & 7];				/* floorcod */
157     snroffset = 960 - 64 * state->csnroffst - 4 * (ba->bai >> 3) + floor;
158     floor >>= 5;
159 
160     exp = expbap->exp;
161     bap = expbap->bap;
162 
163     i = bndstart;
164     j = start;
165     if (start == 0) {	/* not the coupling channel */
166 	int lowcomp;
167 
168 	lowcomp = 0;
169 	j = end - 1;
170 	do {
171 	    if (i < j) {
172 		if (exp[i+1] == exp[i] - 2)
173 		    lowcomp = 384;
174 		else if (lowcomp && (exp[i+1] > exp[i]))
175 		    lowcomp -= 64;
176 	    }
177 	    psd = 128 * exp[i];
178 	    mask = psd + fgain + lowcomp;
179 	    COMPUTE_MASK ();
180 	    bap[i] = (baptab+156)[mask + 4 * exp[i]];
181 	    i++;
182 	} while ((i < 3) || ((i < 7) && (exp[i] > exp[i-1])));
183 	fastleak = psd + fgain;
184 	slowleak = psd + sgain;
185 
186 	while (i < 7) {
187 	    if (i < j) {
188 		if (exp[i+1] == exp[i] - 2)
189 		    lowcomp = 384;
190 		else if (lowcomp && (exp[i+1] > exp[i]))
191 		    lowcomp -= 64;
192 	    }
193 	    psd = 128 * exp[i];
194 	    UPDATE_LEAK ();
195 	    mask = ((fastleak + lowcomp < slowleak) ?
196 		    fastleak + lowcomp : slowleak);
197 	    COMPUTE_MASK ();
198 	    bap[i] = (baptab+156)[mask + 4 * exp[i]];
199 	    i++;
200 	}
201 
202 	if (end == 7)	/* lfe channel */
203 	    return;
204 
205 	do {
206 	    if (exp[i+1] == exp[i] - 2)
207 		lowcomp = 320;
208 	    else if (lowcomp && (exp[i+1] > exp[i]))
209 		lowcomp -= 64;
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 	    i++;
217 	} while (i < 20);
218 
219 	while (lowcomp > 128) {		/* two iterations maximum */
220 	    lowcomp -= 128;
221 	    psd = 128 * exp[i];
222 	    UPDATE_LEAK ();
223 	    mask = ((fastleak + lowcomp < slowleak) ?
224 		    fastleak + lowcomp : slowleak);
225 	    COMPUTE_MASK ();
226 	    bap[i] = (baptab+156)[mask + 4 * exp[i]];
227 	    i++;
228 	}
229 	j = i;
230     }
231 
232     do {
233 	int startband, endband;
234 
235 	startband = j;
236 	endband = ((bndtab-20)[i] < end) ? (bndtab-20)[i] : end;
237 	psd = 128 * exp[j++];
238 	while (j < endband) {
239 	    int next, delta;
240 
241 	    next = 128 * exp[j++];
242 	    delta = next - psd;
243 	    switch (delta >> 9) {
244 	    case -6: case -5: case -4: case -3: case -2:
245 		psd = next;
246 		break;
247 	    case -1:
248 		psd = next + latab[(-delta) >> 1];
249 		break;
250 	    case 0:
251 		psd += latab[delta >> 1];
252 		break;
253 	    }
254 	}
255 	/* minpsd = -289 */
256 	UPDATE_LEAK ();
257 	mask = (fastleak < slowleak) ? fastleak : slowleak;
258 	COMPUTE_MASK ();
259 	i++;
260 	j = startband;
261 	do {
262 	    /* max(mask+4*exp)=147=-(minpsd+fgain-deltba-snroffset)>>5+4*exp */
263 	    /* min(mask+4*exp)=-156=-(sgain-deltba-snroffset)>>5 */
264 	    bap[j] = (baptab+156)[mask + 4 * exp[j]];
265 	} while (++j < endband);
266     } while (j < end);
267 }
268 
269