1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libopus/silk/gain_quant.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,141 @@ 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 +#define OFFSET ( ( MIN_QGAIN_DB * 128 ) / 6 + 16 * 128 ) 1.38 +#define SCALE_Q16 ( ( 65536 * ( N_LEVELS_QGAIN - 1 ) ) / ( ( ( MAX_QGAIN_DB - MIN_QGAIN_DB ) * 128 ) / 6 ) ) 1.39 +#define INV_SCALE_Q16 ( ( 65536 * ( ( ( MAX_QGAIN_DB - MIN_QGAIN_DB ) * 128 ) / 6 ) ) / ( N_LEVELS_QGAIN - 1 ) ) 1.40 + 1.41 +/* Gain scalar quantization with hysteresis, uniform on log scale */ 1.42 +void silk_gains_quant( 1.43 + opus_int8 ind[ MAX_NB_SUBFR ], /* O gain indices */ 1.44 + opus_int32 gain_Q16[ MAX_NB_SUBFR ], /* I/O gains (quantized out) */ 1.45 + opus_int8 *prev_ind, /* I/O last index in previous frame */ 1.46 + const opus_int conditional, /* I first gain is delta coded if 1 */ 1.47 + const opus_int nb_subfr /* I number of subframes */ 1.48 +) 1.49 +{ 1.50 + opus_int k, double_step_size_threshold; 1.51 + 1.52 + for( k = 0; k < nb_subfr; k++ ) { 1.53 + /* Convert to log scale, scale, floor() */ 1.54 + ind[ k ] = silk_SMULWB( SCALE_Q16, silk_lin2log( gain_Q16[ k ] ) - OFFSET ); 1.55 + 1.56 + /* Round towards previous quantized gain (hysteresis) */ 1.57 + if( ind[ k ] < *prev_ind ) { 1.58 + ind[ k ]++; 1.59 + } 1.60 + ind[ k ] = silk_LIMIT_int( ind[ k ], 0, N_LEVELS_QGAIN - 1 ); 1.61 + 1.62 + /* Compute delta indices and limit */ 1.63 + if( k == 0 && conditional == 0 ) { 1.64 + /* Full index */ 1.65 + ind[ k ] = silk_LIMIT_int( ind[ k ], *prev_ind + MIN_DELTA_GAIN_QUANT, N_LEVELS_QGAIN - 1 ); 1.66 + *prev_ind = ind[ k ]; 1.67 + } else { 1.68 + /* Delta index */ 1.69 + ind[ k ] = ind[ k ] - *prev_ind; 1.70 + 1.71 + /* Double the quantization step size for large gain increases, so that the max gain level can be reached */ 1.72 + double_step_size_threshold = 2 * MAX_DELTA_GAIN_QUANT - N_LEVELS_QGAIN + *prev_ind; 1.73 + if( ind[ k ] > double_step_size_threshold ) { 1.74 + ind[ k ] = double_step_size_threshold + silk_RSHIFT( ind[ k ] - double_step_size_threshold + 1, 1 ); 1.75 + } 1.76 + 1.77 + ind[ k ] = silk_LIMIT_int( ind[ k ], MIN_DELTA_GAIN_QUANT, MAX_DELTA_GAIN_QUANT ); 1.78 + 1.79 + /* Accumulate deltas */ 1.80 + if( ind[ k ] > double_step_size_threshold ) { 1.81 + *prev_ind += silk_LSHIFT( ind[ k ], 1 ) - double_step_size_threshold; 1.82 + } else { 1.83 + *prev_ind += ind[ k ]; 1.84 + } 1.85 + 1.86 + /* Shift to make non-negative */ 1.87 + ind[ k ] -= MIN_DELTA_GAIN_QUANT; 1.88 + } 1.89 + 1.90 + /* Scale and convert to linear scale */ 1.91 + gain_Q16[ k ] = silk_log2lin( silk_min_32( silk_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */ 1.92 + } 1.93 +} 1.94 + 1.95 +/* Gains scalar dequantization, uniform on log scale */ 1.96 +void silk_gains_dequant( 1.97 + opus_int32 gain_Q16[ MAX_NB_SUBFR ], /* O quantized gains */ 1.98 + const opus_int8 ind[ MAX_NB_SUBFR ], /* I gain indices */ 1.99 + opus_int8 *prev_ind, /* I/O last index in previous frame */ 1.100 + const opus_int conditional, /* I first gain is delta coded if 1 */ 1.101 + const opus_int nb_subfr /* I number of subframes */ 1.102 +) 1.103 +{ 1.104 + opus_int k, ind_tmp, double_step_size_threshold; 1.105 + 1.106 + for( k = 0; k < nb_subfr; k++ ) { 1.107 + if( k == 0 && conditional == 0 ) { 1.108 + /* Gain index is not allowed to go down more than 16 steps (~21.8 dB) */ 1.109 + *prev_ind = silk_max_int( ind[ k ], *prev_ind - 16 ); 1.110 + } else { 1.111 + /* Delta index */ 1.112 + ind_tmp = ind[ k ] + MIN_DELTA_GAIN_QUANT; 1.113 + 1.114 + /* Accumulate deltas */ 1.115 + double_step_size_threshold = 2 * MAX_DELTA_GAIN_QUANT - N_LEVELS_QGAIN + *prev_ind; 1.116 + if( ind_tmp > double_step_size_threshold ) { 1.117 + *prev_ind += silk_LSHIFT( ind_tmp, 1 ) - double_step_size_threshold; 1.118 + } else { 1.119 + *prev_ind += ind_tmp; 1.120 + } 1.121 + } 1.122 + *prev_ind = silk_LIMIT_int( *prev_ind, 0, N_LEVELS_QGAIN - 1 ); 1.123 + 1.124 + /* Scale and convert to linear scale */ 1.125 + gain_Q16[ k ] = silk_log2lin( silk_min_32( silk_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */ 1.126 + } 1.127 +} 1.128 + 1.129 +/* Compute unique identifier of gain indices vector */ 1.130 +opus_int32 silk_gains_ID( /* O returns unique identifier of gains */ 1.131 + const opus_int8 ind[ MAX_NB_SUBFR ], /* I gain indices */ 1.132 + const opus_int nb_subfr /* I number of subframes */ 1.133 +) 1.134 +{ 1.135 + opus_int k; 1.136 + opus_int32 gainsID; 1.137 + 1.138 + gainsID = 0; 1.139 + for( k = 0; k < nb_subfr; k++ ) { 1.140 + gainsID = silk_ADD_LSHIFT32( ind[ k ], gainsID, 8 ); 1.141 + } 1.142 + 1.143 + return gainsID; 1.144 +}