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 "StateConstructW.h"
18    #include "LPCdecode.h"
19    #include "iCBConstruct.h"
20    #include "doCPLC.h"
21    #include "helpfun.h"
22    #include "constants.h"
23    #include "packing.h"
24    #include "string.h"
25    #include "enhancer.h"
26    #include "hpOutput.h"
27    #include "syntFilter.h"
28 
29    /*----------------------------------------------------------------*
30     *  Initiation of decoder instance.
31     *---------------------------------------------------------------*/
32 
initDecode(iLBC_Dec_Inst_t * iLBCdec_inst,int mode,int use_enhancer)33    short initDecode(                   /* (o) Number of decoded
34                                               samples */
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
38                                               0 to run without
39                                                 enhancer */
40    ){
41        int i;
42 
43        iLBCdec_inst->mode = mode;
44 
45 
46 
47 
48 
49        if (mode==30) {
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;
57            /* ULP init */
58            iLBCdec_inst->ULP_inst=&ULP_30msTbl;
59        }
60        else if (mode==20) {
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;
68            /* ULP init */
69            iLBCdec_inst->ULP_inst=&ULP_20msTbl;
70        }
71        else {
72            exit(2);
73        }
74 
75        memset(iLBCdec_inst->syntMem, 0,
76            LPC_FILTERORDER*sizeof(float));
77        memcpy((*iLBCdec_inst).lsfdeqold, lsfmeanTbl,
78            LPC_FILTERORDER*sizeof(float));
79 
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;
84 
85        iLBCdec_inst->last_lag = 20;
86 
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;
96 
97 
98 
99 
100 
101 
102        memset(iLBCdec_inst->hpomem, 0, 4*sizeof(float));
103 
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;
108 
109        iLBCdec_inst->prev_enh_pl = 0;
110 
111        return (iLBCdec_inst->blockl);
112    }
113 
114    /*----------------------------------------------------------------*
115     *  frame residual decoder function (subrutine to iLBC_decode)
116     *---------------------------------------------------------------*/
117 
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)118    void Decode(
119        iLBC_Dec_Inst_t *iLBCdec_inst,  /* (i/o) the decoder state
120                                                 structure */
121        float *decresidual,             /* (o) decoded residual frame */
122        int start,                      /* (i) location of start
123                                               state */
124        int idxForMax,                  /* (i) codebook index for the
125                                               maximum value */
126        int *idxVec,                /* (i) codebook indexes for the
127                                               samples  in the start
128                                               state */
129        float *syntdenum,               /* (i) the decoded synthesis
130                                               filter coefficients */
131        int *cb_index,                  /* (i) the indexes for the
132                                               adaptive codebook */
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
137                                               of start state */
138        int *extra_gain_index,          /* (i) the indexes for the
139                                               corresponding gains */
140        int state_first                 /* (i) 1 if non adaptive part
141                                               of start state comes
142                                               first 0 if that part
143                                               comes last */
144    ){
145        float reverseDecresidual[BLOCKL_MAX], mem[CB_MEML];
146        int k, meml_gotten, Nfor, Nback, i;
147        int diff, start_pos;
148        int subcount, subframe;
149 
150 
151 
152 
153 
154 
155        diff = STATE_LEN - iLBCdec_inst->state_short_len;
156 
157        if (state_first == 1) {
158            start_pos = (start-1)*SUBL;
159        } else {
160            start_pos = (start-1)*SUBL + diff;
161        }
162 
163        /* decode scalar part of start state */
164 
165        StateConstructW(idxForMax, idxVec,
166            &syntdenum[(start-1)*(LPC_FILTERORDER+1)],
167            &decresidual[start_pos], iLBCdec_inst->state_short_len);
168 
169 
170        if (state_first) { /* put adaptive part in the end */
171 
172            /* setup memory */
173 
174            memset(mem, 0,
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));
179 
180            /* construct decoded vector */
181 
182            iCBConstruct(
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);
186 
187        }
188        else {/* put adaptive part in the beginning */
189 
190            /* create reversed vectors for prediction */
191 
192            for (k=0; k<diff; k++) {
193                reverseDecresidual[k] =
194                    decresidual[(start+1)*SUBL-1-
195                            (k+iLBCdec_inst->state_short_len)];
196            }
197 
198            /* setup memory */
199 
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];
203 
204 
205 
206 
207 
208            }
209            memset(mem, 0, (CB_MEML-k)*sizeof(float));
210 
211            /* construct decoded vector */
212 
213            iCBConstruct(reverseDecresidual, extra_cb_index,
214                extra_gain_index, mem+CB_MEML-stMemLTbl, stMemLTbl,
215                diff, CB_NSTAGES);
216 
217            /* get decoded residual from reversed vector */
218 
219            for (k=0; k<diff; k++) {
220                decresidual[start_pos-1-k] = reverseDecresidual[k];
221            }
222        }
223 
224        /* counter for predicted sub-frames */
225 
226        subcount=0;
227 
228        /* forward prediction of sub-frames */
229 
230        Nfor = iLBCdec_inst->nsub-start-1;
231 
232        if ( Nfor > 0 ){
233 
234            /* setup memory */
235 
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));
239 
240            /* loop over sub-frames to encode */
241 
242            for (subframe=0; subframe<Nfor; subframe++) {
243 
244                /* construct decoded vector */
245 
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);
251 
252                /* update memory */
253 
254                memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
255                memcpy(mem+CB_MEML-SUBL,
256 
257 
258 
259 
260 
261                    &decresidual[(start+1+subframe)*SUBL],
262                    SUBL*sizeof(float));
263 
264                subcount++;
265 
266            }
267 
268        }
269 
270        /* backward prediction of sub-frames */
271 
272        Nback = start-1;
273 
274        if ( Nback > 0 ) {
275 
276            /* setup memory */
277 
278            meml_gotten = SUBL*(iLBCdec_inst->nsub+1-start);
279 
280            if ( meml_gotten > CB_MEML ) {
281                meml_gotten=CB_MEML;
282            }
283            for (k=0; k<meml_gotten; k++) {
284                mem[CB_MEML-1-k] = decresidual[(start-1)*SUBL + k];
285            }
286            memset(mem, 0, (CB_MEML-k)*sizeof(float));
287 
288            /* loop over subframes to decode */
289 
290            for (subframe=0; subframe<Nback; subframe++) {
291 
292                /* construct decoded vector */
293 
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],
298                    SUBL, CB_NSTAGES);
299 
300                /* update memory */
301 
302                memcpy(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
303                memcpy(mem+CB_MEML-SUBL,
304                    &reverseDecresidual[subframe*SUBL],
305                    SUBL*sizeof(float));
306 
307                subcount++;
308            }
309 
310 
311 
312 
313 
314            /* get decoded residual from reversed vector */
315 
316            for (i=0; i<SUBL*Nback; i++)
317                decresidual[SUBL*Nback - i - 1] =
318                reverseDecresidual[i];
319        }
320    }
321 
322    /*----------------------------------------------------------------*
323     *  main decoder function
324     *---------------------------------------------------------------*/
325 
iLBC_decode(float * decblock,unsigned char * bytes,iLBC_Dec_Inst_t * iLBCdec_inst,int mode)326    void iLBC_decode(
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
330                                                 structure */
331        int mode                    /* (i) 0: bad packet, PLC,
332                                               1: normal */
333    ){
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;
339        int lag, ilag;
340        float cc, maxcc;
341        int idxVec[STATE_LEN];
342        /*       int check;*/
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];
347        int state_first;
348        int last_bit;
349        unsigned char *pbytes;
350        float weightdenum[(LPC_FILTERORDER + 1)*NSUB_MAX];
351        int order_plus_one;
352        float syntdenum[NSUB_MAX*(LPC_FILTERORDER+1)];
353        float decresidual[BLOCKL_MAX];
354 
355        if (mode>0) { /* the data are good */
356 
357            /* decode data */
358 
359            pbytes=bytes;
360            pos=0;
361 
362 
363 
364 
365 
366 
367            /* Set everything to zero before decoding */
368 
369            for (k=0; k<LSF_NSPLIT*LPC_N_MAX; k++) {
370                lsf_i[k]=0;
371            }
372            start=0;
373            state_first=0;
374            idxForMax=0;
375            for (k=0; k<iLBCdec_inst->state_short_len; k++) {
376                idxVec[k]=0;
377            }
378            for (k=0; k<CB_NSTAGES; k++) {
379                extra_cb_index[k]=0;
380            }
381            for (k=0; k<CB_NSTAGES; k++) {
382                extra_gain_index[k]=0;
383            }
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;
387                }
388            }
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;
392                }
393            }
394 
395            /* loop over ULP classes */
396 
397            for (ulp=0; ulp<3; ulp++) {
398 
399                /* LSF */
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]);
405                }
406 
407                /* Start block info */
408 
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]);
413 
414                unpack( &pbytes, &lastpart,
415 
416 
417 
418 
419 
420                    iLBCdec_inst->ULP_inst->startfirst_bits[ulp], &pos);
421                packcombine(&state_first, lastpart,
422                    iLBCdec_inst->ULP_inst->startfirst_bits[ulp]);
423 
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]);
428 
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]);
434                }
435 
436                /* 23/22 (20ms/30ms) sample block */
437 
438                for (k=0; k<CB_NSTAGES; k++) {
439                    unpack( &pbytes, &lastpart,
440                        iLBCdec_inst->ULP_inst->extra_cb_index[k][ulp],
441                        &pos);
442                    packcombine(extra_cb_index+k, lastpart,
443                        iLBCdec_inst->ULP_inst->extra_cb_index[k][ulp]);
444                }
445                for (k=0; k<CB_NSTAGES; k++) {
446                    unpack( &pbytes, &lastpart,
447                        iLBCdec_inst->ULP_inst->extra_cb_gain[k][ulp],
448                        &pos);
449                    packcombine(extra_gain_index+k, lastpart,
450                        iLBCdec_inst->ULP_inst->extra_cb_gain[k][ulp]);
451                }
452 
453                /* The two/four (20ms/30ms) 40 sample sub-blocks */
454 
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],
459                            &pos);
460                        packcombine(cb_index+i*CB_NSTAGES+k, lastpart,
461                        iLBCdec_inst->ULP_inst->cb_index[i][k][ulp]);
462                    }
463                }
464 
465                for (i=0; i<iLBCdec_inst->nasub; i++) {
466                    for (k=0; k<CB_NSTAGES; k++) {
467                        unpack( &pbytes, &lastpart,
468 
469 
470 
471 
472 
473                        iLBCdec_inst->ULP_inst->cb_gain[i][k][ulp],
474                            &pos);
475                        packcombine(gain_index+i*CB_NSTAGES+k, lastpart,
476                            iLBCdec_inst->ULP_inst->cb_gain[i][k][ulp]);
477                    }
478                }
479            }
480            /* Extract last bit. If it is 1 this indicates an
481               empty/lost frame */
482            unpack( &pbytes, &last_bit, 1, &pos);
483 
484            /* Check for bit errors or empty/lost frames */
485            if (start<1)
486                mode = 0;
487            if (iLBCdec_inst->mode==20 && start>3)
488                mode = 0;
489            if (iLBCdec_inst->mode==30 && start>5)
490                mode = 0;
491            if (last_bit==1)
492                mode = 0;
493 
494            if (mode==1) { /* No bit errors was detected,
495                              continue decoding */
496 
497                /* adjust index */
498                index_conv_dec(cb_index);
499 
500                /* decode the lsf */
501 
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);
507 
508                Decode(iLBCdec_inst, decresidual, start, idxForMax,
509                    idxVec, syntdenum, cb_index, gain_index,
510                    extra_cb_index, extra_gain_index,
511                    state_first);
512 
513                /* preparing the plc for a future loss! */
514 
515                doThePLC(PLCresidual, PLClpc, 0, decresidual,
516                    syntdenum +
517                    (LPC_FILTERORDER + 1)*(iLBCdec_inst->nsub - 1),
518                    (*iLBCdec_inst).last_lag, iLBCdec_inst);
519 
520 
521 
522 
523 
524 
525 
526                memcpy(decresidual, PLCresidual,
527                    iLBCdec_inst->blockl*sizeof(float));
528            }
529 
530        }
531 
532        if (mode == 0) {
533            /* the data is bad (either a PLC call
534             * was made or a severe bit error was detected)
535             */
536 
537            /* packet loss conceal */
538 
539            memset(zeros, 0, BLOCKL_MAX*sizeof(float));
540 
541            one[0] = 1;
542            memset(one+1, 0, LPC_FILTERORDER*sizeof(float));
543 
544            start=0;
545 
546            doThePLC(PLCresidual, PLClpc, 1, zeros, one,
547                (*iLBCdec_inst).last_lag, iLBCdec_inst);
548            memcpy(decresidual, PLCresidual,
549                iLBCdec_inst->blockl*sizeof(float));
550 
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));
555            }
556        }
557 
558        if (iLBCdec_inst->use_enhancer == 1) {
559 
560            /* post filtering */
561 
562            iLBCdec_inst->last_lag =
563                enhancerInterface(data, decresidual, iLBCdec_inst);
564 
565            /* synthesis filtering */
566 
567            if (iLBCdec_inst->mode==20) {
568                /* Enhancer has 40 samples delay */
569                i=0;
570                syntFilter(data + i*SUBL,
571                    iLBCdec_inst->old_syntdenum +
572                    (i+iLBCdec_inst->nsub-1)*(LPC_FILTERORDER+1),
573                    SUBL, iLBCdec_inst->syntMem);
574 
575 
576 
577 
578 
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);
583                }
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);
591                }
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);
596                }
597            }
598 
599        } else {
600 
601            /* Find last lag */
602            lag = 20;
603            maxcc = xCorrCoef(&decresidual[BLOCKL_MAX-ENH_BLOCKL],
604                &decresidual[BLOCKL_MAX-ENH_BLOCKL-lag], ENH_BLOCKL);
605 
606            for (ilag=21; ilag<120; ilag++) {
607                cc = xCorrCoef(&decresidual[BLOCKL_MAX-ENH_BLOCKL],
608                    &decresidual[BLOCKL_MAX-ENH_BLOCKL-ilag],
609                    ENH_BLOCKL);
610 
611                if (cc > maxcc) {
612                    maxcc = cc;
613                    lag = ilag;
614                }
615            }
616            iLBCdec_inst->last_lag = lag;
617 
618            /* copy data and run synthesis filter */
619 
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);
626            }
627 
628 
629 
630 
631 
632        }
633 
634        /* high pass filtering on output if desired, otherwise
635           copy to out */
636 
637        hpOutput(data, iLBCdec_inst->blockl,
638                    decblock,iLBCdec_inst->hpomem);
639 
640        /* memcpy(decblock,data,iLBCdec_inst->blockl*sizeof(float));*/
641 
642        memcpy(iLBCdec_inst->old_syntdenum, syntdenum,
643 
644            iLBCdec_inst->nsub*(LPC_FILTERORDER+1)*sizeof(float));
645 
646        iLBCdec_inst->prev_enh_pl=0;
647 
648        if (mode==0) { /* PLC was used */
649            iLBCdec_inst->prev_enh_pl=1;
650        }
651    }
652 
653