[asterisk-commits] dvossel: branch dvossel/celt_codec_ftw r313139 - in /team/dvossel/celt_codec_...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Fri Apr 8 12:54:13 CDT 2011


Author: dvossel
Date: Fri Apr  8 12:54:10 2011
New Revision: 313139

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=313139
Log:
CELT codec translator code update

Modified:
    team/dvossel/celt_codec_ftw/codecs/codec_celt.c
    team/dvossel/celt_codec_ftw/include/asterisk/celt.h

Modified: team/dvossel/celt_codec_ftw/codecs/codec_celt.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/celt_codec_ftw/codecs/codec_celt.c?view=diff&rev=313139&r1=313138&r2=313139
==============================================================================
--- team/dvossel/celt_codec_ftw/codecs/codec_celt.c (original)
+++ team/dvossel/celt_codec_ftw/codecs/codec_celt.c Fri Apr  8 12:54:10 2011
@@ -27,6 +27,7 @@
  */
 
 #include "asterisk.h"
+#include "speex/speex_resampler.h"
 
 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
 
@@ -50,27 +51,146 @@
 	AST_FORMAT_SLINEAR96,
 };
 
-static int resamp_new(struct ast_trans_pvt *pvt)
-{
-	int err;
-/*
-	TODO
-	if (!(pvt->pvt = alloc pvt stuff here)) {
-		return -1;
-	}
-*/
-	return 0;
-}
-
-static void resamp_destroy(struct ast_trans_pvt *pvt)
-{
-	/* TODO Destroy pvt stuff here */
-}
-
-
-static int resamp_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
-{
+struct celt_encoder_pvt {
+	int init;
+	CELTMode *mode;
+	CELTEncoder *enc;
+	SpeexResamplerState *resamp;
+};
+struct celt_decoder_pvt {
+	int init;
+	CELTMode *mode;
+	CELTDecoder *dec;
+	SpeexResamplerState *resamp;
+};
+
+static int celt_enc_set(struct ast_trans_pvt *pvt, struct ast_format *slin_src)
+{
+	struct celt_encoder_pvt *enc = pvt->pvt;
+	int slin_rate = ast_format_rate(slin_src);
+	int celt_rate = slin_rate;
+	int frame_size = AST_CELT_DEFAULT_FRAME_SIZE;
+	int error = 0;
+
+	if (pvt->explicit_dst.id) {
+		int val;
+		celt_rate = ast_format_rate(&pvt->explicit_dst);
+		if (!ast_format_get_value(&pvt->explicit_dst, CELT_ATTR_KEY_FRAME_SIZE, &val) && val > 0) {
+			frame_size = val;
+		}
+	}
+
+	if (slin_rate != celt_rate) {
+		/*TODO if src rate not equal dst rate, magic */
+	}
+
+	if (!(enc->mode = celt_mode_create(celt_rate, frame_size, &error))) {
+		ast_log(LOG_WARNING, "Failed to create CELT encoder mode, error code %d\n", error);
+		return -1;
+	}
+
+	if (!(enc->enc = celt_encoder_create(enc->mode, 1, &error))) {
+		ast_log(LOG_WARNING, "Failed to create CELT encoder, error code %d\n", error);
+		celt_mode_destroy(enc->mode);
+		enc->mode = NULL;
+		return -1;
+	}
+
+	enc->init = 1;
+
+	return 0;
+}
+
+static int celt_dec_set(struct ast_trans_pvt *pvt, struct ast_format *celt_src)
+{
+	struct celt_decoder_pvt *dec = pvt->pvt;
+	int celt_rate = ast_format_rate(celt_src);
+	int slin_rate = ast_format_rate(&pvt->t->dst_format);
+	int frame_size = AST_CELT_DEFAULT_FRAME_SIZE;
+	int val;
+	int error = 0;
+
+	if (!ast_format_get_value(celt_src, CELT_ATTR_KEY_FRAME_SIZE, &val) && val > 0) {
+		frame_size = val;
+	}
+
+	if (slin_rate != celt_rate) {
+		/*TODO if src rate not equal dst rate, magic */
+	}
+
+	if (!(dec->mode = celt_mode_create(celt_rate, frame_size, &error))) {
+		ast_log(LOG_WARNING, "Failed to create CELT decoder mode, error code %d\n", error);
+		return -1;
+	}
+
+	if (!(dec->dec = celt_decoder_create(dec->mode, 1, &error))) {
+		ast_log(LOG_WARNING, "Failed to create CELT decoder, error code %d\n", error);
+		celt_mode_destroy(dec->mode);
+		dec->mode = NULL;
+		return -1;
+	}
+
+	dec->init = 1;
+
+	return 0;
+}
+
+static void celt_enc_destroy(struct ast_trans_pvt *pvt)
+{
+	struct celt_encoder_pvt *enc = pvt->pvt;
+
+	if (enc->enc) {
+		celt_encoder_destroy(enc->enc);
+		enc->enc = NULL;
+	}
+	if (enc->mode) {
+		celt_mode_destroy(enc->mode);
+		enc->mode = NULL;
+	}
+	if (enc->resamp) {
+		speex_resampler_destroy(enc->resamp);
+	}
+}
+
+static void celt_dec_destroy(struct ast_trans_pvt *pvt)
+{
+	struct celt_decoder_pvt *dec = pvt->pvt;
+
+	if (dec->dec) {
+		celt_decoder_destroy(dec->dec);
+		dec->dec = NULL;
+	}
+	if (dec->mode) {
+		celt_mode_destroy(dec->mode);
+		dec->mode = NULL;
+	}
+	if (dec->resamp) {
+		speex_resampler_destroy(dec->resamp);
+	}
+}
+
+static int celt_dec_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
+{
+	struct celt_decoder_pvt *dec = pvt->pvt;
+	if (!dec->init) {
+		celt_dec_set(pvt, &f->subclass.format);
+	}
+
 	/* TODO make awesome stuff happen here */
+	return 0;
+}
+
+static int celt_enc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
+{
+	struct celt_decoder_pvt *enc = pvt->pvt;
+
+	if (!enc->init) {
+		celt_enc_set(pvt, &f->subclass.format);
+	}
+
+
+	/* TODO make awesome stuff happen here */
+	return 0;
 }
 
 static int unload_module(void)
@@ -91,13 +211,43 @@
 	int res = 0;
 	int x, idx = 0;
 
-	trans_size = ARRAY_LEN(id_list) * 2
+	/* We need a translator between every slin fmt to CELT, and CELT to every slin fmt */
+	trans_size = ARRAY_LEN(id_list) * 2;
 	if (!(translators = ast_calloc(1, sizeof(struct ast_translator) * trans_size))) {
 		return AST_MODULE_LOAD_FAILURE;
 	}
 
 	for (x = 0; x < ARRAY_LEN(id_list); x++) {
-		/* TODO create translators and register them. */
+
+		/* SLIN to CELT */
+		translators[idx].newpvt = NULL; /* ENC is created on first frame */
+		translators[idx].destroy = celt_enc_destroy;
+		translators[idx].framein = celt_enc_framein;
+		translators[idx].desc_size = sizeof(struct celt_encoder_pvt);
+		translators[idx].buffer_samples = (OUTBUF_SIZE / sizeof(int16_t));
+		translators[idx].buf_size = OUTBUF_SIZE;
+		ast_format_set(&translators[idx].src_format, id_list[x], 0);
+		ast_format_set(&translators[idx].dst_format, AST_FORMAT_CELT, 0);
+		snprintf(translators[idx].name, sizeof(translators[idx].name), "slin %dkhz -> CELT",
+			ast_format_rate(&translators[idx].src_format));
+		res |= ast_register_translator(&translators[idx]);
+
+		idx++;
+
+		/* CELT to SLIN */
+		translators[idx].newpvt = NULL; /* DEC is created on first frame */
+		translators[idx].destroy = celt_dec_destroy;
+		translators[idx].framein = celt_dec_framein;
+		translators[idx].desc_size = sizeof(struct celt_decoder_pvt);
+		translators[idx].buffer_samples = (OUTBUF_SIZE / sizeof(int16_t));
+		translators[idx].buf_size = OUTBUF_SIZE;
+		ast_format_set(&translators[idx].src_format, AST_FORMAT_CELT, 0);
+		ast_format_set(&translators[idx].dst_format, id_list[x], 0);
+		snprintf(translators[idx].name, sizeof(translators[idx].name), "CELT -> slin %dkhz",
+			ast_format_rate(&translators[idx].dst_format));
+		res |= ast_register_translator(&translators[idx]);
+
+		idx++;
 	}
 
 	return res;

Modified: team/dvossel/celt_codec_ftw/include/asterisk/celt.h
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/celt_codec_ftw/include/asterisk/celt.h?view=diff&rev=313139&r1=313138&r2=313139
==============================================================================
--- team/dvossel/celt_codec_ftw/include/asterisk/celt.h (original)
+++ team/dvossel/celt_codec_ftw/include/asterisk/celt.h Fri Apr  8 12:54:10 2011
@@ -25,7 +25,7 @@
 #ifndef _AST_FORMAT_CELT_H_
 #define _AST_FORMAT_CELT_H_
 
-#define AST_CELT_DEFAULT_FRAME_SIZE = 480
+#define AST_CELT_DEFAULT_FRAME_SIZE 480
 
 /*! CELT format attribute key value pairs, all are accessible through ast_format_get_value()*/
 enum celt_attr_keys {




More information about the asterisk-commits mailing list