Coverage Report

Created: 2025-12-31 07:57

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