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/channel.h>
15 #include <asterisk/file.h>
16 #include <asterisk/logger.h>
17 #include <asterisk/sched.h>
18 #include <asterisk/module.h>
19 #include <arpa/inet.h>
29 #define BUF_SIZE 80 /* 160 samples */
31 struct ast_filestream {
32 void *reserved[AST_RESERVED_POINTERS];
33 /* Believe it or not, we must decode/recode to account for the
35 /* This is what a filestream means to us */
36 int fd; /* Descriptor */
37 struct ast_channel *owner;
38 struct ast_frame fr; /* Frame information */
39 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
40 char empty; /* Empty character */
41 unsigned char buf[BUF_SIZE + 3]; /* Output Buffer */
45 short signal; /* Signal level (file side) */
46 short ssindex; /* Signal ssindex (file side) */
47 struct ast_filestream *next;
51 static struct ast_filestream *glist = NULL;
52 static pthread_mutex_t vox_lock = PTHREAD_MUTEX_INITIALIZER;
53 static int glistcnt = 0;
55 static char *name = "vox";
56 static char *desc = "Dialogic VOX (ADPCM) File Format";
57 static char *exts = "vox";
60 * Step size index shift table
63 static short indsft[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
66 * Step size table, where stpsz[i]=floor[16*(11/10)^i]
69 static short stpsz[49] = {
70 16, 17, 19, 21, 23, 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73,
71 80, 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
72 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
73 1060, 1166, 1282, 1411, 1552
80 static short nbl2bit[16][4] = {
81 {1, 0, 0, 0}, {1, 0, 0, 1}, {1, 0, 1, 0}, {1, 0, 1, 1},
82 {1, 1, 0, 0}, {1, 1, 0, 1}, {1, 1, 1, 0}, {1, 1, 1, 1},
83 {-1, 0, 0, 0}, {-1, 0, 0, 1}, {-1, 0, 1, 0}, {-1, 0, 1, 1},
84 {-1, 1, 0, 0}, {-1, 1, 0, 1}, {-1, 1, 1, 0}, {-1, 1, 1, 1}
89 * Decodes the encoded nibble from the adpcm file.
92 * Returns the encoded difference.
95 * Sets the index to the step size table for the next encode.
99 decode (unsigned char encoded, short *ssindex)
102 step = stpsz[*ssindex];
103 diff = nbl2bit[encoded][0] * (step * nbl2bit[encoded][1] +
104 (step >> 1) * nbl2bit[encoded][2] +
105 (step >> 2) * nbl2bit[encoded][3] +
107 *ssindex = *ssindex + indsft[(encoded & 7)];
110 else if (*ssindex > 48)
117 * Takes a signed linear signal and encodes it as ADPCM
118 * For more information see http://support.dialogic.com/appnotes/adpcm.pdf
124 * signal gets updated with each pass.
127 static inline unsigned char
128 adpcm (short csig, short *ssindex, short *signal)
131 unsigned char encoded;
132 step = stpsz[*ssindex];
134 * Clip csig if too large or too small
139 diff = csig - *signal;
163 *signal += decode (encoded, ssindex);
167 static struct ast_filestream *vox_open(int fd)
169 /* We don't have any header to read or anything really, but
170 if we did, it would go here. We also might want to check
171 and be sure it's a valid file. */
172 struct ast_filestream *tmp;
173 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
174 memset(tmp, 0, sizeof(struct ast_filestream));
175 if (pthread_mutex_lock(&vox_lock)) {
176 ast_log(LOG_WARNING, "Unable to lock vox list\n");
184 tmp->fr.data = tmp->buf;
185 tmp->fr.frametype = AST_FRAME_VOICE;
186 tmp->fr.subclass = AST_FORMAT_ADPCM;
187 /* datalen will vary for each frame */
190 tmp->lasttimeout = -1;
192 pthread_mutex_unlock(&vox_lock);
193 ast_update_use_count();
198 static struct ast_filestream *vox_rewrite(int fd, char *comment)
200 /* We don't have any header to read or anything really, but
201 if we did, it would go here. We also might want to check
202 and be sure it's a valid file. */
203 struct ast_filestream *tmp;
204 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
205 memset(tmp, 0, sizeof(struct ast_filestream));
206 if (pthread_mutex_lock(&vox_lock)) {
207 ast_log(LOG_WARNING, "Unable to lock vox list\n");
215 tmp->lasttimeout = -1;
217 pthread_mutex_unlock(&vox_lock);
218 ast_update_use_count();
220 ast_log(LOG_WARNING, "Out of memory\n");
224 static struct ast_frame *vox_read(struct ast_filestream *s)
229 static void vox_close(struct ast_filestream *s)
231 struct ast_filestream *tmp, *tmpl = NULL;
232 if (pthread_mutex_lock(&vox_lock)) {
233 ast_log(LOG_WARNING, "Unable to lock vox list\n");
240 tmpl->next = tmp->next;
250 s->owner->stream = NULL;
251 if (s->owner->streamid > -1)
252 ast_sched_del(s->owner->sched, s->owner->streamid);
253 s->owner->streamid = -1;
255 pthread_mutex_unlock(&vox_lock);
256 ast_update_use_count();
258 ast_log(LOG_WARNING, "Freeing a filestream we don't seem to own\n");
263 static int ast_read_callback(void *data)
268 struct ast_filestream *s = data;
271 /* Send a frame from the file to the appropriate channel */
273 s->fr.frametype = AST_FRAME_VOICE;
274 s->fr.subclass = AST_FORMAT_ADPCM;
275 s->fr.offset = AST_FRIENDLY_OFFSET;
278 if ((res = read(s->fd, s->buf + 3, BUF_SIZE)) < 1) {
280 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
281 s->owner->streamid = -1;
284 /* Store index, then signal */
285 s->buf[0] = s->ssindex & 0xff;
286 s->buf[1] = (s->signal >> 8) & 0xff;
287 s->buf[2] = s->signal & 0xff;
288 /* Do the decoder to be sure we get the right stuff in the signal and index fields. */
289 for (x=3;x<res+3;x++) {
290 s->signal += decode(s->buf[x] >> 4, &s->ssindex);
291 s->signal += decode(s->buf[x] & 0xf, &s->ssindex);
293 s->fr.timelen = res / 4;
294 s->fr.datalen = res + 3;
295 delay = s->fr.timelen;
296 /* Lastly, process the frame */
297 if (ast_write(s->owner, &s->fr)) {
298 ast_log(LOG_WARNING, "Failed to write frame\n");
299 s->owner->streamid = -1;
302 if (s->last.tv_usec || s->last.tv_usec) {
304 gettimeofday(&tv, NULL);
305 ms = 1000 * (tv.tv_sec - s->last.tv_sec) +
306 (tv.tv_usec - s->last.tv_usec) / 1000;
307 s->last.tv_sec = tv.tv_sec;
308 s->last.tv_usec = tv.tv_usec;
309 if ((ms - delay) * (ms - delay) > 4) {
310 /* Compensate if we're more than 2 ms off */
311 s->adj -= (ms - delay);
314 fprintf(stdout, "Delay is %d, adjustment is %d, last was %d\n", delay, s->adj, ms);
320 gettimeofday(&s->last, NULL);
321 if (s->lasttimeout != delay) {
322 /* We'll install the next timeout now. */
323 s->owner->streamid = ast_sched_add(s->owner->sched,
324 delay, ast_read_callback, s);
325 s->lasttimeout = delay;
327 /* Just come back again at the same time */
333 static int vox_apply(struct ast_channel *c, struct ast_filestream *s)
335 /* Select our owner for this stream, and get the ball rolling. */
337 ast_read_callback(s);
341 static int vox_write(struct ast_filestream *fs, struct ast_frame *f)
344 if (f->frametype != AST_FRAME_VOICE) {
345 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
348 if (f->subclass != AST_FORMAT_ADPCM) {
349 ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%d)!\n", f->subclass);
352 if (f->datalen < 3) {
353 ast_log(LOG_WARNING, "Invalid frame of data (< 3 bytes long) from %s\n", f->src);
356 if ((res = write(fs->fd, f->data + 3, f->datalen)) != f->datalen) {
357 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
363 static char *vox_getcomment(struct ast_filestream *s)
370 return ast_format_register(name, exts, AST_FORMAT_ADPCM,
384 struct ast_filestream *tmp, *tmpl;
385 if (pthread_mutex_lock(&vox_lock)) {
386 ast_log(LOG_WARNING, "Unable to lock vox list\n");
392 ast_softhangup(tmp->owner);
397 pthread_mutex_unlock(&vox_lock);
398 return ast_format_unregister(name);
404 if (pthread_mutex_lock(&vox_lock)) {
405 ast_log(LOG_WARNING, "Unable to lock vox list\n");
409 pthread_mutex_unlock(&vox_lock);
421 return ASTERISK_GPL_KEY;