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
extern crate byteorder;
// use byteorder::{WriteBytesExt, LE};
// use std::io::{self, Cursor, Read, Write};
pub const MAX_SAMPLES_PER_FRAME: usize = 1152 * 2;
/// More than ISO spec's
pub const MAX_FREE_FORMAT_FRAME_SIZE: usize = 2304;
pub const MAX_FRAME_SYNC_MATCHES: usize = 10;
/// MUST be >= 320000/8/32000*1152 = 1440
pub const MAX_L3_FRAME_PAYLOAD_BYTES: usize = MAX_FREE_FORMAT_FRAME_SIZE;
pub const MAX_BITRESERVOIR_BYTES: usize = 511;
pub const SHORT_BLOCK_TYPE: usize = 2;
pub const STOP_BLOCK_TYPE: usize = 3;
pub const MODE_MONO: usize = 3;
pub const MODE_JOINT_STEREO: usize = 1;
pub const HDR_SIZE: usize = 4;
pub const BITS_DEQUANTIZER_OUT: i32 = -1;
pub const MAX_SCF: i32 = 255 + BITS_DEQUANTIZER_OUT * 4 - 210;
pub const MAX_SCFI: i32 = & !3;
// TODO: float vs. int16 output?
// type Mp3Sample = i16;
// pub fn decode_frame(
// dec: &Mp3Dec,
// mp3: &[u8],
// mp3_bytes: usize,
// pcm: &[Mp3Sample],
// info: &FrameInfo,
// ) -> i32 {
// 0
// }
/*
pub fn hdr_valid(h: &[u8]) -> bool {
h[0] == 0xFF
&& ((h[1] & 0xF0) == 0xF0 || (h[1] & 0xFE) == 0xE2)
&& hdr_get_layer(h) != 0
&& hdr_get_bitrate(h) != 15
&& hdr_get_sample_rate(h) != 3
}
pub fn hdr_compare(h1: &[u8], h2: &[u8]) -> bool {
hdr_valid(h2)
&& ((h1[1] ^ h2[1]) & 0xFE) == 0
&& ((h1[2] ^ h2[2]) & 0x0C) == 0
&& !(hdr_is_free_format(h1) ^ hdr_is_free_format(h2))
}
pub fn hdr_bitrate_kbps(h: &[u8]) -> u32 {
let halfrate: [[[u32; 15]; 3]; 2] = [
[
[0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 72, 80],
[0, 4, 8, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 72, 80],
[0, 16, 24, 28, 32, 40, 48, 56, 64, 72, 80, 88, 96, 112, 128],
],
[
[0, 16, 20, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160],
[
0, 16, 24, 28, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192,
],
[
0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192, 208, 224,
],
],
];
2 * halfrate[hdr_test_mpeg1(h) as usize][hdr_get_layer(h) as usize - 1]
[hdr_get_bitrate(h) as usize]
}
pub fn hdr_sample_rate_hz(h: &[u8]) -> u32 {
let g_hz: [u32; 3] = [44100, 48000, 32000];
g_hz[hdr_get_sample_rate(h) as usize]
>> (!hdr_test_mpeg1(h)) as u32
>> (!hdr_test_not_mpeg25(h)) as u32
}
pub fn hdr_frame_samples(h: &[u8]) -> u32 {
if hdr_is_layer_1(h) {
384
} else {
1152 >> (hdr_is_frame_576(h) as i32)
}
}
pub fn hdr_frame_bytes(h: &[u8], free_format_size: u32) -> u32 {
let mut frame_bytes = hdr_frame_samples(h) * hdr_bitrate_kbps(h) * 125 / hdr_sample_rate_hz(h);
if hdr_is_layer_1(h) {
// Slot align
frame_bytes &= !3;
}
if frame_bytes != 0 {
frame_bytes
} else {
free_format_size
}
}
pub fn hdr_padding(h: &[u8]) -> u32 {
if hdr_test_padding(h) {
if hdr_is_layer_1(h) {
4
} else {
1
}
} else {
0
}
}
pub fn L12_subband_alloc_table(hdr: &[u8], sci: &mut L12ScaleInfo) -> Vec<L12SubbandAlloc> {
let mode = hdr_get_stereo_mode(hdr) as usize;
let mut nbands;
let mut alloc: Vec<L12SubbandAlloc> = vec![];
let stereo_bands = if mode == MODE_MONO {
0
} else if mode == MODE_JOINT_STEREO {
(hdr_get_stereo_mode_ext(hdr) << 2) + 4
} else {
32
};
if hdr_is_layer_1(hdr) {
alloc.push(L12SubbandAlloc {
tab_offset: 76,
code_tab_width: 4,
band_count: 32,
});
nbands = 32;
} else if !hdr_test_mpeg1(hdr) {
alloc.push(L12SubbandAlloc {
tab_offset: 60,
code_tab_width: 4,
band_count: 4,
});
alloc.push(L12SubbandAlloc {
tab_offset: 44,
code_tab_width: 3,
band_count: 7,
});
alloc.push(L12SubbandAlloc {
tab_offset: 44,
code_tab_width: 2,
band_count: 19,
});
nbands = 30;
} else {
let sample_rate_idx = hdr_get_sample_rate(hdr);
// TODO: Clean up this comparison
let mut kbps = hdr_bitrate_kbps(hdr) >> ((mode != MODE_MONO) as u32);
if kbps == 0 {
kbps = 192;
}
alloc.push(L12SubbandAlloc {
tab_offset: 0,
code_tab_width: 4,
band_count: 3,
});
alloc.push(L12SubbandAlloc {
tab_offset: 16,
code_tab_width: 4,
band_count: 8,
});
alloc.push(L12SubbandAlloc {
tab_offset: 32,
code_tab_width: 3,
band_count: 12,
});
alloc.push(L12SubbandAlloc {
tab_offset: 40,
code_tab_width: 2,
band_count: 7,
});
nbands = 27;
if kbps < 56 {
alloc.clear();
alloc.push(L12SubbandAlloc {
tab_offset: 44,
code_tab_width: 4,
band_count: 2,
});
alloc.push(L12SubbandAlloc {
tab_offset: 44,
code_tab_width: 3,
band_count: 10,
});
nbands = if sample_rate_idx == 2 { 12 } else { 8 };
} else if (kbps >= 96 && sample_rate_idx != 1) {
// TODO: sigh, and possibly weep.
// I think this basically just chops off the last few
// entries in the alloc defined above the previous if
// statement.
nbands = 30;
}
}
sci.total_bands = nbands;
sci.stereo_bands = u8::min(stereo_bands, nbands);
alloc
}
pub fn L12_read_scalefactors(bs: &mut Bs, pba: &[u8], scfcod: &[u8], bands: usize, scf: &mut [f32]) {
// TODO: The C version uses macros to build this array statically,
// which is a PITA so for now we just do it the simple and slower way.
let mut g_deq_L12: Vec<f32> = vec![];
{
let mut DQ = |x: f32| {
g_deq_L12.push(9.53674316e-07 / x);
g_deq_L12.push(7.56931807e-07 / x);
g_deq_L12.push(6.00777173e-07 / x);
};
DQ(3.0);
DQ(7.0);
DQ(15.0);
DQ(31.0);
DQ(63.0);
DQ(127.0);
DQ(255.0);
DQ(511.0);
DQ(1023.0);
DQ(2047.0);
DQ(4095.0);
DQ(8191.0);
DQ(16383.0);
DQ(32767.0);
DQ(65535.0);
DQ(3.0);
DQ(5.0);
DQ(9.0);
}
let mut scf_idx = 0;
for i in 0..bands {
let ba = pba[i];
let mask = if ba != 0 {
4 + ((19 >> scfcod[i]) & 3)
} else {
0
};
let mut m = 4;
while m != 0 {
let s;
if (mask & m) != 0 {
let b = bs.get_bits(6);
let idx = (ba as u32 * 3 - 6 + b % 3) as usize;
s = g_deq_L12[idx] * (1 << 21 >> (b / 3)) as f32;
} else {
s = 0.0;
}
// TODO: Check the post and pre-increment order here!!!
scf[scf_idx] = s;
scf_idx += 1;
}
}
}
pub fn L12_read_scale_info(hdr: &[u8], bs: &mut Bs, sci: &mut L12ScaleInfo) {
let g_bitalloc_code_tab: &[u8] = &[
0, 17, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 0, 17, 18, 3, 19, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 16, 0, 17, 18, 3, 19, 4, 5, 16, 0, 17, 18, 16, 0, 17, 18, 19, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 0, 17, 18, 3, 19, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 0, 2,
3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
];
let subband_alloc = L12_subband_alloc_table(hdr, sci);
let mut subband_alloc_idx = 0;
let mut k: usize = 0;
let mut ba_bits = 0;
let mut ba_code_tab_idx: usize = 0;
for i in 0..(sci.total_bands as usize) {
let ba: u8;
if i == k {
let sb = &subband_alloc[subband_alloc_idx];
k += sb.band_count as usize;
ba_bits = sb.code_tab_width;
ba_code_tab_idx = sb.tab_offset as usize;
subband_alloc_idx += 1;
}
let ba_idx: usize = ba_code_tab_idx + (bs.get_bits(ba_bits as u32) as usize);
ba = g_bitalloc_code_tab[ba_idx];
sci.bitalloc[2 * i + 1] = if sci.stereo_bands != 0 { ba } else { 0 };
}
for i in 0..(2 * sci.total_bands as usize) {
sci.scfcod[i] = if sci.bitalloc[i] != 0 {
if hdr_is_layer_1(hdr) {
2
} else {
bs.get_bits(2) as u8
}
} else {
6
};
}
L12_read_scalefactors(
bs,
&sci.bitalloc,
&sci.scfcod,
(sci.total_bands * 2) as usize,
&mut sci.scf,
);
// TODO: This clear can probably be better.
for i in sci.stereo_bands..sci.total_bands {
let i = i as usize;
sci.bitalloc[2 * i + 1] = 0;
}
}
*/