Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Copyright (c) 2007-2008 CSIRO |
michael@0 | 2 | Copyright (c) 2007-2010 Xiph.Org Foundation |
michael@0 | 3 | Copyright (c) 2008 Gregory Maxwell |
michael@0 | 4 | Written by Jean-Marc Valin and Gregory Maxwell */ |
michael@0 | 5 | /* |
michael@0 | 6 | Redistribution and use in source and binary forms, with or without |
michael@0 | 7 | modification, are permitted provided that the following conditions |
michael@0 | 8 | are met: |
michael@0 | 9 | |
michael@0 | 10 | - Redistributions of source code must retain the above copyright |
michael@0 | 11 | notice, this list of conditions and the following disclaimer. |
michael@0 | 12 | |
michael@0 | 13 | - Redistributions in binary form must reproduce the above copyright |
michael@0 | 14 | notice, this list of conditions and the following disclaimer in the |
michael@0 | 15 | documentation and/or other materials provided with the distribution. |
michael@0 | 16 | |
michael@0 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
michael@0 | 18 | ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
michael@0 | 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
michael@0 | 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
michael@0 | 21 | OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 22 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 23 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 24 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
michael@0 | 25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
michael@0 | 26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
michael@0 | 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 28 | */ |
michael@0 | 29 | |
michael@0 | 30 | #ifdef HAVE_CONFIG_H |
michael@0 | 31 | #include "config.h" |
michael@0 | 32 | #endif |
michael@0 | 33 | |
michael@0 | 34 | #define CELT_ENCODER_C |
michael@0 | 35 | |
michael@0 | 36 | #include "cpu_support.h" |
michael@0 | 37 | #include "os_support.h" |
michael@0 | 38 | #include "mdct.h" |
michael@0 | 39 | #include <math.h> |
michael@0 | 40 | #include "celt.h" |
michael@0 | 41 | #include "pitch.h" |
michael@0 | 42 | #include "bands.h" |
michael@0 | 43 | #include "modes.h" |
michael@0 | 44 | #include "entcode.h" |
michael@0 | 45 | #include "quant_bands.h" |
michael@0 | 46 | #include "rate.h" |
michael@0 | 47 | #include "stack_alloc.h" |
michael@0 | 48 | #include "mathops.h" |
michael@0 | 49 | #include "float_cast.h" |
michael@0 | 50 | #include <stdarg.h> |
michael@0 | 51 | #include "celt_lpc.h" |
michael@0 | 52 | #include "vq.h" |
michael@0 | 53 | |
michael@0 | 54 | |
michael@0 | 55 | /** Encoder state |
michael@0 | 56 | @brief Encoder state |
michael@0 | 57 | */ |
michael@0 | 58 | struct OpusCustomEncoder { |
michael@0 | 59 | const OpusCustomMode *mode; /**< Mode used by the encoder */ |
michael@0 | 60 | int overlap; |
michael@0 | 61 | int channels; |
michael@0 | 62 | int stream_channels; |
michael@0 | 63 | |
michael@0 | 64 | int force_intra; |
michael@0 | 65 | int clip; |
michael@0 | 66 | int disable_pf; |
michael@0 | 67 | int complexity; |
michael@0 | 68 | int upsample; |
michael@0 | 69 | int start, end; |
michael@0 | 70 | |
michael@0 | 71 | opus_int32 bitrate; |
michael@0 | 72 | int vbr; |
michael@0 | 73 | int signalling; |
michael@0 | 74 | int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */ |
michael@0 | 75 | int loss_rate; |
michael@0 | 76 | int lsb_depth; |
michael@0 | 77 | int variable_duration; |
michael@0 | 78 | int lfe; |
michael@0 | 79 | int arch; |
michael@0 | 80 | |
michael@0 | 81 | /* Everything beyond this point gets cleared on a reset */ |
michael@0 | 82 | #define ENCODER_RESET_START rng |
michael@0 | 83 | |
michael@0 | 84 | opus_uint32 rng; |
michael@0 | 85 | int spread_decision; |
michael@0 | 86 | opus_val32 delayedIntra; |
michael@0 | 87 | int tonal_average; |
michael@0 | 88 | int lastCodedBands; |
michael@0 | 89 | int hf_average; |
michael@0 | 90 | int tapset_decision; |
michael@0 | 91 | |
michael@0 | 92 | int prefilter_period; |
michael@0 | 93 | opus_val16 prefilter_gain; |
michael@0 | 94 | int prefilter_tapset; |
michael@0 | 95 | #ifdef RESYNTH |
michael@0 | 96 | int prefilter_period_old; |
michael@0 | 97 | opus_val16 prefilter_gain_old; |
michael@0 | 98 | int prefilter_tapset_old; |
michael@0 | 99 | #endif |
michael@0 | 100 | int consec_transient; |
michael@0 | 101 | AnalysisInfo analysis; |
michael@0 | 102 | |
michael@0 | 103 | opus_val32 preemph_memE[2]; |
michael@0 | 104 | opus_val32 preemph_memD[2]; |
michael@0 | 105 | |
michael@0 | 106 | /* VBR-related parameters */ |
michael@0 | 107 | opus_int32 vbr_reservoir; |
michael@0 | 108 | opus_int32 vbr_drift; |
michael@0 | 109 | opus_int32 vbr_offset; |
michael@0 | 110 | opus_int32 vbr_count; |
michael@0 | 111 | opus_val32 overlap_max; |
michael@0 | 112 | opus_val16 stereo_saving; |
michael@0 | 113 | int intensity; |
michael@0 | 114 | opus_val16 *energy_mask; |
michael@0 | 115 | opus_val16 spec_avg; |
michael@0 | 116 | |
michael@0 | 117 | #ifdef RESYNTH |
michael@0 | 118 | /* +MAX_PERIOD/2 to make space for overlap */ |
michael@0 | 119 | celt_sig syn_mem[2][2*MAX_PERIOD+MAX_PERIOD/2]; |
michael@0 | 120 | #endif |
michael@0 | 121 | |
michael@0 | 122 | celt_sig in_mem[1]; /* Size = channels*mode->overlap */ |
michael@0 | 123 | /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_MAXPERIOD */ |
michael@0 | 124 | /* opus_val16 oldBandE[], Size = channels*mode->nbEBands */ |
michael@0 | 125 | /* opus_val16 oldLogE[], Size = channels*mode->nbEBands */ |
michael@0 | 126 | /* opus_val16 oldLogE2[], Size = channels*mode->nbEBands */ |
michael@0 | 127 | }; |
michael@0 | 128 | |
michael@0 | 129 | int celt_encoder_get_size(int channels) |
michael@0 | 130 | { |
michael@0 | 131 | CELTMode *mode = opus_custom_mode_create(48000, 960, NULL); |
michael@0 | 132 | return opus_custom_encoder_get_size(mode, channels); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels) |
michael@0 | 136 | { |
michael@0 | 137 | int size = sizeof(struct CELTEncoder) |
michael@0 | 138 | + (channels*mode->overlap-1)*sizeof(celt_sig) /* celt_sig in_mem[channels*mode->overlap]; */ |
michael@0 | 139 | + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig) /* celt_sig prefilter_mem[channels*COMBFILTER_MAXPERIOD]; */ |
michael@0 | 140 | + 3*channels*mode->nbEBands*sizeof(opus_val16); /* opus_val16 oldBandE[channels*mode->nbEBands]; */ |
michael@0 | 141 | /* opus_val16 oldLogE[channels*mode->nbEBands]; */ |
michael@0 | 142 | /* opus_val16 oldLogE2[channels*mode->nbEBands]; */ |
michael@0 | 143 | return size; |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | #ifdef CUSTOM_MODES |
michael@0 | 147 | CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error) |
michael@0 | 148 | { |
michael@0 | 149 | int ret; |
michael@0 | 150 | CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels)); |
michael@0 | 151 | /* init will handle the NULL case */ |
michael@0 | 152 | ret = opus_custom_encoder_init(st, mode, channels); |
michael@0 | 153 | if (ret != OPUS_OK) |
michael@0 | 154 | { |
michael@0 | 155 | opus_custom_encoder_destroy(st); |
michael@0 | 156 | st = NULL; |
michael@0 | 157 | } |
michael@0 | 158 | if (error) |
michael@0 | 159 | *error = ret; |
michael@0 | 160 | return st; |
michael@0 | 161 | } |
michael@0 | 162 | #endif /* CUSTOM_MODES */ |
michael@0 | 163 | |
michael@0 | 164 | static int opus_custom_encoder_init_arch(CELTEncoder *st, const CELTMode *mode, |
michael@0 | 165 | int channels, int arch) |
michael@0 | 166 | { |
michael@0 | 167 | if (channels < 0 || channels > 2) |
michael@0 | 168 | return OPUS_BAD_ARG; |
michael@0 | 169 | |
michael@0 | 170 | if (st==NULL || mode==NULL) |
michael@0 | 171 | return OPUS_ALLOC_FAIL; |
michael@0 | 172 | |
michael@0 | 173 | OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels)); |
michael@0 | 174 | |
michael@0 | 175 | st->mode = mode; |
michael@0 | 176 | st->overlap = mode->overlap; |
michael@0 | 177 | st->stream_channels = st->channels = channels; |
michael@0 | 178 | |
michael@0 | 179 | st->upsample = 1; |
michael@0 | 180 | st->start = 0; |
michael@0 | 181 | st->end = st->mode->effEBands; |
michael@0 | 182 | st->signalling = 1; |
michael@0 | 183 | |
michael@0 | 184 | st->arch = arch; |
michael@0 | 185 | |
michael@0 | 186 | st->constrained_vbr = 1; |
michael@0 | 187 | st->clip = 1; |
michael@0 | 188 | |
michael@0 | 189 | st->bitrate = OPUS_BITRATE_MAX; |
michael@0 | 190 | st->vbr = 0; |
michael@0 | 191 | st->force_intra = 0; |
michael@0 | 192 | st->complexity = 5; |
michael@0 | 193 | st->lsb_depth=24; |
michael@0 | 194 | |
michael@0 | 195 | opus_custom_encoder_ctl(st, OPUS_RESET_STATE); |
michael@0 | 196 | |
michael@0 | 197 | return OPUS_OK; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | #ifdef CUSTOM_MODES |
michael@0 | 201 | int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels) |
michael@0 | 202 | { |
michael@0 | 203 | return opus_custom_encoder_init_arch(st, mode, channels, opus_select_arch()); |
michael@0 | 204 | } |
michael@0 | 205 | #endif |
michael@0 | 206 | |
michael@0 | 207 | int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels, |
michael@0 | 208 | int arch) |
michael@0 | 209 | { |
michael@0 | 210 | int ret; |
michael@0 | 211 | ret = opus_custom_encoder_init_arch(st, |
michael@0 | 212 | opus_custom_mode_create(48000, 960, NULL), channels, arch); |
michael@0 | 213 | if (ret != OPUS_OK) |
michael@0 | 214 | return ret; |
michael@0 | 215 | st->upsample = resampling_factor(sampling_rate); |
michael@0 | 216 | return OPUS_OK; |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | #ifdef CUSTOM_MODES |
michael@0 | 220 | void opus_custom_encoder_destroy(CELTEncoder *st) |
michael@0 | 221 | { |
michael@0 | 222 | opus_free(st); |
michael@0 | 223 | } |
michael@0 | 224 | #endif /* CUSTOM_MODES */ |
michael@0 | 225 | |
michael@0 | 226 | |
michael@0 | 227 | static int transient_analysis(const opus_val32 * OPUS_RESTRICT in, int len, int C, |
michael@0 | 228 | opus_val16 *tf_estimate, int *tf_chan) |
michael@0 | 229 | { |
michael@0 | 230 | int i; |
michael@0 | 231 | VARDECL(opus_val16, tmp); |
michael@0 | 232 | opus_val32 mem0,mem1; |
michael@0 | 233 | int is_transient = 0; |
michael@0 | 234 | opus_int32 mask_metric = 0; |
michael@0 | 235 | int c; |
michael@0 | 236 | opus_val16 tf_max; |
michael@0 | 237 | int len2; |
michael@0 | 238 | /* Table of 6*64/x, trained on real data to minimize the average error */ |
michael@0 | 239 | static const unsigned char inv_table[128] = { |
michael@0 | 240 | 255,255,156,110, 86, 70, 59, 51, 45, 40, 37, 33, 31, 28, 26, 25, |
michael@0 | 241 | 23, 22, 21, 20, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12, |
michael@0 | 242 | 12, 12, 11, 11, 11, 10, 10, 10, 9, 9, 9, 9, 9, 9, 8, 8, |
michael@0 | 243 | 8, 8, 8, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6, |
michael@0 | 244 | 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5, |
michael@0 | 245 | 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, |
michael@0 | 246 | 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, |
michael@0 | 247 | 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2, |
michael@0 | 248 | }; |
michael@0 | 249 | SAVE_STACK; |
michael@0 | 250 | ALLOC(tmp, len, opus_val16); |
michael@0 | 251 | |
michael@0 | 252 | len2=len/2; |
michael@0 | 253 | for (c=0;c<C;c++) |
michael@0 | 254 | { |
michael@0 | 255 | opus_val32 mean; |
michael@0 | 256 | opus_int32 unmask=0; |
michael@0 | 257 | opus_val32 norm; |
michael@0 | 258 | opus_val16 maxE; |
michael@0 | 259 | mem0=0; |
michael@0 | 260 | mem1=0; |
michael@0 | 261 | /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */ |
michael@0 | 262 | for (i=0;i<len;i++) |
michael@0 | 263 | { |
michael@0 | 264 | opus_val32 x,y; |
michael@0 | 265 | x = SHR32(in[i+c*len],SIG_SHIFT); |
michael@0 | 266 | y = ADD32(mem0, x); |
michael@0 | 267 | #ifdef FIXED_POINT |
michael@0 | 268 | mem0 = mem1 + y - SHL32(x,1); |
michael@0 | 269 | mem1 = x - SHR32(y,1); |
michael@0 | 270 | #else |
michael@0 | 271 | mem0 = mem1 + y - 2*x; |
michael@0 | 272 | mem1 = x - .5f*y; |
michael@0 | 273 | #endif |
michael@0 | 274 | tmp[i] = EXTRACT16(SHR32(y,2)); |
michael@0 | 275 | /*printf("%f ", tmp[i]);*/ |
michael@0 | 276 | } |
michael@0 | 277 | /*printf("\n");*/ |
michael@0 | 278 | /* First few samples are bad because we don't propagate the memory */ |
michael@0 | 279 | for (i=0;i<12;i++) |
michael@0 | 280 | tmp[i] = 0; |
michael@0 | 281 | |
michael@0 | 282 | #ifdef FIXED_POINT |
michael@0 | 283 | /* Normalize tmp to max range */ |
michael@0 | 284 | { |
michael@0 | 285 | int shift=0; |
michael@0 | 286 | shift = 14-celt_ilog2(1+celt_maxabs16(tmp, len)); |
michael@0 | 287 | if (shift!=0) |
michael@0 | 288 | { |
michael@0 | 289 | for (i=0;i<len;i++) |
michael@0 | 290 | tmp[i] = SHL16(tmp[i], shift); |
michael@0 | 291 | } |
michael@0 | 292 | } |
michael@0 | 293 | #endif |
michael@0 | 294 | |
michael@0 | 295 | mean=0; |
michael@0 | 296 | mem0=0; |
michael@0 | 297 | /* Grouping by two to reduce complexity */ |
michael@0 | 298 | /* Forward pass to compute the post-echo threshold*/ |
michael@0 | 299 | for (i=0;i<len2;i++) |
michael@0 | 300 | { |
michael@0 | 301 | opus_val16 x2 = PSHR32(MULT16_16(tmp[2*i],tmp[2*i]) + MULT16_16(tmp[2*i+1],tmp[2*i+1]),16); |
michael@0 | 302 | mean += x2; |
michael@0 | 303 | #ifdef FIXED_POINT |
michael@0 | 304 | /* FIXME: Use PSHR16() instead */ |
michael@0 | 305 | tmp[i] = mem0 + PSHR32(x2-mem0,4); |
michael@0 | 306 | #else |
michael@0 | 307 | tmp[i] = mem0 + MULT16_16_P15(QCONST16(.0625f,15),x2-mem0); |
michael@0 | 308 | #endif |
michael@0 | 309 | mem0 = tmp[i]; |
michael@0 | 310 | } |
michael@0 | 311 | |
michael@0 | 312 | mem0=0; |
michael@0 | 313 | maxE=0; |
michael@0 | 314 | /* Backward pass to compute the pre-echo threshold */ |
michael@0 | 315 | for (i=len2-1;i>=0;i--) |
michael@0 | 316 | { |
michael@0 | 317 | #ifdef FIXED_POINT |
michael@0 | 318 | /* FIXME: Use PSHR16() instead */ |
michael@0 | 319 | tmp[i] = mem0 + PSHR32(tmp[i]-mem0,3); |
michael@0 | 320 | #else |
michael@0 | 321 | tmp[i] = mem0 + MULT16_16_P15(QCONST16(0.125f,15),tmp[i]-mem0); |
michael@0 | 322 | #endif |
michael@0 | 323 | mem0 = tmp[i]; |
michael@0 | 324 | maxE = MAX16(maxE, mem0); |
michael@0 | 325 | } |
michael@0 | 326 | /*for (i=0;i<len2;i++)printf("%f ", tmp[i]/mean);printf("\n");*/ |
michael@0 | 327 | |
michael@0 | 328 | /* Compute the ratio of the "frame energy" over the harmonic mean of the energy. |
michael@0 | 329 | This essentially corresponds to a bitrate-normalized temporal noise-to-mask |
michael@0 | 330 | ratio */ |
michael@0 | 331 | |
michael@0 | 332 | /* As a compromise with the old transient detector, frame energy is the |
michael@0 | 333 | geometric mean of the energy and half the max */ |
michael@0 | 334 | #ifdef FIXED_POINT |
michael@0 | 335 | /* Costs two sqrt() to avoid overflows */ |
michael@0 | 336 | mean = MULT16_16(celt_sqrt(mean), celt_sqrt(MULT16_16(maxE,len2>>1))); |
michael@0 | 337 | #else |
michael@0 | 338 | mean = celt_sqrt(mean * maxE*.5*len2); |
michael@0 | 339 | #endif |
michael@0 | 340 | /* Inverse of the mean energy in Q15+6 */ |
michael@0 | 341 | norm = SHL32(EXTEND32(len2),6+14)/ADD32(EPSILON,SHR32(mean,1)); |
michael@0 | 342 | /* Compute harmonic mean discarding the unreliable boundaries |
michael@0 | 343 | The data is smooth, so we only take 1/4th of the samples */ |
michael@0 | 344 | unmask=0; |
michael@0 | 345 | for (i=12;i<len2-5;i+=4) |
michael@0 | 346 | { |
michael@0 | 347 | int id; |
michael@0 | 348 | #ifdef FIXED_POINT |
michael@0 | 349 | id = IMAX(0,IMIN(127,MULT16_32_Q15(tmp[i],norm))); /* Do not round to nearest */ |
michael@0 | 350 | #else |
michael@0 | 351 | id = IMAX(0,IMIN(127,(int)floor(64*norm*tmp[i]))); /* Do not round to nearest */ |
michael@0 | 352 | #endif |
michael@0 | 353 | unmask += inv_table[id]; |
michael@0 | 354 | } |
michael@0 | 355 | /*printf("%d\n", unmask);*/ |
michael@0 | 356 | /* Normalize, compensate for the 1/4th of the sample and the factor of 6 in the inverse table */ |
michael@0 | 357 | unmask = 64*unmask*4/(6*(len2-17)); |
michael@0 | 358 | if (unmask>mask_metric) |
michael@0 | 359 | { |
michael@0 | 360 | *tf_chan = c; |
michael@0 | 361 | mask_metric = unmask; |
michael@0 | 362 | } |
michael@0 | 363 | } |
michael@0 | 364 | is_transient = mask_metric>200; |
michael@0 | 365 | |
michael@0 | 366 | /* Arbitrary metric for VBR boost */ |
michael@0 | 367 | tf_max = MAX16(0,celt_sqrt(27*mask_metric)-42); |
michael@0 | 368 | /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */ |
michael@0 | 369 | *tf_estimate = celt_sqrt(MAX16(0, SHL32(MULT16_16(QCONST16(0.0069,14),MIN16(163,tf_max)),14)-QCONST32(0.139,28))); |
michael@0 | 370 | /*printf("%d %f\n", tf_max, mask_metric);*/ |
michael@0 | 371 | RESTORE_STACK; |
michael@0 | 372 | #ifdef FUZZING |
michael@0 | 373 | is_transient = rand()&0x1; |
michael@0 | 374 | #endif |
michael@0 | 375 | /*printf("%d %f %d\n", is_transient, (float)*tf_estimate, tf_max);*/ |
michael@0 | 376 | return is_transient; |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | /* Looks for sudden increases of energy to decide whether we need to patch |
michael@0 | 380 | the transient decision */ |
michael@0 | 381 | int patch_transient_decision(opus_val16 *newE, opus_val16 *oldE, int nbEBands, |
michael@0 | 382 | int end, int C) |
michael@0 | 383 | { |
michael@0 | 384 | int i, c; |
michael@0 | 385 | opus_val32 mean_diff=0; |
michael@0 | 386 | opus_val16 spread_old[26]; |
michael@0 | 387 | /* Apply an aggressive (-6 dB/Bark) spreading function to the old frame to |
michael@0 | 388 | avoid false detection caused by irrelevant bands */ |
michael@0 | 389 | if (C==1) |
michael@0 | 390 | { |
michael@0 | 391 | spread_old[0] = oldE[0]; |
michael@0 | 392 | for (i=1;i<end;i++) |
michael@0 | 393 | spread_old[i] = MAX16(spread_old[i-1]-QCONST16(1.0f, DB_SHIFT), oldE[i]); |
michael@0 | 394 | } else { |
michael@0 | 395 | spread_old[0] = MAX16(oldE[0],oldE[nbEBands]); |
michael@0 | 396 | for (i=1;i<end;i++) |
michael@0 | 397 | spread_old[i] = MAX16(spread_old[i-1]-QCONST16(1.0f, DB_SHIFT), |
michael@0 | 398 | MAX16(oldE[i],oldE[i+nbEBands])); |
michael@0 | 399 | } |
michael@0 | 400 | for (i=end-2;i>=0;i--) |
michael@0 | 401 | spread_old[i] = MAX16(spread_old[i], spread_old[i+1]-QCONST16(1.0f, DB_SHIFT)); |
michael@0 | 402 | /* Compute mean increase */ |
michael@0 | 403 | c=0; do { |
michael@0 | 404 | for (i=2;i<end-1;i++) |
michael@0 | 405 | { |
michael@0 | 406 | opus_val16 x1, x2; |
michael@0 | 407 | x1 = MAX16(0, newE[i]); |
michael@0 | 408 | x2 = MAX16(0, spread_old[i]); |
michael@0 | 409 | mean_diff = ADD32(mean_diff, EXTEND32(MAX16(0, SUB16(x1, x2)))); |
michael@0 | 410 | } |
michael@0 | 411 | } while (++c<C); |
michael@0 | 412 | mean_diff = DIV32(mean_diff, C*(end-3)); |
michael@0 | 413 | /*printf("%f %f %d\n", mean_diff, max_diff, count);*/ |
michael@0 | 414 | return mean_diff > QCONST16(1.f, DB_SHIFT); |
michael@0 | 415 | } |
michael@0 | 416 | |
michael@0 | 417 | /** Apply window and compute the MDCT for all sub-frames and |
michael@0 | 418 | all channels in a frame */ |
michael@0 | 419 | static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS_RESTRICT in, |
michael@0 | 420 | celt_sig * OPUS_RESTRICT out, int C, int CC, int LM, int upsample) |
michael@0 | 421 | { |
michael@0 | 422 | const int overlap = OVERLAP(mode); |
michael@0 | 423 | int N; |
michael@0 | 424 | int B; |
michael@0 | 425 | int shift; |
michael@0 | 426 | int i, b, c; |
michael@0 | 427 | if (shortBlocks) |
michael@0 | 428 | { |
michael@0 | 429 | B = shortBlocks; |
michael@0 | 430 | N = mode->shortMdctSize; |
michael@0 | 431 | shift = mode->maxLM; |
michael@0 | 432 | } else { |
michael@0 | 433 | B = 1; |
michael@0 | 434 | N = mode->shortMdctSize<<LM; |
michael@0 | 435 | shift = mode->maxLM-LM; |
michael@0 | 436 | } |
michael@0 | 437 | c=0; do { |
michael@0 | 438 | for (b=0;b<B;b++) |
michael@0 | 439 | { |
michael@0 | 440 | /* Interleaving the sub-frames while doing the MDCTs */ |
michael@0 | 441 | clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N, &out[b+c*N*B], mode->window, overlap, shift, B); |
michael@0 | 442 | } |
michael@0 | 443 | } while (++c<CC); |
michael@0 | 444 | if (CC==2&&C==1) |
michael@0 | 445 | { |
michael@0 | 446 | for (i=0;i<B*N;i++) |
michael@0 | 447 | out[i] = ADD32(HALF32(out[i]), HALF32(out[B*N+i])); |
michael@0 | 448 | } |
michael@0 | 449 | if (upsample != 1) |
michael@0 | 450 | { |
michael@0 | 451 | c=0; do |
michael@0 | 452 | { |
michael@0 | 453 | int bound = B*N/upsample; |
michael@0 | 454 | for (i=0;i<bound;i++) |
michael@0 | 455 | out[c*B*N+i] *= upsample; |
michael@0 | 456 | for (;i<B*N;i++) |
michael@0 | 457 | out[c*B*N+i] = 0; |
michael@0 | 458 | } while (++c<C); |
michael@0 | 459 | } |
michael@0 | 460 | } |
michael@0 | 461 | |
michael@0 | 462 | |
michael@0 | 463 | void celt_preemphasis(const opus_val16 * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTRICT inp, |
michael@0 | 464 | int N, int CC, int upsample, const opus_val16 *coef, celt_sig *mem, int clip) |
michael@0 | 465 | { |
michael@0 | 466 | int i; |
michael@0 | 467 | opus_val16 coef0; |
michael@0 | 468 | celt_sig m; |
michael@0 | 469 | int Nu; |
michael@0 | 470 | |
michael@0 | 471 | coef0 = coef[0]; |
michael@0 | 472 | |
michael@0 | 473 | |
michael@0 | 474 | Nu = N/upsample; |
michael@0 | 475 | if (upsample!=1) |
michael@0 | 476 | { |
michael@0 | 477 | for (i=0;i<N;i++) |
michael@0 | 478 | inp[i] = 0; |
michael@0 | 479 | } |
michael@0 | 480 | for (i=0;i<Nu;i++) |
michael@0 | 481 | { |
michael@0 | 482 | celt_sig x; |
michael@0 | 483 | |
michael@0 | 484 | x = SCALEIN(pcmp[CC*i]); |
michael@0 | 485 | #ifndef FIXED_POINT |
michael@0 | 486 | /* Replace NaNs with zeros */ |
michael@0 | 487 | if (!(x==x)) |
michael@0 | 488 | x = 0; |
michael@0 | 489 | #endif |
michael@0 | 490 | inp[i*upsample] = x; |
michael@0 | 491 | } |
michael@0 | 492 | |
michael@0 | 493 | #ifndef FIXED_POINT |
michael@0 | 494 | if (clip) |
michael@0 | 495 | { |
michael@0 | 496 | /* Clip input to avoid encoding non-portable files */ |
michael@0 | 497 | for (i=0;i<Nu;i++) |
michael@0 | 498 | inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample])); |
michael@0 | 499 | } |
michael@0 | 500 | #else |
michael@0 | 501 | (void)clip; /* Avoids a warning about clip being unused. */ |
michael@0 | 502 | #endif |
michael@0 | 503 | m = *mem; |
michael@0 | 504 | #ifdef CUSTOM_MODES |
michael@0 | 505 | if (coef[1] != 0) |
michael@0 | 506 | { |
michael@0 | 507 | opus_val16 coef1 = coef[1]; |
michael@0 | 508 | opus_val16 coef2 = coef[2]; |
michael@0 | 509 | for (i=0;i<N;i++) |
michael@0 | 510 | { |
michael@0 | 511 | celt_sig x, tmp; |
michael@0 | 512 | x = inp[i]; |
michael@0 | 513 | /* Apply pre-emphasis */ |
michael@0 | 514 | tmp = MULT16_16(coef2, x); |
michael@0 | 515 | inp[i] = tmp + m; |
michael@0 | 516 | m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp); |
michael@0 | 517 | } |
michael@0 | 518 | } else |
michael@0 | 519 | #endif |
michael@0 | 520 | { |
michael@0 | 521 | for (i=0;i<N;i++) |
michael@0 | 522 | { |
michael@0 | 523 | celt_sig x; |
michael@0 | 524 | x = SHL32(inp[i], SIG_SHIFT); |
michael@0 | 525 | /* Apply pre-emphasis */ |
michael@0 | 526 | inp[i] = x + m; |
michael@0 | 527 | m = - MULT16_32_Q15(coef0, x); |
michael@0 | 528 | } |
michael@0 | 529 | } |
michael@0 | 530 | *mem = m; |
michael@0 | 531 | } |
michael@0 | 532 | |
michael@0 | 533 | |
michael@0 | 534 | |
michael@0 | 535 | static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, opus_val16 bias) |
michael@0 | 536 | { |
michael@0 | 537 | int i; |
michael@0 | 538 | opus_val32 L1; |
michael@0 | 539 | L1 = 0; |
michael@0 | 540 | for (i=0;i<N;i++) |
michael@0 | 541 | L1 += EXTEND32(ABS16(tmp[i])); |
michael@0 | 542 | /* When in doubt, prefer good freq resolution */ |
michael@0 | 543 | L1 = MAC16_32_Q15(L1, LM*bias, L1); |
michael@0 | 544 | return L1; |
michael@0 | 545 | |
michael@0 | 546 | } |
michael@0 | 547 | |
michael@0 | 548 | static int tf_analysis(const CELTMode *m, int len, int isTransient, |
michael@0 | 549 | int *tf_res, int lambda, celt_norm *X, int N0, int LM, |
michael@0 | 550 | int *tf_sum, opus_val16 tf_estimate, int tf_chan) |
michael@0 | 551 | { |
michael@0 | 552 | int i; |
michael@0 | 553 | VARDECL(int, metric); |
michael@0 | 554 | int cost0; |
michael@0 | 555 | int cost1; |
michael@0 | 556 | VARDECL(int, path0); |
michael@0 | 557 | VARDECL(int, path1); |
michael@0 | 558 | VARDECL(celt_norm, tmp); |
michael@0 | 559 | VARDECL(celt_norm, tmp_1); |
michael@0 | 560 | int sel; |
michael@0 | 561 | int selcost[2]; |
michael@0 | 562 | int tf_select=0; |
michael@0 | 563 | opus_val16 bias; |
michael@0 | 564 | |
michael@0 | 565 | SAVE_STACK; |
michael@0 | 566 | bias = MULT16_16_Q14(QCONST16(.04f,15), MAX16(-QCONST16(.25f,14), QCONST16(.5f,14)-tf_estimate)); |
michael@0 | 567 | /*printf("%f ", bias);*/ |
michael@0 | 568 | |
michael@0 | 569 | ALLOC(metric, len, int); |
michael@0 | 570 | ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm); |
michael@0 | 571 | ALLOC(tmp_1, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm); |
michael@0 | 572 | ALLOC(path0, len, int); |
michael@0 | 573 | ALLOC(path1, len, int); |
michael@0 | 574 | |
michael@0 | 575 | *tf_sum = 0; |
michael@0 | 576 | for (i=0;i<len;i++) |
michael@0 | 577 | { |
michael@0 | 578 | int j, k, N; |
michael@0 | 579 | int narrow; |
michael@0 | 580 | opus_val32 L1, best_L1; |
michael@0 | 581 | int best_level=0; |
michael@0 | 582 | N = (m->eBands[i+1]-m->eBands[i])<<LM; |
michael@0 | 583 | /* band is too narrow to be split down to LM=-1 */ |
michael@0 | 584 | narrow = (m->eBands[i+1]-m->eBands[i])==1; |
michael@0 | 585 | for (j=0;j<N;j++) |
michael@0 | 586 | tmp[j] = X[tf_chan*N0 + j+(m->eBands[i]<<LM)]; |
michael@0 | 587 | /* Just add the right channel if we're in stereo */ |
michael@0 | 588 | /*if (C==2) |
michael@0 | 589 | for (j=0;j<N;j++) |
michael@0 | 590 | tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));*/ |
michael@0 | 591 | L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias); |
michael@0 | 592 | best_L1 = L1; |
michael@0 | 593 | /* Check the -1 case for transients */ |
michael@0 | 594 | if (isTransient && !narrow) |
michael@0 | 595 | { |
michael@0 | 596 | for (j=0;j<N;j++) |
michael@0 | 597 | tmp_1[j] = tmp[j]; |
michael@0 | 598 | haar1(tmp_1, N>>LM, 1<<LM); |
michael@0 | 599 | L1 = l1_metric(tmp_1, N, LM+1, bias); |
michael@0 | 600 | if (L1<best_L1) |
michael@0 | 601 | { |
michael@0 | 602 | best_L1 = L1; |
michael@0 | 603 | best_level = -1; |
michael@0 | 604 | } |
michael@0 | 605 | } |
michael@0 | 606 | /*printf ("%f ", L1);*/ |
michael@0 | 607 | for (k=0;k<LM+!(isTransient||narrow);k++) |
michael@0 | 608 | { |
michael@0 | 609 | int B; |
michael@0 | 610 | |
michael@0 | 611 | if (isTransient) |
michael@0 | 612 | B = (LM-k-1); |
michael@0 | 613 | else |
michael@0 | 614 | B = k+1; |
michael@0 | 615 | |
michael@0 | 616 | haar1(tmp, N>>k, 1<<k); |
michael@0 | 617 | |
michael@0 | 618 | L1 = l1_metric(tmp, N, B, bias); |
michael@0 | 619 | |
michael@0 | 620 | if (L1 < best_L1) |
michael@0 | 621 | { |
michael@0 | 622 | best_L1 = L1; |
michael@0 | 623 | best_level = k+1; |
michael@0 | 624 | } |
michael@0 | 625 | } |
michael@0 | 626 | /*printf ("%d ", isTransient ? LM-best_level : best_level);*/ |
michael@0 | 627 | /* metric is in Q1 to be able to select the mid-point (-0.5) for narrower bands */ |
michael@0 | 628 | if (isTransient) |
michael@0 | 629 | metric[i] = 2*best_level; |
michael@0 | 630 | else |
michael@0 | 631 | metric[i] = -2*best_level; |
michael@0 | 632 | *tf_sum += (isTransient ? LM : 0) - metric[i]/2; |
michael@0 | 633 | /* For bands that can't be split to -1, set the metric to the half-way point to avoid |
michael@0 | 634 | biasing the decision */ |
michael@0 | 635 | if (narrow && (metric[i]==0 || metric[i]==-2*LM)) |
michael@0 | 636 | metric[i]-=1; |
michael@0 | 637 | /*printf("%d ", metric[i]);*/ |
michael@0 | 638 | } |
michael@0 | 639 | /*printf("\n");*/ |
michael@0 | 640 | /* Search for the optimal tf resolution, including tf_select */ |
michael@0 | 641 | tf_select = 0; |
michael@0 | 642 | for (sel=0;sel<2;sel++) |
michael@0 | 643 | { |
michael@0 | 644 | cost0 = 0; |
michael@0 | 645 | cost1 = isTransient ? 0 : lambda; |
michael@0 | 646 | for (i=1;i<len;i++) |
michael@0 | 647 | { |
michael@0 | 648 | int curr0, curr1; |
michael@0 | 649 | curr0 = IMIN(cost0, cost1 + lambda); |
michael@0 | 650 | curr1 = IMIN(cost0 + lambda, cost1); |
michael@0 | 651 | cost0 = curr0 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+0]); |
michael@0 | 652 | cost1 = curr1 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+1]); |
michael@0 | 653 | } |
michael@0 | 654 | cost0 = IMIN(cost0, cost1); |
michael@0 | 655 | selcost[sel]=cost0; |
michael@0 | 656 | } |
michael@0 | 657 | /* For now, we're conservative and only allow tf_select=1 for transients. |
michael@0 | 658 | * If tests confirm it's useful for non-transients, we could allow it. */ |
michael@0 | 659 | if (selcost[1]<selcost[0] && isTransient) |
michael@0 | 660 | tf_select=1; |
michael@0 | 661 | cost0 = 0; |
michael@0 | 662 | cost1 = isTransient ? 0 : lambda; |
michael@0 | 663 | /* Viterbi forward pass */ |
michael@0 | 664 | for (i=1;i<len;i++) |
michael@0 | 665 | { |
michael@0 | 666 | int curr0, curr1; |
michael@0 | 667 | int from0, from1; |
michael@0 | 668 | |
michael@0 | 669 | from0 = cost0; |
michael@0 | 670 | from1 = cost1 + lambda; |
michael@0 | 671 | if (from0 < from1) |
michael@0 | 672 | { |
michael@0 | 673 | curr0 = from0; |
michael@0 | 674 | path0[i]= 0; |
michael@0 | 675 | } else { |
michael@0 | 676 | curr0 = from1; |
michael@0 | 677 | path0[i]= 1; |
michael@0 | 678 | } |
michael@0 | 679 | |
michael@0 | 680 | from0 = cost0 + lambda; |
michael@0 | 681 | from1 = cost1; |
michael@0 | 682 | if (from0 < from1) |
michael@0 | 683 | { |
michael@0 | 684 | curr1 = from0; |
michael@0 | 685 | path1[i]= 0; |
michael@0 | 686 | } else { |
michael@0 | 687 | curr1 = from1; |
michael@0 | 688 | path1[i]= 1; |
michael@0 | 689 | } |
michael@0 | 690 | cost0 = curr0 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]); |
michael@0 | 691 | cost1 = curr1 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]); |
michael@0 | 692 | } |
michael@0 | 693 | tf_res[len-1] = cost0 < cost1 ? 0 : 1; |
michael@0 | 694 | /* Viterbi backward pass to check the decisions */ |
michael@0 | 695 | for (i=len-2;i>=0;i--) |
michael@0 | 696 | { |
michael@0 | 697 | if (tf_res[i+1] == 1) |
michael@0 | 698 | tf_res[i] = path1[i+1]; |
michael@0 | 699 | else |
michael@0 | 700 | tf_res[i] = path0[i+1]; |
michael@0 | 701 | } |
michael@0 | 702 | /*printf("%d %f\n", *tf_sum, tf_estimate);*/ |
michael@0 | 703 | RESTORE_STACK; |
michael@0 | 704 | #ifdef FUZZING |
michael@0 | 705 | tf_select = rand()&0x1; |
michael@0 | 706 | tf_res[0] = rand()&0x1; |
michael@0 | 707 | for (i=1;i<len;i++) |
michael@0 | 708 | tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0); |
michael@0 | 709 | #endif |
michael@0 | 710 | return tf_select; |
michael@0 | 711 | } |
michael@0 | 712 | |
michael@0 | 713 | static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc) |
michael@0 | 714 | { |
michael@0 | 715 | int curr, i; |
michael@0 | 716 | int tf_select_rsv; |
michael@0 | 717 | int tf_changed; |
michael@0 | 718 | int logp; |
michael@0 | 719 | opus_uint32 budget; |
michael@0 | 720 | opus_uint32 tell; |
michael@0 | 721 | budget = enc->storage*8; |
michael@0 | 722 | tell = ec_tell(enc); |
michael@0 | 723 | logp = isTransient ? 2 : 4; |
michael@0 | 724 | /* Reserve space to code the tf_select decision. */ |
michael@0 | 725 | tf_select_rsv = LM>0 && tell+logp+1 <= budget; |
michael@0 | 726 | budget -= tf_select_rsv; |
michael@0 | 727 | curr = tf_changed = 0; |
michael@0 | 728 | for (i=start;i<end;i++) |
michael@0 | 729 | { |
michael@0 | 730 | if (tell+logp<=budget) |
michael@0 | 731 | { |
michael@0 | 732 | ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp); |
michael@0 | 733 | tell = ec_tell(enc); |
michael@0 | 734 | curr = tf_res[i]; |
michael@0 | 735 | tf_changed |= curr; |
michael@0 | 736 | } |
michael@0 | 737 | else |
michael@0 | 738 | tf_res[i] = curr; |
michael@0 | 739 | logp = isTransient ? 4 : 5; |
michael@0 | 740 | } |
michael@0 | 741 | /* Only code tf_select if it would actually make a difference. */ |
michael@0 | 742 | if (tf_select_rsv && |
michael@0 | 743 | tf_select_table[LM][4*isTransient+0+tf_changed]!= |
michael@0 | 744 | tf_select_table[LM][4*isTransient+2+tf_changed]) |
michael@0 | 745 | ec_enc_bit_logp(enc, tf_select, 1); |
michael@0 | 746 | else |
michael@0 | 747 | tf_select = 0; |
michael@0 | 748 | for (i=start;i<end;i++) |
michael@0 | 749 | tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]]; |
michael@0 | 750 | /*for(i=0;i<end;i++)printf("%d ", isTransient ? tf_res[i] : LM+tf_res[i]);printf("\n");*/ |
michael@0 | 751 | } |
michael@0 | 752 | |
michael@0 | 753 | |
michael@0 | 754 | static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X, |
michael@0 | 755 | const opus_val16 *bandLogE, int end, int LM, int C, int N0, |
michael@0 | 756 | AnalysisInfo *analysis, opus_val16 *stereo_saving, opus_val16 tf_estimate, |
michael@0 | 757 | int intensity, opus_val16 surround_trim) |
michael@0 | 758 | { |
michael@0 | 759 | int i; |
michael@0 | 760 | opus_val32 diff=0; |
michael@0 | 761 | int c; |
michael@0 | 762 | int trim_index = 5; |
michael@0 | 763 | opus_val16 trim = QCONST16(5.f, 8); |
michael@0 | 764 | opus_val16 logXC, logXC2; |
michael@0 | 765 | if (C==2) |
michael@0 | 766 | { |
michael@0 | 767 | opus_val16 sum = 0; /* Q10 */ |
michael@0 | 768 | opus_val16 minXC; /* Q10 */ |
michael@0 | 769 | /* Compute inter-channel correlation for low frequencies */ |
michael@0 | 770 | for (i=0;i<8;i++) |
michael@0 | 771 | { |
michael@0 | 772 | int j; |
michael@0 | 773 | opus_val32 partial = 0; |
michael@0 | 774 | for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++) |
michael@0 | 775 | partial = MAC16_16(partial, X[j], X[N0+j]); |
michael@0 | 776 | sum = ADD16(sum, EXTRACT16(SHR32(partial, 18))); |
michael@0 | 777 | } |
michael@0 | 778 | sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum); |
michael@0 | 779 | sum = MIN16(QCONST16(1.f, 10), ABS16(sum)); |
michael@0 | 780 | minXC = sum; |
michael@0 | 781 | for (i=8;i<intensity;i++) |
michael@0 | 782 | { |
michael@0 | 783 | int j; |
michael@0 | 784 | opus_val32 partial = 0; |
michael@0 | 785 | for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++) |
michael@0 | 786 | partial = MAC16_16(partial, X[j], X[N0+j]); |
michael@0 | 787 | minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18)))); |
michael@0 | 788 | } |
michael@0 | 789 | minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC)); |
michael@0 | 790 | /*printf ("%f\n", sum);*/ |
michael@0 | 791 | if (sum > QCONST16(.995f,10)) |
michael@0 | 792 | trim_index-=4; |
michael@0 | 793 | else if (sum > QCONST16(.92f,10)) |
michael@0 | 794 | trim_index-=3; |
michael@0 | 795 | else if (sum > QCONST16(.85f,10)) |
michael@0 | 796 | trim_index-=2; |
michael@0 | 797 | else if (sum > QCONST16(.8f,10)) |
michael@0 | 798 | trim_index-=1; |
michael@0 | 799 | /* mid-side savings estimations based on the LF average*/ |
michael@0 | 800 | logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum)); |
michael@0 | 801 | /* mid-side savings estimations based on min correlation */ |
michael@0 | 802 | logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(minXC, minXC))); |
michael@0 | 803 | #ifdef FIXED_POINT |
michael@0 | 804 | /* Compensate for Q20 vs Q14 input and convert output to Q8 */ |
michael@0 | 805 | logXC = PSHR32(logXC-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8); |
michael@0 | 806 | logXC2 = PSHR32(logXC2-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8); |
michael@0 | 807 | #endif |
michael@0 | 808 | |
michael@0 | 809 | trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC)); |
michael@0 | 810 | *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2)); |
michael@0 | 811 | } |
michael@0 | 812 | |
michael@0 | 813 | /* Estimate spectral tilt */ |
michael@0 | 814 | c=0; do { |
michael@0 | 815 | for (i=0;i<end-1;i++) |
michael@0 | 816 | { |
michael@0 | 817 | diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-end); |
michael@0 | 818 | } |
michael@0 | 819 | } while (++c<C); |
michael@0 | 820 | diff /= C*(end-1); |
michael@0 | 821 | /*printf("%f\n", diff);*/ |
michael@0 | 822 | if (diff > QCONST16(2.f, DB_SHIFT)) |
michael@0 | 823 | trim_index--; |
michael@0 | 824 | if (diff > QCONST16(8.f, DB_SHIFT)) |
michael@0 | 825 | trim_index--; |
michael@0 | 826 | if (diff < -QCONST16(4.f, DB_SHIFT)) |
michael@0 | 827 | trim_index++; |
michael@0 | 828 | if (diff < -QCONST16(10.f, DB_SHIFT)) |
michael@0 | 829 | trim_index++; |
michael@0 | 830 | trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), SHR16(diff+QCONST16(1.f, DB_SHIFT),DB_SHIFT-8)/6 )); |
michael@0 | 831 | trim -= SHR16(surround_trim, DB_SHIFT-8); |
michael@0 | 832 | trim -= 2*SHR16(tf_estimate, 14-8); |
michael@0 | 833 | #ifndef DISABLE_FLOAT_API |
michael@0 | 834 | if (analysis->valid) |
michael@0 | 835 | { |
michael@0 | 836 | trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), |
michael@0 | 837 | (opus_val16)(QCONST16(2.f, 8)*(analysis->tonality_slope+.05f)))); |
michael@0 | 838 | } |
michael@0 | 839 | #endif |
michael@0 | 840 | |
michael@0 | 841 | #ifdef FIXED_POINT |
michael@0 | 842 | trim_index = PSHR32(trim, 8); |
michael@0 | 843 | #else |
michael@0 | 844 | trim_index = (int)floor(.5f+trim); |
michael@0 | 845 | #endif |
michael@0 | 846 | if (trim_index<0) |
michael@0 | 847 | trim_index = 0; |
michael@0 | 848 | if (trim_index>10) |
michael@0 | 849 | trim_index = 10; |
michael@0 | 850 | /*printf("%d\n", trim_index);*/ |
michael@0 | 851 | #ifdef FUZZING |
michael@0 | 852 | trim_index = rand()%11; |
michael@0 | 853 | #endif |
michael@0 | 854 | return trim_index; |
michael@0 | 855 | } |
michael@0 | 856 | |
michael@0 | 857 | static int stereo_analysis(const CELTMode *m, const celt_norm *X, |
michael@0 | 858 | int LM, int N0) |
michael@0 | 859 | { |
michael@0 | 860 | int i; |
michael@0 | 861 | int thetas; |
michael@0 | 862 | opus_val32 sumLR = EPSILON, sumMS = EPSILON; |
michael@0 | 863 | |
michael@0 | 864 | /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */ |
michael@0 | 865 | for (i=0;i<13;i++) |
michael@0 | 866 | { |
michael@0 | 867 | int j; |
michael@0 | 868 | for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++) |
michael@0 | 869 | { |
michael@0 | 870 | opus_val32 L, R, M, S; |
michael@0 | 871 | /* We cast to 32-bit first because of the -32768 case */ |
michael@0 | 872 | L = EXTEND32(X[j]); |
michael@0 | 873 | R = EXTEND32(X[N0+j]); |
michael@0 | 874 | M = ADD32(L, R); |
michael@0 | 875 | S = SUB32(L, R); |
michael@0 | 876 | sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R))); |
michael@0 | 877 | sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S))); |
michael@0 | 878 | } |
michael@0 | 879 | } |
michael@0 | 880 | sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS); |
michael@0 | 881 | thetas = 13; |
michael@0 | 882 | /* We don't need thetas for lower bands with LM<=1 */ |
michael@0 | 883 | if (LM<=1) |
michael@0 | 884 | thetas -= 8; |
michael@0 | 885 | return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS) |
michael@0 | 886 | > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR); |
michael@0 | 887 | } |
michael@0 | 888 | |
michael@0 | 889 | static opus_val16 dynalloc_analysis(const opus_val16 *bandLogE, const opus_val16 *bandLogE2, |
michael@0 | 890 | int nbEBands, int start, int end, int C, int *offsets, int lsb_depth, const opus_int16 *logN, |
michael@0 | 891 | int isTransient, int vbr, int constrained_vbr, const opus_int16 *eBands, int LM, |
michael@0 | 892 | int effectiveBytes, opus_int32 *tot_boost_, int lfe, opus_val16 *surround_dynalloc) |
michael@0 | 893 | { |
michael@0 | 894 | int i, c; |
michael@0 | 895 | opus_int32 tot_boost=0; |
michael@0 | 896 | opus_val16 maxDepth; |
michael@0 | 897 | VARDECL(opus_val16, follower); |
michael@0 | 898 | VARDECL(opus_val16, noise_floor); |
michael@0 | 899 | SAVE_STACK; |
michael@0 | 900 | ALLOC(follower, C*nbEBands, opus_val16); |
michael@0 | 901 | ALLOC(noise_floor, C*nbEBands, opus_val16); |
michael@0 | 902 | for (i=0;i<nbEBands;i++) |
michael@0 | 903 | offsets[i] = 0; |
michael@0 | 904 | /* Dynamic allocation code */ |
michael@0 | 905 | maxDepth=-QCONST16(31.9f, DB_SHIFT); |
michael@0 | 906 | for (i=0;i<end;i++) |
michael@0 | 907 | { |
michael@0 | 908 | /* Noise floor must take into account eMeans, the depth, the width of the bands |
michael@0 | 909 | and the preemphasis filter (approx. square of bark band ID) */ |
michael@0 | 910 | noise_floor[i] = MULT16_16(QCONST16(0.0625f, DB_SHIFT),logN[i]) |
michael@0 | 911 | +QCONST16(.5f,DB_SHIFT)+SHL16(9-lsb_depth,DB_SHIFT)-SHL16(eMeans[i],6) |
michael@0 | 912 | +MULT16_16(QCONST16(.0062,DB_SHIFT),(i+5)*(i+5)); |
michael@0 | 913 | } |
michael@0 | 914 | c=0;do |
michael@0 | 915 | { |
michael@0 | 916 | for (i=0;i<end;i++) |
michael@0 | 917 | maxDepth = MAX16(maxDepth, bandLogE[c*nbEBands+i]-noise_floor[i]); |
michael@0 | 918 | } while (++c<C); |
michael@0 | 919 | /* Make sure that dynamic allocation can't make us bust the budget */ |
michael@0 | 920 | if (effectiveBytes > 50 && LM>=1 && !lfe) |
michael@0 | 921 | { |
michael@0 | 922 | int last=0; |
michael@0 | 923 | c=0;do |
michael@0 | 924 | { |
michael@0 | 925 | follower[c*nbEBands] = bandLogE2[c*nbEBands]; |
michael@0 | 926 | for (i=1;i<end;i++) |
michael@0 | 927 | { |
michael@0 | 928 | /* The last band to be at least 3 dB higher than the previous one |
michael@0 | 929 | is the last we'll consider. Otherwise, we run into problems on |
michael@0 | 930 | bandlimited signals. */ |
michael@0 | 931 | if (bandLogE2[c*nbEBands+i] > bandLogE2[c*nbEBands+i-1]+QCONST16(.5f,DB_SHIFT)) |
michael@0 | 932 | last=i; |
michael@0 | 933 | follower[c*nbEBands+i] = MIN16(follower[c*nbEBands+i-1]+QCONST16(1.5f,DB_SHIFT), bandLogE2[c*nbEBands+i]); |
michael@0 | 934 | } |
michael@0 | 935 | for (i=last-1;i>=0;i--) |
michael@0 | 936 | follower[c*nbEBands+i] = MIN16(follower[c*nbEBands+i], MIN16(follower[c*nbEBands+i+1]+QCONST16(2.f,DB_SHIFT), bandLogE2[c*nbEBands+i])); |
michael@0 | 937 | for (i=0;i<end;i++) |
michael@0 | 938 | follower[c*nbEBands+i] = MAX16(follower[c*nbEBands+i], noise_floor[i]); |
michael@0 | 939 | } while (++c<C); |
michael@0 | 940 | if (C==2) |
michael@0 | 941 | { |
michael@0 | 942 | for (i=start;i<end;i++) |
michael@0 | 943 | { |
michael@0 | 944 | /* Consider 24 dB "cross-talk" */ |
michael@0 | 945 | follower[nbEBands+i] = MAX16(follower[nbEBands+i], follower[ i]-QCONST16(4.f,DB_SHIFT)); |
michael@0 | 946 | follower[ i] = MAX16(follower[ i], follower[nbEBands+i]-QCONST16(4.f,DB_SHIFT)); |
michael@0 | 947 | follower[i] = HALF16(MAX16(0, bandLogE[i]-follower[i]) + MAX16(0, bandLogE[nbEBands+i]-follower[nbEBands+i])); |
michael@0 | 948 | } |
michael@0 | 949 | } else { |
michael@0 | 950 | for (i=start;i<end;i++) |
michael@0 | 951 | { |
michael@0 | 952 | follower[i] = MAX16(0, bandLogE[i]-follower[i]); |
michael@0 | 953 | } |
michael@0 | 954 | } |
michael@0 | 955 | for (i=start;i<end;i++) |
michael@0 | 956 | follower[i] = MAX16(follower[i], surround_dynalloc[i]); |
michael@0 | 957 | /* For non-transient CBR/CVBR frames, halve the dynalloc contribution */ |
michael@0 | 958 | if ((!vbr || constrained_vbr)&&!isTransient) |
michael@0 | 959 | { |
michael@0 | 960 | for (i=start;i<end;i++) |
michael@0 | 961 | follower[i] = HALF16(follower[i]); |
michael@0 | 962 | } |
michael@0 | 963 | for (i=start;i<end;i++) |
michael@0 | 964 | { |
michael@0 | 965 | int width; |
michael@0 | 966 | int boost; |
michael@0 | 967 | int boost_bits; |
michael@0 | 968 | |
michael@0 | 969 | if (i<8) |
michael@0 | 970 | follower[i] *= 2; |
michael@0 | 971 | if (i>=12) |
michael@0 | 972 | follower[i] = HALF16(follower[i]); |
michael@0 | 973 | follower[i] = MIN16(follower[i], QCONST16(4, DB_SHIFT)); |
michael@0 | 974 | |
michael@0 | 975 | width = C*(eBands[i+1]-eBands[i])<<LM; |
michael@0 | 976 | if (width<6) |
michael@0 | 977 | { |
michael@0 | 978 | boost = (int)SHR32(EXTEND32(follower[i]),DB_SHIFT); |
michael@0 | 979 | boost_bits = boost*width<<BITRES; |
michael@0 | 980 | } else if (width > 48) { |
michael@0 | 981 | boost = (int)SHR32(EXTEND32(follower[i])*8,DB_SHIFT); |
michael@0 | 982 | boost_bits = (boost*width<<BITRES)/8; |
michael@0 | 983 | } else { |
michael@0 | 984 | boost = (int)SHR32(EXTEND32(follower[i])*width/6,DB_SHIFT); |
michael@0 | 985 | boost_bits = boost*6<<BITRES; |
michael@0 | 986 | } |
michael@0 | 987 | /* For CBR and non-transient CVBR frames, limit dynalloc to 1/4 of the bits */ |
michael@0 | 988 | if ((!vbr || (constrained_vbr&&!isTransient)) |
michael@0 | 989 | && (tot_boost+boost_bits)>>BITRES>>3 > effectiveBytes/4) |
michael@0 | 990 | { |
michael@0 | 991 | opus_int32 cap = ((effectiveBytes/4)<<BITRES<<3); |
michael@0 | 992 | offsets[i] = cap-tot_boost; |
michael@0 | 993 | tot_boost = cap; |
michael@0 | 994 | break; |
michael@0 | 995 | } else { |
michael@0 | 996 | offsets[i] = boost; |
michael@0 | 997 | tot_boost += boost_bits; |
michael@0 | 998 | } |
michael@0 | 999 | } |
michael@0 | 1000 | } |
michael@0 | 1001 | *tot_boost_ = tot_boost; |
michael@0 | 1002 | RESTORE_STACK; |
michael@0 | 1003 | return maxDepth; |
michael@0 | 1004 | } |
michael@0 | 1005 | |
michael@0 | 1006 | |
michael@0 | 1007 | static int run_prefilter(CELTEncoder *st, celt_sig *in, celt_sig *prefilter_mem, int CC, int N, |
michael@0 | 1008 | int prefilter_tapset, int *pitch, opus_val16 *gain, int *qgain, int enabled, int nbAvailableBytes) |
michael@0 | 1009 | { |
michael@0 | 1010 | int c; |
michael@0 | 1011 | VARDECL(celt_sig, _pre); |
michael@0 | 1012 | celt_sig *pre[2]; |
michael@0 | 1013 | const CELTMode *mode; |
michael@0 | 1014 | int pitch_index; |
michael@0 | 1015 | opus_val16 gain1; |
michael@0 | 1016 | opus_val16 pf_threshold; |
michael@0 | 1017 | int pf_on; |
michael@0 | 1018 | int qg; |
michael@0 | 1019 | SAVE_STACK; |
michael@0 | 1020 | |
michael@0 | 1021 | mode = st->mode; |
michael@0 | 1022 | ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig); |
michael@0 | 1023 | |
michael@0 | 1024 | pre[0] = _pre; |
michael@0 | 1025 | pre[1] = _pre + (N+COMBFILTER_MAXPERIOD); |
michael@0 | 1026 | |
michael@0 | 1027 | |
michael@0 | 1028 | c=0; do { |
michael@0 | 1029 | OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD); |
michael@0 | 1030 | OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N); |
michael@0 | 1031 | } while (++c<CC); |
michael@0 | 1032 | |
michael@0 | 1033 | if (enabled) |
michael@0 | 1034 | { |
michael@0 | 1035 | VARDECL(opus_val16, pitch_buf); |
michael@0 | 1036 | ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16); |
michael@0 | 1037 | |
michael@0 | 1038 | pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC, st->arch); |
michael@0 | 1039 | /* Don't search for the fir last 1.5 octave of the range because |
michael@0 | 1040 | there's too many false-positives due to short-term correlation */ |
michael@0 | 1041 | pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N, |
michael@0 | 1042 | COMBFILTER_MAXPERIOD-3*COMBFILTER_MINPERIOD, &pitch_index, |
michael@0 | 1043 | st->arch); |
michael@0 | 1044 | pitch_index = COMBFILTER_MAXPERIOD-pitch_index; |
michael@0 | 1045 | |
michael@0 | 1046 | gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD, |
michael@0 | 1047 | N, &pitch_index, st->prefilter_period, st->prefilter_gain); |
michael@0 | 1048 | if (pitch_index > COMBFILTER_MAXPERIOD-2) |
michael@0 | 1049 | pitch_index = COMBFILTER_MAXPERIOD-2; |
michael@0 | 1050 | gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1); |
michael@0 | 1051 | /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.tonality);*/ |
michael@0 | 1052 | if (st->loss_rate>2) |
michael@0 | 1053 | gain1 = HALF32(gain1); |
michael@0 | 1054 | if (st->loss_rate>4) |
michael@0 | 1055 | gain1 = HALF32(gain1); |
michael@0 | 1056 | if (st->loss_rate>8) |
michael@0 | 1057 | gain1 = 0; |
michael@0 | 1058 | } else { |
michael@0 | 1059 | gain1 = 0; |
michael@0 | 1060 | pitch_index = COMBFILTER_MINPERIOD; |
michael@0 | 1061 | } |
michael@0 | 1062 | |
michael@0 | 1063 | /* Gain threshold for enabling the prefilter/postfilter */ |
michael@0 | 1064 | pf_threshold = QCONST16(.2f,15); |
michael@0 | 1065 | |
michael@0 | 1066 | /* Adjusting the threshold based on rate and continuity */ |
michael@0 | 1067 | if (abs(pitch_index-st->prefilter_period)*10>pitch_index) |
michael@0 | 1068 | pf_threshold += QCONST16(.2f,15); |
michael@0 | 1069 | if (nbAvailableBytes<25) |
michael@0 | 1070 | pf_threshold += QCONST16(.1f,15); |
michael@0 | 1071 | if (nbAvailableBytes<35) |
michael@0 | 1072 | pf_threshold += QCONST16(.1f,15); |
michael@0 | 1073 | if (st->prefilter_gain > QCONST16(.4f,15)) |
michael@0 | 1074 | pf_threshold -= QCONST16(.1f,15); |
michael@0 | 1075 | if (st->prefilter_gain > QCONST16(.55f,15)) |
michael@0 | 1076 | pf_threshold -= QCONST16(.1f,15); |
michael@0 | 1077 | |
michael@0 | 1078 | /* Hard threshold at 0.2 */ |
michael@0 | 1079 | pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15)); |
michael@0 | 1080 | if (gain1<pf_threshold) |
michael@0 | 1081 | { |
michael@0 | 1082 | gain1 = 0; |
michael@0 | 1083 | pf_on = 0; |
michael@0 | 1084 | qg = 0; |
michael@0 | 1085 | } else { |
michael@0 | 1086 | /*This block is not gated by a total bits check only because |
michael@0 | 1087 | of the nbAvailableBytes check above.*/ |
michael@0 | 1088 | if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15)) |
michael@0 | 1089 | gain1=st->prefilter_gain; |
michael@0 | 1090 | |
michael@0 | 1091 | #ifdef FIXED_POINT |
michael@0 | 1092 | qg = ((gain1+1536)>>10)/3-1; |
michael@0 | 1093 | #else |
michael@0 | 1094 | qg = (int)floor(.5f+gain1*32/3)-1; |
michael@0 | 1095 | #endif |
michael@0 | 1096 | qg = IMAX(0, IMIN(7, qg)); |
michael@0 | 1097 | gain1 = QCONST16(0.09375f,15)*(qg+1); |
michael@0 | 1098 | pf_on = 1; |
michael@0 | 1099 | } |
michael@0 | 1100 | /*printf("%d %f\n", pitch_index, gain1);*/ |
michael@0 | 1101 | |
michael@0 | 1102 | c=0; do { |
michael@0 | 1103 | int offset = mode->shortMdctSize-st->overlap; |
michael@0 | 1104 | st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD); |
michael@0 | 1105 | OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap); |
michael@0 | 1106 | if (offset) |
michael@0 | 1107 | comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD, |
michael@0 | 1108 | st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain, |
michael@0 | 1109 | st->prefilter_tapset, st->prefilter_tapset, NULL, 0); |
michael@0 | 1110 | |
michael@0 | 1111 | comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset, |
michael@0 | 1112 | st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1, |
michael@0 | 1113 | st->prefilter_tapset, prefilter_tapset, mode->window, st->overlap); |
michael@0 | 1114 | OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap); |
michael@0 | 1115 | |
michael@0 | 1116 | if (N>COMBFILTER_MAXPERIOD) |
michael@0 | 1117 | { |
michael@0 | 1118 | OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD); |
michael@0 | 1119 | } else { |
michael@0 | 1120 | OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N); |
michael@0 | 1121 | OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N); |
michael@0 | 1122 | } |
michael@0 | 1123 | } while (++c<CC); |
michael@0 | 1124 | |
michael@0 | 1125 | RESTORE_STACK; |
michael@0 | 1126 | *gain = gain1; |
michael@0 | 1127 | *pitch = pitch_index; |
michael@0 | 1128 | *qgain = qg; |
michael@0 | 1129 | return pf_on; |
michael@0 | 1130 | } |
michael@0 | 1131 | |
michael@0 | 1132 | static int compute_vbr(const CELTMode *mode, AnalysisInfo *analysis, opus_int32 base_target, |
michael@0 | 1133 | int LM, opus_int32 bitrate, int lastCodedBands, int C, int intensity, |
michael@0 | 1134 | int constrained_vbr, opus_val16 stereo_saving, int tot_boost, |
michael@0 | 1135 | opus_val16 tf_estimate, int pitch_change, opus_val16 maxDepth, |
michael@0 | 1136 | int variable_duration, int lfe, int has_surround_mask, opus_val16 surround_masking, |
michael@0 | 1137 | opus_val16 temporal_vbr) |
michael@0 | 1138 | { |
michael@0 | 1139 | /* The target rate in 8th bits per frame */ |
michael@0 | 1140 | opus_int32 target; |
michael@0 | 1141 | int coded_bins; |
michael@0 | 1142 | int coded_bands; |
michael@0 | 1143 | opus_val16 tf_calibration; |
michael@0 | 1144 | int nbEBands; |
michael@0 | 1145 | const opus_int16 *eBands; |
michael@0 | 1146 | |
michael@0 | 1147 | nbEBands = mode->nbEBands; |
michael@0 | 1148 | eBands = mode->eBands; |
michael@0 | 1149 | |
michael@0 | 1150 | coded_bands = lastCodedBands ? lastCodedBands : nbEBands; |
michael@0 | 1151 | coded_bins = eBands[coded_bands]<<LM; |
michael@0 | 1152 | if (C==2) |
michael@0 | 1153 | coded_bins += eBands[IMIN(intensity, coded_bands)]<<LM; |
michael@0 | 1154 | |
michael@0 | 1155 | target = base_target; |
michael@0 | 1156 | |
michael@0 | 1157 | /*printf("%f %f %f %f %d %d ", st->analysis.activity, st->analysis.tonality, tf_estimate, st->stereo_saving, tot_boost, coded_bands);*/ |
michael@0 | 1158 | #ifndef DISABLE_FLOAT_API |
michael@0 | 1159 | if (analysis->valid && analysis->activity<.4) |
michael@0 | 1160 | target -= (opus_int32)((coded_bins<<BITRES)*(.4f-analysis->activity)); |
michael@0 | 1161 | #endif |
michael@0 | 1162 | /* Stereo savings */ |
michael@0 | 1163 | if (C==2) |
michael@0 | 1164 | { |
michael@0 | 1165 | int coded_stereo_bands; |
michael@0 | 1166 | int coded_stereo_dof; |
michael@0 | 1167 | opus_val16 max_frac; |
michael@0 | 1168 | coded_stereo_bands = IMIN(intensity, coded_bands); |
michael@0 | 1169 | coded_stereo_dof = (eBands[coded_stereo_bands]<<LM)-coded_stereo_bands; |
michael@0 | 1170 | /* Maximum fraction of the bits we can save if the signal is mono. */ |
michael@0 | 1171 | max_frac = DIV32_16(MULT16_16(QCONST16(0.8f, 15), coded_stereo_dof), coded_bins); |
michael@0 | 1172 | stereo_saving = MIN16(stereo_saving, QCONST16(1.f, 8)); |
michael@0 | 1173 | /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/ |
michael@0 | 1174 | target -= (opus_int32)MIN32(MULT16_32_Q15(max_frac,target), |
michael@0 | 1175 | SHR32(MULT16_16(stereo_saving-QCONST16(0.1f,8),(coded_stereo_dof<<BITRES)),8)); |
michael@0 | 1176 | } |
michael@0 | 1177 | /* Boost the rate according to dynalloc (minus the dynalloc average for calibration). */ |
michael@0 | 1178 | target += tot_boost-(16<<LM); |
michael@0 | 1179 | /* Apply transient boost, compensating for average boost. */ |
michael@0 | 1180 | tf_calibration = variable_duration==OPUS_FRAMESIZE_VARIABLE ? |
michael@0 | 1181 | QCONST16(0.02f,14) : QCONST16(0.04f,14); |
michael@0 | 1182 | target += (opus_int32)SHL32(MULT16_32_Q15(tf_estimate-tf_calibration, target),1); |
michael@0 | 1183 | |
michael@0 | 1184 | #ifndef DISABLE_FLOAT_API |
michael@0 | 1185 | /* Apply tonality boost */ |
michael@0 | 1186 | if (analysis->valid && !lfe) |
michael@0 | 1187 | { |
michael@0 | 1188 | opus_int32 tonal_target; |
michael@0 | 1189 | float tonal; |
michael@0 | 1190 | |
michael@0 | 1191 | /* Tonality boost (compensating for the average). */ |
michael@0 | 1192 | tonal = MAX16(0.f,analysis->tonality-.15f)-0.09f; |
michael@0 | 1193 | tonal_target = target + (opus_int32)((coded_bins<<BITRES)*1.2f*tonal); |
michael@0 | 1194 | if (pitch_change) |
michael@0 | 1195 | tonal_target += (opus_int32)((coded_bins<<BITRES)*.8f); |
michael@0 | 1196 | /*printf("%f %f ", analysis->tonality, tonal);*/ |
michael@0 | 1197 | target = tonal_target; |
michael@0 | 1198 | } |
michael@0 | 1199 | #endif |
michael@0 | 1200 | |
michael@0 | 1201 | if (has_surround_mask&&!lfe) |
michael@0 | 1202 | { |
michael@0 | 1203 | opus_int32 surround_target = target + (opus_int32)SHR32(MULT16_16(surround_masking,coded_bins<<BITRES), DB_SHIFT); |
michael@0 | 1204 | /*printf("%f %d %d %d %d %d %d ", surround_masking, coded_bins, st->end, st->intensity, surround_target, target, st->bitrate);*/ |
michael@0 | 1205 | target = IMAX(target/4, surround_target); |
michael@0 | 1206 | } |
michael@0 | 1207 | |
michael@0 | 1208 | { |
michael@0 | 1209 | opus_int32 floor_depth; |
michael@0 | 1210 | int bins; |
michael@0 | 1211 | bins = eBands[nbEBands-2]<<LM; |
michael@0 | 1212 | /*floor_depth = SHR32(MULT16_16((C*bins<<BITRES),celt_log2(SHL32(MAX16(1,sample_max),13))), DB_SHIFT);*/ |
michael@0 | 1213 | floor_depth = (opus_int32)SHR32(MULT16_16((C*bins<<BITRES),maxDepth), DB_SHIFT); |
michael@0 | 1214 | floor_depth = IMAX(floor_depth, target>>2); |
michael@0 | 1215 | target = IMIN(target, floor_depth); |
michael@0 | 1216 | /*printf("%f %d\n", maxDepth, floor_depth);*/ |
michael@0 | 1217 | } |
michael@0 | 1218 | |
michael@0 | 1219 | if ((!has_surround_mask||lfe) && (constrained_vbr || bitrate<64000)) |
michael@0 | 1220 | { |
michael@0 | 1221 | opus_val16 rate_factor; |
michael@0 | 1222 | #ifdef FIXED_POINT |
michael@0 | 1223 | rate_factor = MAX16(0,(bitrate-32000)); |
michael@0 | 1224 | #else |
michael@0 | 1225 | rate_factor = MAX16(0,(1.f/32768)*(bitrate-32000)); |
michael@0 | 1226 | #endif |
michael@0 | 1227 | if (constrained_vbr) |
michael@0 | 1228 | rate_factor = MIN16(rate_factor, QCONST16(0.67f, 15)); |
michael@0 | 1229 | target = base_target + (opus_int32)MULT16_32_Q15(rate_factor, target-base_target); |
michael@0 | 1230 | |
michael@0 | 1231 | } |
michael@0 | 1232 | |
michael@0 | 1233 | if (!has_surround_mask && tf_estimate < QCONST16(.2f, 14)) |
michael@0 | 1234 | { |
michael@0 | 1235 | opus_val16 amount; |
michael@0 | 1236 | opus_val16 tvbr_factor; |
michael@0 | 1237 | amount = MULT16_16_Q15(QCONST16(.0000031f, 30), IMAX(0, IMIN(32000, 96000-bitrate))); |
michael@0 | 1238 | tvbr_factor = SHR32(MULT16_16(temporal_vbr, amount), DB_SHIFT); |
michael@0 | 1239 | target += (opus_int32)MULT16_32_Q15(tvbr_factor, target); |
michael@0 | 1240 | } |
michael@0 | 1241 | |
michael@0 | 1242 | /* Don't allow more than doubling the rate */ |
michael@0 | 1243 | target = IMIN(2*base_target, target); |
michael@0 | 1244 | |
michael@0 | 1245 | return target; |
michael@0 | 1246 | } |
michael@0 | 1247 | |
michael@0 | 1248 | int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc) |
michael@0 | 1249 | { |
michael@0 | 1250 | int i, c, N; |
michael@0 | 1251 | opus_int32 bits; |
michael@0 | 1252 | ec_enc _enc; |
michael@0 | 1253 | VARDECL(celt_sig, in); |
michael@0 | 1254 | VARDECL(celt_sig, freq); |
michael@0 | 1255 | VARDECL(celt_norm, X); |
michael@0 | 1256 | VARDECL(celt_ener, bandE); |
michael@0 | 1257 | VARDECL(opus_val16, bandLogE); |
michael@0 | 1258 | VARDECL(opus_val16, bandLogE2); |
michael@0 | 1259 | VARDECL(int, fine_quant); |
michael@0 | 1260 | VARDECL(opus_val16, error); |
michael@0 | 1261 | VARDECL(int, pulses); |
michael@0 | 1262 | VARDECL(int, cap); |
michael@0 | 1263 | VARDECL(int, offsets); |
michael@0 | 1264 | VARDECL(int, fine_priority); |
michael@0 | 1265 | VARDECL(int, tf_res); |
michael@0 | 1266 | VARDECL(unsigned char, collapse_masks); |
michael@0 | 1267 | celt_sig *prefilter_mem; |
michael@0 | 1268 | opus_val16 *oldBandE, *oldLogE, *oldLogE2; |
michael@0 | 1269 | int shortBlocks=0; |
michael@0 | 1270 | int isTransient=0; |
michael@0 | 1271 | const int CC = st->channels; |
michael@0 | 1272 | const int C = st->stream_channels; |
michael@0 | 1273 | int LM, M; |
michael@0 | 1274 | int tf_select; |
michael@0 | 1275 | int nbFilledBytes, nbAvailableBytes; |
michael@0 | 1276 | int effEnd; |
michael@0 | 1277 | int codedBands; |
michael@0 | 1278 | int tf_sum; |
michael@0 | 1279 | int alloc_trim; |
michael@0 | 1280 | int pitch_index=COMBFILTER_MINPERIOD; |
michael@0 | 1281 | opus_val16 gain1 = 0; |
michael@0 | 1282 | int dual_stereo=0; |
michael@0 | 1283 | int effectiveBytes; |
michael@0 | 1284 | int dynalloc_logp; |
michael@0 | 1285 | opus_int32 vbr_rate; |
michael@0 | 1286 | opus_int32 total_bits; |
michael@0 | 1287 | opus_int32 total_boost; |
michael@0 | 1288 | opus_int32 balance; |
michael@0 | 1289 | opus_int32 tell; |
michael@0 | 1290 | int prefilter_tapset=0; |
michael@0 | 1291 | int pf_on; |
michael@0 | 1292 | int anti_collapse_rsv; |
michael@0 | 1293 | int anti_collapse_on=0; |
michael@0 | 1294 | int silence=0; |
michael@0 | 1295 | int tf_chan = 0; |
michael@0 | 1296 | opus_val16 tf_estimate; |
michael@0 | 1297 | int pitch_change=0; |
michael@0 | 1298 | opus_int32 tot_boost; |
michael@0 | 1299 | opus_val32 sample_max; |
michael@0 | 1300 | opus_val16 maxDepth; |
michael@0 | 1301 | const OpusCustomMode *mode; |
michael@0 | 1302 | int nbEBands; |
michael@0 | 1303 | int overlap; |
michael@0 | 1304 | const opus_int16 *eBands; |
michael@0 | 1305 | int secondMdct; |
michael@0 | 1306 | int signalBandwidth; |
michael@0 | 1307 | int transient_got_disabled=0; |
michael@0 | 1308 | opus_val16 surround_masking=0; |
michael@0 | 1309 | opus_val16 temporal_vbr=0; |
michael@0 | 1310 | opus_val16 surround_trim = 0; |
michael@0 | 1311 | opus_int32 equiv_rate = 510000; |
michael@0 | 1312 | VARDECL(opus_val16, surround_dynalloc); |
michael@0 | 1313 | ALLOC_STACK; |
michael@0 | 1314 | |
michael@0 | 1315 | mode = st->mode; |
michael@0 | 1316 | nbEBands = mode->nbEBands; |
michael@0 | 1317 | overlap = mode->overlap; |
michael@0 | 1318 | eBands = mode->eBands; |
michael@0 | 1319 | tf_estimate = 0; |
michael@0 | 1320 | if (nbCompressedBytes<2 || pcm==NULL) |
michael@0 | 1321 | { |
michael@0 | 1322 | RESTORE_STACK; |
michael@0 | 1323 | return OPUS_BAD_ARG; |
michael@0 | 1324 | } |
michael@0 | 1325 | |
michael@0 | 1326 | frame_size *= st->upsample; |
michael@0 | 1327 | for (LM=0;LM<=mode->maxLM;LM++) |
michael@0 | 1328 | if (mode->shortMdctSize<<LM==frame_size) |
michael@0 | 1329 | break; |
michael@0 | 1330 | if (LM>mode->maxLM) |
michael@0 | 1331 | { |
michael@0 | 1332 | RESTORE_STACK; |
michael@0 | 1333 | return OPUS_BAD_ARG; |
michael@0 | 1334 | } |
michael@0 | 1335 | M=1<<LM; |
michael@0 | 1336 | N = M*mode->shortMdctSize; |
michael@0 | 1337 | |
michael@0 | 1338 | prefilter_mem = st->in_mem+CC*(st->overlap); |
michael@0 | 1339 | oldBandE = (opus_val16*)(st->in_mem+CC*(st->overlap+COMBFILTER_MAXPERIOD)); |
michael@0 | 1340 | oldLogE = oldBandE + CC*nbEBands; |
michael@0 | 1341 | oldLogE2 = oldLogE + CC*nbEBands; |
michael@0 | 1342 | |
michael@0 | 1343 | if (enc==NULL) |
michael@0 | 1344 | { |
michael@0 | 1345 | tell=1; |
michael@0 | 1346 | nbFilledBytes=0; |
michael@0 | 1347 | } else { |
michael@0 | 1348 | tell=ec_tell(enc); |
michael@0 | 1349 | nbFilledBytes=(tell+4)>>3; |
michael@0 | 1350 | } |
michael@0 | 1351 | |
michael@0 | 1352 | #ifdef CUSTOM_MODES |
michael@0 | 1353 | if (st->signalling && enc==NULL) |
michael@0 | 1354 | { |
michael@0 | 1355 | int tmp = (mode->effEBands-st->end)>>1; |
michael@0 | 1356 | st->end = IMAX(1, mode->effEBands-tmp); |
michael@0 | 1357 | compressed[0] = tmp<<5; |
michael@0 | 1358 | compressed[0] |= LM<<3; |
michael@0 | 1359 | compressed[0] |= (C==2)<<2; |
michael@0 | 1360 | /* Convert "standard mode" to Opus header */ |
michael@0 | 1361 | if (mode->Fs==48000 && mode->shortMdctSize==120) |
michael@0 | 1362 | { |
michael@0 | 1363 | int c0 = toOpus(compressed[0]); |
michael@0 | 1364 | if (c0<0) |
michael@0 | 1365 | { |
michael@0 | 1366 | RESTORE_STACK; |
michael@0 | 1367 | return OPUS_BAD_ARG; |
michael@0 | 1368 | } |
michael@0 | 1369 | compressed[0] = c0; |
michael@0 | 1370 | } |
michael@0 | 1371 | compressed++; |
michael@0 | 1372 | nbCompressedBytes--; |
michael@0 | 1373 | } |
michael@0 | 1374 | #else |
michael@0 | 1375 | celt_assert(st->signalling==0); |
michael@0 | 1376 | #endif |
michael@0 | 1377 | |
michael@0 | 1378 | /* Can't produce more than 1275 output bytes */ |
michael@0 | 1379 | nbCompressedBytes = IMIN(nbCompressedBytes,1275); |
michael@0 | 1380 | nbAvailableBytes = nbCompressedBytes - nbFilledBytes; |
michael@0 | 1381 | |
michael@0 | 1382 | if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX) |
michael@0 | 1383 | { |
michael@0 | 1384 | opus_int32 den=mode->Fs>>BITRES; |
michael@0 | 1385 | vbr_rate=(st->bitrate*frame_size+(den>>1))/den; |
michael@0 | 1386 | #ifdef CUSTOM_MODES |
michael@0 | 1387 | if (st->signalling) |
michael@0 | 1388 | vbr_rate -= 8<<BITRES; |
michael@0 | 1389 | #endif |
michael@0 | 1390 | effectiveBytes = vbr_rate>>(3+BITRES); |
michael@0 | 1391 | } else { |
michael@0 | 1392 | opus_int32 tmp; |
michael@0 | 1393 | vbr_rate = 0; |
michael@0 | 1394 | tmp = st->bitrate*frame_size; |
michael@0 | 1395 | if (tell>1) |
michael@0 | 1396 | tmp += tell; |
michael@0 | 1397 | if (st->bitrate!=OPUS_BITRATE_MAX) |
michael@0 | 1398 | nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes, |
michael@0 | 1399 | (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling)); |
michael@0 | 1400 | effectiveBytes = nbCompressedBytes; |
michael@0 | 1401 | } |
michael@0 | 1402 | if (st->bitrate != OPUS_BITRATE_MAX) |
michael@0 | 1403 | equiv_rate = st->bitrate - (40*C+20)*((400>>LM) - 50); |
michael@0 | 1404 | |
michael@0 | 1405 | if (enc==NULL) |
michael@0 | 1406 | { |
michael@0 | 1407 | ec_enc_init(&_enc, compressed, nbCompressedBytes); |
michael@0 | 1408 | enc = &_enc; |
michael@0 | 1409 | } |
michael@0 | 1410 | |
michael@0 | 1411 | if (vbr_rate>0) |
michael@0 | 1412 | { |
michael@0 | 1413 | /* Computes the max bit-rate allowed in VBR mode to avoid violating the |
michael@0 | 1414 | target rate and buffering. |
michael@0 | 1415 | We must do this up front so that bust-prevention logic triggers |
michael@0 | 1416 | correctly if we don't have enough bits. */ |
michael@0 | 1417 | if (st->constrained_vbr) |
michael@0 | 1418 | { |
michael@0 | 1419 | opus_int32 vbr_bound; |
michael@0 | 1420 | opus_int32 max_allowed; |
michael@0 | 1421 | /* We could use any multiple of vbr_rate as bound (depending on the |
michael@0 | 1422 | delay). |
michael@0 | 1423 | This is clamped to ensure we use at least two bytes if the encoder |
michael@0 | 1424 | was entirely empty, but to allow 0 in hybrid mode. */ |
michael@0 | 1425 | vbr_bound = vbr_rate; |
michael@0 | 1426 | max_allowed = IMIN(IMAX(tell==1?2:0, |
michael@0 | 1427 | (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)), |
michael@0 | 1428 | nbAvailableBytes); |
michael@0 | 1429 | if(max_allowed < nbAvailableBytes) |
michael@0 | 1430 | { |
michael@0 | 1431 | nbCompressedBytes = nbFilledBytes+max_allowed; |
michael@0 | 1432 | nbAvailableBytes = max_allowed; |
michael@0 | 1433 | ec_enc_shrink(enc, nbCompressedBytes); |
michael@0 | 1434 | } |
michael@0 | 1435 | } |
michael@0 | 1436 | } |
michael@0 | 1437 | total_bits = nbCompressedBytes*8; |
michael@0 | 1438 | |
michael@0 | 1439 | effEnd = st->end; |
michael@0 | 1440 | if (effEnd > mode->effEBands) |
michael@0 | 1441 | effEnd = mode->effEBands; |
michael@0 | 1442 | |
michael@0 | 1443 | ALLOC(in, CC*(N+st->overlap), celt_sig); |
michael@0 | 1444 | |
michael@0 | 1445 | sample_max=MAX32(st->overlap_max, celt_maxabs16(pcm, C*(N-overlap)/st->upsample)); |
michael@0 | 1446 | st->overlap_max=celt_maxabs16(pcm+C*(N-overlap)/st->upsample, C*overlap/st->upsample); |
michael@0 | 1447 | sample_max=MAX32(sample_max, st->overlap_max); |
michael@0 | 1448 | #ifdef FIXED_POINT |
michael@0 | 1449 | silence = (sample_max==0); |
michael@0 | 1450 | #else |
michael@0 | 1451 | silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth)); |
michael@0 | 1452 | #endif |
michael@0 | 1453 | #ifdef FUZZING |
michael@0 | 1454 | if ((rand()&0x3F)==0) |
michael@0 | 1455 | silence = 1; |
michael@0 | 1456 | #endif |
michael@0 | 1457 | if (tell==1) |
michael@0 | 1458 | ec_enc_bit_logp(enc, silence, 15); |
michael@0 | 1459 | else |
michael@0 | 1460 | silence=0; |
michael@0 | 1461 | if (silence) |
michael@0 | 1462 | { |
michael@0 | 1463 | /*In VBR mode there is no need to send more than the minimum. */ |
michael@0 | 1464 | if (vbr_rate>0) |
michael@0 | 1465 | { |
michael@0 | 1466 | effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2); |
michael@0 | 1467 | total_bits=nbCompressedBytes*8; |
michael@0 | 1468 | nbAvailableBytes=2; |
michael@0 | 1469 | ec_enc_shrink(enc, nbCompressedBytes); |
michael@0 | 1470 | } |
michael@0 | 1471 | /* Pretend we've filled all the remaining bits with zeros |
michael@0 | 1472 | (that's what the initialiser did anyway) */ |
michael@0 | 1473 | tell = nbCompressedBytes*8; |
michael@0 | 1474 | enc->nbits_total+=tell-ec_tell(enc); |
michael@0 | 1475 | } |
michael@0 | 1476 | c=0; do { |
michael@0 | 1477 | celt_preemphasis(pcm+c, in+c*(N+st->overlap)+st->overlap, N, CC, st->upsample, |
michael@0 | 1478 | mode->preemph, st->preemph_memE+c, st->clip); |
michael@0 | 1479 | } while (++c<CC); |
michael@0 | 1480 | |
michael@0 | 1481 | |
michael@0 | 1482 | |
michael@0 | 1483 | /* Find pitch period and gain */ |
michael@0 | 1484 | { |
michael@0 | 1485 | int enabled; |
michael@0 | 1486 | int qg; |
michael@0 | 1487 | enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && st->start==0 && !silence && !st->disable_pf |
michael@0 | 1488 | && st->complexity >= 5 && !(st->consec_transient && LM!=3 && st->variable_duration==OPUS_FRAMESIZE_VARIABLE); |
michael@0 | 1489 | |
michael@0 | 1490 | prefilter_tapset = st->tapset_decision; |
michael@0 | 1491 | pf_on = run_prefilter(st, in, prefilter_mem, CC, N, prefilter_tapset, &pitch_index, &gain1, &qg, enabled, nbAvailableBytes); |
michael@0 | 1492 | if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && (!st->analysis.valid || st->analysis.tonality > .3) |
michael@0 | 1493 | && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st->prefilter_period)) |
michael@0 | 1494 | pitch_change = 1; |
michael@0 | 1495 | if (pf_on==0) |
michael@0 | 1496 | { |
michael@0 | 1497 | if(st->start==0 && tell+16<=total_bits) |
michael@0 | 1498 | ec_enc_bit_logp(enc, 0, 1); |
michael@0 | 1499 | } else { |
michael@0 | 1500 | /*This block is not gated by a total bits check only because |
michael@0 | 1501 | of the nbAvailableBytes check above.*/ |
michael@0 | 1502 | int octave; |
michael@0 | 1503 | ec_enc_bit_logp(enc, 1, 1); |
michael@0 | 1504 | pitch_index += 1; |
michael@0 | 1505 | octave = EC_ILOG(pitch_index)-5; |
michael@0 | 1506 | ec_enc_uint(enc, octave, 6); |
michael@0 | 1507 | ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave); |
michael@0 | 1508 | pitch_index -= 1; |
michael@0 | 1509 | ec_enc_bits(enc, qg, 3); |
michael@0 | 1510 | ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2); |
michael@0 | 1511 | } |
michael@0 | 1512 | } |
michael@0 | 1513 | |
michael@0 | 1514 | isTransient = 0; |
michael@0 | 1515 | shortBlocks = 0; |
michael@0 | 1516 | if (st->complexity >= 1 && !st->lfe) |
michael@0 | 1517 | { |
michael@0 | 1518 | isTransient = transient_analysis(in, N+st->overlap, CC, |
michael@0 | 1519 | &tf_estimate, &tf_chan); |
michael@0 | 1520 | } |
michael@0 | 1521 | if (LM>0 && ec_tell(enc)+3<=total_bits) |
michael@0 | 1522 | { |
michael@0 | 1523 | if (isTransient) |
michael@0 | 1524 | shortBlocks = M; |
michael@0 | 1525 | } else { |
michael@0 | 1526 | isTransient = 0; |
michael@0 | 1527 | transient_got_disabled=1; |
michael@0 | 1528 | } |
michael@0 | 1529 | |
michael@0 | 1530 | ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */ |
michael@0 | 1531 | ALLOC(bandE,nbEBands*CC, celt_ener); |
michael@0 | 1532 | ALLOC(bandLogE,nbEBands*CC, opus_val16); |
michael@0 | 1533 | |
michael@0 | 1534 | secondMdct = shortBlocks && st->complexity>=8; |
michael@0 | 1535 | ALLOC(bandLogE2, C*nbEBands, opus_val16); |
michael@0 | 1536 | if (secondMdct) |
michael@0 | 1537 | { |
michael@0 | 1538 | compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample); |
michael@0 | 1539 | compute_band_energies(mode, freq, bandE, effEnd, C, M); |
michael@0 | 1540 | amp2Log2(mode, effEnd, st->end, bandE, bandLogE2, C); |
michael@0 | 1541 | for (i=0;i<C*nbEBands;i++) |
michael@0 | 1542 | bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT)); |
michael@0 | 1543 | } |
michael@0 | 1544 | |
michael@0 | 1545 | compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample); |
michael@0 | 1546 | if (CC==2&&C==1) |
michael@0 | 1547 | tf_chan = 0; |
michael@0 | 1548 | compute_band_energies(mode, freq, bandE, effEnd, C, M); |
michael@0 | 1549 | |
michael@0 | 1550 | if (st->lfe) |
michael@0 | 1551 | { |
michael@0 | 1552 | for (i=2;i<st->end;i++) |
michael@0 | 1553 | { |
michael@0 | 1554 | bandE[i] = IMIN(bandE[i], MULT16_32_Q15(QCONST16(1e-4f,15),bandE[0])); |
michael@0 | 1555 | bandE[i] = MAX32(bandE[i], EPSILON); |
michael@0 | 1556 | } |
michael@0 | 1557 | } |
michael@0 | 1558 | amp2Log2(mode, effEnd, st->end, bandE, bandLogE, C); |
michael@0 | 1559 | |
michael@0 | 1560 | ALLOC(surround_dynalloc, C*nbEBands, opus_val16); |
michael@0 | 1561 | for(i=0;i<st->end;i++) |
michael@0 | 1562 | surround_dynalloc[i] = 0; |
michael@0 | 1563 | /* This computes how much masking takes place between surround channels */ |
michael@0 | 1564 | if (st->start==0&&st->energy_mask&&!st->lfe) |
michael@0 | 1565 | { |
michael@0 | 1566 | int mask_end; |
michael@0 | 1567 | int midband; |
michael@0 | 1568 | int count_dynalloc; |
michael@0 | 1569 | opus_val32 mask_avg=0; |
michael@0 | 1570 | opus_val32 diff=0; |
michael@0 | 1571 | int count=0; |
michael@0 | 1572 | mask_end = IMAX(2,st->lastCodedBands); |
michael@0 | 1573 | for (c=0;c<C;c++) |
michael@0 | 1574 | { |
michael@0 | 1575 | for(i=0;i<mask_end;i++) |
michael@0 | 1576 | { |
michael@0 | 1577 | opus_val16 mask; |
michael@0 | 1578 | mask = MAX16(MIN16(st->energy_mask[nbEBands*c+i], |
michael@0 | 1579 | QCONST16(.25f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT)); |
michael@0 | 1580 | if (mask > 0) |
michael@0 | 1581 | mask = HALF16(mask); |
michael@0 | 1582 | mask_avg += MULT16_16(mask, eBands[i+1]-eBands[i]); |
michael@0 | 1583 | count += eBands[i+1]-eBands[i]; |
michael@0 | 1584 | diff += MULT16_16(mask, 1+2*i-mask_end); |
michael@0 | 1585 | } |
michael@0 | 1586 | } |
michael@0 | 1587 | mask_avg = DIV32_16(mask_avg,count); |
michael@0 | 1588 | mask_avg += QCONST16(.2f, DB_SHIFT); |
michael@0 | 1589 | diff = diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end); |
michael@0 | 1590 | /* Again, being conservative */ |
michael@0 | 1591 | diff = HALF32(diff); |
michael@0 | 1592 | diff = MAX32(MIN32(diff, QCONST32(.031f, DB_SHIFT)), -QCONST32(.031f, DB_SHIFT)); |
michael@0 | 1593 | /* Find the band that's in the middle of the coded spectrum */ |
michael@0 | 1594 | for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++); |
michael@0 | 1595 | count_dynalloc=0; |
michael@0 | 1596 | for(i=0;i<mask_end;i++) |
michael@0 | 1597 | { |
michael@0 | 1598 | opus_val32 lin; |
michael@0 | 1599 | opus_val16 unmask; |
michael@0 | 1600 | lin = mask_avg + diff*(i-midband); |
michael@0 | 1601 | if (C==2) |
michael@0 | 1602 | unmask = MAX16(st->energy_mask[i], st->energy_mask[nbEBands+i]); |
michael@0 | 1603 | else |
michael@0 | 1604 | unmask = st->energy_mask[i]; |
michael@0 | 1605 | unmask = MIN16(unmask, QCONST16(.0f, DB_SHIFT)); |
michael@0 | 1606 | unmask -= lin; |
michael@0 | 1607 | if (unmask > QCONST16(.25f, DB_SHIFT)) |
michael@0 | 1608 | { |
michael@0 | 1609 | surround_dynalloc[i] = unmask - QCONST16(.25f, DB_SHIFT); |
michael@0 | 1610 | count_dynalloc++; |
michael@0 | 1611 | } |
michael@0 | 1612 | } |
michael@0 | 1613 | if (count_dynalloc>=3) |
michael@0 | 1614 | { |
michael@0 | 1615 | /* If we need dynalloc in many bands, it's probably because our |
michael@0 | 1616 | initial masking rate was too low. */ |
michael@0 | 1617 | mask_avg += QCONST16(.25f, DB_SHIFT); |
michael@0 | 1618 | if (mask_avg>0) |
michael@0 | 1619 | { |
michael@0 | 1620 | /* Something went really wrong in the original calculations, |
michael@0 | 1621 | disabling masking. */ |
michael@0 | 1622 | mask_avg = 0; |
michael@0 | 1623 | diff = 0; |
michael@0 | 1624 | for(i=0;i<mask_end;i++) |
michael@0 | 1625 | surround_dynalloc[i] = 0; |
michael@0 | 1626 | } else { |
michael@0 | 1627 | for(i=0;i<mask_end;i++) |
michael@0 | 1628 | surround_dynalloc[i] = MAX16(0, surround_dynalloc[i]-QCONST16(.25f, DB_SHIFT)); |
michael@0 | 1629 | } |
michael@0 | 1630 | } |
michael@0 | 1631 | mask_avg += QCONST16(.2f, DB_SHIFT); |
michael@0 | 1632 | /* Convert to 1/64th units used for the trim */ |
michael@0 | 1633 | surround_trim = 64*diff; |
michael@0 | 1634 | /*printf("%d %d ", mask_avg, surround_trim);*/ |
michael@0 | 1635 | surround_masking = mask_avg; |
michael@0 | 1636 | } |
michael@0 | 1637 | /* Temporal VBR (but not for LFE) */ |
michael@0 | 1638 | if (!st->lfe) |
michael@0 | 1639 | { |
michael@0 | 1640 | opus_val16 follow=-QCONST16(10.0f,DB_SHIFT); |
michael@0 | 1641 | opus_val32 frame_avg=0; |
michael@0 | 1642 | opus_val16 offset = shortBlocks?HALF16(SHL16(LM, DB_SHIFT)):0; |
michael@0 | 1643 | for(i=st->start;i<st->end;i++) |
michael@0 | 1644 | { |
michael@0 | 1645 | follow = MAX16(follow-QCONST16(1.f, DB_SHIFT), bandLogE[i]-offset); |
michael@0 | 1646 | if (C==2) |
michael@0 | 1647 | follow = MAX16(follow, bandLogE[i+nbEBands]-offset); |
michael@0 | 1648 | frame_avg += follow; |
michael@0 | 1649 | } |
michael@0 | 1650 | frame_avg /= (st->end-st->start); |
michael@0 | 1651 | temporal_vbr = SUB16(frame_avg,st->spec_avg); |
michael@0 | 1652 | temporal_vbr = MIN16(QCONST16(3.f, DB_SHIFT), MAX16(-QCONST16(1.5f, DB_SHIFT), temporal_vbr)); |
michael@0 | 1653 | st->spec_avg += MULT16_16_Q15(QCONST16(.02f, 15), temporal_vbr); |
michael@0 | 1654 | } |
michael@0 | 1655 | /*for (i=0;i<21;i++) |
michael@0 | 1656 | printf("%f ", bandLogE[i]); |
michael@0 | 1657 | printf("\n");*/ |
michael@0 | 1658 | |
michael@0 | 1659 | if (!secondMdct) |
michael@0 | 1660 | { |
michael@0 | 1661 | for (i=0;i<C*nbEBands;i++) |
michael@0 | 1662 | bandLogE2[i] = bandLogE[i]; |
michael@0 | 1663 | } |
michael@0 | 1664 | |
michael@0 | 1665 | /* Last chance to catch any transient we might have missed in the |
michael@0 | 1666 | time-domain analysis */ |
michael@0 | 1667 | if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5 && !st->lfe) |
michael@0 | 1668 | { |
michael@0 | 1669 | if (patch_transient_decision(bandLogE, oldBandE, nbEBands, st->end, C)) |
michael@0 | 1670 | { |
michael@0 | 1671 | isTransient = 1; |
michael@0 | 1672 | shortBlocks = M; |
michael@0 | 1673 | compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample); |
michael@0 | 1674 | compute_band_energies(mode, freq, bandE, effEnd, C, M); |
michael@0 | 1675 | amp2Log2(mode, effEnd, st->end, bandE, bandLogE, C); |
michael@0 | 1676 | /* Compensate for the scaling of short vs long mdcts */ |
michael@0 | 1677 | for (i=0;i<C*nbEBands;i++) |
michael@0 | 1678 | bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT)); |
michael@0 | 1679 | tf_estimate = QCONST16(.2f,14); |
michael@0 | 1680 | } |
michael@0 | 1681 | } |
michael@0 | 1682 | |
michael@0 | 1683 | if (LM>0 && ec_tell(enc)+3<=total_bits) |
michael@0 | 1684 | ec_enc_bit_logp(enc, isTransient, 3); |
michael@0 | 1685 | |
michael@0 | 1686 | ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */ |
michael@0 | 1687 | |
michael@0 | 1688 | /* Band normalisation */ |
michael@0 | 1689 | normalise_bands(mode, freq, X, bandE, effEnd, C, M); |
michael@0 | 1690 | |
michael@0 | 1691 | ALLOC(tf_res, nbEBands, int); |
michael@0 | 1692 | /* Disable variable tf resolution for hybrid and at very low bitrate */ |
michael@0 | 1693 | if (effectiveBytes>=15*C && st->start==0 && st->complexity>=2 && !st->lfe) |
michael@0 | 1694 | { |
michael@0 | 1695 | int lambda; |
michael@0 | 1696 | if (effectiveBytes<40) |
michael@0 | 1697 | lambda = 12; |
michael@0 | 1698 | else if (effectiveBytes<60) |
michael@0 | 1699 | lambda = 6; |
michael@0 | 1700 | else if (effectiveBytes<100) |
michael@0 | 1701 | lambda = 4; |
michael@0 | 1702 | else |
michael@0 | 1703 | lambda = 3; |
michael@0 | 1704 | lambda*=2; |
michael@0 | 1705 | tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, LM, &tf_sum, tf_estimate, tf_chan); |
michael@0 | 1706 | for (i=effEnd;i<st->end;i++) |
michael@0 | 1707 | tf_res[i] = tf_res[effEnd-1]; |
michael@0 | 1708 | } else { |
michael@0 | 1709 | tf_sum = 0; |
michael@0 | 1710 | for (i=0;i<st->end;i++) |
michael@0 | 1711 | tf_res[i] = isTransient; |
michael@0 | 1712 | tf_select=0; |
michael@0 | 1713 | } |
michael@0 | 1714 | |
michael@0 | 1715 | ALLOC(error, C*nbEBands, opus_val16); |
michael@0 | 1716 | quant_coarse_energy(mode, st->start, st->end, effEnd, bandLogE, |
michael@0 | 1717 | oldBandE, total_bits, error, enc, |
michael@0 | 1718 | C, LM, nbAvailableBytes, st->force_intra, |
michael@0 | 1719 | &st->delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe); |
michael@0 | 1720 | |
michael@0 | 1721 | tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc); |
michael@0 | 1722 | |
michael@0 | 1723 | if (ec_tell(enc)+4<=total_bits) |
michael@0 | 1724 | { |
michael@0 | 1725 | if (st->lfe) |
michael@0 | 1726 | { |
michael@0 | 1727 | st->tapset_decision = 0; |
michael@0 | 1728 | st->spread_decision = SPREAD_NORMAL; |
michael@0 | 1729 | } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C || st->start != 0) |
michael@0 | 1730 | { |
michael@0 | 1731 | if (st->complexity == 0) |
michael@0 | 1732 | st->spread_decision = SPREAD_NONE; |
michael@0 | 1733 | else |
michael@0 | 1734 | st->spread_decision = SPREAD_NORMAL; |
michael@0 | 1735 | } else { |
michael@0 | 1736 | /* Disable new spreading+tapset estimator until we can show it works |
michael@0 | 1737 | better than the old one. So far it seems like spreading_decision() |
michael@0 | 1738 | works best. */ |
michael@0 | 1739 | #if 0 |
michael@0 | 1740 | if (st->analysis.valid) |
michael@0 | 1741 | { |
michael@0 | 1742 | static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)}; |
michael@0 | 1743 | static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)}; |
michael@0 | 1744 | static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), QCONST16(.15f, 15)}; |
michael@0 | 1745 | static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), QCONST16(.05f, 15)}; |
michael@0 | 1746 | st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision); |
michael@0 | 1747 | st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision); |
michael@0 | 1748 | } else |
michael@0 | 1749 | #endif |
michael@0 | 1750 | { |
michael@0 | 1751 | st->spread_decision = spreading_decision(mode, X, |
michael@0 | 1752 | &st->tonal_average, st->spread_decision, &st->hf_average, |
michael@0 | 1753 | &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M); |
michael@0 | 1754 | } |
michael@0 | 1755 | /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/ |
michael@0 | 1756 | /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/ |
michael@0 | 1757 | } |
michael@0 | 1758 | ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5); |
michael@0 | 1759 | } |
michael@0 | 1760 | |
michael@0 | 1761 | ALLOC(offsets, nbEBands, int); |
michael@0 | 1762 | |
michael@0 | 1763 | maxDepth = dynalloc_analysis(bandLogE, bandLogE2, nbEBands, st->start, st->end, C, offsets, |
michael@0 | 1764 | st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr, |
michael@0 | 1765 | eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc); |
michael@0 | 1766 | /* For LFE, everything interesting is in the first band */ |
michael@0 | 1767 | if (st->lfe) |
michael@0 | 1768 | offsets[0] = IMIN(8, effectiveBytes/3); |
michael@0 | 1769 | ALLOC(cap, nbEBands, int); |
michael@0 | 1770 | init_caps(mode,cap,LM,C); |
michael@0 | 1771 | |
michael@0 | 1772 | dynalloc_logp = 6; |
michael@0 | 1773 | total_bits<<=BITRES; |
michael@0 | 1774 | total_boost = 0; |
michael@0 | 1775 | tell = ec_tell_frac(enc); |
michael@0 | 1776 | for (i=st->start;i<st->end;i++) |
michael@0 | 1777 | { |
michael@0 | 1778 | int width, quanta; |
michael@0 | 1779 | int dynalloc_loop_logp; |
michael@0 | 1780 | int boost; |
michael@0 | 1781 | int j; |
michael@0 | 1782 | width = C*(eBands[i+1]-eBands[i])<<LM; |
michael@0 | 1783 | /* quanta is 6 bits, but no more than 1 bit/sample |
michael@0 | 1784 | and no less than 1/8 bit/sample */ |
michael@0 | 1785 | quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width)); |
michael@0 | 1786 | dynalloc_loop_logp = dynalloc_logp; |
michael@0 | 1787 | boost = 0; |
michael@0 | 1788 | for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost |
michael@0 | 1789 | && boost < cap[i]; j++) |
michael@0 | 1790 | { |
michael@0 | 1791 | int flag; |
michael@0 | 1792 | flag = j<offsets[i]; |
michael@0 | 1793 | ec_enc_bit_logp(enc, flag, dynalloc_loop_logp); |
michael@0 | 1794 | tell = ec_tell_frac(enc); |
michael@0 | 1795 | if (!flag) |
michael@0 | 1796 | break; |
michael@0 | 1797 | boost += quanta; |
michael@0 | 1798 | total_boost += quanta; |
michael@0 | 1799 | dynalloc_loop_logp = 1; |
michael@0 | 1800 | } |
michael@0 | 1801 | /* Making dynalloc more likely */ |
michael@0 | 1802 | if (j) |
michael@0 | 1803 | dynalloc_logp = IMAX(2, dynalloc_logp-1); |
michael@0 | 1804 | offsets[i] = boost; |
michael@0 | 1805 | } |
michael@0 | 1806 | |
michael@0 | 1807 | if (C==2) |
michael@0 | 1808 | { |
michael@0 | 1809 | static const opus_val16 intensity_thresholds[21]= |
michael@0 | 1810 | /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 off*/ |
michael@0 | 1811 | { 1, 2, 3, 4, 5, 6, 7, 8,16,24,36,44,50,56,62,67,72,79,88,106,134}; |
michael@0 | 1812 | static const opus_val16 intensity_histeresis[21]= |
michael@0 | 1813 | { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6, 8, 8}; |
michael@0 | 1814 | |
michael@0 | 1815 | /* Always use MS for 2.5 ms frames until we can do a better analysis */ |
michael@0 | 1816 | if (LM!=0) |
michael@0 | 1817 | dual_stereo = stereo_analysis(mode, X, LM, N); |
michael@0 | 1818 | |
michael@0 | 1819 | st->intensity = hysteresis_decision((opus_val16)(equiv_rate/1000), |
michael@0 | 1820 | intensity_thresholds, intensity_histeresis, 21, st->intensity); |
michael@0 | 1821 | st->intensity = IMIN(st->end,IMAX(st->start, st->intensity)); |
michael@0 | 1822 | } |
michael@0 | 1823 | |
michael@0 | 1824 | alloc_trim = 5; |
michael@0 | 1825 | if (tell+(6<<BITRES) <= total_bits - total_boost) |
michael@0 | 1826 | { |
michael@0 | 1827 | if (st->lfe) |
michael@0 | 1828 | alloc_trim = 5; |
michael@0 | 1829 | else |
michael@0 | 1830 | alloc_trim = alloc_trim_analysis(mode, X, bandLogE, |
michael@0 | 1831 | st->end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate, st->intensity, surround_trim); |
michael@0 | 1832 | ec_enc_icdf(enc, alloc_trim, trim_icdf, 7); |
michael@0 | 1833 | tell = ec_tell_frac(enc); |
michael@0 | 1834 | } |
michael@0 | 1835 | |
michael@0 | 1836 | /* Variable bitrate */ |
michael@0 | 1837 | if (vbr_rate>0) |
michael@0 | 1838 | { |
michael@0 | 1839 | opus_val16 alpha; |
michael@0 | 1840 | opus_int32 delta; |
michael@0 | 1841 | /* The target rate in 8th bits per frame */ |
michael@0 | 1842 | opus_int32 target, base_target; |
michael@0 | 1843 | opus_int32 min_allowed; |
michael@0 | 1844 | int lm_diff = mode->maxLM - LM; |
michael@0 | 1845 | |
michael@0 | 1846 | /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms. |
michael@0 | 1847 | The CELT allocator will just not be able to use more than that anyway. */ |
michael@0 | 1848 | nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM)); |
michael@0 | 1849 | base_target = vbr_rate - ((40*C+20)<<BITRES); |
michael@0 | 1850 | |
michael@0 | 1851 | if (st->constrained_vbr) |
michael@0 | 1852 | base_target += (st->vbr_offset>>lm_diff); |
michael@0 | 1853 | |
michael@0 | 1854 | target = compute_vbr(mode, &st->analysis, base_target, LM, equiv_rate, |
michael@0 | 1855 | st->lastCodedBands, C, st->intensity, st->constrained_vbr, |
michael@0 | 1856 | st->stereo_saving, tot_boost, tf_estimate, pitch_change, maxDepth, |
michael@0 | 1857 | st->variable_duration, st->lfe, st->energy_mask!=NULL, surround_masking, |
michael@0 | 1858 | temporal_vbr); |
michael@0 | 1859 | |
michael@0 | 1860 | /* The current offset is removed from the target and the space used |
michael@0 | 1861 | so far is added*/ |
michael@0 | 1862 | target=target+tell; |
michael@0 | 1863 | /* In VBR mode the frame size must not be reduced so much that it would |
michael@0 | 1864 | result in the encoder running out of bits. |
michael@0 | 1865 | The margin of 2 bytes ensures that none of the bust-prevention logic |
michael@0 | 1866 | in the decoder will have triggered so far. */ |
michael@0 | 1867 | min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes; |
michael@0 | 1868 | |
michael@0 | 1869 | nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3); |
michael@0 | 1870 | nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes); |
michael@0 | 1871 | nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes; |
michael@0 | 1872 | |
michael@0 | 1873 | /* By how much did we "miss" the target on that frame */ |
michael@0 | 1874 | delta = target - vbr_rate; |
michael@0 | 1875 | |
michael@0 | 1876 | target=nbAvailableBytes<<(BITRES+3); |
michael@0 | 1877 | |
michael@0 | 1878 | /*If the frame is silent we don't adjust our drift, otherwise |
michael@0 | 1879 | the encoder will shoot to very high rates after hitting a |
michael@0 | 1880 | span of silence, but we do allow the bitres to refill. |
michael@0 | 1881 | This means that we'll undershoot our target in CVBR/VBR modes |
michael@0 | 1882 | on files with lots of silence. */ |
michael@0 | 1883 | if(silence) |
michael@0 | 1884 | { |
michael@0 | 1885 | nbAvailableBytes = 2; |
michael@0 | 1886 | target = 2*8<<BITRES; |
michael@0 | 1887 | delta = 0; |
michael@0 | 1888 | } |
michael@0 | 1889 | |
michael@0 | 1890 | if (st->vbr_count < 970) |
michael@0 | 1891 | { |
michael@0 | 1892 | st->vbr_count++; |
michael@0 | 1893 | alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16)); |
michael@0 | 1894 | } else |
michael@0 | 1895 | alpha = QCONST16(.001f,15); |
michael@0 | 1896 | /* How many bits have we used in excess of what we're allowed */ |
michael@0 | 1897 | if (st->constrained_vbr) |
michael@0 | 1898 | st->vbr_reservoir += target - vbr_rate; |
michael@0 | 1899 | /*printf ("%d\n", st->vbr_reservoir);*/ |
michael@0 | 1900 | |
michael@0 | 1901 | /* Compute the offset we need to apply in order to reach the target */ |
michael@0 | 1902 | if (st->constrained_vbr) |
michael@0 | 1903 | { |
michael@0 | 1904 | st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift); |
michael@0 | 1905 | st->vbr_offset = -st->vbr_drift; |
michael@0 | 1906 | } |
michael@0 | 1907 | /*printf ("%d\n", st->vbr_drift);*/ |
michael@0 | 1908 | |
michael@0 | 1909 | if (st->constrained_vbr && st->vbr_reservoir < 0) |
michael@0 | 1910 | { |
michael@0 | 1911 | /* We're under the min value -- increase rate */ |
michael@0 | 1912 | int adjust = (-st->vbr_reservoir)/(8<<BITRES); |
michael@0 | 1913 | /* Unless we're just coding silence */ |
michael@0 | 1914 | nbAvailableBytes += silence?0:adjust; |
michael@0 | 1915 | st->vbr_reservoir = 0; |
michael@0 | 1916 | /*printf ("+%d\n", adjust);*/ |
michael@0 | 1917 | } |
michael@0 | 1918 | nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes); |
michael@0 | 1919 | /*printf("%d\n", nbCompressedBytes*50*8);*/ |
michael@0 | 1920 | /* This moves the raw bits to take into account the new compressed size */ |
michael@0 | 1921 | ec_enc_shrink(enc, nbCompressedBytes); |
michael@0 | 1922 | } |
michael@0 | 1923 | |
michael@0 | 1924 | /* Bit allocation */ |
michael@0 | 1925 | ALLOC(fine_quant, nbEBands, int); |
michael@0 | 1926 | ALLOC(pulses, nbEBands, int); |
michael@0 | 1927 | ALLOC(fine_priority, nbEBands, int); |
michael@0 | 1928 | |
michael@0 | 1929 | /* bits = packet size - where we are - safety*/ |
michael@0 | 1930 | bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1; |
michael@0 | 1931 | anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0; |
michael@0 | 1932 | bits -= anti_collapse_rsv; |
michael@0 | 1933 | signalBandwidth = st->end-1; |
michael@0 | 1934 | #ifndef DISABLE_FLOAT_API |
michael@0 | 1935 | if (st->analysis.valid) |
michael@0 | 1936 | { |
michael@0 | 1937 | int min_bandwidth; |
michael@0 | 1938 | if (equiv_rate < (opus_int32)32000*C) |
michael@0 | 1939 | min_bandwidth = 13; |
michael@0 | 1940 | else if (equiv_rate < (opus_int32)48000*C) |
michael@0 | 1941 | min_bandwidth = 16; |
michael@0 | 1942 | else if (equiv_rate < (opus_int32)60000*C) |
michael@0 | 1943 | min_bandwidth = 18; |
michael@0 | 1944 | else if (equiv_rate < (opus_int32)80000*C) |
michael@0 | 1945 | min_bandwidth = 19; |
michael@0 | 1946 | else |
michael@0 | 1947 | min_bandwidth = 20; |
michael@0 | 1948 | signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth); |
michael@0 | 1949 | } |
michael@0 | 1950 | #endif |
michael@0 | 1951 | if (st->lfe) |
michael@0 | 1952 | signalBandwidth = 1; |
michael@0 | 1953 | codedBands = compute_allocation(mode, st->start, st->end, offsets, cap, |
michael@0 | 1954 | alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses, |
michael@0 | 1955 | fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBandwidth); |
michael@0 | 1956 | if (st->lastCodedBands) |
michael@0 | 1957 | st->lastCodedBands = IMIN(st->lastCodedBands+1,IMAX(st->lastCodedBands-1,codedBands)); |
michael@0 | 1958 | else |
michael@0 | 1959 | st->lastCodedBands = codedBands; |
michael@0 | 1960 | |
michael@0 | 1961 | quant_fine_energy(mode, st->start, st->end, oldBandE, error, fine_quant, enc, C); |
michael@0 | 1962 | |
michael@0 | 1963 | /* Residual quantisation */ |
michael@0 | 1964 | ALLOC(collapse_masks, C*nbEBands, unsigned char); |
michael@0 | 1965 | quant_all_bands(1, mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks, |
michael@0 | 1966 | bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, st->intensity, tf_res, |
michael@0 | 1967 | nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng); |
michael@0 | 1968 | |
michael@0 | 1969 | if (anti_collapse_rsv > 0) |
michael@0 | 1970 | { |
michael@0 | 1971 | anti_collapse_on = st->consec_transient<2; |
michael@0 | 1972 | #ifdef FUZZING |
michael@0 | 1973 | anti_collapse_on = rand()&0x1; |
michael@0 | 1974 | #endif |
michael@0 | 1975 | ec_enc_bits(enc, anti_collapse_on, 1); |
michael@0 | 1976 | } |
michael@0 | 1977 | quant_energy_finalise(mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C); |
michael@0 | 1978 | |
michael@0 | 1979 | if (silence) |
michael@0 | 1980 | { |
michael@0 | 1981 | for (i=0;i<C*nbEBands;i++) |
michael@0 | 1982 | oldBandE[i] = -QCONST16(28.f,DB_SHIFT); |
michael@0 | 1983 | } |
michael@0 | 1984 | |
michael@0 | 1985 | #ifdef RESYNTH |
michael@0 | 1986 | /* Re-synthesis of the coded audio if required */ |
michael@0 | 1987 | { |
michael@0 | 1988 | celt_sig *out_mem[2]; |
michael@0 | 1989 | |
michael@0 | 1990 | if (anti_collapse_on) |
michael@0 | 1991 | { |
michael@0 | 1992 | anti_collapse(mode, X, collapse_masks, LM, C, N, |
michael@0 | 1993 | st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng); |
michael@0 | 1994 | } |
michael@0 | 1995 | |
michael@0 | 1996 | if (silence) |
michael@0 | 1997 | { |
michael@0 | 1998 | for (i=0;i<C*N;i++) |
michael@0 | 1999 | freq[i] = 0; |
michael@0 | 2000 | } else { |
michael@0 | 2001 | /* Synthesis */ |
michael@0 | 2002 | denormalise_bands(mode, X, freq, oldBandE, st->start, effEnd, C, M); |
michael@0 | 2003 | } |
michael@0 | 2004 | |
michael@0 | 2005 | c=0; do { |
michael@0 | 2006 | OPUS_MOVE(st->syn_mem[c], st->syn_mem[c]+N, 2*MAX_PERIOD-N+overlap/2); |
michael@0 | 2007 | } while (++c<CC); |
michael@0 | 2008 | |
michael@0 | 2009 | if (CC==2&&C==1) |
michael@0 | 2010 | { |
michael@0 | 2011 | for (i=0;i<N;i++) |
michael@0 | 2012 | freq[N+i] = freq[i]; |
michael@0 | 2013 | } |
michael@0 | 2014 | |
michael@0 | 2015 | c=0; do { |
michael@0 | 2016 | out_mem[c] = st->syn_mem[c]+2*MAX_PERIOD-N; |
michael@0 | 2017 | } while (++c<CC); |
michael@0 | 2018 | |
michael@0 | 2019 | compute_inv_mdcts(mode, shortBlocks, freq, out_mem, CC, LM); |
michael@0 | 2020 | |
michael@0 | 2021 | c=0; do { |
michael@0 | 2022 | st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD); |
michael@0 | 2023 | st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD); |
michael@0 | 2024 | comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, mode->shortMdctSize, |
michael@0 | 2025 | st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset, |
michael@0 | 2026 | mode->window, st->overlap); |
michael@0 | 2027 | if (LM!=0) |
michael@0 | 2028 | comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMdctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize, |
michael@0 | 2029 | st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset, |
michael@0 | 2030 | mode->window, overlap); |
michael@0 | 2031 | } while (++c<CC); |
michael@0 | 2032 | |
michael@0 | 2033 | /* We reuse freq[] as scratch space for the de-emphasis */ |
michael@0 | 2034 | deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, mode->preemph, st->preemph_memD, freq); |
michael@0 | 2035 | st->prefilter_period_old = st->prefilter_period; |
michael@0 | 2036 | st->prefilter_gain_old = st->prefilter_gain; |
michael@0 | 2037 | st->prefilter_tapset_old = st->prefilter_tapset; |
michael@0 | 2038 | } |
michael@0 | 2039 | #endif |
michael@0 | 2040 | |
michael@0 | 2041 | st->prefilter_period = pitch_index; |
michael@0 | 2042 | st->prefilter_gain = gain1; |
michael@0 | 2043 | st->prefilter_tapset = prefilter_tapset; |
michael@0 | 2044 | #ifdef RESYNTH |
michael@0 | 2045 | if (LM!=0) |
michael@0 | 2046 | { |
michael@0 | 2047 | st->prefilter_period_old = st->prefilter_period; |
michael@0 | 2048 | st->prefilter_gain_old = st->prefilter_gain; |
michael@0 | 2049 | st->prefilter_tapset_old = st->prefilter_tapset; |
michael@0 | 2050 | } |
michael@0 | 2051 | #endif |
michael@0 | 2052 | |
michael@0 | 2053 | if (CC==2&&C==1) { |
michael@0 | 2054 | for (i=0;i<nbEBands;i++) |
michael@0 | 2055 | oldBandE[nbEBands+i]=oldBandE[i]; |
michael@0 | 2056 | } |
michael@0 | 2057 | |
michael@0 | 2058 | if (!isTransient) |
michael@0 | 2059 | { |
michael@0 | 2060 | for (i=0;i<CC*nbEBands;i++) |
michael@0 | 2061 | oldLogE2[i] = oldLogE[i]; |
michael@0 | 2062 | for (i=0;i<CC*nbEBands;i++) |
michael@0 | 2063 | oldLogE[i] = oldBandE[i]; |
michael@0 | 2064 | } else { |
michael@0 | 2065 | for (i=0;i<CC*nbEBands;i++) |
michael@0 | 2066 | oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]); |
michael@0 | 2067 | } |
michael@0 | 2068 | /* In case start or end were to change */ |
michael@0 | 2069 | c=0; do |
michael@0 | 2070 | { |
michael@0 | 2071 | for (i=0;i<st->start;i++) |
michael@0 | 2072 | { |
michael@0 | 2073 | oldBandE[c*nbEBands+i]=0; |
michael@0 | 2074 | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT); |
michael@0 | 2075 | } |
michael@0 | 2076 | for (i=st->end;i<nbEBands;i++) |
michael@0 | 2077 | { |
michael@0 | 2078 | oldBandE[c*nbEBands+i]=0; |
michael@0 | 2079 | oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT); |
michael@0 | 2080 | } |
michael@0 | 2081 | } while (++c<CC); |
michael@0 | 2082 | |
michael@0 | 2083 | if (isTransient || transient_got_disabled) |
michael@0 | 2084 | st->consec_transient++; |
michael@0 | 2085 | else |
michael@0 | 2086 | st->consec_transient=0; |
michael@0 | 2087 | st->rng = enc->rng; |
michael@0 | 2088 | |
michael@0 | 2089 | /* If there's any room left (can only happen for very high rates), |
michael@0 | 2090 | it's already filled with zeros */ |
michael@0 | 2091 | ec_enc_done(enc); |
michael@0 | 2092 | |
michael@0 | 2093 | #ifdef CUSTOM_MODES |
michael@0 | 2094 | if (st->signalling) |
michael@0 | 2095 | nbCompressedBytes++; |
michael@0 | 2096 | #endif |
michael@0 | 2097 | |
michael@0 | 2098 | RESTORE_STACK; |
michael@0 | 2099 | if (ec_get_error(enc)) |
michael@0 | 2100 | return OPUS_INTERNAL_ERROR; |
michael@0 | 2101 | else |
michael@0 | 2102 | return nbCompressedBytes; |
michael@0 | 2103 | } |
michael@0 | 2104 | |
michael@0 | 2105 | |
michael@0 | 2106 | #ifdef CUSTOM_MODES |
michael@0 | 2107 | |
michael@0 | 2108 | #ifdef FIXED_POINT |
michael@0 | 2109 | int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
michael@0 | 2110 | { |
michael@0 | 2111 | return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL); |
michael@0 | 2112 | } |
michael@0 | 2113 | |
michael@0 | 2114 | #ifndef DISABLE_FLOAT_API |
michael@0 | 2115 | int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
michael@0 | 2116 | { |
michael@0 | 2117 | int j, ret, C, N; |
michael@0 | 2118 | VARDECL(opus_int16, in); |
michael@0 | 2119 | ALLOC_STACK; |
michael@0 | 2120 | |
michael@0 | 2121 | if (pcm==NULL) |
michael@0 | 2122 | return OPUS_BAD_ARG; |
michael@0 | 2123 | |
michael@0 | 2124 | C = st->channels; |
michael@0 | 2125 | N = frame_size; |
michael@0 | 2126 | ALLOC(in, C*N, opus_int16); |
michael@0 | 2127 | |
michael@0 | 2128 | for (j=0;j<C*N;j++) |
michael@0 | 2129 | in[j] = FLOAT2INT16(pcm[j]); |
michael@0 | 2130 | |
michael@0 | 2131 | ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL); |
michael@0 | 2132 | #ifdef RESYNTH |
michael@0 | 2133 | for (j=0;j<C*N;j++) |
michael@0 | 2134 | ((float*)pcm)[j]=in[j]*(1.f/32768.f); |
michael@0 | 2135 | #endif |
michael@0 | 2136 | RESTORE_STACK; |
michael@0 | 2137 | return ret; |
michael@0 | 2138 | } |
michael@0 | 2139 | #endif /* DISABLE_FLOAT_API */ |
michael@0 | 2140 | #else |
michael@0 | 2141 | |
michael@0 | 2142 | int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
michael@0 | 2143 | { |
michael@0 | 2144 | int j, ret, C, N; |
michael@0 | 2145 | VARDECL(celt_sig, in); |
michael@0 | 2146 | ALLOC_STACK; |
michael@0 | 2147 | |
michael@0 | 2148 | if (pcm==NULL) |
michael@0 | 2149 | return OPUS_BAD_ARG; |
michael@0 | 2150 | |
michael@0 | 2151 | C=st->channels; |
michael@0 | 2152 | N=frame_size; |
michael@0 | 2153 | ALLOC(in, C*N, celt_sig); |
michael@0 | 2154 | for (j=0;j<C*N;j++) { |
michael@0 | 2155 | in[j] = SCALEOUT(pcm[j]); |
michael@0 | 2156 | } |
michael@0 | 2157 | |
michael@0 | 2158 | ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL); |
michael@0 | 2159 | #ifdef RESYNTH |
michael@0 | 2160 | for (j=0;j<C*N;j++) |
michael@0 | 2161 | ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]); |
michael@0 | 2162 | #endif |
michael@0 | 2163 | RESTORE_STACK; |
michael@0 | 2164 | return ret; |
michael@0 | 2165 | } |
michael@0 | 2166 | |
michael@0 | 2167 | int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes) |
michael@0 | 2168 | { |
michael@0 | 2169 | return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL); |
michael@0 | 2170 | } |
michael@0 | 2171 | |
michael@0 | 2172 | #endif |
michael@0 | 2173 | |
michael@0 | 2174 | #endif /* CUSTOM_MODES */ |
michael@0 | 2175 | |
michael@0 | 2176 | int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...) |
michael@0 | 2177 | { |
michael@0 | 2178 | va_list ap; |
michael@0 | 2179 | |
michael@0 | 2180 | va_start(ap, request); |
michael@0 | 2181 | switch (request) |
michael@0 | 2182 | { |
michael@0 | 2183 | case OPUS_SET_COMPLEXITY_REQUEST: |
michael@0 | 2184 | { |
michael@0 | 2185 | int value = va_arg(ap, opus_int32); |
michael@0 | 2186 | if (value<0 || value>10) |
michael@0 | 2187 | goto bad_arg; |
michael@0 | 2188 | st->complexity = value; |
michael@0 | 2189 | } |
michael@0 | 2190 | break; |
michael@0 | 2191 | case CELT_SET_START_BAND_REQUEST: |
michael@0 | 2192 | { |
michael@0 | 2193 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2194 | if (value<0 || value>=st->mode->nbEBands) |
michael@0 | 2195 | goto bad_arg; |
michael@0 | 2196 | st->start = value; |
michael@0 | 2197 | } |
michael@0 | 2198 | break; |
michael@0 | 2199 | case CELT_SET_END_BAND_REQUEST: |
michael@0 | 2200 | { |
michael@0 | 2201 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2202 | if (value<1 || value>st->mode->nbEBands) |
michael@0 | 2203 | goto bad_arg; |
michael@0 | 2204 | st->end = value; |
michael@0 | 2205 | } |
michael@0 | 2206 | break; |
michael@0 | 2207 | case CELT_SET_PREDICTION_REQUEST: |
michael@0 | 2208 | { |
michael@0 | 2209 | int value = va_arg(ap, opus_int32); |
michael@0 | 2210 | if (value<0 || value>2) |
michael@0 | 2211 | goto bad_arg; |
michael@0 | 2212 | st->disable_pf = value<=1; |
michael@0 | 2213 | st->force_intra = value==0; |
michael@0 | 2214 | } |
michael@0 | 2215 | break; |
michael@0 | 2216 | case OPUS_SET_PACKET_LOSS_PERC_REQUEST: |
michael@0 | 2217 | { |
michael@0 | 2218 | int value = va_arg(ap, opus_int32); |
michael@0 | 2219 | if (value<0 || value>100) |
michael@0 | 2220 | goto bad_arg; |
michael@0 | 2221 | st->loss_rate = value; |
michael@0 | 2222 | } |
michael@0 | 2223 | break; |
michael@0 | 2224 | case OPUS_SET_VBR_CONSTRAINT_REQUEST: |
michael@0 | 2225 | { |
michael@0 | 2226 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2227 | st->constrained_vbr = value; |
michael@0 | 2228 | } |
michael@0 | 2229 | break; |
michael@0 | 2230 | case OPUS_SET_VBR_REQUEST: |
michael@0 | 2231 | { |
michael@0 | 2232 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2233 | st->vbr = value; |
michael@0 | 2234 | } |
michael@0 | 2235 | break; |
michael@0 | 2236 | case OPUS_SET_BITRATE_REQUEST: |
michael@0 | 2237 | { |
michael@0 | 2238 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2239 | if (value<=500 && value!=OPUS_BITRATE_MAX) |
michael@0 | 2240 | goto bad_arg; |
michael@0 | 2241 | value = IMIN(value, 260000*st->channels); |
michael@0 | 2242 | st->bitrate = value; |
michael@0 | 2243 | } |
michael@0 | 2244 | break; |
michael@0 | 2245 | case CELT_SET_CHANNELS_REQUEST: |
michael@0 | 2246 | { |
michael@0 | 2247 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2248 | if (value<1 || value>2) |
michael@0 | 2249 | goto bad_arg; |
michael@0 | 2250 | st->stream_channels = value; |
michael@0 | 2251 | } |
michael@0 | 2252 | break; |
michael@0 | 2253 | case OPUS_SET_LSB_DEPTH_REQUEST: |
michael@0 | 2254 | { |
michael@0 | 2255 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2256 | if (value<8 || value>24) |
michael@0 | 2257 | goto bad_arg; |
michael@0 | 2258 | st->lsb_depth=value; |
michael@0 | 2259 | } |
michael@0 | 2260 | break; |
michael@0 | 2261 | case OPUS_GET_LSB_DEPTH_REQUEST: |
michael@0 | 2262 | { |
michael@0 | 2263 | opus_int32 *value = va_arg(ap, opus_int32*); |
michael@0 | 2264 | *value=st->lsb_depth; |
michael@0 | 2265 | } |
michael@0 | 2266 | break; |
michael@0 | 2267 | case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST: |
michael@0 | 2268 | { |
michael@0 | 2269 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2270 | st->variable_duration = value; |
michael@0 | 2271 | } |
michael@0 | 2272 | break; |
michael@0 | 2273 | case OPUS_RESET_STATE: |
michael@0 | 2274 | { |
michael@0 | 2275 | int i; |
michael@0 | 2276 | opus_val16 *oldBandE, *oldLogE, *oldLogE2; |
michael@0 | 2277 | oldBandE = (opus_val16*)(st->in_mem+st->channels*(st->overlap+COMBFILTER_MAXPERIOD)); |
michael@0 | 2278 | oldLogE = oldBandE + st->channels*st->mode->nbEBands; |
michael@0 | 2279 | oldLogE2 = oldLogE + st->channels*st->mode->nbEBands; |
michael@0 | 2280 | OPUS_CLEAR((char*)&st->ENCODER_RESET_START, |
michael@0 | 2281 | opus_custom_encoder_get_size(st->mode, st->channels)- |
michael@0 | 2282 | ((char*)&st->ENCODER_RESET_START - (char*)st)); |
michael@0 | 2283 | for (i=0;i<st->channels*st->mode->nbEBands;i++) |
michael@0 | 2284 | oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT); |
michael@0 | 2285 | st->vbr_offset = 0; |
michael@0 | 2286 | st->delayedIntra = 1; |
michael@0 | 2287 | st->spread_decision = SPREAD_NORMAL; |
michael@0 | 2288 | st->tonal_average = 256; |
michael@0 | 2289 | st->hf_average = 0; |
michael@0 | 2290 | st->tapset_decision = 0; |
michael@0 | 2291 | } |
michael@0 | 2292 | break; |
michael@0 | 2293 | #ifdef CUSTOM_MODES |
michael@0 | 2294 | case CELT_SET_INPUT_CLIPPING_REQUEST: |
michael@0 | 2295 | { |
michael@0 | 2296 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2297 | st->clip = value; |
michael@0 | 2298 | } |
michael@0 | 2299 | break; |
michael@0 | 2300 | #endif |
michael@0 | 2301 | case CELT_SET_SIGNALLING_REQUEST: |
michael@0 | 2302 | { |
michael@0 | 2303 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2304 | st->signalling = value; |
michael@0 | 2305 | } |
michael@0 | 2306 | break; |
michael@0 | 2307 | case CELT_SET_ANALYSIS_REQUEST: |
michael@0 | 2308 | { |
michael@0 | 2309 | AnalysisInfo *info = va_arg(ap, AnalysisInfo *); |
michael@0 | 2310 | if (info) |
michael@0 | 2311 | OPUS_COPY(&st->analysis, info, 1); |
michael@0 | 2312 | } |
michael@0 | 2313 | break; |
michael@0 | 2314 | case CELT_GET_MODE_REQUEST: |
michael@0 | 2315 | { |
michael@0 | 2316 | const CELTMode ** value = va_arg(ap, const CELTMode**); |
michael@0 | 2317 | if (value==0) |
michael@0 | 2318 | goto bad_arg; |
michael@0 | 2319 | *value=st->mode; |
michael@0 | 2320 | } |
michael@0 | 2321 | break; |
michael@0 | 2322 | case OPUS_GET_FINAL_RANGE_REQUEST: |
michael@0 | 2323 | { |
michael@0 | 2324 | opus_uint32 * value = va_arg(ap, opus_uint32 *); |
michael@0 | 2325 | if (value==0) |
michael@0 | 2326 | goto bad_arg; |
michael@0 | 2327 | *value=st->rng; |
michael@0 | 2328 | } |
michael@0 | 2329 | break; |
michael@0 | 2330 | case OPUS_SET_LFE_REQUEST: |
michael@0 | 2331 | { |
michael@0 | 2332 | opus_int32 value = va_arg(ap, opus_int32); |
michael@0 | 2333 | st->lfe = value; |
michael@0 | 2334 | } |
michael@0 | 2335 | break; |
michael@0 | 2336 | case OPUS_SET_ENERGY_MASK_REQUEST: |
michael@0 | 2337 | { |
michael@0 | 2338 | opus_val16 *value = va_arg(ap, opus_val16*); |
michael@0 | 2339 | st->energy_mask = value; |
michael@0 | 2340 | } |
michael@0 | 2341 | break; |
michael@0 | 2342 | default: |
michael@0 | 2343 | goto bad_request; |
michael@0 | 2344 | } |
michael@0 | 2345 | va_end(ap); |
michael@0 | 2346 | return OPUS_OK; |
michael@0 | 2347 | bad_arg: |
michael@0 | 2348 | va_end(ap); |
michael@0 | 2349 | return OPUS_BAD_ARG; |
michael@0 | 2350 | bad_request: |
michael@0 | 2351 | va_end(ap); |
michael@0 | 2352 | return OPUS_UNIMPLEMENTED; |
michael@0 | 2353 | } |