Debian Codegraph @

src/libmpg123/layer2.c

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