| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include "asterisk.h" |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include "asterisk/codec.h" |
| 13 |
#include "asterisk/format.h" |
| 14 |
#include "asterisk/frame.h" |
| 15 |
#include "asterisk/linkedlists.h" |
| 16 |
#include "asterisk/logger.h" |
| 17 |
#include "asterisk/module.h" |
| 18 |
#include "asterisk/translate.h" |
| 19 |
|
| 20 |
#include <opencore-amrnb/interf_dec.h> |
| 21 |
#include <opencore-amrnb/interf_enc.h> |
| 22 |
#include <opencore-amrwb/dec_if.h> |
| 23 |
#include <vo-amrwbenc/enc_if.h> |
| 24 |
|
| 25 |
#include "asterisk/amr.h" |
| 26 |
|
| 27 |
#define BUFFER_SAMPLES 16000 |
| 28 |
|
| 29 |
|
| 30 |
#include "asterisk/slin.h" |
| 31 |
#include "ex_amr.h" |
| 32 |
|
| 33 |
struct amr_coder_pvt { |
| 34 |
void *state; |
| 35 |
unsigned int frames; |
| 36 |
int16_t buf[BUFFER_SAMPLES]; |
| 37 |
}; |
| 38 |
|
| 39 |
static int lintoamr_new(struct ast_trans_pvt *pvt) |
| 40 |
{ |
| 41 |
struct amr_coder_pvt *apvt = pvt->pvt; |
| 42 |
const unsigned int sample_rate = pvt->t->src_codec.sample_rate; |
| 43 |
|
| 44 |
struct amr_attr *attr = pvt->explicit_dst ? ast_format_get_attribute_data(pvt->explicit_dst) : NULL; |
| 45 |
const int dtx = attr ? attr->vad : 0; |
| 46 |
|
| 47 |
if (8000 == sample_rate) { |
| 48 |
apvt->state = Encoder_Interface_init(dtx); |
| 49 |
} else if (16000 == sample_rate) { |
| 50 |
apvt->state = E_IF_init(); |
| 51 |
} |
| 52 |
|
| 53 |
if (!apvt->state) { |
| 54 |
ast_log(LOG_ERROR, "Error creating the AMR encoder for %d\n", sample_rate); |
| 55 |
return -1; |
| 56 |
} |
| 57 |
|
| 58 |
apvt->frames = 0; |
| 59 |
ast_debug(3, "Created encoder (%d -> AMR) %p (Format %p)\n", sample_rate, apvt, pvt->explicit_dst); |
| 60 |
|
| 61 |
return 0; |
| 62 |
} |
| 63 |
|
| 64 |
static int amrtolin_new(struct ast_trans_pvt *pvt) |
| 65 |
{ |
| 66 |
struct amr_coder_pvt *apvt = pvt->pvt; |
| 67 |
const unsigned int sample_rate = pvt->t->dst_codec.sample_rate; |
| 68 |
|
| 69 |
if (8000 == sample_rate) { |
| 70 |
apvt->state = Decoder_Interface_init(); |
| 71 |
} else if (16000 == sample_rate) { |
| 72 |
apvt->state = D_IF_init(); |
| 73 |
} |
| 74 |
|
| 75 |
if (!apvt->state) { |
| 76 |
ast_log(LOG_ERROR, "Error creating the AMR decoder for %d\n", sample_rate); |
| 77 |
return -1; |
| 78 |
} |
| 79 |
|
| 80 |
apvt->frames = 0; |
| 81 |
ast_debug(3, "Created decoder (AMR -> %d) %p\n", sample_rate, apvt); |
| 82 |
|
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
static int lintoamr_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) |
| 87 |
{ |
| 88 |
struct amr_coder_pvt *apvt = pvt->pvt; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
memcpy(apvt->buf + pvt->samples, f->data.ptr, f->datalen); |
| 94 |
pvt->samples += f->samples; |
| 95 |
|
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
static struct ast_frame *lintoamr_frameout(struct ast_trans_pvt *pvt) |
| 100 |
{ |
| 101 |
struct amr_coder_pvt *apvt = pvt->pvt; |
| 102 |
const unsigned int sample_rate = pvt->t->src_codec.sample_rate; |
| 103 |
const unsigned int frame_size = sample_rate / 50; |
| 104 |
struct ast_frame *result = NULL; |
| 105 |
struct ast_frame *last = NULL; |
| 106 |
int samples = 0; |
| 107 |
|
| 108 |
struct amr_attr *attr = ast_format_get_attribute_data(pvt->f.subclass.format); |
| 109 |
const int dtx = attr ? attr->vad : 0; |
| 110 |
const int mode = attr ? attr->mode_current : 0; |
| 111 |
const int aligned = attr ? attr->octet_align : 0; |
| 112 |
|
| 113 |
while (pvt->samples >= frame_size) { |
| 114 |
struct ast_frame *current; |
| 115 |
const int force_speech = 0; |
| 116 |
const short *speech = apvt->buf + samples; |
| 117 |
unsigned char *out = pvt->outbuf.uc + 1; |
| 118 |
int status = -1; |
| 119 |
|
| 120 |
if (8000 == sample_rate) { |
| 121 |
status = Encoder_Interface_Encode(apvt->state, mode, speech, out, force_speech); |
| 122 |
} else if (16000 == sample_rate) { |
| 123 |
status = E_IF_encode(apvt->state, mode, speech, out, dtx); |
| 124 |
} |
| 125 |
|
| 126 |
samples += frame_size; |
| 127 |
pvt->samples -= frame_size; |
| 128 |
|
| 129 |
if (status <= 0) { |
| 130 |
ast_log(LOG_ERROR, "Error encoding the AMR frame\n"); |
| 131 |
current = NULL; |
| 132 |
} else if (((out[0] >> 3) & 0x0f) == 15) { |
| 133 |
current = NULL; |
| 134 |
} else if (aligned) { |
| 135 |
pvt->outbuf.uc[0] = (15 << 4); |
| 136 |
|
| 137 |
current = ast_trans_frameout(pvt, status + 1, frame_size); |
| 138 |
} else { |
| 139 |
const int another = ((out[0] >> 7) & 0x01); |
| 140 |
const int type = ((out[0] >> 3) & 0x0f); |
| 141 |
const int quality = ((out[0] >> 2) & 0x01); |
| 142 |
unsigned int i; |
| 143 |
|
| 144 |
|
| 145 |
out[0] = 0; |
| 146 |
out[status] = 0; |
| 147 |
|
| 148 |
for (i = 0; i < status; i++) { |
| 149 |
out[i] = ((out[i] << 6) | (out[i + 1] >> 2)); |
| 150 |
} |
| 151 |
|
| 152 |
out[0] |= ((type << 7) | (quality << 6)); |
| 153 |
pvt->outbuf.uc[0] = ((15 << 4) | (another << 3) | (type >> 1)); |
| 154 |
|
| 155 |
if (8000 == sample_rate) { |
| 156 |
|
| 157 |
const int octets[16] = { 14, 15, 16, 18, 20, 22, 27, 32, 7 }; |
| 158 |
|
| 159 |
status = octets[type]; |
| 160 |
} else if (16000 == sample_rate) { |
| 161 |
|
| 162 |
const int octets[16] = { 18, 24, 33, 37, 41, 47, 51, 59, 61, 7 }; |
| 163 |
|
| 164 |
status = octets[type]; |
| 165 |
} |
| 166 |
|
| 167 |
current = ast_trans_frameout(pvt, status, frame_size); |
| 168 |
} |
| 169 |
|
| 170 |
if (!current) { |
| 171 |
continue; |
| 172 |
} else if (last) { |
| 173 |
AST_LIST_NEXT(last, frame_list) = current; |
| 174 |
} else { |
| 175 |
result = current; |
| 176 |
} |
| 177 |
last = current; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
if (samples) { |
| 182 |
memmove(apvt->buf, apvt->buf + samples, pvt->samples * 2); |
| 183 |
} |
| 184 |
|
| 185 |
return result; |
| 186 |
} |
| 187 |
|
| 188 |
static int amrtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) |
| 189 |
{ |
| 190 |
struct amr_coder_pvt *apvt = pvt->pvt; |
| 191 |
const unsigned int sample_rate = pvt->t->dst_codec.sample_rate; |
| 192 |
const unsigned int frame_size = sample_rate / 50; |
| 193 |
|
| 194 |
struct amr_attr *attr = ast_format_get_attribute_data(f->subclass.format); |
| 195 |
const int aligned = attr ? attr->octet_align : 0; |
| 196 |
const unsigned char mode_next = *(unsigned char *) f->data.ptr >> 4; |
| 197 |
const int bad_frame = 0; |
| 198 |
unsigned char temp[f->datalen]; |
| 199 |
unsigned char *in = f->data.ptr; |
| 200 |
|
| 201 |
if (attr) { |
| 202 |
if (8000 == sample_rate && mode_next <= 7) { |
| 203 |
attr->mode_current = mode_next; |
| 204 |
} else if (16000 == sample_rate && mode_next <= 8) { |
| 205 |
attr->mode_current = mode_next; |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
if (aligned) { |
| 217 |
in++; |
| 218 |
} else { |
| 219 |
const int another = ((in[0] >> 3) & 0x01); |
| 220 |
const int type = ((in[0] << 1 | in[1] >> 7) & 0x0f); |
| 221 |
const int quality = ((in[1] >> 6) & 0x01); |
| 222 |
unsigned int i; |
| 223 |
|
| 224 |
|
| 225 |
for (i = 1; i < (f->datalen - 1); i++) { |
| 226 |
temp[i] = ((in[i] << 2) | (in[i + 1] >> 6)); |
| 227 |
} |
| 228 |
temp[f->datalen - 1] = in[f->datalen - 1] << 2; |
| 229 |
|
| 230 |
temp[0] = ((another << 7) | (type << 3) | (quality << 2)); |
| 231 |
in = temp; |
| 232 |
} |
| 233 |
|
| 234 |
if ((apvt->frames == 0) && (in[0] & 0x80)) { |
| 235 |
apvt->frames = 1; |
| 236 |
ast_log(LOG_WARNING, "multiple frames per packet were not tested\n"); |
| 237 |
} |
| 238 |
|
| 239 |
if (8000 == sample_rate) { |
| 240 |
Decoder_Interface_Decode(apvt->state, in, pvt->outbuf.i16 + pvt->datalen, bad_frame); |
| 241 |
} else if (16000 == sample_rate) { |
| 242 |
D_IF_decode(apvt->state, in, pvt->outbuf.i16 + pvt->datalen, bad_frame); |
| 243 |
} |
| 244 |
|
| 245 |
pvt->samples += frame_size; |
| 246 |
pvt->datalen += frame_size * 2; |
| 247 |
|
| 248 |
return 0; |
| 249 |
} |
| 250 |
|
| 251 |
static void lintoamr_destroy(struct ast_trans_pvt *pvt) |
| 252 |
{ |
| 253 |
struct amr_coder_pvt *apvt = pvt->pvt; |
| 254 |
const unsigned int sample_rate = pvt->t->src_codec.sample_rate; |
| 255 |
|
| 256 |
if (!apvt || !apvt->state) { |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
if (8000 == sample_rate) { |
| 261 |
Encoder_Interface_exit(apvt->state); |
| 262 |
} else if (16000 == sample_rate) { |
| 263 |
E_IF_exit(apvt->state); |
| 264 |
} |
| 265 |
apvt->state = NULL; |
| 266 |
|
| 267 |
ast_debug(3, "Destroyed encoder (%d -> AMR) %p\n", sample_rate, apvt); |
| 268 |
} |
| 269 |
|
| 270 |
static void amrtolin_destroy(struct ast_trans_pvt *pvt) |
| 271 |
{ |
| 272 |
struct amr_coder_pvt *apvt = pvt->pvt; |
| 273 |
const unsigned int sample_rate = pvt->t->dst_codec.sample_rate; |
| 274 |
|
| 275 |
if (!apvt || !apvt->state) { |
| 276 |
return; |
| 277 |
} |
| 278 |
|
| 279 |
if (8000 == sample_rate) { |
| 280 |
Decoder_Interface_exit(apvt->state); |
| 281 |
} else if (16000 == sample_rate) { |
| 282 |
D_IF_exit(apvt->state); |
| 283 |
} |
| 284 |
apvt->state = NULL; |
| 285 |
|
| 286 |
ast_debug(3, "Destroyed decoder (AMR -> %d) %p\n", sample_rate, apvt); |
| 287 |
} |
| 288 |
|
| 289 |
static struct ast_translator amrtolin = { |
| 290 |
.name = "amrtolin", |
| 291 |
.src_codec = { |
| 292 |
.name = "amr", |
| 293 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 294 |
.sample_rate = 8000, |
| 295 |
}, |
| 296 |
.dst_codec = { |
| 297 |
.name = "slin", |
| 298 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 299 |
.sample_rate = 8000, |
| 300 |
}, |
| 301 |
.format = "slin", |
| 302 |
.newpvt = amrtolin_new, |
| 303 |
.framein = amrtolin_framein, |
| 304 |
.destroy = amrtolin_destroy, |
| 305 |
.sample = amr_sample, |
| 306 |
.desc_size = sizeof(struct amr_coder_pvt), |
| 307 |
.buffer_samples = BUFFER_SAMPLES / 2, |
| 308 |
|
| 309 |
.buf_size = BUFFER_SAMPLES, |
| 310 |
}; |
| 311 |
|
| 312 |
static struct ast_translator lintoamr = { |
| 313 |
.name = "lintoamr", |
| 314 |
.src_codec = { |
| 315 |
.name = "slin", |
| 316 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 317 |
.sample_rate = 8000, |
| 318 |
}, |
| 319 |
.dst_codec = { |
| 320 |
.name = "amr", |
| 321 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 322 |
.sample_rate = 8000, |
| 323 |
}, |
| 324 |
.format = "amr", |
| 325 |
.newpvt = lintoamr_new, |
| 326 |
.framein = lintoamr_framein, |
| 327 |
.frameout = lintoamr_frameout, |
| 328 |
.destroy = lintoamr_destroy, |
| 329 |
.sample = slin8_sample, |
| 330 |
.desc_size = sizeof(struct amr_coder_pvt), |
| 331 |
.buffer_samples = BUFFER_SAMPLES / 2, |
| 332 |
.buf_size = BUFFER_SAMPLES, |
| 333 |
}; |
| 334 |
|
| 335 |
static struct ast_translator amrtolin16 = { |
| 336 |
.name = "amrtolin16", |
| 337 |
.src_codec = { |
| 338 |
.name = "amrwb", |
| 339 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 340 |
.sample_rate = 16000, |
| 341 |
}, |
| 342 |
.dst_codec = { |
| 343 |
.name = "slin", |
| 344 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 345 |
.sample_rate = 16000, |
| 346 |
}, |
| 347 |
.format = "slin16", |
| 348 |
.newpvt = amrtolin_new, |
| 349 |
.framein = amrtolin_framein, |
| 350 |
.destroy = amrtolin_destroy, |
| 351 |
.sample = amrwb_sample, |
| 352 |
.desc_size = sizeof(struct amr_coder_pvt), |
| 353 |
.buffer_samples = BUFFER_SAMPLES, |
| 354 |
|
| 355 |
.buf_size = BUFFER_SAMPLES * 2, |
| 356 |
}; |
| 357 |
|
| 358 |
static struct ast_translator lin16toamr = { |
| 359 |
.name = "lin16toamr", |
| 360 |
.src_codec = { |
| 361 |
.name = "slin", |
| 362 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 363 |
.sample_rate = 16000, |
| 364 |
}, |
| 365 |
.dst_codec = { |
| 366 |
.name = "amrwb", |
| 367 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 368 |
.sample_rate = 16000, |
| 369 |
}, |
| 370 |
.format = "amrwb", |
| 371 |
.newpvt = lintoamr_new, |
| 372 |
.framein = lintoamr_framein, |
| 373 |
.frameout = lintoamr_frameout, |
| 374 |
.destroy = lintoamr_destroy, |
| 375 |
.sample = slin16_sample, |
| 376 |
.desc_size = sizeof(struct amr_coder_pvt), |
| 377 |
.buffer_samples = BUFFER_SAMPLES, |
| 378 |
.buf_size = BUFFER_SAMPLES * 2, |
| 379 |
}; |
| 380 |
|
| 381 |
static int unload_module(void) |
| 382 |
{ |
| 383 |
int res; |
| 384 |
|
| 385 |
res = ast_unregister_translator(&amrtolin); |
| 386 |
res |= ast_unregister_translator(&lintoamr); |
| 387 |
res |= ast_unregister_translator(&amrtolin16); |
| 388 |
res |= ast_unregister_translator(&lin16toamr); |
| 389 |
|
| 390 |
return res; |
| 391 |
} |
| 392 |
|
| 393 |
static int load_module(void) |
| 394 |
{ |
| 395 |
int res; |
| 396 |
|
| 397 |
res = ast_register_translator(&amrtolin); |
| 398 |
res |= ast_register_translator(&lintoamr); |
| 399 |
res |= ast_register_translator(&amrtolin16); |
| 400 |
res |= ast_register_translator(&lin16toamr); |
| 401 |
|
| 402 |
if (res) { |
| 403 |
unload_module(); |
| 404 |
return AST_MODULE_LOAD_DECLINE; |
| 405 |
} |
| 406 |
|
| 407 |
return AST_MODULE_LOAD_SUCCESS; |
| 408 |
} |
| 409 |
|
| 410 |
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "AMR Coder/Decoder"); |