• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2014-2020, Cisco Systems, INC
2    Written by XiangMingZhu WeiZhou MinPeng YanWang FrancisQuiers
3 
4    Redistribution and use in source and binary forms, with or without
5    modification, are permitted provided that the following conditions
6    are met:
7 
8    - Redistributions of source code must retain the above copyright
9    notice, this list of conditions and the following disclaimer.
10 
11    - Redistributions in binary form must reproduce the above copyright
12    notice, this list of conditions and the following disclaimer in the
13    documentation and/or other materials provided with the distribution.
14 
15    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18    A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19    OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 
32 #include <xmmintrin.h>
33 #include <emmintrin.h>
34 #include <smmintrin.h>
35 #include "main.h"
36 #include "celt/x86/x86cpu.h"
37 
38 /* Entropy constrained matrix-weighted VQ, hard-coded to 5-element vectors, for a single input data vector */
silk_VQ_WMat_EC_sse4_1(opus_int8 * ind,opus_int32 * res_nrg_Q15,opus_int32 * rate_dist_Q8,opus_int * gain_Q7,const opus_int32 * XX_Q17,const opus_int32 * xX_Q17,const opus_int8 * cb_Q7,const opus_uint8 * cb_gain_Q7,const opus_uint8 * cl_Q5,const opus_int subfr_len,const opus_int32 max_gain_Q7,const opus_int L)39 void silk_VQ_WMat_EC_sse4_1(
40     opus_int8                   *ind,                           /* O    index of best codebook vector               */
41     opus_int32                  *res_nrg_Q15,                   /* O    best residual energy                        */
42     opus_int32                  *rate_dist_Q8,                  /* O    best total bitrate                          */
43     opus_int                    *gain_Q7,                       /* O    sum of absolute LTP coefficients            */
44     const opus_int32            *XX_Q17,                        /* I    correlation matrix                          */
45     const opus_int32            *xX_Q17,                        /* I    correlation vector                          */
46     const opus_int8             *cb_Q7,                         /* I    codebook                                    */
47     const opus_uint8            *cb_gain_Q7,                    /* I    codebook effective gain                     */
48     const opus_uint8            *cl_Q5,                         /* I    code length for each codebook vector        */
49     const opus_int              subfr_len,                      /* I    number of samples per subframe              */
50     const opus_int32            max_gain_Q7,                    /* I    maximum sum of absolute LTP coefficients    */
51     const opus_int              L                               /* I    number of vectors in codebook               */
52 )
53 {
54     opus_int   k, gain_tmp_Q7;
55     const opus_int8 *cb_row_Q7;
56     opus_int32 neg_xX_Q24[ 5 ];
57     opus_int32 sum1_Q15, sum2_Q24;
58     opus_int32 bits_res_Q8, bits_tot_Q8;
59     __m128i v_XX_31_Q17, v_XX_42_Q17, v_cb_row_31_Q7, v_cb_row_42_Q7, v_acc1_Q24, v_acc2_Q24;
60 
61     /* Negate and convert to new Q domain */
62     neg_xX_Q24[ 0 ] = -silk_LSHIFT32( xX_Q17[ 0 ], 7 );
63     neg_xX_Q24[ 1 ] = -silk_LSHIFT32( xX_Q17[ 1 ], 7 );
64     neg_xX_Q24[ 2 ] = -silk_LSHIFT32( xX_Q17[ 2 ], 7 );
65     neg_xX_Q24[ 3 ] = -silk_LSHIFT32( xX_Q17[ 3 ], 7 );
66     neg_xX_Q24[ 4 ] = -silk_LSHIFT32( xX_Q17[ 4 ], 7 );
67 
68     v_XX_31_Q17 = _mm_loadu_si128( (__m128i *)(&XX_Q17[ 1 ] ) );
69     v_XX_42_Q17 = _mm_shuffle_epi32( v_XX_31_Q17, _MM_SHUFFLE( 0, 3, 2, 1 ) );
70 
71     /* Loop over codebook */
72     *rate_dist_Q8 = silk_int32_MAX;
73     *res_nrg_Q15 = silk_int32_MAX;
74     cb_row_Q7 = cb_Q7;
75     /* If things go really bad, at least *ind is set to something safe. */
76     *ind = 0;
77     for( k = 0; k < L; k++ ) {
78         opus_int32 penalty;
79         gain_tmp_Q7 = cb_gain_Q7[k];
80         /* Weighted rate */
81         /* Quantization error: 1 - 2 * xX * cb + cb' * XX * cb */
82         sum1_Q15 = SILK_FIX_CONST( 1.001, 15 );
83 
84         /* Penalty for too large gain */
85         penalty = silk_LSHIFT32( silk_max( silk_SUB32( gain_tmp_Q7, max_gain_Q7 ), 0 ), 11 );
86 
87         /* first row of XX_Q17 */
88         v_cb_row_31_Q7 = OP_CVTEPI8_EPI32_M32( &cb_row_Q7[ 1 ] );
89         v_cb_row_42_Q7 = _mm_shuffle_epi32( v_cb_row_31_Q7, _MM_SHUFFLE( 0, 3, 2, 1 ) );
90         v_cb_row_31_Q7 = _mm_mul_epi32( v_XX_31_Q17, v_cb_row_31_Q7 );
91         v_cb_row_42_Q7 = _mm_mul_epi32( v_XX_42_Q17, v_cb_row_42_Q7 );
92         v_acc1_Q24 = _mm_add_epi64( v_cb_row_31_Q7, v_cb_row_42_Q7);
93         v_acc2_Q24 = _mm_shuffle_epi32( v_acc1_Q24, _MM_SHUFFLE( 1, 0, 3, 2 ) );
94         v_acc1_Q24 = _mm_add_epi64( v_acc1_Q24, v_acc2_Q24);
95         sum2_Q24 = _mm_cvtsi128_si32( v_acc1_Q24 );
96         sum2_Q24 = silk_ADD32( neg_xX_Q24[ 0 ], sum2_Q24 );
97         sum2_Q24 = silk_LSHIFT32( sum2_Q24, 1 );
98         sum2_Q24 = silk_MLA( sum2_Q24,        XX_Q17[  0 ], cb_row_Q7[ 0 ] );
99         sum1_Q15 = silk_SMLAWB( sum1_Q15,        sum2_Q24,  cb_row_Q7[ 0 ] );
100 
101         /* second row of XX_Q17 */
102         sum2_Q24 = silk_MLA( neg_xX_Q24[ 1 ], XX_Q17[  7 ], cb_row_Q7[ 2 ] );
103         sum2_Q24 = silk_MLA( sum2_Q24,        XX_Q17[  8 ], cb_row_Q7[ 3 ] );
104         sum2_Q24 = silk_MLA( sum2_Q24,        XX_Q17[  9 ], cb_row_Q7[ 4 ] );
105         sum2_Q24 = silk_LSHIFT32( sum2_Q24, 1 );
106         sum2_Q24 = silk_MLA( sum2_Q24,        XX_Q17[  6 ], cb_row_Q7[ 1 ] );
107         sum1_Q15 = silk_SMLAWB( sum1_Q15,        sum2_Q24,  cb_row_Q7[ 1 ] );
108 
109         /* third row of XX_Q17 */
110         sum2_Q24 = silk_MLA( neg_xX_Q24[ 2 ], XX_Q17[ 13 ], cb_row_Q7[ 3 ] );
111         sum2_Q24 = silk_MLA( sum2_Q24,        XX_Q17[ 14 ], cb_row_Q7[ 4 ] );
112         sum2_Q24 = silk_LSHIFT32( sum2_Q24, 1 );
113         sum2_Q24 = silk_MLA( sum2_Q24,        XX_Q17[ 12 ], cb_row_Q7[ 2 ] );
114         sum1_Q15 = silk_SMLAWB( sum1_Q15,        sum2_Q24,  cb_row_Q7[ 2 ] );
115 
116         /* fourth row of XX_Q17 */
117         sum2_Q24 = silk_MLA( neg_xX_Q24[ 3 ], XX_Q17[ 19 ], cb_row_Q7[ 4 ] );
118         sum2_Q24 = silk_LSHIFT32( sum2_Q24, 1 );
119         sum2_Q24 = silk_MLA( sum2_Q24,        XX_Q17[ 18 ], cb_row_Q7[ 3 ] );
120         sum1_Q15 = silk_SMLAWB( sum1_Q15,        sum2_Q24,  cb_row_Q7[ 3 ] );
121 
122         /* last row of XX_Q17 */
123         sum2_Q24 = silk_LSHIFT32( neg_xX_Q24[ 4 ], 1 );
124         sum2_Q24 = silk_MLA( sum2_Q24,        XX_Q17[ 24 ], cb_row_Q7[ 4 ] );
125         sum1_Q15 = silk_SMLAWB( sum1_Q15,        sum2_Q24,  cb_row_Q7[ 4 ] );
126 
127         /* find best */
128         if( sum1_Q15 >= 0 ) {
129             /* Translate residual energy to bits using high-rate assumption (6 dB ==> 1 bit/sample) */
130             bits_res_Q8 = silk_SMULBB( subfr_len, silk_lin2log( sum1_Q15 + penalty) - (15 << 7) );
131             /* In the following line we reduce the codelength component by half ("-1"); seems to slightly improve quality */
132             bits_tot_Q8 = silk_ADD_LSHIFT32( bits_res_Q8, cl_Q5[ k ], 3-1 );
133             if( bits_tot_Q8 <= *rate_dist_Q8 ) {
134                 *rate_dist_Q8 = bits_tot_Q8;
135                 *res_nrg_Q15 = sum1_Q15 + penalty;
136                 *ind = (opus_int8)k;
137                 *gain_Q7 = gain_tmp_Q7;
138             }
139         }
140 
141         /* Go to next cbk vector */
142         cb_row_Q7 += LTP_ORDER;
143     }
144 
145 #ifdef OPUS_CHECK_ASM
146     {
147         opus_int8  ind_c = 0;
148         opus_int32 res_nrg_Q15_c = 0;
149         opus_int32 rate_dist_Q8_c = 0;
150         opus_int   gain_Q7_c = 0;
151 
152         silk_VQ_WMat_EC_c(
153             &ind_c,
154             &res_nrg_Q15_c,
155             &rate_dist_Q8_c,
156             &gain_Q7_c,
157             XX_Q17,
158             xX_Q17,
159             cb_Q7,
160             cb_gain_Q7,
161             cl_Q5,
162             subfr_len,
163             max_gain_Q7,
164             L
165         );
166 
167         silk_assert( *ind == ind_c );
168         silk_assert( *res_nrg_Q15 == res_nrg_Q15_c );
169         silk_assert( *rate_dist_Q8 == rate_dist_Q8_c );
170         silk_assert( *gain_Q7 == gain_Q7_c );
171     }
172 #endif
173 }
174