| 1 |
/* |
| 2 |
* Asterisk -- An open source telephony toolkit. |
| 3 |
* |
| 4 |
* The iLBC code is from The IETF code base and is copyright The Internet Society (2004) |
| 5 |
* |
| 6 |
* Copyright (C) 1999 - 2005, Digium, Inc. |
| 7 |
* |
| 8 |
* Mark Spencer <[email protected]> |
| 9 |
* |
| 10 |
* See http://www.asterisk.org for more information about |
| 11 |
* the Asterisk project. Please do not directly contact |
| 12 |
* any of the maintainers of this project for assistance; |
| 13 |
* the project provides a web site, mailing lists and IRC |
| 14 |
* channels for your use. |
| 15 |
* |
| 16 |
* This program is free software, distributed under the terms of |
| 17 |
* the GNU General Public License Version 2. See the LICENSE file |
| 18 |
* at the top of the source tree. |
| 19 |
*/ |
| 20 |
|
| 21 |
/*! \file |
| 22 |
* |
| 23 |
* \brief Translate between signed linear and Internet Low Bitrate Codec |
| 24 |
* |
| 25 |
* \ingroup codecs |
| 26 |
*/ |
| 27 |
|
| 28 |
/*** MODULEINFO |
| 29 |
<use>ilbc</use> |
| 30 |
<support_level>core</support_level> |
| 31 |
***/ |
| 32 |
|
| 33 |
#include "asterisk.h" |
| 34 |
|
| 35 |
#include "asterisk/codec.h" /* for AST_MEDIA_TYPE_AUDIO */ |
| 36 |
#include "asterisk/format.h" /* for ast_format_get_attribute_data */ |
| 37 |
#include "asterisk/frame.h" /* for ast_frame, etc */ |
| 38 |
#include "asterisk/linkedlists.h" /* for AST_LIST_NEXT, etc */ |
| 39 |
#include "asterisk/logger.h" /* for ast_log, ast_debug, etc */ |
| 40 |
#include "asterisk/module.h" |
| 41 |
#include "asterisk/translate.h" /* for ast_trans_pvt, etc */ |
| 42 |
|
| 43 |
#ifdef ILBC_WEBRTC |
| 44 |
#include <ilbc.h> |
| 45 |
typedef uint16_t ilbc_bytes; |
| 46 |
typedef int16_t ilbc_block; |
| 47 |
#define BUF_TYPE i16 |
| 48 |
#else |
| 49 |
#include "ilbc/iLBC_encode.h" |
| 50 |
#include "ilbc/iLBC_decode.h" |
| 51 |
typedef unsigned char ilbc_bytes; |
| 52 |
typedef float ilbc_block; |
| 53 |
#define BUF_TYPE uc |
| 54 |
#endif |
| 55 |
|
| 56 |
#include "asterisk/ilbc.h" |
| 57 |
|
| 58 |
#define USE_ILBC_ENHANCER 0 |
| 59 |
#define BUFFER_SAMPLES 8000 |
| 60 |
|
| 61 |
/* Sample frame data */ |
| 62 |
#include "asterisk/slin.h" |
| 63 |
#include "ex_ilbc.h" |
| 64 |
|
| 65 |
struct ilbc_coder_pvt { |
| 66 |
iLBC_Enc_Inst_t enc; |
| 67 |
iLBC_Dec_Inst_t dec; |
| 68 |
/* Enough to store a full second */ |
| 69 |
int16_t buf[BUFFER_SAMPLES]; |
| 70 |
int16_t inited; |
| 71 |
}; |
| 72 |
|
| 73 |
static int lintoilbc_new(struct ast_trans_pvt *pvt) |
| 74 |
{ |
| 75 |
struct ilbc_coder_pvt *tmp = pvt->pvt; |
| 76 |
struct ilbc_attr *attr = pvt->explicit_dst ? ast_format_get_attribute_data(pvt->explicit_dst) : NULL; |
| 77 |
const unsigned int mode = attr ? attr->mode : 30; |
| 78 |
|
| 79 |
initEncode(&tmp->enc, mode); |
| 80 |
|
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
static int ilbctolin_new(struct ast_trans_pvt *pvt) |
| 85 |
{ |
| 86 |
struct ilbc_coder_pvt *tmp = pvt->pvt; |
| 87 |
|
| 88 |
tmp->inited = 0; /* we do not know the iLBC mode, yet */ |
| 89 |
|
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
/*! \brief decode a frame and store in outbuf */ |
| 94 |
static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) |
| 95 |
{ |
| 96 |
struct ilbc_coder_pvt *tmp = pvt->pvt; |
| 97 |
struct ilbc_attr *attr = ast_format_get_attribute_data(f->subclass.format); |
| 98 |
const unsigned int mode = attr ? attr->mode : 30; |
| 99 |
const unsigned int sample_rate = pvt->t->dst_codec.sample_rate; |
| 100 |
const unsigned int samples_per_frame = mode * sample_rate / 1000; |
| 101 |
const unsigned int octets_per_frame = (mode == 20) ? 38 : 50; |
| 102 |
|
| 103 |
int plc_mode = 1; /* 1 = normal data, 0 = plc */ |
| 104 |
/* Assuming there's space left, decode into the current buffer at |
| 105 |
the tail location. Read in as many frames as there are */ |
| 106 |
int x,i; |
| 107 |
int datalen = f->datalen; |
| 108 |
int16_t *dst = pvt->outbuf.i16; |
| 109 |
ilbc_block tmpf[samples_per_frame]; |
| 110 |
|
| 111 |
if (!f->data.ptr && datalen) { |
| 112 |
ast_debug(1, "issue 16070, ILIB ERROR. data = NULL datalen = %d src = %s\n", datalen, f->src ? f->src : "no src set"); |
| 113 |
f->datalen = 0; |
| 114 |
datalen = 0; |
| 115 |
} |
| 116 |
|
| 117 |
if (datalen == 0) { /* native PLC, set fake datalen and clear plc_mode */ |
| 118 |
datalen = octets_per_frame; |
| 119 |
f->samples = samples_per_frame; |
| 120 |
plc_mode = 0; /* do native plc */ |
| 121 |
pvt->samples += samples_per_frame; |
| 122 |
} |
| 123 |
|
| 124 |
if (datalen % octets_per_frame) { |
| 125 |
ast_log(LOG_WARNING, "Huh? An ilbc frame that isn't a multiple of %u bytes long from %s (%d)?\n", octets_per_frame, f->src, datalen); |
| 126 |
return -1; |
| 127 |
} |
| 128 |
|
| 129 |
if (!tmp->inited) { |
| 130 |
initDecode(&tmp->dec, mode, USE_ILBC_ENHANCER); |
| 131 |
tmp->inited = 1; |
| 132 |
} |
| 133 |
|
| 134 |
for (x = 0; x < datalen; x += octets_per_frame) { |
| 135 |
if (pvt->samples + samples_per_frame > BUFFER_SAMPLES) { |
| 136 |
ast_log(LOG_WARNING, "Out of buffer space\n"); |
| 137 |
return -1; |
| 138 |
} |
| 139 |
iLBC_decode(tmpf, plc_mode ? f->data.ptr + x : NULL, &tmp->dec, plc_mode); |
| 140 |
for (i = 0; i < samples_per_frame; i++) |
| 141 |
dst[pvt->samples + i] = tmpf[i]; |
| 142 |
pvt->samples += samples_per_frame; |
| 143 |
pvt->datalen += samples_per_frame * 2; |
| 144 |
} |
| 145 |
return 0; |
| 146 |
} |
| 147 |
|
| 148 |
/*! \brief store a frame into a temporary buffer, for later decoding */ |
| 149 |
static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) |
| 150 |
{ |
| 151 |
struct ilbc_coder_pvt *tmp = pvt->pvt; |
| 152 |
|
| 153 |
/* Just add the frames to our stream */ |
| 154 |
/* XXX We should look at how old the rest of our stream is, and if it |
| 155 |
is too old, then we should overwrite it entirely, otherwise we can |
| 156 |
get artifacts of earlier talk that do not belong */ |
| 157 |
memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen); |
| 158 |
pvt->samples += f->samples; |
| 159 |
return 0; |
| 160 |
} |
| 161 |
|
| 162 |
/*! \brief encode the temporary buffer and generate a frame */ |
| 163 |
static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt) |
| 164 |
{ |
| 165 |
struct ilbc_coder_pvt *tmp = pvt->pvt; |
| 166 |
struct ast_frame *result = NULL; |
| 167 |
struct ast_frame *last = NULL; |
| 168 |
int samples = 0; /* output samples */ |
| 169 |
|
| 170 |
struct ilbc_attr *attr = ast_format_get_attribute_data(pvt->f.subclass.format); |
| 171 |
const unsigned int mode = attr ? attr->mode : 30; |
| 172 |
const unsigned int sample_rate = pvt->t->dst_codec.sample_rate; |
| 173 |
const unsigned int samples_per_frame = mode * sample_rate / 1000; |
| 174 |
const unsigned int octets_per_frame = (mode == 20) ? 38 : 50; |
| 175 |
|
| 176 |
while (pvt->samples >= samples_per_frame) { |
| 177 |
struct ast_frame *current; |
| 178 |
ilbc_block tmpf[samples_per_frame]; |
| 179 |
int i; |
| 180 |
|
| 181 |
/* Encode a frame of data */ |
| 182 |
for (i = 0; i < samples_per_frame; i++) |
| 183 |
tmpf[i] = tmp->buf[samples + i]; |
| 184 |
iLBC_encode((ilbc_bytes *) pvt->outbuf.BUF_TYPE, tmpf, &tmp->enc); |
| 185 |
|
| 186 |
samples += samples_per_frame; |
| 187 |
pvt->samples -= samples_per_frame; |
| 188 |
|
| 189 |
current = ast_trans_frameout(pvt, octets_per_frame, samples_per_frame); |
| 190 |
if (!current) { |
| 191 |
continue; |
| 192 |
} else if (last) { |
| 193 |
AST_LIST_NEXT(last, frame_list) = current; |
| 194 |
} else { |
| 195 |
result = current; |
| 196 |
} |
| 197 |
last = current; |
| 198 |
} |
| 199 |
|
| 200 |
/* Move the data at the end of the buffer to the front */ |
| 201 |
if (samples) { |
| 202 |
memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2); |
| 203 |
} |
| 204 |
|
| 205 |
return result; |
| 206 |
} |
| 207 |
|
| 208 |
static struct ast_translator ilbctolin = { |
| 209 |
.name = "ilbctolin", |
| 210 |
.src_codec = { |
| 211 |
.name = "ilbc", |
| 212 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 213 |
.sample_rate = 8000, |
| 214 |
}, |
| 215 |
.dst_codec = { |
| 216 |
.name = "slin", |
| 217 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 218 |
.sample_rate = 8000, |
| 219 |
}, |
| 220 |
.format = "slin", |
| 221 |
.newpvt = ilbctolin_new, |
| 222 |
.framein = ilbctolin_framein, |
| 223 |
.sample = ilbc_sample, |
| 224 |
.desc_size = sizeof(struct ilbc_coder_pvt), |
| 225 |
.buf_size = BUFFER_SAMPLES * 2, |
| 226 |
.native_plc = 1, |
| 227 |
}; |
| 228 |
|
| 229 |
static struct ast_translator lintoilbc = { |
| 230 |
.name = "lintoilbc", |
| 231 |
.src_codec = { |
| 232 |
.name = "slin", |
| 233 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 234 |
.sample_rate = 8000, |
| 235 |
}, |
| 236 |
.dst_codec = { |
| 237 |
.name = "ilbc", |
| 238 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 239 |
.sample_rate = 8000, |
| 240 |
}, |
| 241 |
.format = "ilbc", |
| 242 |
.newpvt = lintoilbc_new, |
| 243 |
.framein = lintoilbc_framein, |
| 244 |
.frameout = lintoilbc_frameout, |
| 245 |
.sample = slin8_sample, |
| 246 |
.desc_size = sizeof(struct ilbc_coder_pvt), |
| 247 |
/* frame len (38 bytes), frame size (160 samples), ceil (+ 160 - 1) */ |
| 248 |
.buf_size = (BUFFER_SAMPLES * 38 + 160 - 1) / 160, |
| 249 |
}; |
| 250 |
|
| 251 |
static int unload_module(void) |
| 252 |
{ |
| 253 |
int res; |
| 254 |
|
| 255 |
res = ast_unregister_translator(&lintoilbc); |
| 256 |
res |= ast_unregister_translator(&ilbctolin); |
| 257 |
|
| 258 |
return res; |
| 259 |
} |
| 260 |
|
| 261 |
static int load_module(void) |
| 262 |
{ |
| 263 |
int res; |
| 264 |
|
| 265 |
res = ast_register_translator(&ilbctolin); |
| 266 |
res |= ast_register_translator(&lintoilbc); |
| 267 |
|
| 268 |
if (res) { |
| 269 |
unload_module(); |
| 270 |
return AST_MODULE_LOAD_DECLINE; |
| 271 |
} |
| 272 |
|
| 273 |
return AST_MODULE_LOAD_SUCCESS; |
| 274 |
} |
| 275 |
|
| 276 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "iLBC Coder/Decoder"); |