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