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.
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.
31 <support_level>core</support_level>
36 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
38 #include "asterisk/mod_format.h"
39 #include "asterisk/module.h"
40 #include "asterisk/endian.h"
44 /* Some Ideas for this code came from makewave.c by Jeffrey Chilton */
46 /* Portions of the conversion code are by guido@sienanet.it */
48 #define GSM_FRAME_SIZE 33
49 #define MSGSM_FRAME_SIZE 65
50 #define MSGSM_DATA_OFFSET 60 /* offset of data bytes */
51 #define GSM_SAMPLES 160 /* samples in a GSM block */
52 #define MSGSM_SAMPLES (2*GSM_SAMPLES) /* samples in an MSGSM block */
54 /* begin binary data: */
55 static char msgsm_silence[] = /* 65 */
56 {0x48,0x17,0xD6,0x84,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49
57 ,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92
58 ,0x24,0x09,0x82,0x74,0x61,0x4D,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00
59 ,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48
60 ,0x92,0x24,0x49,0x92,0x00};
61 /* end binary data. size = 65 bytes */
64 /* Believe it or not, we must decode/recode to account for the
66 int secondhalf; /* Are we on the second half */
69 #if __BYTE_ORDER == __LITTLE_ENDIAN
75 #if __BYTE_ORDER == __BIG_ENDIAN
77 (((((b) ) & 0xFF) << 24) | \
78 ((((b) >> 8) & 0xFF) << 16) | \
79 ((((b) >> 16) & 0xFF) << 8) | \
80 ((((b) >> 24) & 0xFF) ))
82 (((((b) ) & 0xFF) << 8) | \
83 ((((b) >> 8) & 0xFF) ))
84 #define ltohl(b) htoll(b)
85 #define ltohs(b) htols(b)
87 #error "Endianess not defined"
92 static int check_header(FILE *f)
94 int type, size, formtype;
99 if (fread(&type, 1, 4, f) != 4) {
100 ast_log(LOG_WARNING, "Read failed (type)\n");
103 if (fread(&size, 1, 4, f) != 4) {
104 ast_log(LOG_WARNING, "Read failed (size)\n");
108 if (fread(&formtype, 1, 4, f) != 4) {
109 ast_log(LOG_WARNING, "Read failed (formtype)\n");
112 if (memcmp(&type, "RIFF", 4)) {
113 ast_log(LOG_WARNING, "Does not begin with RIFF\n");
116 if (memcmp(&formtype, "WAVE", 4)) {
117 ast_log(LOG_WARNING, "Does not contain WAVE\n");
120 if (fread(&fmt, 1, 4, f) != 4) {
121 ast_log(LOG_WARNING, "Read failed (fmt)\n");
124 if (memcmp(&fmt, "fmt ", 4)) {
125 ast_log(LOG_WARNING, "Does not say fmt\n");
128 if (fread(&hsize, 1, 4, f) != 4) {
129 ast_log(LOG_WARNING, "Read failed (formtype)\n");
132 if (ltohl(hsize) != 20) {
133 ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
136 if (fread(&format, 1, 2, f) != 2) {
137 ast_log(LOG_WARNING, "Read failed (format)\n");
140 if (ltohs(format) != 49) {
141 ast_log(LOG_WARNING, "Not a GSM file %d\n", ltohs(format));
144 if (fread(&chans, 1, 2, f) != 2) {
145 ast_log(LOG_WARNING, "Read failed (format)\n");
148 if (ltohs(chans) != 1) {
149 ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
152 if (fread(&freq, 1, 4, f) != 4) {
153 ast_log(LOG_WARNING, "Read failed (freq)\n");
156 if (ltohl(freq) != DEFAULT_SAMPLE_RATE) {
157 ast_log(LOG_WARNING, "Unexpected frequency %d\n", ltohl(freq));
160 /* Ignore the byte frequency */
161 if (fread(&freq, 1, 4, f) != 4) {
162 ast_log(LOG_WARNING, "Read failed (X_1)\n");
165 /* Ignore the two weird fields */
166 if (fread(&freq, 1, 4, f) != 4) {
167 ast_log(LOG_WARNING, "Read failed (X_2/X_3)\n");
170 /* Ignore the byte frequency */
171 if (fread(&freq, 1, 4, f) != 4) {
172 ast_log(LOG_WARNING, "Read failed (Y_1)\n");
175 /* Check for the word fact */
176 if (fread(&fact, 1, 4, f) != 4) {
177 ast_log(LOG_WARNING, "Read failed (fact)\n");
180 if (memcmp(&fact, "fact", 4)) {
181 ast_log(LOG_WARNING, "Does not say fact\n");
184 /* Ignore the "fact value" */
185 if (fread(&fact, 1, 4, f) != 4) {
186 ast_log(LOG_WARNING, "Read failed (fact header)\n");
189 if (fread(&fact, 1, 4, f) != 4) {
190 ast_log(LOG_WARNING, "Read failed (fact value)\n");
193 /* Check for the word data */
194 if (fread(&data, 1, 4, f) != 4) {
195 ast_log(LOG_WARNING, "Read failed (data)\n");
198 if (memcmp(&data, "data", 4)) {
199 ast_log(LOG_WARNING, "Does not say data\n");
202 /* Ignore the data length */
203 if (fread(&data, 1, 4, f) != 4) {
204 ast_log(LOG_WARNING, "Read failed (data)\n");
210 static int update_header(FILE *f)
213 int datalen, filelen, samples;
216 fseek(f, 0, SEEK_END);
218 /* in a gsm WAV, data starts 60 bytes in */
219 bytes = end - MSGSM_DATA_OFFSET;
220 samples = htoll(bytes / MSGSM_FRAME_SIZE * MSGSM_SAMPLES);
221 datalen = htoll(bytes);
222 filelen = htoll(MSGSM_DATA_OFFSET - 8 + bytes);
224 ast_log(LOG_WARNING, "Unable to find our position\n");
227 if (fseek(f, 4, SEEK_SET)) {
228 ast_log(LOG_WARNING, "Unable to set our position\n");
231 if (fwrite(&filelen, 1, 4, f) != 4) {
232 ast_log(LOG_WARNING, "Unable to write file size\n");
235 if (fseek(f, 48, SEEK_SET)) {
236 ast_log(LOG_WARNING, "Unable to set our position\n");
239 if (fwrite(&samples, 1, 4, f) != 4) {
240 ast_log(LOG_WARNING, "Unable to write samples\n");
243 if (fseek(f, 56, SEEK_SET)) {
244 ast_log(LOG_WARNING, "Unable to set our position\n");
247 if (fwrite(&datalen, 1, 4, f) != 4) {
248 ast_log(LOG_WARNING, "Unable to write datalen\n");
251 if (fseeko(f, cur, SEEK_SET)) {
252 ast_log(LOG_WARNING, "Unable to return to position\n");
258 static int write_header(FILE *f)
260 /* Samples per second (always 8000 for this format). */
261 unsigned int sample_rate = htoll(8000);
262 /* Bytes per second (always 1625 for this format). */
263 unsigned int byte_sample_rate = htoll(1625);
264 /* This is the size of the "fmt " subchunk */
265 unsigned int fmtsize = htoll(20);
267 unsigned short fmt = htols(49);
268 /* Mono = 1 channel */
269 unsigned short chans = htols(1);
270 /* Each block of data is exactly 65 bytes in size. */
271 unsigned int block_align = htoll(MSGSM_FRAME_SIZE);
272 /* Not actually 2, but rounded up to the nearest bit */
273 unsigned short bits_per_sample = htols(2);
274 /* Needed for compressed formats */
275 unsigned short extra_format = htols(MSGSM_SAMPLES);
276 /* This is the size of the "fact" subchunk */
277 unsigned int factsize = htoll(4);
278 /* Number of samples in the data chunk */
279 unsigned int num_samples = htoll(0);
280 /* Number of bytes in the data chunk */
281 unsigned int size = htoll(0);
282 /* Write a GSM header, ignoring sizes which will be filled in later */
285 if (fwrite("RIFF", 1, 4, f) != 4) {
286 ast_log(LOG_WARNING, "Unable to write header\n");
290 if (fwrite(&size, 1, 4, f) != 4) {
291 ast_log(LOG_WARNING, "Unable to write header\n");
294 /* 8: Chunk Format */
295 if (fwrite("WAVE", 1, 4, f) != 4) {
296 ast_log(LOG_WARNING, "Unable to write header\n");
299 /* 12: Subchunk 1: ID */
300 if (fwrite("fmt ", 1, 4, f) != 4) {
301 ast_log(LOG_WARNING, "Unable to write header\n");
304 /* 16: Subchunk 1: Size (minus 8) */
305 if (fwrite(&fmtsize, 1, 4, f) != 4) {
306 ast_log(LOG_WARNING, "Unable to write header\n");
309 /* 20: Subchunk 1: Audio format (49) */
310 if (fwrite(&fmt, 1, 2, f) != 2) {
311 ast_log(LOG_WARNING, "Unable to write header\n");
314 /* 22: Subchunk 1: Number of channels */
315 if (fwrite(&chans, 1, 2, f) != 2) {
316 ast_log(LOG_WARNING, "Unable to write header\n");
319 /* 24: Subchunk 1: Sample rate */
320 if (fwrite(&sample_rate, 1, 4, f) != 4) {
321 ast_log(LOG_WARNING, "Unable to write header\n");
324 /* 28: Subchunk 1: Byte rate */
325 if (fwrite(&byte_sample_rate, 1, 4, f) != 4) {
326 ast_log(LOG_WARNING, "Unable to write header\n");
329 /* 32: Subchunk 1: Block align */
330 if (fwrite(&block_align, 1, 4, f) != 4) {
331 ast_log(LOG_WARNING, "Unable to write header\n");
334 /* 36: Subchunk 1: Bits per sample */
335 if (fwrite(&bits_per_sample, 1, 2, f) != 2) {
336 ast_log(LOG_WARNING, "Unable to write header\n");
339 /* 38: Subchunk 1: Extra format bytes */
340 if (fwrite(&extra_format, 1, 2, f) != 2) {
341 ast_log(LOG_WARNING, "Unable to write header\n");
344 /* 40: Subchunk 2: ID */
345 if (fwrite("fact", 1, 4, f) != 4) {
346 ast_log(LOG_WARNING, "Unable to write header\n");
349 /* 44: Subchunk 2: Size (minus 8) */
350 if (fwrite(&factsize, 1, 4, f) != 4) {
351 ast_log(LOG_WARNING, "Unable to write header\n");
354 /* 48: Subchunk 2: Number of samples */
355 if (fwrite(&num_samples, 1, 4, f) != 4) {
356 ast_log(LOG_WARNING, "Unable to write header\n");
359 /* 52: Subchunk 3: ID */
360 if (fwrite("data", 1, 4, f) != 4) {
361 ast_log(LOG_WARNING, "Unable to write header\n");
364 /* 56: Subchunk 3: Size */
365 if (fwrite(&size, 1, 4, f) != 4) {
366 ast_log(LOG_WARNING, "Unable to write header\n");
372 static int wav_open(struct ast_filestream *s)
374 /* We don't have any header to read or anything really, but
375 if we did, it would go here. We also might want to check
376 and be sure it's a valid file. */
377 struct wavg_desc *fs = (struct wavg_desc *)s->_private;
379 if (check_header(s->f))
381 fs->secondhalf = 0; /* not strictly necessary */
385 static int wav_rewrite(struct ast_filestream *s, const char *comment)
387 /* We don't have any header to read or anything really, but
388 if we did, it would go here. We also might want to check
389 and be sure it's a valid file. */
391 if (write_header(s->f))
396 static void wav_close(struct ast_filestream *s)
398 if (s->mode == O_RDONLY) {
407 static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
409 /* Send a frame from the file to the appropriate channel */
410 struct wavg_desc *fs = (struct wavg_desc *)s->_private;
412 s->fr.frametype = AST_FRAME_VOICE;
413 ast_format_set(&s->fr.subclass.format, AST_FORMAT_GSM, 0);
414 s->fr.offset = AST_FRIENDLY_OFFSET;
415 s->fr.samples = GSM_SAMPLES;
417 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE);
418 if (fs->secondhalf) {
419 /* Just return a frame based on the second GSM frame */
420 s->fr.data.ptr = (char *)s->fr.data.ptr + GSM_FRAME_SIZE;
421 s->fr.offset += GSM_FRAME_SIZE;
423 /* read and convert */
424 unsigned char msdata[MSGSM_FRAME_SIZE];
427 if ((res = fread(msdata, 1, MSGSM_FRAME_SIZE, s->f)) != MSGSM_FRAME_SIZE) {
428 if (res && (res != 1))
429 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
432 /* Convert from MS format to two real GSM frames */
433 conv65(msdata, s->fr.data.ptr);
435 fs->secondhalf = !fs->secondhalf;
436 *whennext = GSM_SAMPLES;
440 static int wav_write(struct ast_filestream *s, struct ast_frame *f)
444 struct wavg_desc *fs = (struct wavg_desc *)s->_private;
446 if (f->frametype != AST_FRAME_VOICE) {
447 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
450 if (f->subclass.format.id != AST_FORMAT_GSM) {
451 ast_log(LOG_WARNING, "Asked to write non-GSM frame (%s)!\n", ast_getformatname(&f->subclass.format));
454 /* XXX this might fail... if the input is a multiple of MSGSM_FRAME_SIZE
455 * we assume it is already in the correct format.
457 if (!(f->datalen % MSGSM_FRAME_SIZE)) {
458 size = MSGSM_FRAME_SIZE;
461 size = GSM_FRAME_SIZE;
463 for (len = 0; len < f->datalen ; len += size) {
465 unsigned char *src, msdata[MSGSM_FRAME_SIZE];
466 if (fs->secondhalf) { /* second half of raw gsm to be converted */
467 memcpy(s->buf + GSM_FRAME_SIZE, f->data.ptr + len, GSM_FRAME_SIZE);
468 conv66((unsigned char *) s->buf, msdata);
471 } else if (size == GSM_FRAME_SIZE) { /* first half of raw gsm */
472 memcpy(s->buf, f->data.ptr + len, GSM_FRAME_SIZE);
473 src = NULL; /* nothing to write */
475 } else { /* raw msgsm data */
476 src = f->data.ptr + len;
478 if (src && (res = fwrite(src, 1, MSGSM_FRAME_SIZE, s->f)) != MSGSM_FRAME_SIZE) {
479 ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
486 static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
488 off_t offset = 0, min = MSGSM_DATA_OFFSET, distance, max, cur;
489 struct wavg_desc *s = (struct wavg_desc *)fs->_private;
491 if ((cur = ftello(fs->f)) < 0) {
492 ast_log(AST_LOG_WARNING, "Unable to determine current position in WAV filestream %p: %s\n", fs, strerror(errno));
496 if (fseeko(fs->f, 0, SEEK_END) < 0) {
497 ast_log(AST_LOG_WARNING, "Unable to seek to end of WAV filestream %p: %s\n", fs, strerror(errno));
501 /* XXX ideally, should round correctly */
502 if ((max = ftello(fs->f)) < 0) {
503 ast_log(AST_LOG_WARNING, "Unable to determine max position in WAV filestream %p: %s\n", fs, strerror(errno));
507 /* Compute the distance in bytes, rounded to the block size */
508 distance = (sample_offset/MSGSM_SAMPLES) * MSGSM_FRAME_SIZE;
509 if (whence == SEEK_SET)
510 offset = distance + min;
511 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
512 offset = distance + cur;
513 else if (whence == SEEK_END)
514 offset = max - distance;
515 /* always protect against seeking past end of header */
518 if (whence != SEEK_FORCECUR) {
521 } else if (offset > max) {
523 fseek(fs->f, 0, SEEK_END);
524 for (i=0; i< (offset - max) / MSGSM_FRAME_SIZE; i++) {
525 if (!fwrite(msgsm_silence, 1, MSGSM_FRAME_SIZE, fs->f)) {
526 ast_log(LOG_WARNING, "fwrite() failed: %s\n", strerror(errno));
531 return fseeko(fs->f, offset, SEEK_SET);
534 static int wav_trunc(struct ast_filestream *fs)
539 if ((fd = fileno(fs->f)) < 0) {
540 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for WAV filestream %p: %s\n", fs, strerror(errno));
543 if ((cur = ftello(fs->f)) < 0) {
544 ast_log(AST_LOG_WARNING, "Unable to determine current position in WAV filestream %p: %s\n", fs, strerror(errno));
547 /* Truncate file to current length */
548 if (ftruncate(fd, cur)) {
551 return update_header(fs->f);
554 static off_t wav_tell(struct ast_filestream *fs)
557 offset = ftello(fs->f);
558 /* since this will most likely be used later in play or record, lets stick
559 * to that level of resolution, just even frames boundaries */
560 return (offset - MSGSM_DATA_OFFSET)/MSGSM_FRAME_SIZE*MSGSM_SAMPLES;
563 static struct ast_format_def wav49_f = {
567 .rewrite = wav_rewrite,
574 .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET,
575 .desc_size = sizeof(struct wavg_desc),
578 static int load_module(void)
580 ast_format_set(&wav49_f.format, AST_FORMAT_GSM, 0);
581 if (ast_format_def_register(&wav49_f))
582 return AST_MODULE_LOAD_FAILURE;
583 return AST_MODULE_LOAD_SUCCESS;
586 static int unload_module(void)
588 return ast_format_def_unregister(wav49_f.name);
591 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Microsoft WAV format (Proprietary GSM)",
593 .unload = unload_module,
594 .load_pri = AST_MODPRI_APP_DEPEND