-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer3.c
2698 lines (2189 loc) · 68.5 KB
/
layer3.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
/*
* libmad - MPEG audio decoder library
* Copyright (C) 2000-2004 Underbit Technologies, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: layer3.c,v 1.43 2004/01/23 09:41:32 rob Exp $
*/
# ifdef HAVE_CONFIG_H
# include "config.h"
# endif
# include "global.h"
# include <stdlib.h>
# include <string.h>
# ifdef HAVE_ASSERT_H
# include <assert.h>
# endif
# ifdef HAVE_LIMITS_H
# include <limits.h>
# else
# define CHAR_BIT 8
# endif
# include "fixed.h"
# include "bit.h"
# include "stream.h"
# include "frame.h"
# include "huffman.h"
# include "layer3.h"
/* --- Layer III ----------------------------------------------------------- */
enum {
count1table_select = 0x01,
scalefac_scale = 0x02,
preflag = 0x04,
mixed_block_flag = 0x08
};
enum {
I_STEREO = 0x1,
MS_STEREO = 0x2
};
struct sideinfo {
unsigned int main_data_begin;
unsigned int private_bits;
unsigned char scfsi[2];
struct granule {
struct channel {
/* from side info */
unsigned short part2_3_length;
unsigned short big_values;
unsigned short global_gain;
unsigned short scalefac_compress;
unsigned char flags;
unsigned char block_type;
unsigned char table_select[3];
unsigned char subblock_gain[3];
unsigned char region0_count;
unsigned char region1_count;
/* from main_data */
unsigned char scalefac[39]; /* scalefac_l and/or scalefac_s */
} ch[2];
} gr[2];
};
/*
* scalefactor bit lengths
* derived from section 2.4.2.7 of ISO/IEC 11172-3
*/
static
struct {
unsigned char slen1;
unsigned char slen2;
} const sflen_table[16] = {
{ 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 },
{ 3, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 },
{ 2, 1 }, { 2, 2 }, { 2, 3 }, { 3, 1 },
{ 3, 2 }, { 3, 3 }, { 4, 2 }, { 4, 3 }
};
/*
* number of LSF scalefactor band values
* derived from section 2.4.3.2 of ISO/IEC 13818-3
*/
static
unsigned char const nsfb_table[6][3][4] = {
{ { 6, 5, 5, 5 },
{ 9, 9, 9, 9 },
{ 6, 9, 9, 9 } },
{ { 6, 5, 7, 3 },
{ 9, 9, 12, 6 },
{ 6, 9, 12, 6 } },
{ { 11, 10, 0, 0 },
{ 18, 18, 0, 0 },
{ 15, 18, 0, 0 } },
{ { 7, 7, 7, 0 },
{ 12, 12, 12, 0 },
{ 6, 15, 12, 0 } },
{ { 6, 6, 6, 3 },
{ 12, 9, 9, 6 },
{ 6, 12, 9, 6 } },
{ { 8, 8, 5, 0 },
{ 15, 12, 9, 0 },
{ 6, 18, 9, 0 } }
};
/*
* MPEG-1 scalefactor band widths
* derived from Table B.8 of ISO/IEC 11172-3
*/
static
unsigned char const sfb_48000_long[] = {
4, 4, 4, 4, 4, 4, 6, 6, 6, 8, 10,
12, 16, 18, 22, 28, 34, 40, 46, 54, 54, 192
};
static
unsigned char const sfb_44100_long[] = {
4, 4, 4, 4, 4, 4, 6, 6, 8, 8, 10,
12, 16, 20, 24, 28, 34, 42, 50, 54, 76, 158
};
static
unsigned char const sfb_32000_long[] = {
4, 4, 4, 4, 4, 4, 6, 6, 8, 10, 12,
16, 20, 24, 30, 38, 46, 56, 68, 84, 102, 26
};
static
unsigned char const sfb_48000_short[] = {
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
};
static
unsigned char const sfb_44100_short[] = {
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
};
static
unsigned char const sfb_32000_short[] = {
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
};
static
unsigned char const sfb_48000_mixed[] = {
/* long */ 4, 4, 4, 4, 4, 4, 6, 6,
/* short */ 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
};
static
unsigned char const sfb_44100_mixed[] = {
/* long */ 4, 4, 4, 4, 4, 4, 6, 6,
/* short */ 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
};
static
unsigned char const sfb_32000_mixed[] = {
/* long */ 4, 4, 4, 4, 4, 4, 6, 6,
/* short */ 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
};
/*
* MPEG-2 scalefactor band widths
* derived from Table B.2 of ISO/IEC 13818-3
*/
static
unsigned char const sfb_24000_long[] = {
6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
18, 22, 26, 32, 38, 46, 54, 62, 70, 76, 36
};
static
unsigned char const sfb_22050_long[] = {
6, 6, 6, 6, 6, 6, 8, 10, 12, 14, 16,
20, 24, 28, 32, 38, 46, 52, 60, 68, 58, 54
};
# define sfb_16000_long sfb_22050_long
static
unsigned char const sfb_24000_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, 32, 32, 32, 44, 44, 44, 12, 12, 12
};
static
unsigned char const sfb_22050_short[] = {
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
};
static
unsigned char const sfb_16000_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
};
static
unsigned char const sfb_24000_mixed[] = {
/* long */ 6, 6, 6, 6, 6, 6,
/* short */ 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
};
static
unsigned char const sfb_22050_mixed[] = {
/* long */ 6, 6, 6, 6, 6, 6,
/* short */ 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
};
static
unsigned char const sfb_16000_mixed[] = {
/* long */ 6, 6, 6, 6, 6, 6,
/* short */ 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
};
/*
* MPEG 2.5 scalefactor band widths
* derived from public sources
*/
# define sfb_12000_long sfb_16000_long
# define sfb_11025_long sfb_12000_long
static
unsigned char const sfb_8000_long[] = {
12, 12, 12, 12, 12, 12, 16, 20, 24, 28, 32,
40, 48, 56, 64, 76, 90, 2, 2, 2, 2, 2
};
# define sfb_12000_short sfb_16000_short
# define sfb_11025_short sfb_12000_short
static
unsigned char const sfb_8000_short[] = {
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
};
# define sfb_12000_mixed sfb_16000_mixed
# define sfb_11025_mixed sfb_12000_mixed
/* the 8000 Hz short block scalefactor bands do not break after
the first 36 frequency lines, so this is probably wrong */
static
unsigned char const sfb_8000_mixed[] = {
/* long */ 12, 12, 12,
/* short */ 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
};
static
struct {
unsigned char const *l;
unsigned char const *s;
unsigned char const *m;
} const sfbwidth_table[9] = {
{ sfb_48000_long, sfb_48000_short, sfb_48000_mixed },
{ sfb_44100_long, sfb_44100_short, sfb_44100_mixed },
{ sfb_32000_long, sfb_32000_short, sfb_32000_mixed },
{ sfb_24000_long, sfb_24000_short, sfb_24000_mixed },
{ sfb_22050_long, sfb_22050_short, sfb_22050_mixed },
{ sfb_16000_long, sfb_16000_short, sfb_16000_mixed },
{ sfb_12000_long, sfb_12000_short, sfb_12000_mixed },
{ sfb_11025_long, sfb_11025_short, sfb_11025_mixed },
{ sfb_8000_long, sfb_8000_short, sfb_8000_mixed }
};
/*
* scalefactor band preemphasis (used only when preflag is set)
* derived from Table B.6 of ISO/IEC 11172-3
*/
static
unsigned char const pretab[22] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 3, 3, 3, 2, 0
};
/*
* table for requantization
*
* rq_table[x].mantissa * 2^(rq_table[x].exponent) = x^(4/3)
*/
static
struct fixedfloat {
unsigned long mantissa : 27;
unsigned short exponent : 5;
} const rq_table[8207] = {
# include "rq_table.dat"
};
/*
* fractional powers of two
* used for requantization and joint stereo decoding
*
* root_table[3 + x] = 2^(x/4)
*/
static
mad_fixed_t const root_table[7] = {
MAD_F(0x09837f05) /* 2^(-3/4) == 0.59460355750136 */,
MAD_F(0x0b504f33) /* 2^(-2/4) == 0.70710678118655 */,
MAD_F(0x0d744fcd) /* 2^(-1/4) == 0.84089641525371 */,
MAD_F(0x10000000) /* 2^( 0/4) == 1.00000000000000 */,
MAD_F(0x1306fe0a) /* 2^(+1/4) == 1.18920711500272 */,
MAD_F(0x16a09e66) /* 2^(+2/4) == 1.41421356237310 */,
MAD_F(0x1ae89f99) /* 2^(+3/4) == 1.68179283050743 */
};
/*
* coefficients for aliasing reduction
* derived from Table B.9 of ISO/IEC 11172-3
*
* c[] = { -0.6, -0.535, -0.33, -0.185, -0.095, -0.041, -0.0142, -0.0037 }
* cs[i] = 1 / sqrt(1 + c[i]^2)
* ca[i] = c[i] / sqrt(1 + c[i]^2)
*/
static
mad_fixed_t const cs[8] = {
+MAD_F(0x0db84a81) /* +0.857492926 */, +MAD_F(0x0e1b9d7f) /* +0.881741997 */,
+MAD_F(0x0f31adcf) /* +0.949628649 */, +MAD_F(0x0fbba815) /* +0.983314592 */,
+MAD_F(0x0feda417) /* +0.995517816 */, +MAD_F(0x0ffc8fc8) /* +0.999160558 */,
+MAD_F(0x0fff964c) /* +0.999899195 */, +MAD_F(0x0ffff8d3) /* +0.999993155 */
};
static
mad_fixed_t const ca[8] = {
-MAD_F(0x083b5fe7) /* -0.514495755 */, -MAD_F(0x078c36d2) /* -0.471731969 */,
-MAD_F(0x05039814) /* -0.313377454 */, -MAD_F(0x02e91dd1) /* -0.181913200 */,
-MAD_F(0x0183603a) /* -0.094574193 */, -MAD_F(0x00a7cb87) /* -0.040965583 */,
-MAD_F(0x003a2847) /* -0.014198569 */, -MAD_F(0x000f27b4) /* -0.003699975 */
};
/*
* IMDCT coefficients for short blocks
* derived from section 2.4.3.4.10.2 of ISO/IEC 11172-3
*
* imdct_s[i/even][k] = cos((PI / 24) * (2 * (i / 2) + 7) * (2 * k + 1))
* imdct_s[i /odd][k] = cos((PI / 24) * (2 * (6 + (i-1)/2) + 7) * (2 * k + 1))
*/
static
mad_fixed_t const imdct_s[6][6] = {
# include "imdct_s.dat"
};
# if !defined(ASO_IMDCT)
/*
* windowing coefficients for long blocks
* derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
*
* window_l[i] = sin((PI / 36) * (i + 1/2))
*/
static
mad_fixed_t const window_l[36] = {
MAD_F(0x00b2aa3e) /* 0.043619387 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
MAD_F(0x03768962) /* 0.216439614 */, MAD_F(0x04cfb0e2) /* 0.300705800 */,
MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x07635284) /* 0.461748613 */,
MAD_F(0x0898c779) /* 0.537299608 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
MAD_F(0x0acf37ad) /* 0.675590208 */, MAD_F(0x0bcbe352) /* 0.737277337 */,
MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x0d7e8807) /* 0.843391446 */,
MAD_F(0x0e313245) /* 0.887010833 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
MAD_F(0x0f426cb5) /* 0.953716951 */, MAD_F(0x0f9ee890) /* 0.976296007 */,
MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ffc19fd) /* 0.999048222 */,
MAD_F(0x0ffc19fd) /* 0.999048222 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
MAD_F(0x0f9ee890) /* 0.976296007 */, MAD_F(0x0f426cb5) /* 0.953716951 */,
MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0e313245) /* 0.887010833 */,
MAD_F(0x0d7e8807) /* 0.843391446 */, MAD_F(0x0cb19346) /* 0.793353340 */,
MAD_F(0x0bcbe352) /* 0.737277337 */, MAD_F(0x0acf37ad) /* 0.675590208 */,
MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0898c779) /* 0.537299608 */,
MAD_F(0x07635284) /* 0.461748613 */, MAD_F(0x061f78aa) /* 0.382683432 */,
MAD_F(0x04cfb0e2) /* 0.300705800 */, MAD_F(0x03768962) /* 0.216439614 */,
MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x00b2aa3e) /* 0.043619387 */,
};
# endif /* ASO_IMDCT */
/*
* windowing coefficients for short blocks
* derived from section 2.4.3.4.10.3 of ISO/IEC 11172-3
*
* window_s[i] = sin((PI / 12) * (i + 1/2))
*/
static
mad_fixed_t const window_s[12] = {
MAD_F(0x0216a2a2) /* 0.130526192 */, MAD_F(0x061f78aa) /* 0.382683432 */,
MAD_F(0x09bd7ca0) /* 0.608761429 */, MAD_F(0x0cb19346) /* 0.793353340 */,
MAD_F(0x0ec835e8) /* 0.923879533 */, MAD_F(0x0fdcf549) /* 0.991444861 */,
MAD_F(0x0fdcf549) /* 0.991444861 */, MAD_F(0x0ec835e8) /* 0.923879533 */,
MAD_F(0x0cb19346) /* 0.793353340 */, MAD_F(0x09bd7ca0) /* 0.608761429 */,
MAD_F(0x061f78aa) /* 0.382683432 */, MAD_F(0x0216a2a2) /* 0.130526192 */,
};
/*
* coefficients for intensity stereo processing
* derived from section 2.4.3.4.9.3 of ISO/IEC 11172-3
*
* is_ratio[i] = tan(i * (PI / 12))
* is_table[i] = is_ratio[i] / (1 + is_ratio[i])
*/
static
mad_fixed_t const is_table[7] = {
MAD_F(0x00000000) /* 0.000000000 */,
MAD_F(0x0361962f) /* 0.211324865 */,
MAD_F(0x05db3d74) /* 0.366025404 */,
MAD_F(0x08000000) /* 0.500000000 */,
MAD_F(0x0a24c28c) /* 0.633974596 */,
MAD_F(0x0c9e69d1) /* 0.788675135 */,
MAD_F(0x10000000) /* 1.000000000 */
};
/*
* coefficients for LSF intensity stereo processing
* derived from section 2.4.3.2 of ISO/IEC 13818-3
*
* is_lsf_table[0][i] = (1 / sqrt(sqrt(2)))^(i + 1)
* is_lsf_table[1][i] = (1 / sqrt(2)) ^(i + 1)
*/
static
mad_fixed_t const is_lsf_table[2][15] = {
{
MAD_F(0x0d744fcd) /* 0.840896415 */,
MAD_F(0x0b504f33) /* 0.707106781 */,
MAD_F(0x09837f05) /* 0.594603558 */,
MAD_F(0x08000000) /* 0.500000000 */,
MAD_F(0x06ba27e6) /* 0.420448208 */,
MAD_F(0x05a8279a) /* 0.353553391 */,
MAD_F(0x04c1bf83) /* 0.297301779 */,
MAD_F(0x04000000) /* 0.250000000 */,
MAD_F(0x035d13f3) /* 0.210224104 */,
MAD_F(0x02d413cd) /* 0.176776695 */,
MAD_F(0x0260dfc1) /* 0.148650889 */,
MAD_F(0x02000000) /* 0.125000000 */,
MAD_F(0x01ae89fa) /* 0.105112052 */,
MAD_F(0x016a09e6) /* 0.088388348 */,
MAD_F(0x01306fe1) /* 0.074325445 */
}, {
MAD_F(0x0b504f33) /* 0.707106781 */,
MAD_F(0x08000000) /* 0.500000000 */,
MAD_F(0x05a8279a) /* 0.353553391 */,
MAD_F(0x04000000) /* 0.250000000 */,
MAD_F(0x02d413cd) /* 0.176776695 */,
MAD_F(0x02000000) /* 0.125000000 */,
MAD_F(0x016a09e6) /* 0.088388348 */,
MAD_F(0x01000000) /* 0.062500000 */,
MAD_F(0x00b504f3) /* 0.044194174 */,
MAD_F(0x00800000) /* 0.031250000 */,
MAD_F(0x005a827a) /* 0.022097087 */,
MAD_F(0x00400000) /* 0.015625000 */,
MAD_F(0x002d413d) /* 0.011048543 */,
MAD_F(0x00200000) /* 0.007812500 */,
MAD_F(0x0016a09e) /* 0.005524272 */
}
};
/*
* NAME: III_sideinfo()
* DESCRIPTION: decode frame side information from a bitstream
*/
static
enum mad_error III_sideinfo(struct mad_bitptr *ptr, unsigned int nch,
int lsf, struct sideinfo *si,
unsigned int *data_bitlen,
unsigned int *priv_bitlen)
{
unsigned int ngr, gr, ch, i;
enum mad_error result = MAD_ERROR_NONE;
*data_bitlen = 0;
*priv_bitlen = lsf ? ((nch == 1) ? 1 : 2) : ((nch == 1) ? 5 : 3);
si->main_data_begin = mad_bit_read(ptr, lsf ? 8 : 9);
si->private_bits = mad_bit_read(ptr, *priv_bitlen);
ngr = 1;
if (!lsf) {
ngr = 2;
for (ch = 0; ch < nch; ++ch)
si->scfsi[ch] = mad_bit_read(ptr, 4);
}
for (gr = 0; gr < ngr; ++gr) {
struct granule *granule = &si->gr[gr];
for (ch = 0; ch < nch; ++ch) {
struct channel *channel = &granule->ch[ch];
channel->part2_3_length = mad_bit_read(ptr, 12);
channel->big_values = mad_bit_read(ptr, 9);
channel->global_gain = mad_bit_read(ptr, 8);
channel->scalefac_compress = mad_bit_read(ptr, lsf ? 9 : 4);
*data_bitlen += channel->part2_3_length;
if (channel->big_values > 288 && result == 0)
result = MAD_ERROR_BADBIGVALUES;
channel->flags = 0;
/* window_switching_flag */
if (mad_bit_read(ptr, 1)) {
channel->block_type = mad_bit_read(ptr, 2);
if (channel->block_type == 0 && result == 0)
result = MAD_ERROR_BADBLOCKTYPE;
if (!lsf && channel->block_type == 2 && si->scfsi[ch] && result == 0)
result = MAD_ERROR_BADSCFSI;
channel->region0_count = 7;
channel->region1_count = 36;
if (mad_bit_read(ptr, 1))
channel->flags |= mixed_block_flag;
else if (channel->block_type == 2)
channel->region0_count = 8;
for (i = 0; i < 2; ++i)
channel->table_select[i] = mad_bit_read(ptr, 5);
# if defined(DEBUG)
channel->table_select[2] = 4; /* not used */
# endif
for (i = 0; i < 3; ++i)
channel->subblock_gain[i] = mad_bit_read(ptr, 3);
}
else {
channel->block_type = 0;
for (i = 0; i < 3; ++i)
channel->table_select[i] = mad_bit_read(ptr, 5);
channel->region0_count = mad_bit_read(ptr, 4);
channel->region1_count = mad_bit_read(ptr, 3);
}
/* [preflag,] scalefac_scale, count1table_select */
channel->flags |= mad_bit_read(ptr, lsf ? 2 : 3);
}
}
return result;
}
/*
* NAME: III_scalefactors_lsf()
* DESCRIPTION: decode channel scalefactors for LSF from a bitstream
*/
static
unsigned int III_scalefactors_lsf(struct mad_bitptr *ptr,
struct channel *channel,
struct channel *gr1ch, int mode_extension)
{
struct mad_bitptr start;
unsigned int scalefac_compress, index, slen[4], part, n, i;
unsigned char const *nsfb;
start = *ptr;
scalefac_compress = channel->scalefac_compress;
index = (channel->block_type == 2) ?
((channel->flags & mixed_block_flag) ? 2 : 1) : 0;
if (!((mode_extension & I_STEREO) && gr1ch)) {
if (scalefac_compress < 400) {
slen[0] = (scalefac_compress >> 4) / 5;
slen[1] = (scalefac_compress >> 4) % 5;
slen[2] = (scalefac_compress % 16) >> 2;
slen[3] = scalefac_compress % 4;
nsfb = nsfb_table[0][index];
}
else if (scalefac_compress < 500) {
scalefac_compress -= 400;
slen[0] = (scalefac_compress >> 2) / 5;
slen[1] = (scalefac_compress >> 2) % 5;
slen[2] = scalefac_compress % 4;
slen[3] = 0;
nsfb = nsfb_table[1][index];
}
else {
scalefac_compress -= 500;
slen[0] = scalefac_compress / 3;
slen[1] = scalefac_compress % 3;
slen[2] = 0;
slen[3] = 0;
channel->flags |= preflag;
nsfb = nsfb_table[2][index];
}
n = 0;
for (part = 0; part < 4; ++part) {
for (i = 0; i < nsfb[part]; ++i)
channel->scalefac[n++] = mad_bit_read(ptr, slen[part]);
}
while (n < 39)
channel->scalefac[n++] = 0;
}
else { /* (mode_extension & I_STEREO) && gr1ch (i.e. ch == 1) */
scalefac_compress >>= 1;
if (scalefac_compress < 180) {
slen[0] = scalefac_compress / 36;
slen[1] = (scalefac_compress % 36) / 6;
slen[2] = (scalefac_compress % 36) % 6;
slen[3] = 0;
nsfb = nsfb_table[3][index];
}
else if (scalefac_compress < 244) {
scalefac_compress -= 180;
slen[0] = (scalefac_compress % 64) >> 4;
slen[1] = (scalefac_compress % 16) >> 2;
slen[2] = scalefac_compress % 4;
slen[3] = 0;
nsfb = nsfb_table[4][index];
}
else {
scalefac_compress -= 244;
slen[0] = scalefac_compress / 3;
slen[1] = scalefac_compress % 3;
slen[2] = 0;
slen[3] = 0;
nsfb = nsfb_table[5][index];
}
n = 0;
for (part = 0; part < 4; ++part) {
unsigned int max, is_pos;
max = (1 << slen[part]) - 1;
for (i = 0; i < nsfb[part]; ++i) {
is_pos = mad_bit_read(ptr, slen[part]);
channel->scalefac[n] = is_pos;
gr1ch->scalefac[n++] = (is_pos == max);
}
}
while (n < 39) {
channel->scalefac[n] = 0;
gr1ch->scalefac[n++] = 0; /* apparently not illegal */
}
}
return mad_bit_length(&start, ptr);
}
/*
* NAME: III_scalefactors()
* DESCRIPTION: decode channel scalefactors of one granule from a bitstream
*/
static
unsigned int III_scalefactors(struct mad_bitptr *ptr, struct channel *channel,
struct channel const *gr0ch, unsigned int scfsi)
{
struct mad_bitptr start;
unsigned int slen1, slen2, sfbi;
start = *ptr;
slen1 = sflen_table[channel->scalefac_compress].slen1;
slen2 = sflen_table[channel->scalefac_compress].slen2;
if (channel->block_type == 2) {
unsigned int nsfb;
sfbi = 0;
nsfb = (channel->flags & mixed_block_flag) ? 8 + 3 * 3 : 6 * 3;
while (nsfb--)
channel->scalefac[sfbi++] = mad_bit_read(ptr, slen1);
nsfb = 6 * 3;
while (nsfb--)
channel->scalefac[sfbi++] = mad_bit_read(ptr, slen2);
nsfb = 1 * 3;
while (nsfb--)
channel->scalefac[sfbi++] = 0;
}
else { /* channel->block_type != 2 */
if (scfsi & 0x8) {
for (sfbi = 0; sfbi < 6; ++sfbi)
channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
}
else {
for (sfbi = 0; sfbi < 6; ++sfbi)
channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
}
if (scfsi & 0x4) {
for (sfbi = 6; sfbi < 11; ++sfbi)
channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
}
else {
for (sfbi = 6; sfbi < 11; ++sfbi)
channel->scalefac[sfbi] = mad_bit_read(ptr, slen1);
}
if (scfsi & 0x2) {
for (sfbi = 11; sfbi < 16; ++sfbi)
channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
}
else {
for (sfbi = 11; sfbi < 16; ++sfbi)
channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
}
if (scfsi & 0x1) {
for (sfbi = 16; sfbi < 21; ++sfbi)
channel->scalefac[sfbi] = gr0ch->scalefac[sfbi];
}
else {
for (sfbi = 16; sfbi < 21; ++sfbi)
channel->scalefac[sfbi] = mad_bit_read(ptr, slen2);
}
channel->scalefac[21] = 0;
}
return mad_bit_length(&start, ptr);
}
/*
* The Layer III formula for requantization and scaling is defined by
* section 2.4.3.4.7.1 of ISO/IEC 11172-3, as follows:
*
* long blocks:
* xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
* 2^((1/4) * (global_gain - 210)) *
* 2^-(scalefac_multiplier *
* (scalefac_l[sfb] + preflag * pretab[sfb]))
*
* short blocks:
* xr[i] = sign(is[i]) * abs(is[i])^(4/3) *
* 2^((1/4) * (global_gain - 210 - 8 * subblock_gain[w])) *
* 2^-(scalefac_multiplier * scalefac_s[sfb][w])
*
* where:
* scalefac_multiplier = (scalefac_scale + 1) / 2
*
* The routines III_exponents() and III_requantize() facilitate this
* calculation.
*/
/*
* NAME: III_exponents()
* DESCRIPTION: calculate scalefactor exponents
*/
static
void III_exponents(struct channel const *channel,
unsigned char const *sfbwidth, signed int exponents[39])
{
signed int gain;
unsigned int scalefac_multiplier, sfbi;
gain = (signed int) channel->global_gain - 210;
scalefac_multiplier = (channel->flags & scalefac_scale) ? 2 : 1;
if (channel->block_type == 2) {
unsigned int l;
signed int gain0, gain1, gain2;
sfbi = l = 0;
if (channel->flags & mixed_block_flag) {
unsigned int premask;
premask = (channel->flags & preflag) ? ~0 : 0;
/* long block subbands 0-1 */
while (l < 36) {
exponents[sfbi] = gain -
(signed int) ((channel->scalefac[sfbi] + (pretab[sfbi] & premask)) <<
scalefac_multiplier);
l += sfbwidth[sfbi++];
}
}
/* this is probably wrong for 8000 Hz short/mixed blocks */
gain0 = gain - 8 * (signed int) channel->subblock_gain[0];
gain1 = gain - 8 * (signed int) channel->subblock_gain[1];
gain2 = gain - 8 * (signed int) channel->subblock_gain[2];
while (l < 576) {
exponents[sfbi + 0] = gain0 -
(signed int) (channel->scalefac[sfbi + 0] << scalefac_multiplier);
exponents[sfbi + 1] = gain1 -
(signed int) (channel->scalefac[sfbi + 1] << scalefac_multiplier);
exponents[sfbi + 2] = gain2 -
(signed int) (channel->scalefac[sfbi + 2] << scalefac_multiplier);
l += 3 * sfbwidth[sfbi];
sfbi += 3;
}
}
else { /* channel->block_type != 2 */
if (channel->flags & preflag) {
for (sfbi = 0; sfbi < 22; ++sfbi) {
exponents[sfbi] = gain -
(signed int) ((channel->scalefac[sfbi] + pretab[sfbi]) <<
scalefac_multiplier);
}
}
else {
for (sfbi = 0; sfbi < 22; ++sfbi) {
exponents[sfbi] = gain -
(signed int) (channel->scalefac[sfbi] << scalefac_multiplier);
}
}
}
}
/*
* NAME: III_requantize()
* DESCRIPTION: requantize one (positive) value
*/
static
mad_fixed_t III_requantize(unsigned int value, signed int exp)
{
mad_fixed_t requantized;
signed int frac;
struct fixedfloat const *power;
frac = exp % 4; /* assumes sign(frac) == sign(exp) */
exp /= 4;
power = &rq_table[value];
requantized = power->mantissa;
exp += power->exponent;
if (exp < 0) {
if (-exp >= sizeof(mad_fixed_t) * CHAR_BIT) {
/* underflow */
requantized = 0;
}
else {
requantized += 1L << (-exp - 1);
requantized >>= -exp;
}
}
else {
if (exp >= 5) {
/* overflow */
# if defined(DEBUG)
fprintf(stderr, "requantize overflow (%f * 2^%d)\n",
mad_f_todouble(requantized), exp);
# endif
requantized = MAD_F_MAX;
}
else
requantized <<= exp;
}
return frac ? mad_f_mul(requantized, root_table[3 + frac]) : requantized;
}
/* we must take care that sz >= bits and sz < sizeof(long) lest bits == 0 */
# define MASK(cache, sz, bits) \
(((cache) >> ((sz) - (bits))) & ((1 << (bits)) - 1))
# define MASK1BIT(cache, sz) \
((cache) & (1 << ((sz) - 1)))
/*
* NAME: III_huffdecode()
* DESCRIPTION: decode Huffman code words of one channel of one granule
*/
static
enum mad_error III_huffdecode(struct mad_bitptr *ptr, mad_fixed_t xr[576],
struct channel *channel,
unsigned char const *sfbwidth,
unsigned int part2_length)
{
signed int exponents[39], exp;
signed int const *expptr;
struct mad_bitptr peek;
signed int bits_left, cachesz;
register mad_fixed_t *xrptr;
mad_fixed_t const *sfbound;
register unsigned long bitcache;
bits_left = (signed) channel->part2_3_length - (signed) part2_length;
if (bits_left < 0)
return MAD_ERROR_BADPART3LEN;
III_exponents(channel, sfbwidth, exponents);
peek = *ptr;
mad_bit_skip(ptr, bits_left);
/* align bit reads to byte boundaries */
cachesz = mad_bit_bitsleft(&peek);
cachesz += ((32 - 1 - 24) + (24 - cachesz)) & ~7;
bitcache = mad_bit_read(&peek, cachesz);
bits_left -= cachesz;
xrptr = &xr[0];
/* big_values */
{
unsigned int region, rcount;
struct hufftable const *entry;
union huffpair const *table;
unsigned int linbits, startbits, big_values, reqhits;
mad_fixed_t reqcache[16];
sfbound = xrptr + *sfbwidth++;
rcount = channel->region0_count + 1;
entry = &mad_huff_pair_table[channel->table_select[region = 0]];
table = entry->table;
linbits = entry->linbits;
startbits = entry->startbits;
if (table == 0)
return MAD_ERROR_BADHUFFTABLE;
expptr = &exponents[0];
exp = *expptr++;
reqhits = 0;
big_values = channel->big_values;
while (big_values-- && cachesz + bits_left > 0) {
union huffpair const *pair;
unsigned int clumpsz, value;
register mad_fixed_t requantized;
if (xrptr == sfbound) {
sfbound += *sfbwidth++;
/* change table if region boundary */
if (--rcount == 0) {
if (region == 0)