comparison intercom/ilbc/iLBC_decode.c @ 2:13be24d74cd2

import intercom-0.4.1
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 09:57:52 +0200
parents
children
comparison
equal deleted inserted replaced
1:9cadc470e3da 2:13be24d74cd2
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
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 {
42 int i;
43
44 iLBCdec_inst->mode = mode;
45
46
47
48
49
50 if (mode == 30) {
51 iLBCdec_inst->blockl = BLOCKL_30MS;
52 iLBCdec_inst->nsub = NSUB_30MS;
53 iLBCdec_inst->nasub = NASUB_30MS;
54 iLBCdec_inst->lpc_n = LPC_N_30MS;
55 iLBCdec_inst->no_of_bytes = NO_OF_BYTES_30MS;
56 iLBCdec_inst->no_of_words = NO_OF_WORDS_30MS;
57 iLBCdec_inst->state_short_len = STATE_SHORT_LEN_30MS;
58 /* ULP init */
59 iLBCdec_inst->ULP_inst = &ULP_30msTbl;
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 } else {
71 exit(2);
72 }
73
74 memset(iLBCdec_inst->syntMem, 0, LPC_FILTERORDER * sizeof(float));
75 memcpy((*iLBCdec_inst).lsfdeqold, lsfmeanTbl,
76 LPC_FILTERORDER * sizeof(float));
77
78 memset(iLBCdec_inst->old_syntdenum, 0,
79 ((LPC_FILTERORDER + 1) * NSUB_MAX) * sizeof(float));
80 for (i = 0; i < NSUB_MAX; i++)
81 iLBCdec_inst->old_syntdenum[i * (LPC_FILTERORDER + 1)] = 1.0;
82
83 iLBCdec_inst->last_lag = 20;
84
85 iLBCdec_inst->prevLag = 120;
86 iLBCdec_inst->per = 0.0;
87 iLBCdec_inst->consPLICount = 0;
88 iLBCdec_inst->prevPLI = 0;
89 iLBCdec_inst->prevLpc[0] = 1.0;
90 memset(iLBCdec_inst->prevLpc + 1, 0, LPC_FILTERORDER * sizeof(float));
91 memset(iLBCdec_inst->prevResidual, 0, BLOCKL_MAX * sizeof(float));
92 iLBCdec_inst->seed = 777;
93
94
95
96
97
98
99 memset(iLBCdec_inst->hpomem, 0, 4 * sizeof(float));
100
101 iLBCdec_inst->use_enhancer = use_enhancer;
102 memset(iLBCdec_inst->enh_buf, 0, ENH_BUFL * sizeof(float));
103 for (i = 0; i < ENH_NBLOCKS_TOT; i++)
104 iLBCdec_inst->enh_period[i] = (float) 40.0;
105
106 iLBCdec_inst->prev_enh_pl = 0;
107
108 return (iLBCdec_inst->blockl);
109 }
110
111 /*----------------------------------------------------------------*
112 * frame residual decoder function (subrutine to iLBC_decode)
113 *---------------------------------------------------------------*/
114
115 void Decode(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 {
142 float reverseDecresidual[BLOCKL_MAX], mem[CB_MEML];
143 int k, meml_gotten, Nfor, Nback, i;
144 int diff, start_pos;
145 int subcount, subframe;
146
147
148
149
150
151
152 diff = STATE_LEN - iLBCdec_inst->state_short_len;
153
154 if (state_first == 1) {
155 start_pos = (start - 1) * SUBL;
156 } else {
157 start_pos = (start - 1) * SUBL + diff;
158 }
159
160 /* decode scalar part of start state */
161
162 StateConstructW(idxForMax, idxVec,
163 &syntdenum[(start - 1) * (LPC_FILTERORDER + 1)],
164 &decresidual[start_pos], iLBCdec_inst->state_short_len);
165
166
167 if (state_first) { /* put adaptive part in the end */
168
169 /* setup memory */
170
171 memset(mem, 0,
172 (CB_MEML - iLBCdec_inst->state_short_len) * sizeof(float));
173 memcpy(mem + CB_MEML - iLBCdec_inst->state_short_len,
174 decresidual + start_pos,
175 iLBCdec_inst->state_short_len * sizeof(float));
176
177 /* construct decoded vector */
178
179 iCBConstruct(&decresidual[start_pos +
180 iLBCdec_inst->state_short_len], extra_cb_index,
181 extra_gain_index, mem + CB_MEML - stMemLTbl, stMemLTbl, diff,
182 CB_NSTAGES);
183
184 } else { /* put adaptive part in the beginning */
185
186 /* create reversed vectors for prediction */
187
188 for (k = 0; k < diff; k++) {
189 reverseDecresidual[k] =
190 decresidual[(start + 1) * SUBL - 1 -
191 (k + iLBCdec_inst->state_short_len)];
192 }
193
194 /* setup memory */
195
196 meml_gotten = iLBCdec_inst->state_short_len;
197 for (k = 0; k < meml_gotten; k++) {
198 mem[CB_MEML - 1 - k] = decresidual[start_pos + k];
199
200
201
202
203
204 }
205 memset(mem, 0, (CB_MEML - k) * sizeof(float));
206
207 /* construct decoded vector */
208
209 iCBConstruct(reverseDecresidual, extra_cb_index,
210 extra_gain_index, mem + CB_MEML - stMemLTbl, stMemLTbl,
211 diff, CB_NSTAGES);
212
213 /* get decoded residual from reversed vector */
214
215 for (k = 0; k < diff; k++) {
216 decresidual[start_pos - 1 - k] = reverseDecresidual[k];
217 }
218 }
219
220 /* counter for predicted sub-frames */
221
222 subcount = 0;
223
224 /* forward prediction of sub-frames */
225
226 Nfor = iLBCdec_inst->nsub - start - 1;
227
228 if (Nfor > 0) {
229
230 /* setup memory */
231
232 memset(mem, 0, (CB_MEML - STATE_LEN) * sizeof(float));
233 memcpy(mem + CB_MEML - STATE_LEN, decresidual + (start - 1) * SUBL,
234 STATE_LEN * sizeof(float));
235
236 /* loop over sub-frames to encode */
237
238 for (subframe = 0; subframe < Nfor; subframe++) {
239
240 /* construct decoded vector */
241
242 iCBConstruct(&decresidual[(start + 1 + subframe) * SUBL],
243 cb_index + subcount * CB_NSTAGES,
244 gain_index + subcount * CB_NSTAGES,
245 mem + CB_MEML - memLfTbl[subcount],
246 memLfTbl[subcount], SUBL, CB_NSTAGES);
247
248 /* update memory */
249
250 memcpy(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(float));
251 memcpy(mem + CB_MEML - SUBL,
252 &decresidual[(start + 1 + subframe) * SUBL],
253 SUBL * sizeof(float));
254
255 subcount++;
256
257 }
258
259 }
260
261 /* backward prediction of sub-frames */
262
263 Nback = start - 1;
264
265 if (Nback > 0) {
266
267 /* setup memory */
268
269 meml_gotten = SUBL * (iLBCdec_inst->nsub + 1 - start);
270
271 if (meml_gotten > CB_MEML) {
272 meml_gotten = CB_MEML;
273 }
274 for (k = 0; k < meml_gotten; k++) {
275 mem[CB_MEML - 1 - k] = decresidual[(start - 1) * SUBL + k];
276 }
277 memset(mem, 0, (CB_MEML - k) * sizeof(float));
278
279 /* loop over subframes to decode */
280
281 for (subframe = 0; subframe < Nback; subframe++) {
282
283 /* construct decoded vector */
284
285 iCBConstruct(&reverseDecresidual[subframe * SUBL],
286 cb_index + subcount * CB_NSTAGES,
287 gain_index + subcount * CB_NSTAGES,
288 mem + CB_MEML - memLfTbl[subcount], memLfTbl[subcount],
289 SUBL, CB_NSTAGES);
290
291 /* update memory */
292
293 memcpy(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(float));
294 memcpy(mem + CB_MEML - SUBL,
295 &reverseDecresidual[subframe * SUBL], SUBL * sizeof(float));
296
297 subcount++;
298 }
299
300
301
302
303
304 /* get decoded residual from reversed vector */
305
306 for (i = 0; i < SUBL * Nback; i++)
307 decresidual[SUBL * Nback - i - 1] = reverseDecresidual[i];
308 }
309 }
310
311 /*----------------------------------------------------------------*
312 * main decoder function
313 *---------------------------------------------------------------*/
314
315 void iLBC_decode(float *decblock, /* (o) decoded signal block */
316 unsigned char *bytes, /* (i) encoded signal bits */
317 iLBC_Dec_Inst_t * iLBCdec_inst, /* (i/o) the decoder state
318 structure */
319 int mode /* (i) 0: bad packet, PLC,
320 1: normal */
321 )
322 {
323 float data[BLOCKL_MAX];
324 float lsfdeq[LPC_FILTERORDER * LPC_N_MAX];
325 float PLCresidual[BLOCKL_MAX], PLClpc[LPC_FILTERORDER + 1];
326 float zeros[BLOCKL_MAX], one[LPC_FILTERORDER + 1];
327 int k, i, start, idxForMax, pos, lastpart, ulp;
328 int lag, ilag;
329 float cc, maxcc;
330 int idxVec[STATE_LEN];
331 int check;
332 int gain_index[NASUB_MAX * CB_NSTAGES], extra_gain_index[CB_NSTAGES];
333 int cb_index[CB_NSTAGES * NASUB_MAX], extra_cb_index[CB_NSTAGES];
334 int lsf_i[LSF_NSPLIT * LPC_N_MAX];
335 int state_first;
336 int last_bit;
337 unsigned char *pbytes;
338 float weightdenum[(LPC_FILTERORDER + 1) * NSUB_MAX];
339 int order_plus_one;
340 float syntdenum[NSUB_MAX * (LPC_FILTERORDER + 1)];
341 float decresidual[BLOCKL_MAX];
342
343 if (mode > 0) { /* the data are good */
344
345 /* decode data */
346
347 pbytes = bytes;
348 pos = 0;
349
350
351
352
353
354
355 /* Set everything to zero before decoding */
356
357 for (k = 0; k < LSF_NSPLIT * LPC_N_MAX; k++) {
358 lsf_i[k] = 0;
359 }
360 start = 0;
361 state_first = 0;
362 idxForMax = 0;
363 for (k = 0; k < iLBCdec_inst->state_short_len; k++) {
364 idxVec[k] = 0;
365 }
366 for (k = 0; k < CB_NSTAGES; k++) {
367 extra_cb_index[k] = 0;
368 }
369 for (k = 0; k < CB_NSTAGES; k++) {
370 extra_gain_index[k] = 0;
371 }
372 for (i = 0; i < iLBCdec_inst->nasub; i++) {
373 for (k = 0; k < CB_NSTAGES; k++) {
374 cb_index[i * CB_NSTAGES + k] = 0;
375 }
376 }
377 for (i = 0; i < iLBCdec_inst->nasub; i++) {
378 for (k = 0; k < CB_NSTAGES; k++) {
379 gain_index[i * CB_NSTAGES + k] = 0;
380 }
381 }
382
383 /* loop over ULP classes */
384
385 for (ulp = 0; ulp < 3; ulp++) {
386
387 /* LSF */
388 for (k = 0; k < LSF_NSPLIT * iLBCdec_inst->lpc_n; k++) {
389 unpack(&pbytes, &lastpart,
390 iLBCdec_inst->ULP_inst->lsf_bits[k][ulp], &pos);
391 packcombine(&lsf_i[k], lastpart,
392 iLBCdec_inst->ULP_inst->lsf_bits[k][ulp]);
393 }
394
395 /* Start block info */
396
397 unpack(&pbytes, &lastpart,
398 iLBCdec_inst->ULP_inst->start_bits[ulp], &pos);
399 packcombine(&start, lastpart,
400 iLBCdec_inst->ULP_inst->start_bits[ulp]);
401
402 unpack(&pbytes, &lastpart,
403 iLBCdec_inst->ULP_inst->startfirst_bits[ulp], &pos);
404 packcombine(&state_first, lastpart,
405 iLBCdec_inst->ULP_inst->startfirst_bits[ulp]);
406
407 unpack(&pbytes, &lastpart,
408 iLBCdec_inst->ULP_inst->scale_bits[ulp], &pos);
409 packcombine(&idxForMax, lastpart,
410 iLBCdec_inst->ULP_inst->scale_bits[ulp]);
411
412 for (k = 0; k < iLBCdec_inst->state_short_len; k++) {
413 unpack(&pbytes, &lastpart,
414 iLBCdec_inst->ULP_inst->state_bits[ulp], &pos);
415 packcombine(idxVec + k, lastpart,
416 iLBCdec_inst->ULP_inst->state_bits[ulp]);
417 }
418
419 /* 23/22 (20ms/30ms) sample block */
420
421 for (k = 0; k < CB_NSTAGES; k++) {
422 unpack(&pbytes, &lastpart,
423 iLBCdec_inst->ULP_inst->extra_cb_index[k][ulp], &pos);
424 packcombine(extra_cb_index + k, lastpart,
425 iLBCdec_inst->ULP_inst->extra_cb_index[k][ulp]);
426 }
427 for (k = 0; k < CB_NSTAGES; k++) {
428 unpack(&pbytes, &lastpart,
429 iLBCdec_inst->ULP_inst->extra_cb_gain[k][ulp], &pos);
430 packcombine(extra_gain_index + k, lastpart,
431 iLBCdec_inst->ULP_inst->extra_cb_gain[k][ulp]);
432 }
433
434 /* The two/four (20ms/30ms) 40 sample sub-blocks */
435
436 for (i = 0; i < iLBCdec_inst->nasub; i++) {
437 for (k = 0; k < CB_NSTAGES; k++) {
438 unpack(&pbytes, &lastpart,
439 iLBCdec_inst->ULP_inst->cb_index[i][k][ulp], &pos);
440 packcombine(cb_index + i * CB_NSTAGES + k, lastpart,
441 iLBCdec_inst->ULP_inst->cb_index[i][k][ulp]);
442 }
443 }
444
445 for (i = 0; i < iLBCdec_inst->nasub; i++) {
446 for (k = 0; k < CB_NSTAGES; k++) {
447 unpack(&pbytes, &lastpart,
448 iLBCdec_inst->ULP_inst->cb_gain[i][k][ulp], &pos);
449 packcombine(gain_index + i * CB_NSTAGES + k, lastpart,
450 iLBCdec_inst->ULP_inst->cb_gain[i][k][ulp]);
451 }
452 }
453 }
454 /* Extract last bit. If it is 1 this indicates an
455 empty/lost frame */
456 unpack(&pbytes, &last_bit, 1, &pos);
457
458 /* Check for bit errors or empty/lost frames */
459 if (start < 1)
460 mode = 0;
461 if (iLBCdec_inst->mode == 20 && start > 3)
462 mode = 0;
463 if (iLBCdec_inst->mode == 30 && start > 5)
464 mode = 0;
465 if (last_bit == 1)
466 mode = 0;
467
468 if (mode == 1) { /* No bit errors was detected,
469 continue decoding */
470
471 /* adjust index */
472 index_conv_dec(cb_index);
473
474 /* decode the lsf */
475
476 SimplelsfDEQ(lsfdeq, lsf_i, iLBCdec_inst->lpc_n);
477 check = LSF_check(lsfdeq, LPC_FILTERORDER, iLBCdec_inst->lpc_n);
478 DecoderInterpolateLSF(syntdenum, weightdenum,
479 lsfdeq, LPC_FILTERORDER, iLBCdec_inst);
480
481 Decode(iLBCdec_inst, decresidual, start, idxForMax,
482 idxVec, syntdenum, cb_index, gain_index,
483 extra_cb_index, extra_gain_index, state_first);
484
485 /* preparing the plc for a future loss! */
486
487 doThePLC(PLCresidual, PLClpc, 0, decresidual,
488 syntdenum +
489 (LPC_FILTERORDER + 1) * (iLBCdec_inst->nsub - 1),
490 (*iLBCdec_inst).last_lag, iLBCdec_inst);
491
492
493
494
495
496
497
498 memcpy(decresidual, PLCresidual,
499 iLBCdec_inst->blockl * sizeof(float));
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
547
548
549
550
551 for (i = 1; i < iLBCdec_inst->nsub; i++) {
552 syntFilter(data + i * SUBL,
553 syntdenum + (i - 1) * (LPC_FILTERORDER + 1),
554 SUBL, iLBCdec_inst->syntMem);
555 }
556 } else if (iLBCdec_inst->mode == 30) {
557 /* Enhancer has 80 samples delay */
558 for (i = 0; i < 2; i++) {
559 syntFilter(data + i * SUBL,
560 iLBCdec_inst->old_syntdenum +
561 (i + iLBCdec_inst->nsub - 2) * (LPC_FILTERORDER + 1),
562 SUBL, iLBCdec_inst->syntMem);
563 }
564 for (i = 2; i < iLBCdec_inst->nsub; i++) {
565 syntFilter(data + i * SUBL,
566 syntdenum + (i - 2) * (LPC_FILTERORDER + 1), SUBL,
567 iLBCdec_inst->syntMem);
568 }
569 }
570
571 } else {
572
573 /* Find last lag */
574 lag = 20;
575 maxcc = xCorrCoef(&decresidual[BLOCKL_MAX - ENH_BLOCKL],
576 &decresidual[BLOCKL_MAX - ENH_BLOCKL - lag], ENH_BLOCKL);
577
578 for (ilag = 21; ilag < 120; ilag++) {
579 cc = xCorrCoef(&decresidual[BLOCKL_MAX - ENH_BLOCKL],
580 &decresidual[BLOCKL_MAX - ENH_BLOCKL - ilag], ENH_BLOCKL);
581
582 if (cc > maxcc) {
583 maxcc = cc;
584 lag = ilag;
585 }
586 }
587 iLBCdec_inst->last_lag = lag;
588
589 /* copy data and run synthesis filter */
590
591 memcpy(data, decresidual, iLBCdec_inst->blockl * sizeof(float));
592 for (i = 0; i < iLBCdec_inst->nsub; i++) {
593 syntFilter(data + i * SUBL,
594 syntdenum + i * (LPC_FILTERORDER + 1), SUBL,
595 iLBCdec_inst->syntMem);
596 }
597
598
599
600
601
602 }
603
604 /* high pass filtering on output if desired, otherwise
605 copy to out */
606
607 hpOutput(data, iLBCdec_inst->blockl, decblock, iLBCdec_inst->hpomem);
608
609 /* memcpy(decblock,data,iLBCdec_inst->blockl*sizeof(float)); */
610
611 memcpy(iLBCdec_inst->old_syntdenum, syntdenum,
612 iLBCdec_inst->nsub * (LPC_FILTERORDER + 1) * sizeof(float));
613
614 iLBCdec_inst->prev_enh_pl = 0;
615
616 if (mode == 0) { /* PLC was used */
617 iLBCdec_inst->prev_enh_pl = 1;
618 }
619 }

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.