用於音頻對講: 解g711u數據流和壓縮pcm的數據流的方法



//g711u2pcm
int UlawCodec_decode(int16_t *samples, int count, void *payload, int length)
{
int8_t *ulaws = (int8_t *)payload;
if (length > count) {
length = count;
}
for (int i = 0; i < length; ++i) {
int ulaw = ~ulaws[i];
int exponent = (ulaw >> 4) & 0x07;
int mantissa = ulaw & 0x0F;
int sample = (((mantissa << 3) + 132) << exponent) - 132;
samples[i] = (ulaw < 0 ? -sample : sample);
}
return length;
}

//pcm2g177u
//const int8_t gExponents[128] = {
0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
};

int UlawCodec_encode(void *out, int16_t *in, int mSampleCount)
{
int8_t *ulaws = (int8_t *)out;
for (int i = 0; i < mSampleCount; ++i) {
int sample = in[i];
int sign = (sample >> 8) & 0x80;
if (sample < 0) {
sample = -sample;
}
sample += 132;
if (sample > 32767) {
sample = 32767;
}
int exponent = gExponents[sample >> 8];
int mantissa = (sample >> (exponent + 3)) & 0x0F;
ulaws[i] = ~(sign | (exponent << 4) | mantissa);
}
return mSampleCount;
}
發佈了28 篇原創文章 · 獲贊 50 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章