annotate src/opus-1.3/silk/quant_LTP_gains.c @ 81:7029a4916348

Merge build update
author Chris Cannam
date Thu, 31 Oct 2019 13:36:58 +0000
parents 7aeed7906520
children
rev   line source
Chris@69 1 /***********************************************************************
Chris@69 2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
Chris@69 3 Redistribution and use in source and binary forms, with or without
Chris@69 4 modification, are permitted provided that the following conditions
Chris@69 5 are met:
Chris@69 6 - Redistributions of source code must retain the above copyright notice,
Chris@69 7 this list of conditions and the following disclaimer.
Chris@69 8 - Redistributions in binary form must reproduce the above copyright
Chris@69 9 notice, this list of conditions and the following disclaimer in the
Chris@69 10 documentation and/or other materials provided with the distribution.
Chris@69 11 - Neither the name of Internet Society, IETF or IETF Trust, nor the
Chris@69 12 names of specific contributors, may be used to endorse or promote
Chris@69 13 products derived from this software without specific prior written
Chris@69 14 permission.
Chris@69 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Chris@69 16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Chris@69 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Chris@69 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Chris@69 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Chris@69 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Chris@69 21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Chris@69 22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Chris@69 23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Chris@69 24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Chris@69 25 POSSIBILITY OF SUCH DAMAGE.
Chris@69 26 ***********************************************************************/
Chris@69 27
Chris@69 28 #ifdef HAVE_CONFIG_H
Chris@69 29 #include "config.h"
Chris@69 30 #endif
Chris@69 31
Chris@69 32 #include "main.h"
Chris@69 33 #include "tuning_parameters.h"
Chris@69 34
Chris@69 35 void silk_quant_LTP_gains(
Chris@69 36 opus_int16 B_Q14[ MAX_NB_SUBFR * LTP_ORDER ], /* O Quantized LTP gains */
Chris@69 37 opus_int8 cbk_index[ MAX_NB_SUBFR ], /* O Codebook Index */
Chris@69 38 opus_int8 *periodicity_index, /* O Periodicity Index */
Chris@69 39 opus_int32 *sum_log_gain_Q7, /* I/O Cumulative max prediction gain */
Chris@69 40 opus_int *pred_gain_dB_Q7, /* O LTP prediction gain */
Chris@69 41 const opus_int32 XX_Q17[ MAX_NB_SUBFR*LTP_ORDER*LTP_ORDER ], /* I Correlation matrix in Q18 */
Chris@69 42 const opus_int32 xX_Q17[ MAX_NB_SUBFR*LTP_ORDER ], /* I Correlation vector in Q18 */
Chris@69 43 const opus_int subfr_len, /* I Number of samples per subframe */
Chris@69 44 const opus_int nb_subfr, /* I Number of subframes */
Chris@69 45 int arch /* I Run-time architecture */
Chris@69 46 )
Chris@69 47 {
Chris@69 48 opus_int j, k, cbk_size;
Chris@69 49 opus_int8 temp_idx[ MAX_NB_SUBFR ];
Chris@69 50 const opus_uint8 *cl_ptr_Q5;
Chris@69 51 const opus_int8 *cbk_ptr_Q7;
Chris@69 52 const opus_uint8 *cbk_gain_ptr_Q7;
Chris@69 53 const opus_int32 *XX_Q17_ptr, *xX_Q17_ptr;
Chris@69 54 opus_int32 res_nrg_Q15_subfr, res_nrg_Q15, rate_dist_Q7_subfr, rate_dist_Q7, min_rate_dist_Q7;
Chris@69 55 opus_int32 sum_log_gain_tmp_Q7, best_sum_log_gain_Q7, max_gain_Q7;
Chris@69 56 opus_int gain_Q7;
Chris@69 57
Chris@69 58 /***************************************************/
Chris@69 59 /* iterate over different codebooks with different */
Chris@69 60 /* rates/distortions, and choose best */
Chris@69 61 /***************************************************/
Chris@69 62 min_rate_dist_Q7 = silk_int32_MAX;
Chris@69 63 best_sum_log_gain_Q7 = 0;
Chris@69 64 for( k = 0; k < 3; k++ ) {
Chris@69 65 /* Safety margin for pitch gain control, to take into account factors
Chris@69 66 such as state rescaling/rewhitening. */
Chris@69 67 opus_int32 gain_safety = SILK_FIX_CONST( 0.4, 7 );
Chris@69 68
Chris@69 69 cl_ptr_Q5 = silk_LTP_gain_BITS_Q5_ptrs[ k ];
Chris@69 70 cbk_ptr_Q7 = silk_LTP_vq_ptrs_Q7[ k ];
Chris@69 71 cbk_gain_ptr_Q7 = silk_LTP_vq_gain_ptrs_Q7[ k ];
Chris@69 72 cbk_size = silk_LTP_vq_sizes[ k ];
Chris@69 73
Chris@69 74 /* Set up pointers to first subframe */
Chris@69 75 XX_Q17_ptr = XX_Q17;
Chris@69 76 xX_Q17_ptr = xX_Q17;
Chris@69 77
Chris@69 78 res_nrg_Q15 = 0;
Chris@69 79 rate_dist_Q7 = 0;
Chris@69 80 sum_log_gain_tmp_Q7 = *sum_log_gain_Q7;
Chris@69 81 for( j = 0; j < nb_subfr; j++ ) {
Chris@69 82 max_gain_Q7 = silk_log2lin( ( SILK_FIX_CONST( MAX_SUM_LOG_GAIN_DB / 6.0, 7 ) - sum_log_gain_tmp_Q7 )
Chris@69 83 + SILK_FIX_CONST( 7, 7 ) ) - gain_safety;
Chris@69 84 silk_VQ_WMat_EC(
Chris@69 85 &temp_idx[ j ], /* O index of best codebook vector */
Chris@69 86 &res_nrg_Q15_subfr, /* O residual energy */
Chris@69 87 &rate_dist_Q7_subfr, /* O best weighted quantization error + mu * rate */
Chris@69 88 &gain_Q7, /* O sum of absolute LTP coefficients */
Chris@69 89 XX_Q17_ptr, /* I correlation matrix */
Chris@69 90 xX_Q17_ptr, /* I correlation vector */
Chris@69 91 cbk_ptr_Q7, /* I codebook */
Chris@69 92 cbk_gain_ptr_Q7, /* I codebook effective gains */
Chris@69 93 cl_ptr_Q5, /* I code length for each codebook vector */
Chris@69 94 subfr_len, /* I number of samples per subframe */
Chris@69 95 max_gain_Q7, /* I maximum sum of absolute LTP coefficients */
Chris@69 96 cbk_size, /* I number of vectors in codebook */
Chris@69 97 arch /* I Run-time architecture */
Chris@69 98 );
Chris@69 99
Chris@69 100 res_nrg_Q15 = silk_ADD_POS_SAT32( res_nrg_Q15, res_nrg_Q15_subfr );
Chris@69 101 rate_dist_Q7 = silk_ADD_POS_SAT32( rate_dist_Q7, rate_dist_Q7_subfr );
Chris@69 102 sum_log_gain_tmp_Q7 = silk_max(0, sum_log_gain_tmp_Q7
Chris@69 103 + silk_lin2log( gain_safety + gain_Q7 ) - SILK_FIX_CONST( 7, 7 ));
Chris@69 104
Chris@69 105 XX_Q17_ptr += LTP_ORDER * LTP_ORDER;
Chris@69 106 xX_Q17_ptr += LTP_ORDER;
Chris@69 107 }
Chris@69 108
Chris@69 109 if( rate_dist_Q7 <= min_rate_dist_Q7 ) {
Chris@69 110 min_rate_dist_Q7 = rate_dist_Q7;
Chris@69 111 *periodicity_index = (opus_int8)k;
Chris@69 112 silk_memcpy( cbk_index, temp_idx, nb_subfr * sizeof( opus_int8 ) );
Chris@69 113 best_sum_log_gain_Q7 = sum_log_gain_tmp_Q7;
Chris@69 114 }
Chris@69 115 }
Chris@69 116
Chris@69 117 cbk_ptr_Q7 = silk_LTP_vq_ptrs_Q7[ *periodicity_index ];
Chris@69 118 for( j = 0; j < nb_subfr; j++ ) {
Chris@69 119 for( k = 0; k < LTP_ORDER; k++ ) {
Chris@69 120 B_Q14[ j * LTP_ORDER + k ] = silk_LSHIFT( cbk_ptr_Q7[ cbk_index[ j ] * LTP_ORDER + k ], 7 );
Chris@69 121 }
Chris@69 122 }
Chris@69 123
Chris@69 124 if( nb_subfr == 2 ) {
Chris@69 125 res_nrg_Q15 = silk_RSHIFT32( res_nrg_Q15, 1 );
Chris@69 126 } else {
Chris@69 127 res_nrg_Q15 = silk_RSHIFT32( res_nrg_Q15, 2 );
Chris@69 128 }
Chris@69 129
Chris@69 130 *sum_log_gain_Q7 = best_sum_log_gain_Q7;
Chris@69 131 *pred_gain_dB_Q7 = (opus_int)silk_SMULBB( -3, silk_lin2log( res_nrg_Q15 ) - ( 15 << 7 ) );
Chris@69 132 }