blob: d67e872dcc3123ec86fe7cc9693044b59b748eaf [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
4modification, (subject to the limitations in the disclaimer below)
5are permitted provided that the following conditions are met:
6- 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.
11- Neither the name of Skype Limited, nor the names of specific
12contributors, may be used to endorse or promote products derived from
13this software without specific prior written permission.
14NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
15BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
16CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
17BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
18FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26***********************************************************************/
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"
Gregory Maxwellae231142011-07-30 08:18:48 -040033
34/* Generates excitation for CNG LPC synthesis */
Jean-Marc Valin4dc0b392011-08-15 11:24:37 -040035static inline void silk_CNG_exc(
Gregory Maxwellae231142011-07-30 08:18:48 -040036 opus_int32 residual_Q10[], /* O CNG residual signal Q10 */
37 opus_int32 exc_buf_Q10[], /* I Random samples buffer Q10 */
38 opus_int32 Gain_Q16, /* I Gain to apply */
39 opus_int length, /* I Length */
40 opus_int32 *rand_seed /* I/O Seed to random index generator */
41)
42{
43 opus_int32 seed;
44 opus_int i, idx, exc_mask;
45
46 exc_mask = CNG_BUF_MASK_MAX;
47 while( exc_mask > length ) {
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -070048 exc_mask = silk_RSHIFT( exc_mask, 1 );
Gregory Maxwellae231142011-07-30 08:18:48 -040049 }
50
51 seed = *rand_seed;
52 for( i = 0; i < length; i++ ) {
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -070053 seed = silk_RAND( seed );
54 idx = ( opus_int )( silk_RSHIFT( seed, 24 ) & exc_mask );
55 silk_assert( idx >= 0 );
56 silk_assert( idx <= CNG_BUF_MASK_MAX );
57 residual_Q10[ i ] = ( opus_int16 )silk_SAT16( silk_SMULWW( exc_buf_Q10[ idx ], Gain_Q16 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -040058 }
59 *rand_seed = seed;
60}
61
62void silk_CNG_Reset(
Koen Vosacc7a6c2011-10-28 19:44:26 -040063 silk_decoder_state *psDec /* I/O Decoder state */
Gregory Maxwellae231142011-07-30 08:18:48 -040064)
65{
66 opus_int i, NLSF_step_Q15, NLSF_acc_Q15;
67
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -070068 NLSF_step_Q15 = silk_DIV32_16( silk_int16_MAX, psDec->LPC_order + 1 );
Gregory Maxwellae231142011-07-30 08:18:48 -040069 NLSF_acc_Q15 = 0;
70 for( i = 0; i < psDec->LPC_order; i++ ) {
71 NLSF_acc_Q15 += NLSF_step_Q15;
72 psDec->sCNG.CNG_smth_NLSF_Q15[ i ] = NLSF_acc_Q15;
73 }
74 psDec->sCNG.CNG_smth_Gain_Q16 = 0;
75 psDec->sCNG.rand_seed = 3176576;
76}
77
78/* Updates CNG estimate, and applies the CNG when packet was lost */
79void silk_CNG(
Koen Vosacc7a6c2011-10-28 19:44:26 -040080 silk_decoder_state *psDec, /* I/O Decoder state */
81 silk_decoder_control *psDecCtrl, /* I/O Decoder control */
82 opus_int16 frame[], /* I/O Signal */
83 opus_int length /* I Length of residual */
Gregory Maxwellae231142011-07-30 08:18:48 -040084)
85{
86 opus_int i, j, subfr;
87 opus_int32 sum_Q6, max_Gain_Q16;
88 opus_int16 A_Q12[ MAX_LPC_ORDER ];
89 opus_int32 CNG_sig_Q10[ MAX_FRAME_LENGTH + MAX_LPC_ORDER ];
90 silk_CNG_struct *psCNG = &psDec->sCNG;
91
92 if( psDec->fs_kHz != psCNG->fs_kHz ) {
93 /* Reset state */
94 silk_CNG_Reset( psDec );
95
96 psCNG->fs_kHz = psDec->fs_kHz;
97 }
98 if( psDec->lossCnt == 0 && psDec->prevSignalType == TYPE_NO_VOICE_ACTIVITY ) {
99 /* Update CNG parameters */
100
101 /* Smoothing of LSF's */
102 for( i = 0; i < psDec->LPC_order; i++ ) {
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700103 psCNG->CNG_smth_NLSF_Q15[ i ] += silk_SMULWB( psDec->prevNLSF_Q15[ i ] - psCNG->CNG_smth_NLSF_Q15[ i ], CNG_NLSF_SMTH_Q16 );
Gregory Maxwellae231142011-07-30 08:18:48 -0400104 }
105 /* Find the subframe with the highest gain */
106 max_Gain_Q16 = 0;
107 subfr = 0;
108 for( i = 0; i < psDec->nb_subfr; i++ ) {
109 if( psDecCtrl->Gains_Q16[ i ] > max_Gain_Q16 ) {
110 max_Gain_Q16 = psDecCtrl->Gains_Q16[ i ];
111 subfr = i;
112 }
113 }
114 /* Update CNG excitation buffer with excitation from this subframe */
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700115 silk_memmove( &psCNG->CNG_exc_buf_Q10[ psDec->subfr_length ], psCNG->CNG_exc_buf_Q10, ( psDec->nb_subfr - 1 ) * psDec->subfr_length * sizeof( opus_int32 ) );
116 silk_memcpy( psCNG->CNG_exc_buf_Q10, &psDec->exc_Q10[ subfr * psDec->subfr_length ], psDec->subfr_length * sizeof( opus_int32 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400117
118 /* Smooth gains */
119 for( i = 0; i < psDec->nb_subfr; i++ ) {
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700120 psCNG->CNG_smth_Gain_Q16 += silk_SMULWB( psDecCtrl->Gains_Q16[ i ] - psCNG->CNG_smth_Gain_Q16, CNG_GAIN_SMTH_Q16 );
Gregory Maxwellae231142011-07-30 08:18:48 -0400121 }
122 }
123
124 /* Add CNG when packet is lost or during DTX */
125 if( psDec->lossCnt ) {
126
127 /* Generate CNG excitation */
128 silk_CNG_exc( CNG_sig_Q10 + MAX_LPC_ORDER, psCNG->CNG_exc_buf_Q10, psCNG->CNG_smth_Gain_Q16, length, &psCNG->rand_seed );
129
130 /* Convert CNG NLSF to filter representation */
131 silk_NLSF2A( A_Q12, psCNG->CNG_smth_NLSF_Q15, psDec->LPC_order );
132
133 /* Generate CNG signal, by synthesis filtering */
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700134 silk_memcpy( CNG_sig_Q10, psCNG->CNG_synth_state, MAX_LPC_ORDER * sizeof( opus_int32 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400135 for( i = 0; i < length; i++ ) {
136 /* Partially unrolled */
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700137 sum_Q6 = silk_SMULWB( CNG_sig_Q10[ MAX_LPC_ORDER + i - 1 ], A_Q12[ 0 ] );
138 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 2 ], A_Q12[ 1 ] );
139 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 3 ], A_Q12[ 2 ] );
140 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 4 ], A_Q12[ 3 ] );
141 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 5 ], A_Q12[ 4 ] );
142 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 6 ], A_Q12[ 5 ] );
143 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 7 ], A_Q12[ 6 ] );
144 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 8 ], A_Q12[ 7 ] );
145 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 9 ], A_Q12[ 8 ] );
146 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - 10 ], A_Q12[ 9 ] );
Gregory Maxwellae231142011-07-30 08:18:48 -0400147 for( j = 10; j < psDec->LPC_order; j++ ) {
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700148 sum_Q6 = silk_SMLAWB( sum_Q6, CNG_sig_Q10[ MAX_LPC_ORDER + i - j - 1 ], A_Q12[ j ] );
Gregory Maxwellae231142011-07-30 08:18:48 -0400149 }
150
151 /* Update states */
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700152 CNG_sig_Q10[ MAX_LPC_ORDER + i ] = silk_ADD_LSHIFT( CNG_sig_Q10[ MAX_LPC_ORDER + i ], sum_Q6, 4 );
Gregory Maxwellae231142011-07-30 08:18:48 -0400153
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700154 frame[ i ] = silk_ADD_SAT16( frame[ i ], silk_RSHIFT_ROUND( sum_Q6, 6 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400155 }
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700156 silk_memcpy( psCNG->CNG_synth_state, &CNG_sig_Q10[ length ], MAX_LPC_ORDER * sizeof( opus_int32 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400157 } else {
Jean-Marc Valinfb3a4372011-09-16 00:58:26 -0700158 silk_memset( psCNG->CNG_synth_state, 0, psDec->LPC_order * sizeof( opus_int32 ) );
Gregory Maxwellae231142011-07-30 08:18:48 -0400159 }
160}