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