]> git.sesse.net Git - vlc/blob - src/ac3_decoder/ac3_bit_allocate.c
* AC3 IMDCT and downmix functions are now in plugins, --imdct and
[vlc] / src / ac3_decoder / ac3_bit_allocate.c
1 /*****************************************************************************
2  * ac3_bit_allocate.c: ac3 allocation tables
3  *****************************************************************************
4  * Copyright (C) 2000 VideoLAN
5  * $Id: ac3_bit_allocate.c,v 1.22 2001/05/15 16:19:42 sam Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Aaron Holtzman <aholtzma@engr.uvic.ca>
9  *          Renaud Dartus <reno@videolan.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include "defs.h"
30
31 #include <string.h>                                              /* memcpy() */
32
33 #include "config.h"
34 #include "common.h"
35 #include "threads.h"
36 #include "mtime.h"
37
38 #include "stream_control.h"
39 #include "input_ext-dec.h"
40
41 #include "ac3_imdct.h"
42 #include "ac3_downmix.h"
43 #include "ac3_decoder.h"
44
45 #include "ac3_internal.h"                                 /* DELTA_BIT_REUSE */
46
47
48 static void ba_compute_psd (bit_allocate_t * p_bit, s16 start, s16 end, s16 exps[]);
49
50 static void ba_compute_excitation (bit_allocate_t * p_bit, s16 start, s16 end, s16 fgain,
51                                    s16 fastleak, s16 slowleak, s16 is_lfe);
52 static void ba_compute_mask (bit_allocate_t * p_bit, s16 start, s16 end, u16 fscod,
53                              u16 deltbae, u16 deltnseg, u16 deltoffst[],
54                              u16 deltba[], u16 deltlen[]);
55 static void ba_compute_bap (bit_allocate_t * p_bit, s16 start, s16 end,
56                             s16 snroffset, s16 bap[]);
57
58 /* Misc LUTs for bit allocation process */
59
60 static const s16 slowdec[]  = { 0x0f,  0x11,  0x13,  0x15  };
61 static const s16 fastdec[]  = { 0x3f,  0x53,  0x67,  0x7b  };
62 static const s16 slowgain[] = { 0x540, 0x4d8, 0x478, 0x410 };
63 static const s16 dbpbtab[]  = { 0x000, 0x700, 0x900, 0xb00 };
64
65 static const u16 floortab[] = { 0x2f0, 0x2b0, 0x270, 0x230, 0x1f0, 0x170, 0x0f0, 0xf800 };
66 static const s16 fastgain[] = { 0x080, 0x100, 0x180, 0x200, 0x280, 0x300, 0x380, 0x400  };
67
68 static const s16 bndtab[] = {  0,  1,  2,   3,   4,   5,   6,   7,   8,   9,
69                         10, 11, 12,  13,  14,  15,  16,  17,  18,  19,
70                         20, 21, 22,  23,  24,  25,  26,  27,  28,  31,
71                         34, 37, 40,  43,  46,  49,  55,  61,  67,  73,
72                         79, 85, 97, 109, 121, 133, 157, 181, 205, 229 };
73
74 static const s16 bndsz[]  = { 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
75                         1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
76                         1,  1,  1,  1,  1,  1,  1,  1,  3,  3,
77                         3,  3,  3,  3,  3,  6,  6,  6,  6,  6,
78                         6, 12, 12, 12, 12, 24, 24, 24, 24, 24 };
79
80 static const s16 masktab[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
81                      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 28, 28, 29,
82                      29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34,
83                      34, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 37, 37, 37,
84                      37, 37, 37, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 40,
85                      40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
86                      41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43,
87                      43, 43, 43, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 44, 44,
88                      44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
89                      45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46,
90                      46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46,
91                      46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47,
92                      47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48,
93                      48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
94                      48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,
95                      49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,  0,  0,  0 };
96
97
98 static const s16 latab[] = { 0x0040, 0x003f, 0x003e, 0x003d, 0x003c, 0x003b, 0x003a, 0x0039,
99                     0x0038, 0x0037, 0x0036, 0x0035, 0x0034, 0x0034, 0x0033, 0x0032,
100                     0x0031, 0x0030, 0x002f, 0x002f, 0x002e, 0x002d, 0x002c, 0x002c,
101                     0x002b, 0x002a, 0x0029, 0x0029, 0x0028, 0x0027, 0x0026, 0x0026,
102                     0x0025, 0x0024, 0x0024, 0x0023, 0x0023, 0x0022, 0x0021, 0x0021,
103                     0x0020, 0x0020, 0x001f, 0x001e, 0x001e, 0x001d, 0x001d, 0x001c,
104                     0x001c, 0x001b, 0x001b, 0x001a, 0x001a, 0x0019, 0x0019, 0x0018,
105                     0x0018, 0x0017, 0x0017, 0x0016, 0x0016, 0x0015, 0x0015, 0x0015,
106                     0x0014, 0x0014, 0x0013, 0x0013, 0x0013, 0x0012, 0x0012, 0x0012,
107                     0x0011, 0x0011, 0x0011, 0x0010, 0x0010, 0x0010, 0x000f, 0x000f,
108                     0x000f, 0x000e, 0x000e, 0x000e, 0x000d, 0x000d, 0x000d, 0x000d,
109                     0x000c, 0x000c, 0x000c, 0x000c, 0x000b, 0x000b, 0x000b, 0x000b,
110                     0x000a, 0x000a, 0x000a, 0x000a, 0x000a, 0x0009, 0x0009, 0x0009,
111                     0x0009, 0x0009, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008,
112                     0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0006, 0x0006,
113                     0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0005, 0x0005,
114                     0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0004, 0x0004,
115                     0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004,
116                     0x0004, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003,
117                     0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0002,
118                     0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002,
119                     0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002,
120                     0x0002, 0x0002, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
121                     0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
122                     0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
123                     0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
124                     0x0001, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
125                     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
126                     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
127                     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
128                     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
129                     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
130                     0x0000, 0x0000, 0x0000, 0x0000};
131
132 static const s16 hth[][50] = {{ 0x04d0, 0x04d0, 0x0440, 0x0400, 0x03e0, 0x03c0, 0x03b0, 0x03b0,
133                       0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390,
134                       0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360, 0x0350, 0x0350,
135                       0x0340, 0x0340, 0x0330, 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0,
136                       0x02f0, 0x02f0, 0x0300, 0x0310, 0x0340, 0x0390, 0x03e0, 0x0420,
137                       0x0460, 0x0490, 0x04a0, 0x0460, 0x0440, 0x0440, 0x0520, 0x0800,
138                       0x0840, 0x0840 },
139
140                     { 0x04f0, 0x04f0, 0x0460, 0x0410, 0x03e0, 0x03d0, 0x03c0, 0x03b0,
141                       0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390,
142                       0x0390, 0x0380, 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360,
143                       0x0350, 0x0350, 0x0340, 0x0340, 0x0320, 0x0310, 0x0300, 0x02f0,
144                       0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0320, 0x0350, 0x0390, 0x03e0,
145                       0x0420, 0x0450, 0x04a0, 0x0490, 0x0460, 0x0440, 0x0480, 0x0630,
146                       0x0840, 0x0840 },
147
148                     { 0x0580, 0x0580, 0x04b0, 0x0450, 0x0420, 0x03f0, 0x03e0, 0x03d0,
149                       0x03c0, 0x03b0, 0x03b0, 0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0,
150                       0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390, 0x0390,
151                       0x0380, 0x0380, 0x0380, 0x0370, 0x0360, 0x0350, 0x0340, 0x0330,
152                       0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0310,
153                       0x0330, 0x0350, 0x03c0, 0x0410, 0x0470, 0x04a0, 0x0460, 0x0440,
154                       0x0450, 0x04e0 }};
155
156
157 static const s16 baptab[] = { 0,  1,  1,  1,  1,  1,  2,  2,  3,  3,  3,  4,  4,  5,  5,  6,
158                      6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9, 10,
159                      10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14,
160                      14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15 };
161
162 static __inline__ u16 max_value (s16 a, s16 b)
163 {
164     return (a > b ? a : b);
165 }
166
167 static __inline__ u16 min_value (s16 a, s16 b)
168 {
169     return (a < b ? a : b);
170 }
171
172 static __inline__ s16 logadd (s16 a, s16 b)
173 {
174     s16 c;
175
176     if ((c = a - b) >= 0) {
177         return (a + latab[min_value(((c) >> 1), 255)]);
178     } else {
179         return (b + latab[min_value(((-c) >> 1), 255)]);
180     }
181 }
182
183 static __inline__ s16 calc_lowcomp (s16 a, s16 b0, s16 b1, s16 bin)
184 {
185     if (bin < 7) {
186         if ((b0 + 256) == b1)
187             a = 384;
188         else if (b0 > b1)
189             a = max_value(0, a - 64);
190     } else if (bin < 20) {
191         if ((b0 + 256) == b1)
192             a = 320;
193         else if (b0 > b1)
194             a = max_value(0, a - 64) ;
195     } else
196         a = max_value(0, a - 128);
197
198     return a;
199 }
200
201 void bit_allocate (ac3dec_t * p_ac3dec)
202 {
203     u16 i;
204     s16 fgain;
205     s16 snroffset;
206     s16 start;
207     s16 end;
208     s16 fastleak;
209     s16 slowleak;
210
211     /* Only perform bit_allocation if the exponents have changed or we
212      * have new sideband information */
213     if (p_ac3dec->audblk.chexpstr[0]  == 0 && p_ac3dec->audblk.chexpstr[1] == 0 &&
214         p_ac3dec->audblk.chexpstr[2]  == 0 && p_ac3dec->audblk.chexpstr[3] == 0 &&
215         p_ac3dec->audblk.chexpstr[4]  == 0 && p_ac3dec->audblk.cplexpstr   == 0 &&
216         p_ac3dec->audblk.lfeexpstr    == 0 && p_ac3dec->audblk.baie        == 0 &&
217         p_ac3dec->audblk.snroffste    == 0 && p_ac3dec->audblk.deltbaie    == 0)
218         return;
219
220     /* Do some setup before we do the bit alloc */
221     p_ac3dec->bit_allocate.sdecay = slowdec[p_ac3dec->audblk.sdcycod];
222     p_ac3dec->bit_allocate.fdecay = fastdec[p_ac3dec->audblk.fdcycod];
223     p_ac3dec->bit_allocate.sgain = slowgain[p_ac3dec->audblk.sgaincod];
224     p_ac3dec->bit_allocate.dbknee = dbpbtab[p_ac3dec->audblk.dbpbcod];
225     p_ac3dec->bit_allocate.floor = floortab[p_ac3dec->audblk.floorcod];
226
227     /* if all the SNR offset constants are zero then the whole block is zero */
228     if (!p_ac3dec->audblk.csnroffst    && !p_ac3dec->audblk.fsnroffst[0] &&
229         !p_ac3dec->audblk.fsnroffst[1] && !p_ac3dec->audblk.fsnroffst[2] &&
230         !p_ac3dec->audblk.fsnroffst[3] && !p_ac3dec->audblk.fsnroffst[4] &&
231         !p_ac3dec->audblk.cplfsnroffst && !p_ac3dec->audblk.lfefsnroffst) {
232         memset(p_ac3dec->audblk.fbw_bap,0,sizeof(u16) * 256 * 5);
233         memset(p_ac3dec->audblk.cpl_bap,0,sizeof(u16) * 256);
234         memset(p_ac3dec->audblk.lfe_bap,0,sizeof(u16) * 7);
235         return;
236     }
237
238     for (i = 0; i < p_ac3dec->bsi.nfchans; i++) {
239         start = 0;
240         end = p_ac3dec->audblk.endmant[i] ;
241         fgain = fastgain[p_ac3dec->audblk.fgaincod[i]];
242         snroffset = (((p_ac3dec->audblk.csnroffst - 15) << 4) + p_ac3dec->audblk.fsnroffst[i]) << 2;
243         fastleak = 0;
244         slowleak = 0;
245
246         ba_compute_psd (&p_ac3dec->bit_allocate, start, end, p_ac3dec->audblk.fbw_exp[i]);
247
248         ba_compute_excitation (&p_ac3dec->bit_allocate, start, end , fgain, fastleak, slowleak, 0);
249
250         ba_compute_mask (&p_ac3dec->bit_allocate, start, end, p_ac3dec->syncinfo.fscod,
251                          p_ac3dec->audblk.deltbae[i],
252                          p_ac3dec->audblk.deltnseg[i],
253                          p_ac3dec->audblk.deltoffst[i],
254                          p_ac3dec->audblk.deltba[i],
255                          p_ac3dec->audblk.deltlen[i]);
256
257         ba_compute_bap (&p_ac3dec->bit_allocate, start, end, snroffset, p_ac3dec->audblk.fbw_bap[i]);
258     }
259
260     if (p_ac3dec->audblk.cplinu) {
261         start = p_ac3dec->audblk.cplstrtmant;
262         end = p_ac3dec->audblk.cplendmant;
263         fgain = fastgain[p_ac3dec->audblk.cplfgaincod];
264         snroffset = (((p_ac3dec->audblk.csnroffst - 15) << 4) + p_ac3dec->audblk.cplfsnroffst) << 2 ;
265         fastleak = (p_ac3dec->audblk.cplfleak << 8) + 768;
266         slowleak = (p_ac3dec->audblk.cplsleak << 8) + 768;
267
268         ba_compute_psd (&p_ac3dec->bit_allocate, start, end, p_ac3dec->audblk.cpl_exp);
269
270         ba_compute_excitation (&p_ac3dec->bit_allocate, start, end , fgain, fastleak, slowleak, 0);
271
272         ba_compute_mask (&p_ac3dec->bit_allocate, start, end, p_ac3dec->syncinfo.fscod,
273                          p_ac3dec->audblk.cpldeltbae,
274                          p_ac3dec->audblk.cpldeltnseg,
275                          p_ac3dec->audblk.cpldeltoffst,
276                          p_ac3dec->audblk.cpldeltba,
277                          p_ac3dec->audblk.cpldeltlen);
278
279         ba_compute_bap (&p_ac3dec->bit_allocate, start, end, snroffset, p_ac3dec->audblk.cpl_bap);
280     }
281
282     if (p_ac3dec->bsi.lfeon) {
283         start = 0;
284         end = 7;
285         fgain = fastgain[p_ac3dec->audblk.lfefgaincod];
286         snroffset = (((p_ac3dec->audblk.csnroffst - 15) << 4) + p_ac3dec->audblk.lfefsnroffst) << 2 ;
287         fastleak = 0;
288         slowleak = 0;
289
290         ba_compute_psd (&p_ac3dec->bit_allocate, start, end, p_ac3dec->audblk.lfe_exp);
291
292         ba_compute_excitation (&p_ac3dec->bit_allocate, start, end , fgain, fastleak, slowleak, 1);
293
294         ba_compute_mask (&p_ac3dec->bit_allocate, start, end, p_ac3dec->syncinfo.fscod, 2, 0, 0, 0, 0);
295
296         ba_compute_bap (&p_ac3dec->bit_allocate, start, end, snroffset, p_ac3dec->audblk.lfe_bap);
297     }
298 }
299
300
301 static void ba_compute_psd (bit_allocate_t * p_bit, s16 start, s16 end, s16 exps[])
302 {
303     int bin,j,k;
304     s16 lastbin = 0;
305
306     /* Map the exponents into dBs */
307     for (bin=start; bin<end; bin++) {
308         p_bit->psd[bin] = (3072 - (exps[bin] << 7));
309     }
310
311     /* Integrate the psd function over each bit allocation band */
312     j = start;
313     k = masktab[start];
314
315     do {
316         lastbin = min_value(bndtab[k] + bndsz[k], end);
317         p_bit->bndpsd[k] = p_bit->psd[j];
318         j++;
319
320         for (; j < lastbin; j++) {
321             p_bit->bndpsd[k] = logadd(p_bit->bndpsd[k],p_bit->psd[j]);
322         }
323
324         k++;
325     } while (end > lastbin);
326 }
327
328 static void ba_compute_excitation (bit_allocate_t * p_bit, s16 start, s16 end, 
329                         s16 fgain, s16 fastleak, s16 slowleak, s16 is_lfe)
330 {
331     int bin;
332     s16 bndstrt;
333     s16 bndend;
334     s16 lowcomp = 0;
335     s16 begin = 0;
336
337     /* Compute excitation function */
338     bndstrt = masktab[start];
339     bndend = masktab[end - 1] + 1;
340
341     if (bndstrt == 0) { /* For fbw and lfe channels */
342         lowcomp = calc_lowcomp(lowcomp, p_bit->bndpsd[0], p_bit->bndpsd[1], 0);
343         p_bit->excite[0] = p_bit->bndpsd[0] - fgain - lowcomp;
344         lowcomp = calc_lowcomp(lowcomp, p_bit->bndpsd[1], p_bit->bndpsd[2], 1);
345         p_bit->excite[1] = p_bit->bndpsd[1] - fgain - lowcomp;
346         begin = 7 ;
347
348         /* Note: Do not call calc_lowcomp() for the last band of the lfe channel, (bin = 6) */
349         for (bin = 2; bin < 7; bin++) {
350             if (!(is_lfe && (bin == 6)))
351                 lowcomp = calc_lowcomp (lowcomp, p_bit->bndpsd[bin], p_bit->bndpsd[bin+1], bin);
352             fastleak = p_bit->bndpsd[bin] - fgain;
353             slowleak = p_bit->bndpsd[bin] - p_bit->sgain;
354             p_bit->excite[bin] = fastleak - lowcomp;
355
356             if (!(is_lfe && (bin == 6))) {
357                 if (p_bit->bndpsd[bin] <= 
358                         p_bit->bndpsd[bin+1])
359                 {
360                     begin = bin + 1 ;
361                     break;
362                 }
363             }
364         }
365
366         for (bin = begin; bin < min_value(bndend, 22); bin++) {
367             if (!(is_lfe && (bin == 6)))
368                 lowcomp = calc_lowcomp (lowcomp, p_bit->bndpsd[bin],
369                             p_bit->bndpsd[bin+1], bin);
370             fastleak -= p_bit->fdecay ;
371             fastleak = max_value(fastleak, p_bit->bndpsd[bin] - fgain);
372             slowleak -= p_bit->sdecay ;
373             slowleak = max_value(slowleak, p_bit->bndpsd[bin] - p_bit->sgain);
374             p_bit->excite[bin] = max_value(fastleak - lowcomp, slowleak);
375         }
376         begin = 22;
377     } else { /* For coupling channel */
378         begin = bndstrt;
379     }
380
381     for (bin = begin; bin < bndend; bin++) {
382         fastleak -= p_bit->fdecay;
383         fastleak = max_value(fastleak, p_bit->bndpsd[bin] - fgain);
384         slowleak -= p_bit->sdecay;
385         slowleak = max_value(slowleak, p_bit->bndpsd[bin] - p_bit->sgain);
386         p_bit->excite[bin] = max_value(fastleak, slowleak) ;
387     }
388 }
389
390 static void ba_compute_mask (bit_allocate_t * p_bit, s16 start, s16 end, u16 fscod,
391                              u16 deltbae, u16 deltnseg, u16 deltoffst[],
392                              u16 deltba[], u16 deltlen[])
393 {
394     int bin,k;
395     s16 bndstrt;
396     s16 bndend;
397     s16 delta;
398
399     bndstrt = masktab[start];
400     bndend = masktab[end - 1] + 1;
401
402     /* Compute the masking curve */
403     for (bin = bndstrt; bin < bndend; bin++) {
404         if (p_bit->bndpsd[bin] < p_bit->dbknee) {
405             p_bit->excite[bin] += ((p_bit->dbknee - p_bit->bndpsd[bin]) >> 2);
406         }
407         p_bit->mask[bin] = max_value(p_bit->excite[bin], hth[fscod][bin]);
408     }
409
410     /* Perform delta bit modulation if necessary */
411     if ((deltbae == DELTA_BIT_REUSE) || (deltbae == DELTA_BIT_NEW)) {
412         s16 band = 0;
413         s16 seg = 0;
414
415         for (seg = 0; seg < deltnseg+1; seg++) {
416             band += deltoffst[seg];
417             if (deltba[seg] >= 4) {
418                 delta = (deltba[seg] - 3) << 7;
419             } else {
420                 delta = (deltba[seg] - 4) << 7;
421             }
422             for (k = 0; k < deltlen[seg]; k++) {
423                 p_bit->mask[band] += delta;
424                 band++;
425             }
426         }
427     }
428 }
429
430 static void ba_compute_bap (bit_allocate_t * p_bit, s16 start, s16 end, s16 snroffset,
431                             s16 bap[])
432 {
433     int i,j,k;
434     s16 lastbin = 0;
435     s16 address = 0;
436
437     /* Compute the bit allocation pointer for each bin */
438     i = start;
439     j = masktab[start];
440
441     do {
442         lastbin = min_value(bndtab[j] + bndsz[j], end);
443         p_bit->mask[j] -= snroffset;
444         p_bit->mask[j] -= p_bit->floor;
445
446         if (p_bit->mask[j] < 0)
447             p_bit->mask[j] = 0;
448
449         p_bit->mask[j] &= 0x1fe0;
450         p_bit->mask[j] += p_bit->floor;
451         for (k = i; k < lastbin; k++) {
452             address = (p_bit->psd[i] - p_bit->mask[j]) >> 5;
453             address = min_value(63, max_value(0, address));
454             bap[i] = baptab[address];
455             i++;
456         }
457         j++;
458     } while (end > lastbin);
459 }