1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
|
use std::io::{BufRead, BufReader, Cursor, Read};
/// Destroy an MP3, ripping it's frames apart. Also removes any ID3v2 tags
/// because who needs metadata?
pub struct Breaker {
frames: Vec<Frame>,
}
impl Breaker {
pub fn new() -> Self {
Self { frames: vec![] }
}
pub fn split(&mut self, mut data: Vec<u8>) -> Result<(), std::io::Error> {
let cursor = Cursor::new(data);
let mut reader = BufReader::new(cursor);
let mut consumed = 0;
loop {
let mut three = [0x00, 0x00, 0x00];
reader.read_exact(&mut three)?;
consumed += 3;
if &three == b"ID3" {
println!("ID3v2 offset {:X}", consumed);
Self::skip_id3v2(&mut reader)?
} else if three[0] == 0xFF && three[2] & 0b1110_0000 == 0b1110_0000 {
let mut one_more = [0x00];
reader.read_exact(&mut one_more)?;
}
}
todo!()
}
/// Assumes the ident "TAG" was already consumed
fn skip_id3v2<R: BufRead>(reader: &mut R) -> Result<(), std::io::Error> {
// We don't actually want this, but want to get rid of it.
let mut version_and_flags = [0x00, 0x00, 0x00];
reader.read_exact(&mut version_and_flags)?;
println!(
"Version {} Revision {}",
version_and_flags[0], version_and_flags[1]
);
let mut syncsafe_size = [0x00, 0x00, 0x00, 0x00];
reader.read_exact(&mut syncsafe_size)?;
// Size is MSB
let mut size = syncsafe_size[3] as u32;
// Shift right eight, but back one because most significant bit is 0 due to syncsafe
size |= (syncsafe_size[2] as u32) << 7;
size |= (syncsafe_size[1] as u32) << 14;
size |= (syncsafe_size[0] as u32) << 21;
let human = if size > 1024 * 1024 {
format!("{:.2}MiB", size as f32 / (1024.0 * 1024.0))
} else if size > 1024 {
format!("{:.2}KiB", size as f32 / 1024.0)
} else {
format!("{size}B")
};
println!("ID3v2 size is {human} bytes");
// Make a vec size big. We're not here to be efficient, sorry if this dissapoint you.
let mut skip = vec![0x00; size as usize];
reader.read_exact(&mut skip)
}
}
pub struct Frame {
header: Header,
data: Vec<u8>,
}
pub struct Header {
// I only want to parse what i need, but we need this for writing out, so
raw: [u8; 4],
version: Version,
layer: Layer,
crc: bool,
}
impl Header {
pub fn from_bytes(raw: [u8; 4]) -> Result<Self, Error> {
if raw[0] != 0xFF || raw[1] & 0b1110_0000 != 0b1110_0000 {
return Err(Error::HeaderUnsync);
}
let version = Version::from(raw[1]);
let layer = Layer::from(raw[1]);
let crc = raw[1] & 1 == 0;
let bitrate = Bitrate::resolve(raw[2], version, layer);
//TODO: gen- love, you were trying to get the size of the data field. We need
//to know the sampling rate and the pad bit for that, which happen to be the
//next three bits.
todo!()
}
// Algorithm taken from:
// http://www.multiweb.cz/twoinches/mp3inside.htm
/// The length of the header and data
pub fn length(&self) -> usize {
todo!()
}
/// The length of the audio data. This is just the length - 4
pub fn data_length(&self) -> usize {
self.length() - 4
}
}
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("tried to parse header, but first 11 bits were not 1; not synced!")]
HeaderUnsync,
#[error("The version or the layer was a reserved value")]
CannotResolveBitrate,
#[error("Bitrate bits were all 1")]
BitrateBad,
}
#[derive(Copy, Clone, Debug)]
pub enum Version {
Mpeg2_5,
Reserved,
Mpeg2,
Mpeg1,
}
impl From<u8> for Version {
fn from(byte: u8) -> Self {
match byte & 0b000_11_000 {
0b000_00_000 => Version::Mpeg2_5,
0b000_01_000 => Version::Reserved,
0b000_10_000 => Version::Mpeg2,
0b000_11_000 => Version::Mpeg1,
_ => unreachable!(),
}
}
}
#[derive(Copy, Clone, Debug)]
pub enum Layer {
Reserved,
Layer3,
Layer2,
Layer1,
}
impl From<u8> for Layer {
fn from(byte: u8) -> Self {
match byte & 0b000_00_110 {
0b000_00_000 => Layer::Reserved,
0b000_00_010 => Layer::Layer3,
0b000_00_100 => Layer::Layer2,
0b000_00_110 => Layer::Layer1,
_ => unreachable!(),
}
}
}
/// What do you want from me it's hard to name a thing that's just number.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Bitrate {
RateFree,
Rate8,
Rate16,
Rate24,
Rate32,
Rate40,
Rate48,
Rate56,
Rate64,
Rate80,
Rate96,
Rate112,
Rate128,
Rate144,
Rate160,
Rate176,
Rate192,
Rate224,
Rate256,
Rate288,
Rate320,
Rate352,
Rate384,
Rate416,
Rate448,
}
impl Bitrate {
/// Takes the third byte of the header and other neccesary information
pub fn resolve(third: u8, version: Version, layer: Layer) -> Result<Self, Error> {
#[rustfmt::skip]
macro_rules! v {
(Any) => { _ };
(v1) => { Version::Mpeg1 };
(v2) => { Version::Mpeg2 | Version::Mpeg2_5 };
}
#[rustfmt::skip]
macro_rules! l {
(Any) => { _ };
(l1) => { Layer::Layer1 };
(l2) => { Layer::Layer2 };
(l3) => { Layer::Layer3 };
(l23) => { Layer::Layer2 | Layer::Layer3 };
}
#[rustfmt::skip]
macro_rules! br {
(b0000) => { Fourbit::Zero };
(b0001) => { Fourbit::One };
(b0010) => { Fourbit::Two };
(b0011) => { Fourbit::Three };
(b0100) => { Fourbit::Four };
(b0101) => { Fourbit::Five };
(b0110) => { Fourbit::Six };
(b0111) => { Fourbit::Seven };
(b1000) => { Fourbit::Eight };
(b1001) => { Fourbit::Nine };
(b1010) => { Fourbit::Ten };
(b1011) => { Fourbit::Eleven };
(b1100) => { Fourbit::Twelve };
(b1101) => { Fourbit::Thirteen };
(b1110) => { Fourbit::Fourteen };
(b1111) => { Fourbit::Fifteen };
(down1 b0010) => {br!(b0011)};
(down1 b0011) => {br!(b0100)};
(down1 b0100) => {br!(b0101)};
(down1 b0101) => {br!(b0110)};
(down1 b0110) => {br!(b0111)};
(down1 b0111) => {br!(b1000)};
(down1 b1000) => {br!(b1001)};
(down4 b0010) => {br!(b0110)};
(down4 b0011) => {br!(b0111)};
(down4 b0100) => {br!(b1000)};
(down4 b0101) => {br!(b1001)};
(down4 b0110) => {br!(b1010)};
(down4 b0111) => {br!(b1011)};
(down4 b1000) => {br!(b1100)};
(down5 b0110) => {br!(b1011)};
(down5 b0111) => {br!(b1100)};
(down5 b1000) => {br!(b1101)};
(down6 b0110) => {br!(b1100)};
(down6 b0111) => {br!(b1101)};
(down6 b1000) => {br!(b1110)};
}
macro_rules! vl1 {
($br:ident) => {
brvl!($br v1 l1)
};
}
macro_rules! v2_l23 {
($br:ident) => {
brvl!($br v2 l23)
};
}
macro_rules! brvl {
($br:ident $v:ident $l:ident) => {
(br!($br), v!($v), l!($l))
};
(down1 $br:ident $v:ident $l:ident) => {
(br!(down1 $br), v!($v), l!($l))
};
(down4 $br:ident $v:ident $l:ident) => {
(br!(down4 $br), v!($v), l!($l))
};
(down5 $br:ident $v:ident $l:ident) => {
(br!(down5 $br), v!($v), l!($l))
};
(down6 $br:ident $v:ident $l:ident) => {
(br!(down6 $br), v!($v), l!($l))
};
}
macro_rules! heartbeat_bird {
($col_v1_l2:ident) => {
brvl!($col_v1_l2 v1 l2) | brvl!($col_v1_l2 v2 l1) | brvl!(down1 $col_v1_l2 v1 l3) | brvl!(down4 $col_v1_l2 v2 l23)
};
}
// rustc was apparently unhappy about my brvl!(down4) calls. And down5, down6 :(
macro_rules! crooked_down {
($col_v1_l1:ident) => {
brvl!($col_v1_l1 v1 l1) | (br!(down4 $col_v1_l1), v!(v1), l!(l2)) | (br!(down5 $col_v1_l1), v!(v1), l!(l3)) | (br!(down6 $col_v1_l1), v!(v2), l!(l1))
};
}
let br_4bit = Fourbit::from_u8((third & 0b1111_0000) >> 4).expect("this can't happen");
let tuple = (br_4bit, version, layer);
match tuple {
// These patterns cover a very large surface area
heartbeat_bird!(b0010) => Ok(Bitrate::Rate48),
heartbeat_bird!(b0011) => Ok(Bitrate::Rate56),
heartbeat_bird!(b0100) => Ok(Bitrate::Rate64),
heartbeat_bird!(b0101) => Ok(Bitrate::Rate80),
heartbeat_bird!(b0110) => Ok(Bitrate::Rate96),
heartbeat_bird!(b0111) => Ok(Bitrate::Rate112),
heartbeat_bird!(b1000) => Ok(Bitrate::Rate128),
crooked_down!(b0110) => Ok(Bitrate::Rate192),
crooked_down!(b0111) => Ok(Bitrate::Rate224),
crooked_down!(b1000) => Ok(Bitrate::Rate256),
// Then we start at the top and work our way down,
// row by row as long as there are still values we have to match there
brvl!(b0000 Any Any) => Ok(Bitrate::RateFree),
brvl!(b0001 v1 Any) => Ok(Bitrate::Rate32),
brvl!(b0001 v2 l1) => Ok(Bitrate::Rate32),
brvl!(b0001 v2 l23) => Ok(Bitrate::Rate8),
vl1!(b0010) => Ok(Bitrate::Rate64),
brvl!(b0010 v1 l3) => Ok(Bitrate::Rate40),
v2_l23!(b0010) => Ok(Bitrate::Rate16),
vl1!(b0011) => Ok(Bitrate::Rate96),
v2_l23!(b0011) => Ok(Bitrate::Rate24),
vl1!(b0100) => Ok(Bitrate::Rate128),
v2_l23!(b0100) => Ok(Bitrate::Rate32),
vl1!(b0101) => Ok(Bitrate::Rate160),
v2_l23!(b0101) => Ok(Bitrate::Rate40),
// 0110, 0111, and 1000 are entirely covered by the patterns :D
vl1!(b1001) => Ok(Bitrate::Rate288),
brvl!(b1001 v1 l2) => Ok(Bitrate::Rate160),
brvl!(b1001 v2 l1) => Ok(Bitrate::Rate144),
brvl!(b1010 v1 l1) => Ok(Bitrate::Rate320),
brvl!(b1010 v1 l3) => Ok(Bitrate::Rate160),
brvl!(b1010 v2 l1) => Ok(Bitrate::Rate160),
vl1!(b1011) => Ok(Bitrate::Rate352),
brvl!(b1011 v2 l1) => Ok(Bitrate::Rate176),
vl1!(b1100) => Ok(Bitrate::Rate384),
vl1!(b1101) => Ok(Bitrate::Rate416),
brvl!(b1101 v1 l2) => Ok(Bitrate::Rate320),
v2_l23!(b1101) => Ok(Bitrate::Rate144),
vl1!(b1110) => Ok(Bitrate::Rate448),
brvl!(b1110 v1 l2) => Ok(Bitrate::Rate384),
brvl!(b1110 v1 l3) => Ok(Bitrate::Rate320),
v2_l23!(b1110) => Ok(Bitrate::Rate160),
(br!(b1111), _, _) => Err(Error::BitrateBad),
(_, Version::Reserved, _) | (_, _, Layer::Reserved) => Err(Error::CannotResolveBitrate),
}
}
pub fn from_kbps(kbps: usize) -> Option<Self> {
println!("{kbps}");
match kbps {
8 => Some(Bitrate::Rate8),
16 => Some(Bitrate::Rate16),
24 => Some(Bitrate::Rate24),
32 => Some(Bitrate::Rate32),
40 => Some(Bitrate::Rate40),
48 => Some(Bitrate::Rate48),
56 => Some(Bitrate::Rate56),
64 => Some(Bitrate::Rate64),
80 => Some(Bitrate::Rate80),
96 => Some(Bitrate::Rate96),
112 => Some(Bitrate::Rate112),
128 => Some(Bitrate::Rate128),
144 => Some(Bitrate::Rate144),
160 => Some(Bitrate::Rate160),
176 => Some(Bitrate::Rate176),
192 => Some(Bitrate::Rate192),
224 => Some(Bitrate::Rate224),
256 => Some(Bitrate::Rate256),
288 => Some(Bitrate::Rate288),
320 => Some(Bitrate::Rate320),
352 => Some(Bitrate::Rate352),
384 => Some(Bitrate::Rate384),
416 => Some(Bitrate::Rate416),
448 => Some(Bitrate::Rate448),
_ => None,
}
}
pub const fn kbps(&self) -> Option<usize> {
match self {
Bitrate::RateFree => None,
Bitrate::Rate8 => Some(8),
Bitrate::Rate16 => Some(16),
Bitrate::Rate24 => Some(24),
Bitrate::Rate32 => Some(32),
Bitrate::Rate40 => Some(40),
Bitrate::Rate48 => Some(48),
Bitrate::Rate56 => Some(56),
Bitrate::Rate64 => Some(64),
Bitrate::Rate80 => Some(80),
Bitrate::Rate96 => Some(96),
Bitrate::Rate112 => Some(112),
Bitrate::Rate128 => Some(128),
Bitrate::Rate144 => Some(144),
Bitrate::Rate160 => Some(160),
Bitrate::Rate176 => Some(176),
Bitrate::Rate192 => Some(192),
Bitrate::Rate224 => Some(224),
Bitrate::Rate256 => Some(256),
Bitrate::Rate288 => Some(288),
Bitrate::Rate320 => Some(320),
Bitrate::Rate352 => Some(352),
Bitrate::Rate384 => Some(384),
Bitrate::Rate416 => Some(416),
Bitrate::Rate448 => Some(448),
}
}
pub const fn bitrate(&self) -> Option<usize> {
match self.kbps() {
None => None,
Some(kbr) => Some(kbr * 1000),
}
}
}
pub enum Fourbit {
Zero,
One,
Two,
Three,
Four,
Five,
Six,
Seven,
Eight,
Nine,
Ten,
Eleven,
Twelve,
Thirteen,
Fourteen,
Fifteen,
}
impl Fourbit {
pub fn from_u8(value: u8) -> Option<Self> {
if value > 15 {
return None;
}
Some(match value {
0 => Fourbit::Zero,
1 => Fourbit::One,
2 => Fourbit::Two,
3 => Fourbit::Three,
4 => Fourbit::Four,
5 => Fourbit::Five,
6 => Fourbit::Six,
7 => Fourbit::Seven,
8 => Fourbit::Eight,
9 => Fourbit::Nine,
10 => Fourbit::Ten,
11 => Fourbit::Eleven,
12 => Fourbit::Twelve,
13 => Fourbit::Thirteen,
14 => Fourbit::Fourteen,
15 => Fourbit::Fifteen,
_ => unreachable!(),
})
}
}
#[cfg(test)]
mod test {
use super::{Bitrate, Layer, Version};
// Lookup table of bitrates excluding 0000 and 1111. The former is Free, the latter is Bad
#[rustfmt::skip]
const BR_LUT: [usize; 5 * 14] = [
32, 32, 32, 32, 8,
64, 48, 40, 48, 16,
96, 56, 48, 56, 24,
128, 64, 56, 64, 32,
160, 80, 64, 80, 40,
192, 96, 80, 96, 48,
224, 112, 96, 112, 56,
256, 128, 112, 128, 64,
288, 160, 128, 144, 80,
320, 192, 160, 160, 96,
352, 224, 192, 176, 112,
384, 256, 224, 192, 128,
416, 320, 256, 224, 144,
448, 384, 320, 256, 160
];
fn bitrate_lut() -> Vec<Bitrate> {
BR_LUT
.into_iter()
.map(|kbps| Bitrate::from_kbps(kbps).unwrap())
.collect()
}
#[test]
fn correctly_resolves_bitrates() {
let lut = bitrate_lut();
for index in 0..75 {
let br = (index as u8 / 5) << 4;
let (version, layer) = match index % 5 {
0 => (Version::Mpeg1, Layer::Layer1),
1 => (Version::Mpeg1, Layer::Layer2),
2 => (Version::Mpeg1, Layer::Layer3),
3 => (Version::Mpeg2, Layer::Layer1),
4 => (Version::Mpeg2, Layer::Layer2),
_ => unreachable!(),
};
let resolved_bitrate = Bitrate::resolve(br, version, layer).unwrap();
let bre = match index {
0 | 1 | 2 | 3 | 4 => Bitrate::RateFree,
_ => lut[index - 5],
};
if resolved_bitrate != bre {
panic!("Failed on {:04b}, {version:?}, {layer:?}", br >> 4);
}
assert_eq!(bre, resolved_bitrate)
}
}
}
|