| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | use ec_core::{ChannelLayout, CodecId, TimeBase}; |
| 11 | |
| 12 | |
| 13 | |
| 14 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 15 | pub enum Mapping { |
| 16 | |
| 17 | Vorbis { |
| 18 | |
| 19 | rate: u32, |
| 20 | |
| 21 | channels: u8, |
| 22 | }, |
| 23 | |
| 24 | |
| 25 | Opus { |
| 26 | |
| 27 | channels: u8, |
| 28 | |
| 29 | pre_skip: u16, |
| 30 | |
| 31 | input_rate: u32, |
| 32 | }, |
| 33 | |
| 34 | |
| 35 | Flac { |
| 36 | |
| 37 | rate: u32, |
| 38 | |
| 39 | channels: u8, |
| 40 | |
| 41 | headers: usize, |
| 42 | }, |
| 43 | } |
| 44 | |
| 45 | impl Mapping { |
| 46 | |
| 47 | |
| 48 | |
| 49 | pub fn identify(bos: &[u8]) -> Option<Mapping> { |
| 50 | if bos.len() >= 16 && bos[0] == 1 && &bos[1..7] == b"vorbis" { |
| 51 | return Some(Mapping::Vorbis { |
| 52 | rate: u32::from_le_bytes(bos[12..16].try_into().ok()?), |
| 53 | channels: bos[11], |
| 54 | }); |
| 55 | } |
| 56 | if bos.len() >= 19 && &bos[..8] == b"OpusHead" { |
| 57 | return Some(Mapping::Opus { |
| 58 | channels: bos[9], |
| 59 | pre_skip: u16::from_le_bytes(bos[10..12].try_into().ok()?), |
| 60 | input_rate: u32::from_le_bytes(bos[12..16].try_into().ok()?), |
| 61 | }); |
| 62 | } |
| 63 | if bos.len() >= 51 && bos[0] == 0x7f && &bos[1..5] == b"FLAC" && &bos[9..13] == b"fLaC" { |
| 64 | |
| 65 | |
| 66 | let si = &bos[17..51]; |
| 67 | let rate = |
| 68 | (u32::from(si[10]) << 12) | (u32::from(si[11]) << 4) | u32::from(si[12] >> 4); |
| 69 | let declared = usize::from(u16::from_be_bytes([bos[7], bos[8]])); |
| 70 | return Some(Mapping::Flac { |
| 71 | rate, |
| 72 | channels: ((si[12] >> 1) & 0x07) + 1, |
| 73 | |
| 74 | |
| 75 | headers: (declared + 1).max(2), |
| 76 | }); |
| 77 | } |
| 78 | None |
| 79 | } |
| 80 | |
| 81 | |
| 82 | pub fn codec(&self) -> CodecId { |
| 83 | match self { |
| 84 | Mapping::Vorbis { .. } => CodecId::Vorbis, |
| 85 | Mapping::Opus { .. } => CodecId::Opus, |
| 86 | Mapping::Flac { .. } => CodecId::Flac, |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | |
| 91 | pub fn time_base(&self) -> TimeBase { |
| 92 | match self { |
| 93 | |
| 94 | Mapping::Opus { .. } => TimeBase::from_rate(48_000), |
| 95 | Mapping::Vorbis { rate, .. } | Mapping::Flac { rate, .. } => { |
| 96 | TimeBase::try_new(1, i64::from(*rate)).unwrap_or(TimeBase::from_rate(48_000)) |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | |
| 102 | pub fn sample_rate(&self) -> u32 { |
| 103 | match self { |
| 104 | Mapping::Opus { .. } => 48_000, |
| 105 | Mapping::Vorbis { rate, .. } | Mapping::Flac { rate, .. } => *rate, |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | |
| 110 | |
| 111 | pub fn layout(&self) -> ChannelLayout { |
| 112 | let channels = match self { |
| 113 | Mapping::Vorbis { channels, .. } |
| 114 | | Mapping::Opus { channels, .. } |
| 115 | | Mapping::Flac { channels, .. } => *channels, |
| 116 | }; |
| 117 | ChannelLayout::from_count(usize::from(channels).max(1)) |
| 118 | } |
| 119 | |
| 120 | |
| 121 | |
| 122 | pub fn header_packets(&self) -> usize { |
| 123 | match self { |
| 124 | Mapping::Vorbis { .. } => 3, |
| 125 | Mapping::Opus { .. } => 2, |
| 126 | Mapping::Flac { headers, .. } => *headers, |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | pub fn packet_duration(&self, data: &[u8]) -> Option<i64> { |
| 141 | match self { |
| 142 | Mapping::Opus { .. } => opus_packet_samples(data), |
| 143 | Mapping::Vorbis { .. } | Mapping::Flac { .. } => None, |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | pub fn opus_packet_samples(data: &[u8]) -> Option<i64> { |
| 152 | let toc = *data.first()?; |
| 153 | let config = usize::from(toc >> 3); |
| 154 | |
| 155 | |
| 156 | let samples = match config { |
| 157 | 0..=11 => [480, 960, 1920, 2880][config % 4], |
| 158 | 12..=15 => [480, 960][config % 2], |
| 159 | _ => [120, 240, 480, 960][config % 4], |
| 160 | }; |
| 161 | let frames = match toc & 0x03 { |
| 162 | 0 => 1, |
| 163 | 1 | 2 => 2, |
| 164 | |
| 165 | _ => i64::from(*data.get(1)? & 0x3f), |
| 166 | }; |
| 167 | |
| 168 | match frames * samples { |
| 169 | n if n > 0 && n <= 5760 => Some(n), |
| 170 | _ => None, |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | #[cfg(test)] |
| 175 | mod tests { |
| 176 | use super::*; |
| 177 | |
| 178 | #[test] |
| 179 | fn identifies_the_three_mappings() { |
| 180 | let mut vorbis = vec![1u8]; |
| 181 | vorbis.extend_from_slice(b"vorbis"); |
| 182 | vorbis.extend_from_slice(&0u32.to_le_bytes()); |
| 183 | vorbis.push(2); |
| 184 | vorbis.extend_from_slice(&44_100u32.to_le_bytes()); |
| 185 | vorbis.resize(30, 0); |
| 186 | assert_eq!( |
| 187 | Mapping::identify(&vorbis), |
| 188 | Some(Mapping::Vorbis { |
| 189 | rate: 44_100, |
| 190 | channels: 2 |
| 191 | }) |
| 192 | ); |
| 193 | |
| 194 | let mut opus = Vec::from(*b"OpusHead"); |
| 195 | opus.push(1); |
| 196 | opus.push(6); |
| 197 | opus.extend_from_slice(&312u16.to_le_bytes()); |
| 198 | opus.extend_from_slice(&48_000u32.to_le_bytes()); |
| 199 | opus.resize(19, 0); |
| 200 | let mapping = Mapping::identify(&opus).unwrap(); |
| 201 | assert_eq!( |
| 202 | mapping, |
| 203 | Mapping::Opus { |
| 204 | channels: 6, |
| 205 | pre_skip: 312, |
| 206 | input_rate: 48_000 |
| 207 | } |
| 208 | ); |
| 209 | |
| 210 | assert_eq!(mapping.time_base(), TimeBase::from_rate(48_000)); |
| 211 | assert_eq!(mapping.layout(), ChannelLayout::Surround5_1); |
| 212 | assert_eq!(mapping.header_packets(), 2); |
| 213 | |
| 214 | assert_eq!(Mapping::identify(b"not a header at all"), None); |
| 215 | assert_eq!(Mapping::identify(&[]), None); |
| 216 | } |
| 217 | |
| 218 | #[test] |
| 219 | fn opus_durations_follow_the_toc() { |
| 220 | |
| 221 | assert_eq!(opus_packet_samples(&[16 << 3]), Some(120)); |
| 222 | |
| 223 | |
| 224 | assert_eq!(opus_packet_samples(&[31 << 3]), Some(960)); |
| 225 | |
| 226 | assert_eq!(opus_packet_samples(&[(31 << 3) | 1]), Some(1920)); |
| 227 | |
| 228 | |
| 229 | assert_eq!(opus_packet_samples(&[(31 << 3) | 3, 6]), Some(5760)); |
| 230 | assert_eq!(opus_packet_samples(&[(31 << 3) | 3, 7]), None); |
| 231 | assert_eq!(opus_packet_samples(&[(31 << 3) | 3, 0]), None); |
| 232 | assert_eq!(opus_packet_samples(&[(31 << 3) | 3]), None); |
| 233 | |
| 234 | assert_eq!(opus_packet_samples(&[3 << 3]), Some(2880)); |
| 235 | assert_eq!(opus_packet_samples(&[]), None); |
| 236 | } |
| 237 | } |