blob: 5bb86c52c426e46677682f7e2bfacd89a929e408 [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 */
60 int overlap;
61 int channels;
62 int stream_channels;
63
64 int force_intra;
65 int clip;
66 int disable_pf;
67 int complexity;
68 int upsample;
69 int start, end;
70
71 opus_int32 bitrate;
72 int vbr;
73 int signalling;
74 int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */
75 int loss_rate;
76 int lsb_depth;
Jean-Marc Valin3252bf22013-04-19 03:14:28 -040077 int variable_duration;
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -040078 int lfe;
Aurélien Zanellicd4c8242013-05-31 15:07:00 +020079 int arch;
Jean-Marc Valin69062102012-11-08 09:42:27 -050080
81 /* Everything beyond this point gets cleared on a reset */
82#define ENCODER_RESET_START rng
83
84 opus_uint32 rng;
85 int spread_decision;
86 opus_val32 delayedIntra;
87 int tonal_average;
88 int lastCodedBands;
89 int hf_average;
90 int tapset_decision;
91
92 int prefilter_period;
93 opus_val16 prefilter_gain;
94 int prefilter_tapset;
95#ifdef RESYNTH
96 int prefilter_period_old;
97 opus_val16 prefilter_gain_old;
98 int prefilter_tapset_old;
99#endif
100 int consec_transient;
101 AnalysisInfo analysis;
102
103 opus_val32 preemph_memE[2];
104 opus_val32 preemph_memD[2];
105
106 /* VBR-related parameters */
107 opus_int32 vbr_reservoir;
108 opus_int32 vbr_drift;
109 opus_int32 vbr_offset;
110 opus_int32 vbr_count;
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 */
127};
128
129int celt_encoder_get_size(int channels)
130{
131 CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
132 return opus_custom_encoder_get_size(mode, channels);
133}
134
135OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
136{
137 int size = sizeof(struct CELTEncoder)
138 + (channels*mode->overlap-1)*sizeof(celt_sig) /* celt_sig in_mem[channels*mode->overlap]; */
139 + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig) /* celt_sig prefilter_mem[channels*COMBFILTER_MAXPERIOD]; */
140 + 3*channels*mode->nbEBands*sizeof(opus_val16); /* opus_val16 oldBandE[channels*mode->nbEBands]; */
141 /* opus_val16 oldLogE[channels*mode->nbEBands]; */
142 /* opus_val16 oldLogE2[channels*mode->nbEBands]; */
143 return size;
144}
145
146#ifdef CUSTOM_MODES
147CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
148{
149 int ret;
150 CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels));
151 /* init will handle the NULL case */
152 ret = opus_custom_encoder_init(st, mode, channels);
153 if (ret != OPUS_OK)
154 {
155 opus_custom_encoder_destroy(st);
156 st = NULL;
157 }
158 if (error)
159 *error = ret;
160 return st;
161}
162#endif /* CUSTOM_MODES */
163
164int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels)
165{
166 int ret;
167 ret = opus_custom_encoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
168 if (ret != OPUS_OK)
169 return ret;
170 st->upsample = resampling_factor(sampling_rate);
171 return OPUS_OK;
172}
173
174OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
175{
176 if (channels < 0 || channels > 2)
177 return OPUS_BAD_ARG;
178
179 if (st==NULL || mode==NULL)
180 return OPUS_ALLOC_FAIL;
181
182 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
183
184 st->mode = mode;
185 st->overlap = mode->overlap;
186 st->stream_channels = st->channels = channels;
187
188 st->upsample = 1;
189 st->start = 0;
190 st->end = st->mode->effEBands;
191 st->signalling = 1;
192
Aurélien Zanellicd4c8242013-05-31 15:07:00 +0200193 st->arch = opus_select_arch();
194
Jean-Marc Valin69062102012-11-08 09:42:27 -0500195 st->constrained_vbr = 1;
196 st->clip = 1;
197
198 st->bitrate = OPUS_BITRATE_MAX;
199 st->vbr = 0;
200 st->force_intra = 0;
201 st->complexity = 5;
202 st->lsb_depth=24;
203
204 opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
205
206 return OPUS_OK;
207}
208
209#ifdef CUSTOM_MODES
210void opus_custom_encoder_destroy(CELTEncoder *st)
211{
212 opus_free(st);
213}
214#endif /* CUSTOM_MODES */
215
216
217static int transient_analysis(const opus_val32 * OPUS_RESTRICT in, int len, int C,
218 opus_val16 *tf_estimate, int *tf_chan)
219{
220 int i;
221 VARDECL(opus_val16, tmp);
222 opus_val32 mem0,mem1;
223 int is_transient = 0;
224 opus_int32 mask_metric = 0;
225 int c;
Jean-Marc Valind683c762012-12-21 16:17:38 -0500226 opus_val16 tf_max;
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500227 int len2;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500228 /* Table of 6*64/x, trained on real data to minimize the average error */
229 static const unsigned char inv_table[128] = {
230 255,255,156,110, 86, 70, 59, 51, 45, 40, 37, 33, 31, 28, 26, 25,
231 23, 22, 21, 20, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
232 12, 12, 11, 11, 11, 10, 10, 10, 9, 9, 9, 9, 9, 9, 8, 8,
233 8, 8, 8, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6,
234 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5,
235 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
236 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3,
237 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2,
238 };
239 SAVE_STACK;
240 ALLOC(tmp, len, opus_val16);
241
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500242 len2=len/2;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500243 for (c=0;c<C;c++)
244 {
245 opus_val32 mean;
246 opus_int32 unmask=0;
247 opus_val32 norm;
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500248 opus_val16 maxE;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500249 mem0=0;
250 mem1=0;
251 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
252 for (i=0;i<len;i++)
253 {
254 opus_val32 x,y;
255 x = SHR32(in[i+c*len],SIG_SHIFT);
256 y = ADD32(mem0, x);
257#ifdef FIXED_POINT
258 mem0 = mem1 + y - SHL32(x,1);
259 mem1 = x - SHR32(y,1);
260#else
261 mem0 = mem1 + y - 2*x;
262 mem1 = x - .5f*y;
263#endif
264 tmp[i] = EXTRACT16(SHR32(y,2));
265 /*printf("%f ", tmp[i]);*/
266 }
267 /*printf("\n");*/
268 /* First few samples are bad because we don't propagate the memory */
269 for (i=0;i<12;i++)
270 tmp[i] = 0;
271
272#ifdef FIXED_POINT
273 /* Normalize tmp to max range */
274 {
275 int shift=0;
276 shift = 14-celt_ilog2(1+celt_maxabs16(tmp, len));
277 if (shift!=0)
278 {
279 for (i=0;i<len;i++)
280 tmp[i] = SHL16(tmp[i], shift);
281 }
282 }
283#endif
284
285 mean=0;
286 mem0=0;
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500287 /* Grouping by two to reduce complexity */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500288 /* Forward pass to compute the post-echo threshold*/
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500289 for (i=0;i<len2;i++)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500290 {
291 opus_val16 x2 = PSHR32(MULT16_16(tmp[2*i],tmp[2*i]) + MULT16_16(tmp[2*i+1],tmp[2*i+1]),16);
292 mean += x2;
293#ifdef FIXED_POINT
294 /* FIXME: Use PSHR16() instead */
295 tmp[i] = mem0 + PSHR32(x2-mem0,4);
296#else
297 tmp[i] = mem0 + MULT16_16_P15(QCONST16(.0625f,15),x2-mem0);
298#endif
299 mem0 = tmp[i];
300 }
301
302 mem0=0;
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500303 maxE=0;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500304 /* Backward pass to compute the pre-echo threshold */
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500305 for (i=len2-1;i>=0;i--)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500306 {
307#ifdef FIXED_POINT
308 /* FIXME: Use PSHR16() instead */
309 tmp[i] = mem0 + PSHR32(tmp[i]-mem0,3);
310#else
311 tmp[i] = mem0 + MULT16_16_P15(QCONST16(0.125f,15),tmp[i]-mem0);
312#endif
313 mem0 = tmp[i];
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500314 maxE = MAX16(maxE, mem0);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500315 }
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500316 /*for (i=0;i<len2;i++)printf("%f ", tmp[i]/mean);printf("\n");*/
Jean-Marc Valin69062102012-11-08 09:42:27 -0500317
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500318 /* Compute the ratio of the "frame energy" over the harmonic mean of the energy.
Jean-Marc Valin69062102012-11-08 09:42:27 -0500319 This essentially corresponds to a bitrate-normalized temporal noise-to-mask
320 ratio */
321
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500322 /* As a compromise with the old transient detector, frame energy is the
323 geometric mean of the energy and half the max */
324#ifdef FIXED_POINT
325 /* Costs two sqrt() to avoid overflows */
326 mean = MULT16_16(celt_sqrt(mean), celt_sqrt(MULT16_16(maxE,len2>>1)));
327#else
Gregory Maxwell5280c712013-07-15 15:51:24 -0700328 mean = celt_sqrt(mean * maxE*.5*len2);
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500329#endif
Jean-Marc Valin69062102012-11-08 09:42:27 -0500330 /* Inverse of the mean energy in Q15+6 */
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500331 norm = SHL32(EXTEND32(len2),6+14)/ADD32(EPSILON,SHR32(mean,1));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500332 /* Compute harmonic mean discarding the unreliable boundaries
333 The data is smooth, so we only take 1/4th of the samples */
334 unmask=0;
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500335 for (i=12;i<len2-5;i+=4)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500336 {
337 int id;
338#ifdef FIXED_POINT
339 id = IMAX(0,IMIN(127,MULT16_32_Q15(tmp[i],norm))); /* Do not round to nearest */
340#else
Jean-Marc Valind683c762012-12-21 16:17:38 -0500341 id = IMAX(0,IMIN(127,(int)floor(64*norm*tmp[i]))); /* Do not round to nearest */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500342#endif
343 unmask += inv_table[id];
344 }
345 /*printf("%d\n", unmask);*/
346 /* 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 -0500347 unmask = 64*unmask*4/(6*(len2-17));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500348 if (unmask>mask_metric)
349 {
350 *tf_chan = c;
351 mask_metric = unmask;
352 }
353 }
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500354 is_transient = mask_metric>200;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500355
356 /* Arbitrary metric for VBR boost */
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500357 tf_max = MAX16(0,celt_sqrt(27*mask_metric)-42);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500358 /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */
Jean-Marc Valind683c762012-12-21 16:17:38 -0500359 *tf_estimate = celt_sqrt(MAX16(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 -0500360 /*printf("%d %f\n", tf_max, mask_metric);*/
361 RESTORE_STACK;
362#ifdef FUZZING
363 is_transient = rand()&0x1;
364#endif
365 /*printf("%d %f %d\n", is_transient, (float)*tf_estimate, tf_max);*/
366 return is_transient;
367}
368
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500369/* Looks for sudden increases of energy to decide whether we need to patch
370 the transient decision */
Stefan Hackera32fa312013-09-20 04:07:53 +0200371int patch_transient_decision(opus_val16 *newE, opus_val16 *oldE, int nbEBands,
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500372 int end, int C)
373{
374 int i, c;
375 opus_val32 mean_diff=0;
376 opus_val16 spread_old[26];
377 /* Apply an aggressive (-6 dB/Bark) spreading function to the old frame to
378 avoid false detection caused by irrelevant bands */
379 if (C==1)
380 {
Stefan Hackera32fa312013-09-20 04:07:53 +0200381 spread_old[0] = oldE[0];
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500382 for (i=1;i<end;i++)
Stefan Hackera32fa312013-09-20 04:07:53 +0200383 spread_old[i] = MAX16(spread_old[i-1]-QCONST16(1.0f, DB_SHIFT), oldE[i]);
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500384 } else {
Stefan Hackera32fa312013-09-20 04:07:53 +0200385 spread_old[0] = MAX16(oldE[0],oldE[nbEBands]);
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500386 for (i=1;i<end;i++)
387 spread_old[i] = MAX16(spread_old[i-1]-QCONST16(1.0f, DB_SHIFT),
Stefan Hackera32fa312013-09-20 04:07:53 +0200388 MAX16(oldE[i],oldE[i+nbEBands]));
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500389 }
390 for (i=end-2;i>=0;i--)
391 spread_old[i] = MAX16(spread_old[i], spread_old[i+1]-QCONST16(1.0f, DB_SHIFT));
392 /* Compute mean increase */
393 c=0; do {
394 for (i=2;i<end-1;i++)
395 {
396 opus_val16 x1, x2;
Stefan Hackera32fa312013-09-20 04:07:53 +0200397 x1 = MAX16(0, newE[i]);
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500398 x2 = MAX16(0, spread_old[i]);
399 mean_diff = ADD32(mean_diff, EXTEND32(MAX16(0, SUB16(x1, x2))));
400 }
401 } while (++c<C);
402 mean_diff = DIV32(mean_diff, C*(end-3));
403 /*printf("%f %f %d\n", mean_diff, max_diff, count);*/
404 return mean_diff > QCONST16(1.f, DB_SHIFT);
405}
406
Jean-Marc Valin69062102012-11-08 09:42:27 -0500407/** Apply window and compute the MDCT for all sub-frames and
408 all channels in a frame */
Jean-Marc Valin851f8032013-02-18 01:43:43 -0500409static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS_RESTRICT in,
410 celt_sig * OPUS_RESTRICT out, int C, int CC, int LM, int upsample)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500411{
412 const int overlap = OVERLAP(mode);
413 int N;
414 int B;
415 int shift;
Jean-Marc Valin851f8032013-02-18 01:43:43 -0500416 int i, b, c;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500417 if (shortBlocks)
418 {
419 B = shortBlocks;
420 N = mode->shortMdctSize;
421 shift = mode->maxLM;
422 } else {
423 B = 1;
424 N = mode->shortMdctSize<<LM;
425 shift = mode->maxLM-LM;
426 }
427 c=0; do {
428 for (b=0;b<B;b++)
429 {
430 /* Interleaving the sub-frames while doing the MDCTs */
431 clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N, &out[b+c*N*B], mode->window, overlap, shift, B);
432 }
Jean-Marc Valin851f8032013-02-18 01:43:43 -0500433 } while (++c<CC);
434 if (CC==2&&C==1)
435 {
436 for (i=0;i<B*N;i++)
437 out[i] = ADD32(HALF32(out[i]), HALF32(out[B*N+i]));
438 }
439 if (upsample != 1)
440 {
441 c=0; do
442 {
443 int bound = B*N/upsample;
444 for (i=0;i<bound;i++)
445 out[c*B*N+i] *= upsample;
446 for (;i<B*N;i++)
447 out[c*B*N+i] = 0;
448 } while (++c<C);
449 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500450}
451
452
Jean-Marc Valinfdceae82013-08-30 21:58:02 -0400453void preemphasis(const opus_val16 * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTRICT inp,
Jean-Marc Valin69062102012-11-08 09:42:27 -0500454 int N, int CC, int upsample, const opus_val16 *coef, celt_sig *mem, int clip)
455{
456 int i;
Jean-Marc Valine368e622013-01-03 14:28:28 -0500457 opus_val16 coef0;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500458 celt_sig m;
459 int Nu;
460
461 coef0 = coef[0];
Jean-Marc Valin69062102012-11-08 09:42:27 -0500462
463
464 Nu = N/upsample;
465 if (upsample!=1)
466 {
467 for (i=0;i<N;i++)
468 inp[i] = 0;
469 }
470 for (i=0;i<Nu;i++)
471 {
472 celt_sig x;
473
474 x = SCALEIN(pcmp[CC*i]);
475#ifndef FIXED_POINT
476 /* Replace NaNs with zeros */
477 if (!(x==x))
478 x = 0;
479#endif
480 inp[i*upsample] = x;
481 }
482
483#ifndef FIXED_POINT
484 if (clip)
485 {
486 /* Clip input to avoid encoding non-portable files */
487 for (i=0;i<Nu;i++)
488 inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample]));
489 }
490#endif
491 m = *mem;
Jean-Marc Valine368e622013-01-03 14:28:28 -0500492#ifdef CUSTOM_MODES
493 if (coef[1] != 0)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500494 {
Jean-Marc Valine368e622013-01-03 14:28:28 -0500495 opus_val16 coef1 = coef[1];
Jean-Marc Valin69062102012-11-08 09:42:27 -0500496 opus_val16 coef2 = coef[2];
497 for (i=0;i<N;i++)
498 {
499 opus_val16 x, tmp;
500 x = inp[i];
501 /* Apply pre-emphasis */
502 tmp = MULT16_16(coef2, x);
503 inp[i] = tmp + m;
504 m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp);
505 }
Jean-Marc Valine368e622013-01-03 14:28:28 -0500506 } else
Jean-Marc Valinebdfbfb2013-01-09 11:13:00 -0500507#endif
Jean-Marc Valine368e622013-01-03 14:28:28 -0500508 {
509 for (i=0;i<N;i++)
510 {
511 celt_sig x;
512 x = SHL32(inp[i], SIG_SHIFT);
513 /* Apply pre-emphasis */
514 inp[i] = x + m;
515 m = - MULT16_32_Q15(coef0, x);
516 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500517 }
518 *mem = m;
519}
520
521
522
523static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, opus_val16 bias)
524{
525 int i;
526 opus_val32 L1;
527 L1 = 0;
528 for (i=0;i<N;i++)
529 L1 += EXTEND32(ABS16(tmp[i]));
530 /* When in doubt, prefer good freq resolution */
531 L1 = MAC16_32_Q15(L1, LM*bias, L1);
532 return L1;
533
534}
535
Jean-Marc Valina6d663c2012-11-08 13:26:49 -0500536static int tf_analysis(const CELTMode *m, int len, int isTransient,
537 int *tf_res, int lambda, celt_norm *X, int N0, int LM,
Jean-Marc Valin69062102012-11-08 09:42:27 -0500538 int *tf_sum, opus_val16 tf_estimate, int tf_chan)
539{
540 int i;
541 VARDECL(int, metric);
542 int cost0;
543 int cost1;
544 VARDECL(int, path0);
545 VARDECL(int, path1);
546 VARDECL(celt_norm, tmp);
547 VARDECL(celt_norm, tmp_1);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500548 int sel;
549 int selcost[2];
550 int tf_select=0;
551 opus_val16 bias;
552
553 SAVE_STACK;
Jean-Marc Valindae16fb2012-12-13 21:40:58 -0500554 bias = MULT16_16_Q14(QCONST16(.04f,15), MAX16(-QCONST16(.25f,14), QCONST16(.5f,14)-tf_estimate));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500555 /*printf("%f ", bias);*/
556
Jean-Marc Valin69062102012-11-08 09:42:27 -0500557 ALLOC(metric, len, int);
558 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
559 ALLOC(tmp_1, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
560 ALLOC(path0, len, int);
561 ALLOC(path1, len, int);
562
563 *tf_sum = 0;
564 for (i=0;i<len;i++)
565 {
566 int j, k, N;
567 int narrow;
568 opus_val32 L1, best_L1;
569 int best_level=0;
570 N = (m->eBands[i+1]-m->eBands[i])<<LM;
571 /* band is too narrow to be split down to LM=-1 */
572 narrow = (m->eBands[i+1]-m->eBands[i])==1;
573 for (j=0;j<N;j++)
574 tmp[j] = X[tf_chan*N0 + j+(m->eBands[i]<<LM)];
575 /* Just add the right channel if we're in stereo */
576 /*if (C==2)
577 for (j=0;j<N;j++)
578 tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));*/
579 L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias);
580 best_L1 = L1;
581 /* Check the -1 case for transients */
582 if (isTransient && !narrow)
583 {
584 for (j=0;j<N;j++)
585 tmp_1[j] = tmp[j];
586 haar1(tmp_1, N>>LM, 1<<LM);
587 L1 = l1_metric(tmp_1, N, LM+1, bias);
588 if (L1<best_L1)
589 {
590 best_L1 = L1;
591 best_level = -1;
592 }
593 }
594 /*printf ("%f ", L1);*/
595 for (k=0;k<LM+!(isTransient||narrow);k++)
596 {
597 int B;
598
599 if (isTransient)
600 B = (LM-k-1);
601 else
602 B = k+1;
603
604 haar1(tmp, N>>k, 1<<k);
605
606 L1 = l1_metric(tmp, N, B, bias);
607
608 if (L1 < best_L1)
609 {
610 best_L1 = L1;
611 best_level = k+1;
612 }
613 }
614 /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
615 /* metric is in Q1 to be able to select the mid-point (-0.5) for narrower bands */
616 if (isTransient)
617 metric[i] = 2*best_level;
618 else
619 metric[i] = -2*best_level;
620 *tf_sum += (isTransient ? LM : 0) - metric[i]/2;
621 /* For bands that can't be split to -1, set the metric to the half-way point to avoid
622 biasing the decision */
623 if (narrow && (metric[i]==0 || metric[i]==-2*LM))
624 metric[i]-=1;
625 /*printf("%d ", metric[i]);*/
626 }
627 /*printf("\n");*/
628 /* Search for the optimal tf resolution, including tf_select */
629 tf_select = 0;
630 for (sel=0;sel<2;sel++)
631 {
632 cost0 = 0;
633 cost1 = isTransient ? 0 : lambda;
634 for (i=1;i<len;i++)
635 {
636 int curr0, curr1;
637 curr0 = IMIN(cost0, cost1 + lambda);
638 curr1 = IMIN(cost0 + lambda, cost1);
639 cost0 = curr0 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
640 cost1 = curr1 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+1]);
641 }
642 cost0 = IMIN(cost0, cost1);
643 selcost[sel]=cost0;
644 }
645 /* For now, we're conservative and only allow tf_select=1 for transients.
646 * If tests confirm it's useful for non-transients, we could allow it. */
647 if (selcost[1]<selcost[0] && isTransient)
648 tf_select=1;
649 cost0 = 0;
650 cost1 = isTransient ? 0 : lambda;
651 /* Viterbi forward pass */
652 for (i=1;i<len;i++)
653 {
654 int curr0, curr1;
655 int from0, from1;
656
657 from0 = cost0;
658 from1 = cost1 + lambda;
659 if (from0 < from1)
660 {
661 curr0 = from0;
662 path0[i]= 0;
663 } else {
664 curr0 = from1;
665 path0[i]= 1;
666 }
667
668 from0 = cost0 + lambda;
669 from1 = cost1;
670 if (from0 < from1)
671 {
672 curr1 = from0;
673 path1[i]= 0;
674 } else {
675 curr1 = from1;
676 path1[i]= 1;
677 }
678 cost0 = curr0 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
679 cost1 = curr1 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]);
680 }
681 tf_res[len-1] = cost0 < cost1 ? 0 : 1;
682 /* Viterbi backward pass to check the decisions */
683 for (i=len-2;i>=0;i--)
684 {
685 if (tf_res[i+1] == 1)
686 tf_res[i] = path1[i+1];
687 else
688 tf_res[i] = path0[i+1];
689 }
690 /*printf("%d %f\n", *tf_sum, tf_estimate);*/
691 RESTORE_STACK;
692#ifdef FUZZING
693 tf_select = rand()&0x1;
694 tf_res[0] = rand()&0x1;
695 for (i=1;i<len;i++)
696 tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
697#endif
698 return tf_select;
699}
700
701static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
702{
703 int curr, i;
704 int tf_select_rsv;
705 int tf_changed;
706 int logp;
707 opus_uint32 budget;
708 opus_uint32 tell;
709 budget = enc->storage*8;
710 tell = ec_tell(enc);
711 logp = isTransient ? 2 : 4;
712 /* Reserve space to code the tf_select decision. */
713 tf_select_rsv = LM>0 && tell+logp+1 <= budget;
714 budget -= tf_select_rsv;
715 curr = tf_changed = 0;
716 for (i=start;i<end;i++)
717 {
718 if (tell+logp<=budget)
719 {
720 ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
721 tell = ec_tell(enc);
722 curr = tf_res[i];
723 tf_changed |= curr;
724 }
725 else
726 tf_res[i] = curr;
727 logp = isTransient ? 4 : 5;
728 }
729 /* Only code tf_select if it would actually make a difference. */
730 if (tf_select_rsv &&
731 tf_select_table[LM][4*isTransient+0+tf_changed]!=
732 tf_select_table[LM][4*isTransient+2+tf_changed])
733 ec_enc_bit_logp(enc, tf_select, 1);
734 else
735 tf_select = 0;
736 for (i=start;i<end;i++)
737 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
738 /*for(i=0;i<end;i++)printf("%d ", isTransient ? tf_res[i] : LM+tf_res[i]);printf("\n");*/
739}
740
741
742static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
743 const opus_val16 *bandLogE, int end, int LM, int C, int N0,
744 AnalysisInfo *analysis, opus_val16 *stereo_saving, opus_val16 tf_estimate,
Jean-Marc Valin0f686962013-09-05 12:49:55 -0400745 int intensity, opus_val16 surround_trim)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500746{
747 int i;
748 opus_val32 diff=0;
749 int c;
750 int trim_index = 5;
751 opus_val16 trim = QCONST16(5.f, 8);
752 opus_val16 logXC, logXC2;
753 if (C==2)
754 {
755 opus_val16 sum = 0; /* Q10 */
756 opus_val16 minXC; /* Q10 */
757 /* Compute inter-channel correlation for low frequencies */
758 for (i=0;i<8;i++)
759 {
760 int j;
761 opus_val32 partial = 0;
762 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
763 partial = MAC16_16(partial, X[j], X[N0+j]);
764 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
765 }
766 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
767 sum = MIN16(QCONST16(1.f, 10), ABS16(sum));
768 minXC = sum;
769 for (i=8;i<intensity;i++)
770 {
771 int j;
772 opus_val32 partial = 0;
773 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
774 partial = MAC16_16(partial, X[j], X[N0+j]);
775 minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18))));
776 }
777 minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC));
778 /*printf ("%f\n", sum);*/
779 if (sum > QCONST16(.995f,10))
780 trim_index-=4;
781 else if (sum > QCONST16(.92f,10))
782 trim_index-=3;
783 else if (sum > QCONST16(.85f,10))
784 trim_index-=2;
785 else if (sum > QCONST16(.8f,10))
786 trim_index-=1;
787 /* mid-side savings estimations based on the LF average*/
788 logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum));
789 /* mid-side savings estimations based on min correlation */
790 logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(minXC, minXC)));
791#ifdef FIXED_POINT
792 /* Compensate for Q20 vs Q14 input and convert output to Q8 */
793 logXC = PSHR32(logXC-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8);
794 logXC2 = PSHR32(logXC2-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8);
795#endif
796
797 trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC));
798 *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2));
799 }
800
801 /* Estimate spectral tilt */
802 c=0; do {
803 for (i=0;i<end-1;i++)
804 {
805 diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-end);
806 }
807 } while (++c<C);
808 diff /= C*(end-1);
809 /*printf("%f\n", diff);*/
810 if (diff > QCONST16(2.f, DB_SHIFT))
811 trim_index--;
812 if (diff > QCONST16(8.f, DB_SHIFT))
813 trim_index--;
814 if (diff < -QCONST16(4.f, DB_SHIFT))
815 trim_index++;
816 if (diff < -QCONST16(10.f, DB_SHIFT))
817 trim_index++;
818 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), SHR16(diff+QCONST16(1.f, DB_SHIFT),DB_SHIFT-8)/6 ));
Jean-Marc Valin0f686962013-09-05 12:49:55 -0400819 trim -= SHR16(surround_trim, DB_SHIFT-8);
Jean-Marc Valindae16fb2012-12-13 21:40:58 -0500820 trim -= 2*SHR16(tf_estimate, 14-8);
Jean-Marc Valin3ab03e02013-09-06 16:00:39 -0400821#ifndef DISABLE_FLOAT_API
Jean-Marc Valin69062102012-11-08 09:42:27 -0500822 if (analysis->valid)
823 {
Jean-Marc Valina71c9ad2013-11-13 12:07:01 -0500824 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8),
825 (opus_val16)(QCONST16(2.f, 8)*(analysis->tonality_slope+.05f))));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500826 }
827#endif
828
829#ifdef FIXED_POINT
830 trim_index = PSHR32(trim, 8);
831#else
Jean-Marc Valind683c762012-12-21 16:17:38 -0500832 trim_index = (int)floor(.5f+trim);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500833#endif
834 if (trim_index<0)
835 trim_index = 0;
836 if (trim_index>10)
837 trim_index = 10;
838 /*printf("%d\n", trim_index);*/
839#ifdef FUZZING
840 trim_index = rand()%11;
841#endif
842 return trim_index;
843}
844
845static int stereo_analysis(const CELTMode *m, const celt_norm *X,
846 int LM, int N0)
847{
848 int i;
849 int thetas;
850 opus_val32 sumLR = EPSILON, sumMS = EPSILON;
851
852 /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
853 for (i=0;i<13;i++)
854 {
855 int j;
856 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
857 {
858 opus_val32 L, R, M, S;
859 /* We cast to 32-bit first because of the -32768 case */
860 L = EXTEND32(X[j]);
861 R = EXTEND32(X[N0+j]);
862 M = ADD32(L, R);
863 S = SUB32(L, R);
864 sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
865 sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
866 }
867 }
868 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
869 thetas = 13;
870 /* We don't need thetas for lower bands with LM<=1 */
871 if (LM<=1)
872 thetas -= 8;
873 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
874 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
875}
876
Jean-Marc Valind683c762012-12-21 16:17:38 -0500877static opus_val16 dynalloc_analysis(const opus_val16 *bandLogE, const opus_val16 *bandLogE2,
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500878 int nbEBands, int start, int end, int C, int *offsets, int lsb_depth, const opus_int16 *logN,
879 int isTransient, int vbr, int constrained_vbr, const opus_int16 *eBands, int LM,
Jean-Marc Valin0f686962013-09-05 12:49:55 -0400880 int effectiveBytes, opus_int32 *tot_boost_, int lfe, opus_val16 *surround_dynalloc)
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500881{
882 int i, c;
883 opus_int32 tot_boost=0;
884 opus_val16 maxDepth;
885 VARDECL(opus_val16, follower);
886 VARDECL(opus_val16, noise_floor);
887 SAVE_STACK;
888 ALLOC(follower, C*nbEBands, opus_val16);
889 ALLOC(noise_floor, C*nbEBands, opus_val16);
890 for (i=0;i<nbEBands;i++)
891 offsets[i] = 0;
892 /* Dynamic allocation code */
Jean-Marc Valin82594922013-07-25 13:52:42 -0400893 maxDepth=-QCONST16(31.9f, DB_SHIFT);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500894 for (i=0;i<end;i++)
895 {
896 /* Noise floor must take into account eMeans, the depth, the width of the bands
897 and the preemphasis filter (approx. square of bark band ID) */
898 noise_floor[i] = MULT16_16(QCONST16(0.0625f, DB_SHIFT),logN[i])
899 +QCONST16(.5f,DB_SHIFT)+SHL16(9-lsb_depth,DB_SHIFT)-SHL16(eMeans[i],6)
900 +MULT16_16(QCONST16(.0062,DB_SHIFT),(i+5)*(i+5));
901 }
902 c=0;do
903 {
904 for (i=0;i<end;i++)
905 maxDepth = MAX16(maxDepth, bandLogE[c*nbEBands+i]-noise_floor[i]);
906 } while (++c<C);
907 /* Make sure that dynamic allocation can't make us bust the budget */
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -0400908 if (effectiveBytes > 50 && LM>=1 && !lfe)
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500909 {
910 int last=0;
911 c=0;do
912 {
913 follower[c*nbEBands] = bandLogE2[c*nbEBands];
914 for (i=1;i<end;i++)
915 {
916 /* The last band to be at least 3 dB higher than the previous one
917 is the last we'll consider. Otherwise, we run into problems on
918 bandlimited signals. */
919 if (bandLogE2[c*nbEBands+i] > bandLogE2[c*nbEBands+i-1]+QCONST16(.5f,DB_SHIFT))
920 last=i;
921 follower[c*nbEBands+i] = MIN16(follower[c*nbEBands+i-1]+QCONST16(1.5f,DB_SHIFT), bandLogE2[c*nbEBands+i]);
922 }
923 for (i=last-1;i>=0;i--)
924 follower[c*nbEBands+i] = MIN16(follower[c*nbEBands+i], MIN16(follower[c*nbEBands+i+1]+QCONST16(2.f,DB_SHIFT), bandLogE2[c*nbEBands+i]));
925 for (i=0;i<end;i++)
926 follower[c*nbEBands+i] = MAX16(follower[c*nbEBands+i], noise_floor[i]);
927 } while (++c<C);
928 if (C==2)
929 {
930 for (i=start;i<end;i++)
931 {
932 /* Consider 24 dB "cross-talk" */
933 follower[nbEBands+i] = MAX16(follower[nbEBands+i], follower[ i]-QCONST16(4.f,DB_SHIFT));
934 follower[ i] = MAX16(follower[ i], follower[nbEBands+i]-QCONST16(4.f,DB_SHIFT));
935 follower[i] = HALF16(MAX16(0, bandLogE[i]-follower[i]) + MAX16(0, bandLogE[nbEBands+i]-follower[nbEBands+i]));
936 }
937 } else {
938 for (i=start;i<end;i++)
939 {
940 follower[i] = MAX16(0, bandLogE[i]-follower[i]);
941 }
942 }
Jean-Marc Valin0f686962013-09-05 12:49:55 -0400943 for (i=start;i<end;i++)
944 follower[i] = MAX16(follower[i], surround_dynalloc[i]);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500945 /* For non-transient CBR/CVBR frames, halve the dynalloc contribution */
946 if ((!vbr || constrained_vbr)&&!isTransient)
947 {
948 for (i=start;i<end;i++)
949 follower[i] = HALF16(follower[i]);
950 }
951 for (i=start;i<end;i++)
952 {
953 int width;
954 int boost;
955 int boost_bits;
956
957 if (i<8)
958 follower[i] *= 2;
959 if (i>=12)
960 follower[i] = HALF16(follower[i]);
961 follower[i] = MIN16(follower[i], QCONST16(4, DB_SHIFT));
962
963 width = C*(eBands[i+1]-eBands[i])<<LM;
964 if (width<6)
965 {
Jean-Marc Valind683c762012-12-21 16:17:38 -0500966 boost = (int)SHR32(EXTEND32(follower[i]),DB_SHIFT);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500967 boost_bits = boost*width<<BITRES;
968 } else if (width > 48) {
Jean-Marc Valind683c762012-12-21 16:17:38 -0500969 boost = (int)SHR32(EXTEND32(follower[i])*8,DB_SHIFT);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500970 boost_bits = (boost*width<<BITRES)/8;
971 } else {
Jean-Marc Valind683c762012-12-21 16:17:38 -0500972 boost = (int)SHR32(EXTEND32(follower[i])*width/6,DB_SHIFT);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500973 boost_bits = boost*6<<BITRES;
974 }
975 /* For CBR and non-transient CVBR frames, limit dynalloc to 1/4 of the bits */
976 if ((!vbr || (constrained_vbr&&!isTransient))
977 && (tot_boost+boost_bits)>>BITRES>>3 > effectiveBytes/4)
978 {
Jean-Marc Valine4550922013-07-30 05:12:46 -0400979 opus_int32 cap = ((effectiveBytes/4)<<BITRES<<3);
980 offsets[i] = cap-tot_boost;
981 tot_boost = cap;
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500982 break;
983 } else {
984 offsets[i] = boost;
985 tot_boost += boost_bits;
986 }
987 }
988 }
989 *tot_boost_ = tot_boost;
990 RESTORE_STACK;
991 return maxDepth;
992}
993
994
Jean-Marc Valin69062102012-11-08 09:42:27 -0500995static int run_prefilter(CELTEncoder *st, celt_sig *in, celt_sig *prefilter_mem, int CC, int N,
996 int prefilter_tapset, int *pitch, opus_val16 *gain, int *qgain, int enabled, int nbAvailableBytes)
997{
998 int c;
999 VARDECL(celt_sig, _pre);
1000 celt_sig *pre[2];
1001 const CELTMode *mode;
1002 int pitch_index;
1003 opus_val16 gain1;
1004 opus_val16 pf_threshold;
1005 int pf_on;
1006 int qg;
1007 SAVE_STACK;
1008
1009 mode = st->mode;
1010 ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
1011
1012 pre[0] = _pre;
1013 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
1014
1015
1016 c=0; do {
1017 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
1018 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
1019 } while (++c<CC);
1020
1021 if (enabled)
1022 {
1023 VARDECL(opus_val16, pitch_buf);
1024 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
1025
1026 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC);
1027 /* Don't search for the fir last 1.5 octave of the range because
1028 there's too many false-positives due to short-term correlation */
1029 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
1030 COMBFILTER_MAXPERIOD-3*COMBFILTER_MINPERIOD, &pitch_index);
1031 pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
1032
1033 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
1034 N, &pitch_index, st->prefilter_period, st->prefilter_gain);
1035 if (pitch_index > COMBFILTER_MAXPERIOD-2)
1036 pitch_index = COMBFILTER_MAXPERIOD-2;
1037 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1038 /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.tonality);*/
1039 if (st->loss_rate>2)
1040 gain1 = HALF32(gain1);
1041 if (st->loss_rate>4)
1042 gain1 = HALF32(gain1);
1043 if (st->loss_rate>8)
1044 gain1 = 0;
1045 } else {
1046 gain1 = 0;
1047 pitch_index = COMBFILTER_MINPERIOD;
1048 }
1049
1050 /* Gain threshold for enabling the prefilter/postfilter */
1051 pf_threshold = QCONST16(.2f,15);
1052
1053 /* Adjusting the threshold based on rate and continuity */
1054 if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1055 pf_threshold += QCONST16(.2f,15);
1056 if (nbAvailableBytes<25)
1057 pf_threshold += QCONST16(.1f,15);
1058 if (nbAvailableBytes<35)
1059 pf_threshold += QCONST16(.1f,15);
1060 if (st->prefilter_gain > QCONST16(.4f,15))
1061 pf_threshold -= QCONST16(.1f,15);
1062 if (st->prefilter_gain > QCONST16(.55f,15))
1063 pf_threshold -= QCONST16(.1f,15);
1064
1065 /* Hard threshold at 0.2 */
1066 pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1067 if (gain1<pf_threshold)
1068 {
1069 gain1 = 0;
1070 pf_on = 0;
1071 qg = 0;
1072 } else {
1073 /*This block is not gated by a total bits check only because
1074 of the nbAvailableBytes check above.*/
1075 if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1076 gain1=st->prefilter_gain;
1077
1078#ifdef FIXED_POINT
1079 qg = ((gain1+1536)>>10)/3-1;
1080#else
1081 qg = (int)floor(.5f+gain1*32/3)-1;
1082#endif
1083 qg = IMAX(0, IMIN(7, qg));
1084 gain1 = QCONST16(0.09375f,15)*(qg+1);
1085 pf_on = 1;
1086 }
1087 /*printf("%d %f\n", pitch_index, gain1);*/
1088
1089 c=0; do {
1090 int offset = mode->shortMdctSize-st->overlap;
1091 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1092 OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
1093 if (offset)
1094 comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD,
1095 st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1096 st->prefilter_tapset, st->prefilter_tapset, NULL, 0);
1097
1098 comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
1099 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1100 st->prefilter_tapset, prefilter_tapset, mode->window, st->overlap);
1101 OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
1102
1103 if (N>COMBFILTER_MAXPERIOD)
1104 {
1105 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
1106 } else {
1107 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
1108 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
1109 }
1110 } while (++c<CC);
1111
1112 RESTORE_STACK;
1113 *gain = gain1;
1114 *pitch = pitch_index;
1115 *qgain = qg;
1116 return pf_on;
1117}
1118
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001119static int compute_vbr(const CELTMode *mode, AnalysisInfo *analysis, opus_int32 base_target,
1120 int LM, opus_int32 bitrate, int lastCodedBands, int C, int intensity,
1121 int constrained_vbr, opus_val16 stereo_saving, int tot_boost,
1122 opus_val16 tf_estimate, int pitch_change, opus_val16 maxDepth,
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001123 int variable_duration, int lfe, int has_surround_mask, opus_val16 surround_masking,
1124 opus_val16 temporal_vbr)
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001125{
1126 /* The target rate in 8th bits per frame */
1127 opus_int32 target;
1128 int coded_bins;
1129 int coded_bands;
Jean-Marc Valin39cbc452013-06-13 15:28:53 -04001130 opus_val16 tf_calibration;
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001131 int nbEBands;
1132 const opus_int16 *eBands;
1133
1134 nbEBands = mode->nbEBands;
1135 eBands = mode->eBands;
1136
1137 coded_bands = lastCodedBands ? lastCodedBands : nbEBands;
1138 coded_bins = eBands[coded_bands]<<LM;
1139 if (C==2)
1140 coded_bins += eBands[IMIN(intensity, coded_bands)]<<LM;
1141
1142 target = base_target;
1143
1144 /*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 -04001145#ifndef DISABLE_FLOAT_API
Gregory Maxwell5280c712013-07-15 15:51:24 -07001146 if (analysis->valid && analysis->activity<.4)
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001147 target -= (opus_int32)((coded_bins<<BITRES)*(.4f-analysis->activity));
1148#endif
1149 /* Stereo savings */
1150 if (C==2)
1151 {
1152 int coded_stereo_bands;
1153 int coded_stereo_dof;
1154 opus_val16 max_frac;
1155 coded_stereo_bands = IMIN(intensity, coded_bands);
1156 coded_stereo_dof = (eBands[coded_stereo_bands]<<LM)-coded_stereo_bands;
1157 /* Maximum fraction of the bits we can save if the signal is mono. */
1158 max_frac = DIV32_16(MULT16_16(QCONST16(0.8f, 15), coded_stereo_dof), coded_bins);
Jean-Marc Valin9c23f5c2013-10-28 14:15:18 -04001159 stereo_saving = MIN16(stereo_saving, QCONST16(1.f, 8));
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001160 /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/
1161 target -= (opus_int32)MIN32(MULT16_32_Q15(max_frac,target),
1162 SHR32(MULT16_16(stereo_saving-QCONST16(0.1f,8),(coded_stereo_dof<<BITRES)),8));
1163 }
1164 /* Boost the rate according to dynalloc (minus the dynalloc average for calibration). */
1165 target += tot_boost-(16<<LM);
1166 /* Apply transient boost, compensating for average boost. */
Jean-Marc Valin39cbc452013-06-13 15:28:53 -04001167 tf_calibration = variable_duration==OPUS_FRAMESIZE_VARIABLE ?
1168 QCONST16(0.02f,14) : QCONST16(0.04f,14);
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001169 target += (opus_int32)SHL32(MULT16_32_Q15(tf_estimate-tf_calibration, target),1);
1170
Jean-Marc Valin3ab03e02013-09-06 16:00:39 -04001171#ifndef DISABLE_FLOAT_API
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001172 /* Apply tonality boost */
Jean-Marc Valincd373b52013-07-07 02:51:07 -04001173 if (analysis->valid && !lfe)
1174 {
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001175 opus_int32 tonal_target;
1176 float tonal;
1177
1178 /* Tonality boost (compensating for the average). */
1179 tonal = MAX16(0.f,analysis->tonality-.15f)-0.09f;
1180 tonal_target = target + (opus_int32)((coded_bins<<BITRES)*1.2f*tonal);
1181 if (pitch_change)
1182 tonal_target += (opus_int32)((coded_bins<<BITRES)*.8f);
Jean-Marc Valin942fc812013-10-01 19:27:30 -04001183 /*printf("%f %f ", analysis->tonality, tonal);*/
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001184 target = tonal_target;
1185 }
1186#endif
1187
1188 if (has_surround_mask&&!lfe)
1189 {
Jean-Marc Valin16ba19a2013-06-27 03:40:44 -04001190 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 -04001191 /*printf("%f %d %d %d %d %d %d ", surround_masking, coded_bins, st->end, st->intensity, surround_target, target, st->bitrate);*/
1192 target = IMAX(target/4, surround_target);
1193 }
1194
1195 {
1196 opus_int32 floor_depth;
1197 int bins;
1198 bins = eBands[nbEBands-2]<<LM;
1199 /*floor_depth = SHR32(MULT16_16((C*bins<<BITRES),celt_log2(SHL32(MAX16(1,sample_max),13))), DB_SHIFT);*/
1200 floor_depth = (opus_int32)SHR32(MULT16_16((C*bins<<BITRES),maxDepth), DB_SHIFT);
1201 floor_depth = IMAX(floor_depth, target>>2);
1202 target = IMIN(target, floor_depth);
1203 /*printf("%f %d\n", maxDepth, floor_depth);*/
1204 }
1205
1206 if ((!has_surround_mask||lfe) && (constrained_vbr || bitrate<64000))
1207 {
1208 opus_val16 rate_factor;
1209#ifdef FIXED_POINT
1210 rate_factor = MAX16(0,(bitrate-32000));
1211#else
1212 rate_factor = MAX16(0,(1.f/32768)*(bitrate-32000));
1213#endif
1214 if (constrained_vbr)
1215 rate_factor = MIN16(rate_factor, QCONST16(0.67f, 15));
1216 target = base_target + (opus_int32)MULT16_32_Q15(rate_factor, target-base_target);
1217
1218 }
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001219
Jean-Marc Valincd373b52013-07-07 02:51:07 -04001220 if (!has_surround_mask && tf_estimate < QCONST16(.2f, 14))
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001221 {
1222 opus_val16 amount;
1223 opus_val16 tvbr_factor;
Jean-Marc Valin8e3a1cb2013-07-01 16:12:27 -04001224 amount = MULT16_16_Q15(QCONST16(.0000031f, 30), IMAX(0, IMIN(32000, 96000-bitrate)));
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001225 tvbr_factor = SHR32(MULT16_16(temporal_vbr, amount), DB_SHIFT);
1226 target += (opus_int32)MULT16_32_Q15(tvbr_factor, target);
1227 }
1228
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001229 /* Don't allow more than doubling the rate */
1230 target = IMIN(2*base_target, target);
1231
1232 return target;
1233}
1234
Jean-Marc Valin69062102012-11-08 09:42:27 -05001235int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
1236{
1237 int i, c, N;
1238 opus_int32 bits;
1239 ec_enc _enc;
1240 VARDECL(celt_sig, in);
1241 VARDECL(celt_sig, freq);
1242 VARDECL(celt_norm, X);
1243 VARDECL(celt_ener, bandE);
1244 VARDECL(opus_val16, bandLogE);
1245 VARDECL(opus_val16, bandLogE2);
1246 VARDECL(int, fine_quant);
1247 VARDECL(opus_val16, error);
1248 VARDECL(int, pulses);
1249 VARDECL(int, cap);
1250 VARDECL(int, offsets);
1251 VARDECL(int, fine_priority);
1252 VARDECL(int, tf_res);
1253 VARDECL(unsigned char, collapse_masks);
1254 celt_sig *prefilter_mem;
1255 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
1256 int shortBlocks=0;
1257 int isTransient=0;
1258 const int CC = st->channels;
1259 const int C = st->stream_channels;
1260 int LM, M;
1261 int tf_select;
1262 int nbFilledBytes, nbAvailableBytes;
1263 int effEnd;
1264 int codedBands;
1265 int tf_sum;
1266 int alloc_trim;
1267 int pitch_index=COMBFILTER_MINPERIOD;
1268 opus_val16 gain1 = 0;
1269 int dual_stereo=0;
1270 int effectiveBytes;
1271 int dynalloc_logp;
1272 opus_int32 vbr_rate;
1273 opus_int32 total_bits;
1274 opus_int32 total_boost;
1275 opus_int32 balance;
1276 opus_int32 tell;
1277 int prefilter_tapset=0;
1278 int pf_on;
1279 int anti_collapse_rsv;
1280 int anti_collapse_on=0;
1281 int silence=0;
1282 int tf_chan = 0;
1283 opus_val16 tf_estimate;
1284 int pitch_change=0;
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001285 opus_int32 tot_boost;
Jean-Marc Valinb7bd4c22013-05-18 23:33:48 -04001286 opus_val32 sample_max;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001287 opus_val16 maxDepth;
1288 const OpusCustomMode *mode;
1289 int nbEBands;
1290 int overlap;
1291 const opus_int16 *eBands;
1292 int secondMdct;
Jean-Marc Valin5fb50ad2012-12-21 01:23:26 -05001293 int signalBandwidth;
Jean-Marc Valinf77410d2013-04-19 22:44:03 -04001294 int transient_got_disabled=0;
Jean-Marc Valina4dccd32013-05-04 23:54:20 -04001295 opus_val16 surround_masking=0;
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001296 opus_val16 temporal_vbr=0;
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001297 opus_val16 surround_trim = 0;
Jean-Marc Valinc1959e72013-11-08 19:24:10 -05001298 opus_int32 equiv_rate = 510000;
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001299 VARDECL(opus_val16, surround_dynalloc);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001300 ALLOC_STACK;
1301
1302 mode = st->mode;
1303 nbEBands = mode->nbEBands;
1304 overlap = mode->overlap;
1305 eBands = mode->eBands;
Jean-Marc Valindae16fb2012-12-13 21:40:58 -05001306 tf_estimate = 0;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001307 if (nbCompressedBytes<2 || pcm==NULL)
1308 return OPUS_BAD_ARG;
1309
1310 frame_size *= st->upsample;
1311 for (LM=0;LM<=mode->maxLM;LM++)
1312 if (mode->shortMdctSize<<LM==frame_size)
1313 break;
1314 if (LM>mode->maxLM)
1315 return OPUS_BAD_ARG;
1316 M=1<<LM;
1317 N = M*mode->shortMdctSize;
1318
1319 prefilter_mem = st->in_mem+CC*(st->overlap);
1320 oldBandE = (opus_val16*)(st->in_mem+CC*(st->overlap+COMBFILTER_MAXPERIOD));
1321 oldLogE = oldBandE + CC*nbEBands;
1322 oldLogE2 = oldLogE + CC*nbEBands;
1323
1324 if (enc==NULL)
1325 {
1326 tell=1;
1327 nbFilledBytes=0;
1328 } else {
1329 tell=ec_tell(enc);
1330 nbFilledBytes=(tell+4)>>3;
1331 }
1332
1333#ifdef CUSTOM_MODES
1334 if (st->signalling && enc==NULL)
1335 {
1336 int tmp = (mode->effEBands-st->end)>>1;
1337 st->end = IMAX(1, mode->effEBands-tmp);
1338 compressed[0] = tmp<<5;
1339 compressed[0] |= LM<<3;
1340 compressed[0] |= (C==2)<<2;
1341 /* Convert "standard mode" to Opus header */
1342 if (mode->Fs==48000 && mode->shortMdctSize==120)
1343 {
1344 int c0 = toOpus(compressed[0]);
1345 if (c0<0)
1346 return OPUS_BAD_ARG;
1347 compressed[0] = c0;
1348 }
1349 compressed++;
1350 nbCompressedBytes--;
1351 }
1352#else
1353 celt_assert(st->signalling==0);
1354#endif
1355
1356 /* Can't produce more than 1275 output bytes */
1357 nbCompressedBytes = IMIN(nbCompressedBytes,1275);
1358 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1359
1360 if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1361 {
1362 opus_int32 den=mode->Fs>>BITRES;
1363 vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1364#ifdef CUSTOM_MODES
1365 if (st->signalling)
1366 vbr_rate -= 8<<BITRES;
1367#endif
1368 effectiveBytes = vbr_rate>>(3+BITRES);
1369 } else {
1370 opus_int32 tmp;
1371 vbr_rate = 0;
1372 tmp = st->bitrate*frame_size;
1373 if (tell>1)
1374 tmp += tell;
1375 if (st->bitrate!=OPUS_BITRATE_MAX)
1376 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1377 (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling));
1378 effectiveBytes = nbCompressedBytes;
1379 }
Jean-Marc Valinc1959e72013-11-08 19:24:10 -05001380 if (st->bitrate != OPUS_BITRATE_MAX)
1381 equiv_rate = st->bitrate - (40*C+20)*((400>>LM) - 50);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001382
1383 if (enc==NULL)
1384 {
1385 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1386 enc = &_enc;
1387 }
1388
1389 if (vbr_rate>0)
1390 {
1391 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1392 target rate and buffering.
1393 We must do this up front so that bust-prevention logic triggers
1394 correctly if we don't have enough bits. */
1395 if (st->constrained_vbr)
1396 {
1397 opus_int32 vbr_bound;
1398 opus_int32 max_allowed;
1399 /* We could use any multiple of vbr_rate as bound (depending on the
1400 delay).
1401 This is clamped to ensure we use at least two bytes if the encoder
1402 was entirely empty, but to allow 0 in hybrid mode. */
1403 vbr_bound = vbr_rate;
1404 max_allowed = IMIN(IMAX(tell==1?2:0,
1405 (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1406 nbAvailableBytes);
1407 if(max_allowed < nbAvailableBytes)
1408 {
1409 nbCompressedBytes = nbFilledBytes+max_allowed;
1410 nbAvailableBytes = max_allowed;
1411 ec_enc_shrink(enc, nbCompressedBytes);
1412 }
1413 }
1414 }
1415 total_bits = nbCompressedBytes*8;
1416
1417 effEnd = st->end;
1418 if (effEnd > mode->effEBands)
1419 effEnd = mode->effEBands;
1420
1421 ALLOC(in, CC*(N+st->overlap), celt_sig);
1422
Jean-Marc Valinb7bd4c22013-05-18 23:33:48 -04001423 sample_max=MAX32(st->overlap_max, celt_maxabs16(pcm, C*(N-overlap)/st->upsample));
Jean-Marc Valin69062102012-11-08 09:42:27 -05001424 st->overlap_max=celt_maxabs16(pcm+C*(N-overlap)/st->upsample, C*overlap/st->upsample);
Jean-Marc Valinb7bd4c22013-05-18 23:33:48 -04001425 sample_max=MAX32(sample_max, st->overlap_max);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001426#ifdef FIXED_POINT
1427 silence = (sample_max==0);
1428#else
1429 silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth));
1430#endif
1431#ifdef FUZZING
1432 if ((rand()&0x3F)==0)
1433 silence = 1;
1434#endif
1435 if (tell==1)
1436 ec_enc_bit_logp(enc, silence, 15);
1437 else
1438 silence=0;
1439 if (silence)
1440 {
1441 /*In VBR mode there is no need to send more than the minimum. */
1442 if (vbr_rate>0)
1443 {
1444 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1445 total_bits=nbCompressedBytes*8;
1446 nbAvailableBytes=2;
1447 ec_enc_shrink(enc, nbCompressedBytes);
1448 }
1449 /* Pretend we've filled all the remaining bits with zeros
1450 (that's what the initialiser did anyway) */
1451 tell = nbCompressedBytes*8;
1452 enc->nbits_total+=tell-ec_tell(enc);
1453 }
1454 c=0; do {
1455 preemphasis(pcm+c, in+c*(N+st->overlap)+st->overlap, N, CC, st->upsample,
1456 mode->preemph, st->preemph_memE+c, st->clip);
1457 } while (++c<CC);
1458
1459
1460
1461 /* Find pitch period and gain */
1462 {
1463 int enabled;
1464 int qg;
Jean-Marc Valin0d584b92013-11-13 22:42:04 -05001465 enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && st->start==0 && !silence && !st->disable_pf
Jean-Marc Valin39cbc452013-06-13 15:28:53 -04001466 && st->complexity >= 5 && !(st->consec_transient && LM!=3 && st->variable_duration==OPUS_FRAMESIZE_VARIABLE);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001467
1468 prefilter_tapset = st->tapset_decision;
1469 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 -07001470 if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && (!st->analysis.valid || st->analysis.tonality > .3)
1471 && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st->prefilter_period))
Jean-Marc Valin69062102012-11-08 09:42:27 -05001472 pitch_change = 1;
1473 if (pf_on==0)
1474 {
1475 if(st->start==0 && tell+16<=total_bits)
1476 ec_enc_bit_logp(enc, 0, 1);
1477 } else {
1478 /*This block is not gated by a total bits check only because
1479 of the nbAvailableBytes check above.*/
1480 int octave;
1481 ec_enc_bit_logp(enc, 1, 1);
1482 pitch_index += 1;
1483 octave = EC_ILOG(pitch_index)-5;
1484 ec_enc_uint(enc, octave, 6);
1485 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1486 pitch_index -= 1;
1487 ec_enc_bits(enc, qg, 3);
1488 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1489 }
1490 }
1491
1492 isTransient = 0;
1493 shortBlocks = 0;
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -04001494 if (st->complexity >= 1 && !st->lfe)
Jean-Marc Valin2a5f0562012-11-19 23:17:06 -05001495 {
1496 isTransient = transient_analysis(in, N+st->overlap, CC,
1497 &tf_estimate, &tf_chan);
1498 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05001499 if (LM>0 && ec_tell(enc)+3<=total_bits)
1500 {
Jean-Marc Valin2a5f0562012-11-19 23:17:06 -05001501 if (isTransient)
1502 shortBlocks = M;
Jean-Marc Valin2a5f0562012-11-19 23:17:06 -05001503 } else {
1504 isTransient = 0;
Jean-Marc Valin3252bf22013-04-19 03:14:28 -04001505 transient_got_disabled=1;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001506 }
1507
1508 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
1509 ALLOC(bandE,nbEBands*CC, celt_ener);
1510 ALLOC(bandLogE,nbEBands*CC, opus_val16);
1511
1512 secondMdct = shortBlocks && st->complexity>=8;
1513 ALLOC(bandLogE2, C*nbEBands, opus_val16);
1514 if (secondMdct)
1515 {
Jean-Marc Valin851f8032013-02-18 01:43:43 -05001516 compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001517 compute_band_energies(mode, freq, bandE, effEnd, C, M);
1518 amp2Log2(mode, effEnd, st->end, bandE, bandLogE2, C);
1519 for (i=0;i<C*nbEBands;i++)
1520 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT));
1521 }
1522
Jean-Marc Valin851f8032013-02-18 01:43:43 -05001523 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample);
Jean-Marc Valin69c3dcd2013-02-19 03:42:18 -05001524 if (CC==2&&C==1)
1525 tf_chan = 0;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001526 compute_band_energies(mode, freq, bandE, effEnd, C, M);
1527
Jean-Marc Valin172f66a2013-04-27 02:29:52 -04001528 if (st->lfe)
1529 {
1530 for (i=2;i<st->end;i++)
Jean-Marc Valin43654982013-07-07 00:39:35 -04001531 {
Jean-Marc Valin172f66a2013-04-27 02:29:52 -04001532 bandE[i] = IMIN(bandE[i], MULT16_32_Q15(QCONST16(1e-4f,15),bandE[0]));
Jean-Marc Valin43654982013-07-07 00:39:35 -04001533 bandE[i] = MAX32(bandE[i], EPSILON);
1534 }
Jean-Marc Valin172f66a2013-04-27 02:29:52 -04001535 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05001536 amp2Log2(mode, effEnd, st->end, bandE, bandLogE, C);
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001537
1538 ALLOC(surround_dynalloc, C*nbEBands, opus_val16);
1539 for(i=0;i<st->end;i++)
1540 surround_dynalloc[i] = 0;
Jean-Marc Valind2c484b2013-05-06 16:02:31 -04001541 /* This computes how much masking takes place between surround channels */
Jean-Marc Valin039e9ab2013-09-12 03:05:43 -04001542 if (st->start==0&&st->energy_mask&&!st->lfe)
Jean-Marc Valina4dccd32013-05-04 23:54:20 -04001543 {
Jean-Marc Valin039e9ab2013-09-12 03:05:43 -04001544 int mask_end;
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001545 int midband;
1546 int count_dynalloc;
Jean-Marc Valina4dccd32013-05-04 23:54:20 -04001547 opus_val32 mask_avg=0;
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001548 opus_val32 diff=0;
Jean-Marc Valin039e9ab2013-09-12 03:05:43 -04001549 int count=0;
Jean-Marc Valin942fc812013-10-01 19:27:30 -04001550 mask_end = IMAX(2,st->lastCodedBands);
Jean-Marc Valina4dccd32013-05-04 23:54:20 -04001551 for (c=0;c<C;c++)
Jean-Marc Valind2c484b2013-05-06 16:02:31 -04001552 {
Jean-Marc Valin039e9ab2013-09-12 03:05:43 -04001553 for(i=0;i<mask_end;i++)
Jean-Marc Valind2c484b2013-05-06 16:02:31 -04001554 {
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001555 opus_val16 mask;
1556 mask = MAX16(MIN16(st->energy_mask[nbEBands*c+i],
1557 QCONST16(.25f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT));
1558 if (mask > 0)
1559 mask = HALF16(mask);
1560 mask_avg += MULT16_16(mask, eBands[i+1]-eBands[i]);
1561 count += eBands[i+1]-eBands[i];
1562 diff += MULT16_16(mask, 1+2*i-mask_end);
Jean-Marc Valind2c484b2013-05-06 16:02:31 -04001563 }
1564 }
Jean-Marc Valin039e9ab2013-09-12 03:05:43 -04001565 mask_avg = DIV32_16(mask_avg,count);
Jean-Marc Valin36a21ed2013-09-14 15:46:09 -04001566 mask_avg += QCONST16(.2f, DB_SHIFT);
Jean-Marc Valin039e9ab2013-09-12 03:05:43 -04001567 diff = diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end);
1568 /* Again, being conservative */
1569 diff = HALF32(diff);
1570 diff = MAX32(MIN32(diff, QCONST32(.031f, DB_SHIFT)), -QCONST32(.031f, DB_SHIFT));
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001571 /* Find the band that's in the middle of the coded spectrum */
1572 for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++);
1573 count_dynalloc=0;
Jean-Marc Valin039e9ab2013-09-12 03:05:43 -04001574 for(i=0;i<mask_end;i++)
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001575 {
1576 opus_val32 lin;
1577 opus_val16 unmask;
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001578 lin = mask_avg + diff*(i-midband);
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001579 if (C==2)
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001580 unmask = MAX16(st->energy_mask[i], st->energy_mask[nbEBands+i]);
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001581 else
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001582 unmask = st->energy_mask[i];
1583 unmask = MIN16(unmask, QCONST16(.0f, DB_SHIFT));
1584 unmask -= lin;
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001585 if (unmask > QCONST16(.25f, DB_SHIFT))
1586 {
1587 surround_dynalloc[i] = unmask - QCONST16(.25f, DB_SHIFT);
Jean-Marc Valinae7dc8a2013-09-13 15:05:50 -04001588 count_dynalloc++;
1589 }
1590 }
1591 if (count_dynalloc>=3)
1592 {
1593 /* If we need dynalloc in many bands, it's probably because our
1594 initial masking rate was too low. */
1595 mask_avg += QCONST16(.25f, DB_SHIFT);
1596 if (mask_avg>0)
1597 {
1598 /* Something went really wrong in the original calculations,
1599 disabling masking. */
1600 mask_avg = 0;
1601 diff = 0;
1602 for(i=0;i<mask_end;i++)
1603 surround_dynalloc[i] = 0;
1604 } else {
1605 for(i=0;i<mask_end;i++)
1606 surround_dynalloc[i] = MAX16(0, surround_dynalloc[i]-QCONST16(.25f, DB_SHIFT));
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001607 }
1608 }
Jean-Marc Valin36a21ed2013-09-14 15:46:09 -04001609 mask_avg += QCONST16(.2f, DB_SHIFT);
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001610 /* Convert to 1/64th units used for the trim */
1611 surround_trim = 64*diff;
1612 /*printf("%d %d ", mask_avg, surround_trim);*/
1613 surround_masking = mask_avg;
Jean-Marc Valina4dccd32013-05-04 23:54:20 -04001614 }
Jean-Marc Valin92a06f52013-07-01 16:27:07 -04001615 /* Temporal VBR (but not for LFE) */
1616 if (!st->lfe)
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001617 {
1618 opus_val16 follow=-QCONST16(10.0f,DB_SHIFT);
Jean-Marc Valin8f466272013-10-28 21:50:10 -04001619 opus_val32 frame_avg=0;
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001620 opus_val16 offset = shortBlocks?HALF16(SHL16(LM, DB_SHIFT)):0;
1621 for(i=st->start;i<st->end;i++)
1622 {
1623 follow = MAX16(follow-QCONST16(1.f, DB_SHIFT), bandLogE[i]-offset);
1624 if (C==2)
1625 follow = MAX16(follow, bandLogE[i+nbEBands]-offset);
1626 frame_avg += follow;
1627 }
1628 frame_avg /= (st->end-st->start);
1629 temporal_vbr = SUB16(frame_avg,st->spec_avg);
1630 temporal_vbr = MIN16(QCONST16(3.f, DB_SHIFT), MAX16(-QCONST16(1.5f, DB_SHIFT), temporal_vbr));
1631 st->spec_avg += MULT16_16_Q15(QCONST16(.02f, 15), temporal_vbr);
1632 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05001633 /*for (i=0;i<21;i++)
1634 printf("%f ", bandLogE[i]);
1635 printf("\n");*/
1636
1637 if (!secondMdct)
1638 {
1639 for (i=0;i<C*nbEBands;i++)
1640 bandLogE2[i] = bandLogE[i];
1641 }
1642
Jean-Marc Valin541a4722013-02-17 21:21:30 -05001643 /* Last chance to catch any transient we might have missed in the
1644 time-domain analysis */
Jean-Marc Valin172f66a2013-04-27 02:29:52 -04001645 if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5 && !st->lfe)
Jean-Marc Valin541a4722013-02-17 21:21:30 -05001646 {
1647 if (patch_transient_decision(bandLogE, oldBandE, nbEBands, st->end, C))
1648 {
1649 isTransient = 1;
1650 shortBlocks = M;
Jean-Marc Valin851f8032013-02-18 01:43:43 -05001651 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample);
Jean-Marc Valin541a4722013-02-17 21:21:30 -05001652 compute_band_energies(mode, freq, bandE, effEnd, C, M);
1653 amp2Log2(mode, effEnd, st->end, bandE, bandLogE, C);
1654 /* Compensate for the scaling of short vs long mdcts */
1655 for (i=0;i<C*nbEBands;i++)
1656 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT));
Jean-Marc Valin16ba19a2013-06-27 03:40:44 -04001657 tf_estimate = QCONST16(.2f,14);
Jean-Marc Valin541a4722013-02-17 21:21:30 -05001658 }
1659 }
1660
1661 if (LM>0 && ec_tell(enc)+3<=total_bits)
1662 ec_enc_bit_logp(enc, isTransient, 3);
1663
Jean-Marc Valin69062102012-11-08 09:42:27 -05001664 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1665
1666 /* Band normalisation */
1667 normalise_bands(mode, freq, X, bandE, effEnd, C, M);
1668
1669 ALLOC(tf_res, nbEBands, int);
Jean-Marc Valina6d663c2012-11-08 13:26:49 -05001670 /* Disable variable tf resolution for hybrid and at very low bitrate */
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -04001671 if (effectiveBytes>=15*C && st->start==0 && st->complexity>=2 && !st->lfe)
Jean-Marc Valina6d663c2012-11-08 13:26:49 -05001672 {
1673 int lambda;
1674 if (effectiveBytes<40)
1675 lambda = 12;
1676 else if (effectiveBytes<60)
1677 lambda = 6;
1678 else if (effectiveBytes<100)
1679 lambda = 4;
1680 else
1681 lambda = 3;
1682 lambda*=2;
1683 tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, LM, &tf_sum, tf_estimate, tf_chan);
1684 for (i=effEnd;i<st->end;i++)
1685 tf_res[i] = tf_res[effEnd-1];
1686 } else {
1687 tf_sum = 0;
1688 for (i=0;i<st->end;i++)
1689 tf_res[i] = isTransient;
1690 tf_select=0;
1691 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05001692
1693 ALLOC(error, C*nbEBands, opus_val16);
1694 quant_coarse_energy(mode, st->start, st->end, effEnd, bandLogE,
1695 oldBandE, total_bits, error, enc,
1696 C, LM, nbAvailableBytes, st->force_intra,
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -04001697 &st->delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001698
1699 tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
1700
1701 if (ec_tell(enc)+4<=total_bits)
1702 {
Jean-Marc Valin172f66a2013-04-27 02:29:52 -04001703 if (st->lfe)
1704 {
1705 st->tapset_decision = 0;
1706 st->spread_decision = SPREAD_NORMAL;
1707 } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C || st->start != 0)
Jean-Marc Valin69062102012-11-08 09:42:27 -05001708 {
1709 if (st->complexity == 0)
1710 st->spread_decision = SPREAD_NONE;
Jean-Marc Valin1fd1d7d2012-11-08 17:22:07 -05001711 else
1712 st->spread_decision = SPREAD_NORMAL;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001713 } else {
Jean-Marc Valin41fd7a12012-12-12 14:41:29 -05001714 /* Disable new spreading+tapset estimator until we can show it works
1715 better than the old one. So far it seems like spreading_decision()
1716 works best. */
1717 if (0&&st->analysis.valid)
Jean-Marc Valin69062102012-11-08 09:42:27 -05001718 {
1719 static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
1720 static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
1721 static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), QCONST16(.15f, 15)};
1722 static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), QCONST16(.05f, 15)};
1723 st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision);
1724 st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
1725 } else {
1726 st->spread_decision = spreading_decision(mode, X,
1727 &st->tonal_average, st->spread_decision, &st->hf_average,
1728 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1729 }
1730 /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
1731 /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
1732 }
1733 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1734 }
1735
Jean-Marc Valin69062102012-11-08 09:42:27 -05001736 ALLOC(offsets, nbEBands, int);
1737
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001738 maxDepth = dynalloc_analysis(bandLogE, bandLogE2, nbEBands, st->start, st->end, C, offsets,
1739 st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr,
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001740 eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc);
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -04001741 /* For LFE, everything interesting is in the first band */
1742 if (st->lfe)
1743 offsets[0] = IMIN(8, effectiveBytes/3);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001744 ALLOC(cap, nbEBands, int);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001745 init_caps(mode,cap,LM,C);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001746
Jean-Marc Valin69062102012-11-08 09:42:27 -05001747 dynalloc_logp = 6;
1748 total_bits<<=BITRES;
1749 total_boost = 0;
1750 tell = ec_tell_frac(enc);
1751 for (i=st->start;i<st->end;i++)
1752 {
1753 int width, quanta;
1754 int dynalloc_loop_logp;
1755 int boost;
1756 int j;
1757 width = C*(eBands[i+1]-eBands[i])<<LM;
1758 /* quanta is 6 bits, but no more than 1 bit/sample
1759 and no less than 1/8 bit/sample */
1760 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1761 dynalloc_loop_logp = dynalloc_logp;
1762 boost = 0;
1763 for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
1764 && boost < cap[i]; j++)
1765 {
1766 int flag;
1767 flag = j<offsets[i];
1768 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
1769 tell = ec_tell_frac(enc);
1770 if (!flag)
1771 break;
1772 boost += quanta;
1773 total_boost += quanta;
1774 dynalloc_loop_logp = 1;
1775 }
1776 /* Making dynalloc more likely */
1777 if (j)
1778 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1779 offsets[i] = boost;
1780 }
1781
1782 if (C==2)
1783 {
1784 int effectiveRate;
1785
1786 static const opus_val16 intensity_thresholds[21]=
1787 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 off*/
1788 { 16,21,23,25,27,29,31,33,35,38,42,46,50,54,58,63,68,75,84,102,130};
1789 static const opus_val16 intensity_histeresis[21]=
1790 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5, 6, 8, 12};
1791
1792 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1793 if (LM!=0)
1794 dual_stereo = stereo_analysis(mode, X, LM, N);
1795
1796 /* Account for coarse energy */
1797 effectiveRate = (8*effectiveBytes - 80)>>LM;
1798
1799 /* effectiveRate in kb/s */
1800 effectiveRate = 2*effectiveRate/5;
1801
Jean-Marc Valind683c762012-12-21 16:17:38 -05001802 st->intensity = hysteresis_decision((opus_val16)effectiveRate, intensity_thresholds, intensity_histeresis, 21, st->intensity);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001803 st->intensity = IMIN(st->end,IMAX(st->start, st->intensity));
1804 }
1805
1806 alloc_trim = 5;
1807 if (tell+(6<<BITRES) <= total_bits - total_boost)
1808 {
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -04001809 if (st->lfe)
1810 alloc_trim = 5;
1811 else
1812 alloc_trim = alloc_trim_analysis(mode, X, bandLogE,
Jean-Marc Valin0f686962013-09-05 12:49:55 -04001813 st->end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate, st->intensity, surround_trim);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001814 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
1815 tell = ec_tell_frac(enc);
1816 }
1817
1818 /* Variable bitrate */
1819 if (vbr_rate>0)
1820 {
1821 opus_val16 alpha;
1822 opus_int32 delta;
1823 /* The target rate in 8th bits per frame */
1824 opus_int32 target, base_target;
1825 opus_int32 min_allowed;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001826 int lm_diff = mode->maxLM - LM;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001827
1828 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
1829 The CELT allocator will just not be able to use more than that anyway. */
1830 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001831 base_target = vbr_rate - ((40*C+20)<<BITRES);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001832
1833 if (st->constrained_vbr)
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001834 base_target += (st->vbr_offset>>lm_diff);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001835
Jean-Marc Valinc1959e72013-11-08 19:24:10 -05001836 target = compute_vbr(mode, &st->analysis, base_target, LM, equiv_rate,
Jean-Marc Valin28733d12013-06-10 03:30:01 -04001837 st->lastCodedBands, C, st->intensity, st->constrained_vbr,
1838 st->stereo_saving, tot_boost, tf_estimate, pitch_change, maxDepth,
Jean-Marc Valin3c0aa8f2013-06-25 14:10:27 -04001839 st->variable_duration, st->lfe, st->energy_mask!=NULL, surround_masking,
1840 temporal_vbr);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001841
1842 /* The current offset is removed from the target and the space used
1843 so far is added*/
1844 target=target+tell;
1845 /* In VBR mode the frame size must not be reduced so much that it would
1846 result in the encoder running out of bits.
1847 The margin of 2 bytes ensures that none of the bust-prevention logic
1848 in the decoder will have triggered so far. */
1849 min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes;
1850
1851 nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
1852 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
1853 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
1854
1855 /* By how much did we "miss" the target on that frame */
1856 delta = target - vbr_rate;
1857
1858 target=nbAvailableBytes<<(BITRES+3);
1859
1860 /*If the frame is silent we don't adjust our drift, otherwise
1861 the encoder will shoot to very high rates after hitting a
1862 span of silence, but we do allow the bitres to refill.
1863 This means that we'll undershoot our target in CVBR/VBR modes
1864 on files with lots of silence. */
1865 if(silence)
1866 {
1867 nbAvailableBytes = 2;
1868 target = 2*8<<BITRES;
1869 delta = 0;
1870 }
1871
1872 if (st->vbr_count < 970)
1873 {
1874 st->vbr_count++;
1875 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
1876 } else
1877 alpha = QCONST16(.001f,15);
1878 /* How many bits have we used in excess of what we're allowed */
1879 if (st->constrained_vbr)
1880 st->vbr_reservoir += target - vbr_rate;
1881 /*printf ("%d\n", st->vbr_reservoir);*/
1882
1883 /* Compute the offset we need to apply in order to reach the target */
1884 if (st->constrained_vbr)
1885 {
1886 st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
1887 st->vbr_offset = -st->vbr_drift;
1888 }
1889 /*printf ("%d\n", st->vbr_drift);*/
1890
1891 if (st->constrained_vbr && st->vbr_reservoir < 0)
1892 {
1893 /* We're under the min value -- increase rate */
1894 int adjust = (-st->vbr_reservoir)/(8<<BITRES);
1895 /* Unless we're just coding silence */
1896 nbAvailableBytes += silence?0:adjust;
1897 st->vbr_reservoir = 0;
1898 /*printf ("+%d\n", adjust);*/
1899 }
1900 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
1901 /*printf("%d\n", nbCompressedBytes*50*8);*/
1902 /* This moves the raw bits to take into account the new compressed size */
1903 ec_enc_shrink(enc, nbCompressedBytes);
1904 }
1905
1906 /* Bit allocation */
1907 ALLOC(fine_quant, nbEBands, int);
1908 ALLOC(pulses, nbEBands, int);
1909 ALLOC(fine_priority, nbEBands, int);
1910
1911 /* bits = packet size - where we are - safety*/
1912 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
1913 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1914 bits -= anti_collapse_rsv;
Jean-Marc Valin5fb50ad2012-12-21 01:23:26 -05001915 signalBandwidth = st->end-1;
Jean-Marc Valin3ab03e02013-09-06 16:00:39 -04001916#ifndef DISABLE_FLOAT_API
Jean-Marc Valin5fb50ad2012-12-21 01:23:26 -05001917 if (st->analysis.valid)
Jean-Marc Valin6e277c62013-05-17 14:15:31 -04001918 {
1919 int min_bandwidth;
Jean-Marc Valinc1959e72013-11-08 19:24:10 -05001920 if (equiv_rate < (opus_int32)32000*C)
Jean-Marc Valin6e277c62013-05-17 14:15:31 -04001921 min_bandwidth = 13;
Jean-Marc Valinc1959e72013-11-08 19:24:10 -05001922 else if (equiv_rate < (opus_int32)48000*C)
Jean-Marc Valin6e277c62013-05-17 14:15:31 -04001923 min_bandwidth = 16;
Jean-Marc Valinc1959e72013-11-08 19:24:10 -05001924 else if (equiv_rate < (opus_int32)60000*C)
Jean-Marc Valin6e277c62013-05-17 14:15:31 -04001925 min_bandwidth = 18;
Jean-Marc Valinc1959e72013-11-08 19:24:10 -05001926 else if (equiv_rate < (opus_int32)80000*C)
Jean-Marc Valin6e277c62013-05-17 14:15:31 -04001927 min_bandwidth = 19;
1928 else
1929 min_bandwidth = 20;
1930 signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth);
1931 }
Jean-Marc Valin5fb50ad2012-12-21 01:23:26 -05001932#endif
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -04001933 if (st->lfe)
1934 signalBandwidth = 1;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001935 codedBands = compute_allocation(mode, st->start, st->end, offsets, cap,
1936 alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses,
Jean-Marc Valin5fb50ad2012-12-21 01:23:26 -05001937 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBandwidth);
Jean-Marc Valina4dccd32013-05-04 23:54:20 -04001938 if (st->lastCodedBands)
1939 st->lastCodedBands = IMIN(st->lastCodedBands+1,IMAX(st->lastCodedBands-1,codedBands));
1940 else
1941 st->lastCodedBands = codedBands;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001942
1943 quant_fine_energy(mode, st->start, st->end, oldBandE, error, fine_quant, enc, C);
1944
Jean-Marc Valin69062102012-11-08 09:42:27 -05001945 /* Residual quantisation */
1946 ALLOC(collapse_masks, C*nbEBands, unsigned char);
1947 quant_all_bands(1, mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
1948 bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, st->intensity, tf_res,
1949 nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng);
1950
1951 if (anti_collapse_rsv > 0)
1952 {
1953 anti_collapse_on = st->consec_transient<2;
1954#ifdef FUZZING
1955 anti_collapse_on = rand()&0x1;
1956#endif
1957 ec_enc_bits(enc, anti_collapse_on, 1);
1958 }
1959 quant_energy_finalise(mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
1960
1961 if (silence)
1962 {
1963 for (i=0;i<C*nbEBands;i++)
1964 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
1965 }
1966
1967#ifdef RESYNTH
1968 /* Re-synthesis of the coded audio if required */
1969 {
1970 celt_sig *out_mem[2];
1971
Jean-Marc Valin69062102012-11-08 09:42:27 -05001972 if (anti_collapse_on)
1973 {
1974 anti_collapse(mode, X, collapse_masks, LM, C, N,
1975 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
1976 }
1977
Jean-Marc Valin3afc6ff2013-06-16 15:40:10 -04001978 if (silence)
1979 {
1980 for (i=0;i<C*N;i++)
1981 freq[i] = 0;
1982 } else {
Jean-Marc Valin3afc6ff2013-06-16 15:40:10 -04001983 /* Synthesis */
Jean-Marc Valinee2506b2013-06-16 20:24:52 -04001984 denormalise_bands(mode, X, freq, oldBandE, st->start, effEnd, C, M);
Jean-Marc Valin3afc6ff2013-06-16 15:40:10 -04001985 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05001986
1987 c=0; do {
Nils Wallméniuse0884fe2012-12-01 21:11:50 +01001988 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 -05001989 } while (++c<CC);
1990
1991 if (CC==2&&C==1)
1992 {
1993 for (i=0;i<N;i++)
1994 freq[N+i] = freq[i];
1995 }
1996
1997 c=0; do {
1998 out_mem[c] = st->syn_mem[c]+2*MAX_PERIOD-N;
1999 } while (++c<CC);
2000
2001 compute_inv_mdcts(mode, shortBlocks, freq, out_mem, CC, LM);
2002
2003 c=0; do {
2004 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
2005 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
2006 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, mode->shortMdctSize,
2007 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
2008 mode->window, st->overlap);
2009 if (LM!=0)
2010 comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMdctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize,
2011 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
2012 mode->window, overlap);
2013 } while (++c<CC);
2014
2015 /* We reuse freq[] as scratch space for the de-emphasis */
2016 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, mode->preemph, st->preemph_memD, freq);
2017 st->prefilter_period_old = st->prefilter_period;
2018 st->prefilter_gain_old = st->prefilter_gain;
2019 st->prefilter_tapset_old = st->prefilter_tapset;
2020 }
2021#endif
2022
2023 st->prefilter_period = pitch_index;
2024 st->prefilter_gain = gain1;
2025 st->prefilter_tapset = prefilter_tapset;
2026#ifdef RESYNTH
2027 if (LM!=0)
2028 {
2029 st->prefilter_period_old = st->prefilter_period;
2030 st->prefilter_gain_old = st->prefilter_gain;
2031 st->prefilter_tapset_old = st->prefilter_tapset;
2032 }
2033#endif
2034
2035 if (CC==2&&C==1) {
2036 for (i=0;i<nbEBands;i++)
2037 oldBandE[nbEBands+i]=oldBandE[i];
2038 }
2039
2040 if (!isTransient)
2041 {
2042 for (i=0;i<CC*nbEBands;i++)
2043 oldLogE2[i] = oldLogE[i];
2044 for (i=0;i<CC*nbEBands;i++)
2045 oldLogE[i] = oldBandE[i];
2046 } else {
2047 for (i=0;i<CC*nbEBands;i++)
2048 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
2049 }
2050 /* In case start or end were to change */
2051 c=0; do
2052 {
2053 for (i=0;i<st->start;i++)
2054 {
2055 oldBandE[c*nbEBands+i]=0;
2056 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2057 }
2058 for (i=st->end;i<nbEBands;i++)
2059 {
2060 oldBandE[c*nbEBands+i]=0;
2061 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2062 }
2063 } while (++c<CC);
2064
Jean-Marc Valin3252bf22013-04-19 03:14:28 -04002065 if (isTransient || transient_got_disabled)
Jean-Marc Valin69062102012-11-08 09:42:27 -05002066 st->consec_transient++;
2067 else
2068 st->consec_transient=0;
2069 st->rng = enc->rng;
2070
2071 /* If there's any room left (can only happen for very high rates),
2072 it's already filled with zeros */
2073 ec_enc_done(enc);
2074
2075#ifdef CUSTOM_MODES
2076 if (st->signalling)
2077 nbCompressedBytes++;
2078#endif
2079
2080 RESTORE_STACK;
2081 if (ec_get_error(enc))
2082 return OPUS_INTERNAL_ERROR;
2083 else
2084 return nbCompressedBytes;
2085}
2086
2087
2088#ifdef CUSTOM_MODES
2089
2090#ifdef FIXED_POINT
2091int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2092{
2093 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
2094}
2095
2096#ifndef DISABLE_FLOAT_API
2097int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2098{
2099 int j, ret, C, N;
2100 VARDECL(opus_int16, in);
2101 ALLOC_STACK;
2102
2103 if (pcm==NULL)
2104 return OPUS_BAD_ARG;
2105
2106 C = st->channels;
2107 N = frame_size;
2108 ALLOC(in, C*N, opus_int16);
2109
2110 for (j=0;j<C*N;j++)
2111 in[j] = FLOAT2INT16(pcm[j]);
2112
2113 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2114#ifdef RESYNTH
2115 for (j=0;j<C*N;j++)
2116 ((float*)pcm)[j]=in[j]*(1.f/32768.f);
2117#endif
2118 RESTORE_STACK;
2119 return ret;
2120}
2121#endif /* DISABLE_FLOAT_API */
2122#else
2123
2124int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2125{
2126 int j, ret, C, N;
2127 VARDECL(celt_sig, in);
2128 ALLOC_STACK;
2129
2130 if (pcm==NULL)
2131 return OPUS_BAD_ARG;
2132
2133 C=st->channels;
2134 N=frame_size;
2135 ALLOC(in, C*N, celt_sig);
2136 for (j=0;j<C*N;j++) {
2137 in[j] = SCALEOUT(pcm[j]);
2138 }
2139
2140 ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2141#ifdef RESYNTH
2142 for (j=0;j<C*N;j++)
2143 ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
2144#endif
2145 RESTORE_STACK;
2146 return ret;
2147}
2148
2149int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2150{
2151 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
2152}
2153
2154#endif
2155
2156#endif /* CUSTOM_MODES */
2157
2158int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...)
2159{
2160 va_list ap;
2161
2162 va_start(ap, request);
2163 switch (request)
2164 {
2165 case OPUS_SET_COMPLEXITY_REQUEST:
2166 {
2167 int value = va_arg(ap, opus_int32);
2168 if (value<0 || value>10)
2169 goto bad_arg;
2170 st->complexity = value;
2171 }
2172 break;
2173 case CELT_SET_START_BAND_REQUEST:
2174 {
2175 opus_int32 value = va_arg(ap, opus_int32);
2176 if (value<0 || value>=st->mode->nbEBands)
2177 goto bad_arg;
2178 st->start = value;
2179 }
2180 break;
2181 case CELT_SET_END_BAND_REQUEST:
2182 {
2183 opus_int32 value = va_arg(ap, opus_int32);
2184 if (value<1 || value>st->mode->nbEBands)
2185 goto bad_arg;
2186 st->end = value;
2187 }
2188 break;
2189 case CELT_SET_PREDICTION_REQUEST:
2190 {
2191 int value = va_arg(ap, opus_int32);
2192 if (value<0 || value>2)
2193 goto bad_arg;
2194 st->disable_pf = value<=1;
2195 st->force_intra = value==0;
2196 }
2197 break;
2198 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2199 {
2200 int value = va_arg(ap, opus_int32);
2201 if (value<0 || value>100)
2202 goto bad_arg;
2203 st->loss_rate = value;
2204 }
2205 break;
2206 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2207 {
2208 opus_int32 value = va_arg(ap, opus_int32);
2209 st->constrained_vbr = value;
2210 }
2211 break;
2212 case OPUS_SET_VBR_REQUEST:
2213 {
2214 opus_int32 value = va_arg(ap, opus_int32);
2215 st->vbr = value;
2216 }
2217 break;
2218 case OPUS_SET_BITRATE_REQUEST:
2219 {
2220 opus_int32 value = va_arg(ap, opus_int32);
2221 if (value<=500 && value!=OPUS_BITRATE_MAX)
2222 goto bad_arg;
2223 value = IMIN(value, 260000*st->channels);
2224 st->bitrate = value;
2225 }
2226 break;
2227 case CELT_SET_CHANNELS_REQUEST:
2228 {
2229 opus_int32 value = va_arg(ap, opus_int32);
2230 if (value<1 || value>2)
2231 goto bad_arg;
2232 st->stream_channels = value;
2233 }
2234 break;
2235 case OPUS_SET_LSB_DEPTH_REQUEST:
2236 {
2237 opus_int32 value = va_arg(ap, opus_int32);
2238 if (value<8 || value>24)
2239 goto bad_arg;
2240 st->lsb_depth=value;
2241 }
2242 break;
2243 case OPUS_GET_LSB_DEPTH_REQUEST:
2244 {
2245 opus_int32 *value = va_arg(ap, opus_int32*);
2246 *value=st->lsb_depth;
2247 }
2248 break;
Jean-Marc Valin3252bf22013-04-19 03:14:28 -04002249 case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
2250 {
2251 opus_int32 value = va_arg(ap, opus_int32);
2252 st->variable_duration = value;
2253 }
2254 break;
Jean-Marc Valin69062102012-11-08 09:42:27 -05002255 case OPUS_RESET_STATE:
2256 {
2257 int i;
2258 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
2259 oldBandE = (opus_val16*)(st->in_mem+st->channels*(st->overlap+COMBFILTER_MAXPERIOD));
2260 oldLogE = oldBandE + st->channels*st->mode->nbEBands;
2261 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
2262 OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
2263 opus_custom_encoder_get_size(st->mode, st->channels)-
2264 ((char*)&st->ENCODER_RESET_START - (char*)st));
2265 for (i=0;i<st->channels*st->mode->nbEBands;i++)
2266 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
2267 st->vbr_offset = 0;
2268 st->delayedIntra = 1;
2269 st->spread_decision = SPREAD_NORMAL;
2270 st->tonal_average = 256;
2271 st->hf_average = 0;
2272 st->tapset_decision = 0;
2273 }
2274 break;
2275#ifdef CUSTOM_MODES
2276 case CELT_SET_INPUT_CLIPPING_REQUEST:
2277 {
2278 opus_int32 value = va_arg(ap, opus_int32);
2279 st->clip = value;
2280 }
2281 break;
2282#endif
2283 case CELT_SET_SIGNALLING_REQUEST:
2284 {
2285 opus_int32 value = va_arg(ap, opus_int32);
2286 st->signalling = value;
2287 }
2288 break;
2289 case CELT_SET_ANALYSIS_REQUEST:
2290 {
2291 AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
2292 if (info)
2293 OPUS_COPY(&st->analysis, info, 1);
2294 }
2295 break;
2296 case CELT_GET_MODE_REQUEST:
2297 {
2298 const CELTMode ** value = va_arg(ap, const CELTMode**);
2299 if (value==0)
2300 goto bad_arg;
2301 *value=st->mode;
2302 }
2303 break;
2304 case OPUS_GET_FINAL_RANGE_REQUEST:
2305 {
2306 opus_uint32 * value = va_arg(ap, opus_uint32 *);
2307 if (value==0)
2308 goto bad_arg;
2309 *value=st->rng;
2310 }
2311 break;
Jean-Marc Valinb08c4ca2013-04-26 16:32:10 -04002312 case OPUS_SET_LFE_REQUEST:
2313 {
2314 opus_int32 value = va_arg(ap, opus_int32);
2315 st->lfe = value;
2316 }
2317 break;
Jean-Marc Valina4dccd32013-05-04 23:54:20 -04002318 case OPUS_SET_ENERGY_MASK_REQUEST:
2319 {
2320 opus_val16 *value = va_arg(ap, opus_val16*);
2321 st->energy_mask = value;
2322 }
2323 break;
Jean-Marc Valin69062102012-11-08 09:42:27 -05002324 default:
2325 goto bad_request;
2326 }
2327 va_end(ap);
2328 return OPUS_OK;
2329bad_arg:
2330 va_end(ap);
2331 return OPUS_BAD_ARG;
2332bad_request:
2333 va_end(ap);
2334 return OPUS_UNIMPLEMENTED;
2335}