Chris@69: /*********************************************************************** Chris@69: Copyright (c) 2006-2011, Skype Limited. All rights reserved. Chris@69: Redistribution and use in source and binary forms, with or without Chris@69: modification, are permitted provided that the following conditions Chris@69: are met: Chris@69: - Redistributions of source code must retain the above copyright notice, Chris@69: this list of conditions and the following disclaimer. Chris@69: - Redistributions in binary form must reproduce the above copyright Chris@69: notice, this list of conditions and the following disclaimer in the Chris@69: documentation and/or other materials provided with the distribution. Chris@69: - Neither the name of Internet Society, IETF or IETF Trust, nor the Chris@69: names of specific contributors, may be used to endorse or promote Chris@69: products derived from this software without specific prior written Chris@69: permission. Chris@69: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" Chris@69: AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE Chris@69: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE Chris@69: ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE Chris@69: LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR Chris@69: CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF Chris@69: SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS Chris@69: INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN Chris@69: CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) Chris@69: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE Chris@69: POSSIBILITY OF SUCH DAMAGE. Chris@69: ***********************************************************************/ Chris@69: Chris@69: #ifdef HAVE_CONFIG_H Chris@69: #include "config.h" Chris@69: #endif Chris@69: Chris@69: #include "main.h" Chris@69: Chris@69: #define OFFSET ( ( MIN_QGAIN_DB * 128 ) / 6 + 16 * 128 ) Chris@69: #define SCALE_Q16 ( ( 65536 * ( N_LEVELS_QGAIN - 1 ) ) / ( ( ( MAX_QGAIN_DB - MIN_QGAIN_DB ) * 128 ) / 6 ) ) Chris@69: #define INV_SCALE_Q16 ( ( 65536 * ( ( ( MAX_QGAIN_DB - MIN_QGAIN_DB ) * 128 ) / 6 ) ) / ( N_LEVELS_QGAIN - 1 ) ) Chris@69: Chris@69: /* Gain scalar quantization with hysteresis, uniform on log scale */ Chris@69: void silk_gains_quant( Chris@69: opus_int8 ind[ MAX_NB_SUBFR ], /* O gain indices */ Chris@69: opus_int32 gain_Q16[ MAX_NB_SUBFR ], /* I/O gains (quantized out) */ Chris@69: opus_int8 *prev_ind, /* I/O last index in previous frame */ Chris@69: const opus_int conditional, /* I first gain is delta coded if 1 */ Chris@69: const opus_int nb_subfr /* I number of subframes */ Chris@69: ) Chris@69: { Chris@69: opus_int k, double_step_size_threshold; Chris@69: Chris@69: for( k = 0; k < nb_subfr; k++ ) { Chris@69: /* Convert to log scale, scale, floor() */ Chris@69: ind[ k ] = silk_SMULWB( SCALE_Q16, silk_lin2log( gain_Q16[ k ] ) - OFFSET ); Chris@69: Chris@69: /* Round towards previous quantized gain (hysteresis) */ Chris@69: if( ind[ k ] < *prev_ind ) { Chris@69: ind[ k ]++; Chris@69: } Chris@69: ind[ k ] = silk_LIMIT_int( ind[ k ], 0, N_LEVELS_QGAIN - 1 ); Chris@69: Chris@69: /* Compute delta indices and limit */ Chris@69: if( k == 0 && conditional == 0 ) { Chris@69: /* Full index */ Chris@69: ind[ k ] = silk_LIMIT_int( ind[ k ], *prev_ind + MIN_DELTA_GAIN_QUANT, N_LEVELS_QGAIN - 1 ); Chris@69: *prev_ind = ind[ k ]; Chris@69: } else { Chris@69: /* Delta index */ Chris@69: ind[ k ] = ind[ k ] - *prev_ind; Chris@69: Chris@69: /* Double the quantization step size for large gain increases, so that the max gain level can be reached */ Chris@69: double_step_size_threshold = 2 * MAX_DELTA_GAIN_QUANT - N_LEVELS_QGAIN + *prev_ind; Chris@69: if( ind[ k ] > double_step_size_threshold ) { Chris@69: ind[ k ] = double_step_size_threshold + silk_RSHIFT( ind[ k ] - double_step_size_threshold + 1, 1 ); Chris@69: } Chris@69: Chris@69: ind[ k ] = silk_LIMIT_int( ind[ k ], MIN_DELTA_GAIN_QUANT, MAX_DELTA_GAIN_QUANT ); Chris@69: Chris@69: /* Accumulate deltas */ Chris@69: if( ind[ k ] > double_step_size_threshold ) { Chris@69: *prev_ind += silk_LSHIFT( ind[ k ], 1 ) - double_step_size_threshold; Chris@69: *prev_ind = silk_min_int( *prev_ind, N_LEVELS_QGAIN - 1 ); Chris@69: } else { Chris@69: *prev_ind += ind[ k ]; Chris@69: } Chris@69: Chris@69: /* Shift to make non-negative */ Chris@69: ind[ k ] -= MIN_DELTA_GAIN_QUANT; Chris@69: } Chris@69: Chris@69: /* Scale and convert to linear scale */ Chris@69: gain_Q16[ k ] = silk_log2lin( silk_min_32( silk_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */ Chris@69: } Chris@69: } Chris@69: Chris@69: /* Gains scalar dequantization, uniform on log scale */ Chris@69: void silk_gains_dequant( Chris@69: opus_int32 gain_Q16[ MAX_NB_SUBFR ], /* O quantized gains */ Chris@69: const opus_int8 ind[ MAX_NB_SUBFR ], /* I gain indices */ Chris@69: opus_int8 *prev_ind, /* I/O last index in previous frame */ Chris@69: const opus_int conditional, /* I first gain is delta coded if 1 */ Chris@69: const opus_int nb_subfr /* I number of subframes */ Chris@69: ) Chris@69: { Chris@69: opus_int k, ind_tmp, double_step_size_threshold; Chris@69: Chris@69: for( k = 0; k < nb_subfr; k++ ) { Chris@69: if( k == 0 && conditional == 0 ) { Chris@69: /* Gain index is not allowed to go down more than 16 steps (~21.8 dB) */ Chris@69: *prev_ind = silk_max_int( ind[ k ], *prev_ind - 16 ); Chris@69: } else { Chris@69: /* Delta index */ Chris@69: ind_tmp = ind[ k ] + MIN_DELTA_GAIN_QUANT; Chris@69: Chris@69: /* Accumulate deltas */ Chris@69: double_step_size_threshold = 2 * MAX_DELTA_GAIN_QUANT - N_LEVELS_QGAIN + *prev_ind; Chris@69: if( ind_tmp > double_step_size_threshold ) { Chris@69: *prev_ind += silk_LSHIFT( ind_tmp, 1 ) - double_step_size_threshold; Chris@69: } else { Chris@69: *prev_ind += ind_tmp; Chris@69: } Chris@69: } Chris@69: *prev_ind = silk_LIMIT_int( *prev_ind, 0, N_LEVELS_QGAIN - 1 ); Chris@69: Chris@69: /* Scale and convert to linear scale */ Chris@69: gain_Q16[ k ] = silk_log2lin( silk_min_32( silk_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */ Chris@69: } Chris@69: } Chris@69: Chris@69: /* Compute unique identifier of gain indices vector */ Chris@69: opus_int32 silk_gains_ID( /* O returns unique identifier of gains */ Chris@69: const opus_int8 ind[ MAX_NB_SUBFR ], /* I gain indices */ Chris@69: const opus_int nb_subfr /* I number of subframes */ Chris@69: ) Chris@69: { Chris@69: opus_int k; Chris@69: opus_int32 gainsID; Chris@69: Chris@69: gainsID = 0; Chris@69: for( k = 0; k < nb_subfr; k++ ) { Chris@69: gainsID = silk_ADD_LSHIFT32( ind[ k ], gainsID, 8 ); Chris@69: } Chris@69: Chris@69: return gainsID; Chris@69: }