[asterisk-commits] dvossel: branch dvossel/opus_codec_ftw r330489 - in /team/dvossel/opus_codec_...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Aug 1 15:48:14 CDT 2011


Author: dvossel
Date: Mon Aug  1 15:48:11 2011
New Revision: 330489

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=330489
Log:
Fixes OPUS interop issues found during testing

Modified:
    team/dvossel/opus_codec_ftw/codecs/codec_opus.c
    team/dvossel/opus_codec_ftw/include/asterisk/opus.h
    team/dvossel/opus_codec_ftw/res/res_format_attr_opus.c

Modified: team/dvossel/opus_codec_ftw/codecs/codec_opus.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/opus_codec_ftw/codecs/codec_opus.c?view=diff&rev=330489&r1=330488&r2=330489
==============================================================================
--- team/dvossel/opus_codec_ftw/codecs/codec_opus.c (original)
+++ team/dvossel/opus_codec_ftw/codecs/codec_opus.c Mon Aug  1 15:48:11 2011
@@ -69,6 +69,8 @@
 	int16_t slin_buf[OUTBUF_SIZE];
 	/* Current number of samples in our slin input buffer */
 	unsigned slin_samples;
+	/* The input slin sample rate */
+	unsigned int slin_sample_rate;
 	/* The number of slin samples to encode at a time */
 	unsigned int frame_size;
 	/* OPUS output sample rate */
@@ -97,7 +99,7 @@
 	unsigned slin_samples;
 };
 
-static int opus_enc_set(struct ast_trans_pvt *pvt, struct ast_format *slin_src)
+static int opus_enc_set(struct ast_trans_pvt *pvt, struct ast_format *slin_src, int slin_frame_ms)
 {
 	struct opus_encoder_pvt *enc = pvt->pvt;
 	int slin_rate = ast_format_rate(slin_src);
@@ -120,9 +122,20 @@
 		ast_format_get_value(&pvt->explicit_dst, OPUS_ATTR_KEY_MAX_BITRATE, &max_bitrate);
 	}
 
-	time_period = time_period ? time_period : DEFAULT_TIME_PERIOD;
+	time_period = time_period ? time_period : slin_frame_ms;
+
+	switch (time_period) {
+	/* make sure the time period is valid before sending it to the encoder */
+	case OPUS_ATTR_VAL_PTIME_5:
+	case OPUS_ATTR_VAL_PTIME_10:
+	case OPUS_ATTR_VAL_PTIME_20:
+	case OPUS_ATTR_VAL_PTIME_40:
+	case OPUS_ATTR_VAL_PTIME_60:
+		break;
+	default:
+		time_period = DEFAULT_TIME_PERIOD;
+	}
 	enc_mode = enc_mode == OPUS_ATTR_VAL_MODE_VOICE ? OPUS_APPLICATION_VOIP : OPUS_APPLICATION_AUDIO;
-
 
 	if (slin_rate != opus_rate) {
 		if (!(enc->resamp = speex_resampler_init(1, slin_rate, opus_rate, 5, &error))) {
@@ -130,7 +143,7 @@
 		}
 	}
 
-	if (!(enc->enc = opus_encoder_create(slin_rate, 1, enc_mode))) {
+	if (!(enc->enc = opus_encoder_create(opus_rate, 1, enc_mode))) {
 		ast_log(LOG_WARNING, "Failed to create OPUS encoder\n");
 		speex_resampler_destroy(enc->resamp);
 		return -1;
@@ -148,6 +161,7 @@
 	if (cbr) {
 		opus_encoder_ctl(enc->enc, OPUS_SET_VBR_CONSTRAINT(cbr));
 	}
+
 
 	enc->frame_size = opus_rate / (1000 / time_period);
 	enc->sample_rate = opus_rate;
@@ -156,6 +170,7 @@
 	memset(enc->frame_offsets, 0, sizeof(enc->frame_offsets));
 	enc->frame_offsets_num = 0;
 	enc->frame_offsets_numbytes = OUTBUF_SIZE;
+	enc->slin_sample_rate = slin_rate;
 
 	enc->init = 1;
 
@@ -221,7 +236,7 @@
 	int num_bytes = 0;
 
 	if (!enc->init) {
-		opus_enc_set(pvt, &f->subclass.format);
+		opus_enc_set(pvt, &f->subclass.format, f->len);
 	}
 
 	if (!f->datalen) {
@@ -248,12 +263,12 @@
 	slin_data = enc->slin_buf;
 	opus_data = (unsigned char *) pvt->outbuf.c;
 
-
 	for ( ; enc->slin_samples >= enc->frame_size; enc->slin_samples -= enc->frame_size) {
+
 		num_bytes = opus_encode(enc->enc, slin_data, enc->frame_size, opus_data, enc->frame_offsets_numbytes);
 
 		if (num_bytes <= 0) {
-			/* err */
+			ast_log(LOG_WARNING, "OPUS encoder failed to encode frame. error %d \n", num_bytes);
 			break;
 		}
 
@@ -280,6 +295,7 @@
 	struct ast_frame *frame = NULL;
 	struct ast_frame *cur = NULL;
 	struct ast_frame tmp;
+	int ms;
 	int i;
 
 	for (i = 0; i < enc->frame_offsets_num; i++) {
@@ -291,6 +307,14 @@
 			ast_format_set(&tmp.subclass.format, AST_FORMAT_OPUS,
 				OPUS_ATTR_KEY_SAMP_RATE, enc->sample_rate);
 		}
+
+		/* determine how many ms are in this frame */
+		ms = (enc->frame_size * 1000) / enc->slin_sample_rate;
+		ast_format_append(&tmp.subclass.format,
+			OPUS_ATTR_KEY_PTIME, ms,
+			AST_FORMAT_ATTR_END);
+
+		tmp.len = ms;
 		tmp.datalen = enc->frame_offsets[i].len;
 		tmp.data.ptr = enc->frame_offsets[i].buf;
 		tmp.samples = enc->frame_size;

Modified: team/dvossel/opus_codec_ftw/include/asterisk/opus.h
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/opus_codec_ftw/include/asterisk/opus.h?view=diff&rev=330489&r1=330488&r2=330489
==============================================================================
--- team/dvossel/opus_codec_ftw/include/asterisk/opus.h (original)
+++ team/dvossel/opus_codec_ftw/include/asterisk/opus.h Mon Aug  1 15:48:11 2011
@@ -24,6 +24,10 @@
  */
 #ifndef _AST_FORMAT_OPUS_H_
 #define _AST_FORMAT_OPUS_H_
+
+/* TODO
+ * Opus needs better support for encoding/decoding frames of time intervals differing from 20ms.
+ */
 
 /*! OPUS format attribute key value pairs, all are accessible through ast_format_get_value()*/
 enum opus_attr_keys {

Modified: team/dvossel/opus_codec_ftw/res/res_format_attr_opus.c
URL: http://svnview.digium.com/svn/asterisk/team/dvossel/opus_codec_ftw/res/res_format_attr_opus.c?view=diff&rev=330489&r1=330488&r2=330489
==============================================================================
--- team/dvossel/opus_codec_ftw/res/res_format_attr_opus.c (original)
+++ team/dvossel/opus_codec_ftw/res/res_format_attr_opus.c Mon Aug  1 15:48:11 2011
@@ -147,7 +147,8 @@
 	struct opus_attr *attr_res = (struct opus_attr *) result;
 	int joint = -1;
 
-	attr_res->samplerate = attr1->samplerate & attr2->samplerate;
+	attr_res->samplerate = MIN(attr1->samplerate, attr2->samplerate);
+
 	/* sample rate is the only attribute that has any bearing on if joint capabilities exist or not */
 	if (attr_res->samplerate) {
 		joint = 0;




More information about the asterisk-commits mailing list