annotate src/opus-1.3/silk/gain_quant.c @ 83:ae30d91d2ffe

Replace these with versions built using an older toolset (so as to avoid ABI compatibilities when linking on Ubuntu 14.04 for packaging purposes)
author Chris Cannam
date Fri, 07 Feb 2020 11:51:13 +0000
parents 7aeed7906520
children
rev   line source
Chris@69 1 /***********************************************************************
Chris@69 2 Copyright (c) 2006-2011, Skype Limited. All rights reserved.
Chris@69 3 Redistribution and use in source and binary forms, with or without
Chris@69 4 modification, are permitted provided that the following conditions
Chris@69 5 are met:
Chris@69 6 - Redistributions of source code must retain the above copyright notice,
Chris@69 7 this list of conditions and the following disclaimer.
Chris@69 8 - Redistributions in binary form must reproduce the above copyright
Chris@69 9 notice, this list of conditions and the following disclaimer in the
Chris@69 10 documentation and/or other materials provided with the distribution.
Chris@69 11 - Neither the name of Internet Society, IETF or IETF Trust, nor the
Chris@69 12 names of specific contributors, may be used to endorse or promote
Chris@69 13 products derived from this software without specific prior written
Chris@69 14 permission.
Chris@69 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Chris@69 16 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Chris@69 17 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Chris@69 18 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Chris@69 19 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Chris@69 20 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Chris@69 21 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Chris@69 22 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Chris@69 23 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Chris@69 24 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Chris@69 25 POSSIBILITY OF SUCH DAMAGE.
Chris@69 26 ***********************************************************************/
Chris@69 27
Chris@69 28 #ifdef HAVE_CONFIG_H
Chris@69 29 #include "config.h"
Chris@69 30 #endif
Chris@69 31
Chris@69 32 #include "main.h"
Chris@69 33
Chris@69 34 #define OFFSET ( ( MIN_QGAIN_DB * 128 ) / 6 + 16 * 128 )
Chris@69 35 #define SCALE_Q16 ( ( 65536 * ( N_LEVELS_QGAIN - 1 ) ) / ( ( ( MAX_QGAIN_DB - MIN_QGAIN_DB ) * 128 ) / 6 ) )
Chris@69 36 #define INV_SCALE_Q16 ( ( 65536 * ( ( ( MAX_QGAIN_DB - MIN_QGAIN_DB ) * 128 ) / 6 ) ) / ( N_LEVELS_QGAIN - 1 ) )
Chris@69 37
Chris@69 38 /* Gain scalar quantization with hysteresis, uniform on log scale */
Chris@69 39 void silk_gains_quant(
Chris@69 40 opus_int8 ind[ MAX_NB_SUBFR ], /* O gain indices */
Chris@69 41 opus_int32 gain_Q16[ MAX_NB_SUBFR ], /* I/O gains (quantized out) */
Chris@69 42 opus_int8 *prev_ind, /* I/O last index in previous frame */
Chris@69 43 const opus_int conditional, /* I first gain is delta coded if 1 */
Chris@69 44 const opus_int nb_subfr /* I number of subframes */
Chris@69 45 )
Chris@69 46 {
Chris@69 47 opus_int k, double_step_size_threshold;
Chris@69 48
Chris@69 49 for( k = 0; k < nb_subfr; k++ ) {
Chris@69 50 /* Convert to log scale, scale, floor() */
Chris@69 51 ind[ k ] = silk_SMULWB( SCALE_Q16, silk_lin2log( gain_Q16[ k ] ) - OFFSET );
Chris@69 52
Chris@69 53 /* Round towards previous quantized gain (hysteresis) */
Chris@69 54 if( ind[ k ] < *prev_ind ) {
Chris@69 55 ind[ k ]++;
Chris@69 56 }
Chris@69 57 ind[ k ] = silk_LIMIT_int( ind[ k ], 0, N_LEVELS_QGAIN - 1 );
Chris@69 58
Chris@69 59 /* Compute delta indices and limit */
Chris@69 60 if( k == 0 && conditional == 0 ) {
Chris@69 61 /* Full index */
Chris@69 62 ind[ k ] = silk_LIMIT_int( ind[ k ], *prev_ind + MIN_DELTA_GAIN_QUANT, N_LEVELS_QGAIN - 1 );
Chris@69 63 *prev_ind = ind[ k ];
Chris@69 64 } else {
Chris@69 65 /* Delta index */
Chris@69 66 ind[ k ] = ind[ k ] - *prev_ind;
Chris@69 67
Chris@69 68 /* Double the quantization step size for large gain increases, so that the max gain level can be reached */
Chris@69 69 double_step_size_threshold = 2 * MAX_DELTA_GAIN_QUANT - N_LEVELS_QGAIN + *prev_ind;
Chris@69 70 if( ind[ k ] > double_step_size_threshold ) {
Chris@69 71 ind[ k ] = double_step_size_threshold + silk_RSHIFT( ind[ k ] - double_step_size_threshold + 1, 1 );
Chris@69 72 }
Chris@69 73
Chris@69 74 ind[ k ] = silk_LIMIT_int( ind[ k ], MIN_DELTA_GAIN_QUANT, MAX_DELTA_GAIN_QUANT );
Chris@69 75
Chris@69 76 /* Accumulate deltas */
Chris@69 77 if( ind[ k ] > double_step_size_threshold ) {
Chris@69 78 *prev_ind += silk_LSHIFT( ind[ k ], 1 ) - double_step_size_threshold;
Chris@69 79 *prev_ind = silk_min_int( *prev_ind, N_LEVELS_QGAIN - 1 );
Chris@69 80 } else {
Chris@69 81 *prev_ind += ind[ k ];
Chris@69 82 }
Chris@69 83
Chris@69 84 /* Shift to make non-negative */
Chris@69 85 ind[ k ] -= MIN_DELTA_GAIN_QUANT;
Chris@69 86 }
Chris@69 87
Chris@69 88 /* Scale and convert to linear scale */
Chris@69 89 gain_Q16[ k ] = silk_log2lin( silk_min_32( silk_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */
Chris@69 90 }
Chris@69 91 }
Chris@69 92
Chris@69 93 /* Gains scalar dequantization, uniform on log scale */
Chris@69 94 void silk_gains_dequant(
Chris@69 95 opus_int32 gain_Q16[ MAX_NB_SUBFR ], /* O quantized gains */
Chris@69 96 const opus_int8 ind[ MAX_NB_SUBFR ], /* I gain indices */
Chris@69 97 opus_int8 *prev_ind, /* I/O last index in previous frame */
Chris@69 98 const opus_int conditional, /* I first gain is delta coded if 1 */
Chris@69 99 const opus_int nb_subfr /* I number of subframes */
Chris@69 100 )
Chris@69 101 {
Chris@69 102 opus_int k, ind_tmp, double_step_size_threshold;
Chris@69 103
Chris@69 104 for( k = 0; k < nb_subfr; k++ ) {
Chris@69 105 if( k == 0 && conditional == 0 ) {
Chris@69 106 /* Gain index is not allowed to go down more than 16 steps (~21.8 dB) */
Chris@69 107 *prev_ind = silk_max_int( ind[ k ], *prev_ind - 16 );
Chris@69 108 } else {
Chris@69 109 /* Delta index */
Chris@69 110 ind_tmp = ind[ k ] + MIN_DELTA_GAIN_QUANT;
Chris@69 111
Chris@69 112 /* Accumulate deltas */
Chris@69 113 double_step_size_threshold = 2 * MAX_DELTA_GAIN_QUANT - N_LEVELS_QGAIN + *prev_ind;
Chris@69 114 if( ind_tmp > double_step_size_threshold ) {
Chris@69 115 *prev_ind += silk_LSHIFT( ind_tmp, 1 ) - double_step_size_threshold;
Chris@69 116 } else {
Chris@69 117 *prev_ind += ind_tmp;
Chris@69 118 }
Chris@69 119 }
Chris@69 120 *prev_ind = silk_LIMIT_int( *prev_ind, 0, N_LEVELS_QGAIN - 1 );
Chris@69 121
Chris@69 122 /* Scale and convert to linear scale */
Chris@69 123 gain_Q16[ k ] = silk_log2lin( silk_min_32( silk_SMULWB( INV_SCALE_Q16, *prev_ind ) + OFFSET, 3967 ) ); /* 3967 = 31 in Q7 */
Chris@69 124 }
Chris@69 125 }
Chris@69 126
Chris@69 127 /* Compute unique identifier of gain indices vector */
Chris@69 128 opus_int32 silk_gains_ID( /* O returns unique identifier of gains */
Chris@69 129 const opus_int8 ind[ MAX_NB_SUBFR ], /* I gain indices */
Chris@69 130 const opus_int nb_subfr /* I number of subframes */
Chris@69 131 )
Chris@69 132 {
Chris@69 133 opus_int k;
Chris@69 134 opus_int32 gainsID;
Chris@69 135
Chris@69 136 gainsID = 0;
Chris@69 137 for( k = 0; k < nb_subfr; k++ ) {
Chris@69 138 gainsID = silk_ADD_LSHIFT32( ind[ k ], gainsID, 8 );
Chris@69 139 }
Chris@69 140
Chris@69 141 return gainsID;
Chris@69 142 }