media/libopus/silk/NLSF_encode.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libopus/silk/NLSF_encode.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,136 @@
     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 "stack_alloc.h"
    1.37 +
    1.38 +/***********************/
    1.39 +/* NLSF vector encoder */
    1.40 +/***********************/
    1.41 +opus_int32 silk_NLSF_encode(                                    /* O    Returns RD value in Q25                     */
    1.42 +          opus_int8             *NLSFIndices,                   /* I    Codebook path vector [ LPC_ORDER + 1 ]      */
    1.43 +          opus_int16            *pNLSF_Q15,                     /* I/O  Quantized NLSF vector [ LPC_ORDER ]         */
    1.44 +    const silk_NLSF_CB_struct   *psNLSF_CB,                     /* I    Codebook object                             */
    1.45 +    const opus_int16            *pW_QW,                         /* I    NLSF weight vector [ LPC_ORDER ]            */
    1.46 +    const opus_int              NLSF_mu_Q20,                    /* I    Rate weight for the RD optimization         */
    1.47 +    const opus_int              nSurvivors,                     /* I    Max survivors after first stage             */
    1.48 +    const opus_int              signalType                      /* I    Signal type: 0/1/2                          */
    1.49 +)
    1.50 +{
    1.51 +    opus_int         i, s, ind1, bestIndex, prob_Q8, bits_q7;
    1.52 +    opus_int32       W_tmp_Q9;
    1.53 +    VARDECL( opus_int32, err_Q26 );
    1.54 +    VARDECL( opus_int32, RD_Q25 );
    1.55 +    VARDECL( opus_int, tempIndices1 );
    1.56 +    VARDECL( opus_int8, tempIndices2 );
    1.57 +    opus_int16       res_Q15[      MAX_LPC_ORDER ];
    1.58 +    opus_int16       res_Q10[      MAX_LPC_ORDER ];
    1.59 +    opus_int16       NLSF_tmp_Q15[ MAX_LPC_ORDER ];
    1.60 +    opus_int16       W_tmp_QW[     MAX_LPC_ORDER ];
    1.61 +    opus_int16       W_adj_Q5[     MAX_LPC_ORDER ];
    1.62 +    opus_uint8       pred_Q8[      MAX_LPC_ORDER ];
    1.63 +    opus_int16       ec_ix[        MAX_LPC_ORDER ];
    1.64 +    const opus_uint8 *pCB_element, *iCDF_ptr;
    1.65 +    SAVE_STACK;
    1.66 +
    1.67 +    silk_assert( nSurvivors <= NLSF_VQ_MAX_SURVIVORS );
    1.68 +    silk_assert( signalType >= 0 && signalType <= 2 );
    1.69 +    silk_assert( NLSF_mu_Q20 <= 32767 && NLSF_mu_Q20 >= 0 );
    1.70 +
    1.71 +    /* NLSF stabilization */
    1.72 +    silk_NLSF_stabilize( pNLSF_Q15, psNLSF_CB->deltaMin_Q15, psNLSF_CB->order );
    1.73 +
    1.74 +    /* First stage: VQ */
    1.75 +    ALLOC( err_Q26, psNLSF_CB->nVectors, opus_int32 );
    1.76 +    silk_NLSF_VQ( err_Q26, pNLSF_Q15, psNLSF_CB->CB1_NLSF_Q8, psNLSF_CB->nVectors, psNLSF_CB->order );
    1.77 +
    1.78 +    /* Sort the quantization errors */
    1.79 +    ALLOC( tempIndices1, nSurvivors, opus_int );
    1.80 +    silk_insertion_sort_increasing( err_Q26, tempIndices1, psNLSF_CB->nVectors, nSurvivors );
    1.81 +
    1.82 +    ALLOC( RD_Q25, nSurvivors, opus_int32 );
    1.83 +    ALLOC( tempIndices2, nSurvivors * MAX_LPC_ORDER, opus_int8 );
    1.84 +
    1.85 +    /* Loop over survivors */
    1.86 +    for( s = 0; s < nSurvivors; s++ ) {
    1.87 +        ind1 = tempIndices1[ s ];
    1.88 +
    1.89 +        /* Residual after first stage */
    1.90 +        pCB_element = &psNLSF_CB->CB1_NLSF_Q8[ ind1 * psNLSF_CB->order ];
    1.91 +        for( i = 0; i < psNLSF_CB->order; i++ ) {
    1.92 +            NLSF_tmp_Q15[ i ] = silk_LSHIFT16( (opus_int16)pCB_element[ i ], 7 );
    1.93 +            res_Q15[ i ] = pNLSF_Q15[ i ] - NLSF_tmp_Q15[ i ];
    1.94 +        }
    1.95 +
    1.96 +        /* Weights from codebook vector */
    1.97 +        silk_NLSF_VQ_weights_laroia( W_tmp_QW, NLSF_tmp_Q15, psNLSF_CB->order );
    1.98 +
    1.99 +        /* Apply square-rooted weights */
   1.100 +        for( i = 0; i < psNLSF_CB->order; i++ ) {
   1.101 +            W_tmp_Q9 = silk_SQRT_APPROX( silk_LSHIFT( (opus_int32)W_tmp_QW[ i ], 18 - NLSF_W_Q ) );
   1.102 +            res_Q10[ i ] = (opus_int16)silk_RSHIFT( silk_SMULBB( res_Q15[ i ], W_tmp_Q9 ), 14 );
   1.103 +        }
   1.104 +
   1.105 +        /* Modify input weights accordingly */
   1.106 +        for( i = 0; i < psNLSF_CB->order; i++ ) {
   1.107 +            W_adj_Q5[ i ] = silk_DIV32_16( silk_LSHIFT( (opus_int32)pW_QW[ i ], 5 ), W_tmp_QW[ i ] );
   1.108 +        }
   1.109 +
   1.110 +        /* Unpack entropy table indices and predictor for current CB1 index */
   1.111 +        silk_NLSF_unpack( ec_ix, pred_Q8, psNLSF_CB, ind1 );
   1.112 +
   1.113 +        /* Trellis quantizer */
   1.114 +        RD_Q25[ s ] = silk_NLSF_del_dec_quant( &tempIndices2[ s * MAX_LPC_ORDER ], res_Q10, W_adj_Q5, pred_Q8, ec_ix,
   1.115 +            psNLSF_CB->ec_Rates_Q5, psNLSF_CB->quantStepSize_Q16, psNLSF_CB->invQuantStepSize_Q6, NLSF_mu_Q20, psNLSF_CB->order );
   1.116 +
   1.117 +        /* Add rate for first stage */
   1.118 +        iCDF_ptr = &psNLSF_CB->CB1_iCDF[ ( signalType >> 1 ) * psNLSF_CB->nVectors ];
   1.119 +        if( ind1 == 0 ) {
   1.120 +            prob_Q8 = 256 - iCDF_ptr[ ind1 ];
   1.121 +        } else {
   1.122 +            prob_Q8 = iCDF_ptr[ ind1 - 1 ] - iCDF_ptr[ ind1 ];
   1.123 +        }
   1.124 +        bits_q7 = ( 8 << 7 ) - silk_lin2log( prob_Q8 );
   1.125 +        RD_Q25[ s ] = silk_SMLABB( RD_Q25[ s ], bits_q7, silk_RSHIFT( NLSF_mu_Q20, 2 ) );
   1.126 +    }
   1.127 +
   1.128 +    /* Find the lowest rate-distortion error */
   1.129 +    silk_insertion_sort_increasing( RD_Q25, &bestIndex, nSurvivors, 1 );
   1.130 +
   1.131 +    NLSFIndices[ 0 ] = (opus_int8)tempIndices1[ bestIndex ];
   1.132 +    silk_memcpy( &NLSFIndices[ 1 ], &tempIndices2[ bestIndex * MAX_LPC_ORDER ], psNLSF_CB->order * sizeof( opus_int8 ) );
   1.133 +
   1.134 +    /* Decode */
   1.135 +    silk_NLSF_decode( pNLSF_Q15, NLSFIndices, psNLSF_CB );
   1.136 +
   1.137 +    RESTORE_STACK;
   1.138 +    return RD_Q25[ 0 ];
   1.139 +}

mercurial