Coverage Report

Created: 2025-08-28 07:12

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