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