[asterisk-commits] trunk r18541 - in /trunk: ./ codecs/ include/asterisk/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Sat Apr 8 14:42:55 MST 2006


Author: kpfleming
Date: Sat Apr  8 16:40:57 2006
New Revision: 18541

URL: http://svn.digium.com/view/asterisk?rev=18541&view=rev
Log:
merge rizzo's codec module rework (very similar to the format module rework)

Modified:
    trunk/codecs/codec_a_mu.c
    trunk/codecs/codec_adpcm.c
    trunk/codecs/codec_alaw.c
    trunk/codecs/codec_g723_1.c
    trunk/codecs/codec_g726.c
    trunk/codecs/codec_gsm.c
    trunk/codecs/codec_ilbc.c
    trunk/codecs/codec_lpc10.c
    trunk/codecs/codec_speex.c
    trunk/codecs/codec_ulaw.c
    trunk/include/asterisk/translate.h
    trunk/translate.c

Modified: trunk/codecs/codec_a_mu.c
URL: http://svn.digium.com/view/asterisk/trunk/codecs/codec_a_mu.c?rev=18541&r1=18540&r2=18541&view=diff
==============================================================================
--- trunk/codecs/codec_a_mu.c (original)
+++ trunk/codecs/codec_a_mu.c Sat Apr  8 16:40:57 2006
@@ -43,12 +43,7 @@
 #include "asterisk/ulaw.h"
 #include "asterisk/utils.h"
 
-#define BUFFER_SIZE   8096	/* size for the translation buffers */
-
-AST_MUTEX_DEFINE_STATIC(localuser_lock);
-static int localusecnt = 0;
-
-static char *tdesc = "A-law and Mulaw direct Coder/Decoder";
+#define BUFFER_SAMPLES   8000	/* size for the translation buffers */
 
 static unsigned char mu2a[256];
 static unsigned char a2mu[256];
@@ -57,146 +52,37 @@
 
 #include "ulaw_slin_ex.h"
 
-/*
- * Private workspace for translating signed linear signals to alaw.
- */
+/*! \brief convert frame data and store into the buffer */
+static int alawtoulaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
+{
+	int x;
+	unsigned char *src = f->data;
+	unsigned char *dst = (unsigned char *)pvt->outbuf + pvt->samples;
 
-struct alaw_encoder_pvt
-{
-	struct ast_frame f;
-	char offset[AST_FRIENDLY_OFFSET];	/* Space to build offset */
-	unsigned char outbuf[BUFFER_SIZE];	/* Encoded alaw, two nibbles to a word */
-	int tail;
-};
-
-/*
- * Private workspace for translating laws.
- */
-
-struct ulaw_encoder_pvt
-{
-	struct ast_frame f;
-	char offset[AST_FRIENDLY_OFFSET];	/* Space to build offset */
-	unsigned char outbuf[BUFFER_SIZE];	/* Encoded ulaw values */
-	int tail;
-};
-
-static struct ast_translator_pvt *alawtoulaw_new(void)
-{
-	struct ulaw_encoder_pvt *tmp;
-	
-	if ((tmp = ast_calloc(1, sizeof(*tmp)))) {
-		tmp->tail = 0;
-		localusecnt++;
-		ast_update_use_count();
-	}
-
-	return (struct ast_translator_pvt *)tmp;
-}
-
-static struct ast_translator_pvt *ulawtoalaw_new(void)
-{
-	struct alaw_encoder_pvt *tmp;
-	
-	if ((tmp = ast_calloc(1, sizeof(*tmp)))) {
-		localusecnt++;
-		ast_update_use_count();
-		tmp->tail = 0;
-	}
-
-	return (struct ast_translator_pvt *)tmp;
-}
-
-static int alawtoulaw_framein(struct ast_translator_pvt *pvt, struct ast_frame *f)
-{
-	struct ulaw_encoder_pvt *tmp = (struct ulaw_encoder_pvt *)pvt;
-	int x;
-	unsigned char *b;
-
-	if ((tmp->tail + f->datalen) > sizeof(tmp->outbuf)) {
-		ast_log(LOG_WARNING, "Out of buffer space\n");
-		return -1;
-	}
-
-	/* Reset ssindex and signal to frame's specified values */
-	b = f->data;
-	for (x=0;x<f->datalen;x++)
-		tmp->outbuf[tmp->tail + x] = a2mu[b[x]];
-
-	tmp->tail += f->datalen;
+	for ( x = 0 ; x < f->samples; x++)
+		dst[x] = a2mu[src[x]];
+	pvt->samples += f->samples;
+	pvt->datalen += f->datalen;
 	return 0;
 }
 
-static struct ast_frame *alawtoulaw_frameout(struct ast_translator_pvt *pvt)
+/*! \brief convert frame data and store into the buffer */
+static int ulawtoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
-	struct ulaw_encoder_pvt *tmp = (struct ulaw_encoder_pvt *)pvt;
+	int x;
+	unsigned char *src = f->data;
+	unsigned char *dst = (unsigned char *)pvt->outbuf + pvt->samples;
 
-	if (!tmp->tail)
-		return NULL;
-
-	tmp->f.frametype = AST_FRAME_VOICE;
-	tmp->f.subclass = AST_FORMAT_ULAW;
-	tmp->f.datalen = tmp->tail;
-	tmp->f.samples = tmp->tail;
-	tmp->f.mallocd = 0;
-	tmp->f.offset = AST_FRIENDLY_OFFSET;
-	tmp->f.src = __PRETTY_FUNCTION__;
-	tmp->f.data = tmp->outbuf;
-	tmp->tail = 0;
-	return &tmp->f;
-}
-
-static int ulawtoalaw_framein(struct ast_translator_pvt *pvt, struct ast_frame *f)
-{
-	struct alaw_encoder_pvt *tmp = (struct alaw_encoder_pvt *)pvt;
-	int x;
-	unsigned char *s;
-	if (tmp->tail + f->datalen >= sizeof(tmp->outbuf)) {
-		ast_log(LOG_WARNING, "Out of buffer space\n");
-		return -1;
-	}
-	s = f->data;
-	for (x=0;x<f->datalen;x++) 
-		tmp->outbuf[x+tmp->tail] = mu2a[s[x]];
-	tmp->tail += f->datalen;
+	for ( x = 0 ; x < f->samples; x++)
+		dst[x] = mu2a[src[x]];
+	pvt->samples += f->samples;
+	pvt->datalen += f->datalen;
 	return 0;
 }
 
 /*
- * LinToalaw_FrameOut
- *  Convert a buffer of raw 16-bit signed linear PCM to a buffer
- *  of 4-bit alaw packed two to a byte (Big Endian).
- *
- * Results:
- *  Foo
- *
- * Side effects:
- *  Leftover inbuf data gets packed, tail gets updated.
+ * alawToLin_Sample. Just random data, somehow...
  */
-
-static struct ast_frame *ulawtoalaw_frameout(struct ast_translator_pvt *pvt)
-{
-	struct alaw_encoder_pvt *tmp = (struct alaw_encoder_pvt *)pvt;
-  
-	if (tmp->tail) {
-		tmp->f.frametype = AST_FRAME_VOICE;
-		tmp->f.subclass = AST_FORMAT_ALAW;
-		tmp->f.samples = tmp->tail;
-		tmp->f.mallocd = 0;
-		tmp->f.offset = AST_FRIENDLY_OFFSET;
-		tmp->f.src = __PRETTY_FUNCTION__;
-		tmp->f.data = tmp->outbuf;
-		tmp->f.datalen = tmp->tail;
-		tmp->tail = 0;
-		return &tmp->f;
-	} else
-		return NULL;
-}
-
-/*
- * alawToLin_Sample
- */
-
 static struct ast_frame *alawtoulaw_sample(void)
 {
 	static struct ast_frame f;
@@ -207,7 +93,7 @@
 	f.mallocd = 0;
 	f.offset = 0;
 	f.src = __PRETTY_FUNCTION__;
-	f.data = ulaw_slin_ex;
+	f.data = ulaw_slin_ex; /* XXX what ? */
 	return &f;
 }
 
@@ -225,66 +111,41 @@
 	return &f;
 }
 
-/*
- * alaw_Destroy
- *  Destroys a private workspace.
- *
- * Results:
- *  It's gone!
- *
- * Side effects:
- *  None.
- */
-
-static void alaw_destroy(struct ast_translator_pvt *pvt)
-{
-	free(pvt);
-	localusecnt--;
-	ast_update_use_count();
-}
-
-/*
- * The complete translator for alawToLin.
- */
+static struct ast_module_lock me = { .usecnt = -1 };
 
 static struct ast_translator alawtoulaw = {
-	"alawtoulaw",
-	AST_FORMAT_ALAW,
-	AST_FORMAT_ULAW,
-	alawtoulaw_new,
-	alawtoulaw_framein,
-	alawtoulaw_frameout,
-	alaw_destroy,
-	/* NULL */
-	alawtoulaw_sample
+	.name = "alawtoulaw",
+	.srcfmt = AST_FORMAT_ALAW,
+	.dstfmt = AST_FORMAT_ULAW,
+	.framein = alawtoulaw_framein,
+	.sample = alawtoulaw_sample,
+	.buffer_samples = BUFFER_SAMPLES,
+	.buf_size = BUFFER_SAMPLES,
+	.lockp = &me,
 };
 
-/*
- * The complete translator for LinToalaw.
- */
+static struct ast_translator ulawtoalaw = {
+	.name = "ulawtoalaw",
+	.srcfmt = AST_FORMAT_ULAW,
+	.dstfmt = AST_FORMAT_ALAW,
+	.framein = ulawtoalaw_framein,
+	.sample = ulawtoalaw_sample,
+	.buffer_samples = BUFFER_SAMPLES,
+	.buf_size = BUFFER_SAMPLES,
+	.lockp = &me,
+};
 
-static struct ast_translator ulawtoalaw = {
-	"ulawtoalaw",
-	AST_FORMAT_ULAW,
-	AST_FORMAT_ALAW,
-	ulawtoalaw_new,
-	ulawtoalaw_framein,
-	ulawtoalaw_frameout,
-	alaw_destroy,
-	/* NULL */
-	ulawtoalaw_sample
-};
+/*! \brief standard module glue */
 
 int unload_module(void)
 {
 	int res;
-	ast_mutex_lock(&localuser_lock);
+	ast_mutex_lock(&me.lock);
 	res = ast_unregister_translator(&ulawtoalaw);
-	if (!res)
-		res = ast_unregister_translator(&alawtoulaw);
-	if (localusecnt)
+	res |= ast_unregister_translator(&alawtoulaw);
+	if (me.usecnt)
 		res = -1;
-	ast_mutex_unlock(&localuser_lock);
+	ast_mutex_unlock(&me.lock);
 	return res;
 }
 
@@ -304,20 +165,14 @@
 	return res;
 }
 
-/*
- * Return a description of this module.
- */
-
 char *description(void)
 {
-	return tdesc;
+	return "A-law and Mulaw direct Coder/Decoder";
 }
 
 int usecount(void)
 {
-	int res;
-	OLD_STANDARD_USECOUNT(res);
-	return res;
+	return me.usecnt;
 }
 
 char *key()

Modified: trunk/codecs/codec_adpcm.c
URL: http://svn.digium.com/view/asterisk/trunk/codecs/codec_adpcm.c?rev=18541&r1=18540&r2=18541&view=diff
==============================================================================
--- trunk/codecs/codec_adpcm.c (original)
+++ trunk/codecs/codec_adpcm.c Sat Apr  8 16:40:57 2006
@@ -40,6 +40,7 @@
 
 #include "asterisk/lock.h"
 #include "asterisk/logger.h"
+#include "asterisk/linkedlists.h"
 #include "asterisk/module.h"
 #include "asterisk/config.h"
 #include "asterisk/options.h"
@@ -50,14 +51,7 @@
 /* define NOT_BLI to use a faster but not bit-level identical version */
 /* #define NOT_BLI */
 
-#define BUFFER_SIZE   8096	/* size for the translation buffers */
-
-AST_MUTEX_DEFINE_STATIC(localuser_lock);
-static int localusecnt = 0;
-
-static char *tdesc = "Adaptive Differential PCM Coder/Decoder";
-
-static int useplc = 0;
+#define BUFFER_SAMPLES   8096	/* size for the translation buffers */
 
 /* Sample frame data */
 
@@ -227,250 +221,81 @@
 	return encoded;
 }
 
-/*
- * Private workspace for translating signed linear signals to ADPCM.
- */
-
-struct adpcm_encoder_pvt
-{
-	struct ast_frame f;
-	char offset[AST_FRIENDLY_OFFSET];	/* Space to build offset */
-	short inbuf[BUFFER_SIZE];		/* Unencoded signed linear values */
-	unsigned char outbuf[BUFFER_SIZE];	/* Encoded ADPCM, two nibbles to a word */
+/*----------------- Asterisk-codec glue ------------*/
+
+/*! \brief Workspace for translating signed linear signals to ADPCM. */
+struct adpcm_encoder_pvt {
 	struct adpcm_state state;
-	int tail;
-};
-
-/*
- * Private workspace for translating ADPCM signals to signed linear.
- */
-
-struct adpcm_decoder_pvt
-{
-	struct ast_frame f;
-	char offset[AST_FRIENDLY_OFFSET];	/* Space to build offset */
-	short outbuf[BUFFER_SIZE];		/* Decoded signed linear values */
+	int16_t inbuf[BUFFER_SAMPLES];	/* Unencoded signed linear values */
+};
+
+/*! \brief Workspace for translating ADPCM signals to signed linear. */
+struct adpcm_decoder_pvt {
 	struct adpcm_state state;
-	int tail;
-	plc_state_t plc;
-};
-
-/*
- * AdpcmToLin_New
- *  Create a new instance of adpcm_decoder_pvt.
- *
- * Results:
- *  Returns a pointer to the new instance.
- *
- * Side effects:
- *  None.
- */
-
-static struct ast_translator_pvt *adpcmtolin_new(void)
-{
-	struct adpcm_decoder_pvt *tmp;
-	
-	if ((tmp = ast_calloc(1, sizeof(*tmp)))) {
-		tmp->tail = 0;
-		plc_init(&tmp->plc);
-		localusecnt++;
-		ast_update_use_count();
-	}
-
-	return (struct ast_translator_pvt *)tmp;
-}
-
-/*
- * LinToAdpcm_New
- *  Create a new instance of adpcm_encoder_pvt.
- *
- * Results:
- *  Returns a pointer to the new instance.
- *
- * Side effects:
- *  None.
- */
-
-static struct ast_translator_pvt *lintoadpcm_new(void)
-{
-	struct adpcm_encoder_pvt *tmp;
-	
-	if ((tmp = ast_calloc(1, sizeof(*tmp)))) {
-		localusecnt++;
-		ast_update_use_count();
-		tmp->tail = 0;
-	}
-
-	return (struct ast_translator_pvt *)tmp;
-}
-
-/*
- * AdpcmToLin_FrameIn
- *  Take an input buffer with packed 4-bit ADPCM values and put decoded PCM in outbuf, 
- *  if there is room left.
- *
- * Results:
- *  Foo
- *
- * Side effects:
- *  tmp->tail is the number of packed values in the buffer.
- */
-
-static int adpcmtolin_framein(struct ast_translator_pvt *pvt, struct ast_frame *f)
-{
-	struct adpcm_decoder_pvt *tmp = (struct adpcm_decoder_pvt *)pvt;
+};
+
+/*! \brief decode 4-bit adpcm frame data and store in output buffer */
+static int adpcmtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
+{
+	struct adpcm_decoder_pvt *tmp = pvt->pvt;
 	int x;
-	unsigned char *b;
-
-	if(f->datalen == 0) { /* perform PLC with nominal framesize of 20ms/160 samples */
-		if((tmp->tail + 160) > sizeof(tmp->outbuf) / 2) {
-			ast_log(LOG_WARNING, "Out of buffer space\n");
-			return -1;
-		}
-		if(useplc) {
-			plc_fillin(&tmp->plc, tmp->outbuf+tmp->tail, 160);
-			tmp->tail += 160;
-		}
-		return 0;
-	}
-
-	if (f->datalen * 4 + tmp->tail * 2 > sizeof(tmp->outbuf)) {
-		ast_log(LOG_WARNING, "Out of buffer space\n");
-		return -1;
-	}
-
-	b = f->data;
-
-	for (x=0;x<f->datalen;x++) {
-		tmp->outbuf[tmp->tail++] = decode((b[x] >> 4) & 0xf, &tmp->state);
-		tmp->outbuf[tmp->tail++] = decode(b[x] & 0x0f, &tmp->state);
-	}
-
-	if(useplc)
-		plc_rx(&tmp->plc, tmp->outbuf+tmp->tail-f->datalen*2, f->datalen*2);
-
+	unsigned char *src = f->data;
+	int16_t *dst = (int16_t *)pvt->outbuf + pvt->samples;
+
+	for (x=0; x < f->datalen; x++) {
+		*dst++ = decode((src[x] >> 4) & 0xf, &tmp->state);
+		*dst++ = decode(src[x] & 0x0f, &tmp->state);
+	}
+	pvt->samples += f->samples;
+	pvt->datalen += 2*f->samples;
 	return 0;
 }
 
-/*
- * AdpcmToLin_FrameOut
- *  Convert 4-bit ADPCM encoded signals to 16-bit signed linear.
- *
- * Results:
- *  Converted signals are placed in tmp->f.data, tmp->f.datalen
- *  and tmp->f.samples are calculated.
- *
- * Side effects:
- *  None.
- */
-
-static struct ast_frame *adpcmtolin_frameout(struct ast_translator_pvt *pvt)
-{
-	struct adpcm_decoder_pvt *tmp = (struct adpcm_decoder_pvt *)pvt;
-
-	if (!tmp->tail)
+/*! \brief fill input buffer with 16-bit signed linear PCM values. */
+static int lintoadpcm_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
+{
+	struct adpcm_encoder_pvt *tmp = pvt->pvt;
+
+	memcpy(&tmp->inbuf[pvt->samples], f->data, f->datalen);
+	pvt->samples += f->samples;
+	return 0;
+}
+
+/*! \brief convert inbuf and store into frame */
+static struct ast_frame *lintoadpcm_frameout(struct ast_trans_pvt *pvt)
+{
+	struct adpcm_encoder_pvt *tmp = pvt->pvt;
+	struct ast_frame *f;
+	int i;
+	int samples = pvt->samples;	/* save original number */
+  
+	if (samples < 2)
 		return NULL;
 
-	tmp->f.frametype = AST_FRAME_VOICE;
-	tmp->f.subclass = AST_FORMAT_SLINEAR;
-	tmp->f.datalen = tmp->tail * 2;
-	tmp->f.samples = tmp->tail;
-	tmp->f.mallocd = 0;
-	tmp->f.offset = AST_FRIENDLY_OFFSET;
-	tmp->f.src = __PRETTY_FUNCTION__;
-	tmp->f.data = tmp->outbuf;
-	tmp->tail = 0;
-	return &tmp->f;
-}
-
-/*
- * LinToAdpcm_FrameIn
- *  Fill an input buffer with 16-bit signed linear PCM values.
- *
- * Results:
- *  None.
- *
- * Side effects:
- *  tmp->tail is number of signal values in the input buffer.
- */
-
-static int lintoadpcm_framein(struct ast_translator_pvt *pvt, struct ast_frame *f)
-{
-	struct adpcm_encoder_pvt *tmp = (struct adpcm_encoder_pvt *)pvt;
-
-	if ((tmp->tail + f->datalen / 2) < (sizeof(tmp->inbuf) / 2)) {
-		memcpy(&tmp->inbuf[tmp->tail], f->data, f->datalen);
-		tmp->tail += f->datalen / 2;
-	} else {
-		ast_log(LOG_WARNING, "Out of buffer space\n");
-		return -1;
-	}
-	return 0;
-}
-
-/*
- * LinToAdpcm_FrameOut
- *  Convert a buffer of raw 16-bit signed linear PCM to a buffer
- *  of 4-bit ADPCM packed two to a byte (Big Endian).
- *
- * Results:
- *  Foo
- *
- * Side effects:
- *  Leftover inbuf data gets packed, tail gets updated.
- */
-
-static struct ast_frame *lintoadpcm_frameout(struct ast_translator_pvt *pvt)
-{
-	struct adpcm_encoder_pvt *tmp = (struct adpcm_encoder_pvt *)pvt;
-	int i_max, i;
-  
-	if (tmp->tail < 2)
-		return NULL;
-
-	i_max = tmp->tail & ~1; /* atomic size is 2 samples */
-
-	/* What is this, state debugging? should be #ifdef'd then
-	tmp->outbuf[0] = tmp->ssindex & 0xff;
-	tmp->outbuf[1] = (tmp->signal >> 8) & 0xff;
-	tmp->outbuf[2] = (tmp->signal & 0xff);
-	tmp->outbuf[3] = tmp->zero_count;
-	tmp->outbuf[4] = tmp->next_flag;
-	*/
-	for (i = 0; i < i_max; i+=2) {
-		tmp->outbuf[i/2] =
+	pvt->samples &= ~1; /* atomic size is 2 samples */
+
+	for (i = 0; i < pvt->samples; i += 2) {
+		pvt->outbuf[i/2] =
 			(adpcm(tmp->inbuf[i  ], &tmp->state) << 4) |
 			(adpcm(tmp->inbuf[i+1], &tmp->state)     );
 	};
 
-	tmp->f.frametype = AST_FRAME_VOICE;
-	tmp->f.subclass = AST_FORMAT_ADPCM;
-	tmp->f.samples = i_max;
-	tmp->f.mallocd = 0;
-	tmp->f.offset = AST_FRIENDLY_OFFSET;
-	tmp->f.src = __PRETTY_FUNCTION__;
-	tmp->f.data = tmp->outbuf;
-	tmp->f.datalen = i_max / 2;
+	f = ast_trans_frameout(pvt, pvt->samples/2, 0);
 
 	/*
-	 * If there is a signal left over (there should be no more than
-	 * one) move it to the beginning of the input buffer.
+	 * If there is a left over sample, move it to the beginning
+	 * of the input buffer.
 	 */
 
-	if (tmp->tail == i_max)
-		tmp->tail = 0;
-	else {
-		tmp->inbuf[0] = tmp->inbuf[tmp->tail];
-		tmp->tail = 1;
-	}
-	return &tmp->f;
-}
-
-
-/*
- * AdpcmToLin_Sample
- */
-
+	if (samples & 1) {	/* move the leftover sample at beginning */
+		tmp->inbuf[0] = tmp->inbuf[samples - 1];
+		pvt->samples = 1;
+	}
+	return f;
+}
+
+
+/*! \brief AdpcmToLin_Sample */
 static struct ast_frame *adpcmtolin_sample(void)
 {
 	static struct ast_frame f;
@@ -485,10 +310,7 @@
 	return &f;
 }
 
-/*
- * LinToAdpcm_Sample
- */
-
+/*! \brief LinToAdpcm_Sample */
 static struct ast_frame *lintoadpcm_sample(void)
 {
 	static struct ast_frame f;
@@ -504,75 +326,51 @@
 	return &f;
 }
 
-/*
- * Adpcm_Destroy
- *  Destroys a private workspace.
- *
- * Results:
- *  It's gone!
- *
- * Side effects:
- *  None.
- */
-
-static void adpcm_destroy(struct ast_translator_pvt *pvt)
-{
-	free(pvt);
-	localusecnt--;
-	ast_update_use_count();
-}
-
-/*
- * The complete translator for ADPCMToLin.
- */
+struct ast_module_lock me = { .usecnt = -1 };
 
 static struct ast_translator adpcmtolin = {
-	"adpcmtolin",
-	AST_FORMAT_ADPCM,
-	AST_FORMAT_SLINEAR,
-	adpcmtolin_new,
-	adpcmtolin_framein,
-	adpcmtolin_frameout,
-	adpcm_destroy,
-	/* NULL */
-	adpcmtolin_sample
-};
-
-/*
- * The complete translator for LinToADPCM.
- */
+	.name = "adpcmtolin",
+	.srcfmt = AST_FORMAT_ADPCM,
+	.dstfmt = AST_FORMAT_SLINEAR,
+	.framein = adpcmtolin_framein,
+	.sample = adpcmtolin_sample,
+	.desc_size = sizeof(struct adpcm_decoder_pvt),
+	.buffer_samples = BUFFER_SAMPLES,
+	.buf_size = BUFFER_SAMPLES * 2,
+	.plc_samples = 160,
+	.lockp = &me,
+};
 
 static struct ast_translator lintoadpcm = {
-	"lintoadpcm",
-	AST_FORMAT_SLINEAR,
-	AST_FORMAT_ADPCM,
-	lintoadpcm_new,
-	lintoadpcm_framein,
-	lintoadpcm_frameout,
-	adpcm_destroy,
-	/* NULL */
-	lintoadpcm_sample
+	.name = "lintoadpcm",
+	.srcfmt = AST_FORMAT_SLINEAR,
+	.dstfmt = AST_FORMAT_ADPCM,
+	.framein = lintoadpcm_framein,
+	.frameout = lintoadpcm_frameout,
+	.sample = lintoadpcm_sample,
+	.desc_size = sizeof (struct adpcm_encoder_pvt),
+	.buffer_samples = BUFFER_SAMPLES,
+	.buf_size = BUFFER_SAMPLES/ 2,	/* 2 samples per byte */
+	.lockp = &me,
 };
 
 static void parse_config(void)
 {
-	struct ast_config *cfg;
+	struct ast_config *cfg = ast_config_load("codecs.conf");
 	struct ast_variable *var;
-	if ((cfg = ast_config_load("codecs.conf"))) {
-		if ((var = ast_variable_browse(cfg, "plc"))) {
-			while (var) {
-				if (!strcasecmp(var->name, "genericplc")) {
-					useplc = ast_true(var->value) ? 1 : 0;
-					if (option_verbose > 2)
-						ast_verbose(VERBOSE_PREFIX_3 "codec_adpcm: %susing generic PLC\n", useplc ? "" : "not ");
-				}
-				var = var->next;
-			}
+	if (cfg == NULL)
+		return;
+	for (var = ast_variable_browse(cfg, "plc"); var ; var = var->next) {
+		if (!strcasecmp(var->name, "genericplc")) {
+			adpcmtolin.useplc = ast_true(var->value) ? 1 : 0;
+			if (option_verbose > 2)
+				ast_verbose(VERBOSE_PREFIX_3 "codec_adpcm: %susing generic PLC\n", adpcmtolin.useplc ? "" : "not ");
 		}
-		ast_config_destroy(cfg);
-	}
-}
-
+	}
+	ast_config_destroy(cfg);
+}
+
+/*! \brief standard module glue */
 int reload(void)
 {
 	parse_config();
@@ -582,13 +380,12 @@
 int unload_module(void)
 {
 	int res;
-	ast_mutex_lock(&localuser_lock);
+	ast_mutex_lock(&me.lock);
 	res = ast_unregister_translator(&lintoadpcm);
-	if (!res)
-		res = ast_unregister_translator(&adpcmtolin);
-	if (localusecnt)
+	res |= ast_unregister_translator(&adpcmtolin);
+	if (me.usecnt)
 		res = -1;
-	ast_mutex_unlock(&localuser_lock);
+	ast_mutex_unlock(&me.lock);
 	return res;
 }
 
@@ -604,20 +401,14 @@
 	return res;
 }
 
-/*
- * Return a description of this module.
- */
-
 char *description(void)
 {
-	return tdesc;
+	return "Adaptive Differential PCM Coder/Decoder";
 }
 
 int usecount(void)
 {
-	int res;
-	OLD_STANDARD_USECOUNT(res);
-	return res;
+	return me.usecnt;
 }
 
 char *key()

Modified: trunk/codecs/codec_alaw.c
URL: http://svn.digium.com/view/asterisk/trunk/codecs/codec_alaw.c?rev=18541&r1=18540&r2=18541&view=diff
==============================================================================
--- trunk/codecs/codec_alaw.c (original)
+++ trunk/codecs/codec_alaw.c Sat Apr  8 16:40:57 2006
@@ -44,232 +44,42 @@
 #include "asterisk/alaw.h"
 #include "asterisk/utils.h"
 
-#define BUFFER_SIZE   8096	/* size for the translation buffers */
-
-AST_MUTEX_DEFINE_STATIC(localuser_lock);
-static int localusecnt = 0;
-
-static char *tdesc = "A-law Coder/Decoder";
-
-static int useplc = 0;
+#define BUFFER_SAMPLES   8096	/* size for the translation buffers */
 
 /* Sample frame data (Mu data is okay) */
 
 #include "slin_ulaw_ex.h"
 #include "ulaw_slin_ex.h"
 
-/*!
- * \brief Private workspace for translating signed linear signals to alaw.
- */
-struct alaw_encoder_pvt
+/*! \brief decode frame into lin and fill output buffer. */
+static int alawtolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
-	struct ast_frame f;
-	char offset[AST_FRIENDLY_OFFSET];   /*!< Space to build offset */
-	unsigned char outbuf[BUFFER_SIZE];  /*!< Encoded alaw, two nibbles to a word */
-	int tail;
-};
+	int i;
+	unsigned char *src = f->data;
+	int16_t *dst = (int16_t *)pvt->outbuf;
 
-/*!
- * \brief Private workspace for translating alaw signals to signed linear.
- */
-struct alaw_decoder_pvt
-{
-	struct ast_frame f;
-	char offset[AST_FRIENDLY_OFFSET];	/* Space to build offset */
-	short outbuf[BUFFER_SIZE];		/* Decoded signed linear values */
-	int tail;
-	plc_state_t plc;
-};
-
-/*!
- * \brief alawToLin_New
- *  Create a new instance of alaw_decoder_pvt.
- *
- * Results:
- *  Returns a pointer to the new instance.
- *
- * Side effects:
- *  None.
- */
-
-static struct ast_translator_pvt *alawtolin_new(void)
-{
-	struct alaw_decoder_pvt *tmp;
-	
-	if ((tmp = ast_calloc(1, sizeof(*tmp)))) {
-		tmp->tail = 0;
-		plc_init(&tmp->plc);
-		localusecnt++;
-		ast_update_use_count();
-	}
-
-	return (struct ast_translator_pvt *)tmp;
-}
-
-/*!
- * \brief LinToalaw_New
- *  Create a new instance of alaw_encoder_pvt.
- *
- * Results:
- *  Returns a pointer to the new instance.
- *
- * Side effects:
- *  None.
- */
-
-static struct ast_translator_pvt *lintoalaw_new(void)
-{
-	struct alaw_encoder_pvt *tmp;
-
-	if ((tmp = ast_calloc(1, sizeof(*tmp)))) {
-		localusecnt++;
-		ast_update_use_count();
-		tmp->tail = 0;
-	}
-
-	return (struct ast_translator_pvt *)tmp;
-}
-
-/*!
- * \brief alawToLin_FrameIn
- *  Fill an input buffer with packed 4-bit alaw values if there is room
- *  left.
- *
- * Results:
- *  Foo
- *
- * Side effects:
- *  tmp->tail is the number of packed values in the buffer.
- */
-
-static int alawtolin_framein(struct ast_translator_pvt *pvt, struct ast_frame *f)
-{
-	struct alaw_decoder_pvt *tmp = (struct alaw_decoder_pvt *)pvt;
-	int x;
-	unsigned char *b;
-
-	if(f->datalen == 0) { /* perform PLC with nominal framesize of 20ms/160 samples */
-		if((tmp->tail + 160) * 2 > sizeof(tmp->outbuf)) {
-			ast_log(LOG_WARNING, "Out of buffer space\n");
-			return -1;
-		}
-		if(useplc) {
-			plc_fillin(&tmp->plc, tmp->outbuf+tmp->tail, 160);
-			tmp->tail += 160;
-		}
-		return 0;
-	}
-
-	if ((tmp->tail + f->datalen) * 2 > sizeof(tmp->outbuf)) {
-		ast_log(LOG_WARNING, "Out of buffer space\n");
-		return -1;
-	}
-
-	/* Reset ssindex and signal to frame's specified values */
-	b = f->data;
-	for (x=0;x<f->datalen;x++)
-		tmp->outbuf[tmp->tail + x] = AST_ALAW(b[x]);
-
-	if(useplc)
-		plc_rx(&tmp->plc, tmp->outbuf+tmp->tail, f->datalen);
-
-	tmp->tail += f->datalen;
+	for ( i = 0; i < f->samples; i++)
+		dst[pvt->samples + i] = AST_ALAW(src[i]);
+	pvt->samples += f->samples;
+	pvt->datalen += 2*f->samples;	/* 2 bytes/sample */
 	return 0;
 }
 
-/*!
- * \brief alawToLin_FrameOut
- *  Convert 4-bit alaw encoded signals to 16-bit signed linear.
- *
- * Results:
- *  Converted signals are placed in tmp->f.data, tmp->f.datalen
- *  and tmp->f.samples are calculated.
- *
- * Side effects:
- *  None.
- */
+/*! \brief convert and store input samples in output buffer */
+static int lintoalaw_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
+{
+	int x;
+	char *dst = pvt->outbuf + pvt->samples;
+	int16_t *src = f->data;
 
-static struct ast_frame *alawtolin_frameout(struct ast_translator_pvt *pvt)
-{
-	struct alaw_decoder_pvt *tmp = (struct alaw_decoder_pvt *)pvt;
-
-	if (!tmp->tail)
-		return NULL;
-
-	tmp->f.frametype = AST_FRAME_VOICE;
-	tmp->f.subclass = AST_FORMAT_SLINEAR;
-	tmp->f.datalen = tmp->tail * 2;
-	tmp->f.samples = tmp->tail;
-	tmp->f.mallocd = 0;
-	tmp->f.offset = AST_FRIENDLY_OFFSET;
-	tmp->f.src = __PRETTY_FUNCTION__;
-	tmp->f.data = tmp->outbuf;
-	tmp->tail = 0;
-	return &tmp->f;
-}
-
-/*!
- * \brief LinToalaw_FrameIn
- *  Fill an input buffer with 16-bit signed linear PCM values.
- *
- * Results:
- *  None.
- *
- * Side effects:
- *  tmp->tail is number of signal values in the input buffer.
- */
-
-static int lintoalaw_framein(struct ast_translator_pvt *pvt, struct ast_frame *f)
-{
-	struct alaw_encoder_pvt *tmp = (struct alaw_encoder_pvt *)pvt;
-	int x;
-	short *s;
-	if (tmp->tail + f->datalen / 2 >= sizeof(tmp->outbuf)) {
-		ast_log(LOG_WARNING, "Out of buffer space\n");
-		return -1;
-	}
-	s = f->data;
-	for (x=0;x<f->datalen/2;x++) 
-		tmp->outbuf[x+tmp->tail] = AST_LIN2A(s[x]);
-	tmp->tail += f->datalen/2;
+	for ( x = 0; x < f->samples; x++) 
+		*dst++ = AST_LIN2A(src[x]);
+	pvt->samples += f->samples;
+	pvt->datalen += f->samples;	/* 1 byte/sample */
 	return 0;
 }
 
-/*!
- * \brief LinToalaw_FrameOut
- *  Convert a buffer of raw 16-bit signed linear PCM to a buffer
- *  of 4-bit alaw packed two to a byte (Big Endian).
- *
- * Results:
- *  Foo
- *
- * Side effects:
- *  Leftover inbuf data gets packed, tail gets updated.
- */
-
-static struct ast_frame *lintoalaw_frameout(struct ast_translator_pvt *pvt)
-{
-	struct alaw_encoder_pvt *tmp = (struct alaw_encoder_pvt *)pvt;
-  
-	if (tmp->tail) {
-		tmp->f.frametype = AST_FRAME_VOICE;
-		tmp->f.subclass = AST_FORMAT_ALAW;
-		tmp->f.samples = tmp->tail;
-		tmp->f.mallocd = 0;
-		tmp->f.offset = AST_FRIENDLY_OFFSET;
-		tmp->f.src = __PRETTY_FUNCTION__;
-		tmp->f.data = tmp->outbuf;
-		tmp->f.datalen = tmp->tail;
-		tmp->tail = 0;
-		return &tmp->f;
-	} else
-		return NULL;
-}
-
-/*!
- * \brief alawToLin_Sample
- */
-
+/*! \brief alawToLin_Sample */
 static struct ast_frame *alawtolin_sample(void)
 {
 	static struct ast_frame f;
@@ -284,17 +94,13 @@
 	return &f;
 }
 
-/*!
- * \brief LinToalaw_Sample
- */
-
+/*! \brief LinToalaw_Sample */
 static struct ast_frame *lintoalaw_sample(void)
 {
 	static struct ast_frame f;
 	f.frametype = AST_FRAME_VOICE;
 	f.subclass = AST_FORMAT_SLINEAR;
 	f.datalen = sizeof(slin_ulaw_ex);
-	/* Assume 8000 Hz */
 	f.samples = sizeof(slin_ulaw_ex) / 2;
 	f.mallocd = 0;
 	f.offset = 0;
@@ -303,75 +109,48 @@
 	return &f;
 }
 
-/*!
- * \brief alaw_Destroy
- *  Destroys a private workspace.
- *
- * Results:
- *  It's gone!
- *
- * Side effects:
- *  None.
- */
-
-static void alaw_destroy(struct ast_translator_pvt *pvt)
-{
-	free(pvt);
-	localusecnt--;
-	ast_update_use_count();
-}
-
-/*!
- * \brief The complete translator for alawToLin.
- */
+static struct ast_module_lock me = { .usecnt = -1 };
 
 static struct ast_translator alawtolin = {
-	"alawtolin",
-	AST_FORMAT_ALAW,
-	AST_FORMAT_SLINEAR,
-	alawtolin_new,
-	alawtolin_framein,
-	alawtolin_frameout,
-	alaw_destroy,
-	/* NULL */
-	alawtolin_sample
+	.name = "alawtolin",
+	.srcfmt = AST_FORMAT_ALAW,
+	.dstfmt = AST_FORMAT_SLINEAR,
+	.framein = alawtolin_framein,
+	.sample = alawtolin_sample,
+	.buffer_samples = BUFFER_SAMPLES,
+	.buf_size = BUFFER_SAMPLES * 2,
+	.plc_samples = 160,
+	.lockp = &me,
 };
-
-/*!
- * \brief The complete translator for LinToalaw.
- */
 
 static struct ast_translator lintoalaw = {
 	"lintoalaw",
-	AST_FORMAT_SLINEAR,
-	AST_FORMAT_ALAW,
-	lintoalaw_new,
-	lintoalaw_framein,
-	lintoalaw_frameout,
-	alaw_destroy,
-	/* NULL */
-	lintoalaw_sample
+	.srcfmt = AST_FORMAT_SLINEAR,
+	.dstfmt = AST_FORMAT_ALAW,
+	.framein = lintoalaw_framein,
+	.sample = lintoalaw_sample,
+	.buffer_samples = BUFFER_SAMPLES,
+	.buf_size = BUFFER_SAMPLES,
+	.lockp = &me,
 };
 
 static void parse_config(void)
 {
-	struct ast_config *cfg;
 	struct ast_variable *var;
+	struct ast_config *cfg = ast_config_load("codecs.conf");
+	if (!cfg)
+		return;
+	for (var = ast_variable_browse(cfg, "plc"); var; var = var->next) {
+		if (!strcasecmp(var->name, "genericplc")) {
+			alawtolin.useplc = ast_true(var->value) ? 1 : 0;
+			if (option_verbose > 2)
+				ast_verbose(VERBOSE_PREFIX_3 "codec_alaw: %susing generic PLC\n", alawtolin.useplc ? "" : "not ");
+		}
+	}
+	ast_config_destroy(cfg);
+}
 
-	if ((cfg = ast_config_load("codecs.conf"))) {
-		if ((var = ast_variable_browse(cfg, "plc"))) {
-			while (var) {
-				if (!strcasecmp(var->name, "genericplc")) {
-					useplc = ast_true(var->value) ? 1 : 0;
-					if (option_verbose > 2)
-						ast_verbose(VERBOSE_PREFIX_3 "codec_alaw: %susing generic PLC\n", useplc ? "" : "not ");
-				}
-				var = var->next;
-			}
-		}
-		ast_config_destroy(cfg);
-	}
-}
+/*! \brief standard module stuff */
 
 int reload(void)
 {
@@ -382,13 +161,12 @@
 int unload_module(void)
 {
 	int res;
-	ast_mutex_lock(&localuser_lock);
+	ast_mutex_lock(&me.lock);
 	res = ast_unregister_translator(&lintoalaw);
-	if (!res)
-		res = ast_unregister_translator(&alawtolin);
-	if (localusecnt)
+	res |= ast_unregister_translator(&alawtolin);
+	if (me.usecnt)
 		res = -1;
-	ast_mutex_unlock(&localuser_lock);
+	ast_mutex_unlock(&me.lock);
 	return res;
 }
 
@@ -404,20 +182,14 @@
 	return res;
 }
 
-/*
- * Return a description of this module.
- */
-
 char *description(void)
 {
-	return tdesc;
+	return "A-law Coder/Decoder";
 }
 
 int usecount(void)
 {
-	int res;
-	OLD_STANDARD_USECOUNT(res);
-	return res;
+	return me.usecnt;
 }
 
 char *key()

Modified: trunk/codecs/codec_g723_1.c
URL: http://svn.digium.com/view/asterisk/trunk/codecs/codec_g723_1.c?rev=18541&r1=18540&r2=18541&view=diff
==============================================================================
--- trunk/codecs/codec_g723_1.c (original)
+++ trunk/codecs/codec_g723_1.c Sat Apr  8 16:40:57 2006
@@ -74,14 +74,11 @@
 #include "slin_g723_ex.h"
 #include "g723_slin_ex.h"
 
-AST_MUTEX_DEFINE_STATIC(localuser_lock);
-static int localusecnt=0;
-
-#ifdef ANNEX_B
-static char *tdesc = "Annex B (floating point) G.723.1/PCM16 Codec Translator";
-#else
-static char *tdesc = "Annex A (fixed point) G.723.1/PCM16 Codec Translator";
-#endif
+/* g723_1 has 240 samples per buffer.
+ * We want a buffer which is a multiple...
+ */
+#define	G723_SAMPLES	240
+#define	BUFFER_SAMPLES	8160	/* 240 * 34 */
 
 /* Globals */
 Flag UsePf = True;
@@ -92,37 +89,20 @@
 
 struct g723_encoder_pvt {
 	struct cod_state cod;
-	struct ast_frame f;
-	/* Space to build offset */
-	char offset[AST_FRIENDLY_OFFSET];
-	/* Buffer for our outgoing frame */
-	char outbuf[8000];
-	/* Enough to store a full second */
-	short buf[8000];
-	int tail;
+	int16_t buf[BUFFER_SAMPLES];	/* input buffer */
 };
 
 struct g723_decoder_pvt {
 	struct dec_state dec;
-	struct ast_frame f;
-	/* Space to build offset */
-	char offset[AST_FRIENDLY_OFFSET];
-	/* Enough to store a full second */
-	short buf[8000];
-	int tail;
 };
 
-static struct ast_translator_pvt *g723tolin_new(void)
-{
-	struct g723_decoder_pvt *tmp;	
-	if ((tmp = ast_malloc(sizeof(*tmp)))) {
-		Init_Decod(&tmp->dec);
-	    Init_Dec_Cng(&tmp->dec);
-		tmp->tail = 0;
-		localusecnt++;
-		ast_update_use_count();
-	}
-	return (struct ast_translator_pvt *)tmp;
+static struct ast_trans_pvt *g723tolin_new(struct ast *pvt)
+{
+	struct g723_decoder_pvt *tmp = pvt;
+
+	Init_Decod(&tmp->dec);
+	Init_Dec_Cng(&tmp->dec);
+	return tmp;
 }
 
 static struct ast_frame *lintog723_sample(void)
@@ -131,7 +111,6 @@
 	f.frametype = AST_FRAME_VOICE;
 	f.subclass = AST_FORMAT_SLINEAR;
 	f.datalen = sizeof(slin_g723_ex);
-	/* Assume 8000 Hz */
 	f.samples = sizeof(slin_g723_ex)/2;
 	f.mallocd = 0;
 	f.offset = 0;
@@ -155,53 +134,16 @@
 	return &f;
 }
 
-static struct ast_translator_pvt *lintog723_new(void)
-{
-	struct g723_encoder_pvt *tmp;	
-	if ((tmp = ast_malloc(sizeof(*tmp)))) {
-		Init_Coder(&tmp->cod);
-	    /* Init Comfort Noise Functions */
-   		 if( UseVx ) {
-   	   		Init_Vad(&tmp->cod);
-        	Init_Cod_Cng(&tmp->cod);
-    	 }
-		localusecnt++;
-		ast_update_use_count();
-		tmp->tail = 0;
-	}
-	return (struct ast_translator_pvt *)tmp;
-}
-
-static struct ast_frame *g723tolin_frameout(struct ast_translator_pvt *pvt)
-{
-	struct g723_decoder_pvt *tmp = (struct g723_decoder_pvt *)pvt;
-	if (!tmp->tail)
-		return NULL;
-	/* Signed linear is no particular frame size, so just send whatever
-	   we have in the buffer in one lump sum */
-	tmp->f.frametype = AST_FRAME_VOICE;
-	tmp->f.subclass = AST_FORMAT_SLINEAR;
-	tmp->f.datalen = tmp->tail * 2;
-	/* Assume 8000 Hz */
-	tmp->f.samples = tmp->tail;
-	tmp->f.mallocd = 0;
-	tmp->f.offset = AST_FRIENDLY_OFFSET;
-	tmp->f.src = __PRETTY_FUNCTION__;
-	tmp->f.data = tmp->buf;
-	/* Reset tail pointer */
-	tmp->tail = 0;
-
-#if 0
-	/* Save the frames */
-	{ 
-		static int fd2 = -1;
-		if (fd2 == -1) {
-			fd2 = open("g723.example", O_WRONLY | O_CREAT | O_TRUNC, 0644);
-		}
-		write(fd2, tmp->f.data, tmp->f.datalen);
-	} 		
-#endif
-	return &tmp->f;	
+static void *lintog723_new(void *pvt)
+{
+	struct g723_encoder_pvt *tmp = pvt;
+	Init_Coder(&tmp->cod);
+	/* Init Comfort Noise Functions */
+	if( UseVx ) {
+		Init_Vad(&tmp->cod);
+		Init_Cod_Cng(&tmp->cod);
+	}
+	return tmp;
 }
 
 static int g723_len(unsigned char buf)
@@ -225,19 +167,22 @@
 	return -1;
 }
 
-static int g723tolin_framein(struct ast_translator_pvt *pvt, struct ast_frame *f)
-{
-	struct g723_decoder_pvt *tmp = (struct g723_decoder_pvt *)pvt;
+static int g723tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
+{
+	struct g723_decoder_pvt *tmp = pvt->pvt;
 	int len = 0;
 	int res;
+	int16_t *dst = pvt->outbuf;
 #ifdef  ANNEX_B
 	FLOAT tmpdata[Frame];
 	int x;
 #endif
+	unsigned char *src = f->data;
+
 	while(len < f->datalen) {
 		/* Assuming there's space left, decode into the current buffer at
 		   the tail location */
-		res = g723_len(((unsigned char *)f->data + len)[0]);
+		res = g723_len(src[len]);
 		if (res < 0) {
 			ast_log(LOG_WARNING, "Invalid data\n");
 			return -1;
@@ -246,145 +191,127 @@
 			ast_log(LOG_WARNING, "Measured length exceeds frame length\n");
 			return -1;
 		}
-		if (tmp->tail + Frame < sizeof(tmp->buf)/2) {	
-#ifdef ANNEX_B
-			Decod(&tmp->dec, tmpdata, f->data + len, 0);
-			for (x=0;x<Frame;x++)
-				(tmp->buf + tmp->tail)[x] = (short)(tmpdata[x]); 
-#else
-			Decod(&tmp->dec, tmp->buf + tmp->tail, f->data + len, 0);
-#endif
-			tmp->tail+=Frame;
-		} else {
+		if (pvt->samples + Frame > BUFFER_SAMPLES) {	
 			ast_log(LOG_WARNING, "Out of buffer space\n");
 			return -1;
 		}
+#ifdef ANNEX_B
+		Decod(&tmp->dec, tmpdata, f->data + len, 0);
+		for (x=0;x<Frame;x++)
+			dst[pvt->samples + x] = (int16_t)(tmpdata[x]); 
+#else
+		Decod(&tmp->dec, dst + pvt->samples, f->data + len, 0);
+#endif
+		pvt->samples += Frame;
+		pvt->datalen += 2*Frame; /* 2 bytes/sample */
 		len += res;
 	}
 	return 0;
 }
 
-static int lintog723_framein(struct ast_translator_pvt *pvt, struct ast_frame *f)
+static int lintog723_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	/* Just add the frames to our stream */
 	/* XXX We should look at how old the rest of our stream is, and if it
 	   is too old, then we should overwrite it entirely, otherwise we can
 	   get artifacts of earlier talk that do not belong */
-	struct g723_encoder_pvt *tmp = (struct g723_encoder_pvt *)pvt;
-	if (tmp->tail + f->datalen/2 < sizeof(tmp->buf) / 2) {
-		memcpy(&tmp->buf[tmp->tail], f->data, f->datalen);
-		tmp->tail += f->datalen/2;
-	} else {
+	struct g723_encoder_pvt *tmp = pvt->pvt;
+
+	if (tmp->samples + f->samples > BUFFER_SAMPLES) {
 		ast_log(LOG_WARNING, "Out of buffer space\n");
 		return -1;
 	}
+	memcpy(&tmp->buf[pvt->samples], f->data, f->datalen);
+	pvt->samples += f->samples;
 	return 0;
 }
 
-static struct ast_frame *lintog723_frameout(struct ast_translator_pvt *pvt)
+static struct ast_frame *lintog723_frameout(void *pvt)
 {
 	struct g723_encoder_pvt *tmp = (struct g723_encoder_pvt *)pvt;
+	int samples = 0;	/* how many samples in buffer */
 #ifdef ANNEX_B
 	int x;
 	FLOAT tmpdata[Frame];
 #endif
-	int cnt=0;
+	int cnt = 0;	/* how many bytes so far */
+
 	/* We can't work on anything less than a frame in size */
-	if (tmp->tail < Frame)
+	if (pvt->samples < Frame)
 		return NULL;
-	tmp->f.frametype = AST_FRAME_VOICE;
-	tmp->f.subclass = AST_FORMAT_G723_1;
-	tmp->f.offset = AST_FRIENDLY_OFFSET;
-	tmp->f.src = __PRETTY_FUNCTION__;
-	tmp->f.samples = 0;
-	tmp->f.mallocd = 0;
-	while(tmp->tail >= Frame) {
+	while (pvt->samples >= Frame) {
 		/* Encode a frame of data */
-		if (cnt + 24 >= sizeof(tmp->outbuf)) {
+		/* at most 24 bytes/frame... */
+		if (cnt + 24 > pvt->buf_size) {
 			ast_log(LOG_WARNING, "Out of buffer space\n");
 			return NULL;
 		}
 #ifdef ANNEX_B
-		for (x=0;x<Frame;x++)
+		for ( x = 0; x < Frame ; x++)
 			tmpdata[x] = tmp->buf[x];
-		Coder(&tmp->cod, tmpdata, tmp->outbuf + cnt);
-#else
-		Coder(&tmp->cod, tmp->buf, tmp->outbuf + cnt);
+		Coder(&tmp->cod, tmpdata, pvt->outbuf + cnt);
+#else
+		Coder(&tmp->cod, tmp->buf, pvt->outbuf + cnt);
 #endif
 		/* Assume 8000 Hz */
-		tmp->f.samples += 240;
+		samples += G723_SAMPLES;
 		cnt += g723_len(tmp->outbuf[cnt]);
-		tmp->tail -= Frame;
+		pvt->samples -= Frame;
 		/* Move the data at the end of the buffer to the front */
-		if (tmp->tail)
-			memmove(tmp->buf, tmp->buf + Frame, tmp->tail * 2);
-	}
-	tmp->f.datalen = cnt;
-	tmp->f.data = tmp->outbuf;
-#if 0
-	/* Save to a g723 sample output file... */
-	{ 
-		static int fd = -1;
-		int delay = htonl(30);
-		short size;
-		if (fd < 0)
-			fd = open("trans.g723", O_WRONLY | O_CREAT | O_TRUNC, 0644);
-		if (fd < 0)
-			ast_log(LOG_WARNING, "Unable to create demo\n");
-		write(fd, &delay, 4);
-		size = htons(tmp->f.datalen);
-		write(fd, &size, 2);
-		write(fd, tmp->f.data, tmp->f.datalen);
-	}
-#endif
-	return &tmp->f;	
-}
-
-static void g723_destroy(struct ast_translator_pvt *pvt)
-{
-	free(pvt);
-	localusecnt--;
-	ast_update_use_count();
-}
-
-static struct ast_translator g723tolin =
-#ifdef ANNEX_B
-	{ "g723tolinb", 
-#else
-	{ "g723tolin", 
-#endif
-	   AST_FORMAT_G723_1, AST_FORMAT_SLINEAR,
-	   g723tolin_new,
-	   g723tolin_framein,
-	   g723tolin_frameout,
-	   g723_destroy,
-	   g723tolin_sample
-	   };
-
-static struct ast_translator lintog723 =
-#ifdef ANNEX_B
-	{ "lintog723b", 
-#else
-	{ "lintog723", 
-#endif
-	   AST_FORMAT_SLINEAR, AST_FORMAT_G723_1,
-	   lintog723_new,
-	   lintog723_framein,
-	   lintog723_frameout,
-	   g723_destroy,
-	   lintog723_sample
-	   };
+		/* XXX inefficient... */
+		if (pvt->samples)
+			memmove(tmp->buf, tmp->buf + Frame, pvt->samples * 2);
+	}
+	return ast_trans_frameout(pvt, cnt, samples);
+}
+
+static struct ast_module_lock me = { .usecnt = -1 };
+
+static struct ast_translator g723tolin = {
+	.name =
+#ifdef ANNEX_B
+	"g723btolin", 
+#else
+	"g723tolin", 
+#endif
+	.srcfmt = AST_FORMAT_G723_1,
+	.dstfmt =  AST_FORMAT_SLINEAR,
+	.newpvt = g723tolin_new,
+	.framein = g723tolin_framein,
+	.sample = g723tolin_sample,
+	.desc_size = sizeof(struct ...),
+	.lockp = &me,
+};
+
+static struct ast_translator lintog723 = {
+	.name =
+#ifdef ANNEX_B
+	"lintog723b", 
+#else
+	"lintog723", 
+#endif
+	.srcfmt = AST_FORMAT_SLINEAR,
+	.dstfmt =  AST_FORMAT_G723_1,
+	.new = lintog723_new,
+	.framein = lintog723_framein,
+	.frameout = lintog723_frameout,
+	.destroy = g723_destroy,
+	.sample = lintog723_sample,
+	.lockp = &me,
+	.desc_size = sizeof(struct ...),
+};
+
+/*! \brief standard module glue */
 
 int unload_module(void)
 {
 	int res;
-	ast_mutex_lock(&localuser_lock);
+	ast_mutex_lock(&me.lock);

[... 4075 lines stripped ...]


More information about the asterisk-commits mailing list