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