From 34e473dc4978cc115ed1ba64f77293162c800a1e Mon Sep 17 00:00:00 2001 From: Jamie Hardt Date: Mon, 23 Nov 2020 22:38:10 -0800 Subject: [PATCH] Implementation of complex formats --- Cargo.lock | 7 ++ Cargo.toml | 1 + src/audio_frame_reader.rs | 10 +-- src/chunks.rs | 60 ++++++++++++++-- src/common_format.rs | 86 +++++++++++++++++++++++ src/errors.rs | 14 +++- src/fmt.rs | 142 ++++++++++++++++++++++++++------------ src/lib.rs | 11 ++- src/wavereader.rs | 4 +- tests/integration_test.rs | 2 +- 10 files changed, 275 insertions(+), 62 deletions(-) create mode 100644 src/common_format.rs diff --git a/Cargo.lock b/Cargo.lock index 12e1441..4a1fe24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,7 @@ dependencies = [ "byteorder", "encoding", "serde_json", + "uuid", ] [[package]] @@ -107,3 +108,9 @@ dependencies = [ "ryu", "serde", ] + +[[package]] +name = "uuid" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" diff --git a/Cargo.toml b/Cargo.toml index 0e663da..5c4488e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,4 +16,5 @@ keywords = ["audio", "broadcast", "multimedia","smpte"] [dependencies] byteorder = "1.3.4" encoding = "0.2.33" +uuid = "0.8.1" serde_json = "1.0.59" \ No newline at end of file diff --git a/src/audio_frame_reader.rs b/src/audio_frame_reader.rs index ddc36d1..6715fee 100644 --- a/src/audio_frame_reader.rs +++ b/src/audio_frame_reader.rs @@ -4,7 +4,7 @@ use std::io::SeekFrom::{Start,}; use byteorder::LittleEndian; use byteorder::ReadBytesExt; -use super::fmt::WaveFmt; +use super::fmt::{WaveFmt}; use super::errors::Error; /// Read audio frames @@ -33,7 +33,10 @@ impl AudioFrameReader { "Unable to read audio frames from packed formats: block alignment is {}, should be {}", format.block_alignment, (format.bits_per_sample / 8 ) * format.channel_count); - assert!(format.tag == 1, "Unsupported format tag {}", format.tag); + + assert!(format.tag == 0x01 , + "Unsupported format tag {:?}", format.tag); + AudioFrameReader { inner , format } } @@ -83,5 +86,4 @@ impl AudioFrameReader { Ok( 1 ) } -} - +} \ No newline at end of file diff --git a/src/chunks.rs b/src/chunks.rs index e7402c0..1f3e4c4 100644 --- a/src/chunks.rs +++ b/src/chunks.rs @@ -1,7 +1,5 @@ use std::io::{Read, Write}; -use super::errors::Error as ParserError; - use encoding::{DecoderTrap, EncoderTrap}; use encoding::{Encoding}; use encoding::all::ASCII; @@ -9,7 +7,10 @@ use encoding::all::ASCII; use byteorder::LittleEndian; use byteorder::{ReadBytesExt, WriteBytesExt}; -use super::fmt::WaveFmt; +use uuid::Uuid; + +use super::errors::Error as ParserError; +use super::fmt::{WaveFmt, WaveFmtExtended}; use super::bext::Bext; pub trait ReadBWaveChunks: Read { @@ -26,7 +27,7 @@ pub trait WriteBWaveChunks: Write { impl WriteBWaveChunks for T where T: Write { fn write_wave_fmt(&mut self, format : &WaveFmt) -> Result<(), ParserError> { - self.write_u16::(format.tag)?; + self.write_u16::(format.tag as u16 )?; self.write_u16::(format.channel_count)?; self.write_u32::(format.sample_rate)?; self.write_u32::(format.bytes_per_second)?; @@ -86,14 +87,34 @@ impl WriteBWaveChunks for T where T: Write { impl ReadBWaveChunks for T where T: Read { fn read_wave_fmt(&mut self) -> Result { + let tag_value : u16; Ok(WaveFmt { - tag: self.read_u16::()?, + tag: { + tag_value = self.read_u16::()?; + tag_value + }, channel_count: self.read_u16::()?, sample_rate: self.read_u32::()?, bytes_per_second: self.read_u32::()?, block_alignment: self.read_u16::()?, bits_per_sample: self.read_u16::()?, - extended_format: None + extended_format: { + if tag_value == 0xFFFE { + let cb_size = self.read_u16::()?; + assert!(cb_size >= 22, "Format extension is not correct size"); + Some(WaveFmtExtended { + valid_bits_per_sample: self.read_u16::()?, + channel_mask: self.read_u32::()?, + type_guid: { + let mut buf : [u8; 16] = [0; 16]; + self.read_exact(&mut buf)?; + Uuid::from_slice(&buf)? + } + }) + } else { + None + } + } }) } @@ -143,11 +164,36 @@ impl ReadBWaveChunks for T where T: Read { if version > 1 { Some(val) } else { None } }, coding_history: { - for _ in 0..=180 { self.read_u8()?; } + for _ in 0..180 { self.read_u8()?; } let mut buf = vec![]; self.read_to_end(&mut buf)?; ASCII.decode(&buf, DecoderTrap::Ignore).expect("Error decoding text") } }) } +} + +#[test] +fn test_read_51_wav() { + use super::fmt::ChannelMask; + use super::common_format::CommonFormat; + + let path = "tests/media/pt_24bit_51.wav"; + + let mut w = super::wavereader::WaveReader::open(path).unwrap(); + let format = w.format().unwrap(); + assert_eq!(format.tag, 0xFFFE); + assert_eq!(format.channel_count, 6); + assert_eq!(format.sample_rate, 48000); + let extended = format.extended_format.unwrap(); + + assert_eq!(extended.valid_bits_per_sample, 24); + + let channels = ChannelMask::channels(extended.channel_mask, format.channel_count); + + assert_eq!(channels, [ChannelMask::FrontLeft, ChannelMask::FrontRight, + ChannelMask::FrontCenter, ChannelMask::LowFrequency, + ChannelMask::BackLeft, ChannelMask::BackRight]); + + assert_eq!(format.common_format(), CommonFormat::IntegerPCM); } \ No newline at end of file diff --git a/src/common_format.rs b/src/common_format.rs new file mode 100644 index 0000000..d8e5b68 --- /dev/null +++ b/src/common_format.rs @@ -0,0 +1,86 @@ +use uuid::Uuid; + +/** + * References: + * - http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/multichaudP.pdf +*/ + +// http://dream.cs.bath.ac.uk/researchdev/wave-ex/bformat.html + +const BASIC_PCM: u16 = 0x0001; +const BASIC_FLOAT: u16 = 0x0003; +const BASIC_MPEG: u16 = 0x0050; +const BASIC_EXTENDED: u16 = 0xFFFE; + +/* RC 2361 §4: + + WAVE Format IDs are converted to GUIDs by inserting the hexadecimal + value of the WAVE Format ID into the XXXXXXXX part of the following + template: {XXXXXXXX-0000-0010-8000-00AA00389B71}. For example, a WAVE + Format ID of 123 has the GUID value of {00000123-0000-0010-8000- + 00AA00389B71}. + +*/ + +const UUID_PCM: Uuid = Uuid::from_bytes([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]); + +const UUID_FLOAT: Uuid = Uuid::from_bytes([0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]); + +const UUID_MPEG: Uuid = Uuid::from_bytes([0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71]); + +const UUID_BFORMAT_PCM: Uuid = Uuid::from_bytes([0x01, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11, + 0x86, 0x44, 0xc8, 0xc1, 0xca, 0x00, 0x00, 0x00]); + +const UUID_BFORMAT_FLOAT: Uuid = Uuid::from_bytes([0x03, 0x00, 0x00, 0x00, 0x21, 0x07, 0xd3, 0x11, + 0x86, 0x44, 0xc8, 0xc1, 0xca, 0x00, 0x00, 0x00]); + + +fn uuid_from_basic_tag(tag: u16) -> Uuid { + let tail : [u8; 6] = [0x00,0xaa,0x00,0x38,0x9b,0x71]; + Uuid::from_fields_le(tag as u32, 0x0000, 0x0010, &tail).unwrap() +} + +/// Sample format of the Wave file. +/// +/// +#[derive(Debug, Copy, Clone, PartialEq)] +pub enum CommonFormat { + IntegerPCM, + IeeeFloatPCM, + Mpeg, + AmbisonicBFormatIntegerPCM, + AmbisonicBFormatIeeeFloatPCM, + UnknownBasic(u16), + UnknownExtended(Uuid), +} + +impl CommonFormat { + pub fn make(basic: u16, uuid: Option) -> Self { + match (basic, uuid) { + (BASIC_PCM, _) => Self::IntegerPCM, + (BASIC_FLOAT, _) => Self::IeeeFloatPCM, + (BASIC_MPEG, _) => Self::Mpeg, + (BASIC_EXTENDED, Some(UUID_PCM)) => Self::IntegerPCM, + (BASIC_EXTENDED, Some(UUID_FLOAT))=> Self::IeeeFloatPCM, + (BASIC_EXTENDED, Some(UUID_BFORMAT_PCM)) => Self::AmbisonicBFormatIntegerPCM, + (BASIC_EXTENDED, Some(UUID_BFORMAT_FLOAT)) => Self::AmbisonicBFormatIeeeFloatPCM, + (BASIC_EXTENDED, Some(x)) => CommonFormat::UnknownExtended(x), + (x, _) => CommonFormat::UnknownBasic(x) + } + } + + pub fn take(self) -> (u16, Uuid) { + match self { + Self::IntegerPCM => (BASIC_PCM, UUID_PCM), + Self::IeeeFloatPCM => (BASIC_FLOAT, UUID_FLOAT), + Self::Mpeg => (BASIC_MPEG, UUID_MPEG), + Self::AmbisonicBFormatIntegerPCM => (BASIC_EXTENDED, UUID_BFORMAT_PCM), + Self::AmbisonicBFormatIeeeFloatPCM => (BASIC_EXTENDED, UUID_BFORMAT_FLOAT), + Self::UnknownBasic(x) => ( x, uuid_from_basic_tag(x) ), + Self::UnknownExtended(x) => ( BASIC_EXTENDED, x) + } + } +} \ No newline at end of file diff --git a/src/errors.rs b/src/errors.rs index 84b8794..291812e 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -1,6 +1,8 @@ use std::io; use super::fourcc::FourCC; +use uuid; + /// Errors returned by methods in this crate. #[derive(Debug)] pub enum Error { @@ -8,6 +10,9 @@ pub enum Error { /// An `io::Error` occurred IOError(io::Error), + /// An error occured reading a tag UUID + UuidError(uuid::Error), + /// The file does not begin with a recognized WAVE header HeaderNotRecognized, @@ -34,7 +39,8 @@ pub enum Error { InsufficientDS64Reservation {expected: u64, actual: u64}, /// The file is not optimized for writing new data - DataChunkNotPreparedForAppend + DataChunkNotPreparedForAppend, + } @@ -42,4 +48,10 @@ impl From for Error { fn from(error: io::Error) -> Error { Error::IOError(error) } +} + +impl From for Error { + fn from(error: uuid::Error) -> Error { + Error::UuidError(error) + } } \ No newline at end of file diff --git a/src/fmt.rs b/src/fmt.rs index 5aaeeea..8ffbd58 100644 --- a/src/fmt.rs +++ b/src/fmt.rs @@ -1,44 +1,16 @@ - -/** - * References: - * - http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/Docs/multichaudP.pdf -*/ -#[derive(PartialEq)] -enum FormatTags { - Integer = 0x0001, - Float = 0x0003, - Extensible = 0xFFFE -} +use std::convert::TryFrom; +use uuid::Uuid; +use super::errors::Error; +use super::common_format::CommonFormat; -const PCM_SUBTYPE_UUID: [u8; 16] = [0x00, 0x00, 0x00, 0x01,0x00, 0x00, 0x00, 0x10, 0x80, 0x00, 0x00, 0xaa,0x00, 0x38, 0x9b, 0x71]; - -const FLOAT_SUBTYPE_UUID: [u8; 16] = [0x00, 0x00, 0x00, 0x03,0x00, 0x00, 0x00, 0x10, 0x80, 0x00, 0x00, 0xaa,0x00, 0x38, 0x9b, 0x71]; - -/* - -http://dream.cs.bath.ac.uk/researchdev/wave-ex/bformat.html - -Integer format: -SUBTYPE_AMBISONIC_B_FORMAT_PCM - {00000001-0721-11d3-8644-C8C1CA000000} - -Floating-point format: - -SUBTYPE_AMBISONIC_B_FORMAT_IEEE_FLOAT -{00000003-0721-11d3-8644-C8C1CA000000} - -In the case of ambisonics, I'm guessing we'd ignore the channel map and implied -channels W, X, Y, Z -*/ - /// ADM Audio ID record /// /// This structure relates a channel in the wave file to either a common ADM /// channel definition or further definition in the WAV file's ADM metadata /// chunk. /// -/// An individial channel in a WAV file can have multiple Audio IDs in an ADM +/// An individual channel in a WAV file can have multiple Audio IDs in an ADM /// AudioProgramme. /// /// See BS.2088-1 § 8, also BS.2094, also blahblahblah... @@ -57,7 +29,7 @@ pub struct ChannelDescriptor { /// /// This is either implied (in the case of mono or stereo wave files) or /// explicitly given in `WaveFormatExtentended` for files with more tracks. - speaker: WaveFmtExtendedChannelMask, + speaker: ChannelMask, /// ADM audioTrackUIDs adm_track_audio_ids: Vec, @@ -71,8 +43,8 @@ https://docs.microsoft.com/en-us/windows-hardware/drivers/audio/subformat-guids- These are from http://dream.cs.bath.ac.uk/researchdev/wave-ex/mulchaud.rtf */ -#[derive(Debug)] -pub enum WaveFmtExtendedChannelMask { +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum ChannelMask { DirectOut = 0x0, FrontLeft = 0x1, FrontRight = 0x2, @@ -94,13 +66,53 @@ pub enum WaveFmtExtendedChannelMask { TopBackRight = 0x20000, } +impl From for ChannelMask { + + fn from(value: u32) -> Self { + match value { + 0x1 => Self::FrontLeft, + 0x2 => Self::FrontRight, + 0x4 => Self::FrontCenter, + 0x8 => Self::LowFrequency, + 0x10 => Self::BackLeft, + 0x20 => Self::BackRight, + 0x40 => Self::FrontCenterLeft, + 0x80 => Self::FrontCenterRight, + 0x100 => Self::BackCenter, + 0x200 => Self::SideLeft, + 0x400 => Self::SideRight, + 0x800 => Self::TopCenter, + 0x1000 => Self::TopFrontLeft, + 0x2000 => Self::TopFrontCenter, + 0x4000 => Self::TopFrontRight, + 0x8000 => Self::TopBackLeft, + 0x10000 => Self::TopBackCenter, + 0x20000 => Self::TopBackRight, + _ => Self::DirectOut + } + } +} + +impl ChannelMask { + pub fn channels(input_mask : u32, channel_count: u16) -> Vec { + let reserved_mask = 0xfff2_0000_u32; + if (input_mask & reserved_mask) > 0 { + vec![ ChannelMask::DirectOut ; channel_count as usize ] + } else { + (0..18).map(|i| 1 << i ) + .filter(|mask| mask & input_mask > 0) + .map(|mask| Into::::into(mask)) + .collect() + } + } +} /** * Extended Wave Format * * https://docs.microsoft.com/en-us/windows/win32/api/mmreg/ns-mmreg-waveformatextensible */ -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] pub struct WaveFmtExtended { /// Valid bits per sample @@ -109,12 +121,12 @@ pub struct WaveFmtExtended { /// Channel mask /// /// Identifies the speaker assignment for each channel in the file - pub channel_mask : WaveFmtExtendedChannelMask, + pub channel_mask : u32, /// Codec GUID /// /// Identifies the codec of the audio stream - pub type_guid : [u8; 16], + pub type_guid : Uuid, } /** @@ -125,7 +137,7 @@ pub struct WaveFmtExtended { * rate, sample binary format, channel count, etc. * */ -#[derive(Debug)] +#[derive(Debug, Copy, Clone)] pub struct WaveFmt { /// A tag identifying the codec in use. @@ -168,10 +180,10 @@ impl WaveFmt { let container_bits_per_sample = bits_per_sample + (bits_per_sample % 8); let container_bytes_per_sample= container_bits_per_sample / 8; - let tag :u16 = match channel_count { - 0 => panic!("Error"), - 1..=2 => FormatTags::Integer as u16, - _ => FormatTags::Extensible as u16, + let tag : u16 = match channel_count { + 1..=2 => 0x01, + x if x > 2 => 0xFFFE, + x => panic!("Invalid channel count {}", x) }; WaveFmt { @@ -184,5 +196,47 @@ impl WaveFmt { extended_format: None } } + + pub fn common_format(&self) -> CommonFormat { + CommonFormat::make( self.tag, self.extended_format.map(|ext| ext.type_guid)) + } + + pub fn channels(&self) -> Vec { + match self.channel_count { + 1 => vec![ + ChannelDescriptor { + index: 0, + speaker: ChannelMask::FrontCenter, + adm_track_audio_ids: vec![] + } + ], + 2 => vec![ + ChannelDescriptor { + index: 0, + speaker: ChannelMask::FrontLeft, + adm_track_audio_ids: vec![] + }, + ChannelDescriptor { + index: 1, + speaker: ChannelMask::FrontRight, + adm_track_audio_ids: vec![] + } + ], + x if x > 2 => { + let channel_mask = self.extended_format.map(|x| x.channel_mask).unwrap_or(0); + let channels = ChannelMask::channels(channel_mask, self.channel_count); + let channels_expanded = channels.iter().chain(std::iter::repeat(&ChannelMask::DirectOut)); + + (0..self.channel_count) + .zip(channels_expanded) + .map(|(n,chan)| ChannelDescriptor { + index: n, + speaker: *chan, + adm_track_audio_ids: vec![] + }).collect() + }, + x => panic!("Channel count ({}) was illegal!", x), + } + } } diff --git a/src/lib.rs b/src/lib.rs index 08d7fe3..2514914 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -100,10 +100,14 @@ Things that are _not_ necessarily in the scope of this package: extern crate encoding; extern crate byteorder; +extern crate uuid; -mod parser; mod fourcc; mod errors; +mod common_format; + +mod parser; + mod raw_chunk_reader; mod audio_frame_reader; @@ -114,8 +118,9 @@ mod fmt; mod wavereader; mod wavewriter; +pub use errors::Error; pub use wavereader::{WaveReader}; pub use bext::Bext; -pub use fmt::{WaveFmt, WaveFmtExtended}; -pub use errors::Error; +pub use fmt::{WaveFmt, WaveFmtExtended, ChannelDescriptor}; +pub use common_format::CommonFormat; pub use audio_frame_reader::AudioFrameReader; \ No newline at end of file diff --git a/src/wavereader.rs b/src/wavereader.rs index 2e24496..49e77e0 100644 --- a/src/wavereader.rs +++ b/src/wavereader.rs @@ -9,8 +9,8 @@ use super::fmt::WaveFmt; use super::bext::Bext; use super::audio_frame_reader::AudioFrameReader; use super::chunks::ReadBWaveChunks; -//use super::validation; -//use std::io::SeekFrom::{Start}; + + use std::io::{Read, Seek}; diff --git a/tests/integration_test.rs b/tests/integration_test.rs index 7386f82..1d8d43e 100644 --- a/tests/integration_test.rs +++ b/tests/integration_test.rs @@ -27,7 +27,7 @@ fn test_format_silence() -> Result<(),Error> { assert_eq!(format.sample_rate, 44100); assert_eq!(format.channel_count, 1); - assert_eq!(format.tag, 1); + assert_eq!(format.tag as u16, 1); Ok( () ) }