blob: 41fbfd49c8337c1e1e0a29058498de645d6df6a9 [file] [log] [blame]
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001/* 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 */
58struct OpusCustomEncoder {
59 const OpusCustomMode *mode; /**< Mode used by the encoder */
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -080060 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
128int 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
134OPUS_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
146CELTEncoder *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
163static 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;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800175 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
199int 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
205int 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
218void opus_custom_encoder_destroy(CELTEncoder *st)
219{
220 opus_free(st);
221}
222#endif /* CUSTOM_MODES */
223
224
225static 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 */
flimc91ee5b2016-01-26 14:33:44 +0100277 OPUS_CLEAR(tmp, 12);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800278
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
flimc91ee5b2016-01-26 14:33:44 +0100346 id = MAX32(0,MIN32(127,MULT16_32_Q15(tmp[i]+EPSILON,norm))); /* Do not round to nearest */
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800347#else
flimc91ee5b2016-01-26 14:33:44 +0100348 id = (int)MAX32(0,MIN32(127,floor(64*norm*(tmp[i]+EPSILON)))); /* Do not round to nearest */
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800349#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); */
flimc91ee5b2016-01-26 14:33:44 +0100366 *tf_estimate = celt_sqrt(MAX32(0, SHL32(MULT16_16(QCONST16(0.0069,14),MIN16(163,tf_max)),14)-QCONST32(0.139,28)));
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800367 /*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 */
flimc91ee5b2016-01-26 14:33:44 +0100378static int patch_transient_decision(opus_val16 *newE, opus_val16 *oldE, int nbEBands,
379 int start, int end, int C)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800380{
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 {
flimc91ee5b2016-01-26 14:33:44 +0100388 spread_old[start] = oldE[start];
389 for (i=start+1;i<end;i++)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800390 spread_old[i] = MAX16(spread_old[i-1]-QCONST16(1.0f, DB_SHIFT), oldE[i]);
391 } else {
flimc91ee5b2016-01-26 14:33:44 +0100392 spread_old[start] = MAX16(oldE[start],oldE[start+nbEBands]);
393 for (i=start+1;i<end;i++)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800394 spread_old[i] = MAX16(spread_old[i-1]-QCONST16(1.0f, DB_SHIFT),
395 MAX16(oldE[i],oldE[i+nbEBands]));
396 }
flimc91ee5b2016-01-26 14:33:44 +0100397 for (i=end-2;i>=start;i--)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800398 spread_old[i] = MAX16(spread_old[i], spread_old[i+1]-QCONST16(1.0f, DB_SHIFT));
399 /* Compute mean increase */
400 c=0; do {
flimc91ee5b2016-01-26 14:33:44 +0100401 for (i=IMAX(2,start);i<end-1;i++)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800402 {
403 opus_val16 x1, x2;
flimc91ee5b2016-01-26 14:33:44 +0100404 x1 = MAX16(0, newE[i + c*nbEBands]);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800405 x2 = MAX16(0, spread_old[i]);
406 mean_diff = ADD32(mean_diff, EXTEND32(MAX16(0, SUB16(x1, x2))));
407 }
408 } while (++c<C);
flimc91ee5b2016-01-26 14:33:44 +0100409 mean_diff = DIV32(mean_diff, C*(end-1-IMAX(2,start)));
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800410 /*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 */
416static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS_RESTRICT in,
flimc91ee5b2016-01-26 14:33:44 +0100417 celt_sig * OPUS_RESTRICT out, int C, int CC, int LM, int upsample,
418 int arch)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800419{
flimc91ee5b2016-01-26 14:33:44 +0100420 const int overlap = mode->overlap;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800421 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 */
flimc91ee5b2016-01-26 14:33:44 +0100439 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);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800442 }
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;
flimc91ee5b2016-01-26 14:33:44 +0100456 OPUS_CLEAR(&out[c*B*N+bound], B*N-bound);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800457 } while (++c<C);
458 }
459}
460
461
462void 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];
flimc91ee5b2016-01-26 14:33:44 +0100471 m = *mem;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800472
flimc91ee5b2016-01-26 14:33:44 +0100473 /* 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 }
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800487
488 Nu = N/upsample;
489 if (upsample!=1)
490 {
flimc91ee5b2016-01-26 14:33:44 +0100491 OPUS_CLEAR(inp, N);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800492 }
493 for (i=0;i<Nu;i++)
flimc91ee5b2016-01-26 14:33:44 +0100494 inp[i*upsample] = SCALEIN(pcmp[CC*i]);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800495
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
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800506#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 {
flimc91ee5b2016-01-26 14:33:44 +0100525 opus_val16 x;
526 x = inp[i];
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800527 /* Apply pre-emphasis */
flimc91ee5b2016-01-26 14:33:44 +0100528 inp[i] = SHL32(x, SIG_SHIFT) - m;
529 m = SHR32(MULT16_16(coef0, x), 15-SIG_SHIFT);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800530 }
531 }
532 *mem = m;
533}
534
535
536
537static 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
550static 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 {
flimc91ee5b2016-01-26 14:33:44 +0100580 int k, N;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800581 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;
flimc91ee5b2016-01-26 14:33:44 +0100587 OPUS_COPY(tmp, &X[tf_chan*N0 + (m->eBands[i]<<LM)], N);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800588 /* 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 {
flimc91ee5b2016-01-26 14:33:44 +0100597 OPUS_COPY(tmp_1, tmp, N);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800598 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
713static 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
754static 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,
flimc91ee5b2016-01-26 14:33:44 +0100757 int intensity, opus_val16 surround_trim, int arch)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800758{
759 int i;
760 opus_val32 diff=0;
761 int c;
flimc91ee5b2016-01-26 14:33:44 +0100762 int trim_index;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800763 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 {
flimc91ee5b2016-01-26 14:33:44 +0100772 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);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800775 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 {
flimc91ee5b2016-01-26 14:33:44 +0100782 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);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800785 minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18))));
786 }
787 minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC));
788 /*printf ("%f\n", sum);*/
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800789 /* 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);*/
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800812 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 }
flimc91ee5b2016-01-26 14:33:44 +0100821#else
822 (void)analysis;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800823#endif
824
825#ifdef FIXED_POINT
826 trim_index = PSHR32(trim, 8);
827#else
828 trim_index = (int)floor(.5f+trim);
829#endif
flimc91ee5b2016-01-26 14:33:44 +0100830 trim_index = IMAX(0, IMIN(10, trim_index));
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800831 /*printf("%d\n", trim_index);*/
832#ifdef FUZZING
833 trim_index = rand()%11;
834#endif
835 return trim_index;
836}
837
838static 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
flimc91ee5b2016-01-26 14:33:44 +0100870#define MSWAP(a,b) do {opus_val16 tmp = a;a=b;b=tmp;} while(0)
871static 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
910static 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
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800930static 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);
flimc91ee5b2016-01-26 14:33:44 +0100943 OPUS_CLEAR(offsets, nbEBands);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800944 /* 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 {
flimc91ee5b2016-01-26 14:33:44 +0100965 opus_val16 offset;
966 opus_val16 tmp;
967 opus_val16 *f;
968 f = &follower[c*nbEBands];
969 f[0] = bandLogE2[c*nbEBands];
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800970 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;
flimc91ee5b2016-01-26 14:33:44 +0100977 f[i] = MIN16(f[i-1]+QCONST16(1.5f,DB_SHIFT), bandLogE2[c*nbEBands+i]);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800978 }
979 for (i=last-1;i>=0;i--)
flimc91ee5b2016-01-26 14:33:44 +0100980 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
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800995 for (i=0;i<end;i++)
flimc91ee5b2016-01-26 14:33:44 +0100996 f[i] = MAX16(f[i], noise_floor[i]);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -0800997 } 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
1065static 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;
flimc91ee5b2016-01-26 14:33:44 +01001077 int overlap;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001078 SAVE_STACK;
1079
1080 mode = st->mode;
flimc91ee5b2016-01-26 14:33:44 +01001081 overlap = mode->overlap;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001082 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);
flimc91ee5b2016-01-26 14:33:44 +01001090 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+overlap)+overlap, N);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001091 } 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,
flimc91ee5b2016-01-26 14:33:44 +01001107 N, &pitch_index, st->prefilter_period, st->prefilter_gain, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001108 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 {
flimc91ee5b2016-01-26 14:33:44 +01001163 int offset = mode->shortMdctSize-overlap;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001164 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
flimc91ee5b2016-01-26 14:33:44 +01001165 OPUS_COPY(in+c*(N+overlap), st->in_mem+c*(overlap), overlap);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001166 if (offset)
flimc91ee5b2016-01-26 14:33:44 +01001167 comb_filter(in+c*(N+overlap)+overlap, pre[c]+COMBFILTER_MAXPERIOD,
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001168 st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
flimc91ee5b2016-01-26 14:33:44 +01001169 st->prefilter_tapset, st->prefilter_tapset, NULL, 0, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001170
flimc91ee5b2016-01-26 14:33:44 +01001171 comb_filter(in+c*(N+overlap)+overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001172 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
flimc91ee5b2016-01-26 14:33:44 +01001173 st->prefilter_tapset, prefilter_tapset, mode->window, overlap, st->arch);
1174 OPUS_COPY(st->in_mem+c*(overlap), in+c*(N+overlap)+N, overlap);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001175
1176 if (N>COMBFILTER_MAXPERIOD)
1177 {
1178 OPUS_MOVE(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_MOVE(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
1192static 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 }
flimc91ee5b2016-01-26 14:33:44 +01001259#else
1260 (void)analysis;
1261 (void)pitch_change;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001262#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;
1285#ifdef FIXED_POINT
1286 rate_factor = MAX16(0,(bitrate-32000));
1287#else
1288 rate_factor = MAX16(0,(1.f/32768)*(bitrate-32000));
1289#endif
1290 if (constrained_vbr)
1291 rate_factor = MIN16(rate_factor, QCONST16(0.67f, 15));
1292 target = base_target + (opus_int32)MULT16_32_Q15(rate_factor, target-base_target);
1293
1294 }
1295
1296 if (!has_surround_mask && tf_estimate < QCONST16(.2f, 14))
1297 {
1298 opus_val16 amount;
1299 opus_val16 tvbr_factor;
1300 amount = MULT16_16_Q15(QCONST16(.0000031f, 30), IMAX(0, IMIN(32000, 96000-bitrate)));
1301 tvbr_factor = SHR32(MULT16_16(temporal_vbr, amount), DB_SHIFT);
1302 target += (opus_int32)MULT16_32_Q15(tvbr_factor, target);
1303 }
1304
1305 /* Don't allow more than doubling the rate */
1306 target = IMIN(2*base_target, target);
1307
1308 return target;
1309}
1310
1311int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
1312{
1313 int i, c, N;
1314 opus_int32 bits;
1315 ec_enc _enc;
1316 VARDECL(celt_sig, in);
1317 VARDECL(celt_sig, freq);
1318 VARDECL(celt_norm, X);
1319 VARDECL(celt_ener, bandE);
1320 VARDECL(opus_val16, bandLogE);
1321 VARDECL(opus_val16, bandLogE2);
1322 VARDECL(int, fine_quant);
1323 VARDECL(opus_val16, error);
1324 VARDECL(int, pulses);
1325 VARDECL(int, cap);
1326 VARDECL(int, offsets);
1327 VARDECL(int, fine_priority);
1328 VARDECL(int, tf_res);
1329 VARDECL(unsigned char, collapse_masks);
1330 celt_sig *prefilter_mem;
1331 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
1332 int shortBlocks=0;
1333 int isTransient=0;
1334 const int CC = st->channels;
1335 const int C = st->stream_channels;
1336 int LM, M;
1337 int tf_select;
1338 int nbFilledBytes, nbAvailableBytes;
flimc91ee5b2016-01-26 14:33:44 +01001339 int start;
1340 int end;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001341 int effEnd;
1342 int codedBands;
1343 int tf_sum;
1344 int alloc_trim;
1345 int pitch_index=COMBFILTER_MINPERIOD;
1346 opus_val16 gain1 = 0;
1347 int dual_stereo=0;
1348 int effectiveBytes;
1349 int dynalloc_logp;
1350 opus_int32 vbr_rate;
1351 opus_int32 total_bits;
1352 opus_int32 total_boost;
1353 opus_int32 balance;
1354 opus_int32 tell;
1355 int prefilter_tapset=0;
1356 int pf_on;
1357 int anti_collapse_rsv;
1358 int anti_collapse_on=0;
1359 int silence=0;
1360 int tf_chan = 0;
1361 opus_val16 tf_estimate;
1362 int pitch_change=0;
1363 opus_int32 tot_boost;
1364 opus_val32 sample_max;
1365 opus_val16 maxDepth;
1366 const OpusCustomMode *mode;
1367 int nbEBands;
1368 int overlap;
1369 const opus_int16 *eBands;
1370 int secondMdct;
1371 int signalBandwidth;
1372 int transient_got_disabled=0;
1373 opus_val16 surround_masking=0;
1374 opus_val16 temporal_vbr=0;
1375 opus_val16 surround_trim = 0;
1376 opus_int32 equiv_rate = 510000;
1377 VARDECL(opus_val16, surround_dynalloc);
1378 ALLOC_STACK;
1379
1380 mode = st->mode;
1381 nbEBands = mode->nbEBands;
1382 overlap = mode->overlap;
1383 eBands = mode->eBands;
flimc91ee5b2016-01-26 14:33:44 +01001384 start = st->start;
1385 end = st->end;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001386 tf_estimate = 0;
1387 if (nbCompressedBytes<2 || pcm==NULL)
1388 {
1389 RESTORE_STACK;
1390 return OPUS_BAD_ARG;
1391 }
1392
1393 frame_size *= st->upsample;
1394 for (LM=0;LM<=mode->maxLM;LM++)
1395 if (mode->shortMdctSize<<LM==frame_size)
1396 break;
1397 if (LM>mode->maxLM)
1398 {
1399 RESTORE_STACK;
1400 return OPUS_BAD_ARG;
1401 }
1402 M=1<<LM;
1403 N = M*mode->shortMdctSize;
1404
flimc91ee5b2016-01-26 14:33:44 +01001405 prefilter_mem = st->in_mem+CC*(overlap);
1406 oldBandE = (opus_val16*)(st->in_mem+CC*(overlap+COMBFILTER_MAXPERIOD));
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001407 oldLogE = oldBandE + CC*nbEBands;
1408 oldLogE2 = oldLogE + CC*nbEBands;
1409
1410 if (enc==NULL)
1411 {
1412 tell=1;
1413 nbFilledBytes=0;
1414 } else {
1415 tell=ec_tell(enc);
1416 nbFilledBytes=(tell+4)>>3;
1417 }
1418
1419#ifdef CUSTOM_MODES
1420 if (st->signalling && enc==NULL)
1421 {
flimc91ee5b2016-01-26 14:33:44 +01001422 int tmp = (mode->effEBands-end)>>1;
1423 end = st->end = IMAX(1, mode->effEBands-tmp);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001424 compressed[0] = tmp<<5;
1425 compressed[0] |= LM<<3;
1426 compressed[0] |= (C==2)<<2;
1427 /* Convert "standard mode" to Opus header */
1428 if (mode->Fs==48000 && mode->shortMdctSize==120)
1429 {
1430 int c0 = toOpus(compressed[0]);
1431 if (c0<0)
1432 {
1433 RESTORE_STACK;
1434 return OPUS_BAD_ARG;
1435 }
1436 compressed[0] = c0;
1437 }
1438 compressed++;
1439 nbCompressedBytes--;
1440 }
1441#else
1442 celt_assert(st->signalling==0);
1443#endif
1444
1445 /* Can't produce more than 1275 output bytes */
1446 nbCompressedBytes = IMIN(nbCompressedBytes,1275);
1447 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1448
1449 if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1450 {
1451 opus_int32 den=mode->Fs>>BITRES;
1452 vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1453#ifdef CUSTOM_MODES
1454 if (st->signalling)
1455 vbr_rate -= 8<<BITRES;
1456#endif
1457 effectiveBytes = vbr_rate>>(3+BITRES);
1458 } else {
1459 opus_int32 tmp;
1460 vbr_rate = 0;
1461 tmp = st->bitrate*frame_size;
1462 if (tell>1)
1463 tmp += tell;
1464 if (st->bitrate!=OPUS_BITRATE_MAX)
1465 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1466 (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling));
1467 effectiveBytes = nbCompressedBytes;
1468 }
1469 if (st->bitrate != OPUS_BITRATE_MAX)
1470 equiv_rate = st->bitrate - (40*C+20)*((400>>LM) - 50);
1471
1472 if (enc==NULL)
1473 {
1474 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1475 enc = &_enc;
1476 }
1477
1478 if (vbr_rate>0)
1479 {
1480 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1481 target rate and buffering.
1482 We must do this up front so that bust-prevention logic triggers
1483 correctly if we don't have enough bits. */
1484 if (st->constrained_vbr)
1485 {
1486 opus_int32 vbr_bound;
1487 opus_int32 max_allowed;
1488 /* We could use any multiple of vbr_rate as bound (depending on the
1489 delay).
1490 This is clamped to ensure we use at least two bytes if the encoder
1491 was entirely empty, but to allow 0 in hybrid mode. */
1492 vbr_bound = vbr_rate;
1493 max_allowed = IMIN(IMAX(tell==1?2:0,
1494 (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1495 nbAvailableBytes);
1496 if(max_allowed < nbAvailableBytes)
1497 {
1498 nbCompressedBytes = nbFilledBytes+max_allowed;
1499 nbAvailableBytes = max_allowed;
1500 ec_enc_shrink(enc, nbCompressedBytes);
1501 }
1502 }
1503 }
1504 total_bits = nbCompressedBytes*8;
1505
flimc91ee5b2016-01-26 14:33:44 +01001506 effEnd = end;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001507 if (effEnd > mode->effEBands)
1508 effEnd = mode->effEBands;
1509
flimc91ee5b2016-01-26 14:33:44 +01001510 ALLOC(in, CC*(N+overlap), celt_sig);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001511
1512 sample_max=MAX32(st->overlap_max, celt_maxabs16(pcm, C*(N-overlap)/st->upsample));
1513 st->overlap_max=celt_maxabs16(pcm+C*(N-overlap)/st->upsample, C*overlap/st->upsample);
1514 sample_max=MAX32(sample_max, st->overlap_max);
1515#ifdef FIXED_POINT
1516 silence = (sample_max==0);
1517#else
1518 silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth));
1519#endif
1520#ifdef FUZZING
1521 if ((rand()&0x3F)==0)
1522 silence = 1;
1523#endif
1524 if (tell==1)
1525 ec_enc_bit_logp(enc, silence, 15);
1526 else
1527 silence=0;
1528 if (silence)
1529 {
1530 /*In VBR mode there is no need to send more than the minimum. */
1531 if (vbr_rate>0)
1532 {
1533 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1534 total_bits=nbCompressedBytes*8;
1535 nbAvailableBytes=2;
1536 ec_enc_shrink(enc, nbCompressedBytes);
1537 }
1538 /* Pretend we've filled all the remaining bits with zeros
1539 (that's what the initialiser did anyway) */
1540 tell = nbCompressedBytes*8;
1541 enc->nbits_total+=tell-ec_tell(enc);
1542 }
1543 c=0; do {
flimc91ee5b2016-01-26 14:33:44 +01001544 int need_clip=0;
1545#ifndef FIXED_POINT
1546 need_clip = st->clip && sample_max>65536.f;
1547#endif
1548 celt_preemphasis(pcm+c, in+c*(N+overlap)+overlap, N, CC, st->upsample,
1549 mode->preemph, st->preemph_memE+c, need_clip);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001550 } while (++c<CC);
1551
1552
1553
1554 /* Find pitch period and gain */
1555 {
1556 int enabled;
1557 int qg;
flimc91ee5b2016-01-26 14:33:44 +01001558 enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && start==0 && !silence && !st->disable_pf
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001559 && st->complexity >= 5 && !(st->consec_transient && LM!=3 && st->variable_duration==OPUS_FRAMESIZE_VARIABLE);
1560
1561 prefilter_tapset = st->tapset_decision;
1562 pf_on = run_prefilter(st, in, prefilter_mem, CC, N, prefilter_tapset, &pitch_index, &gain1, &qg, enabled, nbAvailableBytes);
1563 if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && (!st->analysis.valid || st->analysis.tonality > .3)
1564 && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st->prefilter_period))
1565 pitch_change = 1;
1566 if (pf_on==0)
1567 {
flimc91ee5b2016-01-26 14:33:44 +01001568 if(start==0 && tell+16<=total_bits)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001569 ec_enc_bit_logp(enc, 0, 1);
1570 } else {
1571 /*This block is not gated by a total bits check only because
1572 of the nbAvailableBytes check above.*/
1573 int octave;
1574 ec_enc_bit_logp(enc, 1, 1);
1575 pitch_index += 1;
1576 octave = EC_ILOG(pitch_index)-5;
1577 ec_enc_uint(enc, octave, 6);
1578 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1579 pitch_index -= 1;
1580 ec_enc_bits(enc, qg, 3);
1581 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1582 }
1583 }
1584
1585 isTransient = 0;
1586 shortBlocks = 0;
1587 if (st->complexity >= 1 && !st->lfe)
1588 {
flimc91ee5b2016-01-26 14:33:44 +01001589 isTransient = transient_analysis(in, N+overlap, CC,
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001590 &tf_estimate, &tf_chan);
1591 }
1592 if (LM>0 && ec_tell(enc)+3<=total_bits)
1593 {
1594 if (isTransient)
1595 shortBlocks = M;
1596 } else {
1597 isTransient = 0;
1598 transient_got_disabled=1;
1599 }
1600
1601 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
1602 ALLOC(bandE,nbEBands*CC, celt_ener);
1603 ALLOC(bandLogE,nbEBands*CC, opus_val16);
1604
1605 secondMdct = shortBlocks && st->complexity>=8;
1606 ALLOC(bandLogE2, C*nbEBands, opus_val16);
1607 if (secondMdct)
1608 {
flimc91ee5b2016-01-26 14:33:44 +01001609 compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample, st->arch);
1610 compute_band_energies(mode, freq, bandE, effEnd, C, LM);
1611 amp2Log2(mode, effEnd, end, bandE, bandLogE2, C);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001612 for (i=0;i<C*nbEBands;i++)
1613 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT));
1614 }
1615
flimc91ee5b2016-01-26 14:33:44 +01001616 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001617 if (CC==2&&C==1)
1618 tf_chan = 0;
flimc91ee5b2016-01-26 14:33:44 +01001619 compute_band_energies(mode, freq, bandE, effEnd, C, LM);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001620
1621 if (st->lfe)
1622 {
flimc91ee5b2016-01-26 14:33:44 +01001623 for (i=2;i<end;i++)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001624 {
1625 bandE[i] = IMIN(bandE[i], MULT16_32_Q15(QCONST16(1e-4f,15),bandE[0]));
1626 bandE[i] = MAX32(bandE[i], EPSILON);
1627 }
1628 }
flimc91ee5b2016-01-26 14:33:44 +01001629 amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001630
1631 ALLOC(surround_dynalloc, C*nbEBands, opus_val16);
flimc91ee5b2016-01-26 14:33:44 +01001632 OPUS_CLEAR(surround_dynalloc, end);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001633 /* This computes how much masking takes place between surround channels */
flimc91ee5b2016-01-26 14:33:44 +01001634 if (start==0&&st->energy_mask&&!st->lfe)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001635 {
1636 int mask_end;
1637 int midband;
1638 int count_dynalloc;
1639 opus_val32 mask_avg=0;
1640 opus_val32 diff=0;
1641 int count=0;
1642 mask_end = IMAX(2,st->lastCodedBands);
1643 for (c=0;c<C;c++)
1644 {
1645 for(i=0;i<mask_end;i++)
1646 {
1647 opus_val16 mask;
1648 mask = MAX16(MIN16(st->energy_mask[nbEBands*c+i],
1649 QCONST16(.25f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT));
1650 if (mask > 0)
1651 mask = HALF16(mask);
1652 mask_avg += MULT16_16(mask, eBands[i+1]-eBands[i]);
1653 count += eBands[i+1]-eBands[i];
1654 diff += MULT16_16(mask, 1+2*i-mask_end);
1655 }
1656 }
flimc91ee5b2016-01-26 14:33:44 +01001657 celt_assert(count>0);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001658 mask_avg = DIV32_16(mask_avg,count);
1659 mask_avg += QCONST16(.2f, DB_SHIFT);
1660 diff = diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end);
1661 /* Again, being conservative */
1662 diff = HALF32(diff);
1663 diff = MAX32(MIN32(diff, QCONST32(.031f, DB_SHIFT)), -QCONST32(.031f, DB_SHIFT));
1664 /* Find the band that's in the middle of the coded spectrum */
1665 for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++);
1666 count_dynalloc=0;
1667 for(i=0;i<mask_end;i++)
1668 {
1669 opus_val32 lin;
1670 opus_val16 unmask;
1671 lin = mask_avg + diff*(i-midband);
1672 if (C==2)
1673 unmask = MAX16(st->energy_mask[i], st->energy_mask[nbEBands+i]);
1674 else
1675 unmask = st->energy_mask[i];
1676 unmask = MIN16(unmask, QCONST16(.0f, DB_SHIFT));
1677 unmask -= lin;
1678 if (unmask > QCONST16(.25f, DB_SHIFT))
1679 {
1680 surround_dynalloc[i] = unmask - QCONST16(.25f, DB_SHIFT);
1681 count_dynalloc++;
1682 }
1683 }
1684 if (count_dynalloc>=3)
1685 {
1686 /* If we need dynalloc in many bands, it's probably because our
1687 initial masking rate was too low. */
1688 mask_avg += QCONST16(.25f, DB_SHIFT);
1689 if (mask_avg>0)
1690 {
1691 /* Something went really wrong in the original calculations,
1692 disabling masking. */
1693 mask_avg = 0;
1694 diff = 0;
flimc91ee5b2016-01-26 14:33:44 +01001695 OPUS_CLEAR(surround_dynalloc, mask_end);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001696 } else {
1697 for(i=0;i<mask_end;i++)
1698 surround_dynalloc[i] = MAX16(0, surround_dynalloc[i]-QCONST16(.25f, DB_SHIFT));
1699 }
1700 }
1701 mask_avg += QCONST16(.2f, DB_SHIFT);
1702 /* Convert to 1/64th units used for the trim */
1703 surround_trim = 64*diff;
1704 /*printf("%d %d ", mask_avg, surround_trim);*/
1705 surround_masking = mask_avg;
1706 }
1707 /* Temporal VBR (but not for LFE) */
1708 if (!st->lfe)
1709 {
1710 opus_val16 follow=-QCONST16(10.0f,DB_SHIFT);
1711 opus_val32 frame_avg=0;
1712 opus_val16 offset = shortBlocks?HALF16(SHL16(LM, DB_SHIFT)):0;
flimc91ee5b2016-01-26 14:33:44 +01001713 for(i=start;i<end;i++)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001714 {
1715 follow = MAX16(follow-QCONST16(1.f, DB_SHIFT), bandLogE[i]-offset);
1716 if (C==2)
1717 follow = MAX16(follow, bandLogE[i+nbEBands]-offset);
1718 frame_avg += follow;
1719 }
flimc91ee5b2016-01-26 14:33:44 +01001720 frame_avg /= (end-start);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001721 temporal_vbr = SUB16(frame_avg,st->spec_avg);
1722 temporal_vbr = MIN16(QCONST16(3.f, DB_SHIFT), MAX16(-QCONST16(1.5f, DB_SHIFT), temporal_vbr));
1723 st->spec_avg += MULT16_16_Q15(QCONST16(.02f, 15), temporal_vbr);
1724 }
1725 /*for (i=0;i<21;i++)
1726 printf("%f ", bandLogE[i]);
1727 printf("\n");*/
1728
1729 if (!secondMdct)
1730 {
flimc91ee5b2016-01-26 14:33:44 +01001731 OPUS_COPY(bandLogE2, bandLogE, C*nbEBands);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001732 }
1733
1734 /* Last chance to catch any transient we might have missed in the
1735 time-domain analysis */
1736 if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5 && !st->lfe)
1737 {
flimc91ee5b2016-01-26 14:33:44 +01001738 if (patch_transient_decision(bandLogE, oldBandE, nbEBands, start, end, C))
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001739 {
1740 isTransient = 1;
1741 shortBlocks = M;
flimc91ee5b2016-01-26 14:33:44 +01001742 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
1743 compute_band_energies(mode, freq, bandE, effEnd, C, LM);
1744 amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001745 /* Compensate for the scaling of short vs long mdcts */
1746 for (i=0;i<C*nbEBands;i++)
1747 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT));
1748 tf_estimate = QCONST16(.2f,14);
1749 }
1750 }
1751
1752 if (LM>0 && ec_tell(enc)+3<=total_bits)
1753 ec_enc_bit_logp(enc, isTransient, 3);
1754
1755 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1756
1757 /* Band normalisation */
1758 normalise_bands(mode, freq, X, bandE, effEnd, C, M);
1759
1760 ALLOC(tf_res, nbEBands, int);
1761 /* Disable variable tf resolution for hybrid and at very low bitrate */
flimc91ee5b2016-01-26 14:33:44 +01001762 if (effectiveBytes>=15*C && start==0 && st->complexity>=2 && !st->lfe)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001763 {
1764 int lambda;
1765 if (effectiveBytes<40)
1766 lambda = 12;
1767 else if (effectiveBytes<60)
1768 lambda = 6;
1769 else if (effectiveBytes<100)
1770 lambda = 4;
1771 else
1772 lambda = 3;
1773 lambda*=2;
1774 tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, LM, &tf_sum, tf_estimate, tf_chan);
flimc91ee5b2016-01-26 14:33:44 +01001775 for (i=effEnd;i<end;i++)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001776 tf_res[i] = tf_res[effEnd-1];
1777 } else {
1778 tf_sum = 0;
flimc91ee5b2016-01-26 14:33:44 +01001779 for (i=0;i<end;i++)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001780 tf_res[i] = isTransient;
1781 tf_select=0;
1782 }
1783
1784 ALLOC(error, C*nbEBands, opus_val16);
flimc91ee5b2016-01-26 14:33:44 +01001785 quant_coarse_energy(mode, start, end, effEnd, bandLogE,
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001786 oldBandE, total_bits, error, enc,
1787 C, LM, nbAvailableBytes, st->force_intra,
1788 &st->delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
1789
flimc91ee5b2016-01-26 14:33:44 +01001790 tf_encode(start, end, isTransient, tf_res, LM, tf_select, enc);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001791
1792 if (ec_tell(enc)+4<=total_bits)
1793 {
1794 if (st->lfe)
1795 {
1796 st->tapset_decision = 0;
1797 st->spread_decision = SPREAD_NORMAL;
flimc91ee5b2016-01-26 14:33:44 +01001798 } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C || start != 0)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001799 {
1800 if (st->complexity == 0)
1801 st->spread_decision = SPREAD_NONE;
1802 else
1803 st->spread_decision = SPREAD_NORMAL;
1804 } else {
1805 /* Disable new spreading+tapset estimator until we can show it works
1806 better than the old one. So far it seems like spreading_decision()
1807 works best. */
1808#if 0
1809 if (st->analysis.valid)
1810 {
1811 static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
1812 static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
1813 static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), QCONST16(.15f, 15)};
1814 static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), QCONST16(.05f, 15)};
1815 st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision);
1816 st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
1817 } else
1818#endif
1819 {
1820 st->spread_decision = spreading_decision(mode, X,
1821 &st->tonal_average, st->spread_decision, &st->hf_average,
1822 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1823 }
1824 /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
1825 /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
1826 }
1827 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1828 }
1829
1830 ALLOC(offsets, nbEBands, int);
1831
flimc91ee5b2016-01-26 14:33:44 +01001832 maxDepth = dynalloc_analysis(bandLogE, bandLogE2, nbEBands, start, end, C, offsets,
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001833 st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr,
1834 eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc);
1835 /* For LFE, everything interesting is in the first band */
1836 if (st->lfe)
1837 offsets[0] = IMIN(8, effectiveBytes/3);
1838 ALLOC(cap, nbEBands, int);
1839 init_caps(mode,cap,LM,C);
1840
1841 dynalloc_logp = 6;
1842 total_bits<<=BITRES;
1843 total_boost = 0;
1844 tell = ec_tell_frac(enc);
flimc91ee5b2016-01-26 14:33:44 +01001845 for (i=start;i<end;i++)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001846 {
1847 int width, quanta;
1848 int dynalloc_loop_logp;
1849 int boost;
1850 int j;
1851 width = C*(eBands[i+1]-eBands[i])<<LM;
1852 /* quanta is 6 bits, but no more than 1 bit/sample
1853 and no less than 1/8 bit/sample */
1854 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1855 dynalloc_loop_logp = dynalloc_logp;
1856 boost = 0;
1857 for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
1858 && boost < cap[i]; j++)
1859 {
1860 int flag;
1861 flag = j<offsets[i];
1862 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
1863 tell = ec_tell_frac(enc);
1864 if (!flag)
1865 break;
1866 boost += quanta;
1867 total_boost += quanta;
1868 dynalloc_loop_logp = 1;
1869 }
1870 /* Making dynalloc more likely */
1871 if (j)
1872 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1873 offsets[i] = boost;
1874 }
1875
1876 if (C==2)
1877 {
1878 static const opus_val16 intensity_thresholds[21]=
1879 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 off*/
1880 { 1, 2, 3, 4, 5, 6, 7, 8,16,24,36,44,50,56,62,67,72,79,88,106,134};
1881 static const opus_val16 intensity_histeresis[21]=
1882 { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6, 8, 8};
1883
1884 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1885 if (LM!=0)
1886 dual_stereo = stereo_analysis(mode, X, LM, N);
1887
1888 st->intensity = hysteresis_decision((opus_val16)(equiv_rate/1000),
1889 intensity_thresholds, intensity_histeresis, 21, st->intensity);
flimc91ee5b2016-01-26 14:33:44 +01001890 st->intensity = IMIN(end,IMAX(start, st->intensity));
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001891 }
1892
1893 alloc_trim = 5;
1894 if (tell+(6<<BITRES) <= total_bits - total_boost)
1895 {
1896 if (st->lfe)
1897 alloc_trim = 5;
1898 else
1899 alloc_trim = alloc_trim_analysis(mode, X, bandLogE,
flimc91ee5b2016-01-26 14:33:44 +01001900 end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate,
1901 st->intensity, surround_trim, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08001902 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
1903 tell = ec_tell_frac(enc);
1904 }
1905
1906 /* Variable bitrate */
1907 if (vbr_rate>0)
1908 {
1909 opus_val16 alpha;
1910 opus_int32 delta;
1911 /* The target rate in 8th bits per frame */
1912 opus_int32 target, base_target;
1913 opus_int32 min_allowed;
1914 int lm_diff = mode->maxLM - LM;
1915
1916 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
1917 The CELT allocator will just not be able to use more than that anyway. */
1918 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
1919 base_target = vbr_rate - ((40*C+20)<<BITRES);
1920
1921 if (st->constrained_vbr)
1922 base_target += (st->vbr_offset>>lm_diff);
1923
1924 target = compute_vbr(mode, &st->analysis, base_target, LM, equiv_rate,
1925 st->lastCodedBands, C, st->intensity, st->constrained_vbr,
1926 st->stereo_saving, tot_boost, tf_estimate, pitch_change, maxDepth,
1927 st->variable_duration, st->lfe, st->energy_mask!=NULL, surround_masking,
1928 temporal_vbr);
1929
1930 /* The current offset is removed from the target and the space used
1931 so far is added*/
1932 target=target+tell;
1933 /* In VBR mode the frame size must not be reduced so much that it would
1934 result in the encoder running out of bits.
1935 The margin of 2 bytes ensures that none of the bust-prevention logic
1936 in the decoder will have triggered so far. */
1937 min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes;
1938
1939 nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
1940 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
1941 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
1942
1943 /* By how much did we "miss" the target on that frame */
1944 delta = target - vbr_rate;
1945
1946 target=nbAvailableBytes<<(BITRES+3);
1947
1948 /*If the frame is silent we don't adjust our drift, otherwise
1949 the encoder will shoot to very high rates after hitting a
1950 span of silence, but we do allow the bitres to refill.
1951 This means that we'll undershoot our target in CVBR/VBR modes
1952 on files with lots of silence. */
1953 if(silence)
1954 {
1955 nbAvailableBytes = 2;
1956 target = 2*8<<BITRES;
1957 delta = 0;
1958 }
1959
1960 if (st->vbr_count < 970)
1961 {
1962 st->vbr_count++;
1963 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
1964 } else
1965 alpha = QCONST16(.001f,15);
1966 /* How many bits have we used in excess of what we're allowed */
1967 if (st->constrained_vbr)
1968 st->vbr_reservoir += target - vbr_rate;
1969 /*printf ("%d\n", st->vbr_reservoir);*/
1970
1971 /* Compute the offset we need to apply in order to reach the target */
1972 if (st->constrained_vbr)
1973 {
1974 st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
1975 st->vbr_offset = -st->vbr_drift;
1976 }
1977 /*printf ("%d\n", st->vbr_drift);*/
1978
1979 if (st->constrained_vbr && st->vbr_reservoir < 0)
1980 {
1981 /* We're under the min value -- increase rate */
1982 int adjust = (-st->vbr_reservoir)/(8<<BITRES);
1983 /* Unless we're just coding silence */
1984 nbAvailableBytes += silence?0:adjust;
1985 st->vbr_reservoir = 0;
1986 /*printf ("+%d\n", adjust);*/
1987 }
1988 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
1989 /*printf("%d\n", nbCompressedBytes*50*8);*/
1990 /* This moves the raw bits to take into account the new compressed size */
1991 ec_enc_shrink(enc, nbCompressedBytes);
1992 }
1993
1994 /* Bit allocation */
1995 ALLOC(fine_quant, nbEBands, int);
1996 ALLOC(pulses, nbEBands, int);
1997 ALLOC(fine_priority, nbEBands, int);
1998
1999 /* bits = packet size - where we are - safety*/
2000 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
2001 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2002 bits -= anti_collapse_rsv;
flimc91ee5b2016-01-26 14:33:44 +01002003 signalBandwidth = end-1;
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002004#ifndef DISABLE_FLOAT_API
2005 if (st->analysis.valid)
2006 {
2007 int min_bandwidth;
2008 if (equiv_rate < (opus_int32)32000*C)
2009 min_bandwidth = 13;
2010 else if (equiv_rate < (opus_int32)48000*C)
2011 min_bandwidth = 16;
2012 else if (equiv_rate < (opus_int32)60000*C)
2013 min_bandwidth = 18;
2014 else if (equiv_rate < (opus_int32)80000*C)
2015 min_bandwidth = 19;
2016 else
2017 min_bandwidth = 20;
2018 signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth);
2019 }
2020#endif
2021 if (st->lfe)
2022 signalBandwidth = 1;
flimc91ee5b2016-01-26 14:33:44 +01002023 codedBands = compute_allocation(mode, start, end, offsets, cap,
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002024 alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses,
2025 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBandwidth);
2026 if (st->lastCodedBands)
2027 st->lastCodedBands = IMIN(st->lastCodedBands+1,IMAX(st->lastCodedBands-1,codedBands));
2028 else
2029 st->lastCodedBands = codedBands;
2030
flimc91ee5b2016-01-26 14:33:44 +01002031 quant_fine_energy(mode, start, end, oldBandE, error, fine_quant, enc, C);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002032
2033 /* Residual quantisation */
2034 ALLOC(collapse_masks, C*nbEBands, unsigned char);
flimc91ee5b2016-01-26 14:33:44 +01002035 quant_all_bands(1, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
2036 bandE, pulses, shortBlocks, st->spread_decision,
2037 dual_stereo, st->intensity, tf_res, nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv,
2038 balance, enc, LM, codedBands, &st->rng, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002039
2040 if (anti_collapse_rsv > 0)
2041 {
2042 anti_collapse_on = st->consec_transient<2;
2043#ifdef FUZZING
2044 anti_collapse_on = rand()&0x1;
2045#endif
2046 ec_enc_bits(enc, anti_collapse_on, 1);
2047 }
flimc91ee5b2016-01-26 14:33:44 +01002048 quant_energy_finalise(mode, start, end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002049
2050 if (silence)
2051 {
2052 for (i=0;i<C*nbEBands;i++)
2053 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
2054 }
2055
2056#ifdef RESYNTH
2057 /* Re-synthesis of the coded audio if required */
2058 {
2059 celt_sig *out_mem[2];
2060
2061 if (anti_collapse_on)
2062 {
2063 anti_collapse(mode, X, collapse_masks, LM, C, N,
flimc91ee5b2016-01-26 14:33:44 +01002064 start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002065 }
2066
2067 c=0; do {
2068 OPUS_MOVE(st->syn_mem[c], st->syn_mem[c]+N, 2*MAX_PERIOD-N+overlap/2);
2069 } while (++c<CC);
2070
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002071 c=0; do {
2072 out_mem[c] = st->syn_mem[c]+2*MAX_PERIOD-N;
2073 } while (++c<CC);
2074
flimc91ee5b2016-01-26 14:33:44 +01002075 celt_synthesis(mode, X, out_mem, oldBandE, start, effEnd,
2076 C, CC, isTransient, LM, st->upsample, silence, st->arch);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002077
2078 c=0; do {
2079 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
2080 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
2081 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, mode->shortMdctSize,
2082 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
flimc91ee5b2016-01-26 14:33:44 +01002083 mode->window, overlap);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002084 if (LM!=0)
2085 comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMdctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize,
2086 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
2087 mode->window, overlap);
2088 } while (++c<CC);
2089
2090 /* We reuse freq[] as scratch space for the de-emphasis */
flimc91ee5b2016-01-26 14:33:44 +01002091 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, mode->preemph, st->preemph_memD);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002092 st->prefilter_period_old = st->prefilter_period;
2093 st->prefilter_gain_old = st->prefilter_gain;
2094 st->prefilter_tapset_old = st->prefilter_tapset;
2095 }
2096#endif
2097
2098 st->prefilter_period = pitch_index;
2099 st->prefilter_gain = gain1;
2100 st->prefilter_tapset = prefilter_tapset;
2101#ifdef RESYNTH
2102 if (LM!=0)
2103 {
2104 st->prefilter_period_old = st->prefilter_period;
2105 st->prefilter_gain_old = st->prefilter_gain;
2106 st->prefilter_tapset_old = st->prefilter_tapset;
2107 }
2108#endif
2109
2110 if (CC==2&&C==1) {
flimc91ee5b2016-01-26 14:33:44 +01002111 OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002112 }
2113
2114 if (!isTransient)
2115 {
flimc91ee5b2016-01-26 14:33:44 +01002116 OPUS_COPY(oldLogE2, oldLogE, CC*nbEBands);
2117 OPUS_COPY(oldLogE, oldBandE, CC*nbEBands);
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002118 } else {
2119 for (i=0;i<CC*nbEBands;i++)
2120 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
2121 }
2122 /* In case start or end were to change */
2123 c=0; do
2124 {
flimc91ee5b2016-01-26 14:33:44 +01002125 for (i=0;i<start;i++)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002126 {
2127 oldBandE[c*nbEBands+i]=0;
2128 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2129 }
flimc91ee5b2016-01-26 14:33:44 +01002130 for (i=end;i<nbEBands;i++)
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002131 {
2132 oldBandE[c*nbEBands+i]=0;
2133 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2134 }
2135 } while (++c<CC);
2136
2137 if (isTransient || transient_got_disabled)
2138 st->consec_transient++;
2139 else
2140 st->consec_transient=0;
2141 st->rng = enc->rng;
2142
2143 /* If there's any room left (can only happen for very high rates),
2144 it's already filled with zeros */
2145 ec_enc_done(enc);
2146
2147#ifdef CUSTOM_MODES
2148 if (st->signalling)
2149 nbCompressedBytes++;
2150#endif
2151
2152 RESTORE_STACK;
2153 if (ec_get_error(enc))
2154 return OPUS_INTERNAL_ERROR;
2155 else
2156 return nbCompressedBytes;
2157}
2158
2159
2160#ifdef CUSTOM_MODES
2161
2162#ifdef FIXED_POINT
2163int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2164{
2165 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
2166}
2167
2168#ifndef DISABLE_FLOAT_API
2169int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2170{
2171 int j, ret, C, N;
2172 VARDECL(opus_int16, in);
2173 ALLOC_STACK;
2174
2175 if (pcm==NULL)
2176 return OPUS_BAD_ARG;
2177
2178 C = st->channels;
2179 N = frame_size;
2180 ALLOC(in, C*N, opus_int16);
2181
2182 for (j=0;j<C*N;j++)
2183 in[j] = FLOAT2INT16(pcm[j]);
2184
2185 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2186#ifdef RESYNTH
2187 for (j=0;j<C*N;j++)
2188 ((float*)pcm)[j]=in[j]*(1.f/32768.f);
2189#endif
2190 RESTORE_STACK;
2191 return ret;
2192}
2193#endif /* DISABLE_FLOAT_API */
2194#else
2195
2196int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2197{
2198 int j, ret, C, N;
2199 VARDECL(celt_sig, in);
2200 ALLOC_STACK;
2201
2202 if (pcm==NULL)
2203 return OPUS_BAD_ARG;
2204
2205 C=st->channels;
2206 N=frame_size;
2207 ALLOC(in, C*N, celt_sig);
2208 for (j=0;j<C*N;j++) {
2209 in[j] = SCALEOUT(pcm[j]);
2210 }
2211
2212 ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2213#ifdef RESYNTH
2214 for (j=0;j<C*N;j++)
2215 ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
2216#endif
2217 RESTORE_STACK;
2218 return ret;
2219}
2220
2221int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2222{
2223 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
2224}
2225
2226#endif
2227
2228#endif /* CUSTOM_MODES */
2229
2230int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...)
2231{
2232 va_list ap;
2233
2234 va_start(ap, request);
2235 switch (request)
2236 {
2237 case OPUS_SET_COMPLEXITY_REQUEST:
2238 {
2239 int value = va_arg(ap, opus_int32);
2240 if (value<0 || value>10)
2241 goto bad_arg;
2242 st->complexity = value;
2243 }
2244 break;
2245 case CELT_SET_START_BAND_REQUEST:
2246 {
2247 opus_int32 value = va_arg(ap, opus_int32);
2248 if (value<0 || value>=st->mode->nbEBands)
2249 goto bad_arg;
2250 st->start = value;
2251 }
2252 break;
2253 case CELT_SET_END_BAND_REQUEST:
2254 {
2255 opus_int32 value = va_arg(ap, opus_int32);
2256 if (value<1 || value>st->mode->nbEBands)
2257 goto bad_arg;
2258 st->end = value;
2259 }
2260 break;
2261 case CELT_SET_PREDICTION_REQUEST:
2262 {
2263 int value = va_arg(ap, opus_int32);
2264 if (value<0 || value>2)
2265 goto bad_arg;
2266 st->disable_pf = value<=1;
2267 st->force_intra = value==0;
2268 }
2269 break;
2270 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2271 {
2272 int value = va_arg(ap, opus_int32);
2273 if (value<0 || value>100)
2274 goto bad_arg;
2275 st->loss_rate = value;
2276 }
2277 break;
2278 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2279 {
2280 opus_int32 value = va_arg(ap, opus_int32);
2281 st->constrained_vbr = value;
2282 }
2283 break;
2284 case OPUS_SET_VBR_REQUEST:
2285 {
2286 opus_int32 value = va_arg(ap, opus_int32);
2287 st->vbr = value;
2288 }
2289 break;
2290 case OPUS_SET_BITRATE_REQUEST:
2291 {
2292 opus_int32 value = va_arg(ap, opus_int32);
2293 if (value<=500 && value!=OPUS_BITRATE_MAX)
2294 goto bad_arg;
2295 value = IMIN(value, 260000*st->channels);
2296 st->bitrate = value;
2297 }
2298 break;
2299 case CELT_SET_CHANNELS_REQUEST:
2300 {
2301 opus_int32 value = va_arg(ap, opus_int32);
2302 if (value<1 || value>2)
2303 goto bad_arg;
2304 st->stream_channels = value;
2305 }
2306 break;
2307 case OPUS_SET_LSB_DEPTH_REQUEST:
2308 {
2309 opus_int32 value = va_arg(ap, opus_int32);
2310 if (value<8 || value>24)
2311 goto bad_arg;
2312 st->lsb_depth=value;
2313 }
2314 break;
2315 case OPUS_GET_LSB_DEPTH_REQUEST:
2316 {
2317 opus_int32 *value = va_arg(ap, opus_int32*);
2318 *value=st->lsb_depth;
2319 }
2320 break;
2321 case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
2322 {
2323 opus_int32 value = va_arg(ap, opus_int32);
2324 st->variable_duration = value;
2325 }
2326 break;
2327 case OPUS_RESET_STATE:
2328 {
2329 int i;
2330 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
flimc91ee5b2016-01-26 14:33:44 +01002331 oldBandE = (opus_val16*)(st->in_mem+st->channels*(st->mode->overlap+COMBFILTER_MAXPERIOD));
Vignesh Venkatasubramanian2bd8b542014-02-20 10:50:35 -08002332 oldLogE = oldBandE + st->channels*st->mode->nbEBands;
2333 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
2334 OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
2335 opus_custom_encoder_get_size(st->mode, st->channels)-
2336 ((char*)&st->ENCODER_RESET_START - (char*)st));
2337 for (i=0;i<st->channels*st->mode->nbEBands;i++)
2338 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
2339 st->vbr_offset = 0;
2340 st->delayedIntra = 1;
2341 st->spread_decision = SPREAD_NORMAL;
2342 st->tonal_average = 256;
2343 st->hf_average = 0;
2344 st->tapset_decision = 0;
2345 }
2346 break;
2347#ifdef CUSTOM_MODES
2348 case CELT_SET_INPUT_CLIPPING_REQUEST:
2349 {
2350 opus_int32 value = va_arg(ap, opus_int32);
2351 st->clip = value;
2352 }
2353 break;
2354#endif
2355 case CELT_SET_SIGNALLING_REQUEST:
2356 {
2357 opus_int32 value = va_arg(ap, opus_int32);
2358 st->signalling = value;
2359 }
2360 break;
2361 case CELT_SET_ANALYSIS_REQUEST:
2362 {
2363 AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
2364 if (info)
2365 OPUS_COPY(&st->analysis, info, 1);
2366 }
2367 break;
2368 case CELT_GET_MODE_REQUEST:
2369 {
2370 const CELTMode ** value = va_arg(ap, const CELTMode**);
2371 if (value==0)
2372 goto bad_arg;
2373 *value=st->mode;
2374 }
2375 break;
2376 case OPUS_GET_FINAL_RANGE_REQUEST:
2377 {
2378 opus_uint32 * value = va_arg(ap, opus_uint32 *);
2379 if (value==0)
2380 goto bad_arg;
2381 *value=st->rng;
2382 }
2383 break;
2384 case OPUS_SET_LFE_REQUEST:
2385 {
2386 opus_int32 value = va_arg(ap, opus_int32);
2387 st->lfe = value;
2388 }
2389 break;
2390 case OPUS_SET_ENERGY_MASK_REQUEST:
2391 {
2392 opus_val16 *value = va_arg(ap, opus_val16*);
2393 st->energy_mask = value;
2394 }
2395 break;
2396 default:
2397 goto bad_request;
2398 }
2399 va_end(ap);
2400 return OPUS_OK;
2401bad_arg:
2402 va_end(ap);
2403 return OPUS_BAD_ARG;
2404bad_request:
2405 va_end(ap);
2406 return OPUS_UNIMPLEMENTED;
2407}