Properly implement using zaptel for timing of file playback
[asterisk/asterisk.git] / formats / format_vox.c
1 /*
2  * Asterisk -- A telephony toolkit for Linux.
3  *
4  * Flat, binary, ADPCM vox file format.
5  * 
6  * Copyright (C) 1999, Mark Spencer
7  *
8  * Mark Spencer <markster@linux-support.net>
9  *
10  * This program is free software, distributed under the terms of
11  * the GNU General Public License
12  */
13  
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>
21 #include <stdlib.h>
22 #include <sys/time.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <pthread.h>
28 #ifdef __linux__
29 #include <endian.h>
30 #else
31 #include <machine/endian.h>
32 #endif
33
34 #define BUF_SIZE 80             /* 160 samples */
35
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 */
44         int lasttimeout;
45         struct timeval last;
46         short signal;                                           /* Signal level (file side) */
47         short ssindex;                                          /* Signal ssindex (file side) */
48 };
49
50
51 static pthread_mutex_t vox_lock = AST_MUTEX_INITIALIZER;
52 static int glistcnt = 0;
53
54 static char *name = "vox";
55 static char *desc = "Dialogic VOX (ADPCM) File Format";
56 static char *exts = "vox";
57
58 /*
59  * Step size index shift table 
60  */
61
62 static short indsft[8] = { -1, -1, -1, -1, 2, 4, 6, 8 };
63
64 /*
65  * Step size table, where stpsz[i]=floor[16*(11/10)^i]
66  */
67
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
73 };
74
75 /* 
76  * Nibble to bit map
77  */
78
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}
84 };
85
86 /*
87  * Decode(encoded)
88  *  Decodes the encoded nibble from the adpcm file.
89  *
90  * Results:
91  *  Returns the encoded difference.
92  *
93  * Side effects:
94  *  Sets the index to the step size table for the next encode.
95  */
96
97 static inline short
98 decode (unsigned char encoded, short *ssindex)
99 {
100   short diff, step;
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] +
105                                 (step >> 3));
106   *ssindex = *ssindex + indsft[(encoded & 7)];
107   if (*ssindex < 0)
108     *ssindex = 0;
109   else if (*ssindex > 48)
110     *ssindex = 48;
111   return (diff);
112 }
113
114 /*
115  * Adpcm
116  *  Takes a signed linear signal and encodes it as ADPCM
117  *  For more information see http://support.dialogic.com/appnotes/adpcm.pdf
118  *
119  * Results:
120  *  Foo.
121  *
122  * Side effects:
123  *  signal gets updated with each pass.
124  */
125
126 static inline unsigned char
127 adpcm (short csig, short *ssindex, short *signal)
128 {
129   short diff, step;
130   unsigned char encoded;
131   step = stpsz[*ssindex];
132   /* 
133    * Clip csig if too large or too small
134    */
135    
136   csig >>= 4;
137
138   diff = csig - *signal;
139   
140   if (diff < 0)
141     {
142       encoded = 8;
143       diff = -diff;
144     }
145   else
146     encoded = 0;
147   if (diff >= step)
148     {
149       encoded |= 4;
150       diff -= step;
151     }
152   step >>= 1;
153   if (diff >= step)
154     {
155       encoded |= 2;
156       diff -= step;
157     }
158   step >>= 1;
159   if (diff >= step)
160     encoded |= 1;
161     
162   *signal += decode (encoded, ssindex);
163   return (encoded);
164 }
165
166 static struct ast_filestream *vox_open(int fd)
167 {
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");
176                         free(tmp);
177                         return NULL;
178                 }
179                 tmp->fd = fd;
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 */
184                 tmp->fr.src = name;
185                 tmp->fr.mallocd = 0;
186                 tmp->lasttimeout = -1;
187                 glistcnt++;
188                 ast_pthread_mutex_unlock(&vox_lock);
189                 ast_update_use_count();
190         }
191         return tmp;
192 }
193
194 static struct ast_filestream *vox_rewrite(int fd, char *comment)
195 {
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");
204                         free(tmp);
205                         return NULL;
206                 }
207                 tmp->fd = fd;
208                 glistcnt++;
209                 ast_pthread_mutex_unlock(&vox_lock);
210                 ast_update_use_count();
211         } else
212                 ast_log(LOG_WARNING, "Out of memory\n");
213         return tmp;
214 }
215
216 static void vox_close(struct ast_filestream *s)
217 {
218         if (ast_pthread_mutex_lock(&vox_lock)) {
219                 ast_log(LOG_WARNING, "Unable to lock vox list\n");
220                 return;
221         }
222         glistcnt--;
223         ast_pthread_mutex_unlock(&vox_lock);
224         ast_update_use_count();
225         close(s->fd);
226         free(s);
227         s = NULL;
228 }
229
230 static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext)
231 {
232         int res;
233         int x;
234         /* Send a frame from the file to the appropriate channel */
235
236         s->fr.frametype = AST_FRAME_VOICE;
237         s->fr.subclass = AST_FORMAT_ADPCM;
238         s->fr.offset = AST_FRIENDLY_OFFSET;
239         s->fr.mallocd = 0;
240         s->fr.data = s->buf;
241         if ((res = read(s->fd, s->buf + 3, BUF_SIZE)) < 1) {
242                 if (res)
243                         ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
244                 return NULL;
245         }
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);
254         }
255         s->fr.samples = res * 2;
256         s->fr.datalen = res + 3;
257         *whennext = s->fr.samples;
258         return &s->fr;
259 }
260
261 static int vox_write(struct ast_filestream *fs, struct ast_frame *f)
262 {
263         int res;
264         if (f->frametype != AST_FRAME_VOICE) {
265                 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
266                 return -1;
267         }
268         if (f->subclass != AST_FORMAT_ADPCM) {
269                 ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%d)!\n", f->subclass);
270                 return -1;
271         }
272         if (f->datalen < 3) {
273                 ast_log(LOG_WARNING, "Invalid frame of data (< 3 bytes long) from %s\n", f->src);
274                 return -1;
275         }
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));
278                         return -1;
279         }
280         return 0;
281 }
282
283 static char *vox_getcomment(struct ast_filestream *s)
284 {
285         return NULL;
286 }
287
288 static int vox_seek(struct ast_filestream *fs, long sample_offset, int whence)
289 {
290         return -1;
291 }
292
293 static int vox_trunc(struct ast_filestream *fs)
294 {
295         return -1;
296 }
297
298 static long vox_tell(struct ast_filestream *fs)
299 {
300         return -1;
301 }
302
303 int load_module()
304 {
305         return ast_format_register(name, exts, AST_FORMAT_ADPCM,
306                                                                 vox_open,
307                                                                 vox_rewrite,
308                                                                 vox_write,
309                                                                 vox_seek,
310                                                                 vox_trunc,
311                                                                 vox_tell,
312                                                                 vox_read,
313                                                                 vox_close,
314                                                                 vox_getcomment);
315                                                                 
316                                                                 
317 }
318
319 int unload_module()
320 {
321         return ast_format_unregister(name);
322 }       
323
324 int usecount()
325 {
326         int res;
327         if (ast_pthread_mutex_lock(&vox_lock)) {
328                 ast_log(LOG_WARNING, "Unable to lock vox list\n");
329                 return -1;
330         }
331         res = glistcnt;
332         ast_pthread_mutex_unlock(&vox_lock);
333         return res;
334 }
335
336 char *description()
337 {
338         return desc;
339 }
340
341
342 char *key()
343 {
344         return ASTERISK_GPL_KEY;
345 }