-
Notifications
You must be signed in to change notification settings - Fork 11
/
uflac_enc.h
2849 lines (2478 loc) · 63.7 KB
/
uflac_enc.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
/* public domain Simple, Minimalistic, FLAC encoder based on Flake
* ©2018-2020 Yuichiro Nakada
*
* Basic usage:
* uflac_encode("music.flac", pcm, len, 44100, 16, 2, 9);
*
* */
#ifndef UINT32_MAX
#define UINT32_MAX 4294967295
#endif
/**
* Flake: FLAC audio encoder
* Copyright (c) 2006 Justin Ruggles
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
// flake.h
#ifndef FLAKE_H
#define FLAKE_H
#define FLAKE_STRINGIFY(s) FLAKE_TOSTRING(s)
#define FLAKE_TOSTRING(s) #s
#define FLAKE_VERSION 0.11+
#define FLAKE_IDENT "Flake" FLAKE_STRINGIFY(FLAKE_VERSION)
#define FLAKE_ORDER_METHOD_MAX 0
#define FLAKE_ORDER_METHOD_EST 1
#define FLAKE_ORDER_METHOD_2LEVEL 2
#define FLAKE_ORDER_METHOD_4LEVEL 3
#define FLAKE_ORDER_METHOD_8LEVEL 4
#define FLAKE_ORDER_METHOD_SEARCH 5
#define FLAKE_ORDER_METHOD_LOG 6
#define FLAKE_STEREO_METHOD_INDEPENDENT 0
#define FLAKE_STEREO_METHOD_ESTIMATE 1
#define FLAKE_PREDICTION_NONE 0
#define FLAKE_PREDICTION_FIXED 1
#define FLAKE_PREDICTION_LEVINSON 2
typedef struct FlakeEncodeParams {
// compression quality
// set by user prior to calling flake_encode_init
// standard values are 0 to 8
// 0 is lower compression, faster encoding
// 8 is higher compression, slower encoding
// extended values 9 to 12 are slower and/or use
// higher prediction orders
int compression;
// prediction order selection method
// set by user prior to calling flake_encode_init
// if set to less than 0, it is chosen based on compression.
// valid values are 0 to 5
// 0 = use maximum order only
// 1 = use estimation
// 2 = 2-level
// 3 = 4-level
// 4 = 8-level
// 5 = full search
// 6 = log search
int order_method;
// stereo decorrelation method
// set by user prior to calling flake_encode_init
// if set to less than 0, it is chosen based on compression.
// valid values are 0 to 2
// 0 = independent L+R channels
// 1 = mid-side encoding
int stereo_method;
// block size in samples
// set by the user prior to calling flake_encode_init
// if set to 0, a block size is chosen based on block_time_ms
// can also be changed by user before encoding a frame
int block_size;
// block time in milliseconds
// set by the user prior to calling flake_encode_init
// used to calculate block_size based on sample rate
// can also be changed by user before encoding a frame
int block_time_ms;
// padding size in bytes
// set by the user prior to calling flake_encode_init
// if set to less than 0, defaults to 4096
int padding_size;
// maximum encoded frame size
// this is set by flake_encode_init based on input audio format
// it can be used by the user to allocate an output buffer
int max_frame_size;
// minimum prediction order
// set by user prior to calling flake_encode_init
// if set to less than 0, it is chosen based on compression.
// valid values are 0 to 4 for fixed prediction and 1 to 32 for non-fixed
int min_prediction_order;
// maximum prediction order
// set by user prior to calling flake_encode_init
// if set to less than 0, it is chosen based on compression.
// valid values are 0 to 4 for fixed prediction and 1 to 32 for non-fixed
int max_prediction_order;
// type of linear prediction
// set by user prior to calling flake_encode_init
// if set to less than 0, it is chosen based on compression.
// 0 = fixed prediction
// 1 = Levinson-Durbin recursion
int prediction_type;
// minimum partition order
// set by user prior to calling flake_encode_init
// if set to less than 0, it is chosen based on compression.
// valid values are 0 to 8
int min_partition_order;
// maximum partition order
// set by user prior to calling flake_encode_init
// if set to less than 0, it is chosen based on compression.
// valid values are 0 to 8
int max_partition_order;
// whether to use variable block sizes
// set by user prior to calling flake_encode_init
// 0 = fixed block size
// 1 = variable block size
int variable_block_size;
} FlakeEncodeParams;
typedef struct FlakeContext {
// number of audio channels
// set by user prior to calling flake_encode_init
// valid values are 1 to 8
int channels;
// audio sample rate in Hz
// set by user prior to calling flake_encode_init
int sample_rate;
// sample size in bits
// set by user prior to calling flake_encode_init
// only 16-bit is currently supported
int bits_per_sample;
// total stream samples
// set by user prior to calling flake_encode_init
// if 0, stream length is unknown
unsigned int samples;
FlakeEncodeParams params;
// maximum frame size in bytes
// set by flake_encode_init
// this can be used to allocate memory for output
int max_frame_size;
// MD5 digest
// set by flake_encode_close;
unsigned char md5digest[16];
// header bytes
// allocated by flake_encode_init and freed by flake_encode_close
unsigned char *header;
// encoding context, which is hidden from the user
// allocated by flake_encode_init and freed by flake_encode_close
void *private_ctx;
} FlakeContext;
/**
* Sets encoding defaults based on compression level
* params->compression must be set prior to calling
*/
extern int flake_set_defaults(FlakeEncodeParams *params);
/**
* Validates encoding parameters
* @return -1 if error. 0 if ok. 1 if ok but non-Subset.
*/
extern int flake_validate_params(FlakeContext *s);
extern int flake_encode_init(FlakeContext *s);
extern int flake_encode_frame(FlakeContext *s, unsigned char *frame_buffer,
short *samples);
extern void flake_encode_close(FlakeContext *s);
#endif /* FLAKE_H */
// common.h
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#ifndef M_SQRT2
#define M_SQRT2 1.41421356237309504880
#endif
#ifndef EMULATE_INTTYPES
#include <inttypes.h>
#else
#if defined(_WIN32) && defined(_MSC_VER)
typedef __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#endif
#endif /* EMULATE_INTTYPES */
#define ABS(a) ((a) >= 0 ? (a) : (-(a)))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) > (b) ? (b) : (a))
#define CLIP(x,min,max) MAX(MIN((x), (max)), (min))
static inline int
log2i(uint32_t v)
{
int i;
int n = 0;
if (v & 0xffff0000) {
v >>= 16;
n += 16;
}
if (v & 0xff00) {
v >>= 8;
n += 8;
}
for (i=2; i<256; i<<=1) {
if (v >= i) {
n++;
} else {
break;
}
}
return n;
}
#include <string.h>
// strnlen is a GNU extention. providing implementation if needed.
#ifndef HAVE_STRNLEN
/*static*/ inline size_t
strnlen(const char *s, size_t maxlen)
{
size_t i = 0;
while ((s[i] != '\0') && (i < maxlen)) {
i++;
}
return i;
}
#elif !defined(__USE_GNU)
extern size_t strnlen(const char *s, size_t maxlen);
#endif
// bswap.h
static inline uint16_t bswap_16(uint16_t x)
{
return (x>>8) | (x<<8);
}
static inline uint32_t bswap_32(uint32_t x)
{
x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
return (x>>16) | (x<<16);
}
static inline uint64_t bswap_64(uint64_t x)
{
union {
uint64_t ll;
uint32_t l[2];
} w, r;
w.ll = x;
r.l[0] = bswap_32(w.l[1]);
r.l[1] = bswap_32(w.l[0]);
return r.ll;
}
// be2me ... BigEndian to MachineEndian
// le2me ... LittleEndian to MachineEndian
#ifdef WORDS_BIGENDIAN
#define be2me_16(x) (x)
#define be2me_32(x) (x)
#define be2me_64(x) (x)
#define le2me_16(x) bswap_16(x)
#define le2me_32(x) bswap_32(x)
#define le2me_64(x) bswap_64(x)
#else
#define be2me_16(x) bswap_16(x)
#define be2me_32(x) bswap_32(x)
#define be2me_64(x) bswap_64(x)
#define le2me_16(x) (x)
#define le2me_32(x) (x)
#define le2me_64(x) (x)
#endif
// bitio.h
typedef struct BitWriter {
uint32_t bit_buf;
int bit_left;
uint8_t *buffer, *buf_ptr, *buf_end;
int eof;
} BitWriter;
static inline void
bitwriter_init(BitWriter *bw, void *buf, int len)
{
if (len < 0) {
len = 0;
buf = NULL;
}
bw->buffer = (uint8_t*)buf;
bw->buf_end = bw->buffer + len;
bw->buf_ptr = bw->buffer;
bw->bit_left = 32;
bw->bit_buf = 0;
bw->eof = 0;
}
static inline uint32_t
bitwriter_count(BitWriter *bw)
{
// TODO: simplify
return ((((bw->buf_ptr - bw->buffer) << 3) + 32 - bw->bit_left) + 7) >> 3;
}
static inline void
bitwriter_flush(BitWriter *bw)
{
bw->bit_buf <<= bw->bit_left;
while (bw->bit_left < 32 && !bw->eof) {
if (bw->buf_ptr >= bw->buf_end) {
bw->eof = 1;
break;
}
if (bw->buffer != NULL) {
*bw->buf_ptr = bw->bit_buf >> 24;
}
bw->buf_ptr++;
bw->bit_buf <<= 8;
bw->bit_left += 8;
}
bw->bit_left = 32;
bw->bit_buf = 0;
}
static inline void
bitwriter_writebits(BitWriter *bw, int bits, uint32_t val)
{
uint32_t bb=0;
assert(bits == 32 || val < (1U << bits));
if (bits == 0 || bw->eof) {
return;
}
if ((bw->buf_ptr+3) >= bw->buf_end) {
bw->eof = 1;
return;
}
if (bits < bw->bit_left) {
bw->bit_buf = (bw->bit_buf << bits) | val;
bw->bit_left -= bits;
} else {
if (bw->bit_left == 32) {
assert(bits == 32);
bb = val;
} else {
bb = (bw->bit_buf << bw->bit_left) | (val >> (bits - bw->bit_left));
bw->bit_left += (32 - bits);
}
if (bw->buffer != NULL) {
*(uint32_t *)bw->buf_ptr = be2me_32(bb);
}
bw->buf_ptr += 4;
bw->bit_buf = val;
}
}
static inline void
bitwriter_writebits_signed(BitWriter *bw, int bits, int32_t val)
{
assert(bits >= 0 && bits <= 31);
bitwriter_writebits(bw, bits, val & ((1ULL<<bits)-1));
}
static inline void
bitwriter_write_rice_signed(BitWriter *bw, int k, int32_t val)
{
int v, q;
if (k < 0) {
return;
}
// convert signed to unsigned
v = -2*val-1;
v ^= (v>>31);
// write quotient in unary
q = (v >> k) + 1;
while (q > 31) {
bitwriter_writebits(bw, 31, 0);
q -= 31;
}
bitwriter_writebits(bw, q, 1);
// write write remainder in binary using 'k' bits
bitwriter_writebits(bw, k, v&((1<<k)-1));
}
// crc.c
static void
crc_init_table(uint16_t *table, int bits, int poly)
{
int i, j, crc;
poly = (poly + (1<<bits));
for (i=0; i<256; i++) {
crc = i;
for (j=0; j<bits; j++) {
if (crc & (1<<(bits-1))) {
crc = (crc << 1) ^ poly;
} else {
crc <<= 1;
}
}
table[i] = (crc & ((1<<bits)-1));
}
}
/* CRC key for polynomial, x^8 + x^2 + x^1 + 1 */
#define CRC8_POLY 0x07
/* CRC key for polynomial, x^16 + x^15 + x^2 + 1 */
#define CRC16_POLY 0x8005
static uint16_t crc8tab[256];
static uint16_t crc16tab[256];
void
crc_init()
{
crc_init_table(crc8tab, 8, CRC8_POLY);
crc_init_table(crc16tab, 16, CRC16_POLY);
}
static uint16_t
calc_crc(const uint16_t *table, int bits, const uint8_t *data, uint32_t len)
{
uint16_t crc, v1, v2;
crc = 0;
while (len--) {
v1 = (crc << 8) & ((1 << bits) - 1);
v2 = (crc >> (bits - 8)) ^ *data++;
assert(v2 < 256);
crc = v1 ^ table[v2];
}
return crc;
}
uint8_t
calc_crc8(const uint8_t *data, uint32_t len)
{
uint8_t crc;
if (data == NULL) {
return 0;
}
crc = calc_crc(crc8tab, 8, data, len);
return crc;
}
uint16_t
calc_crc16(const uint8_t *data, uint32_t len)
{
uint16_t crc;
if (data == NULL) {
return 0;
}
crc = calc_crc(crc16tab, 16, data, len);
return crc;
}
// lpc.c
#define MAX_LPC_ORDER 32
/**
* Apply Welch window function to audio block
*/
static inline void
apply_welch_window(const int32_t *data, int len, double *w_data)
{
int i;
double c;
c = (2.0 / (len - 1.0)) - 1.0;
for (i=0; i<(len >> 1); i++) {
double w = 1.0 - ((c-i) * (c-i));
w_data[i] = data[i] * w;
w_data[len-1-i] = data[len-1-i] * w;
}
}
/**
* Calculates autocorrelation data from audio samples
* A Welch window function is applied before calculation.
*/
static void
compute_autocorr(const int32_t *data, int len, int lag, double *autoc)
{
int i, j;
double *data1;
double temp, temp2;
data1 = (double*)malloc((len+16) * sizeof(double));
apply_welch_window(data, len, data1);
data1[len] = 0;
for (i=0; i<=lag; ++i) {
temp = 1.0;
temp2 = 1.0;
for (j=0; j<=lag-i; ++j) {
temp += data1[j+i] * data1[j];
}
for (j=lag+1; j<=len-1; j+=2) {
temp += data1[j] * data1[j-i];
temp2 += data1[j+1] * data1[j+1-i];
}
autoc[i] = temp + temp2;
}
free(data1);
}
/**
* Levinson-Durbin recursion.
* Produces LPC coefficients from autocorrelation data.
*/
static void
compute_lpc_coefs(const double *autoc, int max_order, double *ref,
double lpc[][MAX_LPC_ORDER])
{
int i, j, i2;
double r, err, tmp;
double lpc_tmp[MAX_LPC_ORDER];
for (i=0; i<max_order; i++) {
lpc_tmp[i] = 0;
}
err = 1.0;
if (autoc) {
err = autoc[0];
}
for (i=0; i<max_order; i++) {
if (ref) {
r = ref[i];
} else {
r = -autoc[i+1];
for (j=0; j<i; j++) {
r -= lpc_tmp[j] * autoc[i-j];
}
r /= err;
err *= 1.0 - (r * r);
}
i2 = (i >> 1);
lpc_tmp[i] = r;
for (j=0; j<i2; j++) {
tmp = lpc_tmp[j];
lpc_tmp[j] += r * lpc_tmp[i-1-j];
lpc_tmp[i-1-j] += r * tmp;
}
if (i & 1) {
lpc_tmp[j] += lpc_tmp[j] * r;
}
for (j=0; j<=i; j++) {
lpc[i][j] = -lpc_tmp[j];
}
}
}
/**
* Compute LPC coefs for FLAKE_ORDER_METHOD_EST
* Faster LPC coeff computation by first calculating the reflection coefficients
* using Schur recursion. That allows for estimating the optimal order before
* running Levinson recursion.
*/
static int
compute_lpc_coefs_est(const double *autoc, int max_order,
double lpc[][MAX_LPC_ORDER])
{
int i, j;
double error;
double gen[2][MAX_LPC_ORDER];
double ref[MAX_LPC_ORDER];
int order_est;
// Schur recursion
for (i=0; i<max_order; i++) {
gen[0][i] = gen[1][i] = autoc[i+1];
}
error = autoc[0];
ref[0] = -gen[1][0] / error;
error += gen[1][0] * ref[0];
for (i=1; i<max_order; i++) {
for (j=0; j<max_order-i; j++) {
gen[1][j] = gen[1][j+1] + ref[i-1] * gen[0][j];
gen[0][j] = gen[1][j+1] * ref[i-1] + gen[0][j];
}
ref[i] = -gen[1][0] / error;
error += gen[1][0] * ref[i];
}
// Estimate optimal order using reflection coefficients
order_est = 1;
for (i=max_order-1; i>=0; i--) {
if (fabs(ref[i]) > 0.10) {
order_est = i+1;
break;
}
}
// Levinson recursion
compute_lpc_coefs(NULL, order_est, ref, lpc);
return order_est;
}
/**
* Quantize LPC coefficients
*/
static void
quantize_lpc_coefs(double *lpc_in, int order, int precision, int32_t *lpc_out,
int *shift)
{
int i;
double d, cmax, error;
int32_t qmax;
int sh, q;
// define maximum levels
qmax = (1 << (precision - 1)) - 1;
// find maximum coefficient value
cmax = 0.0;
for (i=0; i<order; i++) {
d = fabs(lpc_in[i]);
if (d > cmax) {
cmax = d;
}
}
// if maximum value quantizes to zero, return all zeros
if (cmax * (1 << 15) < 1.0) {
*shift = 0;
memset(lpc_out, 0, sizeof(int32_t) * order);
return;
}
// calculate level shift which scales max coeff to available bits
sh = 15;
while ((cmax * (1 << sh) > qmax) && (sh > 0)) {
sh--;
}
// since negative shift values are unsupported in decoder, scale down
// coefficients instead
if (sh == 0 && cmax > qmax) {
double scale = ((double)qmax) / cmax;
for (i=0; i<order; i++) {
lpc_in[i] *= scale;
}
}
// output quantized coefficients and level shift
error=0;
for (i=0; i<order; i++) {
error += lpc_in[i] * (1 << sh);
q = error + 0.5;
if (q <= -qmax) {
q = -qmax+1;
}
if (q > qmax) {
q = qmax;
}
error -= q;
lpc_out[i] = q;
}
*shift = sh;
}
/**
* Calculate LPC coefficients for multiple orders
*/
int
lpc_calc_coefs(const int32_t *samples, int blocksize, int max_order,
int precision, int omethod, int32_t coefs[][MAX_LPC_ORDER],
int *shift)
{
double autoc[MAX_LPC_ORDER+1];
double lpc[MAX_LPC_ORDER][MAX_LPC_ORDER];
int i;
int opt_order;
compute_autocorr(samples, blocksize, max_order+1, autoc);
opt_order = max_order;
if (omethod == FLAKE_ORDER_METHOD_EST) {
opt_order = compute_lpc_coefs_est(autoc, max_order, lpc);
} else {
compute_lpc_coefs(autoc, max_order, NULL, lpc);
}
switch (omethod) {
case FLAKE_ORDER_METHOD_MAX:
case FLAKE_ORDER_METHOD_EST:
i = opt_order-1;
quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i]);
break;
default:
for (i=0; i<max_order; i++) {
quantize_lpc_coefs(lpc[i], i+1, precision, coefs[i], &shift[i]);
}
break;
}
return opt_order;
}
// md5.c
typedef struct {
uint32_t lo, hi;
uint32_t a, b, c, d;
uint8_t buffer[64];
uint32_t block[16];
} MD5Context;
/*
* The basic MD5 functions.
*
* F is optimized compared to its RFC 1321 definition just like in Colin
* Plumb's implementation.
*/
#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
/*
* The MD5 transformation for all four rounds.
*/
#define STEP(f, a, b, c, d, x, t, s) \
(a) += f((b), (c), (d)) + (x) + (t); \
(a) = (((a) << (s)) | (((a) & 0xFFFFFFFF) >> (32 - (s)))); \
(a) += (b);
/*
* SET reads 4 input bytes in little-endian byte order and stores them
* in a properly aligned word in host byte order.
*/
#define SET(n) \
(ctx->block[(n)] = \
(uint32_t)ptr[(n) * 4] | \
((uint32_t)ptr[(n) * 4 + 1] << 8) | \
((uint32_t)ptr[(n) * 4 + 2] << 16) | \
((uint32_t)ptr[(n) * 4 + 3] << 24))
#define GET(n) \
(ctx->block[(n)])
/*
* This processes one or more 64-byte data blocks, but does NOT update
* the bit counters. There are no alignment requirements.
*/
static const void *
body(MD5Context *ctx, const void *data, uint32_t size)
{
const uint8_t *ptr;
uint32_t a, b, c, d;
uint32_t saved_a, saved_b, saved_c, saved_d;
ptr = (uint8_t*)data;
a = ctx->a;
b = ctx->b;
c = ctx->c;
d = ctx->d;
do {
saved_a = a;
saved_b = b;
saved_c = c;
saved_d = d;
// Round 1
STEP(F, a, b, c, d, SET( 0), 0xD76AA478, 7)
STEP(F, d, a, b, c, SET( 1), 0xE8C7B756, 12)
STEP(F, c, d, a, b, SET( 2), 0x242070DB, 17)
STEP(F, b, c, d, a, SET( 3), 0xC1BDCEEE, 22)
STEP(F, a, b, c, d, SET( 4), 0xF57C0FAF, 7)
STEP(F, d, a, b, c, SET( 5), 0x4787C62A, 12)
STEP(F, c, d, a, b, SET( 6), 0xA8304613, 17)
STEP(F, b, c, d, a, SET( 7), 0xFD469501, 22)
STEP(F, a, b, c, d, SET( 8), 0x698098D8, 7)
STEP(F, d, a, b, c, SET( 9), 0x8B44F7AF, 12)
STEP(F, c, d, a, b, SET(10), 0xFFFF5BB1, 17)
STEP(F, b, c, d, a, SET(11), 0x895CD7BE, 22)
STEP(F, a, b, c, d, SET(12), 0x6B901122, 7)
STEP(F, d, a, b, c, SET(13), 0xFD987193, 12)
STEP(F, c, d, a, b, SET(14), 0xA679438E, 17)
STEP(F, b, c, d, a, SET(15), 0x49B40821, 22)
// Round 2
STEP(G, a, b, c, d, GET( 1), 0xF61E2562, 5)
STEP(G, d, a, b, c, GET( 6), 0xC040B340, 9)
STEP(G, c, d, a, b, GET(11), 0x265E5A51, 14)
STEP(G, b, c, d, a, GET( 0), 0xE9B6C7AA, 20)
STEP(G, a, b, c, d, GET( 5), 0xD62F105D, 5)
STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
STEP(G, c, d, a, b, GET(15), 0xD8A1E681, 14)
STEP(G, b, c, d, a, GET( 4), 0xE7D3FBC8, 20)
STEP(G, a, b, c, d, GET( 9), 0x21E1CDE6, 5)
STEP(G, d, a, b, c, GET(14), 0xC33707D6, 9)
STEP(G, c, d, a, b, GET( 3), 0xF4D50D87, 14)
STEP(G, b, c, d, a, GET( 8), 0x455A14ED, 20)
STEP(G, a, b, c, d, GET(13), 0xA9E3E905, 5)
STEP(G, d, a, b, c, GET( 2), 0xFCEFA3F8, 9)
STEP(G, c, d, a, b, GET( 7), 0x676F02D9, 14)
STEP(G, b, c, d, a, GET(12), 0x8D2A4C8A, 20)
// Round 3
STEP(H, a, b, c, d, GET( 5), 0xFFFA3942, 4)
STEP(H, d, a, b, c, GET( 8), 0x8771F681, 11)
STEP(H, c, d, a, b, GET(11), 0x6D9D6122, 16)
STEP(H, b, c, d, a, GET(14), 0xFDE5380C, 23)
STEP(H, a, b, c, d, GET( 1), 0xA4BEEA44, 4)
STEP(H, d, a, b, c, GET( 4), 0x4BDECFA9, 11)
STEP(H, c, d, a, b, GET( 7), 0xF6BB4B60, 16)
STEP(H, b, c, d, a, GET(10), 0xBEBFBC70, 23)
STEP(H, a, b, c, d, GET(13), 0x289B7EC6, 4)
STEP(H, d, a, b, c, GET( 0), 0xEAA127FA, 11)
STEP(H, c, d, a, b, GET( 3), 0xD4EF3085, 16)
STEP(H, b, c, d, a, GET( 6), 0x04881D05, 23)
STEP(H, a, b, c, d, GET( 9), 0xD9D4D039, 4)
STEP(H, d, a, b, c, GET(12), 0xE6DB99E5, 11)
STEP(H, c, d, a, b, GET(15), 0x1FA27CF8, 16)
STEP(H, b, c, d, a, GET( 2), 0xC4AC5665, 23)
// Round 4
STEP(I, a, b, c, d, GET( 0), 0xF4292244, 6)
STEP(I, d, a, b, c, GET( 7), 0x432AFF97, 10)
STEP(I, c, d, a, b, GET(14), 0xAB9423A7, 15)
STEP(I, b, c, d, a, GET( 5), 0xFC93A039, 21)
STEP(I, a, b, c, d, GET(12), 0x655B59C3, 6)
STEP(I, d, a, b, c, GET( 3), 0x8F0CCC92, 10)
STEP(I, c, d, a, b, GET(10), 0xFFEFF47D, 15)
STEP(I, b, c, d, a, GET( 1), 0x85845DD1, 21)
STEP(I, a, b, c, d, GET( 8), 0x6FA87E4F, 6)
STEP(I, d, a, b, c, GET(15), 0xFE2CE6E0, 10)
STEP(I, c, d, a, b, GET( 6), 0xA3014314, 15)
STEP(I, b, c, d, a, GET(13), 0x4E0811A1, 21)
STEP(I, a, b, c, d, GET( 4), 0xF7537E82, 6)
STEP(I, d, a, b, c, GET(11), 0xBD3AF235, 10)
STEP(I, c, d, a, b, GET( 2), 0x2AD7D2BB, 15)
STEP(I, b, c, d, a, GET( 9), 0xEB86D391, 21)
a += saved_a;
b += saved_b;
c += saved_c;
d += saved_d;
ptr += 64;
} while (size -= 64);
ctx->a = a;
ctx->b = b;
ctx->c = c;
ctx->d = d;
return ptr;
}
void
md5_init(MD5Context *ctx)
{
ctx->a = 0x67452301;
ctx->b = 0xefcdab89;
ctx->c = 0x98badcfe;
ctx->d = 0x10325476;
ctx->lo = 0;
ctx->hi = 0;
}
void
md5_update(MD5Context *ctx, const void *data, uint32_t size)
{
uint32_t saved_lo;
uint32_t used, free;
saved_lo = ctx->lo;
if ((ctx->lo = (saved_lo + size) & 0x1FFFFFFF) < saved_lo) {
ctx->hi++;
}
ctx->hi += size >> 29;
used = saved_lo & 0x3f;
if (used) {
free = 64 - used;
if (size < free) {
memcpy(&ctx->buffer[used], data, size);
return;
}
memcpy(&ctx->buffer[used], data, free);
data = (uint8_t *)data + free;
size -= free;
body(ctx, ctx->buffer, 64);
}
if (size >= 64) {
data = body(ctx, data, size & ~(uint32_t)0x3f);
size &= 0x3f;
}
memcpy(ctx->buffer, data, size);
}
void
md5_final(uint8_t *result, MD5Context *ctx)
{
uint32_t used, free;
used = ctx->lo & 0x3f;
ctx->buffer[used++] = 0x80;
free = 64 - used;
if (free < 8) {
memset(&ctx->buffer[used], 0, free);
body(ctx, ctx->buffer, 64);
used = 0;
free = 64;
}
memset(&ctx->buffer[used], 0, free - 8);