Bug Summary

File:root/firefox-clang/media/libopus/celt/celt_encoder.c
Warning:line 1102, column 17
The left operand of '>' is a garbage value

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name celt_encoder.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -fhalf-no-semantic-interposition -mframe-pointer=all -relaxed-aliasing -ffp-contract=off -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -fdebug-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/media/libopus -fcoverage-compilation-dir=/root/firefox-clang/obj-x86_64-pc-linux-gnu/media/libopus -resource-dir /usr/lib/llvm-21/lib/clang/21 -include /root/firefox-clang/obj-x86_64-pc-linux-gnu/mozilla-config.h -U _FORTIFY_SOURCE -D _FORTIFY_SOURCE=2 -D _GLIBCXX_ASSERTIONS -D DEBUG=1 -D OPUS_BUILD -D OPUS_VERSION=b64041270050d5a84e22fa3df0b3166edef810ea -D USE_ALLOCA -D ENABLE_HARDENING -D OPUS_EXPORT= -D ENABLE_ASSERTIONS -D HAVE_LRINTF -D OPUS_HAVE_RTCD -D CPU_INFO_BY_ASM -D OPUS_X86_MAY_HAVE_SSE -D OPUS_X86_MAY_HAVE_SSE2 -D OPUS_X86_MAY_HAVE_SSE4_1 -D OPUS_X86_MAY_HAVE_AVX -D MOZ_HAS_MOZGLUE -I /root/firefox-clang/media/libopus -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/media/libopus -I /root/firefox-clang/media/libopus/celt -I /root/firefox-clang/media/libopus/include -I /root/firefox-clang/media/libopus/silk -I /root/firefox-clang/media/libopus/silk/fixed -I /root/firefox-clang/media/libopus/silk/float -I /root/firefox-clang/media/libopus/src -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nspr -I /root/firefox-clang/obj-x86_64-pc-linux-gnu/dist/include/nss -D MOZILLA_CLIENT -internal-isystem /usr/lib/llvm-21/lib/clang/21/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/14/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-error=tautological-type-limit-compare -Wno-range-loop-analysis -Wno-error=deprecated-declarations -Wno-error=array-bounds -Wno-error=free-nonheap-object -Wno-error=atomic-alignment -Wno-error=deprecated-builtins -Wno-psabi -Wno-error=builtin-macro-redefined -Wno-unknown-warning-option -Wno-#pragma-messages -ferror-limit 19 -fstrict-flex-arrays=1 -stack-protector 2 -fstack-clash-protection -ftrivial-auto-var-init=pattern -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -vectorize-loops -vectorize-slp -analyzer-checker optin.performance.Padding -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2025-06-27-100320-3286336-1 -x c /root/firefox-clang/media/libopus/celt/celt_encoder.c
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#ifndef M_PI3.14159265358979323846
56#define M_PI3.14159265358979323846 3.141592653
57#endif
58
59
60/** Encoder state
61 @brief Encoder state
62 */
63struct OpusCustomEncoder {
64 const OpusCustomMode *mode; /**< Mode used by the encoder */
65 int channels;
66 int stream_channels;
67
68 int force_intra;
69 int clip;
70 int disable_pf;
71 int complexity;
72 int upsample;
73 int start, end;
74
75 opus_int32 bitrate;
76 int vbr;
77 int signalling;
78 int constrained_vbr; /* If zero, VBR can do whatever it likes with the rate */
79 int loss_rate;
80 int lsb_depth;
81 int lfe;
82 int disable_inv;
83 int arch;
84
85 /* Everything beyond this point gets cleared on a reset */
86#define ENCODER_RESET_STARTrng rng
87
88 opus_uint32 rng;
89 int spread_decision;
90 opus_val32 delayedIntra;
91 int tonal_average;
92 int lastCodedBands;
93 int hf_average;
94 int tapset_decision;
95
96 int prefilter_period;
97 opus_val16 prefilter_gain;
98 int prefilter_tapset;
99#ifdef RESYNTH
100 int prefilter_period_old;
101 opus_val16 prefilter_gain_old;
102 int prefilter_tapset_old;
103#endif
104 int consec_transient;
105 AnalysisInfo analysis;
106 SILKInfo silk_info;
107
108 opus_val32 preemph_memE[2];
109 opus_val32 preemph_memD[2];
110
111 /* VBR-related parameters */
112 opus_int32 vbr_reservoir;
113 opus_int32 vbr_drift;
114 opus_int32 vbr_offset;
115 opus_int32 vbr_count;
116 opus_val32 overlap_max;
117 opus_val16 stereo_saving;
118 int intensity;
119 celt_glog *energy_mask;
120 celt_glog spec_avg;
121
122#ifdef RESYNTH
123 /* +MAX_PERIOD/2 to make space for overlap */
124 celt_sig syn_mem[2][2*MAX_PERIOD1024+MAX_PERIOD1024/2];
125#endif
126
127 celt_sig in_mem[1]; /* Size = channels*mode->overlap */
128 /* celt_sig prefilter_mem[], Size = channels*COMBFILTER_MAXPERIOD */
129 /* celt_glog oldBandE[], Size = channels*mode->nbEBands */
130 /* celt_glog oldLogE[], Size = channels*mode->nbEBands */
131 /* celt_glog oldLogE2[], Size = channels*mode->nbEBands */
132 /* celt_glog energyError[], Size = channels*mode->nbEBands */
133};
134
135int celt_encoder_get_size(int channels)
136{
137 CELTModeOpusCustomMode *mode = opus_custom_mode_create(48000, 960, NULL((void*)0));
138 return opus_custom_encoder_get_size(mode, channels);
139}
140
141OPUS_CUSTOM_NOSTATICstatic inline int opus_custom_encoder_get_size(const CELTModeOpusCustomMode *mode, int channels)
142{
143 int size = sizeof(struct CELTEncoderOpusCustomEncoder)
144 + (channels*mode->overlap-1)*sizeof(celt_sig) /* celt_sig in_mem[channels*mode->overlap]; */
145 + channels*COMBFILTER_MAXPERIOD1024*sizeof(celt_sig) /* celt_sig prefilter_mem[channels*COMBFILTER_MAXPERIOD]; */
146 + 4*channels*mode->nbEBands*sizeof(celt_glog); /* celt_glog oldBandE[channels*mode->nbEBands]; */
147 /* celt_glog oldLogE[channels*mode->nbEBands]; */
148 /* celt_glog oldLogE2[channels*mode->nbEBands]; */
149 /* celt_glog energyError[channels*mode->nbEBands]; */
150 return size;
151}
152
153#ifdef CUSTOM_MODES
154CELTEncoderOpusCustomEncoder *opus_custom_encoder_create(const CELTModeOpusCustomMode *mode, int channels, int *error)
155{
156 int ret;
157 CELTEncoderOpusCustomEncoder *st = (CELTEncoderOpusCustomEncoder *)opus_alloc(opus_custom_encoder_get_size(mode, channels));
158 /* init will handle the NULL case */
159 ret = opus_custom_encoder_init(st, mode, channels);
160 if (ret != OPUS_OK0)
161 {
162 opus_custom_encoder_destroy(st);
163 st = NULL((void*)0);
164 }
165 if (error)
166 *error = ret;
167 return st;
168}
169#endif /* CUSTOM_MODES */
170
171static int opus_custom_encoder_init_arch(CELTEncoderOpusCustomEncoder *st, const CELTModeOpusCustomMode *mode,
172 int channels, int arch)
173{
174 if (channels < 0 || channels > 2)
175 return OPUS_BAD_ARG-1;
176
177 if (st==NULL((void*)0) || mode==NULL((void*)0))
178 return OPUS_ALLOC_FAIL-7;
179
180 OPUS_CLEAR((char*)st, opus_custom_encoder_get_size(mode, channels))(memset(((char*)st), 0, (opus_custom_encoder_get_size(mode, channels
))*sizeof(*((char*)st))))
;
181
182 st->mode = mode;
183 st->stream_channels = st->channels = channels;
184
185 st->upsample = 1;
186 st->start = 0;
187 st->end = st->mode->effEBands;
188 st->signalling = 1;
189 st->arch = arch;
190
191 st->constrained_vbr = 1;
192 st->clip = 1;
193
194 st->bitrate = OPUS_BITRATE_MAX-1;
195 st->vbr = 0;
196 st->force_intra = 0;
197 st->complexity = 5;
198 st->lsb_depth=24;
199
200 opus_custom_encoder_ctl(st, OPUS_RESET_STATE4028);
201
202 return OPUS_OK0;
203}
204
205#ifdef CUSTOM_MODES
206int opus_custom_encoder_init(CELTEncoderOpusCustomEncoder *st, const CELTModeOpusCustomMode *mode, int channels)
207{
208 return opus_custom_encoder_init_arch(st, mode, channels, opus_select_arch());
209}
210#endif
211
212int celt_encoder_init(CELTEncoderOpusCustomEncoder *st, opus_int32 sampling_rate, int channels,
213 int arch)
214{
215 int ret;
216 ret = opus_custom_encoder_init_arch(st,
217 opus_custom_mode_create(48000, 960, NULL((void*)0)), channels, arch);
218 if (ret != OPUS_OK0)
219 return ret;
220 st->upsample = resampling_factor(sampling_rate);
221 return OPUS_OK0;
222}
223
224#ifdef CUSTOM_MODES
225void opus_custom_encoder_destroy(CELTEncoderOpusCustomEncoder *st)
226{
227 opus_free(st);
228}
229#endif /* CUSTOM_MODES */
230
231
232static int transient_analysis(const opus_val32 * OPUS_RESTRICTrestrict in, int len, int C,
233 opus_val16 *tf_estimate, int *tf_chan, int allow_weak_transients,
234 int *weak_transient, opus_val16 tone_freq, opus_val32 toneishness)
235{
236 int i;
237 VARDECL(opus_val16, tmp)opus_val16 *tmp;
238 opus_val32 mem0,mem1;
239 int is_transient = 0;
240 opus_int32 mask_metric = 0;
241 int c;
242 opus_val16 tf_max;
243 int len2;
244 /* Forward masking: 6.7 dB/ms. */
245#ifdef FIXED_POINT
246 int forward_shift = 4;
247#else
248 opus_val16 forward_decay = QCONST16(.0625f,15)(.0625f);
249#endif
250 /* Table of 6*64/x, trained on real data to minimize the average error */
251 static const unsigned char inv_table[128] = {
252 255,255,156,110, 86, 70, 59, 51, 45, 40, 37, 33, 31, 28, 26, 25,
253 23, 22, 21, 20, 19, 18, 17, 16, 16, 15, 15, 14, 13, 13, 12, 12,
254 12, 12, 11, 11, 11, 10, 10, 10, 9, 9, 9, 9, 9, 9, 8, 8,
255 8, 8, 8, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 6, 6,
256 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 5, 5, 5, 5, 5, 5,
257 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
258 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3,
259 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 2,
260 };
261 SAVE_STACK;
262#ifdef FIXED_POINT
263 int in_shift = IMAX(0, celt_ilog2(1+celt_maxabs32(in, C*len))-14)((0) > (celt_ilog2(1+celt_maxabs16(in,C*len))-14) ? (0) : (
celt_ilog2(1+celt_maxabs16(in,C*len))-14))
;
264#endif
265 ALLOC(tmp, len, opus_val16)tmp = ((opus_val16*)__builtin_alloca (sizeof(opus_val16)*(len
)))
;
266
267 *weak_transient = 0;
268 /* For lower bitrates, let's be more conservative and have a forward masking
269 decay of 3.3 dB/ms. This avoids having to code transients at very low
270 bitrate (mostly for hybrid), which can result in unstable energy and/or
271 partial collapse. */
272 if (allow_weak_transients)
273 {
274#ifdef FIXED_POINT
275 forward_shift = 5;
276#else
277 forward_decay = QCONST16(.03125f,15)(.03125f);
278#endif
279 }
280 len2=len/2;
281 for (c=0;c<C;c++)
282 {
283 opus_val32 mean;
284 opus_int32 unmask=0;
285 opus_val32 norm;
286 opus_val16 maxE;
287 mem0=0;
288 mem1=0;
289 /* High-pass filter: (1 - 2*z^-1 + z^-2) / (1 - z^-1 + .5*z^-2) */
290 for (i=0;i<len;i++)
291 {
292#ifndef FIXED_POINT
293 float mem00;
294#endif
295 opus_val32 x,y;
296 x = SHR32(in[i+c*len],in_shift)(in[i+c*len]);
297 y = ADD32(mem0, x)((mem0)+(x));
298#ifdef FIXED_POINT
299 mem0 = mem1 + y - SHL32(x,1)(x);
300 mem1 = x - SHR32(y,1)(y);
301#else
302 /* Original code:
303 mem0 = mem1 + y - 2*x;
304 mem1 = x - .5f*y;
305 Modified code to shorten dependency chains: */
306 mem00=mem0;
307 mem0 = mem0 - x + .5f*mem1;
308 mem1 = x - mem00;
309#endif
310 tmp[i] = SROUND16(y, 2)(y);
311 /*printf("%f ", tmp[i]);*/
312 }
313 /*printf("\n");*/
314 /* First few samples are bad because we don't propagate the memory */
315 OPUS_CLEAR(tmp, 12)(memset((tmp), 0, (12)*sizeof(*(tmp))));
316
317#ifdef FIXED_POINT
318 /* Normalize tmp to max range */
319 {
320 int shift=0;
321 shift = 14-celt_ilog2(MAX16(1, celt_maxabs16(tmp, len))((1) > (celt_maxabs16(tmp, len)) ? (1) : (celt_maxabs16(tmp
, len)))
);
322 if (shift!=0)
323 {
324 for (i=0;i<len;i++)
325 tmp[i] = SHL16(tmp[i], shift)(tmp[i]);
326 }
327 }
328#endif
329
330 mean=0;
331 mem0=0;
332 /* Grouping by two to reduce complexity */
333 /* Forward pass to compute the post-echo threshold*/
334 for (i=0;i<len2;i++)
335 {
336 opus_val32 x2 = PSHR32(MULT16_16(tmp[2*i],tmp[2*i]) + MULT16_16(tmp[2*i+1],tmp[2*i+1]),4)(((opus_val32)(tmp[2*i])*(opus_val32)(tmp[2*i])) + ((opus_val32
)(tmp[2*i+1])*(opus_val32)(tmp[2*i+1])))
;
337 mean += PSHR32(x2, 12)(x2);
338#ifdef FIXED_POINT
339 /* FIXME: Use PSHR16() instead */
340 mem0 = mem0 + PSHR32(x2-mem0,forward_shift)(x2-mem0);
341 tmp[i] = PSHR32(mem0, 12)(mem0);
342#else
343 mem0 = x2 + (1.f-forward_decay)*mem0;
344 tmp[i] = forward_decay*mem0;
345#endif
346 }
347
348 mem0=0;
349 maxE=0;
350 /* Backward pass to compute the pre-echo threshold */
351 for (i=len2-1;i>=0;i--)
352 {
353 /* Backward masking: 13.9 dB/ms. */
354#ifdef FIXED_POINT
355 /* FIXME: Use PSHR16() instead */
356 mem0 = mem0 + PSHR32(SHL32(tmp[i],4)-mem0,3)((tmp[i])-mem0);
357 tmp[i] = PSHR32(mem0, 4)(mem0);
358 maxE = MAX16(maxE, tmp[i])((maxE) > (tmp[i]) ? (maxE) : (tmp[i]));
359#else
360 mem0 = tmp[i] + 0.875f*mem0;
361 tmp[i] = 0.125f*mem0;
362 maxE = MAX16(maxE, 0.125f*mem0)((maxE) > (0.125f*mem0) ? (maxE) : (0.125f*mem0));
363#endif
364 }
365 /*for (i=0;i<len2;i++)printf("%f ", tmp[i]/mean);printf("\n");*/
366
367 /* Compute the ratio of the "frame energy" over the harmonic mean of the energy.
368 This essentially corresponds to a bitrate-normalized temporal noise-to-mask
369 ratio */
370
371 /* As a compromise with the old transient detector, frame energy is the
372 geometric mean of the energy and half the max */
373#ifdef FIXED_POINT
374 /* Costs two sqrt() to avoid overflows */
375 mean = MULT16_16(celt_sqrt(mean), celt_sqrt(MULT16_16(maxE,len2>>1)))((opus_val32)(((float)sqrt(mean)))*(opus_val32)(((float)sqrt(
((opus_val32)(maxE)*(opus_val32)(len2>>1))))))
;
376#else
377 mean = celt_sqrt(mean * maxE*.5*len2)((float)sqrt(mean * maxE*.5*len2));
378#endif
379 /* Inverse of the mean energy in Q15+6 */
380 norm = SHL32(EXTEND32(len2),6+14)((len2))/ADD32(EPSILON,SHR32(mean,1))((1e-15f)+((mean)));
381 /* Compute harmonic mean discarding the unreliable boundaries
382 The data is smooth, so we only take 1/4th of the samples */
383 unmask=0;
384 /* We should never see NaNs here. If we find any, then something really bad happened and we better abort
385 before it does any damage later on. If these asserts are disabled (no hardening), then the table
386 lookup a few lines below (id = ...) is likely to crash dur to an out-of-bounds read. DO NOT FIX
387 that crash on NaN since it could result in a worse issue later on. */
388 celt_assert(!celt_isnan(tmp[0])){if (!(!((tmp[0])!=(tmp[0])))) {celt_fatal("assertion failed: "
"!celt_isnan(tmp[0])", "/root/firefox-clang/media/libopus/celt/celt_encoder.c"
, 388);}}
;
389 celt_assert(!celt_isnan(norm)){if (!(!((norm)!=(norm)))) {celt_fatal("assertion failed: " "!celt_isnan(norm)"
, "/root/firefox-clang/media/libopus/celt/celt_encoder.c", 389
);}}
;
390 for (i=12;i<len2-5;i+=4)
391 {
392 int id;
393#ifdef FIXED_POINT
394 id = MAX32(0,MIN32(127,MULT16_32_Q15(tmp[i]+EPSILON,norm)))((0) > (((127) < (((tmp[i]+1e-15f)*(norm))) ? (127) : (
((tmp[i]+1e-15f)*(norm))))) ? (0) : (((127) < (((tmp[i]+1e-15f
)*(norm))) ? (127) : (((tmp[i]+1e-15f)*(norm))))))
; /* Do not round to nearest */
395#else
396 id = (int)MAX32(0,MIN32(127,floor(64*norm*(tmp[i]+EPSILON))))((0) > (((127) < (floor(64*norm*(tmp[i]+1e-15f))) ? (127
) : (floor(64*norm*(tmp[i]+1e-15f))))) ? (0) : (((127) < (
floor(64*norm*(tmp[i]+1e-15f))) ? (127) : (floor(64*norm*(tmp
[i]+1e-15f))))))
; /* Do not round to nearest */
397#endif
398 unmask += inv_table[id];
399 }
400 /*printf("%d\n", unmask);*/
401 /* Normalize, compensate for the 1/4th of the sample and the factor of 6 in the inverse table */
402 unmask = 64*unmask*4/(6*(len2-17));
403 if (unmask>mask_metric)
404 {
405 *tf_chan = c;
406 mask_metric = unmask;
407 }
408 }
409 is_transient = mask_metric>200;
410 /* Prevent the transient detector from confusing the partial cycle of a
411 very low frequency tone with a transient. */
412 if (toneishness > QCONST32(.98f, 29)(.98f) && tone_freq < QCONST16(0.026f, 13)(0.026f))
413 is_transient = 0;
414 /* For low bitrates, define "weak transients" that need to be
415 handled differently to avoid partial collapse. */
416 if (allow_weak_transients && is_transient && mask_metric<600) {
417 is_transient = 0;
418 *weak_transient = 1;
419 }
420 /* Arbitrary metric for VBR boost */
421 tf_max = MAX16(0,celt_sqrt(27*mask_metric)-42)((0) > (((float)sqrt(27*mask_metric))-42) ? (0) : (((float
)sqrt(27*mask_metric))-42))
;
422 /* *tf_estimate = 1 + MIN16(1, sqrt(MAX16(0, tf_max-30))/20); */
423 *tf_estimate = celt_sqrt(MAX32(0, SHL32(MULT16_16(QCONST16(0.0069,14),MIN16(163,tf_max)),14)-QCONST32(0.139,28)))((float)sqrt(((0) > ((((opus_val32)((0.0069))*(opus_val32)
(((163) < (tf_max) ? (163) : (tf_max)))))-(0.139)) ? (0) :
((((opus_val32)((0.0069))*(opus_val32)(((163) < (tf_max) ?
(163) : (tf_max)))))-(0.139)))))
;
424 /*printf("%d %f\n", tf_max, mask_metric);*/
425 RESTORE_STACK;
426#ifdef FUZZING
427 is_transient = rand()&0x1;
428#endif
429 /*printf("%d %f %d\n", is_transient, (float)*tf_estimate, tf_max);*/
430 return is_transient;
431}
432
433/* Looks for sudden increases of energy to decide whether we need to patch
434 the transient decision */
435static int patch_transient_decision(celt_glog *newE, celt_glog *oldE, int nbEBands,
436 int start, int end, int C)
437{
438 int i, c;
439 opus_val32 mean_diff=0;
440 celt_glog spread_old[26];
441 /* Apply an aggressive (-6 dB/Bark) spreading function to the old frame to
442 avoid false detection caused by irrelevant bands */
443 if (C==1)
444 {
445 spread_old[start] = oldE[start];
446 for (i=start+1;i<end;i++)
447 spread_old[i] = MAXG(spread_old[i-1]-GCONST(1.0f), oldE[i])((spread_old[i-1]-(1.0f)) > (oldE[i]) ? (spread_old[i-1]-(
1.0f)) : (oldE[i]))
;
448 } else {
449 spread_old[start] = MAXG(oldE[start],oldE[start+nbEBands])((oldE[start]) > (oldE[start+nbEBands]) ? (oldE[start]) : (
oldE[start+nbEBands]))
;
450 for (i=start+1;i<end;i++)
451 spread_old[i] = MAXG(spread_old[i-1]-GCONST(1.0f),((spread_old[i-1]-(1.0f)) > (((oldE[i]) > (oldE[i+nbEBands
]) ? (oldE[i]) : (oldE[i+nbEBands]))) ? (spread_old[i-1]-(1.0f
)) : (((oldE[i]) > (oldE[i+nbEBands]) ? (oldE[i]) : (oldE[
i+nbEBands]))))
452 MAXG(oldE[i],oldE[i+nbEBands]))((spread_old[i-1]-(1.0f)) > (((oldE[i]) > (oldE[i+nbEBands
]) ? (oldE[i]) : (oldE[i+nbEBands]))) ? (spread_old[i-1]-(1.0f
)) : (((oldE[i]) > (oldE[i+nbEBands]) ? (oldE[i]) : (oldE[
i+nbEBands]))))
;
453 }
454 for (i=end-2;i>=start;i--)
455 spread_old[i] = MAXG(spread_old[i], spread_old[i+1]-GCONST(1.0f))((spread_old[i]) > (spread_old[i+1]-(1.0f)) ? (spread_old[
i]) : (spread_old[i+1]-(1.0f)))
;
456 /* Compute mean increase */
457 c=0; do {
458 for (i=IMAX(2,start)((2) > (start) ? (2) : (start));i<end-1;i++)
459 {
460 opus_val16 x1, x2;
461 x1 = MAXG(0, newE[i + c*nbEBands])((0) > (newE[i + c*nbEBands]) ? (0) : (newE[i + c*nbEBands
]))
;
462 x2 = MAXG(0, spread_old[i])((0) > (spread_old[i]) ? (0) : (spread_old[i]));
463 mean_diff = ADD32(mean_diff, MAXG(0, SUB32(x1, x2)))((mean_diff)+(((0) > (((x1)-(x2))) ? (0) : (((x1)-(x2)))))
)
;
464 }
465 } while (++c<C);
466 mean_diff = DIV32(mean_diff, C*(end-1-IMAX(2,start)))(((opus_val32)(mean_diff))/(opus_val32)(C*(end-1-((2) > (start
) ? (2) : (start)))))
;
467 /*printf("%f %f %d\n", mean_diff, max_diff, count);*/
468 return mean_diff > GCONST(1.f)(1.f);
469}
470
471/** Apply window and compute the MDCT for all sub-frames and
472 all channels in a frame */
473static void compute_mdcts(const CELTModeOpusCustomMode *mode, int shortBlocks, celt_sig * OPUS_RESTRICTrestrict in,
474 celt_sig * OPUS_RESTRICTrestrict out, int C, int CC, int LM, int upsample,
475 int arch)
476{
477 const int overlap = mode->overlap;
478 int N;
479 int B;
480 int shift;
481 int i, b, c;
482 if (shortBlocks)
483 {
484 B = shortBlocks;
485 N = mode->shortMdctSize;
486 shift = mode->maxLM;
487 } else {
488 B = 1;
489 N = mode->shortMdctSize<<LM;
490 shift = mode->maxLM-LM;
491 }
492 c=0; do {
493 for (b=0;b<B;b++)
494 {
495 /* Interleaving the sub-frames while doing the MDCTs */
496 clt_mdct_forward(&mode->mdct, in+c*(B*N+overlap)+b*N,clt_mdct_forward_c(&mode->mdct, in+c*(B*N+overlap)+b*N
, &out[b+c*N*B], mode->window, overlap, shift, B, arch
)
497 &out[b+c*N*B], mode->window, overlap, shift, B,clt_mdct_forward_c(&mode->mdct, in+c*(B*N+overlap)+b*N
, &out[b+c*N*B], mode->window, overlap, shift, B, arch
)
498 arch)clt_mdct_forward_c(&mode->mdct, in+c*(B*N+overlap)+b*N
, &out[b+c*N*B], mode->window, overlap, shift, B, arch
)
;
499 }
500 } while (++c<CC);
501 if (CC==2&&C==1)
502 {
503 for (i=0;i<B*N;i++)
504 out[i] = ADD32(HALF32(out[i]), HALF32(out[B*N+i]))(((.5f*(out[i])))+((.5f*(out[B*N+i]))));
505 }
506 if (upsample != 1)
507 {
508 c=0; do
509 {
510 int bound = B*N/upsample;
511 for (i=0;i<bound;i++)
512 out[c*B*N+i] *= upsample;
513 OPUS_CLEAR(&out[c*B*N+bound], B*N-bound)(memset((&out[c*B*N+bound]), 0, (B*N-bound)*sizeof(*(&
out[c*B*N+bound]))))
;
514 } while (++c<C);
515 }
516}
517
518
519void celt_preemphasis(const opus_res * OPUS_RESTRICTrestrict pcmp, celt_sig * OPUS_RESTRICTrestrict inp,
520 int N, int CC, int upsample, const opus_val16 *coef, celt_sig *mem, int clip)
521{
522 int i;
523 opus_val16 coef0;
524 celt_sig m;
525 int Nu;
526
527 coef0 = coef[0];
528 m = *mem;
529
530 /* Fast path for the normal 48kHz case and no clipping */
531 if (coef[1] == 0 && upsample == 1 && !clip)
532 {
533 for (i=0;i<N;i++)
534 {
535 celt_sig x;
536 x = RES2SIG(pcmp[CC*i])(32768.f*(pcmp[CC*i]));
537 /* Apply pre-emphasis */
538 inp[i] = x - m;
539 m = MULT16_32_Q15(coef0, x)((coef0)*(x));
540 }
541 *mem = m;
542 return;
543 }
544
545 Nu = N/upsample;
546 if (upsample!=1)
547 {
548 OPUS_CLEAR(inp, N)(memset((inp), 0, (N)*sizeof(*(inp))));
549 }
550 for (i=0;i<Nu;i++)
551 inp[i*upsample] = RES2SIG(pcmp[CC*i])(32768.f*(pcmp[CC*i]));
552
553#ifndef FIXED_POINT
554 if (clip)
555 {
556 /* Clip input to avoid encoding non-portable files */
557 for (i=0;i<Nu;i++)
558 inp[i*upsample] = MAX32(-65536.f, MIN32(65536.f,inp[i*upsample]))((-65536.f) > (((65536.f) < (inp[i*upsample]) ? (65536.f
) : (inp[i*upsample]))) ? (-65536.f) : (((65536.f) < (inp[
i*upsample]) ? (65536.f) : (inp[i*upsample]))))
;
559 }
560#else
561 (void)clip; /* Avoids a warning about clip being unused. */
562#endif
563#ifdef CUSTOM_MODES
564 if (coef[1] != 0)
565 {
566 opus_val16 coef1 = coef[1];
567 opus_val16 coef2 = coef[2];
568 for (i=0;i<N;i++)
569 {
570 celt_sig x, tmp;
571 x = inp[i];
572 /* Apply pre-emphasis */
573 tmp = SHL32(MULT16_32_Q15(coef2, x), 15-SIG_SHIFT)(((coef2)*(x)));
574 inp[i] = tmp + m;
575 m = MULT16_32_Q15(coef1, inp[i])((coef1)*(inp[i])) - MULT16_32_Q15(coef0, tmp)((coef0)*(tmp));
576 }
577 } else
578#endif
579 {
580 for (i=0;i<N;i++)
581 {
582 celt_sig x;
583 x = inp[i];
584 /* Apply pre-emphasis */
585 inp[i] = x - m;
586 m = MULT16_32_Q15(coef0, x)((coef0)*(x));
587 }
588 }
589 *mem = m;
590}
591
592
593
594static opus_val32 l1_metric(const celt_norm *tmp, int N, int LM, opus_val16 bias)
595{
596 int i;
597 opus_val32 L1;
598 L1 = 0;
599 for (i=0;i<N;i++)
600 L1 += EXTEND32(ABS16(tmp[i]))(((float)fabs(tmp[i])));
601 /* When in doubt, prefer good freq resolution */
602 L1 = MAC16_32_Q15(L1, LM*bias, L1)((L1)+(LM*bias)*(L1));
603 return L1;
604
605}
606
607static int tf_analysis(const CELTModeOpusCustomMode *m, int len, int isTransient,
608 int *tf_res, int lambda, celt_norm *X, int N0, int LM,
609 opus_val16 tf_estimate, int tf_chan, int *importance)
610{
611 int i;
612 VARDECL(int, metric)int *metric;
613 int cost0;
614 int cost1;
615 VARDECL(int, path0)int *path0;
616 VARDECL(int, path1)int *path1;
617 VARDECL(celt_norm, tmp)celt_norm *tmp;
618 VARDECL(celt_norm, tmp_1)celt_norm *tmp_1;
619 int sel;
620 int selcost[2];
621 int tf_select=0;
622 opus_val16 bias;
623
624 SAVE_STACK;
625 bias = MULT16_16_Q14(QCONST16(.04f,15), MAX16(-QCONST16(.25f,14), QCONST16(.5f,14)-tf_estimate))(((.04f))*(((-(.25f)) > ((.5f)-tf_estimate) ? (-(.25f)) : (
(.5f)-tf_estimate))))
;
626 /*printf("%f ", bias);*/
627
628 ALLOC(metric, len, int)metric = ((int*)__builtin_alloca (sizeof(int)*(len)));
629 ALLOC(tmp, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm)tmp = ((celt_norm*)__builtin_alloca (sizeof(celt_norm)*((m->
eBands[len]-m->eBands[len-1])<<LM)))
;
630 ALLOC(tmp_1, (m->eBands[len]-m->eBands[len-1])<<LM, celt_norm)tmp_1 = ((celt_norm*)__builtin_alloca (sizeof(celt_norm)*((m->
eBands[len]-m->eBands[len-1])<<LM)))
;
631 ALLOC(path0, len, int)path0 = ((int*)__builtin_alloca (sizeof(int)*(len)));
632 ALLOC(path1, len, int)path1 = ((int*)__builtin_alloca (sizeof(int)*(len)));
633
634 for (i=0;i<len;i++)
635 {
636 int k, N;
637 int narrow;
638 opus_val32 L1, best_L1;
639 int best_level=0;
640 N = (m->eBands[i+1]-m->eBands[i])<<LM;
641 /* band is too narrow to be split down to LM=-1 */
642 narrow = (m->eBands[i+1]-m->eBands[i])==1;
643 OPUS_COPY(tmp, &X[tf_chan*N0 + (m->eBands[i]<<LM)], N)(memcpy((tmp), (&X[tf_chan*N0 + (m->eBands[i]<<LM
)]), (N)*sizeof(*(tmp)) + 0*((tmp)-(&X[tf_chan*N0 + (m->
eBands[i]<<LM)])) ))
;
644 /* Just add the right channel if we're in stereo */
645 /*if (C==2)
646 for (j=0;j<N;j++)
647 tmp[j] = ADD16(SHR16(tmp[j], 1),SHR16(X[N0+j+(m->eBands[i]<<LM)], 1));*/
648 L1 = l1_metric(tmp, N, isTransient ? LM : 0, bias);
649 best_L1 = L1;
650 /* Check the -1 case for transients */
651 if (isTransient && !narrow)
652 {
653 OPUS_COPY(tmp_1, tmp, N)(memcpy((tmp_1), (tmp), (N)*sizeof(*(tmp_1)) + 0*((tmp_1)-(tmp
)) ))
;
654 haar1(tmp_1, N>>LM, 1<<LM);
655 L1 = l1_metric(tmp_1, N, LM+1, bias);
656 if (L1<best_L1)
657 {
658 best_L1 = L1;
659 best_level = -1;
660 }
661 }
662 /*printf ("%f ", L1);*/
663 for (k=0;k<LM+!(isTransient||narrow);k++)
664 {
665 int B;
666
667 if (isTransient)
668 B = (LM-k-1);
669 else
670 B = k+1;
671
672 haar1(tmp, N>>k, 1<<k);
673
674 L1 = l1_metric(tmp, N, B, bias);
675
676 if (L1 < best_L1)
677 {
678 best_L1 = L1;
679 best_level = k+1;
680 }
681 }
682 /*printf ("%d ", isTransient ? LM-best_level : best_level);*/
683 /* metric is in Q1 to be able to select the mid-point (-0.5) for narrower bands */
684 if (isTransient)
685 metric[i] = 2*best_level;
686 else
687 metric[i] = -2*best_level;
688 /* For bands that can't be split to -1, set the metric to the half-way point to avoid
689 biasing the decision */
690 if (narrow && (metric[i]==0 || metric[i]==-2*LM))
691 metric[i]-=1;
692 /*printf("%d ", metric[i]/2 + (!isTransient)*LM);*/
693 }
694 /*printf("\n");*/
695 /* Search for the optimal tf resolution, including tf_select */
696 tf_select = 0;
697 for (sel=0;sel<2;sel++)
698 {
699 cost0 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
700 cost1 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*sel+1]) + (isTransient ? 0 : lambda);
701 for (i=1;i<len;i++)
702 {
703 int curr0, curr1;
704 curr0 = IMIN(cost0, cost1 + lambda)((cost0) < (cost1 + lambda) ? (cost0) : (cost1 + lambda));
705 curr1 = IMIN(cost0 + lambda, cost1)((cost0 + lambda) < (cost1) ? (cost0 + lambda) : (cost1));
706 cost0 = curr0 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+0]);
707 cost1 = curr1 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*sel+1]);
708 }
709 cost0 = IMIN(cost0, cost1)((cost0) < (cost1) ? (cost0) : (cost1));
710 selcost[sel]=cost0;
711 }
712 /* For now, we're conservative and only allow tf_select=1 for transients.
713 * If tests confirm it's useful for non-transients, we could allow it. */
714 if (selcost[1]<selcost[0] && isTransient)
715 tf_select=1;
716 cost0 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
717 cost1 = importance[0]*abs(metric[0]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]) + (isTransient ? 0 : lambda);
718 /* Viterbi forward pass */
719 for (i=1;i<len;i++)
720 {
721 int curr0, curr1;
722 int from0, from1;
723
724 from0 = cost0;
725 from1 = cost1 + lambda;
726 if (from0 < from1)
727 {
728 curr0 = from0;
729 path0[i]= 0;
730 } else {
731 curr0 = from1;
732 path0[i]= 1;
733 }
734
735 from0 = cost0 + lambda;
736 from1 = cost1;
737 if (from0 < from1)
738 {
739 curr1 = from0;
740 path1[i]= 0;
741 } else {
742 curr1 = from1;
743 path1[i]= 1;
744 }
745 cost0 = curr0 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+0]);
746 cost1 = curr1 + importance[i]*abs(metric[i]-2*tf_select_table[LM][4*isTransient+2*tf_select+1]);
747 }
748 tf_res[len-1] = cost0 < cost1 ? 0 : 1;
749 /* Viterbi backward pass to check the decisions */
750 for (i=len-2;i>=0;i--)
751 {
752 if (tf_res[i+1] == 1)
753 tf_res[i] = path1[i+1];
754 else
755 tf_res[i] = path0[i+1];
756 }
757 /*printf("%d %f\n", *tf_sum, tf_estimate);*/
758 RESTORE_STACK;
759#ifdef FUZZING
760 tf_select = rand()&0x1;
761 tf_res[0] = rand()&0x1;
762 for (i=1;i<len;i++)
763 tf_res[i] = tf_res[i-1] ^ ((rand()&0xF) == 0);
764#endif
765 return tf_select;
766}
767
768static void tf_encode(int start, int end, int isTransient, int *tf_res, int LM, int tf_select, ec_enc *enc)
769{
770 int curr, i;
771 int tf_select_rsv;
772 int tf_changed;
773 int logp;
774 opus_uint32 budget;
775 opus_uint32 tell;
776 budget = enc->storage*8;
777 tell = ec_tell(enc);
778 logp = isTransient ? 2 : 4;
779 /* Reserve space to code the tf_select decision. */
780 tf_select_rsv = LM>0 && tell+logp+1 <= budget;
781 budget -= tf_select_rsv;
782 curr = tf_changed = 0;
783 for (i=start;i<end;i++)
784 {
785 if (tell+logp<=budget)
786 {
787 ec_enc_bit_logp(enc, tf_res[i] ^ curr, logp);
788 tell = ec_tell(enc);
789 curr = tf_res[i];
790 tf_changed |= curr;
791 }
792 else
793 tf_res[i] = curr;
794 logp = isTransient ? 4 : 5;
795 }
796 /* Only code tf_select if it would actually make a difference. */
797 if (tf_select_rsv &&
798 tf_select_table[LM][4*isTransient+0+tf_changed]!=
799 tf_select_table[LM][4*isTransient+2+tf_changed])
800 ec_enc_bit_logp(enc, tf_select, 1);
801 else
802 tf_select = 0;
803 for (i=start;i<end;i++)
804 tf_res[i] = tf_select_table[LM][4*isTransient+2*tf_select+tf_res[i]];
805 /*for(i=0;i<end;i++)printf("%d ", isTransient ? tf_res[i] : LM+tf_res[i]);printf("\n");*/
806}
807
808
809static int alloc_trim_analysis(const CELTModeOpusCustomMode *m, const celt_norm *X,
810 const celt_glog *bandLogE, int end, int LM, int C, int N0,
811 AnalysisInfo *analysis, opus_val16 *stereo_saving, opus_val16 tf_estimate,
812 int intensity, celt_glog surround_trim, opus_int32 equiv_rate, int arch)
813{
814 int i;
815 opus_val32 diff=0;
816 int c;
817 int trim_index;
818 opus_val16 trim = QCONST16(5.f, 8)(5.f);
819 opus_val16 logXC, logXC2;
820 /* At low bitrate, reducing the trim seems to help. At higher bitrates, it's less
821 clear what's best, so we're keeping it as it was before, at least for now. */
822 if (equiv_rate < 64000) {
823 trim = QCONST16(4.f, 8)(4.f);
824 } else if (equiv_rate < 80000) {
825 opus_int32 frac = (equiv_rate-64000) >> 10;
826 trim = QCONST16(4.f, 8)(4.f) + QCONST16(1.f/16.f, 8)(1.f/16.f)*frac;
827 }
828 if (C==2)
829 {
830 opus_val16 sum = 0; /* Q10 */
831 opus_val16 minXC; /* Q10 */
832 /* Compute inter-channel correlation for low frequencies */
833 for (i=0;i<8;i++)
834 {
835 opus_val32 partial;
836 partial = celt_inner_prod(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM)],((*CELT_INNER_PROD_IMPL[(arch) & 7])(&X[m->eBands[
i]<<LM], &X[N0+(m->eBands[i]<<LM)], (m->
eBands[i+1]-m->eBands[i])<<LM))
837 (m->eBands[i+1]-m->eBands[i])<<LM, arch)((*CELT_INNER_PROD_IMPL[(arch) & 7])(&X[m->eBands[
i]<<LM], &X[N0+(m->eBands[i]<<LM)], (m->
eBands[i+1]-m->eBands[i])<<LM))
;
838 sum = ADD16(sum, EXTRACT16(SHR32(partial, 18)))((sum)+(((partial))));
839 }
840 sum = MULT16_16_Q15(QCONST16(1.f/8, 15), sum)(((1.f/8))*(sum));
841 sum = MIN16(QCONST16(1.f, 10), ABS16(sum))(((1.f)) < (((float)fabs(sum))) ? ((1.f)) : (((float)fabs(
sum))))
;
842 minXC = sum;
843 for (i=8;i<intensity;i++)
844 {
845 opus_val32 partial;
846 partial = celt_inner_prod(&X[m->eBands[i]<<LM], &X[N0+(m->eBands[i]<<LM)],((*CELT_INNER_PROD_IMPL[(arch) & 7])(&X[m->eBands[
i]<<LM], &X[N0+(m->eBands[i]<<LM)], (m->
eBands[i+1]-m->eBands[i])<<LM))
847 (m->eBands[i+1]-m->eBands[i])<<LM, arch)((*CELT_INNER_PROD_IMPL[(arch) & 7])(&X[m->eBands[
i]<<LM], &X[N0+(m->eBands[i]<<LM)], (m->
eBands[i+1]-m->eBands[i])<<LM))
;
848 minXC = MIN16(minXC, ABS16(EXTRACT16(SHR32(partial, 18))))((minXC) < (((float)fabs(((partial))))) ? (minXC) : (((float
)fabs(((partial))))))
;
849 }
850 minXC = MIN16(QCONST16(1.f, 10), ABS16(minXC))(((1.f)) < (((float)fabs(minXC))) ? ((1.f)) : (((float)fabs
(minXC))))
;
851 /*printf ("%f\n", sum);*/
852 /* mid-side savings estimations based on the LF average*/
853 logXC = celt_log2(QCONST32(1.001f, 20)-MULT16_16(sum, sum))((float)(1.442695040888963387*log((1.001f)-((opus_val32)(sum)
*(opus_val32)(sum)))))
;
854 /* mid-side savings estimations based on min correlation */
855 logXC2 = MAX16(HALF16(logXC), celt_log2(QCONST32(1.001f, 20)-MULT16_16(minXC, minXC)))(((.5f*(logXC))) > (((float)(1.442695040888963387*log((1.001f
)-((opus_val32)(minXC)*(opus_val32)(minXC)))))) ? ((.5f*(logXC
))) : (((float)(1.442695040888963387*log((1.001f)-((opus_val32
)(minXC)*(opus_val32)(minXC)))))))
;
856#ifdef FIXED_POINT
857 /* Compensate for Q20 vs Q14 input and convert output to Q8 */
858 logXC = PSHR32(logXC-QCONST16(6.f, 10),10-8)(logXC-(6.f));
859 logXC2 = PSHR32(logXC2-QCONST16(6.f, 10),10-8)(logXC2-(6.f));
860#endif
861
862 trim += MAX16(-QCONST16(4.f, 8), MULT16_16_Q15(QCONST16(.75f,15),logXC))((-(4.f)) > ((((.75f))*(logXC))) ? (-(4.f)) : ((((.75f))*(
logXC))))
;
863 *stereo_saving = MIN16(*stereo_saving + QCONST16(0.25f, 8), -HALF16(logXC2))((*stereo_saving + (0.25f)) < (-(.5f*(logXC2))) ? (*stereo_saving
+ (0.25f)) : (-(.5f*(logXC2))))
;
864 }
865
866 /* Estimate spectral tilt */
867 c=0; do {
868 for (i=0;i<end-1;i++)
869 {
870 diff += SHR32(bandLogE[i+c*m->nbEBands], 5)(bandLogE[i+c*m->nbEBands])*(opus_int32)(2+2*i-end);
871 }
872 } while (++c<C);
873 diff /= C*(end-1);
874 /*printf("%f\n", diff);*/
875 trim -= MAX32(-QCONST16(2.f, 8), MIN32(QCONST16(2.f, 8), SHR32(diff+QCONST32(1.f, DB_SHIFT-5),DB_SHIFT-13)/6 ))((-(2.f)) > ((((2.f)) < ((diff+(1.f))/6) ? ((2.f)) : ((
diff+(1.f))/6))) ? (-(2.f)) : ((((2.f)) < ((diff+(1.f))/6)
? ((2.f)) : ((diff+(1.f))/6))))
;
876 trim -= SHR16(surround_trim, DB_SHIFT-8)(surround_trim);
877 trim -= 2*SHR16(tf_estimate, 14-8)(tf_estimate);
878#ifndef DISABLE_FLOAT_API
879 if (analysis->valid)
880 {
881 trim -= MAX16(-QCONST16(2.f, 8), MIN16(QCONST16(2.f, 8),((-(2.f)) > ((((2.f)) < ((opus_val16)((2.f)*(analysis->
tonality_slope+.05f))) ? ((2.f)) : ((opus_val16)((2.f)*(analysis
->tonality_slope+.05f))))) ? (-(2.f)) : ((((2.f)) < ((opus_val16
)((2.f)*(analysis->tonality_slope+.05f))) ? ((2.f)) : ((opus_val16
)((2.f)*(analysis->tonality_slope+.05f))))))
882 (opus_val16)(QCONST16(2.f, 8)*(analysis->tonality_slope+.05f))))((-(2.f)) > ((((2.f)) < ((opus_val16)((2.f)*(analysis->
tonality_slope+.05f))) ? ((2.f)) : ((opus_val16)((2.f)*(analysis
->tonality_slope+.05f))))) ? (-(2.f)) : ((((2.f)) < ((opus_val16
)((2.f)*(analysis->tonality_slope+.05f))) ? ((2.f)) : ((opus_val16
)((2.f)*(analysis->tonality_slope+.05f))))))
;
883 }
884#else
885 (void)analysis;
886#endif
887
888#ifdef FIXED_POINT
889 trim_index = PSHR32(trim, 8)(trim);
890#else
891 trim_index = (int)floor(.5f+trim);
892#endif
893 trim_index = IMAX(0, IMIN(10, trim_index))((0) > (((10) < (trim_index) ? (10) : (trim_index))) ? (
0) : (((10) < (trim_index) ? (10) : (trim_index))))
;
894 /*printf("%d\n", trim_index);*/
895#ifdef FUZZING
896 trim_index = rand()%11;
897#endif
898 return trim_index;
899}
900
901static int stereo_analysis(const CELTModeOpusCustomMode *m, const celt_norm *X,
902 int LM, int N0)
903{
904 int i;
905 int thetas;
906 opus_val32 sumLR = EPSILON1e-15f, sumMS = EPSILON1e-15f;
907
908 /* Use the L1 norm to model the entropy of the L/R signal vs the M/S signal */
909 for (i=0;i<13;i++)
910 {
911 int j;
912 for (j=m->eBands[i]<<LM;j<m->eBands[i+1]<<LM;j++)
913 {
914 opus_val32 L, R, M, S;
915 /* We cast to 32-bit first because of the -32768 case */
916 L = EXTEND32(X[j])(X[j]);
917 R = EXTEND32(X[N0+j])(X[N0+j]);
918 M = ADD32(L, R)((L)+(R));
919 S = SUB32(L, R)((L)-(R));
920 sumLR = ADD32(sumLR, ADD32(ABS32(L), ABS32(R)))((sumLR)+(((((float)fabs(L)))+(((float)fabs(R))))));
921 sumMS = ADD32(sumMS, ADD32(ABS32(M), ABS32(S)))((sumMS)+(((((float)fabs(M)))+(((float)fabs(S))))));
922 }
923 }
924 sumMS = MULT16_32_Q15(QCONST16(0.707107f, 15), sumMS)(((0.707107f))*(sumMS));
925 thetas = 13;
926 /* We don't need thetas for lower bands with LM<=1 */
927 if (LM<=1)
928 thetas -= 8;
929 return MULT16_32_Q15((m->eBands[13]<<(LM+1))+thetas, sumMS)(((m->eBands[13]<<(LM+1))+thetas)*(sumMS))
930 > MULT16_32_Q15(m->eBands[13]<<(LM+1), sumLR)((m->eBands[13]<<(LM+1))*(sumLR));
931}
932
933#define MSWAP(a,b)do {celt_glog tmp = a;a=b;b=tmp;} while(0) do {celt_glog tmp = a;a=b;b=tmp;} while(0)
934static celt_glog median_of_5(const celt_glog *x)
935{
936 celt_glog t0, t1, t2, t3, t4;
937 t2 = x[2];
938 if (x[0] > x[1])
939 {
940 t0 = x[1];
941 t1 = x[0];
942 } else {
943 t0 = x[0];
944 t1 = x[1];
945 }
946 if (x[3] > x[4])
947 {
948 t3 = x[4];
949 t4 = x[3];
950 } else {
951 t3 = x[3];
952 t4 = x[4];
953 }
954 if (t0 > t3)
955 {
956 MSWAP(t0, t3)do {celt_glog tmp = t0;t0=t3;t3=tmp;} while(0);
957 MSWAP(t1, t4)do {celt_glog tmp = t1;t1=t4;t4=tmp;} while(0);
958 }
959 if (t2 > t1)
960 {
961 if (t1 < t3)
962 return MING(t2, t3)((t2) < (t3) ? (t2) : (t3));
963 else
964 return MING(t4, t1)((t4) < (t1) ? (t4) : (t1));
965 } else {
966 if (t2 < t3)
967 return MING(t1, t3)((t1) < (t3) ? (t1) : (t3));
968 else
969 return MING(t2, t4)((t2) < (t4) ? (t2) : (t4));
970 }
971}
972
973static celt_glog median_of_3(const celt_glog *x)
974{
975 celt_glog t0, t1, t2;
976 if (x[0] > x[1])
977 {
978 t0 = x[1];
979 t1 = x[0];
980 } else {
981 t0 = x[0];
982 t1 = x[1];
983 }
984 t2 = x[2];
985 if (t1 < t2)
986 return t1;
987 else if (t0 < t2)
988 return t2;
989 else
990 return t0;
991}
992
993static celt_glog dynalloc_analysis(const celt_glog *bandLogE, const celt_glog *bandLogE2, const celt_glog *oldBandE,
994 int nbEBands, int start, int end, int C, int *offsets, int lsb_depth, const opus_int16 *logN,
995 int isTransient, int vbr, int constrained_vbr, const opus_int16 *eBands, int LM,
996 int effectiveBytes, opus_int32 *tot_boost_, int lfe, celt_glog *surround_dynalloc,
997 AnalysisInfo *analysis, int *importance, int *spread_weight, opus_val16 tone_freq, opus_val32 toneishness)
998{
999 int i, c;
1000 opus_int32 tot_boost=0;
1001 celt_glog maxDepth;
1002 VARDECL(celt_glog, follower)celt_glog *follower;
1003 VARDECL(celt_glog, noise_floor)celt_glog *noise_floor;
1004 VARDECL(celt_glog, bandLogE3)celt_glog *bandLogE3;
1005 SAVE_STACK;
1006 ALLOC(follower, C*nbEBands, celt_glog)follower = ((celt_glog*)__builtin_alloca (sizeof(celt_glog)*(
C*nbEBands)))
;
1007 ALLOC(noise_floor, C*nbEBands, celt_glog)noise_floor = ((celt_glog*)__builtin_alloca (sizeof(celt_glog
)*(C*nbEBands)))
;
1008 ALLOC(bandLogE3, nbEBands, celt_glog)bandLogE3 = ((celt_glog*)__builtin_alloca (sizeof(celt_glog)*
(nbEBands)))
;
1009 OPUS_CLEAR(offsets, nbEBands)(memset((offsets), 0, (nbEBands)*sizeof(*(offsets))));
1010 /* Dynamic allocation code */
1011 maxDepth=-GCONST(31.9f)(31.9f);
1012 for (i=0;i<end;i++)
1
Assuming 'i' is >= 'end'
2
Loop condition is false. Execution continues on line 1020
1013 {
1014 /* Noise floor must take into account eMeans, the depth, the width of the bands
1015 and the preemphasis filter (approx. square of bark band ID) */
1016 noise_floor[i] = GCONST(0.0625f)(0.0625f)*logN[i]
1017 +GCONST(.5f)(.5f)+SHL32(9-lsb_depth,DB_SHIFT)(9-lsb_depth)-SHL32(eMeans[i],DB_SHIFT-4)(eMeans[i])
1018 +GCONST(.0062f)(.0062f)*(i+5)*(i+5);
1019 }
1020 c=0;do
5
Loop condition is false. Exiting loop
1021 {
1022 for (i=0;i
2.1
'i' is >= 'end'
<end;i++)
3
Loop condition is false. Execution continues on line 1024
1023 maxDepth = MAXG(maxDepth, bandLogE[c*nbEBands+i]-noise_floor[i])((maxDepth) > (bandLogE[c*nbEBands+i]-noise_floor[i]) ? (maxDepth
) : (bandLogE[c*nbEBands+i]-noise_floor[i]))
;
1024 } while (++c<C);
4
Assuming the condition is false
1025 {
1026 /* Compute a really simple masking model to avoid taking into account completely masked
1027 bands when computing the spreading decision. */
1028 VARDECL(celt_glog, mask)celt_glog *mask;
1029 VARDECL(celt_glog, sig)celt_glog *sig;
1030 ALLOC(mask, nbEBands, celt_glog)mask = ((celt_glog*)__builtin_alloca (sizeof(celt_glog)*(nbEBands
)))
;
1031 ALLOC(sig, nbEBands, celt_glog)sig = ((celt_glog*)__builtin_alloca (sizeof(celt_glog)*(nbEBands
)))
;
1032 for (i=0;i
5.1
'i' is >= 'end'
<end;i++)
6
Loop condition is false. Execution continues on line 1034
1033 mask[i] = bandLogE[i]-noise_floor[i];
1034 if (C
6.1
'C' is not equal to 2
==2)
7
Taking false branch
1035 {
1036 for (i=0;i<end;i++)
1037 mask[i] = MAXG(mask[i], bandLogE[nbEBands+i]-noise_floor[i])((mask[i]) > (bandLogE[nbEBands+i]-noise_floor[i]) ? (mask
[i]) : (bandLogE[nbEBands+i]-noise_floor[i]))
;
1038 }
1039 OPUS_COPY(sig, mask, end)(memcpy((sig), (mask), (end)*sizeof(*(sig)) + 0*((sig)-(mask)
) ))
;
1040 for (i=1;i
7.1
'i' is >= 'end'
<end;i++)
8
Loop condition is false. Execution continues on line 1042
1041 mask[i] = MAXG(mask[i], mask[i-1] - GCONST(2.f))((mask[i]) > (mask[i-1] - (2.f)) ? (mask[i]) : (mask[i-1] -
(2.f)))
;
1042 for (i=end-2;i>=0;i--)
9
Assuming 'i' is < 0
10
Loop condition is false. Execution continues on line 1044
1043 mask[i] = MAXG(mask[i], mask[i+1] - GCONST(3.f))((mask[i]) > (mask[i+1] - (3.f)) ? (mask[i]) : (mask[i+1] -
(3.f)))
;
1044 for (i=0;i
10.1
'i' is >= 'end'
<end;i++)
1045 {
1046 /* Compute SMR: Mask is never more than 72 dB below the peak and never below the noise floor.*/
1047 celt_glog smr = sig[i]-MAXG(MAXG(0, maxDepth-GCONST(12.f)), mask[i])((((0) > (maxDepth-(12.f)) ? (0) : (maxDepth-(12.f)))) >
(mask[i]) ? (((0) > (maxDepth-(12.f)) ? (0) : (maxDepth-(
12.f)))) : (mask[i]))
;
1048 /* Clamp SMR to make sure we're not shifting by something negative or too large. */
1049#ifdef FIXED_POINT
1050 /* FIXME: Use PSHR16() instead */
1051 int shift = -PSHR32(MAXG(-GCONST(5.f), MING(0, smr)), DB_SHIFT)(((-(5.f)) > (((0) < (smr) ? (0) : (smr))) ? (-(5.f)) :
(((0) < (smr) ? (0) : (smr)))))
;
1052#else
1053 int shift = IMIN(5, IMAX(0, -(int)floor(.5f + smr)))((5) < (((0) > (-(int)floor(.5f + smr)) ? (0) : (-(int)
floor(.5f + smr)))) ? (5) : (((0) > (-(int)floor(.5f + smr
)) ? (0) : (-(int)floor(.5f + smr)))))
;
1054#endif
1055 spread_weight[i] = 32 >> shift;
1056 }
1057 /*for (i=0;i<end;i++)
1058 printf("%d ", spread_weight[i]);
1059 printf("\n");*/
1060 }
1061 /* Make sure that dynamic allocation can't make us bust the budget.
1062 We enable the feature starting at 24 kb/s for 20-ms frames
1063 and 96 kb/s for 2.5 ms frames. */
1064 if (effectiveBytes >= (30 + 5*LM) && !lfe)
11
Assuming the condition is true
12
Assuming 'lfe' is 0
13
Taking true branch
1065 {
1066 int last=0;
1067 c=0;do
1068 {
1069 celt_glog offset;
1070 celt_glog tmp;
1071 celt_glog *f;
1072 OPUS_COPY(bandLogE3, &bandLogE2[c*nbEBands], end)(memcpy((bandLogE3), (&bandLogE2[c*nbEBands]), (end)*sizeof
(*(bandLogE3)) + 0*((bandLogE3)-(&bandLogE2[c*nbEBands]))
))
;
1073 if (LM==0) {
14
Assuming 'LM' is not equal to 0
15
Taking false branch
1074 /* For 2.5 ms frames, the first 8 bands have just one bin, so the
1075 energy is highly unreliable (high variance). For that reason,
1076 we take the max with the previous energy so that at least 2 bins
1077 are getting used. */
1078 for (i=0;i<IMIN(8,end)((8) < (end) ? (8) : (end));i++) bandLogE3[i] = MAXG(bandLogE2[c*nbEBands+i], oldBandE[c*nbEBands+i])((bandLogE2[c*nbEBands+i]) > (oldBandE[c*nbEBands+i]) ? (bandLogE2
[c*nbEBands+i]) : (oldBandE[c*nbEBands+i]))
;
1079 }
1080 f = &follower[c*nbEBands];
1081 f[0] = bandLogE3[0];
1082 for (i=1;i
15.1
'i' is >= 'end'
<end;i++)
16
Loop condition is false. Execution continues on line 1091
1083 {
1084 /* The last band to be at least 3 dB higher than the previous one
1085 is the last we'll consider. Otherwise, we run into problems on
1086 bandlimited signals. */
1087 if (bandLogE3[i] > bandLogE3[i-1]+GCONST(.5f)(.5f))
1088 last=i;
1089 f[i] = MING(f[i-1]+GCONST(1.5f), bandLogE3[i])((f[i-1]+(1.5f)) < (bandLogE3[i]) ? (f[i-1]+(1.5f)) : (bandLogE3
[i]))
;
1090 }
1091 for (i=last-1;i>=0;i--)
17
Loop condition is false. Execution continues on line 1097
1092 f[i] = MING(f[i], MING(f[i+1]+GCONST(2.f), bandLogE3[i]))((f[i]) < (((f[i+1]+(2.f)) < (bandLogE3[i]) ? (f[i+1]+(
2.f)) : (bandLogE3[i]))) ? (f[i]) : (((f[i+1]+(2.f)) < (bandLogE3
[i]) ? (f[i+1]+(2.f)) : (bandLogE3[i]))))
;
1093
1094 /* Combine with a median filter to avoid dynalloc triggering unnecessarily.
1095 The "offset" value controls how conservative we are -- a higher offset
1096 reduces the impact of the median filter and makes dynalloc use more bits. */
1097 offset = GCONST(1.f)(1.f);
1098 for (i=2;i<end-2;i++)
18
Loop condition is false. Execution continues on line 1100
1099 f[i] = MAXG(f[i], median_of_5(&bandLogE3[i-2])-offset)((f[i]) > (median_of_5(&bandLogE3[i-2])-offset) ? (f[i
]) : (median_of_5(&bandLogE3[i-2])-offset))
;
1100 tmp = median_of_3(&bandLogE3[0])-offset;
1101 f[0] = MAXG(f[0], tmp)((f[0]) > (tmp) ? (f[0]) : (tmp));
19
Assuming the condition is false
20
'?' condition is false
1102 f[1] = MAXG(f[1], tmp)((f[1]) > (tmp) ? (f[1]) : (tmp));
21
The left operand of '>' is a garbage value
1103 tmp = median_of_3(&bandLogE3[end-3])-offset;
1104 f[end-2] = MAXG(f[end-2], tmp)((f[end-2]) > (tmp) ? (f[end-2]) : (tmp));
1105 f[end-1] = MAXG(f[end-1], tmp)((f[end-1]) > (tmp) ? (f[end-1]) : (tmp));
1106
1107 for (i=0;i<end;i++)
1108 f[i] = MAXG(f[i], noise_floor[i])((f[i]) > (noise_floor[i]) ? (f[i]) : (noise_floor[i]));
1109 } while (++c<C);
1110 if (C==2)
1111 {
1112 for (i=start;i<end;i++)
1113 {
1114 /* Consider 24 dB "cross-talk" */
1115 follower[nbEBands+i] = MAXG(follower[nbEBands+i], follower[ i]-GCONST(4.f))((follower[nbEBands+i]) > (follower[ i]-(4.f)) ? (follower
[nbEBands+i]) : (follower[ i]-(4.f)))
;
1116 follower[ i] = MAXG(follower[ i], follower[nbEBands+i]-GCONST(4.f))((follower[ i]) > (follower[nbEBands+i]-(4.f)) ? (follower
[ i]) : (follower[nbEBands+i]-(4.f)))
;
1117 follower[i] = HALF32(MAXG(0, bandLogE[i]-follower[i]) + MAXG(0, bandLogE[nbEBands+i]-follower[nbEBands+i]))(.5f*(((0) > (bandLogE[i]-follower[i]) ? (0) : (bandLogE[i
]-follower[i])) + ((0) > (bandLogE[nbEBands+i]-follower[nbEBands
+i]) ? (0) : (bandLogE[nbEBands+i]-follower[nbEBands+i]))))
;
1118 }
1119 } else {
1120 for (i=start;i<end;i++)
1121 {
1122 follower[i] = MAXG(0, bandLogE[i]-follower[i])((0) > (bandLogE[i]-follower[i]) ? (0) : (bandLogE[i]-follower
[i]))
;
1123 }
1124 }
1125 for (i=start;i<end;i++)
1126 follower[i] = MAXG(follower[i], surround_dynalloc[i])((follower[i]) > (surround_dynalloc[i]) ? (follower[i]) : (
surround_dynalloc[i]))
;
1127 for (i=start;i<end;i++)
1128 {
1129#ifdef FIXED_POINT
1130 importance[i] = PSHR32(13*celt_exp2_db(MING(follower[i], GCONST(4.f))), 16)(13*((float)exp(0.6931471805599453094*(((follower[i]) < ((
4.f)) ? (follower[i]) : ((4.f)))))))
;
1131#else
1132 importance[i] = (int)floor(.5f+13*celt_exp2_db(MING(follower[i], GCONST(4.f)))((float)exp(0.6931471805599453094*(((follower[i]) < ((4.f)
) ? (follower[i]) : ((4.f))))))
);
1133#endif
1134 }
1135 /* For non-transient CBR/CVBR frames, halve the dynalloc contribution */
1136 if ((!vbr || constrained_vbr)&&!isTransient)
1137 {
1138 for (i=start;i<end;i++)
1139 follower[i] = HALF32(follower[i])(.5f*(follower[i]));
1140 }
1141 for (i=start;i<end;i++)
1142 {
1143 if (i<8)
1144 follower[i] *= 2;
1145 if (i>=12)
1146 follower[i] = HALF32(follower[i])(.5f*(follower[i]));
1147 }
1148 /* Compensate for Opus' under-allocation on tones. */
1149 if (toneishness > QCONST32(.98f, 29)(.98f)) {
1150#ifdef FIXED_POINT
1151 int freq_bin = PSHR32(MULT16_16(tone_freq, QCONST16(120/M_PI, 9)), 13+9)(((opus_val32)(tone_freq)*(opus_val32)((120/3.14159265358979323846
))))
;
1152#else
1153 int freq_bin = (int)floor(.5 + tone_freq*120/M_PI3.14159265358979323846);
1154#endif
1155 for (i=start;i<end;i++) {
1156 if (freq_bin >= eBands[i] && freq_bin <= eBands[i+1]) follower[i] += GCONST(2.f)(2.f);
1157 if (freq_bin >= eBands[i]-1 && freq_bin <= eBands[i+1]+1) follower[i] += GCONST(1.f)(1.f);
1158 if (freq_bin >= eBands[i]-2 && freq_bin <= eBands[i+1]+2) follower[i] += GCONST(1.f)(1.f);
1159 if (freq_bin >= eBands[i]-3 && freq_bin <= eBands[i+1]+3) follower[i] += GCONST(.5f)(.5f);
1160 }
1161 }
1162#ifdef DISABLE_FLOAT_API
1163 (void)analysis;
1164#else
1165 if (analysis->valid)
1166 {
1167 for (i=start;i<IMIN(LEAK_BANDS, end)((19) < (end) ? (19) : (end));i++)
1168 follower[i] = follower[i] + GCONST(1.f/64.f)(1.f/64.f)*analysis->leak_boost[i];
1169 }
1170#endif
1171 for (i=start;i<end;i++)
1172 {
1173 int width;
1174 int boost;
1175 int boost_bits;
1176
1177 follower[i] = MING(follower[i], GCONST(4))((follower[i]) < ((4)) ? (follower[i]) : ((4)));
1178
1179 follower[i] = SHR32(follower[i], 8)(follower[i]);
1180 width = C*(eBands[i+1]-eBands[i])<<LM;
1181 if (width<6)
1182 {
1183 boost = (int)SHR32(follower[i],DB_SHIFT-8)(follower[i]);
1184 boost_bits = boost*width<<BITRES3;
1185 } else if (width > 48) {
1186 boost = (int)SHR32(follower[i]*8,DB_SHIFT-8)(follower[i]*8);
1187 boost_bits = (boost*width<<BITRES3)/8;
1188 } else {
1189 boost = (int)SHR32(follower[i]*width/6,DB_SHIFT-8)(follower[i]*width/6);
1190 boost_bits = boost*6<<BITRES3;
1191 }
1192 /* For CBR and non-transient CVBR frames, limit dynalloc to 2/3 of the bits */
1193 if ((!vbr || (constrained_vbr&&!isTransient))
1194 && (tot_boost+boost_bits)>>BITRES3>>3 > 2*effectiveBytes/3)
1195 {
1196 opus_int32 cap = ((2*effectiveBytes/3)<<BITRES3<<3);
1197 offsets[i] = cap-tot_boost;
1198 tot_boost = cap;
1199 break;
1200 } else {
1201 offsets[i] = boost;
1202 tot_boost += boost_bits;
1203 }
1204 }
1205 } else {
1206 for (i=start;i<end;i++)
1207 importance[i] = 13;
1208 }
1209 *tot_boost_ = tot_boost;
1210 RESTORE_STACK;
1211 return maxDepth;
1212}
1213
1214#ifdef FIXED_POINT
1215void normalize_tone_input(opus_val16 *x, int len) {
1216 opus_val32 ac0=len;
1217 int i;
1218 int shift;
1219 for (i=0;i<len;i++) {
1220 ac0 = ADD32(ac0, SHR32(MULT16_16(x[i], x[i]), 10))((ac0)+((((opus_val32)(x[i])*(opus_val32)(x[i])))));
1221 }
1222 shift = 5 - (28-celt_ilog2(ac0))/2;
1223 if (shift > 0) {
1224 for (i=0;i<len;i++) {
1225 x[i] = PSHR32(x[i], shift)(x[i]);
1226 }
1227 }
1228}
1229int acos_approx(opus_val32 x) {
1230 opus_val16 x14;
1231 opus_val32 tmp;
1232 int flip = x<0;
1233 x = abs(x);
1234 x14 = x>>15;
1235 tmp = (762*x14>>14)-3308;
1236 tmp = (tmp*x14>>14)+25726;
1237 tmp = tmp*celt_sqrt(IMAX(0, (1<<30) - (x<<1)))((float)sqrt(((0) > ((1<<30) - (x<<1)) ? (0) :
((1<<30) - (x<<1)))))
>>16;
1238 if (flip) tmp = 25736 - tmp;
1239 return tmp;
1240}
1241#endif
1242
1243/* Compute the LPC coefficients using a least-squares fit for both forward and backward prediction. */
1244static int tone_lpc(const opus_val16 *x, int len, int delay, opus_val32 *lpc) {
1245 int i;
1246 opus_val32 r00=0, r01=0, r11=0, r02=0, r12=0, r22=0;
1247 opus_val32 edges;
1248 opus_val32 num0, num1, den;
1249 celt_assert(len > 2*delay){if (!(len > 2*delay)) {celt_fatal("assertion failed: " "len > 2*delay"
, "/root/firefox-clang/media/libopus/celt/celt_encoder.c", 1249
);}}
;
1250 /* Compute correlations as if using the forward prediction covariance method. */
1251 for (i=0;i<len-2*delay;i++) {
1252 r00 += MULT16_16(x[i],x[i])((opus_val32)(x[i])*(opus_val32)(x[i]));
1253 r01 += MULT16_16(x[i],x[i+delay])((opus_val32)(x[i])*(opus_val32)(x[i+delay]));
1254 r02 += MULT16_16(x[i],x[i+2*delay])((opus_val32)(x[i])*(opus_val32)(x[i+2*delay]));
1255 }
1256 edges = 0;
1257 for (i=0;i<delay;i++) edges += MULT16_16(x[len+i-2*delay],x[len+i-2*delay])((opus_val32)(x[len+i-2*delay])*(opus_val32)(x[len+i-2*delay]
))
- MULT16_16(x[i],x[i])((opus_val32)(x[i])*(opus_val32)(x[i]));
1258 r11 = r00+edges;
1259 edges = 0;
1260 for (i=0;i<delay;i++) edges += MULT16_16(x[len+i-delay],x[len+i-delay])((opus_val32)(x[len+i-delay])*(opus_val32)(x[len+i-delay])) - MULT16_16(x[i+delay],x[i+delay])((opus_val32)(x[i+delay])*(opus_val32)(x[i+delay]));
1261 r22 = r11+edges;
1262 edges = 0;
1263 for (i=0;i<delay;i++) edges += MULT16_16(x[len+i-2*delay],x[len+i-delay])((opus_val32)(x[len+i-2*delay])*(opus_val32)(x[len+i-delay])) - MULT16_16(x[i],x[i+delay])((opus_val32)(x[i])*(opus_val32)(x[i+delay]));
1264 r12 = r01+edges;
1265 /* Reverse and sum to get the backward contribution. */
1266 {
1267 opus_val32 R00, R01, R11, R02, R12, R22;
1268 R00 = r00 + r22;
1269 R01 = r01 + r12;
1270 R11 = 2*r11;
1271 R02 = 2*r02;
1272 R12 = r12 + r01;
1273 R22 = r00 + r22;
1274 r00 = R00;
1275 r01 = R01;
1276 r11 = R11;
1277 r02 = R02;
1278 r12 = R12;
1279 r22 = R22;
1280 }
1281 /* Solve A*x=b, where A=[r00, r01; r01, r11] and b=[r02; r12]. */
1282 den = MULT32_32_Q31(r00,r11)((r00)*(r11)) - MULT32_32_Q31(r01,r01)((r01)*(r01));
1283#ifdef FIXED_POINT
1284 if (den <= SHR32(MULT32_32_Q31(r00,r11), 10)(((r00)*(r11)))) return 1;
1285#else
1286 if (den < .001f*MULT32_32_Q31(r00,r11)((r00)*(r11))) return 1;
1287#endif
1288 num1 = MULT32_32_Q31(r02,r11)((r02)*(r11)) - MULT32_32_Q31(r01,r12)((r01)*(r12));
1289 if (num1 >= den) lpc[1] = QCONST32(1.f, 29)(1.f);
1290 else if (num1 <= -den) lpc[1] = -QCONST32(1.f, 29)(1.f);
1291 else lpc[1] = frac_div32_q29(num1, den)((float)(num1)/(den));
1292 num0 = MULT32_32_Q31(r00,r12)((r00)*(r12)) - MULT32_32_Q31(r02,r01)((r02)*(r01));
1293 if (HALF32(num0)(.5f*(num0)) >= den) lpc[0] = QCONST32(1.999999f, 29)(1.999999f);
1294 else if (HALF32(num0)(.5f*(num0)) <= -den) lpc[0] = -QCONST32(1.999999f, 29)(1.999999f);
1295 else lpc[0] = frac_div32_q29(num0, den)((float)(num0)/(den));
1296 /*printf("%f %f\n", lpc[0], lpc[1]);*/
1297 return 0;
1298}
1299
1300/* Detects pure of nearly pure tones so we can prevent them from causing problems with the encoder. */
1301static opus_val16 tone_detect(const celt_sig *in, int CC, int N, opus_val32 *toneishness, opus_int32 Fs) {
1302 int i;
1303 int delay = 1;
1304 int fail;
1305 opus_val32 lpc[2];
1306 opus_val16 freq;
1307 VARDECL(opus_val16, x)opus_val16 *x;
1308 ALLOC(x, N, opus_val16)x = ((opus_val16*)__builtin_alloca (sizeof(opus_val16)*(N)));
1309 /* Shift by SIG_SHIFT+1 (+2 for stereo) to account for HF gain of the preemphasis filter. */
1310 if (CC==2) {
1311 for (i=0;i<N;i++) x[i] = PSHR32(ADD32(in[i], in[i+N]), SIG_SHIFT+3)(((in[i])+(in[i+N])));
1312 } else {
1313 for (i=0;i<N;i++) x[i] = PSHR32(in[i], SIG_SHIFT+2)(in[i]);
1314 }
1315#ifdef FIXED_POINT
1316 normalize_tone_input(x, N);
1317#endif
1318 fail = tone_lpc(x, N, delay, lpc);
1319 /* If our LPC filter resonates too close to DC, retry the analysis with down-sampling. */
1320 while (delay <= Fs/3000 && (fail || (lpc[0] > QCONST32(1.f, 29)(1.f) && lpc[1] < 0))) {
1321 delay *= 2;
1322 fail = tone_lpc(x, N, delay, lpc);
1323 }
1324 /* Check that our filter has complex roots. */
1325 if (!fail && MULT32_32_Q31(lpc[0],lpc[0])((lpc[0])*(lpc[0])) + MULT32_32_Q31(QCONST32(3.999999, 29), lpc[1])(((3.999999))*(lpc[1])) < 0) {
1326 /* Squared radius of the poles. */
1327 *toneishness = -lpc[1];
1328#ifdef FIXED_POINT
1329 freq = acos_approx(lpc[0]>>1)/delay;
1330#else
1331 freq = acos(.5f*lpc[0])/delay;
1332#endif
1333 } else {
1334 freq = -1;
1335 *toneishness=0;
1336 }
1337 /*printf("%f %f %f %f\n", freq, lpc[0], lpc[1], *toneishness);*/
1338 return freq;
1339}
1340
1341static int run_prefilter(CELTEncoderOpusCustomEncoder *st, celt_sig *in, celt_sig *prefilter_mem, int CC, int N,
1342 int prefilter_tapset, int *pitch, opus_val16 *gain, int *qgain, int enabled, opus_val16 tf_estimate, int nbAvailableBytes, AnalysisInfo *analysis, opus_val16 tone_freq, opus_val32 toneishness)
1343{
1344 int c;
1345 VARDECL(celt_sig, _pre)celt_sig *_pre;
1346 celt_sig *pre[2];
1347 const CELTModeOpusCustomMode *mode;
1348 int pitch_index;
1349 opus_val16 gain1;
1350 opus_val16 pf_threshold;
1351 int pf_on;
1352 int qg;
1353 int overlap;
1354 opus_val32 before[2]={0}, after[2]={0};
1355 int cancel_pitch=0;
1356 SAVE_STACK;
1357
1358 mode = st->mode;
1359 overlap = mode->overlap;
1360 ALLOC(_pre, CC*(N+COMBFILTER_MAXPERIOD), celt_sig)_pre = ((celt_sig*)__builtin_alloca (sizeof(celt_sig)*(CC*(N+
1024))))
;
1361
1362 pre[0] = _pre;
1363 pre[1] = _pre + (N+COMBFILTER_MAXPERIOD1024);
1364
1365
1366 c=0; do {
1367 OPUS_COPY(pre[c], prefilter_mem+c*COMBFILTER_MAXPERIOD, COMBFILTER_MAXPERIOD)(memcpy((pre[c]), (prefilter_mem+c*1024), (1024)*sizeof(*(pre
[c])) + 0*((pre[c])-(prefilter_mem+c*1024)) ))
;
1368 OPUS_COPY(pre[c]+COMBFILTER_MAXPERIOD, in+c*(N+overlap)+overlap, N)(memcpy((pre[c]+1024), (in+c*(N+overlap)+overlap), (N)*sizeof
(*(pre[c]+1024)) + 0*((pre[c]+1024)-(in+c*(N+overlap)+overlap
)) ))
;
1369 } while (++c<CC);
1370
1371 if (enabled)
1372 {
1373 VARDECL(opus_val16, pitch_buf)opus_val16 *pitch_buf;
1374 ALLOC(pitch_buf, (COMBFILTER_MAXPERIOD+N)>>1, opus_val16)pitch_buf = ((opus_val16*)__builtin_alloca (sizeof(opus_val16
)*((1024 +N)>>1)))
;
1375
1376 pitch_downsample(pre, pitch_buf, COMBFILTER_MAXPERIOD1024+N, CC, st->arch);
1377 /* Don't search for the fir last 1.5 octave of the range because
1378 there's too many false-positives due to short-term correlation */
1379 pitch_search(pitch_buf+(COMBFILTER_MAXPERIOD1024>>1), pitch_buf, N,
1380 COMBFILTER_MAXPERIOD1024-3*COMBFILTER_MINPERIOD15, &pitch_index,
1381 st->arch);
1382 pitch_index = COMBFILTER_MAXPERIOD1024-pitch_index;
1383
1384 gain1 = remove_doubling(pitch_buf, COMBFILTER_MAXPERIOD1024, COMBFILTER_MINPERIOD15,
1385 N, &pitch_index, st->prefilter_period, st->prefilter_gain, st->arch);
1386 if (pitch_index > COMBFILTER_MAXPERIOD1024-2)
1387 pitch_index = COMBFILTER_MAXPERIOD1024-2;
1388 gain1 = MULT16_16_Q15(QCONST16(.7f,15),gain1)(((.7f))*(gain1));
1389 /* If we detect that the signal is dominated by a single tone, don't rely on the standard pitch
1390 estimator, as it can become unreliable. */
1391 if (toneishness > QCONST32(.99f, 29)(.99f)) {
1392 /* If the pitch is too high for our post-filter, apply pitch doubling until
1393 we can get something that fits (not ideal, but better than nothing). */
1394 while (tone_freq >= QCONST16(0.39f, 13)(0.39f)) tone_freq/=2;
1395 if (tone_freq > QCONST16(0.006148f, 13)(0.006148f)) {
1396#ifdef FIXED_POINT
1397 pitch_index = IMIN(51472/tone_freq, COMBFILTER_MAXPERIOD-2)((51472/tone_freq) < (1024 -2) ? (51472/tone_freq) : (1024
-2))
;
1398#else
1399 pitch_index = IMIN((int)floor(.5+2.f*M_PI/tone_freq), COMBFILTER_MAXPERIOD-2)(((int)floor(.5+2.f*3.14159265358979323846/tone_freq)) < (
1024 -2) ? ((int)floor(.5+2.f*3.14159265358979323846/tone_freq
)) : (1024 -2))
;
1400#endif
1401 } else {
1402 /* If the pitch is too low, using a very high pitch will actually give us an improvement
1403 due to the DC component of the filter that will be close to our tone. Again, not ideal,
1404 but if we only have a single tone, it's better than nothing. */
1405 pitch_index = COMBFILTER_MINPERIOD15;
1406 }
1407 gain1 = QCONST16(.75f, 15)(.75f);
1408 }
1409 /*printf("%d %d %f %f\n", pitch_change, pitch_index, gain1, st->analysis.tonality);*/
1410 if (st->loss_rate>2)
1411 gain1 = HALF32(gain1)(.5f*(gain1));
1412 if (st->loss_rate>4)
1413 gain1 = HALF32(gain1)(.5f*(gain1));
1414 if (st->loss_rate>8)
1415 gain1 = 0;
1416 } else {
1417 gain1 = 0;
1418 pitch_index = COMBFILTER_MINPERIOD15;
1419 }
1420#ifndef DISABLE_FLOAT_API
1421 if (analysis->valid)
1422 gain1 = (opus_val16)(gain1 * analysis->max_pitch_ratio);
1423#else
1424 (void)analysis;
1425#endif
1426 /* Gain threshold for enabling the prefilter/postfilter */
1427 pf_threshold = QCONST16(.2f,15)(.2f);
1428
1429 /* Adjusting the threshold based on rate and continuity */
1430 if (abs(pitch_index-st->prefilter_period)*10>pitch_index)
1431 {
1432 pf_threshold += QCONST16(.2f,15)(.2f);
1433 /* Completely disable the prefilter on strong transients without continuity. */
1434 if (tf_estimate > QCONST16(.98f, 14)(.98f))
1435 gain1 = 0;
1436 }
1437 if (nbAvailableBytes<25)
1438 pf_threshold += QCONST16(.1f,15)(.1f);
1439 if (nbAvailableBytes<35)
1440 pf_threshold += QCONST16(.1f,15)(.1f);
1441 if (st->prefilter_gain > QCONST16(.4f,15)(.4f))
1442 pf_threshold -= QCONST16(.1f,15)(.1f);
1443 if (st->prefilter_gain > QCONST16(.55f,15)(.55f))
1444 pf_threshold -= QCONST16(.1f,15)(.1f);
1445
1446 /* Hard threshold at 0.2 */
1447 pf_threshold = MAX16(pf_threshold, QCONST16(.2f,15))((pf_threshold) > ((.2f)) ? (pf_threshold) : ((.2f)));
1448 if (gain1<pf_threshold)
1449 {
1450 gain1 = 0;
1451 pf_on = 0;
1452 qg = 0;
1453 } else {
1454 /*This block is not gated by a total bits check only because
1455 of the nbAvailableBytes check above.*/
1456 if (ABS16(gain1-st->prefilter_gain)((float)fabs(gain1-st->prefilter_gain))<QCONST16(.1f,15)(.1f))
1457 gain1=st->prefilter_gain;
1458
1459#ifdef FIXED_POINT
1460 qg = ((gain1+1536)>>10)/3-1;
1461#else
1462 qg = (int)floor(.5f+gain1*32/3)-1;
1463#endif
1464 qg = IMAX(0, IMIN(7, qg))((0) > (((7) < (qg) ? (7) : (qg))) ? (0) : (((7) < (
qg) ? (7) : (qg))))
;
1465 gain1 = QCONST16(0.09375f,15)(0.09375f)*(qg+1);
1466 pf_on = 1;
1467 }
1468 /*printf("%d %f\n", pitch_index, gain1);*/
1469
1470 c=0; do {
1471 int i;
1472 int offset = mode->shortMdctSize-overlap;
1473 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD)((st->prefilter_period) > (15) ? (st->prefilter_period
) : (15))
;
1474 OPUS_COPY(in+c*(N+overlap), st->in_mem+c*(overlap), overlap)(memcpy((in+c*(N+overlap)), (st->in_mem+c*(overlap)), (overlap
)*sizeof(*(in+c*(N+overlap))) + 0*((in+c*(N+overlap))-(st->
in_mem+c*(overlap))) ))
;
1475 for (i=0;i<N;i++) before[c] += ABS32(SHR32(in[c*(N+overlap)+overlap+i], 12))((float)fabs((in[c*(N+overlap)+overlap+i])));
1476 if (offset)
1477 comb_filter(in+c*(N+overlap)+overlap, pre[c]+COMBFILTER_MAXPERIOD1024,
1478 st->prefilter_period, st->prefilter_period, offset, -st->prefilter_gain, -st->prefilter_gain,
1479 st->prefilter_tapset, st->prefilter_tapset, NULL((void*)0), 0, st->arch);
1480
1481 comb_filter(in+c*(N+overlap)+overlap+offset, pre[c]+COMBFILTER_MAXPERIOD1024+offset,
1482 st->prefilter_period, pitch_index, N-offset, -st->prefilter_gain, -gain1,
1483 st->prefilter_tapset, prefilter_tapset, mode->window, overlap, st->arch);
1484 for (i=0;i<N;i++) after[c] += ABS32(SHR32(in[c*(N+overlap)+overlap+i], 12))((float)fabs((in[c*(N+overlap)+overlap+i])));
1485 } while (++c<CC);
1486
1487 if (CC==2) {
1488 opus_val16 thresh[2];
1489 thresh[0] = MULT16_32_Q15(MULT16_16_Q15(QCONST16(.25f, 15), gain1), before[0])(((((.25f))*(gain1)))*(before[0])) + MULT16_32_Q15(QCONST16(.01f,15), before[1])(((.01f))*(before[1]));
1490 thresh[1] = MULT16_32_Q15(MULT16_16_Q15(QCONST16(.25f, 15), gain1), before[1])(((((.25f))*(gain1)))*(before[1])) + MULT16_32_Q15(QCONST16(.01f,15), before[0])(((.01f))*(before[0]));
1491 /* Don't use the filter if one channel gets significantly worse. */
1492 if (after[0]-before[0] > thresh[0] || after[1]-before[1] > thresh[1]) cancel_pitch = 1;
1493 /* Use the filter only if at least one channel gets significantly better. */
1494 if (before[0]-after[0] < thresh[0] && before[1]-after[1] < thresh[1]) cancel_pitch = 1;
1495 } else {
1496 /* Check that the mono channel actually got better. */
1497 if (after[0] > before[0]) cancel_pitch = 1;
1498 }
1499 /* If needed, revert to a gain of zero. */
1500 if (cancel_pitch) {
1501 c=0; do {
1502 int offset = mode->shortMdctSize-overlap;
1503 OPUS_COPY(in+c*(N+overlap)+overlap, pre[c]+COMBFILTER_MAXPERIOD, N)(memcpy((in+c*(N+overlap)+overlap), (pre[c]+1024), (N)*sizeof
(*(in+c*(N+overlap)+overlap)) + 0*((in+c*(N+overlap)+overlap)
-(pre[c]+1024)) ))
;
1504 comb_filter(in+c*(N+overlap)+overlap+offset, pre[c]+COMBFILTER_MAXPERIOD1024+offset,
1505 st->prefilter_period, pitch_index, overlap, -st->prefilter_gain, -0,
1506 st->prefilter_tapset, prefilter_tapset, mode->window, overlap, st->arch);
1507 } while (++c<CC);
1508 gain1 = 0;
1509 pf_on = 0;
1510 qg = 0;
1511 }
1512
1513 c=0; do {
1514 OPUS_COPY(st->in_mem+c*(overlap), in+c*(N+overlap)+N, overlap)(memcpy((st->in_mem+c*(overlap)), (in+c*(N+overlap)+N), (overlap
)*sizeof(*(st->in_mem+c*(overlap))) + 0*((st->in_mem+c*
(overlap))-(in+c*(N+overlap)+N)) ))
;
1515
1516 if (N>COMBFILTER_MAXPERIOD1024)
1517 {
1518 OPUS_COPY(prefilter_mem+c*COMBFILTER_MAXPERIOD, pre[c]+N, COMBFILTER_MAXPERIOD)(memcpy((prefilter_mem+c*1024), (pre[c]+N), (1024)*sizeof(*(prefilter_mem
+c*1024)) + 0*((prefilter_mem+c*1024)-(pre[c]+N)) ))
;
1519 } else {
1520 OPUS_MOVE(prefilter_mem+c*COMBFILTER_MAXPERIOD, prefilter_mem+c*COMBFILTER_MAXPERIOD+N, COMBFILTER_MAXPERIOD-N)(memmove((prefilter_mem+c*1024), (prefilter_mem+c*1024 +N), (
1024 -N)*sizeof(*(prefilter_mem+c*1024)) + 0*((prefilter_mem+
c*1024)-(prefilter_mem+c*1024 +N)) ))
;
1521 OPUS_COPY(prefilter_mem+c*COMBFILTER_MAXPERIOD+COMBFILTER_MAXPERIOD-N, pre[c]+COMBFILTER_MAXPERIOD, N)(memcpy((prefilter_mem+c*1024 +1024 -N), (pre[c]+1024), (N)*sizeof
(*(prefilter_mem+c*1024 +1024 -N)) + 0*((prefilter_mem+c*1024
+1024 -N)-(pre[c]+1024)) ))
;
1522 }
1523 } while (++c<CC);
1524
1525 RESTORE_STACK;
1526 *gain = gain1;
1527 *pitch = pitch_index;
1528 *qgain = qg;
1529 return pf_on;
1530}
1531
1532static int compute_vbr(const CELTModeOpusCustomMode *mode, AnalysisInfo *analysis, opus_int32 base_target,
1533 int LM, opus_int32 bitrate, int lastCodedBands, int C, int intensity,
1534 int constrained_vbr, opus_val16 stereo_saving, int tot_boost,
1535 opus_val16 tf_estimate, int pitch_change, celt_glog maxDepth,
1536 int lfe, int has_surround_mask, celt_glog surround_masking,
1537 celt_glog temporal_vbr)
1538{
1539 /* The target rate in 8th bits per frame */
1540 opus_int32 target;
1541 int coded_bins;
1542 int coded_bands;
1543 opus_val16 tf_calibration;
1544 int nbEBands;
1545 const opus_int16 *eBands;
1546
1547 nbEBands = mode->nbEBands;
1548 eBands = mode->eBands;
1549
1550 coded_bands = lastCodedBands ? lastCodedBands : nbEBands;
1551 coded_bins = eBands[coded_bands]<<LM;
1552 if (C==2)
1553 coded_bins += eBands[IMIN(intensity, coded_bands)((intensity) < (coded_bands) ? (intensity) : (coded_bands)
)
]<<LM;
1554
1555 target = base_target;
1556
1557 /*printf("%f %f %f %f %d %d ", st->analysis.activity, st->analysis.tonality, tf_estimate, st->stereo_saving, tot_boost, coded_bands);*/
1558#ifndef DISABLE_FLOAT_API
1559 if (analysis->valid && analysis->activity<.4)
1560 target -= (opus_int32)((coded_bins<<BITRES3)*(.4f-analysis->activity));
1561#endif
1562 /* Stereo savings */
1563 if (C==2)
1564 {
1565 int coded_stereo_bands;
1566 int coded_stereo_dof;
1567 opus_val16 max_frac;
1568 coded_stereo_bands = IMIN(intensity, coded_bands)((intensity) < (coded_bands) ? (intensity) : (coded_bands)
)
;
1569 coded_stereo_dof = (eBands[coded_stereo_bands]<<LM)-coded_stereo_bands;
1570 /* Maximum fraction of the bits we can save if the signal is mono. */
1571 max_frac = DIV32_16(MULT16_16(QCONST16(0.8f, 15), coded_stereo_dof), coded_bins)(((opus_val32)(((opus_val32)((0.8f))*(opus_val32)(coded_stereo_dof
))))/(opus_val16)(coded_bins))
;
1572 stereo_saving = MIN16(stereo_saving, QCONST16(1.f, 8))((stereo_saving) < ((1.f)) ? (stereo_saving) : ((1.f)));
1573 /*printf("%d %d %d ", coded_stereo_dof, coded_bins, tot_boost);*/
1574 target -= (opus_int32)MIN32(MULT16_32_Q15(max_frac,target),((((max_frac)*(target))) < ((((opus_val32)(stereo_saving-(
0.1f))*(opus_val32)((coded_stereo_dof<<3))))) ? (((max_frac
)*(target))) : ((((opus_val32)(stereo_saving-(0.1f))*(opus_val32
)((coded_stereo_dof<<3))))))
1575 SHR32(MULT16_16(stereo_saving-QCONST16(0.1f,8),(coded_stereo_dof<<BITRES)),8))((((max_frac)*(target))) < ((((opus_val32)(stereo_saving-(
0.1f))*(opus_val32)((coded_stereo_dof<<3))))) ? (((max_frac
)*(target))) : ((((opus_val32)(stereo_saving-(0.1f))*(opus_val32
)((coded_stereo_dof<<3))))))
;
1576 }
1577 /* Boost the rate according to dynalloc (minus the dynalloc average for calibration). */
1578 target += tot_boost-(19<<LM);
1579 /* Apply transient boost, compensating for average boost. */
1580 tf_calibration = QCONST16(0.044f,14)(0.044f);
1581 target += (opus_int32)SHL32(MULT16_32_Q15(tf_estimate-tf_calibration, target),1)(((tf_estimate-tf_calibration)*(target)));
1582
1583#ifndef DISABLE_FLOAT_API
1584 /* Apply tonality boost */
1585 if (analysis->valid && !lfe)
1586 {
1587 opus_int32 tonal_target;
1588 float tonal;
1589
1590 /* Tonality boost (compensating for the average). */
1591 tonal = MAX16(0.f,analysis->tonality-.15f)((0.f) > (analysis->tonality-.15f) ? (0.f) : (analysis->
tonality-.15f))
-0.12f;
1592 tonal_target = target + (opus_int32)((coded_bins<<BITRES3)*1.2f*tonal);
1593 if (pitch_change)
1594 tonal_target += (opus_int32)((coded_bins<<BITRES3)*.8f);
1595 /*printf("%f %f ", analysis->tonality, tonal);*/
1596 target = tonal_target;
1597 }
1598#else
1599 (void)analysis;
1600 (void)pitch_change;
1601#endif
1602
1603 if (has_surround_mask&&!lfe)
1604 {
1605 opus_int32 surround_target = target + (opus_int32)SHR32(MULT16_16(SHR32(surround_masking,DB_SHIFT-10),coded_bins<<BITRES), 10)(((opus_val32)((surround_masking))*(opus_val32)(coded_bins<<
3)))
;
1606 /*printf("%f %d %d %d %d %d %d ", surround_masking, coded_bins, st->end, st->intensity, surround_target, target, st->bitrate);*/
1607 target = IMAX(target/4, surround_target)((target/4) > (surround_target) ? (target/4) : (surround_target
))
;
1608 }
1609
1610 {
1611 opus_int32 floor_depth;
1612 int bins;
1613 bins = eBands[nbEBands-2]<<LM;
1614 /*floor_depth = SHR32(MULT16_16((C*bins<<BITRES),celt_log2(SHL32(MAX16(1,sample_max),13))), DB_SHIFT);*/
1615 floor_depth = (opus_int32)SHR32(MULT16_32_Q15((C*bins<<BITRES),maxDepth), DB_SHIFT-15)((((C*bins<<3))*(maxDepth)));
1616 floor_depth = IMAX(floor_depth, target>>2)((floor_depth) > (target>>2) ? (floor_depth) : (target
>>2))
;
1617 target = IMIN(target, floor_depth)((target) < (floor_depth) ? (target) : (floor_depth));
1618 /*printf("%f %d\n", maxDepth, floor_depth);*/
1619 }
1620
1621 /* Make VBR less aggressive for constrained VBR because we can't keep a higher bitrate
1622 for long. Needs tuning. */
1623 if ((!has_surround_mask||lfe) && constrained_vbr)
1624 {
1625 target = base_target + (opus_int32)MULT16_32_Q15(QCONST16(0.67f, 15), target-base_target)(((0.67f))*(target-base_target));
1626 }
1627
1628 if (!has_surround_mask && tf_estimate < QCONST16(.2f, 14)(.2f))
1629 {
1630 opus_val16 amount;
1631 opus_val16 tvbr_factor;
1632 amount = MULT16_16_Q15(QCONST16(.0000031f, 30), IMAX(0, IMIN(32000, 96000-bitrate)))(((.0000031f))*(((0) > (((32000) < (96000-bitrate) ? (32000
) : (96000-bitrate))) ? (0) : (((32000) < (96000-bitrate) ?
(32000) : (96000-bitrate))))))
;
1633 tvbr_factor = SHR32(MULT16_16(SHR32(temporal_vbr, DB_SHIFT-10), amount), 10)(((opus_val32)((temporal_vbr))*(opus_val32)(amount)));
1634 target += (opus_int32)MULT16_32_Q15(tvbr_factor, target)((tvbr_factor)*(target));
1635 }
1636
1637 /* Don't allow more than doubling the rate */
1638 target = IMIN(2*base_target, target)((2*base_target) < (target) ? (2*base_target) : (target));
1639
1640 return target;
1641}
1642
1643int celt_encode_with_ec(CELTEncoderOpusCustomEncoder * OPUS_RESTRICTrestrict st, const opus_res * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes, ec_enc *enc)
1644{
1645 int i, c, N;
1646 opus_int32 bits;
1647 ec_enc _enc;
1648 VARDECL(celt_sig, in)celt_sig *in;
1649 VARDECL(celt_sig, freq)celt_sig *freq;
1650 VARDECL(celt_norm, X)celt_norm *X;
1651 VARDECL(celt_ener, bandE)celt_ener *bandE;
1652 VARDECL(celt_glog, bandLogE)celt_glog *bandLogE;
1653 VARDECL(celt_glog, bandLogE2)celt_glog *bandLogE2;
1654 VARDECL(int, fine_quant)int *fine_quant;
1655 VARDECL(celt_glog, error)celt_glog *error;
1656 VARDECL(int, pulses)int *pulses;
1657 VARDECL(int, cap)int *cap;
1658 VARDECL(int, offsets)int *offsets;
1659 VARDECL(int, importance)int *importance;
1660 VARDECL(int, spread_weight)int *spread_weight;
1661 VARDECL(int, fine_priority)int *fine_priority;
1662 VARDECL(int, tf_res)int *tf_res;
1663 VARDECL(unsigned char, collapse_masks)unsigned char *collapse_masks;
1664 celt_sig *prefilter_mem;
1665 celt_glog *oldBandE, *oldLogE, *oldLogE2, *energyError;
1666 int shortBlocks=0;
1667 int isTransient=0;
1668 const int CC = st->channels;
1669 const int C = st->stream_channels;
1670 int LM, M;
1671 int tf_select;
1672 int nbFilledBytes, nbAvailableBytes;
1673 int start;
1674 int end;
1675 int effEnd;
1676 int codedBands;
1677 int alloc_trim;
1678 int pitch_index=COMBFILTER_MINPERIOD15;
1679 opus_val16 gain1 = 0;
1680 int dual_stereo=0;
1681 int effectiveBytes;
1682 int dynalloc_logp;
1683 opus_int32 vbr_rate;
1684 opus_int32 total_bits;
1685 opus_int32 total_boost;
1686 opus_int32 balance;
1687 opus_int32 tell;
1688 opus_int32 tell0_frac;
1689 int prefilter_tapset=0;
1690 int pf_on;
1691 int anti_collapse_rsv;
1692 int anti_collapse_on=0;
1693 int silence=0;
1694 int tf_chan = 0;
1695 opus_val16 tf_estimate;
1696 int pitch_change=0;
1697 opus_int32 tot_boost;
1698 opus_val32 sample_max;
1699 celt_glog maxDepth;
1700 const OpusCustomMode *mode;
1701 int nbEBands;
1702 int overlap;
1703 const opus_int16 *eBands;
1704 int secondMdct;
1705 int signalBandwidth;
1706 int transient_got_disabled=0;
1707 celt_glog surround_masking=0;
1708 celt_glog temporal_vbr=0;
1709 celt_glog surround_trim = 0;
1710 opus_int32 equiv_rate;
1711 int hybrid;
1712 int weak_transient = 0;
1713 int enable_tf_analysis;
1714 opus_val16 tone_freq=-1;
1715 opus_val32 toneishness=0;
1716 VARDECL(celt_glog, surround_dynalloc)celt_glog *surround_dynalloc;
1717 ALLOC_STACK;
1718
1719 mode = st->mode;
1720 nbEBands = mode->nbEBands;
1721 overlap = mode->overlap;
1722 eBands = mode->eBands;
1723 start = st->start;
1724 end = st->end;
1725 hybrid = start != 0;
1726 tf_estimate = 0;
1727 if (nbCompressedBytes<2 || pcm==NULL((void*)0))
1728 {
1729 RESTORE_STACK;
1730 return OPUS_BAD_ARG-1;
1731 }
1732
1733 frame_size *= st->upsample;
1734 for (LM=0;LM<=mode->maxLM;LM++)
1735 if (mode->shortMdctSize<<LM==frame_size)
1736 break;
1737 if (LM>mode->maxLM)
1738 {
1739 RESTORE_STACK;
1740 return OPUS_BAD_ARG-1;
1741 }
1742 M=1<<LM;
1743 N = M*mode->shortMdctSize;
1744
1745 prefilter_mem = st->in_mem+CC*(overlap);
1746 oldBandE = (celt_glog*)(st->in_mem+CC*(overlap+COMBFILTER_MAXPERIOD1024));
1747 oldLogE = oldBandE + CC*nbEBands;
1748 oldLogE2 = oldLogE + CC*nbEBands;
1749 energyError = oldLogE2 + CC*nbEBands;
1750
1751 if (enc==NULL((void*)0))
1752 {
1753 tell0_frac=tell=1;
1754 nbFilledBytes=0;
1755 } else {
1756 tell0_frac=ec_tell_frac(enc);
1757 tell=ec_tell(enc);
1758 nbFilledBytes=(tell+4)>>3;
1759 }
1760
1761#ifdef CUSTOM_MODES
1762 if (st->signalling && enc==NULL((void*)0))
1763 {
1764 int tmp = (mode->effEBands-end)>>1;
1765 end = st->end = IMAX(1, mode->effEBands-tmp)((1) > (mode->effEBands-tmp) ? (1) : (mode->effEBands
-tmp))
;
1766 compressed[0] = tmp<<5;
1767 compressed[0] |= LM<<3;
1768 compressed[0] |= (C==2)<<2;
1769 /* Convert "standard mode" to Opus header */
1770 if (mode->Fs==48000 && mode->shortMdctSize==120)
1771 {
1772 int c0 = toOpus(compressed[0]);
1773 if (c0<0)
1774 {
1775 RESTORE_STACK;
1776 return OPUS_BAD_ARG-1;
1777 }
1778 compressed[0] = c0;
1779 }
1780 compressed++;
1781 nbCompressedBytes--;
1782 }
1783#else
1784 celt_assert(st->signalling==0){if (!(st->signalling==0)) {celt_fatal("assertion failed: "
"st->signalling==0", "/root/firefox-clang/media/libopus/celt/celt_encoder.c"
, 1784);}}
;
1785#endif
1786
1787 /* Can't produce more than 1275 output bytes */
1788 nbCompressedBytes = IMIN(nbCompressedBytes,1275)((nbCompressedBytes) < (1275) ? (nbCompressedBytes) : (1275
))
;
1789
1790 if (st->vbr && st->bitrate!=OPUS_BITRATE_MAX-1)
1791 {
1792 opus_int32 den=mode->Fs>>BITRES3;
1793 vbr_rate=(st->bitrate*frame_size+(den>>1))/den;
1794#ifdef CUSTOM_MODES
1795 if (st->signalling)
1796 vbr_rate -= 8<<BITRES3;
1797#endif
1798 effectiveBytes = vbr_rate>>(3+BITRES3);
1799 } else {
1800 opus_int32 tmp;
1801 vbr_rate = 0;
1802 tmp = st->bitrate*frame_size;
1803 if (tell>1)
1804 tmp += tell*mode->Fs;
1805 if (st->bitrate!=OPUS_BITRATE_MAX-1)
1806 {
1807 nbCompressedBytes = IMAX(2, IMIN(nbCompressedBytes,((2) > (((nbCompressedBytes) < ((tmp+4*mode->Fs)/(8*
mode->Fs)-!!st->signalling) ? (nbCompressedBytes) : ((tmp
+4*mode->Fs)/(8*mode->Fs)-!!st->signalling))) ? (2) :
(((nbCompressedBytes) < ((tmp+4*mode->Fs)/(8*mode->
Fs)-!!st->signalling) ? (nbCompressedBytes) : ((tmp+4*mode
->Fs)/(8*mode->Fs)-!!st->signalling))))
1808 (tmp+4*mode->Fs)/(8*mode->Fs)-!!st->signalling))((2) > (((nbCompressedBytes) < ((tmp+4*mode->Fs)/(8*
mode->Fs)-!!st->signalling) ? (nbCompressedBytes) : ((tmp
+4*mode->Fs)/(8*mode->Fs)-!!st->signalling))) ? (2) :
(((nbCompressedBytes) < ((tmp+4*mode->Fs)/(8*mode->
Fs)-!!st->signalling) ? (nbCompressedBytes) : ((tmp+4*mode
->Fs)/(8*mode->Fs)-!!st->signalling))))
;
1809 if (enc != NULL((void*)0))
1810 ec_enc_shrink(enc, nbCompressedBytes);
1811 }
1812 effectiveBytes = nbCompressedBytes - nbFilledBytes;
1813 }
1814 nbAvailableBytes = nbCompressedBytes - nbFilledBytes;
1815 equiv_rate = ((opus_int32)nbCompressedBytes*8*50 << (3-LM)) - (40*C+20)*((400>>LM) - 50);
1816 if (st->bitrate != OPUS_BITRATE_MAX-1)
1817 equiv_rate = IMIN(equiv_rate, st->bitrate - (40*C+20)*((400>>LM) - 50))((equiv_rate) < (st->bitrate - (40*C+20)*((400>>LM
) - 50)) ? (equiv_rate) : (st->bitrate - (40*C+20)*((400>>
LM) - 50)))
;
1818
1819 if (enc==NULL((void*)0))
1820 {
1821 ec_enc_init(&_enc, compressed, nbCompressedBytes);
1822 enc = &_enc;
1823 }
1824
1825 if (vbr_rate>0)
1826 {
1827 /* Computes the max bit-rate allowed in VBR mode to avoid violating the
1828 target rate and buffering.
1829 We must do this up front so that bust-prevention logic triggers
1830 correctly if we don't have enough bits. */
1831 if (st->constrained_vbr)
1832 {
1833 opus_int32 vbr_bound;
1834 opus_int32 max_allowed;
1835 /* We could use any multiple of vbr_rate as bound (depending on the
1836 delay).
1837 This is clamped to ensure we use at least two bytes if the encoder
1838 was entirely empty, but to allow 0 in hybrid mode. */
1839 vbr_bound = vbr_rate;
1840 max_allowed = IMIN(IMAX(tell==1?2:0,((((tell==1?2:0) > ((vbr_rate+vbr_bound-st->vbr_reservoir
)>>(3 +3)) ? (tell==1?2:0) : ((vbr_rate+vbr_bound-st->
vbr_reservoir)>>(3 +3)))) < (nbAvailableBytes) ? (((
tell==1?2:0) > ((vbr_rate+vbr_bound-st->vbr_reservoir)>>
(3 +3)) ? (tell==1?2:0) : ((vbr_rate+vbr_bound-st->vbr_reservoir
)>>(3 +3)))) : (nbAvailableBytes))
1841 (vbr_rate+vbr_bound-st->vbr_reservoir)>>(BITRES+3)),((((tell==1?2:0) > ((vbr_rate+vbr_bound-st->vbr_reservoir
)>>(3 +3)) ? (tell==1?2:0) : ((vbr_rate+vbr_bound-st->
vbr_reservoir)>>(3 +3)))) < (nbAvailableBytes) ? (((
tell==1?2:0) > ((vbr_rate+vbr_bound-st->vbr_reservoir)>>
(3 +3)) ? (tell==1?2:0) : ((vbr_rate+vbr_bound-st->vbr_reservoir
)>>(3 +3)))) : (nbAvailableBytes))
1842 nbAvailableBytes)((((tell==1?2:0) > ((vbr_rate+vbr_bound-st->vbr_reservoir
)>>(3 +3)) ? (tell==1?2:0) : ((vbr_rate+vbr_bound-st->
vbr_reservoir)>>(3 +3)))) < (nbAvailableBytes) ? (((
tell==1?2:0) > ((vbr_rate+vbr_bound-st->vbr_reservoir)>>
(3 +3)) ? (tell==1?2:0) : ((vbr_rate+vbr_bound-st->vbr_reservoir
)>>(3 +3)))) : (nbAvailableBytes))
;
1843 if(max_allowed < nbAvailableBytes)
1844 {
1845 nbCompressedBytes = nbFilledBytes+max_allowed;
1846 nbAvailableBytes = max_allowed;
1847 ec_enc_shrink(enc, nbCompressedBytes);
1848 }
1849 }
1850 }
1851 total_bits = nbCompressedBytes*8;
1852
1853 effEnd = end;
1854 if (effEnd > mode->effEBands)
1855 effEnd = mode->effEBands;
1856
1857 ALLOC(in, CC*(N+overlap), celt_sig)in = ((celt_sig*)__builtin_alloca (sizeof(celt_sig)*(CC*(N+overlap
))))
;
1858
1859 sample_max=MAX32(st->overlap_max, celt_maxabs_res(pcm, C*(N-overlap)/st->upsample))((st->overlap_max) > (celt_maxabs16(pcm, C*(N-overlap)/
st->upsample)) ? (st->overlap_max) : (celt_maxabs16(pcm
, C*(N-overlap)/st->upsample)))
;
1860 st->overlap_max=celt_maxabs_rescelt_maxabs16(pcm+C*(N-overlap)/st->upsample, C*overlap/st->upsample);
1861 sample_max=MAX32(sample_max, st->overlap_max)((sample_max) > (st->overlap_max) ? (sample_max) : (st->
overlap_max))
;
1862#ifdef FIXED_POINT
1863 silence = (sample_max==0);
1864#else
1865 silence = (sample_max <= (opus_val16)1/(1<<st->lsb_depth));
1866#endif
1867#ifdef FUZZING
1868 if ((rand()&0x3F)==0)
1869 silence = 1;
1870#endif
1871 if (tell==1)
1872 ec_enc_bit_logp(enc, silence, 15);
1873 else
1874 silence=0;
1875 if (silence)
1876 {
1877 /*In VBR mode there is no need to send more than the minimum. */
1878 if (vbr_rate>0)
1879 {
1880 effectiveBytes=nbCompressedBytes=IMIN(nbCompressedBytes, nbFilledBytes+2)((nbCompressedBytes) < (nbFilledBytes+2) ? (nbCompressedBytes
) : (nbFilledBytes+2))
;
1881 total_bits=nbCompressedBytes*8;
1882 nbAvailableBytes=2;
1883 ec_enc_shrink(enc, nbCompressedBytes);
1884 }
1885 /* Pretend we've filled all the remaining bits with zeros
1886 (that's what the initialiser did anyway) */
1887 tell = nbCompressedBytes*8;
1888 enc->nbits_total+=tell-ec_tell(enc);
1889 }
1890 c=0; do {
1891 int need_clip=0;
1892#ifndef FIXED_POINT
1893 need_clip = st->clip && sample_max>65536.f;
1894#endif
1895 celt_preemphasis(pcm+c, in+c*(N+overlap)+overlap, N, CC, st->upsample,
1896 mode->preemph, st->preemph_memE+c, need_clip);
1897 OPUS_COPY(in+c*(N+overlap), &prefilter_mem[(1+c)*COMBFILTER_MAXPERIOD-overlap], overlap)(memcpy((in+c*(N+overlap)), (&prefilter_mem[(1+c)*1024 -overlap
]), (overlap)*sizeof(*(in+c*(N+overlap))) + 0*((in+c*(N+overlap
))-(&prefilter_mem[(1+c)*1024 -overlap])) ))
;
1898 } while (++c<CC);
1899
1900
1901 tone_freq = tone_detect(in, CC, N+overlap, &toneishness, mode->Fs);
1902 isTransient = 0;
1903 shortBlocks = 0;
1904 if (st->complexity >= 1 && !st->lfe)
1905 {
1906 /* Reduces the likelihood of energy instability on fricatives at low bitrate
1907 in hybrid mode. It seems like we still want to have real transients on vowels
1908 though (small SILK quantization offset value). */
1909 int allow_weak_transients = hybrid && effectiveBytes<15 && st->silk_info.signalType != 2;
1910 isTransient = transient_analysis(in, N+overlap, CC,
1911 &tf_estimate, &tf_chan, allow_weak_transients, &weak_transient, tone_freq, toneishness);
1912 }
1913 /* Find pitch period and gain */
1914 {
1915 int enabled;
1916 int qg;
1917 enabled = ((st->lfe&&nbAvailableBytes>3) || nbAvailableBytes>12*C) && !hybrid && !silence && tell+16<=total_bits && !st->disable_pf
1918 && st->complexity >= 5;
1919
1920 prefilter_tapset = st->tapset_decision;
1921 pf_on = run_prefilter(st, in, prefilter_mem, CC, N, prefilter_tapset, &pitch_index, &gain1, &qg, enabled, tf_estimate, nbAvailableBytes, &st->analysis, tone_freq, toneishness);
1922 if ((gain1 > QCONST16(.4f,15)(.4f) || st->prefilter_gain > QCONST16(.4f,15)(.4f)) && (!st->analysis.valid || st->analysis.tonality > .3)
1923 && (pitch_index > 1.26*st->prefilter_period || pitch_index < .79*st->prefilter_period))
1924 pitch_change = 1;
1925 if (pf_on==0)
1926 {
1927 if(!hybrid && tell+16<=total_bits)
1928 ec_enc_bit_logp(enc, 0, 1);
1929 } else {
1930 /*This block is not gated by a total bits check only because
1931 of the nbAvailableBytes check above.*/
1932 int octave;
1933 ec_enc_bit_logp(enc, 1, 1);
1934 pitch_index += 1;
1935 octave = EC_ILOG(pitch_index)(((int)sizeof(unsigned)*8)-(__builtin_clz(pitch_index)))-5;
1936 ec_enc_uint(enc, octave, 6);
1937 ec_enc_bits(enc, pitch_index-(16<<octave), 4+octave);
1938 pitch_index -= 1;
1939 ec_enc_bits(enc, qg, 3);
1940 ec_enc_icdf(enc, prefilter_tapset, tapset_icdf, 2);
1941 }
1942 }
1943 if (LM>0 && ec_tell(enc)+3<=total_bits)
1944 {
1945 if (isTransient)
1946 shortBlocks = M;
1947 } else {
1948 isTransient = 0;
1949 transient_got_disabled=1;
1950 }
1951
1952 ALLOC(freq, CC*N, celt_sig)freq = ((celt_sig*)__builtin_alloca (sizeof(celt_sig)*(CC*N))
)
; /**< Interleaved signal MDCTs */
1953 ALLOC(bandE,nbEBands*CC, celt_ener)bandE = ((celt_ener*)__builtin_alloca (sizeof(celt_ener)*(nbEBands
*CC)))
;
1954 ALLOC(bandLogE,nbEBands*CC, celt_glog)bandLogE = ((celt_glog*)__builtin_alloca (sizeof(celt_glog)*(
nbEBands*CC)))
;
1955
1956 secondMdct = shortBlocks && st->complexity>=8;
1957 ALLOC(bandLogE2, C*nbEBands, celt_glog)bandLogE2 = ((celt_glog*)__builtin_alloca (sizeof(celt_glog)*
(C*nbEBands)))
;
1958 if (secondMdct)
1959 {
1960 compute_mdcts(mode, 0, in, freq, C, CC, LM, st->upsample, st->arch);
1961 compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
1962 amp2Log2(mode, effEnd, end, bandE, bandLogE2, C);
1963 for (c=0;c<C;c++)
1964 {
1965 for (i=0;i<end;i++)
1966 bandLogE2[nbEBands*c+i] += HALF32(SHL32(LM, DB_SHIFT))(.5f*((LM)));
1967 }
1968 }
1969
1970 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
1971 /* This should catch any NaN in the CELT input. Since we're not supposed to see any (they're filtered
1972 at the Opus layer), just abort. */
1973 celt_assert(!celt_isnan(freq[0]) && (C==1 || !celt_isnan(freq[N]))){if (!(!((freq[0])!=(freq[0])) && (C==1 || !((freq[N]
)!=(freq[N]))))) {celt_fatal("assertion failed: " "!celt_isnan(freq[0]) && (C==1 || !celt_isnan(freq[N]))"
, "/root/firefox-clang/media/libopus/celt/celt_encoder.c", 1973
);}}
;
1974 if (CC==2&&C==1)
1975 tf_chan = 0;
1976 compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
1977
1978 if (st->lfe)
1979 {
1980 for (i=2;i<end;i++)
1981 {
1982 bandE[i] = IMIN(bandE[i], MULT16_32_Q15(QCONST16(1e-4f,15),bandE[0]))((bandE[i]) < ((((1e-4f))*(bandE[0]))) ? (bandE[i]) : ((((
1e-4f))*(bandE[0]))))
;
1983 bandE[i] = MAX32(bandE[i], EPSILON)((bandE[i]) > (1e-15f) ? (bandE[i]) : (1e-15f));
1984 }
1985 }
1986 amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
1987
1988 ALLOC(surround_dynalloc, C*nbEBands, celt_glog)surround_dynalloc = ((celt_glog*)__builtin_alloca (sizeof(celt_glog
)*(C*nbEBands)))
;
1989 OPUS_CLEAR(surround_dynalloc, end)(memset((surround_dynalloc), 0, (end)*sizeof(*(surround_dynalloc
))))
;
1990 /* This computes how much masking takes place between surround channels */
1991 if (!hybrid&&st->energy_mask&&!st->lfe)
1992 {
1993 int mask_end;
1994 int midband;
1995 int count_dynalloc;
1996 opus_val32 mask_avg=0;
1997 opus_val32 diff=0;
1998 int count=0;
1999 mask_end = IMAX(2,st->lastCodedBands)((2) > (st->lastCodedBands) ? (2) : (st->lastCodedBands
))
;
2000 for (c=0;c<C;c++)
2001 {
2002 for(i=0;i<mask_end;i++)
2003 {
2004 celt_glog mask;
2005 opus_val16 mask16;
2006 mask = MAXG(MING(st->energy_mask[nbEBands*c+i],((((st->energy_mask[nbEBands*c+i]) < ((.25f)) ? (st->
energy_mask[nbEBands*c+i]) : ((.25f)))) > (-(2.0f)) ? (((st
->energy_mask[nbEBands*c+i]) < ((.25f)) ? (st->energy_mask
[nbEBands*c+i]) : ((.25f)))) : (-(2.0f)))
2007 GCONST(.25f)), -GCONST(2.0f))((((st->energy_mask[nbEBands*c+i]) < ((.25f)) ? (st->
energy_mask[nbEBands*c+i]) : ((.25f)))) > (-(2.0f)) ? (((st
->energy_mask[nbEBands*c+i]) < ((.25f)) ? (st->energy_mask
[nbEBands*c+i]) : ((.25f)))) : (-(2.0f)))
;
2008 if (mask > 0)
2009 mask = HALF32(mask)(.5f*(mask));
2010 mask16 = SHR32(mask, DB_SHIFT-10)(mask);
2011 mask_avg += MULT16_16(mask16, eBands[i+1]-eBands[i])((opus_val32)(mask16)*(opus_val32)(eBands[i+1]-eBands[i]));
2012 count += eBands[i+1]-eBands[i];
2013 diff += MULT16_16(mask16, 1+2*i-mask_end)((opus_val32)(mask16)*(opus_val32)(1+2*i-mask_end));
2014 }
2015 }
2016 celt_assert(count>0){if (!(count>0)) {celt_fatal("assertion failed: " "count>0"
, "/root/firefox-clang/media/libopus/celt/celt_encoder.c", 2016
);}}
;
2017 mask_avg = SHL32(DIV32_16(mask_avg,count), DB_SHIFT-10)((((opus_val32)(mask_avg))/(opus_val16)(count)));
2018 mask_avg += GCONST(.2f)(.2f);
2019 diff = SHL32(diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end), DB_SHIFT-10)(diff*6/(C*(mask_end-1)*(mask_end+1)*mask_end));
2020 /* Again, being conservative */
2021 diff = HALF32(diff)(.5f*(diff));
2022 diff = MAX32(MIN32(diff, GCONST(.031f)), -GCONST(.031f))((((diff) < ((.031f)) ? (diff) : ((.031f)))) > (-(.031f
)) ? (((diff) < ((.031f)) ? (diff) : ((.031f)))) : (-(.031f
)))
;
2023 /* Find the band that's in the middle of the coded spectrum */
2024 for (midband=0;eBands[midband+1] < eBands[mask_end]/2;midband++);
2025 count_dynalloc=0;
2026 for(i=0;i<mask_end;i++)
2027 {
2028 opus_val32 lin;
2029 celt_glog unmask;
2030 lin = mask_avg + diff*(i-midband);
2031 if (C==2)
2032 unmask = MAXG(st->energy_mask[i], st->energy_mask[nbEBands+i])((st->energy_mask[i]) > (st->energy_mask[nbEBands+i]
) ? (st->energy_mask[i]) : (st->energy_mask[nbEBands+i]
))
;
2033 else
2034 unmask = st->energy_mask[i];
2035 unmask = MING(unmask, GCONST(.0f))((unmask) < ((.0f)) ? (unmask) : ((.0f)));
2036 unmask -= lin;
2037 if (unmask > GCONST(.25f)(.25f))
2038 {
2039 surround_dynalloc[i] = unmask - GCONST(.25f)(.25f);
2040 count_dynalloc++;
2041 }
2042 }
2043 if (count_dynalloc>=3)
2044 {
2045 /* If we need dynalloc in many bands, it's probably because our
2046 initial masking rate was too low. */
2047 mask_avg += GCONST(.25f)(.25f);
2048 if (mask_avg>0)
2049 {
2050 /* Something went really wrong in the original calculations,
2051 disabling masking. */
2052 mask_avg = 0;
2053 diff = 0;
2054 OPUS_CLEAR(surround_dynalloc, mask_end)(memset((surround_dynalloc), 0, (mask_end)*sizeof(*(surround_dynalloc
))))
;
2055 } else {
2056 for(i=0;i<mask_end;i++)
2057 surround_dynalloc[i] = MAXG(0, surround_dynalloc[i]-GCONST(.25f))((0) > (surround_dynalloc[i]-(.25f)) ? (0) : (surround_dynalloc
[i]-(.25f)))
;
2058 }
2059 }
2060 mask_avg += GCONST(.2f)(.2f);
2061 /* Convert to 1/64th units used for the trim */
2062 surround_trim = 64*diff;
2063 /*printf("%d %d ", mask_avg, surround_trim);*/
2064 surround_masking = mask_avg;
2065 }
2066 /* Temporal VBR (but not for LFE) */
2067 if (!st->lfe)
2068 {
2069 celt_glog follow=-QCONST32(10.0f, DB_SHIFT-5)(10.0f);
2070 opus_val32 frame_avg=0;
2071 celt_glog offset = shortBlocks?HALF32(SHL32(LM, DB_SHIFT-5))(.5f*((LM))):0;
2072 for(i=start;i<end;i++)
2073 {
2074 follow = MAXG(follow-QCONST32(1.0f, DB_SHIFT-5), SHR32(bandLogE[i],5)-offset)((follow-(1.0f)) > ((bandLogE[i])-offset) ? (follow-(1.0f)
) : ((bandLogE[i])-offset))
;
2075 if (C==2)
2076 follow = MAXG(follow, SHR32(bandLogE[i+nbEBands],5)-offset)((follow) > ((bandLogE[i+nbEBands])-offset) ? (follow) : (
(bandLogE[i+nbEBands])-offset))
;
2077 frame_avg += follow;
2078 }
2079 frame_avg /= (end-start);
2080 temporal_vbr = SUB32(SHL32(frame_avg, 5),st->spec_avg)(((frame_avg))-(st->spec_avg));
2081 temporal_vbr = MING(GCONST(3.f), MAXG(-GCONST(1.5f), temporal_vbr))(((3.f)) < (((-(1.5f)) > (temporal_vbr) ? (-(1.5f)) : (
temporal_vbr))) ? ((3.f)) : (((-(1.5f)) > (temporal_vbr) ?
(-(1.5f)) : (temporal_vbr))))
;
2082 st->spec_avg += MULT16_32_Q15(QCONST16(.02f, 15), temporal_vbr)(((.02f))*(temporal_vbr));
2083 }
2084 /*for (i=0;i<21;i++)
2085 printf("%f ", bandLogE[i]);
2086 printf("\n");*/
2087
2088 if (!secondMdct)
2089 {
2090 OPUS_COPY(bandLogE2, bandLogE, C*nbEBands)(memcpy((bandLogE2), (bandLogE), (C*nbEBands)*sizeof(*(bandLogE2
)) + 0*((bandLogE2)-(bandLogE)) ))
;
2091 }
2092
2093 /* Last chance to catch any transient we might have missed in the
2094 time-domain analysis */
2095 if (LM>0 && ec_tell(enc)+3<=total_bits && !isTransient && st->complexity>=5 && !st->lfe && !hybrid)
2096 {
2097 if (patch_transient_decision(bandLogE, oldBandE, nbEBands, start, end, C))
2098 {
2099 isTransient = 1;
2100 shortBlocks = M;
2101 compute_mdcts(mode, shortBlocks, in, freq, C, CC, LM, st->upsample, st->arch);
2102 compute_band_energies(mode, freq, bandE, effEnd, C, LM, st->arch);
2103 amp2Log2(mode, effEnd, end, bandE, bandLogE, C);
2104 /* Compensate for the scaling of short vs long mdcts */
2105 for (c=0;c<C;c++)
2106 {
2107 for (i=0;i<end;i++)
2108 bandLogE2[nbEBands*c+i] += HALF32(SHL32(LM, DB_SHIFT))(.5f*((LM)));
2109 }
2110 tf_estimate = QCONST16(.2f,14)(.2f);
2111 }
2112 }
2113
2114 if (LM>0 && ec_tell(enc)+3<=total_bits)
2115 ec_enc_bit_logp(enc, isTransient, 3);
2116
2117 ALLOC(X, C*N, celt_norm)X = ((celt_norm*)__builtin_alloca (sizeof(celt_norm)*(C*N))); /**< Interleaved normalised MDCTs */
2118
2119 /* Band normalisation */
2120 normalise_bands(mode, freq, X, bandE, effEnd, C, M);
2121
2122 enable_tf_analysis = effectiveBytes>=15*C && !hybrid && st->complexity>=2 && !st->lfe && toneishness < QCONST32(.98f, 29)(.98f);
2123
2124 ALLOC(offsets, nbEBands, int)offsets = ((int*)__builtin_alloca (sizeof(int)*(nbEBands)));
2125 ALLOC(importance, nbEBands, int)importance = ((int*)__builtin_alloca (sizeof(int)*(nbEBands))
)
;
2126 ALLOC(spread_weight, nbEBands, int)spread_weight = ((int*)__builtin_alloca (sizeof(int)*(nbEBands
)))
;
2127
2128 maxDepth = dynalloc_analysis(bandLogE, bandLogE2, oldBandE, nbEBands, start, end, C, offsets,
2129 st->lsb_depth, mode->logN, isTransient, st->vbr, st->constrained_vbr,
2130 eBands, LM, effectiveBytes, &tot_boost, st->lfe, surround_dynalloc, &st->analysis, importance, spread_weight, tone_freq, toneishness);
2131
2132 ALLOC(tf_res, nbEBands, int)tf_res = ((int*)__builtin_alloca (sizeof(int)*(nbEBands)));
2133 /* Disable variable tf resolution for hybrid and at very low bitrate */
2134 if (enable_tf_analysis)
2135 {
2136 int lambda;
2137 lambda = IMAX(80, 20480/effectiveBytes + 2)((80) > (20480/effectiveBytes + 2) ? (80) : (20480/effectiveBytes
+ 2))
;
2138 tf_select = tf_analysis(mode, effEnd, isTransient, tf_res, lambda, X, N, LM, tf_estimate, tf_chan, importance);
2139 for (i=effEnd;i<end;i++)
2140 tf_res[i] = tf_res[effEnd-1];
2141 } else if (hybrid && weak_transient)
2142 {
2143 /* For weak transients, we rely on the fact that improving time resolution using
2144 TF on a long window is imperfect and will not result in an energy collapse at
2145 low bitrate. */
2146 for (i=0;i<end;i++)
2147 tf_res[i] = 1;
2148 tf_select=0;
2149 } else if (hybrid && effectiveBytes<15 && st->silk_info.signalType != 2)
2150 {
2151 /* For low bitrate hybrid, we force temporal resolution to 5 ms rather than 2.5 ms. */
2152 for (i=0;i<end;i++)
2153 tf_res[i] = 0;
2154 tf_select=isTransient;
2155 } else {
2156 for (i=0;i<end;i++)
2157 tf_res[i] = isTransient;
2158 tf_select=0;
2159 }
2160
2161 ALLOC(error, C*nbEBands, celt_glog)error = ((celt_glog*)__builtin_alloca (sizeof(celt_glog)*(C*nbEBands
)))
;
2162 c=0;
2163 do {
2164 for (i=start;i<end;i++)
2165 {
2166 /* When the energy is stable, slightly bias energy quantization towards
2167 the previous error to make the gain more stable (a constant offset is
2168 better than fluctuations). */
2169 if (ABS32(SUB32(bandLogE[i+c*nbEBands], oldBandE[i+c*nbEBands]))((float)fabs(((bandLogE[i+c*nbEBands])-(oldBandE[i+c*nbEBands
]))))
< GCONST(2.f)(2.f))
2170 {
2171 bandLogE[i+c*nbEBands] -= MULT16_32_Q15(QCONST16(0.25f, 15), energyError[i+c*nbEBands])(((0.25f))*(energyError[i+c*nbEBands]));
2172 }
2173 }
2174 } while (++c < C);
2175 quant_coarse_energy(mode, start, end, effEnd, bandLogE,
2176 oldBandE, total_bits, error, enc,
2177 C, LM, nbAvailableBytes, st->force_intra,
2178 &st->delayedIntra, st->complexity >= 4, st->loss_rate, st->lfe);
2179
2180 tf_encode(start, end, isTransient, tf_res, LM, tf_select, enc);
2181
2182 if (ec_tell(enc)+4<=total_bits)
2183 {
2184 if (st->lfe)
2185 {
2186 st->tapset_decision = 0;
2187 st->spread_decision = SPREAD_NORMAL(2);
2188 } else if (hybrid)
2189 {
2190 if (st->complexity == 0)
2191 st->spread_decision = SPREAD_NONE(0);
2192 else if (isTransient)
2193 st->spread_decision = SPREAD_NORMAL(2);
2194 else
2195 st->spread_decision = SPREAD_AGGRESSIVE(3);
2196 } else if (shortBlocks || st->complexity < 3 || nbAvailableBytes < 10*C)
2197 {
2198 if (st->complexity == 0)
2199 st->spread_decision = SPREAD_NONE(0);
2200 else
2201 st->spread_decision = SPREAD_NORMAL(2);
2202 } else {
2203 /* Disable new spreading+tapset estimator until we can show it works
2204 better than the old one. So far it seems like spreading_decision()
2205 works best. */
2206#if 0
2207 if (st->analysis.valid)
2208 {
2209 static const opus_val16 spread_thresholds[3] = {-QCONST16(.6f, 15)(.6f), -QCONST16(.2f, 15)(.2f), -QCONST16(.07f, 15)(.07f)};
2210 static const opus_val16 spread_histeresis[3] = {QCONST16(.15f, 15)(.15f), QCONST16(.07f, 15)(.07f), QCONST16(.02f, 15)(.02f)};
2211 static const opus_val16 tapset_thresholds[2] = {QCONST16(.0f, 15)(.0f), QCONST16(.15f, 15)(.15f)};
2212 static const opus_val16 tapset_histeresis[2] = {QCONST16(.1f, 15)(.1f), QCONST16(.05f, 15)(.05f)};
2213 st->spread_decision = hysteresis_decision(-st->analysis.tonality, spread_thresholds, spread_histeresis, 3, st->spread_decision);
2214 st->tapset_decision = hysteresis_decision(st->analysis.tonality_slope, tapset_thresholds, tapset_histeresis, 2, st->tapset_decision);
2215 } else
2216#endif
2217 {
2218 st->spread_decision = spreading_decision(mode, X,
2219 &st->tonal_average, st->spread_decision, &st->hf_average,
2220 &st->tapset_decision, pf_on&&!shortBlocks, effEnd, C, M, spread_weight);
2221 }
2222 /*printf("%d %d\n", st->tapset_decision, st->spread_decision);*/
2223 /*printf("%f %d %f %d\n\n", st->analysis.tonality, st->spread_decision, st->analysis.tonality_slope, st->tapset_decision);*/
2224 }
2225 ec_enc_icdf(enc, st->spread_decision, spread_icdf, 5);
2226 } else {
2227 st->spread_decision = SPREAD_NORMAL(2);
2228 }
2229
2230 /* For LFE, everything interesting is in the first band */
2231 if (st->lfe)
2232 offsets[0] = IMIN(8, effectiveBytes/3)((8) < (effectiveBytes/3) ? (8) : (effectiveBytes/3));
2233 ALLOC(cap, nbEBands, int)cap = ((int*)__builtin_alloca (sizeof(int)*(nbEBands)));
2234 init_caps(mode,cap,LM,C);
2235
2236 dynalloc_logp = 6;
2237 total_bits<<=BITRES3;
2238 total_boost = 0;
2239 tell = ec_tell_frac(enc);
2240 for (i=start;i<end;i++)
2241 {
2242 int width, quanta;
2243 int dynalloc_loop_logp;
2244 int boost;
2245 int j;
2246 width = C*(eBands[i+1]-eBands[i])<<LM;
2247 /* quanta is 6 bits, but no more than 1 bit/sample
2248 and no less than 1/8 bit/sample */
2249 quanta = IMIN(width<<BITRES, IMAX(6<<BITRES, width))((width<<3) < (((6<<3) > (width) ? (6<<
3) : (width))) ? (width<<3) : (((6<<3) > (width
) ? (6<<3) : (width))))
;
2250 dynalloc_loop_logp = dynalloc_logp;
2251 boost = 0;
2252 for (j = 0; tell+(dynalloc_loop_logp<<BITRES3) < total_bits-total_boost
2253 && boost < cap[i]; j++)
2254 {
2255 int flag;
2256 flag = j<offsets[i];
2257 ec_enc_bit_logp(enc, flag, dynalloc_loop_logp);
2258 tell = ec_tell_frac(enc);
2259 if (!flag)
2260 break;
2261 boost += quanta;
2262 total_boost += quanta;
2263 dynalloc_loop_logp = 1;
2264 }
2265 /* Making dynalloc more likely */
2266 if (j)
2267 dynalloc_logp = IMAX(2, dynalloc_logp-1)((2) > (dynalloc_logp-1) ? (2) : (dynalloc_logp-1));
2268 offsets[i] = boost;
2269 }
2270
2271 if (C==2)
2272 {
2273 static const opus_val16 intensity_thresholds[21]=
2274 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 off*/
2275 { 1, 2, 3, 4, 5, 6, 7, 8,16,24,36,44,50,56,62,67,72,79,88,106,134};
2276 static const opus_val16 intensity_histeresis[21]=
2277 { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 6, 8, 8};
2278
2279 /* Always use MS for 2.5 ms frames until we can do a better analysis */
2280 if (LM!=0)
2281 dual_stereo = stereo_analysis(mode, X, LM, N);
2282
2283 st->intensity = hysteresis_decision((opus_val16)(equiv_rate/1000),
2284 intensity_thresholds, intensity_histeresis, 21, st->intensity);
2285 st->intensity = IMIN(end,IMAX(start, st->intensity))((end) < (((start) > (st->intensity) ? (start) : (st
->intensity))) ? (end) : (((start) > (st->intensity)
? (start) : (st->intensity))))
;
2286 }
2287
2288 alloc_trim = 5;
2289 if (tell+(6<<BITRES3) <= total_bits - total_boost)
2290 {
2291 if (start > 0 || st->lfe)
2292 {
2293 st->stereo_saving = 0;
2294 alloc_trim = 5;
2295 } else {
2296 alloc_trim = alloc_trim_analysis(mode, X, bandLogE,
2297 end, LM, C, N, &st->analysis, &st->stereo_saving, tf_estimate,
2298 st->intensity, surround_trim, equiv_rate, st->arch);
2299 }
2300 ec_enc_icdf(enc, alloc_trim, trim_icdf, 7);
2301 tell = ec_tell_frac(enc);
2302 }
2303
2304 /* Variable bitrate */
2305 if (vbr_rate>0)
2306 {
2307 opus_val16 alpha;
2308 opus_int32 delta;
2309 /* The target rate in 8th bits per frame */
2310 opus_int32 target, base_target;
2311 opus_int32 min_allowed;
2312 int lm_diff = mode->maxLM - LM;
2313
2314 /* Don't attempt to use more than 510 kb/s, even for frames smaller than 20 ms.
2315 The CELT allocator will just not be able to use more than that anyway. */
2316 nbCompressedBytes = IMIN(nbCompressedBytes,1275>>(3-LM))((nbCompressedBytes) < (1275>>(3-LM)) ? (nbCompressedBytes
) : (1275>>(3-LM)))
;
2317 if (!hybrid)
2318 {
2319 base_target = vbr_rate - ((40*C+20)<<BITRES3);
2320 } else {
2321 base_target = IMAX(0, vbr_rate - ((9*C+4)<<BITRES))((0) > (vbr_rate - ((9*C+4)<<3)) ? (0) : (vbr_rate -
((9*C+4)<<3)))
;
2322 }
2323
2324 if (st->constrained_vbr)
2325 base_target += (st->vbr_offset>>lm_diff);
2326
2327 if (!hybrid)
2328 {
2329 target = compute_vbr(mode, &st->analysis, base_target, LM, equiv_rate,
2330 st->lastCodedBands, C, st->intensity, st->constrained_vbr,
2331 st->stereo_saving, tot_boost, tf_estimate, pitch_change, maxDepth,
2332 st->lfe, st->energy_mask!=NULL((void*)0), surround_masking,
2333 temporal_vbr);
2334 } else {
2335 target = base_target;
2336 /* Tonal frames (offset<100) need more bits than noisy (offset>100) ones. */
2337 if (st->silk_info.offset < 100) target += 12 << BITRES3 >> (3-LM);
2338 if (st->silk_info.offset > 100) target -= 18 << BITRES3 >> (3-LM);
2339 /* Boosting bitrate on transients and vowels with significant temporal
2340 spikes. */
2341 target += (opus_int32)MULT16_16_Q14(tf_estimate-QCONST16(.25f,14), (50<<BITRES))((tf_estimate-(.25f))*((50<<3)));
2342 /* If we have a strong transient, let's make sure it has enough bits to code
2343 the first two bands, so that it can use folding rather than noise. */
2344 if (tf_estimate > QCONST16(.7f,14)(.7f))
2345 target = IMAX(target, 50<<BITRES)((target) > (50<<3) ? (target) : (50<<3));
2346 }
2347 /* The current offset is removed from the target and the space used
2348 so far is added*/
2349 target=target+tell;
2350 /* In VBR mode the frame size must not be reduced so much that it would
2351 result in the encoder running out of bits.
2352 The margin of 2 bytes ensures that none of the bust-prevention logic
2353 in the decoder will have triggered so far. */
2354 min_allowed = ((tell+total_boost+(1<<(BITRES3+3))-1)>>(BITRES3+3)) + 2;
2355 /* Take into account the 37 bits we need to have left in the packet to
2356 signal a redundant frame in hybrid mode. Creating a shorter packet would
2357 create an entropy coder desync. */
2358 if (hybrid)
2359 min_allowed = IMAX(min_allowed, (tell0_frac+(37<<BITRES)+total_boost+(1<<(BITRES+3))-1)>>(BITRES+3))((min_allowed) > ((tell0_frac+(37<<3)+total_boost+(1
<<(3 +3))-1)>>(3 +3)) ? (min_allowed) : ((tell0_frac
+(37<<3)+total_boost+(1<<(3 +3))-1)>>(3 +3)
))
;
2360
2361 nbAvailableBytes = (target+(1<<(BITRES3+2)))>>(BITRES3+3);
2362 nbAvailableBytes = IMAX(min_allowed,nbAvailableBytes)((min_allowed) > (nbAvailableBytes) ? (min_allowed) : (nbAvailableBytes
))
;
2363 nbAvailableBytes = IMIN(nbCompressedBytes,nbAvailableBytes)((nbCompressedBytes) < (nbAvailableBytes) ? (nbCompressedBytes
) : (nbAvailableBytes))
;
2364
2365 /* By how much did we "miss" the target on that frame */
2366 delta = target - vbr_rate;
2367
2368 target=nbAvailableBytes<<(BITRES3+3);
2369
2370 /*If the frame is silent we don't adjust our drift, otherwise
2371 the encoder will shoot to very high rates after hitting a
2372 span of silence, but we do allow the bitres to refill.
2373 This means that we'll undershoot our target in CVBR/VBR modes
2374 on files with lots of silence. */
2375 if(silence)
2376 {
2377 nbAvailableBytes = 2;
2378 target = 2*8<<BITRES3;
2379 delta = 0;
2380 }
2381
2382 if (st->vbr_count < 970)
2383 {
2384 st->vbr_count++;
2385 alpha = celt_rcp(SHL32(EXTEND32(st->vbr_count+20),16))(1.f/(((st->vbr_count+20))));
2386 } else
2387 alpha = QCONST16(.001f,15)(.001f);
2388 /* How many bits have we used in excess of what we're allowed */
2389 if (st->constrained_vbr)
2390 st->vbr_reservoir += target - vbr_rate;
2391 /*printf ("%d\n", st->vbr_reservoir);*/
2392
2393 /* Compute the offset we need to apply in order to reach the target */
2394 if (st->constrained_vbr)
2395 {
2396 st->vbr_drift += (opus_int32)MULT16_32_Q15(alpha,(delta*(1<<lm_diff))-st->vbr_offset-st->vbr_drift)((alpha)*((delta*(1<<lm_diff))-st->vbr_offset-st->
vbr_drift))
;
2397 st->vbr_offset = -st->vbr_drift;
2398 }
2399 /*printf ("%d\n", st->vbr_drift);*/
2400
2401 if (st->constrained_vbr && st->vbr_reservoir < 0)
2402 {
2403 /* We're under the min value -- increase rate */
2404 int adjust = (-st->vbr_reservoir)/(8<<BITRES3);
2405 /* Unless we're just coding silence */
2406 nbAvailableBytes += silence?0:adjust;
2407 st->vbr_reservoir = 0;
2408 /*printf ("+%d\n", adjust);*/
2409 }
2410 nbCompressedBytes = IMIN(nbCompressedBytes,nbAvailableBytes)((nbCompressedBytes) < (nbAvailableBytes) ? (nbCompressedBytes
) : (nbAvailableBytes))
;
2411 /*printf("%d\n", nbCompressedBytes*50*8);*/
2412 /* This moves the raw bits to take into account the new compressed size */
2413 ec_enc_shrink(enc, nbCompressedBytes);
2414 }
2415
2416 /* Bit allocation */
2417 ALLOC(fine_quant, nbEBands, int)fine_quant = ((int*)__builtin_alloca (sizeof(int)*(nbEBands))
)
;
2418 ALLOC(pulses, nbEBands, int)pulses = ((int*)__builtin_alloca (sizeof(int)*(nbEBands)));
2419 ALLOC(fine_priority, nbEBands, int)fine_priority = ((int*)__builtin_alloca (sizeof(int)*(nbEBands
)))
;
2420
2421 /* bits = packet size - where we are - safety*/
2422 bits = (((opus_int32)nbCompressedBytes*8)<<BITRES3) - (opus_int32)ec_tell_frac(enc) - 1;
2423 anti_collapse_rsv = isTransient&&LM>=2&&bits>=((LM+2)<<BITRES3) ? (1<<BITRES3) : 0;
2424 bits -= anti_collapse_rsv;
2425 signalBandwidth = end-1;
2426#ifndef DISABLE_FLOAT_API
2427 if (st->analysis.valid)
2428 {
2429 int min_bandwidth;
2430 if (equiv_rate < (opus_int32)32000*C)
2431 min_bandwidth = 13;
2432 else if (equiv_rate < (opus_int32)48000*C)
2433 min_bandwidth = 16;
2434 else if (equiv_rate < (opus_int32)60000*C)
2435 min_bandwidth = 18;
2436 else if (equiv_rate < (opus_int32)80000*C)
2437 min_bandwidth = 19;
2438 else
2439 min_bandwidth = 20;
2440 signalBandwidth = IMAX(st->analysis.bandwidth, min_bandwidth)((st->analysis.bandwidth) > (min_bandwidth) ? (st->analysis
.bandwidth) : (min_bandwidth))
;
2441 }
2442#endif
2443 if (st->lfe)
2444 signalBandwidth = 1;
2445 codedBands = clt_compute_allocation(mode, start, end, offsets, cap,
2446 alloc_trim, &st->intensity, &dual_stereo, bits, &balance, pulses,
2447 fine_quant, fine_priority, C, LM, enc, 1, st->lastCodedBands, signalBandwidth);
2448 if (st->lastCodedBands)
2449 st->lastCodedBands = IMIN(st->lastCodedBands+1,IMAX(st->lastCodedBands-1,codedBands))((st->lastCodedBands+1) < (((st->lastCodedBands-1) >
(codedBands) ? (st->lastCodedBands-1) : (codedBands))) ? (
st->lastCodedBands+1) : (((st->lastCodedBands-1) > (
codedBands) ? (st->lastCodedBands-1) : (codedBands))))
;
2450 else
2451 st->lastCodedBands = codedBands;
2452
2453 quant_fine_energy(mode, start, end, oldBandE, error, fine_quant, enc, C);
2454
2455 /* Residual quantisation */
2456 ALLOC(collapse_masks, C*nbEBands, unsigned char)collapse_masks = ((unsigned char*)__builtin_alloca (sizeof(unsigned
char)*(C*nbEBands)))
;
2457 quant_all_bands(1, mode, start, end, X, C==2 ? X+N : NULL((void*)0), collapse_masks,
2458 bandE, pulses, shortBlocks, st->spread_decision,
2459 dual_stereo, st->intensity, tf_res, nbCompressedBytes*(8<<BITRES3)-anti_collapse_rsv,
2460 balance, enc, LM, codedBands, &st->rng, st->complexity, st->arch, st->disable_inv);
2461
2462 if (anti_collapse_rsv > 0)
2463 {
2464 anti_collapse_on = st->consec_transient<2;
2465#ifdef FUZZING
2466 anti_collapse_on = rand()&0x1;
2467#endif
2468 ec_enc_bits(enc, anti_collapse_on, 1);
2469 }
2470 quant_energy_finalise(mode, start, end, oldBandE, error, fine_quant, fine_priority, nbCompressedBytes*8-ec_tell(enc), enc, C);
2471 OPUS_CLEAR(energyError, nbEBands*CC)(memset((energyError), 0, (nbEBands*CC)*sizeof(*(energyError)
)))
;
2472 c=0;
2473 do {
2474 for (i=start;i<end;i++)
2475 {
2476 energyError[i+c*nbEBands] = MAXG(-GCONST(0.5f), MING(GCONST(0.5f), error[i+c*nbEBands]))((-(0.5f)) > ((((0.5f)) < (error[i+c*nbEBands]) ? ((0.5f
)) : (error[i+c*nbEBands]))) ? (-(0.5f)) : ((((0.5f)) < (error
[i+c*nbEBands]) ? ((0.5f)) : (error[i+c*nbEBands]))))
;
2477 }
2478 } while (++c < C);
2479
2480 if (silence)
2481 {
2482 for (i=0;i<C*nbEBands;i++)
2483 oldBandE[i] = -GCONST(28.f)(28.f);
2484 }
2485
2486#ifdef RESYNTH
2487 /* Re-synthesis of the coded audio if required */
2488 {
2489 celt_sig *out_mem[2];
2490
2491 if (anti_collapse_on)
2492 {
2493 anti_collapse(mode, X, collapse_masks, LM, C, N,
2494 start, end, oldBandE, oldLogE, oldLogE2, pulses, st->rng, 1, st->arch);
2495 }
2496
2497 c=0; do {
2498 OPUS_MOVE(st->syn_mem[c], st->syn_mem[c]+N, 2*MAX_PERIOD-N+overlap/2)(memmove((st->syn_mem[c]), (st->syn_mem[c]+N), (2*1024 -
N+overlap/2)*sizeof(*(st->syn_mem[c])) + 0*((st->syn_mem
[c])-(st->syn_mem[c]+N)) ))
;
2499 } while (++c<CC);
2500
2501 c=0; do {
2502 out_mem[c] = st->syn_mem[c]+2*MAX_PERIOD1024-N;
2503 } while (++c<CC);
2504
2505 celt_synthesis(mode, X, out_mem, oldBandE, start, effEnd,
2506 C, CC, isTransient, LM, st->upsample, silence, st->arch);
2507
2508 c=0; do {
2509 st->prefilter_period=IMAX(st->prefilter_period, COMBFILTER_MINPERIOD)((st->prefilter_period) > (15) ? (st->prefilter_period
) : (15))
;
2510 st->prefilter_period_old=IMAX(st->prefilter_period_old, COMBFILTER_MINPERIOD)((st->prefilter_period_old) > (15) ? (st->prefilter_period_old
) : (15))
;
2511 comb_filter(out_mem[c], out_mem[c], st->prefilter_period_old, st->prefilter_period, mode->shortMdctSize,
2512 st->prefilter_gain_old, st->prefilter_gain, st->prefilter_tapset_old, st->prefilter_tapset,
2513 mode->window, overlap, st->arch);
2514 if (LM!=0)
2515 comb_filter(out_mem[c]+mode->shortMdctSize, out_mem[c]+mode->shortMdctSize, st->prefilter_period, pitch_index, N-mode->shortMdctSize,
2516 st->prefilter_gain, gain1, st->prefilter_tapset, prefilter_tapset,
2517 mode->window, overlap, st->arch);
2518 } while (++c<CC);
2519
2520 /* We reuse freq[] as scratch space for the de-emphasis */
2521 deemphasis(out_mem, (opus_res*)pcm, N, CC, st->upsample, mode->preemph, st->preemph_memD, 0);
2522 st->prefilter_period_old = st->prefilter_period;
2523 st->prefilter_gain_old = st->prefilter_gain;
2524 st->prefilter_tapset_old = st->prefilter_tapset;
2525 }
2526#endif
2527
2528 st->prefilter_period = pitch_index;
2529 st->prefilter_gain = gain1;
2530 st->prefilter_tapset = prefilter_tapset;
2531#ifdef RESYNTH
2532 if (LM!=0)
2533 {
2534 st->prefilter_period_old = st->prefilter_period;
2535 st->prefilter_gain_old = st->prefilter_gain;
2536 st->prefilter_tapset_old = st->prefilter_tapset;
2537 }
2538#endif
2539
2540 if (CC==2&&C==1) {
2541 OPUS_COPY(&oldBandE[nbEBands], oldBandE, nbEBands)(memcpy((&oldBandE[nbEBands]), (oldBandE), (nbEBands)*sizeof
(*(&oldBandE[nbEBands])) + 0*((&oldBandE[nbEBands])-(
oldBandE)) ))
;
2542 }
2543
2544 if (!isTransient)
2545 {
2546 OPUS_COPY(oldLogE2, oldLogE, CC*nbEBands)(memcpy((oldLogE2), (oldLogE), (CC*nbEBands)*sizeof(*(oldLogE2
)) + 0*((oldLogE2)-(oldLogE)) ))
;
2547 OPUS_COPY(oldLogE, oldBandE, CC*nbEBands)(memcpy((oldLogE), (oldBandE), (CC*nbEBands)*sizeof(*(oldLogE
)) + 0*((oldLogE)-(oldBandE)) ))
;
2548 } else {
2549 for (i=0;i<CC*nbEBands;i++)
2550 oldLogE[i] = MING(oldLogE[i], oldBandE[i])((oldLogE[i]) < (oldBandE[i]) ? (oldLogE[i]) : (oldBandE[i
]))
;
2551 }
2552 /* In case start or end were to change */
2553 c=0; do
2554 {
2555 for (i=0;i<start;i++)
2556 {
2557 oldBandE[c*nbEBands+i]=0;
2558 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f)(28.f);
2559 }
2560 for (i=end;i<nbEBands;i++)
2561 {
2562 oldBandE[c*nbEBands+i]=0;
2563 oldLogE[c*nbEBands+i]=oldLogE2[c*nbEBands+i]=-GCONST(28.f)(28.f);
2564 }
2565 } while (++c<CC);
2566
2567 if (isTransient || transient_got_disabled)
2568 st->consec_transient++;
2569 else
2570 st->consec_transient=0;
2571 st->rng = enc->rng;
2572
2573 /* If there's any room left (can only happen for very high rates),
2574 it's already filled with zeros */
2575 ec_enc_done(enc);
2576
2577#ifdef CUSTOM_MODES
2578 if (st->signalling)
2579 nbCompressedBytes++;
2580#endif
2581
2582 RESTORE_STACK;
2583 if (ec_get_error(enc))
2584 return OPUS_INTERNAL_ERROR-3;
2585 else
2586 return nbCompressedBytes;
2587}
2588
2589
2590#ifdef CUSTOM_MODES
2591
2592#if defined(FIXED_POINT) && !defined(ENABLE_RES24)
2593int opus_custom_encode(CELTEncoderOpusCustomEncoder * OPUS_RESTRICTrestrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2594{
2595 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL((void*)0));
2596}
2597#else
2598int opus_custom_encode(CELTEncoderOpusCustomEncoder * OPUS_RESTRICTrestrict st, const opus_int16 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2599{
2600 int j, ret, C, N;
2601 VARDECL(opus_res, in)opus_res *in;
2602 ALLOC_STACK;
2603
2604 if (pcm==NULL((void*)0))
2605 return OPUS_BAD_ARG-1;
2606
2607 C = st->channels;
2608 N = frame_size;
2609 ALLOC(in, C*N, opus_res)in = ((opus_res*)__builtin_alloca (sizeof(opus_res)*(C*N)));
2610
2611 for (j=0;j<C*N;j++)
2612 in[j] = INT16TORES(pcm[j])((pcm[j])*(1/32768.f));
2613
2614 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL((void*)0));
2615#ifdef RESYNTH
2616 for (j=0;j<C*N;j++)
2617 ((opus_int16*)pcm)[j]=RES2INT16(in[j])FLOAT2INT16(in[j]);
2618#endif
2619 RESTORE_STACK;
2620 return ret;
2621}
2622#endif
2623
2624
2625#if defined(FIXED_POINT) && defined(ENABLE_RES24)
2626int opus_custom_encode24(CELTEncoderOpusCustomEncoder * OPUS_RESTRICTrestrict st, const opus_int32 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2627{
2628 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL((void*)0));
2629}
2630#else
2631int opus_custom_encode24(CELTEncoderOpusCustomEncoder * OPUS_RESTRICTrestrict st, const opus_int32 * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2632{
2633 int j, ret, C, N;
2634 VARDECL(opus_res, in)opus_res *in;
2635 ALLOC_STACK;
2636
2637 if (pcm==NULL((void*)0))
2638 return OPUS_BAD_ARG-1;
2639
2640 C = st->channels;
2641 N = frame_size;
2642 ALLOC(in, C*N, opus_res)in = ((opus_res*)__builtin_alloca (sizeof(opus_res)*(C*N)));
2643
2644 for (j=0;j<C*N;j++)
2645 in[j] = INT24TORES(pcm[j])((1.f/32768.f/256.)*(pcm[j]));
2646
2647 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL((void*)0));
2648#ifdef RESYNTH
2649 for (j=0;j<C*N;j++)
2650 ((opus_int32*)pcm)[j]=RES2INT24(in[j])float2int(32768.f*256.f*(in[j]));
2651#endif
2652 RESTORE_STACK;
2653 return ret;
2654}
2655#endif
2656
2657
2658#ifndef DISABLE_FLOAT_API
2659
2660# if !defined(FIXED_POINT)
2661int opus_custom_encode_float(CELTEncoderOpusCustomEncoder * OPUS_RESTRICTrestrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2662{
2663 return celt_encode_with_ec(st, pcm, frame_size, compressed, nbCompressedBytes, NULL((void*)0));
2664}
2665# else
2666int opus_custom_encode_float(CELTEncoderOpusCustomEncoder * OPUS_RESTRICTrestrict st, const float * pcm, int frame_size, unsigned char *compressed, int nbCompressedBytes)
2667{
2668 int j, ret, C, N;
2669 VARDECL(opus_res, in)opus_res *in;
2670 ALLOC_STACK;
2671
2672 if (pcm==NULL((void*)0))
2673 return OPUS_BAD_ARG-1;
2674
2675 C = st->channels;
2676 N = frame_size;
2677 ALLOC(in, C*N, opus_res)in = ((opus_res*)__builtin_alloca (sizeof(opus_res)*(C*N)));
2678
2679 for (j=0;j<C*N;j++)
2680 in[j] = FLOAT2RES(pcm[j])(pcm[j]);
2681
2682 ret=celt_encode_with_ec(st,in,frame_size,compressed,nbCompressedBytes, NULL((void*)0));
2683#ifdef RESYNTH
2684 for (j=0;j<C*N;j++)
2685 ((float*)pcm)[j]=RES2FLOAT(in[j])(in[j]);
2686#endif
2687 RESTORE_STACK;
2688 return ret;
2689}
2690# endif
2691
2692#endif
2693
2694#endif /* CUSTOM_MODES */
2695
2696int opus_custom_encoder_ctl(CELTEncoderOpusCustomEncoder * OPUS_RESTRICTrestrict st, int request, ...)
2697{
2698 va_list ap;
2699
2700 va_start(ap, request)__builtin_va_start(ap, request);
2701 switch (request)
2702 {
2703 case OPUS_SET_COMPLEXITY_REQUEST4010:
2704 {
2705 int value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2706 if (value<0 || value>10)
2707 goto bad_arg;
2708 st->complexity = value;
2709 }
2710 break;
2711 case CELT_SET_START_BAND_REQUEST10010:
2712 {
2713 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2714 if (value<0 || value>=st->mode->nbEBands)
2715 goto bad_arg;
2716 st->start = value;
2717 }
2718 break;
2719 case CELT_SET_END_BAND_REQUEST10012:
2720 {
2721 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2722 if (value<1 || value>st->mode->nbEBands)
2723 goto bad_arg;
2724 st->end = value;
2725 }
2726 break;
2727 case CELT_SET_PREDICTION_REQUEST10002:
2728 {
2729 int value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2730 if (value<0 || value>2)
2731 goto bad_arg;
2732 st->disable_pf = value<=1;
2733 st->force_intra = value==0;
2734 }
2735 break;
2736 case OPUS_SET_PACKET_LOSS_PERC_REQUEST4014:
2737 {
2738 int value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2739 if (value<0 || value>100)
2740 goto bad_arg;
2741 st->loss_rate = value;
2742 }
2743 break;
2744 case OPUS_SET_VBR_CONSTRAINT_REQUEST4020:
2745 {
2746 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2747 st->constrained_vbr = value;
2748 }
2749 break;
2750 case OPUS_SET_VBR_REQUEST4006:
2751 {
2752 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2753 st->vbr = value;
2754 }
2755 break;
2756 case OPUS_SET_BITRATE_REQUEST4002:
2757 {
2758 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2759 if (value<=500 && value!=OPUS_BITRATE_MAX-1)
2760 goto bad_arg;
2761 value = IMIN(value, 260000*st->channels)((value) < (260000*st->channels) ? (value) : (260000*st
->channels))
;
2762 st->bitrate = value;
2763 }
2764 break;
2765 case CELT_SET_CHANNELS_REQUEST10008:
2766 {
2767 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2768 if (value<1 || value>2)
2769 goto bad_arg;
2770 st->stream_channels = value;
2771 }
2772 break;
2773 case OPUS_SET_LSB_DEPTH_REQUEST4036:
2774 {
2775 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2776 if (value<8 || value>24)
2777 goto bad_arg;
2778 st->lsb_depth=value;
2779 }
2780 break;
2781 case OPUS_GET_LSB_DEPTH_REQUEST4037:
2782 {
2783 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
2784 *value=st->lsb_depth;
2785 }
2786 break;
2787 case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST4046:
2788 {
2789 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2790 if(value<0 || value>1)
2791 {
2792 goto bad_arg;
2793 }
2794 st->disable_inv = value;
2795 }
2796 break;
2797 case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST4047:
2798 {
2799 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
2800 if (!value)
2801 {
2802 goto bad_arg;
2803 }
2804 *value = st->disable_inv;
2805 }
2806 break;
2807 case OPUS_RESET_STATE4028:
2808 {
2809 int i;
2810 celt_glog *oldBandE, *oldLogE, *oldLogE2;
2811 oldBandE = (celt_glog*)(st->in_mem+st->channels*(st->mode->overlap+COMBFILTER_MAXPERIOD1024));
2812 oldLogE = oldBandE + st->channels*st->mode->nbEBands;
2813 oldLogE2 = oldLogE + st->channels*st->mode->nbEBands;
2814 OPUS_CLEAR((char*)&st->ENCODER_RESET_START,(memset(((char*)&st->rng), 0, (opus_custom_encoder_get_size
(st->mode, st->channels)- ((char*)&st->rng - (char
*)st))*sizeof(*((char*)&st->rng))))
2815 opus_custom_encoder_get_size(st->mode, st->channels)-(memset(((char*)&st->rng), 0, (opus_custom_encoder_get_size
(st->mode, st->channels)- ((char*)&st->rng - (char
*)st))*sizeof(*((char*)&st->rng))))
2816 ((char*)&st->ENCODER_RESET_START - (char*)st))(memset(((char*)&st->rng), 0, (opus_custom_encoder_get_size
(st->mode, st->channels)- ((char*)&st->rng - (char
*)st))*sizeof(*((char*)&st->rng))))
;
2817 for (i=0;i<st->channels*st->mode->nbEBands;i++)
2818 oldLogE[i]=oldLogE2[i]=-GCONST(28.f)(28.f);
2819 st->vbr_offset = 0;
2820 st->delayedIntra = 1;
2821 st->spread_decision = SPREAD_NORMAL(2);
2822 st->tonal_average = 256;
2823 st->hf_average = 0;
2824 st->tapset_decision = 0;
2825 }
2826 break;
2827#ifdef CUSTOM_MODES
2828 case CELT_SET_INPUT_CLIPPING_REQUEST10004:
2829 {
2830 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2831 st->clip = value;
2832 }
2833 break;
2834#endif
2835 case CELT_SET_SIGNALLING_REQUEST10016:
2836 {
2837 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2838 st->signalling = value;
2839 }
2840 break;
2841 case CELT_SET_ANALYSIS_REQUEST10022:
2842 {
2843 AnalysisInfo *info = va_arg(ap, AnalysisInfo *)__builtin_va_arg(ap, AnalysisInfo *);
2844 if (info)
2845 OPUS_COPY(&st->analysis, info, 1)(memcpy((&st->analysis), (info), (1)*sizeof(*(&st->
analysis)) + 0*((&st->analysis)-(info)) ))
;
2846 }
2847 break;
2848 case CELT_SET_SILK_INFO_REQUEST10028:
2849 {
2850 SILKInfo *info = va_arg(ap, SILKInfo *)__builtin_va_arg(ap, SILKInfo *);
2851 if (info)
2852 OPUS_COPY(&st->silk_info, info, 1)(memcpy((&st->silk_info), (info), (1)*sizeof(*(&st
->silk_info)) + 0*((&st->silk_info)-(info)) ))
;
2853 }
2854 break;
2855 case CELT_GET_MODE_REQUEST10015:
2856 {
2857 const CELTModeOpusCustomMode ** value = va_arg(ap, const CELTMode**)__builtin_va_arg(ap, const OpusCustomMode**);
2858 if (value==0)
2859 goto bad_arg;
2860 *value=st->mode;
2861 }
2862 break;
2863 case OPUS_GET_FINAL_RANGE_REQUEST4031:
2864 {
2865 opus_uint32 * value = va_arg(ap, opus_uint32 *)__builtin_va_arg(ap, opus_uint32 *);
2866 if (value==0)
2867 goto bad_arg;
2868 *value=st->rng;
2869 }
2870 break;
2871 case OPUS_SET_LFE_REQUEST10024:
2872 {
2873 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2874 st->lfe = value;
2875 }
2876 break;
2877 case OPUS_SET_ENERGY_MASK_REQUEST10026:
2878 {
2879 celt_glog *value = va_arg(ap, celt_glog*)__builtin_va_arg(ap, celt_glog*);
2880 st->energy_mask = value;
2881 }
2882 break;
2883 default:
2884 goto bad_request;
2885 }
2886 va_end(ap)__builtin_va_end(ap);
2887 return OPUS_OK0;
2888bad_arg:
2889 va_end(ap)__builtin_va_end(ap);
2890 return OPUS_BAD_ARG-1;
2891bad_request:
2892 va_end(ap)__builtin_va_end(ap);
2893 return OPUS_UNIMPLEMENTED-5;
2894}