SILK update with LBRR and some bugfixes
[opus.git] / src_common / SKP_Silk_setup.h
1 /***********************************************************************\r
2 Copyright (c) 2006-2011, Skype Limited. All rights reserved. \r
3 Redistribution and use in source and binary forms, with or without \r
4 modification, (subject to the limitations in the disclaimer below) \r
5 are permitted provided that the following conditions are met:\r
6 - Redistributions of source code must retain the above copyright notice,\r
7 this list of conditions and the following disclaimer.\r
8 - Redistributions in binary form must reproduce the above copyright \r
9 notice, this list of conditions and the following disclaimer in the \r
10 documentation and/or other materials provided with the distribution.\r
11 - Neither the name of Skype Limited, nor the names of specific \r
12 contributors, may be used to endorse or promote products derived from \r
13 this software without specific prior written permission.\r
14 NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED \r
15 BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND \r
16 CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,\r
17 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND \r
18 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE \r
19 COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, \r
20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\r
21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF \r
22 USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
23 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE \r
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
26 ***********************************************************************/\r
27 \r
28 #include "SKP_Silk_main.h"\r
29 #include "SKP_Silk_tuning_parameters.h"\r
30 \r
31 SKP_INLINE SKP_int SKP_Silk_setup_complexity(\r
32     SKP_Silk_encoder_state          *psEncC,            /* I/O                      */\r
33     SKP_int                         Complexity          /* I                        */\r
34 )\r
35 {\r
36     SKP_int ret = 0;\r
37 \r
38     /* Set encoding complexity */\r
39     if( Complexity < 2 ) {\r
40         psEncC->pitchEstimationComplexity       = SKP_Silk_PE_MIN_COMPLEX;\r
41         psEncC->pitchEstimationThreshold_Q16    = SKP_FIX_CONST( 0.8, 16 );\r
42         psEncC->pitchEstimationLPCOrder         = 6;\r
43         psEncC->shapingLPCOrder                 = 8;\r
44         psEncC->la_shape                        = 3 * psEncC->fs_kHz;\r
45         psEncC->nStatesDelayedDecision          = 1;\r
46         psEncC->useInterpolatedNLSFs            = 0;\r
47         psEncC->LTPQuantLowComplexity           = 1;\r
48         psEncC->NLSF_MSVQ_Survivors             = 2;\r
49         psEncC->warping_Q16                     = 0;\r
50     } else if( Complexity < 4 ) {\r
51         psEncC->pitchEstimationComplexity       = SKP_Silk_PE_MID_COMPLEX;\r
52         psEncC->pitchEstimationThreshold_Q16    = SKP_FIX_CONST( 0.76, 16 );\r
53         psEncC->pitchEstimationLPCOrder         = 8;\r
54         psEncC->shapingLPCOrder                 = 10;\r
55         psEncC->la_shape                        = 5 * psEncC->fs_kHz;\r
56         psEncC->nStatesDelayedDecision          = 1;\r
57         psEncC->useInterpolatedNLSFs            = 1;\r
58         psEncC->LTPQuantLowComplexity           = 0;\r
59         psEncC->NLSF_MSVQ_Survivors             = 4;\r
60         psEncC->warping_Q16                     = 0;\r
61     } else if( Complexity < 6 ) {\r
62         psEncC->pitchEstimationComplexity       = SKP_Silk_PE_MID_COMPLEX;\r
63         psEncC->pitchEstimationThreshold_Q16    = SKP_FIX_CONST( 0.74, 16 );\r
64         psEncC->pitchEstimationLPCOrder         = 10;\r
65         psEncC->shapingLPCOrder                 = 12;\r
66         psEncC->la_shape                        = 5 * psEncC->fs_kHz;\r
67         psEncC->nStatesDelayedDecision          = 2;\r
68         psEncC->useInterpolatedNLSFs            = 0;\r
69         psEncC->LTPQuantLowComplexity           = 0;\r
70         psEncC->NLSF_MSVQ_Survivors             = 6;\r
71         psEncC->warping_Q16                     = psEncC->fs_kHz * SKP_FIX_CONST( WARPING_MULTIPLIER, 16 );\r
72     } else if( Complexity < 8 ) {\r
73         psEncC->pitchEstimationComplexity       = SKP_Silk_PE_MID_COMPLEX;\r
74         psEncC->pitchEstimationThreshold_Q16    = SKP_FIX_CONST( 0.72, 16 );\r
75         psEncC->pitchEstimationLPCOrder         = 12;\r
76         psEncC->shapingLPCOrder                 = 14;\r
77         psEncC->la_shape                        = 5 * psEncC->fs_kHz;\r
78         psEncC->nStatesDelayedDecision          = 3;\r
79         psEncC->useInterpolatedNLSFs            = 0;\r
80         psEncC->LTPQuantLowComplexity           = 0;\r
81         psEncC->NLSF_MSVQ_Survivors             = 8;\r
82         psEncC->warping_Q16                     = psEncC->fs_kHz * SKP_FIX_CONST( WARPING_MULTIPLIER, 16 );\r
83     } else if( Complexity <= 10 ) {\r
84         psEncC->pitchEstimationComplexity       = SKP_Silk_PE_MAX_COMPLEX;\r
85         psEncC->pitchEstimationThreshold_Q16    = SKP_FIX_CONST( 0.7, 16 );\r
86         psEncC->pitchEstimationLPCOrder         = 16;\r
87         psEncC->shapingLPCOrder                 = 16;\r
88         psEncC->la_shape                        = 5 * psEncC->fs_kHz;\r
89         psEncC->nStatesDelayedDecision          = MAX_DEL_DEC_STATES;\r
90         psEncC->useInterpolatedNLSFs            = 1;\r
91         psEncC->LTPQuantLowComplexity           = 0;\r
92         psEncC->NLSF_MSVQ_Survivors             = 16;\r
93         psEncC->warping_Q16                     = psEncC->fs_kHz * SKP_FIX_CONST( WARPING_MULTIPLIER, 16 );\r
94     } else {\r
95         ret = SKP_SILK_ENC_INVALID_COMPLEXITY_SETTING;\r
96     }\r
97 \r
98     /* Do not allow higher pitch estimation LPC order than predict LPC order */\r
99     psEncC->pitchEstimationLPCOrder = SKP_min_int( psEncC->pitchEstimationLPCOrder, psEncC->predictLPCOrder );\r
100     psEncC->shapeWinLength          = SUB_FRAME_LENGTH_MS * psEncC->fs_kHz + 2 * psEncC->la_shape;\r
101 \r
102     SKP_assert( psEncC->pitchEstimationLPCOrder <= MAX_FIND_PITCH_LPC_ORDER );\r
103     SKP_assert( psEncC->shapingLPCOrder         <= MAX_SHAPE_LPC_ORDER      );\r
104     SKP_assert( psEncC->nStatesDelayedDecision  <= MAX_DEL_DEC_STATES       );\r
105     SKP_assert( psEncC->warping_Q16             <= 32767                    );\r
106     SKP_assert( psEncC->la_shape                <= LA_SHAPE_MAX             );\r
107     SKP_assert( psEncC->shapeWinLength          <= SHAPE_LPC_WIN_MAX        );\r
108     SKP_assert( psEncC->NLSF_MSVQ_Survivors     <= MAX_NLSF_MSVQ_SURVIVORS  );\r
109 \r
110     return( ret );\r
111 }\r
112 \r
113 SKP_INLINE SKP_int SKP_Silk_setup_LBRR(\r
114     SKP_Silk_encoder_state          *psEncC            /* I/O                      */\r
115 )\r
116 {\r
117     SKP_int   ret = SKP_SILK_NO_ERROR;\r
118     SKP_int32 LBRRRate_thres_bps;\r
119 \r
120     if( psEncC->useInBandFEC < 0 || psEncC->useInBandFEC > 1 ) {\r
121         ret = SKP_SILK_ENC_INVALID_INBAND_FEC_SETTING;\r
122     }\r
123     \r
124     if( psEncC->fs_kHz == 8 ) {\r
125         LBRRRate_thres_bps = LBRR_NB_MIN_RATE_BPS;\r
126     } else if( psEncC->fs_kHz == 12 ) {\r
127         LBRRRate_thres_bps = LBRR_MB_MIN_RATE_BPS;\r
128     } else if( psEncC->fs_kHz == 16 ) {\r
129         LBRRRate_thres_bps = LBRR_WB_MIN_RATE_BPS;\r
130     } else {\r
131         SKP_assert( 0 );\r
132     }\r
133 \r
134     LBRRRate_thres_bps = SKP_RSHIFT( SKP_SMULBB( LBRRRate_thres_bps, 7 - psEncC->PacketLoss_perc ), 2 );\r
135     if( psEncC->useInBandFEC && psEncC->TargetRate_bps >= LBRRRate_thres_bps && psEncC->PacketLoss_perc > 0 ) {\r
136         /* Set gain increase / rate reduction for LBRR usage */\r
137         psEncC->LBRR_GainIncreases = SKP_max_int( 6 - SKP_SMULWB( psEncC->PacketLoss_perc, SKP_FIX_CONST( 0.4, 16 ) ), 2 );\r
138         psEncC->LBRR_enabled = 1;\r
139     } else {\r
140         psEncC->LBRR_enabled = 0;\r
141     }\r
142     return ret;\r
143 }\r