Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(93)

Side by Side Diff: celt/celt_encoder.c

Issue 28553003: Updating Opus to a pre-release of 1.1 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/deps/third_party/opus
Patch Set: Removing failing file Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « celt/celt_decoder.c ('k') | celt/celt_lpc.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /* Copyright (c) 2007-2008 CSIRO
2 Copyright (c) 2007-2010 Xiph.Org Foundation
3 Copyright (c) 2008 Gregory Maxwell
4 Written by Jean-Marc Valin and Gregory Maxwell */
5 /*
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9
10 - Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12
13 - Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
21 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
24 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
25 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
26 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #define CELT_ENCODER_C
35
36 #include "cpu_support.h"
37 #include "os_support.h"
38 #include "mdct.h"
39 #include <math.h>
40 #include "celt.h"
41 #include "pitch.h"
42 #include "bands.h"
43 #include "modes.h"
44 #include "entcode.h"
45 #include "quant_bands.h"
46 #include "rate.h"
47 #include "stack_alloc.h"
48 #include "mathops.h"
49 #include "float_cast.h"
50 #include <stdarg.h>
51 #include "celt_lpc.h"
52 #include "vq.h"
53
54
55 /** Encoder state
56 @brief Encoder state
57 */
58 struct OpusCustomEncoder {
59 const OpusCustomMode *mode; /**< Mode used by the encoder */
60 int 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 r ate */
75 int loss_rate;
76 int lsb_depth;
77 int variable_duration;
78 int lfe;
79 int arch;
80
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;
111 opus_val32 overlap_max;
112 opus_val16 stereo_saving;
113 int intensity;
114 opus_val16 *energy_mask;
115 opus_val16 spec_avg;
116
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
129 int 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
135 OPUS_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[cha nnels*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
147 CELTEncoder *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
164 int 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
174 OPUS_CUSTOM_NOSTATIC int opus_custom_encoder_init(CELTEncoder *st, const CELTMod e *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
193 st->arch = opus_select_arch();
194
195 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
210 void opus_custom_encoder_destroy(CELTEncoder *st)
211 {
212 opus_free(st);
213 }
214 #endif /* CUSTOM_MODES */
215
216
217 static 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;
226 opus_val16 tf_max;
227 int len2;
228 /* 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
242 len2=len/2;
243 tf_max = 0;
244 for (c=0;c<C;c++)
245 {
246 opus_val32 mean;
247 opus_int32 unmask=0;
248 opus_val32 norm;
249 opus_val16 maxE;
250 mem0=0;
251 mem1=0;
252 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
253 for (i=0;i<len;i++)
254 {
255 opus_val32 x,y;
256 x = SHR32(in[i+c*len],SIG_SHIFT);
257 y = ADD32(mem0, x);
258 #ifdef FIXED_POINT
259 mem0 = mem1 + y - SHL32(x,1);
260 mem1 = x - SHR32(y,1);
261 #else
262 mem0 = mem1 + y - 2*x;
263 mem1 = x - .5f*y;
264 #endif
265 tmp[i] = EXTRACT16(SHR32(y,2));
266 /*printf("%f ", tmp[i]);*/
267 }
268 /*printf("\n");*/
269 /* First few samples are bad because we don't propagate the memory */
270 for (i=0;i<12;i++)
271 tmp[i] = 0;
272
273 #ifdef FIXED_POINT
274 /* Normalize tmp to max range */
275 {
276 int shift=0;
277 shift = 14-celt_ilog2(1+celt_maxabs16(tmp, len));
278 if (shift!=0)
279 {
280 for (i=0;i<len;i++)
281 tmp[i] = SHL16(tmp[i], shift);
282 }
283 }
284 #endif
285
286 mean=0;
287 mem0=0;
288 /* Grouping by two to reduce complexity */
289 /* Forward pass to compute the post-echo threshold*/
290 for (i=0;i<len2;i++)
291 {
292 opus_val16 x2 = PSHR32(MULT16_16(tmp[2*i],tmp[2*i]) + MULT16_16(tmp[2*i +1],tmp[2*i+1]),16);
293 mean += x2;
294 #ifdef FIXED_POINT
295 /* FIXME: Use PSHR16() instead */
296 tmp[i] = mem0 + PSHR32(x2-mem0,4);
297 #else
298 tmp[i] = mem0 + MULT16_16_P15(QCONST16(.0625f,15),x2-mem0);
299 #endif
300 mem0 = tmp[i];
301 }
302
303 mem0=0;
304 maxE=0;
305 /* Backward pass to compute the pre-echo threshold */
306 for (i=len2-1;i>=0;i--)
307 {
308 #ifdef FIXED_POINT
309 /* FIXME: Use PSHR16() instead */
310 tmp[i] = mem0 + PSHR32(tmp[i]-mem0,3);
311 #else
312 tmp[i] = mem0 + MULT16_16_P15(QCONST16(0.125f,15),tmp[i]-mem0);
313 #endif
314 mem0 = tmp[i];
315 maxE = MAX16(maxE, mem0);
316 }
317 /*for (i=0;i<len2;i++)printf("%f ", tmp[i]/mean);printf("\n");*/
318
319 /* Compute the ratio of the "frame energy" over the harmonic mean of the e nergy.
320 This essentially corresponds to a bitrate-normalized temporal noise-to- mask
321 ratio */
322
323 /* As a compromise with the old transient detector, frame energy is the
324 geometric mean of the energy and half the max */
325 #ifdef FIXED_POINT
326 /* Costs two sqrt() to avoid overflows */
327 mean = MULT16_16(celt_sqrt(mean), celt_sqrt(MULT16_16(maxE,len2>>1)));
328 #else
329 mean = celt_sqrt(mean * maxE*.5*len2);
330 #endif
331 /* Inverse of the mean energy in Q15+6 */
332 norm = SHL32(EXTEND32(len2),6+14)/ADD32(EPSILON,SHR32(mean,1));
333 /* Compute harmonic mean discarding the unreliable boundaries
334 The data is smooth, so we only take 1/4th of the samples */
335 unmask=0;
336 for (i=12;i<len2-5;i+=4)
337 {
338 int id;
339 #ifdef FIXED_POINT
340 id = IMAX(0,IMIN(127,MULT16_32_Q15(tmp[i],norm))); /* Do not round to n earest */
341 #else
342 id = IMAX(0,IMIN(127,(int)floor(64*norm*tmp[i]))); /* Do not round to n earest */
343 #endif
344 unmask += inv_table[id];
345 }
346 /*printf("%d\n", unmask);*/
347 /* Normalize, compensate for the 1/4th of the sample and the factor of 6 i n the inverse table */
348 unmask = 64*unmask*4/(6*(len2-17));
349 if (unmask>mask_metric)
350 {
351 *tf_chan = c;
352 mask_metric = unmask;
353 }
354 }
355 is_transient = mask_metric>200;
356
357 /* Arbitrary metric for VBR boost */
358 tf_max = MAX16(0,celt_sqrt(27*mask_metric)-42);
359 /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */
360 *tf_estimate = celt_sqrt(MAX16(0, SHL32(MULT16_16(QCONST16(0.0069,14),MIN16(1 63,tf_max)),14)-QCONST32(0.139,28)));
361 /*printf("%d %f\n", tf_max, mask_metric);*/
362 RESTORE_STACK;
363 #ifdef FUZZING
364 is_transient = rand()&0x1;
365 #endif
366 /*printf("%d %f %d\n", is_transient, (float)*tf_estimate, tf_max);*/
367 return is_transient;
368 }
369
370 /* Looks for sudden increases of energy to decide whether we need to patch
371 the transient decision */
372 int patch_transient_decision(opus_val16 *newE, opus_val16 *oldE, int nbEBands,
373 int end, int C)
374 {
375 int i, c;
376 opus_val32 mean_diff=0;
377 opus_val16 spread_old[26];
378 /* Apply an aggressive (-6 dB/Bark) spreading function to the old frame to
379 avoid false detection caused by irrelevant bands */
380 if (C==1)
381 {
382 spread_old[0] = oldE[0];
383 for (i=1;i<end;i++)
384 spread_old[i] = MAX16(spread_old[i-1]-QCONST16(1.0f, DB_SHIFT), oldE[i] );
385 } else {
386 spread_old[0] = MAX16(oldE[0],oldE[nbEBands]);
387 for (i=1;i<end;i++)
388 spread_old[i] = MAX16(spread_old[i-1]-QCONST16(1.0f, DB_SHIFT),
389 MAX16(oldE[i],oldE[i+nbEBands]));
390 }
391 for (i=end-2;i>=0;i--)
392 spread_old[i] = MAX16(spread_old[i], spread_old[i+1]-QCONST16(1.0f, DB_SHI FT));
393 /* Compute mean increase */
394 c=0; do {
395 for (i=2;i<end-1;i++)
396 {
397 opus_val16 x1, x2;
398 x1 = MAX16(0, newE[i]);
399 x2 = MAX16(0, spread_old[i]);
400 mean_diff = ADD32(mean_diff, EXTEND32(MAX16(0, SUB16(x1, x2))));
401 }
402 } while (++c<C);
403 mean_diff = DIV32(mean_diff, C*(end-3));
404 /*printf("%f %f %d\n", mean_diff, max_diff, count);*/
405 return mean_diff > QCONST16(1.f, DB_SHIFT);
406 }
407
408 /** Apply window and compute the MDCT for all sub-frames and
409 all channels in a frame */
410 static void compute_mdcts(const CELTMode *mode, int shortBlocks, celt_sig * OPUS _RESTRICT in,
411 celt_sig * OPUS_RESTRICT out, int C, int CC, int LM, i nt upsample)
412 {
413 const int overlap = OVERLAP(mode);
414 int N;
415 int B;
416 int shift;
417 int i, b, c;
418 if (shortBlocks)
419 {
420 B = shortBlocks;
421 N = mode->shortMdctSize;
422 shift = mode->maxLM;
423 } else {
424 B = 1;
425 N = mode->shortMdctSize<<LM;
426 shift = mode->maxLM-LM;
427 }
428 c=0; do {
429 for (b=0;b<B;b++)
430 {
431 /* Interleaving the sub-frames while doing the MDCTs */
432 clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N, &out[b+c*N*B], mo de->window, overlap, shift, B);
433 }
434 } while (++c<CC);
435 if (CC==2&&C==1)
436 {
437 for (i=0;i<B*N;i++)
438 out[i] = ADD32(HALF32(out[i]), HALF32(out[B*N+i]));
439 }
440 if (upsample != 1)
441 {
442 c=0; do
443 {
444 int bound = B*N/upsample;
445 for (i=0;i<bound;i++)
446 out[c*B*N+i] *= upsample;
447 for (;i<B*N;i++)
448 out[c*B*N+i] = 0;
449 } while (++c<C);
450 }
451 }
452
453
454 void preemphasis(const opus_val16 * OPUS_RESTRICT pcmp, celt_sig * OPUS_RESTRICT inp,
455 int N, int CC, int upsample, const opus_val16 *coef, cel t_sig *mem, int clip)
456 {
457 int i;
458 opus_val16 coef0;
459 celt_sig m;
460 int Nu;
461
462 coef0 = coef[0];
463
464
465 Nu = N/upsample;
466 if (upsample!=1)
467 {
468 for (i=0;i<N;i++)
469 inp[i] = 0;
470 }
471 for (i=0;i<Nu;i++)
472 {
473 celt_sig x;
474
475 x = SCALEIN(pcmp[CC*i]);
476 #ifndef FIXED_POINT
477 /* Replace NaNs with zeros */
478 if (!(x==x))
479 x = 0;
480 #endif
481 inp[i*upsample] = x;
482 }
483
484 #ifndef FIXED_POINT
485 if (clip)
486 {
487 /* Clip input to avoid encoding non-portable files */
488 for (i=0;i<Nu;i++)
489 inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample]));
490 }
491 #endif
492 m = *mem;
493 #ifdef CUSTOM_MODES
494 if (coef[1] != 0)
495 {
496 opus_val16 coef1 = coef[1];
497 opus_val16 coef2 = coef[2];
498 for (i=0;i<N;i++)
499 {
500 opus_val16 x, tmp;
501 x = inp[i];
502 /* Apply pre-emphasis */
503 tmp = MULT16_16(coef2, x);
504 inp[i] = tmp + m;
505 m = MULT16_32_Q15(coef1, inp[i]) - MULT16_32_Q15(coef0, tmp);
506 }
507 } else
508 #endif
509 {
510 for (i=0;i<N;i++)
511 {
512 celt_sig x;
513 x = SHL32(inp[i], SIG_SHIFT);
514 /* Apply pre-emphasis */
515 inp[i] = x + m;
516 m = - MULT16_32_Q15(coef0, x);
517 }
518 }
519 *mem = m;
520 }
521
522
523
524 static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, opus_val16 bias )
525 {
526 int i;
527 opus_val32 L1;
528 L1 = 0;
529 for (i=0;i<N;i++)
530 L1 += EXTEND32(ABS16(tmp[i]));
531 /* When in doubt, prefer good freq resolution */
532 L1 = MAC16_32_Q15(L1, LM*bias, L1);
533 return L1;
534
535 }
536
537 static int tf_analysis(const CELTMode *m, int len, int isTransient,
538 int *tf_res, int lambda, celt_norm *X, int N0, int LM,
539 int *tf_sum, opus_val16 tf_estimate, int tf_chan)
540 {
541 int i;
542 VARDECL(int, metric);
543 int cost0;
544 int cost1;
545 VARDECL(int, path0);
546 VARDECL(int, path1);
547 VARDECL(celt_norm, tmp);
548 VARDECL(celt_norm, tmp_1);
549 int sel;
550 int selcost[2];
551 int tf_select=0;
552 opus_val16 bias;
553
554 SAVE_STACK;
555 bias = MULT16_16_Q14(QCONST16(.04f,15), MAX16(-QCONST16(.25f,14), QCONST16(.5 f,14)-tf_estimate));
556 /*printf("%f ", bias);*/
557
558 ALLOC(metric, len, int);
559 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
560 ALLOC(tmp_1, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm);
561 ALLOC(path0, len, int);
562 ALLOC(path1, len, int);
563
564 *tf_sum = 0;
565 for (i=0;i<len;i++)
566 {
567 int j, k, N;
568 int narrow;
569 opus_val32 L1, best_L1;
570 int best_level=0;
571 N = (m->eBands[i+1]-m->eBands[i])<<LM;
572 /* band is too narrow to be split down to LM=-1 */
573 narrow = (m->eBands[i+1]-m->eBands[i])==1;
574 for (j=0;j<N;j++)
575 tmp[j] = X[tf_chan*N0 + j+(m->eBands[i]<<LM)];
576 /* Just add the right channel if we're in stereo */
577 /*if (C==2)
578 for (j=0;j<N;j++)
579 tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1) );*/
580 L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias);
581 best_L1 = L1;
582 /* Check the -1 case for transients */
583 if (isTransient && !narrow)
584 {
585 for (j=0;j<N;j++)
586 tmp_1[j] = tmp[j];
587 haar1(tmp_1, N>>LM, 1<<LM);
588 L1 = l1_metric(tmp_1, N, LM+1, bias);
589 if (L1<best_L1)
590 {
591 best_L1 = L1;
592 best_level = -1;
593 }
594 }
595 /*printf ("%f ", L1);*/
596 for (k=0;k<LM+!(isTransient||narrow);k++)
597 {
598 int B;
599
600 if (isTransient)
601 B = (LM-k-1);
602 else
603 B = k+1;
604
605 haar1(tmp, N>>k, 1<<k);
606
607 L1 = l1_metric(tmp, N, B, bias);
608
609 if (L1 < best_L1)
610 {
611 best_L1 = L1;
612 best_level = k+1;
613 }
614 }
615 /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
616 /* metric is in Q1 to be able to select the mid-point (-0.5) for narrower bands */
617 if (isTransient)
618 metric[i] = 2*best_level;
619 else
620 metric[i] = -2*best_level;
621 *tf_sum += (isTransient ? LM : 0) - metric[i]/2;
622 /* For bands that can't be split to -1, set the metric to the half-way poi nt to avoid
623 biasing the decision */
624 if (narrow && (metric[i]==0 || metric[i]==-2*LM))
625 metric[i]-=1;
626 /*printf("%d ", metric[i]);*/
627 }
628 /*printf("\n");*/
629 /* Search for the optimal tf resolution, including tf_select */
630 tf_select = 0;
631 for (sel=0;sel<2;sel++)
632 {
633 cost0 = 0;
634 cost1 = isTransient ? 0 : lambda;
635 for (i=1;i<len;i++)
636 {
637 int curr0, curr1;
638 curr0 = IMIN(cost0, cost1 + lambda);
639 curr1 = IMIN(cost0 + lambda, cost1);
640 cost0 = curr0 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel +0]);
641 cost1 = curr1 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel +1]);
642 }
643 cost0 = IMIN(cost0, cost1);
644 selcost[sel]=cost0;
645 }
646 /* For now, we're conservative and only allow tf_select=1 for transients.
647 * If tests confirm it's useful for non-transients, we could allow it. */
648 if (selcost[1]<selcost[0] && isTransient)
649 tf_select=1;
650 cost0 = 0;
651 cost1 = isTransient ? 0 : lambda;
652 /* Viterbi forward pass */
653 for (i=1;i<len;i++)
654 {
655 int curr0, curr1;
656 int from0, from1;
657
658 from0 = cost0;
659 from1 = cost1 + lambda;
660 if (from0 < from1)
661 {
662 curr0 = from0;
663 path0[i]= 0;
664 } else {
665 curr0 = from1;
666 path0[i]= 1;
667 }
668
669 from0 = cost0 + lambda;
670 from1 = cost1;
671 if (from0 < from1)
672 {
673 curr1 = from0;
674 path1[i]= 0;
675 } else {
676 curr1 = from1;
677 path1[i]= 1;
678 }
679 cost0 = curr0 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_sel ect+0]);
680 cost1 = curr1 + abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_sel ect+1]);
681 }
682 tf_res[len-1] = cost0 < cost1 ? 0 : 1;
683 /* Viterbi backward pass to check the decisions */
684 for (i=len-2;i>=0;i--)
685 {
686 if (tf_res[i+1] == 1)
687 tf_res[i] = path1[i+1];
688 else
689 tf_res[i] = path0[i+1];
690 }
691 /*printf("%d %f\n", *tf_sum, tf_estimate);*/
692 RESTORE_STACK;
693 #ifdef FUZZING
694 tf_select = rand()&0x1;
695 tf_res[0] = rand()&0x1;
696 for (i=1;i<len;i++)
697 tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
698 #endif
699 return tf_select;
700 }
701
702 static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
703 {
704 int curr, i;
705 int tf_select_rsv;
706 int tf_changed;
707 int logp;
708 opus_uint32 budget;
709 opus_uint32 tell;
710 budget = enc->storage*8;
711 tell = ec_tell(enc);
712 logp = isTransient ? 2 : 4;
713 /* Reserve space to code the tf_select decision. */
714 tf_select_rsv = LM>0 && tell+logp+1 <= budget;
715 budget -= tf_select_rsv;
716 curr = tf_changed = 0;
717 for (i=start;i<end;i++)
718 {
719 if (tell+logp<=budget)
720 {
721 ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
722 tell = ec_tell(enc);
723 curr = tf_res[i];
724 tf_changed |= curr;
725 }
726 else
727 tf_res[i] = curr;
728 logp = isTransient ? 4 : 5;
729 }
730 /* Only code tf_select if it would actually make a difference. */
731 if (tf_select_rsv &&
732 tf_select_table[LM][4*isTransient+0+tf_changed]!=
733 tf_select_table[LM][4*isTransient+2+tf_changed])
734 ec_enc_bit_logp(enc, tf_select, 1);
735 else
736 tf_select = 0;
737 for (i=start;i<end;i++)
738 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
739 /*for(i=0;i<end;i++)printf("%d ", isTransient ? tf_res[i] : LM+tf_res[i]);pri ntf("\n");*/
740 }
741
742
743 static int alloc_trim_analysis(const CELTMode *m, const celt_norm *X,
744 const opus_val16 *bandLogE, int end, int LM, int C, int N0,
745 AnalysisInfo *analysis, opus_val16 *stereo_saving, opus_val16 tf_estimate,
746 int intensity, opus_val16 surround_trim)
747 {
748 int i;
749 opus_val32 diff=0;
750 int c;
751 int trim_index = 5;
752 opus_val16 trim = QCONST16(5.f, 8);
753 opus_val16 logXC, logXC2;
754 if (C==2)
755 {
756 opus_val16 sum = 0; /* Q10 */
757 opus_val16 minXC; /* Q10 */
758 /* Compute inter-channel correlation for low frequencies */
759 for (i=0;i<8;i++)
760 {
761 int j;
762 opus_val32 partial = 0;
763 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
764 partial = MAC16_16(partial, X[j], X[N0+j]);
765 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)));
766 }
767 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum);
768 sum = MIN16(QCONST16(1.f, 10), ABS16(sum));
769 minXC = sum;
770 for (i=8;i<intensity;i++)
771 {
772 int j;
773 opus_val32 partial = 0;
774 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
775 partial = MAC16_16(partial, X[j], X[N0+j]);
776 minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18))));
777 }
778 minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC));
779 /*printf ("%f\n", sum);*/
780 if (sum > QCONST16(.995f,10))
781 trim_index-=4;
782 else if (sum > QCONST16(.92f,10))
783 trim_index-=3;
784 else if (sum > QCONST16(.85f,10))
785 trim_index-=2;
786 else if (sum > QCONST16(.8f,10))
787 trim_index-=1;
788 /* mid-side savings estimations based on the LF average*/
789 logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum));
790 /* mid-side savings estimations based on min correlation */
791 logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(min XC, minXC)));
792 #ifdef FIXED_POINT
793 /* Compensate for Q20 vs Q14 input and convert output to Q8 */
794 logXC = PSHR32(logXC-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8);
795 logXC2 = PSHR32(logXC2-QCONST16(6.f, DB_SHIFT),DB_SHIFT-8);
796 #endif
797
798 trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC));
799 *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2 ));
800 }
801
802 /* Estimate spectral tilt */
803 c=0; do {
804 for (i=0;i<end-1;i++)
805 {
806 diff += bandLogE[i+c*m->nbEBands]*(opus_int32)(2+2*i-end);
807 }
808 } while (++c<C);
809 diff /= C*(end-1);
810 /*printf("%f\n", diff);*/
811 if (diff > QCONST16(2.f, DB_SHIFT))
812 trim_index--;
813 if (diff > QCONST16(8.f, DB_SHIFT))
814 trim_index--;
815 if (diff < -QCONST16(4.f, DB_SHIFT))
816 trim_index++;
817 if (diff < -QCONST16(10.f, DB_SHIFT))
818 trim_index++;
819 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), SHR16(diff+QCONST16( 1.f, DB_SHIFT),DB_SHIFT-8)/6 ));
820 trim -= SHR16(surround_trim, DB_SHIFT-8);
821 trim -= 2*SHR16(tf_estimate, 14-8);
822 #ifndef DISABLE_FLOAT_API
823 if (analysis->valid)
824 {
825 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8), QCONST16(2.f, 8)* (analysis->tonality_slope+.05f)));
826 }
827 #endif
828
829 #ifdef FIXED_POINT
830 trim_index = PSHR32(trim, 8);
831 #else
832 trim_index = (int)floor(.5f+trim);
833 #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
845 static 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
877 static opus_val16 dynalloc_analysis(const opus_val16 *bandLogE, const opus_val16 *bandLogE2,
878 int nbEBands, int start, int end, int C, int *offsets, int lsb_depth, cons t opus_int16 *logN,
879 int isTransient, int vbr, int constrained_vbr, const opus_int16 *eBands, i nt LM,
880 int effectiveBytes, opus_int32 *tot_boost_, int lfe, opus_val16 *surround_ dynalloc)
881 {
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 */
893 maxDepth=-QCONST16(31.9f, DB_SHIFT);
894 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 */
908 if (effectiveBytes > 50 && LM>=1 && !lfe)
909 {
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.5 f,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(followe r[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, ba ndLogE[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 }
943 for (i=start;i<end;i++)
944 follower[i] = MAX16(follower[i], surround_dynalloc[i]);
945 /* 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 {
966 boost = (int)SHR32(EXTEND32(follower[i]),DB_SHIFT);
967 boost_bits = boost*width<<BITRES;
968 } else if (width > 48) {
969 boost = (int)SHR32(EXTEND32(follower[i])*8,DB_SHIFT);
970 boost_bits = (boost*width<<BITRES)/8;
971 } else {
972 boost = (int)SHR32(EXTEND32(follower[i])*width/6,DB_SHIFT);
973 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 {
979 opus_int32 cap = ((effectiveBytes/4)<<BITRES<<3);
980 offsets[i] = cap-tot_boost;
981 tot_boost = cap;
982 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
995 static 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 enable d, 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_MAXPERI OD);
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_MINPER IOD,
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.t onality);*/
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_MAXPERI OD,
1095 st->prefilter_period, st->prefilter_period, offset, -st->prefilte r_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_MAX PERIOD+offset,
1099 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -g ain1,
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_MA XPERIOD);
1106 } else {
1107 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFIL TER_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
1119 static 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,
1123 int variable_duration, int lfe, int has_surround_mask, opus_val16 surround _masking,
1124 opus_val16 temporal_vbr)
1125 {
1126 /* The target rate in 8th bits per frame */
1127 opus_int32 target;
1128 int coded_bins;
1129 int coded_bands;
1130 opus_val16 tf_calibration;
1131 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);*/
1145 #ifndef DISABLE_FLOAT_API
1146 if (analysis->valid && analysis->activity<.4)
1147 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);
1159 /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/
1160 target -= (opus_int32)MIN32(MULT16_32_Q15(max_frac,target),
1161 SHR32(MULT16_16(stereo_saving-QCONST16(0.1f,8),(coded_ster eo_dof<<BITRES)),8));
1162 }
1163 /* Boost the rate according to dynalloc (minus the dynalloc average for calib ration). */
1164 target += tot_boost-(16<<LM);
1165 /* Apply transient boost, compensating for average boost. */
1166 tf_calibration = variable_duration==OPUS_FRAMESIZE_VARIABLE ?
1167 QCONST16(0.02f,14) : QCONST16(0.04f,14);
1168 target += (opus_int32)SHL32(MULT16_32_Q15(tf_estimate-tf_calibration, target) ,1);
1169
1170 #ifndef DISABLE_FLOAT_API
1171 /* Apply tonality boost */
1172 if (analysis->valid && !lfe)
1173 {
1174 opus_int32 tonal_target;
1175 float tonal;
1176
1177 /* Tonality boost (compensating for the average). */
1178 tonal = MAX16(0.f,analysis->tonality-.15f)-0.09f;
1179 tonal_target = target + (opus_int32)((coded_bins<<BITRES)*1.2f*tonal);
1180 if (pitch_change)
1181 tonal_target += (opus_int32)((coded_bins<<BITRES)*.8f);
1182 /*printf("%f %f ", analysis->tonality, tonal);*/
1183 target = tonal_target;
1184 }
1185 #endif
1186
1187 if (has_surround_mask&&!lfe)
1188 {
1189 opus_int32 surround_target = target + (opus_int32)SHR32(MULT16_16(surround _masking,coded_bins<<BITRES), DB_SHIFT);
1190 /*printf("%f %d %d %d %d %d %d ", surround_masking, coded_bins, st->end, s t->intensity, surround_target, target, st->bitrate);*/
1191 target = IMAX(target/4, surround_target);
1192 }
1193
1194 {
1195 opus_int32 floor_depth;
1196 int bins;
1197 bins = eBands[nbEBands-2]<<LM;
1198 /*floor_depth = SHR32(MULT16_16((C*bins<<BITRES),celt_log2(SHL32(MAX16(1,s ample_max),13))), DB_SHIFT);*/
1199 floor_depth = (opus_int32)SHR32(MULT16_16((C*bins<<BITRES),maxDepth), DB_S HIFT);
1200 floor_depth = IMAX(floor_depth, target>>2);
1201 target = IMIN(target, floor_depth);
1202 /*printf("%f %d\n", maxDepth, floor_depth);*/
1203 }
1204
1205 if ((!has_surround_mask||lfe) && (constrained_vbr || bitrate<64000))
1206 {
1207 opus_val16 rate_factor;
1208 #ifdef FIXED_POINT
1209 rate_factor = MAX16(0,(bitrate-32000));
1210 #else
1211 rate_factor = MAX16(0,(1.f/32768)*(bitrate-32000));
1212 #endif
1213 if (constrained_vbr)
1214 rate_factor = MIN16(rate_factor, QCONST16(0.67f, 15));
1215 target = base_target + (opus_int32)MULT16_32_Q15(rate_factor, target-base_ target);
1216
1217 }
1218
1219 if (!has_surround_mask && tf_estimate < QCONST16(.2f, 14))
1220 {
1221 opus_val16 amount;
1222 opus_val16 tvbr_factor;
1223 amount = MULT16_16_Q15(QCONST16(.0000031f, 30), IMAX(0, IMIN(32000, 96000- bitrate)));
1224 tvbr_factor = SHR32(MULT16_16(temporal_vbr, amount), DB_SHIFT);
1225 target += (opus_int32)MULT16_32_Q15(tvbr_factor, target);
1226 }
1227
1228 /* Don't allow more than doubling the rate */
1229 target = IMIN(2*base_target, target);
1230
1231 return target;
1232 }
1233
1234 int celt_encode_with_ec(CELTEncoder * OPUS_RESTRICT st, const opus_val16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
1235 {
1236 int i, c, N;
1237 opus_int32 bits;
1238 ec_enc _enc;
1239 VARDECL(celt_sig, in);
1240 VARDECL(celt_sig, freq);
1241 VARDECL(celt_norm, X);
1242 VARDECL(celt_ener, bandE);
1243 VARDECL(opus_val16, bandLogE);
1244 VARDECL(opus_val16, bandLogE2);
1245 VARDECL(int, fine_quant);
1246 VARDECL(opus_val16, error);
1247 VARDECL(int, pulses);
1248 VARDECL(int, cap);
1249 VARDECL(int, offsets);
1250 VARDECL(int, fine_priority);
1251 VARDECL(int, tf_res);
1252 VARDECL(unsigned char, collapse_masks);
1253 celt_sig *prefilter_mem;
1254 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
1255 int shortBlocks=0;
1256 int isTransient=0;
1257 const int CC = st->channels;
1258 const int C = st->stream_channels;
1259 int LM, M;
1260 int tf_select;
1261 int nbFilledBytes, nbAvailableBytes;
1262 int effEnd;
1263 int codedBands;
1264 int tf_sum;
1265 int alloc_trim;
1266 int pitch_index=COMBFILTER_MINPERIOD;
1267 opus_val16 gain1 = 0;
1268 int dual_stereo=0;
1269 int effectiveBytes;
1270 int dynalloc_logp;
1271 opus_int32 vbr_rate;
1272 opus_int32 total_bits;
1273 opus_int32 total_boost;
1274 opus_int32 balance;
1275 opus_int32 tell;
1276 int prefilter_tapset=0;
1277 int pf_on;
1278 int anti_collapse_rsv;
1279 int anti_collapse_on=0;
1280 int silence=0;
1281 int tf_chan = 0;
1282 opus_val16 tf_estimate;
1283 int pitch_change=0;
1284 opus_int32 tot_boost;
1285 opus_val32 sample_max;
1286 opus_val16 maxDepth;
1287 const OpusCustomMode *mode;
1288 int nbEBands;
1289 int overlap;
1290 const opus_int16 *eBands;
1291 int secondMdct;
1292 int signalBandwidth;
1293 int transient_got_disabled=0;
1294 opus_val16 surround_masking=0;
1295 opus_val16 temporal_vbr=0;
1296 opus_val16 surround_trim = 0;
1297 VARDECL(opus_val16, surround_dynalloc);
1298 ALLOC_STACK;
1299
1300 mode = st->mode;
1301 nbEBands = mode->nbEBands;
1302 overlap = mode->overlap;
1303 eBands = mode->eBands;
1304 tf_estimate = 0;
1305 if (nbCompressedBytes<2 || pcm==NULL)
1306 return OPUS_BAD_ARG;
1307
1308 frame_size *= st->upsample;
1309 for (LM=0;LM<=mode->maxLM;LM++)
1310 if (mode->shortMdctSize<<LM==frame_size)
1311 break;
1312 if (LM>mode->maxLM)
1313 return OPUS_BAD_ARG;
1314 M=1<<LM;
1315 N = M*mode->shortMdctSize;
1316
1317 prefilter_mem = st->in_mem+CC*(st->overlap);
1318 oldBandE = (opus_val16*)(st->in_mem+CC*(st->overlap+COMBFILTER_MAXPERIOD));
1319 oldLogE = oldBandE + CC*nbEBands;
1320 oldLogE2 = oldLogE + CC*nbEBands;
1321
1322 if (enc==NULL)
1323 {
1324 tell=1;
1325 nbFilledBytes=0;
1326 } else {
1327 tell=ec_tell(enc);
1328 nbFilledBytes=(tell+4)>>3;
1329 }
1330
1331 #ifdef CUSTOM_MODES
1332 if (st->signalling && enc==NULL)
1333 {
1334 int tmp = (mode->effEBands-st->end)>>1;
1335 st->end = IMAX(1, mode->effEBands-tmp);
1336 compressed[0] = tmp<<5;
1337 compressed[0] |= LM<<3;
1338 compressed[0] |= (C==2)<<2;
1339 /* Convert "standard mode" to Opus header */
1340 if (mode->Fs==48000 && mode->shortMdctSize==120)
1341 {
1342 int c0 = toOpus(compressed[0]);
1343 if (c0<0)
1344 return OPUS_BAD_ARG;
1345 compressed[0] = c0;
1346 }
1347 compressed++;
1348 nbCompressedBytes--;
1349 }
1350 #else
1351 celt_assert(st->signalling==0);
1352 #endif
1353
1354 /* Can't produce more than 1275 output bytes */
1355 nbCompressedBytes = IMIN(nbCompressedBytes,1275);
1356 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1357
1358 if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX)
1359 {
1360 opus_int32 den=mode->Fs>>BITRES;
1361 vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1362 #ifdef CUSTOM_MODES
1363 if (st->signalling)
1364 vbr_rate -= 8<<BITRES;
1365 #endif
1366 effectiveBytes = vbr_rate>>(3+BITRES);
1367 } else {
1368 opus_int32 tmp;
1369 vbr_rate = 0;
1370 tmp = st->bitrate*frame_size;
1371 if (tell>1)
1372 tmp += tell;
1373 if (st->bitrate!=OPUS_BITRATE_MAX)
1374 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,
1375 (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling));
1376 effectiveBytes = nbCompressedBytes;
1377 }
1378
1379 if (enc==NULL)
1380 {
1381 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1382 enc = &_enc;
1383 }
1384
1385 if (vbr_rate>0)
1386 {
1387 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1388 target rate and buffering.
1389 We must do this up front so that bust-prevention logic triggers
1390 correctly if we don't have enough bits. */
1391 if (st->constrained_vbr)
1392 {
1393 opus_int32 vbr_bound;
1394 opus_int32 max_allowed;
1395 /* We could use any multiple of vbr_rate as bound (depending on the
1396 delay).
1397 This is clamped to ensure we use at least two bytes if the encoder
1398 was entirely empty, but to allow 0 in hybrid mode. */
1399 vbr_bound = vbr_rate;
1400 max_allowed = IMIN(IMAX(tell==1?2:0,
1401 (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),
1402 nbAvailableBytes);
1403 if(max_allowed < nbAvailableBytes)
1404 {
1405 nbCompressedBytes = nbFilledBytes+max_allowed;
1406 nbAvailableBytes = max_allowed;
1407 ec_enc_shrink(enc, nbCompressedBytes);
1408 }
1409 }
1410 }
1411 total_bits = nbCompressedBytes*8;
1412
1413 effEnd = st->end;
1414 if (effEnd > mode->effEBands)
1415 effEnd = mode->effEBands;
1416
1417 ALLOC(in, CC*(N+st->overlap), celt_sig);
1418
1419 sample_max=MAX32(st->overlap_max, celt_maxabs16(pcm, C*(N-overlap)/st->upsamp le));
1420 st->overlap_max=celt_maxabs16(pcm+C*(N-overlap)/st->upsample, C*overlap/st->u psample);
1421 sample_max=MAX32(sample_max, st->overlap_max);
1422 #ifdef FIXED_POINT
1423 silence = (sample_max==0);
1424 #else
1425 silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth));
1426 #endif
1427 #ifdef FUZZING
1428 if ((rand()&0x3F)==0)
1429 silence = 1;
1430 #endif
1431 if (tell==1)
1432 ec_enc_bit_logp(enc, silence, 15);
1433 else
1434 silence=0;
1435 if (silence)
1436 {
1437 /*In VBR mode there is no need to send more than the minimum. */
1438 if (vbr_rate>0)
1439 {
1440 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+ 2);
1441 total_bits=nbCompressedBytes*8;
1442 nbAvailableBytes=2;
1443 ec_enc_shrink(enc, nbCompressedBytes);
1444 }
1445 /* Pretend we've filled all the remaining bits with zeros
1446 (that's what the initialiser did anyway) */
1447 tell = nbCompressedBytes*8;
1448 enc->nbits_total+=tell-ec_tell(enc);
1449 }
1450 c=0; do {
1451 preemphasis(pcm+c, in+c*(N+st->overlap)+st->overlap, N, CC, st->upsample,
1452 mode->preemph, st->preemph_memE+c, st->clip);
1453 } while (++c<CC);
1454
1455
1456
1457 /* Find pitch period and gain */
1458 {
1459 int enabled;
1460 int qg;
1461 enabled = (st->lfe || nbAvailableBytes>12*C) && st->start==0 && !silence & & !st->disable_pf
1462 && st->complexity >= 5 && !(st->consec_transient && LM!=3 && st->var iable_duration==OPUS_FRAMESIZE_VARIABLE);
1463
1464 prefilter_tapset = st->tapset_decision;
1465 pf_on = run_prefilter(st, in, prefilter_mem, CC, N, prefilter_tapset, &pit ch_index, &gain1, &qg, enabled, nbAvailableBytes);
1466 if ((gain1 > QCONST16(.4f,15) || st->prefilter_gain > QCONST16(.4f,15)) && (!st->analysis.valid || st->analysis.tonality > .3)
1467 && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st- >prefilter_period))
1468 pitch_change = 1;
1469 if (pf_on==0)
1470 {
1471 if(st->start==0 && tell+16<=total_bits)
1472 ec_enc_bit_logp(enc, 0, 1);
1473 } else {
1474 /*This block is not gated by a total bits check only because
1475 of the nbAvailableBytes check above.*/
1476 int octave;
1477 ec_enc_bit_logp(enc, 1, 1);
1478 pitch_index += 1;
1479 octave = EC_ILOG(pitch_index)-5;
1480 ec_enc_uint(enc, octave, 6);
1481 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1482 pitch_index -= 1;
1483 ec_enc_bits(enc, qg, 3);
1484 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1485 }
1486 }
1487
1488 isTransient = 0;
1489 shortBlocks = 0;
1490 if (st->complexity >= 1 && !st->lfe)
1491 {
1492 isTransient = transient_analysis(in, N+st->overlap, CC,
1493 &tf_estimate, &tf_chan);
1494 }
1495 if (LM>0 && ec_tell(enc)+3<=total_bits)
1496 {
1497 if (isTransient)
1498 shortBlocks = M;
1499 } else {
1500 isTransient = 0;
1501 transient_got_disabled=1;
1502 }
1503
1504 ALLOC(freq, CC*N, celt_sig); /**< Interleaved signal MDCTs */
1505 ALLOC(bandE,nbEBands*CC, celt_ener);
1506 ALLOC(bandLogE,nbEBands*CC, opus_val16);
1507
1508 secondMdct = shortBlocks && st->complexity>=8;
1509 ALLOC(bandLogE2, C*nbEBands, opus_val16);
1510 if (secondMdct)
1511 {
1512 compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample);
1513 compute_band_energies(mode, freq, bandE, effEnd, C, M);
1514 amp2Log2(mode, effEnd, st->end, bandE, bandLogE2, C);
1515 for (i=0;i<C*nbEBands;i++)
1516 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT));
1517 }
1518
1519 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample);
1520 if (CC==2&&C==1)
1521 tf_chan = 0;
1522 compute_band_energies(mode, freq, bandE, effEnd, C, M);
1523
1524 if (st->lfe)
1525 {
1526 for (i=2;i<st->end;i++)
1527 {
1528 bandE[i] = IMIN(bandE[i], MULT16_32_Q15(QCONST16(1e-4f,15),bandE[0]));
1529 bandE[i] = MAX32(bandE[i], EPSILON);
1530 }
1531 }
1532 amp2Log2(mode, effEnd, st->end, bandE, bandLogE, C);
1533
1534 ALLOC(surround_dynalloc, C*nbEBands, opus_val16);
1535 for(i=0;i<st->end;i++)
1536 surround_dynalloc[i] = 0;
1537 /* This computes how much masking takes place between surround channels */
1538 if (st->start==0&&st->energy_mask&&!st->lfe)
1539 {
1540 int mask_end;
1541 int midband;
1542 int count_dynalloc;
1543 opus_val32 mask_avg=0;
1544 opus_val32 diff=0;
1545 int count=0;
1546 mask_end = IMAX(2,st->lastCodedBands);
1547 for (c=0;c<C;c++)
1548 {
1549 for(i=0;i<mask_end;i++)
1550 {
1551 opus_val16 mask;
1552 mask = MAX16(MIN16(st->energy_mask[nbEBands*c+i],
1553 QCONST16(.25f, DB_SHIFT)), -QCONST16(2.0f, DB_SHIFT));
1554 if (mask > 0)
1555 mask = HALF16(mask);
1556 mask_avg += MULT16_16(mask, eBands[i+1]-eBands[i]);
1557 count += eBands[i+1]-eBands[i];
1558 diff += MULT16_16(mask, 1+2*i-mask_end);
1559 }
1560 }
1561 mask_avg = DIV32_16(mask_avg,count);
1562 mask_avg += QCONST16(.2f, DB_SHIFT);
1563 diff = diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end);
1564 /* Again, being conservative */
1565 diff = HALF32(diff);
1566 diff = MAX32(MIN32(diff, QCONST32(.031f, DB_SHIFT)), -QCONST32(.031f, DB_S HIFT));
1567 /* Find the band that's in the middle of the coded spectrum */
1568 for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++);
1569 count_dynalloc=0;
1570 for(i=0;i<mask_end;i++)
1571 {
1572 opus_val32 lin;
1573 opus_val16 unmask;
1574 lin = mask_avg + diff*(i-midband);
1575 if (C==2)
1576 unmask = MAX16(st->energy_mask[i], st->energy_mask[nbEBands+i]);
1577 else
1578 unmask = st->energy_mask[i];
1579 unmask = MIN16(unmask, QCONST16(.0f, DB_SHIFT));
1580 unmask -= lin;
1581 if (unmask > QCONST16(.25f, DB_SHIFT))
1582 {
1583 surround_dynalloc[i] = unmask - QCONST16(.25f, DB_SHIFT);
1584 count_dynalloc++;
1585 }
1586 }
1587 if (count_dynalloc>=3)
1588 {
1589 /* If we need dynalloc in many bands, it's probably because our
1590 initial masking rate was too low. */
1591 mask_avg += QCONST16(.25f, DB_SHIFT);
1592 if (mask_avg>0)
1593 {
1594 /* Something went really wrong in the original calculations,
1595 disabling masking. */
1596 mask_avg = 0;
1597 diff = 0;
1598 for(i=0;i<mask_end;i++)
1599 surround_dynalloc[i] = 0;
1600 } else {
1601 for(i=0;i<mask_end;i++)
1602 surround_dynalloc[i] = MAX16(0, surround_dynalloc[i]-QCONST16(.25 f, DB_SHIFT));
1603 }
1604 }
1605 mask_avg += QCONST16(.2f, DB_SHIFT);
1606 /* Convert to 1/64th units used for the trim */
1607 surround_trim = 64*diff;
1608 /*printf("%d %d ", mask_avg, surround_trim);*/
1609 surround_masking = mask_avg;
1610 }
1611 /* Temporal VBR (but not for LFE) */
1612 if (!st->lfe)
1613 {
1614 opus_val16 follow=-QCONST16(10.0f,DB_SHIFT);
1615 float frame_avg=0;
1616 opus_val16 offset = shortBlocks?HALF16(SHL16(LM, DB_SHIFT)):0;
1617 for(i=st->start;i<st->end;i++)
1618 {
1619 follow = MAX16(follow-QCONST16(1.f, DB_SHIFT), bandLogE[i]-offset);
1620 if (C==2)
1621 follow = MAX16(follow, bandLogE[i+nbEBands]-offset);
1622 frame_avg += follow;
1623 }
1624 frame_avg /= (st->end-st->start);
1625 temporal_vbr = SUB16(frame_avg,st->spec_avg);
1626 temporal_vbr = MIN16(QCONST16(3.f, DB_SHIFT), MAX16(-QCONST16(1.5f, DB_SHI FT), temporal_vbr));
1627 st->spec_avg += MULT16_16_Q15(QCONST16(.02f, 15), temporal_vbr);
1628 }
1629 /*for (i=0;i<21;i++)
1630 printf("%f ", bandLogE[i]);
1631 printf("\n");*/
1632
1633 if (!secondMdct)
1634 {
1635 for (i=0;i<C*nbEBands;i++)
1636 bandLogE2[i] = bandLogE[i];
1637 }
1638
1639 /* Last chance to catch any transient we might have missed in the
1640 time-domain analysis */
1641 if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5 & & !st->lfe)
1642 {
1643 if (patch_transient_decision(bandLogE, oldBandE, nbEBands, st->end, C))
1644 {
1645 isTransient = 1;
1646 shortBlocks = M;
1647 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample);
1648 compute_band_energies(mode, freq, bandE, effEnd, C, M);
1649 amp2Log2(mode, effEnd, st->end, bandE, bandLogE, C);
1650 /* Compensate for the scaling of short vs long mdcts */
1651 for (i=0;i<C*nbEBands;i++)
1652 bandLogE2[i] += HALF16(SHL16(LM, DB_SHIFT));
1653 tf_estimate = QCONST16(.2f,14);
1654 }
1655 }
1656
1657 if (LM>0 && ec_tell(enc)+3<=total_bits)
1658 ec_enc_bit_logp(enc, isTransient, 3);
1659
1660 ALLOC(X, C*N, celt_norm); /**< Interleaved normalised MDCTs */
1661
1662 /* Band normalisation */
1663 normalise_bands(mode, freq, X, bandE, effEnd, C, M);
1664
1665 ALLOC(tf_res, nbEBands, int);
1666 /* Disable variable tf resolution for hybrid and at very low bitrate */
1667 if (effectiveBytes>=15*C && st->start==0 && st->complexity>=2 && !st->lfe)
1668 {
1669 int lambda;
1670 if (effectiveBytes<40)
1671 lambda = 12;
1672 else if (effectiveBytes<60)
1673 lambda = 6;
1674 else if (effectiveBytes<100)
1675 lambda = 4;
1676 else
1677 lambda = 3;
1678 lambda*=2;
1679 tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, L M, &tf_sum, tf_estimate, tf_chan);
1680 for (i=effEnd;i<st->end;i++)
1681 tf_res[i] = tf_res[effEnd-1];
1682 } else {
1683 tf_sum = 0;
1684 for (i=0;i<st->end;i++)
1685 tf_res[i] = isTransient;
1686 tf_select=0;
1687 }
1688
1689 ALLOC(error, C*nbEBands, opus_val16);
1690 quant_coarse_energy(mode, st->start, st->end, effEnd, bandLogE,
1691 oldBandE, total_bits, error, enc,
1692 C, LM, nbAvailableBytes, st->force_intra,
1693 &st->delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
1694
1695 tf_encode(st->start, st->end, isTransient, tf_res, LM, tf_select, enc);
1696
1697 if (ec_tell(enc)+4<=total_bits)
1698 {
1699 if (st->lfe)
1700 {
1701 st->tapset_decision = 0;
1702 st->spread_decision = SPREAD_NORMAL;
1703 } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C || st->start != 0)
1704 {
1705 if (st->complexity == 0)
1706 st->spread_decision = SPREAD_NONE;
1707 else
1708 st->spread_decision = SPREAD_NORMAL;
1709 } else {
1710 /* Disable new spreading+tapset estimator until we can show it works
1711 better than the old one. So far it seems like spreading_decision()
1712 works best. */
1713 if (0&&st->analysis.valid)
1714 {
1715 static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15), -QCONST16(.2f, 15), -QCONST16(.07f, 15)};
1716 static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15), QCONST16(.07f, 15), QCONST16(.02f, 15)};
1717 static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15), Q CONST16(.15f, 15)};
1718 static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15), Q CONST16(.05f, 15)};
1719 st->spread_decision = hysteresis_decision(-st->analysis.tonality, sp read_thresholds, spread_histeresis, 3, st->spread_decision);
1720 st->tapset_decision = hysteresis_decision(st->analysis.tonality_slop e, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
1721 } else {
1722 st->spread_decision = spreading_decision(mode, X,
1723 &st->tonal_average, st->spread_decision, &st->hf_average,
1724 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M);
1725 }
1726 /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
1727 /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
1728 }
1729 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
1730 }
1731
1732 ALLOC(offsets, nbEBands, int);
1733
1734 maxDepth = dynalloc_analysis(bandLogE, bandLogE2, nbEBands, st->start, st->en d, C, offsets,
1735 st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr,
1736 eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc);
1737 /* For LFE, everything interesting is in the first band */
1738 if (st->lfe)
1739 offsets[0] = IMIN(8, effectiveBytes/3);
1740 ALLOC(cap, nbEBands, int);
1741 init_caps(mode,cap,LM,C);
1742
1743 dynalloc_logp = 6;
1744 total_bits<<=BITRES;
1745 total_boost = 0;
1746 tell = ec_tell_frac(enc);
1747 for (i=st->start;i<st->end;i++)
1748 {
1749 int width, quanta;
1750 int dynalloc_loop_logp;
1751 int boost;
1752 int j;
1753 width = C*(eBands[i+1]-eBands[i])<<LM;
1754 /* quanta is 6 bits, but no more than 1 bit/sample
1755 and no less than 1/8 bit/sample */
1756 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width));
1757 dynalloc_loop_logp = dynalloc_logp;
1758 boost = 0;
1759 for (j = 0; tell+(dynalloc_loop_logp<<BITRES) < total_bits-total_boost
1760 && boost < cap[i]; j++)
1761 {
1762 int flag;
1763 flag = j<offsets[i];
1764 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
1765 tell = ec_tell_frac(enc);
1766 if (!flag)
1767 break;
1768 boost += quanta;
1769 total_boost += quanta;
1770 dynalloc_loop_logp = 1;
1771 }
1772 /* Making dynalloc more likely */
1773 if (j)
1774 dynalloc_logp = IMAX(2, dynalloc_logp-1);
1775 offsets[i] = boost;
1776 }
1777
1778 if (C==2)
1779 {
1780 int effectiveRate;
1781
1782 static const opus_val16 intensity_thresholds[21]=
1783 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 off*/
1784 { 16,21,23,25,27,29,31,33,35,38,42,46,50,54,58,63,68,75,84,102,130};
1785 static const opus_val16 intensity_histeresis[21]=
1786 { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5, 6, 8, 12};
1787
1788 /* Always use MS for 2.5 ms frames until we can do a better analysis */
1789 if (LM!=0)
1790 dual_stereo = stereo_analysis(mode, X, LM, N);
1791
1792 /* Account for coarse energy */
1793 effectiveRate = (8*effectiveBytes - 80)>>LM;
1794
1795 /* effectiveRate in kb/s */
1796 effectiveRate = 2*effectiveRate/5;
1797
1798 st->intensity = hysteresis_decision((opus_val16)effectiveRate, intensity_t hresholds, intensity_histeresis, 21, st->intensity);
1799 st->intensity = IMIN(st->end,IMAX(st->start, st->intensity));
1800 }
1801
1802 alloc_trim = 5;
1803 if (tell+(6<<BITRES) <= total_bits - total_boost)
1804 {
1805 if (st->lfe)
1806 alloc_trim = 5;
1807 else
1808 alloc_trim = alloc_trim_analysis(mode, X, bandLogE,
1809 st->end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate, s t->intensity, surround_trim);
1810 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
1811 tell = ec_tell_frac(enc);
1812 }
1813
1814 /* Variable bitrate */
1815 if (vbr_rate>0)
1816 {
1817 opus_val16 alpha;
1818 opus_int32 delta;
1819 /* The target rate in 8th bits per frame */
1820 opus_int32 target, base_target;
1821 opus_int32 min_allowed;
1822 int lm_diff = mode->maxLM - LM;
1823
1824 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
1825 The CELT allocator will just not be able to use more than that anyway. * /
1826 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM));
1827 base_target = vbr_rate - ((40*C+20)<<BITRES);
1828
1829 if (st->constrained_vbr)
1830 base_target += (st->vbr_offset>>lm_diff);
1831
1832 target = compute_vbr(mode, &st->analysis, base_target, LM, st->bitrate,
1833 st->lastCodedBands, C, st->intensity, st->constrained_vbr,
1834 st->stereo_saving, tot_boost, tf_estimate, pitch_change, maxDepth,
1835 st->variable_duration, st->lfe, st->energy_mask!=NULL, surround_maski ng,
1836 temporal_vbr);
1837
1838 /* The current offset is removed from the target and the space used
1839 so far is added*/
1840 target=target+tell;
1841 /* In VBR mode the frame size must not be reduced so much that it would
1842 result in the encoder running out of bits.
1843 The margin of 2 bytes ensures that none of the bust-prevention logic
1844 in the decoder will have triggered so far. */
1845 min_allowed = ((tell+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3)) + 2 - nbFi lledBytes;
1846
1847 nbAvailableBytes = (target+(1<<(BITRES+2)))>>(BITRES+3);
1848 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes);
1849 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes) - nbFilledBytes;
1850
1851 /* By how much did we "miss" the target on that frame */
1852 delta = target - vbr_rate;
1853
1854 target=nbAvailableBytes<<(BITRES+3);
1855
1856 /*If the frame is silent we don't adjust our drift, otherwise
1857 the encoder will shoot to very high rates after hitting a
1858 span of silence, but we do allow the bitres to refill.
1859 This means that we'll undershoot our target in CVBR/VBR modes
1860 on files with lots of silence. */
1861 if(silence)
1862 {
1863 nbAvailableBytes = 2;
1864 target = 2*8<<BITRES;
1865 delta = 0;
1866 }
1867
1868 if (st->vbr_count < 970)
1869 {
1870 st->vbr_count++;
1871 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16));
1872 } else
1873 alpha = QCONST16(.001f,15);
1874 /* How many bits have we used in excess of what we're allowed */
1875 if (st->constrained_vbr)
1876 st->vbr_reservoir += target - vbr_rate;
1877 /*printf ("%d\n", st->vbr_reservoir);*/
1878
1879 /* Compute the offset we need to apply in order to reach the target */
1880 if (st->constrained_vbr)
1881 {
1882 st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st ->vbr_offset-st->vbr_drift);
1883 st->vbr_offset = -st->vbr_drift;
1884 }
1885 /*printf ("%d\n", st->vbr_drift);*/
1886
1887 if (st->constrained_vbr && st->vbr_reservoir < 0)
1888 {
1889 /* We're under the min value -- increase rate */
1890 int adjust = (-st->vbr_reservoir)/(8<<BITRES);
1891 /* Unless we're just coding silence */
1892 nbAvailableBytes += silence?0:adjust;
1893 st->vbr_reservoir = 0;
1894 /*printf ("+%d\n", adjust);*/
1895 }
1896 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes+nbFilledBytes);
1897 /*printf("%d\n", nbCompressedBytes*50*8);*/
1898 /* This moves the raw bits to take into account the new compressed size */
1899 ec_enc_shrink(enc, nbCompressedBytes);
1900 }
1901
1902 /* Bit allocation */
1903 ALLOC(fine_quant, nbEBands, int);
1904 ALLOC(pulses, nbEBands, int);
1905 ALLOC(fine_priority, nbEBands, int);
1906
1907 /* bits = packet size - where we are - safety*/
1908 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES) - ec_tell_frac(enc) - 1;
1909 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES) ? (1<<BITRES) : 0;
1910 bits -= anti_collapse_rsv;
1911 signalBandwidth = st->end-1;
1912 #ifndef DISABLE_FLOAT_API
1913 if (st->analysis.valid)
1914 {
1915 int min_bandwidth;
1916 if (st->bitrate < (opus_int32)32000*C)
1917 min_bandwidth = 13;
1918 else if (st->bitrate < (opus_int32)48000*C)
1919 min_bandwidth = 16;
1920 else if (st->bitrate < (opus_int32)60000*C)
1921 min_bandwidth = 18;
1922 else if (st->bitrate < (opus_int32)80000*C)
1923 min_bandwidth = 19;
1924 else
1925 min_bandwidth = 20;
1926 signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth);
1927 }
1928 #endif
1929 if (st->lfe)
1930 signalBandwidth = 1;
1931 codedBands = compute_allocation(mode, st->start, st->end, offsets, cap,
1932 alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses,
1933 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBan dwidth);
1934 if (st->lastCodedBands)
1935 st->lastCodedBands = IMIN(st->lastCodedBands+1,IMAX(st->lastCodedBands-1,c odedBands));
1936 else
1937 st->lastCodedBands = codedBands;
1938
1939 quant_fine_energy(mode, st->start, st->end, oldBandE, error, fine_quant, enc, C);
1940
1941 /* Residual quantisation */
1942 ALLOC(collapse_masks, C*nbEBands, unsigned char);
1943 quant_all_bands(1, mode, st->start, st->end, X, C==2 ? X+N : NULL, collapse_m asks,
1944 bandE, pulses, shortBlocks, st->spread_decision, dual_stereo, st->inten sity, tf_res,
1945 nbCompressedBytes*(8<<BITRES)-anti_collapse_rsv, balance, enc, LM, code dBands, &st->rng);
1946
1947 if (anti_collapse_rsv > 0)
1948 {
1949 anti_collapse_on = st->consec_transient<2;
1950 #ifdef FUZZING
1951 anti_collapse_on = rand()&0x1;
1952 #endif
1953 ec_enc_bits(enc, anti_collapse_on, 1);
1954 }
1955 quant_energy_finalise(mode, st->start, st->end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
1956
1957 if (silence)
1958 {
1959 for (i=0;i<C*nbEBands;i++)
1960 oldBandE[i] = -QCONST16(28.f,DB_SHIFT);
1961 }
1962
1963 #ifdef RESYNTH
1964 /* Re-synthesis of the coded audio if required */
1965 {
1966 celt_sig *out_mem[2];
1967
1968 if (anti_collapse_on)
1969 {
1970 anti_collapse(mode, X, collapse_masks, LM, C, N,
1971 st->start, st->end, oldBandE, oldLogE, oldLogE2, pulses, st->rng) ;
1972 }
1973
1974 if (silence)
1975 {
1976 for (i=0;i<C*N;i++)
1977 freq[i] = 0;
1978 } else {
1979 /* Synthesis */
1980 denormalise_bands(mode, X, freq, oldBandE, st->start, effEnd, C, M);
1981 }
1982
1983 c=0; do {
1984 OPUS_MOVE(st->syn_mem[c], st->syn_mem[c]+N, 2*MAX_PERIOD-N+overlap/2);
1985 } while (++c<CC);
1986
1987 if (CC==2&&C==1)
1988 {
1989 for (i=0;i<N;i++)
1990 freq[N+i] = freq[i];
1991 }
1992
1993 c=0; do {
1994 out_mem[c] = st->syn_mem[c]+2*MAX_PERIOD-N;
1995 } while (++c<CC);
1996
1997 compute_inv_mdcts(mode, shortBlocks, freq, out_mem, CC, LM);
1998
1999 c=0; do {
2000 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD);
2001 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINP ERIOD);
2002 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefi lter_period, mode->shortMdctSize,
2003 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_ old, st->prefilter_tapset,
2004 mode->window, st->overlap);
2005 if (LM!=0)
2006 comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMd ctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize,
2007 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tap set,
2008 mode->window, overlap);
2009 } while (++c<CC);
2010
2011 /* We reuse freq[] as scratch space for the de-emphasis */
2012 deemphasis(out_mem, (opus_val16*)pcm, N, CC, st->upsample, mode->preemph, st->preemph_memD, freq);
2013 st->prefilter_period_old = st->prefilter_period;
2014 st->prefilter_gain_old = st->prefilter_gain;
2015 st->prefilter_tapset_old = st->prefilter_tapset;
2016 }
2017 #endif
2018
2019 st->prefilter_period = pitch_index;
2020 st->prefilter_gain = gain1;
2021 st->prefilter_tapset = prefilter_tapset;
2022 #ifdef RESYNTH
2023 if (LM!=0)
2024 {
2025 st->prefilter_period_old = st->prefilter_period;
2026 st->prefilter_gain_old = st->prefilter_gain;
2027 st->prefilter_tapset_old = st->prefilter_tapset;
2028 }
2029 #endif
2030
2031 if (CC==2&&C==1) {
2032 for (i=0;i<nbEBands;i++)
2033 oldBandE[nbEBands+i]=oldBandE[i];
2034 }
2035
2036 if (!isTransient)
2037 {
2038 for (i=0;i<CC*nbEBands;i++)
2039 oldLogE2[i] = oldLogE[i];
2040 for (i=0;i<CC*nbEBands;i++)
2041 oldLogE[i] = oldBandE[i];
2042 } else {
2043 for (i=0;i<CC*nbEBands;i++)
2044 oldLogE[i] = MIN16(oldLogE[i], oldBandE[i]);
2045 }
2046 /* In case start or end were to change */
2047 c=0; do
2048 {
2049 for (i=0;i<st->start;i++)
2050 {
2051 oldBandE[c*nbEBands+i]=0;
2052 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2053 }
2054 for (i=st->end;i<nbEBands;i++)
2055 {
2056 oldBandE[c*nbEBands+i]=0;
2057 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-QCONST16(28.f,DB_SHIFT);
2058 }
2059 } while (++c<CC);
2060
2061 if (isTransient || transient_got_disabled)
2062 st->consec_transient++;
2063 else
2064 st->consec_transient=0;
2065 st->rng = enc->rng;
2066
2067 /* If there's any room left (can only happen for very high rates),
2068 it's already filled with zeros */
2069 ec_enc_done(enc);
2070
2071 #ifdef CUSTOM_MODES
2072 if (st->signalling)
2073 nbCompressedBytes++;
2074 #endif
2075
2076 RESTORE_STACK;
2077 if (ec_get_error(enc))
2078 return OPUS_INTERNAL_ERROR;
2079 else
2080 return nbCompressedBytes;
2081 }
2082
2083
2084 #ifdef CUSTOM_MODES
2085
2086 #ifdef FIXED_POINT
2087 int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, i nt frame_size, unsigned char *compressed, int nbCompressedBytes)
2088 {
2089 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes , NULL);
2090 }
2091
2092 #ifndef DISABLE_FLOAT_API
2093 int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2094 {
2095 int j, ret, C, N;
2096 VARDECL(opus_int16, in);
2097 ALLOC_STACK;
2098
2099 if (pcm==NULL)
2100 return OPUS_BAD_ARG;
2101
2102 C = st->channels;
2103 N = frame_size;
2104 ALLOC(in, C*N, opus_int16);
2105
2106 for (j=0;j<C*N;j++)
2107 in[j] = FLOAT2INT16(pcm[j]);
2108
2109 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL);
2110 #ifdef RESYNTH
2111 for (j=0;j<C*N;j++)
2112 ((float*)pcm)[j]=in[j]*(1.f/32768.f);
2113 #endif
2114 RESTORE_STACK;
2115 return ret;
2116 }
2117 #endif /* DISABLE_FLOAT_API */
2118 #else
2119
2120 int opus_custom_encode(CELTEncoder * OPUS_RESTRICT st, const opus_int16 * pcm, i nt frame_size, unsigned char *compressed, int nbCompressedBytes)
2121 {
2122 int j, ret, C, N;
2123 VARDECL(celt_sig, in);
2124 ALLOC_STACK;
2125
2126 if (pcm==NULL)
2127 return OPUS_BAD_ARG;
2128
2129 C=st->channels;
2130 N=frame_size;
2131 ALLOC(in, C*N, celt_sig);
2132 for (j=0;j<C*N;j++) {
2133 in[j] = SCALEOUT(pcm[j]);
2134 }
2135
2136 ret = celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL );
2137 #ifdef RESYNTH
2138 for (j=0;j<C*N;j++)
2139 ((opus_int16*)pcm)[j] = FLOAT2INT16(in[j]);
2140 #endif
2141 RESTORE_STACK;
2142 return ret;
2143 }
2144
2145 int opus_custom_encode_float(CELTEncoder * OPUS_RESTRICT st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2146 {
2147 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes , NULL);
2148 }
2149
2150 #endif
2151
2152 #endif /* CUSTOM_MODES */
2153
2154 int opus_custom_encoder_ctl(CELTEncoder * OPUS_RESTRICT st, int request, ...)
2155 {
2156 va_list ap;
2157
2158 va_start(ap, request);
2159 switch (request)
2160 {
2161 case OPUS_SET_COMPLEXITY_REQUEST:
2162 {
2163 int value = va_arg(ap, opus_int32);
2164 if (value<0 || value>10)
2165 goto bad_arg;
2166 st->complexity = value;
2167 }
2168 break;
2169 case CELT_SET_START_BAND_REQUEST:
2170 {
2171 opus_int32 value = va_arg(ap, opus_int32);
2172 if (value<0 || value>=st->mode->nbEBands)
2173 goto bad_arg;
2174 st->start = value;
2175 }
2176 break;
2177 case CELT_SET_END_BAND_REQUEST:
2178 {
2179 opus_int32 value = va_arg(ap, opus_int32);
2180 if (value<1 || value>st->mode->nbEBands)
2181 goto bad_arg;
2182 st->end = value;
2183 }
2184 break;
2185 case CELT_SET_PREDICTION_REQUEST:
2186 {
2187 int value = va_arg(ap, opus_int32);
2188 if (value<0 || value>2)
2189 goto bad_arg;
2190 st->disable_pf = value<=1;
2191 st->force_intra = value==0;
2192 }
2193 break;
2194 case OPUS_SET_PACKET_LOSS_PERC_REQUEST:
2195 {
2196 int value = va_arg(ap, opus_int32);
2197 if (value<0 || value>100)
2198 goto bad_arg;
2199 st->loss_rate = value;
2200 }
2201 break;
2202 case OPUS_SET_VBR_CONSTRAINT_REQUEST:
2203 {
2204 opus_int32 value = va_arg(ap, opus_int32);
2205 st->constrained_vbr = value;
2206 }
2207 break;
2208 case OPUS_SET_VBR_REQUEST:
2209 {
2210 opus_int32 value = va_arg(ap, opus_int32);
2211 st->vbr = value;
2212 }
2213 break;
2214 case OPUS_SET_BITRATE_REQUEST:
2215 {
2216 opus_int32 value = va_arg(ap, opus_int32);
2217 if (value<=500 && value!=OPUS_BITRATE_MAX)
2218 goto bad_arg;
2219 value = IMIN(value, 260000*st->channels);
2220 st->bitrate = value;
2221 }
2222 break;
2223 case CELT_SET_CHANNELS_REQUEST:
2224 {
2225 opus_int32 value = va_arg(ap, opus_int32);
2226 if (value<1 || value>2)
2227 goto bad_arg;
2228 st->stream_channels = value;
2229 }
2230 break;
2231 case OPUS_SET_LSB_DEPTH_REQUEST:
2232 {
2233 opus_int32 value = va_arg(ap, opus_int32);
2234 if (value<8 || value>24)
2235 goto bad_arg;
2236 st->lsb_depth=value;
2237 }
2238 break;
2239 case OPUS_GET_LSB_DEPTH_REQUEST:
2240 {
2241 opus_int32 *value = va_arg(ap, opus_int32*);
2242 *value=st->lsb_depth;
2243 }
2244 break;
2245 case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST:
2246 {
2247 opus_int32 value = va_arg(ap, opus_int32);
2248 st->variable_duration = value;
2249 }
2250 break;
2251 case OPUS_RESET_STATE:
2252 {
2253 int i;
2254 opus_val16 *oldBandE, *oldLogE, *oldLogE2;
2255 oldBandE = (opus_val16*)(st->in_mem+st->channels*(st->overlap+COMBFILTE R_MAXPERIOD));
2256 oldLogE = oldBandE + st->channels*st->mode->nbEBands;
2257 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
2258 OPUS_CLEAR((char*)&st->ENCODER_RESET_START,
2259 opus_custom_encoder_get_size(st->mode, st->channels)-
2260 ((char*)&st->ENCODER_RESET_START - (char*)st));
2261 for (i=0;i<st->channels*st->mode->nbEBands;i++)
2262 oldLogE[i]=oldLogE2[i]=-QCONST16(28.f,DB_SHIFT);
2263 st->vbr_offset = 0;
2264 st->delayedIntra = 1;
2265 st->spread_decision = SPREAD_NORMAL;
2266 st->tonal_average = 256;
2267 st->hf_average = 0;
2268 st->tapset_decision = 0;
2269 }
2270 break;
2271 #ifdef CUSTOM_MODES
2272 case CELT_SET_INPUT_CLIPPING_REQUEST:
2273 {
2274 opus_int32 value = va_arg(ap, opus_int32);
2275 st->clip = value;
2276 }
2277 break;
2278 #endif
2279 case CELT_SET_SIGNALLING_REQUEST:
2280 {
2281 opus_int32 value = va_arg(ap, opus_int32);
2282 st->signalling = value;
2283 }
2284 break;
2285 case CELT_SET_ANALYSIS_REQUEST:
2286 {
2287 AnalysisInfo *info = va_arg(ap, AnalysisInfo *);
2288 if (info)
2289 OPUS_COPY(&st->analysis, info, 1);
2290 }
2291 break;
2292 case CELT_GET_MODE_REQUEST:
2293 {
2294 const CELTMode ** value = va_arg(ap, const CELTMode**);
2295 if (value==0)
2296 goto bad_arg;
2297 *value=st->mode;
2298 }
2299 break;
2300 case OPUS_GET_FINAL_RANGE_REQUEST:
2301 {
2302 opus_uint32 * value = va_arg(ap, opus_uint32 *);
2303 if (value==0)
2304 goto bad_arg;
2305 *value=st->rng;
2306 }
2307 break;
2308 case OPUS_SET_LFE_REQUEST:
2309 {
2310 opus_int32 value = va_arg(ap, opus_int32);
2311 st->lfe = value;
2312 }
2313 break;
2314 case OPUS_SET_ENERGY_MASK_REQUEST:
2315 {
2316 opus_val16 *value = va_arg(ap, opus_val16*);
2317 st->energy_mask = value;
2318 }
2319 break;
2320 default:
2321 goto bad_request;
2322 }
2323 va_end(ap);
2324 return OPUS_OK;
2325 bad_arg:
2326 va_end(ap);
2327 return OPUS_BAD_ARG;
2328 bad_request:
2329 va_end(ap);
2330 return OPUS_UNIMPLEMENTED;
2331 }
OLDNEW
« no previous file with comments | « celt/celt_decoder.c ('k') | celt/celt_lpc.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698