forked from adamdruppe/arsd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mp3.d
2851 lines (2567 loc) · 109 KB
/
mp3.d
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/++
Port of the C library [https://github.com/lieff/minimp3|minimp3] to D, with an added high-level API
in the form of [MP3Decoder].
You can use it through [arsd.simpleaudio]'s playMp3 function for a very easy high-level api to just play
the sound. If all you want is the mp3 sound to come out your computer's speakers, use that api. If you want
to inspect a mp3 file's innards, play it through a different api, convert it to different formats, etc., this
module may help you.
Authors:
Original C source: https://github.com/lieff/minimp3
To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty. See <http://creativecommons.org/publicdomain/zero/1.0/>.
Translated to D by Guillaume Piolat. He stripped it down a bit for the needs of audio-formats.
Lightly modified by Adam for arsd inclusion and maintaining compatibility with the published interface of the old implementation.
I added the [MP3Decoder] back from the old implementation and removed `@nogc nothrow` for being annoying. Of course, most things
here don't do GC or throws at runtime anyway, with the exception of first call setups.
History:
This module used to be based on a different minimp3, written in 2008 and based on ffmpeg code. On November 20, 2020, I replaced it with the newer implementation (which is the designated successor of the original minimp3). Since the implementation was all private previously, this meant little outside breakage, but some attribute qualifiers changed.
The new minimp3 implementation is public, but not part of the official `arsd.mp3` API, so I reserve the right to change it again without notice. Only the [MP3Decoder] has stability guarantees. (Though I am unlikely to change what works.)
+/
module arsd.mp3;
/++
The high-level D api for mp3 decoding. You construct it with a read and seek function,
then inspect the members for info about the mp3, then call [decodeNextFrame] and
[frameSamplesFloat] in a loop to get the data until frameSamplesFloat returns an empty
array, indicating you've hit the end of file.
+/
class MP3Decoder {
// https://github.com/lieff/minimp3#high-level-api
private mp3dec_ex_t dec;
private bool isOpen;
private static size_t read_cb_static(void* buf, size_t size, void* user_data) {
//import std.stdio; writeln("request ", size);
auto d = cast(MP3Decoder) user_data;
return d.reader(cast(ubyte[]) buf[0 .. size]);
}
private static int seek_cb_static(uint64_t position, void* user_data) {
auto d = cast(MP3Decoder) user_data;
return d.seeker(position);
}
// read bytes into the buffer, return number of bytes read or 0 for EOF, -1 on error
// will never be called with empty buffer, or buffer more than 128KB
alias ReadBufFn = int delegate (void[] buf);
private int delegate (ubyte[] buf) reader;
private int delegate(ulong where) seeker;
private mp3dec_io_t io;
/++
Creates a mp3 decoder out of two functions: a reader and a seeker. Both must work together
for things to work.
A reader works like `fread` - it gives you a buffer and you fill it as much as you can,
and return the number of bytes filled. A seeker works like `fseek`, it tells you a position
in the file to go to and you do it, then return 0. If you return non-zero, the library will
treat that as an I/O error. You can forward directly to those C functions if you like. Or, you
can refer to a memory buffer, or even a network stream. It is all up to you.
Please note that you are responsible for closing the file when you are done with it. This means
*after* the mp3 decoder is no longer used. The api will not help you determine this.
Also note that the delegates are NOT scope - they are held on to by this class. It is your
responsibility to get lifetimes right; the delegates need to remain valid throughout the use
of the decoder object. As a particular warning, a [std.stdio.File] has a RAII destructor that
will trigger when it goes out of scope - $(I not) at the end of its referenced lifetime. Meaning
the object will be copied for your delegates, but the file will be closed when the function
returns, making it useless! I recommend you encapsulate all this in an additional object.
[arsd.simpleaudio]'s playMp3 function does this encapsulation for you.
History:
It used to take the `reader` as scope, but that changed on November 20, 2022. It also
used to not take the `seeker` argument, but this is now required.
The `reader` used to take `void[] buf`, but now takes `ubyte[] buf`. This is a simple
change in your signature, otherwise they work the same. ubyte is more appropriate since
it is looking for file bytes, not random untyped data.
I realize these are all breaking changes, but the fix is easy and the new functionality,
the [seek] function among others, is worth it. To me anyway, and I hope for you too.
+/
this (int delegate (ubyte[] buf) reader, int delegate(ulong where) seeker) @system {
if (reader is null)
throw new Exception("reader is null");
if (seeker is null)
throw new Exception("seeker is null");
this.reader = reader;
this.seeker = seeker;
io.read = &read_cb_static;
io.read_data = cast(void*) this;
io.seek = &seek_cb_static;
io.seek_data = cast(void*) this;
auto ret = mp3dec_ex_open_cb(&dec, &io, MP3D_SEEK_TO_SAMPLE);
if(ret != 0) {
// import std.stdio; writeln(ret);
throw new Exception("open");
}
isOpen = true;
}
~this () { close(); }
/++
Seeks the decoder to the given sample number, so the next call to [decodeNextFrame] will move to that part of the file.
To go from a time in seconds to a sampleNumber, multiply by [sampleRate] and by [channels]: `mp3.seek(timeInSeconds * mp3.sampleRate * mp3.channels)`.
Returns: `true` if the seek was successful. Among the reasons it can be false is giving an invalid sample number, an i/o error, or the decoder already being closed.
History:
Added November 20, 2022
+/
bool seek(uint sampleNumber) {
if(!isOpen)
return false;
auto ret = mp3dec_ex_seek(&dec, sampleNumber);
// import std.stdio; writeln("seek ", ret);
return true;
}
/++
Closes the decoder, freeing memory associated with it. Remember, this does NOT close any file you referred to in your reader and seeker delegates, it just frees internal memory.
+/
void close () {
if(isOpen) {
mp3dec_ex_close(&dec);
isOpen = false;
}
}
private float[] decodedFramesFloat;
private short[] decodedFramesShort;
private size_t decodedFramesUsed;
private bool shortDecoded;
// Deprecated - it ignores the reader and calls the other overload.
// Only provided for API compatibility with old versions.
// FIXME: when it reaches eof, what do we want to do?
/++
Decodes the next frame of data and stores it in [frameSamplesFloat] (also accessible
through [frameSamples] as `short[]`).
Returns:
`true` if a new frame was decoded, `false` if it wasn't. Possible reasons for failure are trying to decode an invalid mp3 (see [valid]) and reaching end-of-file.
Params:
reader = ignored. Overload only provided for API compatibility with older versions of `arsd.mp3`.
See_Also:
[channels], [sampleRate]
+/
bool decodeNextFrame () {
if(!isOpen)
return false;
if(decodedFramesFloat is null)
decodedFramesFloat = new float[](MINIMP3_MAX_SAMPLES_PER_FRAME);
auto ret = mp3dec_ex_read(&dec, decodedFramesFloat.ptr, decodedFramesFloat.length);
// import std.stdio; writeln("ret ", ret);
decodedFramesUsed = ret;
shortDecoded = false;
if(ret <= 0) {
close();
}
return ret > 0;
}
/// ditto
deprecated bool decodeNextFrame (scope ReadBufFn reader) {
return decodeNextFrame();
}
/++
Returns `true` if the object is in a valid state. May be
false if the stream was corrupted or reached end-of-file.
+/
@property bool valid () const pure nothrow @trusted @nogc {
return isOpen;
}
/++
Returns the sample rate, in hz, of the audio stream. Note that
this is per channel, so if this returns 44,100 hz and [channels]
returns 2, you'd have a total of 88,200 samples per second between
the two channels.
See_Also:
[channels]
+/
@property uint sampleRate () const pure nothrow @trusted @nogc {
return valid ? dec.info.hz : 0;
}
/++
Returns the number of channels in the file. Note the channel
data is interlaced, meaning the first sample is left channel,
second sample right channel, then back and forth (assuming two
channels, of course).
See_Also:
[sampleRate]
+/
@property ubyte channels () const pure nothrow @trusted @nogc {
return (valid ? cast(ubyte) dec.info.channels : 0);
}
/++
Returns the bitrate of the first frame, in kbps.
Note that different frames of the file may vary bitrate, so this
is only an approximation of the whole file.
History:
Added November 21, 2022 (dub v10.10)
+/
@property int bitrate() const pure nothrow @trusted @nogc {
return (valid ? dec.info.bitrate_kbps : 0);
}
/++
Returns the duration of the mp3, in seconds, if available, or `float.nan` if it is unknown
(unknown may happen because it is an unseekable stream without metadata).
History:
Added November 26, 2022 (dub v10.10)
+/
@property float duration() const pure nothrow @trusted @nogc {
return (valid ? (cast(float) dec.samples / sampleRate / channels) : float.nan);
}
/++
Returns the number of samples in the current frame, as prepared by [decodeNextFrame].
You probably never need to actually call this, as it is just `frameSamplesFloat.length / channels`.
See_Also:
[frameSamplesFloat], [frameSamples], [decodeNextFrame], [channels]
+/
@property int samplesInFrame () const pure nothrow @trusted @nogc {
if(valid)
return cast(int) (decodedFramesUsed / channels);
else
return 0;
}
/++
Returns the current frame, as prepared by [decodeNextFrame], in signed 16 bit (`short[]`) format.
This will allocate the buffer on first use, then continue reusing it for the duration of the `MP3Decoder`
instance's lifetime, meaning it will not allocate again in the loop, but you should not keep a reference
to the array because its contents will be overwritten as you continue calling `decodeNextFrame`.
Is you want something that never allocates, see [frameSamplesFloat].
Please note that you MUST call [decodeNextFrame] first.
See_Also:
[frameSamplesFloat], [decodeNextFrame]
History:
This was `@nogc` until November 20, 2022. It now lazily allocates the buffer
if needed. If you want something nogc, use [frameSamplesFloat] and convert it yourself.
+/
@property short[] frameSamples () nothrow {
if(decodedFramesShort is null)
decodedFramesShort = new short[](MINIMP3_MAX_SAMPLES_PER_FRAME);
if(!shortDecoded) {
foreach(i, frame; frameSamplesFloat)
decodedFramesShort[i] = cast(short)(frame * short.max);
shortDecoded = true;
}
return decodedFramesShort[0 .. decodedFramesUsed];
}
/++
Returns the current frame, as prepared by [decodeNextFrame], in floating point (`float[]`) format.
You should not keep a reference to the array because its contents will be overwritten as you continue
calling `decodeNextFrame`.
See_Also:
[frameSamples], [decodeNextFrame]
History:
Added November 20, 2022 (dub v10.10)
+/
@property float[] frameSamplesFloat () nothrow @nogc {
return decodedFramesFloat[0 .. decodedFramesUsed];
}
/++
Calls `seek(0)`. This function is provided for compatibility with older versions of the MP3Decoder api and has no other use.
Params:
reader = ignored, provided just for legacy compatibility
+/
deprecated void restart (scope ReadBufFn reader) { seek(0); }
/++
Does nothing. Only provided for compatibility with older versions of the MP3Decoder api.
Previously would resync to a frame after a file seek, but this is no longer necessary. Call [seek] instead.
+/
deprecated void sync (scope ReadBufFn reader) { }
}
@system:
import core.stdc.stdlib;
import core.stdc.string;
// nothrow:
// @nogc:
alias uint8_t = ubyte;
alias uint16_t = ushort;
alias uint32_t = uint;
alias uint64_t = ulong;
alias int16_t = short;
alias int32_t = int;
@safe enum MINIMP3_MAX_SAMPLES_PER_FRAME = (1152*2);
struct mp3dec_frame_info_t
{
int frame_bytes;
int frame_offset;
int channels;
int hz;
int layer;
int bitrate_kbps;
}
struct mp3dec_t
{
float[9*32][2] mdct_overlap;
float[15*2*32] qmf_state;
int reserv;
int free_format_bytes;
ubyte[4] header;
ubyte[511] reserv_buf;
}
version = MINIMP3_FLOAT_OUTPUT;
alias mp3d_sample_t = float;
enum MAX_FREE_FORMAT_FRAME_SIZE = 2304; /* more than ISO spec's */
enum MAX_FRAME_SYNC_MATCHES = 10;
enum MAX_L3_FRAME_PAYLOAD_BYTES = MAX_FREE_FORMAT_FRAME_SIZE; /* MUST be >= 320000/8/32000*1152 = 1440 */
enum MAX_BITRESERVOIR_BYTES = 511;
enum SHORT_BLOCK_TYPE = 2;
enum STOP_BLOCK_TYPE = 3;
enum MODE_MONO = 3;
enum MODE_JOINT_STEREO = 1;
enum HDR_SIZE = 4;
bool HDR_IS_MONO(const(ubyte)* h)
{
return (((h[3]) & 0xC0) == 0xC0);
}
bool HDR_IS_MS_STEREO(const(ubyte)* h)
{
return (((h[3]) & 0xE0) == 0x60);
}
bool HDR_IS_FREE_FORMAT(const(ubyte)* h)
{
return (((h[2]) & 0xF0) == 0);
}
bool HDR_IS_CRC(const(ubyte)* h)
{
return (!((h[1]) & 1));
}
int HDR_TEST_PADDING(const(ubyte)* h)
{
return ((h[2]) & 0x2);
}
int HDR_TEST_MPEG1(const(ubyte)* h)
{
return ((h[1]) & 0x8);
}
int HDR_TEST_NOT_MPEG25(const(ubyte)* h)
{
return ((h[1]) & 0x10);
}
int HDR_TEST_I_STEREO(const(ubyte)* h)
{
return ((h[3]) & 0x10);
}
int HDR_TEST_MS_STEREO(const(ubyte)* h)
{
return ((h[3]) & 0x20);
}
int HDR_GET_STEREO_MODE(const(ubyte)* h)
{
return (((h[3]) >> 6) & 3);
}
int HDR_GET_STEREO_MODE_EXT(const(ubyte)* h)
{
return (((h[3]) >> 4) & 3);
}
int HDR_GET_LAYER(const(ubyte)* h)
{
return (((h[1]) >> 1) & 3);
}
int HDR_GET_BITRATE(const(ubyte)* h)
{
return ((h[2]) >> 4);
}
int HDR_GET_SAMPLE_RATE(const(ubyte)* h)
{
return (((h[2]) >> 2) & 3);
}
int HDR_GET_MY_SAMPLE_RATE(const(ubyte)* h)
{
return (HDR_GET_SAMPLE_RATE(h) + (((h[1] >> 3) & 1) + ((h[1] >> 4) & 1))*3);
}
bool HDR_IS_FRAME_576(const(ubyte)* h)
{
return ((h[1] & 14) == 2);
}
bool HDR_IS_LAYER_1(const(ubyte)* h)
{
return ((h[1] & 6) == 6);
}
enum BITS_DEQUANTIZER_OUT = -1;
enum MAX_SCF = 255 + BITS_DEQUANTIZER_OUT*4 - 210;
enum MAX_SCFI = (MAX_SCF + 3) & ~3;
int MINIMP3_MIN(int a, int b)
{
return (a > b) ? b : a;
}
ulong MINIMP3_MIN(ulong a, ulong b)
{
return (a > b) ? b : a;
}
int MINIMP3_MAX(int a, int b)
{
return (a < b) ? b : a;
}
struct bs_t
{
const(uint8_t)* buf;
int pos, limit;
}
struct L12_scale_info
{
float[3*64] scf;
uint8_t total_bands;
uint8_t stereo_bands;
ubyte[64] bitalloc;
ubyte[64] scfcod;
}
struct L12_subband_alloc_t
{
uint8_t tab_offset, code_tab_width, band_count;
}
struct L3_gr_info_t
{
const(uint8_t)* sfbtab;
uint16_t part_23_length, big_values, scalefac_compress;
uint8_t global_gain, block_type, mixed_block_flag, n_long_sfb, n_short_sfb;
uint8_t[3] table_select, region_count, subblock_gain;
uint8_t preflag, scalefac_scale, count1_table, scfsi;
}
struct mp3dec_scratch_t
{
bs_t bs;
uint8_t[MAX_BITRESERVOIR_BYTES + MAX_L3_FRAME_PAYLOAD_BYTES] maindata;
L3_gr_info_t[4] gr_info;
float[576][2] grbuf;
float[40] scf;
float[2*32][18 + 15] syn;
uint8_t[39][2] ist_pos;
}
void bs_init(bs_t *bs, const(uint8_t)*data, int bytes)
{
bs.buf = data;
bs.pos = 0;
bs.limit = bytes*8;
}
uint32_t get_bits(bs_t *bs, int n)
{
uint32_t next, cache = 0, s = bs.pos & 7;
int shl = n + s;
const(uint8_t)*p = bs.buf + (bs.pos >> 3);
if ((bs.pos += n) > bs.limit)
return 0;
next = *p++ & (255 >> s);
while ((shl -= 8) > 0)
{
cache |= next << shl;
next = *p++;
}
return cache | (next >> -shl);
}
int hdr_valid(const uint8_t *h)
{
return 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);
}
int hdr_compare(const uint8_t *h1, const uint8_t *h2)
{
return 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));
}
uint hdr_bitrate_kbps(const uint8_t *h)
{
static immutable uint8_t[15][3][2] halfrate =
[
[ [ 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 ] ],
];
return 2*halfrate[!!HDR_TEST_MPEG1(h)][HDR_GET_LAYER(h) - 1][HDR_GET_BITRATE(h)];
}
uint hdr_sample_rate_hz(const uint8_t *h)
{
static immutable uint[3] g_hz = [ 44100, 48000, 32000 ];
return g_hz[HDR_GET_SAMPLE_RATE(h)] >> cast(int)!HDR_TEST_MPEG1(h) >> cast(int)!HDR_TEST_NOT_MPEG25(h);
}
uint hdr_frame_samples(const uint8_t *h)
{
return HDR_IS_LAYER_1(h) ? 384 : (1152 >> cast(int)HDR_IS_FRAME_576(h));
}
int hdr_frame_bytes(const uint8_t *h, int free_format_size)
{
int frame_bytes = hdr_frame_samples(h)*hdr_bitrate_kbps(h)*125/hdr_sample_rate_hz(h);
if (HDR_IS_LAYER_1(h))
{
frame_bytes &= ~3; /* slot align */
}
return frame_bytes ? frame_bytes : free_format_size;
}
static int hdr_padding(const uint8_t *h)
{
return HDR_TEST_PADDING(h) ? (HDR_IS_LAYER_1(h) ? 4 : 1) : 0;
}
const(L12_subband_alloc_t)* L12_subband_alloc_table(const uint8_t *hdr, L12_scale_info *sci)
{
const(L12_subband_alloc_t) *alloc;
int mode = HDR_GET_STEREO_MODE(hdr);
int nbands, stereo_bands = (mode == MODE_MONO) ? 0 : (mode == MODE_JOINT_STEREO) ? (HDR_GET_STEREO_MODE_EXT(hdr) << 2) + 4 : 32;
if (HDR_IS_LAYER_1(hdr))
{
static immutable L12_subband_alloc_t[] g_alloc_L1 =
[
L12_subband_alloc_t(76, 4, 32)
];
alloc = g_alloc_L1.ptr;
nbands = 32;
}
else if (!HDR_TEST_MPEG1(hdr))
{
static immutable L12_subband_alloc_t[] g_alloc_L2M2 =
[
L12_subband_alloc_t(60, 4, 4),
L12_subband_alloc_t(44, 3, 7 ),
L12_subband_alloc_t(44, 2, 19),
];
alloc = g_alloc_L2M2.ptr;
nbands = 30;
}
else
{
static immutable L12_subband_alloc_t[] g_alloc_L2M1 =
[
L12_subband_alloc_t(0, 4, 3),
L12_subband_alloc_t(16, 4, 8),
L12_subband_alloc_t(32, 3, 12),
L12_subband_alloc_t(40, 2, 7)
];
int sample_rate_idx = HDR_GET_SAMPLE_RATE(hdr);
uint kbps = hdr_bitrate_kbps(hdr) >> cast(int)(mode != MODE_MONO);
if (!kbps) /* free-format */
{
kbps = 192;
}
alloc = g_alloc_L2M1.ptr;
nbands = 27;
if (kbps < 56)
{
static immutable L12_subband_alloc_t[] g_alloc_L2M1_lowrate =
[
L12_subband_alloc_t(44, 4, 2),
L12_subband_alloc_t(44, 3, 10)
];
alloc = g_alloc_L2M1_lowrate.ptr;
nbands = sample_rate_idx == 2 ? 12 : 8;
}
else if (kbps >= 96 && sample_rate_idx != 1)
{
nbands = 30;
}
}
sci.total_bands = cast(uint8_t)nbands;
sci.stereo_bands = cast(uint8_t)MINIMP3_MIN(stereo_bands, nbands);
return alloc;
}
void L12_read_scalefactors(bs_t *bs, uint8_t *pba, uint8_t *scfcod, int bands, float *scf)
{
static immutable float[18*3] g_deq_L12 =
[
3.17891e-07, 2.52311e-07, 2.00259e-07, 1.36239e-07, 1.08133e-07, 8.58253e-08,
6.35783e-08, 5.04621e-08, 4.00518e-08, 3.07637e-08, 2.44172e-08, 1.93799e-08,
1.51377e-08, 1.20148e-08, 9.53615e-09, 7.50925e-09, 5.96009e-09, 4.73053e-09,
3.7399e-09, 2.96836e-09, 2.35599e-09, 1.86629e-09, 1.48128e-09, 1.17569e-09,
9.32233e-10, 7.39914e-10, 5.8727e-10, 4.65889e-10, 3.69776e-10, 2.93492e-10,
2.32888e-10, 1.84843e-10, 1.4671e-10, 1.1643e-10, 9.24102e-11, 7.3346e-11,
5.82112e-11, 4.62023e-11, 3.66708e-11, 2.91047e-11, 2.31004e-11, 1.83348e-11,
1.45521e-11, 1.155e-11, 9.16727e-12, 3.17891e-07, 2.52311e-07, 2.00259e-07,
1.90735e-07, 1.51386e-07, 1.20155e-07, 1.05964e-07, 8.41035e-08, 6.6753e-08
];
int i, m;
for (i = 0; i < bands; i++)
{
float s = 0;
int ba = *pba++;
int mask = ba ? 4 + ((19 >> scfcod[i]) & 3) : 0;
for (m = 4; m; m >>= 1)
{
if (mask & m)
{
int b = get_bits(bs, 6);
s = g_deq_L12[ba*3 - 6 + b % 3]*(1 << 21 >> b/3);
}
*scf++ = s;
}
}
}
void L12_read_scale_info(const uint8_t *hdr, bs_t *bs, L12_scale_info *sci)
{
static immutable uint8_t[] g_bitalloc_code_tab =
[
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
];
const(L12_subband_alloc_t)* subband_alloc = L12_subband_alloc_table(hdr, sci);
int i, k = 0, ba_bits = 0;
const(uint8_t)*ba_code_tab = g_bitalloc_code_tab.ptr;
for (i = 0; i < sci.total_bands; i++)
{
uint8_t ba;
if (i == k)
{
k += subband_alloc.band_count;
ba_bits = subband_alloc.code_tab_width;
ba_code_tab = g_bitalloc_code_tab.ptr + subband_alloc.tab_offset;
subband_alloc++;
}
ba = ba_code_tab[get_bits(bs, ba_bits)];
sci.bitalloc[2*i] = ba;
if (i < sci.stereo_bands)
{
ba = ba_code_tab[get_bits(bs, ba_bits)];
}
sci.bitalloc[2*i + 1] = sci.stereo_bands ? ba : 0;
}
for (i = 0; i < 2*sci.total_bands; i++)
{
ubyte temp = ( HDR_IS_LAYER_1(hdr) ? 2 : cast(ubyte) get_bits(bs, 2) );
sci.scfcod[i] = sci.bitalloc[i] ? temp : 6;
}
L12_read_scalefactors(bs, sci.bitalloc.ptr, sci.scfcod.ptr, sci.total_bands*2, sci.scf.ptr);
for (i = sci.stereo_bands; i < sci.total_bands; i++)
{
sci.bitalloc[2*i + 1] = 0;
}
}
int L12_dequantize_granule(float *grbuf, bs_t *bs, L12_scale_info *sci, int group_size)
{
int i, j, k, choff = 576;
for (j = 0; j < 4; j++)
{
float *dst = grbuf + group_size*j;
for (i = 0; i < 2*sci.total_bands; i++)
{
int ba = sci.bitalloc[i];
if (ba != 0)
{
if (ba < 17)
{
int half = (1 << (ba - 1)) - 1;
for (k = 0; k < group_size; k++)
{
dst[k] = cast(float)(cast(int)get_bits(bs, ba) - half);
}
} else
{
uint mod = (2 << (ba - 17)) + 1; /* 3, 5, 9 */
uint code = get_bits(bs, mod + 2 - (mod >> 3)); /* 5, 7, 10 */
for (k = 0; k < group_size; k++, code /= mod)
{
dst[k] = cast(float)(cast(int)(code % mod - mod/2));
}
}
}
dst += choff;
choff = 18 - choff;
}
}
return group_size*4;
}
void L12_apply_scf_384(L12_scale_info *sci, const(float)*scf, float *dst)
{
int i, k;
memcpy(dst + 576 + sci.stereo_bands*18, dst + sci.stereo_bands*18, (sci.total_bands - sci.stereo_bands)*18*float.sizeof);
for (i = 0; i < sci.total_bands; i++, dst += 18, scf += 6)
{
for (k = 0; k < 12; k++)
{
dst[k + 0] *= scf[0];
dst[k + 576] *= scf[3];
}
}
}
int L3_read_side_info(bs_t *bs, L3_gr_info_t *gr, const uint8_t *hdr)
{
static immutable uint8_t[23][8] g_scf_long =
[
[ 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 ],
[ 12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2,0 ],
[ 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 ],
[ 6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36,0 ],
[ 6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54,0 ],
[ 4,4,4,4,4,4,6,6,8,8,10,12,16,20,24,28,34,42,50,54,76,158,0 ],
[ 4,4,4,4,4,4,6,6,6,8,10,12,16,18,22,28,34,40,46,54,54,192,0 ],
[ 4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102,26,0 ]
];
static immutable uint8_t[40][8] g_scf_short = [
[ 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 ],
[ 8,8,8,8,8,8,8,8,8,12,12,12,16,16,16,20,20,20,24,24,24,28,28,28,36,36,36,2,2,2,2,2,2,2,2,2,26,26,26,0 ],
[ 4,4,4,4,4,4,4,4,4,6,6,6,6,6,6,8,8,8,10,10,10,14,14,14,18,18,18,26,26,26,32,32,32,42,42,42,18,18,18,0 ],
[ 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,32,32,32,44,44,44,12,12,12,0 ],
[ 4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 ],
[ 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,22,22,22,30,30,30,56,56,56,0 ],
[ 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,6,6,6,10,10,10,12,12,12,14,14,14,16,16,16,20,20,20,26,26,26,66,66,66,0 ],
[ 4,4,4,4,4,4,4,4,4,4,4,4,6,6,6,8,8,8,12,12,12,16,16,16,20,20,20,26,26,26,34,34,34,42,42,42,12,12,12,0 ]
];
static immutable uint8_t[40][8] g_scf_mixed = [
[ 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 ],
[ 12,12,12,4,4,4,8,8,8,12,12,12,16,16,16,20,20,20,24,24,24,28,28,28,36,36,36,2,2,2,2,2,2,2,2,2,26,26,26,0 ],
[ 6,6,6,6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,14,14,14,18,18,18,26,26,26,32,32,32,42,42,42,18,18,18,0 ],
[ 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,32,32,32,44,44,44,12,12,12,0 ],
[ 6,6,6,6,6,6,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,24,24,24,30,30,30,40,40,40,18,18,18,0 ],
[ 4,4,4,4,4,4,6,6,4,4,4,6,6,6,8,8,8,10,10,10,12,12,12,14,14,14,18,18,18,22,22,22,30,30,30,56,56,56,0 ],
[ 4,4,4,4,4,4,6,6,4,4,4,6,6,6,6,6,6,10,10,10,12,12,12,14,14,14,16,16,16,20,20,20,26,26,26,66,66,66,0 ],
[ 4,4,4,4,4,4,6,6,4,4,4,6,6,6,8,8,8,12,12,12,16,16,16,20,20,20,26,26,26,34,34,34,42,42,42,12,12,12,0 ]
];
uint tables, scfsi = 0;
int main_data_begin, part_23_sum = 0;
int sr_idx = HDR_GET_MY_SAMPLE_RATE(hdr); sr_idx -= (sr_idx != 0);
int gr_count = HDR_IS_MONO(hdr) ? 1 : 2;
if (HDR_TEST_MPEG1(hdr))
{
gr_count *= 2;
main_data_begin = get_bits(bs, 9);
scfsi = get_bits(bs, 7 + gr_count);
} else
{
main_data_begin = get_bits(bs, 8 + gr_count) >> gr_count;
}
do
{
if (HDR_IS_MONO(hdr))
{
scfsi <<= 4;
}
gr.part_23_length = cast(uint16_t)get_bits(bs, 12);
part_23_sum += gr.part_23_length;
gr.big_values = cast(uint16_t)get_bits(bs, 9);
if (gr.big_values > 288)
{
return -1;
}
gr.global_gain = cast(uint8_t)get_bits(bs, 8);
gr.scalefac_compress = cast(uint16_t)get_bits(bs, HDR_TEST_MPEG1(hdr) ? 4 : 9);
gr.sfbtab = g_scf_long[sr_idx].ptr;
gr.n_long_sfb = 22;
gr.n_short_sfb = 0;
if (get_bits(bs, 1))
{
gr.block_type = cast(uint8_t)get_bits(bs, 2);
if (!gr.block_type)
{
return -1;
}
gr.mixed_block_flag = cast(uint8_t)get_bits(bs, 1);
gr.region_count[0] = 7;
gr.region_count[1] = 255;
if (gr.block_type == SHORT_BLOCK_TYPE)
{
scfsi &= 0x0F0F;
if (!gr.mixed_block_flag)
{
gr.region_count[0] = 8;
gr.sfbtab = g_scf_short[sr_idx].ptr;
gr.n_long_sfb = 0;
gr.n_short_sfb = 39;
} else
{
gr.sfbtab = g_scf_mixed[sr_idx].ptr;
gr.n_long_sfb = HDR_TEST_MPEG1(hdr) ? 8 : 6;
gr.n_short_sfb = 30;
}
}
tables = get_bits(bs, 10);
tables <<= 5;
gr.subblock_gain[0] = cast(uint8_t)get_bits(bs, 3);
gr.subblock_gain[1] = cast(uint8_t)get_bits(bs, 3);
gr.subblock_gain[2] = cast(uint8_t)get_bits(bs, 3);
} else
{
gr.block_type = 0;
gr.mixed_block_flag = 0;
tables = get_bits(bs, 15);
gr.region_count[0] = cast(uint8_t)get_bits(bs, 4);
gr.region_count[1] = cast(uint8_t)get_bits(bs, 3);
gr.region_count[2] = 255;
}
gr.table_select[0] = cast(uint8_t)(tables >> 10);
gr.table_select[1] = cast(uint8_t)((tables >> 5) & 31);
gr.table_select[2] = cast(uint8_t)((tables) & 31);
gr.preflag = HDR_TEST_MPEG1(hdr) ? (cast(ubyte) get_bits(bs, 1)) : (gr.scalefac_compress >= 500);
gr.scalefac_scale = cast(uint8_t)get_bits(bs, 1);
gr.count1_table = cast(uint8_t)get_bits(bs, 1);
gr.scfsi = cast(uint8_t)((scfsi >> 12) & 15);
scfsi <<= 4;
gr++;
} while(--gr_count);
if (part_23_sum + bs.pos > bs.limit + main_data_begin*8)
{
return -1;
}
return main_data_begin;
}
void L3_read_scalefactors(uint8_t *scf, uint8_t *ist_pos, const uint8_t *scf_size, const uint8_t *scf_count, bs_t *bitbuf, int scfsi)
{
int i, k;
for (i = 0; i < 4 && scf_count[i]; i++, scfsi *= 2)
{
int cnt = scf_count[i];
if (scfsi & 8)
{
memcpy(scf, ist_pos, cnt);
} else
{
int bits = scf_size[i];
if (!bits)
{
memset(scf, 0, cnt);
memset(ist_pos, 0, cnt);
} else
{
int max_scf = (scfsi < 0) ? (1 << bits) - 1 : -1;
for (k = 0; k < cnt; k++)
{
int s = get_bits(bitbuf, bits);
ist_pos[k] = cast(ubyte)(s == max_scf ? -1 : s);
scf[k] = cast(ubyte)s;
}
}
}
ist_pos += cnt;
scf += cnt;
}
scf[0] = scf[1] = scf[2] = 0;
}
float L3_ldexp_q2(float y, int exp_q2)
{
static immutable float[4] g_expfrac =
[ 9.31322575e-10f,7.83145814e-10f,6.58544508e-10f,5.53767716e-10f ];
int e;
do
{
e = MINIMP3_MIN(30*4, exp_q2);
y *= g_expfrac[e & 3]*(1 << 30 >> (e >> 2));
} while ((exp_q2 -= e) > 0);
return y;
}
void L3_decode_scalefactors(const uint8_t *hdr, uint8_t *ist_pos, bs_t *bs, const L3_gr_info_t *gr, float *scf, int ch)
{
static immutable uint8_t[28][3] g_scf_partitions = [
[ 6,5,5, 5,6,5,5,5,6,5, 7,3,11,10,0,0, 7, 7, 7,0, 6, 6,6,3, 8, 8,5,0 ],
[ 8,9,6,12,6,9,9,9,6,9,12,6,15,18,0,0, 6,15,12,0, 6,12,9,6, 6,18,9,0 ],
[ 9,9,6,12,9,9,9,9,9,9,12,6,18,18,0,0,12,12,12,0,12, 9,9,6,15,12,9,0 ]
];
const(uint8_t)* scf_partition = g_scf_partitions[!!gr.n_short_sfb + !gr.n_long_sfb].ptr;
uint8_t[4] scf_size;
uint8_t[40] iscf;
int i, scf_shift = gr.scalefac_scale + 1, gain_exp, scfsi = gr.scfsi;
float gain;
if (HDR_TEST_MPEG1(hdr))
{
static immutable uint8_t[16] g_scfc_decode = [ 0,1,2,3, 12,5,6,7, 9,10,11,13, 14,15,18,19 ];
int part = g_scfc_decode[gr.scalefac_compress];
scf_size[1] = scf_size[0] = cast(uint8_t)(part >> 2);
scf_size[3] = scf_size[2] = cast(uint8_t)(part & 3);
} else
{
static immutable uint8_t[6*4] g_mod = [ 5,5,4,4,5,5,4,1,4,3,1,1,5,6,6,1,4,4,4,1,4,3,1,1 ];
int k, modprod, sfc, ist = HDR_TEST_I_STEREO(hdr) && ch;
sfc = gr.scalefac_compress >> ist;
for (k = ist*3*4; sfc >= 0; sfc -= modprod, k += 4)
{
for (modprod = 1, i = 3; i >= 0; i--)