2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2006, 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 Flat, binary, ulaw PCM file format.
22 * \arg File name extension: alaw, al, alw, pcm, ulaw, ul, mu, ulw, g722, au
28 <support_level>core</support_level>
33 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
35 #include "asterisk/mod_format.h"
36 #include "asterisk/module.h"
37 #include "asterisk/endian.h"
38 #include "asterisk/ulaw.h"
39 #include "asterisk/alaw.h"
41 #define BUF_SIZE 160 /* 160 bytes, and same number of samples */
43 static char ulaw_silence[BUF_SIZE];
44 static char alaw_silence[BUF_SIZE];
46 /* #define REALTIME_WRITE */ /* XXX does it work at all ? */
50 unsigned long start_time;
53 /* Returns time in msec since system boot. */
54 static unsigned long get_time(void)
61 ast_log( LOG_WARNING, "Cannot get current time\n" );
64 return cur * 1000 / sysconf( _SC_CLK_TCK );
67 static int pcma_open(struct ast_filestream *s)
69 if (s->fmt->format == AST_FORMAT_ALAW)
70 pd->starttime = get_time();
74 static int pcma_rewrite(struct ast_filestream *s, const char *comment)
80 static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext)
84 /* Send a frame from the file to the appropriate channel */
86 s->fr.frametype = AST_FRAME_VOICE;
87 ast_format_copy(&s->fr.subclass.format, &s->fmt->format);
89 AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE);
90 if ((res = fread(s->fr.data.ptr, 1, s->fr.datalen, s->f)) < 1) {
92 ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno));
96 if (s->fmt->format.id == AST_FORMAT_G722)
97 *whennext = s->fr.samples = res * 2;
99 *whennext = s->fr.samples = res;
103 static int pcm_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
105 off_t cur, max, offset = 0;
106 int ret = -1; /* assume error */
108 if ((cur = ftello(fs->f)) < 0) {
109 ast_log(AST_LOG_WARNING, "Unable to determine current position in pcm filestream %p: %s\n", fs, strerror(errno));
113 if (fseeko(fs->f, 0, SEEK_END) < 0) {
114 ast_log(AST_LOG_WARNING, "Unable to seek to end of pcm filestream %p: %s\n", fs, strerror(errno));
118 if ((max = ftello(fs->f) < 0)) {
119 ast_log(AST_LOG_WARNING, "Unable to determine max position in pcm filestream %p: %s\n", fs, strerror(errno));
125 offset = sample_offset;
128 offset = max - sample_offset;
132 offset = cur + sample_offset;
135 ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence);
136 offset = sample_offset;
139 ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", (long) offset);
142 if (whence == SEEK_FORCECUR && offset > max) { /* extend the file */
143 size_t left = offset - max;
144 const char *src = (fs->fmt->format.id == AST_FORMAT_ALAW) ? alaw_silence : ulaw_silence;
147 size_t written = fwrite(src, 1, (left > BUF_SIZE) ? BUF_SIZE : left, fs->f);
152 ret = 0; /* successful */
155 ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", (long) offset, (long) max);
158 ret = fseeko(fs->f, offset, SEEK_SET);
163 static int pcm_trunc(struct ast_filestream *fs)
167 if ((fd = fileno(fs->f)) < 0) {
168 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for pcm filestream %p: %s\n", fs, strerror(errno));
171 if ((cur = ftello(fs->f) < 0)) {
172 ast_log(AST_LOG_WARNING, "Unable to determine current position in pcm filestream %p: %s\n", fs, strerror(errno));
175 /* Truncate file to current length */
176 return ftruncate(fd, cur);
179 static off_t pcm_tell(struct ast_filestream *fs)
181 return ftello(fs->f);
184 static int pcm_write(struct ast_filestream *fs, struct ast_frame *f)
188 if (f->frametype != AST_FRAME_VOICE) {
189 ast_log(LOG_WARNING, "Asked to write non-voice frame!\n");
192 if (ast_format_cmp(&f->subclass.format, &fs->fmt->format) == AST_FORMAT_CMP_NOT_EQUAL) {
193 ast_log(LOG_WARNING, "Asked to write incompatible format frame (%s)!\n", ast_getformatname(&f->subclass.format));
197 #ifdef REALTIME_WRITE
198 if (s->fmt->format == AST_FORMAT_ALAW) {
199 struct pcm_desc *pd = (struct pcm_desc *)fs->_private;
200 struct stat stat_buf;
201 unsigned long cur_time = get_time();
202 unsigned long fpos = ( cur_time - pd->start_time ) * 8; /* 8 bytes per msec */
203 /* Check if we have written to this position yet. If we have, then increment pos by one frame
204 * for some degree of protection against receiving packets in the same clock tick.
207 fstat(fileno(fs->f), &stat_buf );
208 if (stat_buf.st_size > fpos )
209 fpos += f->datalen; /* Incrementing with the size of this current frame */
211 if (stat_buf.st_size < fpos) {
212 /* fill the gap with 0x55 rather than 0. */
214 unsigned long cur, to_write;
216 cur = stat_buf.st_size;
217 if (fseek(fs->f, cur, SEEK_SET) < 0) {
218 ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
221 memset(buf, 0x55, 512);
223 to_write = fpos - cur;
224 if (to_write > sizeof(buf))
225 to_write = sizeof(buf);
226 fwrite(buf, 1, to_write, fs->f);
231 if (fseek(s->f, fpos, SEEK_SET) < 0) {
232 ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) );
236 #endif /* REALTIME_WRITE */
238 if ((res = fwrite(f->data.ptr, 1, f->datalen, fs->f)) != f->datalen) {
239 ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno));
245 /* SUN .au support routines */
247 #define AU_HEADER_SIZE 24
248 #define AU_HEADER(var) uint32_t var[6]
250 #define AU_HDR_MAGIC_OFF 0
251 #define AU_HDR_HDR_SIZE_OFF 1
252 #define AU_HDR_DATA_SIZE_OFF 2
253 #define AU_HDR_ENCODING_OFF 3
254 #define AU_HDR_SAMPLE_RATE_OFF 4
255 #define AU_HDR_CHANNELS_OFF 5
257 #define AU_ENC_8BIT_ULAW 1
259 #define AU_MAGIC 0x2e736e64
260 #if __BYTE_ORDER == __BIG_ENDIAN
266 #if __BYTE_ORDER == __LITTLE_ENDIAN
268 (((((b) ) & 0xFF) << 24) | \
269 ((((b) >> 8) & 0xFF) << 16) | \
270 ((((b) >> 16) & 0xFF) << 8) | \
271 ((((b) >> 24) & 0xFF) ))
273 (((((b) ) & 0xFF) << 8) | \
274 ((((b) >> 8) & 0xFF) ))
275 #define ltohl(b) htoll(b)
276 #define ltohs(b) htols(b)
278 #error "Endianess not defined"
282 static int check_header(FILE *f)
289 uint32_t sample_rate;
292 if (fread(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
293 ast_log(LOG_WARNING, "Read failed (header)\n");
296 magic = ltohl(header[AU_HDR_MAGIC_OFF]);
297 if (magic != (uint32_t) AU_MAGIC) {
298 ast_log(LOG_WARNING, "Bad magic: 0x%x\n", magic);
300 hdr_size = ltohl(header[AU_HDR_HDR_SIZE_OFF]);
301 if (hdr_size < AU_HEADER_SIZE) {
302 hdr_size = AU_HEADER_SIZE;
304 /* data_size = ltohl(header[AU_HDR_DATA_SIZE_OFF]); */
305 encoding = ltohl(header[AU_HDR_ENCODING_OFF]);
306 if (encoding != AU_ENC_8BIT_ULAW) {
307 ast_log(LOG_WARNING, "Unexpected format: %d. Only 8bit ULAW allowed (%d)\n", encoding, AU_ENC_8BIT_ULAW);
310 sample_rate = ltohl(header[AU_HDR_SAMPLE_RATE_OFF]);
311 if (sample_rate != DEFAULT_SAMPLE_RATE) {
312 ast_log(LOG_WARNING, "Sample rate can only be 8000 not %d\n", sample_rate);
315 channels = ltohl(header[AU_HDR_CHANNELS_OFF]);
317 ast_log(LOG_WARNING, "Not in mono: channels=%d\n", channels);
321 fseek(f, 0, SEEK_END);
322 data_size = ftell(f) - hdr_size;
323 if (fseek(f, hdr_size, SEEK_SET) == -1 ) {
324 ast_log(LOG_WARNING, "Failed to skip to data: %d\n", hdr_size);
330 static int update_header(FILE *f)
337 fseek(f, 0, SEEK_END);
339 /* data starts 24 bytes in */
340 bytes = end - AU_HEADER_SIZE;
341 datalen = htoll(bytes);
344 ast_log(LOG_WARNING, "Unable to find our position\n");
347 if (fseek(f, AU_HDR_DATA_SIZE_OFF * sizeof(uint32_t), SEEK_SET)) {
348 ast_log(LOG_WARNING, "Unable to set our position\n");
351 if (fwrite(&datalen, 1, sizeof(datalen), f) != sizeof(datalen)) {
352 ast_log(LOG_WARNING, "Unable to set write file size\n");
355 if (fseek(f, cur, SEEK_SET)) {
356 ast_log(LOG_WARNING, "Unable to return to position\n");
362 static int write_header(FILE *f)
366 header[AU_HDR_MAGIC_OFF] = htoll((uint32_t) AU_MAGIC);
367 header[AU_HDR_HDR_SIZE_OFF] = htoll(AU_HEADER_SIZE);
368 header[AU_HDR_DATA_SIZE_OFF] = 0;
369 header[AU_HDR_ENCODING_OFF] = htoll(AU_ENC_8BIT_ULAW);
370 header[AU_HDR_SAMPLE_RATE_OFF] = htoll(DEFAULT_SAMPLE_RATE);
371 header[AU_HDR_CHANNELS_OFF] = htoll(1);
373 /* Write an au header, ignoring sizes which will be filled in later */
374 fseek(f, 0, SEEK_SET);
375 if (fwrite(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) {
376 ast_log(LOG_WARNING, "Unable to write header\n");
382 static int au_open(struct ast_filestream *s)
384 if (check_header(s->f) < 0)
389 static int au_rewrite(struct ast_filestream *s, const char *comment)
391 if (write_header(s->f))
396 /* XXX check this, probably incorrect */
397 static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence)
399 off_t min = AU_HEADER_SIZE, max, cur;
400 long offset = 0, bytes;
402 if (fs->fmt->format.id == AST_FORMAT_G722)
403 bytes = sample_offset / 2;
405 bytes = sample_offset;
407 if ((cur = ftello(fs->f)) < 0) {
408 ast_log(AST_LOG_WARNING, "Unable to determine current position in au filestream %p: %s\n", fs, strerror(errno));
412 if (fseeko(fs->f, 0, SEEK_END) < 0) {
413 ast_log(AST_LOG_WARNING, "Unable to seek to end of au filestream %p: %s\n", fs, strerror(errno));
417 if ((max = ftello(fs->f) < 0)) {
418 ast_log(AST_LOG_WARNING, "Unable to determine max position in au filestream %p: %s\n", fs, strerror(errno));
422 if (whence == SEEK_SET)
423 offset = bytes + min;
424 else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
425 offset = bytes + cur;
426 else if (whence == SEEK_END)
427 offset = max - bytes;
429 if (whence != SEEK_FORCECUR) {
430 offset = (offset > max) ? max : offset;
433 /* always protect the header space. */
434 offset = (offset < min) ? min : offset;
436 return fseeko(fs->f, offset, SEEK_SET);
439 static int au_trunc(struct ast_filestream *fs)
444 if ((fd = fileno(fs->f)) < 0) {
445 ast_log(AST_LOG_WARNING, "Unable to determine file descriptor for au filestream %p: %s\n", fs, strerror(errno));
448 if ((cur = ftello(fs->f) < 0)) {
449 ast_log(AST_LOG_WARNING, "Unable to determine current position in au filestream %p: %s\n", fs, strerror(errno));
452 /* Truncate file to current length */
453 if (ftruncate(fd, cur)) {
456 return update_header(fs->f);
459 static off_t au_tell(struct ast_filestream *fs)
461 off_t offset = ftello(fs->f);
462 return offset - AU_HEADER_SIZE;
465 static struct ast_format_def alaw_f = {
467 .exts = "alaw|al|alw",
473 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
474 #ifdef REALTIME_WRITE
476 .rewrite = pcma_rewrite,
477 .desc_size = sizeof(struct pcm_desc),
481 static struct ast_format_def pcm_f = {
483 .exts = "pcm|ulaw|ul|mu|ulw",
489 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET,
492 static struct ast_format_def g722_f = {
500 .buf_size = (BUF_SIZE * 2) + AST_FRIENDLY_OFFSET,
503 static struct ast_format_def au_f = {
507 .rewrite = au_rewrite,
513 .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, /* this many shorts */
516 static int load_module(void)
520 /* XXX better init ? */
521 for (i = 0; i < ARRAY_LEN(ulaw_silence); i++)
522 ulaw_silence[i] = AST_LIN2MU(0);
523 for (i = 0; i < ARRAY_LEN(alaw_silence); i++)
524 alaw_silence[i] = AST_LIN2A(0);
526 ast_format_set(&pcm_f.format, AST_FORMAT_ULAW, 0);
527 ast_format_set(&alaw_f.format, AST_FORMAT_ALAW, 0);
528 ast_format_set(&au_f.format, AST_FORMAT_ULAW, 0);
529 ast_format_set(&g722_f.format, AST_FORMAT_G722, 0);
530 if ( ast_format_def_register(&pcm_f)
531 || ast_format_def_register(&alaw_f)
532 || ast_format_def_register(&au_f)
533 || ast_format_def_register(&g722_f) )
534 return AST_MODULE_LOAD_FAILURE;
535 return AST_MODULE_LOAD_SUCCESS;
538 static int unload_module(void)
540 return ast_format_def_unregister(pcm_f.name)
541 || ast_format_def_unregister(alaw_f.name)
542 || ast_format_def_unregister(au_f.name)
543 || ast_format_def_unregister(g722_f.name);
546 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Raw/Sun uLaw/ALaw 8KHz (PCM,PCMA,AU), G.722 16Khz",
548 .unload = unload_module,
549 .load_pri = AST_MODPRI_APP_DEPEND