1 /*****************************************************************************
2 * ac3_bit_allocate.c: ac3 allocation tables
3 *****************************************************************************
4 * Copyright (C) 2000 VideoLAN
5 * $Id: ac3_bit_allocate.c,v 1.16 2001/03/21 13:42:34 sam Exp $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Aaron Holtzman <aholtzma@engr.uvic.ca>
9 * Renaud Dartus <reno@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
27 #include "int_types.h"
28 #include "ac3_decoder.h"
29 #include "ac3_internal.h"
32 static void ba_compute_psd (bit_allocate_t * p_bit, s16 start, s16 end, s16 exps[]);
34 static void ba_compute_excitation (bit_allocate_t * p_bit, s16 start, s16 end, s16 fgain,
35 s16 fastleak, s16 slowleak, s16 is_lfe);
36 static void ba_compute_mask (bit_allocate_t * p_bit, s16 start, s16 end, u16 fscod,
37 u16 deltbae, u16 deltnseg, u16 deltoffst[],
38 u16 deltba[], u16 deltlen[]);
39 static void ba_compute_bap (bit_allocate_t * p_bit, s16 start, s16 end,
40 s16 snroffset, s16 bap[]);
42 /* Misc LUTs for bit allocation process */
44 static const s16 slowdec[] = { 0x0f, 0x11, 0x13, 0x15 };
45 static const s16 fastdec[] = { 0x3f, 0x53, 0x67, 0x7b };
46 static const s16 slowgain[] = { 0x540, 0x4d8, 0x478, 0x410 };
47 static const s16 dbpbtab[] = { 0x000, 0x700, 0x900, 0xb00 };
49 static const u16 floortab[] = { 0x2f0, 0x2b0, 0x270, 0x230, 0x1f0, 0x170, 0x0f0, 0xf800 };
50 static const s16 fastgain[] = { 0x080, 0x100, 0x180, 0x200, 0x280, 0x300, 0x380, 0x400 };
52 static const s16 bndtab[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
53 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
54 20, 21, 22, 23, 24, 25, 26, 27, 28, 31,
55 34, 37, 40, 43, 46, 49, 55, 61, 67, 73,
56 79, 85, 97, 109, 121, 133, 157, 181, 205, 229 };
58 static const s16 bndsz[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
59 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
60 1, 1, 1, 1, 1, 1, 1, 1, 3, 3,
61 3, 3, 3, 3, 3, 6, 6, 6, 6, 6,
62 6, 12, 12, 12, 12, 24, 24, 24, 24, 24 };
64 static const s16 masktab[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
65 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 28, 28, 29,
66 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34,
67 34, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 37, 37, 37,
68 37, 37, 37, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 40,
69 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
70 41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43,
71 43, 43, 43, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 44, 44,
72 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
73 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46,
74 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46,
75 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47,
76 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48,
77 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
78 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,
79 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 0, 0, 0 };
82 static const s16 latab[] = { 0x0040, 0x003f, 0x003e, 0x003d, 0x003c, 0x003b, 0x003a, 0x0039,
83 0x0038, 0x0037, 0x0036, 0x0035, 0x0034, 0x0034, 0x0033, 0x0032,
84 0x0031, 0x0030, 0x002f, 0x002f, 0x002e, 0x002d, 0x002c, 0x002c,
85 0x002b, 0x002a, 0x0029, 0x0029, 0x0028, 0x0027, 0x0026, 0x0026,
86 0x0025, 0x0024, 0x0024, 0x0023, 0x0023, 0x0022, 0x0021, 0x0021,
87 0x0020, 0x0020, 0x001f, 0x001e, 0x001e, 0x001d, 0x001d, 0x001c,
88 0x001c, 0x001b, 0x001b, 0x001a, 0x001a, 0x0019, 0x0019, 0x0018,
89 0x0018, 0x0017, 0x0017, 0x0016, 0x0016, 0x0015, 0x0015, 0x0015,
90 0x0014, 0x0014, 0x0013, 0x0013, 0x0013, 0x0012, 0x0012, 0x0012,
91 0x0011, 0x0011, 0x0011, 0x0010, 0x0010, 0x0010, 0x000f, 0x000f,
92 0x000f, 0x000e, 0x000e, 0x000e, 0x000d, 0x000d, 0x000d, 0x000d,
93 0x000c, 0x000c, 0x000c, 0x000c, 0x000b, 0x000b, 0x000b, 0x000b,
94 0x000a, 0x000a, 0x000a, 0x000a, 0x000a, 0x0009, 0x0009, 0x0009,
95 0x0009, 0x0009, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008,
96 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0006, 0x0006,
97 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0005, 0x0005,
98 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0004, 0x0004,
99 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004,
100 0x0004, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003,
101 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0002,
102 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002,
103 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002,
104 0x0002, 0x0002, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
105 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
106 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
107 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
108 0x0001, 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
109 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
110 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
111 0x0000, 0x0000, 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};
116 static const s16 hth[][50] = {{ 0x04d0, 0x04d0, 0x0440, 0x0400, 0x03e0, 0x03c0, 0x03b0, 0x03b0,
117 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390,
118 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360, 0x0350, 0x0350,
119 0x0340, 0x0340, 0x0330, 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0,
120 0x02f0, 0x02f0, 0x0300, 0x0310, 0x0340, 0x0390, 0x03e0, 0x0420,
121 0x0460, 0x0490, 0x04a0, 0x0460, 0x0440, 0x0440, 0x0520, 0x0800,
124 { 0x04f0, 0x04f0, 0x0460, 0x0410, 0x03e0, 0x03d0, 0x03c0, 0x03b0,
125 0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390,
126 0x0390, 0x0380, 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360,
127 0x0350, 0x0350, 0x0340, 0x0340, 0x0320, 0x0310, 0x0300, 0x02f0,
128 0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0320, 0x0350, 0x0390, 0x03e0,
129 0x0420, 0x0450, 0x04a0, 0x0490, 0x0460, 0x0440, 0x0480, 0x0630,
132 { 0x0580, 0x0580, 0x04b0, 0x0450, 0x0420, 0x03f0, 0x03e0, 0x03d0,
133 0x03c0, 0x03b0, 0x03b0, 0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0,
134 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390, 0x0390,
135 0x0380, 0x0380, 0x0380, 0x0370, 0x0360, 0x0350, 0x0340, 0x0330,
136 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0310,
137 0x0330, 0x0350, 0x03c0, 0x0410, 0x0470, 0x04a0, 0x0460, 0x0440,
141 static const s16 baptab[] = { 0, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6,
142 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10,
143 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14,
144 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15 };
146 static __inline__ u16 max (s16 a, s16 b)
148 return (a > b ? a : b);
151 static __inline__ u16 min (s16 a, s16 b)
153 return (a < b ? a : b);
156 static __inline__ s16 logadd (s16 a, s16 b)
160 if ((c = a - b) >= 0) {
161 return (a + latab[min(((c) >> 1), 255)]);
163 return (b + latab[min(((-c) >> 1), 255)]);
167 static __inline__ s16 calc_lowcomp (s16 a, s16 b0, s16 b1, s16 bin)
170 if ((b0 + 256) == b1)
174 } else if (bin < 20) {
175 if ((b0 + 256) == b1)
185 void bit_allocate (ac3dec_t * p_ac3dec)
195 /* Only perform bit_allocation if the exponents have changed or we
196 * have new sideband information */
197 if (p_ac3dec->audblk.chexpstr[0] == 0 && p_ac3dec->audblk.chexpstr[1] == 0 &&
198 p_ac3dec->audblk.chexpstr[2] == 0 && p_ac3dec->audblk.chexpstr[3] == 0 &&
199 p_ac3dec->audblk.chexpstr[4] == 0 && p_ac3dec->audblk.cplexpstr == 0 &&
200 p_ac3dec->audblk.lfeexpstr == 0 && p_ac3dec->audblk.baie == 0 &&
201 p_ac3dec->audblk.snroffste == 0 && p_ac3dec->audblk.deltbaie == 0)
204 /* Do some setup before we do the bit alloc */
205 p_ac3dec->bit_allocate.sdecay = slowdec[p_ac3dec->audblk.sdcycod];
206 p_ac3dec->bit_allocate.fdecay = fastdec[p_ac3dec->audblk.fdcycod];
207 p_ac3dec->bit_allocate.sgain = slowgain[p_ac3dec->audblk.sgaincod];
208 p_ac3dec->bit_allocate.dbknee = dbpbtab[p_ac3dec->audblk.dbpbcod];
209 p_ac3dec->bit_allocate.floor = floortab[p_ac3dec->audblk.floorcod];
211 /* if all the SNR offset constants are zero then the whole block is zero */
212 if (!p_ac3dec->audblk.csnroffst && !p_ac3dec->audblk.fsnroffst[0] &&
213 !p_ac3dec->audblk.fsnroffst[1] && !p_ac3dec->audblk.fsnroffst[2] &&
214 !p_ac3dec->audblk.fsnroffst[3] && !p_ac3dec->audblk.fsnroffst[4] &&
215 !p_ac3dec->audblk.cplfsnroffst && !p_ac3dec->audblk.lfefsnroffst) {
216 memset(p_ac3dec->audblk.fbw_bap,0,sizeof(u16) * 256 * 5);
217 memset(p_ac3dec->audblk.cpl_bap,0,sizeof(u16) * 256);
218 memset(p_ac3dec->audblk.lfe_bap,0,sizeof(u16) * 7);
222 for (i = 0; i < p_ac3dec->bsi.nfchans; i++) {
224 end = p_ac3dec->audblk.endmant[i] ;
225 fgain = fastgain[p_ac3dec->audblk.fgaincod[i]];
226 snroffset = (((p_ac3dec->audblk.csnroffst - 15) << 4) + p_ac3dec->audblk.fsnroffst[i]) << 2;
230 ba_compute_psd (&p_ac3dec->bit_allocate, start, end, p_ac3dec->audblk.fbw_exp[i]);
232 ba_compute_excitation (&p_ac3dec->bit_allocate, start, end , fgain, fastleak, slowleak, 0);
234 ba_compute_mask (&p_ac3dec->bit_allocate, start, end, p_ac3dec->syncinfo.fscod,
235 p_ac3dec->audblk.deltbae[i],
236 p_ac3dec->audblk.deltnseg[i],
237 p_ac3dec->audblk.deltoffst[i],
238 p_ac3dec->audblk.deltba[i],
239 p_ac3dec->audblk.deltlen[i]);
241 ba_compute_bap (&p_ac3dec->bit_allocate, start, end, snroffset, p_ac3dec->audblk.fbw_bap[i]);
244 if (p_ac3dec->audblk.cplinu) {
245 start = p_ac3dec->audblk.cplstrtmant;
246 end = p_ac3dec->audblk.cplendmant;
247 fgain = fastgain[p_ac3dec->audblk.cplfgaincod];
248 snroffset = (((p_ac3dec->audblk.csnroffst - 15) << 4) + p_ac3dec->audblk.cplfsnroffst) << 2 ;
249 fastleak = (p_ac3dec->audblk.cplfleak << 8) + 768;
250 slowleak = (p_ac3dec->audblk.cplsleak << 8) + 768;
252 ba_compute_psd (&p_ac3dec->bit_allocate, start, end, p_ac3dec->audblk.cpl_exp);
254 ba_compute_excitation (&p_ac3dec->bit_allocate, start, end , fgain, fastleak, slowleak, 0);
256 ba_compute_mask (&p_ac3dec->bit_allocate, start, end, p_ac3dec->syncinfo.fscod,
257 p_ac3dec->audblk.cpldeltbae,
258 p_ac3dec->audblk.cpldeltnseg,
259 p_ac3dec->audblk.cpldeltoffst,
260 p_ac3dec->audblk.cpldeltba,
261 p_ac3dec->audblk.cpldeltlen);
263 ba_compute_bap (&p_ac3dec->bit_allocate, start, end, snroffset, p_ac3dec->audblk.cpl_bap);
266 if (p_ac3dec->bsi.lfeon) {
269 fgain = fastgain[p_ac3dec->audblk.lfefgaincod];
270 snroffset = (((p_ac3dec->audblk.csnroffst - 15) << 4) + p_ac3dec->audblk.lfefsnroffst) << 2 ;
274 ba_compute_psd (&p_ac3dec->bit_allocate, start, end, p_ac3dec->audblk.lfe_exp);
276 ba_compute_excitation (&p_ac3dec->bit_allocate, start, end , fgain, fastleak, slowleak, 1);
278 ba_compute_mask (&p_ac3dec->bit_allocate, start, end, p_ac3dec->syncinfo.fscod, 2, 0, 0, 0, 0);
280 ba_compute_bap (&p_ac3dec->bit_allocate, start, end, snroffset, p_ac3dec->audblk.lfe_bap);
285 static void ba_compute_psd (bit_allocate_t * p_bit, s16 start, s16 end, s16 exps[])
290 /* Map the exponents into dBs */
291 for (bin=start; bin<end; bin++) {
292 p_bit->psd[bin] = (3072 - (exps[bin] << 7));
295 /* Integrate the psd function over each bit allocation band */
300 lastbin = min(bndtab[k] + bndsz[k], end);
301 p_bit->bndpsd[k] = p_bit->psd[j];
304 for (; j < lastbin; j++) {
305 p_bit->bndpsd[k] = logadd(p_bit->bndpsd[k],p_bit->psd[j]);
309 } while (end > lastbin);
312 static void ba_compute_excitation (bit_allocate_t * p_bit, s16 start, s16 end,
313 s16 fgain, s16 fastleak, s16 slowleak, s16 is_lfe)
321 /* Compute excitation function */
322 bndstrt = masktab[start];
323 bndend = masktab[end - 1] + 1;
325 if (bndstrt == 0) { /* For fbw and lfe channels */
326 lowcomp = calc_lowcomp(lowcomp, p_bit->bndpsd[0], p_bit->bndpsd[1], 0);
327 p_bit->excite[0] = p_bit->bndpsd[0] - fgain - lowcomp;
328 lowcomp = calc_lowcomp(lowcomp, p_bit->bndpsd[1], p_bit->bndpsd[2], 1);
329 p_bit->excite[1] = p_bit->bndpsd[1] - fgain - lowcomp;
332 /* Note: Do not call calc_lowcomp() for the last band of the lfe channel, (bin = 6) */
333 for (bin = 2; bin < 7; bin++) {
334 if (!(is_lfe && (bin == 6)))
335 lowcomp = calc_lowcomp (lowcomp, p_bit->bndpsd[bin], p_bit->bndpsd[bin+1], bin);
336 fastleak = p_bit->bndpsd[bin] - fgain;
337 slowleak = p_bit->bndpsd[bin] - p_bit->sgain;
338 p_bit->excite[bin] = fastleak - lowcomp;
340 if (!(is_lfe && (bin == 6))) {
341 if (p_bit->bndpsd[bin] <=
342 p_bit->bndpsd[bin+1])
350 for (bin = begin; bin < min(bndend, 22); bin++) {
351 if (!(is_lfe && (bin == 6)))
352 lowcomp = calc_lowcomp (lowcomp, p_bit->bndpsd[bin],
353 p_bit->bndpsd[bin+1], bin);
354 fastleak -= p_bit->fdecay ;
355 fastleak = max(fastleak, p_bit->bndpsd[bin] - fgain);
356 slowleak -= p_bit->sdecay ;
357 slowleak = max(slowleak, p_bit->bndpsd[bin] - p_bit->sgain);
358 p_bit->excite[bin] = max(fastleak - lowcomp, slowleak);
361 } else { /* For coupling channel */
365 for (bin = begin; bin < bndend; bin++) {
366 fastleak -= p_bit->fdecay;
367 fastleak = max(fastleak, p_bit->bndpsd[bin] - fgain);
368 slowleak -= p_bit->sdecay;
369 slowleak = max(slowleak, p_bit->bndpsd[bin] - p_bit->sgain);
370 p_bit->excite[bin] = max(fastleak, slowleak) ;
374 static void ba_compute_mask (bit_allocate_t * p_bit, s16 start, s16 end, u16 fscod,
375 u16 deltbae, u16 deltnseg, u16 deltoffst[],
376 u16 deltba[], u16 deltlen[])
383 bndstrt = masktab[start];
384 bndend = masktab[end - 1] + 1;
386 /* Compute the masking curve */
387 for (bin = bndstrt; bin < bndend; bin++) {
388 if (p_bit->bndpsd[bin] < p_bit->dbknee) {
389 p_bit->excite[bin] += ((p_bit->dbknee - p_bit->bndpsd[bin]) >> 2);
391 p_bit->mask[bin] = max(p_bit->excite[bin], hth[fscod][bin]);
394 /* Perform delta bit modulation if necessary */
395 if ((deltbae == DELTA_BIT_REUSE) || (deltbae == DELTA_BIT_NEW)) {
399 for (seg = 0; seg < deltnseg+1; seg++) {
400 band += deltoffst[seg];
401 if (deltba[seg] >= 4) {
402 delta = (deltba[seg] - 3) << 7;
404 delta = (deltba[seg] - 4) << 7;
406 for (k = 0; k < deltlen[seg]; k++) {
407 p_bit->mask[band] += delta;
414 static void ba_compute_bap (bit_allocate_t * p_bit, s16 start, s16 end, s16 snroffset,
421 /* Compute the bit allocation pointer for each bin */
426 lastbin = min(bndtab[j] + bndsz[j], end);
427 p_bit->mask[j] -= snroffset;
428 p_bit->mask[j] -= p_bit->floor;
430 if (p_bit->mask[j] < 0)
433 p_bit->mask[j] &= 0x1fe0;
434 p_bit->mask[j] += p_bit->floor;
435 for (k = i; k < lastbin; k++) {
436 address = (p_bit->psd[i] - p_bit->mask[j]) >> 5;
437 address = min(63, max(0, address));
438 bap[i] = baptab[address];
442 } while (end > lastbin);