Coverage Report

Created: 2024-09-06 07:53

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