|
| 1 | +use crate::{ |
| 2 | + encoder::EncoderSettings, sea_decode as rust_sea_decode, sea_encode as rust_sea_encode, |
| 3 | +}; |
| 4 | +use alloc::vec::Vec; |
| 5 | +use core::ffi::c_float; |
| 6 | +use core::slice; |
| 7 | + |
| 8 | +#[repr(C)] |
| 9 | +pub struct CSeaEncoderSettings { |
| 10 | + pub scale_factor_bits: u8, |
| 11 | + pub scale_factor_frames: u8, |
| 12 | + pub residual_bits: c_float, |
| 13 | + pub frames_per_chunk: u16, |
| 14 | + pub vbr: bool, |
| 15 | +} |
| 16 | + |
| 17 | +impl From<&CSeaEncoderSettings> for EncoderSettings { |
| 18 | + fn from(c_settings: &CSeaEncoderSettings) -> Self { |
| 19 | + Self { |
| 20 | + scale_factor_bits: c_settings.scale_factor_bits, |
| 21 | + scale_factor_frames: c_settings.scale_factor_frames, |
| 22 | + residual_bits: c_settings.residual_bits, |
| 23 | + frames_per_chunk: c_settings.frames_per_chunk, |
| 24 | + vbr: c_settings.vbr, |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +#[no_mangle] |
| 30 | +pub extern "C" fn sea_encoder_default_settings() -> CSeaEncoderSettings { |
| 31 | + let default = EncoderSettings::default(); |
| 32 | + CSeaEncoderSettings { |
| 33 | + scale_factor_bits: default.scale_factor_bits, |
| 34 | + scale_factor_frames: default.scale_factor_frames, |
| 35 | + residual_bits: default.residual_bits, |
| 36 | + frames_per_chunk: default.frames_per_chunk, |
| 37 | + vbr: default.vbr, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +#[no_mangle] |
| 42 | +pub unsafe extern "C" fn sea_encode( |
| 43 | + input_samples: *const i16, |
| 44 | + input_length: usize, |
| 45 | + sample_rate: u32, |
| 46 | + channels: u32, |
| 47 | + settings: *const CSeaEncoderSettings, |
| 48 | + output_data: *mut *mut u8, |
| 49 | + output_length: *mut usize, |
| 50 | +) -> i32 { |
| 51 | + if input_samples.is_null() || output_data.is_null() || output_length.is_null() { |
| 52 | + return -1; |
| 53 | + } |
| 54 | + |
| 55 | + let input_slice = slice::from_raw_parts(input_samples, input_length); |
| 56 | + let encoder_settings = if settings.is_null() { |
| 57 | + EncoderSettings::default() |
| 58 | + } else { |
| 59 | + EncoderSettings::from(&*settings) |
| 60 | + }; |
| 61 | + |
| 62 | + let mut encoded = rust_sea_encode(input_slice, sample_rate, channels, encoder_settings); |
| 63 | + |
| 64 | + encoded.shrink_to_fit(); |
| 65 | + let ptr = encoded.as_mut_ptr(); |
| 66 | + let len = encoded.len(); |
| 67 | + core::mem::forget(encoded); |
| 68 | + |
| 69 | + *output_data = ptr; |
| 70 | + *output_length = len; |
| 71 | + |
| 72 | + 0 |
| 73 | +} |
| 74 | + |
| 75 | +#[no_mangle] |
| 76 | +pub unsafe extern "C" fn sea_decode( |
| 77 | + encoded_data: *const u8, |
| 78 | + encoded_length: usize, |
| 79 | + output_samples: *mut *mut i16, |
| 80 | + output_sample_count: *mut usize, |
| 81 | + output_sample_rate: *mut u32, |
| 82 | + output_channels: *mut u32, |
| 83 | +) -> i32 { |
| 84 | + if encoded_data.is_null() || output_samples.is_null() || output_sample_count.is_null() { |
| 85 | + return -1; |
| 86 | + } |
| 87 | + |
| 88 | + let encoded_slice = slice::from_raw_parts(encoded_data, encoded_length); |
| 89 | + |
| 90 | + let decode_info = rust_sea_decode(encoded_slice); |
| 91 | + |
| 92 | + let mut samples = decode_info.samples; |
| 93 | + samples.shrink_to_fit(); |
| 94 | + let ptr = samples.as_mut_ptr(); |
| 95 | + let len = samples.len(); |
| 96 | + core::mem::forget(samples); |
| 97 | + |
| 98 | + *output_samples = ptr; |
| 99 | + *output_sample_count = len; |
| 100 | + |
| 101 | + if !output_sample_rate.is_null() { |
| 102 | + *output_sample_rate = decode_info.sample_rate; |
| 103 | + } |
| 104 | + if !output_channels.is_null() { |
| 105 | + *output_channels = decode_info.channels; |
| 106 | + } |
| 107 | + |
| 108 | + 0 |
| 109 | +} |
| 110 | + |
| 111 | +#[no_mangle] |
| 112 | +pub unsafe extern "C" fn sea_free_packet(data: *mut u8, length: usize) { |
| 113 | + if !data.is_null() { |
| 114 | + let _ = Vec::from_raw_parts(data, length, length); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +#[no_mangle] |
| 119 | +pub unsafe extern "C" fn sea_free_samples(samples: *mut i16, length: usize) { |
| 120 | + if !samples.is_null() { |
| 121 | + let _ = Vec::from_raw_parts(samples, length, length); |
| 122 | + } |
| 123 | +} |
0 commit comments