diff options
author | russell <russell@f38db490-d61c-443f-a65b-d21fe96a405b> | 2008-01-19 00:19:29 +0000 |
---|---|---|
committer | russell <russell@f38db490-d61c-443f-a65b-d21fe96a405b> | 2008-01-19 00:19:29 +0000 |
commit | f8247040e6231c4b3b5099ea3a526348b7941566 (patch) | |
tree | 0cc92ad6ebf6ae49a62f6e7ef8ec819121d63630 /trunk/formats | |
parent | d88e56c61ce2042544c1a8a71c93b69ab2e6ffba (diff) |
Creating tag for the release of asterisk-1.6.0-beta1v1.6.0-beta1
git-svn-id: http://svn.digium.com/svn/asterisk/tags/1.6.0-beta1@99163 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'trunk/formats')
-rw-r--r-- | trunk/formats/Makefile | 20 | ||||
-rw-r--r-- | trunk/formats/format_g723.c | 152 | ||||
-rw-r--r-- | trunk/formats/format_g726.c | 261 | ||||
-rw-r--r-- | trunk/formats/format_g729.c | 148 | ||||
-rw-r--r-- | trunk/formats/format_gsm.c | 170 | ||||
-rw-r--r-- | trunk/formats/format_h263.c | 186 | ||||
-rw-r--r-- | trunk/formats/format_h264.c | 175 | ||||
-rw-r--r-- | trunk/formats/format_ilbc.c | 146 | ||||
-rw-r--r-- | trunk/formats/format_jpeg.c | 115 | ||||
-rw-r--r-- | trunk/formats/format_ogg_vorbis.c | 552 | ||||
-rw-r--r-- | trunk/formats/format_pcm.c | 485 | ||||
-rw-r--r-- | trunk/formats/format_sln.c | 130 | ||||
-rw-r--r-- | trunk/formats/format_sln16.c | 138 | ||||
-rw-r--r-- | trunk/formats/format_vox.c | 135 | ||||
-rw-r--r-- | trunk/formats/format_wav.c | 491 | ||||
-rw-r--r-- | trunk/formats/format_wav_gsm.c | 559 | ||||
-rw-r--r-- | trunk/formats/msgsm.h | 689 |
17 files changed, 4552 insertions, 0 deletions
diff --git a/trunk/formats/Makefile b/trunk/formats/Makefile new file mode 100644 index 000000000..483470103 --- /dev/null +++ b/trunk/formats/Makefile @@ -0,0 +1,20 @@ +# +# Asterisk -- A telephony toolkit for Linux. +# +# Makefile for file format modules +# +# Copyright (C) 1999-2006, Digium, Inc. +# +# This program is free software, distributed under the terms of +# the GNU General Public License +# + +-include $(ASTTOPDIR)/menuselect.makeopts $(ASTTOPDIR)/menuselect.makedeps + +MODULE_PREFIX=format +MENUSELECT_CATEGORY=FORMATS +MENUSELECT_DESCRIPTION=Format Interpreters + +all: _all + +include $(ASTTOPDIR)/Makefile.moddir_rules diff --git a/trunk/formats/format_g723.c b/trunk/formats/format_g723.c new file mode 100644 index 000000000..6e57b4fa8 --- /dev/null +++ b/trunk/formats/format_g723.c @@ -0,0 +1,152 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 1999 - 2005, Digium, Inc. + * + * Mark Spencer <markster@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! + * \file + * + * \brief Old-style G.723.1 frame/timestamp format. + * + * \arg Extensions: g723, g723sf + * \ingroup formats + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" + +#define G723_MAX_SIZE 1024 + +static struct ast_frame *g723_read(struct ast_filestream *s, int *whennext) +{ + unsigned short size; + int res; + int delay; + /* Read the delay for the next packet, and schedule again if necessary */ + /* XXX is this ignored ? */ + if (fread(&delay, 1, 4, s->f) == 4) + delay = ntohl(delay); + else + delay = -1; + if (fread(&size, 1, 2, s->f) != 2) { + /* Out of data, or the file is no longer valid. In any case + go ahead and stop the stream */ + return NULL; + } + /* Looks like we have a frame to read from here */ + size = ntohs(size); + if (size > G723_MAX_SIZE) { + ast_log(LOG_WARNING, "Size %d is invalid\n", size); + /* The file is apparently no longer any good, as we + shouldn't ever get frames even close to this + size. */ + return NULL; + } + /* Read the data into the buffer */ + s->fr.frametype = AST_FRAME_VOICE; + s->fr.subclass = AST_FORMAT_G723_1; + s->fr.mallocd = 0; + AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, size); + if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) != size) { + ast_log(LOG_WARNING, "Short read (%d of %d bytes) (%s)!\n", res, size, strerror(errno)); + return NULL; + } + *whennext = s->fr.samples = 240; + return &s->fr; +} + +static int g723_write(struct ast_filestream *s, struct ast_frame *f) +{ + uint32_t delay; + uint16_t size; + int res; + /* XXX there used to be a check s->fr means a read stream */ + if (f->frametype != AST_FRAME_VOICE) { + ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); + return -1; + } + if (f->subclass != AST_FORMAT_G723_1) { + ast_log(LOG_WARNING, "Asked to write non-g723 frame!\n"); + return -1; + } + delay = 0; + if (f->datalen <= 0) { + ast_log(LOG_WARNING, "Short frame ignored (%d bytes long?)\n", f->datalen); + return 0; + } + if ((res = fwrite(&delay, 1, 4, s->f)) != 4) { + ast_log(LOG_WARNING, "Unable to write delay: res=%d (%s)\n", res, strerror(errno)); + return -1; + } + size = htons(f->datalen); + if ((res = fwrite(&size, 1, 2, s->f)) != 2) { + ast_log(LOG_WARNING, "Unable to write size: res=%d (%s)\n", res, strerror(errno)); + return -1; + } + if ((res = fwrite(f->data, 1, f->datalen, s->f)) != f->datalen) { + ast_log(LOG_WARNING, "Unable to write frame: res=%d (%s)\n", res, strerror(errno)); + return -1; + } + return 0; +} + +static int g723_seek(struct ast_filestream *fs, off_t sample_offset, int whence) +{ + return -1; +} + +static int g723_trunc(struct ast_filestream *fs) +{ + /* Truncate file to current length */ + if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0) + return -1; + return 0; +} + +static off_t g723_tell(struct ast_filestream *fs) +{ + return -1; +} + +static const struct ast_format g723_1_f = { + .name = "g723sf", + .exts = "g723|g723sf", + .format = AST_FORMAT_G723_1, + .write = g723_write, + .seek = g723_seek, + .trunc = g723_trunc, + .tell = g723_tell, + .read = g723_read, + .buf_size = G723_MAX_SIZE + AST_FRIENDLY_OFFSET, +}; + +static int load_module(void) +{ + if (ast_format_register(&g723_1_f)) + return AST_MODULE_LOAD_FAILURE; + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + return ast_format_unregister(g723_1_f.name); +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "G.723.1 Simple Timestamp File Format"); diff --git a/trunk/formats/format_g726.c b/trunk/formats/format_g726.c new file mode 100644 index 000000000..e27476fed --- /dev/null +++ b/trunk/formats/format_g726.c @@ -0,0 +1,261 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (c) 2004 - 2005, inAccess Networks + * + * Michael Manousos <manousos@inaccessnetworks.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*!\file + * + * \brief Headerless G.726 (16/24/32/40kbps) data format for Asterisk. + * + * File name extensions: + * \arg 40 kbps: g726-40 + * \arg 32 kbps: g726-32 + * \arg 24 kbps: g726-24 + * \arg 16 kbps: g726-16 + * \ingroup formats + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" +#include "asterisk/endian.h" + +#define RATE_40 0 +#define RATE_32 1 +#define RATE_24 2 +#define RATE_16 3 + +/* We can only read/write chunks of FRAME_TIME ms G.726 data */ +#define FRAME_TIME 10 /* 10 ms size */ + +#define BUF_SIZE (5*FRAME_TIME) /* max frame size in bytes ? */ +/* Frame sizes in bytes */ +static int frame_size[4] = { + FRAME_TIME * 5, + FRAME_TIME * 4, + FRAME_TIME * 3, + FRAME_TIME * 2 +}; + +struct g726_desc { + int rate; /* RATE_* defines */ +}; + +/* + * Rate dependant format functions (open, rewrite) + */ +static int g726_open(struct ast_filestream *tmp, int rate) +{ + struct g726_desc *s = (struct g726_desc *)tmp->_private; + s->rate = rate; + ast_debug(1, "Created filestream G.726-%dk.\n", 40 - s->rate * 8); + return 0; +} + +static int g726_40_open(struct ast_filestream *s) +{ + return g726_open(s, RATE_40); +} + +static int g726_32_open(struct ast_filestream *s) +{ + return g726_open(s, RATE_32); +} + +static int g726_24_open(struct ast_filestream *s) +{ + return g726_open(s, RATE_24); +} + +static int g726_16_open(struct ast_filestream *s) +{ + return g726_open(s, RATE_16); +} + +static int g726_40_rewrite(struct ast_filestream *s, const char *comment) +{ + return g726_open(s, RATE_40); +} + +static int g726_32_rewrite(struct ast_filestream *s, const char *comment) +{ + return g726_open(s, RATE_32); +} + +static int g726_24_rewrite(struct ast_filestream *s, const char *comment) +{ + return g726_open(s, RATE_24); +} + +static int g726_16_rewrite(struct ast_filestream *s, const char *comment) +{ + return g726_open(s, RATE_16); +} + +/* + * Rate independent format functions (read, write) + */ + +static struct ast_frame *g726_read(struct ast_filestream *s, int *whennext) +{ + int res; + struct g726_desc *fs = (struct g726_desc *)s->_private; + + /* Send a frame from the file to the appropriate channel */ + s->fr.frametype = AST_FRAME_VOICE; + s->fr.subclass = AST_FORMAT_G726; + s->fr.mallocd = 0; + AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, frame_size[fs->rate]); + s->fr.samples = 8 * FRAME_TIME; + if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) != s->fr.datalen) { + if (res) + ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); + return NULL; + } + *whennext = s->fr.samples; + return &s->fr; +} + +static int g726_write(struct ast_filestream *s, struct ast_frame *f) +{ + int res; + struct g726_desc *fs = (struct g726_desc *)s->_private; + + if (f->frametype != AST_FRAME_VOICE) { + ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); + return -1; + } + if (f->subclass != AST_FORMAT_G726) { + ast_log(LOG_WARNING, "Asked to write non-G726 frame (%d)!\n", + f->subclass); + return -1; + } + if (f->datalen % frame_size[fs->rate]) { + ast_log(LOG_WARNING, "Invalid data length %d, should be multiple of %d\n", + f->datalen, frame_size[fs->rate]); + return -1; + } + if ((res = fwrite(f->data, 1, f->datalen, s->f)) != f->datalen) { + ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", + res, frame_size[fs->rate], strerror(errno)); + return -1; + } + return 0; +} + +static int g726_seek(struct ast_filestream *fs, off_t sample_offset, int whence) +{ + return -1; +} + +static int g726_trunc(struct ast_filestream *fs) +{ + return -1; +} + +static off_t g726_tell(struct ast_filestream *fs) +{ + return -1; +} + +static const struct ast_format f[] = { + { + .name = "g726-40", + .exts = "g726-40", + .format = AST_FORMAT_G726, + .open = g726_40_open, + .rewrite = g726_40_rewrite, + .write = g726_write, + .seek = g726_seek, + .trunc = g726_trunc, + .tell = g726_tell, + .read = g726_read, + .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, + .desc_size = sizeof(struct g726_desc), + }, + { + .name = "g726-32", + .exts = "g726-32", + .format = AST_FORMAT_G726, + .open = g726_32_open, + .rewrite = g726_32_rewrite, + .write = g726_write, + .seek = g726_seek, + .trunc = g726_trunc, + .tell = g726_tell, + .read = g726_read, + .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, + .desc_size = sizeof(struct g726_desc), + }, + { + .name = "g726-24", + .exts = "g726-24", + .format = AST_FORMAT_G726, + .open = g726_24_open, + .rewrite = g726_24_rewrite, + .write = g726_write, + .seek = g726_seek, + .trunc = g726_trunc, + .tell = g726_tell, + .read = g726_read, + .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, + .desc_size = sizeof(struct g726_desc), + }, + { + .name = "g726-16", + .exts = "g726-16", + .format = AST_FORMAT_G726, + .open = g726_16_open, + .rewrite = g726_16_rewrite, + .write = g726_write, + .seek = g726_seek, + .trunc = g726_trunc, + .tell = g726_tell, + .read = g726_read, + .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, + .desc_size = sizeof(struct g726_desc), + }, + { .format = 0 } /* terminator */ +}; + +static int load_module(void) +{ + int i; + + for (i = 0; f[i].format ; i++) { + if (ast_format_register(&f[i])) { /* errors are fatal */ + ast_log(LOG_WARNING, "Failed to register format %s.\n", f[i].name); + return AST_MODULE_LOAD_FAILURE; + } + } + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + int i; + + for (i = 0; f[i].format ; i++) { + if (ast_format_unregister(f[i].name)) + ast_log(LOG_WARNING, "Failed to unregister format %s.\n", f[i].name); + } + return(0); +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw G.726 (16/24/32/40kbps) data"); diff --git a/trunk/formats/format_g729.c b/trunk/formats/format_g729.c new file mode 100644 index 000000000..8df463d81 --- /dev/null +++ b/trunk/formats/format_g729.c @@ -0,0 +1,148 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 1999 - 2005, Digium, Inc. + * + * Mark Spencer <markster@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief Save to raw, headerless G729 data. + * \note This is not an encoder/decoder. The codec fo g729 is only + * available with a commercial license from Digium, due to patent + * restrictions. Check http://www.digium.com for information. + * \arg Extensions: g729 + * \ingroup formats + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" +#include "asterisk/endian.h" + +/* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */ + +/* Portions of the conversion code are by guido@sienanet.it */ + +#define BUF_SIZE 20 /* two G729 frames */ +#define G729A_SAMPLES 160 + +static struct ast_frame *g729_read(struct ast_filestream *s, int *whennext) +{ + int res; + /* Send a frame from the file to the appropriate channel */ + s->fr.frametype = AST_FRAME_VOICE; + s->fr.subclass = AST_FORMAT_G729A; + s->fr.mallocd = 0; + s->fr.samples = G729A_SAMPLES; + AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE); + if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) != s->fr.datalen) { + if (res && (res != 10)) /* XXX what for ? */ + ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); + return NULL; + } + *whennext = s->fr.samples; + return &s->fr; +} + +static int g729_write(struct ast_filestream *fs, struct ast_frame *f) +{ + int res; + if (f->frametype != AST_FRAME_VOICE) { + ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); + return -1; + } + if (f->subclass != AST_FORMAT_G729A) { + ast_log(LOG_WARNING, "Asked to write non-G729 frame (%d)!\n", f->subclass); + return -1; + } + if (f->datalen % 10) { + ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 10\n", f->datalen); + return -1; + } + if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) { + ast_log(LOG_WARNING, "Bad write (%d/10): %s\n", res, strerror(errno)); + return -1; + } + return 0; +} + +static int g729_seek(struct ast_filestream *fs, off_t sample_offset, int whence) +{ + long bytes; + off_t min,cur,max,offset=0; + min = 0; + cur = ftello(fs->f); + fseeko(fs->f, 0, SEEK_END); + max = ftello(fs->f); + + bytes = BUF_SIZE * (sample_offset / G729A_SAMPLES); + if (whence == SEEK_SET) + offset = bytes; + else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) + offset = cur + bytes; + else if (whence == SEEK_END) + offset = max - bytes; + if (whence != SEEK_FORCECUR) { + offset = (offset > max)?max:offset; + } + /* protect against seeking beyond begining. */ + offset = (offset < min)?min:offset; + if (fseeko(fs->f, offset, SEEK_SET) < 0) + return -1; + return 0; +} + +static int g729_trunc(struct ast_filestream *fs) +{ + /* Truncate file to current length */ + if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0) + return -1; + return 0; +} + +static off_t g729_tell(struct ast_filestream *fs) +{ + off_t offset = ftello(fs->f); + return (offset/BUF_SIZE)*G729A_SAMPLES; +} + +static const struct ast_format g729_f = { + .name = "g729", + .exts = "g729", + .format = AST_FORMAT_G729A, + .write = g729_write, + .seek = g729_seek, + .trunc = g729_trunc, + .tell = g729_tell, + .read = g729_read, + .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, +}; + +static int load_module(void) +{ + if (ast_format_register(&g729_f)) + return AST_MODULE_LOAD_FAILURE; + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + return ast_format_unregister(g729_f.name); +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw G729 data"); diff --git a/trunk/formats/format_gsm.c b/trunk/formats/format_gsm.c new file mode 100644 index 000000000..d43844e64 --- /dev/null +++ b/trunk/formats/format_gsm.c @@ -0,0 +1,170 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 1999 - 2005, Digium, Inc. + * + * Mark Spencer <markster@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief Save to raw, headerless GSM data. + * \arg File name extension: gsm + * \ingroup formats + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" +#include "asterisk/endian.h" + +#include "msgsm.h" + +/* Some Ideas for this code came from makegsme.c by Jeffrey Chilton */ + +/* Portions of the conversion code are by guido@sienanet.it */ + +#define GSM_FRAME_SIZE 33 +#define GSM_SAMPLES 160 + +/* silent gsm frame */ +/* begin binary data: */ +char gsm_silence[] = /* 33 */ +{0xD8,0x20,0xA2,0xE1,0x5A,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49 +,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24,0x92,0x49,0x24,0x50,0x00,0x49,0x24 +,0x92,0x49,0x24}; +/* end binary data. size = 33 bytes */ + +static struct ast_frame *gsm_read(struct ast_filestream *s, int *whennext) +{ + int res; + + s->fr.frametype = AST_FRAME_VOICE; + s->fr.subclass = AST_FORMAT_GSM; + AST_FRAME_SET_BUFFER(&(s->fr), s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE) + s->fr.mallocd = 0; + if ((res = fread(s->fr.data, 1, GSM_FRAME_SIZE, s->f)) != GSM_FRAME_SIZE) { + if (res) + ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); + return NULL; + } + *whennext = s->fr.samples = GSM_SAMPLES; + return &s->fr; +} + +static int gsm_write(struct ast_filestream *fs, struct ast_frame *f) +{ + int res; + unsigned char gsm[2*GSM_FRAME_SIZE]; + + if (f->frametype != AST_FRAME_VOICE) { + ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); + return -1; + } + if (f->subclass != AST_FORMAT_GSM) { + ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass); + return -1; + } + if (!(f->datalen % 65)) { + /* This is in MSGSM format, need to be converted */ + int len=0; + while(len < f->datalen) { + conv65(f->data + len, gsm); + if ((res = fwrite(gsm, 1, 2*GSM_FRAME_SIZE, fs->f)) != 2*GSM_FRAME_SIZE) { + ast_log(LOG_WARNING, "Bad write (%d/66): %s\n", res, strerror(errno)); + return -1; + } + len += 65; + } + } else { + if (f->datalen % GSM_FRAME_SIZE) { + ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 33\n", f->datalen); + return -1; + } + if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) { + ast_log(LOG_WARNING, "Bad write (%d/33): %s\n", res, strerror(errno)); + return -1; + } + } + return 0; +} + +static int gsm_seek(struct ast_filestream *fs, off_t sample_offset, int whence) +{ + off_t offset=0,min,cur,max,distance; + + min = 0; + cur = ftello(fs->f); + fseeko(fs->f, 0, SEEK_END); + max = ftello(fs->f); + /* have to fudge to frame here, so not fully to sample */ + distance = (sample_offset/GSM_SAMPLES) * GSM_FRAME_SIZE; + if(whence == SEEK_SET) + offset = distance; + else if(whence == SEEK_CUR || whence == SEEK_FORCECUR) + offset = distance + cur; + else if(whence == SEEK_END) + offset = max - distance; + /* Always protect against seeking past the begining. */ + offset = (offset < min)?min:offset; + if (whence != SEEK_FORCECUR) { + offset = (offset > max)?max:offset; + } else if (offset > max) { + int i; + fseeko(fs->f, 0, SEEK_END); + for (i=0; i< (offset - max) / GSM_FRAME_SIZE; i++) { + fwrite(gsm_silence, 1, GSM_FRAME_SIZE, fs->f); + } + } + return fseeko(fs->f, offset, SEEK_SET); +} + +static int gsm_trunc(struct ast_filestream *fs) +{ + return ftruncate(fileno(fs->f), ftello(fs->f)); +} + +static off_t gsm_tell(struct ast_filestream *fs) +{ + off_t offset = ftello(fs->f); + return (offset/GSM_FRAME_SIZE)*GSM_SAMPLES; +} + +static const struct ast_format gsm_f = { + .name = "gsm", + .exts = "gsm", + .format = AST_FORMAT_GSM, + .write = gsm_write, + .seek = gsm_seek, + .trunc = gsm_trunc, + .tell = gsm_tell, + .read = gsm_read, + .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET, /* 2 gsm frames */ +}; + +static int load_module(void) +{ + if (ast_format_register(&gsm_f)) + return AST_MODULE_LOAD_FAILURE; + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + return ast_format_unregister(gsm_f.name); +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw GSM data"); diff --git a/trunk/formats/format_h263.c b/trunk/formats/format_h263.c new file mode 100644 index 000000000..b0b5cb27d --- /dev/null +++ b/trunk/formats/format_h263.c @@ -0,0 +1,186 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 1999 - 2006, Digium, Inc. + * + * Mark Spencer <markster@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief Save to raw, headerless h263 data. + * \arg File name extension: h263 + * \ingroup formats + * \arg See \ref AstVideo + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" +#include "asterisk/endian.h" + +/* Some Ideas for this code came from makeh263e.c by Jeffrey Chilton */ + +/* Portions of the conversion code are by guido@sienanet.it */ + +/* According to: + * http://lists.mpegif.org/pipermail/mp4-tech/2005-July/005741.html + * the maximum actual frame size is not 2048, but 8192. Since the maximum + * theoretical limit is not much larger (32k = 15bits), we'll go for that + * size to ensure we don't corrupt frames sent to us (unless they're + * ridiculously large). */ +#define BUF_SIZE 32768 /* Four real h.263 Frames */ + +struct h263_desc { + unsigned int lastts; +}; + + +static int h263_open(struct ast_filestream *s) +{ + unsigned int ts; + int res; + + if ((res = fread(&ts, 1, sizeof(ts), s->f)) < sizeof(ts)) { + ast_log(LOG_WARNING, "Empty file!\n"); + return -1; + } + return 0; +} + +static struct ast_frame *h263_read(struct ast_filestream *s, int *whennext) +{ + int res; + int mark; + unsigned short len; + unsigned int ts; + struct h263_desc *fs = (struct h263_desc *)s->_private; + + /* Send a frame from the file to the appropriate channel */ + if ((res = fread(&len, 1, sizeof(len), s->f)) < 1) + return NULL; + len = ntohs(len); + mark = (len & 0x8000) ? 1 : 0; + len &= 0x7fff; + if (len > BUF_SIZE) { + ast_log(LOG_WARNING, "Length %d is too long\n", len); + return NULL; + } + s->fr.frametype = AST_FRAME_VIDEO; + s->fr.subclass = AST_FORMAT_H263; + s->fr.mallocd = 0; + AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len); + if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) != s->fr.datalen) { + if (res) + ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); + return NULL; + } + s->fr.samples = fs->lastts; /* XXX what ? */ + s->fr.datalen = len; + s->fr.subclass |= mark; + s->fr.delivery.tv_sec = 0; + s->fr.delivery.tv_usec = 0; + if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) { + fs->lastts = ntohl(ts); + *whennext = fs->lastts * 4/45; + } else + *whennext = 0; + return &s->fr; +} + +static int h263_write(struct ast_filestream *fs, struct ast_frame *f) +{ + int res; + unsigned int ts; + unsigned short len; + int subclass; + int mark=0; + if (f->frametype != AST_FRAME_VIDEO) { + ast_log(LOG_WARNING, "Asked to write non-video frame!\n"); + return -1; + } + subclass = f->subclass; + if (subclass & 0x1) + mark=0x8000; + subclass &= ~0x1; + if (subclass != AST_FORMAT_H263) { + ast_log(LOG_WARNING, "Asked to write non-h263 frame (%d)!\n", f->subclass); + return -1; + } + ts = htonl(f->samples); + if ((res = fwrite(&ts, 1, sizeof(ts), fs->f)) != sizeof(ts)) { + ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno)); + return -1; + } + len = htons(f->datalen | mark); + if ((res = fwrite(&len, 1, sizeof(len), fs->f)) != sizeof(len)) { + ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno)); + return -1; + } + if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) { + ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); + return -1; + } + return 0; +} + +static int h263_seek(struct ast_filestream *fs, off_t sample_offset, int whence) +{ + /* No way Jose */ + return -1; +} + +static int h263_trunc(struct ast_filestream *fs) +{ + /* Truncate file to current length */ + if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0) + return -1; + return 0; +} + +static off_t h263_tell(struct ast_filestream *fs) +{ + off_t offset = ftello(fs->f); + return offset; /* XXX totally bogus, needs fixing */ +} + +static const struct ast_format h263_f = { + .name = "h263", + .exts = "h263", + .format = AST_FORMAT_H263, + .open = h263_open, + .write = h263_write, + .seek = h263_seek, + .trunc = h263_trunc, + .tell = h263_tell, + .read = h263_read, + .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, + .desc_size = sizeof(struct h263_desc), +}; + +static int load_module(void) +{ + if (ast_format_register(&h263_f)) + return AST_MODULE_LOAD_FAILURE; + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + return ast_format_unregister(h263_f.name); +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw H.263 data"); diff --git a/trunk/formats/format_h264.c b/trunk/formats/format_h264.c new file mode 100644 index 000000000..06def313c --- /dev/null +++ b/trunk/formats/format_h264.c @@ -0,0 +1,175 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 1999 - 2005, Digium, Inc. + * + * Mark Spencer <markster@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief Save to raw, headerless h264 data. + * \arg File name extension: h264 + * \ingroup formats + * \arg See \ref AstVideo + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" +#include "asterisk/endian.h" + +/* Some Ideas for this code came from makeh264e.c by Jeffrey Chilton */ + +/* Portions of the conversion code are by guido@sienanet.it */ +/*! \todo Check this buf size estimate, it may be totally wrong for large frame video */ + +#define BUF_SIZE 4096 /* Two Real h264 Frames */ +struct h264_desc { + unsigned int lastts; +}; + +static int h264_open(struct ast_filestream *s) +{ + unsigned int ts; + int res; + if ((res = fread(&ts, 1, sizeof(ts), s->f)) < sizeof(ts)) { + ast_log(LOG_WARNING, "Empty file!\n"); + return -1; + } + return 0; +} + +static struct ast_frame *h264_read(struct ast_filestream *s, int *whennext) +{ + int res; + int mark=0; + unsigned short len; + unsigned int ts; + struct h264_desc *fs = (struct h264_desc *)s->_private; + + /* Send a frame from the file to the appropriate channel */ + if ((res = fread(&len, 1, sizeof(len), s->f)) < 1) + return NULL; + len = ntohs(len); + mark = (len & 0x8000) ? 1 : 0; + len &= 0x7fff; + if (len > BUF_SIZE) { + ast_log(LOG_WARNING, "Length %d is too long\n", len); + len = BUF_SIZE; /* XXX truncate */ + } + s->fr.frametype = AST_FRAME_VIDEO; + s->fr.subclass = AST_FORMAT_H264; + s->fr.mallocd = 0; + AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, len); + if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) != s->fr.datalen) { + if (res) + ast_log(LOG_WARNING, "Short read (%d of %d) (%s)!\n", res, len, strerror(errno)); + return NULL; + } + s->fr.samples = fs->lastts; + s->fr.datalen = len; + s->fr.subclass |= mark; + s->fr.delivery.tv_sec = 0; + s->fr.delivery.tv_usec = 0; + if ((res = fread(&ts, 1, sizeof(ts), s->f)) == sizeof(ts)) { + fs->lastts = ntohl(ts); + *whennext = fs->lastts * 4/45; + } else + *whennext = 0; + return &s->fr; +} + +static int h264_write(struct ast_filestream *s, struct ast_frame *f) +{ + int res; + unsigned int ts; + unsigned short len; + int mark; + + if (f->frametype != AST_FRAME_VIDEO) { + ast_log(LOG_WARNING, "Asked to write non-video frame!\n"); + return -1; + } + mark = (f->subclass & 0x1) ? 0x8000 : 0; + if ((f->subclass & ~0x1) != AST_FORMAT_H264) { + ast_log(LOG_WARNING, "Asked to write non-h264 frame (%d)!\n", f->subclass); + return -1; + } + ts = htonl(f->samples); + if ((res = fwrite(&ts, 1, sizeof(ts), s->f)) != sizeof(ts)) { + ast_log(LOG_WARNING, "Bad write (%d/4): %s\n", res, strerror(errno)); + return -1; + } + len = htons(f->datalen | mark); + if ((res = fwrite(&len, 1, sizeof(len), s->f)) != sizeof(len)) { + ast_log(LOG_WARNING, "Bad write (%d/2): %s\n", res, strerror(errno)); + return -1; + } + if ((res = fwrite(f->data, 1, f->datalen, s->f)) != f->datalen) { + ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); + return -1; + } + return 0; +} + +static int h264_seek(struct ast_filestream *fs, off_t sample_offset, int whence) +{ + /* No way Jose */ + return -1; +} + +static int h264_trunc(struct ast_filestream *fs) +{ + /* Truncate file to current length */ + if (ftruncate(fileno(fs->f), ftell(fs->f)) < 0) + return -1; + return 0; +} + +static off_t h264_tell(struct ast_filestream *fs) +{ + off_t offset = ftell(fs->f); + return offset; /* XXX totally bogus, needs fixing */ +} + +static const struct ast_format h264_f = { + .name = "h264", + .exts = "h264", + .format = AST_FORMAT_H264, + .open = h264_open, + .write = h264_write, + .seek = h264_seek, + .trunc = h264_trunc, + .tell = h264_tell, + .read = h264_read, + .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, + .desc_size = sizeof(struct h264_desc), +}; + +static int load_module(void) +{ + if (ast_format_register(&h264_f)) + return AST_MODULE_LOAD_FAILURE; + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + return ast_format_unregister(h264_f.name); +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw H.264 data"); diff --git a/trunk/formats/format_ilbc.c b/trunk/formats/format_ilbc.c new file mode 100644 index 000000000..aaddc6c38 --- /dev/null +++ b/trunk/formats/format_ilbc.c @@ -0,0 +1,146 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Brian K. West <brian@bkw.org> + * + * Copyright (C) 1999 - 2005, Digium, Inc. + * + * Mark Spencer <markster@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief Save to raw, headerless iLBC data. + * \arg File name extension: ilbc + * \ingroup formats + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" +#include "asterisk/endian.h" + +/* Some Ideas for this code came from makeg729e.c by Jeffrey Chilton */ + +/* Portions of the conversion code are by guido@sienanet.it */ + +#define ILBC_BUF_SIZE 50 /* One Real iLBC Frame */ +#define ILBC_SAMPLES 240 + +static struct ast_frame *ilbc_read(struct ast_filestream *s, int *whennext) +{ + int res; + /* Send a frame from the file to the appropriate channel */ + s->fr.frametype = AST_FRAME_VOICE; + s->fr.subclass = AST_FORMAT_ILBC; + s->fr.mallocd = 0; + AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, ILBC_BUF_SIZE); + if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) != s->fr.datalen) { + if (res) + ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); + return NULL; + } + *whennext = s->fr.samples = ILBC_SAMPLES; + return &s->fr; +} + +static int ilbc_write(struct ast_filestream *fs, struct ast_frame *f) +{ + int res; + if (f->frametype != AST_FRAME_VOICE) { + ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); + return -1; + } + if (f->subclass != AST_FORMAT_ILBC) { + ast_log(LOG_WARNING, "Asked to write non-iLBC frame (%d)!\n", f->subclass); + return -1; + } + if (f->datalen % 50) { + ast_log(LOG_WARNING, "Invalid data length, %d, should be multiple of 50\n", f->datalen); + return -1; + } + if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) { + ast_log(LOG_WARNING, "Bad write (%d/50): %s\n", res, strerror(errno)); + return -1; + } + return 0; +} + +static int ilbc_seek(struct ast_filestream *fs, off_t sample_offset, int whence) +{ + long bytes; + off_t min,cur,max,offset=0; + min = 0; + cur = ftello(fs->f); + fseeko(fs->f, 0, SEEK_END); + max = ftello(fs->f); + + bytes = ILBC_BUF_SIZE * (sample_offset / ILBC_SAMPLES); + if (whence == SEEK_SET) + offset = bytes; + else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) + offset = cur + bytes; + else if (whence == SEEK_END) + offset = max - bytes; + if (whence != SEEK_FORCECUR) { + offset = (offset > max)?max:offset; + } + /* protect against seeking beyond begining. */ + offset = (offset < min)?min:offset; + if (fseeko(fs->f, offset, SEEK_SET) < 0) + return -1; + return 0; +} + +static int ilbc_trunc(struct ast_filestream *fs) +{ + /* Truncate file to current length */ + if (ftruncate(fileno(fs->f), ftello(fs->f)) < 0) + return -1; + return 0; +} + +static off_t ilbc_tell(struct ast_filestream *fs) +{ + off_t offset = ftello(fs->f); + return (offset/ILBC_BUF_SIZE)*ILBC_SAMPLES; +} + +static const struct ast_format ilbc_f = { + .name = "iLBC", + .exts = "ilbc", + .format = AST_FORMAT_ILBC, + .write = ilbc_write, + .seek = ilbc_seek, + .trunc = ilbc_trunc, + .tell = ilbc_tell, + .read = ilbc_read, + .buf_size = ILBC_BUF_SIZE + AST_FRIENDLY_OFFSET, +}; + +static int load_module(void) +{ + if (ast_format_register(&ilbc_f)) + return AST_MODULE_LOAD_FAILURE; + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + return ast_format_unregister(ilbc_f.name); +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw iLBC data"); diff --git a/trunk/formats/format_jpeg.c b/trunk/formats/format_jpeg.c new file mode 100644 index 000000000..4d8d7855d --- /dev/null +++ b/trunk/formats/format_jpeg.c @@ -0,0 +1,115 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 1999 - 2005, Digium, Inc. + * + * Mark Spencer <markster@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief JPEG File format support. + * + * \arg File name extension: jpeg, jpg + * \ingroup formats + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" +#include "asterisk/image.h" +#include "asterisk/endian.h" + +static struct ast_frame *jpeg_read_image(int fd, int len) +{ + struct ast_frame fr; + int res; + char buf[65536]; + if (len > sizeof(buf) || len < 0) { + ast_log(LOG_WARNING, "JPEG image too large to read\n"); + return NULL; + } + res = read(fd, buf, len); + if (res < len) { + ast_log(LOG_WARNING, "Only read %d of %d bytes: %s\n", res, len, strerror(errno)); + } + memset(&fr, 0, sizeof(fr)); + fr.frametype = AST_FRAME_IMAGE; + fr.subclass = AST_FORMAT_JPEG; + fr.data = buf; + fr.src = "JPEG Read"; + fr.datalen = len; + return ast_frisolate(&fr); +} + +static int jpeg_identify(int fd) +{ + char buf[10]; + int res; + res = read(fd, buf, sizeof(buf)); + if (res < sizeof(buf)) + return 0; + if (memcmp(buf + 6, "JFIF", 4)) + return 0; + return 1; +} + +static int jpeg_write_image(int fd, struct ast_frame *fr) +{ + int res=0; + if (fr->frametype != AST_FRAME_IMAGE) { + ast_log(LOG_WARNING, "Not an image\n"); + return -1; + } + if (fr->subclass != AST_FORMAT_JPEG) { + ast_log(LOG_WARNING, "Not a jpeg image\n"); + return -1; + } + if (fr->datalen) { + res = write(fd, fr->data, fr->datalen); + if (res != fr->datalen) { + ast_log(LOG_WARNING, "Only wrote %d of %d bytes: %s\n", res, fr->datalen, strerror(errno)); + return -1; + } + } + return res; +} + +static struct ast_imager jpeg_format = { + .name = "jpg", + .desc = "JPEG (Joint Picture Experts Group)", + .exts = "jpg|jpeg", + .format = AST_FORMAT_JPEG, + .read_image = jpeg_read_image, + .identify = jpeg_identify, + .write_image = jpeg_write_image, +}; + +static int load_module(void) +{ + if (ast_image_register(&jpeg_format)) + return AST_MODULE_LOAD_FAILURE; + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + ast_image_unregister(&jpeg_format); + + return 0; +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "JPEG (Joint Picture Experts Group) Image Format"); diff --git a/trunk/formats/format_ogg_vorbis.c b/trunk/formats/format_ogg_vorbis.c new file mode 100644 index 000000000..669e96a7d --- /dev/null +++ b/trunk/formats/format_ogg_vorbis.c @@ -0,0 +1,552 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 2005, Jeff Ollie + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief OGG/Vorbis streams. + * \arg File name extension: ogg + * \ingroup formats + */ + +/* the order of these dependencies is important... it also specifies + the link order of the libraries during linking +*/ + +/*** MODULEINFO + <depend>vorbis</depend> + <depend>ogg</depend> + ***/ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include <vorbis/codec.h> +#include <vorbis/vorbisenc.h> + +#ifdef _WIN32 +#include <io.h> +#endif + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" + +/* + * this is the number of samples we deal with. Samples are converted + * to SLINEAR so each one uses 2 bytes in the buffer. + */ +#define SAMPLES_MAX 160 +#define BUF_SIZE (2*SAMPLES_MAX) + +#define BLOCK_SIZE 4096 /* used internally in the vorbis routines */ + +struct vorbis_desc { /* format specific parameters */ + /* structures for handling the Ogg container */ + ogg_sync_state oy; + ogg_stream_state os; + ogg_page og; + ogg_packet op; + + /* structures for handling Vorbis audio data */ + vorbis_info vi; + vorbis_comment vc; + vorbis_dsp_state vd; + vorbis_block vb; + + /*! \brief Indicates whether this filestream is set up for reading or writing. */ + int writing; + + /*! \brief Indicates whether an End of Stream condition has been detected. */ + int eos; +}; + +/*! + * \brief Create a new OGG/Vorbis filestream and set it up for reading. + * \param s File that points to on disk storage of the OGG/Vorbis data. + * \return The new filestream. + */ +static int ogg_vorbis_open(struct ast_filestream *s) +{ + int i; + int bytes; + int result; + char **ptr; + char *buffer; + struct vorbis_desc *tmp = (struct vorbis_desc *)s->_private; + + tmp->writing = 0; + + ogg_sync_init(&tmp->oy); + + buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE); + bytes = fread(buffer, 1, BLOCK_SIZE, s->f); + ogg_sync_wrote(&tmp->oy, bytes); + + result = ogg_sync_pageout(&tmp->oy, &tmp->og); + if (result != 1) { + if(bytes < BLOCK_SIZE) { + ast_log(LOG_ERROR, "Run out of data...\n"); + } else { + ast_log(LOG_ERROR, "Input does not appear to be an Ogg bitstream.\n"); + } + ogg_sync_clear(&tmp->oy); + return -1; + } + + ogg_stream_init(&tmp->os, ogg_page_serialno(&tmp->og)); + vorbis_info_init(&tmp->vi); + vorbis_comment_init(&tmp->vc); + + if (ogg_stream_pagein(&tmp->os, &tmp->og) < 0) { + ast_log(LOG_ERROR, "Error reading first page of Ogg bitstream data.\n"); +error: + ogg_stream_clear(&tmp->os); + vorbis_comment_clear(&tmp->vc); + vorbis_info_clear(&tmp->vi); + ogg_sync_clear(&tmp->oy); + return -1; + } + + if (ogg_stream_packetout(&tmp->os, &tmp->op) != 1) { + ast_log(LOG_ERROR, "Error reading initial header packet.\n"); + goto error; + } + + if (vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op) < 0) { + ast_log(LOG_ERROR, "This Ogg bitstream does not contain Vorbis audio data.\n"); + goto error; + } + + for (i = 0; i < 2 ; ) { + while (i < 2) { + result = ogg_sync_pageout(&tmp->oy, &tmp->og); + if (result == 0) + break; + if (result == 1) { + ogg_stream_pagein(&tmp->os, &tmp->og); + while(i < 2) { + result = ogg_stream_packetout(&tmp->os,&tmp->op); + if(result == 0) + break; + if(result < 0) { + ast_log(LOG_ERROR, "Corrupt secondary header. Exiting.\n"); + goto error; + } + vorbis_synthesis_headerin(&tmp->vi, &tmp->vc, &tmp->op); + i++; + } + } + } + + buffer = ogg_sync_buffer(&tmp->oy, BLOCK_SIZE); + bytes = fread(buffer, 1, BLOCK_SIZE, s->f); + if (bytes == 0 && i < 2) { + ast_log(LOG_ERROR, "End of file before finding all Vorbis headers!\n"); + goto error; + } + ogg_sync_wrote(&tmp->oy, bytes); + } + + for (ptr = tmp->vc.user_comments; *ptr; ptr++) + ast_debug(1, "OGG/Vorbis comment: %s\n", *ptr); + ast_debug(1, "OGG/Vorbis bitstream is %d channel, %ldHz\n", tmp->vi.channels, tmp->vi.rate); + ast_debug(1, "OGG/Vorbis file encoded by: %s\n", tmp->vc.vendor); + + if (tmp->vi.channels != 1) { + ast_log(LOG_ERROR, "Only monophonic OGG/Vorbis files are currently supported!\n"); + goto error; + } + + if (tmp->vi.rate != DEFAULT_SAMPLE_RATE) { + ast_log(LOG_ERROR, "Only 8000Hz OGG/Vorbis files are currently supported!\n"); + vorbis_block_clear(&tmp->vb); + vorbis_dsp_clear(&tmp->vd); + goto error; + } + + vorbis_synthesis_init(&tmp->vd, &tmp->vi); + vorbis_block_init(&tmp->vd, &tmp->vb); + + return 0; +} + +/*! + * \brief Create a new OGG/Vorbis filestream and set it up for writing. + * \param s File pointer that points to on-disk storage. + * \param comment Comment that should be embedded in the OGG/Vorbis file. + * \return A new filestream. + */ +static int ogg_vorbis_rewrite(struct ast_filestream *s, + const char *comment) +{ + ogg_packet header; + ogg_packet header_comm; + ogg_packet header_code; + struct vorbis_desc *tmp = (struct vorbis_desc *)s->_private; + + tmp->writing = 1; + + vorbis_info_init(&tmp->vi); + + if (vorbis_encode_init_vbr(&tmp->vi, 1, DEFAULT_SAMPLE_RATE, 0.4)) { + ast_log(LOG_ERROR, "Unable to initialize Vorbis encoder!\n"); + return -1; + } + + vorbis_comment_init(&tmp->vc); + vorbis_comment_add_tag(&tmp->vc, "ENCODER", "Asterisk PBX"); + if (comment) + vorbis_comment_add_tag(&tmp->vc, "COMMENT", (char *) comment); + + vorbis_analysis_init(&tmp->vd, &tmp->vi); + vorbis_block_init(&tmp->vd, &tmp->vb); + + ogg_stream_init(&tmp->os, ast_random()); + + vorbis_analysis_headerout(&tmp->vd, &tmp->vc, &header, &header_comm, + &header_code); + ogg_stream_packetin(&tmp->os, &header); + ogg_stream_packetin(&tmp->os, &header_comm); + ogg_stream_packetin(&tmp->os, &header_code); + + while (!tmp->eos) { + if (ogg_stream_flush(&tmp->os, &tmp->og) == 0) + break; + fwrite(tmp->og.header, 1, tmp->og.header_len, s->f); + fwrite(tmp->og.body, 1, tmp->og.body_len, s->f); + if (ogg_page_eos(&tmp->og)) + tmp->eos = 1; + } + + return 0; +} + +/*! + * \brief Write out any pending encoded data. + * \param s An OGG/Vorbis filestream. + * \param f The file to write to. + */ +static void write_stream(struct vorbis_desc *s, FILE *f) +{ + while (vorbis_analysis_blockout(&s->vd, &s->vb) == 1) { + vorbis_analysis(&s->vb, NULL); + vorbis_bitrate_addblock(&s->vb); + + while (vorbis_bitrate_flushpacket(&s->vd, &s->op)) { + ogg_stream_packetin(&s->os, &s->op); + while (!s->eos) { + if (ogg_stream_pageout(&s->os, &s->og) == 0) { + break; + } + fwrite(s->og.header, 1, s->og.header_len, f); + fwrite(s->og.body, 1, s->og.body_len, f); + if (ogg_page_eos(&s->og)) { + s->eos = 1; + } + } + } + } +} + +/*! + * \brief Write audio data from a frame to an OGG/Vorbis filestream. + * \param fs An OGG/Vorbis filestream. + * \param f A frame containing audio to be written to the filestream. + * \return -1 if there was an error, 0 on success. + */ +static int ogg_vorbis_write(struct ast_filestream *fs, struct ast_frame *f) +{ + int i; + float **buffer; + short *data; + struct vorbis_desc *s = (struct vorbis_desc *)fs->_private; + + if (!s->writing) { + ast_log(LOG_ERROR, "This stream is not set up for writing!\n"); + return -1; + } + + if (f->frametype != AST_FRAME_VOICE) { + ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); + return -1; + } + if (f->subclass != AST_FORMAT_SLINEAR) { + ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n", + f->subclass); + return -1; + } + if (!f->datalen) + return -1; + + data = (short *) f->data; + + buffer = vorbis_analysis_buffer(&s->vd, f->samples); + + for (i = 0; i < f->samples; i++) + buffer[0][i] = (double)data[i] / 32768.0; + + vorbis_analysis_wrote(&s->vd, f->samples); + + write_stream(s, fs->f); + + return 0; +} + +/*! + * \brief Close a OGG/Vorbis filestream. + * \param fs A OGG/Vorbis filestream. + */ +static void ogg_vorbis_close(struct ast_filestream *fs) +{ + struct vorbis_desc *s = (struct vorbis_desc *)fs->_private; + + if (s->writing) { + /* Tell the Vorbis encoder that the stream is finished + * and write out the rest of the data */ + vorbis_analysis_wrote(&s->vd, 0); + write_stream(s, fs->f); + } + + ogg_stream_clear(&s->os); + vorbis_block_clear(&s->vb); + vorbis_dsp_clear(&s->vd); + vorbis_comment_clear(&s->vc); + vorbis_info_clear(&s->vi); + + if (s->writing) { + ogg_sync_clear(&s->oy); + } +} + +/*! + * \brief Get audio data. + * \param fs An OGG/Vorbis filestream. + * \param pcm Pointer to a buffere to store audio data in. + */ + +static int read_samples(struct ast_filestream *fs, float ***pcm) +{ + int samples_in; + int result; + char *buffer; + int bytes; + struct vorbis_desc *s = (struct vorbis_desc *)fs->_private; + + while (1) { + samples_in = vorbis_synthesis_pcmout(&s->vd, pcm); + if (samples_in > 0) { + return samples_in; + } + + /* The Vorbis decoder needs more data... */ + /* See ifOGG has any packets in the current page for the Vorbis decoder. */ + result = ogg_stream_packetout(&s->os, &s->op); + if (result > 0) { + /* Yes OGG had some more packets for the Vorbis decoder. */ + if (vorbis_synthesis(&s->vb, &s->op) == 0) { + vorbis_synthesis_blockin(&s->vd, &s->vb); + } + + continue; + } + + if (result < 0) + ast_log(LOG_WARNING, + "Corrupt or missing data at this page position; continuing...\n"); + + /* No more packets left in the current page... */ + + if (s->eos) { + /* No more pages left in the stream */ + return -1; + } + + while (!s->eos) { + /* See ifOGG has any pages in it's internal buffers */ + result = ogg_sync_pageout(&s->oy, &s->og); + if (result > 0) { + /* Yes, OGG has more pages in it's internal buffers, + add the page to the stream state */ + result = ogg_stream_pagein(&s->os, &s->og); + if (result == 0) { + /* Yes, got a new,valid page */ + if (ogg_page_eos(&s->og)) { + s->eos = 1; + } + break; + } + ast_log(LOG_WARNING, + "Invalid page in the bitstream; continuing...\n"); + } + + if (result < 0) + ast_log(LOG_WARNING, + "Corrupt or missing data in bitstream; continuing...\n"); + + /* No, we need to read more data from the file descrptor */ + /* get a buffer from OGG to read the data into */ + buffer = ogg_sync_buffer(&s->oy, BLOCK_SIZE); + /* read more data from the file descriptor */ + bytes = fread(buffer, 1, BLOCK_SIZE, fs->f); + /* Tell OGG how many bytes we actually read into the buffer */ + ogg_sync_wrote(&s->oy, bytes); + if (bytes == 0) { + s->eos = 1; + } + } + } +} + +/*! + * \brief Read a frame full of audio data from the filestream. + * \param fs The filestream. + * \param whennext Number of sample times to schedule the next call. + * \return A pointer to a frame containing audio data or NULL ifthere is no more audio data. + */ +static struct ast_frame *ogg_vorbis_read(struct ast_filestream *fs, + int *whennext) +{ + int clipflag = 0; + int i; + int j; + double accumulator[SAMPLES_MAX]; + int val; + int samples_in; + int samples_out = 0; + struct vorbis_desc *s = (struct vorbis_desc *)fs->_private; + short *buf; /* SLIN data buffer */ + + fs->fr.frametype = AST_FRAME_VOICE; + fs->fr.subclass = AST_FORMAT_SLINEAR; + fs->fr.mallocd = 0; + AST_FRAME_SET_BUFFER(&fs->fr, fs->buf, AST_FRIENDLY_OFFSET, BUF_SIZE); + buf = (short *)(fs->fr.data); /* SLIN data buffer */ + + while (samples_out != SAMPLES_MAX) { + float **pcm; + int len = SAMPLES_MAX - samples_out; + + /* See ifVorbis decoder has some audio data for us ... */ + samples_in = read_samples(fs, &pcm); + if (samples_in <= 0) + break; + + /* Got some audio data from Vorbis... */ + /* Convert the float audio data to 16-bit signed linear */ + + clipflag = 0; + if (samples_in > len) + samples_in = len; + for (j = 0; j < samples_in; j++) + accumulator[j] = 0.0; + + for (i = 0; i < s->vi.channels; i++) { + float *mono = pcm[i]; + for (j = 0; j < samples_in; j++) + accumulator[j] += mono[j]; + } + + for (j = 0; j < samples_in; j++) { + val = accumulator[j] * 32767.0 / s->vi.channels; + if (val > 32767) { + val = 32767; + clipflag = 1; + } else if (val < -32768) { + val = -32768; + clipflag = 1; + } + buf[samples_out + j] = val; + } + + if (clipflag) + ast_log(LOG_WARNING, "Clipping in frame %ld\n", (long) (s->vd.sequence)); + /* Tell the Vorbis decoder how many samples we actually used. */ + vorbis_synthesis_read(&s->vd, samples_in); + samples_out += samples_in; + } + + if (samples_out > 0) { + fs->fr.datalen = samples_out * 2; + fs->fr.samples = samples_out; + *whennext = samples_out; + + return &fs->fr; + } else { + return NULL; + } +} + +/*! + * \brief Trucate an OGG/Vorbis filestream. + * \param s The filestream to truncate. + * \return 0 on success, -1 on failure. + */ + +static int ogg_vorbis_trunc(struct ast_filestream *s) +{ + ast_log(LOG_WARNING, "Truncation is not supported on OGG/Vorbis streams!\n"); + return -1; +} + +/*! + * \brief Seek to a specific position in an OGG/Vorbis filestream. + * \param s The filestream to truncate. + * \param sample_offset New position for the filestream, measured in 8KHz samples. + * \param whence Location to measure + * \return 0 on success, -1 on failure. + */ +static int ogg_vorbis_seek(struct ast_filestream *s, off_t sample_offset, int whence) +{ + ast_log(LOG_WARNING, "Seeking is not supported on OGG/Vorbis streams!\n"); + return -1; +} + +static off_t ogg_vorbis_tell(struct ast_filestream *s) +{ + ast_log(LOG_WARNING, "Telling is not supported on OGG/Vorbis streams!\n"); + return -1; +} + +static const struct ast_format vorbis_f = { + .name = "ogg_vorbis", + .exts = "ogg", + .format = AST_FORMAT_SLINEAR, + .open = ogg_vorbis_open, + .rewrite = ogg_vorbis_rewrite, + .write = ogg_vorbis_write, + .seek = ogg_vorbis_seek, + .trunc = ogg_vorbis_trunc, + .tell = ogg_vorbis_tell, + .read = ogg_vorbis_read, + .close = ogg_vorbis_close, + .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, + .desc_size = sizeof(struct vorbis_desc), +}; + +static int load_module(void) +{ + if (ast_format_register(&vorbis_f)) + return AST_MODULE_LOAD_FAILURE; + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + return ast_format_unregister(vorbis_f.name); +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "OGG/Vorbis audio"); + diff --git a/trunk/formats/format_pcm.c b/trunk/formats/format_pcm.c new file mode 100644 index 000000000..c514b5863 --- /dev/null +++ b/trunk/formats/format_pcm.c @@ -0,0 +1,485 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 1999 - 2006, Digium, Inc. + * + * Mark Spencer <markster@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief Flat, binary, ulaw PCM file format. + * \arg File name extension: pcm, ulaw, ul, mu + * + * \ingroup formats + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" +#include "asterisk/endian.h" +#include "asterisk/ulaw.h" +#include "asterisk/alaw.h" + +#define BUF_SIZE 160 /* 160 bytes, and same number of samples */ + +static char ulaw_silence[BUF_SIZE]; +static char alaw_silence[BUF_SIZE]; + +/* #define REALTIME_WRITE */ /* XXX does it work at all ? */ + +#ifdef REALTIME_WRITE +struct pcm_desc { + unsigned long start_time; +}; + +/* Returns time in msec since system boot. */ +static unsigned long get_time(void) +{ + struct tms buf; + clock_t cur; + + cur = times( &buf ); + if( cur < 0 ) { + ast_log( LOG_WARNING, "Cannot get current time\n" ); + return 0; + } + return cur * 1000 / sysconf( _SC_CLK_TCK ); +} + +static int pcma_open(struct ast_filestream *s) +{ + if (s->fmt->format == AST_FORMAT_ALAW) + pd->starttime = get_time(); + return 0; +} + +static int pcma_rewrite(struct ast_filestream *s, const char *comment) +{ + return pcma_open(s); +} +#endif + +static struct ast_frame *pcm_read(struct ast_filestream *s, int *whennext) +{ + int res; + + /* Send a frame from the file to the appropriate channel */ + + s->fr.frametype = AST_FRAME_VOICE; + s->fr.subclass = s->fmt->format; + s->fr.mallocd = 0; + AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE); + if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) < 1) { + if (res) + ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); + return NULL; + } + s->fr.datalen = res; + *whennext = s->fr.samples = res; + return &s->fr; +} + +static int pcm_seek(struct ast_filestream *fs, off_t sample_offset, int whence) +{ + off_t cur, max, offset = 0; + int ret = -1; /* assume error */ + + cur = ftello(fs->f); + fseeko(fs->f, 0, SEEK_END); + max = ftello(fs->f); + + switch (whence) { + case SEEK_SET: + offset = sample_offset; + break; + case SEEK_END: + offset = max - sample_offset; + break; + case SEEK_CUR: + case SEEK_FORCECUR: + offset = cur + sample_offset; + break; + default: + ast_log(LOG_WARNING, "invalid whence %d, assuming SEEK_SET\n", whence); + offset = sample_offset; + } + if (offset < 0) { + ast_log(LOG_WARNING, "negative offset %ld, resetting to 0\n", (long) offset); + offset = 0; + } + if (whence == SEEK_FORCECUR && offset > max) { /* extend the file */ + size_t left = offset - max; + const char *src = (fs->fmt->format == AST_FORMAT_ALAW) ? alaw_silence : ulaw_silence; + + while (left) { + size_t written = fwrite(src, 1, (left > BUF_SIZE) ? BUF_SIZE : left, fs->f); + if (written == -1) + break; /* error */ + left -= written; + } + ret = 0; /* successful */ + } else { + if (offset > max) { + ast_log(LOG_WARNING, "offset too large %ld, truncating to %ld\n", (long) offset, (long) max); + offset = max; + } + ret = fseeko(fs->f, offset, SEEK_SET); + } + return ret; +} + +static int pcm_trunc(struct ast_filestream *fs) +{ + return ftruncate(fileno(fs->f), ftello(fs->f)); +} + +static off_t pcm_tell(struct ast_filestream *fs) +{ + return ftello(fs->f); +} + +static int pcm_write(struct ast_filestream *fs, struct ast_frame *f) +{ + int res; + + if (f->frametype != AST_FRAME_VOICE) { + ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); + return -1; + } + if (f->subclass != fs->fmt->format) { + ast_log(LOG_WARNING, "Asked to write incompatible format frame (%d)!\n", f->subclass); + return -1; + } + +#ifdef REALTIME_WRITE + if (s->fmt->format == AST_FORMAT_ALAW) { + struct pcm_desc *pd = (struct pcm_desc *)fs->_private; + struct stat stat_buf; + unsigned long cur_time = get_time(); + unsigned long fpos = ( cur_time - pd->start_time ) * 8; /* 8 bytes per msec */ + /* Check if we have written to this position yet. If we have, then increment pos by one frame + * for some degree of protection against receiving packets in the same clock tick. + */ + + fstat(fileno(fs->f), &stat_buf ); + if (stat_buf.st_size > fpos ) + fpos += f->datalen; /* Incrementing with the size of this current frame */ + + if (stat_buf.st_size < fpos) { + /* fill the gap with 0x55 rather than 0. */ + char buf[1024]; + unsigned long cur, to_write; + + cur = stat_buf.st_size; + if (fseek(fs->f, cur, SEEK_SET) < 0) { + ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) ); + return -1; + } + memset(buf, 0x55, 512); + while (cur < fpos) { + to_write = fpos - cur; + if (to_write > sizeof(buf)) + to_write = sizeof(buf); + fwrite(buf, 1, to_write, fs->f); + cur += to_write; + } + } + + if (fseek(s->f, fpos, SEEK_SET) < 0) { + ast_log( LOG_WARNING, "Cannot seek in file: %s\n", strerror(errno) ); + return -1; + } + } +#endif /* REALTIME_WRITE */ + + if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) { + ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); + return -1; + } + return 0; +} + +/* SUN .au support routines */ + +#define AU_HEADER_SIZE 24 +#define AU_HEADER(var) uint32_t var[6] + +#define AU_HDR_MAGIC_OFF 0 +#define AU_HDR_HDR_SIZE_OFF 1 +#define AU_HDR_DATA_SIZE_OFF 2 +#define AU_HDR_ENCODING_OFF 3 +#define AU_HDR_SAMPLE_RATE_OFF 4 +#define AU_HDR_CHANNELS_OFF 5 + +#define AU_ENC_8BIT_ULAW 1 + +#define AU_MAGIC 0x2e736e64 +#if __BYTE_ORDER == __BIG_ENDIAN +#define htoll(b) (b) +#define htols(b) (b) +#define ltohl(b) (b) +#define ltohs(b) (b) +#else +#if __BYTE_ORDER == __LITTLE_ENDIAN +#define htoll(b) \ + (((((b) ) & 0xFF) << 24) | \ + ((((b) >> 8) & 0xFF) << 16) | \ + ((((b) >> 16) & 0xFF) << 8) | \ + ((((b) >> 24) & 0xFF) )) +#define htols(b) \ + (((((b) ) & 0xFF) << 8) | \ + ((((b) >> 8) & 0xFF) )) +#define ltohl(b) htoll(b) +#define ltohs(b) htols(b) +#else +#error "Endianess not defined" +#endif +#endif + +static int check_header(FILE *f) +{ + AU_HEADER(header); + uint32_t magic; + uint32_t hdr_size; + uint32_t data_size; + uint32_t encoding; + uint32_t sample_rate; + uint32_t channels; + + if (fread(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) { + ast_log(LOG_WARNING, "Read failed (header)\n"); + return -1; + } + magic = ltohl(header[AU_HDR_MAGIC_OFF]); + if (magic != (uint32_t) AU_MAGIC) { + ast_log(LOG_WARNING, "Bad magic: 0x%x\n", magic); + } +/* hdr_size = ltohl(header[AU_HDR_HDR_SIZE_OFF]); + if (hdr_size < AU_HEADER_SIZE)*/ + hdr_size = AU_HEADER_SIZE; +/* data_size = ltohl(header[AU_HDR_DATA_SIZE_OFF]); */ + encoding = ltohl(header[AU_HDR_ENCODING_OFF]); + if (encoding != AU_ENC_8BIT_ULAW) { + ast_log(LOG_WARNING, "Unexpected format: %d. Only 8bit ULAW allowed (%d)\n", encoding, AU_ENC_8BIT_ULAW); + return -1; + } + sample_rate = ltohl(header[AU_HDR_SAMPLE_RATE_OFF]); + if (sample_rate != DEFAULT_SAMPLE_RATE) { + ast_log(LOG_WARNING, "Sample rate can only be 8000 not %d\n", sample_rate); + return -1; + } + channels = ltohl(header[AU_HDR_CHANNELS_OFF]); + if (channels != 1) { + ast_log(LOG_WARNING, "Not in mono: channels=%d\n", channels); + return -1; + } + /* Skip to data */ + fseek(f, 0, SEEK_END); + data_size = ftell(f) - hdr_size; + if (fseek(f, hdr_size, SEEK_SET) == -1 ) { + ast_log(LOG_WARNING, "Failed to skip to data: %d\n", hdr_size); + return -1; + } + return data_size; +} + +static int update_header(FILE *f) +{ + off_t cur, end; + uint32_t datalen; + int bytes; + + cur = ftell(f); + fseek(f, 0, SEEK_END); + end = ftell(f); + /* data starts 24 bytes in */ + bytes = end - AU_HEADER_SIZE; + datalen = htoll(bytes); + + if (cur < 0) { + ast_log(LOG_WARNING, "Unable to find our position\n"); + return -1; + } + if (fseek(f, AU_HDR_DATA_SIZE_OFF * sizeof(uint32_t), SEEK_SET)) { + ast_log(LOG_WARNING, "Unable to set our position\n"); + return -1; + } + if (fwrite(&datalen, 1, sizeof(datalen), f) != sizeof(datalen)) { + ast_log(LOG_WARNING, "Unable to set write file size\n"); + return -1; + } + if (fseek(f, cur, SEEK_SET)) { + ast_log(LOG_WARNING, "Unable to return to position\n"); + return -1; + } + return 0; +} + +static int write_header(FILE *f) +{ + AU_HEADER(header); + + header[AU_HDR_MAGIC_OFF] = htoll((uint32_t) AU_MAGIC); + header[AU_HDR_HDR_SIZE_OFF] = htoll(AU_HEADER_SIZE); + header[AU_HDR_DATA_SIZE_OFF] = 0; + header[AU_HDR_ENCODING_OFF] = htoll(AU_ENC_8BIT_ULAW); + header[AU_HDR_SAMPLE_RATE_OFF] = htoll(DEFAULT_SAMPLE_RATE); + header[AU_HDR_CHANNELS_OFF] = htoll(1); + + /* Write an au header, ignoring sizes which will be filled in later */ + fseek(f, 0, SEEK_SET); + if (fwrite(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + return 0; +} + +static int au_open(struct ast_filestream *s) +{ + if (check_header(s->f) < 0) + return -1; + return 0; +} + +static int au_rewrite(struct ast_filestream *s, const char *comment) +{ + if (write_header(s->f)) + return -1; + return 0; +} + +/* XXX check this, probably incorrect */ +static int au_seek(struct ast_filestream *fs, off_t sample_offset, int whence) +{ + off_t min, max, cur; + long offset = 0, samples; + + samples = sample_offset; + min = AU_HEADER_SIZE; + cur = ftello(fs->f); + fseek(fs->f, 0, SEEK_END); + max = ftello(fs->f); + if (whence == SEEK_SET) + offset = samples + min; + else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) + offset = samples + cur; + else if (whence == SEEK_END) + offset = max - samples; + if (whence != SEEK_FORCECUR) { + offset = (offset > max) ? max : offset; + } + /* always protect the header space. */ + offset = (offset < min) ? min : offset; + return fseeko(fs->f, offset, SEEK_SET); +} + +static int au_trunc(struct ast_filestream *fs) +{ + if (ftruncate(fileno(fs->f), ftell(fs->f))) + return -1; + return update_header(fs->f); +} + +static off_t au_tell(struct ast_filestream *fs) +{ + off_t offset = ftello(fs->f); + return offset - AU_HEADER_SIZE; +} + +static const struct ast_format alaw_f = { + .name = "alaw", + .exts = "alaw|al", + .format = AST_FORMAT_ALAW, + .write = pcm_write, + .seek = pcm_seek, + .trunc = pcm_trunc, + .tell = pcm_tell, + .read = pcm_read, + .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, +#ifdef REALTIME_WRITE + .open = pcma_open, + .rewrite = pcma_rewrite, + .desc_size = sizeof(struct pcm_desc), +#endif +}; + +static const struct ast_format pcm_f = { + .name = "pcm", + .exts = "pcm|ulaw|ul|mu", + .format = AST_FORMAT_ULAW, + .write = pcm_write, + .seek = pcm_seek, + .trunc = pcm_trunc, + .tell = pcm_tell, + .read = pcm_read, + .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, +}; + +static const struct ast_format g722_f = { + .name = "g722", + .exts = "g722", + .format = AST_FORMAT_G722, + .write = pcm_write, + .seek = pcm_seek, + .trunc = pcm_trunc, + .tell = pcm_tell, + .read = pcm_read, + .buf_size = (BUF_SIZE * 2) + AST_FRIENDLY_OFFSET, +}; + +static const struct ast_format au_f = { + .name = "au", + .exts = "au", + .format = AST_FORMAT_ULAW, + .open = au_open, + .rewrite = au_rewrite, + .write = pcm_write, + .seek = au_seek, + .trunc = au_trunc, + .tell = au_tell, + .read = pcm_read, + .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, /* this many shorts */ +}; + +static int load_module(void) +{ + int index; + + /* XXX better init ? */ + for (index = 0; index < (sizeof(ulaw_silence) / sizeof(ulaw_silence[0])); index++) + ulaw_silence[index] = AST_LIN2MU(0); + for (index = 0; index < (sizeof(alaw_silence) / sizeof(alaw_silence[0])); index++) + alaw_silence[index] = AST_LIN2A(0); + + if ( ast_format_register(&pcm_f) + || ast_format_register(&alaw_f) + || ast_format_register(&au_f) + || ast_format_register(&g722_f) ) + return AST_MODULE_LOAD_FAILURE; + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + return ast_format_unregister(pcm_f.name) + || ast_format_unregister(alaw_f.name) + || ast_format_unregister(au_f.name) + || ast_format_unregister(g722_f.name); +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw/Sun uLaw/ALaw 8KHz (PCM,PCMA,AU), G.722 16Khz"); diff --git a/trunk/formats/format_sln.c b/trunk/formats/format_sln.c new file mode 100644 index 000000000..51f796271 --- /dev/null +++ b/trunk/formats/format_sln.c @@ -0,0 +1,130 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 1999 - 2005, Anthony Minessale + * Anthony Minessale (anthmct@yahoo.com) + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief RAW SLINEAR Format + * \arg File name extensions: sln, raw + * \ingroup formats + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" +#include "asterisk/endian.h" + +#define BUF_SIZE 320 /* 320 bytes, 160 samples */ +#define SLIN_SAMPLES 160 + +static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext) +{ + int res; + /* Send a frame from the file to the appropriate channel */ + + s->fr.frametype = AST_FRAME_VOICE; + s->fr.subclass = AST_FORMAT_SLINEAR; + s->fr.mallocd = 0; + AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE); + if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) < 1) { + if (res) + ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); + return NULL; + } + *whennext = s->fr.samples = res/2; + s->fr.datalen = res; + return &s->fr; +} + +static int slinear_write(struct ast_filestream *fs, struct ast_frame *f) +{ + int res; + if (f->frametype != AST_FRAME_VOICE) { + ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); + return -1; + } + if (f->subclass != AST_FORMAT_SLINEAR) { + ast_log(LOG_WARNING, "Asked to write non-slinear frame (%d)!\n", f->subclass); + return -1; + } + if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) { + ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); + return -1; + } + return 0; +} + +static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int whence) +{ + off_t offset=0,min,cur,max; + + min = 0; + sample_offset <<= 1; + cur = ftello(fs->f); + fseeko(fs->f, 0, SEEK_END); + max = ftello(fs->f); + if (whence == SEEK_SET) + offset = sample_offset; + else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) + offset = sample_offset + cur; + else if (whence == SEEK_END) + offset = max - sample_offset; + if (whence != SEEK_FORCECUR) { + offset = (offset > max)?max:offset; + } + /* always protect against seeking past begining. */ + offset = (offset < min)?min:offset; + return fseeko(fs->f, offset, SEEK_SET); +} + +static int slinear_trunc(struct ast_filestream *fs) +{ + return ftruncate(fileno(fs->f), ftello(fs->f)); +} + +static off_t slinear_tell(struct ast_filestream *fs) +{ + return ftello(fs->f) / 2; +} + +static const struct ast_format slin_f = { + .name = "sln", + .exts = "sln|raw", + .format = AST_FORMAT_SLINEAR, + .write = slinear_write, + .seek = slinear_seek, + .trunc = slinear_trunc, + .tell = slinear_tell, + .read = slinear_read, + .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, +}; + +static int load_module(void) +{ + if (ast_format_register(&slin_f)) + return AST_MODULE_LOAD_FAILURE; + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + return ast_format_unregister(slin_f.name); +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw Signed Linear Audio support (SLN)"); diff --git a/trunk/formats/format_sln16.c b/trunk/formats/format_sln16.c new file mode 100644 index 000000000..50349f2dd --- /dev/null +++ b/trunk/formats/format_sln16.c @@ -0,0 +1,138 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 1999 - 2008, Anthony Minessale and Digium, Inc. + * Anthony Minessale (anthmct@yahoo.com) + * Kevin P. Fleming <kpfleming@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief RAW SLINEAR 16 Format + * \arg File name extensions: sln16 + * \ingroup formats + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" +#include "asterisk/endian.h" + +#define BUF_SIZE 640 /* 640 bytes, 320 samples */ +#define SLIN_SAMPLES 320 + +static struct ast_frame *slinear_read(struct ast_filestream *s, int *whennext) +{ + int res; + /* Send a frame from the file to the appropriate channel */ + + s->fr.frametype = AST_FRAME_VOICE; + s->fr.subclass = AST_FORMAT_SLINEAR16; + s->fr.mallocd = 0; + AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE); + if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) < 1) { + if (res) + ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); + return NULL; + } + *whennext = s->fr.samples = res/2; + s->fr.datalen = res; + return &s->fr; +} + +static int slinear_write(struct ast_filestream *fs, struct ast_frame *f) +{ + int res; + + if (f->frametype != AST_FRAME_VOICE) { + ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); + return -1; + } + if (f->subclass != AST_FORMAT_SLINEAR16) { + ast_log(LOG_WARNING, "Asked to write non-slinear16 frame (%d)!\n", f->subclass); + return -1; + } + if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen) { + ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); + return -1; + } + return 0; +} + +static int slinear_seek(struct ast_filestream *fs, off_t sample_offset, int whence) +{ + off_t offset = 0, min = 0, cur, max; + + sample_offset <<= 1; + + cur = ftello(fs->f); + + fseeko(fs->f, 0, SEEK_END); + + max = ftello(fs->f); + + if (whence == SEEK_SET) + offset = sample_offset; + else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) + offset = sample_offset + cur; + else if (whence == SEEK_END) + offset = max - sample_offset; + + if (whence != SEEK_FORCECUR) + offset = (offset > max) ? max : offset; + + /* always protect against seeking past begining. */ + offset = (offset < min) ? min : offset; + + return fseeko(fs->f, offset, SEEK_SET); +} + +static int slinear_trunc(struct ast_filestream *fs) +{ + return ftruncate(fileno(fs->f), ftello(fs->f)); +} + +static off_t slinear_tell(struct ast_filestream *fs) +{ + return ftello(fs->f) / 2; +} + +static const struct ast_format slin_f = { + .name = "sln16", + .exts = "sln16", + .format = AST_FORMAT_SLINEAR16, + .write = slinear_write, + .seek = slinear_seek, + .trunc = slinear_trunc, + .tell = slinear_tell, + .read = slinear_read, + .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, +}; + +static int load_module(void) +{ + if (ast_format_register(&slin_f)) + return AST_MODULE_LOAD_FAILURE; + + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + return ast_format_unregister(slin_f.name); +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Raw Signed Linear 16KHz Audio support (SLN16)"); diff --git a/trunk/formats/format_vox.c b/trunk/formats/format_vox.c new file mode 100644 index 000000000..f22b4881a --- /dev/null +++ b/trunk/formats/format_vox.c @@ -0,0 +1,135 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 1999 - 2005, Digium, Inc. + * + * Mark Spencer <markster@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief Flat, binary, ADPCM vox file format. + * \arg File name extensions: vox + * + * \ingroup formats + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" +#include "asterisk/endian.h" + +#define BUF_SIZE 80 /* 80 bytes, 160 samples */ +#define VOX_SAMPLES 160 + +static struct ast_frame *vox_read(struct ast_filestream *s, int *whennext) +{ + int res; + + /* Send a frame from the file to the appropriate channel */ + s->fr.frametype = AST_FRAME_VOICE; + s->fr.subclass = AST_FORMAT_ADPCM; + s->fr.mallocd = 0; + AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, BUF_SIZE); + if ((res = fread(s->fr.data, 1, s->fr.datalen, s->f)) < 1) { + if (res) + ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); + return NULL; + } + *whennext = s->fr.samples = res * 2; + s->fr.datalen = res; + return &s->fr; +} + +static int vox_write(struct ast_filestream *s, struct ast_frame *f) +{ + int res; + if (f->frametype != AST_FRAME_VOICE) { + ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); + return -1; + } + if (f->subclass != AST_FORMAT_ADPCM) { + ast_log(LOG_WARNING, "Asked to write non-ADPCM frame (%d)!\n", f->subclass); + return -1; + } + if ((res = fwrite(f->data, 1, f->datalen, s->f)) != f->datalen) { + ast_log(LOG_WARNING, "Bad write (%d/%d): %s\n", res, f->datalen, strerror(errno)); + return -1; + } + return 0; +} + +static int vox_seek(struct ast_filestream *fs, off_t sample_offset, int whence) +{ + off_t offset=0,min,cur,max,distance; + + min = 0; + cur = ftello(fs->f); + fseeko(fs->f, 0, SEEK_END); + max = ftello(fs->f); + + /* have to fudge to frame here, so not fully to sample */ + distance = sample_offset/2; + if(whence == SEEK_SET) + offset = distance; + else if(whence == SEEK_CUR || whence == SEEK_FORCECUR) + offset = distance + cur; + else if(whence == SEEK_END) + offset = max - distance; + if (whence != SEEK_FORCECUR) { + offset = (offset > max)?max:offset; + offset = (offset < min)?min:offset; + } + return fseeko(fs->f, offset, SEEK_SET); +} + +static int vox_trunc(struct ast_filestream *fs) +{ + return ftruncate(fileno(fs->f), ftello(fs->f)); +} + +static off_t vox_tell(struct ast_filestream *fs) +{ + off_t offset; + offset = ftello(fs->f) << 1; + return offset; +} + +static const struct ast_format vox_f = { + .name = "vox", + .exts = "vox", + .format = AST_FORMAT_ADPCM, + .write = vox_write, + .seek = vox_seek, + .trunc = vox_trunc, + .tell = vox_tell, + .read = vox_read, + .buf_size = BUF_SIZE + AST_FRIENDLY_OFFSET, +}; + +static int load_module(void) +{ + if (ast_format_register(&vox_f)) + return AST_MODULE_LOAD_FAILURE; + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + return ast_format_unregister(vox_f.name); +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Dialogic VOX (ADPCM) File Format"); diff --git a/trunk/formats/format_wav.c b/trunk/formats/format_wav.c new file mode 100644 index 000000000..2a40dedbd --- /dev/null +++ b/trunk/formats/format_wav.c @@ -0,0 +1,491 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 1999 - 2005, Digium, Inc. + * + * Mark Spencer <markster@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief Work with WAV in the proprietary Microsoft format. + * Microsoft WAV format (8000hz Signed Linear) + * \arg File name extension: wav (lower case) + * \ingroup formats + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" +#include "asterisk/endian.h" + +/* Some Ideas for this code came from makewave.c by Jeffrey Chilton */ + +/* Portions of the conversion code are by guido@sienanet.it */ + +#define WAV_BUF_SIZE 320 + +struct wav_desc { /* format-specific parameters */ + int bytes; + int lasttimeout; + int maxlen; + struct timeval last; +}; + +#define BLOCKSIZE 160 + +#if __BYTE_ORDER == __LITTLE_ENDIAN +#define htoll(b) (b) +#define htols(b) (b) +#define ltohl(b) (b) +#define ltohs(b) (b) +#else +#if __BYTE_ORDER == __BIG_ENDIAN +#define htoll(b) \ + (((((b) ) & 0xFF) << 24) | \ + ((((b) >> 8) & 0xFF) << 16) | \ + ((((b) >> 16) & 0xFF) << 8) | \ + ((((b) >> 24) & 0xFF) )) +#define htols(b) \ + (((((b) ) & 0xFF) << 8) | \ + ((((b) >> 8) & 0xFF) )) +#define ltohl(b) htoll(b) +#define ltohs(b) htols(b) +#else +#error "Endianess not defined" +#endif +#endif + + +static int check_header(FILE *f) +{ + int type, size, formtype; + int fmt, hsize; + short format, chans, bysam, bisam; + int bysec; + int freq; + int data; + if (fread(&type, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (type)\n"); + return -1; + } + if (fread(&size, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (size)\n"); + return -1; + } + size = ltohl(size); + if (fread(&formtype, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (formtype)\n"); + return -1; + } + if (memcmp(&type, "RIFF", 4)) { + ast_log(LOG_WARNING, "Does not begin with RIFF\n"); + return -1; + } + if (memcmp(&formtype, "WAVE", 4)) { + ast_log(LOG_WARNING, "Does not contain WAVE\n"); + return -1; + } + if (fread(&fmt, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (fmt)\n"); + return -1; + } + if (memcmp(&fmt, "fmt ", 4)) { + ast_log(LOG_WARNING, "Does not say fmt\n"); + return -1; + } + if (fread(&hsize, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (formtype)\n"); + return -1; + } + if (ltohl(hsize) < 16) { + ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize)); + return -1; + } + if (fread(&format, 1, 2, f) != 2) { + ast_log(LOG_WARNING, "Read failed (format)\n"); + return -1; + } + if (ltohs(format) != 1) { + ast_log(LOG_WARNING, "Not a wav file %d\n", ltohs(format)); + return -1; + } + if (fread(&chans, 1, 2, f) != 2) { + ast_log(LOG_WARNING, "Read failed (format)\n"); + return -1; + } + if (ltohs(chans) != 1) { + ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans)); + return -1; + } + if (fread(&freq, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (freq)\n"); + return -1; + } + if (ltohl(freq) != DEFAULT_SAMPLE_RATE) { + ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq)); + return -1; + } + /* Ignore the byte frequency */ + if (fread(&bysec, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (BYTES_PER_SECOND)\n"); + return -1; + } + /* Check bytes per sample */ + if (fread(&bysam, 1, 2, f) != 2) { + ast_log(LOG_WARNING, "Read failed (BYTES_PER_SAMPLE)\n"); + return -1; + } + if (ltohs(bysam) != 2) { + ast_log(LOG_WARNING, "Can only handle 16bits per sample: %d\n", ltohs(bysam)); + return -1; + } + if (fread(&bisam, 1, 2, f) != 2) { + ast_log(LOG_WARNING, "Read failed (Bits Per Sample): %d\n", ltohs(bisam)); + return -1; + } + /* Skip any additional header */ + if (fseek(f,ltohl(hsize)-16,SEEK_CUR) == -1 ) { + ast_log(LOG_WARNING, "Failed to skip remaining header bytes: %d\n", ltohl(hsize)-16 ); + return -1; + } + /* Skip any facts and get the first data block */ + for(;;) + { + char buf[4]; + + /* Begin data chunk */ + if (fread(&buf, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (data)\n"); + return -1; + } + /* Data has the actual length of data in it */ + if (fread(&data, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (data)\n"); + return -1; + } + data = ltohl(data); + if(memcmp(buf, "data", 4) == 0 ) + break; + if(memcmp(buf, "fact", 4) != 0 ) { + ast_log(LOG_WARNING, "Unknown block - not fact or data\n"); + return -1; + } + if (fseek(f,data,SEEK_CUR) == -1 ) { + ast_log(LOG_WARNING, "Failed to skip fact block: %d\n", data ); + return -1; + } + } +#if 0 + curpos = lseek(fd, 0, SEEK_CUR); + truelength = lseek(fd, 0, SEEK_END); + lseek(fd, curpos, SEEK_SET); + truelength -= curpos; +#endif + return data; +} + +static int update_header(FILE *f) +{ + off_t cur,end; + int datalen,filelen,bytes; + + cur = ftello(f); + fseek(f, 0, SEEK_END); + end = ftello(f); + /* data starts 44 bytes in */ + bytes = end - 44; + datalen = htoll(bytes); + /* chunk size is bytes of data plus 36 bytes of header */ + filelen = htoll(36 + bytes); + + if (cur < 0) { + ast_log(LOG_WARNING, "Unable to find our position\n"); + return -1; + } + if (fseek(f, 4, SEEK_SET)) { + ast_log(LOG_WARNING, "Unable to set our position\n"); + return -1; + } + if (fwrite(&filelen, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to set write file size\n"); + return -1; + } + if (fseek(f, 40, SEEK_SET)) { + ast_log(LOG_WARNING, "Unable to set our position\n"); + return -1; + } + if (fwrite(&datalen, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to set write datalen\n"); + return -1; + } + if (fseeko(f, cur, SEEK_SET)) { + ast_log(LOG_WARNING, "Unable to return to position\n"); + return -1; + } + return 0; +} + +static int write_header(FILE *f) +{ + unsigned int hz=htoll(8000); + unsigned int bhz = htoll(16000); + unsigned int hs = htoll(16); + unsigned short fmt = htols(1); + unsigned short chans = htols(1); + unsigned short bysam = htols(2); + unsigned short bisam = htols(16); + unsigned int size = htoll(0); + /* Write a wav header, ignoring sizes which will be filled in later */ + fseek(f,0,SEEK_SET); + if (fwrite("RIFF", 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + if (fwrite(&size, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + if (fwrite("WAVEfmt ", 1, 8, f) != 8) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + if (fwrite(&hs, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + if (fwrite(&fmt, 1, 2, f) != 2) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + if (fwrite(&chans, 1, 2, f) != 2) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + if (fwrite(&hz, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + if (fwrite(&bhz, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + if (fwrite(&bysam, 1, 2, f) != 2) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + if (fwrite(&bisam, 1, 2, f) != 2) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + if (fwrite("data", 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + if (fwrite(&size, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + return 0; +} + +static int wav_open(struct ast_filestream *s) +{ + /* We don't have any header to read or anything really, but + if we did, it would go here. We also might want to check + and be sure it's a valid file. */ + struct wav_desc *tmp = (struct wav_desc *)s->_private; + if ((tmp->maxlen = check_header(s->f)) < 0) + return -1; + return 0; +} + +static int wav_rewrite(struct ast_filestream *s, const char *comment) +{ + /* We don't have any header to read or anything really, but + if we did, it would go here. We also might want to check + and be sure it's a valid file. */ + + if (write_header(s->f)) + return -1; + return 0; +} + +static void wav_close(struct ast_filestream *s) +{ + char zero = 0; + struct wav_desc *fs = (struct wav_desc *)s->_private; + /* Pad to even length */ + if (fs->bytes & 0x1) + fwrite(&zero, 1, 1, s->f); +} + +static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext) +{ + int res; + int samples; /* actual samples read */ +#if __BYTE_ORDER == __BIG_ENDIAN + int x; +#endif + short *tmp; + int bytes = WAV_BUF_SIZE; /* in bytes */ + off_t here; + /* Send a frame from the file to the appropriate channel */ + struct wav_desc *fs = (struct wav_desc *)s->_private; + + here = ftello(s->f); + if (fs->maxlen - here < bytes) /* truncate if necessary */ + bytes = fs->maxlen - here; + if (bytes < 0) + bytes = 0; +/* ast_debug(1, "here: %d, maxlen: %d, bytes: %d\n", here, s->maxlen, bytes); */ + s->fr.frametype = AST_FRAME_VOICE; + s->fr.subclass = AST_FORMAT_SLINEAR; + s->fr.mallocd = 0; + AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, bytes); + + if ( (res = fread(s->fr.data, 1, s->fr.datalen, s->f)) <= 0 ) { + if (res) + ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); + return NULL; + } + s->fr.datalen = res; + s->fr.samples = samples = res / 2; + + tmp = (short *)(s->fr.data); +#if __BYTE_ORDER == __BIG_ENDIAN + /* file format is little endian so we need to swap */ + for( x = 0; x < samples; x++) + tmp[x] = (tmp[x] << 8) | ((tmp[x] & 0xff00) >> 8); +#endif + + *whennext = samples; + return &s->fr; +} + +static int wav_write(struct ast_filestream *fs, struct ast_frame *f) +{ +#if __BYTE_ORDER == __BIG_ENDIAN + int x; + short tmp[8000], *tmpi; +#endif + struct wav_desc *s = (struct wav_desc *)fs->_private; + int res; + + if (f->frametype != AST_FRAME_VOICE) { + ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); + return -1; + } + if (f->subclass != AST_FORMAT_SLINEAR) { + ast_log(LOG_WARNING, "Asked to write non-SLINEAR frame (%d)!\n", f->subclass); + return -1; + } + if (!f->datalen) + return -1; + +#if __BYTE_ORDER == __BIG_ENDIAN + /* swap and write */ + if (f->datalen > sizeof(tmp)) { + ast_log(LOG_WARNING, "Data length is too long\n"); + return -1; + } + tmpi = f->data; + for (x=0; x < f->datalen/2; x++) + tmp[x] = (tmpi[x] << 8) | ((tmpi[x] & 0xff00) >> 8); + + if ((res = fwrite(tmp, 1, f->datalen, fs->f)) != f->datalen ) { +#else + /* just write */ + if ((res = fwrite(f->data, 1, f->datalen, fs->f)) != f->datalen ) { +#endif + ast_log(LOG_WARNING, "Bad write (%d): %s\n", res, strerror(errno)); + return -1; + } + + s->bytes += f->datalen; + update_header(fs->f); + + return 0; + +} + +static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence) +{ + off_t min, max, cur, offset = 0, samples; + + samples = sample_offset * 2; /* SLINEAR is 16 bits mono, so sample_offset * 2 = bytes */ + min = 44; /* wav header is 44 bytes */ + cur = ftello(fs->f); + fseeko(fs->f, 0, SEEK_END); + max = ftello(fs->f); + if (whence == SEEK_SET) + offset = samples + min; + else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) + offset = samples + cur; + else if (whence == SEEK_END) + offset = max - samples; + if (whence != SEEK_FORCECUR) { + offset = (offset > max)?max:offset; + } + /* always protect the header space. */ + offset = (offset < min)?min:offset; + return fseeko(fs->f, offset, SEEK_SET); +} + +static int wav_trunc(struct ast_filestream *fs) +{ + if (ftruncate(fileno(fs->f), ftello(fs->f))) + return -1; + return update_header(fs->f); +} + +static off_t wav_tell(struct ast_filestream *fs) +{ + off_t offset; + offset = ftello(fs->f); + /* subtract header size to get samples, then divide by 2 for 16 bit samples */ + return (offset - 44)/2; +} + +static const struct ast_format wav_f = { + .name = "wav", + .exts = "wav", + .format = AST_FORMAT_SLINEAR, + .open = wav_open, + .rewrite = wav_rewrite, + .write = wav_write, + .seek = wav_seek, + .trunc = wav_trunc, + .tell = wav_tell, + .read = wav_read, + .close = wav_close, + .buf_size = WAV_BUF_SIZE + AST_FRIENDLY_OFFSET, + .desc_size = sizeof(struct wav_desc), +}; + +static int load_module(void) +{ + if (ast_format_register(&wav_f)) + return AST_MODULE_LOAD_FAILURE; + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + return ast_format_unregister(wav_f.name); +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Microsoft WAV format (8000Hz Signed Linear)"); diff --git a/trunk/formats/format_wav_gsm.c b/trunk/formats/format_wav_gsm.c new file mode 100644 index 000000000..cf40560de --- /dev/null +++ b/trunk/formats/format_wav_gsm.c @@ -0,0 +1,559 @@ +/* + * Asterisk -- An open source telephony toolkit. + * + * Copyright (C) 1999 - 2005, Digium, Inc. + * + * Mark Spencer <markster@digium.com> + * + * See http://www.asterisk.org for more information about + * the Asterisk project. Please do not directly contact + * any of the maintainers of this project for assistance; + * the project provides a web site, mailing lists and IRC + * channels for your use. + * + * This program is free software, distributed under the terms of + * the GNU General Public License Version 2. See the LICENSE file + * at the top of the source tree. + */ + +/*! \file + * + * \brief Save GSM in the proprietary Microsoft format. + * + * Microsoft WAV format (Proprietary GSM) + * \arg File name extension: WAV,wav49 (Upper case WAV, lower case is another format) + * This format can be played on Windows systems, used for + * e-mail attachments mainly. + * \ingroup formats + */ + +#include "asterisk.h" + +ASTERISK_FILE_VERSION(__FILE__, "$Revision$") + +#include "asterisk/mod_format.h" +#include "asterisk/module.h" +#include "asterisk/endian.h" + +#include "msgsm.h" + +/* Some Ideas for this code came from makewave.c by Jeffrey Chilton */ + +/* Portions of the conversion code are by guido@sienanet.it */ + +#define GSM_FRAME_SIZE 33 +#define MSGSM_FRAME_SIZE 65 +#define MSGSM_DATA_OFFSET 60 /* offset of data bytes */ +#define GSM_SAMPLES 160 /* samples in a GSM block */ +#define MSGSM_SAMPLES (2*GSM_SAMPLES) /* samples in an MSGSM block */ + +/* begin binary data: */ +char msgsm_silence[] = /* 65 */ +{0x48,0x17,0xD6,0x84,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49 +,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92,0x24,0x89,0x02,0x80,0x24,0x49,0x92 +,0x24,0x09,0x82,0x74,0x61,0x4D,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00 +,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48,0x92,0x24,0x49,0x92,0x28,0x00,0x48 +,0x92,0x24,0x49,0x92,0x00}; +/* end binary data. size = 65 bytes */ + +struct wavg_desc { + /* Believe it or not, we must decode/recode to account for the + weird MS format */ + int secondhalf; /* Are we on the second half */ +}; + +#if __BYTE_ORDER == __LITTLE_ENDIAN +#define htoll(b) (b) +#define htols(b) (b) +#define ltohl(b) (b) +#define ltohs(b) (b) +#else +#if __BYTE_ORDER == __BIG_ENDIAN +#define htoll(b) \ + (((((b) ) & 0xFF) << 24) | \ + ((((b) >> 8) & 0xFF) << 16) | \ + ((((b) >> 16) & 0xFF) << 8) | \ + ((((b) >> 24) & 0xFF) )) +#define htols(b) \ + (((((b) ) & 0xFF) << 8) | \ + ((((b) >> 8) & 0xFF) )) +#define ltohl(b) htoll(b) +#define ltohs(b) htols(b) +#else +#error "Endianess not defined" +#endif +#endif + + +static int check_header(FILE *f) +{ + int type, size, formtype; + int fmt, hsize, fact; + short format, chans; + int freq; + int data; + if (fread(&type, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (type)\n"); + return -1; + } + if (fread(&size, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (size)\n"); + return -1; + } + size = ltohl(size); + if (fread(&formtype, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (formtype)\n"); + return -1; + } + if (memcmp(&type, "RIFF", 4)) { + ast_log(LOG_WARNING, "Does not begin with RIFF\n"); + return -1; + } + if (memcmp(&formtype, "WAVE", 4)) { + ast_log(LOG_WARNING, "Does not contain WAVE\n"); + return -1; + } + if (fread(&fmt, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (fmt)\n"); + return -1; + } + if (memcmp(&fmt, "fmt ", 4)) { + ast_log(LOG_WARNING, "Does not say fmt\n"); + return -1; + } + if (fread(&hsize, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (formtype)\n"); + return -1; + } + if (ltohl(hsize) != 20) { + ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize)); + return -1; + } + if (fread(&format, 1, 2, f) != 2) { + ast_log(LOG_WARNING, "Read failed (format)\n"); + return -1; + } + if (ltohs(format) != 49) { + ast_log(LOG_WARNING, "Not a GSM file %d\n", ltohs(format)); + return -1; + } + if (fread(&chans, 1, 2, f) != 2) { + ast_log(LOG_WARNING, "Read failed (format)\n"); + return -1; + } + if (ltohs(chans) != 1) { + ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans)); + return -1; + } + if (fread(&freq, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (freq)\n"); + return -1; + } + if (ltohl(freq) != DEFAULT_SAMPLE_RATE) { + ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq)); + return -1; + } + /* Ignore the byte frequency */ + if (fread(&freq, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (X_1)\n"); + return -1; + } + /* Ignore the two weird fields */ + if (fread(&freq, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (X_2/X_3)\n"); + return -1; + } + /* Ignore the byte frequency */ + if (fread(&freq, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (Y_1)\n"); + return -1; + } + /* Check for the word fact */ + if (fread(&fact, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (fact)\n"); + return -1; + } + if (memcmp(&fact, "fact", 4)) { + ast_log(LOG_WARNING, "Does not say fact\n"); + return -1; + } + /* Ignore the "fact value" */ + if (fread(&fact, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (fact header)\n"); + return -1; + } + if (fread(&fact, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (fact value)\n"); + return -1; + } + /* Check for the word data */ + if (fread(&data, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (data)\n"); + return -1; + } + if (memcmp(&data, "data", 4)) { + ast_log(LOG_WARNING, "Does not say data\n"); + return -1; + } + /* Ignore the data length */ + if (fread(&data, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Read failed (data)\n"); + return -1; + } + return 0; +} + +static int update_header(FILE *f) +{ + off_t cur,end,bytes; + int datalen, filelen, samples; + + cur = ftello(f); + fseek(f, 0, SEEK_END); + end = ftello(f); + /* in a gsm WAV, data starts 60 bytes in */ + bytes = end - MSGSM_DATA_OFFSET; + samples = htoll(bytes / MSGSM_FRAME_SIZE * MSGSM_SAMPLES); + datalen = htoll((bytes + 1) & ~0x1); + filelen = htoll(MSGSM_DATA_OFFSET - 8 + ((bytes + 1) & ~0x1)); + if (cur < 0) { + ast_log(LOG_WARNING, "Unable to find our position\n"); + return -1; + } + if (fseek(f, 4, SEEK_SET)) { + ast_log(LOG_WARNING, "Unable to set our position\n"); + return -1; + } + if (fwrite(&filelen, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write file size\n"); + return -1; + } + if (fseek(f, 48, SEEK_SET)) { + ast_log(LOG_WARNING, "Unable to set our position\n"); + return -1; + } + if (fwrite(&samples, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write samples\n"); + return -1; + } + if (fseek(f, 56, SEEK_SET)) { + ast_log(LOG_WARNING, "Unable to set our position\n"); + return -1; + } + if (fwrite(&datalen, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write datalen\n"); + return -1; + } + if (fseeko(f, cur, SEEK_SET)) { + ast_log(LOG_WARNING, "Unable to return to position\n"); + return -1; + } + return 0; +} + +static int write_header(FILE *f) +{ + /* Samples per second (always 8000 for this format). */ + unsigned int sample_rate = htoll(8000); + /* Bytes per second (always 1625 for this format). */ + unsigned int byte_sample_rate = htoll(1625); + /* This is the size of the "fmt " subchunk */ + unsigned int fmtsize = htoll(20); + /* WAV #49 */ + unsigned short fmt = htols(49); + /* Mono = 1 channel */ + unsigned short chans = htols(1); + /* Each block of data is exactly 65 bytes in size. */ + unsigned int block_align = htoll(MSGSM_FRAME_SIZE); + /* Not actually 2, but rounded up to the nearest bit */ + unsigned short bits_per_sample = htols(2); + /* Needed for compressed formats */ + unsigned short extra_format = htols(MSGSM_SAMPLES); + /* This is the size of the "fact" subchunk */ + unsigned int factsize = htoll(4); + /* Number of samples in the data chunk */ + unsigned int num_samples = htoll(0); + /* Number of bytes in the data chunk */ + unsigned int size = htoll(0); + /* Write a GSM header, ignoring sizes which will be filled in later */ + + /* 0: Chunk ID */ + if (fwrite("RIFF", 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 4: Chunk Size */ + if (fwrite(&size, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 8: Chunk Format */ + if (fwrite("WAVE", 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 12: Subchunk 1: ID */ + if (fwrite("fmt ", 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 16: Subchunk 1: Size (minus 8) */ + if (fwrite(&fmtsize, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 20: Subchunk 1: Audio format (49) */ + if (fwrite(&fmt, 1, 2, f) != 2) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 22: Subchunk 1: Number of channels */ + if (fwrite(&chans, 1, 2, f) != 2) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 24: Subchunk 1: Sample rate */ + if (fwrite(&sample_rate, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 28: Subchunk 1: Byte rate */ + if (fwrite(&byte_sample_rate, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 32: Subchunk 1: Block align */ + if (fwrite(&block_align, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 36: Subchunk 1: Bits per sample */ + if (fwrite(&bits_per_sample, 1, 2, f) != 2) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 38: Subchunk 1: Extra format bytes */ + if (fwrite(&extra_format, 1, 2, f) != 2) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 40: Subchunk 2: ID */ + if (fwrite("fact", 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 44: Subchunk 2: Size (minus 8) */ + if (fwrite(&factsize, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 48: Subchunk 2: Number of samples */ + if (fwrite(&num_samples, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 52: Subchunk 3: ID */ + if (fwrite("data", 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + /* 56: Subchunk 3: Size */ + if (fwrite(&size, 1, 4, f) != 4) { + ast_log(LOG_WARNING, "Unable to write header\n"); + return -1; + } + return 0; +} + +static int wav_open(struct ast_filestream *s) +{ + /* We don't have any header to read or anything really, but + if we did, it would go here. We also might want to check + and be sure it's a valid file. */ + struct wavg_desc *fs = (struct wavg_desc *)s->_private; + + if (check_header(s->f)) + return -1; + fs->secondhalf = 0; /* not strictly necessary */ + return 0; +} + +static int wav_rewrite(struct ast_filestream *s, const char *comment) +{ + /* We don't have any header to read or anything really, but + if we did, it would go here. We also might want to check + and be sure it's a valid file. */ + + if (write_header(s->f)) + return -1; + return 0; +} + +static void wav_close(struct ast_filestream *s) +{ + char zero = 0; + /* Pad to even length */ + fseek(s->f, 0, SEEK_END); + if (ftello(s->f) & 0x1) + fwrite(&zero, 1, 1, s->f); +} + +static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext) +{ + /* Send a frame from the file to the appropriate channel */ + struct wavg_desc *fs = (struct wavg_desc *)s->_private; + + s->fr.frametype = AST_FRAME_VOICE; + s->fr.subclass = AST_FORMAT_GSM; + s->fr.offset = AST_FRIENDLY_OFFSET; + s->fr.samples = GSM_SAMPLES; + s->fr.mallocd = 0; + AST_FRAME_SET_BUFFER(&s->fr, s->buf, AST_FRIENDLY_OFFSET, GSM_FRAME_SIZE); + if (fs->secondhalf) { + /* Just return a frame based on the second GSM frame */ + s->fr.data = (char *)s->fr.data + GSM_FRAME_SIZE; + s->fr.offset += GSM_FRAME_SIZE; + } else { + /* read and convert */ + unsigned char msdata[MSGSM_FRAME_SIZE]; + int res; + + if ((res = fread(msdata, 1, MSGSM_FRAME_SIZE, s->f)) != MSGSM_FRAME_SIZE) { + if (res && (res != 1)) + ast_log(LOG_WARNING, "Short read (%d) (%s)!\n", res, strerror(errno)); + return NULL; + } + /* Convert from MS format to two real GSM frames */ + conv65(msdata, s->fr.data); + } + fs->secondhalf = !fs->secondhalf; + *whennext = GSM_SAMPLES; + return &s->fr; +} + +static int wav_write(struct ast_filestream *s, struct ast_frame *f) +{ + int len; + int size; + struct wavg_desc *fs = (struct wavg_desc *)s->_private; + + if (f->frametype != AST_FRAME_VOICE) { + ast_log(LOG_WARNING, "Asked to write non-voice frame!\n"); + return -1; + } + if (f->subclass != AST_FORMAT_GSM) { + ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass); + return -1; + } + /* XXX this might fail... if the input is a multiple of MSGSM_FRAME_SIZE + * we assume it is already in the correct format. + */ + if (!(f->datalen % MSGSM_FRAME_SIZE)) { + size = MSGSM_FRAME_SIZE; + fs->secondhalf = 0; + } else { + size = GSM_FRAME_SIZE; + } + for (len = 0; len < f->datalen ; len += size) { + int res; + unsigned char *src, msdata[MSGSM_FRAME_SIZE]; + if (fs->secondhalf) { /* second half of raw gsm to be converted */ + memcpy(s->buf + GSM_FRAME_SIZE, f->data + len, GSM_FRAME_SIZE); + conv66((unsigned char *) s->buf, msdata); + src = msdata; + fs->secondhalf = 0; + } else if (size == GSM_FRAME_SIZE) { /* first half of raw gsm */ + memcpy(s->buf, f->data + len, GSM_FRAME_SIZE); + src = NULL; /* nothing to write */ + fs->secondhalf = 1; + } else { /* raw msgsm data */ + src = f->data + len; + } + if (src && (res = fwrite(src, 1, MSGSM_FRAME_SIZE, s->f)) != MSGSM_FRAME_SIZE) { + ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno)); + return -1; + } + update_header(s->f); /* XXX inefficient! */ + } + return 0; +} + +static int wav_seek(struct ast_filestream *fs, off_t sample_offset, int whence) +{ + off_t offset=0, distance, max; + struct wavg_desc *s = (struct wavg_desc *)fs->_private; + + off_t min = MSGSM_DATA_OFFSET; + off_t cur = ftello(fs->f); + fseek(fs->f, 0, SEEK_END); + max = ftello(fs->f); /* XXX ideally, should round correctly */ + /* Compute the distance in bytes, rounded to the block size */ + distance = (sample_offset/MSGSM_SAMPLES) * MSGSM_FRAME_SIZE; + if (whence == SEEK_SET) + offset = distance + min; + else if (whence == SEEK_CUR || whence == SEEK_FORCECUR) + offset = distance + cur; + else if (whence == SEEK_END) + offset = max - distance; + /* always protect against seeking past end of header */ + if (offset < min) + offset = min; + if (whence != SEEK_FORCECUR) { + if (offset > max) + offset = max; + } else if (offset > max) { + int i; + fseek(fs->f, 0, SEEK_END); + for (i=0; i< (offset - max) / MSGSM_FRAME_SIZE; i++) { + fwrite(msgsm_silence, 1, MSGSM_FRAME_SIZE, fs->f); + } + } + s->secondhalf = 0; + return fseeko(fs->f, offset, SEEK_SET); +} + +static int wav_trunc(struct ast_filestream *fs) +{ + if (ftruncate(fileno(fs->f), ftello(fs->f))) + return -1; + return update_header(fs->f); +} + +static off_t wav_tell(struct ast_filestream *fs) +{ + off_t offset; + offset = ftello(fs->f); + /* since this will most likely be used later in play or record, lets stick + * to that level of resolution, just even frames boundaries */ + return (offset - MSGSM_DATA_OFFSET)/MSGSM_FRAME_SIZE*MSGSM_SAMPLES; +} + +static const struct ast_format wav49_f = { + .name = "wav49", + .exts = "WAV|wav49", + .format = AST_FORMAT_GSM, + .open = wav_open, + .rewrite = wav_rewrite, + .write = wav_write, + .seek = wav_seek, + .trunc = wav_trunc, + .tell = wav_tell, + .read = wav_read, + .close = wav_close, + .buf_size = 2*GSM_FRAME_SIZE + AST_FRIENDLY_OFFSET, + .desc_size = sizeof(struct wavg_desc), +}; + +static int load_module(void) +{ + if (ast_format_register(&wav49_f)) + return AST_MODULE_LOAD_FAILURE; + return AST_MODULE_LOAD_SUCCESS; +} + +static int unload_module(void) +{ + return ast_format_unregister(wav49_f.name); +} + +AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Microsoft WAV format (Proprietary GSM)"); diff --git a/trunk/formats/msgsm.h b/trunk/formats/msgsm.h new file mode 100644 index 000000000..f951cc271 --- /dev/null +++ b/trunk/formats/msgsm.h @@ -0,0 +1,689 @@ +/* Conversion routines derived from code by guido@sienanet.it */ + +#define GSM_MAGIC 0xD + +#ifndef GSM_H +typedef unsigned char gsm_byte; +#endif +typedef unsigned char wav_byte; +typedef unsigned int uword; + +#define readGSM_33(c1) { \ + gsm_byte *c = (c1); \ + LARc[0] = (*c++ & 0xF) << 2; /* 1 */ \ + LARc[0] |= (*c >> 6) & 0x3; \ + LARc[1] = *c++ & 0x3F; \ + LARc[2] = (*c >> 3) & 0x1F; \ + LARc[3] = (*c++ & 0x7) << 2; \ + LARc[3] |= (*c >> 6) & 0x3; \ + LARc[4] = (*c >> 2) & 0xF; \ + LARc[5] = (*c++ & 0x3) << 2; \ + LARc[5] |= (*c >> 6) & 0x3; \ + LARc[6] = (*c >> 3) & 0x7; \ + LARc[7] = *c++ & 0x7; \ + Nc[0] = (*c >> 1) & 0x7F; \ + bc[0] = (*c++ & 0x1) << 1; \ + bc[0] |= (*c >> 7) & 0x1; \ + Mc[0] = (*c >> 5) & 0x3; \ + xmaxc[0] = (*c++ & 0x1F) << 1; \ + xmaxc[0] |= (*c >> 7) & 0x1; \ + xmc[0] = (*c >> 4) & 0x7; \ + xmc[1] = (*c >> 1) & 0x7; \ + xmc[2] = (*c++ & 0x1) << 2; \ + xmc[2] |= (*c >> 6) & 0x3; \ + xmc[3] = (*c >> 3) & 0x7; \ + xmc[4] = *c++ & 0x7; \ + xmc[5] = (*c >> 5) & 0x7; \ + xmc[6] = (*c >> 2) & 0x7; \ + xmc[7] = (*c++ & 0x3) << 1; /* 10 */ \ + xmc[7] |= (*c >> 7) & 0x1; \ + xmc[8] = (*c >> 4) & 0x7; \ + xmc[9] = (*c >> 1) & 0x7; \ + xmc[10] = (*c++ & 0x1) << 2; \ + xmc[10] |= (*c >> 6) & 0x3; \ + xmc[11] = (*c >> 3) & 0x7; \ + xmc[12] = *c++ & 0x7; \ + Nc[1] = (*c >> 1) & 0x7F; \ + bc[1] = (*c++ & 0x1) << 1; \ + bc[1] |= (*c >> 7) & 0x1; \ + Mc[1] = (*c >> 5) & 0x3; \ + xmaxc[1] = (*c++ & 0x1F) << 1; \ + xmaxc[1] |= (*c >> 7) & 0x1; \ + xmc[13] = (*c >> 4) & 0x7; \ + xmc[14] = (*c >> 1) & 0x7; \ + xmc[15] = (*c++ & 0x1) << 2; \ + xmc[15] |= (*c >> 6) & 0x3; \ + xmc[16] = (*c >> 3) & 0x7; \ + xmc[17] = *c++ & 0x7; \ + xmc[18] = (*c >> 5) & 0x7; \ + xmc[19] = (*c >> 2) & 0x7; \ + xmc[20] = (*c++ & 0x3) << 1; \ + xmc[20] |= (*c >> 7) & 0x1; \ + xmc[21] = (*c >> 4) & 0x7; \ + xmc[22] = (*c >> 1) & 0x7; \ + xmc[23] = (*c++ & 0x1) << 2; \ + xmc[23] |= (*c >> 6) & 0x3; \ + xmc[24] = (*c >> 3) & 0x7; \ + xmc[25] = *c++ & 0x7; \ + Nc[2] = (*c >> 1) & 0x7F; \ + bc[2] = (*c++ & 0x1) << 1; /* 20 */ \ + bc[2] |= (*c >> 7) & 0x1; \ + Mc[2] = (*c >> 5) & 0x3; \ + xmaxc[2] = (*c++ & 0x1F) << 1; \ + xmaxc[2] |= (*c >> 7) & 0x1; \ + xmc[26] = (*c >> 4) & 0x7; \ + xmc[27] = (*c >> 1) & 0x7; \ + xmc[28] = (*c++ & 0x1) << 2; \ + xmc[28] |= (*c >> 6) & 0x3; \ + xmc[29] = (*c >> 3) & 0x7; \ + xmc[30] = *c++ & 0x7; \ + xmc[31] = (*c >> 5) & 0x7; \ + xmc[32] = (*c >> 2) & 0x7; \ + xmc[33] = (*c++ & 0x3) << 1; \ + xmc[33] |= (*c >> 7) & 0x1; \ + xmc[34] = (*c >> 4) & 0x7; \ + xmc[35] = (*c >> 1) & 0x7; \ + xmc[36] = (*c++ & 0x1) << 2; \ + xmc[36] |= (*c >> 6) & 0x3; \ + xmc[37] = (*c >> 3) & 0x7; \ + xmc[38] = *c++ & 0x7; \ + Nc[3] = (*c >> 1) & 0x7F; \ + bc[3] = (*c++ & 0x1) << 1; \ + bc[3] |= (*c >> 7) & 0x1; \ + Mc[3] = (*c >> 5) & 0x3; \ + xmaxc[3] = (*c++ & 0x1F) << 1; \ + xmaxc[3] |= (*c >> 7) & 0x1; \ + xmc[39] = (*c >> 4) & 0x7; \ + xmc[40] = (*c >> 1) & 0x7; \ + xmc[41] = (*c++ & 0x1) << 2; \ + xmc[41] |= (*c >> 6) & 0x3; \ + xmc[42] = (*c >> 3) & 0x7; \ + xmc[43] = *c++ & 0x7; /* 30 */ \ + xmc[44] = (*c >> 5) & 0x7; \ + xmc[45] = (*c >> 2) & 0x7; \ + xmc[46] = (*c++ & 0x3) << 1; \ + xmc[46] |= (*c >> 7) & 0x1; \ + xmc[47] = (*c >> 4) & 0x7; \ + xmc[48] = (*c >> 1) & 0x7; \ + xmc[49] = (*c++ & 0x1) << 2; \ + xmc[49] |= (*c >> 6) & 0x3; \ + xmc[50] = (*c >> 3) & 0x7; \ + xmc[51] = *c & 0x7; /* 33 */ \ +} + +static inline void conv66(gsm_byte * d, wav_byte * c) { + gsm_byte frame_chain; + unsigned int sr; + unsigned int LARc[8], Nc[4], Mc[4], bc[4], xmaxc[4], xmc[13*4]; + + readGSM_33(d); + sr = 0; + sr = (sr >> 6) | (LARc[0] << 10); + sr = (sr >> 6) | (LARc[1] << 10); + *c++ = sr >> 4; + sr = (sr >> 5) | (LARc[2] << 11); + *c++ = sr >> 7; + sr = (sr >> 5) | (LARc[3] << 11); + sr = (sr >> 4) | (LARc[4] << 12); + *c++ = sr >> 6; + sr = (sr >> 4) | (LARc[5] << 12); + sr = (sr >> 3) | (LARc[6] << 13); + *c++ = sr >> 7; + sr = (sr >> 3) | (LARc[7] << 13); + sr = (sr >> 7) | (Nc[0] << 9); + *c++ = sr >> 5; + sr = (sr >> 2) | (bc[0] << 14); + sr = (sr >> 2) | (Mc[0] << 14); + sr = (sr >> 6) | (xmaxc[0] << 10); + *c++ = sr >> 3; + sr = (sr >> 3 )|( xmc[0] << 13); + *c++ = sr >> 8; + sr = (sr >> 3 )|( xmc[1] << 13); + sr = (sr >> 3 )|( xmc[2] << 13); + sr = (sr >> 3 )|( xmc[3] << 13); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[4] << 13); + sr = (sr >> 3 )|( xmc[5] << 13); + sr = (sr >> 3 )|( xmc[6] << 13); + *c++ = sr >> 6; + sr = (sr >> 3 )|( xmc[7] << 13); + sr = (sr >> 3 )|( xmc[8] << 13); + *c++ = sr >> 8; + sr = (sr >> 3 )|( xmc[9] << 13); + sr = (sr >> 3 )|( xmc[10] << 13); + sr = (sr >> 3 )|( xmc[11] << 13); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[12] << 13); + sr = (sr >> 7 )|( Nc[1] << 9); + *c++ = sr >> 5; + sr = (sr >> 2 )|( bc[1] << 14); + sr = (sr >> 2 )|( Mc[1] << 14); + sr = (sr >> 6 )|( xmaxc[1] << 10); + *c++ = sr >> 3; + sr = (sr >> 3 )|( xmc[13] << 13); + *c++ = sr >> 8; + sr = (sr >> 3 )|( xmc[14] << 13); + sr = (sr >> 3 )|( xmc[15] << 13); + sr = (sr >> 3 )|( xmc[16] << 13); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[17] << 13); + sr = (sr >> 3 )|( xmc[18] << 13); + sr = (sr >> 3 )|( xmc[19] << 13); + *c++ = sr >> 6; + sr = (sr >> 3 )|( xmc[20] << 13); + sr = (sr >> 3 )|( xmc[21] << 13); + *c++ = sr >> 8; + sr = (sr >> 3 )|( xmc[22] << 13); + sr = (sr >> 3 )|( xmc[23] << 13); + sr = (sr >> 3 )|( xmc[24] << 13); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[25] << 13); + sr = (sr >> 7 )|( Nc[2] << 9); + *c++ = sr >> 5; + sr = (sr >> 2 )|( bc[2] << 14); + sr = (sr >> 2 )|( Mc[2] << 14); + sr = (sr >> 6 )|( xmaxc[2] << 10); + *c++ = sr >> 3; + sr = (sr >> 3 )|( xmc[26] << 13); + *c++ = sr >> 8; + sr = (sr >> 3 )|( xmc[27] << 13); + sr = (sr >> 3 )|( xmc[28] << 13); + sr = (sr >> 3 )|( xmc[29] << 13); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[30] << 13); + sr = (sr >> 3 )|( xmc[31] << 13); + sr = (sr >> 3 )|( xmc[32] << 13); + *c++ = sr >> 6; + sr = (sr >> 3 )|( xmc[33] << 13); + sr = (sr >> 3 )|( xmc[34] << 13); + *c++ = sr >> 8; + sr = (sr >> 3 )|( xmc[35] << 13); + sr = (sr >> 3 )|( xmc[36] << 13); + sr = (sr >> 3 )|( xmc[37] << 13); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[38] << 13); + sr = (sr >> 7 )|( Nc[3] << 9); + *c++ = sr >> 5; + sr = (sr >> 2 )|( bc[3] << 14); + sr = (sr >> 2 )|( Mc[3] << 14); + sr = (sr >> 6 )|( xmaxc[3] << 10); + *c++ = sr >> 3; + sr = (sr >> 3 )|( xmc[39] << 13); + *c++ = sr >> 8; + sr = (sr >> 3 )|( xmc[40] << 13); + sr = (sr >> 3 )|( xmc[41] << 13); + sr = (sr >> 3 )|( xmc[42] << 13); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[43] << 13); + sr = (sr >> 3 )|( xmc[44] << 13); + sr = (sr >> 3 )|( xmc[45] << 13); + *c++ = sr >> 6; + sr = (sr >> 3 )|( xmc[46] << 13); + sr = (sr >> 3 )|( xmc[47] << 13); + *c++ = sr >> 8; + sr = (sr >> 3 )|( xmc[48] << 13); + sr = (sr >> 3 )|( xmc[49] << 13); + sr = (sr >> 3 )|( xmc[50] << 13); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[51] << 13); + sr = sr >> 4; + *c = sr >> 8; + frame_chain = *c; + readGSM_33(d+33); /* puts all the parameters into LARc etc. */ + + + sr = 0; +/* sr = (sr >> 4 )|( s->frame_chain << 12); */ + sr = (sr >> 4 )|( frame_chain << 12); + + sr = (sr >> 6 )|( LARc[0] << 10); + *c++ = sr >> 6; + sr = (sr >> 6 )|( LARc[1] << 10); + *c++ = sr >> 8; + sr = (sr >> 5 )|( LARc[2] << 11); + sr = (sr >> 5 )|( LARc[3] << 11); + *c++ = sr >> 6; + sr = (sr >> 4 )|( LARc[4] << 12); + sr = (sr >> 4 )|( LARc[5] << 12); + *c++ = sr >> 6; + sr = (sr >> 3 )|( LARc[6] << 13); + sr = (sr >> 3 )|( LARc[7] << 13); + *c++ = sr >> 8; + sr = (sr >> 7 )|( Nc[0] << 9); + sr = (sr >> 2 )|( bc[0] << 14); + *c++ = sr >> 7; + sr = (sr >> 2 )|( Mc[0] << 14); + sr = (sr >> 6 )|( xmaxc[0] << 10); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[0] << 13); + sr = (sr >> 3 )|( xmc[1] << 13); + sr = (sr >> 3 )|( xmc[2] << 13); + *c++ = sr >> 6; + sr = (sr >> 3 )|( xmc[3] << 13); + sr = (sr >> 3 )|( xmc[4] << 13); + *c++ = sr >> 8; + sr = (sr >> 3 )|( xmc[5] << 13); + sr = (sr >> 3 )|( xmc[6] << 13); + sr = (sr >> 3 )|( xmc[7] << 13); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[8] << 13); + sr = (sr >> 3 )|( xmc[9] << 13); + sr = (sr >> 3 )|( xmc[10] << 13); + *c++ = sr >> 6; + sr = (sr >> 3 )|( xmc[11] << 13); + sr = (sr >> 3 )|( xmc[12] << 13); + *c++ = sr >> 8; + sr = (sr >> 7 )|( Nc[1] << 9); + sr = (sr >> 2 )|( bc[1] << 14); + *c++ = sr >> 7; + sr = (sr >> 2 )|( Mc[1] << 14); + sr = (sr >> 6 )|( xmaxc[1] << 10); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[13] << 13); + sr = (sr >> 3 )|( xmc[14] << 13); + sr = (sr >> 3 )|( xmc[15] << 13); + *c++ = sr >> 6; + sr = (sr >> 3 )|( xmc[16] << 13); + sr = (sr >> 3 )|( xmc[17] << 13); + *c++ = sr >> 8; + sr = (sr >> 3 )|( xmc[18] << 13); + sr = (sr >> 3 )|( xmc[19] << 13); + sr = (sr >> 3 )|( xmc[20] << 13); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[21] << 13); + sr = (sr >> 3 )|( xmc[22] << 13); + sr = (sr >> 3 )|( xmc[23] << 13); + *c++ = sr >> 6; + sr = (sr >> 3 )|( xmc[24] << 13); + sr = (sr >> 3 )|( xmc[25] << 13); + *c++ = sr >> 8; + sr = (sr >> 7 )|( Nc[2] << 9); + sr = (sr >> 2 )|( bc[2] << 14); + *c++ = sr >> 7; + sr = (sr >> 2 )|( Mc[2] << 14); + sr = (sr >> 6 )|( xmaxc[2] << 10); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[26] << 13); + sr = (sr >> 3 )|( xmc[27] << 13); + sr = (sr >> 3 )|( xmc[28] << 13); + *c++ = sr >> 6; + sr = (sr >> 3 )|( xmc[29] << 13); + sr = (sr >> 3 )|( xmc[30] << 13); + *c++ = sr >> 8; + sr = (sr >> 3 )|( xmc[31] << 13); + sr = (sr >> 3 )|( xmc[32] << 13); + sr = (sr >> 3 )|( xmc[33] << 13); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[34] << 13); + sr = (sr >> 3 )|( xmc[35] << 13); + sr = (sr >> 3 )|( xmc[36] << 13); + *c++ = sr >> 6; + sr = (sr >> 3 )|( xmc[37] << 13); + sr = (sr >> 3 )|( xmc[38] << 13); + *c++ = sr >> 8; + sr = (sr >> 7 )|( Nc[3] << 9); + sr = (sr >> 2 )|( bc[3] << 14); + *c++ = sr >> 7; + sr = (sr >> 2 )|( Mc[3] << 14); + sr = (sr >> 6 )|( xmaxc[3] << 10); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[39] << 13); + sr = (sr >> 3 )|( xmc[40] << 13); + sr = (sr >> 3 )|( xmc[41] << 13); + *c++ = sr >> 6; + sr = (sr >> 3 )|( xmc[42] << 13); + sr = (sr >> 3 )|( xmc[43] << 13); + *c++ = sr >> 8; + sr = (sr >> 3 )|( xmc[44] << 13); + sr = (sr >> 3 )|( xmc[45] << 13); + sr = (sr >> 3 )|( xmc[46] << 13); + *c++ = sr >> 7; + sr = (sr >> 3 )|( xmc[47] << 13); + sr = (sr >> 3 )|( xmc[48] << 13); + sr = (sr >> 3 )|( xmc[49] << 13); + *c++ = sr >> 6; + sr = (sr >> 3 )|( xmc[50] << 13); + sr = (sr >> 3 )|( xmc[51] << 13); + *c++ = sr >> 8; + +} + +#define writeGSM_33(c1) { \ + gsm_byte *c = (c1); \ + *c++ = ((GSM_MAGIC & 0xF) << 4) /* 1 */ \ + | ((LARc[0] >> 2) & 0xF); \ + *c++ = ((LARc[0] & 0x3) << 6) \ + | (LARc[1] & 0x3F); \ + *c++ = ((LARc[2] & 0x1F) << 3) \ + | ((LARc[3] >> 2) & 0x7); \ + *c++ = ((LARc[3] & 0x3) << 6) \ + | ((LARc[4] & 0xF) << 2) \ + | ((LARc[5] >> 2) & 0x3); \ + *c++ = ((LARc[5] & 0x3) << 6) \ + | ((LARc[6] & 0x7) << 3) \ + | (LARc[7] & 0x7); \ + *c++ = ((Nc[0] & 0x7F) << 1) \ + | ((bc[0] >> 1) & 0x1); \ + *c++ = ((bc[0] & 0x1) << 7) \ + | ((Mc[0] & 0x3) << 5) \ + | ((xmaxc[0] >> 1) & 0x1F); \ + *c++ = ((xmaxc[0] & 0x1) << 7) \ + | ((xmc[0] & 0x7) << 4) \ + | ((xmc[1] & 0x7) << 1) \ + | ((xmc[2] >> 2) & 0x1); \ + *c++ = ((xmc[2] & 0x3) << 6) \ + | ((xmc[3] & 0x7) << 3) \ + | (xmc[4] & 0x7); \ + *c++ = ((xmc[5] & 0x7) << 5) /* 10 */ \ + | ((xmc[6] & 0x7) << 2) \ + | ((xmc[7] >> 1) & 0x3); \ + *c++ = ((xmc[7] & 0x1) << 7) \ + | ((xmc[8] & 0x7) << 4) \ + | ((xmc[9] & 0x7) << 1) \ + | ((xmc[10] >> 2) & 0x1); \ + *c++ = ((xmc[10] & 0x3) << 6) \ + | ((xmc[11] & 0x7) << 3) \ + | (xmc[12] & 0x7); \ + *c++ = ((Nc[1] & 0x7F) << 1) \ + | ((bc[1] >> 1) & 0x1); \ + *c++ = ((bc[1] & 0x1) << 7) \ + | ((Mc[1] & 0x3) << 5) \ + | ((xmaxc[1] >> 1) & 0x1F); \ + *c++ = ((xmaxc[1] & 0x1) << 7) \ + | ((xmc[13] & 0x7) << 4) \ + | ((xmc[14] & 0x7) << 1) \ + | ((xmc[15] >> 2) & 0x1); \ + *c++ = ((xmc[15] & 0x3) << 6) \ + | ((xmc[16] & 0x7) << 3) \ + | (xmc[17] & 0x7); \ + *c++ = ((xmc[18] & 0x7) << 5) \ + | ((xmc[19] & 0x7) << 2) \ + | ((xmc[20] >> 1) & 0x3); \ + *c++ = ((xmc[20] & 0x1) << 7) \ + | ((xmc[21] & 0x7) << 4) \ + | ((xmc[22] & 0x7) << 1) \ + | ((xmc[23] >> 2) & 0x1); \ + *c++ = ((xmc[23] & 0x3) << 6) \ + | ((xmc[24] & 0x7) << 3) \ + | (xmc[25] & 0x7); \ + *c++ = ((Nc[2] & 0x7F) << 1) /* 20 */ \ + | ((bc[2] >> 1) & 0x1); \ + *c++ = ((bc[2] & 0x1) << 7) \ + | ((Mc[2] & 0x3) << 5) \ + | ((xmaxc[2] >> 1) & 0x1F); \ + *c++ = ((xmaxc[2] & 0x1) << 7) \ + | ((xmc[26] & 0x7) << 4) \ + | ((xmc[27] & 0x7) << 1) \ + | ((xmc[28] >> 2) & 0x1); \ + *c++ = ((xmc[28] & 0x3) << 6) \ + | ((xmc[29] & 0x7) << 3) \ + | (xmc[30] & 0x7); \ + *c++ = ((xmc[31] & 0x7) << 5) \ + | ((xmc[32] & 0x7) << 2) \ + | ((xmc[33] >> 1) & 0x3); \ + *c++ = ((xmc[33] & 0x1) << 7) \ + | ((xmc[34] & 0x7) << 4) \ + | ((xmc[35] & 0x7) << 1) \ + | ((xmc[36] >> 2) & 0x1); \ + *c++ = ((xmc[36] & 0x3) << 6) \ + | ((xmc[37] & 0x7) << 3) \ + | (xmc[38] & 0x7); \ + *c++ = ((Nc[3] & 0x7F) << 1) \ + | ((bc[3] >> 1) & 0x1); \ + *c++ = ((bc[3] & 0x1) << 7) \ + | ((Mc[3] & 0x3) << 5) \ + | ((xmaxc[3] >> 1) & 0x1F); \ + *c++ = ((xmaxc[3] & 0x1) << 7) \ + | ((xmc[39] & 0x7) << 4) \ + | ((xmc[40] & 0x7) << 1) \ + | ((xmc[41] >> 2) & 0x1); \ + *c++ = ((xmc[41] & 0x3) << 6) /* 30 */ \ + | ((xmc[42] & 0x7) << 3) \ + | (xmc[43] & 0x7); \ + *c++ = ((xmc[44] & 0x7) << 5) \ + | ((xmc[45] & 0x7) << 2) \ + | ((xmc[46] >> 1) & 0x3); \ + *c++ = ((xmc[46] & 0x1) << 7) \ + | ((xmc[47] & 0x7) << 4) \ + | ((xmc[48] & 0x7) << 1) \ + | ((xmc[49] >> 2) & 0x1); \ + *c++ = ((xmc[49] & 0x3) << 6) \ + | ((xmc[50] & 0x7) << 3) \ + | (xmc[51] & 0x7); \ +} + +static inline void conv65( wav_byte * c, gsm_byte * d){ + + unsigned int sr = 0; + unsigned int frame_chain; + unsigned int LARc[8], Nc[4], Mc[4], bc[4], xmaxc[4]; + /* silence bogus compiler warning */ + unsigned int xmc[13*4] = { 0, }; + + sr = *c++; + LARc[0] = sr & 0x3f; sr >>= 6; + sr |= (uword)*c++ << 2; + LARc[1] = sr & 0x3f; sr >>= 6; + sr |= (uword)*c++ << 4; + LARc[2] = sr & 0x1f; sr >>= 5; + LARc[3] = sr & 0x1f; sr >>= 5; + sr |= (uword)*c++ << 2; + LARc[4] = sr & 0xf; sr >>= 4; + LARc[5] = sr & 0xf; sr >>= 4; + sr |= (uword)*c++ << 2; /* 5 */ + LARc[6] = sr & 0x7; sr >>= 3; + LARc[7] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 4; + Nc[0] = sr & 0x7f; sr >>= 7; + bc[0] = sr & 0x3; sr >>= 2; + Mc[0] = sr & 0x3; sr >>= 2; + sr |= (uword)*c++ << 1; + xmaxc[0] = sr & 0x3f; sr >>= 6; + xmc[0] = sr & 0x7; sr >>= 3; + sr = *c++; + xmc[1] = sr & 0x7; sr >>= 3; + xmc[2] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 2; + xmc[3] = sr & 0x7; sr >>= 3; + xmc[4] = sr & 0x7; sr >>= 3; + xmc[5] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 1; /* 10 */ + xmc[6] = sr & 0x7; sr >>= 3; + xmc[7] = sr & 0x7; sr >>= 3; + xmc[8] = sr & 0x7; sr >>= 3; + sr = *c++; + xmc[9] = sr & 0x7; sr >>= 3; + xmc[10] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 2; + xmc[11] = sr & 0x7; sr >>= 3; + xmc[12] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 4; + Nc[1] = sr & 0x7f; sr >>= 7; + bc[1] = sr & 0x3; sr >>= 2; + Mc[1] = sr & 0x3; sr >>= 2; + sr |= (uword)*c++ << 1; + xmaxc[1] = sr & 0x3f; sr >>= 6; + xmc[13] = sr & 0x7; sr >>= 3; + sr = *c++; /* 15 */ + xmc[14] = sr & 0x7; sr >>= 3; + xmc[15] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 2; + xmc[16] = sr & 0x7; sr >>= 3; + xmc[17] = sr & 0x7; sr >>= 3; + xmc[18] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 1; + xmc[19] = sr & 0x7; sr >>= 3; + xmc[20] = sr & 0x7; sr >>= 3; + xmc[21] = sr & 0x7; sr >>= 3; + sr = *c++; + xmc[22] = sr & 0x7; sr >>= 3; + xmc[23] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 2; + xmc[24] = sr & 0x7; sr >>= 3; + xmc[25] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 4; /* 20 */ + Nc[2] = sr & 0x7f; sr >>= 7; + bc[2] = sr & 0x3; sr >>= 2; + Mc[2] = sr & 0x3; sr >>= 2; + sr |= (uword)*c++ << 1; + xmaxc[2] = sr & 0x3f; sr >>= 6; + xmc[26] = sr & 0x7; sr >>= 3; + sr = *c++; + xmc[27] = sr & 0x7; sr >>= 3; + xmc[28] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 2; + xmc[29] = sr & 0x7; sr >>= 3; + xmc[30] = sr & 0x7; sr >>= 3; + xmc[31] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 1; + xmc[32] = sr & 0x7; sr >>= 3; + xmc[33] = sr & 0x7; sr >>= 3; + xmc[34] = sr & 0x7; sr >>= 3; + sr = *c++; /* 25 */ + xmc[35] = sr & 0x7; sr >>= 3; + xmc[36] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 2; + xmc[37] = sr & 0x7; sr >>= 3; + xmc[38] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 4; + Nc[3] = sr & 0x7f; sr >>= 7; + bc[3] = sr & 0x3; sr >>= 2; + Mc[3] = sr & 0x3; sr >>= 2; + sr |= (uword)*c++ << 1; + xmaxc[3] = sr & 0x3f; sr >>= 6; + xmc[39] = sr & 0x7; sr >>= 3; + sr = *c++; + xmc[40] = sr & 0x7; sr >>= 3; + xmc[41] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 2; /* 30 */ + xmc[42] = sr & 0x7; sr >>= 3; + xmc[43] = sr & 0x7; sr >>= 3; + xmc[44] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 1; + xmc[45] = sr & 0x7; sr >>= 3; + xmc[46] = sr & 0x7; sr >>= 3; + xmc[47] = sr & 0x7; sr >>= 3; + sr = *c++; + xmc[49] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 2; + xmc[50] = sr & 0x7; sr >>= 3; + xmc[51] = sr & 0x7; sr >>= 3; + + frame_chain = sr & 0xf; + + + writeGSM_33(d);/* LARc etc. -> array of 33 GSM bytes */ + + + sr = frame_chain; + sr |= (uword)*c++ << 4; /* 1 */ + LARc[0] = sr & 0x3f; sr >>= 6; + LARc[1] = sr & 0x3f; sr >>= 6; + sr = *c++; + LARc[2] = sr & 0x1f; sr >>= 5; + sr |= (uword)*c++ << 3; + LARc[3] = sr & 0x1f; sr >>= 5; + LARc[4] = sr & 0xf; sr >>= 4; + sr |= (uword)*c++ << 2; + LARc[5] = sr & 0xf; sr >>= 4; + LARc[6] = sr & 0x7; sr >>= 3; + LARc[7] = sr & 0x7; sr >>= 3; + sr = *c++; /* 5 */ + Nc[0] = sr & 0x7f; sr >>= 7; + sr |= (uword)*c++ << 1; + bc[0] = sr & 0x3; sr >>= 2; + Mc[0] = sr & 0x3; sr >>= 2; + sr |= (uword)*c++ << 5; + xmaxc[0] = sr & 0x3f; sr >>= 6; + xmc[0] = sr & 0x7; sr >>= 3; + xmc[1] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 1; + xmc[2] = sr & 0x7; sr >>= 3; + xmc[3] = sr & 0x7; sr >>= 3; + xmc[4] = sr & 0x7; sr >>= 3; + sr = *c++; + xmc[5] = sr & 0x7; sr >>= 3; + xmc[6] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 2; /* 10 */ + xmc[7] = sr & 0x7; sr >>= 3; + xmc[8] = sr & 0x7; sr >>= 3; + xmc[9] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 1; + xmc[10] = sr & 0x7; sr >>= 3; + xmc[11] = sr & 0x7; sr >>= 3; + xmc[12] = sr & 0x7; sr >>= 3; + sr = *c++; + Nc[1] = sr & 0x7f; sr >>= 7; + sr |= (uword)*c++ << 1; + bc[1] = sr & 0x3; sr >>= 2; + Mc[1] = sr & 0x3; sr >>= 2; + sr |= (uword)*c++ << 5; + xmaxc[1] = sr & 0x3f; sr >>= 6; + xmc[13] = sr & 0x7; sr >>= 3; + xmc[14] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 1; /* 15 */ + xmc[15] = sr & 0x7; sr >>= 3; + xmc[16] = sr & 0x7; sr >>= 3; + xmc[17] = sr & 0x7; sr >>= 3; + sr = *c++; + xmc[18] = sr & 0x7; sr >>= 3; + xmc[19] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 2; + xmc[20] = sr & 0x7; sr >>= 3; + xmc[21] = sr & 0x7; sr >>= 3; + xmc[22] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 1; + xmc[23] = sr & 0x7; sr >>= 3; + xmc[24] = sr & 0x7; sr >>= 3; + xmc[25] = sr & 0x7; sr >>= 3; + sr = *c++; + Nc[2] = sr & 0x7f; sr >>= 7; + sr |= (uword)*c++ << 1; /* 20 */ + bc[2] = sr & 0x3; sr >>= 2; + Mc[2] = sr & 0x3; sr >>= 2; + sr |= (uword)*c++ << 5; + xmaxc[2] = sr & 0x3f; sr >>= 6; + xmc[26] = sr & 0x7; sr >>= 3; + xmc[27] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 1; + xmc[28] = sr & 0x7; sr >>= 3; + xmc[29] = sr & 0x7; sr >>= 3; + xmc[30] = sr & 0x7; sr >>= 3; + sr = *c++; + xmc[31] = sr & 0x7; sr >>= 3; + xmc[32] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 2; + xmc[33] = sr & 0x7; sr >>= 3; + xmc[34] = sr & 0x7; sr >>= 3; + xmc[35] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 1; /* 25 */ + xmc[36] = sr & 0x7; sr >>= 3; + xmc[37] = sr & 0x7; sr >>= 3; + xmc[38] = sr & 0x7; sr >>= 3; + sr = *c++; + Nc[3] = sr & 0x7f; sr >>= 7; + sr |= (uword)*c++ << 1; + bc[3] = sr & 0x3; sr >>= 2; + Mc[3] = sr & 0x3; sr >>= 2; + sr |= (uword)*c++ << 5; + xmaxc[3] = sr & 0x3f; sr >>= 6; + xmc[39] = sr & 0x7; sr >>= 3; + xmc[40] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 1; + xmc[41] = sr & 0x7; sr >>= 3; + xmc[42] = sr & 0x7; sr >>= 3; + xmc[43] = sr & 0x7; sr >>= 3; + sr = *c++; /* 30 */ + xmc[44] = sr & 0x7; sr >>= 3; + xmc[45] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 2; + xmc[46] = sr & 0x7; sr >>= 3; + xmc[47] = sr & 0x7; sr >>= 3; + xmc[48] = sr & 0x7; sr >>= 3; + sr |= (uword)*c++ << 1; + xmc[49] = sr & 0x7; sr >>= 3; + xmc[50] = sr & 0x7; sr >>= 3; + xmc[51] = sr & 0x7; sr >>= 3; + writeGSM_33(d+33); + +} |