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 <netinet/in.h>
21 #include <arpa/inet.h>
32 #include <machine/endian.h>
35 #define BUF_SIZE 80 /* 160 samples */
37 struct ast_filestream {
38 void *reserved[AST_RESERVED_POINTERS];
39 /* This is what a filestream means to us */
40 int fd; /* Descriptor */
41 struct ast_frame fr; /* Frame information */
42 char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
43 char empty; /* Empty character */
44 unsigned char buf[BUF_SIZE + 3]; /* Output Buffer */
47 short signal; /* Signal level (file side) */
48 short ssindex; /* Signal ssindex (file side) */
52 static ast_mutex_t vox_lock = AST_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 (ast_mutex_lock(&vox_lock)) {
176 ast_log(LOG_WARNING, "Unable to lock vox list\n");
181 tmp->fr.data = tmp->buf;
182 tmp->fr.frametype = AST_FRAME_VOICE;
183 tmp->fr.subclass = AST_FORMAT_ADPCM;
184 /* datalen will vary for each frame */
187 tmp->lasttimeout = -1;
189 ast_mutex_unlock(&vox_lock);
190 ast_update_use_count();
195 static struct ast_filestream *vox_rewrite(int fd, char *comment)
197 /* We don't have any header to read or anything really, but
198 if we did, it would go here. We also might want to check
199 and be sure it's a valid file. */
200 struct ast_filestream *tmp;
201 if ((tmp = malloc(sizeof(struct ast_filestream)))) {
202 memset(tmp, 0, sizeof(struct ast_filestream));
203 if (ast_mutex_lock(&vox_lock)) {
204 ast_log(LOG_WARNING, "Unable to lock vox list\n");
210 ast_mutex_unlock(&vox_lock);
211 ast_update_use_count();
213 ast_log(LOG_WARNING, "Out of memory\n");
217 static void vox_close(struct ast_filestream *s)
219 if (ast_mutex_lock(&vox_lock)) {
220 ast_log(LOG_WARNING, "Unable to lock vox list\n");
224 ast_mutex_unlock(&vox_lock);
225 ast_update_use_count();
231 static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
235 /* Send a frame from the file to the appropriate channel */
237 s->fr.frametype = AST_FRAME_VOICE;
238 s->fr.subclass = AST_FORMAT_ADPCM;
239 s->fr.offset = AST_FRIENDLY_OFFSET;
242 if ((res = read(s->fd, s->buf + 3, BUF_SIZE)) < 1) {
244 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
247 /* Store index, then signal */
248 s->buf[0] = s->ssindex & 0xff;
249 s->buf[1] = (s->signal >> 8) & 0xff;
250 s->buf[2] = s->signal & 0xff;
251 /* Do the decoder to be sure we get the right stuff in the signal and index fields. */
252 for (x=3;x<res+3;x++) {
253 s->signal += decode(s->buf[x] >> 4, &s->ssindex);
254 s->signal += decode(s->buf[x] & 0xf, &s->ssindex);
256 s->fr.samples = res * 2;
257 s->fr.datalen = res + 3;
258 *whennext = s->fr.samples;
262 static int vox_write(struct ast_filestream *fs, struct ast_frame *f)
265 if (f->frametype != AST_FRAME_VOICE) {
266 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
269 if (f->subclass != AST_FORMAT_ADPCM) {
270 ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%d)!\n", f->subclass);
273 if (f->datalen < 3) {
274 ast_log(LOG_WARNING, "Invalid frame of data (< 3 bytes long) from %s\n", f->src);
277 if ((res = write(fs->fd, f->data + 3, f->datalen - 3)) != f->datalen - 3) {
278 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
284 static char *vox_getcomment(struct ast_filestream *s)
289 static int vox_seek(struct ast_filestream *fs, long sample_offset, int whence)
294 static int vox_trunc(struct ast_filestream *fs)
299 static long vox_tell(struct ast_filestream *fs)
306 return ast_format_register(name, exts, AST_FORMAT_ADPCM,
322 return ast_format_unregister(name);
328 if (ast_mutex_lock(&vox_lock)) {
329 ast_log(LOG_WARNING, "Unable to lock vox list\n");
333 ast_mutex_unlock(&vox_lock);
345 return ASTERISK_GPL_KEY;