blob: fd4e857afa8accc6d7fad203bb9601889ad01d95 [file] [log] [blame]
Gregory Maxwellae231142011-07-30 08:18:48 -04001/***********************************************************************
2Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3Redistribution and use in source and binary forms, with or without
Jean-Marc Valinae00e602012-04-20 16:31:04 -04004modification, are permitted provided that the following conditions
5are met:
Gregory Maxwellae231142011-07-30 08:18:48 -04006- Redistributions of source code must retain the above copyright notice,
7this list of conditions and the following disclaimer.
8- Redistributions in binary form must reproduce the above copyright
9notice, this list of conditions and the following disclaimer in the
10documentation and/or other materials provided with the distribution.
Ralph Gilesf2446c22013-09-16 14:40:04 -070011- Neither the name of Internet Society, IETF or IETF Trust, nor the
Jean-Marc Valinae00e602012-04-20 16:31:04 -040012names of specific contributors, may be used to endorse or promote
13products derived from this software without specific prior written
14permission.
Timothy B. Terriberry80ad3832013-05-19 18:00:39 -070015THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Jean-Marc Valinae00e602012-04-20 16:31:04 -040016AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25POSSIBILITY OF SUCH DAMAGE.
Gregory Maxwellae231142011-07-30 08:18:48 -040026***********************************************************************/
27
Jean-Marc Valin5a484122011-08-15 10:49:53 -040028#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
Jean-Marc Valin1c2f5632011-09-16 01:16:53 -070032#include "main.h"
Timothy B. Terriberry6f2d9f52012-09-05 07:35:49 -070033#include "stack_alloc.h"
Gregory Maxwellae231142011-07-30 08:18:48 -040034
35/**********************************************************/
36/* Core decoder. Performs inverse NSQ operation LTP + LPC */
37/**********************************************************/
38void silk_decode_core(
Koen Vosacc7a6c2011-10-28 19:44:26 -040039 silk_decoder_state *psDec, /* I/O Decoder state */
40 silk_decoder_control *psDecCtrl, /* I Decoder control */
41 opus_int16 xq[], /* O Decoded speech */
xiangmingzhuc95c9a02014-04-30 15:48:07 +080042 const opus_int16 pulses[ MAX_FRAME_LENGTH ], /* I Pulse signal */
43 int arch /* I Run-time architecture */
Gregory Maxwellae231142011-07-30 08:18:48 -040044)
45{
Koen Vosbf75c8e2011-12-13 14:47:31 -050046 opus_int i, k, lag = 0, start_idx, sLTP_buf_idx, NLSF_interpolation_flag, signalType;
Gregory Maxwellae231142011-07-30 08:18:48 -040047 opus_int16 *A_Q12, *B_Q14, *pxq, A_Q12_tmp[ MAX_LPC_ORDER ];
Timothy B. Terriberry6f2d9f52012-09-05 07:35:49 -070048 VARDECL( opus_int16, sLTP );
49 VARDECL( opus_int32, sLTP_Q15 );
Koen Vosbbfc9c92011-12-13 14:50:12 -050050 opus_int32 LTP_pred_Q13, LPC_pred_Q10, Gain_Q10, inv_gain_Q31, gain_adj_Q16, rand_seed, offset_Q10;
Koen Vosa51ebd62011-12-14 11:39:29 -050051 opus_int32 *pred_lag_ptr, *pexc_Q14, *pres_Q14;
Timothy B. Terriberry6f2d9f52012-09-05 07:35:49 -070052 VARDECL( opus_int32, res_Q14 );
53 VARDECL( opus_int32, sLPC_Q14 );
54 SAVE_STACK;
Gregory Maxwellae231142011-07-30 08:18:48 -040055
Koen Vosa51ebd62011-12-14 11:39:29 -050056 silk_assert( psDec->prev_gain_Q16 != 0 );
Gregory Maxwellae231142011-07-30 08:18:48 -040057
Timothy B. Terriberry6f2d9f52012-09-05 07:35:49 -070058 ALLOC( sLTP, psDec->ltp_mem_length, opus_int16 );
59 ALLOC( sLTP_Q15, psDec->ltp_mem_length + psDec->frame_length, opus_int32 );
60 ALLOC( res_Q14, psDec->subfr_length, opus_int32 );
61 ALLOC( sLPC_Q14, psDec->subfr_length + MAX_LPC_ORDER, opus_int32 );
62
Gregory Maxwellae231142011-07-30 08:18:48 -040063 offset_Q10 = silk_Quantization_Offsets_Q10[ psDec->indices.signalType >> 1 ][ psDec->indices.quantOffsetType ];
64
65 if( psDec->indices.NLSFInterpCoef_Q2 < 1 << 2 ) {
66 NLSF_interpolation_flag = 1;
67 } else {
68 NLSF_interpolation_flag = 0;
69 }
70
71 /* Decode excitation */
72 rand_seed = psDec->indices.Seed;
73 for( i = 0; i < psDec->frame_length; i++ ) {
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -070074 rand_seed = silk_RAND( rand_seed );
Koen Vosa51ebd62011-12-14 11:39:29 -050075 psDec->exc_Q14[ i ] = silk_LSHIFT( (opus_int32)pulses[ i ], 14 );
76 if( psDec->exc_Q14[ i ] > 0 ) {
77 psDec->exc_Q14[ i ] -= QUANT_LEVEL_ADJUST_Q10 << 4;
Gregory Maxwellae231142011-07-30 08:18:48 -040078 } else
Koen Vosa51ebd62011-12-14 11:39:29 -050079 if( psDec->exc_Q14[ i ] < 0 ) {
80 psDec->exc_Q14[ i ] += QUANT_LEVEL_ADJUST_Q10 << 4;
Gregory Maxwellae231142011-07-30 08:18:48 -040081 }
Koen Vosa51ebd62011-12-14 11:39:29 -050082 psDec->exc_Q14[ i ] += offset_Q10 << 4;
Koen Vos54518c82012-01-31 01:51:22 -050083 if( rand_seed < 0 ) {
Jean-Marc Valinee8adbe2012-01-24 14:45:08 +130084 psDec->exc_Q14[ i ] = -psDec->exc_Q14[ i ];
Koen Vos54518c82012-01-31 01:51:22 -050085 }
Gregory Maxwellae231142011-07-30 08:18:48 -040086
Koen Vosa51ebd62011-12-14 11:39:29 -050087 rand_seed = silk_ADD32_ovflw( rand_seed, pulses[ i ] );
Gregory Maxwellae231142011-07-30 08:18:48 -040088 }
89
Koen Vos3195f6c2011-10-10 20:46:32 -040090 /* Copy LPC state */
91 silk_memcpy( sLPC_Q14, psDec->sLPC_Q14_buf, MAX_LPC_ORDER * sizeof( opus_int32 ) );
92
Koen Vosa51ebd62011-12-14 11:39:29 -050093 pexc_Q14 = psDec->exc_Q14;
Koen Vos3195f6c2011-10-10 20:46:32 -040094 pxq = xq;
Gregory Maxwellae231142011-07-30 08:18:48 -040095 sLTP_buf_idx = psDec->ltp_mem_length;
96 /* Loop over subframes */
97 for( k = 0; k < psDec->nb_subfr; k++ ) {
Koen Vosa51ebd62011-12-14 11:39:29 -050098 pres_Q14 = res_Q14;
Gregory Maxwellae231142011-07-30 08:18:48 -040099 A_Q12 = psDecCtrl->PredCoef_Q12[ k >> 1 ];
100
101 /* Preload LPC coeficients to array on stack. Gives small performance gain */
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700102 silk_memcpy( A_Q12_tmp, A_Q12, psDec->LPC_order * sizeof( opus_int16 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400103 B_Q14 = &psDecCtrl->LTPCoef_Q14[ k * LTP_ORDER ];
Gregory Maxwellae231142011-07-30 08:18:48 -0400104 signalType = psDec->indices.signalType;
105
Koen Vos88875662011-10-06 13:38:26 -0400106 Gain_Q10 = silk_RSHIFT( psDecCtrl->Gains_Q16[ k ], 6 );
Koen Vosbbfc9c92011-12-13 14:50:12 -0500107 inv_gain_Q31 = silk_INVERSE32_varQ( psDecCtrl->Gains_Q16[ k ], 47 );
Gregory Maxwellae231142011-07-30 08:18:48 -0400108
Koen Vosbbfc9c92011-12-13 14:50:12 -0500109 /* Calculate gain adjustment factor */
Koen Vosa51ebd62011-12-14 11:39:29 -0500110 if( psDecCtrl->Gains_Q16[ k ] != psDec->prev_gain_Q16 ) {
111 gain_adj_Q16 = silk_DIV32_varQ( psDec->prev_gain_Q16, psDecCtrl->Gains_Q16[ k ], 16 );
Gregory Maxwellae231142011-07-30 08:18:48 -0400112
113 /* Scale short term state */
114 for( i = 0; i < MAX_LPC_ORDER; i++ ) {
Koen Vos3195f6c2011-10-10 20:46:32 -0400115 sLPC_Q14[ i ] = silk_SMULWW( gain_adj_Q16, sLPC_Q14[ i ] );
Gregory Maxwellae231142011-07-30 08:18:48 -0400116 }
Koen Vosbbfc9c92011-12-13 14:50:12 -0500117 } else {
Jean-Marc Valin905197d2012-03-08 14:09:09 -0500118 gain_adj_Q16 = (opus_int32)1 << 16;
Gregory Maxwellae231142011-07-30 08:18:48 -0400119 }
120
121 /* Save inv_gain */
Koen Vosbbfc9c92011-12-13 14:50:12 -0500122 silk_assert( inv_gain_Q31 != 0 );
Koen Vosa51ebd62011-12-14 11:39:29 -0500123 psDec->prev_gain_Q16 = psDecCtrl->Gains_Q16[ k ];
Gregory Maxwellae231142011-07-30 08:18:48 -0400124
125 /* Avoid abrupt transition from voiced PLC to unvoiced normal decoding */
126 if( psDec->lossCnt && psDec->prevSignalType == TYPE_VOICED &&
127 psDec->indices.signalType != TYPE_VOICED && k < MAX_NB_SUBFR/2 ) {
128
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700129 silk_memset( B_Q14, 0, LTP_ORDER * sizeof( opus_int16 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400130 B_Q14[ LTP_ORDER/2 ] = SILK_FIX_CONST( 0.25, 14 );
131
132 signalType = TYPE_VOICED;
133 psDecCtrl->pitchL[ k ] = psDec->lagPrev;
134 }
135
136 if( signalType == TYPE_VOICED ) {
137 /* Voiced */
138 lag = psDecCtrl->pitchL[ k ];
139
140 /* Re-whitening */
Koen Vos3195f6c2011-10-10 20:46:32 -0400141 if( k == 0 || ( k == 2 && NLSF_interpolation_flag ) ) {
Gregory Maxwellae231142011-07-30 08:18:48 -0400142 /* Rewhiten with new A coefs */
143 start_idx = psDec->ltp_mem_length - lag - psDec->LPC_order - LTP_ORDER / 2;
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700144 silk_assert( start_idx > 0 );
Gregory Maxwellae231142011-07-30 08:18:48 -0400145
Koen Vos3195f6c2011-10-10 20:46:32 -0400146 if( k == 2 ) {
147 silk_memcpy( &psDec->outBuf[ psDec->ltp_mem_length ], xq, 2 * psDec->subfr_length * sizeof( opus_int16 ) );
148 }
149
Gregory Maxwellae231142011-07-30 08:18:48 -0400150 silk_LPC_analysis_filter( &sLTP[ start_idx ], &psDec->outBuf[ start_idx + k * psDec->subfr_length ],
xiangmingzhuc95c9a02014-04-30 15:48:07 +0800151 A_Q12, psDec->ltp_mem_length - start_idx, psDec->LPC_order, arch );
Gregory Maxwellae231142011-07-30 08:18:48 -0400152
153 /* After rewhitening the LTP state is unscaled */
Gregory Maxwellae231142011-07-30 08:18:48 -0400154 if( k == 0 ) {
Koen Vos3195f6c2011-10-10 20:46:32 -0400155 /* Do LTP downscaling to reduce inter-packet dependency */
Koen Vosbf75c8e2011-12-13 14:47:31 -0500156 inv_gain_Q31 = silk_LSHIFT( silk_SMULWB( inv_gain_Q31, psDecCtrl->LTP_scale_Q14 ), 2 );
Gregory Maxwellae231142011-07-30 08:18:48 -0400157 }
158 for( i = 0; i < lag + LTP_ORDER/2; i++ ) {
Koen Vosbf75c8e2011-12-13 14:47:31 -0500159 sLTP_Q15[ sLTP_buf_idx - i - 1 ] = silk_SMULWB( inv_gain_Q31, sLTP[ psDec->ltp_mem_length - i - 1 ] );
Gregory Maxwellae231142011-07-30 08:18:48 -0400160 }
161 } else {
162 /* Update LTP state when Gain changes */
Jean-Marc Valin905197d2012-03-08 14:09:09 -0500163 if( gain_adj_Q16 != (opus_int32)1 << 16 ) {
Gregory Maxwellae231142011-07-30 08:18:48 -0400164 for( i = 0; i < lag + LTP_ORDER/2; i++ ) {
Koen Vosbf75c8e2011-12-13 14:47:31 -0500165 sLTP_Q15[ sLTP_buf_idx - i - 1 ] = silk_SMULWW( gain_adj_Q16, sLTP_Q15[ sLTP_buf_idx - i - 1 ] );
Gregory Maxwellae231142011-07-30 08:18:48 -0400166 }
167 }
168 }
169 }
170
171 /* Long-term prediction */
172 if( signalType == TYPE_VOICED ) {
Koen Vosbf75c8e2011-12-13 14:47:31 -0500173 /* Set up pointer */
174 pred_lag_ptr = &sLTP_Q15[ sLTP_buf_idx - lag + LTP_ORDER / 2 ];
Gregory Maxwellae231142011-07-30 08:18:48 -0400175 for( i = 0; i < psDec->subfr_length; i++ ) {
176 /* Unrolled loop */
Koen Vosbbfc9c92011-12-13 14:50:12 -0500177 /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */
178 LTP_pred_Q13 = 2;
179 LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ 0 ], B_Q14[ 0 ] );
Koen Vosbf75c8e2011-12-13 14:47:31 -0500180 LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -1 ], B_Q14[ 1 ] );
181 LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -2 ], B_Q14[ 2 ] );
182 LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -3 ], B_Q14[ 3 ] );
183 LTP_pred_Q13 = silk_SMLAWB( LTP_pred_Q13, pred_lag_ptr[ -4 ], B_Q14[ 4 ] );
Gregory Maxwellae231142011-07-30 08:18:48 -0400184 pred_lag_ptr++;
185
186 /* Generate LPC excitation */
Koen Vosa51ebd62011-12-14 11:39:29 -0500187 pres_Q14[ i ] = silk_ADD_LSHIFT32( pexc_Q14[ i ], LTP_pred_Q13, 1 );
Gregory Maxwellae231142011-07-30 08:18:48 -0400188
189 /* Update states */
Koen Vosa51ebd62011-12-14 11:39:29 -0500190 sLTP_Q15[ sLTP_buf_idx ] = silk_LSHIFT( pres_Q14[ i ], 1 );
Gregory Maxwellae231142011-07-30 08:18:48 -0400191 sLTP_buf_idx++;
192 }
193 } else {
Koen Vosa51ebd62011-12-14 11:39:29 -0500194 pres_Q14 = pexc_Q14;
Gregory Maxwellae231142011-07-30 08:18:48 -0400195 }
196
Gregory Maxwellae231142011-07-30 08:18:48 -0400197 for( i = 0; i < psDec->subfr_length; i++ ) {
Koen Vosbf75c8e2011-12-13 14:47:31 -0500198 /* Short-term prediction */
199 silk_assert( psDec->LPC_order == 10 || psDec->LPC_order == 16 );
Koen Vosbbfc9c92011-12-13 14:50:12 -0500200 /* Avoids introducing a bias because silk_SMLAWB() always rounds to -inf */
201 LPC_pred_Q10 = silk_RSHIFT( psDec->LPC_order, 1 );
202 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 1 ], A_Q12_tmp[ 0 ] );
Koen Vos3195f6c2011-10-10 20:46:32 -0400203 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 2 ], A_Q12_tmp[ 1 ] );
204 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 3 ], A_Q12_tmp[ 2 ] );
205 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 4 ], A_Q12_tmp[ 3 ] );
206 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 5 ], A_Q12_tmp[ 4 ] );
207 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 6 ], A_Q12_tmp[ 5 ] );
208 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 7 ], A_Q12_tmp[ 6 ] );
209 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 8 ], A_Q12_tmp[ 7 ] );
210 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 9 ], A_Q12_tmp[ 8 ] );
211 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 10 ], A_Q12_tmp[ 9 ] );
Koen Vosbf75c8e2011-12-13 14:47:31 -0500212 if( psDec->LPC_order == 16 ) {
213 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 11 ], A_Q12_tmp[ 10 ] );
214 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 12 ], A_Q12_tmp[ 11 ] );
215 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 13 ], A_Q12_tmp[ 12 ] );
216 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 14 ], A_Q12_tmp[ 13 ] );
217 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 15 ], A_Q12_tmp[ 14 ] );
218 LPC_pred_Q10 = silk_SMLAWB( LPC_pred_Q10, sLPC_Q14[ MAX_LPC_ORDER + i - 16 ], A_Q12_tmp[ 15 ] );
Gregory Maxwellae231142011-07-30 08:18:48 -0400219 }
220
221 /* Add prediction to LPC excitation */
Felicia Lim5ead1492016-06-04 10:10:52 -0400222 sLPC_Q14[ MAX_LPC_ORDER + i ] = silk_ADD_SAT32( pres_Q14[ i ], silk_LSHIFT_SAT32( LPC_pred_Q10, 4 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400223
Koen Vosbbfc9c92011-12-13 14:50:12 -0500224 /* Scale with gain */
Koen Vosbf75c8e2011-12-13 14:47:31 -0500225 pxq[ i ] = (opus_int16)silk_SAT16( silk_RSHIFT_ROUND( silk_SMULWW( sLPC_Q14[ MAX_LPC_ORDER + i ], Gain_Q10 ), 8 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400226 }
227
228 /* Update LPC filter state */
Koen Vos3195f6c2011-10-10 20:46:32 -0400229 silk_memcpy( sLPC_Q14, &sLPC_Q14[ psDec->subfr_length ], MAX_LPC_ORDER * sizeof( opus_int32 ) );
Koen Vosa51ebd62011-12-14 11:39:29 -0500230 pexc_Q14 += psDec->subfr_length;
Gregory Maxwellae231142011-07-30 08:18:48 -0400231 pxq += psDec->subfr_length;
232 }
233
Koen Vos3195f6c2011-10-10 20:46:32 -0400234 /* Save LPC state */
235 silk_memcpy( psDec->sLPC_Q14_buf, sLPC_Q14, MAX_LPC_ORDER * sizeof( opus_int32 ) );
Timothy B. Terriberry6f2d9f52012-09-05 07:35:49 -0700236 RESTORE_STACK;
Gregory Maxwellae231142011-07-30 08:18:48 -0400237}