Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(media-info): copy trimmed packed audio by frame

vedanth70ae90e7bc1e
diff --git a/crates/media-info/examples/packed-audio-wrapping-benchmark.rs b/crates/media-info/examples/packed-audio-wrapping-benchmark.rs
new file mode 100644
index 000000000..56bc60a24
--- /dev/null
+++ b/crates/media-info/examples/packed-audio-wrapping-benchmark.rs
@@ -0,0 +1,49 @@
+use cap_media_info::AudioInfo;
+use ffmpeg::format::{Sample, sample::Type};
+use std::{
+ collections::hash_map::DefaultHasher,
+ hash::{Hash, Hasher},
+ hint::black_box,
+ time::Instant,
+};
+
+fn main() {
+ println!("format,channels,frames,sample,ns_per_call,fingerprint");
+ for (format, channels) in [
+ (Sample::F32(Type::Packed), 1),
+ (Sample::F32(Type::Packed), 2),
+ (Sample::F32(Type::Packed), 8),
+ (Sample::F32(Type::Packed), 16),
+ (Sample::F32(Type::Packed), 32),
+ (Sample::I16(Type::Packed), 16),
+ (Sample::F64(Type::Packed), 16),
+ ] {
+ let info = AudioInfo::new_raw(format, 48_000, channels);
+ for (frames, iterations) in [(64, 1000), (480, 500), (4096, 100)] {
+ let bytes = (0..frames * usize::from(channels) * info.sample_size())
+ .map(|index| (index * 97 + 31) as u8)
+ .collect::<Vec<_>>();
+ let wrap = || info.wrap_frame_with_max_channels(black_box(&bytes), black_box(8));
+ let frame = wrap();
+ assert_eq!(frame.samples(), frames);
+ assert_eq!(frame.channels(), channels.min(8));
+ let mut hash = DefaultHasher::new();
+ let used = frames * usize::from(channels.min(8)) * info.sample_size();
+ frame.data(0)[..used].hash(&mut hash);
+ let fingerprint = hash.finish();
+ for sample in 0..18 {
+ let start = Instant::now();
+ for _ in 0..iterations {
+ black_box(wrap());
+ }
+ let elapsed = start.elapsed().as_nanos() as f64 / iterations as f64;
+ if sample >= 3 {
+ println!(
+ "{format:?},{channels},{frames},{},{elapsed:.3},{fingerprint}",
+ sample - 3
+ );
+ }
+ }
+ }
+ }
+}
diff --git a/crates/media-info/src/lib.rs b/crates/media-info/src/lib.rs
index b2eb91ead..67ba2b2e4 100644
--- a/crates/media-info/src/lib.rs
+++ b/crates/media-info/src/lib.rs
@@ -209,17 +209,12 @@ impl AudioInfo {
// frame is allocated with parameters derived from packed_data, so this is safe
frame.data_mut(0)[0..packed_data.len()].copy_from_slice(packed_data);
} else if frame.is_packed() && input_channels > out_channels {
- for (chunk_index, packed_chunk) in packed_data.chunks(packed_sample_size).enumerate() {
- let start = chunk_index * sample_size * out_channels;
-
- let copy_len = sample_size * out_channels;
-
- if let (Some(chunk_slice), Some(frame_slice)) = (
- packed_chunk.get(0..copy_len),
- frame.data_mut(0).get_mut(start..start + copy_len),
- ) {
- frame_slice.copy_from_slice(chunk_slice);
- }
+ let copy_len = sample_size * out_channels;
+ for (packed_chunk, frame_chunk) in packed_data
+ .chunks_exact(packed_sample_size)
+ .zip(frame.data_mut(0).chunks_exact_mut(copy_len))
+ {
+ frame_chunk.copy_from_slice(&packed_chunk[..copy_len]);
}
} else {
for (chunk_index, packed_chunk) in packed_data.chunks(packed_sample_size).enumerate() {
@@ -490,6 +485,45 @@ mod tests {
assert_eq!(&frame.data(0)[0..4], &[1, 2, 3, 4]);
}
+ #[test]
+ fn packed_channel_limits_preserve_complete_frames_and_sample_bytes() {
+ for format in [
+ Sample::U8(Type::Packed),
+ Sample::I16(Type::Packed),
+ Sample::I32(Type::Packed),
+ Sample::F32(Type::Packed),
+ Sample::F64(Type::Packed),
+ ] {
+ for channels in [0, 1, 2, 6, 8, 9, 16, 32] {
+ let info = AudioInfo::new_raw(format, 48_000, channels);
+ let input_stride = info.sample_size() * usize::from(channels.max(1));
+ for max in [0, 1, 2, 8, 64] {
+ let output_channels = usize::from(channels.max(1)).min(max.max(1)).min(8);
+ let output_stride = info.sample_size() * output_channels;
+ for samples in [0, 1, 3, 64] {
+ for tail in [0, input_stride - 1] {
+ let input = (0..samples * input_stride + tail)
+ .map(|index| (index * 97 + 31) as u8)
+ .collect::<Vec<_>>();
+ let expected = input
+ .chunks_exact(input_stride)
+ .flat_map(|frame| frame[..output_stride].iter().copied())
+ .collect::<Vec<_>>();
+ let frame = info.wrap_frame_with_max_channels(&input, max);
+ assert_eq!(frame.samples(), samples);
+ assert_eq!(frame.channels() as usize, output_channels);
+ assert_eq!(frame.rate(), 48_000);
+ assert_eq!(frame.format(), format);
+ if samples > 0 {
+ assert_eq!(&frame.data(0)[..expected.len()], expected);
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
#[test]
fn wrap_packed_frame_max_channels() {
let info = AudioInfo::new_raw(Sample::U8(Type::Packed), 2, 4);