1 /*
2 layer1.c: the layer 1 decoder
3
4 copyright 1995-2009 by the mpg123 project - free software under the terms of the LGPL 2.1
5 see COPYING and AUTHORS files in distribution or http://mpg123.org
6 initially written by Michael Hipp
7
8 may have a few bugs after last optimization ...
9 */
10
11 #include "mpg123lib_intern.h"
12 #include "getbits.h"
13 #include "debug.h"
14
15 /*
16 Allocation value is not allowed to be 15. Initially, libmad showed me the
17 error that mpg123 used to ignore. Then, I found a quote on that in
18 Shlien, S. (1994): Guide to MPEG-1 Audio Standard.
19 IEEE Transactions on Broadcasting 40, 4
20
21 "To avoid conflicts with the synchronization code, code '1111' is defined
22 to be illegal."
23 */
check_balloc(mpg123_handle * fr,unsigned int * balloc,unsigned int * end)24 static int check_balloc(mpg123_handle *fr, unsigned int *balloc, unsigned int *end)
25 {
26 unsigned int *ba;
27 for(ba=balloc; ba != end; ++ba)
28 if(*ba == 15)
29 {
30 if(NOQUIET) error("Illegal bit allocation value.");
31 return -1;
32 }
33
34 return 0;
35 }
36
I_step_one(unsigned int balloc[],unsigned int scale_index[2][SBLIMIT],mpg123_handle * fr)37 static int I_step_one(unsigned int balloc[], unsigned int scale_index[2][SBLIMIT],mpg123_handle *fr)
38 {
39 unsigned int *ba=balloc;
40 unsigned int *sca = (unsigned int *) scale_index;
41
42 if(fr->stereo == 2)
43 {
44 int i;
45 int jsbound = fr->jsbound;
46 for(i=0;i<jsbound;i++)
47 {
48 *ba++ = getbits(fr, 4);
49 *ba++ = getbits(fr, 4);
50 }
51 for(i=jsbound;i<SBLIMIT;i++) *ba++ = getbits(fr, 4);
52
53 if(check_balloc(fr, balloc, ba)) return -1;
54
55 ba = balloc;
56
57 for(i=0;i<jsbound;i++)
58 {
59 if ((*ba++))
60 *sca++ = getbits(fr, 6);
61 if ((*ba++))
62 *sca++ = getbits(fr, 6);
63 }
64 for (i=jsbound;i<SBLIMIT;i++)
65 if((*ba++))
66 {
67 *sca++ = getbits(fr, 6);
68 *sca++ = getbits(fr, 6);
69 }
70 }
71 else
72 {
73 int i;
74 for(i=0;i<SBLIMIT;i++) *ba++ = getbits(fr, 4);
75
76 if(check_balloc(fr, balloc, ba)) return -1;
77
78 ba = balloc;
79 for (i=0;i<SBLIMIT;i++)
80 if ((*ba++))
81 *sca++ = getbits(fr, 6);
82 }
83
84 return 0;
85 }
86
87 /* Something sane in place of undefined (-1)<<n. Well, not really. */
88 #define MINUS_SHIFT(n) ( (int)(((unsigned int)-1)<<(n)) )
89
I_step_two(real fraction[2][SBLIMIT],unsigned int balloc[2* SBLIMIT],unsigned int scale_index[2][SBLIMIT],mpg123_handle * fr)90 static void I_step_two(real fraction[2][SBLIMIT],unsigned int balloc[2*SBLIMIT], unsigned int scale_index[2][SBLIMIT],mpg123_handle *fr)
91 {
92 int i,n;
93 int smpb[2*SBLIMIT]; /* values: 0-65535 */
94 int *sample;
95 register unsigned int *ba;
96 register unsigned int *sca = (unsigned int *) scale_index;
97
98 if(fr->stereo == 2)
99 {
100 int jsbound = fr->jsbound;
101 register real *f0 = fraction[0];
102 register real *f1 = fraction[1];
103 ba = balloc;
104 for(sample=smpb,i=0;i<jsbound;i++)
105 {
106 if((n = *ba++)) *sample++ = getbits(fr, n+1);
107
108 if((n = *ba++)) *sample++ = getbits(fr, n+1);
109 }
110 for(i=jsbound;i<SBLIMIT;i++)
111 if((n = *ba++))
112 *sample++ = getbits(fr, n+1);
113
114 ba = balloc;
115 for(sample=smpb,i=0;i<jsbound;i++)
116 {
117 if((n=*ba++))
118 *f0++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
119 else *f0++ = DOUBLE_TO_REAL(0.0);
120
121 if((n=*ba++))
122 *f1++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
123 else *f1++ = DOUBLE_TO_REAL(0.0);
124 }
125 for(i=jsbound;i<SBLIMIT;i++)
126 {
127 if((n=*ba++))
128 {
129 real samp = DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1);
130 *f0++ = REAL_MUL_SCALE_LAYER12(samp, fr->muls[n+1][*sca++]);
131 *f1++ = REAL_MUL_SCALE_LAYER12(samp, fr->muls[n+1][*sca++]);
132 }
133 else *f0++ = *f1++ = DOUBLE_TO_REAL(0.0);
134 }
135 for(i=fr->down_sample_sblimit;i<32;i++)
136 fraction[0][i] = fraction[1][i] = 0.0;
137 }
138 else
139 {
140 register real *f0 = fraction[0];
141 ba = balloc;
142 for(sample=smpb,i=0;i<SBLIMIT;i++)
143 if ((n = *ba++))
144 *sample++ = getbits(fr, n+1);
145
146 ba = balloc;
147 for(sample=smpb,i=0;i<SBLIMIT;i++)
148 {
149 if((n=*ba++))
150 *f0++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
151 else *f0++ = DOUBLE_TO_REAL(0.0);
152 }
153 for(i=fr->down_sample_sblimit;i<32;i++)
154 fraction[0][i] = DOUBLE_TO_REAL(0.0);
155 }
156 }
157
do_layer1(mpg123_handle * fr)158 int do_layer1(mpg123_handle *fr)
159 {
160 int clip=0;
161 int i,stereo = fr->stereo;
162 unsigned int balloc[2*SBLIMIT];
163 unsigned int scale_index[2][SBLIMIT];
164 real (*fraction)[SBLIMIT] = fr->layer1.fraction; /* fraction[2][SBLIMIT] */
165 int single = fr->single;
166
167 fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : 32;
168
169 if(stereo == 1 || single == SINGLE_MIX) /* I don't see mixing handled here */
170 single = SINGLE_LEFT;
171
172 if(I_step_one(balloc,scale_index,fr))
173 {
174 if(NOQUIET) error("Aborting layer I decoding after step one.\n");
175 return clip;
176 }
177
178 for(i=0;i<SCALE_BLOCK;i++)
179 {
180 I_step_two(fraction,balloc,scale_index,fr);
181
182 if(single != SINGLE_STEREO)
183 clip += (fr->synth_mono)(fraction[single], fr);
184 else
185 clip += (fr->synth_stereo)(fraction[0], fraction[1], fr);
186 }
187
188 return clip;
189 }
190
191
192