Coverage Report

Created: 2025-07-01 07:05

/src/opus/celt/celt_encoder.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (c) 2007-2008 CSIRO
2
   Copyright (c) 2007-2010 Xiph.Org Foundation
3
   Copyright (c) 2008 Gregory Maxwell
4
   Written by Jean-Marc Valin and Gregory Maxwell */
5
/*
6
   Redistribution and use in source and binary forms, with or without
7
   modification, are permitted provided that the following conditions
8
   are met:
9
10
   - Redistributions of source code must retain the above copyright
11
   notice, this list of conditions and the following disclaimer.
12
13
   - Redistributions in binary form must reproduce the above copyright
14
   notice, this list of conditions and the following disclaimer in the
15
   documentation and/or other materials provided with the distribution.
16
17
   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20
   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21
   OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
*/
29
30
#ifdef HAVE_CONFIG_H
31
#include "config.h"
32
#endif
33
34
#define CELT_ENCODER_C
35
36
#include "cpu_support.h"
37
#include "os_support.h"
38
#include "mdct.h"
39
#include <math.h>
40
#include "celt.h"
41
#include "pitch.h"
42
#include "bands.h"
43
#include "modes.h"
44
#include "entcode.h"
45
#include "quant_bands.h"
46
#include "rate.h"
47
#include "stack_alloc.h"
48
#include "mathops.h"
49
#include "float_cast.h"
50
#include <stdarg.h>
51
#include "celt_lpc.h"
52
#include "vq.h"
53
54
55
#ifndef M_PI
56
#define M_PI 3.141592653
57
#endif
58
59
60
/** Encoder state
61
 @brief Encoder state
62
 */
63
struct OpusCustomEncoder {
64
   const OpusCustomMode *mode;     /**< Mode used by the encoder */
65
   int channels;
66
   int stream_channels;
67
68
   int force_intra;
69
   int clip;
70
   int disable_pf;
71
   int complexity;
72
   int upsample;
73
   int start, end;
74
75
   opus_int32 bitrate;
76
   int vbr;
77
   int signalling;
78
   int constrained_vbr;      /* If zero, VBR can do whatever it likes with the rate */
79
   int loss_rate;
80
   int lsb_depth;
81
   int lfe;
82
   int disable_inv;
83
   int arch;
84
#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
1.60M
{
146
1.60M
#ifdef ENABLE_QEXT
147
1.60M
   CELTMode *mode = opus_custom_mode_create(96000, 1920, NULL);
148
#else
149
   CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
150
#endif
151
1.60M
   return opus_custom_encoder_get_size(mode, channels);
152
1.60M
}
153
154
OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
155
2.19M
{
156
2.19M
   int extra=0;
157
2.19M
   int size;
158
#ifdef ENABLE_QEXT
159
   int qext_scale;
160
1.37M
   extra = channels*NB_QEXT_BANDS*sizeof(celt_glog);
161
1.37M
   if (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)) {
162
1.03M
      qext_scale = 2;
163
1.03M
   } else qext_scale = 1;
164
#endif
165
2.19M
   size = sizeof(struct CELTEncoder)
166
2.19M
         + (channels*mode->overlap-1)*sizeof(celt_sig)    /* celt_sig in_mem[channels*mode->overlap]; */
167
2.19M
         + channels*QEXT_SCALE(COMBFILTER_MAXPERIOD)*sizeof(celt_sig) /* celt_sig prefilter_mem[channels*COMBFILTER_MAXPERIOD]; */
168
2.19M
         + 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
2.19M
         + extra;
173
2.19M
   return size;
174
2.19M
}
celt_encoder.c:opus_custom_encoder_get_size
Line
Count
Source
155
1.37M
{
156
1.37M
   int extra=0;
157
1.37M
   int size;
158
1.37M
#ifdef ENABLE_QEXT
159
1.37M
   int qext_scale;
160
1.37M
   extra = channels*NB_QEXT_BANDS*sizeof(celt_glog);
161
1.37M
   if (mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)) {
162
1.03M
      qext_scale = 2;
163
1.03M
   } else qext_scale = 1;
164
1.37M
#endif
165
1.37M
   size = sizeof(struct CELTEncoder)
166
1.37M
         + (channels*mode->overlap-1)*sizeof(celt_sig)    /* celt_sig in_mem[channels*mode->overlap]; */
167
1.37M
         + channels*QEXT_SCALE(COMBFILTER_MAXPERIOD)*sizeof(celt_sig) /* celt_sig prefilter_mem[channels*COMBFILTER_MAXPERIOD]; */
168
1.37M
         + 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
1.37M
         + extra;
173
1.37M
   return size;
174
1.37M
}
celt_encoder.c:opus_custom_encoder_get_size
Line
Count
Source
155
825k
{
156
825k
   int extra=0;
157
825k
   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
825k
   size = sizeof(struct CELTEncoder)
166
825k
         + (channels*mode->overlap-1)*sizeof(celt_sig)    /* celt_sig in_mem[channels*mode->overlap]; */
167
825k
         + channels*QEXT_SCALE(COMBFILTER_MAXPERIOD)*sizeof(celt_sig) /* celt_sig prefilter_mem[channels*COMBFILTER_MAXPERIOD]; */
168
825k
         + 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
825k
         + extra;
173
825k
   return size;
174
825k
}
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
177k
{
197
177k
   if (channels < 0 || channels > 2)
198
0
      return OPUS_BAD_ARG;
199
200
177k
   if (st==NULL || mode==NULL)
201
0
      return OPUS_ALLOC_FAIL;
202
203
177k
   OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
204
205
177k
   st->mode = mode;
206
177k
   st->stream_channels = st->channels = channels;
207
208
177k
   st->upsample = 1;
209
177k
   st->start = 0;
210
177k
   st->end = st->mode->effEBands;
211
177k
   st->signalling = 1;
212
177k
   st->arch = arch;
213
214
177k
   st->constrained_vbr = 1;
215
177k
   st->clip = 1;
216
217
177k
   st->bitrate = OPUS_BITRATE_MAX;
218
177k
   st->vbr = 0;
219
177k
   st->force_intra  = 0;
220
177k
   st->complexity = 5;
221
177k
   st->lsb_depth=24;
222
223
177k
#ifdef ENABLE_QEXT
224
177k
   if (st->mode->Fs == 96000 && (mode->shortMdctSize==240 || mode->shortMdctSize==180)) st->qext_scale = 2;
225
170k
   else st->qext_scale = 1;
226
177k
#endif
227
228
177k
   opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
229
230
177k
   return OPUS_OK;
231
177k
}
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
177k
{
243
177k
   int ret;
244
177k
#ifdef ENABLE_QEXT
245
177k
   if (sampling_rate==96000) {
246
6.91k
      st->upsample = 1;
247
6.91k
      return opus_custom_encoder_init_arch(st,
248
6.91k
              opus_custom_mode_create(96000, 1920, NULL), channels, arch);
249
6.91k
   }
250
170k
#endif
251
170k
   ret = opus_custom_encoder_init_arch(st,
252
170k
           opus_custom_mode_create(48000, 960, NULL), channels, arch);
253
170k
   if (ret != OPUS_OK)
254
0
      return ret;
255
170k
   st->upsample = resampling_factor(sampling_rate);
256
170k
   return OPUS_OK;
257
170k
}
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
175k
{
271
175k
   int i;
272
175k
   VARDECL(opus_val16, tmp);
273
175k
   opus_val32 mem0,mem1;
274
175k
   int is_transient = 0;
275
175k
   opus_int32 mask_metric = 0;
276
175k
   int c;
277
175k
   opus_val16 tf_max;
278
175k
   int len2;
279
   /* Forward masking: 6.7 dB/ms. */
280
#ifdef FIXED_POINT
281
   int forward_shift = 4;
282
#else
283
95.5k
   opus_val16 forward_decay = QCONST16(.0625f,15);
284
#endif
285
   /* Table of 6*64/x, trained on real data to minimize the average error */
286
175k
   static const unsigned char inv_table[128] = {
287
175k
         255,255,156,110, 86, 70, 59, 51, 45, 40, 37, 33, 31, 28, 26, 25,
288
175k
          23, 22, 21, 20, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
289
175k
          12, 12, 11, 11, 11, 10, 10, 10,  9,  9,  9,  9,  9,  9,  8,  8,
290
175k
           8,  8,  8,  7,  7,  7,  7,  7,  7,  6,  6,  6,  6,  6,  6,  6,
291
175k
           6,  6,  6,  6,  6,  6,  6,  6,  6,  5,  5,  5,  5,  5,  5,  5,
292
175k
           5,  5,  5,  5,  5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
293
175k
           4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  3,  3,
294
175k
           3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  2,
295
175k
   };
296
175k
   SAVE_STACK;
297
#ifdef FIXED_POINT
298
80.1k
   int in_shift = IMAX(0, celt_ilog2(1+celt_maxabs32(in, C*len))-14);
299
#endif
300
175k
   ALLOC(tmp, len, opus_val16);
301
302
175k
   *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
175k
   if (allow_weak_transients)
308
2.49k
   {
309
#ifdef FIXED_POINT
310
      forward_shift = 5;
311
#else
312
1.07k
      forward_decay = QCONST16(.03125f,15);
313
#endif
314
2.49k
   }
315
175k
   len2=len/2;
316
441k
   for (c=0;c<C;c++)
317
265k
   {
318
265k
      opus_val32 mean;
319
265k
      opus_int32 unmask=0;
320
265k
      opus_val32 norm;
321
265k
      opus_val16 maxE;
322
265k
      mem0=0;
323
265k
      mem1=0;
324
      /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
325
183M
      for (i=0;i<len;i++)
326
183M
      {
327
#ifndef FIXED_POINT
328
         float mem00;
329
#endif
330
183M
         opus_val32 x,y;
331
183M
         x = SHR32(in[i+c*len],in_shift);
332
183M
         y = ADD32(mem0, x);
333
#ifdef FIXED_POINT
334
75.4M
         mem0 = mem1 + y - SHL32(x,1);
335
75.4M
         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
         mem00=mem0;
342
         mem0 = mem0 - x + .5f*mem1;
343
         mem1 =  x - mem00;
344
#endif
345
183M
         tmp[i] = SROUND16(y, 2);
346
         /*printf("%f ", tmp[i]);*/
347
183M
      }
348
      /*printf("\n");*/
349
      /* First few samples are bad because we don't propagate the memory */
350
265k
      OPUS_CLEAR(tmp, 12);
351
352
#ifdef FIXED_POINT
353
      /* Normalize tmp to max range */
354
      {
355
         int shift=0;
356
115k
         shift = 14-celt_ilog2(MAX16(1, celt_maxabs16(tmp, len)));
357
115k
         if (shift!=0)
358
113k
         {
359
73.9M
            for (i=0;i<len;i++)
360
73.7M
               tmp[i] = SHL16(tmp[i], shift);
361
113k
         }
362
      }
363
#endif
364
365
265k
      mean=0;
366
265k
      mem0=0;
367
      /* Grouping by two to reduce complexity */
368
      /* Forward pass to compute the post-echo threshold*/
369
91.8M
      for (i=0;i<len2;i++)
370
91.6M
      {
371
91.6M
         opus_val32 x2 = PSHR32(MULT16_16(tmp[2*i],tmp[2*i]) + MULT16_16(tmp[2*i+1],tmp[2*i+1]),4);
372
91.6M
         mean += PSHR32(x2, 12);
373
#ifdef FIXED_POINT
374
         /* FIXME: Use PSHR16() instead */
375
37.7M
         mem0 = mem0 + PSHR32(x2-mem0,forward_shift);
376
37.7M
         tmp[i] = PSHR32(mem0, 12);
377
#else
378
         mem0 = x2 + (1.f-forward_decay)*mem0;
379
         tmp[i] = forward_decay*mem0;
380
#endif
381
91.6M
      }
382
383
265k
      mem0=0;
384
265k
      maxE=0;
385
      /* Backward pass to compute the pre-echo threshold */
386
91.8M
      for (i=len2-1;i>=0;i--)
387
91.6M
      {
388
         /* Backward masking: 13.9 dB/ms. */
389
#ifdef FIXED_POINT
390
         /* FIXME: Use PSHR16() instead */
391
37.7M
         mem0 = mem0 + PSHR32(SHL32(tmp[i],4)-mem0,3);
392
37.7M
         tmp[i] = PSHR32(mem0, 4);
393
37.7M
         maxE = MAX16(maxE, tmp[i]);
394
#else
395
         mem0 = tmp[i] + 0.875f*mem0;
396
         tmp[i] = 0.125f*mem0;
397
53.8M
         maxE = MAX16(maxE, 0.125f*mem0);
398
#endif
399
91.6M
      }
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
115k
      mean = MULT16_16(celt_sqrt(mean), celt_sqrt(MULT16_16(maxE,len2>>1)));
411
#else
412
149k
      mean = celt_sqrt(mean * maxE*.5*len2);
413
#endif
414
      /* Inverse of the mean energy in Q15+6 */
415
265k
      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
265k
      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
265k
      celt_assert(!celt_isnan(tmp[0]));
424
265k
      celt_assert(!celt_isnan(norm));
425
22.1M
      for (i=12;i<len2-5;i+=4)
426
21.8M
      {
427
21.8M
         int id;
428
#ifdef FIXED_POINT
429
8.97M
         id = MAX32(0,MIN32(127,MULT16_32_Q15(tmp[i]+EPSILON,norm))); /* Do not round to nearest */
430
#else
431
12.8M
         id = (int)MAX32(0,MIN32(127,floor(64*norm*(tmp[i]+EPSILON)))); /* Do not round to nearest */
432
#endif
433
21.8M
         unmask += inv_table[id];
434
21.8M
      }
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
265k
      unmask = 64*unmask*4/(6*(len2-17));
438
265k
      if (unmask>mask_metric)
439
213k
      {
440
213k
         *tf_chan = c;
441
213k
         mask_metric = unmask;
442
213k
      }
443
265k
   }
444
175k
   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
175k
   if (toneishness > QCONST32(.98f, 29) && tone_freq < QCONST16(0.026f, 13))
448
165
      is_transient = 0;
449
   /* For low bitrates, define "weak transients" that need to be
450
      handled differently to avoid partial collapse. */
451
175k
   if (allow_weak_transients && is_transient && mask_metric<600) {
452
186
      is_transient = 0;
453
186
      *weak_transient = 1;
454
186
   }
455
   /* Arbitrary metric for VBR boost */
456
175k
   tf_max = MAX16(0,celt_sqrt(27*mask_metric)-42);
457
   /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */
458
175k
   *tf_estimate = celt_sqrt(MAX32(0, SHL32(MULT16_16(QCONST16(0.0069,14),MIN16(163,tf_max)),14)-QCONST32(0.139,28)));
459
   /*printf("%d %f\n", tf_max, mask_metric);*/
460
175k
   RESTORE_STACK;
461
#ifdef FUZZING
462
   is_transient = rand()&0x1;
463
#endif
464
   /*printf("%d %f %d\n", is_transient, (float)*tf_estimate, tf_max);*/
465
175k
   return is_transient;
466
175k
}
celt_encoder.c:transient_analysis
Line
Count
Source
270
80.1k
{
271
80.1k
   int i;
272
80.1k
   VARDECL(opus_val16, tmp);
273
80.1k
   opus_val32 mem0,mem1;
274
80.1k
   int is_transient = 0;
275
80.1k
   opus_int32 mask_metric = 0;
276
80.1k
   int c;
277
80.1k
   opus_val16 tf_max;
278
80.1k
   int len2;
279
   /* Forward masking: 6.7 dB/ms. */
280
80.1k
#ifdef FIXED_POINT
281
80.1k
   int forward_shift = 4;
282
#else
283
   opus_val16 forward_decay = QCONST16(.0625f,15);
284
#endif
285
   /* Table of 6*64/x, trained on real data to minimize the average error */
286
80.1k
   static const unsigned char inv_table[128] = {
287
80.1k
         255,255,156,110, 86, 70, 59, 51, 45, 40, 37, 33, 31, 28, 26, 25,
288
80.1k
          23, 22, 21, 20, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
289
80.1k
          12, 12, 11, 11, 11, 10, 10, 10,  9,  9,  9,  9,  9,  9,  8,  8,
290
80.1k
           8,  8,  8,  7,  7,  7,  7,  7,  7,  6,  6,  6,  6,  6,  6,  6,
291
80.1k
           6,  6,  6,  6,  6,  6,  6,  6,  6,  5,  5,  5,  5,  5,  5,  5,
292
80.1k
           5,  5,  5,  5,  5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
293
80.1k
           4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  3,  3,
294
80.1k
           3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  2,
295
80.1k
   };
296
80.1k
   SAVE_STACK;
297
80.1k
#ifdef FIXED_POINT
298
80.1k
   int in_shift = IMAX(0, celt_ilog2(1+celt_maxabs32(in, C*len))-14);
299
80.1k
#endif
300
80.1k
   ALLOC(tmp, len, opus_val16);
301
302
80.1k
   *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
80.1k
   if (allow_weak_transients)
308
1.42k
   {
309
1.42k
#ifdef FIXED_POINT
310
1.42k
      forward_shift = 5;
311
#else
312
      forward_decay = QCONST16(.03125f,15);
313
#endif
314
1.42k
   }
315
80.1k
   len2=len/2;
316
195k
   for (c=0;c<C;c++)
317
115k
   {
318
115k
      opus_val32 mean;
319
115k
      opus_int32 unmask=0;
320
115k
      opus_val32 norm;
321
115k
      opus_val16 maxE;
322
115k
      mem0=0;
323
115k
      mem1=0;
324
      /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
325
75.5M
      for (i=0;i<len;i++)
326
75.4M
      {
327
#ifndef FIXED_POINT
328
         float mem00;
329
#endif
330
75.4M
         opus_val32 x,y;
331
75.4M
         x = SHR32(in[i+c*len],in_shift);
332
75.4M
         y = ADD32(mem0, x);
333
75.4M
#ifdef FIXED_POINT
334
75.4M
         mem0 = mem1 + y - SHL32(x,1);
335
75.4M
         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
         mem00=mem0;
342
         mem0 = mem0 - x + .5f*mem1;
343
         mem1 =  x - mem00;
344
#endif
345
75.4M
         tmp[i] = SROUND16(y, 2);
346
         /*printf("%f ", tmp[i]);*/
347
75.4M
      }
348
      /*printf("\n");*/
349
      /* First few samples are bad because we don't propagate the memory */
350
115k
      OPUS_CLEAR(tmp, 12);
351
352
115k
#ifdef FIXED_POINT
353
      /* Normalize tmp to max range */
354
115k
      {
355
115k
         int shift=0;
356
115k
         shift = 14-celt_ilog2(MAX16(1, celt_maxabs16(tmp, len)));
357
115k
         if (shift!=0)
358
113k
         {
359
73.9M
            for (i=0;i<len;i++)
360
73.7M
               tmp[i] = SHL16(tmp[i], shift);
361
113k
         }
362
115k
      }
363
115k
#endif
364
365
115k
      mean=0;
366
115k
      mem0=0;
367
      /* Grouping by two to reduce complexity */
368
      /* Forward pass to compute the post-echo threshold*/
369
37.8M
      for (i=0;i<len2;i++)
370
37.7M
      {
371
37.7M
         opus_val32 x2 = PSHR32(MULT16_16(tmp[2*i],tmp[2*i]) + MULT16_16(tmp[2*i+1],tmp[2*i+1]),4);
372
37.7M
         mean += PSHR32(x2, 12);
373
37.7M
#ifdef FIXED_POINT
374
         /* FIXME: Use PSHR16() instead */
375
37.7M
         mem0 = mem0 + PSHR32(x2-mem0,forward_shift);
376
37.7M
         tmp[i] = PSHR32(mem0, 12);
377
#else
378
         mem0 = x2 + (1.f-forward_decay)*mem0;
379
         tmp[i] = forward_decay*mem0;
380
#endif
381
37.7M
      }
382
383
115k
      mem0=0;
384
115k
      maxE=0;
385
      /* Backward pass to compute the pre-echo threshold */
386
37.8M
      for (i=len2-1;i>=0;i--)
387
37.7M
      {
388
         /* Backward masking: 13.9 dB/ms. */
389
37.7M
#ifdef FIXED_POINT
390
         /* FIXME: Use PSHR16() instead */
391
37.7M
         mem0 = mem0 + PSHR32(SHL32(tmp[i],4)-mem0,3);
392
37.7M
         tmp[i] = PSHR32(mem0, 4);
393
37.7M
         maxE = MAX16(maxE, tmp[i]);
394
#else
395
         mem0 = tmp[i] + 0.875f*mem0;
396
         tmp[i] = 0.125f*mem0;
397
         maxE = MAX16(maxE, 0.125f*mem0);
398
#endif
399
37.7M
      }
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
115k
#ifdef FIXED_POINT
409
      /* Costs two sqrt() to avoid overflows */
410
115k
      mean = MULT16_16(celt_sqrt(mean), celt_sqrt(MULT16_16(maxE,len2>>1)));
411
#else
412
      mean = celt_sqrt(mean * maxE*.5*len2);
413
#endif
414
      /* Inverse of the mean energy in Q15+6 */
415
115k
      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
115k
      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
115k
      celt_assert(!celt_isnan(tmp[0]));
424
115k
      celt_assert(!celt_isnan(norm));
425
9.08M
      for (i=12;i<len2-5;i+=4)
426
8.97M
      {
427
8.97M
         int id;
428
8.97M
#ifdef FIXED_POINT
429
8.97M
         id = MAX32(0,MIN32(127,MULT16_32_Q15(tmp[i]+EPSILON,norm))); /* Do not round to nearest */
430
#else
431
         id = (int)MAX32(0,MIN32(127,floor(64*norm*(tmp[i]+EPSILON)))); /* Do not round to nearest */
432
#endif
433
8.97M
         unmask += inv_table[id];
434
8.97M
      }
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
115k
      unmask = 64*unmask*4/(6*(len2-17));
438
115k
      if (unmask>mask_metric)
439
95.8k
      {
440
95.8k
         *tf_chan = c;
441
95.8k
         mask_metric = unmask;
442
95.8k
      }
443
115k
   }
444
80.1k
   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
80.1k
   if (toneishness > QCONST32(.98f, 29) && tone_freq < QCONST16(0.026f, 13))
448
29
      is_transient = 0;
449
   /* For low bitrates, define "weak transients" that need to be
450
      handled differently to avoid partial collapse. */
451
80.1k
   if (allow_weak_transients && is_transient && mask_metric<600) {
452
86
      is_transient = 0;
453
86
      *weak_transient = 1;
454
86
   }
455
   /* Arbitrary metric for VBR boost */
456
80.1k
   tf_max = MAX16(0,celt_sqrt(27*mask_metric)-42);
457
   /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */
458
80.1k
   *tf_estimate = celt_sqrt(MAX32(0, SHL32(MULT16_16(QCONST16(0.0069,14),MIN16(163,tf_max)),14)-QCONST32(0.139,28)));
459
   /*printf("%d %f\n", tf_max, mask_metric);*/
460
80.1k
   RESTORE_STACK;
461
#ifdef FUZZING
462
   is_transient = rand()&0x1;
463
#endif
464
   /*printf("%d %f %d\n", is_transient, (float)*tf_estimate, tf_max);*/
465
80.1k
   return is_transient;
466
80.1k
}
celt_encoder.c:transient_analysis
Line
Count
Source
270
95.5k
{
271
95.5k
   int i;
272
95.5k
   VARDECL(opus_val16, tmp);
273
95.5k
   opus_val32 mem0,mem1;
274
95.5k
   int is_transient = 0;
275
95.5k
   opus_int32 mask_metric = 0;
276
95.5k
   int c;
277
95.5k
   opus_val16 tf_max;
278
95.5k
   int len2;
279
   /* Forward masking: 6.7 dB/ms. */
280
#ifdef FIXED_POINT
281
   int forward_shift = 4;
282
#else
283
95.5k
   opus_val16 forward_decay = QCONST16(.0625f,15);
284
95.5k
#endif
285
   /* Table of 6*64/x, trained on real data to minimize the average error */
286
95.5k
   static const unsigned char inv_table[128] = {
287
95.5k
         255,255,156,110, 86, 70, 59, 51, 45, 40, 37, 33, 31, 28, 26, 25,
288
95.5k
          23, 22, 21, 20, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
289
95.5k
          12, 12, 11, 11, 11, 10, 10, 10,  9,  9,  9,  9,  9,  9,  8,  8,
290
95.5k
           8,  8,  8,  7,  7,  7,  7,  7,  7,  6,  6,  6,  6,  6,  6,  6,
291
95.5k
           6,  6,  6,  6,  6,  6,  6,  6,  6,  5,  5,  5,  5,  5,  5,  5,
292
95.5k
           5,  5,  5,  5,  5,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,
293
95.5k
           4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  4,  3,  3,
294
95.5k
           3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  3,  2,
295
95.5k
   };
296
95.5k
   SAVE_STACK;
297
#ifdef FIXED_POINT
298
   int in_shift = IMAX(0, celt_ilog2(1+celt_maxabs32(in, C*len))-14);
299
#endif
300
95.5k
   ALLOC(tmp, len, opus_val16);
301
302
95.5k
   *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
95.5k
   if (allow_weak_transients)
308
1.07k
   {
309
#ifdef FIXED_POINT
310
      forward_shift = 5;
311
#else
312
1.07k
      forward_decay = QCONST16(.03125f,15);
313
1.07k
#endif
314
1.07k
   }
315
95.5k
   len2=len/2;
316
245k
   for (c=0;c<C;c++)
317
149k
   {
318
149k
      opus_val32 mean;
319
149k
      opus_int32 unmask=0;
320
149k
      opus_val32 norm;
321
149k
      opus_val16 maxE;
322
149k
      mem0=0;
323
149k
      mem1=0;
324
      /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
325
107M
      for (i=0;i<len;i++)
326
107M
      {
327
107M
#ifndef FIXED_POINT
328
107M
         float mem00;
329
107M
#endif
330
107M
         opus_val32 x,y;
331
107M
         x = SHR32(in[i+c*len],in_shift);
332
107M
         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
107M
         mem00=mem0;
342
107M
         mem0 = mem0 - x + .5f*mem1;
343
107M
         mem1 =  x - mem00;
344
107M
#endif
345
107M
         tmp[i] = SROUND16(y, 2);
346
         /*printf("%f ", tmp[i]);*/
347
107M
      }
348
      /*printf("\n");*/
349
      /* First few samples are bad because we don't propagate the memory */
350
149k
      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
149k
      mean=0;
366
149k
      mem0=0;
367
      /* Grouping by two to reduce complexity */
368
      /* Forward pass to compute the post-echo threshold*/
369
54.0M
      for (i=0;i<len2;i++)
370
53.8M
      {
371
53.8M
         opus_val32 x2 = PSHR32(MULT16_16(tmp[2*i],tmp[2*i]) + MULT16_16(tmp[2*i+1],tmp[2*i+1]),4);
372
53.8M
         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
53.8M
         mem0 = x2 + (1.f-forward_decay)*mem0;
379
53.8M
         tmp[i] = forward_decay*mem0;
380
53.8M
#endif
381
53.8M
      }
382
383
149k
      mem0=0;
384
149k
      maxE=0;
385
      /* Backward pass to compute the pre-echo threshold */
386
54.0M
      for (i=len2-1;i>=0;i--)
387
53.8M
      {
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
53.8M
         mem0 = tmp[i] + 0.875f*mem0;
396
53.8M
         tmp[i] = 0.125f*mem0;
397
53.8M
         maxE = MAX16(maxE, 0.125f*mem0);
398
53.8M
#endif
399
53.8M
      }
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
149k
      mean = celt_sqrt(mean * maxE*.5*len2);
413
149k
#endif
414
      /* Inverse of the mean energy in Q15+6 */
415
149k
      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
149k
      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
149k
      celt_assert(!celt_isnan(tmp[0]));
424
149k
      celt_assert(!celt_isnan(norm));
425
13.0M
      for (i=12;i<len2-5;i+=4)
426
12.8M
      {
427
12.8M
         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
12.8M
         id = (int)MAX32(0,MIN32(127,floor(64*norm*(tmp[i]+EPSILON)))); /* Do not round to nearest */
432
12.8M
#endif
433
12.8M
         unmask += inv_table[id];
434
12.8M
      }
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
149k
      unmask = 64*unmask*4/(6*(len2-17));
438
149k
      if (unmask>mask_metric)
439
118k
      {
440
118k
         *tf_chan = c;
441
118k
         mask_metric = unmask;
442
118k
      }
443
149k
   }
444
95.5k
   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
95.5k
   if (toneishness > QCONST32(.98f, 29) && tone_freq < QCONST16(0.026f, 13))
448
136
      is_transient = 0;
449
   /* For low bitrates, define "weak transients" that need to be
450
      handled differently to avoid partial collapse. */
451
95.5k
   if (allow_weak_transients && is_transient && mask_metric<600) {
452
100
      is_transient = 0;
453
100
      *weak_transient = 1;
454
100
   }
455
   /* Arbitrary metric for VBR boost */
456
95.5k
   tf_max = MAX16(0,celt_sqrt(27*mask_metric)-42);
457
   /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */
458
95.5k
   *tf_estimate = celt_sqrt(MAX32(0, SHL32(MULT16_16(QCONST16(0.0069,14),MIN16(163,tf_max)),14)-QCONST32(0.139,28)));
459
   /*printf("%d %f\n", tf_max, mask_metric);*/
460
95.5k
   RESTORE_STACK;
461
#ifdef FUZZING
462
   is_transient = rand()&0x1;
463
#endif
464
   /*printf("%d %f %d\n", is_transient, (float)*tf_estimate, tf_max);*/
465
95.5k
   return is_transient;
466
95.5k
}
467
468
/* Looks for sudden increases of energy to decide whether we need to patch
469
   the transient decision */
470
static int patch_transient_decision(celt_glog *newE, celt_glog *oldE, int nbEBands,
471
      int start, int end, int C)
472
27.3k
{
473
27.3k
   int i, c;
474
27.3k
   opus_val32 mean_diff=0;
475
27.3k
   celt_glog spread_old[26];
476
   /* Apply an aggressive (-6 dB/Bark) spreading function to the old frame to
477
      avoid false detection caused by irrelevant bands */
478
27.3k
   if (C==1)
479
15.9k
   {
480
15.9k
      spread_old[start] = oldE[start];
481
235k
      for (i=start+1;i<end;i++)
482
219k
         spread_old[i] = MAXG(spread_old[i-1]-GCONST(1.0f), oldE[i]);
483
15.9k
   } else {
484
11.3k
      spread_old[start] = MAXG(oldE[start],oldE[start+nbEBands]);
485
164k
      for (i=start+1;i<end;i++)
486
153k
         spread_old[i] = MAXG(spread_old[i-1]-GCONST(1.0f),
487
11.3k
                               MAXG(oldE[i],oldE[i+nbEBands]));
488
11.3k
   }
489
400k
   for (i=end-2;i>=start;i--)
490
372k
      spread_old[i] = MAXG(spread_old[i], spread_old[i+1]-GCONST(1.0f));
491
   /* Compute mean increase */
492
38.6k
   c=0; do {
493
487k
      for (i=IMAX(2,start);i<end-1;i++)
494
448k
      {
495
448k
         opus_val16 x1, x2;
496
448k
         x1 = MAXG(0, newE[i + c*nbEBands]);
497
448k
         x2 = MAXG(0, spread_old[i]);
498
448k
         mean_diff = ADD32(mean_diff, MAXG(0, SUB32(x1, x2)));
499
448k
      }
500
38.6k
   } while (++c<C);
501
27.3k
   mean_diff = DIV32(mean_diff, C*(end-1-IMAX(2,start)));
502
   /*printf("%f %f %d\n", mean_diff, max_diff, count);*/
503
27.3k
   return mean_diff > GCONST(1.f);
504
27.3k
}
celt_encoder.c:patch_transient_decision
Line
Count
Source
472
13.6k
{
473
13.6k
   int i, c;
474
13.6k
   opus_val32 mean_diff=0;
475
13.6k
   celt_glog spread_old[26];
476
   /* Apply an aggressive (-6 dB/Bark) spreading function to the old frame to
477
      avoid false detection caused by irrelevant bands */
478
13.6k
   if (C==1)
479
7.99k
   {
480
7.99k
      spread_old[start] = oldE[start];
481
117k
      for (i=start+1;i<end;i++)
482
109k
         spread_old[i] = MAXG(spread_old[i-1]-GCONST(1.0f), oldE[i]);
483
7.99k
   } else {
484
5.66k
      spread_old[start] = MAXG(oldE[start],oldE[start+nbEBands]);
485
82.3k
      for (i=start+1;i<end;i++)
486
76.6k
         spread_old[i] = MAXG(spread_old[i-1]-GCONST(1.0f),
487
5.66k
                               MAXG(oldE[i],oldE[i+nbEBands]));
488
5.66k
   }
489
200k
   for (i=end-2;i>=start;i--)
490
186k
      spread_old[i] = MAXG(spread_old[i], spread_old[i+1]-GCONST(1.0f));
491
   /* Compute mean increase */
492
19.3k
   c=0; do {
493
243k
      for (i=IMAX(2,start);i<end-1;i++)
494
224k
      {
495
224k
         opus_val16 x1, x2;
496
224k
         x1 = MAXG(0, newE[i + c*nbEBands]);
497
224k
         x2 = MAXG(0, spread_old[i]);
498
224k
         mean_diff = ADD32(mean_diff, MAXG(0, SUB32(x1, x2)));
499
224k
      }
500
19.3k
   } while (++c<C);
501
13.6k
   mean_diff = DIV32(mean_diff, C*(end-1-IMAX(2,start)));
502
   /*printf("%f %f %d\n", mean_diff, max_diff, count);*/
503
13.6k
   return mean_diff > GCONST(1.f);
504
13.6k
}
celt_encoder.c:patch_transient_decision
Line
Count
Source
472
13.6k
{
473
13.6k
   int i, c;
474
13.6k
   opus_val32 mean_diff=0;
475
13.6k
   celt_glog spread_old[26];
476
   /* Apply an aggressive (-6 dB/Bark) spreading function to the old frame to
477
      avoid false detection caused by irrelevant bands */
478
13.6k
   if (C==1)
479
7.99k
   {
480
7.99k
      spread_old[start] = oldE[start];
481
117k
      for (i=start+1;i<end;i++)
482
109k
         spread_old[i] = MAXG(spread_old[i-1]-GCONST(1.0f), oldE[i]);
483
7.99k
   } else {
484
5.66k
      spread_old[start] = MAXG(oldE[start],oldE[start+nbEBands]);
485
82.3k
      for (i=start+1;i<end;i++)
486
76.6k
         spread_old[i] = MAXG(spread_old[i-1]-GCONST(1.0f),
487
5.66k
                               MAXG(oldE[i],oldE[i+nbEBands]));
488
5.66k
   }
489
200k
   for (i=end-2;i>=start;i--)
490
186k
      spread_old[i] = MAXG(spread_old[i], spread_old[i+1]-GCONST(1.0f));
491
   /* Compute mean increase */
492
19.3k
   c=0; do {
493
243k
      for (i=IMAX(2,start);i<end-1;i++)
494
224k
      {
495
224k
         opus_val16 x1, x2;
496
224k
         x1 = MAXG(0, newE[i + c*nbEBands]);
497
224k
         x2 = MAXG(0, spread_old[i]);
498
224k
         mean_diff = ADD32(mean_diff, MAXG(0, SUB32(x1, x2)));
499
224k
      }
500
19.3k
   } while (++c<C);
501
13.6k
   mean_diff = DIV32(mean_diff, C*(end-1-IMAX(2,start)));
502
   /*printf("%f %f %d\n", mean_diff, max_diff, count);*/
503
13.6k
   return mean_diff > GCONST(1.f);
504
13.6k
}
505
506
/** Apply window and compute the MDCT for all sub-frames and
507
    all channels in a frame */
508
static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS_RESTRICT in,
509
                          celt_sig * OPUS_RESTRICT out, int C, int CC, int LM, int upsample,
510
                          int arch)
511
480k
{
512
480k
   const int overlap = mode->overlap;
513
480k
   int N;
514
480k
   int B;
515
480k
   int shift;
516
480k
   int i, b, c;
517
480k
   if (shortBlocks)
518
144k
   {
519
144k
      B = shortBlocks;
520
144k
      N = mode->shortMdctSize;
521
144k
      shift = mode->maxLM;
522
335k
   } else {
523
335k
      B = 1;
524
335k
      N = mode->shortMdctSize<<LM;
525
335k
      shift = mode->maxLM-LM;
526
335k
   }
527
734k
   c=0; do {
528
2.65M
      for (b=0;b<B;b++)
529
1.91M
      {
530
         /* Interleaving the sub-frames while doing the MDCTs */
531
1.91M
         clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N,
532
1.91M
                          &out[b+c*N*B], mode->window, overlap, shift, B,
533
1.91M
                          arch);
534
1.91M
      }
535
734k
   } while (++c<CC);
536
480k
   if (CC==2&&C==1)
537
59.0k
   {
538
42.0M
      for (i=0;i<B*N;i++)
539
42.0M
         out[i] = ADD32(HALF32(out[i]), HALF32(out[B*N+i]));
540
59.0k
   }
541
480k
   if (upsample != 1)
542
435k
   {
543
435k
      c=0; do
544
611k
      {
545
611k
         int bound = B*N/upsample;
546
96.2M
         for (i=0;i<bound;i++)
547
95.6M
            out[c*B*N+i] *= upsample;
548
611k
         OPUS_CLEAR(&out[c*B*N+bound], B*N-bound);
549
611k
      } while (++c<C);
550
435k
   }
551
480k
}
celt_encoder.c:compute_mdcts
Line
Count
Source
511
240k
{
512
240k
   const int overlap = mode->overlap;
513
240k
   int N;
514
240k
   int B;
515
240k
   int shift;
516
240k
   int i, b, c;
517
240k
   if (shortBlocks)
518
72.1k
   {
519
72.1k
      B = shortBlocks;
520
72.1k
      N = mode->shortMdctSize;
521
72.1k
      shift = mode->maxLM;
522
167k
   } else {
523
167k
      B = 1;
524
167k
      N = mode->shortMdctSize<<LM;
525
167k
      shift = mode->maxLM-LM;
526
167k
   }
527
367k
   c=0; do {
528
1.32M
      for (b=0;b<B;b++)
529
958k
      {
530
         /* Interleaving the sub-frames while doing the MDCTs */
531
958k
         clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N,
532
958k
                          &out[b+c*N*B], mode->window, overlap, shift, B,
533
958k
                          arch);
534
958k
      }
535
367k
   } while (++c<CC);
536
240k
   if (CC==2&&C==1)
537
29.5k
   {
538
21.0M
      for (i=0;i<B*N;i++)
539
21.0M
         out[i] = ADD32(HALF32(out[i]), HALF32(out[B*N+i]));
540
29.5k
   }
541
240k
   if (upsample != 1)
542
217k
   {
543
217k
      c=0; do
544
305k
      {
545
305k
         int bound = B*N/upsample;
546
48.1M
         for (i=0;i<bound;i++)
547
47.8M
            out[c*B*N+i] *= upsample;
548
305k
         OPUS_CLEAR(&out[c*B*N+bound], B*N-bound);
549
305k
      } while (++c<C);
550
217k
   }
551
240k
}
celt_encoder.c:compute_mdcts
Line
Count
Source
511
240k
{
512
240k
   const int overlap = mode->overlap;
513
240k
   int N;
514
240k
   int B;
515
240k
   int shift;
516
240k
   int i, b, c;
517
240k
   if (shortBlocks)
518
72.1k
   {
519
72.1k
      B = shortBlocks;
520
72.1k
      N = mode->shortMdctSize;
521
72.1k
      shift = mode->maxLM;
522
167k
   } else {
523
167k
      B = 1;
524
167k
      N = mode->shortMdctSize<<LM;
525
167k
      shift = mode->maxLM-LM;
526
167k
   }
527
367k
   c=0; do {
528
1.32M
      for (b=0;b<B;b++)
529
958k
      {
530
         /* Interleaving the sub-frames while doing the MDCTs */
531
958k
         clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N,
532
958k
                          &out[b+c*N*B], mode->window, overlap, shift, B,
533
958k
                          arch);
534
958k
      }
535
367k
   } while (++c<CC);
536
240k
   if (CC==2&&C==1)
537
29.5k
   {
538
21.0M
      for (i=0;i<B*N;i++)
539
21.0M
         out[i] = ADD32(HALF32(out[i]), HALF32(out[B*N+i]));
540
29.5k
   }
541
240k
   if (upsample != 1)
542
217k
   {
543
217k
      c=0; do
544
305k
      {
545
305k
         int bound = B*N/upsample;
546
48.1M
         for (i=0;i<bound;i++)
547
47.8M
            out[c*B*N+i] *= upsample;
548
305k
         OPUS_CLEAR(&out[c*B*N+bound], B*N-bound);
549
305k
      } while (++c<C);
550
217k
   }
551
240k
}
552
553
554
void celt_preemphasis(const opus_res * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTRICT inp,
555
                        int N, int CC, int upsample, const opus_val16 *coef, celt_sig *mem, int clip)
556
303k
{
557
303k
   int i;
558
303k
   opus_val16 coef0;
559
303k
   celt_sig m;
560
303k
   int Nu;
561
562
303k
   coef0 = coef[0];
563
303k
   m = *mem;
564
565
   /* Fast path for the normal 48kHz case and no clipping */
566
303k
   if (coef[1] == 0 && upsample == 1 && !clip)
567
22.7k
   {
568
12.1M
      for (i=0;i<N;i++)
569
12.1M
      {
570
12.1M
         celt_sig x;
571
12.1M
         x = RES2SIG(pcmp[CC*i]);
572
         /* Apply pre-emphasis */
573
12.1M
         inp[i] = x - m;
574
12.1M
         m = MULT16_32_Q15(coef0, x);
575
12.1M
      }
576
22.7k
      *mem = m;
577
22.7k
      return;
578
22.7k
   }
579
580
280k
   Nu = N/upsample;
581
280k
   if (upsample!=1)
582
274k
   {
583
274k
      OPUS_CLEAR(inp, N);
584
274k
   }
585
46.2M
   for (i=0;i<Nu;i++)
586
45.9M
      inp[i*upsample] = RES2SIG(pcmp[CC*i]);
587
588
#ifndef FIXED_POINT
589
154k
   if (clip)
590
0
   {
591
      /* Clip input to avoid encoding non-portable files */
592
0
      for (i=0;i<Nu;i++)
593
0
         inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample]));
594
0
   }
595
#else
596
   (void)clip; /* Avoids a warning about clip being unused. */
597
#endif
598
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) || defined(ENABLE_QEXT)
599
177k
   if (coef[1] != 0)
600
6.26k
   {
601
6.26k
      opus_val16 coef1 = coef[1];
602
#if defined(FIXED_POINT) && defined(ENABLE_QEXT)
603
      /* If we need the extra precision, we use the fact that coef[3] is exact to do a Newton-Raphson
604
         iteration and get us more precision on coef[2]. */
605
5.05k
      opus_val32 coef2_q30 = SHL32(coef[2], 18) + PSHR32(MULT16_16(QCONST32(1.f, 25) - MULT16_16(coef[3], coef[2]), coef[2]), 7);
606
5.05k
      celt_assert(SIG_SHIFT == 12);
607
#else
608
      opus_val16 coef2 = coef[2];
609
#endif
610
4.34M
      for (i=0;i<N;i++)
611
4.33M
      {
612
4.33M
         celt_sig x, tmp;
613
4.33M
         x = inp[i];
614
         /* Apply pre-emphasis */
615
#if defined(FIXED_POINT) && defined(ENABLE_QEXT)
616
3.46M
         tmp = SHL32(MULT32_32_Q31(coef2_q30, x), 1);
617
#else
618
870k
         tmp = SHL32(MULT16_32_Q15(coef2, x), 15-SIG_SHIFT);
619
#endif
620
4.33M
         inp[i] = tmp + m;
621
4.33M
         m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp);
622
4.33M
      }
623
5.05k
   } else
624
171k
#endif
625
171k
   {
626
163M
      for (i=0;i<N;i++)
627
163M
      {
628
163M
         celt_sig x;
629
163M
         x = inp[i];
630
         /* Apply pre-emphasis */
631
163M
         inp[i] = x - m;
632
163M
         m = MULT16_32_Q15(coef0, x);
633
163M
      }
634
171k
   }
635
125k
   *mem = m;
636
125k
}
celt_preemphasis
Line
Count
Source
556
133k
{
557
133k
   int i;
558
133k
   opus_val16 coef0;
559
133k
   celt_sig m;
560
133k
   int Nu;
561
562
133k
   coef0 = coef[0];
563
133k
   m = *mem;
564
565
   /* Fast path for the normal 48kHz case and no clipping */
566
133k
   if (coef[1] == 0 && upsample == 1 && !clip)
567
7.26k
   {
568
3.25M
      for (i=0;i<N;i++)
569
3.25M
      {
570
3.25M
         celt_sig x;
571
3.25M
         x = RES2SIG(pcmp[CC*i]);
572
         /* Apply pre-emphasis */
573
3.25M
         inp[i] = x - m;
574
3.25M
         m = MULT16_32_Q15(coef0, x);
575
3.25M
      }
576
7.26k
      *mem = m;
577
7.26k
      return;
578
7.26k
   }
579
580
125k
   Nu = N/upsample;
581
125k
   if (upsample!=1)
582
120k
   {
583
120k
      OPUS_CLEAR(inp, N);
584
120k
   }
585
21.0M
   for (i=0;i<Nu;i++)
586
20.9M
      inp[i*upsample] = RES2SIG(pcmp[CC*i]);
587
588
#ifndef FIXED_POINT
589
   if (clip)
590
   {
591
      /* Clip input to avoid encoding non-portable files */
592
      for (i=0;i<Nu;i++)
593
         inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample]));
594
   }
595
#else
596
125k
   (void)clip; /* Avoids a warning about clip being unused. */
597
125k
#endif
598
125k
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) || defined(ENABLE_QEXT)
599
125k
   if (coef[1] != 0)
600
5.05k
   {
601
5.05k
      opus_val16 coef1 = coef[1];
602
5.05k
#if defined(FIXED_POINT) && defined(ENABLE_QEXT)
603
      /* If we need the extra precision, we use the fact that coef[3] is exact to do a Newton-Raphson
604
         iteration and get us more precision on coef[2]. */
605
5.05k
      opus_val32 coef2_q30 = SHL32(coef[2], 18) + PSHR32(MULT16_16(QCONST32(1.f, 25) - MULT16_16(coef[3], coef[2]), coef[2]), 7);
606
5.05k
      celt_assert(SIG_SHIFT == 12);
607
#else
608
      opus_val16 coef2 = coef[2];
609
#endif
610
3.47M
      for (i=0;i<N;i++)
611
3.46M
      {
612
3.46M
         celt_sig x, tmp;
613
3.46M
         x = inp[i];
614
         /* Apply pre-emphasis */
615
3.46M
#if defined(FIXED_POINT) && defined(ENABLE_QEXT)
616
3.46M
         tmp = SHL32(MULT32_32_Q31(coef2_q30, x), 1);
617
#else
618
         tmp = SHL32(MULT16_32_Q15(coef2, x), 15-SIG_SHIFT);
619
#endif
620
3.46M
         inp[i] = tmp + m;
621
3.46M
         m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp);
622
3.46M
      }
623
5.05k
   } else
624
120k
#endif
625
120k
   {
626
68.5M
      for (i=0;i<N;i++)
627
68.4M
      {
628
68.4M
         celt_sig x;
629
68.4M
         x = inp[i];
630
         /* Apply pre-emphasis */
631
68.4M
         inp[i] = x - m;
632
68.4M
         m = MULT16_32_Q15(coef0, x);
633
68.4M
      }
634
120k
   }
635
125k
   *mem = m;
636
125k
}
celt_preemphasis
Line
Count
Source
556
57.4k
{
557
57.4k
   int i;
558
57.4k
   opus_val16 coef0;
559
57.4k
   celt_sig m;
560
57.4k
   int Nu;
561
562
57.4k
   coef0 = coef[0];
563
57.4k
   m = *mem;
564
565
   /* Fast path for the normal 48kHz case and no clipping */
566
57.4k
   if (coef[1] == 0 && upsample == 1 && !clip)
567
5.52k
   {
568
3.20M
      for (i=0;i<N;i++)
569
3.19M
      {
570
3.19M
         celt_sig x;
571
3.19M
         x = RES2SIG(pcmp[CC*i]);
572
         /* Apply pre-emphasis */
573
3.19M
         inp[i] = x - m;
574
3.19M
         m = MULT16_32_Q15(coef0, x);
575
3.19M
      }
576
5.52k
      *mem = m;
577
5.52k
      return;
578
5.52k
   }
579
580
51.9k
   Nu = N/upsample;
581
51.9k
   if (upsample!=1)
582
50.6k
   {
583
50.6k
      OPUS_CLEAR(inp, N);
584
50.6k
   }
585
9.16M
   for (i=0;i<Nu;i++)
586
9.11M
      inp[i*upsample] = RES2SIG(pcmp[CC*i]);
587
588
51.9k
#ifndef FIXED_POINT
589
51.9k
   if (clip)
590
0
   {
591
      /* Clip input to avoid encoding non-portable files */
592
0
      for (i=0;i<Nu;i++)
593
0
         inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample]));
594
0
   }
595
#else
596
   (void)clip; /* Avoids a warning about clip being unused. */
597
#endif
598
51.9k
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) || defined(ENABLE_QEXT)
599
51.9k
   if (coef[1] != 0)
600
1.21k
   {
601
1.21k
      opus_val16 coef1 = coef[1];
602
#if defined(FIXED_POINT) && defined(ENABLE_QEXT)
603
      /* If we need the extra precision, we use the fact that coef[3] is exact to do a Newton-Raphson
604
         iteration and get us more precision on coef[2]. */
605
      opus_val32 coef2_q30 = SHL32(coef[2], 18) + PSHR32(MULT16_16(QCONST32(1.f, 25) - MULT16_16(coef[3], coef[2]), coef[2]), 7);
606
      celt_assert(SIG_SHIFT == 12);
607
#else
608
1.21k
      opus_val16 coef2 = coef[2];
609
1.21k
#endif
610
871k
      for (i=0;i<N;i++)
611
870k
      {
612
870k
         celt_sig x, tmp;
613
870k
         x = inp[i];
614
         /* Apply pre-emphasis */
615
#if defined(FIXED_POINT) && defined(ENABLE_QEXT)
616
         tmp = SHL32(MULT32_32_Q31(coef2_q30, x), 1);
617
#else
618
870k
         tmp = SHL32(MULT16_32_Q15(coef2, x), 15-SIG_SHIFT);
619
870k
#endif
620
870k
         inp[i] = tmp + m;
621
870k
         m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp);
622
870k
      }
623
1.21k
   } else
624
50.6k
#endif
625
50.6k
   {
626
32.9M
      for (i=0;i<N;i++)
627
32.8M
      {
628
32.8M
         celt_sig x;
629
32.8M
         x = inp[i];
630
         /* Apply pre-emphasis */
631
32.8M
         inp[i] = x - m;
632
32.8M
         m = MULT16_32_Q15(coef0, x);
633
32.8M
      }
634
50.6k
   }
635
51.9k
   *mem = m;
636
51.9k
}
celt_preemphasis
Line
Count
Source
556
113k
{
557
113k
   int i;
558
113k
   opus_val16 coef0;
559
113k
   celt_sig m;
560
113k
   int Nu;
561
562
113k
   coef0 = coef[0];
563
113k
   m = *mem;
564
565
   /* Fast path for the normal 48kHz case and no clipping */
566
113k
   if (coef[1] == 0 && upsample == 1 && !clip)
567
9.94k
   {
568
5.73M
      for (i=0;i<N;i++)
569
5.72M
      {
570
5.72M
         celt_sig x;
571
5.72M
         x = RES2SIG(pcmp[CC*i]);
572
         /* Apply pre-emphasis */
573
5.72M
         inp[i] = x - m;
574
5.72M
         m = MULT16_32_Q15(coef0, x);
575
5.72M
      }
576
9.94k
      *mem = m;
577
9.94k
      return;
578
9.94k
   }
579
580
103k
   Nu = N/upsample;
581
103k
   if (upsample!=1)
582
103k
   {
583
103k
      OPUS_CLEAR(inp, N);
584
103k
   }
585
16.0M
   for (i=0;i<Nu;i++)
586
15.9M
      inp[i*upsample] = RES2SIG(pcmp[CC*i]);
587
588
103k
#ifndef FIXED_POINT
589
103k
   if (clip)
590
0
   {
591
      /* Clip input to avoid encoding non-portable files */
592
0
      for (i=0;i<Nu;i++)
593
0
         inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample]));
594
0
   }
595
#else
596
   (void)clip; /* Avoids a warning about clip being unused. */
597
#endif
598
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API) || defined(ENABLE_QEXT)
599
   if (coef[1] != 0)
600
   {
601
      opus_val16 coef1 = coef[1];
602
#if defined(FIXED_POINT) && defined(ENABLE_QEXT)
603
      /* If we need the extra precision, we use the fact that coef[3] is exact to do a Newton-Raphson
604
         iteration and get us more precision on coef[2]. */
605
      opus_val32 coef2_q30 = SHL32(coef[2], 18) + PSHR32(MULT16_16(QCONST32(1.f, 25) - MULT16_16(coef[3], coef[2]), coef[2]), 7);
606
      celt_assert(SIG_SHIFT == 12);
607
#else
608
      opus_val16 coef2 = coef[2];
609
#endif
610
      for (i=0;i<N;i++)
611
      {
612
         celt_sig x, tmp;
613
         x = inp[i];
614
         /* Apply pre-emphasis */
615
#if defined(FIXED_POINT) && defined(ENABLE_QEXT)
616
         tmp = SHL32(MULT32_32_Q31(coef2_q30, x), 1);
617
#else
618
         tmp = SHL32(MULT16_32_Q15(coef2, x), 15-SIG_SHIFT);
619
#endif
620
         inp[i] = tmp + m;
621
         m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp);
622
      }
623
   } else
624
#endif
625
103k
   {
626
62.0M
      for (i=0;i<N;i++)
627
61.9M
      {
628
61.9M
         celt_sig x;
629
61.9M
         x = inp[i];
630
         /* Apply pre-emphasis */
631
61.9M
         inp[i] = x - m;
632
61.9M
         m = MULT16_32_Q15(coef0, x);
633
61.9M
      }
634
103k
   }
635
103k
   *mem = m;
636
103k
}
637
638
639
640
static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, opus_val16 bias)
641
4.84M
{
642
4.84M
   int i;
643
4.84M
   opus_val32 L1;
644
4.84M
   L1 = 0;
645
71.0M
   for (i=0;i<N;i++)
646
66.2M
      L1 += EXTEND32(ABS16(SHR32(tmp[i], NORM_SHIFT-14)));
647
   /* When in doubt, prefer good freq resolution */
648
4.84M
   L1 = MAC16_32_Q15(L1, LM*bias, L1);
649
4.84M
   return L1;
650
651
4.84M
}
celt_encoder.c:l1_metric
Line
Count
Source
641
2.29M
{
642
2.29M
   int i;
643
2.29M
   opus_val32 L1;
644
2.29M
   L1 = 0;
645
33.5M
   for (i=0;i<N;i++)
646
31.2M
      L1 += EXTEND32(ABS16(SHR32(tmp[i], NORM_SHIFT-14)));
647
   /* When in doubt, prefer good freq resolution */
648
2.29M
   L1 = MAC16_32_Q15(L1, LM*bias, L1);
649
2.29M
   return L1;
650
651
2.29M
}
celt_encoder.c:l1_metric
Line
Count
Source
641
2.54M
{
642
2.54M
   int i;
643
2.54M
   opus_val32 L1;
644
2.54M
   L1 = 0;
645
37.4M
   for (i=0;i<N;i++)
646
34.9M
      L1 += EXTEND32(ABS16(SHR32(tmp[i], NORM_SHIFT-14)));
647
   /* When in doubt, prefer good freq resolution */
648
2.54M
   L1 = MAC16_32_Q15(L1, LM*bias, L1);
649
2.54M
   return L1;
650
651
2.54M
}
652
653
static int tf_analysis(const CELTMode *m, int len, int isTransient,
654
      int *tf_res, int lambda, celt_norm *X, int N0, int LM,
655
      opus_val16 tf_estimate, int tf_chan, int *importance)
656
188k
{
657
188k
   int i;
658
188k
   VARDECL(int, metric);
659
188k
   int cost0;
660
188k
   int cost1;
661
188k
   VARDECL(int, path0);
662
188k
   VARDECL(int, path1);
663
188k
   VARDECL(celt_norm, tmp);
664
188k
   VARDECL(celt_norm, tmp_1);
665
188k
   int sel;
666
188k
   int selcost[2];
667
188k
   int tf_select=0;
668
188k
   opus_val16 bias;
669
670
188k
   SAVE_STACK;
671
188k
   bias = MULT16_16_Q14(QCONST16(.04f,15), MAX16(-QCONST16(.25f,14), QCONST16(.5f,14)-tf_estimate));
672
   /*printf("%f ", bias);*/
673
674
188k
   ALLOC(metric, len, int);
675
188k
   ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
676
188k
   ALLOC(tmp_1, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
677
188k
   ALLOC(path0, len, int);
678
188k
   ALLOC(path1, len, int);
679
680
3.02M
   for (i=0;i<len;i++)
681
2.83M
   {
682
2.83M
      int k, N;
683
2.83M
      int narrow;
684
2.83M
      opus_val32 L1, best_L1;
685
2.83M
      int best_level=0;
686
2.83M
      N = (m->eBands[i+1]-m->eBands[i])<<LM;
687
      /* band is too narrow to be split down to LM=-1 */
688
2.83M
      narrow = (m->eBands[i+1]-m->eBands[i])==1;
689
2.83M
      OPUS_COPY(tmp, &X[tf_chan*N0 + (m->eBands[i]<<LM)], N);
690
      /* Just add the right channel if we're in stereo */
691
      /*if (C==2)
692
         for (j=0;j<N;j++)
693
            tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));*/
694
2.83M
      L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias);
695
2.83M
      best_L1 = L1;
696
      /* Check the -1 case for transients */
697
2.83M
      if (isTransient && !narrow)
698
800k
      {
699
800k
         OPUS_COPY(tmp_1, tmp, N);
700
800k
         haar1(tmp_1, N>>LM, 1<<LM);
701
800k
         L1 = l1_metric(tmp_1, N, LM+1, bias);
702
800k
         if (L1<best_L1)
703
558k
         {
704
558k
            best_L1 = L1;
705
558k
            best_level = -1;
706
558k
         }
707
800k
      }
708
      /*printf ("%f ", L1);*/
709
8.88M
      for (k=0;k<LM+!(isTransient||narrow);k++)
710
6.04M
      {
711
6.04M
         int B;
712
713
6.04M
         if (isTransient)
714
4.19M
            B = (LM-k-1);
715
1.85M
         else
716
1.85M
            B = k+1;
717
718
6.04M
         haar1(tmp, N>>k, 1<<k);
719
720
6.04M
         L1 = l1_metric(tmp, N, B, bias);
721
722
6.04M
         if (L1 < best_L1)
723
1.07M
         {
724
1.07M
            best_L1 = L1;
725
1.07M
            best_level = k+1;
726
1.07M
         }
727
6.04M
      }
728
      /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
729
      /* metric is in Q1 to be able to select the mid-point (-0.5) for narrower bands */
730
2.83M
      if (isTransient)
731
1.76M
         metric[i] = 2*best_level;
732
1.06M
      else
733
1.06M
         metric[i] = -2*best_level;
734
      /* For bands that can't be split to -1, set the metric to the half-way point to avoid
735
         biasing the decision */
736
2.83M
      if (narrow && (metric[i]==0 || metric[i]==-2*LM))
737
1.18M
         metric[i]-=1;
738
      /*printf("%d ", metric[i]/2 + (!isTransient)*LM);*/
739
2.83M
   }
740
   /*printf("\n");*/
741
   /* Search for the optimal tf resolution, including tf_select */
742
188k
   tf_select = 0;
743
565k
   for (sel=0;sel<2;sel++)
744
376k
   {
745
376k
      cost0 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
746
376k
      cost1 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*sel+1]) + (isTransient ? 0 : lambda);
747
5.66M
      for (i=1;i<len;i++)
748
5.28M
      {
749
5.28M
         int curr0, curr1;
750
5.28M
         curr0 = IMIN(cost0, cost1 + lambda);
751
5.28M
         curr1 = IMIN(cost0 + lambda, cost1);
752
5.28M
         cost0 = curr0 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
753
5.28M
         cost1 = curr1 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+1]);
754
5.28M
      }
755
376k
      cost0 = IMIN(cost0, cost1);
756
376k
      selcost[sel]=cost0;
757
376k
   }
758
   /* For now, we're conservative and only allow tf_select=1 for transients.
759
    * If tests confirm it's useful for non-transients, we could allow it. */
760
188k
   if (selcost[1]<selcost[0] && isTransient)
761
73.8k
      tf_select=1;
762
188k
   cost0 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
763
188k
   cost1 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]) + (isTransient ? 0 : lambda);
764
   /* Viterbi forward pass */
765
2.83M
   for (i=1;i<len;i++)
766
2.64M
   {
767
2.64M
      int curr0, curr1;
768
2.64M
      int from0, from1;
769
770
2.64M
      from0 = cost0;
771
2.64M
      from1 = cost1 + lambda;
772
2.64M
      if (from0 < from1)
773
2.08M
      {
774
2.08M
         curr0 = from0;
775
2.08M
         path0[i]= 0;
776
2.08M
      } else {
777
555k
         curr0 = from1;
778
555k
         path0[i]= 1;
779
555k
      }
780
781
2.64M
      from0 = cost0 + lambda;
782
2.64M
      from1 = cost1;
783
2.64M
      if (from0 < from1)
784
190k
      {
785
190k
         curr1 = from0;
786
190k
         path1[i]= 0;
787
2.45M
      } else {
788
2.45M
         curr1 = from1;
789
2.45M
         path1[i]= 1;
790
2.45M
      }
791
2.64M
      cost0 = curr0 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
792
2.64M
      cost1 = curr1 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]);
793
2.64M
   }
794
188k
   tf_res[len-1] = cost0 < cost1 ? 0 : 1;
795
   /* Viterbi backward pass to check the decisions */
796
2.83M
   for (i=len-2;i>=0;i--)
797
2.64M
   {
798
2.64M
      if (tf_res[i+1] == 1)
799
1.69M
         tf_res[i] = path1[i+1];
800
948k
      else
801
948k
         tf_res[i] = path0[i+1];
802
2.64M
   }
803
   /*printf("%d %f\n", *tf_sum, tf_estimate);*/
804
188k
   RESTORE_STACK;
805
#ifdef FUZZING
806
   tf_select = rand()&0x1;
807
   tf_res[0] = rand()&0x1;
808
   for (i=1;i<len;i++)
809
      tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
810
#endif
811
188k
   return tf_select;
812
188k
}
celt_encoder.c:tf_analysis
Line
Count
Source
656
94.2k
{
657
94.2k
   int i;
658
94.2k
   VARDECL(int, metric);
659
94.2k
   int cost0;
660
94.2k
   int cost1;
661
94.2k
   VARDECL(int, path0);
662
94.2k
   VARDECL(int, path1);
663
94.2k
   VARDECL(celt_norm, tmp);
664
94.2k
   VARDECL(celt_norm, tmp_1);
665
94.2k
   int sel;
666
94.2k
   int selcost[2];
667
94.2k
   int tf_select=0;
668
94.2k
   opus_val16 bias;
669
670
94.2k
   SAVE_STACK;
671
94.2k
   bias = MULT16_16_Q14(QCONST16(.04f,15), MAX16(-QCONST16(.25f,14), QCONST16(.5f,14)-tf_estimate));
672
   /*printf("%f ", bias);*/
673
674
94.2k
   ALLOC(metric, len, int);
675
94.2k
   ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
676
94.2k
   ALLOC(tmp_1, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
677
94.2k
   ALLOC(path0, len, int);
678
94.2k
   ALLOC(path1, len, int);
679
680
1.51M
   for (i=0;i<len;i++)
681
1.41M
   {
682
1.41M
      int k, N;
683
1.41M
      int narrow;
684
1.41M
      opus_val32 L1, best_L1;
685
1.41M
      int best_level=0;
686
1.41M
      N = (m->eBands[i+1]-m->eBands[i])<<LM;
687
      /* band is too narrow to be split down to LM=-1 */
688
1.41M
      narrow = (m->eBands[i+1]-m->eBands[i])==1;
689
1.41M
      OPUS_COPY(tmp, &X[tf_chan*N0 + (m->eBands[i]<<LM)], N);
690
      /* Just add the right channel if we're in stereo */
691
      /*if (C==2)
692
         for (j=0;j<N;j++)
693
            tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));*/
694
1.41M
      L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias);
695
1.41M
      best_L1 = L1;
696
      /* Check the -1 case for transients */
697
1.41M
      if (isTransient && !narrow)
698
400k
      {
699
400k
         OPUS_COPY(tmp_1, tmp, N);
700
400k
         haar1(tmp_1, N>>LM, 1<<LM);
701
400k
         L1 = l1_metric(tmp_1, N, LM+1, bias);
702
400k
         if (L1<best_L1)
703
279k
         {
704
279k
            best_L1 = L1;
705
279k
            best_level = -1;
706
279k
         }
707
400k
      }
708
      /*printf ("%f ", L1);*/
709
4.44M
      for (k=0;k<LM+!(isTransient||narrow);k++)
710
3.02M
      {
711
3.02M
         int B;
712
713
3.02M
         if (isTransient)
714
2.09M
            B = (LM-k-1);
715
927k
         else
716
927k
            B = k+1;
717
718
3.02M
         haar1(tmp, N>>k, 1<<k);
719
720
3.02M
         L1 = l1_metric(tmp, N, B, bias);
721
722
3.02M
         if (L1 < best_L1)
723
537k
         {
724
537k
            best_L1 = L1;
725
537k
            best_level = k+1;
726
537k
         }
727
3.02M
      }
728
      /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
729
      /* metric is in Q1 to be able to select the mid-point (-0.5) for narrower bands */
730
1.41M
      if (isTransient)
731
883k
         metric[i] = 2*best_level;
732
532k
      else
733
532k
         metric[i] = -2*best_level;
734
      /* For bands that can't be split to -1, set the metric to the half-way point to avoid
735
         biasing the decision */
736
1.41M
      if (narrow && (metric[i]==0 || metric[i]==-2*LM))
737
591k
         metric[i]-=1;
738
      /*printf("%d ", metric[i]/2 + (!isTransient)*LM);*/
739
1.41M
   }
740
   /*printf("\n");*/
741
   /* Search for the optimal tf resolution, including tf_select */
742
94.2k
   tf_select = 0;
743
282k
   for (sel=0;sel<2;sel++)
744
188k
   {
745
188k
      cost0 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
746
188k
      cost1 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*sel+1]) + (isTransient ? 0 : lambda);
747
2.83M
      for (i=1;i<len;i++)
748
2.64M
      {
749
2.64M
         int curr0, curr1;
750
2.64M
         curr0 = IMIN(cost0, cost1 + lambda);
751
2.64M
         curr1 = IMIN(cost0 + lambda, cost1);
752
2.64M
         cost0 = curr0 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
753
2.64M
         cost1 = curr1 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+1]);
754
2.64M
      }
755
188k
      cost0 = IMIN(cost0, cost1);
756
188k
      selcost[sel]=cost0;
757
188k
   }
758
   /* For now, we're conservative and only allow tf_select=1 for transients.
759
    * If tests confirm it's useful for non-transients, we could allow it. */
760
94.2k
   if (selcost[1]<selcost[0] && isTransient)
761
36.9k
      tf_select=1;
762
94.2k
   cost0 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
763
94.2k
   cost1 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]) + (isTransient ? 0 : lambda);
764
   /* Viterbi forward pass */
765
1.41M
   for (i=1;i<len;i++)
766
1.32M
   {
767
1.32M
      int curr0, curr1;
768
1.32M
      int from0, from1;
769
770
1.32M
      from0 = cost0;
771
1.32M
      from1 = cost1 + lambda;
772
1.32M
      if (from0 < from1)
773
1.04M
      {
774
1.04M
         curr0 = from0;
775
1.04M
         path0[i]= 0;
776
1.04M
      } else {
777
277k
         curr0 = from1;
778
277k
         path0[i]= 1;
779
277k
      }
780
781
1.32M
      from0 = cost0 + lambda;
782
1.32M
      from1 = cost1;
783
1.32M
      if (from0 < from1)
784
95.4k
      {
785
95.4k
         curr1 = from0;
786
95.4k
         path1[i]= 0;
787
1.22M
      } else {
788
1.22M
         curr1 = from1;
789
1.22M
         path1[i]= 1;
790
1.22M
      }
791
1.32M
      cost0 = curr0 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
792
1.32M
      cost1 = curr1 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]);
793
1.32M
   }
794
94.2k
   tf_res[len-1] = cost0 < cost1 ? 0 : 1;
795
   /* Viterbi backward pass to check the decisions */
796
1.41M
   for (i=len-2;i>=0;i--)
797
1.32M
   {
798
1.32M
      if (tf_res[i+1] == 1)
799
847k
         tf_res[i] = path1[i+1];
800
474k
      else
801
474k
         tf_res[i] = path0[i+1];
802
1.32M
   }
803
   /*printf("%d %f\n", *tf_sum, tf_estimate);*/
804
94.2k
   RESTORE_STACK;
805
#ifdef FUZZING
806
   tf_select = rand()&0x1;
807
   tf_res[0] = rand()&0x1;
808
   for (i=1;i<len;i++)
809
      tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
810
#endif
811
94.2k
   return tf_select;
812
94.2k
}
celt_encoder.c:tf_analysis
Line
Count
Source
656
94.2k
{
657
94.2k
   int i;
658
94.2k
   VARDECL(int, metric);
659
94.2k
   int cost0;
660
94.2k
   int cost1;
661
94.2k
   VARDECL(int, path0);
662
94.2k
   VARDECL(int, path1);
663
94.2k
   VARDECL(celt_norm, tmp);
664
94.2k
   VARDECL(celt_norm, tmp_1);
665
94.2k
   int sel;
666
94.2k
   int selcost[2];
667
94.2k
   int tf_select=0;
668
94.2k
   opus_val16 bias;
669
670
94.2k
   SAVE_STACK;
671
94.2k
   bias = MULT16_16_Q14(QCONST16(.04f,15), MAX16(-QCONST16(.25f,14), QCONST16(.5f,14)-tf_estimate));
672
   /*printf("%f ", bias);*/
673
674
94.2k
   ALLOC(metric, len, int);
675
94.2k
   ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
676
94.2k
   ALLOC(tmp_1, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
677
94.2k
   ALLOC(path0, len, int);
678
94.2k
   ALLOC(path1, len, int);
679
680
1.51M
   for (i=0;i<len;i++)
681
1.41M
   {
682
1.41M
      int k, N;
683
1.41M
      int narrow;
684
1.41M
      opus_val32 L1, best_L1;
685
1.41M
      int best_level=0;
686
1.41M
      N = (m->eBands[i+1]-m->eBands[i])<<LM;
687
      /* band is too narrow to be split down to LM=-1 */
688
1.41M
      narrow = (m->eBands[i+1]-m->eBands[i])==1;
689
1.41M
      OPUS_COPY(tmp, &X[tf_chan*N0 + (m->eBands[i]<<LM)], N);
690
      /* Just add the right channel if we're in stereo */
691
      /*if (C==2)
692
         for (j=0;j<N;j++)
693
            tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));*/
694
1.41M
      L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias);
695
1.41M
      best_L1 = L1;
696
      /* Check the -1 case for transients */
697
1.41M
      if (isTransient && !narrow)
698
400k
      {
699
400k
         OPUS_COPY(tmp_1, tmp, N);
700
400k
         haar1(tmp_1, N>>LM, 1<<LM);
701
400k
         L1 = l1_metric(tmp_1, N, LM+1, bias);
702
400k
         if (L1<best_L1)
703
279k
         {
704
279k
            best_L1 = L1;
705
279k
            best_level = -1;
706
279k
         }
707
400k
      }
708
      /*printf ("%f ", L1);*/
709
4.44M
      for (k=0;k<LM+!(isTransient||narrow);k++)
710
3.02M
      {
711
3.02M
         int B;
712
713
3.02M
         if (isTransient)
714
2.09M
            B = (LM-k-1);
715
927k
         else
716
927k
            B = k+1;
717
718
3.02M
         haar1(tmp, N>>k, 1<<k);
719
720
3.02M
         L1 = l1_metric(tmp, N, B, bias);
721
722
3.02M
         if (L1 < best_L1)
723
537k
         {
724
537k
            best_L1 = L1;
725
537k
            best_level = k+1;
726
537k
         }
727
3.02M
      }
728
      /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
729
      /* metric is in Q1 to be able to select the mid-point (-0.5) for narrower bands */
730
1.41M
      if (isTransient)
731
883k
         metric[i] = 2*best_level;
732
532k
      else
733
532k
         metric[i] = -2*best_level;
734
      /* For bands that can't be split to -1, set the metric to the half-way point to avoid
735
         biasing the decision */
736
1.41M
      if (narrow && (metric[i]==0 || metric[i]==-2*LM))
737
591k
         metric[i]-=1;
738
      /*printf("%d ", metric[i]/2 + (!isTransient)*LM);*/
739
1.41M
   }
740
   /*printf("\n");*/
741
   /* Search for the optimal tf resolution, including tf_select */
742
94.2k
   tf_select = 0;
743
282k
   for (sel=0;sel<2;sel++)
744
188k
   {
745
188k
      cost0 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
746
188k
      cost1 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*sel+1]) + (isTransient ? 0 : lambda);
747
2.83M
      for (i=1;i<len;i++)
748
2.64M
      {
749
2.64M
         int curr0, curr1;
750
2.64M
         curr0 = IMIN(cost0, cost1 + lambda);
751
2.64M
         curr1 = IMIN(cost0 + lambda, cost1);
752
2.64M
         cost0 = curr0 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
753
2.64M
         cost1 = curr1 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+1]);
754
2.64M
      }
755
188k
      cost0 = IMIN(cost0, cost1);
756
188k
      selcost[sel]=cost0;
757
188k
   }
758
   /* For now, we're conservative and only allow tf_select=1 for transients.
759
    * If tests confirm it's useful for non-transients, we could allow it. */
760
94.2k
   if (selcost[1]<selcost[0] && isTransient)
761
36.9k
      tf_select=1;
762
94.2k
   cost0 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
763
94.2k
   cost1 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]) + (isTransient ? 0 : lambda);
764
   /* Viterbi forward pass */
765
1.41M
   for (i=1;i<len;i++)
766
1.32M
   {
767
1.32M
      int curr0, curr1;
768
1.32M
      int from0, from1;
769
770
1.32M
      from0 = cost0;
771
1.32M
      from1 = cost1 + lambda;
772
1.32M
      if (from0 < from1)
773
1.04M
      {
774
1.04M
         curr0 = from0;
775
1.04M
         path0[i]= 0;
776
1.04M
      } else {
777
277k
         curr0 = from1;
778
277k
         path0[i]= 1;
779
277k
      }
780
781
1.32M
      from0 = cost0 + lambda;
782
1.32M
      from1 = cost1;
783
1.32M
      if (from0 < from1)
784
95.4k
      {
785
95.4k
         curr1 = from0;
786
95.4k
         path1[i]= 0;
787
1.22M
      } else {
788
1.22M
         curr1 = from1;
789
1.22M
         path1[i]= 1;
790
1.22M
      }
791
1.32M
      cost0 = curr0 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
792
1.32M
      cost1 = curr1 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]);
793
1.32M
   }
794
94.2k
   tf_res[len-1] = cost0 < cost1 ? 0 : 1;
795
   /* Viterbi backward pass to check the decisions */
796
1.41M
   for (i=len-2;i>=0;i--)
797
1.32M
   {
798
1.32M
      if (tf_res[i+1] == 1)
799
847k
         tf_res[i] = path1[i+1];
800
474k
      else
801
474k
         tf_res[i] = path0[i+1];
802
1.32M
   }
803
   /*printf("%d %f\n", *tf_sum, tf_estimate);*/
804
94.2k
   RESTORE_STACK;
805
#ifdef FUZZING
806
   tf_select = rand()&0x1;
807
   tf_res[0] = rand()&0x1;
808
   for (i=1;i<len;i++)
809
      tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
810
#endif
811
94.2k
   return tf_select;
812
94.2k
}
813
814
static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
815
195k
{
816
195k
   int curr, i;
817
195k
   int tf_select_rsv;
818
195k
   int tf_changed;
819
195k
   int logp;
820
195k
   opus_uint32 budget;
821
195k
   opus_uint32 tell;
822
195k
   budget = enc->storage*8;
823
195k
   tell = ec_tell(enc);
824
195k
   logp = isTransient ? 2 : 4;
825
   /* Reserve space to code the tf_select decision. */
826
195k
   tf_select_rsv = LM>0 && tell+logp+1 <= budget;
827
195k
   budget -= tf_select_rsv;
828
195k
   curr = tf_changed = 0;
829
2.90M
   for (i=start;i<end;i++)
830
2.71M
   {
831
2.71M
      if (tell+logp<=budget)
832
2.11M
      {
833
2.11M
         ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
834
2.11M
         tell = ec_tell(enc);
835
2.11M
         curr = tf_res[i];
836
2.11M
         tf_changed |= curr;
837
2.11M
      }
838
598k
      else
839
598k
         tf_res[i] = curr;
840
2.71M
      logp = isTransient ? 4 : 5;
841
2.71M
   }
842
   /* Only code tf_select if it would actually make a difference. */
843
195k
   if (tf_select_rsv &&
844
195k
         tf_select_table[LM][4*isTransient+0+tf_changed]!=
845
97.0k
         tf_select_table[LM][4*isTransient+2+tf_changed])
846
76.4k
      ec_enc_bit_logp(enc, tf_select, 1);
847
119k
   else
848
119k
      tf_select = 0;
849
2.90M
   for (i=start;i<end;i++)
850
2.71M
      tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
851
   /*for(i=0;i<end;i++)printf("%d ", isTransient ? tf_res[i] : LM+tf_res[i]);printf("\n");*/
852
195k
}
853
854
855
static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
856
      const celt_glog *bandLogE, int end, int LM, int C, int N0,
857
      AnalysisInfo *analysis, opus_val16 *stereo_saving, opus_val16 tf_estimate,
858
      int intensity, celt_glog surround_trim, opus_int32 equiv_rate, int arch)
859
140k
{
860
140k
   int i;
861
140k
   opus_val32 diff=0;
862
140k
   int c;
863
140k
   int trim_index;
864
140k
   opus_val16 trim = QCONST16(5.f, 8);
865
140k
   opus_val16 logXC, logXC2;
866
   /* At low bitrate, reducing the trim seems to help. At higher bitrates, it's less
867
      clear what's best, so we're keeping it as it was before, at least for now. */
868
140k
   if (equiv_rate < 64000) {
869
124k
      trim = QCONST16(4.f, 8);
870
124k
   } else if (equiv_rate < 80000) {
871
7.26k
      opus_int32 frac = (equiv_rate-64000) >> 10;
872
7.26k
      trim = QCONST16(4.f, 8) + QCONST16(1.f/16.f, 8)*frac;
873
7.26k
   }
874
140k
   if (C==2)
875
57.9k
   {
876
57.9k
      opus_val16 sum = 0; /* Q10 */
877
57.9k
      opus_val16 minXC; /* Q10 */
878
      /* Compute inter-channel correlation for low frequencies */
879
521k
      for (i=0;i<8;i++)
880
463k
      {
881
463k
         opus_val32 partial;
882
463k
         partial = celt_inner_prod_norm_shift(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM)],
883
463k
               (m->eBands[i+1]-m->eBands[i])<<LM, arch);
884
463k
         sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
885
463k
      }
886
57.9k
      sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
887
57.9k
      sum = MIN16(QCONST16(1.f, 10), ABS16(sum));
888
57.9k
      minXC = sum;
889
291k
      for (i=8;i<intensity;i++)
890
233k
      {
891
233k
         opus_val32 partial;
892
233k
         partial = celt_inner_prod_norm_shift(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM)],
893
233k
               (m->eBands[i+1]-m->eBands[i])<<LM, arch);
894
233k
         minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18))));
895
233k
      }
896
57.9k
      minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC));
897
      /*printf ("%f\n", sum);*/
898
      /* mid-side savings estimations based on the LF average*/
899
57.9k
      logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum));
900
      /* mid-side savings estimations based on min correlation */
901
57.9k
      logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(minXC, minXC)));
902
#ifdef FIXED_POINT
903
      /* Compensate for Q20 vs Q14 input and convert output to Q8 */
904
26.3k
      logXC = PSHR32(logXC-QCONST16(6.f, 10),10-8);
905
26.3k
      logXC2 = PSHR32(logXC2-QCONST16(6.f, 10),10-8);
906
#endif
907
908
57.9k
      trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC));
909
57.9k
      *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2));
910
57.9k
   }
911
912
   /* Estimate spectral tilt */
913
198k
   c=0; do {
914
2.89M
      for (i=0;i<end-1;i++)
915
2.69M
      {
916
2.69M
         diff += SHR32(bandLogE[i+c*m->nbEBands], 5)*(opus_int32)(2+2*i-end);
917
2.69M
      }
918
198k
   } while (++c<C);
919
140k
   diff /= C*(end-1);
920
   /*printf("%f\n", diff);*/
921
140k
   trim -= MAX32(-QCONST16(2.f, 8), MIN32(QCONST16(2.f, 8), SHR32(diff+QCONST32(1.f, DB_SHIFT-5),DB_SHIFT-13)/6 ));
922
140k
   trim -= SHR16(surround_trim, DB_SHIFT-8);
923
140k
   trim -= 2*SHR16(tf_estimate, 14-8);
924
140k
#ifndef DISABLE_FLOAT_API
925
140k
   if (analysis->valid)
926
18.7k
   {
927
18.7k
      trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8),
928
18.7k
            (opus_val16)(QCONST16(2.f, 8)*(analysis->tonality_slope+.05f))));
929
18.7k
   }
930
#else
931
   (void)analysis;
932
#endif
933
934
#ifdef FIXED_POINT
935
69.0k
   trim_index = PSHR32(trim, 8);
936
#else
937
   trim_index = (int)floor(.5f+trim);
938
#endif
939
140k
   trim_index = IMAX(0, IMIN(10, trim_index));
940
   /*printf("%d\n", trim_index);*/
941
#ifdef FUZZING
942
   trim_index = rand()%11;
943
#endif
944
140k
   return trim_index;
945
140k
}
celt_encoder.c:alloc_trim_analysis
Line
Count
Source
859
69.0k
{
860
69.0k
   int i;
861
69.0k
   opus_val32 diff=0;
862
69.0k
   int c;
863
69.0k
   int trim_index;
864
69.0k
   opus_val16 trim = QCONST16(5.f, 8);
865
69.0k
   opus_val16 logXC, logXC2;
866
   /* At low bitrate, reducing the trim seems to help. At higher bitrates, it's less
867
      clear what's best, so we're keeping it as it was before, at least for now. */
868
69.0k
   if (equiv_rate < 64000) {
869
60.9k
      trim = QCONST16(4.f, 8);
870
60.9k
   } else if (equiv_rate < 80000) {
871
3.55k
      opus_int32 frac = (equiv_rate-64000) >> 10;
872
3.55k
      trim = QCONST16(4.f, 8) + QCONST16(1.f/16.f, 8)*frac;
873
3.55k
   }
874
69.0k
   if (C==2)
875
26.3k
   {
876
26.3k
      opus_val16 sum = 0; /* Q10 */
877
26.3k
      opus_val16 minXC; /* Q10 */
878
      /* Compute inter-channel correlation for low frequencies */
879
237k
      for (i=0;i<8;i++)
880
211k
      {
881
211k
         opus_val32 partial;
882
211k
         partial = celt_inner_prod_norm_shift(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM)],
883
211k
               (m->eBands[i+1]-m->eBands[i])<<LM, arch);
884
211k
         sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
885
211k
      }
886
26.3k
      sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
887
26.3k
      sum = MIN16(QCONST16(1.f, 10), ABS16(sum));
888
26.3k
      minXC = sum;
889
132k
      for (i=8;i<intensity;i++)
890
106k
      {
891
106k
         opus_val32 partial;
892
106k
         partial = celt_inner_prod_norm_shift(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM)],
893
106k
               (m->eBands[i+1]-m->eBands[i])<<LM, arch);
894
106k
         minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18))));
895
106k
      }
896
26.3k
      minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC));
897
      /*printf ("%f\n", sum);*/
898
      /* mid-side savings estimations based on the LF average*/
899
26.3k
      logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum));
900
      /* mid-side savings estimations based on min correlation */
901
26.3k
      logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(minXC, minXC)));
902
26.3k
#ifdef FIXED_POINT
903
      /* Compensate for Q20 vs Q14 input and convert output to Q8 */
904
26.3k
      logXC = PSHR32(logXC-QCONST16(6.f, 10),10-8);
905
26.3k
      logXC2 = PSHR32(logXC2-QCONST16(6.f, 10),10-8);
906
26.3k
#endif
907
908
26.3k
      trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC));
909
26.3k
      *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2));
910
26.3k
   }
911
912
   /* Estimate spectral tilt */
913
95.3k
   c=0; do {
914
1.38M
      for (i=0;i<end-1;i++)
915
1.29M
      {
916
1.29M
         diff += SHR32(bandLogE[i+c*m->nbEBands], 5)*(opus_int32)(2+2*i-end);
917
1.29M
      }
918
95.3k
   } while (++c<C);
919
69.0k
   diff /= C*(end-1);
920
   /*printf("%f\n", diff);*/
921
69.0k
   trim -= MAX32(-QCONST16(2.f, 8), MIN32(QCONST16(2.f, 8), SHR32(diff+QCONST32(1.f, DB_SHIFT-5),DB_SHIFT-13)/6 ));
922
69.0k
   trim -= SHR16(surround_trim, DB_SHIFT-8);
923
69.0k
   trim -= 2*SHR16(tf_estimate, 14-8);
924
69.0k
#ifndef DISABLE_FLOAT_API
925
69.0k
   if (analysis->valid)
926
8.68k
   {
927
8.68k
      trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8),
928
8.68k
            (opus_val16)(QCONST16(2.f, 8)*(analysis->tonality_slope+.05f))));
929
8.68k
   }
930
#else
931
   (void)analysis;
932
#endif
933
934
69.0k
#ifdef FIXED_POINT
935
69.0k
   trim_index = PSHR32(trim, 8);
936
#else
937
   trim_index = (int)floor(.5f+trim);
938
#endif
939
69.0k
   trim_index = IMAX(0, IMIN(10, trim_index));
940
   /*printf("%d\n", trim_index);*/
941
#ifdef FUZZING
942
   trim_index = rand()%11;
943
#endif
944
69.0k
   return trim_index;
945
69.0k
}
celt_encoder.c:alloc_trim_analysis
Line
Count
Source
859
71.5k
{
860
71.5k
   int i;
861
71.5k
   opus_val32 diff=0;
862
71.5k
   int c;
863
71.5k
   int trim_index;
864
71.5k
   opus_val16 trim = QCONST16(5.f, 8);
865
71.5k
   opus_val16 logXC, logXC2;
866
   /* At low bitrate, reducing the trim seems to help. At higher bitrates, it's less
867
      clear what's best, so we're keeping it as it was before, at least for now. */
868
71.5k
   if (equiv_rate < 64000) {
869
63.4k
      trim = QCONST16(4.f, 8);
870
63.4k
   } else if (equiv_rate < 80000) {
871
3.70k
      opus_int32 frac = (equiv_rate-64000) >> 10;
872
3.70k
      trim = QCONST16(4.f, 8) + QCONST16(1.f/16.f, 8)*frac;
873
3.70k
   }
874
71.5k
   if (C==2)
875
31.5k
   {
876
31.5k
      opus_val16 sum = 0; /* Q10 */
877
31.5k
      opus_val16 minXC; /* Q10 */
878
      /* Compute inter-channel correlation for low frequencies */
879
284k
      for (i=0;i<8;i++)
880
252k
      {
881
252k
         opus_val32 partial;
882
252k
         partial = celt_inner_prod_norm_shift(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM)],
883
252k
               (m->eBands[i+1]-m->eBands[i])<<LM, arch);
884
252k
         sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
885
252k
      }
886
31.5k
      sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
887
31.5k
      sum = MIN16(QCONST16(1.f, 10), ABS16(sum));
888
31.5k
      minXC = sum;
889
158k
      for (i=8;i<intensity;i++)
890
127k
      {
891
127k
         opus_val32 partial;
892
127k
         partial = celt_inner_prod_norm_shift(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM)],
893
127k
               (m->eBands[i+1]-m->eBands[i])<<LM, arch);
894
127k
         minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18))));
895
127k
      }
896
31.5k
      minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC));
897
      /*printf ("%f\n", sum);*/
898
      /* mid-side savings estimations based on the LF average*/
899
31.5k
      logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum));
900
      /* mid-side savings estimations based on min correlation */
901
31.5k
      logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(minXC, minXC)));
902
#ifdef FIXED_POINT
903
      /* Compensate for Q20 vs Q14 input and convert output to Q8 */
904
      logXC = PSHR32(logXC-QCONST16(6.f, 10),10-8);
905
      logXC2 = PSHR32(logXC2-QCONST16(6.f, 10),10-8);
906
#endif
907
908
31.5k
      trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC));
909
31.5k
      *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2));
910
31.5k
   }
911
912
   /* Estimate spectral tilt */
913
103k
   c=0; do {
914
1.50M
      for (i=0;i<end-1;i++)
915
1.40M
      {
916
1.40M
         diff += SHR32(bandLogE[i+c*m->nbEBands], 5)*(opus_int32)(2+2*i-end);
917
1.40M
      }
918
103k
   } while (++c<C);
919
71.5k
   diff /= C*(end-1);
920
   /*printf("%f\n", diff);*/
921
71.5k
   trim -= MAX32(-QCONST16(2.f, 8), MIN32(QCONST16(2.f, 8), SHR32(diff+QCONST32(1.f, DB_SHIFT-5),DB_SHIFT-13)/6 ));
922
71.5k
   trim -= SHR16(surround_trim, DB_SHIFT-8);
923
71.5k
   trim -= 2*SHR16(tf_estimate, 14-8);
924
71.5k
#ifndef DISABLE_FLOAT_API
925
71.5k
   if (analysis->valid)
926
10.0k
   {
927
10.0k
      trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8),
928
10.0k
            (opus_val16)(QCONST16(2.f, 8)*(analysis->tonality_slope+.05f))));
929
10.0k
   }
930
#else
931
   (void)analysis;
932
#endif
933
934
#ifdef FIXED_POINT
935
   trim_index = PSHR32(trim, 8);
936
#else
937
71.5k
   trim_index = (int)floor(.5f+trim);
938
71.5k
#endif
939
71.5k
   trim_index = IMAX(0, IMIN(10, trim_index));
940
   /*printf("%d\n", trim_index);*/
941
#ifdef FUZZING
942
   trim_index = rand()%11;
943
#endif
944
71.5k
   return trim_index;
945
71.5k
}
946
947
static int stereo_analysis(const CELTMode *m, const celt_norm *X,
948
      int LM, int N0)
949
54.4k
{
950
54.4k
   int i;
951
54.4k
   int thetas;
952
54.4k
   opus_val32 sumLR = EPSILON, sumMS = EPSILON;
953
954
   /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
955
762k
   for (i=0;i<13;i++)
956
707k
   {
957
707k
      int j;
958
7.83M
      for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
959
7.12M
      {
960
7.12M
         opus_val32 L, R, M, S;
961
         /* We cast to 32-bit first because of the -32768 case */
962
7.12M
         L = SHR32(X[j], NORM_SHIFT-14);
963
7.12M
         R = SHR32(X[N0+j], NORM_SHIFT-14);
964
7.12M
         M = ADD32(L, R);
965
7.12M
         S = SUB32(L, R);
966
7.12M
         sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
967
7.12M
         sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
968
7.12M
      }
969
707k
   }
970
54.4k
   sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
971
54.4k
   thetas = 13;
972
   /* We don't need thetas for lower bands with LM<=1 */
973
54.4k
   if (LM<=1)
974
9.16k
      thetas -= 8;
975
54.4k
   return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
976
54.4k
         > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
977
54.4k
}
celt_encoder.c:stereo_analysis
Line
Count
Source
949
21.7k
{
950
21.7k
   int i;
951
21.7k
   int thetas;
952
21.7k
   opus_val32 sumLR = EPSILON, sumMS = EPSILON;
953
954
   /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
955
305k
   for (i=0;i<13;i++)
956
283k
   {
957
283k
      int j;
958
3.10M
      for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
959
2.82M
      {
960
2.82M
         opus_val32 L, R, M, S;
961
         /* We cast to 32-bit first because of the -32768 case */
962
2.82M
         L = SHR32(X[j], NORM_SHIFT-14);
963
2.82M
         R = SHR32(X[N0+j], NORM_SHIFT-14);
964
2.82M
         M = ADD32(L, R);
965
2.82M
         S = SUB32(L, R);
966
2.82M
         sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
967
2.82M
         sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
968
2.82M
      }
969
283k
   }
970
21.7k
   sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
971
21.7k
   thetas = 13;
972
   /* We don't need thetas for lower bands with LM<=1 */
973
21.7k
   if (LM<=1)
974
3.84k
      thetas -= 8;
975
21.7k
   return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
976
21.7k
         > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
977
21.7k
}
celt_encoder.c:stereo_analysis
Line
Count
Source
949
32.6k
{
950
32.6k
   int i;
951
32.6k
   int thetas;
952
32.6k
   opus_val32 sumLR = EPSILON, sumMS = EPSILON;
953
954
   /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
955
457k
   for (i=0;i<13;i++)
956
424k
   {
957
424k
      int j;
958
4.72M
      for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
959
4.30M
      {
960
4.30M
         opus_val32 L, R, M, S;
961
         /* We cast to 32-bit first because of the -32768 case */
962
4.30M
         L = SHR32(X[j], NORM_SHIFT-14);
963
4.30M
         R = SHR32(X[N0+j], NORM_SHIFT-14);
964
4.30M
         M = ADD32(L, R);
965
4.30M
         S = SUB32(L, R);
966
4.30M
         sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
967
4.30M
         sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
968
4.30M
      }
969
424k
   }
970
32.6k
   sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
971
32.6k
   thetas = 13;
972
   /* We don't need thetas for lower bands with LM<=1 */
973
32.6k
   if (LM<=1)
974
5.31k
      thetas -= 8;
975
32.6k
   return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
976
32.6k
         > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
977
32.6k
}
978
979
573k
#define MSWAP(a,b) do {celt_glog tmp = a;a=b;b=tmp;} while(0)
980
static celt_glog median_of_5(const celt_glog *x)
981
1.63M
{
982
1.63M
   celt_glog t0, t1, t2, t3, t4;
983
1.63M
   t2 = x[2];
984
1.63M
   if (x[0] > x[1])
985
537k
   {
986
537k
      t0 = x[1];
987
537k
      t1 = x[0];
988
1.09M
   } else {
989
1.09M
      t0 = x[0];
990
1.09M
      t1 = x[1];
991
1.09M
   }
992
1.63M
   if (x[3] > x[4])
993
479k
   {
994
479k
      t3 = x[4];
995
479k
      t4 = x[3];
996
1.15M
   } else {
997
1.15M
      t3 = x[3];
998
1.15M
      t4 = x[4];
999
1.15M
   }
1000
1.63M
   if (t0 > t3)
1001
286k
   {
1002
286k
      MSWAP(t0, t3);
1003
286k
      MSWAP(t1, t4);
1004
286k
   }
1005
1.63M
   if (t2 > t1)
1006
1.01M
   {
1007
1.01M
      if (t1 < t3)
1008
849k
         return MING(t2, t3);
1009
163k
      else
1010
163k
         return MING(t4, t1);
1011
1.01M
   } else {
1012
618k
      if (t2 < t3)
1013
483k
         return MING(t1, t3);
1014
134k
      else
1015
134k
         return MING(t2, t4);
1016
618k
   }
1017
1.63M
}
1018
1019
static celt_glog median_of_3(const celt_glog *x)
1020
266k
{
1021
266k
   celt_glog t0, t1, t2;
1022
266k
   if (x[0] > x[1])
1023
106k
   {
1024
106k
      t0 = x[1];
1025
106k
      t1 = x[0];
1026
159k
   } else {
1027
159k
      t0 = x[0];
1028
159k
      t1 = x[1];
1029
159k
   }
1030
266k
   t2 = x[2];
1031
266k
   if (t1 < t2)
1032
161k
      return t1;
1033
104k
   else if (t0 < t2)
1034
42.2k
      return t2;
1035
62.5k
   else
1036
62.5k
      return t0;
1037
266k
}
1038
1039
static celt_glog dynalloc_analysis(const celt_glog *bandLogE, const celt_glog *bandLogE2, const celt_glog *oldBandE,
1040
      int nbEBands, int start, int end, int C, int *offsets, int lsb_depth, const opus_int16 *logN,
1041
      int isTransient, int vbr, int constrained_vbr, const opus_int16 *eBands, int LM,
1042
      int effectiveBytes, opus_int32 *tot_boost_, int lfe, celt_glog *surround_dynalloc,
1043
      AnalysisInfo *analysis, int *importance, int *spread_weight, opus_val16 tone_freq, opus_val32 toneishness
1044
      ARG_QEXT(int qext_scale))
1045
195k
{
1046
195k
   int i, c;
1047
195k
   opus_int32 tot_boost=0;
1048
195k
   celt_glog maxDepth;
1049
195k
   VARDECL(celt_glog, follower);
1050
195k
   VARDECL(celt_glog, noise_floor);
1051
195k
   VARDECL(celt_glog, bandLogE3);
1052
195k
   SAVE_STACK;
1053
195k
   ALLOC(follower, C*nbEBands, celt_glog);
1054
195k
   ALLOC(noise_floor, C*nbEBands, celt_glog);
1055
195k
   ALLOC(bandLogE3, nbEBands, celt_glog);
1056
195k
   OPUS_CLEAR(offsets, nbEBands);
1057
   /* Dynamic allocation code */
1058
195k
   maxDepth=-GCONST(31.9f);
1059
3.10M
   for (i=0;i<end;i++)
1060
2.90M
   {
1061
      /* Noise floor must take into account eMeans, the depth, the width of the bands
1062
         and the preemphasis filter (approx. square of bark band ID) */
1063
2.90M
      noise_floor[i] = GCONST(0.0625f)*logN[i]
1064
2.90M
            +GCONST(.5f)+SHL32(9-lsb_depth,DB_SHIFT)-SHL32(eMeans[i],DB_SHIFT-4)
1065
2.90M
            +GCONST(.0062f)*(i+5)*(i+5);
1066
2.90M
   }
1067
195k
   c=0;do
1068
271k
   {
1069
4.30M
      for (i=0;i<end;i++)
1070
4.03M
         maxDepth = MAXG(maxDepth, bandLogE[c*nbEBands+i]-noise_floor[i]);
1071
271k
   } while (++c<C);
1072
195k
   {
1073
      /* Compute a really simple masking model to avoid taking into account completely masked
1074
         bands when computing the spreading decision. */
1075
195k
      VARDECL(celt_glog, mask);
1076
195k
      VARDECL(celt_glog, sig);
1077
195k
      ALLOC(mask, nbEBands, celt_glog);
1078
195k
      ALLOC(sig, nbEBands, celt_glog);
1079
3.10M
      for (i=0;i<end;i++)
1080
2.90M
         mask[i] = bandLogE[i]-noise_floor[i];
1081
195k
      if (C==2)
1082
75.2k
      {
1083
1.20M
         for (i=0;i<end;i++)
1084
1.12M
            mask[i] = MAXG(mask[i], bandLogE[nbEBands+i]-noise_floor[i]);
1085
75.2k
      }
1086
195k
      OPUS_COPY(sig, mask, end);
1087
2.90M
      for (i=1;i<end;i++)
1088
2.71M
         mask[i] = MAXG(mask[i], mask[i-1] - GCONST(2.f));
1089
2.90M
      for (i=end-2;i>=0;i--)
1090
2.71M
         mask[i] = MAXG(mask[i], mask[i+1] - GCONST(3.f));
1091
3.10M
      for (i=0;i<end;i++)
1092
2.90M
      {
1093
         /* Compute SMR: Mask is never more than 72 dB below the peak and never below the noise floor.*/
1094
2.90M
         celt_glog smr = sig[i]-MAXG(MAXG(0, maxDepth-GCONST(12.f)), mask[i]);
1095
         /* Clamp SMR to make sure we're not shifting by something negative or too large. */
1096
#ifdef FIXED_POINT
1097
         /* FIXME: Use PSHR16() instead */
1098
1.31M
         int shift = -PSHR32(MAXG(-GCONST(5.f), MING(0, smr)), DB_SHIFT);
1099
#else
1100
1.59M
         int shift = IMIN(5, IMAX(0, -(int)floor(.5f + smr)));
1101
#endif
1102
2.90M
         spread_weight[i] = 32 >> shift;
1103
2.90M
      }
1104
      /*for (i=0;i<end;i++)
1105
         printf("%d ", spread_weight[i]);
1106
      printf("\n");*/
1107
195k
   }
1108
   /* Make sure that dynamic allocation can't make us bust the budget.
1109
      We enable the feature starting at 24 kb/s for 20-ms frames
1110
      and 96 kb/s for 2.5 ms frames.  */
1111
195k
   if (effectiveBytes >= (30 + 5*LM) && !lfe)
1112
86.1k
   {
1113
86.1k
      int last=0;
1114
86.1k
      c=0;do
1115
133k
      {
1116
133k
         celt_glog offset;
1117
133k
         celt_glog tmp;
1118
133k
         celt_glog *f;
1119
133k
         OPUS_COPY(bandLogE3, &bandLogE2[c*nbEBands], end);
1120
133k
         if (LM==0) {
1121
            /* For 2.5 ms frames, the first 8 bands have just one bin, so the
1122
               energy is highly unreliable (high variance). For that reason,
1123
               we take the max with the previous energy so that at least 2 bins
1124
               are getting used. */
1125
143k
            for (i=0;i<IMIN(8,end);i++) bandLogE3[i] = MAXG(bandLogE2[c*nbEBands+i], oldBandE[c*nbEBands+i]);
1126
15.9k
         }
1127
133k
         f = &follower[c*nbEBands];
1128
133k
         f[0] = bandLogE3[0];
1129
2.03M
         for (i=1;i<end;i++)
1130
1.90M
         {
1131
            /* The last band to be at least 3 dB higher than the previous one
1132
               is the last we'll consider. Otherwise, we run into problems on
1133
               bandlimited signals. */
1134
1.90M
            if (bandLogE3[i] > bandLogE3[i-1]+GCONST(.5f))
1135
607k
               last=i;
1136
1.90M
            f[i] = MING(f[i-1]+GCONST(1.5f), bandLogE3[i]);
1137
1.90M
         }
1138
1.67M
         for (i=last-1;i>=0;i--)
1139
1.54M
            f[i] = MING(f[i], MING(f[i+1]+GCONST(2.f), bandLogE3[i]));
1140
1141
         /* Combine with a median filter to avoid dynalloc triggering unnecessarily.
1142
            The "offset" value controls how conservative we are -- a higher offset
1143
            reduces the impact of the median filter and makes dynalloc use more bits. */
1144
133k
         offset = GCONST(1.f);
1145
1.63M
         for (i=2;i<end-2;i++)
1146
1.50M
            f[i] = MAXG(f[i], median_of_5(&bandLogE3[i-2])-offset);
1147
133k
         tmp = median_of_3(&bandLogE3[0])-offset;
1148
133k
         f[0] = MAXG(f[0], tmp);
1149
133k
         f[1] = MAXG(f[1], tmp);
1150
133k
         tmp = median_of_3(&bandLogE3[end-3])-offset;
1151
133k
         f[end-2] = MAXG(f[end-2], tmp);
1152
133k
         f[end-1] = MAXG(f[end-1], tmp);
1153
1154
2.16M
         for (i=0;i<end;i++)
1155
2.03M
            f[i] = MAXG(f[i], noise_floor[i]);
1156
133k
      } while (++c<C);
1157
86.1k
      if (C==2)
1158
46.9k
      {
1159
721k
         for (i=start;i<end;i++)
1160
674k
         {
1161
            /* Consider 24 dB "cross-talk" */
1162
674k
            follower[nbEBands+i] = MAXG(follower[nbEBands+i], follower[         i]-GCONST(4.f));
1163
674k
            follower[         i] = MAXG(follower[         i], follower[nbEBands+i]-GCONST(4.f));
1164
674k
            follower[i] = HALF32(MAXG(0, bandLogE[i]-follower[i]) + MAXG(0, bandLogE[nbEBands+i]-follower[nbEBands+i]));
1165
674k
         }
1166
46.9k
      } else {
1167
613k
         for (i=start;i<end;i++)
1168
574k
         {
1169
574k
            follower[i] = MAXG(0, bandLogE[i]-follower[i]);
1170
574k
         }
1171
39.2k
      }
1172
1.33M
      for (i=start;i<end;i++)
1173
1.24M
         follower[i] = MAXG(follower[i], surround_dynalloc[i]);
1174
1.33M
      for (i=start;i<end;i++)
1175
1.24M
      {
1176
#ifdef FIXED_POINT
1177
582k
         importance[i] = PSHR32(13*celt_exp2_db(MING(follower[i], GCONST(4.f))), 16);
1178
#else
1179
666k
         importance[i] = (int)floor(.5f+13*celt_exp2_db(MING(follower[i], GCONST(4.f))));
1180
#endif
1181
1.24M
      }
1182
      /* For non-transient CBR/CVBR frames, halve the dynalloc contribution */
1183
86.1k
      if ((!vbr || constrained_vbr)&&!isTransient)
1184
16.7k
      {
1185
276k
         for (i=start;i<end;i++)
1186
259k
            follower[i] = HALF32(follower[i]);
1187
16.7k
      }
1188
1.33M
      for (i=start;i<end;i++)
1189
1.24M
      {
1190
1.24M
         if (i<8)
1191
656k
            follower[i] *= 2;
1192
1.24M
         if (i>=12)
1193
264k
            follower[i] = HALF32(follower[i]);
1194
1.24M
      }
1195
      /* Compensate for Opus' under-allocation on tones. */
1196
86.1k
      if (toneishness > QCONST32(.98f, 29)) {
1197
#ifdef FIXED_POINT
1198
362
         int freq_bin = PSHR32(QEXT_SCALE((opus_val32)tone_freq)*QCONST16(120/M_PI, 9), 13+9);
1199
#else
1200
486
         int freq_bin = (int)floor(.5 + QEXT_SCALE(tone_freq)*120/M_PI);
1201
#endif
1202
15.0k
         for (i=start;i<end;i++) {
1203
14.2k
            if (freq_bin >= eBands[i] && freq_bin <= eBands[i+1]) follower[i] += GCONST(2.f);
1204
14.2k
            if (freq_bin >= eBands[i]-1 && freq_bin <= eBands[i+1]+1) follower[i] += GCONST(1.f);
1205
14.2k
            if (freq_bin >= eBands[i]-2 && freq_bin <= eBands[i+1]+2) follower[i] += GCONST(1.f);
1206
14.2k
            if (freq_bin >= eBands[i]-3 && freq_bin <= eBands[i+1]+3) follower[i] += GCONST(.5f);
1207
14.2k
         }
1208
848
         if (freq_bin >= eBands[end]) {
1209
424
            follower[end-1] += GCONST(2.f);
1210
424
            follower[end-2] += GCONST(1.f);
1211
424
         }
1212
848
      }
1213
#ifdef DISABLE_FLOAT_API
1214
      (void)analysis;
1215
#else
1216
86.1k
      if (analysis->valid)
1217
21.7k
      {
1218
342k
         for (i=start;i<IMIN(LEAK_BANDS, end);i++)
1219
320k
            follower[i] = follower[i] +  GCONST(1.f/64.f)*analysis->leak_boost[i];
1220
21.7k
      }
1221
86.1k
#endif
1222
1.33M
      for (i=start;i<end;i++)
1223
1.24M
      {
1224
1.24M
         int width;
1225
1.24M
         int boost;
1226
1.24M
         int boost_bits;
1227
1228
1.24M
         follower[i] = MING(follower[i], GCONST(4));
1229
1230
1.24M
         follower[i] = SHR32(follower[i], 8);
1231
1.24M
         width = C*(eBands[i+1]-eBands[i])<<LM;
1232
1.24M
         if (width<6)
1233
207k
         {
1234
207k
            boost = (int)SHR32(follower[i],DB_SHIFT-8);
1235
207k
            boost_bits = boost*width<<BITRES;
1236
1.04M
         } else if (width > 48) {
1237
82.5k
            boost = (int)SHR32(follower[i]*8,DB_SHIFT-8);
1238
82.5k
            boost_bits = (boost*width<<BITRES)/8;
1239
958k
         } else {
1240
958k
            boost = (int)SHR32(follower[i]*width/6,DB_SHIFT-8);
1241
958k
            boost_bits = boost*6<<BITRES;
1242
958k
         }
1243
         /* For CBR and non-transient CVBR frames, limit dynalloc to 2/3 of the bits */
1244
1.24M
         if ((!vbr || (constrained_vbr&&!isTransient))
1245
1.24M
               && (tot_boost+boost_bits)>>BITRES>>3 > 2*effectiveBytes/3)
1246
288
         {
1247
288
            opus_int32 cap = ((2*effectiveBytes/3)<<BITRES<<3);
1248
288
            offsets[i] = cap-tot_boost;
1249
288
            tot_boost = cap;
1250
288
            break;
1251
1.24M
         } else {
1252
1.24M
            offsets[i] = boost;
1253
1.24M
            tot_boost += boost_bits;
1254
1.24M
         }
1255
1.24M
      }
1256
109k
   } else {
1257
1.57M
      for (i=start;i<end;i++)
1258
1.46M
         importance[i] = 13;
1259
109k
   }
1260
195k
   *tot_boost_ = tot_boost;
1261
195k
   RESTORE_STACK;
1262
195k
   return maxDepth;
1263
195k
}
celt_encoder.c:dynalloc_analysis
Line
Count
Source
1045
88.7k
{
1046
88.7k
   int i, c;
1047
88.7k
   opus_int32 tot_boost=0;
1048
88.7k
   celt_glog maxDepth;
1049
88.7k
   VARDECL(celt_glog, follower);
1050
88.7k
   VARDECL(celt_glog, noise_floor);
1051
88.7k
   VARDECL(celt_glog, bandLogE3);
1052
88.7k
   SAVE_STACK;
1053
88.7k
   ALLOC(follower, C*nbEBands, celt_glog);
1054
88.7k
   ALLOC(noise_floor, C*nbEBands, celt_glog);
1055
88.7k
   ALLOC(bandLogE3, nbEBands, celt_glog);
1056
88.7k
   OPUS_CLEAR(offsets, nbEBands);
1057
   /* Dynamic allocation code */
1058
88.7k
   maxDepth=-GCONST(31.9f);
1059
1.40M
   for (i=0;i<end;i++)
1060
1.31M
   {
1061
      /* Noise floor must take into account eMeans, the depth, the width of the bands
1062
         and the preemphasis filter (approx. square of bark band ID) */
1063
1.31M
      noise_floor[i] = GCONST(0.0625f)*logN[i]
1064
1.31M
            +GCONST(.5f)+SHL32(9-lsb_depth,DB_SHIFT)-SHL32(eMeans[i],DB_SHIFT-4)
1065
1.31M
            +GCONST(.0062f)*(i+5)*(i+5);
1066
1.31M
   }
1067
88.7k
   c=0;do
1068
120k
   {
1069
1.90M
      for (i=0;i<end;i++)
1070
1.78M
         maxDepth = MAXG(maxDepth, bandLogE[c*nbEBands+i]-noise_floor[i]);
1071
120k
   } while (++c<C);
1072
88.7k
   {
1073
      /* Compute a really simple masking model to avoid taking into account completely masked
1074
         bands when computing the spreading decision. */
1075
88.7k
      VARDECL(celt_glog, mask);
1076
88.7k
      VARDECL(celt_glog, sig);
1077
88.7k
      ALLOC(mask, nbEBands, celt_glog);
1078
88.7k
      ALLOC(sig, nbEBands, celt_glog);
1079
1.40M
      for (i=0;i<end;i++)
1080
1.31M
         mask[i] = bandLogE[i]-noise_floor[i];
1081
88.7k
      if (C==2)
1082
31.7k
      {
1083
502k
         for (i=0;i<end;i++)
1084
470k
            mask[i] = MAXG(mask[i], bandLogE[nbEBands+i]-noise_floor[i]);
1085
31.7k
      }
1086
88.7k
      OPUS_COPY(sig, mask, end);
1087
1.31M
      for (i=1;i<end;i++)
1088
1.22M
         mask[i] = MAXG(mask[i], mask[i-1] - GCONST(2.f));
1089
1.31M
      for (i=end-2;i>=0;i--)
1090
1.22M
         mask[i] = MAXG(mask[i], mask[i+1] - GCONST(3.f));
1091
1.40M
      for (i=0;i<end;i++)
1092
1.31M
      {
1093
         /* Compute SMR: Mask is never more than 72 dB below the peak and never below the noise floor.*/
1094
1.31M
         celt_glog smr = sig[i]-MAXG(MAXG(0, maxDepth-GCONST(12.f)), mask[i]);
1095
         /* Clamp SMR to make sure we're not shifting by something negative or too large. */
1096
1.31M
#ifdef FIXED_POINT
1097
         /* FIXME: Use PSHR16() instead */
1098
1.31M
         int shift = -PSHR32(MAXG(-GCONST(5.f), MING(0, smr)), DB_SHIFT);
1099
#else
1100
         int shift = IMIN(5, IMAX(0, -(int)floor(.5f + smr)));
1101
#endif
1102
1.31M
         spread_weight[i] = 32 >> shift;
1103
1.31M
      }
1104
      /*for (i=0;i<end;i++)
1105
         printf("%d ", spread_weight[i]);
1106
      printf("\n");*/
1107
88.7k
   }
1108
   /* Make sure that dynamic allocation can't make us bust the budget.
1109
      We enable the feature starting at 24 kb/s for 20-ms frames
1110
      and 96 kb/s for 2.5 ms frames.  */
1111
88.7k
   if (effectiveBytes >= (30 + 5*LM) && !lfe)
1112
39.7k
   {
1113
39.7k
      int last=0;
1114
39.7k
      c=0;do
1115
60.3k
      {
1116
60.3k
         celt_glog offset;
1117
60.3k
         celt_glog tmp;
1118
60.3k
         celt_glog *f;
1119
60.3k
         OPUS_COPY(bandLogE3, &bandLogE2[c*nbEBands], end);
1120
60.3k
         if (LM==0) {
1121
            /* For 2.5 ms frames, the first 8 bands have just one bin, so the
1122
               energy is highly unreliable (high variance). For that reason,
1123
               we take the max with the previous energy so that at least 2 bins
1124
               are getting used. */
1125
75.1k
            for (i=0;i<IMIN(8,end);i++) bandLogE3[i] = MAXG(bandLogE2[c*nbEBands+i], oldBandE[c*nbEBands+i]);
1126
8.35k
         }
1127
60.3k
         f = &follower[c*nbEBands];
1128
60.3k
         f[0] = bandLogE3[0];
1129
921k
         for (i=1;i<end;i++)
1130
861k
         {
1131
            /* The last band to be at least 3 dB higher than the previous one
1132
               is the last we'll consider. Otherwise, we run into problems on
1133
               bandlimited signals. */
1134
861k
            if (bandLogE3[i] > bandLogE3[i-1]+GCONST(.5f))
1135
262k
               last=i;
1136
861k
            f[i] = MING(f[i-1]+GCONST(1.5f), bandLogE3[i]);
1137
861k
         }
1138
733k
         for (i=last-1;i>=0;i--)
1139
672k
            f[i] = MING(f[i], MING(f[i+1]+GCONST(2.f), bandLogE3[i]));
1140
1141
         /* Combine with a median filter to avoid dynalloc triggering unnecessarily.
1142
            The "offset" value controls how conservative we are -- a higher offset
1143
            reduces the impact of the median filter and makes dynalloc use more bits. */
1144
60.3k
         offset = GCONST(1.f);
1145
740k
         for (i=2;i<end-2;i++)
1146
680k
            f[i] = MAXG(f[i], median_of_5(&bandLogE3[i-2])-offset);
1147
60.3k
         tmp = median_of_3(&bandLogE3[0])-offset;
1148
60.3k
         f[0] = MAXG(f[0], tmp);
1149
60.3k
         f[1] = MAXG(f[1], tmp);
1150
60.3k
         tmp = median_of_3(&bandLogE3[end-3])-offset;
1151
60.3k
         f[end-2] = MAXG(f[end-2], tmp);
1152
60.3k
         f[end-1] = MAXG(f[end-1], tmp);
1153
1154
981k
         for (i=0;i<end;i++)
1155
921k
            f[i] = MAXG(f[i], noise_floor[i]);
1156
60.3k
      } while (++c<C);
1157
39.7k
      if (C==2)
1158
20.6k
      {
1159
318k
         for (i=start;i<end;i++)
1160
297k
         {
1161
            /* Consider 24 dB "cross-talk" */
1162
297k
            follower[nbEBands+i] = MAXG(follower[nbEBands+i], follower[         i]-GCONST(4.f));
1163
297k
            follower[         i] = MAXG(follower[         i], follower[nbEBands+i]-GCONST(4.f));
1164
297k
            follower[i] = HALF32(MAXG(0, bandLogE[i]-follower[i]) + MAXG(0, bandLogE[nbEBands+i]-follower[nbEBands+i]));
1165
297k
         }
1166
20.6k
      } else {
1167
304k
         for (i=start;i<end;i++)
1168
285k
         {
1169
285k
            follower[i] = MAXG(0, bandLogE[i]-follower[i]);
1170
285k
         }
1171
19.1k
      }
1172
622k
      for (i=start;i<end;i++)
1173
582k
         follower[i] = MAXG(follower[i], surround_dynalloc[i]);
1174
622k
      for (i=start;i<end;i++)
1175
582k
      {
1176
582k
#ifdef FIXED_POINT
1177
582k
         importance[i] = PSHR32(13*celt_exp2_db(MING(follower[i], GCONST(4.f))), 16);
1178
#else
1179
         importance[i] = (int)floor(.5f+13*celt_exp2_db(MING(follower[i], GCONST(4.f))));
1180
#endif
1181
582k
      }
1182
      /* For non-transient CBR/CVBR frames, halve the dynalloc contribution */
1183
39.7k
      if ((!vbr || constrained_vbr)&&!isTransient)
1184
8.42k
      {
1185
141k
         for (i=start;i<end;i++)
1186
132k
            follower[i] = HALF32(follower[i]);
1187
8.42k
      }
1188
622k
      for (i=start;i<end;i++)
1189
582k
      {
1190
582k
         if (i<8)
1191
305k
            follower[i] *= 2;
1192
582k
         if (i>=12)
1193
124k
            follower[i] = HALF32(follower[i]);
1194
582k
      }
1195
      /* Compensate for Opus' under-allocation on tones. */
1196
39.7k
      if (toneishness > QCONST32(.98f, 29)) {
1197
362
#ifdef FIXED_POINT
1198
362
         int freq_bin = PSHR32(QEXT_SCALE((opus_val32)tone_freq)*QCONST16(120/M_PI, 9), 13+9);
1199
#else
1200
         int freq_bin = (int)floor(.5 + QEXT_SCALE(tone_freq)*120/M_PI);
1201
#endif
1202
6.27k
         for (i=start;i<end;i++) {
1203
5.91k
            if (freq_bin >= eBands[i] && freq_bin <= eBands[i+1]) follower[i] += GCONST(2.f);
1204
5.91k
            if (freq_bin >= eBands[i]-1 && freq_bin <= eBands[i+1]+1) follower[i] += GCONST(1.f);
1205
5.91k
            if (freq_bin >= eBands[i]-2 && freq_bin <= eBands[i+1]+2) follower[i] += GCONST(1.f);
1206
5.91k
            if (freq_bin >= eBands[i]-3 && freq_bin <= eBands[i+1]+3) follower[i] += GCONST(.5f);
1207
5.91k
         }
1208
362
         if (freq_bin >= eBands[end]) {
1209
236
            follower[end-1] += GCONST(2.f);
1210
236
            follower[end-2] += GCONST(1.f);
1211
236
         }
1212
362
      }
1213
#ifdef DISABLE_FLOAT_API
1214
      (void)analysis;
1215
#else
1216
39.7k
      if (analysis->valid)
1217
9.53k
      {
1218
152k
         for (i=start;i<IMIN(LEAK_BANDS, end);i++)
1219
143k
            follower[i] = follower[i] +  GCONST(1.f/64.f)*analysis->leak_boost[i];
1220
9.53k
      }
1221
39.7k
#endif
1222
622k
      for (i=start;i<end;i++)
1223
582k
      {
1224
582k
         int width;
1225
582k
         int boost;
1226
582k
         int boost_bits;
1227
1228
582k
         follower[i] = MING(follower[i], GCONST(4));
1229
1230
582k
         follower[i] = SHR32(follower[i], 8);
1231
582k
         width = C*(eBands[i+1]-eBands[i])<<LM;
1232
582k
         if (width<6)
1233
104k
         {
1234
104k
            boost = (int)SHR32(follower[i],DB_SHIFT-8);
1235
104k
            boost_bits = boost*width<<BITRES;
1236
477k
         } else if (width > 48) {
1237
33.4k
            boost = (int)SHR32(follower[i]*8,DB_SHIFT-8);
1238
33.4k
            boost_bits = (boost*width<<BITRES)/8;
1239
444k
         } else {
1240
444k
            boost = (int)SHR32(follower[i]*width/6,DB_SHIFT-8);
1241
444k
            boost_bits = boost*6<<BITRES;
1242
444k
         }
1243
         /* For CBR and non-transient CVBR frames, limit dynalloc to 2/3 of the bits */
1244
582k
         if ((!vbr || (constrained_vbr&&!isTransient))
1245
582k
               && (tot_boost+boost_bits)>>BITRES>>3 > 2*effectiveBytes/3)
1246
107
         {
1247
107
            opus_int32 cap = ((2*effectiveBytes/3)<<BITRES<<3);
1248
107
            offsets[i] = cap-tot_boost;
1249
107
            tot_boost = cap;
1250
107
            break;
1251
582k
         } else {
1252
582k
            offsets[i] = boost;
1253
582k
            tot_boost += boost_bits;
1254
582k
         }
1255
582k
      }
1256
49.0k
   } else {
1257
695k
      for (i=start;i<end;i++)
1258
646k
         importance[i] = 13;
1259
49.0k
   }
1260
88.7k
   *tot_boost_ = tot_boost;
1261
88.7k
   RESTORE_STACK;
1262
88.7k
   return maxDepth;
1263
88.7k
}
celt_encoder.c:dynalloc_analysis
Line
Count
Source
1045
107k
{
1046
107k
   int i, c;
1047
107k
   opus_int32 tot_boost=0;
1048
107k
   celt_glog maxDepth;
1049
107k
   VARDECL(celt_glog, follower);
1050
107k
   VARDECL(celt_glog, noise_floor);
1051
107k
   VARDECL(celt_glog, bandLogE3);
1052
107k
   SAVE_STACK;
1053
107k
   ALLOC(follower, C*nbEBands, celt_glog);
1054
107k
   ALLOC(noise_floor, C*nbEBands, celt_glog);
1055
107k
   ALLOC(bandLogE3, nbEBands, celt_glog);
1056
107k
   OPUS_CLEAR(offsets, nbEBands);
1057
   /* Dynamic allocation code */
1058
107k
   maxDepth=-GCONST(31.9f);
1059
1.70M
   for (i=0;i<end;i++)
1060
1.59M
   {
1061
      /* Noise floor must take into account eMeans, the depth, the width of the bands
1062
         and the preemphasis filter (approx. square of bark band ID) */
1063
1.59M
      noise_floor[i] = GCONST(0.0625f)*logN[i]
1064
1.59M
            +GCONST(.5f)+SHL32(9-lsb_depth,DB_SHIFT)-SHL32(eMeans[i],DB_SHIFT-4)
1065
1.59M
            +GCONST(.0062f)*(i+5)*(i+5);
1066
1.59M
   }
1067
107k
   c=0;do
1068
150k
   {
1069
2.39M
      for (i=0;i<end;i++)
1070
2.24M
         maxDepth = MAXG(maxDepth, bandLogE[c*nbEBands+i]-noise_floor[i]);
1071
150k
   } while (++c<C);
1072
107k
   {
1073
      /* Compute a really simple masking model to avoid taking into account completely masked
1074
         bands when computing the spreading decision. */
1075
107k
      VARDECL(celt_glog, mask);
1076
107k
      VARDECL(celt_glog, sig);
1077
107k
      ALLOC(mask, nbEBands, celt_glog);
1078
107k
      ALLOC(sig, nbEBands, celt_glog);
1079
1.70M
      for (i=0;i<end;i++)
1080
1.59M
         mask[i] = bandLogE[i]-noise_floor[i];
1081
107k
      if (C==2)
1082
43.5k
      {
1083
698k
         for (i=0;i<end;i++)
1084
655k
            mask[i] = MAXG(mask[i], bandLogE[nbEBands+i]-noise_floor[i]);
1085
43.5k
      }
1086
107k
      OPUS_COPY(sig, mask, end);
1087
1.59M
      for (i=1;i<end;i++)
1088
1.48M
         mask[i] = MAXG(mask[i], mask[i-1] - GCONST(2.f));
1089
1.59M
      for (i=end-2;i>=0;i--)
1090
1.48M
         mask[i] = MAXG(mask[i], mask[i+1] - GCONST(3.f));
1091
1.70M
      for (i=0;i<end;i++)
1092
1.59M
      {
1093
         /* Compute SMR: Mask is never more than 72 dB below the peak and never below the noise floor.*/
1094
1.59M
         celt_glog smr = sig[i]-MAXG(MAXG(0, maxDepth-GCONST(12.f)), mask[i]);
1095
         /* Clamp SMR to make sure we're not shifting by something negative or too large. */
1096
#ifdef FIXED_POINT
1097
         /* FIXME: Use PSHR16() instead */
1098
         int shift = -PSHR32(MAXG(-GCONST(5.f), MING(0, smr)), DB_SHIFT);
1099
#else
1100
1.59M
         int shift = IMIN(5, IMAX(0, -(int)floor(.5f + smr)));
1101
1.59M
#endif
1102
1.59M
         spread_weight[i] = 32 >> shift;
1103
1.59M
      }
1104
      /*for (i=0;i<end;i++)
1105
         printf("%d ", spread_weight[i]);
1106
      printf("\n");*/
1107
107k
   }
1108
   /* Make sure that dynamic allocation can't make us bust the budget.
1109
      We enable the feature starting at 24 kb/s for 20-ms frames
1110
      and 96 kb/s for 2.5 ms frames.  */
1111
107k
   if (effectiveBytes >= (30 + 5*LM) && !lfe)
1112
46.4k
   {
1113
46.4k
      int last=0;
1114
46.4k
      c=0;do
1115
72.7k
      {
1116
72.7k
         celt_glog offset;
1117
72.7k
         celt_glog tmp;
1118
72.7k
         celt_glog *f;
1119
72.7k
         OPUS_COPY(bandLogE3, &bandLogE2[c*nbEBands], end);
1120
72.7k
         if (LM==0) {
1121
            /* For 2.5 ms frames, the first 8 bands have just one bin, so the
1122
               energy is highly unreliable (high variance). For that reason,
1123
               we take the max with the previous energy so that at least 2 bins
1124
               are getting used. */
1125
68.7k
            for (i=0;i<IMIN(8,end);i++) bandLogE3[i] = MAXG(bandLogE2[c*nbEBands+i], oldBandE[c*nbEBands+i]);
1126
7.63k
         }
1127
72.7k
         f = &follower[c*nbEBands];
1128
72.7k
         f[0] = bandLogE3[0];
1129
1.11M
         for (i=1;i<end;i++)
1130
1.04M
         {
1131
            /* The last band to be at least 3 dB higher than the previous one
1132
               is the last we'll consider. Otherwise, we run into problems on
1133
               bandlimited signals. */
1134
1.04M
            if (bandLogE3[i] > bandLogE3[i-1]+GCONST(.5f))
1135
345k
               last=i;
1136
1.04M
            f[i] = MING(f[i-1]+GCONST(1.5f), bandLogE3[i]);
1137
1.04M
         }
1138
941k
         for (i=last-1;i>=0;i--)
1139
868k
            f[i] = MING(f[i], MING(f[i+1]+GCONST(2.f), bandLogE3[i]));
1140
1141
         /* Combine with a median filter to avoid dynalloc triggering unnecessarily.
1142
            The "offset" value controls how conservative we are -- a higher offset
1143
            reduces the impact of the median filter and makes dynalloc use more bits. */
1144
72.7k
         offset = GCONST(1.f);
1145
896k
         for (i=2;i<end-2;i++)
1146
823k
            f[i] = MAXG(f[i], median_of_5(&bandLogE3[i-2])-offset);
1147
72.7k
         tmp = median_of_3(&bandLogE3[0])-offset;
1148
72.7k
         f[0] = MAXG(f[0], tmp);
1149
72.7k
         f[1] = MAXG(f[1], tmp);
1150
72.7k
         tmp = median_of_3(&bandLogE3[end-3])-offset;
1151
72.7k
         f[end-2] = MAXG(f[end-2], tmp);
1152
72.7k
         f[end-1] = MAXG(f[end-1], tmp);
1153
1154
1.18M
         for (i=0;i<end;i++)
1155
1.11M
            f[i] = MAXG(f[i], noise_floor[i]);
1156
72.7k
      } while (++c<C);
1157
46.4k
      if (C==2)
1158
26.3k
      {
1159
403k
         for (i=start;i<end;i++)
1160
376k
         {
1161
            /* Consider 24 dB "cross-talk" */
1162
376k
            follower[nbEBands+i] = MAXG(follower[nbEBands+i], follower[         i]-GCONST(4.f));
1163
376k
            follower[         i] = MAXG(follower[         i], follower[nbEBands+i]-GCONST(4.f));
1164
376k
            follower[i] = HALF32(MAXG(0, bandLogE[i]-follower[i]) + MAXG(0, bandLogE[nbEBands+i]-follower[nbEBands+i]));
1165
376k
         }
1166
26.3k
      } else {
1167
309k
         for (i=start;i<end;i++)
1168
289k
         {
1169
289k
            follower[i] = MAXG(0, bandLogE[i]-follower[i]);
1170
289k
         }
1171
20.1k
      }
1172
712k
      for (i=start;i<end;i++)
1173
666k
         follower[i] = MAXG(follower[i], surround_dynalloc[i]);
1174
712k
      for (i=start;i<end;i++)
1175
666k
      {
1176
#ifdef FIXED_POINT
1177
         importance[i] = PSHR32(13*celt_exp2_db(MING(follower[i], GCONST(4.f))), 16);
1178
#else
1179
666k
         importance[i] = (int)floor(.5f+13*celt_exp2_db(MING(follower[i], GCONST(4.f))));
1180
666k
#endif
1181
666k
      }
1182
      /* For non-transient CBR/CVBR frames, halve the dynalloc contribution */
1183
46.4k
      if ((!vbr || constrained_vbr)&&!isTransient)
1184
8.33k
      {
1185
135k
         for (i=start;i<end;i++)
1186
126k
            follower[i] = HALF32(follower[i]);
1187
8.33k
      }
1188
712k
      for (i=start;i<end;i++)
1189
666k
      {
1190
666k
         if (i<8)
1191
350k
            follower[i] *= 2;
1192
666k
         if (i>=12)
1193
139k
            follower[i] = HALF32(follower[i]);
1194
666k
      }
1195
      /* Compensate for Opus' under-allocation on tones. */
1196
46.4k
      if (toneishness > QCONST32(.98f, 29)) {
1197
#ifdef FIXED_POINT
1198
         int freq_bin = PSHR32(QEXT_SCALE((opus_val32)tone_freq)*QCONST16(120/M_PI, 9), 13+9);
1199
#else
1200
486
         int freq_bin = (int)floor(.5 + QEXT_SCALE(tone_freq)*120/M_PI);
1201
486
#endif
1202
8.77k
         for (i=start;i<end;i++) {
1203
8.28k
            if (freq_bin >= eBands[i] && freq_bin <= eBands[i+1]) follower[i] += GCONST(2.f);
1204
8.28k
            if (freq_bin >= eBands[i]-1 && freq_bin <= eBands[i+1]+1) follower[i] += GCONST(1.f);
1205
8.28k
            if (freq_bin >= eBands[i]-2 && freq_bin <= eBands[i+1]+2) follower[i] += GCONST(1.f);
1206
8.28k
            if (freq_bin >= eBands[i]-3 && freq_bin <= eBands[i+1]+3) follower[i] += GCONST(.5f);
1207
8.28k
         }
1208
486
         if (freq_bin >= eBands[end]) {
1209
188
            follower[end-1] += GCONST(2.f);
1210
188
            follower[end-2] += GCONST(1.f);
1211
188
         }
1212
486
      }
1213
#ifdef DISABLE_FLOAT_API
1214
      (void)analysis;
1215
#else
1216
46.4k
      if (analysis->valid)
1217
12.1k
      {
1218
189k
         for (i=start;i<IMIN(LEAK_BANDS, end);i++)
1219
176k
            follower[i] = follower[i] +  GCONST(1.f/64.f)*analysis->leak_boost[i];
1220
12.1k
      }
1221
46.4k
#endif
1222
712k
      for (i=start;i<end;i++)
1223
665k
      {
1224
665k
         int width;
1225
665k
         int boost;
1226
665k
         int boost_bits;
1227
1228
665k
         follower[i] = MING(follower[i], GCONST(4));
1229
1230
665k
         follower[i] = SHR32(follower[i], 8);
1231
665k
         width = C*(eBands[i+1]-eBands[i])<<LM;
1232
665k
         if (width<6)
1233
102k
         {
1234
102k
            boost = (int)SHR32(follower[i],DB_SHIFT-8);
1235
102k
            boost_bits = boost*width<<BITRES;
1236
563k
         } else if (width > 48) {
1237
49.0k
            boost = (int)SHR32(follower[i]*8,DB_SHIFT-8);
1238
49.0k
            boost_bits = (boost*width<<BITRES)/8;
1239
514k
         } else {
1240
514k
            boost = (int)SHR32(follower[i]*width/6,DB_SHIFT-8);
1241
514k
            boost_bits = boost*6<<BITRES;
1242
514k
         }
1243
         /* For CBR and non-transient CVBR frames, limit dynalloc to 2/3 of the bits */
1244
665k
         if ((!vbr || (constrained_vbr&&!isTransient))
1245
665k
               && (tot_boost+boost_bits)>>BITRES>>3 > 2*effectiveBytes/3)
1246
181
         {
1247
181
            opus_int32 cap = ((2*effectiveBytes/3)<<BITRES<<3);
1248
181
            offsets[i] = cap-tot_boost;
1249
181
            tot_boost = cap;
1250
181
            break;
1251
665k
         } else {
1252
665k
            offsets[i] = boost;
1253
665k
            tot_boost += boost_bits;
1254
665k
         }
1255
665k
      }
1256
60.7k
   } else {
1257
877k
      for (i=start;i<end;i++)
1258
816k
         importance[i] = 13;
1259
60.7k
   }
1260
107k
   *tot_boost_ = tot_boost;
1261
107k
   RESTORE_STACK;
1262
107k
   return maxDepth;
1263
107k
}
1264
1265
#ifdef FIXED_POINT
1266
88.7k
void normalize_tone_input(opus_val16 *x, int len) {
1267
88.7k
   opus_val32 ac0=len;
1268
88.7k
   int i;
1269
88.7k
   int shift;
1270
54.5M
   for (i=0;i<len;i++) {
1271
54.4M
      ac0 = ADD32(ac0, SHR32(MULT16_16(x[i], x[i]), 10));
1272
54.4M
   }
1273
88.7k
   shift = 5 - (28-celt_ilog2(ac0))/2;
1274
88.7k
   if (shift > 0) {
1275
18.9M
      for (i=0;i<len;i++) {
1276
18.9M
         x[i] = PSHR32(x[i], shift);
1277
18.9M
      }
1278
24.9k
   }
1279
88.7k
}
1280
57.9k
int acos_approx(opus_val32 x) {
1281
57.9k
   opus_val16 x14;
1282
57.9k
   opus_val32 tmp;
1283
57.9k
   int flip = x<0;
1284
57.9k
   x = abs(x);
1285
57.9k
   x14 = x>>15;
1286
57.9k
   tmp = (762*x14>>14)-3308;
1287
57.9k
   tmp = (tmp*x14>>14)+25726;
1288
57.9k
   tmp = tmp*celt_sqrt(IMAX(0, (1<<30) - (x<<1)))>>16;
1289
57.9k
   if (flip) tmp = 25736 - tmp;
1290
57.9k
   return tmp;
1291
57.9k
}
1292
#endif
1293
1294
/* Compute the LPC coefficients using a least-squares fit for both forward and backward prediction. */
1295
330k
static int tone_lpc(const opus_val16 *x, int len, int delay, opus_val32 *lpc) {
1296
330k
   int i;
1297
330k
   opus_val32 r00=0, r01=0, r11=0, r02=0, r12=0, r22=0;
1298
330k
   opus_val32 edges;
1299
330k
   opus_val32 num0, num1, den;
1300
330k
   celt_assert(len > 2*delay);
1301
   /* Compute correlations as if using the forward prediction covariance method. */
1302
207M
   for (i=0;i<len-2*delay;i++) {
1303
207M
      r00 += MULT16_16(x[i],x[i]);
1304
207M
      r01 += MULT16_16(x[i],x[i+delay]);
1305
207M
      r02 += MULT16_16(x[i],x[i+2*delay]);
1306
207M
   }
1307
330k
   edges = 0;
1308
2.23M
   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]);
1309
330k
   r11 = r00+edges;
1310
330k
   edges = 0;
1311
2.23M
   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]);
1312
330k
   r22 = r11+edges;
1313
330k
   edges = 0;
1314
2.23M
   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]);
1315
330k
   r12 = r01+edges;
1316
   /* Reverse and sum to get the backward contribution. */
1317
330k
   {
1318
330k
      opus_val32 R00, R01, R11, R02, R12, R22;
1319
330k
      R00 = r00 + r22;
1320
330k
      R01 = r01 + r12;
1321
330k
      R11 = 2*r11;
1322
330k
      R02 = 2*r02;
1323
330k
      R12 = r12 + r01;
1324
330k
      R22 = r00 + r22;
1325
330k
      r00 = R00;
1326
330k
      r01 = R01;
1327
330k
      r11 = R11;
1328
330k
      r02 = R02;
1329
330k
      r12 = R12;
1330
330k
      r22 = R22;
1331
330k
   }
1332
   /* Solve A*x=b, where A=[r00, r01; r01, r11] and b=[r02; r12]. */
1333
330k
   den = MULT32_32_Q31(r00,r11) - MULT32_32_Q31(r01,r01);
1334
#ifdef FIXED_POINT
1335
217k
   if (den <= SHR32(MULT32_32_Q31(r00,r11), 10)) return 1;
1336
#else
1337
113k
   if (den < .001f*MULT32_32_Q31(r00,r11)) return 1;
1338
108k
#endif
1339
172k
   num1 = MULT32_32_Q31(r02,r11) - MULT32_32_Q31(r01,r12);
1340
172k
   if (num1 >= den) lpc[1] = QCONST32(1.f, 29);
1341
159k
   else if (num1 <= -den) lpc[1] = -QCONST32(1.f, 29);
1342
158k
   else lpc[1] = frac_div32_q29(num1, den);
1343
172k
   num0 = MULT32_32_Q31(r00,r12) - MULT32_32_Q31(r02,r01);
1344
172k
   if (HALF32(num0) >= den) lpc[0] = QCONST32(1.999999f, 29);
1345
157k
   else if (HALF32(num0) <= -den) lpc[0] = -QCONST32(1.999999f, 29);
1346
156k
   else lpc[0] = frac_div32_q29(num0, den);
1347
   /*printf("%f %f\n", lpc[0], lpc[1]);*/
1348
172k
   return 0;
1349
330k
}
celt_encoder.c:tone_lpc
Line
Count
Source
1295
217k
static int tone_lpc(const opus_val16 *x, int len, int delay, opus_val32 *lpc) {
1296
217k
   int i;
1297
217k
   opus_val32 r00=0, r01=0, r11=0, r02=0, r12=0, r22=0;
1298
217k
   opus_val32 edges;
1299
217k
   opus_val32 num0, num1, den;
1300
217k
   celt_assert(len > 2*delay);
1301
   /* Compute correlations as if using the forward prediction covariance method. */
1302
129M
   for (i=0;i<len-2*delay;i++) {
1303
129M
      r00 += MULT16_16(x[i],x[i]);
1304
129M
      r01 += MULT16_16(x[i],x[i+delay]);
1305
129M
      r02 += MULT16_16(x[i],x[i+2*delay]);
1306
129M
   }
1307
217k
   edges = 0;
1308
1.94M
   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]);
1309
217k
   r11 = r00+edges;
1310
217k
   edges = 0;
1311
1.94M
   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]);
1312
217k
   r22 = r11+edges;
1313
217k
   edges = 0;
1314
1.94M
   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]);
1315
217k
   r12 = r01+edges;
1316
   /* Reverse and sum to get the backward contribution. */
1317
217k
   {
1318
217k
      opus_val32 R00, R01, R11, R02, R12, R22;
1319
217k
      R00 = r00 + r22;
1320
217k
      R01 = r01 + r12;
1321
217k
      R11 = 2*r11;
1322
217k
      R02 = 2*r02;
1323
217k
      R12 = r12 + r01;
1324
217k
      R22 = r00 + r22;
1325
217k
      r00 = R00;
1326
217k
      r01 = R01;
1327
217k
      r11 = R11;
1328
217k
      r02 = R02;
1329
217k
      r12 = R12;
1330
217k
      r22 = R22;
1331
217k
   }
1332
   /* Solve A*x=b, where A=[r00, r01; r01, r11] and b=[r02; r12]. */
1333
217k
   den = MULT32_32_Q31(r00,r11) - MULT32_32_Q31(r01,r01);
1334
217k
#ifdef FIXED_POINT
1335
217k
   if (den <= SHR32(MULT32_32_Q31(r00,r11), 10)) return 1;
1336
#else
1337
   if (den < .001f*MULT32_32_Q31(r00,r11)) return 1;
1338
#endif
1339
63.5k
   num1 = MULT32_32_Q31(r02,r11) - MULT32_32_Q31(r01,r12);
1340
63.5k
   if (num1 >= den) lpc[1] = QCONST32(1.f, 29);
1341
63.3k
   else if (num1 <= -den) lpc[1] = -QCONST32(1.f, 29);
1342
62.9k
   else lpc[1] = frac_div32_q29(num1, den);
1343
63.5k
   num0 = MULT32_32_Q31(r00,r12) - MULT32_32_Q31(r02,r01);
1344
63.5k
   if (HALF32(num0) >= den) lpc[0] = QCONST32(1.999999f, 29);
1345
63.2k
   else if (HALF32(num0) <= -den) lpc[0] = -QCONST32(1.999999f, 29);
1346
62.0k
   else lpc[0] = frac_div32_q29(num0, den);
1347
   /*printf("%f %f\n", lpc[0], lpc[1]);*/
1348
63.5k
   return 0;
1349
217k
}
celt_encoder.c:tone_lpc
Line
Count
Source
1295
113k
static int tone_lpc(const opus_val16 *x, int len, int delay, opus_val32 *lpc) {
1296
113k
   int i;
1297
113k
   opus_val32 r00=0, r01=0, r11=0, r02=0, r12=0, r22=0;
1298
113k
   opus_val32 edges;
1299
113k
   opus_val32 num0, num1, den;
1300
113k
   celt_assert(len > 2*delay);
1301
   /* Compute correlations as if using the forward prediction covariance method. */
1302
77.5M
   for (i=0;i<len-2*delay;i++) {
1303
77.4M
      r00 += MULT16_16(x[i],x[i]);
1304
77.4M
      r01 += MULT16_16(x[i],x[i+delay]);
1305
77.4M
      r02 += MULT16_16(x[i],x[i+2*delay]);
1306
77.4M
   }
1307
113k
   edges = 0;
1308
290k
   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]);
1309
113k
   r11 = r00+edges;
1310
113k
   edges = 0;
1311
290k
   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]);
1312
113k
   r22 = r11+edges;
1313
113k
   edges = 0;
1314
290k
   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]);
1315
113k
   r12 = r01+edges;
1316
   /* Reverse and sum to get the backward contribution. */
1317
113k
   {
1318
113k
      opus_val32 R00, R01, R11, R02, R12, R22;
1319
113k
      R00 = r00 + r22;
1320
113k
      R01 = r01 + r12;
1321
113k
      R11 = 2*r11;
1322
113k
      R02 = 2*r02;
1323
113k
      R12 = r12 + r01;
1324
113k
      R22 = r00 + r22;
1325
113k
      r00 = R00;
1326
113k
      r01 = R01;
1327
113k
      r11 = R11;
1328
113k
      r02 = R02;
1329
113k
      r12 = R12;
1330
113k
      r22 = R22;
1331
113k
   }
1332
   /* Solve A*x=b, where A=[r00, r01; r01, r11] and b=[r02; r12]. */
1333
113k
   den = MULT32_32_Q31(r00,r11) - MULT32_32_Q31(r01,r01);
1334
#ifdef FIXED_POINT
1335
   if (den <= SHR32(MULT32_32_Q31(r00,r11), 10)) return 1;
1336
#else
1337
113k
   if (den < .001f*MULT32_32_Q31(r00,r11)) return 1;
1338
108k
#endif
1339
108k
   num1 = MULT32_32_Q31(r02,r11) - MULT32_32_Q31(r01,r12);
1340
108k
   if (num1 >= den) lpc[1] = QCONST32(1.f, 29);
1341
96.0k
   else if (num1 <= -den) lpc[1] = -QCONST32(1.f, 29);
1342
95.7k
   else lpc[1] = frac_div32_q29(num1, den);
1343
108k
   num0 = MULT32_32_Q31(r00,r12) - MULT32_32_Q31(r02,r01);
1344
108k
   if (HALF32(num0) >= den) lpc[0] = QCONST32(1.999999f, 29);
1345
94.7k
   else if (HALF32(num0) <= -den) lpc[0] = -QCONST32(1.999999f, 29);
1346
94.1k
   else lpc[0] = frac_div32_q29(num0, den);
1347
   /*printf("%f %f\n", lpc[0], lpc[1]);*/
1348
108k
   return 0;
1349
113k
}
1350
1351
/* Detects pure of nearly pure tones so we can prevent them from causing problems with the encoder. */
1352
391k
static opus_val16 tone_detect(const celt_sig *in, int CC, int N, opus_val32 *toneishness, opus_int32 Fs) {
1353
391k
   int i;
1354
391k
   int delay = 1;
1355
391k
   int fail;
1356
391k
   opus_val32 lpc[2];
1357
391k
   opus_val16 freq;
1358
391k
   VARDECL(opus_val16, x);
1359
391k
   ALLOC(x, N, opus_val16);
1360
   /* Shift by SIG_SHIFT+2 (+3 for stereo) to account for HF gain of the preemphasis filter. */
1361
391k
   if (CC==2) {
1362
150M
      for (i=0;i<N;i++) x[i] = PSHR32(ADD32(in[i], in[i+N]), SIG_SHIFT+3);
1363
199k
   } else {
1364
105M
      for (i=0;i<N;i++) x[i] = PSHR32(in[i], SIG_SHIFT+2);
1365
192k
   }
1366
#ifdef FIXED_POINT
1367
   normalize_tone_input(x, N);
1368
#endif
1369
391k
   fail = tone_lpc(x, N, delay, lpc);
1370
   /* If our LPC filter resonates too close to DC, retry the analysis with down-sampling. */
1371
661k
   while (delay <= Fs/3000 && (fail || (lpc[0] > QCONST32(1.f, 29) && lpc[1] < 0))) {
1372
269k
      delay *= 2;
1373
269k
      fail = tone_lpc(x, N, delay, lpc);
1374
269k
   }
1375
   /* Check that our filter has complex roots. */
1376
391k
   if (!fail && MULT32_32_Q31(lpc[0],lpc[0]) + MULT32_32_Q31(QCONST32(3.999999, 29), lpc[1]) < 0) {
1377
      /* Squared radius of the poles. */
1378
293k
      *toneishness = -lpc[1];
1379
#ifdef FIXED_POINT
1380
      freq = (acos_approx(lpc[0]>>1)+delay/2)/delay;
1381
#else
1382
      freq = acos(.5f*lpc[0])/delay;
1383
#endif
1384
293k
   } else {
1385
98.3k
      freq = -1;
1386
98.3k
      *toneishness=0;
1387
98.3k
   }
1388
   /*printf("%f %f %f %f\n", freq, lpc[0], lpc[1], *toneishness);*/
1389
391k
   return freq;
1390
391k
}
celt_encoder.c:tone_detect
Line
Count
Source
1352
195k
static opus_val16 tone_detect(const celt_sig *in, int CC, int N, opus_val32 *toneishness, opus_int32 Fs) {
1353
195k
   int i;
1354
195k
   int delay = 1;
1355
195k
   int fail;
1356
195k
   opus_val32 lpc[2];
1357
195k
   opus_val16 freq;
1358
195k
   VARDECL(opus_val16, x);
1359
195k
   ALLOC(x, N, opus_val16);
1360
   /* Shift by SIG_SHIFT+2 (+3 for stereo) to account for HF gain of the preemphasis filter. */
1361
195k
   if (CC==2) {
1362
75.3M
      for (i=0;i<N;i++) x[i] = PSHR32(ADD32(in[i], in[i+N]), SIG_SHIFT+3);
1363
99.8k
   } else {
1364
52.6M
      for (i=0;i<N;i++) x[i] = PSHR32(in[i], SIG_SHIFT+2);
1365
96.0k
   }
1366
195k
#ifdef FIXED_POINT
1367
195k
   normalize_tone_input(x, N);
1368
195k
#endif
1369
195k
   fail = tone_lpc(x, N, delay, lpc);
1370
   /* If our LPC filter resonates too close to DC, retry the analysis with down-sampling. */
1371
330k
   while (delay <= Fs/3000 && (fail || (lpc[0] > QCONST32(1.f, 29) && lpc[1] < 0))) {
1372
134k
      delay *= 2;
1373
134k
      fail = tone_lpc(x, N, delay, lpc);
1374
134k
   }
1375
   /* Check that our filter has complex roots. */
1376
195k
   if (!fail && MULT32_32_Q31(lpc[0],lpc[0]) + MULT32_32_Q31(QCONST32(3.999999, 29), lpc[1]) < 0) {
1377
      /* Squared radius of the poles. */
1378
146k
      *toneishness = -lpc[1];
1379
146k
#ifdef FIXED_POINT
1380
146k
      freq = (acos_approx(lpc[0]>>1)+delay/2)/delay;
1381
#else
1382
      freq = acos(.5f*lpc[0])/delay;
1383
#endif
1384
146k
   } else {
1385
49.1k
      freq = -1;
1386
49.1k
      *toneishness=0;
1387
49.1k
   }
1388
   /*printf("%f %f %f %f\n", freq, lpc[0], lpc[1], *toneishness);*/
1389
195k
   return freq;
1390
195k
}
celt_encoder.c:tone_detect
Line
Count
Source
1352
195k
static opus_val16 tone_detect(const celt_sig *in, int CC, int N, opus_val32 *toneishness, opus_int32 Fs) {
1353
195k
   int i;
1354
195k
   int delay = 1;
1355
195k
   int fail;
1356
195k
   opus_val32 lpc[2];
1357
195k
   opus_val16 freq;
1358
195k
   VARDECL(opus_val16, x);
1359
195k
   ALLOC(x, N, opus_val16);
1360
   /* Shift by SIG_SHIFT+2 (+3 for stereo) to account for HF gain of the preemphasis filter. */
1361
195k
   if (CC==2) {
1362
75.3M
      for (i=0;i<N;i++) x[i] = PSHR32(ADD32(in[i], in[i+N]), SIG_SHIFT+3);
1363
99.8k
   } else {
1364
52.6M
      for (i=0;i<N;i++) x[i] = PSHR32(in[i], SIG_SHIFT+2);
1365
96.0k
   }
1366
#ifdef FIXED_POINT
1367
   normalize_tone_input(x, N);
1368
#endif
1369
195k
   fail = tone_lpc(x, N, delay, lpc);
1370
   /* If our LPC filter resonates too close to DC, retry the analysis with down-sampling. */
1371
330k
   while (delay <= Fs/3000 && (fail || (lpc[0] > QCONST32(1.f, 29) && lpc[1] < 0))) {
1372
134k
      delay *= 2;
1373
134k
      fail = tone_lpc(x, N, delay, lpc);
1374
134k
   }
1375
   /* Check that our filter has complex roots. */
1376
195k
   if (!fail && MULT32_32_Q31(lpc[0],lpc[0]) + MULT32_32_Q31(QCONST32(3.999999, 29), lpc[1]) < 0) {
1377
      /* Squared radius of the poles. */
1378
146k
      *toneishness = -lpc[1];
1379
#ifdef FIXED_POINT
1380
      freq = (acos_approx(lpc[0]>>1)+delay/2)/delay;
1381
#else
1382
146k
      freq = acos(.5f*lpc[0])/delay;
1383
146k
#endif
1384
146k
   } else {
1385
49.1k
      freq = -1;
1386
49.1k
      *toneishness=0;
1387
49.1k
   }
1388
   /*printf("%f %f %f %f\n", freq, lpc[0], lpc[1], *toneishness);*/
1389
195k
   return freq;
1390
195k
}
1391
1392
static int run_prefilter(CELTEncoder *st, celt_sig *in, celt_sig *prefilter_mem, int CC, int N,
1393
      int prefilter_tapset, int *pitch, opus_val16 *gain, int *qgain, int enabled, int complexity, opus_val16 tf_estimate,
1394
      int nbAvailableBytes, AnalysisInfo *analysis, opus_val16 tone_freq, opus_val32 toneishness ARG_QEXT(int qext_scale))
1395
195k
{
1396
195k
   int c;
1397
195k
   VARDECL(celt_sig, _pre);
1398
195k
   celt_sig *pre[2];
1399
195k
   const CELTMode *mode;
1400
195k
   int pitch_index;
1401
195k
   opus_val16 gain1;
1402
195k
   opus_val16 pf_threshold;
1403
195k
   int pf_on;
1404
195k
   int qg;
1405
195k
   int overlap;
1406
195k
   int min_period, max_period;
1407
195k
   opus_val32 before[2]={0}, after[2]={0};
1408
195k
   int cancel_pitch=0;
1409
195k
   SAVE_STACK;
1410
1411
195k
   max_period = QEXT_SCALE(COMBFILTER_MAXPERIOD);
1412
195k
   min_period = QEXT_SCALE(COMBFILTER_MINPERIOD);
1413
195k
   mode = st->mode;
1414
195k
   overlap = mode->overlap;
1415
195k
   ALLOC(_pre, CC*(N+max_period), celt_sig);
1416
1417
195k
   pre[0] = _pre;
1418
195k
   pre[1] = _pre + (N+max_period);
1419
1420
1421
295k
   c=0; do {
1422
295k
      OPUS_COPY(pre[c], prefilter_mem+c*max_period, max_period);
1423
295k
      OPUS_COPY(pre[c]+max_period, in+c*(N+overlap)+overlap, N);
1424
295k
   } while (++c<CC);
1425
1426
   /* If we detect that the signal is dominated by a single tone, don't rely on the standard pitch
1427
      estimator, as it can become unreliable. */
1428
195k
   if (enabled && toneishness > QCONST32(.99f, 29)) {
1429
642
      int multiple=1;
1430
      /* Using aliased version of the postfilter above 24 kHz.
1431
         First value is purposely slightly above pi to avoid triggering for Fs=48kHz. */
1432
642
      if (QEXT_SCALE(tone_freq) >= QCONST16(3.1416f, 13)) tone_freq = QCONST16(3.141593f, 13) - tone_freq;
1433
      /* If the pitch is too high for our post-filter, apply pitch doubling until
1434
         we can get something that fits (not ideal, but better than nothing). */
1435
3.28k
      while (QEXT_SCALE(tone_freq) >= multiple*QCONST16(0.39f, 13)) multiple++;
1436
642
      if (QEXT_SCALE(tone_freq) > QCONST16(0.006148f, 13)) {
1437
#ifdef FIXED_POINT
1438
270
         pitch_index = IMIN((51472*multiple+QEXT_SCALE(tone_freq)/2)/QEXT_SCALE(tone_freq), COMBFILTER_MAXPERIOD-2);
1439
#else
1440
279
         pitch_index = IMIN((int)floor(.5+2.f*M_PI*multiple/QEXT_SCALE(tone_freq)), COMBFILTER_MAXPERIOD-2);
1441
#endif
1442
549
      } else {
1443
         /* If the pitch is too low, using a very high pitch will actually give us an improvement
1444
            due to the DC component of the filter that will be close to our tone. Again, not ideal,
1445
            but if we only have a single tone, it's better than nothing. */
1446
93
         pitch_index = COMBFILTER_MINPERIOD;
1447
93
      }
1448
642
      gain1 = QCONST16(.75f, 15);
1449
195k
   } else if (enabled && complexity >= 5) {
1450
79.2k
      VARDECL(opus_val16, pitch_buf);
1451
79.2k
      ALLOC(pitch_buf, (max_period+N)>>1, opus_val16);
1452
1453
79.2k
      pitch_downsample(pre, pitch_buf, max_period+N, CC, st->arch);
1454
      /* Don't search for the fir last 1.5 octave of the range because
1455
         there's too many false-positives due to short-term correlation */
1456
79.2k
      pitch_search(pitch_buf+(max_period>>1), pitch_buf, N,
1457
79.2k
            max_period-3*min_period, &pitch_index,
1458
79.2k
            st->arch);
1459
79.2k
      pitch_index = max_period-pitch_index;
1460
1461
79.2k
      gain1 = remove_doubling(pitch_buf, max_period, min_period,
1462
79.2k
            N, &pitch_index, st->prefilter_period, st->prefilter_gain, st->arch);
1463
79.2k
      if (pitch_index > max_period-QEXT_SCALE(2))
1464
624
         pitch_index = max_period-QEXT_SCALE(2);
1465
79.2k
#ifdef ENABLE_QEXT
1466
79.2k
   pitch_index /= qext_scale;
1467
79.2k
#endif
1468
79.2k
      gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1469
      /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.tonality);*/
1470
79.2k
      if (st->loss_rate>2)
1471
34.4k
         gain1 = HALF32(gain1);
1472
79.2k
      if (st->loss_rate>4)
1473
32.4k
         gain1 = HALF32(gain1);
1474
79.2k
      if (st->loss_rate>8)
1475
30.4k
         gain1 = 0;
1476
116k
   } else {
1477
116k
      gain1 = 0;
1478
116k
      pitch_index = COMBFILTER_MINPERIOD;
1479
116k
   }
1480
195k
#ifndef DISABLE_FLOAT_API
1481
195k
   if (analysis->valid)
1482
30.5k
      gain1 = (opus_val16)(gain1 * analysis->max_pitch_ratio);
1483
#else
1484
   (void)analysis;
1485
#endif
1486
   /* Gain threshold for enabling the prefilter/postfilter */
1487
195k
   pf_threshold = QCONST16(.2f,15);
1488
1489
   /* Adjusting the threshold based on rate and continuity */
1490
195k
   if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1491
158k
   {
1492
158k
      pf_threshold += QCONST16(.2f,15);
1493
      /* Completely disable the prefilter on strong transients without continuity. */
1494
158k
      if (tf_estimate > QCONST16(.98f, 14))
1495
52.2k
         gain1 = 0;
1496
158k
   }
1497
195k
   if (nbAvailableBytes<25)
1498
65.4k
      pf_threshold += QCONST16(.1f,15);
1499
195k
   if (nbAvailableBytes<35)
1500
77.6k
      pf_threshold += QCONST16(.1f,15);
1501
195k
   if (st->prefilter_gain > QCONST16(.4f,15))
1502
1.06k
      pf_threshold -= QCONST16(.1f,15);
1503
195k
   if (st->prefilter_gain > QCONST16(.55f,15))
1504
673
      pf_threshold -= QCONST16(.1f,15);
1505
1506
   /* Hard threshold at 0.2 */
1507
195k
   pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1508
195k
   if (gain1<pf_threshold)
1509
185k
   {
1510
185k
      gain1 = 0;
1511
185k
      pf_on = 0;
1512
185k
      qg = 0;
1513
185k
   } else {
1514
      /*This block is not gated by a total bits check only because
1515
        of the nbAvailableBytes check above.*/
1516
9.97k
      if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1517
655
         gain1=st->prefilter_gain;
1518
1519
#ifdef FIXED_POINT
1520
      qg = ((gain1+1536)>>10)/3-1;
1521
#else
1522
      qg = (int)floor(.5f+gain1*32/3)-1;
1523
#endif
1524
9.97k
      qg = IMAX(0, IMIN(7, qg));
1525
9.97k
      gain1 = QCONST16(0.09375f,15)*(qg+1);
1526
9.97k
      pf_on = 1;
1527
9.97k
   }
1528
   /*printf("%d %f\n", pitch_index, gain1);*/
1529
1530
295k
   c=0; do {
1531
295k
      int i;
1532
295k
      int offset = mode->shortMdctSize-overlap;
1533
295k
      st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1534
295k
      OPUS_COPY(in+c*(N+overlap), st->in_mem+c*(overlap), overlap);
1535
167M
      for (i=0;i<N;i++) before[c] += ABS32(SHR32(in[c*(N+overlap)+overlap+i], 12));
1536
295k
      if (offset)
1537
0
         comb_filter(in+c*(N+overlap)+overlap, pre[c]+max_period,
1538
0
               st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1539
0
               st->prefilter_tapset, st->prefilter_tapset, NULL, 0, st->arch);
1540
1541
295k
      comb_filter(in+c*(N+overlap)+overlap+offset, pre[c]+max_period+offset,
1542
295k
            st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1543
295k
            st->prefilter_tapset, prefilter_tapset, mode->window, overlap, st->arch);
1544
167M
      for (i=0;i<N;i++) after[c] += ABS32(SHR32(in[c*(N+overlap)+overlap+i], 12));
1545
295k
   } while (++c<CC);
1546
1547
195k
   if (CC==2) {
1548
99.8k
      opus_val16 thresh[2];
1549
99.8k
      thresh[0] = MULT16_32_Q15(MULT16_16_Q15(QCONST16(.25f, 15), gain1), before[0]) + MULT16_32_Q15(QCONST16(.01f,15), before[1]);
1550
99.8k
      thresh[1] = MULT16_32_Q15(MULT16_16_Q15(QCONST16(.25f, 15), gain1), before[1]) + MULT16_32_Q15(QCONST16(.01f,15), before[0]);
1551
      /* Don't use the filter if one channel gets significantly worse. */
1552
99.8k
      if (after[0]-before[0] > thresh[0] || after[1]-before[1] > thresh[1]) cancel_pitch = 1;
1553
      /* Use the filter only if at least one channel gets significantly better. */
1554
99.8k
      if (before[0]-after[0] <  thresh[0] && before[1]-after[1] < thresh[1]) cancel_pitch = 1;
1555
99.8k
   } else {
1556
      /* Check that the mono channel actually got better. */
1557
96.0k
      if (after[0] > before[0]) cancel_pitch = 1;
1558
96.0k
   }
1559
   /* If needed, revert to a gain of zero. */
1560
195k
   if (cancel_pitch) {
1561
186k
      c=0; do {
1562
186k
         int offset = mode->shortMdctSize-overlap;
1563
186k
         OPUS_COPY(in+c*(N+overlap)+overlap, pre[c]+max_period, N);
1564
186k
         comb_filter(in+c*(N+overlap)+overlap+offset, pre[c]+max_period+offset,
1565
186k
                     st->prefilter_period, pitch_index, overlap, -st->prefilter_gain, -0,
1566
186k
                     st->prefilter_tapset, prefilter_tapset, mode->window, overlap, st->arch);
1567
186k
      } while (++c<CC);
1568
94.6k
      gain1 = 0;
1569
94.6k
      pf_on = 0;
1570
94.6k
      qg = 0;
1571
94.6k
   }
1572
1573
295k
   c=0; do {
1574
295k
      OPUS_COPY(st->in_mem+c*(overlap), in+c*(N+overlap)+N, overlap);
1575
1576
295k
      if (N>max_period)
1577
0
      {
1578
0
         OPUS_COPY(prefilter_mem+c*max_period, pre[c]+N, max_period);
1579
295k
      } else {
1580
295k
         OPUS_MOVE(prefilter_mem+c*max_period, prefilter_mem+c*max_period+N, max_period-N);
1581
295k
         OPUS_COPY(prefilter_mem+c*max_period+max_period-N, pre[c]+max_period, N);
1582
295k
      }
1583
295k
   } while (++c<CC);
1584
1585
195k
   RESTORE_STACK;
1586
195k
   *gain = gain1;
1587
195k
   *pitch = pitch_index;
1588
195k
   *qgain = qg;
1589
195k
   return pf_on;
1590
195k
}
celt_encoder.c:run_prefilter
Line
Count
Source
1395
88.7k
{
1396
88.7k
   int c;
1397
88.7k
   VARDECL(celt_sig, _pre);
1398
88.7k
   celt_sig *pre[2];
1399
88.7k
   const CELTMode *mode;
1400
88.7k
   int pitch_index;
1401
88.7k
   opus_val16 gain1;
1402
88.7k
   opus_val16 pf_threshold;
1403
88.7k
   int pf_on;
1404
88.7k
   int qg;
1405
88.7k
   int overlap;
1406
88.7k
   int min_period, max_period;
1407
88.7k
   opus_val32 before[2]={0}, after[2]={0};
1408
88.7k
   int cancel_pitch=0;
1409
88.7k
   SAVE_STACK;
1410
1411
88.7k
   max_period = QEXT_SCALE(COMBFILTER_MAXPERIOD);
1412
88.7k
   min_period = QEXT_SCALE(COMBFILTER_MINPERIOD);
1413
88.7k
   mode = st->mode;
1414
88.7k
   overlap = mode->overlap;
1415
88.7k
   ALLOC(_pre, CC*(N+max_period), celt_sig);
1416
1417
88.7k
   pre[0] = _pre;
1418
88.7k
   pre[1] = _pre + (N+max_period);
1419
1420
1421
128k
   c=0; do {
1422
128k
      OPUS_COPY(pre[c], prefilter_mem+c*max_period, max_period);
1423
128k
      OPUS_COPY(pre[c]+max_period, in+c*(N+overlap)+overlap, N);
1424
128k
   } while (++c<CC);
1425
1426
   /* If we detect that the signal is dominated by a single tone, don't rely on the standard pitch
1427
      estimator, as it can become unreliable. */
1428
88.7k
   if (enabled && toneishness > QCONST32(.99f, 29)) {
1429
334
      int multiple=1;
1430
      /* Using aliased version of the postfilter above 24 kHz.
1431
         First value is purposely slightly above pi to avoid triggering for Fs=48kHz. */
1432
334
      if (QEXT_SCALE(tone_freq) >= QCONST16(3.1416f, 13)) tone_freq = QCONST16(3.141593f, 13) - tone_freq;
1433
      /* If the pitch is too high for our post-filter, apply pitch doubling until
1434
         we can get something that fits (not ideal, but better than nothing). */
1435
1.90k
      while (QEXT_SCALE(tone_freq) >= multiple*QCONST16(0.39f, 13)) multiple++;
1436
334
      if (QEXT_SCALE(tone_freq) > QCONST16(0.006148f, 13)) {
1437
270
#ifdef FIXED_POINT
1438
270
         pitch_index = IMIN((51472*multiple+QEXT_SCALE(tone_freq)/2)/QEXT_SCALE(tone_freq), COMBFILTER_MAXPERIOD-2);
1439
#else
1440
         pitch_index = IMIN((int)floor(.5+2.f*M_PI*multiple/QEXT_SCALE(tone_freq)), COMBFILTER_MAXPERIOD-2);
1441
#endif
1442
270
      } else {
1443
         /* If the pitch is too low, using a very high pitch will actually give us an improvement
1444
            due to the DC component of the filter that will be close to our tone. Again, not ideal,
1445
            but if we only have a single tone, it's better than nothing. */
1446
64
         pitch_index = COMBFILTER_MINPERIOD;
1447
64
      }
1448
334
      gain1 = QCONST16(.75f, 15);
1449
88.4k
   } else if (enabled && complexity >= 5) {
1450
40.4k
      VARDECL(opus_val16, pitch_buf);
1451
40.4k
      ALLOC(pitch_buf, (max_period+N)>>1, opus_val16);
1452
1453
40.4k
      pitch_downsample(pre, pitch_buf, max_period+N, CC, st->arch);
1454
      /* Don't search for the fir last 1.5 octave of the range because
1455
         there's too many false-positives due to short-term correlation */
1456
40.4k
      pitch_search(pitch_buf+(max_period>>1), pitch_buf, N,
1457
40.4k
            max_period-3*min_period, &pitch_index,
1458
40.4k
            st->arch);
1459
40.4k
      pitch_index = max_period-pitch_index;
1460
1461
40.4k
      gain1 = remove_doubling(pitch_buf, max_period, min_period,
1462
40.4k
            N, &pitch_index, st->prefilter_period, st->prefilter_gain, st->arch);
1463
40.4k
      if (pitch_index > max_period-QEXT_SCALE(2))
1464
217
         pitch_index = max_period-QEXT_SCALE(2);
1465
40.4k
#ifdef ENABLE_QEXT
1466
40.4k
   pitch_index /= qext_scale;
1467
40.4k
#endif
1468
40.4k
      gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1469
      /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.tonality);*/
1470
40.4k
      if (st->loss_rate>2)
1471
18.9k
         gain1 = HALF32(gain1);
1472
40.4k
      if (st->loss_rate>4)
1473
18.2k
         gain1 = HALF32(gain1);
1474
40.4k
      if (st->loss_rate>8)
1475
17.1k
         gain1 = 0;
1476
47.9k
   } else {
1477
47.9k
      gain1 = 0;
1478
47.9k
      pitch_index = COMBFILTER_MINPERIOD;
1479
47.9k
   }
1480
88.7k
#ifndef DISABLE_FLOAT_API
1481
88.7k
   if (analysis->valid)
1482
11.5k
      gain1 = (opus_val16)(gain1 * analysis->max_pitch_ratio);
1483
#else
1484
   (void)analysis;
1485
#endif
1486
   /* Gain threshold for enabling the prefilter/postfilter */
1487
88.7k
   pf_threshold = QCONST16(.2f,15);
1488
1489
   /* Adjusting the threshold based on rate and continuity */
1490
88.7k
   if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1491
75.4k
   {
1492
75.4k
      pf_threshold += QCONST16(.2f,15);
1493
      /* Completely disable the prefilter on strong transients without continuity. */
1494
75.4k
      if (tf_estimate > QCONST16(.98f, 14))
1495
21.3k
         gain1 = 0;
1496
75.4k
   }
1497
88.7k
   if (nbAvailableBytes<25)
1498
25.9k
      pf_threshold += QCONST16(.1f,15);
1499
88.7k
   if (nbAvailableBytes<35)
1500
31.8k
      pf_threshold += QCONST16(.1f,15);
1501
88.7k
   if (st->prefilter_gain > QCONST16(.4f,15))
1502
579
      pf_threshold -= QCONST16(.1f,15);
1503
88.7k
   if (st->prefilter_gain > QCONST16(.55f,15))
1504
353
      pf_threshold -= QCONST16(.1f,15);
1505
1506
   /* Hard threshold at 0.2 */
1507
88.7k
   pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1508
88.7k
   if (gain1<pf_threshold)
1509
83.1k
   {
1510
83.1k
      gain1 = 0;
1511
83.1k
      pf_on = 0;
1512
83.1k
      qg = 0;
1513
83.1k
   } else {
1514
      /*This block is not gated by a total bits check only because
1515
        of the nbAvailableBytes check above.*/
1516
5.57k
      if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1517
373
         gain1=st->prefilter_gain;
1518
1519
5.57k
#ifdef FIXED_POINT
1520
5.57k
      qg = ((gain1+1536)>>10)/3-1;
1521
#else
1522
      qg = (int)floor(.5f+gain1*32/3)-1;
1523
#endif
1524
5.57k
      qg = IMAX(0, IMIN(7, qg));
1525
5.57k
      gain1 = QCONST16(0.09375f,15)*(qg+1);
1526
5.57k
      pf_on = 1;
1527
5.57k
   }
1528
   /*printf("%d %f\n", pitch_index, gain1);*/
1529
1530
128k
   c=0; do {
1531
128k
      int i;
1532
128k
      int offset = mode->shortMdctSize-overlap;
1533
128k
      st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1534
128k
      OPUS_COPY(in+c*(N+overlap), st->in_mem+c*(overlap), overlap);
1535
67.0M
      for (i=0;i<N;i++) before[c] += ABS32(SHR32(in[c*(N+overlap)+overlap+i], 12));
1536
128k
      if (offset)
1537
0
         comb_filter(in+c*(N+overlap)+overlap, pre[c]+max_period,
1538
0
               st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1539
0
               st->prefilter_tapset, st->prefilter_tapset, NULL, 0, st->arch);
1540
1541
128k
      comb_filter(in+c*(N+overlap)+overlap+offset, pre[c]+max_period+offset,
1542
128k
            st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1543
128k
            st->prefilter_tapset, prefilter_tapset, mode->window, overlap, st->arch);
1544
67.0M
      for (i=0;i<N;i++) after[c] += ABS32(SHR32(in[c*(N+overlap)+overlap+i], 12));
1545
128k
   } while (++c<CC);
1546
1547
88.7k
   if (CC==2) {
1548
39.3k
      opus_val16 thresh[2];
1549
39.3k
      thresh[0] = MULT16_32_Q15(MULT16_16_Q15(QCONST16(.25f, 15), gain1), before[0]) + MULT16_32_Q15(QCONST16(.01f,15), before[1]);
1550
39.3k
      thresh[1] = MULT16_32_Q15(MULT16_16_Q15(QCONST16(.25f, 15), gain1), before[1]) + MULT16_32_Q15(QCONST16(.01f,15), before[0]);
1551
      /* Don't use the filter if one channel gets significantly worse. */
1552
39.3k
      if (after[0]-before[0] > thresh[0] || after[1]-before[1] > thresh[1]) cancel_pitch = 1;
1553
      /* Use the filter only if at least one channel gets significantly better. */
1554
39.3k
      if (before[0]-after[0] <  thresh[0] && before[1]-after[1] < thresh[1]) cancel_pitch = 1;
1555
49.3k
   } else {
1556
      /* Check that the mono channel actually got better. */
1557
49.3k
      if (after[0] > before[0]) cancel_pitch = 1;
1558
49.3k
   }
1559
   /* If needed, revert to a gain of zero. */
1560
88.7k
   if (cancel_pitch) {
1561
68.1k
      c=0; do {
1562
68.1k
         int offset = mode->shortMdctSize-overlap;
1563
68.1k
         OPUS_COPY(in+c*(N+overlap)+overlap, pre[c]+max_period, N);
1564
68.1k
         comb_filter(in+c*(N+overlap)+overlap+offset, pre[c]+max_period+offset,
1565
68.1k
                     st->prefilter_period, pitch_index, overlap, -st->prefilter_gain, -0,
1566
68.1k
                     st->prefilter_tapset, prefilter_tapset, mode->window, overlap, st->arch);
1567
68.1k
      } while (++c<CC);
1568
34.9k
      gain1 = 0;
1569
34.9k
      pf_on = 0;
1570
34.9k
      qg = 0;
1571
34.9k
   }
1572
1573
128k
   c=0; do {
1574
128k
      OPUS_COPY(st->in_mem+c*(overlap), in+c*(N+overlap)+N, overlap);
1575
1576
128k
      if (N>max_period)
1577
0
      {
1578
0
         OPUS_COPY(prefilter_mem+c*max_period, pre[c]+N, max_period);
1579
128k
      } else {
1580
128k
         OPUS_MOVE(prefilter_mem+c*max_period, prefilter_mem+c*max_period+N, max_period-N);
1581
128k
         OPUS_COPY(prefilter_mem+c*max_period+max_period-N, pre[c]+max_period, N);
1582
128k
      }
1583
128k
   } while (++c<CC);
1584
1585
88.7k
   RESTORE_STACK;
1586
88.7k
   *gain = gain1;
1587
88.7k
   *pitch = pitch_index;
1588
88.7k
   *qgain = qg;
1589
88.7k
   return pf_on;
1590
88.7k
}
celt_encoder.c:run_prefilter
Line
Count
Source
1395
107k
{
1396
107k
   int c;
1397
107k
   VARDECL(celt_sig, _pre);
1398
107k
   celt_sig *pre[2];
1399
107k
   const CELTMode *mode;
1400
107k
   int pitch_index;
1401
107k
   opus_val16 gain1;
1402
107k
   opus_val16 pf_threshold;
1403
107k
   int pf_on;
1404
107k
   int qg;
1405
107k
   int overlap;
1406
107k
   int min_period, max_period;
1407
107k
   opus_val32 before[2]={0}, after[2]={0};
1408
107k
   int cancel_pitch=0;
1409
107k
   SAVE_STACK;
1410
1411
107k
   max_period = QEXT_SCALE(COMBFILTER_MAXPERIOD);
1412
107k
   min_period = QEXT_SCALE(COMBFILTER_MINPERIOD);
1413
107k
   mode = st->mode;
1414
107k
   overlap = mode->overlap;
1415
107k
   ALLOC(_pre, CC*(N+max_period), celt_sig);
1416
1417
107k
   pre[0] = _pre;
1418
107k
   pre[1] = _pre + (N+max_period);
1419
1420
1421
167k
   c=0; do {
1422
167k
      OPUS_COPY(pre[c], prefilter_mem+c*max_period, max_period);
1423
167k
      OPUS_COPY(pre[c]+max_period, in+c*(N+overlap)+overlap, N);
1424
167k
   } while (++c<CC);
1425
1426
   /* If we detect that the signal is dominated by a single tone, don't rely on the standard pitch
1427
      estimator, as it can become unreliable. */
1428
107k
   if (enabled && toneishness > QCONST32(.99f, 29)) {
1429
308
      int multiple=1;
1430
      /* Using aliased version of the postfilter above 24 kHz.
1431
         First value is purposely slightly above pi to avoid triggering for Fs=48kHz. */
1432
308
      if (QEXT_SCALE(tone_freq) >= QCONST16(3.1416f, 13)) tone_freq = QCONST16(3.141593f, 13) - tone_freq;
1433
      /* If the pitch is too high for our post-filter, apply pitch doubling until
1434
         we can get something that fits (not ideal, but better than nothing). */
1435
1.37k
      while (QEXT_SCALE(tone_freq) >= multiple*QCONST16(0.39f, 13)) multiple++;
1436
308
      if (QEXT_SCALE(tone_freq) > QCONST16(0.006148f, 13)) {
1437
#ifdef FIXED_POINT
1438
         pitch_index = IMIN((51472*multiple+QEXT_SCALE(tone_freq)/2)/QEXT_SCALE(tone_freq), COMBFILTER_MAXPERIOD-2);
1439
#else
1440
279
         pitch_index = IMIN((int)floor(.5+2.f*M_PI*multiple/QEXT_SCALE(tone_freq)), COMBFILTER_MAXPERIOD-2);
1441
279
#endif
1442
279
      } else {
1443
         /* If the pitch is too low, using a very high pitch will actually give us an improvement
1444
            due to the DC component of the filter that will be close to our tone. Again, not ideal,
1445
            but if we only have a single tone, it's better than nothing. */
1446
29
         pitch_index = COMBFILTER_MINPERIOD;
1447
29
      }
1448
308
      gain1 = QCONST16(.75f, 15);
1449
106k
   } else if (enabled && complexity >= 5) {
1450
38.8k
      VARDECL(opus_val16, pitch_buf);
1451
38.8k
      ALLOC(pitch_buf, (max_period+N)>>1, opus_val16);
1452
1453
38.8k
      pitch_downsample(pre, pitch_buf, max_period+N, CC, st->arch);
1454
      /* Don't search for the fir last 1.5 octave of the range because
1455
         there's too many false-positives due to short-term correlation */
1456
38.8k
      pitch_search(pitch_buf+(max_period>>1), pitch_buf, N,
1457
38.8k
            max_period-3*min_period, &pitch_index,
1458
38.8k
            st->arch);
1459
38.8k
      pitch_index = max_period-pitch_index;
1460
1461
38.8k
      gain1 = remove_doubling(pitch_buf, max_period, min_period,
1462
38.8k
            N, &pitch_index, st->prefilter_period, st->prefilter_gain, st->arch);
1463
38.8k
      if (pitch_index > max_period-QEXT_SCALE(2))
1464
407
         pitch_index = max_period-QEXT_SCALE(2);
1465
38.8k
#ifdef ENABLE_QEXT
1466
38.8k
   pitch_index /= qext_scale;
1467
38.8k
#endif
1468
38.8k
      gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1469
      /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.tonality);*/
1470
38.8k
      if (st->loss_rate>2)
1471
15.4k
         gain1 = HALF32(gain1);
1472
38.8k
      if (st->loss_rate>4)
1473
14.1k
         gain1 = HALF32(gain1);
1474
38.8k
      if (st->loss_rate>8)
1475
13.2k
         gain1 = 0;
1476
68.0k
   } else {
1477
68.0k
      gain1 = 0;
1478
68.0k
      pitch_index = COMBFILTER_MINPERIOD;
1479
68.0k
   }
1480
107k
#ifndef DISABLE_FLOAT_API
1481
107k
   if (analysis->valid)
1482
19.0k
      gain1 = (opus_val16)(gain1 * analysis->max_pitch_ratio);
1483
#else
1484
   (void)analysis;
1485
#endif
1486
   /* Gain threshold for enabling the prefilter/postfilter */
1487
107k
   pf_threshold = QCONST16(.2f,15);
1488
1489
   /* Adjusting the threshold based on rate and continuity */
1490
107k
   if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1491
83.4k
   {
1492
83.4k
      pf_threshold += QCONST16(.2f,15);
1493
      /* Completely disable the prefilter on strong transients without continuity. */
1494
83.4k
      if (tf_estimate > QCONST16(.98f, 14))
1495
30.9k
         gain1 = 0;
1496
83.4k
   }
1497
107k
   if (nbAvailableBytes<25)
1498
39.4k
      pf_threshold += QCONST16(.1f,15);
1499
107k
   if (nbAvailableBytes<35)
1500
45.7k
      pf_threshold += QCONST16(.1f,15);
1501
107k
   if (st->prefilter_gain > QCONST16(.4f,15))
1502
487
      pf_threshold -= QCONST16(.1f,15);
1503
107k
   if (st->prefilter_gain > QCONST16(.55f,15))
1504
320
      pf_threshold -= QCONST16(.1f,15);
1505
1506
   /* Hard threshold at 0.2 */
1507
107k
   pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1508
107k
   if (gain1<pf_threshold)
1509
102k
   {
1510
102k
      gain1 = 0;
1511
102k
      pf_on = 0;
1512
102k
      qg = 0;
1513
102k
   } else {
1514
      /*This block is not gated by a total bits check only because
1515
        of the nbAvailableBytes check above.*/
1516
4.40k
      if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1517
282
         gain1=st->prefilter_gain;
1518
1519
#ifdef FIXED_POINT
1520
      qg = ((gain1+1536)>>10)/3-1;
1521
#else
1522
4.40k
      qg = (int)floor(.5f+gain1*32/3)-1;
1523
4.40k
#endif
1524
4.40k
      qg = IMAX(0, IMIN(7, qg));
1525
4.40k
      gain1 = QCONST16(0.09375f,15)*(qg+1);
1526
4.40k
      pf_on = 1;
1527
4.40k
   }
1528
   /*printf("%d %f\n", pitch_index, gain1);*/
1529
1530
167k
   c=0; do {
1531
167k
      int i;
1532
167k
      int offset = mode->shortMdctSize-overlap;
1533
167k
      st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1534
167k
      OPUS_COPY(in+c*(N+overlap), st->in_mem+c*(overlap), overlap);
1535
99.9M
      for (i=0;i<N;i++) before[c] += ABS32(SHR32(in[c*(N+overlap)+overlap+i], 12));
1536
167k
      if (offset)
1537
0
         comb_filter(in+c*(N+overlap)+overlap, pre[c]+max_period,
1538
0
               st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1539
0
               st->prefilter_tapset, st->prefilter_tapset, NULL, 0, st->arch);
1540
1541
167k
      comb_filter(in+c*(N+overlap)+overlap+offset, pre[c]+max_period+offset,
1542
167k
            st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1543
167k
            st->prefilter_tapset, prefilter_tapset, mode->window, overlap, st->arch);
1544
99.9M
      for (i=0;i<N;i++) after[c] += ABS32(SHR32(in[c*(N+overlap)+overlap+i], 12));
1545
167k
   } while (++c<CC);
1546
1547
107k
   if (CC==2) {
1548
60.5k
      opus_val16 thresh[2];
1549
60.5k
      thresh[0] = MULT16_32_Q15(MULT16_16_Q15(QCONST16(.25f, 15), gain1), before[0]) + MULT16_32_Q15(QCONST16(.01f,15), before[1]);
1550
60.5k
      thresh[1] = MULT16_32_Q15(MULT16_16_Q15(QCONST16(.25f, 15), gain1), before[1]) + MULT16_32_Q15(QCONST16(.01f,15), before[0]);
1551
      /* Don't use the filter if one channel gets significantly worse. */
1552
60.5k
      if (after[0]-before[0] > thresh[0] || after[1]-before[1] > thresh[1]) cancel_pitch = 1;
1553
      /* Use the filter only if at least one channel gets significantly better. */
1554
60.5k
      if (before[0]-after[0] <  thresh[0] && before[1]-after[1] < thresh[1]) cancel_pitch = 1;
1555
60.5k
   } else {
1556
      /* Check that the mono channel actually got better. */
1557
46.6k
      if (after[0] > before[0]) cancel_pitch = 1;
1558
46.6k
   }
1559
   /* If needed, revert to a gain of zero. */
1560
107k
   if (cancel_pitch) {
1561
118k
      c=0; do {
1562
118k
         int offset = mode->shortMdctSize-overlap;
1563
118k
         OPUS_COPY(in+c*(N+overlap)+overlap, pre[c]+max_period, N);
1564
118k
         comb_filter(in+c*(N+overlap)+overlap+offset, pre[c]+max_period+offset,
1565
118k
                     st->prefilter_period, pitch_index, overlap, -st->prefilter_gain, -0,
1566
118k
                     st->prefilter_tapset, prefilter_tapset, mode->window, overlap, st->arch);
1567
118k
      } while (++c<CC);
1568
59.7k
      gain1 = 0;
1569
59.7k
      pf_on = 0;
1570
59.7k
      qg = 0;
1571
59.7k
   }
1572
1573
167k
   c=0; do {
1574
167k
      OPUS_COPY(st->in_mem+c*(overlap), in+c*(N+overlap)+N, overlap);
1575
1576
167k
      if (N>max_period)
1577
0
      {
1578
0
         OPUS_COPY(prefilter_mem+c*max_period, pre[c]+N, max_period);
1579
167k
      } else {
1580
167k
         OPUS_MOVE(prefilter_mem+c*max_period, prefilter_mem+c*max_period+N, max_period-N);
1581
167k
         OPUS_COPY(prefilter_mem+c*max_period+max_period-N, pre[c]+max_period, N);
1582
167k
      }
1583
167k
   } while (++c<CC);
1584
1585
107k
   RESTORE_STACK;
1586
107k
   *gain = gain1;
1587
107k
   *pitch = pitch_index;
1588
107k
   *qgain = qg;
1589
107k
   return pf_on;
1590
107k
}
1591
1592
static int compute_vbr(const CELTMode *mode, AnalysisInfo *analysis, opus_int32 base_target,
1593
      int LM, opus_int32 bitrate, int lastCodedBands, int C, int intensity,
1594
      int constrained_vbr, opus_val16 stereo_saving, int tot_boost,
1595
      opus_val16 tf_estimate, int pitch_change, celt_glog maxDepth,
1596
      int lfe, int has_surround_mask, celt_glog surround_masking,
1597
      celt_glog temporal_vbr ARG_QEXT(int enable_qext))
1598
157k
{
1599
   /* The target rate in 8th bits per frame */
1600
157k
   opus_int32 target;
1601
157k
   int coded_bins;
1602
157k
   int coded_bands;
1603
157k
   opus_val16 tf_calibration;
1604
157k
   int nbEBands;
1605
157k
   const opus_int16 *eBands;
1606
1607
157k
   nbEBands = mode->nbEBands;
1608
157k
   eBands = mode->eBands;
1609
1610
157k
   coded_bands = lastCodedBands ? lastCodedBands : nbEBands;
1611
157k
   coded_bins = eBands[coded_bands]<<LM;
1612
157k
   if (C==2)
1613
64.1k
      coded_bins += eBands[IMIN(intensity, coded_bands)]<<LM;
1614
1615
157k
   target = base_target;
1616
1617
   /*printf("%f %f %f %f %d %d ", st->analysis.activity, st->analysis.tonality, tf_estimate, st->stereo_saving, tot_boost, coded_bands);*/
1618
157k
#ifndef DISABLE_FLOAT_API
1619
157k
   if (analysis->valid && analysis->activity<.4)
1620
418
      target -= (opus_int32)((coded_bins<<BITRES)*(.4f-analysis->activity));
1621
157k
#endif
1622
   /* Stereo savings */
1623
157k
   if (C==2)
1624
64.1k
   {
1625
64.1k
      int coded_stereo_bands;
1626
64.1k
      int coded_stereo_dof;
1627
64.1k
      opus_val16 max_frac;
1628
64.1k
      coded_stereo_bands = IMIN(intensity, coded_bands);
1629
64.1k
      coded_stereo_dof = (eBands[coded_stereo_bands]<<LM)-coded_stereo_bands;
1630
      /* Maximum fraction of the bits we can save if the signal is mono. */
1631
64.1k
      max_frac = DIV32_16(MULT16_16(QCONST16(0.8f, 15), coded_stereo_dof), coded_bins);
1632
64.1k
      stereo_saving = MIN16(stereo_saving, QCONST16(1.f, 8));
1633
      /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/
1634
64.1k
      target -= (opus_int32)MIN32(MULT16_32_Q15(max_frac,target),
1635
64.1k
                      SHR32(MULT16_16(stereo_saving-QCONST16(0.1f,8),(coded_stereo_dof<<BITRES)),8));
1636
64.1k
   }
1637
   /* Boost the rate according to dynalloc (minus the dynalloc average for calibration). */
1638
157k
   target += tot_boost-(19<<LM);
1639
   /* Apply transient boost, compensating for average boost. */
1640
157k
   tf_calibration = QCONST16(0.044f,14);
1641
157k
   target += (opus_int32)SHL32(MULT16_32_Q15(tf_estimate-tf_calibration, target),1);
1642
1643
157k
#ifndef DISABLE_FLOAT_API
1644
   /* Apply tonality boost */
1645
157k
   if (analysis->valid && !lfe)
1646
18.7k
   {
1647
18.7k
      opus_int32 tonal_target;
1648
18.7k
      float tonal;
1649
1650
      /* Tonality boost (compensating for the average). */
1651
18.7k
      tonal = MAX16(0.f,analysis->tonality-.15f)-0.12f;
1652
18.7k
      tonal_target = target + (opus_int32)((coded_bins<<BITRES)*1.2f*tonal);
1653
18.7k
      if (pitch_change)
1654
130
         tonal_target +=  (opus_int32)((coded_bins<<BITRES)*.8f);
1655
      /*printf("%f %f ", analysis->tonality, tonal);*/
1656
18.7k
      target = tonal_target;
1657
18.7k
   }
1658
#else
1659
   (void)analysis;
1660
   (void)pitch_change;
1661
#endif
1662
1663
157k
   if (has_surround_mask&&!lfe)
1664
7.86k
   {
1665
7.86k
      opus_int32 surround_target = target + (opus_int32)SHR32(MULT16_16(SHR32(surround_masking,DB_SHIFT-10),coded_bins<<BITRES), 10);
1666
      /*printf("%f %d %d %d %d %d %d ", surround_masking, coded_bins, st->end, st->intensity, surround_target, target, st->bitrate);*/
1667
7.86k
      target = IMAX(target/4, surround_target);
1668
7.86k
   }
1669
1670
157k
   {
1671
157k
      opus_int32 floor_depth;
1672
157k
      int bins;
1673
157k
      bins = eBands[nbEBands-2]<<LM;
1674
157k
#ifdef ENABLE_QEXT
1675
157k
      if (enable_qext) bins  = mode->shortMdctSize<<LM;
1676
157k
#endif
1677
      /*floor_depth = SHR32(MULT16_16((C*bins<<BITRES),celt_log2(SHL32(MAX16(1,sample_max),13))), DB_SHIFT);*/
1678
157k
      floor_depth = (opus_int32)SHR32(MULT16_32_Q15((C*bins<<BITRES),maxDepth), DB_SHIFT-15);
1679
157k
      floor_depth = IMAX(floor_depth, target>>2);
1680
157k
      target = IMIN(target, floor_depth);
1681
      /*printf("%f %d\n", maxDepth, floor_depth);*/
1682
157k
   }
1683
1684
   /* Make VBR less aggressive for constrained VBR because we can't keep a higher bitrate
1685
      for long. Needs tuning. */
1686
157k
   if ((!has_surround_mask||lfe) && constrained_vbr)
1687
58.6k
   {
1688
58.6k
      target = base_target + (opus_int32)MULT16_32_Q15(QCONST16(0.67f, 15), target-base_target);
1689
58.6k
   }
1690
1691
157k
   if (!has_surround_mask && tf_estimate < QCONST16(.2f, 14))
1692
44.9k
   {
1693
44.9k
      opus_val16 amount;
1694
44.9k
      opus_val16 tvbr_factor;
1695
44.9k
      amount = MULT16_16_Q15(QCONST16(.0000031f, 30), IMAX(0, IMIN(32000, 96000-bitrate)));
1696
44.9k
      tvbr_factor = SHR32(MULT16_16(SHR32(temporal_vbr, DB_SHIFT-10), amount), 10);
1697
44.9k
      target += (opus_int32)MULT16_32_Q15(tvbr_factor, target);
1698
44.9k
   }
1699
1700
   /* Don't allow more than doubling the rate */
1701
157k
   target = IMIN(2*base_target, target);
1702
1703
157k
   return target;
1704
157k
}
celt_encoder.c:compute_vbr
Line
Count
Source
1598
78.9k
{
1599
   /* The target rate in 8th bits per frame */
1600
78.9k
   opus_int32 target;
1601
78.9k
   int coded_bins;
1602
78.9k
   int coded_bands;
1603
78.9k
   opus_val16 tf_calibration;
1604
78.9k
   int nbEBands;
1605
78.9k
   const opus_int16 *eBands;
1606
1607
78.9k
   nbEBands = mode->nbEBands;
1608
78.9k
   eBands = mode->eBands;
1609
1610
78.9k
   coded_bands = lastCodedBands ? lastCodedBands : nbEBands;
1611
78.9k
   coded_bins = eBands[coded_bands]<<LM;
1612
78.9k
   if (C==2)
1613
32.0k
      coded_bins += eBands[IMIN(intensity, coded_bands)]<<LM;
1614
1615
78.9k
   target = base_target;
1616
1617
   /*printf("%f %f %f %f %d %d ", st->analysis.activity, st->analysis.tonality, tf_estimate, st->stereo_saving, tot_boost, coded_bands);*/
1618
78.9k
#ifndef DISABLE_FLOAT_API
1619
78.9k
   if (analysis->valid && analysis->activity<.4)
1620
209
      target -= (opus_int32)((coded_bins<<BITRES)*(.4f-analysis->activity));
1621
78.9k
#endif
1622
   /* Stereo savings */
1623
78.9k
   if (C==2)
1624
32.0k
   {
1625
32.0k
      int coded_stereo_bands;
1626
32.0k
      int coded_stereo_dof;
1627
32.0k
      opus_val16 max_frac;
1628
32.0k
      coded_stereo_bands = IMIN(intensity, coded_bands);
1629
32.0k
      coded_stereo_dof = (eBands[coded_stereo_bands]<<LM)-coded_stereo_bands;
1630
      /* Maximum fraction of the bits we can save if the signal is mono. */
1631
32.0k
      max_frac = DIV32_16(MULT16_16(QCONST16(0.8f, 15), coded_stereo_dof), coded_bins);
1632
32.0k
      stereo_saving = MIN16(stereo_saving, QCONST16(1.f, 8));
1633
      /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/
1634
32.0k
      target -= (opus_int32)MIN32(MULT16_32_Q15(max_frac,target),
1635
32.0k
                      SHR32(MULT16_16(stereo_saving-QCONST16(0.1f,8),(coded_stereo_dof<<BITRES)),8));
1636
32.0k
   }
1637
   /* Boost the rate according to dynalloc (minus the dynalloc average for calibration). */
1638
78.9k
   target += tot_boost-(19<<LM);
1639
   /* Apply transient boost, compensating for average boost. */
1640
78.9k
   tf_calibration = QCONST16(0.044f,14);
1641
78.9k
   target += (opus_int32)SHL32(MULT16_32_Q15(tf_estimate-tf_calibration, target),1);
1642
1643
78.9k
#ifndef DISABLE_FLOAT_API
1644
   /* Apply tonality boost */
1645
78.9k
   if (analysis->valid && !lfe)
1646
9.39k
   {
1647
9.39k
      opus_int32 tonal_target;
1648
9.39k
      float tonal;
1649
1650
      /* Tonality boost (compensating for the average). */
1651
9.39k
      tonal = MAX16(0.f,analysis->tonality-.15f)-0.12f;
1652
9.39k
      tonal_target = target + (opus_int32)((coded_bins<<BITRES)*1.2f*tonal);
1653
9.39k
      if (pitch_change)
1654
65
         tonal_target +=  (opus_int32)((coded_bins<<BITRES)*.8f);
1655
      /*printf("%f %f ", analysis->tonality, tonal);*/
1656
9.39k
      target = tonal_target;
1657
9.39k
   }
1658
#else
1659
   (void)analysis;
1660
   (void)pitch_change;
1661
#endif
1662
1663
78.9k
   if (has_surround_mask&&!lfe)
1664
3.93k
   {
1665
3.93k
      opus_int32 surround_target = target + (opus_int32)SHR32(MULT16_16(SHR32(surround_masking,DB_SHIFT-10),coded_bins<<BITRES), 10);
1666
      /*printf("%f %d %d %d %d %d %d ", surround_masking, coded_bins, st->end, st->intensity, surround_target, target, st->bitrate);*/
1667
3.93k
      target = IMAX(target/4, surround_target);
1668
3.93k
   }
1669
1670
78.9k
   {
1671
78.9k
      opus_int32 floor_depth;
1672
78.9k
      int bins;
1673
78.9k
      bins = eBands[nbEBands-2]<<LM;
1674
78.9k
#ifdef ENABLE_QEXT
1675
78.9k
      if (enable_qext) bins  = mode->shortMdctSize<<LM;
1676
78.9k
#endif
1677
      /*floor_depth = SHR32(MULT16_16((C*bins<<BITRES),celt_log2(SHL32(MAX16(1,sample_max),13))), DB_SHIFT);*/
1678
78.9k
      floor_depth = (opus_int32)SHR32(MULT16_32_Q15((C*bins<<BITRES),maxDepth), DB_SHIFT-15);
1679
78.9k
      floor_depth = IMAX(floor_depth, target>>2);
1680
78.9k
      target = IMIN(target, floor_depth);
1681
      /*printf("%f %d\n", maxDepth, floor_depth);*/
1682
78.9k
   }
1683
1684
   /* Make VBR less aggressive for constrained VBR because we can't keep a higher bitrate
1685
      for long. Needs tuning. */
1686
78.9k
   if ((!has_surround_mask||lfe) && constrained_vbr)
1687
29.3k
   {
1688
29.3k
      target = base_target + (opus_int32)MULT16_32_Q15(QCONST16(0.67f, 15), target-base_target);
1689
29.3k
   }
1690
1691
78.9k
   if (!has_surround_mask && tf_estimate < QCONST16(.2f, 14))
1692
22.4k
   {
1693
22.4k
      opus_val16 amount;
1694
22.4k
      opus_val16 tvbr_factor;
1695
22.4k
      amount = MULT16_16_Q15(QCONST16(.0000031f, 30), IMAX(0, IMIN(32000, 96000-bitrate)));
1696
22.4k
      tvbr_factor = SHR32(MULT16_16(SHR32(temporal_vbr, DB_SHIFT-10), amount), 10);
1697
22.4k
      target += (opus_int32)MULT16_32_Q15(tvbr_factor, target);
1698
22.4k
   }
1699
1700
   /* Don't allow more than doubling the rate */
1701
78.9k
   target = IMIN(2*base_target, target);
1702
1703
78.9k
   return target;
1704
78.9k
}
celt_encoder.c:compute_vbr
Line
Count
Source
1598
78.9k
{
1599
   /* The target rate in 8th bits per frame */
1600
78.9k
   opus_int32 target;
1601
78.9k
   int coded_bins;
1602
78.9k
   int coded_bands;
1603
78.9k
   opus_val16 tf_calibration;
1604
78.9k
   int nbEBands;
1605
78.9k
   const opus_int16 *eBands;
1606
1607
78.9k
   nbEBands = mode->nbEBands;
1608
78.9k
   eBands = mode->eBands;
1609
1610
78.9k
   coded_bands = lastCodedBands ? lastCodedBands : nbEBands;
1611
78.9k
   coded_bins = eBands[coded_bands]<<LM;
1612
78.9k
   if (C==2)
1613
32.0k
      coded_bins += eBands[IMIN(intensity, coded_bands)]<<LM;
1614
1615
78.9k
   target = base_target;
1616
1617
   /*printf("%f %f %f %f %d %d ", st->analysis.activity, st->analysis.tonality, tf_estimate, st->stereo_saving, tot_boost, coded_bands);*/
1618
78.9k
#ifndef DISABLE_FLOAT_API
1619
78.9k
   if (analysis->valid && analysis->activity<.4)
1620
209
      target -= (opus_int32)((coded_bins<<BITRES)*(.4f-analysis->activity));
1621
78.9k
#endif
1622
   /* Stereo savings */
1623
78.9k
   if (C==2)
1624
32.0k
   {
1625
32.0k
      int coded_stereo_bands;
1626
32.0k
      int coded_stereo_dof;
1627
32.0k
      opus_val16 max_frac;
1628
32.0k
      coded_stereo_bands = IMIN(intensity, coded_bands);
1629
32.0k
      coded_stereo_dof = (eBands[coded_stereo_bands]<<LM)-coded_stereo_bands;
1630
      /* Maximum fraction of the bits we can save if the signal is mono. */
1631
32.0k
      max_frac = DIV32_16(MULT16_16(QCONST16(0.8f, 15), coded_stereo_dof), coded_bins);
1632
32.0k
      stereo_saving = MIN16(stereo_saving, QCONST16(1.f, 8));
1633
      /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/
1634
32.0k
      target -= (opus_int32)MIN32(MULT16_32_Q15(max_frac,target),
1635
32.0k
                      SHR32(MULT16_16(stereo_saving-QCONST16(0.1f,8),(coded_stereo_dof<<BITRES)),8));
1636
32.0k
   }
1637
   /* Boost the rate according to dynalloc (minus the dynalloc average for calibration). */
1638
78.9k
   target += tot_boost-(19<<LM);
1639
   /* Apply transient boost, compensating for average boost. */
1640
78.9k
   tf_calibration = QCONST16(0.044f,14);
1641
78.9k
   target += (opus_int32)SHL32(MULT16_32_Q15(tf_estimate-tf_calibration, target),1);
1642
1643
78.9k
#ifndef DISABLE_FLOAT_API
1644
   /* Apply tonality boost */
1645
78.9k
   if (analysis->valid && !lfe)
1646
9.39k
   {
1647
9.39k
      opus_int32 tonal_target;
1648
9.39k
      float tonal;
1649
1650
      /* Tonality boost (compensating for the average). */
1651
9.39k
      tonal = MAX16(0.f,analysis->tonality-.15f)-0.12f;
1652
9.39k
      tonal_target = target + (opus_int32)((coded_bins<<BITRES)*1.2f*tonal);
1653
9.39k
      if (pitch_change)
1654
65
         tonal_target +=  (opus_int32)((coded_bins<<BITRES)*.8f);
1655
      /*printf("%f %f ", analysis->tonality, tonal);*/
1656
9.39k
      target = tonal_target;
1657
9.39k
   }
1658
#else
1659
   (void)analysis;
1660
   (void)pitch_change;
1661
#endif
1662
1663
78.9k
   if (has_surround_mask&&!lfe)
1664
3.93k
   {
1665
3.93k
      opus_int32 surround_target = target + (opus_int32)SHR32(MULT16_16(SHR32(surround_masking,DB_SHIFT-10),coded_bins<<BITRES), 10);
1666
      /*printf("%f %d %d %d %d %d %d ", surround_masking, coded_bins, st->end, st->intensity, surround_target, target, st->bitrate);*/
1667
3.93k
      target = IMAX(target/4, surround_target);
1668
3.93k
   }
1669
1670
78.9k
   {
1671
78.9k
      opus_int32 floor_depth;
1672
78.9k
      int bins;
1673
78.9k
      bins = eBands[nbEBands-2]<<LM;
1674
78.9k
#ifdef ENABLE_QEXT
1675
78.9k
      if (enable_qext) bins  = mode->shortMdctSize<<LM;
1676
78.9k
#endif
1677
      /*floor_depth = SHR32(MULT16_16((C*bins<<BITRES),celt_log2(SHL32(MAX16(1,sample_max),13))), DB_SHIFT);*/
1678
78.9k
      floor_depth = (opus_int32)SHR32(MULT16_32_Q15((C*bins<<BITRES),maxDepth), DB_SHIFT-15);
1679
78.9k
      floor_depth = IMAX(floor_depth, target>>2);
1680
78.9k
      target = IMIN(target, floor_depth);
1681
      /*printf("%f %d\n", maxDepth, floor_depth);*/
1682
78.9k
   }
1683
1684
   /* Make VBR less aggressive for constrained VBR because we can't keep a higher bitrate
1685
      for long. Needs tuning. */
1686
78.9k
   if ((!has_surround_mask||lfe) && constrained_vbr)
1687
29.3k
   {
1688
29.3k
      target = base_target + (opus_int32)MULT16_32_Q15(QCONST16(0.67f, 15), target-base_target);
1689
29.3k
   }
1690
1691
78.9k
   if (!has_surround_mask && tf_estimate < QCONST16(.2f, 14))
1692
22.4k
   {
1693
22.4k
      opus_val16 amount;
1694
22.4k
      opus_val16 tvbr_factor;
1695
22.4k
      amount = MULT16_16_Q15(QCONST16(.0000031f, 30), IMAX(0, IMIN(32000, 96000-bitrate)));
1696
22.4k
      tvbr_factor = SHR32(MULT16_16(SHR32(temporal_vbr, DB_SHIFT-10), amount), 10);
1697
22.4k
      target += (opus_int32)MULT16_32_Q15(tvbr_factor, target);
1698
22.4k
   }
1699
1700
   /* Don't allow more than doubling the rate */
1701
78.9k
   target = IMIN(2*base_target, target);
1702
1703
78.9k
   return target;
1704
78.9k
}
1705
1706
#ifdef ENABLE_QEXT
1707
0
static void encode_qext_stereo_params(ec_enc *ec, int qext_end, int qext_intensity, int qext_dual_stereo) {
1708
0
   ec_enc_uint(ec, qext_intensity, qext_end+1);
1709
0
   if (qext_intensity != 0) ec_enc_bit_logp(ec, qext_dual_stereo, 1);
1710
0
}
1711
#endif
1712
1713
int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_res * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
1714
195k
{
1715
195k
   int i, c, N;
1716
195k
   opus_int32 bits;
1717
195k
   ec_enc _enc;
1718
195k
   VARDECL(celt_sig, in);
1719
195k
   VARDECL(celt_sig, freq);
1720
195k
   VARDECL(celt_norm, X);
1721
195k
   VARDECL(celt_ener, bandE);
1722
195k
   VARDECL(celt_glog, bandLogE);
1723
195k
   VARDECL(celt_glog, bandLogE2);
1724
195k
   VARDECL(int, fine_quant);
1725
195k
   VARDECL(celt_glog, error);
1726
195k
   VARDECL(int, pulses);
1727
195k
   VARDECL(int, cap);
1728
195k
   VARDECL(int, offsets);
1729
195k
   VARDECL(int, importance);
1730
195k
   VARDECL(int, spread_weight);
1731
195k
   VARDECL(int, fine_priority);
1732
195k
   VARDECL(int, tf_res);
1733
195k
   VARDECL(unsigned char, collapse_masks);
1734
195k
   celt_sig *prefilter_mem;
1735
195k
   celt_glog *oldBandE, *oldLogE, *oldLogE2, *energyError;
1736
195k
   int shortBlocks=0;
1737
195k
   int isTransient=0;
1738
195k
   const int CC = st->channels;
1739
195k
   const int C = st->stream_channels;
1740
195k
   int LM, M;
1741
195k
   int tf_select;
1742
195k
   int nbFilledBytes, nbAvailableBytes;
1743
195k
   opus_int32 min_allowed;
1744
195k
   int start;
1745
195k
   int end;
1746
195k
   int effEnd;
1747
195k
   int codedBands;
1748
195k
   int alloc_trim;
1749
195k
   int pitch_index=COMBFILTER_MINPERIOD;
1750
195k
   opus_val16 gain1 = 0;
1751
195k
   int dual_stereo=0;
1752
195k
   int effectiveBytes;
1753
195k
   int dynalloc_logp;
1754
195k
   opus_int32 vbr_rate;
1755
195k
   opus_int32 total_bits;
1756
195k
   opus_int32 total_boost;
1757
195k
   opus_int32 balance;
1758
195k
   opus_int32 tell;
1759
195k
   opus_int32 tell0_frac;
1760
195k
   int prefilter_tapset=0;
1761
195k
   int pf_on;
1762
195k
   int anti_collapse_rsv;
1763
195k
   int anti_collapse_on=0;
1764
195k
   int silence=0;
1765
195k
   int tf_chan = 0;
1766
195k
   opus_val16 tf_estimate;
1767
195k
   int pitch_change=0;
1768
195k
   opus_int32 tot_boost;
1769
195k
   opus_val32 sample_max;
1770
195k
   celt_glog maxDepth;
1771
195k
   const OpusCustomMode *mode;
1772
195k
   int nbEBands;
1773
195k
   int overlap;
1774
195k
   const opus_int16 *eBands;
1775
195k
   int secondMdct;
1776
195k
   int signalBandwidth;
1777
195k
   int transient_got_disabled=0;
1778
195k
   celt_glog surround_masking=0;
1779
195k
   celt_glog temporal_vbr=0;
1780
195k
   celt_glog surround_trim = 0;
1781
195k
   opus_int32 equiv_rate;
1782
195k
   int hybrid;
1783
195k
   int weak_transient = 0;
1784
195k
   int enable_tf_analysis;
1785
195k
   opus_val16 tone_freq=-1;
1786
195k
   opus_val32 toneishness=0;
1787
195k
   VARDECL(celt_glog, surround_dynalloc);
1788
195k
   int qext_bytes=0;
1789
195k
   int packet_size_cap = 1275;
1790
#ifdef ENABLE_QEXT
1791
   int qext_scale;
1792
   int qext_end=0;
1793
   int qext_intensity=0;
1794
   int qext_dual_stereo=0;
1795
   int padding_len_bytes=0;
1796
   unsigned char *ext_payload;
1797
   opus_int32 qext_bits;
1798
   ec_enc ext_enc;
1799
   VARDECL(int, extra_quant);
1800
   VARDECL(int, extra_pulses);
1801
   VARDECL(celt_glog, error_bak);
1802
   const CELTMode *qext_mode = NULL;
1803
118k
   CELTMode qext_mode_struct;
1804
   celt_ener qext_bandE[2*NB_QEXT_BANDS];
1805
   celt_glog qext_bandLogE[2*NB_QEXT_BANDS];
1806
   celt_glog *qext_oldBandE=NULL;
1807
   celt_glog qext_error[2*NB_QEXT_BANDS];
1808
#endif
1809
195k
   ALLOC_STACK;
1810
1811
195k
   mode = st->mode;
1812
195k
   nbEBands = mode->nbEBands;
1813
195k
   overlap = mode->overlap;
1814
195k
   eBands = mode->eBands;
1815
195k
   start = st->start;
1816
195k
   end = st->end;
1817
195k
   hybrid = start != 0;
1818
195k
   tf_estimate = 0;
1819
195k
   if (nbCompressedBytes<2 || pcm==NULL)
1820
0
   {
1821
0
      RESTORE_STACK;
1822
0
      return OPUS_BAD_ARG;
1823
0
   }
1824
1825
195k
   frame_size *= st->upsample;
1826
501k
   for (LM=0;LM<=mode->maxLM;LM++)
1827
501k
      if (mode->shortMdctSize<<LM==frame_size)
1828
195k
         break;
1829
195k
   if (LM>mode->maxLM)
1830
0
   {
1831
0
      RESTORE_STACK;
1832
0
      return OPUS_BAD_ARG;
1833
0
   }
1834
195k
   M=1<<LM;
1835
195k
   N = M*mode->shortMdctSize;
1836
1837
#ifdef ENABLE_QEXT
1838
   qext_scale = st->qext_scale;
1839
118k
   if (st->enable_qext) packet_size_cap = QEXT_PACKET_SIZE_CAP;
1840
#endif
1841
1842
195k
   prefilter_mem = st->in_mem+CC*(overlap);
1843
195k
   oldBandE = (celt_glog*)(st->in_mem+CC*(overlap+QEXT_SCALE(COMBFILTER_MAXPERIOD)));
1844
195k
   oldLogE = oldBandE + CC*nbEBands;
1845
195k
   oldLogE2 = oldLogE + CC*nbEBands;
1846
195k
   energyError = oldLogE2 + CC*nbEBands;
1847
1848
195k
   if (enc==NULL)
1849
0
   {
1850
0
      tell0_frac=tell=1;
1851
0
      nbFilledBytes=0;
1852
195k
   } else {
1853
195k
      tell0_frac=ec_tell_frac(enc);
1854
195k
      tell=ec_tell(enc);
1855
195k
      nbFilledBytes=(tell+4)>>3;
1856
195k
   }
1857
1858
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
1859
   if (st->signalling && enc==NULL)
1860
   {
1861
      int tmp = (mode->effEBands-end)>>1;
1862
      end = st->end = IMAX(1, mode->effEBands-tmp);
1863
      compressed[0] = tmp<<5;
1864
      compressed[0] |= LM<<3;
1865
      compressed[0] |= (C==2)<<2;
1866
      /* Convert "standard mode" to Opus header */
1867
# ifndef ENABLE_QEXT
1868
      if (mode->Fs==48000 && mode->shortMdctSize==120)
1869
# endif
1870
      {
1871
         int c0 = toOpus(compressed[0]);
1872
         if (c0<0)
1873
         {
1874
            RESTORE_STACK;
1875
            return OPUS_BAD_ARG;
1876
         }
1877
         compressed[0] = c0;
1878
      }
1879
      compressed++;
1880
      nbCompressedBytes--;
1881
   }
1882
#else
1883
195k
   celt_assert(st->signalling==0);
1884
195k
#endif
1885
1886
   /* Can't produce more than 1275 output bytes for the main payload, plus any QEXT extra data. */
1887
195k
   nbCompressedBytes = IMIN(nbCompressedBytes,packet_size_cap);
1888
1889
195k
   if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1890
142k
   {
1891
142k
      opus_int32 den=mode->Fs>>BITRES;
1892
142k
      vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1893
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
1894
      if (st->signalling)
1895
         vbr_rate -= 8<<BITRES;
1896
#endif
1897
142k
      effectiveBytes = vbr_rate>>(3+BITRES);
1898
142k
   } else {
1899
52.9k
      opus_int32 tmp;
1900
52.9k
      vbr_rate = 0;
1901
52.9k
      tmp = st->bitrate*frame_size;
1902
52.9k
      if (tell>1)
1903
3.22k
         tmp += tell*mode->Fs;
1904
52.9k
      if (st->bitrate!=OPUS_BITRATE_MAX)
1905
0
      {
1906
0
         nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1907
0
               (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling));
1908
0
         if (enc != NULL)
1909
0
            ec_enc_shrink(enc, nbCompressedBytes);
1910
0
      }
1911
52.9k
      effectiveBytes = nbCompressedBytes - nbFilledBytes;
1912
52.9k
   }
1913
195k
   nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1914
195k
   equiv_rate = ((opus_int32)nbCompressedBytes*8*50 << (3-LM)) - (40*C+20)*((400>>LM) - 50);
1915
195k
   if (st->bitrate != OPUS_BITRATE_MAX)
1916
142k
      equiv_rate = IMIN(equiv_rate, st->bitrate - (40*C+20)*((400>>LM) - 50));
1917
1918
195k
   if (enc==NULL)
1919
0
   {
1920
0
      ec_enc_init(&_enc, compressed, nbCompressedBytes);
1921
0
      enc = &_enc;
1922
0
   }
1923
1924
195k
   if (vbr_rate>0)
1925
142k
   {
1926
      /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1927
          target rate and buffering.
1928
         We must do this up front so that bust-prevention logic triggers
1929
          correctly if we don't have enough bits. */
1930
142k
      if (st->constrained_vbr)
1931
56.9k
      {
1932
56.9k
         opus_int32 vbr_bound;
1933
56.9k
         opus_int32 max_allowed;
1934
         /* We could use any multiple of vbr_rate as bound (depending on the
1935
             delay).
1936
            This is clamped to ensure we use at least two bytes if the encoder
1937
             was entirely empty, but to allow 0 in hybrid mode. */
1938
56.9k
         vbr_bound = vbr_rate;
1939
56.9k
         max_allowed = IMIN(IMAX(tell==1?2:0,
1940
56.9k
               (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1941
56.9k
               nbAvailableBytes);
1942
56.9k
         if(max_allowed < nbAvailableBytes)
1943
34.2k
         {
1944
34.2k
            nbCompressedBytes = nbFilledBytes+max_allowed;
1945
34.2k
            nbAvailableBytes = max_allowed;
1946
34.2k
            ec_enc_shrink(enc, nbCompressedBytes);
1947
34.2k
         }
1948
56.9k
      }
1949
142k
   }
1950
195k
   total_bits = nbCompressedBytes*8;
1951
1952
195k
   effEnd = end;
1953
195k
   if (effEnd > mode->effEBands)
1954
0
      effEnd = mode->effEBands;
1955
1956
195k
   ALLOC(in, CC*(N+overlap), celt_sig);
1957
1958
195k
   sample_max=MAX32(st->overlap_max, celt_maxabs_res(pcm, C*(N-overlap)/st->upsample));
1959
195k
   st->overlap_max=celt_maxabs_res(pcm+C*(N-overlap)/st->upsample, C*overlap/st->upsample);
1960
195k
   sample_max=MAX32(sample_max, st->overlap_max);
1961
#ifdef FIXED_POINT
1962
   silence = (sample_max==0);
1963
#else
1964
   silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth));
1965
#endif
1966
#ifdef FUZZING
1967
   if ((rand()&0x3F)==0)
1968
      silence = 1;
1969
#endif
1970
195k
   if (tell==1)
1971
184k
      ec_enc_bit_logp(enc, silence, 15);
1972
11.5k
   else
1973
11.5k
      silence=0;
1974
195k
   if (silence)
1975
39.0k
   {
1976
      /*In VBR mode there is no need to send more than the minimum. */
1977
39.0k
      if (vbr_rate>0)
1978
32.1k
      {
1979
32.1k
         effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1980
32.1k
         total_bits=nbCompressedBytes*8;
1981
32.1k
         nbAvailableBytes=2;
1982
32.1k
         ec_enc_shrink(enc, nbCompressedBytes);
1983
32.1k
      }
1984
#ifdef ENABLE_QEXT
1985
3.73k
      else if (st->enable_qext) {
1986
0
         nbCompressedBytes = IMIN(nbCompressedBytes, 1275);
1987
0
         nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1988
0
         total_bits = nbCompressedBytes*8;
1989
0
         ec_enc_shrink(enc, nbCompressedBytes);
1990
0
      }
1991
#endif
1992
      /* Pretend we've filled all the remaining bits with zeros
1993
            (that's what the initialiser did anyway) */
1994
39.0k
      tell = nbCompressedBytes*8;
1995
39.0k
      enc->nbits_total+=tell-ec_tell(enc);
1996
39.0k
   }
1997
295k
   c=0; do {
1998
295k
      int need_clip=0;
1999
#ifndef FIXED_POINT
2000
167k
      need_clip = st->clip && sample_max>65536.f;
2001
#endif
2002
295k
      celt_preemphasis(pcm+c, in+c*(N+overlap)+overlap, N, CC, st->upsample,
2003
295k
                  mode->preemph, st->preemph_memE+c, need_clip);
2004
295k
      OPUS_COPY(in+c*(N+overlap), &prefilter_mem[(1+c)*QEXT_SCALE(COMBFILTER_MAXPERIOD)-overlap], overlap);
2005
295k
   } while (++c<CC);
2006
2007
2008
195k
   tone_freq = tone_detect(in, CC, N+overlap, &toneishness, mode->Fs);
2009
195k
   isTransient = 0;
2010
195k
   shortBlocks = 0;
2011
195k
   if (st->complexity >= 1 && !st->lfe)
2012
175k
   {
2013
      /* Reduces the likelihood of energy instability on fricatives at low bitrate
2014
         in hybrid mode. It seems like we still want to have real transients on vowels
2015
         though (small SILK quantization offset value). */
2016
175k
      int allow_weak_transients = hybrid && effectiveBytes<15 && st->silk_info.signalType != 2;
2017
175k
      isTransient = transient_analysis(in, N+overlap, CC,
2018
175k
            &tf_estimate, &tf_chan, allow_weak_transients, &weak_transient, tone_freq, toneishness);
2019
175k
   }
2020
   /* Find pitch period and gain */
2021
195k
   {
2022
195k
      int enabled;
2023
195k
      int qg;
2024
195k
      enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && !hybrid && !silence && tell+16<=total_bits && !st->disable_pf;
2025
2026
195k
      prefilter_tapset = st->tapset_decision;
2027
195k
      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));
2028
195k
      if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && (!st->analysis.valid || st->analysis.tonality > .3)
2029
195k
            && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st->prefilter_period))
2030
1.37k
         pitch_change = 1;
2031
195k
      if (pf_on==0)
2032
193k
      {
2033
193k
         if(!hybrid && tell+16<=total_bits)
2034
142k
            ec_enc_bit_logp(enc, 0, 1);
2035
193k
      } else {
2036
         /*This block is not gated by a total bits check only because
2037
           of the nbAvailableBytes check above.*/
2038
2.47k
         int octave;
2039
2.47k
         ec_enc_bit_logp(enc, 1, 1);
2040
2.47k
         pitch_index += 1;
2041
2.47k
         octave = EC_ILOG(pitch_index)-5;
2042
2.47k
         ec_enc_uint(enc, octave, 6);
2043
2.47k
         ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
2044
2.47k
         pitch_index -= 1;
2045
2.47k
         ec_enc_bits(enc, qg, 3);
2046
2.47k
         ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
2047
2.47k
      }
2048
195k
   }
2049
195k
   if (LM>0 && ec_tell(enc)+3<=total_bits)
2050
98.5k
   {
2051
98.5k
      if (isTransient)
2052
71.7k
         shortBlocks = M;
2053
98.5k
   } else {
2054
97.3k
      isTransient = 0;
2055
97.3k
      transient_got_disabled=1;
2056
97.3k
   }
2057
2058
195k
   ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
2059
195k
   ALLOC(bandE,nbEBands*CC, celt_ener);
2060
195k
   ALLOC(bandLogE,nbEBands*CC, celt_glog);
2061
2062
195k
   secondMdct = shortBlocks && st->complexity>=8;
2063
195k
   ALLOC(bandLogE2, C*nbEBands, celt_glog);
2064
195k
   if (secondMdct)
2065
43.7k
   {
2066
43.7k
      compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample, st->arch);
2067
43.7k
      compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
2068
43.7k
      amp2Log2(mode, effEnd, end, bandE, bandLogE2, C);
2069
109k
      for (c=0;c<C;c++)
2070
66.0k
      {
2071
1.06M
         for (i=0;i<end;i++)
2072
998k
            bandLogE2[nbEBands*c+i] += HALF32(SHL32(LM, DB_SHIFT));
2073
66.0k
      }
2074
43.7k
   }
2075
2076
195k
   compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
2077
   /* This should catch any NaN in the CELT input. Since we're not supposed to see any (they're filtered
2078
      at the Opus layer), just abort. */
2079
195k
   celt_assert(!celt_isnan(freq[0]) && (C==1 || !celt_isnan(freq[N])));
2080
195k
   if (CC==2&&C==1)
2081
24.5k
      tf_chan = 0;
2082
195k
   compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
2083
2084
195k
   if (st->lfe)
2085
921
   {
2086
11.0k
      for (i=2;i<end;i++)
2087
10.1k
      {
2088
10.1k
         bandE[i] = IMIN(bandE[i], MULT16_32_Q15(QCONST16(1e-4f,15),bandE[0]));
2089
10.1k
         bandE[i] = MAX32(bandE[i], EPSILON);
2090
10.1k
      }
2091
921
   }
2092
195k
   amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
2093
2094
195k
   ALLOC(surround_dynalloc, C*nbEBands, celt_glog);
2095
195k
   OPUS_CLEAR(surround_dynalloc, end);
2096
   /* This computes how much masking takes place between surround channels */
2097
195k
   if (!hybrid&&st->energy_mask&&!st->lfe)
2098
7.97k
   {
2099
7.97k
      int mask_end;
2100
7.97k
      int midband;
2101
7.97k
      int count_dynalloc;
2102
7.97k
      opus_val32 mask_avg=0;
2103
7.97k
      opus_val32 diff=0;
2104
7.97k
      int count=0;
2105
7.97k
      mask_end = IMAX(2,st->lastCodedBands);
2106
22.7k
      for (c=0;c<C;c++)
2107
14.7k
      {
2108
117k
         for(i=0;i<mask_end;i++)
2109
102k
         {
2110
102k
            celt_glog mask;
2111
102k
            opus_val16 mask16;
2112
102k
            mask = MAXG(MING(st->energy_mask[nbEBands*c+i],
2113
102k
                   GCONST(.25f)), -GCONST(2.0f));
2114
102k
            if (mask > 0)
2115
62.4k
               mask = HALF32(mask);
2116
102k
            mask16 = SHR32(mask, DB_SHIFT-10);
2117
102k
            mask_avg += MULT16_16(mask16, eBands[i+1]-eBands[i]);
2118
102k
            count += eBands[i+1]-eBands[i];
2119
102k
            diff += MULT16_16(mask16, 1+2*i-mask_end);
2120
102k
         }
2121
14.7k
      }
2122
7.97k
      celt_assert(count>0);
2123
7.97k
      mask_avg = SHL32(DIV32_16(mask_avg,count), DB_SHIFT-10);
2124
7.97k
      mask_avg += GCONST(.2f);
2125
7.97k
      diff = SHL32(diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end), DB_SHIFT-10);
2126
      /* Again, being conservative */
2127
7.97k
      diff = HALF32(diff);
2128
7.97k
      diff = MAX32(MIN32(diff, GCONST(.031f)), -GCONST(.031f));
2129
      /* Find the band that's in the middle of the coded spectrum */
2130
35.8k
      for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++);
2131
7.97k
      count_dynalloc=0;
2132
62.5k
      for(i=0;i<mask_end;i++)
2133
54.5k
      {
2134
54.5k
         opus_val32 lin;
2135
54.5k
         celt_glog unmask;
2136
54.5k
         lin = mask_avg + diff*(i-midband);
2137
54.5k
         if (C==2)
2138
48.1k
            unmask = MAXG(st->energy_mask[i], st->energy_mask[nbEBands+i]);
2139
6.42k
         else
2140
6.42k
            unmask = st->energy_mask[i];
2141
54.5k
         unmask = MING(unmask, GCONST(.0f));
2142
54.5k
         unmask -= lin;
2143
54.5k
         if (unmask > GCONST(.25f))
2144
7.17k
         {
2145
7.17k
            surround_dynalloc[i] = unmask - GCONST(.25f);
2146
7.17k
            count_dynalloc++;
2147
7.17k
         }
2148
54.5k
      }
2149
7.97k
      if (count_dynalloc>=3)
2150
664
      {
2151
         /* If we need dynalloc in many bands, it's probably because our
2152
            initial masking rate was too low. */
2153
664
         mask_avg += GCONST(.25f);
2154
664
         if (mask_avg>0)
2155
67
         {
2156
            /* Something went really wrong in the original calculations,
2157
               disabling masking. */
2158
67
            mask_avg = 0;
2159
67
            diff = 0;
2160
67
            OPUS_CLEAR(surround_dynalloc, mask_end);
2161
597
         } else {
2162
7.87k
            for(i=0;i<mask_end;i++)
2163
7.27k
               surround_dynalloc[i] = MAXG(0, surround_dynalloc[i]-GCONST(.25f));
2164
597
         }
2165
664
      }
2166
7.97k
      mask_avg += GCONST(.2f);
2167
      /* Convert to 1/64th units used for the trim */
2168
7.97k
      surround_trim = 64*diff;
2169
      /*printf("%d %d ", mask_avg, surround_trim);*/
2170
7.97k
      surround_masking = mask_avg;
2171
7.97k
   }
2172
   /* Temporal VBR (but not for LFE) */
2173
195k
   if (!st->lfe)
2174
195k
   {
2175
195k
      celt_glog follow=-QCONST32(10.0f, DB_SHIFT-5);
2176
195k
      opus_val32 frame_avg=0;
2177
195k
      celt_glog offset = shortBlocks?HALF32(SHL32(LM, DB_SHIFT-5)):0;
2178
2.89M
      for(i=start;i<end;i++)
2179
2.70M
      {
2180
2.70M
         follow = MAXG(follow-QCONST32(1.0f, DB_SHIFT-5), SHR32(bandLogE[i],5)-offset);
2181
2.70M
         if (C==2)
2182
1.03M
            follow = MAXG(follow, SHR32(bandLogE[i+nbEBands],5)-offset);
2183
2.70M
         frame_avg += follow;
2184
2.70M
      }
2185
195k
      frame_avg /= (end-start);
2186
195k
      temporal_vbr = SUB32(SHL32(frame_avg, 5),st->spec_avg);
2187
195k
      temporal_vbr = MING(GCONST(3.f), MAXG(-GCONST(1.5f), temporal_vbr));
2188
195k
      st->spec_avg += MULT16_32_Q15(QCONST16(.02f, 15), temporal_vbr);
2189
195k
   }
2190
   /*for (i=0;i<21;i++)
2191
      printf("%f ", bandLogE[i]);
2192
   printf("\n");*/
2193
2194
195k
   if (!secondMdct)
2195
152k
   {
2196
152k
      OPUS_COPY(bandLogE2, bandLogE, C*nbEBands);
2197
152k
   }
2198
2199
   /* Last chance to catch any transient we might have missed in the
2200
      time-domain analysis */
2201
195k
   if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5 && !st->lfe && !hybrid)
2202
13.6k
   {
2203
13.6k
      if (patch_transient_decision(bandLogE, oldBandE, nbEBands, start, end, C))
2204
380
      {
2205
380
         isTransient = 1;
2206
380
         shortBlocks = M;
2207
380
         compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
2208
380
         compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
2209
380
         amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
2210
         /* Compensate for the scaling of short vs long mdcts */
2211
922
         for (c=0;c<C;c++)
2212
542
         {
2213
8.26k
            for (i=0;i<end;i++)
2214
7.71k
               bandLogE2[nbEBands*c+i] += HALF32(SHL32(LM, DB_SHIFT));
2215
542
         }
2216
380
         tf_estimate = QCONST16(.2f,14);
2217
380
      }
2218
13.6k
   }
2219
2220
195k
   if (LM>0 && ec_tell(enc)+3<=total_bits)
2221
98.5k
      ec_enc_bit_logp(enc, isTransient, 3);
2222
2223
195k
   ALLOC(X, C*N, celt_norm);         /**< Interleaved normalised MDCTs */
2224
2225
   /* Band normalisation */
2226
195k
   normalise_bands(mode, freq, X, bandE, effEnd, C, M);
2227
2228
195k
   enable_tf_analysis = effectiveBytes>=15*C && !hybrid && st->complexity>=2 && !st->lfe && toneishness < QCONST32(.98f, 29);
2229
2230
195k
   ALLOC(offsets, nbEBands, int);
2231
195k
   ALLOC(importance, nbEBands, int);
2232
195k
   ALLOC(spread_weight, nbEBands, int);
2233
2234
195k
   maxDepth = dynalloc_analysis(bandLogE, bandLogE2, oldBandE, nbEBands, start, end, C, offsets,
2235
195k
         st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr,
2236
195k
         eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc, &st->analysis, importance, spread_weight, tone_freq, toneishness ARG_QEXT(qext_scale));
2237
2238
195k
   ALLOC(tf_res, nbEBands, int);
2239
   /* Disable variable tf resolution for hybrid and at very low bitrate */
2240
195k
   if (enable_tf_analysis)
2241
94.2k
   {
2242
94.2k
      int lambda;
2243
94.2k
      lambda = IMAX(80, 20480/effectiveBytes + 2);
2244
94.2k
      tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, LM, tf_estimate, tf_chan, importance);
2245
94.2k
      for (i=effEnd;i<end;i++)
2246
0
         tf_res[i] = tf_res[effEnd-1];
2247
101k
   } else if (hybrid && weak_transient)
2248
186
   {
2249
      /* For weak transients, we rely on the fact that improving time resolution using
2250
         TF on a long window is imperfect and will not result in an energy collapse at
2251
         low bitrate. */
2252
3.72k
      for (i=0;i<end;i++)
2253
3.53k
         tf_res[i] = 1;
2254
186
      tf_select=0;
2255
101k
   } else if (hybrid && effectiveBytes<15 && st->silk_info.signalType != 2)
2256
2.51k
   {
2257
      /* For low bitrate hybrid, we force temporal resolution to 5 ms rather than 2.5 ms. */
2258
50.2k
      for (i=0;i<end;i++)
2259
47.7k
         tf_res[i] = 0;
2260
2.51k
      tf_select=isTransient;
2261
99.0k
   } else {
2262
1.54M
      for (i=0;i<end;i++)
2263
1.44M
         tf_res[i] = isTransient;
2264
99.0k
      tf_select=0;
2265
99.0k
   }
2266
2267
195k
   ALLOC(error, C*nbEBands, celt_glog);
2268
195k
   c=0;
2269
271k
   do {
2270
4.01M
      for (i=start;i<end;i++)
2271
3.74M
      {
2272
         /* When the energy is stable, slightly bias energy quantization towards
2273
            the previous error to make the gain more stable (a constant offset is
2274
            better than fluctuations). */
2275
3.74M
         if (ABS32(SUB32(bandLogE[i+c*nbEBands], oldBandE[i+c*nbEBands])) < GCONST(2.f))
2276
959k
         {
2277
959k
            bandLogE[i+c*nbEBands] -= MULT16_32_Q15(QCONST16(0.25f, 15), energyError[i+c*nbEBands]);
2278
959k
         }
2279
3.74M
      }
2280
271k
   } while (++c < C);
2281
195k
   quant_coarse_energy(mode, start, end, effEnd, bandLogE,
2282
195k
         oldBandE, total_bits, error, enc,
2283
195k
         C, LM, nbAvailableBytes, st->force_intra,
2284
195k
         &st->delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
2285
2286
195k
   tf_encode(start, end, isTransient, tf_res, LM, tf_select, enc);
2287
2288
195k
   if (ec_tell(enc)+4<=total_bits)
2289
154k
   {
2290
154k
      if (st->lfe)
2291
769
      {
2292
769
         st->tapset_decision = 0;
2293
769
         st->spread_decision = SPREAD_NORMAL;
2294
154k
      } else if (hybrid)
2295
11.5k
      {
2296
11.5k
         if (st->complexity == 0)
2297
1.19k
            st->spread_decision = SPREAD_NONE;
2298
10.3k
         else if (isTransient)
2299
8.24k
            st->spread_decision = SPREAD_NORMAL;
2300
2.06k
         else
2301
2.06k
            st->spread_decision = SPREAD_AGGRESSIVE;
2302
142k
      } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
2303
86.6k
      {
2304
86.6k
         if (st->complexity == 0)
2305
14.4k
            st->spread_decision = SPREAD_NONE;
2306
72.2k
         else
2307
72.2k
            st->spread_decision = SPREAD_NORMAL;
2308
86.6k
      } else {
2309
         /* Disable new spreading+tapset estimator until we can show it works
2310
            better than the old one. So far it seems like spreading_decision()
2311
            works best. */
2312
#if 0
2313
         if (st->analysis.valid)
2314
         {
2315
            static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
2316
            static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
2317
            static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), QCONST16(.15f, 15)};
2318
            static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), QCONST16(.05f, 15)};
2319
            st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision);
2320
            st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
2321
         } else
2322
#endif
2323
55.8k
         {
2324
55.8k
            st->spread_decision = spreading_decision(mode, X,
2325
55.8k
                  &st->tonal_average, st->spread_decision, &st->hf_average,
2326
55.8k
                  &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M, spread_weight);
2327
55.8k
         }
2328
         /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
2329
         /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
2330
55.8k
      }
2331
154k
      ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
2332
154k
   } else {
2333
41.1k
      st->spread_decision = SPREAD_NORMAL;
2334
41.1k
   }
2335
2336
   /* For LFE, everything interesting is in the first band */
2337
195k
   if (st->lfe)
2338
921
      offsets[0] = IMIN(8, effectiveBytes/3);
2339
195k
   ALLOC(cap, nbEBands, int);
2340
195k
   init_caps(mode,cap,LM,C);
2341
2342
195k
   dynalloc_logp = 6;
2343
195k
   total_bits<<=BITRES;
2344
195k
   total_boost = 0;
2345
195k
   tell = ec_tell_frac(enc);
2346
2.90M
   for (i=start;i<end;i++)
2347
2.71M
   {
2348
2.71M
      int width, quanta;
2349
2.71M
      int dynalloc_loop_logp;
2350
2.71M
      int boost;
2351
2.71M
      int j;
2352
2.71M
      width = C*(eBands[i+1]-eBands[i])<<LM;
2353
      /* quanta is 6 bits, but no more than 1 bit/sample
2354
         and no less than 1/8 bit/sample */
2355
2.71M
      quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
2356
2.71M
      dynalloc_loop_logp = dynalloc_logp;
2357
2.71M
      boost = 0;
2358
2.96M
      for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
2359
2.96M
            && boost < cap[i]; j++)
2360
2.34M
      {
2361
2.34M
         int flag;
2362
2.34M
         flag = j<offsets[i];
2363
2.34M
         ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
2364
2.34M
         tell = ec_tell_frac(enc);
2365
2.34M
         if (!flag)
2366
2.08M
            break;
2367
256k
         boost += quanta;
2368
256k
         total_boost += quanta;
2369
256k
         dynalloc_loop_logp = 1;
2370
256k
      }
2371
      /* Making dynalloc more likely */
2372
2.71M
      if (j)
2373
106k
         dynalloc_logp = IMAX(2, dynalloc_logp-1);
2374
2.71M
      offsets[i] = boost;
2375
2.71M
   }
2376
2377
195k
   if (C==2)
2378
75.2k
   {
2379
75.2k
      static const opus_val16 intensity_thresholds[21]=
2380
      /* 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19  20  off*/
2381
75.2k
        {  1, 2, 3, 4, 5, 6, 7, 8,16,24,36,44,50,56,62,67,72,79,88,106,134};
2382
75.2k
      static const opus_val16 intensity_histeresis[21]=
2383
75.2k
        {  1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6,  8, 8};
2384
2385
      /* Always use MS for 2.5 ms frames until we can do a better analysis */
2386
75.2k
      if (LM!=0)
2387
54.4k
         dual_stereo = stereo_analysis(mode, X, LM, N);
2388
2389
75.2k
      st->intensity = hysteresis_decision((opus_val16)(equiv_rate/1000),
2390
75.2k
            intensity_thresholds, intensity_histeresis, 21, st->intensity);
2391
75.2k
      st->intensity = IMIN(end,IMAX(start, st->intensity));
2392
75.2k
   }
2393
2394
195k
   alloc_trim = 5;
2395
195k
   if (tell+(6<<BITRES) <= total_bits - total_boost)
2396
152k
   {
2397
152k
      if (start > 0 || st->lfe)
2398
12.0k
      {
2399
12.0k
         st->stereo_saving = 0;
2400
12.0k
         alloc_trim = 5;
2401
140k
      } else {
2402
140k
         alloc_trim = alloc_trim_analysis(mode, X, bandLogE,
2403
140k
            end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate,
2404
140k
            st->intensity, surround_trim, equiv_rate, st->arch);
2405
140k
      }
2406
152k
      ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
2407
152k
      tell = ec_tell_frac(enc);
2408
152k
   }
2409
2410
   /* In VBR mode the frame size must not be reduced so much that it would
2411
       result in the encoder running out of bits.
2412
      The margin of 2 bytes ensures that none of the bust-prevention logic
2413
       in the decoder will have triggered so far. */
2414
195k
   min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2;
2415
   /* Take into account the 37 bits we need to have left in the packet to
2416
      signal a redundant frame in hybrid mode. Creating a shorter packet would
2417
      create an entropy coder desync. */
2418
195k
   if (hybrid)
2419
11.5k
      min_allowed = IMAX(min_allowed, (tell0_frac+(37<<BITRES)+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3));
2420
   /* Variable bitrate */
2421
195k
   if (vbr_rate>0)
2422
142k
   {
2423
142k
     opus_val16 alpha;
2424
142k
     opus_int32 delta;
2425
     /* The target rate in 8th bits per frame */
2426
142k
     opus_int32 target, base_target;
2427
142k
     int lm_diff = mode->maxLM - LM;
2428
2429
     /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
2430
        The CELT allocator will just not be able to use more than that anyway. */
2431
142k
     nbCompressedBytes = IMIN(nbCompressedBytes,packet_size_cap>>(3-LM));
2432
142k
     if (!hybrid)
2433
134k
     {
2434
134k
        base_target = vbr_rate - ((40*C+20)<<BITRES);
2435
134k
     } else {
2436
8.37k
        base_target = IMAX(0, vbr_rate - ((9*C+4)<<BITRES));
2437
8.37k
     }
2438
2439
142k
     if (st->constrained_vbr)
2440
56.9k
        base_target += (st->vbr_offset>>lm_diff);
2441
2442
142k
     if (!hybrid)
2443
134k
     {
2444
134k
        target = compute_vbr(mode, &st->analysis, base_target, LM, equiv_rate,
2445
134k
           st->lastCodedBands, C, st->intensity, st->constrained_vbr,
2446
134k
           st->stereo_saving, tot_boost, tf_estimate, pitch_change, maxDepth,
2447
134k
           st->lfe, st->energy_mask!=NULL, surround_masking,
2448
134k
           temporal_vbr ARG_QEXT(st->enable_qext));
2449
134k
     } else {
2450
8.37k
        target = base_target;
2451
        /* Tonal frames (offset<100) need more bits than noisy (offset>100) ones. */
2452
8.37k
        if (st->silk_info.offset < 100) target += 12 << BITRES >> (3-LM);
2453
8.37k
        if (st->silk_info.offset > 100) target -= 18 << BITRES >> (3-LM);
2454
        /* Boosting bitrate on transients and vowels with significant temporal
2455
           spikes. */
2456
8.37k
        target += (opus_int32)MULT16_16_Q14(tf_estimate-QCONST16(.25f,14), (50<<BITRES));
2457
        /* If we have a strong transient, let's make sure it has enough bits to code
2458
           the first two bands, so that it can use folding rather than noise. */
2459
8.37k
        if (tf_estimate > QCONST16(.7f,14))
2460
5.80k
           target = IMAX(target, 50<<BITRES);
2461
8.37k
     }
2462
     /* The current offset is removed from the target and the space used
2463
        so far is added*/
2464
142k
     target=target+tell;
2465
2466
142k
     nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
2467
142k
     nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
2468
142k
     nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes);
2469
2470
     /* By how much did we "miss" the target on that frame */
2471
142k
     delta = target - vbr_rate;
2472
2473
142k
     target=nbAvailableBytes<<(BITRES+3);
2474
2475
     /*If the frame is silent we don't adjust our drift, otherwise
2476
       the encoder will shoot to very high rates after hitting a
2477
       span of silence, but we do allow the bitres to refill.
2478
       This means that we'll undershoot our target in CVBR/VBR modes
2479
       on files with lots of silence. */
2480
142k
     if(silence)
2481
32.1k
     {
2482
32.1k
       nbAvailableBytes = 2;
2483
32.1k
       target = 2*8<<BITRES;
2484
32.1k
       delta = 0;
2485
32.1k
     }
2486
2487
142k
     if (st->vbr_count < 970)
2488
142k
     {
2489
142k
        st->vbr_count++;
2490
142k
        alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
2491
142k
     } else
2492
0
        alpha = QCONST16(.001f,15);
2493
     /* How many bits have we used in excess of what we're allowed */
2494
142k
     if (st->constrained_vbr)
2495
56.9k
        st->vbr_reservoir += target - vbr_rate;
2496
     /*printf ("%d\n", st->vbr_reservoir);*/
2497
2498
     /* Compute the offset we need to apply in order to reach the target */
2499
142k
     if (st->constrained_vbr)
2500
56.9k
     {
2501
56.9k
        st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
2502
56.9k
        st->vbr_offset = -st->vbr_drift;
2503
56.9k
     }
2504
     /*printf ("%d\n", st->vbr_drift);*/
2505
2506
142k
     if (st->constrained_vbr && st->vbr_reservoir < 0)
2507
38.1k
     {
2508
        /* We're under the min value -- increase rate */
2509
38.1k
        int adjust = (-st->vbr_reservoir)/(8<<BITRES);
2510
        /* Unless we're just coding silence */
2511
38.1k
        nbAvailableBytes += silence?0:adjust;
2512
38.1k
        st->vbr_reservoir = 0;
2513
        /*printf ("+%d\n", adjust);*/
2514
38.1k
     }
2515
142k
     nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes);
2516
     /*printf("%d\n", nbCompressedBytes*50*8);*/
2517
     /* This moves the raw bits to take into account the new compressed size */
2518
142k
     ec_enc_shrink(enc, nbCompressedBytes);
2519
142k
   }
2520
#ifdef ENABLE_QEXT
2521
118k
   if (st->enable_qext) {
2522
0
      int new_compressedBytes;
2523
      /* Don't give any bits for the first 80 kb/s per channel. Then 80% of the excess. */
2524
0
      opus_int32 offset = C*80000*frame_size/mode->Fs/8;
2525
0
      qext_bytes = IMAX(nbCompressedBytes-1275, IMAX(0, (nbCompressedBytes-offset)*4/5));
2526
0
      padding_len_bytes = (qext_bytes+253)/254;
2527
0
      qext_bytes = IMIN(qext_bytes, nbCompressedBytes-min_allowed-padding_len_bytes-1);
2528
0
      padding_len_bytes = (qext_bytes+253)/254;
2529
0
      if (qext_bytes > 20) {
2530
0
         new_compressedBytes = nbCompressedBytes-qext_bytes-padding_len_bytes-1;
2531
0
         ec_enc_shrink(enc, new_compressedBytes);
2532
0
         if (compressed == NULL) {
2533
0
            compressed = enc->buf;
2534
0
         }
2535
0
         compressed[-1] |= 0x03; /* Code 3 packet */
2536
0
         enc->buf += 1+padding_len_bytes;
2537
0
         OPUS_MOVE(compressed+1+padding_len_bytes, compressed, new_compressedBytes);
2538
0
         compressed[0] = 0x41; /* Set padding */
2539
0
         for (i=0;i<padding_len_bytes-1;i++) compressed[i+1] = 255;
2540
0
         compressed[padding_len_bytes] = qext_bytes%254 == 0 ? 254 : qext_bytes%254;
2541
0
         ext_payload = compressed+padding_len_bytes+1+new_compressedBytes;
2542
0
         ext_payload[0] = QEXT_EXTENSION_ID<<1;
2543
0
         ext_payload += 1;
2544
0
         qext_bytes -= 1;
2545
0
         OPUS_CLEAR(ext_payload, qext_bytes);
2546
0
         ec_enc_init(&ext_enc, ext_payload, qext_bytes);
2547
0
         nbCompressedBytes = new_compressedBytes;
2548
0
         if (end == nbEBands && (mode->Fs == 48000 ||  mode->Fs == 96000) && (mode->shortMdctSize==120*qext_scale || mode->shortMdctSize==90*qext_scale)) {
2549
0
            compute_qext_mode(&qext_mode_struct, mode);
2550
0
            qext_mode = &qext_mode_struct;
2551
0
            qext_end = (qext_scale == 2) ? NB_QEXT_BANDS : 2;
2552
0
            ec_enc_bit_logp(&ext_enc, qext_end == NB_QEXT_BANDS, 1);
2553
0
         }
2554
0
      } else {
2555
0
         ec_enc_init(&ext_enc, NULL, 0);
2556
0
         qext_bytes = 0;
2557
0
      }
2558
118k
   } else {
2559
118k
      ec_enc_init(&ext_enc, NULL, 0);
2560
118k
   }
2561
#endif
2562
2563
   /* Bit allocation */
2564
195k
   ALLOC(fine_quant, nbEBands, int);
2565
195k
   ALLOC(pulses, nbEBands, int);
2566
195k
   ALLOC(fine_priority, nbEBands, int);
2567
2568
   /* bits =           packet size                    - where we are - safety*/
2569
195k
   bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - (opus_int32)ec_tell_frac(enc) - 1;
2570
195k
   anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2571
195k
   bits -= anti_collapse_rsv;
2572
195k
   signalBandwidth = end-1;
2573
195k
#ifndef DISABLE_FLOAT_API
2574
195k
   if (st->analysis.valid)
2575
30.5k
   {
2576
30.5k
      int min_bandwidth;
2577
30.5k
      if (equiv_rate < (opus_int32)32000*C)
2578
20.9k
         min_bandwidth = 13;
2579
9.64k
      else if (equiv_rate < (opus_int32)48000*C)
2580
4.15k
         min_bandwidth = 16;
2581
5.48k
      else if (equiv_rate < (opus_int32)60000*C)
2582
3.70k
         min_bandwidth = 18;
2583
1.78k
      else  if (equiv_rate < (opus_int32)80000*C)
2584
1.06k
         min_bandwidth = 19;
2585
718
      else
2586
718
         min_bandwidth = 20;
2587
30.5k
      signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth);
2588
30.5k
   }
2589
195k
#endif
2590
195k
   if (st->lfe)
2591
921
      signalBandwidth = 1;
2592
195k
   codedBands = clt_compute_allocation(mode, start, end, offsets, cap,
2593
195k
         alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses,
2594
195k
         fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBandwidth);
2595
195k
   if (st->lastCodedBands)
2596
57.2k
      st->lastCodedBands = IMIN(st->lastCodedBands+1,IMAX(st->lastCodedBands-1,codedBands));
2597
138k
   else
2598
138k
      st->lastCodedBands = codedBands;
2599
2600
195k
   quant_fine_energy(mode, start, end, oldBandE, error, NULL, fine_quant, enc, C);
2601
195k
   OPUS_CLEAR(energyError, nbEBands*CC);
2602
#ifdef ENABLE_QEXT
2603
118k
   if (qext_mode)
2604
0
   {
2605
      /* Don't bias for intra. */
2606
0
      opus_val32 qext_delayedIntra=0;
2607
0
      qext_oldBandE = energyError + CC*nbEBands;
2608
0
      compute_band_energies(qext_mode, freq, qext_bandE, qext_end, C, LM, st->arch);
2609
0
      normalise_bands(qext_mode, freq, X, qext_bandE, qext_end, C, M);
2610
0
      amp2Log2(qext_mode, qext_end, qext_end, qext_bandE, qext_bandLogE, C);
2611
0
      if (C==2) {
2612
0
         qext_intensity = qext_end;
2613
0
         qext_dual_stereo = dual_stereo;
2614
0
         encode_qext_stereo_params(&ext_enc, qext_end, qext_intensity, qext_dual_stereo);
2615
0
      }
2616
0
      quant_coarse_energy(qext_mode, 0, qext_end, qext_end, qext_bandLogE,
2617
0
               qext_oldBandE, qext_bytes*8, qext_error, &ext_enc,
2618
0
               C, LM, qext_bytes, st->force_intra,
2619
0
               &qext_delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
2620
0
   }
2621
118k
   ALLOC(extra_quant, nbEBands+NB_QEXT_BANDS, int);
2622
118k
   ALLOC(extra_pulses, nbEBands+NB_QEXT_BANDS, int);
2623
118k
   ALLOC(error_bak, C*nbEBands, celt_glog);
2624
2625
118k
   qext_bits = ((opus_int32)qext_bytes*8<<BITRES) - (opus_int32)ec_tell_frac(enc) - 1;
2626
   clt_compute_extra_allocation(mode, qext_mode, start, end, qext_end, bandLogE, qext_bandLogE,
2627
         qext_bits, extra_pulses, extra_quant, C, LM, &ext_enc, 1, tone_freq, toneishness);
2628
118k
   OPUS_COPY(error_bak, error, C*nbEBands);
2629
118k
   if (qext_bytes > 0) {
2630
0
      quant_fine_energy(mode, start, end, oldBandE, error, fine_quant, extra_quant, &ext_enc, C);
2631
0
   }
2632
#endif
2633
2634
   /* Residual quantisation */
2635
195k
   ALLOC(collapse_masks, C*nbEBands, unsigned char);
2636
195k
   quant_all_bands(1, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
2637
195k
         bandE, pulses, shortBlocks, st->spread_decision,
2638
195k
         dual_stereo, st->intensity, tf_res, nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv,
2639
195k
         balance, enc, LM, codedBands, &st->rng, st->complexity, st->arch, st->disable_inv
2640
195k
         ARG_QEXT(&ext_enc) ARG_QEXT(extra_pulses)
2641
195k
         ARG_QEXT(qext_bytes*(8<<BITRES)) ARG_QEXT(cap));
2642
2643
#ifdef ENABLE_QEXT
2644
118k
   if (qext_mode) {
2645
0
      VARDECL(int, zeros);
2646
0
      VARDECL(unsigned char, qext_collapse_masks);
2647
0
      ec_enc dummy_enc;
2648
0
      int ext_balance;
2649
0
      ALLOC(zeros, nbEBands, int);
2650
0
      ALLOC(qext_collapse_masks, C*NB_QEXT_BANDS, unsigned char);
2651
0
      ec_enc_init(&dummy_enc, NULL, 0);
2652
0
      OPUS_CLEAR(zeros, end);
2653
0
      ext_balance = qext_bytes*(8<<BITRES) - ec_tell_frac(&ext_enc);
2654
0
      for (i=0;i<qext_end;i++) ext_balance -= extra_pulses[nbEBands+i] + C*(extra_quant[nbEBands+1]<<BITRES);
2655
0
      quant_fine_energy(qext_mode, 0, qext_end, qext_oldBandE, qext_error, NULL, &extra_quant[nbEBands], &ext_enc, C);
2656
0
      quant_all_bands(1, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks,
2657
0
            qext_bandE, &extra_pulses[nbEBands], shortBlocks, st->spread_decision,
2658
0
            qext_dual_stereo, qext_intensity, zeros, qext_bytes*(8<<BITRES),
2659
0
            ext_balance, &ext_enc, LM, qext_end, &st->rng, st->complexity, st->arch, st->disable_inv, &dummy_enc, zeros, 0, NULL);
2660
0
   }
2661
#endif
2662
2663
195k
   if (anti_collapse_rsv > 0)
2664
54.7k
   {
2665
54.7k
      anti_collapse_on = st->consec_transient<2;
2666
#ifdef FUZZING
2667
      anti_collapse_on = rand()&0x1;
2668
#endif
2669
54.7k
      ec_enc_bits(enc, anti_collapse_on, 1);
2670
54.7k
   }
2671
195k
   if (qext_bytes == 0)
2672
195k
      quant_energy_finalise(mode, start, end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
2673
195k
   c=0;
2674
271k
   do {
2675
4.01M
      for (i=start;i<end;i++)
2676
3.74M
      {
2677
3.74M
         energyError[i+c*nbEBands] = MAXG(-GCONST(0.5f), MING(GCONST(0.5f), error[i+c*nbEBands]));
2678
3.74M
      }
2679
271k
   } while (++c < C);
2680
#ifdef ENABLE_QEXT
2681
118k
   if (qext_bytes > 0)
2682
0
      quant_energy_finalise(mode, start, end, NULL, error_bak, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
2683
#endif
2684
195k
   if (silence)
2685
39.0k
   {
2686
1.06M
      for (i=0;i<C*nbEBands;i++)
2687
1.02M
         oldBandE[i] = -GCONST(28.f);
2688
39.0k
   }
2689
2690
#ifdef RESYNTH
2691
   /* Re-synthesis of the coded audio if required */
2692
   {
2693
      celt_sig *out_mem[2];
2694
2695
      if (anti_collapse_on)
2696
      {
2697
         anti_collapse(mode, X, collapse_masks, LM, C, N,
2698
               start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 1, st->arch);
2699
      }
2700
2701
      c=0; do {
2702
         OPUS_MOVE(st->syn_mem[c], st->syn_mem[c]+N, QEXT_SCALE(DEC_PITCH_BUF_SIZE)-N+overlap/2);
2703
      } while (++c<CC);
2704
2705
      c=0; do {
2706
         out_mem[c] = st->syn_mem[c]+QEXT_SCALE(DEC_PITCH_BUF_SIZE)-N;
2707
      } while (++c<CC);
2708
2709
      celt_synthesis(mode, X, out_mem, oldBandE, start, effEnd,
2710
                     C, CC, isTransient, LM, st->upsample, silence, st->arch ARG_QEXT(qext_mode) ARG_QEXT(qext_oldBandE) ARG_QEXT(qext_end));
2711
2712
      c=0; do {
2713
         st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
2714
         st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
2715
         comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, mode->shortMdctSize,
2716
               st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
2717
               mode->window, overlap, st->arch);
2718
         if (LM!=0)
2719
            comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMdctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize,
2720
                  st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
2721
                  mode->window, overlap, st->arch);
2722
      } while (++c<CC);
2723
2724
      /* We reuse freq[] as scratch space for the de-emphasis */
2725
      deemphasis(out_mem, (opus_res*)pcm, N, CC, st->upsample, mode->preemph, st->preemph_memD, 0);
2726
      st->prefilter_period_old = st->prefilter_period;
2727
      st->prefilter_gain_old = st->prefilter_gain;
2728
      st->prefilter_tapset_old = st->prefilter_tapset;
2729
   }
2730
#endif
2731
2732
195k
   st->prefilter_period = pitch_index;
2733
195k
   st->prefilter_gain = gain1;
2734
195k
   st->prefilter_tapset = prefilter_tapset;
2735
#ifdef RESYNTH
2736
   if (LM!=0)
2737
   {
2738
      st->prefilter_period_old = st->prefilter_period;
2739
      st->prefilter_gain_old = st->prefilter_gain;
2740
      st->prefilter_tapset_old = st->prefilter_tapset;
2741
   }
2742
#endif
2743
2744
195k
   if (CC==2&&C==1) {
2745
24.5k
      OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
2746
24.5k
   }
2747
2748
195k
   if (!isTransient)
2749
123k
   {
2750
123k
      OPUS_COPY(oldLogE2, oldLogE, CC*nbEBands);
2751
123k
      OPUS_COPY(oldLogE, oldBandE, CC*nbEBands);
2752
123k
   } else {
2753
2.51M
      for (i=0;i<CC*nbEBands;i++)
2754
2.44M
         oldLogE[i] = MING(oldLogE[i], oldBandE[i]);
2755
72.1k
   }
2756
   /* In case start or end were to change */
2757
195k
   c=0; do
2758
295k
   {
2759
606k
      for (i=0;i<start;i++)
2760
310k
      {
2761
310k
         oldBandE[c*nbEBands+i]=0;
2762
310k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
2763
310k
      }
2764
2.10M
      for (i=end;i<nbEBands;i++)
2765
1.81M
      {
2766
1.81M
         oldBandE[c*nbEBands+i]=0;
2767
1.81M
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
2768
1.81M
      }
2769
295k
   } while (++c<CC);
2770
2771
195k
   if (isTransient || transient_got_disabled)
2772
169k
      st->consec_transient++;
2773
26.4k
   else
2774
26.4k
      st->consec_transient=0;
2775
195k
   st->rng = enc->rng;
2776
2777
   /* If there's any room left (can only happen for very high rates),
2778
      it's already filled with zeros */
2779
195k
   ec_enc_done(enc);
2780
#ifdef ENABLE_QEXT
2781
   ec_enc_done(&ext_enc);
2782
118k
   if (qext_bytes > 0)
2783
0
      nbCompressedBytes += padding_len_bytes+2+qext_bytes;
2784
118k
   if (qext_bytes) st->rng = st->rng ^ ext_enc.rng;
2785
118k
   if (ec_get_error(&ext_enc))
2786
0
      return OPUS_INTERNAL_ERROR;
2787
118k
#endif
2788
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
2789
   if (st->signalling)
2790
      nbCompressedBytes++;
2791
#endif
2792
2793
118k
   RESTORE_STACK;
2794
195k
   if (ec_get_error(enc))
2795
0
      return OPUS_INTERNAL_ERROR;
2796
195k
   else
2797
195k
      return nbCompressedBytes;
2798
118k
}
celt_encode_with_ec
Line
Count
Source
1714
88.7k
{
1715
88.7k
   int i, c, N;
1716
88.7k
   opus_int32 bits;
1717
88.7k
   ec_enc _enc;
1718
88.7k
   VARDECL(celt_sig, in);
1719
88.7k
   VARDECL(celt_sig, freq);
1720
88.7k
   VARDECL(celt_norm, X);
1721
88.7k
   VARDECL(celt_ener, bandE);
1722
88.7k
   VARDECL(celt_glog, bandLogE);
1723
88.7k
   VARDECL(celt_glog, bandLogE2);
1724
88.7k
   VARDECL(int, fine_quant);
1725
88.7k
   VARDECL(celt_glog, error);
1726
88.7k
   VARDECL(int, pulses);
1727
88.7k
   VARDECL(int, cap);
1728
88.7k
   VARDECL(int, offsets);
1729
88.7k
   VARDECL(int, importance);
1730
88.7k
   VARDECL(int, spread_weight);
1731
88.7k
   VARDECL(int, fine_priority);
1732
88.7k
   VARDECL(int, tf_res);
1733
88.7k
   VARDECL(unsigned char, collapse_masks);
1734
88.7k
   celt_sig *prefilter_mem;
1735
88.7k
   celt_glog *oldBandE, *oldLogE, *oldLogE2, *energyError;
1736
88.7k
   int shortBlocks=0;
1737
88.7k
   int isTransient=0;
1738
88.7k
   const int CC = st->channels;
1739
88.7k
   const int C = st->stream_channels;
1740
88.7k
   int LM, M;
1741
88.7k
   int tf_select;
1742
88.7k
   int nbFilledBytes, nbAvailableBytes;
1743
88.7k
   opus_int32 min_allowed;
1744
88.7k
   int start;
1745
88.7k
   int end;
1746
88.7k
   int effEnd;
1747
88.7k
   int codedBands;
1748
88.7k
   int alloc_trim;
1749
88.7k
   int pitch_index=COMBFILTER_MINPERIOD;
1750
88.7k
   opus_val16 gain1 = 0;
1751
88.7k
   int dual_stereo=0;
1752
88.7k
   int effectiveBytes;
1753
88.7k
   int dynalloc_logp;
1754
88.7k
   opus_int32 vbr_rate;
1755
88.7k
   opus_int32 total_bits;
1756
88.7k
   opus_int32 total_boost;
1757
88.7k
   opus_int32 balance;
1758
88.7k
   opus_int32 tell;
1759
88.7k
   opus_int32 tell0_frac;
1760
88.7k
   int prefilter_tapset=0;
1761
88.7k
   int pf_on;
1762
88.7k
   int anti_collapse_rsv;
1763
88.7k
   int anti_collapse_on=0;
1764
88.7k
   int silence=0;
1765
88.7k
   int tf_chan = 0;
1766
88.7k
   opus_val16 tf_estimate;
1767
88.7k
   int pitch_change=0;
1768
88.7k
   opus_int32 tot_boost;
1769
88.7k
   opus_val32 sample_max;
1770
88.7k
   celt_glog maxDepth;
1771
88.7k
   const OpusCustomMode *mode;
1772
88.7k
   int nbEBands;
1773
88.7k
   int overlap;
1774
88.7k
   const opus_int16 *eBands;
1775
88.7k
   int secondMdct;
1776
88.7k
   int signalBandwidth;
1777
88.7k
   int transient_got_disabled=0;
1778
88.7k
   celt_glog surround_masking=0;
1779
88.7k
   celt_glog temporal_vbr=0;
1780
88.7k
   celt_glog surround_trim = 0;
1781
88.7k
   opus_int32 equiv_rate;
1782
88.7k
   int hybrid;
1783
88.7k
   int weak_transient = 0;
1784
88.7k
   int enable_tf_analysis;
1785
88.7k
   opus_val16 tone_freq=-1;
1786
88.7k
   opus_val32 toneishness=0;
1787
88.7k
   VARDECL(celt_glog, surround_dynalloc);
1788
88.7k
   int qext_bytes=0;
1789
88.7k
   int packet_size_cap = 1275;
1790
88.7k
#ifdef ENABLE_QEXT
1791
88.7k
   int qext_scale;
1792
88.7k
   int qext_end=0;
1793
88.7k
   int qext_intensity=0;
1794
88.7k
   int qext_dual_stereo=0;
1795
88.7k
   int padding_len_bytes=0;
1796
88.7k
   unsigned char *ext_payload;
1797
88.7k
   opus_int32 qext_bits;
1798
88.7k
   ec_enc ext_enc;
1799
88.7k
   VARDECL(int, extra_quant);
1800
88.7k
   VARDECL(int, extra_pulses);
1801
88.7k
   VARDECL(celt_glog, error_bak);
1802
88.7k
   const CELTMode *qext_mode = NULL;
1803
88.7k
   CELTMode qext_mode_struct;
1804
88.7k
   celt_ener qext_bandE[2*NB_QEXT_BANDS];
1805
88.7k
   celt_glog qext_bandLogE[2*NB_QEXT_BANDS];
1806
88.7k
   celt_glog *qext_oldBandE=NULL;
1807
88.7k
   celt_glog qext_error[2*NB_QEXT_BANDS];
1808
88.7k
#endif
1809
88.7k
   ALLOC_STACK;
1810
1811
88.7k
   mode = st->mode;
1812
88.7k
   nbEBands = mode->nbEBands;
1813
88.7k
   overlap = mode->overlap;
1814
88.7k
   eBands = mode->eBands;
1815
88.7k
   start = st->start;
1816
88.7k
   end = st->end;
1817
88.7k
   hybrid = start != 0;
1818
88.7k
   tf_estimate = 0;
1819
88.7k
   if (nbCompressedBytes<2 || pcm==NULL)
1820
0
   {
1821
0
      RESTORE_STACK;
1822
0
      return OPUS_BAD_ARG;
1823
0
   }
1824
1825
88.7k
   frame_size *= st->upsample;
1826
212k
   for (LM=0;LM<=mode->maxLM;LM++)
1827
212k
      if (mode->shortMdctSize<<LM==frame_size)
1828
88.7k
         break;
1829
88.7k
   if (LM>mode->maxLM)
1830
0
   {
1831
0
      RESTORE_STACK;
1832
0
      return OPUS_BAD_ARG;
1833
0
   }
1834
88.7k
   M=1<<LM;
1835
88.7k
   N = M*mode->shortMdctSize;
1836
1837
88.7k
#ifdef ENABLE_QEXT
1838
88.7k
   qext_scale = st->qext_scale;
1839
88.7k
   if (st->enable_qext) packet_size_cap = QEXT_PACKET_SIZE_CAP;
1840
88.7k
#endif
1841
1842
88.7k
   prefilter_mem = st->in_mem+CC*(overlap);
1843
88.7k
   oldBandE = (celt_glog*)(st->in_mem+CC*(overlap+QEXT_SCALE(COMBFILTER_MAXPERIOD)));
1844
88.7k
   oldLogE = oldBandE + CC*nbEBands;
1845
88.7k
   oldLogE2 = oldLogE + CC*nbEBands;
1846
88.7k
   energyError = oldLogE2 + CC*nbEBands;
1847
1848
88.7k
   if (enc==NULL)
1849
0
   {
1850
0
      tell0_frac=tell=1;
1851
0
      nbFilledBytes=0;
1852
88.7k
   } else {
1853
88.7k
      tell0_frac=ec_tell_frac(enc);
1854
88.7k
      tell=ec_tell(enc);
1855
88.7k
      nbFilledBytes=(tell+4)>>3;
1856
88.7k
   }
1857
1858
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
1859
   if (st->signalling && enc==NULL)
1860
   {
1861
      int tmp = (mode->effEBands-end)>>1;
1862
      end = st->end = IMAX(1, mode->effEBands-tmp);
1863
      compressed[0] = tmp<<5;
1864
      compressed[0] |= LM<<3;
1865
      compressed[0] |= (C==2)<<2;
1866
      /* Convert "standard mode" to Opus header */
1867
# ifndef ENABLE_QEXT
1868
      if (mode->Fs==48000 && mode->shortMdctSize==120)
1869
# endif
1870
      {
1871
         int c0 = toOpus(compressed[0]);
1872
         if (c0<0)
1873
         {
1874
            RESTORE_STACK;
1875
            return OPUS_BAD_ARG;
1876
         }
1877
         compressed[0] = c0;
1878
      }
1879
      compressed++;
1880
      nbCompressedBytes--;
1881
   }
1882
#else
1883
88.7k
   celt_assert(st->signalling==0);
1884
88.7k
#endif
1885
1886
   /* Can't produce more than 1275 output bytes for the main payload, plus any QEXT extra data. */
1887
88.7k
   nbCompressedBytes = IMIN(nbCompressedBytes,packet_size_cap);
1888
1889
88.7k
   if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1890
63.6k
   {
1891
63.6k
      opus_int32 den=mode->Fs>>BITRES;
1892
63.6k
      vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1893
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
1894
      if (st->signalling)
1895
         vbr_rate -= 8<<BITRES;
1896
#endif
1897
63.6k
      effectiveBytes = vbr_rate>>(3+BITRES);
1898
63.6k
   } else {
1899
25.0k
      opus_int32 tmp;
1900
25.0k
      vbr_rate = 0;
1901
25.0k
      tmp = st->bitrate*frame_size;
1902
25.0k
      if (tell>1)
1903
1.38k
         tmp += tell*mode->Fs;
1904
25.0k
      if (st->bitrate!=OPUS_BITRATE_MAX)
1905
0
      {
1906
0
         nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1907
0
               (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling));
1908
0
         if (enc != NULL)
1909
0
            ec_enc_shrink(enc, nbCompressedBytes);
1910
0
      }
1911
25.0k
      effectiveBytes = nbCompressedBytes - nbFilledBytes;
1912
25.0k
   }
1913
88.7k
   nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1914
88.7k
   equiv_rate = ((opus_int32)nbCompressedBytes*8*50 << (3-LM)) - (40*C+20)*((400>>LM) - 50);
1915
88.7k
   if (st->bitrate != OPUS_BITRATE_MAX)
1916
63.6k
      equiv_rate = IMIN(equiv_rate, st->bitrate - (40*C+20)*((400>>LM) - 50));
1917
1918
88.7k
   if (enc==NULL)
1919
0
   {
1920
0
      ec_enc_init(&_enc, compressed, nbCompressedBytes);
1921
0
      enc = &_enc;
1922
0
   }
1923
1924
88.7k
   if (vbr_rate>0)
1925
63.6k
   {
1926
      /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1927
          target rate and buffering.
1928
         We must do this up front so that bust-prevention logic triggers
1929
          correctly if we don't have enough bits. */
1930
63.6k
      if (st->constrained_vbr)
1931
25.3k
      {
1932
25.3k
         opus_int32 vbr_bound;
1933
25.3k
         opus_int32 max_allowed;
1934
         /* We could use any multiple of vbr_rate as bound (depending on the
1935
             delay).
1936
            This is clamped to ensure we use at least two bytes if the encoder
1937
             was entirely empty, but to allow 0 in hybrid mode. */
1938
25.3k
         vbr_bound = vbr_rate;
1939
25.3k
         max_allowed = IMIN(IMAX(tell==1?2:0,
1940
25.3k
               (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1941
25.3k
               nbAvailableBytes);
1942
25.3k
         if(max_allowed < nbAvailableBytes)
1943
17.4k
         {
1944
17.4k
            nbCompressedBytes = nbFilledBytes+max_allowed;
1945
17.4k
            nbAvailableBytes = max_allowed;
1946
17.4k
            ec_enc_shrink(enc, nbCompressedBytes);
1947
17.4k
         }
1948
25.3k
      }
1949
63.6k
   }
1950
88.7k
   total_bits = nbCompressedBytes*8;
1951
1952
88.7k
   effEnd = end;
1953
88.7k
   if (effEnd > mode->effEBands)
1954
0
      effEnd = mode->effEBands;
1955
1956
88.7k
   ALLOC(in, CC*(N+overlap), celt_sig);
1957
1958
88.7k
   sample_max=MAX32(st->overlap_max, celt_maxabs_res(pcm, C*(N-overlap)/st->upsample));
1959
88.7k
   st->overlap_max=celt_maxabs_res(pcm+C*(N-overlap)/st->upsample, C*overlap/st->upsample);
1960
88.7k
   sample_max=MAX32(sample_max, st->overlap_max);
1961
88.7k
#ifdef FIXED_POINT
1962
88.7k
   silence = (sample_max==0);
1963
#else
1964
   silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth));
1965
#endif
1966
#ifdef FUZZING
1967
   if ((rand()&0x3F)==0)
1968
      silence = 1;
1969
#endif
1970
88.7k
   if (tell==1)
1971
83.6k
      ec_enc_bit_logp(enc, silence, 15);
1972
5.12k
   else
1973
5.12k
      silence=0;
1974
88.7k
   if (silence)
1975
12.4k
   {
1976
      /*In VBR mode there is no need to send more than the minimum. */
1977
12.4k
      if (vbr_rate>0)
1978
9.83k
      {
1979
9.83k
         effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1980
9.83k
         total_bits=nbCompressedBytes*8;
1981
9.83k
         nbAvailableBytes=2;
1982
9.83k
         ec_enc_shrink(enc, nbCompressedBytes);
1983
9.83k
      }
1984
2.64k
#ifdef ENABLE_QEXT
1985
2.64k
      else if (st->enable_qext) {
1986
0
         nbCompressedBytes = IMIN(nbCompressedBytes, 1275);
1987
0
         nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1988
0
         total_bits = nbCompressedBytes*8;
1989
0
         ec_enc_shrink(enc, nbCompressedBytes);
1990
0
      }
1991
12.4k
#endif
1992
      /* Pretend we've filled all the remaining bits with zeros
1993
            (that's what the initialiser did anyway) */
1994
12.4k
      tell = nbCompressedBytes*8;
1995
12.4k
      enc->nbits_total+=tell-ec_tell(enc);
1996
12.4k
   }
1997
128k
   c=0; do {
1998
128k
      int need_clip=0;
1999
#ifndef FIXED_POINT
2000
      need_clip = st->clip && sample_max>65536.f;
2001
#endif
2002
128k
      celt_preemphasis(pcm+c, in+c*(N+overlap)+overlap, N, CC, st->upsample,
2003
128k
                  mode->preemph, st->preemph_memE+c, need_clip);
2004
128k
      OPUS_COPY(in+c*(N+overlap), &prefilter_mem[(1+c)*QEXT_SCALE(COMBFILTER_MAXPERIOD)-overlap], overlap);
2005
128k
   } while (++c<CC);
2006
2007
2008
88.7k
   tone_freq = tone_detect(in, CC, N+overlap, &toneishness, mode->Fs);
2009
88.7k
   isTransient = 0;
2010
88.7k
   shortBlocks = 0;
2011
88.7k
   if (st->complexity >= 1 && !st->lfe)
2012
80.1k
   {
2013
      /* Reduces the likelihood of energy instability on fricatives at low bitrate
2014
         in hybrid mode. It seems like we still want to have real transients on vowels
2015
         though (small SILK quantization offset value). */
2016
80.1k
      int allow_weak_transients = hybrid && effectiveBytes<15 && st->silk_info.signalType != 2;
2017
80.1k
      isTransient = transient_analysis(in, N+overlap, CC,
2018
80.1k
            &tf_estimate, &tf_chan, allow_weak_transients, &weak_transient, tone_freq, toneishness);
2019
80.1k
   }
2020
   /* Find pitch period and gain */
2021
88.7k
   {
2022
88.7k
      int enabled;
2023
88.7k
      int qg;
2024
88.7k
      enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && !hybrid && !silence && tell+16<=total_bits && !st->disable_pf;
2025
2026
88.7k
      prefilter_tapset = st->tapset_decision;
2027
88.7k
      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));
2028
88.7k
      if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && (!st->analysis.valid || st->analysis.tonality > .3)
2029
88.7k
            && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st->prefilter_period))
2030
691
         pitch_change = 1;
2031
88.7k
      if (pf_on==0)
2032
87.4k
      {
2033
87.4k
         if(!hybrid && tell+16<=total_bits)
2034
69.7k
            ec_enc_bit_logp(enc, 0, 1);
2035
87.4k
      } else {
2036
         /*This block is not gated by a total bits check only because
2037
           of the nbAvailableBytes check above.*/
2038
1.31k
         int octave;
2039
1.31k
         ec_enc_bit_logp(enc, 1, 1);
2040
1.31k
         pitch_index += 1;
2041
1.31k
         octave = EC_ILOG(pitch_index)-5;
2042
1.31k
         ec_enc_uint(enc, octave, 6);
2043
1.31k
         ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
2044
1.31k
         pitch_index -= 1;
2045
1.31k
         ec_enc_bits(enc, qg, 3);
2046
1.31k
         ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
2047
1.31k
      }
2048
88.7k
   }
2049
88.7k
   if (LM>0 && ec_tell(enc)+3<=total_bits)
2050
45.3k
   {
2051
45.3k
      if (isTransient)
2052
32.3k
         shortBlocks = M;
2053
45.3k
   } else {
2054
43.4k
      isTransient = 0;
2055
43.4k
      transient_got_disabled=1;
2056
43.4k
   }
2057
2058
88.7k
   ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
2059
88.7k
   ALLOC(bandE,nbEBands*CC, celt_ener);
2060
88.7k
   ALLOC(bandLogE,nbEBands*CC, celt_glog);
2061
2062
88.7k
   secondMdct = shortBlocks && st->complexity>=8;
2063
88.7k
   ALLOC(bandLogE2, C*nbEBands, celt_glog);
2064
88.7k
   if (secondMdct)
2065
22.3k
   {
2066
22.3k
      compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample, st->arch);
2067
22.3k
      compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
2068
22.3k
      amp2Log2(mode, effEnd, end, bandE, bandLogE2, C);
2069
55.1k
      for (c=0;c<C;c++)
2070
32.8k
      {
2071
528k
         for (i=0;i<end;i++)
2072
495k
            bandLogE2[nbEBands*c+i] += HALF32(SHL32(LM, DB_SHIFT));
2073
32.8k
      }
2074
22.3k
   }
2075
2076
88.7k
   compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
2077
   /* This should catch any NaN in the CELT input. Since we're not supposed to see any (they're filtered
2078
      at the Opus layer), just abort. */
2079
88.7k
   celt_assert(!celt_isnan(freq[0]) && (C==1 || !celt_isnan(freq[N])));
2080
88.7k
   if (CC==2&&C==1)
2081
7.64k
      tf_chan = 0;
2082
88.7k
   compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
2083
2084
88.7k
   if (st->lfe)
2085
563
   {
2086
6.75k
      for (i=2;i<end;i++)
2087
6.19k
      {
2088
6.19k
         bandE[i] = IMIN(bandE[i], MULT16_32_Q15(QCONST16(1e-4f,15),bandE[0]));
2089
6.19k
         bandE[i] = MAX32(bandE[i], EPSILON);
2090
6.19k
      }
2091
563
   }
2092
88.7k
   amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
2093
2094
88.7k
   ALLOC(surround_dynalloc, C*nbEBands, celt_glog);
2095
88.7k
   OPUS_CLEAR(surround_dynalloc, end);
2096
   /* This computes how much masking takes place between surround channels */
2097
88.7k
   if (!hybrid&&st->energy_mask&&!st->lfe)
2098
5.09k
   {
2099
5.09k
      int mask_end;
2100
5.09k
      int midband;
2101
5.09k
      int count_dynalloc;
2102
5.09k
      opus_val32 mask_avg=0;
2103
5.09k
      opus_val32 diff=0;
2104
5.09k
      int count=0;
2105
5.09k
      mask_end = IMAX(2,st->lastCodedBands);
2106
14.5k
      for (c=0;c<C;c++)
2107
9.40k
      {
2108
74.8k
         for(i=0;i<mask_end;i++)
2109
65.4k
         {
2110
65.4k
            celt_glog mask;
2111
65.4k
            opus_val16 mask16;
2112
65.4k
            mask = MAXG(MING(st->energy_mask[nbEBands*c+i],
2113
65.4k
                   GCONST(.25f)), -GCONST(2.0f));
2114
65.4k
            if (mask > 0)
2115
48.9k
               mask = HALF32(mask);
2116
65.4k
            mask16 = SHR32(mask, DB_SHIFT-10);
2117
65.4k
            mask_avg += MULT16_16(mask16, eBands[i+1]-eBands[i]);
2118
65.4k
            count += eBands[i+1]-eBands[i];
2119
65.4k
            diff += MULT16_16(mask16, 1+2*i-mask_end);
2120
65.4k
         }
2121
9.40k
      }
2122
5.09k
      celt_assert(count>0);
2123
5.09k
      mask_avg = SHL32(DIV32_16(mask_avg,count), DB_SHIFT-10);
2124
5.09k
      mask_avg += GCONST(.2f);
2125
5.09k
      diff = SHL32(diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end), DB_SHIFT-10);
2126
      /* Again, being conservative */
2127
5.09k
      diff = HALF32(diff);
2128
5.09k
      diff = MAX32(MIN32(diff, GCONST(.031f)), -GCONST(.031f));
2129
      /* Find the band that's in the middle of the coded spectrum */
2130
22.9k
      for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++);
2131
5.09k
      count_dynalloc=0;
2132
39.9k
      for(i=0;i<mask_end;i++)
2133
34.8k
      {
2134
34.8k
         opus_val32 lin;
2135
34.8k
         celt_glog unmask;
2136
34.8k
         lin = mask_avg + diff*(i-midband);
2137
34.8k
         if (C==2)
2138
30.5k
            unmask = MAXG(st->energy_mask[i], st->energy_mask[nbEBands+i]);
2139
4.36k
         else
2140
4.36k
            unmask = st->energy_mask[i];
2141
34.8k
         unmask = MING(unmask, GCONST(.0f));
2142
34.8k
         unmask -= lin;
2143
34.8k
         if (unmask > GCONST(.25f))
2144
4.22k
         {
2145
4.22k
            surround_dynalloc[i] = unmask - GCONST(.25f);
2146
4.22k
            count_dynalloc++;
2147
4.22k
         }
2148
34.8k
      }
2149
5.09k
      if (count_dynalloc>=3)
2150
403
      {
2151
         /* If we need dynalloc in many bands, it's probably because our
2152
            initial masking rate was too low. */
2153
403
         mask_avg += GCONST(.25f);
2154
403
         if (mask_avg>0)
2155
33
         {
2156
            /* Something went really wrong in the original calculations,
2157
               disabling masking. */
2158
33
            mask_avg = 0;
2159
33
            diff = 0;
2160
33
            OPUS_CLEAR(surround_dynalloc, mask_end);
2161
370
         } else {
2162
4.90k
            for(i=0;i<mask_end;i++)
2163
4.53k
               surround_dynalloc[i] = MAXG(0, surround_dynalloc[i]-GCONST(.25f));
2164
370
         }
2165
403
      }
2166
5.09k
      mask_avg += GCONST(.2f);
2167
      /* Convert to 1/64th units used for the trim */
2168
5.09k
      surround_trim = 64*diff;
2169
      /*printf("%d %d ", mask_avg, surround_trim);*/
2170
5.09k
      surround_masking = mask_avg;
2171
5.09k
   }
2172
   /* Temporal VBR (but not for LFE) */
2173
88.7k
   if (!st->lfe)
2174
88.1k
   {
2175
88.1k
      celt_glog follow=-QCONST32(10.0f, DB_SHIFT-5);
2176
88.1k
      opus_val32 frame_avg=0;
2177
88.1k
      celt_glog offset = shortBlocks?HALF32(SHL32(LM, DB_SHIFT-5)):0;
2178
1.31M
      for(i=start;i<end;i++)
2179
1.22M
      {
2180
1.22M
         follow = MAXG(follow-QCONST32(1.0f, DB_SHIFT-5), SHR32(bandLogE[i],5)-offset);
2181
1.22M
         if (C==2)
2182
434k
            follow = MAXG(follow, SHR32(bandLogE[i+nbEBands],5)-offset);
2183
1.22M
         frame_avg += follow;
2184
1.22M
      }
2185
88.1k
      frame_avg /= (end-start);
2186
88.1k
      temporal_vbr = SUB32(SHL32(frame_avg, 5),st->spec_avg);
2187
88.1k
      temporal_vbr = MING(GCONST(3.f), MAXG(-GCONST(1.5f), temporal_vbr));
2188
88.1k
      st->spec_avg += MULT16_32_Q15(QCONST16(.02f, 15), temporal_vbr);
2189
88.1k
   }
2190
   /*for (i=0;i<21;i++)
2191
      printf("%f ", bandLogE[i]);
2192
   printf("\n");*/
2193
2194
88.7k
   if (!secondMdct)
2195
66.4k
   {
2196
66.4k
      OPUS_COPY(bandLogE2, bandLogE, C*nbEBands);
2197
66.4k
   }
2198
2199
   /* Last chance to catch any transient we might have missed in the
2200
      time-domain analysis */
2201
88.7k
   if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5 && !st->lfe && !hybrid)
2202
7.17k
   {
2203
7.17k
      if (patch_transient_decision(bandLogE, oldBandE, nbEBands, start, end, C))
2204
0
      {
2205
0
         isTransient = 1;
2206
0
         shortBlocks = M;
2207
0
         compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
2208
0
         compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
2209
0
         amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
2210
         /* Compensate for the scaling of short vs long mdcts */
2211
0
         for (c=0;c<C;c++)
2212
0
         {
2213
0
            for (i=0;i<end;i++)
2214
0
               bandLogE2[nbEBands*c+i] += HALF32(SHL32(LM, DB_SHIFT));
2215
0
         }
2216
0
         tf_estimate = QCONST16(.2f,14);
2217
0
      }
2218
7.17k
   }
2219
2220
88.7k
   if (LM>0 && ec_tell(enc)+3<=total_bits)
2221
45.3k
      ec_enc_bit_logp(enc, isTransient, 3);
2222
2223
88.7k
   ALLOC(X, C*N, celt_norm);         /**< Interleaved normalised MDCTs */
2224
2225
   /* Band normalisation */
2226
88.7k
   normalise_bands(mode, freq, X, bandE, effEnd, C, M);
2227
2228
88.7k
   enable_tf_analysis = effectiveBytes>=15*C && !hybrid && st->complexity>=2 && !st->lfe && toneishness < QCONST32(.98f, 29);
2229
2230
88.7k
   ALLOC(offsets, nbEBands, int);
2231
88.7k
   ALLOC(importance, nbEBands, int);
2232
88.7k
   ALLOC(spread_weight, nbEBands, int);
2233
2234
88.7k
   maxDepth = dynalloc_analysis(bandLogE, bandLogE2, oldBandE, nbEBands, start, end, C, offsets,
2235
88.7k
         st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr,
2236
88.7k
         eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc, &st->analysis, importance, spread_weight, tone_freq, toneishness ARG_QEXT(qext_scale));
2237
2238
88.7k
   ALLOC(tf_res, nbEBands, int);
2239
   /* Disable variable tf resolution for hybrid and at very low bitrate */
2240
88.7k
   if (enable_tf_analysis)
2241
44.8k
   {
2242
44.8k
      int lambda;
2243
44.8k
      lambda = IMAX(80, 20480/effectiveBytes + 2);
2244
44.8k
      tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, LM, tf_estimate, tf_chan, importance);
2245
44.8k
      for (i=effEnd;i<end;i++)
2246
0
         tf_res[i] = tf_res[effEnd-1];
2247
44.8k
   } else if (hybrid && weak_transient)
2248
86
   {
2249
      /* For weak transients, we rely on the fact that improving time resolution using
2250
         TF on a long window is imperfect and will not result in an energy collapse at
2251
         low bitrate. */
2252
1.72k
      for (i=0;i<end;i++)
2253
1.63k
         tf_res[i] = 1;
2254
86
      tf_select=0;
2255
43.8k
   } else if (hybrid && effectiveBytes<15 && st->silk_info.signalType != 2)
2256
1.44k
   {
2257
      /* For low bitrate hybrid, we force temporal resolution to 5 ms rather than 2.5 ms. */
2258
28.8k
      for (i=0;i<end;i++)
2259
27.4k
         tf_res[i] = 0;
2260
1.44k
      tf_select=isTransient;
2261
42.3k
   } else {
2262
654k
      for (i=0;i<end;i++)
2263
612k
         tf_res[i] = isTransient;
2264
42.3k
      tf_select=0;
2265
42.3k
   }
2266
2267
88.7k
   ALLOC(error, C*nbEBands, celt_glog);
2268
88.7k
   c=0;
2269
120k
   do {
2270
1.78M
      for (i=start;i<end;i++)
2271
1.66M
      {
2272
         /* When the energy is stable, slightly bias energy quantization towards
2273
            the previous error to make the gain more stable (a constant offset is
2274
            better than fluctuations). */
2275
1.66M
         if (ABS32(SUB32(bandLogE[i+c*nbEBands], oldBandE[i+c*nbEBands])) < GCONST(2.f))
2276
429k
         {
2277
429k
            bandLogE[i+c*nbEBands] -= MULT16_32_Q15(QCONST16(0.25f, 15), energyError[i+c*nbEBands]);
2278
429k
         }
2279
1.66M
      }
2280
120k
   } while (++c < C);
2281
88.7k
   quant_coarse_energy(mode, start, end, effEnd, bandLogE,
2282
88.7k
         oldBandE, total_bits, error, enc,
2283
88.7k
         C, LM, nbAvailableBytes, st->force_intra,
2284
88.7k
         &st->delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
2285
2286
88.7k
   tf_encode(start, end, isTransient, tf_res, LM, tf_select, enc);
2287
2288
88.7k
   if (ec_tell(enc)+4<=total_bits)
2289
75.4k
   {
2290
75.4k
      if (st->lfe)
2291
485
      {
2292
485
         st->tapset_decision = 0;
2293
485
         st->spread_decision = SPREAD_NORMAL;
2294
74.9k
      } else if (hybrid)
2295
5.10k
      {
2296
5.10k
         if (st->complexity == 0)
2297
505
            st->spread_decision = SPREAD_NONE;
2298
4.59k
         else if (isTransient)
2299
3.57k
            st->spread_decision = SPREAD_NORMAL;
2300
1.01k
         else
2301
1.01k
            st->spread_decision = SPREAD_AGGRESSIVE;
2302
69.8k
      } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
2303
40.2k
      {
2304
40.2k
         if (st->complexity == 0)
2305
6.36k
            st->spread_decision = SPREAD_NONE;
2306
33.9k
         else
2307
33.9k
            st->spread_decision = SPREAD_NORMAL;
2308
40.2k
      } else {
2309
         /* Disable new spreading+tapset estimator until we can show it works
2310
            better than the old one. So far it seems like spreading_decision()
2311
            works best. */
2312
#if 0
2313
         if (st->analysis.valid)
2314
         {
2315
            static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
2316
            static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
2317
            static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), QCONST16(.15f, 15)};
2318
            static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), QCONST16(.05f, 15)};
2319
            st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision);
2320
            st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
2321
         } else
2322
#endif
2323
29.5k
         {
2324
29.5k
            st->spread_decision = spreading_decision(mode, X,
2325
29.5k
                  &st->tonal_average, st->spread_decision, &st->hf_average,
2326
29.5k
                  &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M, spread_weight);
2327
29.5k
         }
2328
         /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
2329
         /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
2330
29.5k
      }
2331
75.4k
      ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
2332
75.4k
   } else {
2333
13.3k
      st->spread_decision = SPREAD_NORMAL;
2334
13.3k
   }
2335
2336
   /* For LFE, everything interesting is in the first band */
2337
88.7k
   if (st->lfe)
2338
563
      offsets[0] = IMIN(8, effectiveBytes/3);
2339
88.7k
   ALLOC(cap, nbEBands, int);
2340
88.7k
   init_caps(mode,cap,LM,C);
2341
2342
88.7k
   dynalloc_logp = 6;
2343
88.7k
   total_bits<<=BITRES;
2344
88.7k
   total_boost = 0;
2345
88.7k
   tell = ec_tell_frac(enc);
2346
1.31M
   for (i=start;i<end;i++)
2347
1.22M
   {
2348
1.22M
      int width, quanta;
2349
1.22M
      int dynalloc_loop_logp;
2350
1.22M
      int boost;
2351
1.22M
      int j;
2352
1.22M
      width = C*(eBands[i+1]-eBands[i])<<LM;
2353
      /* quanta is 6 bits, but no more than 1 bit/sample
2354
         and no less than 1/8 bit/sample */
2355
1.22M
      quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
2356
1.22M
      dynalloc_loop_logp = dynalloc_logp;
2357
1.22M
      boost = 0;
2358
1.34M
      for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
2359
1.34M
            && boost < cap[i]; j++)
2360
1.13M
      {
2361
1.13M
         int flag;
2362
1.13M
         flag = j<offsets[i];
2363
1.13M
         ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
2364
1.13M
         tell = ec_tell_frac(enc);
2365
1.13M
         if (!flag)
2366
1.02M
            break;
2367
114k
         boost += quanta;
2368
114k
         total_boost += quanta;
2369
114k
         dynalloc_loop_logp = 1;
2370
114k
      }
2371
      /* Making dynalloc more likely */
2372
1.22M
      if (j)
2373
49.0k
         dynalloc_logp = IMAX(2, dynalloc_logp-1);
2374
1.22M
      offsets[i] = boost;
2375
1.22M
   }
2376
2377
88.7k
   if (C==2)
2378
31.7k
   {
2379
31.7k
      static const opus_val16 intensity_thresholds[21]=
2380
      /* 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19  20  off*/
2381
31.7k
        {  1, 2, 3, 4, 5, 6, 7, 8,16,24,36,44,50,56,62,67,72,79,88,106,134};
2382
31.7k
      static const opus_val16 intensity_histeresis[21]=
2383
31.7k
        {  1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6,  8, 8};
2384
2385
      /* Always use MS for 2.5 ms frames until we can do a better analysis */
2386
31.7k
      if (LM!=0)
2387
21.7k
         dual_stereo = stereo_analysis(mode, X, LM, N);
2388
2389
31.7k
      st->intensity = hysteresis_decision((opus_val16)(equiv_rate/1000),
2390
31.7k
            intensity_thresholds, intensity_histeresis, 21, st->intensity);
2391
31.7k
      st->intensity = IMIN(end,IMAX(start, st->intensity));
2392
31.7k
   }
2393
2394
88.7k
   alloc_trim = 5;
2395
88.7k
   if (tell+(6<<BITRES) <= total_bits - total_boost)
2396
74.5k
   {
2397
74.5k
      if (start > 0 || st->lfe)
2398
5.51k
      {
2399
5.51k
         st->stereo_saving = 0;
2400
5.51k
         alloc_trim = 5;
2401
69.0k
      } else {
2402
69.0k
         alloc_trim = alloc_trim_analysis(mode, X, bandLogE,
2403
69.0k
            end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate,
2404
69.0k
            st->intensity, surround_trim, equiv_rate, st->arch);
2405
69.0k
      }
2406
74.5k
      ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
2407
74.5k
      tell = ec_tell_frac(enc);
2408
74.5k
   }
2409
2410
   /* In VBR mode the frame size must not be reduced so much that it would
2411
       result in the encoder running out of bits.
2412
      The margin of 2 bytes ensures that none of the bust-prevention logic
2413
       in the decoder will have triggered so far. */
2414
88.7k
   min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2;
2415
   /* Take into account the 37 bits we need to have left in the packet to
2416
      signal a redundant frame in hybrid mode. Creating a shorter packet would
2417
      create an entropy coder desync. */
2418
88.7k
   if (hybrid)
2419
5.12k
      min_allowed = IMAX(min_allowed, (tell0_frac+(37<<BITRES)+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3));
2420
   /* Variable bitrate */
2421
88.7k
   if (vbr_rate>0)
2422
63.6k
   {
2423
63.6k
     opus_val16 alpha;
2424
63.6k
     opus_int32 delta;
2425
     /* The target rate in 8th bits per frame */
2426
63.6k
     opus_int32 target, base_target;
2427
63.6k
     int lm_diff = mode->maxLM - LM;
2428
2429
     /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
2430
        The CELT allocator will just not be able to use more than that anyway. */
2431
63.6k
     nbCompressedBytes = IMIN(nbCompressedBytes,packet_size_cap>>(3-LM));
2432
63.6k
     if (!hybrid)
2433
59.9k
     {
2434
59.9k
        base_target = vbr_rate - ((40*C+20)<<BITRES);
2435
59.9k
     } else {
2436
3.73k
        base_target = IMAX(0, vbr_rate - ((9*C+4)<<BITRES));
2437
3.73k
     }
2438
2439
63.6k
     if (st->constrained_vbr)
2440
25.3k
        base_target += (st->vbr_offset>>lm_diff);
2441
2442
63.6k
     if (!hybrid)
2443
59.9k
     {
2444
59.9k
        target = compute_vbr(mode, &st->analysis, base_target, LM, equiv_rate,
2445
59.9k
           st->lastCodedBands, C, st->intensity, st->constrained_vbr,
2446
59.9k
           st->stereo_saving, tot_boost, tf_estimate, pitch_change, maxDepth,
2447
59.9k
           st->lfe, st->energy_mask!=NULL, surround_masking,
2448
59.9k
           temporal_vbr ARG_QEXT(st->enable_qext));
2449
59.9k
     } else {
2450
3.73k
        target = base_target;
2451
        /* Tonal frames (offset<100) need more bits than noisy (offset>100) ones. */
2452
3.73k
        if (st->silk_info.offset < 100) target += 12 << BITRES >> (3-LM);
2453
3.73k
        if (st->silk_info.offset > 100) target -= 18 << BITRES >> (3-LM);
2454
        /* Boosting bitrate on transients and vowels with significant temporal
2455
           spikes. */
2456
3.73k
        target += (opus_int32)MULT16_16_Q14(tf_estimate-QCONST16(.25f,14), (50<<BITRES));
2457
        /* If we have a strong transient, let's make sure it has enough bits to code
2458
           the first two bands, so that it can use folding rather than noise. */
2459
3.73k
        if (tf_estimate > QCONST16(.7f,14))
2460
2.54k
           target = IMAX(target, 50<<BITRES);
2461
3.73k
     }
2462
     /* The current offset is removed from the target and the space used
2463
        so far is added*/
2464
63.6k
     target=target+tell;
2465
2466
63.6k
     nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
2467
63.6k
     nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
2468
63.6k
     nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes);
2469
2470
     /* By how much did we "miss" the target on that frame */
2471
63.6k
     delta = target - vbr_rate;
2472
2473
63.6k
     target=nbAvailableBytes<<(BITRES+3);
2474
2475
     /*If the frame is silent we don't adjust our drift, otherwise
2476
       the encoder will shoot to very high rates after hitting a
2477
       span of silence, but we do allow the bitres to refill.
2478
       This means that we'll undershoot our target in CVBR/VBR modes
2479
       on files with lots of silence. */
2480
63.6k
     if(silence)
2481
9.83k
     {
2482
9.83k
       nbAvailableBytes = 2;
2483
9.83k
       target = 2*8<<BITRES;
2484
9.83k
       delta = 0;
2485
9.83k
     }
2486
2487
63.6k
     if (st->vbr_count < 970)
2488
63.6k
     {
2489
63.6k
        st->vbr_count++;
2490
63.6k
        alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
2491
63.6k
     } else
2492
0
        alpha = QCONST16(.001f,15);
2493
     /* How many bits have we used in excess of what we're allowed */
2494
63.6k
     if (st->constrained_vbr)
2495
25.3k
        st->vbr_reservoir += target - vbr_rate;
2496
     /*printf ("%d\n", st->vbr_reservoir);*/
2497
2498
     /* Compute the offset we need to apply in order to reach the target */
2499
63.6k
     if (st->constrained_vbr)
2500
25.3k
     {
2501
25.3k
        st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
2502
25.3k
        st->vbr_offset = -st->vbr_drift;
2503
25.3k
     }
2504
     /*printf ("%d\n", st->vbr_drift);*/
2505
2506
63.6k
     if (st->constrained_vbr && st->vbr_reservoir < 0)
2507
15.8k
     {
2508
        /* We're under the min value -- increase rate */
2509
15.8k
        int adjust = (-st->vbr_reservoir)/(8<<BITRES);
2510
        /* Unless we're just coding silence */
2511
15.8k
        nbAvailableBytes += silence?0:adjust;
2512
15.8k
        st->vbr_reservoir = 0;
2513
        /*printf ("+%d\n", adjust);*/
2514
15.8k
     }
2515
63.6k
     nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes);
2516
     /*printf("%d\n", nbCompressedBytes*50*8);*/
2517
     /* This moves the raw bits to take into account the new compressed size */
2518
63.6k
     ec_enc_shrink(enc, nbCompressedBytes);
2519
63.6k
   }
2520
88.7k
#ifdef ENABLE_QEXT
2521
88.7k
   if (st->enable_qext) {
2522
0
      int new_compressedBytes;
2523
      /* Don't give any bits for the first 80 kb/s per channel. Then 80% of the excess. */
2524
0
      opus_int32 offset = C*80000*frame_size/mode->Fs/8;
2525
0
      qext_bytes = IMAX(nbCompressedBytes-1275, IMAX(0, (nbCompressedBytes-offset)*4/5));
2526
0
      padding_len_bytes = (qext_bytes+253)/254;
2527
0
      qext_bytes = IMIN(qext_bytes, nbCompressedBytes-min_allowed-padding_len_bytes-1);
2528
0
      padding_len_bytes = (qext_bytes+253)/254;
2529
0
      if (qext_bytes > 20) {
2530
0
         new_compressedBytes = nbCompressedBytes-qext_bytes-padding_len_bytes-1;
2531
0
         ec_enc_shrink(enc, new_compressedBytes);
2532
0
         if (compressed == NULL) {
2533
0
            compressed = enc->buf;
2534
0
         }
2535
0
         compressed[-1] |= 0x03; /* Code 3 packet */
2536
0
         enc->buf += 1+padding_len_bytes;
2537
0
         OPUS_MOVE(compressed+1+padding_len_bytes, compressed, new_compressedBytes);
2538
0
         compressed[0] = 0x41; /* Set padding */
2539
0
         for (i=0;i<padding_len_bytes-1;i++) compressed[i+1] = 255;
2540
0
         compressed[padding_len_bytes] = qext_bytes%254 == 0 ? 254 : qext_bytes%254;
2541
0
         ext_payload = compressed+padding_len_bytes+1+new_compressedBytes;
2542
0
         ext_payload[0] = QEXT_EXTENSION_ID<<1;
2543
0
         ext_payload += 1;
2544
0
         qext_bytes -= 1;
2545
0
         OPUS_CLEAR(ext_payload, qext_bytes);
2546
0
         ec_enc_init(&ext_enc, ext_payload, qext_bytes);
2547
0
         nbCompressedBytes = new_compressedBytes;
2548
0
         if (end == nbEBands && (mode->Fs == 48000 ||  mode->Fs == 96000) && (mode->shortMdctSize==120*qext_scale || mode->shortMdctSize==90*qext_scale)) {
2549
0
            compute_qext_mode(&qext_mode_struct, mode);
2550
0
            qext_mode = &qext_mode_struct;
2551
0
            qext_end = (qext_scale == 2) ? NB_QEXT_BANDS : 2;
2552
0
            ec_enc_bit_logp(&ext_enc, qext_end == NB_QEXT_BANDS, 1);
2553
0
         }
2554
0
      } else {
2555
0
         ec_enc_init(&ext_enc, NULL, 0);
2556
0
         qext_bytes = 0;
2557
0
      }
2558
88.7k
   } else {
2559
88.7k
      ec_enc_init(&ext_enc, NULL, 0);
2560
88.7k
   }
2561
88.7k
#endif
2562
2563
   /* Bit allocation */
2564
88.7k
   ALLOC(fine_quant, nbEBands, int);
2565
88.7k
   ALLOC(pulses, nbEBands, int);
2566
88.7k
   ALLOC(fine_priority, nbEBands, int);
2567
2568
   /* bits =           packet size                    - where we are - safety*/
2569
88.7k
   bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - (opus_int32)ec_tell_frac(enc) - 1;
2570
88.7k
   anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2571
88.7k
   bits -= anti_collapse_rsv;
2572
88.7k
   signalBandwidth = end-1;
2573
88.7k
#ifndef DISABLE_FLOAT_API
2574
88.7k
   if (st->analysis.valid)
2575
11.5k
   {
2576
11.5k
      int min_bandwidth;
2577
11.5k
      if (equiv_rate < (opus_int32)32000*C)
2578
7.89k
         min_bandwidth = 13;
2579
3.66k
      else if (equiv_rate < (opus_int32)48000*C)
2580
1.80k
         min_bandwidth = 16;
2581
1.85k
      else if (equiv_rate < (opus_int32)60000*C)
2582
1.44k
         min_bandwidth = 18;
2583
412
      else  if (equiv_rate < (opus_int32)80000*C)
2584
267
         min_bandwidth = 19;
2585
145
      else
2586
145
         min_bandwidth = 20;
2587
11.5k
      signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth);
2588
11.5k
   }
2589
88.7k
#endif
2590
88.7k
   if (st->lfe)
2591
563
      signalBandwidth = 1;
2592
88.7k
   codedBands = clt_compute_allocation(mode, start, end, offsets, cap,
2593
88.7k
         alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses,
2594
88.7k
         fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBandwidth);
2595
88.7k
   if (st->lastCodedBands)
2596
22.6k
      st->lastCodedBands = IMIN(st->lastCodedBands+1,IMAX(st->lastCodedBands-1,codedBands));
2597
66.1k
   else
2598
66.1k
      st->lastCodedBands = codedBands;
2599
2600
88.7k
   quant_fine_energy(mode, start, end, oldBandE, error, NULL, fine_quant, enc, C);
2601
88.7k
   OPUS_CLEAR(energyError, nbEBands*CC);
2602
88.7k
#ifdef ENABLE_QEXT
2603
88.7k
   if (qext_mode)
2604
0
   {
2605
      /* Don't bias for intra. */
2606
0
      opus_val32 qext_delayedIntra=0;
2607
0
      qext_oldBandE = energyError + CC*nbEBands;
2608
0
      compute_band_energies(qext_mode, freq, qext_bandE, qext_end, C, LM, st->arch);
2609
0
      normalise_bands(qext_mode, freq, X, qext_bandE, qext_end, C, M);
2610
0
      amp2Log2(qext_mode, qext_end, qext_end, qext_bandE, qext_bandLogE, C);
2611
0
      if (C==2) {
2612
0
         qext_intensity = qext_end;
2613
0
         qext_dual_stereo = dual_stereo;
2614
0
         encode_qext_stereo_params(&ext_enc, qext_end, qext_intensity, qext_dual_stereo);
2615
0
      }
2616
0
      quant_coarse_energy(qext_mode, 0, qext_end, qext_end, qext_bandLogE,
2617
0
               qext_oldBandE, qext_bytes*8, qext_error, &ext_enc,
2618
0
               C, LM, qext_bytes, st->force_intra,
2619
0
               &qext_delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
2620
0
   }
2621
88.7k
   ALLOC(extra_quant, nbEBands+NB_QEXT_BANDS, int);
2622
88.7k
   ALLOC(extra_pulses, nbEBands+NB_QEXT_BANDS, int);
2623
88.7k
   ALLOC(error_bak, C*nbEBands, celt_glog);
2624
2625
88.7k
   qext_bits = ((opus_int32)qext_bytes*8<<BITRES) - (opus_int32)ec_tell_frac(enc) - 1;
2626
88.7k
   clt_compute_extra_allocation(mode, qext_mode, start, end, qext_end, bandLogE, qext_bandLogE,
2627
88.7k
         qext_bits, extra_pulses, extra_quant, C, LM, &ext_enc, 1, tone_freq, toneishness);
2628
88.7k
   OPUS_COPY(error_bak, error, C*nbEBands);
2629
88.7k
   if (qext_bytes > 0) {
2630
0
      quant_fine_energy(mode, start, end, oldBandE, error, fine_quant, extra_quant, &ext_enc, C);
2631
0
   }
2632
88.7k
#endif
2633
2634
   /* Residual quantisation */
2635
88.7k
   ALLOC(collapse_masks, C*nbEBands, unsigned char);
2636
88.7k
   quant_all_bands(1, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
2637
88.7k
         bandE, pulses, shortBlocks, st->spread_decision,
2638
88.7k
         dual_stereo, st->intensity, tf_res, nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv,
2639
88.7k
         balance, enc, LM, codedBands, &st->rng, st->complexity, st->arch, st->disable_inv
2640
88.7k
         ARG_QEXT(&ext_enc) ARG_QEXT(extra_pulses)
2641
88.7k
         ARG_QEXT(qext_bytes*(8<<BITRES)) ARG_QEXT(cap));
2642
2643
88.7k
#ifdef ENABLE_QEXT
2644
88.7k
   if (qext_mode) {
2645
0
      VARDECL(int, zeros);
2646
0
      VARDECL(unsigned char, qext_collapse_masks);
2647
0
      ec_enc dummy_enc;
2648
0
      int ext_balance;
2649
0
      ALLOC(zeros, nbEBands, int);
2650
0
      ALLOC(qext_collapse_masks, C*NB_QEXT_BANDS, unsigned char);
2651
0
      ec_enc_init(&dummy_enc, NULL, 0);
2652
0
      OPUS_CLEAR(zeros, end);
2653
0
      ext_balance = qext_bytes*(8<<BITRES) - ec_tell_frac(&ext_enc);
2654
0
      for (i=0;i<qext_end;i++) ext_balance -= extra_pulses[nbEBands+i] + C*(extra_quant[nbEBands+1]<<BITRES);
2655
0
      quant_fine_energy(qext_mode, 0, qext_end, qext_oldBandE, qext_error, NULL, &extra_quant[nbEBands], &ext_enc, C);
2656
0
      quant_all_bands(1, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks,
2657
0
            qext_bandE, &extra_pulses[nbEBands], shortBlocks, st->spread_decision,
2658
0
            qext_dual_stereo, qext_intensity, zeros, qext_bytes*(8<<BITRES),
2659
0
            ext_balance, &ext_enc, LM, qext_end, &st->rng, st->complexity, st->arch, st->disable_inv, &dummy_enc, zeros, 0, NULL);
2660
0
   }
2661
88.7k
#endif
2662
2663
88.7k
   if (anti_collapse_rsv > 0)
2664
24.6k
   {
2665
24.6k
      anti_collapse_on = st->consec_transient<2;
2666
#ifdef FUZZING
2667
      anti_collapse_on = rand()&0x1;
2668
#endif
2669
24.6k
      ec_enc_bits(enc, anti_collapse_on, 1);
2670
24.6k
   }
2671
88.7k
   if (qext_bytes == 0)
2672
88.7k
      quant_energy_finalise(mode, start, end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
2673
88.7k
   c=0;
2674
120k
   do {
2675
1.78M
      for (i=start;i<end;i++)
2676
1.66M
      {
2677
1.66M
         energyError[i+c*nbEBands] = MAXG(-GCONST(0.5f), MING(GCONST(0.5f), error[i+c*nbEBands]));
2678
1.66M
      }
2679
120k
   } while (++c < C);
2680
88.7k
#ifdef ENABLE_QEXT
2681
88.7k
   if (qext_bytes > 0)
2682
0
      quant_energy_finalise(mode, start, end, NULL, error_bak, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
2683
88.7k
#endif
2684
88.7k
   if (silence)
2685
12.4k
   {
2686
324k
      for (i=0;i<C*nbEBands;i++)
2687
312k
         oldBandE[i] = -GCONST(28.f);
2688
12.4k
   }
2689
2690
#ifdef RESYNTH
2691
   /* Re-synthesis of the coded audio if required */
2692
   {
2693
      celt_sig *out_mem[2];
2694
2695
      if (anti_collapse_on)
2696
      {
2697
         anti_collapse(mode, X, collapse_masks, LM, C, N,
2698
               start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 1, st->arch);
2699
      }
2700
2701
      c=0; do {
2702
         OPUS_MOVE(st->syn_mem[c], st->syn_mem[c]+N, QEXT_SCALE(DEC_PITCH_BUF_SIZE)-N+overlap/2);
2703
      } while (++c<CC);
2704
2705
      c=0; do {
2706
         out_mem[c] = st->syn_mem[c]+QEXT_SCALE(DEC_PITCH_BUF_SIZE)-N;
2707
      } while (++c<CC);
2708
2709
      celt_synthesis(mode, X, out_mem, oldBandE, start, effEnd,
2710
                     C, CC, isTransient, LM, st->upsample, silence, st->arch ARG_QEXT(qext_mode) ARG_QEXT(qext_oldBandE) ARG_QEXT(qext_end));
2711
2712
      c=0; do {
2713
         st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
2714
         st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
2715
         comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, mode->shortMdctSize,
2716
               st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
2717
               mode->window, overlap, st->arch);
2718
         if (LM!=0)
2719
            comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMdctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize,
2720
                  st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
2721
                  mode->window, overlap, st->arch);
2722
      } while (++c<CC);
2723
2724
      /* We reuse freq[] as scratch space for the de-emphasis */
2725
      deemphasis(out_mem, (opus_res*)pcm, N, CC, st->upsample, mode->preemph, st->preemph_memD, 0);
2726
      st->prefilter_period_old = st->prefilter_period;
2727
      st->prefilter_gain_old = st->prefilter_gain;
2728
      st->prefilter_tapset_old = st->prefilter_tapset;
2729
   }
2730
#endif
2731
2732
88.7k
   st->prefilter_period = pitch_index;
2733
88.7k
   st->prefilter_gain = gain1;
2734
88.7k
   st->prefilter_tapset = prefilter_tapset;
2735
#ifdef RESYNTH
2736
   if (LM!=0)
2737
   {
2738
      st->prefilter_period_old = st->prefilter_period;
2739
      st->prefilter_gain_old = st->prefilter_gain;
2740
      st->prefilter_tapset_old = st->prefilter_tapset;
2741
   }
2742
#endif
2743
2744
88.7k
   if (CC==2&&C==1) {
2745
7.64k
      OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
2746
7.64k
   }
2747
2748
88.7k
   if (!isTransient)
2749
56.3k
   {
2750
56.3k
      OPUS_COPY(oldLogE2, oldLogE, CC*nbEBands);
2751
56.3k
      OPUS_COPY(oldLogE, oldBandE, CC*nbEBands);
2752
56.3k
   } else {
2753
1.08M
      for (i=0;i<CC*nbEBands;i++)
2754
1.05M
         oldLogE[i] = MING(oldLogE[i], oldBandE[i]);
2755
32.3k
   }
2756
   /* In case start or end were to change */
2757
88.7k
   c=0; do
2758
128k
   {
2759
258k
      for (i=0;i<start;i++)
2760
130k
      {
2761
130k
         oldBandE[c*nbEBands+i]=0;
2762
130k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
2763
130k
      }
2764
918k
      for (i=end;i<nbEBands;i++)
2765
790k
      {
2766
790k
         oldBandE[c*nbEBands+i]=0;
2767
790k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
2768
790k
      }
2769
128k
   } while (++c<CC);
2770
2771
88.7k
   if (isTransient || transient_got_disabled)
2772
75.7k
      st->consec_transient++;
2773
12.9k
   else
2774
12.9k
      st->consec_transient=0;
2775
88.7k
   st->rng = enc->rng;
2776
2777
   /* If there's any room left (can only happen for very high rates),
2778
      it's already filled with zeros */
2779
88.7k
   ec_enc_done(enc);
2780
88.7k
#ifdef ENABLE_QEXT
2781
88.7k
   ec_enc_done(&ext_enc);
2782
88.7k
   if (qext_bytes > 0)
2783
0
      nbCompressedBytes += padding_len_bytes+2+qext_bytes;
2784
88.7k
   if (qext_bytes) st->rng = st->rng ^ ext_enc.rng;
2785
88.7k
   if (ec_get_error(&ext_enc))
2786
0
      return OPUS_INTERNAL_ERROR;
2787
88.7k
#endif
2788
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
2789
   if (st->signalling)
2790
      nbCompressedBytes++;
2791
#endif
2792
2793
88.7k
   RESTORE_STACK;
2794
88.7k
   if (ec_get_error(enc))
2795
0
      return OPUS_INTERNAL_ERROR;
2796
88.7k
   else
2797
88.7k
      return nbCompressedBytes;
2798
88.7k
}
celt_encode_with_ec
Line
Count
Source
1714
29.3k
{
1715
29.3k
   int i, c, N;
1716
29.3k
   opus_int32 bits;
1717
29.3k
   ec_enc _enc;
1718
29.3k
   VARDECL(celt_sig, in);
1719
29.3k
   VARDECL(celt_sig, freq);
1720
29.3k
   VARDECL(celt_norm, X);
1721
29.3k
   VARDECL(celt_ener, bandE);
1722
29.3k
   VARDECL(celt_glog, bandLogE);
1723
29.3k
   VARDECL(celt_glog, bandLogE2);
1724
29.3k
   VARDECL(int, fine_quant);
1725
29.3k
   VARDECL(celt_glog, error);
1726
29.3k
   VARDECL(int, pulses);
1727
29.3k
   VARDECL(int, cap);
1728
29.3k
   VARDECL(int, offsets);
1729
29.3k
   VARDECL(int, importance);
1730
29.3k
   VARDECL(int, spread_weight);
1731
29.3k
   VARDECL(int, fine_priority);
1732
29.3k
   VARDECL(int, tf_res);
1733
29.3k
   VARDECL(unsigned char, collapse_masks);
1734
29.3k
   celt_sig *prefilter_mem;
1735
29.3k
   celt_glog *oldBandE, *oldLogE, *oldLogE2, *energyError;
1736
29.3k
   int shortBlocks=0;
1737
29.3k
   int isTransient=0;
1738
29.3k
   const int CC = st->channels;
1739
29.3k
   const int C = st->stream_channels;
1740
29.3k
   int LM, M;
1741
29.3k
   int tf_select;
1742
29.3k
   int nbFilledBytes, nbAvailableBytes;
1743
29.3k
   opus_int32 min_allowed;
1744
29.3k
   int start;
1745
29.3k
   int end;
1746
29.3k
   int effEnd;
1747
29.3k
   int codedBands;
1748
29.3k
   int alloc_trim;
1749
29.3k
   int pitch_index=COMBFILTER_MINPERIOD;
1750
29.3k
   opus_val16 gain1 = 0;
1751
29.3k
   int dual_stereo=0;
1752
29.3k
   int effectiveBytes;
1753
29.3k
   int dynalloc_logp;
1754
29.3k
   opus_int32 vbr_rate;
1755
29.3k
   opus_int32 total_bits;
1756
29.3k
   opus_int32 total_boost;
1757
29.3k
   opus_int32 balance;
1758
29.3k
   opus_int32 tell;
1759
29.3k
   opus_int32 tell0_frac;
1760
29.3k
   int prefilter_tapset=0;
1761
29.3k
   int pf_on;
1762
29.3k
   int anti_collapse_rsv;
1763
29.3k
   int anti_collapse_on=0;
1764
29.3k
   int silence=0;
1765
29.3k
   int tf_chan = 0;
1766
29.3k
   opus_val16 tf_estimate;
1767
29.3k
   int pitch_change=0;
1768
29.3k
   opus_int32 tot_boost;
1769
29.3k
   opus_val32 sample_max;
1770
29.3k
   celt_glog maxDepth;
1771
29.3k
   const OpusCustomMode *mode;
1772
29.3k
   int nbEBands;
1773
29.3k
   int overlap;
1774
29.3k
   const opus_int16 *eBands;
1775
29.3k
   int secondMdct;
1776
29.3k
   int signalBandwidth;
1777
29.3k
   int transient_got_disabled=0;
1778
29.3k
   celt_glog surround_masking=0;
1779
29.3k
   celt_glog temporal_vbr=0;
1780
29.3k
   celt_glog surround_trim = 0;
1781
29.3k
   opus_int32 equiv_rate;
1782
29.3k
   int hybrid;
1783
29.3k
   int weak_transient = 0;
1784
29.3k
   int enable_tf_analysis;
1785
29.3k
   opus_val16 tone_freq=-1;
1786
29.3k
   opus_val32 toneishness=0;
1787
29.3k
   VARDECL(celt_glog, surround_dynalloc);
1788
29.3k
   int qext_bytes=0;
1789
29.3k
   int packet_size_cap = 1275;
1790
29.3k
#ifdef ENABLE_QEXT
1791
29.3k
   int qext_scale;
1792
29.3k
   int qext_end=0;
1793
29.3k
   int qext_intensity=0;
1794
29.3k
   int qext_dual_stereo=0;
1795
29.3k
   int padding_len_bytes=0;
1796
29.3k
   unsigned char *ext_payload;
1797
29.3k
   opus_int32 qext_bits;
1798
29.3k
   ec_enc ext_enc;
1799
29.3k
   VARDECL(int, extra_quant);
1800
29.3k
   VARDECL(int, extra_pulses);
1801
29.3k
   VARDECL(celt_glog, error_bak);
1802
29.3k
   const CELTMode *qext_mode = NULL;
1803
29.3k
   CELTMode qext_mode_struct;
1804
29.3k
   celt_ener qext_bandE[2*NB_QEXT_BANDS];
1805
29.3k
   celt_glog qext_bandLogE[2*NB_QEXT_BANDS];
1806
29.3k
   celt_glog *qext_oldBandE=NULL;
1807
29.3k
   celt_glog qext_error[2*NB_QEXT_BANDS];
1808
29.3k
#endif
1809
29.3k
   ALLOC_STACK;
1810
1811
29.3k
   mode = st->mode;
1812
29.3k
   nbEBands = mode->nbEBands;
1813
29.3k
   overlap = mode->overlap;
1814
29.3k
   eBands = mode->eBands;
1815
29.3k
   start = st->start;
1816
29.3k
   end = st->end;
1817
29.3k
   hybrid = start != 0;
1818
29.3k
   tf_estimate = 0;
1819
29.3k
   if (nbCompressedBytes<2 || pcm==NULL)
1820
0
   {
1821
0
      RESTORE_STACK;
1822
0
      return OPUS_BAD_ARG;
1823
0
   }
1824
1825
29.3k
   frame_size *= st->upsample;
1826
87.3k
   for (LM=0;LM<=mode->maxLM;LM++)
1827
87.3k
      if (mode->shortMdctSize<<LM==frame_size)
1828
29.3k
         break;
1829
29.3k
   if (LM>mode->maxLM)
1830
0
   {
1831
0
      RESTORE_STACK;
1832
0
      return OPUS_BAD_ARG;
1833
0
   }
1834
29.3k
   M=1<<LM;
1835
29.3k
   N = M*mode->shortMdctSize;
1836
1837
29.3k
#ifdef ENABLE_QEXT
1838
29.3k
   qext_scale = st->qext_scale;
1839
29.3k
   if (st->enable_qext) packet_size_cap = QEXT_PACKET_SIZE_CAP;
1840
29.3k
#endif
1841
1842
29.3k
   prefilter_mem = st->in_mem+CC*(overlap);
1843
29.3k
   oldBandE = (celt_glog*)(st->in_mem+CC*(overlap+QEXT_SCALE(COMBFILTER_MAXPERIOD)));
1844
29.3k
   oldLogE = oldBandE + CC*nbEBands;
1845
29.3k
   oldLogE2 = oldLogE + CC*nbEBands;
1846
29.3k
   energyError = oldLogE2 + CC*nbEBands;
1847
1848
29.3k
   if (enc==NULL)
1849
0
   {
1850
0
      tell0_frac=tell=1;
1851
0
      nbFilledBytes=0;
1852
29.3k
   } else {
1853
29.3k
      tell0_frac=ec_tell_frac(enc);
1854
29.3k
      tell=ec_tell(enc);
1855
29.3k
      nbFilledBytes=(tell+4)>>3;
1856
29.3k
   }
1857
1858
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
1859
   if (st->signalling && enc==NULL)
1860
   {
1861
      int tmp = (mode->effEBands-end)>>1;
1862
      end = st->end = IMAX(1, mode->effEBands-tmp);
1863
      compressed[0] = tmp<<5;
1864
      compressed[0] |= LM<<3;
1865
      compressed[0] |= (C==2)<<2;
1866
      /* Convert "standard mode" to Opus header */
1867
# ifndef ENABLE_QEXT
1868
      if (mode->Fs==48000 && mode->shortMdctSize==120)
1869
# endif
1870
      {
1871
         int c0 = toOpus(compressed[0]);
1872
         if (c0<0)
1873
         {
1874
            RESTORE_STACK;
1875
            return OPUS_BAD_ARG;
1876
         }
1877
         compressed[0] = c0;
1878
      }
1879
      compressed++;
1880
      nbCompressedBytes--;
1881
   }
1882
#else
1883
29.3k
   celt_assert(st->signalling==0);
1884
29.3k
#endif
1885
1886
   /* Can't produce more than 1275 output bytes for the main payload, plus any QEXT extra data. */
1887
29.3k
   nbCompressedBytes = IMIN(nbCompressedBytes,packet_size_cap);
1888
1889
29.3k
   if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1890
20.2k
   {
1891
20.2k
      opus_int32 den=mode->Fs>>BITRES;
1892
20.2k
      vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1893
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
1894
      if (st->signalling)
1895
         vbr_rate -= 8<<BITRES;
1896
#endif
1897
20.2k
      effectiveBytes = vbr_rate>>(3+BITRES);
1898
20.2k
   } else {
1899
9.09k
      opus_int32 tmp;
1900
9.09k
      vbr_rate = 0;
1901
9.09k
      tmp = st->bitrate*frame_size;
1902
9.09k
      if (tell>1)
1903
883
         tmp += tell*mode->Fs;
1904
9.09k
      if (st->bitrate!=OPUS_BITRATE_MAX)
1905
0
      {
1906
0
         nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1907
0
               (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling));
1908
0
         if (enc != NULL)
1909
0
            ec_enc_shrink(enc, nbCompressedBytes);
1910
0
      }
1911
9.09k
      effectiveBytes = nbCompressedBytes - nbFilledBytes;
1912
9.09k
   }
1913
29.3k
   nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1914
29.3k
   equiv_rate = ((opus_int32)nbCompressedBytes*8*50 << (3-LM)) - (40*C+20)*((400>>LM) - 50);
1915
29.3k
   if (st->bitrate != OPUS_BITRATE_MAX)
1916
20.2k
      equiv_rate = IMIN(equiv_rate, st->bitrate - (40*C+20)*((400>>LM) - 50));
1917
1918
29.3k
   if (enc==NULL)
1919
0
   {
1920
0
      ec_enc_init(&_enc, compressed, nbCompressedBytes);
1921
0
      enc = &_enc;
1922
0
   }
1923
1924
29.3k
   if (vbr_rate>0)
1925
20.2k
   {
1926
      /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1927
          target rate and buffering.
1928
         We must do this up front so that bust-prevention logic triggers
1929
          correctly if we don't have enough bits. */
1930
20.2k
      if (st->constrained_vbr)
1931
6.16k
      {
1932
6.16k
         opus_int32 vbr_bound;
1933
6.16k
         opus_int32 max_allowed;
1934
         /* We could use any multiple of vbr_rate as bound (depending on the
1935
             delay).
1936
            This is clamped to ensure we use at least two bytes if the encoder
1937
             was entirely empty, but to allow 0 in hybrid mode. */
1938
6.16k
         vbr_bound = vbr_rate;
1939
6.16k
         max_allowed = IMIN(IMAX(tell==1?2:0,
1940
6.16k
               (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1941
6.16k
               nbAvailableBytes);
1942
6.16k
         if(max_allowed < nbAvailableBytes)
1943
2.40k
         {
1944
2.40k
            nbCompressedBytes = nbFilledBytes+max_allowed;
1945
2.40k
            nbAvailableBytes = max_allowed;
1946
2.40k
            ec_enc_shrink(enc, nbCompressedBytes);
1947
2.40k
         }
1948
6.16k
      }
1949
20.2k
   }
1950
29.3k
   total_bits = nbCompressedBytes*8;
1951
1952
29.3k
   effEnd = end;
1953
29.3k
   if (effEnd > mode->effEBands)
1954
0
      effEnd = mode->effEBands;
1955
1956
29.3k
   ALLOC(in, CC*(N+overlap), celt_sig);
1957
1958
29.3k
   sample_max=MAX32(st->overlap_max, celt_maxabs_res(pcm, C*(N-overlap)/st->upsample));
1959
29.3k
   st->overlap_max=celt_maxabs_res(pcm+C*(N-overlap)/st->upsample, C*overlap/st->upsample);
1960
29.3k
   sample_max=MAX32(sample_max, st->overlap_max);
1961
#ifdef FIXED_POINT
1962
   silence = (sample_max==0);
1963
#else
1964
29.3k
   silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth));
1965
29.3k
#endif
1966
#ifdef FUZZING
1967
   if ((rand()&0x3F)==0)
1968
      silence = 1;
1969
#endif
1970
29.3k
   if (tell==1)
1971
27.2k
      ec_enc_bit_logp(enc, silence, 15);
1972
2.14k
   else
1973
2.14k
      silence=0;
1974
29.3k
   if (silence)
1975
5.69k
   {
1976
      /*In VBR mode there is no need to send more than the minimum. */
1977
5.69k
      if (vbr_rate>0)
1978
4.60k
      {
1979
4.60k
         effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1980
4.60k
         total_bits=nbCompressedBytes*8;
1981
4.60k
         nbAvailableBytes=2;
1982
4.60k
         ec_enc_shrink(enc, nbCompressedBytes);
1983
4.60k
      }
1984
1.09k
#ifdef ENABLE_QEXT
1985
1.09k
      else if (st->enable_qext) {
1986
0
         nbCompressedBytes = IMIN(nbCompressedBytes, 1275);
1987
0
         nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1988
0
         total_bits = nbCompressedBytes*8;
1989
0
         ec_enc_shrink(enc, nbCompressedBytes);
1990
0
      }
1991
5.69k
#endif
1992
      /* Pretend we've filled all the remaining bits with zeros
1993
            (that's what the initialiser did anyway) */
1994
5.69k
      tell = nbCompressedBytes*8;
1995
5.69k
      enc->nbits_total+=tell-ec_tell(enc);
1996
5.69k
   }
1997
57.4k
   c=0; do {
1998
57.4k
      int need_clip=0;
1999
57.4k
#ifndef FIXED_POINT
2000
57.4k
      need_clip = st->clip && sample_max>65536.f;
2001
57.4k
#endif
2002
57.4k
      celt_preemphasis(pcm+c, in+c*(N+overlap)+overlap, N, CC, st->upsample,
2003
57.4k
                  mode->preemph, st->preemph_memE+c, need_clip);
2004
57.4k
      OPUS_COPY(in+c*(N+overlap), &prefilter_mem[(1+c)*QEXT_SCALE(COMBFILTER_MAXPERIOD)-overlap], overlap);
2005
57.4k
   } while (++c<CC);
2006
2007
2008
29.3k
   tone_freq = tone_detect(in, CC, N+overlap, &toneishness, mode->Fs);
2009
29.3k
   isTransient = 0;
2010
29.3k
   shortBlocks = 0;
2011
29.3k
   if (st->complexity >= 1 && !st->lfe)
2012
26.0k
   {
2013
      /* Reduces the likelihood of energy instability on fricatives at low bitrate
2014
         in hybrid mode. It seems like we still want to have real transients on vowels
2015
         though (small SILK quantization offset value). */
2016
26.0k
      int allow_weak_transients = hybrid && effectiveBytes<15 && st->silk_info.signalType != 2;
2017
26.0k
      isTransient = transient_analysis(in, N+overlap, CC,
2018
26.0k
            &tf_estimate, &tf_chan, allow_weak_transients, &weak_transient, tone_freq, toneishness);
2019
26.0k
   }
2020
   /* Find pitch period and gain */
2021
29.3k
   {
2022
29.3k
      int enabled;
2023
29.3k
      int qg;
2024
29.3k
      enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && !hybrid && !silence && tell+16<=total_bits && !st->disable_pf;
2025
2026
29.3k
      prefilter_tapset = st->tapset_decision;
2027
29.3k
      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));
2028
29.3k
      if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && (!st->analysis.valid || st->analysis.tonality > .3)
2029
29.3k
            && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st->prefilter_period))
2030
145
         pitch_change = 1;
2031
29.3k
      if (pf_on==0)
2032
29.1k
      {
2033
29.1k
         if(!hybrid && tell+16<=total_bits)
2034
21.3k
            ec_enc_bit_logp(enc, 0, 1);
2035
29.1k
      } else {
2036
         /*This block is not gated by a total bits check only because
2037
           of the nbAvailableBytes check above.*/
2038
220
         int octave;
2039
220
         ec_enc_bit_logp(enc, 1, 1);
2040
220
         pitch_index += 1;
2041
220
         octave = EC_ILOG(pitch_index)-5;
2042
220
         ec_enc_uint(enc, octave, 6);
2043
220
         ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
2044
220
         pitch_index -= 1;
2045
220
         ec_enc_bits(enc, qg, 3);
2046
220
         ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
2047
220
      }
2048
29.3k
   }
2049
29.3k
   if (LM>0 && ec_tell(enc)+3<=total_bits)
2050
17.4k
   {
2051
17.4k
      if (isTransient)
2052
13.0k
         shortBlocks = M;
2053
17.4k
   } else {
2054
11.9k
      isTransient = 0;
2055
11.9k
      transient_got_disabled=1;
2056
11.9k
   }
2057
2058
29.3k
   ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
2059
29.3k
   ALLOC(bandE,nbEBands*CC, celt_ener);
2060
29.3k
   ALLOC(bandLogE,nbEBands*CC, celt_glog);
2061
2062
29.3k
   secondMdct = shortBlocks && st->complexity>=8;
2063
29.3k
   ALLOC(bandLogE2, C*nbEBands, celt_glog);
2064
29.3k
   if (secondMdct)
2065
6.66k
   {
2066
6.66k
      compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample, st->arch);
2067
6.66k
      compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
2068
6.66k
      amp2Log2(mode, effEnd, end, bandE, bandLogE2, C);
2069
18.4k
      for (c=0;c<C;c++)
2070
11.8k
      {
2071
192k
         for (i=0;i<end;i++)
2072
181k
            bandLogE2[nbEBands*c+i] += HALF32(SHL32(LM, DB_SHIFT));
2073
11.8k
      }
2074
6.66k
   }
2075
2076
29.3k
   compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
2077
   /* This should catch any NaN in the CELT input. Since we're not supposed to see any (they're filtered
2078
      at the Opus layer), just abort. */
2079
29.3k
   celt_assert(!celt_isnan(freq[0]) && (C==1 || !celt_isnan(freq[N])));
2080
29.3k
   if (CC==2&&C==1)
2081
7.69k
      tf_chan = 0;
2082
29.3k
   compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
2083
2084
29.3k
   if (st->lfe)
2085
0
   {
2086
0
      for (i=2;i<end;i++)
2087
0
      {
2088
0
         bandE[i] = IMIN(bandE[i], MULT16_32_Q15(QCONST16(1e-4f,15),bandE[0]));
2089
0
         bandE[i] = MAX32(bandE[i], EPSILON);
2090
0
      }
2091
0
   }
2092
29.3k
   amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
2093
2094
29.3k
   ALLOC(surround_dynalloc, C*nbEBands, celt_glog);
2095
29.3k
   OPUS_CLEAR(surround_dynalloc, end);
2096
   /* This computes how much masking takes place between surround channels */
2097
29.3k
   if (!hybrid&&st->energy_mask&&!st->lfe)
2098
0
   {
2099
0
      int mask_end;
2100
0
      int midband;
2101
0
      int count_dynalloc;
2102
0
      opus_val32 mask_avg=0;
2103
0
      opus_val32 diff=0;
2104
0
      int count=0;
2105
0
      mask_end = IMAX(2,st->lastCodedBands);
2106
0
      for (c=0;c<C;c++)
2107
0
      {
2108
0
         for(i=0;i<mask_end;i++)
2109
0
         {
2110
0
            celt_glog mask;
2111
0
            opus_val16 mask16;
2112
0
            mask = MAXG(MING(st->energy_mask[nbEBands*c+i],
2113
0
                   GCONST(.25f)), -GCONST(2.0f));
2114
0
            if (mask > 0)
2115
0
               mask = HALF32(mask);
2116
0
            mask16 = SHR32(mask, DB_SHIFT-10);
2117
0
            mask_avg += MULT16_16(mask16, eBands[i+1]-eBands[i]);
2118
0
            count += eBands[i+1]-eBands[i];
2119
0
            diff += MULT16_16(mask16, 1+2*i-mask_end);
2120
0
         }
2121
0
      }
2122
0
      celt_assert(count>0);
2123
0
      mask_avg = SHL32(DIV32_16(mask_avg,count), DB_SHIFT-10);
2124
0
      mask_avg += GCONST(.2f);
2125
0
      diff = SHL32(diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end), DB_SHIFT-10);
2126
      /* Again, being conservative */
2127
0
      diff = HALF32(diff);
2128
0
      diff = MAX32(MIN32(diff, GCONST(.031f)), -GCONST(.031f));
2129
      /* Find the band that's in the middle of the coded spectrum */
2130
0
      for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++);
2131
0
      count_dynalloc=0;
2132
0
      for(i=0;i<mask_end;i++)
2133
0
      {
2134
0
         opus_val32 lin;
2135
0
         celt_glog unmask;
2136
0
         lin = mask_avg + diff*(i-midband);
2137
0
         if (C==2)
2138
0
            unmask = MAXG(st->energy_mask[i], st->energy_mask[nbEBands+i]);
2139
0
         else
2140
0
            unmask = st->energy_mask[i];
2141
0
         unmask = MING(unmask, GCONST(.0f));
2142
0
         unmask -= lin;
2143
0
         if (unmask > GCONST(.25f))
2144
0
         {
2145
0
            surround_dynalloc[i] = unmask - GCONST(.25f);
2146
0
            count_dynalloc++;
2147
0
         }
2148
0
      }
2149
0
      if (count_dynalloc>=3)
2150
0
      {
2151
         /* If we need dynalloc in many bands, it's probably because our
2152
            initial masking rate was too low. */
2153
0
         mask_avg += GCONST(.25f);
2154
0
         if (mask_avg>0)
2155
0
         {
2156
            /* Something went really wrong in the original calculations,
2157
               disabling masking. */
2158
0
            mask_avg = 0;
2159
0
            diff = 0;
2160
0
            OPUS_CLEAR(surround_dynalloc, mask_end);
2161
0
         } else {
2162
0
            for(i=0;i<mask_end;i++)
2163
0
               surround_dynalloc[i] = MAXG(0, surround_dynalloc[i]-GCONST(.25f));
2164
0
         }
2165
0
      }
2166
0
      mask_avg += GCONST(.2f);
2167
      /* Convert to 1/64th units used for the trim */
2168
0
      surround_trim = 64*diff;
2169
      /*printf("%d %d ", mask_avg, surround_trim);*/
2170
0
      surround_masking = mask_avg;
2171
0
   }
2172
   /* Temporal VBR (but not for LFE) */
2173
29.3k
   if (!st->lfe)
2174
29.3k
   {
2175
29.3k
      celt_glog follow=-QCONST32(10.0f, DB_SHIFT-5);
2176
29.3k
      opus_val32 frame_avg=0;
2177
29.3k
      celt_glog offset = shortBlocks?HALF32(SHL32(LM, DB_SHIFT-5)):0;
2178
433k
      for(i=start;i<end;i++)
2179
404k
      {
2180
404k
         follow = MAXG(follow-QCONST32(1.0f, DB_SHIFT-5), SHR32(bandLogE[i],5)-offset);
2181
404k
         if (C==2)
2182
279k
            follow = MAXG(follow, SHR32(bandLogE[i+nbEBands],5)-offset);
2183
404k
         frame_avg += follow;
2184
404k
      }
2185
29.3k
      frame_avg /= (end-start);
2186
29.3k
      temporal_vbr = SUB32(SHL32(frame_avg, 5),st->spec_avg);
2187
29.3k
      temporal_vbr = MING(GCONST(3.f), MAXG(-GCONST(1.5f), temporal_vbr));
2188
29.3k
      st->spec_avg += MULT16_32_Q15(QCONST16(.02f, 15), temporal_vbr);
2189
29.3k
   }
2190
   /*for (i=0;i<21;i++)
2191
      printf("%f ", bandLogE[i]);
2192
   printf("\n");*/
2193
2194
29.3k
   if (!secondMdct)
2195
22.7k
   {
2196
22.7k
      OPUS_COPY(bandLogE2, bandLogE, C*nbEBands);
2197
22.7k
   }
2198
2199
   /* Last chance to catch any transient we might have missed in the
2200
      time-domain analysis */
2201
29.3k
   if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5 && !st->lfe && !hybrid)
2202
2.01k
   {
2203
2.01k
      if (patch_transient_decision(bandLogE, oldBandE, nbEBands, start, end, C))
2204
116
      {
2205
116
         isTransient = 1;
2206
116
         shortBlocks = M;
2207
116
         compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
2208
116
         compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
2209
116
         amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
2210
         /* Compensate for the scaling of short vs long mdcts */
2211
306
         for (c=0;c<C;c++)
2212
190
         {
2213
3.03k
            for (i=0;i<end;i++)
2214
2.84k
               bandLogE2[nbEBands*c+i] += HALF32(SHL32(LM, DB_SHIFT));
2215
190
         }
2216
116
         tf_estimate = QCONST16(.2f,14);
2217
116
      }
2218
2.01k
   }
2219
2220
29.3k
   if (LM>0 && ec_tell(enc)+3<=total_bits)
2221
17.4k
      ec_enc_bit_logp(enc, isTransient, 3);
2222
2223
29.3k
   ALLOC(X, C*N, celt_norm);         /**< Interleaved normalised MDCTs */
2224
2225
   /* Band normalisation */
2226
29.3k
   normalise_bands(mode, freq, X, bandE, effEnd, C, M);
2227
2228
29.3k
   enable_tf_analysis = effectiveBytes>=15*C && !hybrid && st->complexity>=2 && !st->lfe && toneishness < QCONST32(.98f, 29);
2229
2230
29.3k
   ALLOC(offsets, nbEBands, int);
2231
29.3k
   ALLOC(importance, nbEBands, int);
2232
29.3k
   ALLOC(spread_weight, nbEBands, int);
2233
2234
29.3k
   maxDepth = dynalloc_analysis(bandLogE, bandLogE2, oldBandE, nbEBands, start, end, C, offsets,
2235
29.3k
         st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr,
2236
29.3k
         eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc, &st->analysis, importance, spread_weight, tone_freq, toneishness ARG_QEXT(qext_scale));
2237
2238
29.3k
   ALLOC(tf_res, nbEBands, int);
2239
   /* Disable variable tf resolution for hybrid and at very low bitrate */
2240
29.3k
   if (enable_tf_analysis)
2241
15.9k
   {
2242
15.9k
      int lambda;
2243
15.9k
      lambda = IMAX(80, 20480/effectiveBytes + 2);
2244
15.9k
      tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, LM, tf_estimate, tf_chan, importance);
2245
15.9k
      for (i=effEnd;i<end;i++)
2246
0
         tf_res[i] = tf_res[effEnd-1];
2247
15.9k
   } else if (hybrid && weak_transient)
2248
12
   {
2249
      /* For weak transients, we rely on the fact that improving time resolution using
2250
         TF on a long window is imperfect and will not result in an energy collapse at
2251
         low bitrate. */
2252
240
      for (i=0;i<end;i++)
2253
228
         tf_res[i] = 1;
2254
12
      tf_select=0;
2255
13.3k
   } else if (hybrid && effectiveBytes<15 && st->silk_info.signalType != 2)
2256
48
   {
2257
      /* For low bitrate hybrid, we force temporal resolution to 5 ms rather than 2.5 ms. */
2258
960
      for (i=0;i<end;i++)
2259
912
         tf_res[i] = 0;
2260
48
      tf_select=isTransient;
2261
13.3k
   } else {
2262
212k
      for (i=0;i<end;i++)
2263
199k
         tf_res[i] = isTransient;
2264
13.3k
      tf_select=0;
2265
13.3k
   }
2266
2267
29.3k
   ALLOC(error, C*nbEBands, celt_glog);
2268
29.3k
   c=0;
2269
49.7k
   do {
2270
732k
      for (i=start;i<end;i++)
2271
683k
      {
2272
         /* When the energy is stable, slightly bias energy quantization towards
2273
            the previous error to make the gain more stable (a constant offset is
2274
            better than fluctuations). */
2275
683k
         if (ABS32(SUB32(bandLogE[i+c*nbEBands], oldBandE[i+c*nbEBands])) < GCONST(2.f))
2276
184k
         {
2277
184k
            bandLogE[i+c*nbEBands] -= MULT16_32_Q15(QCONST16(0.25f, 15), energyError[i+c*nbEBands]);
2278
184k
         }
2279
683k
      }
2280
49.7k
   } while (++c < C);
2281
29.3k
   quant_coarse_energy(mode, start, end, effEnd, bandLogE,
2282
29.3k
         oldBandE, total_bits, error, enc,
2283
29.3k
         C, LM, nbAvailableBytes, st->force_intra,
2284
29.3k
         &st->delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
2285
2286
29.3k
   tf_encode(start, end, isTransient, tf_res, LM, tf_select, enc);
2287
2288
29.3k
   if (ec_tell(enc)+4<=total_bits)
2289
23.1k
   {
2290
23.1k
      if (st->lfe)
2291
0
      {
2292
0
         st->tapset_decision = 0;
2293
0
         st->spread_decision = SPREAD_NORMAL;
2294
23.1k
      } else if (hybrid)
2295
2.12k
      {
2296
2.12k
         if (st->complexity == 0)
2297
317
            st->spread_decision = SPREAD_NONE;
2298
1.80k
         else if (isTransient)
2299
1.49k
            st->spread_decision = SPREAD_NORMAL;
2300
311
         else
2301
311
            st->spread_decision = SPREAD_AGGRESSIVE;
2302
20.9k
      } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
2303
14.7k
      {
2304
14.7k
         if (st->complexity == 0)
2305
2.48k
            st->spread_decision = SPREAD_NONE;
2306
12.2k
         else
2307
12.2k
            st->spread_decision = SPREAD_NORMAL;
2308
14.7k
      } else {
2309
         /* Disable new spreading+tapset estimator until we can show it works
2310
            better than the old one. So far it seems like spreading_decision()
2311
            works best. */
2312
#if 0
2313
         if (st->analysis.valid)
2314
         {
2315
            static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
2316
            static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
2317
            static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), QCONST16(.15f, 15)};
2318
            static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), QCONST16(.05f, 15)};
2319
            st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision);
2320
            st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
2321
         } else
2322
#endif
2323
6.20k
         {
2324
6.20k
            st->spread_decision = spreading_decision(mode, X,
2325
6.20k
                  &st->tonal_average, st->spread_decision, &st->hf_average,
2326
6.20k
                  &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M, spread_weight);
2327
6.20k
         }
2328
         /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
2329
         /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
2330
6.20k
      }
2331
23.1k
      ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
2332
23.1k
   } else {
2333
6.25k
      st->spread_decision = SPREAD_NORMAL;
2334
6.25k
   }
2335
2336
   /* For LFE, everything interesting is in the first band */
2337
29.3k
   if (st->lfe)
2338
0
      offsets[0] = IMIN(8, effectiveBytes/3);
2339
29.3k
   ALLOC(cap, nbEBands, int);
2340
29.3k
   init_caps(mode,cap,LM,C);
2341
2342
29.3k
   dynalloc_logp = 6;
2343
29.3k
   total_bits<<=BITRES;
2344
29.3k
   total_boost = 0;
2345
29.3k
   tell = ec_tell_frac(enc);
2346
433k
   for (i=start;i<end;i++)
2347
404k
   {
2348
404k
      int width, quanta;
2349
404k
      int dynalloc_loop_logp;
2350
404k
      int boost;
2351
404k
      int j;
2352
404k
      width = C*(eBands[i+1]-eBands[i])<<LM;
2353
      /* quanta is 6 bits, but no more than 1 bit/sample
2354
         and no less than 1/8 bit/sample */
2355
404k
      quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
2356
404k
      dynalloc_loop_logp = dynalloc_logp;
2357
404k
      boost = 0;
2358
449k
      for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
2359
449k
            && boost < cap[i]; j++)
2360
354k
      {
2361
354k
         int flag;
2362
354k
         flag = j<offsets[i];
2363
354k
         ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
2364
354k
         tell = ec_tell_frac(enc);
2365
354k
         if (!flag)
2366
308k
            break;
2367
45.1k
         boost += quanta;
2368
45.1k
         total_boost += quanta;
2369
45.1k
         dynalloc_loop_logp = 1;
2370
45.1k
      }
2371
      /* Making dynalloc more likely */
2372
404k
      if (j)
2373
19.4k
         dynalloc_logp = IMAX(2, dynalloc_logp-1);
2374
404k
      offsets[i] = boost;
2375
404k
   }
2376
2377
29.3k
   if (C==2)
2378
20.3k
   {
2379
20.3k
      static const opus_val16 intensity_thresholds[21]=
2380
      /* 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19  20  off*/
2381
20.3k
        {  1, 2, 3, 4, 5, 6, 7, 8,16,24,36,44,50,56,62,67,72,79,88,106,134};
2382
20.3k
      static const opus_val16 intensity_histeresis[21]=
2383
20.3k
        {  1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6,  8, 8};
2384
2385
      /* Always use MS for 2.5 ms frames until we can do a better analysis */
2386
20.3k
      if (LM!=0)
2387
15.0k
         dual_stereo = stereo_analysis(mode, X, LM, N);
2388
2389
20.3k
      st->intensity = hysteresis_decision((opus_val16)(equiv_rate/1000),
2390
20.3k
            intensity_thresholds, intensity_histeresis, 21, st->intensity);
2391
20.3k
      st->intensity = IMIN(end,IMAX(start, st->intensity));
2392
20.3k
   }
2393
2394
29.3k
   alloc_trim = 5;
2395
29.3k
   if (tell+(6<<BITRES) <= total_bits - total_boost)
2396
22.6k
   {
2397
22.6k
      if (start > 0 || st->lfe)
2398
2.11k
      {
2399
2.11k
         st->stereo_saving = 0;
2400
2.11k
         alloc_trim = 5;
2401
20.5k
      } else {
2402
20.5k
         alloc_trim = alloc_trim_analysis(mode, X, bandLogE,
2403
20.5k
            end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate,
2404
20.5k
            st->intensity, surround_trim, equiv_rate, st->arch);
2405
20.5k
      }
2406
22.6k
      ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
2407
22.6k
      tell = ec_tell_frac(enc);
2408
22.6k
   }
2409
2410
   /* In VBR mode the frame size must not be reduced so much that it would
2411
       result in the encoder running out of bits.
2412
      The margin of 2 bytes ensures that none of the bust-prevention logic
2413
       in the decoder will have triggered so far. */
2414
29.3k
   min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2;
2415
   /* Take into account the 37 bits we need to have left in the packet to
2416
      signal a redundant frame in hybrid mode. Creating a shorter packet would
2417
      create an entropy coder desync. */
2418
29.3k
   if (hybrid)
2419
2.14k
      min_allowed = IMAX(min_allowed, (tell0_frac+(37<<BITRES)+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3));
2420
   /* Variable bitrate */
2421
29.3k
   if (vbr_rate>0)
2422
20.2k
   {
2423
20.2k
     opus_val16 alpha;
2424
20.2k
     opus_int32 delta;
2425
     /* The target rate in 8th bits per frame */
2426
20.2k
     opus_int32 target, base_target;
2427
20.2k
     int lm_diff = mode->maxLM - LM;
2428
2429
     /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
2430
        The CELT allocator will just not be able to use more than that anyway. */
2431
20.2k
     nbCompressedBytes = IMIN(nbCompressedBytes,packet_size_cap>>(3-LM));
2432
20.2k
     if (!hybrid)
2433
19.0k
     {
2434
19.0k
        base_target = vbr_rate - ((40*C+20)<<BITRES);
2435
19.0k
     } else {
2436
1.25k
        base_target = IMAX(0, vbr_rate - ((9*C+4)<<BITRES));
2437
1.25k
     }
2438
2439
20.2k
     if (st->constrained_vbr)
2440
6.16k
        base_target += (st->vbr_offset>>lm_diff);
2441
2442
20.2k
     if (!hybrid)
2443
19.0k
     {
2444
19.0k
        target = compute_vbr(mode, &st->analysis, base_target, LM, equiv_rate,
2445
19.0k
           st->lastCodedBands, C, st->intensity, st->constrained_vbr,
2446
19.0k
           st->stereo_saving, tot_boost, tf_estimate, pitch_change, maxDepth,
2447
19.0k
           st->lfe, st->energy_mask!=NULL, surround_masking,
2448
19.0k
           temporal_vbr ARG_QEXT(st->enable_qext));
2449
19.0k
     } else {
2450
1.25k
        target = base_target;
2451
        /* Tonal frames (offset<100) need more bits than noisy (offset>100) ones. */
2452
1.25k
        if (st->silk_info.offset < 100) target += 12 << BITRES >> (3-LM);
2453
1.25k
        if (st->silk_info.offset > 100) target -= 18 << BITRES >> (3-LM);
2454
        /* Boosting bitrate on transients and vowels with significant temporal
2455
           spikes. */
2456
1.25k
        target += (opus_int32)MULT16_16_Q14(tf_estimate-QCONST16(.25f,14), (50<<BITRES));
2457
        /* If we have a strong transient, let's make sure it has enough bits to code
2458
           the first two bands, so that it can use folding rather than noise. */
2459
1.25k
        if (tf_estimate > QCONST16(.7f,14))
2460
885
           target = IMAX(target, 50<<BITRES);
2461
1.25k
     }
2462
     /* The current offset is removed from the target and the space used
2463
        so far is added*/
2464
20.2k
     target=target+tell;
2465
2466
20.2k
     nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
2467
20.2k
     nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
2468
20.2k
     nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes);
2469
2470
     /* By how much did we "miss" the target on that frame */
2471
20.2k
     delta = target - vbr_rate;
2472
2473
20.2k
     target=nbAvailableBytes<<(BITRES+3);
2474
2475
     /*If the frame is silent we don't adjust our drift, otherwise
2476
       the encoder will shoot to very high rates after hitting a
2477
       span of silence, but we do allow the bitres to refill.
2478
       This means that we'll undershoot our target in CVBR/VBR modes
2479
       on files with lots of silence. */
2480
20.2k
     if(silence)
2481
4.60k
     {
2482
4.60k
       nbAvailableBytes = 2;
2483
4.60k
       target = 2*8<<BITRES;
2484
4.60k
       delta = 0;
2485
4.60k
     }
2486
2487
20.2k
     if (st->vbr_count < 970)
2488
20.2k
     {
2489
20.2k
        st->vbr_count++;
2490
20.2k
        alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
2491
20.2k
     } else
2492
0
        alpha = QCONST16(.001f,15);
2493
     /* How many bits have we used in excess of what we're allowed */
2494
20.2k
     if (st->constrained_vbr)
2495
6.16k
        st->vbr_reservoir += target - vbr_rate;
2496
     /*printf ("%d\n", st->vbr_reservoir);*/
2497
2498
     /* Compute the offset we need to apply in order to reach the target */
2499
20.2k
     if (st->constrained_vbr)
2500
6.16k
     {
2501
6.16k
        st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
2502
6.16k
        st->vbr_offset = -st->vbr_drift;
2503
6.16k
     }
2504
     /*printf ("%d\n", st->vbr_drift);*/
2505
2506
20.2k
     if (st->constrained_vbr && st->vbr_reservoir < 0)
2507
4.49k
     {
2508
        /* We're under the min value -- increase rate */
2509
4.49k
        int adjust = (-st->vbr_reservoir)/(8<<BITRES);
2510
        /* Unless we're just coding silence */
2511
4.49k
        nbAvailableBytes += silence?0:adjust;
2512
4.49k
        st->vbr_reservoir = 0;
2513
        /*printf ("+%d\n", adjust);*/
2514
4.49k
     }
2515
20.2k
     nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes);
2516
     /*printf("%d\n", nbCompressedBytes*50*8);*/
2517
     /* This moves the raw bits to take into account the new compressed size */
2518
20.2k
     ec_enc_shrink(enc, nbCompressedBytes);
2519
20.2k
   }
2520
29.3k
#ifdef ENABLE_QEXT
2521
29.3k
   if (st->enable_qext) {
2522
0
      int new_compressedBytes;
2523
      /* Don't give any bits for the first 80 kb/s per channel. Then 80% of the excess. */
2524
0
      opus_int32 offset = C*80000*frame_size/mode->Fs/8;
2525
0
      qext_bytes = IMAX(nbCompressedBytes-1275, IMAX(0, (nbCompressedBytes-offset)*4/5));
2526
0
      padding_len_bytes = (qext_bytes+253)/254;
2527
0
      qext_bytes = IMIN(qext_bytes, nbCompressedBytes-min_allowed-padding_len_bytes-1);
2528
0
      padding_len_bytes = (qext_bytes+253)/254;
2529
0
      if (qext_bytes > 20) {
2530
0
         new_compressedBytes = nbCompressedBytes-qext_bytes-padding_len_bytes-1;
2531
0
         ec_enc_shrink(enc, new_compressedBytes);
2532
0
         if (compressed == NULL) {
2533
0
            compressed = enc->buf;
2534
0
         }
2535
0
         compressed[-1] |= 0x03; /* Code 3 packet */
2536
0
         enc->buf += 1+padding_len_bytes;
2537
0
         OPUS_MOVE(compressed+1+padding_len_bytes, compressed, new_compressedBytes);
2538
0
         compressed[0] = 0x41; /* Set padding */
2539
0
         for (i=0;i<padding_len_bytes-1;i++) compressed[i+1] = 255;
2540
0
         compressed[padding_len_bytes] = qext_bytes%254 == 0 ? 254 : qext_bytes%254;
2541
0
         ext_payload = compressed+padding_len_bytes+1+new_compressedBytes;
2542
0
         ext_payload[0] = QEXT_EXTENSION_ID<<1;
2543
0
         ext_payload += 1;
2544
0
         qext_bytes -= 1;
2545
0
         OPUS_CLEAR(ext_payload, qext_bytes);
2546
0
         ec_enc_init(&ext_enc, ext_payload, qext_bytes);
2547
0
         nbCompressedBytes = new_compressedBytes;
2548
0
         if (end == nbEBands && (mode->Fs == 48000 ||  mode->Fs == 96000) && (mode->shortMdctSize==120*qext_scale || mode->shortMdctSize==90*qext_scale)) {
2549
0
            compute_qext_mode(&qext_mode_struct, mode);
2550
0
            qext_mode = &qext_mode_struct;
2551
0
            qext_end = (qext_scale == 2) ? NB_QEXT_BANDS : 2;
2552
0
            ec_enc_bit_logp(&ext_enc, qext_end == NB_QEXT_BANDS, 1);
2553
0
         }
2554
0
      } else {
2555
0
         ec_enc_init(&ext_enc, NULL, 0);
2556
0
         qext_bytes = 0;
2557
0
      }
2558
29.3k
   } else {
2559
29.3k
      ec_enc_init(&ext_enc, NULL, 0);
2560
29.3k
   }
2561
29.3k
#endif
2562
2563
   /* Bit allocation */
2564
29.3k
   ALLOC(fine_quant, nbEBands, int);
2565
29.3k
   ALLOC(pulses, nbEBands, int);
2566
29.3k
   ALLOC(fine_priority, nbEBands, int);
2567
2568
   /* bits =           packet size                    - where we are - safety*/
2569
29.3k
   bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - (opus_int32)ec_tell_frac(enc) - 1;
2570
29.3k
   anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2571
29.3k
   bits -= anti_collapse_rsv;
2572
29.3k
   signalBandwidth = end-1;
2573
29.3k
#ifndef DISABLE_FLOAT_API
2574
29.3k
   if (st->analysis.valid)
2575
5.35k
   {
2576
5.35k
      int min_bandwidth;
2577
5.35k
      if (equiv_rate < (opus_int32)32000*C)
2578
3.54k
         min_bandwidth = 13;
2579
1.80k
      else if (equiv_rate < (opus_int32)48000*C)
2580
746
         min_bandwidth = 16;
2581
1.06k
      else if (equiv_rate < (opus_int32)60000*C)
2582
565
         min_bandwidth = 18;
2583
497
      else  if (equiv_rate < (opus_int32)80000*C)
2584
241
         min_bandwidth = 19;
2585
256
      else
2586
256
         min_bandwidth = 20;
2587
5.35k
      signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth);
2588
5.35k
   }
2589
29.3k
#endif
2590
29.3k
   if (st->lfe)
2591
0
      signalBandwidth = 1;
2592
29.3k
   codedBands = clt_compute_allocation(mode, start, end, offsets, cap,
2593
29.3k
         alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses,
2594
29.3k
         fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBandwidth);
2595
29.3k
   if (st->lastCodedBands)
2596
11.5k
      st->lastCodedBands = IMIN(st->lastCodedBands+1,IMAX(st->lastCodedBands-1,codedBands));
2597
17.7k
   else
2598
17.7k
      st->lastCodedBands = codedBands;
2599
2600
29.3k
   quant_fine_energy(mode, start, end, oldBandE, error, NULL, fine_quant, enc, C);
2601
29.3k
   OPUS_CLEAR(energyError, nbEBands*CC);
2602
29.3k
#ifdef ENABLE_QEXT
2603
29.3k
   if (qext_mode)
2604
0
   {
2605
      /* Don't bias for intra. */
2606
0
      opus_val32 qext_delayedIntra=0;
2607
0
      qext_oldBandE = energyError + CC*nbEBands;
2608
0
      compute_band_energies(qext_mode, freq, qext_bandE, qext_end, C, LM, st->arch);
2609
0
      normalise_bands(qext_mode, freq, X, qext_bandE, qext_end, C, M);
2610
0
      amp2Log2(qext_mode, qext_end, qext_end, qext_bandE, qext_bandLogE, C);
2611
0
      if (C==2) {
2612
0
         qext_intensity = qext_end;
2613
0
         qext_dual_stereo = dual_stereo;
2614
0
         encode_qext_stereo_params(&ext_enc, qext_end, qext_intensity, qext_dual_stereo);
2615
0
      }
2616
0
      quant_coarse_energy(qext_mode, 0, qext_end, qext_end, qext_bandLogE,
2617
0
               qext_oldBandE, qext_bytes*8, qext_error, &ext_enc,
2618
0
               C, LM, qext_bytes, st->force_intra,
2619
0
               &qext_delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
2620
0
   }
2621
29.3k
   ALLOC(extra_quant, nbEBands+NB_QEXT_BANDS, int);
2622
29.3k
   ALLOC(extra_pulses, nbEBands+NB_QEXT_BANDS, int);
2623
29.3k
   ALLOC(error_bak, C*nbEBands, celt_glog);
2624
2625
29.3k
   qext_bits = ((opus_int32)qext_bytes*8<<BITRES) - (opus_int32)ec_tell_frac(enc) - 1;
2626
29.3k
   clt_compute_extra_allocation(mode, qext_mode, start, end, qext_end, bandLogE, qext_bandLogE,
2627
29.3k
         qext_bits, extra_pulses, extra_quant, C, LM, &ext_enc, 1, tone_freq, toneishness);
2628
29.3k
   OPUS_COPY(error_bak, error, C*nbEBands);
2629
29.3k
   if (qext_bytes > 0) {
2630
0
      quant_fine_energy(mode, start, end, oldBandE, error, fine_quant, extra_quant, &ext_enc, C);
2631
0
   }
2632
29.3k
#endif
2633
2634
   /* Residual quantisation */
2635
29.3k
   ALLOC(collapse_masks, C*nbEBands, unsigned char);
2636
29.3k
   quant_all_bands(1, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
2637
29.3k
         bandE, pulses, shortBlocks, st->spread_decision,
2638
29.3k
         dual_stereo, st->intensity, tf_res, nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv,
2639
29.3k
         balance, enc, LM, codedBands, &st->rng, st->complexity, st->arch, st->disable_inv
2640
29.3k
         ARG_QEXT(&ext_enc) ARG_QEXT(extra_pulses)
2641
29.3k
         ARG_QEXT(qext_bytes*(8<<BITRES)) ARG_QEXT(cap));
2642
2643
29.3k
#ifdef ENABLE_QEXT
2644
29.3k
   if (qext_mode) {
2645
0
      VARDECL(int, zeros);
2646
0
      VARDECL(unsigned char, qext_collapse_masks);
2647
0
      ec_enc dummy_enc;
2648
0
      int ext_balance;
2649
0
      ALLOC(zeros, nbEBands, int);
2650
0
      ALLOC(qext_collapse_masks, C*NB_QEXT_BANDS, unsigned char);
2651
0
      ec_enc_init(&dummy_enc, NULL, 0);
2652
0
      OPUS_CLEAR(zeros, end);
2653
0
      ext_balance = qext_bytes*(8<<BITRES) - ec_tell_frac(&ext_enc);
2654
0
      for (i=0;i<qext_end;i++) ext_balance -= extra_pulses[nbEBands+i] + C*(extra_quant[nbEBands+1]<<BITRES);
2655
0
      quant_fine_energy(qext_mode, 0, qext_end, qext_oldBandE, qext_error, NULL, &extra_quant[nbEBands], &ext_enc, C);
2656
0
      quant_all_bands(1, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks,
2657
0
            qext_bandE, &extra_pulses[nbEBands], shortBlocks, st->spread_decision,
2658
0
            qext_dual_stereo, qext_intensity, zeros, qext_bytes*(8<<BITRES),
2659
0
            ext_balance, &ext_enc, LM, qext_end, &st->rng, st->complexity, st->arch, st->disable_inv, &dummy_enc, zeros, 0, NULL);
2660
0
   }
2661
29.3k
#endif
2662
2663
29.3k
   if (anti_collapse_rsv > 0)
2664
10.2k
   {
2665
10.2k
      anti_collapse_on = st->consec_transient<2;
2666
#ifdef FUZZING
2667
      anti_collapse_on = rand()&0x1;
2668
#endif
2669
10.2k
      ec_enc_bits(enc, anti_collapse_on, 1);
2670
10.2k
   }
2671
29.3k
   if (qext_bytes == 0)
2672
29.3k
      quant_energy_finalise(mode, start, end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
2673
29.3k
   c=0;
2674
49.7k
   do {
2675
732k
      for (i=start;i<end;i++)
2676
683k
      {
2677
683k
         energyError[i+c*nbEBands] = MAXG(-GCONST(0.5f), MING(GCONST(0.5f), error[i+c*nbEBands]));
2678
683k
      }
2679
49.7k
   } while (++c < C);
2680
29.3k
#ifdef ENABLE_QEXT
2681
29.3k
   if (qext_bytes > 0)
2682
0
      quant_energy_finalise(mode, start, end, NULL, error_bak, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
2683
29.3k
#endif
2684
29.3k
   if (silence)
2685
5.69k
   {
2686
189k
      for (i=0;i<C*nbEBands;i++)
2687
183k
         oldBandE[i] = -GCONST(28.f);
2688
5.69k
   }
2689
2690
#ifdef RESYNTH
2691
   /* Re-synthesis of the coded audio if required */
2692
   {
2693
      celt_sig *out_mem[2];
2694
2695
      if (anti_collapse_on)
2696
      {
2697
         anti_collapse(mode, X, collapse_masks, LM, C, N,
2698
               start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 1, st->arch);
2699
      }
2700
2701
      c=0; do {
2702
         OPUS_MOVE(st->syn_mem[c], st->syn_mem[c]+N, QEXT_SCALE(DEC_PITCH_BUF_SIZE)-N+overlap/2);
2703
      } while (++c<CC);
2704
2705
      c=0; do {
2706
         out_mem[c] = st->syn_mem[c]+QEXT_SCALE(DEC_PITCH_BUF_SIZE)-N;
2707
      } while (++c<CC);
2708
2709
      celt_synthesis(mode, X, out_mem, oldBandE, start, effEnd,
2710
                     C, CC, isTransient, LM, st->upsample, silence, st->arch ARG_QEXT(qext_mode) ARG_QEXT(qext_oldBandE) ARG_QEXT(qext_end));
2711
2712
      c=0; do {
2713
         st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
2714
         st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
2715
         comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, mode->shortMdctSize,
2716
               st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
2717
               mode->window, overlap, st->arch);
2718
         if (LM!=0)
2719
            comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMdctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize,
2720
                  st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
2721
                  mode->window, overlap, st->arch);
2722
      } while (++c<CC);
2723
2724
      /* We reuse freq[] as scratch space for the de-emphasis */
2725
      deemphasis(out_mem, (opus_res*)pcm, N, CC, st->upsample, mode->preemph, st->preemph_memD, 0);
2726
      st->prefilter_period_old = st->prefilter_period;
2727
      st->prefilter_gain_old = st->prefilter_gain;
2728
      st->prefilter_tapset_old = st->prefilter_tapset;
2729
   }
2730
#endif
2731
2732
29.3k
   st->prefilter_period = pitch_index;
2733
29.3k
   st->prefilter_gain = gain1;
2734
29.3k
   st->prefilter_tapset = prefilter_tapset;
2735
#ifdef RESYNTH
2736
   if (LM!=0)
2737
   {
2738
      st->prefilter_period_old = st->prefilter_period;
2739
      st->prefilter_gain_old = st->prefilter_gain;
2740
      st->prefilter_tapset_old = st->prefilter_tapset;
2741
   }
2742
#endif
2743
2744
29.3k
   if (CC==2&&C==1) {
2745
7.69k
      OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
2746
7.69k
   }
2747
2748
29.3k
   if (!isTransient)
2749
16.1k
   {
2750
16.1k
      OPUS_COPY(oldLogE2, oldLogE, CC*nbEBands);
2751
16.1k
      OPUS_COPY(oldLogE, oldBandE, CC*nbEBands);
2752
16.1k
   } else {
2753
553k
      for (i=0;i<CC*nbEBands;i++)
2754
540k
         oldLogE[i] = MING(oldLogE[i], oldBandE[i]);
2755
13.1k
   }
2756
   /* In case start or end were to change */
2757
29.3k
   c=0; do
2758
57.4k
   {
2759
129k
      for (i=0;i<start;i++)
2760
71.5k
      {
2761
71.5k
         oldBandE[c*nbEBands+i]=0;
2762
71.5k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
2763
71.5k
      }
2764
402k
      for (i=end;i<nbEBands;i++)
2765
344k
      {
2766
344k
         oldBandE[c*nbEBands+i]=0;
2767
344k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
2768
344k
      }
2769
57.4k
   } while (++c<CC);
2770
2771
29.3k
   if (isTransient || transient_got_disabled)
2772
25.1k
      st->consec_transient++;
2773
4.26k
   else
2774
4.26k
      st->consec_transient=0;
2775
29.3k
   st->rng = enc->rng;
2776
2777
   /* If there's any room left (can only happen for very high rates),
2778
      it's already filled with zeros */
2779
29.3k
   ec_enc_done(enc);
2780
29.3k
#ifdef ENABLE_QEXT
2781
29.3k
   ec_enc_done(&ext_enc);
2782
29.3k
   if (qext_bytes > 0)
2783
0
      nbCompressedBytes += padding_len_bytes+2+qext_bytes;
2784
29.3k
   if (qext_bytes) st->rng = st->rng ^ ext_enc.rng;
2785
29.3k
   if (ec_get_error(&ext_enc))
2786
0
      return OPUS_INTERNAL_ERROR;
2787
29.3k
#endif
2788
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
2789
   if (st->signalling)
2790
      nbCompressedBytes++;
2791
#endif
2792
2793
29.3k
   RESTORE_STACK;
2794
29.3k
   if (ec_get_error(enc))
2795
0
      return OPUS_INTERNAL_ERROR;
2796
29.3k
   else
2797
29.3k
      return nbCompressedBytes;
2798
29.3k
}
celt_encode_with_ec
Line
Count
Source
1714
77.7k
{
1715
77.7k
   int i, c, N;
1716
77.7k
   opus_int32 bits;
1717
77.7k
   ec_enc _enc;
1718
77.7k
   VARDECL(celt_sig, in);
1719
77.7k
   VARDECL(celt_sig, freq);
1720
77.7k
   VARDECL(celt_norm, X);
1721
77.7k
   VARDECL(celt_ener, bandE);
1722
77.7k
   VARDECL(celt_glog, bandLogE);
1723
77.7k
   VARDECL(celt_glog, bandLogE2);
1724
77.7k
   VARDECL(int, fine_quant);
1725
77.7k
   VARDECL(celt_glog, error);
1726
77.7k
   VARDECL(int, pulses);
1727
77.7k
   VARDECL(int, cap);
1728
77.7k
   VARDECL(int, offsets);
1729
77.7k
   VARDECL(int, importance);
1730
77.7k
   VARDECL(int, spread_weight);
1731
77.7k
   VARDECL(int, fine_priority);
1732
77.7k
   VARDECL(int, tf_res);
1733
77.7k
   VARDECL(unsigned char, collapse_masks);
1734
77.7k
   celt_sig *prefilter_mem;
1735
77.7k
   celt_glog *oldBandE, *oldLogE, *oldLogE2, *energyError;
1736
77.7k
   int shortBlocks=0;
1737
77.7k
   int isTransient=0;
1738
77.7k
   const int CC = st->channels;
1739
77.7k
   const int C = st->stream_channels;
1740
77.7k
   int LM, M;
1741
77.7k
   int tf_select;
1742
77.7k
   int nbFilledBytes, nbAvailableBytes;
1743
77.7k
   opus_int32 min_allowed;
1744
77.7k
   int start;
1745
77.7k
   int end;
1746
77.7k
   int effEnd;
1747
77.7k
   int codedBands;
1748
77.7k
   int alloc_trim;
1749
77.7k
   int pitch_index=COMBFILTER_MINPERIOD;
1750
77.7k
   opus_val16 gain1 = 0;
1751
77.7k
   int dual_stereo=0;
1752
77.7k
   int effectiveBytes;
1753
77.7k
   int dynalloc_logp;
1754
77.7k
   opus_int32 vbr_rate;
1755
77.7k
   opus_int32 total_bits;
1756
77.7k
   opus_int32 total_boost;
1757
77.7k
   opus_int32 balance;
1758
77.7k
   opus_int32 tell;
1759
77.7k
   opus_int32 tell0_frac;
1760
77.7k
   int prefilter_tapset=0;
1761
77.7k
   int pf_on;
1762
77.7k
   int anti_collapse_rsv;
1763
77.7k
   int anti_collapse_on=0;
1764
77.7k
   int silence=0;
1765
77.7k
   int tf_chan = 0;
1766
77.7k
   opus_val16 tf_estimate;
1767
77.7k
   int pitch_change=0;
1768
77.7k
   opus_int32 tot_boost;
1769
77.7k
   opus_val32 sample_max;
1770
77.7k
   celt_glog maxDepth;
1771
77.7k
   const OpusCustomMode *mode;
1772
77.7k
   int nbEBands;
1773
77.7k
   int overlap;
1774
77.7k
   const opus_int16 *eBands;
1775
77.7k
   int secondMdct;
1776
77.7k
   int signalBandwidth;
1777
77.7k
   int transient_got_disabled=0;
1778
77.7k
   celt_glog surround_masking=0;
1779
77.7k
   celt_glog temporal_vbr=0;
1780
77.7k
   celt_glog surround_trim = 0;
1781
77.7k
   opus_int32 equiv_rate;
1782
77.7k
   int hybrid;
1783
77.7k
   int weak_transient = 0;
1784
77.7k
   int enable_tf_analysis;
1785
77.7k
   opus_val16 tone_freq=-1;
1786
77.7k
   opus_val32 toneishness=0;
1787
77.7k
   VARDECL(celt_glog, surround_dynalloc);
1788
77.7k
   int qext_bytes=0;
1789
77.7k
   int packet_size_cap = 1275;
1790
#ifdef ENABLE_QEXT
1791
   int qext_scale;
1792
   int qext_end=0;
1793
   int qext_intensity=0;
1794
   int qext_dual_stereo=0;
1795
   int padding_len_bytes=0;
1796
   unsigned char *ext_payload;
1797
   opus_int32 qext_bits;
1798
   ec_enc ext_enc;
1799
   VARDECL(int, extra_quant);
1800
   VARDECL(int, extra_pulses);
1801
   VARDECL(celt_glog, error_bak);
1802
   const CELTMode *qext_mode = NULL;
1803
   CELTMode qext_mode_struct;
1804
   celt_ener qext_bandE[2*NB_QEXT_BANDS];
1805
   celt_glog qext_bandLogE[2*NB_QEXT_BANDS];
1806
   celt_glog *qext_oldBandE=NULL;
1807
   celt_glog qext_error[2*NB_QEXT_BANDS];
1808
#endif
1809
77.7k
   ALLOC_STACK;
1810
1811
77.7k
   mode = st->mode;
1812
77.7k
   nbEBands = mode->nbEBands;
1813
77.7k
   overlap = mode->overlap;
1814
77.7k
   eBands = mode->eBands;
1815
77.7k
   start = st->start;
1816
77.7k
   end = st->end;
1817
77.7k
   hybrid = start != 0;
1818
77.7k
   tf_estimate = 0;
1819
77.7k
   if (nbCompressedBytes<2 || pcm==NULL)
1820
0
   {
1821
0
      RESTORE_STACK;
1822
0
      return OPUS_BAD_ARG;
1823
0
   }
1824
1825
77.7k
   frame_size *= st->upsample;
1826
201k
   for (LM=0;LM<=mode->maxLM;LM++)
1827
201k
      if (mode->shortMdctSize<<LM==frame_size)
1828
77.7k
         break;
1829
77.7k
   if (LM>mode->maxLM)
1830
0
   {
1831
0
      RESTORE_STACK;
1832
0
      return OPUS_BAD_ARG;
1833
0
   }
1834
77.7k
   M=1<<LM;
1835
77.7k
   N = M*mode->shortMdctSize;
1836
1837
#ifdef ENABLE_QEXT
1838
   qext_scale = st->qext_scale;
1839
   if (st->enable_qext) packet_size_cap = QEXT_PACKET_SIZE_CAP;
1840
#endif
1841
1842
77.7k
   prefilter_mem = st->in_mem+CC*(overlap);
1843
77.7k
   oldBandE = (celt_glog*)(st->in_mem+CC*(overlap+QEXT_SCALE(COMBFILTER_MAXPERIOD)));
1844
77.7k
   oldLogE = oldBandE + CC*nbEBands;
1845
77.7k
   oldLogE2 = oldLogE + CC*nbEBands;
1846
77.7k
   energyError = oldLogE2 + CC*nbEBands;
1847
1848
77.7k
   if (enc==NULL)
1849
0
   {
1850
0
      tell0_frac=tell=1;
1851
0
      nbFilledBytes=0;
1852
77.7k
   } else {
1853
77.7k
      tell0_frac=ec_tell_frac(enc);
1854
77.7k
      tell=ec_tell(enc);
1855
77.7k
      nbFilledBytes=(tell+4)>>3;
1856
77.7k
   }
1857
1858
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
1859
   if (st->signalling && enc==NULL)
1860
   {
1861
      int tmp = (mode->effEBands-end)>>1;
1862
      end = st->end = IMAX(1, mode->effEBands-tmp);
1863
      compressed[0] = tmp<<5;
1864
      compressed[0] |= LM<<3;
1865
      compressed[0] |= (C==2)<<2;
1866
      /* Convert "standard mode" to Opus header */
1867
# ifndef ENABLE_QEXT
1868
      if (mode->Fs==48000 && mode->shortMdctSize==120)
1869
# endif
1870
      {
1871
         int c0 = toOpus(compressed[0]);
1872
         if (c0<0)
1873
         {
1874
            RESTORE_STACK;
1875
            return OPUS_BAD_ARG;
1876
         }
1877
         compressed[0] = c0;
1878
      }
1879
      compressed++;
1880
      nbCompressedBytes--;
1881
   }
1882
#else
1883
77.7k
   celt_assert(st->signalling==0);
1884
77.7k
#endif
1885
1886
   /* Can't produce more than 1275 output bytes for the main payload, plus any QEXT extra data. */
1887
77.7k
   nbCompressedBytes = IMIN(nbCompressedBytes,packet_size_cap);
1888
1889
77.7k
   if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1890
59.0k
   {
1891
59.0k
      opus_int32 den=mode->Fs>>BITRES;
1892
59.0k
      vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1893
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
1894
      if (st->signalling)
1895
         vbr_rate -= 8<<BITRES;
1896
#endif
1897
59.0k
      effectiveBytes = vbr_rate>>(3+BITRES);
1898
59.0k
   } else {
1899
18.7k
      opus_int32 tmp;
1900
18.7k
      vbr_rate = 0;
1901
18.7k
      tmp = st->bitrate*frame_size;
1902
18.7k
      if (tell>1)
1903
950
         tmp += tell*mode->Fs;
1904
18.7k
      if (st->bitrate!=OPUS_BITRATE_MAX)
1905
0
      {
1906
0
         nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1907
0
               (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling));
1908
0
         if (enc != NULL)
1909
0
            ec_enc_shrink(enc, nbCompressedBytes);
1910
0
      }
1911
18.7k
      effectiveBytes = nbCompressedBytes - nbFilledBytes;
1912
18.7k
   }
1913
77.7k
   nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1914
77.7k
   equiv_rate = ((opus_int32)nbCompressedBytes*8*50 << (3-LM)) - (40*C+20)*((400>>LM) - 50);
1915
77.7k
   if (st->bitrate != OPUS_BITRATE_MAX)
1916
59.0k
      equiv_rate = IMIN(equiv_rate, st->bitrate - (40*C+20)*((400>>LM) - 50));
1917
1918
77.7k
   if (enc==NULL)
1919
0
   {
1920
0
      ec_enc_init(&_enc, compressed, nbCompressedBytes);
1921
0
      enc = &_enc;
1922
0
   }
1923
1924
77.7k
   if (vbr_rate>0)
1925
59.0k
   {
1926
      /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1927
          target rate and buffering.
1928
         We must do this up front so that bust-prevention logic triggers
1929
          correctly if we don't have enough bits. */
1930
59.0k
      if (st->constrained_vbr)
1931
25.4k
      {
1932
25.4k
         opus_int32 vbr_bound;
1933
25.4k
         opus_int32 max_allowed;
1934
         /* We could use any multiple of vbr_rate as bound (depending on the
1935
             delay).
1936
            This is clamped to ensure we use at least two bytes if the encoder
1937
             was entirely empty, but to allow 0 in hybrid mode. */
1938
25.4k
         vbr_bound = vbr_rate;
1939
25.4k
         max_allowed = IMIN(IMAX(tell==1?2:0,
1940
25.4k
               (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1941
25.4k
               nbAvailableBytes);
1942
25.4k
         if(max_allowed < nbAvailableBytes)
1943
14.3k
         {
1944
14.3k
            nbCompressedBytes = nbFilledBytes+max_allowed;
1945
14.3k
            nbAvailableBytes = max_allowed;
1946
14.3k
            ec_enc_shrink(enc, nbCompressedBytes);
1947
14.3k
         }
1948
25.4k
      }
1949
59.0k
   }
1950
77.7k
   total_bits = nbCompressedBytes*8;
1951
1952
77.7k
   effEnd = end;
1953
77.7k
   if (effEnd > mode->effEBands)
1954
0
      effEnd = mode->effEBands;
1955
1956
77.7k
   ALLOC(in, CC*(N+overlap), celt_sig);
1957
1958
77.7k
   sample_max=MAX32(st->overlap_max, celt_maxabs_res(pcm, C*(N-overlap)/st->upsample));
1959
77.7k
   st->overlap_max=celt_maxabs_res(pcm+C*(N-overlap)/st->upsample, C*overlap/st->upsample);
1960
77.7k
   sample_max=MAX32(sample_max, st->overlap_max);
1961
#ifdef FIXED_POINT
1962
   silence = (sample_max==0);
1963
#else
1964
77.7k
   silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth));
1965
77.7k
#endif
1966
#ifdef FUZZING
1967
   if ((rand()&0x3F)==0)
1968
      silence = 1;
1969
#endif
1970
77.7k
   if (tell==1)
1971
73.4k
      ec_enc_bit_logp(enc, silence, 15);
1972
4.32k
   else
1973
4.32k
      silence=0;
1974
77.7k
   if (silence)
1975
20.8k
   {
1976
      /*In VBR mode there is no need to send more than the minimum. */
1977
20.8k
      if (vbr_rate>0)
1978
17.7k
      {
1979
17.7k
         effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1980
17.7k
         total_bits=nbCompressedBytes*8;
1981
17.7k
         nbAvailableBytes=2;
1982
17.7k
         ec_enc_shrink(enc, nbCompressedBytes);
1983
17.7k
      }
1984
#ifdef ENABLE_QEXT
1985
      else if (st->enable_qext) {
1986
         nbCompressedBytes = IMIN(nbCompressedBytes, 1275);
1987
         nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1988
         total_bits = nbCompressedBytes*8;
1989
         ec_enc_shrink(enc, nbCompressedBytes);
1990
      }
1991
#endif
1992
      /* Pretend we've filled all the remaining bits with zeros
1993
            (that's what the initialiser did anyway) */
1994
20.8k
      tell = nbCompressedBytes*8;
1995
20.8k
      enc->nbits_total+=tell-ec_tell(enc);
1996
20.8k
   }
1997
110k
   c=0; do {
1998
110k
      int need_clip=0;
1999
110k
#ifndef FIXED_POINT
2000
110k
      need_clip = st->clip && sample_max>65536.f;
2001
110k
#endif
2002
110k
      celt_preemphasis(pcm+c, in+c*(N+overlap)+overlap, N, CC, st->upsample,
2003
110k
                  mode->preemph, st->preemph_memE+c, need_clip);
2004
110k
      OPUS_COPY(in+c*(N+overlap), &prefilter_mem[(1+c)*QEXT_SCALE(COMBFILTER_MAXPERIOD)-overlap], overlap);
2005
110k
   } while (++c<CC);
2006
2007
2008
77.7k
   tone_freq = tone_detect(in, CC, N+overlap, &toneishness, mode->Fs);
2009
77.7k
   isTransient = 0;
2010
77.7k
   shortBlocks = 0;
2011
77.7k
   if (st->complexity >= 1 && !st->lfe)
2012
69.5k
   {
2013
      /* Reduces the likelihood of energy instability on fricatives at low bitrate
2014
         in hybrid mode. It seems like we still want to have real transients on vowels
2015
         though (small SILK quantization offset value). */
2016
69.5k
      int allow_weak_transients = hybrid && effectiveBytes<15 && st->silk_info.signalType != 2;
2017
69.5k
      isTransient = transient_analysis(in, N+overlap, CC,
2018
69.5k
            &tf_estimate, &tf_chan, allow_weak_transients, &weak_transient, tone_freq, toneishness);
2019
69.5k
   }
2020
   /* Find pitch period and gain */
2021
77.7k
   {
2022
77.7k
      int enabled;
2023
77.7k
      int qg;
2024
77.7k
      enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && !hybrid && !silence && tell+16<=total_bits && !st->disable_pf;
2025
2026
77.7k
      prefilter_tapset = st->tapset_decision;
2027
77.7k
      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));
2028
77.7k
      if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && (!st->analysis.valid || st->analysis.tonality > .3)
2029
77.7k
            && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st->prefilter_period))
2030
536
         pitch_change = 1;
2031
77.7k
      if (pf_on==0)
2032
76.8k
      {
2033
76.8k
         if(!hybrid && tell+16<=total_bits)
2034
51.5k
            ec_enc_bit_logp(enc, 0, 1);
2035
76.8k
      } else {
2036
         /*This block is not gated by a total bits check only because
2037
           of the nbAvailableBytes check above.*/
2038
936
         int octave;
2039
936
         ec_enc_bit_logp(enc, 1, 1);
2040
936
         pitch_index += 1;
2041
936
         octave = EC_ILOG(pitch_index)-5;
2042
936
         ec_enc_uint(enc, octave, 6);
2043
936
         ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
2044
936
         pitch_index -= 1;
2045
936
         ec_enc_bits(enc, qg, 3);
2046
936
         ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
2047
936
      }
2048
77.7k
   }
2049
77.7k
   if (LM>0 && ec_tell(enc)+3<=total_bits)
2050
35.7k
   {
2051
35.7k
      if (isTransient)
2052
26.2k
         shortBlocks = M;
2053
42.0k
   } else {
2054
42.0k
      isTransient = 0;
2055
42.0k
      transient_got_disabled=1;
2056
42.0k
   }
2057
2058
77.7k
   ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
2059
77.7k
   ALLOC(bandE,nbEBands*CC, celt_ener);
2060
77.7k
   ALLOC(bandLogE,nbEBands*CC, celt_glog);
2061
2062
77.7k
   secondMdct = shortBlocks && st->complexity>=8;
2063
77.7k
   ALLOC(bandLogE2, C*nbEBands, celt_glog);
2064
77.7k
   if (secondMdct)
2065
14.7k
   {
2066
14.7k
      compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample, st->arch);
2067
14.7k
      compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
2068
14.7k
      amp2Log2(mode, effEnd, end, bandE, bandLogE2, C);
2069
36.1k
      for (c=0;c<C;c++)
2070
21.3k
      {
2071
343k
         for (i=0;i<end;i++)
2072
321k
            bandLogE2[nbEBands*c+i] += HALF32(SHL32(LM, DB_SHIFT));
2073
21.3k
      }
2074
14.7k
   }
2075
2076
77.7k
   compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
2077
   /* This should catch any NaN in the CELT input. Since we're not supposed to see any (they're filtered
2078
      at the Opus layer), just abort. */
2079
77.7k
   celt_assert(!celt_isnan(freq[0]) && (C==1 || !celt_isnan(freq[N])));
2080
77.7k
   if (CC==2&&C==1)
2081
9.26k
      tf_chan = 0;
2082
77.7k
   compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
2083
2084
77.7k
   if (st->lfe)
2085
358
   {
2086
4.29k
      for (i=2;i<end;i++)
2087
3.93k
      {
2088
3.93k
         bandE[i] = IMIN(bandE[i], MULT16_32_Q15(QCONST16(1e-4f,15),bandE[0]));
2089
3.93k
         bandE[i] = MAX32(bandE[i], EPSILON);
2090
3.93k
      }
2091
358
   }
2092
77.7k
   amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
2093
2094
77.7k
   ALLOC(surround_dynalloc, C*nbEBands, celt_glog);
2095
77.7k
   OPUS_CLEAR(surround_dynalloc, end);
2096
   /* This computes how much masking takes place between surround channels */
2097
77.7k
   if (!hybrid&&st->energy_mask&&!st->lfe)
2098
2.87k
   {
2099
2.87k
      int mask_end;
2100
2.87k
      int midband;
2101
2.87k
      int count_dynalloc;
2102
2.87k
      opus_val32 mask_avg=0;
2103
2.87k
      opus_val32 diff=0;
2104
2.87k
      int count=0;
2105
2.87k
      mask_end = IMAX(2,st->lastCodedBands);
2106
8.25k
      for (c=0;c<C;c++)
2107
5.37k
      {
2108
42.7k
         for(i=0;i<mask_end;i++)
2109
37.3k
         {
2110
37.3k
            celt_glog mask;
2111
37.3k
            opus_val16 mask16;
2112
37.3k
            mask = MAXG(MING(st->energy_mask[nbEBands*c+i],
2113
37.3k
                   GCONST(.25f)), -GCONST(2.0f));
2114
37.3k
            if (mask > 0)
2115
13.4k
               mask = HALF32(mask);
2116
37.3k
            mask16 = SHR32(mask, DB_SHIFT-10);
2117
37.3k
            mask_avg += MULT16_16(mask16, eBands[i+1]-eBands[i]);
2118
37.3k
            count += eBands[i+1]-eBands[i];
2119
37.3k
            diff += MULT16_16(mask16, 1+2*i-mask_end);
2120
37.3k
         }
2121
5.37k
      }
2122
2.87k
      celt_assert(count>0);
2123
2.87k
      mask_avg = SHL32(DIV32_16(mask_avg,count), DB_SHIFT-10);
2124
2.87k
      mask_avg += GCONST(.2f);
2125
2.87k
      diff = SHL32(diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end), DB_SHIFT-10);
2126
      /* Again, being conservative */
2127
2.87k
      diff = HALF32(diff);
2128
2.87k
      diff = MAX32(MIN32(diff, GCONST(.031f)), -GCONST(.031f));
2129
      /* Find the band that's in the middle of the coded spectrum */
2130
12.9k
      for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++);
2131
2.87k
      count_dynalloc=0;
2132
22.5k
      for(i=0;i<mask_end;i++)
2133
19.7k
      {
2134
19.7k
         opus_val32 lin;
2135
19.7k
         celt_glog unmask;
2136
19.7k
         lin = mask_avg + diff*(i-midband);
2137
19.7k
         if (C==2)
2138
17.6k
            unmask = MAXG(st->energy_mask[i], st->energy_mask[nbEBands+i]);
2139
2.06k
         else
2140
2.06k
            unmask = st->energy_mask[i];
2141
19.7k
         unmask = MING(unmask, GCONST(.0f));
2142
19.7k
         unmask -= lin;
2143
19.7k
         if (unmask > GCONST(.25f))
2144
2.95k
         {
2145
2.95k
            surround_dynalloc[i] = unmask - GCONST(.25f);
2146
2.95k
            count_dynalloc++;
2147
2.95k
         }
2148
19.7k
      }
2149
2.87k
      if (count_dynalloc>=3)
2150
261
      {
2151
         /* If we need dynalloc in many bands, it's probably because our
2152
            initial masking rate was too low. */
2153
261
         mask_avg += GCONST(.25f);
2154
261
         if (mask_avg>0)
2155
34
         {
2156
            /* Something went really wrong in the original calculations,
2157
               disabling masking. */
2158
34
            mask_avg = 0;
2159
34
            diff = 0;
2160
34
            OPUS_CLEAR(surround_dynalloc, mask_end);
2161
227
         } else {
2162
2.96k
            for(i=0;i<mask_end;i++)
2163
2.74k
               surround_dynalloc[i] = MAXG(0, surround_dynalloc[i]-GCONST(.25f));
2164
227
         }
2165
261
      }
2166
2.87k
      mask_avg += GCONST(.2f);
2167
      /* Convert to 1/64th units used for the trim */
2168
2.87k
      surround_trim = 64*diff;
2169
      /*printf("%d %d ", mask_avg, surround_trim);*/
2170
2.87k
      surround_masking = mask_avg;
2171
2.87k
   }
2172
   /* Temporal VBR (but not for LFE) */
2173
77.7k
   if (!st->lfe)
2174
77.4k
   {
2175
77.4k
      celt_glog follow=-QCONST32(10.0f, DB_SHIFT-5);
2176
77.4k
      opus_val32 frame_avg=0;
2177
77.4k
      celt_glog offset = shortBlocks?HALF32(SHL32(LM, DB_SHIFT-5)):0;
2178
1.15M
      for(i=start;i<end;i++)
2179
1.07M
      {
2180
1.07M
         follow = MAXG(follow-QCONST32(1.0f, DB_SHIFT-5), SHR32(bandLogE[i],5)-offset);
2181
1.07M
         if (C==2)
2182
316k
            follow = MAXG(follow, SHR32(bandLogE[i+nbEBands],5)-offset);
2183
1.07M
         frame_avg += follow;
2184
1.07M
      }
2185
77.4k
      frame_avg /= (end-start);
2186
77.4k
      temporal_vbr = SUB32(SHL32(frame_avg, 5),st->spec_avg);
2187
77.4k
      temporal_vbr = MING(GCONST(3.f), MAXG(-GCONST(1.5f), temporal_vbr));
2188
77.4k
      st->spec_avg += MULT16_32_Q15(QCONST16(.02f, 15), temporal_vbr);
2189
77.4k
   }
2190
   /*for (i=0;i<21;i++)
2191
      printf("%f ", bandLogE[i]);
2192
   printf("\n");*/
2193
2194
77.7k
   if (!secondMdct)
2195
63.0k
   {
2196
63.0k
      OPUS_COPY(bandLogE2, bandLogE, C*nbEBands);
2197
63.0k
   }
2198
2199
   /* Last chance to catch any transient we might have missed in the
2200
      time-domain analysis */
2201
77.7k
   if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5 && !st->lfe && !hybrid)
2202
4.47k
   {
2203
4.47k
      if (patch_transient_decision(bandLogE, oldBandE, nbEBands, start, end, C))
2204
264
      {
2205
264
         isTransient = 1;
2206
264
         shortBlocks = M;
2207
264
         compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
2208
264
         compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
2209
264
         amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
2210
         /* Compensate for the scaling of short vs long mdcts */
2211
616
         for (c=0;c<C;c++)
2212
352
         {
2213
5.22k
            for (i=0;i<end;i++)
2214
4.87k
               bandLogE2[nbEBands*c+i] += HALF32(SHL32(LM, DB_SHIFT));
2215
352
         }
2216
264
         tf_estimate = QCONST16(.2f,14);
2217
264
      }
2218
4.47k
   }
2219
2220
77.7k
   if (LM>0 && ec_tell(enc)+3<=total_bits)
2221
35.7k
      ec_enc_bit_logp(enc, isTransient, 3);
2222
2223
77.7k
   ALLOC(X, C*N, celt_norm);         /**< Interleaved normalised MDCTs */
2224
2225
   /* Band normalisation */
2226
77.7k
   normalise_bands(mode, freq, X, bandE, effEnd, C, M);
2227
2228
77.7k
   enable_tf_analysis = effectiveBytes>=15*C && !hybrid && st->complexity>=2 && !st->lfe && toneishness < QCONST32(.98f, 29);
2229
2230
77.7k
   ALLOC(offsets, nbEBands, int);
2231
77.7k
   ALLOC(importance, nbEBands, int);
2232
77.7k
   ALLOC(spread_weight, nbEBands, int);
2233
2234
77.7k
   maxDepth = dynalloc_analysis(bandLogE, bandLogE2, oldBandE, nbEBands, start, end, C, offsets,
2235
77.7k
         st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr,
2236
77.7k
         eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc, &st->analysis, importance, spread_weight, tone_freq, toneishness ARG_QEXT(qext_scale));
2237
2238
77.7k
   ALLOC(tf_res, nbEBands, int);
2239
   /* Disable variable tf resolution for hybrid and at very low bitrate */
2240
77.7k
   if (enable_tf_analysis)
2241
33.3k
   {
2242
33.3k
      int lambda;
2243
33.3k
      lambda = IMAX(80, 20480/effectiveBytes + 2);
2244
33.3k
      tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, LM, tf_estimate, tf_chan, importance);
2245
33.3k
      for (i=effEnd;i<end;i++)
2246
0
         tf_res[i] = tf_res[effEnd-1];
2247
44.4k
   } else if (hybrid && weak_transient)
2248
88
   {
2249
      /* For weak transients, we rely on the fact that improving time resolution using
2250
         TF on a long window is imperfect and will not result in an energy collapse at
2251
         low bitrate. */
2252
1.76k
      for (i=0;i<end;i++)
2253
1.67k
         tf_res[i] = 1;
2254
88
      tf_select=0;
2255
44.3k
   } else if (hybrid && effectiveBytes<15 && st->silk_info.signalType != 2)
2256
1.02k
   {
2257
      /* For low bitrate hybrid, we force temporal resolution to 5 ms rather than 2.5 ms. */
2258
20.4k
      for (i=0;i<end;i++)
2259
19.3k
         tf_res[i] = 0;
2260
1.02k
      tf_select=isTransient;
2261
43.2k
   } else {
2262
673k
      for (i=0;i<end;i++)
2263
629k
         tf_res[i] = isTransient;
2264
43.2k
      tf_select=0;
2265
43.2k
   }
2266
2267
77.7k
   ALLOC(error, C*nbEBands, celt_glog);
2268
77.7k
   c=0;
2269
100k
   do {
2270
1.49M
      for (i=start;i<end;i++)
2271
1.39M
      {
2272
         /* When the energy is stable, slightly bias energy quantization towards
2273
            the previous error to make the gain more stable (a constant offset is
2274
            better than fluctuations). */
2275
1.39M
         if (ABS32(SUB32(bandLogE[i+c*nbEBands], oldBandE[i+c*nbEBands])) < GCONST(2.f))
2276
345k
         {
2277
345k
            bandLogE[i+c*nbEBands] -= MULT16_32_Q15(QCONST16(0.25f, 15), energyError[i+c*nbEBands]);
2278
345k
         }
2279
1.39M
      }
2280
100k
   } while (++c < C);
2281
77.7k
   quant_coarse_energy(mode, start, end, effEnd, bandLogE,
2282
77.7k
         oldBandE, total_bits, error, enc,
2283
77.7k
         C, LM, nbAvailableBytes, st->force_intra,
2284
77.7k
         &st->delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
2285
2286
77.7k
   tf_encode(start, end, isTransient, tf_res, LM, tf_select, enc);
2287
2288
77.7k
   if (ec_tell(enc)+4<=total_bits)
2289
56.2k
   {
2290
56.2k
      if (st->lfe)
2291
284
      {
2292
284
         st->tapset_decision = 0;
2293
284
         st->spread_decision = SPREAD_NORMAL;
2294
55.9k
      } else if (hybrid)
2295
4.27k
      {
2296
4.27k
         if (st->complexity == 0)
2297
373
            st->spread_decision = SPREAD_NONE;
2298
3.90k
         else if (isTransient)
2299
3.16k
            st->spread_decision = SPREAD_NORMAL;
2300
736
         else
2301
736
            st->spread_decision = SPREAD_AGGRESSIVE;
2302
51.6k
      } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
2303
31.5k
      {
2304
31.5k
         if (st->complexity == 0)
2305
5.55k
            st->spread_decision = SPREAD_NONE;
2306
26.0k
         else
2307
26.0k
            st->spread_decision = SPREAD_NORMAL;
2308
31.5k
      } else {
2309
         /* Disable new spreading+tapset estimator until we can show it works
2310
            better than the old one. So far it seems like spreading_decision()
2311
            works best. */
2312
#if 0
2313
         if (st->analysis.valid)
2314
         {
2315
            static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
2316
            static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
2317
            static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), QCONST16(.15f, 15)};
2318
            static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), QCONST16(.05f, 15)};
2319
            st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision);
2320
            st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
2321
         } else
2322
#endif
2323
20.1k
         {
2324
20.1k
            st->spread_decision = spreading_decision(mode, X,
2325
20.1k
                  &st->tonal_average, st->spread_decision, &st->hf_average,
2326
20.1k
                  &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M, spread_weight);
2327
20.1k
         }
2328
         /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
2329
         /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
2330
20.1k
      }
2331
56.2k
      ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
2332
56.2k
   } else {
2333
21.5k
      st->spread_decision = SPREAD_NORMAL;
2334
21.5k
   }
2335
2336
   /* For LFE, everything interesting is in the first band */
2337
77.7k
   if (st->lfe)
2338
358
      offsets[0] = IMIN(8, effectiveBytes/3);
2339
77.7k
   ALLOC(cap, nbEBands, int);
2340
77.7k
   init_caps(mode,cap,LM,C);
2341
2342
77.7k
   dynalloc_logp = 6;
2343
77.7k
   total_bits<<=BITRES;
2344
77.7k
   total_boost = 0;
2345
77.7k
   tell = ec_tell_frac(enc);
2346
1.15M
   for (i=start;i<end;i++)
2347
1.07M
   {
2348
1.07M
      int width, quanta;
2349
1.07M
      int dynalloc_loop_logp;
2350
1.07M
      int boost;
2351
1.07M
      int j;
2352
1.07M
      width = C*(eBands[i+1]-eBands[i])<<LM;
2353
      /* quanta is 6 bits, but no more than 1 bit/sample
2354
         and no less than 1/8 bit/sample */
2355
1.07M
      quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
2356
1.07M
      dynalloc_loop_logp = dynalloc_logp;
2357
1.07M
      boost = 0;
2358
1.17M
      for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
2359
1.17M
            && boost < cap[i]; j++)
2360
855k
      {
2361
855k
         int flag;
2362
855k
         flag = j<offsets[i];
2363
855k
         ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
2364
855k
         tell = ec_tell_frac(enc);
2365
855k
         if (!flag)
2366
757k
            break;
2367
97.1k
         boost += quanta;
2368
97.1k
         total_boost += quanta;
2369
97.1k
         dynalloc_loop_logp = 1;
2370
97.1k
      }
2371
      /* Making dynalloc more likely */
2372
1.07M
      if (j)
2373
38.4k
         dynalloc_logp = IMAX(2, dynalloc_logp-1);
2374
1.07M
      offsets[i] = boost;
2375
1.07M
   }
2376
2377
77.7k
   if (C==2)
2378
23.1k
   {
2379
23.1k
      static const opus_val16 intensity_thresholds[21]=
2380
      /* 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19  20  off*/
2381
23.1k
        {  1, 2, 3, 4, 5, 6, 7, 8,16,24,36,44,50,56,62,67,72,79,88,106,134};
2382
23.1k
      static const opus_val16 intensity_histeresis[21]=
2383
23.1k
        {  1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6,  8, 8};
2384
2385
      /* Always use MS for 2.5 ms frames until we can do a better analysis */
2386
23.1k
      if (LM!=0)
2387
17.5k
         dual_stereo = stereo_analysis(mode, X, LM, N);
2388
2389
23.1k
      st->intensity = hysteresis_decision((opus_val16)(equiv_rate/1000),
2390
23.1k
            intensity_thresholds, intensity_histeresis, 21, st->intensity);
2391
23.1k
      st->intensity = IMIN(end,IMAX(start, st->intensity));
2392
23.1k
   }
2393
2394
77.7k
   alloc_trim = 5;
2395
77.7k
   if (tell+(6<<BITRES) <= total_bits - total_boost)
2396
55.4k
   {
2397
55.4k
      if (start > 0 || st->lfe)
2398
4.45k
      {
2399
4.45k
         st->stereo_saving = 0;
2400
4.45k
         alloc_trim = 5;
2401
50.9k
      } else {
2402
50.9k
         alloc_trim = alloc_trim_analysis(mode, X, bandLogE,
2403
50.9k
            end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate,
2404
50.9k
            st->intensity, surround_trim, equiv_rate, st->arch);
2405
50.9k
      }
2406
55.4k
      ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
2407
55.4k
      tell = ec_tell_frac(enc);
2408
55.4k
   }
2409
2410
   /* In VBR mode the frame size must not be reduced so much that it would
2411
       result in the encoder running out of bits.
2412
      The margin of 2 bytes ensures that none of the bust-prevention logic
2413
       in the decoder will have triggered so far. */
2414
77.7k
   min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2;
2415
   /* Take into account the 37 bits we need to have left in the packet to
2416
      signal a redundant frame in hybrid mode. Creating a shorter packet would
2417
      create an entropy coder desync. */
2418
77.7k
   if (hybrid)
2419
4.32k
      min_allowed = IMAX(min_allowed, (tell0_frac+(37<<BITRES)+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3));
2420
   /* Variable bitrate */
2421
77.7k
   if (vbr_rate>0)
2422
59.0k
   {
2423
59.0k
     opus_val16 alpha;
2424
59.0k
     opus_int32 delta;
2425
     /* The target rate in 8th bits per frame */
2426
59.0k
     opus_int32 target, base_target;
2427
59.0k
     int lm_diff = mode->maxLM - LM;
2428
2429
     /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
2430
        The CELT allocator will just not be able to use more than that anyway. */
2431
59.0k
     nbCompressedBytes = IMIN(nbCompressedBytes,packet_size_cap>>(3-LM));
2432
59.0k
     if (!hybrid)
2433
55.6k
     {
2434
55.6k
        base_target = vbr_rate - ((40*C+20)<<BITRES);
2435
55.6k
     } else {
2436
3.37k
        base_target = IMAX(0, vbr_rate - ((9*C+4)<<BITRES));
2437
3.37k
     }
2438
2439
59.0k
     if (st->constrained_vbr)
2440
25.4k
        base_target += (st->vbr_offset>>lm_diff);
2441
2442
59.0k
     if (!hybrid)
2443
55.6k
     {
2444
55.6k
        target = compute_vbr(mode, &st->analysis, base_target, LM, equiv_rate,
2445
55.6k
           st->lastCodedBands, C, st->intensity, st->constrained_vbr,
2446
55.6k
           st->stereo_saving, tot_boost, tf_estimate, pitch_change, maxDepth,
2447
55.6k
           st->lfe, st->energy_mask!=NULL, surround_masking,
2448
55.6k
           temporal_vbr ARG_QEXT(st->enable_qext));
2449
55.6k
     } else {
2450
3.37k
        target = base_target;
2451
        /* Tonal frames (offset<100) need more bits than noisy (offset>100) ones. */
2452
3.37k
        if (st->silk_info.offset < 100) target += 12 << BITRES >> (3-LM);
2453
3.37k
        if (st->silk_info.offset > 100) target -= 18 << BITRES >> (3-LM);
2454
        /* Boosting bitrate on transients and vowels with significant temporal
2455
           spikes. */
2456
3.37k
        target += (opus_int32)MULT16_16_Q14(tf_estimate-QCONST16(.25f,14), (50<<BITRES));
2457
        /* If we have a strong transient, let's make sure it has enough bits to code
2458
           the first two bands, so that it can use folding rather than noise. */
2459
3.37k
        if (tf_estimate > QCONST16(.7f,14))
2460
2.38k
           target = IMAX(target, 50<<BITRES);
2461
3.37k
     }
2462
     /* The current offset is removed from the target and the space used
2463
        so far is added*/
2464
59.0k
     target=target+tell;
2465
2466
59.0k
     nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
2467
59.0k
     nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
2468
59.0k
     nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes);
2469
2470
     /* By how much did we "miss" the target on that frame */
2471
59.0k
     delta = target - vbr_rate;
2472
2473
59.0k
     target=nbAvailableBytes<<(BITRES+3);
2474
2475
     /*If the frame is silent we don't adjust our drift, otherwise
2476
       the encoder will shoot to very high rates after hitting a
2477
       span of silence, but we do allow the bitres to refill.
2478
       This means that we'll undershoot our target in CVBR/VBR modes
2479
       on files with lots of silence. */
2480
59.0k
     if(silence)
2481
17.7k
     {
2482
17.7k
       nbAvailableBytes = 2;
2483
17.7k
       target = 2*8<<BITRES;
2484
17.7k
       delta = 0;
2485
17.7k
     }
2486
2487
59.0k
     if (st->vbr_count < 970)
2488
59.0k
     {
2489
59.0k
        st->vbr_count++;
2490
59.0k
        alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
2491
59.0k
     } else
2492
0
        alpha = QCONST16(.001f,15);
2493
     /* How many bits have we used in excess of what we're allowed */
2494
59.0k
     if (st->constrained_vbr)
2495
25.4k
        st->vbr_reservoir += target - vbr_rate;
2496
     /*printf ("%d\n", st->vbr_reservoir);*/
2497
2498
     /* Compute the offset we need to apply in order to reach the target */
2499
59.0k
     if (st->constrained_vbr)
2500
25.4k
     {
2501
25.4k
        st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
2502
25.4k
        st->vbr_offset = -st->vbr_drift;
2503
25.4k
     }
2504
     /*printf ("%d\n", st->vbr_drift);*/
2505
2506
59.0k
     if (st->constrained_vbr && st->vbr_reservoir < 0)
2507
17.7k
     {
2508
        /* We're under the min value -- increase rate */
2509
17.7k
        int adjust = (-st->vbr_reservoir)/(8<<BITRES);
2510
        /* Unless we're just coding silence */
2511
17.7k
        nbAvailableBytes += silence?0:adjust;
2512
17.7k
        st->vbr_reservoir = 0;
2513
        /*printf ("+%d\n", adjust);*/
2514
17.7k
     }
2515
59.0k
     nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes);
2516
     /*printf("%d\n", nbCompressedBytes*50*8);*/
2517
     /* This moves the raw bits to take into account the new compressed size */
2518
59.0k
     ec_enc_shrink(enc, nbCompressedBytes);
2519
59.0k
   }
2520
#ifdef ENABLE_QEXT
2521
   if (st->enable_qext) {
2522
      int new_compressedBytes;
2523
      /* Don't give any bits for the first 80 kb/s per channel. Then 80% of the excess. */
2524
      opus_int32 offset = C*80000*frame_size/mode->Fs/8;
2525
      qext_bytes = IMAX(nbCompressedBytes-1275, IMAX(0, (nbCompressedBytes-offset)*4/5));
2526
      padding_len_bytes = (qext_bytes+253)/254;
2527
      qext_bytes = IMIN(qext_bytes, nbCompressedBytes-min_allowed-padding_len_bytes-1);
2528
      padding_len_bytes = (qext_bytes+253)/254;
2529
      if (qext_bytes > 20) {
2530
         new_compressedBytes = nbCompressedBytes-qext_bytes-padding_len_bytes-1;
2531
         ec_enc_shrink(enc, new_compressedBytes);
2532
         if (compressed == NULL) {
2533
            compressed = enc->buf;
2534
         }
2535
         compressed[-1] |= 0x03; /* Code 3 packet */
2536
         enc->buf += 1+padding_len_bytes;
2537
         OPUS_MOVE(compressed+1+padding_len_bytes, compressed, new_compressedBytes);
2538
         compressed[0] = 0x41; /* Set padding */
2539
         for (i=0;i<padding_len_bytes-1;i++) compressed[i+1] = 255;
2540
         compressed[padding_len_bytes] = qext_bytes%254 == 0 ? 254 : qext_bytes%254;
2541
         ext_payload = compressed+padding_len_bytes+1+new_compressedBytes;
2542
         ext_payload[0] = QEXT_EXTENSION_ID<<1;
2543
         ext_payload += 1;
2544
         qext_bytes -= 1;
2545
         OPUS_CLEAR(ext_payload, qext_bytes);
2546
         ec_enc_init(&ext_enc, ext_payload, qext_bytes);
2547
         nbCompressedBytes = new_compressedBytes;
2548
         if (end == nbEBands && (mode->Fs == 48000 ||  mode->Fs == 96000) && (mode->shortMdctSize==120*qext_scale || mode->shortMdctSize==90*qext_scale)) {
2549
            compute_qext_mode(&qext_mode_struct, mode);
2550
            qext_mode = &qext_mode_struct;
2551
            qext_end = (qext_scale == 2) ? NB_QEXT_BANDS : 2;
2552
            ec_enc_bit_logp(&ext_enc, qext_end == NB_QEXT_BANDS, 1);
2553
         }
2554
      } else {
2555
         ec_enc_init(&ext_enc, NULL, 0);
2556
         qext_bytes = 0;
2557
      }
2558
   } else {
2559
      ec_enc_init(&ext_enc, NULL, 0);
2560
   }
2561
#endif
2562
2563
   /* Bit allocation */
2564
77.7k
   ALLOC(fine_quant, nbEBands, int);
2565
77.7k
   ALLOC(pulses, nbEBands, int);
2566
77.7k
   ALLOC(fine_priority, nbEBands, int);
2567
2568
   /* bits =           packet size                    - where we are - safety*/
2569
77.7k
   bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - (opus_int32)ec_tell_frac(enc) - 1;
2570
77.7k
   anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2571
77.7k
   bits -= anti_collapse_rsv;
2572
77.7k
   signalBandwidth = end-1;
2573
77.7k
#ifndef DISABLE_FLOAT_API
2574
77.7k
   if (st->analysis.valid)
2575
13.6k
   {
2576
13.6k
      int min_bandwidth;
2577
13.6k
      if (equiv_rate < (opus_int32)32000*C)
2578
9.48k
         min_bandwidth = 13;
2579
4.17k
      else if (equiv_rate < (opus_int32)48000*C)
2580
1.60k
         min_bandwidth = 16;
2581
2.56k
      else if (equiv_rate < (opus_int32)60000*C)
2582
1.69k
         min_bandwidth = 18;
2583
874
      else  if (equiv_rate < (opus_int32)80000*C)
2584
557
         min_bandwidth = 19;
2585
317
      else
2586
317
         min_bandwidth = 20;
2587
13.6k
      signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth);
2588
13.6k
   }
2589
77.7k
#endif
2590
77.7k
   if (st->lfe)
2591
358
      signalBandwidth = 1;
2592
77.7k
   codedBands = clt_compute_allocation(mode, start, end, offsets, cap,
2593
77.7k
         alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses,
2594
77.7k
         fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBandwidth);
2595
77.7k
   if (st->lastCodedBands)
2596
23.0k
      st->lastCodedBands = IMIN(st->lastCodedBands+1,IMAX(st->lastCodedBands-1,codedBands));
2597
54.7k
   else
2598
54.7k
      st->lastCodedBands = codedBands;
2599
2600
77.7k
   quant_fine_energy(mode, start, end, oldBandE, error, NULL, fine_quant, enc, C);
2601
77.7k
   OPUS_CLEAR(energyError, nbEBands*CC);
2602
#ifdef ENABLE_QEXT
2603
   if (qext_mode)
2604
   {
2605
      /* Don't bias for intra. */
2606
      opus_val32 qext_delayedIntra=0;
2607
      qext_oldBandE = energyError + CC*nbEBands;
2608
      compute_band_energies(qext_mode, freq, qext_bandE, qext_end, C, LM, st->arch);
2609
      normalise_bands(qext_mode, freq, X, qext_bandE, qext_end, C, M);
2610
      amp2Log2(qext_mode, qext_end, qext_end, qext_bandE, qext_bandLogE, C);
2611
      if (C==2) {
2612
         qext_intensity = qext_end;
2613
         qext_dual_stereo = dual_stereo;
2614
         encode_qext_stereo_params(&ext_enc, qext_end, qext_intensity, qext_dual_stereo);
2615
      }
2616
      quant_coarse_energy(qext_mode, 0, qext_end, qext_end, qext_bandLogE,
2617
               qext_oldBandE, qext_bytes*8, qext_error, &ext_enc,
2618
               C, LM, qext_bytes, st->force_intra,
2619
               &qext_delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
2620
   }
2621
   ALLOC(extra_quant, nbEBands+NB_QEXT_BANDS, int);
2622
   ALLOC(extra_pulses, nbEBands+NB_QEXT_BANDS, int);
2623
   ALLOC(error_bak, C*nbEBands, celt_glog);
2624
2625
   qext_bits = ((opus_int32)qext_bytes*8<<BITRES) - (opus_int32)ec_tell_frac(enc) - 1;
2626
   clt_compute_extra_allocation(mode, qext_mode, start, end, qext_end, bandLogE, qext_bandLogE,
2627
         qext_bits, extra_pulses, extra_quant, C, LM, &ext_enc, 1, tone_freq, toneishness);
2628
   OPUS_COPY(error_bak, error, C*nbEBands);
2629
   if (qext_bytes > 0) {
2630
      quant_fine_energy(mode, start, end, oldBandE, error, fine_quant, extra_quant, &ext_enc, C);
2631
   }
2632
#endif
2633
2634
   /* Residual quantisation */
2635
77.7k
   ALLOC(collapse_masks, C*nbEBands, unsigned char);
2636
77.7k
   quant_all_bands(1, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
2637
77.7k
         bandE, pulses, shortBlocks, st->spread_decision,
2638
77.7k
         dual_stereo, st->intensity, tf_res, nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv,
2639
77.7k
         balance, enc, LM, codedBands, &st->rng, st->complexity, st->arch, st->disable_inv
2640
77.7k
         ARG_QEXT(&ext_enc) ARG_QEXT(extra_pulses)
2641
77.7k
         ARG_QEXT(qext_bytes*(8<<BITRES)) ARG_QEXT(cap));
2642
2643
#ifdef ENABLE_QEXT
2644
   if (qext_mode) {
2645
      VARDECL(int, zeros);
2646
      VARDECL(unsigned char, qext_collapse_masks);
2647
      ec_enc dummy_enc;
2648
      int ext_balance;
2649
      ALLOC(zeros, nbEBands, int);
2650
      ALLOC(qext_collapse_masks, C*NB_QEXT_BANDS, unsigned char);
2651
      ec_enc_init(&dummy_enc, NULL, 0);
2652
      OPUS_CLEAR(zeros, end);
2653
      ext_balance = qext_bytes*(8<<BITRES) - ec_tell_frac(&ext_enc);
2654
      for (i=0;i<qext_end;i++) ext_balance -= extra_pulses[nbEBands+i] + C*(extra_quant[nbEBands+1]<<BITRES);
2655
      quant_fine_energy(qext_mode, 0, qext_end, qext_oldBandE, qext_error, NULL, &extra_quant[nbEBands], &ext_enc, C);
2656
      quant_all_bands(1, qext_mode, 0, qext_end, X, C==2 ? X+N : NULL, qext_collapse_masks,
2657
            qext_bandE, &extra_pulses[nbEBands], shortBlocks, st->spread_decision,
2658
            qext_dual_stereo, qext_intensity, zeros, qext_bytes*(8<<BITRES),
2659
            ext_balance, &ext_enc, LM, qext_end, &st->rng, st->complexity, st->arch, st->disable_inv, &dummy_enc, zeros, 0, NULL);
2660
   }
2661
#endif
2662
2663
77.7k
   if (anti_collapse_rsv > 0)
2664
19.8k
   {
2665
19.8k
      anti_collapse_on = st->consec_transient<2;
2666
#ifdef FUZZING
2667
      anti_collapse_on = rand()&0x1;
2668
#endif
2669
19.8k
      ec_enc_bits(enc, anti_collapse_on, 1);
2670
19.8k
   }
2671
77.7k
   if (qext_bytes == 0)
2672
77.7k
      quant_energy_finalise(mode, start, end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
2673
77.7k
   c=0;
2674
100k
   do {
2675
1.49M
      for (i=start;i<end;i++)
2676
1.39M
      {
2677
1.39M
         energyError[i+c*nbEBands] = MAXG(-GCONST(0.5f), MING(GCONST(0.5f), error[i+c*nbEBands]));
2678
1.39M
      }
2679
100k
   } while (++c < C);
2680
#ifdef ENABLE_QEXT
2681
   if (qext_bytes > 0)
2682
      quant_energy_finalise(mode, start, end, NULL, error_bak, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
2683
#endif
2684
77.7k
   if (silence)
2685
20.8k
   {
2686
547k
      for (i=0;i<C*nbEBands;i++)
2687
526k
         oldBandE[i] = -GCONST(28.f);
2688
20.8k
   }
2689
2690
#ifdef RESYNTH
2691
   /* Re-synthesis of the coded audio if required */
2692
   {
2693
      celt_sig *out_mem[2];
2694
2695
      if (anti_collapse_on)
2696
      {
2697
         anti_collapse(mode, X, collapse_masks, LM, C, N,
2698
               start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 1, st->arch);
2699
      }
2700
2701
      c=0; do {
2702
         OPUS_MOVE(st->syn_mem[c], st->syn_mem[c]+N, QEXT_SCALE(DEC_PITCH_BUF_SIZE)-N+overlap/2);
2703
      } while (++c<CC);
2704
2705
      c=0; do {
2706
         out_mem[c] = st->syn_mem[c]+QEXT_SCALE(DEC_PITCH_BUF_SIZE)-N;
2707
      } while (++c<CC);
2708
2709
      celt_synthesis(mode, X, out_mem, oldBandE, start, effEnd,
2710
                     C, CC, isTransient, LM, st->upsample, silence, st->arch ARG_QEXT(qext_mode) ARG_QEXT(qext_oldBandE) ARG_QEXT(qext_end));
2711
2712
      c=0; do {
2713
         st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
2714
         st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
2715
         comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, mode->shortMdctSize,
2716
               st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
2717
               mode->window, overlap, st->arch);
2718
         if (LM!=0)
2719
            comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMdctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize,
2720
                  st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
2721
                  mode->window, overlap, st->arch);
2722
      } while (++c<CC);
2723
2724
      /* We reuse freq[] as scratch space for the de-emphasis */
2725
      deemphasis(out_mem, (opus_res*)pcm, N, CC, st->upsample, mode->preemph, st->preemph_memD, 0);
2726
      st->prefilter_period_old = st->prefilter_period;
2727
      st->prefilter_gain_old = st->prefilter_gain;
2728
      st->prefilter_tapset_old = st->prefilter_tapset;
2729
   }
2730
#endif
2731
2732
77.7k
   st->prefilter_period = pitch_index;
2733
77.7k
   st->prefilter_gain = gain1;
2734
77.7k
   st->prefilter_tapset = prefilter_tapset;
2735
#ifdef RESYNTH
2736
   if (LM!=0)
2737
   {
2738
      st->prefilter_period_old = st->prefilter_period;
2739
      st->prefilter_gain_old = st->prefilter_gain;
2740
      st->prefilter_tapset_old = st->prefilter_tapset;
2741
   }
2742
#endif
2743
2744
77.7k
   if (CC==2&&C==1) {
2745
9.26k
      OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
2746
9.26k
   }
2747
2748
77.7k
   if (!isTransient)
2749
51.2k
   {
2750
51.2k
      OPUS_COPY(oldLogE2, oldLogE, CC*nbEBands);
2751
51.2k
      OPUS_COPY(oldLogE, oldBandE, CC*nbEBands);
2752
51.2k
   } else {
2753
874k
      for (i=0;i<CC*nbEBands;i++)
2754
847k
         oldLogE[i] = MING(oldLogE[i], oldBandE[i]);
2755
26.5k
   }
2756
   /* In case start or end were to change */
2757
77.7k
   c=0; do
2758
110k
   {
2759
219k
      for (i=0;i<start;i++)
2760
109k
      {
2761
109k
         oldBandE[c*nbEBands+i]=0;
2762
109k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
2763
109k
      }
2764
787k
      for (i=end;i<nbEBands;i++)
2765
677k
      {
2766
677k
         oldBandE[c*nbEBands+i]=0;
2767
677k
         oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f);
2768
677k
      }
2769
110k
   } while (++c<CC);
2770
2771
77.7k
   if (isTransient || transient_got_disabled)
2772
68.5k
      st->consec_transient++;
2773
9.21k
   else
2774
9.21k
      st->consec_transient=0;
2775
77.7k
   st->rng = enc->rng;
2776
2777
   /* If there's any room left (can only happen for very high rates),
2778
      it's already filled with zeros */
2779
77.7k
   ec_enc_done(enc);
2780
#ifdef ENABLE_QEXT
2781
   ec_enc_done(&ext_enc);
2782
   if (qext_bytes > 0)
2783
      nbCompressedBytes += padding_len_bytes+2+qext_bytes;
2784
   if (qext_bytes) st->rng = st->rng ^ ext_enc.rng;
2785
   if (ec_get_error(&ext_enc))
2786
      return OPUS_INTERNAL_ERROR;
2787
#endif
2788
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
2789
   if (st->signalling)
2790
      nbCompressedBytes++;
2791
#endif
2792
2793
77.7k
   RESTORE_STACK;
2794
77.7k
   if (ec_get_error(enc))
2795
0
      return OPUS_INTERNAL_ERROR;
2796
77.7k
   else
2797
77.7k
      return nbCompressedBytes;
2798
77.7k
}
2799
2800
2801
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
2802
2803
#if defined(FIXED_POINT) && !defined(ENABLE_RES24)
2804
int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2805
{
2806
   return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
2807
}
2808
#else
2809
int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2810
{
2811
   int j, ret, C, N;
2812
   VARDECL(opus_res, in);
2813
   ALLOC_STACK;
2814
2815
   if (pcm==NULL)
2816
      return OPUS_BAD_ARG;
2817
2818
   C = st->channels;
2819
   N = frame_size;
2820
   ALLOC(in, C*N, opus_res);
2821
2822
   for (j=0;j<C*N;j++)
2823
     in[j] = INT16TORES(pcm[j]);
2824
2825
   ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2826
#ifdef RESYNTH
2827
   for (j=0;j<C*N;j++)
2828
      ((opus_int16*)pcm)[j]=RES2INT16(in[j]);
2829
#endif
2830
   RESTORE_STACK;
2831
   return ret;
2832
}
2833
#endif
2834
2835
2836
#if defined(FIXED_POINT) && defined(ENABLE_RES24)
2837
int opus_custom_encode24(CELTEncoder * OPUS_RESTRICT st, const opus_int32 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2838
{
2839
   return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
2840
}
2841
#else
2842
int opus_custom_encode24(CELTEncoder * OPUS_RESTRICT st, const opus_int32 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2843
{
2844
   int j, ret, C, N;
2845
   VARDECL(opus_res, in);
2846
   ALLOC_STACK;
2847
2848
   if (pcm==NULL)
2849
      return OPUS_BAD_ARG;
2850
2851
   C = st->channels;
2852
   N = frame_size;
2853
   ALLOC(in, C*N, opus_res);
2854
2855
   for (j=0;j<C*N;j++)
2856
     in[j] = INT24TORES(pcm[j]);
2857
2858
   ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2859
#ifdef RESYNTH
2860
   for (j=0;j<C*N;j++)
2861
      ((opus_int32*)pcm)[j]=RES2INT24(in[j]);
2862
#endif
2863
   RESTORE_STACK;
2864
   return ret;
2865
}
2866
#endif
2867
2868
2869
#ifndef DISABLE_FLOAT_API
2870
2871
# if !defined(FIXED_POINT)
2872
int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2873
{
2874
   return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
2875
}
2876
# else
2877
int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2878
{
2879
   int j, ret, C, N;
2880
   VARDECL(opus_res, in);
2881
   ALLOC_STACK;
2882
2883
   if (pcm==NULL)
2884
      return OPUS_BAD_ARG;
2885
2886
   C = st->channels;
2887
   N = frame_size;
2888
   ALLOC(in, C*N, opus_res);
2889
2890
   for (j=0;j<C*N;j++)
2891
     in[j] = FLOAT2RES(pcm[j]);
2892
2893
   ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2894
#ifdef RESYNTH
2895
   for (j=0;j<C*N;j++)
2896
      ((float*)pcm)[j]=RES2FLOAT(in[j]);
2897
#endif
2898
   RESTORE_STACK;
2899
   return ret;
2900
}
2901
# endif
2902
2903
#endif
2904
2905
#endif /* CUSTOM_MODES */
2906
2907
int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...)
2908
11.0M
{
2909
11.0M
   va_list ap;
2910
2911
11.0M
   va_start(ap, request);
2912
11.0M
   switch (request)
2913
11.0M
   {
2914
1.18M
      case OPUS_SET_COMPLEXITY_REQUEST:
2915
1.18M
      {
2916
1.18M
         int value = va_arg(ap, opus_int32);
2917
1.18M
         if (value<0 || value>10)
2918
0
            goto bad_arg;
2919
1.18M
         st->complexity = value;
2920
1.18M
      }
2921
0
      break;
2922
693k
      case CELT_SET_START_BAND_REQUEST:
2923
693k
      {
2924
693k
         opus_int32 value = va_arg(ap, opus_int32);
2925
693k
         if (value<0 || value>=st->mode->nbEBands)
2926
0
            goto bad_arg;
2927
693k
         st->start = value;
2928
693k
      }
2929
0
      break;
2930
693k
      case CELT_SET_END_BAND_REQUEST:
2931
693k
      {
2932
693k
         opus_int32 value = va_arg(ap, opus_int32);
2933
693k
         if (value<1 || value>st->mode->nbEBands)
2934
0
            goto bad_arg;
2935
693k
         st->end = value;
2936
693k
      }
2937
0
      break;
2938
392k
      case CELT_SET_PREDICTION_REQUEST:
2939
392k
      {
2940
392k
         int value = va_arg(ap, opus_int32);
2941
392k
         if (value<0 || value>2)
2942
0
            goto bad_arg;
2943
392k
         st->disable_pf = value<=1;
2944
392k
         st->force_intra = value==0;
2945
392k
      }
2946
0
      break;
2947
591k
      case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2948
591k
      {
2949
591k
         int value = va_arg(ap, opus_int32);
2950
591k
         if (value<0 || value>100)
2951
0
            goto bad_arg;
2952
591k
         st->loss_rate = value;
2953
591k
      }
2954
0
      break;
2955
286k
      case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2956
286k
      {
2957
286k
         opus_int32 value = va_arg(ap, opus_int32);
2958
286k
         st->constrained_vbr = value;
2959
286k
      }
2960
286k
      break;
2961
661k
      case OPUS_SET_VBR_REQUEST:
2962
661k
      {
2963
661k
         opus_int32 value = va_arg(ap, opus_int32);
2964
661k
         st->vbr = value;
2965
661k
      }
2966
661k
      break;
2967
980k
      case OPUS_SET_BITRATE_REQUEST:
2968
980k
      {
2969
980k
         opus_int32 value = va_arg(ap, opus_int32);
2970
980k
         if (value<=500 && value!=OPUS_BITRATE_MAX)
2971
0
            goto bad_arg;
2972
980k
         value = IMIN(value, 750000*st->channels);
2973
980k
         st->bitrate = value;
2974
980k
      }
2975
0
      break;
2976
693k
      case CELT_SET_CHANNELS_REQUEST:
2977
693k
      {
2978
693k
         opus_int32 value = va_arg(ap, opus_int32);
2979
693k
         if (value<1 || value>2)
2980
0
            goto bad_arg;
2981
693k
         st->stream_channels = value;
2982
693k
      }
2983
0
      break;
2984
475k
      case OPUS_SET_LSB_DEPTH_REQUEST:
2985
475k
      {
2986
475k
          opus_int32 value = va_arg(ap, opus_int32);
2987
475k
          if (value<8 || value>24)
2988
0
             goto bad_arg;
2989
475k
          st->lsb_depth=value;
2990
475k
      }
2991
0
      break;
2992
0
      case OPUS_GET_LSB_DEPTH_REQUEST:
2993
0
      {
2994
0
          opus_int32 *value = va_arg(ap, opus_int32*);
2995
0
          *value=st->lsb_depth;
2996
0
      }
2997
0
      break;
2998
591k
      case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
2999
591k
      {
3000
591k
          opus_int32 value = va_arg(ap, opus_int32);
3001
591k
          if(value<0 || value>1)
3002
0
          {
3003
0
             goto bad_arg;
3004
0
          }
3005
591k
          st->disable_inv = value;
3006
591k
      }
3007
0
      break;
3008
0
      case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
3009
0
      {
3010
0
          opus_int32 *value = va_arg(ap, opus_int32*);
3011
0
          if (!value)
3012
0
          {
3013
0
             goto bad_arg;
3014
0
          }
3015
0
          *value = st->disable_inv;
3016
0
      }
3017
0
      break;
3018
#ifdef ENABLE_QEXT
3019
457k
      case OPUS_SET_QEXT_REQUEST:
3020
457k
      {
3021
457k
          opus_int32 value = va_arg(ap, opus_int32);
3022
457k
          if(value<0 || value>1)
3023
0
          {
3024
0
             goto bad_arg;
3025
0
          }
3026
457k
          st->enable_qext = value;
3027
457k
      }
3028
0
      break;
3029
0
      case OPUS_GET_QEXT_REQUEST:
3030
0
      {
3031
0
          opus_int32 *value = va_arg(ap, opus_int32*);
3032
0
          if (!value)
3033
0
          {
3034
0
             goto bad_arg;
3035
0
          }
3036
0
          *value = st->enable_qext;
3037
0
      }
3038
0
      break;
3039
0
#endif
3040
591k
      case OPUS_RESET_STATE:
3041
591k
      {
3042
591k
         int i;
3043
591k
         celt_glog *oldBandE, *oldLogE, *oldLogE2;
3044
591k
         oldBandE = (celt_glog*)(st->in_mem+st->channels*(st->mode->overlap+QEXT_SCALE2(COMBFILTER_MAXPERIOD, st->qext_scale)));
3045
591k
         oldLogE = oldBandE + st->channels*st->mode->nbEBands;
3046
591k
         oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
3047
591k
         OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
3048
591k
               opus_custom_encoder_get_size(st->mode, st->channels)-
3049
591k
               ((char*)&st->ENCODER_RESET_START - (char*)st));
3050
17.9M
         for (i=0;i<st->channels*st->mode->nbEBands;i++)
3051
17.3M
            oldLogE[i]=oldLogE2[i]=-GCONST(28.f);
3052
591k
         st->vbr_offset = 0;
3053
591k
         st->delayedIntra = 1;
3054
591k
         st->spread_decision = SPREAD_NORMAL;
3055
591k
         st->tonal_average = 256;
3056
591k
         st->hf_average = 0;
3057
591k
         st->tapset_decision = 0;
3058
591k
      }
3059
591k
      break;
3060
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
3061
      case CELT_SET_INPUT_CLIPPING_REQUEST:
3062
      {
3063
         opus_int32 value = va_arg(ap, opus_int32);
3064
         st->clip = value;
3065
      }
3066
      break;
3067
#endif
3068
591k
      case CELT_SET_SIGNALLING_REQUEST:
3069
591k
      {
3070
591k
         opus_int32 value = va_arg(ap, opus_int32);
3071
591k
         st->signalling = value;
3072
591k
      }
3073
591k
      break;
3074
392k
      case CELT_SET_ANALYSIS_REQUEST:
3075
392k
      {
3076
392k
         AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
3077
392k
         if (info)
3078
392k
            OPUS_COPY(&st->analysis, info, 1);
3079
392k
      }
3080
392k
      break;
3081
23.5k
      case CELT_SET_SILK_INFO_REQUEST:
3082
23.5k
      {
3083
23.5k
         SILKInfo *info = va_arg(ap, SILKInfo *);
3084
23.5k
         if (info)
3085
23.5k
            OPUS_COPY(&st->silk_info, info, 1);
3086
23.5k
      }
3087
23.5k
      break;
3088
1.32M
      case CELT_GET_MODE_REQUEST:
3089
1.32M
      {
3090
1.32M
         const CELTMode ** value = va_arg(ap, const CELTMode**);
3091
1.32M
         if (value==0)
3092
0
            goto bad_arg;
3093
1.32M
         *value=st->mode;
3094
1.32M
      }
3095
0
      break;
3096
392k
      case OPUS_GET_FINAL_RANGE_REQUEST:
3097
392k
      {
3098
392k
         opus_uint32 * value = va_arg(ap, opus_uint32 *);
3099
392k
         if (value==0)
3100
0
            goto bad_arg;
3101
392k
         *value=st->rng;
3102
392k
      }
3103
0
      break;
3104
1.26k
      case OPUS_SET_LFE_REQUEST:
3105
1.26k
      {
3106
1.26k
          opus_int32 value = va_arg(ap, opus_int32);
3107
1.26k
          st->lfe = value;
3108
1.26k
      }
3109
1.26k
      break;
3110
9.33k
      case OPUS_SET_ENERGY_MASK_REQUEST:
3111
9.33k
      {
3112
9.33k
         celt_glog *value = va_arg(ap, celt_glog*);
3113
9.33k
          st->energy_mask = value;
3114
9.33k
      }
3115
9.33k
      break;
3116
0
      default:
3117
0
         goto bad_request;
3118
11.0M
   }
3119
11.0M
   va_end(ap);
3120
11.0M
   return OPUS_OK;
3121
0
bad_arg:
3122
0
   va_end(ap);
3123
0
   return OPUS_BAD_ARG;
3124
0
bad_request:
3125
0
   va_end(ap);
3126
0
   return OPUS_UNIMPLEMENTED;
3127
11.0M
}
opus_custom_encoder_ctl
Line
Count
Source
2908
3.41M
{
2909
3.41M
   va_list ap;
2910
2911
3.41M
   va_start(ap, request);
2912
3.41M
   switch (request)
2913
3.41M
   {
2914
354k
      case OPUS_SET_COMPLEXITY_REQUEST:
2915
354k
      {
2916
354k
         int value = va_arg(ap, opus_int32);
2917
354k
         if (value<0 || value>10)
2918
0
            goto bad_arg;
2919
354k
         st->complexity = value;
2920
354k
      }
2921
0
      break;
2922
210k
      case CELT_SET_START_BAND_REQUEST:
2923
210k
      {
2924
210k
         opus_int32 value = va_arg(ap, opus_int32);
2925
210k
         if (value<0 || value>=st->mode->nbEBands)
2926
0
            goto bad_arg;
2927
210k
         st->start = value;
2928
210k
      }
2929
0
      break;
2930
210k
      case CELT_SET_END_BAND_REQUEST:
2931
210k
      {
2932
210k
         opus_int32 value = va_arg(ap, opus_int32);
2933
210k
         if (value<1 || value>st->mode->nbEBands)
2934
0
            goto bad_arg;
2935
210k
         st->end = value;
2936
210k
      }
2937
0
      break;
2938
118k
      case CELT_SET_PREDICTION_REQUEST:
2939
118k
      {
2940
118k
         int value = va_arg(ap, opus_int32);
2941
118k
         if (value<0 || value>2)
2942
0
            goto bad_arg;
2943
118k
         st->disable_pf = value<=1;
2944
118k
         st->force_intra = value==0;
2945
118k
      }
2946
0
      break;
2947
177k
      case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2948
177k
      {
2949
177k
         int value = va_arg(ap, opus_int32);
2950
177k
         if (value<0 || value>100)
2951
0
            goto bad_arg;
2952
177k
         st->loss_rate = value;
2953
177k
      }
2954
0
      break;
2955
84.0k
      case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2956
84.0k
      {
2957
84.0k
         opus_int32 value = va_arg(ap, opus_int32);
2958
84.0k
         st->constrained_vbr = value;
2959
84.0k
      }
2960
84.0k
      break;
2961
197k
      case OPUS_SET_VBR_REQUEST:
2962
197k
      {
2963
197k
         opus_int32 value = va_arg(ap, opus_int32);
2964
197k
         st->vbr = value;
2965
197k
      }
2966
197k
      break;
2967
294k
      case OPUS_SET_BITRATE_REQUEST:
2968
294k
      {
2969
294k
         opus_int32 value = va_arg(ap, opus_int32);
2970
294k
         if (value<=500 && value!=OPUS_BITRATE_MAX)
2971
0
            goto bad_arg;
2972
294k
         value = IMIN(value, 750000*st->channels);
2973
294k
         st->bitrate = value;
2974
294k
      }
2975
0
      break;
2976
210k
      case CELT_SET_CHANNELS_REQUEST:
2977
210k
      {
2978
210k
         opus_int32 value = va_arg(ap, opus_int32);
2979
210k
         if (value<1 || value>2)
2980
0
            goto bad_arg;
2981
210k
         st->stream_channels = value;
2982
210k
      }
2983
0
      break;
2984
143k
      case OPUS_SET_LSB_DEPTH_REQUEST:
2985
143k
      {
2986
143k
          opus_int32 value = va_arg(ap, opus_int32);
2987
143k
          if (value<8 || value>24)
2988
0
             goto bad_arg;
2989
143k
          st->lsb_depth=value;
2990
143k
      }
2991
0
      break;
2992
0
      case OPUS_GET_LSB_DEPTH_REQUEST:
2993
0
      {
2994
0
          opus_int32 *value = va_arg(ap, opus_int32*);
2995
0
          *value=st->lsb_depth;
2996
0
      }
2997
0
      break;
2998
177k
      case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
2999
177k
      {
3000
177k
          opus_int32 value = va_arg(ap, opus_int32);
3001
177k
          if(value<0 || value>1)
3002
0
          {
3003
0
             goto bad_arg;
3004
0
          }
3005
177k
          st->disable_inv = value;
3006
177k
      }
3007
0
      break;
3008
0
      case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
3009
0
      {
3010
0
          opus_int32 *value = va_arg(ap, opus_int32*);
3011
0
          if (!value)
3012
0
          {
3013
0
             goto bad_arg;
3014
0
          }
3015
0
          *value = st->disable_inv;
3016
0
      }
3017
0
      break;
3018
0
#ifdef ENABLE_QEXT
3019
228k
      case OPUS_SET_QEXT_REQUEST:
3020
228k
      {
3021
228k
          opus_int32 value = va_arg(ap, opus_int32);
3022
228k
          if(value<0 || value>1)
3023
0
          {
3024
0
             goto bad_arg;
3025
0
          }
3026
228k
          st->enable_qext = value;
3027
228k
      }
3028
0
      break;
3029
0
      case OPUS_GET_QEXT_REQUEST:
3030
0
      {
3031
0
          opus_int32 *value = va_arg(ap, opus_int32*);
3032
0
          if (!value)
3033
0
          {
3034
0
             goto bad_arg;
3035
0
          }
3036
0
          *value = st->enable_qext;
3037
0
      }
3038
0
      break;
3039
0
#endif
3040
177k
      case OPUS_RESET_STATE:
3041
177k
      {
3042
177k
         int i;
3043
177k
         celt_glog *oldBandE, *oldLogE, *oldLogE2;
3044
177k
         oldBandE = (celt_glog*)(st->in_mem+st->channels*(st->mode->overlap+QEXT_SCALE2(COMBFILTER_MAXPERIOD, st->qext_scale)));
3045
177k
         oldLogE = oldBandE + st->channels*st->mode->nbEBands;
3046
177k
         oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
3047
177k
         OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
3048
177k
               opus_custom_encoder_get_size(st->mode, st->channels)-
3049
177k
               ((char*)&st->ENCODER_RESET_START - (char*)st));
3050
5.59M
         for (i=0;i<st->channels*st->mode->nbEBands;i++)
3051
5.42M
            oldLogE[i]=oldLogE2[i]=-GCONST(28.f);
3052
177k
         st->vbr_offset = 0;
3053
177k
         st->delayedIntra = 1;
3054
177k
         st->spread_decision = SPREAD_NORMAL;
3055
177k
         st->tonal_average = 256;
3056
177k
         st->hf_average = 0;
3057
177k
         st->tapset_decision = 0;
3058
177k
      }
3059
177k
      break;
3060
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
3061
      case CELT_SET_INPUT_CLIPPING_REQUEST:
3062
      {
3063
         opus_int32 value = va_arg(ap, opus_int32);
3064
         st->clip = value;
3065
      }
3066
      break;
3067
#endif
3068
177k
      case CELT_SET_SIGNALLING_REQUEST:
3069
177k
      {
3070
177k
         opus_int32 value = va_arg(ap, opus_int32);
3071
177k
         st->signalling = value;
3072
177k
      }
3073
177k
      break;
3074
118k
      case CELT_SET_ANALYSIS_REQUEST:
3075
118k
      {
3076
118k
         AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
3077
118k
         if (info)
3078
118k
            OPUS_COPY(&st->analysis, info, 1);
3079
118k
      }
3080
118k
      break;
3081
7.37k
      case CELT_SET_SILK_INFO_REQUEST:
3082
7.37k
      {
3083
7.37k
         SILKInfo *info = va_arg(ap, SILKInfo *);
3084
7.37k
         if (info)
3085
7.37k
            OPUS_COPY(&st->silk_info, info, 1);
3086
7.37k
      }
3087
7.37k
      break;
3088
403k
      case CELT_GET_MODE_REQUEST:
3089
403k
      {
3090
403k
         const CELTMode ** value = va_arg(ap, const CELTMode**);
3091
403k
         if (value==0)
3092
0
            goto bad_arg;
3093
403k
         *value=st->mode;
3094
403k
      }
3095
0
      break;
3096
118k
      case OPUS_GET_FINAL_RANGE_REQUEST:
3097
118k
      {
3098
118k
         opus_uint32 * value = va_arg(ap, opus_uint32 *);
3099
118k
         if (value==0)
3100
0
            goto bad_arg;
3101
118k
         *value=st->rng;
3102
118k
      }
3103
0
      break;
3104
394
      case OPUS_SET_LFE_REQUEST:
3105
394
      {
3106
394
          opus_int32 value = va_arg(ap, opus_int32);
3107
394
          st->lfe = value;
3108
394
      }
3109
394
      break;
3110
2.98k
      case OPUS_SET_ENERGY_MASK_REQUEST:
3111
2.98k
      {
3112
2.98k
         celt_glog *value = va_arg(ap, celt_glog*);
3113
2.98k
          st->energy_mask = value;
3114
2.98k
      }
3115
2.98k
      break;
3116
0
      default:
3117
0
         goto bad_request;
3118
3.41M
   }
3119
3.41M
   va_end(ap);
3120
3.41M
   return OPUS_OK;
3121
0
bad_arg:
3122
0
   va_end(ap);
3123
0
   return OPUS_BAD_ARG;
3124
0
bad_request:
3125
0
   va_end(ap);
3126
0
   return OPUS_UNIMPLEMENTED;
3127
3.41M
}
opus_custom_encoder_ctl
Line
Count
Source
2908
3.41M
{
2909
3.41M
   va_list ap;
2910
2911
3.41M
   va_start(ap, request);
2912
3.41M
   switch (request)
2913
3.41M
   {
2914
354k
      case OPUS_SET_COMPLEXITY_REQUEST:
2915
354k
      {
2916
354k
         int value = va_arg(ap, opus_int32);
2917
354k
         if (value<0 || value>10)
2918
0
            goto bad_arg;
2919
354k
         st->complexity = value;
2920
354k
      }
2921
0
      break;
2922
210k
      case CELT_SET_START_BAND_REQUEST:
2923
210k
      {
2924
210k
         opus_int32 value = va_arg(ap, opus_int32);
2925
210k
         if (value<0 || value>=st->mode->nbEBands)
2926
0
            goto bad_arg;
2927
210k
         st->start = value;
2928
210k
      }
2929
0
      break;
2930
210k
      case CELT_SET_END_BAND_REQUEST:
2931
210k
      {
2932
210k
         opus_int32 value = va_arg(ap, opus_int32);
2933
210k
         if (value<1 || value>st->mode->nbEBands)
2934
0
            goto bad_arg;
2935
210k
         st->end = value;
2936
210k
      }
2937
0
      break;
2938
118k
      case CELT_SET_PREDICTION_REQUEST:
2939
118k
      {
2940
118k
         int value = va_arg(ap, opus_int32);
2941
118k
         if (value<0 || value>2)
2942
0
            goto bad_arg;
2943
118k
         st->disable_pf = value<=1;
2944
118k
         st->force_intra = value==0;
2945
118k
      }
2946
0
      break;
2947
177k
      case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2948
177k
      {
2949
177k
         int value = va_arg(ap, opus_int32);
2950
177k
         if (value<0 || value>100)
2951
0
            goto bad_arg;
2952
177k
         st->loss_rate = value;
2953
177k
      }
2954
0
      break;
2955
84.0k
      case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2956
84.0k
      {
2957
84.0k
         opus_int32 value = va_arg(ap, opus_int32);
2958
84.0k
         st->constrained_vbr = value;
2959
84.0k
      }
2960
84.0k
      break;
2961
197k
      case OPUS_SET_VBR_REQUEST:
2962
197k
      {
2963
197k
         opus_int32 value = va_arg(ap, opus_int32);
2964
197k
         st->vbr = value;
2965
197k
      }
2966
197k
      break;
2967
294k
      case OPUS_SET_BITRATE_REQUEST:
2968
294k
      {
2969
294k
         opus_int32 value = va_arg(ap, opus_int32);
2970
294k
         if (value<=500 && value!=OPUS_BITRATE_MAX)
2971
0
            goto bad_arg;
2972
294k
         value = IMIN(value, 750000*st->channels);
2973
294k
         st->bitrate = value;
2974
294k
      }
2975
0
      break;
2976
210k
      case CELT_SET_CHANNELS_REQUEST:
2977
210k
      {
2978
210k
         opus_int32 value = va_arg(ap, opus_int32);
2979
210k
         if (value<1 || value>2)
2980
0
            goto bad_arg;
2981
210k
         st->stream_channels = value;
2982
210k
      }
2983
0
      break;
2984
143k
      case OPUS_SET_LSB_DEPTH_REQUEST:
2985
143k
      {
2986
143k
          opus_int32 value = va_arg(ap, opus_int32);
2987
143k
          if (value<8 || value>24)
2988
0
             goto bad_arg;
2989
143k
          st->lsb_depth=value;
2990
143k
      }
2991
0
      break;
2992
0
      case OPUS_GET_LSB_DEPTH_REQUEST:
2993
0
      {
2994
0
          opus_int32 *value = va_arg(ap, opus_int32*);
2995
0
          *value=st->lsb_depth;
2996
0
      }
2997
0
      break;
2998
177k
      case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
2999
177k
      {
3000
177k
          opus_int32 value = va_arg(ap, opus_int32);
3001
177k
          if(value<0 || value>1)
3002
0
          {
3003
0
             goto bad_arg;
3004
0
          }
3005
177k
          st->disable_inv = value;
3006
177k
      }
3007
0
      break;
3008
0
      case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
3009
0
      {
3010
0
          opus_int32 *value = va_arg(ap, opus_int32*);
3011
0
          if (!value)
3012
0
          {
3013
0
             goto bad_arg;
3014
0
          }
3015
0
          *value = st->disable_inv;
3016
0
      }
3017
0
      break;
3018
0
#ifdef ENABLE_QEXT
3019
228k
      case OPUS_SET_QEXT_REQUEST:
3020
228k
      {
3021
228k
          opus_int32 value = va_arg(ap, opus_int32);
3022
228k
          if(value<0 || value>1)
3023
0
          {
3024
0
             goto bad_arg;
3025
0
          }
3026
228k
          st->enable_qext = value;
3027
228k
      }
3028
0
      break;
3029
0
      case OPUS_GET_QEXT_REQUEST:
3030
0
      {
3031
0
          opus_int32 *value = va_arg(ap, opus_int32*);
3032
0
          if (!value)
3033
0
          {
3034
0
             goto bad_arg;
3035
0
          }
3036
0
          *value = st->enable_qext;
3037
0
      }
3038
0
      break;
3039
0
#endif
3040
177k
      case OPUS_RESET_STATE:
3041
177k
      {
3042
177k
         int i;
3043
177k
         celt_glog *oldBandE, *oldLogE, *oldLogE2;
3044
177k
         oldBandE = (celt_glog*)(st->in_mem+st->channels*(st->mode->overlap+QEXT_SCALE2(COMBFILTER_MAXPERIOD, st->qext_scale)));
3045
177k
         oldLogE = oldBandE + st->channels*st->mode->nbEBands;
3046
177k
         oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
3047
177k
         OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
3048
177k
               opus_custom_encoder_get_size(st->mode, st->channels)-
3049
177k
               ((char*)&st->ENCODER_RESET_START - (char*)st));
3050
5.59M
         for (i=0;i<st->channels*st->mode->nbEBands;i++)
3051
5.42M
            oldLogE[i]=oldLogE2[i]=-GCONST(28.f);
3052
177k
         st->vbr_offset = 0;
3053
177k
         st->delayedIntra = 1;
3054
177k
         st->spread_decision = SPREAD_NORMAL;
3055
177k
         st->tonal_average = 256;
3056
177k
         st->hf_average = 0;
3057
177k
         st->tapset_decision = 0;
3058
177k
      }
3059
177k
      break;
3060
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
3061
      case CELT_SET_INPUT_CLIPPING_REQUEST:
3062
      {
3063
         opus_int32 value = va_arg(ap, opus_int32);
3064
         st->clip = value;
3065
      }
3066
      break;
3067
#endif
3068
177k
      case CELT_SET_SIGNALLING_REQUEST:
3069
177k
      {
3070
177k
         opus_int32 value = va_arg(ap, opus_int32);
3071
177k
         st->signalling = value;
3072
177k
      }
3073
177k
      break;
3074
118k
      case CELT_SET_ANALYSIS_REQUEST:
3075
118k
      {
3076
118k
         AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
3077
118k
         if (info)
3078
118k
            OPUS_COPY(&st->analysis, info, 1);
3079
118k
      }
3080
118k
      break;
3081
7.37k
      case CELT_SET_SILK_INFO_REQUEST:
3082
7.37k
      {
3083
7.37k
         SILKInfo *info = va_arg(ap, SILKInfo *);
3084
7.37k
         if (info)
3085
7.37k
            OPUS_COPY(&st->silk_info, info, 1);
3086
7.37k
      }
3087
7.37k
      break;
3088
403k
      case CELT_GET_MODE_REQUEST:
3089
403k
      {
3090
403k
         const CELTMode ** value = va_arg(ap, const CELTMode**);
3091
403k
         if (value==0)
3092
0
            goto bad_arg;
3093
403k
         *value=st->mode;
3094
403k
      }
3095
0
      break;
3096
118k
      case OPUS_GET_FINAL_RANGE_REQUEST:
3097
118k
      {
3098
118k
         opus_uint32 * value = va_arg(ap, opus_uint32 *);
3099
118k
         if (value==0)
3100
0
            goto bad_arg;
3101
118k
         *value=st->rng;
3102
118k
      }
3103
0
      break;
3104
394
      case OPUS_SET_LFE_REQUEST:
3105
394
      {
3106
394
          opus_int32 value = va_arg(ap, opus_int32);
3107
394
          st->lfe = value;
3108
394
      }
3109
394
      break;
3110
2.98k
      case OPUS_SET_ENERGY_MASK_REQUEST:
3111
2.98k
      {
3112
2.98k
         celt_glog *value = va_arg(ap, celt_glog*);
3113
2.98k
          st->energy_mask = value;
3114
2.98k
      }
3115
2.98k
      break;
3116
0
      default:
3117
0
         goto bad_request;
3118
3.41M
   }
3119
3.41M
   va_end(ap);
3120
3.41M
   return OPUS_OK;
3121
0
bad_arg:
3122
0
   va_end(ap);
3123
0
   return OPUS_BAD_ARG;
3124
0
bad_request:
3125
0
   va_end(ap);
3126
0
   return OPUS_UNIMPLEMENTED;
3127
3.41M
}
opus_custom_encoder_ctl
Line
Count
Source
2908
2.10M
{
2909
2.10M
   va_list ap;
2910
2911
2.10M
   va_start(ap, request);
2912
2.10M
   switch (request)
2913
2.10M
   {
2914
237k
      case OPUS_SET_COMPLEXITY_REQUEST:
2915
237k
      {
2916
237k
         int value = va_arg(ap, opus_int32);
2917
237k
         if (value<0 || value>10)
2918
0
            goto bad_arg;
2919
237k
         st->complexity = value;
2920
237k
      }
2921
0
      break;
2922
136k
      case CELT_SET_START_BAND_REQUEST:
2923
136k
      {
2924
136k
         opus_int32 value = va_arg(ap, opus_int32);
2925
136k
         if (value<0 || value>=st->mode->nbEBands)
2926
0
            goto bad_arg;
2927
136k
         st->start = value;
2928
136k
      }
2929
0
      break;
2930
136k
      case CELT_SET_END_BAND_REQUEST:
2931
136k
      {
2932
136k
         opus_int32 value = va_arg(ap, opus_int32);
2933
136k
         if (value<1 || value>st->mode->nbEBands)
2934
0
            goto bad_arg;
2935
136k
         st->end = value;
2936
136k
      }
2937
0
      break;
2938
77.8k
      case CELT_SET_PREDICTION_REQUEST:
2939
77.8k
      {
2940
77.8k
         int value = va_arg(ap, opus_int32);
2941
77.8k
         if (value<0 || value>2)
2942
0
            goto bad_arg;
2943
77.8k
         st->disable_pf = value<=1;
2944
77.8k
         st->force_intra = value==0;
2945
77.8k
      }
2946
0
      break;
2947
118k
      case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2948
118k
      {
2949
118k
         int value = va_arg(ap, opus_int32);
2950
118k
         if (value<0 || value>100)
2951
0
            goto bad_arg;
2952
118k
         st->loss_rate = value;
2953
118k
      }
2954
0
      break;
2955
59.0k
      case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2956
59.0k
      {
2957
59.0k
         opus_int32 value = va_arg(ap, opus_int32);
2958
59.0k
         st->constrained_vbr = value;
2959
59.0k
      }
2960
59.0k
      break;
2961
133k
      case OPUS_SET_VBR_REQUEST:
2962
133k
      {
2963
133k
         opus_int32 value = va_arg(ap, opus_int32);
2964
133k
         st->vbr = value;
2965
133k
      }
2966
133k
      break;
2967
195k
      case OPUS_SET_BITRATE_REQUEST:
2968
195k
      {
2969
195k
         opus_int32 value = va_arg(ap, opus_int32);
2970
195k
         if (value<=500 && value!=OPUS_BITRATE_MAX)
2971
0
            goto bad_arg;
2972
195k
         value = IMIN(value, 750000*st->channels);
2973
195k
         st->bitrate = value;
2974
195k
      }
2975
0
      break;
2976
136k
      case CELT_SET_CHANNELS_REQUEST:
2977
136k
      {
2978
136k
         opus_int32 value = va_arg(ap, opus_int32);
2979
136k
         if (value<1 || value>2)
2980
0
            goto bad_arg;
2981
136k
         st->stream_channels = value;
2982
136k
      }
2983
0
      break;
2984
94.6k
      case OPUS_SET_LSB_DEPTH_REQUEST:
2985
94.6k
      {
2986
94.6k
          opus_int32 value = va_arg(ap, opus_int32);
2987
94.6k
          if (value<8 || value>24)
2988
0
             goto bad_arg;
2989
94.6k
          st->lsb_depth=value;
2990
94.6k
      }
2991
0
      break;
2992
0
      case OPUS_GET_LSB_DEPTH_REQUEST:
2993
0
      {
2994
0
          opus_int32 *value = va_arg(ap, opus_int32*);
2995
0
          *value=st->lsb_depth;
2996
0
      }
2997
0
      break;
2998
118k
      case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
2999
118k
      {
3000
118k
          opus_int32 value = va_arg(ap, opus_int32);
3001
118k
          if(value<0 || value>1)
3002
0
          {
3003
0
             goto bad_arg;
3004
0
          }
3005
118k
          st->disable_inv = value;
3006
118k
      }
3007
0
      break;
3008
0
      case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
3009
0
      {
3010
0
          opus_int32 *value = va_arg(ap, opus_int32*);
3011
0
          if (!value)
3012
0
          {
3013
0
             goto bad_arg;
3014
0
          }
3015
0
          *value = st->disable_inv;
3016
0
      }
3017
0
      break;
3018
#ifdef ENABLE_QEXT
3019
      case OPUS_SET_QEXT_REQUEST:
3020
      {
3021
          opus_int32 value = va_arg(ap, opus_int32);
3022
          if(value<0 || value>1)
3023
          {
3024
             goto bad_arg;
3025
          }
3026
          st->enable_qext = value;
3027
      }
3028
      break;
3029
      case OPUS_GET_QEXT_REQUEST:
3030
      {
3031
          opus_int32 *value = va_arg(ap, opus_int32*);
3032
          if (!value)
3033
          {
3034
             goto bad_arg;
3035
          }
3036
          *value = st->enable_qext;
3037
      }
3038
      break;
3039
#endif
3040
118k
      case OPUS_RESET_STATE:
3041
118k
      {
3042
118k
         int i;
3043
118k
         celt_glog *oldBandE, *oldLogE, *oldLogE2;
3044
118k
         oldBandE = (celt_glog*)(st->in_mem+st->channels*(st->mode->overlap+QEXT_SCALE2(COMBFILTER_MAXPERIOD, st->qext_scale)));
3045
118k
         oldLogE = oldBandE + st->channels*st->mode->nbEBands;
3046
118k
         oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
3047
118k
         OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
3048
118k
               opus_custom_encoder_get_size(st->mode, st->channels)-
3049
118k
               ((char*)&st->ENCODER_RESET_START - (char*)st));
3050
3.37M
         for (i=0;i<st->channels*st->mode->nbEBands;i++)
3051
3.25M
            oldLogE[i]=oldLogE2[i]=-GCONST(28.f);
3052
118k
         st->vbr_offset = 0;
3053
118k
         st->delayedIntra = 1;
3054
118k
         st->spread_decision = SPREAD_NORMAL;
3055
118k
         st->tonal_average = 256;
3056
118k
         st->hf_average = 0;
3057
118k
         st->tapset_decision = 0;
3058
118k
      }
3059
118k
      break;
3060
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
3061
      case CELT_SET_INPUT_CLIPPING_REQUEST:
3062
      {
3063
         opus_int32 value = va_arg(ap, opus_int32);
3064
         st->clip = value;
3065
      }
3066
      break;
3067
#endif
3068
118k
      case CELT_SET_SIGNALLING_REQUEST:
3069
118k
      {
3070
118k
         opus_int32 value = va_arg(ap, opus_int32);
3071
118k
         st->signalling = value;
3072
118k
      }
3073
118k
      break;
3074
77.8k
      case CELT_SET_ANALYSIS_REQUEST:
3075
77.8k
      {
3076
77.8k
         AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
3077
77.8k
         if (info)
3078
77.8k
            OPUS_COPY(&st->analysis, info, 1);
3079
77.8k
      }
3080
77.8k
      break;
3081
4.40k
      case CELT_SET_SILK_INFO_REQUEST:
3082
4.40k
      {
3083
4.40k
         SILKInfo *info = va_arg(ap, SILKInfo *);
3084
4.40k
         if (info)
3085
4.40k
            OPUS_COPY(&st->silk_info, info, 1);
3086
4.40k
      }
3087
4.40k
      break;
3088
259k
      case CELT_GET_MODE_REQUEST:
3089
259k
      {
3090
259k
         const CELTMode ** value = va_arg(ap, const CELTMode**);
3091
259k
         if (value==0)
3092
0
            goto bad_arg;
3093
259k
         *value=st->mode;
3094
259k
      }
3095
0
      break;
3096
77.8k
      case OPUS_GET_FINAL_RANGE_REQUEST:
3097
77.8k
      {
3098
77.8k
         opus_uint32 * value = va_arg(ap, opus_uint32 *);
3099
77.8k
         if (value==0)
3100
0
            goto bad_arg;
3101
77.8k
         *value=st->rng;
3102
77.8k
      }
3103
0
      break;
3104
237
      case OPUS_SET_LFE_REQUEST:
3105
237
      {
3106
237
          opus_int32 value = va_arg(ap, opus_int32);
3107
237
          st->lfe = value;
3108
237
      }
3109
237
      break;
3110
1.68k
      case OPUS_SET_ENERGY_MASK_REQUEST:
3111
1.68k
      {
3112
1.68k
         celt_glog *value = va_arg(ap, celt_glog*);
3113
1.68k
          st->energy_mask = value;
3114
1.68k
      }
3115
1.68k
      break;
3116
0
      default:
3117
0
         goto bad_request;
3118
2.10M
   }
3119
2.10M
   va_end(ap);
3120
2.10M
   return OPUS_OK;
3121
0
bad_arg:
3122
0
   va_end(ap);
3123
0
   return OPUS_BAD_ARG;
3124
0
bad_request:
3125
0
   va_end(ap);
3126
0
   return OPUS_UNIMPLEMENTED;
3127
2.10M
}
opus_custom_encoder_ctl
Line
Count
Source
2908
2.10M
{
2909
2.10M
   va_list ap;
2910
2911
2.10M
   va_start(ap, request);
2912
2.10M
   switch (request)
2913
2.10M
   {
2914
237k
      case OPUS_SET_COMPLEXITY_REQUEST:
2915
237k
      {
2916
237k
         int value = va_arg(ap, opus_int32);
2917
237k
         if (value<0 || value>10)
2918
0
            goto bad_arg;
2919
237k
         st->complexity = value;
2920
237k
      }
2921
0
      break;
2922
136k
      case CELT_SET_START_BAND_REQUEST:
2923
136k
      {
2924
136k
         opus_int32 value = va_arg(ap, opus_int32);
2925
136k
         if (value<0 || value>=st->mode->nbEBands)
2926
0
            goto bad_arg;
2927
136k
         st->start = value;
2928
136k
      }
2929
0
      break;
2930
136k
      case CELT_SET_END_BAND_REQUEST:
2931
136k
      {
2932
136k
         opus_int32 value = va_arg(ap, opus_int32);
2933
136k
         if (value<1 || value>st->mode->nbEBands)
2934
0
            goto bad_arg;
2935
136k
         st->end = value;
2936
136k
      }
2937
0
      break;
2938
77.8k
      case CELT_SET_PREDICTION_REQUEST:
2939
77.8k
      {
2940
77.8k
         int value = va_arg(ap, opus_int32);
2941
77.8k
         if (value<0 || value>2)
2942
0
            goto bad_arg;
2943
77.8k
         st->disable_pf = value<=1;
2944
77.8k
         st->force_intra = value==0;
2945
77.8k
      }
2946
0
      break;
2947
118k
      case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2948
118k
      {
2949
118k
         int value = va_arg(ap, opus_int32);
2950
118k
         if (value<0 || value>100)
2951
0
            goto bad_arg;
2952
118k
         st->loss_rate = value;
2953
118k
      }
2954
0
      break;
2955
59.0k
      case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2956
59.0k
      {
2957
59.0k
         opus_int32 value = va_arg(ap, opus_int32);
2958
59.0k
         st->constrained_vbr = value;
2959
59.0k
      }
2960
59.0k
      break;
2961
133k
      case OPUS_SET_VBR_REQUEST:
2962
133k
      {
2963
133k
         opus_int32 value = va_arg(ap, opus_int32);
2964
133k
         st->vbr = value;
2965
133k
      }
2966
133k
      break;
2967
195k
      case OPUS_SET_BITRATE_REQUEST:
2968
195k
      {
2969
195k
         opus_int32 value = va_arg(ap, opus_int32);
2970
195k
         if (value<=500 && value!=OPUS_BITRATE_MAX)
2971
0
            goto bad_arg;
2972
195k
         value = IMIN(value, 750000*st->channels);
2973
195k
         st->bitrate = value;
2974
195k
      }
2975
0
      break;
2976
136k
      case CELT_SET_CHANNELS_REQUEST:
2977
136k
      {
2978
136k
         opus_int32 value = va_arg(ap, opus_int32);
2979
136k
         if (value<1 || value>2)
2980
0
            goto bad_arg;
2981
136k
         st->stream_channels = value;
2982
136k
      }
2983
0
      break;
2984
94.6k
      case OPUS_SET_LSB_DEPTH_REQUEST:
2985
94.6k
      {
2986
94.6k
          opus_int32 value = va_arg(ap, opus_int32);
2987
94.6k
          if (value<8 || value>24)
2988
0
             goto bad_arg;
2989
94.6k
          st->lsb_depth=value;
2990
94.6k
      }
2991
0
      break;
2992
0
      case OPUS_GET_LSB_DEPTH_REQUEST:
2993
0
      {
2994
0
          opus_int32 *value = va_arg(ap, opus_int32*);
2995
0
          *value=st->lsb_depth;
2996
0
      }
2997
0
      break;
2998
118k
      case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
2999
118k
      {
3000
118k
          opus_int32 value = va_arg(ap, opus_int32);
3001
118k
          if(value<0 || value>1)
3002
0
          {
3003
0
             goto bad_arg;
3004
0
          }
3005
118k
          st->disable_inv = value;
3006
118k
      }
3007
0
      break;
3008
0
      case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
3009
0
      {
3010
0
          opus_int32 *value = va_arg(ap, opus_int32*);
3011
0
          if (!value)
3012
0
          {
3013
0
             goto bad_arg;
3014
0
          }
3015
0
          *value = st->disable_inv;
3016
0
      }
3017
0
      break;
3018
#ifdef ENABLE_QEXT
3019
      case OPUS_SET_QEXT_REQUEST:
3020
      {
3021
          opus_int32 value = va_arg(ap, opus_int32);
3022
          if(value<0 || value>1)
3023
          {
3024
             goto bad_arg;
3025
          }
3026
          st->enable_qext = value;
3027
      }
3028
      break;
3029
      case OPUS_GET_QEXT_REQUEST:
3030
      {
3031
          opus_int32 *value = va_arg(ap, opus_int32*);
3032
          if (!value)
3033
          {
3034
             goto bad_arg;
3035
          }
3036
          *value = st->enable_qext;
3037
      }
3038
      break;
3039
#endif
3040
118k
      case OPUS_RESET_STATE:
3041
118k
      {
3042
118k
         int i;
3043
118k
         celt_glog *oldBandE, *oldLogE, *oldLogE2;
3044
118k
         oldBandE = (celt_glog*)(st->in_mem+st->channels*(st->mode->overlap+QEXT_SCALE2(COMBFILTER_MAXPERIOD, st->qext_scale)));
3045
118k
         oldLogE = oldBandE + st->channels*st->mode->nbEBands;
3046
118k
         oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
3047
118k
         OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
3048
118k
               opus_custom_encoder_get_size(st->mode, st->channels)-
3049
118k
               ((char*)&st->ENCODER_RESET_START - (char*)st));
3050
3.37M
         for (i=0;i<st->channels*st->mode->nbEBands;i++)
3051
3.25M
            oldLogE[i]=oldLogE2[i]=-GCONST(28.f);
3052
118k
         st->vbr_offset = 0;
3053
118k
         st->delayedIntra = 1;
3054
118k
         st->spread_decision = SPREAD_NORMAL;
3055
118k
         st->tonal_average = 256;
3056
118k
         st->hf_average = 0;
3057
118k
         st->tapset_decision = 0;
3058
118k
      }
3059
118k
      break;
3060
#if defined(CUSTOM_MODES) || defined(ENABLE_OPUS_CUSTOM_API)
3061
      case CELT_SET_INPUT_CLIPPING_REQUEST:
3062
      {
3063
         opus_int32 value = va_arg(ap, opus_int32);
3064
         st->clip = value;
3065
      }
3066
      break;
3067
#endif
3068
118k
      case CELT_SET_SIGNALLING_REQUEST:
3069
118k
      {
3070
118k
         opus_int32 value = va_arg(ap, opus_int32);
3071
118k
         st->signalling = value;
3072
118k
      }
3073
118k
      break;
3074
77.8k
      case CELT_SET_ANALYSIS_REQUEST:
3075
77.8k
      {
3076
77.8k
         AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
3077
77.8k
         if (info)
3078
77.8k
            OPUS_COPY(&st->analysis, info, 1);
3079
77.8k
      }
3080
77.8k
      break;
3081
4.40k
      case CELT_SET_SILK_INFO_REQUEST:
3082
4.40k
      {
3083
4.40k
         SILKInfo *info = va_arg(ap, SILKInfo *);
3084
4.40k
         if (info)
3085
4.40k
            OPUS_COPY(&st->silk_info, info, 1);
3086
4.40k
      }
3087
4.40k
      break;
3088
259k
      case CELT_GET_MODE_REQUEST:
3089
259k
      {
3090
259k
         const CELTMode ** value = va_arg(ap, const CELTMode**);
3091
259k
         if (value==0)
3092
0
            goto bad_arg;
3093
259k
         *value=st->mode;
3094
259k
      }
3095
0
      break;
3096
77.8k
      case OPUS_GET_FINAL_RANGE_REQUEST:
3097
77.8k
      {
3098
77.8k
         opus_uint32 * value = va_arg(ap, opus_uint32 *);
3099
77.8k
         if (value==0)
3100
0
            goto bad_arg;
3101
77.8k
         *value=st->rng;
3102
77.8k
      }
3103
0
      break;
3104
237
      case OPUS_SET_LFE_REQUEST:
3105
237
      {
3106
237
          opus_int32 value = va_arg(ap, opus_int32);
3107
237
          st->lfe = value;
3108
237
      }
3109
237
      break;
3110
1.68k
      case OPUS_SET_ENERGY_MASK_REQUEST:
3111
1.68k
      {
3112
1.68k
         celt_glog *value = va_arg(ap, celt_glog*);
3113
1.68k
          st->energy_mask = value;
3114
1.68k
      }
3115
1.68k
      break;
3116
0
      default:
3117
0
         goto bad_request;
3118
2.10M
   }
3119
2.10M
   va_end(ap);
3120
2.10M
   return OPUS_OK;
3121
0
bad_arg:
3122
0
   va_end(ap);
3123
0
   return OPUS_BAD_ARG;
3124
0
bad_request:
3125
0
   va_end(ap);
3126
0
   return OPUS_UNIMPLEMENTED;
3127
2.10M
}