/src/mpg123/src/libmpg123/layer2.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | layer2.c: the layer 2 decoder, root of mpg123 |
3 | | |
4 | | copyright 1994-2021 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 | | mpg123 started as mp2 decoder a long time ago... |
9 | | part of this file is required for layer 1, too. |
10 | | */ |
11 | | |
12 | | |
13 | | #include "mpg123lib_intern.h" |
14 | | |
15 | | #ifndef NO_LAYER2 |
16 | | #include "l2tables.h" |
17 | | #endif |
18 | | |
19 | | #include "getbits.h" |
20 | | |
21 | | #ifndef NO_LAYER12 /* Stuff needed for layer I and II. */ |
22 | | |
23 | | #include "l12tabs.h" |
24 | | |
25 | | #ifdef RUNTIME_TABLES |
26 | | #include "init_layer12.h" |
27 | | #endif |
28 | | |
29 | | // The layer12_table is already in real format (fixed or float), just needs |
30 | | // a little scaling in the MMX/SSE case. |
31 | | |
32 | | void INT123_init_layer12_stuff(mpg123_handle *fr, real* (*init_table)(mpg123_handle *fr, real *table, int m)) |
33 | 0 | { |
34 | 0 | int k; |
35 | 0 | real *table; |
36 | 0 | for(k=0;k<27;k++) |
37 | 0 | { |
38 | 0 | table = init_table(fr, fr->muls[k], k); |
39 | 0 | *table++ = 0.0; |
40 | 0 | } |
41 | 0 | } |
42 | | |
43 | | real* INT123_init_layer12_table(mpg123_handle *fr, real *table, int m) |
44 | 0 | { |
45 | 0 | int i; |
46 | 0 | for(i=0;i<63;i++) |
47 | 0 | *table++ = layer12_table[m][i]; |
48 | 0 | return table; |
49 | 0 | } |
50 | | |
51 | | #ifdef OPT_MMXORSSE |
52 | | real* INT123_init_layer12_table_mmx(mpg123_handle *fr, real *table, int m) |
53 | 0 | { |
54 | 0 | int i,j; |
55 | 0 | if(!fr->p.down_sample) |
56 | 0 | { |
57 | 0 | for(j=3,i=0;i<63;i++,j--) |
58 | 0 | *table++ = 16384 * layer12_table[m][i]; |
59 | 0 | } |
60 | 0 | else |
61 | 0 | { |
62 | 0 | for(j=3,i=0;i<63;i++,j--) |
63 | 0 | *table++ = layer12_table[m][i]; |
64 | 0 | } |
65 | 0 | return table; |
66 | 0 | } |
67 | | #endif |
68 | | |
69 | | #endif /* NO_LAYER12 */ |
70 | | |
71 | | /* The rest is the actual decoding of layer II data. */ |
72 | | |
73 | | #ifndef NO_LAYER2 |
74 | | |
75 | | static int II_step_one(unsigned int *bit_alloc,int *scale,mpg123_handle *fr) |
76 | 0 | { |
77 | 0 | int stereo = fr->stereo-1; |
78 | 0 | int sblimit = fr->II_sblimit; |
79 | 0 | int jsbound = fr->jsbound; |
80 | 0 | int sblimit2 = fr->II_sblimit<<stereo; |
81 | 0 | const struct al_table *alloc1 = fr->alloc; |
82 | 0 | int i; |
83 | 0 | unsigned int scfsi_buf[64]; |
84 | 0 | unsigned int *scfsi,*bita; |
85 | 0 | int sc,step; |
86 | | /* Count the bits needed for getbits_fast(). */ |
87 | 0 | unsigned int needbits = 0; |
88 | 0 | unsigned int scale_bits[4] = { 18, 12, 6, 12 }; |
89 | |
|
90 | 0 | bita = bit_alloc; |
91 | 0 | if(stereo) |
92 | 0 | { |
93 | 0 | for(i=jsbound;i;i--,alloc1+=(1<<step)) |
94 | 0 | { |
95 | 0 | step=alloc1->bits; |
96 | 0 | bita[0] = (char) getbits(fr, step); |
97 | 0 | bita[1] = (char) getbits(fr, step); |
98 | 0 | needbits += ((bita[0]?1:0)+(bita[1]?1:0))*2; |
99 | 0 | bita+=2; |
100 | 0 | } |
101 | 0 | for(i=sblimit-jsbound;i;i--,alloc1+=(1<<step)) |
102 | 0 | { |
103 | 0 | step=alloc1->bits; |
104 | 0 | bita[0] = (char) getbits(fr, step); |
105 | 0 | bita[1] = bita[0]; |
106 | 0 | needbits += (bita[0]?1:0)*2*2; |
107 | 0 | bita+=2; |
108 | 0 | } |
109 | 0 | bita = bit_alloc; |
110 | 0 | scfsi=scfsi_buf; |
111 | |
|
112 | 0 | if(fr->bits_avail < needbits) |
113 | 0 | { |
114 | 0 | if(NOQUIET) |
115 | 0 | error2("need %u bits, have %li", needbits, fr->bits_avail); |
116 | 0 | return -1; |
117 | 0 | } |
118 | 0 | for(i=sblimit2;i;i--) |
119 | 0 | if(*bita++) *scfsi++ = (char) getbits_fast(fr, 2); |
120 | 0 | } |
121 | 0 | else /* mono */ |
122 | 0 | { |
123 | 0 | for(i=sblimit;i;i--,alloc1+=(1<<step)) |
124 | 0 | { |
125 | 0 | step=alloc1->bits; |
126 | 0 | *bita = (char) getbits(fr, step); |
127 | 0 | if(*bita) |
128 | 0 | needbits += 2; |
129 | 0 | ++bita; |
130 | 0 | } |
131 | 0 | bita = bit_alloc; |
132 | 0 | scfsi=scfsi_buf; |
133 | 0 | if(fr->bits_avail < needbits) |
134 | 0 | { |
135 | 0 | if(NOQUIET) |
136 | 0 | error2("need %u bits, have %li", needbits, fr->bits_avail); |
137 | 0 | return -1; |
138 | 0 | } |
139 | 0 | for(i=sblimit;i;i--) |
140 | 0 | if(*bita++) *scfsi++ = (char) getbits_fast(fr, 2); |
141 | 0 | } |
142 | | |
143 | 0 | needbits = 0; |
144 | 0 | bita = bit_alloc; |
145 | 0 | scfsi=scfsi_buf; |
146 | 0 | for(i=sblimit2;i;--i) |
147 | 0 | if(*bita++) |
148 | 0 | needbits += scale_bits[*scfsi++]; |
149 | 0 | if(fr->bits_avail < needbits) |
150 | 0 | { |
151 | 0 | if(NOQUIET) |
152 | 0 | error2("need %u bits, have %li", needbits, fr->bits_avail); |
153 | 0 | return -1; |
154 | 0 | } |
155 | | |
156 | 0 | bita = bit_alloc; |
157 | 0 | scfsi=scfsi_buf; |
158 | 0 | for(i=sblimit2;i;--i) |
159 | 0 | if(*bita++) |
160 | 0 | switch(*scfsi++) |
161 | 0 | { |
162 | 0 | case 0: |
163 | 0 | *scale++ = getbits_fast(fr, 6); |
164 | 0 | *scale++ = getbits_fast(fr, 6); |
165 | 0 | *scale++ = getbits_fast(fr, 6); |
166 | 0 | break; |
167 | 0 | case 1 : |
168 | 0 | *scale++ = sc = getbits_fast(fr, 6); |
169 | 0 | *scale++ = sc; |
170 | 0 | *scale++ = getbits_fast(fr, 6); |
171 | 0 | break; |
172 | 0 | case 2: |
173 | 0 | *scale++ = sc = getbits_fast(fr, 6); |
174 | 0 | *scale++ = sc; |
175 | 0 | *scale++ = sc; |
176 | 0 | break; |
177 | 0 | default: /* case 3 */ |
178 | 0 | *scale++ = getbits_fast(fr, 6); |
179 | 0 | *scale++ = sc = getbits_fast(fr, 6); |
180 | 0 | *scale++ = sc; |
181 | 0 | break; |
182 | 0 | } |
183 | | |
184 | 0 | return 0; |
185 | 0 | } |
186 | | |
187 | | |
188 | | static void II_step_two(unsigned int *bit_alloc,real fraction[2][4][SBLIMIT],int *scale,mpg123_handle *fr,int x1) |
189 | 0 | { |
190 | 0 | int i,j,k,ba; |
191 | 0 | int stereo = fr->stereo; |
192 | 0 | int sblimit = fr->II_sblimit; |
193 | 0 | int jsbound = fr->jsbound; |
194 | 0 | const struct al_table *alloc2,*alloc1 = fr->alloc; |
195 | 0 | unsigned int *bita=bit_alloc; |
196 | 0 | int d1,step; |
197 | |
|
198 | 0 | for(i=0;i<jsbound;i++,alloc1+=(1<<step)) |
199 | 0 | { |
200 | 0 | step = alloc1->bits; |
201 | 0 | for(j=0;j<stereo;j++) |
202 | 0 | { |
203 | 0 | if( (ba=*bita++) ) |
204 | 0 | { |
205 | 0 | k=(alloc2 = alloc1+ba)->bits; |
206 | 0 | if( (d1=alloc2->d) < 0) |
207 | 0 | { |
208 | 0 | real cm=fr->muls[k][scale[x1]]; |
209 | 0 | fraction[j][0][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm); |
210 | 0 | fraction[j][1][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm); |
211 | 0 | fraction[j][2][i] = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1), cm); |
212 | 0 | } |
213 | 0 | else |
214 | 0 | { |
215 | 0 | const unsigned char *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab }; |
216 | 0 | unsigned int m=scale[x1]; |
217 | 0 | unsigned int idx = (unsigned int) getbits(fr, k); |
218 | 0 | const unsigned char *tab = table[d1] + idx + idx + idx; |
219 | 0 | fraction[j][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m]); |
220 | 0 | fraction[j][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m]); |
221 | 0 | fraction[j][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m]); |
222 | 0 | } |
223 | 0 | scale+=3; |
224 | 0 | } |
225 | 0 | else |
226 | 0 | fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = DOUBLE_TO_REAL(0.0); |
227 | 0 | if(fr->bits_avail < 0) |
228 | 0 | return; /* Caller checks that again. */ |
229 | 0 | } |
230 | 0 | } |
231 | | |
232 | 0 | for(i=jsbound;i<sblimit;i++,alloc1+=(1<<step)) |
233 | 0 | { |
234 | 0 | step = alloc1->bits; |
235 | 0 | bita++; /* channel 1 and channel 2 bitalloc are the same */ |
236 | 0 | if( (ba=*bita++) ) |
237 | 0 | { |
238 | 0 | k=(alloc2 = alloc1+ba)->bits; |
239 | 0 | if( (d1=alloc2->d) < 0) |
240 | 0 | { |
241 | 0 | real cm; |
242 | 0 | cm=fr->muls[k][scale[x1+3]]; |
243 | 0 | fraction[0][0][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1); |
244 | 0 | fraction[0][1][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1); |
245 | 0 | fraction[0][2][i] = DOUBLE_TO_REAL_15((int)getbits(fr, k) + d1); |
246 | 0 | fraction[1][0][i] = REAL_MUL_SCALE_LAYER12(fraction[0][0][i], cm); |
247 | 0 | fraction[1][1][i] = REAL_MUL_SCALE_LAYER12(fraction[0][1][i], cm); |
248 | 0 | fraction[1][2][i] = REAL_MUL_SCALE_LAYER12(fraction[0][2][i], cm); |
249 | 0 | cm=fr->muls[k][scale[x1]]; |
250 | 0 | fraction[0][0][i] = REAL_MUL_SCALE_LAYER12(fraction[0][0][i], cm); |
251 | 0 | fraction[0][1][i] = REAL_MUL_SCALE_LAYER12(fraction[0][1][i], cm); |
252 | 0 | fraction[0][2][i] = REAL_MUL_SCALE_LAYER12(fraction[0][2][i], cm); |
253 | 0 | } |
254 | 0 | else |
255 | 0 | { |
256 | 0 | const unsigned char *table[] = { 0,0,0,grp_3tab,0,grp_5tab,0,0,0,grp_9tab }; |
257 | 0 | unsigned int m1 = scale[x1]; |
258 | 0 | unsigned int m2 = scale[x1+3]; |
259 | 0 | unsigned int idx = (unsigned int) getbits(fr, k); |
260 | 0 | const unsigned char *tab = table[d1] + idx + idx + idx; |
261 | 0 | fraction[0][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][0][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m2]); |
262 | 0 | fraction[0][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][1][i] = REAL_SCALE_LAYER12(fr->muls[*tab++][m2]); |
263 | 0 | fraction[0][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m1]); fraction[1][2][i] = REAL_SCALE_LAYER12(fr->muls[*tab][m2]); |
264 | 0 | } |
265 | 0 | scale+=6; |
266 | 0 | if(fr->bits_avail < 0) |
267 | 0 | return; /* Caller checks that again. */ |
268 | 0 | } |
269 | 0 | else |
270 | 0 | { |
271 | 0 | fraction[0][0][i] = fraction[0][1][i] = fraction[0][2][i] = |
272 | 0 | fraction[1][0][i] = fraction[1][1][i] = fraction[1][2][i] = DOUBLE_TO_REAL(0.0); |
273 | 0 | } |
274 | | /* |
275 | | Historic comment... |
276 | | should we use individual scalefac for channel 2 or |
277 | | is the current way the right one , where we just copy channel 1 to |
278 | | channel 2 ?? |
279 | | The current 'strange' thing is, that we throw away the scalefac |
280 | | values for the second channel ...!! |
281 | | -> changed .. now we use the scalefac values of channel one !! |
282 | | */ |
283 | 0 | } |
284 | | |
285 | 0 | if(sblimit > (fr->down_sample_sblimit) ) |
286 | 0 | sblimit = fr->down_sample_sblimit; |
287 | |
|
288 | 0 | for(i=sblimit;i<SBLIMIT;i++) |
289 | 0 | for (j=0;j<stereo;j++) |
290 | 0 | fraction[j][0][i] = fraction[j][1][i] = fraction[j][2][i] = DOUBLE_TO_REAL(0.0); |
291 | 0 | } |
292 | | |
293 | | |
294 | | static void II_select_table(mpg123_handle *fr) |
295 | 0 | { |
296 | 0 | const int translate[3][2][16] = |
297 | 0 | { |
298 | 0 | { |
299 | 0 | { 0,2,2,2,2,2,2,0,0,0,1,1,1,1,1,0 }, |
300 | 0 | { 0,2,2,0,0,0,1,1,1,1,1,1,1,1,1,0 } |
301 | 0 | }, |
302 | 0 | { |
303 | 0 | { 0,2,2,2,2,2,2,0,0,0,0,0,0,0,0,0 }, |
304 | 0 | { 0,2,2,0,0,0,0,0,0,0,0,0,0,0,0,0 } |
305 | 0 | }, |
306 | 0 | { |
307 | 0 | { 0,3,3,3,3,3,3,0,0,0,1,1,1,1,1,0 }, |
308 | 0 | { 0,3,3,0,0,0,1,1,1,1,1,1,1,1,1,0 } |
309 | 0 | } |
310 | 0 | }; |
311 | |
|
312 | 0 | int table,sblim; |
313 | 0 | const struct al_table *tables[5] = { alloc_0, alloc_1, alloc_2, alloc_3 , alloc_4 }; |
314 | 0 | const int sblims[5] = { 27 , 30 , 8, 12 , 30 }; |
315 | |
|
316 | 0 | if(fr->hdr.sampling_frequency >= 3) /* Or equivalent: (fr->lsf == 1) */ |
317 | 0 | table = 4; |
318 | 0 | else |
319 | 0 | table = translate[fr->hdr.sampling_frequency][2-fr->stereo][fr->hdr.bitrate_index]; |
320 | |
|
321 | 0 | sblim = sblims[table]; |
322 | 0 | fr->alloc = tables[table]; |
323 | 0 | fr->II_sblimit = sblim; |
324 | 0 | } |
325 | | |
326 | | |
327 | | int INT123_do_layer2(mpg123_handle *fr) |
328 | 0 | { |
329 | 0 | int clip=0; |
330 | 0 | int i,j; |
331 | 0 | int stereo = fr->stereo; |
332 | | /* pick_table clears unused subbands */ |
333 | | /* replacement for real fraction[2][4][SBLIMIT], needs alignment. */ |
334 | 0 | real (*fraction)[4][SBLIMIT] = fr->layer2.fraction; |
335 | 0 | unsigned int bit_alloc[64]; |
336 | 0 | int scale[192]; |
337 | 0 | int single = fr->single; |
338 | |
|
339 | 0 | II_select_table(fr); |
340 | 0 | fr->jsbound = (fr->hdr.mode == MPG_MD_JOINT_STEREO) ? (fr->hdr.mode_ext<<2)+4 : fr->II_sblimit; |
341 | |
|
342 | 0 | if(fr->jsbound > fr->II_sblimit) |
343 | 0 | { |
344 | 0 | fprintf(stderr, "Truncating stereo boundary to sideband limit.\n"); |
345 | 0 | fr->jsbound=fr->II_sblimit; |
346 | 0 | } |
347 | | |
348 | | /* TODO: What happens with mono mixing, actually? */ |
349 | 0 | if(stereo == 1 || single == SINGLE_MIX) /* also, mix not really handled */ |
350 | 0 | single = SINGLE_LEFT; |
351 | |
|
352 | 0 | if(II_step_one(bit_alloc, scale, fr)) |
353 | 0 | { |
354 | 0 | if(NOQUIET) |
355 | 0 | error("first step of layer I decoding failed"); |
356 | 0 | return clip; |
357 | 0 | } |
358 | | |
359 | 0 | for(i=0;i<SCALE_BLOCK;i++) |
360 | 0 | { |
361 | 0 | II_step_two(bit_alloc,fraction,scale,fr,i>>2); |
362 | 0 | if(fr->bits_avail < 0) |
363 | 0 | { |
364 | 0 | if(NOQUIET) |
365 | 0 | error("missing bits in layer II step two"); |
366 | 0 | return clip; |
367 | 0 | } |
368 | 0 | for(j=0;j<3;j++) |
369 | 0 | { |
370 | 0 | if(single != SINGLE_STEREO) |
371 | 0 | clip += (fr->synth_mono)(fraction[single][j], fr); |
372 | 0 | else |
373 | 0 | clip += (fr->synth_stereo)(fraction[0][j], fraction[1][j], fr); |
374 | 0 | } |
375 | 0 | } |
376 | | |
377 | 0 | return clip; |
378 | 0 | } |
379 | | |
380 | | #endif /* NO_LAYER2 */ |