1use crate::externs::{free, malloc};
2
3pub mod arch_h {
4 pub type opus_val16 = f32;
5 pub type opus_val32 = f32;
6 pub const CELT_SIG_SCALE: f32 = 32768.0f32;
7 pub const Q15ONE: f32 = 1.0f32;
8 pub const EPSILON: f32 = 1e-15f32;
9 pub const VERY_SMALL: f32 = 1e-30f32;
10}
11pub mod stddef_h {
12 pub type size_t = u64;
13 pub const NULL: i32 = 0;
14}
15pub mod xmmintrin_h {
16 #[cfg(target_arch = "x86")]
17 pub use core::arch::x86::{__m128, _mm_cvt_ss2si, _mm_cvtss_si32, _mm_set_ss};
18 #[cfg(target_arch = "x86_64")]
19 pub use core::arch::x86_64::{__m128, _mm_cvt_ss2si, _mm_cvtss_si32, _mm_set_ss};
20}
21pub mod cpu_support_h {
22 #[inline]
23 pub unsafe fn opus_select_arch() -> i32 {
24 return 0;
25 }
26}
27use self::arch_h::{opus_val16, opus_val32, CELT_SIG_SCALE, EPSILON, Q15ONE, VERY_SMALL};
28pub use self::cpu_support_h::opus_select_arch;
29pub use self::stddef_h::{size_t, NULL};
30use crate::celt::celt::{
31 CELT_GET_MODE_REQUEST, CELT_SET_ANALYSIS_REQUEST, CELT_SET_CHANNELS_REQUEST,
32 CELT_SET_END_BAND_REQUEST, CELT_SET_PREDICTION_REQUEST, CELT_SET_SIGNALLING_REQUEST,
33 CELT_SET_SILK_INFO_REQUEST, CELT_SET_START_BAND_REQUEST, OPUS_SET_ENERGY_MASK_REQUEST,
34 OPUS_SET_LFE_REQUEST,
35};
36use crate::celt::celt_encoder::{
37 celt_encode_with_ec, celt_encoder_get_size, celt_encoder_init, OpusCustomEncoder, SILKInfo,
38};
39use crate::celt::entcode::ec_tell;
40use crate::celt::entenc::ec_enc;
41use crate::celt::entenc::{ec_enc_bit_logp, ec_enc_done, ec_enc_init, ec_enc_shrink, ec_enc_uint};
42use crate::celt::float_cast::FLOAT2INT16;
43use crate::celt::mathops::{celt_exp2, celt_maxabs16, celt_sqrt};
44use crate::celt::modes::OpusCustomMode;
45use crate::celt::pitch::celt_inner_prod_c;
46use crate::externs::{memcpy, memmove, memset};
47use crate::silk::define::{
48 DTX_ACTIVITY_THRESHOLD, MAX_CONSECUTIVE_DTX, NB_SPEECH_FRAMES_BEFORE_DTX, VAD_NO_DECISION,
49};
50use crate::silk::enc_API::silk_EncControlStruct;
51use crate::silk::enc_API::{silk_Encode, silk_Get_Encoder_Size, silk_InitEncoder};
52use crate::silk::float::structs_FLP::silk_encoder;
53use crate::silk::lin2log::silk_lin2log;
54use crate::silk::log2lin::silk_log2lin;
55use crate::src::analysis::{
56 downmix_func, run_analysis, tonality_analysis_init, tonality_analysis_reset, AnalysisInfo,
57 TonalityAnalysisState,
58};
59use crate::src::opus_defines::{
60 OPUS_ALLOC_FAIL, OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_RESTRICTED_LOWDELAY,
61 OPUS_APPLICATION_VOIP, OPUS_AUTO, OPUS_BAD_ARG, OPUS_BANDWIDTH_FULLBAND,
62 OPUS_BANDWIDTH_MEDIUMBAND, OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_SUPERWIDEBAND,
63 OPUS_BANDWIDTH_WIDEBAND, OPUS_BITRATE_MAX, OPUS_BUFFER_TOO_SMALL, OPUS_FRAMESIZE_100_MS,
64 OPUS_FRAMESIZE_10_MS, OPUS_FRAMESIZE_120_MS, OPUS_FRAMESIZE_20_MS, OPUS_FRAMESIZE_2_5_MS,
65 OPUS_FRAMESIZE_40_MS, OPUS_FRAMESIZE_5_MS, OPUS_FRAMESIZE_60_MS, OPUS_FRAMESIZE_80_MS,
66 OPUS_FRAMESIZE_ARG, OPUS_GET_APPLICATION_REQUEST, OPUS_GET_BANDWIDTH_REQUEST,
67 OPUS_GET_BITRATE_REQUEST, OPUS_GET_COMPLEXITY_REQUEST, OPUS_GET_DTX_REQUEST,
68 OPUS_GET_EXPERT_FRAME_DURATION_REQUEST, OPUS_GET_FINAL_RANGE_REQUEST,
69 OPUS_GET_FORCE_CHANNELS_REQUEST, OPUS_GET_INBAND_FEC_REQUEST, OPUS_GET_IN_DTX_REQUEST,
70 OPUS_GET_LOOKAHEAD_REQUEST, OPUS_GET_LSB_DEPTH_REQUEST, OPUS_GET_MAX_BANDWIDTH_REQUEST,
71 OPUS_GET_PACKET_LOSS_PERC_REQUEST, OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST,
72 OPUS_GET_PREDICTION_DISABLED_REQUEST, OPUS_GET_SAMPLE_RATE_REQUEST, OPUS_GET_SIGNAL_REQUEST,
73 OPUS_GET_VBR_CONSTRAINT_REQUEST, OPUS_GET_VBR_REQUEST, OPUS_INTERNAL_ERROR, OPUS_OK,
74 OPUS_RESET_STATE, OPUS_SET_APPLICATION_REQUEST, OPUS_SET_BANDWIDTH_REQUEST,
75 OPUS_SET_BITRATE_REQUEST, OPUS_SET_COMPLEXITY_REQUEST, OPUS_SET_DTX_REQUEST,
76 OPUS_SET_EXPERT_FRAME_DURATION_REQUEST, OPUS_SET_FORCE_CHANNELS_REQUEST,
77 OPUS_SET_INBAND_FEC_REQUEST, OPUS_SET_LSB_DEPTH_REQUEST, OPUS_SET_MAX_BANDWIDTH_REQUEST,
78 OPUS_SET_PACKET_LOSS_PERC_REQUEST, OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST,
79 OPUS_SET_PREDICTION_DISABLED_REQUEST, OPUS_SET_SIGNAL_REQUEST, OPUS_SET_VBR_CONSTRAINT_REQUEST,
80 OPUS_SET_VBR_REQUEST, OPUS_SIGNAL_MUSIC, OPUS_SIGNAL_VOICE, OPUS_UNIMPLEMENTED,
81};
82use crate::src::opus_private::{
83 align, MODE_CELT_ONLY, MODE_HYBRID, MODE_SILK_ONLY, OPUS_GET_VOICE_RATIO_REQUEST,
84 OPUS_SET_FORCE_MODE_REQUEST, OPUS_SET_VOICE_RATIO_REQUEST,
85};
86use crate::varargs::VarArgs;
87use crate::{
88 opus_custom_encoder_ctl, opus_packet_pad, opus_repacketizer_cat, opus_repacketizer_init,
89 opus_repacketizer_out_range_impl, OpusRepacketizer,
90};
91
92#[derive(Copy, Clone)]
93#[repr(C)]
94pub struct OpusEncoder {
95 pub(crate) celt_enc_offset: i32,
96 pub(crate) silk_enc_offset: i32,
97 pub(crate) silk_mode: silk_EncControlStruct,
98 pub(crate) application: i32,
99 pub(crate) channels: i32,
100 pub(crate) delay_compensation: i32,
101 pub(crate) force_channels: i32,
102 pub(crate) signal_type: i32,
103 pub(crate) user_bandwidth: i32,
104 pub(crate) max_bandwidth: i32,
105 pub(crate) user_forced_mode: i32,
106 pub(crate) voice_ratio: i32,
107 pub(crate) Fs: i32,
108 pub(crate) use_vbr: i32,
109 pub(crate) vbr_constraint: i32,
110 pub(crate) variable_duration: i32,
111 pub(crate) bitrate_bps: i32,
112 pub(crate) user_bitrate_bps: i32,
113 pub(crate) lsb_depth: i32,
114 pub(crate) encoder_buffer: i32,
115 pub(crate) lfe: i32,
116 pub(crate) arch: i32,
117 pub(crate) use_dtx: i32,
118 pub(crate) analysis: TonalityAnalysisState,
119 pub(crate) stream_channels: i32,
120 pub(crate) hybrid_stereo_width_Q14: i16,
121 pub(crate) variable_HP_smth2_Q15: i32,
122 pub(crate) prev_HB_gain: opus_val16,
123 pub(crate) hp_mem: [opus_val32; 4],
124 pub(crate) mode: i32,
125 pub(crate) prev_mode: i32,
126 pub(crate) prev_channels: i32,
127 pub(crate) prev_framesize: i32,
128 pub(crate) bandwidth: i32,
129 pub(crate) auto_bandwidth: i32,
130 pub(crate) silk_bw_switch: i32,
131 pub(crate) first: i32,
132 pub(crate) energy_masking: *mut opus_val16,
133 pub(crate) width_mem: StereoWidthState,
134 pub(crate) delay_buffer: [opus_val16; 960],
135 pub(crate) detected_bandwidth: i32,
136 pub(crate) nb_no_activity_frames: i32,
137 pub(crate) peak_signal_energy: opus_val32,
138 pub(crate) nonfinal_frame: i32,
139 pub(crate) rangeFinal: u32,
140}
141#[derive(Copy, Clone)]
142#[repr(C)]
143pub struct StereoWidthState {
144 pub XX: opus_val32,
145 pub XY: opus_val32,
146 pub YY: opus_val32,
147 pub smoothed_width: opus_val16,
148 pub max_follower: opus_val16,
149}
150pub const PSEUDO_SNR_THRESHOLD: f32 = 316.23f32;
151static mut mono_voice_bandwidth_thresholds: [i32; 8] =
152 [9000, 700, 9000, 700, 13500, 1000, 14000, 2000];
153static mut mono_music_bandwidth_thresholds: [i32; 8] =
154 [9000, 700, 9000, 700, 11000, 1000, 12000, 2000];
155static mut stereo_voice_bandwidth_thresholds: [i32; 8] =
156 [9000, 700, 9000, 700, 13500, 1000, 14000, 2000];
157static mut stereo_music_bandwidth_thresholds: [i32; 8] =
158 [9000, 700, 9000, 700, 11000, 1000, 12000, 2000];
159static mut stereo_voice_threshold: i32 = 19000;
160static mut stereo_music_threshold: i32 = 17000;
161static mut mode_thresholds: [[i32; 2]; 2] = [[64000, 10000], [44000, 10000]];
162static mut fec_thresholds: [i32; 10] = [
163 12000, 1000, 14000, 1000, 16000, 1000, 20000, 1000, 22000, 1000,
164];
165pub unsafe fn opus_encoder_get_size(channels: i32) -> i32 {
166 let mut silkEncSizeBytes: i32 = 0;
167 let mut celtEncSizeBytes: i32 = 0;
168 let mut ret: i32 = 0;
169 if channels < 1 || channels > 2 {
170 return 0;
171 }
172 ret = silk_Get_Encoder_Size(&mut silkEncSizeBytes);
173 if ret != 0 {
174 return 0;
175 }
176 silkEncSizeBytes = align(silkEncSizeBytes);
177 celtEncSizeBytes = celt_encoder_get_size(channels);
178 return align(::core::mem::size_of::<OpusEncoder>() as u64 as i32)
179 + silkEncSizeBytes
180 + celtEncSizeBytes;
181}
182pub unsafe fn opus_encoder_init(
183 st: *mut OpusEncoder,
184 Fs: i32,
185 channels: i32,
186 application: i32,
187) -> i32 {
188 let mut silk_enc: *mut core::ffi::c_void = 0 as *mut core::ffi::c_void;
189 let mut celt_enc: *mut OpusCustomEncoder = 0 as *mut OpusCustomEncoder;
190 let mut err: i32 = 0;
191 let mut ret: i32 = 0;
192 let mut silkEncSizeBytes: i32 = 0;
193 if Fs != 48000 && Fs != 24000 && Fs != 16000 && Fs != 12000 && Fs != 8000
194 || channels != 1 && channels != 2
195 || application != OPUS_APPLICATION_VOIP
196 && application != OPUS_APPLICATION_AUDIO
197 && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY
198 {
199 return OPUS_BAD_ARG;
200 }
201 memset(
202 st as *mut i8 as *mut core::ffi::c_void,
203 0,
204 (opus_encoder_get_size(channels) as u64).wrapping_mul(::core::mem::size_of::<i8>() as u64),
205 );
206 ret = silk_Get_Encoder_Size(&mut silkEncSizeBytes);
207 if ret != 0 {
208 return OPUS_BAD_ARG;
209 }
210 silkEncSizeBytes = align(silkEncSizeBytes);
211 (*st).silk_enc_offset = align(::core::mem::size_of::<OpusEncoder>() as u64 as i32);
212 (*st).celt_enc_offset = (*st).silk_enc_offset + silkEncSizeBytes;
213 silk_enc = (st as *mut i8).offset((*st).silk_enc_offset as isize) as *mut core::ffi::c_void;
214 celt_enc = (st as *mut i8).offset((*st).celt_enc_offset as isize) as *mut OpusCustomEncoder;
215 (*st).channels = channels;
216 (*st).stream_channels = (*st).channels;
217 (*st).Fs = Fs;
218 (*st).arch = opus_select_arch();
219 ret = silk_InitEncoder(silk_enc, (*st).arch, &mut (*st).silk_mode);
220 if ret != 0 {
221 return OPUS_INTERNAL_ERROR;
222 }
223 (*st).silk_mode.nChannelsAPI = channels;
224 (*st).silk_mode.nChannelsInternal = channels;
225 (*st).silk_mode.API_sampleRate = (*st).Fs;
226 (*st).silk_mode.maxInternalSampleRate = 16000;
227 (*st).silk_mode.minInternalSampleRate = 8000;
228 (*st).silk_mode.desiredInternalSampleRate = 16000;
229 (*st).silk_mode.payloadSize_ms = 20;
230 (*st).silk_mode.bitRate = 25000;
231 (*st).silk_mode.packetLossPercentage = 0;
232 (*st).silk_mode.complexity = 9;
233 (*st).silk_mode.useInBandFEC = 0;
234 (*st).silk_mode.useDTX = 0;
235 (*st).silk_mode.useCBR = 0;
236 (*st).silk_mode.reducedDependency = 0;
237 err = celt_encoder_init(celt_enc, Fs, channels, (*st).arch);
238 if err != OPUS_OK {
239 return OPUS_INTERNAL_ERROR;
240 }
241 opus_custom_encoder_ctl!(celt_enc, CELT_SET_SIGNALLING_REQUEST, 0);
242 opus_custom_encoder_ctl!(
243 celt_enc,
244 OPUS_SET_COMPLEXITY_REQUEST,
245 (*st).silk_mode.complexity,
246 );
247 (*st).use_vbr = 1;
248 (*st).vbr_constraint = 1;
249 (*st).user_bitrate_bps = OPUS_AUTO;
250 (*st).bitrate_bps = 3000 + Fs * channels;
251 (*st).application = application;
252 (*st).signal_type = OPUS_AUTO;
253 (*st).user_bandwidth = OPUS_AUTO;
254 (*st).max_bandwidth = OPUS_BANDWIDTH_FULLBAND;
255 (*st).force_channels = OPUS_AUTO;
256 (*st).user_forced_mode = OPUS_AUTO;
257 (*st).voice_ratio = -1;
258 (*st).encoder_buffer = (*st).Fs / 100;
259 (*st).lsb_depth = 24;
260 (*st).variable_duration = OPUS_FRAMESIZE_ARG;
261 (*st).delay_compensation = (*st).Fs / 250;
262 (*st).hybrid_stereo_width_Q14 = ((1) << 14) as i16;
263 (*st).prev_HB_gain = Q15ONE;
264 (*st).variable_HP_smth2_Q15 = ((silk_lin2log(60) as u32) << 8) as i32;
265 (*st).first = 1;
266 (*st).mode = MODE_HYBRID;
267 (*st).bandwidth = OPUS_BANDWIDTH_FULLBAND;
268 tonality_analysis_init(&mut (*st).analysis, (*st).Fs);
269 (*st).analysis.application = (*st).application;
270 return OPUS_OK;
271}
272unsafe fn gen_toc(mode: i32, mut framerate: i32, bandwidth: i32, channels: i32) -> u8 {
273 let mut period: i32 = 0;
274 let mut toc: u8 = 0;
275 period = 0;
276 while framerate < 400 {
277 framerate <<= 1;
278 period += 1;
279 }
280 if mode == MODE_SILK_ONLY {
281 toc = (bandwidth - OPUS_BANDWIDTH_NARROWBAND << 5) as u8;
282 toc = (toc as i32 | (period - 2) << 3) as u8;
283 } else if mode == MODE_CELT_ONLY {
284 let mut tmp: i32 = bandwidth - OPUS_BANDWIDTH_MEDIUMBAND;
285 if tmp < 0 {
286 tmp = 0;
287 }
288 toc = 0x80;
289 toc = (toc as i32 | tmp << 5) as u8;
290 toc = (toc as i32 | period << 3) as u8;
291 } else {
292 toc = 0x60;
293 toc = (toc as i32 | bandwidth - OPUS_BANDWIDTH_SUPERWIDEBAND << 4) as u8;
294 toc = (toc as i32 | (period - 2) << 3) as u8;
295 }
296 toc = (toc as i32 | ((channels == 2) as i32) << 2) as u8;
297 return toc;
298}
299unsafe fn silk_biquad_float(
300 in_0: *const opus_val16,
301 B_Q28: *const i32,
302 A_Q28: *const i32,
303 S: *mut opus_val32,
304 out: *mut opus_val16,
305 len: i32,
306 stride: i32,
307) {
308 let mut k: i32 = 0;
309 let mut vout: opus_val32 = 0.;
310 let mut inval: opus_val32 = 0.;
311 let mut A: [opus_val32; 2] = [0.; 2];
312 let mut B: [opus_val32; 3] = [0.; 3];
313 A[0 as usize] = *A_Q28.offset(0 as isize) as f32 * (1.0f32 / ((1) << 28) as f32);
314 A[1 as usize] = *A_Q28.offset(1 as isize) as f32 * (1.0f32 / ((1) << 28) as f32);
315 B[0 as usize] = *B_Q28.offset(0 as isize) as f32 * (1.0f32 / ((1) << 28) as f32);
316 B[1 as usize] = *B_Q28.offset(1 as isize) as f32 * (1.0f32 / ((1) << 28) as f32);
317 B[2 as usize] = *B_Q28.offset(2 as isize) as f32 * (1.0f32 / ((1) << 28) as f32);
318 k = 0;
319 while k < len {
320 inval = *in_0.offset((k * stride) as isize);
321 vout = *S.offset(0 as isize) + B[0 as usize] * inval;
322 *S.offset(0 as isize) =
323 *S.offset(1 as isize) - vout * A[0 as usize] + B[1 as usize] * inval;
324 *S.offset(1 as isize) = -vout * A[1 as usize] + B[2 as usize] * inval + VERY_SMALL;
325 *out.offset((k * stride) as isize) = vout;
326 k += 1;
327 }
328}
329unsafe fn hp_cutoff(
330 in_0: *const opus_val16,
331 cutoff_Hz: i32,
332 out: *mut opus_val16,
333 hp_mem: *mut opus_val32,
334 len: i32,
335 channels: i32,
336 Fs: i32,
337 _arch: i32,
338) {
339 let mut B_Q28: [i32; 3] = [0; 3];
340 let mut A_Q28: [i32; 2] = [0; 2];
341 let mut Fc_Q19: i32 = 0;
342 let mut r_Q28: i32 = 0;
343 let mut r_Q22: i32 = 0;
344 Fc_Q19 = (1.5f64 * 3.14159f64 / 1000 as f64 * ((1) << 19) as f64 + 0.5f64) as i32 as i16 as i32
345 * cutoff_Hz as i16 as i32
346 / (Fs / 1000);
347 r_Q28 = (1.0f64 * ((1) << 28) as f64 + 0.5f64) as i32
348 - (0.92f64 * ((1) << 9) as f64 + 0.5f64) as i32 * Fc_Q19;
349 B_Q28[0 as usize] = r_Q28;
350 B_Q28[1 as usize] = ((-r_Q28 as u32) << 1) as i32;
351 B_Q28[2 as usize] = r_Q28;
352 r_Q22 = r_Q28 >> 6;
353 A_Q28[0 as usize] = (r_Q22 as i64
354 * ((Fc_Q19 as i64 * Fc_Q19 as i64 >> 16) as i32
355 - (2.0f64 * ((1) << 22) as f64 + 0.5f64) as i32) as i64
356 >> 16) as i32;
357 A_Q28[1 as usize] = (r_Q22 as i64 * r_Q22 as i64 >> 16) as i32;
358 silk_biquad_float(
359 in_0,
360 B_Q28.as_mut_ptr(),
361 A_Q28.as_mut_ptr(),
362 hp_mem,
363 out,
364 len,
365 channels,
366 );
367 if channels == 2 {
368 silk_biquad_float(
369 in_0.offset(1 as isize),
370 B_Q28.as_mut_ptr(),
371 A_Q28.as_mut_ptr(),
372 hp_mem.offset(2 as isize),
373 out.offset(1 as isize),
374 len,
375 channels,
376 );
377 }
378}
379unsafe fn dc_reject(
380 in_0: *const opus_val16,
381 cutoff_Hz: i32,
382 out: *mut opus_val16,
383 hp_mem: *mut opus_val32,
384 len: i32,
385 channels: i32,
386 Fs: i32,
387) {
388 let mut i: i32 = 0;
389 let mut coef: f32 = 0.;
390 let mut coef2: f32 = 0.;
391 coef = 6.3f32 * cutoff_Hz as f32 / Fs as f32;
392 coef2 = 1 as f32 - coef;
393 if channels == 2 {
394 let mut m0: f32 = 0.;
395 let mut m2: f32 = 0.;
396 m0 = *hp_mem.offset(0 as isize);
397 m2 = *hp_mem.offset(2 as isize);
398 i = 0;
399 while i < len {
400 let mut x0: opus_val32 = 0.;
401 let mut x1: opus_val32 = 0.;
402 let mut out0: opus_val32 = 0.;
403 let mut out1: opus_val32 = 0.;
404 x0 = *in_0.offset((2 * i + 0) as isize);
405 x1 = *in_0.offset((2 * i + 1) as isize);
406 out0 = x0 - m0;
407 out1 = x1 - m2;
408 m0 = coef * x0 + VERY_SMALL + coef2 * m0;
409 m2 = coef * x1 + VERY_SMALL + coef2 * m2;
410 *out.offset((2 * i + 0) as isize) = out0;
411 *out.offset((2 * i + 1) as isize) = out1;
412 i += 1;
413 }
414 *hp_mem.offset(0 as isize) = m0;
415 *hp_mem.offset(2 as isize) = m2;
416 } else {
417 let mut m0_0: f32 = 0.;
418 m0_0 = *hp_mem.offset(0 as isize);
419 i = 0;
420 while i < len {
421 let mut x: opus_val32 = 0.;
422 let mut y: opus_val32 = 0.;
423 x = *in_0.offset(i as isize);
424 y = x - m0_0;
425 m0_0 = coef * x + VERY_SMALL + coef2 * m0_0;
426 *out.offset(i as isize) = y;
427 i += 1;
428 }
429 *hp_mem.offset(0 as isize) = m0_0;
430 };
431}
432unsafe fn stereo_fade(
433 in_0: *const opus_val16,
434 out: *mut opus_val16,
435 mut g1: opus_val16,
436 mut g2: opus_val16,
437 overlap48: i32,
438 frame_size: i32,
439 channels: i32,
440 window: *const opus_val16,
441 Fs: i32,
442) {
443 let mut i: i32 = 0;
444 let mut overlap: i32 = 0;
445 let mut inc: i32 = 0;
446 inc = 48000 / Fs;
447 overlap = overlap48 / inc;
448 g1 = Q15ONE - g1;
449 g2 = Q15ONE - g2;
450 i = 0;
451 while i < overlap {
452 let mut diff: opus_val32 = 0.;
453 let mut g: opus_val16 = 0.;
454 let mut w: opus_val16 = 0.;
455 w = *window.offset((i * inc) as isize) * *window.offset((i * inc) as isize);
456 g = w * g2 + (1.0f32 - w) * g1;
457 diff = 0.5f32
458 * (*in_0.offset((i * channels) as isize) - *in_0.offset((i * channels + 1) as isize));
459 diff = g * diff;
460 *out.offset((i * channels) as isize) = *out.offset((i * channels) as isize) - diff;
461 *out.offset((i * channels + 1) as isize) = *out.offset((i * channels + 1) as isize) + diff;
462 i += 1;
463 }
464 while i < frame_size {
465 let mut diff_0: opus_val32 = 0.;
466 diff_0 = 0.5f32
467 * (*in_0.offset((i * channels) as isize) - *in_0.offset((i * channels + 1) as isize));
468 diff_0 = g2 * diff_0;
469 *out.offset((i * channels) as isize) = *out.offset((i * channels) as isize) - diff_0;
470 *out.offset((i * channels + 1) as isize) =
471 *out.offset((i * channels + 1) as isize) + diff_0;
472 i += 1;
473 }
474}
475unsafe fn gain_fade(
476 in_0: *const opus_val16,
477 out: *mut opus_val16,
478 g1: opus_val16,
479 g2: opus_val16,
480 overlap48: i32,
481 frame_size: i32,
482 channels: i32,
483 window: *const opus_val16,
484 Fs: i32,
485) {
486 let mut i: i32 = 0;
487 let mut inc: i32 = 0;
488 let mut overlap: i32 = 0;
489 let mut c: i32 = 0;
490 inc = 48000 / Fs;
491 overlap = overlap48 / inc;
492 if channels == 1 {
493 i = 0;
494 while i < overlap {
495 let mut g: opus_val16 = 0.;
496 let mut w: opus_val16 = 0.;
497 w = *window.offset((i * inc) as isize) * *window.offset((i * inc) as isize);
498 g = w * g2 + (1.0f32 - w) * g1;
499 *out.offset(i as isize) = g * *in_0.offset(i as isize);
500 i += 1;
501 }
502 } else {
503 i = 0;
504 while i < overlap {
505 let mut g_0: opus_val16 = 0.;
506 let mut w_0: opus_val16 = 0.;
507 w_0 = *window.offset((i * inc) as isize) * *window.offset((i * inc) as isize);
508 g_0 = w_0 * g2 + (1.0f32 - w_0) * g1;
509 *out.offset((i * 2) as isize) = g_0 * *in_0.offset((i * 2) as isize);
510 *out.offset((i * 2 + 1) as isize) = g_0 * *in_0.offset((i * 2 + 1) as isize);
511 i += 1;
512 }
513 }
514 c = 0;
515 loop {
516 i = overlap;
517 while i < frame_size {
518 *out.offset((i * channels + c) as isize) =
519 g2 * *in_0.offset((i * channels + c) as isize);
520 i += 1;
521 }
522 c += 1;
523 if !(c < channels) {
524 break;
525 }
526 }
527}
528pub unsafe fn opus_encoder_create(
529 Fs: i32,
530 channels: i32,
531 application: i32,
532 error: *mut i32,
533) -> *mut OpusEncoder {
534 let mut ret: i32 = 0;
535 let mut st: *mut OpusEncoder = 0 as *mut OpusEncoder;
536 if Fs != 48000 && Fs != 24000 && Fs != 16000 && Fs != 12000 && Fs != 8000
537 || channels != 1 && channels != 2
538 || application != OPUS_APPLICATION_VOIP
539 && application != OPUS_APPLICATION_AUDIO
540 && application != OPUS_APPLICATION_RESTRICTED_LOWDELAY
541 {
542 if !error.is_null() {
543 *error = OPUS_BAD_ARG;
544 }
545 return NULL as *mut OpusEncoder;
546 }
547 st = malloc(opus_encoder_get_size(channels) as size_t) as *mut OpusEncoder;
548 if st.is_null() {
549 if !error.is_null() {
550 *error = OPUS_ALLOC_FAIL;
551 }
552 return NULL as *mut OpusEncoder;
553 }
554 ret = opus_encoder_init(st, Fs, channels, application);
555 if !error.is_null() {
556 *error = ret;
557 }
558 if ret != OPUS_OK {
559 free(st as *mut core::ffi::c_void);
560 st = NULL as *mut OpusEncoder;
561 }
562 return st;
563}
564unsafe fn user_bitrate_to_bitrate(
565 st: *mut OpusEncoder,
566 mut frame_size: i32,
567 max_data_bytes: i32,
568) -> i32 {
569 if frame_size == 0 {
570 frame_size = (*st).Fs / 400;
571 }
572 if (*st).user_bitrate_bps == OPUS_AUTO {
573 return 60 * (*st).Fs / frame_size + (*st).Fs * (*st).channels;
574 } else if (*st).user_bitrate_bps == OPUS_BITRATE_MAX {
575 return max_data_bytes * 8 * (*st).Fs / frame_size;
576 } else {
577 return (*st).user_bitrate_bps;
578 };
579}
580pub unsafe fn downmix_float(
581 mut _x: *const core::ffi::c_void,
582 y: *mut opus_val32,
583 subframe: i32,
584 offset: i32,
585 c1: i32,
586 c2: i32,
587 C: i32,
588) {
589 let mut x: *const f32 = 0 as *const f32;
590 let mut j: i32 = 0;
591 x = _x as *const f32;
592 j = 0;
593 while j < subframe {
594 *y.offset(j as isize) = *x.offset(((j + offset) * C + c1) as isize) * CELT_SIG_SCALE;
595 j += 1;
596 }
597 if c2 > -1 {
598 j = 0;
599 while j < subframe {
600 let ref mut fresh0 = *y.offset(j as isize);
601 *fresh0 += *x.offset(((j + offset) * C + c2) as isize) * CELT_SIG_SCALE;
602 j += 1;
603 }
604 } else if c2 == -(2) {
605 let mut c: i32 = 0;
606 c = 1;
607 while c < C {
608 j = 0;
609 while j < subframe {
610 let ref mut fresh1 = *y.offset(j as isize);
611 *fresh1 += *x.offset(((j + offset) * C + c) as isize) * CELT_SIG_SCALE;
612 j += 1;
613 }
614 c += 1;
615 }
616 }
617}
618pub unsafe fn downmix_int(
619 mut _x: *const core::ffi::c_void,
620 y: *mut opus_val32,
621 subframe: i32,
622 offset: i32,
623 c1: i32,
624 c2: i32,
625 C: i32,
626) {
627 let mut x: *const i16 = 0 as *const i16;
628 let mut j: i32 = 0;
629 x = _x as *const i16;
630 j = 0;
631 while j < subframe {
632 *y.offset(j as isize) = *x.offset(((j + offset) * C + c1) as isize) as opus_val32;
633 j += 1;
634 }
635 if c2 > -1 {
636 j = 0;
637 while j < subframe {
638 let ref mut fresh2 = *y.offset(j as isize);
639 *fresh2 += *x.offset(((j + offset) * C + c2) as isize) as i32 as f32;
640 j += 1;
641 }
642 } else if c2 == -(2) {
643 let mut c: i32 = 0;
644 c = 1;
645 while c < C {
646 j = 0;
647 while j < subframe {
648 let ref mut fresh3 = *y.offset(j as isize);
649 *fresh3 += *x.offset(((j + offset) * C + c) as isize) as i32 as f32;
650 j += 1;
651 }
652 c += 1;
653 }
654 }
655}
656pub unsafe fn frame_size_select(frame_size: i32, variable_duration: i32, Fs: i32) -> i32 {
657 let mut new_size: i32 = 0;
658 if frame_size < Fs / 400 {
659 return -1;
660 }
661 if variable_duration == OPUS_FRAMESIZE_ARG {
662 new_size = frame_size;
663 } else if variable_duration >= OPUS_FRAMESIZE_2_5_MS
664 && variable_duration <= OPUS_FRAMESIZE_120_MS
665 {
666 if variable_duration <= OPUS_FRAMESIZE_40_MS {
667 new_size = (Fs / 400) << variable_duration - OPUS_FRAMESIZE_2_5_MS;
668 } else {
669 new_size = (variable_duration - OPUS_FRAMESIZE_2_5_MS - 2) * Fs / 50;
670 }
671 } else {
672 return -1;
673 }
674 if new_size > frame_size {
675 return -1;
676 }
677 if 400 * new_size != Fs
678 && 200 * new_size != Fs
679 && 100 * new_size != Fs
680 && 50 * new_size != Fs
681 && 25 * new_size != Fs
682 && 50 * new_size != 3 * Fs
683 && 50 * new_size != 4 * Fs
684 && 50 * new_size != 5 * Fs
685 && 50 * new_size != 6 * Fs
686 {
687 return -1;
688 }
689 return new_size;
690}
691pub unsafe fn compute_stereo_width(
692 pcm: *const opus_val16,
693 frame_size: i32,
694 Fs: i32,
695 mem: *mut StereoWidthState,
696) -> opus_val16 {
697 let mut xx: opus_val32 = 0.;
698 let mut xy: opus_val32 = 0.;
699 let mut yy: opus_val32 = 0.;
700 let mut sqrt_xx: opus_val16 = 0.;
701 let mut sqrt_yy: opus_val16 = 0.;
702 let mut qrrt_xx: opus_val16 = 0.;
703 let mut qrrt_yy: opus_val16 = 0.;
704 let mut frame_rate: i32 = 0;
705 let mut i: i32 = 0;
706 let mut short_alpha: opus_val16 = 0.;
707 frame_rate = Fs / frame_size;
708 short_alpha =
709 Q15ONE - 25 as opus_val32 * 1.0f32 / (if 50 > frame_rate { 50 } else { frame_rate }) as f32;
710 yy = 0 as opus_val32;
711 xy = yy;
712 xx = xy;
713 i = 0;
714 while i < frame_size - 3 {
715 let mut pxx: opus_val32 = 0 as opus_val32;
716 let mut pxy: opus_val32 = 0 as opus_val32;
717 let mut pyy: opus_val32 = 0 as opus_val32;
718 let mut x: opus_val16 = 0.;
719 let mut y: opus_val16 = 0.;
720 x = *pcm.offset((2 * i) as isize);
721 y = *pcm.offset((2 * i + 1) as isize);
722 pxx = x * x;
723 pxy = x * y;
724 pyy = y * y;
725 x = *pcm.offset((2 * i + 2) as isize);
726 y = *pcm.offset((2 * i + 3) as isize);
727 pxx += x * x;
728 pxy += x * y;
729 pyy += y * y;
730 x = *pcm.offset((2 * i + 4) as isize);
731 y = *pcm.offset((2 * i + 5) as isize);
732 pxx += x * x;
733 pxy += x * y;
734 pyy += y * y;
735 x = *pcm.offset((2 * i + 6) as isize);
736 y = *pcm.offset((2 * i + 7) as isize);
737 pxx += x * x;
738 pxy += x * y;
739 pyy += y * y;
740 xx += pxx;
741 xy += pxy;
742 yy += pyy;
743 i += 4;
744 }
745 if !(xx < 1e9f32) || xx != xx || !(yy < 1e9f32) || yy != yy {
746 yy = 0 as opus_val32;
747 xx = yy;
748 xy = xx;
749 }
750 (*mem).XX += short_alpha * (xx - (*mem).XX);
751 (*mem).XY += short_alpha * (xy - (*mem).XY);
752 (*mem).YY += short_alpha * (yy - (*mem).YY);
753 (*mem).XX = if 0 as f32 > (*mem).XX {
754 0 as f32
755 } else {
756 (*mem).XX
757 };
758 (*mem).XY = if 0 as f32 > (*mem).XY {
759 0 as f32
760 } else {
761 (*mem).XY
762 };
763 (*mem).YY = if 0 as f32 > (*mem).YY {
764 0 as f32
765 } else {
766 (*mem).YY
767 };
768 if (if (*mem).XX > (*mem).YY {
769 (*mem).XX
770 } else {
771 (*mem).YY
772 }) > 8e-4f32
773 {
774 let mut corr: opus_val16 = 0.;
775 let mut ldiff: opus_val16 = 0.;
776 let mut width: opus_val16 = 0.;
777 sqrt_xx = celt_sqrt((*mem).XX);
778 sqrt_yy = celt_sqrt((*mem).YY);
779 qrrt_xx = celt_sqrt(sqrt_xx);
780 qrrt_yy = celt_sqrt(sqrt_yy);
781 (*mem).XY = if (*mem).XY < sqrt_xx * sqrt_yy {
782 (*mem).XY
783 } else {
784 sqrt_xx * sqrt_yy
785 };
786 corr = (*mem).XY / (1e-15f32 + sqrt_xx * sqrt_yy);
787 ldiff = 1.0f32 * (qrrt_xx - qrrt_yy).abs() / (EPSILON + qrrt_xx + qrrt_yy);
788 width = celt_sqrt(1.0f32 - corr * corr) * ldiff;
789 (*mem).smoothed_width += (width - (*mem).smoothed_width) / frame_rate as f32;
790 (*mem).max_follower =
791 if (*mem).max_follower - 0.02f32 / frame_rate as f32 > (*mem).smoothed_width {
792 (*mem).max_follower - 0.02f32 / frame_rate as f32
793 } else {
794 (*mem).smoothed_width
795 };
796 }
797 return if 1.0f32 < 20 as opus_val32 * (*mem).max_follower {
798 1.0f32
799 } else {
800 20 as opus_val32 * (*mem).max_follower
801 };
802}
803unsafe fn decide_fec(
804 useInBandFEC: i32,
805 PacketLoss_perc: i32,
806 last_fec: i32,
807 mode: i32,
808 bandwidth: *mut i32,
809 rate: i32,
810) -> i32 {
811 let mut orig_bandwidth: i32 = 0;
812 if useInBandFEC == 0 || PacketLoss_perc == 0 || mode == MODE_CELT_ONLY {
813 return 0;
814 }
815 orig_bandwidth = *bandwidth;
816 loop {
817 let mut hysteresis: i32 = 0;
818 let mut LBRR_rate_thres_bps: i32 = 0;
819 LBRR_rate_thres_bps =
820 fec_thresholds[(2 * (*bandwidth - OPUS_BANDWIDTH_NARROWBAND)) as usize];
821 hysteresis = fec_thresholds[(2 * (*bandwidth - OPUS_BANDWIDTH_NARROWBAND) + 1) as usize];
822 if last_fec == 1 {
823 LBRR_rate_thres_bps -= hysteresis;
824 }
825 if last_fec == 0 {
826 LBRR_rate_thres_bps += hysteresis;
827 }
828 LBRR_rate_thres_bps = ((LBRR_rate_thres_bps
829 * (125
830 - (if PacketLoss_perc < 25 {
831 PacketLoss_perc
832 } else {
833 25
834 }))) as i64
835 * (0.01f64 * ((1) << 16) as f64 + 0.5f64) as i32 as i16 as i64
836 >> 16) as i32;
837 if rate > LBRR_rate_thres_bps {
838 return 1;
839 } else if PacketLoss_perc <= 5 {
840 return 0;
841 } else {
842 if !(*bandwidth > OPUS_BANDWIDTH_NARROWBAND) {
843 break;
844 }
845 *bandwidth -= 1;
846 }
847 }
848 *bandwidth = orig_bandwidth;
849 return 0;
850}
851unsafe fn compute_silk_rate_for_hybrid(
852 mut rate: i32,
853 bandwidth: i32,
854 frame20ms: i32,
855 vbr: i32,
856 fec: i32,
857 channels: i32,
858) -> i32 {
859 let mut entry: i32 = 0;
860 let mut i: i32 = 0;
861 let mut N: i32 = 0;
862 let mut silk_rate: i32 = 0;
863 static mut rate_table: [[i32; 5]; 7] = [
864 [0, 0, 0, 0, 0],
865 [12000, 10000, 10000, 11000, 11000],
866 [16000, 13500, 13500, 15000, 15000],
867 [20000, 16000, 16000, 18000, 18000],
868 [24000, 18000, 18000, 21000, 21000],
869 [32000, 22000, 22000, 28000, 28000],
870 [64000, 38000, 38000, 50000, 50000],
871 ];
872 rate /= channels;
873 entry = 1 + frame20ms + 2 * fec;
874 N = (::core::mem::size_of::<[[i32; 5]; 7]>() as u64)
875 .wrapping_div(::core::mem::size_of::<[i32; 5]>() as u64) as i32;
876 i = 1;
877 while i < N {
878 if rate_table[i as usize][0 as usize] > rate {
879 break;
880 }
881 i += 1;
882 }
883 if i == N {
884 silk_rate = rate_table[(i - 1) as usize][entry as usize];
885 silk_rate += (rate - rate_table[(i - 1) as usize][0 as usize]) / 2;
886 } else {
887 let mut lo: i32 = 0;
888 let mut hi: i32 = 0;
889 let mut x0: i32 = 0;
890 let mut x1: i32 = 0;
891 lo = rate_table[(i - 1) as usize][entry as usize];
892 hi = rate_table[i as usize][entry as usize];
893 x0 = rate_table[(i - 1) as usize][0 as usize];
894 x1 = rate_table[i as usize][0 as usize];
895 silk_rate = (lo * (x1 - rate) + hi * (rate - x0)) / (x1 - x0);
896 }
897 if vbr == 0 {
898 silk_rate += 100;
899 }
900 if bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND {
901 silk_rate += 300;
902 }
903 silk_rate *= channels;
904 if channels == 2 && rate >= 12000 {
905 silk_rate -= 1000;
906 }
907 return silk_rate;
908}
909unsafe fn compute_equiv_rate(
910 bitrate: i32,
911 channels: i32,
912 frame_rate: i32,
913 vbr: i32,
914 mode: i32,
915 complexity: i32,
916 loss: i32,
917) -> i32 {
918 let mut equiv: i32 = 0;
919 equiv = bitrate;
920 if frame_rate > 50 {
921 equiv -= (40 * channels + 20) * (frame_rate - 50);
922 }
923 if vbr == 0 {
924 equiv -= equiv / 12;
925 }
926 equiv = equiv * (90 + complexity) / 100;
927 if mode == MODE_SILK_ONLY || mode == MODE_HYBRID {
928 if complexity < 2 {
929 equiv = equiv * 4 / 5;
930 }
931 equiv -= equiv * loss / (6 * loss + 10);
932 } else if mode == MODE_CELT_ONLY {
933 if complexity < 5 {
934 equiv = equiv * 9 / 10;
935 }
936 } else {
937 equiv -= equiv * loss / (12 * loss + 20);
938 }
939 return equiv;
940}
941pub unsafe fn is_digital_silence(
942 pcm: *const opus_val16,
943 frame_size: i32,
944 channels: i32,
945 lsb_depth: i32,
946) -> i32 {
947 let mut silence: i32 = 0;
948 let mut sample_max: opus_val32 = 0 as opus_val32;
949 sample_max = celt_maxabs16(pcm, frame_size * channels);
950 silence = (sample_max <= 1 as opus_val16 / ((1) << lsb_depth) as f32) as i32;
951 return silence;
952}
953unsafe fn compute_frame_energy(
954 pcm: *const opus_val16,
955 frame_size: i32,
956 channels: i32,
957 _arch: i32,
958) -> opus_val32 {
959 let len: i32 = frame_size * channels;
960 return celt_inner_prod_c(pcm, pcm, len) / len as f32;
961}
962unsafe fn decide_dtx_mode(
963 activity_probability: f32,
964 nb_no_activity_frames: *mut i32,
965 peak_signal_energy: opus_val32,
966 pcm: *const opus_val16,
967 frame_size: i32,
968 channels: i32,
969 mut is_silence: i32,
970 arch: i32,
971) -> i32 {
972 let mut noise_energy: opus_val32 = 0.;
973 if is_silence == 0 {
974 if activity_probability < DTX_ACTIVITY_THRESHOLD {
975 noise_energy = compute_frame_energy(pcm, frame_size, channels, arch);
976 is_silence = (peak_signal_energy >= PSEUDO_SNR_THRESHOLD * noise_energy) as i32;
977 }
978 }
979 if is_silence != 0 {
980 *nb_no_activity_frames += 1;
981 if *nb_no_activity_frames > NB_SPEECH_FRAMES_BEFORE_DTX {
982 if *nb_no_activity_frames <= NB_SPEECH_FRAMES_BEFORE_DTX + MAX_CONSECUTIVE_DTX {
983 return 1;
984 } else {
985 *nb_no_activity_frames = NB_SPEECH_FRAMES_BEFORE_DTX;
986 }
987 }
988 } else {
989 *nb_no_activity_frames = 0;
990 }
991 return 0;
992}
993unsafe fn encode_multiframe_packet(
994 st: *mut OpusEncoder,
995 pcm: *const opus_val16,
996 nb_frames: i32,
997 frame_size: i32,
998 data: *mut u8,
999 out_data_bytes: i32,
1000 to_celt: i32,
1001 lsb_depth: i32,
1002 float_api: i32,
1003) -> i32 {
1004 let mut i: i32 = 0;
1005 let mut ret: i32 = 0;
1006 let mut bak_mode: i32 = 0;
1007 let mut bak_bandwidth: i32 = 0;
1008 let mut bak_channels: i32 = 0;
1009 let mut bak_to_mono: i32 = 0;
1010 let mut max_header_bytes: i32 = 0;
1011 let mut bytes_per_frame: i32 = 0;
1012 let mut cbr_bytes: i32 = 0;
1013 let mut repacketize_len: i32 = 0;
1014 let mut tmp_len: i32 = 0;
1015 max_header_bytes = if nb_frames == 2 {
1016 3
1017 } else {
1018 2 + (nb_frames - 1) * 2
1019 };
1020 if (*st).use_vbr != 0 || (*st).user_bitrate_bps == OPUS_BITRATE_MAX {
1021 repacketize_len = out_data_bytes;
1022 } else {
1023 cbr_bytes = 3 * (*st).bitrate_bps / (3 * 8 * (*st).Fs / (frame_size * nb_frames));
1024 repacketize_len = if cbr_bytes < out_data_bytes {
1025 cbr_bytes
1026 } else {
1027 out_data_bytes
1028 };
1029 }
1030 bytes_per_frame = if (1276) < 1 + (repacketize_len - max_header_bytes) / nb_frames {
1031 1276
1032 } else {
1033 1 + (repacketize_len - max_header_bytes) / nb_frames
1034 };
1035 let vla = (nb_frames * bytes_per_frame) as usize;
1036 let mut tmp_data: Vec<u8> = ::std::vec::from_elem(0, vla);
1037 let mut rp: [OpusRepacketizer; 1] = [OpusRepacketizer {
1038 toc: 0,
1039 nb_frames: 0,
1040 frames: [0 as *const u8; 48],
1041 len: [0; 48],
1042 framesize: 0,
1043 }; 1];
1044 opus_repacketizer_init(rp.as_mut_ptr());
1045 bak_mode = (*st).user_forced_mode;
1046 bak_bandwidth = (*st).user_bandwidth;
1047 bak_channels = (*st).force_channels;
1048 (*st).user_forced_mode = (*st).mode;
1049 (*st).user_bandwidth = (*st).bandwidth;
1050 (*st).force_channels = (*st).stream_channels;
1051 bak_to_mono = (*st).silk_mode.toMono;
1052 if bak_to_mono != 0 {
1053 (*st).force_channels = 1;
1054 } else {
1055 (*st).prev_channels = (*st).stream_channels;
1056 }
1057 i = 0;
1058 while i < nb_frames {
1059 (*st).silk_mode.toMono = 0;
1060 (*st).nonfinal_frame = (i < nb_frames - 1) as i32;
1061 if to_celt != 0 && i == nb_frames - 1 {
1062 (*st).user_forced_mode = MODE_CELT_ONLY;
1063 }
1064 tmp_len = opus_encode_native(
1065 st,
1066 pcm.offset((i * ((*st).channels * frame_size)) as isize),
1067 frame_size,
1068 tmp_data.as_mut_ptr().offset((i * bytes_per_frame) as isize),
1069 bytes_per_frame,
1070 lsb_depth,
1071 NULL as *const core::ffi::c_void,
1072 0,
1073 0,
1074 0,
1075 0,
1076 None,
1077 float_api,
1078 );
1079 if tmp_len < 0 {
1080 return OPUS_INTERNAL_ERROR;
1081 }
1082 ret = opus_repacketizer_cat(
1083 rp.as_mut_ptr(),
1084 tmp_data.as_mut_ptr().offset((i * bytes_per_frame) as isize),
1085 tmp_len,
1086 );
1087 if ret < 0 {
1088 return OPUS_INTERNAL_ERROR;
1089 }
1090 i += 1;
1091 }
1092 ret = opus_repacketizer_out_range_impl(
1093 rp.as_mut_ptr(),
1094 0,
1095 nb_frames,
1096 data,
1097 repacketize_len,
1098 0,
1099 ((*st).use_vbr == 0) as i32,
1100 );
1101 if ret < 0 {
1102 return OPUS_INTERNAL_ERROR;
1103 }
1104 (*st).user_forced_mode = bak_mode;
1105 (*st).user_bandwidth = bak_bandwidth;
1106 (*st).force_channels = bak_channels;
1107 (*st).silk_mode.toMono = bak_to_mono;
1108 return ret;
1109}
1110unsafe fn compute_redundancy_bytes(
1111 max_data_bytes: i32,
1112 bitrate_bps: i32,
1113 frame_rate: i32,
1114 channels: i32,
1115) -> i32 {
1116 let mut redundancy_bytes_cap: i32 = 0;
1117 let mut redundancy_bytes: i32 = 0;
1118 let mut redundancy_rate: i32 = 0;
1119 let mut base_bits: i32 = 0;
1120 let mut available_bits: i32 = 0;
1121 base_bits = 40 * channels + 20;
1122 redundancy_rate = bitrate_bps + base_bits * (200 - frame_rate);
1123 redundancy_rate = 3 * redundancy_rate / 2;
1124 redundancy_bytes = redundancy_rate / 1600;
1125 available_bits = max_data_bytes * 8 - 2 * base_bits;
1126 redundancy_bytes_cap = (available_bits * 240 / (240 + 48000 / frame_rate) + base_bits) / 8;
1127 redundancy_bytes = if redundancy_bytes < redundancy_bytes_cap {
1128 redundancy_bytes
1129 } else {
1130 redundancy_bytes_cap
1131 };
1132 if redundancy_bytes > 4 + 8 * channels {
1133 redundancy_bytes = if (257) < redundancy_bytes {
1134 257
1135 } else {
1136 redundancy_bytes
1137 };
1138 } else {
1139 redundancy_bytes = 0;
1140 }
1141 return redundancy_bytes;
1142}
1143pub unsafe fn opus_encode_native(
1144 st: *mut OpusEncoder,
1145 pcm: *const opus_val16,
1146 frame_size: i32,
1147 mut data: *mut u8,
1148 out_data_bytes: i32,
1149 mut lsb_depth: i32,
1150 analysis_pcm: *const core::ffi::c_void,
1151 analysis_size: i32,
1152 c1: i32,
1153 c2: i32,
1154 analysis_channels: i32,
1155 downmix: downmix_func,
1156 float_api: i32,
1157) -> i32 {
1158 let mut silk_enc: *mut core::ffi::c_void = 0 as *mut core::ffi::c_void;
1159 let mut celt_enc: *mut OpusCustomEncoder = 0 as *mut OpusCustomEncoder;
1160 let mut i: i32 = 0;
1161 let mut ret: i32 = 0;
1162 let mut nBytes: i32 = 0;
1163 let mut enc: ec_enc = ec_enc {
1164 buf: &mut [],
1165 storage: 0,
1166 end_offs: 0,
1167 end_window: 0,
1168 nend_bits: 0,
1169 nbits_total: 0,
1170 offs: 0,
1171 rng: 0,
1172 val: 0,
1173 ext: 0,
1174 rem: 0,
1175 error: 0,
1176 };
1177 let mut bytes_target: i32 = 0;
1178 let mut prefill: i32 = 0;
1179 let mut start_band: i32 = 0;
1180 let mut redundancy: i32 = 0;
1181 let mut redundancy_bytes: i32 = 0;
1182 let mut celt_to_silk: i32 = 0;
1183 let mut nb_compr_bytes: i32 = 0;
1184 let mut to_celt: i32 = 0;
1185 let mut redundant_rng: u32 = 0;
1186 let mut cutoff_Hz: i32 = 0;
1187 let mut hp_freq_smth1: i32 = 0;
1188 let mut voice_est: i32 = 0;
1189 let mut equiv_rate: i32 = 0;
1190 let mut delay_compensation: i32 = 0;
1191 let mut frame_rate: i32 = 0;
1192 let mut max_rate: i32 = 0;
1193 let mut curr_bandwidth: i32 = 0;
1194 let mut HB_gain: opus_val16 = 0.;
1195 let mut max_data_bytes: i32 = 0;
1196 let mut total_buffer: i32 = 0;
1197 let mut stereo_width: opus_val16 = 0.;
1198 let mut celt_mode: *const OpusCustomMode = 0 as *const OpusCustomMode;
1199 let mut analysis_info: AnalysisInfo = AnalysisInfo {
1200 valid: 0,
1201 tonality: 0.,
1202 tonality_slope: 0.,
1203 noisiness: 0.,
1204 activity: 0.,
1205 music_prob: 0.,
1206 music_prob_min: 0.,
1207 music_prob_max: 0.,
1208 bandwidth: 0,
1209 activity_probability: 0.,
1210 max_pitch_ratio: 0.,
1211 leak_boost: [0; 19],
1212 };
1213 let mut analysis_read_pos_bak: i32 = -1;
1214 let mut analysis_read_subframe_bak: i32 = -1;
1215 let mut is_silence: i32 = 0;
1216 max_data_bytes = if (1276) < out_data_bytes {
1217 1276
1218 } else {
1219 out_data_bytes
1220 };
1221 (*st).rangeFinal = 0;
1222 if frame_size <= 0 || max_data_bytes <= 0 {
1223 return OPUS_BAD_ARG;
1224 }
1225 if max_data_bytes == 1 && (*st).Fs == frame_size * 10 {
1226 return OPUS_BUFFER_TOO_SMALL;
1227 }
1228 silk_enc = (st as *mut i8).offset((*st).silk_enc_offset as isize) as *mut core::ffi::c_void;
1229 celt_enc = (st as *mut i8).offset((*st).celt_enc_offset as isize) as *mut OpusCustomEncoder;
1230 if (*st).application == OPUS_APPLICATION_RESTRICTED_LOWDELAY {
1231 delay_compensation = 0;
1232 } else {
1233 delay_compensation = (*st).delay_compensation;
1234 }
1235 lsb_depth = if lsb_depth < (*st).lsb_depth {
1236 lsb_depth
1237 } else {
1238 (*st).lsb_depth
1239 };
1240 opus_custom_encoder_ctl!(celt_enc, CELT_GET_MODE_REQUEST, &mut celt_mode);
1241 analysis_info.valid = 0;
1242 if (*st).silk_mode.complexity >= 7 && (*st).Fs >= 16000 {
1243 is_silence = is_digital_silence(pcm, frame_size, (*st).channels, lsb_depth);
1244 analysis_read_pos_bak = (*st).analysis.read_pos;
1245 analysis_read_subframe_bak = (*st).analysis.read_subframe;
1246 run_analysis(
1247 &mut (*st).analysis,
1248 celt_mode,
1249 analysis_pcm,
1250 analysis_size,
1251 frame_size,
1252 c1,
1253 c2,
1254 analysis_channels,
1255 (*st).Fs,
1256 lsb_depth,
1257 downmix,
1258 &mut analysis_info,
1259 );
1260 if is_silence == 0 && analysis_info.activity_probability > DTX_ACTIVITY_THRESHOLD {
1261 (*st).peak_signal_energy = if 0.999f32 * (*st).peak_signal_energy
1262 > compute_frame_energy(pcm, frame_size, (*st).channels, (*st).arch)
1263 {
1264 0.999f32 * (*st).peak_signal_energy
1265 } else {
1266 compute_frame_energy(pcm, frame_size, (*st).channels, (*st).arch)
1267 };
1268 }
1269 } else if (*st).analysis.initialized != 0 {
1270 tonality_analysis_reset(&mut (*st).analysis);
1271 }
1272 if is_silence == 0 {
1273 (*st).voice_ratio = -1;
1274 }
1275 (*st).detected_bandwidth = 0;
1276 if analysis_info.valid != 0 {
1277 let mut analysis_bandwidth: i32 = 0;
1278 if (*st).signal_type == OPUS_AUTO {
1279 let mut prob: f32 = 0.;
1280 if (*st).prev_mode == 0 {
1281 prob = analysis_info.music_prob;
1282 } else if (*st).prev_mode == MODE_CELT_ONLY {
1283 prob = analysis_info.music_prob_max;
1284 } else {
1285 prob = analysis_info.music_prob_min;
1286 }
1287 (*st).voice_ratio = (0.5 + (100.0 * (1.0 - prob))).floor() as i32;
1288 }
1289 analysis_bandwidth = analysis_info.bandwidth;
1290 if analysis_bandwidth <= 12 {
1291 (*st).detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1292 } else if analysis_bandwidth <= 14 {
1293 (*st).detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1294 } else if analysis_bandwidth <= 16 {
1295 (*st).detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1296 } else if analysis_bandwidth <= 18 {
1297 (*st).detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1298 } else {
1299 (*st).detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1300 }
1301 }
1302 if (*st).channels == 2 && (*st).force_channels != 1 {
1303 stereo_width = compute_stereo_width(pcm, frame_size, (*st).Fs, &mut (*st).width_mem);
1304 } else {
1305 stereo_width = 0 as opus_val16;
1306 }
1307 total_buffer = delay_compensation;
1308 (*st).bitrate_bps = user_bitrate_to_bitrate(st, frame_size, max_data_bytes);
1309 frame_rate = (*st).Fs / frame_size;
1310 if (*st).use_vbr == 0 {
1311 let mut cbrBytes: i32 = 0;
1312 let frame_rate12: i32 = 12 * (*st).Fs / frame_size;
1313 cbrBytes =
1314 if (12 * (*st).bitrate_bps / 8 + frame_rate12 / 2) / frame_rate12 < max_data_bytes {
1315 (12 * (*st).bitrate_bps / 8 + frame_rate12 / 2) / frame_rate12
1316 } else {
1317 max_data_bytes
1318 };
1319 (*st).bitrate_bps = cbrBytes * frame_rate12 * 8 / 12;
1320 max_data_bytes = if 1 > cbrBytes { 1 } else { cbrBytes };
1321 }
1322 if max_data_bytes < 3
1323 || (*st).bitrate_bps < 3 * frame_rate * 8
1324 || frame_rate < 50 && (max_data_bytes * frame_rate < 300 || (*st).bitrate_bps < 2400)
1325 {
1326 let mut tocmode: i32 = (*st).mode;
1327 let mut bw: i32 = if (*st).bandwidth == 0 {
1328 OPUS_BANDWIDTH_NARROWBAND
1329 } else {
1330 (*st).bandwidth
1331 };
1332 let mut packet_code: i32 = 0;
1333 let mut num_multiframes: i32 = 0;
1334 if tocmode == 0 {
1335 tocmode = MODE_SILK_ONLY;
1336 }
1337 if frame_rate > 100 {
1338 tocmode = MODE_CELT_ONLY;
1339 }
1340 if frame_rate == 25 && tocmode != MODE_SILK_ONLY {
1341 frame_rate = 50;
1342 packet_code = 1;
1343 }
1344 if frame_rate <= 16 {
1345 if out_data_bytes == 1 || tocmode == MODE_SILK_ONLY && frame_rate != 10 {
1346 tocmode = MODE_SILK_ONLY;
1347 packet_code = (frame_rate <= 12) as i32;
1348 frame_rate = if frame_rate == 12 { 25 } else { 16 };
1349 } else {
1350 num_multiframes = 50 / frame_rate;
1351 frame_rate = 50;
1352 packet_code = 3;
1353 }
1354 }
1355 if tocmode == MODE_SILK_ONLY && bw > OPUS_BANDWIDTH_WIDEBAND {
1356 bw = OPUS_BANDWIDTH_WIDEBAND;
1357 } else if tocmode == MODE_CELT_ONLY && bw == OPUS_BANDWIDTH_MEDIUMBAND {
1358 bw = OPUS_BANDWIDTH_NARROWBAND;
1359 } else if tocmode == MODE_HYBRID && bw <= OPUS_BANDWIDTH_SUPERWIDEBAND {
1360 bw = OPUS_BANDWIDTH_SUPERWIDEBAND;
1361 }
1362 *data.offset(0 as isize) = gen_toc(tocmode, frame_rate, bw, (*st).stream_channels);
1363 let ref mut fresh4 = *data.offset(0 as isize);
1364 *fresh4 = (*fresh4 as i32 | packet_code) as u8;
1365 ret = if packet_code <= 1 { 1 } else { 2 };
1366 max_data_bytes = if max_data_bytes > ret {
1367 max_data_bytes
1368 } else {
1369 ret
1370 };
1371 if packet_code == 3 {
1372 *data.offset(1 as isize) = num_multiframes as u8;
1373 }
1374 if (*st).use_vbr == 0 {
1375 ret = opus_packet_pad(data, ret, max_data_bytes);
1376 if ret == OPUS_OK {
1377 ret = max_data_bytes;
1378 } else {
1379 ret = OPUS_INTERNAL_ERROR;
1380 }
1381 }
1382 return ret;
1383 }
1384 max_rate = frame_rate * max_data_bytes * 8;
1385 equiv_rate = compute_equiv_rate(
1386 (*st).bitrate_bps,
1387 (*st).channels,
1388 (*st).Fs / frame_size,
1389 (*st).use_vbr,
1390 0,
1391 (*st).silk_mode.complexity,
1392 (*st).silk_mode.packetLossPercentage,
1393 );
1394 if (*st).signal_type == OPUS_SIGNAL_VOICE {
1395 voice_est = 127;
1396 } else if (*st).signal_type == OPUS_SIGNAL_MUSIC {
1397 voice_est = 0;
1398 } else if (*st).voice_ratio >= 0 {
1399 voice_est = (*st).voice_ratio * 327 >> 8;
1400 if (*st).application == OPUS_APPLICATION_AUDIO {
1401 voice_est = if voice_est < 115 { voice_est } else { 115 };
1402 }
1403 } else if (*st).application == OPUS_APPLICATION_VOIP {
1404 voice_est = 115;
1405 } else {
1406 voice_est = 48;
1407 }
1408 if (*st).force_channels != OPUS_AUTO && (*st).channels == 2 {
1409 (*st).stream_channels = (*st).force_channels;
1410 } else if (*st).channels == 2 {
1411 let mut stereo_threshold: i32 = 0;
1412 stereo_threshold = stereo_music_threshold
1413 + (voice_est * voice_est * (stereo_voice_threshold - stereo_music_threshold) >> 14);
1414 if (*st).stream_channels == 2 {
1415 stereo_threshold -= 1000;
1416 } else {
1417 stereo_threshold += 1000;
1418 }
1419 (*st).stream_channels = if equiv_rate > stereo_threshold { 2 } else { 1 };
1420 } else {
1421 (*st).stream_channels = (*st).channels;
1422 }
1423 equiv_rate = compute_equiv_rate(
1424 (*st).bitrate_bps,
1425 (*st).stream_channels,
1426 (*st).Fs / frame_size,
1427 (*st).use_vbr,
1428 0,
1429 (*st).silk_mode.complexity,
1430 (*st).silk_mode.packetLossPercentage,
1431 );
1432 (*st).silk_mode.useDTX =
1433 ((*st).use_dtx != 0 && !(analysis_info.valid != 0 || is_silence != 0)) as i32;
1434 if (*st).application == OPUS_APPLICATION_RESTRICTED_LOWDELAY {
1435 (*st).mode = MODE_CELT_ONLY;
1436 } else if (*st).user_forced_mode == OPUS_AUTO {
1437 let mut mode_voice: i32 = 0;
1438 let mut mode_music: i32 = 0;
1439 let mut threshold: i32 = 0;
1440 mode_voice = ((1.0f32 - stereo_width) * mode_thresholds[0 as usize][0 as usize] as f32
1441 + stereo_width * mode_thresholds[1 as usize][0 as usize] as f32)
1442 as i32;
1443 mode_music = ((1.0f32 - stereo_width) * mode_thresholds[1 as usize][1 as usize] as f32
1444 + stereo_width * mode_thresholds[1 as usize][1 as usize] as f32)
1445 as i32;
1446 threshold = mode_music + (voice_est * voice_est * (mode_voice - mode_music) >> 14);
1447 if (*st).application == OPUS_APPLICATION_VOIP {
1448 threshold += 8000;
1449 }
1450 if (*st).prev_mode == MODE_CELT_ONLY {
1451 threshold -= 4000;
1452 } else if (*st).prev_mode > 0 {
1453 threshold += 4000;
1454 }
1455 (*st).mode = if equiv_rate >= threshold {
1456 MODE_CELT_ONLY
1457 } else {
1458 MODE_SILK_ONLY
1459 };
1460 if (*st).silk_mode.useInBandFEC != 0
1461 && (*st).silk_mode.packetLossPercentage > 128 - voice_est >> 4
1462 {
1463 (*st).mode = MODE_SILK_ONLY;
1464 }
1465 if (*st).silk_mode.useDTX != 0 && voice_est > 100 {
1466 (*st).mode = MODE_SILK_ONLY;
1467 }
1468 if max_data_bytes
1469 < (if frame_rate > 50 { 9000 } else { 6000 }) * frame_size / ((*st).Fs * 8)
1470 {
1471 (*st).mode = MODE_CELT_ONLY;
1472 }
1473 } else {
1474 (*st).mode = (*st).user_forced_mode;
1475 }
1476 if (*st).mode != MODE_CELT_ONLY && frame_size < (*st).Fs / 100 {
1477 (*st).mode = MODE_CELT_ONLY;
1478 }
1479 if (*st).lfe != 0 {
1480 (*st).mode = MODE_CELT_ONLY;
1481 }
1482 if (*st).prev_mode > 0
1483 && ((*st).mode != MODE_CELT_ONLY && (*st).prev_mode == MODE_CELT_ONLY
1484 || (*st).mode == MODE_CELT_ONLY && (*st).prev_mode != MODE_CELT_ONLY)
1485 {
1486 redundancy = 1;
1487 celt_to_silk = ((*st).mode != MODE_CELT_ONLY) as i32;
1488 if celt_to_silk == 0 {
1489 if frame_size >= (*st).Fs / 100 {
1490 (*st).mode = (*st).prev_mode;
1491 to_celt = 1;
1492 } else {
1493 redundancy = 0;
1494 }
1495 }
1496 }
1497 if (*st).stream_channels == 1
1498 && (*st).prev_channels == 2
1499 && (*st).silk_mode.toMono == 0
1500 && (*st).mode != MODE_CELT_ONLY
1501 && (*st).prev_mode != MODE_CELT_ONLY
1502 {
1503 (*st).silk_mode.toMono = 1;
1504 (*st).stream_channels = 2;
1505 } else {
1506 (*st).silk_mode.toMono = 0;
1507 }
1508 equiv_rate = compute_equiv_rate(
1509 (*st).bitrate_bps,
1510 (*st).stream_channels,
1511 (*st).Fs / frame_size,
1512 (*st).use_vbr,
1513 (*st).mode,
1514 (*st).silk_mode.complexity,
1515 (*st).silk_mode.packetLossPercentage,
1516 );
1517 if (*st).mode != MODE_CELT_ONLY && (*st).prev_mode == MODE_CELT_ONLY {
1518 let mut dummy: silk_EncControlStruct = silk_EncControlStruct {
1519 nChannelsAPI: 0,
1520 nChannelsInternal: 0,
1521 API_sampleRate: 0,
1522 maxInternalSampleRate: 0,
1523 minInternalSampleRate: 0,
1524 desiredInternalSampleRate: 0,
1525 payloadSize_ms: 0,
1526 bitRate: 0,
1527 packetLossPercentage: 0,
1528 complexity: 0,
1529 useInBandFEC: 0,
1530 LBRR_coded: 0,
1531 useDTX: 0,
1532 useCBR: 0,
1533 maxBits: 0,
1534 toMono: 0,
1535 opusCanSwitch: 0,
1536 reducedDependency: 0,
1537 internalSampleRate: 0,
1538 allowBandwidthSwitch: 0,
1539 inWBmodeWithoutVariableLP: 0,
1540 stereoWidth_Q14: 0,
1541 switchReady: 0,
1542 signalType: 0,
1543 offset: 0,
1544 };
1545 silk_InitEncoder(silk_enc, (*st).arch, &mut dummy);
1546 prefill = 1;
1547 }
1548 if (*st).mode == MODE_CELT_ONLY || (*st).first != 0 || (*st).silk_mode.allowBandwidthSwitch != 0
1549 {
1550 let mut voice_bandwidth_thresholds: *const i32 = 0 as *const i32;
1551 let mut music_bandwidth_thresholds: *const i32 = 0 as *const i32;
1552 let mut bandwidth_thresholds: [i32; 8] = [0; 8];
1553 let mut bandwidth: i32 = OPUS_BANDWIDTH_FULLBAND;
1554 if (*st).channels == 2 && (*st).force_channels != 1 {
1555 voice_bandwidth_thresholds = stereo_voice_bandwidth_thresholds.as_ptr();
1556 music_bandwidth_thresholds = stereo_music_bandwidth_thresholds.as_ptr();
1557 } else {
1558 voice_bandwidth_thresholds = mono_voice_bandwidth_thresholds.as_ptr();
1559 music_bandwidth_thresholds = mono_music_bandwidth_thresholds.as_ptr();
1560 }
1561 i = 0;
1562 while i < 8 {
1563 bandwidth_thresholds[i as usize] = *music_bandwidth_thresholds.offset(i as isize)
1564 + (voice_est
1565 * voice_est
1566 * (*voice_bandwidth_thresholds.offset(i as isize)
1567 - *music_bandwidth_thresholds.offset(i as isize))
1568 >> 14);
1569 i += 1;
1570 }
1571 loop {
1572 let mut threshold_0: i32 = 0;
1573 let mut hysteresis: i32 = 0;
1574 threshold_0 =
1575 bandwidth_thresholds[(2 * (bandwidth - OPUS_BANDWIDTH_MEDIUMBAND)) as usize];
1576 hysteresis =
1577 bandwidth_thresholds[(2 * (bandwidth - OPUS_BANDWIDTH_MEDIUMBAND) + 1) as usize];
1578 if (*st).first == 0 {
1579 if (*st).auto_bandwidth >= bandwidth {
1580 threshold_0 -= hysteresis;
1581 } else {
1582 threshold_0 += hysteresis;
1583 }
1584 }
1585 if equiv_rate >= threshold_0 {
1586 break;
1587 }
1588 bandwidth -= 1;
1589 if !(bandwidth > OPUS_BANDWIDTH_NARROWBAND) {
1590 break;
1591 }
1592 }
1593 if bandwidth == OPUS_BANDWIDTH_MEDIUMBAND {
1594 bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1595 }
1596 (*st).auto_bandwidth = bandwidth;
1597 (*st).bandwidth = (*st).auto_bandwidth;
1598 if (*st).first == 0
1599 && (*st).mode != MODE_CELT_ONLY
1600 && (*st).silk_mode.inWBmodeWithoutVariableLP == 0
1601 && (*st).bandwidth > OPUS_BANDWIDTH_WIDEBAND
1602 {
1603 (*st).bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1604 }
1605 }
1606 if (*st).bandwidth > (*st).max_bandwidth {
1607 (*st).bandwidth = (*st).max_bandwidth;
1608 }
1609 if (*st).user_bandwidth != OPUS_AUTO {
1610 (*st).bandwidth = (*st).user_bandwidth;
1611 }
1612 if (*st).mode != MODE_CELT_ONLY && max_rate < 15000 {
1613 (*st).bandwidth = if (*st).bandwidth < 1103 {
1614 (*st).bandwidth
1615 } else {
1616 1103
1617 };
1618 }
1619 if (*st).Fs <= 24000 && (*st).bandwidth > OPUS_BANDWIDTH_SUPERWIDEBAND {
1620 (*st).bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1621 }
1622 if (*st).Fs <= 16000 && (*st).bandwidth > OPUS_BANDWIDTH_WIDEBAND {
1623 (*st).bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1624 }
1625 if (*st).Fs <= 12000 && (*st).bandwidth > OPUS_BANDWIDTH_MEDIUMBAND {
1626 (*st).bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1627 }
1628 if (*st).Fs <= 8000 && (*st).bandwidth > OPUS_BANDWIDTH_NARROWBAND {
1629 (*st).bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1630 }
1631 if (*st).detected_bandwidth != 0 && (*st).user_bandwidth == OPUS_AUTO {
1632 let mut min_detected_bandwidth: i32 = 0;
1633 if equiv_rate <= 18000 * (*st).stream_channels && (*st).mode == MODE_CELT_ONLY {
1634 min_detected_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1635 } else if equiv_rate <= 24000 * (*st).stream_channels && (*st).mode == MODE_CELT_ONLY {
1636 min_detected_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
1637 } else if equiv_rate <= 30000 * (*st).stream_channels {
1638 min_detected_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1639 } else if equiv_rate <= 44000 * (*st).stream_channels {
1640 min_detected_bandwidth = OPUS_BANDWIDTH_SUPERWIDEBAND;
1641 } else {
1642 min_detected_bandwidth = OPUS_BANDWIDTH_FULLBAND;
1643 }
1644 (*st).detected_bandwidth = if (*st).detected_bandwidth > min_detected_bandwidth {
1645 (*st).detected_bandwidth
1646 } else {
1647 min_detected_bandwidth
1648 };
1649 (*st).bandwidth = if (*st).bandwidth < (*st).detected_bandwidth {
1650 (*st).bandwidth
1651 } else {
1652 (*st).detected_bandwidth
1653 };
1654 }
1655 (*st).silk_mode.LBRR_coded = decide_fec(
1656 (*st).silk_mode.useInBandFEC,
1657 (*st).silk_mode.packetLossPercentage,
1658 (*st).silk_mode.LBRR_coded,
1659 (*st).mode,
1660 &mut (*st).bandwidth,
1661 equiv_rate,
1662 );
1663 opus_custom_encoder_ctl!(celt_enc, OPUS_SET_LSB_DEPTH_REQUEST, lsb_depth);
1664 if (*st).mode == MODE_CELT_ONLY && (*st).bandwidth == OPUS_BANDWIDTH_MEDIUMBAND {
1665 (*st).bandwidth = OPUS_BANDWIDTH_WIDEBAND;
1666 }
1667 if (*st).lfe != 0 {
1668 (*st).bandwidth = OPUS_BANDWIDTH_NARROWBAND;
1669 }
1670 curr_bandwidth = (*st).bandwidth;
1671 if (*st).mode == MODE_SILK_ONLY && curr_bandwidth > OPUS_BANDWIDTH_WIDEBAND {
1672 (*st).mode = MODE_HYBRID;
1673 }
1674 if (*st).mode == MODE_HYBRID && curr_bandwidth <= OPUS_BANDWIDTH_WIDEBAND {
1675 (*st).mode = MODE_SILK_ONLY;
1676 }
1677 if frame_size > (*st).Fs / 50 && (*st).mode != MODE_SILK_ONLY || frame_size > 3 * (*st).Fs / 50
1678 {
1679 let mut enc_frame_size: i32 = 0;
1680 let mut nb_frames: i32 = 0;
1681 if (*st).mode == MODE_SILK_ONLY {
1682 if frame_size == 2 * (*st).Fs / 25 {
1683 enc_frame_size = (*st).Fs / 25;
1684 } else if frame_size == 3 * (*st).Fs / 25 {
1685 enc_frame_size = 3 * (*st).Fs / 50;
1686 } else {
1687 enc_frame_size = (*st).Fs / 50;
1688 }
1689 } else {
1690 enc_frame_size = (*st).Fs / 50;
1691 }
1692 nb_frames = frame_size / enc_frame_size;
1693 if analysis_read_pos_bak != -1 {
1694 (*st).analysis.read_pos = analysis_read_pos_bak;
1695 (*st).analysis.read_subframe = analysis_read_subframe_bak;
1696 }
1697 ret = encode_multiframe_packet(
1698 st,
1699 pcm,
1700 nb_frames,
1701 enc_frame_size,
1702 data,
1703 out_data_bytes,
1704 to_celt,
1705 lsb_depth,
1706 float_api,
1707 );
1708 return ret;
1709 }
1710 if (*st).silk_bw_switch != 0 {
1711 redundancy = 1;
1712 celt_to_silk = 1;
1713 (*st).silk_bw_switch = 0;
1714 prefill = 2;
1715 }
1716 if (*st).mode == MODE_CELT_ONLY {
1717 redundancy = 0;
1718 }
1719 if redundancy != 0 {
1720 redundancy_bytes = compute_redundancy_bytes(
1721 max_data_bytes,
1722 (*st).bitrate_bps,
1723 frame_rate,
1724 (*st).stream_channels,
1725 );
1726 if redundancy_bytes == 0 {
1727 redundancy = 0;
1728 }
1729 }
1730 bytes_target =
1731 (if max_data_bytes - redundancy_bytes < (*st).bitrate_bps * frame_size / ((*st).Fs * 8) {
1732 max_data_bytes - redundancy_bytes
1733 } else {
1734 (*st).bitrate_bps * frame_size / ((*st).Fs * 8)
1735 }) - 1;
1736 data = data.offset(1 as isize);
1737 enc = ec_enc_init(std::slice::from_raw_parts_mut(
1738 data,
1739 (max_data_bytes - 1) as usize,
1740 ));
1741 let vla = ((total_buffer + frame_size) * (*st).channels) as usize;
1742 let mut pcm_buf: Vec<opus_val16> = ::std::vec::from_elem(0., vla);
1743 memcpy(
1744 pcm_buf.as_mut_ptr() as *mut core::ffi::c_void,
1745 &mut *((*st).delay_buffer)
1746 .as_mut_ptr()
1747 .offset((((*st).encoder_buffer - total_buffer) * (*st).channels) as isize)
1748 as *mut opus_val16 as *const core::ffi::c_void,
1749 ((total_buffer * (*st).channels) as u64)
1750 .wrapping_mul(::core::mem::size_of::<opus_val16>() as u64)
1751 .wrapping_add(
1752 (0 * pcm_buf.as_mut_ptr().offset_from(
1753 &mut *((*st).delay_buffer)
1754 .as_mut_ptr()
1755 .offset((((*st).encoder_buffer - total_buffer) * (*st).channels) as isize),
1756 ) as i64) as u64,
1757 ),
1758 );
1759 if (*st).mode == MODE_CELT_ONLY {
1760 hp_freq_smth1 = ((silk_lin2log(60) as u32) << 8) as i32;
1761 } else {
1762 hp_freq_smth1 = (*(silk_enc as *mut silk_encoder)).state_Fxx[0 as usize]
1763 .sCmn
1764 .variable_HP_smth1_Q15;
1765 }
1766 (*st).variable_HP_smth2_Q15 = ((*st).variable_HP_smth2_Q15 as i64
1767 + ((hp_freq_smth1 - (*st).variable_HP_smth2_Q15) as i64
1768 * ((0.015f32 * ((1) << 16) as f32) as f64 + 0.5f64) as i32 as i16 as i64
1769 >> 16)) as i32;
1770 cutoff_Hz = silk_log2lin((*st).variable_HP_smth2_Q15 >> 8);
1771 if (*st).application == OPUS_APPLICATION_VOIP {
1772 hp_cutoff(
1773 pcm,
1774 cutoff_Hz,
1775 &mut *pcm_buf
1776 .as_mut_ptr()
1777 .offset((total_buffer * (*st).channels) as isize),
1778 ((*st).hp_mem).as_mut_ptr(),
1779 frame_size,
1780 (*st).channels,
1781 (*st).Fs,
1782 (*st).arch,
1783 );
1784 } else {
1785 dc_reject(
1786 pcm,
1787 3,
1788 &mut *pcm_buf
1789 .as_mut_ptr()
1790 .offset((total_buffer * (*st).channels) as isize),
1791 ((*st).hp_mem).as_mut_ptr(),
1792 frame_size,
1793 (*st).channels,
1794 (*st).Fs,
1795 );
1796 }
1797 if float_api != 0 {
1798 let mut sum: opus_val32 = 0.;
1799 sum = celt_inner_prod_c(
1800 &mut *pcm_buf
1801 .as_mut_ptr()
1802 .offset((total_buffer * (*st).channels) as isize),
1803 &mut *pcm_buf
1804 .as_mut_ptr()
1805 .offset((total_buffer * (*st).channels) as isize),
1806 frame_size * (*st).channels,
1807 );
1808 if !(sum < 1e9f32) || sum != sum {
1809 memset(
1810 &mut *pcm_buf
1811 .as_mut_ptr()
1812 .offset((total_buffer * (*st).channels) as isize)
1813 as *mut opus_val16 as *mut core::ffi::c_void,
1814 0,
1815 ((frame_size * (*st).channels) as u64)
1816 .wrapping_mul(::core::mem::size_of::<opus_val16>() as u64),
1817 );
1818 (*st).hp_mem[3 as usize] = 0 as opus_val32;
1819 (*st).hp_mem[2 as usize] = (*st).hp_mem[3 as usize];
1820 (*st).hp_mem[1 as usize] = (*st).hp_mem[2 as usize];
1821 (*st).hp_mem[0 as usize] = (*st).hp_mem[1 as usize];
1822 }
1823 }
1824 HB_gain = Q15ONE;
1825 if (*st).mode != MODE_CELT_ONLY {
1826 let mut total_bitRate: i32 = 0;
1827 let mut celt_rate: i32 = 0;
1828 let mut activity: i32 = 0;
1829 let vla_0 = ((*st).channels * frame_size) as usize;
1830 let mut pcm_silk: Vec<i16> = ::std::vec::from_elem(0, vla_0);
1831 activity = VAD_NO_DECISION;
1832 if analysis_info.valid != 0 {
1833 activity = (analysis_info.activity_probability >= DTX_ACTIVITY_THRESHOLD) as i32;
1834 }
1835 total_bitRate = 8 * bytes_target * frame_rate;
1836 if (*st).mode == MODE_HYBRID {
1837 (*st).silk_mode.bitRate = compute_silk_rate_for_hybrid(
1838 total_bitRate,
1839 curr_bandwidth,
1840 ((*st).Fs == 50 * frame_size) as i32,
1841 (*st).use_vbr,
1842 (*st).silk_mode.LBRR_coded,
1843 (*st).stream_channels,
1844 );
1845 if ((*st).energy_masking).is_null() {
1846 celt_rate = total_bitRate - (*st).silk_mode.bitRate;
1847 HB_gain = Q15ONE - celt_exp2(-celt_rate as f32 * (1.0f32 / 1024f32));
1848 }
1849 } else {
1850 (*st).silk_mode.bitRate = total_bitRate;
1851 }
1852 if !((*st).energy_masking).is_null() && (*st).use_vbr != 0 && (*st).lfe == 0 {
1853 let mut mask_sum: opus_val32 = 0 as opus_val32;
1854 let mut masking_depth: opus_val16 = 0.;
1855 let mut rate_offset: i32 = 0;
1856 let mut c: i32 = 0;
1857 let mut end: i32 = 17;
1858 let mut srate: i16 = 16000;
1859 if (*st).bandwidth == OPUS_BANDWIDTH_NARROWBAND {
1860 end = 13;
1861 srate = 8000;
1862 } else if (*st).bandwidth == OPUS_BANDWIDTH_MEDIUMBAND {
1863 end = 15;
1864 srate = 12000;
1865 }
1866 c = 0;
1867 while c < (*st).channels {
1868 i = 0;
1869 while i < end {
1870 let mut mask: opus_val16 = 0.;
1871 mask = if (if *((*st).energy_masking).offset((21 * c + i) as isize) < 0.5f32 {
1872 *((*st).energy_masking).offset((21 * c + i) as isize)
1873 } else {
1874 0.5f32
1875 }) > -2.0f32
1876 {
1877 if *((*st).energy_masking).offset((21 * c + i) as isize) < 0.5f32 {
1878 *((*st).energy_masking).offset((21 * c + i) as isize)
1879 } else {
1880 0.5f32
1881 }
1882 } else {
1883 -2.0f32
1884 };
1885 if mask > 0 as f32 {
1886 mask = 0.5f32 * mask;
1887 }
1888 mask_sum += mask;
1889 i += 1;
1890 }
1891 c += 1;
1892 }
1893 masking_depth = mask_sum / end as f32 * (*st).channels as f32;
1894 masking_depth += 0.2f32;
1895 rate_offset = (srate as opus_val32 * masking_depth) as i32;
1896 rate_offset = if rate_offset > -(2) * (*st).silk_mode.bitRate / 3 {
1897 rate_offset
1898 } else {
1899 -(2) * (*st).silk_mode.bitRate / 3
1900 };
1901 if (*st).bandwidth == OPUS_BANDWIDTH_SUPERWIDEBAND
1902 || (*st).bandwidth == OPUS_BANDWIDTH_FULLBAND
1903 {
1904 (*st).silk_mode.bitRate += 3 * rate_offset / 5;
1905 } else {
1906 (*st).silk_mode.bitRate += rate_offset;
1907 }
1908 }
1909 (*st).silk_mode.payloadSize_ms = 1000 * frame_size / (*st).Fs;
1910 (*st).silk_mode.nChannelsAPI = (*st).channels;
1911 (*st).silk_mode.nChannelsInternal = (*st).stream_channels;
1912 if curr_bandwidth == OPUS_BANDWIDTH_NARROWBAND {
1913 (*st).silk_mode.desiredInternalSampleRate = 8000;
1914 } else if curr_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND {
1915 (*st).silk_mode.desiredInternalSampleRate = 12000;
1916 } else {
1917 assert!((*st).mode == 1001 || curr_bandwidth == 1103);
1918 (*st).silk_mode.desiredInternalSampleRate = 16000;
1919 }
1920 if (*st).mode == MODE_HYBRID {
1921 (*st).silk_mode.minInternalSampleRate = 16000;
1922 } else {
1923 (*st).silk_mode.minInternalSampleRate = 8000;
1924 }
1925 (*st).silk_mode.maxInternalSampleRate = 16000;
1926 if (*st).mode == MODE_SILK_ONLY {
1927 let mut effective_max_rate: i32 = max_rate;
1928 if frame_rate > 50 {
1929 effective_max_rate = effective_max_rate * 2 / 3;
1930 }
1931 if effective_max_rate < 8000 {
1932 (*st).silk_mode.maxInternalSampleRate = 12000;
1933 (*st).silk_mode.desiredInternalSampleRate =
1934 if (12000) < (*st).silk_mode.desiredInternalSampleRate {
1935 12000
1936 } else {
1937 (*st).silk_mode.desiredInternalSampleRate
1938 };
1939 }
1940 if effective_max_rate < 7000 {
1941 (*st).silk_mode.maxInternalSampleRate = 8000;
1942 (*st).silk_mode.desiredInternalSampleRate =
1943 if (8000) < (*st).silk_mode.desiredInternalSampleRate {
1944 8000
1945 } else {
1946 (*st).silk_mode.desiredInternalSampleRate
1947 };
1948 }
1949 }
1950 (*st).silk_mode.useCBR = ((*st).use_vbr == 0) as i32;
1951 (*st).silk_mode.maxBits = (max_data_bytes - 1) * 8;
1952 if redundancy != 0 && redundancy_bytes >= 2 {
1953 (*st).silk_mode.maxBits -= redundancy_bytes * 8 + 1;
1954 if (*st).mode == MODE_HYBRID {
1955 (*st).silk_mode.maxBits -= 20;
1956 }
1957 }
1958 if (*st).silk_mode.useCBR != 0 {
1959 if (*st).mode == MODE_HYBRID {
1960 (*st).silk_mode.maxBits =
1961 if (*st).silk_mode.maxBits < (*st).silk_mode.bitRate * frame_size / (*st).Fs {
1962 (*st).silk_mode.maxBits
1963 } else {
1964 (*st).silk_mode.bitRate * frame_size / (*st).Fs
1965 };
1966 }
1967 } else if (*st).mode == MODE_HYBRID {
1968 let maxBitRate: i32 = compute_silk_rate_for_hybrid(
1969 (*st).silk_mode.maxBits * (*st).Fs / frame_size,
1970 curr_bandwidth,
1971 ((*st).Fs == 50 * frame_size) as i32,
1972 (*st).use_vbr,
1973 (*st).silk_mode.LBRR_coded,
1974 (*st).stream_channels,
1975 );
1976 (*st).silk_mode.maxBits = maxBitRate * frame_size / (*st).Fs;
1977 }
1978 if prefill != 0 {
1979 let mut zero: i32 = 0;
1980 let mut prefill_offset: i32 = 0;
1981 prefill_offset =
1982 (*st).channels * ((*st).encoder_buffer - (*st).delay_compensation - (*st).Fs / 400);
1983 gain_fade(
1984 ((*st).delay_buffer)
1985 .as_mut_ptr()
1986 .offset(prefill_offset as isize),
1987 ((*st).delay_buffer)
1988 .as_mut_ptr()
1989 .offset(prefill_offset as isize),
1990 0 as opus_val16,
1991 Q15ONE,
1992 (*celt_mode).overlap,
1993 (*st).Fs / 400,
1994 (*st).channels,
1995 (*celt_mode).window,
1996 (*st).Fs,
1997 );
1998 memset(
1999 ((*st).delay_buffer).as_mut_ptr() as *mut core::ffi::c_void,
2000 0,
2001 (prefill_offset as u64).wrapping_mul(::core::mem::size_of::<opus_val16>() as u64),
2002 );
2003 i = 0;
2004 while i < (*st).encoder_buffer * (*st).channels {
2005 *pcm_silk.as_mut_ptr().offset(i as isize) =
2006 FLOAT2INT16((*st).delay_buffer[i as usize]);
2007 i += 1;
2008 }
2009 silk_Encode(
2010 silk_enc,
2011 &mut (*st).silk_mode,
2012 pcm_silk.as_mut_ptr(),
2013 (*st).encoder_buffer,
2014 None,
2015 &mut zero,
2016 prefill,
2017 activity,
2018 );
2019 (*st).silk_mode.opusCanSwitch = 0;
2020 }
2021 i = 0;
2022 while i < frame_size * (*st).channels {
2023 *pcm_silk.as_mut_ptr().offset(i as isize) = FLOAT2INT16(
2024 *pcm_buf
2025 .as_mut_ptr()
2026 .offset((total_buffer * (*st).channels + i) as isize),
2027 );
2028 i += 1;
2029 }
2030 ret = silk_Encode(
2031 silk_enc,
2032 &mut (*st).silk_mode,
2033 pcm_silk.as_mut_ptr(),
2034 frame_size,
2035 Some(&mut enc),
2036 &mut nBytes,
2037 0,
2038 activity,
2039 );
2040 if ret != 0 {
2041 return OPUS_INTERNAL_ERROR;
2042 }
2043 if (*st).mode == MODE_SILK_ONLY {
2044 if (*st).silk_mode.internalSampleRate == 8000 {
2045 curr_bandwidth = OPUS_BANDWIDTH_NARROWBAND;
2046 } else if (*st).silk_mode.internalSampleRate == 12000 {
2047 curr_bandwidth = OPUS_BANDWIDTH_MEDIUMBAND;
2048 } else if (*st).silk_mode.internalSampleRate == 16000 {
2049 curr_bandwidth = OPUS_BANDWIDTH_WIDEBAND;
2050 }
2051 } else {
2052 assert!((*st).silk_mode.internalSampleRate == 16000)
2053 };
2054 (*st).silk_mode.opusCanSwitch =
2055 ((*st).silk_mode.switchReady != 0 && (*st).nonfinal_frame == 0) as i32;
2056 if nBytes == 0 {
2057 (*st).rangeFinal = 0;
2058 *data.offset(-1 as isize) = gen_toc(
2059 (*st).mode,
2060 (*st).Fs / frame_size,
2061 curr_bandwidth,
2062 (*st).stream_channels,
2063 );
2064 return 1;
2065 }
2066 if (*st).silk_mode.opusCanSwitch != 0 {
2067 redundancy_bytes = compute_redundancy_bytes(
2068 max_data_bytes,
2069 (*st).bitrate_bps,
2070 frame_rate,
2071 (*st).stream_channels,
2072 );
2073 redundancy = (redundancy_bytes != 0) as i32;
2074 celt_to_silk = 0;
2075 (*st).silk_bw_switch = 1;
2076 }
2077 }
2078 let mut endband: i32 = 21;
2079 match curr_bandwidth {
2080 OPUS_BANDWIDTH_NARROWBAND => {
2081 endband = 13;
2082 }
2083 OPUS_BANDWIDTH_MEDIUMBAND | OPUS_BANDWIDTH_WIDEBAND => {
2084 endband = 17;
2085 }
2086 OPUS_BANDWIDTH_SUPERWIDEBAND => {
2087 endband = 19;
2088 }
2089 OPUS_BANDWIDTH_FULLBAND => {
2090 endband = 21;
2091 }
2092 _ => {}
2093 }
2094 opus_custom_encoder_ctl!(celt_enc, CELT_SET_END_BAND_REQUEST, endband);
2095 opus_custom_encoder_ctl!(celt_enc, CELT_SET_CHANNELS_REQUEST, (*st).stream_channels);
2096 opus_custom_encoder_ctl!(celt_enc, OPUS_SET_BITRATE_REQUEST, -1);
2097 if (*st).mode != MODE_SILK_ONLY {
2098 let mut celt_pred: opus_val32 = 2 as opus_val32;
2099 opus_custom_encoder_ctl!(celt_enc, OPUS_SET_VBR_REQUEST, 0);
2100 if (*st).silk_mode.reducedDependency != 0 {
2101 celt_pred = 0 as opus_val32;
2102 }
2103 opus_custom_encoder_ctl!(celt_enc, CELT_SET_PREDICTION_REQUEST, celt_pred as i32);
2104 if (*st).mode == MODE_HYBRID {
2105 if (*st).use_vbr != 0 {
2106 opus_custom_encoder_ctl!(
2107 celt_enc,
2108 OPUS_SET_BITRATE_REQUEST,
2109 (*st).bitrate_bps - (*st).silk_mode.bitRate,
2110 );
2111 opus_custom_encoder_ctl!(celt_enc, OPUS_SET_VBR_CONSTRAINT_REQUEST, 0,);
2112 }
2113 } else if (*st).use_vbr != 0 {
2114 opus_custom_encoder_ctl!(celt_enc, OPUS_SET_VBR_REQUEST, 1);
2115 opus_custom_encoder_ctl!(
2116 celt_enc,
2117 OPUS_SET_VBR_CONSTRAINT_REQUEST,
2118 (*st).vbr_constraint,
2119 );
2120 opus_custom_encoder_ctl!(celt_enc, OPUS_SET_BITRATE_REQUEST, (*st).bitrate_bps);
2121 }
2122 }
2123 let vla_1 = ((*st).channels * (*st).Fs / 400) as usize;
2124 let mut tmp_prefill: Vec<opus_val16> = ::std::vec::from_elem(0., vla_1);
2125 if (*st).mode != MODE_SILK_ONLY && (*st).mode != (*st).prev_mode && (*st).prev_mode > 0 {
2126 memcpy(
2127 tmp_prefill.as_mut_ptr() as *mut core::ffi::c_void,
2128 &mut *((*st).delay_buffer).as_mut_ptr().offset(
2129 (((*st).encoder_buffer - total_buffer - (*st).Fs / 400) * (*st).channels) as isize,
2130 ) as *mut opus_val16 as *const core::ffi::c_void,
2131 (((*st).channels * (*st).Fs / 400) as u64)
2132 .wrapping_mul(::core::mem::size_of::<opus_val16>() as u64)
2133 .wrapping_add(
2134 (0 * tmp_prefill.as_mut_ptr().offset_from(
2135 &mut *((*st).delay_buffer).as_mut_ptr().offset(
2136 (((*st).encoder_buffer - total_buffer - (*st).Fs / 400)
2137 * (*st).channels) as isize,
2138 ),
2139 ) as i64) as u64,
2140 ),
2141 );
2142 }
2143 if (*st).channels * ((*st).encoder_buffer - (frame_size + total_buffer)) > 0 {
2144 memmove(
2145 ((*st).delay_buffer).as_mut_ptr() as *mut core::ffi::c_void,
2146 &mut *((*st).delay_buffer)
2147 .as_mut_ptr()
2148 .offset(((*st).channels * frame_size) as isize) as *mut opus_val16
2149 as *const core::ffi::c_void,
2150 (((*st).channels * ((*st).encoder_buffer - frame_size - total_buffer)) as u64)
2151 .wrapping_mul(::core::mem::size_of::<opus_val16>() as u64)
2152 .wrapping_add(
2153 (0 * ((*st).delay_buffer).as_mut_ptr().offset_from(
2154 &mut *((*st).delay_buffer)
2155 .as_mut_ptr()
2156 .offset(((*st).channels * frame_size) as isize),
2157 ) as i64) as u64,
2158 ),
2159 );
2160 memcpy(
2161 &mut *((*st).delay_buffer).as_mut_ptr().offset(
2162 ((*st).channels * ((*st).encoder_buffer - frame_size - total_buffer)) as isize,
2163 ) as *mut opus_val16 as *mut core::ffi::c_void,
2164 &mut *pcm_buf.as_mut_ptr().offset(0 as isize) as *mut opus_val16
2165 as *const core::ffi::c_void,
2166 (((frame_size + total_buffer) * (*st).channels) as u64)
2167 .wrapping_mul(::core::mem::size_of::<opus_val16>() as u64)
2168 .wrapping_add(
2169 (0 * (&mut *((*st).delay_buffer).as_mut_ptr().offset(
2170 ((*st).channels * ((*st).encoder_buffer - frame_size - total_buffer))
2171 as isize,
2172 ) as *mut opus_val16)
2173 .offset_from(&mut *pcm_buf.as_mut_ptr().offset(0 as isize))
2174 as i64) as u64,
2175 ),
2176 );
2177 } else {
2178 memcpy(
2179 ((*st).delay_buffer).as_mut_ptr() as *mut core::ffi::c_void,
2180 &mut *pcm_buf.as_mut_ptr().offset(
2181 ((frame_size + total_buffer - (*st).encoder_buffer) * (*st).channels) as isize,
2182 ) as *mut opus_val16 as *const core::ffi::c_void,
2183 (((*st).encoder_buffer * (*st).channels) as u64)
2184 .wrapping_mul(::core::mem::size_of::<opus_val16>() as u64)
2185 .wrapping_add(
2186 (0 * ((*st).delay_buffer).as_mut_ptr().offset_from(
2187 &mut *pcm_buf.as_mut_ptr().offset(
2188 ((frame_size + total_buffer - (*st).encoder_buffer) * (*st).channels)
2189 as isize,
2190 ),
2191 ) as i64) as u64,
2192 ),
2193 );
2194 }
2195 if (*st).prev_HB_gain < Q15ONE || HB_gain < Q15ONE {
2196 gain_fade(
2197 pcm_buf.as_mut_ptr(),
2198 pcm_buf.as_mut_ptr(),
2199 (*st).prev_HB_gain,
2200 HB_gain,
2201 (*celt_mode).overlap,
2202 frame_size,
2203 (*st).channels,
2204 (*celt_mode).window,
2205 (*st).Fs,
2206 );
2207 }
2208 (*st).prev_HB_gain = HB_gain;
2209 if (*st).mode != MODE_HYBRID || (*st).stream_channels == 1 {
2210 if equiv_rate > 32000 {
2211 (*st).silk_mode.stereoWidth_Q14 = 16384;
2212 } else if equiv_rate < 16000 {
2213 (*st).silk_mode.stereoWidth_Q14 = 0;
2214 } else {
2215 (*st).silk_mode.stereoWidth_Q14 =
2216 16384 - 2048 * (32000 - equiv_rate) / (equiv_rate - 14000);
2217 }
2218 }
2219 if ((*st).energy_masking).is_null() && (*st).channels == 2 {
2220 if ((*st).hybrid_stereo_width_Q14 as i32) < (1) << 14
2221 || (*st).silk_mode.stereoWidth_Q14 < (1) << 14
2222 {
2223 let mut g1: opus_val16 = 0.;
2224 let mut g2: opus_val16 = 0.;
2225 g1 = (*st).hybrid_stereo_width_Q14 as opus_val16;
2226 g2 = (*st).silk_mode.stereoWidth_Q14 as opus_val16;
2227 g1 *= 1.0f32 / 16384 as f32;
2228 g2 *= 1.0f32 / 16384 as f32;
2229 stereo_fade(
2230 pcm_buf.as_mut_ptr(),
2231 pcm_buf.as_mut_ptr(),
2232 g1,
2233 g2,
2234 (*celt_mode).overlap,
2235 frame_size,
2236 (*st).channels,
2237 (*celt_mode).window,
2238 (*st).Fs,
2239 );
2240 (*st).hybrid_stereo_width_Q14 = (*st).silk_mode.stereoWidth_Q14 as i16;
2241 }
2242 }
2243 if (*st).mode != MODE_CELT_ONLY
2244 && ec_tell(&mut enc) + 17 + 20 * ((*st).mode == MODE_HYBRID) as i32
2245 <= 8 * (max_data_bytes - 1)
2246 {
2247 if (*st).mode == MODE_HYBRID {
2248 ec_enc_bit_logp(&mut enc, redundancy, 12);
2249 }
2250 if redundancy != 0 {
2251 let mut max_redundancy: i32 = 0;
2252 ec_enc_bit_logp(&mut enc, celt_to_silk, 1);
2253 if (*st).mode == MODE_HYBRID {
2254 max_redundancy = max_data_bytes - 1 - (ec_tell(&mut enc) + 8 + 3 + 7 >> 3);
2255 } else {
2256 max_redundancy = max_data_bytes - 1 - (ec_tell(&mut enc) + 7 >> 3);
2257 }
2258 redundancy_bytes = if max_redundancy < redundancy_bytes {
2259 max_redundancy
2260 } else {
2261 redundancy_bytes
2262 };
2263 redundancy_bytes = if (257)
2264 < (if 2 > redundancy_bytes {
2265 2
2266 } else {
2267 redundancy_bytes
2268 }) {
2269 257
2270 } else if 2 > redundancy_bytes {
2271 2
2272 } else {
2273 redundancy_bytes
2274 };
2275 if (*st).mode == MODE_HYBRID {
2276 ec_enc_uint(&mut enc, (redundancy_bytes - 2) as u32, 256);
2277 }
2278 }
2279 } else {
2280 redundancy = 0;
2281 }
2282 if redundancy == 0 {
2283 (*st).silk_bw_switch = 0;
2284 redundancy_bytes = 0;
2285 }
2286 if (*st).mode != MODE_CELT_ONLY {
2287 start_band = 17;
2288 }
2289 if (*st).mode == MODE_SILK_ONLY {
2290 ret = ec_tell(&mut enc) + 7 >> 3;
2291 ec_enc_done(&mut enc);
2292 nb_compr_bytes = ret;
2293 } else {
2294 nb_compr_bytes = max_data_bytes - 1 - redundancy_bytes;
2295 ec_enc_shrink(&mut enc, nb_compr_bytes as u32);
2296 }
2297 if redundancy != 0 || (*st).mode != MODE_SILK_ONLY {
2298 opus_custom_encoder_ctl!(celt_enc, CELT_SET_ANALYSIS_REQUEST, &mut analysis_info);
2299 }
2300 if (*st).mode == MODE_HYBRID {
2301 let mut info: SILKInfo = SILKInfo {
2302 signalType: 0,
2303 offset: 0,
2304 };
2305 info.signalType = (*st).silk_mode.signalType;
2306 info.offset = (*st).silk_mode.offset;
2307 opus_custom_encoder_ctl!(celt_enc, CELT_SET_SILK_INFO_REQUEST, &mut info);
2308 }
2309 if redundancy != 0 && celt_to_silk != 0 {
2310 let mut err: i32 = 0;
2311 opus_custom_encoder_ctl!(celt_enc, CELT_SET_START_BAND_REQUEST, 0);
2312 opus_custom_encoder_ctl!(celt_enc, OPUS_SET_VBR_REQUEST, 0);
2313 opus_custom_encoder_ctl!(celt_enc, OPUS_SET_BITRATE_REQUEST, -1);
2314 err = celt_encode_with_ec(
2315 celt_enc,
2316 pcm_buf.as_mut_ptr(),
2317 (*st).Fs / 200,
2318 data.offset(nb_compr_bytes as isize),
2319 redundancy_bytes,
2320 None,
2321 );
2322 if err < 0 {
2323 return OPUS_INTERNAL_ERROR;
2324 }
2325 opus_custom_encoder_ctl!(celt_enc, OPUS_GET_FINAL_RANGE_REQUEST, &mut redundant_rng);
2326 opus_custom_encoder_ctl!(celt_enc, OPUS_RESET_STATE);
2327 }
2328 opus_custom_encoder_ctl!(celt_enc, CELT_SET_START_BAND_REQUEST, start_band);
2329 if (*st).mode != MODE_SILK_ONLY {
2330 if (*st).mode != (*st).prev_mode && (*st).prev_mode > 0 {
2331 let mut dummy_0: [u8; 2] = [0; 2];
2332 opus_custom_encoder_ctl!(celt_enc, OPUS_RESET_STATE);
2333 celt_encode_with_ec(
2334 celt_enc,
2335 tmp_prefill.as_mut_ptr(),
2336 (*st).Fs / 400,
2337 dummy_0.as_mut_ptr(),
2338 2,
2339 None,
2340 );
2341 opus_custom_encoder_ctl!(celt_enc, CELT_SET_PREDICTION_REQUEST, 0);
2342 }
2343 if ec_tell(&mut enc) <= 8 * nb_compr_bytes {
2344 if redundancy != 0
2345 && celt_to_silk != 0
2346 && (*st).mode == MODE_HYBRID
2347 && (*st).use_vbr != 0
2348 {
2349 opus_custom_encoder_ctl!(
2350 celt_enc,
2351 OPUS_SET_BITRATE_REQUEST,
2352 (*st).bitrate_bps - (*st).silk_mode.bitRate,
2353 );
2354 }
2355 opus_custom_encoder_ctl!(celt_enc, OPUS_SET_VBR_REQUEST, (*st).use_vbr);
2356 ret = celt_encode_with_ec(
2357 celt_enc,
2358 pcm_buf.as_mut_ptr(),
2359 frame_size,
2360 NULL as *mut u8,
2361 nb_compr_bytes,
2362 Some(&mut enc),
2363 );
2364 if ret < 0 {
2365 return OPUS_INTERNAL_ERROR;
2366 }
2367 if redundancy != 0
2368 && celt_to_silk != 0
2369 && (*st).mode == MODE_HYBRID
2370 && (*st).use_vbr != 0
2371 {
2372 memmove(
2373 data.offset(ret as isize) as *mut core::ffi::c_void,
2374 data.offset(nb_compr_bytes as isize) as *const core::ffi::c_void,
2375 (redundancy_bytes as u64)
2376 .wrapping_mul(::core::mem::size_of::<u8>() as u64)
2377 .wrapping_add(
2378 (0 * data
2379 .offset(ret as isize)
2380 .offset_from(data.offset(nb_compr_bytes as isize))
2381 as i64) as u64,
2382 ),
2383 );
2384 nb_compr_bytes = nb_compr_bytes + redundancy_bytes;
2385 }
2386 }
2387 }
2388 if redundancy != 0 && celt_to_silk == 0 {
2389 let mut err_0: i32 = 0;
2390 let mut dummy_1: [u8; 2] = [0; 2];
2391 let mut N2: i32 = 0;
2392 let mut N4: i32 = 0;
2393 N2 = (*st).Fs / 200;
2394 N4 = (*st).Fs / 400;
2395 opus_custom_encoder_ctl!(celt_enc, OPUS_RESET_STATE);
2396 opus_custom_encoder_ctl!(celt_enc, CELT_SET_START_BAND_REQUEST, 0);
2397 opus_custom_encoder_ctl!(celt_enc, CELT_SET_PREDICTION_REQUEST, 0);
2398 opus_custom_encoder_ctl!(celt_enc, OPUS_SET_VBR_REQUEST, 0);
2399 opus_custom_encoder_ctl!(celt_enc, OPUS_SET_BITRATE_REQUEST, -1);
2400 if (*st).mode == MODE_HYBRID {
2401 nb_compr_bytes = ret;
2402 ec_enc_shrink(&mut enc, nb_compr_bytes as u32);
2403 }
2404 celt_encode_with_ec(
2405 celt_enc,
2406 pcm_buf
2407 .as_mut_ptr()
2408 .offset(((*st).channels * (frame_size - N2 - N4)) as isize),
2409 N4,
2410 dummy_1.as_mut_ptr(),
2411 2,
2412 None,
2413 );
2414 err_0 = celt_encode_with_ec(
2415 celt_enc,
2416 pcm_buf
2417 .as_mut_ptr()
2418 .offset(((*st).channels * (frame_size - N2)) as isize),
2419 N2,
2420 data.offset(nb_compr_bytes as isize),
2421 redundancy_bytes,
2422 None,
2423 );
2424 if err_0 < 0 {
2425 return OPUS_INTERNAL_ERROR;
2426 }
2427 opus_custom_encoder_ctl!(celt_enc, OPUS_GET_FINAL_RANGE_REQUEST, &mut redundant_rng);
2428 }
2429 data = data.offset(-1);
2430 *data.offset(0 as isize) = gen_toc(
2431 (*st).mode,
2432 (*st).Fs / frame_size,
2433 curr_bandwidth,
2434 (*st).stream_channels,
2435 );
2436 (*st).rangeFinal = enc.rng ^ redundant_rng;
2437 if to_celt != 0 {
2438 (*st).prev_mode = MODE_CELT_ONLY;
2439 } else {
2440 (*st).prev_mode = (*st).mode;
2441 }
2442 (*st).prev_channels = (*st).stream_channels;
2443 (*st).prev_framesize = frame_size;
2444 (*st).first = 0;
2445 if (*st).use_dtx != 0 && (analysis_info.valid != 0 || is_silence != 0) {
2446 if decide_dtx_mode(
2447 analysis_info.activity_probability,
2448 &mut (*st).nb_no_activity_frames,
2449 (*st).peak_signal_energy,
2450 pcm,
2451 frame_size,
2452 (*st).channels,
2453 is_silence,
2454 (*st).arch,
2455 ) != 0
2456 {
2457 (*st).rangeFinal = 0;
2458 *data.offset(0 as isize) = gen_toc(
2459 (*st).mode,
2460 (*st).Fs / frame_size,
2461 curr_bandwidth,
2462 (*st).stream_channels,
2463 );
2464 return 1;
2465 }
2466 } else {
2467 (*st).nb_no_activity_frames = 0;
2468 }
2469 if ec_tell(&mut enc) > (max_data_bytes - 1) * 8 {
2470 if max_data_bytes < 2 {
2471 return OPUS_BUFFER_TOO_SMALL;
2472 }
2473 *data.offset(1 as isize) = 0;
2474 ret = 1;
2475 (*st).rangeFinal = 0;
2476 } else if (*st).mode == MODE_SILK_ONLY && redundancy == 0 {
2477 while ret > 2 && *data.offset(ret as isize) as i32 == 0 {
2478 ret -= 1;
2479 }
2480 }
2481 ret += 1 + redundancy_bytes;
2482 if (*st).use_vbr == 0 {
2483 if opus_packet_pad(data, ret, max_data_bytes) != OPUS_OK {
2484 return OPUS_INTERNAL_ERROR;
2485 }
2486 ret = max_data_bytes;
2487 }
2488 return ret;
2489}
2490pub unsafe fn opus_encode(
2491 st: *mut OpusEncoder,
2492 pcm: *const i16,
2493 analysis_frame_size: i32,
2494 data: *mut u8,
2495 max_data_bytes: i32,
2496) -> i32 {
2497 let mut i: i32 = 0;
2498 let mut ret: i32 = 0;
2499 let mut frame_size: i32 = 0;
2500 frame_size = frame_size_select(analysis_frame_size, (*st).variable_duration, (*st).Fs);
2501 if frame_size <= 0 {
2502 return OPUS_BAD_ARG;
2503 }
2504 let vla = (frame_size * (*st).channels) as usize;
2505 let mut in_0: Vec<f32> = ::std::vec::from_elem(0., vla);
2506 i = 0;
2507 while i < frame_size * (*st).channels {
2508 *in_0.as_mut_ptr().offset(i as isize) =
2509 1.0f32 / 32768 as f32 * *pcm.offset(i as isize) as i32 as f32;
2510 i += 1;
2511 }
2512 ret = opus_encode_native(
2513 st,
2514 in_0.as_mut_ptr(),
2515 frame_size,
2516 data,
2517 max_data_bytes,
2518 16,
2519 pcm as *const core::ffi::c_void,
2520 analysis_frame_size,
2521 0,
2522 -(2),
2523 (*st).channels,
2524 Some(
2525 downmix_int
2526 as unsafe fn(
2527 *const core::ffi::c_void,
2528 *mut opus_val32,
2529 i32,
2530 i32,
2531 i32,
2532 i32,
2533 i32,
2534 ) -> (),
2535 ),
2536 0,
2537 );
2538 return ret;
2539}
2540pub unsafe fn opus_encode_float(
2541 st: *mut OpusEncoder,
2542 pcm: *const f32,
2543 analysis_frame_size: i32,
2544 data: *mut u8,
2545 out_data_bytes: i32,
2546) -> i32 {
2547 let mut frame_size: i32 = 0;
2548 frame_size = frame_size_select(analysis_frame_size, (*st).variable_duration, (*st).Fs);
2549 return opus_encode_native(
2550 st,
2551 pcm,
2552 frame_size,
2553 data,
2554 out_data_bytes,
2555 24,
2556 pcm as *const core::ffi::c_void,
2557 analysis_frame_size,
2558 0,
2559 -(2),
2560 (*st).channels,
2561 Some(
2562 downmix_float
2563 as unsafe fn(
2564 *const core::ffi::c_void,
2565 *mut opus_val32,
2566 i32,
2567 i32,
2568 i32,
2569 i32,
2570 i32,
2571 ) -> (),
2572 ),
2573 1,
2574 );
2575}
2576pub unsafe fn opus_encoder_ctl_impl(st: *mut OpusEncoder, request: i32, args: VarArgs) -> i32 {
2577 let mut current_block: u64;
2578 let mut ret: i32 = 0;
2579 let mut celt_enc: *mut OpusCustomEncoder = 0 as *mut OpusCustomEncoder;
2580 ret = OPUS_OK;
2581
2582 let mut ap = args;
2583
2584 celt_enc = (st as *mut i8).offset((*st).celt_enc_offset as isize) as *mut OpusCustomEncoder;
2585 match request {
2586 OPUS_SET_APPLICATION_REQUEST => {
2587 let value: i32 = ap.arg::<i32>();
2588 if value != OPUS_APPLICATION_VOIP
2589 && value != OPUS_APPLICATION_AUDIO
2590 && value != OPUS_APPLICATION_RESTRICTED_LOWDELAY
2591 || (*st).first == 0 && (*st).application != value
2592 {
2593 ret = OPUS_BAD_ARG;
2594 } else {
2595 (*st).application = value;
2596 (*st).analysis.application = value;
2597 }
2598 current_block = 16167632229894708628;
2599 }
2600 OPUS_GET_APPLICATION_REQUEST => {
2601 let value_0 = ap.arg::<&mut i32>();
2602 *value_0 = (*st).application;
2603 current_block = 16167632229894708628;
2604 }
2605 OPUS_SET_BITRATE_REQUEST => {
2606 let mut value_1: i32 = ap.arg::<i32>();
2607 if value_1 != OPUS_AUTO && value_1 != OPUS_BITRATE_MAX {
2608 if value_1 <= 0 {
2609 current_block = 12343738388509029619;
2610 } else {
2611 if value_1 <= 500 {
2612 value_1 = 500;
2613 } else if value_1 > 300000 * (*st).channels {
2614 value_1 = 300000 * (*st).channels;
2615 }
2616 current_block = 6057473163062296781;
2617 }
2618 } else {
2619 current_block = 6057473163062296781;
2620 }
2621 match current_block {
2622 12343738388509029619 => {}
2623 _ => {
2624 (*st).user_bitrate_bps = value_1;
2625 current_block = 16167632229894708628;
2626 }
2627 }
2628 }
2629 OPUS_GET_BITRATE_REQUEST => {
2630 let value_2 = ap.arg::<&mut i32>();
2631 *value_2 = user_bitrate_to_bitrate(st, (*st).prev_framesize, 1276);
2632 current_block = 16167632229894708628;
2633 }
2634 OPUS_SET_FORCE_CHANNELS_REQUEST => {
2635 let value_3: i32 = ap.arg::<i32>();
2636 if (value_3 < 1 || value_3 > (*st).channels) && value_3 != OPUS_AUTO {
2637 current_block = 12343738388509029619;
2638 } else {
2639 (*st).force_channels = value_3;
2640 current_block = 16167632229894708628;
2641 }
2642 }
2643 OPUS_GET_FORCE_CHANNELS_REQUEST => {
2644 let value_4 = ap.arg::<&mut i32>();
2645 *value_4 = (*st).force_channels;
2646 current_block = 16167632229894708628;
2647 }
2648 OPUS_SET_MAX_BANDWIDTH_REQUEST => {
2649 let value_5: i32 = ap.arg::<i32>();
2650 if value_5 < OPUS_BANDWIDTH_NARROWBAND || value_5 > OPUS_BANDWIDTH_FULLBAND {
2651 current_block = 12343738388509029619;
2652 } else {
2653 (*st).max_bandwidth = value_5;
2654 if (*st).max_bandwidth == OPUS_BANDWIDTH_NARROWBAND {
2655 (*st).silk_mode.maxInternalSampleRate = 8000;
2656 } else if (*st).max_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND {
2657 (*st).silk_mode.maxInternalSampleRate = 12000;
2658 } else {
2659 (*st).silk_mode.maxInternalSampleRate = 16000;
2660 }
2661 current_block = 16167632229894708628;
2662 }
2663 }
2664 OPUS_GET_MAX_BANDWIDTH_REQUEST => {
2665 let value_6 = ap.arg::<&mut i32>();
2666 *value_6 = (*st).max_bandwidth;
2667 current_block = 16167632229894708628;
2668 }
2669 OPUS_SET_BANDWIDTH_REQUEST => {
2670 let value_7: i32 = ap.arg::<i32>();
2671 if (value_7 < OPUS_BANDWIDTH_NARROWBAND || value_7 > OPUS_BANDWIDTH_FULLBAND)
2672 && value_7 != OPUS_AUTO
2673 {
2674 current_block = 12343738388509029619;
2675 } else {
2676 (*st).user_bandwidth = value_7;
2677 if (*st).user_bandwidth == OPUS_BANDWIDTH_NARROWBAND {
2678 (*st).silk_mode.maxInternalSampleRate = 8000;
2679 } else if (*st).user_bandwidth == OPUS_BANDWIDTH_MEDIUMBAND {
2680 (*st).silk_mode.maxInternalSampleRate = 12000;
2681 } else {
2682 (*st).silk_mode.maxInternalSampleRate = 16000;
2683 }
2684 current_block = 16167632229894708628;
2685 }
2686 }
2687 OPUS_GET_BANDWIDTH_REQUEST => {
2688 let value_8 = ap.arg::<&mut i32>();
2689 *value_8 = (*st).bandwidth;
2690 current_block = 16167632229894708628;
2691 }
2692 OPUS_SET_DTX_REQUEST => {
2693 let value_9: i32 = ap.arg::<i32>();
2694 if value_9 < 0 || value_9 > 1 {
2695 current_block = 12343738388509029619;
2696 } else {
2697 (*st).use_dtx = value_9;
2698 current_block = 16167632229894708628;
2699 }
2700 }
2701 OPUS_GET_DTX_REQUEST => {
2702 let value_10 = ap.arg::<&mut i32>();
2703 *value_10 = (*st).use_dtx;
2704 current_block = 16167632229894708628;
2705 }
2706 OPUS_SET_COMPLEXITY_REQUEST => {
2707 let value_11: i32 = ap.arg::<i32>();
2708 if value_11 < 0 || value_11 > 10 {
2709 current_block = 12343738388509029619;
2710 } else {
2711 (*st).silk_mode.complexity = value_11;
2712 opus_custom_encoder_ctl!(celt_enc, OPUS_SET_COMPLEXITY_REQUEST, value_11);
2713 current_block = 16167632229894708628;
2714 }
2715 }
2716 OPUS_GET_COMPLEXITY_REQUEST => {
2717 let value_12 = ap.arg::<&mut i32>();
2718 *value_12 = (*st).silk_mode.complexity;
2719 current_block = 16167632229894708628;
2720 }
2721 OPUS_SET_INBAND_FEC_REQUEST => {
2722 let value_13: i32 = ap.arg::<i32>();
2723 if value_13 < 0 || value_13 > 1 {
2724 current_block = 12343738388509029619;
2725 } else {
2726 (*st).silk_mode.useInBandFEC = value_13;
2727 current_block = 16167632229894708628;
2728 }
2729 }
2730 OPUS_GET_INBAND_FEC_REQUEST => {
2731 let value_14 = ap.arg::<&mut i32>();
2732 *value_14 = (*st).silk_mode.useInBandFEC;
2733 current_block = 16167632229894708628;
2734 }
2735 OPUS_SET_PACKET_LOSS_PERC_REQUEST => {
2736 let value_15: i32 = ap.arg::<i32>();
2737 if value_15 < 0 || value_15 > 100 {
2738 current_block = 12343738388509029619;
2739 } else {
2740 (*st).silk_mode.packetLossPercentage = value_15;
2741 opus_custom_encoder_ctl!(celt_enc, OPUS_SET_PACKET_LOSS_PERC_REQUEST, value_15);
2742 current_block = 16167632229894708628;
2743 }
2744 }
2745 OPUS_GET_PACKET_LOSS_PERC_REQUEST => {
2746 let value_16 = ap.arg::<&mut i32>();
2747 *value_16 = (*st).silk_mode.packetLossPercentage;
2748 current_block = 16167632229894708628;
2749 }
2750 OPUS_SET_VBR_REQUEST => {
2751 let value_17: i32 = ap.arg::<i32>();
2752 if value_17 < 0 || value_17 > 1 {
2753 current_block = 12343738388509029619;
2754 } else {
2755 (*st).use_vbr = value_17;
2756 (*st).silk_mode.useCBR = 1 - value_17;
2757 current_block = 16167632229894708628;
2758 }
2759 }
2760 OPUS_GET_VBR_REQUEST => {
2761 let value_18 = ap.arg::<&mut i32>();
2762 *value_18 = (*st).use_vbr;
2763 current_block = 16167632229894708628;
2764 }
2765 OPUS_SET_VOICE_RATIO_REQUEST => {
2766 let value_19: i32 = ap.arg::<i32>();
2767 if value_19 < -1 || value_19 > 100 {
2768 current_block = 12343738388509029619;
2769 } else {
2770 (*st).voice_ratio = value_19;
2771 current_block = 16167632229894708628;
2772 }
2773 }
2774 OPUS_GET_VOICE_RATIO_REQUEST => {
2775 let value_20 = ap.arg::<&mut i32>();
2776 *value_20 = (*st).voice_ratio;
2777 current_block = 16167632229894708628;
2778 }
2779 OPUS_SET_VBR_CONSTRAINT_REQUEST => {
2780 let value_21: i32 = ap.arg::<i32>();
2781 if value_21 < 0 || value_21 > 1 {
2782 current_block = 12343738388509029619;
2783 } else {
2784 (*st).vbr_constraint = value_21;
2785 current_block = 16167632229894708628;
2786 }
2787 }
2788 OPUS_GET_VBR_CONSTRAINT_REQUEST => {
2789 let value_22 = ap.arg::<&mut i32>();
2790 *value_22 = (*st).vbr_constraint;
2791 current_block = 16167632229894708628;
2792 }
2793 OPUS_SET_SIGNAL_REQUEST => {
2794 let value_23: i32 = ap.arg::<i32>();
2795 if value_23 != OPUS_AUTO
2796 && value_23 != OPUS_SIGNAL_VOICE
2797 && value_23 != OPUS_SIGNAL_MUSIC
2798 {
2799 current_block = 12343738388509029619;
2800 } else {
2801 (*st).signal_type = value_23;
2802 current_block = 16167632229894708628;
2803 }
2804 }
2805 OPUS_GET_SIGNAL_REQUEST => {
2806 let value_24 = ap.arg::<&mut i32>();
2807 *value_24 = (*st).signal_type;
2808 current_block = 16167632229894708628;
2809 }
2810 OPUS_GET_LOOKAHEAD_REQUEST => {
2811 let value_25 = ap.arg::<&mut i32>();
2812 *value_25 = (*st).Fs / 400;
2813 if (*st).application != OPUS_APPLICATION_RESTRICTED_LOWDELAY {
2814 *value_25 += (*st).delay_compensation;
2815 }
2816 current_block = 16167632229894708628;
2817 }
2818 OPUS_GET_SAMPLE_RATE_REQUEST => {
2819 let value_26 = ap.arg::<&mut i32>();
2820 *value_26 = (*st).Fs;
2821 current_block = 16167632229894708628;
2822 }
2823 OPUS_GET_FINAL_RANGE_REQUEST => {
2824 let value_27 = ap.arg::<&mut u32>();
2825 *value_27 = (*st).rangeFinal;
2826 current_block = 16167632229894708628;
2827 }
2828 OPUS_SET_LSB_DEPTH_REQUEST => {
2829 let value_28: i32 = ap.arg::<i32>();
2830 if value_28 < 8 || value_28 > 24 {
2831 current_block = 12343738388509029619;
2832 } else {
2833 (*st).lsb_depth = value_28;
2834 current_block = 16167632229894708628;
2835 }
2836 }
2837 OPUS_GET_LSB_DEPTH_REQUEST => {
2838 let value_29 = ap.arg::<&mut i32>();
2839 *value_29 = (*st).lsb_depth;
2840 current_block = 16167632229894708628;
2841 }
2842 OPUS_SET_EXPERT_FRAME_DURATION_REQUEST => {
2843 let value_30: i32 = ap.arg::<i32>();
2844 if value_30 != OPUS_FRAMESIZE_ARG
2845 && value_30 != OPUS_FRAMESIZE_2_5_MS
2846 && value_30 != OPUS_FRAMESIZE_5_MS
2847 && value_30 != OPUS_FRAMESIZE_10_MS
2848 && value_30 != OPUS_FRAMESIZE_20_MS
2849 && value_30 != OPUS_FRAMESIZE_40_MS
2850 && value_30 != OPUS_FRAMESIZE_60_MS
2851 && value_30 != OPUS_FRAMESIZE_80_MS
2852 && value_30 != OPUS_FRAMESIZE_100_MS
2853 && value_30 != OPUS_FRAMESIZE_120_MS
2854 {
2855 current_block = 12343738388509029619;
2856 } else {
2857 (*st).variable_duration = value_30;
2858 current_block = 16167632229894708628;
2859 }
2860 }
2861 OPUS_GET_EXPERT_FRAME_DURATION_REQUEST => {
2862 let value_31 = ap.arg::<&mut i32>();
2863 *value_31 = (*st).variable_duration;
2864 current_block = 16167632229894708628;
2865 }
2866 OPUS_SET_PREDICTION_DISABLED_REQUEST => {
2867 let value_32: i32 = ap.arg::<i32>();
2868 if value_32 > 1 || value_32 < 0 {
2869 current_block = 12343738388509029619;
2870 } else {
2871 (*st).silk_mode.reducedDependency = value_32;
2872 current_block = 16167632229894708628;
2873 }
2874 }
2875 OPUS_GET_PREDICTION_DISABLED_REQUEST => {
2876 let value_33 = ap.arg::<&mut i32>();
2877 *value_33 = (*st).silk_mode.reducedDependency;
2878 current_block = 16167632229894708628;
2879 }
2880 OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST => {
2881 let value_34: i32 = ap.arg::<i32>();
2882 if value_34 < 0 || value_34 > 1 {
2883 current_block = 12343738388509029619;
2884 } else {
2885 opus_custom_encoder_ctl!(
2886 celt_enc,
2887 OPUS_SET_PHASE_INVERSION_DISABLED_REQUEST,
2888 value_34,
2889 );
2890 current_block = 16167632229894708628;
2891 }
2892 }
2893 OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST => {
2894 let value_35 = ap.arg::<&mut i32>();
2895 opus_custom_encoder_ctl!(
2896 celt_enc,
2897 OPUS_GET_PHASE_INVERSION_DISABLED_REQUEST,
2898 value_35
2899 );
2900 current_block = 16167632229894708628;
2901 }
2902 OPUS_RESET_STATE => {
2903 let mut silk_enc: *mut core::ffi::c_void = 0 as *mut core::ffi::c_void;
2904 let mut dummy: silk_EncControlStruct = silk_EncControlStruct {
2905 nChannelsAPI: 0,
2906 nChannelsInternal: 0,
2907 API_sampleRate: 0,
2908 maxInternalSampleRate: 0,
2909 minInternalSampleRate: 0,
2910 desiredInternalSampleRate: 0,
2911 payloadSize_ms: 0,
2912 bitRate: 0,
2913 packetLossPercentage: 0,
2914 complexity: 0,
2915 useInBandFEC: 0,
2916 LBRR_coded: 0,
2917 useDTX: 0,
2918 useCBR: 0,
2919 maxBits: 0,
2920 toMono: 0,
2921 opusCanSwitch: 0,
2922 reducedDependency: 0,
2923 internalSampleRate: 0,
2924 allowBandwidthSwitch: 0,
2925 inWBmodeWithoutVariableLP: 0,
2926 stereoWidth_Q14: 0,
2927 switchReady: 0,
2928 signalType: 0,
2929 offset: 0,
2930 };
2931 let mut start: *mut i8 = 0 as *mut i8;
2932 silk_enc =
2933 (st as *mut i8).offset((*st).silk_enc_offset as isize) as *mut core::ffi::c_void;
2934 tonality_analysis_reset(&mut (*st).analysis);
2935 start = &mut (*st).stream_channels as *mut i32 as *mut i8;
2936 memset(
2937 start as *mut core::ffi::c_void,
2938 0,
2939 (::core::mem::size_of::<OpusEncoder>() as u64)
2940 .wrapping_sub(start.offset_from(st as *mut i8) as i64 as u64)
2941 .wrapping_mul(::core::mem::size_of::<i8>() as u64),
2942 );
2943 opus_custom_encoder_ctl!(celt_enc, OPUS_RESET_STATE);
2944 silk_InitEncoder(silk_enc, (*st).arch, &mut dummy);
2945 (*st).stream_channels = (*st).channels;
2946 (*st).hybrid_stereo_width_Q14 = ((1) << 14) as i16;
2947 (*st).prev_HB_gain = Q15ONE;
2948 (*st).first = 1;
2949 (*st).mode = MODE_HYBRID;
2950 (*st).bandwidth = OPUS_BANDWIDTH_FULLBAND;
2951 (*st).variable_HP_smth2_Q15 = ((silk_lin2log(60) as u32) << 8) as i32;
2952 current_block = 16167632229894708628;
2953 }
2954 OPUS_SET_FORCE_MODE_REQUEST => {
2955 let value_36: i32 = ap.arg::<i32>();
2956 if (value_36 < MODE_SILK_ONLY || value_36 > MODE_CELT_ONLY) && value_36 != OPUS_AUTO {
2957 current_block = 12343738388509029619;
2958 } else {
2959 (*st).user_forced_mode = value_36;
2960 current_block = 16167632229894708628;
2961 }
2962 }
2963 OPUS_SET_LFE_REQUEST => {
2964 let value_37: i32 = ap.arg::<i32>();
2965 (*st).lfe = value_37;
2966 ret = opus_custom_encoder_ctl!(celt_enc, OPUS_SET_LFE_REQUEST, value_37);
2967 current_block = 16167632229894708628;
2968 }
2969 OPUS_SET_ENERGY_MASK_REQUEST => {
2970 let value_38: *mut opus_val16 = ap.arg::<*mut opus_val16>();
2971 (*st).energy_masking = value_38;
2972 ret = opus_custom_encoder_ctl!(
2973 celt_enc,
2974 OPUS_SET_ENERGY_MASK_REQUEST,
2975 value_38.offset(value_38.offset_from(value_38) as i64 as isize),
2976 );
2977 current_block = 16167632229894708628;
2978 }
2979 OPUS_GET_IN_DTX_REQUEST => {
2980 let value_39: &mut i32 = ap.arg::<&mut i32>();
2981 if (*st).silk_mode.useDTX != 0
2982 && ((*st).prev_mode == MODE_SILK_ONLY || (*st).prev_mode == MODE_HYBRID)
2983 {
2984 let mut n: i32 = 0;
2985 let silk_enc_0: *mut core::ffi::c_void = (st as *mut i8)
2986 .offset((*st).silk_enc_offset as isize)
2987 as *mut core::ffi::c_void;
2988 *value_39 = 1;
2989 n = 0;
2990 while n < (*st).silk_mode.nChannelsInternal {
2991 *value_39 = (*value_39 != 0
2992 && (*(silk_enc_0 as *mut silk_encoder)).state_Fxx[n as usize]
2993 .sCmn
2994 .noSpeechCounter
2995 >= NB_SPEECH_FRAMES_BEFORE_DTX) as i32;
2996 n += 1;
2997 }
2998 } else if (*st).use_dtx != 0 {
2999 *value_39 = ((*st).nb_no_activity_frames >= NB_SPEECH_FRAMES_BEFORE_DTX) as i32;
3000 } else {
3001 *value_39 = 0;
3002 }
3003 current_block = 16167632229894708628;
3004 }
3005 CELT_GET_MODE_REQUEST => {
3006 let value_40: &mut *const OpusCustomMode = ap.arg::<&mut *const OpusCustomMode>();
3007 ret = opus_custom_encoder_ctl!(celt_enc, CELT_GET_MODE_REQUEST, value_40);
3008 current_block = 16167632229894708628;
3009 }
3010 _ => {
3011 ret = OPUS_UNIMPLEMENTED;
3012 current_block = 16167632229894708628;
3013 }
3014 }
3015 match current_block {
3016 12343738388509029619 => return OPUS_BAD_ARG,
3017 _ => return ret,
3018 };
3019}
3020#[macro_export]
3021macro_rules! opus_encoder_ctl {
3022 ($st:expr, $request:expr, $($args:expr),*) => {
3023 $crate::opus_encoder_ctl_impl($st, $request, $crate::varargs!($($args),*))
3024 };
3025 ($st:expr, $request:expr, $($args:expr),*,) => {
3026 opus_encoder_ctl!($st, $request, $($args),*)
3027 };
3028 ($st:expr, $request:expr) => {
3029 opus_encoder_ctl!($st, $request,)
3030 };
3031}
3032pub unsafe fn opus_encoder_destroy(st: *mut OpusEncoder) {
3033 free(st as *mut core::ffi::c_void);
3034}