2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
11 /******************************************************************
13 iLBC Speech Coder ANSI-C Source Code
15 WebRtcIlbcfix_DecodeResidual.c
17 ******************************************************************/
20 #include "state_construct.h"
21 #include "cb_construct.h"
22 #include "index_conv_dec.h"
24 #include "constants.h"
25 #include "enhancer_interface.h"
26 #include "xcorr_coef.h"
27 #include "lsf_check.h"
29 /*----------------------------------------------------------------*
30 * frame residual decoder function (subrutine to iLBC_decode)
31 *---------------------------------------------------------------*/
33 void WebRtcIlbcfix_DecodeResidual(
34 iLBC_Dec_Inst_t *iLBCdec_inst,
35 /* (i/o) the decoder state structure */
36 iLBC_bits *iLBC_encbits, /* (i/o) Encoded bits, which are used
38 int16_t *decresidual, /* (o) decoded residual frame */
39 int16_t *syntdenum /* (i) the decoded synthesis filter
42 int16_t meml_gotten, Nfor, Nback, diff, start_pos;
43 int16_t subcount, subframe;
44 int16_t *reverseDecresidual = iLBCdec_inst->enh_buf; /* Reversed decoded data, used for decoding backwards in time (reuse memory in state) */
45 int16_t *memVec = iLBCdec_inst->prevResidual; /* Memory for codebook and filter state (reuse memory in state) */
46 int16_t *mem = &memVec[CB_HALFFILTERLEN]; /* Memory for codebook */
48 diff = STATE_LEN - iLBCdec_inst->state_short_len;
50 if (iLBC_encbits->state_first == 1) {
51 start_pos = (iLBC_encbits->startIdx-1)*SUBL;
53 start_pos = (iLBC_encbits->startIdx-1)*SUBL + diff;
56 /* decode scalar part of start state */
58 WebRtcIlbcfix_StateConstruct(iLBC_encbits->idxForMax,
59 iLBC_encbits->idxVec, &syntdenum[(iLBC_encbits->startIdx-1)*(LPC_FILTERORDER+1)],
60 &decresidual[start_pos], iLBCdec_inst->state_short_len
63 if (iLBC_encbits->state_first) { /* put adaptive part in the end */
67 WebRtcSpl_MemSetW16(mem, 0, (int16_t)(CB_MEML-iLBCdec_inst->state_short_len));
68 WEBRTC_SPL_MEMCPY_W16(mem+CB_MEML-iLBCdec_inst->state_short_len, decresidual+start_pos,
69 iLBCdec_inst->state_short_len);
71 /* construct decoded vector */
73 WebRtcIlbcfix_CbConstruct(
74 &decresidual[start_pos+iLBCdec_inst->state_short_len],
75 iLBC_encbits->cb_index, iLBC_encbits->gain_index,
76 mem+CB_MEML-ST_MEM_L_TBL,
77 ST_MEM_L_TBL, (int16_t)diff
81 else {/* put adaptive part in the beginning */
85 meml_gotten = iLBCdec_inst->state_short_len;
86 WebRtcSpl_MemCpyReversedOrder(mem+CB_MEML-1,
87 decresidual+start_pos, meml_gotten);
88 WebRtcSpl_MemSetW16(mem, 0, (int16_t)(CB_MEML-meml_gotten));
90 /* construct decoded vector */
92 WebRtcIlbcfix_CbConstruct(
94 iLBC_encbits->cb_index, iLBC_encbits->gain_index,
95 mem+CB_MEML-ST_MEM_L_TBL,
99 /* get decoded residual from reversed vector */
101 WebRtcSpl_MemCpyReversedOrder(&decresidual[start_pos-1],
102 reverseDecresidual, diff);
105 /* counter for predicted subframes */
109 /* forward prediction of subframes */
111 Nfor = iLBCdec_inst->nsub-iLBC_encbits->startIdx-1;
116 WebRtcSpl_MemSetW16(mem, 0, CB_MEML-STATE_LEN);
117 WEBRTC_SPL_MEMCPY_W16(mem+CB_MEML-STATE_LEN,
118 decresidual+(iLBC_encbits->startIdx-1)*SUBL, STATE_LEN);
120 /* loop over subframes to encode */
122 for (subframe=0; subframe<Nfor; subframe++) {
124 /* construct decoded vector */
125 WebRtcIlbcfix_CbConstruct(
126 &decresidual[(iLBC_encbits->startIdx+1+subframe)*SUBL],
127 iLBC_encbits->cb_index+subcount*CB_NSTAGES,
128 iLBC_encbits->gain_index+subcount*CB_NSTAGES,
129 mem, MEM_LF_TBL, SUBL
133 WEBRTC_SPL_MEMMOVE_W16(mem, mem+SUBL, CB_MEML-SUBL);
134 WEBRTC_SPL_MEMCPY_W16(mem+CB_MEML-SUBL,
135 &decresidual[(iLBC_encbits->startIdx+1+subframe)*SUBL], SUBL);
142 /* backward prediction of subframes */
144 Nback = iLBC_encbits->startIdx-1;
150 meml_gotten = SUBL*(iLBCdec_inst->nsub+1-iLBC_encbits->startIdx);
151 if( meml_gotten > CB_MEML ) {
155 WebRtcSpl_MemCpyReversedOrder(mem+CB_MEML-1,
156 decresidual+(iLBC_encbits->startIdx-1)*SUBL, meml_gotten);
157 WebRtcSpl_MemSetW16(mem, 0, (int16_t)(CB_MEML-meml_gotten));
159 /* loop over subframes to decode */
161 for (subframe=0; subframe<Nback; subframe++) {
163 /* construct decoded vector */
164 WebRtcIlbcfix_CbConstruct(
165 &reverseDecresidual[subframe*SUBL],
166 iLBC_encbits->cb_index+subcount*CB_NSTAGES,
167 iLBC_encbits->gain_index+subcount*CB_NSTAGES,
168 mem, MEM_LF_TBL, SUBL
172 WEBRTC_SPL_MEMMOVE_W16(mem, mem+SUBL, CB_MEML-SUBL);
173 WEBRTC_SPL_MEMCPY_W16(mem+CB_MEML-SUBL,
174 &reverseDecresidual[subframe*SUBL], SUBL);
179 /* get decoded residual from reversed vector */
180 WebRtcSpl_MemCpyReversedOrder(decresidual+SUBL*Nback-1,
181 reverseDecresidual, SUBL*Nback);