1 #include <unistd.h> /* getpid() */
3 #include <stdio.h> /* "intf_msg.h" */
4 #include <stdlib.h> /* malloc(), free() */
5 #include <sys/soundcard.h> /* "audio_output.h" */
7 #include <sys/uio.h> /* "input.h" */
12 #include "vlc_thread.h"
13 #include "debug.h" /* "input_netlist.h" */
15 #include "intf_msg.h" /* intf_DbgMsg(), intf_ErrMsg() */
17 #include "input.h" /* pes_packet_t */
18 #include "input_netlist.h" /* input_NetlistFreePES() */
19 #include "decoder_fifo.h" /* DECODER_FIFO_(ISEMPTY|START|INCSTART)() */
21 #include "audio_output.h"
23 #include "ac3_decoder.h"
24 #include "ac3_bit_allocate.h"
27 static inline s16 logadd(s16 a,s16 b);
28 static s16 calc_lowcomp(s16 a,s16 b0,s16 b1,s16 bin);
29 static inline u16 min(s16 a,s16 b);
30 static inline u16 max(s16 a,s16 b);
32 static void ba_compute_psd(s16 start, s16 end, s16 exps[],
33 s16 psd[], s16 bndpsd[]);
35 static void ba_compute_excitation(s16 start, s16 end,s16 fgain,
36 s16 fastleak, s16 slowleak, s16 is_lfe, s16 bndpsd[],
38 static void ba_compute_mask(s16 start, s16 end, u16 fscod,
39 u16 deltbae, u16 deltnseg, u16 deltoffst[], u16 deltba[],
40 u16 deltlen[], s16 excite[], s16 mask[]);
41 static void ba_compute_bap(s16 start, s16 end, s16 snroffset,
42 s16 psd[], s16 mask[], s16 bap[]);
44 /* Misc LUTs for bit allocation process */
46 static s16 slowdec[] = { 0x0f, 0x11, 0x13, 0x15 };
47 static s16 fastdec[] = { 0x3f, 0x53, 0x67, 0x7b };
48 static s16 slowgain[] = { 0x540, 0x4d8, 0x478, 0x410 };
49 static s16 dbpbtab[] = { 0x000, 0x700, 0x900, 0xb00 };
51 static u16 floortab[] = { 0x2f0, 0x2b0, 0x270, 0x230, 0x1f0, 0x170, 0x0f0, 0xf800 };
52 static s16 fastgain[] = { 0x080, 0x100, 0x180, 0x200, 0x280, 0x300, 0x380, 0x400 };
55 static s16 bndtab[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
56 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
57 20, 21, 22, 23, 24, 25, 26, 27, 28, 31,
58 34, 37, 40, 43, 46, 49, 55, 61, 67, 73,
59 79, 85, 97, 109, 121, 133, 157, 181, 205, 229 };
61 static s16 bndsz[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
62 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
63 1, 1, 1, 1, 1, 1, 1, 1, 3, 3,
64 3, 3, 3, 3, 3, 6, 6, 6, 6, 6,
65 6, 12, 12, 12, 12, 24, 24, 24, 24, 24 };
67 static s16 masktab[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
68 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 28, 28, 29,
69 29, 29, 30, 30, 30, 31, 31, 31, 32, 32, 32, 33, 33, 33, 34, 34,
70 34, 35, 35, 35, 35, 35, 35, 36, 36, 36, 36, 36, 36, 37, 37, 37,
71 37, 37, 37, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 40,
72 40, 40, 40, 40, 40, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41,
73 41, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 43, 43, 43,
74 43, 43, 43, 43, 43, 43, 43, 43, 43, 44, 44, 44, 44, 44, 44, 44,
75 44, 44, 44, 44, 44, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45,
76 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 46, 46, 46,
77 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46, 46,
78 46, 46, 46, 46, 46, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47,
79 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 47, 48, 48, 48,
80 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
81 48, 48, 48, 48, 48, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49,
82 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 49, 0, 0, 0 };
85 static s16 latab[] = { 0x0040, 0x003f, 0x003e, 0x003d, 0x003c, 0x003b, 0x003a, 0x0039,
86 0x0038, 0x0037, 0x0036, 0x0035, 0x0034, 0x0034, 0x0033, 0x0032,
87 0x0031, 0x0030, 0x002f, 0x002f, 0x002e, 0x002d, 0x002c, 0x002c,
88 0x002b, 0x002a, 0x0029, 0x0029, 0x0028, 0x0027, 0x0026, 0x0026,
89 0x0025, 0x0024, 0x0024, 0x0023, 0x0023, 0x0022, 0x0021, 0x0021,
90 0x0020, 0x0020, 0x001f, 0x001e, 0x001e, 0x001d, 0x001d, 0x001c,
91 0x001c, 0x001b, 0x001b, 0x001a, 0x001a, 0x0019, 0x0019, 0x0018,
92 0x0018, 0x0017, 0x0017, 0x0016, 0x0016, 0x0015, 0x0015, 0x0015,
93 0x0014, 0x0014, 0x0013, 0x0013, 0x0013, 0x0012, 0x0012, 0x0012,
94 0x0011, 0x0011, 0x0011, 0x0010, 0x0010, 0x0010, 0x000f, 0x000f,
95 0x000f, 0x000e, 0x000e, 0x000e, 0x000d, 0x000d, 0x000d, 0x000d,
96 0x000c, 0x000c, 0x000c, 0x000c, 0x000b, 0x000b, 0x000b, 0x000b,
97 0x000a, 0x000a, 0x000a, 0x000a, 0x000a, 0x0009, 0x0009, 0x0009,
98 0x0009, 0x0009, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008, 0x0008,
99 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0007, 0x0006, 0x0006,
100 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0006, 0x0005, 0x0005,
101 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0005, 0x0004, 0x0004,
102 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004, 0x0004,
103 0x0004, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003,
104 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0003, 0x0002,
105 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002,
106 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002, 0x0002,
107 0x0002, 0x0002, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
108 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
109 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
110 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001, 0x0001,
111 0x0001, 0x0001, 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, 0x0000, 0x0000, 0x0000, 0x0000,
115 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
116 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
117 0x0000, 0x0000, 0x0000, 0x0000};
119 static s16 hth[][50] = {{ 0x04d0, 0x04d0, 0x0440, 0x0400, 0x03e0, 0x03c0, 0x03b0, 0x03b0,
120 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390,
121 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360, 0x0350, 0x0350,
122 0x0340, 0x0340, 0x0330, 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0,
123 0x02f0, 0x02f0, 0x0300, 0x0310, 0x0340, 0x0390, 0x03e0, 0x0420,
124 0x0460, 0x0490, 0x04a0, 0x0460, 0x0440, 0x0440, 0x0520, 0x0800,
127 { 0x04f0, 0x04f0, 0x0460, 0x0410, 0x03e0, 0x03d0, 0x03c0, 0x03b0,
128 0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390,
129 0x0390, 0x0380, 0x0380, 0x0380, 0x0370, 0x0370, 0x0360, 0x0360,
130 0x0350, 0x0350, 0x0340, 0x0340, 0x0320, 0x0310, 0x0300, 0x02f0,
131 0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0320, 0x0350, 0x0390, 0x03e0,
132 0x0420, 0x0450, 0x04a0, 0x0490, 0x0460, 0x0440, 0x0480, 0x0630,
135 { 0x0580, 0x0580, 0x04b0, 0x0450, 0x0420, 0x03f0, 0x03e0, 0x03d0,
136 0x03c0, 0x03b0, 0x03b0, 0x03b0, 0x03a0, 0x03a0, 0x03a0, 0x03a0,
137 0x03a0, 0x03a0, 0x03a0, 0x03a0, 0x0390, 0x0390, 0x0390, 0x0390,
138 0x0380, 0x0380, 0x0380, 0x0370, 0x0360, 0x0350, 0x0340, 0x0330,
139 0x0320, 0x0310, 0x0300, 0x02f0, 0x02f0, 0x02f0, 0x0300, 0x0310,
140 0x0330, 0x0350, 0x03c0, 0x0410, 0x0470, 0x04a0, 0x0460, 0x0440,
144 static s16 baptab[] = { 0, 1, 1, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6,
145 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10,
146 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12, 13, 13, 13, 13, 14,
147 14, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15 };
155 static s16 bndpsd[256];
156 static s16 excite[256];
157 static s16 mask[256];
159 static __inline__ u16 max( s16 a, s16 b )
161 return( a > b ? a : b );
164 static __inline__ u16 min( s16 a, s16 b )
166 return( a < b ? a : b );
169 static __inline__ s16 logadd( s16 a, s16 b )
173 if ( (c = a - b) >= 0 )
175 return( a + latab[min(((c) >> 1), 255)] );
179 return( b + latab[min(((-c) >> 1), 255)] );
183 static __inline__ s16 calc_lowcomp( s16 a, s16 b0, s16 b1, s16 bin )
187 if ((b0 + 256) == b1)
194 if ((b0 + 256) == b1)
205 void bit_allocate( ac3dec_thread_t * p_ac3dec )
215 /* Only perform bit_allocation if the exponents have changed or we
216 * have new sideband information */
217 if (p_ac3dec->audblk.chexpstr[0] == 0 && p_ac3dec->audblk.chexpstr[1] == 0 &&
218 p_ac3dec->audblk.chexpstr[2] == 0 && p_ac3dec->audblk.chexpstr[3] == 0 &&
219 p_ac3dec->audblk.chexpstr[4] == 0 && p_ac3dec->audblk.cplexpstr == 0 &&
220 p_ac3dec->audblk.lfeexpstr == 0 && p_ac3dec->audblk.baie == 0 &&
221 p_ac3dec->audblk.snroffste == 0 && p_ac3dec->audblk.deltbaie == 0)
224 /* Do some setup before we do the bit alloc */
225 sdecay = slowdec[p_ac3dec->audblk.sdcycod];
226 fdecay = fastdec[p_ac3dec->audblk.fdcycod];
227 sgain = slowgain[p_ac3dec->audblk.sgaincod];
228 dbknee = dbpbtab[p_ac3dec->audblk.dbpbcod];
229 floor = floortab[p_ac3dec->audblk.floorcod];
231 /* if all the SNR offset constants are zero then the whole block is zero */
232 if(!p_ac3dec->audblk.csnroffst && !p_ac3dec->audblk.fsnroffst[0] &&
233 !p_ac3dec->audblk.fsnroffst[1] && !p_ac3dec->audblk.fsnroffst[2] &&
234 !p_ac3dec->audblk.fsnroffst[3] && !p_ac3dec->audblk.fsnroffst[4] &&
235 !p_ac3dec->audblk.cplfsnroffst && !p_ac3dec->audblk.lfefsnroffst)
237 memset(p_ac3dec->audblk.fbw_bap,0,sizeof(u16) * 256 * 5);
238 memset(p_ac3dec->audblk.cpl_bap,0,sizeof(u16) * 256);
239 memset(p_ac3dec->audblk.lfe_bap,0,sizeof(u16) * 7);
243 for(i = 0; i < p_ac3dec->bsi.nfchans; i++)
246 end = p_ac3dec->audblk.endmant[i] ;
247 fgain = fastgain[p_ac3dec->audblk.fgaincod[i]];
248 snroffset = (((p_ac3dec->audblk.csnroffst - 15) << 4) + p_ac3dec->audblk.fsnroffst[i]) << 2 ;
252 ba_compute_psd(start, end, p_ac3dec->audblk.fbw_exp[i], psd, bndpsd);
254 ba_compute_excitation(start, end , fgain, fastleak, slowleak, 0, bndpsd, excite);
256 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);
258 ba_compute_bap(start, end, snroffset, psd, mask, p_ac3dec->audblk.fbw_bap[i]);
261 if(p_ac3dec->audblk.cplinu)
263 start = p_ac3dec->audblk.cplstrtmant;
264 end = p_ac3dec->audblk.cplendmant;
265 fgain = fastgain[p_ac3dec->audblk.cplfgaincod];
266 snroffset = (((p_ac3dec->audblk.csnroffst - 15) << 4) + p_ac3dec->audblk.cplfsnroffst) << 2 ;
267 fastleak = (p_ac3dec->audblk.cplfleak << 8) + 768;
268 slowleak = (p_ac3dec->audblk.cplsleak << 8) + 768;
270 ba_compute_psd(start, end, p_ac3dec->audblk.cpl_exp, psd, bndpsd);
272 ba_compute_excitation(start, end , fgain, fastleak, slowleak, 0, bndpsd, excite);
274 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);
276 ba_compute_bap(start, end, snroffset, psd, mask, p_ac3dec->audblk.cpl_bap);
279 if(p_ac3dec->bsi.lfeon)
283 fgain = fastgain[p_ac3dec->audblk.lfefgaincod];
284 snroffset = (((p_ac3dec->audblk.csnroffst - 15) << 4) + p_ac3dec->audblk.lfefsnroffst) << 2 ;
288 ba_compute_psd(start, end, p_ac3dec->audblk.lfe_exp, psd, bndpsd);
290 ba_compute_excitation(start, end , fgain, fastleak, slowleak, 1, bndpsd, excite);
292 ba_compute_mask(start, end, p_ac3dec->syncinfo.fscod, 2, 0, 0, 0, 0, excite, mask);
294 ba_compute_bap(start, end, snroffset, psd, mask, p_ac3dec->audblk.lfe_bap);
299 static void ba_compute_psd(s16 start, s16 end, s16 exps[],
300 s16 psd[], s16 bndpsd[])
305 /* Map the exponents into dBs */
306 for (bin=start; bin<end; bin++)
308 psd[bin] = (3072 - (exps[bin] << 7));
311 /* Integrate the psd function over each bit allocation band */
317 lastbin = min(bndtab[k] + bndsz[k], end);
321 for (i = j; i < lastbin; i++)
323 bndpsd[k] = logadd(bndpsd[k], psd[j]);
328 } while (end > lastbin);
331 static void ba_compute_excitation(s16 start, s16 end,s16 fgain,
332 s16 fastleak, s16 slowleak, s16 is_lfe, s16 bndpsd[],
341 /* Compute excitation function */
342 bndstrt = masktab[start];
343 bndend = masktab[end - 1] + 1;
345 if (bndstrt == 0) /* For fbw and lfe channels */
347 lowcomp = calc_lowcomp(lowcomp, bndpsd[0], bndpsd[1], 0);
348 excite[0] = bndpsd[0] - fgain - lowcomp;
349 lowcomp = calc_lowcomp(lowcomp, bndpsd[1], bndpsd[2], 1);
350 excite[1] = bndpsd[1] - fgain - lowcomp;
353 /* Note: Do not call calc_lowcomp() for the last band of the lfe channel, (bin = 6) */
354 for (bin = 2; bin < 7; bin++)
356 if (!(is_lfe && (bin == 6)))
357 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin);
358 fastleak = bndpsd[bin] - fgain;
359 slowleak = bndpsd[bin] - sgain;
360 excite[bin] = fastleak - lowcomp;
362 if (!(is_lfe && (bin == 6)))
364 if (bndpsd[bin] <= bndpsd[bin+1])
372 for (bin = begin; bin < min(bndend, 22); bin++)
374 if (!(is_lfe && (bin == 6)))
375 lowcomp = calc_lowcomp(lowcomp, bndpsd[bin], bndpsd[bin+1], bin);
377 fastleak = max(fastleak, bndpsd[bin] - fgain);
379 slowleak = max(slowleak, bndpsd[bin] - sgain);
380 excite[bin] = max(fastleak - lowcomp, slowleak);
384 else /* For coupling channel */
389 for (bin = begin; bin < bndend; bin++)
392 fastleak = max(fastleak, bndpsd[bin] - fgain);
394 slowleak = max(slowleak, bndpsd[bin] - sgain);
395 excite[bin] = max(fastleak, slowleak) ;
399 static void ba_compute_mask(s16 start, s16 end, u16 fscod,
400 u16 deltbae, u16 deltnseg, u16 deltoffst[], u16 deltba[],
401 u16 deltlen[], s16 excite[], s16 mask[])
408 bndstrt = masktab[start];
409 bndend = masktab[end - 1] + 1;
411 /* Compute the masking curve */
412 for (bin = bndstrt; bin < bndend; bin++)
414 if (bndpsd[bin] < dbknee)
416 excite[bin] += ((dbknee - bndpsd[bin]) >> 2);
418 mask[bin] = max(excite[bin], hth[fscod][bin]);
421 /* Perform delta bit modulation if necessary */
422 if ((deltbae == DELTA_BIT_REUSE) || (deltbae == DELTA_BIT_NEW))
427 for (seg = 0; seg < deltnseg+1; seg++)
429 band += deltoffst[seg];
430 if (deltba[seg] >= 4)
432 delta = (deltba[seg] - 3) << 7;
436 delta = (deltba[seg] - 4) << 7;
438 for (k = 0; k < deltlen[seg]; k++)
447 static void ba_compute_bap(s16 start, s16 end, s16 snroffset,
448 s16 psd[], s16 mask[], s16 bap[])
454 /* Compute the bit allocation pointer for each bin */
460 lastbin = min(bndtab[j] + bndsz[j], end);
461 mask[j] -= snroffset;
469 for (k = i; k < lastbin; k++)
471 address = (psd[i] - mask[j]) >> 5;
472 address = min(63, max(0, address));
473 bap[i] = baptab[address];
477 } while (end > lastbin);