• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3  *
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.
9  */
10 
11 /******************************************************************
12 
13  iLBC Speech Coder ANSI-C Source Code
14 
15  WebRtcIlbcfix_DecodeResidual.c
16 
17 ******************************************************************/
18 
19 #include "modules/audio_coding/codecs/ilbc/decode_residual.h"
20 
21 #include <string.h>
22 
23 #include "modules/audio_coding/codecs/ilbc/cb_construct.h"
24 #include "modules/audio_coding/codecs/ilbc/constants.h"
25 #include "modules/audio_coding/codecs/ilbc/defines.h"
26 #include "modules/audio_coding/codecs/ilbc/do_plc.h"
27 #include "modules/audio_coding/codecs/ilbc/enhancer_interface.h"
28 #include "modules/audio_coding/codecs/ilbc/index_conv_dec.h"
29 #include "modules/audio_coding/codecs/ilbc/lsf_check.h"
30 #include "modules/audio_coding/codecs/ilbc/state_construct.h"
31 #include "modules/audio_coding/codecs/ilbc/xcorr_coef.h"
32 
33 /*----------------------------------------------------------------*
34  *  frame residual decoder function (subrutine to iLBC_decode)
35  *---------------------------------------------------------------*/
36 
WebRtcIlbcfix_DecodeResidual(IlbcDecoder * iLBCdec_inst,iLBC_bits * iLBC_encbits,int16_t * decresidual,int16_t * syntdenum)37 bool WebRtcIlbcfix_DecodeResidual(
38     IlbcDecoder *iLBCdec_inst,
39     /* (i/o) the decoder state structure */
40     iLBC_bits *iLBC_encbits, /* (i/o) Encoded bits, which are used
41                                 for the decoding  */
42     int16_t *decresidual,  /* (o) decoded residual frame */
43     int16_t *syntdenum   /* (i) the decoded synthesis filter
44                                   coefficients */
45                                   ) {
46   size_t meml_gotten, diff, start_pos;
47   size_t subcount, subframe;
48   int16_t *reverseDecresidual = iLBCdec_inst->enh_buf; /* Reversed decoded data, used for decoding backwards in time (reuse memory in state) */
49   int16_t *memVec = iLBCdec_inst->prevResidual;  /* Memory for codebook and filter state (reuse memory in state) */
50   int16_t *mem = &memVec[CB_HALFFILTERLEN];   /* Memory for codebook */
51 
52   diff = STATE_LEN - iLBCdec_inst->state_short_len;
53 
54   if (iLBC_encbits->state_first == 1) {
55     start_pos = (iLBC_encbits->startIdx-1)*SUBL;
56   } else {
57     start_pos = (iLBC_encbits->startIdx-1)*SUBL + diff;
58   }
59 
60   /* decode scalar part of start state */
61 
62   WebRtcIlbcfix_StateConstruct(iLBC_encbits->idxForMax,
63                                iLBC_encbits->idxVec, &syntdenum[(iLBC_encbits->startIdx-1)*(LPC_FILTERORDER+1)],
64                                &decresidual[start_pos], iLBCdec_inst->state_short_len
65                                );
66 
67   if (iLBC_encbits->state_first) { /* put adaptive part in the end */
68 
69     /* setup memory */
70 
71     WebRtcSpl_MemSetW16(mem, 0, CB_MEML - iLBCdec_inst->state_short_len);
72     WEBRTC_SPL_MEMCPY_W16(mem+CB_MEML-iLBCdec_inst->state_short_len, decresidual+start_pos,
73                           iLBCdec_inst->state_short_len);
74 
75     /* construct decoded vector */
76 
77     if (!WebRtcIlbcfix_CbConstruct(
78             &decresidual[start_pos + iLBCdec_inst->state_short_len],
79             iLBC_encbits->cb_index, iLBC_encbits->gain_index,
80             mem + CB_MEML - ST_MEM_L_TBL, ST_MEM_L_TBL, diff))
81       return false;  // Error.
82 
83   }
84   else {/* put adaptive part in the beginning */
85 
86     /* setup memory */
87 
88     meml_gotten = iLBCdec_inst->state_short_len;
89     WebRtcSpl_MemCpyReversedOrder(mem+CB_MEML-1,
90                                   decresidual+start_pos, meml_gotten);
91     WebRtcSpl_MemSetW16(mem, 0, CB_MEML - meml_gotten);
92 
93     /* construct decoded vector */
94 
95     if (!WebRtcIlbcfix_CbConstruct(reverseDecresidual, iLBC_encbits->cb_index,
96                                    iLBC_encbits->gain_index,
97                                    mem + CB_MEML - ST_MEM_L_TBL, ST_MEM_L_TBL,
98                                    diff))
99       return false;  // Error.
100 
101     /* get decoded residual from reversed vector */
102 
103     WebRtcSpl_MemCpyReversedOrder(&decresidual[start_pos-1],
104                                   reverseDecresidual, diff);
105   }
106 
107   /* counter for predicted subframes */
108 
109   subcount=1;
110 
111   /* forward prediction of subframes */
112 
113   if (iLBCdec_inst->nsub > iLBC_encbits->startIdx + 1) {
114 
115     /* setup memory */
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);
119 
120     /* loop over subframes to encode */
121 
122     size_t Nfor = iLBCdec_inst->nsub - iLBC_encbits->startIdx - 1;
123     for (subframe=0; subframe<Nfor; subframe++) {
124 
125       /* construct decoded vector */
126       if (!WebRtcIlbcfix_CbConstruct(
127               &decresidual[(iLBC_encbits->startIdx + 1 + subframe) * SUBL],
128               iLBC_encbits->cb_index + subcount * CB_NSTAGES,
129               iLBC_encbits->gain_index + subcount * CB_NSTAGES, mem, MEM_LF_TBL,
130               SUBL))
131         return false;  // Error;
132 
133       /* update memory */
134       memmove(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(*mem));
135       WEBRTC_SPL_MEMCPY_W16(mem+CB_MEML-SUBL,
136                             &decresidual[(iLBC_encbits->startIdx+1+subframe)*SUBL], SUBL);
137 
138       subcount++;
139     }
140 
141   }
142 
143   /* backward prediction of subframes */
144 
145   if (iLBC_encbits->startIdx > 1) {
146 
147     /* setup memory */
148 
149     meml_gotten = SUBL*(iLBCdec_inst->nsub+1-iLBC_encbits->startIdx);
150     if( meml_gotten > CB_MEML ) {
151       meml_gotten=CB_MEML;
152     }
153 
154     WebRtcSpl_MemCpyReversedOrder(mem+CB_MEML-1,
155                                   decresidual+(iLBC_encbits->startIdx-1)*SUBL, meml_gotten);
156     WebRtcSpl_MemSetW16(mem, 0, CB_MEML - meml_gotten);
157 
158     /* loop over subframes to decode */
159 
160     size_t Nback = iLBC_encbits->startIdx - 1;
161     for (subframe=0; subframe<Nback; subframe++) {
162 
163       /* construct decoded vector */
164       if (!WebRtcIlbcfix_CbConstruct(
165               &reverseDecresidual[subframe * SUBL],
166               iLBC_encbits->cb_index + subcount * CB_NSTAGES,
167               iLBC_encbits->gain_index + subcount * CB_NSTAGES, mem, MEM_LF_TBL,
168               SUBL))
169         return false;  // Error.
170 
171       /* update memory */
172       memmove(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(*mem));
173       WEBRTC_SPL_MEMCPY_W16(mem+CB_MEML-SUBL,
174                             &reverseDecresidual[subframe*SUBL], SUBL);
175 
176       subcount++;
177     }
178 
179     /* get decoded residual from reversed vector */
180     WebRtcSpl_MemCpyReversedOrder(decresidual+SUBL*Nback-1,
181                                   reverseDecresidual, SUBL*Nback);
182   }
183 
184   return true;  // Success.
185 }
186