Skip to content

Commit

Permalink
Add ilbc support
Browse files Browse the repository at this point in the history
  • Loading branch information
N4IRS committed Feb 13, 2018
1 parent c5eba9f commit f4d8650
Show file tree
Hide file tree
Showing 56 changed files with 18,578 additions and 18 deletions.
12 changes: 6 additions & 6 deletions asterisk/codecs/codec_ilbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision: 114611 $")
#include "ilbc_slin_ex.h"

#define USE_ILBC_ENHANCER 0
#define ILBC_MS 30
/* #define ILBC_MS 20 */
/*#define ILBC_MS 30 */
#define ILBC_MS 20

#define ILBC_FRAME_LEN 50 /* apparently... */
#define ILBC_SAMPLES 240 /* 30ms at 8000 hz */
#define ILBC_FRAME_LEN 38 /* apparently... */
#define ILBC_SAMPLES 160 /* 20ms at 8000 hz */
#define BUFFER_SAMPLES 8000

struct ilbc_coder_pvt {
Expand Down Expand Up @@ -107,7 +107,7 @@ static struct ast_frame *ilbctolin_sample(void)
f.frametype = AST_FRAME_VOICE;
f.subclass = AST_FORMAT_ILBC;
f.datalen = sizeof(ilbc_slin_ex);
/* All frames are 30 ms long */
/* All frames are 20 ms long */
f.samples = ILBC_SAMPLES;
f.mallocd = 0;
f.offset = 0;
Expand Down Expand Up @@ -135,7 +135,7 @@ static int ilbctolin_framein(struct ast_trans_pvt *pvt, struct ast_frame *f)
}

if (f->datalen % ILBC_FRAME_LEN) {
ast_log(LOG_WARNING, "Huh? An ilbc frame that isn't a multiple of 50 bytes long from %s (%d)?\n", f->src, f->datalen);
ast_log(LOG_WARNING, "Huh? An ilbc frame that isn't a multiple of 38 bytes long from %s (%d)?\n", f->src, f->datalen);
return -1;
}

Expand Down
114 changes: 114 additions & 0 deletions asterisk/codecs/ilbc/FrameClassify.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

/******************************************************************
iLBC Speech Coder ANSI-C Source Code
FrameClassify.c
Copyright (C) The Internet Society (2004).
All Rights Reserved.
******************************************************************/

#include "iLBC_define.h"

/*---------------------------------------------------------------*
* Classification of subframes to localize start state
*--------------------------------------------------------------*/

int FrameClassify( /* index to the max-energy sub-frame */
iLBC_Enc_Inst_t *iLBCenc_inst,
/* (i/o) the encoder state structure */
float *residual /* (i) lpc residual signal */
) {
float max_ssqEn, fssqEn[NSUB_MAX], bssqEn[NSUB_MAX], *pp;
int n, l, max_ssqEn_n;
const float ssqEn_win[NSUB_MAX-1]={(float)0.8,(float)0.9,
(float)1.0,(float)0.9,(float)0.8};
const float sampEn_win[5]={(float)1.0/(float)6.0,
(float)2.0/(float)6.0, (float)3.0/(float)6.0,
(float)4.0/(float)6.0, (float)5.0/(float)6.0};

/* init the front and back energies to zero */

memset(fssqEn, 0, NSUB_MAX*sizeof(float));
memset(bssqEn, 0, NSUB_MAX*sizeof(float));

/* Calculate front of first seqence */

n=0;
pp=residual;
for (l=0; l<5; l++) {
fssqEn[n] += sampEn_win[l] * (*pp) * (*pp);
pp++;
}
for (l=5; l<SUBL; l++) {





fssqEn[n] += (*pp) * (*pp);
pp++;
}

/* Calculate front and back of all middle sequences */

for (n=1; n<iLBCenc_inst->nsub-1; n++) {
pp=residual+n*SUBL;
for (l=0; l<5; l++) {
fssqEn[n] += sampEn_win[l] * (*pp) * (*pp);
bssqEn[n] += (*pp) * (*pp);
pp++;
}
for (l=5; l<SUBL-5; l++) {
fssqEn[n] += (*pp) * (*pp);
bssqEn[n] += (*pp) * (*pp);
pp++;
}
for (l=SUBL-5; l<SUBL; l++) {
fssqEn[n] += (*pp) * (*pp);
bssqEn[n] += sampEn_win[SUBL-l-1] * (*pp) * (*pp);
pp++;
}
}

/* Calculate back of last seqence */

n=iLBCenc_inst->nsub-1;
pp=residual+n*SUBL;
for (l=0; l<SUBL-5; l++) {
bssqEn[n] += (*pp) * (*pp);
pp++;
}
for (l=SUBL-5; l<SUBL; l++) {
bssqEn[n] += sampEn_win[SUBL-l-1] * (*pp) * (*pp);
pp++;
}

/* find the index to the weighted 80 sample with
most energy */

if (iLBCenc_inst->mode==20) l=1;
else l=0;

max_ssqEn=(fssqEn[0]+bssqEn[1])*ssqEn_win[l];
max_ssqEn_n=1;
for (n=2; n<iLBCenc_inst->nsub; n++) {






l++;
if ((fssqEn[n-1]+bssqEn[n])*ssqEn_win[l] > max_ssqEn) {
max_ssqEn=(fssqEn[n-1]+bssqEn[n]) *
ssqEn_win[l];
max_ssqEn_n=n;
}
}

return max_ssqEn_n;
}

27 changes: 27 additions & 0 deletions asterisk/codecs/ilbc/FrameClassify.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

/******************************************************************
iLBC Speech Coder ANSI-C Source Code
FrameClassify.h
Copyright (C) The Internet Society (2004).
All Rights Reserved.
******************************************************************/

#ifndef __iLBC_FRAMECLASSIFY_H
#define __iLBC_FRAMECLASSIFY_H

int FrameClassify( /* index to the max-energy sub-frame */
iLBC_Enc_Inst_t *iLBCenc_inst,
/* (i/o) the encoder state structure */
float *residual /* (i) lpc residual signal */
);





#endif

158 changes: 158 additions & 0 deletions asterisk/codecs/ilbc/LPCdecode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@

/******************************************************************
iLBC Speech Coder ANSI-C Source Code
LPC_decode.c
Copyright (C) The Internet Society (2004).
All Rights Reserved.
******************************************************************/

#include <math.h>
#include <string.h>

#include "helpfun.h"
#include "lsf.h"
#include "iLBC_define.h"
#include "constants.h"

/*---------------------------------------------------------------*
* interpolation of lsf coefficients for the decoder
*--------------------------------------------------------------*/

void LSFinterpolate2a_dec(
float *a, /* (o) lpc coefficients for a sub-frame */
float *lsf1, /* (i) first lsf coefficient vector */
float *lsf2, /* (i) second lsf coefficient vector */
float coef, /* (i) interpolation weight */
int length /* (i) length of lsf vectors */
){
float lsftmp[LPC_FILTERORDER];

interpolate(lsftmp, lsf1, lsf2, coef, length);
lsf2a(a, lsftmp);
}

/*---------------------------------------------------------------*
* obtain dequantized lsf coefficients from quantization index
*--------------------------------------------------------------*/

void SimplelsfDEQ(
float *lsfdeq, /* (o) dequantized lsf coefficients */
int *index, /* (i) quantization index */
int lpc_n /* (i) number of LPCs */
){
int i, j, pos, cb_pos;





/* decode first LSF */

pos = 0;
cb_pos = 0;
for (i = 0; i < LSF_NSPLIT; i++) {
for (j = 0; j < dim_lsfCbTbl[i]; j++) {
lsfdeq[pos + j] = lsfCbTbl[cb_pos +
(long)(index[i])*dim_lsfCbTbl[i] + j];
}
pos += dim_lsfCbTbl[i];
cb_pos += size_lsfCbTbl[i]*dim_lsfCbTbl[i];
}

if (lpc_n>1) {

/* decode last LSF */

pos = 0;
cb_pos = 0;
for (i = 0; i < LSF_NSPLIT; i++) {
for (j = 0; j < dim_lsfCbTbl[i]; j++) {
lsfdeq[LPC_FILTERORDER + pos + j] =
lsfCbTbl[cb_pos +
(long)(index[LSF_NSPLIT + i])*
dim_lsfCbTbl[i] + j];
}
pos += dim_lsfCbTbl[i];
cb_pos += size_lsfCbTbl[i]*dim_lsfCbTbl[i];
}
}
}

/*----------------------------------------------------------------*
* obtain synthesis and weighting filters form lsf coefficients
*---------------------------------------------------------------*/

void DecoderInterpolateLSF(
float *syntdenum, /* (o) synthesis filter coefficients */
float *weightdenum, /* (o) weighting denumerator
coefficients */
float *lsfdeq, /* (i) dequantized lsf coefficients */
int length, /* (i) length of lsf coefficient vector */
iLBC_Dec_Inst_t *iLBCdec_inst
/* (i) the decoder state structure */
){
int i, pos, lp_length;
float lp[LPC_FILTERORDER + 1], *lsfdeq2;






lsfdeq2 = lsfdeq + length;
lp_length = length + 1;

if (iLBCdec_inst->mode==30) {
/* sub-frame 1: Interpolation between old and first */

LSFinterpolate2a_dec(lp, iLBCdec_inst->lsfdeqold, lsfdeq,
lsf_weightTbl_30ms[0], length);
memcpy(syntdenum,lp,lp_length*sizeof(float));
bwexpand(weightdenum, lp, LPC_CHIRP_WEIGHTDENUM,
lp_length);

/* sub-frames 2 to 6: interpolation between first
and last LSF */

pos = lp_length;
for (i = 1; i < 6; i++) {
LSFinterpolate2a_dec(lp, lsfdeq, lsfdeq2,
lsf_weightTbl_30ms[i], length);
memcpy(syntdenum + pos,lp,lp_length*sizeof(float));
bwexpand(weightdenum + pos, lp,
LPC_CHIRP_WEIGHTDENUM, lp_length);
pos += lp_length;
}
}
else {
pos = 0;
for (i = 0; i < iLBCdec_inst->nsub; i++) {
LSFinterpolate2a_dec(lp, iLBCdec_inst->lsfdeqold,
lsfdeq, lsf_weightTbl_20ms[i], length);
memcpy(syntdenum+pos,lp,lp_length*sizeof(float));
bwexpand(weightdenum+pos, lp, LPC_CHIRP_WEIGHTDENUM,
lp_length);
pos += lp_length;
}
}

/* update memory */

if (iLBCdec_inst->mode==30)
memcpy(iLBCdec_inst->lsfdeqold, lsfdeq2,
length*sizeof(float));
else
memcpy(iLBCdec_inst->lsfdeqold, lsfdeq,
length*sizeof(float));

}






52 changes: 52 additions & 0 deletions asterisk/codecs/ilbc/LPCdecode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

/******************************************************************
iLBC Speech Coder ANSI-C Source Code
LPC_decode.h
Copyright (C) The Internet Society (2004).
All Rights Reserved.
******************************************************************/

#ifndef __iLBC_LPC_DECODE_H
#define __iLBC_LPC_DECODE_H

void LSFinterpolate2a_dec(
float *a, /* (o) lpc coefficients for a sub-frame */
float *lsf1, /* (i) first lsf coefficient vector */
float *lsf2, /* (i) second lsf coefficient vector */
float coef, /* (i) interpolation weight */
int length /* (i) length of lsf vectors */
);

void SimplelsfDEQ(
float *lsfdeq, /* (o) dequantized lsf coefficients */
int *index, /* (i) quantization index */
int lpc_n /* (i) number of LPCs */
);

void DecoderInterpolateLSF(
float *syntdenum, /* (o) synthesis filter coefficients */
float *weightdenum, /* (o) weighting denumerator
coefficients */
float *lsfdeq, /* (i) dequantized lsf coefficients */
int length, /* (i) length of lsf coefficient vector */
iLBC_Dec_Inst_t *iLBCdec_inst
/* (i) the decoder state structure */
);

#endif












Loading

0 comments on commit f4d8650

Please sign in to comment.