Skip to content

Latest commit

 

History

History
2146 lines (1854 loc) · 51.8 KB

layer3.c

File metadata and controls

2146 lines (1854 loc) · 51.8 KB
 
Nov 10, 2019
Nov 10, 2019
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
/*
layer3.c: the layer 3 decoder
copyright 1995-2017 by the mpg123 project - free software under the terms of the LGPL 2.1
see COPYING and AUTHORS files in distribution or http://mpg123.org
initially written by Michael Hipp
Dear visitor:
If you feel you don't understand fully the works of this file, your feeling might be correct.
Optimize-TODO: put short bands into the band-field without the stride of 3 reals
Length-optimze: unify long and short band code where it is possible
The int-vs-pointer situation has to be cleaned up.
*/
#include "mpg123lib_intern.h"
#ifdef USE_NEW_HUFFTABLE
#include "newhuffman.h"
#else
#include "huffman.h"
#endif
#include "getbits.h"
#include "debug.h"
/* define CUT_SFB21 if you want to cut-off the frequency above 16kHz */
#if 0
#define CUT_SFB21
#endif
#ifdef REAL_IS_FIXED
#define NEW_DCT9
#include "l3_integer_tables.h"
#else
/* static one-time calculated tables... or so */
static real ispow[8207];
static real aa_ca[8],aa_cs[8];
static ALIGNED(16) real win[4][36];
static ALIGNED(16) real win1[4][36];
real COS9[9]; /* dct36_3dnow wants to use that */
static real COS6_1,COS6_2;
real tfcos36[9]; /* dct36_3dnow wants to use that */
static real tfcos12[3];
#define NEW_DCT9
#ifdef NEW_DCT9
static real cos9[3],cos18[3];
static real tan1_1[16],tan2_1[16],tan1_2[16],tan2_2[16];
static real pow1_1[2][32],pow2_1[2][32],pow1_2[2][32],pow2_2[2][32];
#endif
#endif
/* Decoder state data, living on the stack of do_layer3. */
struct gr_info_s
{
int scfsi;
unsigned part2_3_length;
unsigned big_values;
unsigned scalefac_compress;
unsigned block_type;
unsigned mixed_block_flag;
unsigned table_select[3];
/* Making those two signed int as workaround for open64/pathscale/sun compilers, and also for consistency, since they're worked on together with other signed variables. */
int maxband[3];
int maxbandl;
unsigned maxb;
unsigned region1start;
unsigned region2start;
unsigned preflag;
unsigned scalefac_scale;
unsigned count1table_select;
real *full_gain[3];
real *pow2gain;
};
struct III_sideinfo
{
unsigned main_data_begin;
unsigned private_bits;
/* Hm, funny... struct inside struct... */
struct { struct gr_info_s gr[2]; } ch[2];
};
struct bandInfoStruct
{
unsigned short longIdx[23];
unsigned char longDiff[22];
unsigned short shortIdx[14];
unsigned char shortDiff[13];
};
/* Techy details about our friendly MPEG data. Fairly constant over the years;-) */
static const struct bandInfoStruct bandInfo[9] =
{
{ /* MPEG 1.0 */
{0,4,8,12,16,20,24,30,36,44,52,62,74, 90,110,134,162,196,238,288,342,418,576},
{4,4,4,4,4,4,6,6,8, 8,10,12,16,20,24,28,34,42,50,54, 76,158},
{0,4*3,8*3,12*3,16*3,22*3,30*3,40*3,52*3,66*3, 84*3,106*3,136*3,192*3},
{4,4,4,4,6,8,10,12,14,18,22,30,56}
},
{
{0,4,8,12,16,20,24,30,36,42,50,60,72, 88,106,128,156,190,230,276,330,384,576},
{4,4,4,4,4,4,6,6,6, 8,10,12,16,18,22,28,34,40,46,54, 54,192},
{0,4*3,8*3,12*3,16*3,22*3,28*3,38*3,50*3,64*3, 80*3,100*3,126*3,192*3},
{4,4,4,4,6,6,10,12,14,16,20,26,66}
},
{
{0,4,8,12,16,20,24,30,36,44,54,66,82,102,126,156,194,240,296,364,448,550,576},
{4,4,4,4,4,4,6,6,8,10,12,16,20,24,30,38,46,56,68,84,102, 26},
{0,4*3,8*3,12*3,16*3,22*3,30*3,42*3,58*3,78*3,104*3,138*3,180*3,192*3},
{4,4,4,4,6,8,12,16,20,26,34,42,12}
},
{ /* MPEG 2.0 */
{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
{6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 } ,
{0,4*3,8*3,12*3,18*3,24*3,32*3,42*3,56*3,74*3,100*3,132*3,174*3,192*3} ,
{4,4,4,6,6,8,10,14,18,26,32,42,18 }
},
{ /* Twiddling 3 values here (not just 330->332!) fixed bug 1895025. */
{0,6,12,18,24,30,36,44,54,66,80,96,114,136,162,194,232,278,332,394,464,540,576},
{6,6,6,6,6,6,8,10,12,14,16,18,22,26,32,38,46,54,62,70,76,36 },
{0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,136*3,180*3,192*3},
{4,4,4,6,8,10,12,14,18,24,32,44,12 }
},
{
{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
{6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54 },
{0,4*3,8*3,12*3,18*3,26*3,36*3,48*3,62*3,80*3,104*3,134*3,174*3,192*3},
{4,4,4,6,8,10,12,14,18,24,30,40,18 }
},
{ /* MPEG 2.5 */
{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
{6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
{0,12,24,36,54,78,108,144,186,240,312,402,522,576},
{4,4,4,6,8,10,12,14,18,24,30,40,18}
},
{
{0,6,12,18,24,30,36,44,54,66,80,96,116,140,168,200,238,284,336,396,464,522,576},
{6,6,6,6,6,6,8,10,12,14,16,20,24,28,32,38,46,52,60,68,58,54},
{0,12,24,36,54,78,108,144,186,240,312,402,522,576},
{4,4,4,6,8,10,12,14,18,24,30,40,18}
},
{
{0,12,24,36,48,60,72,88,108,132,160,192,232,280,336,400,476,566,568,570,572,574,576},
{12,12,12,12,12,12,16,20,24,28,32,40,48,56,64,76,90,2,2,2,2,2},
{0, 24, 48, 72,108,156,216,288,372,480,486,492,498,576},
{8,8,8,12,16,20,24,28,36,2,2,2,26}
}
};
static int mapbuf0[9][152];
static int mapbuf1[9][156];
static int mapbuf2[9][44];
static int *map[9][3];
static int *mapend[9][3];
static unsigned int n_slen2[512]; /* MPEG 2.0 slen for 'normal' mode */
static unsigned int i_slen2[256]; /* MPEG 2.0 slen for intensity stereo */
/* Some helpers used in init_layer3 */
#ifdef OPT_MMXORSSE
real init_layer3_gainpow2_mmx(mpg123_handle *fr, int i)
{
if(!fr->p.down_sample) return DOUBLE_TO_REAL(16384.0 * pow((double)2.0,-0.25 * (double) (i+210) ));
else return DOUBLE_TO_REAL(pow((double)2.0,-0.25 * (double) (i+210)));
}
#endif
real init_layer3_gainpow2(mpg123_handle *fr, int i)
{
#if defined(REAL_IS_FIXED) && defined(PRECALC_TABLES)
return gainpow2[i+256];
#else
return DOUBLE_TO_REAL_SCALE_LAYER3(pow((double)2.0,-0.25 * (double) (i+210)),i+256);
#endif
}
/* init tables for layer-3 ... specific with the downsampling... */
void init_layer3(void)
{
int i,j,k,l;
#if !defined(REAL_IS_FIXED) || !defined(PRECALC_TABLES)
for(i=0;i<8207;i++)
ispow[i] = DOUBLE_TO_REAL_POW43(pow((double)i,(double)4.0/3.0));
for(i=0;i<8;i++)
{
const double Ci[8] = {-0.6,-0.535,-0.33,-0.185,-0.095,-0.041,-0.0142,-0.0037};
double sq = sqrt(1.0+Ci[i]*Ci[i]);
aa_cs[i] = DOUBLE_TO_REAL(1.0/sq);
aa_ca[i] = DOUBLE_TO_REAL(Ci[i]/sq);
}
for(i=0;i<18;i++)
{
win[0][i] = win[1][i] =
DOUBLE_TO_REAL( 0.5*sin(M_PI/72.0 * (double)(2*(i+0) +1)) / cos(M_PI * (double)(2*(i+0) +19) / 72.0) );
win[0][i+18] = win[3][i+18] =
DOUBLE_TO_REAL( 0.5*sin(M_PI/72.0 * (double)(2*(i+18)+1)) / cos(M_PI * (double)(2*(i+18)+19) / 72.0) );
}
for(i=0;i<6;i++)
{
win[1][i+18] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+18)+19) / 72.0 ));
win[3][i+12] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (2*(i+12)+19) / 72.0 ));
win[1][i+24] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+13) ) / cos ( M_PI * (double) (2*(i+24)+19) / 72.0 ));
win[1][i+30] = win[3][i] = DOUBLE_TO_REAL(0.0);
win[3][i+6 ] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+1 ) ) / cos ( M_PI * (double) (2*(i+6 )+19) / 72.0 ));
}
for(i=0;i<9;i++)
COS9[i] = DOUBLE_TO_REAL(cos( M_PI / 18.0 * (double) i));
for(i=0;i<9;i++)
tfcos36[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 36.0 ));
for(i=0;i<3;i++)
tfcos12[i] = DOUBLE_TO_REAL(0.5 / cos ( M_PI * (double) (i*2+1) / 12.0 ));
COS6_1 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 1));
COS6_2 = DOUBLE_TO_REAL(cos( M_PI / 6.0 * (double) 2));
#ifdef NEW_DCT9
cos9[0] = DOUBLE_TO_REAL(cos(1.0*M_PI/9.0));
cos9[1] = DOUBLE_TO_REAL(cos(5.0*M_PI/9.0));
cos9[2] = DOUBLE_TO_REAL(cos(7.0*M_PI/9.0));
cos18[0] = DOUBLE_TO_REAL(cos(1.0*M_PI/18.0));
cos18[1] = DOUBLE_TO_REAL(cos(11.0*M_PI/18.0));
cos18[2] = DOUBLE_TO_REAL(cos(13.0*M_PI/18.0));
#endif
for(i=0;i<12;i++)
{
win[2][i] = DOUBLE_TO_REAL(0.5 * sin( M_PI / 24.0 * (double) (2*i+1) ) / cos ( M_PI * (double) (2*i+7) / 24.0 ));
}
for(i=0;i<16;i++)
{
double t = tan( (double) i * M_PI / 12.0 );
tan1_1[i] = DOUBLE_TO_REAL_15(t / (1.0+t));
tan2_1[i] = DOUBLE_TO_REAL_15(1.0 / (1.0 + t));
tan1_2[i] = DOUBLE_TO_REAL_15(M_SQRT2 * t / (1.0+t));
tan2_2[i] = DOUBLE_TO_REAL_15(M_SQRT2 / (1.0 + t));
}
for(i=0;i<32;i++)
{
for(j=0;j<2;j++)
{
double base = pow(2.0,-0.25*(j+1.0));
double p1=1.0,p2=1.0;
if(i > 0)
{
if( i & 1 ) p1 = pow(base,(i+1.0)*0.5);
else p2 = pow(base,i*0.5);
}
pow1_1[j][i] = DOUBLE_TO_REAL_15(p1);
pow2_1[j][i] = DOUBLE_TO_REAL_15(p2);
pow1_2[j][i] = DOUBLE_TO_REAL_15(M_SQRT2 * p1);
pow2_2[j][i] = DOUBLE_TO_REAL_15(M_SQRT2 * p2);
}
}
#endif
for(j=0;j<4;j++)
{
const int len[4] = { 36,36,12,36 };
for(i=0;i<len[j];i+=2) win1[j][i] = + win[j][i];
for(i=1;i<len[j];i+=2) win1[j][i] = - win[j][i];
}
for(j=0;j<9;j++)
{
const struct bandInfoStruct *bi = &bandInfo[j];
int *mp;
int cb,lwin;
const unsigned char *bdf;
int switch_idx;
mp = map[j][0] = mapbuf0[j];
bdf = bi->longDiff;
switch_idx = (j < 3) ? 8 : 6;
for(i=0,cb = 0; cb < switch_idx ; cb++,i+=*bdf++)
{
*mp++ = (*bdf) >> 1;
*mp++ = i;
*mp++ = 3;
*mp++ = cb;
}
bdf = bi->shortDiff+3;
for(cb=3;cb<13;cb++)
{
int l = (*bdf++) >> 1;
for(lwin=0;lwin<3;lwin++)
{
*mp++ = l;
*mp++ = i + lwin;
*mp++ = lwin;
*mp++ = cb;
}
i += 6*l;
}
mapend[j][0] = mp;
mp = map[j][1] = mapbuf1[j];
bdf = bi->shortDiff+0;
for(i=0,cb=0;cb<13;cb++)
{
int l = (*bdf++) >> 1;
for(lwin=0;lwin<3;lwin++)
{
*mp++ = l;
*mp++ = i + lwin;
*mp++ = lwin;
*mp++ = cb;
}
i += 6*l;
}
mapend[j][1] = mp;
mp = map[j][2] = mapbuf2[j];
bdf = bi->longDiff;
for(cb = 0; cb < 22 ; cb++)
{
*mp++ = (*bdf++) >> 1;
*mp++ = cb;
}
mapend[j][2] = mp;
}
/* Now for some serious loopings! */
for(i=0;i<5;i++)
for(j=0;j<6;j++)
for(k=0;k<6;k++)
{
int n = k + j * 6 + i * 36;
i_slen2[n] = i|(j<<3)|(k<<6)|(3<<12);
}
for(i=0;i<4;i++)
for(j=0;j<4;j++)
for(k=0;k<4;k++)
{
int n = k + j * 4 + i * 16;
i_slen2[n+180] = i|(j<<3)|(k<<6)|(4<<12);
}
for(i=0;i<4;i++)
for(j=0;j<3;j++)
{
int n = j + i * 3;
i_slen2[n+244] = i|(j<<3) | (5<<12);
n_slen2[n+500] = i|(j<<3) | (2<<12) | (1<<15);
}
for(i=0;i<5;i++)
for(j=0;j<5;j++)
for(k=0;k<4;k++)
for(l=0;l<4;l++)
{
int n = l + k * 4 + j * 16 + i * 80;
n_slen2[n] = i|(j<<3)|(k<<6)|(l<<9)|(0<<12);
}
for(i=0;i<5;i++)
for(j=0;j<5;j++)
for(k=0;k<4;k++)
{
int n = k + j * 4 + i * 20;
n_slen2[n+400] = i|(j<<3)|(k<<6)|(1<<12);
}
}
void init_layer3_stuff(mpg123_handle *fr, real (*gainpow2)(mpg123_handle *fr, int i))
{
int i,j;
for(i=-256;i<118+4;i++) fr->gainpow2[i+256] = gainpow2(fr,i);
for(j=0;j<9;j++)
{
for(i=0;i<23;i++)
{
fr->longLimit[j][i] = (bandInfo[j].longIdx[i] - 1 + 8) / 18 + 1;
if(fr->longLimit[j][i] > (fr->down_sample_sblimit) )
fr->longLimit[j][i] = fr->down_sample_sblimit;
}
for(i=0;i<14;i++)
{
fr->shortLimit[j][i] = (bandInfo[j].shortIdx[i] - 1) / 18 + 1;
if(fr->shortLimit[j][i] > (fr->down_sample_sblimit) )
fr->shortLimit[j][i] = fr->down_sample_sblimit;
}
}
}
/*
Observe!
Now come the actualy decoding routines.
*/
/* read additional side information (for MPEG 1 and MPEG 2) */
static int III_get_side_info(mpg123_handle *fr, struct III_sideinfo *si,int stereo, int ms_stereo,long sfreq,int single)
{
int ch, gr;
int powdiff = (single == SINGLE_MIX) ? 4 : 0;
const int tabs[2][5] = { { 2,9,5,3,4 } , { 1,8,1,2,9 } };
const int *tab = tabs[fr->lsf];
si->main_data_begin = getbits(fr, tab[1]);
if(si->main_data_begin > fr->bitreservoir)
{
if(!fr->to_ignore && VERBOSE2) fprintf(stderr, "Note: missing %d bytes in bit reservoir for frame %li\n", (int)(si->main_data_begin - fr->bitreservoir), (long)fr->num);
/* overwrite main_data_begin for the really available bit reservoir */
backbits(fr, tab[1]);
if(fr->lsf == 0)
{
fr->wordpointer[0] = (unsigned char) (fr->bitreservoir >> 1);
fr->wordpointer[1] = (unsigned char) ((fr->bitreservoir & 1) << 7);
}
else fr->wordpointer[0] = (unsigned char) fr->bitreservoir;
/* zero "side-info" data for a silence-frame
without touching audio data used as bit reservoir for following frame */
memset(fr->wordpointer+2, 0, fr->ssize-2);
/* reread the new bit reservoir offset */
si->main_data_begin = getbits(fr, tab[1]);
}
/* Keep track of the available data bytes for the bit reservoir.
Think: Substract the 2 crc bytes in parser already? */
fr->bitreservoir = fr->bitreservoir + fr->framesize - fr->ssize - (fr->error_protection ? 2 : 0);
/* Limit the reservoir to the max for MPEG 1.0 or 2.x . */
if(fr->bitreservoir > (unsigned int) (fr->lsf == 0 ? 511 : 255))
fr->bitreservoir = (fr->lsf == 0 ? 511 : 255);
/* Now back into less commented territory. It's code. It works. */
if (stereo == 1)
si->private_bits = getbits_fast(fr, tab[2]);
else
si->private_bits = getbits_fast(fr, tab[3]);
if(!fr->lsf) for(ch=0; ch<stereo; ch++)
{
si->ch[ch].gr[0].scfsi = -1;
si->ch[ch].gr[1].scfsi = getbits_fast(fr, 4);
}
for (gr=0; gr<tab[0]; gr++)
for (ch=0; ch<stereo; ch++)
{
register struct gr_info_s *gr_info = &(si->ch[ch].gr[gr]);
gr_info->part2_3_length = getbits(fr, 12);
gr_info->big_values = getbits(fr, 9);
if(gr_info->big_values > 288)
{
if(NOQUIET) error("big_values too large!");
gr_info->big_values = 288;
}
gr_info->pow2gain = fr->gainpow2+256 - getbits_fast(fr, 8) + powdiff;
if(ms_stereo) gr_info->pow2gain += 2;
gr_info->scalefac_compress = getbits(fr, tab[4]);
if(gr_info->part2_3_length == 0)
{
if(gr_info->scalefac_compress > 0)
debug1( "scalefac_compress _should_ be zero instead of %i"
, gr_info->scalefac_compress );
gr_info->scalefac_compress = 0;
}
if(get1bit(fr))
{ /* window switch flag */
int i;
gr_info->block_type = getbits_fast(fr, 2);
gr_info->mixed_block_flag = get1bit(fr);
gr_info->table_select[0] = getbits_fast(fr, 5);
gr_info->table_select[1] = getbits_fast(fr, 5);
/*
table_select[2] not needed, because there is no region2,
but to satisfy some verification tools we set it either.
*/
gr_info->table_select[2] = 0;
for(i=0;i<3;i++)
gr_info->full_gain[i] = gr_info->pow2gain + (getbits_fast(fr, 3)<<3);
if(gr_info->block_type == 0)
{
if(NOQUIET) error("Blocktype == 0 and window-switching == 1 not allowed.");
return 1;
}
/* region_count/start parameters are implicit in this case. */
if( (!fr->lsf || (gr_info->block_type == 2)) && !fr->mpeg25)
{
gr_info->region1start = 36>>1;
gr_info->region2start = 576>>1;
}
else
{
if(fr->mpeg25)
{
int r0c,r1c;
if((gr_info->block_type == 2) && (!gr_info->mixed_block_flag) ) r0c = 5;
else r0c = 7;
/* r0c+1+r1c+1 == 22, always. */
r1c = 20 - r0c;
gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
}
else
{
gr_info->region1start = 54>>1;
gr_info->region2start = 576>>1;
}
}
}
else
{
int i,r0c,r1c;
for (i=0; i<3; i++)
gr_info->table_select[i] = getbits_fast(fr, 5);
r0c = getbits_fast(fr, 4); /* 0 .. 15 */
r1c = getbits_fast(fr, 3); /* 0 .. 7 */
gr_info->region1start = bandInfo[sfreq].longIdx[r0c+1] >> 1 ;
/* max(r0c+r1c+2) = 15+7+2 = 24 */
if(r0c+1+r1c+1 > 22) gr_info->region2start = 576>>1;
else gr_info->region2start = bandInfo[sfreq].longIdx[r0c+1+r1c+1] >> 1;
gr_info->block_type = 0;
gr_info->mixed_block_flag = 0;
}
if(!fr->lsf) gr_info->preflag = get1bit(fr);
gr_info->scalefac_scale = get1bit(fr);
gr_info->count1table_select = get1bit(fr);
}
return 0;
}
/* read scalefactors */
static int III_get_scale_factors_1(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int ch,int gr)
{
const unsigned char slen[2][16] =
{
{0, 0, 0, 0, 3, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4},
{0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3, 1, 2, 3, 2, 3}
};
int numbits;
int num0 = slen[0][gr_info->scalefac_compress];
int num1 = slen[1][gr_info->scalefac_compress];
if(gr_info->part2_3_length == 0)
{
int i;
for(i=0;i<39;i++)
*scf++ = 0;
return 0;
}
if(gr_info->block_type == 2)
{
int i=18;
numbits = (num0 + num1) * 18;
if(gr_info->mixed_block_flag)
{
for (i=8;i;i--)
*scf++ = getbits_fast(fr, num0);
i = 9;
numbits -= num0; /* num0 * 17 + num1 * 18 */
}
for(;i;i--) *scf++ = getbits_fast(fr, num0);
for(i = 18; i; i--) *scf++ = getbits_fast(fr, num1);
*scf++ = 0; *scf++ = 0; *scf++ = 0; /* short[13][0..2] = 0 */
}
else
{
int i;
int scfsi = gr_info->scfsi;
if(scfsi < 0)
{ /* scfsi < 0 => granule == 0 */
for(i=11;i;i--) *scf++ = getbits_fast(fr, num0);
for(i=10;i;i--) *scf++ = getbits_fast(fr, num1);
numbits = (num0 + num1) * 10 + num0;
*scf++ = 0;
}
else
{
numbits = 0;
if(!(scfsi & 0x8))
{
for (i=0;i<6;i++) *scf++ = getbits_fast(fr, num0);
numbits += num0 * 6;
}
else scf += 6;
if(!(scfsi & 0x4))
{
for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num0);
numbits += num0 * 5;
}
else scf += 5;
if(!(scfsi & 0x2))
{
for(i=0;i<5;i++) *scf++ = getbits_fast(fr, num1);
numbits += num1 * 5;
}
else scf += 5;
if(!(scfsi & 0x1))
{
for (i=0;i<5;i++) *scf++ = getbits_fast(fr, num1);
numbits += num1 * 5;
}
else scf += 5;
*scf++ = 0; /* no l[21] in original sources */
}
}
return numbits;
}
static int III_get_scale_factors_2(mpg123_handle *fr, int *scf,struct gr_info_s *gr_info,int i_stereo)
{
const unsigned char *pnt;
int i,j,n=0,numbits=0;
unsigned int slen;
const unsigned char stab[3][6][4] =
{
{
{ 6, 5, 5,5 } , { 6, 5, 7,3 } , { 11,10,0,0},
{ 7, 7, 7,0 } , { 6, 6, 6,3 } , { 8, 8,5,0}
},
{
{ 9, 9, 9,9 } , { 9, 9,12,6 } , { 18,18,0,0},
{12,12,12,0 } , {12, 9, 9,6 } , { 15,12,9,0}
},
{
{ 6, 9, 9,9 } , { 6, 9,12,6 } , { 15,18,0,0},
{ 6,15,12,0 } , { 6,12, 9,6 } , { 6,18,9,0}
}
};
if(i_stereo) /* i_stereo AND second channel -> do_layer3() checks this */
slen = i_slen2[gr_info->scalefac_compress>>1];
else
slen = n_slen2[gr_info->scalefac_compress];
gr_info->preflag = (slen>>15) & 0x1;
n = 0;
if( gr_info->block_type == 2 )
{
n++;
if(gr_info->mixed_block_flag) n++;
}
pnt = stab[n][(slen>>12)&0x7];
if(gr_info->part2_3_length == 0)
{
int i;
for(i=0;i<39;i++)
*scf++ = 0;
return 0;
}
for(i=0;i<4;i++)
{
int num = slen & 0x7;
slen >>= 3;
if(num)
{
for(j=0;j<(int)(pnt[i]);j++) *scf++ = getbits_fast(fr, num);
numbits += pnt[i] * num;
}
else
for(j=0;j<(int)(pnt[i]);j++) *scf++ = 0;
}
n = (n << 1) + 1;
for(i=0;i<n;i++) *scf++ = 0;
return numbits;
}
static unsigned char pretab_choice[2][22] =
{
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,2,2,3,3,3,2,0}
};
/*
Dequantize samples
...includes Huffman decoding
*/
/* 24 is enough because tab13 has max. a 19 bit huffvector */
/* The old code played games with shifting signed integers around in not quite */
/* legal ways. Also, it used long where just 32 bits are required. This could */
/* be good or bad on 64 bit architectures ... anyway, making clear that */
/* 32 bits suffice is a benefit. */
#if 0
/* To reconstruct old code, use this: */
#define MASK_STYPE long
#define MASK_UTYPE unsigned long
#define MASK_TYPE MASK_STYPE
#define MSB_MASK (mask < 0)
#else
/* This should be more proper: */
#define MASK_STYPE int32_t
#define MASK_UTYPE uint32_t
#define MASK_TYPE MASK_UTYPE
#define MSB_MASK ((MASK_UTYPE)mask & (MASK_UTYPE)1<<(sizeof(MASK_TYPE)*8-1))
#endif
#define BITSHIFT ((sizeof(MASK_TYPE)-1)*8)
#define REFRESH_MASK \
while(num < BITSHIFT) { \
mask |= ((MASK_UTYPE)getbyte(fr))<<(BITSHIFT-num); \
num += 8; \
part2remain -= 8; }
/* Complicated way of checking for msb value. This used to be (mask < 0). */
static int III_dequantize_sample(mpg123_handle *fr, real xr[SBLIMIT][SSLIMIT],int *scf, struct gr_info_s *gr_info,int sfreq,int part2bits)
{
int shift = 1 + gr_info->scalefac_scale;
real *xrpnt = (real *) xr;
int l[3],l3;
int part2remain = gr_info->part2_3_length - part2bits;
int *me;
#ifdef REAL_IS_FIXED
int gainpow2_scale_idx = 378;
#endif
/* Assumption: If there is some part2_3_length at all, there should be
enough of it to work with properly. In case of zero length we silently
zero things. */
if(gr_info->part2_3_length > 0)
{
/* mhipp tree has this split up a bit... */
int num=getbitoffset(fr);
MASK_TYPE mask;
/* We must split this, because for num==0 the shift is undefined if you do it in one step. */
mask = ((MASK_UTYPE) getbits(fr, num))<<BITSHIFT;
mask <<= 8-num;
part2remain -= num;
{
int bv = gr_info->big_values;
int region1 = gr_info->region1start;
int region2 = gr_info->region2start;
l3 = ((576>>1)-bv)>>1;
/* we may lose the 'odd' bit here !! check this later again */
if(bv <= region1)
{
l[0] = bv;
l[1] = 0;
l[2] = 0;
}
else
{
l[0] = region1;
if(bv <= region2)
{
l[1] = bv - l[0];
l[2] = 0;
}
else
{
l[1] = region2 - l[0];
l[2] = bv - region2;
}
}
}
#define CHECK_XRPNT if(xrpnt >= &xr[SBLIMIT][0]) \
{ \
if(NOQUIET) \
error2("attempted xrpnt overflow (%p !< %p)", (void*) xrpnt, (void*) &xr[SBLIMIT][0]); \
return 1; \
}
if(gr_info->block_type == 2)
{
/* decoding with short or mixed mode BandIndex table */
int i,max[4];
int step=0,lwin=3,cb=0;
register real v = 0.0;
register int *m,mc;
if(gr_info->mixed_block_flag)
{
max[3] = -1;
max[0] = max[1] = max[2] = 2;
m = map[sfreq][0];
me = mapend[sfreq][0];
}
else
{
max[0] = max[1] = max[2] = max[3] = -1;
/* max[3] not really needed in this case */
m = map[sfreq][1];
me = mapend[sfreq][1];
}
mc = 0;
for(i=0;i<2;i++)
{
int lp = l[i];
const struct newhuff *h = ht+gr_info->table_select[i];
for(;lp;lp--,mc--)
{
register MASK_STYPE x,y;
if( (!mc) )
{
mc = *m++;
xrpnt = ((real *) xr) + (*m++);
lwin = *m++;
cb = *m++;
if(lwin == 3)
{
#ifdef REAL_IS_FIXED
gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
#endif
v = gr_info->pow2gain[(*scf++) << shift];
step = 1;
}
else
{
#ifdef REAL_IS_FIXED
gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2);
#endif
v = gr_info->full_gain[lwin][(*scf++) << shift];
step = 3;
}
}
{
const short *val = h->table;
REFRESH_MASK;
#ifdef USE_NEW_HUFFTABLE
while((y=val[(MASK_UTYPE)mask>>(BITSHIFT+4)])<0)
{
val -= y;
num -= 4;
mask <<= 4;
}
num -= (y >> 8);
mask <<= (y >> 8);
x = (y >> 4) & 0xf;
y &= 0xf;
#else
while((y=*val++)<0)
{
if (MSB_MASK) val -= y;
num--;
mask <<= 1;
}
x = y >> 4;
y &= 0xf;
#endif
}
CHECK_XRPNT;
if(x == 15 && h->linbits)
{
max[lwin] = cb;
REFRESH_MASK;
x += ((MASK_UTYPE) mask) >> (BITSHIFT+8-h->linbits);
num -= h->linbits+1;
mask <<= h->linbits;
if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
mask <<= 1;
}
else if(x)
{
max[lwin] = cb;
if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[x], v, gainpow2_scale_idx);
else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[x], v, gainpow2_scale_idx);
num--;
mask <<= 1;
}
else *xrpnt = DOUBLE_TO_REAL(0.0);
xrpnt += step;
CHECK_XRPNT;
if(y == 15 && h->linbits)
{
max[lwin] = cb;
REFRESH_MASK;
y += ((MASK_UTYPE) mask) >> (BITSHIFT+8-h->linbits);
num -= h->linbits+1;
mask <<= h->linbits;
if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
mask <<= 1;
}
else if(y)
{
max[lwin] = cb;
if(MSB_MASK) *xrpnt = REAL_MUL_SCALE_LAYER3(-ispow[y], v, gainpow2_scale_idx);
else *xrpnt = REAL_MUL_SCALE_LAYER3( ispow[y], v, gainpow2_scale_idx);
num--;
mask <<= 1;
}
else *xrpnt = DOUBLE_TO_REAL(0.0);
xrpnt += step;
}
}
for(;l3 && (part2remain+num > 0);l3--)
{
const struct newhuff* h;
const short* val;
register short a;
h = htc+gr_info->count1table_select;
val = h->table;
REFRESH_MASK;
while((a=*val++)<0)
{
if(MSB_MASK) val -= a;
num--;
mask <<= 1;
}
if(part2remain+num <= 0)
{
num -= part2remain+num;
break;
}
for(i=0;i<4;i++)
{
if(!(i & 1))
{
if(!mc)
{
mc = *m++;
xrpnt = ((real *) xr) + (*m++);
lwin = *m++;
cb = *m++;
if(lwin == 3)
{
#ifdef REAL_IS_FIXED
gainpow2_scale_idx = (int)(gr_info->pow2gain + (*scf << shift) - fr->gainpow2);
#endif
v = gr_info->pow2gain[(*scf++) << shift];
step = 1;
}
else
{
#ifdef REAL_IS_FIXED
gainpow2_scale_idx = (int)(gr_info->full_gain[lwin] + (*scf << shift) - fr->gainpow2);
#endif
v = gr_info->full_gain[lwin][(*scf++) << shift];
step = 3;
}
}
mc--;
}
CHECK_XRPNT;
if( (a & (0x8>>i)) )
{
max[lwin] = cb;