Coverage Report

Created: 2025-08-29 06:59

/src/mpg123/src/libmpg123/layer1.c
Line
Count
Source (jump to first uncovered line)
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
92.8k
{
26
92.8k
  unsigned int *ba;
27
1.85M
  for(ba=balloc; ba != end; ++ba)
28
1.82M
  if(*ba == 15)
29
64.3k
  {
30
64.3k
    if(NOQUIET) error("Illegal bit allocation value.");
31
64.3k
    return -1;
32
64.3k
  }
33
34
28.5k
  return 0;
35
92.8k
}
36
37
#define NEED_BITS(fr, num) \
38
525k
  if((fr)->bits_avail < num) \
39
525k
  { \
40
261k
    if(NOQUIET) \
41
261k
      error2("%u bits needed, %li available", num, (fr)->bits_avail); \
42
261k
    return -1; \
43
261k
  } \
44
45
static int I_step_one(unsigned int balloc[], unsigned int scale_index[2][SBLIMIT],mpg123_handle *fr)
46
334k
{
47
334k
  unsigned int *ba=balloc;
48
334k
  unsigned int *sca = (unsigned int *) scale_index;
49
50
334k
  if(fr->stereo == 2)
51
238k
  {
52
238k
    int i;
53
238k
    int jsbound = fr->jsbound;
54
238k
    unsigned int needbits = jsbound*2*4 + (SBLIMIT-jsbound)*4;
55
56
238k
    NEED_BITS(fr, needbits);
57
44.3k
    needbits = 0;
58
991k
    for(i=0;i<jsbound;i++)
59
946k
    {
60
946k
      ba[0] = getbits_fast(fr, 4);
61
946k
      ba[1] = getbits_fast(fr, 4);
62
946k
      needbits += ((ba[0]?1:0)+(ba[1]?1:0))*6;
63
946k
      ba+=2;
64
946k
    }
65
517k
    for(i=jsbound;i<SBLIMIT;i++)
66
472k
    {
67
472k
      *ba = getbits_fast(fr, 4);
68
472k
      needbits += (*ba?1:0)*12;
69
472k
      ++ba;
70
472k
    }
71
72
44.3k
    if(check_balloc(fr, balloc, ba)) return -1;
73
74
10.4k
    ba = balloc;
75
10.4k
    NEED_BITS(fr, needbits)
76
164k
    for(i=0;i<jsbound;i++)
77
155k
    {
78
155k
      if ((*ba++))
79
47.8k
        *sca++ = getbits_fast(fr, 6);
80
155k
      if ((*ba++))
81
50.5k
        *sca++ = getbits_fast(fr, 6);
82
155k
    }
83
137k
    for (i=jsbound;i<SBLIMIT;i++) if((*ba++))
84
45.7k
    {
85
45.7k
      *sca++ =  getbits_fast(fr, 6);
86
45.7k
      *sca++ =  getbits_fast(fr, 6);
87
45.7k
    }
88
8.86k
  }
89
95.9k
  else
90
95.9k
  {
91
95.9k
    int i;
92
95.9k
    unsigned int needbits = SBLIMIT*4;
93
94
95.9k
    NEED_BITS(fr, needbits)
95
48.5k
    needbits = 0;
96
1.60M
    for(i=0;i<SBLIMIT;i++)
97
1.55M
    {
98
1.55M
      *ba = getbits_fast(fr, 4);
99
1.55M
      needbits += (*ba?1:0)*6;
100
1.55M
      ++ba;
101
1.55M
    }
102
103
48.5k
    if(check_balloc(fr, balloc, ba)) return -1;
104
105
18.1k
    ba = balloc;
106
18.1k
    NEED_BITS(fr, needbits)
107
455k
    for (i=0;i<SBLIMIT;i++)
108
442k
      if ((*ba++))
109
123k
        *sca++ = getbits_fast(fr, 6);
110
13.8k
  }
111
112
22.6k
  return 0;
113
334k
}
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
162k
{
120
162k
  int i,n;
121
162k
  int smpb[2*SBLIMIT]; /* values: 0-65535 */
122
162k
  int *sample;
123
162k
  register unsigned int *ba;
124
162k
  register unsigned int *sca = (unsigned int *) scale_index;
125
126
162k
  if(fr->stereo == 2)
127
67.9k
  {
128
67.9k
    unsigned int needbits = 0;
129
67.9k
    int jsbound = fr->jsbound;
130
67.9k
    register real *f0 = fraction[0];
131
67.9k
    register real *f1 = fraction[1];
132
133
67.9k
    ba = balloc;
134
1.33M
    for(sample=smpb,i=0;i<jsbound;i++)
135
1.27M
    {
136
1.27M
      if((n=*ba++))
137
227k
        needbits += n+1;
138
1.27M
      if((n=*ba++))
139
250k
        needbits += n+1;
140
1.27M
    }
141
972k
    for(i=jsbound;i<SBLIMIT;i++) 
142
904k
      if((n = *ba++))
143
220k
        needbits += n+1;
144
67.9k
    NEED_BITS(fr, needbits)
145
146
63.1k
    ba = balloc;
147
1.25M
    for(sample=smpb,i=0;i<jsbound;i++)
148
1.19M
    {
149
1.19M
      if((n = *ba++)) *sample++ = getbits(fr, n+1);
150
151
1.19M
      if((n = *ba++)) *sample++ = getbits(fr, n+1);
152
1.19M
    }
153
886k
    for(i=jsbound;i<SBLIMIT;i++) 
154
823k
    if((n = *ba++))
155
180k
    *sample++ = getbits(fr, n+1);
156
157
63.1k
    ba = balloc;
158
1.25M
    for(sample=smpb,i=0;i<jsbound;i++)
159
1.19M
    {
160
1.19M
      if((n=*ba++))
161
184k
      *f0++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
162
1.01M
      else *f0++ = DOUBLE_TO_REAL(0.0);
163
164
1.19M
      if((n=*ba++))
165
207k
      *f1++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
166
988k
      else *f1++ = DOUBLE_TO_REAL(0.0);
167
1.19M
    }
168
886k
    for(i=jsbound;i<SBLIMIT;i++)
169
823k
    {
170
823k
      if((n=*ba++))
171
180k
      {
172
180k
        real samp = DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1);
173
180k
        *f0++ = REAL_MUL_SCALE_LAYER12(samp, fr->muls[n+1][*sca++]);
174
180k
        *f1++ = REAL_MUL_SCALE_LAYER12(samp, fr->muls[n+1][*sca++]);
175
180k
      }
176
643k
      else *f0++ = *f1++ = DOUBLE_TO_REAL(0.0);
177
823k
    }
178
63.1k
    for(i=fr->down_sample_sblimit;i<32;i++)
179
0
    fraction[0][i] = fraction[1][i] = 0.0;
180
63.1k
  }
181
94.6k
  else
182
94.6k
  {
183
94.6k
    unsigned int needbits = 0;
184
94.6k
    register real *f0 = fraction[0];
185
186
94.6k
    ba = balloc;
187
3.12M
    for(sample=smpb,i=0;i<SBLIMIT;i++)
188
3.02M
      if((n = *ba++))
189
638k
        needbits += n+1;
190
94.6k
    NEED_BITS(fr, needbits);
191
192
84.8k
    ba = balloc;
193
2.80M
    for(sample=smpb,i=0;i<SBLIMIT;i++)
194
2.71M
    if ((n = *ba++))
195
522k
    *sample++ = getbits(fr, n+1);
196
197
198
84.8k
    ba = balloc;
199
2.80M
    for(sample=smpb,i=0;i<SBLIMIT;i++)
200
2.71M
    {
201
2.71M
      if((n=*ba++))
202
522k
      *f0++ = REAL_MUL_SCALE_LAYER12(DOUBLE_TO_REAL_15(MINUS_SHIFT(n) + (*sample++) + 1), fr->muls[n+1][*sca++]);
203
2.19M
      else *f0++ = DOUBLE_TO_REAL(0.0);
204
2.71M
    }
205
84.8k
    for(i=fr->down_sample_sblimit;i<32;i++)
206
0
    fraction[0][i] = DOUBLE_TO_REAL(0.0);
207
84.8k
  }
208
147k
  return 0;
209
162k
}
210
211
int INT123_do_layer1(mpg123_handle *fr)
212
334k
{
213
334k
  int clip=0;
214
334k
  int i,stereo = fr->stereo;
215
334k
  unsigned int balloc[2*SBLIMIT];
216
334k
  unsigned int scale_index[2][SBLIMIT];
217
334k
  real (*fraction)[SBLIMIT] = fr->layer1.fraction; /* fraction[2][SBLIMIT] */
218
334k
  int single = fr->single;
219
220
334k
  fr->jsbound = (fr->hdr.mode == MPG_MD_JOINT_STEREO) ? (fr->hdr.mode_ext<<2)+4 : 32;
221
222
334k
  if(stereo == 1 || single == SINGLE_MIX) /* I don't see mixing handled here */
223
95.9k
  single = SINGLE_LEFT;
224
225
334k
  if(I_step_one(balloc,scale_index,fr))
226
311k
  {
227
311k
    if(NOQUIET)
228
311k
      error("Aborting layer I decoding after step one.");
229
311k
    return clip;
230
311k
  }
231
232
170k
  for(i=0;i<SCALE_BLOCK;i++)
233
162k
  {
234
162k
    if(I_step_two(fraction,balloc,scale_index,fr))
235
14.6k
    {
236
14.6k
      if(NOQUIET)
237
14.6k
        error("Aborting layer I decoding after step two.");
238
14.6k
      return clip;
239
14.6k
    }
240
241
147k
    if(single != SINGLE_STEREO)
242
84.8k
    clip += (fr->synth_mono)(fraction[single], fr);
243
63.1k
    else
244
63.1k
    clip += (fr->synth_stereo)(fraction[0], fraction[1], fr);
245
147k
  }
246
247
8.01k
  return clip;
248
22.6k
}
249
250