2 * Asterisk -- A telephony toolkit for Linux.
4 * Flat, binary, ADPCM vox file format.
6 * Copyright (C) 1999, Mark Spencer
8 * Mark Spencer <markster@linux-support.net>
10 * This program is free software, distributed under the terms of
11 * the GNU General Public License
14 #include <asterisk/lock.h>
15 #include <asterisk/channel.h>
16 #include <asterisk/file.h>
17 #include <asterisk/logger.h>
18 #include <asterisk/sched.h>
19 #include <asterisk/module.h>
20 #include <arpa/inet.h>
31 #include <machine/endian.h>
34 #define BUF_SIZE 80 /* 160 samples */
36 struct ast_filestream {
37 void *reserved[AST_RESERVED_POINTERS];
38 /* This is what a filestream means to us */
39 int fd; /* Descriptor */
40 struct ast_frame fr; /* Frame information */
41 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
42 char empty; /* Empty character */
43 unsigned char buf[BUF_SIZE + 3]; /* Output Buffer */
46 short signal; /* Signal level (file side) */
47 short ssindex; /* Signal ssindex (file side) */
51 static pthread_mutex_t vox_lock = AST_MUTEX_INITIALIZER;
52 static int glistcnt = 0;
54 static char *name = "vox";
55 static char *desc = "Dialogic VOX (ADPCM) File Format";
56 static char *exts = "vox";
59 * Step size index shift table
62 static short indsft[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
65 * Step size table, where stpsz[i]=floor[16*(11/10)^i]
68 static short stpsz[49] = {
69 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73,
70 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
71 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
72 1060, 1166, 1282, 1411, 1552
79 static short nbl2bit[16][4] = {
80 {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1},
81 {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1},
82 {-1, 0, 0, 0}, {-1, 0, 0, 1}, {-1, 0, 1, 0}, {-1, 0, 1, 1},
83 {-1, 1, 0, 0}, {-1, 1, 0, 1}, {-1, 1, 1, 0}, {-1, 1, 1, 1}
88 * Decodes the encoded nibble from the adpcm file.
91 * Returns the encoded difference.
94 * Sets the index to the step size table for the next encode.
98 decode (unsigned char encoded, short *ssindex)
101 step = stpsz[*ssindex];
102 diff = nbl2bit[encoded][0] * (step * nbl2bit[encoded][1] +
103 (step >> 1) * nbl2bit[encoded][2] +
104 (step >> 2) * nbl2bit[encoded][3] +
106 *ssindex = *ssindex + indsft[(encoded & 7)];
109 else if (*ssindex > 48)
116 * Takes a signed linear signal and encodes it as ADPCM
117 * For more information see http://support.dialogic.com/appnotes/adpcm.pdf
123 * signal gets updated with each pass.
126 static inline unsigned char
127 adpcm (short csig, short *ssindex, short *signal)
130 unsigned char encoded;
131 step = stpsz[*ssindex];
133 * Clip csig if too large or too small
138 diff = csig - *signal;
162 *signal += decode (encoded, ssindex);
166 static struct ast_filestream *vox_open(int fd)
168 /* We don't have any header to read or anything really, but
169 if we did, it would go here. We also might want to check
170 and be sure it's a valid file. */
171 struct ast_filestream *tmp;
172 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
173 memset(tmp, 0, sizeof(struct ast_filestream));
174 if (ast_pthread_mutex_lock(&vox_lock)) {
175 ast_log(LOG_WARNING, "Unable to lock vox list\n");
180 tmp->fr.data = tmp->buf;
181 tmp->fr.frametype = AST_FRAME_VOICE;
182 tmp->fr.subclass = AST_FORMAT_ADPCM;
183 /* datalen will vary for each frame */
186 tmp->lasttimeout = -1;
188 ast_pthread_mutex_unlock(&vox_lock);
189 ast_update_use_count();
194 static struct ast_filestream *vox_rewrite(int fd, char *comment)
196 /* We don't have any header to read or anything really, but
197 if we did, it would go here. We also might want to check
198 and be sure it's a valid file. */
199 struct ast_filestream *tmp;
200 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
201 memset(tmp, 0, sizeof(struct ast_filestream));
202 if (ast_pthread_mutex_lock(&vox_lock)) {
203 ast_log(LOG_WARNING, "Unable to lock vox list\n");
209 ast_pthread_mutex_unlock(&vox_lock);
210 ast_update_use_count();
212 ast_log(LOG_WARNING, "Out of memory\n");
216 static void vox_close(struct ast_filestream *s)
218 if (ast_pthread_mutex_lock(&vox_lock)) {
219 ast_log(LOG_WARNING, "Unable to lock vox list\n");
223 ast_pthread_mutex_unlock(&vox_lock);
224 ast_update_use_count();
230 static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
234 /* Send a frame from the file to the appropriate channel */
236 s->fr.frametype = AST_FRAME_VOICE;
237 s->fr.subclass = AST_FORMAT_ADPCM;
238 s->fr.offset = AST_FRIENDLY_OFFSET;
241 if ((res = read(s->fd, s->buf + 3, BUF_SIZE)) < 1) {
243 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
246 /* Store index, then signal */
247 s->buf[0] = s->ssindex & 0xff;
248 s->buf[1] = (s->signal >> 8) & 0xff;
249 s->buf[2] = s->signal & 0xff;
250 /* Do the decoder to be sure we get the right stuff in the signal and index fields. */
251 for (x=3;x<res+3;x++) {
252 s->signal += decode(s->buf[x] >> 4, &s->ssindex);
253 s->signal += decode(s->buf[x] & 0xf, &s->ssindex);
255 s->fr.samples = res * 2;
256 s->fr.datalen = res + 3;
257 *whennext = s->fr.samples;
261 static int vox_write(struct ast_filestream *fs, struct ast_frame *f)
264 if (f->frametype != AST_FRAME_VOICE) {
265 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
268 if (f->subclass != AST_FORMAT_ADPCM) {
269 ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%d)!\n", f->subclass);
272 if (f->datalen < 3) {
273 ast_log(LOG_WARNING, "Invalid frame of data (< 3 bytes long) from %s\n", f->src);
276 if ((res = write(fs->fd, f->data + 3, f->datalen)) != f->datalen) {
277 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
283 static char *vox_getcomment(struct ast_filestream *s)
288 static int vox_seek(struct ast_filestream *fs, long sample_offset, int whence)
293 static int vox_trunc(struct ast_filestream *fs)
298 static long vox_tell(struct ast_filestream *fs)
305 return ast_format_register(name, exts, AST_FORMAT_ADPCM,
321 return ast_format_unregister(name);
327 if (ast_pthread_mutex_lock(&vox_lock)) {
328 ast_log(LOG_WARNING, "Unable to lock vox list\n");
332 ast_pthread_mutex_unlock(&vox_lock);
344 return ASTERISK_GPL_KEY;