media/libopus/silk/encode_indices.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libopus/silk/encode_indices.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,181 @@
     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 +
    1.37 +/* Encode side-information parameters to payload */
    1.38 +void silk_encode_indices(
    1.39 +    silk_encoder_state          *psEncC,                        /* I/O  Encoder state                               */
    1.40 +    ec_enc                      *psRangeEnc,                    /* I/O  Compressor data structure                   */
    1.41 +    opus_int                    FrameIndex,                     /* I    Frame number                                */
    1.42 +    opus_int                    encode_LBRR,                    /* I    Flag indicating LBRR data is being encoded  */
    1.43 +    opus_int                    condCoding                      /* I    The type of conditional coding to use       */
    1.44 +)
    1.45 +{
    1.46 +    opus_int   i, k, typeOffset;
    1.47 +    opus_int   encode_absolute_lagIndex, delta_lagIndex;
    1.48 +    opus_int16 ec_ix[ MAX_LPC_ORDER ];
    1.49 +    opus_uint8 pred_Q8[ MAX_LPC_ORDER ];
    1.50 +    const SideInfoIndices *psIndices;
    1.51 +
    1.52 +    if( encode_LBRR ) {
    1.53 +         psIndices = &psEncC->indices_LBRR[ FrameIndex ];
    1.54 +    } else {
    1.55 +         psIndices = &psEncC->indices;
    1.56 +    }
    1.57 +
    1.58 +    /*******************************************/
    1.59 +    /* Encode signal type and quantizer offset */
    1.60 +    /*******************************************/
    1.61 +    typeOffset = 2 * psIndices->signalType + psIndices->quantOffsetType;
    1.62 +    silk_assert( typeOffset >= 0 && typeOffset < 6 );
    1.63 +    silk_assert( encode_LBRR == 0 || typeOffset >= 2 );
    1.64 +    if( encode_LBRR || typeOffset >= 2 ) {
    1.65 +        ec_enc_icdf( psRangeEnc, typeOffset - 2, silk_type_offset_VAD_iCDF, 8 );
    1.66 +    } else {
    1.67 +        ec_enc_icdf( psRangeEnc, typeOffset, silk_type_offset_no_VAD_iCDF, 8 );
    1.68 +    }
    1.69 +
    1.70 +    /****************/
    1.71 +    /* Encode gains */
    1.72 +    /****************/
    1.73 +    /* first subframe */
    1.74 +    if( condCoding == CODE_CONDITIONALLY ) {
    1.75 +        /* conditional coding */
    1.76 +        silk_assert( psIndices->GainsIndices[ 0 ] >= 0 && psIndices->GainsIndices[ 0 ] < MAX_DELTA_GAIN_QUANT - MIN_DELTA_GAIN_QUANT + 1 );
    1.77 +        ec_enc_icdf( psRangeEnc, psIndices->GainsIndices[ 0 ], silk_delta_gain_iCDF, 8 );
    1.78 +    } else {
    1.79 +        /* independent coding, in two stages: MSB bits followed by 3 LSBs */
    1.80 +        silk_assert( psIndices->GainsIndices[ 0 ] >= 0 && psIndices->GainsIndices[ 0 ] < N_LEVELS_QGAIN );
    1.81 +        ec_enc_icdf( psRangeEnc, silk_RSHIFT( psIndices->GainsIndices[ 0 ], 3 ), silk_gain_iCDF[ psIndices->signalType ], 8 );
    1.82 +        ec_enc_icdf( psRangeEnc, psIndices->GainsIndices[ 0 ] & 7, silk_uniform8_iCDF, 8 );
    1.83 +    }
    1.84 +
    1.85 +    /* remaining subframes */
    1.86 +    for( i = 1; i < psEncC->nb_subfr; i++ ) {
    1.87 +        silk_assert( psIndices->GainsIndices[ i ] >= 0 && psIndices->GainsIndices[ i ] < MAX_DELTA_GAIN_QUANT - MIN_DELTA_GAIN_QUANT + 1 );
    1.88 +        ec_enc_icdf( psRangeEnc, psIndices->GainsIndices[ i ], silk_delta_gain_iCDF, 8 );
    1.89 +    }
    1.90 +
    1.91 +    /****************/
    1.92 +    /* Encode NLSFs */
    1.93 +    /****************/
    1.94 +    ec_enc_icdf( psRangeEnc, psIndices->NLSFIndices[ 0 ], &psEncC->psNLSF_CB->CB1_iCDF[ ( psIndices->signalType >> 1 ) * psEncC->psNLSF_CB->nVectors ], 8 );
    1.95 +    silk_NLSF_unpack( ec_ix, pred_Q8, psEncC->psNLSF_CB, psIndices->NLSFIndices[ 0 ] );
    1.96 +    silk_assert( psEncC->psNLSF_CB->order == psEncC->predictLPCOrder );
    1.97 +    for( i = 0; i < psEncC->psNLSF_CB->order; i++ ) {
    1.98 +        if( psIndices->NLSFIndices[ i+1 ] >= NLSF_QUANT_MAX_AMPLITUDE ) {
    1.99 +            ec_enc_icdf( psRangeEnc, 2 * NLSF_QUANT_MAX_AMPLITUDE, &psEncC->psNLSF_CB->ec_iCDF[ ec_ix[ i ] ], 8 );
   1.100 +            ec_enc_icdf( psRangeEnc, psIndices->NLSFIndices[ i+1 ] - NLSF_QUANT_MAX_AMPLITUDE, silk_NLSF_EXT_iCDF, 8 );
   1.101 +        } else if( psIndices->NLSFIndices[ i+1 ] <= -NLSF_QUANT_MAX_AMPLITUDE ) {
   1.102 +            ec_enc_icdf( psRangeEnc, 0, &psEncC->psNLSF_CB->ec_iCDF[ ec_ix[ i ] ], 8 );
   1.103 +            ec_enc_icdf( psRangeEnc, -psIndices->NLSFIndices[ i+1 ] - NLSF_QUANT_MAX_AMPLITUDE, silk_NLSF_EXT_iCDF, 8 );
   1.104 +        } else {
   1.105 +            ec_enc_icdf( psRangeEnc, psIndices->NLSFIndices[ i+1 ] + NLSF_QUANT_MAX_AMPLITUDE, &psEncC->psNLSF_CB->ec_iCDF[ ec_ix[ i ] ], 8 );
   1.106 +        }
   1.107 +    }
   1.108 +
   1.109 +    /* Encode NLSF interpolation factor */
   1.110 +    if( psEncC->nb_subfr == MAX_NB_SUBFR ) {
   1.111 +        silk_assert( psIndices->NLSFInterpCoef_Q2 >= 0 && psIndices->NLSFInterpCoef_Q2 < 5 );
   1.112 +        ec_enc_icdf( psRangeEnc, psIndices->NLSFInterpCoef_Q2, silk_NLSF_interpolation_factor_iCDF, 8 );
   1.113 +    }
   1.114 +
   1.115 +    if( psIndices->signalType == TYPE_VOICED )
   1.116 +    {
   1.117 +        /*********************/
   1.118 +        /* Encode pitch lags */
   1.119 +        /*********************/
   1.120 +        /* lag index */
   1.121 +        encode_absolute_lagIndex = 1;
   1.122 +        if( condCoding == CODE_CONDITIONALLY && psEncC->ec_prevSignalType == TYPE_VOICED ) {
   1.123 +            /* Delta Encoding */
   1.124 +            delta_lagIndex = psIndices->lagIndex - psEncC->ec_prevLagIndex;
   1.125 +            if( delta_lagIndex < -8 || delta_lagIndex > 11 ) {
   1.126 +                delta_lagIndex = 0;
   1.127 +            } else {
   1.128 +                delta_lagIndex = delta_lagIndex + 9;
   1.129 +                encode_absolute_lagIndex = 0; /* Only use delta */
   1.130 +            }
   1.131 +            silk_assert( delta_lagIndex >= 0 && delta_lagIndex < 21 );
   1.132 +            ec_enc_icdf( psRangeEnc, delta_lagIndex, silk_pitch_delta_iCDF, 8 );
   1.133 +        }
   1.134 +        if( encode_absolute_lagIndex ) {
   1.135 +            /* Absolute encoding */
   1.136 +            opus_int32 pitch_high_bits, pitch_low_bits;
   1.137 +            pitch_high_bits = silk_DIV32_16( psIndices->lagIndex, silk_RSHIFT( psEncC->fs_kHz, 1 ) );
   1.138 +            pitch_low_bits = psIndices->lagIndex - silk_SMULBB( pitch_high_bits, silk_RSHIFT( psEncC->fs_kHz, 1 ) );
   1.139 +            silk_assert( pitch_low_bits < psEncC->fs_kHz / 2 );
   1.140 +            silk_assert( pitch_high_bits < 32 );
   1.141 +            ec_enc_icdf( psRangeEnc, pitch_high_bits, silk_pitch_lag_iCDF, 8 );
   1.142 +            ec_enc_icdf( psRangeEnc, pitch_low_bits, psEncC->pitch_lag_low_bits_iCDF, 8 );
   1.143 +        }
   1.144 +        psEncC->ec_prevLagIndex = psIndices->lagIndex;
   1.145 +
   1.146 +        /* Countour index */
   1.147 +        silk_assert(   psIndices->contourIndex  >= 0 );
   1.148 +        silk_assert( ( psIndices->contourIndex < 34 && psEncC->fs_kHz  > 8 && psEncC->nb_subfr == 4 ) ||
   1.149 +                    ( psIndices->contourIndex < 11 && psEncC->fs_kHz == 8 && psEncC->nb_subfr == 4 ) ||
   1.150 +                    ( psIndices->contourIndex < 12 && psEncC->fs_kHz  > 8 && psEncC->nb_subfr == 2 ) ||
   1.151 +                    ( psIndices->contourIndex <  3 && psEncC->fs_kHz == 8 && psEncC->nb_subfr == 2 ) );
   1.152 +        ec_enc_icdf( psRangeEnc, psIndices->contourIndex, psEncC->pitch_contour_iCDF, 8 );
   1.153 +
   1.154 +        /********************/
   1.155 +        /* Encode LTP gains */
   1.156 +        /********************/
   1.157 +        /* PERIndex value */
   1.158 +        silk_assert( psIndices->PERIndex >= 0 && psIndices->PERIndex < 3 );
   1.159 +        ec_enc_icdf( psRangeEnc, psIndices->PERIndex, silk_LTP_per_index_iCDF, 8 );
   1.160 +
   1.161 +        /* Codebook Indices */
   1.162 +        for( k = 0; k < psEncC->nb_subfr; k++ ) {
   1.163 +            silk_assert( psIndices->LTPIndex[ k ] >= 0 && psIndices->LTPIndex[ k ] < ( 8 << psIndices->PERIndex ) );
   1.164 +            ec_enc_icdf( psRangeEnc, psIndices->LTPIndex[ k ], silk_LTP_gain_iCDF_ptrs[ psIndices->PERIndex ], 8 );
   1.165 +        }
   1.166 +
   1.167 +        /**********************/
   1.168 +        /* Encode LTP scaling */
   1.169 +        /**********************/
   1.170 +        if( condCoding == CODE_INDEPENDENTLY ) {
   1.171 +            silk_assert( psIndices->LTP_scaleIndex >= 0 && psIndices->LTP_scaleIndex < 3 );
   1.172 +            ec_enc_icdf( psRangeEnc, psIndices->LTP_scaleIndex, silk_LTPscale_iCDF, 8 );
   1.173 +        }
   1.174 +        silk_assert( !condCoding || psIndices->LTP_scaleIndex == 0 );
   1.175 +    }
   1.176 +
   1.177 +    psEncC->ec_prevSignalType = psIndices->signalType;
   1.178 +
   1.179 +    /***************/
   1.180 +    /* Encode seed */
   1.181 +    /***************/
   1.182 +    silk_assert( psIndices->Seed >= 0 && psIndices->Seed < 4 );
   1.183 +    ec_enc_icdf( psRangeEnc, psIndices->Seed, silk_uniform4_iCDF, 8 );
   1.184 +}

mercurial