|
|
@ -22,409 +22,144 @@ use crate::util::content_type::ContentType; |
|
|
|
const ID3V2_IDENTIFIER: [u8; 3] = [0x49, 0x44, 0x33]; |
|
|
|
const ID3V2_IDENTIFIER: [u8; 3] = [0x49, 0x44, 0x33]; |
|
|
|
const ID3V2_HEADER_LENGTH: usize = 10; |
|
|
|
const ID3V2_HEADER_LENGTH: usize = 10; |
|
|
|
const MP3_HEADER_LENGTH: usize = 4; |
|
|
|
const MP3_HEADER_LENGTH: usize = 4; |
|
|
|
const MP3_HEADER_SYNC_WORD_MASK: u32 = 0xFFE00000; // 11111111111000000000000000000000
|
|
|
|
|
|
|
|
const MP3_HEADER_VERSION_MASK: u32 = 0x00180000; // 00000000000110000000000000000000
|
|
|
|
|
|
|
|
const MP3_HEADER_LAYER_MASK: u32 = 0x00060000; // 00000000000001100000000000000000
|
|
|
|
|
|
|
|
const MP3_HEADER_BITRATE_MASK: u32 = 0x0000F000; // 00000000000000001111000000000000
|
|
|
|
|
|
|
|
const MP3_HEADER_SAMPLING_RATE_MASK: u32 = 0x00000C00; // 00000000000000000000110000000000
|
|
|
|
|
|
|
|
const MP3_HEADER_PADDING_MASK: u32 = 0x00000200; // 00000000000000000000001000000000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// const MP3_HEADER_SYNC_WORD_MASK: u32 = 0xFFF00000; // 11111111111100000000000000000000
|
|
|
|
// bitrate table for mpeg Version+Layer
|
|
|
|
// const MP3_HEADER_VERSION_MASK: u32 = 0xC0000; // 00000000000011000000000000000000
|
|
|
|
const MP3_BITRATE_TABLE: [[[u32; 15]; 3]; 2] = [ |
|
|
|
// const MP3_HEADER_LAYER_MASK: u32 = 0x30000; // 00000000000000110000000000000000
|
|
|
|
// mpegv1
|
|
|
|
// const MP3_HEADER_BITRATE_MASK: u32 = 0x7800; // 00000000000000000111100000000000
|
|
|
|
[ |
|
|
|
// const MP3_HEADER_SAMPLING_RATE_MASK: u32 = 0x600; // 00000000000000000000011000000000
|
|
|
|
// layer I
|
|
|
|
// const MP3_HEADER_PADDING_MASK: u32 = 0x100; // 00000000000000000000000100000000
|
|
|
|
[ |
|
|
|
|
|
|
|
0, 32000, 64000, 96000, 128000, 160000, |
|
|
|
#[derive(Debug)] |
|
|
|
192000, 224000, 256000, 288000, 320000, |
|
|
|
enum AudioVersion { |
|
|
|
352000, 384000, 416000, 448000, |
|
|
|
MpegV1, |
|
|
|
], |
|
|
|
MpegV2, |
|
|
|
// layer II
|
|
|
|
MpegV25, |
|
|
|
[ |
|
|
|
} |
|
|
|
0, 32000, 48000, 56000, 64000, 80000, |
|
|
|
|
|
|
|
96000, 112000, 128000, 160000, 192000, |
|
|
|
#[derive(Debug)] |
|
|
|
224000, 256000, 320000, 384000, |
|
|
|
enum LayerIndex { |
|
|
|
], |
|
|
|
LayerI, |
|
|
|
// layer III
|
|
|
|
LayerII, |
|
|
|
[ |
|
|
|
LayerIII, |
|
|
|
0, 32000, 40000, 48000, 56000, 64000, |
|
|
|
} |
|
|
|
80000, 96000, 112000, 128000, 160000, |
|
|
|
|
|
|
|
192000, 224000, 256000, 320000, |
|
|
|
fn get_bitrate(header: u32, audio_version: &AudioVersion, layer: &LayerIndex) -> Option<u16> { |
|
|
|
], |
|
|
|
match header & MP3_HEADER_BITRATE_MASK { |
|
|
|
], |
|
|
|
// 0001
|
|
|
|
//mpegv2
|
|
|
|
0x1000 => { |
|
|
|
[ |
|
|
|
match audio_version { |
|
|
|
// layer I
|
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
[ |
|
|
|
match layer { |
|
|
|
0, 32_000, 48_000, 56_000, 64_000, 80_000, |
|
|
|
LayerIndex::LayerI | LayerIndex::LayerII | LayerIndex::LayerIII => return Some(32), |
|
|
|
96_000, 112_000, 128_000, 144_000, 160_000, |
|
|
|
} |
|
|
|
176_000, 192_000, 224_000, 256_000, |
|
|
|
} |
|
|
|
], |
|
|
|
|
|
|
|
// layer III
|
|
|
|
AudioVersion::MpegV2 | AudioVersion::MpegV25 => { |
|
|
|
[ |
|
|
|
match layer { |
|
|
|
0, 8_000, 16_000, 24_000, 32_000, 40_000, 48_000, |
|
|
|
LayerIndex::LayerI => return Some(32), |
|
|
|
56_000, 64_000, 80_000, 96_000, 112_000, 128_000, |
|
|
|
LayerIndex::LayerII | LayerIndex::LayerIII => return Some(8), |
|
|
|
144_000, 160_000, |
|
|
|
} |
|
|
|
], |
|
|
|
} |
|
|
|
// layer III
|
|
|
|
} |
|
|
|
[ |
|
|
|
} |
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
|
|
|
|
|
|
|
], |
|
|
|
|
|
|
|
], |
|
|
|
// 0010
|
|
|
|
]; |
|
|
|
0x2000 => { |
|
|
|
|
|
|
|
match audio_version { |
|
|
|
enum Layer { |
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
I, |
|
|
|
match layer { |
|
|
|
II, |
|
|
|
LayerIndex::LayerI => return Some(64), |
|
|
|
III, |
|
|
|
LayerIndex::LayerII => return Some(48), |
|
|
|
} |
|
|
|
LayerIndex::LayerIII => return Some(40), |
|
|
|
|
|
|
|
} |
|
|
|
#[derive(Debug, PartialEq, Eq)] |
|
|
|
} |
|
|
|
enum MpegVersion { |
|
|
|
|
|
|
|
V1, |
|
|
|
AudioVersion::MpegV2 | AudioVersion::MpegV25 => { |
|
|
|
V2, |
|
|
|
match layer { |
|
|
|
V2_5, |
|
|
|
LayerIndex::LayerI => return Some(48), |
|
|
|
} |
|
|
|
LayerIndex::LayerII | LayerIndex::LayerIII => return Some(16), |
|
|
|
|
|
|
|
} |
|
|
|
struct MP3Header { |
|
|
|
} |
|
|
|
bitrate: u32, |
|
|
|
} |
|
|
|
layer: Layer, |
|
|
|
} |
|
|
|
version: MpegVersion, |
|
|
|
|
|
|
|
sampling_rate: u32, |
|
|
|
// 0011
|
|
|
|
padding: bool, |
|
|
|
0x3000 => { |
|
|
|
} |
|
|
|
match audio_version { |
|
|
|
|
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
impl MP3Header { |
|
|
|
match layer { |
|
|
|
fn from_bytes(header_bytes: &[u8; MP3_HEADER_LENGTH]) -> Result<MP3Header, &'static str> { |
|
|
|
LayerIndex::LayerI => return Some(96), |
|
|
|
let header: u32 = u32::from_be_bytes(*header_bytes); |
|
|
|
LayerIndex::LayerII => return Some(56), |
|
|
|
// check sync
|
|
|
|
LayerIndex::LayerIII => return Some(48), |
|
|
|
if header & 0xFFE00000 != 0xFFE00000 { |
|
|
|
} |
|
|
|
return Err("does not contain sync"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
AudioVersion::MpegV2 | AudioVersion::MpegV25 => { |
|
|
|
// get version
|
|
|
|
match layer { |
|
|
|
let version: MpegVersion; |
|
|
|
LayerIndex::LayerI => return Some(56), |
|
|
|
match (header & 0x180000) >> 19 { |
|
|
|
LayerIndex::LayerII | LayerIndex::LayerIII => return Some(24), |
|
|
|
0b00 => version = MpegVersion::V2_5, |
|
|
|
} |
|
|
|
0b10 => version = MpegVersion::V2, |
|
|
|
} |
|
|
|
0b11 => version = MpegVersion::V1, |
|
|
|
} |
|
|
|
_ => return Err("invalid mpeg version"), |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 0100
|
|
|
|
|
|
|
|
0x4000 => { |
|
|
|
|
|
|
|
match audio_version { |
|
|
|
|
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(128), |
|
|
|
|
|
|
|
LayerIndex::LayerII => return Some(64), |
|
|
|
|
|
|
|
LayerIndex::LayerIII => return Some(56), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AudioVersion::MpegV2 | AudioVersion::MpegV25 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(64), |
|
|
|
|
|
|
|
LayerIndex::LayerII | LayerIndex::LayerIII => return Some(32), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 0101
|
|
|
|
|
|
|
|
0x5000 => { |
|
|
|
|
|
|
|
match audio_version { |
|
|
|
|
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(160), |
|
|
|
|
|
|
|
LayerIndex::LayerII => return Some(80), |
|
|
|
|
|
|
|
LayerIndex::LayerIII => return Some(64), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AudioVersion::MpegV2 | AudioVersion::MpegV25 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(80), |
|
|
|
|
|
|
|
LayerIndex::LayerII | LayerIndex::LayerIII => return Some(40), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 0110
|
|
|
|
|
|
|
|
0x6000 => { |
|
|
|
|
|
|
|
match audio_version { |
|
|
|
|
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(192), |
|
|
|
|
|
|
|
LayerIndex::LayerII => return Some(96), |
|
|
|
|
|
|
|
LayerIndex::LayerIII => return Some(80), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
AudioVersion::MpegV2 | AudioVersion::MpegV25 => { |
|
|
|
// get layer
|
|
|
|
match layer { |
|
|
|
let layer: Layer; |
|
|
|
LayerIndex::LayerI => return Some(96), |
|
|
|
match (header & 0x60000) >> 17 { |
|
|
|
LayerIndex::LayerII | LayerIndex::LayerIII => return Some(48), |
|
|
|
0b01 => layer = Layer::III, |
|
|
|
} |
|
|
|
0b10 => layer = Layer::II, |
|
|
|
} |
|
|
|
0b11 => layer = Layer::I, |
|
|
|
} |
|
|
|
_ => return Err("invalid mpeg layer"), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 0111
|
|
|
|
// calculate bitrate
|
|
|
|
0x7000 => { |
|
|
|
let bitrate: u32; |
|
|
|
match audio_version { |
|
|
|
match ((header & 0xF000) >> 12, &version, &layer) { |
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
(n, MpegVersion::V1, Layer::I) => bitrate = MP3_BITRATE_TABLE[0][0][n as usize], |
|
|
|
match layer { |
|
|
|
(n, MpegVersion::V1, Layer::II) => bitrate = MP3_BITRATE_TABLE[0][1][n as usize], |
|
|
|
LayerIndex::LayerI => return Some(224), |
|
|
|
(n, MpegVersion::V1, Layer::III) => bitrate = MP3_BITRATE_TABLE[0][2][n as usize], |
|
|
|
LayerIndex::LayerII => return Some(112), |
|
|
|
(n, MpegVersion::V2|MpegVersion::V2_5, Layer::I) => bitrate = MP3_BITRATE_TABLE[1][0][n as usize], |
|
|
|
LayerIndex::LayerIII => return Some(96), |
|
|
|
(n, MpegVersion::V2|MpegVersion::V2_5, Layer::III) => bitrate = MP3_BITRATE_TABLE[1][1][n as usize], |
|
|
|
} |
|
|
|
_ => return Err("invalid or too tricky frame header to calculate bitrate"), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
AudioVersion::MpegV2 | AudioVersion::MpegV25 => { |
|
|
|
// sample rate
|
|
|
|
match layer { |
|
|
|
let sampling_rate: u32; |
|
|
|
LayerIndex::LayerI => return Some(112), |
|
|
|
match ((header & 0xC00) >> 10, &version) { |
|
|
|
LayerIndex::LayerII | LayerIndex::LayerIII => return Some(56), |
|
|
|
(0b00, MpegVersion::V1) => sampling_rate = 44100, |
|
|
|
} |
|
|
|
(0b01, MpegVersion::V1) => sampling_rate = 48000, |
|
|
|
} |
|
|
|
(0b10, MpegVersion::V1) => sampling_rate = 32000, |
|
|
|
} |
|
|
|
(0b00, MpegVersion::V2) => sampling_rate = 22050, |
|
|
|
} |
|
|
|
(0b01, MpegVersion::V2) => sampling_rate = 24000, |
|
|
|
|
|
|
|
(0b10, MpegVersion::V2) => sampling_rate = 16000, |
|
|
|
// 1000
|
|
|
|
(0b00, MpegVersion::V2_5) => sampling_rate = 11025, |
|
|
|
0x8000 => { |
|
|
|
(0b01, MpegVersion::V2_5) => sampling_rate = 12000, |
|
|
|
match audio_version { |
|
|
|
(0b10, MpegVersion::V2_5) => sampling_rate = 8000, |
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
_ => return Err("invalid sampling rate calculation"), |
|
|
|
match layer { |
|
|
|
} |
|
|
|
LayerIndex::LayerI => return Some(256), |
|
|
|
|
|
|
|
LayerIndex::LayerII => return Some(128), |
|
|
|
let padding: bool = header & 0x200 != 0; |
|
|
|
LayerIndex::LayerIII => return Some(112), |
|
|
|
|
|
|
|
} |
|
|
|
return Ok(MP3Header{ |
|
|
|
} |
|
|
|
bitrate: bitrate, |
|
|
|
|
|
|
|
layer: layer, |
|
|
|
AudioVersion::MpegV2 | AudioVersion::MpegV25 => { |
|
|
|
version: version, |
|
|
|
match layer { |
|
|
|
sampling_rate: sampling_rate, |
|
|
|
LayerIndex::LayerI => return Some(128), |
|
|
|
padding: padding, |
|
|
|
LayerIndex::LayerII | LayerIndex::LayerIII => return Some(64), |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
fn frame_size(&self) -> usize { |
|
|
|
} |
|
|
|
return { |
|
|
|
|
|
|
|
(if self.version == MpegVersion::V1 {144} else {72} * |
|
|
|
// 1001
|
|
|
|
self.bitrate / |
|
|
|
0x9000 => { |
|
|
|
self.sampling_rate) as usize + if self.padding {1} else {0}
|
|
|
|
match audio_version { |
|
|
|
}; |
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(288), |
|
|
|
|
|
|
|
LayerIndex::LayerII => return Some(160), |
|
|
|
|
|
|
|
LayerIndex::LayerIII => return Some(128), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AudioVersion::MpegV2 | AudioVersion::MpegV25 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(144), |
|
|
|
|
|
|
|
LayerIndex::LayerII | LayerIndex::LayerIII => return Some(80), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 1010
|
|
|
|
|
|
|
|
0xA000 => { |
|
|
|
|
|
|
|
match audio_version { |
|
|
|
|
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(320), |
|
|
|
|
|
|
|
LayerIndex::LayerII => return Some(192), |
|
|
|
|
|
|
|
LayerIndex::LayerIII => return Some(160), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AudioVersion::MpegV2 | AudioVersion::MpegV25 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(160), |
|
|
|
|
|
|
|
LayerIndex::LayerII | LayerIndex::LayerIII => return Some(96), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 1011
|
|
|
|
|
|
|
|
0xB000 => { |
|
|
|
|
|
|
|
match audio_version { |
|
|
|
|
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(352), |
|
|
|
|
|
|
|
LayerIndex::LayerII => return Some(224), |
|
|
|
|
|
|
|
LayerIndex::LayerIII => return Some(192), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AudioVersion::MpegV2 | AudioVersion::MpegV25 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(176), |
|
|
|
|
|
|
|
LayerIndex::LayerII | LayerIndex::LayerIII => return Some(112), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 1100
|
|
|
|
|
|
|
|
0xC000 => { |
|
|
|
|
|
|
|
match audio_version { |
|
|
|
|
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(384), |
|
|
|
|
|
|
|
LayerIndex::LayerII => return Some(256), |
|
|
|
|
|
|
|
LayerIndex::LayerIII => return Some(224), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AudioVersion::MpegV2 | AudioVersion::MpegV25 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(192), |
|
|
|
|
|
|
|
LayerIndex::LayerII | LayerIndex::LayerIII => return Some(128), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 1101
|
|
|
|
|
|
|
|
0xD000 => { |
|
|
|
|
|
|
|
match audio_version { |
|
|
|
|
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(416), |
|
|
|
|
|
|
|
LayerIndex::LayerII => return Some(320), |
|
|
|
|
|
|
|
LayerIndex::LayerIII => return Some(256), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AudioVersion::MpegV2 | AudioVersion::MpegV25 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(224), |
|
|
|
|
|
|
|
LayerIndex::LayerII | LayerIndex::LayerIII => return Some(144), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 1110
|
|
|
|
|
|
|
|
0xE000 => { |
|
|
|
|
|
|
|
match audio_version { |
|
|
|
|
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(448), |
|
|
|
|
|
|
|
LayerIndex::LayerII => return Some(384), |
|
|
|
|
|
|
|
LayerIndex::LayerIII => return Some(320), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AudioVersion::MpegV2 | AudioVersion::MpegV25 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return Some(256), |
|
|
|
|
|
|
|
LayerIndex::LayerII | LayerIndex::LayerIII => return Some(160), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_ => { |
|
|
|
|
|
|
|
// invalid bitrate index
|
|
|
|
|
|
|
|
return None; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn get_samples_per_frame(audio_version: &AudioVersion, layer: &LayerIndex) -> u16 { |
|
|
|
|
|
|
|
match audio_version { |
|
|
|
|
|
|
|
AudioVersion::MpegV1 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return 384, |
|
|
|
|
|
|
|
LayerIndex::LayerII => return 1152, |
|
|
|
|
|
|
|
LayerIndex::LayerIII => return 1152, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AudioVersion::MpegV2 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return 384, |
|
|
|
|
|
|
|
LayerIndex::LayerII => return 1152, |
|
|
|
|
|
|
|
LayerIndex::LayerIII => return 576, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
AudioVersion::MpegV25 => { |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => return 384, |
|
|
|
|
|
|
|
LayerIndex::LayerII => return 1152, |
|
|
|
|
|
|
|
LayerIndex::LayerIII => return 576, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn get_sampling_rate(header: u32, audio_version: &AudioVersion) -> Option<u16> { |
|
|
|
|
|
|
|
match header & MP3_HEADER_SAMPLING_RATE_MASK { |
|
|
|
|
|
|
|
// 00
|
|
|
|
|
|
|
|
0x0 => { |
|
|
|
|
|
|
|
match audio_version { |
|
|
|
|
|
|
|
AudioVersion::MpegV1 => return Some(44100), |
|
|
|
|
|
|
|
AudioVersion::MpegV2 => return Some(22050), |
|
|
|
|
|
|
|
AudioVersion::MpegV25 => return Some(11025), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 01
|
|
|
|
|
|
|
|
0x400 => { |
|
|
|
|
|
|
|
match audio_version { |
|
|
|
|
|
|
|
AudioVersion::MpegV1 => return Some(48000), |
|
|
|
|
|
|
|
AudioVersion::MpegV2 => return Some(24000), |
|
|
|
|
|
|
|
AudioVersion::MpegV25 => return Some(12000), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 10
|
|
|
|
|
|
|
|
0x800 => { |
|
|
|
|
|
|
|
match audio_version { |
|
|
|
|
|
|
|
AudioVersion::MpegV1 => return Some(32000), |
|
|
|
|
|
|
|
AudioVersion::MpegV2 => return Some(16000), |
|
|
|
|
|
|
|
AudioVersion::MpegV25 => return Some(8000), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// invalid value
|
|
|
|
|
|
|
|
_ => return None
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn get_audio_version(header: u32) -> Option<AudioVersion> { |
|
|
|
|
|
|
|
match header & MP3_HEADER_VERSION_MASK { |
|
|
|
|
|
|
|
// 00
|
|
|
|
|
|
|
|
0x0 => return Some(AudioVersion::MpegV25), |
|
|
|
|
|
|
|
// 10
|
|
|
|
|
|
|
|
0x100000 => return Some(AudioVersion::MpegV2), |
|
|
|
|
|
|
|
// 11
|
|
|
|
|
|
|
|
0x180000 => return Some(AudioVersion::MpegV1), |
|
|
|
|
|
|
|
// invalid version id
|
|
|
|
|
|
|
|
_ => return None, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn get_layer(header: u32) -> Option<LayerIndex> { |
|
|
|
|
|
|
|
match header & MP3_HEADER_LAYER_MASK { |
|
|
|
|
|
|
|
// 01
|
|
|
|
|
|
|
|
0x20000 => return Some(LayerIndex::LayerIII), |
|
|
|
|
|
|
|
// 10
|
|
|
|
|
|
|
|
0x40000 => return Some(LayerIndex::LayerII), |
|
|
|
|
|
|
|
// 11
|
|
|
|
|
|
|
|
0x60000 => return Some(LayerIndex::LayerI), |
|
|
|
|
|
|
|
// invalid layer index
|
|
|
|
|
|
|
|
_ => return None, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn rip_mp3(data: &[u8], start_index: usize) -> Option<Position> { |
|
|
|
pub fn rip_mp3(data: &[u8], start_index: usize) -> Option<Position> { |
|
|
|
if data.len() < ID3V2_HEADER_LENGTH + MP3_HEADER_LENGTH + 1 || |
|
|
|
if data.len() < ID3V2_HEADER_LENGTH + MP3_HEADER_LENGTH + 1 || |
|
|
@ -438,13 +173,10 @@ pub fn rip_mp3(data: &[u8], start_index: usize) -> Option<Position> { |
|
|
|
content_type: ContentType::MP3, |
|
|
|
content_type: ContentType::MP3, |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
let mut cursor_index: usize; |
|
|
|
|
|
|
|
for i in start_index..data.len() { |
|
|
|
for i in start_index..data.len() { |
|
|
|
if i < ID3V2_HEADER_LENGTH && position.start == usize::MAX { |
|
|
|
if i < ID3V2_HEADER_LENGTH && position.start == usize::MAX { |
|
|
|
if data[i..i + ID3V2_IDENTIFIER.len()] == ID3V2_IDENTIFIER { |
|
|
|
if data[i..i + ID3V2_IDENTIFIER.len()] == ID3V2_IDENTIFIER { |
|
|
|
// found ID3v2 tag (the beginning of the MP3 file)
|
|
|
|
// found ID3v2 tag (the beginning of the MP3 file)
|
|
|
|
println!("id3 at {}", i); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get tag length
|
|
|
|
// get tag length
|
|
|
|
let mut tag_length_bytes: [u8; 4] = [0; 4]; |
|
|
|
let mut tag_length_bytes: [u8; 4] = [0; 4]; |
|
|
|
for j in 0..4 { |
|
|
|
for j in 0..4 { |
|
|
@ -458,7 +190,6 @@ pub fn rip_mp3(data: &[u8], start_index: usize) -> Option<Position> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let id3v2_end_index: usize = i + ID3V2_HEADER_LENGTH + tag_length as usize; |
|
|
|
let id3v2_end_index: usize = i + ID3V2_HEADER_LENGTH + tag_length as usize; |
|
|
|
println!("id3v2 end index is {}; the data size is {}", id3v2_end_index, data.len()); |
|
|
|
|
|
|
|
if id3v2_end_index + MP3_HEADER_LENGTH > data.len() - 1 { |
|
|
|
if id3v2_end_index + MP3_HEADER_LENGTH > data.len() - 1 { |
|
|
|
println!("not enough data length"); |
|
|
|
println!("not enough data length"); |
|
|
|
// strange: there's a valid ID3 tag but not enough data to store any music
|
|
|
|
// strange: there's a valid ID3 tag but not enough data to store any music
|
|
|
@ -466,114 +197,33 @@ pub fn rip_mp3(data: &[u8], start_index: usize) -> Option<Position> { |
|
|
|
} |
|
|
|
} |
|
|
|
position.start = i; |
|
|
|
position.start = i; |
|
|
|
|
|
|
|
|
|
|
|
cursor_index = id3v2_end_index; |
|
|
|
position.end = id3v2_end_index; |
|
|
|
loop { |
|
|
|
|
|
|
|
if cursor_index >= data.len() - 1 { |
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// whole header
|
|
|
|
|
|
|
|
let mut mp3_header_bytes: [u8 ; MP3_HEADER_LENGTH] = [0; MP3_HEADER_LENGTH]; |
|
|
|
|
|
|
|
for j in 0..MP3_HEADER_LENGTH { |
|
|
|
|
|
|
|
mp3_header_bytes[j] = data[cursor_index + j]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
let mp3_header: u32 = u32::from_be_bytes(mp3_header_bytes); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// check for sync word
|
|
|
|
|
|
|
|
if !mp3_header & MP3_HEADER_SYNC_WORD_MASK == MP3_HEADER_SYNC_WORD_MASK { |
|
|
|
|
|
|
|
println!("SYNCWORD NO"); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// that's really an MP3 !
|
|
|
|
|
|
|
|
println!("mpeg header at {}", cursor_index); |
|
|
|
|
|
|
|
cursor_index += MP3_HEADER_LENGTH; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
println!("{:#032b}", mp3_header); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// retrieve audio version
|
|
|
|
|
|
|
|
let audio_version: AudioVersion; |
|
|
|
|
|
|
|
match get_audio_version(mp3_header) { |
|
|
|
|
|
|
|
Some(version) => audio_version = version, |
|
|
|
|
|
|
|
None => {println!("VERSION NO {:032x}", mp3_header & MP3_HEADER_VERSION_MASK); break}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
println!("audio version is {:?}", audio_version); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// get layer
|
|
|
|
|
|
|
|
let layer: LayerIndex; |
|
|
|
|
|
|
|
match get_layer(mp3_header) { |
|
|
|
|
|
|
|
Some(l) => layer = l, |
|
|
|
|
|
|
|
None => {println!("LAYER NO"); break}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
println!("layer is {:?}", layer); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// decode that HUGE bitrate table
|
|
|
|
|
|
|
|
let bitrate: u16; |
|
|
|
|
|
|
|
match get_bitrate(mp3_header, &audio_version, &layer) { |
|
|
|
|
|
|
|
Some(rate) => bitrate = rate, |
|
|
|
|
|
|
|
None => {println!("BITRATE NO"); break}, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
println!("bitrate is {}", bitrate); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// samples per frame
|
|
|
|
|
|
|
|
// let samples_per_frame: u16 = get_samples_per_frame(&audio_version, &layer);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// sampling rate
|
|
|
|
|
|
|
|
let sampling_rate: u16; |
|
|
|
|
|
|
|
match get_sampling_rate(mp3_header, &audio_version) { |
|
|
|
|
|
|
|
Some(rate) => sampling_rate = rate, |
|
|
|
|
|
|
|
None => {println!("SAMPLING RATE NO"); break}, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
println!("sampling rate is {}", sampling_rate); |
|
|
|
// return None immediately if id3 tag was not found
|
|
|
|
|
|
|
|
if position.start == usize::MAX { |
|
|
|
|
|
|
|
return None; |
|
|
|
// padding
|
|
|
|
|
|
|
|
let padding: u8; |
|
|
|
|
|
|
|
if mp3_header == MP3_HEADER_PADDING_MASK { |
|
|
|
|
|
|
|
padding = 1; |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
padding = 0;
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
println!("padding is {}", padding); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let slot_size: u32; |
|
|
|
|
|
|
|
match layer { |
|
|
|
|
|
|
|
LayerIndex::LayerI => slot_size = 4, |
|
|
|
|
|
|
|
_ => slot_size = 1, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let multiplier: u32; |
|
|
|
// try to extract mp3 frames
|
|
|
|
match layer { |
|
|
|
let mut mp3_header_bytes: [u8; MP3_HEADER_LENGTH] = [0; MP3_HEADER_LENGTH]; |
|
|
|
LayerIndex::LayerI => multiplier = 12, |
|
|
|
while position.end < data.len() - MP3_HEADER_LENGTH { |
|
|
|
_ => multiplier = 144000, |
|
|
|
for j in 0..MP3_HEADER_LENGTH { |
|
|
|
|
|
|
|
mp3_header_bytes[j] = data[position.end + j]; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
let slot_count: u32 = ((multiplier as u32 * (bitrate as u32 * 1000)) / sampling_rate as u32) + padding as u32; |
|
|
|
match MP3Header::from_bytes(&mp3_header_bytes) { |
|
|
|
|
|
|
|
Ok(header) => { |
|
|
|
// finally calculate frame size
|
|
|
|
position.end += header.frame_size(); |
|
|
|
let frame_size: u32 = slot_count * slot_size; |
|
|
|
|
|
|
|
println!("frame size is {}", frame_size); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// set cursor to the next frame
|
|
|
|
|
|
|
|
cursor_index += frame_size as usize; |
|
|
|
|
|
|
|
// extend end position
|
|
|
|
|
|
|
|
position.end = cursor_index; |
|
|
|
|
|
|
|
println!("frame end at {}", cursor_index); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
Err(_) => { |
|
|
|
if position.start != usize::MAX && position.end != usize::MAX { |
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if position.start == usize::MAX || position.end == usize::MAX || position.end <= position.start { |
|
|
|
if position.start == usize::MAX || position.end == usize::MAX || position.end <= position.start { |
|
|
|
return None; |
|
|
|
return None; |
|
|
|