[Asterisk-code-review] transcode: Fix transcoding while different in frame size. (asterisk[13])

Alexander Traud asteriskteam at digium.com
Fri Aug 28 15:50:16 CDT 2015


Alexander Traud has uploaded a new change for review.

  https://gerrit.asterisk.org/1155

Change subject: transcode: Fix transcoding while different in frame size.
......................................................................

transcode: Fix transcoding while different in frame size.

When Asterisk transcoded between codecs, each with a different frame size (for
example between iLBC 30 and Speex-WB), too large frames were created by
ast_trans_frameout. Now, ast_trans_frameout is called with the correct frame
length, creating several frames when necessary. Affects all transcoding modules
which used ast_trans_frameout: GSM, iLBC, LPC10, and Speex.

ASTERISK-25353 #close

Change-Id: I2e229569d73191d66a4e43fef35432db24000212
---
M codecs/codec_gsm.c
M codecs/codec_ilbc.c
M codecs/codec_lpc10.c
M codecs/codec_speex.c
M main/translate.c
5 files changed, 111 insertions(+), 81 deletions(-)


  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/55/1155/1

diff --git a/codecs/codec_gsm.c b/codecs/codec_gsm.c
index 8cb4943..cc9aa46 100644
--- a/codecs/codec_gsm.c
+++ b/codecs/codec_gsm.c
@@ -39,6 +39,7 @@
 #include "asterisk/config.h"
 #include "asterisk/module.h"
 #include "asterisk/utils.h"
+#include "asterisk/linkedlists.h"
 
 #ifdef HAVE_GSM_HEADER
 #include "gsm.h"
@@ -139,25 +140,30 @@
 static struct ast_frame *lintogsm_frameout(struct ast_trans_pvt *pvt)
 {
 	struct gsm_translator_pvt *tmp = pvt->pvt;
-	int datalen = 0;
-	int samples = 0;
+	struct ast_frame *result = NULL;
+	struct ast_frame *last = NULL;
 
-	/* We can't work on anything less than a frame in size */
-	if (pvt->samples < GSM_SAMPLES)
-		return NULL;
 	while (pvt->samples >= GSM_SAMPLES) {
+		struct ast_frame *current = NULL;
+
 		/* Encode a frame of data */
-		gsm_encode(tmp->gsm, tmp->buf + samples, (gsm_byte *) pvt->outbuf.c + datalen);
-		datalen += GSM_FRAME_LEN;
-		samples += GSM_SAMPLES;
+		gsm_encode(tmp->gsm, tmp->buf, (gsm_byte *) pvt->outbuf.c);
+
+		/* Move the data at the end of the buffer to the front */
 		pvt->samples -= GSM_SAMPLES;
+		if (pvt->samples) {
+			memmove(tmp->buf, tmp->buf + GSM_SAMPLES, pvt->samples * 2);
+		}
+
+		current = ast_trans_frameout(pvt, GSM_FRAME_LEN, GSM_SAMPLES);
+		if (last) {
+			AST_LIST_NEXT(last, frame_list) = current;
+		} else {
+			result = current;
+		}
+		last = current;
 	}
-
-	/* Move the data at the end of the buffer to the front */
-	if (pvt->samples)
-		memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
-
-	return ast_trans_frameout(pvt, datalen, samples);
+	return result;
 }
 
 static void gsm_destroy_stuff(struct ast_trans_pvt *pvt)
diff --git a/codecs/codec_ilbc.c b/codecs/codec_ilbc.c
index af23b90..dbb6fe2 100644
--- a/codecs/codec_ilbc.c
+++ b/codecs/codec_ilbc.c
@@ -37,6 +37,7 @@
 #include "asterisk/translate.h"
 #include "asterisk/module.h"
 #include "asterisk/utils.h"
+#include "asterisk/linkedlists.h"
 
 #ifdef ILBC_WEBRTC
 #include <ilbc.h>
@@ -150,31 +151,34 @@
 static struct ast_frame *lintoilbc_frameout(struct ast_trans_pvt *pvt)
 {
 	struct ilbc_coder_pvt *tmp = pvt->pvt;
-	int datalen = 0;
-	int samples = 0;
+	struct ast_frame *result = NULL;
+	struct ast_frame *last = NULL;
 
-	/* We can't work on anything less than a frame in size */
-	if (pvt->samples < ILBC_SAMPLES)
-		return NULL;
 	while (pvt->samples >= ILBC_SAMPLES) {
+		struct ast_frame *current = NULL;
 		ilbc_block tmpf[ILBC_SAMPLES];
 		int i;
 
 		/* Encode a frame of data */
 		for (i = 0 ; i < ILBC_SAMPLES ; i++)
-			tmpf[i] = tmp->buf[samples + i];
-		iLBC_encode( (ilbc_bytes*)pvt->outbuf.BUF_TYPE + datalen, tmpf, &tmp->enc);
+			tmpf[i] = tmp->buf[i];
+		iLBC_encode( (ilbc_bytes*)pvt->outbuf.BUF_TYPE, tmpf, &tmp->enc);
 
-		datalen += ILBC_FRAME_LEN;
-		samples += ILBC_SAMPLES;
+		/* Move the data at the end of the buffer to the front */
 		pvt->samples -= ILBC_SAMPLES;
+		if (pvt->samples) {
+			memmove(tmp->buf, tmp->buf + ILBC_SAMPLES, pvt->samples * 2);
+		}
+
+		current = ast_trans_frameout(pvt, ILBC_FRAME_LEN, ILBC_SAMPLES);
+		if (last) {
+			AST_LIST_NEXT(last, frame_list) = current;
+		} else {
+			result = current;
+		}
+		last = current;
 	}
-
-	/* Move the data at the end of the buffer to the front */
-	if (pvt->samples)
-		memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
-
-	return ast_trans_frameout(pvt, datalen, samples);
+	return result;
 }
 
 static struct ast_translator ilbctolin = {
diff --git a/codecs/codec_lpc10.c b/codecs/codec_lpc10.c
index ca2eb8e..ed0945f 100644
--- a/codecs/codec_lpc10.c
+++ b/codecs/codec_lpc10.c
@@ -39,6 +39,7 @@
 #include "asterisk/config.h"
 #include "asterisk/module.h"
 #include "asterisk/utils.h"
+#include "asterisk/linkedlists.h"
 
 #include "lpc10/lpc10.h"
 
@@ -161,30 +162,39 @@
 {
 	struct lpc10_coder_pvt *tmp = pvt->pvt;
 	int x;
-	int datalen = 0;	/* output frame */
-	int samples = 0;	/* output samples */
+	struct ast_frame *result = NULL;
+	struct ast_frame *last = NULL;
 	float tmpbuf[LPC10_SAMPLES_PER_FRAME];
 	INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME];	/* XXX what ??? */
-	/* We can't work on anything less than a frame in size */
-	if (pvt->samples < LPC10_SAMPLES_PER_FRAME)
-		return NULL;
-	while (pvt->samples >=  LPC10_SAMPLES_PER_FRAME) {
+
+	while (pvt->samples >= LPC10_SAMPLES_PER_FRAME) {
+		struct ast_frame *current = NULL;
+
 		/* Encode a frame of data */
 		for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++)
-			tmpbuf[x] = (float)tmp->buf[x + samples] / 32768.0;
+			tmpbuf[x] = (float)tmp->buf[x] / 32768.0;
 		lpc10_encode(tmpbuf, bits, tmp->lpc10.enc);
-		build_bits(pvt->outbuf.uc + datalen, bits);
-		datalen += LPC10_BYTES_IN_COMPRESSED_FRAME;
-		samples += LPC10_SAMPLES_PER_FRAME;
+		build_bits(pvt->outbuf.uc, bits);
+
+		/* Move the data at the end of the buffer to the front */
 		pvt->samples -= LPC10_SAMPLES_PER_FRAME;
+		if (pvt->samples) {
+			memmove(tmp->buf, tmp->buf + LPC10_SAMPLES_PER_FRAME, pvt->samples * 2);
+		}
+
 		/* Use one of the two left over bits to record if this is a 22 or 23 ms frame...
 		   important for IAX use */
 		tmp->longer = 1 - tmp->longer;
+
+		current = ast_trans_frameout(pvt, LPC10_BYTES_IN_COMPRESSED_FRAME, LPC10_SAMPLES_PER_FRAME);
+		if (last) {
+			AST_LIST_NEXT(last, frame_list) = current;
+		} else {
+			result = current;
+		}
+		last = current;
 	}
-	/* Move the data at the end of the buffer to the front */
-	if (pvt->samples)
-		memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
-	return ast_trans_frameout(pvt, datalen, samples);
+	return result;
 }
 
 
diff --git a/codecs/codec_speex.c b/codecs/codec_speex.c
index c91070d..4ef191d 100644
--- a/codecs/codec_speex.c
+++ b/codecs/codec_speex.c
@@ -54,6 +54,7 @@
 #include "asterisk/module.h"
 #include "asterisk/config.h"
 #include "asterisk/utils.h"
+#include "asterisk/linkedlists.h"
 
 /* codec variables */
 static int quality = 3;
@@ -259,23 +260,24 @@
 static struct ast_frame *lintospeex_frameout(struct ast_trans_pvt *pvt)
 {
 	struct speex_coder_pvt *tmp = pvt->pvt;
-	int is_speech=1;
-	int datalen = 0;	/* output bytes */
-	int samples = 0;	/* output samples */
+	struct ast_frame *result = NULL;
+	struct ast_frame *last = NULL;
 
-	/* We can't work on anything less than a frame in size */
-	if (pvt->samples < tmp->framesize)
-		return NULL;
-	speex_bits_reset(&tmp->bits);
 	while (pvt->samples >= tmp->framesize) {
+		int is_speech=1;
+		int datalen = 0; /* output bytes */
+		struct ast_frame *current = NULL;
+
+		speex_bits_reset(&tmp->bits);
+
 #ifdef _SPEEX_TYPES_H
 		/* Preprocess audio */
 		if (preproc)
-			is_speech = speex_preprocess(tmp->pp, tmp->buf + samples, NULL);
+			is_speech = speex_preprocess(tmp->pp, tmp->buf, NULL);
 		/* Encode a frame of data */
 		if (is_speech) {
 			/* If DTX enabled speex_encode returns 0 during silence */
-			is_speech = speex_encode_int(tmp->speex, tmp->buf + samples, &tmp->bits) || !dtx;
+			is_speech = speex_encode_int(tmp->speex, tmp->buf, &tmp->bits) || !dtx;
 		} else {
 			/* 5 zeros interpreted by Speex as silence (submode 0) */
 			speex_bits_pack(&tmp->bits, 0, 5);
@@ -286,26 +288,25 @@
 			int x;
 			/* Convert to floating point */
 			for (x = 0; x < tmp->framesize; x++)
-				fbuf[x] = tmp->buf[samples + x];
+				fbuf[x] = tmp->buf[x];
 			/* Encode a frame of data */
 			is_speech = speex_encode(tmp->speex, fbuf, &tmp->bits) || !dtx;
 		}
 #endif
-		samples += tmp->framesize;
+		/* Move the data at the end of the buffer to the front */
 		pvt->samples -= tmp->framesize;
-	}
+		if (pvt->samples) {
+			memmove(tmp->buf, tmp->buf + tmp->framesize, pvt->samples * 2);
+		}
 
-	/* Move the data at the end of the buffer to the front */
-	if (pvt->samples)
-		memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2);
-
-	/* Use AST_FRAME_CNG to signify the start of any silence period */
-	if (is_speech) {
-		tmp->silent_state = 0;
-	} else {
-		if (tmp->silent_state) {
-			return NULL;
-		} else {
+		/* Use AST_FRAME_CNG to signify the start of any silence period */
+		if (is_speech) {
+			tmp->silent_state = 0;
+			/* Terminate bit stream */
+			speex_bits_pack(&tmp->bits, 15, 5);
+			datalen = speex_bits_write(&tmp->bits, pvt->outbuf.c, pvt->t->buf_size);
+			current = ast_trans_frameout(pvt, datalen, tmp->framesize);
+		} else if (!tmp->silent_state) {
 			struct ast_frame frm = {
 				.frametype = AST_FRAME_CNG,
 				.src = pvt->t->name,
@@ -320,14 +321,17 @@
 			tmp->silent_state = 1;
 
 			/* XXX what now ? format etc... */
-			return ast_frisolate(&frm);
+			current = ast_frisolate(&frm);
 		}
-	}
 
-	/* Terminate bit stream */
-	speex_bits_pack(&tmp->bits, 15, 5);
-	datalen = speex_bits_write(&tmp->bits, pvt->outbuf.c, pvt->t->buf_size);
-	return ast_trans_frameout(pvt, datalen, samples);
+		if (last) {
+			AST_LIST_NEXT(last, frame_list) = current;
+		} else {
+			result = current;
+		}
+		last = current;
+	}
+	return result;
 }
 
 static void speextolin_destroy(struct ast_trans_pvt *arg)
diff --git a/main/translate.c b/main/translate.c
index 2b11d7b..128cc73 100644
--- a/main/translate.c
+++ b/main/translate.c
@@ -44,6 +44,7 @@
 #include "asterisk/cli.h"
 #include "asterisk/term.h"
 #include "asterisk/format.h"
+#include "asterisk/linkedlists.h"
 
 /*! \todo
  * TODO: sample frames for each supported input format.
@@ -556,21 +557,26 @@
 	if (out) {
 		/* we have a frame, play with times */
 		if (!ast_tvzero(delivery)) {
+			struct ast_frame *current = out;
+
 			/* Regenerate prediction after a discontinuity */
 			if (ast_tvzero(path->nextout)) {
 				path->nextout = ast_tvnow();
 			}
 
-			/* Use next predicted outgoing timestamp */
-			out->delivery = path->nextout;
+			while (current) {
+				/* Use next predicted outgoing timestamp */
+				current->delivery = path->nextout;
 
-			/* Predict next outgoing timestamp from samples in this
-			   frame. */
-			path->nextout = ast_tvadd(path->nextout, ast_samp2tv(
-				 out->samples, ast_format_get_sample_rate(out->subclass.format)));
-			if (f->samples != out->samples && ast_test_flag(out, AST_FRFLAG_HAS_TIMING_INFO)) {
-				ast_debug(4, "Sample size different %d vs %d\n", f->samples, out->samples);
-				ast_clear_flag(out, AST_FRFLAG_HAS_TIMING_INFO);
+				/* Predict next outgoing timestamp from samples in this
+				   frame. */
+				path->nextout = ast_tvadd(path->nextout, ast_samp2tv(
+				 current->samples, ast_format_get_sample_rate(current->subclass.format)));
+				if (f->samples != current->samples && ast_test_flag(current, AST_FRFLAG_HAS_TIMING_INFO)) {
+					ast_debug(4, "Sample size different %d vs %d\n", f->samples, current->samples);
+					ast_clear_flag(current, AST_FRFLAG_HAS_TIMING_INFO);
+				}
+				current = AST_LIST_NEXT(current, frame_list);
 			}
 		} else {
 			out->delivery = ast_tv(0, 0);

-- 
To view, visit https://gerrit.asterisk.org/1155
To unsubscribe, visit https://gerrit.asterisk.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2e229569d73191d66a4e43fef35432db24000212
Gerrit-PatchSet: 1
Gerrit-Project: asterisk
Gerrit-Branch: 13
Gerrit-Owner: Alexander Traud <pabstraud at compuserve.com>



More information about the asterisk-code-review mailing list