diff options
author | Alexander Traud <pabstraud@compuserve.com> | 2015-08-28 22:42:23 +0200 |
---|---|---|
committer | Alexander Traud <pabstraud@compuserve.com> | 2015-09-17 10:01:48 -0500 |
commit | b88c54fa4bd537bde46519abb95e30a5f96673ac (patch) | |
tree | 84318723a2ab5ce06f0880b3f0cbb144f8e8e3cd /codecs | |
parent | 5c713fdf18ffa934e0cac8ddb29e4ad95a68200b (diff) |
translate: Fix transcoding while different in frame size.
When Asterisk translates 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
Diffstat (limited to 'codecs')
-rw-r--r-- | codecs/codec_gsm.c | 29 | ||||
-rw-r--r-- | codecs/codec_ilbc.c | 28 | ||||
-rw-r--r-- | codecs/codec_lpc10.c | 41 | ||||
-rw-r--r-- | codecs/codec_speex.c | 60 |
4 files changed, 104 insertions, 54 deletions
diff --git a/codecs/codec_gsm.c b/codecs/codec_gsm.c index 8cb49430f..a18dc0abe 100644 --- a/codecs/codec_gsm.c +++ b/codecs/codec_gsm.c @@ -39,6 +39,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #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,35 @@ static int lintogsm_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) 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; + int samples = 0; /* output samples */ - /* 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; + /* Encode a frame of data */ - gsm_encode(tmp->gsm, tmp->buf + samples, (gsm_byte *) pvt->outbuf.c + datalen); - datalen += GSM_FRAME_LEN; + gsm_encode(tmp->gsm, tmp->buf + samples, (gsm_byte *) pvt->outbuf.c); samples += GSM_SAMPLES; pvt->samples -= GSM_SAMPLES; + + current = ast_trans_frameout(pvt, GSM_FRAME_LEN, GSM_SAMPLES); + if (!current) { + continue; + } else 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) + if (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 af23b906d..2646f49bd 100644 --- a/codecs/codec_ilbc.c +++ b/codecs/codec_ilbc.c @@ -37,6 +37,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #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,40 @@ static int lintoilbc_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) 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; + int samples = 0; /* output samples */ - /* 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; 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); + iLBC_encode((ilbc_bytes *) pvt->outbuf.BUF_TYPE, tmpf, &tmp->enc); - datalen += ILBC_FRAME_LEN; samples += ILBC_SAMPLES; pvt->samples -= ILBC_SAMPLES; + + current = ast_trans_frameout(pvt, ILBC_FRAME_LEN, ILBC_SAMPLES); + if (!current) { + continue; + } else 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) + if (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 ca2eb8ef0..a62eed3a8 100644 --- a/codecs/codec_lpc10.c +++ b/codecs/codec_lpc10.c @@ -39,6 +39,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/config.h" #include "asterisk/module.h" #include "asterisk/utils.h" +#include "asterisk/linkedlists.h" #include "lpc10/lpc10.h" @@ -160,31 +161,45 @@ static int lintolpc10_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) static struct ast_frame *lintolpc10_frameout(struct ast_trans_pvt *pvt) { struct lpc10_coder_pvt *tmp = pvt->pvt; - int x; - int datalen = 0; /* output frame */ - int samples = 0; /* output samples */ - 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) { + struct ast_frame *result = NULL; + struct ast_frame *last = NULL; + int samples = 0; /* output samples */ + + while (pvt->samples >= LPC10_SAMPLES_PER_FRAME) { + struct ast_frame *current; + float tmpbuf[LPC10_SAMPLES_PER_FRAME]; + INT32 bits[LPC10_BITS_IN_COMPRESSED_FRAME]; /* XXX what ??? */ + int x; + /* Encode a frame of data */ for (x=0;x<LPC10_SAMPLES_PER_FRAME;x++) tmpbuf[x] = (float)tmp->buf[x + samples] / 32768.0; lpc10_encode(tmpbuf, bits, tmp->lpc10.enc); - build_bits(pvt->outbuf.uc + datalen, bits); - datalen += LPC10_BYTES_IN_COMPRESSED_FRAME; + build_bits(pvt->outbuf.uc, bits); + samples += LPC10_SAMPLES_PER_FRAME; pvt->samples -= LPC10_SAMPLES_PER_FRAME; /* 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 (!current) { + continue; + } else 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) + if (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 c91070d9e..c9adceb2f 100644 --- a/codecs/codec_speex.c +++ b/codecs/codec_speex.c @@ -54,6 +54,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/module.h" #include "asterisk/config.h" #include "asterisk/utils.h" +#include "asterisk/frame.h" +#include "asterisk/linkedlists.h" /* codec variables */ static int quality = 3; @@ -259,15 +261,16 @@ static int lintospeex_framein(struct ast_trans_pvt *pvt, struct ast_frame *f) 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; + int samples = 0; /* output samples */ - /* 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) { + struct ast_frame *current; + int is_speech = 1; + + speex_bits_reset(&tmp->bits); + #ifdef _SPEEX_TYPES_H /* Preprocess audio */ if (preproc) @@ -293,18 +296,18 @@ static struct ast_frame *lintospeex_frameout(struct ast_trans_pvt *pvt) #endif samples += tmp->framesize; pvt->samples -= tmp->framesize; - } - /* 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; + /* Use AST_FRAME_CNG to signify the start of any silence period */ + if (is_speech) { + int datalen = 0; /* output bytes */ + + 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) { + current = NULL; } else { struct ast_frame frm = { .frametype = AST_FRAME_CNG, @@ -320,14 +323,25 @@ static struct ast_frame *lintospeex_frameout(struct ast_trans_pvt *pvt) tmp->silent_state = 1; /* XXX what now ? format etc... */ - return ast_frisolate(&frm); + current = ast_frisolate(&frm); } + + if (!current) { + continue; + } else 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 (samples) { + memmove(tmp->buf, tmp->buf + samples, pvt->samples * 2); } - /* 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); + return result; } static void speextolin_destroy(struct ast_trans_pvt *arg) |