]> git.sesse.net Git - vlc/blobdiff - src/ac3_decoder/ac3_bit_allocate.c
* Header cleaning: filled all empty authors fields, added CVS $Id stuff.
[vlc] / src / ac3_decoder / ac3_bit_allocate.c
index 9b99abbc1c2311bd5b38450703d00c5dd2b43b33..e1c6a33ca751b3f5a222a1493a3db9c6ed367c8e 100644 (file)
@@ -2,23 +2,25 @@
  * ac3_bit_allocate.c: ac3 allocation tables
  *****************************************************************************
  * Copyright (C) 2000 VideoLAN
+ * $Id: ac3_bit_allocate.c,v 1.16 2001/03/21 13:42:34 sam Exp $
  *
- * Authors:
+ * Authors: Michel Kaempf <maxx@via.ecp.fr>
+ *          Aaron Holtzman <aholtzma@engr.uvic.ca>
+ *          Renaud Dartus <reno@videolan.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- *
+ * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
  *
- * You should have received a copy of the GNU General Public
- * License along with this program; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
  *****************************************************************************/
 #include "defs.h"
 
 #include "ac3_decoder.h"
 #include "ac3_internal.h"
 
-/*
-static inline s16 logadd (s16 a, s16  b);
-static s16 calc_lowcomp (s16 a, s16 b0, s16 b1, s16 bin);
-static inline u16 min (s16 a, s16 b);
-static inline u16 max (s16 a, s16 b);
-*/
-
-static void ba_compute_psd (s16 start, s16 end, s16 exps[],
-                           s16 psd[], s16 bndpsd[]);
-
-static void ba_compute_excitation (s16 start, s16 end, s16 fgain,
-                                  s16 fastleak, s16 slowleak, s16 is_lfe,
-                                  s16 bndpsd[], s16 excite[]);
-static void ba_compute_mask (s16 start, s16 end, u16 fscod,
-                            u16 deltbae, u16 deltnseg, u16 deltoffst[],
-                            u16 deltba[], u16 deltlen[], s16 excite[],
-                            s16 mask[]);
-static void ba_compute_bap (s16 start, s16 end, s16 snroffset,
-                           s16 psd[], s16 mask[], s16 bap[]);
+
+static void ba_compute_psd (bit_allocate_t * p_bit, s16 start, s16 end, s16 exps[]);
+
+static void ba_compute_excitation (bit_allocate_t * p_bit, s16 start, s16 end, s16 fgain,
+                                   s16 fastleak, s16 slowleak, s16 is_lfe);
+static void ba_compute_mask (bit_allocate_t * p_bit, s16 start, s16 end, u16 fscod,
+                             u16 deltbae, u16 deltnseg, u16 deltoffst[],
+                             u16 deltba[], u16 deltlen[]);
+static void ba_compute_bap (bit_allocate_t * p_bit, s16 start, s16 end,
+                            s16 snroffset, s16 bap[]);
 
 /* Misc LUTs for bit allocation process */
 
-static s16 slowdec[]  = { 0x0f,  0x11,  0x13,  0x15  };
-static s16 fastdec[]  = { 0x3f,  0x53,  0x67,  0x7b  };
-static s16 slowgain[] = { 0x540, 0x4d8, 0x478, 0x410 };
-static s16 dbpbtab[]  = { 0x000, 0x700, 0x900, 0xb00 };
+static const s16 slowdec[]  = { 0x0f,  0x11,  0x13,  0x15  };
+static const s16 fastdec[]  = { 0x3f,  0x53,  0x67,  0x7b  };
+static const s16 slowgain[] = { 0x540, 0x4d8, 0x478, 0x410 };
+static const s16 dbpbtab[]  = { 0x000, 0x700, 0x900, 0xb00 };
 
-static u16 floortab[] = { 0x2f0, 0x2b0, 0x270, 0x230, 0x1f0, 0x170, 0x0f0, 0xf800 };
-static s16 fastgain[] = { 0x080, 0x100, 0x180, 0x200, 0x280, 0x300, 0x380, 0x400  };
+static const u16 floortab[] = { 0x2f0, 0x2b0, 0x270, 0x230, 0x1f0, 0x170, 0x0f0, 0xf800 };
+static const s16 fastgain[] = { 0x080, 0x100, 0x180, 0x200, 0x280, 0x300, 0x380, 0x400  };
 
-static s16 bndtab[] = {  0,  1,  2,   3,   4,   5,   6,   7,   8,   9,
-                       10, 11, 12,  13,  14,  15,  16,  17,  18,  19,
-                       20, 21, 22,  23,  24,  25,  26,  27,  28,  31,
-                       34, 37, 40,  43,  46,  49,  55,  61,  67,  73,
-                       79, 85, 97, 109, 121, 133, 157, 181, 205, 229 };
+static const s16 bndtab[] = {  0,  1,  2,   3,   4,   5,   6,   7,   8,   9,
+                        10, 11, 12,  13,  14,  15,  16,  17,  18,  19,
+                        20, 21, 22,  23,  24,  25,  26,  27,  28,  31,
+                        34, 37, 40,  43,  46,  49,  55,  61,  67,  73,
+                        79, 85, 97, 109, 121, 133, 157, 181, 205, 229 };
 
-static s16 bndsz[]  = { 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
-                       1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
-                       1,  1,  1,  1,  1,  1,  1,  1,  3,  3,
-                       3,  3,  3,  3,  3,  6,  6,  6,  6,  6,
-                       6, 12, 12, 12, 12, 24, 24, 24, 24, 24 };
+static const s16 bndsz[]  = { 1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
+                        1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
+                        1,  1,  1,  1,  1,  1,  1,  1,  3,  3,
+                        3,  3,  3,  3,  3,  6,  6,  6,  6,  6,
+                        6, 12, 12, 12, 12, 24, 24, 24, 24, 24 };
 
-static s16 masktab[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
+static const s16 masktab[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
                      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 28, 28, 29,
                      29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34,
                      34, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 37, 37, 37,
@@ -86,7 +79,7 @@ static s16 masktab[] = { 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13,
                      49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,  0,  0,  0 };
 
 
-static s16 latab[] = { 0x0040, 0x003f, 0x003e, 0x003d, 0x003c, 0x003b, 0x003a, 0x0039,
+static const s16 latab[] = { 0x0040, 0x003f, 0x003e, 0x003d, 0x003c, 0x003b, 0x003a, 0x0039,
                     0x0038, 0x0037, 0x0036, 0x0035, 0x0034, 0x0034, 0x0033, 0x0032,
                     0x0031, 0x0030, 0x002f, 0x002f, 0x002e, 0x002d, 0x002c, 0x002c,
                     0x002b, 0x002a, 0x0029, 0x0029, 0x0028, 0x0027, 0x0026, 0x0026,
@@ -120,7 +113,7 @@ static s16 latab[] = { 0x0040, 0x003f, 0x003e, 0x003d, 0x003c, 0x003b, 0x003a, 0
                     0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
                     0x0000, 0x0000, 0x0000, 0x0000};
 
-static s16 hth[][50] = {{ 0x04d0, 0x04d0, 0x0440, 0x0400, 0x03e0, 0x03c0, 0x03b0, 0x03b0,
+static const s16 hth[][50] = {{ 0x04d0, 0x04d0, 0x0440, 0x0400, 0x03e0, 0x03c0, 0x03b0, 0x03b0,
                       0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390,
                       0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360, 0x0350, 0x0350,
                       0x0340, 0x0340, 0x0330, 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0,
@@ -145,21 +138,11 @@ static s16 hth[][50] = {{ 0x04d0, 0x04d0, 0x0440, 0x0400, 0x03e0, 0x03c0, 0x03b0
                       0x0450, 0x04e0 }};
 
 
-static s16 baptab[] = { 0,  1,  1,  1,  1,  1,  2,  2,  3,  3,  3,  4,  4,  5,  5,  6,
+static const s16 baptab[] = { 0,  1,  1,  1,  1,  1,  2,  2,  3,  3,  3,  4,  4,  5,  5,  6,
                      6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  8,  9,  9,  9,  9, 10,
                      10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14,
                      14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15 };
 
-static s16 sdecay;
-static s16 fdecay;
-static s16 sgain;
-static s16 dbknee;
-static s16 floor;
-static s16 psd[256];
-static s16 bndpsd[256];
-static s16 excite[256];
-static s16 mask[256];
-
 static __inline__ u16 max (s16 a, s16 b)
 {
     return (a > b ? a : b);
@@ -212,24 +195,24 @@ void bit_allocate (ac3dec_t * p_ac3dec)
     /* Only perform bit_allocation if the exponents have changed or we
      * have new sideband information */
     if (p_ac3dec->audblk.chexpstr[0]  == 0 && p_ac3dec->audblk.chexpstr[1] == 0 &&
-       p_ac3dec->audblk.chexpstr[2]  == 0 && p_ac3dec->audblk.chexpstr[3] == 0 &&
-       p_ac3dec->audblk.chexpstr[4]  == 0 && p_ac3dec->audblk.cplexpstr   == 0 &&
-       p_ac3dec->audblk.lfeexpstr    == 0 && p_ac3dec->audblk.baie        == 0 &&
-       p_ac3dec->audblk.snroffste    == 0 && p_ac3dec->audblk.deltbaie    == 0)
+        p_ac3dec->audblk.chexpstr[2]  == 0 && p_ac3dec->audblk.chexpstr[3] == 0 &&
+        p_ac3dec->audblk.chexpstr[4]  == 0 && p_ac3dec->audblk.cplexpstr   == 0 &&
+        p_ac3dec->audblk.lfeexpstr    == 0 && p_ac3dec->audblk.baie        == 0 &&
+        p_ac3dec->audblk.snroffste    == 0 && p_ac3dec->audblk.deltbaie    == 0)
         return;
 
     /* Do some setup before we do the bit alloc */
-    sdecay = slowdec[p_ac3dec->audblk.sdcycod];
-    fdecay = fastdec[p_ac3dec->audblk.fdcycod];
-    sgain = slowgain[p_ac3dec->audblk.sgaincod];
-    dbknee = dbpbtab[p_ac3dec->audblk.dbpbcod];
-    floor = floortab[p_ac3dec->audblk.floorcod];
+    p_ac3dec->bit_allocate.sdecay = slowdec[p_ac3dec->audblk.sdcycod];
+    p_ac3dec->bit_allocate.fdecay = fastdec[p_ac3dec->audblk.fdcycod];
+    p_ac3dec->bit_allocate.sgain = slowgain[p_ac3dec->audblk.sgaincod];
+    p_ac3dec->bit_allocate.dbknee = dbpbtab[p_ac3dec->audblk.dbpbcod];
+    p_ac3dec->bit_allocate.floor = floortab[p_ac3dec->audblk.floorcod];
 
     /* if all the SNR offset constants are zero then the whole block is zero */
     if (!p_ac3dec->audblk.csnroffst    && !p_ac3dec->audblk.fsnroffst[0] &&
-       !p_ac3dec->audblk.fsnroffst[1] && !p_ac3dec->audblk.fsnroffst[2] &&
-       !p_ac3dec->audblk.fsnroffst[3] && !p_ac3dec->audblk.fsnroffst[4] &&
-       !p_ac3dec->audblk.cplfsnroffst && !p_ac3dec->audblk.lfefsnroffst) {
+        !p_ac3dec->audblk.fsnroffst[1] && !p_ac3dec->audblk.fsnroffst[2] &&
+        !p_ac3dec->audblk.fsnroffst[3] && !p_ac3dec->audblk.fsnroffst[4] &&
+        !p_ac3dec->audblk.cplfsnroffst && !p_ac3dec->audblk.lfefsnroffst) {
         memset(p_ac3dec->audblk.fbw_bap,0,sizeof(u16) * 256 * 5);
         memset(p_ac3dec->audblk.cpl_bap,0,sizeof(u16) * 256);
         memset(p_ac3dec->audblk.lfe_bap,0,sizeof(u16) * 7);
@@ -240,24 +223,22 @@ void bit_allocate (ac3dec_t * p_ac3dec)
         start = 0;
         end = p_ac3dec->audblk.endmant[i] ;
         fgain = fastgain[p_ac3dec->audblk.fgaincod[i]];
-        snroffset = (((p_ac3dec->audblk.csnroffst - 15) << 4) + p_ac3dec->audblk.fsnroffst[i]) << 2 ;
+        snroffset = (((p_ac3dec->audblk.csnroffst - 15) << 4) + p_ac3dec->audblk.fsnroffst[i]) << 2;
         fastleak = 0;
         slowleak = 0;
 
-        ba_compute_psd (start, end, p_ac3dec->audblk.fbw_exp[i], psd, bndpsd);
+        ba_compute_psd (&p_ac3dec->bit_allocate, start, end, p_ac3dec->audblk.fbw_exp[i]);
 
-        ba_compute_excitation (start, end , fgain, fastleak, slowleak, 0,
-                              bndpsd, excite);
+        ba_compute_excitation (&p_ac3dec->bit_allocate, start, end , fgain, fastleak, slowleak, 0);
 
-        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);
+        ba_compute_mask (&p_ac3dec->bit_allocate, 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]);
 
-        ba_compute_bap (start, end, snroffset, psd, mask,
-                       p_ac3dec->audblk.fbw_bap[i]);
+        ba_compute_bap (&p_ac3dec->bit_allocate, start, end, snroffset, p_ac3dec->audblk.fbw_bap[i]);
     }
 
     if (p_ac3dec->audblk.cplinu) {
@@ -268,20 +249,18 @@ void bit_allocate (ac3dec_t * p_ac3dec)
         fastleak = (p_ac3dec->audblk.cplfleak << 8) + 768;
         slowleak = (p_ac3dec->audblk.cplsleak << 8) + 768;
 
-        ba_compute_psd (start, end, p_ac3dec->audblk.cpl_exp, psd, bndpsd);
+        ba_compute_psd (&p_ac3dec->bit_allocate, start, end, p_ac3dec->audblk.cpl_exp);
 
-        ba_compute_excitation (start, end , fgain, fastleak, slowleak, 0,
-                              bndpsd, excite);
+        ba_compute_excitation (&p_ac3dec->bit_allocate, start, end , fgain, fastleak, slowleak, 0);
 
-        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);
+        ba_compute_mask (&p_ac3dec->bit_allocate, 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);
 
-        ba_compute_bap (start, end, snroffset, psd, mask,
-                       p_ac3dec->audblk.cpl_bap);
+        ba_compute_bap (&p_ac3dec->bit_allocate, start, end, snroffset, p_ac3dec->audblk.cpl_bap);
     }
 
     if (p_ac3dec->bsi.lfeon) {
@@ -292,29 +271,25 @@ void bit_allocate (ac3dec_t * p_ac3dec)
         fastleak = 0;
         slowleak = 0;
 
-        ba_compute_psd (start, end, p_ac3dec->audblk.lfe_exp, psd, bndpsd);
+        ba_compute_psd (&p_ac3dec->bit_allocate, start, end, p_ac3dec->audblk.lfe_exp);
 
-        ba_compute_excitation (start, end , fgain, fastleak, slowleak, 1,
-                              bndpsd, excite);
+        ba_compute_excitation (&p_ac3dec->bit_allocate, start, end , fgain, fastleak, slowleak, 1);
 
-        ba_compute_mask (start, end, p_ac3dec->syncinfo.fscod, 2, 0, 0, 0, 0,
-                        excite, mask);
+        ba_compute_mask (&p_ac3dec->bit_allocate, start, end, p_ac3dec->syncinfo.fscod, 2, 0, 0, 0, 0);
 
-        ba_compute_bap (start, end, snroffset, psd, mask,
-                       p_ac3dec->audblk.lfe_bap);
+        ba_compute_bap (&p_ac3dec->bit_allocate, start, end, snroffset, p_ac3dec->audblk.lfe_bap);
     }
 }
 
 
-static void ba_compute_psd (s16 start, s16 end, s16 exps[], s16 psd[],
-                           s16 bndpsd[])
+static void ba_compute_psd (bit_allocate_t * p_bit, s16 start, s16 end, s16 exps[])
 {
-    int bin,i,j,k;
+    int bin,j,k;
     s16 lastbin = 0;
 
     /* Map the exponents into dBs */
     for (bin=start; bin<end; bin++) {
-        psd[bin] = (3072 - (exps[bin] << 7));
+        p_bit->psd[bin] = (3072 - (exps[bin] << 7));
     }
 
     /* Integrate the psd function over each bit allocation band */
@@ -323,21 +298,19 @@ static void ba_compute_psd (s16 start, s16 end, s16 exps[], s16 psd[],
 
     do {
         lastbin = min(bndtab[k] + bndsz[k], end);
-        bndpsd[k] = psd[j];
+        p_bit->bndpsd[k] = p_bit->psd[j];
         j++;
 
-        for (i = j; i < lastbin; i++) {
-            bndpsd[k] = logadd(bndpsd[k], psd[j]);
-            j++;
+        for (; j < lastbin; j++) {
+            p_bit->bndpsd[k] = logadd(p_bit->bndpsd[k],p_bit->psd[j]);
         }
 
         k++;
     } while (end > lastbin);
 }
 
-static void ba_compute_excitation (s16 start, s16 end,s16 fgain, s16 fastleak,
-                                  s16 slowleak, s16 is_lfe, s16 bndpsd[],
-                                  s16 excite[])
+static void ba_compute_excitation (bit_allocate_t * p_bit, s16 start, s16 end, 
+                        s16 fgain, s16 fastleak, s16 slowleak, s16 is_lfe)
 {
     int bin;
     s16 bndstrt;
@@ -350,22 +323,24 @@ static void ba_compute_excitation (s16 start, s16 end,s16 fgain, s16 fastleak,
     bndend = masktab[end - 1] + 1;
 
     if (bndstrt == 0) { /* For fbw and lfe channels */
-        lowcomp = calc_lowcomp(lowcomp, bndpsd[0], bndpsd[1], 0);
-        excite[0] = bndpsd[0] - fgain - lowcomp;
-        lowcomp = calc_lowcomp(lowcomp, bndpsd[1], bndpsd[2], 1);
-        excite[1] = bndpsd[1] - fgain - lowcomp;
+        lowcomp = calc_lowcomp(lowcomp, p_bit->bndpsd[0], p_bit->bndpsd[1], 0);
+        p_bit->excite[0] = p_bit->bndpsd[0] - fgain - lowcomp;
+        lowcomp = calc_lowcomp(lowcomp, p_bit->bndpsd[1], p_bit->bndpsd[2], 1);
+        p_bit->excite[1] = p_bit->bndpsd[1] - fgain - lowcomp;
         begin = 7 ;
 
         /* Note: Do not call calc_lowcomp() for the last band of the lfe channel, (bin = 6) */
         for (bin = 2; bin < 7; bin++) {
             if (!(is_lfe && (bin == 6)))
-                lowcomp = calc_lowcomp (lowcomp, bndpsd[bin], bndpsd[bin+1], bin);
-           fastleak = bndpsd[bin] - fgain;
-           slowleak = bndpsd[bin] - sgain;
-           excite[bin] = fastleak - lowcomp;
+                lowcomp = calc_lowcomp (lowcomp, p_bit->bndpsd[bin], p_bit->bndpsd[bin+1], bin);
+            fastleak = p_bit->bndpsd[bin] - fgain;
+            slowleak = p_bit->bndpsd[bin] - p_bit->sgain;
+            p_bit->excite[bin] = fastleak - lowcomp;
 
             if (!(is_lfe && (bin == 6))) {
-                if (bndpsd[bin] <= bndpsd[bin+1]) {
+                if (p_bit->bndpsd[bin] <= 
+                        p_bit->bndpsd[bin+1])
+                {
                     begin = bin + 1 ;
                     break;
                 }
@@ -374,12 +349,13 @@ static void ba_compute_excitation (s16 start, s16 end,s16 fgain, s16 fastleak,
 
         for (bin = begin; bin < min(bndend, 22); bin++) {
             if (!(is_lfe && (bin == 6)))
-                lowcomp = calc_lowcomp (lowcomp, bndpsd[bin], bndpsd[bin+1], bin);
-           fastleak -= fdecay ;
-           fastleak = max(fastleak, bndpsd[bin] - fgain);
-           slowleak -= sdecay ;
-           slowleak = max(slowleak, bndpsd[bin] - sgain);
-           excite[bin] = max(fastleak - lowcomp, slowleak);
+                lowcomp = calc_lowcomp (lowcomp, p_bit->bndpsd[bin],
+                            p_bit->bndpsd[bin+1], bin);
+            fastleak -= p_bit->fdecay ;
+            fastleak = max(fastleak, p_bit->bndpsd[bin] - fgain);
+            slowleak -= p_bit->sdecay ;
+            slowleak = max(slowleak, p_bit->bndpsd[bin] - p_bit->sgain);
+            p_bit->excite[bin] = max(fastleak - lowcomp, slowleak);
         }
         begin = 22;
     } else { /* For coupling channel */
@@ -387,17 +363,17 @@ static void ba_compute_excitation (s16 start, s16 end,s16 fgain, s16 fastleak,
     }
 
     for (bin = begin; bin < bndend; bin++) {
-        fastleak -= fdecay;
-        fastleak = max(fastleak, bndpsd[bin] - fgain);
-        slowleak -= sdecay;
-        slowleak = max(slowleak, bndpsd[bin] - sgain);
-        excite[bin] = max(fastleak, slowleak) ;
+        fastleak -= p_bit->fdecay;
+        fastleak = max(fastleak, p_bit->bndpsd[bin] - fgain);
+        slowleak -= p_bit->sdecay;
+        slowleak = max(slowleak, p_bit->bndpsd[bin] - p_bit->sgain);
+        p_bit->excite[bin] = max(fastleak, slowleak) ;
     }
 }
 
-static void ba_compute_mask (s16 start, s16 end, u16 fscod, u16 deltbae,
-                            u16 deltnseg, u16 deltoffst[], u16 deltba[],
-                            u16 deltlen[], s16 excite[], s16 mask[])
+static void ba_compute_mask (bit_allocate_t * p_bit, s16 start, s16 end, u16 fscod,
+                             u16 deltbae, u16 deltnseg, u16 deltoffst[],
+                             u16 deltba[], u16 deltlen[])
 {
     int bin,k;
     s16 bndstrt;
@@ -409,10 +385,10 @@ static void ba_compute_mask (s16 start, s16 end, u16 fscod, u16 deltbae,
 
     /* Compute the masking curve */
     for (bin = bndstrt; bin < bndend; bin++) {
-        if (bndpsd[bin] < dbknee) {
-            excite[bin] += ((dbknee - bndpsd[bin]) >> 2);
+        if (p_bit->bndpsd[bin] < p_bit->dbknee) {
+            p_bit->excite[bin] += ((p_bit->dbknee - p_bit->bndpsd[bin]) >> 2);
         }
-        mask[bin] = max(excite[bin], hth[fscod][bin]);
+        p_bit->mask[bin] = max(p_bit->excite[bin], hth[fscod][bin]);
     }
 
     /* Perform delta bit modulation if necessary */
@@ -428,15 +404,15 @@ static void ba_compute_mask (s16 start, s16 end, u16 fscod, u16 deltbae,
                 delta = (deltba[seg] - 4) << 7;
             }
             for (k = 0; k < deltlen[seg]; k++) {
-               mask[band] += delta;
-               band++;
+                p_bit->mask[band] += delta;
+                band++;
             }
         }
     }
 }
 
-static void ba_compute_bap (s16 start, s16 end, s16 snroffset, s16 psd[],
-                           s16 mask[], s16 bap[])
+static void ba_compute_bap (bit_allocate_t * p_bit, s16 start, s16 end, s16 snroffset,
+                            s16 bap[])
 {
     int i,j,k;
     s16 lastbin = 0;
@@ -448,16 +424,16 @@ static void ba_compute_bap (s16 start, s16 end, s16 snroffset, s16 psd[],
 
     do {
         lastbin = min(bndtab[j] + bndsz[j], end);
-        mask[j] -= snroffset;
-        mask[j] -= floor;
+        p_bit->mask[j] -= snroffset;
+        p_bit->mask[j] -= p_bit->floor;
 
-        if (mask[j] < 0)
-            mask[j] = 0;
+        if (p_bit->mask[j] < 0)
+            p_bit->mask[j] = 0;
 
-        mask[j] &= 0x1fe0;
-        mask[j] += floor;
+        p_bit->mask[j] &= 0x1fe0;
+        p_bit->mask[j] += p_bit->floor;
         for (k = i; k < lastbin; k++) {
-            address = (psd[i] - mask[j]) >> 5;
+            address = (p_bit->psd[i] - p_bit->mask[j]) >> 5;
             address = min(63, max(0, address));
             bap[i] = baptab[address];
             i++;