-
Notifications
You must be signed in to change notification settings - Fork 0
/
tflac.h
5095 lines (4186 loc) · 167 KB
/
tflac.h
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
/* SPDX-License-Identifier: 0BSD */
#ifndef TFLAC_HEADER_GUARD
#define TFLAC_HEADER_GUARD
/*
Copyright (C) 2024 John Regan <[email protected]>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
/*
TFLAC
=====
A single-header FLAC encoder that doesn't use the standard library
and doesn't allocate memory (it makes you do it instead!).
Building
========
In one C file, define TFLAC_IMPLEMENTATION before including this header, like:
#define TFLAC_IMPLEMENTATION
#include "tflac.h"
You'll need to allocate a tflac, initialize it with tflac_init,
then set the blocksize, bitdepth, channels, and samplerate fields.
tflac t;
tflac_init(&t);
t.channels = 2;
t.blocksize = 1152;
t.bitdepth = 16;
t.samplerate = 44100;
You'll need to allocate a block of memory for tflac to use for residuals calcuation.
You can get the needed memory size with either the TFLAC_SIZE_MEMORY macro or tflac_size_memory()
function. Both versions just require a blocksize.
Then call tflac_validate with your memory block and the size, it will validate that all
your fields make sense and record pointers into the memory block.
tflac_u32 mem_size = tflac_size_memory(1152);
void* block = malloc(mem_size);
tflac_validate(&t, block, mem_size);
You'll also need to allocate a block of memory for storing your encoded
audio. You can find this size with wither the TFLAC_SIZE_FRAME macro
or tflac_size_frame() function. Both versions need the blocksize,
channels, and bitdepth, and return the maximum amount of bytes required
to hold a frame of audio.
tflac_u32 buffer_len = tflac_size_frame(1152, 2, 16);
void* buffer = malloc(buffer_len);
Finally to encode audio, you'll call the appropriate tflac_encode function,
depending on how your source data is laid out:
- tflac_s32** (planar) => tflac_encode_s32p
- tflac_s32* (interleaved) => tflac_encode_s32i
- tflac_s16** (planar) => tflac_encode_s16p
- tflac_s16* (interleaved) => tflac_encode_s16i
You'll also provide the current block size (all blocks except the last need to
use the same block size), a buffer to encode data to, and your buffer's length.
tflac_s32* samples[2];
tflac_u32 used = 0;
samples[0] = { 0, 1, 2, 3, 4, 5, 6, 7 };
samples[1] = { 8, 9, 10, 11, 12, 13, 14, 15 };
tflac_encode_int32p(&t, 8, samples, buffer, buffer_len, &used);
You can now write out used bytes of buffer.
The library also has a convenience function for creating a STREAMINFO block:
tflac_streaminfo(&t, 1, buffer, bufferlen, &used);
The first argument is whether to set the "is-last-block" flag.
Other metadata blocks are out-of-scope, as is writing the "fLaC" stream marker.
*/
#ifndef TFLAC_CONST
#if defined(__GNUC__) && __GNUC__ > 3 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5)
#define TFLAC_CONST __attribute__((__const__))
#else
#define TFLAC_CONST
#endif
#endif
#ifndef TFLAC_PURE
#if defined(__GNUC__) && __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
#define TFLAC_PURE __attribute__((__pure__))
#else
#define TFLAC_PURE
#endif
#endif
#ifndef TFLAC_RESTRICT
#if __STDC_VERSION__ >= 199901L
#define TFLAC_RESTRICT restrict
#elif defined(__GNUC__) && __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
#define TFLAC_RESTRICT __restrict
#elif defined(__GNUC__) && __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1)
#define TFLAC_RESTRICT __restrict
#elif defined(_MSC_VER) && _MSC_VER >= 1400
#define TFLAC_RESTRICT __restrict
#else
#define TFLAC_RESTRICT
#endif
#endif
#ifndef TFLAC_PUBLIC
#define TFLAC_PUBLIC
#endif
#ifndef TFLAC_PRIVATE
#define TFLAC_PRIVATE static
#endif
#define TFLAC_SIZE_STREAMINFO 38UL
#define TFLAC_SIZE_FRAME(blocksize, channels, bitdepth) \
(UINT32_C(18) + UINT32_C(8) + UINT32_C(channels) + \
(( \
(UINT32_C(blocksize) * UINT32_C(bitdepth) * (UINT32_C(channels) * (UINT32_C(channels) != (UINT32_C(2))))) + \
(UINT32_C(blocksize) * UINT32_C(bitdepth) * (UINT32_C(channels) == UINT32_C(2))) + \
(UINT32_C(blocksize) * (UINT32_C(bitdepth) + (UINT32_C(bitdepth) != UINT32_C(32))) * (UINT32_C(channels) == UINT32_C(2))) + \
7 \
) / 8) )
#define TFLAC_SIZE_MEMORY(blocksize) (15UL + (5UL * ((15UL + (UINT32_C(blocksize) * 4UL)) & UINT32_C(0xFFFFFFF0))))
#ifdef __cplusplus
extern "C" {
#endif
#include <stddef.h>
#include <limits.h>
#if (__STDC_VERSION__ >= 199901L) || defined(__GNUC__)
#include <stdint.h>
typedef uint8_t tflac_u8;
typedef int8_t tflac_s8;
typedef uint16_t tflac_u16;
typedef int16_t tflac_s16;
typedef uint32_t tflac_u32;
typedef int32_t tflac_s32;
typedef uintptr_t tflac_uptr;
#ifndef TFLAC_32BIT_ONLY
typedef uint64_t tflac_u64;
typedef int64_t tflac_s64;
#endif
#else
#ifndef UINT8_MAX
#define UINT8_MAX 0xFF
#endif
#ifndef INT8_MAX
#define INT8_MAX 0x7F
#endif
#ifndef UINT16_MAX
#define UINT16_MAX 0xFFFF
#endif
#ifndef INT16_MAX
#define INT16_MAX 0x7FFF
#endif
#ifndef UINT32_MAX
#define UINT32_MAX 0xFFFFFFFFUL
#endif
#ifndef INT32_MAX
#define INT32_MAX 0x7FFFFFFFL
#endif
#if (UINT8_MAX == UCHAR_MAX)
typedef unsigned char tflac_u8;
#ifndef UINT8_C
#define UINT8_C(x) ((tflac_u8)x)
#endif
#else
#error "not supported"
#endif
#if (INT8_MAX == SCHAR_MAX)
typedef signed char tflac_s8;
#ifndef INT8_C
#define INT8_C(x) ((tflac_s8)x)
#endif
#ifndef INT8_MIN
#define INT8_MIN SCHAR_MIN
#endif
#else
#error "not supported"
#endif
#if (UINT16_MAX == USHRT_MAX)
typedef unsigned short tflac_u16;
#ifndef UINT8_C
#define UINT8_C(x) ((tflac_u8)x)
#endif
#else
#error "not supported"
#endif
#if (INT16_MAX == SHRT_MAX)
typedef signed short tflac_s16;
#ifndef INT16_C
#define INT16_C(x) ((tflac_s16)x)
#ifndef INT16_MIN
#define INT16_MIN SHRT_MIN
#endif
#endif
#else
#error "not supported"
#endif
#if (UINT32_MAX == UINT_MAX)
typedef unsigned int tflac_u32;
#ifndef UINT32_C
#define UINT32_C(x) (x ## U)
#endif
#elif (UINT32_MAX == ULONG_MAX)
typedef unsigned long tflac_u32;
#ifndef UINT32_C
#define UINT32_C(x) (x ## UL)
#endif
#else
#error "not supported"
#endif
#if (INT32_MAX == INT_MAX)
typedef signed int tflac_s32;
#ifndef INT32_C
#define INT32_C(x) (x)
#endif
#ifndef INT32_MIN
#define INT32_MIN INT_MIN
#endif
#elif (INT32_MAX == LONG_MAX)
typedef signed long tflac_s32;
#ifndef INT32_C
#define INT32_C(x) (x ## L)
#endif
#ifndef INT32_MIN
#define INT32_MIN LONG_MIN
#endif
#else
#error "not supported"
#endif
#ifdef _MSC_VER
#ifndef TFLAC_32BIT_ONLY
typedef unsigned __int64 tflac_u64;
typedef signed __int64 tflac_s64;
#ifndef UINT64_MAX
#define UINT64_MAX 0xFFFFFFFFFFFFFFFFui64
#endif
#ifndef INT64_MAX
#define INT64_MAX 0x7FFFFFFFFFFFFFFFi64
#endif
#ifndef UINT64_C
#define UINT64_C(x) (x ## ui64)
#endif
#ifndef INT64_C
#define INT64_C(x) (x ## i64)
#endif
#endif
#if !defined(_UINTPTR_T_DEFINED)
/* this only occurs with MSVC < 8.0 which
* was the first to support 64-bit mode, so
* we'll just assume 32-bit uintptr_t */
typedef tflac_u32 tflac_uptr;
#else
typedef uintptr_t tflac_uptr;
#endif
#else
/* we're on an unknown platform! */
#define TFLAC_32BIT_ONLY
typedef tflac_u32 tflac_uptr;
#endif
#endif /* end no stdint.h */
#ifdef TFLAC_32BIT_ONLY
struct tflac_u64 {
tflac_u32 lo;
tflac_u32 hi;
};
typedef struct tflac_u64 tflac_u64;
typedef tflac_u64 tflac_s64;
typedef tflac_u32 tflac_uint;
#define tflac_pack_uintbe tflac_pack_u32be
#define tflac_pack_uintle tflac_pack_u32le
#else
typedef tflac_u64 tflac_uint;
#define tflac_pack_uintbe tflac_pack_u64be
#define tflac_pack_uintle tflac_pack_u64le
#endif
enum TFLAC_SUBFRAME_TYPE {
TFLAC_SUBFRAME_CONSTANT = 0,
TFLAC_SUBFRAME_VERBATIM = 1,
TFLAC_SUBFRAME_FIXED = 2,
TFLAC_SUBFRAME_LPC = 3,
TFLAC_SUBFRAME_TYPE_COUNT = 4
};
typedef enum TFLAC_SUBFRAME_TYPE TFLAC_SUBFRAME_TYPE;
enum TFLAC_CHANNEL_MODE {
TFLAC_CHANNEL_INDEPENDENT = 0,
TFLAC_CHANNEL_LEFT_SIDE = 1,
TFLAC_CHANNEL_SIDE_RIGHT = 2,
TFLAC_CHANNEL_MID_SIDE = 3,
TFLAC_CHANNEL_MODE_COUNT = 4,
};
typedef enum TFLAC_CHANNEL_MODE TFLAC_CHANNEL_MODE;
struct tflac_bitwriter {
tflac_uint val;
tflac_u32 bits;
tflac_u32 pos;
tflac_u32 len;
tflac_u32 tot;
tflac_u8* buffer;
};
typedef struct tflac_bitwriter tflac_bitwriter;
struct tflac_md5 {
tflac_u32 a;
tflac_u32 b;
tflac_u32 c;
tflac_u32 d;
tflac_u32 pos;
tflac_u64 total;
tflac_u8 buffer[64+8];
};
typedef struct tflac_md5 tflac_md5;
struct tflac {
tflac_bitwriter bw;
tflac_md5 md5_ctx;
tflac_u32 blocksize;
tflac_u32 samplerate;
tflac_u32 channels;
tflac_u32 bitdepth;
tflac_u8 channel_mode;
tflac_u8 max_rice_value; /* defaults to 14 if bitdepth < 16; 30 otherwise */
tflac_u8 min_partition_order; /* defaults to 0 */
tflac_u8 max_partition_order; /* defaults to 0, should be <=8 to be in streamable subset */
tflac_u8 partition_order;
tflac_u8 enable_constant_subframe;
tflac_u8 enable_fixed_subframe;
tflac_u8 enable_md5;
tflac_u32 frame_header;
tflac_u64 samplecount;
tflac_u32 frameno;
tflac_u32 verbatim_subframe_bits;
tflac_u32 cur_blocksize;
tflac_u32 max_frame_len; /* stores the max allowed frame length */
tflac_u32 min_frame_size;
tflac_u32 max_frame_size;
tflac_u32 wasted_bits;
tflac_u32 subframe_bitdepth;
tflac_u8 constant;
tflac_u8 md5_digest[16];
void (*calculate_order[5])(
tflac_u32 blocksize,
const tflac_s32* TFLAC_RESTRICT,
tflac_s32* TFLAC_RESTRICT,
tflac_u64* TFLAC_RESTRICT
);
tflac_u64 residual_errors[5];
tflac_s32* residuals[5]; /* orders 0, 1, 2, 3, 4 */
#ifndef TFLAC_DISABLE_COUNTERS
tflac_u64 subframe_type_counts[8][TFLAC_SUBFRAME_TYPE_COUNT]; /* stores stats on what
subframes were used per-channel */
#endif
};
typedef struct tflac tflac;
extern const char* const tflac_subframe_types[4];
/* runtime CPU features detection, should be called once, globally */
TFLAC_PUBLIC
void tflac_detect_cpu(void);
/* alternative, enable/disable SSE2 globally, returns 0 on success, anything
* else is an error */
TFLAC_PUBLIC
int tflac_default_sse2(int enable);
TFLAC_PUBLIC
int tflac_default_ssse3(int enable);
TFLAC_PUBLIC
int tflac_default_sse4_1(int enable);
/* you can also enable sse2 on the individual encoder, down below */
/* returns the maximum number of bytes to store a whole FLAC frame */
TFLAC_PUBLIC
TFLAC_CONST
tflac_u32 tflac_size_frame(tflac_u32 blocksize, tflac_u32 channels, tflac_u32 bitdepth);
/* returns the memory size of the tflac struct */
TFLAC_PUBLIC
TFLAC_CONST
tflac_u32 tflac_size(void);
/* returns how much memory is required for storing residuals */
TFLAC_PUBLIC
TFLAC_CONST
tflac_u32 tflac_size_memory(tflac_u32 blocksize);
/* return the size needed to write a STREAMINFO block */
TFLAC_PUBLIC
TFLAC_CONST
tflac_u32 tflac_size_streaminfo(void);
/* initialize a tflac struct */
TFLAC_PUBLIC
void tflac_init(tflac *);
/* validates that the encoder settings are OK, sets up needed memory pointers */
TFLAC_PUBLIC
int tflac_validate(tflac *, void* ptr, tflac_u32 len);
TFLAC_PUBLIC
int tflac_encode_s16p(tflac *, tflac_u32 blocksize, tflac_s16** samples, void* buffer, tflac_u32 len, tflac_u32* used);
TFLAC_PUBLIC
int tflac_encode_s16i(tflac *, tflac_u32 blocksize, tflac_s16* samples, void* buffer, tflac_u32 len, tflac_u32* used);
TFLAC_PUBLIC
int tflac_encode_s32p(tflac *, tflac_u32 blocksize, tflac_s32** samples, void* buffer, tflac_u32 len, tflac_u32* used);
TFLAC_PUBLIC
int tflac_encode_s32i(tflac *, tflac_u32 blocksize, tflac_s32* samples, void* buffer, tflac_u32 len, tflac_u32* used);
/* computes the final MD5 digest, if it was enabled */
TFLAC_PUBLIC
void tflac_finalize(tflac *);
/* encode a STREAMINFO block */
TFLAC_PUBLIC
int tflac_encode_streaminfo(const tflac *, tflac_u32 lastflag, void* buffer, tflac_u32 len, tflac_u32* used);
/* setters for various fields */
TFLAC_PUBLIC
void tflac_set_blocksize(tflac *t, tflac_u32 blocksize);
TFLAC_PUBLIC
void tflac_set_samplerate(tflac *t, tflac_u32 samplerate);
TFLAC_PUBLIC
void tflac_set_channels(tflac *t, tflac_u32 channels);
TFLAC_PUBLIC
void tflac_set_bitdepth(tflac *t, tflac_u32 bitdepth);
TFLAC_PUBLIC
void tflac_set_channel_mode(tflac *t, tflac_u32 channel_mode);
TFLAC_PUBLIC
void tflac_set_max_rice_value(tflac *t, tflac_u32 max_rice_value);
TFLAC_PUBLIC
void tflac_set_min_partition_order(tflac *t, tflac_u32 min_partition_order);
TFLAC_PUBLIC
void tflac_set_max_partition_order(tflac *t, tflac_u32 max_partition_order);
TFLAC_PUBLIC
void tflac_set_constant_subframe(tflac* t, tflac_u32 enable);
TFLAC_PUBLIC
void tflac_set_fixed_subframe(tflac* t, tflac_u32 enable);
TFLAC_PUBLIC
void tflac_set_enable_md5(tflac* t, tflac_u32 enable);
/* one of the few setters that can return an error, try
* to set the default to use sse2. returns 0 on success,
* 1 on error (because SSE2 support was not compiled */
TFLAC_PUBLIC
tflac_u32 tflac_enable_sse2(tflac* t, tflac_u32 enable);
TFLAC_PUBLIC
tflac_u32 tflac_enable_ssse3(tflac* t, tflac_u32 enable);
TFLAC_PUBLIC
tflac_u32 tflac_enable_sse4_1(tflac* t, tflac_u32 enable);
/* getters for various fields */
TFLAC_PURE
TFLAC_PUBLIC
tflac_u32 tflac_get_blocksize(const tflac* t);
TFLAC_PURE
TFLAC_PUBLIC
tflac_u32 tflac_get_samplerate(const tflac* t);
TFLAC_PURE
TFLAC_PUBLIC
tflac_u32 tflac_get_channels(const tflac* t);
TFLAC_PURE
TFLAC_PUBLIC
tflac_u32 tflac_get_bitdepth(const tflac* t);
TFLAC_PURE
TFLAC_PUBLIC
tflac_u32 tflac_get_channel_mode(const tflac* t);
TFLAC_PURE
TFLAC_PUBLIC
tflac_u32 tflac_get_max_rice_value(const tflac* t);
TFLAC_PURE
TFLAC_PUBLIC
tflac_u32 tflac_get_min_partition_order(const tflac* t);
TFLAC_PURE
TFLAC_PUBLIC
tflac_u32 tflac_get_max_partition_order(const tflac* t);
TFLAC_PURE
TFLAC_PUBLIC
tflac_u32 tflac_get_constant_subframe(const tflac* t);
TFLAC_PURE
TFLAC_PUBLIC
tflac_u32 tflac_get_fixed_subframe(const tflac* t);
TFLAC_PURE
TFLAC_PUBLIC
tflac_u32 tflac_get_enable_md5(const tflac* t);
TFLAC_PURE
TFLAC_PUBLIC
tflac_u32 tflac_get_enable_sse2(const tflac* t);
TFLAC_PURE
TFLAC_PUBLIC
tflac_u32 tflac_get_enable_ssse3(const tflac* t);
TFLAC_PURE
TFLAC_PUBLIC
tflac_u32 tflac_get_enable_sse4_1(const tflac* t);
#ifdef __cplusplus
}
#endif
#endif /* ifndef TFLAC_HEADER_GUARD */
#ifdef TFLAC_IMPLEMENTATION
#if defined(__x86_64__) || defined(_M_X64)
#define TFLAC_X64
#elif defined(__i386) || defined(_M_IX86)
#define TFLAC_X86
#endif
#ifndef TFLAC_ASSUME_ALIGNED
#if defined(__GNUC__)
#define TFLAC_ASSUME_ALIGNED(x,a) __builtin_assume_aligned(x,a)
#else
#define TFLAC_ASSUME_ALIGNED(x,a) x
#endif
#endif
#ifndef TFLAC_INLINE
#if __STDC_VERSION__ >= 199901L
#define TFLAC_INLINE inline
#elif defined(__GNUC__) && __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
#define TFLAC_INLINE __inline__
#elif defined(_MSC_VER) && _MSC_VER >= 1200
#define TFLAC_INLINE __inline
#else
#define TFLAC_INLINE
#endif
#endif
#ifndef TFLAC_UNLIKELY
#if defined(__GNUC__) && __GNUC__ >= 3
#define TFLAC_UNLIKELY(x) __builtin_expect(!!(x),0)
#else
#define TFLAC_UNLIKELY(x) (!!(x))
#endif
#endif
#ifndef TFLAC_LIKELY
#if defined(__GNUC__) && __GNUC__ >= 3
#define TFLAC_LIKELY(x) __builtin_expect(!!(x),1)
#else
#define TFLAC_LIKELY(x) (!!(x))
#endif
#endif
#ifndef TFLAC_ASSUME
#if defined(__GNUC__) && __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
#define TFLAC_ASSUME(x) do { if (!(x)) __builtin_unreachable(); } while (0);
#elif defined(_MSC_VER) && _MSC_VER >= 1310
#define TFLAC_ASSUME(x) do { if (!(x)) __assume(0); } while(0);
#else
#define TFLAC_ASSUME(x)
#endif
#endif
#ifndef TFLAC_ASSERT
#ifdef NDEBUG
#define TFLAC_ASSERT(x) TFLAC_ASSUME(x)
#else
#include <assert.h>
#define TFLAC_ASSERT(x) assert(x)
#endif
#endif
#ifdef TFLAC_32BIT_ONLY
#define TFLAC_UINT_MAX UINT32_MAX
#define TFLAC_BW_BITS (CHAR_BIT * sizeof(tflac_uint))
#else
#define TFLAC_UINT_MAX UINT64_MAX
#define TFLAC_BW_BITS (CHAR_BIT * sizeof(tflac_uint))
#endif
#if defined(_MSC_VER) && _MSC_VER >= 1400
#include <intrin.h>
#pragma intrinsic(_BitScanForward)
#ifdef TFLAC_X64
#pragma intrinsic(_BitScanForward64)
#endif
#endif
#if defined(TFLAC_X86) || defined(TFLAC_X64)
#ifdef __GNUC__
#include <cpuid.h>
#endif
#endif
#ifndef TFLAC_DISABLE_SSE2
/* TODO check for compiler support */
#ifdef __SSE2__
#define TFLAC_ENABLE_SSE2
#endif
#if defined(_MSC_VER) && _MSC_VER >= 1400
#define TFLAC_ENABLE_SSE2
#if _MSC_VER < 1500
#define _mm_cvtsi128_si64(x) _mm_cvtsi128_si64x(x)
#endif
#endif
#endif
#ifndef TFLAC_DISABLE_SSSE3
#ifdef __SSSE3__
#define TFLAC_ENABLE_SSSE3
#endif
#if defined(_MSC_VER) && _MSC_VER >= 1500
#define TFLAC_ENABLE_SSSE3
#endif
#endif /* DISABLE_SSSE3 */
#ifndef TFLAC_DISABLE_SSE4_1
#ifdef __SSE4_1__
#define TFLAC_ENABLE_SSE4_1
#endif
#if defined(_MSC_VER) && _MSC_VER >= 1500
#define TFLAC_ENABLE_SSE4_1
#endif
#endif
#ifdef TFLAC_ENABLE_SSE2
#include <emmintrin.h>
#endif
#ifdef TFLAC_ENABLE_SSSE3
#include <tmmintrin.h>
#endif
#ifdef TFLAC_ENABLE_SSE4_1
#include <smmintrin.h>
#endif
#ifdef TFLAC_32BIT_ONLY
TFLAC_PRIVATE TFLAC_INLINE
void tflac_u64_cast(tflac_u64* u, tflac_u32 v) {
u->hi = 0;
u->lo = v;
}
TFLAC_PRIVATE TFLAC_INLINE
void tflac_u64_add(tflac_u64* a, const tflac_u64* b) {
a->hi += b->hi;
a->hi += (a->lo += b->lo) < b->lo;
}
TFLAC_PRIVATE TFLAC_INLINE
void tflac_u64_sub(tflac_u64* a, const tflac_u64* b) {
tflac_u32 t = a->lo;
a->hi -= b->hi;
a->hi -= (a->lo -= b->lo) > t;
}
TFLAC_PRIVATE TFLAC_INLINE
void tflac_u64_add_word(tflac_u64* a, tflac_u32 b) {
a->hi += (a->lo += b) < b;
}
TFLAC_PRIVATE TFLAC_INLINE
int tflac_u64_cmp(const tflac_u64* a, const tflac_u64* b) {
if(a->hi == b->hi) {
if(a->lo == b->lo) return 0;
return a->lo < b->lo ? -1 : 1;
}
return a->hi < b->hi ? -1 : 1;
}
TFLAC_PRIVATE TFLAC_INLINE
int tflac_u64_cmp_word(const tflac_u64* a, tflac_u32 b) {
if(a->hi) return 1;
if(a->lo == b) return 0;
return a->lo < b ? -1 : 1;
}
TFLAC_PRIVATE TFLAC_INLINE
void tflac_s64_cast(tflac_s64* s, tflac_s32 v) {
s->hi = (tflac_u32)(v >> 31);
s->lo = (tflac_u32)(v);
}
#define tflac_s64_add(a,b) tflac_u64_add( (tflac_u64*)(a), (const tflac_u64*)(b) )
TFLAC_PRIVATE TFLAC_INLINE
void tflac_s64_abs(tflac_u64* u, const tflac_s64* a) {
const tflac_s32 mask = ((tflac_s32)a->hi) >> 31;
tflac_u32 carry;
carry = (u->lo = (a->lo ^ mask) - mask) < a->lo;
u->hi = (a->hi ^ mask) - mask - carry;
}
TFLAC_PRIVATE TFLAC_INLINE
void tflac_s64_neg(tflac_s64* s) {
s->hi = ~s->hi;
s->lo = ~s->lo;
s->hi += (s->lo += 1) == 0;
}
TFLAC_PRIVATE TFLAC_INLINE
void tflac_s64_cast32(tflac_s32* d, const tflac_s64* s) {
*d = s->lo;
}
#define TFLAC_U64_CAST(x,y) (tflac_u64_cast(&(x),(tflac_u32)(y)))
#define TFLAC_U64_ADD_WORD(x,y) (tflac_u64_add_word(&x,(tflac_u32)(y)))
#define TFLAC_U64_ADD(x,y) (tflac_u64_add(&(x),&(y)))
#define TFLAC_U64_GT(x,y) (tflac_u64_cmp(&(x),&(y)) == 1)
#define TFLAC_U64_LT(x,y) (tflac_u64_cmp(&(x),&(y)) == -1)
#define TFLAC_U64_EQ(x,y) (tflac_u64_cmp(&(x),&(y)) == 0)
#define TFLAC_U64_GT_WORD(x,y) (tflac_u64_cmp_word(&(x),y) == 1)
#define TFLAC_U64_LT_WORD(x,y) (tflac_u64_cmp_word(&(x),y) == -1)
#define TFLAC_U64_EQ_WORD(x,y) (tflac_u64_cmp_word(&(x),y) == 0)
#define TFLAC_S64_CAST(x,y) (tflac_s64_cast(&(x),(tflac_s32)(y)))
#define TFLAC_S64_NEG(x) (tflac_s64_neg(&(x)))
#define TFLAC_S64_ADD(x,y) (tflac_s64_add(&(x),&(y)))
#define TFLAC_S64_ABS(x,y) (tflac_s64_abs(&(x),&(y)))
#define TFLAC_S64_CAST32(x,y) (tflac_s64_cast32( &(x), &(y) ))
static const tflac_u64 TFLAC_U64_ZERO = { 0, 0 };
static const tflac_u64 TFLAC_U64_MAX = { UINT32_MAX, UINT32_MAX } ;
#else
TFLAC_PRIVATE TFLAC_INLINE
tflac_s64 tflac_s64_abs(tflac_s64 v) {
#ifdef __GNUC__
return __builtin_llabs(v);
#else
const tflac_s64 mask = v >> 63;
return (v ^ mask) - mask;
#endif
}
#define TFLAC_U64_CAST(x,y) ( (x) = (tflac_u64)(y) )
#define TFLAC_U64_ADD_WORD(x,y) ((x) += (tflac_u64)(y) )
#define TFLAC_U64_ADD(x,y) ( (x) += (y) )
#define TFLAC_U64_GT(x,y) ((x) > (y))
#define TFLAC_U64_LT(x,y) ((x) < (y))
#define TFLAC_U64_EQ(x,y) ((x) == (y))
#define TFLAC_U64_GT_WORD(x,y) ((x) > (tflac_u64)(y))
#define TFLAC_U64_LT_WORD(x,y) ((x) < (tflac_u64)(y))
#define TFLAC_U64_EQ_WORD(x,y) ((x) == (tflac_u64)(y))
#define TFLAC_S64_CAST(x,y) ( (x) = (tflac_s64)(y) )
#define TFLAC_S64_ADD(x,y) ( (x) += (y) )
#define TFLAC_S64_ABS(x,y) ( (x) = (tflac_u64)(tflac_s64_abs((y))) )
#define TFLAC_S64_CAST32(x,y) ( (x) = (tflac_s32)(y) )
#define TFLAC_U64_ZERO UINT64_C(0)
#define TFLAC_U64_MAX UINT64_MAX
#endif
TFLAC_PRIVATE
TFLAC_INLINE tflac_s32 tflac_s32_abs(tflac_s32 v) {
#ifdef __GNUC__
return __builtin_abs(v);
#else
const tflac_s32 mask = v >> 31;
return (v ^ mask) - mask;
#endif
}
TFLAC_PRIVATE
TFLAC_INLINE
tflac_u16 tflac_unpack_u16be(const tflac_u8* d) {
return (((tflac_u16)d[1]) ) |
(((tflac_u16)d[0])<< 8);
}
TFLAC_PRIVATE
TFLAC_INLINE
tflac_u32 tflac_unpack_u32le(const tflac_u8* d) {
return (((tflac_u32)d[0]) ) |
(((tflac_u32)d[1])<< 8) |
(((tflac_u32)d[2])<<16) |
(((tflac_u32)d[3])<<24);
}
TFLAC_PRIVATE
TFLAC_INLINE
tflac_u32 tflac_unpack_u32be(const tflac_u8* d) {
return (((tflac_u32)d[3]) ) |
(((tflac_u32)d[2])<< 8) |
(((tflac_u32)d[1])<<16) |
(((tflac_u32)d[0])<<24);
}
TFLAC_PRIVATE
TFLAC_INLINE
void tflac_pack_u32le(tflac_u8* d, tflac_u32 n) {
d[0] = (tflac_u8)( n );
d[1] = (tflac_u8)( n >> 8 );
d[2] = (tflac_u8)( n >> 16 );
d[3] = (tflac_u8)( n >> 24 );
}
TFLAC_PRIVATE
TFLAC_INLINE
void tflac_pack_u32be(tflac_u8* d, tflac_u32 n) {
d[0] = (tflac_u8)( n >> 24 );
d[1] = (tflac_u8)( n >> 16 );
d[2] = (tflac_u8)( n >> 8 );
d[3] = (tflac_u8)( n );
}
TFLAC_PRIVATE
TFLAC_INLINE
void tflac_pack_u64le(tflac_u8* d, tflac_u64 n) {
#ifdef TFLAC_32BIT_ONLY
tflac_pack_u32le(&d[0],n.lo);
tflac_pack_u32le(&d[4],n.hi);
#else
d[0] = (tflac_u8)( n );
d[1] = (tflac_u8)( n >> 8 );
d[2] = (tflac_u8)( n >> 16 );
d[3] = (tflac_u8)( n >> 24 );
d[4] = (tflac_u8)( n >> 32 );
d[5] = (tflac_u8)( n >> 40 );
d[6] = (tflac_u8)( n >> 48 );
d[7] = (tflac_u8)( n >> 56 );
#endif
}
TFLAC_PRIVATE
TFLAC_INLINE
void tflac_pack_u64be(tflac_u8* d, tflac_u64 n) {
#ifdef TFLAC_32BIT_ONLY
tflac_pack_u32be(d,n.hi);
tflac_pack_u32be(d,n.lo);
#else
d[0] = (tflac_u8)( n >> 56 );
d[1] = (tflac_u8)( n >> 48 );
d[2] = (tflac_u8)( n >> 40 );
d[3] = (tflac_u8)( n >> 32 );
d[4] = (tflac_u8)( n >> 24 );
d[5] = (tflac_u8)( n >> 16 );
d[6] = (tflac_u8)( n >> 8 );
d[7] = (tflac_u8)( n );
#endif
}
typedef void (*tflac_md5_calculator)(tflac*, void* samples);
typedef void (*tflac_stereo_decorrelator)(tflac*, tflac_u32 channel, void* samples);
struct tflac_encode_params {
tflac_u32 blocksize;
tflac_u32 buffer_len;
void* buffer;
void* samples;
tflac_u32 *used;
tflac_md5_calculator calculate_md5;
tflac_stereo_decorrelator decorrelate;
};
typedef struct tflac_encode_params tflac_encode_params;
const char* const tflac_subframe_types[4] = {
"CONSTANT",
"VERBATIM",
"FIXED",
"LPC",
};
TFLAC_PRIVATE
TFLAC_CONST
tflac_u32 tflac_max_size_frame(tflac_u32 blocksize, tflac_u32 channels, tflac_u32 bitdepth) {
/* since blocksize is really a 16-bit value and everything else is way under that
* we don't have to worry about overflow */
/* the max bytes used for frame headers and footer is 18:
* 2 for frame sync + blocking strategy
* 1 for block size + sample rate
* 1 for channel assignment and sample size
* 7 for maximum sample number (we use frame number so it's really 6)
* 2 for optional 16-bit blocksize
* 2 for optional 16-bit samplerate
* 1 for crc8
* 2 for crc16 */
/* additionally if the bitdepth is less than 32 we could have a bitdepth+1 subframe */
return UINT32_C(18) +
/* each subframe header takes 1 byte */
channels +
((
/* for non-stereo files we always use independent subframes */
(blocksize * bitdepth * (channels * (channels != 2))) +
/* on a stereo file we will have 1 subframe at our current bitdepth */