00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "asterisk.h"
00033
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 228441 $")
00035
00036 #include "asterisk/translate.h"
00037 #include "asterisk/module.h"
00038 #include "asterisk/utils.h"
00039
00040 #include "ilbc/iLBC_encode.h"
00041 #include "ilbc/iLBC_decode.h"
00042
00043 #define USE_ILBC_ENHANCER 0
00044 #define ILBC_MS 30
00045
00046
00047 #define ILBC_FRAME_LEN 50
00048 #define ILBC_SAMPLES 240
00049 #define BUFFER_SAMPLES 8000
00050
00051
00052 #include "asterisk/slin.h"
00053 #include "ex_ilbc.h"
00054
00055 struct ilbc_coder_pvt {
00056 iLBC_Enc_Inst_t enc;
00057 iLBC_Dec_Inst_t dec;
00058
00059 int16_t buf[BUFFER_SAMPLES];
00060 };
00061
00062 static int lintoilbc_new(struct ast_trans_pvt *pvt)
00063 {
00064 struct ilbc_coder_pvt *tmp = pvt->pvt;
00065
00066 initEncode(&tmp->enc, ILBC_MS);
00067
00068 return 0;
00069 }
00070
00071 static int ilbctolin_new(struct ast_trans_pvt *pvt)
00072 {
00073 struct ilbc_coder_pvt *tmp = pvt->pvt;
00074
00075 initDecode(&tmp->dec, ILBC_MS, USE_ILBC_ENHANCER);
00076
00077 return 0;
00078 }
00079
00080
00081 static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00082 {
00083 struct ilbc_coder_pvt *tmp = pvt->pvt;
00084 int plc_mode = 1;
00085
00086
00087 int x,i;
00088 int16_t *dst = pvt->outbuf.i16;
00089 float tmpf[ILBC_SAMPLES];
00090
00091 if (!f->data.ptr && f->datalen) {
00092 ast_log(LOG_DEBUG, "issue 16070, ILIB ERROR. data = NULL datalen = %d src = %s\n", f->datalen, f->src ? f->src : "no src set");
00093 f->datalen = 0;
00094 }
00095
00096 if (f->datalen == 0) {
00097 f->datalen = ILBC_FRAME_LEN;
00098 f->samples = ILBC_SAMPLES;
00099 plc_mode = 0;
00100 pvt->samples += ILBC_SAMPLES;
00101 }
00102
00103 if (f->datalen % ILBC_FRAME_LEN) {
00104 ast_log(LOG_WARNING, "Huh? An ilbc frame that isn't a multiple of 50 bytes long from %s (%d)?\n", f->src, f->datalen);
00105 return -1;
00106 }
00107
00108 for (x=0; x < f->datalen ; x += ILBC_FRAME_LEN) {
00109 if (pvt->samples + ILBC_SAMPLES > BUFFER_SAMPLES) {
00110 ast_log(LOG_WARNING, "Out of buffer space\n");
00111 return -1;
00112 }
00113 iLBC_decode(tmpf, plc_mode ? f->data.ptr + x : NULL, &tmp->dec, plc_mode);
00114 for ( i=0; i < ILBC_SAMPLES; i++)
00115 dst[pvt->samples + i] = tmpf[i];
00116 pvt->samples += ILBC_SAMPLES;
00117 pvt->datalen += 2*ILBC_SAMPLES;
00118 }
00119 return 0;
00120 }
00121
00122
00123 static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
00124 {
00125 struct ilbc_coder_pvt *tmp = pvt->pvt;
00126
00127
00128
00129
00130
00131 memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
00132 pvt->samples += f->samples;
00133 return 0;
00134 }
00135
00136
00137 static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
00138 {
00139 struct ilbc_coder_pvt *tmp = pvt->pvt;
00140 int datalen = 0;
00141 int samples = 0;
00142
00143
00144 if (pvt->samples < ILBC_SAMPLES)
00145 return NULL;
00146 while (pvt->samples >= ILBC_SAMPLES) {
00147 float tmpf[ILBC_SAMPLES];
00148 int i;
00149
00150
00151 for (i = 0 ; i < ILBC_SAMPLES ; i++)
00152 tmpf[i] = tmp->buf[samples + i];
00153 iLBC_encode( pvt->outbuf.uc + datalen, tmpf, &tmp->enc);
00154
00155 datalen += ILBC_FRAME_LEN;
00156 samples += ILBC_SAMPLES;
00157 pvt->samples -= ILBC_SAMPLES;
00158 }
00159
00160
00161 if (pvt->samples)
00162 memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
00163
00164 return ast_trans_frameout(pvt, datalen, samples);
00165 }
00166
00167 static struct ast_translator ilbctolin = {
00168 .name = "ilbctolin",
00169 .srcfmt = AST_FORMAT_ILBC,
00170 .dstfmt = AST_FORMAT_SLINEAR,
00171 .newpvt = ilbctolin_new,
00172 .framein = ilbctolin_framein,
00173 .sample = ilbc_sample,
00174 .desc_size = sizeof(struct ilbc_coder_pvt),
00175 .buf_size = BUFFER_SAMPLES * 2,
00176 .native_plc = 1,
00177 };
00178
00179 static struct ast_translator lintoilbc = {
00180 .name = "lintoilbc",
00181 .srcfmt = AST_FORMAT_SLINEAR,
00182 .dstfmt = AST_FORMAT_ILBC,
00183 .newpvt = lintoilbc_new,
00184 .framein = lintoilbc_framein,
00185 .frameout = lintoilbc_frameout,
00186 .sample = slin8_sample,
00187 .desc_size = sizeof(struct ilbc_coder_pvt),
00188 .buf_size = (BUFFER_SAMPLES * ILBC_FRAME_LEN + ILBC_SAMPLES - 1) / ILBC_SAMPLES,
00189 };
00190
00191 static int unload_module(void)
00192 {
00193 int res;
00194
00195 res = ast_unregister_translator(&lintoilbc);
00196 res |= ast_unregister_translator(&ilbctolin);
00197
00198 return res;
00199 }
00200
00201 static int load_module(void)
00202 {
00203 int res;
00204
00205 res = ast_register_translator(&ilbctolin);
00206 if (!res)
00207 res=ast_register_translator(&lintoilbc);
00208 else
00209 ast_unregister_translator(&ilbctolin);
00210 if (res)
00211 return AST_MODULE_LOAD_FAILURE;
00212 return AST_MODULE_LOAD_SUCCESS;
00213 }
00214
00215 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "iLBC Coder/Decoder");