| 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/lock.h" |
| 37 |
#include "asterisk/linkedlists.h" |
| 38 |
#include "asterisk/module.h" |
| 39 |
#include "asterisk/config.h" |
| 40 |
#include "asterisk/translate.h" |
| 41 |
#include "asterisk/utils.h" |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#define BUFFER_SAMPLES 8096 |
| 47 |
|
| 48 |
|
| 49 |
#include "asterisk/slin.h" |
| 50 |
#include "ex_adpcm.h" |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
static int indsft[8] = { -1, -1, -1, -1, 2, 4, 6, 8 }; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
static int stpsz[49] = { |
| 63 |
16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, |
| 64 |
80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279, |
| 65 |
307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963, |
| 66 |
1060, 1166, 1282, 1411, 1552 |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
struct adpcm_state { |
| 74 |
int ssindex; |
| 75 |
int signal; |
| 76 |
int zero_count; |
| 77 |
int next_flag; |
| 78 |
}; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
static inline short decode(int encoded, struct adpcm_state *state) |
| 92 |
{ |
| 93 |
int diff; |
| 94 |
int step; |
| 95 |
int sign; |
| 96 |
|
| 97 |
step = stpsz[state->ssindex]; |
| 98 |
|
| 99 |
sign = encoded & 0x08; |
| 100 |
encoded &= 0x07; |
| 101 |
#ifdef NOT_BLI |
| 102 |
diff = (((encoded << 1) + 1) * step) >> 3; |
| 103 |
#else |
| 104 |
diff = step >> 3; |
| 105 |
if (encoded & 4) |
| 106 |
diff += step; |
| 107 |
if (encoded & 2) |
| 108 |
diff += step >> 1; |
| 109 |
if (encoded & 1) |
| 110 |
diff += step >> 2; |
| 111 |
if ((encoded >> 1) & step & 0x1) |
| 112 |
diff++; |
| 113 |
#endif |
| 114 |
if (sign) |
| 115 |
diff = -diff; |
| 116 |
|
| 117 |
if (state->next_flag & 0x1) |
| 118 |
state->signal -= 8; |
| 119 |
else if (state->next_flag & 0x2) |
| 120 |
state->signal += 8; |
| 121 |
|
| 122 |
state->signal += diff; |
| 123 |
|
| 124 |
if (state->signal > 2047) |
| 125 |
state->signal = 2047; |
| 126 |
else if (state->signal < -2047) |
| 127 |
state->signal = -2047; |
| 128 |
|
| 129 |
state->next_flag = 0; |
| 130 |
|
| 131 |
#ifdef AUTO_RETURN |
| 132 |
if (encoded) |
| 133 |
state->zero_count = 0; |
| 134 |
else if (++(state->zero_count) == 24) { |
| 135 |
state->zero_count = 0; |
| 136 |
if (state->signal > 0) |
| 137 |
state->next_flag = 0x1; |
| 138 |
else if (state->signal < 0) |
| 139 |
state->next_flag = 0x2; |
| 140 |
} |
| 141 |
#endif |
| 142 |
|
| 143 |
state->ssindex += indsft[encoded]; |
| 144 |
if (state->ssindex < 0) |
| 145 |
state->ssindex = 0; |
| 146 |
else if (state->ssindex > 48) |
| 147 |
state->ssindex = 48; |
| 148 |
|
| 149 |
return state->signal << 4; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
static inline int adpcm(short csig, struct adpcm_state *state) |
| 165 |
{ |
| 166 |
int diff; |
| 167 |
int step; |
| 168 |
int encoded; |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
csig >>= 4; |
| 174 |
|
| 175 |
step = stpsz[state->ssindex]; |
| 176 |
diff = csig - state->signal; |
| 177 |
|
| 178 |
#ifdef NOT_BLI |
| 179 |
if (diff < 0) { |
| 180 |
encoded = (-diff << 2) / step; |
| 181 |
if (encoded > 7) |
| 182 |
encoded = 7; |
| 183 |
encoded |= 0x08; |
| 184 |
} else { |
| 185 |
encoded = (diff << 2) / step; |
| 186 |
if (encoded > 7) |
| 187 |
encoded = 7; |
| 188 |
} |
| 189 |
#else |
| 190 |
if (diff < 0) { |
| 191 |
encoded = 8; |
| 192 |
diff = -diff; |
| 193 |
} else |
| 194 |
encoded = 0; |
| 195 |
if (diff >= step) { |
| 196 |
encoded |= 4; |
| 197 |
diff -= step; |
| 198 |
} |
| 199 |
step >>= 1; |
| 200 |
if (diff >= step) { |
| 201 |
encoded |= 2; |
| 202 |
diff -= step; |
| 203 |
} |
| 204 |
step >>= 1; |
| 205 |
if (diff >= step) |
| 206 |
encoded |= 1; |
| 207 |
#endif |
| 208 |
|
| 209 |
|
| 210 |
decode(encoded, state); |
| 211 |
|
| 212 |
return encoded; |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
struct adpcm_encoder_pvt { |
| 219 |
struct adpcm_state state; |
| 220 |
int16_t inbuf[BUFFER_SAMPLES]; |
| 221 |
}; |
| 222 |
|
| 223 |
|
| 224 |
struct adpcm_decoder_pvt { |
| 225 |
struct adpcm_state state; |
| 226 |
}; |
| 227 |
|
| 228 |
|
| 229 |
static int adpcmtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) |
| 230 |
{ |
| 231 |
struct adpcm_decoder_pvt *tmp = pvt->pvt; |
| 232 |
int x = f->datalen; |
| 233 |
unsigned char *src = f->data.ptr; |
| 234 |
int16_t *dst = pvt->outbuf.i16 + pvt->samples; |
| 235 |
|
| 236 |
while (x--) { |
| 237 |
*dst++ = decode((*src >> 4) & 0xf, &tmp->state); |
| 238 |
*dst++ = decode(*src++ & 0x0f, &tmp->state); |
| 239 |
} |
| 240 |
pvt->samples += f->samples; |
| 241 |
pvt->datalen += 2*f->samples; |
| 242 |
return 0; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
static int lintoadpcm_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) |
| 247 |
{ |
| 248 |
struct adpcm_encoder_pvt *tmp = pvt->pvt; |
| 249 |
|
| 250 |
memcpy(&tmp->inbuf[pvt->samples], f->data.ptr, f->datalen); |
| 251 |
pvt->samples += f->samples; |
| 252 |
return 0; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
static struct ast_frame *lintoadpcm_frameout(struct ast_trans_pvt *pvt) |
| 257 |
{ |
| 258 |
struct adpcm_encoder_pvt *tmp = pvt->pvt; |
| 259 |
struct ast_frame *f; |
| 260 |
int i; |
| 261 |
int samples = pvt->samples; |
| 262 |
|
| 263 |
if (samples < 2) |
| 264 |
return NULL; |
| 265 |
|
| 266 |
pvt->samples &= ~1; |
| 267 |
|
| 268 |
for (i = 0; i < pvt->samples; i += 2) { |
| 269 |
pvt->outbuf.c[i/2] = |
| 270 |
(adpcm(tmp->inbuf[i ], &tmp->state) << 4) | |
| 271 |
(adpcm(tmp->inbuf[i+1], &tmp->state) ); |
| 272 |
}; |
| 273 |
|
| 274 |
f = ast_trans_frameout(pvt, pvt->samples/2, 0); |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
if (samples & 1) { |
| 282 |
tmp->inbuf[0] = tmp->inbuf[samples - 1]; |
| 283 |
pvt->samples = 1; |
| 284 |
} |
| 285 |
return f; |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
static struct ast_translator adpcmtolin = { |
| 290 |
.name = "adpcmtolin", |
| 291 |
.src_codec = { |
| 292 |
.name = "adpcm", |
| 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 |
.framein = adpcmtolin_framein, |
| 303 |
.sample = adpcm_sample, |
| 304 |
.desc_size = sizeof(struct adpcm_decoder_pvt), |
| 305 |
.buffer_samples = BUFFER_SAMPLES, |
| 306 |
.buf_size = BUFFER_SAMPLES * 2, |
| 307 |
}; |
| 308 |
|
| 309 |
static struct ast_translator lintoadpcm = { |
| 310 |
.name = "lintoadpcm", |
| 311 |
.src_codec = { |
| 312 |
.name = "slin", |
| 313 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 314 |
.sample_rate = 8000, |
| 315 |
}, |
| 316 |
.dst_codec = { |
| 317 |
.name = "adpcm", |
| 318 |
.type = AST_MEDIA_TYPE_AUDIO, |
| 319 |
.sample_rate = 8000, |
| 320 |
}, |
| 321 |
.format = "adpcm", |
| 322 |
.framein = lintoadpcm_framein, |
| 323 |
.frameout = lintoadpcm_frameout, |
| 324 |
.sample = slin8_sample, |
| 325 |
.desc_size = sizeof (struct adpcm_encoder_pvt), |
| 326 |
.buffer_samples = BUFFER_SAMPLES, |
| 327 |
.buf_size = BUFFER_SAMPLES/ 2, |
| 328 |
}; |
| 329 |
|
| 330 |
static int unload_module(void) |
| 331 |
{ |
| 332 |
int res; |
| 333 |
|
| 334 |
res = ast_unregister_translator(&lintoadpcm); |
| 335 |
res |= ast_unregister_translator(&adpcmtolin); |
| 336 |
|
| 337 |
return res; |
| 338 |
} |
| 339 |
|
| 340 |
static int load_module(void) |
| 341 |
{ |
| 342 |
int res = 0; |
| 343 |
|
| 344 |
res = ast_register_translator(&adpcmtolin); |
| 345 |
res |= ast_register_translator(&lintoadpcm); |
| 346 |
|
| 347 |
if (res) { |
| 348 |
unload_module(); |
| 349 |
return AST_MODULE_LOAD_DECLINE; |
| 350 |
} |
| 351 |
|
| 352 |
return AST_MODULE_LOAD_SUCCESS; |
| 353 |
} |
| 354 |
|
| 355 |
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "Adaptive Differential PCM Coder/Decoder", |
| 356 |
.support_level = AST_MODULE_SUPPORT_CORE, |
| 357 |
.load = load_module, |
| 358 |
.unload = unload_module, |
| 359 |
); |