Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Luni-4 committed Mar 15, 2024
1 parent 3f94f5a commit 42b7e48
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl AudioChannel {
channels_map.is_empty().then_some(channels_map)
}

pub(crate) fn into_ffms2(channels_map: Vec<Self>) -> i64 {
pub(crate) fn into_ffms2(channels_map: &[Self]) -> i64 {
channels_map.iter().fold(0, |acc, audio_channel| {
if let Some(channel) = Self::AUDIO_CHANNELS
.iter()
Expand Down Expand Up @@ -405,7 +405,7 @@ impl AudioSource {

/// Sets audio samples output format.
pub fn output_format(&self, options: &ResampleOptions) -> Result<()> {
let channel_layout = match options.channel_layout {
let channel_layout = match &options.channel_layout {
Some(channel_layout) => channel_layout,
None => {
return Err(Error::FFMS2(Cow::Borrowed(
Expand All @@ -427,7 +427,7 @@ impl AudioSource {
let err = unsafe {
ffms2_sys::FFMS_SetOutputFormatA(
self.0,
options.into_ffms2(channel_layout, sample_format),
&options.ffms2_resample(channel_layout, sample_format),
error.as_mut_ptr(),
)
};
Expand Down
101 changes: 88 additions & 13 deletions src/resample.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,47 @@ pub enum MatrixEncoding {
}

impl MatrixEncoding {
pub(crate) const fn new(matrix_encoding: FFMS_MatrixEncoding) -> Self {
use ffms2_sys::FFMS_MatrixEncoding::*;
const fn new(matrix_encoding: FFMS_MatrixEncoding) -> Self {
match matrix_encoding {
FFMS_MATRIX_ENCODING_NONE => Self::None,
FFMS_MATRIX_ENCODING_DOBLY => Self::Dolby,
FFMS_MATRIX_ENCODING_PRO_LOGIC_II => Self::ProLogicII,
FFMS_MATRIX_ENCODING_PRO_LOGIC_IIX => Self::ProLogicIIX,
FFMS_MATRIX_ENCODING_PRO_LOGIC_IIZ => Self::ProLogicIIZ,
FFMS_MATRIX_ENCODING_DOLBY_EX => Self::DolbyEx,
FFMS_MATRIX_ENCODING_DOLBY_HEADPHONE => Self::DolbyHeadphone,
FFMS_MatrixEncoding::FFMS_MATRIX_ENCODING_NONE => Self::None,
FFMS_MatrixEncoding::FFMS_MATRIX_ENCODING_DOBLY => Self::Dolby,
FFMS_MatrixEncoding::FFMS_MATRIX_ENCODING_PRO_LOGIC_II => {
Self::ProLogicII
}
FFMS_MatrixEncoding::FFMS_MATRIX_ENCODING_PRO_LOGIC_IIX => {
Self::ProLogicIIX
}
FFMS_MatrixEncoding::FFMS_MATRIX_ENCODING_PRO_LOGIC_IIZ => {
Self::ProLogicIIZ
}
FFMS_MatrixEncoding::FFMS_MATRIX_ENCODING_DOLBY_EX => {
Self::DolbyEx
}
FFMS_MatrixEncoding::FFMS_MATRIX_ENCODING_DOLBY_HEADPHONE => {
Self::DolbyHeadphone
}
}
}

const fn into_ffms2(self) -> FFMS_MatrixEncoding {
match self {
Self::None => FFMS_MatrixEncoding::FFMS_MATRIX_ENCODING_NONE,
Self::Dolby => FFMS_MatrixEncoding::FFMS_MATRIX_ENCODING_DOBLY,
Self::ProLogicII => {
FFMS_MatrixEncoding::FFMS_MATRIX_ENCODING_PRO_LOGIC_II
}
Self::ProLogicIIX => {
FFMS_MatrixEncoding::FFMS_MATRIX_ENCODING_PRO_LOGIC_IIX
}
Self::ProLogicIIZ => {
FFMS_MatrixEncoding::FFMS_MATRIX_ENCODING_PRO_LOGIC_IIZ
}
Self::DolbyEx => {
FFMS_MatrixEncoding::FFMS_MATRIX_ENCODING_DOLBY_EX
}
Self::DolbyHeadphone => {
FFMS_MatrixEncoding::FFMS_MATRIX_ENCODING_DOLBY_HEADPHONE
}
}
}
}
Expand Down Expand Up @@ -81,7 +112,7 @@ impl SampleFormat {
}
}

const fn into_ffms2(&self) -> FFMS_SampleFormat {
const fn into_ffms2(self) -> FFMS_SampleFormat {
match self {
Self::U8 => FFMS_SampleFormat::FFMS_FMT_U8,
Self::S16 => FFMS_SampleFormat::FFMS_FMT_S16,
Expand Down Expand Up @@ -120,6 +151,21 @@ impl ResampleFilterType {
}
}
}

const fn into_ffms2(self) -> (i32, FFMS_ResampleFilterType) {
match self {
Self::Cubic => {
(0, FFMS_ResampleFilterType::FFMS_RESAMPLE_FILTER_CUBIC)
}
Self::Sinc => {
(0, FFMS_ResampleFilterType::FFMS_RESAMPLE_FILTER_SINC)
}
Self::Kaiser(kaiser_beta) => (
kaiser_beta as i32,
FFMS_ResampleFilterType::FFMS_RESAMPLE_FILTER_KAISER,
),
}
}
}

/// Audio dither method.
Expand Down Expand Up @@ -147,6 +193,16 @@ impl AudioDitherMethod {
FFMS_AudioDitherMethod::FFMS_RESAMPLE_DITHER_TRIANGULAR_NOISESHAPING => Self::TriangularNoiseShaping,
}
}

const fn into_ffms2(self) -> FFMS_AudioDitherMethod {
match self {
Self::None => FFMS_AudioDitherMethod::FFMS_RESAMPLE_DITHER_NONE,
Self::Rectangular => FFMS_AudioDitherMethod::FFMS_RESAMPLE_DITHER_RECTANGULAR,
Self::Triangular => FFMS_AudioDitherMethod::FFMS_RESAMPLE_DITHER_TRIANGULAR,
Self::TriangularHighPass => FFMS_AudioDitherMethod::FFMS_RESAMPLE_DITHER_TRIANGULAR_HIGHPASS,
Self::TriangularNoiseShaping => FFMS_AudioDitherMethod::FFMS_RESAMPLE_DITHER_TRIANGULAR_NOISESHAPING,
}
}
}

/// Channel Mixing Matrix Coefficient Types.
Expand All @@ -172,6 +228,18 @@ impl MixingCoefficientType {
}
}
}

const fn into_ffms2(self) -> FFMS_MixingCoefficientType {
match self {
Self::Q8 => FFMS_MixingCoefficientType::FFMS_MIXING_COEFFICIENT_Q8,
Self::Q15 => {
FFMS_MixingCoefficientType::FFMS_MIXING_COEFFICIENT_Q15
}
Self::Flt => {
FFMS_MixingCoefficientType::FFMS_MIXING_COEFFICIENT_FLT
}
}
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -255,23 +323,30 @@ impl ResampleOptions {
}
}

pub(crate) fn into_ffms2(
pub(crate) fn ffms2_resample(
&self,
channel_layout: Vec<AudioChannel>,
channel_layout: &[AudioChannel],
sample_format: SampleFormat,
) -> FFMS_ResampleOptions {
let (kaiser_beta, filter_type) = self.filter_type.into_ffms2();
FFMS_ResampleOptions {
ChannelLayout: AudioChannel::into_ffms2(channel_layout),
SampleFormat: sample_format.into_ffms2(),
SampleRate: self.sample_rate as i32,
MixingCoefficientType: self.mixing_coefficient.into_ffms2(),
MixingCoefficientType: self.mixing_coefficient_type.into_ffms2(),
CenterMixLevel: self.center_mix_level,
SurroundMixLevel: self.surround_mix_level,
LFEMixLevel: self.lfe_mix_level,
Normalize: self.normalize as i32,
ForceResample: if self.force_resample { 1 } else { 0 },
ResampleFilterSize: self.filter_size as i32,
ResamplePhaseShift: self.phase_shift as i32,
LinearInterpolation: if self.linear_interpolation { 1 } else { 0 },
CutoffFrequencyRatio: self.cutoff_frequency_ratio,
MatrixedStereoEncoding: self.matrix_stereo_encoding.into_ffms2(),
KaiserBeta: kaiser_beta,
FilterType: filter_type,
DitherMethod: self.audio_dither_method.into_ffms2(),
}
}
}

0 comments on commit 42b7e48

Please sign in to comment.