Coverage Report

Created: 2026-03-07 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpg123/src/libmpg123/layer1.c
Line
Count
Source
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 "../common/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
355
{
26
355
  unsigned int *ba;
27
12.8k
  for(ba=balloc; ba != end; ++ba)
28
12.5k
  if(*ba == 15)
29
58
  {
30
58
    if(NOQUIET) error("Illegal bit allocation value.");
31
58
    return -1;
32
58
  }
33
34
297
  return 0;
35
355
}
36
37
#define NEED_BITS(fr, num) \
38
2.78k
  if((fr)->bits_avail < num) \
39
2.78k
  { \
40
543
    if(NOQUIET) \
41
543
      error2("%u bits needed, %li available", num, (fr)->bits_avail); \
42
543
    return -1; \
43
543
  } \
44
45
static int I_step_one(unsigned int balloc[], unsigned int scale_index[2][SBLIMIT],mpg123_handle *fr)
46
687
{
47
687
  unsigned int *ba=balloc;
48
687
  unsigned int *sca = (unsigned int *) scale_index;
49
50
687
  if(fr->stereo == 2)
51
306
  {
52
306
    int i;
53
306
    int jsbound = fr->jsbound;
54
306
    unsigned int needbits = jsbound*2*4 + (SBLIMIT-jsbound)*4;
55
56
306
    NEED_BITS(fr, needbits);
57
225
    needbits = 0;
58
3.33k
    for(i=0;i<jsbound;i++)
59
3.11k
    {
60
3.11k
      ba[0] = getbits_fast(fr, 4);
61
3.11k
      ba[1] = getbits_fast(fr, 4);
62
3.11k
      needbits += ((ba[0]?1:0)+(ba[1]?1:0))*6;
63
3.11k
      ba+=2;
64
3.11k
    }
65
4.31k
    for(i=jsbound;i<SBLIMIT;i++)
66
4.08k
    {
67
4.08k
      *ba = getbits_fast(fr, 4);
68
4.08k
      needbits += (*ba?1:0)*12;
69
4.08k
      ++ba;
70
4.08k
    }
71
72
225
    if(check_balloc(fr, balloc, ba)) return -1;
73
74
192
    ba = balloc;
75
192
    NEED_BITS(fr, needbits)
76
2.66k
    for(i=0;i<jsbound;i++)
77
2.48k
    {
78
2.48k
      if ((*ba++))
79
673
        *sca++ = getbits_fast(fr, 6);
80
2.48k
      if ((*ba++))
81
784
        *sca++ = getbits_fast(fr, 6);
82
2.48k
    }
83
3.45k
    for (i=jsbound;i<SBLIMIT;i++) if((*ba++))
84
432
    {
85
432
      *sca++ =  getbits_fast(fr, 6);
86
432
      *sca++ =  getbits_fast(fr, 6);
87
432
    }
88
180
  }
89
381
  else
90
381
  {
91
381
    int i;
92
381
    unsigned int needbits = SBLIMIT*4;
93
94
381
    NEED_BITS(fr, needbits)
95
130
    needbits = 0;
96
4.29k
    for(i=0;i<SBLIMIT;i++)
97
4.16k
    {
98
4.16k
      *ba = getbits_fast(fr, 4);
99
4.16k
      needbits += (*ba?1:0)*6;
100
4.16k
      ++ba;
101
4.16k
    }
102
103
130
    if(check_balloc(fr, balloc, ba)) return -1;
104
105
105
    ba = balloc;
106
105
    NEED_BITS(fr, needbits)
107
2.93k
    for (i=0;i<SBLIMIT;i++)
108
2.84k
      if ((*ba++))
109
856
        *sca++ = getbits_fast(fr, 6);
110
89
  }
111
112
269
  return 0;
113
687
}
114
115
/* Something sane in place of undefined (-1)<<n. Well, not really. */
116
#define MINUS_SHIFT(n) ( (int)(((unsigned int)-1)<<(n)) )
117
118
static int I_step_two(real fraction[2][SBLIMIT],unsigned int balloc[2*SBLIMIT], unsigned int scale_index[2][SBLIMIT],mpg123_handle *fr)
119
1.80k
{
120
1.80k
  int i,n;
121
1.80k
  int smpb[2*SBLIMIT]; /* values: 0-65535 */
122
1.80k
  int *sample;
123
1.80k
  register unsigned int *ba;
124
1.80k
  register unsigned int *sca = (unsigned int *) scale_index;
125
126
1.80k
  if(fr->stereo == 2)
127
1.25k
  {
128
1.25k
    unsigned int needbits = 0;
129
1.25k
    int jsbound = fr->jsbound;
130
1.25k
    register real *f0 = fraction[0];
131
1.25k
    register real *f1 = fraction[1];
132
133
1.25k
    ba = balloc;
134
17.8k
    for(sample=smpb,i=0;i<jsbound;i++)
135
16.5k
    {
136
16.5k
      if((n=*ba++))
137
3.18k
        needbits += n+1;
138
16.5k
      if((n=*ba++))
139
4.36k
        needbits += n+1;
140
16.5k
    }
141
24.6k
    for(i=jsbound;i<SBLIMIT;i++) 
142
23.4k
      if((n = *ba++))
143
2.24k
        needbits += n+1;
144
1.25k
    NEED_BITS(fr, needbits)
145
146
1.12k
    ba = balloc;
147
15.9k
    for(sample=smpb,i=0;i<jsbound;i++)
148
14.8k
    {
149
14.8k
      if((n = *ba++)) *sample++ = getbits(fr, n+1);
150
151
14.8k
      if((n = *ba++)) *sample++ = getbits(fr, n+1);
152
14.8k
    }
153
22.3k
    for(i=jsbound;i<SBLIMIT;i++) 
154
21.2k
    if((n = *ba++))
155
1.88k
    *sample++ = getbits(fr, n+1);
156
157
1.12k
    ba = balloc;
158
15.9k
    for(sample=smpb,i=0;i<jsbound;i++)
159
14.8k
    {
160
14.8k
      if((n=*ba++))
161
2.57k
      *f0++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
162
12.2k
      else *f0++ = DOUBLE_TO_REAL(0.0);
163
164
14.8k
      if((n=*ba++))
165
3.71k
      *f1++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
166
11.1k
      else *f1++ = DOUBLE_TO_REAL(0.0);
167
14.8k
    }
168
22.3k
    for(i=jsbound;i<SBLIMIT;i++)
169
21.2k
    {
170
21.2k
      if((n=*ba++))
171
1.88k
      {
172
1.88k
        real samp = DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1);
173
1.88k
        *f0++ = REAL_MUL_SCALE_LAYER12(samp, fr->muls[n+1][*sca++]);
174
1.88k
        *f1++ = REAL_MUL_SCALE_LAYER12(samp, fr->muls[n+1][*sca++]);
175
1.88k
      }
176
19.3k
      else *f0++ = *f1++ = DOUBLE_TO_REAL(0.0);
177
21.2k
    }
178
1.12k
    for(i=fr->down_sample_sblimit;i<32;i++)
179
0
    fraction[0][i] = fraction[1][i] = 0.0;
180
1.12k
  }
181
551
  else
182
551
  {
183
551
    unsigned int needbits = 0;
184
551
    register real *f0 = fraction[0];
185
186
551
    ba = balloc;
187
18.1k
    for(sample=smpb,i=0;i<SBLIMIT;i++)
188
17.6k
      if((n = *ba++))
189
3.23k
        needbits += n+1;
190
551
    NEED_BITS(fr, needbits);
191
192
491
    ba = balloc;
193
16.2k
    for(sample=smpb,i=0;i<SBLIMIT;i++)
194
15.7k
    if ((n = *ba++))
195
2.48k
    *sample++ = getbits(fr, n+1);
196
197
198
491
    ba = balloc;
199
16.2k
    for(sample=smpb,i=0;i<SBLIMIT;i++)
200
15.7k
    {
201
15.7k
      if((n=*ba++))
202
2.48k
      *f0++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
203
13.2k
      else *f0++ = DOUBLE_TO_REAL(0.0);
204
15.7k
    }
205
491
    for(i=fr->down_sample_sblimit;i<32;i++)
206
0
    fraction[0][i] = DOUBLE_TO_REAL(0.0);
207
491
  }
208
1.61k
  return 0;
209
1.80k
}
210
211
int INT123_do_layer1(mpg123_handle *fr)
212
687
{
213
687
  int clip=0;
214
687
  int i,stereo = fr->stereo;
215
687
  unsigned int balloc[2*SBLIMIT];
216
687
  unsigned int scale_index[2][SBLIMIT];
217
687
  real (*fraction)[SBLIMIT] = fr->layer1.fraction; /* fraction[2][SBLIMIT] */
218
687
  int single = fr->single;
219
220
687
  fr->jsbound = (fr->hdr.mode == MPG_MD_JOINT_STEREO) ? (fr->hdr.mode_ext<<2)+4 : 32;
221
222
687
  if(stereo == 1 || single == SINGLE_MIX) /* I don't see mixing handled here */
223
381
  single = SINGLE_LEFT;
224
225
687
  if(I_step_one(balloc,scale_index,fr))
226
418
  {
227
418
    if(NOQUIET)
228
418
      error("Aborting layer I decoding after step one.");
229
418
    return clip;
230
418
  }
231
232
1.88k
  for(i=0;i<SCALE_BLOCK;i++)
233
1.80k
  {
234
1.80k
    if(I_step_two(fraction,balloc,scale_index,fr))
235
183
    {
236
183
      if(NOQUIET)
237
183
        error("Aborting layer I decoding after step two.");
238
183
      return clip;
239
183
    }
240
241
1.61k
    if(single != SINGLE_STEREO)
242
491
    clip += (fr->synth_mono)(fraction[single], fr);
243
1.12k
    else
244
1.12k
    clip += (fr->synth_stereo)(fraction[0], fraction[1], fr);
245
1.61k
  }
246
247
86
  return clip;
248
269
}
249
250