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