blob: 053e5a3b2900c16f2907fb03be537ce3b5b1092b [file] [log] [blame]
Jean-Marc Valin69062102012-11-08 09:42:27 -05001/* 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
Jean-Marc Valin1ecb7ea2012-11-08 11:25:20 -050034#define CELT_ENCODER_C
Jean-Marc Valin69062102012-11-08 09:42:27 -050035
Aurélien Zanellicd4c8242013-05-31 15:07:00 +020036#include "cpu_support.h"
Jean-Marc Valin69062102012-11-08 09:42:27 -050037#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 */
Jean-Marc Valin69062102012-11-08 09:42:27 -050060 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;
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -040076 int lfe;
Jean-Marc Valin18a380a2016-04-20 13:27:06 -040077 int disable_inv;
Aurélien Zanellicd4c8242013-05-31 15:07:00 +020078 int arch;
Jean-Marc Valin69062102012-11-08 09:42:27 -050079
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;
Jean-Marc Valin61714e92016-04-23 00:34:53 -0400101 SILKInfo silk_info;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500102
103 opus_val32 preemph_memE[2];
104 opus_val32 preemph_memD[2];
105
106 /* VBR-related parameters */
107 opus_int32 vbr_reservoir;
108 opus_int32 vbr_drift;
109 opus_int32 vbr_offset;
110 opus_int32 vbr_count;
Jean-Marc Valinb7bd4c22013-05-18 23:33:48 -0400111 opus_val32 overlap_max;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500112 opus_val16 stereo_saving;
113 int intensity;
Jean-Marc Valina4dccd32013-05-04 23:54:20 -0400114 opus_val16 *energy_mask;
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -0400115 opus_val16 spec_avg;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500116
117#ifdef RESYNTH
118 /* +MAX_PERIOD/2 to make space for overlap */
119 celt_sig syn_mem[2][2*MAX_PERIOD+MAX_PERIOD/2];
120#endif
121
122 celt_sig in_mem[1]; /* Size = channels*mode->overlap */
123 /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_MAXPERIOD */
124 /* opus_val16 oldBandE[], Size = channels*mode->nbEBands */
125 /* opus_val16 oldLogE[], Size = channels*mode->nbEBands */
126 /* opus_val16 oldLogE2[], Size = channels*mode->nbEBands */
Jean-Marc Valin6fccb4b2016-07-04 01:06:11 -0400127 /* opus_val16 energyError[], Size = channels*mode->nbEBands */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500128};
129
130int celt_encoder_get_size(int channels)
131{
132 CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
133 return opus_custom_encoder_get_size(mode, channels);
134}
135
136OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
137{
138 int size = sizeof(struct CELTEncoder)
139 + (channels*mode->overlap-1)*sizeof(celt_sig) /* celt_sig in_mem[channels*mode->overlap]; */
140 + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig) /* celt_sig prefilter_mem[channels*COMBFILTER_MAXPERIOD]; */
Jean-Marc Valin6fccb4b2016-07-04 01:06:11 -0400141 + 4*channels*mode->nbEBands*sizeof(opus_val16); /* opus_val16 oldBandE[channels*mode->nbEBands]; */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500142 /* opus_val16 oldLogE[channels*mode->nbEBands]; */
143 /* opus_val16 oldLogE2[channels*mode->nbEBands]; */
Jean-Marc Valin6fccb4b2016-07-04 01:06:11 -0400144 /* opus_val16 energyError[channels*mode->nbEBands]; */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500145 return size;
146}
147
148#ifdef CUSTOM_MODES
149CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
150{
151 int ret;
152 CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels));
153 /* init will handle the NULL case */
154 ret = opus_custom_encoder_init(st, mode, channels);
155 if (ret != OPUS_OK)
156 {
157 opus_custom_encoder_destroy(st);
158 st = NULL;
159 }
160 if (error)
161 *error = ret;
162 return st;
163}
164#endif /* CUSTOM_MODES */
165
Timothy B. Terriberry39386e02013-11-18 13:30:13 -0500166static int opus_custom_encoder_init_arch(CELTEncoder *st, const CELTMode *mode,
167 int channels, int arch)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500168{
169 if (channels < 0 || channels > 2)
170 return OPUS_BAD_ARG;
171
172 if (st==NULL || mode==NULL)
173 return OPUS_ALLOC_FAIL;
174
175 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
176
177 st->mode = mode;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500178 st->stream_channels = st->channels = channels;
179
180 st->upsample = 1;
181 st->start = 0;
182 st->end = st->mode->effEBands;
183 st->signalling = 1;
Timothy B. Terriberry39386e02013-11-18 13:30:13 -0500184 st->arch = arch;
Aurélien Zanellicd4c8242013-05-31 15:07:00 +0200185
Jean-Marc Valin69062102012-11-08 09:42:27 -0500186 st->constrained_vbr = 1;
187 st->clip = 1;
188
189 st->bitrate = OPUS_BITRATE_MAX;
190 st->vbr = 0;
191 st->force_intra = 0;
192 st->complexity = 5;
193 st->lsb_depth=24;
194
195 opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
196
197 return OPUS_OK;
198}
199
Jean-Marc Valin41e89062013-11-20 19:34:14 -0500200#ifdef CUSTOM_MODES
201int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
Timothy B. Terriberry39386e02013-11-18 13:30:13 -0500202{
203 return opus_custom_encoder_init_arch(st, mode, channels, opus_select_arch());
204}
Jean-Marc Valin41e89062013-11-20 19:34:14 -0500205#endif
Timothy B. Terriberry39386e02013-11-18 13:30:13 -0500206
207int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels,
208 int arch)
209{
210 int ret;
211 ret = opus_custom_encoder_init_arch(st,
212 opus_custom_mode_create(48000, 960, NULL), channels, arch);
213 if (ret != OPUS_OK)
214 return ret;
215 st->upsample = resampling_factor(sampling_rate);
216 return OPUS_OK;
217}
218
Jean-Marc Valin69062102012-11-08 09:42:27 -0500219#ifdef CUSTOM_MODES
220void opus_custom_encoder_destroy(CELTEncoder *st)
221{
222 opus_free(st);
223}
224#endif /* CUSTOM_MODES */
225
226
227static int transient_analysis(const opus_val32 * OPUS_RESTRICT in, int len, int C,
Jean-Marc Valin7e122392016-10-29 17:02:36 -0400228 opus_val16 *tf_estimate, int *tf_chan, int allow_weak_transients,
Jean-Marc Valin90f20c62016-10-28 16:44:38 -0400229 int *weak_transient)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500230{
231 int i;
232 VARDECL(opus_val16, tmp);
233 opus_val32 mem0,mem1;
234 int is_transient = 0;
235 opus_int32 mask_metric = 0;
236 int c;
Jean-Marc Valind683c762012-12-21 16:17:38 -0500237 opus_val16 tf_max;
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500238 int len2;
Jean-Marc Valin2af92cd2016-10-27 14:14:28 -0400239 /* Forward masking: 6.7 dB/ms. */
240#ifdef FIXED_POINT
241 int forward_shift = 4;
242#else
243 opus_val16 forward_decay = QCONST16(.0625f,15);
244#endif
Jean-Marc Valin69062102012-11-08 09:42:27 -0500245 /* Table of 6*64/x, trained on real data to minimize the average error */
246 static const unsigned char inv_table[128] = {
247 255,255,156,110, 86, 70, 59, 51, 45, 40, 37, 33, 31, 28, 26, 25,
248 23, 22, 21, 20, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
249 12, 12, 11, 11, 11, 10, 10, 10, 9, 9, 9, 9, 9, 9, 8, 8,
250 8, 8, 8, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6,
251 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5,
252 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
253 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3,
254 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2,
255 };
256 SAVE_STACK;
257 ALLOC(tmp, len, opus_val16);
258
Jean-Marc Valin90f20c62016-10-28 16:44:38 -0400259 *weak_transient = 0;
Jean-Marc Valin2af92cd2016-10-27 14:14:28 -0400260 /* For lower bitrates, let's be more conservative and have a forward masking
261 decay of 3.3 dB/ms. This avoids having to code transients at very low
262 bitrate (mostly for hybrid), which can result in unstable energy and/or
263 partial collapse. */
Jean-Marc Valin7e122392016-10-29 17:02:36 -0400264 if (allow_weak_transients)
Jean-Marc Valin2af92cd2016-10-27 14:14:28 -0400265 {
266#ifdef FIXED_POINT
267 forward_shift = 5;
268#else
269 forward_decay = QCONST16(.03125f,15);
270#endif
271 }
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500272 len2=len/2;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500273 for (c=0;c<C;c++)
274 {
275 opus_val32 mean;
276 opus_int32 unmask=0;
277 opus_val32 norm;
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500278 opus_val16 maxE;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500279 mem0=0;
280 mem1=0;
281 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
282 for (i=0;i<len;i++)
283 {
284 opus_val32 x,y;
285 x = SHR32(in[i+c*len],SIG_SHIFT);
286 y = ADD32(mem0, x);
287#ifdef FIXED_POINT
288 mem0 = mem1 + y - SHL32(x,1);
289 mem1 = x - SHR32(y,1);
290#else
291 mem0 = mem1 + y - 2*x;
292 mem1 = x - .5f*y;
293#endif
Jean-Marc Valin84043f72016-07-24 17:54:56 -0400294 tmp[i] = SROUND16(y, 2);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500295 /*printf("%f ", tmp[i]);*/
296 }
297 /*printf("\n");*/
298 /* First few samples are bad because we don't propagate the memory */
Jean-Marc Valinff072002013-12-08 23:31:30 -0500299 OPUS_CLEAR(tmp, 12);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500300
301#ifdef FIXED_POINT
302 /* Normalize tmp to max range */
303 {
304 int shift=0;
Jean-Marc Valin84043f72016-07-24 17:54:56 -0400305 shift = 14-celt_ilog2(MAX16(1, celt_maxabs16(tmp, len)));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500306 if (shift!=0)
307 {
308 for (i=0;i<len;i++)
309 tmp[i] = SHL16(tmp[i], shift);
310 }
311 }
312#endif
313
314 mean=0;
315 mem0=0;
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500316 /* Grouping by two to reduce complexity */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500317 /* Forward pass to compute the post-echo threshold*/
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500318 for (i=0;i<len2;i++)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500319 {
320 opus_val16 x2 = PSHR32(MULT16_16(tmp[2*i],tmp[2*i]) + MULT16_16(tmp[2*i+1],tmp[2*i+1]),16);
321 mean += x2;
322#ifdef FIXED_POINT
323 /* FIXME: Use PSHR16() instead */
Jean-Marc Valin2af92cd2016-10-27 14:14:28 -0400324 tmp[i] = mem0 + PSHR32(x2-mem0,forward_shift);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500325#else
Jean-Marc Valin2af92cd2016-10-27 14:14:28 -0400326 tmp[i] = mem0 + MULT16_16_P15(forward_decay,x2-mem0);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500327#endif
328 mem0 = tmp[i];
329 }
330
331 mem0=0;
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500332 maxE=0;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500333 /* Backward pass to compute the pre-echo threshold */
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500334 for (i=len2-1;i>=0;i--)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500335 {
Jean-Marc Valin2af92cd2016-10-27 14:14:28 -0400336 /* Backward masking: 13.9 dB/ms. */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500337#ifdef FIXED_POINT
338 /* FIXME: Use PSHR16() instead */
339 tmp[i] = mem0 + PSHR32(tmp[i]-mem0,3);
340#else
341 tmp[i] = mem0 + MULT16_16_P15(QCONST16(0.125f,15),tmp[i]-mem0);
342#endif
343 mem0 = tmp[i];
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500344 maxE = MAX16(maxE, mem0);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500345 }
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500346 /*for (i=0;i<len2;i++)printf("%f ", tmp[i]/mean);printf("\n");*/
Jean-Marc Valin69062102012-11-08 09:42:27 -0500347
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500348 /* Compute the ratio of the "frame energy" over the harmonic mean of the energy.
Jean-Marc Valin69062102012-11-08 09:42:27 -0500349 This essentially corresponds to a bitrate-normalized temporal noise-to-mask
350 ratio */
351
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500352 /* As a compromise with the old transient detector, frame energy is the
353 geometric mean of the energy and half the max */
354#ifdef FIXED_POINT
355 /* Costs two sqrt() to avoid overflows */
356 mean = MULT16_16(celt_sqrt(mean), celt_sqrt(MULT16_16(maxE,len2>>1)));
357#else
Gregory Maxwell5280c712013-07-15 15:51:24 -0700358 mean = celt_sqrt(mean * maxE*.5*len2);
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500359#endif
Jean-Marc Valin69062102012-11-08 09:42:27 -0500360 /* Inverse of the mean energy in Q15+6 */
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500361 norm = SHL32(EXTEND32(len2),6+14)/ADD32(EPSILON,SHR32(mean,1));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500362 /* Compute harmonic mean discarding the unreliable boundaries
363 The data is smooth, so we only take 1/4th of the samples */
364 unmask=0;
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500365 for (i=12;i<len2-5;i+=4)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500366 {
367 int id;
368#ifdef FIXED_POINT
Jean-Marc Valineda57aa2015-12-03 14:12:01 -0500369 id = MAX32(0,MIN32(127,MULT16_32_Q15(tmp[i]+EPSILON,norm))); /* Do not round to nearest */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500370#else
Jean-Marc Valineda57aa2015-12-03 14:12:01 -0500371 id = (int)MAX32(0,MIN32(127,floor(64*norm*(tmp[i]+EPSILON)))); /* Do not round to nearest */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500372#endif
373 unmask += inv_table[id];
374 }
375 /*printf("%d\n", unmask);*/
376 /* Normalize, compensate for the 1/4th of the sample and the factor of 6 in the inverse table */
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500377 unmask = 64*unmask*4/(6*(len2-17));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500378 if (unmask>mask_metric)
379 {
380 *tf_chan = c;
381 mask_metric = unmask;
382 }
383 }
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500384 is_transient = mask_metric>200;
Jean-Marc Valin90f20c62016-10-28 16:44:38 -0400385 /* For low bitrates, define "weak transients" that need to be
386 handled differently to avoid partial collapse. */
Jean-Marc Valin7e122392016-10-29 17:02:36 -0400387 if (allow_weak_transients && is_transient && mask_metric<600) {
Jean-Marc Valin90f20c62016-10-28 16:44:38 -0400388 is_transient = 0;
389 *weak_transient = 1;
390 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500391 /* Arbitrary metric for VBR boost */
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500392 tf_max = MAX16(0,celt_sqrt(27*mask_metric)-42);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500393 /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */
Pedro Becerraa9b7def2013-12-09 16:08:29 -0500394 *tf_estimate = celt_sqrt(MAX32(0, SHL32(MULT16_16(QCONST16(0.0069,14),MIN16(163,tf_max)),14)-QCONST32(0.139,28)));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500395 /*printf("%d %f\n", tf_max, mask_metric);*/
396 RESTORE_STACK;
397#ifdef FUZZING
398 is_transient = rand()&0x1;
399#endif
400 /*printf("%d %f %d\n", is_transient, (float)*tf_estimate, tf_max);*/
401 return is_transient;
402}
403
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500404/* Looks for sudden increases of energy to decide whether we need to patch
405 the transient decision */
Jean-Marc Valin99618092015-12-04 16:42:19 -0500406static int patch_transient_decision(opus_val16 *newE, opus_val16 *oldE, int nbEBands,
407 int start, int end, int C)
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500408{
409 int i, c;
410 opus_val32 mean_diff=0;
411 opus_val16 spread_old[26];
412 /* Apply an aggressive (-6 dB/Bark) spreading function to the old frame to
413 avoid false detection caused by irrelevant bands */
414 if (C==1)
415 {
Jean-Marc Valin99618092015-12-04 16:42:19 -0500416 spread_old[start] = oldE[start];
417 for (i=start+1;i<end;i++)
Stefan Hackera32fa312013-09-20 04:07:53 +0200418 spread_old[i] = MAX16(spread_old[i-1]-QCONST16(1.0f, DB_SHIFT), oldE[i]);
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500419 } else {
Jean-Marc Valin99618092015-12-04 16:42:19 -0500420 spread_old[start] = MAX16(oldE[start],oldE[start+nbEBands]);
421 for (i=start+1;i<end;i++)
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500422 spread_old[i] = MAX16(spread_old[i-1]-QCONST16(1.0f, DB_SHIFT),
Stefan Hackera32fa312013-09-20 04:07:53 +0200423 MAX16(oldE[i],oldE[i+nbEBands]));
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500424 }
Jean-Marc Valin99618092015-12-04 16:42:19 -0500425 for (i=end-2;i>=start;i--)
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500426 spread_old[i] = MAX16(spread_old[i], spread_old[i+1]-QCONST16(1.0f, DB_SHIFT));
427 /* Compute mean increase */
428 c=0; do {
Jean-Marc Valin99618092015-12-04 16:42:19 -0500429 for (i=IMAX(2,start);i<end-1;i++)
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500430 {
431 opus_val16 x1, x2;
Jean-Marc Valin99618092015-12-04 16:42:19 -0500432 x1 = MAX16(0, newE[i + c*nbEBands]);
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500433 x2 = MAX16(0, spread_old[i]);
434 mean_diff = ADD32(mean_diff, EXTEND32(MAX16(0, SUB16(x1, x2))));
435 }
436 } while (++c<C);
Jean-Marc Valin99618092015-12-04 16:42:19 -0500437 mean_diff = DIV32(mean_diff, C*(end-1-IMAX(2,start)));
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500438 /*printf("%f %f %d\n", mean_diff, max_diff, count);*/
439 return mean_diff > QCONST16(1.f, DB_SHIFT);
440}
441
Jean-Marc Valin69062102012-11-08 09:42:27 -0500442/** Apply window and compute the MDCT for all sub-frames and
443 all channels in a frame */
Jean-Marc Valin851f8032013-02-18 01:43:43 -0500444static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS_RESTRICT in,
Viswanath Puttaguntaf48abe82015-05-15 12:42:19 -0500445 celt_sig * OPUS_RESTRICT out, int C, int CC, int LM, int upsample,
446 int arch)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500447{
Jean-Marc Valine1f84622013-12-29 18:45:49 -0500448 const int overlap = mode->overlap;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500449 int N;
450 int B;
451 int shift;
Jean-Marc Valin851f8032013-02-18 01:43:43 -0500452 int i, b, c;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500453 if (shortBlocks)
454 {
455 B = shortBlocks;
456 N = mode->shortMdctSize;
457 shift = mode->maxLM;
458 } else {
459 B = 1;
460 N = mode->shortMdctSize<<LM;
461 shift = mode->maxLM-LM;
462 }
463 c=0; do {
464 for (b=0;b<B;b++)
465 {
466 /* Interleaving the sub-frames while doing the MDCTs */
Viswanath Puttaguntaf48abe82015-05-15 12:42:19 -0500467 clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N,
468 &out[b+c*N*B], mode->window, overlap, shift, B,
469 arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500470 }
Jean-Marc Valin851f8032013-02-18 01:43:43 -0500471 } while (++c<CC);
472 if (CC==2&&C==1)
473 {
474 for (i=0;i<B*N;i++)
475 out[i] = ADD32(HALF32(out[i]), HALF32(out[B*N+i]));
476 }
477 if (upsample != 1)
478 {
479 c=0; do
480 {
481 int bound = B*N/upsample;
482 for (i=0;i<bound;i++)
483 out[c*B*N+i] *= upsample;
Jean-Marc Valinff072002013-12-08 23:31:30 -0500484 OPUS_CLEAR(&out[c*B*N+bound], B*N-bound);
Jean-Marc Valin851f8032013-02-18 01:43:43 -0500485 } while (++c<C);
486 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500487}
488
489
Jean-Marc Valin2dc27df2013-11-13 19:35:43 -0500490void celt_preemphasis(const opus_val16 * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTRICT inp,
Jean-Marc Valin69062102012-11-08 09:42:27 -0500491 int N, int CC, int upsample, const opus_val16 *coef, celt_sig *mem, int clip)
492{
493 int i;
Jean-Marc Valine368e622013-01-03 14:28:28 -0500494 opus_val16 coef0;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500495 celt_sig m;
496 int Nu;
497
498 coef0 = coef[0];
Jean-Marc Valin56269082013-12-05 16:40:59 -0500499 m = *mem;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500500
Jean-Marc Valinc94e4bb2013-12-08 03:31:50 -0500501 /* Fast path for the normal 48kHz case and no clipping */
502 if (coef[1] == 0 && upsample == 1 && !clip)
Jean-Marc Valin56269082013-12-05 16:40:59 -0500503 {
504 for (i=0;i<N;i++)
505 {
506 opus_val16 x;
507 x = SCALEIN(pcmp[CC*i]);
508 /* Apply pre-emphasis */
509 inp[i] = SHL32(x, SIG_SHIFT) - m;
510 m = SHR32(MULT16_16(coef0, x), 15-SIG_SHIFT);
511 }
512 *mem = m;
513 return;
514 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500515
516 Nu = N/upsample;
517 if (upsample!=1)
518 {
Jean-Marc Valinff072002013-12-08 23:31:30 -0500519 OPUS_CLEAR(inp, N);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500520 }
521 for (i=0;i<Nu;i++)
Jean-Marc Valinc94e4bb2013-12-08 03:31:50 -0500522 inp[i*upsample] = SCALEIN(pcmp[CC*i]);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500523
524#ifndef FIXED_POINT
525 if (clip)
526 {
527 /* Clip input to avoid encoding non-portable files */
528 for (i=0;i<Nu;i++)
529 inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample]));
530 }
Jean-Marc Valind6eb9c42013-11-25 22:33:43 -0500531#else
532 (void)clip; /* Avoids a warning about clip being unused. */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500533#endif
Jean-Marc Valine368e622013-01-03 14:28:28 -0500534#ifdef CUSTOM_MODES
535 if (coef[1] != 0)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500536 {
Jean-Marc Valine368e622013-01-03 14:28:28 -0500537 opus_val16 coef1 = coef[1];
Jean-Marc Valin69062102012-11-08 09:42:27 -0500538 opus_val16 coef2 = coef[2];
539 for (i=0;i<N;i++)
540 {
Jean-Marc Valin96408b62013-12-02 20:02:37 -0500541 celt_sig x, tmp;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500542 x = inp[i];
543 /* Apply pre-emphasis */
544 tmp = MULT16_16(coef2, x);
545 inp[i] = tmp + m;
546 m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp);
547 }
Jean-Marc Valine368e622013-01-03 14:28:28 -0500548 } else
Jean-Marc Valinebdfbfb2013-01-09 11:13:00 -0500549#endif
Jean-Marc Valine368e622013-01-03 14:28:28 -0500550 {
551 for (i=0;i<N;i++)
552 {
Jean-Marc Valinaed10092013-12-05 13:36:48 -0500553 opus_val16 x;
554 x = inp[i];
Jean-Marc Valine368e622013-01-03 14:28:28 -0500555 /* Apply pre-emphasis */
Jean-Marc Valinaed10092013-12-05 13:36:48 -0500556 inp[i] = SHL32(x, SIG_SHIFT) - m;
557 m = SHR32(MULT16_16(coef0, x), 15-SIG_SHIFT);
Jean-Marc Valine368e622013-01-03 14:28:28 -0500558 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500559 }
560 *mem = m;
561}
562
563
564
565static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, opus_val16 bias)
566{
567 int i;
568 opus_val32 L1;
569 L1 = 0;
570 for (i=0;i<N;i++)
571 L1 += EXTEND32(ABS16(tmp[i]));
572 /* When in doubt, prefer good freq resolution */
573 L1 = MAC16_32_Q15(L1, LM*bias, L1);
574 return L1;
575
576}
577
Jean-Marc Valina6d663c2012-11-08 13:26:49 -0500578static int tf_analysis(const CELTMode *m, int len, int isTransient,
579 int *tf_res, int lambda, celt_norm *X, int N0, int LM,
Jean-Marc Valin2ca6df02016-06-08 02:15:21 -0400580 opus_val16 tf_estimate, int tf_chan)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500581{
582 int i;
583 VARDECL(int, metric);
584 int cost0;
585 int cost1;
586 VARDECL(int, path0);
587 VARDECL(int, path1);
588 VARDECL(celt_norm, tmp);
589 VARDECL(celt_norm, tmp_1);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500590 int sel;
591 int selcost[2];
592 int tf_select=0;
593 opus_val16 bias;
594
595 SAVE_STACK;
Jean-Marc Valindae16fb2012-12-13 21:40:58 -0500596 bias = MULT16_16_Q14(QCONST16(.04f,15), MAX16(-QCONST16(.25f,14), QCONST16(.5f,14)-tf_estimate));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500597 /*printf("%f ", bias);*/
598
Jean-Marc Valin69062102012-11-08 09:42:27 -0500599 ALLOC(metric, len, int);
600 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
601 ALLOC(tmp_1, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
602 ALLOC(path0, len, int);
603 ALLOC(path1, len, int);
604
Jean-Marc Valin69062102012-11-08 09:42:27 -0500605 for (i=0;i<len;i++)
606 {
Jean-Marc Valin57cd8492013-12-09 02:33:42 -0500607 int k, N;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500608 int narrow;
609 opus_val32 L1, best_L1;
610 int best_level=0;
611 N = (m->eBands[i+1]-m->eBands[i])<<LM;
612 /* band is too narrow to be split down to LM=-1 */
613 narrow = (m->eBands[i+1]-m->eBands[i])==1;
Jean-Marc Valinff072002013-12-08 23:31:30 -0500614 OPUS_COPY(tmp, &X[tf_chan*N0 + (m->eBands[i]<<LM)], N);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500615 /* Just add the right channel if we're in stereo */
616 /*if (C==2)
617 for (j=0;j<N;j++)
618 tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));*/
619 L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias);
620 best_L1 = L1;
621 /* Check the -1 case for transients */
622 if (isTransient && !narrow)
623 {
Jean-Marc Valinff072002013-12-08 23:31:30 -0500624 OPUS_COPY(tmp_1, tmp, N);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500625 haar1(tmp_1, N>>LM, 1<<LM);
626 L1 = l1_metric(tmp_1, N, LM+1, bias);
627 if (L1<best_L1)
628 {
629 best_L1 = L1;
630 best_level = -1;
631 }
632 }
633 /*printf ("%f ", L1);*/
634 for (k=0;k<LM+!(isTransient||narrow);k++)
635 {
636 int B;
637
638 if (isTransient)
639 B = (LM-k-1);
640 else
641 B = k+1;
642
643 haar1(tmp, N>>k, 1<<k);
644
645 L1 = l1_metric(tmp, N, B, bias);
646
647 if (L1 < best_L1)
648 {
649 best_L1 = L1;
650 best_level = k+1;
651 }
652 }
653 /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
654 /* metric is in Q1 to be able to select the mid-point (-0.5) for narrower bands */
655 if (isTransient)
656 metric[i] = 2*best_level;
657 else
658 metric[i] = -2*best_level;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500659 /* For bands that can't be split to -1, set the metric to the half-way point to avoid
660 biasing the decision */
661 if (narrow && (metric[i]==0 || metric[i]==-2*LM))
662 metric[i]-=1;
663 /*printf("%d ", metric[i]);*/
664 }
665 /*printf("\n");*/
666 /* Search for the optimal tf resolution, including tf_select */
667 tf_select = 0;
668 for (sel=0;sel<2;sel++)
669 {
670 cost0 = 0;
671 cost1 = isTransient ? 0 : lambda;
672 for (i=1;i<len;i++)
673 {
674 int curr0, curr1;
675 curr0 = IMIN(cost0, cost1 + lambda);
676 curr1 = IMIN(cost0 + lambda, cost1);
677 cost0 = curr0 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
678 cost1 = curr1 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+1]);
679 }
680 cost0 = IMIN(cost0, cost1);
681 selcost[sel]=cost0;
682 }
683 /* For now, we're conservative and only allow tf_select=1 for transients.
684 * If tests confirm it's useful for non-transients, we could allow it. */
685 if (selcost[1]<selcost[0] && isTransient)
686 tf_select=1;
687 cost0 = 0;
688 cost1 = isTransient ? 0 : lambda;
689 /* Viterbi forward pass */
690 for (i=1;i<len;i++)
691 {
692 int curr0, curr1;
693 int from0, from1;
694
695 from0 = cost0;
696 from1 = cost1 + lambda;
697 if (from0 < from1)
698 {
699 curr0 = from0;
700 path0[i]= 0;
701 } else {
702 curr0 = from1;
703 path0[i]= 1;
704 }
705
706 from0 = cost0 + lambda;
707 from1 = cost1;
708 if (from0 < from1)
709 {
710 curr1 = from0;
711 path1[i]= 0;
712 } else {
713 curr1 = from1;
714 path1[i]= 1;
715 }
716 cost0 = curr0 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
717 cost1 = curr1 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]);
718 }
719 tf_res[len-1] = cost0 < cost1 ? 0 : 1;
720 /* Viterbi backward pass to check the decisions */
721 for (i=len-2;i>=0;i--)
722 {
723 if (tf_res[i+1] == 1)
724 tf_res[i] = path1[i+1];
725 else
726 tf_res[i] = path0[i+1];
727 }
728 /*printf("%d %f\n", *tf_sum, tf_estimate);*/
729 RESTORE_STACK;
730#ifdef FUZZING
731 tf_select = rand()&0x1;
732 tf_res[0] = rand()&0x1;
733 for (i=1;i<len;i++)
734 tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
735#endif
736 return tf_select;
737}
738
739static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
740{
741 int curr, i;
742 int tf_select_rsv;
743 int tf_changed;
744 int logp;
745 opus_uint32 budget;
746 opus_uint32 tell;
747 budget = enc->storage*8;
748 tell = ec_tell(enc);
749 logp = isTransient ? 2 : 4;
750 /* Reserve space to code the tf_select decision. */
751 tf_select_rsv = LM>0 && tell+logp+1 <= budget;
752 budget -= tf_select_rsv;
753 curr = tf_changed = 0;
754 for (i=start;i<end;i++)
755 {
756 if (tell+logp<=budget)
757 {
758 ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
759 tell = ec_tell(enc);
760 curr = tf_res[i];
761 tf_changed |= curr;
762 }
763 else
764 tf_res[i] = curr;
765 logp = isTransient ? 4 : 5;
766 }
767 /* Only code tf_select if it would actually make a difference. */
768 if (tf_select_rsv &&
769 tf_select_table[LM][4*isTransient+0+tf_changed]!=
770 tf_select_table[LM][4*isTransient+2+tf_changed])
771 ec_enc_bit_logp(enc, tf_select, 1);
772 else
773 tf_select = 0;
774 for (i=start;i<end;i++)
775 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
776 /*for(i=0;i<end;i++)printf("%d ", isTransient ? tf_res[i] : LM+tf_res[i]);printf("\n");*/
777}
778
779
780static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
781 const opus_val16 *bandLogE, int end, int LM, int C, int N0,
782 AnalysisInfo *analysis, opus_val16 *stereo_saving, opus_val16 tf_estimate,
Jean-Marc Valina671ac52017-05-24 01:07:20 -0400783 int intensity, opus_val16 surround_trim, opus_int32 equiv_rate, int arch)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500784{
785 int i;
786 opus_val32 diff=0;
787 int c;
Jean-Marc Valin4a168eb2013-12-11 01:34:06 -0500788 int trim_index;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500789 opus_val16 trim = QCONST16(5.f, 8);
790 opus_val16 logXC, logXC2;
Jean-Marc Valina671ac52017-05-24 01:07:20 -0400791 /* At low bitrate, reducing the trim seems to help. At higher bitrates, it's less
792 clear what's best, so we're keeping it as it was before, at least for now. */
793 if (equiv_rate < 64000) {
794 trim = QCONST16(4.f, 8);
795 } else if (equiv_rate < 80000) {
796 opus_int32 frac = (equiv_rate-64000) >> 10;
Jean-Marc Valin59b907c2017-05-24 14:21:08 -0400797 trim = QCONST16(4.f, 8) + QCONST16(1.f/16.f, 8)*frac;
Jean-Marc Valina671ac52017-05-24 01:07:20 -0400798 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500799 if (C==2)
800 {
801 opus_val16 sum = 0; /* Q10 */
802 opus_val16 minXC; /* Q10 */
803 /* Compute inter-channel correlation for low frequencies */
804 for (i=0;i<8;i++)
805 {
Jean-Marc Valin57cd8492013-12-09 02:33:42 -0500806 opus_val32 partial;
xiangmingzhuc95c9a02014-04-30 15:48:07 +0800807 partial = celt_inner_prod(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM)],
808 (m->eBands[i+1]-m->eBands[i])<<LM, arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500809 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
810 }
811 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
812 sum = MIN16(QCONST16(1.f, 10), ABS16(sum));
813 minXC = sum;
814 for (i=8;i<intensity;i++)
815 {
Jean-Marc Valin57cd8492013-12-09 02:33:42 -0500816 opus_val32 partial;
xiangmingzhuc95c9a02014-04-30 15:48:07 +0800817 partial = celt_inner_prod(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM)],
818 (m->eBands[i+1]-m->eBands[i])<<LM, arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500819 minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18))));
820 }
821 minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC));
822 /*printf ("%f\n", sum);*/
Jean-Marc Valin69062102012-11-08 09:42:27 -0500823 /* mid-side savings estimations based on the LF average*/
824 logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum));
825 /* mid-side savings estimations based on min correlation */
826 logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(minXC, minXC)));
827#ifdef FIXED_POINT
828 /* Compensate for Q20 vs Q14 input and convert output to Q8 */
829 logXC = PSHR32(logXC-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8);
830 logXC2 = PSHR32(logXC2-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8);
831#endif
832
833 trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC));
834 *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2));
835 }
836
837 /* Estimate spectral tilt */
838 c=0; do {
839 for (i=0;i<end-1;i++)
840 {
841 diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-end);
842 }
843 } while (++c<C);
844 diff /= C*(end-1);
845 /*printf("%f\n", diff);*/
Jean-Marc Valin3609a222017-05-24 01:21:51 -0400846 trim -= MAX32(-QCONST16(2.f, 8), MIN32(QCONST16(2.f, 8), SHR32(diff+QCONST16(1.f, DB_SHIFT),DB_SHIFT-8)/6 ));
Jean-Marc Valin0f686962013-09-05 12:49:55 -0400847 trim -= SHR16(surround_trim, DB_SHIFT-8);
Jean-Marc Valindae16fb2012-12-13 21:40:58 -0500848 trim -= 2*SHR16(tf_estimate, 14-8);
Jean-Marc Valin3ab03e02013-09-06 16:00:39 -0400849#ifndef DISABLE_FLOAT_API
Jean-Marc Valin69062102012-11-08 09:42:27 -0500850 if (analysis->valid)
851 {
Jean-Marc Valina71c9ad2013-11-13 12:07:01 -0500852 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8),
853 (opus_val16)(QCONST16(2.f, 8)*(analysis->tonality_slope+.05f))));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500854 }
Timothy B. Terriberry23f503a2014-12-26 08:31:39 -0800855#else
856 (void)analysis;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500857#endif
858
859#ifdef FIXED_POINT
860 trim_index = PSHR32(trim, 8);
861#else
Jean-Marc Valind683c762012-12-21 16:17:38 -0500862 trim_index = (int)floor(.5f+trim);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500863#endif
Jean-Marc Valin4a168eb2013-12-11 01:34:06 -0500864 trim_index = IMAX(0, IMIN(10, trim_index));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500865 /*printf("%d\n", trim_index);*/
866#ifdef FUZZING
867 trim_index = rand()%11;
868#endif
869 return trim_index;
870}
871
872static int stereo_analysis(const CELTMode *m, const celt_norm *X,
873 int LM, int N0)
874{
875 int i;
876 int thetas;
877 opus_val32 sumLR = EPSILON, sumMS = EPSILON;
878
879 /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
880 for (i=0;i<13;i++)
881 {
882 int j;
883 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
884 {
885 opus_val32 L, R, M, S;
886 /* We cast to 32-bit first because of the -32768 case */
887 L = EXTEND32(X[j]);
888 R = EXTEND32(X[N0+j]);
889 M = ADD32(L, R);
890 S = SUB32(L, R);
891 sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
892 sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
893 }
894 }
895 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
896 thetas = 13;
897 /* We don't need thetas for lower bands with LM<=1 */
898 if (LM<=1)
899 thetas -= 8;
900 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
901 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
902}
903
Jean-Marc Valin05548fa2014-01-19 01:31:00 -0500904#define MSWAP(a,b) do {opus_val16 tmp = a;a=b;b=tmp;} while(0)
905static opus_val16 median_of_5(const opus_val16 *x)
906{
907 opus_val16 t0, t1, t2, t3, t4;
908 t2 = x[2];
909 if (x[0] > x[1])
910 {
911 t0 = x[1];
912 t1 = x[0];
913 } else {
914 t0 = x[0];
915 t1 = x[1];
916 }
917 if (x[3] > x[4])
918 {
919 t3 = x[4];
920 t4 = x[3];
921 } else {
922 t3 = x[3];
923 t4 = x[4];
924 }
925 if (t0 > t3)
926 {
927 MSWAP(t0, t3);
928 MSWAP(t1, t4);
929 }
930 if (t2 > t1)
931 {
932 if (t1 < t3)
933 return MIN16(t2, t3);
934 else
935 return MIN16(t4, t1);
936 } else {
937 if (t2 < t3)
938 return MIN16(t1, t3);
939 else
940 return MIN16(t2, t4);
941 }
942}
943
944static opus_val16 median_of_3(const opus_val16 *x)
945{
946 opus_val16 t0, t1, t2;
947 if (x[0] > x[1])
948 {
949 t0 = x[1];
950 t1 = x[0];
951 } else {
952 t0 = x[0];
953 t1 = x[1];
954 }
955 t2 = x[2];
956 if (t1 < t2)
957 return t1;
958 else if (t0 < t2)
959 return t2;
960 else
961 return t0;
962}
963
Jean-Marc Valind683c762012-12-21 16:17:38 -0500964static opus_val16 dynalloc_analysis(const opus_val16 *bandLogE, const opus_val16 *bandLogE2,
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500965 int nbEBands, int start, int end, int C, int *offsets, int lsb_depth, const opus_int16 *logN,
966 int isTransient, int vbr, int constrained_vbr, const opus_int16 *eBands, int LM,
Jean-Marc Valin0cc4d962017-06-01 03:14:13 -0400967 int effectiveBytes, opus_int32 *tot_boost_, int lfe, opus_val16 *surround_dynalloc, AnalysisInfo *analysis)
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500968{
969 int i, c;
970 opus_int32 tot_boost=0;
971 opus_val16 maxDepth;
972 VARDECL(opus_val16, follower);
973 VARDECL(opus_val16, noise_floor);
974 SAVE_STACK;
975 ALLOC(follower, C*nbEBands, opus_val16);
976 ALLOC(noise_floor, C*nbEBands, opus_val16);
Jean-Marc Valinff072002013-12-08 23:31:30 -0500977 OPUS_CLEAR(offsets, nbEBands);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500978 /* Dynamic allocation code */
Jean-Marc Valin82594922013-07-25 13:52:42 -0400979 maxDepth=-QCONST16(31.9f, DB_SHIFT);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500980 for (i=0;i<end;i++)
981 {
982 /* Noise floor must take into account eMeans, the depth, the width of the bands
983 and the preemphasis filter (approx. square of bark band ID) */
984 noise_floor[i] = MULT16_16(QCONST16(0.0625f, DB_SHIFT),logN[i])
985 +QCONST16(.5f,DB_SHIFT)+SHL16(9-lsb_depth,DB_SHIFT)-SHL16(eMeans[i],6)
986 +MULT16_16(QCONST16(.0062,DB_SHIFT),(i+5)*(i+5));
987 }
988 c=0;do
989 {
990 for (i=0;i<end;i++)
991 maxDepth = MAX16(maxDepth, bandLogE[c*nbEBands+i]-noise_floor[i]);
992 } while (++c<C);
993 /* Make sure that dynamic allocation can't make us bust the budget */
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -0400994 if (effectiveBytes > 50 && LM>=1 && !lfe)
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500995 {
996 int last=0;
997 c=0;do
998 {
Jean-Marc Valin05548fa2014-01-19 01:31:00 -0500999 opus_val16 offset;
1000 opus_val16 tmp;
Jean-Marc Valin379af352014-01-19 01:42:04 -05001001 opus_val16 *f;
1002 f = &follower[c*nbEBands];
1003 f[0] = bandLogE2[c*nbEBands];
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001004 for (i=1;i<end;i++)
1005 {
1006 /* The last band to be at least 3 dB higher than the previous one
1007 is the last we'll consider. Otherwise, we run into problems on
1008 bandlimited signals. */
1009 if (bandLogE2[c*nbEBands+i] > bandLogE2[c*nbEBands+i-1]+QCONST16(.5f,DB_SHIFT))
1010 last=i;
Jean-Marc Valin379af352014-01-19 01:42:04 -05001011 f[i] = MIN16(f[i-1]+QCONST16(1.5f,DB_SHIFT), bandLogE2[c*nbEBands+i]);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001012 }
1013 for (i=last-1;i>=0;i--)
Jean-Marc Valin379af352014-01-19 01:42:04 -05001014 f[i] = MIN16(f[i], MIN16(f[i+1]+QCONST16(2.f,DB_SHIFT), bandLogE2[c*nbEBands+i]));
Jean-Marc Valin05548fa2014-01-19 01:31:00 -05001015
1016 /* Combine with a median filter to avoid dynalloc triggering unnecessarily.
1017 The "offset" value controls how conservative we are -- a higher offset
1018 reduces the impact of the median filter and makes dynalloc use more bits. */
1019 offset = QCONST16(1.f, DB_SHIFT);
1020 for (i=2;i<end-2;i++)
Jean-Marc Valin379af352014-01-19 01:42:04 -05001021 f[i] = MAX16(f[i], median_of_5(&bandLogE2[c*nbEBands+i-2])-offset);
Jean-Marc Valin05548fa2014-01-19 01:31:00 -05001022 tmp = median_of_3(&bandLogE2[c*nbEBands])-offset;
Jean-Marc Valin379af352014-01-19 01:42:04 -05001023 f[0] = MAX16(f[0], tmp);
1024 f[1] = MAX16(f[1], tmp);
Jean-Marc Valince1173c2014-01-20 18:45:57 -05001025 tmp = median_of_3(&bandLogE2[c*nbEBands+end-3])-offset;
1026 f[end-2] = MAX16(f[end-2], tmp);
1027 f[end-1] = MAX16(f[end-1], tmp);
Jean-Marc Valin05548fa2014-01-19 01:31:00 -05001028
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001029 for (i=0;i<end;i++)
Jean-Marc Valin379af352014-01-19 01:42:04 -05001030 f[i] = MAX16(f[i], noise_floor[i]);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001031 } while (++c<C);
1032 if (C==2)
1033 {
1034 for (i=start;i<end;i++)
1035 {
1036 /* Consider 24 dB "cross-talk" */
1037 follower[nbEBands+i] = MAX16(follower[nbEBands+i], follower[ i]-QCONST16(4.f,DB_SHIFT));
1038 follower[ i] = MAX16(follower[ i], follower[nbEBands+i]-QCONST16(4.f,DB_SHIFT));
1039 follower[i] = HALF16(MAX16(0, bandLogE[i]-follower[i]) + MAX16(0, bandLogE[nbEBands+i]-follower[nbEBands+i]));
1040 }
1041 } else {
1042 for (i=start;i<end;i++)
1043 {
1044 follower[i] = MAX16(0, bandLogE[i]-follower[i]);
1045 }
1046 }
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001047 for (i=start;i<end;i++)
1048 follower[i] = MAX16(follower[i], surround_dynalloc[i]);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001049 /* For non-transient CBR/CVBR frames, halve the dynalloc contribution */
1050 if ((!vbr || constrained_vbr)&&!isTransient)
1051 {
1052 for (i=start;i<end;i++)
1053 follower[i] = HALF16(follower[i]);
1054 }
1055 for (i=start;i<end;i++)
1056 {
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001057 if (i<8)
1058 follower[i] *= 2;
1059 if (i>=12)
1060 follower[i] = HALF16(follower[i]);
Jean-Marc Valin0cc4d962017-06-01 03:14:13 -04001061 }
1062#ifdef DISABLE_FLOAT_API
1063 (void)analysis;
1064#else
1065 if (analysis->valid)
1066 {
1067 for (i=start;i<IMIN(LEAK_BANDS, end);i++)
1068 follower[i] = follower[i] + QCONST16(1.f/64.f, DB_SHIFT)*analysis->leak_boost[i];
1069 }
1070#endif
1071 for (i=start;i<end;i++)
1072 {
1073 int width;
1074 int boost;
1075 int boost_bits;
1076
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001077 follower[i] = MIN16(follower[i], QCONST16(4, DB_SHIFT));
1078
1079 width = C*(eBands[i+1]-eBands[i])<<LM;
1080 if (width<6)
1081 {
Jean-Marc Valind683c762012-12-21 16:17:38 -05001082 boost = (int)SHR32(EXTEND32(follower[i]),DB_SHIFT);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001083 boost_bits = boost*width<<BITRES;
1084 } else if (width > 48) {
Jean-Marc Valind683c762012-12-21 16:17:38 -05001085 boost = (int)SHR32(EXTEND32(follower[i])*8,DB_SHIFT);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001086 boost_bits = (boost*width<<BITRES)/8;
1087 } else {
Jean-Marc Valind683c762012-12-21 16:17:38 -05001088 boost = (int)SHR32(EXTEND32(follower[i])*width/6,DB_SHIFT);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001089 boost_bits = boost*6<<BITRES;
1090 }
Jean-Marc Valin45433082017-06-01 03:08:58 -04001091 /* For CBR and non-transient CVBR frames, limit dynalloc to 2/3 of the bits */
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001092 if ((!vbr || (constrained_vbr&&!isTransient))
Jean-Marc Valin45433082017-06-01 03:08:58 -04001093 && (tot_boost+boost_bits)>>BITRES>>3 > 2*effectiveBytes/3)
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001094 {
Jean-Marc Valin45433082017-06-01 03:08:58 -04001095 opus_int32 cap = ((2*effectiveBytes/3)<<BITRES<<3);
Jean-Marc Valine4550922013-07-30 05:12:46 -04001096 offsets[i] = cap-tot_boost;
1097 tot_boost = cap;
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001098 break;
1099 } else {
1100 offsets[i] = boost;
1101 tot_boost += boost_bits;
1102 }
1103 }
1104 }
1105 *tot_boost_ = tot_boost;
1106 RESTORE_STACK;
1107 return maxDepth;
1108}
1109
1110
Jean-Marc Valin69062102012-11-08 09:42:27 -05001111static int run_prefilter(CELTEncoder *st, celt_sig *in, celt_sig *prefilter_mem, int CC, int N,
1112 int prefilter_tapset, int *pitch, opus_val16 *gain, int *qgain, int enabled, int nbAvailableBytes)
1113{
1114 int c;
1115 VARDECL(celt_sig, _pre);
1116 celt_sig *pre[2];
1117 const CELTMode *mode;
1118 int pitch_index;
1119 opus_val16 gain1;
1120 opus_val16 pf_threshold;
1121 int pf_on;
1122 int qg;
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001123 int overlap;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001124 SAVE_STACK;
1125
1126 mode = st->mode;
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001127 overlap = mode->overlap;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001128 ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
1129
1130 pre[0] = _pre;
1131 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
1132
1133
1134 c=0; do {
1135 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001136 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+overlap)+overlap, N);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001137 } while (++c<CC);
1138
1139 if (enabled)
1140 {
1141 VARDECL(opus_val16, pitch_buf);
1142 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
1143
Timothy B. Terriberry39386e02013-11-18 13:30:13 -05001144 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC, st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001145 /* Don't search for the fir last 1.5 octave of the range because
1146 there's too many false-positives due to short-term correlation */
1147 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
Timothy B. Terriberry39386e02013-11-18 13:30:13 -05001148 COMBFILTER_MAXPERIOD-3*COMBFILTER_MINPERIOD, &pitch_index,
1149 st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001150 pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
1151
1152 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
xiangmingzhuc95c9a02014-04-30 15:48:07 +08001153 N, &pitch_index, st->prefilter_period, st->prefilter_gain, st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001154 if (pitch_index > COMBFILTER_MAXPERIOD-2)
1155 pitch_index = COMBFILTER_MAXPERIOD-2;
1156 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1157 /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.tonality);*/
1158 if (st->loss_rate>2)
1159 gain1 = HALF32(gain1);
1160 if (st->loss_rate>4)
1161 gain1 = HALF32(gain1);
1162 if (st->loss_rate>8)
1163 gain1 = 0;
1164 } else {
1165 gain1 = 0;
1166 pitch_index = COMBFILTER_MINPERIOD;
1167 }
1168
1169 /* Gain threshold for enabling the prefilter/postfilter */
1170 pf_threshold = QCONST16(.2f,15);
1171
1172 /* Adjusting the threshold based on rate and continuity */
1173 if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1174 pf_threshold += QCONST16(.2f,15);
1175 if (nbAvailableBytes<25)
1176 pf_threshold += QCONST16(.1f,15);
1177 if (nbAvailableBytes<35)
1178 pf_threshold += QCONST16(.1f,15);
1179 if (st->prefilter_gain > QCONST16(.4f,15))
1180 pf_threshold -= QCONST16(.1f,15);
1181 if (st->prefilter_gain > QCONST16(.55f,15))
1182 pf_threshold -= QCONST16(.1f,15);
1183
1184 /* Hard threshold at 0.2 */
1185 pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1186 if (gain1<pf_threshold)
1187 {
1188 gain1 = 0;
1189 pf_on = 0;
1190 qg = 0;
1191 } else {
1192 /*This block is not gated by a total bits check only because
1193 of the nbAvailableBytes check above.*/
1194 if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1195 gain1=st->prefilter_gain;
1196
1197#ifdef FIXED_POINT
1198 qg = ((gain1+1536)>>10)/3-1;
1199#else
1200 qg = (int)floor(.5f+gain1*32/3)-1;
1201#endif
1202 qg = IMAX(0, IMIN(7, qg));
1203 gain1 = QCONST16(0.09375f,15)*(qg+1);
1204 pf_on = 1;
1205 }
1206 /*printf("%d %f\n", pitch_index, gain1);*/
1207
1208 c=0; do {
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001209 int offset = mode->shortMdctSize-overlap;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001210 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001211 OPUS_COPY(in+c*(N+overlap), st->in_mem+c*(overlap), overlap);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001212 if (offset)
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001213 comb_filter(in+c*(N+overlap)+overlap, pre[c]+COMBFILTER_MAXPERIOD,
Jean-Marc Valin69062102012-11-08 09:42:27 -05001214 st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
Jonathan Lennox43120f02015-08-03 17:04:27 -04001215 st->prefilter_tapset, st->prefilter_tapset, NULL, 0, st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001216
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001217 comb_filter(in+c*(N+overlap)+overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
Jean-Marc Valin69062102012-11-08 09:42:27 -05001218 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
Jonathan Lennox43120f02015-08-03 17:04:27 -04001219 st->prefilter_tapset, prefilter_tapset, mode->window, overlap, st->arch);
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001220 OPUS_COPY(st->in_mem+c*(overlap), in+c*(N+overlap)+N, overlap);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001221
1222 if (N>COMBFILTER_MAXPERIOD)
1223 {
Jean-Marc Valinb66080a2016-06-20 12:11:05 -04001224 OPUS_COPY(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001225 } else {
1226 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
Jean-Marc Valinb66080a2016-06-20 12:11:05 -04001227 OPUS_COPY(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001228 }
1229 } while (++c<CC);
1230
1231 RESTORE_STACK;
1232 *gain = gain1;
1233 *pitch = pitch_index;
1234 *qgain = qg;
1235 return pf_on;
1236}
1237
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001238static int compute_vbr(const CELTMode *mode, AnalysisInfo *analysis, opus_int32 base_target,
1239 int LM, opus_int32 bitrate, int lastCodedBands, int C, int intensity,
1240 int constrained_vbr, opus_val16 stereo_saving, int tot_boost,
1241 opus_val16 tf_estimate, int pitch_change, opus_val16 maxDepth,
Jean-Marc Valin132ed592016-11-04 11:41:13 -04001242 int lfe, int has_surround_mask, opus_val16 surround_masking,
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001243 opus_val16 temporal_vbr)
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001244{
1245 /* The target rate in 8th bits per frame */
1246 opus_int32 target;
1247 int coded_bins;
1248 int coded_bands;
Jean-Marc Valin39cbc452013-06-13 15:28:53 -04001249 opus_val16 tf_calibration;
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001250 int nbEBands;
1251 const opus_int16 *eBands;
1252
1253 nbEBands = mode->nbEBands;
1254 eBands = mode->eBands;
1255
1256 coded_bands = lastCodedBands ? lastCodedBands : nbEBands;
1257 coded_bins = eBands[coded_bands]<<LM;
1258 if (C==2)
1259 coded_bins += eBands[IMIN(intensity, coded_bands)]<<LM;
1260
1261 target = base_target;
1262
1263 /*printf("%f %f %f %f %d %d ", st->analysis.activity, st->analysis.tonality, tf_estimate, st->stereo_saving, tot_boost, coded_bands);*/
Jean-Marc Valin3ab03e02013-09-06 16:00:39 -04001264#ifndef DISABLE_FLOAT_API
Gregory Maxwell5280c712013-07-15 15:51:24 -07001265 if (analysis->valid && analysis->activity<.4)
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001266 target -= (opus_int32)((coded_bins<<BITRES)*(.4f-analysis->activity));
1267#endif
1268 /* Stereo savings */
1269 if (C==2)
1270 {
1271 int coded_stereo_bands;
1272 int coded_stereo_dof;
1273 opus_val16 max_frac;
1274 coded_stereo_bands = IMIN(intensity, coded_bands);
1275 coded_stereo_dof = (eBands[coded_stereo_bands]<<LM)-coded_stereo_bands;
1276 /* Maximum fraction of the bits we can save if the signal is mono. */
1277 max_frac = DIV32_16(MULT16_16(QCONST16(0.8f, 15), coded_stereo_dof), coded_bins);
Jean-Marc Valin9c23f5c2013-10-28 14:15:18 -04001278 stereo_saving = MIN16(stereo_saving, QCONST16(1.f, 8));
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001279 /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/
1280 target -= (opus_int32)MIN32(MULT16_32_Q15(max_frac,target),
1281 SHR32(MULT16_16(stereo_saving-QCONST16(0.1f,8),(coded_stereo_dof<<BITRES)),8));
1282 }
1283 /* Boost the rate according to dynalloc (minus the dynalloc average for calibration). */
Jean-Marc Valinc2d32332017-06-02 14:01:36 -04001284 target += tot_boost-(19<<LM);
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001285 /* Apply transient boost, compensating for average boost. */
Jean-Marc Valinf26afaa2017-06-02 15:16:24 -04001286 tf_calibration = QCONST16(0.044f,14);
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001287 target += (opus_int32)SHL32(MULT16_32_Q15(tf_estimate-tf_calibration, target),1);
1288
Jean-Marc Valin3ab03e02013-09-06 16:00:39 -04001289#ifndef DISABLE_FLOAT_API
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001290 /* Apply tonality boost */
Jean-Marc Valincd373b52013-07-07 02:51:07 -04001291 if (analysis->valid && !lfe)
1292 {
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001293 opus_int32 tonal_target;
1294 float tonal;
1295
1296 /* Tonality boost (compensating for the average). */
Jean-Marc Valinf26afaa2017-06-02 15:16:24 -04001297 tonal = MAX16(0.f,analysis->tonality-.15f)-0.12f;
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001298 tonal_target = target + (opus_int32)((coded_bins<<BITRES)*1.2f*tonal);
1299 if (pitch_change)
1300 tonal_target += (opus_int32)((coded_bins<<BITRES)*.8f);
Jean-Marc Valin942fc812013-10-01 19:27:30 -04001301 /*printf("%f %f ", analysis->tonality, tonal);*/
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001302 target = tonal_target;
1303 }
Timothy B. Terriberry23f503a2014-12-26 08:31:39 -08001304#else
1305 (void)analysis;
1306 (void)pitch_change;
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001307#endif
1308
1309 if (has_surround_mask&&!lfe)
1310 {
Jean-Marc Valin16ba19a2013-06-27 03:40:44 -04001311 opus_int32 surround_target = target + (opus_int32)SHR32(MULT16_16(surround_masking,coded_bins<<BITRES), DB_SHIFT);
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001312 /*printf("%f %d %d %d %d %d %d ", surround_masking, coded_bins, st->end, st->intensity, surround_target, target, st->bitrate);*/
1313 target = IMAX(target/4, surround_target);
1314 }
1315
1316 {
1317 opus_int32 floor_depth;
1318 int bins;
1319 bins = eBands[nbEBands-2]<<LM;
1320 /*floor_depth = SHR32(MULT16_16((C*bins<<BITRES),celt_log2(SHL32(MAX16(1,sample_max),13))), DB_SHIFT);*/
1321 floor_depth = (opus_int32)SHR32(MULT16_16((C*bins<<BITRES),maxDepth), DB_SHIFT);
1322 floor_depth = IMAX(floor_depth, target>>2);
1323 target = IMIN(target, floor_depth);
1324 /*printf("%f %d\n", maxDepth, floor_depth);*/
1325 }
1326
Jean-Marc Valin59618a52015-11-16 23:03:17 -05001327 /* Make VBR less aggressive for constrained VBR because we can't keep a higher bitrate
1328 for long. Needs tuning. */
1329 if ((!has_surround_mask||lfe) && constrained_vbr)
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001330 {
Jean-Marc Valin59618a52015-11-16 23:03:17 -05001331 target = base_target + (opus_int32)MULT16_32_Q15(QCONST16(0.67f, 15), target-base_target);
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001332 }
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001333
Jean-Marc Valincd373b52013-07-07 02:51:07 -04001334 if (!has_surround_mask && tf_estimate < QCONST16(.2f, 14))
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001335 {
1336 opus_val16 amount;
1337 opus_val16 tvbr_factor;
Jean-Marc Valin8e3a1cb2013-07-01 16:12:27 -04001338 amount = MULT16_16_Q15(QCONST16(.0000031f, 30), IMAX(0, IMIN(32000, 96000-bitrate)));
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001339 tvbr_factor = SHR32(MULT16_16(temporal_vbr, amount), DB_SHIFT);
1340 target += (opus_int32)MULT16_32_Q15(tvbr_factor, target);
1341 }
1342
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001343 /* Don't allow more than doubling the rate */
1344 target = IMIN(2*base_target, target);
1345
1346 return target;
1347}
1348
Jean-Marc Valin69062102012-11-08 09:42:27 -05001349int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
1350{
1351 int i, c, N;
1352 opus_int32 bits;
1353 ec_enc _enc;
1354 VARDECL(celt_sig, in);
1355 VARDECL(celt_sig, freq);
1356 VARDECL(celt_norm, X);
1357 VARDECL(celt_ener, bandE);
1358 VARDECL(opus_val16, bandLogE);
1359 VARDECL(opus_val16, bandLogE2);
1360 VARDECL(int, fine_quant);
1361 VARDECL(opus_val16, error);
1362 VARDECL(int, pulses);
1363 VARDECL(int, cap);
1364 VARDECL(int, offsets);
1365 VARDECL(int, fine_priority);
1366 VARDECL(int, tf_res);
1367 VARDECL(unsigned char, collapse_masks);
1368 celt_sig *prefilter_mem;
Jean-Marc Valin6fccb4b2016-07-04 01:06:11 -04001369 opus_val16 *oldBandE, *oldLogE, *oldLogE2, *energyError;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001370 int shortBlocks=0;
1371 int isTransient=0;
1372 const int CC = st->channels;
1373 const int C = st->stream_channels;
1374 int LM, M;
1375 int tf_select;
1376 int nbFilledBytes, nbAvailableBytes;
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001377 int start;
1378 int end;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001379 int effEnd;
1380 int codedBands;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001381 int alloc_trim;
1382 int pitch_index=COMBFILTER_MINPERIOD;
1383 opus_val16 gain1 = 0;
1384 int dual_stereo=0;
1385 int effectiveBytes;
1386 int dynalloc_logp;
1387 opus_int32 vbr_rate;
1388 opus_int32 total_bits;
1389 opus_int32 total_boost;
1390 opus_int32 balance;
1391 opus_int32 tell;
Jean-Marc Valinbcd6aba2015-12-06 10:50:15 -05001392 opus_int32 tell0_frac;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001393 int prefilter_tapset=0;
1394 int pf_on;
1395 int anti_collapse_rsv;
1396 int anti_collapse_on=0;
1397 int silence=0;
1398 int tf_chan = 0;
1399 opus_val16 tf_estimate;
1400 int pitch_change=0;
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001401 opus_int32 tot_boost;
Jean-Marc Valinb7bd4c22013-05-18 23:33:48 -04001402 opus_val32 sample_max;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001403 opus_val16 maxDepth;
1404 const OpusCustomMode *mode;
1405 int nbEBands;
1406 int overlap;
1407 const opus_int16 *eBands;
1408 int secondMdct;
Jean-Marc Valin5fb50ad2012-12-21 01:23:26 -05001409 int signalBandwidth;
Jean-Marc Valinf77410d2013-04-19 22:44:03 -04001410 int transient_got_disabled=0;
Jean-Marc Valina4dccd32013-05-04 23:54:20 -04001411 opus_val16 surround_masking=0;
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001412 opus_val16 temporal_vbr=0;
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001413 opus_val16 surround_trim = 0;
Jean-Marc Valin14845912016-06-08 02:33:56 -04001414 opus_int32 equiv_rate;
Jean-Marc Valinbcd6aba2015-12-06 10:50:15 -05001415 int hybrid;
Jean-Marc Valin90f20c62016-10-28 16:44:38 -04001416 int weak_transient = 0;
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001417 VARDECL(opus_val16, surround_dynalloc);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001418 ALLOC_STACK;
1419
1420 mode = st->mode;
1421 nbEBands = mode->nbEBands;
1422 overlap = mode->overlap;
1423 eBands = mode->eBands;
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001424 start = st->start;
1425 end = st->end;
Jean-Marc Valinbcd6aba2015-12-06 10:50:15 -05001426 hybrid = start != 0;
Jean-Marc Valindae16fb2012-12-13 21:40:58 -05001427 tf_estimate = 0;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001428 if (nbCompressedBytes<2 || pcm==NULL)
Jean-Marc Valine83d2aa2013-11-15 01:52:28 -05001429 {
Jean-Marc Valin1b28e0c2013-11-15 01:57:25 -05001430 RESTORE_STACK;
Jean-Marc Valine83d2aa2013-11-15 01:52:28 -05001431 return OPUS_BAD_ARG;
1432 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05001433
1434 frame_size *= st->upsample;
1435 for (LM=0;LM<=mode->maxLM;LM++)
1436 if (mode->shortMdctSize<<LM==frame_size)
1437 break;
1438 if (LM>mode->maxLM)
Jean-Marc Valine83d2aa2013-11-15 01:52:28 -05001439 {
Jean-Marc Valin1b28e0c2013-11-15 01:57:25 -05001440 RESTORE_STACK;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001441 return OPUS_BAD_ARG;
Jean-Marc Valine83d2aa2013-11-15 01:52:28 -05001442 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05001443 M=1<<LM;
1444 N = M*mode->shortMdctSize;
1445
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001446 prefilter_mem = st->in_mem+CC*(overlap);
1447 oldBandE = (opus_val16*)(st->in_mem+CC*(overlap+COMBFILTER_MAXPERIOD));
Jean-Marc Valin69062102012-11-08 09:42:27 -05001448 oldLogE = oldBandE + CC*nbEBands;
1449 oldLogE2 = oldLogE + CC*nbEBands;
Jean-Marc Valin6fccb4b2016-07-04 01:06:11 -04001450 energyError = oldLogE2 + CC*nbEBands;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001451
1452 if (enc==NULL)
1453 {
Jean-Marc Valinbcd6aba2015-12-06 10:50:15 -05001454 tell0_frac=tell=1;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001455 nbFilledBytes=0;
1456 } else {
Jean-Marc Valinbcd6aba2015-12-06 10:50:15 -05001457 tell0_frac=tell=ec_tell_frac(enc);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001458 tell=ec_tell(enc);
1459 nbFilledBytes=(tell+4)>>3;
1460 }
1461
1462#ifdef CUSTOM_MODES
1463 if (st->signalling && enc==NULL)
1464 {
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001465 int tmp = (mode->effEBands-end)>>1;
1466 end = st->end = IMAX(1, mode->effEBands-tmp);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001467 compressed[0] = tmp<<5;
1468 compressed[0] |= LM<<3;
1469 compressed[0] |= (C==2)<<2;
1470 /* Convert "standard mode" to Opus header */
1471 if (mode->Fs==48000 && mode->shortMdctSize==120)
1472 {
1473 int c0 = toOpus(compressed[0]);
1474 if (c0<0)
Jean-Marc Valine83d2aa2013-11-15 01:52:28 -05001475 {
Jean-Marc Valin1b28e0c2013-11-15 01:57:25 -05001476 RESTORE_STACK;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001477 return OPUS_BAD_ARG;
Jean-Marc Valine83d2aa2013-11-15 01:52:28 -05001478 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05001479 compressed[0] = c0;
1480 }
1481 compressed++;
1482 nbCompressedBytes--;
1483 }
1484#else
1485 celt_assert(st->signalling==0);
1486#endif
1487
1488 /* Can't produce more than 1275 output bytes */
1489 nbCompressedBytes = IMIN(nbCompressedBytes,1275);
1490 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1491
1492 if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1493 {
1494 opus_int32 den=mode->Fs>>BITRES;
1495 vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1496#ifdef CUSTOM_MODES
1497 if (st->signalling)
1498 vbr_rate -= 8<<BITRES;
1499#endif
1500 effectiveBytes = vbr_rate>>(3+BITRES);
1501 } else {
1502 opus_int32 tmp;
1503 vbr_rate = 0;
1504 tmp = st->bitrate*frame_size;
1505 if (tell>1)
1506 tmp += tell;
1507 if (st->bitrate!=OPUS_BITRATE_MAX)
1508 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1509 (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling));
Jean-Marc Valinbcd6aba2015-12-06 10:50:15 -05001510 effectiveBytes = nbCompressedBytes - nbFilledBytes;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001511 }
Jean-Marc Valin14845912016-06-08 02:33:56 -04001512 equiv_rate = ((opus_int32)nbCompressedBytes*8*50 >> (3-LM)) - (40*C+20)*((400>>LM) - 50);
Jean-Marc Valinc1959e72013-11-08 19:24:10 -05001513 if (st->bitrate != OPUS_BITRATE_MAX)
Jean-Marc Valin14845912016-06-08 02:33:56 -04001514 equiv_rate = IMIN(equiv_rate, st->bitrate - (40*C+20)*((400>>LM) - 50));
Jean-Marc Valin69062102012-11-08 09:42:27 -05001515
1516 if (enc==NULL)
1517 {
1518 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1519 enc = &_enc;
1520 }
1521
1522 if (vbr_rate>0)
1523 {
1524 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1525 target rate and buffering.
1526 We must do this up front so that bust-prevention logic triggers
1527 correctly if we don't have enough bits. */
1528 if (st->constrained_vbr)
1529 {
1530 opus_int32 vbr_bound;
1531 opus_int32 max_allowed;
1532 /* We could use any multiple of vbr_rate as bound (depending on the
1533 delay).
1534 This is clamped to ensure we use at least two bytes if the encoder
1535 was entirely empty, but to allow 0 in hybrid mode. */
1536 vbr_bound = vbr_rate;
1537 max_allowed = IMIN(IMAX(tell==1?2:0,
1538 (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1539 nbAvailableBytes);
1540 if(max_allowed < nbAvailableBytes)
1541 {
1542 nbCompressedBytes = nbFilledBytes+max_allowed;
1543 nbAvailableBytes = max_allowed;
1544 ec_enc_shrink(enc, nbCompressedBytes);
1545 }
1546 }
1547 }
1548 total_bits = nbCompressedBytes*8;
1549
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001550 effEnd = end;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001551 if (effEnd > mode->effEBands)
1552 effEnd = mode->effEBands;
1553
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001554 ALLOC(in, CC*(N+overlap), celt_sig);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001555
Jean-Marc Valinb7bd4c22013-05-18 23:33:48 -04001556 sample_max=MAX32(st->overlap_max, celt_maxabs16(pcm, C*(N-overlap)/st->upsample));
Jean-Marc Valin69062102012-11-08 09:42:27 -05001557 st->overlap_max=celt_maxabs16(pcm+C*(N-overlap)/st->upsample, C*overlap/st->upsample);
Jean-Marc Valinb7bd4c22013-05-18 23:33:48 -04001558 sample_max=MAX32(sample_max, st->overlap_max);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001559#ifdef FIXED_POINT
1560 silence = (sample_max==0);
1561#else
1562 silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth));
1563#endif
1564#ifdef FUZZING
1565 if ((rand()&0x3F)==0)
1566 silence = 1;
1567#endif
1568 if (tell==1)
1569 ec_enc_bit_logp(enc, silence, 15);
1570 else
1571 silence=0;
1572 if (silence)
1573 {
1574 /*In VBR mode there is no need to send more than the minimum. */
1575 if (vbr_rate>0)
1576 {
1577 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1578 total_bits=nbCompressedBytes*8;
1579 nbAvailableBytes=2;
1580 ec_enc_shrink(enc, nbCompressedBytes);
1581 }
1582 /* Pretend we've filled all the remaining bits with zeros
1583 (that's what the initialiser did anyway) */
1584 tell = nbCompressedBytes*8;
1585 enc->nbits_total+=tell-ec_tell(enc);
1586 }
1587 c=0; do {
Jean-Marc Valinc94e4bb2013-12-08 03:31:50 -05001588 int need_clip=0;
1589#ifndef FIXED_POINT
1590 need_clip = st->clip && sample_max>65536.f;
1591#endif
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001592 celt_preemphasis(pcm+c, in+c*(N+overlap)+overlap, N, CC, st->upsample,
Jean-Marc Valinc94e4bb2013-12-08 03:31:50 -05001593 mode->preemph, st->preemph_memE+c, need_clip);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001594 } while (++c<CC);
1595
1596
1597
1598 /* Find pitch period and gain */
1599 {
1600 int enabled;
1601 int qg;
Jean-Marc Valin7e0ca432015-12-25 13:12:58 -05001602 enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && !hybrid && !silence && !st->disable_pf
Jean-Marc Valin132ed592016-11-04 11:41:13 -04001603 && st->complexity >= 5;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001604
1605 prefilter_tapset = st->tapset_decision;
1606 pf_on = run_prefilter(st, in, prefilter_mem, CC, N, prefilter_tapset, &pitch_index, &gain1, &qg, enabled, nbAvailableBytes);
Gregory Maxwell5280c712013-07-15 15:51:24 -07001607 if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && (!st->analysis.valid || st->analysis.tonality > .3)
1608 && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st->prefilter_period))
Jean-Marc Valin69062102012-11-08 09:42:27 -05001609 pitch_change = 1;
1610 if (pf_on==0)
1611 {
Jean-Marc Valin7e0ca432015-12-25 13:12:58 -05001612 if(!hybrid && tell+16<=total_bits)
Jean-Marc Valin69062102012-11-08 09:42:27 -05001613 ec_enc_bit_logp(enc, 0, 1);
1614 } else {
1615 /*This block is not gated by a total bits check only because
1616 of the nbAvailableBytes check above.*/
1617 int octave;
1618 ec_enc_bit_logp(enc, 1, 1);
1619 pitch_index += 1;
1620 octave = EC_ILOG(pitch_index)-5;
1621 ec_enc_uint(enc, octave, 6);
1622 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1623 pitch_index -= 1;
1624 ec_enc_bits(enc, qg, 3);
1625 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1626 }
1627 }
1628
1629 isTransient = 0;
1630 shortBlocks = 0;
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -04001631 if (st->complexity >= 1 && !st->lfe)
Jean-Marc Valin2a5f0562012-11-19 23:17:06 -05001632 {
Jean-Marc Valin7e122392016-10-29 17:02:36 -04001633 /* Reduces the likelihood of energy instability on fricatives at low bitrate
1634 in hybrid mode. It seems like we still want to have real transients on vowels
1635 though (small SILK quantization offset value). */
1636 int allow_weak_transients = hybrid && effectiveBytes<15 && st->silk_info.offset >= 100;
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001637 isTransient = transient_analysis(in, N+overlap, CC,
Jean-Marc Valin7e122392016-10-29 17:02:36 -04001638 &tf_estimate, &tf_chan, allow_weak_transients, &weak_transient);
Jean-Marc Valin2a5f0562012-11-19 23:17:06 -05001639 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05001640 if (LM>0 && ec_tell(enc)+3<=total_bits)
1641 {
Jean-Marc Valin2a5f0562012-11-19 23:17:06 -05001642 if (isTransient)
1643 shortBlocks = M;
Jean-Marc Valin2a5f0562012-11-19 23:17:06 -05001644 } else {
1645 isTransient = 0;
Jean-Marc Valin3252bf22013-04-19 03:14:28 -04001646 transient_got_disabled=1;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001647 }
1648
1649 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
1650 ALLOC(bandE,nbEBands*CC, celt_ener);
1651 ALLOC(bandLogE,nbEBands*CC, opus_val16);
1652
1653 secondMdct = shortBlocks && st->complexity>=8;
1654 ALLOC(bandLogE2, C*nbEBands, opus_val16);
1655 if (secondMdct)
1656 {
Viswanath Puttaguntaf48abe82015-05-15 12:42:19 -05001657 compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample, st->arch);
Linfeng Zhanga1ae8212016-09-07 15:29:03 -07001658 compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001659 amp2Log2(mode, effEnd, end, bandE, bandLogE2, C);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001660 for (i=0;i<C*nbEBands;i++)
1661 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT));
1662 }
1663
Viswanath Puttaguntaf48abe82015-05-15 12:42:19 -05001664 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
Jean-Marc Valin69c3dcd2013-02-19 03:42:18 -05001665 if (CC==2&&C==1)
1666 tf_chan = 0;
Linfeng Zhanga1ae8212016-09-07 15:29:03 -07001667 compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001668
Jean-Marc Valin172f66a2013-04-27 02:29:52 -04001669 if (st->lfe)
1670 {
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001671 for (i=2;i<end;i++)
Jean-Marc Valin43654982013-07-07 00:39:35 -04001672 {
Jean-Marc Valin172f66a2013-04-27 02:29:52 -04001673 bandE[i] = IMIN(bandE[i], MULT16_32_Q15(QCONST16(1e-4f,15),bandE[0]));
Jean-Marc Valin43654982013-07-07 00:39:35 -04001674 bandE[i] = MAX32(bandE[i], EPSILON);
1675 }
Jean-Marc Valin172f66a2013-04-27 02:29:52 -04001676 }
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001677 amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001678
1679 ALLOC(surround_dynalloc, C*nbEBands, opus_val16);
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001680 OPUS_CLEAR(surround_dynalloc, end);
Jean-Marc Valind2c484b2013-05-06 16:02:31 -04001681 /* This computes how much masking takes place between surround channels */
Jean-Marc Valin7e0ca432015-12-25 13:12:58 -05001682 if (!hybrid&&st->energy_mask&&!st->lfe)
Jean-Marc Valina4dccd32013-05-04 23:54:20 -04001683 {
Jean-Marc Valin039e9ab2013-09-12 03:05:43 -04001684 int mask_end;
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001685 int midband;
1686 int count_dynalloc;
Jean-Marc Valina4dccd32013-05-04 23:54:20 -04001687 opus_val32 mask_avg=0;
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001688 opus_val32 diff=0;
Jean-Marc Valin039e9ab2013-09-12 03:05:43 -04001689 int count=0;
Jean-Marc Valin942fc812013-10-01 19:27:30 -04001690 mask_end = IMAX(2,st->lastCodedBands);
Jean-Marc Valina4dccd32013-05-04 23:54:20 -04001691 for (c=0;c<C;c++)
Jean-Marc Valind2c484b2013-05-06 16:02:31 -04001692 {
Jean-Marc Valin039e9ab2013-09-12 03:05:43 -04001693 for(i=0;i<mask_end;i++)
Jean-Marc Valind2c484b2013-05-06 16:02:31 -04001694 {
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001695 opus_val16 mask;
1696 mask = MAX16(MIN16(st->energy_mask[nbEBands*c+i],
1697 QCONST16(.25f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT));
1698 if (mask > 0)
1699 mask = HALF16(mask);
1700 mask_avg += MULT16_16(mask, eBands[i+1]-eBands[i]);
1701 count += eBands[i+1]-eBands[i];
1702 diff += MULT16_16(mask, 1+2*i-mask_end);
Jean-Marc Valind2c484b2013-05-06 16:02:31 -04001703 }
1704 }
Gregory Maxwell6d2d5c42014-04-16 18:29:26 -07001705 celt_assert(count>0);
Jean-Marc Valin039e9ab2013-09-12 03:05:43 -04001706 mask_avg = DIV32_16(mask_avg,count);
Jean-Marc Valin36a21ed2013-09-14 15:46:09 -04001707 mask_avg += QCONST16(.2f, DB_SHIFT);
Jean-Marc Valin039e9ab2013-09-12 03:05:43 -04001708 diff = diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end);
1709 /* Again, being conservative */
1710 diff = HALF32(diff);
1711 diff = MAX32(MIN32(diff, QCONST32(.031f, DB_SHIFT)), -QCONST32(.031f, DB_SHIFT));
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001712 /* Find the band that's in the middle of the coded spectrum */
1713 for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++);
1714 count_dynalloc=0;
Jean-Marc Valin039e9ab2013-09-12 03:05:43 -04001715 for(i=0;i<mask_end;i++)
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001716 {
1717 opus_val32 lin;
1718 opus_val16 unmask;
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001719 lin = mask_avg + diff*(i-midband);
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001720 if (C==2)
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001721 unmask = MAX16(st->energy_mask[i], st->energy_mask[nbEBands+i]);
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001722 else
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001723 unmask = st->energy_mask[i];
1724 unmask = MIN16(unmask, QCONST16(.0f, DB_SHIFT));
1725 unmask -= lin;
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001726 if (unmask > QCONST16(.25f, DB_SHIFT))
1727 {
1728 surround_dynalloc[i] = unmask - QCONST16(.25f, DB_SHIFT);
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001729 count_dynalloc++;
1730 }
1731 }
1732 if (count_dynalloc>=3)
1733 {
1734 /* If we need dynalloc in many bands, it's probably because our
1735 initial masking rate was too low. */
1736 mask_avg += QCONST16(.25f, DB_SHIFT);
1737 if (mask_avg>0)
1738 {
1739 /* Something went really wrong in the original calculations,
1740 disabling masking. */
1741 mask_avg = 0;
1742 diff = 0;
Jean-Marc Valinff072002013-12-08 23:31:30 -05001743 OPUS_CLEAR(surround_dynalloc, mask_end);
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001744 } else {
1745 for(i=0;i<mask_end;i++)
1746 surround_dynalloc[i] = MAX16(0, surround_dynalloc[i]-QCONST16(.25f, DB_SHIFT));
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001747 }
1748 }
Jean-Marc Valin36a21ed2013-09-14 15:46:09 -04001749 mask_avg += QCONST16(.2f, DB_SHIFT);
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001750 /* Convert to 1/64th units used for the trim */
1751 surround_trim = 64*diff;
1752 /*printf("%d %d ", mask_avg, surround_trim);*/
1753 surround_masking = mask_avg;
Jean-Marc Valina4dccd32013-05-04 23:54:20 -04001754 }
Jean-Marc Valin92a06f52013-07-01 16:27:07 -04001755 /* Temporal VBR (but not for LFE) */
1756 if (!st->lfe)
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001757 {
1758 opus_val16 follow=-QCONST16(10.0f,DB_SHIFT);
Jean-Marc Valin8f466272013-10-28 21:50:10 -04001759 opus_val32 frame_avg=0;
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001760 opus_val16 offset = shortBlocks?HALF16(SHL16(LM, DB_SHIFT)):0;
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001761 for(i=start;i<end;i++)
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001762 {
1763 follow = MAX16(follow-QCONST16(1.f, DB_SHIFT), bandLogE[i]-offset);
1764 if (C==2)
1765 follow = MAX16(follow, bandLogE[i+nbEBands]-offset);
1766 frame_avg += follow;
1767 }
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001768 frame_avg /= (end-start);
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001769 temporal_vbr = SUB16(frame_avg,st->spec_avg);
1770 temporal_vbr = MIN16(QCONST16(3.f, DB_SHIFT), MAX16(-QCONST16(1.5f, DB_SHIFT), temporal_vbr));
1771 st->spec_avg += MULT16_16_Q15(QCONST16(.02f, 15), temporal_vbr);
1772 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05001773 /*for (i=0;i<21;i++)
1774 printf("%f ", bandLogE[i]);
1775 printf("\n");*/
1776
1777 if (!secondMdct)
1778 {
Jean-Marc Valinff072002013-12-08 23:31:30 -05001779 OPUS_COPY(bandLogE2, bandLogE, C*nbEBands);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001780 }
1781
Jean-Marc Valin541a4722013-02-17 21:21:30 -05001782 /* Last chance to catch any transient we might have missed in the
1783 time-domain analysis */
Jean-Marc Valin92d24922016-06-06 16:52:50 -04001784 if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5 && !st->lfe && !hybrid)
Jean-Marc Valin541a4722013-02-17 21:21:30 -05001785 {
Jean-Marc Valin99618092015-12-04 16:42:19 -05001786 if (patch_transient_decision(bandLogE, oldBandE, nbEBands, start, end, C))
Jean-Marc Valin541a4722013-02-17 21:21:30 -05001787 {
1788 isTransient = 1;
1789 shortBlocks = M;
Viswanath Puttaguntaf48abe82015-05-15 12:42:19 -05001790 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
Linfeng Zhanga1ae8212016-09-07 15:29:03 -07001791 compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001792 amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
Jean-Marc Valin541a4722013-02-17 21:21:30 -05001793 /* Compensate for the scaling of short vs long mdcts */
1794 for (i=0;i<C*nbEBands;i++)
1795 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT));
Jean-Marc Valin16ba19a2013-06-27 03:40:44 -04001796 tf_estimate = QCONST16(.2f,14);
Jean-Marc Valin541a4722013-02-17 21:21:30 -05001797 }
1798 }
1799
1800 if (LM>0 && ec_tell(enc)+3<=total_bits)
1801 ec_enc_bit_logp(enc, isTransient, 3);
1802
Jean-Marc Valin69062102012-11-08 09:42:27 -05001803 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1804
1805 /* Band normalisation */
1806 normalise_bands(mode, freq, X, bandE, effEnd, C, M);
1807
1808 ALLOC(tf_res, nbEBands, int);
Jean-Marc Valina6d663c2012-11-08 13:26:49 -05001809 /* Disable variable tf resolution for hybrid and at very low bitrate */
Jean-Marc Valin7e0ca432015-12-25 13:12:58 -05001810 if (effectiveBytes>=15*C && !hybrid && st->complexity>=2 && !st->lfe)
Jean-Marc Valina6d663c2012-11-08 13:26:49 -05001811 {
1812 int lambda;
Jean-Marc Valin7780d4a2016-06-08 15:19:03 -04001813 lambda = IMAX(5, 1280/effectiveBytes + 2);
Jean-Marc Valin2ca6df02016-06-08 02:15:21 -04001814 tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, LM, tf_estimate, tf_chan);
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001815 for (i=effEnd;i<end;i++)
Jean-Marc Valina6d663c2012-11-08 13:26:49 -05001816 tf_res[i] = tf_res[effEnd-1];
Jean-Marc Valin90f20c62016-10-28 16:44:38 -04001817 } else if (hybrid && weak_transient)
1818 {
1819 /* For weak transients, we rely on the fact that improving time resolution using
1820 TF on a long window is imperfect and will not result in an energy collapse at
1821 low bitrate. */
1822 for (i=0;i<end;i++)
1823 tf_res[i] = 1;
1824 tf_select=0;
Jean-Marc Valin8229f072016-06-06 16:54:29 -04001825 } else if (hybrid && effectiveBytes<15)
1826 {
1827 /* For low bitrate hybrid, we force temporal resolution to 5 ms rather than 2.5 ms. */
Jean-Marc Valin8229f072016-06-06 16:54:29 -04001828 for (i=0;i<end;i++)
1829 tf_res[i] = 0;
1830 tf_select=isTransient;
Jean-Marc Valina6d663c2012-11-08 13:26:49 -05001831 } else {
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001832 for (i=0;i<end;i++)
Jean-Marc Valina6d663c2012-11-08 13:26:49 -05001833 tf_res[i] = isTransient;
1834 tf_select=0;
1835 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05001836
1837 ALLOC(error, C*nbEBands, opus_val16);
Jean-Marc Valin6fccb4b2016-07-04 01:06:11 -04001838 c=0;
1839 do {
1840 for (i=start;i<end;i++)
1841 {
1842 /* When the energy is stable, slightly bias energy quantization towards
1843 the previous error to make the gain more stable (a constant offset is
1844 better than fluctuations). */
1845 if (ABS32(SUB32(bandLogE[i+c*nbEBands], oldBandE[i+c*nbEBands])) < QCONST16(2.f, DB_SHIFT))
1846 {
1847 bandLogE[i+c*nbEBands] -= MULT16_16_Q15(energyError[i+c*nbEBands], QCONST16(0.25f, 15));
1848 }
1849 }
1850 } while (++c < C);
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001851 quant_coarse_energy(mode, start, end, effEnd, bandLogE,
Jean-Marc Valin69062102012-11-08 09:42:27 -05001852 oldBandE, total_bits, error, enc,
1853 C, LM, nbAvailableBytes, st->force_intra,
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -04001854 &st->delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001855
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001856 tf_encode(start, end, isTransient, tf_res, LM, tf_select, enc);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001857
1858 if (ec_tell(enc)+4<=total_bits)
1859 {
Jean-Marc Valin172f66a2013-04-27 02:29:52 -04001860 if (st->lfe)
1861 {
1862 st->tapset_decision = 0;
1863 st->spread_decision = SPREAD_NORMAL;
Jean-Marc Valinf705e9b2016-06-06 13:43:13 -04001864 } else if (hybrid)
1865 {
1866 if (st->complexity == 0)
1867 st->spread_decision = SPREAD_NONE;
1868 else if (isTransient)
1869 st->spread_decision = SPREAD_NORMAL;
1870 else
1871 st->spread_decision = SPREAD_AGGRESSIVE;
1872 } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
Jean-Marc Valin69062102012-11-08 09:42:27 -05001873 {
1874 if (st->complexity == 0)
1875 st->spread_decision = SPREAD_NONE;
Jean-Marc Valin1fd1d7d2012-11-08 17:22:07 -05001876 else
1877 st->spread_decision = SPREAD_NORMAL;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001878 } else {
Jean-Marc Valin41fd7a12012-12-12 14:41:29 -05001879 /* Disable new spreading+tapset estimator until we can show it works
1880 better than the old one. So far it seems like spreading_decision()
1881 works best. */
Jean-Marc Valinab86a9c2013-11-13 23:06:25 -05001882#if 0
1883 if (st->analysis.valid)
Jean-Marc Valin69062102012-11-08 09:42:27 -05001884 {
1885 static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
1886 static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
1887 static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), QCONST16(.15f, 15)};
1888 static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), QCONST16(.05f, 15)};
1889 st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision);
1890 st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
Jean-Marc Valinab86a9c2013-11-13 23:06:25 -05001891 } else
1892#endif
1893 {
Jean-Marc Valin69062102012-11-08 09:42:27 -05001894 st->spread_decision = spreading_decision(mode, X,
1895 &st->tonal_average, st->spread_decision, &st->hf_average,
1896 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1897 }
1898 /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
1899 /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
1900 }
1901 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1902 }
1903
Jean-Marc Valin69062102012-11-08 09:42:27 -05001904 ALLOC(offsets, nbEBands, int);
1905
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001906 maxDepth = dynalloc_analysis(bandLogE, bandLogE2, nbEBands, start, end, C, offsets,
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001907 st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr,
Jean-Marc Valin0cc4d962017-06-01 03:14:13 -04001908 eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc, &st->analysis);
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -04001909 /* For LFE, everything interesting is in the first band */
1910 if (st->lfe)
1911 offsets[0] = IMIN(8, effectiveBytes/3);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001912 ALLOC(cap, nbEBands, int);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001913 init_caps(mode,cap,LM,C);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001914
Jean-Marc Valin69062102012-11-08 09:42:27 -05001915 dynalloc_logp = 6;
1916 total_bits<<=BITRES;
1917 total_boost = 0;
1918 tell = ec_tell_frac(enc);
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001919 for (i=start;i<end;i++)
Jean-Marc Valin69062102012-11-08 09:42:27 -05001920 {
1921 int width, quanta;
1922 int dynalloc_loop_logp;
1923 int boost;
1924 int j;
1925 width = C*(eBands[i+1]-eBands[i])<<LM;
1926 /* quanta is 6 bits, but no more than 1 bit/sample
1927 and no less than 1/8 bit/sample */
1928 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1929 dynalloc_loop_logp = dynalloc_logp;
1930 boost = 0;
1931 for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
1932 && boost < cap[i]; j++)
1933 {
1934 int flag;
1935 flag = j<offsets[i];
1936 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
1937 tell = ec_tell_frac(enc);
1938 if (!flag)
1939 break;
1940 boost += quanta;
1941 total_boost += quanta;
1942 dynalloc_loop_logp = 1;
1943 }
1944 /* Making dynalloc more likely */
1945 if (j)
1946 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1947 offsets[i] = boost;
1948 }
1949
1950 if (C==2)
1951 {
Jean-Marc Valin2924af42013-11-25 21:24:30 -05001952 static const opus_val16 intensity_thresholds[21]=
Jean-Marc Valin69062102012-11-08 09:42:27 -05001953 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 off*/
Jean-Marc Valin2924af42013-11-25 21:24:30 -05001954 { 1, 2, 3, 4, 5, 6, 7, 8,16,24,36,44,50,56,62,67,72,79,88,106,134};
Jean-Marc Valin69062102012-11-08 09:42:27 -05001955 static const opus_val16 intensity_histeresis[21]=
Jean-Marc Valina47d6f32013-11-24 23:59:09 -05001956 { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6, 8, 8};
Jean-Marc Valin69062102012-11-08 09:42:27 -05001957
1958 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1959 if (LM!=0)
1960 dual_stereo = stereo_analysis(mode, X, LM, N);
1961
Jean-Marc Valin086ea7c2013-11-29 16:47:52 -05001962 st->intensity = hysteresis_decision((opus_val16)(equiv_rate/1000),
Jean-Marc Valin2924af42013-11-25 21:24:30 -05001963 intensity_thresholds, intensity_histeresis, 21, st->intensity);
Jean-Marc Valin91f80102013-12-10 18:38:44 -05001964 st->intensity = IMIN(end,IMAX(start, st->intensity));
Jean-Marc Valin69062102012-11-08 09:42:27 -05001965 }
1966
1967 alloc_trim = 5;
1968 if (tell+(6<<BITRES) <= total_bits - total_boost)
1969 {
Jean-Marc Valin0247d342015-12-05 14:31:54 -05001970 if (start > 0 || st->lfe)
1971 {
1972 st->stereo_saving = 0;
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -04001973 alloc_trim = 5;
Jean-Marc Valin0247d342015-12-05 14:31:54 -05001974 } else {
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -04001975 alloc_trim = alloc_trim_analysis(mode, X, bandLogE,
xiangmingzhuc95c9a02014-04-30 15:48:07 +08001976 end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate,
Jean-Marc Valina671ac52017-05-24 01:07:20 -04001977 st->intensity, surround_trim, equiv_rate, st->arch);
Jean-Marc Valin0247d342015-12-05 14:31:54 -05001978 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05001979 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
1980 tell = ec_tell_frac(enc);
1981 }
1982
1983 /* Variable bitrate */
1984 if (vbr_rate>0)
1985 {
1986 opus_val16 alpha;
1987 opus_int32 delta;
1988 /* The target rate in 8th bits per frame */
1989 opus_int32 target, base_target;
1990 opus_int32 min_allowed;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001991 int lm_diff = mode->maxLM - LM;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001992
1993 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
1994 The CELT allocator will just not be able to use more than that anyway. */
1995 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
Jean-Marc Valinbcd6aba2015-12-06 10:50:15 -05001996 if (!hybrid)
1997 {
1998 base_target = vbr_rate - ((40*C+20)<<BITRES);
1999 } else {
2000 base_target = IMAX(0, vbr_rate - ((9*C+4)<<BITRES));
2001 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05002002
2003 if (st->constrained_vbr)
Jean-Marc Valin28733d12013-06-10 03:30:01 -04002004 base_target += (st->vbr_offset>>lm_diff);
Jean-Marc Valin69062102012-11-08 09:42:27 -05002005
Jean-Marc Valinbcd6aba2015-12-06 10:50:15 -05002006 if (!hybrid)
2007 {
2008 target = compute_vbr(mode, &st->analysis, base_target, LM, equiv_rate,
Jean-Marc Valin28733d12013-06-10 03:30:01 -04002009 st->lastCodedBands, C, st->intensity, st->constrained_vbr,
2010 st->stereo_saving, tot_boost, tf_estimate, pitch_change, maxDepth,
Jean-Marc Valin132ed592016-11-04 11:41:13 -04002011 st->lfe, st->energy_mask!=NULL, surround_masking,
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04002012 temporal_vbr);
Jean-Marc Valinbcd6aba2015-12-06 10:50:15 -05002013 } else {
2014 target = base_target;
Jean-Marc Valin61714e92016-04-23 00:34:53 -04002015 /* Tonal frames (offset<100) need more bits than noisy (offset>100) ones. */
2016 if (st->silk_info.offset < 100) target += 12 << BITRES >> (3-LM);
2017 if (st->silk_info.offset > 100) target -= 18 << BITRES >> (3-LM);
Jean-Marc Valin78fc6642016-05-31 17:54:20 -04002018 /* Boosting bitrate on transients and vowels with significant temporal
2019 spikes. */
Mark Harrisd6d70372017-02-20 19:51:40 -08002020 target += (opus_int32)MULT16_16_Q14(tf_estimate-QCONST16(.25f,14), (50<<BITRES));
Jean-Marc Valinbcd6aba2015-12-06 10:50:15 -05002021 /* If we have a strong transient, let's make sure it has enough bits to code
2022 the first two bands, so that it can use folding rather than noise. */
2023 if (tf_estimate > QCONST16(.7f,14))
Jean-Marc Valin78fc6642016-05-31 17:54:20 -04002024 target = IMAX(target, 50<<BITRES);
Jean-Marc Valinbcd6aba2015-12-06 10:50:15 -05002025 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05002026 /* The current offset is removed from the target and the space used
2027 so far is added*/
2028 target=target+tell;
2029 /* In VBR mode the frame size must not be reduced so much that it would
2030 result in the encoder running out of bits.
2031 The margin of 2 bytes ensures that none of the bust-prevention logic
2032 in the decoder will have triggered so far. */
Jean-Marc Valinbcd6aba2015-12-06 10:50:15 -05002033 min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2;
2034 /* Take into account the 37 bits we need to have left in the packet to
2035 signal a redundant frame in hybrid mode. Creating a shorter packet would
2036 create an entropy coder desync. */
2037 if (hybrid)
2038 min_allowed = IMAX(min_allowed, (tell0_frac+(37<<BITRES)+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3));
Jean-Marc Valin69062102012-11-08 09:42:27 -05002039
2040 nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
2041 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
Jean-Marc Valinbcd6aba2015-12-06 10:50:15 -05002042 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes);
Jean-Marc Valin69062102012-11-08 09:42:27 -05002043
2044 /* By how much did we "miss" the target on that frame */
2045 delta = target - vbr_rate;
2046
2047 target=nbAvailableBytes<<(BITRES+3);
2048
2049 /*If the frame is silent we don't adjust our drift, otherwise
2050 the encoder will shoot to very high rates after hitting a
2051 span of silence, but we do allow the bitres to refill.
2052 This means that we'll undershoot our target in CVBR/VBR modes
2053 on files with lots of silence. */
2054 if(silence)
2055 {
2056 nbAvailableBytes = 2;
2057 target = 2*8<<BITRES;
2058 delta = 0;
2059 }
2060
2061 if (st->vbr_count < 970)
2062 {
2063 st->vbr_count++;
2064 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
2065 } else
2066 alpha = QCONST16(.001f,15);
2067 /* How many bits have we used in excess of what we're allowed */
2068 if (st->constrained_vbr)
2069 st->vbr_reservoir += target - vbr_rate;
2070 /*printf ("%d\n", st->vbr_reservoir);*/
2071
2072 /* Compute the offset we need to apply in order to reach the target */
2073 if (st->constrained_vbr)
2074 {
2075 st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
2076 st->vbr_offset = -st->vbr_drift;
2077 }
2078 /*printf ("%d\n", st->vbr_drift);*/
2079
2080 if (st->constrained_vbr && st->vbr_reservoir < 0)
2081 {
2082 /* We're under the min value -- increase rate */
2083 int adjust = (-st->vbr_reservoir)/(8<<BITRES);
2084 /* Unless we're just coding silence */
2085 nbAvailableBytes += silence?0:adjust;
2086 st->vbr_reservoir = 0;
2087 /*printf ("+%d\n", adjust);*/
2088 }
Jean-Marc Valinbcd6aba2015-12-06 10:50:15 -05002089 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes);
Jean-Marc Valin69062102012-11-08 09:42:27 -05002090 /*printf("%d\n", nbCompressedBytes*50*8);*/
2091 /* This moves the raw bits to take into account the new compressed size */
2092 ec_enc_shrink(enc, nbCompressedBytes);
2093 }
2094
2095 /* Bit allocation */
2096 ALLOC(fine_quant, nbEBands, int);
2097 ALLOC(pulses, nbEBands, int);
2098 ALLOC(fine_priority, nbEBands, int);
2099
2100 /* bits = packet size - where we are - safety*/
2101 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
2102 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
2103 bits -= anti_collapse_rsv;
Jean-Marc Valin91f80102013-12-10 18:38:44 -05002104 signalBandwidth = end-1;
Jean-Marc Valin3ab03e02013-09-06 16:00:39 -04002105#ifndef DISABLE_FLOAT_API
Jean-Marc Valin5fb50ad2012-12-21 01:23:26 -05002106 if (st->analysis.valid)
Jean-Marc Valin6e277c62013-05-17 14:15:31 -04002107 {
2108 int min_bandwidth;
Jean-Marc Valinc1959e72013-11-08 19:24:10 -05002109 if (equiv_rate < (opus_int32)32000*C)
Jean-Marc Valin6e277c62013-05-17 14:15:31 -04002110 min_bandwidth = 13;
Jean-Marc Valinc1959e72013-11-08 19:24:10 -05002111 else if (equiv_rate < (opus_int32)48000*C)
Jean-Marc Valin6e277c62013-05-17 14:15:31 -04002112 min_bandwidth = 16;
Jean-Marc Valinc1959e72013-11-08 19:24:10 -05002113 else if (equiv_rate < (opus_int32)60000*C)
Jean-Marc Valin6e277c62013-05-17 14:15:31 -04002114 min_bandwidth = 18;
Jean-Marc Valinc1959e72013-11-08 19:24:10 -05002115 else if (equiv_rate < (opus_int32)80000*C)
Jean-Marc Valin6e277c62013-05-17 14:15:31 -04002116 min_bandwidth = 19;
2117 else
2118 min_bandwidth = 20;
2119 signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth);
2120 }
Jean-Marc Valin5fb50ad2012-12-21 01:23:26 -05002121#endif
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -04002122 if (st->lfe)
2123 signalBandwidth = 1;
Jean-Marc Valin91f80102013-12-10 18:38:44 -05002124 codedBands = compute_allocation(mode, start, end, offsets, cap,
Jean-Marc Valin69062102012-11-08 09:42:27 -05002125 alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses,
Jean-Marc Valin5fb50ad2012-12-21 01:23:26 -05002126 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBandwidth);
Jean-Marc Valina4dccd32013-05-04 23:54:20 -04002127 if (st->lastCodedBands)
2128 st->lastCodedBands = IMIN(st->lastCodedBands+1,IMAX(st->lastCodedBands-1,codedBands));
2129 else
2130 st->lastCodedBands = codedBands;
Jean-Marc Valin69062102012-11-08 09:42:27 -05002131
Jean-Marc Valin91f80102013-12-10 18:38:44 -05002132 quant_fine_energy(mode, start, end, oldBandE, error, fine_quant, enc, C);
Jean-Marc Valin69062102012-11-08 09:42:27 -05002133
Jean-Marc Valin69062102012-11-08 09:42:27 -05002134 /* Residual quantisation */
2135 ALLOC(collapse_masks, C*nbEBands, unsigned char);
Jean-Marc Valin91f80102013-12-10 18:38:44 -05002136 quant_all_bands(1, mode, start, end, X, C==2 ? X+N : NULL, collapse_masks,
xiangmingzhuc95c9a02014-04-30 15:48:07 +08002137 bandE, pulses, shortBlocks, st->spread_decision,
2138 dual_stereo, st->intensity, tf_res, nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv,
Jean-Marc Valin18a380a2016-04-20 13:27:06 -04002139 balance, enc, LM, codedBands, &st->rng, st->complexity, st->arch, st->disable_inv);
Jean-Marc Valin69062102012-11-08 09:42:27 -05002140
2141 if (anti_collapse_rsv > 0)
2142 {
2143 anti_collapse_on = st->consec_transient<2;
2144#ifdef FUZZING
2145 anti_collapse_on = rand()&0x1;
2146#endif
2147 ec_enc_bits(enc, anti_collapse_on, 1);
2148 }
Jean-Marc Valin91f80102013-12-10 18:38:44 -05002149 quant_energy_finalise(mode, start, end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
Jean-Marc Valin6fccb4b2016-07-04 01:06:11 -04002150 OPUS_CLEAR(energyError, nbEBands*CC);
2151 c=0;
2152 do {
2153 for (i=start;i<end;i++)
2154 {
2155 energyError[i+c*nbEBands] = MAX16(-QCONST16(0.5f, 15), MIN16(QCONST16(0.5f, 15), error[i+c*nbEBands]));
2156 }
2157 } while (++c < C);
Jean-Marc Valin69062102012-11-08 09:42:27 -05002158
2159 if (silence)
2160 {
2161 for (i=0;i<C*nbEBands;i++)
2162 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
2163 }
2164
2165#ifdef RESYNTH
2166 /* Re-synthesis of the coded audio if required */
2167 {
2168 celt_sig *out_mem[2];
2169
Jean-Marc Valin69062102012-11-08 09:42:27 -05002170 if (anti_collapse_on)
2171 {
2172 anti_collapse(mode, X, collapse_masks, LM, C, N,
Jean-Marc Valin91f80102013-12-10 18:38:44 -05002173 start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
Jean-Marc Valin69062102012-11-08 09:42:27 -05002174 }
2175
Jean-Marc Valin69062102012-11-08 09:42:27 -05002176 c=0; do {
Nils Wallméniuse0884fe2012-12-01 21:11:50 +01002177 OPUS_MOVE(st->syn_mem[c], st->syn_mem[c]+N, 2*MAX_PERIOD-N+overlap/2);
Jean-Marc Valin69062102012-11-08 09:42:27 -05002178 } while (++c<CC);
2179
Jean-Marc Valin69062102012-11-08 09:42:27 -05002180 c=0; do {
2181 out_mem[c] = st->syn_mem[c]+2*MAX_PERIOD-N;
2182 } while (++c<CC);
2183
Viswanath Puttagunta19c54062015-05-15 12:42:20 -05002184 celt_synthesis(mode, X, out_mem, oldBandE, start, effEnd,
2185 C, CC, isTransient, LM, st->upsample, silence, st->arch);
Jean-Marc Valin69062102012-11-08 09:42:27 -05002186
2187 c=0; do {
2188 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
2189 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
2190 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, mode->shortMdctSize,
2191 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
Jean-Marc Valin91f80102013-12-10 18:38:44 -05002192 mode->window, overlap);
Jean-Marc Valin69062102012-11-08 09:42:27 -05002193 if (LM!=0)
2194 comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMdctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize,
2195 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
2196 mode->window, overlap);
2197 } while (++c<CC);
2198
2199 /* We reuse freq[] as scratch space for the de-emphasis */
Jean-Marc Valinbdc7b932014-01-06 08:58:38 -05002200 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, mode->preemph, st->preemph_memD);
Jean-Marc Valin69062102012-11-08 09:42:27 -05002201 st->prefilter_period_old = st->prefilter_period;
2202 st->prefilter_gain_old = st->prefilter_gain;
2203 st->prefilter_tapset_old = st->prefilter_tapset;
2204 }
2205#endif
2206
2207 st->prefilter_period = pitch_index;
2208 st->prefilter_gain = gain1;
2209 st->prefilter_tapset = prefilter_tapset;
2210#ifdef RESYNTH
2211 if (LM!=0)
2212 {
2213 st->prefilter_period_old = st->prefilter_period;
2214 st->prefilter_gain_old = st->prefilter_gain;
2215 st->prefilter_tapset_old = st->prefilter_tapset;
2216 }
2217#endif
2218
2219 if (CC==2&&C==1) {
Jean-Marc Valinff072002013-12-08 23:31:30 -05002220 OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands);
Jean-Marc Valin69062102012-11-08 09:42:27 -05002221 }
2222
2223 if (!isTransient)
2224 {
Jean-Marc Valinff072002013-12-08 23:31:30 -05002225 OPUS_COPY(oldLogE2, oldLogE, CC*nbEBands);
2226 OPUS_COPY(oldLogE, oldBandE, CC*nbEBands);
Jean-Marc Valin69062102012-11-08 09:42:27 -05002227 } else {
2228 for (i=0;i<CC*nbEBands;i++)
2229 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
2230 }
2231 /* In case start or end were to change */
2232 c=0; do
2233 {
Jean-Marc Valin91f80102013-12-10 18:38:44 -05002234 for (i=0;i<start;i++)
Jean-Marc Valin69062102012-11-08 09:42:27 -05002235 {
2236 oldBandE[c*nbEBands+i]=0;
2237 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2238 }
Jean-Marc Valin91f80102013-12-10 18:38:44 -05002239 for (i=end;i<nbEBands;i++)
Jean-Marc Valin69062102012-11-08 09:42:27 -05002240 {
2241 oldBandE[c*nbEBands+i]=0;
2242 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2243 }
2244 } while (++c<CC);
2245
Jean-Marc Valin3252bf22013-04-19 03:14:28 -04002246 if (isTransient || transient_got_disabled)
Jean-Marc Valin69062102012-11-08 09:42:27 -05002247 st->consec_transient++;
2248 else
2249 st->consec_transient=0;
2250 st->rng = enc->rng;
2251
2252 /* If there's any room left (can only happen for very high rates),
2253 it's already filled with zeros */
2254 ec_enc_done(enc);
2255
2256#ifdef CUSTOM_MODES
2257 if (st->signalling)
2258 nbCompressedBytes++;
2259#endif
2260
2261 RESTORE_STACK;
2262 if (ec_get_error(enc))
2263 return OPUS_INTERNAL_ERROR;
2264 else
2265 return nbCompressedBytes;
2266}
2267
2268
2269#ifdef CUSTOM_MODES
2270
2271#ifdef FIXED_POINT
2272int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2273{
2274 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
2275}
2276
2277#ifndef DISABLE_FLOAT_API
2278int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2279{
2280 int j, ret, C, N;
2281 VARDECL(opus_int16, in);
2282 ALLOC_STACK;
2283
2284 if (pcm==NULL)
2285 return OPUS_BAD_ARG;
2286
2287 C = st->channels;
2288 N = frame_size;
2289 ALLOC(in, C*N, opus_int16);
2290
2291 for (j=0;j<C*N;j++)
2292 in[j] = FLOAT2INT16(pcm[j]);
2293
2294 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2295#ifdef RESYNTH
2296 for (j=0;j<C*N;j++)
2297 ((float*)pcm)[j]=in[j]*(1.f/32768.f);
2298#endif
2299 RESTORE_STACK;
2300 return ret;
2301}
2302#endif /* DISABLE_FLOAT_API */
2303#else
2304
2305int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2306{
2307 int j, ret, C, N;
2308 VARDECL(celt_sig, in);
2309 ALLOC_STACK;
2310
2311 if (pcm==NULL)
2312 return OPUS_BAD_ARG;
2313
2314 C=st->channels;
2315 N=frame_size;
2316 ALLOC(in, C*N, celt_sig);
2317 for (j=0;j<C*N;j++) {
2318 in[j] = SCALEOUT(pcm[j]);
2319 }
2320
2321 ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2322#ifdef RESYNTH
2323 for (j=0;j<C*N;j++)
2324 ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
2325#endif
2326 RESTORE_STACK;
2327 return ret;
2328}
2329
2330int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2331{
2332 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
2333}
2334
2335#endif
2336
2337#endif /* CUSTOM_MODES */
2338
2339int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...)
2340{
2341 va_list ap;
2342
2343 va_start(ap, request);
2344 switch (request)
2345 {
2346 case OPUS_SET_COMPLEXITY_REQUEST:
2347 {
2348 int value = va_arg(ap, opus_int32);
2349 if (value<0 || value>10)
2350 goto bad_arg;
2351 st->complexity = value;
2352 }
2353 break;
2354 case CELT_SET_START_BAND_REQUEST:
2355 {
2356 opus_int32 value = va_arg(ap, opus_int32);
2357 if (value<0 || value>=st->mode->nbEBands)
2358 goto bad_arg;
2359 st->start = value;
2360 }
2361 break;
2362 case CELT_SET_END_BAND_REQUEST:
2363 {
2364 opus_int32 value = va_arg(ap, opus_int32);
2365 if (value<1 || value>st->mode->nbEBands)
2366 goto bad_arg;
2367 st->end = value;
2368 }
2369 break;
2370 case CELT_SET_PREDICTION_REQUEST:
2371 {
2372 int value = va_arg(ap, opus_int32);
2373 if (value<0 || value>2)
2374 goto bad_arg;
2375 st->disable_pf = value<=1;
2376 st->force_intra = value==0;
2377 }
2378 break;
2379 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2380 {
2381 int value = va_arg(ap, opus_int32);
2382 if (value<0 || value>100)
2383 goto bad_arg;
2384 st->loss_rate = value;
2385 }
2386 break;
2387 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2388 {
2389 opus_int32 value = va_arg(ap, opus_int32);
2390 st->constrained_vbr = value;
2391 }
2392 break;
2393 case OPUS_SET_VBR_REQUEST:
2394 {
2395 opus_int32 value = va_arg(ap, opus_int32);
2396 st->vbr = value;
2397 }
2398 break;
2399 case OPUS_SET_BITRATE_REQUEST:
2400 {
2401 opus_int32 value = va_arg(ap, opus_int32);
2402 if (value<=500 && value!=OPUS_BITRATE_MAX)
2403 goto bad_arg;
2404 value = IMIN(value, 260000*st->channels);
2405 st->bitrate = value;
2406 }
2407 break;
2408 case CELT_SET_CHANNELS_REQUEST:
2409 {
2410 opus_int32 value = va_arg(ap, opus_int32);
2411 if (value<1 || value>2)
2412 goto bad_arg;
2413 st->stream_channels = value;
2414 }
2415 break;
2416 case OPUS_SET_LSB_DEPTH_REQUEST:
2417 {
2418 opus_int32 value = va_arg(ap, opus_int32);
2419 if (value<8 || value>24)
2420 goto bad_arg;
2421 st->lsb_depth=value;
2422 }
2423 break;
2424 case OPUS_GET_LSB_DEPTH_REQUEST:
2425 {
2426 opus_int32 *value = va_arg(ap, opus_int32*);
2427 *value=st->lsb_depth;
2428 }
2429 break;
Jean-Marc Valin18a380a2016-04-20 13:27:06 -04002430 case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST:
2431 {
2432 opus_int32 value = va_arg(ap, opus_int32);
2433 if(value<0 || value>1)
2434 {
2435 goto bad_arg;
2436 }
2437 st->disable_inv = value;
2438 }
2439 break;
2440 case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST:
2441 {
2442 opus_int32 *value = va_arg(ap, opus_int32*);
2443 if (!value)
2444 {
2445 goto bad_arg;
2446 }
2447 *value = st->disable_inv;
2448 }
2449 break;
Jean-Marc Valin69062102012-11-08 09:42:27 -05002450 case OPUS_RESET_STATE:
2451 {
2452 int i;
2453 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
Jean-Marc Valin91f80102013-12-10 18:38:44 -05002454 oldBandE = (opus_val16*)(st->in_mem+st->channels*(st->mode->overlap+COMBFILTER_MAXPERIOD));
Jean-Marc Valin69062102012-11-08 09:42:27 -05002455 oldLogE = oldBandE + st->channels*st->mode->nbEBands;
2456 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
2457 OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
2458 opus_custom_encoder_get_size(st->mode, st->channels)-
2459 ((char*)&st->ENCODER_RESET_START - (char*)st));
2460 for (i=0;i<st->channels*st->mode->nbEBands;i++)
2461 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
2462 st->vbr_offset = 0;
2463 st->delayedIntra = 1;
2464 st->spread_decision = SPREAD_NORMAL;
2465 st->tonal_average = 256;
2466 st->hf_average = 0;
2467 st->tapset_decision = 0;
2468 }
2469 break;
2470#ifdef CUSTOM_MODES
2471 case CELT_SET_INPUT_CLIPPING_REQUEST:
2472 {
2473 opus_int32 value = va_arg(ap, opus_int32);
2474 st->clip = value;
2475 }
2476 break;
2477#endif
2478 case CELT_SET_SIGNALLING_REQUEST:
2479 {
2480 opus_int32 value = va_arg(ap, opus_int32);
2481 st->signalling = value;
2482 }
2483 break;
2484 case CELT_SET_ANALYSIS_REQUEST:
2485 {
2486 AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
2487 if (info)
2488 OPUS_COPY(&st->analysis, info, 1);
2489 }
2490 break;
Jean-Marc Valin61714e92016-04-23 00:34:53 -04002491 case CELT_SET_SILK_INFO_REQUEST:
2492 {
2493 SILKInfo *info = va_arg(ap, SILKInfo *);
2494 if (info)
2495 OPUS_COPY(&st->silk_info, info, 1);
2496 }
2497 break;
Jean-Marc Valin69062102012-11-08 09:42:27 -05002498 case CELT_GET_MODE_REQUEST:
2499 {
2500 const CELTMode ** value = va_arg(ap, const CELTMode**);
2501 if (value==0)
2502 goto bad_arg;
2503 *value=st->mode;
2504 }
2505 break;
2506 case OPUS_GET_FINAL_RANGE_REQUEST:
2507 {
2508 opus_uint32 * value = va_arg(ap, opus_uint32 *);
2509 if (value==0)
2510 goto bad_arg;
2511 *value=st->rng;
2512 }
2513 break;
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -04002514 case OPUS_SET_LFE_REQUEST:
2515 {
2516 opus_int32 value = va_arg(ap, opus_int32);
2517 st->lfe = value;
2518 }
2519 break;
Jean-Marc Valina4dccd32013-05-04 23:54:20 -04002520 case OPUS_SET_ENERGY_MASK_REQUEST:
2521 {
2522 opus_val16 *value = va_arg(ap, opus_val16*);
2523 st->energy_mask = value;
2524 }
2525 break;
Jean-Marc Valin69062102012-11-08 09:42:27 -05002526 default:
2527 goto bad_request;
2528 }
2529 va_end(ap);
2530 return OPUS_OK;
2531bad_arg:
2532 va_end(ap);
2533 return OPUS_BAD_ARG;
2534bad_request:
2535 va_end(ap);
2536 return OPUS_UNIMPLEMENTED;
2537}