blob: c22100cafecbb8c7e6d9332f4a6e4399656ea569 [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
36#include "os_support.h"
37#include "mdct.h"
38#include <math.h>
39#include "celt.h"
40#include "pitch.h"
41#include "bands.h"
42#include "modes.h"
43#include "entcode.h"
44#include "quant_bands.h"
45#include "rate.h"
46#include "stack_alloc.h"
47#include "mathops.h"
48#include "float_cast.h"
49#include <stdarg.h>
50#include "celt_lpc.h"
51#include "vq.h"
52
53
54/** Encoder state
55 @brief Encoder state
56 */
57struct OpusCustomEncoder {
58 const OpusCustomMode *mode; /**< Mode used by the encoder */
59 int overlap;
60 int channels;
61 int stream_channels;
62
63 int force_intra;
64 int clip;
65 int disable_pf;
66 int complexity;
67 int upsample;
68 int start, end;
69
70 opus_int32 bitrate;
71 int vbr;
72 int signalling;
73 int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */
74 int loss_rate;
75 int lsb_depth;
Jean-Marc Valin3252bf22013-04-19 03:14:28 -040076 int variable_duration;
Jean-Marc Valin69062102012-11-08 09:42:27 -050077
78 /* Everything beyond this point gets cleared on a reset */
79#define ENCODER_RESET_START rng
80
81 opus_uint32 rng;
82 int spread_decision;
83 opus_val32 delayedIntra;
84 int tonal_average;
85 int lastCodedBands;
86 int hf_average;
87 int tapset_decision;
88
89 int prefilter_period;
90 opus_val16 prefilter_gain;
91 int prefilter_tapset;
92#ifdef RESYNTH
93 int prefilter_period_old;
94 opus_val16 prefilter_gain_old;
95 int prefilter_tapset_old;
96#endif
97 int consec_transient;
98 AnalysisInfo analysis;
99
100 opus_val32 preemph_memE[2];
101 opus_val32 preemph_memD[2];
102
103 /* VBR-related parameters */
104 opus_int32 vbr_reservoir;
105 opus_int32 vbr_drift;
106 opus_int32 vbr_offset;
107 opus_int32 vbr_count;
108 opus_val16 overlap_max;
109 opus_val16 stereo_saving;
110 int intensity;
111
112#ifdef RESYNTH
113 /* +MAX_PERIOD/2 to make space for overlap */
114 celt_sig syn_mem[2][2*MAX_PERIOD+MAX_PERIOD/2];
115#endif
116
117 celt_sig in_mem[1]; /* Size = channels*mode->overlap */
118 /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_MAXPERIOD */
119 /* opus_val16 oldBandE[], Size = channels*mode->nbEBands */
120 /* opus_val16 oldLogE[], Size = channels*mode->nbEBands */
121 /* opus_val16 oldLogE2[], Size = channels*mode->nbEBands */
122};
123
124int celt_encoder_get_size(int channels)
125{
126 CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
127 return opus_custom_encoder_get_size(mode, channels);
128}
129
130OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_get_size(const CELTMode *mode, int channels)
131{
132 int size = sizeof(struct CELTEncoder)
133 + (channels*mode->overlap-1)*sizeof(celt_sig) /* celt_sig in_mem[channels*mode->overlap]; */
134 + channels*COMBFILTER_MAXPERIOD*sizeof(celt_sig) /* celt_sig prefilter_mem[channels*COMBFILTER_MAXPERIOD]; */
135 + 3*channels*mode->nbEBands*sizeof(opus_val16); /* opus_val16 oldBandE[channels*mode->nbEBands]; */
136 /* opus_val16 oldLogE[channels*mode->nbEBands]; */
137 /* opus_val16 oldLogE2[channels*mode->nbEBands]; */
138 return size;
139}
140
141#ifdef CUSTOM_MODES
142CELTEncoder *opus_custom_encoder_create(const CELTMode *mode, int channels, int *error)
143{
144 int ret;
145 CELTEncoder *st = (CELTEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels));
146 /* init will handle the NULL case */
147 ret = opus_custom_encoder_init(st, mode, channels);
148 if (ret != OPUS_OK)
149 {
150 opus_custom_encoder_destroy(st);
151 st = NULL;
152 }
153 if (error)
154 *error = ret;
155 return st;
156}
157#endif /* CUSTOM_MODES */
158
159int celt_encoder_init(CELTEncoder *st, opus_int32 sampling_rate, int channels)
160{
161 int ret;
162 ret = opus_custom_encoder_init(st, opus_custom_mode_create(48000, 960, NULL), channels);
163 if (ret != OPUS_OK)
164 return ret;
165 st->upsample = resampling_factor(sampling_rate);
166 return OPUS_OK;
167}
168
169OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_init(CELTEncoder *st, const CELTMode *mode, int channels)
170{
171 if (channels < 0 || channels > 2)
172 return OPUS_BAD_ARG;
173
174 if (st==NULL || mode==NULL)
175 return OPUS_ALLOC_FAIL;
176
177 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels));
178
179 st->mode = mode;
180 st->overlap = mode->overlap;
181 st->stream_channels = st->channels = channels;
182
183 st->upsample = 1;
184 st->start = 0;
185 st->end = st->mode->effEBands;
186 st->signalling = 1;
187
188 st->constrained_vbr = 1;
189 st->clip = 1;
190
191 st->bitrate = OPUS_BITRATE_MAX;
192 st->vbr = 0;
193 st->force_intra = 0;
194 st->complexity = 5;
195 st->lsb_depth=24;
196
197 opus_custom_encoder_ctl(st, OPUS_RESET_STATE);
198
199 return OPUS_OK;
200}
201
202#ifdef CUSTOM_MODES
203void opus_custom_encoder_destroy(CELTEncoder *st)
204{
205 opus_free(st);
206}
207#endif /* CUSTOM_MODES */
208
209
210static int transient_analysis(const opus_val32 * OPUS_RESTRICT in, int len, int C,
211 opus_val16 *tf_estimate, int *tf_chan)
212{
213 int i;
214 VARDECL(opus_val16, tmp);
215 opus_val32 mem0,mem1;
216 int is_transient = 0;
217 opus_int32 mask_metric = 0;
218 int c;
Jean-Marc Valind683c762012-12-21 16:17:38 -0500219 opus_val16 tf_max;
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500220 int len2;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500221 /* Table of 6*64/x, trained on real data to minimize the average error */
222 static const unsigned char inv_table[128] = {
223 255,255,156,110, 86, 70, 59, 51, 45, 40, 37, 33, 31, 28, 26, 25,
224 23, 22, 21, 20, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
225 12, 12, 11, 11, 11, 10, 10, 10, 9, 9, 9, 9, 9, 9, 8, 8,
226 8, 8, 8, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6,
227 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5,
228 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
229 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3,
230 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2,
231 };
232 SAVE_STACK;
233 ALLOC(tmp, len, opus_val16);
234
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500235 len2=len/2;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500236 tf_max = 0;
237 for (c=0;c<C;c++)
238 {
239 opus_val32 mean;
240 opus_int32 unmask=0;
241 opus_val32 norm;
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500242 opus_val16 maxE;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500243 mem0=0;
244 mem1=0;
245 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
246 for (i=0;i<len;i++)
247 {
248 opus_val32 x,y;
249 x = SHR32(in[i+c*len],SIG_SHIFT);
250 y = ADD32(mem0, x);
251#ifdef FIXED_POINT
252 mem0 = mem1 + y - SHL32(x,1);
253 mem1 = x - SHR32(y,1);
254#else
255 mem0 = mem1 + y - 2*x;
256 mem1 = x - .5f*y;
257#endif
258 tmp[i] = EXTRACT16(SHR32(y,2));
259 /*printf("%f ", tmp[i]);*/
260 }
261 /*printf("\n");*/
262 /* First few samples are bad because we don't propagate the memory */
263 for (i=0;i<12;i++)
264 tmp[i] = 0;
265
266#ifdef FIXED_POINT
267 /* Normalize tmp to max range */
268 {
269 int shift=0;
270 shift = 14-celt_ilog2(1+celt_maxabs16(tmp, len));
271 if (shift!=0)
272 {
273 for (i=0;i<len;i++)
274 tmp[i] = SHL16(tmp[i], shift);
275 }
276 }
277#endif
278
279 mean=0;
280 mem0=0;
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500281 /* Grouping by two to reduce complexity */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500282 /* Forward pass to compute the post-echo threshold*/
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500283 for (i=0;i<len2;i++)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500284 {
285 opus_val16 x2 = PSHR32(MULT16_16(tmp[2*i],tmp[2*i]) + MULT16_16(tmp[2*i+1],tmp[2*i+1]),16);
286 mean += x2;
287#ifdef FIXED_POINT
288 /* FIXME: Use PSHR16() instead */
289 tmp[i] = mem0 + PSHR32(x2-mem0,4);
290#else
291 tmp[i] = mem0 + MULT16_16_P15(QCONST16(.0625f,15),x2-mem0);
292#endif
293 mem0 = tmp[i];
294 }
295
296 mem0=0;
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500297 maxE=0;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500298 /* Backward pass to compute the pre-echo threshold */
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500299 for (i=len2-1;i>=0;i--)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500300 {
301#ifdef FIXED_POINT
302 /* FIXME: Use PSHR16() instead */
303 tmp[i] = mem0 + PSHR32(tmp[i]-mem0,3);
304#else
305 tmp[i] = mem0 + MULT16_16_P15(QCONST16(0.125f,15),tmp[i]-mem0);
306#endif
307 mem0 = tmp[i];
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500308 maxE = MAX16(maxE, mem0);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500309 }
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500310 /*for (i=0;i<len2;i++)printf("%f ", tmp[i]/mean);printf("\n");*/
Jean-Marc Valin69062102012-11-08 09:42:27 -0500311
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500312 /* Compute the ratio of the "frame energy" over the harmonic mean of the energy.
Jean-Marc Valin69062102012-11-08 09:42:27 -0500313 This essentially corresponds to a bitrate-normalized temporal noise-to-mask
314 ratio */
315
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500316 /* As a compromise with the old transient detector, frame energy is the
317 geometric mean of the energy and half the max */
318#ifdef FIXED_POINT
319 /* Costs two sqrt() to avoid overflows */
320 mean = MULT16_16(celt_sqrt(mean), celt_sqrt(MULT16_16(maxE,len2>>1)));
321#else
Jean-Marc Valind683c762012-12-21 16:17:38 -0500322 mean = celt_sqrt(mean * maxE*.5*len2);
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500323#endif
Jean-Marc Valin69062102012-11-08 09:42:27 -0500324 /* Inverse of the mean energy in Q15+6 */
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500325 norm = SHL32(EXTEND32(len2),6+14)/ADD32(EPSILON,SHR32(mean,1));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500326 /* Compute harmonic mean discarding the unreliable boundaries
327 The data is smooth, so we only take 1/4th of the samples */
328 unmask=0;
Jean-Marc Valin144b6e62012-11-10 10:13:03 -0500329 for (i=12;i<len2-5;i+=4)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500330 {
331 int id;
332#ifdef FIXED_POINT
333 id = IMAX(0,IMIN(127,MULT16_32_Q15(tmp[i],norm))); /* Do not round to nearest */
334#else
Jean-Marc Valind683c762012-12-21 16:17:38 -0500335 id = IMAX(0,IMIN(127,(int)floor(64*norm*tmp[i]))); /* Do not round to nearest */
Jean-Marc Valin69062102012-11-08 09:42:27 -0500336#endif
337 unmask += inv_table[id];
338 }
339 /*printf("%d\n", unmask);*/
340 /* 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 -0500341 unmask = 64*unmask*4/(6*(len2-17));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500342 if (unmask>mask_metric)
343 {
344 *tf_chan = c;
345 mask_metric = unmask;
346 }
347 }
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500348 is_transient = mask_metric>200;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500349
350 /* Arbitrary metric for VBR boost */
Jean-Marc Valin413caa02012-11-19 16:36:22 -0500351 tf_max = MAX16(0,celt_sqrt(27*mask_metric)-42);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500352 /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */
Jean-Marc Valind683c762012-12-21 16:17:38 -0500353 *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 -0500354 /*printf("%d %f\n", tf_max, mask_metric);*/
355 RESTORE_STACK;
356#ifdef FUZZING
357 is_transient = rand()&0x1;
358#endif
359 /*printf("%d %f %d\n", is_transient, (float)*tf_estimate, tf_max);*/
360 return is_transient;
361}
362
Jean-Marc Valin541a4722013-02-17 21:21:30 -0500363/* Looks for sudden increases of energy to decide whether we need to patch
364 the transient decision */
365int patch_transient_decision(opus_val16 *new, opus_val16 *old, int nbEBands,
366 int end, int C)
367{
368 int i, c;
369 opus_val32 mean_diff=0;
370 opus_val16 spread_old[26];
371 /* Apply an aggressive (-6 dB/Bark) spreading function to the old frame to
372 avoid false detection caused by irrelevant bands */
373 if (C==1)
374 {
375 spread_old[0] = old[0];
376 for (i=1;i<end;i++)
377 spread_old[i] = MAX16(spread_old[i-1]-QCONST16(1.0f, DB_SHIFT), old[i]);
378 } else {
379 spread_old[0] = MAX16(old[0],old[nbEBands]);
380 for (i=1;i<end;i++)
381 spread_old[i] = MAX16(spread_old[i-1]-QCONST16(1.0f, DB_SHIFT),
382 MAX16(old[i],old[i+nbEBands]));
383 }
384 for (i=end-2;i>=0;i--)
385 spread_old[i] = MAX16(spread_old[i], spread_old[i+1]-QCONST16(1.0f, DB_SHIFT));
386 /* Compute mean increase */
387 c=0; do {
388 for (i=2;i<end-1;i++)
389 {
390 opus_val16 x1, x2;
391 x1 = MAX16(0, new[i]);
392 x2 = MAX16(0, spread_old[i]);
393 mean_diff = ADD32(mean_diff, EXTEND32(MAX16(0, SUB16(x1, x2))));
394 }
395 } while (++c<C);
396 mean_diff = DIV32(mean_diff, C*(end-3));
397 /*printf("%f %f %d\n", mean_diff, max_diff, count);*/
398 return mean_diff > QCONST16(1.f, DB_SHIFT);
399}
400
Jean-Marc Valin69062102012-11-08 09:42:27 -0500401/** Apply window and compute the MDCT for all sub-frames and
402 all channels in a frame */
Jean-Marc Valin851f8032013-02-18 01:43:43 -0500403static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS_RESTRICT in,
404 celt_sig * OPUS_RESTRICT out, int C, int CC, int LM, int upsample)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500405{
406 const int overlap = OVERLAP(mode);
407 int N;
408 int B;
409 int shift;
Jean-Marc Valin851f8032013-02-18 01:43:43 -0500410 int i, b, c;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500411 if (shortBlocks)
412 {
413 B = shortBlocks;
414 N = mode->shortMdctSize;
415 shift = mode->maxLM;
416 } else {
417 B = 1;
418 N = mode->shortMdctSize<<LM;
419 shift = mode->maxLM-LM;
420 }
421 c=0; do {
422 for (b=0;b<B;b++)
423 {
424 /* Interleaving the sub-frames while doing the MDCTs */
425 clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N, &out[b+c*N*B], mode->window, overlap, shift, B);
426 }
Jean-Marc Valin851f8032013-02-18 01:43:43 -0500427 } while (++c<CC);
428 if (CC==2&&C==1)
429 {
430 for (i=0;i<B*N;i++)
431 out[i] = ADD32(HALF32(out[i]), HALF32(out[B*N+i]));
432 }
433 if (upsample != 1)
434 {
435 c=0; do
436 {
437 int bound = B*N/upsample;
438 for (i=0;i<bound;i++)
439 out[c*B*N+i] *= upsample;
440 for (;i<B*N;i++)
441 out[c*B*N+i] = 0;
442 } while (++c<C);
443 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500444}
445
446
447static void preemphasis(const opus_val16 * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTRICT inp,
448 int N, int CC, int upsample, const opus_val16 *coef, celt_sig *mem, int clip)
449{
450 int i;
Jean-Marc Valine368e622013-01-03 14:28:28 -0500451 opus_val16 coef0;
Jean-Marc Valin69062102012-11-08 09:42:27 -0500452 celt_sig m;
453 int Nu;
454
455 coef0 = coef[0];
Jean-Marc Valin69062102012-11-08 09:42:27 -0500456
457
458 Nu = N/upsample;
459 if (upsample!=1)
460 {
461 for (i=0;i<N;i++)
462 inp[i] = 0;
463 }
464 for (i=0;i<Nu;i++)
465 {
466 celt_sig x;
467
468 x = SCALEIN(pcmp[CC*i]);
469#ifndef FIXED_POINT
470 /* Replace NaNs with zeros */
471 if (!(x==x))
472 x = 0;
473#endif
474 inp[i*upsample] = x;
475 }
476
477#ifndef FIXED_POINT
478 if (clip)
479 {
480 /* Clip input to avoid encoding non-portable files */
481 for (i=0;i<Nu;i++)
482 inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample]));
483 }
484#endif
485 m = *mem;
Jean-Marc Valine368e622013-01-03 14:28:28 -0500486#ifdef CUSTOM_MODES
487 if (coef[1] != 0)
Jean-Marc Valin69062102012-11-08 09:42:27 -0500488 {
Jean-Marc Valine368e622013-01-03 14:28:28 -0500489 opus_val16 coef1 = coef[1];
Jean-Marc Valin69062102012-11-08 09:42:27 -0500490 opus_val16 coef2 = coef[2];
491 for (i=0;i<N;i++)
492 {
493 opus_val16 x, tmp;
494 x = inp[i];
495 /* Apply pre-emphasis */
496 tmp = MULT16_16(coef2, x);
497 inp[i] = tmp + m;
498 m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp);
499 }
Jean-Marc Valine368e622013-01-03 14:28:28 -0500500 } else
Jean-Marc Valinebdfbfb2013-01-09 11:13:00 -0500501#endif
Jean-Marc Valine368e622013-01-03 14:28:28 -0500502 {
503 for (i=0;i<N;i++)
504 {
505 celt_sig x;
506 x = SHL32(inp[i], SIG_SHIFT);
507 /* Apply pre-emphasis */
508 inp[i] = x + m;
509 m = - MULT16_32_Q15(coef0, x);
510 }
Jean-Marc Valin69062102012-11-08 09:42:27 -0500511 }
512 *mem = m;
513}
514
515
516
517static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, opus_val16 bias)
518{
519 int i;
520 opus_val32 L1;
521 L1 = 0;
522 for (i=0;i<N;i++)
523 L1 += EXTEND32(ABS16(tmp[i]));
524 /* When in doubt, prefer good freq resolution */
525 L1 = MAC16_32_Q15(L1, LM*bias, L1);
526 return L1;
527
528}
529
Jean-Marc Valina6d663c2012-11-08 13:26:49 -0500530static int tf_analysis(const CELTMode *m, int len, int isTransient,
531 int *tf_res, int lambda, celt_norm *X, int N0, int LM,
Jean-Marc Valin69062102012-11-08 09:42:27 -0500532 int *tf_sum, opus_val16 tf_estimate, int tf_chan)
533{
534 int i;
535 VARDECL(int, metric);
536 int cost0;
537 int cost1;
538 VARDECL(int, path0);
539 VARDECL(int, path1);
540 VARDECL(celt_norm, tmp);
541 VARDECL(celt_norm, tmp_1);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500542 int sel;
543 int selcost[2];
544 int tf_select=0;
545 opus_val16 bias;
546
547 SAVE_STACK;
Jean-Marc Valindae16fb2012-12-13 21:40:58 -0500548 bias = MULT16_16_Q14(QCONST16(.04f,15), MAX16(-QCONST16(.25f,14), QCONST16(.5f,14)-tf_estimate));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500549 /*printf("%f ", bias);*/
550
Jean-Marc Valin69062102012-11-08 09:42:27 -0500551 ALLOC(metric, len, int);
552 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
553 ALLOC(tmp_1, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
554 ALLOC(path0, len, int);
555 ALLOC(path1, len, int);
556
557 *tf_sum = 0;
558 for (i=0;i<len;i++)
559 {
560 int j, k, N;
561 int narrow;
562 opus_val32 L1, best_L1;
563 int best_level=0;
564 N = (m->eBands[i+1]-m->eBands[i])<<LM;
565 /* band is too narrow to be split down to LM=-1 */
566 narrow = (m->eBands[i+1]-m->eBands[i])==1;
567 for (j=0;j<N;j++)
568 tmp[j] = X[tf_chan*N0 + j+(m->eBands[i]<<LM)];
569 /* Just add the right channel if we're in stereo */
570 /*if (C==2)
571 for (j=0;j<N;j++)
572 tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));*/
573 L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias);
574 best_L1 = L1;
575 /* Check the -1 case for transients */
576 if (isTransient && !narrow)
577 {
578 for (j=0;j<N;j++)
579 tmp_1[j] = tmp[j];
580 haar1(tmp_1, N>>LM, 1<<LM);
581 L1 = l1_metric(tmp_1, N, LM+1, bias);
582 if (L1<best_L1)
583 {
584 best_L1 = L1;
585 best_level = -1;
586 }
587 }
588 /*printf ("%f ", L1);*/
589 for (k=0;k<LM+!(isTransient||narrow);k++)
590 {
591 int B;
592
593 if (isTransient)
594 B = (LM-k-1);
595 else
596 B = k+1;
597
598 haar1(tmp, N>>k, 1<<k);
599
600 L1 = l1_metric(tmp, N, B, bias);
601
602 if (L1 < best_L1)
603 {
604 best_L1 = L1;
605 best_level = k+1;
606 }
607 }
608 /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
609 /* metric is in Q1 to be able to select the mid-point (-0.5) for narrower bands */
610 if (isTransient)
611 metric[i] = 2*best_level;
612 else
613 metric[i] = -2*best_level;
614 *tf_sum += (isTransient ? LM : 0) - metric[i]/2;
615 /* For bands that can't be split to -1, set the metric to the half-way point to avoid
616 biasing the decision */
617 if (narrow && (metric[i]==0 || metric[i]==-2*LM))
618 metric[i]-=1;
619 /*printf("%d ", metric[i]);*/
620 }
621 /*printf("\n");*/
622 /* Search for the optimal tf resolution, including tf_select */
623 tf_select = 0;
624 for (sel=0;sel<2;sel++)
625 {
626 cost0 = 0;
627 cost1 = isTransient ? 0 : lambda;
628 for (i=1;i<len;i++)
629 {
630 int curr0, curr1;
631 curr0 = IMIN(cost0, cost1 + lambda);
632 curr1 = IMIN(cost0 + lambda, cost1);
633 cost0 = curr0 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
634 cost1 = curr1 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+1]);
635 }
636 cost0 = IMIN(cost0, cost1);
637 selcost[sel]=cost0;
638 }
639 /* For now, we're conservative and only allow tf_select=1 for transients.
640 * If tests confirm it's useful for non-transients, we could allow it. */
641 if (selcost[1]<selcost[0] && isTransient)
642 tf_select=1;
643 cost0 = 0;
644 cost1 = isTransient ? 0 : lambda;
645 /* Viterbi forward pass */
646 for (i=1;i<len;i++)
647 {
648 int curr0, curr1;
649 int from0, from1;
650
651 from0 = cost0;
652 from1 = cost1 + lambda;
653 if (from0 < from1)
654 {
655 curr0 = from0;
656 path0[i]= 0;
657 } else {
658 curr0 = from1;
659 path0[i]= 1;
660 }
661
662 from0 = cost0 + lambda;
663 from1 = cost1;
664 if (from0 < from1)
665 {
666 curr1 = from0;
667 path1[i]= 0;
668 } else {
669 curr1 = from1;
670 path1[i]= 1;
671 }
672 cost0 = curr0 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
673 cost1 = curr1 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]);
674 }
675 tf_res[len-1] = cost0 < cost1 ? 0 : 1;
676 /* Viterbi backward pass to check the decisions */
677 for (i=len-2;i>=0;i--)
678 {
679 if (tf_res[i+1] == 1)
680 tf_res[i] = path1[i+1];
681 else
682 tf_res[i] = path0[i+1];
683 }
684 /*printf("%d %f\n", *tf_sum, tf_estimate);*/
685 RESTORE_STACK;
686#ifdef FUZZING
687 tf_select = rand()&0x1;
688 tf_res[0] = rand()&0x1;
689 for (i=1;i<len;i++)
690 tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
691#endif
692 return tf_select;
693}
694
695static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
696{
697 int curr, i;
698 int tf_select_rsv;
699 int tf_changed;
700 int logp;
701 opus_uint32 budget;
702 opus_uint32 tell;
703 budget = enc->storage*8;
704 tell = ec_tell(enc);
705 logp = isTransient ? 2 : 4;
706 /* Reserve space to code the tf_select decision. */
707 tf_select_rsv = LM>0 && tell+logp+1 <= budget;
708 budget -= tf_select_rsv;
709 curr = tf_changed = 0;
710 for (i=start;i<end;i++)
711 {
712 if (tell+logp<=budget)
713 {
714 ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
715 tell = ec_tell(enc);
716 curr = tf_res[i];
717 tf_changed |= curr;
718 }
719 else
720 tf_res[i] = curr;
721 logp = isTransient ? 4 : 5;
722 }
723 /* Only code tf_select if it would actually make a difference. */
724 if (tf_select_rsv &&
725 tf_select_table[LM][4*isTransient+0+tf_changed]!=
726 tf_select_table[LM][4*isTransient+2+tf_changed])
727 ec_enc_bit_logp(enc, tf_select, 1);
728 else
729 tf_select = 0;
730 for (i=start;i<end;i++)
731 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
732 /*for(i=0;i<end;i++)printf("%d ", isTransient ? tf_res[i] : LM+tf_res[i]);printf("\n");*/
733}
734
735
736static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
737 const opus_val16 *bandLogE, int end, int LM, int C, int N0,
738 AnalysisInfo *analysis, opus_val16 *stereo_saving, opus_val16 tf_estimate,
739 int intensity)
740{
741 int i;
742 opus_val32 diff=0;
743 int c;
744 int trim_index = 5;
745 opus_val16 trim = QCONST16(5.f, 8);
746 opus_val16 logXC, logXC2;
747 if (C==2)
748 {
749 opus_val16 sum = 0; /* Q10 */
750 opus_val16 minXC; /* Q10 */
751 /* Compute inter-channel correlation for low frequencies */
752 for (i=0;i<8;i++)
753 {
754 int j;
755 opus_val32 partial = 0;
756 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
757 partial = MAC16_16(partial, X[j], X[N0+j]);
758 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
759 }
760 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
761 sum = MIN16(QCONST16(1.f, 10), ABS16(sum));
762 minXC = sum;
763 for (i=8;i<intensity;i++)
764 {
765 int j;
766 opus_val32 partial = 0;
767 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
768 partial = MAC16_16(partial, X[j], X[N0+j]);
769 minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18))));
770 }
771 minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC));
772 /*printf ("%f\n", sum);*/
773 if (sum > QCONST16(.995f,10))
774 trim_index-=4;
775 else if (sum > QCONST16(.92f,10))
776 trim_index-=3;
777 else if (sum > QCONST16(.85f,10))
778 trim_index-=2;
779 else if (sum > QCONST16(.8f,10))
780 trim_index-=1;
781 /* mid-side savings estimations based on the LF average*/
782 logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum));
783 /* mid-side savings estimations based on min correlation */
784 logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(minXC, minXC)));
785#ifdef FIXED_POINT
786 /* Compensate for Q20 vs Q14 input and convert output to Q8 */
787 logXC = PSHR32(logXC-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8);
788 logXC2 = PSHR32(logXC2-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8);
789#endif
790
791 trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC));
792 *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2));
793 }
794
795 /* Estimate spectral tilt */
796 c=0; do {
797 for (i=0;i<end-1;i++)
798 {
799 diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-end);
800 }
801 } while (++c<C);
802 diff /= C*(end-1);
803 /*printf("%f\n", diff);*/
804 if (diff > QCONST16(2.f, DB_SHIFT))
805 trim_index--;
806 if (diff > QCONST16(8.f, DB_SHIFT))
807 trim_index--;
808 if (diff < -QCONST16(4.f, DB_SHIFT))
809 trim_index++;
810 if (diff < -QCONST16(10.f, DB_SHIFT))
811 trim_index++;
812 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), SHR16(diff+QCONST16(1.f, DB_SHIFT),DB_SHIFT-8)/6 ));
Jean-Marc Valindae16fb2012-12-13 21:40:58 -0500813 trim -= 2*SHR16(tf_estimate, 14-8);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500814#ifndef FIXED_POINT
815 if (analysis->valid)
816 {
Jean-Marc Valind683c762012-12-21 16:17:38 -0500817 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), 2*(analysis->tonality_slope+.05f)));
Jean-Marc Valin69062102012-11-08 09:42:27 -0500818 }
819#endif
820
821#ifdef FIXED_POINT
822 trim_index = PSHR32(trim, 8);
823#else
Jean-Marc Valind683c762012-12-21 16:17:38 -0500824 trim_index = (int)floor(.5f+trim);
Jean-Marc Valin69062102012-11-08 09:42:27 -0500825#endif
826 if (trim_index<0)
827 trim_index = 0;
828 if (trim_index>10)
829 trim_index = 10;
830 /*printf("%d\n", trim_index);*/
831#ifdef FUZZING
832 trim_index = rand()%11;
833#endif
834 return trim_index;
835}
836
837static int stereo_analysis(const CELTMode *m, const celt_norm *X,
838 int LM, int N0)
839{
840 int i;
841 int thetas;
842 opus_val32 sumLR = EPSILON, sumMS = EPSILON;
843
844 /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
845 for (i=0;i<13;i++)
846 {
847 int j;
848 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
849 {
850 opus_val32 L, R, M, S;
851 /* We cast to 32-bit first because of the -32768 case */
852 L = EXTEND32(X[j]);
853 R = EXTEND32(X[N0+j]);
854 M = ADD32(L, R);
855 S = SUB32(L, R);
856 sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)));
857 sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)));
858 }
859 }
860 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS);
861 thetas = 13;
862 /* We don't need thetas for lower bands with LM<=1 */
863 if (LM<=1)
864 thetas -= 8;
865 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)
866 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR);
867}
868
Jean-Marc Valind683c762012-12-21 16:17:38 -0500869static opus_val16 dynalloc_analysis(const opus_val16 *bandLogE, const opus_val16 *bandLogE2,
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500870 int nbEBands, int start, int end, int C, int *offsets, int lsb_depth, const opus_int16 *logN,
871 int isTransient, int vbr, int constrained_vbr, const opus_int16 *eBands, int LM,
872 int effectiveBytes, opus_int32 *tot_boost_)
873{
874 int i, c;
875 opus_int32 tot_boost=0;
876 opus_val16 maxDepth;
877 VARDECL(opus_val16, follower);
878 VARDECL(opus_val16, noise_floor);
879 SAVE_STACK;
880 ALLOC(follower, C*nbEBands, opus_val16);
881 ALLOC(noise_floor, C*nbEBands, opus_val16);
882 for (i=0;i<nbEBands;i++)
883 offsets[i] = 0;
884 /* Dynamic allocation code */
885 maxDepth=-QCONST16(32.f, DB_SHIFT);
886 for (i=0;i<end;i++)
887 {
888 /* Noise floor must take into account eMeans, the depth, the width of the bands
889 and the preemphasis filter (approx. square of bark band ID) */
890 noise_floor[i] = MULT16_16(QCONST16(0.0625f, DB_SHIFT),logN[i])
891 +QCONST16(.5f,DB_SHIFT)+SHL16(9-lsb_depth,DB_SHIFT)-SHL16(eMeans[i],6)
892 +MULT16_16(QCONST16(.0062,DB_SHIFT),(i+5)*(i+5));
893 }
894 c=0;do
895 {
896 for (i=0;i<end;i++)
897 maxDepth = MAX16(maxDepth, bandLogE[c*nbEBands+i]-noise_floor[i]);
898 } while (++c<C);
899 /* Make sure that dynamic allocation can't make us bust the budget */
900 if (effectiveBytes > 50 && LM>=1)
901 {
902 int last=0;
903 c=0;do
904 {
905 follower[c*nbEBands] = bandLogE2[c*nbEBands];
906 for (i=1;i<end;i++)
907 {
908 /* The last band to be at least 3 dB higher than the previous one
909 is the last we'll consider. Otherwise, we run into problems on
910 bandlimited signals. */
911 if (bandLogE2[c*nbEBands+i] > bandLogE2[c*nbEBands+i-1]+QCONST16(.5f,DB_SHIFT))
912 last=i;
913 follower[c*nbEBands+i] = MIN16(follower[c*nbEBands+i-1]+QCONST16(1.5f,DB_SHIFT), bandLogE2[c*nbEBands+i]);
914 }
915 for (i=last-1;i>=0;i--)
916 follower[c*nbEBands+i] = MIN16(follower[c*nbEBands+i], MIN16(follower[c*nbEBands+i+1]+QCONST16(2.f,DB_SHIFT), bandLogE2[c*nbEBands+i]));
917 for (i=0;i<end;i++)
918 follower[c*nbEBands+i] = MAX16(follower[c*nbEBands+i], noise_floor[i]);
919 } while (++c<C);
920 if (C==2)
921 {
922 for (i=start;i<end;i++)
923 {
924 /* Consider 24 dB "cross-talk" */
925 follower[nbEBands+i] = MAX16(follower[nbEBands+i], follower[ i]-QCONST16(4.f,DB_SHIFT));
926 follower[ i] = MAX16(follower[ i], follower[nbEBands+i]-QCONST16(4.f,DB_SHIFT));
927 follower[i] = HALF16(MAX16(0, bandLogE[i]-follower[i]) + MAX16(0, bandLogE[nbEBands+i]-follower[nbEBands+i]));
928 }
929 } else {
930 for (i=start;i<end;i++)
931 {
932 follower[i] = MAX16(0, bandLogE[i]-follower[i]);
933 }
934 }
935 /* For non-transient CBR/CVBR frames, halve the dynalloc contribution */
936 if ((!vbr || constrained_vbr)&&!isTransient)
937 {
938 for (i=start;i<end;i++)
939 follower[i] = HALF16(follower[i]);
940 }
941 for (i=start;i<end;i++)
942 {
943 int width;
944 int boost;
945 int boost_bits;
946
947 if (i<8)
948 follower[i] *= 2;
949 if (i>=12)
950 follower[i] = HALF16(follower[i]);
951 follower[i] = MIN16(follower[i], QCONST16(4, DB_SHIFT));
952
953 width = C*(eBands[i+1]-eBands[i])<<LM;
954 if (width<6)
955 {
Jean-Marc Valind683c762012-12-21 16:17:38 -0500956 boost = (int)SHR32(EXTEND32(follower[i]),DB_SHIFT);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500957 boost_bits = boost*width<<BITRES;
958 } else if (width > 48) {
Jean-Marc Valind683c762012-12-21 16:17:38 -0500959 boost = (int)SHR32(EXTEND32(follower[i])*8,DB_SHIFT);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500960 boost_bits = (boost*width<<BITRES)/8;
961 } else {
Jean-Marc Valind683c762012-12-21 16:17:38 -0500962 boost = (int)SHR32(EXTEND32(follower[i])*width/6,DB_SHIFT);
Jean-Marc Valin10b30e72012-11-10 00:44:03 -0500963 boost_bits = boost*6<<BITRES;
964 }
965 /* For CBR and non-transient CVBR frames, limit dynalloc to 1/4 of the bits */
966 if ((!vbr || (constrained_vbr&&!isTransient))
967 && (tot_boost+boost_bits)>>BITRES>>3 > effectiveBytes/4)
968 {
969 offsets[i] = 0;
970 break;
971 } else {
972 offsets[i] = boost;
973 tot_boost += boost_bits;
974 }
975 }
976 }
977 *tot_boost_ = tot_boost;
978 RESTORE_STACK;
979 return maxDepth;
980}
981
982
Jean-Marc Valin69062102012-11-08 09:42:27 -0500983static int run_prefilter(CELTEncoder *st, celt_sig *in, celt_sig *prefilter_mem, int CC, int N,
984 int prefilter_tapset, int *pitch, opus_val16 *gain, int *qgain, int enabled, int nbAvailableBytes)
985{
986 int c;
987 VARDECL(celt_sig, _pre);
988 celt_sig *pre[2];
989 const CELTMode *mode;
990 int pitch_index;
991 opus_val16 gain1;
992 opus_val16 pf_threshold;
993 int pf_on;
994 int qg;
995 SAVE_STACK;
996
997 mode = st->mode;
998 ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig);
999
1000 pre[0] = _pre;
1001 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD);
1002
1003
1004 c=0; do {
1005 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD);
1006 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+st->overlap)+st->overlap, N);
1007 } while (++c<CC);
1008
1009 if (enabled)
1010 {
1011 VARDECL(opus_val16, pitch_buf);
1012 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16);
1013
1014 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD+N, CC);
1015 /* Don't search for the fir last 1.5 octave of the range because
1016 there's too many false-positives due to short-term correlation */
1017 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD>>1), pitch_buf, N,
1018 COMBFILTER_MAXPERIOD-3*COMBFILTER_MINPERIOD, &pitch_index);
1019 pitch_index = COMBFILTER_MAXPERIOD-pitch_index;
1020
1021 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD, COMBFILTER_MINPERIOD,
1022 N, &pitch_index, st->prefilter_period, st->prefilter_gain);
1023 if (pitch_index > COMBFILTER_MAXPERIOD-2)
1024 pitch_index = COMBFILTER_MAXPERIOD-2;
1025 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1);
1026 /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.tonality);*/
1027 if (st->loss_rate>2)
1028 gain1 = HALF32(gain1);
1029 if (st->loss_rate>4)
1030 gain1 = HALF32(gain1);
1031 if (st->loss_rate>8)
1032 gain1 = 0;
1033 } else {
1034 gain1 = 0;
1035 pitch_index = COMBFILTER_MINPERIOD;
1036 }
1037
1038 /* Gain threshold for enabling the prefilter/postfilter */
1039 pf_threshold = QCONST16(.2f,15);
1040
1041 /* Adjusting the threshold based on rate and continuity */
1042 if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1043 pf_threshold += QCONST16(.2f,15);
1044 if (nbAvailableBytes<25)
1045 pf_threshold += QCONST16(.1f,15);
1046 if (nbAvailableBytes<35)
1047 pf_threshold += QCONST16(.1f,15);
1048 if (st->prefilter_gain > QCONST16(.4f,15))
1049 pf_threshold -= QCONST16(.1f,15);
1050 if (st->prefilter_gain > QCONST16(.55f,15))
1051 pf_threshold -= QCONST16(.1f,15);
1052
1053 /* Hard threshold at 0.2 */
1054 pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15));
1055 if (gain1<pf_threshold)
1056 {
1057 gain1 = 0;
1058 pf_on = 0;
1059 qg = 0;
1060 } else {
1061 /*This block is not gated by a total bits check only because
1062 of the nbAvailableBytes check above.*/
1063 if (ABS16(gain1-st->prefilter_gain)<QCONST16(.1f,15))
1064 gain1=st->prefilter_gain;
1065
1066#ifdef FIXED_POINT
1067 qg = ((gain1+1536)>>10)/3-1;
1068#else
1069 qg = (int)floor(.5f+gain1*32/3)-1;
1070#endif
1071 qg = IMAX(0, IMIN(7, qg));
1072 gain1 = QCONST16(0.09375f,15)*(qg+1);
1073 pf_on = 1;
1074 }
1075 /*printf("%d %f\n", pitch_index, gain1);*/
1076
1077 c=0; do {
1078 int offset = mode->shortMdctSize-st->overlap;
1079 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1080 OPUS_COPY(in+c*(N+st->overlap), st->in_mem+c*(st->overlap), st->overlap);
1081 if (offset)
1082 comb_filter(in+c*(N+st->overlap)+st->overlap, pre[c]+COMBFILTER_MAXPERIOD,
1083 st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1084 st->prefilter_tapset, st->prefilter_tapset, NULL, 0);
1085
1086 comb_filter(in+c*(N+st->overlap)+st->overlap+offset, pre[c]+COMBFILTER_MAXPERIOD+offset,
1087 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1088 st->prefilter_tapset, prefilter_tapset, mode->window, st->overlap);
1089 OPUS_COPY(st->in_mem+c*(st->overlap), in+c*(N+st->overlap)+N, st->overlap);
1090
1091 if (N>COMBFILTER_MAXPERIOD)
1092 {
1093 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD);
1094 } else {
1095 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N);
1096 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N);
1097 }
1098 } while (++c<CC);
1099
1100 RESTORE_STACK;
1101 *gain = gain1;
1102 *pitch = pitch_index;
1103 *qgain = qg;
1104 return pf_on;
1105}
1106
1107int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
1108{
1109 int i, c, N;
1110 opus_int32 bits;
1111 ec_enc _enc;
1112 VARDECL(celt_sig, in);
1113 VARDECL(celt_sig, freq);
1114 VARDECL(celt_norm, X);
1115 VARDECL(celt_ener, bandE);
1116 VARDECL(opus_val16, bandLogE);
1117 VARDECL(opus_val16, bandLogE2);
1118 VARDECL(int, fine_quant);
1119 VARDECL(opus_val16, error);
1120 VARDECL(int, pulses);
1121 VARDECL(int, cap);
1122 VARDECL(int, offsets);
1123 VARDECL(int, fine_priority);
1124 VARDECL(int, tf_res);
1125 VARDECL(unsigned char, collapse_masks);
1126 celt_sig *prefilter_mem;
1127 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
1128 int shortBlocks=0;
1129 int isTransient=0;
1130 const int CC = st->channels;
1131 const int C = st->stream_channels;
1132 int LM, M;
1133 int tf_select;
1134 int nbFilledBytes, nbAvailableBytes;
1135 int effEnd;
1136 int codedBands;
1137 int tf_sum;
1138 int alloc_trim;
1139 int pitch_index=COMBFILTER_MINPERIOD;
1140 opus_val16 gain1 = 0;
1141 int dual_stereo=0;
1142 int effectiveBytes;
1143 int dynalloc_logp;
1144 opus_int32 vbr_rate;
1145 opus_int32 total_bits;
1146 opus_int32 total_boost;
1147 opus_int32 balance;
1148 opus_int32 tell;
1149 int prefilter_tapset=0;
1150 int pf_on;
1151 int anti_collapse_rsv;
1152 int anti_collapse_on=0;
1153 int silence=0;
1154 int tf_chan = 0;
1155 opus_val16 tf_estimate;
1156 int pitch_change=0;
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001157 opus_int32 tot_boost;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001158 opus_val16 sample_max;
1159 opus_val16 maxDepth;
1160 const OpusCustomMode *mode;
1161 int nbEBands;
1162 int overlap;
1163 const opus_int16 *eBands;
1164 int secondMdct;
Jean-Marc Valin5fb50ad2012-12-21 01:23:26 -05001165 int signalBandwidth;
Jean-Marc Valin3252bf22013-04-19 03:14:28 -04001166 int transient_got_disabled;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001167 ALLOC_STACK;
1168
1169 mode = st->mode;
1170 nbEBands = mode->nbEBands;
1171 overlap = mode->overlap;
1172 eBands = mode->eBands;
Jean-Marc Valindae16fb2012-12-13 21:40:58 -05001173 tf_estimate = 0;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001174 if (nbCompressedBytes<2 || pcm==NULL)
1175 return OPUS_BAD_ARG;
1176
1177 frame_size *= st->upsample;
1178 for (LM=0;LM<=mode->maxLM;LM++)
1179 if (mode->shortMdctSize<<LM==frame_size)
1180 break;
1181 if (LM>mode->maxLM)
1182 return OPUS_BAD_ARG;
1183 M=1<<LM;
1184 N = M*mode->shortMdctSize;
1185
1186 prefilter_mem = st->in_mem+CC*(st->overlap);
1187 oldBandE = (opus_val16*)(st->in_mem+CC*(st->overlap+COMBFILTER_MAXPERIOD));
1188 oldLogE = oldBandE + CC*nbEBands;
1189 oldLogE2 = oldLogE + CC*nbEBands;
1190
1191 if (enc==NULL)
1192 {
1193 tell=1;
1194 nbFilledBytes=0;
1195 } else {
1196 tell=ec_tell(enc);
1197 nbFilledBytes=(tell+4)>>3;
1198 }
1199
1200#ifdef CUSTOM_MODES
1201 if (st->signalling && enc==NULL)
1202 {
1203 int tmp = (mode->effEBands-st->end)>>1;
1204 st->end = IMAX(1, mode->effEBands-tmp);
1205 compressed[0] = tmp<<5;
1206 compressed[0] |= LM<<3;
1207 compressed[0] |= (C==2)<<2;
1208 /* Convert "standard mode" to Opus header */
1209 if (mode->Fs==48000 && mode->shortMdctSize==120)
1210 {
1211 int c0 = toOpus(compressed[0]);
1212 if (c0<0)
1213 return OPUS_BAD_ARG;
1214 compressed[0] = c0;
1215 }
1216 compressed++;
1217 nbCompressedBytes--;
1218 }
1219#else
1220 celt_assert(st->signalling==0);
1221#endif
1222
1223 /* Can't produce more than 1275 output bytes */
1224 nbCompressedBytes = IMIN(nbCompressedBytes,1275);
1225 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1226
1227 if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1228 {
1229 opus_int32 den=mode->Fs>>BITRES;
1230 vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1231#ifdef CUSTOM_MODES
1232 if (st->signalling)
1233 vbr_rate -= 8<<BITRES;
1234#endif
1235 effectiveBytes = vbr_rate>>(3+BITRES);
1236 } else {
1237 opus_int32 tmp;
1238 vbr_rate = 0;
1239 tmp = st->bitrate*frame_size;
1240 if (tell>1)
1241 tmp += tell;
1242 if (st->bitrate!=OPUS_BITRATE_MAX)
1243 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1244 (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling));
1245 effectiveBytes = nbCompressedBytes;
1246 }
1247
1248 if (enc==NULL)
1249 {
1250 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1251 enc = &_enc;
1252 }
1253
1254 if (vbr_rate>0)
1255 {
1256 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1257 target rate and buffering.
1258 We must do this up front so that bust-prevention logic triggers
1259 correctly if we don't have enough bits. */
1260 if (st->constrained_vbr)
1261 {
1262 opus_int32 vbr_bound;
1263 opus_int32 max_allowed;
1264 /* We could use any multiple of vbr_rate as bound (depending on the
1265 delay).
1266 This is clamped to ensure we use at least two bytes if the encoder
1267 was entirely empty, but to allow 0 in hybrid mode. */
1268 vbr_bound = vbr_rate;
1269 max_allowed = IMIN(IMAX(tell==1?2:0,
1270 (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1271 nbAvailableBytes);
1272 if(max_allowed < nbAvailableBytes)
1273 {
1274 nbCompressedBytes = nbFilledBytes+max_allowed;
1275 nbAvailableBytes = max_allowed;
1276 ec_enc_shrink(enc, nbCompressedBytes);
1277 }
1278 }
1279 }
1280 total_bits = nbCompressedBytes*8;
1281
1282 effEnd = st->end;
1283 if (effEnd > mode->effEBands)
1284 effEnd = mode->effEBands;
1285
1286 ALLOC(in, CC*(N+st->overlap), celt_sig);
1287
1288 sample_max=MAX16(st->overlap_max, celt_maxabs16(pcm, C*(N-overlap)/st->upsample));
1289 st->overlap_max=celt_maxabs16(pcm+C*(N-overlap)/st->upsample, C*overlap/st->upsample);
1290 sample_max=MAX16(sample_max, st->overlap_max);
1291#ifdef FIXED_POINT
1292 silence = (sample_max==0);
1293#else
1294 silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth));
1295#endif
1296#ifdef FUZZING
1297 if ((rand()&0x3F)==0)
1298 silence = 1;
1299#endif
1300 if (tell==1)
1301 ec_enc_bit_logp(enc, silence, 15);
1302 else
1303 silence=0;
1304 if (silence)
1305 {
1306 /*In VBR mode there is no need to send more than the minimum. */
1307 if (vbr_rate>0)
1308 {
1309 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2);
1310 total_bits=nbCompressedBytes*8;
1311 nbAvailableBytes=2;
1312 ec_enc_shrink(enc, nbCompressedBytes);
1313 }
1314 /* Pretend we've filled all the remaining bits with zeros
1315 (that's what the initialiser did anyway) */
1316 tell = nbCompressedBytes*8;
1317 enc->nbits_total+=tell-ec_tell(enc);
1318 }
1319 c=0; do {
1320 preemphasis(pcm+c, in+c*(N+st->overlap)+st->overlap, N, CC, st->upsample,
1321 mode->preemph, st->preemph_memE+c, st->clip);
1322 } while (++c<CC);
1323
1324
1325
1326 /* Find pitch period and gain */
1327 {
1328 int enabled;
1329 int qg;
Jean-Marc Valin3252bf22013-04-19 03:14:28 -04001330 enabled = nbAvailableBytes>12*C && st->start==0 && !silence && !st->disable_pf
1331 && st->complexity >= 5 && !(st->consec_transient && LM!=3 && st->variable_duration);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001332
1333 prefilter_tapset = st->tapset_decision;
1334 pf_on = run_prefilter(st, in, prefilter_mem, CC, N, prefilter_tapset, &pitch_index, &gain1, &qg, enabled, nbAvailableBytes);
Jean-Marc Valin48ac1222012-11-14 02:39:27 -05001335 if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && (!st->analysis.valid || st->analysis.tonality > .3)
Jean-Marc Valin69062102012-11-08 09:42:27 -05001336 && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st->prefilter_period))
1337 pitch_change = 1;
1338 if (pf_on==0)
1339 {
1340 if(st->start==0 && tell+16<=total_bits)
1341 ec_enc_bit_logp(enc, 0, 1);
1342 } else {
1343 /*This block is not gated by a total bits check only because
1344 of the nbAvailableBytes check above.*/
1345 int octave;
1346 ec_enc_bit_logp(enc, 1, 1);
1347 pitch_index += 1;
1348 octave = EC_ILOG(pitch_index)-5;
1349 ec_enc_uint(enc, octave, 6);
1350 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1351 pitch_index -= 1;
1352 ec_enc_bits(enc, qg, 3);
1353 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1354 }
1355 }
1356
1357 isTransient = 0;
1358 shortBlocks = 0;
Jean-Marc Valin2a5f0562012-11-19 23:17:06 -05001359 if (st->complexity >= 1)
1360 {
1361 isTransient = transient_analysis(in, N+st->overlap, CC,
1362 &tf_estimate, &tf_chan);
1363 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05001364 if (LM>0 && ec_tell(enc)+3<=total_bits)
1365 {
Jean-Marc Valin2a5f0562012-11-19 23:17:06 -05001366 if (isTransient)
1367 shortBlocks = M;
Jean-Marc Valin2a5f0562012-11-19 23:17:06 -05001368 } else {
1369 isTransient = 0;
Jean-Marc Valin3252bf22013-04-19 03:14:28 -04001370 transient_got_disabled=1;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001371 }
1372
1373 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
1374 ALLOC(bandE,nbEBands*CC, celt_ener);
1375 ALLOC(bandLogE,nbEBands*CC, opus_val16);
1376
1377 secondMdct = shortBlocks && st->complexity>=8;
1378 ALLOC(bandLogE2, C*nbEBands, opus_val16);
1379 if (secondMdct)
1380 {
Jean-Marc Valin851f8032013-02-18 01:43:43 -05001381 compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001382 compute_band_energies(mode, freq, bandE, effEnd, C, M);
1383 amp2Log2(mode, effEnd, st->end, bandE, bandLogE2, C);
1384 for (i=0;i<C*nbEBands;i++)
1385 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT));
1386 }
1387
Jean-Marc Valin851f8032013-02-18 01:43:43 -05001388 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample);
Jean-Marc Valin69c3dcd2013-02-19 03:42:18 -05001389 if (CC==2&&C==1)
1390 tf_chan = 0;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001391 compute_band_energies(mode, freq, bandE, effEnd, C, M);
1392
1393 amp2Log2(mode, effEnd, st->end, bandE, bandLogE, C);
1394 /*for (i=0;i<21;i++)
1395 printf("%f ", bandLogE[i]);
1396 printf("\n");*/
1397
1398 if (!secondMdct)
1399 {
1400 for (i=0;i<C*nbEBands;i++)
1401 bandLogE2[i] = bandLogE[i];
1402 }
1403
Jean-Marc Valin541a4722013-02-17 21:21:30 -05001404 /* Last chance to catch any transient we might have missed in the
1405 time-domain analysis */
1406 if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5)
1407 {
1408 if (patch_transient_decision(bandLogE, oldBandE, nbEBands, st->end, C))
1409 {
1410 isTransient = 1;
1411 shortBlocks = M;
Jean-Marc Valin851f8032013-02-18 01:43:43 -05001412 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample);
Jean-Marc Valin541a4722013-02-17 21:21:30 -05001413 compute_band_energies(mode, freq, bandE, effEnd, C, M);
1414 amp2Log2(mode, effEnd, st->end, bandE, bandLogE, C);
1415 /* Compensate for the scaling of short vs long mdcts */
1416 for (i=0;i<C*nbEBands;i++)
1417 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT));
1418 tf_estimate = QCONST16(.2,14);
1419 }
1420 }
1421
1422 if (LM>0 && ec_tell(enc)+3<=total_bits)
1423 ec_enc_bit_logp(enc, isTransient, 3);
1424
Jean-Marc Valin69062102012-11-08 09:42:27 -05001425 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1426
1427 /* Band normalisation */
1428 normalise_bands(mode, freq, X, bandE, effEnd, C, M);
1429
1430 ALLOC(tf_res, nbEBands, int);
Jean-Marc Valina6d663c2012-11-08 13:26:49 -05001431 /* Disable variable tf resolution for hybrid and at very low bitrate */
Jean-Marc Valin90bac9d2012-12-14 14:01:06 -05001432 if (effectiveBytes>=15*C && st->start==0 && st->complexity>=2)
Jean-Marc Valina6d663c2012-11-08 13:26:49 -05001433 {
1434 int lambda;
1435 if (effectiveBytes<40)
1436 lambda = 12;
1437 else if (effectiveBytes<60)
1438 lambda = 6;
1439 else if (effectiveBytes<100)
1440 lambda = 4;
1441 else
1442 lambda = 3;
1443 lambda*=2;
1444 tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, LM, &tf_sum, tf_estimate, tf_chan);
1445 for (i=effEnd;i<st->end;i++)
1446 tf_res[i] = tf_res[effEnd-1];
1447 } else {
1448 tf_sum = 0;
1449 for (i=0;i<st->end;i++)
1450 tf_res[i] = isTransient;
1451 tf_select=0;
1452 }
Jean-Marc Valin69062102012-11-08 09:42:27 -05001453
1454 ALLOC(error, C*nbEBands, opus_val16);
1455 quant_coarse_energy(mode, st->start, st->end, effEnd, bandLogE,
1456 oldBandE, total_bits, error, enc,
1457 C, LM, nbAvailableBytes, st->force_intra,
1458 &st->delayedIntra, st->complexity >= 4, st->loss_rate);
1459
1460 tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
1461
1462 if (ec_tell(enc)+4<=total_bits)
1463 {
Jean-Marc Valin1fd1d7d2012-11-08 17:22:07 -05001464 if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C || st->start != 0)
Jean-Marc Valin69062102012-11-08 09:42:27 -05001465 {
1466 if (st->complexity == 0)
1467 st->spread_decision = SPREAD_NONE;
Jean-Marc Valin1fd1d7d2012-11-08 17:22:07 -05001468 else
1469 st->spread_decision = SPREAD_NORMAL;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001470 } else {
Jean-Marc Valin41fd7a12012-12-12 14:41:29 -05001471 /* Disable new spreading+tapset estimator until we can show it works
1472 better than the old one. So far it seems like spreading_decision()
1473 works best. */
1474 if (0&&st->analysis.valid)
Jean-Marc Valin69062102012-11-08 09:42:27 -05001475 {
1476 static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
1477 static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
1478 static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), QCONST16(.15f, 15)};
1479 static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), QCONST16(.05f, 15)};
1480 st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision);
1481 st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
1482 } else {
1483 st->spread_decision = spreading_decision(mode, X,
1484 &st->tonal_average, st->spread_decision, &st->hf_average,
1485 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1486 }
1487 /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
1488 /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
1489 }
1490 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1491 }
1492
Jean-Marc Valin69062102012-11-08 09:42:27 -05001493 ALLOC(offsets, nbEBands, int);
1494
Jean-Marc Valin10b30e72012-11-10 00:44:03 -05001495 maxDepth = dynalloc_analysis(bandLogE, bandLogE2, nbEBands, st->start, st->end, C, offsets,
1496 st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr,
1497 eBands, LM, effectiveBytes, &tot_boost);
1498 ALLOC(cap, nbEBands, int);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001499 init_caps(mode,cap,LM,C);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001500
Jean-Marc Valin69062102012-11-08 09:42:27 -05001501 dynalloc_logp = 6;
1502 total_bits<<=BITRES;
1503 total_boost = 0;
1504 tell = ec_tell_frac(enc);
1505 for (i=st->start;i<st->end;i++)
1506 {
1507 int width, quanta;
1508 int dynalloc_loop_logp;
1509 int boost;
1510 int j;
1511 width = C*(eBands[i+1]-eBands[i])<<LM;
1512 /* quanta is 6 bits, but no more than 1 bit/sample
1513 and no less than 1/8 bit/sample */
1514 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1515 dynalloc_loop_logp = dynalloc_logp;
1516 boost = 0;
1517 for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
1518 && boost < cap[i]; j++)
1519 {
1520 int flag;
1521 flag = j<offsets[i];
1522 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
1523 tell = ec_tell_frac(enc);
1524 if (!flag)
1525 break;
1526 boost += quanta;
1527 total_boost += quanta;
1528 dynalloc_loop_logp = 1;
1529 }
1530 /* Making dynalloc more likely */
1531 if (j)
1532 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1533 offsets[i] = boost;
1534 }
1535
1536 if (C==2)
1537 {
1538 int effectiveRate;
1539
1540 static const opus_val16 intensity_thresholds[21]=
1541 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 off*/
1542 { 16,21,23,25,27,29,31,33,35,38,42,46,50,54,58,63,68,75,84,102,130};
1543 static const opus_val16 intensity_histeresis[21]=
1544 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5, 6, 8, 12};
1545
1546 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1547 if (LM!=0)
1548 dual_stereo = stereo_analysis(mode, X, LM, N);
1549
1550 /* Account for coarse energy */
1551 effectiveRate = (8*effectiveBytes - 80)>>LM;
1552
1553 /* effectiveRate in kb/s */
1554 effectiveRate = 2*effectiveRate/5;
1555
Jean-Marc Valind683c762012-12-21 16:17:38 -05001556 st->intensity = hysteresis_decision((opus_val16)effectiveRate, intensity_thresholds, intensity_histeresis, 21, st->intensity);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001557 st->intensity = IMIN(st->end,IMAX(st->start, st->intensity));
1558 }
1559
1560 alloc_trim = 5;
1561 if (tell+(6<<BITRES) <= total_bits - total_boost)
1562 {
1563 alloc_trim = alloc_trim_analysis(mode, X, bandLogE,
1564 st->end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate, st->intensity);
1565 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
1566 tell = ec_tell_frac(enc);
1567 }
1568
1569 /* Variable bitrate */
1570 if (vbr_rate>0)
1571 {
1572 opus_val16 alpha;
1573 opus_int32 delta;
1574 /* The target rate in 8th bits per frame */
1575 opus_int32 target, base_target;
1576 opus_int32 min_allowed;
1577 int coded_bins;
1578 int coded_bands;
1579 int lm_diff = mode->maxLM - LM;
1580 coded_bands = st->lastCodedBands ? st->lastCodedBands : nbEBands;
1581 coded_bins = eBands[coded_bands]<<LM;
1582 if (C==2)
1583 coded_bins += eBands[IMIN(st->intensity, coded_bands)]<<LM;
1584
1585 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
1586 The CELT allocator will just not be able to use more than that anyway. */
1587 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
1588 target = vbr_rate - ((40*C+20)<<BITRES);
1589 base_target = target;
1590
1591 if (st->constrained_vbr)
1592 target += (st->vbr_offset>>lm_diff);
1593
1594 /*printf("%f %f %f %f %d %d ", st->analysis.activity, st->analysis.tonality, tf_estimate, st->stereo_saving, tot_boost, coded_bands);*/
1595#ifndef FIXED_POINT
1596 if (st->analysis.valid && st->analysis.activity<.4)
Jean-Marc Valind683c762012-12-21 16:17:38 -05001597 target -= (opus_int32)((coded_bins<<BITRES)*(.4f-st->analysis.activity));
Jean-Marc Valin69062102012-11-08 09:42:27 -05001598#endif
1599 /* Stereo savings */
1600 if (C==2)
1601 {
1602 int coded_stereo_bands;
1603 int coded_stereo_dof;
Jean-Marc Valin1122d292012-12-12 15:16:27 -05001604 opus_val16 max_frac;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001605 coded_stereo_bands = IMIN(st->intensity, coded_bands);
1606 coded_stereo_dof = (eBands[coded_stereo_bands]<<LM)-coded_stereo_bands;
Jean-Marc Valin1122d292012-12-12 15:16:27 -05001607 /* Maximum fraction of the bits we can save if the signal is mono. */
1608 max_frac = DIV32_16(MULT16_16(QCONST16(0.8f, 15), coded_stereo_dof), coded_bins);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001609 /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/
Jean-Marc Valind683c762012-12-21 16:17:38 -05001610 target -= (opus_int32)MIN32(MULT16_32_Q15(max_frac,target),
Jean-Marc Valin28ef2772012-12-14 13:21:09 -05001611 SHR16(MULT16_16(st->stereo_saving-QCONST16(0.1f,8),(coded_stereo_dof<<BITRES)),8));
Jean-Marc Valin69062102012-11-08 09:42:27 -05001612 }
Jean-Marc Valinf8809dd2012-12-12 15:22:39 -05001613 /* Boost the rate according to dynalloc (minus the dynalloc average for calibration). */
Jean-Marc Valindae16fb2012-12-13 21:40:58 -05001614 target += tot_boost-(16<<LM);
1615 /* Apply transient boost, compensating for average boost. */
Jean-Marc Valind683c762012-12-21 16:17:38 -05001616 target += (opus_int32)SHL32(MULT16_32_Q15(tf_estimate-QCONST16(0.04f,14), target),1);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001617
1618#ifndef FIXED_POINT
1619 /* Apply tonality boost */
1620 if (st->analysis.valid) {
Jean-Marc Valind683c762012-12-21 16:17:38 -05001621 opus_int32 tonal_target;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001622 float tonal;
1623
Jean-Marc Valin28ef2772012-12-14 13:21:09 -05001624 /* Tonality boost (compensating for the average). */
Jean-Marc Valind683c762012-12-21 16:17:38 -05001625 tonal = MAX16(0.f,st->analysis.tonality-.15f)-0.09f;
1626 tonal_target = target + (opus_int32)((coded_bins<<BITRES)*1.2f*tonal);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001627 if (pitch_change)
Jean-Marc Valind683c762012-12-21 16:17:38 -05001628 tonal_target += (opus_int32)((coded_bins<<BITRES)*.8f);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001629 /*printf("%f %f ", st->analysis.tonality, tonal);*/
Jean-Marc Valinb33db8f2012-12-12 14:45:16 -05001630 target = tonal_target;
Jean-Marc Valin69062102012-11-08 09:42:27 -05001631 }
1632#endif
1633
1634 {
1635 opus_int32 floor_depth;
1636 int bins;
1637 bins = eBands[nbEBands-2]<<LM;
1638 /*floor_depth = SHR32(MULT16_16((C*bins<<BITRES),celt_log2(SHL32(MAX16(1,sample_max),13))), DB_SHIFT);*/
Jean-Marc Valind683c762012-12-21 16:17:38 -05001639 floor_depth = (opus_int32)SHR32(MULT16_16((C*bins<<BITRES),maxDepth), DB_SHIFT);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001640 floor_depth = IMAX(floor_depth, target>>2);
1641 target = IMIN(target, floor_depth);
1642 /*printf("%f %d\n", maxDepth, floor_depth);*/
1643 }
1644
1645 if (st->constrained_vbr || st->bitrate<64000)
1646 {
1647 opus_val16 rate_factor;
1648#ifdef FIXED_POINT
1649 rate_factor = MAX16(0,(st->bitrate-32000));
1650#else
1651 rate_factor = MAX16(0,(1.f/32768)*(st->bitrate-32000));
1652#endif
1653 if (st->constrained_vbr)
1654 rate_factor = MIN16(rate_factor, QCONST16(0.67f, 15));
Jean-Marc Valind683c762012-12-21 16:17:38 -05001655 target = base_target + (opus_int32)MULT16_32_Q15(rate_factor, target-base_target);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001656
1657 }
1658 /* Don't allow more than doubling the rate */
1659 target = IMIN(2*base_target, target);
1660
1661 /* The current offset is removed from the target and the space used
1662 so far is added*/
1663 target=target+tell;
1664 /* In VBR mode the frame size must not be reduced so much that it would
1665 result in the encoder running out of bits.
1666 The margin of 2 bytes ensures that none of the bust-prevention logic
1667 in the decoder will have triggered so far. */
1668 min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFilledBytes;
1669
1670 nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
1671 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
1672 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
1673
1674 /* By how much did we "miss" the target on that frame */
1675 delta = target - vbr_rate;
1676
1677 target=nbAvailableBytes<<(BITRES+3);
1678
1679 /*If the frame is silent we don't adjust our drift, otherwise
1680 the encoder will shoot to very high rates after hitting a
1681 span of silence, but we do allow the bitres to refill.
1682 This means that we'll undershoot our target in CVBR/VBR modes
1683 on files with lots of silence. */
1684 if(silence)
1685 {
1686 nbAvailableBytes = 2;
1687 target = 2*8<<BITRES;
1688 delta = 0;
1689 }
1690
1691 if (st->vbr_count < 970)
1692 {
1693 st->vbr_count++;
1694 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
1695 } else
1696 alpha = QCONST16(.001f,15);
1697 /* How many bits have we used in excess of what we're allowed */
1698 if (st->constrained_vbr)
1699 st->vbr_reservoir += target - vbr_rate;
1700 /*printf ("%d\n", st->vbr_reservoir);*/
1701
1702 /* Compute the offset we need to apply in order to reach the target */
1703 if (st->constrained_vbr)
1704 {
1705 st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift);
1706 st->vbr_offset = -st->vbr_drift;
1707 }
1708 /*printf ("%d\n", st->vbr_drift);*/
1709
1710 if (st->constrained_vbr && st->vbr_reservoir < 0)
1711 {
1712 /* We're under the min value -- increase rate */
1713 int adjust = (-st->vbr_reservoir)/(8<<BITRES);
1714 /* Unless we're just coding silence */
1715 nbAvailableBytes += silence?0:adjust;
1716 st->vbr_reservoir = 0;
1717 /*printf ("+%d\n", adjust);*/
1718 }
1719 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
1720 /*printf("%d\n", nbCompressedBytes*50*8);*/
1721 /* This moves the raw bits to take into account the new compressed size */
1722 ec_enc_shrink(enc, nbCompressedBytes);
1723 }
1724
1725 /* Bit allocation */
1726 ALLOC(fine_quant, nbEBands, int);
1727 ALLOC(pulses, nbEBands, int);
1728 ALLOC(fine_priority, nbEBands, int);
1729
1730 /* bits = packet size - where we are - safety*/
1731 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
1732 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1733 bits -= anti_collapse_rsv;
Jean-Marc Valin5fb50ad2012-12-21 01:23:26 -05001734 signalBandwidth = st->end-1;
1735#ifndef FIXED_POINT
1736 if (st->analysis.valid)
1737 signalBandwidth = st->analysis.bandwidth;
1738#endif
Jean-Marc Valin69062102012-11-08 09:42:27 -05001739 codedBands = compute_allocation(mode, st->start, st->end, offsets, cap,
1740 alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses,
Jean-Marc Valin5fb50ad2012-12-21 01:23:26 -05001741 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBandwidth);
Jean-Marc Valin69062102012-11-08 09:42:27 -05001742 st->lastCodedBands = codedBands;
1743
1744 quant_fine_energy(mode, st->start, st->end, oldBandE, error, fine_quant, enc, C);
1745
1746#ifdef MEASURE_NORM_MSE
1747 float X0[3000];
1748 float bandE0[60];
1749 c=0; do
1750 for (i=0;i<N;i++)
1751 X0[i+c*N] = X[i+c*N];
1752 while (++c<C);
1753 for (i=0;i<C*nbEBands;i++)
1754 bandE0[i] = bandE[i];
1755#endif
1756
1757 /* Residual quantisation */
1758 ALLOC(collapse_masks, C*nbEBands, unsigned char);
1759 quant_all_bands(1, mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_masks,
1760 bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, st->intensity, tf_res,
1761 nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, codedBands, &st->rng);
1762
1763 if (anti_collapse_rsv > 0)
1764 {
1765 anti_collapse_on = st->consec_transient<2;
1766#ifdef FUZZING
1767 anti_collapse_on = rand()&0x1;
1768#endif
1769 ec_enc_bits(enc, anti_collapse_on, 1);
1770 }
1771 quant_energy_finalise(mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
1772
1773 if (silence)
1774 {
1775 for (i=0;i<C*nbEBands;i++)
1776 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
1777 }
1778
1779#ifdef RESYNTH
1780 /* Re-synthesis of the coded audio if required */
1781 {
1782 celt_sig *out_mem[2];
1783
1784 log2Amp(mode, st->start, st->end, bandE, oldBandE, C);
1785 if (silence)
1786 {
1787 for (i=0;i<C*nbEBands;i++)
1788 bandE[i] = 0;
1789 }
1790
1791#ifdef MEASURE_NORM_MSE
1792 measure_norm_mse(mode, X, X0, bandE, bandE0, M, N, C);
1793#endif
1794 if (anti_collapse_on)
1795 {
1796 anti_collapse(mode, X, collapse_masks, LM, C, N,
1797 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng);
1798 }
1799
1800 /* Synthesis */
1801 denormalise_bands(mode, X, freq, bandE, st->start, effEnd, C, M);
1802
1803 c=0; do {
Nils Wallméniuse0884fe2012-12-01 21:11:50 +01001804 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 -05001805 } while (++c<CC);
1806
1807 if (CC==2&&C==1)
1808 {
1809 for (i=0;i<N;i++)
1810 freq[N+i] = freq[i];
1811 }
1812
1813 c=0; do {
1814 out_mem[c] = st->syn_mem[c]+2*MAX_PERIOD-N;
1815 } while (++c<CC);
1816
1817 compute_inv_mdcts(mode, shortBlocks, freq, out_mem, CC, LM);
1818
1819 c=0; do {
1820 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
1821 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD);
1822 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, mode->shortMdctSize,
1823 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
1824 mode->window, st->overlap);
1825 if (LM!=0)
1826 comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMdctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize,
1827 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
1828 mode->window, overlap);
1829 } while (++c<CC);
1830
1831 /* We reuse freq[] as scratch space for the de-emphasis */
1832 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, mode->preemph, st->preemph_memD, freq);
1833 st->prefilter_period_old = st->prefilter_period;
1834 st->prefilter_gain_old = st->prefilter_gain;
1835 st->prefilter_tapset_old = st->prefilter_tapset;
1836 }
1837#endif
1838
1839 st->prefilter_period = pitch_index;
1840 st->prefilter_gain = gain1;
1841 st->prefilter_tapset = prefilter_tapset;
1842#ifdef RESYNTH
1843 if (LM!=0)
1844 {
1845 st->prefilter_period_old = st->prefilter_period;
1846 st->prefilter_gain_old = st->prefilter_gain;
1847 st->prefilter_tapset_old = st->prefilter_tapset;
1848 }
1849#endif
1850
1851 if (CC==2&&C==1) {
1852 for (i=0;i<nbEBands;i++)
1853 oldBandE[nbEBands+i]=oldBandE[i];
1854 }
1855
1856 if (!isTransient)
1857 {
1858 for (i=0;i<CC*nbEBands;i++)
1859 oldLogE2[i] = oldLogE[i];
1860 for (i=0;i<CC*nbEBands;i++)
1861 oldLogE[i] = oldBandE[i];
1862 } else {
1863 for (i=0;i<CC*nbEBands;i++)
1864 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
1865 }
1866 /* In case start or end were to change */
1867 c=0; do
1868 {
1869 for (i=0;i<st->start;i++)
1870 {
1871 oldBandE[c*nbEBands+i]=0;
1872 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1873 }
1874 for (i=st->end;i<nbEBands;i++)
1875 {
1876 oldBandE[c*nbEBands+i]=0;
1877 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
1878 }
1879 } while (++c<CC);
1880
Jean-Marc Valin3252bf22013-04-19 03:14:28 -04001881 if (isTransient || transient_got_disabled)
Jean-Marc Valin69062102012-11-08 09:42:27 -05001882 st->consec_transient++;
1883 else
1884 st->consec_transient=0;
1885 st->rng = enc->rng;
1886
1887 /* If there's any room left (can only happen for very high rates),
1888 it's already filled with zeros */
1889 ec_enc_done(enc);
1890
1891#ifdef CUSTOM_MODES
1892 if (st->signalling)
1893 nbCompressedBytes++;
1894#endif
1895
1896 RESTORE_STACK;
1897 if (ec_get_error(enc))
1898 return OPUS_INTERNAL_ERROR;
1899 else
1900 return nbCompressedBytes;
1901}
1902
1903
1904#ifdef CUSTOM_MODES
1905
1906#ifdef FIXED_POINT
1907int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1908{
1909 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1910}
1911
1912#ifndef DISABLE_FLOAT_API
1913int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1914{
1915 int j, ret, C, N;
1916 VARDECL(opus_int16, in);
1917 ALLOC_STACK;
1918
1919 if (pcm==NULL)
1920 return OPUS_BAD_ARG;
1921
1922 C = st->channels;
1923 N = frame_size;
1924 ALLOC(in, C*N, opus_int16);
1925
1926 for (j=0;j<C*N;j++)
1927 in[j] = FLOAT2INT16(pcm[j]);
1928
1929 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1930#ifdef RESYNTH
1931 for (j=0;j<C*N;j++)
1932 ((float*)pcm)[j]=in[j]*(1.f/32768.f);
1933#endif
1934 RESTORE_STACK;
1935 return ret;
1936}
1937#endif /* DISABLE_FLOAT_API */
1938#else
1939
1940int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1941{
1942 int j, ret, C, N;
1943 VARDECL(celt_sig, in);
1944 ALLOC_STACK;
1945
1946 if (pcm==NULL)
1947 return OPUS_BAD_ARG;
1948
1949 C=st->channels;
1950 N=frame_size;
1951 ALLOC(in, C*N, celt_sig);
1952 for (j=0;j<C*N;j++) {
1953 in[j] = SCALEOUT(pcm[j]);
1954 }
1955
1956 ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
1957#ifdef RESYNTH
1958 for (j=0;j<C*N;j++)
1959 ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
1960#endif
1961 RESTORE_STACK;
1962 return ret;
1963}
1964
1965int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
1966{
1967 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL);
1968}
1969
1970#endif
1971
1972#endif /* CUSTOM_MODES */
1973
1974int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...)
1975{
1976 va_list ap;
1977
1978 va_start(ap, request);
1979 switch (request)
1980 {
1981 case OPUS_SET_COMPLEXITY_REQUEST:
1982 {
1983 int value = va_arg(ap, opus_int32);
1984 if (value<0 || value>10)
1985 goto bad_arg;
1986 st->complexity = value;
1987 }
1988 break;
1989 case CELT_SET_START_BAND_REQUEST:
1990 {
1991 opus_int32 value = va_arg(ap, opus_int32);
1992 if (value<0 || value>=st->mode->nbEBands)
1993 goto bad_arg;
1994 st->start = value;
1995 }
1996 break;
1997 case CELT_SET_END_BAND_REQUEST:
1998 {
1999 opus_int32 value = va_arg(ap, opus_int32);
2000 if (value<1 || value>st->mode->nbEBands)
2001 goto bad_arg;
2002 st->end = value;
2003 }
2004 break;
2005 case CELT_SET_PREDICTION_REQUEST:
2006 {
2007 int value = va_arg(ap, opus_int32);
2008 if (value<0 || value>2)
2009 goto bad_arg;
2010 st->disable_pf = value<=1;
2011 st->force_intra = value==0;
2012 }
2013 break;
2014 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2015 {
2016 int value = va_arg(ap, opus_int32);
2017 if (value<0 || value>100)
2018 goto bad_arg;
2019 st->loss_rate = value;
2020 }
2021 break;
2022 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2023 {
2024 opus_int32 value = va_arg(ap, opus_int32);
2025 st->constrained_vbr = value;
2026 }
2027 break;
2028 case OPUS_SET_VBR_REQUEST:
2029 {
2030 opus_int32 value = va_arg(ap, opus_int32);
2031 st->vbr = value;
2032 }
2033 break;
2034 case OPUS_SET_BITRATE_REQUEST:
2035 {
2036 opus_int32 value = va_arg(ap, opus_int32);
2037 if (value<=500 && value!=OPUS_BITRATE_MAX)
2038 goto bad_arg;
2039 value = IMIN(value, 260000*st->channels);
2040 st->bitrate = value;
2041 }
2042 break;
2043 case CELT_SET_CHANNELS_REQUEST:
2044 {
2045 opus_int32 value = va_arg(ap, opus_int32);
2046 if (value<1 || value>2)
2047 goto bad_arg;
2048 st->stream_channels = value;
2049 }
2050 break;
2051 case OPUS_SET_LSB_DEPTH_REQUEST:
2052 {
2053 opus_int32 value = va_arg(ap, opus_int32);
2054 if (value<8 || value>24)
2055 goto bad_arg;
2056 st->lsb_depth=value;
2057 }
2058 break;
2059 case OPUS_GET_LSB_DEPTH_REQUEST:
2060 {
2061 opus_int32 *value = va_arg(ap, opus_int32*);
2062 *value=st->lsb_depth;
2063 }
2064 break;
Jean-Marc Valin3252bf22013-04-19 03:14:28 -04002065 case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
2066 {
2067 opus_int32 value = va_arg(ap, opus_int32);
2068 st->variable_duration = value;
2069 }
2070 break;
Jean-Marc Valin69062102012-11-08 09:42:27 -05002071 case OPUS_RESET_STATE:
2072 {
2073 int i;
2074 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
2075 oldBandE = (opus_val16*)(st->in_mem+st->channels*(st->overlap+COMBFILTER_MAXPERIOD));
2076 oldLogE = oldBandE + st->channels*st->mode->nbEBands;
2077 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
2078 OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
2079 opus_custom_encoder_get_size(st->mode, st->channels)-
2080 ((char*)&st->ENCODER_RESET_START - (char*)st));
2081 for (i=0;i<st->channels*st->mode->nbEBands;i++)
2082 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
2083 st->vbr_offset = 0;
2084 st->delayedIntra = 1;
2085 st->spread_decision = SPREAD_NORMAL;
2086 st->tonal_average = 256;
2087 st->hf_average = 0;
2088 st->tapset_decision = 0;
2089 }
2090 break;
2091#ifdef CUSTOM_MODES
2092 case CELT_SET_INPUT_CLIPPING_REQUEST:
2093 {
2094 opus_int32 value = va_arg(ap, opus_int32);
2095 st->clip = value;
2096 }
2097 break;
2098#endif
2099 case CELT_SET_SIGNALLING_REQUEST:
2100 {
2101 opus_int32 value = va_arg(ap, opus_int32);
2102 st->signalling = value;
2103 }
2104 break;
2105 case CELT_SET_ANALYSIS_REQUEST:
2106 {
2107 AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
2108 if (info)
2109 OPUS_COPY(&st->analysis, info, 1);
2110 }
2111 break;
2112 case CELT_GET_MODE_REQUEST:
2113 {
2114 const CELTMode ** value = va_arg(ap, const CELTMode**);
2115 if (value==0)
2116 goto bad_arg;
2117 *value=st->mode;
2118 }
2119 break;
2120 case OPUS_GET_FINAL_RANGE_REQUEST:
2121 {
2122 opus_uint32 * value = va_arg(ap, opus_uint32 *);
2123 if (value==0)
2124 goto bad_arg;
2125 *value=st->rng;
2126 }
2127 break;
2128 default:
2129 goto bad_request;
2130 }
2131 va_end(ap);
2132 return OPUS_OK;
2133bad_arg:
2134 va_end(ap);
2135 return OPUS_BAD_ARG;
2136bad_request:
2137 va_end(ap);
2138 return OPUS_UNIMPLEMENTED;
2139}