niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
kwiberg | 619a211 | 2016-08-24 02:46:44 -0700 | [diff] [blame] | 19 | #include "decode_residual.h" |
| 20 | |
bjornv@webrtc.org | 5227534 | 2014-08-20 10:09:34 +0000 | [diff] [blame] | 21 | #include <string.h> |
| 22 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 23 | #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.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 33 | /*----------------------------------------------------------------* |
| 34 | * frame residual decoder function (subrutine to iLBC_decode) |
| 35 | *---------------------------------------------------------------*/ |
| 36 | |
kwiberg | 619a211 | 2016-08-24 02:46:44 -0700 | [diff] [blame] | 37 | bool WebRtcIlbcfix_DecodeResidual( |
pbos@webrtc.org | eb54446 | 2014-12-17 15:23:29 +0000 | [diff] [blame] | 38 | IlbcDecoder *iLBCdec_inst, |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | /* (i/o) the decoder state structure */ |
| 40 | iLBC_bits *iLBC_encbits, /* (i/o) Encoded bits, which are used |
| 41 | for the decoding */ |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 42 | int16_t *decresidual, /* (o) decoded residual frame */ |
| 43 | int16_t *syntdenum /* (i) the decoded synthesis filter |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 44 | coefficients */ |
| 45 | ) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 46 | size_t meml_gotten, diff, start_pos; |
| 47 | size_t subcount, subframe; |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 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 */ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 71 | WebRtcSpl_MemSetW16(mem, 0, CB_MEML - iLBCdec_inst->state_short_len); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
kwiberg | 619a211 | 2016-08-24 02:46:44 -0700 | [diff] [blame] | 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. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | |
| 83 | } |
| 84 | else {/* put adaptive part in the beginning */ |
| 85 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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); |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 91 | WebRtcSpl_MemSetW16(mem, 0, CB_MEML - meml_gotten); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 92 | |
| 93 | /* construct decoded vector */ |
| 94 | |
kwiberg | 619a211 | 2016-08-24 02:46:44 -0700 | [diff] [blame] | 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. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Peter Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 113 | if (iLBCdec_inst->nsub > iLBC_encbits->startIdx + 1) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 122 | size_t Nfor = iLBCdec_inst->nsub - iLBC_encbits->startIdx - 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | for (subframe=0; subframe<Nfor; subframe++) { |
| 124 | |
| 125 | /* construct decoded vector */ |
kwiberg | 619a211 | 2016-08-24 02:46:44 -0700 | [diff] [blame] | 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; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 132 | |
| 133 | /* update memory */ |
bjornv@webrtc.org | 5227534 | 2014-08-20 10:09:34 +0000 | [diff] [blame] | 134 | memmove(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(*mem)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Peter Kasting | f045e4d | 2015-06-10 21:15:38 -0700 | [diff] [blame] | 145 | if (iLBC_encbits->startIdx > 1) { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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); |
Peter Kasting | b7e5054 | 2015-06-11 12:55:50 -0700 | [diff] [blame] | 156 | WebRtcSpl_MemSetW16(mem, 0, CB_MEML - meml_gotten); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 157 | |
| 158 | /* loop over subframes to decode */ |
| 159 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 160 | size_t Nback = iLBC_encbits->startIdx - 1; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 161 | for (subframe=0; subframe<Nback; subframe++) { |
| 162 | |
| 163 | /* construct decoded vector */ |
kwiberg | 619a211 | 2016-08-24 02:46:44 -0700 | [diff] [blame] | 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. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 170 | |
| 171 | /* update memory */ |
bjornv@webrtc.org | 5227534 | 2014-08-20 10:09:34 +0000 | [diff] [blame] | 172 | memmove(mem, mem + SUBL, (CB_MEML - SUBL) * sizeof(*mem)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | } |
kwiberg | 619a211 | 2016-08-24 02:46:44 -0700 | [diff] [blame] | 183 | |
| 184 | return true; // Success. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 185 | } |