cannam@154: /* Copyright (c) 2014, Cisco Systems, INC cannam@154: Written by XiangMingZhu WeiZhou MinPeng YanWang cannam@154: cannam@154: Redistribution and use in source and binary forms, with or without cannam@154: modification, are permitted provided that the following conditions cannam@154: are met: cannam@154: cannam@154: - Redistributions of source code must retain the above copyright cannam@154: notice, this list of conditions and the following disclaimer. cannam@154: cannam@154: - Redistributions in binary form must reproduce the above copyright cannam@154: notice, this list of conditions and the following disclaimer in the cannam@154: documentation and/or other materials provided with the distribution. cannam@154: cannam@154: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@154: ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT cannam@154: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR cannam@154: A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER cannam@154: OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@154: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@154: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR cannam@154: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF cannam@154: LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING cannam@154: NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS cannam@154: SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@154: */ cannam@154: cannam@154: #ifdef HAVE_CONFIG_H cannam@154: #include "config.h" cannam@154: #endif cannam@154: cannam@154: #include cannam@154: #include cannam@154: #include cannam@154: #include "main.h" cannam@154: #include "celt/x86/x86cpu.h" cannam@154: cannam@154: /* Entropy constrained matrix-weighted VQ, hard-coded to 5-element vectors, for a single input data vector */ cannam@154: void silk_VQ_WMat_EC_sse4_1( cannam@154: opus_int8 *ind, /* O index of best codebook vector */ cannam@154: opus_int32 *rate_dist_Q14, /* O best weighted quant error + mu * rate */ cannam@154: opus_int *gain_Q7, /* O sum of absolute LTP coefficients */ cannam@154: const opus_int16 *in_Q14, /* I input vector to be quantized */ cannam@154: const opus_int32 *W_Q18, /* I weighting matrix */ cannam@154: const opus_int8 *cb_Q7, /* I codebook */ cannam@154: const opus_uint8 *cb_gain_Q7, /* I codebook effective gain */ cannam@154: const opus_uint8 *cl_Q5, /* I code length for each codebook vector */ cannam@154: const opus_int mu_Q9, /* I tradeoff betw. weighted error and rate */ cannam@154: const opus_int32 max_gain_Q7, /* I maximum sum of absolute LTP coefficients */ cannam@154: opus_int L /* I number of vectors in codebook */ cannam@154: ) cannam@154: { cannam@154: opus_int k, gain_tmp_Q7; cannam@154: const opus_int8 *cb_row_Q7; cannam@154: opus_int16 diff_Q14[ 5 ]; cannam@154: opus_int32 sum1_Q14, sum2_Q16; cannam@154: cannam@154: __m128i C_tmp1, C_tmp2, C_tmp3, C_tmp4, C_tmp5; cannam@154: /* Loop over codebook */ cannam@154: *rate_dist_Q14 = silk_int32_MAX; cannam@154: cb_row_Q7 = cb_Q7; cannam@154: for( k = 0; k < L; k++ ) { cannam@154: gain_tmp_Q7 = cb_gain_Q7[k]; cannam@154: cannam@154: diff_Q14[ 0 ] = in_Q14[ 0 ] - silk_LSHIFT( cb_row_Q7[ 0 ], 7 ); cannam@154: cannam@154: C_tmp1 = OP_CVTEPI16_EPI32_M64( &in_Q14[ 1 ] ); cannam@154: C_tmp2 = OP_CVTEPI8_EPI32_M32( &cb_row_Q7[ 1 ] ); cannam@154: C_tmp2 = _mm_slli_epi32( C_tmp2, 7 ); cannam@154: C_tmp1 = _mm_sub_epi32( C_tmp1, C_tmp2 ); cannam@154: cannam@154: diff_Q14[ 1 ] = _mm_extract_epi16( C_tmp1, 0 ); cannam@154: diff_Q14[ 2 ] = _mm_extract_epi16( C_tmp1, 2 ); cannam@154: diff_Q14[ 3 ] = _mm_extract_epi16( C_tmp1, 4 ); cannam@154: diff_Q14[ 4 ] = _mm_extract_epi16( C_tmp1, 6 ); cannam@154: cannam@154: /* Weighted rate */ cannam@154: sum1_Q14 = silk_SMULBB( mu_Q9, cl_Q5[ k ] ); cannam@154: cannam@154: /* Penalty for too large gain */ cannam@154: sum1_Q14 = silk_ADD_LSHIFT32( sum1_Q14, silk_max( silk_SUB32( gain_tmp_Q7, max_gain_Q7 ), 0 ), 10 ); cannam@154: cannam@154: silk_assert( sum1_Q14 >= 0 ); cannam@154: cannam@154: /* first row of W_Q18 */ cannam@154: C_tmp3 = _mm_loadu_si128( (__m128i *)(&W_Q18[ 1 ] ) ); cannam@154: C_tmp4 = _mm_mul_epi32( C_tmp3, C_tmp1 ); cannam@154: C_tmp4 = _mm_srli_si128( C_tmp4, 2 ); cannam@154: cannam@154: C_tmp1 = _mm_shuffle_epi32( C_tmp1, _MM_SHUFFLE( 0, 3, 2, 1 ) ); /* shift right 4 bytes */ cannam@154: C_tmp3 = _mm_shuffle_epi32( C_tmp3, _MM_SHUFFLE( 0, 3, 2, 1 ) ); /* shift right 4 bytes */ cannam@154: cannam@154: C_tmp5 = _mm_mul_epi32( C_tmp3, C_tmp1 ); cannam@154: C_tmp5 = _mm_srli_si128( C_tmp5, 2 ); cannam@154: cannam@154: C_tmp5 = _mm_add_epi32( C_tmp4, C_tmp5 ); cannam@154: C_tmp5 = _mm_slli_epi32( C_tmp5, 1 ); cannam@154: cannam@154: C_tmp5 = _mm_add_epi32( C_tmp5, _mm_shuffle_epi32( C_tmp5, _MM_SHUFFLE( 0, 0, 0, 2 ) ) ); cannam@154: sum2_Q16 = _mm_cvtsi128_si32( C_tmp5 ); cannam@154: cannam@154: sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 0 ], diff_Q14[ 0 ] ); cannam@154: sum1_Q14 = silk_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14[ 0 ] ); cannam@154: cannam@154: /* second row of W_Q18 */ cannam@154: sum2_Q16 = silk_SMULWB( W_Q18[ 7 ], diff_Q14[ 2 ] ); cannam@154: sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 8 ], diff_Q14[ 3 ] ); cannam@154: sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 9 ], diff_Q14[ 4 ] ); cannam@154: sum2_Q16 = silk_LSHIFT( sum2_Q16, 1 ); cannam@154: sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 6 ], diff_Q14[ 1 ] ); cannam@154: sum1_Q14 = silk_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14[ 1 ] ); cannam@154: cannam@154: /* third row of W_Q18 */ cannam@154: sum2_Q16 = silk_SMULWB( W_Q18[ 13 ], diff_Q14[ 3 ] ); cannam@154: sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 14 ], diff_Q14[ 4 ] ); cannam@154: sum2_Q16 = silk_LSHIFT( sum2_Q16, 1 ); cannam@154: sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 12 ], diff_Q14[ 2 ] ); cannam@154: sum1_Q14 = silk_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14[ 2 ] ); cannam@154: cannam@154: /* fourth row of W_Q18 */ cannam@154: sum2_Q16 = silk_SMULWB( W_Q18[ 19 ], diff_Q14[ 4 ] ); cannam@154: sum2_Q16 = silk_LSHIFT( sum2_Q16, 1 ); cannam@154: sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 18 ], diff_Q14[ 3 ] ); cannam@154: sum1_Q14 = silk_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14[ 3 ] ); cannam@154: cannam@154: /* last row of W_Q18 */ cannam@154: sum2_Q16 = silk_SMULWB( W_Q18[ 24 ], diff_Q14[ 4 ] ); cannam@154: sum1_Q14 = silk_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14[ 4 ] ); cannam@154: cannam@154: silk_assert( sum1_Q14 >= 0 ); cannam@154: cannam@154: /* find best */ cannam@154: if( sum1_Q14 < *rate_dist_Q14 ) { cannam@154: *rate_dist_Q14 = sum1_Q14; cannam@154: *ind = (opus_int8)k; cannam@154: *gain_Q7 = gain_tmp_Q7; cannam@154: } cannam@154: cannam@154: /* Go to next cbk vector */ cannam@154: cb_row_Q7 += LTP_ORDER; cannam@154: } cannam@154: }