| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| •30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#include "asterisk.h" |
| 35 |
|
| 36 |
#include "asterisk/codec.h" |
| 37 |
#include "asterisk/format.h" |
| 38 |
#include "asterisk/frame.h" |
| 39 |
#include "asterisk/linkedlists.h" |
| 40 |
#include "asterisk/logger.h" |
| 41 |
#include "asterisk/module.h" |
| 42 |
#include "asterisk/translate.h" |
| 43 |
|
| 44 |
#ifdef ILBC_WEBRTC |
| 45 |
#include <ilbc.h> |
| 46 |
typedef uint16_t ilbc_bytes; |
| 47 |
typedef int16_t ilbc_block; |
| 48 |
#define BUF_TYPE i16 |
| 49 |
#else |
| 50 |
#include "ilbc/iLBC_encode.h" |
| 51 |
#include "ilbc/iLBC_decode.h" |
| 52 |
typedef unsigned char ilbc_bytes; |
| 53 |
typedef float ilbc_block; |
| 54 |
#define BUF_TYPE uc |
| 55 |
#endif |
| 56 |
|
| 57 |
#include "asterisk/ilbc.h" |
| 58 |
|
| 59 |
#define USE_ILBC_ENHANCER 0 |
| 60 |
#define BUFFER_SAMPLES 8000 |
| 61 |
|
| 62 |
|
| 63 |
#include "asterisk/slin.h" |
| 64 |
#include "ex_ilbc.h" |
| 65 |
|
| 66 |
struct ilbc_coder_pvt { |
| 67 |
iLBC_Enc_Inst_t enc; |
| 68 |
iLBC_Dec_Inst_t dec; |
| 69 |
|
| 70 |
int16_t buf[BUFFER_SAMPLES]; |
| 71 |
int16_t inited; |
| 72 |
}; |
| 73 |
|
| 74 |
static int lintoilbc_new(struct ast_trans_pvt *pvt) |
| 75 |
{ |
| 76 |
struct ilbc_coder_pvt *tmp = pvt->pvt; |
| 77 |
struct ilbc_attr *attr = pvt->explicit_dst ? ast_format_get_attribute_data(pvt->explicit_dst) : NULL; |
| 78 |
const unsigned int mode = attr ? attr->mode : 30; |
| 79 |
|
| 80 |
initEncode(&tmp->enc, mode); |
| 81 |
|
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
static int ilbctolin_new(struct ast_trans_pvt *pvt) |
| 86 |
{ |
| 87 |
struct ilbc_coder_pvt *tmp = pvt->pvt; |
| 88 |
|
| 89 |
tmp->inited = 0; |
| 90 |
|
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) |
| 96 |
{ |
| 97 |
struct ilbc_coder_pvt *tmp = pvt->pvt; |
| 98 |
struct ilbc_attr *attr = ast_format_get_attribute_data(f->subclass.format); |
| 99 |
const unsigned int mode = attr ? attr->mode : 30; |
| 100 |
const unsigned int sample_rate = pvt->t->dst_codec.sample_rate; |
| 101 |
const unsigned int samples_per_frame = mode * sample_rate / 1000; |
| 102 |
const unsigned int octets_per_frame = (mode == 20) ? 38 : 50; |
| 103 |
|
| 104 |
int plc_mode = 1; |
| 105 |
|
| 106 |
|
| 107 |
int x,i; |
| 108 |
int datalen = f->datalen; |
| 109 |
int16_t *dst = pvt->outbuf.i16; |
| 110 |
ilbc_block tmpf[samples_per_frame]; |
| 111 |
|
| 112 |
if (!f->data.ptr && datalen) { |
| 113 |
ast_debug(1, "issue 16070, ILIB ERROR. data = NULL datalen = %d src = %s\n", datalen, f->src ? f->src : "no src set"); |
| 114 |
f->datalen = 0; |
| 115 |
datalen = 0; |
| 116 |
} |
| 117 |
|
| 118 |
if (datalen == 0) { |
| 119 |
datalen = octets_per_frame; |
| 120 |
f->samples = samples_per_frame; |
| 121 |
plc_mode = 0; |
| 122 |
pvt->samples += samples_per_frame; |
| 123 |
} |
| 124 |
|
| 125 |
if (datalen % octets_per_frame) { |
| 126 |
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); |
| 127 |
return -1; |
| 128 |
} |
| 129 |
|
| 130 |
if (!tmp->inited) { |
| 131 |
initDecode(&tmp->dec, mode, USE_ILBC_ENHANCER); |
| 132 |
tmp->inited = 1; |
| 133 |
} |
| 134 |
|
| 135 |
for (x = 0; x < datalen; x += octets_per_frame) { |
| 136 |
if (pvt->samples + samples_per_frame > BUFFER_SAMPLES) { |
| 137 |
ast_log(LOG_WARNING, "Out of buffer space\n"); |
| 138 |
return -1; |
| 139 |
} |
| 140 |
iLBC_decode(tmpf, plc_mode ? f->data.ptr + x : NULL, &tmp->dec, plc_mode); |
| 141 |
for (i = 0; i < samples_per_frame; i++) |
| 142 |
dst[pvt->samples + i] = tmpf[i]; |
| 143 |
pvt->samples += samples_per_frame; |
| 144 |
pvt->datalen += samples_per_frame * 2; |
| 145 |
} |
| 146 |
return 0; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) |
| 151 |
{ |
| 152 |
struct ilbc_coder_pvt *tmp = pvt->pvt; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
memcpy(tmp->buf + pvt->samples, f->data.ptr, f->datalen); |
| 159 |
pvt->samples += f->samples; |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt) |
| 165 |
{ |
| 166 |
struct ilbc_coder_pvt *tmp = pvt->pvt; |
| 167 |
struct ast_frame *result = NULL; |
| 168 |
struct ast_frame *last = NULL; |
| 169 |
int samples = 0; |
| 170 |
|
| 171 |
struct ilbc_attr *attr = ast_format_get_attribute_data(pvt->f.subclass.format); |
| 172 |
const unsigned int mode = attr ? attr->mode : 30; |
| 173 |
const unsigned int sample_rate = pvt->t->dst_codec.sample_rate; |
| 174 |
const unsigned int samples_per_frame = mode * sample_rate / 1000; |
| 175 |
const unsigned int octets_per_frame = (mode == 20) ? 38 : 50; |
| 176 |
|
| 177 |
while (pvt->samples >= samples_per_frame) { |
| 178 |
struct ast_frame *current; |
| 179 |
ilbc_block tmpf[samples_per_frame]; |
| 180 |
int i; |
| 181 |
|
| 182 |
|
| 183 |
for (i = 0; i < samples_per_frame; i++) |
| 184 |
tmpf[i] = tmp->buf[samples + i]; |
| 185 |
iLBC_encode((ilbc_bytes *) pvt->outbuf.BUF_TYPE, tmpf, &tmp->enc); |
| 186 |
|
| 187 |
samples += samples_per_frame; |
| 188 |
pvt->samples -= samples_per_frame; |
| 189 |
|
| 190 |
current = ast_trans_frameout(pvt, octets_per_frame, samples_per_frame); |
| 191 |
if (!current) { |
| 192 |
continue; |
| 193 |
} else if (last) { |
| 194 |
AST_LIST_NEXT(last, frame_list) = current; |
| 195 |
} else { |
| 196 |
result = current; |
| 197 |
} |
| 198 |
last = current; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
if (samples) { |
| 203 |
memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2); |
| 204 |
} |
| 205 |
|
| 206 |
return result; |
| 207 |
} |
| 208 |
|
| 209 |
static struct ast_translator ilbctolin = { |
| 210 |
.name = "ilbctolin", |
| 211 |
.src_codec = { |
| 212 |
.name = "ilbc", |
| 213 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 214 |
.sample_rate = 8000, |
| 215 |
}, |
| 216 |
.dst_codec = { |
| 217 |
.name = "slin", |
| 218 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 219 |
.sample_rate = 8000, |
| 220 |
}, |
| 221 |
.format = "slin", |
| 222 |
.newpvt = ilbctolin_new, |
| 223 |
.framein = ilbctolin_framein, |
| 224 |
.sample = ilbc_sample, |
| 225 |
.desc_size = sizeof(struct ilbc_coder_pvt), |
| 226 |
.buf_size = BUFFER_SAMPLES * 2, |
| 227 |
.native_plc = 1, |
| 228 |
}; |
| 229 |
|
| 230 |
static struct ast_translator lintoilbc = { |
| 231 |
.name = "lintoilbc", |
| 232 |
.src_codec = { |
| 233 |
.name = "slin", |
| 234 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 235 |
.sample_rate = 8000, |
| 236 |
}, |
| 237 |
.dst_codec = { |
| 238 |
.name = "ilbc", |
| 239 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 240 |
.sample_rate = 8000, |
| 241 |
}, |
| 242 |
.format = "ilbc", |
| 243 |
.newpvt = lintoilbc_new, |
| 244 |
.framein = lintoilbc_framein, |
| 245 |
.frameout = lintoilbc_frameout, |
| 246 |
.sample = slin8_sample, |
| 247 |
.desc_size = sizeof(struct ilbc_coder_pvt), |
| 248 |
|
| 249 |
.buf_size = (BUFFER_SAMPLES * 38 + 160 - 1) / 160, |
| 250 |
}; |
| 251 |
|
| 252 |
static int unload_module(void) |
| 253 |
{ |
| 254 |
int res; |
| 255 |
|
| 256 |
res = ast_unregister_translator(&lintoilbc); |
| 257 |
res |= ast_unregister_translator(&ilbctolin); |
| 258 |
|
| 259 |
return res; |
| 260 |
} |
| 261 |
|
| 262 |
static int load_module(void) |
| 263 |
{ |
| 264 |
int res; |
| 265 |
|
| 266 |
res = ast_register_translator(&ilbctolin); |
| 267 |
res |= ast_register_translator(&lintoilbc); |
| 268 |
|
| 269 |
if (res) { |
| 270 |
unload_module(); |
| 271 |
return AST_MODULE_LOAD_DECLINE; |
| 272 |
} |
| 273 |
|
| 274 |
return AST_MODULE_LOAD_SUCCESS; |
| 275 |
} |
| 276 |
|
| 277 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "iLBC Coder/Decoder"); |