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 Linux Support Services, Inc. All rights reserved.
10 * Karl Sackett <krs@linux-support.net>, 2001-3-21
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License
16 #include <asterisk/lock.h>
17 #include <asterisk/logger.h>
18 #include <asterisk/module.h>
19 #include <asterisk/translate.h>
20 #include <asterisk/channel.h>
22 #include <netinet/in.h>
29 #define BUFFER_SIZE 8096 /* size for the translation buffers */
31 AST_MUTEX_DEFINE_STATIC(localuser_lock);
32 static int localusecnt = 0;
34 static char *tdesc = "Adaptive Differential PCM Coder/Decoder";
36 /* Sample frame data */
38 #include "slin_adpcm_ex.h"
39 #include "adpcm_slin_ex.h"
42 * Step size index shift table
45 static short indsft[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
48 * Step size table, where stpsz[i]=floor[16*(11/10)^i]
51 static short stpsz[49] = {
52 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73,
53 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
54 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
55 1060, 1166, 1282, 1411, 1552
62 static short nbl2bit[16][4] = {
63 {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1},
64 {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1},
65 {-1, 0, 0, 0}, {-1, 0, 0, 1}, {-1, 0, 1, 0}, {-1, 0, 1, 1},
66 {-1, 1, 0, 0}, {-1, 1, 0, 1}, {-1, 1, 1, 0}, {-1, 1, 1, 1}
71 * Decodes the encoded nibble from the adpcm file.
74 * Returns the encoded difference.
77 * Sets the index to the step size table for the next encode.
81 decode (unsigned char encoded, short *ssindex, short *signal, unsigned char *rkey, unsigned char *next)
84 step = stpsz[*ssindex];
86 diff = step * nbl2bit[encoded][1] +
87 (step >> 1) * nbl2bit[encoded][2] +
88 (step >> 2) * nbl2bit[encoded][3] +
90 if (nbl2bit[encoded][2] && (step & 0x1))
92 diff *= nbl2bit[encoded][0];
96 else if ( *next & 0x2 )
103 else if (*signal < -2047)
111 else if ( ++(*rkey) == 24 ) {
115 else if (*signal < 0)
120 *ssindex = *ssindex + indsft[(encoded & 7)];
123 else if (*ssindex > 48)
129 * Takes a signed linear signal and encodes it as ADPCM
130 * For more information see http://support.dialogic.com/appnotes/adpcm.pdf
136 * signal gets updated with each pass.
139 static inline unsigned char
140 adpcm (short csig, short *ssindex, short *signal, unsigned char *rkey, unsigned char *next)
143 unsigned char encoded;
144 step = stpsz[*ssindex];
146 * Clip csig if too large or too small
151 diff = csig - *signal;
175 decode (encoded, ssindex, signal, rkey, next);
180 * Private workspace for translating signed linear signals to ADPCM.
183 struct adpcm_encoder_pvt
186 char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
187 short inbuf[BUFFER_SIZE]; /* Unencoded signed linear values */
188 unsigned char outbuf[BUFFER_SIZE]; /* Encoded ADPCM, two nibbles to a word */
191 unsigned char zero_count;
192 unsigned char next_flag;
197 * Private workspace for translating ADPCM signals to signed linear.
200 struct adpcm_decoder_pvt
203 char offset[AST_FRIENDLY_OFFSET]; /* Space to build offset */
204 short outbuf[BUFFER_SIZE]; /* Decoded signed linear values */
207 unsigned char zero_count;
208 unsigned char next_flag;
214 * Create a new instance of adpcm_decoder_pvt.
217 * Returns a pointer to the new instance.
223 static struct ast_translator_pvt *
224 adpcmtolin_new (void)
226 struct adpcm_decoder_pvt *tmp;
227 tmp = malloc (sizeof (struct adpcm_decoder_pvt));
230 memset(tmp, 0, sizeof(*tmp));
233 ast_update_use_count ();
235 return (struct ast_translator_pvt *) tmp;
240 * Create a new instance of adpcm_encoder_pvt.
243 * Returns a pointer to the new instance.
249 static struct ast_translator_pvt *
250 lintoadpcm_new (void)
252 struct adpcm_encoder_pvt *tmp;
253 tmp = malloc (sizeof (struct adpcm_encoder_pvt));
256 memset(tmp, 0, sizeof(*tmp));
258 ast_update_use_count ();
261 return (struct ast_translator_pvt *) tmp;
266 * Fill an input buffer with packed 4-bit ADPCM values if there is room
273 * tmp->tail is the number of packed values in the buffer.
277 adpcmtolin_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
279 struct adpcm_decoder_pvt *tmp = (struct adpcm_decoder_pvt *) pvt;
283 if (f->datalen * 4 > sizeof(tmp->outbuf)) {
284 ast_log(LOG_WARNING, "Out of buffer space\n");
290 for (x=0;x<f->datalen;x++) {
291 decode((b[x] >> 4) & 0xf, &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag);
292 tmp->outbuf[tmp->tail++] = tmp->signal << 4;
294 decode(b[x] & 0x0f, &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag);
295 tmp->outbuf[tmp->tail++] = tmp->signal << 4;
302 * AdpcmToLin_FrameOut
303 * Convert 4-bit ADPCM encoded signals to 16-bit signed linear.
306 * Converted signals are placed in tmp->f.data, tmp->f.datalen
307 * and tmp->f.samples are calculated.
313 static struct ast_frame *
314 adpcmtolin_frameout (struct ast_translator_pvt *pvt)
316 struct adpcm_decoder_pvt *tmp = (struct adpcm_decoder_pvt *) pvt;
321 tmp->f.frametype = AST_FRAME_VOICE;
322 tmp->f.subclass = AST_FORMAT_SLINEAR;
323 tmp->f.datalen = tmp->tail *2;
324 tmp->f.samples = tmp->tail;
326 tmp->f.offset = AST_FRIENDLY_OFFSET;
327 tmp->f.src = __PRETTY_FUNCTION__;
328 tmp->f.data = tmp->outbuf;
335 * Fill an input buffer with 16-bit signed linear PCM values.
341 * tmp->tail is number of signal values in the input buffer.
345 lintoadpcm_framein (struct ast_translator_pvt *pvt, struct ast_frame *f)
347 struct adpcm_encoder_pvt *tmp = (struct adpcm_encoder_pvt *) pvt;
349 if ((tmp->tail + f->datalen / 2) < (sizeof (tmp->inbuf) / 2))
351 memcpy (&tmp->inbuf[tmp->tail], f->data, f->datalen);
352 tmp->tail += f->datalen / 2;
356 ast_log (LOG_WARNING, "Out of buffer space\n");
363 * LinToAdpcm_FrameOut
364 * Convert a buffer of raw 16-bit signed linear PCM to a buffer
365 * of 4-bit ADPCM packed two to a byte (Big Endian).
371 * Leftover inbuf data gets packed, tail gets updated.
374 static struct ast_frame *
375 lintoadpcm_frameout (struct ast_translator_pvt *pvt)
377 struct adpcm_encoder_pvt *tmp = (struct adpcm_encoder_pvt *) pvt;
378 unsigned char adpcm0, adpcm1;
381 if (tmp->tail < 2) return NULL;
384 i_max = (tmp->tail / 2) * 2;
386 tmp->outbuf[0] = tmp->ssindex & 0xff;
387 tmp->outbuf[1] = (tmp->signal >> 8) & 0xff;
388 tmp->outbuf[2] = (tmp->signal & 0xff);
389 tmp->outbuf[3] = tmp->zero_count;
390 tmp->outbuf[4] = tmp->next_flag;
392 for (i = 0; i < i_max; i+=2)
394 adpcm0 = adpcm (tmp->inbuf[i], &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag);
395 adpcm1 = adpcm (tmp->inbuf[i+1], &tmp->ssindex, &tmp->signal, &tmp->zero_count, &tmp->next_flag);
397 tmp->outbuf[i/2] = (adpcm0 << 4) | adpcm1;
401 tmp->f.frametype = AST_FRAME_VOICE;
402 tmp->f.subclass = AST_FORMAT_ADPCM;
403 tmp->f.samples = i_max;
405 tmp->f.offset = AST_FRIENDLY_OFFSET;
406 tmp->f.src = __PRETTY_FUNCTION__;
407 tmp->f.data = tmp->outbuf;
408 tmp->f.datalen = i_max / 2;
411 * If there is a signal left over (there should be no more than
412 * one) move it to the beginning of the input buffer.
415 if (tmp->tail == i_max)
419 tmp->inbuf[0] = tmp->inbuf[tmp->tail];
430 static struct ast_frame *
431 adpcmtolin_sample (void)
433 static struct ast_frame f;
434 f.frametype = AST_FRAME_VOICE;
435 f.subclass = AST_FORMAT_ADPCM;
436 f.datalen = sizeof (adpcm_slin_ex);
437 f.samples = sizeof(adpcm_slin_ex) * 2;
440 f.src = __PRETTY_FUNCTION__;
441 f.data = adpcm_slin_ex;
449 static struct ast_frame *
450 lintoadpcm_sample (void)
452 static struct ast_frame f;
453 f.frametype = AST_FRAME_VOICE;
454 f.subclass = AST_FORMAT_SLINEAR;
455 f.datalen = sizeof (slin_adpcm_ex);
457 f.samples = sizeof (slin_adpcm_ex) / 2;
460 f.src = __PRETTY_FUNCTION__;
461 f.data = slin_adpcm_ex;
467 * Destroys a private workspace.
477 adpcm_destroy (struct ast_translator_pvt *pvt)
481 ast_update_use_count ();
485 * The complete translator for ADPCMToLin.
488 static struct ast_translator adpcmtolin = {
501 * The complete translator for LinToADPCM.
504 static struct ast_translator lintoadpcm = {
520 ast_mutex_lock (&localuser_lock);
521 res = ast_unregister_translator (&lintoadpcm);
523 res = ast_unregister_translator (&adpcmtolin);
526 ast_mutex_unlock (&localuser_lock);
534 res = ast_register_translator (&adpcmtolin);
536 res = ast_register_translator (&lintoadpcm);
538 ast_unregister_translator (&adpcmtolin);
543 * Return a description of this module.
556 STANDARD_USECOUNT (res);
563 return ASTERISK_GPL_KEY;