From a2c08fe0f0272a5a19f45e85e30a670991edf9ea Mon Sep 17 00:00:00 2001
From: Russell Bryant <russell@russellbryant.com>
Date: Thu, 10 Jan 2008 23:16:09 +0000
Subject: [PATCH] Fix various issues in codec_g722.  - The most common fix
 being made here is to fix all of the places where the    number of output
 samples and output bytes gets updated in the translator    state structure. 
 - Fix a number of other places where the number of samples provided as an   
 initialization value to a struct was incorrect.

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@97975 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 codecs/codec_g722.c | 65 ++++++++++++++++++++++++++++++++++-----------
 1 file changed, 50 insertions(+), 15 deletions(-)

diff --git a/codecs/codec_g722.c b/codecs/codec_g722.c
index 6b52407f6c..ac0b179f1c 100644
--- a/codecs/codec_g722.c
+++ b/codecs/codec_g722.c
@@ -98,25 +98,60 @@ static int g722tolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	struct g722_decoder_pvt *tmp = pvt->pvt;
 	unsigned char *src = f->data;
-	int16_t *dst = (int16_t *) pvt->outbuf + pvt->samples;
+	int out_samples;
 
-	g722_decode(&tmp->g722, dst, src, f->samples);
-	pvt->samples += f->samples;
-	pvt->datalen += 2 * f->samples;
+	out_samples = g722_decode(&tmp->g722, (int16_t *) &pvt->outbuf[pvt->samples * sizeof(int16_t)], 
+		src, f->samples);
+
+	pvt->samples += out_samples;
+
+	pvt->datalen += (out_samples * sizeof(int16_t));
+
+	return 0;
+}
+
+static int g722tolin16_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
+{
+	struct g722_decoder_pvt *tmp = pvt->pvt;
+	int out_samples;
+
+	out_samples = g722_decode(&tmp->g722, (int16_t *) &pvt->outbuf[pvt->samples * sizeof(int16_t)], 
+		(uint8_t *) f->data, f->samples);
+
+	/* sample rate the same between formats, but don't assume that it won't output more ... */
+	pvt->samples += out_samples;
+
+	pvt->datalen += (out_samples * sizeof(int16_t));
 
 	return 0;
 }
 
 static int lintog722_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
+{
+	struct g722_encoder_pvt *tmp = pvt->pvt;
+	int outlen;
+
+	outlen = g722_encode(&tmp->g722, (uint8_t *) (&pvt->outbuf[pvt->datalen]), 
+		(int16_t *) f->data, f->samples);
+
+	pvt->samples += outlen;
+
+	pvt->datalen += outlen;
+
+	return 0;
+}
+
+static int lin16tog722_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
 {
 	struct g722_encoder_pvt *tmp = pvt->pvt;
 	int16_t *src = f->data;
+	int outlen;
+
+	outlen = g722_encode(&tmp->g722, (uint8_t*)(&pvt->outbuf[pvt->datalen]), src, f->samples);
 
-	g722_encode(&tmp->g722, (uint8_t*)(&pvt->outbuf[pvt->datalen]), src, f->samples);
-	/* Since G.722 64kbps per second is one bye per sample, all of these
-	   calculations are easy */
-	pvt->samples += f->samples;
-	pvt->datalen += f->samples;
+	pvt->samples += outlen;
+
+	pvt->datalen += outlen;
 
 	return 0;
 }
@@ -127,7 +162,7 @@ static struct ast_frame *g722tolin_sample(void)
 		.frametype = AST_FRAME_VOICE,
 		.subclass = AST_FORMAT_G722,
 		.datalen = sizeof(g722_slin_ex),
-		.samples = sizeof(g722_slin_ex) / sizeof(g722_slin_ex[0]),
+		.samples = sizeof(g722_slin_ex),
 		.src = __PRETTY_FUNCTION__,
 		.data = g722_slin_ex,
 	};
@@ -141,7 +176,7 @@ static struct ast_frame *g722tolin16_sample(void)
 		.frametype = AST_FRAME_VOICE,
 		.subclass = AST_FORMAT_G722,
 		.datalen = sizeof(slin_g722_ex),
-		.samples = sizeof(slin_g722_ex) / sizeof(slin_g722_ex[0]),
+		.samples = sizeof(slin_g722_ex),
 		.src = __PRETTY_FUNCTION__,
 		.data = slin_g722_ex,
 	};
@@ -185,7 +220,7 @@ static struct ast_translator g722tolin = {
 	.framein = g722tolin_framein,
 	.sample = g722tolin_sample,
 	.desc_size = sizeof(struct g722_decoder_pvt),
-	.buffer_samples = BUFFER_SAMPLES,
+	.buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
 	.buf_size = BUFFER_SAMPLES,
 	.plc_samples = 160,
 };
@@ -207,10 +242,10 @@ static struct ast_translator g722tolin16 = {
 	.srcfmt = AST_FORMAT_G722,
 	.dstfmt = AST_FORMAT_SLINEAR16,
 	.newpvt = g722tolin16_new,	/* same for both directions */
-	.framein = g722tolin_framein,
+	.framein = g722tolin16_framein,
 	.sample = g722tolin16_sample,
 	.desc_size = sizeof(struct g722_decoder_pvt),
-	.buffer_samples = BUFFER_SAMPLES,
+	.buffer_samples = BUFFER_SAMPLES / sizeof(int16_t),
 	.buf_size = BUFFER_SAMPLES,
 	.plc_samples = 160,
 };
@@ -220,7 +255,7 @@ static struct ast_translator lin16tog722 = {
 	.srcfmt = AST_FORMAT_SLINEAR16,
 	.dstfmt = AST_FORMAT_G722,
 	.newpvt = lin16tog722_new,	/* same for both directions */
-	.framein = lintog722_framein,
+	.framein = lin16tog722_framein,
 	.sample = lin16tog722_sample,
 	.desc_size = sizeof(struct g722_encoder_pvt),
 	.buffer_samples = BUFFER_SAMPLES,
-- 
GitLab