Bug Summary

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