Largely simplify format handlers (for file copy etc.)

collecting common functions in a single place and removing
them from the individual handlers.
The full description is on mantis,
http://bugs.digium.com/view.php?id=6375
and only the ogg_vorbis handler needs to be converted to
the new structure.

As a result of this change, format_au.c and format_pcm_alaw.c
should go away (in a separate commit) as their functionality
(trivial) has been merged in another file.



git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@17243 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Luigi Rizzo
2006-04-04 12:59:25 +00:00
parent ec67c650ad
commit 4beb6deeaa
18 changed files with 1748 additions and 2790 deletions

View File

@@ -54,6 +54,12 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
/* Portions of the conversion code are by guido@sienanet.it */
#define GSM_FRAME_SIZE 33
#define MSGSM_FRAME_SIZE 65
#define MSGSM_DATA_OFS 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
@@ -63,29 +69,12 @@ char msgsm_silence[] = /* 65 */
,0x92,0x24,0x49,0x92,0x00};
/* end binary data. size = 65 bytes */
struct ast_filestream {
void *reserved[AST_RESERVED_POINTERS];
struct wavg_desc {
/* Believe it or not, we must decode/recode to account for the
weird MS format */
/* This is what a filestream means to us */
FILE *f; /* Descriptor */
struct ast_frame fr; /* Frame information */
char waste[AST_FRIENDLY_OFFSET]; /* Buffer for sending frames, etc */
char empty; /* Empty character */
unsigned char gsm[66]; /* Two Real GSM Frames */
int foffset;
int secondhalf; /* Are we on the second half */
struct timeval last;
};
AST_MUTEX_DEFINE_STATIC(wav_lock);
static int glistcnt = 0;
static char *name = "wav49";
static char *desc = "Microsoft WAV format (Proprietary GSM)";
static char *exts = "WAV|wav49";
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define htoll(b) (b)
#define htols(b) (b)
@@ -173,7 +162,7 @@ static int check_header(FILE *f)
ast_log(LOG_WARNING, "Read failed (freq)\n");
return -1;
}
if (ltohl(freq) != 8000) {
if (ltohl(freq) != DEFAULT_SAMPLE_RATE) {
ast_log(LOG_WARNING, "Unexpected freqency %d\n", ltohl(freq));
return -1;
}
@@ -236,7 +225,7 @@ static int update_header(FILE *f)
fseek(f, 0, SEEK_END);
end = ftello(f);
/* in a gsm WAV, data starts 60 bytes in */
bytes = end - 60;
bytes = end - MSGSM_DATA_OFS;
datalen = htoll((bytes + 1) & ~0x1);
filelen = htoll(52 + ((bytes + 1) & ~0x1));
if (cur < 0) {
@@ -268,7 +257,7 @@ static int update_header(FILE *f)
static int write_header(FILE *f)
{
unsigned int hz=htoll(8000);
unsigned int hz=htoll(DEFAULT_SAMPLE_RATE); /* XXX the following are relate to DEFAULT_SAMPLE_RATE ? */
unsigned int bhz = htoll(1625);
unsigned int hs = htoll(20);
unsigned short fmt = htols(49);
@@ -347,119 +336,78 @@ static int write_header(FILE *f)
return 0;
}
static struct ast_filestream *wav_open(FILE *f)
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 ast_filestream *tmp;
if ((tmp = malloc(sizeof(struct ast_filestream)))) {
memset(tmp, 0, sizeof(struct ast_filestream));
if (check_header(f)) {
free(tmp);
return NULL;
}
if (ast_mutex_lock(&wav_lock)) {
ast_log(LOG_WARNING, "Unable to lock wav list\n");
free(tmp);
return NULL;
}
tmp->f = f;
tmp->fr.data = tmp->gsm;
tmp->fr.frametype = AST_FRAME_VOICE;
tmp->fr.subclass = AST_FORMAT_GSM;
/* datalen will vary for each frame */
tmp->fr.src = name;
tmp->fr.mallocd = 0;
tmp->secondhalf = 0;
glistcnt++;
ast_mutex_unlock(&wav_lock);
ast_update_use_count();
}
return tmp;
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 struct ast_filestream *wav_rewrite(FILE *f, const char *comment)
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. */
struct ast_filestream *tmp;
if ((tmp = malloc(sizeof(struct ast_filestream)))) {
memset(tmp, 0, sizeof(struct ast_filestream));
if (write_header(f)) {
free(tmp);
return NULL;
}
if (ast_mutex_lock(&wav_lock)) {
ast_log(LOG_WARNING, "Unable to lock wav list\n");
free(tmp);
return NULL;
}
tmp->f = f;
glistcnt++;
ast_mutex_unlock(&wav_lock);
ast_update_use_count();
} else
ast_log(LOG_WARNING, "Out of memory\n");
return tmp;
if (write_header(s->f))
return -1;
return 0;
}
static void wav_close(struct ast_filestream *s)
{
char zero = 0;
if (ast_mutex_lock(&wav_lock)) {
ast_log(LOG_WARNING, "Unable to lock wav list\n");
return;
}
glistcnt--;
ast_mutex_unlock(&wav_lock);
ast_update_use_count();
/* Pad to even length */
fseek(s->f, 0, SEEK_END);
if (ftello(s->f) & 0x1)
fwrite(&zero, 1, 1, s->f);
fclose(s->f);
free(s);
s = NULL;
}
static struct ast_frame *wav_read(struct ast_filestream *s, int *whennext)
{
int res;
char msdata[66];
/* 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 = 160;
s->fr.datalen = 33;
s->fr.samples = GSM_SAMPLES;
s->fr.mallocd = 0;
if (s->secondhalf) {
FR_SET_BUF(&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 = s->gsm + 33;
s->fr.data = (char *)s->fr.data + GSM_FRAME_SIZE;
s->fr.offset += GSM_FRAME_SIZE;
} else {
if ((res = fread(msdata, 1, 65, s->f)) != 65) {
/* read and convert */
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->gsm);
s->fr.data = s->gsm;
conv65(msdata, s->fr.data);
}
s->secondhalf = !s->secondhalf;
*whennext = 160;
fs->secondhalf = !fs->secondhalf;
*whennext = GSM_SAMPLES;
return &s->fr;
}
static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
static int wav_write(struct ast_filestream *s, struct ast_frame *f)
{
int res;
char msdata[66];
int len =0;
int alreadyms=0;
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;
@@ -468,65 +416,70 @@ static int wav_write(struct ast_filestream *fs, struct ast_frame *f)
ast_log(LOG_WARNING, "Asked to write non-GSM frame (%d)!\n", f->subclass);
return -1;
}
if (!(f->datalen % 65))
alreadyms = 1;
while(len < f->datalen) {
if (alreadyms) {
/* 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;
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(s->buf, msdata);
src = msdata;
fs->secondhalf = 0;
if ((res = fwrite(f->data + len, 1, 65, fs->f)) != 65) {
ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
return -1;
}
update_header(fs->f);
len += 65;
} else {
if (fs->secondhalf) {
memcpy(fs->gsm + 33, f->data + len, 33);
conv66(fs->gsm, msdata);
if ((res = fwrite(msdata, 1, 65, fs->f)) != 65) {
ast_log(LOG_WARNING, "Bad write (%d/65): %s\n", res, strerror(errno));
return -1;
}
update_header(fs->f);
} else {
/* Copy the data and do nothing */
memcpy(fs->gsm, f->data + len, 33);
}
fs->secondhalf = !fs->secondhalf;
len += 33;
} 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, size, s->f)) != 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,cur,min,max;
min = 60;
cur = ftello(fs->f);
off_t offset=0, distance, max;
struct wavg_desc *s = (struct wavg_desc *)fs->private;
off_t min = MSGSM_DATA_OFS;
off_t cur = ftello(fs->f);
fseek(fs->f, 0, SEEK_END);
max = ftello(fs->f);
/* I'm getting sloppy here, I'm only going to go to even splits of the 2
* frames, if you want tighter cuts use format_gsm, format_pcm, or format_wav */
distance = (sample_offset/320) * 65;
if(whence == SEEK_SET)
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)
else if (whence == SEEK_CUR || whence == SEEK_FORCECUR)
offset = distance + cur;
else if(whence == SEEK_END)
else if (whence == SEEK_END)
offset = max - distance;
/* always protect against seeking past end of header */
offset = (offset < min)?min:offset;
if (offset < min)
offset = min;
if (whence != SEEK_FORCECUR) {
offset = (offset > max)?max:offset;
if (offset > max)
offset = max;
} else if (offset > max) {
int i;
fseek(fs->f, 0, SEEK_END);
for (i=0; i< (offset - max) / 65; i++) {
fwrite(msgsm_silence, 1, 65, fs->f);
for (i=0; i< (offset - max) / MSGSM_FRAME_SIZE; i++) {
fwrite(msgsm_silence, 1, MSGSM_FRAME_SIZE, fs->f);
}
}
fs->secondhalf = 0;
s->secondhalf = 0;
return fseeko(fs->f, offset, SEEK_SET);
}
@@ -543,46 +496,49 @@ static off_t wav_tell(struct ast_filestream *fs)
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 - 52)/65*320;
/* XXX why 52 ? */
return (offset - 52)/MSGSM_FRAME_SIZE*MSGSM_SAMPLES;
}
static char *wav_getcomment(struct ast_filestream *s)
{
return NULL;
}
static struct ast_format_lock me = { .usecnt = -1 };
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),
.lockp = &me,
};
int load_module()
{
return ast_format_register(name, exts, AST_FORMAT_GSM,
wav_open,
wav_rewrite,
wav_write,
wav_seek,
wav_trunc,
wav_tell,
wav_read,
wav_close,
wav_getcomment);
return ast_format_register(&wav49_f);
}
int unload_module()
{
return ast_format_unregister(name);
return ast_format_unregister(wav49_f.name);
}
int usecount()
{
return glistcnt;
return me.usecnt;
}
char *description()
{
return desc;
return "Microsoft WAV format (Proprietary GSM)";
}
char *key()
{
return ASTERISK_GPL_KEY;