1 
2 /******************************************************************
3 
4     iLBC Speech Coder ANSI-C Source Code
5 
6     iLBC_decode.c
7 
8     Copyright (C) The Internet Society (2004).
9     All Rights Reserved.
10 
11 ******************************************************************/
12 
13 #include <math.h>
14 #include <stdlib.h>
15 
16 #include "iLBC_define.h"
17 #include "iLBC_decode.h"
18 #include "StateConstructW.h"
19 #include "LPCdecode.h"
20 #include "iCBConstruct.h"
21 #include "doCPLC.h"
22 #include "helpfun.h"
23 #include "constants.h"
24 #include "packing.h"
25 #include "string.h"
26 #include "enhancer.h"
27 #include "hpOutput.h"
28 #include "syntFilter.h"
29 
30 /*----------------------------------------------------------------*
31  *  Initiation of decoder instance.
32  *---------------------------------------------------------------*/
33 
initDecode(iLBC_Dec_Inst_t * iLBCdec_inst,int mode,int use_enhancer)34 short initDecode(                   /* (o) Number of decoded
35                                            samples */
36     iLBC_Dec_Inst_t *iLBCdec_inst,  /* (i/o) Decoder instance */
37     int mode,                       /* (i) frame size mode */
38     int use_enhancer                /* (i) 1 to use enhancer
39                                            0 to run without
40                                              enhancer */
41 ){
42     int i;
43 
44     iLBCdec_inst->mode = mode;
45 
46     if (mode==30) {
47         iLBCdec_inst->blockl = BLOCKL_30MS;
48         iLBCdec_inst->nsub = NSUB_30MS;
49         iLBCdec_inst->nasub = NASUB_30MS;
50         iLBCdec_inst->lpc_n = LPC_N_30MS;
51 
52 
53         iLBCdec_inst->no_of_bytes = NO_OF_BYTES_30MS;
54         iLBCdec_inst->no_of_words = NO_OF_WORDS_30MS;
55         iLBCdec_inst->state_short_len=STATE_SHORT_LEN_30MS;
56         /* ULP init */
57         iLBCdec_inst->ULP_inst=&ULP_30msTbl;
58     }
59     else if (mode==20) {
60         iLBCdec_inst->blockl = BLOCKL_20MS;
61         iLBCdec_inst->nsub = NSUB_20MS;
62         iLBCdec_inst->nasub = NASUB_20MS;
63         iLBCdec_inst->lpc_n = LPC_N_20MS;
64         iLBCdec_inst->no_of_bytes = NO_OF_BYTES_20MS;
65         iLBCdec_inst->no_of_words = NO_OF_WORDS_20MS;
66         iLBCdec_inst->state_short_len=STATE_SHORT_LEN_20MS;
67         /* ULP init */
68         iLBCdec_inst->ULP_inst=&ULP_20msTbl;
69     }
70     else {
71         exit(2);
72     }
73 
74     memset(iLBCdec_inst->syntMem, 0,
75         LPC_FILTERORDER*sizeof(float));
76     memcpy((*iLBCdec_inst).lsfdeqold, lsfmeanTbl,
77         LPC_FILTERORDER*sizeof(float));
78 
79     memset(iLBCdec_inst->old_syntdenum, 0,
80         ((LPC_FILTERORDER + 1)*NSUB_MAX)*sizeof(float));
81     for (i=0; i<NSUB_MAX; i++)
82         iLBCdec_inst->old_syntdenum[i*(LPC_FILTERORDER+1)]=1.0;
83 
84     iLBCdec_inst->last_lag = 20;
85 
86     iLBCdec_inst->prevLag = 120;
87     iLBCdec_inst->per = 0.0;
88     iLBCdec_inst->consPLICount = 0;
89     iLBCdec_inst->prevPLI = 0;
90     iLBCdec_inst->prevLpc[0] = 1.0;
91     memset(iLBCdec_inst->prevLpc+1,0,
92         LPC_FILTERORDER*sizeof(float));
93     memset(iLBCdec_inst->prevResidual, 0, BLOCKL_MAX*sizeof(float));
94     iLBCdec_inst->seed=777;
95 
96     memset(iLBCdec_inst->hpomem, 0, 4*sizeof(float));
97 
98     iLBCdec_inst->use_enhancer = use_enhancer;
99     memset(iLBCdec_inst->enh_buf, 0, ENH_BUFL*sizeof(float));
100     for (i=0;i<ENH_NBLOCKS_TOT;i++)
101         iLBCdec_inst->enh_period[i]=(float)40.0;
102 
103     iLBCdec_inst->prev_enh_pl = 0;
104 
105     return (iLBCdec_inst->blockl);
106 }
107 
108 
109 
110 /*----------------------------------------------------------------*
111  *  frame residual decoder function (subrutine to iLBC_decode)
112  *---------------------------------------------------------------*/
113 
Decode(iLBC_Dec_Inst_t * iLBCdec_inst,float * decresidual,int start,int idxForMax,int * idxVec,float * syntdenum,int * cb_index,int * gain_index,int * extra_cb_index,int * extra_gain_index,int state_first)114 static void Decode(
115     iLBC_Dec_Inst_t *iLBCdec_inst,  /* (i/o) the decoder state
116                                              structure */
117     float *decresidual,             /* (o) decoded residual frame */
118     int start,                      /* (i) location of start
119                                            state */
120     int idxForMax,                  /* (i) codebook index for the
121                                            maximum value */
122     int *idxVec,                /* (i) codebook indexes for the
123                                            samples  in the start
124                                            state */
125     float *syntdenum,               /* (i) the decoded synthesis
126                                            filter coefficients */
127     int *cb_index,                  /* (i) the indexes for the
128                                            adaptive codebook */
129     int *gain_index,            /* (i) the indexes for the
130                                            corresponding gains */
131     int *extra_cb_index,        /* (i) the indexes for the
132                                            adaptive codebook part
133                                            of start state */
134     int *extra_gain_index,          /* (i) the indexes for the
135                                            corresponding gains */
136     int state_first                 /* (i) 1 if non adaptive part
137                                            of start state comes
138                                            first 0 if that part
139                                            comes last */
140 ){
141     float reverseDecresidual[BLOCKL_MAX], mem[CB_MEML];
142     int k, meml_gotten, Nfor, Nback, i;
143     int diff, start_pos;
144     int subcount, subframe;
145 
146     diff = STATE_LEN - iLBCdec_inst->state_short_len;
147 
148     if (state_first == 1) {
149         start_pos = (start-1)*SUBL;
150     } else {
151         start_pos = (start-1)*SUBL + diff;
152     }
153 
154     /* decode scalar part of start state */
155 
156     StateConstructW(idxForMax, idxVec,
157         &syntdenum[(start-1)*(LPC_FILTERORDER+1)],
158         &decresidual[start_pos], iLBCdec_inst->state_short_len);
159 
160 
161     if (state_first) { /* put adaptive part in the end */
162 
163 
164 
165         /* setup memory */
166 
167         memset(mem, 0,
168             (CB_MEML-iLBCdec_inst->state_short_len)*sizeof(float));
169         memcpy(mem+CB_MEML-iLBCdec_inst->state_short_len,
170             decresidual+start_pos,
171             iLBCdec_inst->state_short_len*sizeof(float));
172 
173         /* construct decoded vector */
174 
175         iCBConstruct(
176             &decresidual[start_pos+iLBCdec_inst->state_short_len],
177             extra_cb_index, extra_gain_index, mem+CB_MEML-stMemLTbl,
178             stMemLTbl, diff, CB_NSTAGES);
179 
180     }
181     else {/* put adaptive part in the beginning */
182 
183         /* create reversed vectors for prediction */
184 
185         for (k=0; k<diff; k++) {
186             reverseDecresidual[k] =
187                 decresidual[(start+1)*SUBL-1-
188                         (k+iLBCdec_inst->state_short_len)];
189         }
190 
191         /* setup memory */
192 
193         meml_gotten = iLBCdec_inst->state_short_len;
194         for (k=0; k<meml_gotten; k++){
195             mem[CB_MEML-1-k] = decresidual[start_pos + k];
196         }
197         memset(mem, 0, (CB_MEML-k)*sizeof(float));
198 
199         /* construct decoded vector */
200 
201         iCBConstruct(reverseDecresidual, extra_cb_index,
202             extra_gain_index, mem+CB_MEML-stMemLTbl, stMemLTbl,
203             diff, CB_NSTAGES);
204 
205         /* get decoded residual from reversed vector */
206 
207         for (k=0; k<diff; k++) {
208             decresidual[start_pos-1-k] = reverseDecresidual[k];
209         }
210     }
211 
212     /* counter for predicted sub-frames */
213 
214     subcount=0;
215 
216     /* forward prediction of sub-frames */
217 
218     Nfor = iLBCdec_inst->nsub-start-1;
219 
220 
221 
222     if ( Nfor > 0 ){
223 
224         /* setup memory */
225 
226         memset(mem, 0, (CB_MEML-STATE_LEN)*sizeof(float));
227         memcpy(mem+CB_MEML-STATE_LEN, decresidual+(start-1)*SUBL,
228             STATE_LEN*sizeof(float));
229 
230         /* loop over sub-frames to encode */
231 
232         for (subframe=0; subframe<Nfor; subframe++) {
233 
234             /* construct decoded vector */
235 
236             iCBConstruct(&decresidual[(start+1+subframe)*SUBL],
237                 cb_index+subcount*CB_NSTAGES,
238                 gain_index+subcount*CB_NSTAGES,
239                 mem+CB_MEML-memLfTbl[subcount],
240                 memLfTbl[subcount], SUBL, CB_NSTAGES);
241 
242             /* update memory */
243 
244             memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
245             memcpy(mem+CB_MEML-SUBL,
246                 &decresidual[(start+1+subframe)*SUBL],
247                 SUBL*sizeof(float));
248 
249             subcount++;
250 
251         }
252 
253     }
254 
255     /* backward prediction of sub-frames */
256 
257     Nback = start-1;
258 
259     if ( Nback > 0 ) {
260 
261         /* setup memory */
262 
263         meml_gotten = SUBL*(iLBCdec_inst->nsub+1-start);
264 
265         if ( meml_gotten > CB_MEML ) {
266             meml_gotten=CB_MEML;
267         }
268         for (k=0; k<meml_gotten; k++) {
269             mem[CB_MEML-1-k] = decresidual[(start-1)*SUBL + k];
270         }
271         memset(mem, 0, (CB_MEML-k)*sizeof(float));
272 
273         /* loop over subframes to decode */
274 
275 
276 
277         for (subframe=0; subframe<Nback; subframe++) {
278 
279             /* construct decoded vector */
280 
281             iCBConstruct(&reverseDecresidual[subframe*SUBL],
282                 cb_index+subcount*CB_NSTAGES,
283                 gain_index+subcount*CB_NSTAGES,
284                 mem+CB_MEML-memLfTbl[subcount], memLfTbl[subcount],
285                 SUBL, CB_NSTAGES);
286 
287             /* update memory */
288 
289             memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
290             memcpy(mem+CB_MEML-SUBL,
291                 &reverseDecresidual[subframe*SUBL],
292                 SUBL*sizeof(float));
293 
294             subcount++;
295         }
296 
297         /* get decoded residual from reversed vector */
298 
299         for (i=0; i<SUBL*Nback; i++)
300             decresidual[SUBL*Nback - i - 1] =
301             reverseDecresidual[i];
302     }
303 }
304 
305 /*----------------------------------------------------------------*
306  *  main decoder function
307  *---------------------------------------------------------------*/
308 
iLBC_decode(float * decblock,unsigned char * bytes,iLBC_Dec_Inst_t * iLBCdec_inst,int mode)309 void iLBC_decode(
310     float *decblock,            /* (o) decoded signal block */
311     unsigned char *bytes,           /* (i) encoded signal bits */
312     iLBC_Dec_Inst_t *iLBCdec_inst,  /* (i/o) the decoder state
313                                              structure */
314     int mode                    /* (i) 0: bad packet, PLC,
315                                            1: normal */
316 ){
317     float data[BLOCKL_MAX];
318     float lsfdeq[LPC_FILTERORDER*LPC_N_MAX];
319     float PLCresidual[BLOCKL_MAX], PLClpc[LPC_FILTERORDER + 1];
320     float zeros[BLOCKL_MAX], one[LPC_FILTERORDER + 1];
321     int k, i, start, idxForMax, pos, lastpart, ulp;
322     int lag, ilag;
323     float cc, maxcc;
324     int idxVec[STATE_LEN];
325     int check;
326     int gain_index[NASUB_MAX*CB_NSTAGES],
327         extra_gain_index[CB_NSTAGES];
328     int cb_index[CB_NSTAGES*NASUB_MAX], extra_cb_index[CB_NSTAGES];
329     int lsf_i[LSF_NSPLIT*LPC_N_MAX];
330     int state_first;
331 
332 
333     int last_bit;
334     unsigned char *pbytes;
335     float weightdenum[(LPC_FILTERORDER + 1)*NSUB_MAX];
336     int order_plus_one;
337     float syntdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
338     float decresidual[BLOCKL_MAX];
339 
340     if (mode>0) { /* the data are good */
341 
342         /* decode data */
343 
344         pbytes=bytes;
345         pos=0;
346 
347         /* Set everything to zero before decoding */
348 
349         for (k=0; k<LSF_NSPLIT*LPC_N_MAX; k++) {
350             lsf_i[k]=0;
351         }
352         start=0;
353         state_first=0;
354         idxForMax=0;
355         for (k=0; k<iLBCdec_inst->state_short_len; k++) {
356             idxVec[k]=0;
357         }
358         for (k=0; k<CB_NSTAGES; k++) {
359             extra_cb_index[k]=0;
360         }
361         for (k=0; k<CB_NSTAGES; k++) {
362             extra_gain_index[k]=0;
363         }
364         for (i=0; i<iLBCdec_inst->nasub; i++) {
365             for (k=0; k<CB_NSTAGES; k++) {
366                 cb_index[i*CB_NSTAGES+k]=0;
367             }
368         }
369         for (i=0; i<iLBCdec_inst->nasub; i++) {
370             for (k=0; k<CB_NSTAGES; k++) {
371                 gain_index[i*CB_NSTAGES+k]=0;
372             }
373         }
374 
375         /* loop over ULP classes */
376 
377         for (ulp=0; ulp<3; ulp++) {
378 
379             /* LSF */
380             for (k=0; k<LSF_NSPLIT*iLBCdec_inst->lpc_n; k++){
381                 unpack( &pbytes, &lastpart,
382                     iLBCdec_inst->ULP_inst->lsf_bits[k][ulp], &pos);
383                 packcombine(&lsf_i[k], lastpart,
384                     iLBCdec_inst->ULP_inst->lsf_bits[k][ulp]);
385             }
386 
387 
388 
389             /* Start block info */
390 
391             unpack( &pbytes, &lastpart,
392                 iLBCdec_inst->ULP_inst->start_bits[ulp], &pos);
393             packcombine(&start, lastpart,
394                 iLBCdec_inst->ULP_inst->start_bits[ulp]);
395 
396             unpack( &pbytes, &lastpart,
397                 iLBCdec_inst->ULP_inst->startfirst_bits[ulp], &pos);
398             packcombine(&state_first, lastpart,
399                 iLBCdec_inst->ULP_inst->startfirst_bits[ulp]);
400 
401             unpack( &pbytes, &lastpart,
402                 iLBCdec_inst->ULP_inst->scale_bits[ulp], &pos);
403             packcombine(&idxForMax, lastpart,
404                 iLBCdec_inst->ULP_inst->scale_bits[ulp]);
405 
406             for (k=0; k<iLBCdec_inst->state_short_len; k++) {
407                 unpack( &pbytes, &lastpart,
408                     iLBCdec_inst->ULP_inst->state_bits[ulp], &pos);
409                 packcombine(idxVec+k, lastpart,
410                     iLBCdec_inst->ULP_inst->state_bits[ulp]);
411             }
412 
413             /* 23/22 (20ms/30ms) sample block */
414 
415             for (k=0; k<CB_NSTAGES; k++) {
416                 unpack( &pbytes, &lastpart,
417                     iLBCdec_inst->ULP_inst->extra_cb_index[k][ulp],
418                     &pos);
419                 packcombine(extra_cb_index+k, lastpart,
420                     iLBCdec_inst->ULP_inst->extra_cb_index[k][ulp]);
421             }
422             for (k=0; k<CB_NSTAGES; k++) {
423                 unpack( &pbytes, &lastpart,
424                     iLBCdec_inst->ULP_inst->extra_cb_gain[k][ulp],
425                     &pos);
426                 packcombine(extra_gain_index+k, lastpart,
427                     iLBCdec_inst->ULP_inst->extra_cb_gain[k][ulp]);
428             }
429 
430             /* The two/four (20ms/30ms) 40 sample sub-blocks */
431 
432             for (i=0; i<iLBCdec_inst->nasub; i++) {
433                 for (k=0; k<CB_NSTAGES; k++) {
434                     unpack( &pbytes, &lastpart,
435                     iLBCdec_inst->ULP_inst->cb_index[i][k][ulp],
436                         &pos);
437                     packcombine(cb_index+i*CB_NSTAGES+k, lastpart,
438                     iLBCdec_inst->ULP_inst->cb_index[i][k][ulp]);
439                 }
440             }
441 
442             for (i=0; i<iLBCdec_inst->nasub; i++) {
443 
444 
445                 for (k=0; k<CB_NSTAGES; k++) {
446                     unpack( &pbytes, &lastpart,
447 
448                     iLBCdec_inst->ULP_inst->cb_gain[i][k][ulp],
449                         &pos);
450                     packcombine(gain_index+i*CB_NSTAGES+k, lastpart,
451                         iLBCdec_inst->ULP_inst->cb_gain[i][k][ulp]);
452                 }
453             }
454         }
455         /* Extract last bit. If it is 1 this indicates an
456            empty/lost frame */
457         unpack( &pbytes, &last_bit, 1, &pos);
458 
459         /* Check for bit errors or empty/lost frames */
460         if (start<1)
461             mode = 0;
462         if (iLBCdec_inst->mode==20 && start>3)
463             mode = 0;
464         if (iLBCdec_inst->mode==30 && start>5)
465             mode = 0;
466         if (last_bit==1)
467             mode = 0;
468 
469         if (mode==1) { /* No bit errors was detected,
470                           continue decoding */
471 
472             /* adjust index */
473             index_conv_dec(cb_index);
474 
475             /* decode the lsf */
476 
477             SimplelsfDEQ(lsfdeq, lsf_i, iLBCdec_inst->lpc_n);
478             check=LSF_check(lsfdeq, LPC_FILTERORDER,
479                 iLBCdec_inst->lpc_n);
480             DecoderInterpolateLSF(syntdenum, weightdenum,
481                 lsfdeq, LPC_FILTERORDER, iLBCdec_inst);
482 
483             Decode(iLBCdec_inst, decresidual, start, idxForMax,
484                 idxVec, syntdenum, cb_index, gain_index,
485                 extra_cb_index, extra_gain_index,
486                 state_first);
487 
488             /* preparing the plc for a future loss! */
489 
490             doThePLC(PLCresidual, PLClpc, 0, decresidual,
491                 syntdenum +
492                 (LPC_FILTERORDER + 1)*(iLBCdec_inst->nsub - 1),
493                 (*iLBCdec_inst).last_lag, iLBCdec_inst);
494 
495 
496             memcpy(decresidual, PLCresidual,
497                 iLBCdec_inst->blockl*sizeof(float));
498         }
499 
500 
501 
502     }
503 
504     if (mode == 0) {
505         /* the data is bad (either a PLC call
506          * was made or a severe bit error was detected)
507          */
508 
509         /* packet loss conceal */
510 
511         memset(zeros, 0, BLOCKL_MAX*sizeof(float));
512 
513         one[0] = 1;
514         memset(one+1, 0, LPC_FILTERORDER*sizeof(float));
515 
516         start=0;
517 
518         doThePLC(PLCresidual, PLClpc, 1, zeros, one,
519             (*iLBCdec_inst).last_lag, iLBCdec_inst);
520         memcpy(decresidual, PLCresidual,
521             iLBCdec_inst->blockl*sizeof(float));
522 
523         order_plus_one = LPC_FILTERORDER + 1;
524         for (i = 0; i < iLBCdec_inst->nsub; i++) {
525             memcpy(syntdenum+(i*order_plus_one), PLClpc,
526                 order_plus_one*sizeof(float));
527         }
528     }
529 
530     if (iLBCdec_inst->use_enhancer == 1) {
531 
532         /* post filtering */
533 
534         iLBCdec_inst->last_lag =
535             enhancerInterface(data, decresidual, iLBCdec_inst);
536 
537         /* synthesis filtering */
538 
539         if (iLBCdec_inst->mode==20) {
540             /* Enhancer has 40 samples delay */
541             i=0;
542             syntFilter(data + i*SUBL,
543                 iLBCdec_inst->old_syntdenum +
544                 (i+iLBCdec_inst->nsub-1)*(LPC_FILTERORDER+1),
545                 SUBL, iLBCdec_inst->syntMem);
546             for (i=1; i < iLBCdec_inst->nsub; i++) {
547                 syntFilter(data + i*SUBL,
548                     syntdenum + (i-1)*(LPC_FILTERORDER+1),
549                     SUBL, iLBCdec_inst->syntMem);
550             }
551         } else if (iLBCdec_inst->mode==30) {
552             /* Enhancer has 80 samples delay */
553             for (i=0; i < 2; i++) {
554                 syntFilter(data + i*SUBL,
555 
556 
557                     iLBCdec_inst->old_syntdenum +
558                     (i+iLBCdec_inst->nsub-2)*(LPC_FILTERORDER+1),
559                     SUBL, iLBCdec_inst->syntMem);
560             }
561             for (i=2; i < iLBCdec_inst->nsub; i++) {
562                 syntFilter(data + i*SUBL,
563                     syntdenum + (i-2)*(LPC_FILTERORDER+1), SUBL,
564                     iLBCdec_inst->syntMem);
565             }
566         }
567 
568     } else {
569 
570         /* Find last lag */
571         lag = 20;
572         maxcc = xCorrCoef(&decresidual[BLOCKL_MAX-ENH_BLOCKL],
573             &decresidual[BLOCKL_MAX-ENH_BLOCKL-lag], ENH_BLOCKL);
574 
575         for (ilag=21; ilag<120; ilag++) {
576             cc = xCorrCoef(&decresidual[BLOCKL_MAX-ENH_BLOCKL],
577                 &decresidual[BLOCKL_MAX-ENH_BLOCKL-ilag],
578                 ENH_BLOCKL);
579 
580             if (cc > maxcc) {
581                 maxcc = cc;
582                 lag = ilag;
583             }
584         }
585         iLBCdec_inst->last_lag = lag;
586 
587         /* copy data and run synthesis filter */
588 
589         memcpy(data, decresidual,
590             iLBCdec_inst->blockl*sizeof(float));
591         for (i=0; i < iLBCdec_inst->nsub; i++) {
592             syntFilter(data + i*SUBL,
593                 syntdenum + i*(LPC_FILTERORDER+1), SUBL,
594                 iLBCdec_inst->syntMem);
595         }
596     }
597 
598     /* high pass filtering on output if desired, otherwise
599        copy to out */
600 
601     hpOutput(data, iLBCdec_inst->blockl,
602                 decblock,iLBCdec_inst->hpomem);
603 
604     /* memcpy(decblock,data,iLBCdec_inst->blockl*sizeof(float));*/
605 
606     memcpy(iLBCdec_inst->old_syntdenum, syntdenum,
607 
608         iLBCdec_inst->nsub*(LPC_FILTERORDER+1)*sizeof(float));
609 
610     iLBCdec_inst->prev_enh_pl=0;
611 
612 
613 
614     if (mode==0) { /* PLC was used */
615         iLBCdec_inst->prev_enh_pl=1;
616     }
617 }
618 
619 
620