e5f1869205803f1f4c431fdfd7dca953521e4a43
[reactos.git] / reactos / sdk / lib / 3rdparty / libmpg123 / layer1.c
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 */
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
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 static void I_step_two(real fraction[2][SBLIMIT],unsigned int balloc[2*SBLIMIT], unsigned int scale_index[2][SBLIMIT],mpg123_handle *fr)
88 {
89 int i,n;
90 int smpb[2*SBLIMIT]; /* values: 0-65535 */
91 int *sample;
92 register unsigned int *ba;
93 register unsigned int *sca = (unsigned int *) scale_index;
94
95 if(fr->stereo == 2)
96 {
97 int jsbound = fr->jsbound;
98 register real *f0 = fraction[0];
99 register real *f1 = fraction[1];
100 ba = balloc;
101 for(sample=smpb,i=0;i<jsbound;i++)
102 {
103 if((n = *ba++)) *sample++ = getbits(fr, n+1);
104
105 if((n = *ba++)) *sample++ = getbits(fr, n+1);
106 }
107 for(i=jsbound;i<SBLIMIT;i++)
108 if((n = *ba++))
109 *sample++ = getbits(fr, n+1);
110
111 ba = balloc;
112 for(sample=smpb,i=0;i<jsbound;i++)
113 {
114 if((n=*ba++))
115 *f0++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15( ((-1)<<n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
116 else *f0++ = DOUBLE_TO_REAL(0.0);
117
118 if((n=*ba++))
119 *f1++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15( ((-1)<<n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
120 else *f1++ = DOUBLE_TO_REAL(0.0);
121 }
122 for(i=jsbound;i<SBLIMIT;i++)
123 {
124 if((n=*ba++))
125 {
126 real samp = DOUBLE_TO_REAL_15( ((-1)<<n) + (*sample++) + 1);
127 *f0++ = REAL_MUL_SCALE_LAYER12(samp, fr->muls[n+1][*sca++]);
128 *f1++ = REAL_MUL_SCALE_LAYER12(samp, fr->muls[n+1][*sca++]);
129 }
130 else *f0++ = *f1++ = DOUBLE_TO_REAL(0.0);
131 }
132 for(i=fr->down_sample_sblimit;i<32;i++)
133 fraction[0][i] = fraction[1][i] = 0.0;
134 }
135 else
136 {
137 register real *f0 = fraction[0];
138 ba = balloc;
139 for(sample=smpb,i=0;i<SBLIMIT;i++)
140 if ((n = *ba++))
141 *sample++ = getbits(fr, n+1);
142
143 ba = balloc;
144 for(sample=smpb,i=0;i<SBLIMIT;i++)
145 {
146 if((n=*ba++))
147 *f0++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15( ((-1)<<n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
148 else *f0++ = DOUBLE_TO_REAL(0.0);
149 }
150 for(i=fr->down_sample_sblimit;i<32;i++)
151 fraction[0][i] = DOUBLE_TO_REAL(0.0);
152 }
153 }
154
155 int do_layer1(mpg123_handle *fr)
156 {
157 int clip=0;
158 int i,stereo = fr->stereo;
159 unsigned int balloc[2*SBLIMIT];
160 unsigned int scale_index[2][SBLIMIT];
161 real (*fraction)[SBLIMIT] = fr->layer1.fraction; /* fraction[2][SBLIMIT] */
162 int single = fr->single;
163
164 fr->jsbound = (fr->mode == MPG_MD_JOINT_STEREO) ? (fr->mode_ext<<2)+4 : 32;
165
166 if(stereo == 1 || single == SINGLE_MIX) /* I don't see mixing handled here */
167 single = SINGLE_LEFT;
168
169 if(I_step_one(balloc,scale_index,fr))
170 {
171 if(NOQUIET) error("Aborting layer I decoding after step one.\n");
172 return clip;
173 }
174
175 for(i=0;i<SCALE_BLOCK;i++)
176 {
177 I_step_two(fraction,balloc,scale_index,fr);
178
179 if(single != SINGLE_STEREO)
180 clip += (fr->synth_mono)(fraction[single], fr);
181 else
182 clip += (fr->synth_stereo)(fraction[0], fraction[1], fr);
183 }
184
185 return clip;
186 }
187
188