-
-/**
- * Generate a Kaiser Window.
- */
-static void k_window_init(int alpha, double *window, int n, int iter)
-{
- int j, k;
- double a, x;
- a = alpha * M_PI / n;
- a = a*a;
- for(k=0; k<n; k++) {
- x = k * (n - k) * a;
- window[k] = 1.0;
- for(j=iter; j>0; j--) {
- window[k] = (window[k] * x / (j*j)) + 1.0;
- }
- }
-}
-
-/**
- * Generate a Kaiser-Bessel Derived Window.
- * @param alpha determines window shape
- * @param window array to fill with window values
- * @param iter number of iterations to use in BesselI0
- */
-
-static void kbd_window_init(int alpha, int16_t *out_window, int iter)
-{
- int k, n2;
- double kwindow[256];
- double window[256];
-
- n2 = 256;
- k_window_init(alpha, kwindow, n2, iter);
- window[0] = kwindow[0];
- for(k=1; k<n2; k++) {
- window[k] = window[k-1] + kwindow[k];
- }
- for(k=0; k<n2; k++) {
- window[k] = sqrt(window[k] / (window[n2-1]+1));
- //out_window[k] = round(window[k]*((1<<15)-1)); //enable this for a rounded window
- out_window[k] = fix15(window[k]);
- }
-}
-
-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. */
-void ac3_parametric_bit_allocation(AC3BitAllocParameters *s, uint8_t *bap,
- int8_t *exp, int start, int end,
- int snroffset, int fgain, int is_lfe,
- int deltbae,int deltnseg,
- uint8_t *deltoffst, uint8_t *deltlen, uint8_t *deltba)
-{
- int bin,i,j,k,end1,v,v1,bndstrt,bndend,lowcomp,begin;
- int fastleak,slowleak,address,tmp;
- int16_t psd[256]; /* scaled exponents */
- int16_t bndpsd[50]; /* interpolated exponents */
- int16_t excite[50]; /* excitation */
- int16_t 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;
-
- if (bndstrt == 0) {
- 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++) {
- if (!(is_lfe && bin == 6))
- lowcomp = calc_lowcomp1(lowcomp, bndpsd[bin], bndpsd[bin+1]) ;
- fastleak = bndpsd[bin] - fgain ;
- slowleak = bndpsd[bin] - s->sgain ;
- excite[bin] = fastleak - lowcomp ;
- if (!(is_lfe && bin == 6)) {
- if (bndpsd[bin] <= bndpsd[bin+1]) {
- begin = bin + 1 ;
- break ;
- }
- }
- }
-
- end1=bndend;
- if (end1 > 22) end1=22;
-
- for (bin = begin; bin < end1; bin++) {
- if (!(is_lfe && bin == 6))
- 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;
- }
- begin = 22;
- } else {
- /* coupling channel */
- begin = bndstrt;
-
- fastleak = (s->cplfleak << 8) + 768;
- slowleak = (s->cplsleak << 8) + 768;
- }
-
- for (bin = begin; 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;
- }
-
- /* delta bit allocation */
-
- if (deltbae == 0 || deltbae == 1) {
- int band, seg, delta;
- band = 0 ;
- for (seg = 0; seg < deltnseg; seg++) {
- band += deltoffst[seg] ;
- if (deltba[seg] >= 4) {
- delta = (deltba[seg] - 3) << 7;
- } else {
- delta = (deltba[seg] - 4) << 7;
- }
- for (k = 0; k < deltlen[seg]; k++) {
- mask[band] += delta ;
- band++ ;
- }
- }
- }
-
- /* 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++]) ;
-}
-