Merge changes from team/russell/issue_9520
[asterisk-bristuff.git] / formats / format_wav_gsm.c
blob1655cbd21e1008c9498e9e3ae55dfc4c2b5c88b1
1 /*
2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005, Digium, Inc.
6 * Mark Spencer <markster@digium.com>
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
19 /*! \file
21 * \brief Save GSM in the proprietary Microsoft format.
23 * Microsoft WAV format (Proprietary GSM)
24 * \arg File name extension: WAV,wav49 (Upper case WAV, lower case is another format)
25 * This format can be played on Windows systems, used for
26 * e-mail attachments mainly.
27 * \ingroup formats
30 #include "asterisk.h"
32 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
34 #include <unistd.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <stdlib.h>
38 #include <sys/time.h>
39 #include <stdio.h>
40 #include <errno.h>
41 #include <string.h>
43 #include "asterisk/lock.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/file.h"
46 #include "asterisk/logger.h"
47 #include "asterisk/sched.h"
48 #include "asterisk/module.h"
49 #include "asterisk/endian.h"
51 #include "msgsm.h"
53 /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
55 /* Portions of the conversion code are by guido@sienanet.it */
57 #define GSM_FRAME_SIZE 33
58 #define MSGSM_FRAME_SIZE 65
59 #define MSGSM_DATA_OFFSET 60 /* offset of data bytes */
60 #define GSM_SAMPLES 160 /* samples in a GSM block */
61 #define MSGSM_SAMPLES (2*GSM_SAMPLES) /* samples in an MSGSM block */
63 /* begin binary data: */
64 char msgsm_silence[] = /* 65 */
65 {0x48,0x17,0xD6,0x84,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49
66 ,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92
67 ,0x24,0x09,0x82,0x74,0x61,0x4D,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00
68 ,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48
69 ,0x92,0x24,0x49,0x92,0x00};
70 /* end binary data. size = 65 bytes */
72 struct wavg_desc {
73 /* Believe it or not, we must decode/recode to account for the
74 weird MS format */
75 int secondhalf; /* Are we on the second half */
78 #if __BYTE_ORDER == __LITTLE_ENDIAN
79 #define htoll(b) (b)
80 #define htols(b) (b)
81 #define ltohl(b) (b)
82 #define ltohs(b) (b)
83 #else
84 #if __BYTE_ORDER == __BIG_ENDIAN
85 #define htoll(b) \
86 (((((b) ) & 0xFF) << 24) | \
87 ((((b) >> 8) & 0xFF) << 16) | \
88 ((((b) >> 16) & 0xFF) << 8) | \
89 ((((b) >> 24) & 0xFF) ))
90 #define htols(b) \
91 (((((b) ) & 0xFF) << 8) | \
92 ((((b) >> 8) & 0xFF) ))
93 #define ltohl(b) htoll(b)
94 #define ltohs(b) htols(b)
95 #else
96 #error "Endianess not defined"
97 #endif
98 #endif
101 static int check_header(FILE *f)
103 int type, size, formtype;
104 int fmt, hsize, fact;
105 short format, chans;
106 int freq;
107 int data;
108 if (fread(&type, 1, 4, f) != 4) {
109 ast_log(LOG_WARNING, "Read failed (type)\n");
110 return -1;
112 if (fread(&size, 1, 4, f) != 4) {
113 ast_log(LOG_WARNING, "Read failed (size)\n");
114 return -1;
116 size = ltohl(size);
117 if (fread(&formtype, 1, 4, f) != 4) {
118 ast_log(LOG_WARNING, "Read failed (formtype)\n");
119 return -1;
121 if (memcmp(&type, "RIFF", 4)) {
122 ast_log(LOG_WARNING, "Does not begin with RIFF\n");
123 return -1;
125 if (memcmp(&formtype, "WAVE", 4)) {
126 ast_log(LOG_WARNING, "Does not contain WAVE\n");
127 return -1;
129 if (fread(&fmt, 1, 4, f) != 4) {
130 ast_log(LOG_WARNING, "Read failed (fmt)\n");
131 return -1;
133 if (memcmp(&fmt, "fmt ", 4)) {
134 ast_log(LOG_WARNING, "Does not say fmt\n");
135 return -1;
137 if (fread(&hsize, 1, 4, f) != 4) {
138 ast_log(LOG_WARNING, "Read failed (formtype)\n");
139 return -1;
141 if (ltohl(hsize) != 20) {
142 ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
143 return -1;
145 if (fread(&format, 1, 2, f) != 2) {
146 ast_log(LOG_WARNING, "Read failed (format)\n");
147 return -1;
149 if (ltohs(format) != 49) {
150 ast_log(LOG_WARNING, "Not a GSM file %d\n", ltohs(format));
151 return -1;
153 if (fread(&chans, 1, 2, f) != 2) {
154 ast_log(LOG_WARNING, "Read failed (format)\n");
155 return -1;
157 if (ltohs(chans) != 1) {
158 ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
159 return -1;
161 if (fread(&freq, 1, 4, f) != 4) {
162 ast_log(LOG_WARNING, "Read failed (freq)\n");
163 return -1;
165 if (ltohl(freq) != DEFAULT_SAMPLE_RATE) {
166 ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq));
167 return -1;
169 /* Ignore the byte frequency */
170 if (fread(&freq, 1, 4, f) != 4) {
171 ast_log(LOG_WARNING, "Read failed (X_1)\n");
172 return -1;
174 /* Ignore the two weird fields */
175 if (fread(&freq, 1, 4, f) != 4) {
176 ast_log(LOG_WARNING, "Read failed (X_2/X_3)\n");
177 return -1;
179 /* Ignore the byte frequency */
180 if (fread(&freq, 1, 4, f) != 4) {
181 ast_log(LOG_WARNING, "Read failed (Y_1)\n");
182 return -1;
184 /* Check for the word fact */
185 if (fread(&fact, 1, 4, f) != 4) {
186 ast_log(LOG_WARNING, "Read failed (fact)\n");
187 return -1;
189 if (memcmp(&fact, "fact", 4)) {
190 ast_log(LOG_WARNING, "Does not say fact\n");
191 return -1;
193 /* Ignore the "fact value" */
194 if (fread(&fact, 1, 4, f) != 4) {
195 ast_log(LOG_WARNING, "Read failed (fact header)\n");
196 return -1;
198 if (fread(&fact, 1, 4, f) != 4) {
199 ast_log(LOG_WARNING, "Read failed (fact value)\n");
200 return -1;
202 /* Check for the word data */
203 if (fread(&data, 1, 4, f) != 4) {
204 ast_log(LOG_WARNING, "Read failed (data)\n");
205 return -1;
207 if (memcmp(&data, "data", 4)) {
208 ast_log(LOG_WARNING, "Does not say data\n");
209 return -1;
211 /* Ignore the data length */
212 if (fread(&data, 1, 4, f) != 4) {
213 ast_log(LOG_WARNING, "Read failed (data)\n");
214 return -1;
216 return 0;
219 static int update_header(FILE *f)
221 off_t cur,end,bytes;
222 int datalen, filelen, samples;
224 cur = ftello(f);
225 fseek(f, 0, SEEK_END);
226 end = ftello(f);
227 /* in a gsm WAV, data starts 60 bytes in */
228 bytes = end - MSGSM_DATA_OFFSET;
229 samples = htoll(bytes / MSGSM_FRAME_SIZE * MSGSM_SAMPLES);
230 datalen = htoll(bytes);
231 filelen = htoll(MSGSM_DATA_OFFSET - 8 + bytes);
232 if (cur < 0) {
233 ast_log(LOG_WARNING, "Unable to find our position\n");
234 return -1;
236 if (fseek(f, 4, SEEK_SET)) {
237 ast_log(LOG_WARNING, "Unable to set our position\n");
238 return -1;
240 if (fwrite(&filelen, 1, 4, f) != 4) {
241 ast_log(LOG_WARNING, "Unable to write file size\n");
242 return -1;
244 if (fseek(f, 48, SEEK_SET)) {
245 ast_log(LOG_WARNING, "Unable to set our position\n");
246 return -1;
248 if (fwrite(&samples, 1, 4, f) != 4) {
249 ast_log(LOG_WARNING, "Unable to write samples\n");
250 return -1;
252 if (fseek(f, 56, SEEK_SET)) {
253 ast_log(LOG_WARNING, "Unable to set our position\n");
254 return -1;
256 if (fwrite(&datalen, 1, 4, f) != 4) {
257 ast_log(LOG_WARNING, "Unable to write datalen\n");
258 return -1;
260 if (fseeko(f, cur, SEEK_SET)) {
261 ast_log(LOG_WARNING, "Unable to return to position\n");
262 return -1;
264 return 0;
267 static int write_header(FILE *f)
269 /* Samples per second (always 8000 for this format). */
270 unsigned int sample_rate = htoll(8000);
271 /* Bytes per second (always 1625 for this format). */
272 unsigned int byte_sample_rate = htoll(1625);
273 /* This is the size of the "fmt " subchunk */
274 unsigned int fmtsize = htoll(20);
275 /* WAV #49 */
276 unsigned short fmt = htols(49);
277 /* Mono = 1 channel */
278 unsigned short chans = htols(1);
279 /* Each block of data is exactly 65 bytes in size. */
280 unsigned int block_align = htoll(MSGSM_FRAME_SIZE);
281 /* Not actually 2, but rounded up to the nearest bit */
282 unsigned short bits_per_sample = htols(2);
283 /* Needed for compressed formats */
284 unsigned short extra_format = htols(MSGSM_SAMPLES);
285 /* This is the size of the "fact" subchunk */
286 unsigned int factsize = htoll(4);
287 /* Number of samples in the data chunk */
288 unsigned int num_samples = htoll(0);
289 /* Number of bytes in the data chunk */
290 unsigned int size = htoll(0);
291 /* Write a GSM header, ignoring sizes which will be filled in later */
293 /* 0: Chunk ID */
294 if (fwrite("RIFF", 1, 4, f) != 4) {
295 ast_log(LOG_WARNING, "Unable to write header\n");
296 return -1;
298 /* 4: Chunk Size */
299 if (fwrite(&size, 1, 4, f) != 4) {
300 ast_log(LOG_WARNING, "Unable to write header\n");
301 return -1;
303 /* 8: Chunk Format */
304 if (fwrite("WAVE", 1, 4, f) != 4) {
305 ast_log(LOG_WARNING, "Unable to write header\n");
306 return -1;
308 /* 12: Subchunk 1: ID */
309 if (fwrite("fmt ", 1, 4, f) != 4) {
310 ast_log(LOG_WARNING, "Unable to write header\n");
311 return -1;
313 /* 16: Subchunk 1: Size (minus 8) */
314 if (fwrite(&fmtsize, 1, 4, f) != 4) {
315 ast_log(LOG_WARNING, "Unable to write header\n");
316 return -1;
318 /* 20: Subchunk 1: Audio format (49) */
319 if (fwrite(&fmt, 1, 2, f) != 2) {
320 ast_log(LOG_WARNING, "Unable to write header\n");
321 return -1;
323 /* 22: Subchunk 1: Number of channels */
324 if (fwrite(&chans, 1, 2, f) != 2) {
325 ast_log(LOG_WARNING, "Unable to write header\n");
326 return -1;
328 /* 24: Subchunk 1: Sample rate */
329 if (fwrite(&sample_rate, 1, 4, f) != 4) {
330 ast_log(LOG_WARNING, "Unable to write header\n");
331 return -1;
333 /* 28: Subchunk 1: Byte rate */
334 if (fwrite(&byte_sample_rate, 1, 4, f) != 4) {
335 ast_log(LOG_WARNING, "Unable to write header\n");
336 return -1;
338 /* 32: Subchunk 1: Block align */
339 if (fwrite(&block_align, 1, 4, f) != 4) {
340 ast_log(LOG_WARNING, "Unable to write header\n");
341 return -1;
343 /* 36: Subchunk 1: Bits per sample */
344 if (fwrite(&bits_per_sample, 1, 2, f) != 2) {
345 ast_log(LOG_WARNING, "Unable to write header\n");
346 return -1;
348 /* 38: Subchunk 1: Extra format bytes */
349 if (fwrite(&extra_format, 1, 2, f) != 2) {
350 ast_log(LOG_WARNING, "Unable to write header\n");
351 return -1;
353 /* 40: Subchunk 2: ID */
354 if (fwrite("fact", 1, 4, f) != 4) {
355 ast_log(LOG_WARNING, "Unable to write header\n");
356 return -1;
358 /* 44: Subchunk 2: Size (minus 8) */
359 if (fwrite(&factsize, 1, 4, f) != 4) {
360 ast_log(LOG_WARNING, "Unable to write header\n");
361 return -1;
363 /* 48: Subchunk 2: Number of samples */
364 if (fwrite(&num_samples, 1, 4, f) != 4) {
365 ast_log(LOG_WARNING, "Unable to write header\n");
366 return -1;
368 /* 52: Subchunk 3: ID */
369 if (fwrite("data", 1, 4, f) != 4) {
370 ast_log(LOG_WARNING, "Unable to write header\n");
371 return -1;
373 /* 56: Subchunk 3: Size */
374 if (fwrite(&size, 1, 4, f) != 4) {
375 ast_log(LOG_WARNING, "Unable to write header\n");
376 return -1;
378 return 0;
381 static int wav_open(struct ast_filestream *s)
383 /* We don't have any header to read or anything really, but
384 if we did, it would go here. We also might want to check
385 and be sure it's a valid file. */
386 struct wavg_desc *fs = (struct wavg_desc *)s->_private;
388 if (check_header(s->f))
389 return -1;
390 fs->secondhalf = 0; /* not strictly necessary */
391 return 0;
394 static int wav_rewrite(struct ast_filestream *s, const char *comment)
396 /* We don't have any header to read or anything really, but
397 if we did, it would go here. We also might want to check
398 and be sure it's a valid file. */
400 if (write_header(s->f))
401 return -1;
402 return 0;
405 static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
407 /* Send a frame from the file to the appropriate channel */
408 struct wavg_desc *fs = (struct wavg_desc *)s->_private;
410 s->fr.frametype = AST_FRAME_VOICE;
411 s->fr.subclass = AST_FORMAT_GSM;
412 s->fr.offset = AST_FRIENDLY_OFFSET;
413 s->fr.samples = GSM_SAMPLES;
414 s->fr.mallocd = 0;
415 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE);
416 if (fs->secondhalf) {
417 /* Just return a frame based on the second GSM frame */
418 s->fr.data = (char *)s->fr.data + GSM_FRAME_SIZE;
419 s->fr.offset += GSM_FRAME_SIZE;
420 } else {
421 /* read and convert */
422 unsigned char msdata[MSGSM_FRAME_SIZE];
423 int res;
425 if ((res = fread(msdata, 1, MSGSM_FRAME_SIZE, s->f)) != MSGSM_FRAME_SIZE) {
426 if (res && (res != 1))
427 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
428 return NULL;
430 /* Convert from MS format to two real GSM frames */
431 conv65(msdata, s->fr.data);
433 fs->secondhalf = !fs->secondhalf;
434 *whennext = GSM_SAMPLES;
435 return &s->fr;
438 static int wav_write(struct ast_filestream *s, struct ast_frame *f)
440 int len;
441 int size;
442 struct wavg_desc *fs = (struct wavg_desc *)s->_private;
444 if (f->frametype != AST_FRAME_VOICE) {
445 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
446 return -1;
448 if (f->subclass != AST_FORMAT_GSM) {
449 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
450 return -1;
452 /* XXX this might fail... if the input is a multiple of MSGSM_FRAME_SIZE
453 * we assume it is already in the correct format.
455 if (!(f->datalen % MSGSM_FRAME_SIZE)) {
456 size = MSGSM_FRAME_SIZE;
457 fs->secondhalf = 0;
458 } else {
459 size = GSM_FRAME_SIZE;
461 for (len = 0; len < f->datalen ; len += size) {
462 int res;
463 unsigned char *src, msdata[MSGSM_FRAME_SIZE];
464 if (fs->secondhalf) { /* second half of raw gsm to be converted */
465 memcpy(s->buf + GSM_FRAME_SIZE, f->data + len, GSM_FRAME_SIZE);
466 conv66((unsigned char *) s->buf, msdata);
467 src = msdata;
468 fs->secondhalf = 0;
469 } else if (size == GSM_FRAME_SIZE) { /* first half of raw gsm */
470 memcpy(s->buf, f->data + len, GSM_FRAME_SIZE);
471 src = NULL; /* nothing to write */
472 fs->secondhalf = 1;
473 } else { /* raw msgsm data */
474 src = f->data + len;
476 if (src && (res = fwrite(src, 1, MSGSM_FRAME_SIZE, s->f)) != MSGSM_FRAME_SIZE) {
477 ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
478 return -1;
480 update_header(s->f); /* XXX inefficient! */
482 return 0;
485 static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
487 off_t offset=0, distance, max;
488 struct wavg_desc *s = (struct wavg_desc *)fs->_private;
490 off_t min = MSGSM_DATA_OFFSET;
491 off_t cur = ftello(fs->f);
492 fseek(fs->f, 0, SEEK_END);
493 max = ftello(fs->f); /* XXX ideally, should round correctly */
494 /* Compute the distance in bytes, rounded to the block size */
495 distance = (sample_offset/MSGSM_SAMPLES) * MSGSM_FRAME_SIZE;
496 if (whence == SEEK_SET)
497 offset = distance + min;
498 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
499 offset = distance + cur;
500 else if (whence == SEEK_END)
501 offset = max - distance;
502 /* always protect against seeking past end of header */
503 if (offset < min)
504 offset = min;
505 if (whence != SEEK_FORCECUR) {
506 if (offset > max)
507 offset = max;
508 } else if (offset > max) {
509 int i;
510 fseek(fs->f, 0, SEEK_END);
511 for (i=0; i< (offset - max) / MSGSM_FRAME_SIZE; i++) {
512 fwrite(msgsm_silence, 1, MSGSM_FRAME_SIZE, fs->f);
515 s->secondhalf = 0;
516 return fseeko(fs->f, offset, SEEK_SET);
519 static int wav_trunc(struct ast_filestream *fs)
521 if (ftruncate(fileno(fs->f), ftello(fs->f)))
522 return -1;
523 return update_header(fs->f);
526 static off_t wav_tell(struct ast_filestream *fs)
528 off_t offset;
529 offset = ftello(fs->f);
530 /* since this will most likely be used later in play or record, lets stick
531 * to that level of resolution, just even frames boundaries */
532 return (offset - MSGSM_DATA_OFFSET)/MSGSM_FRAME_SIZE*MSGSM_SAMPLES;
535 static const struct ast_format wav49_f = {
536 .name = "wav49",
537 .exts = "WAV|wav49",
538 .format = AST_FORMAT_GSM,
539 .open = wav_open,
540 .rewrite = wav_rewrite,
541 .write = wav_write,
542 .seek = wav_seek,
543 .trunc = wav_trunc,
544 .tell = wav_tell,
545 .read = wav_read,
546 .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET,
547 .desc_size = sizeof(struct wavg_desc),
550 static int load_module(void)
552 return ast_format_register(&wav49_f);
555 static int unload_module(void)
557 return ast_format_unregister(wav49_f.name);
560 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Microsoft WAV format (Proprietary GSM)");