Thu, 15 Jan 2015 15:59:08 +0100
Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | /* Copyright (c) 2007-2008 CSIRO |
michael@0 | 2 | Copyright (c) 2007-2009 Xiph.Org Foundation |
michael@0 | 3 | Written by Jean-Marc Valin */ |
michael@0 | 4 | /* |
michael@0 | 5 | Redistribution and use in source and binary forms, with or without |
michael@0 | 6 | modification, are permitted provided that the following conditions |
michael@0 | 7 | are met: |
michael@0 | 8 | |
michael@0 | 9 | - Redistributions of source code must retain the above copyright |
michael@0 | 10 | notice, this list of conditions and the following disclaimer. |
michael@0 | 11 | |
michael@0 | 12 | - Redistributions in binary form must reproduce the above copyright |
michael@0 | 13 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 14 | documentation and/or other materials provided with the distribution. |
michael@0 | 15 | |
michael@0 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 17 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 18 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 19 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
michael@0 | 20 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 21 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 22 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 23 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
michael@0 | 24 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
michael@0 | 25 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@0 | 26 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 27 | */ |
michael@0 | 28 | |
michael@0 | 29 | #ifdef HAVE_CONFIG_H |
michael@0 | 30 | #include "config.h" |
michael@0 | 31 | #endif |
michael@0 | 32 | |
michael@0 | 33 | #include "quant_bands.h" |
michael@0 | 34 | #include "laplace.h" |
michael@0 | 35 | #include <math.h> |
michael@0 | 36 | #include "os_support.h" |
michael@0 | 37 | #include "arch.h" |
michael@0 | 38 | #include "mathops.h" |
michael@0 | 39 | #include "stack_alloc.h" |
michael@0 | 40 | #include "rate.h" |
michael@0 | 41 | |
michael@0 | 42 | #ifdef FIXED_POINT |
michael@0 | 43 | /* Mean energy in each band quantized in Q4 */ |
michael@0 | 44 | const signed char eMeans[25] = { |
michael@0 | 45 | 103,100, 92, 85, 81, |
michael@0 | 46 | 77, 72, 70, 78, 75, |
michael@0 | 47 | 73, 71, 78, 74, 69, |
michael@0 | 48 | 72, 70, 74, 76, 71, |
michael@0 | 49 | 60, 60, 60, 60, 60 |
michael@0 | 50 | }; |
michael@0 | 51 | #else |
michael@0 | 52 | /* Mean energy in each band quantized in Q4 and converted back to float */ |
michael@0 | 53 | const opus_val16 eMeans[25] = { |
michael@0 | 54 | 6.437500f, 6.250000f, 5.750000f, 5.312500f, 5.062500f, |
michael@0 | 55 | 4.812500f, 4.500000f, 4.375000f, 4.875000f, 4.687500f, |
michael@0 | 56 | 4.562500f, 4.437500f, 4.875000f, 4.625000f, 4.312500f, |
michael@0 | 57 | 4.500000f, 4.375000f, 4.625000f, 4.750000f, 4.437500f, |
michael@0 | 58 | 3.750000f, 3.750000f, 3.750000f, 3.750000f, 3.750000f |
michael@0 | 59 | }; |
michael@0 | 60 | #endif |
michael@0 | 61 | /* prediction coefficients: 0.9, 0.8, 0.65, 0.5 */ |
michael@0 | 62 | #ifdef FIXED_POINT |
michael@0 | 63 | static const opus_val16 pred_coef[4] = {29440, 26112, 21248, 16384}; |
michael@0 | 64 | static const opus_val16 beta_coef[4] = {30147, 22282, 12124, 6554}; |
michael@0 | 65 | static const opus_val16 beta_intra = 4915; |
michael@0 | 66 | #else |
michael@0 | 67 | static const opus_val16 pred_coef[4] = {29440/32768., 26112/32768., 21248/32768., 16384/32768.}; |
michael@0 | 68 | static const opus_val16 beta_coef[4] = {30147/32768., 22282/32768., 12124/32768., 6554/32768.}; |
michael@0 | 69 | static const opus_val16 beta_intra = 4915/32768.; |
michael@0 | 70 | #endif |
michael@0 | 71 | |
michael@0 | 72 | /*Parameters of the Laplace-like probability models used for the coarse energy. |
michael@0 | 73 | There is one pair of parameters for each frame size, prediction type |
michael@0 | 74 | (inter/intra), and band number. |
michael@0 | 75 | The first number of each pair is the probability of 0, and the second is the |
michael@0 | 76 | decay rate, both in Q8 precision.*/ |
michael@0 | 77 | static const unsigned char e_prob_model[4][2][42] = { |
michael@0 | 78 | /*120 sample frames.*/ |
michael@0 | 79 | { |
michael@0 | 80 | /*Inter*/ |
michael@0 | 81 | { |
michael@0 | 82 | 72, 127, 65, 129, 66, 128, 65, 128, 64, 128, 62, 128, 64, 128, |
michael@0 | 83 | 64, 128, 92, 78, 92, 79, 92, 78, 90, 79, 116, 41, 115, 40, |
michael@0 | 84 | 114, 40, 132, 26, 132, 26, 145, 17, 161, 12, 176, 10, 177, 11 |
michael@0 | 85 | }, |
michael@0 | 86 | /*Intra*/ |
michael@0 | 87 | { |
michael@0 | 88 | 24, 179, 48, 138, 54, 135, 54, 132, 53, 134, 56, 133, 55, 132, |
michael@0 | 89 | 55, 132, 61, 114, 70, 96, 74, 88, 75, 88, 87, 74, 89, 66, |
michael@0 | 90 | 91, 67, 100, 59, 108, 50, 120, 40, 122, 37, 97, 43, 78, 50 |
michael@0 | 91 | } |
michael@0 | 92 | }, |
michael@0 | 93 | /*240 sample frames.*/ |
michael@0 | 94 | { |
michael@0 | 95 | /*Inter*/ |
michael@0 | 96 | { |
michael@0 | 97 | 83, 78, 84, 81, 88, 75, 86, 74, 87, 71, 90, 73, 93, 74, |
michael@0 | 98 | 93, 74, 109, 40, 114, 36, 117, 34, 117, 34, 143, 17, 145, 18, |
michael@0 | 99 | 146, 19, 162, 12, 165, 10, 178, 7, 189, 6, 190, 8, 177, 9 |
michael@0 | 100 | }, |
michael@0 | 101 | /*Intra*/ |
michael@0 | 102 | { |
michael@0 | 103 | 23, 178, 54, 115, 63, 102, 66, 98, 69, 99, 74, 89, 71, 91, |
michael@0 | 104 | 73, 91, 78, 89, 86, 80, 92, 66, 93, 64, 102, 59, 103, 60, |
michael@0 | 105 | 104, 60, 117, 52, 123, 44, 138, 35, 133, 31, 97, 38, 77, 45 |
michael@0 | 106 | } |
michael@0 | 107 | }, |
michael@0 | 108 | /*480 sample frames.*/ |
michael@0 | 109 | { |
michael@0 | 110 | /*Inter*/ |
michael@0 | 111 | { |
michael@0 | 112 | 61, 90, 93, 60, 105, 42, 107, 41, 110, 45, 116, 38, 113, 38, |
michael@0 | 113 | 112, 38, 124, 26, 132, 27, 136, 19, 140, 20, 155, 14, 159, 16, |
michael@0 | 114 | 158, 18, 170, 13, 177, 10, 187, 8, 192, 6, 175, 9, 159, 10 |
michael@0 | 115 | }, |
michael@0 | 116 | /*Intra*/ |
michael@0 | 117 | { |
michael@0 | 118 | 21, 178, 59, 110, 71, 86, 75, 85, 84, 83, 91, 66, 88, 73, |
michael@0 | 119 | 87, 72, 92, 75, 98, 72, 105, 58, 107, 54, 115, 52, 114, 55, |
michael@0 | 120 | 112, 56, 129, 51, 132, 40, 150, 33, 140, 29, 98, 35, 77, 42 |
michael@0 | 121 | } |
michael@0 | 122 | }, |
michael@0 | 123 | /*960 sample frames.*/ |
michael@0 | 124 | { |
michael@0 | 125 | /*Inter*/ |
michael@0 | 126 | { |
michael@0 | 127 | 42, 121, 96, 66, 108, 43, 111, 40, 117, 44, 123, 32, 120, 36, |
michael@0 | 128 | 119, 33, 127, 33, 134, 34, 139, 21, 147, 23, 152, 20, 158, 25, |
michael@0 | 129 | 154, 26, 166, 21, 173, 16, 184, 13, 184, 10, 150, 13, 139, 15 |
michael@0 | 130 | }, |
michael@0 | 131 | /*Intra*/ |
michael@0 | 132 | { |
michael@0 | 133 | 22, 178, 63, 114, 74, 82, 84, 83, 92, 82, 103, 62, 96, 72, |
michael@0 | 134 | 96, 67, 101, 73, 107, 72, 113, 55, 118, 52, 125, 52, 118, 52, |
michael@0 | 135 | 117, 55, 135, 49, 137, 39, 157, 32, 145, 29, 97, 33, 77, 40 |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | }; |
michael@0 | 139 | |
michael@0 | 140 | static const unsigned char small_energy_icdf[3]={2,1,0}; |
michael@0 | 141 | |
michael@0 | 142 | static opus_val32 loss_distortion(const opus_val16 *eBands, opus_val16 *oldEBands, int start, int end, int len, int C) |
michael@0 | 143 | { |
michael@0 | 144 | int c, i; |
michael@0 | 145 | opus_val32 dist = 0; |
michael@0 | 146 | c=0; do { |
michael@0 | 147 | for (i=start;i<end;i++) |
michael@0 | 148 | { |
michael@0 | 149 | opus_val16 d = SUB16(SHR16(eBands[i+c*len], 3), SHR16(oldEBands[i+c*len], 3)); |
michael@0 | 150 | dist = MAC16_16(dist, d,d); |
michael@0 | 151 | } |
michael@0 | 152 | } while (++c<C); |
michael@0 | 153 | return MIN32(200,SHR32(dist,2*DB_SHIFT-6)); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | static int quant_coarse_energy_impl(const CELTMode *m, int start, int end, |
michael@0 | 157 | const opus_val16 *eBands, opus_val16 *oldEBands, |
michael@0 | 158 | opus_int32 budget, opus_int32 tell, |
michael@0 | 159 | const unsigned char *prob_model, opus_val16 *error, ec_enc *enc, |
michael@0 | 160 | int C, int LM, int intra, opus_val16 max_decay, int lfe) |
michael@0 | 161 | { |
michael@0 | 162 | int i, c; |
michael@0 | 163 | int badness = 0; |
michael@0 | 164 | opus_val32 prev[2] = {0,0}; |
michael@0 | 165 | opus_val16 coef; |
michael@0 | 166 | opus_val16 beta; |
michael@0 | 167 | |
michael@0 | 168 | if (tell+3 <= budget) |
michael@0 | 169 | ec_enc_bit_logp(enc, intra, 3); |
michael@0 | 170 | if (intra) |
michael@0 | 171 | { |
michael@0 | 172 | coef = 0; |
michael@0 | 173 | beta = beta_intra; |
michael@0 | 174 | } else { |
michael@0 | 175 | beta = beta_coef[LM]; |
michael@0 | 176 | coef = pred_coef[LM]; |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | /* Encode at a fixed coarse resolution */ |
michael@0 | 180 | for (i=start;i<end;i++) |
michael@0 | 181 | { |
michael@0 | 182 | c=0; |
michael@0 | 183 | do { |
michael@0 | 184 | int bits_left; |
michael@0 | 185 | int qi, qi0; |
michael@0 | 186 | opus_val32 q; |
michael@0 | 187 | opus_val16 x; |
michael@0 | 188 | opus_val32 f, tmp; |
michael@0 | 189 | opus_val16 oldE; |
michael@0 | 190 | opus_val16 decay_bound; |
michael@0 | 191 | x = eBands[i+c*m->nbEBands]; |
michael@0 | 192 | oldE = MAX16(-QCONST16(9.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]); |
michael@0 | 193 | #ifdef FIXED_POINT |
michael@0 | 194 | f = SHL32(EXTEND32(x),7) - PSHR32(MULT16_16(coef,oldE), 8) - prev[c]; |
michael@0 | 195 | /* Rounding to nearest integer here is really important! */ |
michael@0 | 196 | qi = (f+QCONST32(.5f,DB_SHIFT+7))>>(DB_SHIFT+7); |
michael@0 | 197 | decay_bound = EXTRACT16(MAX32(-QCONST16(28.f,DB_SHIFT), |
michael@0 | 198 | SUB32((opus_val32)oldEBands[i+c*m->nbEBands],max_decay))); |
michael@0 | 199 | #else |
michael@0 | 200 | f = x-coef*oldE-prev[c]; |
michael@0 | 201 | /* Rounding to nearest integer here is really important! */ |
michael@0 | 202 | qi = (int)floor(.5f+f); |
michael@0 | 203 | decay_bound = MAX16(-QCONST16(28.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]) - max_decay; |
michael@0 | 204 | #endif |
michael@0 | 205 | /* Prevent the energy from going down too quickly (e.g. for bands |
michael@0 | 206 | that have just one bin) */ |
michael@0 | 207 | if (qi < 0 && x < decay_bound) |
michael@0 | 208 | { |
michael@0 | 209 | qi += (int)SHR16(SUB16(decay_bound,x), DB_SHIFT); |
michael@0 | 210 | if (qi > 0) |
michael@0 | 211 | qi = 0; |
michael@0 | 212 | } |
michael@0 | 213 | qi0 = qi; |
michael@0 | 214 | /* If we don't have enough bits to encode all the energy, just assume |
michael@0 | 215 | something safe. */ |
michael@0 | 216 | tell = ec_tell(enc); |
michael@0 | 217 | bits_left = budget-tell-3*C*(end-i); |
michael@0 | 218 | if (i!=start && bits_left < 30) |
michael@0 | 219 | { |
michael@0 | 220 | if (bits_left < 24) |
michael@0 | 221 | qi = IMIN(1, qi); |
michael@0 | 222 | if (bits_left < 16) |
michael@0 | 223 | qi = IMAX(-1, qi); |
michael@0 | 224 | } |
michael@0 | 225 | if (lfe && i>=2) |
michael@0 | 226 | qi = IMIN(qi, 0); |
michael@0 | 227 | if (budget-tell >= 15) |
michael@0 | 228 | { |
michael@0 | 229 | int pi; |
michael@0 | 230 | pi = 2*IMIN(i,20); |
michael@0 | 231 | ec_laplace_encode(enc, &qi, |
michael@0 | 232 | prob_model[pi]<<7, prob_model[pi+1]<<6); |
michael@0 | 233 | } |
michael@0 | 234 | else if(budget-tell >= 2) |
michael@0 | 235 | { |
michael@0 | 236 | qi = IMAX(-1, IMIN(qi, 1)); |
michael@0 | 237 | ec_enc_icdf(enc, 2*qi^-(qi<0), small_energy_icdf, 2); |
michael@0 | 238 | } |
michael@0 | 239 | else if(budget-tell >= 1) |
michael@0 | 240 | { |
michael@0 | 241 | qi = IMIN(0, qi); |
michael@0 | 242 | ec_enc_bit_logp(enc, -qi, 1); |
michael@0 | 243 | } |
michael@0 | 244 | else |
michael@0 | 245 | qi = -1; |
michael@0 | 246 | error[i+c*m->nbEBands] = PSHR32(f,7) - SHL16(qi,DB_SHIFT); |
michael@0 | 247 | badness += abs(qi0-qi); |
michael@0 | 248 | q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT); |
michael@0 | 249 | |
michael@0 | 250 | tmp = PSHR32(MULT16_16(coef,oldE),8) + prev[c] + SHL32(q,7); |
michael@0 | 251 | #ifdef FIXED_POINT |
michael@0 | 252 | tmp = MAX32(-QCONST32(28.f, DB_SHIFT+7), tmp); |
michael@0 | 253 | #endif |
michael@0 | 254 | oldEBands[i+c*m->nbEBands] = PSHR32(tmp, 7); |
michael@0 | 255 | prev[c] = prev[c] + SHL32(q,7) - MULT16_16(beta,PSHR32(q,8)); |
michael@0 | 256 | } while (++c < C); |
michael@0 | 257 | } |
michael@0 | 258 | return lfe ? 0 : badness; |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | void quant_coarse_energy(const CELTMode *m, int start, int end, int effEnd, |
michael@0 | 262 | const opus_val16 *eBands, opus_val16 *oldEBands, opus_uint32 budget, |
michael@0 | 263 | opus_val16 *error, ec_enc *enc, int C, int LM, int nbAvailableBytes, |
michael@0 | 264 | int force_intra, opus_val32 *delayedIntra, int two_pass, int loss_rate, int lfe) |
michael@0 | 265 | { |
michael@0 | 266 | int intra; |
michael@0 | 267 | opus_val16 max_decay; |
michael@0 | 268 | VARDECL(opus_val16, oldEBands_intra); |
michael@0 | 269 | VARDECL(opus_val16, error_intra); |
michael@0 | 270 | ec_enc enc_start_state; |
michael@0 | 271 | opus_uint32 tell; |
michael@0 | 272 | int badness1=0; |
michael@0 | 273 | opus_int32 intra_bias; |
michael@0 | 274 | opus_val32 new_distortion; |
michael@0 | 275 | SAVE_STACK; |
michael@0 | 276 | |
michael@0 | 277 | intra = force_intra || (!two_pass && *delayedIntra>2*C*(end-start) && nbAvailableBytes > (end-start)*C); |
michael@0 | 278 | intra_bias = (opus_int32)((budget**delayedIntra*loss_rate)/(C*512)); |
michael@0 | 279 | new_distortion = loss_distortion(eBands, oldEBands, start, effEnd, m->nbEBands, C); |
michael@0 | 280 | |
michael@0 | 281 | tell = ec_tell(enc); |
michael@0 | 282 | if (tell+3 > budget) |
michael@0 | 283 | two_pass = intra = 0; |
michael@0 | 284 | |
michael@0 | 285 | max_decay = QCONST16(16.f,DB_SHIFT); |
michael@0 | 286 | if (end-start>10) |
michael@0 | 287 | { |
michael@0 | 288 | #ifdef FIXED_POINT |
michael@0 | 289 | max_decay = MIN32(max_decay, SHL32(EXTEND32(nbAvailableBytes),DB_SHIFT-3)); |
michael@0 | 290 | #else |
michael@0 | 291 | max_decay = MIN32(max_decay, .125f*nbAvailableBytes); |
michael@0 | 292 | #endif |
michael@0 | 293 | } |
michael@0 | 294 | if (lfe) |
michael@0 | 295 | max_decay=3; |
michael@0 | 296 | enc_start_state = *enc; |
michael@0 | 297 | |
michael@0 | 298 | ALLOC(oldEBands_intra, C*m->nbEBands, opus_val16); |
michael@0 | 299 | ALLOC(error_intra, C*m->nbEBands, opus_val16); |
michael@0 | 300 | OPUS_COPY(oldEBands_intra, oldEBands, C*m->nbEBands); |
michael@0 | 301 | |
michael@0 | 302 | if (two_pass || intra) |
michael@0 | 303 | { |
michael@0 | 304 | badness1 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands_intra, budget, |
michael@0 | 305 | tell, e_prob_model[LM][1], error_intra, enc, C, LM, 1, max_decay, lfe); |
michael@0 | 306 | } |
michael@0 | 307 | |
michael@0 | 308 | if (!intra) |
michael@0 | 309 | { |
michael@0 | 310 | unsigned char *intra_buf; |
michael@0 | 311 | ec_enc enc_intra_state; |
michael@0 | 312 | opus_int32 tell_intra; |
michael@0 | 313 | opus_uint32 nstart_bytes; |
michael@0 | 314 | opus_uint32 nintra_bytes; |
michael@0 | 315 | opus_uint32 save_bytes; |
michael@0 | 316 | int badness2; |
michael@0 | 317 | VARDECL(unsigned char, intra_bits); |
michael@0 | 318 | |
michael@0 | 319 | tell_intra = ec_tell_frac(enc); |
michael@0 | 320 | |
michael@0 | 321 | enc_intra_state = *enc; |
michael@0 | 322 | |
michael@0 | 323 | nstart_bytes = ec_range_bytes(&enc_start_state); |
michael@0 | 324 | nintra_bytes = ec_range_bytes(&enc_intra_state); |
michael@0 | 325 | intra_buf = ec_get_buffer(&enc_intra_state) + nstart_bytes; |
michael@0 | 326 | save_bytes = nintra_bytes-nstart_bytes; |
michael@0 | 327 | if (save_bytes == 0) |
michael@0 | 328 | save_bytes = ALLOC_NONE; |
michael@0 | 329 | ALLOC(intra_bits, save_bytes, unsigned char); |
michael@0 | 330 | /* Copy bits from intra bit-stream */ |
michael@0 | 331 | OPUS_COPY(intra_bits, intra_buf, nintra_bytes - nstart_bytes); |
michael@0 | 332 | |
michael@0 | 333 | *enc = enc_start_state; |
michael@0 | 334 | |
michael@0 | 335 | badness2 = quant_coarse_energy_impl(m, start, end, eBands, oldEBands, budget, |
michael@0 | 336 | tell, e_prob_model[LM][intra], error, enc, C, LM, 0, max_decay, lfe); |
michael@0 | 337 | |
michael@0 | 338 | if (two_pass && (badness1 < badness2 || (badness1 == badness2 && ((opus_int32)ec_tell_frac(enc))+intra_bias > tell_intra))) |
michael@0 | 339 | { |
michael@0 | 340 | *enc = enc_intra_state; |
michael@0 | 341 | /* Copy intra bits to bit-stream */ |
michael@0 | 342 | OPUS_COPY(intra_buf, intra_bits, nintra_bytes - nstart_bytes); |
michael@0 | 343 | OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands); |
michael@0 | 344 | OPUS_COPY(error, error_intra, C*m->nbEBands); |
michael@0 | 345 | intra = 1; |
michael@0 | 346 | } |
michael@0 | 347 | } else { |
michael@0 | 348 | OPUS_COPY(oldEBands, oldEBands_intra, C*m->nbEBands); |
michael@0 | 349 | OPUS_COPY(error, error_intra, C*m->nbEBands); |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | if (intra) |
michael@0 | 353 | *delayedIntra = new_distortion; |
michael@0 | 354 | else |
michael@0 | 355 | *delayedIntra = ADD32(MULT16_32_Q15(MULT16_16_Q15(pred_coef[LM], pred_coef[LM]),*delayedIntra), |
michael@0 | 356 | new_distortion); |
michael@0 | 357 | |
michael@0 | 358 | RESTORE_STACK; |
michael@0 | 359 | } |
michael@0 | 360 | |
michael@0 | 361 | void quant_fine_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, opus_val16 *error, int *fine_quant, ec_enc *enc, int C) |
michael@0 | 362 | { |
michael@0 | 363 | int i, c; |
michael@0 | 364 | |
michael@0 | 365 | /* Encode finer resolution */ |
michael@0 | 366 | for (i=start;i<end;i++) |
michael@0 | 367 | { |
michael@0 | 368 | opus_int16 frac = 1<<fine_quant[i]; |
michael@0 | 369 | if (fine_quant[i] <= 0) |
michael@0 | 370 | continue; |
michael@0 | 371 | c=0; |
michael@0 | 372 | do { |
michael@0 | 373 | int q2; |
michael@0 | 374 | opus_val16 offset; |
michael@0 | 375 | #ifdef FIXED_POINT |
michael@0 | 376 | /* Has to be without rounding */ |
michael@0 | 377 | q2 = (error[i+c*m->nbEBands]+QCONST16(.5f,DB_SHIFT))>>(DB_SHIFT-fine_quant[i]); |
michael@0 | 378 | #else |
michael@0 | 379 | q2 = (int)floor((error[i+c*m->nbEBands]+.5f)*frac); |
michael@0 | 380 | #endif |
michael@0 | 381 | if (q2 > frac-1) |
michael@0 | 382 | q2 = frac-1; |
michael@0 | 383 | if (q2<0) |
michael@0 | 384 | q2 = 0; |
michael@0 | 385 | ec_enc_bits(enc, q2, fine_quant[i]); |
michael@0 | 386 | #ifdef FIXED_POINT |
michael@0 | 387 | offset = SUB16(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+QCONST16(.5f,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT)); |
michael@0 | 388 | #else |
michael@0 | 389 | offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f; |
michael@0 | 390 | #endif |
michael@0 | 391 | oldEBands[i+c*m->nbEBands] += offset; |
michael@0 | 392 | error[i+c*m->nbEBands] -= offset; |
michael@0 | 393 | /*printf ("%f ", error[i] - offset);*/ |
michael@0 | 394 | } while (++c < C); |
michael@0 | 395 | } |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | void quant_energy_finalise(const CELTMode *m, int start, int end, opus_val16 *oldEBands, opus_val16 *error, int *fine_quant, int *fine_priority, int bits_left, ec_enc *enc, int C) |
michael@0 | 399 | { |
michael@0 | 400 | int i, prio, c; |
michael@0 | 401 | |
michael@0 | 402 | /* Use up the remaining bits */ |
michael@0 | 403 | for (prio=0;prio<2;prio++) |
michael@0 | 404 | { |
michael@0 | 405 | for (i=start;i<end && bits_left>=C ;i++) |
michael@0 | 406 | { |
michael@0 | 407 | if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio) |
michael@0 | 408 | continue; |
michael@0 | 409 | c=0; |
michael@0 | 410 | do { |
michael@0 | 411 | int q2; |
michael@0 | 412 | opus_val16 offset; |
michael@0 | 413 | q2 = error[i+c*m->nbEBands]<0 ? 0 : 1; |
michael@0 | 414 | ec_enc_bits(enc, q2, 1); |
michael@0 | 415 | #ifdef FIXED_POINT |
michael@0 | 416 | offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5f,DB_SHIFT),fine_quant[i]+1); |
michael@0 | 417 | #else |
michael@0 | 418 | offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384); |
michael@0 | 419 | #endif |
michael@0 | 420 | oldEBands[i+c*m->nbEBands] += offset; |
michael@0 | 421 | bits_left--; |
michael@0 | 422 | } while (++c < C); |
michael@0 | 423 | } |
michael@0 | 424 | } |
michael@0 | 425 | } |
michael@0 | 426 | |
michael@0 | 427 | void unquant_coarse_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int intra, ec_dec *dec, int C, int LM) |
michael@0 | 428 | { |
michael@0 | 429 | const unsigned char *prob_model = e_prob_model[LM][intra]; |
michael@0 | 430 | int i, c; |
michael@0 | 431 | opus_val32 prev[2] = {0, 0}; |
michael@0 | 432 | opus_val16 coef; |
michael@0 | 433 | opus_val16 beta; |
michael@0 | 434 | opus_int32 budget; |
michael@0 | 435 | opus_int32 tell; |
michael@0 | 436 | |
michael@0 | 437 | if (intra) |
michael@0 | 438 | { |
michael@0 | 439 | coef = 0; |
michael@0 | 440 | beta = beta_intra; |
michael@0 | 441 | } else { |
michael@0 | 442 | beta = beta_coef[LM]; |
michael@0 | 443 | coef = pred_coef[LM]; |
michael@0 | 444 | } |
michael@0 | 445 | |
michael@0 | 446 | budget = dec->storage*8; |
michael@0 | 447 | |
michael@0 | 448 | /* Decode at a fixed coarse resolution */ |
michael@0 | 449 | for (i=start;i<end;i++) |
michael@0 | 450 | { |
michael@0 | 451 | c=0; |
michael@0 | 452 | do { |
michael@0 | 453 | int qi; |
michael@0 | 454 | opus_val32 q; |
michael@0 | 455 | opus_val32 tmp; |
michael@0 | 456 | /* It would be better to express this invariant as a |
michael@0 | 457 | test on C at function entry, but that isn't enough |
michael@0 | 458 | to make the static analyzer happy. */ |
michael@0 | 459 | celt_assert(c<2); |
michael@0 | 460 | tell = ec_tell(dec); |
michael@0 | 461 | if(budget-tell>=15) |
michael@0 | 462 | { |
michael@0 | 463 | int pi; |
michael@0 | 464 | pi = 2*IMIN(i,20); |
michael@0 | 465 | qi = ec_laplace_decode(dec, |
michael@0 | 466 | prob_model[pi]<<7, prob_model[pi+1]<<6); |
michael@0 | 467 | } |
michael@0 | 468 | else if(budget-tell>=2) |
michael@0 | 469 | { |
michael@0 | 470 | qi = ec_dec_icdf(dec, small_energy_icdf, 2); |
michael@0 | 471 | qi = (qi>>1)^-(qi&1); |
michael@0 | 472 | } |
michael@0 | 473 | else if(budget-tell>=1) |
michael@0 | 474 | { |
michael@0 | 475 | qi = -ec_dec_bit_logp(dec, 1); |
michael@0 | 476 | } |
michael@0 | 477 | else |
michael@0 | 478 | qi = -1; |
michael@0 | 479 | q = (opus_val32)SHL32(EXTEND32(qi),DB_SHIFT); |
michael@0 | 480 | |
michael@0 | 481 | oldEBands[i+c*m->nbEBands] = MAX16(-QCONST16(9.f,DB_SHIFT), oldEBands[i+c*m->nbEBands]); |
michael@0 | 482 | tmp = PSHR32(MULT16_16(coef,oldEBands[i+c*m->nbEBands]),8) + prev[c] + SHL32(q,7); |
michael@0 | 483 | #ifdef FIXED_POINT |
michael@0 | 484 | tmp = MAX32(-QCONST32(28.f, DB_SHIFT+7), tmp); |
michael@0 | 485 | #endif |
michael@0 | 486 | oldEBands[i+c*m->nbEBands] = PSHR32(tmp, 7); |
michael@0 | 487 | prev[c] = prev[c] + SHL32(q,7) - MULT16_16(beta,PSHR32(q,8)); |
michael@0 | 488 | } while (++c < C); |
michael@0 | 489 | } |
michael@0 | 490 | } |
michael@0 | 491 | |
michael@0 | 492 | void unquant_fine_energy(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int *fine_quant, ec_dec *dec, int C) |
michael@0 | 493 | { |
michael@0 | 494 | int i, c; |
michael@0 | 495 | /* Decode finer resolution */ |
michael@0 | 496 | for (i=start;i<end;i++) |
michael@0 | 497 | { |
michael@0 | 498 | if (fine_quant[i] <= 0) |
michael@0 | 499 | continue; |
michael@0 | 500 | c=0; |
michael@0 | 501 | do { |
michael@0 | 502 | int q2; |
michael@0 | 503 | opus_val16 offset; |
michael@0 | 504 | q2 = ec_dec_bits(dec, fine_quant[i]); |
michael@0 | 505 | #ifdef FIXED_POINT |
michael@0 | 506 | offset = SUB16(SHR32(SHL32(EXTEND32(q2),DB_SHIFT)+QCONST16(.5f,DB_SHIFT),fine_quant[i]),QCONST16(.5f,DB_SHIFT)); |
michael@0 | 507 | #else |
michael@0 | 508 | offset = (q2+.5f)*(1<<(14-fine_quant[i]))*(1.f/16384) - .5f; |
michael@0 | 509 | #endif |
michael@0 | 510 | oldEBands[i+c*m->nbEBands] += offset; |
michael@0 | 511 | } while (++c < C); |
michael@0 | 512 | } |
michael@0 | 513 | } |
michael@0 | 514 | |
michael@0 | 515 | void unquant_energy_finalise(const CELTMode *m, int start, int end, opus_val16 *oldEBands, int *fine_quant, int *fine_priority, int bits_left, ec_dec *dec, int C) |
michael@0 | 516 | { |
michael@0 | 517 | int i, prio, c; |
michael@0 | 518 | |
michael@0 | 519 | /* Use up the remaining bits */ |
michael@0 | 520 | for (prio=0;prio<2;prio++) |
michael@0 | 521 | { |
michael@0 | 522 | for (i=start;i<end && bits_left>=C ;i++) |
michael@0 | 523 | { |
michael@0 | 524 | if (fine_quant[i] >= MAX_FINE_BITS || fine_priority[i]!=prio) |
michael@0 | 525 | continue; |
michael@0 | 526 | c=0; |
michael@0 | 527 | do { |
michael@0 | 528 | int q2; |
michael@0 | 529 | opus_val16 offset; |
michael@0 | 530 | q2 = ec_dec_bits(dec, 1); |
michael@0 | 531 | #ifdef FIXED_POINT |
michael@0 | 532 | offset = SHR16(SHL16(q2,DB_SHIFT)-QCONST16(.5f,DB_SHIFT),fine_quant[i]+1); |
michael@0 | 533 | #else |
michael@0 | 534 | offset = (q2-.5f)*(1<<(14-fine_quant[i]-1))*(1.f/16384); |
michael@0 | 535 | #endif |
michael@0 | 536 | oldEBands[i+c*m->nbEBands] += offset; |
michael@0 | 537 | bits_left--; |
michael@0 | 538 | } while (++c < C); |
michael@0 | 539 | } |
michael@0 | 540 | } |
michael@0 | 541 | } |
michael@0 | 542 | |
michael@0 | 543 | void amp2Log2(const CELTMode *m, int effEnd, int end, |
michael@0 | 544 | celt_ener *bandE, opus_val16 *bandLogE, int C) |
michael@0 | 545 | { |
michael@0 | 546 | int c, i; |
michael@0 | 547 | c=0; |
michael@0 | 548 | do { |
michael@0 | 549 | for (i=0;i<effEnd;i++) |
michael@0 | 550 | bandLogE[i+c*m->nbEBands] = |
michael@0 | 551 | celt_log2(SHL32(bandE[i+c*m->nbEBands],2)) |
michael@0 | 552 | - SHL16((opus_val16)eMeans[i],6); |
michael@0 | 553 | for (i=effEnd;i<end;i++) |
michael@0 | 554 | bandLogE[c*m->nbEBands+i] = -QCONST16(14.f,DB_SHIFT); |
michael@0 | 555 | } while (++c < C); |
michael@0 | 556 | } |