From a43ad8f2dd93abb1ae88c4f0b2e8a77b1f1fc0e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Dr=C3=B6ge?= Date: Thu, 30 Jan 2020 10:43:39 +0200 Subject: [PATCH] lewton: Don't copy decoded samples if no channel reordering is needed --- gst-plugin-lewton/src/lewtondec.rs | 71 ++++++++++++++++++------------ 1 file changed, 42 insertions(+), 29 deletions(-) diff --git a/gst-plugin-lewton/src/lewtondec.rs b/gst-plugin-lewton/src/lewtondec.rs index 9e0b8657..8910f4a6 100644 --- a/gst-plugin-lewton/src/lewtondec.rs +++ b/gst-plugin-lewton/src/lewtondec.rs @@ -418,33 +418,32 @@ impl LewtonDec { return element.finish_frame(None, 1); } - let mut outbuf = element - .allocate_output_buffer(sample_count as usize * audio_info.bpf() as usize) - .map_err(|_| { - gst_element_error!( - element, - gst::StreamError::Decode, - ["Failed to allocate output buffer"] - ); - gst::FlowError::Error - })?; + let outbuf = if let Some(ref reorder_map) = state.reorder_map { + let mut outbuf = element + .allocate_output_buffer(sample_count as usize * audio_info.bpf() as usize) + .map_err(|_| { + gst_element_error!( + element, + gst::StreamError::Decode, + ["Failed to allocate output buffer"] + ); + gst::FlowError::Error + })?; + { + // And copy the decoded data into our output buffer while reordering the channels to the + // GStreamer channel order + let outbuf = outbuf.get_mut().unwrap(); + let mut outmap = outbuf.map_writable().map_err(|_| { + gst_element_error!( + element, + gst::StreamError::Decode, + ["Failed to map output buffer writable"] + ); + gst::FlowError::Error + })?; - // And copy the decoded data into our output buffer, if needed - // while reordering the channels to the GStreamer channel order - { - let outbuf = outbuf.get_mut().unwrap(); - let mut outmap = outbuf.map_writable().map_err(|_| { - gst_element_error!( - element, - gst::StreamError::Decode, - ["Failed to map output buffer writable"] - ); - gst::FlowError::Error - })?; - - let outdata = outmap.as_mut_slice_of::().unwrap(); - let channels = audio_info.channels() as usize; - if let Some(ref reorder_map) = state.reorder_map { + let outdata = outmap.as_mut_slice_of::().unwrap(); + let channels = audio_info.channels() as usize; assert!(reorder_map.len() >= channels); assert!(reorder_map[..channels].iter().all(|c| *c < channels)); @@ -456,10 +455,24 @@ impl LewtonDec { output[reorder_map[c]] = *s; } } - } else { - outdata.copy_from_slice(&decoded.samples); } - } + + outbuf + } else { + struct CastVec(Vec); + impl AsRef<[u8]> for CastVec { + fn as_ref(&self) -> &[u8] { + self.0.as_byte_slice() + } + } + impl AsMut<[u8]> for CastVec { + fn as_mut(&mut self) -> &mut [u8] { + self.0.as_mut_byte_slice() + } + } + + gst::Buffer::from_mut_slice(CastVec(decoded.samples)) + }; element.finish_frame(Some(outbuf), 1) }