2
/******************************************************************
4
iLBC Speech Coder ANSI-C Source Code
8
Copyright (C) The Internet Society (2004).
11
******************************************************************/
16
#include "iLBC_define.h"
17
#include "StateConstructW.h"
18
#include "LPCdecode.h"
19
#include "iCBConstruct.h"
22
#include "constants.h"
27
#include "syntFilter.h"
29
/*----------------------------------------------------------------*
30
* Initiation of decoder instance.
31
*---------------------------------------------------------------*/
33
short initDecode( /* (o) Number of decoded
35
iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) Decoder instance */
36
int mode, /* (i) frame size mode */
37
int use_enhancer /* (i) 1 to use enhancer
43
iLBCdec_inst->mode = mode;
50
iLBCdec_inst->blockl = BLOCKL_30MS;
51
iLBCdec_inst->nsub = NSUB_30MS;
52
iLBCdec_inst->nasub = NASUB_30MS;
53
iLBCdec_inst->lpc_n = LPC_N_30MS;
54
iLBCdec_inst->no_of_bytes = NO_OF_BYTES_30MS;
55
iLBCdec_inst->no_of_words = NO_OF_WORDS_30MS;
56
iLBCdec_inst->state_short_len=STATE_SHORT_LEN_30MS;
58
iLBCdec_inst->ULP_inst=&ULP_30msTbl;
61
iLBCdec_inst->blockl = BLOCKL_20MS;
62
iLBCdec_inst->nsub = NSUB_20MS;
63
iLBCdec_inst->nasub = NASUB_20MS;
64
iLBCdec_inst->lpc_n = LPC_N_20MS;
65
iLBCdec_inst->no_of_bytes = NO_OF_BYTES_20MS;
66
iLBCdec_inst->no_of_words = NO_OF_WORDS_20MS;
67
iLBCdec_inst->state_short_len=STATE_SHORT_LEN_20MS;
69
iLBCdec_inst->ULP_inst=&ULP_20msTbl;
75
memset(iLBCdec_inst->syntMem, 0,
76
LPC_FILTERORDER*sizeof(float));
77
memcpy((*iLBCdec_inst).lsfdeqold, lsfmeanTbl,
78
LPC_FILTERORDER*sizeof(float));
80
memset(iLBCdec_inst->old_syntdenum, 0,
81
((LPC_FILTERORDER + 1)*NSUB_MAX)*sizeof(float));
82
for (i=0; i<NSUB_MAX; i++)
83
iLBCdec_inst->old_syntdenum[i*(LPC_FILTERORDER+1)]=1.0;
85
iLBCdec_inst->last_lag = 20;
87
iLBCdec_inst->prevLag = 120;
88
iLBCdec_inst->per = 0.0;
89
iLBCdec_inst->consPLICount = 0;
90
iLBCdec_inst->prevPLI = 0;
91
iLBCdec_inst->prevLpc[0] = 1.0;
92
memset(iLBCdec_inst->prevLpc+1,0,
93
LPC_FILTERORDER*sizeof(float));
94
memset(iLBCdec_inst->prevResidual, 0, BLOCKL_MAX*sizeof(float));
95
iLBCdec_inst->seed=777;
102
memset(iLBCdec_inst->hpomem, 0, 4*sizeof(float));
104
iLBCdec_inst->use_enhancer = use_enhancer;
105
memset(iLBCdec_inst->enh_buf, 0, ENH_BUFL*sizeof(float));
106
for (i=0;i<ENH_NBLOCKS_TOT;i++)
107
iLBCdec_inst->enh_period[i]=(float)40.0;
109
iLBCdec_inst->prev_enh_pl = 0;
111
return (short)(iLBCdec_inst->blockl);
114
/*----------------------------------------------------------------*
115
* frame residual decoder function (subrutine to iLBC_decode)
116
*---------------------------------------------------------------*/
119
iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) the decoder state
121
float *decresidual, /* (o) decoded residual frame */
122
int start, /* (i) location of start
124
int idxForMax, /* (i) codebook index for the
126
int *idxVec, /* (i) codebook indexes for the
129
float *syntdenum, /* (i) the decoded synthesis
130
filter coefficients */
131
int *cb_index, /* (i) the indexes for the
133
int *gain_index, /* (i) the indexes for the
134
corresponding gains */
135
int *extra_cb_index, /* (i) the indexes for the
136
adaptive codebook part
138
int *extra_gain_index, /* (i) the indexes for the
139
corresponding gains */
140
int state_first /* (i) 1 if non adaptive part
145
float reverseDecresidual[BLOCKL_MAX], mem[CB_MEML];
146
int k, meml_gotten, Nfor, Nback, i;
148
int subcount, subframe;
155
diff = STATE_LEN - iLBCdec_inst->state_short_len;
157
if (state_first == 1) {
158
start_pos = (start-1)*SUBL;
160
start_pos = (start-1)*SUBL + diff;
163
/* decode scalar part of start state */
165
StateConstructW(idxForMax, idxVec,
166
&syntdenum[(start-1)*(LPC_FILTERORDER+1)],
167
&decresidual[start_pos], iLBCdec_inst->state_short_len);
170
if (state_first) { /* put adaptive part in the end */
175
(CB_MEML-iLBCdec_inst->state_short_len)*sizeof(float));
176
memcpy(mem+CB_MEML-iLBCdec_inst->state_short_len,
177
decresidual+start_pos,
178
iLBCdec_inst->state_short_len*sizeof(float));
180
/* construct decoded vector */
183
&decresidual[start_pos+iLBCdec_inst->state_short_len],
184
extra_cb_index, extra_gain_index, mem+CB_MEML-stMemLTbl,
185
stMemLTbl, diff, CB_NSTAGES);
188
else {/* put adaptive part in the beginning */
190
/* create reversed vectors for prediction */
192
for (k=0; k<diff; k++) {
193
reverseDecresidual[k] =
194
decresidual[(start+1)*SUBL-1-
195
(k+iLBCdec_inst->state_short_len)];
200
meml_gotten = iLBCdec_inst->state_short_len;
201
for (k=0; k<meml_gotten; k++){
202
mem[CB_MEML-1-k] = decresidual[start_pos + k];
209
memset(mem, 0, (CB_MEML-k)*sizeof(float));
211
/* construct decoded vector */
213
iCBConstruct(reverseDecresidual, extra_cb_index,
214
extra_gain_index, mem+CB_MEML-stMemLTbl, stMemLTbl,
217
/* get decoded residual from reversed vector */
219
for (k=0; k<diff; k++) {
220
decresidual[start_pos-1-k] = reverseDecresidual[k];
224
/* counter for predicted sub-frames */
228
/* forward prediction of sub-frames */
230
Nfor = iLBCdec_inst->nsub-start-1;
236
memset(mem, 0, (CB_MEML-STATE_LEN)*sizeof(float));
237
memcpy(mem+CB_MEML-STATE_LEN, decresidual+(start-1)*SUBL,
238
STATE_LEN*sizeof(float));
240
/* loop over sub-frames to encode */
242
for (subframe=0; subframe<Nfor; subframe++) {
244
/* construct decoded vector */
246
iCBConstruct(&decresidual[(start+1+subframe)*SUBL],
247
cb_index+subcount*CB_NSTAGES,
248
gain_index+subcount*CB_NSTAGES,
249
mem+CB_MEML-memLfTbl[subcount],
250
memLfTbl[subcount], SUBL, CB_NSTAGES);
254
memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
255
memcpy(mem+CB_MEML-SUBL,
261
&decresidual[(start+1+subframe)*SUBL],
270
/* backward prediction of sub-frames */
278
meml_gotten = SUBL*(iLBCdec_inst->nsub+1-start);
280
if ( meml_gotten > CB_MEML ) {
283
for (k=0; k<meml_gotten; k++) {
284
mem[CB_MEML-1-k] = decresidual[(start-1)*SUBL + k];
286
memset(mem, 0, (CB_MEML-k)*sizeof(float));
288
/* loop over subframes to decode */
290
for (subframe=0; subframe<Nback; subframe++) {
292
/* construct decoded vector */
294
iCBConstruct(&reverseDecresidual[subframe*SUBL],
295
cb_index+subcount*CB_NSTAGES,
296
gain_index+subcount*CB_NSTAGES,
297
mem+CB_MEML-memLfTbl[subcount], memLfTbl[subcount],
302
memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
303
memcpy(mem+CB_MEML-SUBL,
304
&reverseDecresidual[subframe*SUBL],
314
/* get decoded residual from reversed vector */
316
for (i=0; i<SUBL*Nback; i++)
317
decresidual[SUBL*Nback - i - 1] =
318
reverseDecresidual[i];
322
/*----------------------------------------------------------------*
323
* main decoder function
324
*---------------------------------------------------------------*/
327
float *decblock, /* (o) decoded signal block */
328
unsigned char *bytes, /* (i) encoded signal bits */
329
iLBC_Dec_Inst_t *iLBCdec_inst, /* (i/o) the decoder state
331
int mode /* (i) 0: bad packet, PLC,
334
float data[BLOCKL_MAX];
335
float lsfdeq[LPC_FILTERORDER*LPC_N_MAX];
336
float PLCresidual[BLOCKL_MAX], PLClpc[LPC_FILTERORDER + 1];
337
float zeros[BLOCKL_MAX], one[LPC_FILTERORDER + 1];
338
int k, i, start, idxForMax, pos, lastpart, ulp;
341
int idxVec[STATE_LEN];
343
int gain_index[NASUB_MAX*CB_NSTAGES],
344
extra_gain_index[CB_NSTAGES];
345
int cb_index[CB_NSTAGES*NASUB_MAX], extra_cb_index[CB_NSTAGES];
346
int lsf_i[LSF_NSPLIT*LPC_N_MAX];
349
unsigned char *pbytes;
350
float weightdenum[(LPC_FILTERORDER + 1)*NSUB_MAX];
352
float syntdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
353
float decresidual[BLOCKL_MAX];
355
if (mode>0) { /* the data are good */
367
/* Set everything to zero before decoding */
369
for (k=0; k<LSF_NSPLIT*LPC_N_MAX; k++) {
375
for (k=0; k<iLBCdec_inst->state_short_len; k++) {
378
for (k=0; k<CB_NSTAGES; k++) {
381
for (k=0; k<CB_NSTAGES; k++) {
382
extra_gain_index[k]=0;
384
for (i=0; i<iLBCdec_inst->nasub; i++) {
385
for (k=0; k<CB_NSTAGES; k++) {
386
cb_index[i*CB_NSTAGES+k]=0;
389
for (i=0; i<iLBCdec_inst->nasub; i++) {
390
for (k=0; k<CB_NSTAGES; k++) {
391
gain_index[i*CB_NSTAGES+k]=0;
395
/* loop over ULP classes */
397
for (ulp=0; ulp<3; ulp++) {
400
for (k=0; k<LSF_NSPLIT*iLBCdec_inst->lpc_n; k++){
401
unpack( &pbytes, &lastpart,
402
iLBCdec_inst->ULP_inst->lsf_bits[k][ulp], &pos);
403
packcombine(&lsf_i[k], lastpart,
404
iLBCdec_inst->ULP_inst->lsf_bits[k][ulp]);
407
/* Start block info */
409
unpack( &pbytes, &lastpart,
410
iLBCdec_inst->ULP_inst->start_bits[ulp], &pos);
411
packcombine(&start, lastpart,
412
iLBCdec_inst->ULP_inst->start_bits[ulp]);
414
unpack( &pbytes, &lastpart,
420
iLBCdec_inst->ULP_inst->startfirst_bits[ulp], &pos);
421
packcombine(&state_first, lastpart,
422
iLBCdec_inst->ULP_inst->startfirst_bits[ulp]);
424
unpack( &pbytes, &lastpart,
425
iLBCdec_inst->ULP_inst->scale_bits[ulp], &pos);
426
packcombine(&idxForMax, lastpart,
427
iLBCdec_inst->ULP_inst->scale_bits[ulp]);
429
for (k=0; k<iLBCdec_inst->state_short_len; k++) {
430
unpack( &pbytes, &lastpart,
431
iLBCdec_inst->ULP_inst->state_bits[ulp], &pos);
432
packcombine(idxVec+k, lastpart,
433
iLBCdec_inst->ULP_inst->state_bits[ulp]);
436
/* 23/22 (20ms/30ms) sample block */
438
for (k=0; k<CB_NSTAGES; k++) {
439
unpack( &pbytes, &lastpart,
440
iLBCdec_inst->ULP_inst->extra_cb_index[k][ulp],
442
packcombine(extra_cb_index+k, lastpart,
443
iLBCdec_inst->ULP_inst->extra_cb_index[k][ulp]);
445
for (k=0; k<CB_NSTAGES; k++) {
446
unpack( &pbytes, &lastpart,
447
iLBCdec_inst->ULP_inst->extra_cb_gain[k][ulp],
449
packcombine(extra_gain_index+k, lastpart,
450
iLBCdec_inst->ULP_inst->extra_cb_gain[k][ulp]);
453
/* The two/four (20ms/30ms) 40 sample sub-blocks */
455
for (i=0; i<iLBCdec_inst->nasub; i++) {
456
for (k=0; k<CB_NSTAGES; k++) {
457
unpack( &pbytes, &lastpart,
458
iLBCdec_inst->ULP_inst->cb_index[i][k][ulp],
460
packcombine(cb_index+i*CB_NSTAGES+k, lastpart,
461
iLBCdec_inst->ULP_inst->cb_index[i][k][ulp]);
465
for (i=0; i<iLBCdec_inst->nasub; i++) {
466
for (k=0; k<CB_NSTAGES; k++) {
467
unpack( &pbytes, &lastpart,
473
iLBCdec_inst->ULP_inst->cb_gain[i][k][ulp],
475
packcombine(gain_index+i*CB_NSTAGES+k, lastpart,
476
iLBCdec_inst->ULP_inst->cb_gain[i][k][ulp]);
480
/* Extract last bit. If it is 1 this indicates an
482
unpack( &pbytes, &last_bit, 1, &pos);
484
/* Check for bit errors or empty/lost frames */
487
if (iLBCdec_inst->mode==20 && start>3)
489
if (iLBCdec_inst->mode==30 && start>5)
494
if (mode==1) { /* No bit errors was detected,
498
index_conv_dec(cb_index);
502
SimplelsfDEQ(lsfdeq, lsf_i, iLBCdec_inst->lpc_n);
503
check=LSF_check(lsfdeq, LPC_FILTERORDER,
504
iLBCdec_inst->lpc_n);
505
DecoderInterpolateLSF(syntdenum, weightdenum,
506
lsfdeq, LPC_FILTERORDER, iLBCdec_inst);
508
Decode(iLBCdec_inst, decresidual, start, idxForMax,
509
idxVec, syntdenum, cb_index, gain_index,
510
extra_cb_index, extra_gain_index,
513
/* preparing the plc for a future loss! */
515
doThePLC(PLCresidual, PLClpc, 0, decresidual,
517
(LPC_FILTERORDER + 1)*(iLBCdec_inst->nsub - 1),
518
(*iLBCdec_inst).last_lag, iLBCdec_inst);
526
memcpy(decresidual, PLCresidual,
527
iLBCdec_inst->blockl*sizeof(float));
533
/* the data is bad (either a PLC call
534
* was made or a severe bit error was detected)
537
/* packet loss conceal */
539
memset(zeros, 0, BLOCKL_MAX*sizeof(float));
542
memset(one+1, 0, LPC_FILTERORDER*sizeof(float));
546
doThePLC(PLCresidual, PLClpc, 1, zeros, one,
547
(*iLBCdec_inst).last_lag, iLBCdec_inst);
548
memcpy(decresidual, PLCresidual,
549
iLBCdec_inst->blockl*sizeof(float));
551
order_plus_one = LPC_FILTERORDER + 1;
552
for (i = 0; i < iLBCdec_inst->nsub; i++) {
553
memcpy(syntdenum+(i*order_plus_one), PLClpc,
554
order_plus_one*sizeof(float));
558
if (iLBCdec_inst->use_enhancer == 1) {
562
iLBCdec_inst->last_lag =
563
enhancerInterface(data, decresidual, iLBCdec_inst);
565
/* synthesis filtering */
567
if (iLBCdec_inst->mode==20) {
568
/* Enhancer has 40 samples delay */
570
syntFilter(data + i*SUBL,
571
iLBCdec_inst->old_syntdenum +
572
(i+iLBCdec_inst->nsub-1)*(LPC_FILTERORDER+1),
573
SUBL, iLBCdec_inst->syntMem);
579
for (i=1; i < iLBCdec_inst->nsub; i++) {
580
syntFilter(data + i*SUBL,
581
syntdenum + (i-1)*(LPC_FILTERORDER+1),
582
SUBL, iLBCdec_inst->syntMem);
584
} else if (iLBCdec_inst->mode==30) {
585
/* Enhancer has 80 samples delay */
586
for (i=0; i < 2; i++) {
587
syntFilter(data + i*SUBL,
588
iLBCdec_inst->old_syntdenum +
589
(i+iLBCdec_inst->nsub-2)*(LPC_FILTERORDER+1),
590
SUBL, iLBCdec_inst->syntMem);
592
for (i=2; i < iLBCdec_inst->nsub; i++) {
593
syntFilter(data + i*SUBL,
594
syntdenum + (i-2)*(LPC_FILTERORDER+1), SUBL,
595
iLBCdec_inst->syntMem);
603
maxcc = xCorrCoef(&decresidual[BLOCKL_MAX-ENH_BLOCKL],
604
&decresidual[BLOCKL_MAX-ENH_BLOCKL-lag], ENH_BLOCKL);
606
for (ilag=21; ilag<120; ilag++) {
607
cc = xCorrCoef(&decresidual[BLOCKL_MAX-ENH_BLOCKL],
608
&decresidual[BLOCKL_MAX-ENH_BLOCKL-ilag],
616
iLBCdec_inst->last_lag = lag;
618
/* copy data and run synthesis filter */
620
memcpy(data, decresidual,
621
iLBCdec_inst->blockl*sizeof(float));
622
for (i=0; i < iLBCdec_inst->nsub; i++) {
623
syntFilter(data + i*SUBL,
624
syntdenum + i*(LPC_FILTERORDER+1), SUBL,
625
iLBCdec_inst->syntMem);
634
/* high pass filtering on output if desired, otherwise
637
hpOutput(data, iLBCdec_inst->blockl,
638
decblock,iLBCdec_inst->hpomem);
640
/* memcpy(decblock,data,iLBCdec_inst->blockl*sizeof(float));*/
642
memcpy(iLBCdec_inst->old_syntdenum, syntdenum,
644
iLBCdec_inst->nsub*(LPC_FILTERORDER+1)*sizeof(float));
646
iLBCdec_inst->prev_enh_pl=0;
648
if (mode==0) { /* PLC was used */
649
iLBCdec_inst->prev_enh_pl=1;