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