Coverage Report

Created: 2023-06-07 06:25

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