media/libopus/silk/quant_LTP_gains.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libopus/silk/quant_LTP_gains.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,128 @@
     1.4 +/***********************************************************************
     1.5 +Copyright (c) 2006-2011, Skype Limited. All rights reserved.
     1.6 +Redistribution and use in source and binary forms, with or without
     1.7 +modification, are permitted provided that the following conditions
     1.8 +are met:
     1.9 +- Redistributions of source code must retain the above copyright notice,
    1.10 +this list of conditions and the following disclaimer.
    1.11 +- Redistributions in binary form must reproduce the above copyright
    1.12 +notice, this list of conditions and the following disclaimer in the
    1.13 +documentation and/or other materials provided with the distribution.
    1.14 +- Neither the name of Internet Society, IETF or IETF Trust, nor the
    1.15 +names of specific contributors, may be used to endorse or promote
    1.16 +products derived from this software without specific prior written
    1.17 +permission.
    1.18 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.19 +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.20 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.21 +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
    1.22 +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.23 +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.24 +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.25 +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.26 +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.27 +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.28 +POSSIBILITY OF SUCH DAMAGE.
    1.29 +***********************************************************************/
    1.30 +
    1.31 +#ifdef HAVE_CONFIG_H
    1.32 +#include "config.h"
    1.33 +#endif
    1.34 +
    1.35 +#include "main.h"
    1.36 +#include "tuning_parameters.h"
    1.37 +
    1.38 +void silk_quant_LTP_gains(
    1.39 +    opus_int16                  B_Q14[ MAX_NB_SUBFR * LTP_ORDER ],          /* I/O  (un)quantized LTP gains         */
    1.40 +    opus_int8                   cbk_index[ MAX_NB_SUBFR ],                  /* O    Codebook Index                  */
    1.41 +    opus_int8                   *periodicity_index,                         /* O    Periodicity Index               */
    1.42 +	opus_int32					*sum_log_gain_Q7,							/* I/O  Cumulative max prediction gain  */
    1.43 +    const opus_int32            W_Q18[ MAX_NB_SUBFR*LTP_ORDER*LTP_ORDER ],  /* I    Error Weights in Q18            */
    1.44 +    opus_int                    mu_Q9,                                      /* I    Mu value (R/D tradeoff)         */
    1.45 +    opus_int                    lowComplexity,                              /* I    Flag for low complexity         */
    1.46 +    const opus_int              nb_subfr                                    /* I    number of subframes             */
    1.47 +)
    1.48 +{
    1.49 +    opus_int             j, k, cbk_size;
    1.50 +    opus_int8            temp_idx[ MAX_NB_SUBFR ];
    1.51 +    const opus_uint8     *cl_ptr_Q5;
    1.52 +    const opus_int8      *cbk_ptr_Q7;
    1.53 +    const opus_uint8     *cbk_gain_ptr_Q7;
    1.54 +    const opus_int16     *b_Q14_ptr;
    1.55 +    const opus_int32     *W_Q18_ptr;
    1.56 +    opus_int32           rate_dist_Q14_subfr, rate_dist_Q14, min_rate_dist_Q14;
    1.57 +	opus_int32           sum_log_gain_tmp_Q7, best_sum_log_gain_Q7, max_gain_Q7, gain_Q7;
    1.58 +
    1.59 +    /***************************************************/
    1.60 +    /* iterate over different codebooks with different */
    1.61 +    /* rates/distortions, and choose best */
    1.62 +    /***************************************************/
    1.63 +    min_rate_dist_Q14 = silk_int32_MAX;
    1.64 +    best_sum_log_gain_Q7 = 0;
    1.65 +    for( k = 0; k < 3; k++ ) {
    1.66 +        /* Safety margin for pitch gain control, to take into account factors
    1.67 +           such as state rescaling/rewhitening. */
    1.68 +        opus_int32 gain_safety = SILK_FIX_CONST( 0.4, 7 );
    1.69 +
    1.70 +        cl_ptr_Q5  = silk_LTP_gain_BITS_Q5_ptrs[ k ];
    1.71 +        cbk_ptr_Q7 = silk_LTP_vq_ptrs_Q7[        k ];
    1.72 +        cbk_gain_ptr_Q7 = silk_LTP_vq_gain_ptrs_Q7[ k ];
    1.73 +        cbk_size   = silk_LTP_vq_sizes[          k ];
    1.74 +
    1.75 +        /* Set up pointer to first subframe */
    1.76 +        W_Q18_ptr = W_Q18;
    1.77 +        b_Q14_ptr = B_Q14;
    1.78 +
    1.79 +        rate_dist_Q14 = 0;
    1.80 +		sum_log_gain_tmp_Q7 = *sum_log_gain_Q7;
    1.81 +        for( j = 0; j < nb_subfr; j++ ) {
    1.82 +			max_gain_Q7 = silk_log2lin( ( SILK_FIX_CONST( MAX_SUM_LOG_GAIN_DB / 6.0, 7 ) - sum_log_gain_tmp_Q7 ) 
    1.83 +										+ SILK_FIX_CONST( 7, 7 ) ) - gain_safety;
    1.84 +
    1.85 +            silk_VQ_WMat_EC(
    1.86 +                &temp_idx[ j ],         /* O    index of best codebook vector                           */
    1.87 +                &rate_dist_Q14_subfr,   /* O    best weighted quantization error + mu * rate            */
    1.88 +				&gain_Q7,               /* O    sum of absolute LTP coefficients                        */
    1.89 +                b_Q14_ptr,              /* I    input vector to be quantized                            */
    1.90 +                W_Q18_ptr,              /* I    weighting matrix                                        */
    1.91 +                cbk_ptr_Q7,             /* I    codebook                                                */
    1.92 +                cbk_gain_ptr_Q7,        /* I    codebook effective gains                                */
    1.93 +                cl_ptr_Q5,              /* I    code length for each codebook vector                    */
    1.94 +                mu_Q9,                  /* I    tradeoff between weighted error and rate                */
    1.95 +				max_gain_Q7,            /* I    maximum sum of absolute LTP coefficients                */
    1.96 +                cbk_size                /* I    number of vectors in codebook                           */
    1.97 +            );
    1.98 +
    1.99 +            rate_dist_Q14 = silk_ADD_POS_SAT32( rate_dist_Q14, rate_dist_Q14_subfr );
   1.100 +            sum_log_gain_tmp_Q7 = silk_max(0, sum_log_gain_tmp_Q7
   1.101 +                                + silk_lin2log( gain_safety + gain_Q7 ) - SILK_FIX_CONST( 7, 7 ));
   1.102 +
   1.103 +            b_Q14_ptr += LTP_ORDER;
   1.104 +            W_Q18_ptr += LTP_ORDER * LTP_ORDER;
   1.105 +        }
   1.106 +
   1.107 +        /* Avoid never finding a codebook */
   1.108 +        rate_dist_Q14 = silk_min( silk_int32_MAX - 1, rate_dist_Q14 );
   1.109 +
   1.110 +        if( rate_dist_Q14 < min_rate_dist_Q14 ) {
   1.111 +            min_rate_dist_Q14 = rate_dist_Q14;
   1.112 +            *periodicity_index = (opus_int8)k;
   1.113 +            silk_memcpy( cbk_index, temp_idx, nb_subfr * sizeof( opus_int8 ) );
   1.114 +			best_sum_log_gain_Q7 = sum_log_gain_tmp_Q7;
   1.115 +        }
   1.116 +
   1.117 +        /* Break early in low-complexity mode if rate distortion is below threshold */
   1.118 +        if( lowComplexity && ( rate_dist_Q14 < silk_LTP_gain_middle_avg_RD_Q14 ) ) {
   1.119 +            break;
   1.120 +        }
   1.121 +    }
   1.122 +
   1.123 +    cbk_ptr_Q7 = silk_LTP_vq_ptrs_Q7[ *periodicity_index ];
   1.124 +    for( j = 0; j < nb_subfr; j++ ) {
   1.125 +        for( k = 0; k < LTP_ORDER; k++ ) {
   1.126 +            B_Q14[ j * LTP_ORDER + k ] = silk_LSHIFT( cbk_ptr_Q7[ cbk_index[ j ] * LTP_ORDER + k ], 7 );
   1.127 +        }
   1.128 +    }
   1.129 +	*sum_log_gain_Q7 = best_sum_log_gain_Q7;
   1.130 +}
   1.131 +

mercurial