Skip to content

Commit 8698542

Browse files
committed
refactor: make opus_decoder_get_size return correct size; prepare for removal of raw malloc from decoder
1 parent 70cd0d1 commit 8698542

File tree

4 files changed

+29
-79
lines changed

4 files changed

+29
-79
lines changed

src/celt/celt_decoder.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub struct OpusCustomDecoder {
9090
pub const PLC_PITCH_LAG_MAX: i32 = 720;
9191
pub const PLC_PITCH_LAG_MIN: i32 = 100;
9292
pub const DECODE_BUFFER_SIZE: usize = 2048;
93-
pub unsafe fn validate_celt_decoder(st: &OpusCustomDecoder) {
93+
pub fn validate_celt_decoder(st: &OpusCustomDecoder) {
9494
assert_eq!(st.mode, opus_custom_mode_create(48000, 960, None).unwrap());
9595
assert_eq!(st.overlap, 120);
9696
assert!(st.channels == 1 || st.channels == 2);
@@ -112,16 +112,6 @@ pub unsafe fn validate_celt_decoder(st: &OpusCustomDecoder) {
112112
assert!(st.postfilter_tapset_old <= 2);
113113
assert!(st.postfilter_tapset_old >= 0);
114114
}
115-
pub fn celt_decoder_get_size(channels: i32) -> usize {
116-
let mode = opus_custom_mode_create(48000, 960, None).unwrap();
117-
opus_custom_decoder_get_size(mode, channels)
118-
}
119-
#[inline]
120-
fn opus_custom_decoder_get_size(_mode: &OpusCustomMode, _channels: i32) -> usize {
121-
// NOTE: we've replaced all the inline allocation tomfoolery with just multiple vector allocations.
122-
// we could probably do better by allocating from a bump allocator or something, but this is just way easier for now
123-
return core::mem::size_of::<OpusCustomDecoder>();
124-
}
125115
pub fn celt_decoder_init(sampling_rate: i32, channels: usize) -> OpusCustomDecoder {
126116
let mode = opus_custom_mode_create(48000, 960, None).unwrap();
127117
let mut st = opus_custom_decoder_init(mode, channels);

src/silk/dec_API.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,7 @@ pub struct silk_decoder {
4545
pub nChannelsInternal: i32,
4646
pub prev_decode_only_middle: i32,
4747
}
48-
pub fn silk_Get_Decoder_Size(decSizeBytes: &mut i32) -> i32 {
49-
let ret: i32 = SILK_NO_ERROR;
50-
*decSizeBytes = ::core::mem::size_of::<silk_decoder>() as u64 as i32;
51-
return ret;
52-
}
53-
pub unsafe fn silk_InitDecoder() -> silk_decoder {
48+
pub fn silk_InitDecoder() -> silk_decoder {
5449
silk_decoder {
5550
channel_state: [silk_init_decoder(), silk_init_decoder()],
5651
sStereo: stereo_dec_state::default(),

src/src/opus_decoder.rs

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ pub mod stddef_h {
88
pub type size_t = u64;
99
pub const NULL: i32 = 0;
1010
}
11-
pub mod cpu_support_h {
12-
#[inline]
13-
pub unsafe fn opus_select_arch() -> i32 {
14-
return 0;
15-
}
16-
}
1711
pub mod stack_alloc_h {
1812
pub const ALLOC_NONE: i32 = 1;
1913
#[inline]
@@ -22,13 +16,10 @@ pub mod stack_alloc_h {
2216
}
2317
}
2418
pub use self::arch_h::{opus_val16, opus_val32};
25-
pub use self::cpu_support_h::opus_select_arch;
2619
pub use self::stack_alloc_h::{_opus_false, ALLOC_NONE};
2720
pub use self::stddef_h::{size_t, NULL};
2821
use crate::celt::celt::CELT_SET_SIGNALLING_REQUEST;
29-
use crate::celt::celt_decoder::{
30-
celt_decode_with_ec, celt_decoder_get_size, celt_decoder_init, OpusCustomDecoder,
31-
};
22+
use crate::celt::celt_decoder::{celt_decode_with_ec, celt_decoder_init, OpusCustomDecoder};
3223
use crate::celt::entcode::ec_tell;
3324
use crate::celt::entdec::ec_dec;
3425
use crate::celt::entdec::{ec_dec_bit_logp, ec_dec_init, ec_dec_uint};
@@ -37,7 +28,7 @@ use crate::celt::mathops::celt_exp2;
3728
use crate::celt::modes::OpusCustomMode;
3829
use crate::externs::memset;
3930
use crate::silk::dec_API::{silk_DecControlStruct, silk_decoder};
40-
use crate::silk::dec_API::{silk_Decode, silk_Get_Decoder_Size, silk_InitDecoder};
31+
use crate::silk::dec_API::{silk_Decode, silk_InitDecoder};
4132
use crate::src::opus::opus_packet_parse_impl;
4233
use crate::src::opus_defines::{
4334
OPUS_ALLOC_FAIL, OPUS_BAD_ARG, OPUS_BANDWIDTH_FULLBAND, OPUS_BANDWIDTH_MEDIUMBAND,
@@ -62,7 +53,6 @@ pub struct OpusDecoder {
6253
pub(crate) Fs: i32,
6354
pub(crate) DecControl: silk_DecControlStruct,
6455
pub(crate) decode_gain: i32,
65-
pub(crate) arch: i32,
6656
pub(crate) stream_channels: i32,
6757
pub(crate) bandwidth: i32,
6858
pub(crate) mode: i32,
@@ -102,26 +92,10 @@ unsafe fn validate_opus_decoder(st: &OpusDecoder) {
10292
|| (*st).DecControl.payloadSize_ms == 40
10393
|| (*st).DecControl.payloadSize_ms == 60
10494
);
105-
assert!((*st).arch >= 0);
106-
assert!((*st).arch <= 0);
10795
assert!((*st).stream_channels == 1 || (*st).stream_channels == 2);
10896
}
109-
pub unsafe fn opus_decoder_get_size(channels: i32) -> i32 {
110-
let mut silkDecSizeBytes: i32 = 0;
111-
let mut celtDecSizeBytes: i32 = 0;
112-
let mut ret: i32 = 0;
113-
if channels < 1 || channels > 2 {
114-
return 0;
115-
}
116-
ret = silk_Get_Decoder_Size(&mut silkDecSizeBytes);
117-
if ret != 0 {
118-
return 0;
119-
}
120-
silkDecSizeBytes = align(silkDecSizeBytes);
121-
celtDecSizeBytes = celt_decoder_get_size(channels) as i32;
122-
return align(::core::mem::size_of::<OpusDecoder>() as u64 as i32)
123-
+ silkDecSizeBytes
124-
+ celtDecSizeBytes;
97+
pub fn opus_decoder_get_size(_channels: i32) -> usize {
98+
align(core::mem::size_of::<OpusDecoder>() as _) as usize
12599
}
126100
pub unsafe fn opus_decoder_init(st: *mut OpusDecoder, Fs: i32, channels: i32) -> i32 {
127101
if Fs != 48000 && Fs != 24000 && Fs != 16000 && Fs != 12000 && Fs != 8000
@@ -144,7 +118,6 @@ pub unsafe fn opus_decoder_init(st: *mut OpusDecoder, Fs: i32, channels: i32) ->
144118
opus_custom_decoder_ctl!(&mut (*st).celt_dec, CELT_SET_SIGNALLING_REQUEST, 0);
145119
(*st).prev_mode = 0;
146120
(*st).frame_size = Fs / 400;
147-
(*st).arch = opus_select_arch();
148121
return OPUS_OK;
149122
}
150123
pub unsafe fn opus_decoder_create(Fs: i32, channels: i32, error: *mut i32) -> *mut OpusDecoder {
@@ -413,7 +386,7 @@ unsafe fn opus_decode_frame(
413386
&mut dec,
414387
pcm_ptr,
415388
&mut silk_frame_size,
416-
(*st).arch,
389+
0,
417390
);
418391
if silk_ret != 0 {
419392
if lost_flag != 0 {

src/src/opus_multistream_decoder.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,14 @@ unsafe fn validate_ms_decoder(st: *mut OpusMSDecoder) {
5353
validate_layout(&mut (*st).layout);
5454
}
5555
pub unsafe fn opus_multistream_decoder_get_size(nb_streams: i32, nb_coupled_streams: i32) -> i32 {
56-
let mut coupled_size: i32 = 0;
57-
let mut mono_size: i32 = 0;
5856
if nb_streams < 1 || nb_coupled_streams > nb_streams || nb_coupled_streams < 0 {
5957
return 0;
6058
}
61-
coupled_size = opus_decoder_get_size(2);
62-
mono_size = opus_decoder_get_size(1);
59+
let coupled_size = opus_decoder_get_size(2);
60+
let mono_size = opus_decoder_get_size(1);
6361
return align(::core::mem::size_of::<OpusMSDecoder>() as u64 as i32)
64-
+ nb_coupled_streams * align(coupled_size)
65-
+ (nb_streams - nb_coupled_streams) * align(mono_size);
62+
+ nb_coupled_streams * align(coupled_size as _)
63+
+ (nb_streams - nb_coupled_streams) * align(mono_size as _);
6664
}
6765
pub unsafe fn opus_multistream_decoder_init(
6866
st: *mut OpusMSDecoder,
@@ -72,8 +70,6 @@ pub unsafe fn opus_multistream_decoder_init(
7270
coupled_streams: i32,
7371
mapping: *const u8,
7472
) -> i32 {
75-
let mut coupled_size: i32 = 0;
76-
let mut mono_size: i32 = 0;
7773
let mut i: i32 = 0;
7874
let mut ret: i32 = 0;
7975
let mut ptr: *mut i8 = 0 as *mut i8;
@@ -99,23 +95,23 @@ pub unsafe fn opus_multistream_decoder_init(
9995
}
10096
ptr = (st as *mut i8)
10197
.offset(align(::core::mem::size_of::<OpusMSDecoder>() as u64 as i32) as isize);
102-
coupled_size = opus_decoder_get_size(2);
103-
mono_size = opus_decoder_get_size(1);
98+
let coupled_size = opus_decoder_get_size(2);
99+
let mono_size = opus_decoder_get_size(1);
104100
i = 0;
105101
while i < (*st).layout.nb_coupled_streams {
106102
ret = opus_decoder_init(ptr as *mut OpusDecoder, Fs, 2);
107103
if ret != OPUS_OK {
108104
return ret;
109105
}
110-
ptr = ptr.offset(align(coupled_size) as isize);
106+
ptr = ptr.offset(align(coupled_size as _) as isize);
111107
i += 1;
112108
}
113109
while i < (*st).layout.nb_streams {
114110
ret = opus_decoder_init(ptr as *mut OpusDecoder, Fs, 1);
115111
if ret != OPUS_OK {
116112
return ret;
117113
}
118-
ptr = ptr.offset(align(mono_size) as isize);
114+
ptr = ptr.offset(align(mono_size as _) as isize);
119115
i += 1;
120116
}
121117
return OPUS_OK;
@@ -214,8 +210,6 @@ pub unsafe fn opus_multistream_decode_native(
214210
user_data: *mut core::ffi::c_void,
215211
) -> i32 {
216212
let mut Fs: i32 = 0;
217-
let mut coupled_size: i32 = 0;
218-
let mut mono_size: i32 = 0;
219213
let mut s: i32 = 0;
220214
let mut c: i32 = 0;
221215
let mut ptr: *mut i8 = 0 as *mut i8;
@@ -234,8 +228,8 @@ pub unsafe fn opus_multistream_decode_native(
234228
let mut buf: Vec<opus_val16> = ::std::vec::from_elem(0., vla);
235229
ptr = (st as *mut i8)
236230
.offset(align(::core::mem::size_of::<OpusMSDecoder>() as u64 as i32) as isize);
237-
coupled_size = opus_decoder_get_size(2);
238-
mono_size = opus_decoder_get_size(1);
231+
let coupled_size = opus_decoder_get_size(2);
232+
let mono_size = opus_decoder_get_size(1);
239233
if len == 0 {
240234
do_plc = 1;
241235
}
@@ -263,9 +257,9 @@ pub unsafe fn opus_multistream_decode_native(
263257
dec = ptr as *mut OpusDecoder;
264258
ptr = ptr.offset(
265259
(if s < (*st).layout.nb_coupled_streams {
266-
align(coupled_size)
260+
align(coupled_size as _)
267261
} else {
268-
align(mono_size)
262+
align(mono_size as _)
269263
}) as isize,
270264
);
271265
if do_plc == 0 && len <= 0 {
@@ -492,12 +486,10 @@ pub unsafe fn opus_multistream_decoder_ctl_va_list(
492486
mut ap: VarArgs,
493487
) -> i32 {
494488
let current_block: u64;
495-
let mut coupled_size: i32 = 0;
496-
let mut mono_size: i32 = 0;
497489
let mut ptr: *mut i8 = 0 as *mut i8;
498490
let mut ret: i32 = OPUS_OK;
499-
coupled_size = opus_decoder_get_size(2);
500-
mono_size = opus_decoder_get_size(1);
491+
let coupled_size = opus_decoder_get_size(2);
492+
let mono_size = opus_decoder_get_size(1);
501493
ptr = (st as *mut i8)
502494
.offset(align(::core::mem::size_of::<OpusMSDecoder>() as u64 as i32) as isize);
503495
match request {
@@ -522,9 +514,9 @@ pub unsafe fn opus_multistream_decoder_ctl_va_list(
522514
let mut dec_0: *mut OpusDecoder = 0 as *mut OpusDecoder;
523515
dec_0 = ptr as *mut OpusDecoder;
524516
if s < (*st).layout.nb_coupled_streams {
525-
ptr = ptr.offset(align(coupled_size) as isize);
517+
ptr = ptr.offset(align(coupled_size as _) as isize);
526518
} else {
527-
ptr = ptr.offset(align(mono_size) as isize);
519+
ptr = ptr.offset(align(mono_size as _) as isize);
528520
}
529521
ret = opus_decoder_ctl!(dec_0, request, &mut tmp);
530522
if ret != OPUS_OK {
@@ -542,9 +534,9 @@ pub unsafe fn opus_multistream_decoder_ctl_va_list(
542534
let mut dec_1: *mut OpusDecoder = 0 as *mut OpusDecoder;
543535
dec_1 = ptr as *mut OpusDecoder;
544536
if s_0 < (*st).layout.nb_coupled_streams {
545-
ptr = ptr.offset(align(coupled_size) as isize);
537+
ptr = ptr.offset(align(coupled_size as _) as isize);
546538
} else {
547-
ptr = ptr.offset(align(mono_size) as isize);
539+
ptr = ptr.offset(align(mono_size as _) as isize);
548540
}
549541
ret = opus_decoder_ctl!(dec_1, OPUS_RESET_STATE);
550542
if ret != OPUS_OK {
@@ -566,9 +558,9 @@ pub unsafe fn opus_multistream_decoder_ctl_va_list(
566558
s_1 = 0;
567559
while s_1 < stream_id {
568560
if s_1 < (*st).layout.nb_coupled_streams {
569-
ptr = ptr.offset(align(coupled_size) as isize);
561+
ptr = ptr.offset(align(coupled_size as _) as isize);
570562
} else {
571-
ptr = ptr.offset(align(mono_size) as isize);
563+
ptr = ptr.offset(align(mono_size as _) as isize);
572564
}
573565
s_1 += 1;
574566
}
@@ -584,9 +576,9 @@ pub unsafe fn opus_multistream_decoder_ctl_va_list(
584576
let mut dec_2: *mut OpusDecoder = 0 as *mut OpusDecoder;
585577
dec_2 = ptr as *mut OpusDecoder;
586578
if s_2 < (*st).layout.nb_coupled_streams {
587-
ptr = ptr.offset(align(coupled_size) as isize);
579+
ptr = ptr.offset(align(coupled_size as _) as isize);
588580
} else {
589-
ptr = ptr.offset(align(mono_size) as isize);
581+
ptr = ptr.offset(align(mono_size as _) as isize);
590582
}
591583
ret = opus_decoder_ctl!(dec_2, request, value_2);
592584
if ret != OPUS_OK {

0 commit comments

Comments
 (0)