// SPDX-License-Identifier: GPL-2.0-only // PJSIP (pjmedia / pjmedia-codec) G.722 and G.711 audio codec implementation // Adapted from Teluu pjproject (pjmedia/src/pjmedia-codec/g722 & pjmedia/alaw_ulaw.c) #ifndef BANANARCHY_PJSIP_MEDIA_H #define BANANARCHY_PJSIP_MEDIA_H #include #include #include #include namespace bananarchy { namespace pjsip { inline int saturate(int v, int maxVal, int minVal) { if (v > maxVal) return maxVal; if (v < minVal) return minVal; return v; } // --------------------------------------------------------------------------- // PJSIP ITU-T G.711 u-law / A-law (pjmedia/src/pjmedia/alaw_ulaw.c) // --------------------------------------------------------------------------- inline int g711Search(int val, const int16_t* table, int size) { for (int i = 0; i < size; ++i) { if (val <= table[i]) return i; } return size; } inline uint8_t linear2alaw(int pcmVal) { static constexpr int16_t kSegEnd[8] = { 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF }; int mask; if (pcmVal >= 0) { mask = 0xD5; } else { mask = 0x55; pcmVal = -pcmVal - 1; if (pcmVal < 0) pcmVal = 32767; } int seg = g711Search(pcmVal, kSegEnd, 8); if (seg >= 8) { return static_cast(0x7F ^ mask); } uint8_t aval = static_cast(seg << 4); if (seg < 2) { aval |= (pcmVal >> 4) & 0x0F; } else { aval |= (pcmVal >> (seg + 3)) & 0x0F; } return static_cast(aval ^ mask); } inline int16_t alaw2linear(uint8_t aVal) { aVal ^= 0x55; int t = (aVal & 0x0F) << 4; int seg = (static_cast(aVal) & 0x70) >> 4; switch (seg) { case 0: t += 8; break; case 1: t += 0x108; break; default: t += 0x108; t <<= seg - 1; break; } return static_cast((aVal & 0x80) ? t : -t); } inline uint8_t linear2ulaw(int pcmVal) { static constexpr int16_t kSegEnd[8] = { 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF }; constexpr int kBias = 0x84; int mask; if (pcmVal < 0) { pcmVal = kBias - pcmVal; mask = 0x7F; } else { pcmVal += kBias; mask = 0xFF; } int seg = g711Search(pcmVal, kSegEnd, 8); if (seg >= 8) { return static_cast(0x7F ^ mask); } uint8_t uval = static_cast((seg << 4) | ((pcmVal >> (seg + 3)) & 0x0F)); return static_cast(uval ^ mask); } inline int16_t ulaw2linear(uint8_t uVal) { constexpr int kBias = 0x84; uVal = static_cast(~uVal); int t = ((uVal & 0x0F) << 3) + kBias; t <<= (static_cast(uVal) & 0x70) >> 4; return static_cast((uVal & 0x80) ? (kBias - t) : (t - kBias)); } // --------------------------------------------------------------------------- // PJSIP ITU-T G.722 Wideband 16 kHz Codec (pjmedia-codec/g722) // --------------------------------------------------------------------------- inline constexpr int kG722QmfCoeff[24] = { 3, -11, -11, 53, 12, -156, 32, 362, -210, -805, 951, 3876, 3876, 951, -805, -210, 362, 32, -156, 12, 53, -11, -11, 3 }; inline constexpr int kG722Qm4[16] = { 0, -20456, -12896, -8968, -6288, -4240, -2584, -1200, 20456, 12896, 8968, 6288, 4240, 2584, 1200, 0 }; inline constexpr int kG722Ilb[32] = { 2048, 2093, 2139, 2186, 2233, 2282, 2332, 2383, 2435, 2489, 2543, 2599, 2656, 2714, 2774, 2834, 2896, 2960, 3025, 3091, 3158, 3228, 3298, 3371, 3444, 3520, 3597, 3676, 3756, 3838, 3922, 4008 }; inline constexpr int kG722Wl[8] = { -60, -30, 58, 172, 334, 538, 1198, 3042 }; inline constexpr int kG722Rl42[16] = { 0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0 }; inline constexpr int kG722Qm2[4] = { -7408, -1616, 7408, 1616 }; inline constexpr int kG722Wh[3] = { 0, -214, 798 }; inline constexpr int kG722Rh2[4] = { 2, 1, 2, 1 }; struct G722BandState { int s = 0; int det = 32; int sp = 0; int sz = 0; int r[3] {}; int a[3] {}; int ap[3] {}; int p[3] {}; int d[7] {}; int b[7] {}; int bp[7] {}; int sg[7] {}; int nb = 0; }; inline int g722Block2l(int il, int detl) { int ril = il >> 2; int wd2 = kG722Qm4[ril]; return (detl * wd2) >> 15; } inline int g722Block3l(G722BandState& band, int il) { int ril = il >> 2; int il4 = kG722Rl42[ril]; int wd = (band.nb * 32512) >> 15; int nbpl = saturate(wd + kG722Wl[il4], 18432, 0); int wd1 = (nbpl >> 6) & 31; int wd2 = nbpl >> 11; int wd3 = ((8 - wd2) < 0) ? (kG722Ilb[wd1] << (wd2 - 8)) : (kG722Ilb[wd1] >> (8 - wd2)); band.nb = nbpl; return wd3 << 2; } inline int g722Block2h(int ih, int deth) { return (deth * kG722Qm2[ih & 3]) >> 15; } inline int g722Block3h(G722BandState& band, int ih) { int ih2 = kG722Rh2[ih & 3]; int wd = (band.nb * 32512) >> 15; int nbph = saturate(wd + kG722Wh[ih2], 22528, 0); int wd1 = (nbph >> 6) & 31; int wd2 = nbph >> 11; int wd3 = ((10 - wd2) < 0) ? (kG722Ilb[wd1] << (wd2 - 10)) : (kG722Ilb[wd1] >> (10 - wd2)); band.nb = nbph; return wd3 << 2; } inline int g722Block4(G722BandState& band, int dl) { int sl = band.s; band.d[0] = dl; // RECONS band.r[0] = saturate(sl + band.d[0], 32767, -32768); // PARREC band.p[0] = saturate(band.d[0] + band.sz, 32767, -32768); // UPPOL2 band.sg[0] = band.p[0] >> 15; band.sg[1] = band.p[1] >> 15; band.sg[2] = band.p[2] >> 15; int wd1 = saturate(band.a[1] * 4, 32767, -32768); int wd2 = (band.sg[0] == band.sg[1]) ? -wd1 : wd1; if (wd2 > 32767) wd2 = 32767; wd2 = wd2 >> 7; int wd3 = (band.sg[0] == band.sg[2]) ? 128 : -128; int wd4 = wd2 + wd3; int wd5 = (band.a[2] * 32512) >> 15; band.ap[2] = saturate(wd4 + wd5, 12288, -12288); // UPPOL1 band.sg[0] = band.p[0] >> 15; band.sg[1] = band.p[1] >> 15; wd1 = (band.sg[0] == band.sg[1]) ? 192 : -192; wd2 = (band.a[1] * 32640) >> 15; band.ap[1] = saturate(wd1 + wd2, 32767, -32768); wd3 = saturate(15360 - band.ap[2], 32767, -32768); if (band.ap[1] > wd3) band.ap[1] = wd3; else if (band.ap[1] < -wd3) band.ap[1] = -wd3; // UPZERO wd1 = (band.d[0] == 0) ? 0 : 128; band.sg[0] = band.d[0] >> 15; for (int i = 1; i < 7; ++i) { band.sg[i] = band.d[i] >> 15; wd2 = (band.sg[i] == band.sg[0]) ? wd1 : -wd1; wd3 = (band.b[i] * 32640) >> 15; band.bp[i] = saturate(wd2 + wd3, 32767, -32768); } // DELAYA for (int i = 6; i > 0; --i) { band.d[i] = band.d[i - 1]; band.b[i] = band.bp[i]; } for (int i = 2; i > 0; --i) { band.r[i] = band.r[i - 1]; band.p[i] = band.p[i - 1]; band.a[i] = band.ap[i]; } // FILTEP wd1 = saturate(band.r[1] + band.r[1], 32767, -32768); wd1 = (band.a[1] * wd1) >> 15; wd2 = saturate(band.r[2] + band.r[2], 32767, -32768); wd2 = (band.a[2] * wd2) >> 15; band.sp = saturate(wd1 + wd2, 32767, -32768); // FILTEZ band.sz = 0; for (int i = 6; i > 0; --i) { int wd = saturate(band.d[i] + band.d[i], 32767, -32768); band.sz += (band.b[i] * wd) >> 15; band.sz = saturate(band.sz, 32767, -32768); } // PREDIC sl = saturate(band.sp + band.sz, 32767, -32768); return sl; } class G722Codec { public: G722Codec() { reset(); } void reset() { encLow_ = G722BandState{}; encHigh_ = G722BandState{}; encLow_.det = 32; encHigh_.det = 8; std::memset(txX_, 0, sizeof(txX_)); decLow_ = G722BandState{}; decHigh_ = G722BandState{}; decLow_.det = 32; decHigh_.det = 8; std::memset(rxXd_, 0, sizeof(rxXd_)); std::memset(rxXs_, 0, sizeof(rxXs_)); } size_t encodeFrame(const int16_t* pcm16k, size_t numSamples, uint8_t* outBuf) { static constexpr int kQ6[32] = { 0, 35, 72, 110, 150, 190, 233, 276, 323, 370, 422, 473, 530, 587, 650, 714, 786, 858, 940, 1023, 1121, 1219, 1339, 1458, 1612, 1765, 1980, 2195, 2557, 2919, 0, 0 }; static constexpr int kIln[32] = { 0, 63, 62, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 0 }; static constexpr int kIlp[32] = { 0, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 0 }; static constexpr int kIhn[3] = { 0, 1, 0 }; static constexpr int kIhp[3] = { 0, 3, 2 }; size_t outIdx = 0; for (size_t i = 0; i + 1 < numSamples; i += 2) { // tx_qmf (16-bit PCM input -> 14-bit sub-band scale via >> 14) std::memmove(&txX_[2], txX_, 22 * sizeof(txX_[0])); txX_[1] = pcm16k[i]; txX_[0] = pcm16k[i + 1]; int sumOdd = 0; for (int j = 1; j < 24; j += 2) sumOdd += txX_[j] * kG722QmfCoeff[j]; int sumEven = 0; for (int j = 0; j < 24; j += 2) sumEven += txX_[j] * kG722QmfCoeff[j]; int xlow = saturate((sumEven + sumOdd) >> 14, 16383, -16384); int xhigh = saturate((sumEven - sumOdd) >> 14, 16383, -16384); // block1l int el = saturate(xlow - encLow_.s, 32767, -32768); int sil = el >> 15; int wd = (sil == 0) ? el : ((32767 - el) & 32767); int mil = 1; for (int j = 1; j < 30; ++j) { int hdu = (kQ6[j] << 3) * encLow_.det; int wd1 = hdu >> 15; if (wd >= wd1) mil = j + 1; else break; } int ilow = (sil == -1) ? kIln[mil] : kIlp[mil]; int dlowt = g722Block2l(ilow, encLow_.det); encLow_.det = g722Block3l(encLow_, ilow); encLow_.s = g722Block4(encLow_, dlowt); // block1h int eh = saturate(xhigh - encHigh_.s, 32767, -32768); int sih = eh >> 15; int wdh = (sih == 0) ? eh : ((32767 - eh) & 32767); int hduh = (564 << 3) * encHigh_.det; int mih = (wdh >= (hduh >> 15)) ? 2 : 1; int ihigh = (sih == -1) ? kIhn[mih] : kIhp[mih]; int dhigh = g722Block2h(ihigh, encHigh_.det); encHigh_.det = g722Block3h(encHigh_, ihigh); encHigh_.s = g722Block4(encHigh_, dhigh); outBuf[outIdx++] = static_cast(((ihigh & 0x03) << 6) | (ilow & 0x3F)); } return outIdx; } size_t decodeFrame(const uint8_t* inBuf, size_t inLen, int16_t* outPcm16k, size_t maxSamples) { static constexpr int kQm6[64] = { -136, -136, -136, -136, -24808, -21904, -19008, -16704, -14984, -13512, -12280, -11192, -10232, -9360, -8576, -7856, -7192, -6576, -6000, -5456, -4944, -4464, -4008, -3576, -3168, -2776, -2400, -2032, -1688, -1360, -1040, -728, 24808, 21904, 19008, 16704, 14984, 13512, 12280, 11192, 10232, 9360, 8576, 7856, 7192, 6576, 6000, 5456, 4944, 4464, 4008, 3576, 3168, 2776, 2400, 2032, 1688, 1360, 1040, 728, 432, 136, -432, -136 }; size_t outCount = 0; for (size_t i = 0; i < inLen && outCount + 1 < maxSamples; ++i) { int ilowr = inBuf[i] & 63; int ihigh = (inBuf[i] >> 6) & 3; // Low band decoder (block5l + block6l + block2l + block3l + block4l) int dl = (decLow_.det * kQm6[ilowr]) >> 15; int ylow = saturate(decLow_.s + dl, 32767, -32768); int rlow = saturate(ylow, 16383, -16384); int dlowt = g722Block2l(ilowr, decLow_.det); decLow_.det = g722Block3l(decLow_, ilowr); decLow_.s = g722Block4(decLow_, dlowt); // High band decoder (block2h + block5h + block3h + block4h) int dhigh = g722Block2h(ihigh, decHigh_.det); int rhigh = saturate(dhigh + decHigh_.s, 16383, -16384); decHigh_.det = g722Block3h(decHigh_, ihigh); decHigh_.s = g722Block4(decHigh_, dhigh); // rx_qmf (14-bit sub-band -> 16-bit full-scale PCM via >> 11) std::memmove(&rxXd_[1], rxXd_, 11 * sizeof(rxXd_[0])); std::memmove(&rxXs_[1], rxXs_, 11 * sizeof(rxXs_[0])); rxXd_[0] = saturate(rlow - rhigh, 16383, -16384); rxXs_[0] = saturate(rlow + rhigh, 16383, -16384); int xout1 = 0; int xout2 = 0; for (int j = 0; j < 12; ++j) { xout1 += rxXd_[j] * kG722QmfCoeff[2 * j]; xout2 += rxXs_[j] * kG722QmfCoeff[2 * j + 1]; } outPcm16k[outCount++] = static_cast(saturate(xout1 >> 11, 32767, -32768)); outPcm16k[outCount++] = static_cast(saturate(xout2 >> 11, 32767, -32768)); } return outCount; } private: G722BandState encLow_; G722BandState encHigh_; int txX_[24] {}; G722BandState decLow_; G722BandState decHigh_; int rxXd_[12] {}; int rxXs_[12] {}; }; } // namespace pjsip } // namespace bananarchy #endif // BANANARCHY_PJSIP_MEDIA_H