git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@418 d0543943-73ff-0310-b7d9-9358b9ac24b2
This commit is contained in:
Anthony Minessale
2006-01-20 02:02:03 +00:00
parent 1e04eb13b5
commit 0ea203849f
43 changed files with 3940 additions and 4103 deletions

View File

@@ -47,71 +47,70 @@
*/
#define SIGN_BIT (0x80) /* Sign bit for a A-law byte. */
#define QUANT_MASK (0xf) /* Quantization field mask. */
#define NSEGS (8) /* Number of A-law segments. */
#define SEG_SHIFT (4) /* Left shift for segment number. */
#define NSEGS (8) /* Number of A-law segments. */
#define SEG_SHIFT (4) /* Left shift for segment number. */
#define SEG_MASK (0x70) /* Segment field mask. */
static short seg_aend[8] = { 0x1F, 0x3F, 0x7F, 0xFF,
0x1FF, 0x3FF, 0x7FF, 0xFFF
};
static short seg_uend[8] = { 0x3F, 0x7F, 0xFF, 0x1FF,
0x3FF, 0x7FF, 0xFFF, 0x1FFF
};
static short seg_aend[8] = {0x1F, 0x3F, 0x7F, 0xFF,
0x1FF, 0x3FF, 0x7FF, 0xFFF};
static short seg_uend[8] = {0x3F, 0x7F, 0xFF, 0x1FF,
0x3FF, 0x7FF, 0xFFF, 0x1FFF};
/* copy from CCITT G.711 specifications */
unsigned char _u2a[128] = { /* u- to A-law conversions */
1, 1, 2, 2, 3, 3, 4, 4,
5, 5, 6, 6, 7, 7, 8, 8,
9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24,
25, 27, 29, 31, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44,
46, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, 62,
64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79,
unsigned char _u2a[128] = { /* u- to A-law conversions */
1, 1, 2, 2, 3, 3, 4, 4,
5, 5, 6, 6, 7, 7, 8, 8,
9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24,
25, 27, 29, 31, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44,
46, 48, 49, 50, 51, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, 62,
64, 65, 66, 67, 68, 69, 70, 71,
72, 73, 74, 75, 76, 77, 78, 79,
/* corrected:
81, 82, 83, 84, 85, 86, 87, 88,
should be: */
80, 82, 83, 84, 85, 86, 87, 88,
89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99, 100, 101, 102, 103, 104,
105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 123, 124, 125, 126, 127, 128
};
80, 82, 83, 84, 85, 86, 87, 88,
89, 90, 91, 92, 93, 94, 95, 96,
97, 98, 99, 100, 101, 102, 103, 104,
105, 106, 107, 108, 109, 110, 111, 112,
113, 114, 115, 116, 117, 118, 119, 120,
121, 122, 123, 124, 125, 126, 127, 128};
unsigned char _a2u[128] = { /* A- to u-law conversions */
1, 3, 5, 7, 9, 11, 13, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
32, 32, 33, 33, 34, 34, 35, 35,
36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 48, 49, 49,
50, 51, 52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 62, 63, 64, 64,
65, 66, 67, 68, 69, 70, 71, 72,
unsigned char _a2u[128] = { /* A- to u-law conversions */
1, 3, 5, 7, 9, 11, 13, 15,
16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31,
32, 32, 33, 33, 34, 34, 35, 35,
36, 37, 38, 39, 40, 41, 42, 43,
44, 45, 46, 47, 48, 48, 49, 49,
50, 51, 52, 53, 54, 55, 56, 57,
58, 59, 60, 61, 62, 63, 64, 64,
65, 66, 67, 68, 69, 70, 71, 72,
/* corrected:
73, 74, 75, 76, 77, 78, 79, 79,
should be: */
73, 74, 75, 76, 77, 78, 79, 80,
80, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99, 100, 101, 102, 103,
104, 105, 106, 107, 108, 109, 110, 111,
112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127
};
73, 74, 75, 76, 77, 78, 79, 80,
80, 81, 82, 83, 84, 85, 86, 87,
88, 89, 90, 91, 92, 93, 94, 95,
96, 97, 98, 99, 100, 101, 102, 103,
104, 105, 106, 107, 108, 109, 110, 111,
112, 113, 114, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127};
static short search(short val, short *table, short size)
static short search(
short val,
short *table,
short size)
{
short i;
for (i = 0; i < size; i++) {
if (val <= *table++)
return (i);
}
return (size);
short i;
for (i = 0; i < size; i++) {
if (val <= *table++)
return (i);
}
return (size);
}
/*
@@ -133,63 +132,66 @@ static short search(short val, short *table, short size)
* For further information see John C. Bellamy's Digital Telephony, 1982,
* John Wiley & Sons, pps 98-111 and 472-476.
*/
unsigned char linear2alaw(short pcm_val)
{ /* 2's complement (16-bit range) */
short mask;
short seg;
unsigned char aval;
unsigned char
linear2alaw(short pcm_val) /* 2's complement (16-bit range) */
{
short mask;
short seg;
unsigned char aval;
pcm_val = pcm_val >> 3;
pcm_val = pcm_val >> 3;
if (pcm_val >= 0) {
mask = 0xD5; /* sign (7th) bit = 1 */
} else {
mask = 0x55; /* sign bit = 0 */
pcm_val = -pcm_val - 1;
}
/* Convert the scaled magnitude to segment number. */
seg = search(pcm_val, seg_aend, 8);
/* Combine the sign, segment, and quantization bits. */
if (seg >= 8) /* out of range, return maximum value. */
return (unsigned char) (0x7F ^ mask);
else {
aval = (unsigned char) seg << SEG_SHIFT;
if (seg < 2)
aval |= (pcm_val >> 1) & QUANT_MASK;
else
aval |= (pcm_val >> seg) & QUANT_MASK;
return (aval ^ mask);
}
if (pcm_val >= 0) {
mask = 0xD5; /* sign (7th) bit = 1 */
} else {
mask = 0x55; /* sign bit = 0 */
pcm_val = -pcm_val - 1;
}
/* Convert the scaled magnitude to segment number. */
seg = search(pcm_val, seg_aend, 8);
/* Combine the sign, segment, and quantization bits. */
if (seg >= 8) /* out of range, return maximum value. */
return (unsigned char) (0x7F ^ mask);
else {
aval = (unsigned char) seg << SEG_SHIFT;
if (seg < 2)
aval |= (pcm_val >> 1) & QUANT_MASK;
else
aval |= (pcm_val >> seg) & QUANT_MASK;
return (aval ^ mask);
}
}
/*
* alaw2linear() - Convert an A-law value to 16-bit linear PCM
*
*/
short alaw2linear(unsigned char a_val)
short
alaw2linear(
unsigned char a_val)
{
short t;
short seg;
a_val ^= 0x55;
t = (a_val & QUANT_MASK) << 4;
seg = ((unsigned) a_val & SEG_MASK) >> SEG_SHIFT;
switch (seg) {
case 0:
t += 8;
break;
case 1:
t += 0x108;
break;
default:
t += 0x108;
t <<= seg - 1;
}
return ((a_val & SIGN_BIT) ? t : -t);
short t;
short seg;
a_val ^= 0x55;
t = (a_val & QUANT_MASK) << 4;
seg = ((unsigned)a_val & SEG_MASK) >> SEG_SHIFT;
switch (seg) {
case 0:
t += 8;
break;
case 1:
t += 0x108;
break;
default:
t += 0x108;
t <<= seg - 1;
}
return ((a_val & SIGN_BIT) ? t : -t);
}
#define BIAS (0x84) /* Bias for linear code. */
@@ -224,38 +226,39 @@ short alaw2linear(unsigned char a_val)
* For further information see John C. Bellamy's Digital Telephony, 1982,
* John Wiley & Sons, pps 98-111 and 472-476.
*/
unsigned char linear2ulaw(short pcm_val)
{ /* 2's complement (16-bit range) */
short mask;
short seg;
unsigned char uval;
/* Get the sign and the magnitude of the value. */
pcm_val = pcm_val >> 2;
if (pcm_val < 0) {
pcm_val = -pcm_val;
mask = 0x7F;
} else {
mask = 0xFF;
}
if (pcm_val > CLIP)
pcm_val = CLIP; /* clip the magnitude */
pcm_val += (BIAS >> 2);
/* Convert the scaled magnitude to segment number. */
seg = search(pcm_val, seg_uend, 8);
/*
* Combine the sign, segment, quantization bits;
* and complement the code word.
*/
if (seg >= 8) /* out of range, return maximum value. */
return (unsigned char) (0x7F ^ mask);
else {
uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
return (uval ^ mask);
}
unsigned char
linear2ulaw(
short pcm_val) /* 2's complement (16-bit range) */
{
short mask;
short seg;
unsigned char uval;
/* Get the sign and the magnitude of the value. */
pcm_val = pcm_val >> 2;
if (pcm_val < 0) {
pcm_val = -pcm_val;
mask = 0x7F;
} else {
mask = 0xFF;
}
if ( pcm_val > CLIP ) pcm_val = CLIP; /* clip the magnitude */
pcm_val += (BIAS >> 2);
/* Convert the scaled magnitude to segment number. */
seg = search(pcm_val, seg_uend, 8);
/*
* Combine the sign, segment, quantization bits;
* and complement the code word.
*/
if (seg >= 8) /* out of range, return maximum value. */
return (unsigned char) (0x7F ^ mask);
else {
uval = (unsigned char) (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF);
return (uval ^ mask);
}
}
/*
@@ -267,35 +270,43 @@ unsigned char linear2ulaw(short pcm_val)
* Note that this function expects to be passed the complement of the
* original code word. This is in keeping with ISDN conventions.
*/
short ulaw2linear(unsigned char u_val)
short
ulaw2linear(
unsigned char u_val)
{
short t;
/* Complement to obtain normal u-law value. */
u_val = ~u_val;
/*
* Extract and bias the quantization bits. Then
* shift up by the segment number and subtract out the bias.
*/
t = ((u_val & QUANT_MASK) << 3) + BIAS;
t <<= ((unsigned) u_val & SEG_MASK) >> SEG_SHIFT;
return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
short t;
/* Complement to obtain normal u-law value. */
u_val = ~u_val;
/*
* Extract and bias the quantization bits. Then
* shift up by the segment number and subtract out the bias.
*/
t = ((u_val & QUANT_MASK) << 3) + BIAS;
t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
}
/* A-law to u-law conversion */
unsigned char alaw2ulaw(unsigned char aval)
unsigned char
alaw2ulaw(
unsigned char aval)
{
aval &= 0xff;
return (unsigned char) ((aval & 0x80) ? (0xFF ^ _a2u[aval ^ 0xD5]) : (0x7F ^ _a2u[aval ^ 0x55]));
aval &= 0xff;
return (unsigned char) ((aval & 0x80) ? (0xFF ^ _a2u[aval ^ 0xD5]) :
(0x7F ^ _a2u[aval ^ 0x55]));
}
/* u-law to A-law conversion */
unsigned char ulaw2alaw(unsigned char uval)
unsigned char
ulaw2alaw(
unsigned char uval)
{
uval &= 0xff;
return (unsigned char) ((uval & 0x80) ? (0xD5 ^ (_u2a[0xFF ^ uval] - 1)) : (0x55 ^ (_u2a[0x7F ^ uval] - 1)));
uval &= 0xff;
return (unsigned char) ((uval & 0x80) ? (0xD5 ^ (_u2a[0xFF ^ uval] - 1)) :
(0x55 ^ (_u2a[0x7F ^ uval] - 1)));
}
/* ---------- end of g711.c ----------------------------------------------------- */

View File

@@ -36,8 +36,7 @@
static const char modname[] = "mod_g711codec";
static switch_status switch_g711u_init(switch_codec *codec, switch_codec_flag flags,
const struct switch_codec_settings *codec_settings)
static switch_status switch_g711u_init(switch_codec *codec, switch_codec_flag flags, const struct switch_codec_settings *codec_settings)
{
int encoding, decoding;
@@ -58,7 +57,9 @@ static switch_status switch_g711u_encode(switch_codec *codec,
size_t decoded_data_len,
int decoded_rate,
void *encoded_data,
size_t *encoded_data_len, int *encoded_rate, unsigned int *flag)
size_t *encoded_data_len,
int *encoded_rate,
unsigned int *flag)
{
short *dbuf;
unsigned char *ebuf;
@@ -82,7 +83,9 @@ static switch_status switch_g711u_decode(switch_codec *codec,
size_t encoded_data_len,
int encoded_rate,
void *decoded_data,
size_t *decoded_data_len, int *decoded_rate, unsigned int *flag)
size_t *decoded_data_len,
int *decoded_rate,
unsigned int *flag)
{
short *dbuf;
unsigned char *ebuf;
@@ -111,8 +114,7 @@ static switch_status switch_g711u_destroy(switch_codec *codec)
}
static switch_status switch_g711a_init(switch_codec *codec, switch_codec_flag flags,
const struct switch_codec_settings *codec_settings)
static switch_status switch_g711a_init(switch_codec *codec, switch_codec_flag flags, const struct switch_codec_settings *codec_settings)
{
int encoding, decoding;
@@ -133,7 +135,9 @@ static switch_status switch_g711a_encode(switch_codec *codec,
size_t decoded_data_len,
int decoded_rate,
void *encoded_data,
size_t *encoded_data_len, int *encoded_rate, unsigned int *flag)
size_t *encoded_data_len,
int *encoded_rate,
unsigned int *flag)
{
short *dbuf;
unsigned char *ebuf;
@@ -157,7 +161,9 @@ static switch_status switch_g711a_decode(switch_codec *codec,
size_t encoded_data_len,
int encoded_rate,
void *decoded_data,
size_t *decoded_data_len, int *decoded_rate, unsigned int *flag)
size_t *decoded_data_len,
int *decoded_rate,
unsigned int *flag)
{
short *dbuf;
unsigned char *ebuf;
@@ -189,122 +195,126 @@ static switch_status switch_g711a_destroy(switch_codec *codec)
static const switch_codec_implementation g711u_8k_60ms_implementation = {
/*.samples_per_second */ 8000,
/*.bits_per_second */ 19200,
/*.microseconds_per_frame */ 60000,
/*.samples_per_frame */ 480,
/*.bytes_per_frame */ 960,
/*.encoded_bytes_per_frame */ 480,
/*.number_of_channels */ 1,
/*.pref_frames_per_packet */ 1,
/*.max_frames_per_packet */ 1,
/*.init */ switch_g711u_init,
/*.encode */ switch_g711u_encode,
/*.decode */ switch_g711u_decode,
/*.destroy */ switch_g711u_destroy
/*.samples_per_second*/ 8000,
/*.bits_per_second*/ 19200,
/*.microseconds_per_frame*/ 60000,
/*.samples_per_frame*/ 480,
/*.bytes_per_frame*/ 960,
/*.encoded_bytes_per_frame*/ 480,
/*.number_of_channels*/ 1,
/*.pref_frames_per_packet*/ 1,
/*.max_frames_per_packet*/ 1,
/*.init*/ switch_g711u_init,
/*.encode*/ switch_g711u_encode,
/*.decode*/ switch_g711u_decode,
/*.destroy*/ switch_g711u_destroy
};
static const switch_codec_implementation g711u_8k_30ms_implementation = {
/*.samples_per_second */ 8000,
/*.bits_per_second */ 96000,
/*.microseconds_per_frame */ 30000,
/*.samples_per_frame */ 240,
/*.bytes_per_frame */ 480,
/*.encoded_bytes_per_frame */ 240,
/*.number_of_channels */ 1,
/*.pref_frames_per_packet */ 1,
/*.max_frames_per_packet */ 1,
/*.init */ switch_g711u_init,
/*.encode */ switch_g711u_encode,
/*.decode */ switch_g711u_decode,
/*.destroy */ switch_g711u_destroy,
/*.next */ &g711u_8k_60ms_implementation
/*.samples_per_second*/ 8000,
/*.bits_per_second*/ 96000,
/*.microseconds_per_frame*/ 30000,
/*.samples_per_frame*/ 240,
/*.bytes_per_frame*/ 480,
/*.encoded_bytes_per_frame*/ 240,
/*.number_of_channels*/ 1,
/*.pref_frames_per_packet*/ 1,
/*.max_frames_per_packet*/ 1,
/*.init*/ switch_g711u_init,
/*.encode*/ switch_g711u_encode,
/*.decode*/ switch_g711u_decode,
/*.destroy*/ switch_g711u_destroy,
/*.next*/ &g711u_8k_60ms_implementation
};
static const switch_codec_implementation g711u_16k_implementation = {
/*.samples_per_second */ 16000,
/*.bits_per_second */ 128000,
/*.microseconds_per_frame */ 20000,
/*.samples_per_frame */ 320,
/*.bytes_per_frame */ 640,
/*.encoded_bytes_per_frame */ 320,
/*.number_of_channels */ 1,
/*.pref_frames_per_packet */ 1,
/*.max_frames_per_packet */ 1,
/*.init */ switch_g711u_init,
/*.encode */ switch_g711u_encode,
/*.decode */ switch_g711u_decode,
/*.destroy */ switch_g711u_destroy,
/*.samples_per_second*/ 16000,
/*.bits_per_second*/ 128000,
/*.microseconds_per_frame*/ 20000,
/*.samples_per_frame*/ 320,
/*.bytes_per_frame*/ 640,
/*.encoded_bytes_per_frame*/ 320,
/*.number_of_channels*/ 1,
/*.pref_frames_per_packet*/ 1,
/*.max_frames_per_packet*/ 1,
/*.init*/ switch_g711u_init,
/*.encode*/ switch_g711u_encode,
/*.decode*/ switch_g711u_decode,
/*.destroy*/ switch_g711u_destroy,
};
static const switch_codec_implementation g711u_8k_implementation = {
/*.samples_per_second */ 8000,
/*.bits_per_second */ 64000,
/*.microseconds_per_frame */ 20000,
/*.samples_per_frame */ 160,
/*.bytes_per_frame */ 320,
/*.encoded_bytes_per_frame */ 160,
/*.number_of_channels */ 1,
/*.pref_frames_per_packet */ 1,
/*.max_frames_per_packet */ 1,
/*.init */ switch_g711u_init,
/*.encode */ switch_g711u_encode,
/*.decode */ switch_g711u_decode,
/*.destroy */ switch_g711u_destroy,
/*.next */ &g711u_16k_implementation
/*.samples_per_second*/ 8000,
/*.bits_per_second*/ 64000,
/*.microseconds_per_frame*/ 20000,
/*.samples_per_frame*/ 160,
/*.bytes_per_frame*/ 320,
/*.encoded_bytes_per_frame*/ 160,
/*.number_of_channels*/ 1,
/*.pref_frames_per_packet*/ 1,
/*.max_frames_per_packet*/ 1,
/*.init*/ switch_g711u_init,
/*.encode*/ switch_g711u_encode,
/*.decode*/ switch_g711u_decode,
/*.destroy*/ switch_g711u_destroy,
/*.next*/ &g711u_16k_implementation
};
static const switch_codec_implementation g711a_8k_implementation = {
/*.samples_per_second */ 8000,
/*.bits_per_second */ 64000,
/*.microseconds_per_frame */ 20000,
/*.samples_per_frame */ 160,
/*.bytes_per_frame */ 320,
/*.encoded_bytes_per_frame */ 160,
/*.number_of_channels */ 1,
/*.pref_frames_per_packet */ 1,
/*.max_frames_per_packet */ 1,
/*.init */ switch_g711a_init,
/*.encode */ switch_g711a_encode,
/*.decode */ switch_g711a_decode,
/*.destroy */ switch_g711a_destroy
/*.samples_per_second*/ 8000,
/*.bits_per_second*/ 64000,
/*.microseconds_per_frame*/ 20000,
/*.samples_per_frame*/ 160,
/*.bytes_per_frame*/ 320,
/*.encoded_bytes_per_frame*/ 160,
/*.number_of_channels*/ 1,
/*.pref_frames_per_packet*/ 1,
/*.max_frames_per_packet*/ 1,
/*.init*/ switch_g711a_init,
/*.encode*/ switch_g711a_encode,
/*.decode*/ switch_g711a_decode,
/*.destroy*/ switch_g711a_destroy
};
static const switch_codec_interface g711a_codec_interface = {
/*.interface_name */ "g711 alaw",
/*.codec_type */ SWITCH_CODEC_TYPE_AUDIO,
/*.ianacode */ 8,
/*.iananame */ "PCMA",
/*.implementations */ &g711a_8k_implementation
/*.interface_name*/ "g711 alaw",
/*.codec_type*/ SWITCH_CODEC_TYPE_AUDIO,
/*.ianacode*/ 8,
/*.iananame*/ "PCMA",
/*.implementations*/ &g711a_8k_implementation
};
static const switch_codec_interface g711u_codec_interface = {
/*.interface_name */ "g711 ulaw",
/*.codec_type */ SWITCH_CODEC_TYPE_AUDIO,
/*.ianacode */ 0,
/*.iananame */ "PCMU",
/*.implementations */ &g711u_8k_implementation,
/*.next */ &g711a_codec_interface
/*.interface_name*/ "g711 ulaw",
/*.codec_type*/ SWITCH_CODEC_TYPE_AUDIO,
/*.ianacode*/ 0,
/*.iananame*/ "PCMU",
/*.implementations*/ &g711u_8k_implementation,
/*.next*/ &g711a_codec_interface
};
static switch_loadable_module_interface g711_module_interface = {
/*.module_name */ modname,
/*.endpoint_interface */ NULL,
/*.timer_interface */ NULL,
/*.dialplan_interface */ NULL,
/*.codec_interface */ &g711u_codec_interface,
/*.application_interface */ NULL
/*.module_name*/ modname,
/*.endpoint_interface*/ NULL,
/*.timer_interface*/ NULL,
/*.dialplan_interface*/ NULL,
/*.codec_interface*/ &g711u_codec_interface,
/*.application_interface*/ NULL
};
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename)
{
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename) {
/* connect my internal structure to the blank pointer passed to me */
*interface = &g711_module_interface;
/* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS;
}

View File

@@ -28,142 +28,176 @@
*
* mod_codec_g729.c -- G729 Codec Module
*
*/
*/
#include "switch.h"
#include "g729.h"
static const char modname[] = "mod_codec_g729";
struct g729_context {
struct dec_state decoder_object;
struct cod_state encoder_object;
};
static switch_status switch_g729_init(switch_codec *codec, switch_codec_flag flags,
const struct switch_codec_settings *codec_settings)
{
struct g729_context *context = NULL;
int encoding, decoding;
encoding = (flags & SWITCH_CODEC_FLAG_ENCODE);
decoding = (flags & SWITCH_CODEC_FLAG_DECODE);
if (!(encoding || decoding) || (!(context = switch_core_alloc(codec->memory_pool, sizeof(struct g729_context))))) {
return SWITCH_STATUS_FALSE;
} else {
if (encoding) {
g729_init_coder(&context->encoder_object, 0);
}
if (decoding) {
g729_init_decoder(&context->decoder_object);
}
codec->private = context;
return SWITCH_STATUS_SUCCESS;
}
}
static switch_status switch_g729_destroy(switch_codec *codec)
{
codec->private = NULL;
return SWITCH_STATUS_SUCCESS;
}
static switch_status switch_g729_encode(switch_codec *codec,
switch_codec *other_codec,
void *decoded_data,
size_t decoded_data_len,
int decoded_rate,
void *encoded_data,
size_t *encoded_data_len,
int *encoded_rate,
unsigned int *flag)
{
struct g729_context *context = codec->private;
int cbret = 0;
if (!context) {
return SWITCH_STATUS_FALSE;
}
if (decoded_data_len % 160 == 0) {
unsigned int new_len = 0;
INT16 * ddp = decoded_data;
char *edp = encoded_data;
int x;
int loops = (int) decoded_data_len / 160;
for (x = 0; x < loops && new_len < *encoded_data_len; x++) {
g729_coder(&context->encoder_object, ddp, edp, &cbret);
edp += 10;
ddp += 80;
new_len += 10;
}
if (new_len <= *encoded_data_len) {
*encoded_data_len = new_len;
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "buffer overflow!!! %u >= %u\n", new_len, *encoded_data_len);
return SWITCH_STATUS_FALSE;
}
}
return SWITCH_STATUS_SUCCESS;
}
static const char modname[] = "mod_codec_g729";
struct g729_context {
struct dec_state decoder_object;
struct cod_state encoder_object;
};
static switch_status switch_g729_init(switch_codec *codec, switch_codec_flag flags, const struct switch_codec_settings *codec_settings)
{
struct g729_context *context = NULL;
int encoding, decoding;
encoding = (flags & SWITCH_CODEC_FLAG_ENCODE);
decoding = (flags & SWITCH_CODEC_FLAG_DECODE);
if (!(encoding || decoding) || (!(context = switch_core_alloc(codec->memory_pool, sizeof(struct g729_context))))) {
return SWITCH_STATUS_FALSE;
} else {
if (encoding) {
g729_init_coder(&context->encoder_object, 0);
}
if (decoding) {
g729_init_decoder(&context->decoder_object);
}
codec->private = context;
return SWITCH_STATUS_SUCCESS;
}
}
static switch_status switch_g729_destroy(switch_codec *codec)
{
codec->private = NULL;
return SWITCH_STATUS_SUCCESS;
}
static switch_status switch_g729_encode(switch_codec *codec,
switch_codec *other_codec,
void *decoded_data,
size_t decoded_data_len,
int decoded_rate,
void *encoded_data,
size_t *encoded_data_len,
int *encoded_rate,
unsigned int *flag)
{
struct g729_context *context = codec->private;
int cbret = 0;
if (!context) {
return SWITCH_STATUS_FALSE;
}
if (decoded_data_len % 160 == 0) {
unsigned int new_len = 0;
INT16 *ddp = decoded_data;
char *edp = encoded_data;
int x;
int loops = (int) decoded_data_len / 160;
for(x = 0; x < loops && new_len < *encoded_data_len; x++) {
g729_coder(&context->encoder_object, ddp, edp, &cbret);
edp += 10;
ddp += 80;
new_len += 10;
}
if( new_len <= *encoded_data_len ) {
*encoded_data_len = new_len;
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "buffer overflow!!! %u >= %u\n", new_len, *encoded_data_len);
return SWITCH_STATUS_FALSE;
}
}
return SWITCH_STATUS_SUCCESS;
}
static switch_status switch_g729_decode(switch_codec *codec,
switch_codec *other_codec,
void *encoded_data,
size_t encoded_data_len,
int encoded_rate,
void *decoded_data,
size_t *decoded_data_len,
int *decoded_rate,
unsigned int *flag)
{
struct g729_context *context = codec->private;
if (!context) {
return SWITCH_STATUS_FALSE;
}
if (encoded_data_len % 10 == 0) {
int loops = (int) encoded_data_len / 10;
char *edp = encoded_data;
short *ddp = decoded_data;
int x;
unsigned int new_len = 0;
for(x = 0; x < loops && new_len < *decoded_data_len; x++) {
g729_decoder(&context->decoder_object, ddp, edp, 10);
ddp += 80;
edp += 10;
new_len += 160;
}
if (new_len <= *decoded_data_len) {
*decoded_data_len = new_len;
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "buffer overflow!!!\n");
return SWITCH_STATUS_FALSE;
}
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "yo this frame is an odd size [%d]\n", encoded_data_len);
}
return SWITCH_STATUS_SUCCESS;
}
/* Registration */
static const switch_codec_implementation g729_8k_implementation = {
/*.samples_per_second*/ 8000,
/*.bits_per_second*/ 64000,
/*.microseconds_per_frame*/ 20000,
/*.samples_per_frame*/ 160,
/*.bytes_per_frame*/ 320,
/*.encoded_bytes_per_frame*/ 20,
/*.number_of_channels*/ 1,
/*.pref_frames_per_packet*/ 1,
/*.max_frames_per_packet*/ 24,
/*.init*/ switch_g729_init,
/*.encode*/ switch_g729_encode,
/*.decode*/ switch_g729_decode,
/*.destroy*/ switch_g729_destroy,
};
static const switch_codec_interface g729_codec_interface = {
/*.interface_name*/ "g729",
/*.codec_type*/ SWITCH_CODEC_TYPE_AUDIO,
/*.ianacode*/ 18,
/*.iananame*/ "G729",
/*.implementations*/ &g729_8k_implementation,
};
static switch_loadable_module_interface g729_module_interface = {
/*.module_name*/ modname,
/*.endpoint_interface*/ NULL,
/*.timer_interface*/ NULL,
/*.dialplan_interface*/ NULL,
/*.codec_interface*/ &g729_codec_interface,
/*.application_interface*/ NULL
};
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename) {
/* connect my internal structure to the blank pointer passed to me */
*interface = &g729_module_interface;
/* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS;
}

View File

@@ -28,148 +28,180 @@
*
* mod_codec_gsm.c -- gsm Codec Module
*
*/
*/
#include "switch.h"
#include "gsm.h"
static const char modname[] = "mod_codec_gsm";
struct gsm_context {
gsm encoder;
gsm decoder;
};
static switch_status switch_gsm_init(switch_codec *codec, switch_codec_flag flags,
const struct switch_codec_settings *codec_settings)
{
struct gsm_context *context;
int encoding, decoding;
encoding = (flags & SWITCH_CODEC_FLAG_ENCODE);
decoding = (flags & SWITCH_CODEC_FLAG_DECODE);
if (!(encoding || decoding)) {
return SWITCH_STATUS_FALSE;
} else {
context = switch_core_alloc(codec->memory_pool, sizeof(*context));
if (encoding)
context->encoder = gsm_create();
if (decoding)
context->decoder = gsm_create();
}
codec->private = context;
return SWITCH_STATUS_SUCCESS;
}
static switch_status switch_gsm_destroy(switch_codec *codec)
{
struct gsm_context *context = codec->private;
int encoding = (codec->flags & SWITCH_CODEC_FLAG_ENCODE);
int decoding = (codec->flags & SWITCH_CODEC_FLAG_DECODE);
if (encoding)
gsm_destroy(context->encoder);
if (decoding)
gsm_destroy(context->decoder);
codec->private = NULL;
return SWITCH_STATUS_SUCCESS;
}
static switch_status switch_gsm_encode(switch_codec *codec,
switch_codec *other_codec,
void *decoded_data,
size_t decoded_data_len,
int decoded_rate,
void *encoded_data,
size_t *encoded_data_len,
int *encoded_rate,
unsigned int *flag)
{
struct gsm_context *context = codec->private;
int cbret = 0;
if (!context) {
return SWITCH_STATUS_FALSE;
}
if (decoded_data_len % 320 == 0) {
unsigned int new_len = 0;
gsm_signal * ddp = decoded_data;
gsm_byte * edp = encoded_data;
int x;
int loops = (int) decoded_data_len / 320;
for (x = 0; x < loops && new_len < *encoded_data_len; x++) {
gsm_encode(context->encoder, ddp, edp);
edp += 33;
ddp += 160;
new_len += 33;
}
if (new_len <= *encoded_data_len) {
*encoded_data_len = new_len;
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "buffer overflow!!! %u >= %u\n", new_len, *encoded_data_len);
return SWITCH_STATUS_FALSE;
}
}
return SWITCH_STATUS_SUCCESS;
static const char modname[] = "mod_codec_gsm";
struct gsm_context {
gsm encoder;
gsm decoder;
};
static switch_status switch_gsm_init(switch_codec *codec, switch_codec_flag flags, const struct switch_codec_settings *codec_settings)
{
struct gsm_context *context;
int encoding, decoding;
encoding = (flags & SWITCH_CODEC_FLAG_ENCODE);
decoding = (flags & SWITCH_CODEC_FLAG_DECODE);
if (!(encoding || decoding)) {
return SWITCH_STATUS_FALSE;
} else {
context = switch_core_alloc(codec->memory_pool, sizeof(*context));
if (encoding) context->encoder = gsm_create();
if (decoding) context->decoder = gsm_create();
}
codec->private = context;
return SWITCH_STATUS_SUCCESS;
}
static switch_status switch_gsm_destroy(switch_codec *codec)
{
struct gsm_context *context = codec->private;
int encoding = (codec->flags & SWITCH_CODEC_FLAG_ENCODE);
int decoding = (codec->flags & SWITCH_CODEC_FLAG_DECODE);
if (encoding) gsm_destroy(context->encoder);
if (decoding) gsm_destroy(context->decoder);
codec->private = NULL;
return SWITCH_STATUS_SUCCESS;
}
static switch_status switch_gsm_encode(switch_codec *codec,
switch_codec *other_codec,
void *decoded_data,
size_t decoded_data_len,
int decoded_rate,
void *encoded_data,
size_t *encoded_data_len,
int *encoded_rate,
unsigned int *flag)
{
struct gsm_context *context = codec->private;
int cbret = 0;
if (!context) {
return SWITCH_STATUS_FALSE;
}
if (decoded_data_len % 320 == 0) {
unsigned int new_len = 0;
gsm_signal *ddp = decoded_data;
gsm_byte *edp = encoded_data;
int x;
int loops = (int) decoded_data_len / 320;
for(x = 0; x < loops && new_len < *encoded_data_len; x++) {
gsm_encode(context->encoder, ddp, edp);
edp += 33;
ddp += 160;
new_len += 33;
}
if( new_len <= *encoded_data_len ) {
*encoded_data_len = new_len;
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "buffer overflow!!! %u >= %u\n", new_len, *encoded_data_len);
return SWITCH_STATUS_FALSE;
}
}
return SWITCH_STATUS_SUCCESS;
}
static switch_status switch_gsm_decode(switch_codec *codec,
switch_codec *other_codec,
void *encoded_data,
size_t encoded_data_len,
int encoded_rate,
void *decoded_data,
size_t *decoded_data_len,
int *decoded_rate,
unsigned int *flag)
{
struct gsm_context *context = codec->private;
if (!context) {
return SWITCH_STATUS_FALSE;
}
if (encoded_data_len % 33 == 0) {
int loops = (int) encoded_data_len / 33;
gsm_byte *edp = encoded_data;
gsm_signal *ddp = decoded_data;
int x;
unsigned int new_len = 0;
for(x = 0; x < loops && new_len < *decoded_data_len; x++) {
gsm_decode(context->decoder, edp, ddp);
ddp += 160;
edp += 33;
new_len += 320;
}
if (new_len <= *decoded_data_len) {
*decoded_data_len = new_len;
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "buffer overflow!!!\n");
return SWITCH_STATUS_FALSE;
}
} else {
switch_console_printf(SWITCH_CHANNEL_CONSOLE, "yo this frame is an odd size [%d]\n", encoded_data_len);
}
return SWITCH_STATUS_SUCCESS;
}
/* Registration */
static const switch_codec_implementation gsm_8k_implementation = {
/*.samples_per_second*/ 8000,
/*.bits_per_second*/ 13200,
/*.microseconds_per_frame*/ 20000,
/*.samples_per_frame*/ 160,
/*.bytes_per_frame*/ 320,
/*.encoded_bytes_per_frame*/ 33,
/*.number_of_channels*/ 1,
/*.pref_frames_per_packet*/ 1,
/*.max_frames_per_packet*/ 1,
/*.init*/ switch_gsm_init,
/*.encode*/ switch_gsm_encode,
/*.decode*/ switch_gsm_decode,
/*.destroy*/ switch_gsm_destroy,
};
static const switch_codec_interface gsm_codec_interface = {
/*.interface_name*/ "gsm",
/*.codec_type*/ SWITCH_CODEC_TYPE_AUDIO,
/*.ianacode*/ 3,
/*.iananame*/ "gsm",
/*.implementations*/ &gsm_8k_implementation,
};
static switch_loadable_module_interface gsm_module_interface = {
/*.module_name*/ modname,
/*.endpoint_interface*/ NULL,
/*.timer_interface*/ NULL,
/*.dialplan_interface*/ NULL,
/*.codec_interface*/ &gsm_codec_interface,
/*.application_interface*/ NULL
};
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename) {
/* connect my internal structure to the blank pointer passed to me */
*interface = &gsm_module_interface;
/* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS;
}

View File

@@ -35,8 +35,7 @@
static const char modname[] = "mod_rawaudio";
static switch_status switch_raw_init(switch_codec *codec, switch_codec_flag flags,
const struct switch_codec_settings *codec_settings)
static switch_status switch_raw_init(switch_codec *codec, switch_codec_flag flags, const struct switch_codec_settings *codec_settings)
{
int encoding, decoding;
struct raw_context *context = NULL;
@@ -52,15 +51,17 @@ static switch_status switch_raw_init(switch_codec *codec, switch_codec_flag flag
}
static switch_status switch_raw_encode(switch_codec *codec,
switch_codec *other_codec,
void *decoded_data,
size_t decoded_data_len,
int decoded_rate,
void *encoded_data,
size_t *encoded_data_len, int *encoded_rate, unsigned int *flag)
switch_codec *other_codec,
void *decoded_data,
size_t decoded_data_len,
int decoded_rate,
void *encoded_data,
size_t *encoded_data_len,
int *encoded_rate,
unsigned int *flag)
{
/* NOOP indicates that the audio in is already the same as the audio out, so no conversion was necessary. */
/* NOOP indicates that the audio in is already the same as the audio out, so no conversion was necessary.*/
if (codec->implementation->samples_per_second != other_codec->implementation->samples_per_second) {
memcpy(encoded_data, decoded_data, decoded_data_len);
*encoded_data_len = decoded_data_len;
@@ -70,12 +71,14 @@ static switch_status switch_raw_encode(switch_codec *codec,
}
static switch_status switch_raw_decode(switch_codec *codec,
switch_codec *other_codec,
void *encoded_data,
size_t encoded_data_len,
int encoded_rate,
void *decoded_data,
size_t *decoded_data_len, int *decoded_rate, unsigned int *flag)
switch_codec *other_codec,
void *encoded_data,
size_t encoded_data_len,
int encoded_rate,
void *decoded_data,
size_t *decoded_data_len,
int *decoded_rate,
unsigned int *flag)
{
if (codec->implementation->samples_per_second != other_codec->implementation->samples_per_second) {
memcpy(decoded_data, encoded_data, encoded_data_len);
@@ -88,12 +91,12 @@ static switch_status switch_raw_decode(switch_codec *codec,
static switch_status switch_raw_destroy(switch_codec *codec)
{
return SWITCH_STATUS_SUCCESS;
}
static const switch_codec_implementation raw_32k_implementation = {
/*.samples_per_second = */ 32000,
/*.samples_per_second = */ 32000,
/*.bits_per_second = */ 512000,
/*.microseconds_per_frame = */ 20000,
/*.samples_per_frame = */ 640,
@@ -161,48 +164,52 @@ static const switch_codec_implementation raw_8k_implementation = {
static const switch_codec_implementation raw_8k_30ms_implementation = {
/*.samples_per_second */ 8000,
/*.bits_per_second */ 128000,
/*.microseconds_per_frame */ 30000,
/*.samples_per_frame */ 240,
/*.bytes_per_frame */ 480,
/*.encoded_bytes_per_frame */ 480,
/*.number_of_channels */ 1,
/*.pref_frames_per_packet */ 1,
/*.max_frames_per_packet */ 1,
/*.init */ switch_raw_init,
/*.encode */ switch_raw_encode,
/*.decode */ switch_raw_decode,
/*.destroy */ switch_raw_destroy,
/*.next */ &raw_8k_implementation
/*.samples_per_second*/ 8000,
/*.bits_per_second*/ 128000,
/*.microseconds_per_frame*/ 30000,
/*.samples_per_frame*/ 240,
/*.bytes_per_frame*/ 480,
/*.encoded_bytes_per_frame*/ 480,
/*.number_of_channels*/ 1,
/*.pref_frames_per_packet*/ 1,
/*.max_frames_per_packet*/ 1,
/*.init*/ switch_raw_init,
/*.encode*/ switch_raw_encode,
/*.decode*/ switch_raw_decode,
/*.destroy*/ switch_raw_destroy,
/*.next*/ &raw_8k_implementation
};
static const switch_codec_interface raw_codec_interface = {
/*.interface_name */ "raw signed linear (16 bit)",
/*.codec_type */ SWITCH_CODEC_TYPE_AUDIO,
/*.ianacode */ 10,
/*.iananame */ "L16",
/*.implementations */ &raw_8k_30ms_implementation
/*.interface_name*/ "raw signed linear (16 bit)",
/*.codec_type*/ SWITCH_CODEC_TYPE_AUDIO,
/*.ianacode*/ 10,
/*.iananame*/ "L16",
/*.implementations*/ &raw_8k_30ms_implementation
};
static switch_loadable_module_interface raw_module_interface = {
/*.module_name */ modname,
/*.endpoint_interface */ NULL,
/*.timer_interface */ NULL,
/*.dialplan_interface */ NULL,
/*.codec_interface */ &raw_codec_interface,
/*.application_interface */ NULL,
/*.api_interface */ NULL,
///*.file_interface*/ &raw_file_interface
/*.module_name*/ modname,
/*.endpoint_interface*/ NULL,
/*.timer_interface*/ NULL,
/*.dialplan_interface*/ NULL,
/*.codec_interface*/ &raw_codec_interface,
/*.application_interface*/ NULL,
/*.api_interface*/ NULL,
///*.file_interface*/ &raw_file_interface
};
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename)
{
/* connect my internal structure to the blank pointer passed to me */
SWITCH_MOD_DECLARE(switch_status) switch_module_load(const switch_loadable_module_interface **interface, char *filename) {
/* connect my internal structure to the blank pointer passed to me */
*interface = &raw_module_interface;
/* indicate that the module should continue to be loaded */
/* indicate that the module should continue to be loaded */
return SWITCH_STATUS_SUCCESS;
}

View File

@@ -36,22 +36,22 @@
static const char modname[] = "mod_speexcodec";
const struct switch_codec_settings default_codec_settings = {
/*.quality */ 5,
/*.complexity */ 5,
/*.enhancement */ 1,
/*.vad */ 0,
/*.vbr */ 0,
/*.vbr_quality */ 4,
/*.abr */ 0,
/*.dtx */ 0,
/*.preproc */ 0,
/*.pp_vad */ 0,
/*.pp_agc */ 0,
/*.pp_agc_level */ 8000,
/*.pp_denoise */ 0,
/*.pp_dereverb */ 0,
/*.pp_dereverb_decay */ 0.4f,
/*.pp_dereverb_level */ 0.3f,
/*.quality*/ 5,
/*.complexity*/ 5,
/*.enhancement*/ 1,
/*.vad*/ 0,
/*.vbr*/ 0,
/*.vbr_quality*/ 4,
/*.abr*/ 0,
/*.dtx*/ 0,
/*.preproc*/ 0,
/*.pp_vad*/ 0,
/*.pp_agc*/ 0,
/*.pp_agc_level*/ 8000,
/*.pp_denoise*/ 0,
/*.pp_dereverb*/ 0,
/*.pp_dereverb_decay*/ 0.4f,
/*.pp_dereverb_level*/ 0.3f,
};
struct speex_context {
@@ -72,12 +72,11 @@ struct speex_context {
int decoder_mode;
};
static switch_status switch_speex_init(switch_codec *codec, switch_codec_flag flags,
const struct switch_codec_settings *codec_settings)
static switch_status switch_speex_init(switch_codec *codec, switch_codec_flag flags, const struct switch_codec_settings *codec_settings)
{
struct speex_context *context = NULL;
int encoding, decoding;
encoding = (flags & SWITCH_CODEC_FLAG_ENCODE);
decoding = (flags & SWITCH_CODEC_FLAG_DECODE);
@@ -91,7 +90,7 @@ static switch_status switch_speex_init(switch_codec *codec, switch_codec_flag fl
return SWITCH_STATUS_FALSE;
} else {
const SpeexMode *mode = NULL;
context->codec = codec;
if (codec->implementation->samples_per_second == 8000) {
mode = &speex_nb_mode;
@@ -111,17 +110,14 @@ static switch_status switch_speex_init(switch_codec *codec, switch_codec_flag fl
speex_encoder_ctl(context->encoder_state, SPEEX_GET_FRAME_SIZE, &context->encoder_frame_size);
speex_encoder_ctl(context->encoder_state, SPEEX_SET_COMPLEXITY, &codec->codec_settings.complexity);
if (codec->codec_settings.preproc) {
context->pp =
speex_preprocess_state_init(context->encoder_frame_size, codec->implementation->samples_per_second);
context->pp = speex_preprocess_state_init(context->encoder_frame_size, codec->implementation->samples_per_second);
speex_preprocess_ctl(context->pp, SPEEX_PREPROCESS_SET_VAD, &codec->codec_settings.pp_vad);
speex_preprocess_ctl(context->pp, SPEEX_PREPROCESS_SET_AGC, &codec->codec_settings.pp_agc);
speex_preprocess_ctl(context->pp, SPEEX_PREPROCESS_SET_AGC_LEVEL, &codec->codec_settings.pp_agc_level);
speex_preprocess_ctl(context->pp, SPEEX_PREPROCESS_SET_DENOISE, &codec->codec_settings.pp_denoise);
speex_preprocess_ctl(context->pp, SPEEX_PREPROCESS_SET_DEREVERB, &codec->codec_settings.pp_dereverb);
speex_preprocess_ctl(context->pp, SPEEX_PREPROCESS_SET_DEREVERB_DECAY,
&codec->codec_settings.pp_dereverb_decay);
speex_preprocess_ctl(context->pp, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL,
&codec->codec_settings.pp_dereverb_level);
speex_preprocess_ctl(context->pp, SPEEX_PREPROCESS_SET_DEREVERB_DECAY, &codec->codec_settings.pp_dereverb_decay);
speex_preprocess_ctl(context->pp, SPEEX_PREPROCESS_SET_DEREVERB_LEVEL, &codec->codec_settings.pp_dereverb_level);
}
if (!codec->codec_settings.abr && !codec->codec_settings.vbr) {
@@ -138,8 +134,8 @@ static switch_status switch_speex_init(switch_codec *codec, switch_codec_flag fl
speex_encoder_ctl(context->encoder_state, SPEEX_SET_ABR, &codec->codec_settings.abr);
}
if (codec->codec_settings.dtx) {
speex_encoder_ctl(context->encoder_state, SPEEX_SET_DTX, &codec->codec_settings.dtx);
}
speex_encoder_ctl(context->encoder_state, SPEEX_SET_DTX, &codec->codec_settings.dtx);
}
}
if (decoding) {
@@ -158,12 +154,14 @@ static switch_status switch_speex_init(switch_codec *codec, switch_codec_flag fl
}
static switch_status switch_speex_encode(switch_codec *codec,
switch_codec *other_codec,
void *decoded_data,
size_t decoded_data_len,
int decoded_rate,
void *encoded_data,
size_t *encoded_data_len, int *encoded_rate, unsigned int *flag)
switch_codec *other_codec,
void *decoded_data,
size_t decoded_data_len,
int decoded_rate,
void *encoded_data,
size_t *encoded_data_len,
int *encoded_rate,
unsigned int *flag)
{
struct speex_context *context = codec->private;
short *buf;
@@ -180,8 +178,7 @@ static switch_status switch_speex_encode(switch_codec *codec,
}
if (is_speech) {
is_speech = speex_encode_int(context->encoder_state, buf, &context->encoder_bits)
|| !context->codec->codec_settings.dtx;
is_speech = speex_encode_int(context->encoder_state, buf, &context->encoder_bits) || ! context->codec->codec_settings.dtx;
} else {
speex_bits_pack(&context->encoder_bits, 0, 5);
}
@@ -205,7 +202,7 @@ static switch_status switch_speex_encode(switch_codec *codec,
speex_bits_pack(&context->encoder_bits, 15, 5);
*encoded_data_len = speex_bits_write(&context->encoder_bits, (char *) encoded_data, context->encoder_frame_size);
*encoded_data_len = speex_bits_write(&context->encoder_bits, (char *)encoded_data, context->encoder_frame_size);
speex_bits_reset(&context->encoder_bits);
@@ -213,12 +210,14 @@ static switch_status switch_speex_encode(switch_codec *codec,
}
static switch_status switch_speex_decode(switch_codec *codec,
switch_codec *other_codec,
void *encoded_data,
size_t encoded_data_len,
int encoded_rate,
void *decoded_data,
size_t *decoded_data_len, int *decoded_rate, unsigned int *flag)
switch_codec *other_codec,
void *encoded_data,
size_t encoded_data_len,
int encoded_rate,
void *decoded_data,
size_t *decoded_data_len,
int *decoded_rate,
unsigned int *flag)
{
struct speex_context *context = codec->private;
short *buf;
@@ -231,10 +230,10 @@ static switch_status switch_speex_decode(switch_codec *codec,
if (*flag & SWITCH_CODEC_FLAG_SILENCE) {
speex_decode_int(context->decoder_state, NULL, buf);
} else {
speex_bits_read_from(&context->decoder_bits, (char *) encoded_data, (int) *decoded_data_len);
speex_bits_read_from(&context->decoder_bits, (char *)encoded_data, (int)*decoded_data_len);
speex_decode_int(context->decoder_state, &context->decoder_bits, buf);
}
return SWITCH_STATUS_SUCCESS;
}
@@ -262,80 +261,79 @@ static switch_status switch_speex_destroy(switch_codec *codec)
}
codec->private = NULL;
return SWITCH_STATUS_SUCCESS;
}
/* Registration */
static const switch_codec_implementation speex_32k_implementation = {
/*.samples_per_second */ 32000,
/*.bits_per_second */ 512000,
/*.nanoseconds_per_frame */ 20000,
/*.samples_per_frame */ 640,
/*.bytes_per_frame */ 1280,
/*.encoded_bytes_per_frame */ 1280,
/*.number_of_channels */ 1,
/*.pref_frames_per_packet */ 1,
/*.max_frames_per_packet */ 1,
/*.init */ switch_speex_init,
/*.encode */ switch_speex_encode,
/*.decode */ switch_speex_decode,
/*.destroy */ switch_speex_destroy
/*.samples_per_second*/ 32000,
/*.bits_per_second*/ 512000,
/*.nanoseconds_per_frame*/ 20000,
/*.samples_per_frame*/ 640,
/*.bytes_per_frame*/ 1280,
/*.encoded_bytes_per_frame*/ 1280,
/*.number_of_channels*/ 1,
/*.pref_frames_per_packet*/ 1,
/*.max_frames_per_packet*/ 1,
/*.init*/ switch_speex_init,
/*.encode*/ switch_speex_encode,
/*.decode*/ switch_speex_decode,
/*.destroy*/ switch_speex_destroy
};
static const switch_codec_implementation speex_16k_implementation = {
/*.samples_per_second */ 16000,
/*.bits_per_second */ 256000,
/*.nanoseconds_per_frame */ 20000,
/*.samples_per_frame */ 320,
/*.bytes_per_frame */ 640,
/*.encoded_bytes_per_frame */ 640,
/*.number_of_channels */ 1,
/*.pref_frames_per_packet */ 1,
/*.max_frames_per_packet */ 1,
/*.init */ switch_speex_init,
/*.encode */ switch_speex_encode,
/*.decode */ switch_speex_decode,
/*.destroy */ switch_speex_destroy,
/*.next */ &speex_32k_implementation
/*.samples_per_second*/ 16000,
/*.bits_per_second*/ 256000,
/*.nanoseconds_per_frame*/ 20000,
/*.samples_per_frame*/ 320,
/*.bytes_per_frame*/ 640,
/*.encoded_bytes_per_frame*/ 640,
/*.number_of_channels*/ 1,
/*.pref_frames_per_packet*/ 1,
/*.max_frames_per_packet*/ 1,
/*.init*/ switch_speex_init,
/*.encode*/ switch_speex_encode,
/*.decode*/ switch_speex_decode,
/*.destroy*/ switch_speex_destroy,
/*.next*/ &speex_32k_implementation
};
static const switch_codec_implementation speex_8k_implementation = {
/*.samples_per_second */ 8000,
/*.bits_per_second */ 128000,
/*.nanoseconds_per_frame */ 20000,
/*.samples_per_frame */ 160,
/*.bytes_per_frame */ 320,
/*.encoded_bytes_per_frame */ 320,
/*.number_of_channels */ 1,
/*.pref_frames_per_packet */ 1,
/*.max_frames_per_packet */ 1,
/*.init */ switch_speex_init,
/*.encode */ switch_speex_encode,
/*.decode */ switch_speex_decode,
/*.destroy */ switch_speex_destroy,
/*.next */ &speex_16k_implementation
/*.samples_per_second*/ 8000,
/*.bits_per_second*/ 128000,
/*.nanoseconds_per_frame*/ 20000,
/*.samples_per_frame*/ 160,
/*.bytes_per_frame*/ 320,
/*.encoded_bytes_per_frame*/ 320,
/*.number_of_channels*/ 1,
/*.pref_frames_per_packet*/ 1,
/*.max_frames_per_packet*/ 1,
/*.init*/ switch_speex_init,
/*.encode*/ switch_speex_encode,
/*.decode*/ switch_speex_decode,
/*.destroy*/ switch_speex_destroy,
/*.next*/ &speex_16k_implementation
};
static const switch_codec_interface speex_codec_interface = {
/*.interface_name */ "speex",
/*.codec_type */ SWITCH_CODEC_TYPE_AUDIO,
/*.ianacode */ 98,
/*.iananame */ "speex",
/*.implementations */ &speex_8k_implementation
/*.interface_name*/ "speex",
/*.codec_type*/ SWITCH_CODEC_TYPE_AUDIO,
/*.ianacode*/ 98,
/*.iananame*/ "speex",
/*.implementations*/ &speex_8k_implementation
};
static switch_loadable_module_interface speex_module_interface = {
/*.module_name */ modname,
/*.endpoint_interface */ NULL,
/*.timer_interface */ NULL,
/*.dialplan_interface */ NULL,
/*.codec_interface */ &speex_codec_interface,
/*.application_interface */ NULL
/*.module_name*/ modname,
/*.endpoint_interface*/ NULL,
/*.timer_interface*/ NULL,
/*.dialplan_interface*/ NULL,
/*.codec_interface*/ &speex_codec_interface,
/*.application_interface*/ NULL
};
SWITCH_MOD_DECLARE(switch_status) switch_module_load(switch_loadable_module_interface **interface, char *filename)
{
SWITCH_MOD_DECLARE(switch_status) switch_module_load(switch_loadable_module_interface **interface, char *filename) {
/* connect my internal structure to the blank pointer passed to me */
*interface = &speex_module_interface;