annotate src/opus-1.3/celt/celt_encoder.c @ 69:7aeed7906520

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