mpa_parser.cpp
来自「ac3的解码程序」· C++ 代码 · 共 654 行 · 第 1/2 页
CPP
654 行
switch (scfsi[ch][sb])
{
case 0 : // all three scale factors transmitted
scale[ch][0][sb] = scale_tbl[bs.get(6)] * c;
scale[ch][1][sb] = scale_tbl[bs.get(6)] * c;
scale[ch][2][sb] = scale_tbl[bs.get(6)] * c;
break;
case 1 : // scale factor 1 & 3 transmitted
scale[ch][0][sb] =
scale[ch][1][sb] = scale_tbl[bs.get(6)] * c;
scale[ch][2][sb] = scale_tbl[bs.get(6)] * c;
break;
case 3 : // scale factor 1 & 2 transmitted
scale[ch][0][sb] = scale_tbl[bs.get(6)] * c;
scale[ch][1][sb] =
scale[ch][2][sb] = scale_tbl[bs.get(6)] * c;
break;
case 2 : // only one scale factor transmitted
scale[ch][0][sb] =
scale[ch][1][sb] =
scale[ch][2][sb] = scale_tbl[bs.get(6)] * c;
break;
default : break;
}
}
else
{
scale[ch][0][sb] = scale[ch][1][sb] =
scale[ch][2][sb] = 0;
}
}
// do we need this?
for (sb = sblimit; sb < SBLIMIT; sb++)
for (ch = 0; ch < nch; ch++)
scale[ch][0][sb] = scale[ch][1][sb] =
scale[ch][2][sb] = 0;
/////////////////////////////////////////////////////////
// Decode fraction and synthesis
sample_t *sptr[MPA_NCH];
for (int i = 0; i < SCALE_BLOCK; i++)
{
sptr[0] = &samples[0][i * SBLIMIT * 3];
sptr[1] = &samples[1][i * SBLIMIT * 3];
II_decode_fraction(sptr, bit_alloc, scale, i >> 2);
for (ch = 0; ch < nch; ch++)
{
synth[ch]->synth(&samples[ch][i * SBLIMIT * 3 ]);
synth[ch]->synth(&samples[ch][i * SBLIMIT * 3 + 1 * SBLIMIT]);
synth[ch]->synth(&samples[ch][i * SBLIMIT * 3 + 2 * SBLIMIT]);
}
}
return true;
}
void
MPAParser::II_decode_fraction(
sample_t *fraction[MPA_NCH],
int16_t bit_alloc[MPA_NCH][SBLIMIT],
sample_t scale[MPA_NCH][3][SBLIMIT],
int x)
{
int sb, ch;
int nch = bsi.nch;
int sblimit = bsi.sblimit;
int jsbound = bsi.jsbound;
uint16_t s0, s1, s2;
int16_t d;
int16_t ba; // signed!
for (sb = 0; sb < sblimit; sb++)
for (ch = 0; ch < ((sb < jsbound)? nch: 1 ); ch++)
{
// ba means number of bits to read;
// negative numbers mean sample triplets
ba = bit_alloc[ch][sb];
if (ba)
{
if (ba > 0)
{
d = d_tbl[ba]; // ba > 0 => ba = quant
s0 = (uint16_t) bs.get(ba);
s1 = (uint16_t) bs.get(ba);
s2 = (uint16_t) bs.get(ba);
ba = 16 - ba; // number of bits we should shift
}
else // nlevels = 3, 5, 9; ba = -5, -7, -10
{
// packed triplet of samples
ba = -ba;
unsigned int pack = (unsigned int) bs.get(ba);
switch (ba)
{
case 5:
s0 = pack % 3; pack /= 3;
s1 = pack % 3; pack /= 3;
s2 = pack % 3;
d = d_tbl[0];
ba = 14;
break;
case 7:
s0 = pack % 5; pack /= 5;
s1 = pack % 5; pack /= 5;
s2 = pack % 5;
d = d_tbl[1];
ba = 13;
break;
case 10:
s0 = pack % 9; pack /= 9;
s1 = pack % 9; pack /= 9;
s2 = pack % 9;
d = d_tbl[2];
ba = 12;
break;
}
} // if (ba > 0) .. else ..
#define dequantize(r, s, d, bits) \
{ \
s = ((unsigned short) s) << bits; \
s = (s & 0x7fff) | (~s & 0x8000); \
r = (sample_t)((short)(s) + d) * scale[ch][x][sb]; \
}
#define dequantize2(r1, r2, s, d, bits) \
{ \
s = ((unsigned short) s) << bits; \
s = (s & 0x7fff) | (~s & 0x8000); \
sample_t f = sample_t((short)(s) + d); \
r1 = f * scale[0][x][sb]; \
r2 = f * scale[1][x][sb]; \
}
if (nch > 1 && sb >= jsbound)
{
dequantize2(fraction[0][sb ], fraction[1][sb ], s0, d, ba);
dequantize2(fraction[0][sb + 1 * SBLIMIT], fraction[1][sb + 1 * SBLIMIT], s1, d, ba);
dequantize2(fraction[0][sb + 2 * SBLIMIT], fraction[1][sb + 2 * SBLIMIT], s2, d, ba);
}
else
{
dequantize(fraction[ch][sb ], s0, d, ba);
dequantize(fraction[ch][sb + 1 * SBLIMIT], s1, d, ba);
dequantize(fraction[ch][sb + 2 * SBLIMIT], s2, d, ba);
}
}
else // ba = 0; no sample transmitted
{
fraction[ch][sb ] = 0.0;
fraction[ch][sb + 1 * SBLIMIT] = 0.0;
fraction[ch][sb + 2 * SBLIMIT] = 0.0;
if (nch > 1 && sb >= jsbound)
{
fraction[1][sb ] = 0.0;
fraction[1][sb + 1 * SBLIMIT] = 0.0;
fraction[1][sb + 2 * SBLIMIT] = 0.0;
}
} // if (ba) ... else ...
} // for (ch = 0; ch < ((sb < jsbound)? nch: 1 ); ch++)
// for (sb = 0; sb < sblimit; sb++)
for (ch = 0; ch < nch; ch++)
for (sb = sblimit; sb < SBLIMIT; sb++)
{
fraction[ch][sb ] = 0.0;
fraction[ch][sb + SBLIMIT] = 0.0;
fraction[ch][sb + 2 * SBLIMIT] = 0.0;
}
}
///////////////////////////////////////////////////////////////////////////////
// Layer I
///////////////////////////////////////////////////////////////////////////////
bool
MPAParser::I_decode_frame()
{
int ch, sb;
int nch = bsi.nch;
int jsbound = bsi.jsbound;
int16_t bit_alloc[MPA_NCH][SBLIMIT];
sample_t scale[MPA_NCH][SBLIMIT];
/////////////////////////////////////////////////////////
// CRC check
// Note that we include CRC word into processing AFTER
// protected data. Due to CRC properties we must get zero
// result in case of no errors.
/*
if (hdr.error_protection && do_crc)
{
uint32_t crc_bits = (jsbound << 3) + ((32 - jsbound) << 2);
uint32_t crc = crc16.crc_init(0xffff);
crc = crc16.calc_bits(crc, frame + 2, 0, 16, bs_type); // header
crc = crc16.calc_bits(crc, frame + 6, 0, crc_bits, bs_type); // frame data
crc = crc16.calc_bits(crc, frame + 4, 0, 16, bs_type); // crc
if (crc)
return false;
}
*/
/////////////////////////////////////////////////////////
// Load bitalloc
for (sb = 0; sb < jsbound; sb++)
for (ch = 0; ch < nch; ch++)
bit_alloc[ch][sb] = bs.get(4);
for (sb = jsbound; sb < SBLIMIT; sb++)
bit_alloc[0][sb] = bit_alloc[1][sb] = bs.get(4);
/////////////////////////////////////////////////////////
// Load scale
for (sb = 0; sb < SBLIMIT; sb++)
for (ch = 0; ch < nch; ch++)
if (bit_alloc[ch][sb])
scale[ch][sb] = scale_tbl[bs.get(6)]; // 6 bit per scale factor
else
scale[ch][sb] = 0;
/////////////////////////////////////////////////////////
// Decode fraction and synthesis
sample_t *sptr[2];
for (int i = 0; i < SCALE_BLOCK * SBLIMIT; i += SBLIMIT)
{
sptr[0] = &samples[0][i];
sptr[1] = &samples[1][i];
I_decode_fraction(sptr, bit_alloc, scale);
for (ch = 0; ch < nch; ch++)
synth[ch]->synth(&samples[ch][i]);
}
return true;
}
void
MPAParser::I_decode_fraction(
sample_t *fraction[MPA_NCH],
int16_t bit_alloc[MPA_NCH][SBLIMIT],
sample_t scale[MPA_NCH][SBLIMIT])
{
int sb, ch;
int nch = bsi.nch;
int jsbound = bsi.jsbound;
int sample[MPA_NCH][SBLIMIT];
int ba;
/////////////////////////////////////////////////////////
// buffer samples
for (sb = 0; sb < jsbound; sb++)
for (ch = 0; ch < nch; ch++)
{
ba = bit_alloc[ch][sb];
if (ba)
sample[ch][sb] = (unsigned int) bs.get(ba + 1);
else
sample[ch][sb] = 0;
}
for (sb = jsbound; sb < SBLIMIT; sb++)
{
ba = bit_alloc[0][sb];
int s;
if (ba)
s = (unsigned int) bs.get(ba + 1);
else
s = 0;
for (ch = 0; ch < nch; ch++)
sample[ch][sb] = s;
}
/////////////////////////////////////////////////////////
// Dequantize
for (sb = 0; sb < SBLIMIT; sb++)
for (ch = 0; ch < nch; ch++)
if (bit_alloc[ch][sb])
{
ba = bit_alloc[ch][sb] + 1;
if (((sample[ch][sb] >> (ba-1)) & 1) == 1)
fraction[ch][sb] = 0.0;
else
fraction[ch][sb] = -1.0;
fraction[ch][sb] += (sample_t) (sample[ch][sb] & ((1<<(ba-1))-1)) /
(sample_t) (1L<<(ba-1));
fraction[ch][sb] =
(sample_t) (fraction[ch][sb]+1.0/(sample_t)(1L<<(ba-1))) *
(sample_t) (1L<<ba) / (sample_t) ((1L<<ba)-1);
}
else
fraction[ch][sb] = 0.0;
/////////////////////////////////////////////////////////
// Denormalize
for (ch = 0; ch < nch; ch++)
for (sb = 0; sb < SBLIMIT; sb++)
fraction[ch][sb] *= scale[ch][sb];
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?