diff --git a/daemon/src/audio/codecs/g722.cpp b/daemon/src/audio/codecs/g722.cpp index 4d1a1333714c913a9a1d9cbd6ecd8475ac7e1b6d..fa6c88fabd3ae0960dfd2478b29d442428258589 100644 --- a/daemon/src/audio/codecs/g722.cpp +++ b/daemon/src/audio/codecs/g722.cpp @@ -33,335 +33,293 @@ #include "audiocodec.h" #include "sfl_types.h" #include "g722.h" -#include "noncopyable.h" -#include <stdlib.h> -#include <string.h> +#include <cstdlib> +#include <cstring> #include <cassert> class G722 : public sfl::AudioCodec { public: - G722(int payload=9) : sfl::AudioCodec(payload, "G722"), - decode_s(new g722_decode_state_t), - encode_s(new g722_encode_state_t) { + G722(int payload = 9) : sfl::AudioCodec(payload, "G722"), decode_state_(), encode_state_() { clockRate_ = 16000; frameSize_ = 320; // samples, 20 ms at 16kHz channel_ = 1; bitrate_ = 64; hasDynamicPayload_ = false; - g722_decode_init(); - g722_encode_init(); + g722_state_init(decode_state_); + g722_state_init(encode_state_); } - ~G722() { - g722_decode_release(); - g722_encode_release(); - } - - virtual int decode(short *dst, unsigned char *src, size_t buf_size) { - assert(buf_size == frameSize_ / sizeof(SFLDataFormat) * encode_s->bits_per_sample / 8); - return g722_decode((int16_t*) dst, (const uint8_t*) src, buf_size); + private: + virtual int decode(SFLDataFormat *dst, unsigned char *src, size_t buf_size) + { + assert(buf_size == frameSize_ / sizeof(SFLDataFormat) * encode_state_.bits_per_sample / 8); + return g722_decode(dst, (const uint8_t*) src, buf_size); } - virtual int encode(unsigned char *dst, short *src, size_t buf_size) { - int out = g722_encode((uint8_t*) dst, (const int16_t*) src, frameSize_); - assert((size_t)out <= buf_size); + virtual int encode(unsigned char *dst, SFLDataFormat *src, size_t buf_size) + { + int out = g722_encode((uint8_t*) dst, src, frameSize_); + assert((size_t) out <= buf_size); return out; } - - void g722_encode_init() { - encode_s->itu_test_mode = false; - - // 8 => 64 kbps; 7 => 56 kbps; 6 => 48 kbps - encode_s->bits_per_sample = 8; - - // Enable 8khz mode, encode using lower subband only - encode_s->eight_k = false; - - // Never set packed true when using 64 kbps - encode_s->packed = false; - - memset(encode_s->band, 0, sizeof(decode_s->band)); - encode_s->band[0].det = 32; - encode_s->band[1].det = 8; - - memset(encode_s->x, 0, sizeof(encode_s->x)); - - decode_s->in_buffer = 0; - decode_s->in_bits = 0; - decode_s->out_buffer = 0; - decode_s->out_bits = 0; - } - - void g722_decode_init() { - - decode_s->itu_test_mode = false; + static void g722_state_init(g722_state_t &state) + { + state.itu_test_mode = false; // 8 => 64 kbps; 7 => 56 kbps; 6 => 48 kbps - decode_s->bits_per_sample = 8; + state.bits_per_sample = 8; // Enable 8khz mode, encode using lower subband only - decode_s->eight_k = false; + state.eight_k = false; // Never set packed true when using 64 kbps - decode_s->packed = false; - - memset(decode_s->band, 0, sizeof(decode_s->band)); - decode_s->band[0].det = 32; - decode_s->band[1].det = 8; + state.packed = false; - decode_s->in_bits = 0; + memset(state.band, 0, sizeof(state.band)); + state.band[0].det = 32; + state.band[1].det = 8; - memset(decode_s->x, 0, sizeof(decode_s->x)); + memset(state.x, 0, sizeof(state.x)); - decode_s->in_buffer = 0; - decode_s->in_bits = 0; - decode_s->out_buffer = 0; - decode_s->out_bits = 0; + state.in_buffer = 0; + state.in_bits = 0; + state.out_buffer = 0; + state.out_bits = 0; } - int16_t saturate(int32_t amp) { - int16_t amp16 = 0; + SFLDataFormat saturate(int32_t amp) + { + SFLDataFormat amp16 = 0; /* Hopefully this is optimised for the common case - not clipping */ - amp16 = (int16_t) amp; + amp16 = (SFLDataFormat) amp; if (amp == amp16) return amp16; if (amp > INT16_MAX) - return INT16_MAX; + return INT16_MAX; - return INT16_MIN; + return INT16_MIN; } - - void block4_encode(int band, int d) { + void block4_encode(int band, int d) + { int wd1 = 0; int wd2 = 0; int wd3 = 0; int i = 0; /* Block 4, RECONS */ - encode_s->band[band].d[0] = d; - encode_s->band[band].r[0] = saturate(encode_s->band[band].s + d); + encode_state_.band[band].d[0] = d; + encode_state_.band[band].r[0] = saturate(encode_state_.band[band].s + d); /* Block 4, PARREC */ - encode_s->band[band].p[0] = saturate(encode_s->band[band].sz + d); + encode_state_.band[band].p[0] = saturate(encode_state_.band[band].sz + d); /* Block 4, UPPOL2 */ for (i = 0; i < 3; i++) - encode_s->band[band].sg[i] = encode_s->band[band].p[i] >> 15; + encode_state_.band[band].sg[i] = encode_state_.band[band].p[i] >> 15; - wd1 = saturate(encode_s->band[band].a[1] << 2); + wd1 = saturate(encode_state_.band[band].a[1] << 2); - wd2 = (encode_s->band[band].sg[0] == encode_s->band[band].sg[1]) ? -wd1 : wd1; + wd2 = (encode_state_.band[band].sg[0] == encode_state_.band[band].sg[1]) ? -wd1 : wd1; if (wd2 > 32767) wd2 = 32767; - wd3 = (wd2 >> 7) + ((encode_s->band[band].sg[0] == encode_s->band[band].sg[2]) ? 128 : -128); + wd3 = (wd2 >> 7) + ((encode_state_.band[band].sg[0] == encode_state_.band[band].sg[2]) ? 128 : -128); - wd3 += (encode_s->band[band].a[2]*32512) >> 15; + wd3 += (encode_state_.band[band].a[2]*32512) >> 15; if (wd3 > 12288) wd3 = 12288; else if (wd3 < -12288) wd3 = -12288; - encode_s->band[band].ap[2] = wd3; + encode_state_.band[band].ap[2] = wd3; /* Block 4, UPPOL1 */ - encode_s->band[band].sg[0] = encode_s->band[band].p[0] >> 15; + encode_state_.band[band].sg[0] = encode_state_.band[band].p[0] >> 15; - encode_s->band[band].sg[1] = encode_s->band[band].p[1] >> 15; + encode_state_.band[band].sg[1] = encode_state_.band[band].p[1] >> 15; - wd1 = (encode_s->band[band].sg[0] == encode_s->band[band].sg[1]) ? 192 : -192; + wd1 = (encode_state_.band[band].sg[0] == encode_state_.band[band].sg[1]) ? 192 : -192; - wd2 = (encode_s->band[band].a[1]*32640) >> 15; + wd2 = (encode_state_.band[band].a[1]*32640) >> 15; - encode_s->band[band].ap[1] = saturate(wd1 + wd2); + encode_state_.band[band].ap[1] = saturate(wd1 + wd2); - wd3 = saturate(15360 - encode_s->band[band].ap[2]); + wd3 = saturate(15360 - encode_state_.band[band].ap[2]); - if (encode_s->band[band].ap[1] > wd3) - encode_s->band[band].ap[1] = wd3; - else if (encode_s->band[band].ap[1] < -wd3) - encode_s->band[band].ap[1] = -wd3; + if (encode_state_.band[band].ap[1] > wd3) + encode_state_.band[band].ap[1] = wd3; + else if (encode_state_.band[band].ap[1] < -wd3) + encode_state_.band[band].ap[1] = -wd3; /* Block 4, UPZERO */ wd1 = (d == 0) ? 0 : 128; - encode_s->band[band].sg[0] = d >> 15; + encode_state_.band[band].sg[0] = d >> 15; for (i = 1; i < 7; i++) { - encode_s->band[band].sg[i] = encode_s->band[band].d[i] >> 15; - wd2 = (encode_s->band[band].sg[i] == encode_s->band[band].sg[0]) ? wd1 : -wd1; - wd3 = (encode_s->band[band].b[i]*32640) >> 15; - encode_s->band[band].bp[i] = saturate(wd2 + wd3); + encode_state_.band[band].sg[i] = encode_state_.band[band].d[i] >> 15; + wd2 = (encode_state_.band[band].sg[i] == encode_state_.band[band].sg[0]) ? wd1 : -wd1; + wd3 = (encode_state_.band[band].b[i]*32640) >> 15; + encode_state_.band[band].bp[i] = saturate(wd2 + wd3); } /* Block 4, DELAYA */ for (i = 6; i > 0; i--) { - encode_s->band[band].d[i] = encode_s->band[band].d[i - 1]; - encode_s->band[band].b[i] = encode_s->band[band].bp[i]; + encode_state_.band[band].d[i] = encode_state_.band[band].d[i - 1]; + encode_state_.band[band].b[i] = encode_state_.band[band].bp[i]; } for (i = 2; i > 0; i--) { - encode_s->band[band].r[i] = encode_s->band[band].r[i - 1]; - encode_s->band[band].p[i] = encode_s->band[band].p[i - 1]; - encode_s->band[band].a[i] = encode_s->band[band].ap[i]; + encode_state_.band[band].r[i] = encode_state_.band[band].r[i - 1]; + encode_state_.band[band].p[i] = encode_state_.band[band].p[i - 1]; + encode_state_.band[band].a[i] = encode_state_.band[band].ap[i]; } /* Block 4, FILTEP */ - wd1 = saturate(encode_s->band[band].r[1] + encode_s->band[band].r[1]); + wd1 = saturate(encode_state_.band[band].r[1] + encode_state_.band[band].r[1]); - wd1 = (encode_s->band[band].a[1]*wd1) >> 15; + wd1 = (encode_state_.band[band].a[1]*wd1) >> 15; - wd2 = saturate(encode_s->band[band].r[2] + encode_s->band[band].r[2]); + wd2 = saturate(encode_state_.band[band].r[2] + encode_state_.band[band].r[2]); - wd2 = (encode_s->band[band].a[2]*wd2) >> 15; + wd2 = (encode_state_.band[band].a[2]*wd2) >> 15; - encode_s->band[band].sp = saturate(wd1 + wd2); + encode_state_.band[band].sp = saturate(wd1 + wd2); /* Block 4, FILTEZ */ - encode_s->band[band].sz = 0; + encode_state_.band[band].sz = 0; for (i = 6; i > 0; i--) { - wd1 = saturate(encode_s->band[band].d[i] + encode_s->band[band].d[i]); - encode_s->band[band].sz += (encode_s->band[band].b[i]*wd1) >> 15; + wd1 = saturate(encode_state_.band[band].d[i] + encode_state_.band[band].d[i]); + encode_state_.band[band].sz += (encode_state_.band[band].b[i]*wd1) >> 15; } - encode_s->band[band].sz = saturate(encode_s->band[band].sz); + encode_state_.band[band].sz = saturate(encode_state_.band[band].sz); /* Block 4, PREDIC */ - encode_s->band[band].s = saturate(encode_s->band[band].sp + encode_s->band[band].sz); + encode_state_.band[band].s = saturate(encode_state_.band[band].sp + encode_state_.band[band].sz); } - void block4_decode(int band, int d) { + void block4_decode(int band, int d) + { int wd1 = 0; int wd2 = 0; int wd3 = 0; int i = 0; /* Block 4, RECONS */ - decode_s->band[band].d[0] = d; - decode_s->band[band].r[0] = saturate(decode_s->band[band].s + d); + decode_state_.band[band].d[0] = d; + decode_state_.band[band].r[0] = saturate(decode_state_.band[band].s + d); /* Block 4, PARREC */ - decode_s->band[band].p[0] = saturate(decode_s->band[band].sz + d); + decode_state_.band[band].p[0] = saturate(decode_state_.band[band].sz + d); /* Block 4, UPPOL2 */ for (i = 0; i < 3; i++) - decode_s->band[band].sg[i] = decode_s->band[band].p[i] >> 15; + decode_state_.band[band].sg[i] = decode_state_.band[band].p[i] >> 15; - wd1 = saturate(decode_s->band[band].a[1] << 2); + wd1 = saturate(decode_state_.band[band].a[1] << 2); - wd2 = (decode_s->band[band].sg[0] == decode_s->band[band].sg[1]) ? -wd1 : wd1; + wd2 = (decode_state_.band[band].sg[0] == decode_state_.band[band].sg[1]) ? -wd1 : wd1; if (wd2 > 32767) wd2 = 32767; - wd3 = (decode_s->band[band].sg[0] == decode_s->band[band].sg[2]) ? 128 : -128; + wd3 = (decode_state_.band[band].sg[0] == decode_state_.band[band].sg[2]) ? 128 : -128; wd3 += (wd2 >> 7); - wd3 += (decode_s->band[band].a[2]*32512) >> 15; + wd3 += (decode_state_.band[band].a[2]*32512) >> 15; if (wd3 > 12288) wd3 = 12288; else if (wd3 < -12288) wd3 = -12288; - decode_s->band[band].ap[2] = wd3; + decode_state_.band[band].ap[2] = wd3; /* Block 4, UPPOL1 */ - decode_s->band[band].sg[0] = decode_s->band[band].p[0] >> 15; + decode_state_.band[band].sg[0] = decode_state_.band[band].p[0] >> 15; - decode_s->band[band].sg[1] = decode_s->band[band].p[1] >> 15; + decode_state_.band[band].sg[1] = decode_state_.band[band].p[1] >> 15; - wd1 = (decode_s->band[band].sg[0] == decode_s->band[band].sg[1]) ? 192 : -192; + wd1 = (decode_state_.band[band].sg[0] == decode_state_.band[band].sg[1]) ? 192 : -192; - wd2 = (decode_s->band[band].a[1]*32640) >> 15; + wd2 = (decode_state_.band[band].a[1]*32640) >> 15; - decode_s->band[band].ap[1] = saturate(wd1 + wd2); + decode_state_.band[band].ap[1] = saturate(wd1 + wd2); - wd3 = saturate(15360 - decode_s->band[band].ap[2]); + wd3 = saturate(15360 - decode_state_.band[band].ap[2]); - if (decode_s->band[band].ap[1] > wd3) - decode_s->band[band].ap[1] = wd3; - else if (decode_s->band[band].ap[1] < -wd3) - decode_s->band[band].ap[1] = -wd3; + if (decode_state_.band[band].ap[1] > wd3) + decode_state_.band[band].ap[1] = wd3; + else if (decode_state_.band[band].ap[1] < -wd3) + decode_state_.band[band].ap[1] = -wd3; /* Block 4, UPZERO */ wd1 = (d == 0) ? 0 : 128; - decode_s->band[band].sg[0] = d >> 15; + decode_state_.band[band].sg[0] = d >> 15; for (i = 1; i < 7; i++) { - decode_s->band[band].sg[i] = decode_s->band[band].d[i] >> 15; - wd2 = (decode_s->band[band].sg[i] == decode_s->band[band].sg[0]) ? wd1 : -wd1; - wd3 = (decode_s->band[band].b[i]*32640) >> 15; - decode_s->band[band].bp[i] = saturate(wd2 + wd3); + decode_state_.band[band].sg[i] = decode_state_.band[band].d[i] >> 15; + wd2 = (decode_state_.band[band].sg[i] == decode_state_.band[band].sg[0]) ? wd1 : -wd1; + wd3 = (decode_state_.band[band].b[i]*32640) >> 15; + decode_state_.band[band].bp[i] = saturate(wd2 + wd3); } /* Block 4, DELAYA */ for (i = 6; i > 0; i--) { - decode_s->band[band].d[i] = decode_s->band[band].d[i - 1]; - decode_s->band[band].b[i] = decode_s->band[band].bp[i]; + decode_state_.band[band].d[i] = decode_state_.band[band].d[i - 1]; + decode_state_.band[band].b[i] = decode_state_.band[band].bp[i]; } for (i = 2; i > 0; i--) { - decode_s->band[band].r[i] = decode_s->band[band].r[i - 1]; - decode_s->band[band].p[i] = decode_s->band[band].p[i - 1]; - decode_s->band[band].a[i] = decode_s->band[band].ap[i]; + decode_state_.band[band].r[i] = decode_state_.band[band].r[i - 1]; + decode_state_.band[band].p[i] = decode_state_.band[band].p[i - 1]; + decode_state_.band[band].a[i] = decode_state_.band[band].ap[i]; } /* Block 4, FILTEP */ - wd1 = saturate(decode_s->band[band].r[1] + decode_s->band[band].r[1]); + wd1 = saturate(decode_state_.band[band].r[1] + decode_state_.band[band].r[1]); - wd1 = (decode_s->band[band].a[1]*wd1) >> 15; + wd1 = (decode_state_.band[band].a[1]*wd1) >> 15; - wd2 = saturate(decode_s->band[band].r[2] + decode_s->band[band].r[2]); + wd2 = saturate(decode_state_.band[band].r[2] + decode_state_.band[band].r[2]); - wd2 = (decode_s->band[band].a[2]*wd2) >> 15; + wd2 = (decode_state_.band[band].a[2]*wd2) >> 15; - decode_s->band[band].sp = saturate(wd1 + wd2); + decode_state_.band[band].sp = saturate(wd1 + wd2); /* Block 4, FILTEZ */ - decode_s->band[band].sz = 0; + decode_state_.band[band].sz = 0; for (i = 6; i > 0; i--) { - wd1 = saturate(decode_s->band[band].d[i] + decode_s->band[band].d[i]); - decode_s->band[band].sz += (decode_s->band[band].b[i]*wd1) >> 15; + wd1 = saturate(decode_state_.band[band].d[i] + decode_state_.band[band].d[i]); + decode_state_.band[band].sz += (decode_state_.band[band].b[i]*wd1) >> 15; } - decode_s->band[band].sz = saturate(decode_s->band[band].sz); + decode_state_.band[band].sz = saturate(decode_state_.band[band].sz); /* Block 4, PREDIC */ - decode_s->band[band].s = saturate(decode_s->band[band].sp + decode_s->band[band].sz); - } - - int g722_encode_release() { - delete decode_s; - decode_s = NULL; - return 0; + decode_state_.band[band].s = saturate(decode_state_.band[band].sp + decode_state_.band[band].sz); } - - int g722_decode_release() { - delete encode_s; - encode_s = NULL; - return 0; - } - - int g722_decode(int16_t amp[], const uint8_t g722_data[], int len) { + int g722_decode(SFLDataFormat amp[], const uint8_t g722_data[], int len) + { static const int wl[8] = {-60, -30, 58, 172, 334, 538, 1198, 3042 }; static const int rl42[16] = {0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0 }; static const int ilb[32] = { @@ -433,22 +391,22 @@ class G722 : public sfl::AudioCodec { for (j = 0; j < len;) { - if (decode_s->packed) { + if (decode_state_.packed) { /* Unpack the code bits */ - if (decode_s->in_bits < decode_s->bits_per_sample) { - decode_s->in_buffer |= (g722_data[j++] << decode_s->in_bits); - decode_s->in_bits += 8; + if (decode_state_.in_bits < decode_state_.bits_per_sample) { + decode_state_.in_buffer |= (g722_data[j++] << decode_state_.in_bits); + decode_state_.in_bits += 8; } - code = decode_s->in_buffer & ((1 << decode_s->bits_per_sample) - 1); + code = decode_state_.in_buffer & ((1 << decode_state_.bits_per_sample) - 1); - decode_s->in_buffer >>= decode_s->bits_per_sample; - decode_s->in_bits -= decode_s->bits_per_sample; + decode_state_.in_buffer >>= decode_state_.bits_per_sample; + decode_state_.in_bits -= decode_state_.bits_per_sample; } else { code = g722_data[j++]; } - switch (decode_s->bits_per_sample) { + switch (decode_state_.bits_per_sample) { default: @@ -474,10 +432,10 @@ class G722 : public sfl::AudioCodec { } /* Block 5L, LOW BAND INVQBL */ - wd2 = (decode_s->band[0].det*wd2) >> 15; + wd2 = (decode_state_.band[0].det*wd2) >> 15; /* Block 5L, RECONS */ - rlow = decode_s->band[0].s + wd2; + rlow = decode_state_.band[0].s + wd2; /* Block 6L, LIMIT */ if (rlow > 16383) @@ -488,12 +446,12 @@ class G722 : public sfl::AudioCodec { /* Block 2L, INVQAL */ wd2 = qm4[wd1]; - dlowt = (decode_s->band[0].det*wd2) >> 15; + dlowt = (decode_state_.band[0].det*wd2) >> 15; /* Block 3L, LOGSCL */ wd2 = rl42[wd1]; - wd1 = (decode_s->band[0].nb*127) >> 7; + wd1 = (decode_state_.band[0].nb*127) >> 7; wd1 += wl[wd2]; @@ -502,25 +460,25 @@ class G722 : public sfl::AudioCodec { else if (wd1 > 18432) wd1 = 18432; - decode_s->band[0].nb = wd1; + decode_state_.band[0].nb = wd1; /* Block 3L, SCALEL */ - wd1 = (decode_s->band[0].nb >> 6) & 31; + wd1 = (decode_state_.band[0].nb >> 6) & 31; - wd2 = 8 - (decode_s->band[0].nb >> 11); + wd2 = 8 - (decode_state_.band[0].nb >> 11); wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2); - decode_s->band[0].det = wd3 << 2; + decode_state_.band[0].det = wd3 << 2; block4_decode(0, dlowt); - if (!decode_s->eight_k) { + if (!decode_state_.eight_k) { /* Block 2H, INVQAH */ wd2 = qm2[ihigh]; - dhigh = (decode_s->band[1].det*wd2) >> 15; + dhigh = (decode_state_.band[1].det*wd2) >> 15; /* Block 5H, RECONS */ - rhigh = dhigh + decode_s->band[1].s; + rhigh = dhigh + decode_state_.band[1].s; /* Block 6H, LIMIT */ if (rhigh > 16383) @@ -531,7 +489,7 @@ class G722 : public sfl::AudioCodec { /* Block 2H, INVQAH */ wd2 = rh2[ihigh]; - wd1 = (decode_s->band[1].nb*127) >> 7; + wd1 = (decode_state_.band[1].nb*127) >> 7; wd1 += wh[wd2]; @@ -540,47 +498,47 @@ class G722 : public sfl::AudioCodec { else if (wd1 > 22528) wd1 = 22528; - decode_s->band[1].nb = wd1; + decode_state_.band[1].nb = wd1; /* Block 3H, SCALEH */ - wd1 = (decode_s->band[1].nb >> 6) & 31; + wd1 = (decode_state_.band[1].nb >> 6) & 31; - wd2 = 10 - (decode_s->band[1].nb >> 11); + wd2 = 10 - (decode_state_.band[1].nb >> 11); wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2); - decode_s->band[1].det = wd3 << 2; + decode_state_.band[1].det = wd3 << 2; block4_decode(1, dhigh); } - if (decode_s->itu_test_mode) { - amp[outlen++] = (int16_t)(rlow << 1); - amp[outlen++] = (int16_t)(rhigh << 1); + if (decode_state_.itu_test_mode) { + amp[outlen++] = (SFLDataFormat)(rlow << 1); + amp[outlen++] = (SFLDataFormat)(rhigh << 1); } else { - if (decode_s->eight_k) { - amp[outlen++] = (int16_t) rlow; + if (decode_state_.eight_k) { + amp[outlen++] = (SFLDataFormat) rlow; } else { /* Apply the receive QMF */ for (i = 0; i < 22; i++) - decode_s->x[i] = decode_s->x[i + 2]; + decode_state_.x[i] = decode_state_.x[i + 2]; - decode_s->x[22] = rlow + rhigh; + decode_state_.x[22] = rlow + rhigh; - decode_s->x[23] = rlow - rhigh; + decode_state_.x[23] = rlow - rhigh; xout1 = 0; xout2 = 0; for (i = 0; i < 12; i++) { - xout2 += decode_s->x[2*i]*qmf_coeffs[i]; - xout1 += decode_s->x[2*i + 1]*qmf_coeffs[11 - i]; + xout2 += decode_state_.x[2*i]*qmf_coeffs[i]; + xout1 += decode_state_.x[2*i + 1]*qmf_coeffs[11 - i]; } - amp[outlen++] = (int16_t)(xout1 >> 12); + amp[outlen++] = (SFLDataFormat)(xout1 >> 12); - amp[outlen++] = (int16_t)(xout2 >> 12); + amp[outlen++] = (SFLDataFormat)(xout2 >> 12); } } } @@ -588,7 +546,8 @@ class G722 : public sfl::AudioCodec { return outlen; } - int g722_encode(uint8_t g722_data[], const int16_t amp[], int len) { + int g722_encode(uint8_t g722_data[], const SFLDataFormat amp[], int len) + { static const int q6[32] = { 0, 35, 72, 110, 150, 190, 233, 276, 323, 370, 422, 473, 530, 587, 650, 714, @@ -666,21 +625,21 @@ class G722 : public sfl::AudioCodec { xhigh = 0; for (j = 0; j < len;) { - if (encode_s->itu_test_mode) { + if (encode_state_.itu_test_mode) { xlow = xhigh = amp[j++] >> 1; } else { - if (encode_s->eight_k) { + if (encode_state_.eight_k) { xlow = amp[j++]; } else { /* Apply the transmit QMF */ /* Shuffle the buffer down */ for (i = 0; i < 22; i++) - encode_s->x[i] = encode_s->x[i + 2]; + encode_state_.x[i] = encode_state_.x[i + 2]; - encode_s->x[22] = amp[j++]; + encode_state_.x[22] = amp[j++]; - encode_s->x[23] = amp[j++]; + encode_state_.x[23] = amp[j++]; /* Discard every other QMF output */ sumeven = 0; @@ -688,8 +647,8 @@ class G722 : public sfl::AudioCodec { sumodd = 0; for (i = 0; i < 12; i++) { - sumodd += encode_s->x[2*i]*qmf_coeffs[i]; - sumeven += encode_s->x[2*i + 1]*qmf_coeffs[11 - i]; + sumodd += encode_state_.x[2*i]*qmf_coeffs[i]; + sumeven += encode_state_.x[2*i + 1]*qmf_coeffs[11 - i]; } xlow = (sumeven + sumodd) >> 13; @@ -699,13 +658,13 @@ class G722 : public sfl::AudioCodec { } /* Block 1L, SUBTRA */ - el = saturate(xlow - encode_s->band[0].s); + el = saturate(xlow - encode_state_.band[0].s); /* Block 1L, QUANTL */ wd = (el >= 0) ? el : - (el + 1); for (i = 1; i < 30; i++) { - wd1 = (q6[i]*encode_s->band[0].det) >> 12; + wd1 = (q6[i]*encode_state_.band[0].det) >> 12; if (wd < wd1) break; @@ -716,79 +675,79 @@ class G722 : public sfl::AudioCodec { /* Block 2L, INVQAL */ ril = ilow >> 2; wd2 = qm4[ril]; - dlow = (encode_s->band[0].det*wd2) >> 15; + dlow = (encode_state_.band[0].det*wd2) >> 15; /* Block 3L, LOGSCL */ il4 = rl42[ril]; - wd = (encode_s->band[0].nb*127) >> 7; - encode_s->band[0].nb = wd + wl[il4]; + wd = (encode_state_.band[0].nb*127) >> 7; + encode_state_.band[0].nb = wd + wl[il4]; - if (encode_s->band[0].nb < 0) - encode_s->band[0].nb = 0; - else if (encode_s->band[0].nb > 18432) - encode_s->band[0].nb = 18432; + if (encode_state_.band[0].nb < 0) + encode_state_.band[0].nb = 0; + else if (encode_state_.band[0].nb > 18432) + encode_state_.band[0].nb = 18432; /* Block 3L, SCALEL */ - wd1 = (encode_s->band[0].nb >> 6) & 31; + wd1 = (encode_state_.band[0].nb >> 6) & 31; - wd2 = 8 - (encode_s->band[0].nb >> 11); + wd2 = 8 - (encode_state_.band[0].nb >> 11); wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2); - encode_s->band[0].det = wd3 << 2; + encode_state_.band[0].det = wd3 << 2; block4_encode(0, dlow); - if (encode_s->eight_k) { + if (encode_state_.eight_k) { /* Just leave the high bits as zero */ - code = (0xC0 | ilow) >> (8 - encode_s->bits_per_sample); + code = (0xC0 | ilow) >> (8 - encode_state_.bits_per_sample); } else { /* Block 1H, SUBTRA */ - eh = saturate(xhigh - encode_s->band[1].s); + eh = saturate(xhigh - encode_state_.band[1].s); /* Block 1H, QUANTH */ wd = (eh >= 0) ? eh : - (eh + 1); - wd1 = (564*encode_s->band[1].det) >> 12; + wd1 = (564*encode_state_.band[1].det) >> 12; mih = (wd >= wd1) ? 2 : 1; ihigh = (eh < 0) ? ihn[mih] : ihp[mih]; /* Block 2H, INVQAH */ wd2 = qm2[ihigh]; - dhigh = (encode_s->band[1].det*wd2) >> 15; + dhigh = (encode_state_.band[1].det*wd2) >> 15; /* Block 3H, LOGSCH */ ih2 = rh2[ihigh]; - wd = (encode_s->band[1].nb*127) >> 7; - encode_s->band[1].nb = wd + wh[ih2]; + wd = (encode_state_.band[1].nb*127) >> 7; + encode_state_.band[1].nb = wd + wh[ih2]; - if (encode_s->band[1].nb < 0) - encode_s->band[1].nb = 0; - else if (encode_s->band[1].nb > 22528) - encode_s->band[1].nb = 22528; + if (encode_state_.band[1].nb < 0) + encode_state_.band[1].nb = 0; + else if (encode_state_.band[1].nb > 22528) + encode_state_.band[1].nb = 22528; /* Block 3H, SCALEH */ - wd1 = (encode_s->band[1].nb >> 6) & 31; + wd1 = (encode_state_.band[1].nb >> 6) & 31; - wd2 = 10 - (encode_s->band[1].nb >> 11); + wd2 = 10 - (encode_state_.band[1].nb >> 11); wd3 = (wd2 < 0) ? (ilb[wd1] << -wd2) : (ilb[wd1] >> wd2); - encode_s->band[1].det = wd3 << 2; + encode_state_.band[1].det = wd3 << 2; block4_encode(1, dhigh); - code = ((ihigh << 6) | ilow) >> (8 - encode_s->bits_per_sample); + code = ((ihigh << 6) | ilow) >> (8 - encode_state_.bits_per_sample); } - if (encode_s->packed) { + if (encode_state_.packed) { /* Pack the code bits */ - encode_s->out_buffer |= (code << encode_s->out_bits); - encode_s->out_bits += encode_s->bits_per_sample; + encode_state_.out_buffer |= (code << encode_state_.out_bits); + encode_state_.out_bits += encode_state_.bits_per_sample; - if (encode_s->out_bits >= 8) { - g722_data[g722_bytes++] = (uint8_t)(encode_s->out_buffer & 0xFF); - encode_s->out_bits -= 8; - encode_s->out_buffer >>= 8; + if (encode_state_.out_bits >= 8) { + g722_data[g722_bytes++] = (uint8_t)(encode_state_.out_buffer & 0xFF); + encode_state_.out_bits -= 8; + encode_state_.out_buffer >>= 8; } } else { g722_data[g722_bytes++] = (uint8_t) code; @@ -798,12 +757,8 @@ class G722 : public sfl::AudioCodec { return g722_bytes; } - private: - NON_COPYABLE(G722); - - g722_decode_state_t *decode_s; - g722_encode_state_t *encode_s; - + g722_state_t decode_state_; + g722_state_t encode_state_; }; // the class factories diff --git a/daemon/src/audio/codecs/g722.h b/daemon/src/audio/codecs/g722.h index cb589e54c801af74b04f7ed4c236ede61839ee0c..b117a09ed06025b54d4865bcf287a7eb38c9c4cd 100644 --- a/daemon/src/audio/codecs/g722.h +++ b/daemon/src/audio/codecs/g722.h @@ -30,10 +30,8 @@ * as that of the covered work. */ - - -#if !defined(_G722_H_) -#define _G722_H_ +#ifndef G722_H_ +#define G722_H_ /** @@ -54,13 +52,13 @@ enum { }; #ifndef INT16_MAX -#define INT16_MAX 32767 +#define INT16_MAX 32767 #endif #ifndef INT16_MIN -#define INT16_MIN (-32768) +#define INT16_MIN (-32768) #endif -typedef struct { +struct g722_state_t { /*! TRUE if the operating in the special ITU test mode, with the band split filters disabled. */ int itu_test_mode; @@ -94,58 +92,6 @@ typedef struct { int in_bits; unsigned int out_buffer; int out_bits; -} g722_encode_state_t; - -typedef struct { - /*! TRUE if the operating in the special ITU test mode, with the band split filters - disabled. */ - int itu_test_mode; - /*! TRUE if the G.722 data is packed */ - int packed; - /*! TRUE if decode to 8k samples/second */ - int eight_k; - /*! 6 for 48000kbps, 7 for 56000kbps, or 8 for 64000kbps. */ - int bits_per_sample; - - /*! Signal history for the QMF */ - int x[24]; - - struct { - int s; - int sp; - int sz; - 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; - int det; - } band[2]; - - unsigned int in_buffer; - int in_bits; - unsigned int out_buffer; - int out_bits; -} g722_decode_state_t; - -#ifdef __cplusplus -extern "C" { -#endif - - void g722_encode_init(); - int g722_encode_release(); - int g722_encode(uint8_t g722_data[], const int16_t amp[], int len); - - void g722_decode_init(); - int g722_decode_release(); - int g722_decode(int16_t amp[], const uint8_t g722_data[], int len); - -#ifdef __cplusplus -} -#endif +}; -#endif +#endif // G722_H_