Make file access in cdr_custom similar to cdr_csv.
[asterisk-bristuff.git] / codecs / ilbc / iLBC_decode.c
blob22bfff792d282ec4aff476ba57dfd4b2608448ae
2 /******************************************************************
4 iLBC Speech Coder ANSI-C Source Code
6 iLBC_decode.c
8 Copyright (C) The Internet Society (2004).
9 All Rights Reserved.
11 ******************************************************************/
13 #include <math.h>
14 #include <stdlib.h>
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"
30 /*----------------------------------------------------------------*
31 * Initiation of decoder instance.
32 *---------------------------------------------------------------*/
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 */
42 int i;
44 iLBCdec_inst->mode = mode;
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;
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;
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;
70 else {
71 exit(2);
74 memset(iLBCdec_inst->syntMem, 0,
75 LPC_FILTERORDER*sizeof(float));
76 memcpy((*iLBCdec_inst).lsfdeqold, lsfmeanTbl,
77 LPC_FILTERORDER*sizeof(float));
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;
84 iLBCdec_inst->last_lag = 20;
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;
96 memset(iLBCdec_inst->hpomem, 0, 4*sizeof(float));
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;
103 iLBCdec_inst->prev_enh_pl = 0;
105 return (iLBCdec_inst->blockl);
110 /*----------------------------------------------------------------*
111 * frame residual decoder function (subrutine to iLBC_decode)
112 *---------------------------------------------------------------*/
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 */
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;
146 diff = STATE_LEN - iLBCdec_inst->state_short_len;
148 if (state_first == 1) {
149 start_pos = (start-1)*SUBL;
150 } else {
151 start_pos = (start-1)*SUBL + diff;
154 /* decode scalar part of start state */
156 StateConstructW(idxForMax, idxVec,
157 &syntdenum[(start-1)*(LPC_FILTERORDER+1)],
158 &decresidual[start_pos], iLBCdec_inst->state_short_len);
161 if (state_first) { /* put adaptive part in the end */
165 /* setup memory */
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));
173 /* construct decoded vector */
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);
181 else {/* put adaptive part in the beginning */
183 /* create reversed vectors for prediction */
185 for (k=0; k<diff; k++) {
186 reverseDecresidual[k] =
187 decresidual[(start+1)*SUBL-1-
188 (k+iLBCdec_inst->state_short_len)];
191 /* setup memory */
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];
197 memset(mem, 0, (CB_MEML-k)*sizeof(float));
199 /* construct decoded vector */
201 iCBConstruct(reverseDecresidual, extra_cb_index,
202 extra_gain_index, mem+CB_MEML-stMemLTbl, stMemLTbl,
203 diff, CB_NSTAGES);
205 /* get decoded residual from reversed vector */
207 for (k=0; k<diff; k++) {
208 decresidual[start_pos-1-k] = reverseDecresidual[k];
212 /* counter for predicted sub-frames */
214 subcount=0;
216 /* forward prediction of sub-frames */
218 Nfor = iLBCdec_inst->nsub-start-1;
222 if ( Nfor > 0 ){
224 /* setup memory */
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));
230 /* loop over sub-frames to encode */
232 for (subframe=0; subframe<Nfor; subframe++) {
234 /* construct decoded vector */
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);
242 /* update memory */
244 memmove(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
245 memcpy(mem+CB_MEML-SUBL,
246 &decresidual[(start+1+subframe)*SUBL],
247 SUBL*sizeof(float));
249 subcount++;
255 /* backward prediction of sub-frames */
257 Nback = start-1;
259 if ( Nback > 0 ) {
261 /* setup memory */
263 meml_gotten = SUBL*(iLBCdec_inst->nsub+1-start);
265 if ( meml_gotten > CB_MEML ) {
266 meml_gotten=CB_MEML;
268 for (k=0; k<meml_gotten; k++) {
269 mem[CB_MEML-1-k] = decresidual[(start-1)*SUBL + k];
271 memset(mem, 0, (CB_MEML-k)*sizeof(float));
273 /* loop over subframes to decode */
277 for (subframe=0; subframe<Nback; subframe++) {
279 /* construct decoded vector */
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);
287 /* update memory */
289 memmove(mem, mem+SUBL, (CB_MEML-SUBL)*sizeof(float));
290 memcpy(mem+CB_MEML-SUBL,
291 &reverseDecresidual[subframe*SUBL],
292 SUBL*sizeof(float));
294 subcount++;
297 /* get decoded residual from reversed vector */
299 for (i=0; i<SUBL*Nback; i++)
300 decresidual[SUBL*Nback - i - 1] =
301 reverseDecresidual[i];
305 /*----------------------------------------------------------------*
306 * main decoder function
307 *---------------------------------------------------------------*/
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 */
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;
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];
340 if (mode>0) { /* the data are good */
342 /* decode data */
344 pbytes=bytes;
345 pos=0;
347 /* Set everything to zero before decoding */
349 for (k=0; k<LSF_NSPLIT*LPC_N_MAX; k++) {
350 lsf_i[k]=0;
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;
358 for (k=0; k<CB_NSTAGES; k++) {
359 extra_cb_index[k]=0;
361 for (k=0; k<CB_NSTAGES; k++) {
362 extra_gain_index[k]=0;
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;
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;
375 /* loop over ULP classes */
377 for (ulp=0; ulp<3; ulp++) {
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]);
389 /* Start block info */
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]);
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]);
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]);
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]);
413 /* 23/22 (20ms/30ms) sample block */
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]);
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]);
430 /* The two/four (20ms/30ms) 40 sample sub-blocks */
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]);
442 for (i=0; i<iLBCdec_inst->nasub; i++) {
445 for (k=0; k<CB_NSTAGES; k++) {
446 unpack( &pbytes, &lastpart,
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]);
455 /* Extract last bit. If it is 1 this indicates an
456 empty/lost frame */
457 unpack( &pbytes, &last_bit, 1, &pos);
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;
469 if (mode==1) { /* No bit errors was detected,
470 continue decoding */
472 /* adjust index */
473 index_conv_dec(cb_index);
475 /* decode the lsf */
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);
483 Decode(iLBCdec_inst, decresidual, start, idxForMax,
484 idxVec, syntdenum, cb_index, gain_index,
485 extra_cb_index, extra_gain_index,
486 state_first);
488 /* preparing the plc for a future loss! */
490 doThePLC(PLCresidual, PLClpc, 0, decresidual,
491 syntdenum +
492 (LPC_FILTERORDER + 1)*(iLBCdec_inst->nsub - 1),
493 (*iLBCdec_inst).last_lag, iLBCdec_inst);
496 memcpy(decresidual, PLCresidual,
497 iLBCdec_inst->blockl*sizeof(float));
504 if (mode == 0) {
505 /* the data is bad (either a PLC call
506 * was made or a severe bit error was detected)
509 /* packet loss conceal */
511 memset(zeros, 0, BLOCKL_MAX*sizeof(float));
513 one[0] = 1;
514 memset(one+1, 0, LPC_FILTERORDER*sizeof(float));
516 start=0;
518 doThePLC(PLCresidual, PLClpc, 1, zeros, one,
519 (*iLBCdec_inst).last_lag, iLBCdec_inst);
520 memcpy(decresidual, PLCresidual,
521 iLBCdec_inst->blockl*sizeof(float));
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));
530 if (iLBCdec_inst->use_enhancer == 1) {
532 /* post filtering */
534 iLBCdec_inst->last_lag =
535 enhancerInterface(data, decresidual, iLBCdec_inst);
537 /* synthesis filtering */
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);
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,
557 iLBCdec_inst->old_syntdenum +
558 (i+iLBCdec_inst->nsub-2)*(LPC_FILTERORDER+1),
559 SUBL, iLBCdec_inst->syntMem);
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);
568 } else {
570 /* Find last lag */
571 lag = 20;
572 maxcc = xCorrCoef(&decresidual[BLOCKL_MAX-ENH_BLOCKL],
573 &decresidual[BLOCKL_MAX-ENH_BLOCKL-lag], ENH_BLOCKL);
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);
580 if (cc > maxcc) {
581 maxcc = cc;
582 lag = ilag;
585 iLBCdec_inst->last_lag = lag;
587 /* copy data and run synthesis filter */
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);
598 /* high pass filtering on output if desired, otherwise
599 copy to out */
601 hpOutput(data, iLBCdec_inst->blockl,
602 decblock,iLBCdec_inst->hpomem);
604 /* memcpy(decblock,data,iLBCdec_inst->blockl*sizeof(float));*/
606 memcpy(iLBCdec_inst->old_syntdenum, syntdenum,
608 iLBCdec_inst->nsub*(LPC_FILTERORDER+1)*sizeof(float));
610 iLBCdec_inst->prev_enh_pl=0;
614 if (mode==0) { /* PLC was used */
615 iLBCdec_inst->prev_enh_pl=1;