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