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