blob: a308cfbf1c50db424152bb978f22329adc37f7be [file] [log] [blame]
Alexandre Lision744f7422013-09-25 11:39:37 -04001/***********************************************************************
2Copyright (c) 2006-2011, Skype Limited. All rights reserved.
3Redistribution and use in source and binary forms, with or without
4modification, are permitted provided that the following conditions
5are 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 Internet Society, IETF or IETF Trust, nor the
12names of specific contributors, may be used to endorse or promote
13products derived from this software without specific prior written
14permission.
15THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS”
16AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25POSSIBILITY OF SUCH DAMAGE.
26***********************************************************************/
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include "main.h"
33
34/* Entropy constrained matrix-weighted VQ, hard-coded to 5-element vectors, for a single input data vector */
35void silk_VQ_WMat_EC(
36 opus_int8 *ind, /* O index of best codebook vector */
37 opus_int32 *rate_dist_Q14, /* O best weighted quant error + mu * rate */
38 const opus_int16 *in_Q14, /* I input vector to be quantized */
39 const opus_int32 *W_Q18, /* I weighting matrix */
40 const opus_int8 *cb_Q7, /* I codebook */
41 const opus_uint8 *cl_Q5, /* I code length for each codebook vector */
42 const opus_int mu_Q9, /* I tradeoff betw. weighted error and rate */
43 opus_int L /* I number of vectors in codebook */
44)
45{
46 opus_int k;
47 const opus_int8 *cb_row_Q7;
48 opus_int16 diff_Q14[ 5 ];
49 opus_int32 sum1_Q14, sum2_Q16;
50
51 /* Loop over codebook */
52 *rate_dist_Q14 = silk_int32_MAX;
53 cb_row_Q7 = cb_Q7;
54 for( k = 0; k < L; k++ ) {
55 diff_Q14[ 0 ] = in_Q14[ 0 ] - silk_LSHIFT( cb_row_Q7[ 0 ], 7 );
56 diff_Q14[ 1 ] = in_Q14[ 1 ] - silk_LSHIFT( cb_row_Q7[ 1 ], 7 );
57 diff_Q14[ 2 ] = in_Q14[ 2 ] - silk_LSHIFT( cb_row_Q7[ 2 ], 7 );
58 diff_Q14[ 3 ] = in_Q14[ 3 ] - silk_LSHIFT( cb_row_Q7[ 3 ], 7 );
59 diff_Q14[ 4 ] = in_Q14[ 4 ] - silk_LSHIFT( cb_row_Q7[ 4 ], 7 );
60
61 /* Weighted rate */
62 sum1_Q14 = silk_SMULBB( mu_Q9, cl_Q5[ k ] );
63
64 silk_assert( sum1_Q14 >= 0 );
65
66 /* first row of W_Q18 */
67 sum2_Q16 = silk_SMULWB( W_Q18[ 1 ], diff_Q14[ 1 ] );
68 sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 2 ], diff_Q14[ 2 ] );
69 sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 3 ], diff_Q14[ 3 ] );
70 sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 4 ], diff_Q14[ 4 ] );
71 sum2_Q16 = silk_LSHIFT( sum2_Q16, 1 );
72 sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 0 ], diff_Q14[ 0 ] );
73 sum1_Q14 = silk_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14[ 0 ] );
74
75 /* second row of W_Q18 */
76 sum2_Q16 = silk_SMULWB( W_Q18[ 7 ], diff_Q14[ 2 ] );
77 sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 8 ], diff_Q14[ 3 ] );
78 sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 9 ], diff_Q14[ 4 ] );
79 sum2_Q16 = silk_LSHIFT( sum2_Q16, 1 );
80 sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 6 ], diff_Q14[ 1 ] );
81 sum1_Q14 = silk_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14[ 1 ] );
82
83 /* third row of W_Q18 */
84 sum2_Q16 = silk_SMULWB( W_Q18[ 13 ], diff_Q14[ 3 ] );
85 sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 14 ], diff_Q14[ 4 ] );
86 sum2_Q16 = silk_LSHIFT( sum2_Q16, 1 );
87 sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 12 ], diff_Q14[ 2 ] );
88 sum1_Q14 = silk_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14[ 2 ] );
89
90 /* fourth row of W_Q18 */
91 sum2_Q16 = silk_SMULWB( W_Q18[ 19 ], diff_Q14[ 4 ] );
92 sum2_Q16 = silk_LSHIFT( sum2_Q16, 1 );
93 sum2_Q16 = silk_SMLAWB( sum2_Q16, W_Q18[ 18 ], diff_Q14[ 3 ] );
94 sum1_Q14 = silk_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14[ 3 ] );
95
96 /* last row of W_Q18 */
97 sum2_Q16 = silk_SMULWB( W_Q18[ 24 ], diff_Q14[ 4 ] );
98 sum1_Q14 = silk_SMLAWB( sum1_Q14, sum2_Q16, diff_Q14[ 4 ] );
99
100 silk_assert( sum1_Q14 >= 0 );
101
102 /* find best */
103 if( sum1_Q14 < *rate_dist_Q14 ) {
104 *rate_dist_Q14 = sum1_Q14;
105 *ind = (opus_int8)k;
106 }
107
108 /* Go to next cbk vector */
109 cb_row_Q7 += LTP_ORDER;
110 }
111}