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.
32 ASTERISK_FILE_VERSION(__FILE__
, "$Revision$")
35 #include <netinet/in.h>
36 #include <arpa/inet.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"
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 */
73 /* Believe it or not, we must decode/recode to account for the
75 int secondhalf
; /* Are we on the second half */
78 #if __BYTE_ORDER == __LITTLE_ENDIAN
84 #if __BYTE_ORDER == __BIG_ENDIAN
86 (((((b) ) & 0xFF) << 24) | \
87 ((((b) >> 8) & 0xFF) << 16) | \
88 ((((b) >> 16) & 0xFF) << 8) | \
89 ((((b) >> 24) & 0xFF) ))
91 (((((b) ) & 0xFF) << 8) | \
92 ((((b) >> 8) & 0xFF) ))
93 #define ltohl(b) htoll(b)
94 #define ltohs(b) htols(b)
96 #error "Endianess not defined"
101 static int check_header(FILE *f
)
103 int type
, size
, formtype
;
104 int fmt
, hsize
, fact
;
108 if (fread(&type
, 1, 4, f
) != 4) {
109 ast_log(LOG_WARNING
, "Read failed (type)\n");
112 if (fread(&size
, 1, 4, f
) != 4) {
113 ast_log(LOG_WARNING
, "Read failed (size)\n");
117 if (fread(&formtype
, 1, 4, f
) != 4) {
118 ast_log(LOG_WARNING
, "Read failed (formtype)\n");
121 if (memcmp(&type
, "RIFF", 4)) {
122 ast_log(LOG_WARNING
, "Does not begin with RIFF\n");
125 if (memcmp(&formtype
, "WAVE", 4)) {
126 ast_log(LOG_WARNING
, "Does not contain WAVE\n");
129 if (fread(&fmt
, 1, 4, f
) != 4) {
130 ast_log(LOG_WARNING
, "Read failed (fmt)\n");
133 if (memcmp(&fmt
, "fmt ", 4)) {
134 ast_log(LOG_WARNING
, "Does not say fmt\n");
137 if (fread(&hsize
, 1, 4, f
) != 4) {
138 ast_log(LOG_WARNING
, "Read failed (formtype)\n");
141 if (ltohl(hsize
) != 20) {
142 ast_log(LOG_WARNING
, "Unexpected header size %d\n", ltohl(hsize
));
145 if (fread(&format
, 1, 2, f
) != 2) {
146 ast_log(LOG_WARNING
, "Read failed (format)\n");
149 if (ltohs(format
) != 49) {
150 ast_log(LOG_WARNING
, "Not a GSM file %d\n", ltohs(format
));
153 if (fread(&chans
, 1, 2, f
) != 2) {
154 ast_log(LOG_WARNING
, "Read failed (format)\n");
157 if (ltohs(chans
) != 1) {
158 ast_log(LOG_WARNING
, "Not in mono %d\n", ltohs(chans
));
161 if (fread(&freq
, 1, 4, f
) != 4) {
162 ast_log(LOG_WARNING
, "Read failed (freq)\n");
165 if (ltohl(freq
) != DEFAULT_SAMPLE_RATE
) {
166 ast_log(LOG_WARNING
, "Unexpected freqency %d\n", ltohl(freq
));
169 /* Ignore the byte frequency */
170 if (fread(&freq
, 1, 4, f
) != 4) {
171 ast_log(LOG_WARNING
, "Read failed (X_1)\n");
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");
179 /* Ignore the byte frequency */
180 if (fread(&freq
, 1, 4, f
) != 4) {
181 ast_log(LOG_WARNING
, "Read failed (Y_1)\n");
184 /* Check for the word fact */
185 if (fread(&fact
, 1, 4, f
) != 4) {
186 ast_log(LOG_WARNING
, "Read failed (fact)\n");
189 if (memcmp(&fact
, "fact", 4)) {
190 ast_log(LOG_WARNING
, "Does not say fact\n");
193 /* Ignore the "fact value" */
194 if (fread(&fact
, 1, 4, f
) != 4) {
195 ast_log(LOG_WARNING
, "Read failed (fact header)\n");
198 if (fread(&fact
, 1, 4, f
) != 4) {
199 ast_log(LOG_WARNING
, "Read failed (fact value)\n");
202 /* Check for the word data */
203 if (fread(&data
, 1, 4, f
) != 4) {
204 ast_log(LOG_WARNING
, "Read failed (data)\n");
207 if (memcmp(&data
, "data", 4)) {
208 ast_log(LOG_WARNING
, "Does not say data\n");
211 /* Ignore the data length */
212 if (fread(&data
, 1, 4, f
) != 4) {
213 ast_log(LOG_WARNING
, "Read failed (data)\n");
219 static int update_header(FILE *f
)
222 int datalen
, filelen
, samples
;
225 fseek(f
, 0, SEEK_END
);
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
+ 1) & ~0x1);
231 filelen
= htoll(MSGSM_DATA_OFFSET
- 8 + ((bytes
+ 1) & ~0x1));
233 ast_log(LOG_WARNING
, "Unable to find our position\n");
236 if (fseek(f
, 4, SEEK_SET
)) {
237 ast_log(LOG_WARNING
, "Unable to set our position\n");
240 if (fwrite(&filelen
, 1, 4, f
) != 4) {
241 ast_log(LOG_WARNING
, "Unable to write file size\n");
244 if (fseek(f
, 48, SEEK_SET
)) {
245 ast_log(LOG_WARNING
, "Unable to set our position\n");
248 if (fwrite(&samples
, 1, 4, f
) != 4) {
249 ast_log(LOG_WARNING
, "Unable to write samples\n");
252 if (fseek(f
, 56, SEEK_SET
)) {
253 ast_log(LOG_WARNING
, "Unable to set our position\n");
256 if (fwrite(&datalen
, 1, 4, f
) != 4) {
257 ast_log(LOG_WARNING
, "Unable to write datalen\n");
260 if (fseeko(f
, cur
, SEEK_SET
)) {
261 ast_log(LOG_WARNING
, "Unable to return to position\n");
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);
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 */
294 if (fwrite("RIFF", 1, 4, f
) != 4) {
295 ast_log(LOG_WARNING
, "Unable to write header\n");
299 if (fwrite(&size
, 1, 4, f
) != 4) {
300 ast_log(LOG_WARNING
, "Unable to write header\n");
303 /* 8: Chunk Format */
304 if (fwrite("WAVE", 1, 4, f
) != 4) {
305 ast_log(LOG_WARNING
, "Unable to write header\n");
308 /* 12: Subchunk 1: ID */
309 if (fwrite("fmt ", 1, 4, f
) != 4) {
310 ast_log(LOG_WARNING
, "Unable to write header\n");
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");
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");
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");
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");
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");
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");
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");
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");
353 /* 40: Subchunk 2: ID */
354 if (fwrite("fact", 1, 4, f
) != 4) {
355 ast_log(LOG_WARNING
, "Unable to write header\n");
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");
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");
368 /* 52: Subchunk 3: ID */
369 if (fwrite("data", 1, 4, f
) != 4) {
370 ast_log(LOG_WARNING
, "Unable to write header\n");
373 /* 56: Subchunk 3: Size */
374 if (fwrite(&size
, 1, 4, f
) != 4) {
375 ast_log(LOG_WARNING
, "Unable to write header\n");
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
))
390 fs
->secondhalf
= 0; /* not strictly necessary */
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
))
405 static void wav_close(struct ast_filestream
*s
)
408 /* Pad to even length */
409 fseek(s
->f
, 0, SEEK_END
);
410 if (ftello(s
->f
) & 0x1)
411 fwrite(&zero
, 1, 1, s
->f
);
414 static struct ast_frame
*wav_read(struct ast_filestream
*s
, int *whennext
)
416 /* Send a frame from the file to the appropriate channel */
417 struct wavg_desc
*fs
= (struct wavg_desc
*)s
->_private
;
419 s
->fr
.frametype
= AST_FRAME_VOICE
;
420 s
->fr
.subclass
= AST_FORMAT_GSM
;
421 s
->fr
.offset
= AST_FRIENDLY_OFFSET
;
422 s
->fr
.samples
= GSM_SAMPLES
;
424 AST_FRAME_SET_BUFFER(&s
->fr
, s
->buf
, AST_FRIENDLY_OFFSET
, GSM_FRAME_SIZE
);
425 if (fs
->secondhalf
) {
426 /* Just return a frame based on the second GSM frame */
427 s
->fr
.data
= (char *)s
->fr
.data
+ GSM_FRAME_SIZE
;
428 s
->fr
.offset
+= GSM_FRAME_SIZE
;
430 /* read and convert */
431 unsigned char msdata
[MSGSM_FRAME_SIZE
];
434 if ((res
= fread(msdata
, 1, MSGSM_FRAME_SIZE
, s
->f
)) != MSGSM_FRAME_SIZE
) {
435 if (res
&& (res
!= 1))
436 ast_log(LOG_WARNING
, "Short read (%d) (%s)!\n", res
, strerror(errno
));
439 /* Convert from MS format to two real GSM frames */
440 conv65(msdata
, s
->fr
.data
);
442 fs
->secondhalf
= !fs
->secondhalf
;
443 *whennext
= GSM_SAMPLES
;
447 static int wav_write(struct ast_filestream
*s
, struct ast_frame
*f
)
451 struct wavg_desc
*fs
= (struct wavg_desc
*)s
->_private
;
453 if (f
->frametype
!= AST_FRAME_VOICE
) {
454 ast_log(LOG_WARNING
, "Asked to write non-voice frame!\n");
457 if (f
->subclass
!= AST_FORMAT_GSM
) {
458 ast_log(LOG_WARNING
, "Asked to write non-GSM frame (%d)!\n", f
->subclass
);
461 /* XXX this might fail... if the input is a multiple of MSGSM_FRAME_SIZE
462 * we assume it is already in the correct format.
464 if (!(f
->datalen
% MSGSM_FRAME_SIZE
)) {
465 size
= MSGSM_FRAME_SIZE
;
468 size
= GSM_FRAME_SIZE
;
470 for (len
= 0; len
< f
->datalen
; len
+= size
) {
472 unsigned char *src
, msdata
[MSGSM_FRAME_SIZE
];
473 if (fs
->secondhalf
) { /* second half of raw gsm to be converted */
474 memcpy(s
->buf
+ GSM_FRAME_SIZE
, f
->data
+ len
, GSM_FRAME_SIZE
);
475 conv66((unsigned char *) s
->buf
, msdata
);
478 } else if (size
== GSM_FRAME_SIZE
) { /* first half of raw gsm */
479 memcpy(s
->buf
, f
->data
+ len
, GSM_FRAME_SIZE
);
480 src
= NULL
; /* nothing to write */
482 } else { /* raw msgsm data */
485 if (src
&& (res
= fwrite(src
, 1, MSGSM_FRAME_SIZE
, s
->f
)) != MSGSM_FRAME_SIZE
) {
486 ast_log(LOG_WARNING
, "Bad write (%d/65): %s\n", res
, strerror(errno
));
489 update_header(s
->f
); /* XXX inefficient! */
494 static int wav_seek(struct ast_filestream
*fs
, off_t sample_offset
, int whence
)
496 off_t offset
=0, distance
, max
;
497 struct wavg_desc
*s
= (struct wavg_desc
*)fs
->_private
;
499 off_t min
= MSGSM_DATA_OFFSET
;
500 off_t cur
= ftello(fs
->f
);
501 fseek(fs
->f
, 0, SEEK_END
);
502 max
= ftello(fs
->f
); /* XXX ideally, should round correctly */
503 /* Compute the distance in bytes, rounded to the block size */
504 distance
= (sample_offset
/MSGSM_SAMPLES
) * MSGSM_FRAME_SIZE
;
505 if (whence
== SEEK_SET
)
506 offset
= distance
+ min
;
507 else if (whence
== SEEK_CUR
|| whence
== SEEK_FORCECUR
)
508 offset
= distance
+ cur
;
509 else if (whence
== SEEK_END
)
510 offset
= max
- distance
;
511 /* always protect against seeking past end of header */
514 if (whence
!= SEEK_FORCECUR
) {
517 } else if (offset
> max
) {
519 fseek(fs
->f
, 0, SEEK_END
);
520 for (i
=0; i
< (offset
- max
) / MSGSM_FRAME_SIZE
; i
++) {
521 fwrite(msgsm_silence
, 1, MSGSM_FRAME_SIZE
, fs
->f
);
525 return fseeko(fs
->f
, offset
, SEEK_SET
);
528 static int wav_trunc(struct ast_filestream
*fs
)
530 if (ftruncate(fileno(fs
->f
), ftello(fs
->f
)))
532 return update_header(fs
->f
);
535 static off_t
wav_tell(struct ast_filestream
*fs
)
538 offset
= ftello(fs
->f
);
539 /* since this will most likely be used later in play or record, lets stick
540 * to that level of resolution, just even frames boundaries */
541 return (offset
- MSGSM_DATA_OFFSET
)/MSGSM_FRAME_SIZE
*MSGSM_SAMPLES
;
544 static const struct ast_format wav49_f
= {
547 .format
= AST_FORMAT_GSM
,
549 .rewrite
= wav_rewrite
,
556 .buf_size
= 2*GSM_FRAME_SIZE
+ AST_FRIENDLY_OFFSET
,
557 .desc_size
= sizeof(struct wavg_desc
),
560 static int load_module(void)
562 return ast_format_register(&wav49_f
);
565 static int unload_module(void)
567 return ast_format_unregister(wav49_f
.name
);
570 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY
, "Microsoft WAV format (Proprietary GSM)");