annotate src/opus-1.3/celt/celt_encoder.c @ 169:223a55898ab9 tip default

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