summaryrefslogtreecommitdiff
path: root/ac3dec/bit_allocate.c
diff options
context:
space:
mode:
Diffstat (limited to 'ac3dec/bit_allocate.c')
-rw-r--r--ac3dec/bit_allocate.c278
1 files changed, 145 insertions, 133 deletions
diff --git a/ac3dec/bit_allocate.c b/ac3dec/bit_allocate.c
index 053e09cd..00874340 100644
--- a/ac3dec/bit_allocate.c
+++ b/ac3dec/bit_allocate.c
@@ -28,46 +28,46 @@
-static inline sint_16 logadd(sint_16 a,sint_16 b);
-static sint_16 calc_lowcomp(sint_16 a,sint_16 b0,sint_16 b1,sint_16 bin);
-static inline uint_16 min(sint_16 a,sint_16 b);
-static inline uint_16 max(sint_16 a,sint_16 b);
-static void ba_compute_psd(sint_16 start, sint_16 end, sint_16 exps[],
- sint_16 psd[], sint_16 bndpsd[]);
-
-static void ba_compute_excitation(sint_16 start, sint_16 end,sint_16 fgain,
- sint_16 fastleak, sint_16 slowleak, sint_16 is_lfe, sint_16 bndpsd[],
- sint_16 excite[]);
-static void ba_compute_mask(sint_16 start, sint_16 end, uint_16 fscod,
- uint_16 deltbae, uint_16 deltnseg, uint_16 deltoffst[], uint_16 deltba[],
- uint_16 deltlen[], sint_16 excite[], sint_16 mask[]);
-static void ba_compute_bap(sint_16 start, sint_16 end, sint_16 snroffset,
- sint_16 psd[], sint_16 mask[], sint_16 bap[]);
+static inline int16_t logadd(int16_t a,int16_t b);
+static int16_t calc_lowcomp(int16_t a,int16_t b0,int16_t b1,int16_t bin);
+static inline uint16_t min(int16_t a,int16_t b);
+static inline uint16_t max(int16_t a,int16_t b);
+static void ba_compute_psd(int16_t start, int16_t end, int16_t exps[],
+ int16_t psd[], int16_t bndpsd[]);
+
+static void ba_compute_excitation(int16_t start, int16_t end,int16_t fgain,
+ int16_t fastleak, int16_t slowleak, int16_t is_lfe, int16_t bndpsd[],
+ int16_t excite[]);
+static void ba_compute_mask(int16_t start, int16_t end, uint16_t fscod,
+ uint16_t deltbae, uint16_t deltnseg, uint16_t deltoffst[], uint16_t deltba[],
+ uint16_t deltlen[], int16_t excite[], int16_t mask[]);
+static void ba_compute_bap(int16_t start, int16_t end, int16_t snroffset,
+ int16_t psd[], int16_t mask[], int16_t bap[]);
/* Misc LUTs for bit allocation process */
-static sint_16 slowdec[] = { 0x0f, 0x11, 0x13, 0x15 };
-static sint_16 fastdec[] = { 0x3f, 0x53, 0x67, 0x7b };
-static sint_16 slowgain[] = { 0x540, 0x4d8, 0x478, 0x410 };
-static sint_16 dbpbtab[] = { 0x000, 0x700, 0x900, 0xb00 };
+static int16_t slowdec[] = { 0x0f, 0x11, 0x13, 0x15 };
+static int16_t fastdec[] = { 0x3f, 0x53, 0x67, 0x7b };
+static int16_t slowgain[] = { 0x540, 0x4d8, 0x478, 0x410 };
+static int16_t dbpbtab[] = { 0x000, 0x700, 0x900, 0xb00 };
-static uint_16 floortab[] = { 0x2f0, 0x2b0, 0x270, 0x230, 0x1f0, 0x170, 0x0f0, 0xf800 };
-static sint_16 fastgain[] = { 0x080, 0x100, 0x180, 0x200, 0x280, 0x300, 0x380, 0x400 };
+static uint16_t floortab[] = { 0x2f0, 0x2b0, 0x270, 0x230, 0x1f0, 0x170, 0x0f0, 0xf800 };
+static int16_t fastgain[] = { 0x080, 0x100, 0x180, 0x200, 0x280, 0x300, 0x380, 0x400 };
-static sint_16 bndtab[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+static int16_t 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 sint_16 bndsz[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+static int16_t 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 sint_16 masktab[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
+static int16_t 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,
@@ -85,7 +85,7 @@ static sint_16 masktab[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 0, 0, 0 };
-static sint_16 latab[] = { 0x0040, 0x003f, 0x003e, 0x003d, 0x003c, 0x003b, 0x003a, 0x0039,
+static int16_t 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,
@@ -119,7 +119,7 @@ static sint_16 latab[] = { 0x0040, 0x003f, 0x003e, 0x003d, 0x003c, 0x003b, 0x003
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000};
-static sint_16 hth[][50] = {{ 0x04d0, 0x04d0, 0x0440, 0x0400, 0x03e0, 0x03c0, 0x03b0, 0x03b0,
+static int16_t 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,
@@ -144,38 +144,50 @@ static sint_16 hth[][50] = {{ 0x04d0, 0x04d0, 0x0440, 0x0400, 0x03e0, 0x03c0, 0x
0x0450, 0x04e0 }};
-static sint_16 baptab[] = { 0, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6,
+static int16_t 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 sint_16 sdecay;
-static sint_16 fdecay;
-static sint_16 sgain;
-static sint_16 dbknee;
-static sint_16 floor;
-static sint_16 psd[256];
-static sint_16 bndpsd[256];
-static sint_16 excite[256];
-static sint_16 mask[256];
-
-static inline uint_16
-max(sint_16 a,sint_16 b)
+static int16_t sdecay;
+static int16_t fdecay;
+static int16_t sgain;
+static int16_t dbknee;
+static int16_t floor;
+static int16_t psd[256];
+static int16_t bndpsd[256];
+static int16_t excite[256];
+static int16_t mask[256];
+
+
+/**
+ *
+ **/
+
+static inline uint16_t max(int16_t a,int16_t b)
{
return (a > b ? a : b);
}
+
+
+/**
+ *
+ **/
-static inline uint_16
-min(sint_16 a,sint_16 b)
+static inline uint16_t min(int16_t a,int16_t b)
{
return (a < b ? a : b);
}
-static inline sint_16
-logadd(sint_16 a,sint_16 b)
+
+/**
+ *
+ **/
+
+static inline int16_t logadd(int16_t a,int16_t b)
{
- sint_16 c;
- sint_16 address;
+ int16_t c;
+ int16_t address;
c = a - b;
address = min((abs(c) >> 1), 255);
@@ -187,15 +199,19 @@ logadd(sint_16 a,sint_16 b)
}
-void bit_allocate(uint_16 fscod, bsi_t *bsi, audblk_t *audblk)
+/**
+ *
+ **/
+
+void bit_allocate(uint16_t fscod, bsi_t *bsi, audblk_t *audblk)
{
- uint_16 i;
- sint_16 fgain;
- sint_16 snroffset;
- sint_16 start;
- sint_16 end;
- sint_16 fastleak;
- sint_16 slowleak;
+ uint16_t i;
+ int16_t fgain;
+ int16_t snroffset;
+ int16_t start;
+ int16_t end;
+ int16_t fastleak;
+ int16_t slowleak;
/* Only perform bit_allocation if the exponents have changed or we
* have new sideband information */
@@ -217,11 +233,10 @@ void bit_allocate(uint_16 fscod, bsi_t *bsi, audblk_t *audblk)
if(!audblk->csnroffst && !audblk->fsnroffst[0] &&
!audblk->fsnroffst[1] && !audblk->fsnroffst[2] &&
!audblk->fsnroffst[3] && !audblk->fsnroffst[4] &&
- !audblk->cplfsnroffst && !audblk->lfefsnroffst)
- {
- memset(audblk->fbw_bap,0,sizeof(uint_16) * 256 * 5);
- memset(audblk->cpl_bap,0,sizeof(uint_16) * 256);
- memset(audblk->lfe_bap,0,sizeof(uint_16) * 7);
+ !audblk->cplfsnroffst && !audblk->lfefsnroffst) {
+ memset(audblk->fbw_bap,0,sizeof(uint16_t) * 256 * 5);
+ memset(audblk->cpl_bap,0,sizeof(uint16_t) * 256);
+ memset(audblk->lfe_bap,0,sizeof(uint16_t) * 7);
return;
}
@@ -245,8 +260,7 @@ void bit_allocate(uint_16 fscod, bsi_t *bsi, audblk_t *audblk)
ba_compute_bap(start, end, snroffset, psd, mask, audblk->fbw_bap[i]);
}
- if(audblk->cplinu)
- {
+ if(audblk->cplinu) {
start = audblk->cplstrtmant;
end = audblk->cplendmant;
fgain = fastgain[audblk->cplfgaincod];
@@ -264,8 +278,7 @@ void bit_allocate(uint_16 fscod, bsi_t *bsi, audblk_t *audblk)
ba_compute_bap(start, end, snroffset, psd, mask, audblk->cpl_bap);
}
- if(bsi->lfeon)
- {
+ if(bsi->lfeon) {
start = 0;
end = 7;
fgain = fastgain[audblk->lfefgaincod];
@@ -285,15 +298,18 @@ void bit_allocate(uint_16 fscod, bsi_t *bsi, audblk_t *audblk)
}
-static void ba_compute_psd(sint_16 start, sint_16 end, sint_16 exps[],
- sint_16 psd[], sint_16 bndpsd[])
+/**
+ *
+ **/
+
+static void ba_compute_psd(int16_t start, int16_t end, int16_t exps[],
+ int16_t psd[], int16_t bndpsd[])
{
int bin,i,j,k;
- sint_16 lastbin = 0;
+ int16_t lastbin = 0;
/* Map the exponents into dBs */
- for (bin=start; bin<end; bin++)
- {
+ for (bin=start; bin<end; bin++) {
psd[bin] = (3072 - (exps[bin] << 7));
}
@@ -301,14 +317,12 @@ static void ba_compute_psd(sint_16 start, sint_16 end, sint_16 exps[],
j = start;
k = masktab[start];
- do
- {
+ do {
lastbin = min(bndtab[k] + bndsz[k], end);
bndpsd[k] = psd[j];
j++;
- for (i = j; i < lastbin; i++)
- {
+ for (i = j; i < lastbin; i++) {
bndpsd[k] = logadd(bndpsd[k], psd[j]);
j++;
}
@@ -317,49 +331,49 @@ static void ba_compute_psd(sint_16 start, sint_16 end, sint_16 exps[],
} while (end > lastbin);
}
-static void ba_compute_excitation(sint_16 start, sint_16 end,sint_16 fgain,
- sint_16 fastleak, sint_16 slowleak, sint_16 is_lfe, sint_16 bndpsd[],
- sint_16 excite[])
+
+/**
+ *
+ **/
+
+static void ba_compute_excitation(int16_t start, int16_t end,int16_t fgain,
+ int16_t fastleak, int16_t slowleak, int16_t is_lfe, int16_t bndpsd[],
+ int16_t excite[])
{
int bin;
- sint_16 bndstrt;
- sint_16 bndend;
- sint_16 lowcomp = 0;
- sint_16 begin = 0;
+ int16_t bndstrt;
+ int16_t bndend;
+ int16_t lowcomp = 0;
+ int16_t begin = 0;
/* Compute excitation function */
bndstrt = masktab[start];
bndend = masktab[end - 1] + 1;
- if (bndstrt == 0) /* For fbw and lfe channels */
- {
+ 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;
begin = 7 ;
- /* Note: Do not call calc_lowcomp() for the last band of the lfe channel, (bin = 6) */
- for (bin = 2; bin < 7; bin++)
- {
+// 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;
- if (!(is_lfe && (bin == 6)))
- {
- if (bndpsd[bin] <= bndpsd[bin+1])
- {
+ if (!(is_lfe && (bin == 6))) {
+ if (bndpsd[bin] <= bndpsd[bin+1]) {
begin = bin + 1 ;
break;
}
}
}
- for (bin = begin; bin < min(bndend, 22); bin++)
- {
+ 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 ;
@@ -371,12 +385,9 @@ static void ba_compute_excitation(sint_16 start, sint_16 end,sint_16 fgain,
begin = 22;
}
else /* For coupling channel */
- {
begin = bndstrt;
- }
- for (bin = begin; bin < bndend; bin++)
- {
+ for (bin = begin; bin < bndend; bin++) {
fastleak -= fdecay;
fastleak = max(fastleak, bndpsd[bin] - fgain);
slowleak -= sdecay;
@@ -385,49 +396,47 @@ static void ba_compute_excitation(sint_16 start, sint_16 end,sint_16 fgain,
}
}
-static void ba_compute_mask(sint_16 start, sint_16 end, uint_16 fscod,
- uint_16 deltbae, uint_16 deltnseg, uint_16 deltoffst[], uint_16 deltba[],
- uint_16 deltlen[], sint_16 excite[], sint_16 mask[])
+
+/**
+ *
+ **/
+
+static void ba_compute_mask(int16_t start, int16_t end, uint16_t fscod,
+ uint16_t deltbae, uint16_t deltnseg, uint16_t deltoffst[], uint16_t deltba[],
+ uint16_t deltlen[], int16_t excite[], int16_t mask[])
{
int bin,k;
- sint_16 bndstrt;
- sint_16 bndend;
- sint_16 delta;
+ int16_t bndstrt;
+ int16_t bndend;
+ int16_t delta;
bndstrt = masktab[start];
bndend = masktab[end - 1] + 1;
/* Compute the masking curve */
- for (bin = bndstrt; bin < bndend; bin++)
- {
- if (bndpsd[bin] < dbknee)
- {
+ for (bin = bndstrt; bin < bndend; bin++) {
+ if (bndpsd[bin] < dbknee) {
excite[bin] += ((dbknee - bndpsd[bin]) >> 2);
}
mask[bin] = max(excite[bin], hth[fscod][bin]);
}
/* Perform delta bit modulation if necessary */
- if ((deltbae == DELTA_BIT_REUSE) || (deltbae == DELTA_BIT_NEW))
- {
- sint_16 band = 0;
- sint_16 seg = 0;
+ if ((deltbae == DELTA_BIT_REUSE) || (deltbae == DELTA_BIT_NEW)) {
+ int16_t band = 0;
+ int16_t seg = 0;
- for (seg = 0; seg < deltnseg+1; seg++)
- {
+ for (seg = 0; seg < deltnseg+1; seg++) {
band += deltoffst[seg];
- if (deltba[seg] >= 4)
- {
+
+ if (deltba[seg] >= 4) {
delta = (deltba[seg] - 3) << 7;
- }
- else
- {
+ } else {
delta = (deltba[seg] - 4) << 7;
}
- for (k = 0; k < deltlen[seg]; k++)
- {
+ for (k = 0; k < deltlen[seg]; k++) {
mask[band] += delta;
band++;
}
@@ -435,19 +444,23 @@ static void ba_compute_mask(sint_16 start, sint_16 end, uint_16 fscod,
}
}
-static void ba_compute_bap(sint_16 start, sint_16 end, sint_16 snroffset,
- sint_16 psd[], sint_16 mask[], sint_16 bap[])
+
+/**
+ *
+ **/
+
+static void ba_compute_bap(int16_t start, int16_t end, int16_t snroffset,
+ int16_t psd[], int16_t mask[], int16_t bap[])
{
int i,j,k;
- sint_16 lastbin = 0;
- sint_16 address = 0;
+ int16_t lastbin = 0;
+ int16_t address = 0;
/* Compute the bit allocation pointer for each bin */
i = start;
j = masktab[start];
- do
- {
+ do {
lastbin = min(bndtab[j] + bndsz[j], end);
mask[j] -= snroffset;
mask[j] -= floor;
@@ -457,8 +470,7 @@ static void ba_compute_bap(sint_16 start, sint_16 end, sint_16 snroffset,
mask[j] &= 0x1fe0;
mask[j] += floor;
- for (k = i; k < lastbin; k++)
- {
+ for (k = i; k < lastbin; k++) {
address = (psd[i] - mask[j]) >> 5;
address = min(63, max(0, address));
bap[i] = baptab[address];
@@ -468,25 +480,25 @@ static void ba_compute_bap(sint_16 start, sint_16 end, sint_16 snroffset,
} while (end > lastbin);
}
-static sint_16
-calc_lowcomp(sint_16 a,sint_16 b0,sint_16 b1,sint_16 bin)
+
+/**
+ *
+ **/
+
+static int16_t calc_lowcomp (int16_t a, int16_t b0, int16_t b1, int16_t bin)
{
- if (bin < 7)
- {
+ if (bin < 7) {
if ((b0 + 256) == b1)
a = 384;
else if (b0 > b1)
a = max(0, a - 64);
- }
- else if (bin < 20)
- {
+ } else if (bin < 20) {
if ((b0 + 256) == b1)
a = 320;
else if (b0 > b1)
a = max(0, a - 64) ;
- }
- else
+ } else
a = max(0, a - 128);
return(a);