From 582ffb7b157fdefb2dbafb40fa74a21e388b161c Mon Sep 17 00:00:00 2001 From: Kostya Shishkov Date: Wed, 27 Nov 2019 08:20:48 +0100 Subject: [PATCH] core/soundcvt: add DupMono mode for remixing speed-up --- nihav-core/src/soundcvt/mod.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/nihav-core/src/soundcvt/mod.rs b/nihav-core/src/soundcvt/mod.rs index f206ba2..b9030f3 100644 --- a/nihav-core/src/soundcvt/mod.rs +++ b/nihav-core/src/soundcvt/mod.rs @@ -16,12 +16,14 @@ enum ChannelOp { Passthrough, Reorder(Vec), Remix(Vec), + DupMono(Vec), } impl ChannelOp { fn is_remix(&self) -> bool { match *self { ChannelOp::Remix(_) => true, + ChannelOp::DupMono(_) => true, _ => false, } } @@ -52,6 +54,12 @@ fn remix_i32(ch_op: &ChannelOp, src: &Vec, dst: &mut Vec) { *out = sum as i32; } } + if let ChannelOp::DupMono(ref dup_mat) = ch_op { + let src = src[0]; + for (out, copy) in dst.iter_mut().zip(dup_mat.iter()) { + *out = if *copy { src } else { 0 }; + } + } } fn remix_f32(ch_op: &ChannelOp, src: &Vec, dst: &mut Vec) { @@ -65,6 +73,12 @@ fn remix_f32(ch_op: &ChannelOp, src: &Vec, dst: &mut Vec) { *out = sum; } } + if let ChannelOp::DupMono(ref dup_mat) = ch_op { + let src = src[0]; + for (out, copy) in dst.iter_mut().zip(dup_mat.iter()) { + *out = if *copy { src } else { 0.0 }; + } + } } fn read_samples(src: &NAAudioBuffer, mut idx: usize, dst: &mut Vec) { @@ -168,6 +182,7 @@ fn read_packed(src: &NAAudioBuffer, idx: usize, dst: &mut Vec, fm for el in dst.iter_mut() { let src = &data[offset..]; *el = if !fmt.float { +println!("fmt = {} bytes, be: {}", fmt.bits, fmt.be); match (bytes, fmt.be) { (1, _) => src[0].cvt_into(), (2, true) => (read_u16be(src).unwrap() as i16).cvt_into(), @@ -255,9 +270,20 @@ Result { } else if needs_reorder { let reorder_mat = calculate_reorder_matrix(src_chmap, dst_chmap); ChannelOp::Reorder(reorder_mat) - } else { + } else if src_chmap.num_channels() > 1 { let remix_mat = calculate_remix_matrix(src_chmap, dst_chmap); ChannelOp::Remix(remix_mat) + } else { + let mut dup_mat: Vec = Vec::with_capacity(dst_chmap.num_channels()); + for i in 0..dst_chmap.num_channels() { + let ch = dst_chmap.get_channel(i); + if ch.is_left() || ch.is_right() || ch == NAChannelType::C { + dup_mat.push(true); + } else { + dup_mat.push(false); + } + } + ChannelOp::DupMono(dup_mat) }; let src_fmt = src_info.get_format(); -- 2.39.5