blob: dd5c353cb53e420d7b421c0edea7b7da3dbcf384 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
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
kwiberg619a2112016-08-24 02:46:44 -070019#include "decode_residual.h"
20
bjornv@webrtc.org52275342014-08-20 10:09:34 +000021#include <string.h>
22
niklase@google.com470e71d2011-07-07 08:21:25 +000023#include "defines.h"
24#include "state_construct.h"
25#include "cb_construct.h"
26#include "index_conv_dec.h"
27#include "do_plc.h"
28#include "constants.h"
29#include "enhancer_interface.h"
30#include "xcorr_coef.h"
31#include "lsf_check.h"
32
niklase@google.com470e71d2011-07-07 08:21:25 +000033/*----------------------------------------------------------------*
34 * frame residual decoder function (subrutine to iLBC_decode)
35 *---------------------------------------------------------------*/
36
kwiberg619a2112016-08-24 02:46:44 -070037bool WebRtcIlbcfix_DecodeResidual(
pbos@webrtc.orgeb544462014-12-17 15:23:29 +000038 IlbcDecoder *iLBCdec_inst,
niklase@google.com470e71d2011-07-07 08:21:25 +000039 /* (i/o) the decoder state structure */
40 iLBC_bits *iLBC_encbits, /* (i/o) Encoded bits, which are used
41 for the decoding */
pbos@webrtc.org0946a562013-04-09 00:28:06 +000042 int16_t *decresidual, /* (o) decoded residual frame */
43 int16_t *syntdenum /* (i) the decoded synthesis filter
niklase@google.com470e71d2011-07-07 08:21:25 +000044 coefficients */
45 ) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070046 size_t meml_gotten, diff, start_pos;
47 size_t subcount, subframe;
pbos@webrtc.org0946a562013-04-09 00:28:06 +000048 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 */
niklase@google.com470e71d2011-07-07 08:21:25 +000051
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
Peter Kastingb7e50542015-06-11 12:55:50 -070071 WebRtcSpl_MemSetW16(mem, 0, CB_MEML - iLBCdec_inst->state_short_len);
niklase@google.com470e71d2011-07-07 08:21:25 +000072 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
kwiberg619a2112016-08-24 02:46:44 -070077 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.
niklase@google.com470e71d2011-07-07 08:21:25 +000082
83 }
84 else {/* put adaptive part in the beginning */
85
niklase@google.com470e71d2011-07-07 08:21:25 +000086 /* setup memory */
87
88 meml_gotten = iLBCdec_inst->state_short_len;
89 WebRtcSpl_MemCpyReversedOrder(mem+CB_MEML-1,
90 decresidual+start_pos, meml_gotten);
Peter Kastingb7e50542015-06-11 12:55:50 -070091 WebRtcSpl_MemSetW16(mem, 0, CB_MEML - meml_gotten);
niklase@google.com470e71d2011-07-07 08:21:25 +000092
93 /* construct decoded vector */
94
kwiberg619a2112016-08-24 02:46:44 -070095 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.
niklase@google.com470e71d2011-07-07 08:21:25 +0000100
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
Peter Kastingf045e4d2015-06-10 21:15:38 -0700113 if (iLBCdec_inst->nsub > iLBC_encbits->startIdx + 1) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
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
Peter Kastingdce40cf2015-08-24 14:52:23 -0700122 size_t Nfor = iLBCdec_inst->nsub - iLBC_encbits->startIdx - 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000123 for (subframe=0; subframe<Nfor; subframe++) {
124
125 /* construct decoded vector */
kwiberg619a2112016-08-24 02:46:44 -0700126 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;
niklase@google.com470e71d2011-07-07 08:21:25 +0000132
133 /* update memory */
bjornv@webrtc.org52275342014-08-20 10:09:34 +0000134 memmove(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(*mem));
niklase@google.com470e71d2011-07-07 08:21:25 +0000135 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
Peter Kastingf045e4d2015-06-10 21:15:38 -0700145 if (iLBC_encbits->startIdx > 1) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000146
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);
Peter Kastingb7e50542015-06-11 12:55:50 -0700156 WebRtcSpl_MemSetW16(mem, 0, CB_MEML - meml_gotten);
niklase@google.com470e71d2011-07-07 08:21:25 +0000157
158 /* loop over subframes to decode */
159
Peter Kastingdce40cf2015-08-24 14:52:23 -0700160 size_t Nback = iLBC_encbits->startIdx - 1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000161 for (subframe=0; subframe<Nback; subframe++) {
162
163 /* construct decoded vector */
kwiberg619a2112016-08-24 02:46:44 -0700164 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.
niklase@google.com470e71d2011-07-07 08:21:25 +0000170
171 /* update memory */
bjornv@webrtc.org52275342014-08-20 10:09:34 +0000172 memmove(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(*mem));
niklase@google.com470e71d2011-07-07 08:21:25 +0000173 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 }
kwiberg619a2112016-08-24 02:46:44 -0700183
184 return true; // Success.
niklase@google.com470e71d2011-07-07 08:21:25 +0000185}