Bug Summary

File:root/firefox-clang/obj-x86_64-pc-linux-gnu/media/libopus/./../../../media/libopus/src/opus_encoder.c
Warning:line 1715, column 29
Although the value stored to 'curr_bandwidth' is used in the enclosing expression, the value is never actually read from 'curr_bandwidth'

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 Unified_c_media_libopus7.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=0145a6248c2171bb6c4e03fb54fe016b5123e37e -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-11-12-001017-1011516-1 -x c Unified_c_media_libopus7.c
1/* Copyright (c) 2010-2011 Xiph.Org Foundation, Skype Limited
2 Written by Jean-Marc Valin and Koen Vos */
3/*
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7
8 - Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 - Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
19 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include <stdarg.h>
33#include "celt.h"
34#include "entenc.h"
35#include "modes.h"
36#include "API.h"
37#include "stack_alloc.h"
38#include "float_cast.h"
39#include "opus.h"
40#include "arch.h"
41#include "pitch.h"
42#include "opus_private.h"
43#include "os_support.h"
44#include "cpu_support.h"
45#include "analysis.h"
46#include "mathops.h"
47#include "tuning_parameters.h"
48
49#ifdef ENABLE_DRED
50#include "dred_coding.h"
51#endif
52
53#ifdef FIXED_POINT
54#include "fixed/structs_FIX.h"
55#else
56#include "float/structs_FLP.h"
57#endif
58#ifdef ENABLE_OSCE_TRAINING_DATA
59#include <stdio.h>
60#endif
61
62#ifdef ENABLE_QEXT
63#define MAX_ENCODER_BUFFER480 960
64#else
65#define MAX_ENCODER_BUFFER480 480
66#endif
67
68#define PSEUDO_SNR_THRESHOLD316.23f 316.23f /* 10^(25/10) */
69
70typedef struct {
71 opus_val32 XX, XY, YY;
72 opus_val16 smoothed_width;
73 opus_val16 max_follower;
74} StereoWidthState;
75
76struct OpusEncoder {
77 int celt_enc_offset;
78 int silk_enc_offset;
79 silk_EncControlStruct silk_mode;
80#ifdef ENABLE_DRED
81 DREDEnc dred_encoder;
82#endif
83 int application;
84 int channels;
85 int delay_compensation;
86 int force_channels;
87 int signal_type;
88 int user_bandwidth;
89 int max_bandwidth;
90 int user_forced_mode;
91 int voice_ratio;
92 opus_int32 Fs;
93 int use_vbr;
94 int vbr_constraint;
95 int variable_duration;
96 opus_int32 bitrate_bps;
97 opus_int32 user_bitrate_bps;
98 int lsb_depth;
99 int encoder_buffer;
100 int lfe;
101 int arch;
102 int use_dtx; /* general DTX for both SILK and CELT */
103 int fec_config;
104#ifndef DISABLE_FLOAT_API
105 TonalityAnalysisState analysis;
106#endif
107#ifdef ENABLE_QEXT
108 int enable_qext;
109#endif
110
111#define OPUS_ENCODER_RESET_STARTstream_channels stream_channels
112 int stream_channels;
113 opus_int16 hybrid_stereo_width_Q14;
114 opus_int32 variable_HP_smth2_Q15;
115 opus_val16 prev_HB_gain;
116 opus_val32 hp_mem[4];
117 int mode;
118 int prev_mode;
119 int prev_channels;
120 int prev_framesize;
121 int bandwidth;
122 /* Bandwidth determined automatically from the rate (before any other adjustment) */
123 int auto_bandwidth;
124 int silk_bw_switch;
125 /* Sampling rate (at the API level) */
126 int first;
127 celt_glog * energy_masking;
128 StereoWidthState width_mem;
129#ifndef DISABLE_FLOAT_API
130 int detected_bandwidth;
131#endif
132 int nb_no_activity_ms_Q1;
133 opus_val32 peak_signal_energy;
134#ifdef ENABLE_DRED
135 int dred_duration;
136 int dred_q0;
137 int dred_dQ;
138 int dred_qmax;
139 int dred_target_chunks;
140 unsigned char activity_mem[DRED_MAX_FRAMES*4]; /* 2.5ms resolution*/
141#endif
142 int nonfinal_frame; /* current frame is not the final in a packet */
143 opus_uint32 rangeFinal;
144 /* Needs to be the last field because it may be partially or completely omitted. */
145 opus_res delay_buffer[MAX_ENCODER_BUFFER480*2];
146};
147
148/* Transition tables for the voice and music. First column is the
149 middle (memoriless) threshold. The second column is the hysteresis
150 (difference with the middle) */
151static const opus_int32 mono_voice_bandwidth_thresholds[8] = {
152 9000, 700, /* NB<->MB */
153 9000, 700, /* MB<->WB */
154 13500, 1000, /* WB<->SWB */
155 14000, 2000, /* SWB<->FB */
156};
157static const opus_int32 mono_music_bandwidth_thresholds[8] = {
158 9000, 700, /* NB<->MB */
159 9000, 700, /* MB<->WB */
160 11000, 1000, /* WB<->SWB */
161 12000, 2000, /* SWB<->FB */
162};
163static const opus_int32 stereo_voice_bandwidth_thresholds[8] = {
164 9000, 700, /* NB<->MB */
165 9000, 700, /* MB<->WB */
166 13500, 1000, /* WB<->SWB */
167 14000, 2000, /* SWB<->FB */
168};
169static const opus_int32 stereo_music_bandwidth_thresholds[8] = {
170 9000, 700, /* NB<->MB */
171 9000, 700, /* MB<->WB */
172 11000, 1000, /* WB<->SWB */
173 12000, 2000, /* SWB<->FB */
174};
175/* Threshold bit-rates for switching between mono and stereo */
176static const opus_int32 stereo_voice_threshold = 19000;
177static const opus_int32 stereo_music_threshold = 17000;
178
179/* Threshold bit-rate for switching between SILK/hybrid and CELT-only */
180static const opus_int32 mode_thresholds[2][2] = {
181 /* voice */ /* music */
182 { 64000, 10000}, /* mono */
183 { 44000, 10000}, /* stereo */
184};
185
186static const opus_int32 fec_thresholds[] = {
187 12000, 1000, /* NB */
188 14000, 1000, /* MB */
189 16000, 1000, /* WB */
190 20000, 1000, /* SWB */
191 22000, 1000, /* FB */
192};
193
194int opus_encoder_get_size(int channels)
195{
196 int ret;
197 ret = opus_encoder_init(NULL((void*)0), 48000, channels, OPUS_APPLICATION_AUDIO2049);
198 if (ret < 0)
199 return 0;
200 else
201 return ret;
202}
203
204int opus_encoder_init(OpusEncoder* st, opus_int32 Fs, int channels, int application)
205{
206 void *silk_enc=NULL((void*)0);
207 CELTEncoderOpusCustomEncoder *celt_enc=NULL((void*)0);
208 int err;
209 int ret, silkEncSizeBytes, celtEncSizeBytes=0;
210 int tot_size;
211 int base_size;
212
213 if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000
214#ifdef ENABLE_QEXT
215 &&Fs!=96000
216#endif
217 )||(channels!=1&&channels!=2)||
218 (application != OPUS_APPLICATION_VOIP2048 && application != OPUS_APPLICATION_AUDIO2049
219 && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY2051
220 && application != OPUS_APPLICATION_RESTRICTED_SILK2052
221 && application != OPUS_APPLICATION_RESTRICTED_CELT2053))
222 return OPUS_BAD_ARG-1;
223
224 /* Create SILK encoder */
225 ret = silk_Get_Encoder_Size( &silkEncSizeBytes, channels );
226 if (ret)
227 return OPUS_BAD_ARG-1;
228 silkEncSizeBytes = align(silkEncSizeBytes);
229 if (application == OPUS_APPLICATION_RESTRICTED_CELT2053)
230 silkEncSizeBytes = 0;
231 if (application != OPUS_APPLICATION_RESTRICTED_SILK2052)
232 celtEncSizeBytes = celt_encoder_get_size(channels);
233 base_size = align(sizeof(OpusEncoder));
234 if (application == OPUS_APPLICATION_RESTRICTED_SILK2052 || application == OPUS_APPLICATION_RESTRICTED_CELT2053) {
235 base_size = align(base_size - MAX_ENCODER_BUFFER480*2*sizeof(opus_res));
236 } else if (channels==1)
237 base_size = align(base_size - MAX_ENCODER_BUFFER480*sizeof(opus_res));
238 tot_size = base_size+silkEncSizeBytes+celtEncSizeBytes;
239 if (st == NULL((void*)0)) {
240 return tot_size;
241 }
242 OPUS_CLEAR((char*)st, tot_size)(memset(((char*)st), 0, (tot_size)*sizeof(*((char*)st))));
243 st->silk_enc_offset = base_size;
244 st->celt_enc_offset = st->silk_enc_offset+silkEncSizeBytes;
245
246 st->stream_channels = st->channels = channels;
247
248 st->Fs = Fs;
249
250 st->arch = opus_select_arch();
251
252 if (application != OPUS_APPLICATION_RESTRICTED_CELT2053)
253 {
254 silk_enc = (char*)st+st->silk_enc_offset;
255 ret = silk_InitEncoder( silk_enc, st->channels, st->arch, &st->silk_mode );
256 }
257 if(ret)return OPUS_INTERNAL_ERROR-3;
258
259 /* default SILK parameters */
260 st->silk_mode.nChannelsAPI = channels;
261 st->silk_mode.nChannelsInternal = channels;
262 st->silk_mode.API_sampleRate = st->Fs;
263 st->silk_mode.maxInternalSampleRate = 16000;
264 st->silk_mode.minInternalSampleRate = 8000;
265 st->silk_mode.desiredInternalSampleRate = 16000;
266 st->silk_mode.payloadSize_ms = 20;
267 st->silk_mode.bitRate = 25000;
268 st->silk_mode.packetLossPercentage = 0;
269 st->silk_mode.complexity = 9;
270 st->silk_mode.useInBandFEC = 0;
271 st->silk_mode.useDRED = 0;
272 st->silk_mode.useDTX = 0;
273 st->silk_mode.useCBR = 0;
274 st->silk_mode.reducedDependency = 0;
275
276 /* Create CELT encoder */
277 /* Initialize CELT encoder */
278 if (application != OPUS_APPLICATION_RESTRICTED_SILK2052)
279 {
280 celt_enc = (CELTEncoderOpusCustomEncoder*)((char*)st+st->celt_enc_offset);
281 err = celt_encoder_init(celt_enc, Fs, channels, st->arch);
282 if(err!=OPUS_OK0)return OPUS_INTERNAL_ERROR-3;
283 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, CELT_SET_SIGNALLING(0)10016, (((void)((0) == (opus_int32)0)), (opus_int32)(0)));
284 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(st->silk_mode.complexity)4010, (((void)((st->silk_mode.complexity) == (opus_int32)0
)), (opus_int32)(st->silk_mode.complexity))
);
285 }
286
287#ifdef ENABLE_DRED
288 /* Initialize DRED Encoder */
289 dred_encoder_init( &st->dred_encoder, Fs, channels );
290#endif
291
292 st->use_vbr = 1;
293 /* Makes constrained VBR the default (safer for real-time use) */
294 st->vbr_constraint = 1;
295 st->user_bitrate_bps = OPUS_AUTO-1000;
296 st->bitrate_bps = 3000+Fs*channels;
297 st->application = application;
298 st->signal_type = OPUS_AUTO-1000;
299 st->user_bandwidth = OPUS_AUTO-1000;
300 st->max_bandwidth = OPUS_BANDWIDTH_FULLBAND1105;
301 st->force_channels = OPUS_AUTO-1000;
302 st->user_forced_mode = OPUS_AUTO-1000;
303 st->voice_ratio = -1;
304 if (application != OPUS_APPLICATION_RESTRICTED_CELT2053 && application != OPUS_APPLICATION_RESTRICTED_SILK2052)
305 st->encoder_buffer = st->Fs/100;
306 else
307 st->encoder_buffer = 0;
308 st->lsb_depth = 24;
309 st->variable_duration = OPUS_FRAMESIZE_ARG5000;
310
311 /* Delay compensation of 4 ms (2.5 ms for SILK's extra look-ahead
312 + 1.5 ms for SILK resamplers and stereo prediction) */
313 st->delay_compensation = st->Fs/250;
314
315 st->hybrid_stereo_width_Q14 = 1 << 14;
316 st->prev_HB_gain = Q15ONE1.0f;
317 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 )((opus_int32)((opus_uint32)(silk_lin2log( 60 ))<<(8)));
318 st->first = 1;
319 st->mode = MODE_HYBRID1001;
320 st->bandwidth = OPUS_BANDWIDTH_FULLBAND1105;
321
322#ifndef DISABLE_FLOAT_API
323 tonality_analysis_init(&st->analysis, st->Fs);
324 st->analysis.application = st->application;
325#endif
326
327 return OPUS_OK0;
328}
329
330static unsigned char gen_toc(int mode, int framerate, int bandwidth, int channels)
331{
332 int period;
333 unsigned char toc;
334 period = 0;
335 while (framerate < 400)
336 {
337 framerate <<= 1;
338 period++;
339 }
340 if (mode == MODE_SILK_ONLY1000)
341 {
342 toc = (bandwidth-OPUS_BANDWIDTH_NARROWBAND1101)<<5;
343 toc |= (period-2)<<3;
344 } else if (mode == MODE_CELT_ONLY1002)
345 {
346 int tmp = bandwidth-OPUS_BANDWIDTH_MEDIUMBAND1102;
347 if (tmp < 0)
348 tmp = 0;
349 toc = 0x80;
350 toc |= tmp << 5;
351 toc |= period<<3;
352 } else /* Hybrid */
353 {
354 toc = 0x60;
355 toc |= (bandwidth-OPUS_BANDWIDTH_SUPERWIDEBAND1104)<<4;
356 toc |= (period-2)<<3;
357 }
358 toc |= (channels==2)<<2;
359 return toc;
360}
361
362#ifdef FIXED_POINT
363/* Second order ARMA filter, alternative implementation */
364void silk_biquad_res(
365 const opus_res *in, /* I input signal */
366 const opus_int32 *B_Q28, /* I MA coefficients [3] */
367 const opus_int32 *A_Q28, /* I AR coefficients [2] */
368 opus_int32 *S, /* I/O State vector [2] */
369 opus_res *out, /* O output signal */
370 const opus_int32 len, /* I signal length (must be even) */
371 int stride
372)
373{
374 /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */
375 opus_intint k;
376 opus_int32 inval, A0_U_Q28, A0_L_Q28, A1_U_Q28, A1_L_Q28, out32_Q14;
377
378 /* Negate A_Q28 values and split in two parts */
379 A0_L_Q28 = ( -A_Q28[ 0 ] ) & 0x00003FFF; /* lower part */
380 A0_U_Q28 = silk_RSHIFT( -A_Q28[ 0 ], 14 )((-A_Q28[ 0 ])>>(14)); /* upper part */
381 A1_L_Q28 = ( -A_Q28[ 1 ] ) & 0x00003FFF; /* lower part */
382 A1_U_Q28 = silk_RSHIFT( -A_Q28[ 1 ], 14 )((-A_Q28[ 1 ])>>(14)); /* upper part */
383
384 for( k = 0; k < len; k++ ) {
385 /* S[ 0 ], S[ 1 ]: Q12 */
386 inval = RES2INT16(in[ k*stride ])FLOAT2INT16(in[ k*stride ]);
387 out32_Q14 = silk_LSHIFT( silk_SMLAWB( S[ 0 ], B_Q28[ 0 ], inval ), 2 )((opus_int32)((opus_uint32)(((opus_int32)((S[ 0 ]) + (((B_Q28
[ 0 ]) * (opus_int64)((opus_int16)(inval))) >> 16))))<<
(2)))
;
388
389 S[ 0 ] = S[1] + silk_RSHIFT_ROUND( silk_SMULWB( out32_Q14, A0_L_Q28 ), 14 )((14) == 1 ? ((((opus_int32)(((out32_Q14) * (opus_int64)((opus_int16
)(A0_L_Q28))) >> 16))) >> 1) + ((((opus_int32)(((
out32_Q14) * (opus_int64)((opus_int16)(A0_L_Q28))) >> 16
))) & 1) : (((((opus_int32)(((out32_Q14) * (opus_int64)((
opus_int16)(A0_L_Q28))) >> 16))) >> ((14) - 1)) +
1) >> 1)
;
390 S[ 0 ] = silk_SMLAWB( S[ 0 ], out32_Q14, A0_U_Q28 )((opus_int32)((S[ 0 ]) + (((out32_Q14) * (opus_int64)((opus_int16
)(A0_U_Q28))) >> 16)))
;
391 S[ 0 ] = silk_SMLAWB( S[ 0 ], B_Q28[ 1 ], inval)((opus_int32)((S[ 0 ]) + (((B_Q28[ 1 ]) * (opus_int64)((opus_int16
)(inval))) >> 16)))
;
392
393 S[ 1 ] = silk_RSHIFT_ROUND( silk_SMULWB( out32_Q14, A1_L_Q28 ), 14 )((14) == 1 ? ((((opus_int32)(((out32_Q14) * (opus_int64)((opus_int16
)(A1_L_Q28))) >> 16))) >> 1) + ((((opus_int32)(((
out32_Q14) * (opus_int64)((opus_int16)(A1_L_Q28))) >> 16
))) & 1) : (((((opus_int32)(((out32_Q14) * (opus_int64)((
opus_int16)(A1_L_Q28))) >> 16))) >> ((14) - 1)) +
1) >> 1)
;
394 S[ 1 ] = silk_SMLAWB( S[ 1 ], out32_Q14, A1_U_Q28 )((opus_int32)((S[ 1 ]) + (((out32_Q14) * (opus_int64)((opus_int16
)(A1_U_Q28))) >> 16)))
;
395 S[ 1 ] = silk_SMLAWB( S[ 1 ], B_Q28[ 2 ], inval )((opus_int32)((S[ 1 ]) + (((B_Q28[ 2 ]) * (opus_int64)((opus_int16
)(inval))) >> 16)))
;
396
397 /* Scale back to Q0 and saturate */
398 out[ k*stride ] = INT16TORES( silk_SAT16( silk_RSHIFT( out32_Q14 + (1<<14) - 1, 14 ) ) )((((((out32_Q14 + (1<<14) - 1)>>(14))) > 0x7FFF
? 0x7FFF : ((((out32_Q14 + (1<<14) - 1)>>(14))) <
((opus_int16)0x8000) ? ((opus_int16)0x8000) : (((out32_Q14 +
(1<<14) - 1)>>(14))))))*(1/32768.f))
;
399 }
400}
401#else
402static void silk_biquad_res(
403 const opus_res *in, /* I: Input signal */
404 const opus_int32 *B_Q28, /* I: MA coefficients [3] */
405 const opus_int32 *A_Q28, /* I: AR coefficients [2] */
406 opus_val32 *S, /* I/O: State vector [2] */
407 opus_res *out, /* O: Output signal */
408 const opus_int32 len, /* I: Signal length (must be even) */
409 int stride
410)
411{
412 /* DIRECT FORM II TRANSPOSED (uses 2 element state vector) */
413 opus_intint k;
414 opus_val32 vout;
415 opus_val32 inval;
416 opus_val32 A[2], B[3];
417
418 A[0] = (opus_val32)(A_Q28[0] * (1.f/((opus_int32)1<<28)));
419 A[1] = (opus_val32)(A_Q28[1] * (1.f/((opus_int32)1<<28)));
420 B[0] = (opus_val32)(B_Q28[0] * (1.f/((opus_int32)1<<28)));
421 B[1] = (opus_val32)(B_Q28[1] * (1.f/((opus_int32)1<<28)));
422 B[2] = (opus_val32)(B_Q28[2] * (1.f/((opus_int32)1<<28)));
423
424 /* Negate A_Q28 values and split in two parts */
425
426 for( k = 0; k < len; k++ ) {
427 /* S[ 0 ], S[ 1 ]: Q12 */
428 inval = in[ k*stride ];
429 vout = S[ 0 ] + B[0]*inval;
430
431 S[ 0 ] = S[1] - vout*A[0] + B[1]*inval;
432
433 S[ 1 ] = - vout*A[1] + B[2]*inval + VERY_SMALL1e-30f;
434
435 /* Scale back to Q0 and saturate */
436 out[ k*stride ] = vout;
437 }
438}
439#endif
440
441static void hp_cutoff(const opus_res *in, opus_int32 cutoff_Hz, opus_res *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs, int arch)
442{
443 opus_int32 B_Q28[ 3 ], A_Q28[ 2 ];
444 opus_int32 Fc_Q19, r_Q28, r_Q22;
445 (void)arch;
446
447 silk_assert( cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ) ){if (!(cutoff_Hz <= 0x7FFFFFFF / ((opus_int32)((1.5 * 3.14159
/ 1000) * ((opus_int64)1 << (19)) + 0.5)))) {_silk_fatal
("assertion failed: " "cutoff_Hz <= silk_int32_MAX / SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 )"
, "./../../../media/libopus/src/opus_encoder.c", 447);;}}
;
448 Fc_Q19 = silk_DIV32_16( silk_SMULBB( SILK_FIX_CONST( 1.5 * 3.14159 / 1000, 19 ), cutoff_Hz ), Fs/1000 )((opus_int32)((((opus_int32)((opus_int16)(((opus_int32)((1.5 *
3.14159 / 1000) * ((opus_int64)1 << (19)) + 0.5)))) * (
opus_int32)((opus_int16)(cutoff_Hz)))) / (Fs/1000)))
;
449 silk_assert( Fc_Q19 > 0 && Fc_Q19 < 32768 ){if (!(Fc_Q19 > 0 && Fc_Q19 < 32768)) {_silk_fatal
("assertion failed: " "Fc_Q19 > 0 && Fc_Q19 < 32768"
, "./../../../media/libopus/src/opus_encoder.c", 449);;}}
;
450
451 r_Q28 = SILK_FIX_CONST( 1.0, 28 )((opus_int32)((1.0) * ((opus_int64)1 << (28)) + 0.5)) - silk_MUL( SILK_FIX_CONST( 0.92, 9 ), Fc_Q19 )((((opus_int32)((0.92) * ((opus_int64)1 << (9)) + 0.5))
) * (Fc_Q19))
;
452
453 /* b = r * [ 1; -2; 1 ]; */
454 /* a = [ 1; -2 * r * ( 1 - 0.5 * Fc^2 ); r^2 ]; */
455 B_Q28[ 0 ] = r_Q28;
456 B_Q28[ 1 ] = silk_LSHIFT( -r_Q28, 1 )((opus_int32)((opus_uint32)(-r_Q28)<<(1)));
457 B_Q28[ 2 ] = r_Q28;
458
459 /* -r * ( 2 - Fc * Fc ); */
460 r_Q22 = silk_RSHIFT( r_Q28, 6 )((r_Q28)>>(6));
461 A_Q28[ 0 ] = silk_SMULWW( r_Q22, silk_SMULWW( Fc_Q19, Fc_Q19 ) - SILK_FIX_CONST( 2.0, 22 ) )((opus_int32)(((opus_int64)(r_Q22) * (((opus_int32)(((opus_int64
)(Fc_Q19) * (Fc_Q19)) >> 16)) - ((opus_int32)((2.0) * (
(opus_int64)1 << (22)) + 0.5)))) >> 16))
;
462 A_Q28[ 1 ] = silk_SMULWW( r_Q22, r_Q22 )((opus_int32)(((opus_int64)(r_Q22) * (r_Q22)) >> 16));
463
464#if defined(FIXED_POINT) && !defined(ENABLE_RES24)
465 if( channels == 1 ) {
466 silk_biquad_alt_stride1( in, B_Q28, A_Q28, hp_mem, out, len );
467 } else {
468 silk_biquad_alt_stride2( in, B_Q28, A_Q28, hp_mem, out, len, arch )((void)(arch), silk_biquad_alt_stride2_c(in, B_Q28, A_Q28, hp_mem
, out, len))
;
469 }
470#else
471 silk_biquad_res( in, B_Q28, A_Q28, hp_mem, out, len, channels );
472 if( channels == 2 ) {
473 silk_biquad_res( in+1, B_Q28, A_Q28, hp_mem+2, out+1, len, channels );
474 }
475#endif
476}
477
478#ifdef FIXED_POINT
479static void dc_reject(const opus_res *in, opus_int32 cutoff_Hz, opus_res *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs)
480{
481 int c, i;
482 int shift;
483
484 /* Approximates -round(log2(6.3*cutoff_Hz/Fs)) */
485 shift=celt_ilog2(Fs/(cutoff_Hz*4));
486 for (c=0;c<channels;c++)
487 {
488 for (i=0;i<len;i++)
489 {
490 opus_val32 x, y;
491 /* Saturate at +6 dBFS to avoid any wrap-around. */
492 x = SATURATE((opus_val32)in[channels*i+c], (1<<16<<RES_SHIFT)-1)((opus_val32)in[channels*i+c]);
493 x = SHL32(x, 14-RES_SHIFT)(x);
494 y = x-hp_mem[2*c];
495 hp_mem[2*c] = hp_mem[2*c] + PSHR32(x - hp_mem[2*c], shift)(x - hp_mem[2*c]);
496#ifdef ENABLE_RES24
497 /* Don't saturate if we have the headroom to avoid it. */
498 out[channels*i+c] = PSHR32(y, 14-RES_SHIFT)(y);
499#else
500 out[channels*i+c] = SATURATE(PSHR32(y, 14-RES_SHIFT), 32767)((y));
501#endif
502 }
503 }
504}
505
506#else
507static void dc_reject(const opus_val16 *in, opus_int32 cutoff_Hz, opus_val16 *out, opus_val32 *hp_mem, int len, int channels, opus_int32 Fs)
508{
509 int i;
510 float coef, coef2;
511 coef = 6.3f*cutoff_Hz/Fs;
512 coef2 = 1-coef;
513 if (channels==2)
514 {
515 float m0, m2;
516 m0 = hp_mem[0];
517 m2 = hp_mem[2];
518 for (i=0;i<len;i++)
519 {
520 opus_val32 x0, x1, out0, out1;
521 x0 = in[2*i+0];
522 x1 = in[2*i+1];
523 out0 = x0-m0;
524 out1 = x1-m2;
525 m0 = coef*x0 + VERY_SMALL1e-30f + coef2*m0;
526 m2 = coef*x1 + VERY_SMALL1e-30f + coef2*m2;
527 out[2*i+0] = out0;
528 out[2*i+1] = out1;
529 }
530 hp_mem[0] = m0;
531 hp_mem[2] = m2;
532 } else {
533 float m0;
534 m0 = hp_mem[0];
535 for (i=0;i<len;i++)
536 {
537 opus_val32 x, y;
538 x = in[i];
539 y = x-m0;
540 m0 = coef*x + VERY_SMALL1e-30f + coef2*m0;
541 out[i] = y;
542 }
543 hp_mem[0] = m0;
544 }
545}
546#endif
547
548static void stereo_fade(const opus_res *in, opus_res *out, opus_val16 g1, opus_val16 g2,
549 int overlap48, int frame_size, int channels, const celt_coef *window, opus_int32 Fs)
550{
551 int i;
552 int overlap;
553 int inc;
554 inc = IMAX(1, 48000/Fs)((1) > (48000/Fs) ? (1) : (48000/Fs));
555 overlap=overlap48/inc;
556 g1 = Q15ONE1.0f-g1;
557 g2 = Q15ONE1.0f-g2;
558 for (i=0;i<overlap;i++)
559 {
560 opus_val32 diff;
561 opus_val16 g, w;
562 w = COEF2VAL16(window[i*inc])(window[i*inc]);
563 w = MULT16_16_Q15(w, w)((w)*(w));
564 g = SHR32(MAC16_16(MULT16_16(w,g2),(((((opus_val32)(w)*(opus_val32)(g2)))+(opus_val32)(1.0f -w)*
(opus_val32)(g1)))
565 Q15ONE-w, g1), 15)(((((opus_val32)(w)*(opus_val32)(g2)))+(opus_val32)(1.0f -w)*
(opus_val32)(g1)))
;
566 diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1])(.5f*((opus_val32)in[i*channels] - (opus_val32)in[i*channels+
1]))
;
567 diff = MULT16_RES_Q15(g, diff)((g)*(diff));
568 out[i*channels] = out[i*channels] - diff;
569 out[i*channels+1] = out[i*channels+1] + diff;
570 }
571 for (;i<frame_size;i++)
572 {
573 opus_val32 diff;
574 diff = HALF32((opus_val32)in[i*channels] - (opus_val32)in[i*channels+1])(.5f*((opus_val32)in[i*channels] - (opus_val32)in[i*channels+
1]))
;
575 diff = MULT16_RES_Q15(g2, diff)((g2)*(diff));
576 out[i*channels] = out[i*channels] - diff;
577 out[i*channels+1] = out[i*channels+1] + diff;
578 }
579}
580
581static void gain_fade(const opus_res *in, opus_res *out, opus_val16 g1, opus_val16 g2,
582 int overlap48, int frame_size, int channels, const celt_coef *window, opus_int32 Fs)
583{
584 int i;
585 int inc;
586 int overlap;
587 int c;
588 inc = IMAX(1, 48000/Fs)((1) > (48000/Fs) ? (1) : (48000/Fs));
589 overlap=overlap48/inc;
590 if (channels==1)
591 {
592 for (i=0;i<overlap;i++)
593 {
594 opus_val16 g, w;
595 w = COEF2VAL16(window[i*inc])(window[i*inc]);
596 w = MULT16_16_Q15(w, w)((w)*(w));
597 g = SHR32(MAC16_16(MULT16_16(w,g2),(((((opus_val32)(w)*(opus_val32)(g2)))+(opus_val32)(1.0f -w)*
(opus_val32)(g1)))
598 Q15ONE-w, g1), 15)(((((opus_val32)(w)*(opus_val32)(g2)))+(opus_val32)(1.0f -w)*
(opus_val32)(g1)))
;
599 out[i] = MULT16_RES_Q15(g, in[i])((g)*(in[i]));
600 }
601 } else {
602 for (i=0;i<overlap;i++)
603 {
604 opus_val16 g, w;
605 w = COEF2VAL16(window[i*inc])(window[i*inc]);
606 w = MULT16_16_Q15(w, w)((w)*(w));
607 g = SHR32(MAC16_16(MULT16_16(w,g2),(((((opus_val32)(w)*(opus_val32)(g2)))+(opus_val32)(1.0f -w)*
(opus_val32)(g1)))
608 Q15ONE-w, g1), 15)(((((opus_val32)(w)*(opus_val32)(g2)))+(opus_val32)(1.0f -w)*
(opus_val32)(g1)))
;
609 out[i*2] = MULT16_RES_Q15(g, in[i*2])((g)*(in[i*2]));
610 out[i*2+1] = MULT16_RES_Q15(g, in[i*2+1])((g)*(in[i*2+1]));
611 }
612 }
613 c=0;do {
614 for (i=overlap;i<frame_size;i++)
615 {
616 out[i*channels+c] = MULT16_RES_Q15(g2, in[i*channels+c])((g2)*(in[i*channels+c]));
617 }
618 }
619 while (++c<channels);
620}
621
622OpusEncoder *opus_encoder_create(opus_int32 Fs, int channels, int application, int *error)
623{
624 int ret;
625 OpusEncoder *st;
626 int size;
627 if((Fs!=48000&&Fs!=24000&&Fs!=16000&&Fs!=12000&&Fs!=8000
628#ifdef ENABLE_QEXT
629 &&Fs!=96000
630#endif
631 )||(channels!=1&&channels!=2)||
632 (application != OPUS_APPLICATION_VOIP2048 && application != OPUS_APPLICATION_AUDIO2049
633 && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY2051
634 && application != OPUS_APPLICATION_RESTRICTED_SILK2052
635 && application != OPUS_APPLICATION_RESTRICTED_CELT2053))
636 {
637 if (error)
638 *error = OPUS_BAD_ARG-1;
639 return NULL((void*)0);
640 }
641 size = opus_encoder_init(NULL((void*)0), Fs, channels, application);
642 if (size <= 0)
643 {
644 if (error)
645 *error = OPUS_INTERNAL_ERROR-3;
646 return NULL((void*)0);
647 }
648 st = (OpusEncoder *)opus_alloc(size);
649 if (st == NULL((void*)0))
650 {
651 if (error)
652 *error = OPUS_ALLOC_FAIL-7;
653 return NULL((void*)0);
654 }
655 ret = opus_encoder_init(st, Fs, channels, application);
656 if (error)
657 *error = ret;
658 if (ret != OPUS_OK0)
659 {
660 opus_free(st);
661 st = NULL((void*)0);
662 }
663 return st;
664}
665
666#ifdef ENABLE_DRED
667
668static const float dred_bits_table[16] = {73.2f, 68.1f, 62.5f, 57.0f, 51.5f, 45.7f, 39.9f, 32.4f, 26.4f, 20.4f, 16.3f, 13.f, 9.3f, 8.2f, 7.2f, 6.4f};
669static int estimate_dred_bitrate(int q0, int dQ, int qmax, int duration, opus_int32 target_bits, int *target_chunks) {
670 int dred_chunks;
671 int i;
672 float bits;
673 /* Signaling DRED costs 3 bytes. */
674 bits = 8*(3+DRED_EXPERIMENTAL_BYTES);
675 /* Approximation for the size of the IS. */
676 bits += 50.f+dred_bits_table[q0];
677 dred_chunks = IMIN((duration+5)/4, DRED_NUM_REDUNDANCY_FRAMES/2)(((duration+5)/4) < (DRED_NUM_REDUNDANCY_FRAMES/2) ? ((duration
+5)/4) : (DRED_NUM_REDUNDANCY_FRAMES/2))
;
678 if (target_chunks != NULL((void*)0)) *target_chunks = 0;
679 for (i=0;i<dred_chunks;i++) {
680 int q = compute_quantizer(q0, dQ, qmax, i);
681 bits += dred_bits_table[q];
682 if (target_chunks != NULL((void*)0) && bits < target_bits) *target_chunks = i+1;
683 }
684 return (int)floor(.5f+bits);
685}
686
687static opus_int32 compute_dred_bitrate(OpusEncoder *st, opus_int32 bitrate_bps, int frame_size)
688{
689 float dred_frac;
690 int bitrate_offset;
691 opus_int32 dred_bitrate;
692 opus_int32 target_dred_bitrate;
693 int target_chunks;
694 opus_int32 max_dred_bits;
695 int q0, dQ, qmax;
696 if (st->silk_mode.useInBandFEC) {
697 dred_frac = MIN16(.7f, 3.f*st->silk_mode.packetLossPercentage/100.f)((.7f) < (3.f*st->silk_mode.packetLossPercentage/100.f)
? (.7f) : (3.f*st->silk_mode.packetLossPercentage/100.f))
;
698 bitrate_offset = 20000;
699 } else {
700 if (st->silk_mode.packetLossPercentage > 5) {
701 dred_frac = MIN16(.8f, .55f + st->silk_mode.packetLossPercentage/100.f)((.8f) < (.55f + st->silk_mode.packetLossPercentage/100.f
) ? (.8f) : (.55f + st->silk_mode.packetLossPercentage/100.f
))
;
702 } else {
703 dred_frac = 12*st->silk_mode.packetLossPercentage/100.f;
704 }
705 bitrate_offset = 12000;
706 }
707 /* Account for the fact that longer packets require less redundancy. */
708 dred_frac = dred_frac/(dred_frac + (1-dred_frac)*(frame_size*50.f)/st->Fs);
709 /* Approximate fit based on a few experiments. Could probably be improved. */
710 q0 = IMIN(15, IMAX(4, 51 - 3*EC_ILOG(IMAX(1, bitrate_bps-bitrate_offset))))((15) < (((4) > (51 - 3*(((int)sizeof(unsigned)*8)-(__builtin_clz
(((1) > (bitrate_bps-bitrate_offset) ? (1) : (bitrate_bps-
bitrate_offset)))))) ? (4) : (51 - 3*(((int)sizeof(unsigned)*
8)-(__builtin_clz(((1) > (bitrate_bps-bitrate_offset) ? (1
) : (bitrate_bps-bitrate_offset)))))))) ? (15) : (((4) > (
51 - 3*(((int)sizeof(unsigned)*8)-(__builtin_clz(((1) > (bitrate_bps
-bitrate_offset) ? (1) : (bitrate_bps-bitrate_offset)))))) ? (
4) : (51 - 3*(((int)sizeof(unsigned)*8)-(__builtin_clz(((1) >
(bitrate_bps-bitrate_offset) ? (1) : (bitrate_bps-bitrate_offset
)))))))))
;
711 dQ = bitrate_bps-bitrate_offset > 36000 ? 3 : 5;
712 qmax = 15;
713 target_dred_bitrate = IMAX(0, (int)(dred_frac*(bitrate_bps-bitrate_offset)))((0) > ((int)(dred_frac*(bitrate_bps-bitrate_offset))) ? (
0) : ((int)(dred_frac*(bitrate_bps-bitrate_offset))))
;
714 if (st->dred_duration > 0) {
715 opus_int32 target_bits = bitrate_to_bits(target_dred_bitrate, st->Fs, frame_size);
716 max_dred_bits = estimate_dred_bitrate(q0, dQ, qmax, st->dred_duration, target_bits, &target_chunks);
717 } else {
718 max_dred_bits = 0;
719 target_chunks=0;
720 }
721 dred_bitrate = IMIN(target_dred_bitrate, bits_to_bitrate(max_dred_bits, st->Fs, frame_size))((target_dred_bitrate) < (bits_to_bitrate(max_dred_bits, st
->Fs, frame_size)) ? (target_dred_bitrate) : (bits_to_bitrate
(max_dred_bits, st->Fs, frame_size)))
;
722 /* If we can't afford enough bits, don't bother with DRED at all. */
723 if (target_chunks < 2)
724 dred_bitrate = 0;
725 st->dred_q0 = q0;
726 st->dred_dQ = dQ;
727 st->dred_qmax = qmax;
728 st->dred_target_chunks = target_chunks;
729 return dred_bitrate;
730}
731#endif
732
733static opus_int32 user_bitrate_to_bitrate(OpusEncoder *st, int frame_size, int max_data_bytes)
734{
735 opus_int32 max_bitrate, user_bitrate;
736 if(!frame_size)frame_size=st->Fs/400;
737 max_bitrate = bits_to_bitrate(max_data_bytes*8, st->Fs, frame_size);
738 if (st->user_bitrate_bps==OPUS_AUTO-1000)
739 user_bitrate = 60*st->Fs/frame_size + st->Fs*st->channels;
740 else if (st->user_bitrate_bps==OPUS_BITRATE_MAX-1)
741 user_bitrate = 1500000;
742 else
743 user_bitrate = st->user_bitrate_bps;
744 return IMIN(user_bitrate, max_bitrate)((user_bitrate) < (max_bitrate) ? (user_bitrate) : (max_bitrate
))
;
745}
746
747#ifndef DISABLE_FLOAT_API
748void downmix_float(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C)
749{
750 const float *x;
751 int j;
752
753 x = (const float *)_x;
754 for (j=0;j<subframe;j++)
755 y[j] = FLOAT2SIG(x[(j+offset)*C+c1])((x[(j+offset)*C+c1])*32768.f);
756 if (c2>-1)
757 {
758 for (j=0;j<subframe;j++)
759 y[j] += FLOAT2SIG(x[(j+offset)*C+c2])((x[(j+offset)*C+c2])*32768.f);
760 } else if (c2==-2)
761 {
762 int c;
763 for (c=1;c<C;c++)
764 {
765 for (j=0;j<subframe;j++)
766 y[j] += FLOAT2SIG(x[(j+offset)*C+c])((x[(j+offset)*C+c])*32768.f);
767 }
768 }
769#ifndef FIXED_POINT
770 /* Cap signal to +6 dBFS to avoid problems in the analysis. */
771 for (j=0;j<subframe;j++)
772 {
773 if (y[j] < -65536.f) y[j] = -65536.f;
774 if (y[j] > 65536.f) y[j] = 65536.f;
775 if (celt_isnan(y[j])((y[j])!=(y[j]))) y[j] = 0;
776 }
777#endif
778}
779#endif
780
781void downmix_int(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C)
782{
783 const opus_int16 *x;
784 int j;
785
786 x = (const opus_int16 *)_x;
787 for (j=0;j<subframe;j++)
788 y[j] = INT16TOSIG(x[(j+offset)*C+c1])((float)(x[(j+offset)*C+c1]));
789 if (c2>-1)
790 {
791 for (j=0;j<subframe;j++)
792 y[j] += INT16TOSIG(x[(j+offset)*C+c2])((float)(x[(j+offset)*C+c2]));
793 } else if (c2==-2)
794 {
795 int c;
796 for (c=1;c<C;c++)
797 {
798 for (j=0;j<subframe;j++)
799 y[j] += INT16TOSIG(x[(j+offset)*C+c])((float)(x[(j+offset)*C+c]));
800 }
801 }
802}
803
804void downmix_int24(const void *_x, opus_val32 *y, int subframe, int offset, int c1, int c2, int C)
805{
806 const opus_int32 *x;
807 int j;
808
809 x = (const opus_int32 *)_x;
810 for (j=0;j<subframe;j++)
811 y[j] = INT24TOSIG(x[(j+offset)*C+c1])((float)(x[(j+offset)*C+c1])*(1.f/256.f));
812 if (c2>-1)
813 {
814 for (j=0;j<subframe;j++)
815 y[j] += INT24TOSIG(x[(j+offset)*C+c2])((float)(x[(j+offset)*C+c2])*(1.f/256.f));
816 } else if (c2==-2)
817 {
818 int c;
819 for (c=1;c<C;c++)
820 {
821 for (j=0;j<subframe;j++)
822 y[j] += INT24TOSIG(x[(j+offset)*C+c])((float)(x[(j+offset)*C+c])*(1.f/256.f));
823 }
824 }
825}
826
827opus_int32 frame_size_select(int application, opus_int32 frame_size, int variable_duration, opus_int32 Fs)
828{
829 int new_size;
830 if (frame_size<Fs/400)
831 return -1;
832 if (variable_duration == OPUS_FRAMESIZE_ARG5000)
833 new_size = frame_size;
834 else if (variable_duration >= OPUS_FRAMESIZE_2_5_MS5001 && variable_duration <= OPUS_FRAMESIZE_120_MS5009)
835 {
836 if (variable_duration <= OPUS_FRAMESIZE_40_MS5005)
837 new_size = (Fs/400)<<(variable_duration-OPUS_FRAMESIZE_2_5_MS5001);
838 else
839 new_size = (variable_duration-OPUS_FRAMESIZE_2_5_MS5001-2)*Fs/50;
840 }
841 else
842 return -1;
843 if (new_size>frame_size)
844 return -1;
845 /* Every legal duration below is at most 6*Fs/50 (120 ms), so a caller
846 passing a frame_size large enough to overflow the multiplications
847 that follow can only be trying to smuggle through an out-of-range
848 value: new_size can't legitimately reach here already bigger than
849 Fs itself, so reject it before the overflow-prone checks run. */
850 if (new_size>Fs)
851 return -1;
852 if (400*new_size!=Fs && 200*new_size!=Fs && 100*new_size!=Fs &&
853 50*new_size!=Fs && 25*new_size!=Fs && 50*new_size!=3*Fs &&
854 50*new_size!=4*Fs && 50*new_size!=5*Fs && 50*new_size!=6*Fs)
855 return -1;
856 if (application == OPUS_APPLICATION_RESTRICTED_SILK2052 && new_size < Fs/100)
857 return -1;
858 return new_size;
859}
860
861opus_val16 compute_stereo_width(const opus_res *pcm, int frame_size, opus_int32 Fs, StereoWidthState *mem)
862{
863 opus_val32 xx, xy, yy;
864 opus_val16 sqrt_xx, sqrt_yy;
865 opus_val16 qrrt_xx, qrrt_yy;
866 int frame_rate;
867 int i;
868 opus_val16 short_alpha;
869#ifdef FIXED_POINT
870 int shift = celt_ilog2(frame_size)-2;
871#endif
872 frame_rate = Fs/frame_size;
873 short_alpha = MULT16_16(25, Q15ONE)((opus_val32)(25)*(opus_val32)(1.0f))/IMAX(50,frame_rate)((50) > (frame_rate) ? (50) : (frame_rate));
874 xx=xy=yy=0;
875 /* Unroll by 4. The frame size is always a multiple of 4 *except* for
876 2.5 ms frames at 12 kHz. Since this setting is very rare (and very
877 stupid), we just discard the last two samples. */
878 for (i=0;i<frame_size-3;i+=4)
879 {
880 opus_val32 pxx=0;
881 opus_val32 pxy=0;
882 opus_val32 pyy=0;
883 opus_val16 x, y;
884 x = RES2VAL16(pcm[2*i])(pcm[2*i]);
885 y = RES2VAL16(pcm[2*i+1])(pcm[2*i+1]);
886 pxx = SHR32(MULT16_16(x,x),2)(((opus_val32)(x)*(opus_val32)(x)));
887 pxy = SHR32(MULT16_16(x,y),2)(((opus_val32)(x)*(opus_val32)(y)));
888 pyy = SHR32(MULT16_16(y,y),2)(((opus_val32)(y)*(opus_val32)(y)));
889 x = RES2VAL16(pcm[2*i+2])(pcm[2*i+2]);
890 y = RES2VAL16(pcm[2*i+3])(pcm[2*i+3]);
891 pxx += SHR32(MULT16_16(x,x),2)(((opus_val32)(x)*(opus_val32)(x)));
892 pxy += SHR32(MULT16_16(x,y),2)(((opus_val32)(x)*(opus_val32)(y)));
893 pyy += SHR32(MULT16_16(y,y),2)(((opus_val32)(y)*(opus_val32)(y)));
894 x = RES2VAL16(pcm[2*i+4])(pcm[2*i+4]);
895 y = RES2VAL16(pcm[2*i+5])(pcm[2*i+5]);
896 pxx += SHR32(MULT16_16(x,x),2)(((opus_val32)(x)*(opus_val32)(x)));
897 pxy += SHR32(MULT16_16(x,y),2)(((opus_val32)(x)*(opus_val32)(y)));
898 pyy += SHR32(MULT16_16(y,y),2)(((opus_val32)(y)*(opus_val32)(y)));
899 x = RES2VAL16(pcm[2*i+6])(pcm[2*i+6]);
900 y = RES2VAL16(pcm[2*i+7])(pcm[2*i+7]);
901 pxx += SHR32(MULT16_16(x,x),2)(((opus_val32)(x)*(opus_val32)(x)));
902 pxy += SHR32(MULT16_16(x,y),2)(((opus_val32)(x)*(opus_val32)(y)));
903 pyy += SHR32(MULT16_16(y,y),2)(((opus_val32)(y)*(opus_val32)(y)));
904
905 xx += SHR32(pxx, shift)(pxx);
906 xy += SHR32(pxy, shift)(pxy);
907 yy += SHR32(pyy, shift)(pyy);
908 }
909#ifndef FIXED_POINT
910 if (!(xx < 1e9f) || celt_isnan(xx)((xx)!=(xx)) || !(yy < 1e9f) || celt_isnan(yy)((yy)!=(yy)))
911 {
912 xy = xx = yy = 0;
913 }
914#endif
915 mem->XX += MULT16_32_Q15(short_alpha, xx-mem->XX)((short_alpha)*(xx-mem->XX));
916 /*mem->XY += MULT16_32_Q15(short_alpha, xy-mem->XY);*/
917 /* Rewritten to avoid overflows on abrupt sign change. */
918 mem->XY = MULT16_32_Q15(Q15ONE - short_alpha, mem->XY)((1.0f - short_alpha)*(mem->XY)) + MULT16_32_Q15(short_alpha, xy)((short_alpha)*(xy));
919 mem->YY += MULT16_32_Q15(short_alpha, yy-mem->YY)((short_alpha)*(yy-mem->YY));
920 mem->XX = MAX32(0, mem->XX)((0) > (mem->XX) ? (0) : (mem->XX));
921 mem->XY = MAX32(0, mem->XY)((0) > (mem->XY) ? (0) : (mem->XY));
922 mem->YY = MAX32(0, mem->YY)((0) > (mem->YY) ? (0) : (mem->YY));
923 if (MAX32(mem->XX, mem->YY)((mem->XX) > (mem->YY) ? (mem->XX) : (mem->YY)
)
>QCONST16(8e-4f, 18)(8e-4f))
924 {
925 opus_val16 corr;
926 opus_val16 ldiff;
927 opus_val16 width;
928 sqrt_xx = celt_sqrt(mem->XX)((float)sqrt(mem->XX));
929 sqrt_yy = celt_sqrt(mem->YY)((float)sqrt(mem->YY));
930 qrrt_xx = celt_sqrt(sqrt_xx)((float)sqrt(sqrt_xx));
931 qrrt_yy = celt_sqrt(sqrt_yy)((float)sqrt(sqrt_yy));
932 /* Inter-channel correlation */
933 mem->XY = MIN32(mem->XY, sqrt_xx*sqrt_yy)((mem->XY) < (sqrt_xx*sqrt_yy) ? (mem->XY) : (sqrt_xx
*sqrt_yy))
;
934 corr = SHR32(frac_div32(mem->XY,EPSILON+MULT16_16(sqrt_xx,sqrt_yy)),16)(((float)(mem->XY)/(1e-15f +((opus_val32)(sqrt_xx)*(opus_val32
)(sqrt_yy)))))
;
935 /* Approximate loudness difference */
936 ldiff = MULT16_16(Q15ONE, ABS16(qrrt_xx-qrrt_yy))((opus_val32)(1.0f)*(opus_val32)(((float)fabs(qrrt_xx-qrrt_yy
))))
/(EPSILON1e-15f+qrrt_xx+qrrt_yy);
937 width = MULT16_16_Q15(MIN16(Q15ONE, celt_sqrt(QCONST32(1.f,30)-MULT16_16(corr,corr))), ldiff)((((1.0f) < (((float)sqrt((1.f)-((opus_val32)(corr)*(opus_val32
)(corr))))) ? (1.0f) : (((float)sqrt((1.f)-((opus_val32)(corr
)*(opus_val32)(corr)))))))*(ldiff))
;
938 /* Smoothing over one second */
939 mem->smoothed_width += (width-mem->smoothed_width)/frame_rate;
940 /* Peak follower */
941 mem->max_follower = MAX16(mem->max_follower-QCONST16(.02f,15)/frame_rate, mem->smoothed_width)((mem->max_follower-(.02f)/frame_rate) > (mem->smoothed_width
) ? (mem->max_follower-(.02f)/frame_rate) : (mem->smoothed_width
))
;
942 }
943 /*printf("%f %f %f %f %f ", corr/(float)Q15ONE, ldiff/(float)Q15ONE, width/(float)Q15ONE, mem->smoothed_width/(float)Q15ONE, mem->max_follower/(float)Q15ONE);*/
944 return EXTRACT16(MIN32(Q15ONE, MULT16_16(20, mem->max_follower)))(((1.0f) < (((opus_val32)(20)*(opus_val32)(mem->max_follower
))) ? (1.0f) : (((opus_val32)(20)*(opus_val32)(mem->max_follower
)))))
;
945}
946
947static int decide_fec(int useInBandFEC, int PacketLoss_perc, int last_fec, int mode, int *bandwidth, opus_int32 rate)
948{
949 int orig_bandwidth;
950 if (!useInBandFEC || PacketLoss_perc == 0 || mode == MODE_CELT_ONLY1002)
951 return 0;
952 orig_bandwidth = *bandwidth;
953 for (;;)
954 {
955 opus_int32 hysteresis;
956 opus_int32 LBRR_rate_thres_bps;
957 /* Compute threshold for using FEC at the current bandwidth setting */
958 LBRR_rate_thres_bps = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND1101)];
959 hysteresis = fec_thresholds[2*(*bandwidth - OPUS_BANDWIDTH_NARROWBAND1101) + 1];
960 if (last_fec == 1) LBRR_rate_thres_bps -= hysteresis;
961 if (last_fec == 0) LBRR_rate_thres_bps += hysteresis;
962 LBRR_rate_thres_bps = silk_SMULWB( silk_MUL( LBRR_rate_thres_bps,((opus_int32)(((((LBRR_rate_thres_bps) * (125 - (((PacketLoss_perc
) < (25)) ? (PacketLoss_perc) : (25))))) * (opus_int64)((opus_int16
)(((opus_int32)((0.01) * ((opus_int64)1 << (16)) + 0.5)
)))) >> 16))
963 125 - silk_min( PacketLoss_perc, 25 ) ), SILK_FIX_CONST( 0.01, 16 ) )((opus_int32)(((((LBRR_rate_thres_bps) * (125 - (((PacketLoss_perc
) < (25)) ? (PacketLoss_perc) : (25))))) * (opus_int64)((opus_int16
)(((opus_int32)((0.01) * ((opus_int64)1 << (16)) + 0.5)
)))) >> 16))
;
964 /* If loss <= 5%, we look at whether we have enough rate to enable FEC.
965 If loss > 5%, we decrease the bandwidth until we can enable FEC. */
966 if (rate > LBRR_rate_thres_bps)
967 return 1;
968 else if (PacketLoss_perc <= 5)
969 return 0;
970 else if (*bandwidth > OPUS_BANDWIDTH_NARROWBAND1101)
971 (*bandwidth)--;
972 else
973 break;
974 }
975 /* Couldn't find any bandwidth to enable FEC, keep original bandwidth. */
976 *bandwidth = orig_bandwidth;
977 return 0;
978}
979
980static int compute_silk_rate_for_hybrid(int rate, int bandwidth, int frame20ms, int vbr, int fec, int channels) {
981 int entry;
982 int i;
983 int N;
984 int silk_rate;
985 static const int rate_table[][5] = {
986 /* |total| |-------- SILK------------|
987 |-- No FEC -| |--- FEC ---|
988 10ms 20ms 10ms 20ms */
989 { 0, 0, 0, 0, 0},
990 {12000, 10000, 10000, 11000, 11000},
991 {16000, 13500, 13500, 15000, 15000},
992 {20000, 16000, 16000, 18000, 18000},
993 {24000, 18000, 18000, 21000, 21000},
994 {32000, 22000, 22000, 28000, 28000},
995 {64000, 38000, 38000, 50000, 50000}
996 };
997 /* Do the allocation per-channel. */
998 rate /= channels;
999 entry = 1 + frame20ms + 2*fec;
1000 N = sizeof(rate_table)/sizeof(rate_table[0]);
1001 for (i=1;i<N;i++)
1002 {
1003 if (rate_table[i][0] > rate) break;
1004 }
1005 if (i == N)
1006 {
1007 silk_rate = rate_table[i-1][entry];
1008 /* For now, just give 50% of the extra bits to SILK. */
1009 silk_rate += (rate-rate_table[i-1][0])/2;
1010 } else {
1011 opus_int32 lo, hi, x0, x1;
1012 lo = rate_table[i-1][entry];
1013 hi = rate_table[i][entry];
1014 x0 = rate_table[i-1][0];
1015 x1 = rate_table[i][0];
1016 silk_rate = (lo*(x1-rate) + hi*(rate-x0))/(x1-x0);
1017 }
1018 if (!vbr)
1019 {
1020 /* Tiny boost to SILK for CBR. We should probably tune this better. */
1021 silk_rate += 100;
1022 }
1023 if (bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND1104)
1024 silk_rate += 300;
1025 silk_rate *= channels;
1026 /* Small adjustment for stereo (calibrated for 32 kb/s, haven't tried other bitrates). */
1027 if (channels == 2 && rate >= 12000)
1028 silk_rate -= 1000;
1029 return silk_rate;
1030}
1031
1032/* Returns the equivalent bitrate corresponding to 20 ms frames,
1033 complexity 10 VBR operation. */
1034static opus_int32 compute_equiv_rate(opus_int32 bitrate, int channels,
1035 int frame_rate, int vbr, int mode, int complexity, int loss)
1036{
1037 opus_int32 equiv;
1038 equiv = bitrate;
1039 /* Take into account overhead from smaller frames. */
1040 if (frame_rate > 50)
1041 equiv -= (40*channels+20)*(frame_rate - 50);
1042 /* CBR is about a 8% penalty for both SILK and CELT. */
1043 if (!vbr)
1044 equiv -= equiv/12;
1045 /* Complexity makes about 10% difference (from 0 to 10) in general. */
1046 equiv = equiv * (90+complexity)/100;
1047 if (mode == MODE_SILK_ONLY1000 || mode == MODE_HYBRID1001)
1048 {
1049 /* SILK complexity 0-1 uses the non-delayed-decision NSQ, which
1050 costs about 20%. */
1051 if (complexity<2)
1052 equiv = equiv*4/5;
1053 equiv -= equiv*loss/(6*loss + 10);
1054 } else if (mode == MODE_CELT_ONLY1002) {
1055 /* CELT complexity 0-4 doesn't have the pitch filter, which costs
1056 about 10%. */
1057 if (complexity<5)
1058 equiv = equiv*9/10;
1059 } else {
1060 /* Mode not known yet */
1061 /* Half the SILK loss*/
1062 equiv -= equiv*loss/(12*loss + 20);
1063 }
1064 return equiv;
1065}
1066
1067int is_digital_silence(const opus_res* pcm, int frame_size, int channels, int lsb_depth)
1068{
1069 int silence = 0;
1070 opus_val32 sample_max = 0;
1071#ifdef MLP_TRAINING
1072 return 0;
1073#endif
1074 sample_max = celt_maxabs_rescelt_maxabs16(pcm, frame_size*channels);
1075
1076#ifdef FIXED_POINT
1077 silence = (sample_max == 0);
1078 (void)lsb_depth;
1079#else
1080 silence = (sample_max <= (opus_val16) 1 / (1 << lsb_depth));
1081#endif
1082
1083 return silence;
1084}
1085
1086#ifdef FIXED_POINT
1087static opus_val32 compute_frame_energy(const opus_res *pcm, int frame_size, int channels, int arch)
1088{
1089 int i;
1090 opus_val32 sample_max;
1091 int max_shift;
1092 int shift;
1093 opus_val32 energy = 0;
1094 int len = frame_size*channels;
1095 (void)arch;
1096 /* Max amplitude in the signal */
1097 sample_max = RES2INT16(celt_maxabs_res(pcm, len))FLOAT2INT16(celt_maxabs16(pcm, len));
1098
1099 /* Compute the right shift required in the MAC to avoid an overflow */
1100 max_shift = celt_ilog2(len);
1101 shift = IMAX(0, (celt_ilog2(1+sample_max) << 1) + max_shift - 28)((0) > ((celt_ilog2(1+sample_max) << 1) + max_shift -
28) ? (0) : ((celt_ilog2(1+sample_max) << 1) + max_shift
- 28))
;
1102
1103 /* Compute the energy */
1104 for (i=0; i<len; i++)
1105 energy += SHR32(MULT16_16(RES2INT16(pcm[i]), RES2INT16(pcm[i])), shift)(((opus_val32)(FLOAT2INT16(pcm[i]))*(opus_val32)(FLOAT2INT16(
pcm[i]))))
;
1106
1107 /* Normalize energy by the frame size and left-shift back to the original position */
1108 energy /= len;
1109 energy = SHL32(energy, shift)(energy);
1110
1111 return energy;
1112}
1113#else
1114static opus_val32 compute_frame_energy(const opus_val16 *pcm, int frame_size, int channels, int arch)
1115{
1116 int len = frame_size*channels;
1117 return celt_inner_prod(pcm, pcm, len, arch)((*CELT_INNER_PROD_IMPL[(arch) & 7])(pcm, pcm, len))/len;
1118}
1119#endif
1120
1121/* Decides if DTX should be turned on (=1) or off (=0) */
1122static int decide_dtx_mode(opus_intint activity, /* indicates if this frame contains speech/music */
1123 int *nb_no_activity_ms_Q1, /* number of consecutive milliseconds with no activity, in Q1 */
1124 int frame_size_ms_Q1 /* number of milliseconds in this update, in Q1 */
1125 )
1126
1127{
1128 if (!activity)
1129 {
1130 /* The number of consecutive DTX frames should be within the allowed bounds.
1131 Note that the allowed bound is defined in the SILK headers and assumes 20 ms
1132 frames. As this function can be called with any frame length, a conversion to
1133 milliseconds is done before the comparisons. */
1134 (*nb_no_activity_ms_Q1) += frame_size_ms_Q1;
1135 if (*nb_no_activity_ms_Q1 > NB_SPEECH_FRAMES_BEFORE_DTX10*20*2)
1136 {
1137 if (*nb_no_activity_ms_Q1 <= (NB_SPEECH_FRAMES_BEFORE_DTX10 + MAX_CONSECUTIVE_DTX20)*20*2)
1138 /* Valid frame for DTX! */
1139 return 1;
1140 else
1141 (*nb_no_activity_ms_Q1) = NB_SPEECH_FRAMES_BEFORE_DTX10*20*2;
1142 }
1143 } else
1144 (*nb_no_activity_ms_Q1) = 0;
1145
1146 return 0;
1147}
1148
1149static int compute_redundancy_bytes(opus_int32 max_data_bytes, opus_int32 bitrate_bps, int frame_rate, int channels)
1150{
1151 int redundancy_bytes_cap;
1152 int redundancy_bytes;
1153 opus_int32 redundancy_rate;
1154 int base_bits;
1155 opus_int32 available_bits;
1156 base_bits = (40*channels+20);
1157
1158 /* Equivalent rate for 5 ms frames. */
1159 redundancy_rate = bitrate_bps + base_bits*(200 - frame_rate);
1160 /* For VBR, further increase the bitrate if we can afford it. It's pretty short
1161 and we'll avoid artefacts. */
1162 redundancy_rate = 3*redundancy_rate/2;
1163 redundancy_bytes = redundancy_rate/1600;
1164
1165 /* Compute the max rate we can use given CBR or VBR with cap. */
1166 available_bits = max_data_bytes*8 - 2*base_bits;
1167 redundancy_bytes_cap = (available_bits*240/(240+48000/frame_rate) + base_bits)/8;
1168 redundancy_bytes = IMIN(redundancy_bytes, redundancy_bytes_cap)((redundancy_bytes) < (redundancy_bytes_cap) ? (redundancy_bytes
) : (redundancy_bytes_cap))
;
1169 /* It we can't get enough bits for redundancy to be worth it, rely on the decoder PLC. */
1170 if (redundancy_bytes > 4 + 8*channels)
1171 redundancy_bytes = IMIN(257, redundancy_bytes)((257) < (redundancy_bytes) ? (257) : (redundancy_bytes));
1172 else
1173 redundancy_bytes = 0;
1174 return redundancy_bytes;
1175}
1176
1177static opus_int32 opus_encode_frame_native(OpusEncoder *st, const opus_res *pcm, int frame_size,
1178 unsigned char *data, opus_int32 max_data_bytes,
1179 int float_api, int first_frame,
1180#ifdef ENABLE_DRED
1181 opus_int32 dred_bitrate_bps,
1182#endif
1183#ifndef DISABLE_FLOAT_API
1184 AnalysisInfo *analysis_info,
1185#endif
1186 int is_silence, int redundancy, int celt_to_silk, int prefill,
1187 opus_int32 equiv_rate, int to_celt);
1188
1189opus_int32 opus_encode_native(OpusEncoder *st, const opus_res *pcm, int frame_size,
1190 unsigned char *data, opus_int32 out_data_bytes, int lsb_depth,
1191 const void *analysis_pcm, opus_int32 analysis_size, int c1, int c2,
1192 int analysis_channels, downmix_func downmix, int float_api)
1193{
1194 void *silk_enc=NULL((void*)0);
1195 CELTEncoderOpusCustomEncoder *celt_enc=NULL((void*)0);
1196 int i;
1197 int ret=0;
1198 int prefill=0;
1199 int redundancy = 0;
1200 int celt_to_silk = 0;
1201 int to_celt = 0;
1202 int voice_est; /* Probability of voice in Q7 */
1203 opus_int32 equiv_rate;
1204 int frame_rate;
1205 opus_int32 max_rate; /* Max bitrate we're allowed to use */
1206 int curr_bandwidth;
1207 opus_int32 max_data_bytes; /* Max number of bytes we're allowed to use */
1208 opus_int32 cbr_bytes=-1;
1209 opus_val16 stereo_width;
1210 const CELTModeOpusCustomMode *celt_mode=NULL((void*)0);
1211 int packet_size_cap = 1276;
1212#ifndef DISABLE_FLOAT_API
1213 AnalysisInfo analysis_info;
1214 int analysis_read_pos_bak=-1;
1215 int analysis_read_subframe_bak=-1;
1216#endif
1217 int is_silence = 0;
1218#ifdef ENABLE_DRED
1219 opus_int32 dred_bitrate_bps;
1220#endif
1221 ALLOC_STACK;
1222
1223#ifdef ENABLE_QEXT
1224 if (st->enable_qext) packet_size_cap = QEXT_PACKET_SIZE_CAP;
1225#endif
1226
1227 /* Just avoid insane packet sizes here, but the real bounds are applied later on. */
1228 max_data_bytes = IMIN(packet_size_cap*6, out_data_bytes)((packet_size_cap*6) < (out_data_bytes) ? (packet_size_cap
*6) : (out_data_bytes))
;
1229
1230 st->rangeFinal = 0;
1231 if (frame_size <= 0 || max_data_bytes <= 0)
1232 {
1233 RESTORE_STACK;
1234 return OPUS_BAD_ARG-1;
1235 }
1236
1237 /* Cannot encode 100 ms in 1 byte */
1238 if (max_data_bytes==1 && st->Fs==(frame_size*10))
1239 {
1240 RESTORE_STACK;
1241 return OPUS_BUFFER_TOO_SMALL-2;
1242 }
1243
1244 if (st->application != OPUS_APPLICATION_RESTRICTED_CELT2053)
1245 silk_enc = (char*)st+st->silk_enc_offset;
1246 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
1247 celt_enc = (CELTEncoderOpusCustomEncoder*)((char*)st+st->celt_enc_offset);
1248
1249 lsb_depth = IMIN(lsb_depth, st->lsb_depth)((lsb_depth) < (st->lsb_depth) ? (lsb_depth) : (st->
lsb_depth))
;
1250
1251 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
1252 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode)10015, ((&celt_mode) + ((&celt_mode) - (const OpusCustomMode
**)(&celt_mode)))
);
1253 is_silence = is_digital_silence(pcm, frame_size, st->channels, lsb_depth);
1254#ifndef DISABLE_FLOAT_API
1255 analysis_info.valid = 0;
1256#ifdef FIXED_POINT
1257 if (st->silk_mode.complexity >= 10 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
1258#else
1259 if (st->silk_mode.complexity >= 7 && st->Fs>=16000 && st->Fs<=48000 && st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
1260#endif
1261 {
1262 analysis_read_pos_bak = st->analysis.read_pos;
1263 analysis_read_subframe_bak = st->analysis.read_subframe;
1264 run_analysis(&st->analysis, celt_mode, analysis_pcm, analysis_size, frame_size,
1265 c1, c2, analysis_channels, st->Fs,
1266 lsb_depth, downmix, &analysis_info);
1267
1268 } else if (st->analysis.initialized) {
1269 tonality_analysis_reset(&st->analysis);
1270 }
1271#else
1272 (void)analysis_pcm;
1273 (void)analysis_size;
1274 (void)c1;
1275 (void)c2;
1276 (void)analysis_channels;
1277 (void)downmix;
1278#endif
1279
1280 /* Reset voice_ratio if this frame is not silent or if analysis is disabled.
1281 * Otherwise, preserve voice_ratio from the last non-silent frame */
1282 if (!is_silence)
1283 st->voice_ratio = -1;
1284#ifndef DISABLE_FLOAT_API
1285 st->detected_bandwidth = 0;
1286 if (analysis_info.valid)
1287 {
1288 int analysis_bandwidth;
1289 if (st->signal_type == OPUS_AUTO-1000)
1290 {
1291 float prob;
1292 if (st->prev_mode == 0)
1293 prob = analysis_info.music_prob;
1294 else if (st->prev_mode == MODE_CELT_ONLY1002)
1295 prob = analysis_info.music_prob_max;
1296 else
1297 prob = analysis_info.music_prob_min;
1298 st->voice_ratio = (int)floor(.5+100*(1-prob));
1299 }
1300
1301 analysis_bandwidth = analysis_info.bandwidth;
1302 if (analysis_bandwidth<=12)
1303 st->detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND1101;
1304 else if (analysis_bandwidth<=14)
1305 st->detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND1102;
1306 else if (analysis_bandwidth<=16)
1307 st->detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND1103;
1308 else if (analysis_bandwidth<=18)
1309 st->detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND1104;
1310 else
1311 st->detected_bandwidth = OPUS_BANDWIDTH_FULLBAND1105;
1312 }
1313#else
1314 st->voice_ratio = -1;
1315#endif
1316
1317 /* Track the peak signal energy */
1318#ifndef DISABLE_FLOAT_API
1319 if (!analysis_info.valid || analysis_info.activity_probability > DTX_ACTIVITY_THRESHOLD0.1f)
1320#endif
1321 {
1322 if (!is_silence)
1323 {
1324 st->peak_signal_energy = MAX32(MULT16_32_Q15(QCONST16(0.999f, 15), st->peak_signal_energy),(((((0.999f))*(st->peak_signal_energy))) > (compute_frame_energy
(pcm, frame_size, st->channels, st->arch)) ? ((((0.999f
))*(st->peak_signal_energy))) : (compute_frame_energy(pcm,
frame_size, st->channels, st->arch)))
1325 compute_frame_energy(pcm, frame_size, st->channels, st->arch))(((((0.999f))*(st->peak_signal_energy))) > (compute_frame_energy
(pcm, frame_size, st->channels, st->arch)) ? ((((0.999f
))*(st->peak_signal_energy))) : (compute_frame_energy(pcm,
frame_size, st->channels, st->arch)))
;
1326 }
1327 }
1328 if (st->channels==2 && st->force_channels!=1)
1329 stereo_width = compute_stereo_width(pcm, frame_size, st->Fs, &st->width_mem);
1330 else
1331 stereo_width = 0;
1332 st->bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
1333
1334 frame_rate = st->Fs/frame_size;
1335 if (!st->use_vbr)
1336 {
1337 cbr_bytes = IMIN((bitrate_to_bits(st->bitrate_bps, st->Fs, frame_size)+4)/8, max_data_bytes)(((bitrate_to_bits(st->bitrate_bps, st->Fs, frame_size)
+4)/8) < (max_data_bytes) ? ((bitrate_to_bits(st->bitrate_bps
, st->Fs, frame_size)+4)/8) : (max_data_bytes))
;
1338 st->bitrate_bps = bits_to_bitrate(cbr_bytes*8, st->Fs, frame_size);
1339 /* Make sure we provide at least one byte to avoid failing. */
1340 max_data_bytes = IMAX(1, cbr_bytes)((1) > (cbr_bytes) ? (1) : (cbr_bytes));
1341 }
1342#ifdef ENABLE_DRED
1343 /* Allocate some of the bits to DRED if needed. */
1344 dred_bitrate_bps = compute_dred_bitrate(st, st->bitrate_bps, frame_size);
1345 st->bitrate_bps -= dred_bitrate_bps;
1346#endif
1347 if (max_data_bytes<3 || st->bitrate_bps < 3*frame_rate*8
1348 || (frame_rate<50 && (max_data_bytes*(opus_int32)frame_rate<300 || st->bitrate_bps < 2400)))
1349 {
1350 /*If the space is too low to do something useful, emit 'PLC' frames.*/
1351 int tocmode = st->mode;
1352 int bw = st->bandwidth == 0 ? OPUS_BANDWIDTH_NARROWBAND1101 : st->bandwidth;
1353 int packet_code = 0;
1354 int num_multiframes = 0;
1355
1356 if (tocmode==0)
1357 tocmode = MODE_SILK_ONLY1000;
1358 if (frame_rate>100)
1359 tocmode = MODE_CELT_ONLY1002;
1360 /* 40 ms -> 2 x 20 ms if in CELT_ONLY or HYBRID mode */
1361 if (frame_rate==25 && tocmode!=MODE_SILK_ONLY1000)
1362 {
1363 frame_rate = 50;
1364 packet_code = 1;
1365 }
1366
1367 /* >= 60 ms frames */
1368 if (frame_rate<=16)
1369 {
1370 /* 1 x 60 ms, 2 x 40 ms, 2 x 60 ms */
1371 if (out_data_bytes==1 || (tocmode==MODE_SILK_ONLY1000 && frame_rate!=10))
1372 {
1373 tocmode = MODE_SILK_ONLY1000;
1374
1375 packet_code = frame_rate <= 12;
1376 frame_rate = frame_rate == 12 ? 25 : 16;
1377 }
1378 else
1379 {
1380 num_multiframes = 50/frame_rate;
1381 frame_rate = 50;
1382 packet_code = 3;
1383 }
1384 }
1385
1386 if(tocmode==MODE_SILK_ONLY1000&&bw>OPUS_BANDWIDTH_WIDEBAND1103)
1387 bw=OPUS_BANDWIDTH_WIDEBAND1103;
1388 else if (tocmode==MODE_CELT_ONLY1002&&bw==OPUS_BANDWIDTH_MEDIUMBAND1102)
1389 bw=OPUS_BANDWIDTH_NARROWBAND1101;
1390 else if (tocmode==MODE_HYBRID1001&&bw<=OPUS_BANDWIDTH_SUPERWIDEBAND1104)
1391 bw=OPUS_BANDWIDTH_SUPERWIDEBAND1104;
1392
1393 data[0] = gen_toc(tocmode, frame_rate, bw, st->stream_channels);
1394 data[0] |= packet_code;
1395
1396 ret = packet_code <= 1 ? 1 : 2;
1397
1398 max_data_bytes = IMAX(max_data_bytes, ret)((max_data_bytes) > (ret) ? (max_data_bytes) : (ret));
1399
1400 if (packet_code==3)
1401 data[1] = num_multiframes;
1402
1403 if (!st->use_vbr)
1404 {
1405 ret = opus_packet_pad(data, ret, max_data_bytes);
1406 if (ret == OPUS_OK0)
1407 ret = max_data_bytes;
1408 else
1409 ret = OPUS_INTERNAL_ERROR-3;
1410 }
1411 RESTORE_STACK;
1412 return ret;
1413 }
1414 max_rate = bits_to_bitrate(max_data_bytes*8, st->Fs, frame_size);
1415
1416 /* Equivalent 20-ms rate for mode/channel/bandwidth decisions */
1417 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->channels, st->Fs/frame_size,
1418 st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1419
1420 if (st->signal_type == OPUS_SIGNAL_VOICE3001)
1421 voice_est = 127;
1422 else if (st->signal_type == OPUS_SIGNAL_MUSIC3002)
1423 voice_est = 0;
1424 else if (st->voice_ratio >= 0)
1425 {
1426 voice_est = st->voice_ratio*327>>8;
1427 /* For AUDIO, never be more than 90% confident of having speech */
1428 if (st->application == OPUS_APPLICATION_AUDIO2049)
1429 voice_est = IMIN(voice_est, 115)((voice_est) < (115) ? (voice_est) : (115));
1430 } else if (st->application == OPUS_APPLICATION_VOIP2048)
1431 voice_est = 115;
1432 else
1433 voice_est = 48;
1434
1435 if (st->force_channels!=OPUS_AUTO-1000 && st->channels == 2)
1436 {
1437 st->stream_channels = st->force_channels;
1438 } else {
1439#ifdef FUZZING
1440 (void)stereo_music_threshold;
1441 (void)stereo_voice_threshold;
1442 /* Random mono/stereo decision */
1443 if (st->channels == 2 && (rand()&0x1F)==0)
1444 st->stream_channels = 3-st->stream_channels;
1445#else
1446 /* Rate-dependent mono-stereo decision */
1447 if (st->channels == 2)
1448 {
1449 opus_int32 stereo_threshold;
1450 stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14);
1451 if (st->stream_channels == 2)
1452 stereo_threshold -= 1000;
1453 else
1454 stereo_threshold += 1000;
1455 st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
1456 } else {
1457 st->stream_channels = st->channels;
1458 }
1459#endif
1460 }
1461 /* Update equivalent rate for channels decision. */
1462 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1463 st->use_vbr, 0, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1464
1465 /* Allow SILK DTX if DTX is enabled but the generalized DTX cannot be used,
1466 e.g. because of the complexity setting or sample rate. */
1467#ifndef DISABLE_FLOAT_API
1468 st->silk_mode.useDTX = st->use_dtx && !(analysis_info.valid || is_silence);
1469#else
1470 st->silk_mode.useDTX = st->use_dtx && !is_silence;
1471#endif
1472
1473 /* Mode selection depending on application and signal type */
1474 if (st->application == OPUS_APPLICATION_RESTRICTED_SILK2052)
1475 {
1476 st->mode = MODE_SILK_ONLY1000;
1477 } else if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY2051 || st->application == OPUS_APPLICATION_RESTRICTED_CELT2053)
1478 {
1479 st->mode = MODE_CELT_ONLY1002;
1480 } else if (st->user_forced_mode == OPUS_AUTO-1000)
1481 {
1482#ifdef FUZZING
1483 (void)stereo_width;
1484 (void)mode_thresholds;
1485 /* Random mode switching */
1486 if ((rand()&0xF)==0)
1487 {
1488 if ((rand()&0x1)==0)
1489 st->mode = MODE_CELT_ONLY1002;
1490 else
1491 st->mode = MODE_SILK_ONLY1000;
1492 } else {
1493 if (st->prev_mode==MODE_CELT_ONLY1002)
1494 st->mode = MODE_CELT_ONLY1002;
1495 else
1496 st->mode = MODE_SILK_ONLY1000;
1497 }
1498#else
1499 opus_int32 mode_voice, mode_music;
1500 opus_int32 threshold;
1501
1502 /* Interpolate based on stereo width */
1503 mode_voice = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[0][0])((1.0f -stereo_width)*(mode_thresholds[0][0]))
1504 + MULT16_32_Q15(stereo_width,mode_thresholds[1][0])((stereo_width)*(mode_thresholds[1][0])));
1505 mode_music = (opus_int32)(MULT16_32_Q15(Q15ONE-stereo_width,mode_thresholds[1][1])((1.0f -stereo_width)*(mode_thresholds[1][1]))
1506 + MULT16_32_Q15(stereo_width,mode_thresholds[1][1])((stereo_width)*(mode_thresholds[1][1])));
1507 /* Interpolate based on speech/music probability */
1508 threshold = mode_music + ((voice_est*voice_est*(mode_voice-mode_music))>>14);
1509 /* Bias towards SILK for VoIP because of some useful features */
1510 if (st->application == OPUS_APPLICATION_VOIP2048)
1511 threshold += 8000;
1512
1513 /*printf("%f %d\n", stereo_width/(float)Q15ONE, threshold);*/
1514 /* Hysteresis */
1515 if (st->prev_mode == MODE_CELT_ONLY1002)
1516 threshold -= 4000;
1517 else if (st->prev_mode>0)
1518 threshold += 4000;
1519
1520 st->mode = (equiv_rate >= threshold) ? MODE_CELT_ONLY1002: MODE_SILK_ONLY1000;
1521
1522 /* When FEC is enabled and there's enough packet loss, use SILK.
1523 Unless the FEC is set to 2, in which case we don't switch to SILK if we're confident we have music. */
1524 if (st->silk_mode.useInBandFEC && st->silk_mode.packetLossPercentage > (128-voice_est)>>4 && (st->fec_config != 2 || voice_est > 25))
1525 st->mode = MODE_SILK_ONLY1000;
1526 /* When encoding voice and DTX is enabled but the generalized DTX cannot be used,
1527 use SILK in order to make use of its DTX. */
1528 if (st->silk_mode.useDTX && voice_est > 100)
1529 st->mode = MODE_SILK_ONLY1000;
1530#endif
1531
1532 /* If max_data_bytes represents less than 6 kb/s, switch to CELT-only mode */
1533 if (max_data_bytes < bitrate_to_bits(frame_rate > 50 ? 9000 : 6000, st->Fs, frame_size)/8)
1534 st->mode = MODE_CELT_ONLY1002;
1535 } else {
1536 st->mode = st->user_forced_mode;
1537 }
1538
1539 /* Override the chosen mode to make sure we meet the requested frame size */
1540 if (st->mode != MODE_CELT_ONLY1002 && frame_size < st->Fs/100)
1541 {
1542 celt_assert(st->application != OPUS_APPLICATION_RESTRICTED_SILK){if (!(st->application != 2052)) {celt_fatal("assertion failed: "
"st->application != OPUS_APPLICATION_RESTRICTED_SILK", "./../../../media/libopus/src/opus_encoder.c"
, 1542);}}
;
1543 st->mode = MODE_CELT_ONLY1002;
1544 }
1545 if (st->lfe && st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
1546 st->mode = MODE_CELT_ONLY1002;
1547
1548 if (st->prev_mode > 0 &&
1549 ((st->mode != MODE_CELT_ONLY1002 && st->prev_mode == MODE_CELT_ONLY1002) ||
1550 (st->mode == MODE_CELT_ONLY1002 && st->prev_mode != MODE_CELT_ONLY1002)))
1551 {
1552 redundancy = 1;
1553 celt_to_silk = (st->mode != MODE_CELT_ONLY1002);
1554 if (!celt_to_silk)
1555 {
1556 /* Switch to SILK/hybrid if frame size is 10 ms or more*/
1557 if (frame_size >= st->Fs/100)
1558 {
1559 st->mode = st->prev_mode;
1560 to_celt = 1;
1561 } else {
1562 redundancy=0;
1563 }
1564 }
1565 }
1566
1567 /* When encoding multiframes, we can ask for a switch to CELT only in the last frame. This switch
1568 * is processed above as the requested mode shouldn't interrupt stereo->mono transition. */
1569 if (st->stream_channels == 1 && st->prev_channels ==2 && st->silk_mode.toMono==0
1570 && st->mode != MODE_CELT_ONLY1002 && st->prev_mode != MODE_CELT_ONLY1002)
1571 {
1572 /* Delay stereo->mono transition by two frames so that SILK can do a smooth downmix */
1573 st->silk_mode.toMono = 1;
1574 st->stream_channels = 2;
1575 } else {
1576 st->silk_mode.toMono = 0;
1577 }
1578
1579 /* Update equivalent rate with mode decision. */
1580 equiv_rate = compute_equiv_rate(st->bitrate_bps, st->stream_channels, st->Fs/frame_size,
1581 st->use_vbr, st->mode, st->silk_mode.complexity, st->silk_mode.packetLossPercentage);
1582
1583 if (st->mode != MODE_CELT_ONLY1002 && st->prev_mode == MODE_CELT_ONLY1002)
1584 {
1585 silk_EncControlStruct dummy;
1586 silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy);
1587 prefill=1;
1588 }
1589
1590 /* Automatic (rate-dependent) bandwidth selection */
1591 if (st->mode == MODE_CELT_ONLY1002 || st->first || st->silk_mode.allowBandwidthSwitch)
1592 {
1593 const opus_int32 *voice_bandwidth_thresholds, *music_bandwidth_thresholds;
1594 opus_int32 bandwidth_thresholds[8];
1595 int bandwidth = OPUS_BANDWIDTH_FULLBAND1105;
1596
1597 if (st->channels==2 && st->force_channels!=1)
1598 {
1599 voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds;
1600 music_bandwidth_thresholds = stereo_music_bandwidth_thresholds;
1601 } else {
1602 voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds;
1603 music_bandwidth_thresholds = mono_music_bandwidth_thresholds;
1604 }
1605 /* Interpolate bandwidth thresholds depending on voice estimation */
1606 for (i=0;i<8;i++)
1607 {
1608 bandwidth_thresholds[i] = music_bandwidth_thresholds[i]
1609 + ((voice_est*voice_est*(voice_bandwidth_thresholds[i]-music_bandwidth_thresholds[i]))>>14);
1610 }
1611 do {
1612 int threshold, hysteresis;
1613 threshold = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND1102)];
1614 hysteresis = bandwidth_thresholds[2*(bandwidth-OPUS_BANDWIDTH_MEDIUMBAND1102)+1];
1615 if (!st->first)
1616 {
1617 if (st->auto_bandwidth >= bandwidth)
1618 threshold -= hysteresis;
1619 else
1620 threshold += hysteresis;
1621 }
1622 if (equiv_rate >= threshold)
1623 break;
1624 } while (--bandwidth>OPUS_BANDWIDTH_NARROWBAND1101);
1625 /* We don't use mediumband anymore, except when explicitly requested or during
1626 mode transitions. */
1627 if (bandwidth == OPUS_BANDWIDTH_MEDIUMBAND1102)
1628 bandwidth = OPUS_BANDWIDTH_WIDEBAND1103;
1629 st->bandwidth = st->auto_bandwidth = bandwidth;
1630 /* Prevents any transition to SWB/FB until the SILK layer has fully
1631 switched to WB mode and turned the variable LP filter off */
1632 if (!st->first && st->mode != MODE_CELT_ONLY1002 && !st->silk_mode.inWBmodeWithoutVariableLP && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND1103)
1633 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND1103;
1634 }
1635
1636 if (st->bandwidth>st->max_bandwidth)
1637 st->bandwidth = st->max_bandwidth;
1638
1639 if (st->user_bandwidth != OPUS_AUTO-1000)
1640 st->bandwidth = st->user_bandwidth;
1641
1642 /* This prevents us from using hybrid at unsafe CBR/max rates */
1643 if (st->mode != MODE_CELT_ONLY1002 && max_rate < 15000)
1644 {
1645 st->bandwidth = IMIN(st->bandwidth, OPUS_BANDWIDTH_WIDEBAND)((st->bandwidth) < (1103) ? (st->bandwidth) : (1103)
)
;
1646 }
1647
1648 /* Prevents Opus from wasting bits on frequencies that are above
1649 the Nyquist rate of the input signal */
1650 if (st->Fs <= 24000 && st->bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND1104)
1651 st->bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND1104;
1652 if (st->Fs <= 16000 && st->bandwidth > OPUS_BANDWIDTH_WIDEBAND1103)
1653 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND1103;
1654 if (st->Fs <= 12000 && st->bandwidth > OPUS_BANDWIDTH_MEDIUMBAND1102)
1655 st->bandwidth = OPUS_BANDWIDTH_MEDIUMBAND1102;
1656 if (st->Fs <= 8000 && st->bandwidth > OPUS_BANDWIDTH_NARROWBAND1101)
1657 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND1101;
1658#ifndef DISABLE_FLOAT_API
1659 /* Use detected bandwidth to reduce the encoded bandwidth. */
1660 if (st->detected_bandwidth && st->user_bandwidth == OPUS_AUTO-1000)
1661 {
1662 int min_detected_bandwidth;
1663 /* Makes bandwidth detection more conservative just in case the detector
1664 gets it wrong when we could have coded a high bandwidth transparently.
1665 When operating in SILK/hybrid mode, we don't go below wideband to avoid
1666 more complicated switches that require redundancy. */
1667 if (equiv_rate <= 18000*st->stream_channels && st->mode == MODE_CELT_ONLY1002)
1668 min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND1101;
1669 else if (equiv_rate <= 24000*st->stream_channels && st->mode == MODE_CELT_ONLY1002)
1670 min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND1102;
1671 else if (equiv_rate <= 30000*st->stream_channels)
1672 min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND1103;
1673 else if (equiv_rate <= 44000*st->stream_channels)
1674 min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND1104;
1675 else
1676 min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND1105;
1677
1678 st->detected_bandwidth = IMAX(st->detected_bandwidth, min_detected_bandwidth)((st->detected_bandwidth) > (min_detected_bandwidth) ? (
st->detected_bandwidth) : (min_detected_bandwidth))
;
1679 st->bandwidth = IMIN(st->bandwidth, st->detected_bandwidth)((st->bandwidth) < (st->detected_bandwidth) ? (st->
bandwidth) : (st->detected_bandwidth))
;
1680 }
1681#endif
1682 st->silk_mode.LBRR_coded = decide_fec(st->silk_mode.useInBandFEC, st->silk_mode.packetLossPercentage,
1683 st->silk_mode.LBRR_coded, st->mode, &st->bandwidth, equiv_rate);
1684 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
1685 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_LSB_DEPTH(lsb_depth)4036, (((void)((lsb_depth) == (opus_int32)0)), (opus_int32)(lsb_depth
))
);
1686
1687 /* CELT mode doesn't support mediumband, use wideband instead */
1688 if (st->mode == MODE_CELT_ONLY1002 && st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND1102)
1689 st->bandwidth = OPUS_BANDWIDTH_WIDEBAND1103;
1690 if (st->lfe)
1691 st->bandwidth = OPUS_BANDWIDTH_NARROWBAND1101;
1692
1693 curr_bandwidth = st->bandwidth;
1694
1695 if (st->application == OPUS_APPLICATION_RESTRICTED_SILK2052 && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND1103)
1696 st->bandwidth = curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND1103;
1697 /* Chooses the appropriate mode for speech
1698 *NEVER* switch to/from CELT-only mode here as this will invalidate some assumptions */
1699 if (st->mode == MODE_SILK_ONLY1000 && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND1103)
1700 st->mode = MODE_HYBRID1001;
1701 if (st->mode == MODE_HYBRID1001 && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND1103)
1702 st->mode = MODE_SILK_ONLY1000;
1703 /* At very low bitrates, the hybrid rate split can leave CELT with less than
1704 its minimum bitrate (500 b/s), in which case CELT's OPUS_SET_BITRATE
1705 would fail and CELT would run uncontrolled. Use SILK-only instead. */
1706 if (st->mode == MODE_HYBRID1001)
1707 {
1708 opus_int32 silk_rate;
1709 silk_rate = compute_silk_rate_for_hybrid(st->bitrate_bps, curr_bandwidth,
1710 st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
1711 st->stream_channels);
1712 if (st->bitrate_bps - silk_rate <= 500)
1713 {
1714 st->mode = MODE_SILK_ONLY1000;
1715 st->bandwidth = curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND1103;
Although the value stored to 'curr_bandwidth' is used in the enclosing expression, the value is never actually read from 'curr_bandwidth'
1716 }
1717 }
1718
1719 /* Can't support higher than >60 ms frames, and >20 ms when in Hybrid or CELT-only modes */
1720 if ((frame_size > st->Fs/50 && (st->mode != MODE_SILK_ONLY1000)) || frame_size > 3*st->Fs/50)
1721 {
1722 int enc_frame_size;
1723 int nb_frames;
1724 VARDECL(unsigned char, tmp_data)unsigned char *tmp_data;
1725 VARDECL(OpusRepacketizer, rp)OpusRepacketizer *rp;
1726 int max_header_bytes;
1727 opus_int32 repacketize_len;
1728 opus_int32 max_len_sum;
1729 opus_int32 tot_size=0;
1730 unsigned char *curr_data;
1731 int tmp_len;
1732 int dtx_count = 0;
1733 int bak_to_mono;
1734
1735 if (st->mode == MODE_SILK_ONLY1000)
1736 {
1737 if (frame_size == 2*st->Fs/25) /* 80 ms -> 2x 40 ms */
1738 enc_frame_size = st->Fs/25;
1739 else if (frame_size == 3*st->Fs/25) /* 120 ms -> 2x 60 ms */
1740 enc_frame_size = 3*st->Fs/50;
1741 else /* 100 ms -> 5x 20 ms */
1742 enc_frame_size = st->Fs/50;
1743 }
1744 else
1745 enc_frame_size = st->Fs/50;
1746
1747 nb_frames = frame_size/enc_frame_size;
1748
1749#ifndef DISABLE_FLOAT_API
1750 if (analysis_read_pos_bak!= -1)
1751 {
1752 /* Reset analysis position to the beginning of the first frame so we
1753 can use it one frame at a time. */
1754 st->analysis.read_pos = analysis_read_pos_bak;
1755 st->analysis.read_subframe = analysis_read_subframe_bak;
1756 }
1757#endif
1758
1759 /* Worst cases:
1760 * 2 frames: Code 2 with different compressed sizes
1761 * >2 frames: Code 3 VBR */
1762 max_header_bytes = nb_frames == 2 ? 3 : (2+(nb_frames-1)*2);
1763#ifdef ENABLE_QEXT
1764 /* Cover the use of the separators that are the only thing that can get us over
1765 once we consider that we need to subtract the extension overhead in each
1766 of the individual frames. Also consider that a separator can get our padding
1767 from 254 to 255, which costs an extra length byte (at most once). */
1768 if (st->enable_qext) max_header_bytes += (nb_frames-1) + 1;
1769#endif
1770
1771 /* max_data_bytes already accounts for the packet size cap and, in CBR
1772 mode, for the bitrate, so there is no need to go back to the size of
1773 the caller's buffer here. */
1774 repacketize_len = max_data_bytes;
1775 max_len_sum = nb_frames + repacketize_len - max_header_bytes;
1776
1777 ALLOC(tmp_data, max_len_sum, unsigned char)tmp_data = ((unsigned char*)__builtin_alloca (sizeof(unsigned
char)*(max_len_sum)))
;
1778 curr_data = tmp_data;
1779 ALLOC(rp, 1, OpusRepacketizer)rp = ((OpusRepacketizer*)__builtin_alloca (sizeof(OpusRepacketizer
)*(1)))
;
1780 opus_repacketizer_init(rp);
1781
1782
1783 bak_to_mono = st->silk_mode.toMono;
1784 if (bak_to_mono)
1785 st->force_channels = 1;
1786 else
1787 st->prev_channels = st->stream_channels;
1788
1789 for (i=0;i<nb_frames;i++)
1790 {
1791 int first_frame;
1792 int frame_to_celt;
1793 int frame_redundancy;
1794 opus_int32 curr_max;
1795 /* Attempt DRED encoding until we have a non-DTX frame. In case of DTX refresh,
1796 that allows for DRED not to be in the first frame. */
1797 first_frame = (i == 0) || (i == dtx_count);
1798 st->silk_mode.toMono = 0;
1799 st->nonfinal_frame = i<(nb_frames-1);
1800
1801 /* When switching from SILK/Hybrid to CELT, only ask for a switch at the last frame */
1802 frame_to_celt = to_celt && i==nb_frames-1;
1803 frame_redundancy = redundancy && (frame_to_celt || (!to_celt && i==0));
1804
1805 curr_max = IMIN(bitrate_to_bits(st->bitrate_bps, st->Fs, enc_frame_size)/8, max_len_sum/nb_frames)((bitrate_to_bits(st->bitrate_bps, st->Fs, enc_frame_size
)/8) < (max_len_sum/nb_frames) ? (bitrate_to_bits(st->bitrate_bps
, st->Fs, enc_frame_size)/8) : (max_len_sum/nb_frames))
;
1806#ifdef ENABLE_DRED
1807 curr_max = IMIN(curr_max, (max_len_sum-bitrate_to_bits(dred_bitrate_bps, st->Fs, frame_size)/8)/nb_frames)((curr_max) < ((max_len_sum-bitrate_to_bits(dred_bitrate_bps
, st->Fs, frame_size)/8)/nb_frames) ? (curr_max) : ((max_len_sum
-bitrate_to_bits(dred_bitrate_bps, st->Fs, frame_size)/8)/
nb_frames))
;
1808 if (first_frame) curr_max += bitrate_to_bits(dred_bitrate_bps, st->Fs, frame_size)/8;
1809#endif
1810#ifdef ENABLE_QEXT
1811 /* Leave room for signaling the extension size once we repacketize. */
1812 if (st->enable_qext)
1813 curr_max -= curr_max/254;
1814#endif
1815 curr_max = IMIN(max_len_sum-tot_size, curr_max)((max_len_sum-tot_size) < (curr_max) ? (max_len_sum-tot_size
) : (curr_max))
;
1816#ifndef DISABLE_FLOAT_API
1817 if (analysis_read_pos_bak != -1) {
1818 /* Get analysis for current frame. */
1819 tonality_get_info(&st->analysis, &analysis_info, enc_frame_size);
1820 }
1821#endif
1822 is_silence = is_digital_silence(pcm+i*(st->channels*enc_frame_size), enc_frame_size, st->channels, lsb_depth);
1823
1824 tmp_len = opus_encode_frame_native(st, pcm+i*(st->channels*enc_frame_size), enc_frame_size, curr_data, curr_max, float_api, first_frame,
1825#ifdef ENABLE_DRED
1826 dred_bitrate_bps,
1827#endif
1828#ifndef DISABLE_FLOAT_API
1829 &analysis_info,
1830#endif
1831 is_silence, frame_redundancy, celt_to_silk, prefill,
1832 equiv_rate, frame_to_celt
1833 );
1834 if (tmp_len<0)
1835 {
1836 RESTORE_STACK;
1837 return OPUS_INTERNAL_ERROR-3;
1838 } else if (tmp_len==1) {
1839 dtx_count++;
1840 }
1841 ret = opus_repacketizer_cat(rp, curr_data, tmp_len);
1842
1843 if (ret<0)
1844 {
1845 RESTORE_STACK;
1846 return OPUS_INTERNAL_ERROR-3;
1847 }
1848 tot_size += tmp_len;
1849 curr_data += tmp_len;
1850 }
1851 ret = opus_repacketizer_out_range_impl(rp, 0, nb_frames, data, repacketize_len, 0, !st->use_vbr && (dtx_count != nb_frames), NULL((void*)0), 0);
1852 if (ret<0)
1853 {
1854 ret = OPUS_INTERNAL_ERROR-3;
1855 }
1856 st->silk_mode.toMono = bak_to_mono;
1857 RESTORE_STACK;
1858 return ret;
1859 } else {
1860 ret = opus_encode_frame_native(st, pcm, frame_size, data, max_data_bytes, float_api, 1,
1861#ifdef ENABLE_DRED
1862 dred_bitrate_bps,
1863#endif
1864#ifndef DISABLE_FLOAT_API
1865 &analysis_info,
1866#endif
1867 is_silence, redundancy, celt_to_silk, prefill,
1868 equiv_rate, to_celt
1869 );
1870 RESTORE_STACK;
1871 return ret;
1872 }
1873}
1874
1875static opus_int32 opus_encode_frame_native(OpusEncoder *st, const opus_res *pcm, int frame_size,
1876 unsigned char *data, opus_int32 orig_max_data_bytes,
1877 int float_api, int first_frame,
1878#ifdef ENABLE_DRED
1879 opus_int32 dred_bitrate_bps,
1880#endif
1881#ifndef DISABLE_FLOAT_API
1882 AnalysisInfo *analysis_info,
1883#endif
1884 int is_silence, int redundancy, int celt_to_silk, int prefill,
1885 opus_int32 equiv_rate, int to_celt)
1886{
1887 void *silk_enc=NULL((void*)0);
1888 CELTEncoderOpusCustomEncoder *celt_enc=NULL((void*)0);
1889 const CELTModeOpusCustomMode *celt_mode=NULL((void*)0);
1890 int i;
1891 int ret=0;
1892 int max_data_bytes;
1893 opus_int32 nBytes;
1894 ec_enc enc;
1895 int bits_target;
1896 int start_band = 0;
1897 int redundancy_bytes = 0; /* Number of bytes to use for redundancy frame */
1898 int nb_compr_bytes;
1899 opus_uint32 redundant_rng = 0;
1900 int cutoff_Hz;
1901 int hp_freq_smth1;
1902 opus_val16 HB_gain;
1903 int apply_padding;
1904 int frame_rate;
1905 int curr_bandwidth;
1906 int delay_compensation;
1907 int total_buffer;
1908 opus_intint activity = VAD_NO_DECISION-1;
1909 VARDECL(opus_res, pcm_buf)opus_res *pcm_buf;
1910 VARDECL(opus_res, tmp_prefill)opus_res *tmp_prefill;
1911 SAVE_STACK;
1912
1913 max_data_bytes = IMIN(orig_max_data_bytes, 1276)((orig_max_data_bytes) < (1276) ? (orig_max_data_bytes) : (
1276))
;
1914 st->rangeFinal = 0;
1915 if (st->application != OPUS_APPLICATION_RESTRICTED_CELT2053)
1916 silk_enc = (char*)st+st->silk_enc_offset;
1917 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
1918 {
1919 celt_enc = (CELTEncoderOpusCustomEncoder*)((char*)st+st->celt_enc_offset);
1920 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, CELT_GET_MODE(&celt_mode)10015, ((&celt_mode) + ((&celt_mode) - (const OpusCustomMode
**)(&celt_mode)))
);
1921 }
1922 curr_bandwidth = st->bandwidth;
1923 if (st->application == OPUS_APPLICATION_RESTRICTED_LOWDELAY2051 || st->application == OPUS_APPLICATION_RESTRICTED_CELT2053 || st->application == OPUS_APPLICATION_RESTRICTED_SILK2052)
1924 delay_compensation = 0;
1925 else
1926 delay_compensation = st->delay_compensation;
1927 total_buffer = delay_compensation;
1928
1929 frame_rate = st->Fs/frame_size;
1930
1931 if (is_silence)
1932 {
1933 activity = !is_silence;
1934 }
1935#ifndef DISABLE_FLOAT_API
1936 else if (analysis_info->valid) {
1937 activity = analysis_info->activity_probability >= DTX_ACTIVITY_THRESHOLD0.1f;
1938 if (!activity)
1939 {
1940 /* Mark as active if this noise frame is sufficiently loud */
1941 opus_val32 noise_energy = compute_frame_energy(pcm, frame_size, st->channels, st->arch);
1942 activity = st->peak_signal_energy < (PSEUDO_SNR_THRESHOLD316.23f * noise_energy);
1943 }
1944 }
1945#endif
1946 else if (st->mode == MODE_CELT_ONLY1002) {
1947 opus_val32 noise_energy = compute_frame_energy(pcm, frame_size, st->channels, st->arch);
1948 /* Boosting peak energy a bit because we didn't just average the active frames. */
1949 activity = st->peak_signal_energy < (QCONST16(PSEUDO_SNR_THRESHOLD, 0)(316.23f) * (opus_val64)HALF32(noise_energy)(.5f*(noise_energy)));
1950 }
1951
1952 /* For the first frame at a new SILK bandwidth */
1953 if (st->silk_bw_switch)
1954 {
1955 redundancy = 1;
1956 celt_to_silk = 1;
1957 st->silk_bw_switch = 0;
1958 /* Do a prefill without resetting the sampling rate control. */
1959 prefill=2;
1960 }
1961
1962 /* If we decided to go with CELT, make sure redundancy is off, no matter what
1963 we decided earlier. */
1964 if (st->mode == MODE_CELT_ONLY1002)
1965 redundancy = 0;
1966
1967 if (redundancy)
1968 {
1969 redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
1970 if (redundancy_bytes == 0)
1971 redundancy = 0;
1972 }
1973 if (st->application == OPUS_APPLICATION_RESTRICTED_SILK2052)
1974 {
1975 redundancy = 0;
1976 redundancy_bytes = 0;
1977 }
1978
1979 /* printf("%d %d %d %d\n", st->bitrate_bps, st->stream_channels, st->mode, curr_bandwidth); */
1980 bits_target = IMIN(8*(max_data_bytes-redundancy_bytes), bitrate_to_bits(st->bitrate_bps, st->Fs, frame_size))((8*(max_data_bytes-redundancy_bytes)) < (bitrate_to_bits(
st->bitrate_bps, st->Fs, frame_size)) ? (8*(max_data_bytes
-redundancy_bytes)) : (bitrate_to_bits(st->bitrate_bps, st
->Fs, frame_size)))
- 8;
1981
1982 data += 1;
1983
1984 ec_enc_init(&enc, data, orig_max_data_bytes-1);
1985
1986 ALLOC(pcm_buf, (total_buffer+frame_size)*st->channels, opus_res)pcm_buf = ((opus_res*)__builtin_alloca (sizeof(opus_res)*((total_buffer
+frame_size)*st->channels)))
;
1987 OPUS_COPY(pcm_buf, &st->delay_buffer[(st->encoder_buffer-total_buffer)*st->channels], total_buffer*st->channels)(memcpy((pcm_buf), (&st->delay_buffer[(st->encoder_buffer
-total_buffer)*st->channels]), (total_buffer*st->channels
)*sizeof(*(pcm_buf)) + 0*((pcm_buf)-(&st->delay_buffer
[(st->encoder_buffer-total_buffer)*st->channels])) ))
;
1988
1989 if (st->mode == MODE_CELT_ONLY1002)
1990 hp_freq_smth1 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 )((opus_int32)((opus_uint32)(silk_lin2log( 60 ))<<(8)));
1991 else
1992 hp_freq_smth1 = ((silk_encoder*)silk_enc)->state_Fxx[0].sCmn.variable_HP_smth1_Q15;
1993
1994 st->variable_HP_smth2_Q15 = silk_SMLAWB( st->variable_HP_smth2_Q15,((opus_int32)((st->variable_HP_smth2_Q15) + (((hp_freq_smth1
- st->variable_HP_smth2_Q15) * (opus_int64)((opus_int16)(
((opus_int32)((0.015f) * ((opus_int64)1 << (16)) + 0.5)
)))) >> 16)))
1995 hp_freq_smth1 - st->variable_HP_smth2_Q15, SILK_FIX_CONST( VARIABLE_HP_SMTH_COEF2, 16 ) )((opus_int32)((st->variable_HP_smth2_Q15) + (((hp_freq_smth1
- st->variable_HP_smth2_Q15) * (opus_int64)((opus_int16)(
((opus_int32)((0.015f) * ((opus_int64)1 << (16)) + 0.5)
)))) >> 16)))
;
1996
1997 /* convert from log scale to Hertz */
1998 cutoff_Hz = silk_log2lin( silk_RSHIFT( st->variable_HP_smth2_Q15, 8 )((st->variable_HP_smth2_Q15)>>(8)) );
1999
2000 if (st->application == OPUS_APPLICATION_VOIP2048)
2001 {
2002 hp_cutoff(pcm, cutoff_Hz, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs, st->arch);
2003
2004#ifdef ENABLE_OSCE_TRAINING_DATA
2005 /* write out high pass filtered clean signal*/
2006 static FILE *fout =NULL((void*)0);
2007 if (fout == NULL((void*)0))
2008 {
2009 fout = fopen("clean_hp.s16", "wb");
2010 }
2011
2012 {
2013 int idx;
2014 opus_int16 tmp;
2015 for (idx = 0; idx < frame_size; idx++)
2016 {
2017 tmp = (opus_int16) (32768 * pcm_buf[total_buffer + idx] + 0.5f);
2018 fwrite(&tmp, sizeof(tmp), 1, fout);
2019 }
2020 }
2021#endif
2022 } else {
2023#ifdef ENABLE_QEXT
2024 /* FIXME: Avoid glitching when we switch qext on/off dynamically. */
2025 if (st->enable_qext) OPUS_COPY(&pcm_buf[total_buffer*st->channels], pcm, frame_size*st->channels)(memcpy((&pcm_buf[total_buffer*st->channels]), (pcm), (
frame_size*st->channels)*sizeof(*(&pcm_buf[total_buffer
*st->channels])) + 0*((&pcm_buf[total_buffer*st->channels
])-(pcm)) ))
;
2026 else
2027#endif
2028 dc_reject(pcm, 3, &pcm_buf[total_buffer*st->channels], st->hp_mem, frame_size, st->channels, st->Fs);
2029 }
2030#ifndef FIXED_POINT
2031 if (float_api)
2032 {
2033 opus_val32 sum;
2034 sum = celt_inner_prod(&pcm_buf[total_buffer*st->channels], &pcm_buf[total_buffer*st->channels], frame_size*st->channels, st->arch)((*CELT_INNER_PROD_IMPL[(st->arch) & 7])(&pcm_buf[
total_buffer*st->channels], &pcm_buf[total_buffer*st->
channels], frame_size*st->channels))
;
2035 /* This should filter out both NaNs and ridiculous signals that could
2036 cause NaNs further down. */
2037 if (!(sum < 1e9f) || celt_isnan(sum)((sum)!=(sum)))
2038 {
2039 OPUS_CLEAR(&pcm_buf[total_buffer*st->channels], frame_size*st->channels)(memset((&pcm_buf[total_buffer*st->channels]), 0, (frame_size
*st->channels)*sizeof(*(&pcm_buf[total_buffer*st->channels
]))))
;
2040 st->hp_mem[0] = st->hp_mem[1] = st->hp_mem[2] = st->hp_mem[3] = 0;
2041 }
2042 }
2043#else
2044 (void)float_api;
2045#endif
2046
2047#ifdef ENABLE_DRED
2048 /* Compute the DRED features. Needs to be before SILK because of DTX. */
2049 if ( st->dred_duration > 0 && st->dred_encoder.loaded ) {
2050 int frame_size_400Hz;
2051 /* DRED Encoder */
2052 dred_compute_latents( &st->dred_encoder, &pcm_buf[total_buffer*st->channels], frame_size, total_buffer, st->arch );
2053 frame_size_400Hz = frame_size*400/st->Fs;
2054 OPUS_MOVE(&st->activity_mem[frame_size_400Hz], st->activity_mem, 4*DRED_MAX_FRAMES-frame_size_400Hz)(memmove((&st->activity_mem[frame_size_400Hz]), (st->
activity_mem), (4*DRED_MAX_FRAMES-frame_size_400Hz)*sizeof(*(
&st->activity_mem[frame_size_400Hz])) + 0*((&st->
activity_mem[frame_size_400Hz])-(st->activity_mem)) ))
;
2055 for (i=0;i<frame_size_400Hz;i++)
2056 st->activity_mem[i] = activity;
2057 } else {
2058 st->dred_encoder.latents_buffer_fill = 0;
2059 OPUS_CLEAR(st->activity_mem, 4*DRED_MAX_FRAMES)(memset((st->activity_mem), 0, (4*DRED_MAX_FRAMES)*sizeof(
*(st->activity_mem))))
;
2060 }
2061#endif
2062
2063 /* SILK processing */
2064 HB_gain = Q15ONE1.0f;
2065 if (st->mode != MODE_CELT_ONLY1002)
2066 {
2067 opus_int32 total_bitRate, celt_rate;
2068 const opus_res *pcm_silk;
2069
2070 /* Distribute bits between SILK and CELT */
2071 total_bitRate = bits_to_bitrate(bits_target, st->Fs, frame_size);
2072 if( st->mode == MODE_HYBRID1001 ) {
2073 /* Base rate for SILK */
2074 st->silk_mode.bitRate = compute_silk_rate_for_hybrid(total_bitRate,
2075 curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
2076 st->stream_channels);
2077 if (!st->energy_masking)
2078 {
2079 /* Increasingly attenuate high band when it gets allocated fewer bits */
2080 celt_rate = total_bitRate - st->silk_mode.bitRate;
2081 HB_gain = Q15ONE1.0f - SHR32(celt_exp2(-celt_rate * QCONST16(1.f/1024, 10)), 1)(((float)exp(0.6931471805599453094*(-celt_rate * (1.f/1024)))
))
;
2082 }
2083 } else {
2084 /* SILK gets all bits */
2085 st->silk_mode.bitRate = total_bitRate;
2086 }
2087
2088 /* Surround masking for SILK */
2089 if (st->energy_masking && st->use_vbr && !st->lfe)
2090 {
2091 opus_val32 mask_sum=0;
2092 celt_glog masking_depth;
2093 opus_int32 rate_offset;
2094 int c;
2095 int end = 17;
2096 opus_int16 srate = 16000;
2097 if (st->bandwidth == OPUS_BANDWIDTH_NARROWBAND1101)
2098 {
2099 end = 13;
2100 srate = 8000;
2101 } else if (st->bandwidth == OPUS_BANDWIDTH_MEDIUMBAND1102)
2102 {
2103 end = 15;
2104 srate = 12000;
2105 }
2106 for (c=0;c<st->channels;c++)
2107 {
2108 for(i=0;i<end;i++)
2109 {
2110 celt_glog mask;
2111 mask = MAXG(MING(st->energy_masking[21*c+i],((((st->energy_masking[21*c+i]) < ((.5f)) ? (st->energy_masking
[21*c+i]) : ((.5f)))) > (-(2.0f)) ? (((st->energy_masking
[21*c+i]) < ((.5f)) ? (st->energy_masking[21*c+i]) : ((
.5f)))) : (-(2.0f)))
2112 GCONST(.5f)), -GCONST(2.0f))((((st->energy_masking[21*c+i]) < ((.5f)) ? (st->energy_masking
[21*c+i]) : ((.5f)))) > (-(2.0f)) ? (((st->energy_masking
[21*c+i]) < ((.5f)) ? (st->energy_masking[21*c+i]) : ((
.5f)))) : (-(2.0f)))
;
2113 if (mask > 0)
2114 mask = HALF32(mask)(.5f*(mask));
2115 mask_sum += mask;
2116 }
2117 }
2118 /* Conservative rate reduction, we cut the masking in half */
2119 masking_depth = mask_sum / end*st->channels;
2120 masking_depth += GCONST(.2f)(.2f);
2121 rate_offset = (opus_int32)PSHR32(MULT16_16(srate, SHR32(masking_depth, DB_SHIFT-10)), 10)(((opus_val32)(srate)*(opus_val32)((masking_depth))));
2122 rate_offset = MAX32(rate_offset, -2*st->silk_mode.bitRate/3)((rate_offset) > (-2*st->silk_mode.bitRate/3) ? (rate_offset
) : (-2*st->silk_mode.bitRate/3))
;
2123 /* Split the rate change between the SILK and CELT part for hybrid. */
2124 if (st->bandwidth==OPUS_BANDWIDTH_SUPERWIDEBAND1104 || st->bandwidth==OPUS_BANDWIDTH_FULLBAND1105)
2125 st->silk_mode.bitRate += 3*rate_offset/5;
2126 else
2127 st->silk_mode.bitRate += rate_offset;
2128 }
2129
2130 st->silk_mode.payloadSize_ms = 1000 * frame_size / st->Fs;
2131 st->silk_mode.nChannelsAPI = st->channels;
2132 st->silk_mode.nChannelsInternal = st->stream_channels;
2133 if (curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND1101) {
2134 st->silk_mode.desiredInternalSampleRate = 8000;
2135 } else if (curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND1102) {
2136 st->silk_mode.desiredInternalSampleRate = 12000;
2137 } else {
2138 celt_assert( st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND ){if (!(st->mode == 1001 || curr_bandwidth == 1103)) {celt_fatal
("assertion failed: " "st->mode == MODE_HYBRID || curr_bandwidth == OPUS_BANDWIDTH_WIDEBAND"
, "./../../../media/libopus/src/opus_encoder.c", 2138);}}
;
2139 st->silk_mode.desiredInternalSampleRate = 16000;
2140 }
2141 if( st->mode == MODE_HYBRID1001 ) {
2142 /* Don't allow bandwidth reduction at lowest bitrates in hybrid mode */
2143 st->silk_mode.minInternalSampleRate = 16000;
2144 } else {
2145 st->silk_mode.minInternalSampleRate = 8000;
2146 }
2147
2148 st->silk_mode.maxInternalSampleRate = 16000;
2149 if (st->mode == MODE_SILK_ONLY1000)
2150 {
2151 opus_int32 effective_max_rate = bits_to_bitrate(max_data_bytes*8, st->Fs, frame_size);
2152 if (frame_rate > 50)
2153 effective_max_rate = effective_max_rate*2/3;
2154 if (effective_max_rate < 8000)
2155 {
2156 st->silk_mode.maxInternalSampleRate = 12000;
2157 st->silk_mode.desiredInternalSampleRate = IMIN(12000, st->silk_mode.desiredInternalSampleRate)((12000) < (st->silk_mode.desiredInternalSampleRate) ? (
12000) : (st->silk_mode.desiredInternalSampleRate))
;
2158 }
2159 if (effective_max_rate < 7000)
2160 {
2161 st->silk_mode.maxInternalSampleRate = 8000;
2162 st->silk_mode.desiredInternalSampleRate = IMIN(8000, st->silk_mode.desiredInternalSampleRate)((8000) < (st->silk_mode.desiredInternalSampleRate) ? (
8000) : (st->silk_mode.desiredInternalSampleRate))
;
2163 }
2164#ifdef ENABLE_QEXT
2165 /* At 96 kHz, we don't have the input resampler to do 8 or 12 kHz. */
2166 if (st->Fs==96000) st->silk_mode.maxInternalSampleRate = st->silk_mode.desiredInternalSampleRate = 16000;
2167#endif
2168 }
2169
2170 st->silk_mode.useCBR = !st->use_vbr;
2171
2172 /* Call SILK encoder for the low band */
2173
2174 /* Max bits for SILK, counting ToC, redundancy bytes, and optionally redundancy. */
2175 st->silk_mode.maxBits = (max_data_bytes-1)*8;
2176 if (redundancy && redundancy_bytes >= 2)
2177 {
2178 /* Counting 1 bit for redundancy position and 20 bits for flag+size (only for hybrid). */
2179 st->silk_mode.maxBits -= redundancy_bytes*8 + 1;
2180 if (st->mode == MODE_HYBRID1001)
2181 st->silk_mode.maxBits -= 20;
2182 }
2183 if (st->silk_mode.useCBR)
2184 {
2185 /* When we're in CBR mode, but we have non-SILK data to encode, switch SILK to VBR with cap to
2186 save on complexity. Any variations will be absorbed by CELT and/or DRED and we can still
2187 produce a constant bitrate without wasting bits. */
2188#ifdef ENABLE_DRED
2189 if (st->mode == MODE_HYBRID1001 || dred_bitrate_bps > 0)
2190#else
2191 if (st->mode == MODE_HYBRID1001)
2192#endif
2193 {
2194 /* Allow SILK to steal up to 25% of the remaining bits */
2195 opus_int16 other_bits = IMAX(0, st->silk_mode.maxBits - st->silk_mode.bitRate * frame_size / st->Fs)((0) > (st->silk_mode.maxBits - st->silk_mode.bitRate
* frame_size / st->Fs) ? (0) : (st->silk_mode.maxBits -
st->silk_mode.bitRate * frame_size / st->Fs))
;
2196 st->silk_mode.maxBits = IMAX(0, st->silk_mode.maxBits - other_bits*3/4)((0) > (st->silk_mode.maxBits - other_bits*3/4) ? (0) :
(st->silk_mode.maxBits - other_bits*3/4))
;
2197 st->silk_mode.useCBR = 0;
2198 }
2199 } else {
2200 /* Constrained VBR. */
2201 if (st->mode == MODE_HYBRID1001)
2202 {
2203 /* Compute SILK bitrate corresponding to the max total bits available */
2204 opus_int32 maxBitRate = compute_silk_rate_for_hybrid(st->silk_mode.maxBits*st->Fs / frame_size,
2205 curr_bandwidth, st->Fs == 50 * frame_size, st->use_vbr, st->silk_mode.LBRR_coded,
2206 st->stream_channels);
2207 st->silk_mode.maxBits = bitrate_to_bits(maxBitRate, st->Fs, frame_size);
2208 }
2209 }
2210
2211 if (prefill && st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
2212 {
2213 opus_int32 zero=0;
2214 int prefill_offset;
2215 /* Use a smooth onset for the SILK prefill to avoid the encoder trying to encode
2216 a discontinuity. The exact location is what we need to avoid leaving any "gap"
2217 in the audio when mixing with the redundant CELT frame. Here we can afford to
2218 overwrite st->delay_buffer because the only thing that uses it before it gets
2219 rewritten is tmp_prefill[] and even then only the part after the ramp really
2220 gets used (rather than sent to the encoder and discarded) */
2221 prefill_offset = st->channels*(st->encoder_buffer-st->delay_compensation-st->Fs/400);
2222 gain_fade(st->delay_buffer+prefill_offset, st->delay_buffer+prefill_offset,
2223 0, Q15ONE1.0f, celt_mode->overlap, st->Fs/400, st->channels, celt_mode->window, st->Fs);
2224 OPUS_CLEAR(st->delay_buffer, prefill_offset)(memset((st->delay_buffer), 0, (prefill_offset)*sizeof(*(st
->delay_buffer))))
;
2225 pcm_silk = st->delay_buffer;
2226 silk_Encode( silk_enc, &st->silk_mode, pcm_silk, st->encoder_buffer, NULL((void*)0), &zero, prefill, activity );
2227 /* Prevent a second switch in the real encode call. */
2228 st->silk_mode.opusCanSwitch = 0;
2229 }
2230
2231 pcm_silk = pcm_buf+total_buffer*st->channels;
2232 ret = silk_Encode( silk_enc, &st->silk_mode, pcm_silk, frame_size, &enc, &nBytes, 0, activity );
2233 if( ret ) {
2234 /*fprintf (stderr, "SILK encode error: %d\n", ret);*/
2235 /* Handle error */
2236 RESTORE_STACK;
2237 return OPUS_INTERNAL_ERROR-3;
2238 }
2239
2240 /* Extract SILK internal bandwidth for signaling in first byte */
2241 if( st->mode == MODE_SILK_ONLY1000 ) {
2242 if( st->silk_mode.internalSampleRate == 8000 ) {
2243 curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND1101;
2244 } else if( st->silk_mode.internalSampleRate == 12000 ) {
2245 curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND1102;
2246 } else if( st->silk_mode.internalSampleRate == 16000 ) {
2247 curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND1103;
2248 }
2249 } else {
2250 celt_assert( st->silk_mode.internalSampleRate == 16000 ){if (!(st->silk_mode.internalSampleRate == 16000)) {celt_fatal
("assertion failed: " "st->silk_mode.internalSampleRate == 16000"
, "./../../../media/libopus/src/opus_encoder.c", 2250);}}
;
2251 }
2252
2253 st->silk_mode.opusCanSwitch = st->silk_mode.switchReady && !st->nonfinal_frame;
2254
2255 if (activity == VAD_NO_DECISION-1) {
2256 activity = (st->silk_mode.signalType != TYPE_NO_VOICE_ACTIVITY0);
2257#ifdef ENABLE_DRED
2258 for (i=0;i<frame_size*400/st->Fs;i++)
2259 st->activity_mem[i] = activity;
2260#endif
2261 }
2262 if (nBytes==0)
2263 {
2264 st->rangeFinal = 0;
2265 data[-1] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2266 RESTORE_STACK;
2267 return 1;
2268 }
2269
2270 /* FIXME: How do we allocate the redundancy for CBR? */
2271 if (st->silk_mode.opusCanSwitch)
2272 {
2273 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
2274 {
2275 redundancy_bytes = compute_redundancy_bytes(max_data_bytes, st->bitrate_bps, frame_rate, st->stream_channels);
2276 redundancy = (redundancy_bytes != 0);
2277 }
2278 celt_to_silk = 0;
2279 st->silk_bw_switch = 1;
2280 }
2281 }
2282
2283 /* CELT processing */
2284 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
2285 {
2286 int endband=21;
2287
2288 switch(curr_bandwidth)
2289 {
2290 case OPUS_BANDWIDTH_NARROWBAND1101:
2291 endband = 13;
2292 break;
2293 case OPUS_BANDWIDTH_MEDIUMBAND1102:
2294 case OPUS_BANDWIDTH_WIDEBAND1103:
2295 endband = 17;
2296 break;
2297 case OPUS_BANDWIDTH_SUPERWIDEBAND1104:
2298 endband = 19;
2299 break;
2300 case OPUS_BANDWIDTH_FULLBAND1105:
2301 endband = 21;
2302 break;
2303 }
2304 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, CELT_SET_END_BAND(endband)10012, (((void)((endband) == (opus_int32)0)), (opus_int32)(endband
))
);
2305 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, CELT_SET_CHANNELS(st->stream_channels)10008, (((void)((st->stream_channels) == (opus_int32)0)), (
opus_int32)(st->stream_channels))
);
2306 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX)4002, (((void)((-1) == (opus_int32)0)), (opus_int32)(-1)));
2307 }
2308 if (st->mode != MODE_SILK_ONLY1000)
2309 {
2310 opus_val32 celt_pred=2;
2311 /* We may still decide to disable prediction later */
2312 if (st->silk_mode.reducedDependency)
2313 celt_pred = 0;
2314 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, CELT_SET_PREDICTION(celt_pred)10002, (((void)((celt_pred) == (opus_int32)0)), (opus_int32)(
celt_pred))
);
2315 }
2316
2317 ALLOC(tmp_prefill, st->channels*st->Fs/400, opus_res)tmp_prefill = ((opus_res*)__builtin_alloca (sizeof(opus_res)*
(st->channels*st->Fs/400)))
;
2318 if (st->mode != MODE_SILK_ONLY1000 && st->mode != st->prev_mode && st->prev_mode > 0
2319 && st->application != OPUS_APPLICATION_RESTRICTED_CELT2053)
2320 {
2321 OPUS_COPY(tmp_prefill, &st->delay_buffer[(st->encoder_buffer-total_buffer-st->Fs/400)*st->channels], st->channels*st->Fs/400)(memcpy((tmp_prefill), (&st->delay_buffer[(st->encoder_buffer
-total_buffer-st->Fs/400)*st->channels]), (st->channels
*st->Fs/400)*sizeof(*(tmp_prefill)) + 0*((tmp_prefill)-(&
st->delay_buffer[(st->encoder_buffer-total_buffer-st->
Fs/400)*st->channels])) ))
;
2322 }
2323
2324 if (st->channels*(st->encoder_buffer-(frame_size+total_buffer)) > 0)
2325 {
2326 OPUS_MOVE(st->delay_buffer, &st->delay_buffer[st->channels*frame_size], st->channels*(st->encoder_buffer-frame_size-total_buffer))(memmove((st->delay_buffer), (&st->delay_buffer[st->
channels*frame_size]), (st->channels*(st->encoder_buffer
-frame_size-total_buffer))*sizeof(*(st->delay_buffer)) + 0
*((st->delay_buffer)-(&st->delay_buffer[st->channels
*frame_size])) ))
;
2327 OPUS_COPY(&st->delay_buffer[st->channels*(st->encoder_buffer-frame_size-total_buffer)],(memcpy((&st->delay_buffer[st->channels*(st->encoder_buffer
-frame_size-total_buffer)]), (&pcm_buf[0]), ((frame_size+
total_buffer)*st->channels)*sizeof(*(&st->delay_buffer
[st->channels*(st->encoder_buffer-frame_size-total_buffer
)])) + 0*((&st->delay_buffer[st->channels*(st->encoder_buffer
-frame_size-total_buffer)])-(&pcm_buf[0])) ))
2328 &pcm_buf[0],(memcpy((&st->delay_buffer[st->channels*(st->encoder_buffer
-frame_size-total_buffer)]), (&pcm_buf[0]), ((frame_size+
total_buffer)*st->channels)*sizeof(*(&st->delay_buffer
[st->channels*(st->encoder_buffer-frame_size-total_buffer
)])) + 0*((&st->delay_buffer[st->channels*(st->encoder_buffer
-frame_size-total_buffer)])-(&pcm_buf[0])) ))
2329 (frame_size+total_buffer)*st->channels)(memcpy((&st->delay_buffer[st->channels*(st->encoder_buffer
-frame_size-total_buffer)]), (&pcm_buf[0]), ((frame_size+
total_buffer)*st->channels)*sizeof(*(&st->delay_buffer
[st->channels*(st->encoder_buffer-frame_size-total_buffer
)])) + 0*((&st->delay_buffer[st->channels*(st->encoder_buffer
-frame_size-total_buffer)])-(&pcm_buf[0])) ))
;
2330 } else {
2331 OPUS_COPY(st->delay_buffer, &pcm_buf[(frame_size+total_buffer-st->encoder_buffer)*st->channels], st->encoder_buffer*st->channels)(memcpy((st->delay_buffer), (&pcm_buf[(frame_size+total_buffer
-st->encoder_buffer)*st->channels]), (st->encoder_buffer
*st->channels)*sizeof(*(st->delay_buffer)) + 0*((st->
delay_buffer)-(&pcm_buf[(frame_size+total_buffer-st->encoder_buffer
)*st->channels])) ))
;
2332 }
2333 /* gain_fade() and stereo_fade() need to be after the buffer copying
2334 because we don't want any of this to affect the SILK part */
2335 if( ( st->prev_HB_gain < Q15ONE1.0f || HB_gain < Q15ONE1.0f ) && celt_mode != NULL((void*)0) ) {
2336 gain_fade(pcm_buf, pcm_buf,
2337 st->prev_HB_gain, HB_gain, celt_mode->overlap, frame_size, st->channels, celt_mode->window, st->Fs);
2338 }
2339 st->prev_HB_gain = HB_gain;
2340 if (st->mode != MODE_HYBRID1001 || st->stream_channels==1)
2341 {
2342 if (equiv_rate > 32000)
2343 st->silk_mode.stereoWidth_Q14 = 16384;
2344 else if (equiv_rate < 16000)
2345 st->silk_mode.stereoWidth_Q14 = 0;
2346 else
2347 st->silk_mode.stereoWidth_Q14 = 16384 - 2048*(opus_int32)(32000-equiv_rate)/(equiv_rate-14000);
2348 }
2349 if( !st->energy_masking && st->channels == 2 ) {
2350 /* Apply stereo width reduction (at low bitrates) */
2351 if( st->hybrid_stereo_width_Q14 < (1 << 14) || st->silk_mode.stereoWidth_Q14 < (1 << 14) ) {
2352 opus_val16 g1, g2;
2353 g1 = st->hybrid_stereo_width_Q14;
2354 g2 = (opus_val16)(st->silk_mode.stereoWidth_Q14);
2355#ifdef FIXED_POINT
2356 g1 = g1==16384 ? Q15ONE1.0f : SHL16(g1,1)(g1);
2357 g2 = g2==16384 ? Q15ONE1.0f : SHL16(g2,1)(g2);
2358#else
2359 g1 *= (1.f/16384);
2360 g2 *= (1.f/16384);
2361#endif
2362 if ( celt_mode != NULL((void*)0) )
2363 {
2364 stereo_fade(pcm_buf, pcm_buf, g1, g2, celt_mode->overlap,
2365 frame_size, st->channels, celt_mode->window, st->Fs);
2366 }
2367 st->hybrid_stereo_width_Q14 = st->silk_mode.stereoWidth_Q14;
2368 }
2369 }
2370
2371 if ( st->mode != MODE_CELT_ONLY1002 && ec_tell(&enc)+17+20*(st->mode == MODE_HYBRID1001) <= 8*(max_data_bytes-1))
2372 {
2373 /* For SILK mode, the redundancy is inferred from the length */
2374 if (st->mode == MODE_HYBRID1001)
2375 ec_enc_bit_logp(&enc, redundancy, 12);
2376 if (redundancy)
2377 {
2378 int max_redundancy;
2379 ec_enc_bit_logp(&enc, celt_to_silk, 1);
2380 if (st->mode == MODE_HYBRID1001)
2381 {
2382 /* Reserve the 8 bits needed for the redundancy length,
2383 and at least a few bits for CELT if possible */
2384 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+8+3+7)>>3);
2385 }
2386 else
2387 max_redundancy = (max_data_bytes-1)-((ec_tell(&enc)+7)>>3);
2388 /* Target the same bit-rate for redundancy as for the rest,
2389 up to a max of 257 bytes */
2390 redundancy_bytes = IMIN(max_redundancy, redundancy_bytes)((max_redundancy) < (redundancy_bytes) ? (max_redundancy) :
(redundancy_bytes))
;
2391 redundancy_bytes = IMIN(257, IMAX(2, redundancy_bytes))((257) < (((2) > (redundancy_bytes) ? (2) : (redundancy_bytes
))) ? (257) : (((2) > (redundancy_bytes) ? (2) : (redundancy_bytes
))))
;
2392 if (st->mode == MODE_HYBRID1001)
2393 ec_enc_uint(&enc, redundancy_bytes-2, 256);
2394 }
2395 } else {
2396 redundancy = 0;
2397 }
2398
2399 if (!redundancy)
2400 {
2401 st->silk_bw_switch = 0;
2402 redundancy_bytes = 0;
2403 }
2404 if (st->mode != MODE_CELT_ONLY1002)start_band=17;
2405
2406 if (st->mode == MODE_SILK_ONLY1000)
2407 {
2408 ret = (ec_tell(&enc)+7)>>3;
2409 ec_enc_done(&enc);
2410 nb_compr_bytes = ret;
2411 } else {
2412 nb_compr_bytes = (max_data_bytes-1)-redundancy_bytes;
2413#ifdef ENABLE_QEXT
2414 if (st->mode == MODE_CELT_ONLY1002 && st->enable_qext) {
2415 celt_assert(redundancy_bytes==0){if (!(redundancy_bytes==0)) {celt_fatal("assertion failed: "
"redundancy_bytes==0", "./../../../media/libopus/src/opus_encoder.c"
, 2415);}}
;
2416 nb_compr_bytes = orig_max_data_bytes-1;
2417 }
2418#endif
2419#ifdef ENABLE_DRED
2420 if (st->dred_duration > 0)
2421 {
2422 int max_celt_bytes;
2423 opus_int32 dred_bytes = bitrate_to_bits(dred_bitrate_bps, st->Fs, frame_size)/8;
2424 /* Allow CELT to steal up to 25% of the remaining bits. */
2425 max_celt_bytes = nb_compr_bytes - dred_bytes*3/4;
2426 /* But try to give CELT at least 5 bytes to prevent a mismatch with
2427 the redundancy signaling. */
2428 max_celt_bytes = IMAX((ec_tell(&enc)+7)/8 + 5, max_celt_bytes)(((ec_tell(&enc)+7)/8 + 5) > (max_celt_bytes) ? ((ec_tell
(&enc)+7)/8 + 5) : (max_celt_bytes))
;
2429 /* Subject to the original max. */
2430 nb_compr_bytes = IMIN(nb_compr_bytes, max_celt_bytes)((nb_compr_bytes) < (max_celt_bytes) ? (nb_compr_bytes) : (
max_celt_bytes))
;
2431 }
2432#endif
2433 ec_enc_shrink(&enc, nb_compr_bytes);
2434 }
2435
2436#ifndef DISABLE_FLOAT_API
2437 if (redundancy || st->mode != MODE_SILK_ONLY1000)
2438 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, CELT_SET_ANALYSIS(analysis_info)10022, ((analysis_info) + ((analysis_info) - (const AnalysisInfo
*)(analysis_info)))
);
2439#endif
2440 if (st->mode == MODE_HYBRID1001) {
2441 SILKInfo info;
2442 info.signalType = st->silk_mode.signalType;
2443 info.offset = st->silk_mode.offset;
2444 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, CELT_SET_SILK_INFO(&info)10028, ((&info) + ((&info) - (const SILKInfo*)(&info
)))
);
2445 }
2446
2447 /* 5 ms redundant frame for CELT->SILK */
2448 if (redundancy && celt_to_silk)
2449 {
2450 int err;
2451 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)10010, (((void)((0) == (opus_int32)0)), (opus_int32)(0)));
2452 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_VBR(0)4006, (((void)((0) == (opus_int32)0)), (opus_int32)(0)));
2453 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX)4002, (((void)((-1) == (opus_int32)0)), (opus_int32)(-1)));
2454 err = celt_encode_with_ec(celt_enc, pcm_buf, st->Fs/200, data+nb_compr_bytes, redundancy_bytes, NULL((void*)0));
2455 if (err < 0)
2456 {
2457 RESTORE_STACK;
2458 return OPUS_INTERNAL_ERROR-3;
2459 }
2460 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)4031, ((&redundant_rng) + ((&redundant_rng) - (opus_uint32
*)(&redundant_rng)))
);
2461 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_RESET_STATE4028);
2462 }
2463
2464 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
2465 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, CELT_SET_START_BAND(start_band)10010, (((void)((start_band) == (opus_int32)0)), (opus_int32)
(start_band))
);
2466
2467 data[-1] = 0;
2468 if (st->mode != MODE_SILK_ONLY1000)
2469 {
2470 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_VBR(st->use_vbr)4006, (((void)((st->use_vbr) == (opus_int32)0)), (opus_int32
)(st->use_vbr))
);
2471 if (st->mode == MODE_HYBRID1001)
2472 {
2473 if( st->use_vbr ) {
2474 /* Never go below the minimum rate CELT accepts (the ctl would fail). */
2475 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_BITRATE(IMAX(501, st->bitrate_bps-st->silk_mode.bitRate))4002, (((void)((((501) > (st->bitrate_bps-st->silk_mode
.bitRate) ? (501) : (st->bitrate_bps-st->silk_mode.bitRate
))) == (opus_int32)0)), (opus_int32)(((501) > (st->bitrate_bps
-st->silk_mode.bitRate) ? (501) : (st->bitrate_bps-st->
silk_mode.bitRate))))
);
2476 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0)4020, (((void)((0) == (opus_int32)0)), (opus_int32)(0)));
2477 }
2478 } else {
2479 if (st->use_vbr)
2480 {
2481 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_VBR(1)4006, (((void)((1) == (opus_int32)0)), (opus_int32)(1)));
2482 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(st->vbr_constraint)4020, (((void)((st->vbr_constraint) == (opus_int32)0)), (opus_int32
)(st->vbr_constraint))
);
2483 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_BITRATE(st->bitrate_bps)4002, (((void)((st->bitrate_bps) == (opus_int32)0)), (opus_int32
)(st->bitrate_bps))
);
2484 }
2485 }
2486#ifdef ENABLE_DRED
2487 /* When Using DRED CBR, we can actually make the CELT part VBR and have DRED pick up the slack. */
2488 if (!st->use_vbr && st->dred_duration > 0)
2489 {
2490 opus_int32 celt_bitrate = st->bitrate_bps;
2491 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_VBR(1)4006, (((void)((1) == (opus_int32)0)), (opus_int32)(1)));
2492 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_VBR_CONSTRAINT(0)4020, (((void)((0) == (opus_int32)0)), (opus_int32)(0)));
2493 if (st->mode == MODE_HYBRID1001) {
2494 celt_bitrate = IMAX(501, celt_bitrate - st->silk_mode.bitRate)((501) > (celt_bitrate - st->silk_mode.bitRate) ? (501)
: (celt_bitrate - st->silk_mode.bitRate))
;
2495 }
2496 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_BITRATE(celt_bitrate)4002, (((void)((celt_bitrate) == (opus_int32)0)), (opus_int32
)(celt_bitrate))
);
2497 }
2498#endif
2499 if (st->mode != st->prev_mode && st->prev_mode > 0 && st->application != OPUS_APPLICATION_RESTRICTED_CELT2053)
2500 {
2501 unsigned char dummy[2];
2502 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_RESET_STATE4028);
2503
2504 /* Prefilling */
2505 celt_encode_with_ec(celt_enc, tmp_prefill, st->Fs/400, dummy, 2, NULL((void*)0));
2506 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)10002, (((void)((0) == (opus_int32)0)), (opus_int32)(0)));
2507 }
2508 /* If false, we already busted the budget and we'll end up with a "PLC frame" */
2509 if (ec_tell(&enc) <= 8*nb_compr_bytes)
2510 {
2511#ifdef ENABLE_QEXT
2512 if (st->mode == MODE_CELT_ONLY1002) celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_QEXT(st->enable_qext)4056, (((void)((st->enable_qext) == (opus_int32)0)), (opus_int32
)(st->enable_qext))
);
2513#endif
2514 ret = celt_encode_with_ec(celt_enc, pcm_buf, frame_size, NULL((void*)0), nb_compr_bytes, &enc);
2515#ifdef ENABLE_QEXT
2516 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_QEXT(0)4056, (((void)((0) == (opus_int32)0)), (opus_int32)(0)));
2517#endif
2518 if (ret < 0)
2519 {
2520 RESTORE_STACK;
2521 return OPUS_INTERNAL_ERROR-3;
2522 }
2523 /* Put CELT->SILK redundancy data in the right place. */
2524 if (redundancy && celt_to_silk && st->mode==MODE_HYBRID1001 && nb_compr_bytes != ret)
2525 {
2526 OPUS_MOVE(data+ret, data+nb_compr_bytes, redundancy_bytes)(memmove((data+ret), (data+nb_compr_bytes), (redundancy_bytes
)*sizeof(*(data+ret)) + 0*((data+ret)-(data+nb_compr_bytes)) )
)
;
2527 nb_compr_bytes = ret+redundancy_bytes;
2528 }
2529 }
2530 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&st->rangeFinal)4031, ((&st->rangeFinal) + ((&st->rangeFinal) -
(opus_uint32*)(&st->rangeFinal)))
);
2531 } else {
2532 st->rangeFinal = enc.rng;
2533 }
2534
2535 /* 5 ms redundant frame for SILK->CELT */
2536 if (redundancy && !celt_to_silk)
2537 {
2538 int err;
2539 unsigned char dummy[2];
2540 int N2, N4;
2541 N2 = st->Fs/200;
2542 N4 = st->Fs/400;
2543
2544 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_RESET_STATE4028);
2545 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, CELT_SET_START_BAND(0)10010, (((void)((0) == (opus_int32)0)), (opus_int32)(0)));
2546 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, CELT_SET_PREDICTION(0)10002, (((void)((0) == (opus_int32)0)), (opus_int32)(0)));
2547 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_VBR(0)4006, (((void)((0) == (opus_int32)0)), (opus_int32)(0)));
2548 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_BITRATE(OPUS_BITRATE_MAX)4002, (((void)((-1) == (opus_int32)0)), (opus_int32)(-1)));
2549
2550 if (st->mode == MODE_HYBRID1001)
2551 {
2552 /* Shrink packet to what the encoder actually used. */
2553 nb_compr_bytes = ret;
2554 ec_enc_shrink(&enc, nb_compr_bytes);
2555 }
2556 /* NOTE: We could speed this up slightly (at the expense of code size) by just adding a function that prefills the buffer */
2557 celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2-N4), N4, dummy, 2, NULL((void*)0));
2558
2559 err = celt_encode_with_ec(celt_enc, pcm_buf+st->channels*(frame_size-N2), N2, data+nb_compr_bytes, redundancy_bytes, NULL((void*)0));
2560 if (err < 0)
2561 {
2562 RESTORE_STACK;
2563 return OPUS_INTERNAL_ERROR-3;
2564 }
2565 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_GET_FINAL_RANGE(&redundant_rng)4031, ((&redundant_rng) + ((&redundant_rng) - (opus_uint32
*)(&redundant_rng)))
);
2566 }
2567
2568
2569
2570 /* Signalling the mode in the first byte */
2571 data--;
2572 data[0] |= gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2573
2574 st->rangeFinal ^= redundant_rng;
2575
2576 if (to_celt)
2577 st->prev_mode = MODE_CELT_ONLY1002;
2578 else
2579 st->prev_mode = st->mode;
2580 st->prev_channels = st->stream_channels;
2581 st->prev_framesize = frame_size;
2582
2583 st->first = 0;
2584
2585 /* DTX decision */
2586 if (st->use_dtx && !st->silk_mode.useDTX)
2587 {
2588 if (decide_dtx_mode(activity, &st->nb_no_activity_ms_Q1, 2*1000*frame_size/st->Fs))
2589 {
2590 st->rangeFinal = 0;
2591 data[0] = gen_toc(st->mode, st->Fs/frame_size, curr_bandwidth, st->stream_channels);
2592 RESTORE_STACK;
2593 return 1;
2594 }
2595 } else {
2596 st->nb_no_activity_ms_Q1 = 0;
2597 }
2598
2599 /* In the unlikely case that the SILK encoder busted its target, tell
2600 the decoder to call the PLC */
2601 if (ec_tell(&enc) > (max_data_bytes-1)*8)
2602 {
2603 if (max_data_bytes < 2)
2604 {
2605 RESTORE_STACK;
2606 return OPUS_BUFFER_TOO_SMALL-2;
2607 }
2608 data[1] = 0;
2609 ret = 1;
2610 st->rangeFinal = 0;
2611 } else if (st->mode==MODE_SILK_ONLY1000&&!redundancy)
2612 {
2613 /*When in LPC only mode it's perfectly
2614 reasonable to strip off trailing zero bytes as
2615 the required range decoder behavior is to
2616 fill these in. This can't be done when the MDCT
2617 modes are used because the decoder needs to know
2618 the actual length for allocation purposes.*/
2619 while(ret>2&&data[ret]==0)ret--;
2620 }
2621 /* Count ToC and redundancy */
2622 ret += 1+redundancy_bytes;
2623 apply_padding = !st->use_vbr;
2624#ifdef ENABLE_DRED
2625 if (st->dred_duration > 0 && st->dred_encoder.loaded && first_frame) {
2626 opus_extension_data extension;
2627 unsigned char buf[DRED_MAX_DATA_SIZE];
2628 int dred_chunks;
2629 int dred_bytes_left;
2630 dred_chunks = IMIN((st->dred_duration+5)/4, DRED_NUM_REDUNDANCY_FRAMES/2)(((st->dred_duration+5)/4) < (DRED_NUM_REDUNDANCY_FRAMES
/2) ? ((st->dred_duration+5)/4) : (DRED_NUM_REDUNDANCY_FRAMES
/2))
;
2631 if (st->use_vbr) dred_chunks = IMIN(dred_chunks, st->dred_target_chunks)((dred_chunks) < (st->dred_target_chunks) ? (dred_chunks
) : (st->dred_target_chunks))
;
2632 /* Remaining space for DRED, accounting for cost the 3 extra bytes for code 3, padding length, and extension number. */
2633 dred_bytes_left = IMIN(DRED_MAX_DATA_SIZE, orig_max_data_bytes-ret-3)((DRED_MAX_DATA_SIZE) < (orig_max_data_bytes-ret-3) ? (DRED_MAX_DATA_SIZE
) : (orig_max_data_bytes-ret-3))
;
2634 /* Account for the extra bytes required to signal large padding length. */
2635 dred_bytes_left -= (dred_bytes_left+1+DRED_EXPERIMENTAL_BYTES)/255;
2636 /* Check whether we actually have something to encode. */
2637 if (dred_chunks >= 1 && dred_bytes_left >= DRED_MIN_BYTES+DRED_EXPERIMENTAL_BYTES) {
2638 int dred_bytes;
2639#ifdef DRED_EXPERIMENTAL_VERSION
2640 /* Add temporary extension type and version.
2641 These bytes will be removed once extension is finalized. */
2642 buf[0] = 'D';
2643 buf[1] = DRED_EXPERIMENTAL_VERSION;
2644#endif
2645 dred_bytes = dred_encode_silk_frame(&st->dred_encoder, buf+DRED_EXPERIMENTAL_BYTES, dred_chunks, dred_bytes_left-DRED_EXPERIMENTAL_BYTES,
2646 st->dred_q0, st->dred_dQ, st->dred_qmax, st->activity_mem, st->arch);
2647 if (dred_bytes > 0) {
2648 dred_bytes += DRED_EXPERIMENTAL_BYTES;
2649 celt_assert(dred_bytes <= dred_bytes_left){if (!(dred_bytes <= dred_bytes_left)) {celt_fatal("assertion failed: "
"dred_bytes <= dred_bytes_left", "./../../../media/libopus/src/opus_encoder.c"
, 2649);}}
;
2650 extension.id = DRED_EXTENSION_ID;
2651 extension.frame = 0;
2652 extension.data = buf;
2653 extension.len = dred_bytes;
2654 ret = opus_packet_pad_impl(data, ret, orig_max_data_bytes, !st->use_vbr, &extension, 1);
2655 if (ret < 0)
2656 {
2657 RESTORE_STACK;
2658 return OPUS_INTERNAL_ERROR-3;
2659 }
2660 apply_padding = 0;
2661 }
2662 }
2663 }
2664#else
2665 (void)first_frame; /* Avoids a warning about first_frame being unused. */
2666#endif
2667 if (apply_padding)
2668 {
2669 if (opus_packet_pad(data, ret, orig_max_data_bytes) != OPUS_OK0)
2670 {
2671 RESTORE_STACK;
2672 return OPUS_INTERNAL_ERROR-3;
2673 }
2674 ret = orig_max_data_bytes;
2675 }
2676 RESTORE_STACK;
2677 return ret;
2678}
2679
2680
2681
2682#if defined(FIXED_POINT) && !defined(ENABLE_RES24)
2683opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
2684 unsigned char *data, opus_int32 max_data_bytes)
2685{
2686 int frame_size;
2687 frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs);
2688 return opus_encode_native(st, pcm, frame_size, data, max_data_bytes, 16,
2689 pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 0);
2690}
2691#else
2692opus_int32 opus_encode(OpusEncoder *st, const opus_int16 *pcm, int analysis_frame_size,
2693 unsigned char *data, opus_int32 max_data_bytes)
2694{
2695 int i, ret;
2696 int frame_size;
2697 VARDECL(opus_res, in)opus_res *in;
2698 ALLOC_STACK;
2699
2700 frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs);
2701 if (frame_size <= 0)
2702 {
2703 RESTORE_STACK;
2704 return OPUS_BAD_ARG-1;
2705 }
2706 ALLOC(in, frame_size*st->channels, opus_res)in = ((opus_res*)__builtin_alloca (sizeof(opus_res)*(frame_size
*st->channels)))
;
2707
2708 for (i=0;i<frame_size*st->channels;i++)
2709 in[i] = INT16TORES(pcm[i])((pcm[i])*(1/32768.f));
2710 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, 16,
2711 pcm, analysis_frame_size, 0, -2, st->channels, downmix_int, 1);
2712 RESTORE_STACK;
2713 return ret;
2714}
2715#endif
2716
2717#if defined(FIXED_POINT) && defined(ENABLE_RES24)
2718opus_int32 opus_encode24(OpusEncoder *st, const opus_int32 *pcm, int analysis_frame_size,
2719 unsigned char *data, opus_int32 max_data_bytes)
2720{
2721 int frame_size;
2722 frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs);
2723 return opus_encode_native(st, pcm, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH24,
2724 pcm, analysis_frame_size, 0, -2, st->channels, downmix_int24, 0);
2725}
2726#else
2727opus_int32 opus_encode24(OpusEncoder *st, const opus_int32 *pcm, int analysis_frame_size,
2728 unsigned char *data, opus_int32 max_data_bytes)
2729{
2730 int i, ret;
2731 int frame_size;
2732 VARDECL(opus_res, in)opus_res *in;
2733 ALLOC_STACK;
2734
2735 frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs);
2736 if (frame_size <= 0)
2737 {
2738 RESTORE_STACK;
2739 return OPUS_BAD_ARG-1;
2740 }
2741 ALLOC(in, frame_size*st->channels, opus_res)in = ((opus_res*)__builtin_alloca (sizeof(opus_res)*(frame_size
*st->channels)))
;
2742
2743 for (i=0;i<frame_size*st->channels;i++)
2744 in[i] = INT24TORES(pcm[i])((1.f/32768.f/256.f)*(pcm[i]));
2745 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH24,
2746 pcm, analysis_frame_size, 0, -2, st->channels, downmix_int24, 1);
2747 RESTORE_STACK;
2748 return ret;
2749}
2750#endif
2751
2752
2753#ifndef DISABLE_FLOAT_API
2754
2755# if !defined(FIXED_POINT)
2756opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
2757 unsigned char *data, opus_int32 out_data_bytes)
2758{
2759 int frame_size;
2760 frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs);
2761 return opus_encode_native(st, pcm, frame_size, data, out_data_bytes, MAX_ENCODING_DEPTH24,
2762 pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
2763}
2764# else
2765opus_int32 opus_encode_float(OpusEncoder *st, const float *pcm, int analysis_frame_size,
2766 unsigned char *data, opus_int32 max_data_bytes)
2767{
2768 int i, ret;
2769 int frame_size;
2770 VARDECL(opus_res, in)opus_res *in;
2771 ALLOC_STACK;
2772
2773 frame_size = frame_size_select(st->application, analysis_frame_size, st->variable_duration, st->Fs);
2774 if (frame_size <= 0)
2775 {
2776 RESTORE_STACK;
2777 return OPUS_BAD_ARG-1;
2778 }
2779 ALLOC(in, frame_size*st->channels, opus_res)in = ((opus_res*)__builtin_alloca (sizeof(opus_res)*(frame_size
*st->channels)))
;
2780
2781 for (i=0;i<frame_size*st->channels;i++)
2782 in[i] = FLOAT2RES(pcm[i])(pcm[i]);
2783 ret = opus_encode_native(st, in, frame_size, data, max_data_bytes, MAX_ENCODING_DEPTH24,
2784 pcm, analysis_frame_size, 0, -2, st->channels, downmix_float, 1);
2785 RESTORE_STACK;
2786 return ret;
2787}
2788# endif
2789
2790#endif
2791
2792
2793int opus_encoder_ctl(OpusEncoder *st, int request, ...)
2794{
2795 int ret;
2796 CELTEncoderOpusCustomEncoder *celt_enc=NULL((void*)0);
2797 va_list ap;
2798
2799 ret = OPUS_OK0;
2800 va_start(ap, request)__builtin_va_start(ap, request);
2801
2802 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
2803 celt_enc = (CELTEncoderOpusCustomEncoder*)((char*)st+st->celt_enc_offset);
2804
2805 switch (request)
2806 {
2807 case OPUS_SET_APPLICATION_REQUEST4000:
2808 {
2809 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2810 if (st->application == OPUS_APPLICATION_RESTRICTED_SILK2052 || st->application == OPUS_APPLICATION_RESTRICTED_CELT2053)
2811 {
2812 ret = OPUS_BAD_ARG-1;
2813 break;
2814 }
2815 if ( (value != OPUS_APPLICATION_VOIP2048 && value != OPUS_APPLICATION_AUDIO2049
2816 && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY2051)
2817 || (!st->first && st->application != value))
2818 {
2819 ret = OPUS_BAD_ARG-1;
2820 break;
2821 }
2822 st->application = value;
2823#ifndef DISABLE_FLOAT_API
2824 st->analysis.application = value;
2825#endif
2826 }
2827 break;
2828 case OPUS_GET_APPLICATION_REQUEST4001:
2829 {
2830 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
2831 if (!value)
2832 {
2833 goto bad_arg;
2834 }
2835 *value = st->application;
2836 }
2837 break;
2838 case OPUS_SET_BITRATE_REQUEST4002:
2839 {
2840 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2841 if (value != OPUS_AUTO-1000 && value != OPUS_BITRATE_MAX-1)
2842 {
2843 if (value <= 0)
2844 goto bad_arg;
2845 else if (value <= 500)
2846 value = 500;
2847 else if (value > (opus_int32)750000*st->channels)
2848 value = (opus_int32)750000*st->channels;
2849 }
2850 st->user_bitrate_bps = value;
2851 }
2852 break;
2853 case OPUS_GET_BITRATE_REQUEST4003:
2854 {
2855 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
2856 if (!value)
2857 {
2858 goto bad_arg;
2859 }
2860 *value = user_bitrate_to_bitrate(st, st->prev_framesize, 1276);
2861 }
2862 break;
2863 case OPUS_SET_FORCE_CHANNELS_REQUEST4022:
2864 {
2865 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2866 if((value<1 || value>st->channels) && value != OPUS_AUTO-1000)
2867 {
2868 goto bad_arg;
2869 }
2870 st->force_channels = value;
2871 }
2872 break;
2873 case OPUS_GET_FORCE_CHANNELS_REQUEST4023:
2874 {
2875 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
2876 if (!value)
2877 {
2878 goto bad_arg;
2879 }
2880 *value = st->force_channels;
2881 }
2882 break;
2883 case OPUS_SET_MAX_BANDWIDTH_REQUEST4004:
2884 {
2885 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2886 if (value < OPUS_BANDWIDTH_NARROWBAND1101 || value > OPUS_BANDWIDTH_FULLBAND1105)
2887 {
2888 goto bad_arg;
2889 }
2890 st->max_bandwidth = value;
2891 if (st->max_bandwidth == OPUS_BANDWIDTH_NARROWBAND1101) {
2892 st->silk_mode.maxInternalSampleRate = 8000;
2893 } else if (st->max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND1102) {
2894 st->silk_mode.maxInternalSampleRate = 12000;
2895 } else {
2896 st->silk_mode.maxInternalSampleRate = 16000;
2897 }
2898 }
2899 break;
2900 case OPUS_GET_MAX_BANDWIDTH_REQUEST4005:
2901 {
2902 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
2903 if (!value)
2904 {
2905 goto bad_arg;
2906 }
2907 *value = st->max_bandwidth;
2908 }
2909 break;
2910 case OPUS_SET_BANDWIDTH_REQUEST4008:
2911 {
2912 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2913 if ((value < OPUS_BANDWIDTH_NARROWBAND1101 || value > OPUS_BANDWIDTH_FULLBAND1105) && value != OPUS_AUTO-1000)
2914 {
2915 goto bad_arg;
2916 }
2917 st->user_bandwidth = value;
2918 if (st->user_bandwidth == OPUS_BANDWIDTH_NARROWBAND1101) {
2919 st->silk_mode.maxInternalSampleRate = 8000;
2920 } else if (st->user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND1102) {
2921 st->silk_mode.maxInternalSampleRate = 12000;
2922 } else {
2923 st->silk_mode.maxInternalSampleRate = 16000;
2924 }
2925 }
2926 break;
2927 case OPUS_GET_BANDWIDTH_REQUEST4009:
2928 {
2929 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
2930 if (!value)
2931 {
2932 goto bad_arg;
2933 }
2934 *value = st->bandwidth;
2935 }
2936 break;
2937 case OPUS_SET_DTX_REQUEST4016:
2938 {
2939 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2940 if(value<0 || value>1)
2941 {
2942 goto bad_arg;
2943 }
2944 st->use_dtx = value;
2945 }
2946 break;
2947 case OPUS_GET_DTX_REQUEST4017:
2948 {
2949 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
2950 if (!value)
2951 {
2952 goto bad_arg;
2953 }
2954 *value = st->use_dtx;
2955 }
2956 break;
2957 case OPUS_SET_COMPLEXITY_REQUEST4010:
2958 {
2959 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2960 if(value<0 || value>10)
2961 {
2962 goto bad_arg;
2963 }
2964 st->silk_mode.complexity = value;
2965 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
2966 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_COMPLEXITY(value)4010, (((void)((value) == (opus_int32)0)), (opus_int32)(value
))
);
2967 }
2968 break;
2969 case OPUS_GET_COMPLEXITY_REQUEST4011:
2970 {
2971 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
2972 if (!value)
2973 {
2974 goto bad_arg;
2975 }
2976 *value = st->silk_mode.complexity;
2977 }
2978 break;
2979 case OPUS_SET_INBAND_FEC_REQUEST4012:
2980 {
2981 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
2982 if(value<0 || value>2)
2983 {
2984 goto bad_arg;
2985 }
2986 st->fec_config = value;
2987 st->silk_mode.useInBandFEC = (value != 0);
2988 }
2989 break;
2990 case OPUS_GET_INBAND_FEC_REQUEST4013:
2991 {
2992 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
2993 if (!value)
2994 {
2995 goto bad_arg;
2996 }
2997 *value = st->fec_config;
2998 }
2999 break;
3000 case OPUS_SET_PACKET_LOSS_PERC_REQUEST4014:
3001 {
3002 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
3003 if (value < 0 || value > 100)
3004 {
3005 goto bad_arg;
3006 }
3007 st->silk_mode.packetLossPercentage = value;
3008 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
3009 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_PACKET_LOSS_PERC(value)4014, (((void)((value) == (opus_int32)0)), (opus_int32)(value
))
);
3010 }
3011 break;
3012 case OPUS_GET_PACKET_LOSS_PERC_REQUEST4015:
3013 {
3014 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
3015 if (!value)
3016 {
3017 goto bad_arg;
3018 }
3019 *value = st->silk_mode.packetLossPercentage;
3020 }
3021 break;
3022 case OPUS_SET_VBR_REQUEST4006:
3023 {
3024 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
3025 if(value<0 || value>1)
3026 {
3027 goto bad_arg;
3028 }
3029 st->use_vbr = value;
3030 st->silk_mode.useCBR = 1-value;
3031 }
3032 break;
3033 case OPUS_GET_VBR_REQUEST4007:
3034 {
3035 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
3036 if (!value)
3037 {
3038 goto bad_arg;
3039 }
3040 *value = st->use_vbr;
3041 }
3042 break;
3043 case OPUS_SET_VOICE_RATIO_REQUEST11018:
3044 {
3045 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
3046 if (value<-1 || value>100)
3047 {
3048 goto bad_arg;
3049 }
3050 st->voice_ratio = value;
3051 }
3052 break;
3053 case OPUS_GET_VOICE_RATIO_REQUEST11019:
3054 {
3055 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
3056 if (!value)
3057 {
3058 goto bad_arg;
3059 }
3060 *value = st->voice_ratio;
3061 }
3062 break;
3063 case OPUS_SET_VBR_CONSTRAINT_REQUEST4020:
3064 {
3065 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
3066 if(value<0 || value>1)
3067 {
3068 goto bad_arg;
3069 }
3070 st->vbr_constraint = value;
3071 }
3072 break;
3073 case OPUS_GET_VBR_CONSTRAINT_REQUEST4021:
3074 {
3075 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
3076 if (!value)
3077 {
3078 goto bad_arg;
3079 }
3080 *value = st->vbr_constraint;
3081 }
3082 break;
3083 case OPUS_SET_SIGNAL_REQUEST4024:
3084 {
3085 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
3086 if(value!=OPUS_AUTO-1000 && value!=OPUS_SIGNAL_VOICE3001 && value!=OPUS_SIGNAL_MUSIC3002)
3087 {
3088 goto bad_arg;
3089 }
3090 st->signal_type = value;
3091 }
3092 break;
3093 case OPUS_GET_SIGNAL_REQUEST4025:
3094 {
3095 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
3096 if (!value)
3097 {
3098 goto bad_arg;
3099 }
3100 *value = st->signal_type;
3101 }
3102 break;
3103 case OPUS_GET_LOOKAHEAD_REQUEST4027:
3104 {
3105 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
3106 if (!value)
3107 {
3108 goto bad_arg;
3109 }
3110 *value = st->Fs/400;
3111 if (st->application != OPUS_APPLICATION_RESTRICTED_LOWDELAY2051 && st->application != OPUS_APPLICATION_RESTRICTED_CELT2053)
3112 *value += st->delay_compensation;
3113 }
3114 break;
3115 case OPUS_GET_SAMPLE_RATE_REQUEST4029:
3116 {
3117 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
3118 if (!value)
3119 {
3120 goto bad_arg;
3121 }
3122 *value = st->Fs;
3123 }
3124 break;
3125 case OPUS_GET_FINAL_RANGE_REQUEST4031:
3126 {
3127 opus_uint32 *value = va_arg(ap, opus_uint32*)__builtin_va_arg(ap, opus_uint32*);
3128 if (!value)
3129 {
3130 goto bad_arg;
3131 }
3132 *value = st->rangeFinal;
3133 }
3134 break;
3135 case OPUS_SET_LSB_DEPTH_REQUEST4036:
3136 {
3137 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
3138 if (value<8 || value>24)
3139 {
3140 goto bad_arg;
3141 }
3142 st->lsb_depth=value;
3143 }
3144 break;
3145 case OPUS_GET_LSB_DEPTH_REQUEST4037:
3146 {
3147 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
3148 if (!value)
3149 {
3150 goto bad_arg;
3151 }
3152 *value = st->lsb_depth;
3153 }
3154 break;
3155 case OPUS_SET_EXPERT_FRAME_DURATION_REQUEST4040:
3156 {
3157 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
3158 if (value != OPUS_FRAMESIZE_ARG5000 && value != OPUS_FRAMESIZE_2_5_MS5001 &&
3159 value != OPUS_FRAMESIZE_5_MS5002 && value != OPUS_FRAMESIZE_10_MS5003 &&
3160 value != OPUS_FRAMESIZE_20_MS5004 && value != OPUS_FRAMESIZE_40_MS5005 &&
3161 value != OPUS_FRAMESIZE_60_MS5006 && value != OPUS_FRAMESIZE_80_MS5007 &&
3162 value != OPUS_FRAMESIZE_100_MS5008 && value != OPUS_FRAMESIZE_120_MS5009)
3163 {
3164 goto bad_arg;
3165 }
3166 st->variable_duration = value;
3167 }
3168 break;
3169 case OPUS_GET_EXPERT_FRAME_DURATION_REQUEST4041:
3170 {
3171 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
3172 if (!value)
3173 {
3174 goto bad_arg;
3175 }
3176 *value = st->variable_duration;
3177 }
3178 break;
3179 case OPUS_SET_PREDICTION_DISABLED_REQUEST4042:
3180 {
3181 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
3182 if (value > 1 || value < 0)
3183 goto bad_arg;
3184 st->silk_mode.reducedDependency = value;
3185 }
3186 break;
3187 case OPUS_GET_PREDICTION_DISABLED_REQUEST4043:
3188 {
3189 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
3190 if (!value)
3191 goto bad_arg;
3192 *value = st->silk_mode.reducedDependency;
3193 }
3194 break;
3195 case OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST4046:
3196 {
3197 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
3198 if(value<0 || value>1)
3199 {
3200 goto bad_arg;
3201 }
3202 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
3203 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_PHASE_INVERSION_DISABLED(value)4046, (((void)((value) == (opus_int32)0)), (opus_int32)(value
))
);
3204 }
3205 break;
3206 case OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST4047:
3207 {
3208 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
3209 if (!value)
3210 {
3211 goto bad_arg;
3212 }
3213 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
3214 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_GET_PHASE_INVERSION_DISABLED(value)4047, ((value) + ((value) - (opus_int32*)(value))));
3215 else
3216 *value = 0;
3217 }
3218 break;
3219#ifdef ENABLE_DRED
3220 case OPUS_SET_DRED_DURATION_REQUEST4050:
3221 {
3222 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
3223 if(value<0 || value>DRED_MAX_FRAMES)
3224 {
3225 goto bad_arg;
3226 }
3227 st->dred_duration = value;
3228 st->silk_mode.useDRED = !!value;
3229 }
3230 break;
3231 case OPUS_GET_DRED_DURATION_REQUEST4051:
3232 {
3233 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
3234 if (!value)
3235 {
3236 goto bad_arg;
3237 }
3238 *value = st->dred_duration;
3239 }
3240 break;
3241#endif
3242#ifdef ENABLE_QEXT
3243 case OPUS_SET_QEXT_REQUEST4056:
3244 {
3245 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
3246 if(value<0 || value>1)
3247 {
3248 goto bad_arg;
3249 }
3250 st->enable_qext = value;
3251 }
3252 break;
3253 case OPUS_GET_QEXT_REQUEST4057:
3254 {
3255 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
3256 if (!value)
3257 {
3258 goto bad_arg;
3259 }
3260 *value = st->enable_qext;
3261 }
3262 break;
3263#endif
3264 case OPUS_RESET_STATE4028:
3265 {
3266 void *silk_enc;
3267 silk_EncControlStruct dummy;
3268 char *start;
3269 silk_enc = (char*)st+st->silk_enc_offset;
3270#ifndef DISABLE_FLOAT_API
3271 tonality_analysis_reset(&st->analysis);
3272#endif
3273
3274 start = (char*)&st->OPUS_ENCODER_RESET_STARTstream_channels;
3275 OPUS_CLEAR(start, st->silk_enc_offset - (start - (char*)st))(memset((start), 0, (st->silk_enc_offset - (start - (char*
)st))*sizeof(*(start))))
;
3276
3277 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
3278 celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_RESET_STATE4028);
3279 if (st->application != OPUS_APPLICATION_RESTRICTED_CELT2053)
3280 silk_InitEncoder( silk_enc, st->channels, st->arch, &dummy );
3281#ifdef ENABLE_DRED
3282 /* Initialize DRED Encoder */
3283 dred_encoder_reset( &st->dred_encoder );
3284#endif
3285 st->stream_channels = st->channels;
3286 st->hybrid_stereo_width_Q14 = 1 << 14;
3287 st->prev_HB_gain = Q15ONE1.0f;
3288 st->first = 1;
3289 st->mode = MODE_HYBRID1001;
3290 st->bandwidth = OPUS_BANDWIDTH_FULLBAND1105;
3291 st->variable_HP_smth2_Q15 = silk_LSHIFT( silk_lin2log( VARIABLE_HP_MIN_CUTOFF_HZ ), 8 )((opus_int32)((opus_uint32)(silk_lin2log( 60 ))<<(8)));
3292 }
3293 break;
3294 case OPUS_SET_FORCE_MODE_REQUEST11002:
3295 {
3296 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
3297 if ((value < MODE_SILK_ONLY1000 || value > MODE_CELT_ONLY1002) && value != OPUS_AUTO-1000)
3298 {
3299 goto bad_arg;
3300 }
3301 st->user_forced_mode = value;
3302 }
3303 break;
3304 case OPUS_SET_LFE_REQUEST10024:
3305 {
3306 opus_int32 value = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
3307 st->lfe = value;
3308 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
3309 ret = celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_LFE(value)10024, (((void)((value) == (opus_int32)0)), (opus_int32)(value
))
);
3310 }
3311 break;
3312 case OPUS_SET_ENERGY_MASK_REQUEST10026:
3313 {
3314 celt_glog *value = va_arg(ap, celt_glog*)__builtin_va_arg(ap, celt_glog*);
3315 st->energy_masking = value;
3316 if (st->application != OPUS_APPLICATION_RESTRICTED_SILK2052)
3317 ret = celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, OPUS_SET_ENERGY_MASK(value)10026, ((value) + ((value) - (celt_glog*)(value))));
3318 }
3319 break;
3320 case OPUS_GET_IN_DTX_REQUEST4049:
3321 {
3322 opus_int32 *value = va_arg(ap, opus_int32*)__builtin_va_arg(ap, opus_int32*);
3323 if (!value)
3324 {
3325 goto bad_arg;
3326 }
3327 if (st->silk_mode.useDTX && (st->prev_mode == MODE_SILK_ONLY1000 || st->prev_mode == MODE_HYBRID1001)) {
3328 /* DTX determined by Silk. */
3329 silk_encoder *silk_enc = (silk_encoder*)(void *)((char*)st+st->silk_enc_offset);
3330 *value = silk_enc->state_Fxx[0].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX10;
3331 /* Stereo: check second channel unless only the middle channel was encoded. */
3332 if(*value == 1 && st->silk_mode.nChannelsInternal == 2 && silk_enc->prev_decode_only_middle == 0) {
3333 *value = silk_enc->state_Fxx[1].sCmn.noSpeechCounter >= NB_SPEECH_FRAMES_BEFORE_DTX10;
3334 }
3335 }
3336 else if (st->use_dtx) {
3337 /* DTX determined by Opus. */
3338 *value = st->nb_no_activity_ms_Q1 >= NB_SPEECH_FRAMES_BEFORE_DTX10*20*2;
3339 }
3340 else {
3341 *value = 0;
3342 }
3343 }
3344 break;
3345#ifdef USE_WEIGHTS_FILE
3346 case OPUS_SET_DNN_BLOB_REQUEST4052:
3347 {
3348 const unsigned char *data = va_arg(ap, const unsigned char *)__builtin_va_arg(ap, const unsigned char *);
3349 opus_int32 len = va_arg(ap, opus_int32)__builtin_va_arg(ap, opus_int32);
3350 if(len<0 || data == NULL((void*)0))
3351 {
3352 goto bad_arg;
3353 }
3354#ifdef ENABLE_DRED
3355 ret = dred_encoder_load_model(&st->dred_encoder, data, len);
3356#endif
3357 }
3358 break;
3359#endif
3360 case CELT_GET_MODE_REQUEST10015:
3361 {
3362 const CELTModeOpusCustomMode ** value = va_arg(ap, const CELTMode**)__builtin_va_arg(ap, const OpusCustomMode**);
3363 if (!value)
3364 {
3365 goto bad_arg;
3366 }
3367 celt_assert(celt_enc != NULL){if (!(celt_enc != ((void*)0))) {celt_fatal("assertion failed: "
"celt_enc != NULL", "./../../../media/libopus/src/opus_encoder.c"
, 3367);}}
;
3368 ret = celt_encoder_ctlopus_custom_encoder_ctl(celt_enc, CELT_GET_MODE(value)10015, ((value) + ((value) - (const OpusCustomMode**)(value))
)
);
3369 }
3370 break;
3371 default:
3372 /* fprintf(stderr, "unknown opus_encoder_ctl() request: %d", request);*/
3373 ret = OPUS_UNIMPLEMENTED-5;
3374 break;
3375 }
3376 va_end(ap)__builtin_va_end(ap);
3377 return ret;
3378bad_arg:
3379 va_end(ap)__builtin_va_end(ap);
3380 return OPUS_BAD_ARG-1;
3381}
3382
3383void opus_encoder_destroy(OpusEncoder *st)
3384{
3385 opus_free(st);
3386}