- add third_party src.
[platform/framework/web/crosswalk.git] / src / third_party / webrtc / modules / audio_coding / codecs / ilbc / decode_residual.c
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 "defines.h"
20 #include "state_construct.h"
21 #include "cb_construct.h"
22 #include "index_conv_dec.h"
23 #include "do_plc.h"
24 #include "constants.h"
25 #include "enhancer_interface.h"
26 #include "xcorr_coef.h"
27 #include "lsf_check.h"
28
29 /*----------------------------------------------------------------*
30  *  frame residual decoder function (subrutine to iLBC_decode)
31  *---------------------------------------------------------------*/
32
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
37                                 for the decoding  */
38     int16_t *decresidual,  /* (o) decoded residual frame */
39     int16_t *syntdenum   /* (i) the decoded synthesis filter
40                                   coefficients */
41                                   ) {
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 */
47
48   diff = STATE_LEN - iLBCdec_inst->state_short_len;
49
50   if (iLBC_encbits->state_first == 1) {
51     start_pos = (iLBC_encbits->startIdx-1)*SUBL;
52   } else {
53     start_pos = (iLBC_encbits->startIdx-1)*SUBL + diff;
54   }
55
56   /* decode scalar part of start state */
57
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
61                                );
62
63   if (iLBC_encbits->state_first) { /* put adaptive part in the end */
64
65     /* setup memory */
66
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);
70
71     /* construct decoded vector */
72
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
78                               );
79
80   }
81   else {/* put adaptive part in the beginning */
82
83     /* setup memory */
84
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));
89
90     /* construct decoded vector */
91
92     WebRtcIlbcfix_CbConstruct(
93         reverseDecresidual,
94         iLBC_encbits->cb_index, iLBC_encbits->gain_index,
95         mem+CB_MEML-ST_MEM_L_TBL,
96         ST_MEM_L_TBL, diff
97                               );
98
99     /* get decoded residual from reversed vector */
100
101     WebRtcSpl_MemCpyReversedOrder(&decresidual[start_pos-1],
102                                   reverseDecresidual, diff);
103   }
104
105   /* counter for predicted subframes */
106
107   subcount=1;
108
109   /* forward prediction of subframes */
110
111   Nfor = iLBCdec_inst->nsub-iLBC_encbits->startIdx-1;
112
113   if( Nfor > 0 ) {
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     for (subframe=0; subframe<Nfor; subframe++) {
123
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
130                                 );
131
132       /* update memory */
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);
136
137       subcount++;
138     }
139
140   }
141
142   /* backward prediction of subframes */
143
144   Nback = iLBC_encbits->startIdx-1;
145
146   if( Nback > 0 ){
147
148     /* setup memory */
149
150     meml_gotten = SUBL*(iLBCdec_inst->nsub+1-iLBC_encbits->startIdx);
151     if( meml_gotten > CB_MEML ) {
152       meml_gotten=CB_MEML;
153     }
154
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));
158
159     /* loop over subframes to decode */
160
161     for (subframe=0; subframe<Nback; subframe++) {
162
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
169                                 );
170
171       /* update memory */
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);
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 }