123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include "asterisk/translate.h"
- #include "asterisk/module.h"
- #include "asterisk/utils.h"
- #include "ilbc/iLBC_encode.h"
- #include "ilbc/iLBC_decode.h"
- #define USE_ILBC_ENHANCER 0
- #define ILBC_MS 30
- #define ILBC_FRAME_LEN 50
- #define ILBC_SAMPLES 240
- #define BUFFER_SAMPLES 8000
- #include "asterisk/slin.h"
- #include "ex_ilbc.h"
- struct ilbc_coder_pvt {
- iLBC_Enc_Inst_t enc;
- iLBC_Dec_Inst_t dec;
-
- int16_t buf[BUFFER_SAMPLES];
- };
- static int lintoilbc_new(struct ast_trans_pvt *pvt)
- {
- struct ilbc_coder_pvt *tmp = pvt->pvt;
- initEncode(&tmp->enc, ILBC_MS);
- return 0;
- }
- static int ilbctolin_new(struct ast_trans_pvt *pvt)
- {
- struct ilbc_coder_pvt *tmp = pvt->pvt;
- initDecode(&tmp->dec, ILBC_MS, USE_ILBC_ENHANCER);
- return 0;
- }
- static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
- {
- struct ilbc_coder_pvt *tmp = pvt->pvt;
- int plc_mode = 1;
-
- int x,i;
- int16_t *dst = pvt->outbuf.i16;
- float tmpf[ILBC_SAMPLES];
- if (!f->data.ptr && f->datalen) {
- ast_log(LOG_DEBUG, "issue 16070, ILIB ERROR. data = NULL datalen = %d src = %s\n", f->datalen, f->src ? f->src : "no src set");
- f->datalen = 0;
- }
- if (f->datalen == 0) {
- f->datalen = ILBC_FRAME_LEN;
- f->samples = ILBC_SAMPLES;
- plc_mode = 0;
- pvt->samples += ILBC_SAMPLES;
- }
- if (f->datalen % ILBC_FRAME_LEN) {
- 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);
- return -1;
- }
-
- for (x=0; x < f->datalen ; x += ILBC_FRAME_LEN) {
- if (pvt->samples + ILBC_SAMPLES > BUFFER_SAMPLES) {
- ast_log(LOG_WARNING, "Out of buffer space\n");
- return -1;
- }
- iLBC_decode(tmpf, plc_mode ? f->data.ptr + x : NULL, &tmp->dec, plc_mode);
- for ( i=0; i < ILBC_SAMPLES; i++)
- dst[pvt->samples + i] = tmpf[i];
- pvt->samples += ILBC_SAMPLES;
- pvt->datalen += 2*ILBC_SAMPLES;
- }
- return 0;
- }
- static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
- {
- struct ilbc_coder_pvt *tmp = pvt->pvt;
-
-
- memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen);
- pvt->samples += f->samples;
- return 0;
- }
- static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
- {
- struct ilbc_coder_pvt *tmp = pvt->pvt;
- int datalen = 0;
- int samples = 0;
-
- if (pvt->samples < ILBC_SAMPLES)
- return NULL;
- while (pvt->samples >= ILBC_SAMPLES) {
- float tmpf[ILBC_SAMPLES];
- int i;
-
- for (i = 0 ; i < ILBC_SAMPLES ; i++)
- tmpf[i] = tmp->buf[samples + i];
- iLBC_encode( pvt->outbuf.uc + datalen, tmpf, &tmp->enc);
- datalen += ILBC_FRAME_LEN;
- samples += ILBC_SAMPLES;
- pvt->samples -= ILBC_SAMPLES;
- }
-
- if (pvt->samples)
- memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
- return ast_trans_frameout(pvt, datalen, samples);
- }
- static struct ast_translator ilbctolin = {
- .name = "ilbctolin",
- .srcfmt = AST_FORMAT_ILBC,
- .dstfmt = AST_FORMAT_SLINEAR,
- .newpvt = ilbctolin_new,
- .framein = ilbctolin_framein,
- .sample = ilbc_sample,
- .desc_size = sizeof(struct ilbc_coder_pvt),
- .buf_size = BUFFER_SAMPLES * 2,
- .native_plc = 1,
- };
- static struct ast_translator lintoilbc = {
- .name = "lintoilbc",
- .srcfmt = AST_FORMAT_SLINEAR,
- .dstfmt = AST_FORMAT_ILBC,
- .newpvt = lintoilbc_new,
- .framein = lintoilbc_framein,
- .frameout = lintoilbc_frameout,
- .sample = slin8_sample,
- .desc_size = sizeof(struct ilbc_coder_pvt),
- .buf_size = (BUFFER_SAMPLES * ILBC_FRAME_LEN + ILBC_SAMPLES - 1) / ILBC_SAMPLES,
- };
- static int unload_module(void)
- {
- int res;
- res = ast_unregister_translator(&lintoilbc);
- res |= ast_unregister_translator(&ilbctolin);
- return res;
- }
- static int load_module(void)
- {
- int res;
- res = ast_register_translator(&ilbctolin);
- if (!res)
- res=ast_register_translator(&lintoilbc);
- else
- ast_unregister_translator(&ilbctolin);
- if (res)
- return AST_MODULE_LOAD_FAILURE;
- return AST_MODULE_LOAD_SUCCESS;
- }
- AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "iLBC Coder/Decoder");
|