summaryrefslogtreecommitdiffstats
path: root/tinyDAV/src/codecs/g722
diff options
context:
space:
mode:
Diffstat (limited to 'tinyDAV/src/codecs/g722')
-rwxr-xr-xtinyDAV/src/codecs/g722/g722_decode.c203
-rwxr-xr-xtinyDAV/src/codecs/g722/g722_encode.c171
-rwxr-xr-xtinyDAV/src/codecs/g722/tdav_codec_g722.c271
3 files changed, 320 insertions, 325 deletions
diff --git a/tinyDAV/src/codecs/g722/g722_decode.c b/tinyDAV/src/codecs/g722/g722_decode.c
index b6b7830..c9c26af 100755
--- a/tinyDAV/src/codecs/g722/g722_decode.c
+++ b/tinyDAV/src/codecs/g722/g722_decode.c
@@ -7,7 +7,7 @@
*
* Copyright (C) 2005 Steve Underwood
*
- * Despite my general liking of the GPL, I place my own contributions
+ * Despite my general liking of the GPL, I place my own contributions
* to this code in the public domain for the benefit of all mankind -
* even the slimy ones who might try to proprietize my work and use it
* to my detriment.
@@ -48,10 +48,12 @@ static __inline int16_t saturate(int32_t amp)
/* Hopefully this is optimised for the common case - not clipping */
amp16 = (int16_t) amp;
- if (amp == amp16)
+ if (amp == amp16) {
return amp16;
- if (amp > TDAV_INT16_MAX)
+ }
+ if (amp > TDAV_INT16_MAX) {
return TDAV_INT16_MAX;
+ }
return TDAV_INT16_MIN;
}
/*- End of function --------------------------------------------------------*/
@@ -73,20 +75,24 @@ static void block4(g722_decode_state_t *s, int band, int d)
s->band[band].p[0] = saturate(s->band[band].sz + d);
/* Block 4, UPPOL2 */
- for (i = 0; i < 3; i++)
+ for (i = 0; i < 3; i++) {
s->band[band].sg[i] = s->band[band].p[i] >> 15;
+ }
wd1 = saturate(s->band[band].a[1] << 2);
wd2 = (s->band[band].sg[0] == s->band[band].sg[1]) ? -wd1 : wd1;
- if (wd2 > 32767)
+ if (wd2 > 32767) {
wd2 = 32767;
+ }
wd3 = (s->band[band].sg[0] == s->band[band].sg[2]) ? 128 : -128;
wd3 += (wd2 >> 7);
wd3 += (s->band[band].a[2]*32512) >> 15;
- if (wd3 > 12288)
+ if (wd3 > 12288) {
wd3 = 12288;
- else if (wd3 < -12288)
+ }
+ else if (wd3 < -12288) {
wd3 = -12288;
+ }
s->band[band].ap[2] = wd3;
/* Block 4, UPPOL1 */
@@ -97,16 +103,17 @@ static void block4(g722_decode_state_t *s, int band, int d)
s->band[band].ap[1] = saturate(wd1 + wd2);
wd3 = saturate(15360 - s->band[band].ap[2]);
- if (s->band[band].ap[1] > wd3)
+ if (s->band[band].ap[1] > wd3) {
s->band[band].ap[1] = wd3;
- else if (s->band[band].ap[1] < -wd3)
+ }
+ else if (s->band[band].ap[1] < -wd3) {
s->band[band].ap[1] = -wd3;
+ }
/* Block 4, UPZERO */
wd1 = (d == 0) ? 0 : 128;
s->band[band].sg[0] = d >> 15;
- for (i = 1; i < 7; i++)
- {
+ for (i = 1; i < 7; i++) {
s->band[band].sg[i] = s->band[band].d[i] >> 15;
wd2 = (s->band[band].sg[i] == s->band[band].sg[0]) ? wd1 : -wd1;
wd3 = (s->band[band].b[i]*32640) >> 15;
@@ -114,14 +121,12 @@ static void block4(g722_decode_state_t *s, int band, int d)
}
/* Block 4, DELAYA */
- for (i = 6; i > 0; i--)
- {
+ for (i = 6; i > 0; i--) {
s->band[band].d[i] = s->band[band].d[i - 1];
s->band[band].b[i] = s->band[band].bp[i];
}
-
- for (i = 2; i > 0; i--)
- {
+
+ for (i = 2; i > 0; i--) {
s->band[band].r[i] = s->band[band].r[i - 1];
s->band[band].p[i] = s->band[band].p[i - 1];
s->band[band].a[i] = s->band[band].ap[i];
@@ -136,8 +141,7 @@ static void block4(g722_decode_state_t *s, int band, int d)
/* Block 4, FILTEZ */
s->band[band].sz = 0;
- for (i = 6; i > 0; i--)
- {
+ for (i = 6; i > 0; i--) {
wd1 = saturate(s->band[band].d[i] + s->band[band].d[i]);
s->band[band].sz += (s->band[band].b[i]*wd1) >> 15;
}
@@ -150,24 +154,30 @@ static void block4(g722_decode_state_t *s, int band, int d)
g722_decode_state_t *g722_decode_init(g722_decode_state_t *s, int rate, int options)
{
- if (s == NULL)
- {
- if ((s = (g722_decode_state_t *) malloc(sizeof(*s))) == NULL)
+ if (s == NULL) {
+ if ((s = (g722_decode_state_t *) malloc(sizeof(*s))) == NULL) {
return NULL;
+ }
}
memset(s, 0, sizeof(*s));
- if (rate == 48000)
+ if (rate == 48000) {
s->bits_per_sample = 6;
- else if (rate == 56000)
+ }
+ else if (rate == 56000) {
s->bits_per_sample = 7;
- else
+ }
+ else {
s->bits_per_sample = 8;
- if ((options & G722_SAMPLE_RATE_8000))
+ }
+ if ((options & G722_SAMPLE_RATE_8000)) {
s->eight_k = TRUE;
- if ((options & G722_PACKED) && s->bits_per_sample != 8)
+ }
+ if ((options & G722_PACKED) && s->bits_per_sample != 8) {
s->packed = TRUE;
- else
+ }
+ else {
s->packed = FALSE;
+ }
s->band[0].det = 32;
s->band[1].det = 8;
return s;
@@ -186,8 +196,7 @@ int g722_decode(g722_decode_state_t *s, int16_t amp[],
{
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] =
- {
+ static const int ilb[32] = {
2048, 2093, 2139, 2186, 2233, 2282, 2332,
2383, 2435, 2489, 2543, 2599, 2656, 2714,
2774, 2834, 2896, 2960, 3025, 3091, 3158,
@@ -197,46 +206,42 @@ int g722_decode(g722_decode_state_t *s, int16_t amp[],
static const int wh[3] = {0, -214, 798};
static const int rh2[4] = {2, 1, 2, 1};
static const int qm2[4] = {-7408, -1616, 7408, 1616};
- static const int qm4[16] =
- {
- 0, -20456, -12896, -8968,
- -6288, -4240, -2584, -1200,
- 20456, 12896, 8968, 6288,
- 4240, 2584, 1200, 0
+ static const int qm4[16] = {
+ 0, -20456, -12896, -8968,
+ -6288, -4240, -2584, -1200,
+ 20456, 12896, 8968, 6288,
+ 4240, 2584, 1200, 0
};
- static const int qm5[32] =
- {
- -280, -280, -23352, -17560,
- -14120, -11664, -9752, -8184,
- -6864, -5712, -4696, -3784,
- -2960, -2208, -1520, -880,
- 23352, 17560, 14120, 11664,
- 9752, 8184, 6864, 5712,
- 4696, 3784, 2960, 2208,
- 1520, 880, 280, -280
+ static const int qm5[32] = {
+ -280, -280, -23352, -17560,
+ -14120, -11664, -9752, -8184,
+ -6864, -5712, -4696, -3784,
+ -2960, -2208, -1520, -880,
+ 23352, 17560, 14120, 11664,
+ 9752, 8184, 6864, 5712,
+ 4696, 3784, 2960, 2208,
+ 1520, 880, 280, -280
};
- static const int qm6[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
+ static const int qm6[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
};
- static const int qmf_coeffs[12] =
- {
- 3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
+ static const int qmf_coeffs[12] = {
+ 3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
};
int dlowt;
@@ -256,13 +261,10 @@ int g722_decode(g722_decode_state_t *s, int16_t amp[],
outlen = 0;
rhigh = 0;
- for (j = 0; j < len; )
- {
- if (s->packed)
- {
+ for (j = 0; j < len; ) {
+ if (s->packed) {
/* Unpack the code bits */
- if (s->in_bits < s->bits_per_sample)
- {
+ if (s->in_bits < s->bits_per_sample) {
s->in_buffer |= (g722_data[j++] << s->in_bits);
s->in_bits += 8;
}
@@ -270,13 +272,11 @@ int g722_decode(g722_decode_state_t *s, int16_t amp[],
s->in_buffer >>= s->bits_per_sample;
s->in_bits -= s->bits_per_sample;
}
- else
- {
+ else {
code = g722_data[j++];
}
- switch (s->bits_per_sample)
- {
+ switch (s->bits_per_sample) {
default:
case 8:
wd1 = code & 0x3F;
@@ -301,10 +301,12 @@ int g722_decode(g722_decode_state_t *s, int16_t amp[],
/* Block 5L, RECONS */
rlow = s->band[0].s + wd2;
/* Block 6L, LIMIT */
- if (rlow > 16383)
+ if (rlow > 16383) {
rlow = 16383;
- else if (rlow < -16384)
+ }
+ else if (rlow < -16384) {
rlow = -16384;
+ }
/* Block 2L, INVQAL */
wd2 = qm4[wd1];
@@ -314,12 +316,14 @@ int g722_decode(g722_decode_state_t *s, int16_t amp[],
wd2 = rl42[wd1];
wd1 = (s->band[0].nb*127) >> 7;
wd1 += wl[wd2];
- if (wd1 < 0)
+ if (wd1 < 0) {
wd1 = 0;
- else if (wd1 > 18432)
+ }
+ else if (wd1 > 18432) {
wd1 = 18432;
+ }
s->band[0].nb = wd1;
-
+
/* Block 3L, SCALEL */
wd1 = (s->band[0].nb >> 6) & 31;
wd2 = 8 - (s->band[0].nb >> 11);
@@ -327,30 +331,33 @@ int g722_decode(g722_decode_state_t *s, int16_t amp[],
s->band[0].det = wd3 << 2;
block4(s, 0, dlowt);
-
- if (!s->eight_k)
- {
+
+ if (!s->eight_k) {
/* Block 2H, INVQAH */
wd2 = qm2[ihigh];
dhigh = (s->band[1].det*wd2) >> 15;
/* Block 5H, RECONS */
rhigh = dhigh + s->band[1].s;
/* Block 6H, LIMIT */
- if (rhigh > 16383)
+ if (rhigh > 16383) {
rhigh = 16383;
- else if (rhigh < -16384)
+ }
+ else if (rhigh < -16384) {
rhigh = -16384;
+ }
/* Block 2H, INVQAH */
wd2 = rh2[ihigh];
wd1 = (s->band[1].nb*127) >> 7;
wd1 += wh[wd2];
- if (wd1 < 0)
+ if (wd1 < 0) {
wd1 = 0;
- else if (wd1 > 22528)
+ }
+ else if (wd1 > 22528) {
wd1 = 22528;
+ }
s->band[1].nb = wd1;
-
+
/* Block 3H, SCALEH */
wd1 = (s->band[1].nb >> 6) & 31;
wd2 = 10 - (s->band[1].nb >> 11);
@@ -360,29 +367,25 @@ int g722_decode(g722_decode_state_t *s, int16_t amp[],
block4(s, 1, dhigh);
}
- if (s->itu_test_mode)
- {
+ if (s->itu_test_mode) {
amp[outlen++] = (int16_t) (rlow << 1);
amp[outlen++] = (int16_t) (rhigh << 1);
}
- else
- {
- if (s->eight_k)
- {
+ else {
+ if (s->eight_k) {
amp[outlen++] = (int16_t) (rlow << 1);
}
- else
- {
+ else {
/* Apply the receive QMF */
- for (i = 0; i < 22; i++)
+ for (i = 0; i < 22; i++) {
s->x[i] = s->x[i + 2];
+ }
s->x[22] = rlow + rhigh;
s->x[23] = rlow - rhigh;
xout1 = 0;
xout2 = 0;
- for (i = 0; i < 12; i++)
- {
+ for (i = 0; i < 12; i++) {
xout2 += s->x[2*i]*qmf_coeffs[i];
xout1 += s->x[2*i + 1]*qmf_coeffs[11 - i];
}
diff --git a/tinyDAV/src/codecs/g722/g722_encode.c b/tinyDAV/src/codecs/g722/g722_encode.c
index 68758eb..a0bf821 100755
--- a/tinyDAV/src/codecs/g722/g722_encode.c
+++ b/tinyDAV/src/codecs/g722/g722_encode.c
@@ -9,7 +9,7 @@
*
* All rights reserved.
*
- * Despite my general liking of the GPL, I place my own contributions
+ * Despite my general liking of the GPL, I place my own contributions
* to this code in the public domain for the benefit of all mankind -
* even the slimy ones who might try to proprietize my work and use it
* to my detriment.
@@ -47,10 +47,12 @@ static __inline int16_t saturate(int32_t amp)
/* Hopefully this is optimised for the common case - not clipping */
amp16 = (int16_t) amp;
- if (amp == amp16)
+ if (amp == amp16) {
return amp16;
- if (amp > TDAV_INT16_MAX)
+ }
+ if (amp > TDAV_INT16_MAX) {
return TDAV_INT16_MAX;
+ }
return TDAV_INT16_MIN;
}
/*- End of function --------------------------------------------------------*/
@@ -70,19 +72,23 @@ static void block4(g722_encode_state_t *s, int band, int d)
s->band[band].p[0] = saturate(s->band[band].sz + d);
/* Block 4, UPPOL2 */
- for (i = 0; i < 3; i++)
+ for (i = 0; i < 3; i++) {
s->band[band].sg[i] = s->band[band].p[i] >> 15;
+ }
wd1 = saturate(s->band[band].a[1] << 2);
wd2 = (s->band[band].sg[0] == s->band[band].sg[1]) ? -wd1 : wd1;
- if (wd2 > 32767)
+ if (wd2 > 32767) {
wd2 = 32767;
+ }
wd3 = (wd2 >> 7) + ((s->band[band].sg[0] == s->band[band].sg[2]) ? 128 : -128);
wd3 += (s->band[band].a[2]*32512) >> 15;
- if (wd3 > 12288)
+ if (wd3 > 12288) {
wd3 = 12288;
- else if (wd3 < -12288)
+ }
+ else if (wd3 < -12288) {
wd3 = -12288;
+ }
s->band[band].ap[2] = wd3;
/* Block 4, UPPOL1 */
@@ -93,16 +99,17 @@ static void block4(g722_encode_state_t *s, int band, int d)
s->band[band].ap[1] = saturate(wd1 + wd2);
wd3 = saturate(15360 - s->band[band].ap[2]);
- if (s->band[band].ap[1] > wd3)
+ if (s->band[band].ap[1] > wd3) {
s->band[band].ap[1] = wd3;
- else if (s->band[band].ap[1] < -wd3)
+ }
+ else if (s->band[band].ap[1] < -wd3) {
s->band[band].ap[1] = -wd3;
+ }
/* Block 4, UPZERO */
wd1 = (d == 0) ? 0 : 128;
s->band[band].sg[0] = d >> 15;
- for (i = 1; i < 7; i++)
- {
+ for (i = 1; i < 7; i++) {
s->band[band].sg[i] = s->band[band].d[i] >> 15;
wd2 = (s->band[band].sg[i] == s->band[band].sg[0]) ? wd1 : -wd1;
wd3 = (s->band[band].b[i]*32640) >> 15;
@@ -110,14 +117,12 @@ static void block4(g722_encode_state_t *s, int band, int d)
}
/* Block 4, DELAYA */
- for (i = 6; i > 0; i--)
- {
+ for (i = 6; i > 0; i--) {
s->band[band].d[i] = s->band[band].d[i - 1];
s->band[band].b[i] = s->band[band].bp[i];
}
-
- for (i = 2; i > 0; i--)
- {
+
+ for (i = 2; i > 0; i--) {
s->band[band].r[i] = s->band[band].r[i - 1];
s->band[band].p[i] = s->band[band].p[i - 1];
s->band[band].a[i] = s->band[band].ap[i];
@@ -132,8 +137,7 @@ static void block4(g722_encode_state_t *s, int band, int d)
/* Block 4, FILTEZ */
s->band[band].sz = 0;
- for (i = 6; i > 0; i--)
- {
+ for (i = 6; i > 0; i--) {
wd1 = saturate(s->band[band].d[i] + s->band[band].d[i]);
s->band[band].sz += (s->band[band].b[i]*wd1) >> 15;
}
@@ -146,24 +150,30 @@ static void block4(g722_encode_state_t *s, int band, int d)
g722_encode_state_t *g722_encode_init(g722_encode_state_t *s, int rate, int options)
{
- if (s == NULL)
- {
- if ((s = (g722_encode_state_t *) malloc(sizeof(*s))) == NULL)
+ if (s == NULL) {
+ if ((s = (g722_encode_state_t *) malloc(sizeof(*s))) == NULL) {
return NULL;
+ }
}
memset(s, 0, sizeof(*s));
- if (rate == 48000)
+ if (rate == 48000) {
s->bits_per_sample = 6;
- else if (rate == 56000)
+ }
+ else if (rate == 56000) {
s->bits_per_sample = 7;
- else
+ }
+ else {
s->bits_per_sample = 8;
- if ((options & G722_SAMPLE_RATE_8000))
+ }
+ if ((options & G722_SAMPLE_RATE_8000)) {
s->eight_k = TRUE;
- if ((options & G722_PACKED) && s->bits_per_sample != 8)
+ }
+ if ((options & G722_PACKED) && s->bits_per_sample != 8) {
s->packed = TRUE;
- else
+ }
+ else {
s->packed = FALSE;
+ }
s->band[0].det = 32;
s->band[1].det = 8;
return s;
@@ -197,57 +207,48 @@ int16_t limitValues (int16_t rl)
int g722_encode(g722_encode_state_t *s, uint8_t g722_data[],
const int16_t amp[], int len)
{
- static const int q6[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,
+ static const int q6[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 const int iln[32] =
- {
- 0, 63, 62, 31, 30, 29, 28, 27,
+ static const int iln[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 const int ilp[32] =
- {
- 0, 61, 60, 59, 58, 57, 56, 55,
+ static const int ilp[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 const int wl[8] =
- {
+ static const int wl[8] = {
-60, -30, 58, 172, 334, 538, 1198, 3042
};
- static const int rl42[16] =
- {
+ 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] =
- {
+ static const int ilb[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
};
- static const int qm4[16] =
- {
- 0, -20456, -12896, -8968,
- -6288, -4240, -2584, -1200,
- 20456, 12896, 8968, 6288,
- 4240, 2584, 1200, 0
+ static const int qm4[16] = {
+ 0, -20456, -12896, -8968,
+ -6288, -4240, -2584, -1200,
+ 20456, 12896, 8968, 6288,
+ 4240, 2584, 1200, 0
};
- static const int qm2[4] =
- {
+ static const int qm2[4] = {
-7408, -1616, 7408, 1616
};
- static const int qmf_coeffs[12] =
- {
- 3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
+ static const int qmf_coeffs[12] = {
+ 3, -11, 12, 32, -210, 951, 3876, -805, 362, -156, 53, -11,
};
static const int ihn[3] = {0, 1, 0};
static const int ihp[3] = {0, 3, 2};
@@ -281,34 +282,29 @@ int g722_encode(g722_encode_state_t *s, uint8_t g722_data[],
g722_bytes = 0;
xhigh = 0;
- for (j = 0; j < len; )
- {
- if (s->itu_test_mode)
- {
+ for (j = 0; j < len; ) {
+ if (s->itu_test_mode) {
xlow =
- xhigh = amp[j++] >> 1;
+ xhigh = amp[j++] >> 1;
}
- else
- {
- if (s->eight_k)
- {
+ else {
+ if (s->eight_k) {
/* We shift by 1 to allow for the 15 bit input to the G.722 algorithm. */
xlow = amp[j++] >> 1;
}
- else
- {
+ else {
/* Apply the transmit QMF */
/* Shuffle the buffer down */
- for (i = 0; i < 22; i++)
+ for (i = 0; i < 22; i++) {
s->x[i] = s->x[i + 2];
+ }
s->x[22] = amp[j++];
s->x[23] = amp[j++];
-
+
/* Discard every other QMF output */
sumeven = 0;
sumodd = 0;
- for (i = 0; i < 12; i++)
- {
+ for (i = 0; i < 12; i++) {
sumodd += s->x[2*i]*qmf_coeffs[i];
sumeven += s->x[2*i + 1]*qmf_coeffs[11 - i];
}
@@ -334,11 +330,11 @@ int g722_encode(g722_encode_state_t *s, uint8_t g722_data[],
/* Block 1L, QUANTL */
wd = (el >= 0) ? el : -(el + 1);
- for (i = 1; i < 30; i++)
- {
+ for (i = 1; i < 30; i++) {
wd1 = (q6[i]*s->band[0].det) >> 12;
- if (wd < wd1)
+ if (wd < wd1) {
break;
+ }
}
ilow = (el < 0) ? iln[i] : ilp[i];
@@ -351,10 +347,12 @@ int g722_encode(g722_encode_state_t *s, uint8_t g722_data[],
il4 = rl42[ril];
wd = (s->band[0].nb*127) >> 7;
s->band[0].nb = wd + wl[il4];
- if (s->band[0].nb < 0)
+ if (s->band[0].nb < 0) {
s->band[0].nb = 0;
- else if (s->band[0].nb > 18432)
+ }
+ else if (s->band[0].nb > 18432) {
s->band[0].nb = 18432;
+ }
/* Block 3L, SCALEL */
wd1 = (s->band[0].nb >> 6) & 31;
@@ -363,14 +361,12 @@ int g722_encode(g722_encode_state_t *s, uint8_t g722_data[],
s->band[0].det = wd3 << 2;
block4(s, 0, dlow);
-
- if (s->eight_k)
- {
+
+ if (s->eight_k) {
/* Just leave the high bits as zero */
code = (0xC0 | ilow) >> (8 - s->bits_per_sample);
}
- else
- {
+ else {
/* Block 1H, SUBTRA */
eh = saturate(xhigh - s->band[1].s);
@@ -388,10 +384,12 @@ int g722_encode(g722_encode_state_t *s, uint8_t g722_data[],
ih2 = rh2[ihigh];
wd = (s->band[1].nb*127) >> 7;
s->band[1].nb = wd + wh[ih2];
- if (s->band[1].nb < 0)
+ if (s->band[1].nb < 0) {
s->band[1].nb = 0;
- else if (s->band[1].nb > 22528)
+ }
+ else if (s->band[1].nb > 22528) {
s->band[1].nb = 22528;
+ }
/* Block 3H, SCALEH */
wd1 = (s->band[1].nb >> 6) & 31;
@@ -403,20 +401,17 @@ int g722_encode(g722_encode_state_t *s, uint8_t g722_data[],
code = ((ihigh << 6) | ilow) >> (8 - s->bits_per_sample);
}
- if (s->packed)
- {
+ if (s->packed) {
/* Pack the code bits */
s->out_buffer |= (code << s->out_bits);
s->out_bits += s->bits_per_sample;
- if (s->out_bits >= 8)
- {
+ if (s->out_bits >= 8) {
g722_data[g722_bytes++] = (uint8_t) (s->out_buffer & 0xFF);
s->out_bits -= 8;
s->out_buffer >>= 8;
}
}
- else
- {
+ else {
g722_data[g722_bytes++] = (uint8_t) code;
}
}
diff --git a/tinyDAV/src/codecs/g722/tdav_codec_g722.c b/tinyDAV/src/codecs/g722/tdav_codec_g722.c
index 749fa04..f543bdf 100755
--- a/tinyDAV/src/codecs/g722/tdav_codec_g722.c
+++ b/tinyDAV/src/codecs/g722/tdav_codec_g722.c
@@ -26,124 +26,123 @@
#include "tsk_memory.h"
#include "tsk_debug.h"
-typedef struct tdav_codec_g722_s
-{
- TMEDIA_DECLARE_CODEC_AUDIO;
+typedef struct tdav_codec_g722_s {
+ TMEDIA_DECLARE_CODEC_AUDIO;
- g722_encode_state_t *enc_state;
- g722_decode_state_t *dec_state;
+ g722_encode_state_t *enc_state;
+ g722_decode_state_t *dec_state;
}
tdav_codec_g722_t;
static int tdav_codec_g722_open(tmedia_codec_t* self)
{
- tdav_codec_g722_t* g722 = (tdav_codec_g722_t*)self;
-
- if (!g722){
- TSK_DEBUG_ERROR("Invalid parameter");
- return -1;
- }
-
- // Initialize the decoder
- if (!g722->dec_state){
- if (!(g722->dec_state = tsk_calloc(1, sizeof(g722_decode_state_t)))){
- TSK_DEBUG_ERROR("Failed to create G.722 decoder state");
- return -2;
- }
- // Create and/or reset the G.722 decoder
- // Bitrate 64 kbps and wideband mode (2)
- if (!(g722->dec_state = g722_decode_init(g722->dec_state, 64000, 2))){
- TSK_DEBUG_ERROR("g722_decode_init failed");
- return -3;
- }
- }
-
- // Initialize the encoder
- if (!g722->enc_state){
- if (!(g722->enc_state = tsk_calloc(1, sizeof(g722_encode_state_t)))){
- TSK_DEBUG_ERROR("Failed to create G.722 encoder state");
- return -4;
- }
- // Create and/or reset the G.722 encoder
- // Bitrate 64 kbps and wideband mode (2)
- if (!(g722->enc_state = g722_encode_init(g722->enc_state, 64000, 2))){
- TSK_DEBUG_ERROR("g722_encode_init failed");
- return -5;
- }
- }
-
- return 0;
+ tdav_codec_g722_t* g722 = (tdav_codec_g722_t*)self;
+
+ if (!g722) {
+ TSK_DEBUG_ERROR("Invalid parameter");
+ return -1;
+ }
+
+ // Initialize the decoder
+ if (!g722->dec_state) {
+ if (!(g722->dec_state = tsk_calloc(1, sizeof(g722_decode_state_t)))) {
+ TSK_DEBUG_ERROR("Failed to create G.722 decoder state");
+ return -2;
+ }
+ // Create and/or reset the G.722 decoder
+ // Bitrate 64 kbps and wideband mode (2)
+ if (!(g722->dec_state = g722_decode_init(g722->dec_state, 64000, 2))) {
+ TSK_DEBUG_ERROR("g722_decode_init failed");
+ return -3;
+ }
+ }
+
+ // Initialize the encoder
+ if (!g722->enc_state) {
+ if (!(g722->enc_state = tsk_calloc(1, sizeof(g722_encode_state_t)))) {
+ TSK_DEBUG_ERROR("Failed to create G.722 encoder state");
+ return -4;
+ }
+ // Create and/or reset the G.722 encoder
+ // Bitrate 64 kbps and wideband mode (2)
+ if (!(g722->enc_state = g722_encode_init(g722->enc_state, 64000, 2))) {
+ TSK_DEBUG_ERROR("g722_encode_init failed");
+ return -5;
+ }
+ }
+
+ return 0;
}
static int tdav_codec_g722_close(tmedia_codec_t* self)
{
- tdav_codec_g722_t* g722 = (tdav_codec_g722_t*)self;
+ tdav_codec_g722_t* g722 = (tdav_codec_g722_t*)self;
- (void)(g722);
+ (void)(g722);
- /* resources will be freed by the dctor() */
+ /* resources will be freed by the dctor() */
- return 0;
+ return 0;
}
static tsk_size_t tdav_codec_g722_encode(tmedia_codec_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size)
{
- tsk_size_t out_g722_size;
- tdav_codec_g722_t* g722 = (tdav_codec_g722_t*)self;
+ tsk_size_t out_g722_size;
+ tdav_codec_g722_t* g722 = (tdav_codec_g722_t*)self;
- if (!self || !in_data || !in_size || !out_data){
- TSK_DEBUG_ERROR("Invalid parameter");
- return 0;
- }
+ if (!self || !in_data || !in_size || !out_data) {
+ TSK_DEBUG_ERROR("Invalid parameter");
+ return 0;
+ }
- out_g722_size = in_size >> 2;
+ out_g722_size = in_size >> 2;
- if (*out_max_size < out_g722_size){
- if (!(*out_data = tsk_realloc(*out_data, out_g722_size))){
- TSK_DEBUG_ERROR("Failed to allocate new buffer");
- *out_max_size = 0;
- return 0;
- }
- *out_max_size = out_g722_size;
- }
+ if (*out_max_size < out_g722_size) {
+ if (!(*out_data = tsk_realloc(*out_data, out_g722_size))) {
+ TSK_DEBUG_ERROR("Failed to allocate new buffer");
+ *out_max_size = 0;
+ return 0;
+ }
+ *out_max_size = out_g722_size;
+ }
- g722_encode(g722->enc_state, (uint8_t*)*out_data, (int16_t*)in_data, (int)in_size / sizeof(int16_t));
+ g722_encode(g722->enc_state, (uint8_t*)*out_data, (int16_t*)in_data, (int)in_size / sizeof(int16_t));
- return out_g722_size;
+ return out_g722_size;
}
static tsk_size_t tdav_codec_g722_decode(tmedia_codec_t* self, const void* in_data, tsk_size_t in_size, void** out_data, tsk_size_t* out_max_size, const tsk_object_t* proto_hdr)
{
- tdav_codec_g722_t* g722 = (tdav_codec_g722_t*)self;
-
- if (!self || !in_data || !in_size || !out_data){
- TSK_DEBUG_ERROR("Invalid parameter");
- return 0;
- }
-
- /* allocate new buffer */
- if (*out_max_size < (in_size << 2)){
- if (!(*out_data = tsk_realloc(*out_data, in_size << 2))){
- TSK_DEBUG_ERROR("Failed to allocate new buffer");
- *out_max_size = 0;
- return 0;
- }
- *out_max_size = in_size << 2;
- }
-
- g722_decode(g722->dec_state, (int16_t*)*out_data, (uint8_t*)in_data, (int)in_size);
-
- return (in_size << 2);
+ tdav_codec_g722_t* g722 = (tdav_codec_g722_t*)self;
+
+ if (!self || !in_data || !in_size || !out_data) {
+ TSK_DEBUG_ERROR("Invalid parameter");
+ return 0;
+ }
+
+ /* allocate new buffer */
+ if (*out_max_size < (in_size << 2)) {
+ if (!(*out_data = tsk_realloc(*out_data, in_size << 2))) {
+ TSK_DEBUG_ERROR("Failed to allocate new buffer");
+ *out_max_size = 0;
+ return 0;
+ }
+ *out_max_size = in_size << 2;
+ }
+
+ g722_decode(g722->dec_state, (int16_t*)*out_data, (uint8_t*)in_data, (int)in_size);
+
+ return (in_size << 2);
}
static tsk_bool_t tdav_codec_g722_sdp_att_match(const tmedia_codec_t* codec, const char* att_name, const char* att_value)
{
- return tsk_true;
+ return tsk_true;
}
static char* tdav_codec_g722_sdp_att_get(const tmedia_codec_t* codec, const char* att_name)
{
- return tsk_null;
+ return tsk_null;
}
//
@@ -153,67 +152,65 @@ static char* tdav_codec_g722_sdp_att_get(const tmedia_codec_t* codec, const char
/* constructor */
static tsk_object_t* tdav_codec_g722_ctor(tsk_object_t * self, va_list * app)
{
- tdav_codec_g722_t *g722 = self;
- if (g722){
- /* init base: called by tmedia_codec_create() */
- /* init self */
+ tdav_codec_g722_t *g722 = self;
+ if (g722) {
+ /* init base: called by tmedia_codec_create() */
+ /* init self */
- }
- return self;
+ }
+ return self;
}
/* destructor */
static tsk_object_t* tdav_codec_g722_dtor(tsk_object_t * self)
{
- tdav_codec_g722_t *g722 = self;
- if (g722){
- /* deinit base */
- tmedia_codec_audio_deinit(g722);
- /* deinit self */
- if (g722->enc_state){
- g722_encode_release(g722->enc_state), g722->enc_state = tsk_null;
- }
- if (g722->dec_state){
- g722_decode_release(g722->dec_state), g722->dec_state = tsk_null;
- }
- }
-
- return self;
+ tdav_codec_g722_t *g722 = self;
+ if (g722) {
+ /* deinit base */
+ tmedia_codec_audio_deinit(g722);
+ /* deinit self */
+ if (g722->enc_state) {
+ g722_encode_release(g722->enc_state), g722->enc_state = tsk_null;
+ }
+ if (g722->dec_state) {
+ g722_decode_release(g722->dec_state), g722->dec_state = tsk_null;
+ }
+ }
+
+ return self;
}
/* object definition */
-static const tsk_object_def_t tdav_codec_g722_def_s =
-{
- sizeof(tdav_codec_g722_t),
- tdav_codec_g722_ctor,
- tdav_codec_g722_dtor,
- tmedia_codec_cmp,
+static const tsk_object_def_t tdav_codec_g722_def_s = {
+ sizeof(tdav_codec_g722_t),
+ tdav_codec_g722_ctor,
+ tdav_codec_g722_dtor,
+ tmedia_codec_cmp,
};
/* plugin definition*/
-static const tmedia_codec_plugin_def_t tdav_codec_g722_plugin_def_s =
-{
- &tdav_codec_g722_def_s,
-
- tmedia_audio,
- tmedia_codec_id_g722,
- "G722",
- "g722 Codec (native)",
- TMEDIA_CODEC_FORMAT_G722,
- tsk_false,
- 16000,
-
- { /* audio */
- 1, // channels
- 0 // ptime @deprecated
- },
-
- /* video */
- { 0 },
-
- tsk_null, // set()
- tdav_codec_g722_open,
- tdav_codec_g722_close,
- tdav_codec_g722_encode,
- tdav_codec_g722_decode,
- tdav_codec_g722_sdp_att_match,
- tdav_codec_g722_sdp_att_get
+static const tmedia_codec_plugin_def_t tdav_codec_g722_plugin_def_s = {
+ &tdav_codec_g722_def_s,
+
+ tmedia_audio,
+ tmedia_codec_id_g722,
+ "G722",
+ "g722 Codec (native)",
+ TMEDIA_CODEC_FORMAT_G722,
+ tsk_false,
+ 16000,
+
+ { /* audio */
+ 1, // channels
+ 0 // ptime @deprecated
+ },
+
+ /* video */
+ { 0 },
+
+ tsk_null, // set()
+ tdav_codec_g722_open,
+ tdav_codec_g722_close,
+ tdav_codec_g722_encode,
+ tdav_codec_g722_decode,
+ tdav_codec_g722_sdp_att_match,
+ tdav_codec_g722_sdp_att_get
};
const tmedia_codec_plugin_def_t *tdav_codec_g722_plugin_def_t = &tdav_codec_g722_plugin_def_s;
OpenPOWER on IntegriCloud