Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | pj | 1 | /* |
2 | * Mpeg Layer-2 audio decoder |
||
3 | * -------------------------- |
||
4 | * copyright (c) 1995 by Michael Hipp, All rights reserved. See also 'README' |
||
5 | * |
||
6 | */ |
||
7 | |||
8 | #include "mpg123.h" |
||
9 | |||
10 | static int grp_3tab[32 * 3] = { 0, }; /* used: 27 */ |
||
11 | static int grp_5tab[128 * 3] = { 0, }; /* used: 125 */ |
||
12 | static int grp_9tab[1024 * 3] = { 0, }; /* used: 729 */ |
||
13 | |||
14 | real muls[27][64]; /* also used by layer 1 */ |
||
15 | |||
16 | void init_layer2(void) |
||
17 | { |
||
18 | static double mulmul[27] = { |
||
19 | 0.0 , -2.0/3.0 , 2.0/3.0 , |
||
20 | 2.0/7.0 , 2.0/15.0 , 2.0/31.0, 2.0/63.0 , 2.0/127.0 , 2.0/255.0 , |
||
21 | 2.0/511.0 , 2.0/1023.0 , 2.0/2047.0 , 2.0/4095.0 , 2.0/8191.0 , |
||
22 | 2.0/16383.0 , 2.0/32767.0 , 2.0/65535.0 , |
||
23 | -4.0/5.0 , -2.0/5.0 , 2.0/5.0, 4.0/5.0 , |
||
24 | -8.0/9.0 , -4.0/9.0 , -2.0/9.0 , 2.0/9.0 , 4.0/9.0 , 8.0/9.0 }; |
||
25 | static int base[3][9] = { |
||
26 | { 1 , 0, 2 , } , |
||
27 | { 17, 18, 0 , 19, 20 , } , |
||
28 | { 21, 1, 22, 23, 0, 24, 25, 2, 26 } }; |
||
29 | int i,j,k,l,len; |
||
30 | real *table; |
||
31 | static int tablen[3] = { 3 , 5 , 9 }; |
||
32 | static int *itable,*tables[3] = { grp_3tab , grp_5tab , grp_9tab }; |
||
33 | |||
34 | for(i=0;i<3;i++) |
||
35 | { |
||
36 | itable = tables[i]; |
||
37 | len = tablen[i]; |
||
38 | for(j=0;j<len;j++) |
||
39 | for(k=0;k<len;k++) |
||
40 | for(l=0;l<len;l++) |
||
41 | { |
||
42 | *itable++ = base[i][l]; |
||
43 | *itable++ = base[i][k]; |
||
44 | *itable++ = base[i][j]; |
||
45 | } |
||
46 | } |
||
47 | |||
48 | for(k=0;k<27;k++) |
||
49 | { |
||
50 | double m=mulmul[k]; |
||
51 | table = muls[k]; |
||
52 | for(j=3,i=0;i<63;i++,j--) |
||
53 | *table++ = m * pow(2.0,(double) j / 3.0); |
||
54 | *table++ = 0.0; |
||
55 | } |
||
56 | } |
||
57 | |||
58 | |||
59 | void II_step_one(unsigned int *bit_alloc,int *scale,struct frame *fr) |
||
60 | { |
||
61 | int stereo = fr->stereo-1; |
||
62 | int sblimit = fr->II_sblimit; |
||
63 | int jsbound = fr->jsbound; |
||
64 | int sblimit2 = fr->II_sblimit<<stereo; |
||
65 | struct al_table *alloc1 = fr->alloc; |
||
66 | int i; |
||
67 | static unsigned int scfsi_buf[64]; |
||
68 | unsigned int *scfsi,*bita; |
||
69 | int sc,step; |
||
70 | |||
71 | bita = bit_alloc; |
||
72 | if(stereo) |
||
73 | { |
||
74 | for (i=jsbound;i;i--,alloc1+=(1<<step)) |
||
75 | { |
||
76 | *bita++ = (char) getbits(step=alloc1->bits); |
||
77 | *bita++ = (char) getbits(step); |
||
78 | } |
||
79 | for (i=sblimit-jsbound;i;i--,alloc1+=(1<<step)) |
||
80 | { |
||
81 | bita[0] = (char) getbits(step=alloc1->bits); |
||
82 | bita[1] = bita[0]; |
||
83 | bita+=2; |
||
84 | } |
||
85 | bita = bit_alloc; |
||
86 | scfsi=scfsi_buf; |
||
87 | for (i=sblimit2;i;i--) |
||
88 | if (*bita++) |
||
89 | *scfsi++ = (char) getbits_fast(2); |
||
90 | } |
||
91 | else /* mono */ |
||
92 | { |
||
93 | for (i=sblimit;i;i--,alloc1+=(1<<step)) |
||
94 | *bita++ = (char) getbits(step=alloc1->bits); |
||
95 | bita = bit_alloc; |
||
96 | scfsi=scfsi_buf; |
||
97 | for (i=sblimit;i;i--) |
||
98 | if (*bita++) |
||
99 | *scfsi++ = (char) getbits_fast(2); |
||
100 | } |
||
101 | |||
102 | bita = bit_alloc; |
||
103 | scfsi=scfsi_buf; |
||
104 | for (i=sblimit2;i;i--) |
||
105 | if (*bita++) |
||
106 | switch (*scfsi++) |
||
107 | { |
||
108 | case 0: |
||
109 | *scale++ = getbits_fast(6); |
||
110 | *scale++ = getbits_fast(6); |
||
111 | *scale++ = getbits_fast(6); |
||
112 | break; |
||
113 | case 1 : |
||
114 | *scale++ = sc = getbits_fast(6); |
||
115 | *scale++ = sc; |
||
116 | *scale++ = getbits_fast(6); |
||
117 | break; |
||
118 | case 2: |
||
119 | *scale++ = sc = getbits_fast(6); |
||
120 | *scale++ = sc; |
||
121 | *scale++ = sc; |
||
122 | break; |
||
123 | default: /* case 3 */ |
||
124 | *scale++ = getbits_fast(6); |
||
125 | *scale++ = sc = getbits_fast(6); |
||
126 | *scale++ = sc; |
||
127 | break; |
||
128 | } |
||
129 | |||
130 | } |
||
131 | |||
132 | void II_step_two(unsigned int *bit_alloc,real fraction[2][4][SBLIMIT],int *scale,struct frame *fr,int x1) |
||
133 | { |
||
134 | int i,j,k,ba; |
||
135 | int stereo = fr->stereo; |
||
136 | int sblimit = fr->II_sblimit; |
||
137 | int jsbound = fr->jsbound; |
||
138 | struct al_table *alloc2,*alloc1 = fr->alloc; |
||
139 | unsigned int *bita=bit_alloc; |
||
140 | int d1,step; |
||
141 | |||
142 | for (i=0;i<jsbound;i++,alloc1+=(1<<step)) |
||
143 | { |
||
144 | step = alloc1->bits; |
||
145 | for (j=0;j<stereo;j++) |
||
146 | { |
||
147 | if ( (ba=*bita++) ) |
||
148 | { |
||
149 | k=(alloc2 = alloc1+ba)->bits; |
||
150 | if( (d1=alloc2->d) < 0) |
||
151 | { |
||
152 | real cm=muls[k][scale[x1]]; |
||
153 | fraction[j][0][i] = ((real) ((int)getbits(k) + d1)) * cm; |
||
154 | fraction[j][1][i] = ((real) ((int)getbits(k) + d1)) * cm; |
||
155 | fraction[j][2][i] = ((real) ((int)getbits(k) + d1)) * cm; |
||
156 | } |
||
157 | else |
||
158 | { |
||
159 | static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab }; |
||
160 | unsigned int idx,*tab,m=scale[x1]; |
||
161 | idx = (unsigned int) getbits(k); |
||
162 | tab = (unsigned int *) (table[d1] + idx + idx + idx); |
||
163 | fraction[j][0][i] = muls[*tab++][m]; |
||
164 | fraction[j][1][i] = muls[*tab++][m]; |
||
165 | fraction[j][2][i] = muls[*tab][m]; |
||
166 | } |
||
167 | scale+=3; |
||
168 | } |
||
169 | else |
||
170 | fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0; |
||
171 | } |
||
172 | } |
||
173 | |||
174 | for (i=jsbound;i<sblimit;i++,alloc1+=(1<<step)) |
||
175 | { |
||
176 | step = alloc1->bits; |
||
177 | bita++; /* channel 1 and channel 2 bitalloc are the same */ |
||
178 | if ( (ba=*bita++) ) |
||
179 | { |
||
180 | k=(alloc2 = alloc1+ba)->bits; |
||
181 | if( (d1=alloc2->d) < 0) |
||
182 | { |
||
183 | real cm; |
||
184 | cm=muls[k][scale[x1+3]]; |
||
185 | fraction[1][0][i] = (fraction[0][0][i] = (real) ((int)getbits(k) + d1) ) * cm; |
||
186 | fraction[1][1][i] = (fraction[0][1][i] = (real) ((int)getbits(k) + d1) ) * cm; |
||
187 | fraction[1][2][i] = (fraction[0][2][i] = (real) ((int)getbits(k) + d1) ) * cm; |
||
188 | cm=muls[k][scale[x1]]; |
||
189 | fraction[0][0][i] *= cm; fraction[0][1][i] *= cm; fraction[0][2][i] *= cm; |
||
190 | } |
||
191 | else |
||
192 | { |
||
193 | static int *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab }; |
||
194 | unsigned int idx,*tab,m1,m2; |
||
195 | m1 = scale[x1]; m2 = scale[x1+3]; |
||
196 | idx = (unsigned int) getbits(k); |
||
197 | tab = (unsigned int *) (table[d1] + idx + idx + idx); |
||
198 | fraction[0][0][i] = muls[*tab][m1]; fraction[1][0][i] = muls[*tab++][m2]; |
||
199 | fraction[0][1][i] = muls[*tab][m1]; fraction[1][1][i] = muls[*tab++][m2]; |
||
200 | fraction[0][2][i] = muls[*tab][m1]; fraction[1][2][i] = muls[*tab][m2]; |
||
201 | } |
||
202 | scale+=6; |
||
203 | } |
||
204 | else { |
||
205 | fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] = |
||
206 | fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = 0.0; |
||
207 | } |
||
208 | /* |
||
209 | should we use individual scalefac for channel 2 or |
||
210 | is the current way the right one , where we just copy channel 1 to |
||
211 | channel 2 ?? |
||
212 | The current 'strange' thing is, that we throw away the scalefac |
||
213 | values for the second channel ...!! |
||
214 | -> changed .. now we use the scalefac values of channel one !! |
||
215 | */ |
||
216 | } |
||
217 | |||
218 | if(sblimit > (SBLIMIT>>fr->down_sample) ) |
||
219 | sblimit = SBLIMIT>>fr->down_sample; |
||
220 | |||
221 | for(i=sblimit;i<SBLIMIT;i++) |
||
222 | for (j=0;j<stereo;j++) |
||
223 | fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = 0.0; |
||
224 | |||
225 | } |
||
226 | |||
227 | int do_layer2(struct frame *fr,int outmode,struct audio_info_struct *ai) |
||
228 | { |
||
229 | int clip=0; |
||
230 | int i,j; |
||
231 | int stereo = fr->stereo; |
||
232 | real fraction[2][4][SBLIMIT]; /* pick_table clears unused subbands */ |
||
233 | unsigned int bit_alloc[64]; |
||
234 | int scale[192]; |
||
235 | int single = fr->single; |
||
236 | |||
237 | if(stereo == 1 || single == 3) |
||
238 | single = 0; |
||
239 | |||
240 | II_step_one(bit_alloc, scale, fr); |
||
241 | |||
242 | for (i=0;i<SCALE_BLOCK;i++) |
||
243 | { |
||
244 | II_step_two(bit_alloc,fraction,scale,fr,i>>2); |
||
245 | for (j=0;j<3;j++) |
||
246 | { |
||
247 | if(single >= 0) |
||
248 | { |
||
249 | clip += (fr->synth_mono) (fraction[single][j],pcm_sample+pcm_point); |
||
250 | } |
||
251 | else { |
||
252 | clip += (fr->synth) (fraction[0][j],0,pcm_sample+pcm_point); |
||
253 | clip += (fr->synth) (fraction[1][j],1,pcm_sample+pcm_point); |
||
254 | } |
||
255 | pcm_point += fr->block_size; |
||
256 | |||
257 | if(pcm_point == audiobufsize) |
||
258 | audio_flush(outmode,ai); |
||
259 | } |
||
260 | } |
||
261 | |||
262 | return clip; |
||
263 | } |
||
264 | |||
265 |