1 /* codec_adpcm.c - translate between signed linear and Dialogic ADPCM
3 * Asterisk -- A telephony toolkit for Linux.
5 * Based on frompcm.c and topcm.c from the Emiliano MIPL browser/
6 * interpreter. See http://www.bsdtelephony.com.mx
8 * Copyright (c) 2001 - 2005 Digium, Inc.
11 * Karl Sackett <krs@linux-support.net>, 2001-03-21
13 * This program is free software, distributed under the terms of
14 * the GNU General Public License
17 #include <asterisk/lock.h>
18 #include <asterisk/logger.h>
19 #include <asterisk/module.h>
20 #include <asterisk/config.h>
21 #include <asterisk/options.h>
22 #include <asterisk/translate.h>
23 #include <asterisk/channel.h>
25 #include <netinet/in.h>
31 /* define NOT_BLI to use a faster but not bit-level identical version */
34 #define BUFFER_SIZE 8096 /* size for the translation buffers */
36 AST_MUTEX_DEFINE_STATIC(localuser_lock);
37 static int localusecnt = 0;
39 static char *tdesc = "Adaptive Differential PCM Coder/Decoder";
41 static int useplc = 0;
43 /* Sample frame data */
45 #include "slin_adpcm_ex.h"
46 #include "adpcm_slin_ex.h"
49 * Step size index shift table
52 static int indsft[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
55 * Step size table, where stpsz[i]=floor[16*(11/10)^i]
58 static int stpsz[49] = {
59 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73,
60 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
61 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
62 1060, 1166, 1282, 1411, 1552
66 * Decoder/Encoder state
67 * States for both encoder and decoder are synchronized
78 * Decodes the encoded nibble from the adpcm file.
81 * Returns the encoded difference.
84 * Sets the index to the step size table for the next encode.
88 decode(int encoded, struct adpcm_state* state)
94 step = stpsz[state->ssindex];
96 sign = encoded & 0x08;
99 diff = (((encoded << 1) + 1) * step) >> 3;
102 if (encoded & 4) diff += step;
103 if (encoded & 2) diff += step >> 1;
104 if (encoded & 1) diff += step >> 2;
105 if ((encoded >> 1) & step & 0x1)
111 if (state->next_flag & 0x1)
113 else if (state->next_flag & 0x2)
116 state->signal += diff;
118 if (state->signal > 2047)
119 state->signal = 2047;
120 else if (state->signal < -2047)
121 state->signal = -2047;
123 state->next_flag = 0;
127 state->zero_count = 0;
128 else if (++(state->zero_count) == 24)
130 state->zero_count = 0;
131 if (state->signal > 0)
132 state->next_flag = 0x1;
133 else if (state->signal < 0)
134 state->next_flag = 0x2;
138 state->ssindex += indsft[encoded];
139 if (state->ssindex < 0)
141 else if (state->ssindex > 48)
144 return state->signal << 4;
149 * Takes a signed linear signal and encodes it as ADPCM
150 * For more information see http://support.dialogic.com/appnotes/adpcm.pdf
156 * signal gets updated with each pass.
160 adpcm(short csig, struct adpcm_state* state)
167 * Clip csig if too large or too small
171 step = stpsz[state->ssindex];
172 diff = csig - state->signal;
177 encoded = (-diff << 2) / step;
184 encoded = (diff << 2) / step;
212 /* feedback to state */
213 decode(encoded, state);
219 * Private workspace for translating signed linear signals to ADPCM.
222 struct adpcm_encoder_pvt
225 char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
226 short inbuf[BUFFER_SIZE]; /* Unencoded signed linear values */
227 unsigned char outbuf[BUFFER_SIZE]; /* Encoded ADPCM, two nibbles to a word */
228 struct adpcm_state state;
233 * Private workspace for translating ADPCM signals to signed linear.
236 struct adpcm_decoder_pvt
239 char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
240 short outbuf[BUFFER_SIZE]; /* Decoded signed linear values */
241 struct adpcm_state state;
248 * Create a new instance of adpcm_decoder_pvt.
251 * Returns a pointer to the new instance.
257 static struct ast_translator_pvt *
258 adpcmtolin_new (void)
260 struct adpcm_decoder_pvt *tmp;
261 tmp = malloc (sizeof (struct adpcm_decoder_pvt));
264 memset(tmp, 0, sizeof(*tmp));
268 ast_update_use_count ();
270 return (struct ast_translator_pvt *) tmp;
275 * Create a new instance of adpcm_encoder_pvt.
278 * Returns a pointer to the new instance.
284 static struct ast_translator_pvt *
285 lintoadpcm_new (void)
287 struct adpcm_encoder_pvt *tmp;
288 tmp = malloc (sizeof (struct adpcm_encoder_pvt));
291 memset(tmp, 0, sizeof(*tmp));
293 ast_update_use_count ();
296 return (struct ast_translator_pvt *) tmp;
301 * Take an input buffer with packed 4-bit ADPCM values and put decoded PCM in outbuf,
302 * if there is room left.
308 * tmp->tail is the number of packed values in the buffer.
312 adpcmtolin_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
314 struct adpcm_decoder_pvt *tmp = (struct adpcm_decoder_pvt *) pvt;
318 if(f->datalen == 0) { /* perform PLC with nominal framesize of 20ms/160 samples */
319 if((tmp->tail + 160) > sizeof(tmp->outbuf) / 2) {
320 ast_log(LOG_WARNING, "Out of buffer space\n");
324 plc_fillin(&tmp->plc, tmp->outbuf+tmp->tail, 160);
330 if (f->datalen * 4 + tmp->tail * 2 > sizeof(tmp->outbuf)) {
331 ast_log(LOG_WARNING, "Out of buffer space\n");
337 for (x=0;x<f->datalen;x++) {
338 tmp->outbuf[tmp->tail++] = decode((b[x] >> 4) & 0xf, &tmp->state);
339 tmp->outbuf[tmp->tail++] = decode(b[x] & 0x0f, &tmp->state);
342 if(useplc) plc_rx(&tmp->plc, tmp->outbuf+tmp->tail-f->datalen*2, f->datalen*2);
348 * AdpcmToLin_FrameOut
349 * Convert 4-bit ADPCM encoded signals to 16-bit signed linear.
352 * Converted signals are placed in tmp->f.data, tmp->f.datalen
353 * and tmp->f.samples are calculated.
359 static struct ast_frame *
360 adpcmtolin_frameout (struct ast_translator_pvt *pvt)
362 struct adpcm_decoder_pvt *tmp = (struct adpcm_decoder_pvt *) pvt;
367 tmp->f.frametype = AST_FRAME_VOICE;
368 tmp->f.subclass = AST_FORMAT_SLINEAR;
369 tmp->f.datalen = tmp->tail *2;
370 tmp->f.samples = tmp->tail;
372 tmp->f.offset = AST_FRIENDLY_OFFSET;
373 tmp->f.src = __PRETTY_FUNCTION__;
374 tmp->f.data = tmp->outbuf;
381 * Fill an input buffer with 16-bit signed linear PCM values.
387 * tmp->tail is number of signal values in the input buffer.
391 lintoadpcm_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
393 struct adpcm_encoder_pvt *tmp = (struct adpcm_encoder_pvt *) pvt;
395 if ((tmp->tail + f->datalen / 2) < (sizeof (tmp->inbuf) / 2))
397 memcpy (&tmp->inbuf[tmp->tail], f->data, f->datalen);
398 tmp->tail += f->datalen / 2;
402 ast_log (LOG_WARNING, "Out of buffer space\n");
409 * LinToAdpcm_FrameOut
410 * Convert a buffer of raw 16-bit signed linear PCM to a buffer
411 * of 4-bit ADPCM packed two to a byte (Big Endian).
417 * Leftover inbuf data gets packed, tail gets updated.
420 static struct ast_frame *
421 lintoadpcm_frameout (struct ast_translator_pvt *pvt)
423 struct adpcm_encoder_pvt *tmp = (struct adpcm_encoder_pvt *) pvt;
426 if (tmp->tail < 2) return NULL;
429 i_max = tmp->tail & ~1; /* atomic size is 2 samples */
431 /* What is this, state debugging? should be #ifdef'd then
432 tmp->outbuf[0] = tmp->ssindex & 0xff;
433 tmp->outbuf[1] = (tmp->signal >> 8) & 0xff;
434 tmp->outbuf[2] = (tmp->signal & 0xff);
435 tmp->outbuf[3] = tmp->zero_count;
436 tmp->outbuf[4] = tmp->next_flag;
438 for (i = 0; i < i_max; i+=2)
441 (adpcm(tmp->inbuf[i ], &tmp->state) << 4) |
442 (adpcm(tmp->inbuf[i+1], &tmp->state) );
446 tmp->f.frametype = AST_FRAME_VOICE;
447 tmp->f.subclass = AST_FORMAT_ADPCM;
448 tmp->f.samples = i_max;
450 tmp->f.offset = AST_FRIENDLY_OFFSET;
451 tmp->f.src = __PRETTY_FUNCTION__;
452 tmp->f.data = tmp->outbuf;
453 tmp->f.datalen = i_max / 2;
456 * If there is a signal left over (there should be no more than
457 * one) move it to the beginning of the input buffer.
460 if (tmp->tail == i_max)
464 tmp->inbuf[0] = tmp->inbuf[tmp->tail];
475 static struct ast_frame *
476 adpcmtolin_sample (void)
478 static struct ast_frame f;
479 f.frametype = AST_FRAME_VOICE;
480 f.subclass = AST_FORMAT_ADPCM;
481 f.datalen = sizeof (adpcm_slin_ex);
482 f.samples = sizeof(adpcm_slin_ex) * 2;
485 f.src = __PRETTY_FUNCTION__;
486 f.data = adpcm_slin_ex;
494 static struct ast_frame *
495 lintoadpcm_sample (void)
497 static struct ast_frame f;
498 f.frametype = AST_FRAME_VOICE;
499 f.subclass = AST_FORMAT_SLINEAR;
500 f.datalen = sizeof (slin_adpcm_ex);
502 f.samples = sizeof (slin_adpcm_ex) / 2;
505 f.src = __PRETTY_FUNCTION__;
506 f.data = slin_adpcm_ex;
512 * Destroys a private workspace.
522 adpcm_destroy (struct ast_translator_pvt *pvt)
526 ast_update_use_count ();
530 * The complete translator for ADPCMToLin.
533 static struct ast_translator adpcmtolin = {
546 * The complete translator for LinToADPCM.
549 static struct ast_translator lintoadpcm = {
564 struct ast_config *cfg;
565 struct ast_variable *var;
566 if ((cfg = ast_config_load("codecs.conf"))) {
567 if ((var = ast_variable_browse(cfg, "plc"))) {
569 if (!strcasecmp(var->name, "genericplc")) {
570 useplc = ast_true(var->value) ? 1 : 0;
571 if (option_verbose > 2)
572 ast_verbose(VERBOSE_PREFIX_3 "CODEC ULAW: %susing generic PLC\n", useplc ? "" : "not ");
591 ast_mutex_lock (&localuser_lock);
592 res = ast_unregister_translator (&lintoadpcm);
594 res = ast_unregister_translator (&adpcmtolin);
597 ast_mutex_unlock (&localuser_lock);
606 res = ast_register_translator (&adpcmtolin);
608 res = ast_register_translator (&lintoadpcm);
610 ast_unregister_translator (&adpcmtolin);
615 * Return a description of this module.
628 STANDARD_USECOUNT (res);
635 return ASTERISK_GPL_KEY;