-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrflac.c
1752 lines (1395 loc) · 59.1 KB
/
rflac.c
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
#include "rflac.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h> /* For memset() */
/*
This is something that could be optimized to good effect. Use the CPUs built-in CLZ instruction.
*/
static rflac_uint32 rflac_clz_64(rflac_uint64 x)
{
rflac_uint32 n;
rflac_uint32 clz_table_4[] = {
0,
4,
3, 3,
2, 2, 2, 2,
1, 1, 1, 1, 1, 1, 1, 1
};
if (x == 0) {
return sizeof(x)*8;
}
n = clz_table_4[x >> (sizeof(x)*8 - 4)];
if (n == 0) {
if ((x & ((rflac_uint64)0xFFFFFFFF << 32)) == 0) { n = 32; x <<= 32; }
if ((x & ((rflac_uint64)0xFFFF0000 << 32)) == 0) { n += 16; x <<= 16; }
if ((x & ((rflac_uint64)0xFF000000 << 32)) == 0) { n += 8; x <<= 8; }
if ((x & ((rflac_uint64)0xF0000000 << 32)) == 0) { n += 4; x <<= 4; }
n += clz_table_4[x >> (sizeof(x)*8 - 4)];
}
return n - 1;
}
static rflac_uint32 rflac_swap_endian_32(rflac_uint32 x)
{
return ((x & 0xFF000000) >> 24) |
((x & 0x00FF0000) >> 8) |
((x & 0x0000FF00) << 8) |
((x & 0x000000FF) << 24);
}
static rflac_uint64 rflac_swap_endian_64(rflac_uint64 x)
{
/* Weird "<< 32" bitshift is required for C89 because it doesn't support 64-bit constants. Should be optimized out by a good compiler. */
return ((x & ((rflac_uint64)0xFF000000 << 32)) >> 56) |
((x & ((rflac_uint64)0x00FF0000 << 32)) >> 40) |
((x & ((rflac_uint64)0x0000FF00 << 32)) >> 24) |
((x & ((rflac_uint64)0x000000FF << 32)) >> 8) |
((x & ((rflac_uint64)0xFF000000 )) << 8) |
((x & ((rflac_uint64)0x00FF0000 )) << 24) |
((x & ((rflac_uint64)0x0000FF00 )) << 40) |
((x & ((rflac_uint64)0x000000FF )) << 56);
}
static rflac_bool8 rflac_is_little_endian()
{
#if defined(__i386) || defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64)
return 1;
#elif defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN
return 0;
#else
int n = 1;
return (*(char*)&n) == 1;
#endif
}
static rflac_uint32 rflac_be2host_32(rflac_uint32 x)
{
if (rflac_is_little_endian()) {
return rflac_swap_endian_32(x);
}
return x;
}
static rflac_uint64 rflac_be2host_64(rflac_uint64 x)
{
if (rflac_is_little_endian()) {
return rflac_swap_endian_64(x);
}
return x;
}
/**************************************************************************************************
Bit Stream
This is taken from dr_flac with a few adjustments. For implementation simplicity, it always uses a
64-bit cache rather than choosing between 32-bit and 64-bit depending on the architecture. For an
optimal solution you would want to use a 32-bit cache on 32-bit architectures.
The bit stream is not necessary for understanding the FLAC format. It's just a tool to help with
reading data that is not byte-clean. You do not need to understand the implementation details of
the bit stream in order to understand the FLAC format so you can skip this section if you're just
wanting to learn about the FLAC format.
**************************************************************************************************/
typedef struct
{
const unsigned char* pData8;
size_t dataSize;
size_t bytesRead;
/*
Indicates whether or not the bistream is in a reset state. This is required in case rflac_bs_get_bytes_consumed() is
called on a freshly initialized bitstream. It will use consumedBits to determine how many bytes in the L1 cache has
been consumed, however this will be initialized to RFLAC_CACHE_L1_SIZE_BYTES() which would result in an incorrect result.
*/
rflac_bool8 isReset;
/*
The number of unaligned bytes in the L2 cache. This will always be 0 until the end of the stream is hit. At the end of the
stream there will be a number of bytes that don't cleanly fit in an L1 cache line, so we use this variable to know whether
or not the bistreamer needs to run on a slower path to read those last bytes. This will never be more than sizeof(rflac_uint64).
*/
size_t unalignedByteCount;
/* The content of the unaligned bytes. */
rflac_uint64 unalignedCache;
/* The index of the next valid cache line in the "L2" cache. */
rflac_uint32 nextL2Line;
/* The number of bits that have been consumed by the cache. This is used to determine how many valid bits are remaining. */
rflac_uint32 consumedBits;
/*
The cached data which was most recently read from the client. There are two levels of cache. Data flows as such:
Client -> L2 -> L1. The L2 -> L1 movement is aligned and runs on a fast path in just a few instructions.
*/
rflac_uint64 cacheL2[4096/sizeof(rflac_uint64)];
rflac_uint64 cache;
} rflac_bs;
#define RFLAC_CACHE_L1_SIZE_BYTES(bs) (sizeof((bs)->cache))
#define RFLAC_CACHE_L1_SIZE_BITS(bs) (sizeof((bs)->cache)*8)
#define RFLAC_CACHE_L1_BITS_REMAINING(bs) (RFLAC_CACHE_L1_SIZE_BITS(bs) - (bs)->consumedBits)
#define RFLAC_CACHE_L1_SELECTION_MASK(_bitCount) (~((~(rflac_uint64)0) >> (_bitCount)))
#define RFLAC_CACHE_L1_SELECTION_SHIFT(bs, _bitCount) (RFLAC_CACHE_L1_SIZE_BITS(bs) - (_bitCount))
#define RFLAC_CACHE_L1_SELECT(bs, _bitCount) (((bs)->cache) & RFLAC_CACHE_L1_SELECTION_MASK(_bitCount))
#define RFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, _bitCount) (RFLAC_CACHE_L1_SELECT((bs), (_bitCount)) >> RFLAC_CACHE_L1_SELECTION_SHIFT((bs), (_bitCount)))
#define RFLAC_CACHE_L1_SELECT_AND_SHIFT_SAFE(bs, _bitCount)(RFLAC_CACHE_L1_SELECT((bs), (_bitCount)) >> (RFLAC_CACHE_L1_SELECTION_SHIFT((bs), (_bitCount)) & (RFLAC_CACHE_L1_SIZE_BITS(bs)-1)))
#define RFLAC_CACHE_L2_SIZE_BYTES(bs) (sizeof((bs)->cacheL2))
#define RFLAC_CACHE_L2_LINE_COUNT(bs) (RFLAC_CACHE_L2_SIZE_BYTES(bs) / sizeof((bs)->cacheL2[0]))
#define RFLAC_CACHE_L2_LINES_REMAINING(bs) (RFLAC_CACHE_L2_LINE_COUNT(bs) - (bs)->nextL2Line)
static rflac_result rflac_bs_init(const void* pData, size_t dataSize, rflac_bs* bs)
{
assert(bs != NULL);
assert(pData != NULL);
memset(bs, 0, sizeof(*bs));
bs->pData8 = (const unsigned char*)pData;
bs->dataSize = dataSize;
bs->bytesRead = 0;
bs->isReset = RFLAC_TRUE;
bs->nextL2Line = RFLAC_CACHE_L2_LINE_COUNT(bs); /* <-- This clears the L2 cache. */
bs->consumedBits = RFLAC_CACHE_L1_SIZE_BITS(bs); /* <-- This clears the L1 cache. */
return RFLAC_SUCCESS;
}
static rflac_result rflac_bs_reload_l1_cache_from_l2(rflac_bs* bs)
{
size_t bytesRead;
size_t alignedL1LineCount;
/* TODO: This can be optimized and simpified. There's no need to read from the client because the data itself *is* the L2 cache. */
/* Fast path. Try loading straight from L2. */
if (bs->nextL2Line < RFLAC_CACHE_L2_LINE_COUNT(bs)) {
bs->cache = bs->cacheL2[bs->nextL2Line++];
return RFLAC_SUCCESS;
}
/*
If we get here it means we've run out of data in the L2 cache. We'll need to fetch more from the client, if there's
any left.
*/
if (bs->unalignedByteCount > 0) {
return RFLAC_NOT_ENOUGH_DATA; /* If we have any unaligned bytes it means there's no more aligned bytes left in the client. */
}
{
size_t bytesToRead = RFLAC_CACHE_L2_SIZE_BYTES(bs);
if (bytesToRead > (bs->dataSize - bs->bytesRead)) {
bytesToRead = (bs->dataSize - bs->bytesRead);
}
memcpy(bs->cacheL2, bs->pData8 + bs->bytesRead, bytesToRead);
bytesRead = bytesToRead;
bs->bytesRead += bytesToRead;
}
bs->nextL2Line = 0;
if (bytesRead == RFLAC_CACHE_L2_SIZE_BYTES(bs)) {
bs->cache = bs->cacheL2[bs->nextL2Line++];
return RFLAC_SUCCESS;
}
/*
If we get here it means we were unable to retrieve enough data to fill the entire L2 cache. It probably
means we've just reached the end of the file. We need to move the valid data down to the end of the buffer
and adjust the index of the next line accordingly. Also keep in mind that the L2 cache must be aligned to
the size of the L1 so we'll need to seek backwards by any misaligned bytes.
*/
alignedL1LineCount = bytesRead / RFLAC_CACHE_L1_SIZE_BYTES(bs);
/* We need to keep track of any unaligned bytes for later use. */
bs->unalignedByteCount = bytesRead - (alignedL1LineCount * RFLAC_CACHE_L1_SIZE_BYTES(bs));
if (bs->unalignedByteCount > 0) {
bs->unalignedCache = bs->cacheL2[alignedL1LineCount];
}
if (alignedL1LineCount > 0) {
size_t offset = RFLAC_CACHE_L2_LINE_COUNT(bs) - alignedL1LineCount;
size_t i;
for (i = alignedL1LineCount; i > 0; --i) {
bs->cacheL2[i-1 + offset] = bs->cacheL2[i-1];
}
bs->nextL2Line = (rflac_uint32)offset;
bs->cache = bs->cacheL2[bs->nextL2Line++];
return RFLAC_SUCCESS;
} else {
/* If we get into this branch it means we weren't able to load any L1-aligned data. */
bs->nextL2Line = RFLAC_CACHE_L2_LINE_COUNT(bs);
return RFLAC_NOT_ENOUGH_DATA;
}
}
static rflac_result rflac_bs_reload_cache(rflac_bs* bs)
{
size_t bytesRead;
/* If we're reloading the cache it means the bitstream is no longer in a reset state. This state is required for rflac_bs_get_bytes_consumed(). Would be nice if we could remove this. */
bs->isReset = RFLAC_FALSE;
/* Fast path. Try just moving the next value in the L2 cache to the L1 cache. */
if (rflac_bs_reload_l1_cache_from_l2(bs) == RFLAC_SUCCESS) {
bs->cache = rflac_be2host_64(bs->cache);
bs->consumedBits = 0;
return RFLAC_SUCCESS;
}
/* Slow path. */
/*
If we get here it means we have failed to load the L1 cache from the L2. Likely we've just reached the end of the stream and the last
few bytes did not meet the alignment requirements for the L2 cache. In this case we need to fall back to a slower path and read the
data from the unaligned cache.
*/
bytesRead = bs->unalignedByteCount;
if (bytesRead == 0) {
bs->consumedBits = RFLAC_CACHE_L1_SIZE_BITS(bs); /* <-- The stream has been exhausted, so marked the bits as consumed. */
return RFLAC_NOT_ENOUGH_DATA;
}
assert(bytesRead < RFLAC_CACHE_L1_SIZE_BYTES(bs));
bs->consumedBits = (rflac_uint32)(RFLAC_CACHE_L1_SIZE_BYTES(bs) - bytesRead) * 8;
bs->cache = rflac_be2host_64(bs->unalignedCache);
bs->cache &= RFLAC_CACHE_L1_SELECTION_MASK(RFLAC_CACHE_L1_BITS_REMAINING(bs)); /* <-- Make sure the consumed bits are always set to zero. Other parts of the library depend on this property. */
bs->unalignedByteCount = 0; /* <-- At this point the unaligned bytes have been moved into the cache and we thus have no more unaligned bytes. */
return RFLAC_SUCCESS;
}
static size_t rflac_bs_get_bytes_consumed(rflac_bs* bs)
{
size_t bytesConsumed;
/* This is total bytes taken from the buffer, minus whatever is in the L2 cache, minus the L1 cache, plus any bits that have been consumed in the L1 cache. */
bytesConsumed = bs->bytesRead - (RFLAC_CACHE_L2_LINES_REMAINING(bs) * sizeof(bs->cacheL2[0])) - bs->unalignedByteCount;
if (!bs->isReset) {
bytesConsumed -= sizeof(bs->cache);
bytesConsumed += bs->consumedBits >> 3;
}
return bytesConsumed;
}
static rflac_result rflac_bs_read_uint32(rflac_bs* bs, unsigned int bitCount, rflac_uint32* pResultOut)
{
rflac_result result;
assert(bs != NULL);
assert(pResultOut != NULL);
assert(bitCount > 0);
assert(bitCount <= 32);
if (bs->consumedBits == RFLAC_CACHE_L1_SIZE_BITS(bs)) {
result = rflac_bs_reload_cache(bs);
if (result != RFLAC_SUCCESS) {
return result;
}
}
if (bitCount <= RFLAC_CACHE_L1_BITS_REMAINING(bs)) {
*pResultOut = (rflac_uint32)RFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCount);
bs->consumedBits += bitCount;
bs->cache <<= bitCount;
return RFLAC_SUCCESS;
} else {
/* It straddles the cached data. It will never cover more than the next chunk. We just read the number in two parts and combine them. */
rflac_uint32 bitCountHi = RFLAC_CACHE_L1_BITS_REMAINING(bs);
rflac_uint32 bitCountLo = bitCount - bitCountHi;
rflac_uint32 resultHi;
assert(bitCountHi > 0);
assert(bitCountHi < 32);
resultHi = (rflac_uint32)RFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCountHi);
result = rflac_bs_reload_cache(bs);
if (result != RFLAC_SUCCESS) {
return result;
}
if (bitCountLo > RFLAC_CACHE_L1_BITS_REMAINING(bs)) {
/* This happens when we get to end of stream */
return RFLAC_NOT_ENOUGH_DATA;
}
*pResultOut = (resultHi << bitCountLo) | (rflac_uint32)RFLAC_CACHE_L1_SELECT_AND_SHIFT(bs, bitCountLo);
bs->consumedBits += bitCountLo;
bs->cache <<= bitCountLo;
return RFLAC_SUCCESS;
}
}
static rflac_result rflac_bs_read_int32(rflac_bs* bs, unsigned int bitCount, rflac_int32* pResult)
{
rflac_uint32 x;
rflac_result result;
assert(bs != NULL);
assert(pResult != NULL);
assert(bitCount > 0);
assert(bitCount <= 32);
result = rflac_bs_read_uint32(bs, bitCount, &x);
if (result != RFLAC_SUCCESS) {
return result;
}
/* Do not attempt to shift by 32 as it's undefined. */
if (bitCount < 32) {
rflac_uint32 signbit;
signbit = ((x >> (bitCount-1)) & 0x01);
x |= (~signbit + 1) << bitCount;
}
*pResult = (rflac_int32)x;
return RFLAC_SUCCESS;
}
static rflac_result drflac_bs_read_uint64(rflac_bs* bs, unsigned int bitCount, rflac_uint64* pResultOut)
{
rflac_uint32 xHi;
rflac_uint32 xLo;
rflac_result result;
assert(bitCount <= 64);
assert(bitCount > 32);
result = rflac_bs_read_uint32(bs, bitCount - 32, &xHi);
if (result != RFLAC_SUCCESS) {
return result;
}
result = rflac_bs_read_uint32(bs, 32, &xLo);
if (result != RFLAC_SUCCESS) {
return result;
}
*pResultOut = (((rflac_uint64)xHi) << 32) | ((rflac_uint64)xLo);
return RFLAC_SUCCESS;
}
static rflac_result rflac_bs_read_uint16(rflac_bs* bs, unsigned int bitCount, rflac_uint16* pResult)
{
rflac_uint32 x;
rflac_result result;
assert(bs != NULL);
assert(pResult != NULL);
assert(bitCount > 0);
assert(bitCount <= 16);
result = rflac_bs_read_uint32(bs, bitCount, &x);
if (result != RFLAC_SUCCESS) {
return result;
}
*pResult = (rflac_uint16)x;
return RFLAC_SUCCESS;
}
static rflac_result rflac_bs_read_uint8(rflac_bs* bs, unsigned int bitCount, rflac_uint8* pResult)
{
rflac_uint32 x;
rflac_result result;
assert(bs != NULL);
assert(pResult != NULL);
assert(bitCount > 0);
assert(bitCount <= 8);
result = rflac_bs_read_uint32(bs, bitCount, &x);
if (result != RFLAC_SUCCESS) {
return result;
}
*pResult = (rflac_uint8)x;
return RFLAC_SUCCESS;
}
static rflac_result rflac_bs_read_int8(rflac_bs* bs, unsigned int bitCount, rflac_int8* pResult)
{
rflac_int32 x;
rflac_result result;
assert(bs != NULL);
assert(pResult != NULL);
assert(bitCount > 0);
assert(bitCount <= 8);
result = rflac_bs_read_int32(bs, bitCount, &x);
if (result != RFLAC_SUCCESS) {
return result;
}
*pResult = (rflac_int8)x;
return RFLAC_SUCCESS;
}
static rflac_result rflac_bs_seek_bits(rflac_bs* bs, size_t bitsToSeek)
{
if (bitsToSeek <= RFLAC_CACHE_L1_BITS_REMAINING(bs)) {
bs->consumedBits += (rflac_uint32)bitsToSeek;
bs->cache <<= bitsToSeek;
return RFLAC_SUCCESS;
} else {
/* It straddles the cached data. This function isn't called too frequently so I'm favouring simplicity here. */
bitsToSeek -= RFLAC_CACHE_L1_BITS_REMAINING(bs);
bs->consumedBits += RFLAC_CACHE_L1_BITS_REMAINING(bs);
bs->cache = 0;
while (bitsToSeek >= RFLAC_CACHE_L1_SIZE_BITS(bs)) {
rflac_uint64 bin;
rflac_result result;
result = drflac_bs_read_uint64(bs, RFLAC_CACHE_L1_SIZE_BITS(bs), &bin);
if (result != RFLAC_SUCCESS) {
return result;
}
bitsToSeek -= RFLAC_CACHE_L1_SIZE_BITS(bs);
}
/* Whole leftover bytes. */
while (bitsToSeek >= 8) {
rflac_uint8 bin;
rflac_result result;
result = rflac_bs_read_uint8(bs, 8, &bin);
if (result != RFLAC_SUCCESS) {
return result;
}
bitsToSeek -= 8;
}
/* Leftover bits. */
if (bitsToSeek > 0) {
rflac_uint8 bin;
rflac_result result;
result = rflac_bs_read_uint8(bs, (rflac_uint32)bitsToSeek, &bin);
if (result != RFLAC_SUCCESS) {
return result;
}
bitsToSeek = 0; /* <-- Necessary for the assert below. */
}
assert(bitsToSeek == 0);
return RFLAC_SUCCESS;
}
}
static rflac_result rflac_bs_seek_to_next_byte(rflac_bs* bs)
{
assert(bs != NULL);
return rflac_bs_seek_bits(bs, RFLAC_CACHE_L1_BITS_REMAINING(bs) & 7);
}
static rflac_result rflac_bs_read_unary(rflac_bs* bs, rflac_uint32* pValue)
{
rflac_uint32 zeroCounter = 0;
rflac_uint32 setBitOffsetPlus1;
rflac_result result;
while (bs->cache == 0) {
zeroCounter += (rflac_uint32)RFLAC_CACHE_L1_BITS_REMAINING(bs);
result = rflac_bs_reload_cache(bs);
if (result != RFLAC_SUCCESS) {
return result;
}
}
if (bs->cache == 1) {
/* Not catching this would lead to undefined behaviour: a shift of a 32-bit number by 32 or more is undefined */
*pValue = zeroCounter + (rflac_uint32)RFLAC_CACHE_L1_BITS_REMAINING(bs) - 1;
result = rflac_bs_reload_cache(bs);
if (result != RFLAC_SUCCESS) {
return result;
}
return RFLAC_SUCCESS;
}
setBitOffsetPlus1 = rflac_clz_64(bs->cache);
setBitOffsetPlus1 += 1;
if (setBitOffsetPlus1 > RFLAC_CACHE_L1_BITS_REMAINING(bs)) {
/* This happens when we get to end of stream */
return RFLAC_NOT_ENOUGH_DATA;
}
bs->consumedBits += setBitOffsetPlus1;
bs->cache <<= setBitOffsetPlus1;
*pValue = zeroCounter + setBitOffsetPlus1 - 1;
return RFLAC_SUCCESS;
}
typedef enum
{
RFLAC_METADATA_BLOCK_TYPE_STREAMINFO = 0,
RFLAC_METADATA_BLOCK_TYPE_PADDING = 1,
RFLAC_METADATA_BLOCK_TYPE_APPLICATION = 2,
RFLAC_METADATA_BLOCK_TYPE_SEEKTABLE = 3,
RFLAC_METADATA_BLOCK_TYPE_VORBIS_COMMENT = 4,
RFLAC_METADATA_BLOCK_TYPE_CUESHEET = 5,
RFLAC_METADATA_BLOCK_TYPE_PICTURE = 6
} rflac_metadata_block_type;
typedef struct
{
rflac_bool8 last;
rflac_uint8 type;
size_t size;
const void* data;
} rflac_metadata_block;
static rflac_result rflac_read_metadata_block(const void* pData, size_t dataSize, size_t* pBytesConsumed, rflac_metadata_block* pMetadata)
{
size_t totalBytesConsumed = 0;
const unsigned char* pData8 = (const unsigned char*)pData;
assert(pData8 != NULL);
/*
Section 9.1
Each metadata block starts with a 4 byte header. The first bit in
this header flags whether a metadata block is the last one, it is a 0
when other metadata blocks follow, otherwise it is a 1. The 7
remaining bits of the first header byte contain the type of the
metadata block as an unsigned number between 0 and 126 according to
the following table. A value of 127 (i.e. 0b1111111) is invalid.
The three bytes that follow code for the size of the metadata block
in bytes excluding the 4 header bytes as an unsigned number coded
big-endian.
*/
if (dataSize < 4) {
return RFLAC_NOT_ENOUGH_DATA;
}
pMetadata->last = (pData8[0] & 0x80) != 0;
pMetadata->type = (pData8[0] & 0x7F);
pMetadata->size = (pData8[1] << 16) | (pData8[2] << 8) | (pData8[3] << 0);
pData8 += 4;
totalBytesConsumed += 4;
/*
Section 9.1
A value of 127 (i.e. 0b1111111) is invalid.
*/
if (pMetadata->type == 127) {
return RFLAC_INVALID_FILE;
}
/* The data should be sitting on the data, but make sure there's enough data. */
if ((dataSize - totalBytesConsumed) < pMetadata->size) {
return RFLAC_NOT_ENOUGH_DATA;
}
pMetadata->data = (const void*)pData8;
pData8 += pMetadata->size;
totalBytesConsumed += pMetadata->size;
/* Getting here means we successfully read the metadata. */
*pBytesConsumed = totalBytesConsumed;
return RFLAC_SUCCESS;
}
/*
9.2. Streaminfo
*/
#define RFLAC_METADATA_BLOCK_STREAMINFO_SIZE 34
typedef struct
{
rflac_uint16 minBlockSize;
rflac_uint16 maxBlockSize;
rflac_uint32 minFrameSizeInBytes;
rflac_uint32 maxFrameSizeInBytes;
rflac_uint32 sampleRate;
rflac_uint8 channels;
rflac_uint8 bitsPerSample;
rflac_uint64 totalPCMFrameCount;
rflac_uint8 md5[16];
} rflac_metadata_block_streaminfo;
static rflac_result rflac_read_metadata_block_streaminfo(rflac_metadata_block* pMetadata, rflac_metadata_block_streaminfo* pStreaminfo)
{
const unsigned char* pData8 = (const unsigned char*)pMetadata->data;
assert(pMetadata != NULL);
assert(pStreaminfo != NULL);
if (pMetadata->size < RFLAC_METADATA_BLOCK_STREAMINFO_SIZE) {
return RFLAC_INVALID_FILE; /* The STREAMINFO block must be at least this size. If not, it's an invalid file. RFLAC_NOT_ENOUGH_DATA will have been returned at an earlier stage if relevant. */
}
pStreaminfo->minBlockSize = (pData8[ 0] << 8) | (pData8[ 1] << 0);
pStreaminfo->maxBlockSize = (pData8[ 2] << 8) | (pData8[ 3] << 0);
pStreaminfo->minFrameSizeInBytes = (pData8[ 4] << 16) | (pData8[ 5] << 8) | (pData8[ 6] << 0);
pStreaminfo->maxFrameSizeInBytes = (pData8[ 7] << 16) | (pData8[ 8] << 8) | (pData8[ 9] << 0);
pStreaminfo->sampleRate = ((pData8[10] << 16) | (pData8[11] << 8) | (pData8[12] << 0)) >> 4;
pStreaminfo->channels = ((pData8[12] & 0x0E) >> 1) + 1; /* +1 because it's stored as minus 1. */
pStreaminfo->bitsPerSample = (((pData8[12] & 0x01) << 4) | ((pData8[13] & 0xF0) >> 4)) + 1; /* +1 because it's stored as minus 1. */
pStreaminfo->totalPCMFrameCount = (((rflac_uint64)pData8[13] & 0x0F) << 32) | ((pData8[14] << 24) | (pData8[15] << 16) | (pData8[16] << 8) | (pData8[17] << 0));
return RFLAC_SUCCESS;
}
rflac_result rflac_init(rflac* pFlac, const void* pData, size_t dataSize, size_t* pBytesConsumed)
{
size_t totalBytesConsumed = 0;
const unsigned char* pData8 = (const unsigned char*)pData;
if (pFlac == NULL) {
return RFLAC_INVALID_ARGS;
}
memset(pFlac, 0, sizeof(*pFlac));
if (pBytesConsumed == NULL) {
return RFLAC_INVALID_ARGS; /* You must know how many bytes were consumed for correct usage of this decoder. */
}
*pBytesConsumed = 0;
if (pData == NULL) {
return RFLAC_INVALID_ARGS;
}
/*
Section 7.
A FLAC bitstream consists of the fLaC (i.e. 0x664C6143) marker at the beginning of the stream
*/
if ((dataSize - totalBytesConsumed) < 4) {
return RFLAC_NOT_ENOUGH_DATA;
}
if (pData8[0] != 'f' && pData8[1] != 'L' && pData8[2] != 'a' && pData8[3] != 'C') {
return RFLAC_INVALID_FILE;
}
pData8 += 4;
totalBytesConsumed += 4;
/*
Section 7. (Cont. from above)
followed by a mandatory metadata block (called the STREAMINFO block), any number of other metadata blocks,
then the audio frames.
*/
{
rflac_result result;
rflac_metadata_block metadata;
rflac_metadata_block_streaminfo streaminfo;
size_t bytesConsumed;
/* STREAMINFO */
result = rflac_read_metadata_block(pData8, dataSize - totalBytesConsumed, &bytesConsumed, &metadata);
if (result != RFLAC_SUCCESS) {
return result;
}
pData8 += bytesConsumed;
totalBytesConsumed += bytesConsumed;
if (metadata.type != RFLAC_METADATA_BLOCK_TYPE_STREAMINFO) {
return RFLAC_INVALID_FILE;
}
/* Parse the STREAMINFO block so we can get some basic information and do some validation. */
result = rflac_read_metadata_block_streaminfo(&metadata, &streaminfo);
if (result != RFLAC_SUCCESS) {
return RFLAC_INVALID_FILE;
}
/*
Section 9.2
FLAC specifies a minimum block size of 16 and a maximum block size of
65535, meaning the bit patterns corresponding to the numbers 0-15 in
the minimum block size and maximum block size fields are invalid.
*/
if (streaminfo.minBlockSize < RFLAC_MIN_BLOCK_SIZE_IN_SAMPLES) {
return RFLAC_INVALID_FILE;
}
if (streaminfo.channels < RFLAC_MIN_CHANNELS || streaminfo.channels > RFLAC_MAX_CHANNELS) {
return RFLAC_INVALID_FILE;
}
if (streaminfo.sampleRate < RFLAC_MIN_SAMPLE_RATE || streaminfo.channels > RFLAC_MAX_SAMPLE_RATE) {
return RFLAC_INVALID_FILE;
}
if (streaminfo.bitsPerSample < RFLAC_MIN_BITS_PER_SAMPLE || streaminfo.bitsPerSample > RFLAC_MAX_BITS_PER_SAMPLE) {
return RFLAC_INVALID_FILE;
}
pFlac->bitsPerSample = streaminfo.bitsPerSample;
pFlac->sampleRate = streaminfo.sampleRate;
pFlac->channels = streaminfo.channels;
pFlac->totalPCMFrameCount = streaminfo.totalPCMFrameCount;
pFlac->minBlockSize = streaminfo.minBlockSize;
pFlac->maxBlockSize = streaminfo.maxBlockSize;
/* Remaining metadata blocks. */
for (;;) {
result = rflac_read_metadata_block(pData8, dataSize - totalBytesConsumed, &bytesConsumed, &metadata);
if (result != RFLAC_SUCCESS) {
return result;
}
pData8 += bytesConsumed;
totalBytesConsumed += bytesConsumed;
/*
Section 9.2
There MUST be no more than one streaminfo
metadata block per FLAC stream.
*/
if (metadata.type == RFLAC_METADATA_BLOCK_TYPE_STREAMINFO) {
return RFLAC_INVALID_FILE;
}
if (metadata.last == 1) {
break;
}
}
}
/* Getting here means we're done with initialization. The next part should be audio data. */
*pBytesConsumed = totalBytesConsumed;
return RFLAC_SUCCESS;
}
typedef enum
{
RFLAC_STEREO_DECORRELATION_MODE_INDEPENDANT,
RFLAC_STEREO_DECORRELATION_MODE_LEFT_SIDE,
RFLAC_STEREO_DECORRELATION_MODE_SIDE_RIGHT,
RFLAC_STEREO_DECORRELATION_MODE_MID_SIDE
} rflac_stereo_decorrelation_mode;
typedef struct
{
rflac_uint16 syncCode;
rflac_uint8 blockingStrategy;
rflac_uint64 pcmFrameIndex; /* The index of the first PCM frame in the stream this FLAC frame encodes. */
rflac_uint16 pcmFrameCount;
rflac_uint32 sampleRate;
rflac_uint8 channels;
rflac_stereo_decorrelation_mode stereoDecorrelationMode;
rflac_uint8 bitsPerSample;
rflac_uint32 crc8;
} rflac_frame_header;
static rflac_result rflac_decode_frame_header(rflac* pFlac, const void* pData, size_t dataSize, size_t* pBytesConsumed, rflac_frame_header* pFrameHeader)
{
size_t totalBytesConsumed = 0;
const unsigned char* pData8 = (const unsigned char*)pData;
rflac_uint8 blocksizeCode;
rflac_uint8 sampleRateCode;
rflac_uint8 channelCode;
rflac_uint8 bitDepthCode;
rflac_uint32 codedNumber; /* Either a PCM frame number, or a FLAC frame number, depending on the blocking strategy. */
rflac_uint32 codedNumberByteCount;
assert(pFlac != NULL);
assert(pData != NULL);
assert(pBytesConsumed != NULL);
assert(pFrameHeader != NULL);
*pBytesConsumed = 0;
/*
Section 10.1
Each frame MUST start on a byte boundary and starts with the 15-bit
frame sync code 0b111111111111100. Following the sync code is the
blocking strategy bit, which MUST NOT change during the audio stream.
*/
if ((dataSize - totalBytesConsumed) < 4) {
return RFLAC_NOT_ENOUGH_DATA;
}
pFrameHeader->syncCode = ((pData8[0] << 8) | (pData8[1] << 0)) & 0xFFFE;
pFrameHeader->blockingStrategy = pData8[1] & 0x01;
if (pFrameHeader->syncCode != 0xFFF8) {
return RFLAC_INVALID_FILE;
}
/*
Section 10.1.1
Following the frame sync code and blocksize strategy bit are 4 bits
referred to as the blocksize bits.
*/
blocksizeCode = (pData8[2] & 0xF0) >> 4;
if (blocksizeCode == 0) {
return RFLAC_INVALID_FILE; /* Reserved. */
}
if (blocksizeCode == 1) {
pFrameHeader->pcmFrameCount = 192;
} else if (blocksizeCode >= 2 && blocksizeCode <= 5) {
pFrameHeader->pcmFrameCount = 144 << blocksizeCode;
} else if (blocksizeCode == 6 || blocksizeCode == 7) {
// Will be read later.
} else if (blocksizeCode >= 8 && blocksizeCode <= 15) {
pFrameHeader->pcmFrameCount = 1 << blocksizeCode;
} else {
return RFLAC_INVALID_FILE; /* Should never hit this. */
}
/*
Section 10.1.2
The next 4 bits, referred to as the sample rate bits, contain the
sample rate according to the following table.
*/
sampleRateCode = (pData8[2] & 0x0F);
if (sampleRateCode == 0) {
pFrameHeader->sampleRate = pFlac->sampleRate;
} else if (sampleRateCode >= 1 && sampleRateCode <= 11) {
rflac_uint32 sampleRateLookup[] = {
0, /* Unused. */
88200,
176400,
192000,
8000,
16000,
22050,
24000,
32000,
44100,
48000,
96000
};
pFrameHeader->sampleRate = sampleRateLookup[sampleRateCode];
} else if (sampleRateCode >= 12 && sampleRateCode <= 14) {
// Will be read later.
} else {
return RFLAC_INVALID_FILE;
}
/*
Section 10.1.3
The next 4 bits (the first 4 bits of the fourth byte of each frame),
referred to as the channels bits, code for both the number of
channels as well as any stereo decorrelation used
*/
channelCode = (pData8[3] & 0xF0) >> 4;
pFrameHeader->stereoDecorrelationMode = RFLAC_STEREO_DECORRELATION_MODE_INDEPENDANT;
if (channelCode >= 0 && channelCode <= 7) {
pFrameHeader->channels = channelCode + 1;
} else if (channelCode == 8) {
pFrameHeader->channels = 2;
pFrameHeader->stereoDecorrelationMode = RFLAC_STEREO_DECORRELATION_MODE_LEFT_SIDE;
} else if (channelCode == 9) {
pFrameHeader->channels = 2;
pFrameHeader->stereoDecorrelationMode = RFLAC_STEREO_DECORRELATION_MODE_SIDE_RIGHT;
} else if (channelCode == 10) {
pFrameHeader->channels = 2;
pFrameHeader->stereoDecorrelationMode = RFLAC_STEREO_DECORRELATION_MODE_MID_SIDE;
} else {
return RFLAC_INVALID_FILE;
}
/*
Section 10.1.4
The next 3 bits code for the bit depth of the samples in the subframe
*/
bitDepthCode = (pData8[3] & 0x0E) >> 1;
if (bitDepthCode == 0) {
pFrameHeader->bitsPerSample = pFlac->bitsPerSample;
} else if (bitDepthCode == 1) {
pFrameHeader->bitsPerSample = 8;
} else if (bitDepthCode == 2) {
pFrameHeader->bitsPerSample = 12;
} else if (bitDepthCode == 3) {
return RFLAC_INVALID_FILE;
} else if (bitDepthCode == 4) {
pFrameHeader->bitsPerSample = 16;
} else if (bitDepthCode == 5) {
pFrameHeader->bitsPerSample = 20;
} else if (bitDepthCode == 6) {
pFrameHeader->bitsPerSample = 24;
} else if (bitDepthCode == 7) {
pFrameHeader->bitsPerSample = 32;
}
/*
Section 10.1.4
The next bit is reserved and MUST be zero.
*/
if ((pData8[3] & 0x01) != 0) {
return RFLAC_INVALID_FILE;
}
pData8 += 4;
totalBytesConsumed += 4;
/*
Section 10.1.5
Following the reserved bit (starting at the fifth byte of the frame)
is either a sample or a frame number, which will be referred to as
the coded number. When dealing with variable blocksize streams, the
sample number of the first sample in the frame is encoded. When the