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
);
231 filelen
= htoll(MSGSM_DATA_OFFSET
- 8 + bytes
);
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 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
;
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
;
421 /* read and convert */
422 unsigned char msdata
[MSGSM_FRAME_SIZE
];
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
));
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
;
438 static int wav_write(struct ast_filestream
*s
, struct ast_frame
*f
)
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");
448 if (f
->subclass
!= AST_FORMAT_GSM
) {
449 ast_log(LOG_WARNING
, "Asked to write non-GSM frame (%d)!\n", f
->subclass
);
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
;
459 size
= GSM_FRAME_SIZE
;
461 for (len
= 0; len
< f
->datalen
; len
+= size
) {
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
);
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 */
473 } else { /* raw msgsm data */
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
));
480 update_header(s
->f
); /* XXX inefficient! */
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 */
505 if (whence
!= SEEK_FORCECUR
) {
508 } else if (offset
> max
) {
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
);
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
)))
523 return update_header(fs
->f
);
526 static off_t
wav_tell(struct ast_filestream
*fs
)
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
= {
538 .format
= AST_FORMAT_GSM
,
540 .rewrite
= wav_rewrite
,
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)");