[asterisk-commits] branch mattf/asterisk-wideband - r7817 in /team/mattf/asterisk-wideband: ./ c...

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Thu Jan 5 11:17:32 CST 2006


Author: mattf
Date: Thu Jan  5 11:17:31 2006
New Revision: 7817

URL: http://svn.digium.com/view/asterisk?rev=7817&view=rev
Log:
Finished up quick and dirty library based reampling codec (get libresample to use it).

Modified:
    team/mattf/asterisk-wideband/codecs/Makefile
    team/mattf/asterisk-wideband/codecs/codec_resample.c
    team/mattf/asterisk-wideband/frame.c
    team/mattf/asterisk-wideband/translate.c

Modified: team/mattf/asterisk-wideband/codecs/Makefile
URL: http://svn.digium.com/view/asterisk/team/mattf/asterisk-wideband/codecs/Makefile?rev=7817&r1=7816&r2=7817&view=diff
==============================================================================
--- team/mattf/asterisk-wideband/codecs/Makefile (original)
+++ team/mattf/asterisk-wideband/codecs/Makefile Thu Jan  5 11:17:31 2006
@@ -119,7 +119,7 @@
 	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} $(LIBLPC10) -lm
 
 codec_resample.so: codec_resample.o
-	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB}
+	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB} libresample.a
 
 %.so : %.o
 	$(CC) $(SOLINK) -o $@ ${CYGSOLINK} $< ${CYGSOLIB}

Modified: team/mattf/asterisk-wideband/codecs/codec_resample.c
URL: http://svn.digium.com/view/asterisk/team/mattf/asterisk-wideband/codecs/codec_resample.c?rev=7817&r1=7816&r2=7817&view=diff
==============================================================================
--- team/mattf/asterisk-wideband/codecs/codec_resample.c (original)
+++ team/mattf/asterisk-wideband/codecs/codec_resample.c Thu Jan  5 11:17:31 2006
@@ -20,102 +20,183 @@
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 7221 $")
 
+#include <stdlib.h>
 #include "asterisk/lock.h"
 #include "asterisk/logger.h"
 #include "asterisk/module.h"
 #include "asterisk/translate.h"
 #include "asterisk/channel.h"
+#include "libresample.h"
 
 AST_MUTEX_DEFINE_STATIC(localuser_lock);
 static int localusecnt = 0;
 
+static char *tdesc = "Asterisk Resampling codec (Powered by libresample)";
+#define TRAN_BUF_SIZE 1024
+
+static short example_frame[320] = { 0, };
+
 struct ast_translator_pvt {
+	float ibuf[TRAN_BUF_SIZE];
+	float obuf[TRAN_BUF_SIZE];
+	float resamplefactor;
+	int isamples;
+	int osamples;
+	void *resample_pvt;
 	struct ast_frame f;
 	char offset[AST_FRIENDLY_OFFSET];
-	short outbuf[16000];
-	short buf[16000];
-	short lastsample;
+	short sampbuf[TRAN_BUF_SIZE];
 	int tail;
 };
 
-static struct ast_frame * lin8to16_frameout(struct ast_translator_pvt *tmp)
-{
-	return NULL;
-}
-
-static int lin8to16_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
-{
+static struct ast_frame * resample_frameout(struct ast_translator_pvt *tmp)
+{
+	int i;
+	if (!tmp->osamples)
+		return NULL;
+
+	/* Convert to short */
+	for (i = 0; i < tmp->osamples; i++)
+		tmp->sampbuf[i] = (short) tmp->obuf[i];
+
+	tmp->f.frametype = AST_FRAME_VOICE;
+	tmp->f.subclass = AST_FORMAT_SLINEAR16;
+	tmp->f.datalen = tmp->osamples * 2;
+	tmp->f.samples = tmp->osamples;
+	/* Reset our tail */
+	tmp->osamples = 0;
+
+	tmp->f.mallocd = 0;
+	tmp->f.offset = AST_FRIENDLY_OFFSET;
+	tmp->f.src = __PRETTY_FUNCTION__;
+	tmp->f.data = tmp->sampbuf;
+
+	return &tmp->f;
+}
+
+static int resample_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
+{
+	int i;
+	int isamples;
+	short *data = f->data;
+
+	tmp->isamples = f->samples;
+	/* Convert the samples to floats */
+	for (i = 0; i < f->samples; i++)
+		tmp->ibuf[i] = (float) data[i];
+
+	tmp->osamples = resample_process(tmp->resample_pvt, tmp->resamplefactor, tmp->ibuf, tmp->isamples, 0, &isamples, tmp->obuf, tmp->isamples * tmp->resamplefactor);
+
+	if (isamples != tmp->isamples)
+		ast_log(LOG_DEBUG, "Resampler only used %d of %d samples input!\n", isamples, tmp->isamples);
+
+	if (tmp->osamples != (int)(tmp->isamples * tmp->resamplefactor))
+		ast_log(LOG_DEBUG, "Resampler output %d samples, when we should have got %d samples!\n", tmp->osamples, (int)(tmp->isamples * tmp->resamplefactor));
+
 	return 0;
 }
 
 static struct ast_translator_pvt * lin8to16_new(void)
 {
-	return NULL;
+	struct ast_translator_pvt *h;
+	h = malloc(sizeof(struct ast_translator_pvt));
+	if (h) {
+		memset(h, 0, sizeof(struct ast_translator_pvt));
+		h->resamplefactor = 2;
+		h->resample_pvt = resample_open(0, h->resamplefactor, h->resamplefactor);
+		if (!h->resample_pvt) {
+			free(h);
+			return NULL;
+		}
+		localusecnt++;
+		ast_update_use_count();
+	}
+	return h;
 }
 
 static struct ast_frame *lin8to16_sample(void)
 {
-	return NULL;
-}
-
-static void lin8to16_destroy(struct ast_translator_pvt *pvt)
-{
+	static struct ast_frame f;
+	f.frametype = AST_FRAME_VOICE;
+	f.subclass = AST_FORMAT_SLINEAR;
+	f.datalen = 160 * 2;
+	f.samples = 160;
+	f.mallocd = 0;
+	f.offset = AST_FRIENDLY_OFFSET;
+	f.src = __PRETTY_FUNCTION__;
+	f.data = example_frame;
+	return &f;
+}
+
+static void resample_destroy(struct ast_translator_pvt *pvt)
+{
+	resample_close(pvt->resample_pvt);
+	localusecnt--;
+	free(pvt);
+	ast_update_use_count();
 	return;
 }
 
+static struct ast_translator_pvt * lin16to8_new(void)
+{
+	struct ast_translator_pvt *h = NULL;
+	h = malloc(sizeof(struct ast_translator_pvt));
+	if (h) {
+		memset(h, 0, sizeof(struct ast_translator_pvt));
+		h->resamplefactor = .5;
+		h->resample_pvt = resample_open(0, h->resamplefactor, h->resamplefactor);
+		if (!h->resample_pvt) {
+			free(h);
+			return NULL;
+		}
+		localusecnt++;
+		ast_update_use_count();
+	}
+	return h;
+}
+
+static struct ast_frame *lin16to8_sample(void)
+{
+	static struct ast_frame f;
+	f.frametype = AST_FRAME_VOICE;
+	f.subclass = AST_FORMAT_SLINEAR16;
+	f.datalen = 320 * 2;
+	f.samples = 320;
+	f.mallocd = 0;
+	f.offset = AST_FRIENDLY_OFFSET;
+	f.src = __PRETTY_FUNCTION__;
+	f.data = example_frame;
+	return &f;
+}
+
+int reload(void)
+{
+	return 0;
+}
+
 static struct ast_translator lin8to16 =
 {
-		"lin8tolin16",
-		AST_FORMAT_SLINEAR,
-		AST_FORMAT_SLINEAR16,
-		lin8to16_new,
-		lin8to16_framein,
-		lin8to16_frameout,
-		lin8to16_destroy,
-		lin8to16_sample,
+	"lin8tolin16",
+	AST_FORMAT_SLINEAR,
+	AST_FORMAT_SLINEAR16,
+	lin8to16_new,
+	resample_framein,
+	resample_frameout,
+	resample_destroy,
+	lin8to16_sample,
 };
 
-static struct ast_frame * lin16to8_frameout(struct ast_translator_pvt *tmp)
-{
-	return NULL;
-}
-
-static int lin16to8_framein(struct ast_translator_pvt *tmp, struct ast_frame *f)
-{
-	return 0;
-}
-
-static struct ast_translator_pvt * lin16to8_new(void)
-{
-	return NULL;
-}
-
-static struct ast_frame *lin16to8_sample(void)
-{
-	return NULL;
-}
-
-static void lin16to8_destroy(struct ast_translator_pvt *pvt)
-{
-	return;
-}
-
 static struct ast_translator lin16to8 =
 {
-		"lin16tolin8",
-		AST_FORMAT_SLINEAR,
-		AST_FORMAT_SLINEAR16,
-		lin16to8_new,
-		lin16to8_framein,
-		lin16to8_frameout,
-		lin16to8_destroy,
-		lin16to8_sample,
+	"lin16tolin8",
+	AST_FORMAT_SLINEAR16,
+	AST_FORMAT_SLINEAR,
+	lin16to8_new,
+	resample_framein,
+	resample_frameout,
+	resample_destroy,
+	lin16to8_sample,
 };
-
-int reload(void)
-{
-	return 0;
-}
 
 int unload_module(void)
 {
@@ -146,3 +227,20 @@
 
 	return res;
 }
+
+char * description(void)
+{
+	return tdesc;
+}
+
+int usecount(void)
+{
+	int res;
+	STANDARD_USECOUNT (res);
+	return res;
+}
+
+char * key(void)
+{
+	return ASTERISK_GPL_KEY;
+}

Modified: team/mattf/asterisk-wideband/frame.c
URL: http://svn.digium.com/view/asterisk/team/mattf/asterisk-wideband/frame.c?rev=7817&r1=7816&r2=7817&view=diff
==============================================================================
--- team/mattf/asterisk-wideband/frame.c (original)
+++ team/mattf/asterisk-wideband/frame.c Thu Jan  5 11:17:31 2006
@@ -90,7 +90,7 @@
 	{ 1, AST_FORMAT_LPC10, "lpc10", "LPC10", 8000 },	/*!< codec_lpc10.c */
 	{ 1, AST_FORMAT_G729A, "g729", "G.729A", 8000 },	/*!< Binary commercial distribution */
 	{ 1, AST_FORMAT_SPEEX, "speex", "SpeeX", 8000 },	/*!< codec_speex.c */
-	{ 1, AST_FORMAT_ILBC, "ilbc", "iLBC", 8000},	/*!< codec_ilbc.c */
+	{ 1, AST_FORMAT_ILBC, "ilbc", "iLBC", 8000 },	/*!< codec_ilbc.c */
 	{ 1, AST_FORMAT_SLINEAR16, "slin16", "16 bit Signed Linear PCM (16khz)", 16000 },
 	{ 0, 0, "nothing", "undefined", -1 },
 	{ 0, 0, "nothing", "undefined", -1 },
@@ -403,6 +403,7 @@
 
 	for (i = 0; codec; i++)
 		codec >>= 1;
+	i -= 1;
 
 	return AST_FORMAT_LIST[i].samplerate;
 }

Modified: team/mattf/asterisk-wideband/translate.c
URL: http://svn.digium.com/view/asterisk/team/mattf/asterisk-wideband/translate.c?rev=7817&r1=7816&r2=7817&view=diff
==============================================================================
--- team/mattf/asterisk-wideband/translate.c (original)
+++ team/mattf/asterisk-wideband/translate.c Thu Jan  5 11:17:31 2006
@@ -225,7 +225,7 @@
 	struct ast_translator_pvt *pvt;
 	struct ast_frame *f, *out;
 	struct timeval start;
-	int cost, rate;
+	float cost, rate;
 
 	if(!samples)
 		samples = 1;
@@ -275,7 +275,7 @@
 	cost = ast_tvdiff_ms(ast_tvnow(), start);
 	t->destroy(pvt);
 	/* New cost */
-	t->cost = cost / (samples / (rate / 8000));
+	t->cost = (int) (cost / (samples / (rate / 8000)));
 	if (!t->cost)
 		t->cost = 1;
 }
@@ -339,9 +339,9 @@
 /*! \brief CLI "show translation" command handler */
 static int show_translation(int fd, int argc, char *argv[])
 {
-#define SHOW_TRANS 11
+#define SHOW_TRANS 12
 	int x, y, z;
-	char line[80];
+	char line[100];
 	if (argc > 4) 
 		return RESULT_SHOWUSAGE;
 



More information about the asterisk-commits mailing list