| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include "codec_gsm.h" |
| 16 |
#include "iaxclient_lib.h" |
| 17 |
#include "gsm.h" |
| 18 |
|
| 19 |
struct state { |
| 20 |
gsm gsmstate; |
| 21 |
plc_state_t plc; |
| 22 |
}; |
| 23 |
|
| 24 |
|
| 25 |
static void destroy ( struct iaxc_audio_codec *c) { |
| 26 |
|
| 27 |
struct state * encstate = (struct state *) c->encstate; |
| 28 |
struct state * decstate = (struct state *) c->decstate; |
| 29 |
|
| 30 |
gsm_destroy(encstate->gsmstate); |
| 31 |
gsm_destroy(decstate->gsmstate); |
| 32 |
free(c->encstate); |
| 33 |
free(c->decstate); |
| 34 |
free(c); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
static int decode ( struct iaxc_audio_codec *c, |
| 39 |
int *inlen, unsigned char *in, int *outlen, short *out ) { |
| 40 |
struct state * decstate = (struct state *) c->decstate; |
| 41 |
|
| 42 |
|
| 43 |
if(*inlen == 0) { |
| 44 |
int interp_len = 160; |
| 45 |
if(*outlen < interp_len) interp_len = *outlen; |
| 46 |
plc_fillin(&decstate->plc,out,interp_len); |
| 47 |
*outlen -= interp_len; |
| 48 |
return 0; |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
while( (*inlen >= 33) && (*outlen >= 160) ) { |
| 53 |
if(gsm_decode(decstate->gsmstate, in, out)) |
| 54 |
{ |
| 55 |
fprintf(stderr, "codec_gsm: gsm_decode returned error\n"); |
| 56 |
return -1; |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
plc_rx(&decstate->plc,out,160); |
| 61 |
|
| 62 |
|
| 63 |
*inlen -= 33; |
| 64 |
in += 33; |
| 65 |
*outlen -= 160; |
| 66 |
out += 160; |
| 67 |
} |
| 68 |
|
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
static int encode ( struct iaxc_audio_codec *c, |
| 73 |
int *inlen, short *in, int *outlen, unsigned char *out ) { |
| 74 |
|
| 75 |
struct state * encstate = (struct state *) c->encstate; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
while( (*inlen >= 160) && (*outlen >= 33) ) { |
| 80 |
gsm_encode(encstate->gsmstate, in, out); |
| 81 |
|
| 82 |
|
| 83 |
*inlen -= 160; |
| 84 |
in += 160; |
| 85 |
*outlen -= 33; |
| 86 |
out += 33; |
| 87 |
} |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
struct iaxc_audio_codec *codec_audio_gsm_new() { |
| 93 |
|
| 94 |
struct state * encstate; |
| 95 |
struct state * decstate; |
| 96 |
struct iaxc_audio_codec *c = (struct iaxc_audio_codec *)calloc(sizeof(struct iaxc_audio_codec),1); |
| 97 |
|
| 98 |
|
| 99 |
if(!c) return c; |
| 100 |
|
| 101 |
strcpy(c->name,"gsm 06.10"); |
| 102 |
c->format = IAXC_FORMAT_GSM; |
| 103 |
c->encode = encode; |
| 104 |
c->decode = decode; |
| 105 |
c->destroy = destroy; |
| 106 |
|
| 107 |
c->minimum_frame_size = 160; |
| 108 |
|
| 109 |
c->encstate = calloc(sizeof(struct state),1); |
| 110 |
c->decstate = calloc(sizeof(struct state),1); |
| 111 |
|
| 112 |
|
| 113 |
if(!(c->encstate && c->decstate)) |
| 114 |
return NULL; |
| 115 |
|
| 116 |
encstate = (struct state *) c->encstate; |
| 117 |
decstate = (struct state *) c->decstate; |
| 118 |
|
| 119 |
encstate->gsmstate = gsm_create(); |
| 120 |
decstate->gsmstate = gsm_create(); |
| 121 |
|
| 122 |
if(!(encstate->gsmstate && decstate->gsmstate)) |
| 123 |
return NULL; |
| 124 |
|
| 125 |
return c; |
| 126 |
} |
| 127 |
|