-static inline int calc_lowcomp1(int a, int b0, int b1)
-{
- if ((b0 + 256) == b1) {
- a = 384 ;
- } else if (b0 > b1) {
- a = a - 64;
- if (a < 0) a=0;
- }
- return a;
-}
-
-static inline int calc_lowcomp(int a, int b0, int b1, int bin)
-{
- if (bin < 7) {
- if ((b0 + 256) == b1) {
- a = 384 ;
- } else if (b0 > b1) {
- a = a - 64;
- if (a < 0) a=0;
- }
- } else if (bin < 20) {
- if ((b0 + 256) == b1) {
- a = 320 ;
- } else if (b0 > b1) {
- a= a - 64;
- if (a < 0) a=0;
- }
- } else {
- a = a - 128;
- if (a < 0) a=0;
- }
- return a;
-}
-
-/* AC3 bit allocation. The algorithm is the one described in the AC3
- spec with some optimizations because of our simplified encoding
- assumptions. */
-void parametric_bit_allocation(AC3EncodeContext *s, UINT8 *bap,
- INT8 *exp, int start, int end,
- int snroffset, int fgain)
-{
- int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
- int fastleak,slowleak,address,tmp;
- INT16 psd[256]; /* scaled exponents */
- INT16 bndpsd[50]; /* interpolated exponents */
- INT16 excite[50]; /* excitation */
- INT16 mask[50]; /* masking value */
-
- /* exponent mapping to PSD */
- for(bin=start;bin<end;bin++) {
- psd[bin]=(3072 - (exp[bin] << 7));
- }
-
- /* PSD integration */
- j=start;
- k=masktab[start];
- do {
- v=psd[j];
- j++;
- end1=bndtab[k+1];
- if (end1 > end) end1=end;
- for(i=j;i<end1;i++) {
- int c,adr;
- /* logadd */
- v1=psd[j];
- c=v-v1;
- if (c >= 0) {
- adr=c >> 1;
- if (adr > 255) adr=255;
- v=v + latab[adr];
- } else {
- adr=(-c) >> 1;
- if (adr > 255) adr=255;
- v=v1 + latab[adr];
- }
- j++;
- }
- bndpsd[k]=v;
- k++;
- } while (end > bndtab[k]);
-
- /* excitation function */
- bndstrt = masktab[start];
- bndend = masktab[end-1] + 1;
-
- lowcomp = 0;
- lowcomp = calc_lowcomp1(lowcomp, bndpsd[0], bndpsd[1]) ;
- excite[0] = bndpsd[0] - fgain - lowcomp ;
- lowcomp = calc_lowcomp1(lowcomp, bndpsd[1], bndpsd[2]) ;
- excite[1] = bndpsd[1] - fgain - lowcomp ;
- begin = 7 ;
- for (bin = 2; bin < 7; bin++) {
- lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1]) ;
- fastleak = bndpsd[bin] - fgain ;
- slowleak = bndpsd[bin] - s->sgain ;
- excite[bin] = fastleak - lowcomp ;
- if (bndpsd[bin] <= bndpsd[bin+1]) {
- begin = bin + 1 ;
- break ;
- }
- }
-
- end1=bndend;
- if (end1 > 22) end1=22;
-
- for (bin = begin; bin < end1; bin++) {
- lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin) ;
-
- fastleak -= s->fdecay ;
- v = bndpsd[bin] - fgain;
- if (fastleak < v) fastleak = v;
-
- slowleak -= s->sdecay ;
- v = bndpsd[bin] - s->sgain;
- if (slowleak < v) slowleak = v;
-
- v=fastleak - lowcomp;
- if (slowleak > v) v=slowleak;
-
- excite[bin] = v;
- }
-
- for (bin = 22; bin < bndend; bin++) {
- fastleak -= s->fdecay ;
- v = bndpsd[bin] - fgain;
- if (fastleak < v) fastleak = v;
- slowleak -= s->sdecay ;
- v = bndpsd[bin] - s->sgain;
- if (slowleak < v) slowleak = v;
-
- v=fastleak;
- if (slowleak > v) v = slowleak;
- excite[bin] = v;
- }
-
- /* compute masking curve */
-
- for (bin = bndstrt; bin < bndend; bin++) {
- v1 = excite[bin];
- tmp = s->dbknee - bndpsd[bin];
- if (tmp > 0) {
- v1 += tmp >> 2;
- }
- v=hth[bin >> s->halfratecod][s->fscod];
- if (v1 > v) v=v1;
- mask[bin] = v;
- }
-
- /* compute bit allocation */
-
- i = start ;
- j = masktab[start] ;
- do {
- v=mask[j];
- v -= snroffset ;
- v -= s->floor ;
- if (v < 0) v = 0;
- v &= 0x1fe0 ;
- v += s->floor ;
-
- end1=bndtab[j] + bndsz[j];
- if (end1 > end) end1=end;
-
- for (k = i; k < end1; k++) {
- address = (psd[i] - v) >> 5 ;
- if (address < 0) address=0;
- else if (address > 63) address=63;
- bap[i] = baptab[address];
- i++;
- }
- } while (end > bndtab[j++]) ;
-}
-